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
)
328 ts_pop_de2(&t1
, &t2
);
330 ts_push_bt(bt_op(TYPE_BT(&t1
), TYPE_BT(&t2
)));
333 static void ts_addop(int op
)
336 ts_pop_de2(&t1
, &t2
);
337 if (!t1
.ptr
&& !t2
.ptr
) {
339 ts_push_bt(bt_op(TYPE_BT(&t1
), TYPE_BT(&t2
)));
342 if (t1
.ptr
&& !t2
.ptr
)
344 if (!t1
.ptr
&& t2
.ptr
)
345 if (type_szde(&t2
) > 1) {
346 o_num(type_szde(&t2
));
349 if (t1
.ptr
&& !t2
.ptr
)
352 if (t1
.ptr
&& t2
.ptr
) {
353 int sz
= type_szde(&t1
);
358 ts_push_bt(4 | BT_SIGNED
);
360 ts_push(t1
.ptr
? &t1
: &t2
);
364 #define ALIGN(x, a) (((x) + (a) - 1) & ~((a) - 1))
365 #define MIN(a, b) ((a) < (b) ? (a) : (b))
367 static int type_alignment(struct type
*t
)
369 if (t
->flags
& T_ARRAY
&& !t
->ptr
)
370 return type_alignment(&arrays
[t
->id
].type
);
371 if (t
->flags
& T_STRUCT
&& !t
->ptr
)
372 return type_alignment(&structs
[t
->id
].fields
[0].type
);
373 return MIN(LONGSZ
, type_totsz(t
));
376 static void structdef(void *data
, struct name
*name
, unsigned flags
)
378 struct structinfo
*si
= data
;
381 if (si
->size
< type_totsz(&name
->type
))
382 si
->size
= type_totsz(&name
->type
);
384 struct type
*t
= &name
->type
;
385 int alignment
= type_alignment(t
);
386 if (t
->flags
& T_ARRAY
&& !t
->ptr
)
387 alignment
= MIN(LONGSZ
, type_totsz(&arrays
[t
->id
].type
));
388 si
->size
= ALIGN(si
->size
, alignment
);
389 name
->addr
= si
->size
;
390 si
->size
+= type_totsz(&name
->type
);
392 memcpy(&si
->fields
[si
->nfields
++], name
, sizeof(*name
));
395 static int readdefs(void (*def
)(void *, struct name
*, unsigned f
), void *data
);
397 static int struct_create(char *name
, int isunion
)
399 int id
= struct_find(name
, isunion
);
400 struct structinfo
*si
= &structs
[id
];
402 while (tok_jmp('}')) {
403 readdefs(structdef
, si
);
409 static void readexpr(void);
411 static void enum_create(void)
415 while (tok_jmp('}')) {
417 tok_expect(TOK_NAME
);
418 strcpy(name
, tok_id());
419 if (tok_see() == '=') {
424 err("const expr expected!\n");
431 static int basetype(struct type
*type
, unsigned *flags
)
438 char name
[NAMELEN
] = "";
476 isunion
= tok_get() == TOK_UNION
;
477 if (!tok_jmp(TOK_NAME
))
478 strcpy(name
, tok_id());
479 if (tok_see() == '{')
480 type
->id
= struct_create(name
, isunion
);
482 type
->id
= struct_find(name
, isunion
);
483 type
->flags
|= T_STRUCT
;
489 if (tok_see() == '{')
491 type
->bt
= 4 | BT_SIGNED
;
494 if (tok_see() == TOK_NAME
) {
495 int id
= typedef_find(tok_id());
498 memcpy(type
, &typedefs
[id
].type
,
511 type
->bt
= size
| (sign
? BT_SIGNED
: 0);
515 static int readname(struct type
*main
, char *name
,
516 struct type
*base
, unsigned flags
);
518 static int readtype(struct type
*type
)
520 return readname(type
, NULL
, NULL
, 0);
523 static void readptrs(struct type
*type
)
525 while (!tok_jmp('*')) {
532 /* used to differenciate labels from case and cond exprs */
536 static void readpre(void);
538 static char *tmp_str(char *buf
, int len
)
540 static char name
[NAMELEN
];
543 sprintf(name
, "__neatcc.s%d", id
++);
544 dat
= dat_dat(name
, len
, 0);
545 memcpy(dat
, buf
, len
);
549 static void readprimary(void)
552 if (!tok_jmp(TOK_NUM
)) {
554 int bt
= tok_num(&n
);
559 if (!tok_jmp(TOK_STR
)) {
563 t
.bt
= 1 | BT_SIGNED
;
569 o_sym(tmp_str(buf
, len
));
572 if (!tok_jmp(TOK_NAME
)) {
573 struct name unkn
= {""};
574 char *name
= unkn
.name
;
576 strcpy(name
, tok_id());
577 /* don't search for labels here */
578 if (!ncexpr
&& !caseexpr
&& tok_see() == ':')
580 for (i
= nlocals
- 1; i
>= 0; --i
) {
581 struct type
*t
= &locals
[i
].type
;
582 if (!strcmp(locals
[i
].name
, name
)) {
583 o_local(locals
[i
].addr
);
588 if ((n
= global_find(name
)) != -1) {
589 struct name
*g
= &globals
[n
];
590 struct type
*t
= &g
->type
;
591 char *elfname
= *g
->elfname
? g
->elfname
: g
->name
;
596 if (!enum_find(&n
, name
)) {
597 ts_push_bt(4 | BT_SIGNED
);
601 if (tok_see() != '(')
602 err("unknown symbol\n");
616 if (!t
.ptr
|| !o
.ptr
)
626 static void arrayderef(void)
632 if (!(t
.flags
& T_ARRAY
) && t
.addr
) {
634 o_deref(TYPE_BT(&t
));
649 static void inc_post(int op
)
651 struct type t
= ts
[nts
- 1];
652 /* pushing the value before inc */
658 /* increment by 1 or pointer size */
662 o_num(t
.ptr
> 0 ? type_szde(&t
) : 1);
666 o_assign(TYPE_BT(&t
));
670 static void readfield(void)
674 tok_expect(TOK_NAME
);
677 field
= struct_field(t
.id
, tok_id());
682 ts_push_addr(&field
->type
);
685 #define MAXFUNCS (1 << 10)
687 static struct funcinfo
{
688 struct type args
[MAXFIELDS
];
694 static int func_create(struct type
*ret
, struct name
*args
, int nargs
)
696 struct funcinfo
*fi
= &funcs
[nfuncs
++];
698 if (nfuncs
>= MAXFUNCS
)
699 err("nomem: MAXFUNCS reached!\n");
700 memcpy(&fi
->ret
, ret
, sizeof(*ret
));
701 for (i
= 0; i
< nargs
; i
++)
702 memcpy(&fi
->args
[i
], &args
[i
].type
, sizeof(*ret
));
707 static void readcall(void)
713 if (t
.flags
& T_FUNC
&& t
.ptr
> 0)
715 fi
= t
.flags
& T_FUNC
? &funcs
[t
.id
] : NULL
;
716 if (tok_see() != ')') {
721 } while (!tok_jmp(','));
724 o_call(argc
, fi
? TYPE_BT(&fi
->ret
) : 4 | BT_SIGNED
);
726 if (TYPE_BT(&fi
->ret
))
727 o_cast(TYPE_BT(&fi
->ret
));
730 ts_push_bt(4 | BT_SIGNED
);
734 static void readpost(void)
748 if (!tok_jmp(TOK2("++"))) {
752 if (!tok_jmp(TOK2("--"))) {
760 if (!tok_jmp(TOK2("->"))) {
769 static void inc_pre(int op
)
773 /* copy the destination */
775 ts_push(&ts
[nts
- 1]);
776 /* increment by 1 or pointer size */
778 o_num(t
.ptr
> 0 ? type_szde(&t
) : 1);
780 /* assign the result */
781 o_assign(TYPE_BT(&t
));
785 static void readpre(void)
792 die("cannot use the address\n");
804 err("dereferencing non-pointer\n");
806 o_deref(TYPE_BT(&t
));
816 ts_push_bt(4 | BT_SIGNED
);
831 if (!tok_jmp(TOK2("++"))) {
835 if (!tok_jmp(TOK2("--"))) {
839 if (!tok_jmp(TOK_SIZEOF
)) {
841 int op
= !tok_jmp('(');
843 int nogen
= !o_nogen();
851 o_num(type_totsz(&t
));
859 static void readmul(void)
882 static void readadd(void)
900 static void shift(int uop
, int sop
)
904 ts_pop_de2(NULL
, &t
);
905 o_bop(BT_SIGNED
& TYPE_BT(&t
) ? sop
: uop
);
906 ts_push_bt(TYPE_BT(&t
));
909 static void readshift(void)
913 if (!tok_jmp(TOK2("<<"))) {
917 if (!tok_jmp(TOK2(">>"))) {
925 static void cmp(int op
)
928 ts_pop_de2(NULL
, NULL
);
930 ts_push_bt(4 | BT_SIGNED
);
933 static void readcmp(void)
945 if (!tok_jmp(TOK2("<="))) {
949 if (!tok_jmp(TOK2(">="))) {
957 static void eq(int op
)
960 ts_pop_de2(NULL
, NULL
);
962 ts_push_bt(4 | BT_SIGNED
);
965 static void readeq(void)
969 if (!tok_jmp(TOK2("=="))) {
973 if (!tok_jmp(TOK2("!="))) {
981 static void readbitand(void)
984 while (!tok_jmp('&')) {
990 static void readxor(void)
993 while (!tok_jmp('^')) {
999 static void readbitor(void)
1002 while (!tok_jmp('|')) {
1008 #define MAXCOND (1 << 7)
1010 static void readand(void)
1012 long conds
[MAXCOND
];
1017 if (tok_see() != TOK2("&&"))
1021 conds
[nconds
++] = o_jz(0);
1022 while (!tok_jmp(TOK2("&&"))) {
1025 conds
[nconds
++] = o_jz(0);
1030 for (i
= 0; i
< nconds
; i
++)
1031 o_filljmp(conds
[i
]);
1036 ts_push_bt(4 | BT_SIGNED
);
1039 static void reador(void)
1041 long conds
[MAXCOND
];
1046 if (tok_see() != TOK2("||"))
1050 conds
[nconds
++] = o_jnz(0);
1051 while (!tok_jmp(TOK2("||"))) {
1054 conds
[nconds
++] = o_jnz(0);
1059 for (i
= 0; i
< nconds
; i
++)
1060 o_filljmp(conds
[i
]);
1065 ts_push_bt(4 | BT_SIGNED
);
1068 static int readcexpr_const(void)
1095 static void readcexpr(void)
1104 if (readcexpr_const()) {
1121 static void opassign(int op
, int ptrop
)
1123 struct type t
= ts
[nts
- 1];
1128 o_assign(TYPE_BT(&ts
[nts
- 1]));
1133 static void doassign(void)
1135 struct type t
= ts
[nts
- 1];
1136 if (!t
.ptr
&& t
.flags
& T_STRUCT
) {
1138 o_num(type_totsz(&t
));
1142 o_assign(TYPE_BT(&ts
[nts
- 1]));
1147 static void readexpr(void)
1150 if (!tok_jmp('=')) {
1155 if (!tok_jmp(TOK2("+="))) {
1159 if (!tok_jmp(TOK2("-="))) {
1163 if (!tok_jmp(TOK2("*="))) {
1167 if (!tok_jmp(TOK2("/="))) {
1171 if (!tok_jmp(TOK2("%="))) {
1175 if (!tok_jmp(TOK3("<<="))) {
1179 if (!tok_jmp(TOK3(">>="))) {
1183 if (!tok_jmp(TOK3("&="))) {
1187 if (!tok_jmp(TOK3("|="))) {
1191 if (!tok_jmp(TOK3("^="))) {
1197 static void readestmt(void)
1203 } while (!tok_jmp(','));
1206 static void o_localoff(long addr
, int off
)
1215 static struct type
*innertype(struct type
*t
)
1217 if (t
->flags
& T_ARRAY
&& !t
->ptr
)
1218 return innertype(&arrays
[t
->id
].type
);
1222 static void initexpr(struct type
*t
, int off
, void *obj
,
1223 void (*set
)(void *obj
, int off
, struct type
*t
))
1229 if (!t
->ptr
&& t
->flags
& T_STRUCT
) {
1230 struct structinfo
*si
= &structs
[t
->id
];
1232 for (i
= 0; i
< si
->nfields
; i
++) {
1233 struct name
*field
= &si
->fields
[i
];
1234 if (!tok_jmp('.')) {
1235 tok_expect(TOK_NAME
);
1236 field
= struct_field(t
->id
, tok_id());
1239 initexpr(&field
->type
, off
+ field
->addr
, obj
, set
);
1240 if (tok_jmp(',') || tok_see() == '}')
1243 } else if (t
->flags
& T_ARRAY
) {
1244 struct type
*t_de
= &arrays
[t
->id
].type
;
1246 for (i
= 0; ; i
++) {
1248 struct type
*it
= t_de
;
1249 if (!tok_jmp('[')) {
1256 if (tok_see() != '{')
1257 it
= innertype(t_de
);
1258 initexpr(it
, off
+ type_totsz(it
) * idx
, obj
, set
);
1259 if (tok_jmp(',') || tok_see() == '}')
1266 static void jumpbrace(void)
1269 while (tok_see() != '}' || depth
--)
1270 if (tok_get() == '{')
1275 static int initsize(void)
1277 long addr
= tok_addr();
1279 if (!tok_jmp(TOK_STR
)) {
1286 while (tok_jmp('}')) {
1288 if (!tok_jmp('[')) {
1297 while (tok_see() != '}' && tok_see() != ',')
1298 if (tok_get() == '{')
1307 #define F_GLOBAL(flags) (!((flags) & F_STATIC))
1309 static void globalinit(void *obj
, int off
, struct type
*t
)
1311 struct name
*name
= obj
;
1312 char *elfname
= *name
->elfname
? name
->elfname
: name
->name
;
1313 if (t
->flags
& T_ARRAY
&& tok_see() == TOK_STR
) {
1314 struct type
*t_de
= &arrays
[t
->id
].type
;
1315 if (!t_de
->ptr
&& !t_de
->flags
&& TYPE_SZ(t_de
) == 1) {
1318 tok_expect(TOK_STR
);
1320 memcpy((void *) name
->addr
+ off
, buf
, len
);
1325 o_datset(elfname
, off
, TYPE_BT(t
));
1329 static void globaldef(void *data
, struct name
*name
, unsigned flags
)
1331 struct type
*t
= &name
->type
;
1332 char *elfname
= *name
->elfname
? name
->elfname
: name
->name
;
1334 if (t
->flags
& T_ARRAY
&& !t
->ptr
&& !arrays
[t
->id
].n
)
1335 if (~flags
& F_EXTERN
)
1336 arrays
[t
->id
].n
= initsize();
1338 if (!(flags
& F_EXTERN
) && (!(t
->flags
& T_FUNC
) || t
->ptr
)) {
1340 name
->addr
= (long) dat_dat(elfname
, sz
, F_GLOBAL(flags
));
1342 dat_bss(elfname
, sz
, F_GLOBAL(flags
));
1346 initexpr(t
, 0, name
, globalinit
);
1349 static void localinit(void *obj
, int off
, struct type
*t
)
1351 long addr
= *(long *) obj
;
1352 if (t
->flags
& T_ARRAY
&& tok_see() == TOK_STR
) {
1353 struct type
*t_de
= &arrays
[t
->id
].type
;
1354 if (!t_de
->ptr
&& !t_de
->flags
&& TYPE_SZ(t_de
) == 1) {
1357 tok_expect(TOK_STR
);
1359 o_localoff(addr
, off
);
1360 o_sym(tmp_str(buf
, len
));
1367 o_localoff(addr
, off
);
1375 /* current function name */
1376 static char func_name
[NAMELEN
];
1378 static void localdef(void *data
, struct name
*name
, unsigned flags
)
1380 struct type
*t
= &name
->type
;
1381 if (flags
& (F_STATIC
| F_EXTERN
)) {
1382 sprintf(name
->elfname
, "__neatcc.%s.%s", func_name
, name
->name
);
1383 globaldef(data
, name
, flags
);
1386 if (t
->flags
& T_ARRAY
&& !t
->ptr
&& !arrays
[t
->id
].n
)
1387 arrays
[t
->id
].n
= initsize();
1388 name
->addr
= o_mklocal(type_totsz(&name
->type
));
1390 if (flags
& F_INIT
) {
1391 if (t
->flags
& (T_ARRAY
| T_STRUCT
) && !t
->ptr
) {
1392 o_local(name
->addr
);
1394 o_num(type_totsz(t
));
1398 initexpr(t
, 0, &name
->addr
, localinit
);
1402 static void funcdef(char *name
, struct type
*type
, struct name
*args
,
1403 int nargs
, unsigned flags
)
1405 struct name global
= {""};
1407 strcpy(global
.name
, name
);
1408 strcpy(func_name
, name
);
1409 memcpy(&global
.type
, type
, sizeof(*type
));
1410 o_func_beg(name
, F_GLOBAL(flags
));
1411 global_add(&global
);
1412 for (i
= 0; i
< nargs
; i
++) {
1413 args
[i
].addr
= o_arg(i
);
1414 local_add(&args
[i
]);
1418 static int readargs(struct name
*args
)
1422 while (tok_see() != ')') {
1423 if (!tok_jmp(TOK3("...")))
1425 readname(&args
[nargs
].type
, args
[nargs
].name
, NULL
, 0);
1426 array2ptr(&args
[nargs
].type
);
1432 if (nargs
== 1 && !TYPE_BT(&args
[0].type
))
1437 static int readname(struct type
*main
, char *name
,
1438 struct type
*base
, unsigned flags
)
1440 struct type tpool
[3];
1442 struct type
*type
= &tpool
[npool
++];
1443 struct type
*func
= NULL
;
1444 struct type
*ret
= NULL
;
1448 memset(tpool
, 0, sizeof(tpool
));
1452 if (basetype(type
, &flags
))
1455 memcpy(type
, base
, sizeof(*base
));
1458 if (!tok_jmp('(')) {
1460 type
= &tpool
[npool
++];
1464 if (!tok_jmp(TOK_NAME
) && name
)
1465 strcpy(name
, tok_id());
1466 while (!tok_jmp('[')) {
1472 err("const expr expected\n");
1477 for (i
= nar
- 1; i
>= 0; i
--) {
1478 type
->id
= array_add(type
, arsz
[i
]);
1479 if (func
&& i
== nar
- 1)
1480 func
= &arrays
[type
->id
].type
;
1481 type
->flags
= T_ARRAY
;
1487 if (tok_see() == '(') {
1488 struct name args
[MAXARGS
] = {{""}};
1489 int nargs
= readargs(args
);
1493 type
= &tpool
[npool
++];
1496 func
->flags
= T_FUNC
;
1498 func
->id
= func_create(ret
, args
, nargs
);
1499 if (fdef
&& tok_see() == '{') {
1500 funcdef(name
, func
, args
, nargs
, flags
);
1504 memcpy(main
, type
, sizeof(*type
));
1508 static int readdefs(void (*def
)(void *data
, struct name
*name
, unsigned flags
),
1512 unsigned base_flags
;
1513 if (basetype(&base
, &base_flags
))
1515 while (tok_see() != ';' && tok_see() != '{') {
1516 struct name name
= {{""}};
1517 unsigned flags
= base_flags
;
1518 if (readname(&name
.type
, name
.name
, &base
, flags
))
1522 def(data
, &name
, flags
);
1528 static void typedefdef(void *data
, struct name
*name
, unsigned flags
)
1530 typedef_add(name
->name
, &name
->type
);
1533 static void readstmt(void);
1535 #define MAXCASES (1 << 7)
1537 static void readswitch(void)
1539 int break_beg
= nbreaks
;
1540 long val_addr
= o_mklocal(LONGSZ
);
1541 long matched
[MAXCASES
];
1552 o_assign(TYPE_BT(&t
));
1557 while (tok_jmp('}')) {
1559 while (tok_see() == TOK_CASE
|| tok_see() == TOK_DEFAULT
) {
1561 matched
[nmatched
++] = o_jmp(0);
1564 if (!tok_jmp(TOK_CASE
)) {
1570 o_deref(TYPE_BT(&t
));
1578 if (!tok_jmp(TOK_DEFAULT
)) {
1583 for (i
= 0; i
< nmatched
; i
++)
1584 o_filljmp(matched
[i
]);
1588 o_rmlocal(val_addr
, LONGSZ
);
1591 break_fill(o_mklabel(), break_beg
);
1594 #define MAXGOTO (1 << 10)
1596 static struct gotoinfo
{
1602 static struct labelinfo
{
1608 static void goto_add(char *name
)
1610 strcpy(gotos
[ngotos
].name
, name
);
1611 gotos
[ngotos
++].addr
= o_jmp(0);
1614 static void label_add(char *name
)
1616 strcpy(labels
[nlabels
].name
, name
);
1617 labels
[nlabels
++].addr
= o_mklabel();
1620 static void goto_fill(void)
1623 for (i
= 0; i
< ngotos
; i
++)
1624 for (j
= 0; j
< nlabels
; j
++)
1625 if (!strcmp(gotos
[i
].name
, labels
[j
].name
)) {
1626 o_filljmp2(gotos
[i
].addr
, labels
[j
].addr
);
1631 static void readstmt(void)
1635 if (!tok_jmp('{')) {
1636 int _nlocals
= nlocals
;
1637 int _nglobals
= nglobals
;
1638 int _nenums
= nenums
;
1639 int _ntypedefs
= ntypedefs
;
1640 int _nstructs
= nstructs
;
1641 int _nfuncs
= nfuncs
;
1642 int _narrays
= narrays
;
1643 while (tok_jmp('}'))
1647 ntypedefs
= _ntypedefs
;
1648 nstructs
= _nstructs
;
1651 nglobals
= _nglobals
;
1654 if (!readdefs(localdef
, NULL
)) {
1658 if (!tok_jmp(TOK_TYPEDEF
)) {
1659 readdefs(typedefdef
, NULL
);
1663 if (!tok_jmp(TOK_IF
)) {
1671 if (!tok_jmp(TOK_ELSE
)) {
1681 if (!tok_jmp(TOK_WHILE
)) {
1683 int break_beg
= nbreaks
;
1684 int continue_beg
= ncontinues
;
1694 break_fill(o_mklabel(), break_beg
);
1695 continue_fill(l1
, continue_beg
);
1698 if (!tok_jmp(TOK_DO
)) {
1700 int break_beg
= nbreaks
;
1701 int continue_beg
= ncontinues
;
1704 tok_expect(TOK_WHILE
);
1711 break_fill(o_mklabel(), break_beg
);
1712 continue_fill(l2
, continue_beg
);
1715 if (!tok_jmp(TOK_FOR
)) {
1716 long l_check
, l_jump
, j_fail
, j_pass
;
1717 int break_beg
= nbreaks
;
1718 int continue_beg
= ncontinues
;
1721 if (tok_see() != ';')
1724 l_check
= o_mklabel();
1725 if (tok_see() != ';') {
1733 l_jump
= o_mklabel();
1734 if (tok_see() != ')')
1743 break_fill(o_mklabel(), break_beg
);
1744 continue_fill(l_jump
, continue_beg
);
1747 if (!tok_jmp(TOK_SWITCH
)) {
1751 if (!tok_jmp(TOK_RETURN
)) {
1752 int ret
= tok_see() != ';';
1761 if (!tok_jmp(TOK_BREAK
)) {
1763 breaks
[nbreaks
++] = o_jmp(0);
1766 if (!tok_jmp(TOK_CONTINUE
)) {
1768 continues
[ncontinues
++] = o_jmp(0);
1771 if (!tok_jmp(TOK_GOTO
)) {
1772 tok_expect(TOK_NAME
);
1779 if (!tok_jmp(':')) {
1780 label_add(tok_id());
1786 static void readdecl(void)
1788 if (!tok_jmp(TOK_TYPEDEF
)) {
1789 readdefs(typedefdef
, NULL
);
1793 readdefs(globaldef
, NULL
);
1794 if (tok_see() == '{') {
1798 func_name
[0] = '\0';
1807 static void parse(void)
1809 while (tok_see() != TOK_EOF
)
1813 int main(int argc
, char *argv
[])
1818 while (i
< argc
&& argv
[i
][0] == '-') {
1819 if (argv
[i
][1] == 'I')
1820 cpp_addpath(argv
[i
][2] ? argv
[i
] + 2 : argv
[++i
]);
1821 if (argv
[i
][1] == 'D') {
1822 char *name
= argv
[i
] + 2;
1824 char *eq
= strchr(name
, '=');
1829 cpp_define(name
, def
);
1834 die("neatcc: no file given\n");
1835 if (cpp_init(argv
[i
]))
1836 die("neatcc: cannot open input file\n");
1838 strcpy(obj
, argv
[i
]);
1839 obj
[strlen(obj
) - 1] = 'o';
1840 ofd
= open(obj
, O_WRONLY
| O_TRUNC
| O_CREAT
, 0600);