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))
65 #define TYPE_BT(t) ((t)->ptr ? LONGSZ : (t)->bt)
66 #define TYPE_SZ(t) ((t)->ptr ? LONGSZ : (t)->bt & BT_SZMASK)
67 #define TYPE_VOID(t) (!(t)->bt && !(t)->flags && !(t)->ptr)
69 /* type->flag values */
74 /* variable definition flags */
82 int id
; /* for structs, functions and arrays */
83 int addr
; /* the address is passed to gen.c; deref for value */
87 static struct type ts
[NTMPS
];
90 static void ts_push_bt(unsigned bt
)
98 static void ts_push(struct type
*t
)
100 struct type
*d
= &ts
[nts
++];
101 memcpy(d
, t
, sizeof(*t
));
104 static void ts_push_addr(struct type
*t
)
107 ts
[nts
- 1].addr
= 1;
110 static void ts_pop(struct type
*type
)
117 void err(char *fmt
, ...)
122 vsprintf(msg
, fmt
, ap
);
124 die("%s: %s", cpp_loc(tok_addr()), msg
);
129 char elfname
[NAMELEN
]; /* local elf name for function static variables */
131 long addr
; /* local stack offset, global data addr, struct offset */
134 static struct name locals
[NLOCALS
];
136 static struct name globals
[NGLOBALS
];
139 static void local_add(struct name
*name
)
141 if (nlocals
>= NLOCALS
)
142 err("nomem: NLOCALS reached!\n");
143 memcpy(&locals
[nlocals
++], name
, sizeof(*name
));
146 static int local_find(char *name
)
149 for (i
= nlocals
- 1; i
>= 0; --i
)
150 if (!strcmp(locals
[i
].name
, name
))
155 static int global_find(char *name
)
158 for (i
= nglobals
- 1; i
>= 0; i
--)
159 if (!strcmp(name
, globals
[i
].name
))
164 static void global_add(struct name
*name
)
166 if (nglobals
>= NGLOBALS
)
167 err("nomem: NGLOBALS reached!\n");
168 memcpy(&globals
[nglobals
++], name
, sizeof(*name
));
171 #define LABEL() (++label)
173 static int label
; /* last used label id */
174 static int l_break
; /* current break label */
175 static int l_cont
; /* current continue label */
177 static struct enumval
{
183 static void enum_add(char *name
, int val
)
185 struct enumval
*ev
= &enums
[nenums
++];
186 if (nenums
>= NENUMS
)
187 err("nomem: NENUMS reached!\n");
188 strcpy(ev
->name
, name
);
192 static int enum_find(int *val
, char *name
)
195 for (i
= nenums
- 1; i
>= 0; --i
)
196 if (!strcmp(name
, enums
[i
].name
)) {
203 static struct typdefinfo
{
206 } typedefs
[NTYPEDEFS
];
207 static int ntypedefs
;
209 static void typedef_add(char *name
, struct type
*type
)
211 struct typdefinfo
*ti
= &typedefs
[ntypedefs
++];
212 if (ntypedefs
>= NTYPEDEFS
)
213 err("nomem: NTYPEDEFS reached!\n");
214 strcpy(ti
->name
, name
);
215 memcpy(&ti
->type
, type
, sizeof(*type
));
218 static int typedef_find(char *name
)
221 for (i
= ntypedefs
- 1; i
>= 0; --i
)
222 if (!strcmp(name
, typedefs
[i
].name
))
227 static struct array
{
233 static int array_add(struct type
*type
, int n
)
235 struct array
*a
= &arrays
[narrays
++];
236 if (narrays
>= NARRAYS
)
237 err("nomem: NARRAYS reached!\n");
238 memcpy(&a
->type
, type
, sizeof(*type
));
243 static void array2ptr(struct type
*t
)
245 if (t
->flags
& T_ARRAY
&& !t
->ptr
) {
246 memcpy(t
, &arrays
[t
->id
].type
, sizeof(*t
));
251 static struct structinfo
{
253 struct name fields
[NFIELDS
];
260 static int struct_find(char *name
, int isunion
)
263 for (i
= nstructs
- 1; i
>= 0; --i
)
264 if (*structs
[i
].name
&& !strcmp(name
, structs
[i
].name
) &&
265 structs
[i
].isunion
== isunion
)
268 if (nstructs
>= NSTRUCTS
)
269 err("nomem: NSTRUCTS reached!\n");
270 memset(&structs
[i
], 0, sizeof(structs
[i
]));
271 strcpy(structs
[i
].name
, name
);
272 structs
[i
].isunion
= isunion
;
276 static struct name
*struct_field(int id
, char *name
)
278 struct structinfo
*si
= &structs
[id
];
280 for (i
= 0; i
< si
->nfields
; i
++)
281 if (!strcmp(name
, si
->fields
[i
].name
))
282 return &si
->fields
[i
];
283 err("field not found\n");
287 /* return t's size */
288 static int type_totsz(struct type
*t
)
292 if (t
->flags
& T_ARRAY
)
293 return arrays
[t
->id
].n
* type_totsz(&arrays
[t
->id
].type
);
294 return t
->flags
& T_STRUCT
? structs
[t
->id
].size
: BT_SZ(t
->bt
);
297 /* return t's dereferenced size */
298 static unsigned type_szde(struct type
*t
)
303 return type_totsz(&de
);
306 /* dereference stack top if t->addr (ie. address is pushed to gen.c) */
307 static void ts_de(int deref
)
309 struct type
*t
= &ts
[nts
- 1];
311 if (deref
&& t
->addr
&& (t
->ptr
|| !(t
->flags
& T_FUNC
)))
316 /* pop stack pop to *t and dereference if t->addr */
317 static void ts_pop_de(struct type
*t
)
323 /* pop the top 2 stack values and dereference them if t->addr */
324 static void ts_pop_de2(struct type
*t1
, struct type
*t2
)
332 static int tok_jmp(int tok
)
334 if (tok_see() != tok
)
340 static void tok_expect(int tok
)
342 if (tok_get() != tok
)
343 err("syntax error\n");
346 /* the result of a binary operation on variables of type bt1 and bt2 */
347 static unsigned bt_op(unsigned bt1
, unsigned bt2
)
349 unsigned s1
= BT_SZ(bt1
);
350 unsigned s2
= BT_SZ(bt2
);
351 return ((bt1
| bt2
) & BT_SIGNED
) | (s1
> s2
? s1
: s2
);
354 /* push the result of a binary operation on the type stack */
355 static void ts_binop(int op
)
359 ts_pop_de2(&t1
, &t2
);
360 if (op
== O_DIV
|| op
== O_MOD
)
363 bt
= bt_op(TYPE_BT(&t1
), TYPE_BT(&t2
));
364 o_bop(op
| (bt
& BT_SIGNED
? O_SIGNED
: 0));
368 /* push the result of an additive binary operation on the type stack */
369 static void ts_addop(int op
)
372 ts_pop_de2(&t1
, &t2
);
373 if (!t1
.ptr
&& !t2
.ptr
) {
375 ts_push_bt(bt_op(TYPE_BT(&t1
), TYPE_BT(&t2
)));
378 if (t1
.ptr
&& !t2
.ptr
)
380 if (!t1
.ptr
&& t2
.ptr
)
381 if (type_szde(&t2
) > 1) {
382 o_num(type_szde(&t2
));
385 if (t1
.ptr
&& !t2
.ptr
)
388 if (t1
.ptr
&& t2
.ptr
) {
389 int sz
= type_szde(&t1
);
394 ts_push_bt(4 | BT_SIGNED
);
396 ts_push(t1
.ptr
? &t1
: &t2
);
400 /* function prototypes for parsing function and variable declarations */
401 static int readname(struct type
*main
, char *name
, struct type
*base
);
402 static int readtype(struct type
*type
);
403 static int readdefs(void (*def
)(void *data
, struct name
*name
, unsigned flags
),
405 static int readdefs_int(void (*def
)(void *data
, struct name
*name
, unsigned flags
),
408 /* function prototypes for parsing initializer expressions */
409 static int initsize(void);
410 static void initexpr(struct type
*t
, int off
, void *obj
,
411 void (*set
)(void *obj
, int off
, struct type
*t
));
413 static int type_alignment(struct type
*t
)
415 if (t
->flags
& T_ARRAY
&& !t
->ptr
)
416 return type_alignment(&arrays
[t
->id
].type
);
417 if (t
->flags
& T_STRUCT
&& !t
->ptr
)
418 return type_alignment(&structs
[t
->id
].fields
[0].type
);
419 return MIN(LONGSZ
, type_totsz(t
));
422 static void structdef(void *data
, struct name
*name
, unsigned flags
)
424 struct structinfo
*si
= data
;
427 if (si
->size
< type_totsz(&name
->type
))
428 si
->size
= type_totsz(&name
->type
);
430 struct type
*t
= &name
->type
;
431 int alignment
= type_alignment(t
);
432 if (t
->flags
& T_ARRAY
&& !t
->ptr
)
433 alignment
= MIN(LONGSZ
, type_totsz(&arrays
[t
->id
].type
));
434 si
->size
= ALIGN(si
->size
, alignment
);
435 name
->addr
= si
->size
;
436 si
->size
+= type_totsz(&name
->type
);
438 memcpy(&si
->fields
[si
->nfields
++], name
, sizeof(*name
));
441 static int struct_create(char *name
, int isunion
)
443 int id
= struct_find(name
, isunion
);
444 struct structinfo
*si
= &structs
[id
];
446 while (tok_jmp('}')) {
447 readdefs(structdef
, si
);
453 static void readexpr(void);
455 static void enum_create(void)
459 while (tok_jmp('}')) {
461 tok_expect(TOK_NAME
);
462 strcpy(name
, tok_id());
467 err("const expr expected!\n");
474 /* used to differentiate labels from case and cond exprs */
478 static void readpre(void);
480 static char *tmp_str(char *buf
, int len
)
482 static char name
[NAMELEN
];
484 sprintf(name
, "__neatcc.s%d", id
++);
485 o_dscpy(o_dsnew(name
, len
, 0), buf
, len
);
489 static void readprimary(void)
491 if (!tok_jmp(TOK_NUM
)) {
493 int bt
= tok_num(&n
);
498 if (!tok_jmp(TOK_STR
)) {
499 struct type t
= {}; /* char type inside the arrays */
500 struct type a
= {}; /* the char array type */
504 t
.bt
= 1 | BT_SIGNED
;
505 a
.id
= array_add(&t
, len
);
508 o_sym(tmp_str(buf
, len
));
511 if (!tok_jmp(TOK_NAME
)) {
512 struct name unkn
= {""};
513 char *name
= unkn
.name
;
515 strcpy(name
, tok_id());
516 /* don't search for labels here */
517 if (!ncexpr
&& !caseexpr
&& tok_see() == ':')
519 if ((n
= local_find(name
)) != -1) {
520 struct name
*l
= &locals
[n
];
522 ts_push_addr(&l
->type
);
525 if ((n
= global_find(name
)) != -1) {
526 struct name
*g
= &globals
[n
];
527 o_sym(*g
->elfname
? g
->elfname
: g
->name
);
528 ts_push_addr(&g
->type
);
531 if (!enum_find(&n
, name
)) {
532 ts_push_bt(4 | BT_SIGNED
);
536 if (tok_see() != '(')
537 err("unknown symbol <%s>\n", name
);
551 if (!t
.ptr
|| !o
.ptr
)
555 while (tok_jmp(')')) {
566 static void arrayderef(void)
572 if (!(t
.flags
& T_ARRAY
&& !t
.ptr
) && t
.addr
) {
574 o_deref(TYPE_BT(&t
));
589 static void inc_post(int op
)
591 struct type t
= ts
[nts
- 1];
592 /* pushing the value before inc */
598 /* increment by 1 or pointer size */
602 o_num(t
.ptr
> 0 ? type_szde(&t
) : 1);
606 o_assign(TYPE_BT(&t
));
610 static void readfield(void)
614 tok_expect(TOK_NAME
);
617 field
= struct_field(t
.id
, tok_id());
622 ts_push_addr(&field
->type
);
625 static struct funcinfo
{
626 struct type args
[NARGS
];
630 /* function and argument names; useful only when defining */
631 char argnames
[NARGS
][NAMELEN
];
636 static int func_create(struct type
*ret
, char *name
, char argnames
[][NAMELEN
],
637 struct type
*args
, int nargs
, int varg
)
639 struct funcinfo
*fi
= &funcs
[nfuncs
++];
641 if (nfuncs
>= NFUNCS
)
642 err("nomem: NFUNCS reached!\n");
643 memcpy(&fi
->ret
, ret
, sizeof(*ret
));
644 for (i
= 0; i
< nargs
; i
++)
645 memcpy(&fi
->args
[i
], &args
[i
], sizeof(*ret
));
648 strcpy(fi
->name
, name
? name
: "");
649 for (i
= 0; i
< nargs
; i
++)
650 strcpy(fi
->argnames
[i
], argnames
[i
]);
654 static void readcall(void)
660 if (t
.flags
& T_FUNC
&& t
.ptr
> 0)
662 fi
= t
.flags
& T_FUNC
? &funcs
[t
.id
] : NULL
;
663 if (tok_see() != ')') {
668 } while (!tok_jmp(','));
671 o_call(argc
, fi
? TYPE_BT(&fi
->ret
) : 4 | BT_SIGNED
);
673 if (TYPE_BT(&fi
->ret
))
674 o_cast(TYPE_BT(&fi
->ret
));
677 ts_push_bt(4 | BT_SIGNED
);
681 static void readpost(void)
695 if (!tok_jmp(TOK2("++"))) {
699 if (!tok_jmp(TOK2("--"))) {
707 if (!tok_jmp(TOK2("->"))) {
716 static void inc_pre(int op
)
720 /* copy the destination */
722 ts_push(&ts
[nts
- 1]);
723 /* increment by 1 or pointer size */
725 o_num(t
.ptr
> 0 ? type_szde(&t
) : 1);
727 /* assign the result */
728 o_assign(TYPE_BT(&t
));
732 static void readpre(void)
739 err("cannot use the address\n");
751 err("dereferencing non-pointer\n");
753 o_deref(TYPE_BT(&t
));
763 ts_push_bt(4 | BT_SIGNED
);
782 if (!tok_jmp(TOK2("++"))) {
786 if (!tok_jmp(TOK2("--"))) {
790 if (!tok_jmp(TOK_SIZEOF
)) {
792 int op
= !tok_jmp('(');
803 o_num(type_totsz(&t
));
811 static void readmul(void)
834 static void readadd(void)
852 static void shift(int op
)
856 ts_pop_de2(NULL
, &t
);
857 o_bop(op
| (BT_SIGNED
& TYPE_BT(&t
) ? O_SIGNED
: 0));
858 ts_push_bt(TYPE_BT(&t
));
861 static void readshift(void)
865 if (!tok_jmp(TOK2("<<"))) {
869 if (!tok_jmp(TOK2(">>"))) {
877 static void cmp(int op
)
882 ts_pop_de2(&t1
, &t2
);
883 bt
= bt_op(TYPE_BT(&t1
), TYPE_BT(&t2
));
884 o_bop(op
| (bt
& BT_SIGNED
? O_SIGNED
: 0));
885 ts_push_bt(4 | BT_SIGNED
);
888 static void readcmp(void)
900 if (!tok_jmp(TOK2("<="))) {
904 if (!tok_jmp(TOK2(">="))) {
912 static void eq(int op
)
915 ts_pop_de2(NULL
, NULL
);
917 ts_push_bt(4 | BT_SIGNED
);
920 static void readeq(void)
924 if (!tok_jmp(TOK2("=="))) {
928 if (!tok_jmp(TOK2("!="))) {
936 static void readbitand(void)
939 while (!tok_jmp('&')) {
945 static void readxor(void)
948 while (!tok_jmp('^')) {
954 static void readbitor(void)
957 while (!tok_jmp('|')) {
963 static void readand(void)
967 if (tok_see() != TOK2("&&"))
974 while (!tok_jmp(TOK2("&&"))) {
987 ts_push_bt(4 | BT_SIGNED
);
990 static void reador(void)
994 if (tok_see() != TOK2("||"))
1001 while (!tok_jmp(TOK2("||"))) {
1014 ts_push_bt(4 | BT_SIGNED
);
1017 static void readcexpr(void);
1019 static int readcexpr_const(void)
1027 /* both branches yield the same type; so ignore the first */
1035 /* making sure t->addr == 0 on both branches */
1042 static void readcexpr(void)
1050 if (readcexpr_const()) {
1051 int l_fail
= LABEL();
1052 int l_end
= LABEL();
1056 /* both branches yield the same type; so ignore the first */
1058 if (!TYPE_VOID(&ret
))
1065 /* making sure t->addr == 0 on both branches */
1067 if (!TYPE_VOID(&ret
)) {
1076 static void opassign(int op
, int ptrop
)
1078 struct type t
= ts
[nts
- 1];
1082 if (op
== O_ADD
|| op
== O_SUB
)
1086 o_assign(TYPE_BT(&ts
[nts
- 2]));
1091 static void doassign(void)
1093 struct type t
= ts
[nts
- 1];
1094 if (!t
.ptr
&& t
.flags
& T_STRUCT
) {
1096 o_num(type_totsz(&t
));
1100 o_assign(TYPE_BT(&ts
[nts
- 1]));
1105 static void readexpr(void)
1108 if (!tok_jmp('=')) {
1113 if (!tok_jmp(TOK2("+="))) {
1117 if (!tok_jmp(TOK2("-="))) {
1121 if (!tok_jmp(TOK2("*="))) {
1125 if (!tok_jmp(TOK2("/="))) {
1129 if (!tok_jmp(TOK2("%="))) {
1133 if (!tok_jmp(TOK3("<<="))) {
1137 if (!tok_jmp(TOK3(">>="))) {
1141 if (!tok_jmp(TOK3("&="))) {
1145 if (!tok_jmp(TOK3("|="))) {
1149 if (!tok_jmp(TOK3("^="))) {
1155 static void readestmt(void)
1161 } while (!tok_jmp(','));
1164 #define F_GLOBAL(flags) (!((flags) & F_STATIC))
1166 static void globalinit(void *obj
, int off
, struct type
*t
)
1168 struct name
*name
= obj
;
1169 char *elfname
= *name
->elfname
? name
->elfname
: name
->name
;
1170 if (t
->flags
& T_ARRAY
&& tok_see() == TOK_STR
) {
1171 struct type
*t_de
= &arrays
[t
->id
].type
;
1172 if (!t_de
->ptr
&& !t_de
->flags
&& TYPE_SZ(t_de
) == 1) {
1175 tok_expect(TOK_STR
);
1176 tok_str(&buf
, &len
);
1177 o_dscpy(name
->addr
+ off
, buf
, len
);
1182 o_dsset(elfname
, off
, TYPE_BT(t
));
1186 static void readfunc(struct name
*name
, int flags
);
1188 static void globaldef(void *data
, struct name
*name
, unsigned flags
)
1190 struct type
*t
= &name
->type
;
1191 char *elfname
= *name
->elfname
? name
->elfname
: name
->name
;
1193 if (t
->flags
& T_ARRAY
&& !t
->ptr
&& !arrays
[t
->id
].n
)
1194 if (~flags
& F_EXTERN
)
1195 arrays
[t
->id
].n
= initsize();
1197 if (!(flags
& F_EXTERN
) && (!(t
->flags
& T_FUNC
) || t
->ptr
)) {
1198 if (tok_see() == '=')
1199 name
->addr
= o_dsnew(elfname
, sz
, F_GLOBAL(flags
));
1201 o_bsnew(elfname
, sz
, F_GLOBAL(flags
));
1205 initexpr(t
, 0, name
, globalinit
);
1206 if (tok_see() == '{' && name
->type
.flags
& T_FUNC
)
1207 readfunc(name
, flags
);
1210 /* generate the address of local + off */
1211 static void o_localoff(long addr
, int off
)
1220 static void localinit(void *obj
, int off
, struct type
*t
)
1222 long addr
= *(long *) obj
;
1223 if (t
->flags
& T_ARRAY
&& tok_see() == TOK_STR
) {
1224 struct type
*t_de
= &arrays
[t
->id
].type
;
1225 if (!t_de
->ptr
&& !t_de
->flags
&& TYPE_SZ(t_de
) == 1) {
1228 tok_expect(TOK_STR
);
1229 tok_str(&buf
, &len
);
1230 o_localoff(addr
, off
);
1231 o_sym(tmp_str(buf
, len
));
1238 o_localoff(addr
, off
);
1246 /* current function name */
1247 static char func_name
[NAMELEN
];
1249 static void localdef(void *data
, struct name
*name
, unsigned flags
)
1251 struct type
*t
= &name
->type
;
1252 if ((flags
& F_EXTERN
) || ((t
->flags
& T_FUNC
) && !t
->ptr
)) {
1256 if (flags
& F_STATIC
) {
1257 sprintf(name
->elfname
, "__neatcc.%s.%s", func_name
, name
->name
);
1258 globaldef(data
, name
, flags
);
1261 if (t
->flags
& T_ARRAY
&& !t
->ptr
&& !arrays
[t
->id
].n
)
1262 arrays
[t
->id
].n
= initsize();
1263 name
->addr
= o_mklocal(type_totsz(&name
->type
));
1265 if (!tok_jmp('=')) {
1266 if (t
->flags
& (T_ARRAY
| T_STRUCT
) && !t
->ptr
) {
1267 o_local(name
->addr
);
1269 o_num(type_totsz(t
));
1273 initexpr(t
, 0, &name
->addr
, localinit
);
1277 static void typedefdef(void *data
, struct name
*name
, unsigned flags
)
1279 typedef_add(name
->name
, &name
->type
);
1282 static void readstmt(void);
1284 static void readswitch(void)
1286 int o_break
= l_break
;
1287 long val_addr
= o_mklocal(LONGSZ
);
1289 int ncases
= 0; /* number of case labels */
1290 int l_failed
= LABEL(); /* address of last failed jmp */
1291 int l_matched
= LABEL(); /* address of last walk through jmp */
1292 int l_default
= 0; /* default case label */
1299 o_assign(TYPE_BT(&t
));
1304 while (tok_jmp('}')) {
1305 if (tok_see() != TOK_CASE
&& tok_see() != TOK_DEFAULT
) {
1311 if (tok_get() == TOK_CASE
) {
1319 o_deref(TYPE_BT(&t
));
1326 l_default
= LABEL();
1331 l_matched
= LABEL();
1334 o_rmlocal(val_addr
, LONGSZ
);
1343 static char label_name
[NLABELS
][NAMELEN
];
1344 static int label_ids
[NLABELS
];
1347 static int label_id(char *name
)
1350 for (i
= nlabels
- 1; i
>= 0; --i
)
1351 if (!strcmp(label_name
[i
], name
))
1352 return label_ids
[i
];
1353 strcpy(label_name
[nlabels
], name
);
1354 label_ids
[nlabels
] = LABEL();
1355 return label_ids
[nlabels
++];
1358 static void readstmt(void)
1362 if (!tok_jmp('{')) {
1363 int _nlocals
= nlocals
;
1364 int _nglobals
= nglobals
;
1365 int _nenums
= nenums
;
1366 int _ntypedefs
= ntypedefs
;
1367 int _nstructs
= nstructs
;
1368 int _nfuncs
= nfuncs
;
1369 int _narrays
= narrays
;
1370 while (tok_jmp('}'))
1374 ntypedefs
= _ntypedefs
;
1375 nstructs
= _nstructs
;
1378 nglobals
= _nglobals
;
1381 if (!readdefs(localdef
, NULL
)) {
1385 if (!tok_jmp(TOK_TYPEDEF
)) {
1386 readdefs(typedefdef
, NULL
);
1390 if (!tok_jmp(TOK_IF
)) {
1391 int l_fail
= LABEL();
1392 int l_end
= LABEL();
1399 if (!tok_jmp(TOK_ELSE
)) {
1409 if (!tok_jmp(TOK_WHILE
)) {
1410 int o_break
= l_break
;
1411 int o_cont
= l_cont
;
1427 if (!tok_jmp(TOK_DO
)) {
1428 int o_break
= l_break
;
1429 int o_cont
= l_cont
;
1430 int l_beg
= LABEL();
1435 tok_expect(TOK_WHILE
);
1448 if (!tok_jmp(TOK_FOR
)) {
1449 int o_break
= l_break
;
1450 int o_cont
= l_cont
;
1451 int l_check
= LABEL(); /* for condition label */
1452 int l_body
= LABEL(); /* for block label */
1456 if (tok_see() != ';')
1460 if (tok_see() != ';') {
1468 if (tok_see() != ')')
1480 if (!tok_jmp(TOK_SWITCH
)) {
1484 if (!tok_jmp(TOK_RETURN
)) {
1485 int ret
= tok_see() != ';';
1494 if (!tok_jmp(TOK_BREAK
)) {
1499 if (!tok_jmp(TOK_CONTINUE
)) {
1504 if (!tok_jmp(TOK_GOTO
)) {
1505 tok_expect(TOK_NAME
);
1506 o_jmp(label_id(tok_id()));
1512 if (!tok_jmp(':')) {
1513 o_label(label_id(tok_id()));
1519 static void readfunc(struct name
*name
, int flags
)
1521 struct funcinfo
*fi
= &funcs
[name
->type
.id
];
1522 long beg
= tok_addr();
1524 strcpy(func_name
, fi
->name
);
1525 o_func_beg(func_name
, fi
->nargs
, F_GLOBAL(flags
), fi
->varg
);
1526 for (i
= 0; i
< fi
->nargs
; i
++) {
1527 struct name arg
= {"", "", fi
->args
[i
], o_arg2loc(i
)};
1528 strcpy(arg
.name
, fi
->argnames
[i
]);
1531 /* first pass: collecting statistics */
1537 /* second pass: generating code */
1543 func_name
[0] = '\0';
1547 static void readdecl(void)
1549 if (!tok_jmp(TOK_TYPEDEF
)) {
1550 readdefs(typedefdef
, NULL
);
1554 readdefs_int(globaldef
, NULL
);
1558 static void parse(void)
1560 while (tok_see() != TOK_EOF
)
1564 static void compat_macros(void)
1566 cpp_define("__STDC__", "");
1567 cpp_define("__linux__", "");
1568 cpp_define(I_ARCH
, "");
1570 /* ignored keywords */
1571 cpp_define("const", "");
1572 cpp_define("register", "");
1573 cpp_define("volatile", "");
1574 cpp_define("inline", "");
1575 cpp_define("restrict", "");
1576 cpp_define("__inline__", "");
1577 cpp_define("__restrict__", "");
1578 cpp_define("__attribute__(x)", "");
1579 cpp_define("__builtin_va_list__", "long");
1582 int main(int argc
, char *argv
[])
1588 while (i
< argc
&& argv
[i
][0] == '-') {
1589 if (argv
[i
][1] == 'I')
1590 cpp_addpath(argv
[i
][2] ? argv
[i
] + 2 : argv
[++i
]);
1591 if (argv
[i
][1] == 'D') {
1592 char *name
= argv
[i
] + 2;
1594 char *eq
= strchr(name
, '=');
1599 cpp_define(name
, def
);
1601 if (argv
[i
][1] == 'o')
1602 strcpy(obj
, argv
[i
][2] ? argv
[i
] + 2 : argv
[++i
]);
1606 die("neatcc: no file given\n");
1607 if (cpp_init(argv
[i
]))
1608 die("neatcc: cannot open <%s>\n", argv
[i
]);
1611 strcpy(obj
, argv
[i
]);
1612 obj
[strlen(obj
) - 1] = 'o';
1614 ofd
= open(obj
, O_WRONLY
| O_TRUNC
| O_CREAT
, 0600);
1621 /* parsing function and variable declarations */
1623 /* read the base type of a variable */
1624 static int basetype(struct type
*type
, unsigned *flags
)
1631 char name
[NAMELEN
] = "";
1637 switch (tok_see()) {
1669 isunion
= tok_get() == TOK_UNION
;
1670 if (!tok_jmp(TOK_NAME
))
1671 strcpy(name
, tok_id());
1672 if (tok_see() == '{')
1673 type
->id
= struct_create(name
, isunion
);
1675 type
->id
= struct_find(name
, isunion
);
1676 type
->flags
|= T_STRUCT
;
1682 if (tok_see() == '{')
1684 type
->bt
= 4 | BT_SIGNED
;
1687 if (tok_see() == TOK_NAME
) {
1688 int id
= typedef_find(tok_id());
1691 memcpy(type
, &typedefs
[id
].type
,
1704 type
->bt
= size
| (sign
? BT_SIGNED
: 0);
1708 static void readptrs(struct type
*type
)
1710 while (!tok_jmp('*')) {
1717 /* read function arguments */
1718 static int readargs(struct type
*args
, char argnames
[][NAMELEN
], int *varg
)
1723 while (tok_see() != ')') {
1724 if (!tok_jmp(TOK3("..."))) {
1728 if (readname(&args
[nargs
], argnames
[nargs
], NULL
)) {
1729 /* argument has no type, assume int */
1730 tok_expect(TOK_NAME
);
1731 memset(&args
[nargs
], 0, sizeof(struct type
));
1732 args
[nargs
].bt
= 4 | BT_SIGNED
;
1733 strcpy(argnames
[nargs
], tok_id());
1735 /* argument arrays are pointers */
1736 array2ptr(&args
[nargs
]);
1743 if (nargs
== 1 && !TYPE_BT(&args
[0]))
1748 /* read K&R function arguments */
1749 static void krdef(void *data
, struct name
*name
, unsigned flags
)
1751 struct funcinfo
*fi
= data
;
1753 for (i
= 0; i
< fi
->nargs
; i
++)
1754 if (!strcmp(fi
->argnames
[i
], name
->name
))
1755 memcpy(&fi
->args
[i
], &name
->type
, sizeof(name
->type
));
1759 * readarrays() parses array specifiers when reading a definition in
1760 * readname(). The "type" parameter contains the type contained in the
1761 * inner array; for instance, type in "int *a[10][20]" would be an int
1762 * pointer. When returning, the "type" parameter is changed to point
1763 * to the final array. The function returns a pointer to the type in
1764 * the inner array; this is useful when the type is not complete yet,
1765 * like when creating an array of function pointers as in
1766 * "int (*f[10])(int)". If there is no array brackets, NULL is returned.
1768 static struct type
*readarrays(struct type
*type
)
1771 struct type
*inner
= NULL
;
1774 while (!tok_jmp('[')) {
1780 err("const expr expected\n");
1785 for (i
= nar
- 1; i
>= 0; i
--) {
1786 type
->id
= array_add(type
, arsz
[i
]);
1788 inner
= &arrays
[type
->id
].type
;
1789 type
->flags
= T_ARRAY
;
1797 * readname() reads a variable definition; the name is copied into
1798 * "name" and the type is copied into "main" argument. The "base"
1799 * argument, if not NULL, indicates the base type of the variable.
1800 * For instance, the base type of "a" and "b" in "int *a, b[10]" is
1801 * "int". If NULL, basetype() is called directly to read the base
1802 * type of the variable. readname() returns zero, only if the
1803 * variable can be read.
1805 static int readname(struct type
*main
, char *name
, struct type
*base
)
1807 struct type tpool
[3];
1809 struct type
*type
= &tpool
[npool
++];
1810 struct type
*ptype
= NULL
; /* type inside parenthesis */
1811 struct type
*btype
= NULL
; /* type before parenthesis */
1814 memset(tpool
, 0, sizeof(tpool
));
1818 if (basetype(type
, &flags
))
1821 memcpy(type
, base
, sizeof(*base
));
1824 if (!tok_jmp('(')) {
1826 type
= &tpool
[npool
++];
1830 if (!tok_jmp(TOK_NAME
) && name
)
1831 strcpy(name
, tok_id());
1832 inner
= readarrays(type
);
1837 if (tok_see() == '(') {
1838 struct type args
[NARGS
];
1839 char argnames
[NARGS
][NAMELEN
];
1841 int nargs
= readargs(args
, argnames
, &varg
);
1844 type
= &tpool
[npool
++];
1847 ptype
->flags
= T_FUNC
;
1849 ptype
->id
= func_create(btype
, name
, argnames
, args
, nargs
, varg
);
1850 if (tok_see() != ';')
1851 while (tok_see() != '{' && !readdefs(krdef
, &funcs
[ptype
->id
]))
1854 if (ptype
&& readarrays(type
))
1857 memcpy(main
, type
, sizeof(*type
));
1861 static int readtype(struct type
*type
)
1863 return readname(type
, NULL
, NULL
);
1867 * readdef() reads a variable definitions statement. The definition
1868 * statement can appear in anywhere: global variables, function
1869 * local variables, struct fields, and typedefs. For each defined
1870 * variable, def() callback is called with the appropriate name
1871 * struct and flags; the callback should finish parsing the definition
1872 * by possibly reading the initializer expression and saving the name
1875 static int readdefs(void (*def
)(void *data
, struct name
*name
, unsigned flags
),
1879 unsigned base_flags
;
1880 if (basetype(&base
, &base_flags
))
1882 if (tok_see() == ';' || tok_see() == '{')
1885 struct name name
= {{""}};
1886 if (readname(&name
.type
, name
.name
, &base
))
1888 def(data
, &name
, base_flags
);
1889 } while (!tok_jmp(','));
1893 /* just like readdefs, but default to int type; for handling K&R functions */
1894 static int readdefs_int(void (*def
)(void *data
, struct name
*name
, unsigned flags
),
1899 if (basetype(&base
, &flags
)) {
1900 if (tok_see() != TOK_NAME
)
1902 memset(&base
, 0, sizeof(base
));
1903 base
.bt
= 4 | BT_SIGNED
;
1905 if (tok_see() != ';') {
1907 struct name name
= {{""}};
1908 if (readname(&name
.type
, name
.name
, &base
))
1910 def(data
, &name
, flags
);
1911 } while (!tok_jmp(','));
1917 /* parsing initializer expressions */
1919 static void jumpbrace(void)
1922 while (tok_see() != '}' || depth
--)
1923 if (tok_get() == '{')
1928 /* compute the size of the initializer expression */
1929 static int initsize(void)
1931 long addr
= tok_addr();
1935 if (!tok_jmp(TOK_STR
)) {
1941 while (tok_jmp('}')) {
1943 if (!tok_jmp('[')) {
1952 while (tok_see() != '}' && tok_see() != ',')
1953 if (tok_get() == '{')
1961 static struct type
*innertype(struct type
*t
)
1963 if (t
->flags
& T_ARRAY
&& !t
->ptr
)
1964 return innertype(&arrays
[t
->id
].type
);
1968 /* read the initializer expression and initialize basic types using set() cb */
1969 static void initexpr(struct type
*t
, int off
, void *obj
,
1970 void (*set
)(void *obj
, int off
, struct type
*t
))
1976 if (!t
->ptr
&& t
->flags
& T_STRUCT
) {
1977 struct structinfo
*si
= &structs
[t
->id
];
1979 for (i
= 0; i
< si
->nfields
&& tok_see() != '}'; i
++) {
1980 struct name
*field
= &si
->fields
[i
];
1981 if (!tok_jmp('.')) {
1982 tok_expect(TOK_NAME
);
1983 field
= struct_field(t
->id
, tok_id());
1986 initexpr(&field
->type
, off
+ field
->addr
, obj
, set
);
1990 } else if (t
->flags
& T_ARRAY
) {
1991 struct type
*t_de
= &arrays
[t
->id
].type
;
1993 /* handling extra braces as in: char s[] = {"sth"} */
1994 if (TYPE_SZ(t_de
) == 1 && tok_see() == TOK_STR
) {
1999 for (i
= 0; tok_see() != '}'; i
++) {
2001 struct type
*it
= t_de
;
2002 if (!tok_jmp('[')) {
2009 if (tok_see() != '{' && (tok_see() != TOK_STR
||
2010 !(it
->flags
& T_ARRAY
)))
2011 it
= innertype(t_de
);
2012 initexpr(it
, off
+ type_totsz(it
) * idx
, obj
, set
);