2 * neatcc - a small and simple C compiler
4 * Copyright (C) 2010-2012 Ali Gholami Rudi
6 * This program is released under GNU GPL version 2.
15 #include <sys/types.h>
20 static int nogen
; /* don't generate code */
22 #define o_bop(op) {if (!nogen) o_bop(op);}
23 #define o_uop(op) {if (!nogen) o_uop(op);}
24 #define o_cast(bt) {if (!nogen) o_cast(bt);}
25 #define o_memcpy() {if (!nogen) o_memcpy();}
26 #define o_memset() {if (!nogen) o_memset();}
27 #define o_call(argc, ret) {if (!nogen) o_call(argc, ret);}
28 #define o_ret(ret) {if (!nogen) o_ret(ret);}
29 #define o_assign(bt) {if (!nogen) o_assign(bt);}
30 #define o_deref(bt) {if (!nogen) o_deref(bt);}
31 #define o_load() {if (!nogen) o_load();}
32 #define o_popnum(c) (nogen ? 0 : o_popnum(c))
33 #define o_num(n) {if (!nogen) o_num(n);}
34 #define o_local(addr) {if (!nogen) o_local(addr);}
35 #define o_sym(sym) {if (!nogen) o_sym(sym);}
36 #define o_tmpdrop(n) {if (!nogen) o_tmpdrop(n);}
37 #define o_tmpswap() {if (!nogen) o_tmpswap();}
38 #define o_tmpcopy() {if (!nogen) o_tmpcopy();}
39 #define o_label(id) {if (!nogen) o_label(id);}
40 #define o_jz(id) {if (!nogen) o_jz(id);}
41 #define o_jnz(id) {if (!nogen) o_jnz(id);}
42 #define o_jmp(id) {if (!nogen) o_jmp(id);}
43 #define o_fork() {if (!nogen) o_fork();}
44 #define o_forkpush() {if (!nogen) o_forkpush();}
45 #define o_forkjoin() {if (!nogen) o_forkjoin();}
47 #define MAXLOCALS (1 << 10)
48 #define MAXGLOBALS (1 << 10)
49 #define MAXARGS (1 << 5)
51 #define ALIGN(x, a) (((x) + (a) - 1) & ~((a) - 1))
52 #define MIN(a, b) ((a) < (b) ? (a) : (b))
54 #define TYPE_BT(t) ((t)->ptr ? LONGSZ : (t)->bt)
55 #define TYPE_SZ(t) ((t)->ptr ? LONGSZ : (t)->bt & BT_SZMASK)
56 #define TYPE_VOID(t) (!(t)->bt && !(t)->flags && !(t)->ptr)
58 /* type->flag values */
63 /* variable definition flags */
71 int id
; /* for structs, functions and arrays */
72 int addr
; /* the address is passed to gen.c; deref for value */
76 static struct type ts
[MAXTMP
];
79 static void ts_push_bt(unsigned bt
)
87 static void ts_push(struct type
*t
)
89 struct type
*d
= &ts
[nts
++];
90 memcpy(d
, t
, sizeof(*t
));
93 static void ts_push_addr(struct type
*t
)
99 static void ts_pop(struct type
*type
)
106 void err(char *fmt
, ...)
111 vsprintf(msg
, fmt
, ap
);
113 die("%s: %s", cpp_loc(tok_addr()), msg
);
118 char elfname
[NAMELEN
]; /* local elf name for static variables in function */
120 long addr
; /* local stack offset, global data addr, struct offset */
123 static struct name locals
[MAXLOCALS
];
125 static struct name globals
[MAXGLOBALS
];
128 static void local_add(struct name
*name
)
130 if (nlocals
>= MAXLOCALS
)
131 err("nomem: MAXLOCALS reached!\n");
132 memcpy(&locals
[nlocals
++], name
, sizeof(*name
));
135 static int local_find(char *name
)
138 for (i
= nlocals
- 1; i
>= 0; --i
)
139 if (!strcmp(locals
[i
].name
, name
))
144 static int global_find(char *name
)
147 for (i
= nglobals
- 1; i
>= 0; i
--)
148 if (!strcmp(name
, globals
[i
].name
))
153 static void global_add(struct name
*name
)
155 if (nglobals
>= MAXGLOBALS
)
156 err("nomem: MAXGLOBALS reached!\n");
157 memcpy(&globals
[nglobals
++], name
, sizeof(*name
));
160 #define LABEL() (++label)
162 static int label
; /* last used label id */
163 static int l_break
; /* current break label */
164 static int l_cont
; /* current continue label */
166 #define MAXENUMS (1 << 10)
168 static struct enumval
{
174 static void enum_add(char *name
, int val
)
176 struct enumval
*ev
= &enums
[nenums
++];
177 if (nenums
>= MAXENUMS
)
178 err("nomem: MAXENUMS reached!\n");
179 strcpy(ev
->name
, name
);
183 static int enum_find(int *val
, char *name
)
186 for (i
= nenums
- 1; i
>= 0; --i
)
187 if (!strcmp(name
, enums
[i
].name
)) {
194 #define MAXTYPEDEFS (1 << 10)
196 static struct typdefinfo
{
199 } typedefs
[MAXTYPEDEFS
];
200 static int ntypedefs
;
202 static void typedef_add(char *name
, struct type
*type
)
204 struct typdefinfo
*ti
= &typedefs
[ntypedefs
++];
205 if (ntypedefs
>= MAXTYPEDEFS
)
206 err("nomem: MAXTYPEDEFS reached!\n");
207 strcpy(ti
->name
, name
);
208 memcpy(&ti
->type
, type
, sizeof(*type
));
211 static int typedef_find(char *name
)
214 for (i
= ntypedefs
- 1; i
>= 0; --i
)
215 if (!strcmp(name
, typedefs
[i
].name
))
220 #define MAXARRAYS (1 << 10)
222 static struct array
{
228 static int array_add(struct type
*type
, int n
)
230 struct array
*a
= &arrays
[narrays
++];
231 if (narrays
>= MAXARRAYS
)
232 err("nomem: MAXARRAYS reached!\n");
233 memcpy(&a
->type
, type
, sizeof(*type
));
238 static void array2ptr(struct type
*t
)
240 if (t
->flags
& T_ARRAY
&& !t
->ptr
) {
241 memcpy(t
, &arrays
[t
->id
].type
, sizeof(*t
));
246 #define MAXSTRUCTS (1 << 10)
247 #define MAXFIELDS (1 << 7)
249 static struct structinfo
{
251 struct name fields
[MAXFIELDS
];
255 } structs
[MAXSTRUCTS
];
258 static int struct_find(char *name
, int isunion
)
261 for (i
= nstructs
- 1; i
>= 0; --i
)
262 if (*structs
[i
].name
&& !strcmp(name
, structs
[i
].name
) &&
263 structs
[i
].isunion
== isunion
)
266 if (nstructs
>= MAXSTRUCTS
)
267 err("nomem: MAXTYPES reached!\n");
268 memset(&structs
[i
], 0, sizeof(structs
[i
]));
269 strcpy(structs
[i
].name
, name
);
270 structs
[i
].isunion
= isunion
;
274 static struct name
*struct_field(int id
, char *name
)
276 struct structinfo
*si
= &structs
[id
];
278 for (i
= 0; i
< si
->nfields
; i
++)
279 if (!strcmp(name
, si
->fields
[i
].name
))
280 return &si
->fields
[i
];
281 err("field not found\n");
285 /* return t's size */
286 static int type_totsz(struct type
*t
)
290 if (t
->flags
& T_ARRAY
)
291 return arrays
[t
->id
].n
* type_totsz(&arrays
[t
->id
].type
);
292 return t
->flags
& T_STRUCT
? structs
[t
->id
].size
: BT_SZ(t
->bt
);
295 /* return t's dereferenced size */
296 static unsigned type_szde(struct type
*t
)
301 return type_totsz(&de
);
304 /* dereference stack top if t->addr (ie. address is pushed to gen.c) */
305 static void ts_de(int deref
)
307 struct type
*t
= &ts
[nts
- 1];
309 if (deref
&& t
->addr
&& (t
->ptr
|| !(t
->flags
& T_FUNC
)))
314 /* pop stack pop to *t and dereference if t->addr */
315 static void ts_pop_de(struct type
*t
)
321 /* pop the top 2 stack values and dereference them if t->addr */
322 static void ts_pop_de2(struct type
*t1
, struct type
*t2
)
330 static int tok_jmp(int tok
)
332 if (tok_see() != tok
)
338 static void tok_expect(int tok
)
340 if (tok_get() != tok
)
341 err("syntax error\n");
344 static unsigned bt_op(unsigned bt1
, unsigned bt2
)
346 unsigned s1
= BT_SZ(bt1
);
347 unsigned s2
= BT_SZ(bt2
);
348 return ((bt1
| bt2
) & BT_SIGNED
) | (s1
> s2
? s1
: s2
);
351 static void ts_binop(int op
)
355 ts_pop_de2(&t1
, &t2
);
356 if (op
== O_DIV
|| op
== O_MOD
)
359 bt
= bt_op(TYPE_BT(&t1
), TYPE_BT(&t2
));
360 o_bop(op
| (bt
& BT_SIGNED
? O_SIGNED
: 0));
364 static void ts_addop(int op
)
367 ts_pop_de2(&t1
, &t2
);
368 if (!t1
.ptr
&& !t2
.ptr
) {
370 ts_push_bt(bt_op(TYPE_BT(&t1
), TYPE_BT(&t2
)));
373 if (t1
.ptr
&& !t2
.ptr
)
375 if (!t1
.ptr
&& t2
.ptr
)
376 if (type_szde(&t2
) > 1) {
377 o_num(type_szde(&t2
));
380 if (t1
.ptr
&& !t2
.ptr
)
383 if (t1
.ptr
&& t2
.ptr
) {
384 int sz
= type_szde(&t1
);
389 ts_push_bt(4 | BT_SIGNED
);
391 ts_push(t1
.ptr
? &t1
: &t2
);
395 /* function prototypes for parsing function and variable declarations */
396 static int readname(struct type
*main
, char *name
, struct type
*base
);
397 static int readtype(struct type
*type
);
398 static int readdefs(void (*def
)(void *data
, struct name
*name
, unsigned flags
),
400 static int readdefs_int(void (*def
)(void *data
, struct name
*name
, unsigned flags
),
403 /* function prototypes for parsing initializer expressions */
404 static int initsize(void);
405 static void initexpr(struct type
*t
, int off
, void *obj
,
406 void (*set
)(void *obj
, int off
, struct type
*t
));
408 static int type_alignment(struct type
*t
)
410 if (t
->flags
& T_ARRAY
&& !t
->ptr
)
411 return type_alignment(&arrays
[t
->id
].type
);
412 if (t
->flags
& T_STRUCT
&& !t
->ptr
)
413 return type_alignment(&structs
[t
->id
].fields
[0].type
);
414 return MIN(LONGSZ
, type_totsz(t
));
417 static void structdef(void *data
, struct name
*name
, unsigned flags
)
419 struct structinfo
*si
= data
;
422 if (si
->size
< type_totsz(&name
->type
))
423 si
->size
= type_totsz(&name
->type
);
425 struct type
*t
= &name
->type
;
426 int alignment
= type_alignment(t
);
427 if (t
->flags
& T_ARRAY
&& !t
->ptr
)
428 alignment
= MIN(LONGSZ
, type_totsz(&arrays
[t
->id
].type
));
429 si
->size
= ALIGN(si
->size
, alignment
);
430 name
->addr
= si
->size
;
431 si
->size
+= type_totsz(&name
->type
);
433 memcpy(&si
->fields
[si
->nfields
++], name
, sizeof(*name
));
436 static int struct_create(char *name
, int isunion
)
438 int id
= struct_find(name
, isunion
);
439 struct structinfo
*si
= &structs
[id
];
441 while (tok_jmp('}')) {
442 readdefs(structdef
, si
);
448 static void readexpr(void);
450 static void enum_create(void)
454 while (tok_jmp('}')) {
456 tok_expect(TOK_NAME
);
457 strcpy(name
, tok_id());
462 err("const expr expected!\n");
469 /* used to differenciate labels from case and cond exprs */
473 static void readpre(void);
475 static char *tmp_str(char *buf
, int len
)
477 static char name
[NAMELEN
];
480 sprintf(name
, "__neatcc.s%d", id
++);
481 dat
= o_mkdat(name
, len
, 0);
482 memcpy(dat
, buf
, len
);
486 static void readprimary(void)
488 if (!tok_jmp(TOK_NUM
)) {
490 int bt
= tok_num(&n
);
495 if (!tok_jmp(TOK_STR
)) {
496 struct type t
= {}; /* char type inside the arrays */
497 struct type a
= {}; /* the char array type */
499 int len
= tok_str(buf
);
500 t
.bt
= 1 | BT_SIGNED
;
501 a
.id
= array_add(&t
, len
);
504 o_sym(tmp_str(buf
, len
));
507 if (!tok_jmp(TOK_NAME
)) {
508 struct name unkn
= {""};
509 char *name
= unkn
.name
;
511 strcpy(name
, tok_id());
512 /* don't search for labels here */
513 if (!ncexpr
&& !caseexpr
&& tok_see() == ':')
515 if ((n
= local_find(name
)) != -1) {
516 struct name
*l
= &locals
[n
];
518 ts_push_addr(&l
->type
);
521 if ((n
= global_find(name
)) != -1) {
522 struct name
*g
= &globals
[n
];
523 o_sym(*g
->elfname
? g
->elfname
: g
->name
);
524 ts_push_addr(&g
->type
);
527 if (!enum_find(&n
, name
)) {
528 ts_push_bt(4 | BT_SIGNED
);
532 if (tok_see() != '(')
533 err("unknown symbol <%s>\n", name
);
547 if (!t
.ptr
|| !o
.ptr
)
551 while (tok_jmp(')')) {
562 static void arrayderef(void)
568 if (!(t
.flags
& T_ARRAY
&& !t
.ptr
) && t
.addr
) {
570 o_deref(TYPE_BT(&t
));
585 static void inc_post(int op
)
587 struct type t
= ts
[nts
- 1];
588 /* pushing the value before inc */
594 /* increment by 1 or pointer size */
598 o_num(t
.ptr
> 0 ? type_szde(&t
) : 1);
602 o_assign(TYPE_BT(&t
));
606 static void readfield(void)
610 tok_expect(TOK_NAME
);
613 field
= struct_field(t
.id
, tok_id());
618 ts_push_addr(&field
->type
);
621 #define MAXFUNCS (1 << 10)
623 static struct funcinfo
{
624 struct type args
[MAXARGS
];
628 /* function and argument names; useful only when defining */
629 char argnames
[MAXARGS
][NAMELEN
];
634 static int func_create(struct type
*ret
, char *name
, char argnames
[][NAMELEN
],
635 struct type
*args
, int nargs
, int varg
)
637 struct funcinfo
*fi
= &funcs
[nfuncs
++];
639 if (nfuncs
>= MAXFUNCS
)
640 err("nomem: MAXFUNCS reached!\n");
641 memcpy(&fi
->ret
, ret
, sizeof(*ret
));
642 for (i
= 0; i
< nargs
; i
++)
643 memcpy(&fi
->args
[i
], &args
[i
], sizeof(*ret
));
646 strcpy(fi
->name
, name
? name
: "");
647 for (i
= 0; i
< nargs
; i
++)
648 strcpy(fi
->argnames
[i
], argnames
[i
]);
652 static void readcall(void)
658 if (t
.flags
& T_FUNC
&& t
.ptr
> 0)
660 fi
= t
.flags
& T_FUNC
? &funcs
[t
.id
] : NULL
;
661 if (tok_see() != ')') {
666 } while (!tok_jmp(','));
669 o_call(argc
, fi
? TYPE_BT(&fi
->ret
) : 4 | BT_SIGNED
);
671 if (TYPE_BT(&fi
->ret
))
672 o_cast(TYPE_BT(&fi
->ret
));
675 ts_push_bt(4 | BT_SIGNED
);
679 static void readpost(void)
693 if (!tok_jmp(TOK2("++"))) {
697 if (!tok_jmp(TOK2("--"))) {
705 if (!tok_jmp(TOK2("->"))) {
714 static void inc_pre(int op
)
718 /* copy the destination */
720 ts_push(&ts
[nts
- 1]);
721 /* increment by 1 or pointer size */
723 o_num(t
.ptr
> 0 ? type_szde(&t
) : 1);
725 /* assign the result */
726 o_assign(TYPE_BT(&t
));
730 static void readpre(void)
737 err("cannot use the address\n");
749 err("dereferencing non-pointer\n");
751 o_deref(TYPE_BT(&t
));
761 ts_push_bt(4 | BT_SIGNED
);
776 if (!tok_jmp(TOK2("++"))) {
780 if (!tok_jmp(TOK2("--"))) {
784 if (!tok_jmp(TOK_SIZEOF
)) {
786 int op
= !tok_jmp('(');
797 o_num(type_totsz(&t
));
805 static void readmul(void)
828 static void readadd(void)
846 static void shift(int op
)
850 ts_pop_de2(NULL
, &t
);
851 o_bop(op
| (BT_SIGNED
& TYPE_BT(&t
) ? O_SIGNED
: 0));
852 ts_push_bt(TYPE_BT(&t
));
855 static void readshift(void)
859 if (!tok_jmp(TOK2("<<"))) {
863 if (!tok_jmp(TOK2(">>"))) {
871 static void cmp(int op
)
876 ts_pop_de2(&t1
, &t2
);
877 bt
= bt_op(TYPE_BT(&t1
), TYPE_BT(&t2
));
878 o_bop(op
| (bt
& BT_SIGNED
? O_SIGNED
: 0));
879 ts_push_bt(4 | BT_SIGNED
);
882 static void readcmp(void)
894 if (!tok_jmp(TOK2("<="))) {
898 if (!tok_jmp(TOK2(">="))) {
906 static void eq(int op
)
909 ts_pop_de2(NULL
, NULL
);
911 ts_push_bt(4 | BT_SIGNED
);
914 static void readeq(void)
918 if (!tok_jmp(TOK2("=="))) {
922 if (!tok_jmp(TOK2("!="))) {
930 static void readbitand(void)
933 while (!tok_jmp('&')) {
939 static void readxor(void)
942 while (!tok_jmp('^')) {
948 static void readbitor(void)
951 while (!tok_jmp('|')) {
957 #define MAXCOND (1 << 7)
959 static void readand(void)
962 int l_fail
= LABEL();
964 if (tok_see() != TOK2("&&"))
969 while (!tok_jmp(TOK2("&&"))) {
982 ts_push_bt(4 | BT_SIGNED
);
985 static void reador(void)
987 int l_pass
= LABEL();
990 if (tok_see() != TOK2("||"))
995 while (!tok_jmp(TOK2("||"))) {
1008 ts_push_bt(4 | BT_SIGNED
);
1011 static void readcexpr(void);
1013 static int readcexpr_const(void)
1021 /* both branches yield the same type; so ignore the first */
1029 /* making sure t->addr == 0 on both branches */
1036 static void readcexpr(void)
1044 if (readcexpr_const()) {
1045 int l_fail
= LABEL();
1046 int l_end
= LABEL();
1050 /* both branches yield the same type; so ignore the first */
1052 if (!TYPE_VOID(&ret
))
1059 /* making sure t->addr == 0 on both branches */
1061 if (!TYPE_VOID(&ret
)) {
1070 static void opassign(int op
, int ptrop
)
1072 struct type t
= ts
[nts
- 1];
1077 o_assign(TYPE_BT(&ts
[nts
- 2]));
1082 static void doassign(void)
1084 struct type t
= ts
[nts
- 1];
1085 if (!t
.ptr
&& t
.flags
& T_STRUCT
) {
1087 o_num(type_totsz(&t
));
1091 o_assign(TYPE_BT(&ts
[nts
- 1]));
1096 static void readexpr(void)
1099 if (!tok_jmp('=')) {
1104 if (!tok_jmp(TOK2("+="))) {
1108 if (!tok_jmp(TOK2("-="))) {
1112 if (!tok_jmp(TOK2("*="))) {
1116 if (!tok_jmp(TOK2("/="))) {
1120 if (!tok_jmp(TOK2("%="))) {
1124 if (!tok_jmp(TOK3("<<="))) {
1128 if (!tok_jmp(TOK3(">>="))) {
1132 if (!tok_jmp(TOK3("&="))) {
1136 if (!tok_jmp(TOK3("|="))) {
1140 if (!tok_jmp(TOK3("^="))) {
1146 static void readestmt(void)
1152 } while (!tok_jmp(','));
1155 #define F_GLOBAL(flags) (!((flags) & F_STATIC))
1157 static void globalinit(void *obj
, int off
, struct type
*t
)
1159 struct name
*name
= obj
;
1160 char *elfname
= *name
->elfname
? name
->elfname
: name
->name
;
1161 if (t
->flags
& T_ARRAY
&& tok_see() == TOK_STR
) {
1162 struct type
*t_de
= &arrays
[t
->id
].type
;
1163 if (!t_de
->ptr
&& !t_de
->flags
&& TYPE_SZ(t_de
) == 1) {
1166 tok_expect(TOK_STR
);
1168 memcpy((void *) name
->addr
+ off
, buf
, len
);
1173 o_datset(elfname
, off
, TYPE_BT(t
));
1177 static void readfunc(struct name
*name
, int flags
);
1179 static void globaldef(void *data
, struct name
*name
, unsigned flags
)
1181 struct type
*t
= &name
->type
;
1182 char *elfname
= *name
->elfname
? name
->elfname
: name
->name
;
1184 if (t
->flags
& T_ARRAY
&& !t
->ptr
&& !arrays
[t
->id
].n
)
1185 if (~flags
& F_EXTERN
)
1186 arrays
[t
->id
].n
= initsize();
1188 if (!(flags
& F_EXTERN
) && (!(t
->flags
& T_FUNC
) || t
->ptr
)) {
1189 if (tok_see() == '=')
1190 name
->addr
= (long) o_mkdat(elfname
, sz
, F_GLOBAL(flags
));
1192 o_mkbss(elfname
, sz
, F_GLOBAL(flags
));
1196 initexpr(t
, 0, name
, globalinit
);
1197 if (tok_see() == '{' && name
->type
.flags
& T_FUNC
)
1198 readfunc(name
, flags
);
1201 /* generate the address of local + off */
1202 static void o_localoff(long addr
, int off
)
1211 static void localinit(void *obj
, int off
, struct type
*t
)
1213 long addr
= *(long *) obj
;
1214 if (t
->flags
& T_ARRAY
&& tok_see() == TOK_STR
) {
1215 struct type
*t_de
= &arrays
[t
->id
].type
;
1216 if (!t_de
->ptr
&& !t_de
->flags
&& TYPE_SZ(t_de
) == 1) {
1219 tok_expect(TOK_STR
);
1221 o_localoff(addr
, off
);
1222 o_sym(tmp_str(buf
, len
));
1229 o_localoff(addr
, off
);
1237 /* current function name */
1238 static char func_name
[NAMELEN
];
1240 static void localdef(void *data
, struct name
*name
, unsigned flags
)
1242 struct type
*t
= &name
->type
;
1243 if ((flags
& F_EXTERN
) || ((t
->flags
& T_FUNC
) && !t
->ptr
)) {
1247 if (flags
& F_STATIC
) {
1248 sprintf(name
->elfname
, "__neatcc.%s.%s", func_name
, name
->name
);
1249 globaldef(data
, name
, flags
);
1252 if (t
->flags
& T_ARRAY
&& !t
->ptr
&& !arrays
[t
->id
].n
)
1253 arrays
[t
->id
].n
= initsize();
1254 name
->addr
= o_mklocal(type_totsz(&name
->type
));
1256 if (!tok_jmp('=')) {
1257 if (t
->flags
& (T_ARRAY
| T_STRUCT
) && !t
->ptr
) {
1258 o_local(name
->addr
);
1260 o_num(type_totsz(t
));
1264 initexpr(t
, 0, &name
->addr
, localinit
);
1268 static void typedefdef(void *data
, struct name
*name
, unsigned flags
)
1270 typedef_add(name
->name
, &name
->type
);
1273 static void readstmt(void);
1275 #define MAXCASES (1 << 7)
1277 static void readswitch(void)
1279 int o_break
= l_break
;
1280 long val_addr
= o_mklocal(LONGSZ
);
1282 int ncases
= 0; /* number of case labels */
1283 int l_failed
= LABEL(); /* address of last failed jmp */
1284 int l_matched
= LABEL(); /* address of last walk through jmp */
1285 int l_default
= 0; /* default case label */
1292 o_assign(TYPE_BT(&t
));
1297 while (tok_jmp('}')) {
1298 if (tok_see() != TOK_CASE
&& tok_see() != TOK_DEFAULT
) {
1304 if (tok_get() == TOK_CASE
) {
1312 o_deref(TYPE_BT(&t
));
1319 l_default
= LABEL();
1324 l_matched
= LABEL();
1327 o_rmlocal(val_addr
, LONGSZ
);
1336 #define MAXLABELS (1 << 10)
1338 static char label_name
[MAXLABELS
][NAMELEN
];
1339 static int label_ids
[MAXLABELS
];
1342 static int label_id(char *name
)
1345 for (i
= nlabels
- 1; i
>= 0; --i
)
1346 if (!strcmp(label_name
[i
], name
))
1347 return label_ids
[i
];
1348 strcpy(label_name
[nlabels
], name
);
1349 label_ids
[nlabels
] = LABEL();
1350 return label_ids
[nlabels
++];
1353 static void readstmt(void)
1357 if (!tok_jmp('{')) {
1358 int _nlocals
= nlocals
;
1359 int _nglobals
= nglobals
;
1360 int _nenums
= nenums
;
1361 int _ntypedefs
= ntypedefs
;
1362 int _nstructs
= nstructs
;
1363 int _nfuncs
= nfuncs
;
1364 int _narrays
= narrays
;
1365 while (tok_jmp('}'))
1369 ntypedefs
= _ntypedefs
;
1370 nstructs
= _nstructs
;
1373 nglobals
= _nglobals
;
1376 if (!readdefs(localdef
, NULL
)) {
1380 if (!tok_jmp(TOK_TYPEDEF
)) {
1381 readdefs(typedefdef
, NULL
);
1385 if (!tok_jmp(TOK_IF
)) {
1386 int l_fail
= LABEL();
1387 int l_end
= LABEL();
1394 if (!tok_jmp(TOK_ELSE
)) {
1404 if (!tok_jmp(TOK_WHILE
)) {
1405 int o_break
= l_break
;
1406 int o_cont
= l_cont
;
1422 if (!tok_jmp(TOK_DO
)) {
1423 int o_break
= l_break
;
1424 int o_cont
= l_cont
;
1425 int l_beg
= LABEL();
1430 tok_expect(TOK_WHILE
);
1443 if (!tok_jmp(TOK_FOR
)) {
1444 int o_break
= l_break
;
1445 int o_cont
= l_cont
;
1446 int l_check
= LABEL(); /* for condition label */
1447 int l_body
= LABEL(); /* for block label */
1451 if (tok_see() != ';')
1455 if (tok_see() != ';') {
1463 if (tok_see() != ')')
1475 if (!tok_jmp(TOK_SWITCH
)) {
1479 if (!tok_jmp(TOK_RETURN
)) {
1480 int ret
= tok_see() != ';';
1489 if (!tok_jmp(TOK_BREAK
)) {
1494 if (!tok_jmp(TOK_CONTINUE
)) {
1499 if (!tok_jmp(TOK_GOTO
)) {
1500 tok_expect(TOK_NAME
);
1501 o_jmp(label_id(tok_id()));
1507 if (!tok_jmp(':')) {
1508 o_label(label_id(tok_id()));
1514 static void readfunc(struct name
*name
, int flags
)
1516 struct funcinfo
*fi
= &funcs
[name
->type
.id
];
1517 long beg
= tok_addr();
1519 strcpy(func_name
, fi
->name
);
1520 o_func_beg(func_name
, fi
->nargs
, F_GLOBAL(flags
), fi
->varg
);
1521 for (i
= 0; i
< fi
->nargs
; i
++) {
1522 struct name arg
= {"", "", fi
->args
[i
], o_arg2loc(i
)};
1523 strcpy(arg
.name
, fi
->argnames
[i
]);
1526 /* first pass: collecting statistics */
1530 /* second pass: generating code */
1534 func_name
[0] = '\0';
1540 static void readdecl(void)
1542 if (!tok_jmp(TOK_TYPEDEF
)) {
1543 readdefs(typedefdef
, NULL
);
1547 readdefs_int(globaldef
, NULL
);
1551 static void parse(void)
1553 while (tok_see() != TOK_EOF
)
1557 static void compat_macros(void)
1559 cpp_define("__STDC__", "");
1560 cpp_define("__linux__", "");
1561 cpp_define(I_ARCH
, "");
1563 /* ignored keywords */
1564 cpp_define("const", "");
1565 cpp_define("register", "");
1566 cpp_define("volatile", "");
1567 cpp_define("inline", "");
1568 cpp_define("restrict", "");
1569 cpp_define("__inline__", "");
1570 cpp_define("__restrict__", "");
1571 cpp_define("__attribute__(x)", "");
1572 cpp_define("__builtin_va_list__", "long");
1575 int main(int argc
, char *argv
[])
1581 while (i
< argc
&& argv
[i
][0] == '-') {
1582 if (argv
[i
][1] == 'I')
1583 cpp_addpath(argv
[i
][2] ? argv
[i
] + 2 : argv
[++i
]);
1584 if (argv
[i
][1] == 'D') {
1585 char *name
= argv
[i
] + 2;
1587 char *eq
= strchr(name
, '=');
1592 cpp_define(name
, def
);
1594 if (argv
[i
][1] == 'o')
1595 strcpy(obj
, argv
[i
][2] ? argv
[i
] + 2 : argv
[++i
]);
1599 die("neatcc: no file given\n");
1600 if (cpp_init(argv
[i
]))
1601 die("neatcc: cannot open <%s>\n", argv
[i
]);
1604 strcpy(obj
, argv
[i
]);
1605 obj
[strlen(obj
) - 1] = 'o';
1607 ofd
= open(obj
, O_WRONLY
| O_TRUNC
| O_CREAT
, 0600);
1614 /* parsing function and variable declarations */
1616 /* read the base type of a variable */
1617 static int basetype(struct type
*type
, unsigned *flags
)
1624 char name
[NAMELEN
] = "";
1630 switch (tok_see()) {
1662 isunion
= tok_get() == TOK_UNION
;
1663 if (!tok_jmp(TOK_NAME
))
1664 strcpy(name
, tok_id());
1665 if (tok_see() == '{')
1666 type
->id
= struct_create(name
, isunion
);
1668 type
->id
= struct_find(name
, isunion
);
1669 type
->flags
|= T_STRUCT
;
1675 if (tok_see() == '{')
1677 type
->bt
= 4 | BT_SIGNED
;
1680 if (tok_see() == TOK_NAME
) {
1681 int id
= typedef_find(tok_id());
1684 memcpy(type
, &typedefs
[id
].type
,
1697 type
->bt
= size
| (sign
? BT_SIGNED
: 0);
1701 static void readptrs(struct type
*type
)
1703 while (!tok_jmp('*')) {
1710 /* read function arguments */
1711 static int readargs(struct type
*args
, char argnames
[][NAMELEN
], int *varg
)
1716 while (tok_see() != ')') {
1717 if (!tok_jmp(TOK3("..."))) {
1721 if (readname(&args
[nargs
], argnames
[nargs
], NULL
)) {
1722 /* argument has no type, assume int */
1723 tok_expect(TOK_NAME
);
1724 memset(&args
[nargs
], 0, sizeof(struct type
));
1725 args
[nargs
].bt
= 4 | BT_SIGNED
;
1726 strcpy(argnames
[nargs
], tok_id());
1728 /* argument arrays are pointers */
1729 array2ptr(&args
[nargs
]);
1736 if (nargs
== 1 && !TYPE_BT(&args
[0]))
1741 /* read K&R function arguments */
1742 static void krdef(void *data
, struct name
*name
, unsigned flags
)
1744 struct funcinfo
*fi
= data
;
1746 for (i
= 0; i
< fi
->nargs
; i
++)
1747 if (!strcmp(fi
->argnames
[i
], name
->name
))
1748 memcpy(&fi
->args
[i
], &name
->type
, sizeof(name
->type
));
1752 * readarrays() parses array specifiers when reading a definition in
1753 * readname(). The "type" parameter contains the type contained in the
1754 * inner array; for instance, type in "int *a[10][20]" would be an int
1755 * pointer. When returning, the "type" parameter is changed to point
1756 * to the final array. The function returns a pointer to the type in
1757 * the inner array; this is useful when the type is not complete yet,
1758 * like when creating an array of function pointers as in
1759 * "int (*f[10])(int)". If there is no array brackets, NULL is returned.
1761 static struct type
*readarrays(struct type
*type
)
1764 struct type
*inner
= NULL
;
1767 while (!tok_jmp('[')) {
1773 err("const expr expected\n");
1778 for (i
= nar
- 1; i
>= 0; i
--) {
1779 type
->id
= array_add(type
, arsz
[i
]);
1781 inner
= &arrays
[type
->id
].type
;
1782 type
->flags
= T_ARRAY
;
1790 * readname() reads a variable definition; the name is copied into
1791 * "name" and the type is copied into "main" argument. The "base"
1792 * argument, if not NULL, indicates the base type of the variable.
1793 * For instance, the base type of "a" and "b" in "int *a, b[10]" is
1794 * "int". If NULL, basetype() is called directly to read the base
1795 * type of the variable. readname() returns zero, only if the
1796 * variable can be read.
1798 static int readname(struct type
*main
, char *name
, struct type
*base
)
1800 struct type tpool
[3];
1802 struct type
*type
= &tpool
[npool
++];
1803 struct type
*ptype
= NULL
; /* type inside parenthesis */
1804 struct type
*btype
= NULL
; /* type before parenthesis */
1807 memset(tpool
, 0, sizeof(tpool
));
1811 if (basetype(type
, &flags
))
1814 memcpy(type
, base
, sizeof(*base
));
1817 if (!tok_jmp('(')) {
1819 type
= &tpool
[npool
++];
1823 if (!tok_jmp(TOK_NAME
) && name
)
1824 strcpy(name
, tok_id());
1825 inner
= readarrays(type
);
1830 if (tok_see() == '(') {
1831 struct type args
[MAXARGS
];
1832 char argnames
[MAXARGS
][NAMELEN
];
1834 int nargs
= readargs(args
, argnames
, &varg
);
1837 type
= &tpool
[npool
++];
1840 ptype
->flags
= T_FUNC
;
1842 ptype
->id
= func_create(btype
, name
, argnames
, args
, nargs
, varg
);
1843 if (tok_see() != ';')
1844 while (tok_see() != '{' && !readdefs(krdef
, &funcs
[ptype
->id
]))
1847 if (ptype
&& readarrays(type
))
1850 memcpy(main
, type
, sizeof(*type
));
1854 static int readtype(struct type
*type
)
1856 return readname(type
, NULL
, NULL
);
1860 * readdef() reads a variable definitions statement. The definition
1861 * statement can appear in anywhere: global variables, function
1862 * local variables, struct fields, and typedefs. For each defined
1863 * variable, def() callback is called with the appropriate name
1864 * struct and flags; the callback should finish parsing the definition
1865 * by possibly reading the initializer expression and saving the name
1868 static int readdefs(void (*def
)(void *data
, struct name
*name
, unsigned flags
),
1872 unsigned base_flags
;
1873 if (basetype(&base
, &base_flags
))
1875 if (tok_see() == ';' || tok_see() == '{')
1878 struct name name
= {{""}};
1879 if (readname(&name
.type
, name
.name
, &base
))
1881 def(data
, &name
, base_flags
);
1882 } while (!tok_jmp(','));
1886 /* just like readdefs, but default to int type; for handling K&R functions */
1887 static int readdefs_int(void (*def
)(void *data
, struct name
*name
, unsigned flags
),
1892 if (basetype(&base
, &flags
)) {
1893 if (tok_see() != TOK_NAME
)
1895 memset(&base
, 0, sizeof(base
));
1896 base
.bt
= 4 | BT_SIGNED
;
1898 if (tok_see() != ';') {
1900 struct name name
= {{""}};
1901 if (readname(&name
.type
, name
.name
, &base
))
1903 def(data
, &name
, flags
);
1904 } while (!tok_jmp(','));
1910 /* parsing initializer expressions */
1912 static void jumpbrace(void)
1915 while (tok_see() != '}' || depth
--)
1916 if (tok_get() == '{')
1921 /* compute the size of the initializer expression */
1922 static int initsize(void)
1924 long addr
= tok_addr();
1928 if (!tok_jmp(TOK_STR
)) {
1934 while (tok_jmp('}')) {
1936 if (!tok_jmp('[')) {
1945 while (tok_see() != '}' && tok_see() != ',')
1946 if (tok_get() == '{')
1954 static struct type
*innertype(struct type
*t
)
1956 if (t
->flags
& T_ARRAY
&& !t
->ptr
)
1957 return innertype(&arrays
[t
->id
].type
);
1961 /* read the initializer expression and initialize basic types using set() cb */
1962 static void initexpr(struct type
*t
, int off
, void *obj
,
1963 void (*set
)(void *obj
, int off
, struct type
*t
))
1969 if (!t
->ptr
&& t
->flags
& T_STRUCT
) {
1970 struct structinfo
*si
= &structs
[t
->id
];
1972 for (i
= 0; i
< si
->nfields
&& tok_see() != '}'; i
++) {
1973 struct name
*field
= &si
->fields
[i
];
1974 if (!tok_jmp('.')) {
1975 tok_expect(TOK_NAME
);
1976 field
= struct_field(t
->id
, tok_id());
1979 initexpr(&field
->type
, off
+ field
->addr
, obj
, set
);
1983 } else if (t
->flags
& T_ARRAY
) {
1984 struct type
*t_de
= &arrays
[t
->id
].type
;
1986 /* handling extra braces as in: char s[] = {"sth"} */
1987 if (TYPE_SZ(t_de
) == 1 && tok_see() == TOK_STR
) {
1992 for (i
= 0; tok_see() != '}'; i
++) {
1994 struct type
*it
= t_de
;
1995 if (!tok_jmp('[')) {
2002 if (tok_see() != '{' && (tok_see() != TOK_STR
||
2003 !(it
->flags
& T_ARRAY
)))
2004 it
= innertype(t_de
);
2005 initexpr(it
, off
+ type_totsz(it
) * idx
, obj
, set
);