2 * THE NEATCC C COMPILER
4 * Copyright (C) 2010-2016 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
31 #include <sys/types.h>
34 #define ALIGN(x, a) (((x) + (a) - 1) & ~((a) - 1))
35 #define MIN(a, b) ((a) < (b) ? (a) : (b))
36 #define MAX(a, b) ((a) < (b) ? (b) : (a))
38 #define TYPE_BT(t) ((t)->ptr ? ULNG : (t)->bt)
39 #define TYPE_SZ(t) ((t)->ptr ? ULNG : (t)->bt & T_MSIZE)
40 #define TYPE_VOID(t) (!(t)->bt && !(t)->flags && !(t)->ptr)
42 /* type->flag values */
47 /* variable definition flags */
55 int id
; /* for structs, functions and arrays */
56 int addr
; /* the address is passed to gen.c; deref for value */
60 static struct type ts
[NTMPS
];
63 static void ts_push_bt(unsigned bt
)
70 o_cast(bt
); /* casting to architecture word */
74 static void ts_push(struct type
*t
)
76 struct type
*d
= &ts
[nts
++];
77 memcpy(d
, t
, sizeof(*t
));
80 static void ts_push_addr(struct type
*t
)
86 static void ts_pop(struct type
*type
)
93 void err(char *fmt
, ...)
98 vsprintf(msg
, fmt
, ap
);
100 die("%s: %s", cpp_loc(tok_addr()), msg
);
103 void *mextend(void *old
, long oldsz
, long newsz
, long memsz
)
105 void *new = malloc(newsz
* memsz
);
106 memcpy(new, old
, oldsz
* memsz
);
107 memset(new + oldsz
* memsz
, 0, (newsz
- oldsz
) * memsz
);
114 char elfname
[NAMELEN
]; /* local elf name for function static variables */
116 long addr
; /* local stack offset, global data addr, struct offset */
119 static struct name
*locals
;
120 static int locals_n
, locals_sz
;
121 static struct name
*globals
;
122 static int globals_n
, globals_sz
;
124 static void local_add(struct name
*name
)
126 if (locals_n
>= locals_sz
) {
127 locals_sz
= MAX(128, locals_sz
* 2);
128 locals
= mextend(locals
, locals_n
, locals_sz
, sizeof(locals
[0]));
130 memcpy(&locals
[locals_n
++], name
, sizeof(*name
));
133 static int local_find(char *name
)
136 for (i
= locals_n
- 1; i
>= 0; --i
)
137 if (!strcmp(locals
[i
].name
, name
))
142 static int global_find(char *name
)
145 for (i
= globals_n
- 1; i
>= 0; i
--)
146 if (!strcmp(name
, globals
[i
].name
))
151 static void global_add(struct name
*name
)
153 if (globals_n
>= globals_sz
) {
154 globals_sz
= MAX(128, globals_sz
* 2);
155 globals
= mextend(globals
, globals_n
, globals_sz
, sizeof(globals
[0]));
157 memcpy(&globals
[globals_n
++], name
, sizeof(*name
));
160 #define LABEL() (++label)
162 static int label
; /* last used label id */
163 static int l_break
; /* current break label */
164 static int l_cont
; /* current continue label */
166 static struct enumval
{
170 static int enums_n
, enums_sz
;
172 static void enum_add(char *name
, int val
)
175 if (enums_n
>= enums_sz
) {
176 enums_sz
= MAX(128, enums_sz
* 2);
177 enums
= mextend(enums
, enums_n
, enums_sz
, sizeof(enums
[0]));
179 ev
= &enums
[enums_n
++];
180 strcpy(ev
->name
, name
);
184 static int enum_find(int *val
, char *name
)
187 for (i
= enums_n
- 1; i
>= 0; --i
)
188 if (!strcmp(name
, enums
[i
].name
)) {
195 static struct typdefinfo
{
199 static int typedefs_n
, typedefs_sz
;
201 static void typedef_add(char *name
, struct type
*type
)
203 struct typdefinfo
*ti
;
204 if (typedefs_n
>= typedefs_sz
) {
205 typedefs_sz
= MAX(128, typedefs_sz
* 2);
206 typedefs
= mextend(typedefs
, typedefs_n
, typedefs_sz
,
207 sizeof(typedefs
[0]));
209 ti
= &typedefs
[typedefs_n
++];
210 strcpy(ti
->name
, name
);
211 memcpy(&ti
->type
, type
, sizeof(*type
));
214 static int typedef_find(char *name
)
217 for (i
= typedefs_n
- 1; i
>= 0; --i
)
218 if (!strcmp(name
, typedefs
[i
].name
))
223 static struct array
{
227 static int arrays_n
, arrays_sz
;
229 static int array_add(struct type
*type
, int n
)
232 if (arrays_n
>= arrays_sz
) {
233 arrays_sz
= MAX(128, arrays_sz
* 2);
234 arrays
= mextend(arrays
, arrays_n
, arrays_sz
, sizeof(arrays
[0]));
236 a
= &arrays
[arrays_n
++];
237 memcpy(&a
->type
, type
, sizeof(*type
));
242 static void array2ptr(struct type
*t
)
244 if (t
->flags
& T_ARRAY
&& !t
->ptr
) {
245 memcpy(t
, &arrays
[t
->id
].type
, sizeof(*t
));
250 static struct structinfo
{
252 struct name fields
[NFIELDS
];
257 static int structs_n
, structs_sz
;
259 static int struct_find(char *name
, int isunion
)
262 for (i
= structs_n
- 1; i
>= 0; --i
)
263 if (*structs
[i
].name
&& !strcmp(name
, structs
[i
].name
) &&
264 structs
[i
].isunion
== isunion
)
266 if (structs_n
>= structs_sz
) {
267 structs_sz
= MAX(128, structs_sz
* 2);
268 structs
= mextend(structs
, structs_n
, structs_sz
, sizeof(structs
[0]));
271 memset(&structs
[i
], 0, sizeof(structs
[i
]));
272 strcpy(structs
[i
].name
, name
);
273 structs
[i
].isunion
= isunion
;
277 static struct name
*struct_field(int id
, char *name
)
279 struct structinfo
*si
= &structs
[id
];
281 for (i
= 0; i
< si
->nfields
; i
++)
282 if (!strcmp(name
, si
->fields
[i
].name
))
283 return &si
->fields
[i
];
284 err("unknown field <%s>\n", name
);
288 /* return t's size */
289 static int type_totsz(struct type
*t
)
293 if (t
->flags
& T_ARRAY
)
294 return arrays
[t
->id
].n
* type_totsz(&arrays
[t
->id
].type
);
295 return t
->flags
& T_STRUCT
? structs
[t
->id
].size
: T_SZ(t
->bt
);
298 /* return t's dereferenced size */
299 static unsigned type_szde(struct type
*t
)
304 return type_totsz(&de
);
307 /* dereference stack top if t->addr (ie. address is pushed to gen.c) */
308 static void ts_de(int deref
)
310 struct type
*t
= &ts
[nts
- 1];
312 if (deref
&& t
->addr
&& (t
->ptr
|| !(t
->flags
& T_FUNC
)))
317 /* pop stack pop to *t and dereference if t->addr */
318 static void ts_pop_de(struct type
*t
)
324 /* pop the top 2 stack values and dereference them if t->addr */
325 static void ts_pop_de2(struct type
*t1
, struct type
*t2
)
333 /* the previous identifier; to handle labels */
334 static char tok_previden
[NAMELEN
];
336 static char *tok_iden(void)
338 snprintf(tok_previden
, sizeof(tok_previden
), "%s", tok_get());
342 static int tok_jmp(char *tok
)
344 if (strcmp(tok
, tok_see()))
350 static int tok_comes(char *tok
)
352 return !strcmp(tok
, tok_see());
355 static void tok_req(char *tok
)
357 char *got
= tok_get();
358 if (strcmp(tok
, got
))
359 err("syntax error (expected <%s> but got <%s>)\n", tok
, got
);
362 static int tok_grp(void)
364 int c
= (unsigned char) tok_see()[0];
367 if (c
== '\'' || isdigit(c
))
369 if (c
== '_' || isalpha(c
))
374 /* the result of a binary operation on variables of type bt1 and bt2 */
375 static unsigned bt_op(unsigned bt1
, unsigned bt2
)
377 int sz
= MAX(T_SZ(bt1
), T_SZ(bt2
));
378 return ((bt1
| bt2
) & T_MSIGN
) | MAX(sz
, UINT
);
381 /* the result of a unary operation on variables of bt */
382 static unsigned bt_uop(unsigned bt
)
384 return bt_op(bt
, UINT
);
387 /* push the result of a binary operation on the type stack */
388 static void ts_binop(int op
)
391 unsigned bt1
, bt2
, bt
;
392 ts_pop_de2(&t1
, &t2
);
395 bt
= bt_op(bt1
, bt2
);
396 if (op
== O_DIV
|| op
== O_MOD
)
397 bt
= T_MK(bt2
& T_MSIGN
, bt
);
402 /* push the result of an additive binary operation on the type stack */
403 static void ts_addop(int op
)
406 ts_pop_de2(&t1
, &t2
);
407 if (!t1
.ptr
&& !t2
.ptr
) {
409 ts_push_bt(bt_op(TYPE_BT(&t1
), TYPE_BT(&t2
)));
412 if (t1
.ptr
&& !t2
.ptr
)
414 if (!t1
.ptr
&& t2
.ptr
)
415 if (type_szde(&t2
) > 1) {
416 o_num(type_szde(&t2
));
419 if (t1
.ptr
&& !t2
.ptr
)
422 if (t1
.ptr
&& t2
.ptr
) {
423 int sz
= type_szde(&t1
);
430 ts_push(t1
.ptr
? &t1
: &t2
);
434 /* function prototypes for parsing function and variable declarations */
435 static int readname(struct type
*main
, char *name
, struct type
*base
);
436 static int readtype(struct type
*type
);
437 static int readdefs(void (*def
)(long data
, struct name
*name
, unsigned flags
),
439 static int readdefs_int(void (*def
)(long data
, struct name
*name
, unsigned flags
),
442 /* function prototypes for parsing initializer expressions */
443 static int initsize(void);
444 static void initexpr(struct type
*t
, int off
, void *obj
,
445 void (*set
)(void *obj
, int off
, struct type
*t
));
447 static int type_alignment(struct type
*t
)
449 if (t
->flags
& T_ARRAY
&& !t
->ptr
)
450 return type_alignment(&arrays
[t
->id
].type
);
451 if (t
->flags
& T_STRUCT
&& !t
->ptr
)
452 return type_alignment(&structs
[t
->id
].fields
[0].type
);
453 return MIN(ULNG
, type_totsz(t
));
456 static void structdef(long data
, struct name
*name
, unsigned flags
)
458 struct structinfo
*si
= &structs
[data
];
461 if (si
->size
< type_totsz(&name
->type
))
462 si
->size
= type_totsz(&name
->type
);
464 struct type
*t
= &name
->type
;
465 int alignment
= type_alignment(t
);
466 if (t
->flags
& T_ARRAY
&& !t
->ptr
)
467 alignment
= MIN(ULNG
, type_totsz(&arrays
[t
->id
].type
));
468 si
->size
= ALIGN(si
->size
, alignment
);
469 name
->addr
= si
->size
;
470 si
->size
+= type_totsz(&name
->type
);
472 memcpy(&si
->fields
[si
->nfields
++], name
, sizeof(*name
));
475 static int struct_create(char *name
, int isunion
)
477 int id
= struct_find(name
, isunion
);
479 while (tok_jmp("}")) {
480 readdefs(structdef
, id
);
486 static void readexpr(void);
488 static void enum_create(void)
492 while (tok_jmp("}")) {
494 strcpy(name
, tok_get());
499 err("const expr expected!\n");
506 /* used to differentiate labels from case and cond exprs */
510 static void readpre(void);
512 static char *tmp_str(char *buf
, int len
)
514 static char name
[NAMELEN
];
516 sprintf(name
, "__neatcc.s%d", id
++);
518 o_dscpy(o_dsnew(name
, len
+ 1, 0), buf
, len
+ 1);
522 static void readprimary(void)
524 if (tok_grp() == '0') {
526 int bt
= tok_num(tok_get(), &n
);
531 if (tok_grp() == '"') {
532 struct type t
= {}; /* char type inside the arrays */
533 struct type a
= {}; /* the char array type */
534 char *buf
= tok_get() + 1;
535 int len
= tok_len() - 2;
537 a
.id
= array_add(&t
, len
+ 1);
539 o_sym(tmp_str(buf
, len
));
543 if (tok_grp() == 'a') {
544 struct name unkn
= {""};
545 char *name
= unkn
.name
;
547 strcpy(name
, tok_iden());
548 /* don't search for labels here */
549 if (!ncexpr
&& !caseexpr
&& tok_comes(":"))
551 if ((n
= local_find(name
)) != -1) {
552 struct name
*l
= &locals
[n
];
554 ts_push_addr(&l
->type
);
557 if ((n
= global_find(name
)) != -1) {
558 struct name
*g
= &globals
[n
];
559 o_sym(*g
->elfname
? g
->elfname
: g
->name
);
560 ts_push_addr(&g
->type
);
563 if (!enum_find(&n
, name
)) {
569 err("unknown symbol <%s>\n", name
);
583 if (!t
.ptr
|| !o
.ptr
)
587 while (tok_jmp(")")) {
598 static void arrayderef(void)
604 if (!(t
.flags
& T_ARRAY
&& !t
.ptr
) && t
.addr
) {
606 o_deref(TYPE_BT(&t
));
621 static void inc_post(int op
)
623 struct type t
= ts
[nts
- 1];
624 /* pushing the value before inc */
629 /* increment by 1 or pointer size */
633 o_num(t
.ptr
> 0 ? type_szde(&t
) : 1);
637 o_assign(TYPE_BT(&t
));
641 static void readfield(void)
647 field
= struct_field(t
.id
, tok_get());
652 ts_push_addr(&field
->type
);
655 static struct funcinfo
{
656 struct type args
[NARGS
];
660 /* function and argument names; useful only when defining */
661 char argnames
[NARGS
][NAMELEN
];
664 static int funcs_n
, funcs_sz
;
666 static int func_create(struct type
*ret
, char *name
, char argnames
[][NAMELEN
],
667 struct type
*args
, int nargs
, int varg
)
671 if (funcs_n
>= funcs_sz
) {
672 funcs_sz
= MAX(128, funcs_sz
* 2);
673 funcs
= mextend(funcs
, funcs_n
, funcs_sz
, sizeof(funcs
[0]));
675 fi
= &funcs
[funcs_n
++];
676 memcpy(&fi
->ret
, ret
, sizeof(*ret
));
677 for (i
= 0; i
< nargs
; i
++)
678 memcpy(&fi
->args
[i
], &args
[i
], sizeof(*ret
));
681 strcpy(fi
->name
, name
? name
: "");
682 for (i
= 0; i
< nargs
; i
++)
683 strcpy(fi
->argnames
[i
], argnames
[i
]);
687 static void readcall(void)
693 if (t
.flags
& T_FUNC
&& t
.ptr
> 0)
695 if (!tok_comes(")")) {
700 } while (!tok_jmp(","));
703 fi
= t
.flags
& T_FUNC
? &funcs
[t
.id
] : NULL
;
704 o_call(argc
, fi
? TYPE_BT(&fi
->ret
) : SINT
);
706 if (TYPE_BT(&fi
->ret
))
707 o_cast(TYPE_BT(&fi
->ret
));
714 static void readpost(void)
728 if (!tok_jmp("++")) {
732 if (!tok_jmp("--")) {
740 if (!tok_jmp("->")) {
749 static void inc_pre(int op
)
753 /* copy the destination */
755 ts_push(&ts
[nts
- 1]);
756 /* increment by 1 or pointer size */
758 o_num(t
.ptr
> 0 ? type_szde(&t
) : 1);
760 /* assign the result */
761 o_assign(TYPE_BT(&t
));
765 static void readpre(void)
772 err("cannot use the address\n");
783 err("dereferencing non-pointer\n");
785 o_deref(TYPE_BT(&t
));
802 ts_push_bt(bt_uop(TYPE_BT(&t
)));
810 ts_push_bt(bt_uop(TYPE_BT(&t
)));
818 ts_push_bt(bt_uop(TYPE_BT(&t
)));
821 if (!tok_jmp("++")) {
825 if (!tok_jmp("--")) {
829 if (!tok_jmp("sizeof")) {
831 int op
= !tok_jmp("(");
842 o_num(type_totsz(&t
));
851 static void readmul(void)
874 static void readadd(void)
892 static void shift(int op
)
896 ts_pop_de2(NULL
, &t
);
897 o_bop(O_MK(op
, TYPE_BT(&t
)));
898 ts_push_bt(bt_uop(TYPE_BT(&t
)));
901 static void readshift(void)
905 if (!tok_jmp("<<")) {
909 if (!tok_jmp(">>")) {
917 static void cmp(int op
)
922 ts_pop_de2(&t1
, &t2
);
923 bt
= bt_op(TYPE_BT(&t1
), TYPE_BT(&t2
));
928 static void readcmp(void)
940 if (!tok_jmp("<=")) {
944 if (!tok_jmp(">=")) {
952 static void eq(int op
)
955 ts_pop_de2(NULL
, NULL
);
960 static void readeq(void)
964 if (!tok_jmp("==")) {
968 if (!tok_jmp("!=")) {
976 static void readbitand(void)
979 while (!tok_jmp("&")) {
985 static void readxor(void)
988 while (!tok_jmp("^")) {
994 static void readbitor(void)
997 while (!tok_jmp("|")) {
1003 static void savelocal(long val
, int bt
)
1011 static void loadlocal(long val
, int bt
)
1018 static void readand(void)
1023 if (!tok_comes("&&"))
1025 val
= o_mklocal(UINT
);
1030 while (!tok_jmp("&&")) {
1036 savelocal(val
, UINT
);
1040 savelocal(val
, UINT
);
1042 loadlocal(val
, SINT
);
1046 static void reador(void)
1051 if (!tok_comes("||"))
1053 val
= o_mklocal(UINT
);
1059 while (!tok_jmp("||")) {
1066 savelocal(val
, SINT
);
1070 savelocal(val
, SINT
);
1072 loadlocal(val
, SINT
);
1076 static void readcexpr(void);
1078 static int readcexpr_const(void)
1086 /* both branches yield the same type; so ignore the first */
1096 /* making sure t->addr == 0 on both branches */
1105 static void readcexpr(void)
1112 if (readcexpr_const()) {
1114 int l_fail
= LABEL();
1115 int l_end
= LABEL();
1119 /* both branches yield the same type; so ignore the first */
1121 if (!TYPE_VOID(&ret
)) {
1122 val
= o_mklocal(ULNG
);
1123 savelocal(val
, ULNG
);
1130 /* making sure t->addr == 0 on both branches */
1132 if (!TYPE_VOID(&ret
)) {
1133 savelocal(val
, ULNG
);
1136 if (!TYPE_VOID(&ret
)) {
1137 loadlocal(val
, ULNG
);
1143 static void opassign(int op
, int ptrop
)
1145 struct type t
= ts
[nts
- 1];
1149 if (op
== O_ADD
|| op
== O_SUB
)
1153 o_assign(TYPE_BT(&ts
[nts
- 2]));
1158 static void doassign(void)
1160 struct type t
= ts
[nts
- 1];
1161 if (!t
.ptr
&& t
.flags
& T_STRUCT
) {
1163 o_num(type_totsz(&t
));
1167 o_assign(TYPE_BT(&ts
[nts
- 1]));
1172 static void readexpr(void)
1175 if (!tok_jmp("=")) {
1180 if (!tok_jmp("+=")) {
1184 if (!tok_jmp("-=")) {
1188 if (!tok_jmp("*=")) {
1192 if (!tok_jmp("/=")) {
1196 if (!tok_jmp("%=")) {
1200 if (!tok_jmp("<<=")) {
1204 if (!tok_jmp(">>=")) {
1208 if (!tok_jmp("&=")) {
1212 if (!tok_jmp("|=")) {
1216 if (!tok_jmp("^=")) {
1222 static void readestmt(void)
1228 } while (!tok_jmp(","));
1231 #define F_GLOBAL(flags) (!((flags) & F_STATIC))
1233 static void globalinit(void *obj
, int off
, struct type
*t
)
1235 struct name
*name
= obj
;
1236 char *elfname
= *name
->elfname
? name
->elfname
: name
->name
;
1237 if (t
->flags
& T_ARRAY
&& tok_grp() == '"') {
1238 struct type
*t_de
= &arrays
[t
->id
].type
;
1239 if (!t_de
->ptr
&& !t_de
->flags
&& TYPE_SZ(t_de
) == 1) {
1240 char *buf
= tok_get() + 1;
1241 int len
= tok_len() - 2;
1243 o_dscpy(name
->addr
+ off
, buf
, len
+ 1);
1248 o_dsset(elfname
, off
, TYPE_BT(t
));
1252 static void readfunc(struct name
*name
, int flags
);
1254 static void globaldef(long data
, struct name
*name
, unsigned flags
)
1256 struct type
*t
= &name
->type
;
1257 char *elfname
= *name
->elfname
? name
->elfname
: name
->name
;
1259 if (t
->flags
& T_ARRAY
&& !t
->ptr
&& !arrays
[t
->id
].n
)
1260 if (~flags
& F_EXTERN
)
1261 arrays
[t
->id
].n
= initsize();
1263 if (!(flags
& F_EXTERN
) && (!(t
->flags
& T_FUNC
) || t
->ptr
)) {
1265 name
->addr
= o_dsnew(elfname
, sz
, F_GLOBAL(flags
));
1267 o_bsnew(elfname
, sz
, F_GLOBAL(flags
));
1271 initexpr(t
, 0, name
, globalinit
);
1272 if (tok_comes("{") && name
->type
.flags
& T_FUNC
)
1273 readfunc(name
, flags
);
1276 /* generate the address of local + off */
1277 static void o_localoff(long addr
, int off
)
1286 static void localinit(void *obj
, int off
, struct type
*t
)
1288 long addr
= *(long *) obj
;
1289 if (t
->flags
& T_ARRAY
&& tok_grp() == '"') {
1290 struct type
*t_de
= &arrays
[t
->id
].type
;
1291 if (!t_de
->ptr
&& !t_de
->flags
&& TYPE_SZ(t_de
) == 1) {
1292 char *buf
= tok_get() + 1;
1293 int len
= tok_len() - 2;
1294 o_localoff(addr
, off
);
1295 o_sym(tmp_str(buf
, len
));
1302 o_localoff(addr
, off
);
1310 /* current function name */
1311 static char func_name
[NAMELEN
];
1313 static void localdef(long data
, struct name
*name
, unsigned flags
)
1315 struct type
*t
= &name
->type
;
1316 if ((flags
& F_EXTERN
) || ((t
->flags
& T_FUNC
) && !t
->ptr
)) {
1320 if (flags
& F_STATIC
) {
1321 sprintf(name
->elfname
, "__neatcc.%s.%s", func_name
, name
->name
);
1322 globaldef(data
, name
, flags
);
1325 if (t
->flags
& T_ARRAY
&& !t
->ptr
&& !arrays
[t
->id
].n
)
1326 arrays
[t
->id
].n
= initsize();
1327 name
->addr
= o_mklocal(type_totsz(&name
->type
));
1329 if (!tok_jmp("=")) {
1330 if (t
->flags
& (T_ARRAY
| T_STRUCT
) && !t
->ptr
) {
1331 o_local(name
->addr
);
1333 o_num(type_totsz(t
));
1337 initexpr(t
, 0, &name
->addr
, localinit
);
1341 static void typedefdef(long data
, struct name
*name
, unsigned flags
)
1343 typedef_add(name
->name
, &name
->type
);
1346 static void readstmt(void);
1348 static void readswitch(void)
1350 int o_break
= l_break
;
1351 long val_addr
= o_mklocal(ULNG
);
1353 int ncases
= 0; /* number of case labels */
1354 int l_failed
= LABEL(); /* address of last failed jmp */
1355 int l_matched
= LABEL(); /* address of last walk through jmp */
1356 int l_default
= 0; /* default case label */
1363 o_assign(TYPE_BT(&t
));
1368 while (tok_jmp("}")) {
1369 if (!tok_comes("case") && !tok_comes("default")) {
1375 if (!strcmp("case", tok_get())) {
1383 o_deref(TYPE_BT(&t
));
1390 l_default
= LABEL();
1395 l_matched
= LABEL();
1398 o_rmlocal(val_addr
, ULNG
);
1407 static char (*label_name
)[NAMELEN
];
1408 static int *label_ids
;
1409 static int label_n
, label_sz
;
1411 static int label_id(char *name
)
1414 if (label_n
>= label_sz
) {
1415 label_sz
= MAX(128, label_sz
* 2);
1416 label_name
= mextend(label_name
, label_n
, label_sz
,
1417 sizeof(label_name
[0]));
1418 label_ids
= mextend(label_ids
, label_n
, label_sz
,
1419 sizeof(label_ids
[0]));
1421 for (i
= label_n
- 1; i
>= 0; --i
)
1422 if (!strcmp(label_name
[i
], name
))
1423 return label_ids
[i
];
1424 strcpy(label_name
[label_n
], name
);
1425 label_ids
[label_n
] = LABEL();
1426 return label_ids
[label_n
++];
1429 static void readstmt(void)
1433 if (!tok_jmp("{")) {
1434 int _nlocals
= locals_n
;
1435 int _nglobals
= globals_n
;
1436 int _nenums
= enums_n
;
1437 int _ntypedefs
= typedefs_n
;
1438 int _nstructs
= structs_n
;
1439 int _nfuncs
= funcs_n
;
1440 int _narrays
= arrays_n
;
1441 while (tok_jmp("}"))
1443 locals_n
= _nlocals
;
1445 typedefs_n
= _ntypedefs
;
1446 structs_n
= _nstructs
;
1448 arrays_n
= _narrays
;
1449 globals_n
= _nglobals
;
1452 if (!readdefs(localdef
, 0)) {
1456 if (!tok_jmp("typedef")) {
1457 readdefs(typedefdef
, 0);
1461 if (!tok_jmp("if")) {
1462 int l_fail
= LABEL();
1463 int l_end
= LABEL();
1470 if (!tok_jmp("else")) {
1480 if (!tok_jmp("while")) {
1481 int o_break
= l_break
;
1482 int o_cont
= l_cont
;
1498 if (!tok_jmp("do")) {
1499 int o_break
= l_break
;
1500 int o_cont
= l_cont
;
1501 int l_beg
= LABEL();
1520 if (!tok_jmp("for")) {
1521 int o_break
= l_break
;
1522 int o_cont
= l_cont
;
1523 int l_check
= LABEL(); /* for condition label */
1524 int l_body
= LABEL(); /* for block label */
1528 if (!tok_comes(";"))
1532 if (!tok_comes(";")) {
1540 if (!tok_comes(")"))
1552 if (!tok_jmp("switch")) {
1556 if (!tok_jmp("return")) {
1557 int ret
= !tok_comes(";");
1566 if (!tok_jmp("break")) {
1571 if (!tok_jmp("continue")) {
1576 if (!tok_jmp("goto")) {
1577 o_jmp(label_id(tok_get()));
1583 if (!tok_jmp(":")) {
1584 o_label(label_id(tok_previden
));
1590 static void readfunc(struct name
*name
, int flags
)
1592 struct funcinfo
*fi
= &funcs
[name
->type
.id
];
1594 strcpy(func_name
, fi
->name
);
1595 o_func_beg(func_name
, fi
->nargs
, F_GLOBAL(flags
), fi
->varg
);
1596 for (i
= 0; i
< fi
->nargs
; i
++) {
1597 struct name arg
= {"", "", fi
->args
[i
], o_arg2loc(i
)};
1598 strcpy(arg
.name
, fi
->argnames
[i
]);
1605 func_name
[0] = '\0';
1609 static void readdecl(void)
1611 if (!tok_jmp("typedef")) {
1612 readdefs(typedefdef
, 0);
1616 readdefs_int(globaldef
, 0);
1620 static void parse(void)
1626 static void compat_macros(void)
1628 cpp_define("__STDC__", "");
1629 cpp_define("__linux__", "");
1630 cpp_define(I_ARCH
, "");
1632 /* ignored keywords */
1633 cpp_define("const", "");
1634 cpp_define("register", "");
1635 cpp_define("volatile", "");
1636 cpp_define("inline", "");
1637 cpp_define("restrict", "");
1638 cpp_define("__inline__", "");
1639 cpp_define("__restrict__", "");
1640 cpp_define("__attribute__(x)", "");
1641 cpp_define("__builtin_va_list__", "long");
1644 int main(int argc
, char *argv
[])
1650 while (i
< argc
&& argv
[i
][0] == '-') {
1651 if (argv
[i
][1] == 'I')
1652 cpp_path(argv
[i
][2] ? argv
[i
] + 2 : argv
[++i
]);
1653 if (argv
[i
][1] == 'D') {
1654 char *name
= argv
[i
] + 2;
1656 char *eq
= strchr(name
, '=');
1661 cpp_define(name
, def
);
1663 if (argv
[i
][1] == 'o')
1664 strcpy(obj
, argv
[i
][2] ? argv
[i
] + 2 : argv
[++i
]);
1668 die("neatcc: no file given\n");
1669 if (cpp_init(argv
[i
]))
1670 die("neatcc: cannot open <%s>\n", argv
[i
]);
1674 strcpy(obj
, argv
[i
]);
1675 obj
[strlen(obj
) - 1] = 'o';
1686 ofd
= open(obj
, O_WRONLY
| O_TRUNC
| O_CREAT
, 0600);
1693 /* parsing function and variable declarations */
1695 /* read the base type of a variable */
1696 static int basetype(struct type
*type
, unsigned *flags
)
1703 char name
[NAMELEN
] = "";
1709 if (!tok_jmp("static")) {
1711 } else if (!tok_jmp("extern")) {
1713 } else if (!tok_jmp("void")) {
1717 } else if (!tok_jmp("int")) {
1719 } else if (!tok_jmp("char")) {
1722 } else if (!tok_jmp("short")) {
1724 } else if (!tok_jmp("long")) {
1726 } else if (!tok_jmp("signed")) {
1728 } else if (!tok_jmp("unsigned")) {
1730 } else if (tok_comes("union") || tok_comes("struct")) {
1731 isunion
= !strcmp("union", tok_get());
1732 if (tok_grp() == 'a')
1733 strcpy(name
, tok_get());
1735 type
->id
= struct_create(name
, isunion
);
1737 type
->id
= struct_find(name
, isunion
);
1738 type
->flags
|= T_STRUCT
;
1741 } else if (!tok_jmp("enum")) {
1742 if (tok_grp() == 'a')
1749 if (tok_grp() == 'a') {
1750 int id
= typedef_find(tok_see());
1753 memcpy(type
, &typedefs
[id
].type
,
1765 type
->bt
= size
| (sign
? T_MSIGN
: 0);
1769 static void readptrs(struct type
*type
)
1771 while (!tok_jmp("*")) {
1778 /* read function arguments */
1779 static int readargs(struct type
*args
, char argnames
[][NAMELEN
], int *varg
)
1784 while (!tok_comes(")")) {
1785 if (!tok_jmp("...")) {
1789 if (readname(&args
[nargs
], argnames
[nargs
], NULL
)) {
1790 /* argument has no type, assume int */
1791 memset(&args
[nargs
], 0, sizeof(struct type
));
1792 args
[nargs
].bt
= SINT
;
1793 strcpy(argnames
[nargs
], tok_get());
1795 /* argument arrays are pointers */
1796 array2ptr(&args
[nargs
]);
1803 if (nargs
== 1 && !TYPE_BT(&args
[0]))
1808 /* read K&R function arguments */
1809 static void krdef(long data
, struct name
*name
, unsigned flags
)
1811 struct funcinfo
*fi
= &funcs
[data
];
1813 for (i
= 0; i
< fi
->nargs
; i
++)
1814 if (!strcmp(fi
->argnames
[i
], name
->name
))
1815 memcpy(&fi
->args
[i
], &name
->type
, sizeof(name
->type
));
1819 * readarrays() parses array specifiers when reading a definition in
1820 * readname(). The "type" parameter contains the type contained in the
1821 * inner array; for instance, type in "int *a[10][20]" would be an int
1822 * pointer. When returning, the "type" parameter is changed to point
1823 * to the final array. The function returns a pointer to the type in
1824 * the inner array; this is useful when the type is not complete yet,
1825 * like when creating an array of function pointers as in
1826 * "int (*f[10])(int)". If there is no array brackets, NULL is returned.
1828 static struct type
*readarrays(struct type
*type
)
1831 struct type
*inner
= NULL
;
1834 while (!tok_jmp("[")) {
1840 err("const expr expected\n");
1845 for (i
= nar
- 1; i
>= 0; i
--) {
1846 type
->id
= array_add(type
, arsz
[i
]);
1848 inner
= &arrays
[type
->id
].type
;
1849 type
->flags
= T_ARRAY
;
1856 static struct type
*innertype(struct type
*t
)
1858 while (t
->flags
& T_ARRAY
&& !t
->ptr
)
1859 t
= &arrays
[t
->id
].type
;
1863 static void innertype_modify(struct type
*t
, struct type
*s
)
1865 struct type
*inner
= innertype(t
);
1866 int ptr
= inner
->ptr
;
1867 memcpy(inner
, s
, sizeof(*inner
));
1872 * readname() reads a variable definition; the name is copied into
1873 * "name" and the type is copied into "main" argument. The "base"
1874 * argument, if not NULL, indicates the base type of the variable.
1875 * For instance, the base type of "a" and "b" in "int *a, b[10]" is
1876 * "int". If NULL, basetype() is called directly to read the base
1877 * type of the variable. readname() returns zero, only if the
1878 * variable can be read.
1880 static int readname(struct type
*main
, char *name
, struct type
*base
)
1882 struct type type
; /* the main type */
1883 struct type btype
; /* type before parenthesis; e.g. "int *" in "int *(*p)[10] */
1889 if (basetype(&type
, &flags
))
1895 paren
= !tok_jmp("(");
1900 if (tok_grp() == 'a' && name
)
1901 strcpy(name
, tok_get());
1905 if (tok_comes("(")) {
1906 struct type args
[NARGS
];
1907 char argnames
[NARGS
][NAMELEN
];
1909 int nargs
= readargs(args
, argnames
, &varg
);
1910 struct type rtype
= type
; /* return type */
1911 struct type ftype
= {0}; /* function type */
1914 ftype
.flags
= T_FUNC
;
1916 ftype
.id
= func_create(&rtype
, name
, argnames
, args
, nargs
, varg
);
1918 innertype_modify(&type
, &ftype
);
1921 if (!tok_comes(";"))
1922 while (!tok_comes("{") && !readdefs(krdef
, type
.id
))
1925 if (paren
&& readarrays(&btype
))
1926 innertype_modify(&type
, &btype
);
1932 static int readtype(struct type
*type
)
1934 return readname(type
, NULL
, NULL
);
1938 * readdefs() reads a variable definition statement. The definition
1939 * can appear in different contexts: global variables, function
1940 * local variables, struct fields, and typedefs. For each defined
1941 * variable, def() callback is called with the appropriate name
1942 * struct and flags; the callback should finish parsing the definition
1943 * by possibly reading the initializer expression and saving the name
1946 static int readdefs(void (*def
)(long data
, struct name
*name
, unsigned flags
),
1950 unsigned base_flags
;
1951 if (basetype(&base
, &base_flags
))
1953 if (tok_comes(";") || tok_comes("{"))
1956 struct name name
= {{""}};
1957 if (readname(&name
.type
, name
.name
, &base
))
1959 def(data
, &name
, base_flags
);
1960 } while (!tok_jmp(","));
1964 /* just like readdefs, but default to int type; for handling K&R functions */
1965 static int readdefs_int(void (*def
)(long data
, struct name
*name
, unsigned flags
),
1970 if (basetype(&base
, &flags
)) {
1971 if (tok_grp() != 'a')
1973 memset(&base
, 0, sizeof(base
));
1976 if (!tok_comes(";")) {
1978 struct name name
= {{""}};
1979 if (readname(&name
.type
, name
.name
, &base
))
1981 def(data
, &name
, flags
);
1982 } while (!tok_jmp(","));
1988 /* parsing initializer expressions */
1990 static void jumpbrace(void)
1993 while (!tok_comes("}") || depth
--)
1994 if (!strcmp("{", tok_get()))
1999 /* compute the size of the initializer expression */
2000 static int initsize(void)
2002 long addr
= tok_addr();
2006 if (tok_grp() == '"') {
2008 n
= tok_len() - 2 + 1;
2013 while (tok_jmp("}")) {
2015 if (!tok_jmp("[")) {
2024 while (!tok_comes("}") && !tok_comes(","))
2025 if (!strcmp("{", tok_get()))
2033 /* read the initializer expression and initialize basic types using set() cb */
2034 static void initexpr(struct type
*t
, int off
, void *obj
,
2035 void (*set
)(void *obj
, int off
, struct type
*t
))
2041 if (!t
->ptr
&& t
->flags
& T_STRUCT
) {
2042 struct structinfo
*si
= &structs
[t
->id
];
2044 for (i
= 0; i
< si
->nfields
&& !tok_comes("}"); i
++) {
2045 struct name
*field
= &si
->fields
[i
];
2046 if (!tok_jmp(".")) {
2047 field
= struct_field(t
->id
, tok_get());
2050 initexpr(&field
->type
, off
+ field
->addr
, obj
, set
);
2054 } else if (t
->flags
& T_ARRAY
) {
2055 struct type t_de
= arrays
[t
->id
].type
;
2057 /* handling extra braces as in: char s[] = {"sth"} */
2058 if (TYPE_SZ(&t_de
) == 1 && tok_grp() == '"') {
2063 for (i
= 0; !tok_comes("}"); i
++) {
2065 struct type it
= t_de
;
2066 if (!tok_jmp("[")) {
2073 if (!tok_comes("{") && (tok_grp() != '"' ||
2074 !(it
.flags
& T_ARRAY
)))
2075 it
= *innertype(&t_de
);
2076 initexpr(&it
, off
+ type_totsz(&it
) * idx
, obj
, set
);