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)
589 if (!tok_jmp(TOK_NUM
)) {
591 int bt
= tok_num(&n
);
596 if (!tok_jmp(TOK_STR
)) {
600 t
.bt
= 1 | BT_SIGNED
;
606 o_sym(tmp_str(buf
, len
));
609 if (!tok_jmp(TOK_NAME
)) {
610 struct name unkn
= {""};
611 char *name
= unkn
.name
;
613 strcpy(name
, tok_id());
614 /* don't search for labels here */
615 if (!ncexpr
&& !caseexpr
&& tok_see() == ':')
617 if ((n
= local_find(name
)) != -1) {
618 struct name
*l
= &locals
[n
];
620 ts_push_addr(&l
->type
);
623 if ((n
= global_find(name
)) != -1) {
624 struct name
*g
= &globals
[n
];
625 o_sym(*g
->elfname
? g
->elfname
: g
->name
);
626 ts_push_addr(&g
->type
);
629 if (!enum_find(&n
, name
)) {
630 ts_push_bt(4 | BT_SIGNED
);
634 if (tok_see() != '(')
635 err("unknown symbol\n");
649 if (!t
.ptr
|| !o
.ptr
)
659 static void arrayderef(void)
665 if (!(t
.flags
& T_ARRAY
) && t
.addr
) {
667 o_deref(TYPE_BT(&t
));
682 static void inc_post(int op
)
684 struct type t
= ts
[nts
- 1];
685 /* pushing the value before inc */
691 /* increment by 1 or pointer size */
695 o_num(t
.ptr
> 0 ? type_szde(&t
) : 1);
699 o_assign(TYPE_BT(&t
));
703 static void readfield(void)
707 tok_expect(TOK_NAME
);
710 field
= struct_field(t
.id
, tok_id());
715 ts_push_addr(&field
->type
);
718 #define MAXFUNCS (1 << 10)
720 static struct funcinfo
{
721 struct type args
[MAXFIELDS
];
728 static int func_create(struct type
*ret
, struct name
*args
, int nargs
)
730 struct funcinfo
*fi
= &funcs
[nfuncs
++];
732 if (nfuncs
>= MAXFUNCS
)
733 err("nomem: MAXFUNCS reached!\n");
734 memcpy(&fi
->ret
, ret
, sizeof(*ret
));
735 for (i
= 0; i
< nargs
; i
++)
736 memcpy(&fi
->args
[i
], &args
[i
].type
, sizeof(*ret
));
741 static void readcall(void)
747 if (t
.flags
& T_FUNC
&& t
.ptr
> 0)
749 fi
= t
.flags
& T_FUNC
? &funcs
[t
.id
] : NULL
;
750 if (tok_see() != ')') {
755 } while (!tok_jmp(','));
758 o_call(argc
, fi
? TYPE_BT(&fi
->ret
) : 4 | BT_SIGNED
);
760 if (TYPE_BT(&fi
->ret
))
761 o_cast(TYPE_BT(&fi
->ret
));
764 ts_push_bt(4 | BT_SIGNED
);
768 static void readpost(void)
782 if (!tok_jmp(TOK2("++"))) {
786 if (!tok_jmp(TOK2("--"))) {
794 if (!tok_jmp(TOK2("->"))) {
803 static void inc_pre(int op
)
807 /* copy the destination */
809 ts_push(&ts
[nts
- 1]);
810 /* increment by 1 or pointer size */
812 o_num(t
.ptr
> 0 ? type_szde(&t
) : 1);
814 /* assign the result */
815 o_assign(TYPE_BT(&t
));
819 static void readpre(void)
826 err("cannot use the address\n");
838 err("dereferencing non-pointer\n");
840 o_deref(TYPE_BT(&t
));
850 ts_push_bt(4 | BT_SIGNED
);
865 if (!tok_jmp(TOK2("++"))) {
869 if (!tok_jmp(TOK2("--"))) {
873 if (!tok_jmp(TOK_SIZEOF
)) {
875 int op
= !tok_jmp('(');
886 o_num(type_totsz(&t
));
894 static void readmul(void)
917 static void readadd(void)
935 static void shift(int op
)
939 ts_pop_de2(NULL
, &t
);
940 o_bop(op
| (BT_SIGNED
& TYPE_BT(&t
) ? O_SIGNED
: 0));
941 ts_push_bt(TYPE_BT(&t
));
944 static void readshift(void)
948 if (!tok_jmp(TOK2("<<"))) {
952 if (!tok_jmp(TOK2(">>"))) {
960 static void cmp(int op
)
965 ts_pop_de2(&t1
, &t2
);
966 bt
= bt_op(TYPE_BT(&t1
), TYPE_BT(&t2
));
967 o_bop(op
| (bt
& BT_SIGNED
? O_SIGNED
: 0));
968 ts_push_bt(4 | BT_SIGNED
);
971 static void readcmp(void)
983 if (!tok_jmp(TOK2("<="))) {
987 if (!tok_jmp(TOK2(">="))) {
995 static void eq(int op
)
998 ts_pop_de2(NULL
, NULL
);
1000 ts_push_bt(4 | BT_SIGNED
);
1003 static void readeq(void)
1007 if (!tok_jmp(TOK2("=="))) {
1011 if (!tok_jmp(TOK2("!="))) {
1019 static void readbitand(void)
1022 while (!tok_jmp('&')) {
1028 static void readxor(void)
1031 while (!tok_jmp('^')) {
1037 static void readbitor(void)
1040 while (!tok_jmp('|')) {
1046 #define MAXCOND (1 << 7)
1048 static void readand(void)
1050 long conds
[MAXCOND
];
1055 if (tok_see() != TOK2("&&"))
1059 conds
[nconds
++] = o_jz(0);
1060 while (!tok_jmp(TOK2("&&"))) {
1063 conds
[nconds
++] = o_jz(0);
1068 for (i
= 0; i
< nconds
; i
++)
1069 o_filljmp(conds
[i
]);
1074 ts_push_bt(4 | BT_SIGNED
);
1077 static void reador(void)
1079 long conds
[MAXCOND
];
1084 if (tok_see() != TOK2("||"))
1088 conds
[nconds
++] = o_jnz(0);
1089 while (!tok_jmp(TOK2("||"))) {
1092 conds
[nconds
++] = o_jnz(0);
1097 for (i
= 0; i
< nconds
; i
++)
1098 o_filljmp(conds
[i
]);
1103 ts_push_bt(4 | BT_SIGNED
);
1106 static void readcexpr(void);
1108 static int readcexpr_const(void)
1116 /* both branches yield the same type; so ignore the first */
1124 /* making sure t->addr == 0 on both branches */
1131 static void readcexpr(void)
1140 if (readcexpr_const()) {
1143 /* both branches yield the same type; so ignore the first */
1151 /* making sure t->addr == 0 on both branches */
1160 static void opassign(int op
, int ptrop
)
1162 struct type t
= ts
[nts
- 1];
1167 o_assign(TYPE_BT(&ts
[nts
- 2]));
1172 static void doassign(void)
1174 struct type t
= ts
[nts
- 1];
1175 if (!t
.ptr
&& t
.flags
& T_STRUCT
) {
1177 o_num(type_totsz(&t
));
1181 o_assign(TYPE_BT(&ts
[nts
- 1]));
1186 static void readexpr(void)
1189 if (!tok_jmp('=')) {
1194 if (!tok_jmp(TOK2("+="))) {
1198 if (!tok_jmp(TOK2("-="))) {
1202 if (!tok_jmp(TOK2("*="))) {
1206 if (!tok_jmp(TOK2("/="))) {
1210 if (!tok_jmp(TOK2("%="))) {
1214 if (!tok_jmp(TOK3("<<="))) {
1218 if (!tok_jmp(TOK3(">>="))) {
1222 if (!tok_jmp(TOK3("&="))) {
1226 if (!tok_jmp(TOK3("|="))) {
1230 if (!tok_jmp(TOK3("^="))) {
1236 static void readestmt(void)
1242 } while (!tok_jmp(','));
1245 static void o_localoff(long addr
, int off
)
1254 static struct type
*innertype(struct type
*t
)
1256 if (t
->flags
& T_ARRAY
&& !t
->ptr
)
1257 return innertype(&arrays
[t
->id
].type
);
1261 static void initexpr(struct type
*t
, int off
, void *obj
,
1262 void (*set
)(void *obj
, int off
, struct type
*t
))
1268 if (!t
->ptr
&& t
->flags
& T_STRUCT
) {
1269 struct structinfo
*si
= &structs
[t
->id
];
1271 for (i
= 0; i
< si
->nfields
&& tok_see() != '}'; i
++) {
1272 struct name
*field
= &si
->fields
[i
];
1273 if (!tok_jmp('.')) {
1274 tok_expect(TOK_NAME
);
1275 field
= struct_field(t
->id
, tok_id());
1278 initexpr(&field
->type
, off
+ field
->addr
, obj
, set
);
1282 } else if (t
->flags
& T_ARRAY
) {
1283 struct type
*t_de
= &arrays
[t
->id
].type
;
1285 for (i
= 0; tok_see() != '}'; i
++) {
1287 struct type
*it
= t_de
;
1288 if (!tok_jmp('[')) {
1295 if (tok_see() != '{')
1296 it
= innertype(t_de
);
1297 initexpr(it
, off
+ type_totsz(it
) * idx
, obj
, set
);
1305 static void jumpbrace(void)
1308 while (tok_see() != '}' || depth
--)
1309 if (tok_get() == '{')
1314 static int initsize(void)
1316 long addr
= tok_addr();
1318 if (!tok_jmp(TOK_STR
)) {
1324 while (tok_jmp('}')) {
1326 if (!tok_jmp('[')) {
1335 while (tok_see() != '}' && tok_see() != ',')
1336 if (tok_get() == '{')
1344 #define F_GLOBAL(flags) (!((flags) & F_STATIC))
1346 static void globalinit(void *obj
, int off
, struct type
*t
)
1348 struct name
*name
= obj
;
1349 char *elfname
= *name
->elfname
? name
->elfname
: name
->name
;
1350 if (t
->flags
& T_ARRAY
&& tok_see() == TOK_STR
) {
1351 struct type
*t_de
= &arrays
[t
->id
].type
;
1352 if (!t_de
->ptr
&& !t_de
->flags
&& TYPE_SZ(t_de
) == 1) {
1355 tok_expect(TOK_STR
);
1357 memcpy((void *) name
->addr
+ off
, buf
, len
);
1362 o_datset(elfname
, off
, TYPE_BT(t
));
1366 static void globaldef(void *data
, struct name
*name
, unsigned flags
)
1368 struct type
*t
= &name
->type
;
1369 char *elfname
= *name
->elfname
? name
->elfname
: name
->name
;
1371 if (t
->flags
& T_ARRAY
&& !t
->ptr
&& !arrays
[t
->id
].n
)
1372 if (~flags
& F_EXTERN
)
1373 arrays
[t
->id
].n
= initsize();
1375 if (!(flags
& F_EXTERN
) && (!(t
->flags
& T_FUNC
) || t
->ptr
)) {
1377 name
->addr
= (long) o_mkdat(elfname
, sz
, F_GLOBAL(flags
));
1379 o_mkbss(elfname
, sz
, F_GLOBAL(flags
));
1383 initexpr(t
, 0, name
, globalinit
);
1386 static void localinit(void *obj
, int off
, struct type
*t
)
1388 long addr
= *(long *) obj
;
1389 if (t
->flags
& T_ARRAY
&& tok_see() == TOK_STR
) {
1390 struct type
*t_de
= &arrays
[t
->id
].type
;
1391 if (!t_de
->ptr
&& !t_de
->flags
&& TYPE_SZ(t_de
) == 1) {
1394 tok_expect(TOK_STR
);
1396 o_localoff(addr
, off
);
1397 o_sym(tmp_str(buf
, len
));
1404 o_localoff(addr
, off
);
1412 /* current function name */
1413 static char func_name
[NAMELEN
];
1415 static void localdef(void *data
, struct name
*name
, unsigned flags
)
1417 struct type
*t
= &name
->type
;
1418 if ((flags
& F_EXTERN
) || (t
->flags
& T_FUNC
) && !t
->ptr
) {
1422 if (flags
& F_STATIC
) {
1423 sprintf(name
->elfname
, "__neatcc.%s.%s", func_name
, name
->name
);
1424 globaldef(data
, name
, flags
);
1427 if (t
->flags
& T_ARRAY
&& !t
->ptr
&& !arrays
[t
->id
].n
)
1428 arrays
[t
->id
].n
= initsize();
1429 name
->addr
= o_mklocal(type_totsz(&name
->type
));
1431 if (flags
& F_INIT
) {
1432 if (t
->flags
& (T_ARRAY
| T_STRUCT
) && !t
->ptr
) {
1433 o_local(name
->addr
);
1435 o_num(type_totsz(t
));
1439 initexpr(t
, 0, &name
->addr
, localinit
);
1443 static void funcdef(char *name
, struct type
*type
, struct name
*args
,
1444 int nargs
, int varg
, unsigned flags
)
1446 struct name global
= {""};
1448 strcpy(global
.name
, name
);
1449 strcpy(func_name
, name
);
1450 memcpy(&global
.type
, type
, sizeof(*type
));
1451 o_func_beg(name
, nargs
, F_GLOBAL(flags
), varg
);
1452 global_add(&global
);
1453 for (i
= 0; i
< nargs
; i
++) {
1454 args
[i
].addr
= o_arg2loc(i
);
1455 local_add(&args
[i
]);
1459 static int readargs(struct name
*args
, int *varg
)
1464 while (tok_see() != ')') {
1465 if (!tok_jmp(TOK3("..."))) {
1469 readname(&args
[nargs
].type
, args
[nargs
].name
, NULL
, 0);
1470 array2ptr(&args
[nargs
].type
);
1476 if (nargs
== 1 && !TYPE_BT(&args
[0].type
))
1481 static int readname(struct type
*main
, char *name
,
1482 struct type
*base
, unsigned flags
)
1484 struct type tpool
[3];
1486 struct type
*type
= &tpool
[npool
++];
1487 struct type
*func
= NULL
;
1488 struct type
*ret
= NULL
;
1492 memset(tpool
, 0, sizeof(tpool
));
1496 if (basetype(type
, &flags
))
1499 memcpy(type
, base
, sizeof(*base
));
1502 if (!tok_jmp('(')) {
1504 type
= &tpool
[npool
++];
1508 if (!tok_jmp(TOK_NAME
) && name
)
1509 strcpy(name
, tok_id());
1510 while (!tok_jmp('[')) {
1516 err("const expr expected\n");
1521 for (i
= nar
- 1; i
>= 0; i
--) {
1522 type
->id
= array_add(type
, arsz
[i
]);
1523 if (func
&& i
== nar
- 1)
1524 func
= &arrays
[type
->id
].type
;
1525 type
->flags
= T_ARRAY
;
1531 if (tok_see() == '(') {
1532 struct name args
[MAXARGS
] = {{""}};
1534 int nargs
= readargs(args
, &varg
);
1538 type
= &tpool
[npool
++];
1541 func
->flags
= T_FUNC
;
1543 func
->id
= func_create(ret
, args
, nargs
);
1544 if (fdef
&& tok_see() == '{') {
1545 funcdef(name
, func
, args
, nargs
, varg
, flags
);
1549 memcpy(main
, type
, sizeof(*type
));
1553 static int readdefs(void (*def
)(void *data
, struct name
*name
, unsigned flags
),
1557 unsigned base_flags
;
1558 if (basetype(&base
, &base_flags
))
1560 while (tok_see() != ';' && tok_see() != '{') {
1561 struct name name
= {{""}};
1562 unsigned flags
= base_flags
;
1563 if (readname(&name
.type
, name
.name
, &base
, flags
))
1567 def(data
, &name
, flags
);
1573 static void typedefdef(void *data
, struct name
*name
, unsigned flags
)
1575 typedef_add(name
->name
, &name
->type
);
1578 static void readstmt(void);
1580 #define MAXCASES (1 << 7)
1582 static void readswitch(void)
1584 int break_beg
= nbreaks
;
1585 long val_addr
= o_mklocal(LONGSZ
);
1587 int ncases
= 0; /* number of case labels */
1588 long last_failed
= -1; /* address of last failed jmp */
1589 long last_matched
= -1; /* address of last walk through jmp */
1590 long default_addr
= -1; /* address of the default label */
1596 o_assign(TYPE_BT(&t
));
1601 while (tok_jmp('}')) {
1602 if (tok_see() != TOK_CASE
&& tok_see() != TOK_DEFAULT
) {
1607 last_matched
= o_jmp(0);
1608 if (tok_get() == TOK_CASE
) {
1609 if (last_failed
>= 0)
1610 o_filljmp(last_failed
);
1616 o_deref(TYPE_BT(&t
));
1618 last_failed
= o_jz(0);
1621 default_addr
= o_mklabel();
1624 if (last_matched
>= 0)
1625 o_filljmp(last_matched
);
1628 o_rmlocal(val_addr
, LONGSZ
);
1629 if (last_failed
>= 0)
1630 o_filljmp2(last_failed
,
1631 default_addr
>= 0 ? default_addr
: o_mklabel());
1632 break_fill(o_mklabel(), break_beg
);
1635 #define MAXGOTO (1 << 10)
1637 static struct gotoinfo
{
1643 static struct labelinfo
{
1649 static void goto_add(char *name
)
1651 strcpy(gotos
[ngotos
].name
, name
);
1652 gotos
[ngotos
++].addr
= o_jmp(0);
1655 static void label_add(char *name
)
1657 strcpy(labels
[nlabels
].name
, name
);
1658 labels
[nlabels
++].addr
= o_mklabel();
1661 static void goto_fill(void)
1664 for (i
= 0; i
< ngotos
; i
++)
1665 for (j
= 0; j
< nlabels
; j
++)
1666 if (!strcmp(gotos
[i
].name
, labels
[j
].name
)) {
1667 o_filljmp2(gotos
[i
].addr
, labels
[j
].addr
);
1672 static void readstmt(void)
1676 if (!tok_jmp('{')) {
1677 int _nlocals
= nlocals
;
1678 int _nglobals
= nglobals
;
1679 int _nenums
= nenums
;
1680 int _ntypedefs
= ntypedefs
;
1681 int _nstructs
= nstructs
;
1682 int _nfuncs
= nfuncs
;
1683 int _narrays
= narrays
;
1684 while (tok_jmp('}'))
1688 ntypedefs
= _ntypedefs
;
1689 nstructs
= _nstructs
;
1692 nglobals
= _nglobals
;
1695 if (!readdefs(localdef
, NULL
)) {
1699 if (!tok_jmp(TOK_TYPEDEF
)) {
1700 readdefs(typedefdef
, NULL
);
1704 if (!tok_jmp(TOK_IF
)) {
1712 if (!tok_jmp(TOK_ELSE
)) {
1722 if (!tok_jmp(TOK_WHILE
)) {
1724 int break_beg
= nbreaks
;
1725 int continue_beg
= ncontinues
;
1735 break_fill(o_mklabel(), break_beg
);
1736 continue_fill(l1
, continue_beg
);
1739 if (!tok_jmp(TOK_DO
)) {
1741 int break_beg
= nbreaks
;
1742 int continue_beg
= ncontinues
;
1745 tok_expect(TOK_WHILE
);
1752 break_fill(o_mklabel(), break_beg
);
1753 continue_fill(l2
, continue_beg
);
1757 if (!tok_jmp(TOK_FOR
)) {
1758 long l_check
, l_jump
, j_fail
, j_pass
;
1759 int break_beg
= nbreaks
;
1760 int continue_beg
= ncontinues
;
1763 if (tok_see() != ';')
1766 l_check
= o_mklabel();
1767 if (tok_see() != ';') {
1775 l_jump
= o_mklabel();
1776 if (tok_see() != ')')
1785 break_fill(o_mklabel(), break_beg
);
1786 continue_fill(l_jump
, continue_beg
);
1789 if (!tok_jmp(TOK_SWITCH
)) {
1793 if (!tok_jmp(TOK_RETURN
)) {
1794 int ret
= tok_see() != ';';
1803 if (!tok_jmp(TOK_BREAK
)) {
1805 breaks
[nbreaks
++] = o_jmp(0);
1808 if (!tok_jmp(TOK_CONTINUE
)) {
1810 continues
[ncontinues
++] = o_jmp(0);
1813 if (!tok_jmp(TOK_GOTO
)) {
1814 tok_expect(TOK_NAME
);
1821 if (!tok_jmp(':')) {
1822 label_add(tok_id());
1828 static void readdecl(void)
1830 if (!tok_jmp(TOK_TYPEDEF
)) {
1831 readdefs(typedefdef
, NULL
);
1835 readdefs(globaldef
, NULL
);
1836 if (tok_see() == '{') {
1840 func_name
[0] = '\0';
1849 static void parse(void)
1851 while (tok_see() != TOK_EOF
)
1855 static void compat_macros(void)
1857 cpp_define("__STDC__", "");
1858 cpp_define("__arm__", "");
1859 cpp_define("__linux__", "");
1861 /* ignored keywords */
1862 cpp_define("const", "");
1863 cpp_define("register", "");
1864 cpp_define("volatile", "");
1865 cpp_define("inline", "");
1866 cpp_define("restrict", "");
1867 cpp_define("__inline__", "");
1868 cpp_define("__restrict__", "");
1869 cpp_define("__attribute__(x)", "");
1870 cpp_define("__builtin_va_list__", "long");
1873 int main(int argc
, char *argv
[])
1879 while (i
< argc
&& argv
[i
][0] == '-') {
1880 if (argv
[i
][1] == 'I')
1881 cpp_addpath(argv
[i
][2] ? argv
[i
] + 2 : argv
[++i
]);
1882 if (argv
[i
][1] == 'D') {
1883 char *name
= argv
[i
] + 2;
1885 char *eq
= strchr(name
, '=');
1890 cpp_define(name
, def
);
1892 if (argv
[i
][1] == 'o')
1893 strcpy(obj
, argv
[i
][2] ? argv
[i
] + 2 : argv
[++i
]);
1897 die("neatcc: no file given\n");
1898 if (cpp_init(argv
[i
]))
1899 die("neatcc: cannot open <%s>\n", argv
[i
]);
1902 strcpy(obj
, argv
[i
]);
1903 obj
[strlen(obj
) - 1] = 'o';
1905 ofd
= open(obj
, O_WRONLY
| O_TRUNC
| O_CREAT
, 0600);