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
)
748 if (!t
.ptr
|| sz
== 1) {
753 o_assign(TYPE_BT(&t
));
758 static void readpre(void)
764 if (!(type
.flags
& T_FUNC
) && !type
.ptr
)
775 if (!(t
.flags
& T_FUNC
) || t
.ptr
> 0) {
777 o_deref(TYPE_BT(&t
));
787 ts_push_bt(4 | BT_SIGNED
);
800 if (!tok_jmp(TOK2("++"))) {
801 inc_pre(O_INC
, O_ADD
);
804 if (!tok_jmp(TOK2("--"))) {
805 inc_pre(O_DEC
, O_SUB
);
808 if (!tok_jmp(TOK_SIZEOF
)) {
810 int op
= !tok_jmp('(');
812 int nogen
= !o_nogen();
820 o_num(type_totsz(&t
), 4);
828 static void readmul(void)
851 static void readadd(void)
869 static void shift(int op
)
876 ts_push_bt(TYPE_BT(&t
));
879 static void readshift(void)
883 if (!tok_jmp(TOK2("<<"))) {
887 if (!tok_jmp(TOK2(">>"))) {
895 static void cmp(int op
)
901 ts_push_bt(4 | BT_SIGNED
);
904 static void readcmp(void)
916 if (!tok_jmp(TOK2("<="))) {
920 if (!tok_jmp(TOK2(">="))) {
928 static void eq(int op
)
934 ts_push_bt(4 | BT_SIGNED
);
937 static void readeq(void)
941 if (!tok_jmp(TOK2("=="))) {
945 if (!tok_jmp(TOK2("!="))) {
953 static void readbitand(void)
956 while (!tok_jmp('&')) {
962 static void readxor(void)
965 while (!tok_jmp('^')) {
971 static void readbitor(void)
974 while (!tok_jmp('|')) {
980 #define MAXCOND (1 << 7)
982 static void readand(void)
989 if (tok_see() != TOK2("&&"))
992 conds
[nconds
++] = o_jz(0);
994 while (!tok_jmp(TOK2("&&"))) {
996 conds
[nconds
++] = o_jz(0);
999 o_num(1, 4 | BT_SIGNED
);
1002 for (i
= 0; i
< nconds
; i
++)
1003 o_filljmp(conds
[i
]);
1004 o_num(0, 4 | BT_SIGNED
);
1008 ts_push_bt(4 | BT_SIGNED
);
1011 static void reador(void)
1013 long conds
[MAXCOND
];
1018 if (tok_see() != TOK2("||"))
1021 conds
[nconds
++] = o_jnz(0);
1023 while (!tok_jmp(TOK2("||"))) {
1025 conds
[nconds
++] = o_jnz(0);
1028 o_num(0, 4 | BT_SIGNED
);
1031 for (i
= 0; i
< nconds
; i
++)
1032 o_filljmp(conds
[i
]);
1033 o_num(1, 4 | BT_SIGNED
);
1037 ts_push_bt(4 | BT_SIGNED
);
1040 static int readcexpr_const(void)
1067 static void readcexpr(void)
1076 if (readcexpr_const()) {
1093 static void opassign(int op
, int ptrop
)
1095 struct type
*t
= &ts
[nts
- 1];
1096 if (ptrop
&& (t
->flags
& T_ARRAY
|| t
->ptr
)) {
1101 o_assign(TYPE_BT(&ts
[nts
- 1]));
1109 static void doassign(void)
1113 if (!t
.ptr
&& t
.flags
& T_STRUCT
)
1114 o_memcpy(type_totsz(&t
));
1116 o_assign(TYPE_BT(&ts
[nts
- 1]));
1119 static void readexpr(void)
1122 if (!tok_jmp('=')) {
1127 if (!tok_jmp(TOK2("+="))) {
1131 if (!tok_jmp(TOK2("-="))) {
1135 if (!tok_jmp(TOK2("*="))) {
1139 if (!tok_jmp(TOK2("/="))) {
1143 if (!tok_jmp(TOK2("%="))) {
1147 if (!tok_jmp(TOK3("<<="))) {
1151 if (!tok_jmp(TOK3(">>="))) {
1155 if (!tok_jmp(TOK3("&="))) {
1159 if (!tok_jmp(TOK3("|="))) {
1163 if (!tok_jmp(TOK3("^="))) {
1169 static void readestmt(void)
1175 } while (!tok_jmp(','));
1178 static void o_localoff(long addr
, int off
, unsigned bt
)
1189 static struct type
*innertype(struct type
*t
)
1191 if (t
->flags
& T_ARRAY
&& !t
->ptr
)
1192 return innertype(&arrays
[t
->id
].type
);
1196 static void initexpr(struct type
*t
, int off
, void *obj
,
1197 void (*set
)(void *obj
, int off
, struct type
*t
))
1203 if (!t
->ptr
&& t
->flags
& T_STRUCT
) {
1204 struct structinfo
*si
= &structs
[t
->id
];
1206 for (i
= 0; i
< si
->nfields
; i
++) {
1207 struct name
*field
= &si
->fields
[i
];
1208 if (!tok_jmp('.')) {
1209 tok_expect(TOK_NAME
);
1210 field
= struct_field(t
->id
, tok_id());
1213 initexpr(&field
->type
, off
+ field
->addr
, obj
, set
);
1214 if (tok_jmp(',') || tok_see() == '}')
1217 } else if (t
->flags
& T_ARRAY
) {
1218 struct type
*t_de
= &arrays
[t
->id
].type
;
1220 for (i
= 0; ; i
++) {
1222 struct type
*it
= t_de
;
1223 if (!tok_jmp('[')) {
1230 if (tok_see() != '{')
1231 it
= innertype(t_de
);
1232 initexpr(it
, off
+ type_totsz(it
) * idx
, obj
, set
);
1233 if (tok_jmp(',') || tok_see() == '}')
1240 static void jumpbrace(void)
1243 while (tok_see() != '}' || depth
--)
1244 if (tok_get() == '{')
1249 static int initsize(void)
1251 long addr
= tok_addr();
1253 if (!tok_jmp(TOK_STR
)) {
1260 while (tok_jmp('}')) {
1262 if (!tok_jmp('[')) {
1271 while (tok_see() != '}' && tok_see() != ',')
1272 if (tok_get() == '{')
1281 #define F_GLOBAL(flags) (!((flags) & F_STATIC))
1283 static void globalinit(void *obj
, int off
, struct type
*t
)
1285 long addr
= *(long *) obj
;
1286 if (t
->flags
& T_ARRAY
&& tok_see() == TOK_STR
) {
1287 struct type
*t_de
= &arrays
[t
->id
].type
;
1288 if (!t_de
->ptr
&& !t_de
->flags
&& TYPE_SZ(t_de
) == 1) {
1291 tok_expect(TOK_STR
);
1293 out_datcpy(addr
, off
, buf
, len
);
1298 o_datset(addr
, off
, TYPE_BT(t
));
1302 static void globaldef(void *data
, struct name
*name
, unsigned flags
)
1304 struct type
*t
= &name
->type
;
1305 char *varname
= flags
& F_STATIC
? NULL
: name
->name
;
1307 if (t
->flags
& T_ARRAY
&& !t
->ptr
&& !arrays
[t
->id
].n
)
1308 arrays
[t
->id
].n
= initsize();
1310 if (flags
& F_EXTERN
|| t
->flags
& T_FUNC
&& !t
->ptr
)
1312 else if (flags
& F_INIT
)
1313 name
->addr
= out_mkdat(varname
, NULL
, sz
, F_GLOBAL(flags
));
1315 name
->addr
= out_mkvar(varname
, sz
, F_GLOBAL(flags
));
1318 initexpr(t
, 0, &name
->addr
, globalinit
);
1321 static void localinit(void *obj
, int off
, struct type
*t
)
1323 long addr
= *(long *) obj
;
1324 if (t
->flags
& T_ARRAY
&& tok_see() == TOK_STR
) {
1325 struct type
*t_de
= &arrays
[t
->id
].type
;
1326 if (!t_de
->ptr
&& !t_de
->flags
&& TYPE_SZ(t_de
) == 1) {
1329 tok_expect(TOK_STR
);
1331 o_localoff(addr
, off
, TYPE_BT(t
));
1332 o_symaddr(out_mkdat(NULL
, buf
, len
, 0), TYPE_BT(t
));
1338 o_localoff(addr
, off
, TYPE_BT(t
));
1346 static void localdef(void *data
, struct name
*name
, unsigned flags
)
1348 struct type
*t
= &name
->type
;
1349 if (flags
& (F_STATIC
| F_EXTERN
)) {
1350 globaldef(data
, name
, flags
);
1353 if (t
->flags
& T_ARRAY
&& !t
->ptr
&& !arrays
[t
->id
].n
)
1354 arrays
[t
->id
].n
= initsize();
1355 name
->addr
= o_mklocal(type_totsz(&name
->type
));
1357 if (flags
& F_INIT
) {
1358 if (t
->flags
& (T_ARRAY
| T_STRUCT
) && !t
->ptr
) {
1359 o_local(name
->addr
, TYPE_BT(t
));
1360 o_memset(0, type_totsz(t
));
1363 initexpr(t
, 0, &name
->addr
, localinit
);
1367 static void funcdef(char *name
, struct type
*type
, struct name
*args
,
1368 int nargs
, unsigned flags
)
1372 strcpy(global
.name
, name
);
1373 memcpy(&global
.type
, type
, sizeof(*type
));
1374 global
.addr
= o_func_beg(name
, F_GLOBAL(flags
));
1376 global_add(&global
);
1377 ret_bt
= TYPE_BT(&funcs
[type
->id
].ret
);
1378 for (i
= 0; i
< nargs
; i
++) {
1379 args
[i
].addr
= o_arg(i
, type_totsz(&args
[i
].type
));
1380 local_add(&args
[i
]);
1384 static int readargs(struct name
*args
)
1388 while (tok_see() != ')') {
1389 if (!tok_jmp(TOK3("...")))
1391 readname(&args
[nargs
].type
, args
[nargs
].name
, NULL
, 0);
1392 array2ptr(&args
[nargs
].type
);
1398 if (nargs
== 1 && !TYPE_BT(&args
[0].type
))
1403 static int readname(struct type
*main
, char *name
,
1404 struct type
*base
, unsigned flags
)
1406 struct type tpool
[3];
1408 struct type
*type
= &tpool
[npool
++];
1409 struct type
*func
= NULL
;
1410 struct type
*ret
= NULL
;
1414 memset(tpool
, 0, sizeof(tpool
));
1418 if (basetype(type
, &flags
))
1421 memcpy(type
, base
, sizeof(*base
));
1424 if (!tok_jmp('(')) {
1426 type
= &tpool
[npool
++];
1430 if (!tok_jmp(TOK_NAME
) && name
)
1431 strcpy(name
, tok_id());
1432 while (!tok_jmp('[')) {
1438 err("const expr expected\n");
1443 for (i
= nar
- 1; i
>= 0; i
--) {
1444 type
->id
= array_add(type
, arsz
[i
]);
1445 if (type
->flags
& T_FUNC
)
1446 func
= &arrays
[type
->id
].type
;
1447 type
->flags
= T_ARRAY
;
1453 if (tok_see() == '(') {
1454 struct name args
[MAXARGS
];
1455 int nargs
= readargs(args
);
1459 type
= &tpool
[npool
++];
1462 func
->flags
= T_FUNC
;
1464 func
->id
= func_create(ret
, args
, nargs
);
1465 if (fdef
&& tok_see() == '{') {
1466 funcdef(name
, func
, args
, nargs
, flags
);
1470 memcpy(main
, type
, sizeof(*type
));
1474 static int readdefs(void (*def
)(void *data
, struct name
*name
, unsigned flags
),
1479 if (basetype(&base
, &flags
))
1481 while (tok_see() != ';' && tok_see() != '{') {
1484 if (readname(&name
.type
, name
.name
, &base
, flags
))
1488 def(data
, &name
, flags
);
1494 static void typedefdef(void *data
, struct name
*name
, unsigned flags
)
1496 typedef_add(name
->name
, &name
->type
);
1499 static void readstmt(void);
1501 #define MAXCASES (1 << 7)
1503 static void readswitch(void)
1505 int break_beg
= nbreaks
;
1506 long val_addr
= o_mklocal(LONGSZ
);
1507 long matched
[MAXCASES
];
1516 o_local(val_addr
, TYPE_BT(&t
));
1518 o_assign(TYPE_BT(&t
));
1522 while (tok_jmp('}')) {
1524 while (tok_see() == TOK_CASE
|| tok_see() == TOK_DEFAULT
) {
1526 matched
[nmatched
++] = o_jmp(0);
1529 if (!tok_jmp(TOK_CASE
)) {
1533 o_local(val_addr
, TYPE_BT(&t
));
1542 if (!tok_jmp(TOK_DEFAULT
)) {
1547 for (i
= 0; i
< nmatched
; i
++)
1548 o_filljmp(matched
[i
]);
1552 o_rmlocal(val_addr
, LONGSZ
);
1555 break_fill(o_mklabel(), break_beg
);
1558 #define MAXGOTO (1 << 10)
1560 static struct gotoinfo
{
1566 static struct labelinfo
{
1572 static void goto_add(char *name
)
1574 strcpy(gotos
[ngotos
].name
, name
);
1575 gotos
[ngotos
++].addr
= o_jmp(0);
1578 static void label_add(char *name
)
1580 strcpy(labels
[nlabels
].name
, name
);
1581 labels
[nlabels
++].addr
= o_mklabel();
1584 static void goto_fill(void)
1587 for (i
= 0; i
< ngotos
; i
++)
1588 for (j
= 0; j
< nlabels
; j
++)
1589 if (!strcmp(gotos
[i
].name
, labels
[j
].name
)) {
1590 o_filljmp2(gotos
[i
].addr
, labels
[j
].addr
);
1595 static void readstmt(void)
1599 if (!tok_jmp('{')) {
1600 int _nlocals
= nlocals
;
1601 int _nglobals
= nglobals
;
1602 int _nenums
= nenums
;
1603 int _ntypedefs
= ntypedefs
;
1604 int _nstructs
= nstructs
;
1605 int _nfuncs
= nfuncs
;
1606 int _narrays
= narrays
;
1607 while (tok_jmp('}'))
1611 ntypedefs
= _ntypedefs
;
1612 nstructs
= _nstructs
;
1615 nglobals
= _nglobals
;
1618 if (!readdefs(localdef
, NULL
)) {
1622 if (!tok_jmp(TOK_TYPEDEF
)) {
1623 readdefs(typedefdef
, NULL
);
1627 if (!tok_jmp(TOK_IF
)) {
1634 if (!tok_jmp(TOK_ELSE
)) {
1644 if (!tok_jmp(TOK_WHILE
)) {
1646 int break_beg
= nbreaks
;
1647 int continue_beg
= ncontinues
;
1656 break_fill(o_mklabel(), break_beg
);
1657 continue_fill(l1
, continue_beg
);
1660 if (!tok_jmp(TOK_DO
)) {
1662 int break_beg
= nbreaks
;
1663 int continue_beg
= ncontinues
;
1666 tok_expect(TOK_WHILE
);
1672 break_fill(o_mklabel(), break_beg
);
1673 continue_fill(l2
, continue_beg
);
1676 if (!tok_jmp(TOK_FOR
)) {
1677 long l_check
, l_jump
, j_fail
, j_pass
;
1678 int break_beg
= nbreaks
;
1679 int continue_beg
= ncontinues
;
1682 if (tok_see() != ';')
1685 l_check
= o_mklabel();
1686 if (tok_see() != ';') {
1693 l_jump
= o_mklabel();
1694 if (tok_see() != ')')
1703 break_fill(o_mklabel(), break_beg
);
1704 continue_fill(l_jump
, continue_beg
);
1707 if (!tok_jmp(TOK_SWITCH
)) {
1711 if (!tok_jmp(TOK_RETURN
)) {
1712 int ret
= tok_see() != ';';
1719 if (!tok_jmp(TOK_BREAK
)) {
1721 breaks
[nbreaks
++] = o_jmp(0);
1724 if (!tok_jmp(TOK_CONTINUE
)) {
1726 continues
[ncontinues
++] = o_jmp(0);
1729 if (!tok_jmp(TOK_GOTO
)) {
1730 tok_expect(TOK_NAME
);
1737 if (!tok_jmp(':')) {
1738 label_add(tok_id());
1744 static void readdecl(void)
1746 if (!tok_jmp(TOK_TYPEDEF
)) {
1747 readdefs(typedefdef
, NULL
);
1751 readdefs(globaldef
, NULL
);
1752 if (tok_see() == '{') {
1764 static void parse(void)
1766 while (tok_see() != TOK_EOF
)
1770 int main(int argc
, char *argv
[])
1775 while (i
< argc
&& argv
[i
][0] == '-') {
1776 if (argv
[i
][1] == 'I')
1777 cpp_addpath(argv
[i
][2] ? argv
[i
] + 2 : argv
[++i
]);
1778 if (argv
[i
][1] == 'D') {
1779 char *name
= argv
[i
] + 2;
1781 char *eq
= strchr(name
, '=');
1786 cpp_define(name
, def
);
1791 die("neatcc: no file given\n");
1792 if (cpp_init(argv
[i
]))
1793 die("neatcc: cannot open input file\n");
1795 strcpy(obj
, argv
[i
]);
1796 obj
[strlen(obj
) - 1] = 'o';
1797 ofd
= open(obj
, O_WRONLY
| O_TRUNC
| O_CREAT
, 0600);