2 * neatcc - the neatcc compiler
4 * Copyright (C) 2010-2015 Ali Gholami Rudi
6 * This program is released under the Modified BSD license.
11 * The parser reads tokens from the tokenizer (tok_*) and calls the
12 * appropriate code generation functions (o_*). The generator
13 * maintains a stack of values pushed via, for instance, o_num()
14 * and generates the necessary code for the accesses to the items
15 * in this stack, like o_bop() for performing a binary operations
16 * on the top two items of the stack. The parser maintains the
17 * types of values pushed to the generator stack in its type stack
18 * (ts_*). For instance, for binary operations two types are
19 * popped first and the resulting type is pushed to the type stack
30 #include <sys/types.h>
36 static int nogen
; /* do not generate code, if set */
37 #define o_bop(op) {if (!nogen) o_bop(op);}
38 #define o_uop(op) {if (!nogen) o_uop(op);}
39 #define o_cast(bt) {if (!nogen) o_cast(bt);}
40 #define o_memcpy() {if (!nogen) o_memcpy();}
41 #define o_memset() {if (!nogen) o_memset();}
42 #define o_call(argc, ret) {if (!nogen) o_call(argc, ret);}
43 #define o_ret(ret) {if (!nogen) o_ret(ret);}
44 #define o_assign(bt) {if (!nogen) o_assign(bt);}
45 #define o_deref(bt) {if (!nogen) o_deref(bt);}
46 #define o_load() {if (!nogen) o_load();}
47 #define o_popnum(c) (nogen ? 0 : o_popnum(c))
48 #define o_num(n) {if (!nogen) o_num(n);}
49 #define o_local(addr) {if (!nogen) o_local(addr);}
50 #define o_sym(sym) {if (!nogen) o_sym(sym);}
51 #define o_tmpdrop(n) {if (!nogen) o_tmpdrop(n);}
52 #define o_tmpswap() {if (!nogen) o_tmpswap();}
53 #define o_tmpcopy() {if (!nogen) o_tmpcopy();}
54 #define o_label(id) {if (!nogen) o_label(id);}
55 #define o_jz(id) {if (!nogen) o_jz(id);}
56 #define o_jnz(id) {if (!nogen) o_jnz(id);}
57 #define o_jmp(id) {if (!nogen) o_jmp(id);}
58 #define o_fork() {if (!nogen) o_fork();}
59 #define o_forkpush() {if (!nogen) o_forkpush();}
60 #define o_forkjoin() {if (!nogen) o_forkjoin();}
62 #define ALIGN(x, a) (((x) + (a) - 1) & ~((a) - 1))
63 #define MIN(a, b) ((a) < (b) ? (a) : (b))
64 #define MAX(a, b) ((a) < (b) ? (b) : (a))
66 #define TYPE_BT(t) ((t)->ptr ? LONGSZ : (t)->bt)
67 #define TYPE_SZ(t) ((t)->ptr ? LONGSZ : (t)->bt & BT_SZMASK)
68 #define TYPE_VOID(t) (!(t)->bt && !(t)->flags && !(t)->ptr)
70 /* type->flag values */
75 /* variable definition flags */
83 int id
; /* for structs, functions and arrays */
84 int addr
; /* the address is passed to gen.c; deref for value */
88 static struct type ts
[NTMPS
];
91 static void ts_push_bt(unsigned bt
)
98 o_cast(bt
); /* casting to architecture word */
102 static void ts_push(struct type
*t
)
104 struct type
*d
= &ts
[nts
++];
105 memcpy(d
, t
, sizeof(*t
));
108 static void ts_push_addr(struct type
*t
)
111 ts
[nts
- 1].addr
= 1;
114 static void ts_pop(struct type
*type
)
121 void err(char *fmt
, ...)
126 vsprintf(msg
, fmt
, ap
);
128 die("%s: %s", cpp_loc(tok_addr()), msg
);
133 char elfname
[NAMELEN
]; /* local elf name for function static variables */
135 long addr
; /* local stack offset, global data addr, struct offset */
138 static struct name locals
[NLOCALS
];
140 static struct name globals
[NGLOBALS
];
143 static void local_add(struct name
*name
)
145 if (nlocals
>= NLOCALS
)
146 err("nomem: NLOCALS reached!\n");
147 memcpy(&locals
[nlocals
++], name
, sizeof(*name
));
150 static int local_find(char *name
)
153 for (i
= nlocals
- 1; i
>= 0; --i
)
154 if (!strcmp(locals
[i
].name
, name
))
159 static int global_find(char *name
)
162 for (i
= nglobals
- 1; i
>= 0; i
--)
163 if (!strcmp(name
, globals
[i
].name
))
168 static void global_add(struct name
*name
)
170 if (nglobals
>= NGLOBALS
)
171 err("nomem: NGLOBALS reached!\n");
172 memcpy(&globals
[nglobals
++], name
, sizeof(*name
));
175 #define LABEL() (++label)
177 static int label
; /* last used label id */
178 static int l_break
; /* current break label */
179 static int l_cont
; /* current continue label */
181 static struct enumval
{
187 static void enum_add(char *name
, int val
)
189 struct enumval
*ev
= &enums
[nenums
++];
190 if (nenums
>= NENUMS
)
191 err("nomem: NENUMS reached!\n");
192 strcpy(ev
->name
, name
);
196 static int enum_find(int *val
, char *name
)
199 for (i
= nenums
- 1; i
>= 0; --i
)
200 if (!strcmp(name
, enums
[i
].name
)) {
207 static struct typdefinfo
{
210 } typedefs
[NTYPEDEFS
];
211 static int ntypedefs
;
213 static void typedef_add(char *name
, struct type
*type
)
215 struct typdefinfo
*ti
= &typedefs
[ntypedefs
++];
216 if (ntypedefs
>= NTYPEDEFS
)
217 err("nomem: NTYPEDEFS reached!\n");
218 strcpy(ti
->name
, name
);
219 memcpy(&ti
->type
, type
, sizeof(*type
));
222 static int typedef_find(char *name
)
225 for (i
= ntypedefs
- 1; i
>= 0; --i
)
226 if (!strcmp(name
, typedefs
[i
].name
))
231 static struct array
{
237 static int array_add(struct type
*type
, int n
)
239 struct array
*a
= &arrays
[narrays
++];
240 if (narrays
>= NARRAYS
)
241 err("nomem: NARRAYS reached!\n");
242 memcpy(&a
->type
, type
, sizeof(*type
));
247 static void array2ptr(struct type
*t
)
249 if (t
->flags
& T_ARRAY
&& !t
->ptr
) {
250 memcpy(t
, &arrays
[t
->id
].type
, sizeof(*t
));
255 static struct structinfo
{
257 struct name fields
[NFIELDS
];
264 static int struct_find(char *name
, int isunion
)
267 for (i
= nstructs
- 1; i
>= 0; --i
)
268 if (*structs
[i
].name
&& !strcmp(name
, structs
[i
].name
) &&
269 structs
[i
].isunion
== isunion
)
272 if (nstructs
>= NSTRUCTS
)
273 err("nomem: NSTRUCTS reached!\n");
274 memset(&structs
[i
], 0, sizeof(structs
[i
]));
275 strcpy(structs
[i
].name
, name
);
276 structs
[i
].isunion
= isunion
;
280 static struct name
*struct_field(int id
, char *name
)
282 struct structinfo
*si
= &structs
[id
];
284 for (i
= 0; i
< si
->nfields
; i
++)
285 if (!strcmp(name
, si
->fields
[i
].name
))
286 return &si
->fields
[i
];
287 err("field not found\n");
291 /* return t's size */
292 static int type_totsz(struct type
*t
)
296 if (t
->flags
& T_ARRAY
)
297 return arrays
[t
->id
].n
* type_totsz(&arrays
[t
->id
].type
);
298 return t
->flags
& T_STRUCT
? structs
[t
->id
].size
: BT_SZ(t
->bt
);
301 /* return t's dereferenced size */
302 static unsigned type_szde(struct type
*t
)
307 return type_totsz(&de
);
310 /* dereference stack top if t->addr (ie. address is pushed to gen.c) */
311 static void ts_de(int deref
)
313 struct type
*t
= &ts
[nts
- 1];
315 if (deref
&& t
->addr
&& (t
->ptr
|| !(t
->flags
& T_FUNC
)))
320 /* pop stack pop to *t and dereference if t->addr */
321 static void ts_pop_de(struct type
*t
)
327 /* pop the top 2 stack values and dereference them if t->addr */
328 static void ts_pop_de2(struct type
*t1
, struct type
*t2
)
336 static int tok_jmp(int tok
)
338 if (tok_see() != tok
)
344 static void tok_expect(int tok
)
346 if (tok_get() != tok
)
347 err("syntax error\n");
350 /* the result of a binary operation on variables of type bt1 and bt2 */
351 static unsigned bt_op(unsigned bt1
, unsigned bt2
)
353 int sz
= MAX(BT_SZ(bt1
), BT_SZ(bt2
));
354 return ((bt1
| bt2
) & BT_SIGNED
) | MAX(sz
, 4);
357 /* the result of a unary operation on variables of bt */
358 static unsigned bt_uop(unsigned bt
)
363 /* push the result of a binary operation on the type stack */
364 static void ts_binop(int op
)
367 unsigned bt1
, bt2
, bt
;
368 ts_pop_de2(&t1
, &t2
);
371 bt
= bt_op(bt1
, bt2
);
372 if (op
== O_DIV
|| op
== O_MOD
)
373 bt
= BT(bt2
& BT_SIGNED
, bt
);
374 o_bop(op
| (bt
& BT_SIGNED
? O_SIGNED
: 0));
378 /* push the result of an additive binary operation on the type stack */
379 static void ts_addop(int op
)
382 ts_pop_de2(&t1
, &t2
);
383 if (!t1
.ptr
&& !t2
.ptr
) {
385 ts_push_bt(bt_op(TYPE_BT(&t1
), TYPE_BT(&t2
)));
388 if (t1
.ptr
&& !t2
.ptr
)
390 if (!t1
.ptr
&& t2
.ptr
)
391 if (type_szde(&t2
) > 1) {
392 o_num(type_szde(&t2
));
395 if (t1
.ptr
&& !t2
.ptr
)
398 if (t1
.ptr
&& t2
.ptr
) {
399 int sz
= type_szde(&t1
);
404 ts_push_bt(LONGSZ
| BT_SIGNED
);
406 ts_push(t1
.ptr
? &t1
: &t2
);
410 /* function prototypes for parsing function and variable declarations */
411 static int readname(struct type
*main
, char *name
, struct type
*base
);
412 static int readtype(struct type
*type
);
413 static int readdefs(void (*def
)(void *data
, struct name
*name
, unsigned flags
),
415 static int readdefs_int(void (*def
)(void *data
, struct name
*name
, unsigned flags
),
418 /* function prototypes for parsing initializer expressions */
419 static int initsize(void);
420 static void initexpr(struct type
*t
, int off
, void *obj
,
421 void (*set
)(void *obj
, int off
, struct type
*t
));
423 static int type_alignment(struct type
*t
)
425 if (t
->flags
& T_ARRAY
&& !t
->ptr
)
426 return type_alignment(&arrays
[t
->id
].type
);
427 if (t
->flags
& T_STRUCT
&& !t
->ptr
)
428 return type_alignment(&structs
[t
->id
].fields
[0].type
);
429 return MIN(LONGSZ
, type_totsz(t
));
432 static void structdef(void *data
, struct name
*name
, unsigned flags
)
434 struct structinfo
*si
= data
;
437 if (si
->size
< type_totsz(&name
->type
))
438 si
->size
= type_totsz(&name
->type
);
440 struct type
*t
= &name
->type
;
441 int alignment
= type_alignment(t
);
442 if (t
->flags
& T_ARRAY
&& !t
->ptr
)
443 alignment
= MIN(LONGSZ
, type_totsz(&arrays
[t
->id
].type
));
444 si
->size
= ALIGN(si
->size
, alignment
);
445 name
->addr
= si
->size
;
446 si
->size
+= type_totsz(&name
->type
);
448 memcpy(&si
->fields
[si
->nfields
++], name
, sizeof(*name
));
451 static int struct_create(char *name
, int isunion
)
453 int id
= struct_find(name
, isunion
);
454 struct structinfo
*si
= &structs
[id
];
456 while (tok_jmp('}')) {
457 readdefs(structdef
, si
);
463 static void readexpr(void);
465 static void enum_create(void)
469 while (tok_jmp('}')) {
471 tok_expect(TOK_NAME
);
472 strcpy(name
, tok_id());
477 err("const expr expected!\n");
484 /* used to differentiate labels from case and cond exprs */
488 static void readpre(void);
490 static char *tmp_str(char *buf
, int len
)
492 static char name
[NAMELEN
];
494 sprintf(name
, "__neatcc.s%d", id
++);
495 o_dscpy(o_dsnew(name
, len
, 0), buf
, len
);
499 static void readprimary(void)
501 if (!tok_jmp(TOK_NUM
)) {
503 int bt
= tok_num(&n
);
508 if (!tok_jmp(TOK_STR
)) {
509 struct type t
= {}; /* char type inside the arrays */
510 struct type a
= {}; /* the char array type */
514 t
.bt
= 1 | BT_SIGNED
;
515 a
.id
= array_add(&t
, len
);
517 o_sym(tmp_str(buf
, len
));
521 if (!tok_jmp(TOK_NAME
)) {
522 struct name unkn
= {""};
523 char *name
= unkn
.name
;
525 strcpy(name
, tok_id());
526 /* don't search for labels here */
527 if (!ncexpr
&& !caseexpr
&& tok_see() == ':')
529 if ((n
= local_find(name
)) != -1) {
530 struct name
*l
= &locals
[n
];
532 ts_push_addr(&l
->type
);
535 if ((n
= global_find(name
)) != -1) {
536 struct name
*g
= &globals
[n
];
537 o_sym(*g
->elfname
? g
->elfname
: g
->name
);
538 ts_push_addr(&g
->type
);
541 if (!enum_find(&n
, name
)) {
543 ts_push_bt(4 | BT_SIGNED
);
546 if (tok_see() != '(')
547 err("unknown symbol <%s>\n", name
);
561 if (!t
.ptr
|| !o
.ptr
)
565 while (tok_jmp(')')) {
576 static void arrayderef(void)
582 if (!(t
.flags
& T_ARRAY
&& !t
.ptr
) && t
.addr
) {
584 o_deref(TYPE_BT(&t
));
599 static void inc_post(int op
)
601 struct type t
= ts
[nts
- 1];
602 /* pushing the value before inc */
608 /* increment by 1 or pointer size */
612 o_num(t
.ptr
> 0 ? type_szde(&t
) : 1);
616 o_assign(TYPE_BT(&t
));
620 static void readfield(void)
624 tok_expect(TOK_NAME
);
627 field
= struct_field(t
.id
, tok_id());
632 ts_push_addr(&field
->type
);
635 static struct funcinfo
{
636 struct type args
[NARGS
];
640 /* function and argument names; useful only when defining */
641 char argnames
[NARGS
][NAMELEN
];
646 static int func_create(struct type
*ret
, char *name
, char argnames
[][NAMELEN
],
647 struct type
*args
, int nargs
, int varg
)
649 struct funcinfo
*fi
= &funcs
[nfuncs
++];
651 if (nfuncs
>= NFUNCS
)
652 err("nomem: NFUNCS reached!\n");
653 memcpy(&fi
->ret
, ret
, sizeof(*ret
));
654 for (i
= 0; i
< nargs
; i
++)
655 memcpy(&fi
->args
[i
], &args
[i
], sizeof(*ret
));
658 strcpy(fi
->name
, name
? name
: "");
659 for (i
= 0; i
< nargs
; i
++)
660 strcpy(fi
->argnames
[i
], argnames
[i
]);
664 static void readcall(void)
670 if (t
.flags
& T_FUNC
&& t
.ptr
> 0)
672 fi
= t
.flags
& T_FUNC
? &funcs
[t
.id
] : NULL
;
673 if (tok_see() != ')') {
678 } while (!tok_jmp(','));
681 o_call(argc
, fi
? TYPE_BT(&fi
->ret
) : 4 | BT_SIGNED
);
683 if (TYPE_BT(&fi
->ret
))
684 o_cast(TYPE_BT(&fi
->ret
));
687 ts_push_bt(4 | BT_SIGNED
);
691 static void readpost(void)
705 if (!tok_jmp(TOK2("++"))) {
709 if (!tok_jmp(TOK2("--"))) {
717 if (!tok_jmp(TOK2("->"))) {
726 static void inc_pre(int op
)
730 /* copy the destination */
732 ts_push(&ts
[nts
- 1]);
733 /* increment by 1 or pointer size */
735 o_num(t
.ptr
> 0 ? type_szde(&t
) : 1);
737 /* assign the result */
738 o_assign(TYPE_BT(&t
));
742 static void readpre(void)
749 err("cannot use the address\n");
760 err("dereferencing non-pointer\n");
762 o_deref(TYPE_BT(&t
));
772 ts_push_bt(4 | BT_SIGNED
);
779 ts_push_bt(bt_uop(TYPE_BT(&t
)));
787 ts_push_bt(bt_uop(TYPE_BT(&t
)));
795 ts_push_bt(bt_uop(TYPE_BT(&t
)));
798 if (!tok_jmp(TOK2("++"))) {
802 if (!tok_jmp(TOK2("--"))) {
806 if (!tok_jmp(TOK_SIZEOF
)) {
808 int op
= !tok_jmp('(');
818 o_num(type_totsz(&t
));
827 static void readmul(void)
850 static void readadd(void)
868 static void shift(int op
)
872 ts_pop_de2(NULL
, &t
);
873 o_bop(op
| (BT_SIGNED
& TYPE_BT(&t
) ? O_SIGNED
: 0));
874 ts_push_bt(bt_uop(TYPE_BT(&t
)));
877 static void readshift(void)
881 if (!tok_jmp(TOK2("<<"))) {
885 if (!tok_jmp(TOK2(">>"))) {
893 static void cmp(int op
)
898 ts_pop_de2(&t1
, &t2
);
899 bt
= bt_op(TYPE_BT(&t1
), TYPE_BT(&t2
));
900 o_bop(op
| (bt
& BT_SIGNED
? O_SIGNED
: 0));
901 ts_push_bt(4 | BT_SIGNED
);
904 static void readcmp(void)
916 if (!tok_jmp(TOK2("<="))) {
920 if (!tok_jmp(TOK2(">="))) {
928 static void eq(int op
)
931 ts_pop_de2(NULL
, NULL
);
933 ts_push_bt(4 | BT_SIGNED
);
936 static void readeq(void)
940 if (!tok_jmp(TOK2("=="))) {
944 if (!tok_jmp(TOK2("!="))) {
952 static void readbitand(void)
955 while (!tok_jmp('&')) {
961 static void readxor(void)
964 while (!tok_jmp('^')) {
970 static void readbitor(void)
973 while (!tok_jmp('|')) {
979 static void readand(void)
983 if (tok_see() != TOK2("&&"))
990 while (!tok_jmp(TOK2("&&"))) {
1003 ts_push_bt(4 | BT_SIGNED
);
1006 static void reador(void)
1010 if (tok_see() != TOK2("||"))
1017 while (!tok_jmp(TOK2("||"))) {
1030 ts_push_bt(4 | BT_SIGNED
);
1033 static void readcexpr(void);
1035 static int readcexpr_const(void)
1043 /* both branches yield the same type; so ignore the first */
1051 /* making sure t->addr == 0 on both branches */
1058 static void readcexpr(void)
1066 if (readcexpr_const()) {
1067 int l_fail
= LABEL();
1068 int l_end
= LABEL();
1072 /* both branches yield the same type; so ignore the first */
1074 if (!TYPE_VOID(&ret
))
1081 /* making sure t->addr == 0 on both branches */
1083 if (!TYPE_VOID(&ret
)) {
1092 static void opassign(int op
, int ptrop
)
1094 struct type t
= ts
[nts
- 1];
1098 if (op
== O_ADD
|| op
== O_SUB
)
1102 o_assign(TYPE_BT(&ts
[nts
- 2]));
1107 static void doassign(void)
1109 struct type t
= ts
[nts
- 1];
1110 if (!t
.ptr
&& t
.flags
& T_STRUCT
) {
1112 o_num(type_totsz(&t
));
1116 o_assign(TYPE_BT(&ts
[nts
- 1]));
1121 static void readexpr(void)
1124 if (!tok_jmp('=')) {
1129 if (!tok_jmp(TOK2("+="))) {
1133 if (!tok_jmp(TOK2("-="))) {
1137 if (!tok_jmp(TOK2("*="))) {
1141 if (!tok_jmp(TOK2("/="))) {
1145 if (!tok_jmp(TOK2("%="))) {
1149 if (!tok_jmp(TOK3("<<="))) {
1153 if (!tok_jmp(TOK3(">>="))) {
1157 if (!tok_jmp(TOK3("&="))) {
1161 if (!tok_jmp(TOK3("|="))) {
1165 if (!tok_jmp(TOK3("^="))) {
1171 static void readestmt(void)
1177 } while (!tok_jmp(','));
1180 #define F_GLOBAL(flags) (!((flags) & F_STATIC))
1182 static void globalinit(void *obj
, int off
, struct type
*t
)
1184 struct name
*name
= obj
;
1185 char *elfname
= *name
->elfname
? name
->elfname
: name
->name
;
1186 if (t
->flags
& T_ARRAY
&& tok_see() == TOK_STR
) {
1187 struct type
*t_de
= &arrays
[t
->id
].type
;
1188 if (!t_de
->ptr
&& !t_de
->flags
&& TYPE_SZ(t_de
) == 1) {
1191 tok_expect(TOK_STR
);
1192 tok_str(&buf
, &len
);
1193 o_dscpy(name
->addr
+ off
, buf
, len
);
1198 o_dsset(elfname
, off
, TYPE_BT(t
));
1202 static void readfunc(struct name
*name
, int flags
);
1204 static void globaldef(void *data
, struct name
*name
, unsigned flags
)
1206 struct type
*t
= &name
->type
;
1207 char *elfname
= *name
->elfname
? name
->elfname
: name
->name
;
1209 if (t
->flags
& T_ARRAY
&& !t
->ptr
&& !arrays
[t
->id
].n
)
1210 if (~flags
& F_EXTERN
)
1211 arrays
[t
->id
].n
= initsize();
1213 if (!(flags
& F_EXTERN
) && (!(t
->flags
& T_FUNC
) || t
->ptr
)) {
1214 if (tok_see() == '=')
1215 name
->addr
= o_dsnew(elfname
, sz
, F_GLOBAL(flags
));
1217 o_bsnew(elfname
, sz
, F_GLOBAL(flags
));
1221 initexpr(t
, 0, name
, globalinit
);
1222 if (tok_see() == '{' && name
->type
.flags
& T_FUNC
)
1223 readfunc(name
, flags
);
1226 /* generate the address of local + off */
1227 static void o_localoff(long addr
, int off
)
1236 static void localinit(void *obj
, int off
, struct type
*t
)
1238 long addr
= *(long *) obj
;
1239 if (t
->flags
& T_ARRAY
&& tok_see() == TOK_STR
) {
1240 struct type
*t_de
= &arrays
[t
->id
].type
;
1241 if (!t_de
->ptr
&& !t_de
->flags
&& TYPE_SZ(t_de
) == 1) {
1244 tok_expect(TOK_STR
);
1245 tok_str(&buf
, &len
);
1246 o_localoff(addr
, off
);
1247 o_sym(tmp_str(buf
, len
));
1254 o_localoff(addr
, off
);
1262 /* current function name */
1263 static char func_name
[NAMELEN
];
1265 static void localdef(void *data
, struct name
*name
, unsigned flags
)
1267 struct type
*t
= &name
->type
;
1268 if ((flags
& F_EXTERN
) || ((t
->flags
& T_FUNC
) && !t
->ptr
)) {
1272 if (flags
& F_STATIC
) {
1273 sprintf(name
->elfname
, "__neatcc.%s.%s", func_name
, name
->name
);
1274 globaldef(data
, name
, flags
);
1277 if (t
->flags
& T_ARRAY
&& !t
->ptr
&& !arrays
[t
->id
].n
)
1278 arrays
[t
->id
].n
= initsize();
1279 name
->addr
= o_mklocal(type_totsz(&name
->type
));
1281 if (!tok_jmp('=')) {
1282 if (t
->flags
& (T_ARRAY
| T_STRUCT
) && !t
->ptr
) {
1283 o_local(name
->addr
);
1285 o_num(type_totsz(t
));
1289 initexpr(t
, 0, &name
->addr
, localinit
);
1293 static void typedefdef(void *data
, struct name
*name
, unsigned flags
)
1295 typedef_add(name
->name
, &name
->type
);
1298 static void readstmt(void);
1300 static void readswitch(void)
1302 int o_break
= l_break
;
1303 long val_addr
= o_mklocal(LONGSZ
);
1305 int ncases
= 0; /* number of case labels */
1306 int l_failed
= LABEL(); /* address of last failed jmp */
1307 int l_matched
= LABEL(); /* address of last walk through jmp */
1308 int l_default
= 0; /* default case label */
1315 o_assign(TYPE_BT(&t
));
1320 while (tok_jmp('}')) {
1321 if (tok_see() != TOK_CASE
&& tok_see() != TOK_DEFAULT
) {
1327 if (tok_get() == TOK_CASE
) {
1335 o_deref(TYPE_BT(&t
));
1342 l_default
= LABEL();
1347 l_matched
= LABEL();
1350 o_rmlocal(val_addr
, LONGSZ
);
1359 static char label_name
[NLABELS
][NAMELEN
];
1360 static int label_ids
[NLABELS
];
1363 static int label_id(char *name
)
1366 for (i
= nlabels
- 1; i
>= 0; --i
)
1367 if (!strcmp(label_name
[i
], name
))
1368 return label_ids
[i
];
1369 strcpy(label_name
[nlabels
], name
);
1370 label_ids
[nlabels
] = LABEL();
1371 return label_ids
[nlabels
++];
1374 static void readstmt(void)
1378 if (!tok_jmp('{')) {
1379 int _nlocals
= nlocals
;
1380 int _nglobals
= nglobals
;
1381 int _nenums
= nenums
;
1382 int _ntypedefs
= ntypedefs
;
1383 int _nstructs
= nstructs
;
1384 int _nfuncs
= nfuncs
;
1385 int _narrays
= narrays
;
1386 while (tok_jmp('}'))
1390 ntypedefs
= _ntypedefs
;
1391 nstructs
= _nstructs
;
1394 nglobals
= _nglobals
;
1397 if (!readdefs(localdef
, NULL
)) {
1401 if (!tok_jmp(TOK_TYPEDEF
)) {
1402 readdefs(typedefdef
, NULL
);
1406 if (!tok_jmp(TOK_IF
)) {
1407 int l_fail
= LABEL();
1408 int l_end
= LABEL();
1415 if (!tok_jmp(TOK_ELSE
)) {
1425 if (!tok_jmp(TOK_WHILE
)) {
1426 int o_break
= l_break
;
1427 int o_cont
= l_cont
;
1443 if (!tok_jmp(TOK_DO
)) {
1444 int o_break
= l_break
;
1445 int o_cont
= l_cont
;
1446 int l_beg
= LABEL();
1451 tok_expect(TOK_WHILE
);
1464 if (!tok_jmp(TOK_FOR
)) {
1465 int o_break
= l_break
;
1466 int o_cont
= l_cont
;
1467 int l_check
= LABEL(); /* for condition label */
1468 int l_body
= LABEL(); /* for block label */
1472 if (tok_see() != ';')
1476 if (tok_see() != ';') {
1484 if (tok_see() != ')')
1496 if (!tok_jmp(TOK_SWITCH
)) {
1500 if (!tok_jmp(TOK_RETURN
)) {
1501 int ret
= tok_see() != ';';
1510 if (!tok_jmp(TOK_BREAK
)) {
1515 if (!tok_jmp(TOK_CONTINUE
)) {
1520 if (!tok_jmp(TOK_GOTO
)) {
1521 tok_expect(TOK_NAME
);
1522 o_jmp(label_id(tok_id()));
1528 if (!tok_jmp(':')) {
1529 o_label(label_id(tok_id()));
1535 static void readfunc(struct name
*name
, int flags
)
1537 struct funcinfo
*fi
= &funcs
[name
->type
.id
];
1538 long beg
= tok_addr();
1540 strcpy(func_name
, fi
->name
);
1541 o_func_beg(func_name
, fi
->nargs
, F_GLOBAL(flags
), fi
->varg
);
1542 for (i
= 0; i
< fi
->nargs
; i
++) {
1543 struct name arg
= {"", "", fi
->args
[i
], o_arg2loc(i
)};
1544 strcpy(arg
.name
, fi
->argnames
[i
]);
1547 /* first pass: collecting statistics */
1553 /* second pass: generating code */
1559 func_name
[0] = '\0';
1563 static void readdecl(void)
1565 if (!tok_jmp(TOK_TYPEDEF
)) {
1566 readdefs(typedefdef
, NULL
);
1570 readdefs_int(globaldef
, NULL
);
1574 static void parse(void)
1576 while (tok_see() != TOK_EOF
)
1580 static void compat_macros(void)
1582 cpp_define("__STDC__", "");
1583 cpp_define("__linux__", "");
1584 cpp_define(I_ARCH
, "");
1586 /* ignored keywords */
1587 cpp_define("const", "");
1588 cpp_define("register", "");
1589 cpp_define("volatile", "");
1590 cpp_define("inline", "");
1591 cpp_define("restrict", "");
1592 cpp_define("__inline__", "");
1593 cpp_define("__restrict__", "");
1594 cpp_define("__attribute__(x)", "");
1595 cpp_define("__builtin_va_list__", "long");
1598 int main(int argc
, char *argv
[])
1604 while (i
< argc
&& argv
[i
][0] == '-') {
1605 if (argv
[i
][1] == 'I')
1606 cpp_addpath(argv
[i
][2] ? argv
[i
] + 2 : argv
[++i
]);
1607 if (argv
[i
][1] == 'D') {
1608 char *name
= argv
[i
] + 2;
1610 char *eq
= strchr(name
, '=');
1615 cpp_define(name
, def
);
1617 if (argv
[i
][1] == 'o')
1618 strcpy(obj
, argv
[i
][2] ? argv
[i
] + 2 : argv
[++i
]);
1622 die("neatcc: no file given\n");
1623 if (cpp_init(argv
[i
]))
1624 die("neatcc: cannot open <%s>\n", argv
[i
]);
1627 strcpy(obj
, argv
[i
]);
1628 obj
[strlen(obj
) - 1] = 'o';
1630 ofd
= open(obj
, O_WRONLY
| O_TRUNC
| O_CREAT
, 0600);
1637 /* parsing function and variable declarations */
1639 /* read the base type of a variable */
1640 static int basetype(struct type
*type
, unsigned *flags
)
1647 char name
[NAMELEN
] = "";
1653 switch (tok_see()) {
1685 isunion
= tok_get() == TOK_UNION
;
1686 if (!tok_jmp(TOK_NAME
))
1687 strcpy(name
, tok_id());
1688 if (tok_see() == '{')
1689 type
->id
= struct_create(name
, isunion
);
1691 type
->id
= struct_find(name
, isunion
);
1692 type
->flags
|= T_STRUCT
;
1698 if (tok_see() == '{')
1700 type
->bt
= 4 | BT_SIGNED
;
1703 if (tok_see() == TOK_NAME
) {
1704 int id
= typedef_find(tok_id());
1707 memcpy(type
, &typedefs
[id
].type
,
1720 type
->bt
= size
| (sign
? BT_SIGNED
: 0);
1724 static void readptrs(struct type
*type
)
1726 while (!tok_jmp('*')) {
1733 /* read function arguments */
1734 static int readargs(struct type
*args
, char argnames
[][NAMELEN
], int *varg
)
1739 while (tok_see() != ')') {
1740 if (!tok_jmp(TOK3("..."))) {
1744 if (readname(&args
[nargs
], argnames
[nargs
], NULL
)) {
1745 /* argument has no type, assume int */
1746 tok_expect(TOK_NAME
);
1747 memset(&args
[nargs
], 0, sizeof(struct type
));
1748 args
[nargs
].bt
= 4 | BT_SIGNED
;
1749 strcpy(argnames
[nargs
], tok_id());
1751 /* argument arrays are pointers */
1752 array2ptr(&args
[nargs
]);
1759 if (nargs
== 1 && !TYPE_BT(&args
[0]))
1764 /* read K&R function arguments */
1765 static void krdef(void *data
, struct name
*name
, unsigned flags
)
1767 struct funcinfo
*fi
= data
;
1769 for (i
= 0; i
< fi
->nargs
; i
++)
1770 if (!strcmp(fi
->argnames
[i
], name
->name
))
1771 memcpy(&fi
->args
[i
], &name
->type
, sizeof(name
->type
));
1775 * readarrays() parses array specifiers when reading a definition in
1776 * readname(). The "type" parameter contains the type contained in the
1777 * inner array; for instance, type in "int *a[10][20]" would be an int
1778 * pointer. When returning, the "type" parameter is changed to point
1779 * to the final array. The function returns a pointer to the type in
1780 * the inner array; this is useful when the type is not complete yet,
1781 * like when creating an array of function pointers as in
1782 * "int (*f[10])(int)". If there is no array brackets, NULL is returned.
1784 static struct type
*readarrays(struct type
*type
)
1787 struct type
*inner
= NULL
;
1790 while (!tok_jmp('[')) {
1796 err("const expr expected\n");
1801 for (i
= nar
- 1; i
>= 0; i
--) {
1802 type
->id
= array_add(type
, arsz
[i
]);
1804 inner
= &arrays
[type
->id
].type
;
1805 type
->flags
= T_ARRAY
;
1813 * readname() reads a variable definition; the name is copied into
1814 * "name" and the type is copied into "main" argument. The "base"
1815 * argument, if not NULL, indicates the base type of the variable.
1816 * For instance, the base type of "a" and "b" in "int *a, b[10]" is
1817 * "int". If NULL, basetype() is called directly to read the base
1818 * type of the variable. readname() returns zero, only if the
1819 * variable can be read.
1821 static int readname(struct type
*main
, char *name
, struct type
*base
)
1823 struct type tpool
[3];
1825 struct type
*type
= &tpool
[npool
++];
1826 struct type
*ptype
= NULL
; /* type inside parenthesis */
1827 struct type
*btype
= NULL
; /* type before parenthesis */
1830 memset(tpool
, 0, sizeof(tpool
));
1834 if (basetype(type
, &flags
))
1837 memcpy(type
, base
, sizeof(*base
));
1840 if (!tok_jmp('(')) {
1842 type
= &tpool
[npool
++];
1846 if (!tok_jmp(TOK_NAME
) && name
)
1847 strcpy(name
, tok_id());
1848 inner
= readarrays(type
);
1853 if (tok_see() == '(') {
1854 struct type args
[NARGS
];
1855 char argnames
[NARGS
][NAMELEN
];
1857 int nargs
= readargs(args
, argnames
, &varg
);
1860 type
= &tpool
[npool
++];
1863 ptype
->flags
= T_FUNC
;
1865 ptype
->id
= func_create(btype
, name
, argnames
, args
, nargs
, varg
);
1866 if (tok_see() != ';')
1867 while (tok_see() != '{' && !readdefs(krdef
, &funcs
[ptype
->id
]))
1870 if (ptype
&& readarrays(type
))
1873 memcpy(main
, type
, sizeof(*type
));
1877 static int readtype(struct type
*type
)
1879 return readname(type
, NULL
, NULL
);
1883 * readdef() reads a variable definitions statement. The definition
1884 * statement can appear in anywhere: global variables, function
1885 * local variables, struct fields, and typedefs. For each defined
1886 * variable, def() callback is called with the appropriate name
1887 * struct and flags; the callback should finish parsing the definition
1888 * by possibly reading the initializer expression and saving the name
1891 static int readdefs(void (*def
)(void *data
, struct name
*name
, unsigned flags
),
1895 unsigned base_flags
;
1896 if (basetype(&base
, &base_flags
))
1898 if (tok_see() == ';' || tok_see() == '{')
1901 struct name name
= {{""}};
1902 if (readname(&name
.type
, name
.name
, &base
))
1904 def(data
, &name
, base_flags
);
1905 } while (!tok_jmp(','));
1909 /* just like readdefs, but default to int type; for handling K&R functions */
1910 static int readdefs_int(void (*def
)(void *data
, struct name
*name
, unsigned flags
),
1915 if (basetype(&base
, &flags
)) {
1916 if (tok_see() != TOK_NAME
)
1918 memset(&base
, 0, sizeof(base
));
1919 base
.bt
= 4 | BT_SIGNED
;
1921 if (tok_see() != ';') {
1923 struct name name
= {{""}};
1924 if (readname(&name
.type
, name
.name
, &base
))
1926 def(data
, &name
, flags
);
1927 } while (!tok_jmp(','));
1933 /* parsing initializer expressions */
1935 static void jumpbrace(void)
1938 while (tok_see() != '}' || depth
--)
1939 if (tok_get() == '{')
1944 /* compute the size of the initializer expression */
1945 static int initsize(void)
1947 long addr
= tok_addr();
1951 if (!tok_jmp(TOK_STR
)) {
1957 while (tok_jmp('}')) {
1959 if (!tok_jmp('[')) {
1968 while (tok_see() != '}' && tok_see() != ',')
1969 if (tok_get() == '{')
1977 static struct type
*innertype(struct type
*t
)
1979 if (t
->flags
& T_ARRAY
&& !t
->ptr
)
1980 return innertype(&arrays
[t
->id
].type
);
1984 /* read the initializer expression and initialize basic types using set() cb */
1985 static void initexpr(struct type
*t
, int off
, void *obj
,
1986 void (*set
)(void *obj
, int off
, struct type
*t
))
1992 if (!t
->ptr
&& t
->flags
& T_STRUCT
) {
1993 struct structinfo
*si
= &structs
[t
->id
];
1995 for (i
= 0; i
< si
->nfields
&& tok_see() != '}'; i
++) {
1996 struct name
*field
= &si
->fields
[i
];
1997 if (!tok_jmp('.')) {
1998 tok_expect(TOK_NAME
);
1999 field
= struct_field(t
->id
, tok_id());
2002 initexpr(&field
->type
, off
+ field
->addr
, obj
, set
);
2006 } else if (t
->flags
& T_ARRAY
) {
2007 struct type
*t_de
= &arrays
[t
->id
].type
;
2009 /* handling extra braces as in: char s[] = {"sth"} */
2010 if (TYPE_SZ(t_de
) == 1 && tok_see() == TOK_STR
) {
2015 for (i
= 0; tok_see() != '}'; i
++) {
2017 struct type
*it
= t_de
;
2018 if (!tok_jmp('[')) {
2025 if (tok_see() != '{' && (tok_see() != TOK_STR
||
2026 !(it
->flags
& T_ARRAY
)))
2027 it
= innertype(t_de
);
2028 initexpr(it
, off
+ type_totsz(it
) * idx
, obj
, set
);