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 static int type_totsz(struct type
*t
)
296 if (t
->flags
& T_ARRAY
)
297 return arrays
[t
->id
].n
* type_totsz(&arrays
[t
->id
].type
);
298 return t
->flags
& T_STRUCT
? structs
[t
->id
].size
: BT_SZ(t
->bt
);
301 static unsigned type_szde(struct type
*t
)
306 return type_totsz(&de
);
309 static void ts_de(int deref
)
311 struct type
*t
= &ts
[nts
- 1];
312 if (deref
&& t
->addr
&& (!(t
->flags
& T_ARRAY
) || (t
->ptr
)))
317 static void ts_pop_de(struct type
*t
)
324 if (t
->addr
&& (t
->ptr
|| !(t
->flags
& T_FUNC
)))
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 for (i
= 0; tok_see() != '}'; i
++) {
1286 struct type
*it
= t_de
;
1287 if (!tok_jmp('[')) {
1294 if (tok_see() != '{')
1295 it
= innertype(t_de
);
1296 initexpr(it
, off
+ type_totsz(it
) * idx
, obj
, set
);
1304 static void jumpbrace(void)
1307 while (tok_see() != '}' || depth
--)
1308 if (tok_get() == '{')
1313 static int initsize(void)
1315 long addr
= tok_addr();
1317 if (!tok_jmp(TOK_STR
)) {
1323 while (tok_jmp('}')) {
1325 if (!tok_jmp('[')) {
1334 while (tok_see() != '}' && tok_see() != ',')
1335 if (tok_get() == '{')
1343 #define F_GLOBAL(flags) (!((flags) & F_STATIC))
1345 static void globalinit(void *obj
, int off
, struct type
*t
)
1347 struct name
*name
= obj
;
1348 char *elfname
= *name
->elfname
? name
->elfname
: name
->name
;
1349 if (t
->flags
& T_ARRAY
&& tok_see() == TOK_STR
) {
1350 struct type
*t_de
= &arrays
[t
->id
].type
;
1351 if (!t_de
->ptr
&& !t_de
->flags
&& TYPE_SZ(t_de
) == 1) {
1354 tok_expect(TOK_STR
);
1356 memcpy((void *) name
->addr
+ off
, buf
, len
);
1361 o_datset(elfname
, off
, TYPE_BT(t
));
1365 static void globaldef(void *data
, struct name
*name
, unsigned flags
)
1367 struct type
*t
= &name
->type
;
1368 char *elfname
= *name
->elfname
? name
->elfname
: name
->name
;
1370 if (t
->flags
& T_ARRAY
&& !t
->ptr
&& !arrays
[t
->id
].n
)
1371 if (~flags
& F_EXTERN
)
1372 arrays
[t
->id
].n
= initsize();
1374 if (!(flags
& F_EXTERN
) && (!(t
->flags
& T_FUNC
) || t
->ptr
)) {
1376 name
->addr
= (long) o_mkdat(elfname
, sz
, F_GLOBAL(flags
));
1378 o_mkbss(elfname
, sz
, F_GLOBAL(flags
));
1382 initexpr(t
, 0, name
, globalinit
);
1385 static void localinit(void *obj
, int off
, struct type
*t
)
1387 long addr
= *(long *) obj
;
1388 if (t
->flags
& T_ARRAY
&& tok_see() == TOK_STR
) {
1389 struct type
*t_de
= &arrays
[t
->id
].type
;
1390 if (!t_de
->ptr
&& !t_de
->flags
&& TYPE_SZ(t_de
) == 1) {
1393 tok_expect(TOK_STR
);
1395 o_localoff(addr
, off
);
1396 o_sym(tmp_str(buf
, len
));
1403 o_localoff(addr
, off
);
1411 /* current function name */
1412 static char func_name
[NAMELEN
];
1414 static void localdef(void *data
, struct name
*name
, unsigned flags
)
1416 struct type
*t
= &name
->type
;
1417 if ((flags
& F_EXTERN
) || (t
->flags
& T_FUNC
) && !t
->ptr
) {
1421 if (flags
& F_STATIC
) {
1422 sprintf(name
->elfname
, "__neatcc.%s.%s", func_name
, name
->name
);
1423 globaldef(data
, name
, flags
);
1426 if (t
->flags
& T_ARRAY
&& !t
->ptr
&& !arrays
[t
->id
].n
)
1427 arrays
[t
->id
].n
= initsize();
1428 name
->addr
= o_mklocal(type_totsz(&name
->type
));
1430 if (flags
& F_INIT
) {
1431 if (t
->flags
& (T_ARRAY
| T_STRUCT
) && !t
->ptr
) {
1432 o_local(name
->addr
);
1434 o_num(type_totsz(t
));
1438 initexpr(t
, 0, &name
->addr
, localinit
);
1442 static void funcdef(char *name
, struct type
*type
, struct name
*args
,
1443 int nargs
, int varg
, unsigned flags
)
1445 struct name global
= {""};
1447 strcpy(global
.name
, name
);
1448 strcpy(func_name
, name
);
1449 memcpy(&global
.type
, type
, sizeof(*type
));
1450 o_func_beg(name
, nargs
, F_GLOBAL(flags
), varg
);
1451 global_add(&global
);
1452 for (i
= 0; i
< nargs
; i
++) {
1453 args
[i
].addr
= o_arg2loc(i
);
1454 local_add(&args
[i
]);
1458 static int readargs(struct name
*args
, int *varg
)
1463 while (tok_see() != ')') {
1464 if (!tok_jmp(TOK3("..."))) {
1468 readname(&args
[nargs
].type
, args
[nargs
].name
, NULL
, 0);
1469 array2ptr(&args
[nargs
].type
);
1475 if (nargs
== 1 && !TYPE_BT(&args
[0].type
))
1480 static int readname(struct type
*main
, char *name
,
1481 struct type
*base
, unsigned flags
)
1483 struct type tpool
[3];
1485 struct type
*type
= &tpool
[npool
++];
1486 struct type
*func
= NULL
;
1487 struct type
*ret
= NULL
;
1491 memset(tpool
, 0, sizeof(tpool
));
1495 if (basetype(type
, &flags
))
1498 memcpy(type
, base
, sizeof(*base
));
1501 if (!tok_jmp('(')) {
1503 type
= &tpool
[npool
++];
1507 if (!tok_jmp(TOK_NAME
) && name
)
1508 strcpy(name
, tok_id());
1509 while (!tok_jmp('[')) {
1515 err("const expr expected\n");
1520 for (i
= nar
- 1; i
>= 0; i
--) {
1521 type
->id
= array_add(type
, arsz
[i
]);
1522 if (func
&& i
== nar
- 1)
1523 func
= &arrays
[type
->id
].type
;
1524 type
->flags
= T_ARRAY
;
1530 if (tok_see() == '(') {
1531 struct name args
[MAXARGS
] = {{""}};
1533 int nargs
= readargs(args
, &varg
);
1537 type
= &tpool
[npool
++];
1540 func
->flags
= T_FUNC
;
1542 func
->id
= func_create(ret
, args
, nargs
);
1543 if (fdef
&& tok_see() == '{') {
1544 funcdef(name
, func
, args
, nargs
, varg
, flags
);
1548 memcpy(main
, type
, sizeof(*type
));
1552 static int readdefs(void (*def
)(void *data
, struct name
*name
, unsigned flags
),
1556 unsigned base_flags
;
1557 if (basetype(&base
, &base_flags
))
1559 while (tok_see() != ';' && tok_see() != '{') {
1560 struct name name
= {{""}};
1561 unsigned flags
= base_flags
;
1562 if (readname(&name
.type
, name
.name
, &base
, flags
))
1566 def(data
, &name
, flags
);
1572 static void typedefdef(void *data
, struct name
*name
, unsigned flags
)
1574 typedef_add(name
->name
, &name
->type
);
1577 static void readstmt(void);
1579 #define MAXCASES (1 << 7)
1581 static void readswitch(void)
1583 int break_beg
= nbreaks
;
1584 long val_addr
= o_mklocal(LONGSZ
);
1586 int ncases
= 0; /* number of case labels */
1587 long last_failed
= -1; /* address of last failed jmp */
1588 long last_matched
= -1; /* address of last walk through jmp */
1589 long default_addr
= -1; /* address of the default label */
1595 o_assign(TYPE_BT(&t
));
1600 while (tok_jmp('}')) {
1601 if (tok_see() != TOK_CASE
&& tok_see() != TOK_DEFAULT
) {
1606 last_matched
= o_jmp(0);
1607 if (tok_get() == TOK_CASE
) {
1608 if (last_failed
>= 0)
1609 o_filljmp(last_failed
);
1615 o_deref(TYPE_BT(&t
));
1617 last_failed
= o_jz(0);
1621 last_failed
= o_jmp(0);
1622 default_addr
= o_mklabel();
1625 if (last_matched
>= 0)
1626 o_filljmp(last_matched
);
1629 o_rmlocal(val_addr
, LONGSZ
);
1630 if (last_failed
>= 0)
1631 o_filljmp2(last_failed
,
1632 default_addr
>= 0 ? default_addr
: o_mklabel());
1633 break_fill(o_mklabel(), break_beg
);
1636 #define MAXGOTO (1 << 10)
1638 static struct gotoinfo
{
1644 static struct labelinfo
{
1650 static void goto_add(char *name
)
1652 strcpy(gotos
[ngotos
].name
, name
);
1653 gotos
[ngotos
++].addr
= o_jmp(0);
1656 static void label_add(char *name
)
1658 strcpy(labels
[nlabels
].name
, name
);
1659 labels
[nlabels
++].addr
= o_mklabel();
1662 static void goto_fill(void)
1665 for (i
= 0; i
< ngotos
; i
++)
1666 for (j
= 0; j
< nlabels
; j
++)
1667 if (!strcmp(gotos
[i
].name
, labels
[j
].name
)) {
1668 o_filljmp2(gotos
[i
].addr
, labels
[j
].addr
);
1673 static void readstmt(void)
1677 if (!tok_jmp('{')) {
1678 int _nlocals
= nlocals
;
1679 int _nglobals
= nglobals
;
1680 int _nenums
= nenums
;
1681 int _ntypedefs
= ntypedefs
;
1682 int _nstructs
= nstructs
;
1683 int _nfuncs
= nfuncs
;
1684 int _narrays
= narrays
;
1685 while (tok_jmp('}'))
1689 ntypedefs
= _ntypedefs
;
1690 nstructs
= _nstructs
;
1693 nglobals
= _nglobals
;
1696 if (!readdefs(localdef
, NULL
)) {
1700 if (!tok_jmp(TOK_TYPEDEF
)) {
1701 readdefs(typedefdef
, NULL
);
1705 if (!tok_jmp(TOK_IF
)) {
1713 if (!tok_jmp(TOK_ELSE
)) {
1723 if (!tok_jmp(TOK_WHILE
)) {
1725 int break_beg
= nbreaks
;
1726 int continue_beg
= ncontinues
;
1736 break_fill(o_mklabel(), break_beg
);
1737 continue_fill(l1
, continue_beg
);
1740 if (!tok_jmp(TOK_DO
)) {
1742 int break_beg
= nbreaks
;
1743 int continue_beg
= ncontinues
;
1746 tok_expect(TOK_WHILE
);
1753 break_fill(o_mklabel(), break_beg
);
1754 continue_fill(l2
, continue_beg
);
1758 if (!tok_jmp(TOK_FOR
)) {
1759 long l_check
, l_jump
, j_fail
, j_pass
;
1760 int break_beg
= nbreaks
;
1761 int continue_beg
= ncontinues
;
1764 if (tok_see() != ';')
1767 l_check
= o_mklabel();
1768 if (tok_see() != ';') {
1776 l_jump
= o_mklabel();
1777 if (tok_see() != ')')
1786 break_fill(o_mklabel(), break_beg
);
1787 continue_fill(l_jump
, continue_beg
);
1790 if (!tok_jmp(TOK_SWITCH
)) {
1794 if (!tok_jmp(TOK_RETURN
)) {
1795 int ret
= tok_see() != ';';
1804 if (!tok_jmp(TOK_BREAK
)) {
1806 breaks
[nbreaks
++] = o_jmp(0);
1809 if (!tok_jmp(TOK_CONTINUE
)) {
1811 continues
[ncontinues
++] = o_jmp(0);
1814 if (!tok_jmp(TOK_GOTO
)) {
1815 tok_expect(TOK_NAME
);
1822 if (!tok_jmp(':')) {
1823 label_add(tok_id());
1829 static void readdecl(void)
1831 if (!tok_jmp(TOK_TYPEDEF
)) {
1832 readdefs(typedefdef
, NULL
);
1836 readdefs(globaldef
, NULL
);
1837 if (tok_see() == '{') {
1841 func_name
[0] = '\0';
1850 static void parse(void)
1852 while (tok_see() != TOK_EOF
)
1856 static void compat_macros(void)
1858 cpp_define("__STDC__", "");
1859 cpp_define("__arm__", "");
1860 cpp_define("__linux__", "");
1862 /* ignored keywords */
1863 cpp_define("const", "");
1864 cpp_define("register", "");
1865 cpp_define("volatile", "");
1866 cpp_define("inline", "");
1867 cpp_define("restrict", "");
1868 cpp_define("__inline__", "");
1869 cpp_define("__restrict__", "");
1870 cpp_define("__attribute__(x)", "");
1871 cpp_define("__builtin_va_list__", "long");
1874 int main(int argc
, char *argv
[])
1880 while (i
< argc
&& argv
[i
][0] == '-') {
1881 if (argv
[i
][1] == 'I')
1882 cpp_addpath(argv
[i
][2] ? argv
[i
] + 2 : argv
[++i
]);
1883 if (argv
[i
][1] == 'D') {
1884 char *name
= argv
[i
] + 2;
1886 char *eq
= strchr(name
, '=');
1891 cpp_define(name
, def
);
1893 if (argv
[i
][1] == 'o')
1894 strcpy(obj
, argv
[i
][2] ? argv
[i
] + 2 : argv
[++i
]);
1898 die("neatcc: no file given\n");
1899 if (cpp_init(argv
[i
]))
1900 die("neatcc: cannot open <%s>\n", argv
[i
]);
1903 strcpy(obj
, argv
[i
]);
1904 obj
[strlen(obj
) - 1] = 'o';
1906 ofd
= open(obj
, O_WRONLY
| O_TRUNC
| O_CREAT
, 0600);