2 * THE NEATCC C COMPILER
4 * Copyright (C) 2010-2016 Ali Gholami Rudi
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 * The parser reads tokens from the tokenizer (tok_*) and calls the
22 * appropriate code generation functions (o_*). The generator
23 * maintains a stack of values pushed via, for instance, o_num()
24 * and generates the necessary code for the accesses to the items
25 * in this stack, like o_bop() for performing a binary operations
26 * on the top two items of the stack. The parser maintains the
27 * types of values pushed to the generator stack in its type stack
28 * (ts_*). For instance, for binary operations two types are
29 * popped first and the resulting type is pushed to the type stack
41 #include <sys/types.h>
44 #define ALIGN(x, a) (((x) + (a) - 1) & ~((a) - 1))
45 #define MIN(a, b) ((a) < (b) ? (a) : (b))
46 #define MAX(a, b) ((a) < (b) ? (b) : (a))
48 #define TYPE_BT(t) ((t)->ptr ? ULNG : (t)->bt)
49 #define TYPE_SZ(t) ((t)->ptr ? ULNG : (t)->bt & T_MSIZE)
50 #define TYPE_VOID(t) (!(t)->bt && !(t)->flags && !(t)->ptr)
52 /* type->flag values */
57 /* variable definition flags */
65 int id
; /* for structs, functions and arrays */
66 int addr
; /* the address is passed to gen.c; deref for value */
70 static struct type ts
[NTMPS
];
73 static void ts_push_bt(unsigned bt
)
80 o_cast(bt
); /* casting to architecture word */
84 static void ts_push(struct type
*t
)
86 struct type
*d
= &ts
[nts
++];
87 memcpy(d
, t
, sizeof(*t
));
90 static void ts_push_addr(struct type
*t
)
96 static void ts_pop(struct type
*type
)
103 void err(char *fmt
, ...)
108 vsprintf(msg
, fmt
, ap
);
110 die("%s: %s", cpp_loc(tok_addr()), msg
);
113 void *mextend(void *old
, long oldsz
, long newsz
, long memsz
)
115 void *new = malloc(newsz
* memsz
);
116 memcpy(new, old
, oldsz
* memsz
);
117 memset(new + oldsz
* memsz
, 0, (newsz
- oldsz
) * memsz
);
124 char elfname
[NAMELEN
]; /* local elf name for function static variables */
126 long addr
; /* local stack offset, global data addr, struct offset */
129 static struct name
*locals
;
130 static int locals_n
, locals_sz
;
131 static struct name
*globals
;
132 static int globals_n
, globals_sz
;
134 static void local_add(struct name
*name
)
136 if (locals_n
>= locals_sz
) {
137 locals_sz
= MAX(128, locals_sz
* 2);
138 locals
= mextend(locals
, locals_n
, locals_sz
, sizeof(locals
[0]));
140 memcpy(&locals
[locals_n
++], name
, sizeof(*name
));
143 static int local_find(char *name
)
146 for (i
= locals_n
- 1; i
>= 0; --i
)
147 if (!strcmp(locals
[i
].name
, name
))
152 static int global_find(char *name
)
155 for (i
= globals_n
- 1; i
>= 0; i
--)
156 if (!strcmp(name
, globals
[i
].name
))
161 static void global_add(struct name
*name
)
163 if (globals_n
>= globals_sz
) {
164 globals_sz
= MAX(128, globals_sz
* 2);
165 globals
= mextend(globals
, globals_n
, globals_sz
, sizeof(globals
[0]));
167 memcpy(&globals
[globals_n
++], name
, sizeof(*name
));
170 #define LABEL() (++label)
172 static int label
; /* last used label id */
173 static int l_break
; /* current break label */
174 static int l_cont
; /* current continue label */
176 static struct enumval
{
180 static int enums_n
, enums_sz
;
182 static void enum_add(char *name
, int val
)
185 if (enums_n
>= enums_sz
) {
186 enums_sz
= MAX(128, enums_sz
* 2);
187 enums
= mextend(enums
, enums_n
, enums_sz
, sizeof(enums
[0]));
189 ev
= &enums
[enums_n
++];
190 strcpy(ev
->name
, name
);
194 static int enum_find(int *val
, char *name
)
197 for (i
= enums_n
- 1; i
>= 0; --i
)
198 if (!strcmp(name
, enums
[i
].name
)) {
205 static struct typdefinfo
{
209 static int typedefs_n
, typedefs_sz
;
211 static void typedef_add(char *name
, struct type
*type
)
213 struct typdefinfo
*ti
;
214 if (typedefs_n
>= typedefs_sz
) {
215 typedefs_sz
= MAX(128, typedefs_sz
* 2);
216 typedefs
= mextend(typedefs
, typedefs_n
, typedefs_sz
,
217 sizeof(typedefs
[0]));
219 ti
= &typedefs
[typedefs_n
++];
220 strcpy(ti
->name
, name
);
221 memcpy(&ti
->type
, type
, sizeof(*type
));
224 static int typedef_find(char *name
)
227 for (i
= typedefs_n
- 1; i
>= 0; --i
)
228 if (!strcmp(name
, typedefs
[i
].name
))
233 static struct array
{
237 static int arrays_n
, arrays_sz
;
239 static int array_add(struct type
*type
, int n
)
242 if (arrays_n
>= arrays_sz
) {
243 arrays_sz
= MAX(128, arrays_sz
* 2);
244 arrays
= mextend(arrays
, arrays_n
, arrays_sz
, sizeof(arrays
[0]));
246 a
= &arrays
[arrays_n
++];
247 memcpy(&a
->type
, type
, sizeof(*type
));
252 static void array2ptr(struct type
*t
)
254 if (t
->flags
& T_ARRAY
&& !t
->ptr
) {
255 memcpy(t
, &arrays
[t
->id
].type
, sizeof(*t
));
260 static struct structinfo
{
262 struct name fields
[NFIELDS
];
267 static int structs_n
, structs_sz
;
269 static int struct_find(char *name
, int isunion
)
272 for (i
= structs_n
- 1; i
>= 0; --i
)
273 if (*structs
[i
].name
&& !strcmp(name
, structs
[i
].name
) &&
274 structs
[i
].isunion
== isunion
)
276 if (structs_n
>= structs_sz
) {
277 structs_sz
= MAX(128, structs_sz
* 2);
278 structs
= mextend(structs
, structs_n
, structs_sz
, sizeof(structs
[0]));
281 memset(&structs
[i
], 0, sizeof(structs
[i
]));
282 strcpy(structs
[i
].name
, name
);
283 structs
[i
].isunion
= isunion
;
287 static struct name
*struct_field(int id
, char *name
)
289 struct structinfo
*si
= &structs
[id
];
291 for (i
= 0; i
< si
->nfields
; i
++)
292 if (!strcmp(name
, si
->fields
[i
].name
))
293 return &si
->fields
[i
];
294 err("unknown field <%s>\n", name
);
298 /* return t's size */
299 static int type_totsz(struct type
*t
)
303 if (t
->flags
& T_ARRAY
)
304 return arrays
[t
->id
].n
* type_totsz(&arrays
[t
->id
].type
);
305 return t
->flags
& T_STRUCT
? structs
[t
->id
].size
: T_SZ(t
->bt
);
308 /* return t's dereferenced size */
309 static unsigned type_szde(struct type
*t
)
314 return type_totsz(&de
);
317 /* dereference stack top if t->addr (ie. address is pushed to gen.c) */
318 static void ts_de(int deref
)
320 struct type
*t
= &ts
[nts
- 1];
322 if (deref
&& t
->addr
&& (t
->ptr
|| !(t
->flags
& T_FUNC
)))
327 /* pop stack pop to *t and dereference if t->addr */
328 static void ts_pop_de(struct type
*t
)
334 /* pop the top 2 stack values and dereference them if t->addr */
335 static void ts_pop_de2(struct type
*t1
, struct type
*t2
)
343 /* the previous identifier; to handle labels */
344 static char tok_previden
[NAMELEN
];
346 static char *tok_iden(void)
348 snprintf(tok_previden
, sizeof(tok_previden
), "%s", tok_get());
352 static int tok_jmp(char *tok
)
354 if (strcmp(tok
, tok_see()))
360 static int tok_comes(char *tok
)
362 return !strcmp(tok
, tok_see());
365 static void tok_req(char *tok
)
367 char *got
= tok_get();
368 if (strcmp(tok
, got
))
369 err("syntax error (expected <%s> but got <%s>)\n", tok
, got
);
372 static int tok_grp(void)
374 int c
= (unsigned char) tok_see()[0];
377 if (c
== '\'' || isdigit(c
))
379 if (c
== '_' || isalpha(c
))
384 /* the result of a binary operation on variables of type bt1 and bt2 */
385 static unsigned bt_op(unsigned bt1
, unsigned bt2
)
387 int sz
= MAX(T_SZ(bt1
), T_SZ(bt2
));
388 return ((bt1
| bt2
) & T_MSIGN
) | MAX(sz
, UINT
);
391 /* the result of a unary operation on variables of bt */
392 static unsigned bt_uop(unsigned bt
)
394 return bt_op(bt
, UINT
);
397 /* push the result of a binary operation on the type stack */
398 static void ts_binop(int op
)
401 unsigned bt1
, bt2
, bt
;
402 ts_pop_de2(&t1
, &t2
);
405 bt
= bt_op(bt1
, bt2
);
406 if (op
== O_DIV
|| op
== O_MOD
)
407 bt
= T_MK(bt2
& T_MSIGN
, bt
);
412 /* push the result of an additive binary operation on the type stack */
413 static void ts_addop(int op
)
416 ts_pop_de2(&t1
, &t2
);
417 if (!t1
.ptr
&& !t2
.ptr
) {
419 ts_push_bt(bt_op(TYPE_BT(&t1
), TYPE_BT(&t2
)));
422 if (t1
.ptr
&& !t2
.ptr
)
424 if (!t1
.ptr
&& t2
.ptr
)
425 if (type_szde(&t2
) > 1) {
426 o_num(type_szde(&t2
));
429 if (t1
.ptr
&& !t2
.ptr
)
432 if (t1
.ptr
&& t2
.ptr
) {
433 int sz
= type_szde(&t1
);
440 ts_push(t1
.ptr
? &t1
: &t2
);
444 /* function prototypes for parsing function and variable declarations */
445 static int readname(struct type
*main
, char *name
, struct type
*base
);
446 static int readtype(struct type
*type
);
447 static int readdefs(void (*def
)(long data
, struct name
*name
, unsigned flags
),
449 static int readdefs_int(void (*def
)(long data
, struct name
*name
, unsigned flags
),
452 /* function prototypes for parsing initializer expressions */
453 static int initsize(void);
454 static void initexpr(struct type
*t
, int off
, void *obj
,
455 void (*set
)(void *obj
, int off
, struct type
*t
));
457 static int type_alignment(struct type
*t
)
459 if (t
->flags
& T_ARRAY
&& !t
->ptr
)
460 return type_alignment(&arrays
[t
->id
].type
);
461 if (t
->flags
& T_STRUCT
&& !t
->ptr
)
462 return type_alignment(&structs
[t
->id
].fields
[0].type
);
463 return MIN(ULNG
, type_totsz(t
));
466 static void structdef(long data
, struct name
*name
, unsigned flags
)
468 struct structinfo
*si
= &structs
[data
];
471 if (si
->size
< type_totsz(&name
->type
))
472 si
->size
= type_totsz(&name
->type
);
474 struct type
*t
= &name
->type
;
475 int alignment
= type_alignment(t
);
476 if (t
->flags
& T_ARRAY
&& !t
->ptr
)
477 alignment
= MIN(ULNG
, type_totsz(&arrays
[t
->id
].type
));
478 si
->size
= ALIGN(si
->size
, alignment
);
479 name
->addr
= si
->size
;
480 si
->size
+= type_totsz(&name
->type
);
482 memcpy(&si
->fields
[si
->nfields
++], name
, sizeof(*name
));
485 static int struct_create(char *name
, int isunion
)
487 int id
= struct_find(name
, isunion
);
489 while (tok_jmp("}")) {
490 readdefs(structdef
, id
);
496 static void readexpr(void);
498 static void enum_create(void)
502 while (tok_jmp("}")) {
504 strcpy(name
, tok_get());
509 err("const expr expected!\n");
516 /* used to differentiate labels from case and cond exprs */
520 static void readpre(void);
522 static char *tmp_str(char *buf
, int len
)
524 static char name
[NAMELEN
];
526 sprintf(name
, "__neatcc.s%d", id
++);
528 o_dscpy(o_dsnew(name
, len
+ 1, 0), buf
, len
+ 1);
532 static void readprimary(void)
534 if (tok_grp() == '0') {
536 int bt
= tok_num(tok_get(), &n
);
541 if (tok_grp() == '"') {
542 struct type t
= {}; /* char type inside the arrays */
543 struct type a
= {}; /* the char array type */
544 char *buf
= tok_get() + 1;
545 int len
= tok_len() - 2;
547 a
.id
= array_add(&t
, len
+ 1);
549 o_sym(tmp_str(buf
, len
));
553 if (tok_grp() == 'a') {
554 struct name unkn
= {""};
555 char *name
= unkn
.name
;
557 strcpy(name
, tok_iden());
558 /* don't search for labels here */
559 if (!ncexpr
&& !caseexpr
&& tok_comes(":"))
561 if ((n
= local_find(name
)) != -1) {
562 struct name
*l
= &locals
[n
];
564 ts_push_addr(&l
->type
);
567 if ((n
= global_find(name
)) != -1) {
568 struct name
*g
= &globals
[n
];
569 o_sym(*g
->elfname
? g
->elfname
: g
->name
);
570 ts_push_addr(&g
->type
);
573 if (!enum_find(&n
, name
)) {
579 err("unknown symbol <%s>\n", name
);
593 if (!t
.ptr
|| !o
.ptr
)
597 while (tok_jmp(")")) {
608 static void arrayderef(void)
614 if (!(t
.flags
& T_ARRAY
&& !t
.ptr
) && t
.addr
) {
616 o_deref(TYPE_BT(&t
));
631 static void inc_post(int op
)
633 struct type t
= ts
[nts
- 1];
634 /* pushing the value before inc */
639 /* increment by 1 or pointer size */
643 o_num(t
.ptr
> 0 ? type_szde(&t
) : 1);
647 o_assign(TYPE_BT(&t
));
651 static void readfield(void)
657 field
= struct_field(t
.id
, tok_get());
662 ts_push_addr(&field
->type
);
665 static struct funcinfo
{
666 struct type args
[NARGS
];
670 /* function and argument names; useful only when defining */
671 char argnames
[NARGS
][NAMELEN
];
674 static int funcs_n
, funcs_sz
;
676 static int func_create(struct type
*ret
, char *name
, char argnames
[][NAMELEN
],
677 struct type
*args
, int nargs
, int varg
)
681 if (funcs_n
>= funcs_sz
) {
682 funcs_sz
= MAX(128, funcs_sz
* 2);
683 funcs
= mextend(funcs
, funcs_n
, funcs_sz
, sizeof(funcs
[0]));
685 fi
= &funcs
[funcs_n
++];
686 memcpy(&fi
->ret
, ret
, sizeof(*ret
));
687 for (i
= 0; i
< nargs
; i
++)
688 memcpy(&fi
->args
[i
], &args
[i
], sizeof(*ret
));
691 strcpy(fi
->name
, name
? name
: "");
692 for (i
= 0; i
< nargs
; i
++)
693 strcpy(fi
->argnames
[i
], argnames
[i
]);
697 static void readcall(void)
703 if (t
.flags
& T_FUNC
&& t
.ptr
> 0)
705 if (!tok_comes(")")) {
710 } while (!tok_jmp(","));
713 fi
= t
.flags
& T_FUNC
? &funcs
[t
.id
] : NULL
;
714 o_call(argc
, fi
? TYPE_BT(&fi
->ret
) : SINT
);
716 if (TYPE_BT(&fi
->ret
))
717 o_cast(TYPE_BT(&fi
->ret
));
724 static void readpost(void)
738 if (!tok_jmp("++")) {
742 if (!tok_jmp("--")) {
750 if (!tok_jmp("->")) {
759 static void inc_pre(int op
)
763 /* copy the destination */
765 ts_push(&ts
[nts
- 1]);
766 /* increment by 1 or pointer size */
768 o_num(t
.ptr
> 0 ? type_szde(&t
) : 1);
770 /* assign the result */
771 o_assign(TYPE_BT(&t
));
775 static void readpre(void)
782 err("cannot use the address\n");
793 err("dereferencing non-pointer\n");
795 o_deref(TYPE_BT(&t
));
812 ts_push_bt(bt_uop(TYPE_BT(&t
)));
820 ts_push_bt(bt_uop(TYPE_BT(&t
)));
828 ts_push_bt(bt_uop(TYPE_BT(&t
)));
831 if (!tok_jmp("++")) {
835 if (!tok_jmp("--")) {
839 if (!tok_jmp("sizeof")) {
841 int op
= !tok_jmp("(");
852 o_num(type_totsz(&t
));
861 static void readmul(void)
884 static void readadd(void)
902 static void shift(int op
)
906 ts_pop_de2(NULL
, &t
);
907 o_bop(O_MK(op
, TYPE_BT(&t
)));
908 ts_push_bt(bt_uop(TYPE_BT(&t
)));
911 static void readshift(void)
915 if (!tok_jmp("<<")) {
919 if (!tok_jmp(">>")) {
927 static void cmp(int op
)
932 ts_pop_de2(&t1
, &t2
);
933 bt
= bt_op(TYPE_BT(&t1
), TYPE_BT(&t2
));
938 static void readcmp(void)
950 if (!tok_jmp("<=")) {
954 if (!tok_jmp(">=")) {
962 static void eq(int op
)
965 ts_pop_de2(NULL
, NULL
);
970 static void readeq(void)
974 if (!tok_jmp("==")) {
978 if (!tok_jmp("!=")) {
986 static void readbitand(void)
989 while (!tok_jmp("&")) {
995 static void readxor(void)
998 while (!tok_jmp("^")) {
1004 static void readbitor(void)
1007 while (!tok_jmp("|")) {
1013 static void savelocal(long val
, int bt
)
1021 static void loadlocal(long val
, int bt
)
1028 static void readand(void)
1033 if (!tok_comes("&&"))
1035 val
= o_mklocal(UINT
);
1040 while (!tok_jmp("&&")) {
1046 savelocal(val
, UINT
);
1050 savelocal(val
, UINT
);
1052 loadlocal(val
, SINT
);
1056 static void reador(void)
1061 if (!tok_comes("||"))
1063 val
= o_mklocal(UINT
);
1069 while (!tok_jmp("||")) {
1076 savelocal(val
, SINT
);
1080 savelocal(val
, SINT
);
1082 loadlocal(val
, SINT
);
1086 static void readcexpr(void);
1088 static int readcexpr_const(void)
1096 /* both branches yield the same type; so ignore the first */
1106 /* making sure t->addr == 0 on both branches */
1115 static void readcexpr(void)
1122 if (readcexpr_const()) {
1124 int l_fail
= LABEL();
1125 int l_end
= LABEL();
1129 /* both branches yield the same type; so ignore the first */
1131 if (!TYPE_VOID(&ret
)) {
1132 val
= o_mklocal(ULNG
);
1133 savelocal(val
, ULNG
);
1140 /* making sure t->addr == 0 on both branches */
1142 if (!TYPE_VOID(&ret
)) {
1143 savelocal(val
, ULNG
);
1146 if (!TYPE_VOID(&ret
)) {
1147 loadlocal(val
, ULNG
);
1153 static void opassign(int op
, int ptrop
)
1155 struct type t
= ts
[nts
- 1];
1159 if (op
== O_ADD
|| op
== O_SUB
)
1163 o_assign(TYPE_BT(&ts
[nts
- 2]));
1168 static void doassign(void)
1170 struct type t
= ts
[nts
- 1];
1171 if (!t
.ptr
&& t
.flags
& T_STRUCT
) {
1173 o_num(type_totsz(&t
));
1177 o_assign(TYPE_BT(&ts
[nts
- 1]));
1182 static void readexpr(void)
1185 if (!tok_jmp("=")) {
1190 if (!tok_jmp("+=")) {
1194 if (!tok_jmp("-=")) {
1198 if (!tok_jmp("*=")) {
1202 if (!tok_jmp("/=")) {
1206 if (!tok_jmp("%=")) {
1210 if (!tok_jmp("<<=")) {
1214 if (!tok_jmp(">>=")) {
1218 if (!tok_jmp("&=")) {
1222 if (!tok_jmp("|=")) {
1226 if (!tok_jmp("^=")) {
1232 static void readestmt(void)
1238 } while (!tok_jmp(","));
1241 #define F_GLOBAL(flags) (!((flags) & F_STATIC))
1243 static void globalinit(void *obj
, int off
, struct type
*t
)
1245 struct name
*name
= obj
;
1246 char *elfname
= *name
->elfname
? name
->elfname
: name
->name
;
1247 if (t
->flags
& T_ARRAY
&& tok_grp() == '"') {
1248 struct type
*t_de
= &arrays
[t
->id
].type
;
1249 if (!t_de
->ptr
&& !t_de
->flags
&& TYPE_SZ(t_de
) == 1) {
1250 char *buf
= tok_get() + 1;
1251 int len
= tok_len() - 2;
1253 o_dscpy(name
->addr
+ off
, buf
, len
+ 1);
1258 o_dsset(elfname
, off
, TYPE_BT(t
));
1262 static void readfunc(struct name
*name
, int flags
);
1264 static void globaldef(long data
, struct name
*name
, unsigned flags
)
1266 struct type
*t
= &name
->type
;
1267 char *elfname
= *name
->elfname
? name
->elfname
: name
->name
;
1269 if (t
->flags
& T_ARRAY
&& !t
->ptr
&& !arrays
[t
->id
].n
)
1270 if (~flags
& F_EXTERN
)
1271 arrays
[t
->id
].n
= initsize();
1273 if (!(flags
& F_EXTERN
) && (!(t
->flags
& T_FUNC
) || t
->ptr
)) {
1275 name
->addr
= o_dsnew(elfname
, sz
, F_GLOBAL(flags
));
1277 o_bsnew(elfname
, sz
, F_GLOBAL(flags
));
1281 initexpr(t
, 0, name
, globalinit
);
1282 if (tok_comes("{") && name
->type
.flags
& T_FUNC
)
1283 readfunc(name
, flags
);
1286 /* generate the address of local + off */
1287 static void o_localoff(long addr
, int off
)
1296 static void localinit(void *obj
, int off
, struct type
*t
)
1298 long addr
= *(long *) obj
;
1299 if (t
->flags
& T_ARRAY
&& tok_grp() == '"') {
1300 struct type
*t_de
= &arrays
[t
->id
].type
;
1301 if (!t_de
->ptr
&& !t_de
->flags
&& TYPE_SZ(t_de
) == 1) {
1302 char *buf
= tok_get() + 1;
1303 int len
= tok_len() - 2;
1304 o_localoff(addr
, off
);
1305 o_sym(tmp_str(buf
, len
));
1312 o_localoff(addr
, off
);
1320 /* current function name */
1321 static char func_name
[NAMELEN
];
1323 static void localdef(long data
, struct name
*name
, unsigned flags
)
1325 struct type
*t
= &name
->type
;
1326 if ((flags
& F_EXTERN
) || ((t
->flags
& T_FUNC
) && !t
->ptr
)) {
1330 if (flags
& F_STATIC
) {
1331 sprintf(name
->elfname
, "__neatcc.%s.%s", func_name
, name
->name
);
1332 globaldef(data
, name
, flags
);
1335 if (t
->flags
& T_ARRAY
&& !t
->ptr
&& !arrays
[t
->id
].n
)
1336 arrays
[t
->id
].n
= initsize();
1337 name
->addr
= o_mklocal(type_totsz(&name
->type
));
1339 if (!tok_jmp("=")) {
1340 /* this is not necessary for "struct x = y" */
1341 if (t
->flags
& (T_ARRAY
| T_STRUCT
) && !t
->ptr
) {
1342 o_local(name
->addr
);
1344 o_num(type_totsz(t
));
1348 initexpr(t
, 0, &name
->addr
, localinit
);
1352 static void typedefdef(long data
, struct name
*name
, unsigned flags
)
1354 typedef_add(name
->name
, &name
->type
);
1357 static void readstmt(void);
1359 static void readswitch(void)
1361 int o_break
= l_break
;
1362 long val_addr
= o_mklocal(ULNG
);
1364 int ncases
= 0; /* number of case labels */
1365 int l_failed
= LABEL(); /* address of last failed jmp */
1366 int l_matched
= LABEL(); /* address of last walk through jmp */
1367 int l_default
= 0; /* default case label */
1374 o_assign(TYPE_BT(&t
));
1379 while (tok_jmp("}")) {
1380 if (!tok_comes("case") && !tok_comes("default")) {
1386 if (!strcmp("case", tok_get())) {
1394 o_deref(TYPE_BT(&t
));
1401 l_default
= LABEL();
1406 l_matched
= LABEL();
1409 o_rmlocal(val_addr
, ULNG
);
1418 static char (*label_name
)[NAMELEN
];
1419 static int *label_ids
;
1420 static int label_n
, label_sz
;
1422 static int label_id(char *name
)
1425 if (label_n
>= label_sz
) {
1426 label_sz
= MAX(128, label_sz
* 2);
1427 label_name
= mextend(label_name
, label_n
, label_sz
,
1428 sizeof(label_name
[0]));
1429 label_ids
= mextend(label_ids
, label_n
, label_sz
,
1430 sizeof(label_ids
[0]));
1432 for (i
= label_n
- 1; i
>= 0; --i
)
1433 if (!strcmp(label_name
[i
], name
))
1434 return label_ids
[i
];
1435 strcpy(label_name
[label_n
], name
);
1436 label_ids
[label_n
] = LABEL();
1437 return label_ids
[label_n
++];
1440 static void readstmt(void)
1444 if (!tok_jmp("{")) {
1445 int _nlocals
= locals_n
;
1446 int _nglobals
= globals_n
;
1447 int _nenums
= enums_n
;
1448 int _ntypedefs
= typedefs_n
;
1449 int _nstructs
= structs_n
;
1450 int _nfuncs
= funcs_n
;
1451 int _narrays
= arrays_n
;
1452 while (tok_jmp("}"))
1454 locals_n
= _nlocals
;
1456 typedefs_n
= _ntypedefs
;
1457 structs_n
= _nstructs
;
1459 arrays_n
= _narrays
;
1460 globals_n
= _nglobals
;
1463 if (!readdefs(localdef
, 0)) {
1467 if (!tok_jmp("typedef")) {
1468 readdefs(typedefdef
, 0);
1472 if (!tok_jmp("if")) {
1473 int l_fail
= LABEL();
1474 int l_end
= LABEL();
1481 if (!tok_jmp("else")) {
1491 if (!tok_jmp("while")) {
1492 int o_break
= l_break
;
1493 int o_cont
= l_cont
;
1509 if (!tok_jmp("do")) {
1510 int o_break
= l_break
;
1511 int o_cont
= l_cont
;
1512 int l_beg
= LABEL();
1531 if (!tok_jmp("for")) {
1532 int o_break
= l_break
;
1533 int o_cont
= l_cont
;
1534 int l_check
= LABEL(); /* for condition label */
1535 int l_body
= LABEL(); /* for block label */
1539 if (!tok_comes(";"))
1543 if (!tok_comes(";")) {
1551 if (!tok_comes(")"))
1563 if (!tok_jmp("switch")) {
1567 if (!tok_jmp("return")) {
1568 int ret
= !tok_comes(";");
1577 if (!tok_jmp("break")) {
1582 if (!tok_jmp("continue")) {
1587 if (!tok_jmp("goto")) {
1588 o_jmp(label_id(tok_get()));
1594 if (!tok_jmp(":")) {
1595 o_label(label_id(tok_previden
));
1601 static void readfunc(struct name
*name
, int flags
)
1603 struct funcinfo
*fi
= &funcs
[name
->type
.id
];
1605 strcpy(func_name
, fi
->name
);
1606 o_func_beg(func_name
, fi
->nargs
, F_GLOBAL(flags
), fi
->varg
);
1607 for (i
= 0; i
< fi
->nargs
; i
++) {
1608 struct name arg
= {"", "", fi
->args
[i
], o_arg2loc(i
)};
1609 strcpy(arg
.name
, fi
->argnames
[i
]);
1616 func_name
[0] = '\0';
1620 static void readdecl(void)
1622 if (!tok_jmp("typedef")) {
1623 readdefs(typedefdef
, 0);
1627 readdefs_int(globaldef
, 0);
1631 static void parse(void)
1637 static void compat_macros(void)
1639 cpp_define("__STDC__", "");
1640 cpp_define("__linux__", "");
1641 cpp_define(I_ARCH
, "");
1643 /* ignored keywords */
1644 cpp_define("const", "");
1645 cpp_define("register", "");
1646 cpp_define("volatile", "");
1647 cpp_define("inline", "");
1648 cpp_define("restrict", "");
1649 cpp_define("__inline__", "");
1650 cpp_define("__restrict__", "");
1651 cpp_define("__attribute__(x)", "");
1652 cpp_define("__builtin_va_list__", "long");
1655 static int ncc_opt
= 2;
1657 /* return one if the given optimization level is enabled */
1660 return level
<= ncc_opt
;
1663 int main(int argc
, char *argv
[])
1669 for (i
= 1; i
< argc
&& argv
[i
][0] == '-'; i
++) {
1670 if (argv
[i
][1] == 'I')
1671 cpp_path(argv
[i
][2] ? argv
[i
] + 2 : argv
[++i
]);
1672 if (argv
[i
][1] == 'O')
1673 ncc_opt
= argv
[i
][2] ? atoi(argv
[i
] + 2) : 2;
1674 if (argv
[i
][1] == 'D') {
1675 char *name
= argv
[i
] + 2;
1677 char *eq
= strchr(name
, '=');
1682 cpp_define(name
, def
);
1684 if (argv
[i
][1] == 'o')
1685 strcpy(obj
, argv
[i
][2] ? argv
[i
] + 2 : argv
[++i
]);
1686 if (argv
[i
][1] == 'h') {
1687 printf("neatcc: [options] source\n");
1689 printf("Options:\n");
1690 printf(" -I dir \tspecify a header directory\n");
1691 printf(" -o out \tspecify output file name\n");
1692 printf(" -Dname=val \tdefine macro\n");
1693 printf(" -On \toptimize (-O0 to disable)\n");
1698 die("neatcc: no file given\n");
1699 if (cpp_init(argv
[i
]))
1700 die("neatcc: cannot open <%s>\n", argv
[i
]);
1704 strcpy(obj
, argv
[i
]);
1705 obj
[strlen(obj
) - 1] = 'o';
1716 ofd
= open(obj
, O_WRONLY
| O_TRUNC
| O_CREAT
, 0600);
1723 /* parsing function and variable declarations */
1725 /* read the base type of a variable */
1726 static int basetype(struct type
*type
, unsigned *flags
)
1733 char name
[NAMELEN
] = "";
1739 if (!tok_jmp("static")) {
1741 } else if (!tok_jmp("extern")) {
1743 } else if (!tok_jmp("void")) {
1747 } else if (!tok_jmp("int")) {
1749 } else if (!tok_jmp("char")) {
1752 } else if (!tok_jmp("short")) {
1754 } else if (!tok_jmp("long")) {
1756 } else if (!tok_jmp("signed")) {
1758 } else if (!tok_jmp("unsigned")) {
1760 } else if (tok_comes("union") || tok_comes("struct")) {
1761 isunion
= !strcmp("union", tok_get());
1762 if (tok_grp() == 'a')
1763 strcpy(name
, tok_get());
1765 type
->id
= struct_create(name
, isunion
);
1767 type
->id
= struct_find(name
, isunion
);
1768 type
->flags
|= T_STRUCT
;
1771 } else if (!tok_jmp("enum")) {
1772 if (tok_grp() == 'a')
1779 if (tok_grp() == 'a') {
1780 int id
= typedef_find(tok_see());
1783 memcpy(type
, &typedefs
[id
].type
,
1795 type
->bt
= size
| (sign
? T_MSIGN
: 0);
1799 static void readptrs(struct type
*type
)
1801 while (!tok_jmp("*")) {
1808 /* read function arguments */
1809 static int readargs(struct type
*args
, char argnames
[][NAMELEN
], int *varg
)
1814 while (!tok_comes(")")) {
1815 if (!tok_jmp("...")) {
1819 if (readname(&args
[nargs
], argnames
[nargs
], NULL
)) {
1820 /* argument has no type, assume int */
1821 memset(&args
[nargs
], 0, sizeof(struct type
));
1822 args
[nargs
].bt
= SINT
;
1823 strcpy(argnames
[nargs
], tok_get());
1825 /* argument arrays are pointers */
1826 array2ptr(&args
[nargs
]);
1833 if (nargs
== 1 && !TYPE_BT(&args
[0]))
1838 /* read K&R function arguments */
1839 static void krdef(long data
, struct name
*name
, unsigned flags
)
1841 struct funcinfo
*fi
= &funcs
[data
];
1843 for (i
= 0; i
< fi
->nargs
; i
++)
1844 if (!strcmp(fi
->argnames
[i
], name
->name
))
1845 memcpy(&fi
->args
[i
], &name
->type
, sizeof(name
->type
));
1849 * readarrays() parses array specifiers when reading a definition in
1850 * readname(). The "type" parameter contains the type contained in the
1851 * inner array; for instance, type in "int *a[10][20]" would be an int
1852 * pointer. When returning, the "type" parameter is changed to point
1853 * to the final array. The function returns a pointer to the type in
1854 * the inner array; this is useful when the type is not complete yet,
1855 * like when creating an array of function pointers as in
1856 * "int (*f[10])(int)". If there is no array brackets, NULL is returned.
1858 static struct type
*readarrays(struct type
*type
)
1861 struct type
*inner
= NULL
;
1864 while (!tok_jmp("[")) {
1870 err("const expr expected\n");
1875 for (i
= nar
- 1; i
>= 0; i
--) {
1876 type
->id
= array_add(type
, arsz
[i
]);
1878 inner
= &arrays
[type
->id
].type
;
1879 type
->flags
= T_ARRAY
;
1886 static struct type
*innertype(struct type
*t
)
1888 while (t
->flags
& T_ARRAY
&& !t
->ptr
)
1889 t
= &arrays
[t
->id
].type
;
1893 static void innertype_modify(struct type
*t
, struct type
*s
)
1895 struct type
*inner
= innertype(t
);
1896 int ptr
= inner
->ptr
;
1897 memcpy(inner
, s
, sizeof(*inner
));
1902 * readname() reads a variable definition; the name is copied into
1903 * "name" and the type is copied into "main" argument. The "base"
1904 * argument, if not NULL, indicates the base type of the variable.
1905 * For instance, the base type of "a" and "b" in "int *a, b[10]" is
1906 * "int". If NULL, basetype() is called directly to read the base
1907 * type of the variable. readname() returns zero, only if the
1908 * variable can be read.
1910 static int readname(struct type
*main
, char *name
, struct type
*base
)
1912 struct type type
; /* the main type */
1913 struct type btype
; /* type before parenthesis; e.g. "int *" in "int *(*p)[10] */
1919 if (basetype(&type
, &flags
))
1925 paren
= !tok_jmp("(");
1930 if (tok_grp() == 'a' && name
)
1931 strcpy(name
, tok_get());
1935 if (tok_comes("(")) {
1936 struct type args
[NARGS
];
1937 char argnames
[NARGS
][NAMELEN
];
1939 int nargs
= readargs(args
, argnames
, &varg
);
1940 struct type rtype
= type
; /* return type */
1941 struct type ftype
= {0}; /* function type */
1944 ftype
.flags
= T_FUNC
;
1946 ftype
.id
= func_create(&rtype
, name
, argnames
, args
, nargs
, varg
);
1948 innertype_modify(&type
, &ftype
);
1951 if (!tok_comes(";"))
1952 while (!tok_comes("{") && !readdefs(krdef
, type
.id
))
1955 if (paren
&& readarrays(&btype
))
1956 innertype_modify(&type
, &btype
);
1962 static int readtype(struct type
*type
)
1964 return readname(type
, NULL
, NULL
);
1968 * readdefs() reads a variable definition statement. The definition
1969 * can appear in different contexts: global variables, function
1970 * local variables, struct fields, and typedefs. For each defined
1971 * variable, def() callback is called with the appropriate name
1972 * struct and flags; the callback should finish parsing the definition
1973 * by possibly reading the initializer expression and saving the name
1976 static int readdefs(void (*def
)(long data
, struct name
*name
, unsigned flags
),
1980 unsigned base_flags
;
1981 if (basetype(&base
, &base_flags
))
1983 if (tok_comes(";") || tok_comes("{"))
1986 struct name name
= {{""}};
1987 if (readname(&name
.type
, name
.name
, &base
))
1989 def(data
, &name
, base_flags
);
1990 } while (!tok_jmp(","));
1994 /* just like readdefs, but default to int type; for handling K&R functions */
1995 static int readdefs_int(void (*def
)(long data
, struct name
*name
, unsigned flags
),
2000 if (basetype(&base
, &flags
)) {
2001 if (tok_grp() != 'a')
2003 memset(&base
, 0, sizeof(base
));
2006 if (!tok_comes(";")) {
2008 struct name name
= {{""}};
2009 if (readname(&name
.type
, name
.name
, &base
))
2011 def(data
, &name
, flags
);
2012 } while (!tok_jmp(","));
2018 /* parsing initializer expressions */
2020 static void jumpbrace(void)
2023 while (!tok_comes("}") || depth
--)
2024 if (!strcmp("{", tok_get()))
2029 /* compute the size of the initializer expression */
2030 static int initsize(void)
2032 long addr
= tok_addr();
2036 if (tok_grp() == '"') {
2038 n
= tok_len() - 2 + 1;
2043 while (tok_jmp("}")) {
2045 if (!tok_jmp("[")) {
2054 while (!tok_comes("}") && !tok_comes(","))
2055 if (!strcmp("{", tok_get()))
2063 /* read the initializer expression and initialize basic types using set() cb */
2064 static void initexpr(struct type
*t
, int off
, void *obj
,
2065 void (*set
)(void *obj
, int off
, struct type
*t
))
2071 if (!t
->ptr
&& t
->flags
& T_STRUCT
) {
2072 struct structinfo
*si
= &structs
[t
->id
];
2074 for (i
= 0; i
< si
->nfields
&& !tok_comes("}"); i
++) {
2075 struct name
*field
= &si
->fields
[i
];
2076 if (!tok_jmp(".")) {
2077 field
= struct_field(t
->id
, tok_get());
2080 initexpr(&field
->type
, off
+ field
->addr
, obj
, set
);
2084 } else if (t
->flags
& T_ARRAY
) {
2085 struct type t_de
= arrays
[t
->id
].type
;
2087 /* handling extra braces as in: char s[] = {"sth"} */
2088 if (TYPE_SZ(&t_de
) == 1 && tok_grp() == '"') {
2093 for (i
= 0; !tok_comes("}"); i
++) {
2095 struct type it
= t_de
;
2096 if (!tok_jmp("[")) {
2103 if (!tok_comes("{") && (tok_grp() != '"' ||
2104 !(it
.flags
& T_ARRAY
)))
2105 it
= *innertype(&t_de
);
2106 initexpr(&it
, off
+ type_totsz(&it
) * idx
, obj
, set
);