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];
1081 o_assign(TYPE_BT(&ts
[nts
- 2]));
1086 static void doassign(void)
1088 struct type t
= ts
[nts
- 1];
1089 if (!t
.ptr
&& t
.flags
& T_STRUCT
) {
1091 o_num(type_totsz(&t
));
1095 o_assign(TYPE_BT(&ts
[nts
- 1]));
1100 static void readexpr(void)
1103 if (!tok_jmp('=')) {
1108 if (!tok_jmp(TOK2("+="))) {
1112 if (!tok_jmp(TOK2("-="))) {
1116 if (!tok_jmp(TOK2("*="))) {
1120 if (!tok_jmp(TOK2("/="))) {
1124 if (!tok_jmp(TOK2("%="))) {
1128 if (!tok_jmp(TOK3("<<="))) {
1132 if (!tok_jmp(TOK3(">>="))) {
1136 if (!tok_jmp(TOK3("&="))) {
1140 if (!tok_jmp(TOK3("|="))) {
1144 if (!tok_jmp(TOK3("^="))) {
1150 static void readestmt(void)
1156 } while (!tok_jmp(','));
1159 #define F_GLOBAL(flags) (!((flags) & F_STATIC))
1161 static void globalinit(void *obj
, int off
, struct type
*t
)
1163 struct name
*name
= obj
;
1164 char *elfname
= *name
->elfname
? name
->elfname
: name
->name
;
1165 if (t
->flags
& T_ARRAY
&& tok_see() == TOK_STR
) {
1166 struct type
*t_de
= &arrays
[t
->id
].type
;
1167 if (!t_de
->ptr
&& !t_de
->flags
&& TYPE_SZ(t_de
) == 1) {
1170 tok_expect(TOK_STR
);
1172 memcpy((void *) name
->addr
+ off
, buf
, len
);
1177 o_datset(elfname
, off
, TYPE_BT(t
));
1181 static void readfunc(struct name
*name
, int flags
);
1183 static void globaldef(void *data
, struct name
*name
, unsigned flags
)
1185 struct type
*t
= &name
->type
;
1186 char *elfname
= *name
->elfname
? name
->elfname
: name
->name
;
1188 if (t
->flags
& T_ARRAY
&& !t
->ptr
&& !arrays
[t
->id
].n
)
1189 if (~flags
& F_EXTERN
)
1190 arrays
[t
->id
].n
= initsize();
1192 if (!(flags
& F_EXTERN
) && (!(t
->flags
& T_FUNC
) || t
->ptr
)) {
1193 if (tok_see() == '=')
1194 name
->addr
= (long) o_mkdat(elfname
, sz
, F_GLOBAL(flags
));
1196 o_mkbss(elfname
, sz
, F_GLOBAL(flags
));
1200 initexpr(t
, 0, name
, globalinit
);
1201 if (tok_see() == '{' && name
->type
.flags
& T_FUNC
)
1202 readfunc(name
, flags
);
1205 /* generate the address of local + off */
1206 static void o_localoff(long addr
, int off
)
1215 static void localinit(void *obj
, int off
, struct type
*t
)
1217 long addr
= *(long *) obj
;
1218 if (t
->flags
& T_ARRAY
&& tok_see() == TOK_STR
) {
1219 struct type
*t_de
= &arrays
[t
->id
].type
;
1220 if (!t_de
->ptr
&& !t_de
->flags
&& TYPE_SZ(t_de
) == 1) {
1223 tok_expect(TOK_STR
);
1225 o_localoff(addr
, off
);
1226 o_sym(tmp_str(buf
, len
));
1233 o_localoff(addr
, off
);
1241 /* current function name */
1242 static char func_name
[NAMELEN
];
1244 static void localdef(void *data
, struct name
*name
, unsigned flags
)
1246 struct type
*t
= &name
->type
;
1247 if ((flags
& F_EXTERN
) || ((t
->flags
& T_FUNC
) && !t
->ptr
)) {
1251 if (flags
& F_STATIC
) {
1252 sprintf(name
->elfname
, "__neatcc.%s.%s", func_name
, name
->name
);
1253 globaldef(data
, name
, flags
);
1256 if (t
->flags
& T_ARRAY
&& !t
->ptr
&& !arrays
[t
->id
].n
)
1257 arrays
[t
->id
].n
= initsize();
1258 name
->addr
= o_mklocal(type_totsz(&name
->type
));
1260 if (!tok_jmp('=')) {
1261 if (t
->flags
& (T_ARRAY
| T_STRUCT
) && !t
->ptr
) {
1262 o_local(name
->addr
);
1264 o_num(type_totsz(t
));
1268 initexpr(t
, 0, &name
->addr
, localinit
);
1272 static void typedefdef(void *data
, struct name
*name
, unsigned flags
)
1274 typedef_add(name
->name
, &name
->type
);
1277 static void readstmt(void);
1279 #define MAXCASES (1 << 7)
1281 static void readswitch(void)
1283 int o_break
= l_break
;
1284 long val_addr
= o_mklocal(LONGSZ
);
1286 int ncases
= 0; /* number of case labels */
1287 int l_failed
= LABEL(); /* address of last failed jmp */
1288 int l_matched
= LABEL(); /* address of last walk through jmp */
1289 int l_default
= 0; /* default case label */
1296 o_assign(TYPE_BT(&t
));
1301 while (tok_jmp('}')) {
1302 if (tok_see() != TOK_CASE
&& tok_see() != TOK_DEFAULT
) {
1308 if (tok_get() == TOK_CASE
) {
1316 o_deref(TYPE_BT(&t
));
1323 l_default
= LABEL();
1328 l_matched
= LABEL();
1331 o_rmlocal(val_addr
, LONGSZ
);
1340 #define MAXLABELS (1 << 10)
1342 static char label_name
[MAXLABELS
][NAMELEN
];
1343 static int label_ids
[MAXLABELS
];
1346 static int label_id(char *name
)
1349 for (i
= nlabels
- 1; i
>= 0; --i
)
1350 if (!strcmp(label_name
[i
], name
))
1351 return label_ids
[i
];
1352 strcpy(label_name
[nlabels
], name
);
1353 label_ids
[nlabels
] = LABEL();
1354 return label_ids
[nlabels
++];
1357 static void readstmt(void)
1361 if (!tok_jmp('{')) {
1362 int _nlocals
= nlocals
;
1363 int _nglobals
= nglobals
;
1364 int _nenums
= nenums
;
1365 int _ntypedefs
= ntypedefs
;
1366 int _nstructs
= nstructs
;
1367 int _nfuncs
= nfuncs
;
1368 int _narrays
= narrays
;
1369 while (tok_jmp('}'))
1373 ntypedefs
= _ntypedefs
;
1374 nstructs
= _nstructs
;
1377 nglobals
= _nglobals
;
1380 if (!readdefs(localdef
, NULL
)) {
1384 if (!tok_jmp(TOK_TYPEDEF
)) {
1385 readdefs(typedefdef
, NULL
);
1389 if (!tok_jmp(TOK_IF
)) {
1390 int l_fail
= LABEL();
1391 int l_end
= LABEL();
1398 if (!tok_jmp(TOK_ELSE
)) {
1408 if (!tok_jmp(TOK_WHILE
)) {
1409 int o_break
= l_break
;
1410 int o_cont
= l_cont
;
1426 if (!tok_jmp(TOK_DO
)) {
1427 int o_break
= l_break
;
1428 int o_cont
= l_cont
;
1429 int l_beg
= LABEL();
1434 tok_expect(TOK_WHILE
);
1447 if (!tok_jmp(TOK_FOR
)) {
1448 int o_break
= l_break
;
1449 int o_cont
= l_cont
;
1450 int l_check
= LABEL(); /* for condition label */
1451 int l_body
= LABEL(); /* for block label */
1455 if (tok_see() != ';')
1459 if (tok_see() != ';') {
1467 if (tok_see() != ')')
1479 if (!tok_jmp(TOK_SWITCH
)) {
1483 if (!tok_jmp(TOK_RETURN
)) {
1484 int ret
= tok_see() != ';';
1493 if (!tok_jmp(TOK_BREAK
)) {
1498 if (!tok_jmp(TOK_CONTINUE
)) {
1503 if (!tok_jmp(TOK_GOTO
)) {
1504 tok_expect(TOK_NAME
);
1505 o_jmp(label_id(tok_id()));
1511 if (!tok_jmp(':')) {
1512 o_label(label_id(tok_id()));
1518 static void readfunc(struct name
*name
, int flags
)
1520 struct funcinfo
*fi
= &funcs
[name
->type
.id
];
1521 long beg
= tok_addr();
1523 strcpy(func_name
, fi
->name
);
1524 o_func_beg(func_name
, fi
->nargs
, F_GLOBAL(flags
), fi
->varg
);
1525 for (i
= 0; i
< fi
->nargs
; i
++) {
1526 struct name arg
= {"", "", fi
->args
[i
], o_arg2loc(i
)};
1527 strcpy(arg
.name
, fi
->argnames
[i
]);
1530 /* first pass: collecting statistics */
1534 /* second pass: generating code */
1538 func_name
[0] = '\0';
1544 static void readdecl(void)
1546 if (!tok_jmp(TOK_TYPEDEF
)) {
1547 readdefs(typedefdef
, NULL
);
1551 readdefs_int(globaldef
, NULL
);
1555 static void parse(void)
1557 while (tok_see() != TOK_EOF
)
1561 static void compat_macros(void)
1563 cpp_define("__STDC__", "");
1564 cpp_define("__linux__", "");
1565 cpp_define(I_ARCH
, "");
1567 /* ignored keywords */
1568 cpp_define("const", "");
1569 cpp_define("register", "");
1570 cpp_define("volatile", "");
1571 cpp_define("inline", "");
1572 cpp_define("restrict", "");
1573 cpp_define("__inline__", "");
1574 cpp_define("__restrict__", "");
1575 cpp_define("__attribute__(x)", "");
1576 cpp_define("__builtin_va_list__", "long");
1579 int main(int argc
, char *argv
[])
1585 while (i
< argc
&& argv
[i
][0] == '-') {
1586 if (argv
[i
][1] == 'I')
1587 cpp_addpath(argv
[i
][2] ? argv
[i
] + 2 : argv
[++i
]);
1588 if (argv
[i
][1] == 'D') {
1589 char *name
= argv
[i
] + 2;
1591 char *eq
= strchr(name
, '=');
1596 cpp_define(name
, def
);
1598 if (argv
[i
][1] == 'o')
1599 strcpy(obj
, argv
[i
][2] ? argv
[i
] + 2 : argv
[++i
]);
1603 die("neatcc: no file given\n");
1604 if (cpp_init(argv
[i
]))
1605 die("neatcc: cannot open <%s>\n", argv
[i
]);
1608 strcpy(obj
, argv
[i
]);
1609 obj
[strlen(obj
) - 1] = 'o';
1611 ofd
= open(obj
, O_WRONLY
| O_TRUNC
| O_CREAT
, 0600);
1618 /* parsing function and variable declarations */
1620 /* read the base type of a variable */
1621 static int basetype(struct type
*type
, unsigned *flags
)
1628 char name
[NAMELEN
] = "";
1634 switch (tok_see()) {
1666 isunion
= tok_get() == TOK_UNION
;
1667 if (!tok_jmp(TOK_NAME
))
1668 strcpy(name
, tok_id());
1669 if (tok_see() == '{')
1670 type
->id
= struct_create(name
, isunion
);
1672 type
->id
= struct_find(name
, isunion
);
1673 type
->flags
|= T_STRUCT
;
1679 if (tok_see() == '{')
1681 type
->bt
= 4 | BT_SIGNED
;
1684 if (tok_see() == TOK_NAME
) {
1685 int id
= typedef_find(tok_id());
1688 memcpy(type
, &typedefs
[id
].type
,
1701 type
->bt
= size
| (sign
? BT_SIGNED
: 0);
1705 static void readptrs(struct type
*type
)
1707 while (!tok_jmp('*')) {
1714 /* read function arguments */
1715 static int readargs(struct type
*args
, char argnames
[][NAMELEN
], int *varg
)
1720 while (tok_see() != ')') {
1721 if (!tok_jmp(TOK3("..."))) {
1725 if (readname(&args
[nargs
], argnames
[nargs
], NULL
)) {
1726 /* argument has no type, assume int */
1727 tok_expect(TOK_NAME
);
1728 memset(&args
[nargs
], 0, sizeof(struct type
));
1729 args
[nargs
].bt
= 4 | BT_SIGNED
;
1730 strcpy(argnames
[nargs
], tok_id());
1732 /* argument arrays are pointers */
1733 array2ptr(&args
[nargs
]);
1740 if (nargs
== 1 && !TYPE_BT(&args
[0]))
1745 /* read K&R function arguments */
1746 static void krdef(void *data
, struct name
*name
, unsigned flags
)
1748 struct funcinfo
*fi
= data
;
1750 for (i
= 0; i
< fi
->nargs
; i
++)
1751 if (!strcmp(fi
->argnames
[i
], name
->name
))
1752 memcpy(&fi
->args
[i
], &name
->type
, sizeof(name
->type
));
1756 * readarrays() parses array specifiers when reading a definition in
1757 * readname(). The "type" parameter contains the type contained in the
1758 * inner array; for instance, type in "int *a[10][20]" would be an int
1759 * pointer. When returning, the "type" parameter is changed to point
1760 * to the final array. The function returns a pointer to the type in
1761 * the inner array; this is useful when the type is not complete yet,
1762 * like when creating an array of function pointers as in
1763 * "int (*f[10])(int)". If there is no array brackets, NULL is returned.
1765 static struct type
*readarrays(struct type
*type
)
1768 struct type
*inner
= NULL
;
1771 while (!tok_jmp('[')) {
1777 err("const expr expected\n");
1782 for (i
= nar
- 1; i
>= 0; i
--) {
1783 type
->id
= array_add(type
, arsz
[i
]);
1785 inner
= &arrays
[type
->id
].type
;
1786 type
->flags
= T_ARRAY
;
1794 * readname() reads a variable definition; the name is copied into
1795 * "name" and the type is copied into "main" argument. The "base"
1796 * argument, if not NULL, indicates the base type of the variable.
1797 * For instance, the base type of "a" and "b" in "int *a, b[10]" is
1798 * "int". If NULL, basetype() is called directly to read the base
1799 * type of the variable. readname() returns zero, only if the
1800 * variable can be read.
1802 static int readname(struct type
*main
, char *name
, struct type
*base
)
1804 struct type tpool
[3];
1806 struct type
*type
= &tpool
[npool
++];
1807 struct type
*ptype
= NULL
; /* type inside parenthesis */
1808 struct type
*btype
= NULL
; /* type before parenthesis */
1811 memset(tpool
, 0, sizeof(tpool
));
1815 if (basetype(type
, &flags
))
1818 memcpy(type
, base
, sizeof(*base
));
1821 if (!tok_jmp('(')) {
1823 type
= &tpool
[npool
++];
1827 if (!tok_jmp(TOK_NAME
) && name
)
1828 strcpy(name
, tok_id());
1829 inner
= readarrays(type
);
1834 if (tok_see() == '(') {
1835 struct type args
[MAXARGS
];
1836 char argnames
[MAXARGS
][NAMELEN
];
1838 int nargs
= readargs(args
, argnames
, &varg
);
1841 type
= &tpool
[npool
++];
1844 ptype
->flags
= T_FUNC
;
1846 ptype
->id
= func_create(btype
, name
, argnames
, args
, nargs
, varg
);
1847 if (tok_see() != ';')
1848 while (tok_see() != '{' && !readdefs(krdef
, &funcs
[ptype
->id
]))
1851 if (ptype
&& readarrays(type
))
1854 memcpy(main
, type
, sizeof(*type
));
1858 static int readtype(struct type
*type
)
1860 return readname(type
, NULL
, NULL
);
1864 * readdef() reads a variable definitions statement. The definition
1865 * statement can appear in anywhere: global variables, function
1866 * local variables, struct fields, and typedefs. For each defined
1867 * variable, def() callback is called with the appropriate name
1868 * struct and flags; the callback should finish parsing the definition
1869 * by possibly reading the initializer expression and saving the name
1872 static int readdefs(void (*def
)(void *data
, struct name
*name
, unsigned flags
),
1876 unsigned base_flags
;
1877 if (basetype(&base
, &base_flags
))
1879 if (tok_see() == ';' || tok_see() == '{')
1882 struct name name
= {{""}};
1883 if (readname(&name
.type
, name
.name
, &base
))
1885 def(data
, &name
, base_flags
);
1886 } while (!tok_jmp(','));
1890 /* just like readdefs, but default to int type; for handling K&R functions */
1891 static int readdefs_int(void (*def
)(void *data
, struct name
*name
, unsigned flags
),
1896 if (basetype(&base
, &flags
)) {
1897 if (tok_see() != TOK_NAME
)
1899 memset(&base
, 0, sizeof(base
));
1900 base
.bt
= 4 | BT_SIGNED
;
1902 if (tok_see() != ';') {
1904 struct name name
= {{""}};
1905 if (readname(&name
.type
, name
.name
, &base
))
1907 def(data
, &name
, flags
);
1908 } while (!tok_jmp(','));
1914 /* parsing initializer expressions */
1916 static void jumpbrace(void)
1919 while (tok_see() != '}' || depth
--)
1920 if (tok_get() == '{')
1925 /* compute the size of the initializer expression */
1926 static int initsize(void)
1928 long addr
= tok_addr();
1932 if (!tok_jmp(TOK_STR
)) {
1938 while (tok_jmp('}')) {
1940 if (!tok_jmp('[')) {
1949 while (tok_see() != '}' && tok_see() != ',')
1950 if (tok_get() == '{')
1958 static struct type
*innertype(struct type
*t
)
1960 if (t
->flags
& T_ARRAY
&& !t
->ptr
)
1961 return innertype(&arrays
[t
->id
].type
);
1965 /* read the initializer expression and initialize basic types using set() cb */
1966 static void initexpr(struct type
*t
, int off
, void *obj
,
1967 void (*set
)(void *obj
, int off
, struct type
*t
))
1973 if (!t
->ptr
&& t
->flags
& T_STRUCT
) {
1974 struct structinfo
*si
= &structs
[t
->id
];
1976 for (i
= 0; i
< si
->nfields
&& tok_see() != '}'; i
++) {
1977 struct name
*field
= &si
->fields
[i
];
1978 if (!tok_jmp('.')) {
1979 tok_expect(TOK_NAME
);
1980 field
= struct_field(t
->id
, tok_id());
1983 initexpr(&field
->type
, off
+ field
->addr
, obj
, set
);
1987 } else if (t
->flags
& T_ARRAY
) {
1988 struct type
*t_de
= &arrays
[t
->id
].type
;
1990 /* handling extra braces as in: char s[] = {"sth"} */
1991 if (TYPE_SZ(t_de
) == 1 && tok_see() == TOK_STR
) {
1996 for (i
= 0; tok_see() != '}'; i
++) {
1998 struct type
*it
= t_de
;
1999 if (!tok_jmp('[')) {
2006 if (tok_see() != '{' && (tok_see() != TOK_STR
||
2007 !(it
->flags
& T_ARRAY
)))
2008 it
= innertype(t_de
);
2009 initexpr(it
, off
+ type_totsz(it
) * idx
, obj
, set
);