2 * neatcc - the neatcc compiler
4 * Copyright (C) 2010-2014 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
)
99 static void ts_push(struct type
*t
)
101 struct type
*d
= &ts
[nts
++];
102 memcpy(d
, t
, sizeof(*t
));
105 static void ts_push_addr(struct type
*t
)
108 ts
[nts
- 1].addr
= 1;
111 static void ts_pop(struct type
*type
)
118 void err(char *fmt
, ...)
123 vsprintf(msg
, fmt
, ap
);
125 die("%s: %s", cpp_loc(tok_addr()), msg
);
130 char elfname
[NAMELEN
]; /* local elf name for function static variables */
132 long addr
; /* local stack offset, global data addr, struct offset */
135 static struct name locals
[NLOCALS
];
137 static struct name globals
[NGLOBALS
];
140 static void local_add(struct name
*name
)
142 if (nlocals
>= NLOCALS
)
143 err("nomem: NLOCALS reached!\n");
144 memcpy(&locals
[nlocals
++], name
, sizeof(*name
));
147 static int local_find(char *name
)
150 for (i
= nlocals
- 1; i
>= 0; --i
)
151 if (!strcmp(locals
[i
].name
, name
))
156 static int global_find(char *name
)
159 for (i
= nglobals
- 1; i
>= 0; i
--)
160 if (!strcmp(name
, globals
[i
].name
))
165 static void global_add(struct name
*name
)
167 if (nglobals
>= NGLOBALS
)
168 err("nomem: NGLOBALS reached!\n");
169 memcpy(&globals
[nglobals
++], name
, sizeof(*name
));
172 #define LABEL() (++label)
174 static int label
; /* last used label id */
175 static int l_break
; /* current break label */
176 static int l_cont
; /* current continue label */
178 static struct enumval
{
184 static void enum_add(char *name
, int val
)
186 struct enumval
*ev
= &enums
[nenums
++];
187 if (nenums
>= NENUMS
)
188 err("nomem: NENUMS reached!\n");
189 strcpy(ev
->name
, name
);
193 static int enum_find(int *val
, char *name
)
196 for (i
= nenums
- 1; i
>= 0; --i
)
197 if (!strcmp(name
, enums
[i
].name
)) {
204 static struct typdefinfo
{
207 } typedefs
[NTYPEDEFS
];
208 static int ntypedefs
;
210 static void typedef_add(char *name
, struct type
*type
)
212 struct typdefinfo
*ti
= &typedefs
[ntypedefs
++];
213 if (ntypedefs
>= NTYPEDEFS
)
214 err("nomem: NTYPEDEFS reached!\n");
215 strcpy(ti
->name
, name
);
216 memcpy(&ti
->type
, type
, sizeof(*type
));
219 static int typedef_find(char *name
)
222 for (i
= ntypedefs
- 1; i
>= 0; --i
)
223 if (!strcmp(name
, typedefs
[i
].name
))
228 static struct array
{
234 static int array_add(struct type
*type
, int n
)
236 struct array
*a
= &arrays
[narrays
++];
237 if (narrays
>= NARRAYS
)
238 err("nomem: NARRAYS reached!\n");
239 memcpy(&a
->type
, type
, sizeof(*type
));
244 static void array2ptr(struct type
*t
)
246 if (t
->flags
& T_ARRAY
&& !t
->ptr
) {
247 memcpy(t
, &arrays
[t
->id
].type
, sizeof(*t
));
252 static struct structinfo
{
254 struct name fields
[NFIELDS
];
261 static int struct_find(char *name
, int isunion
)
264 for (i
= nstructs
- 1; i
>= 0; --i
)
265 if (*structs
[i
].name
&& !strcmp(name
, structs
[i
].name
) &&
266 structs
[i
].isunion
== isunion
)
269 if (nstructs
>= NSTRUCTS
)
270 err("nomem: NSTRUCTS reached!\n");
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("field not found\n");
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
: BT_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 static int tok_jmp(int tok
)
335 if (tok_see() != tok
)
341 static void tok_expect(int tok
)
343 if (tok_get() != tok
)
344 err("syntax error\n");
347 /* the result of a binary operation on variables of type bt1 and bt2 */
348 static unsigned bt_op(unsigned bt1
, unsigned bt2
)
350 int sz
= MAX(BT_SZ(bt1
), BT_SZ(bt2
));
351 return ((bt1
| bt2
) & BT_SIGNED
) | MAX(sz
, 4);
354 /* the result of a unary operation on variables of bt */
355 static unsigned bt_uop(unsigned bt
)
360 /* push the result of a binary operation on the type stack */
361 static void ts_binop(int op
)
364 unsigned bt1
, bt2
, bt
;
365 ts_pop_de2(&t1
, &t2
);
368 bt
= bt_op(bt1
, bt2
);
369 if (op
== O_DIV
|| op
== O_MOD
)
370 bt
= BT(bt2
& BT_SIGNED
, bt
);
371 o_bop(op
| (bt
& BT_SIGNED
? O_SIGNED
: 0));
375 /* push the result of an additive binary operation on the type stack */
376 static void ts_addop(int op
)
379 ts_pop_de2(&t1
, &t2
);
380 if (!t1
.ptr
&& !t2
.ptr
) {
382 ts_push_bt(bt_op(TYPE_BT(&t1
), TYPE_BT(&t2
)));
385 if (t1
.ptr
&& !t2
.ptr
)
387 if (!t1
.ptr
&& t2
.ptr
)
388 if (type_szde(&t2
) > 1) {
389 o_num(type_szde(&t2
));
392 if (t1
.ptr
&& !t2
.ptr
)
395 if (t1
.ptr
&& t2
.ptr
) {
396 int sz
= type_szde(&t1
);
401 ts_push_bt(LONGSZ
| BT_SIGNED
);
403 ts_push(t1
.ptr
? &t1
: &t2
);
407 /* function prototypes for parsing function and variable declarations */
408 static int readname(struct type
*main
, char *name
, struct type
*base
);
409 static int readtype(struct type
*type
);
410 static int readdefs(void (*def
)(void *data
, struct name
*name
, unsigned flags
),
412 static int readdefs_int(void (*def
)(void *data
, struct name
*name
, unsigned flags
),
415 /* function prototypes for parsing initializer expressions */
416 static int initsize(void);
417 static void initexpr(struct type
*t
, int off
, void *obj
,
418 void (*set
)(void *obj
, int off
, struct type
*t
));
420 static int type_alignment(struct type
*t
)
422 if (t
->flags
& T_ARRAY
&& !t
->ptr
)
423 return type_alignment(&arrays
[t
->id
].type
);
424 if (t
->flags
& T_STRUCT
&& !t
->ptr
)
425 return type_alignment(&structs
[t
->id
].fields
[0].type
);
426 return MIN(LONGSZ
, type_totsz(t
));
429 static void structdef(void *data
, struct name
*name
, unsigned flags
)
431 struct structinfo
*si
= data
;
434 if (si
->size
< type_totsz(&name
->type
))
435 si
->size
= type_totsz(&name
->type
);
437 struct type
*t
= &name
->type
;
438 int alignment
= type_alignment(t
);
439 if (t
->flags
& T_ARRAY
&& !t
->ptr
)
440 alignment
= MIN(LONGSZ
, type_totsz(&arrays
[t
->id
].type
));
441 si
->size
= ALIGN(si
->size
, alignment
);
442 name
->addr
= si
->size
;
443 si
->size
+= type_totsz(&name
->type
);
445 memcpy(&si
->fields
[si
->nfields
++], name
, sizeof(*name
));
448 static int struct_create(char *name
, int isunion
)
450 int id
= struct_find(name
, isunion
);
451 struct structinfo
*si
= &structs
[id
];
453 while (tok_jmp('}')) {
454 readdefs(structdef
, si
);
460 static void readexpr(void);
462 static void enum_create(void)
466 while (tok_jmp('}')) {
468 tok_expect(TOK_NAME
);
469 strcpy(name
, tok_id());
474 err("const expr expected!\n");
481 /* used to differentiate labels from case and cond exprs */
485 static void readpre(void);
487 static char *tmp_str(char *buf
, int len
)
489 static char name
[NAMELEN
];
491 sprintf(name
, "__neatcc.s%d", id
++);
492 o_dscpy(o_dsnew(name
, len
, 0), buf
, len
);
496 static void readprimary(void)
498 if (!tok_jmp(TOK_NUM
)) {
500 int bt
= tok_num(&n
);
505 if (!tok_jmp(TOK_STR
)) {
506 struct type t
= {}; /* char type inside the arrays */
507 struct type a
= {}; /* the char array type */
511 t
.bt
= 1 | BT_SIGNED
;
512 a
.id
= array_add(&t
, len
);
514 o_sym(tmp_str(buf
, len
));
518 if (!tok_jmp(TOK_NAME
)) {
519 struct name unkn
= {""};
520 char *name
= unkn
.name
;
522 strcpy(name
, tok_id());
523 /* don't search for labels here */
524 if (!ncexpr
&& !caseexpr
&& tok_see() == ':')
526 if ((n
= local_find(name
)) != -1) {
527 struct name
*l
= &locals
[n
];
529 ts_push_addr(&l
->type
);
532 if ((n
= global_find(name
)) != -1) {
533 struct name
*g
= &globals
[n
];
534 o_sym(*g
->elfname
? g
->elfname
: g
->name
);
535 ts_push_addr(&g
->type
);
538 if (!enum_find(&n
, name
)) {
540 ts_push_bt(4 | BT_SIGNED
);
543 if (tok_see() != '(')
544 err("unknown symbol <%s>\n", name
);
558 if (!t
.ptr
|| !o
.ptr
)
562 while (tok_jmp(')')) {
573 static void arrayderef(void)
579 if (!(t
.flags
& T_ARRAY
&& !t
.ptr
) && t
.addr
) {
581 o_deref(TYPE_BT(&t
));
596 static void inc_post(int op
)
598 struct type t
= ts
[nts
- 1];
599 /* pushing the value before inc */
605 /* increment by 1 or pointer size */
609 o_num(t
.ptr
> 0 ? type_szde(&t
) : 1);
613 o_assign(TYPE_BT(&t
));
617 static void readfield(void)
621 tok_expect(TOK_NAME
);
624 field
= struct_field(t
.id
, tok_id());
629 ts_push_addr(&field
->type
);
632 static struct funcinfo
{
633 struct type args
[NARGS
];
637 /* function and argument names; useful only when defining */
638 char argnames
[NARGS
][NAMELEN
];
643 static int func_create(struct type
*ret
, char *name
, char argnames
[][NAMELEN
],
644 struct type
*args
, int nargs
, int varg
)
646 struct funcinfo
*fi
= &funcs
[nfuncs
++];
648 if (nfuncs
>= NFUNCS
)
649 err("nomem: NFUNCS reached!\n");
650 memcpy(&fi
->ret
, ret
, sizeof(*ret
));
651 for (i
= 0; i
< nargs
; i
++)
652 memcpy(&fi
->args
[i
], &args
[i
], sizeof(*ret
));
655 strcpy(fi
->name
, name
? name
: "");
656 for (i
= 0; i
< nargs
; i
++)
657 strcpy(fi
->argnames
[i
], argnames
[i
]);
661 static void readcall(void)
667 if (t
.flags
& T_FUNC
&& t
.ptr
> 0)
669 fi
= t
.flags
& T_FUNC
? &funcs
[t
.id
] : NULL
;
670 if (tok_see() != ')') {
675 } while (!tok_jmp(','));
678 o_call(argc
, fi
? TYPE_BT(&fi
->ret
) : 4 | BT_SIGNED
);
680 if (TYPE_BT(&fi
->ret
))
681 o_cast(TYPE_BT(&fi
->ret
));
684 ts_push_bt(4 | BT_SIGNED
);
688 static void readpost(void)
702 if (!tok_jmp(TOK2("++"))) {
706 if (!tok_jmp(TOK2("--"))) {
714 if (!tok_jmp(TOK2("->"))) {
723 static void inc_pre(int op
)
727 /* copy the destination */
729 ts_push(&ts
[nts
- 1]);
730 /* increment by 1 or pointer size */
732 o_num(t
.ptr
> 0 ? type_szde(&t
) : 1);
734 /* assign the result */
735 o_assign(TYPE_BT(&t
));
739 static void readpre(void)
746 err("cannot use the address\n");
757 err("dereferencing non-pointer\n");
759 o_deref(TYPE_BT(&t
));
769 ts_push_bt(4 | BT_SIGNED
);
776 ts_push_bt(bt_uop(TYPE_BT(&t
)));
784 ts_push_bt(bt_uop(TYPE_BT(&t
)));
792 ts_push_bt(bt_uop(TYPE_BT(&t
)));
795 if (!tok_jmp(TOK2("++"))) {
799 if (!tok_jmp(TOK2("--"))) {
803 if (!tok_jmp(TOK_SIZEOF
)) {
805 int op
= !tok_jmp('(');
815 o_num(type_totsz(&t
));
824 static void readmul(void)
847 static void readadd(void)
865 static void shift(int op
)
869 ts_pop_de2(NULL
, &t
);
870 o_bop(op
| (BT_SIGNED
& TYPE_BT(&t
) ? O_SIGNED
: 0));
871 ts_push_bt(bt_uop(TYPE_BT(&t
)));
874 static void readshift(void)
878 if (!tok_jmp(TOK2("<<"))) {
882 if (!tok_jmp(TOK2(">>"))) {
890 static void cmp(int op
)
895 ts_pop_de2(&t1
, &t2
);
896 bt
= bt_op(TYPE_BT(&t1
), TYPE_BT(&t2
));
897 o_bop(op
| (bt
& BT_SIGNED
? O_SIGNED
: 0));
898 ts_push_bt(4 | BT_SIGNED
);
901 static void readcmp(void)
913 if (!tok_jmp(TOK2("<="))) {
917 if (!tok_jmp(TOK2(">="))) {
925 static void eq(int op
)
928 ts_pop_de2(NULL
, NULL
);
930 ts_push_bt(4 | BT_SIGNED
);
933 static void readeq(void)
937 if (!tok_jmp(TOK2("=="))) {
941 if (!tok_jmp(TOK2("!="))) {
949 static void readbitand(void)
952 while (!tok_jmp('&')) {
958 static void readxor(void)
961 while (!tok_jmp('^')) {
967 static void readbitor(void)
970 while (!tok_jmp('|')) {
976 static void readand(void)
980 if (tok_see() != TOK2("&&"))
987 while (!tok_jmp(TOK2("&&"))) {
1000 ts_push_bt(4 | BT_SIGNED
);
1003 static void reador(void)
1007 if (tok_see() != TOK2("||"))
1014 while (!tok_jmp(TOK2("||"))) {
1027 ts_push_bt(4 | BT_SIGNED
);
1030 static void readcexpr(void);
1032 static int readcexpr_const(void)
1040 /* both branches yield the same type; so ignore the first */
1048 /* making sure t->addr == 0 on both branches */
1055 static void readcexpr(void)
1063 if (readcexpr_const()) {
1064 int l_fail
= LABEL();
1065 int l_end
= LABEL();
1069 /* both branches yield the same type; so ignore the first */
1071 if (!TYPE_VOID(&ret
))
1078 /* making sure t->addr == 0 on both branches */
1080 if (!TYPE_VOID(&ret
)) {
1089 static void opassign(int op
, int ptrop
)
1091 struct type t
= ts
[nts
- 1];
1095 if (op
== O_ADD
|| op
== O_SUB
)
1099 o_assign(TYPE_BT(&ts
[nts
- 2]));
1104 static void doassign(void)
1106 struct type t
= ts
[nts
- 1];
1107 if (!t
.ptr
&& t
.flags
& T_STRUCT
) {
1109 o_num(type_totsz(&t
));
1113 o_assign(TYPE_BT(&ts
[nts
- 1]));
1118 static void readexpr(void)
1121 if (!tok_jmp('=')) {
1126 if (!tok_jmp(TOK2("+="))) {
1130 if (!tok_jmp(TOK2("-="))) {
1134 if (!tok_jmp(TOK2("*="))) {
1138 if (!tok_jmp(TOK2("/="))) {
1142 if (!tok_jmp(TOK2("%="))) {
1146 if (!tok_jmp(TOK3("<<="))) {
1150 if (!tok_jmp(TOK3(">>="))) {
1154 if (!tok_jmp(TOK3("&="))) {
1158 if (!tok_jmp(TOK3("|="))) {
1162 if (!tok_jmp(TOK3("^="))) {
1168 static void readestmt(void)
1174 } while (!tok_jmp(','));
1177 #define F_GLOBAL(flags) (!((flags) & F_STATIC))
1179 static void globalinit(void *obj
, int off
, struct type
*t
)
1181 struct name
*name
= obj
;
1182 char *elfname
= *name
->elfname
? name
->elfname
: name
->name
;
1183 if (t
->flags
& T_ARRAY
&& tok_see() == TOK_STR
) {
1184 struct type
*t_de
= &arrays
[t
->id
].type
;
1185 if (!t_de
->ptr
&& !t_de
->flags
&& TYPE_SZ(t_de
) == 1) {
1188 tok_expect(TOK_STR
);
1189 tok_str(&buf
, &len
);
1190 o_dscpy(name
->addr
+ off
, buf
, len
);
1195 o_dsset(elfname
, off
, TYPE_BT(t
));
1199 static void readfunc(struct name
*name
, int flags
);
1201 static void globaldef(void *data
, struct name
*name
, unsigned flags
)
1203 struct type
*t
= &name
->type
;
1204 char *elfname
= *name
->elfname
? name
->elfname
: name
->name
;
1206 if (t
->flags
& T_ARRAY
&& !t
->ptr
&& !arrays
[t
->id
].n
)
1207 if (~flags
& F_EXTERN
)
1208 arrays
[t
->id
].n
= initsize();
1210 if (!(flags
& F_EXTERN
) && (!(t
->flags
& T_FUNC
) || t
->ptr
)) {
1211 if (tok_see() == '=')
1212 name
->addr
= o_dsnew(elfname
, sz
, F_GLOBAL(flags
));
1214 o_bsnew(elfname
, sz
, F_GLOBAL(flags
));
1218 initexpr(t
, 0, name
, globalinit
);
1219 if (tok_see() == '{' && name
->type
.flags
& T_FUNC
)
1220 readfunc(name
, flags
);
1223 /* generate the address of local + off */
1224 static void o_localoff(long addr
, int off
)
1233 static void localinit(void *obj
, int off
, struct type
*t
)
1235 long addr
= *(long *) obj
;
1236 if (t
->flags
& T_ARRAY
&& tok_see() == TOK_STR
) {
1237 struct type
*t_de
= &arrays
[t
->id
].type
;
1238 if (!t_de
->ptr
&& !t_de
->flags
&& TYPE_SZ(t_de
) == 1) {
1241 tok_expect(TOK_STR
);
1242 tok_str(&buf
, &len
);
1243 o_localoff(addr
, off
);
1244 o_sym(tmp_str(buf
, len
));
1251 o_localoff(addr
, off
);
1259 /* current function name */
1260 static char func_name
[NAMELEN
];
1262 static void localdef(void *data
, struct name
*name
, unsigned flags
)
1264 struct type
*t
= &name
->type
;
1265 if ((flags
& F_EXTERN
) || ((t
->flags
& T_FUNC
) && !t
->ptr
)) {
1269 if (flags
& F_STATIC
) {
1270 sprintf(name
->elfname
, "__neatcc.%s.%s", func_name
, name
->name
);
1271 globaldef(data
, name
, flags
);
1274 if (t
->flags
& T_ARRAY
&& !t
->ptr
&& !arrays
[t
->id
].n
)
1275 arrays
[t
->id
].n
= initsize();
1276 name
->addr
= o_mklocal(type_totsz(&name
->type
));
1278 if (!tok_jmp('=')) {
1279 if (t
->flags
& (T_ARRAY
| T_STRUCT
) && !t
->ptr
) {
1280 o_local(name
->addr
);
1282 o_num(type_totsz(t
));
1286 initexpr(t
, 0, &name
->addr
, localinit
);
1290 static void typedefdef(void *data
, struct name
*name
, unsigned flags
)
1292 typedef_add(name
->name
, &name
->type
);
1295 static void readstmt(void);
1297 static void readswitch(void)
1299 int o_break
= l_break
;
1300 long val_addr
= o_mklocal(LONGSZ
);
1302 int ncases
= 0; /* number of case labels */
1303 int l_failed
= LABEL(); /* address of last failed jmp */
1304 int l_matched
= LABEL(); /* address of last walk through jmp */
1305 int l_default
= 0; /* default case label */
1312 o_assign(TYPE_BT(&t
));
1317 while (tok_jmp('}')) {
1318 if (tok_see() != TOK_CASE
&& tok_see() != TOK_DEFAULT
) {
1324 if (tok_get() == TOK_CASE
) {
1332 o_deref(TYPE_BT(&t
));
1339 l_default
= LABEL();
1344 l_matched
= LABEL();
1347 o_rmlocal(val_addr
, LONGSZ
);
1356 static char label_name
[NLABELS
][NAMELEN
];
1357 static int label_ids
[NLABELS
];
1360 static int label_id(char *name
)
1363 for (i
= nlabels
- 1; i
>= 0; --i
)
1364 if (!strcmp(label_name
[i
], name
))
1365 return label_ids
[i
];
1366 strcpy(label_name
[nlabels
], name
);
1367 label_ids
[nlabels
] = LABEL();
1368 return label_ids
[nlabels
++];
1371 static void readstmt(void)
1375 if (!tok_jmp('{')) {
1376 int _nlocals
= nlocals
;
1377 int _nglobals
= nglobals
;
1378 int _nenums
= nenums
;
1379 int _ntypedefs
= ntypedefs
;
1380 int _nstructs
= nstructs
;
1381 int _nfuncs
= nfuncs
;
1382 int _narrays
= narrays
;
1383 while (tok_jmp('}'))
1387 ntypedefs
= _ntypedefs
;
1388 nstructs
= _nstructs
;
1391 nglobals
= _nglobals
;
1394 if (!readdefs(localdef
, NULL
)) {
1398 if (!tok_jmp(TOK_TYPEDEF
)) {
1399 readdefs(typedefdef
, NULL
);
1403 if (!tok_jmp(TOK_IF
)) {
1404 int l_fail
= LABEL();
1405 int l_end
= LABEL();
1412 if (!tok_jmp(TOK_ELSE
)) {
1422 if (!tok_jmp(TOK_WHILE
)) {
1423 int o_break
= l_break
;
1424 int o_cont
= l_cont
;
1440 if (!tok_jmp(TOK_DO
)) {
1441 int o_break
= l_break
;
1442 int o_cont
= l_cont
;
1443 int l_beg
= LABEL();
1448 tok_expect(TOK_WHILE
);
1461 if (!tok_jmp(TOK_FOR
)) {
1462 int o_break
= l_break
;
1463 int o_cont
= l_cont
;
1464 int l_check
= LABEL(); /* for condition label */
1465 int l_body
= LABEL(); /* for block label */
1469 if (tok_see() != ';')
1473 if (tok_see() != ';') {
1481 if (tok_see() != ')')
1493 if (!tok_jmp(TOK_SWITCH
)) {
1497 if (!tok_jmp(TOK_RETURN
)) {
1498 int ret
= tok_see() != ';';
1507 if (!tok_jmp(TOK_BREAK
)) {
1512 if (!tok_jmp(TOK_CONTINUE
)) {
1517 if (!tok_jmp(TOK_GOTO
)) {
1518 tok_expect(TOK_NAME
);
1519 o_jmp(label_id(tok_id()));
1525 if (!tok_jmp(':')) {
1526 o_label(label_id(tok_id()));
1532 static void readfunc(struct name
*name
, int flags
)
1534 struct funcinfo
*fi
= &funcs
[name
->type
.id
];
1535 long beg
= tok_addr();
1537 strcpy(func_name
, fi
->name
);
1538 o_func_beg(func_name
, fi
->nargs
, F_GLOBAL(flags
), fi
->varg
);
1539 for (i
= 0; i
< fi
->nargs
; i
++) {
1540 struct name arg
= {"", "", fi
->args
[i
], o_arg2loc(i
)};
1541 strcpy(arg
.name
, fi
->argnames
[i
]);
1544 /* first pass: collecting statistics */
1550 /* second pass: generating code */
1556 func_name
[0] = '\0';
1560 static void readdecl(void)
1562 if (!tok_jmp(TOK_TYPEDEF
)) {
1563 readdefs(typedefdef
, NULL
);
1567 readdefs_int(globaldef
, NULL
);
1571 static void parse(void)
1573 while (tok_see() != TOK_EOF
)
1577 static void compat_macros(void)
1579 cpp_define("__STDC__", "");
1580 cpp_define("__linux__", "");
1581 cpp_define(I_ARCH
, "");
1583 /* ignored keywords */
1584 cpp_define("const", "");
1585 cpp_define("register", "");
1586 cpp_define("volatile", "");
1587 cpp_define("inline", "");
1588 cpp_define("restrict", "");
1589 cpp_define("__inline__", "");
1590 cpp_define("__restrict__", "");
1591 cpp_define("__attribute__(x)", "");
1592 cpp_define("__builtin_va_list__", "long");
1595 int main(int argc
, char *argv
[])
1601 while (i
< argc
&& argv
[i
][0] == '-') {
1602 if (argv
[i
][1] == 'I')
1603 cpp_addpath(argv
[i
][2] ? argv
[i
] + 2 : argv
[++i
]);
1604 if (argv
[i
][1] == 'D') {
1605 char *name
= argv
[i
] + 2;
1607 char *eq
= strchr(name
, '=');
1612 cpp_define(name
, def
);
1614 if (argv
[i
][1] == 'o')
1615 strcpy(obj
, argv
[i
][2] ? argv
[i
] + 2 : argv
[++i
]);
1619 die("neatcc: no file given\n");
1620 if (cpp_init(argv
[i
]))
1621 die("neatcc: cannot open <%s>\n", argv
[i
]);
1624 strcpy(obj
, argv
[i
]);
1625 obj
[strlen(obj
) - 1] = 'o';
1627 ofd
= open(obj
, O_WRONLY
| O_TRUNC
| O_CREAT
, 0600);
1634 /* parsing function and variable declarations */
1636 /* read the base type of a variable */
1637 static int basetype(struct type
*type
, unsigned *flags
)
1644 char name
[NAMELEN
] = "";
1650 switch (tok_see()) {
1682 isunion
= tok_get() == TOK_UNION
;
1683 if (!tok_jmp(TOK_NAME
))
1684 strcpy(name
, tok_id());
1685 if (tok_see() == '{')
1686 type
->id
= struct_create(name
, isunion
);
1688 type
->id
= struct_find(name
, isunion
);
1689 type
->flags
|= T_STRUCT
;
1695 if (tok_see() == '{')
1697 type
->bt
= 4 | BT_SIGNED
;
1700 if (tok_see() == TOK_NAME
) {
1701 int id
= typedef_find(tok_id());
1704 memcpy(type
, &typedefs
[id
].type
,
1717 type
->bt
= size
| (sign
? BT_SIGNED
: 0);
1721 static void readptrs(struct type
*type
)
1723 while (!tok_jmp('*')) {
1730 /* read function arguments */
1731 static int readargs(struct type
*args
, char argnames
[][NAMELEN
], int *varg
)
1736 while (tok_see() != ')') {
1737 if (!tok_jmp(TOK3("..."))) {
1741 if (readname(&args
[nargs
], argnames
[nargs
], NULL
)) {
1742 /* argument has no type, assume int */
1743 tok_expect(TOK_NAME
);
1744 memset(&args
[nargs
], 0, sizeof(struct type
));
1745 args
[nargs
].bt
= 4 | BT_SIGNED
;
1746 strcpy(argnames
[nargs
], tok_id());
1748 /* argument arrays are pointers */
1749 array2ptr(&args
[nargs
]);
1756 if (nargs
== 1 && !TYPE_BT(&args
[0]))
1761 /* read K&R function arguments */
1762 static void krdef(void *data
, struct name
*name
, unsigned flags
)
1764 struct funcinfo
*fi
= data
;
1766 for (i
= 0; i
< fi
->nargs
; i
++)
1767 if (!strcmp(fi
->argnames
[i
], name
->name
))
1768 memcpy(&fi
->args
[i
], &name
->type
, sizeof(name
->type
));
1772 * readarrays() parses array specifiers when reading a definition in
1773 * readname(). The "type" parameter contains the type contained in the
1774 * inner array; for instance, type in "int *a[10][20]" would be an int
1775 * pointer. When returning, the "type" parameter is changed to point
1776 * to the final array. The function returns a pointer to the type in
1777 * the inner array; this is useful when the type is not complete yet,
1778 * like when creating an array of function pointers as in
1779 * "int (*f[10])(int)". If there is no array brackets, NULL is returned.
1781 static struct type
*readarrays(struct type
*type
)
1784 struct type
*inner
= NULL
;
1787 while (!tok_jmp('[')) {
1793 err("const expr expected\n");
1798 for (i
= nar
- 1; i
>= 0; i
--) {
1799 type
->id
= array_add(type
, arsz
[i
]);
1801 inner
= &arrays
[type
->id
].type
;
1802 type
->flags
= T_ARRAY
;
1810 * readname() reads a variable definition; the name is copied into
1811 * "name" and the type is copied into "main" argument. The "base"
1812 * argument, if not NULL, indicates the base type of the variable.
1813 * For instance, the base type of "a" and "b" in "int *a, b[10]" is
1814 * "int". If NULL, basetype() is called directly to read the base
1815 * type of the variable. readname() returns zero, only if the
1816 * variable can be read.
1818 static int readname(struct type
*main
, char *name
, struct type
*base
)
1820 struct type tpool
[3];
1822 struct type
*type
= &tpool
[npool
++];
1823 struct type
*ptype
= NULL
; /* type inside parenthesis */
1824 struct type
*btype
= NULL
; /* type before parenthesis */
1827 memset(tpool
, 0, sizeof(tpool
));
1831 if (basetype(type
, &flags
))
1834 memcpy(type
, base
, sizeof(*base
));
1837 if (!tok_jmp('(')) {
1839 type
= &tpool
[npool
++];
1843 if (!tok_jmp(TOK_NAME
) && name
)
1844 strcpy(name
, tok_id());
1845 inner
= readarrays(type
);
1850 if (tok_see() == '(') {
1851 struct type args
[NARGS
];
1852 char argnames
[NARGS
][NAMELEN
];
1854 int nargs
= readargs(args
, argnames
, &varg
);
1857 type
= &tpool
[npool
++];
1860 ptype
->flags
= T_FUNC
;
1862 ptype
->id
= func_create(btype
, name
, argnames
, args
, nargs
, varg
);
1863 if (tok_see() != ';')
1864 while (tok_see() != '{' && !readdefs(krdef
, &funcs
[ptype
->id
]))
1867 if (ptype
&& readarrays(type
))
1870 memcpy(main
, type
, sizeof(*type
));
1874 static int readtype(struct type
*type
)
1876 return readname(type
, NULL
, NULL
);
1880 * readdef() reads a variable definitions statement. The definition
1881 * statement can appear in anywhere: global variables, function
1882 * local variables, struct fields, and typedefs. For each defined
1883 * variable, def() callback is called with the appropriate name
1884 * struct and flags; the callback should finish parsing the definition
1885 * by possibly reading the initializer expression and saving the name
1888 static int readdefs(void (*def
)(void *data
, struct name
*name
, unsigned flags
),
1892 unsigned base_flags
;
1893 if (basetype(&base
, &base_flags
))
1895 if (tok_see() == ';' || tok_see() == '{')
1898 struct name name
= {{""}};
1899 if (readname(&name
.type
, name
.name
, &base
))
1901 def(data
, &name
, base_flags
);
1902 } while (!tok_jmp(','));
1906 /* just like readdefs, but default to int type; for handling K&R functions */
1907 static int readdefs_int(void (*def
)(void *data
, struct name
*name
, unsigned flags
),
1912 if (basetype(&base
, &flags
)) {
1913 if (tok_see() != TOK_NAME
)
1915 memset(&base
, 0, sizeof(base
));
1916 base
.bt
= 4 | BT_SIGNED
;
1918 if (tok_see() != ';') {
1920 struct name name
= {{""}};
1921 if (readname(&name
.type
, name
.name
, &base
))
1923 def(data
, &name
, flags
);
1924 } while (!tok_jmp(','));
1930 /* parsing initializer expressions */
1932 static void jumpbrace(void)
1935 while (tok_see() != '}' || depth
--)
1936 if (tok_get() == '{')
1941 /* compute the size of the initializer expression */
1942 static int initsize(void)
1944 long addr
= tok_addr();
1948 if (!tok_jmp(TOK_STR
)) {
1954 while (tok_jmp('}')) {
1956 if (!tok_jmp('[')) {
1965 while (tok_see() != '}' && tok_see() != ',')
1966 if (tok_get() == '{')
1974 static struct type
*innertype(struct type
*t
)
1976 if (t
->flags
& T_ARRAY
&& !t
->ptr
)
1977 return innertype(&arrays
[t
->id
].type
);
1981 /* read the initializer expression and initialize basic types using set() cb */
1982 static void initexpr(struct type
*t
, int off
, void *obj
,
1983 void (*set
)(void *obj
, int off
, struct type
*t
))
1989 if (!t
->ptr
&& t
->flags
& T_STRUCT
) {
1990 struct structinfo
*si
= &structs
[t
->id
];
1992 for (i
= 0; i
< si
->nfields
&& tok_see() != '}'; i
++) {
1993 struct name
*field
= &si
->fields
[i
];
1994 if (!tok_jmp('.')) {
1995 tok_expect(TOK_NAME
);
1996 field
= struct_field(t
->id
, tok_id());
1999 initexpr(&field
->type
, off
+ field
->addr
, obj
, set
);
2003 } else if (t
->flags
& T_ARRAY
) {
2004 struct type
*t_de
= &arrays
[t
->id
].type
;
2006 /* handling extra braces as in: char s[] = {"sth"} */
2007 if (TYPE_SZ(t_de
) == 1 && tok_see() == TOK_STR
) {
2012 for (i
= 0; tok_see() != '}'; i
++) {
2014 struct type
*it
= t_de
;
2015 if (!tok_jmp('[')) {
2022 if (tok_see() != '{' && (tok_see() != TOK_STR
||
2023 !(it
->flags
& T_ARRAY
)))
2024 it
= innertype(t_de
);
2025 initexpr(it
, off
+ type_totsz(it
) * idx
, obj
, set
);