2 * neatcc - A small and simple C compiler
4 * Copyright (C) 2010 Ali Gholami Rudi
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License, as published by the
8 * Free Software Foundation.
15 #include <sys/types.h>
19 #define MAXLOCALS (1 << 10)
20 #define MAXGLOBALS (1 << 10)
21 #define MAXARGS (1 << 5)
22 #define print(s) write(2, (s), strlen(s));
24 #define TYPE_BT(t) ((t)->ptr ? LONGSZ : (t)->bt)
25 #define TYPE_SZ(t) ((t)->ptr ? LONGSZ : (t)->bt & BT_SZMASK)
39 int id
; /* for structs, functions and arrays */
43 static struct type ts
[MAXTMP
];
46 static void ts_push_bt(unsigned bt
)
53 static void ts_push(struct type
*t
)
55 memcpy(&ts
[nts
++], t
, sizeof(*t
));
56 if (t
->flags
& (T_FUNC
| T_ARRAY
) && !t
->ptr
)
60 static void ts_pop(struct type
*type
)
76 int len
= cpp_loc(err
, tok_addr());
77 strcpy(err
+ len
, msg
);
85 int unused
; /* unreferenced external symbols */
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
)
194 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 int tok_jmp(int tok
)
278 if (tok_see() != tok
)
284 static void tok_expect(int tok
)
286 if (tok_get() != tok
)
287 err("syntax error\n");
290 static unsigned bt_op(unsigned bt1
, unsigned bt2
)
292 unsigned s1
= BT_SZ(bt1
);
293 unsigned s2
= BT_SZ(bt2
);
294 return (bt1
| bt2
) & BT_SIGNED
| (s1
> s2
? s1
: s2
);
297 static void ts_binop(int op
)
303 ts_push_bt(bt_op(TYPE_BT(&t1
), TYPE_BT(&t2
)));
306 static void ts_addop(int op
)
313 if (!t1
.ptr
&& !t2
.ptr
) {
315 ts_push_bt(bt_op(TYPE_BT(&t1
), TYPE_BT(&t2
)));
318 if (t1
.ptr
&& !t2
.ptr
) {
324 if (!t1
.ptr
&& t2
.ptr
)
325 if (type_szde(&t2
) > 1) {
326 o_num(type_szde(&t2
), 4);
330 if (t1
.ptr
&& t2
.ptr
) {
331 int sz
= type_szde(&t1
);
336 ts_push_bt(4 | BT_SIGNED
);
342 #define ALIGN(x, a) (((x) + (a) - 1) & ~((a) - 1))
343 #define MIN(a, b) ((a) < (b) ? (a) : (b))
345 static int type_alignment(struct type
*t
)
347 if (t
->flags
& T_ARRAY
&& !t
->ptr
)
348 return type_alignment(&arrays
[t
->id
].type
);
349 if (t
->flags
& T_STRUCT
&& !t
->ptr
)
350 return type_alignment(&structs
[t
->id
].fields
[0].type
);
351 return MIN(LONGSZ
, type_totsz(t
));
354 static void structdef(void *data
, struct name
*name
, unsigned flags
)
356 struct structinfo
*si
= data
;
359 if (si
->size
< type_totsz(&name
->type
))
360 si
->size
= type_totsz(&name
->type
);
362 struct type
*t
= &name
->type
;
363 int alignment
= type_alignment(t
);
364 if (t
->flags
& T_ARRAY
&& !t
->ptr
)
365 alignment
= MIN(LONGSZ
, type_totsz(&arrays
[t
->id
].type
));
366 si
->size
= ALIGN(si
->size
, alignment
);
367 name
->addr
= si
->size
;
368 si
->size
+= type_totsz(&name
->type
);
370 memcpy(&si
->fields
[si
->nfields
++], name
, sizeof(*name
));
373 static int readdefs(void (*def
)(void *, struct name
*, unsigned f
), void *data
);
375 static int struct_create(char *name
, int isunion
)
377 int id
= struct_find(name
, isunion
);
378 struct structinfo
*si
= &structs
[id
];
380 while (tok_jmp('}')) {
381 readdefs(structdef
, si
);
387 static void readexpr(void);
389 static void enum_create(void)
393 while (tok_jmp('}')) {
395 tok_expect(TOK_NAME
);
396 strcpy(name
, tok_id());
397 if (tok_see() == '=') {
402 err("const expr expected!\n");
409 static int basetype(struct type
*type
, unsigned *flags
)
416 char name
[NAMELEN
] = "";
453 isunion
= tok_get() == TOK_UNION
;
454 if (!tok_jmp(TOK_NAME
))
455 strcpy(name
, tok_id());
456 if (tok_see() == '{')
457 type
->id
= struct_create(name
, isunion
);
459 type
->id
= struct_find(name
, isunion
);
460 type
->flags
|= T_STRUCT
;
466 if (tok_see() == '{')
468 type
->bt
= 4 | BT_SIGNED
;
471 if (tok_see() == TOK_NAME
) {
472 int id
= typedef_find(tok_id());
475 memcpy(type
, &typedefs
[id
].type
,
488 type
->bt
= size
| (sign
? BT_SIGNED
: 0);
492 static int readname(struct type
*main
, char *name
,
493 struct type
*base
, unsigned flags
);
495 static int readtype(struct type
*type
)
497 return readname(type
, NULL
, NULL
, 0);
500 static void readptrs(struct type
*type
)
502 while (!tok_jmp('*')) {
509 /* used to differenciate labels from case and cond exprs */
513 static void readpre(void);
515 static void readprimary(void)
518 if (!tok_jmp(TOK_NUM
)) {
520 int bt
= tok_num(&n
);
525 if (!tok_jmp(TOK_STR
)) {
529 t
.bt
= 1 | BT_SIGNED
;
534 o_symaddr(out_mkdat(NULL
, buf
, len
, 0), TYPE_BT(&t
));
538 if (!tok_jmp(TOK_NAME
)) {
539 struct name unkn
= {0};
540 char *name
= unkn
.name
;
542 strcpy(name
, tok_id());
543 /* don't search for labels here */
544 if (!ncexpr
&& !caseexpr
&& tok_see() == ':')
546 for (i
= nlocals
- 1; i
>= 0; --i
) {
547 struct type
*t
= &locals
[i
].type
;
548 if (!strcmp(locals
[i
].name
, name
)) {
549 o_local(locals
[i
].addr
, TYPE_BT(t
));
554 if ((n
= global_find(name
)) != -1) {
555 struct name
*g
= &globals
[n
];
556 struct type
*t
= &g
->type
;
559 if (t
->flags
& T_FUNC
&& !t
->ptr
)
560 g
->addr
= out_mkundef(name
, 0);
562 g
->addr
= out_mkundef(name
, type_totsz(t
));
564 o_symaddr(g
->addr
, TYPE_BT(t
));
568 if (!enum_find(&n
, name
)) {
569 ts_push_bt(4 | BT_SIGNED
);
570 o_num(n
, 4 | BT_SIGNED
);
573 if (tok_see() != '(')
574 err("unknown symbol\n");
575 unkn
.addr
= out_mkundef(unkn
.name
, 0);
578 o_symaddr(unkn
.addr
, LONGSZ
);
589 if (!t
.ptr
|| !o
.ptr
)
599 void arrayderef(struct type
*t
)
601 int sz
= type_totsz(t
);
610 static void inc_post(int inc_one
, int inc_many
)
612 struct type
*t
= &ts
[nts
- 1];
613 int sz
= type_szde(t
);
617 if (!t
->ptr
|| sz
== 1) {
623 o_assign(TYPE_BT(t
));
628 static void readfield(void)
632 tok_expect(TOK_NAME
);
635 field
= struct_field(t
.id
, tok_id());
637 o_num(field
->addr
, 4);
640 o_deref(TYPE_BT(&field
->type
));
641 ts_push(&field
->type
);
644 #define MAXFUNCS (1 << 10)
646 static struct funcinfo
{
647 struct type args
[MAXFIELDS
];
652 static unsigned ret_bt
;
654 static int func_create(struct type
*ret
, struct name
*args
, int nargs
)
656 struct funcinfo
*fi
= &funcs
[nfuncs
++];
658 if (nfuncs
>= MAXFUNCS
)
659 err("nomem: MAXFUNCS reached!\n");
660 memcpy(&fi
->ret
, ret
, sizeof(*ret
));
661 for (i
= 0; i
< nargs
; i
++)
662 memcpy(&fi
->args
[i
], &args
[i
].type
, sizeof(*ret
));
667 static void readcall(void)
670 unsigned bt
[MAXARGS
];
675 if (t
.flags
& T_FUNC
&& t
.ptr
> 0)
677 fi
= t
.flags
& T_FUNC
? &funcs
[t
.id
] : NULL
;
678 if (tok_see() != ')') {
681 bt
[argc
++] = TYPE_BT(&t
);
683 while (!tok_jmp(',')) {
686 bt
[argc
++] = TYPE_BT(&t
);
690 for (i
= 0; i
< fi
->nargs
; i
++)
691 bt
[i
] = TYPE_BT(&fi
->args
[i
]);
692 o_call(argc
, bt
, fi
? TYPE_BT(&fi
->ret
) : 4 | BT_SIGNED
);
696 ts_push_bt(4 | BT_SIGNED
);
699 static void readpost(void)
719 if (!tok_jmp(TOK2("++"))) {
720 inc_post(O_INC
, O_ADD
);
723 if (!tok_jmp(TOK2("--"))) {
724 inc_post(O_DEC
, O_SUB
);
732 if (!tok_jmp(TOK2("->"))) {
740 static void inc_pre(int inc_one
, int inc_many
)
746 sz
= (t
->flags
& T_ARRAY
|| t
->ptr
) ? type_szde(t
) : 1;
753 o_assign(TYPE_BT(t
));
757 static void readpre(void)
763 if (!(type
.flags
& T_FUNC
) && !type
.ptr
)
774 if (!(t
.flags
& T_FUNC
) || t
.ptr
> 0) {
776 o_deref(TYPE_BT(&t
));
786 ts_push_bt(4 | BT_SIGNED
);
799 if (!tok_jmp(TOK2("++"))) {
800 inc_pre(O_INC
, O_ADD
);
803 if (!tok_jmp(TOK2("--"))) {
804 inc_pre(O_DEC
, O_SUB
);
807 if (!tok_jmp(TOK_SIZEOF
)) {
809 int op
= !tok_jmp('(');
811 int nogen
= !o_nogen();
819 o_num(type_totsz(&t
), 4);
827 static void readmul(void)
850 static void readadd(void)
868 static void shift(int op
)
875 ts_push_bt(TYPE_BT(&t
));
878 static void readshift(void)
882 if (!tok_jmp(TOK2("<<"))) {
886 if (!tok_jmp(TOK2(">>"))) {
894 static void cmp(int op
)
900 ts_push_bt(4 | BT_SIGNED
);
903 static void readcmp(void)
915 if (!tok_jmp(TOK2("<="))) {
919 if (!tok_jmp(TOK2(">="))) {
927 static void eq(int op
)
933 ts_push_bt(4 | BT_SIGNED
);
936 static void readeq(void)
940 if (!tok_jmp(TOK2("=="))) {
944 if (!tok_jmp(TOK2("!="))) {
952 static void readbitand(void)
955 while (!tok_jmp('&')) {
961 static void readxor(void)
964 while (!tok_jmp('^')) {
970 static void readbitor(void)
973 while (!tok_jmp('|')) {
979 #define MAXCOND (1 << 7)
981 static void readand(void)
988 if (tok_see() != TOK2("&&"))
991 conds
[nconds
++] = o_jz(0);
993 while (!tok_jmp(TOK2("&&"))) {
995 conds
[nconds
++] = o_jz(0);
998 o_num(1, 4 | BT_SIGNED
);
1001 for (i
= 0; i
< nconds
; i
++)
1002 o_filljmp(conds
[i
]);
1003 o_num(0, 4 | BT_SIGNED
);
1007 ts_push_bt(4 | BT_SIGNED
);
1010 static void reador(void)
1012 long conds
[MAXCOND
];
1017 if (tok_see() != TOK2("||"))
1020 conds
[nconds
++] = o_jnz(0);
1022 while (!tok_jmp(TOK2("||"))) {
1024 conds
[nconds
++] = o_jnz(0);
1027 o_num(0, 4 | BT_SIGNED
);
1030 for (i
= 0; i
< nconds
; i
++)
1031 o_filljmp(conds
[i
]);
1032 o_num(1, 4 | BT_SIGNED
);
1036 ts_push_bt(4 | BT_SIGNED
);
1039 static int readcexpr_const(void)
1066 static void readcexpr(void)
1075 if (readcexpr_const()) {
1092 static void opassign(int op
, int ptrop
)
1094 struct type
*t
= &ts
[nts
- 1];
1095 if (ptrop
&& (t
->flags
& T_ARRAY
|| t
->ptr
)) {
1100 o_assign(TYPE_BT(&ts
[nts
- 1]));
1108 static void doassign(void)
1112 if (!t
.ptr
&& t
.flags
& T_STRUCT
)
1113 o_memcpy(type_totsz(&t
));
1115 o_assign(TYPE_BT(&ts
[nts
- 1]));
1118 static void readexpr(void)
1121 if (!tok_jmp('=')) {
1126 if (!tok_jmp(TOK2("+="))) {
1130 if (!tok_jmp(TOK2("-="))) {
1134 if (!tok_jmp(TOK2("*="))) {
1138 if (!tok_jmp(TOK2("/="))) {
1142 if (!tok_jmp(TOK2("%="))) {
1146 if (!tok_jmp(TOK3("<<="))) {
1150 if (!tok_jmp(TOK3(">>="))) {
1154 if (!tok_jmp(TOK3("&="))) {
1158 if (!tok_jmp(TOK3("|="))) {
1162 if (!tok_jmp(TOK3("^="))) {
1168 static void readestmt(void)
1174 } while (!tok_jmp(','));
1177 static void o_localoff(long addr
, int off
, unsigned bt
)
1188 static struct type
*innertype(struct type
*t
)
1190 if (t
->flags
& T_ARRAY
&& !t
->ptr
)
1191 return innertype(&arrays
[t
->id
].type
);
1195 static void initexpr(struct type
*t
, int off
, void *obj
,
1196 void (*set
)(void *obj
, int off
, struct type
*t
))
1202 if (!t
->ptr
&& t
->flags
& T_STRUCT
) {
1203 struct structinfo
*si
= &structs
[t
->id
];
1205 for (i
= 0; i
< si
->nfields
; i
++) {
1206 struct name
*field
= &si
->fields
[i
];
1207 if (!tok_jmp('.')) {
1208 tok_expect(TOK_NAME
);
1209 field
= struct_field(t
->id
, tok_id());
1212 initexpr(&field
->type
, off
+ field
->addr
, obj
, set
);
1213 if (tok_jmp(',') || tok_see() == '}')
1216 } else if (t
->flags
& T_ARRAY
) {
1217 struct type
*t_de
= &arrays
[t
->id
].type
;
1219 for (i
= 0; ; i
++) {
1221 struct type
*it
= t_de
;
1222 if (!tok_jmp('[')) {
1229 if (tok_see() != '{')
1230 it
= innertype(t_de
);
1231 initexpr(it
, off
+ type_totsz(it
) * idx
, obj
, set
);
1232 if (tok_jmp(',') || tok_see() == '}')
1239 static void jumpbrace(void)
1242 while (tok_see() != '}' || depth
--)
1243 if (tok_get() == '{')
1248 static int initsize(void)
1250 long addr
= tok_addr();
1252 if (!tok_jmp(TOK_STR
)) {
1259 while (tok_jmp('}')) {
1261 if (!tok_jmp('[')) {
1270 while (tok_see() != '}' && tok_see() != ',')
1271 if (tok_get() == '{')
1280 #define F_GLOBAL(flags) (!((flags) & F_STATIC))
1282 static void globalinit(void *obj
, int off
, struct type
*t
)
1284 long addr
= *(long *) obj
;
1285 if (t
->flags
& T_ARRAY
&& tok_see() == TOK_STR
) {
1286 struct type
*t_de
= &arrays
[t
->id
].type
;
1287 if (!t_de
->ptr
&& !t_de
->flags
&& TYPE_SZ(t_de
) == 1) {
1290 tok_expect(TOK_STR
);
1292 out_datcpy(addr
, off
, buf
, len
);
1297 o_datset(addr
, off
, TYPE_BT(t
));
1301 static void globaldef(void *data
, struct name
*name
, unsigned flags
)
1303 struct type
*t
= &name
->type
;
1304 char *varname
= flags
& F_STATIC
? NULL
: name
->name
;
1306 if (t
->flags
& T_ARRAY
&& !t
->ptr
&& !arrays
[t
->id
].n
)
1307 arrays
[t
->id
].n
= initsize();
1309 if (flags
& F_EXTERN
|| t
->flags
& T_FUNC
&& !t
->ptr
)
1311 else if (flags
& F_INIT
)
1312 name
->addr
= out_mkdat(varname
, NULL
, sz
, F_GLOBAL(flags
));
1314 name
->addr
= out_mkvar(varname
, sz
, F_GLOBAL(flags
));
1317 initexpr(t
, 0, &name
->addr
, globalinit
);
1320 static void localinit(void *obj
, int off
, struct type
*t
)
1322 long addr
= *(long *) obj
;
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 o_localoff(addr
, off
, TYPE_BT(t
));
1331 o_symaddr(out_mkdat(NULL
, buf
, len
, 0), TYPE_BT(t
));
1337 o_localoff(addr
, off
, TYPE_BT(t
));
1345 static void localdef(void *data
, struct name
*name
, unsigned flags
)
1347 struct type
*t
= &name
->type
;
1348 if (flags
& (F_STATIC
| F_EXTERN
)) {
1349 globaldef(data
, name
, flags
);
1352 if (t
->flags
& T_ARRAY
&& !t
->ptr
&& !arrays
[t
->id
].n
)
1353 arrays
[t
->id
].n
= initsize();
1354 name
->addr
= o_mklocal(type_totsz(&name
->type
));
1356 if (flags
& F_INIT
) {
1357 if (t
->flags
& (T_ARRAY
| T_STRUCT
) && !t
->ptr
) {
1358 o_local(name
->addr
, TYPE_BT(t
));
1359 o_memset(0, type_totsz(t
));
1362 initexpr(t
, 0, &name
->addr
, localinit
);
1366 static void funcdef(char *name
, struct type
*type
, struct name
*args
,
1367 int nargs
, unsigned flags
)
1371 strcpy(global
.name
, name
);
1372 memcpy(&global
.type
, type
, sizeof(*type
));
1373 global
.addr
= o_func_beg(name
, F_GLOBAL(flags
));
1375 global_add(&global
);
1376 ret_bt
= TYPE_BT(&funcs
[type
->id
].ret
);
1377 for (i
= 0; i
< nargs
; i
++) {
1378 args
[i
].addr
= o_arg(i
, type_totsz(&args
[i
].type
));
1379 local_add(&args
[i
]);
1383 static int readargs(struct name
*args
)
1387 while (tok_see() != ')') {
1388 if (!tok_jmp(TOK3("...")))
1390 readname(&args
[nargs
].type
, args
[nargs
].name
, NULL
, 0);
1391 array2ptr(&args
[nargs
].type
);
1397 if (nargs
== 1 && !TYPE_BT(&args
[0].type
))
1402 static int readname(struct type
*main
, char *name
,
1403 struct type
*base
, unsigned flags
)
1405 struct type tpool
[3];
1407 struct type
*type
= &tpool
[npool
++];
1408 struct type
*func
= NULL
;
1409 struct type
*ret
= NULL
;
1413 memset(tpool
, 0, sizeof(tpool
));
1417 if (basetype(type
, &flags
))
1420 memcpy(type
, base
, sizeof(*base
));
1423 if (!tok_jmp('(')) {
1425 type
= &tpool
[npool
++];
1429 if (!tok_jmp(TOK_NAME
) && name
)
1430 strcpy(name
, tok_id());
1431 while (!tok_jmp('[')) {
1437 err("const expr expected\n");
1442 for (i
= nar
- 1; i
>= 0; i
--) {
1443 type
->id
= array_add(type
, arsz
[i
]);
1444 if (func
&& i
== nar
- 1)
1445 func
= &arrays
[type
->id
].type
;
1446 type
->flags
= T_ARRAY
;
1452 if (tok_see() == '(') {
1453 struct name args
[MAXARGS
];
1454 int nargs
= readargs(args
);
1458 type
= &tpool
[npool
++];
1461 func
->flags
= T_FUNC
;
1463 func
->id
= func_create(ret
, args
, nargs
);
1464 if (fdef
&& tok_see() == '{') {
1465 funcdef(name
, func
, args
, nargs
, flags
);
1469 memcpy(main
, type
, sizeof(*type
));
1473 static int readdefs(void (*def
)(void *data
, struct name
*name
, unsigned flags
),
1478 if (basetype(&base
, &flags
))
1480 while (tok_see() != ';' && tok_see() != '{') {
1483 if (readname(&name
.type
, name
.name
, &base
, flags
))
1487 def(data
, &name
, flags
);
1493 static void typedefdef(void *data
, struct name
*name
, unsigned flags
)
1495 typedef_add(name
->name
, &name
->type
);
1498 static void readstmt(void);
1500 #define MAXCASES (1 << 7)
1502 static void readswitch(void)
1504 int break_beg
= nbreaks
;
1505 long val_addr
= o_mklocal(LONGSZ
);
1506 long matched
[MAXCASES
];
1515 o_local(val_addr
, TYPE_BT(&t
));
1517 o_assign(TYPE_BT(&t
));
1521 while (tok_jmp('}')) {
1523 while (tok_see() == TOK_CASE
|| tok_see() == TOK_DEFAULT
) {
1525 matched
[nmatched
++] = o_jmp(0);
1528 if (!tok_jmp(TOK_CASE
)) {
1532 o_local(val_addr
, TYPE_BT(&t
));
1541 if (!tok_jmp(TOK_DEFAULT
)) {
1546 for (i
= 0; i
< nmatched
; i
++)
1547 o_filljmp(matched
[i
]);
1551 o_rmlocal(val_addr
, LONGSZ
);
1554 break_fill(o_mklabel(), break_beg
);
1557 #define MAXGOTO (1 << 10)
1559 static struct gotoinfo
{
1565 static struct labelinfo
{
1571 static void goto_add(char *name
)
1573 strcpy(gotos
[ngotos
].name
, name
);
1574 gotos
[ngotos
++].addr
= o_jmp(0);
1577 static void label_add(char *name
)
1579 strcpy(labels
[nlabels
].name
, name
);
1580 labels
[nlabels
++].addr
= o_mklabel();
1583 static void goto_fill(void)
1586 for (i
= 0; i
< ngotos
; i
++)
1587 for (j
= 0; j
< nlabels
; j
++)
1588 if (!strcmp(gotos
[i
].name
, labels
[j
].name
)) {
1589 o_filljmp2(gotos
[i
].addr
, labels
[j
].addr
);
1594 static void readstmt(void)
1598 if (!tok_jmp('{')) {
1599 int _nlocals
= nlocals
;
1600 int _nglobals
= nglobals
;
1601 int _nenums
= nenums
;
1602 int _ntypedefs
= ntypedefs
;
1603 int _nstructs
= nstructs
;
1604 int _nfuncs
= nfuncs
;
1605 int _narrays
= narrays
;
1606 while (tok_jmp('}'))
1610 ntypedefs
= _ntypedefs
;
1611 nstructs
= _nstructs
;
1614 nglobals
= _nglobals
;
1617 if (!readdefs(localdef
, NULL
)) {
1621 if (!tok_jmp(TOK_TYPEDEF
)) {
1622 readdefs(typedefdef
, NULL
);
1626 if (!tok_jmp(TOK_IF
)) {
1633 if (!tok_jmp(TOK_ELSE
)) {
1643 if (!tok_jmp(TOK_WHILE
)) {
1645 int break_beg
= nbreaks
;
1646 int continue_beg
= ncontinues
;
1655 break_fill(o_mklabel(), break_beg
);
1656 continue_fill(l1
, continue_beg
);
1659 if (!tok_jmp(TOK_DO
)) {
1661 int break_beg
= nbreaks
;
1662 int continue_beg
= ncontinues
;
1665 tok_expect(TOK_WHILE
);
1671 break_fill(o_mklabel(), break_beg
);
1672 continue_fill(l2
, continue_beg
);
1675 if (!tok_jmp(TOK_FOR
)) {
1676 long l_check
, l_jump
, j_fail
, j_pass
;
1677 int break_beg
= nbreaks
;
1678 int continue_beg
= ncontinues
;
1681 if (tok_see() != ';')
1684 l_check
= o_mklabel();
1685 if (tok_see() != ';') {
1692 l_jump
= o_mklabel();
1693 if (tok_see() != ')')
1702 break_fill(o_mklabel(), break_beg
);
1703 continue_fill(l_jump
, continue_beg
);
1706 if (!tok_jmp(TOK_SWITCH
)) {
1710 if (!tok_jmp(TOK_RETURN
)) {
1711 int ret
= tok_see() != ';';
1718 if (!tok_jmp(TOK_BREAK
)) {
1720 breaks
[nbreaks
++] = o_jmp(0);
1723 if (!tok_jmp(TOK_CONTINUE
)) {
1725 continues
[ncontinues
++] = o_jmp(0);
1728 if (!tok_jmp(TOK_GOTO
)) {
1729 tok_expect(TOK_NAME
);
1736 if (!tok_jmp(':')) {
1737 label_add(tok_id());
1743 static void readdecl(void)
1745 if (!tok_jmp(TOK_TYPEDEF
)) {
1746 readdefs(typedefdef
, NULL
);
1750 readdefs(globaldef
, NULL
);
1751 if (tok_see() == '{') {
1763 static void parse(void)
1765 while (tok_see() != TOK_EOF
)
1769 int main(int argc
, char *argv
[])
1774 while (i
< argc
&& argv
[i
][0] == '-') {
1775 if (argv
[i
][1] == 'I')
1776 cpp_addpath(argv
[i
][2] ? argv
[i
] + 2 : argv
[++i
]);
1777 if (argv
[i
][1] == 'D') {
1778 char *name
= argv
[i
] + 2;
1780 char *eq
= strchr(name
, '=');
1785 cpp_define(name
, def
);
1790 die("neatcc: no file given\n");
1791 if (cpp_init(argv
[i
]))
1792 die("neatcc: cannot open input file\n");
1794 strcpy(obj
, argv
[i
]);
1795 obj
[strlen(obj
) - 1] = 'o';
1796 ofd
= open(obj
, O_WRONLY
| O_TRUNC
| O_CREAT
, 0600);