2 * neatcc - A small and simple x86_64 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 ? 8 : (t)->bt)
25 #define TYPE_SZ(t) ((t)->ptr ? 8 : (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(void (*o_sth
)(void))
303 ts_push_bt(bt_op(TYPE_BT(&t1
), TYPE_BT(&t2
)));
306 static void ts_binop_add(void (*o_sth
)(void))
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 void structdef(void *data
, struct name
*name
, unsigned flags
)
347 struct structinfo
*si
= data
;
350 if (si
->size
< type_totsz(&name
->type
))
351 si
->size
= type_totsz(&name
->type
);
353 struct type
*t
= &name
->type
;
354 int alignment
= MIN(8, type_totsz(t
));
355 if (t
->flags
& T_ARRAY
&& !t
->ptr
)
356 alignment
= MIN(8, type_totsz(&arrays
[t
->id
].type
));
357 si
->size
= ALIGN(si
->size
, alignment
);
358 name
->addr
= si
->size
;
359 si
->size
+= type_totsz(&name
->type
);
361 memcpy(&si
->fields
[si
->nfields
++], name
, sizeof(*name
));
364 static int readdefs(void (*def
)(void *, struct name
*, unsigned f
), void *data
);
366 static int struct_create(char *name
, int isunion
)
368 int id
= struct_find(name
, isunion
);
369 struct structinfo
*si
= &structs
[id
];
371 while (tok_jmp('}')) {
372 readdefs(structdef
, si
);
378 static void readexpr(void);
380 static void enum_create(void)
384 while (tok_jmp('}')) {
386 tok_expect(TOK_NAME
);
387 strcpy(name
, tok_id());
388 if (tok_see() == '=') {
393 err("const expr expected!\n");
400 static int basetype(struct type
*type
, unsigned *flags
)
407 char name
[NAMELEN
] = "";
444 isunion
= tok_get() == TOK_UNION
;
445 if (!tok_jmp(TOK_NAME
))
446 strcpy(name
, tok_id());
447 if (tok_see() == '{')
448 type
->id
= struct_create(name
, isunion
);
450 type
->id
= struct_find(name
, isunion
);
451 type
->flags
|= T_STRUCT
;
457 if (tok_see() == '{')
459 type
->bt
= 4 | BT_SIGNED
;
462 if (tok_see() == TOK_NAME
) {
463 int id
= typedef_find(tok_id());
466 memcpy(type
, &typedefs
[id
].type
,
479 type
->bt
= size
| (sign
? BT_SIGNED
: 0);
483 static int readname(struct type
*main
, char *name
,
484 struct type
*base
, unsigned flags
);
486 static int readtype(struct type
*type
)
488 return readname(type
, NULL
, NULL
, 0);
491 static void readptrs(struct type
*type
)
493 while (!tok_jmp('*')) {
500 /* used to differenciate labels from case and cond exprs */
504 static void readpre(void);
506 static void readprimary(void)
509 if (!tok_jmp(TOK_NUM
)) {
511 int bt
= tok_num(&n
);
516 if (!tok_jmp(TOK_STR
)) {
520 t
.bt
= 1 | BT_SIGNED
;
525 o_symaddr(out_mkdat(NULL
, buf
, len
, 0), TYPE_BT(&t
));
529 if (!tok_jmp(TOK_NAME
)) {
530 struct name unkn
= {0};
531 char *name
= unkn
.name
;
533 strcpy(name
, tok_id());
534 /* don't search for labels here */
535 if (!ncexpr
&& !caseexpr
&& tok_see() == ':')
537 for (i
= nlocals
- 1; i
>= 0; --i
) {
538 struct type
*t
= &locals
[i
].type
;
539 if (!strcmp(locals
[i
].name
, name
)) {
540 o_local(locals
[i
].addr
, TYPE_BT(t
));
545 if ((n
= global_find(name
)) != -1) {
546 struct name
*g
= &globals
[n
];
547 struct type
*t
= &g
->type
;
550 if (t
->flags
& T_FUNC
&& !t
->ptr
)
551 g
->addr
= out_mkundef(name
, 0);
553 g
->addr
= out_mkundef(name
, type_totsz(t
));
555 o_symaddr(g
->addr
, TYPE_BT(t
));
559 if (!enum_find(&n
, name
)) {
560 ts_push_bt(4 | BT_SIGNED
);
561 o_num(n
, 4 | BT_SIGNED
);
564 if (tok_see() != '(')
565 err("unknown symbol\n");
566 unkn
.addr
= out_mkundef(unkn
.name
, 0);
569 o_symaddr(unkn
.addr
, 8);
580 if (!t
.ptr
|| !o
.ptr
)
590 void arrayderef(struct type
*t
)
592 int sz
= type_totsz(t
);
601 static void inc_post(void (*op
)(void))
603 unsigned bt
= TYPE_BT(&ts
[nts
- 1]);
617 static void readfield(void)
621 tok_expect(TOK_NAME
);
624 field
= struct_field(t
.id
, tok_id());
626 o_num(field
->addr
, 4);
629 o_deref(TYPE_BT(&field
->type
));
630 ts_push(&field
->type
);
633 #define MAXFUNCS (1 << 10)
635 static struct funcinfo
{
636 struct type args
[MAXFIELDS
];
641 static unsigned ret_bt
;
643 static int func_create(struct type
*ret
, struct name
*args
, int nargs
)
645 struct funcinfo
*fi
= &funcs
[nfuncs
++];
647 if (nfuncs
>= MAXFUNCS
)
648 err("nomem: MAXFUNCS reached!\n");
649 memcpy(&fi
->ret
, ret
, sizeof(*ret
));
650 for (i
= 0; i
< nargs
; i
++)
651 memcpy(&fi
->args
[i
], &args
[i
].type
, sizeof(*ret
));
656 static void readcall(void)
659 unsigned bt
[MAXARGS
];
664 if (t
.flags
& T_FUNC
&& t
.ptr
> 0)
666 fi
= t
.flags
& T_FUNC
? &funcs
[t
.id
] : NULL
;
667 if (tok_see() != ')') {
670 bt
[argc
++] = TYPE_BT(&t
);
672 while (!tok_jmp(',')) {
675 bt
[argc
++] = TYPE_BT(&t
);
679 for (i
= 0; i
< fi
->nargs
; i
++)
680 bt
[i
] = TYPE_BT(&fi
->args
[i
]);
681 o_call(argc
, bt
, fi
? TYPE_BT(&fi
->ret
) : 4 | BT_SIGNED
);
685 ts_push_bt(4 | BT_SIGNED
);
688 static void readpost(void)
708 if (!tok_jmp(TOK2("++"))) {
712 if (!tok_jmp(TOK2("--"))) {
721 if (!tok_jmp(TOK2("->"))) {
729 static void inc_pre(void (*op
)(void))
733 bt
= TYPE_BT(&ts
[nts
- 1]);
743 static void readpre(void)
749 if (!(type
.flags
& T_FUNC
) && !type
.ptr
)
760 if (!(t
.flags
& T_FUNC
) || t
.ptr
> 0) {
762 o_deref(TYPE_BT(&t
));
772 ts_push_bt(4 | BT_SIGNED
);
785 if (!tok_jmp(TOK2("++"))) {
789 if (!tok_jmp(TOK2("--"))) {
793 if (!tok_jmp(TOK_SIZEOF
)) {
795 int op
= !tok_jmp('(');
797 int nogen
= !o_nogen();
805 o_num(type_totsz(&t
), 4);
813 static void readmul(void)
836 static void readadd(void)
854 static void shift(void (*op
)(void))
861 ts_push_bt(TYPE_BT(&t
));
864 static void readshift(void)
868 if (!tok_jmp(TOK2("<<"))) {
872 if (!tok_jmp(TOK2(">>"))) {
880 static void cmp(void (*op
)(void))
886 ts_push_bt(4 | BT_SIGNED
);
889 static void readcmp(void)
901 if (!tok_jmp(TOK2("<="))) {
905 if (!tok_jmp(TOK2(">="))) {
913 static void eq(void (*op
)(void))
919 ts_push_bt(4 | BT_SIGNED
);
922 static void readeq(void)
926 if (!tok_jmp(TOK2("=="))) {
930 if (!tok_jmp(TOK2("!="))) {
938 static void readbitand(void)
941 while (!tok_jmp('&')) {
947 static void readxor(void)
950 while (!tok_jmp('^')) {
956 static void readbitor(void)
959 while (!tok_jmp('|')) {
965 #define MAXCOND (1 << 7)
967 static void readand(void)
974 if (tok_see() != TOK2("&&"))
977 conds
[nconds
++] = o_jz(0);
979 while (!tok_jmp(TOK2("&&"))) {
981 conds
[nconds
++] = o_jz(0);
984 o_num(1, 4 | BT_SIGNED
);
987 for (i
= 0; i
< nconds
; i
++)
989 o_num(0, 4 | BT_SIGNED
);
993 ts_push_bt(4 | BT_SIGNED
);
996 static void reador(void)
1003 if (tok_see() != TOK2("||"))
1006 conds
[nconds
++] = o_jnz(0);
1008 while (!tok_jmp(TOK2("||"))) {
1010 conds
[nconds
++] = o_jnz(0);
1013 o_num(0, 4 | BT_SIGNED
);
1016 for (i
= 0; i
< nconds
; i
++)
1017 o_filljmp(conds
[i
]);
1018 o_num(1, 4 | BT_SIGNED
);
1022 ts_push_bt(4 | BT_SIGNED
);
1025 static int readcexpr_const(void)
1052 static void readcexpr(void)
1061 if (readcexpr_const()) {
1078 static void opassign(void (*bop
)(void (*op
)(void)), void (*op
)(void))
1080 unsigned bt
= TYPE_BT(&ts
[nts
- 1]);
1088 static void doassign(void)
1092 if (!t
.ptr
&& t
.flags
& T_STRUCT
)
1093 o_memcpy(type_totsz(&t
));
1095 o_assign(TYPE_BT(&ts
[nts
- 1]));
1098 static void readexpr(void)
1101 if (!tok_jmp('=')) {
1106 if (!tok_jmp(TOK2("+="))) {
1107 opassign(ts_binop_add
, o_add
);
1110 if (!tok_jmp(TOK2("-="))) {
1111 opassign(ts_binop_add
, o_sub
);
1114 if (!tok_jmp(TOK2("*="))) {
1115 opassign(ts_binop
, o_mul
);
1118 if (!tok_jmp(TOK2("/="))) {
1119 opassign(ts_binop
, o_div
);
1122 if (!tok_jmp(TOK2("%="))) {
1123 opassign(ts_binop
, o_mod
);
1126 if (!tok_jmp(TOK3("<<="))) {
1127 opassign(ts_binop
, o_shl
);
1130 if (!tok_jmp(TOK3(">>="))) {
1131 opassign(ts_binop
, o_shr
);
1134 if (!tok_jmp(TOK3("&="))) {
1135 opassign(ts_binop
, o_and
);
1138 if (!tok_jmp(TOK3("|="))) {
1139 opassign(ts_binop
, o_or
);
1142 if (!tok_jmp(TOK3("^="))) {
1143 opassign(ts_binop
, o_xor
);
1148 static void readestmt(void)
1154 } while (!tok_jmp(','));
1157 static void o_localoff(long addr
, int off
, unsigned bt
)
1168 static struct type
*innertype(struct type
*t
)
1170 if (t
->flags
& T_ARRAY
&& !t
->ptr
)
1171 return innertype(&arrays
[t
->id
].type
);
1175 static void initexpr(struct type
*t
, int off
, void *obj
,
1176 void (*set
)(void *obj
, int off
, struct type
*t
))
1182 if (!t
->ptr
&& t
->flags
& T_STRUCT
) {
1183 struct structinfo
*si
= &structs
[t
->id
];
1185 for (i
= 0; i
< si
->nfields
; i
++) {
1186 struct name
*field
= &si
->fields
[i
];
1187 if (!tok_jmp('.')) {
1188 tok_expect(TOK_NAME
);
1189 field
= struct_field(t
->id
, tok_id());
1192 initexpr(&field
->type
, off
+ field
->addr
, obj
, set
);
1193 if (tok_jmp(',') || tok_see() == '}')
1196 } else if (t
->flags
& T_ARRAY
) {
1197 struct type
*t_de
= &arrays
[t
->id
].type
;
1199 for (i
= 0; ; i
++) {
1201 struct type
*it
= t_de
;
1202 if (!tok_jmp('[')) {
1209 if (tok_see() != '{')
1210 it
= innertype(t_de
);
1211 initexpr(it
, off
+ type_totsz(it
) * idx
, obj
, set
);
1212 if (tok_jmp(',') || tok_see() == '}')
1219 static void jumpbrace(void)
1222 while (tok_see() != '}' || depth
--)
1223 if (tok_get() == '{')
1228 static int initsize(void)
1230 long addr
= tok_addr();
1232 if (!tok_jmp(TOK_STR
)) {
1239 while (tok_jmp('}')) {
1241 if (!tok_jmp('[')) {
1250 while (tok_see() != '}' && tok_see() != ',')
1251 if (tok_get() == '{')
1260 #define F_GLOBAL(flags) (!((flags) & F_STATIC))
1262 static void globalinit(void *obj
, int off
, struct type
*t
)
1264 long addr
= *(long *) obj
;
1265 if (t
->flags
& T_ARRAY
&& tok_see() == TOK_STR
) {
1266 struct type
*t_de
= &arrays
[t
->id
].type
;
1267 if (!t_de
->ptr
&& !t_de
->flags
&& TYPE_SZ(t_de
) == 1) {
1270 tok_expect(TOK_STR
);
1272 out_datcpy(addr
, off
, buf
, len
);
1277 o_datset(addr
, off
, TYPE_BT(t
));
1281 static void globaldef(void *data
, struct name
*name
, unsigned flags
)
1283 struct type
*t
= &name
->type
;
1284 char *varname
= flags
& F_STATIC
? NULL
: name
->name
;
1286 if (t
->flags
& T_ARRAY
&& !t
->ptr
&& !arrays
[t
->id
].n
)
1287 arrays
[t
->id
].n
= initsize();
1289 if (flags
& F_EXTERN
|| t
->flags
& T_FUNC
&& !t
->ptr
)
1291 else if (flags
& F_INIT
)
1292 name
->addr
= out_mkdat(varname
, NULL
, sz
, F_GLOBAL(flags
));
1294 name
->addr
= out_mkvar(varname
, sz
, F_GLOBAL(flags
));
1297 initexpr(t
, 0, &name
->addr
, globalinit
);
1300 static void localinit(void *obj
, int off
, struct type
*t
)
1302 long addr
= *(long *) obj
;
1303 if (t
->flags
& T_ARRAY
&& tok_see() == TOK_STR
) {
1304 struct type
*t_de
= &arrays
[t
->id
].type
;
1305 if (!t_de
->ptr
&& !t_de
->flags
&& TYPE_SZ(t_de
) == 1) {
1308 tok_expect(TOK_STR
);
1310 o_localoff(addr
, off
, TYPE_BT(t
));
1311 o_symaddr(out_mkdat(NULL
, buf
, len
, 0), TYPE_BT(t
));
1317 o_localoff(addr
, off
, TYPE_BT(t
));
1325 static void localdef(void *data
, struct name
*name
, unsigned flags
)
1327 struct type
*t
= &name
->type
;
1328 if (flags
& (F_STATIC
| F_EXTERN
)) {
1329 globaldef(data
, name
, flags
);
1332 if (t
->flags
& T_ARRAY
&& !t
->ptr
&& !arrays
[t
->id
].n
)
1333 arrays
[t
->id
].n
= initsize();
1334 name
->addr
= o_mklocal(type_totsz(&name
->type
));
1336 if (flags
& F_INIT
) {
1337 if (t
->flags
& (T_ARRAY
| T_STRUCT
) && !t
->ptr
) {
1338 o_local(name
->addr
, TYPE_BT(t
));
1339 o_memset(0, type_totsz(t
));
1342 initexpr(t
, 0, &name
->addr
, localinit
);
1346 static void funcdef(char *name
, struct type
*type
, struct name
*args
,
1347 int nargs
, unsigned flags
)
1351 strcpy(global
.name
, name
);
1352 memcpy(&global
.type
, type
, sizeof(*type
));
1353 global
.addr
= o_func_beg(name
, F_GLOBAL(flags
));
1355 global_add(&global
);
1356 ret_bt
= TYPE_BT(&funcs
[type
->id
].ret
);
1357 for (i
= 0; i
< nargs
; i
++) {
1358 args
[i
].addr
= o_arg(i
, type_totsz(&args
[i
].type
));
1359 local_add(&args
[i
]);
1363 static int readargs(struct name
*args
)
1367 while (tok_see() != ')') {
1368 if (!tok_jmp(TOK3("...")))
1370 readname(&args
[nargs
].type
, args
[nargs
].name
, NULL
, 0);
1371 array2ptr(&args
[nargs
].type
);
1377 if (nargs
== 1 && !TYPE_BT(&args
[0].type
))
1382 static int readname(struct type
*main
, char *name
,
1383 struct type
*base
, unsigned flags
)
1385 struct type tpool
[3];
1387 struct type
*type
= &tpool
[npool
++];
1388 struct type
*func
= NULL
;
1389 struct type
*ret
= NULL
;
1393 memset(tpool
, 0, sizeof(tpool
));
1397 if (basetype(type
, &flags
))
1400 memcpy(type
, base
, sizeof(*base
));
1403 if (!tok_jmp('(')) {
1405 type
= &tpool
[npool
++];
1409 if (!tok_jmp(TOK_NAME
) && name
)
1410 strcpy(name
, tok_id());
1411 while (!tok_jmp('[')) {
1417 err("const expr expected\n");
1422 for (i
= nar
- 1; i
>= 0; i
--) {
1423 type
->id
= array_add(type
, arsz
[i
]);
1424 if (type
->flags
& T_FUNC
)
1425 func
= &arrays
[type
->id
].type
;
1426 type
->flags
= T_ARRAY
;
1432 if (tok_see() == '(') {
1433 struct name args
[MAXARGS
];
1434 int nargs
= readargs(args
);
1438 type
= &tpool
[npool
++];
1441 func
->flags
= T_FUNC
;
1443 func
->id
= func_create(ret
, args
, nargs
);
1444 if (fdef
&& tok_see() == '{') {
1445 funcdef(name
, func
, args
, nargs
, flags
);
1449 memcpy(main
, type
, sizeof(*type
));
1453 static int readdefs(void (*def
)(void *data
, struct name
*name
, unsigned flags
),
1458 if (basetype(&base
, &flags
))
1460 while (tok_see() != ';' && tok_see() != '{') {
1463 if (readname(&name
.type
, name
.name
, &base
, flags
))
1467 def(data
, &name
, flags
);
1473 static void typedefdef(void *data
, struct name
*name
, unsigned flags
)
1475 typedef_add(name
->name
, &name
->type
);
1478 static void readstmt(void);
1480 #define MAXCASES (1 << 7)
1482 static void readswitch(void)
1484 int break_beg
= nbreaks
;
1485 long val_addr
= o_mklocal(8);
1486 long matched
[MAXCASES
];
1495 o_local(val_addr
, TYPE_BT(&t
));
1497 o_assign(TYPE_BT(&t
));
1501 while (tok_jmp('}')) {
1503 while (tok_see() == TOK_CASE
|| tok_see() == TOK_DEFAULT
) {
1505 matched
[nmatched
++] = o_jmp(0);
1508 if (!tok_jmp(TOK_CASE
)) {
1512 o_local(val_addr
, TYPE_BT(&t
));
1521 if (!tok_jmp(TOK_DEFAULT
)) {
1526 for (i
= 0; i
< nmatched
; i
++)
1527 o_filljmp(matched
[i
]);
1531 o_rmlocal(val_addr
, 8);
1534 break_fill(o_mklabel(), break_beg
);
1537 #define MAXGOTO (1 << 10)
1539 static struct gotoinfo
{
1545 static struct labelinfo
{
1551 static void goto_add(char *name
)
1553 strcpy(gotos
[ngotos
].name
, name
);
1554 gotos
[ngotos
++].addr
= o_jmp(0);
1557 static void label_add(char *name
)
1559 strcpy(labels
[nlabels
].name
, name
);
1560 labels
[nlabels
++].addr
= o_mklabel();
1563 static void goto_fill(void)
1566 for (i
= 0; i
< ngotos
; i
++)
1567 for (j
= 0; j
< nlabels
; j
++)
1568 if (!strcmp(gotos
[i
].name
, labels
[j
].name
)) {
1569 o_filljmp2(gotos
[i
].addr
, labels
[j
].addr
);
1574 static void readstmt(void)
1578 if (!tok_jmp('{')) {
1579 int _nlocals
= nlocals
;
1580 int _nglobals
= nglobals
;
1581 int _nenums
= nenums
;
1582 int _ntypedefs
= ntypedefs
;
1583 int _nstructs
= nstructs
;
1584 int _nfuncs
= nfuncs
;
1585 int _narrays
= narrays
;
1586 while (tok_jmp('}'))
1590 ntypedefs
= _ntypedefs
;
1591 nstructs
= _nstructs
;
1594 nglobals
= _nglobals
;
1597 if (!readdefs(localdef
, NULL
)) {
1601 if (!tok_jmp(TOK_TYPEDEF
)) {
1602 readdefs(typedefdef
, NULL
);
1606 if (!tok_jmp(TOK_IF
)) {
1613 if (!tok_jmp(TOK_ELSE
)) {
1623 if (!tok_jmp(TOK_WHILE
)) {
1625 int break_beg
= nbreaks
;
1626 int continue_beg
= ncontinues
;
1635 break_fill(o_mklabel(), break_beg
);
1636 continue_fill(l1
, continue_beg
);
1639 if (!tok_jmp(TOK_DO
)) {
1641 int break_beg
= nbreaks
;
1642 int continue_beg
= ncontinues
;
1645 tok_expect(TOK_WHILE
);
1651 break_fill(o_mklabel(), break_beg
);
1652 continue_fill(l2
, continue_beg
);
1655 if (!tok_jmp(TOK_FOR
)) {
1656 long l_check
, l_jump
, j_fail
, j_pass
;
1657 int break_beg
= nbreaks
;
1658 int continue_beg
= ncontinues
;
1661 if (tok_see() != ';')
1664 l_check
= o_mklabel();
1665 if (tok_see() != ';') {
1672 l_jump
= o_mklabel();
1673 if (tok_see() != ')')
1682 break_fill(o_mklabel(), break_beg
);
1683 continue_fill(l_jump
, continue_beg
);
1686 if (!tok_jmp(TOK_SWITCH
)) {
1690 if (!tok_jmp(TOK_RETURN
)) {
1691 int ret
= tok_see() != ';';
1698 if (!tok_jmp(TOK_BREAK
)) {
1700 breaks
[nbreaks
++] = o_jmp(0);
1703 if (!tok_jmp(TOK_CONTINUE
)) {
1705 continues
[ncontinues
++] = o_jmp(0);
1708 if (!tok_jmp(TOK_GOTO
)) {
1709 tok_expect(TOK_NAME
);
1716 if (!tok_jmp(':')) {
1717 label_add(tok_id());
1723 static void readdecl(void)
1725 if (!tok_jmp(TOK_TYPEDEF
)) {
1726 readdefs(typedefdef
, NULL
);
1730 readdefs(globaldef
, NULL
);
1731 if (tok_see() == '{') {
1743 static void parse(void)
1745 while (tok_see() != TOK_EOF
)
1749 int main(int argc
, char *argv
[])
1754 while (i
< argc
&& argv
[i
][0] == '-') {
1755 if (argv
[i
][1] == 'I')
1756 cpp_addpath(argv
[i
][2] ? argv
[i
] + 2 : argv
[++i
]);
1757 if (argv
[i
][1] == 'D') {
1758 char *name
= argv
[i
] + 2;
1760 char *eq
= strchr(name
, '=');
1765 cpp_define(name
, def
);
1770 die("neatcc: no file given\n");
1771 if (cpp_init(argv
[i
]))
1772 die("neatcc: cannot open input file\n");
1774 strcpy(obj
, argv
[i
]);
1775 obj
[strlen(obj
) - 1] = 'o';
1776 ofd
= open(obj
, O_WRONLY
| O_TRUNC
| O_CREAT
, 0600);