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 /* Type conversion according to ISO9899, sec. 6.3.1.8.
388 * http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf
391 * Size: Always convert to largest size.
393 * - Same sign: Keep sign.
394 * - Same size or unsigned larger: Convert to unsigned.
395 * - Signed larger: Convert to signed.
397 int sz
= MAX(T_SZ(bt1
), T_SZ(bt2
));
398 /* the unsigned operand is at least as large as the signed one */
399 if ((!T_SG(bt1
) && T_SG(bt2
) && T_SZ(bt1
) >= T_SZ(bt2
)) ||
400 (T_SG(bt1
) && !T_SG(bt2
) && T_SZ(bt1
) <= T_SZ(bt2
)))
401 return MAX(sz
, UINT
);
402 return T_SG(bt1
) | T_SG(bt2
) | MAX(sz
, UINT
);
405 /* the result of a unary operation on variables of bt */
406 static unsigned bt_uop(unsigned bt
)
408 return bt_op(bt
, SINT
);
411 /* push the result of a binary operation on the type stack */
412 static void ts_binop(int op
)
415 unsigned bt1
, bt2
, bt
;
416 ts_pop_de2(&t1
, &t2
);
419 bt
= bt_op(bt1
, bt2
);
420 if (op
== O_DIV
|| op
== O_MOD
)
421 bt
= T_MK(bt2
& T_MSIGN
, bt
);
426 /* push the result of an additive binary operation on the type stack */
427 static void ts_addop(int op
)
430 ts_pop_de2(&t1
, &t2
);
431 if (!t1
.ptr
&& !t2
.ptr
) {
433 ts_push_bt(bt_op(TYPE_BT(&t1
), TYPE_BT(&t2
)));
436 if (t1
.ptr
&& !t2
.ptr
)
438 if (!t1
.ptr
&& t2
.ptr
)
439 if (type_szde(&t2
) > 1) {
440 o_num(type_szde(&t2
));
443 if (t1
.ptr
&& !t2
.ptr
)
446 if (t1
.ptr
&& t2
.ptr
) {
447 int sz
= type_szde(&t1
);
454 ts_push(t1
.ptr
? &t1
: &t2
);
458 /* function prototypes for parsing function and variable declarations */
459 static int readname(struct type
*main
, char *name
, struct type
*base
);
460 static int readtype(struct type
*type
);
461 static int readdefs(void (*def
)(long data
, struct name
*name
, unsigned flags
),
463 static int readdefs_int(void (*def
)(long data
, struct name
*name
, unsigned flags
),
466 /* function prototypes for parsing initializer expressions */
467 static int initsize(void);
468 static void initexpr(struct type
*t
, int off
, void *obj
,
469 void (*set
)(void *obj
, int off
, struct type
*t
));
471 static int type_alignment(struct type
*t
)
473 if (t
->flags
& T_ARRAY
&& !t
->ptr
)
474 return type_alignment(&arrays
[t
->id
].type
);
475 if (t
->flags
& T_STRUCT
&& !t
->ptr
)
476 return type_alignment(&structs
[t
->id
].fields
[0].type
);
477 return MIN(ULNG
, type_totsz(t
));
480 static void structdef(long data
, struct name
*name
, unsigned flags
)
482 struct structinfo
*si
= &structs
[data
];
485 if (si
->size
< type_totsz(&name
->type
))
486 si
->size
= type_totsz(&name
->type
);
488 struct type
*t
= &name
->type
;
489 int alignment
= type_alignment(t
);
490 if (t
->flags
& T_ARRAY
&& !t
->ptr
)
491 alignment
= MIN(ULNG
, type_totsz(&arrays
[t
->id
].type
));
492 si
->size
= ALIGN(si
->size
, alignment
);
493 name
->addr
= si
->size
;
494 si
->size
+= type_totsz(&name
->type
);
496 memcpy(&si
->fields
[si
->nfields
++], name
, sizeof(*name
));
499 static int struct_create(char *name
, int isunion
)
501 int id
= struct_find(name
, isunion
);
503 while (tok_jmp("}")) {
504 readdefs(structdef
, id
);
510 static void readexpr(void);
512 static void enum_create(void)
516 while (tok_jmp("}")) {
518 strcpy(name
, tok_get());
523 err("const expr expected!\n");
530 /* used to differentiate labels from case and cond exprs */
534 static void readpre(void);
536 static char *tmp_str(char *buf
, int len
)
538 static char name
[NAMELEN
];
540 sprintf(name
, "__neatcc.s%d", id
++);
542 o_dscpy(o_dsnew(name
, len
+ 1, 0), buf
, len
+ 1);
546 static void readprimary(void)
548 if (tok_grp() == '0') {
550 int bt
= tok_num(tok_get(), &n
);
555 if (tok_grp() == '"') {
556 struct type t
= {}; /* char type inside the arrays */
557 struct type a
= {}; /* the char array type */
558 char *buf
= tok_get() + 1;
559 int len
= tok_len() - 2;
561 a
.id
= array_add(&t
, len
+ 1);
563 o_sym(tmp_str(buf
, len
));
567 if (tok_grp() == 'a') {
568 struct name unkn
= {""};
569 char *name
= unkn
.name
;
571 strcpy(name
, tok_iden());
572 /* don't search for labels here */
573 if (!ncexpr
&& !caseexpr
&& tok_comes(":"))
575 if ((n
= local_find(name
)) != -1) {
576 struct name
*l
= &locals
[n
];
578 ts_push_addr(&l
->type
);
581 if ((n
= global_find(name
)) != -1) {
582 struct name
*g
= &globals
[n
];
583 o_sym(*g
->elfname
? g
->elfname
: g
->name
);
584 ts_push_addr(&g
->type
);
587 if (!enum_find(&n
, name
)) {
593 err("unknown symbol <%s>\n", name
);
607 if (!t
.ptr
|| !o
.ptr
)
611 while (tok_jmp(")")) {
622 static void arrayderef(void)
628 if (!(t
.flags
& T_ARRAY
&& !t
.ptr
) && t
.addr
) {
630 o_deref(TYPE_BT(&t
));
645 static void inc_post(int op
)
647 struct type t
= ts
[nts
- 1];
648 /* pushing the value before inc */
653 /* increment by 1 or pointer size */
657 o_num(t
.ptr
> 0 ? type_szde(&t
) : 1);
661 o_assign(TYPE_BT(&t
));
665 static void readfield(void)
671 field
= struct_field(t
.id
, tok_get());
676 ts_push_addr(&field
->type
);
679 static struct funcinfo
{
680 struct type args
[NARGS
];
684 /* function and argument names; useful only when defining */
685 char argnames
[NARGS
][NAMELEN
];
688 static int funcs_n
, funcs_sz
;
690 static int func_create(struct type
*ret
, char *name
, char argnames
[][NAMELEN
],
691 struct type
*args
, int nargs
, int varg
)
695 if (funcs_n
>= funcs_sz
) {
696 funcs_sz
= MAX(128, funcs_sz
* 2);
697 funcs
= mextend(funcs
, funcs_n
, funcs_sz
, sizeof(funcs
[0]));
699 fi
= &funcs
[funcs_n
++];
700 memcpy(&fi
->ret
, ret
, sizeof(*ret
));
701 for (i
= 0; i
< nargs
; i
++)
702 memcpy(&fi
->args
[i
], &args
[i
], sizeof(*ret
));
705 strcpy(fi
->name
, name
? name
: "");
706 for (i
= 0; i
< nargs
; i
++)
707 strcpy(fi
->argnames
[i
], argnames
[i
]);
711 static void readcall(void)
717 if (t
.flags
& T_FUNC
&& t
.ptr
> 0)
719 if (!tok_comes(")")) {
724 } while (!tok_jmp(","));
727 fi
= t
.flags
& T_FUNC
? &funcs
[t
.id
] : NULL
;
728 o_call(argc
, fi
? TYPE_BT(&fi
->ret
) : SINT
);
730 if (TYPE_BT(&fi
->ret
))
731 o_cast(TYPE_BT(&fi
->ret
));
738 static void readpost(void)
752 if (!tok_jmp("++")) {
756 if (!tok_jmp("--")) {
764 if (!tok_jmp("->")) {
773 static void inc_pre(int op
)
777 /* copy the destination */
779 ts_push(&ts
[nts
- 1]);
780 /* increment by 1 or pointer size */
782 o_num(t
.ptr
> 0 ? type_szde(&t
) : 1);
784 /* assign the result */
785 o_assign(TYPE_BT(&t
));
789 static void readpre(void)
796 err("cannot use the address\n");
807 err("dereferencing non-pointer\n");
809 o_deref(TYPE_BT(&t
));
826 ts_push_bt(bt_uop(TYPE_BT(&t
)));
834 ts_push_bt(bt_uop(TYPE_BT(&t
)));
842 ts_push_bt(bt_uop(TYPE_BT(&t
)));
845 if (!tok_jmp("++")) {
849 if (!tok_jmp("--")) {
853 if (!tok_jmp("sizeof")) {
855 int op
= !tok_jmp("(");
866 o_num(type_totsz(&t
));
875 static void readmul(void)
898 static void readadd(void)
916 static void shift(int op
)
920 ts_pop_de2(NULL
, &t
);
921 o_bop(O_MK(op
, TYPE_BT(&t
)));
922 ts_push_bt(bt_uop(TYPE_BT(&t
)));
925 static void readshift(void)
929 if (!tok_jmp("<<")) {
933 if (!tok_jmp(">>")) {
941 static void cmp(int op
)
946 ts_pop_de2(&t1
, &t2
);
947 bt
= bt_op(TYPE_BT(&t1
), TYPE_BT(&t2
));
952 static void readcmp(void)
964 if (!tok_jmp("<=")) {
968 if (!tok_jmp(">=")) {
976 static void eq(int op
)
979 ts_pop_de2(NULL
, NULL
);
984 static void readeq(void)
988 if (!tok_jmp("==")) {
992 if (!tok_jmp("!=")) {
1000 static void readbitand(void)
1003 while (!tok_jmp("&")) {
1009 static void readxor(void)
1012 while (!tok_jmp("^")) {
1018 static void readbitor(void)
1021 while (!tok_jmp("|")) {
1027 static void savelocal(long val
, int bt
)
1035 static void loadlocal(long val
, int bt
)
1042 static void readand(void)
1047 if (!tok_comes("&&"))
1049 val
= o_mklocal(UINT
);
1054 while (!tok_jmp("&&")) {
1060 savelocal(val
, UINT
);
1064 savelocal(val
, UINT
);
1066 loadlocal(val
, SINT
);
1070 static void reador(void)
1075 if (!tok_comes("||"))
1077 val
= o_mklocal(UINT
);
1083 while (!tok_jmp("||")) {
1090 savelocal(val
, SINT
);
1094 savelocal(val
, SINT
);
1096 loadlocal(val
, SINT
);
1100 static void readcexpr(void);
1102 static int readcexpr_const(void)
1110 /* both branches yield the same type; so ignore the first */
1120 /* making sure t->addr == 0 on both branches */
1129 static void readcexpr(void)
1136 if (readcexpr_const()) {
1138 int l_fail
= LABEL();
1139 int l_end
= LABEL();
1143 /* both branches yield the same type; so ignore the first */
1145 if (!TYPE_VOID(&ret
)) {
1146 val
= o_mklocal(ULNG
);
1147 savelocal(val
, ULNG
);
1154 /* making sure t->addr == 0 on both branches */
1156 if (!TYPE_VOID(&ret
)) {
1157 savelocal(val
, ULNG
);
1160 if (!TYPE_VOID(&ret
)) {
1161 loadlocal(val
, ULNG
);
1167 static void opassign(int op
, int ptrop
)
1169 struct type t
= ts
[nts
- 1];
1173 if (op
== O_ADD
|| op
== O_SUB
)
1177 o_assign(TYPE_BT(&ts
[nts
- 2]));
1182 static void doassign(void)
1184 struct type t
= ts
[nts
- 1];
1185 if (!t
.ptr
&& t
.flags
& T_STRUCT
) {
1187 o_num(type_totsz(&t
));
1191 o_assign(TYPE_BT(&ts
[nts
- 1]));
1196 static void readexpr(void)
1199 if (!tok_jmp("=")) {
1204 if (!tok_jmp("+=")) {
1208 if (!tok_jmp("-=")) {
1212 if (!tok_jmp("*=")) {
1216 if (!tok_jmp("/=")) {
1220 if (!tok_jmp("%=")) {
1224 if (!tok_jmp("<<=")) {
1228 if (!tok_jmp(">>=")) {
1232 if (!tok_jmp("&=")) {
1236 if (!tok_jmp("|=")) {
1240 if (!tok_jmp("^=")) {
1246 static void readestmt(void)
1252 } while (!tok_jmp(","));
1255 #define F_GLOBAL(flags) (!((flags) & F_STATIC))
1257 static void globalinit(void *obj
, int off
, struct type
*t
)
1259 struct name
*name
= obj
;
1260 char *elfname
= *name
->elfname
? name
->elfname
: name
->name
;
1261 if (t
->flags
& T_ARRAY
&& tok_grp() == '"') {
1262 struct type
*t_de
= &arrays
[t
->id
].type
;
1263 if (!t_de
->ptr
&& !t_de
->flags
&& TYPE_SZ(t_de
) == 1) {
1264 char *buf
= tok_get() + 1;
1265 int len
= tok_len() - 2;
1267 o_dscpy(name
->addr
+ off
, buf
, len
+ 1);
1273 o_dsset(elfname
, off
, TYPE_BT(t
));
1277 static void readfunc(struct name
*name
, int flags
);
1279 static void globaldef(long data
, struct name
*name
, unsigned flags
)
1281 struct type
*t
= &name
->type
;
1282 char *elfname
= *name
->elfname
? name
->elfname
: name
->name
;
1284 if (t
->flags
& T_ARRAY
&& !t
->ptr
&& !arrays
[t
->id
].n
)
1285 if (~flags
& F_EXTERN
)
1286 arrays
[t
->id
].n
= initsize();
1288 if (!(flags
& F_EXTERN
) && (!(t
->flags
& T_FUNC
) || t
->ptr
)) {
1290 name
->addr
= o_dsnew(elfname
, sz
, F_GLOBAL(flags
));
1292 o_bsnew(elfname
, sz
, F_GLOBAL(flags
));
1296 initexpr(t
, 0, name
, globalinit
);
1297 if (tok_comes("{") && name
->type
.flags
& T_FUNC
)
1298 readfunc(name
, flags
);
1301 /* generate the address of local + off */
1302 static void o_localoff(long addr
, int off
)
1311 static void localinit(void *obj
, int off
, struct type
*t
)
1313 long addr
= *(long *) obj
;
1314 if (t
->flags
& T_ARRAY
&& tok_grp() == '"') {
1315 struct type
*t_de
= &arrays
[t
->id
].type
;
1316 if (!t_de
->ptr
&& !t_de
->flags
&& TYPE_SZ(t_de
) == 1) {
1317 char *buf
= tok_get() + 1;
1318 int len
= tok_len() - 2;
1319 o_localoff(addr
, off
);
1320 o_sym(tmp_str(buf
, len
));
1327 o_localoff(addr
, off
);
1335 /* current function name */
1336 static char func_name
[NAMELEN
];
1338 static void localdef(long data
, struct name
*name
, unsigned flags
)
1340 struct type
*t
= &name
->type
;
1341 if ((flags
& F_EXTERN
) || ((t
->flags
& T_FUNC
) && !t
->ptr
)) {
1345 if (flags
& F_STATIC
) {
1346 sprintf(name
->elfname
, "__neatcc.%s.%s", func_name
, name
->name
);
1347 globaldef(data
, name
, flags
);
1350 if (t
->flags
& T_ARRAY
&& !t
->ptr
&& !arrays
[t
->id
].n
)
1351 arrays
[t
->id
].n
= initsize();
1352 name
->addr
= o_mklocal(type_totsz(&name
->type
));
1354 if (!tok_jmp("=")) {
1355 /* this is not necessary for "struct x = y" */
1356 if (t
->flags
& (T_ARRAY
| T_STRUCT
) && !t
->ptr
) {
1357 o_local(name
->addr
);
1359 o_num(type_totsz(t
));
1363 initexpr(t
, 0, &name
->addr
, localinit
);
1367 static void typedefdef(long data
, struct name
*name
, unsigned flags
)
1369 typedef_add(name
->name
, &name
->type
);
1372 static void readstmt(void);
1374 static void readswitch(void)
1376 int o_break
= l_break
;
1377 long val_addr
= o_mklocal(ULNG
);
1379 int ncases
= 0; /* number of case labels */
1380 int l_failed
= LABEL(); /* address of last failed jmp */
1381 int l_matched
= LABEL(); /* address of last walk through jmp */
1382 int l_default
= 0; /* default case label */
1389 o_assign(TYPE_BT(&t
));
1393 while (tok_jmp("}")) {
1394 if (!tok_comes("case") && !tok_comes("default")) {
1400 if (!strcmp("case", tok_get())) {
1408 o_deref(TYPE_BT(&t
));
1415 l_default
= LABEL();
1420 l_matched
= LABEL();
1423 o_rmlocal(val_addr
, ULNG
);
1432 static char (*label_name
)[NAMELEN
];
1433 static int *label_ids
;
1434 static int label_n
, label_sz
;
1436 static int label_id(char *name
)
1439 if (label_n
>= label_sz
) {
1440 label_sz
= MAX(128, label_sz
* 2);
1441 label_name
= mextend(label_name
, label_n
, label_sz
,
1442 sizeof(label_name
[0]));
1443 label_ids
= mextend(label_ids
, label_n
, label_sz
,
1444 sizeof(label_ids
[0]));
1446 for (i
= label_n
- 1; i
>= 0; --i
)
1447 if (!strcmp(label_name
[i
], name
))
1448 return label_ids
[i
];
1449 strcpy(label_name
[label_n
], name
);
1450 label_ids
[label_n
] = LABEL();
1451 return label_ids
[label_n
++];
1454 static void readstmt(void)
1458 if (!tok_jmp("{")) {
1459 int _nlocals
= locals_n
;
1460 int _nglobals
= globals_n
;
1461 int _nenums
= enums_n
;
1462 int _ntypedefs
= typedefs_n
;
1463 int _nstructs
= structs_n
;
1464 int _nfuncs
= funcs_n
;
1465 int _narrays
= arrays_n
;
1466 while (tok_jmp("}"))
1468 locals_n
= _nlocals
;
1470 typedefs_n
= _ntypedefs
;
1471 structs_n
= _nstructs
;
1473 arrays_n
= _narrays
;
1474 globals_n
= _nglobals
;
1477 if (!readdefs(localdef
, 0)) {
1481 if (!tok_jmp("typedef")) {
1482 readdefs(typedefdef
, 0);
1486 if (!tok_jmp("if")) {
1487 int l_fail
= LABEL();
1488 int l_end
= LABEL();
1495 if (!tok_jmp("else")) {
1505 if (!tok_jmp("while")) {
1506 int o_break
= l_break
;
1507 int o_cont
= l_cont
;
1523 if (!tok_jmp("do")) {
1524 int o_break
= l_break
;
1525 int o_cont
= l_cont
;
1526 int l_beg
= LABEL();
1545 if (!tok_jmp("for")) {
1546 int o_break
= l_break
;
1547 int o_cont
= l_cont
;
1548 int l_check
= LABEL(); /* for condition label */
1549 int l_body
= LABEL(); /* for block label */
1553 if (!tok_comes(";"))
1557 if (!tok_comes(";")) {
1565 if (!tok_comes(")"))
1577 if (!tok_jmp("switch")) {
1581 if (!tok_jmp("return")) {
1582 int ret
= !tok_comes(";");
1591 if (!tok_jmp("break")) {
1596 if (!tok_jmp("continue")) {
1601 if (!tok_jmp("goto")) {
1602 o_jmp(label_id(tok_get()));
1608 if (!tok_jmp(":")) {
1609 o_label(label_id(tok_previden
));
1615 static void readfunc(struct name
*name
, int flags
)
1617 struct funcinfo
*fi
= &funcs
[name
->type
.id
];
1619 strcpy(func_name
, fi
->name
);
1620 o_func_beg(func_name
, fi
->nargs
, F_GLOBAL(flags
), fi
->varg
);
1621 for (i
= 0; i
< fi
->nargs
; i
++) {
1622 struct name arg
= {"", "", fi
->args
[i
], o_arg2loc(i
)};
1623 strcpy(arg
.name
, fi
->argnames
[i
]);
1630 func_name
[0] = '\0';
1634 static void readdecl(void)
1636 if (!tok_jmp("typedef")) {
1637 readdefs(typedefdef
, 0);
1641 readdefs_int(globaldef
, 0);
1645 static void parse(void)
1651 static void compat_macros(void)
1653 cpp_define("__STDC__", "");
1654 cpp_define("__linux__", "");
1655 cpp_define(I_ARCH
, "");
1656 cpp_define("__neatcc__", "");
1658 /* ignored keywords */
1659 cpp_define("const", "");
1660 cpp_define("register", "");
1661 cpp_define("volatile", "");
1662 cpp_define("inline", "");
1663 cpp_define("restrict", "");
1664 cpp_define("__inline__", "");
1665 cpp_define("__restrict__", "");
1666 cpp_define("__attribute__(x)", "");
1667 cpp_define("__builtin_va_list__", "long");
1670 static int ncc_opt
= 2;
1672 /* return one if the given optimization level is enabled */
1675 return level
<= ncc_opt
;
1678 int main(int argc
, char *argv
[])
1685 for (i
= 1; i
< argc
&& argv
[i
][0] == '-'; i
++) {
1686 if (argv
[i
][1] == 'I')
1687 cpp_path(argv
[i
][2] ? argv
[i
] + 2 : argv
[++i
]);
1688 if (argv
[i
][1] == 'O')
1689 ncc_opt
= argv
[i
][2] ? atoi(argv
[i
] + 2) : 2;
1690 if (argv
[i
][1] == 'E')
1692 if (argv
[i
][1] == 'D') {
1693 char *name
= argv
[i
] + 2;
1695 char *eq
= strchr(name
, '=');
1700 cpp_define(name
, def
);
1702 if (argv
[i
][1] == 'o')
1703 strcpy(obj
, argv
[i
][2] ? argv
[i
] + 2 : argv
[++i
]);
1704 if (argv
[i
][1] == 'h') {
1705 printf("Usage: %s [options] source\n", argv
[0]);
1707 printf("Options:\n");
1708 printf(" -I dir \tspecify a header directory\n");
1709 printf(" -o out \tspecify output file name\n");
1710 printf(" -E \tpreprocess only\n");
1711 printf(" -Dname=val \tdefine a macro\n");
1712 printf(" -On \toptimize (-O0 to disable)\n");
1717 die("neatcc: no file given\n");
1718 if (cpp_init(argv
[i
]))
1719 die("neatcc: cannot open <%s>\n", argv
[i
]);
1724 ofd
= open(obj
, O_WRONLY
| O_TRUNC
| O_CREAT
, 0600);
1725 while (!cpp_read(&cbuf
, &clen
))
1726 write(ofd
, cbuf
, clen
);
1734 char *cp
= strrchr(argv
[i
], '/');
1735 strcpy(obj
, cp
? cp
+ 1 : argv
[i
]);
1736 obj
[strlen(obj
) - 1] = 'o';
1747 ofd
= open(obj
, O_WRONLY
| O_TRUNC
| O_CREAT
, 0600);
1754 /* parsing function and variable declarations */
1756 /* read the base type of a variable */
1757 static int basetype(struct type
*type
, unsigned *flags
)
1764 char name
[NAMELEN
] = "";
1770 if (!tok_jmp("static")) {
1772 } else if (!tok_jmp("extern")) {
1774 } else if (!tok_jmp("void")) {
1778 } else if (!tok_jmp("int")) {
1780 } else if (!tok_jmp("char")) {
1783 } else if (!tok_jmp("short")) {
1785 } else if (!tok_jmp("long")) {
1787 } else if (!tok_jmp("signed")) {
1789 } else if (!tok_jmp("unsigned")) {
1791 } else if (tok_comes("union") || tok_comes("struct")) {
1792 isunion
= !strcmp("union", tok_get());
1793 if (tok_grp() == 'a')
1794 strcpy(name
, tok_get());
1796 type
->id
= struct_create(name
, isunion
);
1798 type
->id
= struct_find(name
, isunion
);
1799 type
->flags
|= T_STRUCT
;
1802 } else if (!tok_jmp("enum")) {
1803 if (tok_grp() == 'a')
1810 if (tok_grp() == 'a') {
1811 int id
= typedef_find(tok_see());
1814 memcpy(type
, &typedefs
[id
].type
,
1826 type
->bt
= size
| (sign
? T_MSIGN
: 0);
1830 static void readptrs(struct type
*type
)
1832 while (!tok_jmp("*")) {
1839 /* read function arguments */
1840 static int readargs(struct type
*args
, char argnames
[][NAMELEN
], int *varg
)
1845 while (!tok_comes(")")) {
1846 if (!tok_jmp("...")) {
1850 if (readname(&args
[nargs
], argnames
[nargs
], NULL
)) {
1851 /* argument has no type, assume int */
1852 memset(&args
[nargs
], 0, sizeof(struct type
));
1853 args
[nargs
].bt
= SINT
;
1854 strcpy(argnames
[nargs
], tok_get());
1856 /* argument arrays are pointers */
1857 array2ptr(&args
[nargs
]);
1864 if (nargs
== 1 && !TYPE_BT(&args
[0]))
1869 /* read K&R function arguments */
1870 static void krdef(long data
, struct name
*name
, unsigned flags
)
1872 struct funcinfo
*fi
= &funcs
[data
];
1874 for (i
= 0; i
< fi
->nargs
; i
++)
1875 if (!strcmp(fi
->argnames
[i
], name
->name
))
1876 memcpy(&fi
->args
[i
], &name
->type
, sizeof(name
->type
));
1880 * readarrays() parses array specifiers when reading a definition in
1881 * readname(). The "type" parameter contains the type contained in the
1882 * inner array; for instance, type in "int *a[10][20]" would be an int
1883 * pointer. When returning, the "type" parameter is changed to point
1884 * to the final array. The function returns a pointer to the type in
1885 * the inner array; this is useful when the type is not complete yet,
1886 * like when creating an array of function pointers as in
1887 * "int (*f[10])(int)". If there is no array brackets, NULL is returned.
1889 static struct type
*readarrays(struct type
*type
)
1892 struct type
*inner
= NULL
;
1895 while (!tok_jmp("[")) {
1901 err("const expr expected\n");
1906 for (i
= nar
- 1; i
>= 0; i
--) {
1907 type
->id
= array_add(type
, arsz
[i
]);
1909 inner
= &arrays
[type
->id
].type
;
1910 type
->flags
= T_ARRAY
;
1917 static struct type
*innertype(struct type
*t
)
1919 while (t
->flags
& T_ARRAY
&& !t
->ptr
)
1920 t
= &arrays
[t
->id
].type
;
1924 static void innertype_modify(struct type
*t
, struct type
*s
)
1926 struct type
*inner
= innertype(t
);
1927 int ptr
= inner
->ptr
;
1928 memcpy(inner
, s
, sizeof(*inner
));
1933 * readname() reads a variable definition; the name is copied into
1934 * "name" and the type is copied into "main" argument. The "base"
1935 * argument, if not NULL, indicates the base type of the variable.
1936 * For instance, the base type of "a" and "b" in "int *a, b[10]" is
1937 * "int". If NULL, basetype() is called directly to read the base
1938 * type of the variable. readname() returns zero, only if the
1939 * variable can be read.
1941 static int readname(struct type
*main
, char *name
, struct type
*base
)
1943 struct type type
; /* the main type */
1944 struct type btype
; /* type before parenthesis; e.g. "int *" in "int *(*p)[10] */
1950 if (basetype(&type
, &flags
))
1956 paren
= !tok_jmp("(");
1961 if (tok_grp() == 'a' && name
)
1962 strcpy(name
, tok_get());
1966 if (tok_comes("(")) {
1967 struct type args
[NARGS
];
1968 char argnames
[NARGS
][NAMELEN
];
1970 int nargs
= readargs(args
, argnames
, &varg
);
1971 struct type rtype
= type
; /* return type */
1972 struct type ftype
= {0}; /* function type */
1975 ftype
.flags
= T_FUNC
;
1977 ftype
.id
= func_create(&rtype
, name
, argnames
, args
, nargs
, varg
);
1979 innertype_modify(&type
, &ftype
);
1982 if (!tok_comes(";"))
1983 while (!tok_comes("{") && !readdefs(krdef
, type
.id
))
1986 if (paren
&& readarrays(&btype
))
1987 innertype_modify(&type
, &btype
);
1993 static int readtype(struct type
*type
)
1995 return readname(type
, NULL
, NULL
);
1999 * readdefs() reads a variable definition statement. The definition
2000 * can appear in different contexts: global variables, function
2001 * local variables, struct fields, and typedefs. For each defined
2002 * variable, def() callback is called with the appropriate name
2003 * struct and flags; the callback should finish parsing the definition
2004 * by possibly reading the initializer expression and saving the name
2007 static int readdefs(void (*def
)(long data
, struct name
*name
, unsigned flags
),
2011 unsigned base_flags
;
2012 if (basetype(&base
, &base_flags
))
2014 if (tok_comes(";") || tok_comes("{"))
2017 struct name name
= {{""}};
2018 if (readname(&name
.type
, name
.name
, &base
))
2020 def(data
, &name
, base_flags
);
2021 } while (!tok_jmp(","));
2025 /* just like readdefs, but default to int type; for handling K&R functions */
2026 static int readdefs_int(void (*def
)(long data
, struct name
*name
, unsigned flags
),
2032 if (basetype(&base
, &flags
)) {
2034 if (tok_grp() != 'a')
2036 memset(&base
, 0, sizeof(base
));
2039 if (!tok_comes(";")) {
2041 struct name name
= {{""}};
2042 if (readname(&name
.type
, name
.name
, &base
))
2044 if (nobase
&& tok_grp() == 'a')
2045 err("type missing!\n");
2046 def(data
, &name
, flags
);
2047 } while (!tok_jmp(","));
2053 /* parsing initializer expressions */
2055 static void jumpbrace(void)
2058 while (!tok_comes("}") || depth
--)
2059 if (!strcmp("{", tok_get()))
2064 /* compute the size of the initializer expression */
2065 static int initsize(void)
2067 long addr
= tok_addr();
2071 if (tok_grp() == '"') {
2073 n
= tok_len() - 2 + 1;
2078 while (tok_jmp("}")) {
2080 if (!tok_jmp("[")) {
2089 while (!tok_comes("}") && !tok_comes(","))
2090 if (!strcmp("{", tok_get()))
2098 /* read the initializer expression and initialize basic types using set() cb */
2099 static void initexpr(struct type
*t
, int off
, void *obj
,
2100 void (*set
)(void *obj
, int off
, struct type
*t
))
2106 if (!t
->ptr
&& t
->flags
& T_STRUCT
) {
2107 struct structinfo
*si
= &structs
[t
->id
];
2109 for (i
= 0; i
< si
->nfields
&& !tok_comes("}"); i
++) {
2110 struct name
*field
= &si
->fields
[i
];
2111 if (!tok_jmp(".")) {
2112 field
= struct_field(t
->id
, tok_get());
2115 initexpr(&field
->type
, off
+ field
->addr
, obj
, set
);
2119 } else if (t
->flags
& T_ARRAY
) {
2120 struct type t_de
= arrays
[t
->id
].type
;
2122 /* handling extra braces as in: char s[] = {"sth"} */
2123 if (TYPE_SZ(&t_de
) == 1 && tok_grp() == '"') {
2128 for (i
= 0; !tok_comes("}"); i
++) {
2130 struct type it
= t_de
;
2131 if (!tok_jmp("[")) {
2138 if (!tok_comes("{") && (tok_grp() != '"' ||
2139 !(it
.flags
& T_ARRAY
)))
2140 it
= *innertype(&t_de
);
2141 initexpr(&it
, off
+ type_totsz(&it
) * idx
, obj
, set
);