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 op
)
904 ts_pop_de2(NULL
, &t
);
905 o_bop(op
| (BT_SIGNED
& TYPE_BT(&t
) ? O_SIGNED
: 0));
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
)
930 ts_pop_de2(&t1
, &t2
);
931 bt
= bt_op(TYPE_BT(&t1
), TYPE_BT(&t2
));
932 o_bop(op
| (bt
& BT_SIGNED
? O_SIGNED
: 0));
933 ts_push_bt(4 | BT_SIGNED
);
936 static void readcmp(void)
948 if (!tok_jmp(TOK2("<="))) {
952 if (!tok_jmp(TOK2(">="))) {
960 static void eq(int op
)
963 ts_pop_de2(NULL
, NULL
);
965 ts_push_bt(4 | BT_SIGNED
);
968 static void readeq(void)
972 if (!tok_jmp(TOK2("=="))) {
976 if (!tok_jmp(TOK2("!="))) {
984 static void readbitand(void)
987 while (!tok_jmp('&')) {
993 static void readxor(void)
996 while (!tok_jmp('^')) {
1002 static void readbitor(void)
1005 while (!tok_jmp('|')) {
1011 #define MAXCOND (1 << 7)
1013 static void readand(void)
1015 long conds
[MAXCOND
];
1020 if (tok_see() != TOK2("&&"))
1024 conds
[nconds
++] = o_jz(0);
1025 while (!tok_jmp(TOK2("&&"))) {
1028 conds
[nconds
++] = o_jz(0);
1033 for (i
= 0; i
< nconds
; i
++)
1034 o_filljmp(conds
[i
]);
1039 ts_push_bt(4 | BT_SIGNED
);
1042 static void reador(void)
1044 long conds
[MAXCOND
];
1049 if (tok_see() != TOK2("||"))
1053 conds
[nconds
++] = o_jnz(0);
1054 while (!tok_jmp(TOK2("||"))) {
1057 conds
[nconds
++] = o_jnz(0);
1062 for (i
= 0; i
< nconds
; i
++)
1063 o_filljmp(conds
[i
]);
1068 ts_push_bt(4 | BT_SIGNED
);
1071 static int readcexpr_const(void)
1098 static void readcexpr(void)
1107 if (readcexpr_const()) {
1124 static void opassign(int op
, int ptrop
)
1126 struct type t
= ts
[nts
- 1];
1131 o_assign(TYPE_BT(&ts
[nts
- 1]));
1136 static void doassign(void)
1138 struct type t
= ts
[nts
- 1];
1139 if (!t
.ptr
&& t
.flags
& T_STRUCT
) {
1141 o_num(type_totsz(&t
));
1145 o_assign(TYPE_BT(&ts
[nts
- 1]));
1150 static void readexpr(void)
1153 if (!tok_jmp('=')) {
1158 if (!tok_jmp(TOK2("+="))) {
1162 if (!tok_jmp(TOK2("-="))) {
1166 if (!tok_jmp(TOK2("*="))) {
1170 if (!tok_jmp(TOK2("/="))) {
1174 if (!tok_jmp(TOK2("%="))) {
1178 if (!tok_jmp(TOK3("<<="))) {
1182 if (!tok_jmp(TOK3(">>="))) {
1186 if (!tok_jmp(TOK3("&="))) {
1190 if (!tok_jmp(TOK3("|="))) {
1194 if (!tok_jmp(TOK3("^="))) {
1200 static void readestmt(void)
1206 } while (!tok_jmp(','));
1209 static void o_localoff(long addr
, int off
)
1218 static struct type
*innertype(struct type
*t
)
1220 if (t
->flags
& T_ARRAY
&& !t
->ptr
)
1221 return innertype(&arrays
[t
->id
].type
);
1225 static void initexpr(struct type
*t
, int off
, void *obj
,
1226 void (*set
)(void *obj
, int off
, struct type
*t
))
1232 if (!t
->ptr
&& t
->flags
& T_STRUCT
) {
1233 struct structinfo
*si
= &structs
[t
->id
];
1235 for (i
= 0; i
< si
->nfields
; i
++) {
1236 struct name
*field
= &si
->fields
[i
];
1237 if (!tok_jmp('.')) {
1238 tok_expect(TOK_NAME
);
1239 field
= struct_field(t
->id
, tok_id());
1242 initexpr(&field
->type
, off
+ field
->addr
, obj
, set
);
1243 if (tok_jmp(',') || tok_see() == '}')
1246 } else if (t
->flags
& T_ARRAY
) {
1247 struct type
*t_de
= &arrays
[t
->id
].type
;
1249 for (i
= 0; ; i
++) {
1251 struct type
*it
= t_de
;
1252 if (!tok_jmp('[')) {
1259 if (tok_see() != '{')
1260 it
= innertype(t_de
);
1261 initexpr(it
, off
+ type_totsz(it
) * idx
, obj
, set
);
1262 if (tok_jmp(',') || tok_see() == '}')
1269 static void jumpbrace(void)
1272 while (tok_see() != '}' || depth
--)
1273 if (tok_get() == '{')
1278 static int initsize(void)
1280 long addr
= tok_addr();
1282 if (!tok_jmp(TOK_STR
)) {
1289 while (tok_jmp('}')) {
1291 if (!tok_jmp('[')) {
1300 while (tok_see() != '}' && tok_see() != ',')
1301 if (tok_get() == '{')
1310 #define F_GLOBAL(flags) (!((flags) & F_STATIC))
1312 static void globalinit(void *obj
, int off
, struct type
*t
)
1314 struct name
*name
= obj
;
1315 char *elfname
= *name
->elfname
? name
->elfname
: name
->name
;
1316 if (t
->flags
& T_ARRAY
&& tok_see() == TOK_STR
) {
1317 struct type
*t_de
= &arrays
[t
->id
].type
;
1318 if (!t_de
->ptr
&& !t_de
->flags
&& TYPE_SZ(t_de
) == 1) {
1321 tok_expect(TOK_STR
);
1323 memcpy((void *) name
->addr
+ off
, buf
, len
);
1328 o_datset(elfname
, off
, TYPE_BT(t
));
1332 static void globaldef(void *data
, struct name
*name
, unsigned flags
)
1334 struct type
*t
= &name
->type
;
1335 char *elfname
= *name
->elfname
? name
->elfname
: name
->name
;
1337 if (t
->flags
& T_ARRAY
&& !t
->ptr
&& !arrays
[t
->id
].n
)
1338 if (~flags
& F_EXTERN
)
1339 arrays
[t
->id
].n
= initsize();
1341 if (!(flags
& F_EXTERN
) && (!(t
->flags
& T_FUNC
) || t
->ptr
)) {
1343 name
->addr
= (long) dat_dat(elfname
, sz
, F_GLOBAL(flags
));
1345 dat_bss(elfname
, sz
, F_GLOBAL(flags
));
1349 initexpr(t
, 0, name
, globalinit
);
1352 static void localinit(void *obj
, int off
, struct type
*t
)
1354 long addr
= *(long *) obj
;
1355 if (t
->flags
& T_ARRAY
&& tok_see() == TOK_STR
) {
1356 struct type
*t_de
= &arrays
[t
->id
].type
;
1357 if (!t_de
->ptr
&& !t_de
->flags
&& TYPE_SZ(t_de
) == 1) {
1360 tok_expect(TOK_STR
);
1362 o_localoff(addr
, off
);
1363 o_sym(tmp_str(buf
, len
));
1370 o_localoff(addr
, off
);
1378 /* current function name */
1379 static char func_name
[NAMELEN
];
1381 static void localdef(void *data
, struct name
*name
, unsigned flags
)
1383 struct type
*t
= &name
->type
;
1384 if (flags
& (F_STATIC
| F_EXTERN
)) {
1385 sprintf(name
->elfname
, "__neatcc.%s.%s", func_name
, name
->name
);
1386 globaldef(data
, name
, flags
);
1389 if (t
->flags
& T_ARRAY
&& !t
->ptr
&& !arrays
[t
->id
].n
)
1390 arrays
[t
->id
].n
= initsize();
1391 name
->addr
= o_mklocal(type_totsz(&name
->type
));
1393 if (flags
& F_INIT
) {
1394 if (t
->flags
& (T_ARRAY
| T_STRUCT
) && !t
->ptr
) {
1395 o_local(name
->addr
);
1397 o_num(type_totsz(t
));
1401 initexpr(t
, 0, &name
->addr
, localinit
);
1405 static void funcdef(char *name
, struct type
*type
, struct name
*args
,
1406 int nargs
, unsigned flags
)
1408 struct name global
= {""};
1410 strcpy(global
.name
, name
);
1411 strcpy(func_name
, name
);
1412 memcpy(&global
.type
, type
, sizeof(*type
));
1413 o_func_beg(name
, F_GLOBAL(flags
));
1414 global_add(&global
);
1415 for (i
= 0; i
< nargs
; i
++) {
1416 args
[i
].addr
= o_arg(i
);
1417 local_add(&args
[i
]);
1421 static int readargs(struct name
*args
)
1425 while (tok_see() != ')') {
1426 if (!tok_jmp(TOK3("...")))
1428 readname(&args
[nargs
].type
, args
[nargs
].name
, NULL
, 0);
1429 array2ptr(&args
[nargs
].type
);
1435 if (nargs
== 1 && !TYPE_BT(&args
[0].type
))
1440 static int readname(struct type
*main
, char *name
,
1441 struct type
*base
, unsigned flags
)
1443 struct type tpool
[3];
1445 struct type
*type
= &tpool
[npool
++];
1446 struct type
*func
= NULL
;
1447 struct type
*ret
= NULL
;
1451 memset(tpool
, 0, sizeof(tpool
));
1455 if (basetype(type
, &flags
))
1458 memcpy(type
, base
, sizeof(*base
));
1461 if (!tok_jmp('(')) {
1463 type
= &tpool
[npool
++];
1467 if (!tok_jmp(TOK_NAME
) && name
)
1468 strcpy(name
, tok_id());
1469 while (!tok_jmp('[')) {
1475 err("const expr expected\n");
1480 for (i
= nar
- 1; i
>= 0; i
--) {
1481 type
->id
= array_add(type
, arsz
[i
]);
1482 if (func
&& i
== nar
- 1)
1483 func
= &arrays
[type
->id
].type
;
1484 type
->flags
= T_ARRAY
;
1490 if (tok_see() == '(') {
1491 struct name args
[MAXARGS
] = {{""}};
1492 int nargs
= readargs(args
);
1496 type
= &tpool
[npool
++];
1499 func
->flags
= T_FUNC
;
1501 func
->id
= func_create(ret
, args
, nargs
);
1502 if (fdef
&& tok_see() == '{') {
1503 funcdef(name
, func
, args
, nargs
, flags
);
1507 memcpy(main
, type
, sizeof(*type
));
1511 static int readdefs(void (*def
)(void *data
, struct name
*name
, unsigned flags
),
1515 unsigned base_flags
;
1516 if (basetype(&base
, &base_flags
))
1518 while (tok_see() != ';' && tok_see() != '{') {
1519 struct name name
= {{""}};
1520 unsigned flags
= base_flags
;
1521 if (readname(&name
.type
, name
.name
, &base
, flags
))
1525 def(data
, &name
, flags
);
1531 static void typedefdef(void *data
, struct name
*name
, unsigned flags
)
1533 typedef_add(name
->name
, &name
->type
);
1536 static void readstmt(void);
1538 #define MAXCASES (1 << 7)
1540 static void readswitch(void)
1542 int break_beg
= nbreaks
;
1543 long val_addr
= o_mklocal(LONGSZ
);
1544 long matched
[MAXCASES
];
1555 o_assign(TYPE_BT(&t
));
1560 while (tok_jmp('}')) {
1562 while (tok_see() == TOK_CASE
|| tok_see() == TOK_DEFAULT
) {
1564 matched
[nmatched
++] = o_jmp(0);
1567 if (!tok_jmp(TOK_CASE
)) {
1573 o_deref(TYPE_BT(&t
));
1581 if (!tok_jmp(TOK_DEFAULT
)) {
1586 for (i
= 0; i
< nmatched
; i
++)
1587 o_filljmp(matched
[i
]);
1591 o_rmlocal(val_addr
, LONGSZ
);
1594 break_fill(o_mklabel(), break_beg
);
1597 #define MAXGOTO (1 << 10)
1599 static struct gotoinfo
{
1605 static struct labelinfo
{
1611 static void goto_add(char *name
)
1613 strcpy(gotos
[ngotos
].name
, name
);
1614 gotos
[ngotos
++].addr
= o_jmp(0);
1617 static void label_add(char *name
)
1619 strcpy(labels
[nlabels
].name
, name
);
1620 labels
[nlabels
++].addr
= o_mklabel();
1623 static void goto_fill(void)
1626 for (i
= 0; i
< ngotos
; i
++)
1627 for (j
= 0; j
< nlabels
; j
++)
1628 if (!strcmp(gotos
[i
].name
, labels
[j
].name
)) {
1629 o_filljmp2(gotos
[i
].addr
, labels
[j
].addr
);
1634 static void readstmt(void)
1638 if (!tok_jmp('{')) {
1639 int _nlocals
= nlocals
;
1640 int _nglobals
= nglobals
;
1641 int _nenums
= nenums
;
1642 int _ntypedefs
= ntypedefs
;
1643 int _nstructs
= nstructs
;
1644 int _nfuncs
= nfuncs
;
1645 int _narrays
= narrays
;
1646 while (tok_jmp('}'))
1650 ntypedefs
= _ntypedefs
;
1651 nstructs
= _nstructs
;
1654 nglobals
= _nglobals
;
1657 if (!readdefs(localdef
, NULL
)) {
1661 if (!tok_jmp(TOK_TYPEDEF
)) {
1662 readdefs(typedefdef
, NULL
);
1666 if (!tok_jmp(TOK_IF
)) {
1674 if (!tok_jmp(TOK_ELSE
)) {
1684 if (!tok_jmp(TOK_WHILE
)) {
1686 int break_beg
= nbreaks
;
1687 int continue_beg
= ncontinues
;
1697 break_fill(o_mklabel(), break_beg
);
1698 continue_fill(l1
, continue_beg
);
1701 if (!tok_jmp(TOK_DO
)) {
1703 int break_beg
= nbreaks
;
1704 int continue_beg
= ncontinues
;
1707 tok_expect(TOK_WHILE
);
1714 break_fill(o_mklabel(), break_beg
);
1715 continue_fill(l2
, continue_beg
);
1718 if (!tok_jmp(TOK_FOR
)) {
1719 long l_check
, l_jump
, j_fail
, j_pass
;
1720 int break_beg
= nbreaks
;
1721 int continue_beg
= ncontinues
;
1724 if (tok_see() != ';')
1727 l_check
= o_mklabel();
1728 if (tok_see() != ';') {
1736 l_jump
= o_mklabel();
1737 if (tok_see() != ')')
1746 break_fill(o_mklabel(), break_beg
);
1747 continue_fill(l_jump
, continue_beg
);
1750 if (!tok_jmp(TOK_SWITCH
)) {
1754 if (!tok_jmp(TOK_RETURN
)) {
1755 int ret
= tok_see() != ';';
1764 if (!tok_jmp(TOK_BREAK
)) {
1766 breaks
[nbreaks
++] = o_jmp(0);
1769 if (!tok_jmp(TOK_CONTINUE
)) {
1771 continues
[ncontinues
++] = o_jmp(0);
1774 if (!tok_jmp(TOK_GOTO
)) {
1775 tok_expect(TOK_NAME
);
1782 if (!tok_jmp(':')) {
1783 label_add(tok_id());
1789 static void readdecl(void)
1791 if (!tok_jmp(TOK_TYPEDEF
)) {
1792 readdefs(typedefdef
, NULL
);
1796 readdefs(globaldef
, NULL
);
1797 if (tok_see() == '{') {
1801 func_name
[0] = '\0';
1810 static void parse(void)
1812 while (tok_see() != TOK_EOF
)
1816 int main(int argc
, char *argv
[])
1821 while (i
< argc
&& argv
[i
][0] == '-') {
1822 if (argv
[i
][1] == 'I')
1823 cpp_addpath(argv
[i
][2] ? argv
[i
] + 2 : argv
[++i
]);
1824 if (argv
[i
][1] == 'D') {
1825 char *name
= argv
[i
] + 2;
1827 char *eq
= strchr(name
, '=');
1832 cpp_define(name
, def
);
1837 die("neatcc: no file given\n");
1838 if (cpp_init(argv
[i
]))
1839 die("neatcc: cannot open input file\n");
1841 strcpy(obj
, argv
[i
]);
1842 obj
[strlen(obj
) - 1] = 'o';
1843 ofd
= open(obj
, O_WRONLY
| O_TRUNC
| O_CREAT
, 0600);