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('(');
883 o_num(type_totsz(&t
));
891 static void readmul(void)
914 static void readadd(void)
932 static void shift(int op
)
936 ts_pop_de2(NULL
, &t
);
937 o_bop(op
| (BT_SIGNED
& TYPE_BT(&t
) ? O_SIGNED
: 0));
938 ts_push_bt(TYPE_BT(&t
));
941 static void readshift(void)
945 if (!tok_jmp(TOK2("<<"))) {
949 if (!tok_jmp(TOK2(">>"))) {
957 static void cmp(int op
)
962 ts_pop_de2(&t1
, &t2
);
963 bt
= bt_op(TYPE_BT(&t1
), TYPE_BT(&t2
));
964 o_bop(op
| (bt
& BT_SIGNED
? O_SIGNED
: 0));
965 ts_push_bt(4 | BT_SIGNED
);
968 static void readcmp(void)
980 if (!tok_jmp(TOK2("<="))) {
984 if (!tok_jmp(TOK2(">="))) {
992 static void eq(int op
)
995 ts_pop_de2(NULL
, NULL
);
997 ts_push_bt(4 | BT_SIGNED
);
1000 static void readeq(void)
1004 if (!tok_jmp(TOK2("=="))) {
1008 if (!tok_jmp(TOK2("!="))) {
1016 static void readbitand(void)
1019 while (!tok_jmp('&')) {
1025 static void readxor(void)
1028 while (!tok_jmp('^')) {
1034 static void readbitor(void)
1037 while (!tok_jmp('|')) {
1043 #define MAXCOND (1 << 7)
1045 static void readand(void)
1047 long conds
[MAXCOND
];
1052 if (tok_see() != TOK2("&&"))
1056 conds
[nconds
++] = o_jz(0);
1057 while (!tok_jmp(TOK2("&&"))) {
1060 conds
[nconds
++] = o_jz(0);
1065 for (i
= 0; i
< nconds
; i
++)
1066 o_filljmp(conds
[i
]);
1071 ts_push_bt(4 | BT_SIGNED
);
1074 static void reador(void)
1076 long conds
[MAXCOND
];
1081 if (tok_see() != TOK2("||"))
1085 conds
[nconds
++] = o_jnz(0);
1086 while (!tok_jmp(TOK2("||"))) {
1089 conds
[nconds
++] = o_jnz(0);
1094 for (i
= 0; i
< nconds
; i
++)
1095 o_filljmp(conds
[i
]);
1100 ts_push_bt(4 | BT_SIGNED
);
1103 static int readcexpr_const(void)
1111 /* both branches yield the same type; so ignore the first */
1119 /* making sure t->addr == 0 on both branches */
1126 static void readcexpr(void)
1135 if (readcexpr_const()) {
1138 /* both branches yield the same type; so ignore the first */
1146 /* making sure t->addr == 0 on both branches */
1155 static void opassign(int op
, int ptrop
)
1157 struct type t
= ts
[nts
- 1];
1162 o_assign(TYPE_BT(&ts
[nts
- 2]));
1167 static void doassign(void)
1169 struct type t
= ts
[nts
- 1];
1170 if (!t
.ptr
&& t
.flags
& T_STRUCT
) {
1172 o_num(type_totsz(&t
));
1176 o_assign(TYPE_BT(&ts
[nts
- 1]));
1181 static void readexpr(void)
1184 if (!tok_jmp('=')) {
1189 if (!tok_jmp(TOK2("+="))) {
1193 if (!tok_jmp(TOK2("-="))) {
1197 if (!tok_jmp(TOK2("*="))) {
1201 if (!tok_jmp(TOK2("/="))) {
1205 if (!tok_jmp(TOK2("%="))) {
1209 if (!tok_jmp(TOK3("<<="))) {
1213 if (!tok_jmp(TOK3(">>="))) {
1217 if (!tok_jmp(TOK3("&="))) {
1221 if (!tok_jmp(TOK3("|="))) {
1225 if (!tok_jmp(TOK3("^="))) {
1231 static void readestmt(void)
1237 } while (!tok_jmp(','));
1240 static void o_localoff(long addr
, int off
)
1249 static struct type
*innertype(struct type
*t
)
1251 if (t
->flags
& T_ARRAY
&& !t
->ptr
)
1252 return innertype(&arrays
[t
->id
].type
);
1256 static void initexpr(struct type
*t
, int off
, void *obj
,
1257 void (*set
)(void *obj
, int off
, struct type
*t
))
1263 if (!t
->ptr
&& t
->flags
& T_STRUCT
) {
1264 struct structinfo
*si
= &structs
[t
->id
];
1266 for (i
= 0; i
< si
->nfields
; i
++) {
1267 struct name
*field
= &si
->fields
[i
];
1268 if (!tok_jmp('.')) {
1269 tok_expect(TOK_NAME
);
1270 field
= struct_field(t
->id
, tok_id());
1273 initexpr(&field
->type
, off
+ field
->addr
, obj
, set
);
1274 if (tok_jmp(',') || tok_see() == '}')
1277 } else if (t
->flags
& T_ARRAY
) {
1278 struct type
*t_de
= &arrays
[t
->id
].type
;
1280 for (i
= 0; ; i
++) {
1282 struct type
*it
= t_de
;
1283 if (!tok_jmp('[')) {
1290 if (tok_see() != '{')
1291 it
= innertype(t_de
);
1292 initexpr(it
, off
+ type_totsz(it
) * idx
, obj
, set
);
1293 if (tok_jmp(',') || tok_see() == '}')
1300 static void jumpbrace(void)
1303 while (tok_see() != '}' || depth
--)
1304 if (tok_get() == '{')
1309 static int initsize(void)
1311 long addr
= tok_addr();
1313 if (!tok_jmp(TOK_STR
)) {
1319 while (tok_jmp('}')) {
1321 if (!tok_jmp('[')) {
1330 while (tok_see() != '}' && tok_see() != ',')
1331 if (tok_get() == '{')
1339 #define F_GLOBAL(flags) (!((flags) & F_STATIC))
1341 static void globalinit(void *obj
, int off
, struct type
*t
)
1343 struct name
*name
= obj
;
1344 char *elfname
= *name
->elfname
? name
->elfname
: name
->name
;
1345 if (t
->flags
& T_ARRAY
&& tok_see() == TOK_STR
) {
1346 struct type
*t_de
= &arrays
[t
->id
].type
;
1347 if (!t_de
->ptr
&& !t_de
->flags
&& TYPE_SZ(t_de
) == 1) {
1350 tok_expect(TOK_STR
);
1352 memcpy((void *) name
->addr
+ off
, buf
, len
);
1357 o_datset(elfname
, off
, TYPE_BT(t
));
1361 static void globaldef(void *data
, struct name
*name
, unsigned flags
)
1363 struct type
*t
= &name
->type
;
1364 char *elfname
= *name
->elfname
? name
->elfname
: name
->name
;
1366 if (t
->flags
& T_ARRAY
&& !t
->ptr
&& !arrays
[t
->id
].n
)
1367 if (~flags
& F_EXTERN
)
1368 arrays
[t
->id
].n
= initsize();
1370 if (!(flags
& F_EXTERN
) && (!(t
->flags
& T_FUNC
) || t
->ptr
)) {
1372 name
->addr
= (long) o_mkdat(elfname
, sz
, F_GLOBAL(flags
));
1374 o_mkbss(elfname
, sz
, F_GLOBAL(flags
));
1378 initexpr(t
, 0, name
, globalinit
);
1381 static void localinit(void *obj
, int off
, struct type
*t
)
1383 long addr
= *(long *) obj
;
1384 if (t
->flags
& T_ARRAY
&& tok_see() == TOK_STR
) {
1385 struct type
*t_de
= &arrays
[t
->id
].type
;
1386 if (!t_de
->ptr
&& !t_de
->flags
&& TYPE_SZ(t_de
) == 1) {
1389 tok_expect(TOK_STR
);
1391 o_localoff(addr
, off
);
1392 o_sym(tmp_str(buf
, len
));
1399 o_localoff(addr
, off
);
1407 /* current function name */
1408 static char func_name
[NAMELEN
];
1410 static void localdef(void *data
, struct name
*name
, unsigned flags
)
1412 struct type
*t
= &name
->type
;
1413 if ((flags
& F_EXTERN
) || (t
->flags
& T_FUNC
) && !t
->ptr
) {
1417 if (flags
& F_STATIC
) {
1418 sprintf(name
->elfname
, "__neatcc.%s.%s", func_name
, name
->name
);
1419 globaldef(data
, name
, flags
);
1422 if (t
->flags
& T_ARRAY
&& !t
->ptr
&& !arrays
[t
->id
].n
)
1423 arrays
[t
->id
].n
= initsize();
1424 name
->addr
= o_mklocal(type_totsz(&name
->type
));
1426 if (flags
& F_INIT
) {
1427 if (t
->flags
& (T_ARRAY
| T_STRUCT
) && !t
->ptr
) {
1428 o_local(name
->addr
);
1430 o_num(type_totsz(t
));
1434 initexpr(t
, 0, &name
->addr
, localinit
);
1438 static void funcdef(char *name
, struct type
*type
, struct name
*args
,
1439 int nargs
, int varg
, unsigned flags
)
1441 struct name global
= {""};
1443 strcpy(global
.name
, name
);
1444 strcpy(func_name
, name
);
1445 memcpy(&global
.type
, type
, sizeof(*type
));
1446 o_func_beg(name
, nargs
, F_GLOBAL(flags
), varg
);
1447 global_add(&global
);
1448 for (i
= 0; i
< nargs
; i
++) {
1449 args
[i
].addr
= o_arg2loc(i
);
1450 local_add(&args
[i
]);
1454 static int readargs(struct name
*args
, int *varg
)
1459 while (tok_see() != ')') {
1460 if (!tok_jmp(TOK3("..."))) {
1464 readname(&args
[nargs
].type
, args
[nargs
].name
, NULL
, 0);
1465 array2ptr(&args
[nargs
].type
);
1471 if (nargs
== 1 && !TYPE_BT(&args
[0].type
))
1476 static int readname(struct type
*main
, char *name
,
1477 struct type
*base
, unsigned flags
)
1479 struct type tpool
[3];
1481 struct type
*type
= &tpool
[npool
++];
1482 struct type
*func
= NULL
;
1483 struct type
*ret
= NULL
;
1487 memset(tpool
, 0, sizeof(tpool
));
1491 if (basetype(type
, &flags
))
1494 memcpy(type
, base
, sizeof(*base
));
1497 if (!tok_jmp('(')) {
1499 type
= &tpool
[npool
++];
1503 if (!tok_jmp(TOK_NAME
) && name
)
1504 strcpy(name
, tok_id());
1505 while (!tok_jmp('[')) {
1511 err("const expr expected\n");
1516 for (i
= nar
- 1; i
>= 0; i
--) {
1517 type
->id
= array_add(type
, arsz
[i
]);
1518 if (func
&& i
== nar
- 1)
1519 func
= &arrays
[type
->id
].type
;
1520 type
->flags
= T_ARRAY
;
1526 if (tok_see() == '(') {
1527 struct name args
[MAXARGS
] = {{""}};
1529 int nargs
= readargs(args
, &varg
);
1533 type
= &tpool
[npool
++];
1536 func
->flags
= T_FUNC
;
1538 func
->id
= func_create(ret
, args
, nargs
);
1539 if (fdef
&& tok_see() == '{') {
1540 funcdef(name
, func
, args
, nargs
, varg
, flags
);
1544 memcpy(main
, type
, sizeof(*type
));
1548 static int readdefs(void (*def
)(void *data
, struct name
*name
, unsigned flags
),
1552 unsigned base_flags
;
1553 if (basetype(&base
, &base_flags
))
1555 while (tok_see() != ';' && tok_see() != '{') {
1556 struct name name
= {{""}};
1557 unsigned flags
= base_flags
;
1558 if (readname(&name
.type
, name
.name
, &base
, flags
))
1562 def(data
, &name
, flags
);
1568 static void typedefdef(void *data
, struct name
*name
, unsigned flags
)
1570 typedef_add(name
->name
, &name
->type
);
1573 static void readstmt(void);
1575 #define MAXCASES (1 << 7)
1577 static void readswitch(void)
1579 int break_beg
= nbreaks
;
1580 long val_addr
= o_mklocal(LONGSZ
);
1582 int ncases
= 0; /* number of case labels */
1583 long last_failed
= -1; /* address of last failed jmp */
1584 long last_matched
= -1; /* address of last walk through jmp */
1585 long default_addr
= -1; /* address of the default label */
1591 o_assign(TYPE_BT(&t
));
1596 while (tok_jmp('}')) {
1597 if (tok_see() != TOK_CASE
&& tok_see() != TOK_DEFAULT
) {
1602 last_matched
= o_jmp(0);
1603 if (tok_get() == TOK_CASE
) {
1604 if (last_failed
>= 0)
1605 o_filljmp(last_failed
);
1611 o_deref(TYPE_BT(&t
));
1613 last_failed
= o_jz(0);
1616 default_addr
= o_mklabel();
1619 if (last_matched
>= 0)
1620 o_filljmp(last_matched
);
1623 o_rmlocal(val_addr
, LONGSZ
);
1624 if (last_failed
>= 0)
1625 o_filljmp2(last_failed
,
1626 default_addr
>= 0 ? default_addr
: o_mklabel());
1627 break_fill(o_mklabel(), break_beg
);
1630 #define MAXGOTO (1 << 10)
1632 static struct gotoinfo
{
1638 static struct labelinfo
{
1644 static void goto_add(char *name
)
1646 strcpy(gotos
[ngotos
].name
, name
);
1647 gotos
[ngotos
++].addr
= o_jmp(0);
1650 static void label_add(char *name
)
1652 strcpy(labels
[nlabels
].name
, name
);
1653 labels
[nlabels
++].addr
= o_mklabel();
1656 static void goto_fill(void)
1659 for (i
= 0; i
< ngotos
; i
++)
1660 for (j
= 0; j
< nlabels
; j
++)
1661 if (!strcmp(gotos
[i
].name
, labels
[j
].name
)) {
1662 o_filljmp2(gotos
[i
].addr
, labels
[j
].addr
);
1667 static void readstmt(void)
1671 if (!tok_jmp('{')) {
1672 int _nlocals
= nlocals
;
1673 int _nglobals
= nglobals
;
1674 int _nenums
= nenums
;
1675 int _ntypedefs
= ntypedefs
;
1676 int _nstructs
= nstructs
;
1677 int _nfuncs
= nfuncs
;
1678 int _narrays
= narrays
;
1679 while (tok_jmp('}'))
1683 ntypedefs
= _ntypedefs
;
1684 nstructs
= _nstructs
;
1687 nglobals
= _nglobals
;
1690 if (!readdefs(localdef
, NULL
)) {
1694 if (!tok_jmp(TOK_TYPEDEF
)) {
1695 readdefs(typedefdef
, NULL
);
1699 if (!tok_jmp(TOK_IF
)) {
1707 if (!tok_jmp(TOK_ELSE
)) {
1717 if (!tok_jmp(TOK_WHILE
)) {
1719 int break_beg
= nbreaks
;
1720 int continue_beg
= ncontinues
;
1730 break_fill(o_mklabel(), break_beg
);
1731 continue_fill(l1
, continue_beg
);
1734 if (!tok_jmp(TOK_DO
)) {
1736 int break_beg
= nbreaks
;
1737 int continue_beg
= ncontinues
;
1740 tok_expect(TOK_WHILE
);
1747 break_fill(o_mklabel(), break_beg
);
1748 continue_fill(l2
, continue_beg
);
1752 if (!tok_jmp(TOK_FOR
)) {
1753 long l_check
, l_jump
, j_fail
, j_pass
;
1754 int break_beg
= nbreaks
;
1755 int continue_beg
= ncontinues
;
1758 if (tok_see() != ';')
1761 l_check
= o_mklabel();
1762 if (tok_see() != ';') {
1770 l_jump
= o_mklabel();
1771 if (tok_see() != ')')
1780 break_fill(o_mklabel(), break_beg
);
1781 continue_fill(l_jump
, continue_beg
);
1784 if (!tok_jmp(TOK_SWITCH
)) {
1788 if (!tok_jmp(TOK_RETURN
)) {
1789 int ret
= tok_see() != ';';
1798 if (!tok_jmp(TOK_BREAK
)) {
1800 breaks
[nbreaks
++] = o_jmp(0);
1803 if (!tok_jmp(TOK_CONTINUE
)) {
1805 continues
[ncontinues
++] = o_jmp(0);
1808 if (!tok_jmp(TOK_GOTO
)) {
1809 tok_expect(TOK_NAME
);
1816 if (!tok_jmp(':')) {
1817 label_add(tok_id());
1823 static void readdecl(void)
1825 if (!tok_jmp(TOK_TYPEDEF
)) {
1826 readdefs(typedefdef
, NULL
);
1830 readdefs(globaldef
, NULL
);
1831 if (tok_see() == '{') {
1835 func_name
[0] = '\0';
1844 static void parse(void)
1846 while (tok_see() != TOK_EOF
)
1850 static void compat_macros(void)
1852 cpp_define("__STDC__", "");
1853 cpp_define("__arm__", "");
1854 cpp_define("__linux__", "");
1856 /* ignored keywords */
1857 cpp_define("const", "");
1858 cpp_define("register", "");
1859 cpp_define("volatile", "");
1860 cpp_define("inline", "");
1861 cpp_define("restrict", "");
1862 cpp_define("__inline__", "");
1863 cpp_define("__restrict__", "");
1864 cpp_define("__attribute__(x)", "");
1865 cpp_define("__builtin_va_list__", "long");
1868 int main(int argc
, char *argv
[])
1874 while (i
< argc
&& argv
[i
][0] == '-') {
1875 if (argv
[i
][1] == 'I')
1876 cpp_addpath(argv
[i
][2] ? argv
[i
] + 2 : argv
[++i
]);
1877 if (argv
[i
][1] == 'D') {
1878 char *name
= argv
[i
] + 2;
1880 char *eq
= strchr(name
, '=');
1885 cpp_define(name
, def
);
1887 if (argv
[i
][1] == 'o')
1888 strcpy(obj
, argv
[i
][2] ? argv
[i
] + 2 : argv
[++i
]);
1892 die("neatcc: no file given\n");
1893 if (cpp_init(argv
[i
]))
1894 die("neatcc: cannot open input file\n");
1897 strcpy(obj
, argv
[i
]);
1898 obj
[strlen(obj
) - 1] = 'o';
1900 ofd
= open(obj
, O_WRONLY
| O_TRUNC
| O_CREAT
, 0600);