2 * neatcc - a small and simple C compiler
4 * Copyright (C) 2010-2011 Ali Gholami Rudi
6 * This program is released under GNU GPL version 2.
14 #include <sys/types.h>
19 static int nogen
; /* don't generate code */
21 #define o_bop(op) {if (!nogen) o_bop(op);}
22 #define o_uop(op) {if (!nogen) o_uop(op);}
23 #define o_cast(bt) {if (!nogen) o_cast(bt);}
24 #define o_memcpy() {if (!nogen) o_memcpy();}
25 #define o_memset() {if (!nogen) o_memset();}
26 #define o_call(argc, ret) {if (!nogen) o_call(argc, ret);}
27 #define o_ret(ret) {if (!nogen) o_ret(ret);}
28 #define o_assign(bt) {if (!nogen) o_assign(bt);}
29 #define o_deref(bt) {if (!nogen) o_deref(bt);}
30 #define o_load() {if (!nogen) o_load();}
31 #define o_popnum(c) (nogen ? 0 : o_popnum(c))
32 #define o_num(n) {if (!nogen) o_num(n);}
33 #define o_local(addr) {if (!nogen) o_local(addr);}
34 #define o_sym(sym) {if (!nogen) o_sym(sym);}
35 #define o_tmpdrop(n) {if (!nogen) o_tmpdrop(n);}
36 #define o_tmpswap() {if (!nogen) o_tmpswap();}
37 #define o_tmpcopy() {if (!nogen) o_tmpcopy();}
38 #define o_mklabel() (nogen ? 0 : o_mklabel())
39 #define o_jz(addr) (nogen ? 0 : o_jz(addr))
40 #define o_jnz(addr) (nogen ? 0 : o_jnz(addr))
41 #define o_jmp(addr) (nogen ? 0 : o_jmp(addr))
42 #define o_filljmp(addr) {if (!nogen) o_filljmp(addr);}
43 #define o_filljmp2(addr, dst) {if (!nogen) o_filljmp2(addr, dst);}
44 #define o_fork() {if (!nogen) o_fork();}
45 #define o_forkpush() {if (!nogen) o_forkpush();}
46 #define o_forkjoin() {if (!nogen) o_forkjoin();}
48 #define MAXLOCALS (1 << 10)
49 #define MAXGLOBALS (1 << 10)
50 #define MAXARGS (1 << 5)
52 #define TYPE_BT(t) ((t)->ptr ? LONGSZ : (t)->bt)
53 #define TYPE_SZ(t) ((t)->ptr ? LONGSZ : (t)->bt & BT_SZMASK)
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
[MAXTMP
];
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
)
105 int len
= cpp_loc(err
, tok_addr());
106 strcpy(err
+ len
, msg
);
112 char elfname
[NAMELEN
]; /* local elf name for static variables in function */
114 long addr
; /* local stack offset, global data addr, struct offset */
117 static struct name locals
[MAXLOCALS
];
119 static struct name globals
[MAXGLOBALS
];
122 static void local_add(struct name
*name
)
124 if (nlocals
>= MAXLOCALS
)
125 err("nomem: MAXLOCALS reached!\n");
126 memcpy(&locals
[nlocals
++], name
, sizeof(*name
));
129 static int global_find(char *name
)
132 for (i
= 0; i
< nglobals
; i
++)
133 if (!strcmp(name
, globals
[i
].name
))
138 static void global_add(struct name
*name
)
140 int found
= global_find(name
->name
);
141 int i
= found
== -1 ? nglobals
++ : found
;
142 if (nglobals
>= MAXGLOBALS
)
143 err("nomem: MAXGLOBALS reached!\n");
144 memcpy(&globals
[i
], name
, sizeof(*name
));
147 #define MAXENUMS (1 << 10)
149 static struct enumval
{
155 static void enum_add(char *name
, int val
)
157 struct enumval
*ev
= &enums
[nenums
++];
158 if (nenums
>= MAXENUMS
)
159 err("nomem: MAXENUMS reached!\n");
160 strcpy(ev
->name
, name
);
164 static int enum_find(int *val
, char *name
)
167 for (i
= nenums
- 1; i
>= 0; --i
)
168 if (!strcmp(name
, enums
[i
].name
)) {
175 #define MAXTYPEDEFS (1 << 10)
177 static struct typdefinfo
{
180 } typedefs
[MAXTYPEDEFS
];
181 static int ntypedefs
;
183 static void typedef_add(char *name
, struct type
*type
)
185 struct typdefinfo
*ti
= &typedefs
[ntypedefs
++];
186 if (ntypedefs
>= MAXTYPEDEFS
)
187 err("nomem: MAXTYPEDEFS reached!\n");
188 strcpy(ti
->name
, name
);
189 memcpy(&ti
->type
, type
, sizeof(*type
));
192 static int typedef_find(char *name
)
195 for (i
= ntypedefs
- 1; i
>= 0; --i
)
196 if (!strcmp(name
, typedefs
[i
].name
))
201 #define MAXARRAYS (1 << 10)
203 static struct array
{
209 static int array_add(struct type
*type
, int n
)
211 struct array
*a
= &arrays
[narrays
++];
212 if (narrays
>= MAXARRAYS
)
213 err("nomem: MAXARRAYS reached!\n");
214 memcpy(&a
->type
, type
, sizeof(*type
));
219 static void array2ptr(struct type
*t
)
221 if (t
->flags
& T_ARRAY
&& !t
->ptr
) {
222 memcpy(t
, &arrays
[t
->id
].type
, sizeof(*t
));
227 #define MAXSTRUCTS (1 << 10)
228 #define MAXFIELDS (1 << 7)
230 static struct structinfo
{
232 struct name fields
[MAXFIELDS
];
236 } structs
[MAXSTRUCTS
];
239 static int struct_find(char *name
, int isunion
)
242 for (i
= nstructs
- 1; i
>= 0; --i
)
243 if (*structs
[i
].name
&& !strcmp(name
, structs
[i
].name
) &&
244 structs
[i
].isunion
== isunion
)
247 if (nstructs
>= MAXSTRUCTS
)
248 err("nomem: MAXTYPES reached!\n");
249 memset(&structs
[i
], 0, sizeof(structs
[i
]));
250 strcpy(structs
[i
].name
, name
);
251 structs
[i
].isunion
= isunion
;
255 static struct name
*struct_field(int id
, char *name
)
257 struct structinfo
*si
= &structs
[id
];
259 for (i
= 0; i
< si
->nfields
; i
++)
260 if (!strcmp(name
, si
->fields
[i
].name
))
261 return &si
->fields
[i
];
262 err("field not found\n");
265 #define MAXBREAK (1 << 7)
267 static long breaks
[MAXBREAK
];
269 static long continues
[MAXBREAK
];
270 static int ncontinues
;
272 static void break_fill(long addr
, int till
)
275 for (i
= till
; i
< nbreaks
; i
++)
276 o_filljmp2(breaks
[i
], addr
);
280 static void continue_fill(long addr
, int till
)
283 for (i
= till
; i
< ncontinues
; i
++)
284 o_filljmp2(continues
[i
], addr
);
288 static int type_totsz(struct type
*t
)
292 if (t
->flags
& T_ARRAY
)
293 return arrays
[t
->id
].n
* type_totsz(&arrays
[t
->id
].type
);
294 return t
->flags
& T_STRUCT
? structs
[t
->id
].size
: BT_SZ(t
->bt
);
297 static unsigned type_szde(struct type
*t
)
302 return type_totsz(&de
);
305 static void ts_de(int deref
)
307 struct type
*t
= &ts
[nts
- 1];
308 if (deref
&& t
->addr
&& (!(t
->flags
& T_ARRAY
) || (t
->ptr
)))
313 static void ts_pop_de(struct type
*t
)
320 if (t
->addr
&& (t
->ptr
|| !(t
->flags
& T_FUNC
)))
325 static void ts_pop_de2(struct type
*t1
, struct type
*t2
)
333 static int tok_jmp(int tok
)
335 if (tok_see() != tok
)
341 static void tok_expect(int tok
)
343 if (tok_get() != tok
)
344 err("syntax error\n");
347 static unsigned bt_op(unsigned bt1
, unsigned bt2
)
349 unsigned s1
= BT_SZ(bt1
);
350 unsigned s2
= BT_SZ(bt2
);
351 return (bt1
| bt2
) & BT_SIGNED
| (s1
> s2
? s1
: s2
);
354 static void ts_binop(int op
)
358 ts_pop_de2(&t1
, &t2
);
359 if (op
== O_DIV
|| op
== O_MOD
)
362 bt
= bt_op(TYPE_BT(&t1
), TYPE_BT(&t2
));
363 o_bop(op
| (bt
& BT_SIGNED
? O_SIGNED
: 0));
367 static void ts_addop(int op
)
370 ts_pop_de2(&t1
, &t2
);
371 if (!t1
.ptr
&& !t2
.ptr
) {
373 ts_push_bt(bt_op(TYPE_BT(&t1
), TYPE_BT(&t2
)));
376 if (t1
.ptr
&& !t2
.ptr
)
378 if (!t1
.ptr
&& t2
.ptr
)
379 if (type_szde(&t2
) > 1) {
380 o_num(type_szde(&t2
));
383 if (t1
.ptr
&& !t2
.ptr
)
386 if (t1
.ptr
&& t2
.ptr
) {
387 int sz
= type_szde(&t1
);
392 ts_push_bt(4 | BT_SIGNED
);
394 ts_push(t1
.ptr
? &t1
: &t2
);
398 #define ALIGN(x, a) (((x) + (a) - 1) & ~((a) - 1))
399 #define MIN(a, b) ((a) < (b) ? (a) : (b))
401 static int type_alignment(struct type
*t
)
403 if (t
->flags
& T_ARRAY
&& !t
->ptr
)
404 return type_alignment(&arrays
[t
->id
].type
);
405 if (t
->flags
& T_STRUCT
&& !t
->ptr
)
406 return type_alignment(&structs
[t
->id
].fields
[0].type
);
407 return MIN(LONGSZ
, type_totsz(t
));
410 static void structdef(void *data
, struct name
*name
, unsigned flags
)
412 struct structinfo
*si
= data
;
415 if (si
->size
< type_totsz(&name
->type
))
416 si
->size
= type_totsz(&name
->type
);
418 struct type
*t
= &name
->type
;
419 int alignment
= type_alignment(t
);
420 if (t
->flags
& T_ARRAY
&& !t
->ptr
)
421 alignment
= MIN(LONGSZ
, type_totsz(&arrays
[t
->id
].type
));
422 si
->size
= ALIGN(si
->size
, alignment
);
423 name
->addr
= si
->size
;
424 si
->size
+= type_totsz(&name
->type
);
426 memcpy(&si
->fields
[si
->nfields
++], name
, sizeof(*name
));
429 static int readdefs(void (*def
)(void *, struct name
*, unsigned f
), void *data
);
431 static int struct_create(char *name
, int isunion
)
433 int id
= struct_find(name
, isunion
);
434 struct structinfo
*si
= &structs
[id
];
436 while (tok_jmp('}')) {
437 readdefs(structdef
, si
);
443 static void readexpr(void);
445 static void enum_create(void)
449 while (tok_jmp('}')) {
451 tok_expect(TOK_NAME
);
452 strcpy(name
, tok_id());
457 err("const expr expected!\n");
464 static int basetype(struct type
*type
, unsigned *flags
)
471 char name
[NAMELEN
] = "";
509 isunion
= tok_get() == TOK_UNION
;
510 if (!tok_jmp(TOK_NAME
))
511 strcpy(name
, tok_id());
512 if (tok_see() == '{')
513 type
->id
= struct_create(name
, isunion
);
515 type
->id
= struct_find(name
, isunion
);
516 type
->flags
|= T_STRUCT
;
522 if (tok_see() == '{')
524 type
->bt
= 4 | BT_SIGNED
;
527 if (tok_see() == TOK_NAME
) {
528 int id
= typedef_find(tok_id());
531 memcpy(type
, &typedefs
[id
].type
,
544 type
->bt
= size
| (sign
? BT_SIGNED
: 0);
548 static int readname(struct type
*main
, char *name
,
549 struct type
*base
, unsigned flags
);
551 static int readtype(struct type
*type
)
553 return readname(type
, NULL
, NULL
, 0);
556 static void readptrs(struct type
*type
)
558 while (!tok_jmp('*')) {
565 /* used to differenciate labels from case and cond exprs */
569 static void readpre(void);
571 static char *tmp_str(char *buf
, int len
)
573 static char name
[NAMELEN
];
576 sprintf(name
, "__neatcc.s%d", id
++);
577 dat
= o_mkdat(name
, len
, 0);
578 memcpy(dat
, buf
, len
);
582 static void readprimary(void)
585 if (!tok_jmp(TOK_NUM
)) {
587 int bt
= tok_num(&n
);
592 if (!tok_jmp(TOK_STR
)) {
596 t
.bt
= 1 | BT_SIGNED
;
602 o_sym(tmp_str(buf
, len
));
605 if (!tok_jmp(TOK_NAME
)) {
606 struct name unkn
= {""};
607 char *name
= unkn
.name
;
609 strcpy(name
, tok_id());
610 /* don't search for labels here */
611 if (!ncexpr
&& !caseexpr
&& tok_see() == ':')
613 for (i
= nlocals
- 1; i
>= 0; --i
) {
614 struct type
*t
= &locals
[i
].type
;
615 if (!strcmp(locals
[i
].name
, name
)) {
616 o_local(locals
[i
].addr
);
621 if ((n
= global_find(name
)) != -1) {
622 struct name
*g
= &globals
[n
];
623 struct type
*t
= &g
->type
;
624 char *elfname
= *g
->elfname
? g
->elfname
: g
->name
;
629 if (!enum_find(&n
, name
)) {
630 ts_push_bt(4 | BT_SIGNED
);
634 if (tok_see() != '(')
635 err("unknown symbol\n");
649 if (!t
.ptr
|| !o
.ptr
)
659 static void arrayderef(void)
665 if (!(t
.flags
& T_ARRAY
) && t
.addr
) {
667 o_deref(TYPE_BT(&t
));
682 static void inc_post(int op
)
684 struct type t
= ts
[nts
- 1];
685 /* pushing the value before inc */
691 /* increment by 1 or pointer size */
695 o_num(t
.ptr
> 0 ? type_szde(&t
) : 1);
699 o_assign(TYPE_BT(&t
));
703 static void readfield(void)
707 tok_expect(TOK_NAME
);
710 field
= struct_field(t
.id
, tok_id());
715 ts_push_addr(&field
->type
);
718 #define MAXFUNCS (1 << 10)
720 static struct funcinfo
{
721 struct type args
[MAXFIELDS
];
728 static int func_create(struct type
*ret
, struct name
*args
, int nargs
)
730 struct funcinfo
*fi
= &funcs
[nfuncs
++];
732 if (nfuncs
>= MAXFUNCS
)
733 err("nomem: MAXFUNCS reached!\n");
734 memcpy(&fi
->ret
, ret
, sizeof(*ret
));
735 for (i
= 0; i
< nargs
; i
++)
736 memcpy(&fi
->args
[i
], &args
[i
].type
, sizeof(*ret
));
741 static void readcall(void)
747 if (t
.flags
& T_FUNC
&& t
.ptr
> 0)
749 fi
= t
.flags
& T_FUNC
? &funcs
[t
.id
] : NULL
;
750 if (tok_see() != ')') {
755 } while (!tok_jmp(','));
758 o_call(argc
, fi
? TYPE_BT(&fi
->ret
) : 4 | BT_SIGNED
);
760 if (TYPE_BT(&fi
->ret
))
761 o_cast(TYPE_BT(&fi
->ret
));
764 ts_push_bt(4 | BT_SIGNED
);
768 static void readpost(void)
782 if (!tok_jmp(TOK2("++"))) {
786 if (!tok_jmp(TOK2("--"))) {
794 if (!tok_jmp(TOK2("->"))) {
803 static void inc_pre(int op
)
807 /* copy the destination */
809 ts_push(&ts
[nts
- 1]);
810 /* increment by 1 or pointer size */
812 o_num(t
.ptr
> 0 ? type_szde(&t
) : 1);
814 /* assign the result */
815 o_assign(TYPE_BT(&t
));
819 static void readpre(void)
826 die("cannot use the address\n");
838 err("dereferencing non-pointer\n");
840 o_deref(TYPE_BT(&t
));
850 ts_push_bt(4 | BT_SIGNED
);
865 if (!tok_jmp(TOK2("++"))) {
869 if (!tok_jmp(TOK2("--"))) {
873 if (!tok_jmp(TOK_SIZEOF
)) {
875 int op
= !tok_jmp('(');
886 o_num(type_totsz(&t
));
894 static void readmul(void)
917 static void readadd(void)
935 static void shift(int op
)
939 ts_pop_de2(NULL
, &t
);
940 o_bop(op
| (BT_SIGNED
& TYPE_BT(&t
) ? O_SIGNED
: 0));
941 ts_push_bt(TYPE_BT(&t
));
944 static void readshift(void)
948 if (!tok_jmp(TOK2("<<"))) {
952 if (!tok_jmp(TOK2(">>"))) {
960 static void cmp(int op
)
965 ts_pop_de2(&t1
, &t2
);
966 bt
= bt_op(TYPE_BT(&t1
), TYPE_BT(&t2
));
967 o_bop(op
| (bt
& BT_SIGNED
? O_SIGNED
: 0));
968 ts_push_bt(4 | BT_SIGNED
);
971 static void readcmp(void)
983 if (!tok_jmp(TOK2("<="))) {
987 if (!tok_jmp(TOK2(">="))) {
995 static void eq(int op
)
998 ts_pop_de2(NULL
, NULL
);
1000 ts_push_bt(4 | BT_SIGNED
);
1003 static void readeq(void)
1007 if (!tok_jmp(TOK2("=="))) {
1011 if (!tok_jmp(TOK2("!="))) {
1019 static void readbitand(void)
1022 while (!tok_jmp('&')) {
1028 static void readxor(void)
1031 while (!tok_jmp('^')) {
1037 static void readbitor(void)
1040 while (!tok_jmp('|')) {
1046 #define MAXCOND (1 << 7)
1048 static void readand(void)
1050 long conds
[MAXCOND
];
1055 if (tok_see() != TOK2("&&"))
1059 conds
[nconds
++] = o_jz(0);
1060 while (!tok_jmp(TOK2("&&"))) {
1063 conds
[nconds
++] = o_jz(0);
1068 for (i
= 0; i
< nconds
; i
++)
1069 o_filljmp(conds
[i
]);
1074 ts_push_bt(4 | BT_SIGNED
);
1077 static void reador(void)
1079 long conds
[MAXCOND
];
1084 if (tok_see() != TOK2("||"))
1088 conds
[nconds
++] = o_jnz(0);
1089 while (!tok_jmp(TOK2("||"))) {
1092 conds
[nconds
++] = o_jnz(0);
1097 for (i
= 0; i
< nconds
; i
++)
1098 o_filljmp(conds
[i
]);
1103 ts_push_bt(4 | BT_SIGNED
);
1106 static int readcexpr_const(void)
1114 /* both branches yield the same type; so ignore the first */
1122 /* making sure t->addr == 0 on both branches */
1129 static void readcexpr(void)
1138 if (readcexpr_const()) {
1141 /* both branches yield the same type; so ignore the first */
1149 /* making sure t->addr == 0 on both branches */
1158 static void opassign(int op
, int ptrop
)
1160 struct type t
= ts
[nts
- 1];
1165 o_assign(TYPE_BT(&ts
[nts
- 2]));
1170 static void doassign(void)
1172 struct type t
= ts
[nts
- 1];
1173 if (!t
.ptr
&& t
.flags
& T_STRUCT
) {
1175 o_num(type_totsz(&t
));
1179 o_assign(TYPE_BT(&ts
[nts
- 1]));
1184 static void readexpr(void)
1187 if (!tok_jmp('=')) {
1192 if (!tok_jmp(TOK2("+="))) {
1196 if (!tok_jmp(TOK2("-="))) {
1200 if (!tok_jmp(TOK2("*="))) {
1204 if (!tok_jmp(TOK2("/="))) {
1208 if (!tok_jmp(TOK2("%="))) {
1212 if (!tok_jmp(TOK3("<<="))) {
1216 if (!tok_jmp(TOK3(">>="))) {
1220 if (!tok_jmp(TOK3("&="))) {
1224 if (!tok_jmp(TOK3("|="))) {
1228 if (!tok_jmp(TOK3("^="))) {
1234 static void readestmt(void)
1240 } while (!tok_jmp(','));
1243 static void o_localoff(long addr
, int off
)
1252 static struct type
*innertype(struct type
*t
)
1254 if (t
->flags
& T_ARRAY
&& !t
->ptr
)
1255 return innertype(&arrays
[t
->id
].type
);
1259 static void initexpr(struct type
*t
, int off
, void *obj
,
1260 void (*set
)(void *obj
, int off
, struct type
*t
))
1266 if (!t
->ptr
&& t
->flags
& T_STRUCT
) {
1267 struct structinfo
*si
= &structs
[t
->id
];
1269 for (i
= 0; i
< si
->nfields
; i
++) {
1270 struct name
*field
= &si
->fields
[i
];
1271 if (!tok_jmp('.')) {
1272 tok_expect(TOK_NAME
);
1273 field
= struct_field(t
->id
, tok_id());
1276 initexpr(&field
->type
, off
+ field
->addr
, obj
, set
);
1277 if (tok_jmp(',') || tok_see() == '}')
1280 } else if (t
->flags
& T_ARRAY
) {
1281 struct type
*t_de
= &arrays
[t
->id
].type
;
1283 for (i
= 0; ; i
++) {
1285 struct type
*it
= t_de
;
1286 if (!tok_jmp('[')) {
1293 if (tok_see() != '{')
1294 it
= innertype(t_de
);
1295 initexpr(it
, off
+ type_totsz(it
) * idx
, obj
, set
);
1296 if (tok_jmp(',') || tok_see() == '}')
1303 static void jumpbrace(void)
1306 while (tok_see() != '}' || depth
--)
1307 if (tok_get() == '{')
1312 static int initsize(void)
1314 long addr
= tok_addr();
1316 if (!tok_jmp(TOK_STR
)) {
1322 while (tok_jmp('}')) {
1324 if (!tok_jmp('[')) {
1333 while (tok_see() != '}' && tok_see() != ',')
1334 if (tok_get() == '{')
1342 #define F_GLOBAL(flags) (!((flags) & F_STATIC))
1344 static void globalinit(void *obj
, int off
, struct type
*t
)
1346 struct name
*name
= obj
;
1347 char *elfname
= *name
->elfname
? name
->elfname
: name
->name
;
1348 if (t
->flags
& T_ARRAY
&& tok_see() == TOK_STR
) {
1349 struct type
*t_de
= &arrays
[t
->id
].type
;
1350 if (!t_de
->ptr
&& !t_de
->flags
&& TYPE_SZ(t_de
) == 1) {
1353 tok_expect(TOK_STR
);
1355 memcpy((void *) name
->addr
+ off
, buf
, len
);
1360 o_datset(elfname
, off
, TYPE_BT(t
));
1364 static void globaldef(void *data
, struct name
*name
, unsigned flags
)
1366 struct type
*t
= &name
->type
;
1367 char *elfname
= *name
->elfname
? name
->elfname
: name
->name
;
1369 if (t
->flags
& T_ARRAY
&& !t
->ptr
&& !arrays
[t
->id
].n
)
1370 if (~flags
& F_EXTERN
)
1371 arrays
[t
->id
].n
= initsize();
1373 if (!(flags
& F_EXTERN
) && (!(t
->flags
& T_FUNC
) || t
->ptr
)) {
1375 name
->addr
= (long) o_mkdat(elfname
, sz
, F_GLOBAL(flags
));
1377 o_mkbss(elfname
, sz
, F_GLOBAL(flags
));
1381 initexpr(t
, 0, name
, globalinit
);
1384 static void localinit(void *obj
, int off
, struct type
*t
)
1386 long addr
= *(long *) obj
;
1387 if (t
->flags
& T_ARRAY
&& tok_see() == TOK_STR
) {
1388 struct type
*t_de
= &arrays
[t
->id
].type
;
1389 if (!t_de
->ptr
&& !t_de
->flags
&& TYPE_SZ(t_de
) == 1) {
1392 tok_expect(TOK_STR
);
1394 o_localoff(addr
, off
);
1395 o_sym(tmp_str(buf
, len
));
1402 o_localoff(addr
, off
);
1410 /* current function name */
1411 static char func_name
[NAMELEN
];
1413 static void localdef(void *data
, struct name
*name
, unsigned flags
)
1415 struct type
*t
= &name
->type
;
1416 if ((flags
& F_EXTERN
) || (t
->flags
& T_FUNC
) && !t
->ptr
) {
1420 if (flags
& F_STATIC
) {
1421 sprintf(name
->elfname
, "__neatcc.%s.%s", func_name
, name
->name
);
1422 globaldef(data
, name
, flags
);
1425 if (t
->flags
& T_ARRAY
&& !t
->ptr
&& !arrays
[t
->id
].n
)
1426 arrays
[t
->id
].n
= initsize();
1427 name
->addr
= o_mklocal(type_totsz(&name
->type
));
1429 if (flags
& F_INIT
) {
1430 if (t
->flags
& (T_ARRAY
| T_STRUCT
) && !t
->ptr
) {
1431 o_local(name
->addr
);
1433 o_num(type_totsz(t
));
1437 initexpr(t
, 0, &name
->addr
, localinit
);
1441 static void funcdef(char *name
, struct type
*type
, struct name
*args
,
1442 int nargs
, int varg
, unsigned flags
)
1444 struct name global
= {""};
1446 strcpy(global
.name
, name
);
1447 strcpy(func_name
, name
);
1448 memcpy(&global
.type
, type
, sizeof(*type
));
1449 o_func_beg(name
, nargs
, F_GLOBAL(flags
), varg
);
1450 global_add(&global
);
1451 for (i
= 0; i
< nargs
; i
++) {
1452 args
[i
].addr
= o_arg2loc(i
);
1453 local_add(&args
[i
]);
1457 static int readargs(struct name
*args
, int *varg
)
1462 while (tok_see() != ')') {
1463 if (!tok_jmp(TOK3("..."))) {
1467 readname(&args
[nargs
].type
, args
[nargs
].name
, NULL
, 0);
1468 array2ptr(&args
[nargs
].type
);
1474 if (nargs
== 1 && !TYPE_BT(&args
[0].type
))
1479 static int readname(struct type
*main
, char *name
,
1480 struct type
*base
, unsigned flags
)
1482 struct type tpool
[3];
1484 struct type
*type
= &tpool
[npool
++];
1485 struct type
*func
= NULL
;
1486 struct type
*ret
= NULL
;
1490 memset(tpool
, 0, sizeof(tpool
));
1494 if (basetype(type
, &flags
))
1497 memcpy(type
, base
, sizeof(*base
));
1500 if (!tok_jmp('(')) {
1502 type
= &tpool
[npool
++];
1506 if (!tok_jmp(TOK_NAME
) && name
)
1507 strcpy(name
, tok_id());
1508 while (!tok_jmp('[')) {
1514 err("const expr expected\n");
1519 for (i
= nar
- 1; i
>= 0; i
--) {
1520 type
->id
= array_add(type
, arsz
[i
]);
1521 if (func
&& i
== nar
- 1)
1522 func
= &arrays
[type
->id
].type
;
1523 type
->flags
= T_ARRAY
;
1529 if (tok_see() == '(') {
1530 struct name args
[MAXARGS
] = {{""}};
1532 int nargs
= readargs(args
, &varg
);
1536 type
= &tpool
[npool
++];
1539 func
->flags
= T_FUNC
;
1541 func
->id
= func_create(ret
, args
, nargs
);
1542 if (fdef
&& tok_see() == '{') {
1543 funcdef(name
, func
, args
, nargs
, varg
, flags
);
1547 memcpy(main
, type
, sizeof(*type
));
1551 static int readdefs(void (*def
)(void *data
, struct name
*name
, unsigned flags
),
1555 unsigned base_flags
;
1556 if (basetype(&base
, &base_flags
))
1558 while (tok_see() != ';' && tok_see() != '{') {
1559 struct name name
= {{""}};
1560 unsigned flags
= base_flags
;
1561 if (readname(&name
.type
, name
.name
, &base
, flags
))
1565 def(data
, &name
, flags
);
1571 static void typedefdef(void *data
, struct name
*name
, unsigned flags
)
1573 typedef_add(name
->name
, &name
->type
);
1576 static void readstmt(void);
1578 #define MAXCASES (1 << 7)
1580 static void readswitch(void)
1582 int break_beg
= nbreaks
;
1583 long val_addr
= o_mklocal(LONGSZ
);
1585 int ncases
= 0; /* number of case labels */
1586 long last_failed
= -1; /* address of last failed jmp */
1587 long last_matched
= -1; /* address of last walk through jmp */
1588 long default_addr
= -1; /* address of the default label */
1594 o_assign(TYPE_BT(&t
));
1599 while (tok_jmp('}')) {
1600 if (tok_see() != TOK_CASE
&& tok_see() != TOK_DEFAULT
) {
1605 last_matched
= o_jmp(0);
1606 if (tok_get() == TOK_CASE
) {
1607 if (last_failed
>= 0)
1608 o_filljmp(last_failed
);
1614 o_deref(TYPE_BT(&t
));
1616 last_failed
= o_jz(0);
1619 default_addr
= o_mklabel();
1622 if (last_matched
>= 0)
1623 o_filljmp(last_matched
);
1626 o_rmlocal(val_addr
, LONGSZ
);
1627 if (last_failed
>= 0)
1628 o_filljmp2(last_failed
,
1629 default_addr
>= 0 ? default_addr
: o_mklabel());
1630 break_fill(o_mklabel(), break_beg
);
1633 #define MAXGOTO (1 << 10)
1635 static struct gotoinfo
{
1641 static struct labelinfo
{
1647 static void goto_add(char *name
)
1649 strcpy(gotos
[ngotos
].name
, name
);
1650 gotos
[ngotos
++].addr
= o_jmp(0);
1653 static void label_add(char *name
)
1655 strcpy(labels
[nlabels
].name
, name
);
1656 labels
[nlabels
++].addr
= o_mklabel();
1659 static void goto_fill(void)
1662 for (i
= 0; i
< ngotos
; i
++)
1663 for (j
= 0; j
< nlabels
; j
++)
1664 if (!strcmp(gotos
[i
].name
, labels
[j
].name
)) {
1665 o_filljmp2(gotos
[i
].addr
, labels
[j
].addr
);
1670 static void readstmt(void)
1674 if (!tok_jmp('{')) {
1675 int _nlocals
= nlocals
;
1676 int _nglobals
= nglobals
;
1677 int _nenums
= nenums
;
1678 int _ntypedefs
= ntypedefs
;
1679 int _nstructs
= nstructs
;
1680 int _nfuncs
= nfuncs
;
1681 int _narrays
= narrays
;
1682 while (tok_jmp('}'))
1686 ntypedefs
= _ntypedefs
;
1687 nstructs
= _nstructs
;
1690 nglobals
= _nglobals
;
1693 if (!readdefs(localdef
, NULL
)) {
1697 if (!tok_jmp(TOK_TYPEDEF
)) {
1698 readdefs(typedefdef
, NULL
);
1702 if (!tok_jmp(TOK_IF
)) {
1710 if (!tok_jmp(TOK_ELSE
)) {
1720 if (!tok_jmp(TOK_WHILE
)) {
1722 int break_beg
= nbreaks
;
1723 int continue_beg
= ncontinues
;
1733 break_fill(o_mklabel(), break_beg
);
1734 continue_fill(l1
, continue_beg
);
1737 if (!tok_jmp(TOK_DO
)) {
1739 int break_beg
= nbreaks
;
1740 int continue_beg
= ncontinues
;
1743 tok_expect(TOK_WHILE
);
1750 break_fill(o_mklabel(), break_beg
);
1751 continue_fill(l2
, continue_beg
);
1755 if (!tok_jmp(TOK_FOR
)) {
1756 long l_check
, l_jump
, j_fail
, j_pass
;
1757 int break_beg
= nbreaks
;
1758 int continue_beg
= ncontinues
;
1761 if (tok_see() != ';')
1764 l_check
= o_mklabel();
1765 if (tok_see() != ';') {
1773 l_jump
= o_mklabel();
1774 if (tok_see() != ')')
1783 break_fill(o_mklabel(), break_beg
);
1784 continue_fill(l_jump
, continue_beg
);
1787 if (!tok_jmp(TOK_SWITCH
)) {
1791 if (!tok_jmp(TOK_RETURN
)) {
1792 int ret
= tok_see() != ';';
1801 if (!tok_jmp(TOK_BREAK
)) {
1803 breaks
[nbreaks
++] = o_jmp(0);
1806 if (!tok_jmp(TOK_CONTINUE
)) {
1808 continues
[ncontinues
++] = o_jmp(0);
1811 if (!tok_jmp(TOK_GOTO
)) {
1812 tok_expect(TOK_NAME
);
1819 if (!tok_jmp(':')) {
1820 label_add(tok_id());
1826 static void readdecl(void)
1828 if (!tok_jmp(TOK_TYPEDEF
)) {
1829 readdefs(typedefdef
, NULL
);
1833 readdefs(globaldef
, NULL
);
1834 if (tok_see() == '{') {
1838 func_name
[0] = '\0';
1847 static void parse(void)
1849 while (tok_see() != TOK_EOF
)
1853 static void compat_macros(void)
1855 cpp_define("__STDC__", "");
1856 cpp_define("__arm__", "");
1857 cpp_define("__linux__", "");
1859 /* ignored keywords */
1860 cpp_define("const", "");
1861 cpp_define("register", "");
1862 cpp_define("volatile", "");
1863 cpp_define("inline", "");
1864 cpp_define("restrict", "");
1865 cpp_define("__inline__", "");
1866 cpp_define("__restrict__", "");
1867 cpp_define("__attribute__(x)", "");
1868 cpp_define("__builtin_va_list__", "long");
1871 int main(int argc
, char *argv
[])
1877 while (i
< argc
&& argv
[i
][0] == '-') {
1878 if (argv
[i
][1] == 'I')
1879 cpp_addpath(argv
[i
][2] ? argv
[i
] + 2 : argv
[++i
]);
1880 if (argv
[i
][1] == 'D') {
1881 char *name
= argv
[i
] + 2;
1883 char *eq
= strchr(name
, '=');
1888 cpp_define(name
, def
);
1890 if (argv
[i
][1] == 'o')
1891 strcpy(obj
, argv
[i
][2] ? argv
[i
] + 2 : argv
[++i
]);
1895 die("neatcc: no file given\n");
1896 if (cpp_init(argv
[i
]))
1897 die("neatcc: cannot open input file\n");
1900 strcpy(obj
, argv
[i
]);
1901 obj
[strlen(obj
) - 1] = 'o';
1903 ofd
= open(obj
, O_WRONLY
| O_TRUNC
| O_CREAT
, 0600);