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 global_find(char *name
)
129 for (i
= 0; i
< nglobals
; i
++)
130 if (!strcmp(name
, globals
[i
].name
))
135 static void global_add(struct name
*name
)
137 int found
= global_find(name
->name
);
138 int i
= found
== -1 ? nglobals
++ : found
;
139 if (nglobals
>= MAXGLOBALS
)
140 err("nomem: MAXGLOBALS reached!\n");
141 memcpy(&globals
[i
], name
, sizeof(*name
));
144 #define MAXENUMS (1 << 10)
146 static struct enumval
{
152 static void enum_add(char *name
, int val
)
154 struct enumval
*ev
= &enums
[nenums
++];
155 if (nenums
>= MAXENUMS
)
156 err("nomem: MAXENUMS reached!\n");
157 strcpy(ev
->name
, name
);
161 static int enum_find(int *val
, char *name
)
164 for (i
= nenums
- 1; i
>= 0; --i
)
165 if (!strcmp(name
, enums
[i
].name
)) {
172 #define MAXTYPEDEFS (1 << 10)
174 static struct typdefinfo
{
177 } typedefs
[MAXTYPEDEFS
];
178 static int ntypedefs
;
180 static void typedef_add(char *name
, struct type
*type
)
182 struct typdefinfo
*ti
= &typedefs
[ntypedefs
++];
183 if (ntypedefs
>= MAXTYPEDEFS
)
184 err("nomem: MAXTYPEDEFS reached!\n");
185 strcpy(ti
->name
, name
);
186 memcpy(&ti
->type
, type
, sizeof(*type
));
189 static int typedef_find(char *name
)
192 for (i
= ntypedefs
- 1; i
>= 0; --i
)
193 if (!strcmp(name
, typedefs
[i
].name
))
198 #define MAXARRAYS (1 << 10)
200 static struct array
{
206 static int array_add(struct type
*type
, int n
)
208 struct array
*a
= &arrays
[narrays
++];
209 if (narrays
>= MAXARRAYS
)
210 err("nomem: MAXARRAYS reached!\n");
211 memcpy(&a
->type
, type
, sizeof(*type
));
216 static void array2ptr(struct type
*t
)
218 if (t
->flags
& T_ARRAY
&& !t
->ptr
) {
219 memcpy(t
, &arrays
[t
->id
].type
, sizeof(*t
));
224 #define MAXSTRUCTS (1 << 10)
225 #define MAXFIELDS (1 << 7)
227 static struct structinfo
{
229 struct name fields
[MAXFIELDS
];
233 } structs
[MAXSTRUCTS
];
236 static int struct_find(char *name
, int isunion
)
239 for (i
= nstructs
- 1; i
>= 0; --i
)
240 if (*structs
[i
].name
&& !strcmp(name
, structs
[i
].name
) &&
241 structs
[i
].isunion
== isunion
)
244 if (nstructs
>= MAXSTRUCTS
)
245 err("nomem: MAXTYPES reached!\n");
246 memset(&structs
[i
], 0, sizeof(structs
[i
]));
247 strcpy(structs
[i
].name
, name
);
248 structs
[i
].isunion
= isunion
;
252 static struct name
*struct_field(int id
, char *name
)
254 struct structinfo
*si
= &structs
[id
];
256 for (i
= 0; i
< si
->nfields
; i
++)
257 if (!strcmp(name
, si
->fields
[i
].name
))
258 return &si
->fields
[i
];
259 err("field not found\n");
262 #define MAXBREAK (1 << 7)
264 static long breaks
[MAXBREAK
];
266 static long continues
[MAXBREAK
];
267 static int ncontinues
;
269 static void break_fill(long addr
, int till
)
272 for (i
= till
; i
< nbreaks
; i
++)
273 o_filljmp2(breaks
[i
], addr
);
277 static void continue_fill(long addr
, int till
)
280 for (i
= till
; i
< ncontinues
; i
++)
281 o_filljmp2(continues
[i
], addr
);
285 static int type_totsz(struct type
*t
)
289 if (t
->flags
& T_ARRAY
)
290 return arrays
[t
->id
].n
* type_totsz(&arrays
[t
->id
].type
);
291 return t
->flags
& T_STRUCT
? structs
[t
->id
].size
: BT_SZ(t
->bt
);
294 static unsigned type_szde(struct type
*t
)
299 return type_totsz(&de
);
302 static void ts_de(int deref
)
304 struct type
*t
= &ts
[nts
- 1];
305 if (deref
&& t
->addr
&& (!(t
->flags
& T_ARRAY
) || (t
->ptr
)))
310 static void ts_pop_de(struct type
*t
)
317 if (t
->addr
&& (t
->ptr
|| !(t
->flags
& T_FUNC
)))
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 #define ALIGN(x, a) (((x) + (a) - 1) & ~((a) - 1))
396 #define MIN(a, b) ((a) < (b) ? (a) : (b))
398 static int type_alignment(struct type
*t
)
400 if (t
->flags
& T_ARRAY
&& !t
->ptr
)
401 return type_alignment(&arrays
[t
->id
].type
);
402 if (t
->flags
& T_STRUCT
&& !t
->ptr
)
403 return type_alignment(&structs
[t
->id
].fields
[0].type
);
404 return MIN(LONGSZ
, type_totsz(t
));
407 static void structdef(void *data
, struct name
*name
, unsigned flags
)
409 struct structinfo
*si
= data
;
412 if (si
->size
< type_totsz(&name
->type
))
413 si
->size
= type_totsz(&name
->type
);
415 struct type
*t
= &name
->type
;
416 int alignment
= type_alignment(t
);
417 if (t
->flags
& T_ARRAY
&& !t
->ptr
)
418 alignment
= MIN(LONGSZ
, type_totsz(&arrays
[t
->id
].type
));
419 si
->size
= ALIGN(si
->size
, alignment
);
420 name
->addr
= si
->size
;
421 si
->size
+= type_totsz(&name
->type
);
423 memcpy(&si
->fields
[si
->nfields
++], name
, sizeof(*name
));
426 static int readdefs(void (*def
)(void *, struct name
*, unsigned f
), void *data
);
428 static int struct_create(char *name
, int isunion
)
430 int id
= struct_find(name
, isunion
);
431 struct structinfo
*si
= &structs
[id
];
433 while (tok_jmp('}')) {
434 readdefs(structdef
, si
);
440 static void readexpr(void);
442 static void enum_create(void)
446 while (tok_jmp('}')) {
448 tok_expect(TOK_NAME
);
449 strcpy(name
, tok_id());
454 err("const expr expected!\n");
461 static int basetype(struct type
*type
, unsigned *flags
)
468 char name
[NAMELEN
] = "";
506 isunion
= tok_get() == TOK_UNION
;
507 if (!tok_jmp(TOK_NAME
))
508 strcpy(name
, tok_id());
509 if (tok_see() == '{')
510 type
->id
= struct_create(name
, isunion
);
512 type
->id
= struct_find(name
, isunion
);
513 type
->flags
|= T_STRUCT
;
519 if (tok_see() == '{')
521 type
->bt
= 4 | BT_SIGNED
;
524 if (tok_see() == TOK_NAME
) {
525 int id
= typedef_find(tok_id());
528 memcpy(type
, &typedefs
[id
].type
,
541 type
->bt
= size
| (sign
? BT_SIGNED
: 0);
545 static int readname(struct type
*main
, char *name
,
546 struct type
*base
, unsigned flags
);
548 static int readtype(struct type
*type
)
550 return readname(type
, NULL
, NULL
, 0);
553 static void readptrs(struct type
*type
)
555 while (!tok_jmp('*')) {
562 /* used to differenciate labels from case and cond exprs */
566 static void readpre(void);
568 static char *tmp_str(char *buf
, int len
)
570 static char name
[NAMELEN
];
573 sprintf(name
, "__neatcc.s%d", id
++);
574 dat
= o_mkdat(name
, len
, 0);
575 memcpy(dat
, buf
, len
);
579 static void readprimary(void)
582 if (!tok_jmp(TOK_NUM
)) {
584 int bt
= tok_num(&n
);
589 if (!tok_jmp(TOK_STR
)) {
593 t
.bt
= 1 | BT_SIGNED
;
599 o_sym(tmp_str(buf
, len
));
602 if (!tok_jmp(TOK_NAME
)) {
603 struct name unkn
= {""};
604 char *name
= unkn
.name
;
606 strcpy(name
, tok_id());
607 /* don't search for labels here */
608 if (!ncexpr
&& !caseexpr
&& tok_see() == ':')
610 for (i
= nlocals
- 1; i
>= 0; --i
) {
611 struct type
*t
= &locals
[i
].type
;
612 if (!strcmp(locals
[i
].name
, name
)) {
613 o_local(locals
[i
].addr
);
618 if ((n
= global_find(name
)) != -1) {
619 struct name
*g
= &globals
[n
];
620 struct type
*t
= &g
->type
;
621 char *elfname
= *g
->elfname
? g
->elfname
: g
->name
;
626 if (!enum_find(&n
, name
)) {
627 ts_push_bt(4 | BT_SIGNED
);
631 if (tok_see() != '(')
632 err("unknown symbol\n");
646 if (!t
.ptr
|| !o
.ptr
)
656 static void arrayderef(void)
662 if (!(t
.flags
& T_ARRAY
) && t
.addr
) {
664 o_deref(TYPE_BT(&t
));
679 static void inc_post(int op
)
681 struct type t
= ts
[nts
- 1];
682 /* pushing the value before inc */
688 /* increment by 1 or pointer size */
692 o_num(t
.ptr
> 0 ? type_szde(&t
) : 1);
696 o_assign(TYPE_BT(&t
));
700 static void readfield(void)
704 tok_expect(TOK_NAME
);
707 field
= struct_field(t
.id
, tok_id());
712 ts_push_addr(&field
->type
);
715 #define MAXFUNCS (1 << 10)
717 static struct funcinfo
{
718 struct type args
[MAXFIELDS
];
725 static int func_create(struct type
*ret
, struct name
*args
, int nargs
)
727 struct funcinfo
*fi
= &funcs
[nfuncs
++];
729 if (nfuncs
>= MAXFUNCS
)
730 err("nomem: MAXFUNCS reached!\n");
731 memcpy(&fi
->ret
, ret
, sizeof(*ret
));
732 for (i
= 0; i
< nargs
; i
++)
733 memcpy(&fi
->args
[i
], &args
[i
].type
, sizeof(*ret
));
738 static void readcall(void)
744 if (t
.flags
& T_FUNC
&& t
.ptr
> 0)
746 fi
= t
.flags
& T_FUNC
? &funcs
[t
.id
] : NULL
;
747 if (tok_see() != ')') {
752 } while (!tok_jmp(','));
755 o_call(argc
, fi
? TYPE_BT(&fi
->ret
) : 4 | BT_SIGNED
);
757 if (TYPE_BT(&fi
->ret
))
758 o_cast(TYPE_BT(&fi
->ret
));
761 ts_push_bt(4 | BT_SIGNED
);
765 static void readpost(void)
779 if (!tok_jmp(TOK2("++"))) {
783 if (!tok_jmp(TOK2("--"))) {
791 if (!tok_jmp(TOK2("->"))) {
800 static void inc_pre(int op
)
804 /* copy the destination */
806 ts_push(&ts
[nts
- 1]);
807 /* increment by 1 or pointer size */
809 o_num(t
.ptr
> 0 ? type_szde(&t
) : 1);
811 /* assign the result */
812 o_assign(TYPE_BT(&t
));
816 static void readpre(void)
823 err("cannot use the address\n");
835 err("dereferencing non-pointer\n");
837 o_deref(TYPE_BT(&t
));
847 ts_push_bt(4 | BT_SIGNED
);
862 if (!tok_jmp(TOK2("++"))) {
866 if (!tok_jmp(TOK2("--"))) {
870 if (!tok_jmp(TOK_SIZEOF
)) {
872 int op
= !tok_jmp('(');
883 o_num(type_totsz(&t
));
891 static void readmul(void)
914 static void readadd(void)
932 static void shift(int op
)
936 ts_pop_de2(NULL
, &t
);
937 o_bop(op
| (BT_SIGNED
& TYPE_BT(&t
) ? O_SIGNED
: 0));
938 ts_push_bt(TYPE_BT(&t
));
941 static void readshift(void)
945 if (!tok_jmp(TOK2("<<"))) {
949 if (!tok_jmp(TOK2(">>"))) {
957 static void cmp(int op
)
962 ts_pop_de2(&t1
, &t2
);
963 bt
= bt_op(TYPE_BT(&t1
), TYPE_BT(&t2
));
964 o_bop(op
| (bt
& BT_SIGNED
? O_SIGNED
: 0));
965 ts_push_bt(4 | BT_SIGNED
);
968 static void readcmp(void)
980 if (!tok_jmp(TOK2("<="))) {
984 if (!tok_jmp(TOK2(">="))) {
992 static void eq(int op
)
995 ts_pop_de2(NULL
, NULL
);
997 ts_push_bt(4 | BT_SIGNED
);
1000 static void readeq(void)
1004 if (!tok_jmp(TOK2("=="))) {
1008 if (!tok_jmp(TOK2("!="))) {
1016 static void readbitand(void)
1019 while (!tok_jmp('&')) {
1025 static void readxor(void)
1028 while (!tok_jmp('^')) {
1034 static void readbitor(void)
1037 while (!tok_jmp('|')) {
1043 #define MAXCOND (1 << 7)
1045 static void readand(void)
1047 long conds
[MAXCOND
];
1052 if (tok_see() != TOK2("&&"))
1056 conds
[nconds
++] = o_jz(0);
1057 while (!tok_jmp(TOK2("&&"))) {
1060 conds
[nconds
++] = o_jz(0);
1065 for (i
= 0; i
< nconds
; i
++)
1066 o_filljmp(conds
[i
]);
1071 ts_push_bt(4 | BT_SIGNED
);
1074 static void reador(void)
1076 long conds
[MAXCOND
];
1081 if (tok_see() != TOK2("||"))
1085 conds
[nconds
++] = o_jnz(0);
1086 while (!tok_jmp(TOK2("||"))) {
1089 conds
[nconds
++] = o_jnz(0);
1094 for (i
= 0; i
< nconds
; i
++)
1095 o_filljmp(conds
[i
]);
1100 ts_push_bt(4 | BT_SIGNED
);
1103 static void readcexpr(void);
1105 static int readcexpr_const(void)
1113 /* both branches yield the same type; so ignore the first */
1121 /* making sure t->addr == 0 on both branches */
1128 static void readcexpr(void)
1137 if (readcexpr_const()) {
1140 /* both branches yield the same type; so ignore the first */
1148 /* making sure t->addr == 0 on both branches */
1157 static void opassign(int op
, int ptrop
)
1159 struct type t
= ts
[nts
- 1];
1164 o_assign(TYPE_BT(&ts
[nts
- 2]));
1169 static void doassign(void)
1171 struct type t
= ts
[nts
- 1];
1172 if (!t
.ptr
&& t
.flags
& T_STRUCT
) {
1174 o_num(type_totsz(&t
));
1178 o_assign(TYPE_BT(&ts
[nts
- 1]));
1183 static void readexpr(void)
1186 if (!tok_jmp('=')) {
1191 if (!tok_jmp(TOK2("+="))) {
1195 if (!tok_jmp(TOK2("-="))) {
1199 if (!tok_jmp(TOK2("*="))) {
1203 if (!tok_jmp(TOK2("/="))) {
1207 if (!tok_jmp(TOK2("%="))) {
1211 if (!tok_jmp(TOK3("<<="))) {
1215 if (!tok_jmp(TOK3(">>="))) {
1219 if (!tok_jmp(TOK3("&="))) {
1223 if (!tok_jmp(TOK3("|="))) {
1227 if (!tok_jmp(TOK3("^="))) {
1233 static void readestmt(void)
1239 } while (!tok_jmp(','));
1242 static void o_localoff(long addr
, int off
)
1251 static struct type
*innertype(struct type
*t
)
1253 if (t
->flags
& T_ARRAY
&& !t
->ptr
)
1254 return innertype(&arrays
[t
->id
].type
);
1258 static void initexpr(struct type
*t
, int off
, void *obj
,
1259 void (*set
)(void *obj
, int off
, struct type
*t
))
1265 if (!t
->ptr
&& t
->flags
& T_STRUCT
) {
1266 struct structinfo
*si
= &structs
[t
->id
];
1268 for (i
= 0; i
< si
->nfields
; i
++) {
1269 struct name
*field
= &si
->fields
[i
];
1270 if (!tok_jmp('.')) {
1271 tok_expect(TOK_NAME
);
1272 field
= struct_field(t
->id
, tok_id());
1275 initexpr(&field
->type
, off
+ field
->addr
, obj
, set
);
1276 if (tok_jmp(',') || tok_see() == '}')
1279 } else if (t
->flags
& T_ARRAY
) {
1280 struct type
*t_de
= &arrays
[t
->id
].type
;
1282 for (i
= 0; ; i
++) {
1284 struct type
*it
= t_de
;
1285 if (!tok_jmp('[')) {
1292 if (tok_see() != '{')
1293 it
= innertype(t_de
);
1294 initexpr(it
, off
+ type_totsz(it
) * idx
, obj
, set
);
1295 if (tok_jmp(',') || tok_see() == '}')
1302 static void jumpbrace(void)
1305 while (tok_see() != '}' || depth
--)
1306 if (tok_get() == '{')
1311 static int initsize(void)
1313 long addr
= tok_addr();
1315 if (!tok_jmp(TOK_STR
)) {
1321 while (tok_jmp('}')) {
1323 if (!tok_jmp('[')) {
1332 while (tok_see() != '}' && tok_see() != ',')
1333 if (tok_get() == '{')
1341 #define F_GLOBAL(flags) (!((flags) & F_STATIC))
1343 static void globalinit(void *obj
, int off
, struct type
*t
)
1345 struct name
*name
= obj
;
1346 char *elfname
= *name
->elfname
? name
->elfname
: name
->name
;
1347 if (t
->flags
& T_ARRAY
&& tok_see() == TOK_STR
) {
1348 struct type
*t_de
= &arrays
[t
->id
].type
;
1349 if (!t_de
->ptr
&& !t_de
->flags
&& TYPE_SZ(t_de
) == 1) {
1352 tok_expect(TOK_STR
);
1354 memcpy((void *) name
->addr
+ off
, buf
, len
);
1359 o_datset(elfname
, off
, TYPE_BT(t
));
1363 static void globaldef(void *data
, struct name
*name
, unsigned flags
)
1365 struct type
*t
= &name
->type
;
1366 char *elfname
= *name
->elfname
? name
->elfname
: name
->name
;
1368 if (t
->flags
& T_ARRAY
&& !t
->ptr
&& !arrays
[t
->id
].n
)
1369 if (~flags
& F_EXTERN
)
1370 arrays
[t
->id
].n
= initsize();
1372 if (!(flags
& F_EXTERN
) && (!(t
->flags
& T_FUNC
) || t
->ptr
)) {
1374 name
->addr
= (long) o_mkdat(elfname
, sz
, F_GLOBAL(flags
));
1376 o_mkbss(elfname
, sz
, F_GLOBAL(flags
));
1380 initexpr(t
, 0, name
, globalinit
);
1383 static void localinit(void *obj
, int off
, struct type
*t
)
1385 long addr
= *(long *) obj
;
1386 if (t
->flags
& T_ARRAY
&& tok_see() == TOK_STR
) {
1387 struct type
*t_de
= &arrays
[t
->id
].type
;
1388 if (!t_de
->ptr
&& !t_de
->flags
&& TYPE_SZ(t_de
) == 1) {
1391 tok_expect(TOK_STR
);
1393 o_localoff(addr
, off
);
1394 o_sym(tmp_str(buf
, len
));
1401 o_localoff(addr
, off
);
1409 /* current function name */
1410 static char func_name
[NAMELEN
];
1412 static void localdef(void *data
, struct name
*name
, unsigned flags
)
1414 struct type
*t
= &name
->type
;
1415 if ((flags
& F_EXTERN
) || (t
->flags
& T_FUNC
) && !t
->ptr
) {
1419 if (flags
& F_STATIC
) {
1420 sprintf(name
->elfname
, "__neatcc.%s.%s", func_name
, name
->name
);
1421 globaldef(data
, name
, flags
);
1424 if (t
->flags
& T_ARRAY
&& !t
->ptr
&& !arrays
[t
->id
].n
)
1425 arrays
[t
->id
].n
= initsize();
1426 name
->addr
= o_mklocal(type_totsz(&name
->type
));
1428 if (flags
& F_INIT
) {
1429 if (t
->flags
& (T_ARRAY
| T_STRUCT
) && !t
->ptr
) {
1430 o_local(name
->addr
);
1432 o_num(type_totsz(t
));
1436 initexpr(t
, 0, &name
->addr
, localinit
);
1440 static void funcdef(char *name
, struct type
*type
, struct name
*args
,
1441 int nargs
, int varg
, unsigned flags
)
1443 struct name global
= {""};
1445 strcpy(global
.name
, name
);
1446 strcpy(func_name
, name
);
1447 memcpy(&global
.type
, type
, sizeof(*type
));
1448 o_func_beg(name
, nargs
, F_GLOBAL(flags
), varg
);
1449 global_add(&global
);
1450 for (i
= 0; i
< nargs
; i
++) {
1451 args
[i
].addr
= o_arg2loc(i
);
1452 local_add(&args
[i
]);
1456 static int readargs(struct name
*args
, int *varg
)
1461 while (tok_see() != ')') {
1462 if (!tok_jmp(TOK3("..."))) {
1466 readname(&args
[nargs
].type
, args
[nargs
].name
, NULL
, 0);
1467 array2ptr(&args
[nargs
].type
);
1473 if (nargs
== 1 && !TYPE_BT(&args
[0].type
))
1478 static int readname(struct type
*main
, char *name
,
1479 struct type
*base
, unsigned flags
)
1481 struct type tpool
[3];
1483 struct type
*type
= &tpool
[npool
++];
1484 struct type
*func
= NULL
;
1485 struct type
*ret
= NULL
;
1489 memset(tpool
, 0, sizeof(tpool
));
1493 if (basetype(type
, &flags
))
1496 memcpy(type
, base
, sizeof(*base
));
1499 if (!tok_jmp('(')) {
1501 type
= &tpool
[npool
++];
1505 if (!tok_jmp(TOK_NAME
) && name
)
1506 strcpy(name
, tok_id());
1507 while (!tok_jmp('[')) {
1513 err("const expr expected\n");
1518 for (i
= nar
- 1; i
>= 0; i
--) {
1519 type
->id
= array_add(type
, arsz
[i
]);
1520 if (func
&& i
== nar
- 1)
1521 func
= &arrays
[type
->id
].type
;
1522 type
->flags
= T_ARRAY
;
1528 if (tok_see() == '(') {
1529 struct name args
[MAXARGS
] = {{""}};
1531 int nargs
= readargs(args
, &varg
);
1535 type
= &tpool
[npool
++];
1538 func
->flags
= T_FUNC
;
1540 func
->id
= func_create(ret
, args
, nargs
);
1541 if (fdef
&& tok_see() == '{') {
1542 funcdef(name
, func
, args
, nargs
, varg
, flags
);
1546 memcpy(main
, type
, sizeof(*type
));
1550 static int readdefs(void (*def
)(void *data
, struct name
*name
, unsigned flags
),
1554 unsigned base_flags
;
1555 if (basetype(&base
, &base_flags
))
1557 while (tok_see() != ';' && tok_see() != '{') {
1558 struct name name
= {{""}};
1559 unsigned flags
= base_flags
;
1560 if (readname(&name
.type
, name
.name
, &base
, flags
))
1564 def(data
, &name
, flags
);
1570 static void typedefdef(void *data
, struct name
*name
, unsigned flags
)
1572 typedef_add(name
->name
, &name
->type
);
1575 static void readstmt(void);
1577 #define MAXCASES (1 << 7)
1579 static void readswitch(void)
1581 int break_beg
= nbreaks
;
1582 long val_addr
= o_mklocal(LONGSZ
);
1584 int ncases
= 0; /* number of case labels */
1585 long last_failed
= -1; /* address of last failed jmp */
1586 long last_matched
= -1; /* address of last walk through jmp */
1587 long default_addr
= -1; /* address of the default label */
1593 o_assign(TYPE_BT(&t
));
1598 while (tok_jmp('}')) {
1599 if (tok_see() != TOK_CASE
&& tok_see() != TOK_DEFAULT
) {
1604 last_matched
= o_jmp(0);
1605 if (tok_get() == TOK_CASE
) {
1606 if (last_failed
>= 0)
1607 o_filljmp(last_failed
);
1613 o_deref(TYPE_BT(&t
));
1615 last_failed
= o_jz(0);
1618 default_addr
= o_mklabel();
1621 if (last_matched
>= 0)
1622 o_filljmp(last_matched
);
1625 o_rmlocal(val_addr
, LONGSZ
);
1626 if (last_failed
>= 0)
1627 o_filljmp2(last_failed
,
1628 default_addr
>= 0 ? default_addr
: o_mklabel());
1629 break_fill(o_mklabel(), break_beg
);
1632 #define MAXGOTO (1 << 10)
1634 static struct gotoinfo
{
1640 static struct labelinfo
{
1646 static void goto_add(char *name
)
1648 strcpy(gotos
[ngotos
].name
, name
);
1649 gotos
[ngotos
++].addr
= o_jmp(0);
1652 static void label_add(char *name
)
1654 strcpy(labels
[nlabels
].name
, name
);
1655 labels
[nlabels
++].addr
= o_mklabel();
1658 static void goto_fill(void)
1661 for (i
= 0; i
< ngotos
; i
++)
1662 for (j
= 0; j
< nlabels
; j
++)
1663 if (!strcmp(gotos
[i
].name
, labels
[j
].name
)) {
1664 o_filljmp2(gotos
[i
].addr
, labels
[j
].addr
);
1669 static void readstmt(void)
1673 if (!tok_jmp('{')) {
1674 int _nlocals
= nlocals
;
1675 int _nglobals
= nglobals
;
1676 int _nenums
= nenums
;
1677 int _ntypedefs
= ntypedefs
;
1678 int _nstructs
= nstructs
;
1679 int _nfuncs
= nfuncs
;
1680 int _narrays
= narrays
;
1681 while (tok_jmp('}'))
1685 ntypedefs
= _ntypedefs
;
1686 nstructs
= _nstructs
;
1689 nglobals
= _nglobals
;
1692 if (!readdefs(localdef
, NULL
)) {
1696 if (!tok_jmp(TOK_TYPEDEF
)) {
1697 readdefs(typedefdef
, NULL
);
1701 if (!tok_jmp(TOK_IF
)) {
1709 if (!tok_jmp(TOK_ELSE
)) {
1719 if (!tok_jmp(TOK_WHILE
)) {
1721 int break_beg
= nbreaks
;
1722 int continue_beg
= ncontinues
;
1732 break_fill(o_mklabel(), break_beg
);
1733 continue_fill(l1
, continue_beg
);
1736 if (!tok_jmp(TOK_DO
)) {
1738 int break_beg
= nbreaks
;
1739 int continue_beg
= ncontinues
;
1742 tok_expect(TOK_WHILE
);
1749 break_fill(o_mklabel(), break_beg
);
1750 continue_fill(l2
, continue_beg
);
1754 if (!tok_jmp(TOK_FOR
)) {
1755 long l_check
, l_jump
, j_fail
, j_pass
;
1756 int break_beg
= nbreaks
;
1757 int continue_beg
= ncontinues
;
1760 if (tok_see() != ';')
1763 l_check
= o_mklabel();
1764 if (tok_see() != ';') {
1772 l_jump
= o_mklabel();
1773 if (tok_see() != ')')
1782 break_fill(o_mklabel(), break_beg
);
1783 continue_fill(l_jump
, continue_beg
);
1786 if (!tok_jmp(TOK_SWITCH
)) {
1790 if (!tok_jmp(TOK_RETURN
)) {
1791 int ret
= tok_see() != ';';
1800 if (!tok_jmp(TOK_BREAK
)) {
1802 breaks
[nbreaks
++] = o_jmp(0);
1805 if (!tok_jmp(TOK_CONTINUE
)) {
1807 continues
[ncontinues
++] = o_jmp(0);
1810 if (!tok_jmp(TOK_GOTO
)) {
1811 tok_expect(TOK_NAME
);
1818 if (!tok_jmp(':')) {
1819 label_add(tok_id());
1825 static void readdecl(void)
1827 if (!tok_jmp(TOK_TYPEDEF
)) {
1828 readdefs(typedefdef
, NULL
);
1832 readdefs(globaldef
, NULL
);
1833 if (tok_see() == '{') {
1837 func_name
[0] = '\0';
1846 static void parse(void)
1848 while (tok_see() != TOK_EOF
)
1852 static void compat_macros(void)
1854 cpp_define("__STDC__", "");
1855 cpp_define("__arm__", "");
1856 cpp_define("__linux__", "");
1858 /* ignored keywords */
1859 cpp_define("const", "");
1860 cpp_define("register", "");
1861 cpp_define("volatile", "");
1862 cpp_define("inline", "");
1863 cpp_define("restrict", "");
1864 cpp_define("__inline__", "");
1865 cpp_define("__restrict__", "");
1866 cpp_define("__attribute__(x)", "");
1867 cpp_define("__builtin_va_list__", "long");
1870 int main(int argc
, char *argv
[])
1876 while (i
< argc
&& argv
[i
][0] == '-') {
1877 if (argv
[i
][1] == 'I')
1878 cpp_addpath(argv
[i
][2] ? argv
[i
] + 2 : argv
[++i
]);
1879 if (argv
[i
][1] == 'D') {
1880 char *name
= argv
[i
] + 2;
1882 char *eq
= strchr(name
, '=');
1887 cpp_define(name
, def
);
1889 if (argv
[i
][1] == 'o')
1890 strcpy(obj
, argv
[i
][2] ? argv
[i
] + 2 : argv
[++i
]);
1894 die("neatcc: no file given\n");
1895 if (cpp_init(argv
[i
]))
1896 die("neatcc: cannot open <%s>\n", argv
[i
]);
1899 strcpy(obj
, argv
[i
]);
1900 obj
[strlen(obj
) - 1] = 'o';
1902 ofd
= open(obj
, O_WRONLY
| O_TRUNC
| O_CREAT
, 0600);