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 */
486 t
.bt
= 1 | BT_SIGNED
;
487 a
.id
= array_add(&t
, len
);
490 o_sym(tmp_str(buf
, len
));
493 if (!tok_jmp(TOK_NAME
)) {
494 struct name unkn
= {""};
495 char *name
= unkn
.name
;
497 strcpy(name
, tok_id());
498 /* don't search for labels here */
499 if (!ncexpr
&& !caseexpr
&& tok_see() == ':')
501 if ((n
= local_find(name
)) != -1) {
502 struct name
*l
= &locals
[n
];
504 ts_push_addr(&l
->type
);
507 if ((n
= global_find(name
)) != -1) {
508 struct name
*g
= &globals
[n
];
509 o_sym(*g
->elfname
? g
->elfname
: g
->name
);
510 ts_push_addr(&g
->type
);
513 if (!enum_find(&n
, name
)) {
514 ts_push_bt(4 | BT_SIGNED
);
518 if (tok_see() != '(')
519 err("unknown symbol <%s>\n", name
);
533 if (!t
.ptr
|| !o
.ptr
)
537 while (tok_jmp(')')) {
548 static void arrayderef(void)
554 if (!(t
.flags
& T_ARRAY
&& !t
.ptr
) && t
.addr
) {
556 o_deref(TYPE_BT(&t
));
571 static void inc_post(int op
)
573 struct type t
= ts
[nts
- 1];
574 /* pushing the value before inc */
580 /* increment by 1 or pointer size */
584 o_num(t
.ptr
> 0 ? type_szde(&t
) : 1);
588 o_assign(TYPE_BT(&t
));
592 static void readfield(void)
596 tok_expect(TOK_NAME
);
599 field
= struct_field(t
.id
, tok_id());
604 ts_push_addr(&field
->type
);
607 static struct funcinfo
{
608 struct type args
[NARGS
];
612 /* function and argument names; useful only when defining */
613 char argnames
[NARGS
][NAMELEN
];
618 static int func_create(struct type
*ret
, char *name
, char argnames
[][NAMELEN
],
619 struct type
*args
, int nargs
, int varg
)
621 struct funcinfo
*fi
= &funcs
[nfuncs
++];
623 if (nfuncs
>= NFUNCS
)
624 err("nomem: NFUNCS reached!\n");
625 memcpy(&fi
->ret
, ret
, sizeof(*ret
));
626 for (i
= 0; i
< nargs
; i
++)
627 memcpy(&fi
->args
[i
], &args
[i
], sizeof(*ret
));
630 strcpy(fi
->name
, name
? name
: "");
631 for (i
= 0; i
< nargs
; i
++)
632 strcpy(fi
->argnames
[i
], argnames
[i
]);
636 static void readcall(void)
642 if (t
.flags
& T_FUNC
&& t
.ptr
> 0)
644 fi
= t
.flags
& T_FUNC
? &funcs
[t
.id
] : NULL
;
645 if (tok_see() != ')') {
650 } while (!tok_jmp(','));
653 o_call(argc
, fi
? TYPE_BT(&fi
->ret
) : 4 | BT_SIGNED
);
655 if (TYPE_BT(&fi
->ret
))
656 o_cast(TYPE_BT(&fi
->ret
));
659 ts_push_bt(4 | BT_SIGNED
);
663 static void readpost(void)
677 if (!tok_jmp(TOK2("++"))) {
681 if (!tok_jmp(TOK2("--"))) {
689 if (!tok_jmp(TOK2("->"))) {
698 static void inc_pre(int op
)
702 /* copy the destination */
704 ts_push(&ts
[nts
- 1]);
705 /* increment by 1 or pointer size */
707 o_num(t
.ptr
> 0 ? type_szde(&t
) : 1);
709 /* assign the result */
710 o_assign(TYPE_BT(&t
));
714 static void readpre(void)
721 err("cannot use the address\n");
733 err("dereferencing non-pointer\n");
735 o_deref(TYPE_BT(&t
));
745 ts_push_bt(4 | BT_SIGNED
);
764 if (!tok_jmp(TOK2("++"))) {
768 if (!tok_jmp(TOK2("--"))) {
772 if (!tok_jmp(TOK_SIZEOF
)) {
774 int op
= !tok_jmp('(');
785 o_num(type_totsz(&t
));
793 static void readmul(void)
816 static void readadd(void)
834 static void shift(int op
)
838 ts_pop_de2(NULL
, &t
);
839 o_bop(op
| (BT_SIGNED
& TYPE_BT(&t
) ? O_SIGNED
: 0));
840 ts_push_bt(TYPE_BT(&t
));
843 static void readshift(void)
847 if (!tok_jmp(TOK2("<<"))) {
851 if (!tok_jmp(TOK2(">>"))) {
859 static void cmp(int op
)
864 ts_pop_de2(&t1
, &t2
);
865 bt
= bt_op(TYPE_BT(&t1
), TYPE_BT(&t2
));
866 o_bop(op
| (bt
& BT_SIGNED
? O_SIGNED
: 0));
867 ts_push_bt(4 | BT_SIGNED
);
870 static void readcmp(void)
882 if (!tok_jmp(TOK2("<="))) {
886 if (!tok_jmp(TOK2(">="))) {
894 static void eq(int op
)
897 ts_pop_de2(NULL
, NULL
);
899 ts_push_bt(4 | BT_SIGNED
);
902 static void readeq(void)
906 if (!tok_jmp(TOK2("=="))) {
910 if (!tok_jmp(TOK2("!="))) {
918 static void readbitand(void)
921 while (!tok_jmp('&')) {
927 static void readxor(void)
930 while (!tok_jmp('^')) {
936 static void readbitor(void)
939 while (!tok_jmp('|')) {
945 static void readand(void)
948 int l_fail
= LABEL();
950 if (tok_see() != TOK2("&&"))
955 while (!tok_jmp(TOK2("&&"))) {
968 ts_push_bt(4 | BT_SIGNED
);
971 static void reador(void)
973 int l_pass
= LABEL();
976 if (tok_see() != TOK2("||"))
981 while (!tok_jmp(TOK2("||"))) {
994 ts_push_bt(4 | BT_SIGNED
);
997 static void readcexpr(void);
999 static int readcexpr_const(void)
1007 /* both branches yield the same type; so ignore the first */
1015 /* making sure t->addr == 0 on both branches */
1022 static void readcexpr(void)
1030 if (readcexpr_const()) {
1031 int l_fail
= LABEL();
1032 int l_end
= LABEL();
1036 /* both branches yield the same type; so ignore the first */
1038 if (!TYPE_VOID(&ret
))
1045 /* making sure t->addr == 0 on both branches */
1047 if (!TYPE_VOID(&ret
)) {
1056 static void opassign(int op
, int ptrop
)
1058 struct type t
= ts
[nts
- 1];
1062 if (op
== O_ADD
|| op
== O_SUB
)
1066 o_assign(TYPE_BT(&ts
[nts
- 2]));
1071 static void doassign(void)
1073 struct type t
= ts
[nts
- 1];
1074 if (!t
.ptr
&& t
.flags
& T_STRUCT
) {
1076 o_num(type_totsz(&t
));
1080 o_assign(TYPE_BT(&ts
[nts
- 1]));
1085 static void readexpr(void)
1088 if (!tok_jmp('=')) {
1093 if (!tok_jmp(TOK2("+="))) {
1097 if (!tok_jmp(TOK2("-="))) {
1101 if (!tok_jmp(TOK2("*="))) {
1105 if (!tok_jmp(TOK2("/="))) {
1109 if (!tok_jmp(TOK2("%="))) {
1113 if (!tok_jmp(TOK3("<<="))) {
1117 if (!tok_jmp(TOK3(">>="))) {
1121 if (!tok_jmp(TOK3("&="))) {
1125 if (!tok_jmp(TOK3("|="))) {
1129 if (!tok_jmp(TOK3("^="))) {
1135 static void readestmt(void)
1141 } while (!tok_jmp(','));
1144 #define F_GLOBAL(flags) (!((flags) & F_STATIC))
1146 static void globalinit(void *obj
, int off
, struct type
*t
)
1148 struct name
*name
= obj
;
1149 char *elfname
= *name
->elfname
? name
->elfname
: name
->name
;
1150 if (t
->flags
& T_ARRAY
&& tok_see() == TOK_STR
) {
1151 struct type
*t_de
= &arrays
[t
->id
].type
;
1152 if (!t_de
->ptr
&& !t_de
->flags
&& TYPE_SZ(t_de
) == 1) {
1155 tok_expect(TOK_STR
);
1156 tok_str(&buf
, &len
);
1157 o_dscpy(name
->addr
+ off
, buf
, len
);
1162 o_dsset(elfname
, off
, TYPE_BT(t
));
1166 static void readfunc(struct name
*name
, int flags
);
1168 static void globaldef(void *data
, struct name
*name
, unsigned flags
)
1170 struct type
*t
= &name
->type
;
1171 char *elfname
= *name
->elfname
? name
->elfname
: name
->name
;
1173 if (t
->flags
& T_ARRAY
&& !t
->ptr
&& !arrays
[t
->id
].n
)
1174 if (~flags
& F_EXTERN
)
1175 arrays
[t
->id
].n
= initsize();
1177 if (!(flags
& F_EXTERN
) && (!(t
->flags
& T_FUNC
) || t
->ptr
)) {
1178 if (tok_see() == '=')
1179 name
->addr
= o_dsnew(elfname
, sz
, F_GLOBAL(flags
));
1181 o_bsnew(elfname
, sz
, F_GLOBAL(flags
));
1185 initexpr(t
, 0, name
, globalinit
);
1186 if (tok_see() == '{' && name
->type
.flags
& T_FUNC
)
1187 readfunc(name
, flags
);
1190 /* generate the address of local + off */
1191 static void o_localoff(long addr
, int off
)
1200 static void localinit(void *obj
, int off
, struct type
*t
)
1202 long addr
= *(long *) obj
;
1203 if (t
->flags
& T_ARRAY
&& tok_see() == TOK_STR
) {
1204 struct type
*t_de
= &arrays
[t
->id
].type
;
1205 if (!t_de
->ptr
&& !t_de
->flags
&& TYPE_SZ(t_de
) == 1) {
1208 tok_expect(TOK_STR
);
1209 tok_str(&buf
, &len
);
1210 o_localoff(addr
, off
);
1211 o_sym(tmp_str(buf
, len
));
1218 o_localoff(addr
, off
);
1226 /* current function name */
1227 static char func_name
[NAMELEN
];
1229 static void localdef(void *data
, struct name
*name
, unsigned flags
)
1231 struct type
*t
= &name
->type
;
1232 if ((flags
& F_EXTERN
) || ((t
->flags
& T_FUNC
) && !t
->ptr
)) {
1236 if (flags
& F_STATIC
) {
1237 sprintf(name
->elfname
, "__neatcc.%s.%s", func_name
, name
->name
);
1238 globaldef(data
, name
, flags
);
1241 if (t
->flags
& T_ARRAY
&& !t
->ptr
&& !arrays
[t
->id
].n
)
1242 arrays
[t
->id
].n
= initsize();
1243 name
->addr
= o_mklocal(type_totsz(&name
->type
));
1245 if (!tok_jmp('=')) {
1246 if (t
->flags
& (T_ARRAY
| T_STRUCT
) && !t
->ptr
) {
1247 o_local(name
->addr
);
1249 o_num(type_totsz(t
));
1253 initexpr(t
, 0, &name
->addr
, localinit
);
1257 static void typedefdef(void *data
, struct name
*name
, unsigned flags
)
1259 typedef_add(name
->name
, &name
->type
);
1262 static void readstmt(void);
1264 static void readswitch(void)
1266 int o_break
= l_break
;
1267 long val_addr
= o_mklocal(LONGSZ
);
1269 int ncases
= 0; /* number of case labels */
1270 int l_failed
= LABEL(); /* address of last failed jmp */
1271 int l_matched
= LABEL(); /* address of last walk through jmp */
1272 int l_default
= 0; /* default case label */
1279 o_assign(TYPE_BT(&t
));
1284 while (tok_jmp('}')) {
1285 if (tok_see() != TOK_CASE
&& tok_see() != TOK_DEFAULT
) {
1291 if (tok_get() == TOK_CASE
) {
1299 o_deref(TYPE_BT(&t
));
1306 l_default
= LABEL();
1311 l_matched
= LABEL();
1314 o_rmlocal(val_addr
, LONGSZ
);
1323 static char label_name
[NLABELS
][NAMELEN
];
1324 static int label_ids
[NLABELS
];
1327 static int label_id(char *name
)
1330 for (i
= nlabels
- 1; i
>= 0; --i
)
1331 if (!strcmp(label_name
[i
], name
))
1332 return label_ids
[i
];
1333 strcpy(label_name
[nlabels
], name
);
1334 label_ids
[nlabels
] = LABEL();
1335 return label_ids
[nlabels
++];
1338 static void readstmt(void)
1342 if (!tok_jmp('{')) {
1343 int _nlocals
= nlocals
;
1344 int _nglobals
= nglobals
;
1345 int _nenums
= nenums
;
1346 int _ntypedefs
= ntypedefs
;
1347 int _nstructs
= nstructs
;
1348 int _nfuncs
= nfuncs
;
1349 int _narrays
= narrays
;
1350 while (tok_jmp('}'))
1354 ntypedefs
= _ntypedefs
;
1355 nstructs
= _nstructs
;
1358 nglobals
= _nglobals
;
1361 if (!readdefs(localdef
, NULL
)) {
1365 if (!tok_jmp(TOK_TYPEDEF
)) {
1366 readdefs(typedefdef
, NULL
);
1370 if (!tok_jmp(TOK_IF
)) {
1371 int l_fail
= LABEL();
1372 int l_end
= LABEL();
1379 if (!tok_jmp(TOK_ELSE
)) {
1389 if (!tok_jmp(TOK_WHILE
)) {
1390 int o_break
= l_break
;
1391 int o_cont
= l_cont
;
1407 if (!tok_jmp(TOK_DO
)) {
1408 int o_break
= l_break
;
1409 int o_cont
= l_cont
;
1410 int l_beg
= LABEL();
1415 tok_expect(TOK_WHILE
);
1428 if (!tok_jmp(TOK_FOR
)) {
1429 int o_break
= l_break
;
1430 int o_cont
= l_cont
;
1431 int l_check
= LABEL(); /* for condition label */
1432 int l_body
= LABEL(); /* for block label */
1436 if (tok_see() != ';')
1440 if (tok_see() != ';') {
1448 if (tok_see() != ')')
1460 if (!tok_jmp(TOK_SWITCH
)) {
1464 if (!tok_jmp(TOK_RETURN
)) {
1465 int ret
= tok_see() != ';';
1474 if (!tok_jmp(TOK_BREAK
)) {
1479 if (!tok_jmp(TOK_CONTINUE
)) {
1484 if (!tok_jmp(TOK_GOTO
)) {
1485 tok_expect(TOK_NAME
);
1486 o_jmp(label_id(tok_id()));
1492 if (!tok_jmp(':')) {
1493 o_label(label_id(tok_id()));
1499 static void readfunc(struct name
*name
, int flags
)
1501 struct funcinfo
*fi
= &funcs
[name
->type
.id
];
1502 long beg
= tok_addr();
1504 strcpy(func_name
, fi
->name
);
1505 o_func_beg(func_name
, fi
->nargs
, F_GLOBAL(flags
), fi
->varg
);
1506 for (i
= 0; i
< fi
->nargs
; i
++) {
1507 struct name arg
= {"", "", fi
->args
[i
], o_arg2loc(i
)};
1508 strcpy(arg
.name
, fi
->argnames
[i
]);
1511 /* first pass: collecting statistics */
1515 /* second pass: generating code */
1519 func_name
[0] = '\0';
1525 static void readdecl(void)
1527 if (!tok_jmp(TOK_TYPEDEF
)) {
1528 readdefs(typedefdef
, NULL
);
1532 readdefs_int(globaldef
, NULL
);
1536 static void parse(void)
1538 while (tok_see() != TOK_EOF
)
1542 static void compat_macros(void)
1544 cpp_define("__STDC__", "");
1545 cpp_define("__linux__", "");
1546 cpp_define(I_ARCH
, "");
1548 /* ignored keywords */
1549 cpp_define("const", "");
1550 cpp_define("register", "");
1551 cpp_define("volatile", "");
1552 cpp_define("inline", "");
1553 cpp_define("restrict", "");
1554 cpp_define("__inline__", "");
1555 cpp_define("__restrict__", "");
1556 cpp_define("__attribute__(x)", "");
1557 cpp_define("__builtin_va_list__", "long");
1560 int main(int argc
, char *argv
[])
1566 while (i
< argc
&& argv
[i
][0] == '-') {
1567 if (argv
[i
][1] == 'I')
1568 cpp_addpath(argv
[i
][2] ? argv
[i
] + 2 : argv
[++i
]);
1569 if (argv
[i
][1] == 'D') {
1570 char *name
= argv
[i
] + 2;
1572 char *eq
= strchr(name
, '=');
1577 cpp_define(name
, def
);
1579 if (argv
[i
][1] == 'o')
1580 strcpy(obj
, argv
[i
][2] ? argv
[i
] + 2 : argv
[++i
]);
1584 die("neatcc: no file given\n");
1585 if (cpp_init(argv
[i
]))
1586 die("neatcc: cannot open <%s>\n", argv
[i
]);
1589 strcpy(obj
, argv
[i
]);
1590 obj
[strlen(obj
) - 1] = 'o';
1592 ofd
= open(obj
, O_WRONLY
| O_TRUNC
| O_CREAT
, 0600);
1599 /* parsing function and variable declarations */
1601 /* read the base type of a variable */
1602 static int basetype(struct type
*type
, unsigned *flags
)
1609 char name
[NAMELEN
] = "";
1615 switch (tok_see()) {
1647 isunion
= tok_get() == TOK_UNION
;
1648 if (!tok_jmp(TOK_NAME
))
1649 strcpy(name
, tok_id());
1650 if (tok_see() == '{')
1651 type
->id
= struct_create(name
, isunion
);
1653 type
->id
= struct_find(name
, isunion
);
1654 type
->flags
|= T_STRUCT
;
1660 if (tok_see() == '{')
1662 type
->bt
= 4 | BT_SIGNED
;
1665 if (tok_see() == TOK_NAME
) {
1666 int id
= typedef_find(tok_id());
1669 memcpy(type
, &typedefs
[id
].type
,
1682 type
->bt
= size
| (sign
? BT_SIGNED
: 0);
1686 static void readptrs(struct type
*type
)
1688 while (!tok_jmp('*')) {
1695 /* read function arguments */
1696 static int readargs(struct type
*args
, char argnames
[][NAMELEN
], int *varg
)
1701 while (tok_see() != ')') {
1702 if (!tok_jmp(TOK3("..."))) {
1706 if (readname(&args
[nargs
], argnames
[nargs
], NULL
)) {
1707 /* argument has no type, assume int */
1708 tok_expect(TOK_NAME
);
1709 memset(&args
[nargs
], 0, sizeof(struct type
));
1710 args
[nargs
].bt
= 4 | BT_SIGNED
;
1711 strcpy(argnames
[nargs
], tok_id());
1713 /* argument arrays are pointers */
1714 array2ptr(&args
[nargs
]);
1721 if (nargs
== 1 && !TYPE_BT(&args
[0]))
1726 /* read K&R function arguments */
1727 static void krdef(void *data
, struct name
*name
, unsigned flags
)
1729 struct funcinfo
*fi
= data
;
1731 for (i
= 0; i
< fi
->nargs
; i
++)
1732 if (!strcmp(fi
->argnames
[i
], name
->name
))
1733 memcpy(&fi
->args
[i
], &name
->type
, sizeof(name
->type
));
1737 * readarrays() parses array specifiers when reading a definition in
1738 * readname(). The "type" parameter contains the type contained in the
1739 * inner array; for instance, type in "int *a[10][20]" would be an int
1740 * pointer. When returning, the "type" parameter is changed to point
1741 * to the final array. The function returns a pointer to the type in
1742 * the inner array; this is useful when the type is not complete yet,
1743 * like when creating an array of function pointers as in
1744 * "int (*f[10])(int)". If there is no array brackets, NULL is returned.
1746 static struct type
*readarrays(struct type
*type
)
1749 struct type
*inner
= NULL
;
1752 while (!tok_jmp('[')) {
1758 err("const expr expected\n");
1763 for (i
= nar
- 1; i
>= 0; i
--) {
1764 type
->id
= array_add(type
, arsz
[i
]);
1766 inner
= &arrays
[type
->id
].type
;
1767 type
->flags
= T_ARRAY
;
1775 * readname() reads a variable definition; the name is copied into
1776 * "name" and the type is copied into "main" argument. The "base"
1777 * argument, if not NULL, indicates the base type of the variable.
1778 * For instance, the base type of "a" and "b" in "int *a, b[10]" is
1779 * "int". If NULL, basetype() is called directly to read the base
1780 * type of the variable. readname() returns zero, only if the
1781 * variable can be read.
1783 static int readname(struct type
*main
, char *name
, struct type
*base
)
1785 struct type tpool
[3];
1787 struct type
*type
= &tpool
[npool
++];
1788 struct type
*ptype
= NULL
; /* type inside parenthesis */
1789 struct type
*btype
= NULL
; /* type before parenthesis */
1792 memset(tpool
, 0, sizeof(tpool
));
1796 if (basetype(type
, &flags
))
1799 memcpy(type
, base
, sizeof(*base
));
1802 if (!tok_jmp('(')) {
1804 type
= &tpool
[npool
++];
1808 if (!tok_jmp(TOK_NAME
) && name
)
1809 strcpy(name
, tok_id());
1810 inner
= readarrays(type
);
1815 if (tok_see() == '(') {
1816 struct type args
[NARGS
];
1817 char argnames
[NARGS
][NAMELEN
];
1819 int nargs
= readargs(args
, argnames
, &varg
);
1822 type
= &tpool
[npool
++];
1825 ptype
->flags
= T_FUNC
;
1827 ptype
->id
= func_create(btype
, name
, argnames
, args
, nargs
, varg
);
1828 if (tok_see() != ';')
1829 while (tok_see() != '{' && !readdefs(krdef
, &funcs
[ptype
->id
]))
1832 if (ptype
&& readarrays(type
))
1835 memcpy(main
, type
, sizeof(*type
));
1839 static int readtype(struct type
*type
)
1841 return readname(type
, NULL
, NULL
);
1845 * readdef() reads a variable definitions statement. The definition
1846 * statement can appear in anywhere: global variables, function
1847 * local variables, struct fields, and typedefs. For each defined
1848 * variable, def() callback is called with the appropriate name
1849 * struct and flags; the callback should finish parsing the definition
1850 * by possibly reading the initializer expression and saving the name
1853 static int readdefs(void (*def
)(void *data
, struct name
*name
, unsigned flags
),
1857 unsigned base_flags
;
1858 if (basetype(&base
, &base_flags
))
1860 if (tok_see() == ';' || tok_see() == '{')
1863 struct name name
= {{""}};
1864 if (readname(&name
.type
, name
.name
, &base
))
1866 def(data
, &name
, base_flags
);
1867 } while (!tok_jmp(','));
1871 /* just like readdefs, but default to int type; for handling K&R functions */
1872 static int readdefs_int(void (*def
)(void *data
, struct name
*name
, unsigned flags
),
1877 if (basetype(&base
, &flags
)) {
1878 if (tok_see() != TOK_NAME
)
1880 memset(&base
, 0, sizeof(base
));
1881 base
.bt
= 4 | BT_SIGNED
;
1883 if (tok_see() != ';') {
1885 struct name name
= {{""}};
1886 if (readname(&name
.type
, name
.name
, &base
))
1888 def(data
, &name
, flags
);
1889 } while (!tok_jmp(','));
1895 /* parsing initializer expressions */
1897 static void jumpbrace(void)
1900 while (tok_see() != '}' || depth
--)
1901 if (tok_get() == '{')
1906 /* compute the size of the initializer expression */
1907 static int initsize(void)
1909 long addr
= tok_addr();
1913 if (!tok_jmp(TOK_STR
)) {
1919 while (tok_jmp('}')) {
1921 if (!tok_jmp('[')) {
1930 while (tok_see() != '}' && tok_see() != ',')
1931 if (tok_get() == '{')
1939 static struct type
*innertype(struct type
*t
)
1941 if (t
->flags
& T_ARRAY
&& !t
->ptr
)
1942 return innertype(&arrays
[t
->id
].type
);
1946 /* read the initializer expression and initialize basic types using set() cb */
1947 static void initexpr(struct type
*t
, int off
, void *obj
,
1948 void (*set
)(void *obj
, int off
, struct type
*t
))
1954 if (!t
->ptr
&& t
->flags
& T_STRUCT
) {
1955 struct structinfo
*si
= &structs
[t
->id
];
1957 for (i
= 0; i
< si
->nfields
&& tok_see() != '}'; i
++) {
1958 struct name
*field
= &si
->fields
[i
];
1959 if (!tok_jmp('.')) {
1960 tok_expect(TOK_NAME
);
1961 field
= struct_field(t
->id
, tok_id());
1964 initexpr(&field
->type
, off
+ field
->addr
, obj
, set
);
1968 } else if (t
->flags
& T_ARRAY
) {
1969 struct type
*t_de
= &arrays
[t
->id
].type
;
1971 /* handling extra braces as in: char s[] = {"sth"} */
1972 if (TYPE_SZ(t_de
) == 1 && tok_see() == TOK_STR
) {
1977 for (i
= 0; tok_see() != '}'; i
++) {
1979 struct type
*it
= t_de
;
1980 if (!tok_jmp('[')) {
1987 if (tok_see() != '{' && (tok_see() != TOK_STR
||
1988 !(it
->flags
& T_ARRAY
)))
1989 it
= innertype(t_de
);
1990 initexpr(it
, off
+ type_totsz(it
) * idx
, obj
, set
);