2 * neatcc - a small and simple C compiler
4 * Copyright (C) 2010-2011 Ali Gholami Rudi
6 * This program is released under GNU GPL version 2.
14 #include <sys/types.h>
19 static int nogen
; /* don't generate code */
21 #define o_bop(op) {if (!nogen) o_bop(op);}
22 #define o_uop(op) {if (!nogen) o_uop(op);}
23 #define o_cast(bt) {if (!nogen) o_cast(bt);}
24 #define o_memcpy() {if (!nogen) o_memcpy();}
25 #define o_memset() {if (!nogen) o_memset();}
26 #define o_call(argc, ret) {if (!nogen) o_call(argc, ret);}
27 #define o_ret(ret) {if (!nogen) o_ret(ret);}
28 #define o_assign(bt) {if (!nogen) o_assign(bt);}
29 #define o_deref(bt) {if (!nogen) o_deref(bt);}
30 #define o_load() {if (!nogen) o_load();}
31 #define o_popnum(c) (nogen ? 0 : o_popnum(c))
32 #define o_num(n) {if (!nogen) o_num(n);}
33 #define o_local(addr) {if (!nogen) o_local(addr);}
34 #define o_sym(sym) {if (!nogen) o_sym(sym);}
35 #define o_tmpdrop(n) {if (!nogen) o_tmpdrop(n);}
36 #define o_tmpswap() {if (!nogen) o_tmpswap();}
37 #define o_tmpcopy() {if (!nogen) o_tmpcopy();}
38 #define o_mklabel() (nogen ? 0 : o_mklabel())
39 #define o_jz(addr) (nogen ? 0 : o_jz(addr))
40 #define o_jnz(addr) (nogen ? 0 : o_jnz(addr))
41 #define o_jmp(addr) (nogen ? 0 : o_jmp(addr))
42 #define o_filljmp(addr) {if (!nogen) o_filljmp(addr);}
43 #define o_filljmp2(addr, dst) {if (!nogen) o_filljmp2(addr, dst);}
44 #define o_fork() {if (!nogen) o_fork();}
45 #define o_forkpush() {if (!nogen) o_forkpush();}
46 #define o_forkjoin() {if (!nogen) o_forkjoin();}
48 #define MAXLOCALS (1 << 10)
49 #define MAXGLOBALS (1 << 10)
50 #define MAXARGS (1 << 5)
52 #define ALIGN(x, a) (((x) + (a) - 1) & ~((a) - 1))
53 #define MIN(a, b) ((a) < (b) ? (a) : (b))
55 #define TYPE_BT(t) ((t)->ptr ? LONGSZ : (t)->bt)
56 #define TYPE_SZ(t) ((t)->ptr ? LONGSZ : (t)->bt & BT_SZMASK)
58 /* type->flag values */
63 /* variable definition flags */
71 int id
; /* for structs, functions and arrays */
72 int addr
; /* the address is passed to gen.c; deref for value */
76 static struct type ts
[MAXTMP
];
79 static void ts_push_bt(unsigned bt
)
87 static void ts_push(struct type
*t
)
89 struct type
*d
= &ts
[nts
++];
90 memcpy(d
, t
, sizeof(*t
));
93 static void ts_push_addr(struct type
*t
)
99 static void ts_pop(struct type
*type
)
108 die("%s: %s", cpp_loc(tok_addr()), msg
);
113 char elfname
[NAMELEN
]; /* local elf name for static variables in function */
115 long addr
; /* local stack offset, global data addr, struct offset */
118 static struct name locals
[MAXLOCALS
];
120 static struct name globals
[MAXGLOBALS
];
123 static void local_add(struct name
*name
)
125 if (nlocals
>= MAXLOCALS
)
126 err("nomem: MAXLOCALS reached!\n");
127 memcpy(&locals
[nlocals
++], name
, sizeof(*name
));
130 static int local_find(char *name
)
133 for (i
= nlocals
- 1; i
>= 0; --i
)
134 if (!strcmp(locals
[i
].name
, name
))
139 static int global_find(char *name
)
142 for (i
= nglobals
- 1; i
>= 0; i
--)
143 if (!strcmp(name
, globals
[i
].name
))
148 static void global_add(struct name
*name
)
150 if (nglobals
>= MAXGLOBALS
)
151 err("nomem: MAXGLOBALS reached!\n");
152 memcpy(&globals
[nglobals
++], name
, sizeof(*name
));
155 #define MAXENUMS (1 << 10)
157 static struct enumval
{
163 static void enum_add(char *name
, int val
)
165 struct enumval
*ev
= &enums
[nenums
++];
166 if (nenums
>= MAXENUMS
)
167 err("nomem: MAXENUMS reached!\n");
168 strcpy(ev
->name
, name
);
172 static int enum_find(int *val
, char *name
)
175 for (i
= nenums
- 1; i
>= 0; --i
)
176 if (!strcmp(name
, enums
[i
].name
)) {
183 #define MAXTYPEDEFS (1 << 10)
185 static struct typdefinfo
{
188 } typedefs
[MAXTYPEDEFS
];
189 static int ntypedefs
;
191 static void typedef_add(char *name
, struct type
*type
)
193 struct typdefinfo
*ti
= &typedefs
[ntypedefs
++];
194 if (ntypedefs
>= MAXTYPEDEFS
)
195 err("nomem: MAXTYPEDEFS reached!\n");
196 strcpy(ti
->name
, name
);
197 memcpy(&ti
->type
, type
, sizeof(*type
));
200 static int typedef_find(char *name
)
203 for (i
= ntypedefs
- 1; i
>= 0; --i
)
204 if (!strcmp(name
, typedefs
[i
].name
))
209 #define MAXARRAYS (1 << 10)
211 static struct array
{
217 static int array_add(struct type
*type
, int n
)
219 struct array
*a
= &arrays
[narrays
++];
220 if (narrays
>= MAXARRAYS
)
221 err("nomem: MAXARRAYS reached!\n");
222 memcpy(&a
->type
, type
, sizeof(*type
));
227 static void array2ptr(struct type
*t
)
229 if (t
->flags
& T_ARRAY
&& !t
->ptr
) {
230 memcpy(t
, &arrays
[t
->id
].type
, sizeof(*t
));
235 #define MAXSTRUCTS (1 << 10)
236 #define MAXFIELDS (1 << 7)
238 static struct structinfo
{
240 struct name fields
[MAXFIELDS
];
244 } structs
[MAXSTRUCTS
];
247 static int struct_find(char *name
, int isunion
)
250 for (i
= nstructs
- 1; i
>= 0; --i
)
251 if (*structs
[i
].name
&& !strcmp(name
, structs
[i
].name
) &&
252 structs
[i
].isunion
== isunion
)
255 if (nstructs
>= MAXSTRUCTS
)
256 err("nomem: MAXTYPES reached!\n");
257 memset(&structs
[i
], 0, sizeof(structs
[i
]));
258 strcpy(structs
[i
].name
, name
);
259 structs
[i
].isunion
= isunion
;
263 static struct name
*struct_field(int id
, char *name
)
265 struct structinfo
*si
= &structs
[id
];
267 for (i
= 0; i
< si
->nfields
; i
++)
268 if (!strcmp(name
, si
->fields
[i
].name
))
269 return &si
->fields
[i
];
270 err("field not found\n");
273 #define MAXBREAK (1 << 7)
275 static long breaks
[MAXBREAK
];
277 static long continues
[MAXBREAK
];
278 static int ncontinues
;
280 static void break_fill(long addr
, int till
)
283 for (i
= till
; i
< nbreaks
; i
++)
284 o_filljmp2(breaks
[i
], addr
);
288 static void continue_fill(long addr
, int till
)
291 for (i
= till
; i
< ncontinues
; i
++)
292 o_filljmp2(continues
[i
], addr
);
296 /* return t's size */
297 static int type_totsz(struct type
*t
)
301 if (t
->flags
& T_ARRAY
)
302 return arrays
[t
->id
].n
* type_totsz(&arrays
[t
->id
].type
);
303 return t
->flags
& T_STRUCT
? structs
[t
->id
].size
: BT_SZ(t
->bt
);
306 /* return t's dereferenced size */
307 static unsigned type_szde(struct type
*t
)
312 return type_totsz(&de
);
315 /* dereference stack top if t->addr (ie. address is pushed to gen.c) */
316 static void ts_de(int deref
)
318 struct type
*t
= &ts
[nts
- 1];
320 if (deref
&& t
->addr
&& (t
->ptr
|| !(t
->flags
& T_FUNC
)))
325 /* pop stack pop to *t and dereference if t->addr */
326 static void ts_pop_de(struct type
*t
)
332 /* pop the top 2 stack values and dereference them if t->addr */
333 static void ts_pop_de2(struct type
*t1
, struct type
*t2
)
341 static int tok_jmp(int tok
)
343 if (tok_see() != tok
)
349 static void tok_expect(int tok
)
351 if (tok_get() != tok
)
352 err("syntax error\n");
355 static unsigned bt_op(unsigned bt1
, unsigned bt2
)
357 unsigned s1
= BT_SZ(bt1
);
358 unsigned s2
= BT_SZ(bt2
);
359 return (bt1
| bt2
) & BT_SIGNED
| (s1
> s2
? s1
: s2
);
362 static void ts_binop(int op
)
366 ts_pop_de2(&t1
, &t2
);
367 if (op
== O_DIV
|| op
== O_MOD
)
370 bt
= bt_op(TYPE_BT(&t1
), TYPE_BT(&t2
));
371 o_bop(op
| (bt
& BT_SIGNED
? O_SIGNED
: 0));
375 static void ts_addop(int op
)
378 ts_pop_de2(&t1
, &t2
);
379 if (!t1
.ptr
&& !t2
.ptr
) {
381 ts_push_bt(bt_op(TYPE_BT(&t1
), TYPE_BT(&t2
)));
384 if (t1
.ptr
&& !t2
.ptr
)
386 if (!t1
.ptr
&& t2
.ptr
)
387 if (type_szde(&t2
) > 1) {
388 o_num(type_szde(&t2
));
391 if (t1
.ptr
&& !t2
.ptr
)
394 if (t1
.ptr
&& t2
.ptr
) {
395 int sz
= type_szde(&t1
);
400 ts_push_bt(4 | BT_SIGNED
);
402 ts_push(t1
.ptr
? &t1
: &t2
);
406 /* function prototypes for parsing function and variable declarations */
407 static int readname(struct type
*main
, char *name
, struct type
*base
);
408 static int readtype(struct type
*type
);
409 static int readdefs(void (*def
)(void *data
, struct name
*name
, unsigned flags
),
411 static int readdefs_int(void (*def
)(void *data
, struct name
*name
, unsigned flags
),
414 /* function prototypes for parsing initializer expressions */
415 static int initsize(void);
416 static void initexpr(struct type
*t
, int off
, void *obj
,
417 void (*set
)(void *obj
, int off
, struct type
*t
));
419 static int type_alignment(struct type
*t
)
421 if (t
->flags
& T_ARRAY
&& !t
->ptr
)
422 return type_alignment(&arrays
[t
->id
].type
);
423 if (t
->flags
& T_STRUCT
&& !t
->ptr
)
424 return type_alignment(&structs
[t
->id
].fields
[0].type
);
425 return MIN(LONGSZ
, type_totsz(t
));
428 static void structdef(void *data
, struct name
*name
, unsigned flags
)
430 struct structinfo
*si
= data
;
433 if (si
->size
< type_totsz(&name
->type
))
434 si
->size
= type_totsz(&name
->type
);
436 struct type
*t
= &name
->type
;
437 int alignment
= type_alignment(t
);
438 if (t
->flags
& T_ARRAY
&& !t
->ptr
)
439 alignment
= MIN(LONGSZ
, type_totsz(&arrays
[t
->id
].type
));
440 si
->size
= ALIGN(si
->size
, alignment
);
441 name
->addr
= si
->size
;
442 si
->size
+= type_totsz(&name
->type
);
444 memcpy(&si
->fields
[si
->nfields
++], name
, sizeof(*name
));
447 static int struct_create(char *name
, int isunion
)
449 int id
= struct_find(name
, isunion
);
450 struct structinfo
*si
= &structs
[id
];
452 while (tok_jmp('}')) {
453 readdefs(structdef
, si
);
459 static void readexpr(void);
461 static void enum_create(void)
465 while (tok_jmp('}')) {
467 tok_expect(TOK_NAME
);
468 strcpy(name
, tok_id());
473 err("const expr expected!\n");
480 /* used to differenciate labels from case and cond exprs */
484 static void readpre(void);
486 static char *tmp_str(char *buf
, int len
)
488 static char name
[NAMELEN
];
491 sprintf(name
, "__neatcc.s%d", id
++);
492 dat
= o_mkdat(name
, len
, 0);
493 memcpy(dat
, buf
, len
);
497 static void readprimary(void)
499 if (!tok_jmp(TOK_NUM
)) {
501 int bt
= tok_num(&n
);
506 if (!tok_jmp(TOK_STR
)) {
510 t
.bt
= 1 | BT_SIGNED
;
516 o_sym(tmp_str(buf
, len
));
519 if (!tok_jmp(TOK_NAME
)) {
520 struct name unkn
= {""};
521 char *name
= unkn
.name
;
523 strcpy(name
, tok_id());
524 /* don't search for labels here */
525 if (!ncexpr
&& !caseexpr
&& tok_see() == ':')
527 if ((n
= local_find(name
)) != -1) {
528 struct name
*l
= &locals
[n
];
530 ts_push_addr(&l
->type
);
533 if ((n
= global_find(name
)) != -1) {
534 struct name
*g
= &globals
[n
];
535 o_sym(*g
->elfname
? g
->elfname
: g
->name
);
536 ts_push_addr(&g
->type
);
539 if (!enum_find(&n
, name
)) {
540 ts_push_bt(4 | BT_SIGNED
);
544 if (tok_see() != '(')
545 err("unknown symbol\n");
559 if (!t
.ptr
|| !o
.ptr
)
563 while (tok_jmp(')')) {
574 static void arrayderef(void)
580 if (!(t
.flags
& T_ARRAY
&& !t
.ptr
) && t
.addr
) {
582 o_deref(TYPE_BT(&t
));
597 static void inc_post(int op
)
599 struct type t
= ts
[nts
- 1];
600 /* pushing the value before inc */
606 /* increment by 1 or pointer size */
610 o_num(t
.ptr
> 0 ? type_szde(&t
) : 1);
614 o_assign(TYPE_BT(&t
));
618 static void readfield(void)
622 tok_expect(TOK_NAME
);
625 field
= struct_field(t
.id
, tok_id());
630 ts_push_addr(&field
->type
);
633 #define MAXFUNCS (1 << 10)
635 static struct funcinfo
{
636 struct type args
[MAXARGS
];
640 /* function and argument names; useful only when defining */
641 char argnames
[MAXARGS
][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
>= MAXFUNCS
)
652 err("nomem: MAXFUNCS 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
);
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");
761 err("dereferencing non-pointer\n");
763 o_deref(TYPE_BT(&t
));
773 ts_push_bt(4 | BT_SIGNED
);
788 if (!tok_jmp(TOK2("++"))) {
792 if (!tok_jmp(TOK2("--"))) {
796 if (!tok_jmp(TOK_SIZEOF
)) {
798 int op
= !tok_jmp('(');
809 o_num(type_totsz(&t
));
817 static void readmul(void)
840 static void readadd(void)
858 static void shift(int op
)
862 ts_pop_de2(NULL
, &t
);
863 o_bop(op
| (BT_SIGNED
& TYPE_BT(&t
) ? O_SIGNED
: 0));
864 ts_push_bt(TYPE_BT(&t
));
867 static void readshift(void)
871 if (!tok_jmp(TOK2("<<"))) {
875 if (!tok_jmp(TOK2(">>"))) {
883 static void cmp(int op
)
888 ts_pop_de2(&t1
, &t2
);
889 bt
= bt_op(TYPE_BT(&t1
), TYPE_BT(&t2
));
890 o_bop(op
| (bt
& BT_SIGNED
? O_SIGNED
: 0));
891 ts_push_bt(4 | BT_SIGNED
);
894 static void readcmp(void)
906 if (!tok_jmp(TOK2("<="))) {
910 if (!tok_jmp(TOK2(">="))) {
918 static void eq(int op
)
921 ts_pop_de2(NULL
, NULL
);
923 ts_push_bt(4 | BT_SIGNED
);
926 static void readeq(void)
930 if (!tok_jmp(TOK2("=="))) {
934 if (!tok_jmp(TOK2("!="))) {
942 static void readbitand(void)
945 while (!tok_jmp('&')) {
951 static void readxor(void)
954 while (!tok_jmp('^')) {
960 static void readbitor(void)
963 while (!tok_jmp('|')) {
969 #define MAXCOND (1 << 7)
971 static void readand(void)
978 if (tok_see() != TOK2("&&"))
982 conds
[nconds
++] = o_jz(0);
983 while (!tok_jmp(TOK2("&&"))) {
986 conds
[nconds
++] = o_jz(0);
991 for (i
= 0; i
< nconds
; i
++)
997 ts_push_bt(4 | BT_SIGNED
);
1000 static void reador(void)
1002 long conds
[MAXCOND
];
1007 if (tok_see() != TOK2("||"))
1011 conds
[nconds
++] = o_jnz(0);
1012 while (!tok_jmp(TOK2("||"))) {
1015 conds
[nconds
++] = o_jnz(0);
1020 for (i
= 0; i
< nconds
; i
++)
1021 o_filljmp(conds
[i
]);
1026 ts_push_bt(4 | BT_SIGNED
);
1029 static void readcexpr(void);
1031 static int readcexpr_const(void)
1039 /* both branches yield the same type; so ignore the first */
1047 /* making sure t->addr == 0 on both branches */
1054 static void readcexpr(void)
1063 if (readcexpr_const()) {
1066 /* both branches yield the same type; so ignore the first */
1074 /* making sure t->addr == 0 on both branches */
1083 static void opassign(int op
, int ptrop
)
1085 struct type t
= ts
[nts
- 1];
1090 o_assign(TYPE_BT(&ts
[nts
- 2]));
1095 static void doassign(void)
1097 struct type t
= ts
[nts
- 1];
1098 if (!t
.ptr
&& t
.flags
& T_STRUCT
) {
1100 o_num(type_totsz(&t
));
1104 o_assign(TYPE_BT(&ts
[nts
- 1]));
1109 static void readexpr(void)
1112 if (!tok_jmp('=')) {
1117 if (!tok_jmp(TOK2("+="))) {
1121 if (!tok_jmp(TOK2("-="))) {
1125 if (!tok_jmp(TOK2("*="))) {
1129 if (!tok_jmp(TOK2("/="))) {
1133 if (!tok_jmp(TOK2("%="))) {
1137 if (!tok_jmp(TOK3("<<="))) {
1141 if (!tok_jmp(TOK3(">>="))) {
1145 if (!tok_jmp(TOK3("&="))) {
1149 if (!tok_jmp(TOK3("|="))) {
1153 if (!tok_jmp(TOK3("^="))) {
1159 static void readestmt(void)
1165 } while (!tok_jmp(','));
1168 #define F_GLOBAL(flags) (!((flags) & F_STATIC))
1170 static void globalinit(void *obj
, int off
, struct type
*t
)
1172 struct name
*name
= obj
;
1173 char *elfname
= *name
->elfname
? name
->elfname
: name
->name
;
1174 if (t
->flags
& T_ARRAY
&& tok_see() == TOK_STR
) {
1175 struct type
*t_de
= &arrays
[t
->id
].type
;
1176 if (!t_de
->ptr
&& !t_de
->flags
&& TYPE_SZ(t_de
) == 1) {
1179 tok_expect(TOK_STR
);
1181 memcpy((void *) name
->addr
+ off
, buf
, len
);
1186 o_datset(elfname
, off
, TYPE_BT(t
));
1190 static void readfunc(struct name
*name
, int flags
);
1192 static void globaldef(void *data
, struct name
*name
, unsigned flags
)
1194 struct type
*t
= &name
->type
;
1195 char *elfname
= *name
->elfname
? name
->elfname
: name
->name
;
1197 if (t
->flags
& T_ARRAY
&& !t
->ptr
&& !arrays
[t
->id
].n
)
1198 if (~flags
& F_EXTERN
)
1199 arrays
[t
->id
].n
= initsize();
1201 if (!(flags
& F_EXTERN
) && (!(t
->flags
& T_FUNC
) || t
->ptr
)) {
1202 if (tok_see() == '=')
1203 name
->addr
= (long) o_mkdat(elfname
, sz
, F_GLOBAL(flags
));
1205 o_mkbss(elfname
, sz
, F_GLOBAL(flags
));
1209 initexpr(t
, 0, name
, globalinit
);
1210 if (tok_see() == '{' && name
->type
.flags
& T_FUNC
)
1211 readfunc(name
, flags
);
1214 /* generate the address of local + off */
1215 static void o_localoff(long addr
, int off
)
1224 static void localinit(void *obj
, int off
, struct type
*t
)
1226 long addr
= *(long *) obj
;
1227 if (t
->flags
& T_ARRAY
&& tok_see() == TOK_STR
) {
1228 struct type
*t_de
= &arrays
[t
->id
].type
;
1229 if (!t_de
->ptr
&& !t_de
->flags
&& TYPE_SZ(t_de
) == 1) {
1232 tok_expect(TOK_STR
);
1234 o_localoff(addr
, off
);
1235 o_sym(tmp_str(buf
, len
));
1242 o_localoff(addr
, off
);
1250 /* current function name */
1251 static char func_name
[NAMELEN
];
1253 static void localdef(void *data
, struct name
*name
, unsigned flags
)
1255 struct type
*t
= &name
->type
;
1256 if ((flags
& F_EXTERN
) || (t
->flags
& T_FUNC
) && !t
->ptr
) {
1260 if (flags
& F_STATIC
) {
1261 sprintf(name
->elfname
, "__neatcc.%s.%s", func_name
, name
->name
);
1262 globaldef(data
, name
, flags
);
1265 if (t
->flags
& T_ARRAY
&& !t
->ptr
&& !arrays
[t
->id
].n
)
1266 arrays
[t
->id
].n
= initsize();
1267 name
->addr
= o_mklocal(type_totsz(&name
->type
));
1269 if (!tok_jmp('=')) {
1270 if (t
->flags
& (T_ARRAY
| T_STRUCT
) && !t
->ptr
) {
1271 o_local(name
->addr
);
1273 o_num(type_totsz(t
));
1277 initexpr(t
, 0, &name
->addr
, localinit
);
1281 /* read K&R function arguments */
1282 void krdef(void *data
, struct name
*name
, unsigned flags
)
1284 struct funcinfo
*fi
= data
;
1286 for (i
= 0; i
< fi
->nargs
; i
++)
1287 if (!strcmp(fi
->argnames
[i
], name
->name
))
1288 memcpy(&fi
->args
[i
], &name
->type
, sizeof(name
->type
));
1291 static void typedefdef(void *data
, struct name
*name
, unsigned flags
)
1293 typedef_add(name
->name
, &name
->type
);
1296 static void readstmt(void);
1298 #define MAXCASES (1 << 7)
1300 static void readswitch(void)
1302 int break_beg
= nbreaks
;
1303 long val_addr
= o_mklocal(LONGSZ
);
1305 int ncases
= 0; /* number of case labels */
1306 long last_failed
= -1; /* address of last failed jmp */
1307 long last_matched
= -1; /* address of last walk through jmp */
1308 long default_addr
= -1; /* address of the default label */
1314 o_assign(TYPE_BT(&t
));
1319 while (tok_jmp('}')) {
1320 if (tok_see() != TOK_CASE
&& tok_see() != TOK_DEFAULT
) {
1325 last_matched
= o_jmp(0);
1326 if (tok_get() == TOK_CASE
) {
1327 if (last_failed
>= 0)
1328 o_filljmp(last_failed
);
1334 o_deref(TYPE_BT(&t
));
1336 last_failed
= o_jz(0);
1340 last_failed
= o_jmp(0);
1341 default_addr
= o_mklabel();
1344 if (last_matched
>= 0)
1345 o_filljmp(last_matched
);
1348 o_rmlocal(val_addr
, LONGSZ
);
1349 if (last_failed
>= 0)
1350 o_filljmp2(last_failed
,
1351 default_addr
>= 0 ? default_addr
: o_mklabel());
1352 break_fill(o_mklabel(), break_beg
);
1355 #define MAXGOTO (1 << 10)
1357 static struct gotoinfo
{
1363 static struct labelinfo
{
1369 static void goto_add(char *name
)
1371 strcpy(gotos
[ngotos
].name
, name
);
1372 gotos
[ngotos
++].addr
= o_jmp(0);
1375 static void label_add(char *name
)
1377 strcpy(labels
[nlabels
].name
, name
);
1378 labels
[nlabels
++].addr
= o_mklabel();
1381 static void goto_fill(void)
1384 for (i
= 0; i
< ngotos
; i
++)
1385 for (j
= 0; j
< nlabels
; j
++)
1386 if (!strcmp(gotos
[i
].name
, labels
[j
].name
)) {
1387 o_filljmp2(gotos
[i
].addr
, labels
[j
].addr
);
1392 static void readstmt(void)
1396 if (!tok_jmp('{')) {
1397 int _nlocals
= nlocals
;
1398 int _nglobals
= nglobals
;
1399 int _nenums
= nenums
;
1400 int _ntypedefs
= ntypedefs
;
1401 int _nstructs
= nstructs
;
1402 int _nfuncs
= nfuncs
;
1403 int _narrays
= narrays
;
1404 while (tok_jmp('}'))
1408 ntypedefs
= _ntypedefs
;
1409 nstructs
= _nstructs
;
1412 nglobals
= _nglobals
;
1415 if (!readdefs(localdef
, NULL
)) {
1419 if (!tok_jmp(TOK_TYPEDEF
)) {
1420 readdefs(typedefdef
, NULL
);
1424 if (!tok_jmp(TOK_IF
)) {
1432 if (!tok_jmp(TOK_ELSE
)) {
1442 if (!tok_jmp(TOK_WHILE
)) {
1444 int break_beg
= nbreaks
;
1445 int continue_beg
= ncontinues
;
1455 break_fill(o_mklabel(), break_beg
);
1456 continue_fill(l1
, continue_beg
);
1459 if (!tok_jmp(TOK_DO
)) {
1461 int break_beg
= nbreaks
;
1462 int continue_beg
= ncontinues
;
1465 tok_expect(TOK_WHILE
);
1472 break_fill(o_mklabel(), break_beg
);
1473 continue_fill(l2
, continue_beg
);
1477 if (!tok_jmp(TOK_FOR
)) {
1478 long l_check
, l_jump
, j_fail
, j_pass
;
1479 int break_beg
= nbreaks
;
1480 int continue_beg
= ncontinues
;
1483 if (tok_see() != ';')
1486 l_check
= o_mklabel();
1487 if (tok_see() != ';') {
1495 l_jump
= o_mklabel();
1496 if (tok_see() != ')')
1505 break_fill(o_mklabel(), break_beg
);
1506 continue_fill(l_jump
, continue_beg
);
1509 if (!tok_jmp(TOK_SWITCH
)) {
1513 if (!tok_jmp(TOK_RETURN
)) {
1514 int ret
= tok_see() != ';';
1523 if (!tok_jmp(TOK_BREAK
)) {
1525 breaks
[nbreaks
++] = o_jmp(0);
1528 if (!tok_jmp(TOK_CONTINUE
)) {
1530 continues
[ncontinues
++] = o_jmp(0);
1533 if (!tok_jmp(TOK_GOTO
)) {
1534 tok_expect(TOK_NAME
);
1541 if (!tok_jmp(':')) {
1542 label_add(tok_id());
1548 static void readfunc(struct name
*name
, int flags
)
1550 struct funcinfo
*fi
= &funcs
[name
->type
.id
];
1552 strcpy(func_name
, fi
->name
);
1553 o_func_beg(func_name
, fi
->nargs
, F_GLOBAL(flags
), fi
->varg
);
1554 for (i
= 0; i
< fi
->nargs
; i
++) {
1555 struct name arg
= {"", "", fi
->args
[i
], o_arg2loc(i
)};
1556 strcpy(arg
.name
, fi
->argnames
[i
]);
1562 func_name
[0] = '\0';
1568 static void readdecl(void)
1570 if (!tok_jmp(TOK_TYPEDEF
)) {
1571 readdefs(typedefdef
, NULL
);
1575 readdefs_int(globaldef
, NULL
);
1579 static void parse(void)
1581 while (tok_see() != TOK_EOF
)
1585 static void compat_macros(void)
1587 cpp_define("__STDC__", "");
1588 cpp_define("__linux__", "");
1590 cpp_define("__arm__", "");
1592 cpp_define("__i386__", "");
1595 /* ignored keywords */
1596 cpp_define("const", "");
1597 cpp_define("register", "");
1598 cpp_define("volatile", "");
1599 cpp_define("inline", "");
1600 cpp_define("restrict", "");
1601 cpp_define("__inline__", "");
1602 cpp_define("__restrict__", "");
1603 cpp_define("__attribute__(x)", "");
1604 cpp_define("__builtin_va_list__", "long");
1607 int main(int argc
, char *argv
[])
1613 while (i
< argc
&& argv
[i
][0] == '-') {
1614 if (argv
[i
][1] == 'I')
1615 cpp_addpath(argv
[i
][2] ? argv
[i
] + 2 : argv
[++i
]);
1616 if (argv
[i
][1] == 'D') {
1617 char *name
= argv
[i
] + 2;
1619 char *eq
= strchr(name
, '=');
1624 cpp_define(name
, def
);
1626 if (argv
[i
][1] == 'o')
1627 strcpy(obj
, argv
[i
][2] ? argv
[i
] + 2 : argv
[++i
]);
1631 die("neatcc: no file given\n");
1632 if (cpp_init(argv
[i
]))
1633 die("neatcc: cannot open <%s>\n", argv
[i
]);
1636 strcpy(obj
, argv
[i
]);
1637 obj
[strlen(obj
) - 1] = 'o';
1639 ofd
= open(obj
, O_WRONLY
| O_TRUNC
| O_CREAT
, 0600);
1646 /* parsing function and variable declarations */
1648 /* read the base type of a variable */
1649 static int basetype(struct type
*type
, unsigned *flags
)
1656 char name
[NAMELEN
] = "";
1662 switch (tok_see()) {
1694 isunion
= tok_get() == TOK_UNION
;
1695 if (!tok_jmp(TOK_NAME
))
1696 strcpy(name
, tok_id());
1697 if (tok_see() == '{')
1698 type
->id
= struct_create(name
, isunion
);
1700 type
->id
= struct_find(name
, isunion
);
1701 type
->flags
|= T_STRUCT
;
1707 if (tok_see() == '{')
1709 type
->bt
= 4 | BT_SIGNED
;
1712 if (tok_see() == TOK_NAME
) {
1713 int id
= typedef_find(tok_id());
1716 memcpy(type
, &typedefs
[id
].type
,
1729 type
->bt
= size
| (sign
? BT_SIGNED
: 0);
1733 static void readptrs(struct type
*type
)
1735 while (!tok_jmp('*')) {
1742 /* read function arguments */
1743 static int readargs(struct type
*args
, char argnames
[][NAMELEN
], int *varg
)
1748 while (tok_see() != ')') {
1749 if (!tok_jmp(TOK3("..."))) {
1753 if (readname(&args
[nargs
], argnames
[nargs
], NULL
)) {
1754 /* argument has no type, assume int */
1755 tok_expect(TOK_NAME
);
1756 memset(&args
[nargs
], 0, sizeof(struct type
));
1757 args
[nargs
].bt
= 4 | BT_SIGNED
;
1758 strcpy(argnames
[nargs
], tok_id());
1760 /* argument arrays are pointers */
1761 array2ptr(&args
[nargs
]);
1768 if (nargs
== 1 && !TYPE_BT(&args
[0]))
1774 * readarrays() parses array specifiers when reading a definition in
1775 * readname(). The "type" parameter contains the type contained in the
1776 * inner array; for instance, type in "int *a[10][20]" would be an int
1777 * pointer. When returning, the "type" parameter is changed to point
1778 * to the final array. The function returns a pointer to the type in
1779 * the inner array; this is useful when the type is not complete yet,
1780 * like when creating an array of function pointers as in
1781 * "int (*f[10])(int)". If there is no array brackets, NULL is returned.
1783 static struct type
*readarrays(struct type
*type
)
1786 struct type
*inner
= NULL
;
1789 while (!tok_jmp('[')) {
1795 err("const expr expected\n");
1800 for (i
= nar
- 1; i
>= 0; i
--) {
1801 type
->id
= array_add(type
, arsz
[i
]);
1803 inner
= &arrays
[type
->id
].type
;
1804 type
->flags
= T_ARRAY
;
1812 * readname() reads a variable definition; the name is copied into
1813 * "name" and the type is copied into "main" argument. The "base"
1814 * argument, if not NULL, indicates the base type of the variable.
1815 * For instance, the base type of "a" and "b" in "int *a, b[10]" is
1816 * "int". If NULL, basetype() is called directly to read the base
1817 * type of the variable. readname() returns zero, only if the
1818 * variable can be read.
1820 static int readname(struct type
*main
, char *name
, struct type
*base
)
1822 struct type tpool
[3];
1824 struct type
*type
= &tpool
[npool
++];
1825 struct type
*ptype
= NULL
; /* type inside parenthesis */
1826 struct type
*btype
= NULL
; /* type before parenthesis */
1829 memset(tpool
, 0, sizeof(tpool
));
1833 if (basetype(type
, &flags
))
1836 memcpy(type
, base
, sizeof(*base
));
1839 if (!tok_jmp('(')) {
1841 type
= &tpool
[npool
++];
1845 if (!tok_jmp(TOK_NAME
) && name
)
1846 strcpy(name
, tok_id());
1847 inner
= readarrays(type
);
1852 if (tok_see() == '(') {
1853 struct type args
[MAXARGS
];
1854 char argnames
[MAXARGS
][NAMELEN
];
1856 int nargs
= readargs(args
, argnames
, &varg
);
1859 type
= &tpool
[npool
++];
1862 ptype
->flags
= T_FUNC
;
1864 ptype
->id
= func_create(btype
, name
, argnames
, args
, nargs
, varg
);
1865 if (tok_see() != ';')
1866 while (tok_see() != '{' && !readdefs(krdef
, &funcs
[ptype
->id
]))
1869 if (ptype
&& readarrays(type
))
1872 memcpy(main
, type
, sizeof(*type
));
1876 static int readtype(struct type
*type
)
1878 return readname(type
, NULL
, NULL
);
1882 * readdef() reads a variable definitions statement. The definition
1883 * statement can appear in anywhere: global variables, function
1884 * local variables, struct fields, and typedefs. For each defined
1885 * variable, def() callback is called with the appropriate name
1886 * struct and flags; the callback should finish parsing the definition
1887 * by possibly reading the initializer expression and saving the name
1890 static int readdefs(void (*def
)(void *data
, struct name
*name
, unsigned flags
),
1894 unsigned base_flags
;
1895 if (basetype(&base
, &base_flags
))
1897 if (tok_see() == ';' || tok_see() == '{')
1900 struct name name
= {{""}};
1901 if (readname(&name
.type
, name
.name
, &base
))
1903 def(data
, &name
, base_flags
);
1904 } while (!tok_jmp(','));
1908 /* just like readdefs, but default to int type; for handling K&R functions */
1909 static int readdefs_int(void (*def
)(void *data
, struct name
*name
, unsigned flags
),
1914 if (basetype(&base
, &flags
)) {
1915 if (tok_see() != TOK_NAME
)
1917 memset(&base
, 0, sizeof(base
));
1918 base
.bt
= 4 | BT_SIGNED
;
1920 if (tok_see() != ';') {
1922 struct name name
= {{""}};
1923 if (readname(&name
.type
, name
.name
, &base
))
1925 def(data
, &name
, flags
);
1926 } while (!tok_jmp(','));
1932 /* parsing initializer expressions */
1934 static void jumpbrace(void)
1937 while (tok_see() != '}' || depth
--)
1938 if (tok_get() == '{')
1943 /* compute the size of the initializer expression */
1944 static int initsize(void)
1946 long addr
= tok_addr();
1949 err("array size unspecified");
1950 if (!tok_jmp(TOK_STR
)) {
1956 while (tok_jmp('}')) {
1958 if (!tok_jmp('[')) {
1967 while (tok_see() != '}' && tok_see() != ',')
1968 if (tok_get() == '{')
1976 static struct type
*innertype(struct type
*t
)
1978 if (t
->flags
& T_ARRAY
&& !t
->ptr
)
1979 return innertype(&arrays
[t
->id
].type
);
1983 /* read the initializer expression and initialize basic types using set() cb */
1984 static void initexpr(struct type
*t
, int off
, void *obj
,
1985 void (*set
)(void *obj
, int off
, struct type
*t
))
1991 if (!t
->ptr
&& t
->flags
& T_STRUCT
) {
1992 struct structinfo
*si
= &structs
[t
->id
];
1994 for (i
= 0; i
< si
->nfields
&& tok_see() != '}'; i
++) {
1995 struct name
*field
= &si
->fields
[i
];
1996 if (!tok_jmp('.')) {
1997 tok_expect(TOK_NAME
);
1998 field
= struct_field(t
->id
, tok_id());
2001 initexpr(&field
->type
, off
+ field
->addr
, obj
, set
);
2005 } else if (t
->flags
& T_ARRAY
) {
2006 struct type
*t_de
= &arrays
[t
->id
].type
;
2008 /* handling extra braces as in: char s[] = {"sth"} */
2009 if (TYPE_SZ(t_de
) == 1 && tok_see() == TOK_STR
) {
2014 for (i
= 0; tok_see() != '}'; i
++) {
2016 struct type
*it
= t_de
;
2017 if (!tok_jmp('[')) {
2024 if (tok_see() != '{' && (tok_see() != TOK_STR
||
2025 !(it
->flags
& T_ARRAY
)))
2026 it
= innertype(t_de
);
2027 initexpr(it
, off
+ type_totsz(it
) * idx
, obj
, set
);