2 * neatcc - a small and simple C compiler
4 * Copyright (C) 2010-2013 Ali Gholami Rudi
6 * This program is released under the Modified BSD license.
15 #include <sys/types.h>
20 static int nogen
; /* don't generate code */
22 #define o_bop(op) {if (!nogen) o_bop(op);}
23 #define o_uop(op) {if (!nogen) o_uop(op);}
24 #define o_cast(bt) {if (!nogen) o_cast(bt);}
25 #define o_memcpy() {if (!nogen) o_memcpy();}
26 #define o_memset() {if (!nogen) o_memset();}
27 #define o_call(argc, ret) {if (!nogen) o_call(argc, ret);}
28 #define o_ret(ret) {if (!nogen) o_ret(ret);}
29 #define o_assign(bt) {if (!nogen) o_assign(bt);}
30 #define o_deref(bt) {if (!nogen) o_deref(bt);}
31 #define o_load() {if (!nogen) o_load();}
32 #define o_popnum(c) (nogen ? 0 : o_popnum(c))
33 #define o_num(n) {if (!nogen) o_num(n);}
34 #define o_local(addr) {if (!nogen) o_local(addr);}
35 #define o_sym(sym) {if (!nogen) o_sym(sym);}
36 #define o_tmpdrop(n) {if (!nogen) o_tmpdrop(n);}
37 #define o_tmpswap() {if (!nogen) o_tmpswap();}
38 #define o_tmpcopy() {if (!nogen) o_tmpcopy();}
39 #define o_label(id) {if (!nogen) o_label(id);}
40 #define o_jz(id) {if (!nogen) o_jz(id);}
41 #define o_jnz(id) {if (!nogen) o_jnz(id);}
42 #define o_jmp(id) {if (!nogen) o_jmp(id);}
43 #define o_fork() {if (!nogen) o_fork();}
44 #define o_forkpush() {if (!nogen) o_forkpush();}
45 #define o_forkjoin() {if (!nogen) o_forkjoin();}
47 #define MAXLOCALS (1 << 10)
48 #define MAXGLOBALS (1 << 10)
49 #define MAXARGS (1 << 5)
51 #define ALIGN(x, a) (((x) + (a) - 1) & ~((a) - 1))
52 #define MIN(a, b) ((a) < (b) ? (a) : (b))
54 #define TYPE_BT(t) ((t)->ptr ? LONGSZ : (t)->bt)
55 #define TYPE_SZ(t) ((t)->ptr ? LONGSZ : (t)->bt & BT_SZMASK)
56 #define TYPE_VOID(t) (!(t)->bt && !(t)->flags && !(t)->ptr)
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
)
106 void err(char *fmt
, ...)
111 vsprintf(msg
, fmt
, ap
);
113 die("%s: %s", cpp_loc(tok_addr()), msg
);
118 char elfname
[NAMELEN
]; /* local elf name for static variables in function */
120 long addr
; /* local stack offset, global data addr, struct offset */
123 static struct name locals
[MAXLOCALS
];
125 static struct name globals
[MAXGLOBALS
];
128 static void local_add(struct name
*name
)
130 if (nlocals
>= MAXLOCALS
)
131 err("nomem: MAXLOCALS reached!\n");
132 memcpy(&locals
[nlocals
++], name
, sizeof(*name
));
135 static int local_find(char *name
)
138 for (i
= nlocals
- 1; i
>= 0; --i
)
139 if (!strcmp(locals
[i
].name
, name
))
144 static int global_find(char *name
)
147 for (i
= nglobals
- 1; i
>= 0; i
--)
148 if (!strcmp(name
, globals
[i
].name
))
153 static void global_add(struct name
*name
)
155 if (nglobals
>= MAXGLOBALS
)
156 err("nomem: MAXGLOBALS reached!\n");
157 memcpy(&globals
[nglobals
++], 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 #define MAXENUMS (1 << 10)
168 static struct enumval
{
174 static void enum_add(char *name
, int val
)
176 struct enumval
*ev
= &enums
[nenums
++];
177 if (nenums
>= MAXENUMS
)
178 err("nomem: MAXENUMS reached!\n");
179 strcpy(ev
->name
, name
);
183 static int enum_find(int *val
, char *name
)
186 for (i
= nenums
- 1; i
>= 0; --i
)
187 if (!strcmp(name
, enums
[i
].name
)) {
194 #define MAXTYPEDEFS (1 << 10)
196 static struct typdefinfo
{
199 } typedefs
[MAXTYPEDEFS
];
200 static int ntypedefs
;
202 static void typedef_add(char *name
, struct type
*type
)
204 struct typdefinfo
*ti
= &typedefs
[ntypedefs
++];
205 if (ntypedefs
>= MAXTYPEDEFS
)
206 err("nomem: MAXTYPEDEFS reached!\n");
207 strcpy(ti
->name
, name
);
208 memcpy(&ti
->type
, type
, sizeof(*type
));
211 static int typedef_find(char *name
)
214 for (i
= ntypedefs
- 1; i
>= 0; --i
)
215 if (!strcmp(name
, typedefs
[i
].name
))
220 #define MAXARRAYS (1 << 10)
222 static struct array
{
228 static int array_add(struct type
*type
, int n
)
230 struct array
*a
= &arrays
[narrays
++];
231 if (narrays
>= MAXARRAYS
)
232 err("nomem: MAXARRAYS reached!\n");
233 memcpy(&a
->type
, type
, sizeof(*type
));
238 static void array2ptr(struct type
*t
)
240 if (t
->flags
& T_ARRAY
&& !t
->ptr
) {
241 memcpy(t
, &arrays
[t
->id
].type
, sizeof(*t
));
246 #define MAXSTRUCTS (1 << 10)
247 #define MAXFIELDS (1 << 7)
249 static struct structinfo
{
251 struct name fields
[MAXFIELDS
];
255 } structs
[MAXSTRUCTS
];
258 static int struct_find(char *name
, int isunion
)
261 for (i
= nstructs
- 1; i
>= 0; --i
)
262 if (*structs
[i
].name
&& !strcmp(name
, structs
[i
].name
) &&
263 structs
[i
].isunion
== isunion
)
266 if (nstructs
>= MAXSTRUCTS
)
267 err("nomem: MAXTYPES reached!\n");
268 memset(&structs
[i
], 0, sizeof(structs
[i
]));
269 strcpy(structs
[i
].name
, name
);
270 structs
[i
].isunion
= isunion
;
274 static struct name
*struct_field(int id
, char *name
)
276 struct structinfo
*si
= &structs
[id
];
278 for (i
= 0; i
< si
->nfields
; i
++)
279 if (!strcmp(name
, si
->fields
[i
].name
))
280 return &si
->fields
[i
];
281 err("field not found\n");
285 /* return t's size */
286 static int type_totsz(struct type
*t
)
290 if (t
->flags
& T_ARRAY
)
291 return arrays
[t
->id
].n
* type_totsz(&arrays
[t
->id
].type
);
292 return t
->flags
& T_STRUCT
? structs
[t
->id
].size
: BT_SZ(t
->bt
);
295 /* return t's dereferenced size */
296 static unsigned type_szde(struct type
*t
)
301 return type_totsz(&de
);
304 /* dereference stack top if t->addr (ie. address is pushed to gen.c) */
305 static void ts_de(int deref
)
307 struct type
*t
= &ts
[nts
- 1];
309 if (deref
&& t
->addr
&& (t
->ptr
|| !(t
->flags
& T_FUNC
)))
314 /* pop stack pop to *t and dereference if t->addr */
315 static void ts_pop_de(struct type
*t
)
321 /* pop the top 2 stack values and dereference them if t->addr */
322 static void ts_pop_de2(struct type
*t1
, struct type
*t2
)
330 static int tok_jmp(int tok
)
332 if (tok_see() != tok
)
338 static void tok_expect(int tok
)
340 if (tok_get() != tok
)
341 err("syntax error\n");
344 static unsigned bt_op(unsigned bt1
, unsigned bt2
)
346 unsigned s1
= BT_SZ(bt1
);
347 unsigned s2
= BT_SZ(bt2
);
348 return ((bt1
| bt2
) & BT_SIGNED
) | (s1
> s2
? s1
: s2
);
351 static void ts_binop(int op
)
355 ts_pop_de2(&t1
, &t2
);
356 if (op
== O_DIV
|| op
== O_MOD
)
359 bt
= bt_op(TYPE_BT(&t1
), TYPE_BT(&t2
));
360 o_bop(op
| (bt
& BT_SIGNED
? O_SIGNED
: 0));
364 static void ts_addop(int op
)
367 ts_pop_de2(&t1
, &t2
);
368 if (!t1
.ptr
&& !t2
.ptr
) {
370 ts_push_bt(bt_op(TYPE_BT(&t1
), TYPE_BT(&t2
)));
373 if (t1
.ptr
&& !t2
.ptr
)
375 if (!t1
.ptr
&& t2
.ptr
)
376 if (type_szde(&t2
) > 1) {
377 o_num(type_szde(&t2
));
380 if (t1
.ptr
&& !t2
.ptr
)
383 if (t1
.ptr
&& t2
.ptr
) {
384 int sz
= type_szde(&t1
);
389 ts_push_bt(4 | BT_SIGNED
);
391 ts_push(t1
.ptr
? &t1
: &t2
);
395 /* function prototypes for parsing function and variable declarations */
396 static int readname(struct type
*main
, char *name
, struct type
*base
);
397 static int readtype(struct type
*type
);
398 static int readdefs(void (*def
)(void *data
, struct name
*name
, unsigned flags
),
400 static int readdefs_int(void (*def
)(void *data
, struct name
*name
, unsigned flags
),
403 /* function prototypes for parsing initializer expressions */
404 static int initsize(void);
405 static void initexpr(struct type
*t
, int off
, void *obj
,
406 void (*set
)(void *obj
, int off
, struct type
*t
));
408 static int type_alignment(struct type
*t
)
410 if (t
->flags
& T_ARRAY
&& !t
->ptr
)
411 return type_alignment(&arrays
[t
->id
].type
);
412 if (t
->flags
& T_STRUCT
&& !t
->ptr
)
413 return type_alignment(&structs
[t
->id
].fields
[0].type
);
414 return MIN(LONGSZ
, type_totsz(t
));
417 static void structdef(void *data
, struct name
*name
, unsigned flags
)
419 struct structinfo
*si
= data
;
422 if (si
->size
< type_totsz(&name
->type
))
423 si
->size
= type_totsz(&name
->type
);
425 struct type
*t
= &name
->type
;
426 int alignment
= type_alignment(t
);
427 if (t
->flags
& T_ARRAY
&& !t
->ptr
)
428 alignment
= MIN(LONGSZ
, type_totsz(&arrays
[t
->id
].type
));
429 si
->size
= ALIGN(si
->size
, alignment
);
430 name
->addr
= si
->size
;
431 si
->size
+= type_totsz(&name
->type
);
433 memcpy(&si
->fields
[si
->nfields
++], name
, sizeof(*name
));
436 static int struct_create(char *name
, int isunion
)
438 int id
= struct_find(name
, isunion
);
439 struct structinfo
*si
= &structs
[id
];
441 while (tok_jmp('}')) {
442 readdefs(structdef
, si
);
448 static void readexpr(void);
450 static void enum_create(void)
454 while (tok_jmp('}')) {
456 tok_expect(TOK_NAME
);
457 strcpy(name
, tok_id());
462 err("const expr expected!\n");
469 /* used to differenciate labels from case and cond exprs */
473 static void readpre(void);
475 static char *tmp_str(char *buf
, int len
)
477 static char name
[NAMELEN
];
480 sprintf(name
, "__neatcc.s%d", id
++);
481 dat
= o_mkdat(name
, len
, 0);
482 memcpy(dat
, buf
, len
);
486 static void readprimary(void)
488 if (!tok_jmp(TOK_NUM
)) {
490 int bt
= tok_num(&n
);
495 if (!tok_jmp(TOK_STR
)) {
496 struct type t
= {}; /* char type inside the arrays */
497 struct type a
= {}; /* the char array type */
499 int len
= tok_str(buf
);
500 t
.bt
= 1 | BT_SIGNED
;
501 a
.id
= array_add(&t
, len
);
504 o_sym(tmp_str(buf
, len
));
507 if (!tok_jmp(TOK_NAME
)) {
508 struct name unkn
= {""};
509 char *name
= unkn
.name
;
511 strcpy(name
, tok_id());
512 /* don't search for labels here */
513 if (!ncexpr
&& !caseexpr
&& tok_see() == ':')
515 if ((n
= local_find(name
)) != -1) {
516 struct name
*l
= &locals
[n
];
518 ts_push_addr(&l
->type
);
521 if ((n
= global_find(name
)) != -1) {
522 struct name
*g
= &globals
[n
];
523 o_sym(*g
->elfname
? g
->elfname
: g
->name
);
524 ts_push_addr(&g
->type
);
527 if (!enum_find(&n
, name
)) {
528 ts_push_bt(4 | BT_SIGNED
);
532 if (tok_see() != '(')
533 err("unknown symbol <%s>\n", name
);
547 if (!t
.ptr
|| !o
.ptr
)
551 while (tok_jmp(')')) {
562 static void arrayderef(void)
568 if (!(t
.flags
& T_ARRAY
&& !t
.ptr
) && t
.addr
) {
570 o_deref(TYPE_BT(&t
));
585 static void inc_post(int op
)
587 struct type t
= ts
[nts
- 1];
588 /* pushing the value before inc */
594 /* increment by 1 or pointer size */
598 o_num(t
.ptr
> 0 ? type_szde(&t
) : 1);
602 o_assign(TYPE_BT(&t
));
606 static void readfield(void)
610 tok_expect(TOK_NAME
);
613 field
= struct_field(t
.id
, tok_id());
618 ts_push_addr(&field
->type
);
621 #define MAXFUNCS (1 << 10)
623 static struct funcinfo
{
624 struct type args
[MAXARGS
];
628 /* function and argument names; useful only when defining */
629 char argnames
[MAXARGS
][NAMELEN
];
634 static int func_create(struct type
*ret
, char *name
, char argnames
[][NAMELEN
],
635 struct type
*args
, int nargs
, int varg
)
637 struct funcinfo
*fi
= &funcs
[nfuncs
++];
639 if (nfuncs
>= MAXFUNCS
)
640 err("nomem: MAXFUNCS reached!\n");
641 memcpy(&fi
->ret
, ret
, sizeof(*ret
));
642 for (i
= 0; i
< nargs
; i
++)
643 memcpy(&fi
->args
[i
], &args
[i
], sizeof(*ret
));
646 strcpy(fi
->name
, name
? name
: "");
647 for (i
= 0; i
< nargs
; i
++)
648 strcpy(fi
->argnames
[i
], argnames
[i
]);
652 static void readcall(void)
658 if (t
.flags
& T_FUNC
&& t
.ptr
> 0)
660 fi
= t
.flags
& T_FUNC
? &funcs
[t
.id
] : NULL
;
661 if (tok_see() != ')') {
666 } while (!tok_jmp(','));
669 o_call(argc
, fi
? TYPE_BT(&fi
->ret
) : 4 | BT_SIGNED
);
671 if (TYPE_BT(&fi
->ret
))
672 o_cast(TYPE_BT(&fi
->ret
));
675 ts_push_bt(4 | BT_SIGNED
);
679 static void readpost(void)
693 if (!tok_jmp(TOK2("++"))) {
697 if (!tok_jmp(TOK2("--"))) {
705 if (!tok_jmp(TOK2("->"))) {
714 static void inc_pre(int op
)
718 /* copy the destination */
720 ts_push(&ts
[nts
- 1]);
721 /* increment by 1 or pointer size */
723 o_num(t
.ptr
> 0 ? type_szde(&t
) : 1);
725 /* assign the result */
726 o_assign(TYPE_BT(&t
));
730 static void readpre(void)
737 err("cannot use the address\n");
749 err("dereferencing non-pointer\n");
751 o_deref(TYPE_BT(&t
));
761 ts_push_bt(4 | BT_SIGNED
);
780 if (!tok_jmp(TOK2("++"))) {
784 if (!tok_jmp(TOK2("--"))) {
788 if (!tok_jmp(TOK_SIZEOF
)) {
790 int op
= !tok_jmp('(');
801 o_num(type_totsz(&t
));
809 static void readmul(void)
832 static void readadd(void)
850 static void shift(int op
)
854 ts_pop_de2(NULL
, &t
);
855 o_bop(op
| (BT_SIGNED
& TYPE_BT(&t
) ? O_SIGNED
: 0));
856 ts_push_bt(TYPE_BT(&t
));
859 static void readshift(void)
863 if (!tok_jmp(TOK2("<<"))) {
867 if (!tok_jmp(TOK2(">>"))) {
875 static void cmp(int op
)
880 ts_pop_de2(&t1
, &t2
);
881 bt
= bt_op(TYPE_BT(&t1
), TYPE_BT(&t2
));
882 o_bop(op
| (bt
& BT_SIGNED
? O_SIGNED
: 0));
883 ts_push_bt(4 | BT_SIGNED
);
886 static void readcmp(void)
898 if (!tok_jmp(TOK2("<="))) {
902 if (!tok_jmp(TOK2(">="))) {
910 static void eq(int op
)
913 ts_pop_de2(NULL
, NULL
);
915 ts_push_bt(4 | BT_SIGNED
);
918 static void readeq(void)
922 if (!tok_jmp(TOK2("=="))) {
926 if (!tok_jmp(TOK2("!="))) {
934 static void readbitand(void)
937 while (!tok_jmp('&')) {
943 static void readxor(void)
946 while (!tok_jmp('^')) {
952 static void readbitor(void)
955 while (!tok_jmp('|')) {
961 #define MAXCOND (1 << 7)
963 static void readand(void)
966 int l_fail
= LABEL();
968 if (tok_see() != TOK2("&&"))
973 while (!tok_jmp(TOK2("&&"))) {
986 ts_push_bt(4 | BT_SIGNED
);
989 static void reador(void)
991 int l_pass
= LABEL();
994 if (tok_see() != TOK2("||"))
999 while (!tok_jmp(TOK2("||"))) {
1012 ts_push_bt(4 | BT_SIGNED
);
1015 static void readcexpr(void);
1017 static int readcexpr_const(void)
1025 /* both branches yield the same type; so ignore the first */
1033 /* making sure t->addr == 0 on both branches */
1040 static void readcexpr(void)
1048 if (readcexpr_const()) {
1049 int l_fail
= LABEL();
1050 int l_end
= LABEL();
1054 /* both branches yield the same type; so ignore the first */
1056 if (!TYPE_VOID(&ret
))
1063 /* making sure t->addr == 0 on both branches */
1065 if (!TYPE_VOID(&ret
)) {
1074 static void opassign(int op
, int ptrop
)
1076 struct type t
= ts
[nts
- 1];
1080 if (op
== O_ADD
|| op
== O_SUB
)
1084 o_assign(TYPE_BT(&ts
[nts
- 2]));
1089 static void doassign(void)
1091 struct type t
= ts
[nts
- 1];
1092 if (!t
.ptr
&& t
.flags
& T_STRUCT
) {
1094 o_num(type_totsz(&t
));
1098 o_assign(TYPE_BT(&ts
[nts
- 1]));
1103 static void readexpr(void)
1106 if (!tok_jmp('=')) {
1111 if (!tok_jmp(TOK2("+="))) {
1115 if (!tok_jmp(TOK2("-="))) {
1119 if (!tok_jmp(TOK2("*="))) {
1123 if (!tok_jmp(TOK2("/="))) {
1127 if (!tok_jmp(TOK2("%="))) {
1131 if (!tok_jmp(TOK3("<<="))) {
1135 if (!tok_jmp(TOK3(">>="))) {
1139 if (!tok_jmp(TOK3("&="))) {
1143 if (!tok_jmp(TOK3("|="))) {
1147 if (!tok_jmp(TOK3("^="))) {
1153 static void readestmt(void)
1159 } while (!tok_jmp(','));
1162 #define F_GLOBAL(flags) (!((flags) & F_STATIC))
1164 static void globalinit(void *obj
, int off
, struct type
*t
)
1166 struct name
*name
= obj
;
1167 char *elfname
= *name
->elfname
? name
->elfname
: name
->name
;
1168 if (t
->flags
& T_ARRAY
&& tok_see() == TOK_STR
) {
1169 struct type
*t_de
= &arrays
[t
->id
].type
;
1170 if (!t_de
->ptr
&& !t_de
->flags
&& TYPE_SZ(t_de
) == 1) {
1173 tok_expect(TOK_STR
);
1175 memcpy((void *) name
->addr
+ off
, buf
, len
);
1180 o_datset(elfname
, off
, TYPE_BT(t
));
1184 static void readfunc(struct name
*name
, int flags
);
1186 static void globaldef(void *data
, struct name
*name
, unsigned flags
)
1188 struct type
*t
= &name
->type
;
1189 char *elfname
= *name
->elfname
? name
->elfname
: name
->name
;
1191 if (t
->flags
& T_ARRAY
&& !t
->ptr
&& !arrays
[t
->id
].n
)
1192 if (~flags
& F_EXTERN
)
1193 arrays
[t
->id
].n
= initsize();
1195 if (!(flags
& F_EXTERN
) && (!(t
->flags
& T_FUNC
) || t
->ptr
)) {
1196 if (tok_see() == '=')
1197 name
->addr
= (long) o_mkdat(elfname
, sz
, F_GLOBAL(flags
));
1199 o_mkbss(elfname
, sz
, F_GLOBAL(flags
));
1203 initexpr(t
, 0, name
, globalinit
);
1204 if (tok_see() == '{' && name
->type
.flags
& T_FUNC
)
1205 readfunc(name
, flags
);
1208 /* generate the address of local + off */
1209 static void o_localoff(long addr
, int off
)
1218 static void localinit(void *obj
, int off
, struct type
*t
)
1220 long addr
= *(long *) obj
;
1221 if (t
->flags
& T_ARRAY
&& tok_see() == TOK_STR
) {
1222 struct type
*t_de
= &arrays
[t
->id
].type
;
1223 if (!t_de
->ptr
&& !t_de
->flags
&& TYPE_SZ(t_de
) == 1) {
1226 tok_expect(TOK_STR
);
1228 o_localoff(addr
, off
);
1229 o_sym(tmp_str(buf
, len
));
1236 o_localoff(addr
, off
);
1244 /* current function name */
1245 static char func_name
[NAMELEN
];
1247 static void localdef(void *data
, struct name
*name
, unsigned flags
)
1249 struct type
*t
= &name
->type
;
1250 if ((flags
& F_EXTERN
) || ((t
->flags
& T_FUNC
) && !t
->ptr
)) {
1254 if (flags
& F_STATIC
) {
1255 sprintf(name
->elfname
, "__neatcc.%s.%s", func_name
, name
->name
);
1256 globaldef(data
, name
, flags
);
1259 if (t
->flags
& T_ARRAY
&& !t
->ptr
&& !arrays
[t
->id
].n
)
1260 arrays
[t
->id
].n
= initsize();
1261 name
->addr
= o_mklocal(type_totsz(&name
->type
));
1263 if (!tok_jmp('=')) {
1264 if (t
->flags
& (T_ARRAY
| T_STRUCT
) && !t
->ptr
) {
1265 o_local(name
->addr
);
1267 o_num(type_totsz(t
));
1271 initexpr(t
, 0, &name
->addr
, localinit
);
1275 static void typedefdef(void *data
, struct name
*name
, unsigned flags
)
1277 typedef_add(name
->name
, &name
->type
);
1280 static void readstmt(void);
1282 #define MAXCASES (1 << 7)
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 #define MAXLABELS (1 << 10)
1345 static char label_name
[MAXLABELS
][NAMELEN
];
1346 static int label_ids
[MAXLABELS
];
1349 static int label_id(char *name
)
1352 for (i
= nlabels
- 1; i
>= 0; --i
)
1353 if (!strcmp(label_name
[i
], name
))
1354 return label_ids
[i
];
1355 strcpy(label_name
[nlabels
], name
);
1356 label_ids
[nlabels
] = LABEL();
1357 return label_ids
[nlabels
++];
1360 static void readstmt(void)
1364 if (!tok_jmp('{')) {
1365 int _nlocals
= nlocals
;
1366 int _nglobals
= nglobals
;
1367 int _nenums
= nenums
;
1368 int _ntypedefs
= ntypedefs
;
1369 int _nstructs
= nstructs
;
1370 int _nfuncs
= nfuncs
;
1371 int _narrays
= narrays
;
1372 while (tok_jmp('}'))
1376 ntypedefs
= _ntypedefs
;
1377 nstructs
= _nstructs
;
1380 nglobals
= _nglobals
;
1383 if (!readdefs(localdef
, NULL
)) {
1387 if (!tok_jmp(TOK_TYPEDEF
)) {
1388 readdefs(typedefdef
, NULL
);
1392 if (!tok_jmp(TOK_IF
)) {
1393 int l_fail
= LABEL();
1394 int l_end
= LABEL();
1401 if (!tok_jmp(TOK_ELSE
)) {
1411 if (!tok_jmp(TOK_WHILE
)) {
1412 int o_break
= l_break
;
1413 int o_cont
= l_cont
;
1429 if (!tok_jmp(TOK_DO
)) {
1430 int o_break
= l_break
;
1431 int o_cont
= l_cont
;
1432 int l_beg
= LABEL();
1437 tok_expect(TOK_WHILE
);
1450 if (!tok_jmp(TOK_FOR
)) {
1451 int o_break
= l_break
;
1452 int o_cont
= l_cont
;
1453 int l_check
= LABEL(); /* for condition label */
1454 int l_body
= LABEL(); /* for block label */
1458 if (tok_see() != ';')
1462 if (tok_see() != ';') {
1470 if (tok_see() != ')')
1482 if (!tok_jmp(TOK_SWITCH
)) {
1486 if (!tok_jmp(TOK_RETURN
)) {
1487 int ret
= tok_see() != ';';
1496 if (!tok_jmp(TOK_BREAK
)) {
1501 if (!tok_jmp(TOK_CONTINUE
)) {
1506 if (!tok_jmp(TOK_GOTO
)) {
1507 tok_expect(TOK_NAME
);
1508 o_jmp(label_id(tok_id()));
1514 if (!tok_jmp(':')) {
1515 o_label(label_id(tok_id()));
1521 static void readfunc(struct name
*name
, int flags
)
1523 struct funcinfo
*fi
= &funcs
[name
->type
.id
];
1524 long beg
= tok_addr();
1526 strcpy(func_name
, fi
->name
);
1527 o_func_beg(func_name
, fi
->nargs
, F_GLOBAL(flags
), fi
->varg
);
1528 for (i
= 0; i
< fi
->nargs
; i
++) {
1529 struct name arg
= {"", "", fi
->args
[i
], o_arg2loc(i
)};
1530 strcpy(arg
.name
, fi
->argnames
[i
]);
1533 /* first pass: collecting statistics */
1537 /* second pass: generating code */
1541 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
[MAXARGS
];
1839 char argnames
[MAXARGS
][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
);