2 * neatcc - a small and simple C compiler
4 * Copyright (C) 2010-2011 Ali Gholami Rudi
6 * This file is released under GNU GPL version 2.
14 #include <sys/types.h>
19 #define MAXLOCALS (1 << 10)
20 #define MAXGLOBALS (1 << 10)
21 #define MAXARGS (1 << 5)
23 #define TYPE_BT(t) ((t)->ptr ? LONGSZ : (t)->bt)
24 #define TYPE_SZ(t) ((t)->ptr ? LONGSZ : (t)->bt & BT_SZMASK)
38 int id
; /* for structs, functions and arrays */
39 int addr
; /* the address is passed to gen.c; deref for value */
43 static struct type ts
[MAXTMP
];
46 static void ts_push_bt(unsigned bt
)
54 static void ts_push(struct type
*t
)
56 struct type
*d
= &ts
[nts
++];
57 memcpy(d
, t
, sizeof(*t
));
60 static void ts_push_addr(struct type
*t
)
66 static void ts_pop(struct type
*type
)
76 int len
= cpp_loc(err
, tok_addr());
77 strcpy(err
+ len
, msg
);
83 char elfname
[NAMELEN
]; /* local elf name for static variables in function */
85 long addr
; /* local stack offset, global data addr, struct offset */
88 static struct name locals
[MAXLOCALS
];
90 static struct name globals
[MAXGLOBALS
];
93 static void local_add(struct name
*name
)
95 if (nlocals
>= MAXLOCALS
)
96 err("nomem: MAXLOCALS reached!\n");
97 memcpy(&locals
[nlocals
++], name
, sizeof(*name
));
100 static int global_find(char *name
)
103 for (i
= 0; i
< nglobals
; i
++)
104 if (!strcmp(name
, globals
[i
].name
))
109 static void global_add(struct name
*name
)
111 int found
= global_find(name
->name
);
112 int i
= found
== -1 ? nglobals
++ : found
;
113 if (nglobals
>= MAXGLOBALS
)
114 err("nomem: MAXGLOBALS reached!\n");
115 memcpy(&globals
[i
], name
, sizeof(*name
));
118 #define MAXENUMS (1 << 10)
120 static struct enumval
{
126 static void enum_add(char *name
, int val
)
128 struct enumval
*ev
= &enums
[nenums
++];
129 if (nenums
>= MAXENUMS
)
130 err("nomem: MAXENUMS reached!\n");
131 strcpy(ev
->name
, name
);
135 static int enum_find(int *val
, char *name
)
138 for (i
= nenums
- 1; i
>= 0; --i
)
139 if (!strcmp(name
, enums
[i
].name
)) {
146 #define MAXTYPEDEFS (1 << 10)
148 static struct typdefinfo
{
151 } typedefs
[MAXTYPEDEFS
];
152 static int ntypedefs
;
154 static void typedef_add(char *name
, struct type
*type
)
156 struct typdefinfo
*ti
= &typedefs
[ntypedefs
++];
157 if (ntypedefs
>= MAXTYPEDEFS
)
158 err("nomem: MAXTYPEDEFS reached!\n");
159 strcpy(ti
->name
, name
);
160 memcpy(&ti
->type
, type
, sizeof(*type
));
163 static int typedef_find(char *name
)
166 for (i
= ntypedefs
- 1; i
>= 0; --i
)
167 if (!strcmp(name
, typedefs
[i
].name
))
172 #define MAXARRAYS (1 << 10)
174 static struct array
{
180 static int array_add(struct type
*type
, int n
)
182 struct array
*a
= &arrays
[narrays
++];
183 if (narrays
>= MAXARRAYS
)
184 err("nomem: MAXARRAYS reached!\n");
185 memcpy(&a
->type
, type
, sizeof(*type
));
190 static void array2ptr(struct type
*t
)
192 if (t
->flags
& T_ARRAY
&& !t
->ptr
) {
193 memcpy(t
, &arrays
[t
->id
].type
, sizeof(*t
));
198 #define MAXSTRUCTS (1 << 10)
199 #define MAXFIELDS (1 << 7)
201 static struct structinfo
{
203 struct name fields
[MAXFIELDS
];
207 } structs
[MAXSTRUCTS
];
210 static int struct_find(char *name
, int isunion
)
213 for (i
= nstructs
- 1; i
>= 0; --i
)
214 if (*structs
[i
].name
&& !strcmp(name
, structs
[i
].name
) &&
215 structs
[i
].isunion
== isunion
)
218 if (nstructs
>= MAXSTRUCTS
)
219 err("nomem: MAXTYPES reached!\n");
220 memset(&structs
[i
], 0, sizeof(structs
[i
]));
221 strcpy(structs
[i
].name
, name
);
222 structs
[i
].isunion
= isunion
;
226 static struct name
*struct_field(int id
, char *name
)
228 struct structinfo
*si
= &structs
[id
];
230 for (i
= 0; i
< si
->nfields
; i
++)
231 if (!strcmp(name
, si
->fields
[i
].name
))
232 return &si
->fields
[i
];
233 err("field not found\n");
236 #define MAXBREAK (1 << 7)
238 static long breaks
[MAXBREAK
];
240 static long continues
[MAXBREAK
];
241 static int ncontinues
;
243 static void break_fill(long addr
, int till
)
246 for (i
= till
; i
< nbreaks
; i
++)
247 o_filljmp2(breaks
[i
], addr
);
251 static void continue_fill(long addr
, int till
)
254 for (i
= till
; i
< ncontinues
; i
++)
255 o_filljmp2(continues
[i
], addr
);
259 static int type_totsz(struct type
*t
)
263 if (t
->flags
& T_ARRAY
)
264 return arrays
[t
->id
].n
* type_totsz(&arrays
[t
->id
].type
);
265 return t
->flags
& T_STRUCT
? structs
[t
->id
].size
: BT_SZ(t
->bt
);
268 static unsigned type_szde(struct type
*t
)
273 return type_totsz(&de
);
276 static void ts_de(int deref
)
278 struct type
*t
= &ts
[nts
- 1];
279 if (deref
&& t
->addr
&& (!(t
->flags
& T_ARRAY
) || (t
->ptr
)))
284 static void ts_pop_de(struct type
*t
)
291 if (t
->addr
&& !(t
->flags
& T_FUNC
))
296 static void ts_pop_de2(struct type
*t1
, struct type
*t2
)
304 static int tok_jmp(int tok
)
306 if (tok_see() != tok
)
312 static void tok_expect(int tok
)
314 if (tok_get() != tok
)
315 err("syntax error\n");
318 static unsigned bt_op(unsigned bt1
, unsigned bt2
)
320 unsigned s1
= BT_SZ(bt1
);
321 unsigned s2
= BT_SZ(bt2
);
322 return (bt1
| bt2
) & BT_SIGNED
| (s1
> s2
? s1
: s2
);
325 static void ts_binop(int op
)
329 ts_pop_de2(&t1
, &t2
);
330 if (op
== O_DIV
|| op
== O_MOD
)
333 bt
= bt_op(TYPE_BT(&t1
), TYPE_BT(&t2
));
334 o_bop(op
| (bt
& BT_SIGNED
? O_SIGNED
: 0));
338 static void ts_addop(int op
)
341 ts_pop_de2(&t1
, &t2
);
342 if (!t1
.ptr
&& !t2
.ptr
) {
344 ts_push_bt(bt_op(TYPE_BT(&t1
), TYPE_BT(&t2
)));
347 if (t1
.ptr
&& !t2
.ptr
)
349 if (!t1
.ptr
&& t2
.ptr
)
350 if (type_szde(&t2
) > 1) {
351 o_num(type_szde(&t2
));
354 if (t1
.ptr
&& !t2
.ptr
)
357 if (t1
.ptr
&& t2
.ptr
) {
358 int sz
= type_szde(&t1
);
363 ts_push_bt(4 | BT_SIGNED
);
365 ts_push(t1
.ptr
? &t1
: &t2
);
369 #define ALIGN(x, a) (((x) + (a) - 1) & ~((a) - 1))
370 #define MIN(a, b) ((a) < (b) ? (a) : (b))
372 static int type_alignment(struct type
*t
)
374 if (t
->flags
& T_ARRAY
&& !t
->ptr
)
375 return type_alignment(&arrays
[t
->id
].type
);
376 if (t
->flags
& T_STRUCT
&& !t
->ptr
)
377 return type_alignment(&structs
[t
->id
].fields
[0].type
);
378 return MIN(LONGSZ
, type_totsz(t
));
381 static void structdef(void *data
, struct name
*name
, unsigned flags
)
383 struct structinfo
*si
= data
;
386 if (si
->size
< type_totsz(&name
->type
))
387 si
->size
= type_totsz(&name
->type
);
389 struct type
*t
= &name
->type
;
390 int alignment
= type_alignment(t
);
391 if (t
->flags
& T_ARRAY
&& !t
->ptr
)
392 alignment
= MIN(LONGSZ
, type_totsz(&arrays
[t
->id
].type
));
393 si
->size
= ALIGN(si
->size
, alignment
);
394 name
->addr
= si
->size
;
395 si
->size
+= type_totsz(&name
->type
);
397 memcpy(&si
->fields
[si
->nfields
++], name
, sizeof(*name
));
400 static int readdefs(void (*def
)(void *, struct name
*, unsigned f
), void *data
);
402 static int struct_create(char *name
, int isunion
)
404 int id
= struct_find(name
, isunion
);
405 struct structinfo
*si
= &structs
[id
];
407 while (tok_jmp('}')) {
408 readdefs(structdef
, si
);
414 static void readexpr(void);
416 static void enum_create(void)
420 while (tok_jmp('}')) {
422 tok_expect(TOK_NAME
);
423 strcpy(name
, tok_id());
424 if (tok_see() == '=') {
429 err("const expr expected!\n");
436 static int basetype(struct type
*type
, unsigned *flags
)
443 char name
[NAMELEN
] = "";
481 isunion
= tok_get() == TOK_UNION
;
482 if (!tok_jmp(TOK_NAME
))
483 strcpy(name
, tok_id());
484 if (tok_see() == '{')
485 type
->id
= struct_create(name
, isunion
);
487 type
->id
= struct_find(name
, isunion
);
488 type
->flags
|= T_STRUCT
;
494 if (tok_see() == '{')
496 type
->bt
= 4 | BT_SIGNED
;
499 if (tok_see() == TOK_NAME
) {
500 int id
= typedef_find(tok_id());
503 memcpy(type
, &typedefs
[id
].type
,
516 type
->bt
= size
| (sign
? BT_SIGNED
: 0);
520 static int readname(struct type
*main
, char *name
,
521 struct type
*base
, unsigned flags
);
523 static int readtype(struct type
*type
)
525 return readname(type
, NULL
, NULL
, 0);
528 static void readptrs(struct type
*type
)
530 while (!tok_jmp('*')) {
537 /* used to differenciate labels from case and cond exprs */
541 static void readpre(void);
543 static char *tmp_str(char *buf
, int len
)
545 static char name
[NAMELEN
];
548 sprintf(name
, "__neatcc.s%d", id
++);
549 dat
= dat_dat(name
, len
, 0);
550 memcpy(dat
, buf
, len
);
554 static void readprimary(void)
557 if (!tok_jmp(TOK_NUM
)) {
559 int bt
= tok_num(&n
);
564 if (!tok_jmp(TOK_STR
)) {
568 t
.bt
= 1 | BT_SIGNED
;
574 o_sym(tmp_str(buf
, len
));
577 if (!tok_jmp(TOK_NAME
)) {
578 struct name unkn
= {""};
579 char *name
= unkn
.name
;
581 strcpy(name
, tok_id());
582 /* don't search for labels here */
583 if (!ncexpr
&& !caseexpr
&& tok_see() == ':')
585 for (i
= nlocals
- 1; i
>= 0; --i
) {
586 struct type
*t
= &locals
[i
].type
;
587 if (!strcmp(locals
[i
].name
, name
)) {
588 o_local(locals
[i
].addr
);
593 if ((n
= global_find(name
)) != -1) {
594 struct name
*g
= &globals
[n
];
595 struct type
*t
= &g
->type
;
596 char *elfname
= *g
->elfname
? g
->elfname
: g
->name
;
601 if (!enum_find(&n
, name
)) {
602 ts_push_bt(4 | BT_SIGNED
);
606 if (tok_see() != '(')
607 err("unknown symbol\n");
621 if (!t
.ptr
|| !o
.ptr
)
631 static void arrayderef(void)
637 if (!(t
.flags
& T_ARRAY
) && t
.addr
) {
639 o_deref(TYPE_BT(&t
));
654 static void inc_post(int op
)
656 struct type t
= ts
[nts
- 1];
657 /* pushing the value before inc */
663 /* increment by 1 or pointer size */
667 o_num(t
.ptr
> 0 ? type_szde(&t
) : 1);
671 o_assign(TYPE_BT(&t
));
675 static void readfield(void)
679 tok_expect(TOK_NAME
);
682 field
= struct_field(t
.id
, tok_id());
687 ts_push_addr(&field
->type
);
690 #define MAXFUNCS (1 << 10)
692 static struct funcinfo
{
693 struct type args
[MAXFIELDS
];
699 static int func_create(struct type
*ret
, struct name
*args
, int nargs
)
701 struct funcinfo
*fi
= &funcs
[nfuncs
++];
703 if (nfuncs
>= MAXFUNCS
)
704 err("nomem: MAXFUNCS reached!\n");
705 memcpy(&fi
->ret
, ret
, sizeof(*ret
));
706 for (i
= 0; i
< nargs
; i
++)
707 memcpy(&fi
->args
[i
], &args
[i
].type
, sizeof(*ret
));
712 static void readcall(void)
718 if (t
.flags
& T_FUNC
&& t
.ptr
> 0)
720 fi
= t
.flags
& T_FUNC
? &funcs
[t
.id
] : NULL
;
721 if (tok_see() != ')') {
726 } while (!tok_jmp(','));
729 o_call(argc
, fi
? TYPE_BT(&fi
->ret
) : 4 | BT_SIGNED
);
731 if (TYPE_BT(&fi
->ret
))
732 o_cast(TYPE_BT(&fi
->ret
));
735 ts_push_bt(4 | BT_SIGNED
);
739 static void readpost(void)
753 if (!tok_jmp(TOK2("++"))) {
757 if (!tok_jmp(TOK2("--"))) {
765 if (!tok_jmp(TOK2("->"))) {
774 static void inc_pre(int op
)
778 /* copy the destination */
780 ts_push(&ts
[nts
- 1]);
781 /* increment by 1 or pointer size */
783 o_num(t
.ptr
> 0 ? type_szde(&t
) : 1);
785 /* assign the result */
786 o_assign(TYPE_BT(&t
));
790 static void readpre(void)
797 die("cannot use the address\n");
809 err("dereferencing non-pointer\n");
811 o_deref(TYPE_BT(&t
));
821 ts_push_bt(4 | BT_SIGNED
);
836 if (!tok_jmp(TOK2("++"))) {
840 if (!tok_jmp(TOK2("--"))) {
844 if (!tok_jmp(TOK_SIZEOF
)) {
846 int op
= !tok_jmp('(');
848 int nogen
= !o_nogen();
856 o_num(type_totsz(&t
));
864 static void readmul(void)
887 static void readadd(void)
905 static void shift(int op
)
909 ts_pop_de2(NULL
, &t
);
910 o_bop(op
| (BT_SIGNED
& TYPE_BT(&t
) ? O_SIGNED
: 0));
911 ts_push_bt(TYPE_BT(&t
));
914 static void readshift(void)
918 if (!tok_jmp(TOK2("<<"))) {
922 if (!tok_jmp(TOK2(">>"))) {
930 static void cmp(int op
)
935 ts_pop_de2(&t1
, &t2
);
936 bt
= bt_op(TYPE_BT(&t1
), TYPE_BT(&t2
));
937 o_bop(op
| (bt
& BT_SIGNED
? O_SIGNED
: 0));
938 ts_push_bt(4 | BT_SIGNED
);
941 static void readcmp(void)
953 if (!tok_jmp(TOK2("<="))) {
957 if (!tok_jmp(TOK2(">="))) {
965 static void eq(int op
)
968 ts_pop_de2(NULL
, NULL
);
970 ts_push_bt(4 | BT_SIGNED
);
973 static void readeq(void)
977 if (!tok_jmp(TOK2("=="))) {
981 if (!tok_jmp(TOK2("!="))) {
989 static void readbitand(void)
992 while (!tok_jmp('&')) {
998 static void readxor(void)
1001 while (!tok_jmp('^')) {
1007 static void readbitor(void)
1010 while (!tok_jmp('|')) {
1016 #define MAXCOND (1 << 7)
1018 static void readand(void)
1020 long conds
[MAXCOND
];
1025 if (tok_see() != TOK2("&&"))
1029 conds
[nconds
++] = o_jz(0);
1030 while (!tok_jmp(TOK2("&&"))) {
1033 conds
[nconds
++] = o_jz(0);
1038 for (i
= 0; i
< nconds
; i
++)
1039 o_filljmp(conds
[i
]);
1044 ts_push_bt(4 | BT_SIGNED
);
1047 static void reador(void)
1049 long conds
[MAXCOND
];
1054 if (tok_see() != TOK2("||"))
1058 conds
[nconds
++] = o_jnz(0);
1059 while (!tok_jmp(TOK2("||"))) {
1062 conds
[nconds
++] = o_jnz(0);
1067 for (i
= 0; i
< nconds
; i
++)
1068 o_filljmp(conds
[i
]);
1073 ts_push_bt(4 | BT_SIGNED
);
1076 static int readcexpr_const(void)
1103 static void readcexpr(void)
1112 if (readcexpr_const()) {
1131 static void opassign(int op
, int ptrop
)
1133 struct type t
= ts
[nts
- 1];
1138 o_assign(TYPE_BT(&ts
[nts
- 1]));
1143 static void doassign(void)
1145 struct type t
= ts
[nts
- 1];
1146 if (!t
.ptr
&& t
.flags
& T_STRUCT
) {
1148 o_num(type_totsz(&t
));
1152 o_assign(TYPE_BT(&ts
[nts
- 1]));
1157 static void readexpr(void)
1160 if (!tok_jmp('=')) {
1165 if (!tok_jmp(TOK2("+="))) {
1169 if (!tok_jmp(TOK2("-="))) {
1173 if (!tok_jmp(TOK2("*="))) {
1177 if (!tok_jmp(TOK2("/="))) {
1181 if (!tok_jmp(TOK2("%="))) {
1185 if (!tok_jmp(TOK3("<<="))) {
1189 if (!tok_jmp(TOK3(">>="))) {
1193 if (!tok_jmp(TOK3("&="))) {
1197 if (!tok_jmp(TOK3("|="))) {
1201 if (!tok_jmp(TOK3("^="))) {
1207 static void readestmt(void)
1213 } while (!tok_jmp(','));
1216 static void o_localoff(long addr
, int off
)
1225 static struct type
*innertype(struct type
*t
)
1227 if (t
->flags
& T_ARRAY
&& !t
->ptr
)
1228 return innertype(&arrays
[t
->id
].type
);
1232 static void initexpr(struct type
*t
, int off
, void *obj
,
1233 void (*set
)(void *obj
, int off
, struct type
*t
))
1239 if (!t
->ptr
&& t
->flags
& T_STRUCT
) {
1240 struct structinfo
*si
= &structs
[t
->id
];
1242 for (i
= 0; i
< si
->nfields
; i
++) {
1243 struct name
*field
= &si
->fields
[i
];
1244 if (!tok_jmp('.')) {
1245 tok_expect(TOK_NAME
);
1246 field
= struct_field(t
->id
, tok_id());
1249 initexpr(&field
->type
, off
+ field
->addr
, obj
, set
);
1250 if (tok_jmp(',') || tok_see() == '}')
1253 } else if (t
->flags
& T_ARRAY
) {
1254 struct type
*t_de
= &arrays
[t
->id
].type
;
1256 for (i
= 0; ; i
++) {
1258 struct type
*it
= t_de
;
1259 if (!tok_jmp('[')) {
1266 if (tok_see() != '{')
1267 it
= innertype(t_de
);
1268 initexpr(it
, off
+ type_totsz(it
) * idx
, obj
, set
);
1269 if (tok_jmp(',') || tok_see() == '}')
1276 static void jumpbrace(void)
1279 while (tok_see() != '}' || depth
--)
1280 if (tok_get() == '{')
1285 static int initsize(void)
1287 long addr
= tok_addr();
1289 if (!tok_jmp(TOK_STR
)) {
1296 while (tok_jmp('}')) {
1298 if (!tok_jmp('[')) {
1307 while (tok_see() != '}' && tok_see() != ',')
1308 if (tok_get() == '{')
1317 #define F_GLOBAL(flags) (!((flags) & F_STATIC))
1319 static void globalinit(void *obj
, int off
, struct type
*t
)
1321 struct name
*name
= obj
;
1322 char *elfname
= *name
->elfname
? name
->elfname
: name
->name
;
1323 if (t
->flags
& T_ARRAY
&& tok_see() == TOK_STR
) {
1324 struct type
*t_de
= &arrays
[t
->id
].type
;
1325 if (!t_de
->ptr
&& !t_de
->flags
&& TYPE_SZ(t_de
) == 1) {
1328 tok_expect(TOK_STR
);
1330 memcpy((void *) name
->addr
+ off
, buf
, len
);
1335 o_datset(elfname
, off
, TYPE_BT(t
));
1339 static void globaldef(void *data
, struct name
*name
, unsigned flags
)
1341 struct type
*t
= &name
->type
;
1342 char *elfname
= *name
->elfname
? name
->elfname
: name
->name
;
1344 if (t
->flags
& T_ARRAY
&& !t
->ptr
&& !arrays
[t
->id
].n
)
1345 if (~flags
& F_EXTERN
)
1346 arrays
[t
->id
].n
= initsize();
1348 if (!(flags
& F_EXTERN
) && (!(t
->flags
& T_FUNC
) || t
->ptr
)) {
1350 name
->addr
= (long) dat_dat(elfname
, sz
, F_GLOBAL(flags
));
1352 dat_bss(elfname
, sz
, F_GLOBAL(flags
));
1356 initexpr(t
, 0, name
, globalinit
);
1359 static void localinit(void *obj
, int off
, struct type
*t
)
1361 long addr
= *(long *) obj
;
1362 if (t
->flags
& T_ARRAY
&& tok_see() == TOK_STR
) {
1363 struct type
*t_de
= &arrays
[t
->id
].type
;
1364 if (!t_de
->ptr
&& !t_de
->flags
&& TYPE_SZ(t_de
) == 1) {
1367 tok_expect(TOK_STR
);
1369 o_localoff(addr
, off
);
1370 o_sym(tmp_str(buf
, len
));
1377 o_localoff(addr
, off
);
1385 /* current function name */
1386 static char func_name
[NAMELEN
];
1388 static void localdef(void *data
, struct name
*name
, unsigned flags
)
1390 struct type
*t
= &name
->type
;
1391 if (flags
& (F_STATIC
| F_EXTERN
)) {
1392 sprintf(name
->elfname
, "__neatcc.%s.%s", func_name
, name
->name
);
1393 globaldef(data
, name
, flags
);
1396 if (t
->flags
& T_ARRAY
&& !t
->ptr
&& !arrays
[t
->id
].n
)
1397 arrays
[t
->id
].n
= initsize();
1398 name
->addr
= o_mklocal(type_totsz(&name
->type
));
1400 if (flags
& F_INIT
) {
1401 if (t
->flags
& (T_ARRAY
| T_STRUCT
) && !t
->ptr
) {
1402 o_local(name
->addr
);
1404 o_num(type_totsz(t
));
1408 initexpr(t
, 0, &name
->addr
, localinit
);
1412 static void funcdef(char *name
, struct type
*type
, struct name
*args
,
1413 int nargs
, unsigned flags
)
1415 struct name global
= {""};
1417 strcpy(global
.name
, name
);
1418 strcpy(func_name
, name
);
1419 memcpy(&global
.type
, type
, sizeof(*type
));
1420 o_func_beg(name
, F_GLOBAL(flags
));
1421 global_add(&global
);
1422 for (i
= 0; i
< nargs
; i
++) {
1423 args
[i
].addr
= o_arg(i
);
1424 local_add(&args
[i
]);
1428 static int readargs(struct name
*args
)
1432 while (tok_see() != ')') {
1433 if (!tok_jmp(TOK3("...")))
1435 readname(&args
[nargs
].type
, args
[nargs
].name
, NULL
, 0);
1436 array2ptr(&args
[nargs
].type
);
1442 if (nargs
== 1 && !TYPE_BT(&args
[0].type
))
1447 static int readname(struct type
*main
, char *name
,
1448 struct type
*base
, unsigned flags
)
1450 struct type tpool
[3];
1452 struct type
*type
= &tpool
[npool
++];
1453 struct type
*func
= NULL
;
1454 struct type
*ret
= NULL
;
1458 memset(tpool
, 0, sizeof(tpool
));
1462 if (basetype(type
, &flags
))
1465 memcpy(type
, base
, sizeof(*base
));
1468 if (!tok_jmp('(')) {
1470 type
= &tpool
[npool
++];
1474 if (!tok_jmp(TOK_NAME
) && name
)
1475 strcpy(name
, tok_id());
1476 while (!tok_jmp('[')) {
1482 err("const expr expected\n");
1487 for (i
= nar
- 1; i
>= 0; i
--) {
1488 type
->id
= array_add(type
, arsz
[i
]);
1489 if (func
&& i
== nar
- 1)
1490 func
= &arrays
[type
->id
].type
;
1491 type
->flags
= T_ARRAY
;
1497 if (tok_see() == '(') {
1498 struct name args
[MAXARGS
] = {{""}};
1499 int nargs
= readargs(args
);
1503 type
= &tpool
[npool
++];
1506 func
->flags
= T_FUNC
;
1508 func
->id
= func_create(ret
, args
, nargs
);
1509 if (fdef
&& tok_see() == '{') {
1510 funcdef(name
, func
, args
, nargs
, flags
);
1514 memcpy(main
, type
, sizeof(*type
));
1518 static int readdefs(void (*def
)(void *data
, struct name
*name
, unsigned flags
),
1522 unsigned base_flags
;
1523 if (basetype(&base
, &base_flags
))
1525 while (tok_see() != ';' && tok_see() != '{') {
1526 struct name name
= {{""}};
1527 unsigned flags
= base_flags
;
1528 if (readname(&name
.type
, name
.name
, &base
, flags
))
1532 def(data
, &name
, flags
);
1538 static void typedefdef(void *data
, struct name
*name
, unsigned flags
)
1540 typedef_add(name
->name
, &name
->type
);
1543 static void readstmt(void);
1545 #define MAXCASES (1 << 7)
1547 static void readswitch(void)
1549 int break_beg
= nbreaks
;
1550 long val_addr
= o_mklocal(LONGSZ
);
1551 long matched
[MAXCASES
];
1562 o_assign(TYPE_BT(&t
));
1567 while (tok_jmp('}')) {
1569 while (tok_see() == TOK_CASE
|| tok_see() == TOK_DEFAULT
) {
1571 matched
[nmatched
++] = o_jmp(0);
1574 if (!tok_jmp(TOK_CASE
)) {
1580 o_deref(TYPE_BT(&t
));
1588 if (!tok_jmp(TOK_DEFAULT
)) {
1593 for (i
= 0; i
< nmatched
; i
++)
1594 o_filljmp(matched
[i
]);
1598 o_rmlocal(val_addr
, LONGSZ
);
1601 break_fill(o_mklabel(), break_beg
);
1604 #define MAXGOTO (1 << 10)
1606 static struct gotoinfo
{
1612 static struct labelinfo
{
1618 static void goto_add(char *name
)
1620 strcpy(gotos
[ngotos
].name
, name
);
1621 gotos
[ngotos
++].addr
= o_jmp(0);
1624 static void label_add(char *name
)
1626 strcpy(labels
[nlabels
].name
, name
);
1627 labels
[nlabels
++].addr
= o_mklabel();
1630 static void goto_fill(void)
1633 for (i
= 0; i
< ngotos
; i
++)
1634 for (j
= 0; j
< nlabels
; j
++)
1635 if (!strcmp(gotos
[i
].name
, labels
[j
].name
)) {
1636 o_filljmp2(gotos
[i
].addr
, labels
[j
].addr
);
1641 static void readstmt(void)
1645 if (!tok_jmp('{')) {
1646 int _nlocals
= nlocals
;
1647 int _nglobals
= nglobals
;
1648 int _nenums
= nenums
;
1649 int _ntypedefs
= ntypedefs
;
1650 int _nstructs
= nstructs
;
1651 int _nfuncs
= nfuncs
;
1652 int _narrays
= narrays
;
1653 while (tok_jmp('}'))
1657 ntypedefs
= _ntypedefs
;
1658 nstructs
= _nstructs
;
1661 nglobals
= _nglobals
;
1664 if (!readdefs(localdef
, NULL
)) {
1668 if (!tok_jmp(TOK_TYPEDEF
)) {
1669 readdefs(typedefdef
, NULL
);
1673 if (!tok_jmp(TOK_IF
)) {
1681 if (!tok_jmp(TOK_ELSE
)) {
1691 if (!tok_jmp(TOK_WHILE
)) {
1693 int break_beg
= nbreaks
;
1694 int continue_beg
= ncontinues
;
1704 break_fill(o_mklabel(), break_beg
);
1705 continue_fill(l1
, continue_beg
);
1708 if (!tok_jmp(TOK_DO
)) {
1710 int break_beg
= nbreaks
;
1711 int continue_beg
= ncontinues
;
1714 tok_expect(TOK_WHILE
);
1721 break_fill(o_mklabel(), break_beg
);
1722 continue_fill(l2
, continue_beg
);
1725 if (!tok_jmp(TOK_FOR
)) {
1726 long l_check
, l_jump
, j_fail
, j_pass
;
1727 int break_beg
= nbreaks
;
1728 int continue_beg
= ncontinues
;
1731 if (tok_see() != ';')
1734 l_check
= o_mklabel();
1735 if (tok_see() != ';') {
1743 l_jump
= o_mklabel();
1744 if (tok_see() != ')')
1753 break_fill(o_mklabel(), break_beg
);
1754 continue_fill(l_jump
, continue_beg
);
1757 if (!tok_jmp(TOK_SWITCH
)) {
1761 if (!tok_jmp(TOK_RETURN
)) {
1762 int ret
= tok_see() != ';';
1771 if (!tok_jmp(TOK_BREAK
)) {
1773 breaks
[nbreaks
++] = o_jmp(0);
1776 if (!tok_jmp(TOK_CONTINUE
)) {
1778 continues
[ncontinues
++] = o_jmp(0);
1781 if (!tok_jmp(TOK_GOTO
)) {
1782 tok_expect(TOK_NAME
);
1789 if (!tok_jmp(':')) {
1790 label_add(tok_id());
1796 static void readdecl(void)
1798 if (!tok_jmp(TOK_TYPEDEF
)) {
1799 readdefs(typedefdef
, NULL
);
1803 readdefs(globaldef
, NULL
);
1804 if (tok_see() == '{') {
1808 func_name
[0] = '\0';
1817 static void parse(void)
1819 while (tok_see() != TOK_EOF
)
1823 int main(int argc
, char *argv
[])
1828 while (i
< argc
&& argv
[i
][0] == '-') {
1829 if (argv
[i
][1] == 'I')
1830 cpp_addpath(argv
[i
][2] ? argv
[i
] + 2 : argv
[++i
]);
1831 if (argv
[i
][1] == 'D') {
1832 char *name
= argv
[i
] + 2;
1834 char *eq
= strchr(name
, '=');
1839 cpp_define(name
, def
);
1844 die("neatcc: no file given\n");
1845 if (cpp_init(argv
[i
]))
1846 die("neatcc: cannot open input file\n");
1848 strcpy(obj
, argv
[i
]);
1849 obj
[strlen(obj
) - 1] = 'o';
1850 ofd
= open(obj
, O_WRONLY
| O_TRUNC
| O_CREAT
, 0600);