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 TYPE_BT(t) ((t)->ptr ? LONGSZ : (t)->bt)
53 #define TYPE_SZ(t) ((t)->ptr ? LONGSZ : (t)->bt & BT_SZMASK)
67 int id
; /* for structs, functions and arrays */
68 int addr
; /* the address is passed to gen.c; deref for value */
72 static struct type ts
[MAXTMP
];
75 static void ts_push_bt(unsigned bt
)
83 static void ts_push(struct type
*t
)
85 struct type
*d
= &ts
[nts
++];
86 memcpy(d
, t
, sizeof(*t
));
89 static void ts_push_addr(struct type
*t
)
95 static void ts_pop(struct type
*type
)
104 die("%s: %s", cpp_loc(tok_addr()), msg
);
109 char elfname
[NAMELEN
]; /* local elf name for static variables in function */
111 long addr
; /* local stack offset, global data addr, struct offset */
114 static struct name locals
[MAXLOCALS
];
116 static struct name globals
[MAXGLOBALS
];
119 static void local_add(struct name
*name
)
121 if (nlocals
>= MAXLOCALS
)
122 err("nomem: MAXLOCALS reached!\n");
123 memcpy(&locals
[nlocals
++], name
, sizeof(*name
));
126 static int local_find(char *name
)
129 for (i
= nlocals
- 1; i
>= 0; --i
)
130 if (!strcmp(locals
[i
].name
, name
))
135 static int global_find(char *name
)
138 for (i
= nglobals
- 1; i
>= 0; i
--)
139 if (!strcmp(name
, globals
[i
].name
))
144 static void global_add(struct name
*name
)
146 if (nglobals
>= MAXGLOBALS
)
147 err("nomem: MAXGLOBALS reached!\n");
148 memcpy(&globals
[nglobals
++], name
, sizeof(*name
));
151 #define MAXENUMS (1 << 10)
153 static struct enumval
{
159 static void enum_add(char *name
, int val
)
161 struct enumval
*ev
= &enums
[nenums
++];
162 if (nenums
>= MAXENUMS
)
163 err("nomem: MAXENUMS reached!\n");
164 strcpy(ev
->name
, name
);
168 static int enum_find(int *val
, char *name
)
171 for (i
= nenums
- 1; i
>= 0; --i
)
172 if (!strcmp(name
, enums
[i
].name
)) {
179 #define MAXTYPEDEFS (1 << 10)
181 static struct typdefinfo
{
184 } typedefs
[MAXTYPEDEFS
];
185 static int ntypedefs
;
187 static void typedef_add(char *name
, struct type
*type
)
189 struct typdefinfo
*ti
= &typedefs
[ntypedefs
++];
190 if (ntypedefs
>= MAXTYPEDEFS
)
191 err("nomem: MAXTYPEDEFS reached!\n");
192 strcpy(ti
->name
, name
);
193 memcpy(&ti
->type
, type
, sizeof(*type
));
196 static int typedef_find(char *name
)
199 for (i
= ntypedefs
- 1; i
>= 0; --i
)
200 if (!strcmp(name
, typedefs
[i
].name
))
205 #define MAXARRAYS (1 << 10)
207 static struct array
{
213 static int array_add(struct type
*type
, int n
)
215 struct array
*a
= &arrays
[narrays
++];
216 if (narrays
>= MAXARRAYS
)
217 err("nomem: MAXARRAYS reached!\n");
218 memcpy(&a
->type
, type
, sizeof(*type
));
223 static void array2ptr(struct type
*t
)
225 if (t
->flags
& T_ARRAY
&& !t
->ptr
) {
226 memcpy(t
, &arrays
[t
->id
].type
, sizeof(*t
));
231 #define MAXSTRUCTS (1 << 10)
232 #define MAXFIELDS (1 << 7)
234 static struct structinfo
{
236 struct name fields
[MAXFIELDS
];
240 } structs
[MAXSTRUCTS
];
243 static int struct_find(char *name
, int isunion
)
246 for (i
= nstructs
- 1; i
>= 0; --i
)
247 if (*structs
[i
].name
&& !strcmp(name
, structs
[i
].name
) &&
248 structs
[i
].isunion
== isunion
)
251 if (nstructs
>= MAXSTRUCTS
)
252 err("nomem: MAXTYPES reached!\n");
253 memset(&structs
[i
], 0, sizeof(structs
[i
]));
254 strcpy(structs
[i
].name
, name
);
255 structs
[i
].isunion
= isunion
;
259 static struct name
*struct_field(int id
, char *name
)
261 struct structinfo
*si
= &structs
[id
];
263 for (i
= 0; i
< si
->nfields
; i
++)
264 if (!strcmp(name
, si
->fields
[i
].name
))
265 return &si
->fields
[i
];
266 err("field not found\n");
269 #define MAXBREAK (1 << 7)
271 static long breaks
[MAXBREAK
];
273 static long continues
[MAXBREAK
];
274 static int ncontinues
;
276 static void break_fill(long addr
, int till
)
279 for (i
= till
; i
< nbreaks
; i
++)
280 o_filljmp2(breaks
[i
], addr
);
284 static void continue_fill(long addr
, int till
)
287 for (i
= till
; i
< ncontinues
; i
++)
288 o_filljmp2(continues
[i
], addr
);
292 /* return t's size */
293 static int type_totsz(struct type
*t
)
297 if (t
->flags
& T_ARRAY
)
298 return arrays
[t
->id
].n
* type_totsz(&arrays
[t
->id
].type
);
299 return t
->flags
& T_STRUCT
? structs
[t
->id
].size
: BT_SZ(t
->bt
);
302 /* return t's dereferenced size */
303 static unsigned type_szde(struct type
*t
)
308 return type_totsz(&de
);
311 /* dereference stack top if t->addr (ie. address is pushed to gen.c) */
312 static void ts_de(int deref
)
314 struct type
*t
= &ts
[nts
- 1];
316 if (deref
&& t
->addr
&& (t
->ptr
|| !(t
->flags
& T_FUNC
)))
321 /* pop stack pop to *t and dereference if t->addr */
322 static void ts_pop_de(struct type
*t
)
328 /* pop the top 2 stack values and dereference them if t->addr */
329 static void ts_pop_de2(struct type
*t1
, struct type
*t2
)
337 static int tok_jmp(int tok
)
339 if (tok_see() != tok
)
345 static void tok_expect(int tok
)
347 if (tok_get() != tok
)
348 err("syntax error\n");
351 static unsigned bt_op(unsigned bt1
, unsigned bt2
)
353 unsigned s1
= BT_SZ(bt1
);
354 unsigned s2
= BT_SZ(bt2
);
355 return (bt1
| bt2
) & BT_SIGNED
| (s1
> s2
? s1
: s2
);
358 static void ts_binop(int op
)
362 ts_pop_de2(&t1
, &t2
);
363 if (op
== O_DIV
|| op
== O_MOD
)
366 bt
= bt_op(TYPE_BT(&t1
), TYPE_BT(&t2
));
367 o_bop(op
| (bt
& BT_SIGNED
? O_SIGNED
: 0));
371 static void ts_addop(int op
)
374 ts_pop_de2(&t1
, &t2
);
375 if (!t1
.ptr
&& !t2
.ptr
) {
377 ts_push_bt(bt_op(TYPE_BT(&t1
), TYPE_BT(&t2
)));
380 if (t1
.ptr
&& !t2
.ptr
)
382 if (!t1
.ptr
&& t2
.ptr
)
383 if (type_szde(&t2
) > 1) {
384 o_num(type_szde(&t2
));
387 if (t1
.ptr
&& !t2
.ptr
)
390 if (t1
.ptr
&& t2
.ptr
) {
391 int sz
= type_szde(&t1
);
396 ts_push_bt(4 | BT_SIGNED
);
398 ts_push(t1
.ptr
? &t1
: &t2
);
402 #define ALIGN(x, a) (((x) + (a) - 1) & ~((a) - 1))
403 #define MIN(a, b) ((a) < (b) ? (a) : (b))
405 static int type_alignment(struct type
*t
)
407 if (t
->flags
& T_ARRAY
&& !t
->ptr
)
408 return type_alignment(&arrays
[t
->id
].type
);
409 if (t
->flags
& T_STRUCT
&& !t
->ptr
)
410 return type_alignment(&structs
[t
->id
].fields
[0].type
);
411 return MIN(LONGSZ
, type_totsz(t
));
414 static void structdef(void *data
, struct name
*name
, unsigned flags
)
416 struct structinfo
*si
= data
;
419 if (si
->size
< type_totsz(&name
->type
))
420 si
->size
= type_totsz(&name
->type
);
422 struct type
*t
= &name
->type
;
423 int alignment
= type_alignment(t
);
424 if (t
->flags
& T_ARRAY
&& !t
->ptr
)
425 alignment
= MIN(LONGSZ
, type_totsz(&arrays
[t
->id
].type
));
426 si
->size
= ALIGN(si
->size
, alignment
);
427 name
->addr
= si
->size
;
428 si
->size
+= type_totsz(&name
->type
);
430 memcpy(&si
->fields
[si
->nfields
++], name
, sizeof(*name
));
433 static int readdefs(void (*def
)(void *, struct name
*, unsigned f
), void *data
);
435 static int struct_create(char *name
, int isunion
)
437 int id
= struct_find(name
, isunion
);
438 struct structinfo
*si
= &structs
[id
];
440 while (tok_jmp('}')) {
441 readdefs(structdef
, si
);
447 static void readexpr(void);
449 static void enum_create(void)
453 while (tok_jmp('}')) {
455 tok_expect(TOK_NAME
);
456 strcpy(name
, tok_id());
461 err("const expr expected!\n");
468 static int basetype(struct type
*type
, unsigned *flags
)
475 char name
[NAMELEN
] = "";
513 isunion
= tok_get() == TOK_UNION
;
514 if (!tok_jmp(TOK_NAME
))
515 strcpy(name
, tok_id());
516 if (tok_see() == '{')
517 type
->id
= struct_create(name
, isunion
);
519 type
->id
= struct_find(name
, isunion
);
520 type
->flags
|= T_STRUCT
;
526 if (tok_see() == '{')
528 type
->bt
= 4 | BT_SIGNED
;
531 if (tok_see() == TOK_NAME
) {
532 int id
= typedef_find(tok_id());
535 memcpy(type
, &typedefs
[id
].type
,
548 type
->bt
= size
| (sign
? BT_SIGNED
: 0);
552 static int readname(struct type
*main
, char *name
,
553 struct type
*base
, unsigned flags
);
555 static int readtype(struct type
*type
)
557 return readname(type
, NULL
, NULL
, 0);
560 static void readptrs(struct type
*type
)
562 while (!tok_jmp('*')) {
569 /* used to differenciate labels from case and cond exprs */
573 static void readpre(void);
575 static char *tmp_str(char *buf
, int len
)
577 static char name
[NAMELEN
];
580 sprintf(name
, "__neatcc.s%d", id
++);
581 dat
= o_mkdat(name
, len
, 0);
582 memcpy(dat
, buf
, len
);
586 static void readprimary(void)
588 if (!tok_jmp(TOK_NUM
)) {
590 int bt
= tok_num(&n
);
595 if (!tok_jmp(TOK_STR
)) {
599 t
.bt
= 1 | BT_SIGNED
;
605 o_sym(tmp_str(buf
, len
));
608 if (!tok_jmp(TOK_NAME
)) {
609 struct name unkn
= {""};
610 char *name
= unkn
.name
;
612 strcpy(name
, tok_id());
613 /* don't search for labels here */
614 if (!ncexpr
&& !caseexpr
&& tok_see() == ':')
616 if ((n
= local_find(name
)) != -1) {
617 struct name
*l
= &locals
[n
];
619 ts_push_addr(&l
->type
);
622 if ((n
= global_find(name
)) != -1) {
623 struct name
*g
= &globals
[n
];
624 o_sym(*g
->elfname
? g
->elfname
: g
->name
);
625 ts_push_addr(&g
->type
);
628 if (!enum_find(&n
, name
)) {
629 ts_push_bt(4 | BT_SIGNED
);
633 if (tok_see() != '(')
634 err("unknown symbol\n");
648 if (!t
.ptr
|| !o
.ptr
)
658 static void arrayderef(void)
664 if (!(t
.flags
& T_ARRAY
) && t
.addr
) {
666 o_deref(TYPE_BT(&t
));
681 static void inc_post(int op
)
683 struct type t
= ts
[nts
- 1];
684 /* pushing the value before inc */
690 /* increment by 1 or pointer size */
694 o_num(t
.ptr
> 0 ? type_szde(&t
) : 1);
698 o_assign(TYPE_BT(&t
));
702 static void readfield(void)
706 tok_expect(TOK_NAME
);
709 field
= struct_field(t
.id
, tok_id());
714 ts_push_addr(&field
->type
);
717 #define MAXFUNCS (1 << 10)
719 static struct funcinfo
{
720 struct type args
[MAXFIELDS
];
727 static int func_create(struct type
*ret
, struct name
*args
, int nargs
)
729 struct funcinfo
*fi
= &funcs
[nfuncs
++];
731 if (nfuncs
>= MAXFUNCS
)
732 err("nomem: MAXFUNCS reached!\n");
733 memcpy(&fi
->ret
, ret
, sizeof(*ret
));
734 for (i
= 0; i
< nargs
; i
++)
735 memcpy(&fi
->args
[i
], &args
[i
].type
, sizeof(*ret
));
740 static void readcall(void)
746 if (t
.flags
& T_FUNC
&& t
.ptr
> 0)
748 fi
= t
.flags
& T_FUNC
? &funcs
[t
.id
] : NULL
;
749 if (tok_see() != ')') {
754 } while (!tok_jmp(','));
757 o_call(argc
, fi
? TYPE_BT(&fi
->ret
) : 4 | BT_SIGNED
);
759 if (TYPE_BT(&fi
->ret
))
760 o_cast(TYPE_BT(&fi
->ret
));
763 ts_push_bt(4 | BT_SIGNED
);
767 static void readpost(void)
781 if (!tok_jmp(TOK2("++"))) {
785 if (!tok_jmp(TOK2("--"))) {
793 if (!tok_jmp(TOK2("->"))) {
802 static void inc_pre(int op
)
806 /* copy the destination */
808 ts_push(&ts
[nts
- 1]);
809 /* increment by 1 or pointer size */
811 o_num(t
.ptr
> 0 ? type_szde(&t
) : 1);
813 /* assign the result */
814 o_assign(TYPE_BT(&t
));
818 static void readpre(void)
825 err("cannot use the address\n");
837 err("dereferencing non-pointer\n");
839 o_deref(TYPE_BT(&t
));
849 ts_push_bt(4 | BT_SIGNED
);
864 if (!tok_jmp(TOK2("++"))) {
868 if (!tok_jmp(TOK2("--"))) {
872 if (!tok_jmp(TOK_SIZEOF
)) {
874 int op
= !tok_jmp('(');
885 o_num(type_totsz(&t
));
893 static void readmul(void)
916 static void readadd(void)
934 static void shift(int op
)
938 ts_pop_de2(NULL
, &t
);
939 o_bop(op
| (BT_SIGNED
& TYPE_BT(&t
) ? O_SIGNED
: 0));
940 ts_push_bt(TYPE_BT(&t
));
943 static void readshift(void)
947 if (!tok_jmp(TOK2("<<"))) {
951 if (!tok_jmp(TOK2(">>"))) {
959 static void cmp(int op
)
964 ts_pop_de2(&t1
, &t2
);
965 bt
= bt_op(TYPE_BT(&t1
), TYPE_BT(&t2
));
966 o_bop(op
| (bt
& BT_SIGNED
? O_SIGNED
: 0));
967 ts_push_bt(4 | BT_SIGNED
);
970 static void readcmp(void)
982 if (!tok_jmp(TOK2("<="))) {
986 if (!tok_jmp(TOK2(">="))) {
994 static void eq(int op
)
997 ts_pop_de2(NULL
, NULL
);
999 ts_push_bt(4 | BT_SIGNED
);
1002 static void readeq(void)
1006 if (!tok_jmp(TOK2("=="))) {
1010 if (!tok_jmp(TOK2("!="))) {
1018 static void readbitand(void)
1021 while (!tok_jmp('&')) {
1027 static void readxor(void)
1030 while (!tok_jmp('^')) {
1036 static void readbitor(void)
1039 while (!tok_jmp('|')) {
1045 #define MAXCOND (1 << 7)
1047 static void readand(void)
1049 long conds
[MAXCOND
];
1054 if (tok_see() != TOK2("&&"))
1058 conds
[nconds
++] = o_jz(0);
1059 while (!tok_jmp(TOK2("&&"))) {
1062 conds
[nconds
++] = o_jz(0);
1067 for (i
= 0; i
< nconds
; i
++)
1068 o_filljmp(conds
[i
]);
1073 ts_push_bt(4 | BT_SIGNED
);
1076 static void reador(void)
1078 long conds
[MAXCOND
];
1083 if (tok_see() != TOK2("||"))
1087 conds
[nconds
++] = o_jnz(0);
1088 while (!tok_jmp(TOK2("||"))) {
1091 conds
[nconds
++] = o_jnz(0);
1096 for (i
= 0; i
< nconds
; i
++)
1097 o_filljmp(conds
[i
]);
1102 ts_push_bt(4 | BT_SIGNED
);
1105 static void readcexpr(void);
1107 static int readcexpr_const(void)
1115 /* both branches yield the same type; so ignore the first */
1123 /* making sure t->addr == 0 on both branches */
1130 static void readcexpr(void)
1139 if (readcexpr_const()) {
1142 /* both branches yield the same type; so ignore the first */
1150 /* making sure t->addr == 0 on both branches */
1159 static void opassign(int op
, int ptrop
)
1161 struct type t
= ts
[nts
- 1];
1166 o_assign(TYPE_BT(&ts
[nts
- 2]));
1171 static void doassign(void)
1173 struct type t
= ts
[nts
- 1];
1174 if (!t
.ptr
&& t
.flags
& T_STRUCT
) {
1176 o_num(type_totsz(&t
));
1180 o_assign(TYPE_BT(&ts
[nts
- 1]));
1185 static void readexpr(void)
1188 if (!tok_jmp('=')) {
1193 if (!tok_jmp(TOK2("+="))) {
1197 if (!tok_jmp(TOK2("-="))) {
1201 if (!tok_jmp(TOK2("*="))) {
1205 if (!tok_jmp(TOK2("/="))) {
1209 if (!tok_jmp(TOK2("%="))) {
1213 if (!tok_jmp(TOK3("<<="))) {
1217 if (!tok_jmp(TOK3(">>="))) {
1221 if (!tok_jmp(TOK3("&="))) {
1225 if (!tok_jmp(TOK3("|="))) {
1229 if (!tok_jmp(TOK3("^="))) {
1235 static void readestmt(void)
1241 } while (!tok_jmp(','));
1244 static void o_localoff(long addr
, int off
)
1253 static struct type
*innertype(struct type
*t
)
1255 if (t
->flags
& T_ARRAY
&& !t
->ptr
)
1256 return innertype(&arrays
[t
->id
].type
);
1260 static void initexpr(struct type
*t
, int off
, void *obj
,
1261 void (*set
)(void *obj
, int off
, struct type
*t
))
1267 if (!t
->ptr
&& t
->flags
& T_STRUCT
) {
1268 struct structinfo
*si
= &structs
[t
->id
];
1270 for (i
= 0; i
< si
->nfields
&& tok_see() != '}'; i
++) {
1271 struct name
*field
= &si
->fields
[i
];
1272 if (!tok_jmp('.')) {
1273 tok_expect(TOK_NAME
);
1274 field
= struct_field(t
->id
, tok_id());
1277 initexpr(&field
->type
, off
+ field
->addr
, obj
, set
);
1281 } else if (t
->flags
& T_ARRAY
) {
1282 struct type
*t_de
= &arrays
[t
->id
].type
;
1284 /* handling extra braces as in: char s[] = {"sth"} */
1285 if (TYPE_SZ(t_de
) == 1 && tok_see() == TOK_STR
) {
1290 for (i
= 0; tok_see() != '}'; i
++) {
1292 struct type
*it
= t_de
;
1293 if (!tok_jmp('[')) {
1300 if (tok_see() != '{' && (tok_see() != TOK_STR
||
1301 !(it
->flags
& T_ARRAY
)))
1302 it
= innertype(t_de
);
1303 initexpr(it
, off
+ type_totsz(it
) * idx
, obj
, set
);
1311 static void jumpbrace(void)
1314 while (tok_see() != '}' || depth
--)
1315 if (tok_get() == '{')
1320 static int initsize(void)
1322 long addr
= tok_addr();
1324 if (!tok_jmp(TOK_STR
)) {
1330 while (tok_jmp('}')) {
1332 if (!tok_jmp('[')) {
1341 while (tok_see() != '}' && tok_see() != ',')
1342 if (tok_get() == '{')
1350 #define F_GLOBAL(flags) (!((flags) & F_STATIC))
1352 static void globalinit(void *obj
, int off
, struct type
*t
)
1354 struct name
*name
= obj
;
1355 char *elfname
= *name
->elfname
? name
->elfname
: name
->name
;
1356 if (t
->flags
& T_ARRAY
&& tok_see() == TOK_STR
) {
1357 struct type
*t_de
= &arrays
[t
->id
].type
;
1358 if (!t_de
->ptr
&& !t_de
->flags
&& TYPE_SZ(t_de
) == 1) {
1361 tok_expect(TOK_STR
);
1363 memcpy((void *) name
->addr
+ off
, buf
, len
);
1368 o_datset(elfname
, off
, TYPE_BT(t
));
1372 static void globaldef(void *data
, struct name
*name
, unsigned flags
)
1374 struct type
*t
= &name
->type
;
1375 char *elfname
= *name
->elfname
? name
->elfname
: name
->name
;
1377 if (t
->flags
& T_ARRAY
&& !t
->ptr
&& !arrays
[t
->id
].n
)
1378 if (~flags
& F_EXTERN
)
1379 arrays
[t
->id
].n
= initsize();
1381 if (!(flags
& F_EXTERN
) && (!(t
->flags
& T_FUNC
) || t
->ptr
)) {
1383 name
->addr
= (long) o_mkdat(elfname
, sz
, F_GLOBAL(flags
));
1385 o_mkbss(elfname
, sz
, F_GLOBAL(flags
));
1389 initexpr(t
, 0, name
, globalinit
);
1392 static void localinit(void *obj
, int off
, struct type
*t
)
1394 long addr
= *(long *) obj
;
1395 if (t
->flags
& T_ARRAY
&& tok_see() == TOK_STR
) {
1396 struct type
*t_de
= &arrays
[t
->id
].type
;
1397 if (!t_de
->ptr
&& !t_de
->flags
&& TYPE_SZ(t_de
) == 1) {
1400 tok_expect(TOK_STR
);
1402 o_localoff(addr
, off
);
1403 o_sym(tmp_str(buf
, len
));
1410 o_localoff(addr
, off
);
1418 /* current function name */
1419 static char func_name
[NAMELEN
];
1421 static void localdef(void *data
, struct name
*name
, unsigned flags
)
1423 struct type
*t
= &name
->type
;
1424 if ((flags
& F_EXTERN
) || (t
->flags
& T_FUNC
) && !t
->ptr
) {
1428 if (flags
& F_STATIC
) {
1429 sprintf(name
->elfname
, "__neatcc.%s.%s", func_name
, name
->name
);
1430 globaldef(data
, name
, flags
);
1433 if (t
->flags
& T_ARRAY
&& !t
->ptr
&& !arrays
[t
->id
].n
)
1434 arrays
[t
->id
].n
= initsize();
1435 name
->addr
= o_mklocal(type_totsz(&name
->type
));
1437 if (flags
& F_INIT
) {
1438 if (t
->flags
& (T_ARRAY
| T_STRUCT
) && !t
->ptr
) {
1439 o_local(name
->addr
);
1441 o_num(type_totsz(t
));
1445 initexpr(t
, 0, &name
->addr
, localinit
);
1449 static void funcdef(char *name
, struct type
*type
, struct name
*args
,
1450 int nargs
, int varg
, unsigned flags
)
1452 struct name global
= {""};
1454 strcpy(global
.name
, name
);
1455 strcpy(func_name
, name
);
1456 memcpy(&global
.type
, type
, sizeof(*type
));
1457 o_func_beg(name
, nargs
, F_GLOBAL(flags
), varg
);
1458 global_add(&global
);
1459 for (i
= 0; i
< nargs
; i
++) {
1460 args
[i
].addr
= o_arg2loc(i
);
1461 local_add(&args
[i
]);
1465 static int readargs(struct name
*args
, int *varg
)
1470 while (tok_see() != ')') {
1471 if (!tok_jmp(TOK3("..."))) {
1475 readname(&args
[nargs
].type
, args
[nargs
].name
, NULL
, 0);
1476 array2ptr(&args
[nargs
].type
);
1482 if (nargs
== 1 && !TYPE_BT(&args
[0].type
))
1487 static int readname(struct type
*main
, char *name
,
1488 struct type
*base
, unsigned flags
)
1490 struct type tpool
[3];
1492 struct type
*type
= &tpool
[npool
++];
1493 struct type
*func
= NULL
;
1494 struct type
*ret
= NULL
;
1498 memset(tpool
, 0, sizeof(tpool
));
1502 if (basetype(type
, &flags
))
1505 memcpy(type
, base
, sizeof(*base
));
1508 if (!tok_jmp('(')) {
1510 type
= &tpool
[npool
++];
1514 if (!tok_jmp(TOK_NAME
) && name
)
1515 strcpy(name
, tok_id());
1516 while (!tok_jmp('[')) {
1522 err("const expr expected\n");
1527 for (i
= nar
- 1; i
>= 0; i
--) {
1528 type
->id
= array_add(type
, arsz
[i
]);
1529 if (func
&& i
== nar
- 1)
1530 func
= &arrays
[type
->id
].type
;
1531 type
->flags
= T_ARRAY
;
1537 if (tok_see() == '(') {
1538 struct name args
[MAXARGS
] = {{""}};
1540 int nargs
= readargs(args
, &varg
);
1544 type
= &tpool
[npool
++];
1547 func
->flags
= T_FUNC
;
1549 func
->id
= func_create(ret
, args
, nargs
);
1550 if (fdef
&& tok_see() == '{') {
1551 funcdef(name
, func
, args
, nargs
, varg
, flags
);
1555 memcpy(main
, type
, sizeof(*type
));
1559 static int readdefs(void (*def
)(void *data
, struct name
*name
, unsigned flags
),
1563 unsigned base_flags
;
1564 if (basetype(&base
, &base_flags
))
1566 while (tok_see() != ';' && tok_see() != '{') {
1567 struct name name
= {{""}};
1568 unsigned flags
= base_flags
;
1569 if (readname(&name
.type
, name
.name
, &base
, flags
))
1573 def(data
, &name
, flags
);
1579 static void typedefdef(void *data
, struct name
*name
, unsigned flags
)
1581 typedef_add(name
->name
, &name
->type
);
1584 static void readstmt(void);
1586 #define MAXCASES (1 << 7)
1588 static void readswitch(void)
1590 int break_beg
= nbreaks
;
1591 long val_addr
= o_mklocal(LONGSZ
);
1593 int ncases
= 0; /* number of case labels */
1594 long last_failed
= -1; /* address of last failed jmp */
1595 long last_matched
= -1; /* address of last walk through jmp */
1596 long default_addr
= -1; /* address of the default label */
1602 o_assign(TYPE_BT(&t
));
1607 while (tok_jmp('}')) {
1608 if (tok_see() != TOK_CASE
&& tok_see() != TOK_DEFAULT
) {
1613 last_matched
= o_jmp(0);
1614 if (tok_get() == TOK_CASE
) {
1615 if (last_failed
>= 0)
1616 o_filljmp(last_failed
);
1622 o_deref(TYPE_BT(&t
));
1624 last_failed
= o_jz(0);
1628 last_failed
= o_jmp(0);
1629 default_addr
= o_mklabel();
1632 if (last_matched
>= 0)
1633 o_filljmp(last_matched
);
1636 o_rmlocal(val_addr
, LONGSZ
);
1637 if (last_failed
>= 0)
1638 o_filljmp2(last_failed
,
1639 default_addr
>= 0 ? default_addr
: o_mklabel());
1640 break_fill(o_mklabel(), break_beg
);
1643 #define MAXGOTO (1 << 10)
1645 static struct gotoinfo
{
1651 static struct labelinfo
{
1657 static void goto_add(char *name
)
1659 strcpy(gotos
[ngotos
].name
, name
);
1660 gotos
[ngotos
++].addr
= o_jmp(0);
1663 static void label_add(char *name
)
1665 strcpy(labels
[nlabels
].name
, name
);
1666 labels
[nlabels
++].addr
= o_mklabel();
1669 static void goto_fill(void)
1672 for (i
= 0; i
< ngotos
; i
++)
1673 for (j
= 0; j
< nlabels
; j
++)
1674 if (!strcmp(gotos
[i
].name
, labels
[j
].name
)) {
1675 o_filljmp2(gotos
[i
].addr
, labels
[j
].addr
);
1680 static void readstmt(void)
1684 if (!tok_jmp('{')) {
1685 int _nlocals
= nlocals
;
1686 int _nglobals
= nglobals
;
1687 int _nenums
= nenums
;
1688 int _ntypedefs
= ntypedefs
;
1689 int _nstructs
= nstructs
;
1690 int _nfuncs
= nfuncs
;
1691 int _narrays
= narrays
;
1692 while (tok_jmp('}'))
1696 ntypedefs
= _ntypedefs
;
1697 nstructs
= _nstructs
;
1700 nglobals
= _nglobals
;
1703 if (!readdefs(localdef
, NULL
)) {
1707 if (!tok_jmp(TOK_TYPEDEF
)) {
1708 readdefs(typedefdef
, NULL
);
1712 if (!tok_jmp(TOK_IF
)) {
1720 if (!tok_jmp(TOK_ELSE
)) {
1730 if (!tok_jmp(TOK_WHILE
)) {
1732 int break_beg
= nbreaks
;
1733 int continue_beg
= ncontinues
;
1743 break_fill(o_mklabel(), break_beg
);
1744 continue_fill(l1
, continue_beg
);
1747 if (!tok_jmp(TOK_DO
)) {
1749 int break_beg
= nbreaks
;
1750 int continue_beg
= ncontinues
;
1753 tok_expect(TOK_WHILE
);
1760 break_fill(o_mklabel(), break_beg
);
1761 continue_fill(l2
, continue_beg
);
1765 if (!tok_jmp(TOK_FOR
)) {
1766 long l_check
, l_jump
, j_fail
, j_pass
;
1767 int break_beg
= nbreaks
;
1768 int continue_beg
= ncontinues
;
1771 if (tok_see() != ';')
1774 l_check
= o_mklabel();
1775 if (tok_see() != ';') {
1783 l_jump
= o_mklabel();
1784 if (tok_see() != ')')
1793 break_fill(o_mklabel(), break_beg
);
1794 continue_fill(l_jump
, continue_beg
);
1797 if (!tok_jmp(TOK_SWITCH
)) {
1801 if (!tok_jmp(TOK_RETURN
)) {
1802 int ret
= tok_see() != ';';
1811 if (!tok_jmp(TOK_BREAK
)) {
1813 breaks
[nbreaks
++] = o_jmp(0);
1816 if (!tok_jmp(TOK_CONTINUE
)) {
1818 continues
[ncontinues
++] = o_jmp(0);
1821 if (!tok_jmp(TOK_GOTO
)) {
1822 tok_expect(TOK_NAME
);
1829 if (!tok_jmp(':')) {
1830 label_add(tok_id());
1836 static void readdecl(void)
1838 if (!tok_jmp(TOK_TYPEDEF
)) {
1839 readdefs(typedefdef
, NULL
);
1843 readdefs(globaldef
, NULL
);
1844 if (tok_see() == '{') {
1848 func_name
[0] = '\0';
1857 static void parse(void)
1859 while (tok_see() != TOK_EOF
)
1863 static void compat_macros(void)
1865 cpp_define("__STDC__", "");
1866 cpp_define("__linux__", "");
1868 cpp_define("__arm__", "");
1870 cpp_define("__i386__", "");
1873 /* ignored keywords */
1874 cpp_define("const", "");
1875 cpp_define("register", "");
1876 cpp_define("volatile", "");
1877 cpp_define("inline", "");
1878 cpp_define("restrict", "");
1879 cpp_define("__inline__", "");
1880 cpp_define("__restrict__", "");
1881 cpp_define("__attribute__(x)", "");
1882 cpp_define("__builtin_va_list__", "long");
1885 int main(int argc
, char *argv
[])
1891 while (i
< argc
&& argv
[i
][0] == '-') {
1892 if (argv
[i
][1] == 'I')
1893 cpp_addpath(argv
[i
][2] ? argv
[i
] + 2 : argv
[++i
]);
1894 if (argv
[i
][1] == 'D') {
1895 char *name
= argv
[i
] + 2;
1897 char *eq
= strchr(name
, '=');
1902 cpp_define(name
, def
);
1904 if (argv
[i
][1] == 'o')
1905 strcpy(obj
, argv
[i
][2] ? argv
[i
] + 2 : argv
[++i
]);
1909 die("neatcc: no file given\n");
1910 if (cpp_init(argv
[i
]))
1911 die("neatcc: cannot open <%s>\n", argv
[i
]);
1914 strcpy(obj
, argv
[i
]);
1915 obj
[strlen(obj
) - 1] = 'o';
1917 ofd
= open(obj
, O_WRONLY
| O_TRUNC
| O_CREAT
, 0600);