2 * neatcc - the neatcc compiler
4 * Copyright (C) 2010-2013 Ali Gholami Rudi
6 * This program is released under the Modified BSD license.
15 #include <sys/types.h>
21 static int nogen
; /* do not generate code, if set */
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 ALIGN(x, a) (((x) + (a) - 1) & ~((a) - 1))
48 #define MIN(a, b) ((a) < (b) ? (a) : (b))
50 #define TYPE_BT(t) ((t)->ptr ? LONGSZ : (t)->bt)
51 #define TYPE_SZ(t) ((t)->ptr ? LONGSZ : (t)->bt & BT_SZMASK)
52 #define TYPE_VOID(t) (!(t)->bt && !(t)->flags && !(t)->ptr)
54 /* type->flag values */
59 /* variable definition flags */
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
[NTMPS
];
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
)
102 void err(char *fmt
, ...)
107 vsprintf(msg
, fmt
, ap
);
109 die("%s: %s", cpp_loc(tok_addr()), msg
);
114 char elfname
[NAMELEN
]; /* local elf name for function static variables */
116 long addr
; /* local stack offset, global data addr, struct offset */
119 static struct name locals
[NLOCALS
];
121 static struct name globals
[NGLOBALS
];
124 static void local_add(struct name
*name
)
126 if (nlocals
>= NLOCALS
)
127 err("nomem: NLOCALS reached!\n");
128 memcpy(&locals
[nlocals
++], name
, sizeof(*name
));
131 static int local_find(char *name
)
134 for (i
= nlocals
- 1; i
>= 0; --i
)
135 if (!strcmp(locals
[i
].name
, name
))
140 static int global_find(char *name
)
143 for (i
= nglobals
- 1; i
>= 0; i
--)
144 if (!strcmp(name
, globals
[i
].name
))
149 static void global_add(struct name
*name
)
151 if (nglobals
>= NGLOBALS
)
152 err("nomem: NGLOBALS reached!\n");
153 memcpy(&globals
[nglobals
++], name
, sizeof(*name
));
156 #define LABEL() (++label)
158 static int label
; /* last used label id */
159 static int l_break
; /* current break label */
160 static int l_cont
; /* current continue label */
162 static struct enumval
{
168 static void enum_add(char *name
, int val
)
170 struct enumval
*ev
= &enums
[nenums
++];
171 if (nenums
>= NENUMS
)
172 err("nomem: NENUMS reached!\n");
173 strcpy(ev
->name
, name
);
177 static int enum_find(int *val
, char *name
)
180 for (i
= nenums
- 1; i
>= 0; --i
)
181 if (!strcmp(name
, enums
[i
].name
)) {
188 static struct typdefinfo
{
191 } typedefs
[NTYPEDEFS
];
192 static int ntypedefs
;
194 static void typedef_add(char *name
, struct type
*type
)
196 struct typdefinfo
*ti
= &typedefs
[ntypedefs
++];
197 if (ntypedefs
>= NTYPEDEFS
)
198 err("nomem: NTYPEDEFS reached!\n");
199 strcpy(ti
->name
, name
);
200 memcpy(&ti
->type
, type
, sizeof(*type
));
203 static int typedef_find(char *name
)
206 for (i
= ntypedefs
- 1; i
>= 0; --i
)
207 if (!strcmp(name
, typedefs
[i
].name
))
212 static struct array
{
218 static int array_add(struct type
*type
, int n
)
220 struct array
*a
= &arrays
[narrays
++];
221 if (narrays
>= NARRAYS
)
222 err("nomem: NARRAYS reached!\n");
223 memcpy(&a
->type
, type
, sizeof(*type
));
228 static void array2ptr(struct type
*t
)
230 if (t
->flags
& T_ARRAY
&& !t
->ptr
) {
231 memcpy(t
, &arrays
[t
->id
].type
, sizeof(*t
));
236 static struct structinfo
{
238 struct name fields
[NFIELDS
];
245 static int struct_find(char *name
, int isunion
)
248 for (i
= nstructs
- 1; i
>= 0; --i
)
249 if (*structs
[i
].name
&& !strcmp(name
, structs
[i
].name
) &&
250 structs
[i
].isunion
== isunion
)
253 if (nstructs
>= NSTRUCTS
)
254 err("nomem: NSTRUCTS reached!\n");
255 memset(&structs
[i
], 0, sizeof(structs
[i
]));
256 strcpy(structs
[i
].name
, name
);
257 structs
[i
].isunion
= isunion
;
261 static struct name
*struct_field(int id
, char *name
)
263 struct structinfo
*si
= &structs
[id
];
265 for (i
= 0; i
< si
->nfields
; i
++)
266 if (!strcmp(name
, si
->fields
[i
].name
))
267 return &si
->fields
[i
];
268 err("field not found\n");
272 /* return t's size */
273 static int type_totsz(struct type
*t
)
277 if (t
->flags
& T_ARRAY
)
278 return arrays
[t
->id
].n
* type_totsz(&arrays
[t
->id
].type
);
279 return t
->flags
& T_STRUCT
? structs
[t
->id
].size
: BT_SZ(t
->bt
);
282 /* return t's dereferenced size */
283 static unsigned type_szde(struct type
*t
)
288 return type_totsz(&de
);
291 /* dereference stack top if t->addr (ie. address is pushed to gen.c) */
292 static void ts_de(int deref
)
294 struct type
*t
= &ts
[nts
- 1];
296 if (deref
&& t
->addr
&& (t
->ptr
|| !(t
->flags
& T_FUNC
)))
301 /* pop stack pop to *t and dereference if t->addr */
302 static void ts_pop_de(struct type
*t
)
308 /* pop the top 2 stack values and dereference them if t->addr */
309 static void ts_pop_de2(struct type
*t1
, struct type
*t2
)
317 static int tok_jmp(int tok
)
319 if (tok_see() != tok
)
325 static void tok_expect(int tok
)
327 if (tok_get() != tok
)
328 err("syntax error\n");
331 static unsigned bt_op(unsigned bt1
, unsigned bt2
)
333 unsigned s1
= BT_SZ(bt1
);
334 unsigned s2
= BT_SZ(bt2
);
335 return ((bt1
| bt2
) & BT_SIGNED
) | (s1
> s2
? s1
: s2
);
338 static void ts_binop(int op
)
342 ts_pop_de2(&t1
, &t2
);
343 if (op
== O_DIV
|| op
== O_MOD
)
346 bt
= bt_op(TYPE_BT(&t1
), TYPE_BT(&t2
));
347 o_bop(op
| (bt
& BT_SIGNED
? O_SIGNED
: 0));
351 static void ts_addop(int op
)
354 ts_pop_de2(&t1
, &t2
);
355 if (!t1
.ptr
&& !t2
.ptr
) {
357 ts_push_bt(bt_op(TYPE_BT(&t1
), TYPE_BT(&t2
)));
360 if (t1
.ptr
&& !t2
.ptr
)
362 if (!t1
.ptr
&& t2
.ptr
)
363 if (type_szde(&t2
) > 1) {
364 o_num(type_szde(&t2
));
367 if (t1
.ptr
&& !t2
.ptr
)
370 if (t1
.ptr
&& t2
.ptr
) {
371 int sz
= type_szde(&t1
);
376 ts_push_bt(4 | BT_SIGNED
);
378 ts_push(t1
.ptr
? &t1
: &t2
);
382 /* function prototypes for parsing function and variable declarations */
383 static int readname(struct type
*main
, char *name
, struct type
*base
);
384 static int readtype(struct type
*type
);
385 static int readdefs(void (*def
)(void *data
, struct name
*name
, unsigned flags
),
387 static int readdefs_int(void (*def
)(void *data
, struct name
*name
, unsigned flags
),
390 /* function prototypes for parsing initializer expressions */
391 static int initsize(void);
392 static void initexpr(struct type
*t
, int off
, void *obj
,
393 void (*set
)(void *obj
, int off
, struct type
*t
));
395 static int type_alignment(struct type
*t
)
397 if (t
->flags
& T_ARRAY
&& !t
->ptr
)
398 return type_alignment(&arrays
[t
->id
].type
);
399 if (t
->flags
& T_STRUCT
&& !t
->ptr
)
400 return type_alignment(&structs
[t
->id
].fields
[0].type
);
401 return MIN(LONGSZ
, type_totsz(t
));
404 static void structdef(void *data
, struct name
*name
, unsigned flags
)
406 struct structinfo
*si
= data
;
409 if (si
->size
< type_totsz(&name
->type
))
410 si
->size
= type_totsz(&name
->type
);
412 struct type
*t
= &name
->type
;
413 int alignment
= type_alignment(t
);
414 if (t
->flags
& T_ARRAY
&& !t
->ptr
)
415 alignment
= MIN(LONGSZ
, type_totsz(&arrays
[t
->id
].type
));
416 si
->size
= ALIGN(si
->size
, alignment
);
417 name
->addr
= si
->size
;
418 si
->size
+= type_totsz(&name
->type
);
420 memcpy(&si
->fields
[si
->nfields
++], name
, sizeof(*name
));
423 static int struct_create(char *name
, int isunion
)
425 int id
= struct_find(name
, isunion
);
426 struct structinfo
*si
= &structs
[id
];
428 while (tok_jmp('}')) {
429 readdefs(structdef
, si
);
435 static void readexpr(void);
437 static void enum_create(void)
441 while (tok_jmp('}')) {
443 tok_expect(TOK_NAME
);
444 strcpy(name
, tok_id());
449 err("const expr expected!\n");
456 /* used to differenciate labels from case and cond exprs */
460 static void readpre(void);
462 static char *tmp_str(char *buf
, int len
)
464 static char name
[NAMELEN
];
466 sprintf(name
, "__neatcc.s%d", id
++);
467 o_dscpy(o_dsnew(name
, len
, 0), buf
, len
);
471 static void readprimary(void)
473 if (!tok_jmp(TOK_NUM
)) {
475 int bt
= tok_num(&n
);
480 if (!tok_jmp(TOK_STR
)) {
481 struct type t
= {}; /* char type inside the arrays */
482 struct type a
= {}; /* the char array type */
484 int len
= tok_str(buf
);
485 t
.bt
= 1 | BT_SIGNED
;
486 a
.id
= array_add(&t
, len
);
489 o_sym(tmp_str(buf
, len
));
492 if (!tok_jmp(TOK_NAME
)) {
493 struct name unkn
= {""};
494 char *name
= unkn
.name
;
496 strcpy(name
, tok_id());
497 /* don't search for labels here */
498 if (!ncexpr
&& !caseexpr
&& tok_see() == ':')
500 if ((n
= local_find(name
)) != -1) {
501 struct name
*l
= &locals
[n
];
503 ts_push_addr(&l
->type
);
506 if ((n
= global_find(name
)) != -1) {
507 struct name
*g
= &globals
[n
];
508 o_sym(*g
->elfname
? g
->elfname
: g
->name
);
509 ts_push_addr(&g
->type
);
512 if (!enum_find(&n
, name
)) {
513 ts_push_bt(4 | BT_SIGNED
);
517 if (tok_see() != '(')
518 err("unknown symbol <%s>\n", name
);
532 if (!t
.ptr
|| !o
.ptr
)
536 while (tok_jmp(')')) {
547 static void arrayderef(void)
553 if (!(t
.flags
& T_ARRAY
&& !t
.ptr
) && t
.addr
) {
555 o_deref(TYPE_BT(&t
));
570 static void inc_post(int op
)
572 struct type t
= ts
[nts
- 1];
573 /* pushing the value before inc */
579 /* increment by 1 or pointer size */
583 o_num(t
.ptr
> 0 ? type_szde(&t
) : 1);
587 o_assign(TYPE_BT(&t
));
591 static void readfield(void)
595 tok_expect(TOK_NAME
);
598 field
= struct_field(t
.id
, tok_id());
603 ts_push_addr(&field
->type
);
606 static struct funcinfo
{
607 struct type args
[NARGS
];
611 /* function and argument names; useful only when defining */
612 char argnames
[NARGS
][NAMELEN
];
617 static int func_create(struct type
*ret
, char *name
, char argnames
[][NAMELEN
],
618 struct type
*args
, int nargs
, int varg
)
620 struct funcinfo
*fi
= &funcs
[nfuncs
++];
622 if (nfuncs
>= NFUNCS
)
623 err("nomem: NFUNCS reached!\n");
624 memcpy(&fi
->ret
, ret
, sizeof(*ret
));
625 for (i
= 0; i
< nargs
; i
++)
626 memcpy(&fi
->args
[i
], &args
[i
], sizeof(*ret
));
629 strcpy(fi
->name
, name
? name
: "");
630 for (i
= 0; i
< nargs
; i
++)
631 strcpy(fi
->argnames
[i
], argnames
[i
]);
635 static void readcall(void)
641 if (t
.flags
& T_FUNC
&& t
.ptr
> 0)
643 fi
= t
.flags
& T_FUNC
? &funcs
[t
.id
] : NULL
;
644 if (tok_see() != ')') {
649 } while (!tok_jmp(','));
652 o_call(argc
, fi
? TYPE_BT(&fi
->ret
) : 4 | BT_SIGNED
);
654 if (TYPE_BT(&fi
->ret
))
655 o_cast(TYPE_BT(&fi
->ret
));
658 ts_push_bt(4 | BT_SIGNED
);
662 static void readpost(void)
676 if (!tok_jmp(TOK2("++"))) {
680 if (!tok_jmp(TOK2("--"))) {
688 if (!tok_jmp(TOK2("->"))) {
697 static void inc_pre(int op
)
701 /* copy the destination */
703 ts_push(&ts
[nts
- 1]);
704 /* increment by 1 or pointer size */
706 o_num(t
.ptr
> 0 ? type_szde(&t
) : 1);
708 /* assign the result */
709 o_assign(TYPE_BT(&t
));
713 static void readpre(void)
720 err("cannot use the address\n");
732 err("dereferencing non-pointer\n");
734 o_deref(TYPE_BT(&t
));
744 ts_push_bt(4 | BT_SIGNED
);
763 if (!tok_jmp(TOK2("++"))) {
767 if (!tok_jmp(TOK2("--"))) {
771 if (!tok_jmp(TOK_SIZEOF
)) {
773 int op
= !tok_jmp('(');
784 o_num(type_totsz(&t
));
792 static void readmul(void)
815 static void readadd(void)
833 static void shift(int op
)
837 ts_pop_de2(NULL
, &t
);
838 o_bop(op
| (BT_SIGNED
& TYPE_BT(&t
) ? O_SIGNED
: 0));
839 ts_push_bt(TYPE_BT(&t
));
842 static void readshift(void)
846 if (!tok_jmp(TOK2("<<"))) {
850 if (!tok_jmp(TOK2(">>"))) {
858 static void cmp(int op
)
863 ts_pop_de2(&t1
, &t2
);
864 bt
= bt_op(TYPE_BT(&t1
), TYPE_BT(&t2
));
865 o_bop(op
| (bt
& BT_SIGNED
? O_SIGNED
: 0));
866 ts_push_bt(4 | BT_SIGNED
);
869 static void readcmp(void)
881 if (!tok_jmp(TOK2("<="))) {
885 if (!tok_jmp(TOK2(">="))) {
893 static void eq(int op
)
896 ts_pop_de2(NULL
, NULL
);
898 ts_push_bt(4 | BT_SIGNED
);
901 static void readeq(void)
905 if (!tok_jmp(TOK2("=="))) {
909 if (!tok_jmp(TOK2("!="))) {
917 static void readbitand(void)
920 while (!tok_jmp('&')) {
926 static void readxor(void)
929 while (!tok_jmp('^')) {
935 static void readbitor(void)
938 while (!tok_jmp('|')) {
944 static void readand(void)
947 int l_fail
= LABEL();
949 if (tok_see() != TOK2("&&"))
954 while (!tok_jmp(TOK2("&&"))) {
967 ts_push_bt(4 | BT_SIGNED
);
970 static void reador(void)
972 int l_pass
= LABEL();
975 if (tok_see() != TOK2("||"))
980 while (!tok_jmp(TOK2("||"))) {
993 ts_push_bt(4 | BT_SIGNED
);
996 static void readcexpr(void);
998 static int readcexpr_const(void)
1006 /* both branches yield the same type; so ignore the first */
1014 /* making sure t->addr == 0 on both branches */
1021 static void readcexpr(void)
1029 if (readcexpr_const()) {
1030 int l_fail
= LABEL();
1031 int l_end
= LABEL();
1035 /* both branches yield the same type; so ignore the first */
1037 if (!TYPE_VOID(&ret
))
1044 /* making sure t->addr == 0 on both branches */
1046 if (!TYPE_VOID(&ret
)) {
1055 static void opassign(int op
, int ptrop
)
1057 struct type t
= ts
[nts
- 1];
1061 if (op
== O_ADD
|| op
== O_SUB
)
1065 o_assign(TYPE_BT(&ts
[nts
- 2]));
1070 static void doassign(void)
1072 struct type t
= ts
[nts
- 1];
1073 if (!t
.ptr
&& t
.flags
& T_STRUCT
) {
1075 o_num(type_totsz(&t
));
1079 o_assign(TYPE_BT(&ts
[nts
- 1]));
1084 static void readexpr(void)
1087 if (!tok_jmp('=')) {
1092 if (!tok_jmp(TOK2("+="))) {
1096 if (!tok_jmp(TOK2("-="))) {
1100 if (!tok_jmp(TOK2("*="))) {
1104 if (!tok_jmp(TOK2("/="))) {
1108 if (!tok_jmp(TOK2("%="))) {
1112 if (!tok_jmp(TOK3("<<="))) {
1116 if (!tok_jmp(TOK3(">>="))) {
1120 if (!tok_jmp(TOK3("&="))) {
1124 if (!tok_jmp(TOK3("|="))) {
1128 if (!tok_jmp(TOK3("^="))) {
1134 static void readestmt(void)
1140 } while (!tok_jmp(','));
1143 #define F_GLOBAL(flags) (!((flags) & F_STATIC))
1145 static void globalinit(void *obj
, int off
, struct type
*t
)
1147 struct name
*name
= obj
;
1148 char *elfname
= *name
->elfname
? name
->elfname
: name
->name
;
1149 if (t
->flags
& T_ARRAY
&& tok_see() == TOK_STR
) {
1150 struct type
*t_de
= &arrays
[t
->id
].type
;
1151 if (!t_de
->ptr
&& !t_de
->flags
&& TYPE_SZ(t_de
) == 1) {
1154 tok_expect(TOK_STR
);
1156 o_dscpy(name
->addr
+ off
, buf
, len
);
1161 o_dsset(elfname
, off
, TYPE_BT(t
));
1165 static void readfunc(struct name
*name
, int flags
);
1167 static void globaldef(void *data
, struct name
*name
, unsigned flags
)
1169 struct type
*t
= &name
->type
;
1170 char *elfname
= *name
->elfname
? name
->elfname
: name
->name
;
1172 if (t
->flags
& T_ARRAY
&& !t
->ptr
&& !arrays
[t
->id
].n
)
1173 if (~flags
& F_EXTERN
)
1174 arrays
[t
->id
].n
= initsize();
1176 if (!(flags
& F_EXTERN
) && (!(t
->flags
& T_FUNC
) || t
->ptr
)) {
1177 if (tok_see() == '=')
1178 name
->addr
= o_dsnew(elfname
, sz
, F_GLOBAL(flags
));
1180 o_bsnew(elfname
, sz
, F_GLOBAL(flags
));
1184 initexpr(t
, 0, name
, globalinit
);
1185 if (tok_see() == '{' && name
->type
.flags
& T_FUNC
)
1186 readfunc(name
, flags
);
1189 /* generate the address of local + off */
1190 static void o_localoff(long addr
, int off
)
1199 static void localinit(void *obj
, int off
, struct type
*t
)
1201 long addr
= *(long *) obj
;
1202 if (t
->flags
& T_ARRAY
&& tok_see() == TOK_STR
) {
1203 struct type
*t_de
= &arrays
[t
->id
].type
;
1204 if (!t_de
->ptr
&& !t_de
->flags
&& TYPE_SZ(t_de
) == 1) {
1207 tok_expect(TOK_STR
);
1209 o_localoff(addr
, off
);
1210 o_sym(tmp_str(buf
, len
));
1217 o_localoff(addr
, off
);
1225 /* current function name */
1226 static char func_name
[NAMELEN
];
1228 static void localdef(void *data
, struct name
*name
, unsigned flags
)
1230 struct type
*t
= &name
->type
;
1231 if ((flags
& F_EXTERN
) || ((t
->flags
& T_FUNC
) && !t
->ptr
)) {
1235 if (flags
& F_STATIC
) {
1236 sprintf(name
->elfname
, "__neatcc.%s.%s", func_name
, name
->name
);
1237 globaldef(data
, name
, flags
);
1240 if (t
->flags
& T_ARRAY
&& !t
->ptr
&& !arrays
[t
->id
].n
)
1241 arrays
[t
->id
].n
= initsize();
1242 name
->addr
= o_mklocal(type_totsz(&name
->type
));
1244 if (!tok_jmp('=')) {
1245 if (t
->flags
& (T_ARRAY
| T_STRUCT
) && !t
->ptr
) {
1246 o_local(name
->addr
);
1248 o_num(type_totsz(t
));
1252 initexpr(t
, 0, &name
->addr
, localinit
);
1256 static void typedefdef(void *data
, struct name
*name
, unsigned flags
)
1258 typedef_add(name
->name
, &name
->type
);
1261 static void readstmt(void);
1263 static void readswitch(void)
1265 int o_break
= l_break
;
1266 long val_addr
= o_mklocal(LONGSZ
);
1268 int ncases
= 0; /* number of case labels */
1269 int l_failed
= LABEL(); /* address of last failed jmp */
1270 int l_matched
= LABEL(); /* address of last walk through jmp */
1271 int l_default
= 0; /* default case label */
1278 o_assign(TYPE_BT(&t
));
1283 while (tok_jmp('}')) {
1284 if (tok_see() != TOK_CASE
&& tok_see() != TOK_DEFAULT
) {
1290 if (tok_get() == TOK_CASE
) {
1298 o_deref(TYPE_BT(&t
));
1305 l_default
= LABEL();
1310 l_matched
= LABEL();
1313 o_rmlocal(val_addr
, LONGSZ
);
1322 static char label_name
[NLABELS
][NAMELEN
];
1323 static int label_ids
[NLABELS
];
1326 static int label_id(char *name
)
1329 for (i
= nlabels
- 1; i
>= 0; --i
)
1330 if (!strcmp(label_name
[i
], name
))
1331 return label_ids
[i
];
1332 strcpy(label_name
[nlabels
], name
);
1333 label_ids
[nlabels
] = LABEL();
1334 return label_ids
[nlabels
++];
1337 static void readstmt(void)
1341 if (!tok_jmp('{')) {
1342 int _nlocals
= nlocals
;
1343 int _nglobals
= nglobals
;
1344 int _nenums
= nenums
;
1345 int _ntypedefs
= ntypedefs
;
1346 int _nstructs
= nstructs
;
1347 int _nfuncs
= nfuncs
;
1348 int _narrays
= narrays
;
1349 while (tok_jmp('}'))
1353 ntypedefs
= _ntypedefs
;
1354 nstructs
= _nstructs
;
1357 nglobals
= _nglobals
;
1360 if (!readdefs(localdef
, NULL
)) {
1364 if (!tok_jmp(TOK_TYPEDEF
)) {
1365 readdefs(typedefdef
, NULL
);
1369 if (!tok_jmp(TOK_IF
)) {
1370 int l_fail
= LABEL();
1371 int l_end
= LABEL();
1378 if (!tok_jmp(TOK_ELSE
)) {
1388 if (!tok_jmp(TOK_WHILE
)) {
1389 int o_break
= l_break
;
1390 int o_cont
= l_cont
;
1406 if (!tok_jmp(TOK_DO
)) {
1407 int o_break
= l_break
;
1408 int o_cont
= l_cont
;
1409 int l_beg
= LABEL();
1414 tok_expect(TOK_WHILE
);
1427 if (!tok_jmp(TOK_FOR
)) {
1428 int o_break
= l_break
;
1429 int o_cont
= l_cont
;
1430 int l_check
= LABEL(); /* for condition label */
1431 int l_body
= LABEL(); /* for block label */
1435 if (tok_see() != ';')
1439 if (tok_see() != ';') {
1447 if (tok_see() != ')')
1459 if (!tok_jmp(TOK_SWITCH
)) {
1463 if (!tok_jmp(TOK_RETURN
)) {
1464 int ret
= tok_see() != ';';
1473 if (!tok_jmp(TOK_BREAK
)) {
1478 if (!tok_jmp(TOK_CONTINUE
)) {
1483 if (!tok_jmp(TOK_GOTO
)) {
1484 tok_expect(TOK_NAME
);
1485 o_jmp(label_id(tok_id()));
1491 if (!tok_jmp(':')) {
1492 o_label(label_id(tok_id()));
1498 static void readfunc(struct name
*name
, int flags
)
1500 struct funcinfo
*fi
= &funcs
[name
->type
.id
];
1501 long beg
= tok_addr();
1503 strcpy(func_name
, fi
->name
);
1504 o_func_beg(func_name
, fi
->nargs
, F_GLOBAL(flags
), fi
->varg
);
1505 for (i
= 0; i
< fi
->nargs
; i
++) {
1506 struct name arg
= {"", "", fi
->args
[i
], o_arg2loc(i
)};
1507 strcpy(arg
.name
, fi
->argnames
[i
]);
1510 /* first pass: collecting statistics */
1514 /* second pass: generating code */
1518 func_name
[0] = '\0';
1524 static void readdecl(void)
1526 if (!tok_jmp(TOK_TYPEDEF
)) {
1527 readdefs(typedefdef
, NULL
);
1531 readdefs_int(globaldef
, NULL
);
1535 static void parse(void)
1537 while (tok_see() != TOK_EOF
)
1541 static void compat_macros(void)
1543 cpp_define("__STDC__", "");
1544 cpp_define("__linux__", "");
1545 cpp_define(I_ARCH
, "");
1547 /* ignored keywords */
1548 cpp_define("const", "");
1549 cpp_define("register", "");
1550 cpp_define("volatile", "");
1551 cpp_define("inline", "");
1552 cpp_define("restrict", "");
1553 cpp_define("__inline__", "");
1554 cpp_define("__restrict__", "");
1555 cpp_define("__attribute__(x)", "");
1556 cpp_define("__builtin_va_list__", "long");
1559 int main(int argc
, char *argv
[])
1565 while (i
< argc
&& argv
[i
][0] == '-') {
1566 if (argv
[i
][1] == 'I')
1567 cpp_addpath(argv
[i
][2] ? argv
[i
] + 2 : argv
[++i
]);
1568 if (argv
[i
][1] == 'D') {
1569 char *name
= argv
[i
] + 2;
1571 char *eq
= strchr(name
, '=');
1576 cpp_define(name
, def
);
1578 if (argv
[i
][1] == 'o')
1579 strcpy(obj
, argv
[i
][2] ? argv
[i
] + 2 : argv
[++i
]);
1583 die("neatcc: no file given\n");
1584 if (cpp_init(argv
[i
]))
1585 die("neatcc: cannot open <%s>\n", argv
[i
]);
1588 strcpy(obj
, argv
[i
]);
1589 obj
[strlen(obj
) - 1] = 'o';
1591 ofd
= open(obj
, O_WRONLY
| O_TRUNC
| O_CREAT
, 0600);
1598 /* parsing function and variable declarations */
1600 /* read the base type of a variable */
1601 static int basetype(struct type
*type
, unsigned *flags
)
1608 char name
[NAMELEN
] = "";
1614 switch (tok_see()) {
1646 isunion
= tok_get() == TOK_UNION
;
1647 if (!tok_jmp(TOK_NAME
))
1648 strcpy(name
, tok_id());
1649 if (tok_see() == '{')
1650 type
->id
= struct_create(name
, isunion
);
1652 type
->id
= struct_find(name
, isunion
);
1653 type
->flags
|= T_STRUCT
;
1659 if (tok_see() == '{')
1661 type
->bt
= 4 | BT_SIGNED
;
1664 if (tok_see() == TOK_NAME
) {
1665 int id
= typedef_find(tok_id());
1668 memcpy(type
, &typedefs
[id
].type
,
1681 type
->bt
= size
| (sign
? BT_SIGNED
: 0);
1685 static void readptrs(struct type
*type
)
1687 while (!tok_jmp('*')) {
1694 /* read function arguments */
1695 static int readargs(struct type
*args
, char argnames
[][NAMELEN
], int *varg
)
1700 while (tok_see() != ')') {
1701 if (!tok_jmp(TOK3("..."))) {
1705 if (readname(&args
[nargs
], argnames
[nargs
], NULL
)) {
1706 /* argument has no type, assume int */
1707 tok_expect(TOK_NAME
);
1708 memset(&args
[nargs
], 0, sizeof(struct type
));
1709 args
[nargs
].bt
= 4 | BT_SIGNED
;
1710 strcpy(argnames
[nargs
], tok_id());
1712 /* argument arrays are pointers */
1713 array2ptr(&args
[nargs
]);
1720 if (nargs
== 1 && !TYPE_BT(&args
[0]))
1725 /* read K&R function arguments */
1726 static void krdef(void *data
, struct name
*name
, unsigned flags
)
1728 struct funcinfo
*fi
= data
;
1730 for (i
= 0; i
< fi
->nargs
; i
++)
1731 if (!strcmp(fi
->argnames
[i
], name
->name
))
1732 memcpy(&fi
->args
[i
], &name
->type
, sizeof(name
->type
));
1736 * readarrays() parses array specifiers when reading a definition in
1737 * readname(). The "type" parameter contains the type contained in the
1738 * inner array; for instance, type in "int *a[10][20]" would be an int
1739 * pointer. When returning, the "type" parameter is changed to point
1740 * to the final array. The function returns a pointer to the type in
1741 * the inner array; this is useful when the type is not complete yet,
1742 * like when creating an array of function pointers as in
1743 * "int (*f[10])(int)". If there is no array brackets, NULL is returned.
1745 static struct type
*readarrays(struct type
*type
)
1748 struct type
*inner
= NULL
;
1751 while (!tok_jmp('[')) {
1757 err("const expr expected\n");
1762 for (i
= nar
- 1; i
>= 0; i
--) {
1763 type
->id
= array_add(type
, arsz
[i
]);
1765 inner
= &arrays
[type
->id
].type
;
1766 type
->flags
= T_ARRAY
;
1774 * readname() reads a variable definition; the name is copied into
1775 * "name" and the type is copied into "main" argument. The "base"
1776 * argument, if not NULL, indicates the base type of the variable.
1777 * For instance, the base type of "a" and "b" in "int *a, b[10]" is
1778 * "int". If NULL, basetype() is called directly to read the base
1779 * type of the variable. readname() returns zero, only if the
1780 * variable can be read.
1782 static int readname(struct type
*main
, char *name
, struct type
*base
)
1784 struct type tpool
[3];
1786 struct type
*type
= &tpool
[npool
++];
1787 struct type
*ptype
= NULL
; /* type inside parenthesis */
1788 struct type
*btype
= NULL
; /* type before parenthesis */
1791 memset(tpool
, 0, sizeof(tpool
));
1795 if (basetype(type
, &flags
))
1798 memcpy(type
, base
, sizeof(*base
));
1801 if (!tok_jmp('(')) {
1803 type
= &tpool
[npool
++];
1807 if (!tok_jmp(TOK_NAME
) && name
)
1808 strcpy(name
, tok_id());
1809 inner
= readarrays(type
);
1814 if (tok_see() == '(') {
1815 struct type args
[NARGS
];
1816 char argnames
[NARGS
][NAMELEN
];
1818 int nargs
= readargs(args
, argnames
, &varg
);
1821 type
= &tpool
[npool
++];
1824 ptype
->flags
= T_FUNC
;
1826 ptype
->id
= func_create(btype
, name
, argnames
, args
, nargs
, varg
);
1827 if (tok_see() != ';')
1828 while (tok_see() != '{' && !readdefs(krdef
, &funcs
[ptype
->id
]))
1831 if (ptype
&& readarrays(type
))
1834 memcpy(main
, type
, sizeof(*type
));
1838 static int readtype(struct type
*type
)
1840 return readname(type
, NULL
, NULL
);
1844 * readdef() reads a variable definitions statement. The definition
1845 * statement can appear in anywhere: global variables, function
1846 * local variables, struct fields, and typedefs. For each defined
1847 * variable, def() callback is called with the appropriate name
1848 * struct and flags; the callback should finish parsing the definition
1849 * by possibly reading the initializer expression and saving the name
1852 static int readdefs(void (*def
)(void *data
, struct name
*name
, unsigned flags
),
1856 unsigned base_flags
;
1857 if (basetype(&base
, &base_flags
))
1859 if (tok_see() == ';' || tok_see() == '{')
1862 struct name name
= {{""}};
1863 if (readname(&name
.type
, name
.name
, &base
))
1865 def(data
, &name
, base_flags
);
1866 } while (!tok_jmp(','));
1870 /* just like readdefs, but default to int type; for handling K&R functions */
1871 static int readdefs_int(void (*def
)(void *data
, struct name
*name
, unsigned flags
),
1876 if (basetype(&base
, &flags
)) {
1877 if (tok_see() != TOK_NAME
)
1879 memset(&base
, 0, sizeof(base
));
1880 base
.bt
= 4 | BT_SIGNED
;
1882 if (tok_see() != ';') {
1884 struct name name
= {{""}};
1885 if (readname(&name
.type
, name
.name
, &base
))
1887 def(data
, &name
, flags
);
1888 } while (!tok_jmp(','));
1894 /* parsing initializer expressions */
1896 static void jumpbrace(void)
1899 while (tok_see() != '}' || depth
--)
1900 if (tok_get() == '{')
1905 /* compute the size of the initializer expression */
1906 static int initsize(void)
1908 long addr
= tok_addr();
1912 if (!tok_jmp(TOK_STR
)) {
1918 while (tok_jmp('}')) {
1920 if (!tok_jmp('[')) {
1929 while (tok_see() != '}' && tok_see() != ',')
1930 if (tok_get() == '{')
1938 static struct type
*innertype(struct type
*t
)
1940 if (t
->flags
& T_ARRAY
&& !t
->ptr
)
1941 return innertype(&arrays
[t
->id
].type
);
1945 /* read the initializer expression and initialize basic types using set() cb */
1946 static void initexpr(struct type
*t
, int off
, void *obj
,
1947 void (*set
)(void *obj
, int off
, struct type
*t
))
1953 if (!t
->ptr
&& t
->flags
& T_STRUCT
) {
1954 struct structinfo
*si
= &structs
[t
->id
];
1956 for (i
= 0; i
< si
->nfields
&& tok_see() != '}'; i
++) {
1957 struct name
*field
= &si
->fields
[i
];
1958 if (!tok_jmp('.')) {
1959 tok_expect(TOK_NAME
);
1960 field
= struct_field(t
->id
, tok_id());
1963 initexpr(&field
->type
, off
+ field
->addr
, obj
, set
);
1967 } else if (t
->flags
& T_ARRAY
) {
1968 struct type
*t_de
= &arrays
[t
->id
].type
;
1970 /* handling extra braces as in: char s[] = {"sth"} */
1971 if (TYPE_SZ(t_de
) == 1 && tok_see() == TOK_STR
) {
1976 for (i
= 0; tok_see() != '}'; i
++) {
1978 struct type
*it
= t_de
;
1979 if (!tok_jmp('[')) {
1986 if (tok_see() != '{' && (tok_see() != TOK_STR
||
1987 !(it
->flags
& T_ARRAY
)))
1988 it
= innertype(t_de
);
1989 initexpr(it
, off
+ type_totsz(it
) * idx
, obj
, set
);