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 o_num(type_szde(&t1
), 4);
333 ts_push_bt(4 | BT_SIGNED
);
339 #define ALIGN(x, a) (((x) + (a) - 1) & ~((a) - 1))
340 #define MIN(a, b) ((a) < (b) ? (a) : (b))
342 static void structdef(void *data
, struct name
*name
, unsigned flags
)
344 struct structinfo
*si
= data
;
347 if (si
->size
< type_totsz(&name
->type
))
348 si
->size
= type_totsz(&name
->type
);
350 struct type
*t
= &name
->type
;
351 int alignment
= MIN(8, type_totsz(t
));
352 if (t
->flags
& T_ARRAY
&& !t
->ptr
)
353 alignment
= MIN(8, type_totsz(&arrays
[t
->id
].type
));
354 si
->size
= ALIGN(si
->size
, alignment
);
355 name
->addr
= si
->size
;
356 si
->size
+= type_totsz(&name
->type
);
358 memcpy(&si
->fields
[si
->nfields
++], name
, sizeof(*name
));
361 static int readdefs(void (*def
)(void *, struct name
*, unsigned f
), void *data
);
363 static int struct_create(char *name
, int isunion
)
365 int id
= struct_find(name
, isunion
);
366 struct structinfo
*si
= &structs
[id
];
368 while (tok_jmp('}')) {
369 readdefs(structdef
, si
);
375 static void readexpr(void);
377 static void enum_create(void)
381 while (tok_jmp('}')) {
383 tok_expect(TOK_NAME
);
384 strcpy(name
, tok_id());
385 if (tok_see() == '=') {
390 err("const expr expected!\n");
397 static int basetype(struct type
*type
, unsigned *flags
)
404 char name
[NAMELEN
] = "";
441 isunion
= tok_get() == TOK_UNION
;
442 if (!tok_jmp(TOK_NAME
))
443 strcpy(name
, tok_id());
444 if (tok_see() == '{')
445 type
->id
= struct_create(name
, isunion
);
447 type
->id
= struct_find(name
, isunion
);
448 type
->flags
|= T_STRUCT
;
454 if (tok_see() == '{')
456 type
->bt
= 4 | BT_SIGNED
;
459 if (tok_see() == TOK_NAME
) {
460 int id
= typedef_find(tok_id());
463 memcpy(type
, &typedefs
[id
].type
,
476 type
->bt
= size
| (sign
? BT_SIGNED
: 0);
480 static int readname(struct type
*main
, char *name
,
481 struct type
*base
, unsigned flags
);
483 static int readtype(struct type
*type
)
485 return readname(type
, NULL
, NULL
, 0);
488 static void readptrs(struct type
*type
)
490 while (!tok_jmp('*')) {
497 /* used to differenciate labels from case and cond exprs */
501 static void readpre(void);
503 static void readprimary(void)
506 if (!tok_jmp(TOK_NUM
)) {
508 int bt
= tok_num(&n
);
513 if (!tok_jmp(TOK_STR
)) {
517 t
.bt
= 1 | BT_SIGNED
;
522 o_symaddr(out_mkdat(NULL
, buf
, len
, 0), TYPE_BT(&t
));
526 if (!tok_jmp(TOK_NAME
)) {
527 struct name unkn
= {0};
528 char *name
= unkn
.name
;
530 strcpy(name
, tok_id());
531 /* don't search for labels here */
532 if (!ncexpr
&& !caseexpr
&& tok_see() == ':')
534 for (i
= nlocals
- 1; i
>= 0; --i
) {
535 struct type
*t
= &locals
[i
].type
;
536 if (!strcmp(locals
[i
].name
, name
)) {
537 o_local(locals
[i
].addr
, TYPE_BT(t
));
542 if ((n
= global_find(name
)) != -1) {
543 struct name
*g
= &globals
[n
];
544 struct type
*t
= &g
->type
;
547 if (t
->flags
& T_FUNC
&& !t
->ptr
)
548 g
->addr
= out_mkundef(name
, 0);
550 g
->addr
= out_mkundef(name
, type_totsz(t
));
552 o_symaddr(g
->addr
, TYPE_BT(t
));
556 if (!enum_find(&n
, name
)) {
557 ts_push_bt(4 | BT_SIGNED
);
558 o_num(n
, 4 | BT_SIGNED
);
561 if (tok_see() != '(')
562 err("unknown symbol\n");
563 unkn
.addr
= out_mkundef(unkn
.name
, 0);
566 o_symaddr(unkn
.addr
, 8);
577 if (!t
.ptr
|| !o
.ptr
)
587 void arrayderef(struct type
*t
)
589 int sz
= type_totsz(t
);
598 static void inc_post(void (*op
)(void))
600 unsigned bt
= TYPE_BT(&ts
[nts
- 1]);
614 static void readfield(void)
618 tok_expect(TOK_NAME
);
621 field
= struct_field(t
.id
, tok_id());
623 o_num(field
->addr
, 4);
626 o_deref(TYPE_BT(&field
->type
));
627 ts_push(&field
->type
);
630 #define MAXFUNCS (1 << 10)
632 static struct funcinfo
{
633 struct type args
[MAXFIELDS
];
638 static unsigned ret_bt
;
640 static int func_create(struct type
*ret
, struct name
*args
, int nargs
)
642 struct funcinfo
*fi
= &funcs
[nfuncs
++];
644 if (nfuncs
>= MAXFUNCS
)
645 err("nomem: MAXFUNCS reached!\n");
646 memcpy(&fi
->ret
, ret
, sizeof(*ret
));
647 for (i
= 0; i
< nargs
; i
++)
648 memcpy(&fi
->args
[i
], &args
[i
].type
, sizeof(*ret
));
653 static void readcall(void)
656 unsigned bt
[MAXARGS
];
661 if (t
.flags
& T_FUNC
&& t
.ptr
> 0)
663 fi
= t
.flags
& T_FUNC
? &funcs
[t
.id
] : NULL
;
664 if (tok_see() != ')') {
667 bt
[argc
++] = TYPE_BT(&t
);
669 while (!tok_jmp(',')) {
672 bt
[argc
++] = TYPE_BT(&t
);
676 for (i
= 0; i
< fi
->nargs
; i
++)
677 bt
[i
] = TYPE_BT(&fi
->args
[i
]);
678 o_call(argc
, bt
, fi
? TYPE_BT(&fi
->ret
) : 4 | BT_SIGNED
);
682 ts_push_bt(4 | BT_SIGNED
);
685 static void readpost(void)
705 if (!tok_jmp(TOK2("++"))) {
709 if (!tok_jmp(TOK2("--"))) {
718 if (!tok_jmp(TOK2("->"))) {
726 static void inc_pre(void (*op
)(void))
728 unsigned bt
= TYPE_BT(&ts
[nts
- 1]);
739 static void readpre(void)
745 if (!(type
.flags
& T_FUNC
) && !type
.ptr
)
756 if (!(t
.flags
& T_FUNC
) || t
.ptr
> 0) {
758 o_deref(TYPE_BT(&t
));
768 ts_push_bt(4 | BT_SIGNED
);
781 if (!tok_jmp(TOK2("++"))) {
785 if (!tok_jmp(TOK2("--"))) {
789 if (!tok_jmp(TOK_SIZEOF
)) {
791 int op
= !tok_jmp('(');
793 int nogen
= !o_nogen();
801 o_num(type_totsz(&t
), 4);
809 static void readmul(void)
832 static void readadd(void)
850 static void shift(void (*op
)(void))
857 ts_push_bt(TYPE_BT(&t
));
860 static void readshift(void)
864 if (!tok_jmp(TOK2("<<"))) {
868 if (!tok_jmp(TOK2(">>"))) {
876 static void cmp(void (*op
)(void))
882 ts_push_bt(4 | BT_SIGNED
);
885 static void readcmp(void)
897 if (!tok_jmp(TOK2("<="))) {
901 if (!tok_jmp(TOK2(">="))) {
909 static void eq(void (*op
)(void))
915 ts_push_bt(4 | BT_SIGNED
);
918 static void readeq(void)
922 if (!tok_jmp(TOK2("=="))) {
926 if (!tok_jmp(TOK2("!="))) {
934 static void readbitand(void)
937 while (!tok_jmp('&')) {
943 static void readxor(void)
946 while (!tok_jmp('^')) {
952 static void readbitor(void)
955 while (!tok_jmp('|')) {
961 #define MAXCOND (1 << 7)
963 static void readand(void)
970 if (tok_see() != TOK2("&&"))
973 conds
[nconds
++] = o_jz(0);
975 while (!tok_jmp(TOK2("&&"))) {
977 conds
[nconds
++] = o_jz(0);
980 o_num(1, 4 | BT_SIGNED
);
983 for (i
= 0; i
< nconds
; i
++)
985 o_num(0, 4 | BT_SIGNED
);
989 ts_push_bt(4 | BT_SIGNED
);
992 static void reador(void)
999 if (tok_see() != TOK2("||"))
1002 conds
[nconds
++] = o_jnz(0);
1004 while (!tok_jmp(TOK2("||"))) {
1006 conds
[nconds
++] = o_jnz(0);
1009 o_num(0, 4 | BT_SIGNED
);
1012 for (i
= 0; i
< nconds
; i
++)
1013 o_filljmp(conds
[i
]);
1014 o_num(1, 4 | BT_SIGNED
);
1018 ts_push_bt(4 | BT_SIGNED
);
1021 static int readcexpr_const(void)
1048 static void readcexpr(void)
1057 if (readcexpr_const()) {
1074 static void opassign(void (*bop
)(void (*op
)(void)), void (*op
)(void))
1076 unsigned bt
= TYPE_BT(&ts
[nts
- 1]);
1084 static void doassign(void)
1088 if (!t
.ptr
&& t
.flags
& T_STRUCT
)
1089 o_memcpy(type_totsz(&t
));
1091 o_assign(TYPE_BT(&ts
[nts
- 1]));
1094 static void readexpr(void)
1097 if (!tok_jmp('=')) {
1102 if (!tok_jmp(TOK2("+="))) {
1103 opassign(ts_binop_add
, o_add
);
1106 if (!tok_jmp(TOK2("-="))) {
1107 opassign(ts_binop_add
, o_sub
);
1110 if (!tok_jmp(TOK2("*="))) {
1111 opassign(ts_binop
, o_mul
);
1114 if (!tok_jmp(TOK2("/="))) {
1115 opassign(ts_binop
, o_div
);
1118 if (!tok_jmp(TOK2("%="))) {
1119 opassign(ts_binop
, o_mod
);
1122 if (!tok_jmp(TOK3("<<="))) {
1123 opassign(ts_binop
, o_shl
);
1126 if (!tok_jmp(TOK3(">>="))) {
1127 opassign(ts_binop
, o_shr
);
1130 if (!tok_jmp(TOK3("&="))) {
1131 opassign(ts_binop
, o_and
);
1134 if (!tok_jmp(TOK3("|="))) {
1135 opassign(ts_binop
, o_or
);
1138 if (!tok_jmp(TOK3("^="))) {
1139 opassign(ts_binop
, o_xor
);
1144 static void readestmt(void)
1150 } while (!tok_jmp(','));
1153 static void o_localoff(long addr
, int off
, unsigned bt
)
1164 static struct type
*innertype(struct type
*t
)
1166 if (t
->flags
& T_ARRAY
&& !t
->ptr
)
1167 return innertype(&arrays
[t
->id
].type
);
1171 static void initexpr(struct type
*t
, int off
, void *obj
,
1172 void (*set
)(void *obj
, int off
, struct type
*t
))
1178 if (!t
->ptr
&& t
->flags
& T_STRUCT
) {
1179 struct structinfo
*si
= &structs
[t
->id
];
1181 for (i
= 0; i
< si
->nfields
; i
++) {
1182 struct name
*field
= &si
->fields
[i
];
1183 if (!tok_jmp('.')) {
1184 tok_expect(TOK_NAME
);
1185 field
= struct_field(t
->id
, tok_id());
1188 initexpr(&field
->type
, off
+ field
->addr
, obj
, set
);
1189 if (tok_jmp(',') || tok_see() == '}')
1192 } else if (t
->flags
& T_ARRAY
) {
1193 struct type
*t_de
= &arrays
[t
->id
].type
;
1195 for (i
= 0; ; i
++) {
1197 struct type
*it
= t_de
;
1198 if (!tok_jmp('[')) {
1205 if (tok_see() != '{')
1206 it
= innertype(t_de
);
1207 initexpr(it
, off
+ type_totsz(it
) * idx
, obj
, set
);
1208 if (tok_jmp(',') || tok_see() == '}')
1215 static void jumpbrace(void)
1218 while (tok_see() != '}' || depth
--)
1219 if (tok_get() == '{')
1224 static int initsize(void)
1226 long addr
= tok_addr();
1228 if (!tok_jmp(TOK_STR
)) {
1235 while (tok_jmp('}')) {
1237 if (!tok_jmp('[')) {
1246 while (tok_see() != '}' && tok_see() != ',')
1247 if (tok_get() == '{')
1256 #define F_GLOBAL(flags) (!((flags) & F_STATIC))
1258 static void globalinit(void *obj
, int off
, struct type
*t
)
1260 long addr
= *(long *) obj
;
1261 if (t
->flags
& T_ARRAY
&& tok_see() == TOK_STR
) {
1262 struct type
*t_de
= &arrays
[t
->id
].type
;
1263 if (!t_de
->ptr
&& !t_de
->flags
&& TYPE_SZ(t_de
) == 1) {
1266 tok_expect(TOK_STR
);
1268 out_datcpy(addr
, off
, buf
, len
);
1273 o_datset(addr
, off
, TYPE_BT(t
));
1277 static void globaldef(void *data
, struct name
*name
, unsigned flags
)
1279 struct type
*t
= &name
->type
;
1280 char *varname
= flags
& F_STATIC
? NULL
: name
->name
;
1282 if (t
->flags
& T_ARRAY
&& !t
->ptr
&& !arrays
[t
->id
].n
)
1283 arrays
[t
->id
].n
= initsize();
1285 if (flags
& F_EXTERN
|| t
->flags
& T_FUNC
&& !t
->ptr
)
1287 else if (flags
& F_INIT
)
1288 name
->addr
= out_mkdat(varname
, NULL
, sz
, F_GLOBAL(flags
));
1290 name
->addr
= out_mkvar(varname
, sz
, F_GLOBAL(flags
));
1293 initexpr(t
, 0, &name
->addr
, globalinit
);
1296 static void localinit(void *obj
, int off
, struct type
*t
)
1298 long addr
= *(long *) obj
;
1299 if (t
->flags
& T_ARRAY
&& tok_see() == TOK_STR
) {
1300 struct type
*t_de
= &arrays
[t
->id
].type
;
1301 if (!t_de
->ptr
&& !t_de
->flags
&& TYPE_SZ(t_de
) == 1) {
1304 tok_expect(TOK_STR
);
1306 o_localoff(addr
, off
, TYPE_BT(t
));
1307 o_symaddr(out_mkdat(NULL
, buf
, len
, 0), TYPE_BT(t
));
1313 o_localoff(addr
, off
, TYPE_BT(t
));
1321 static void localdef(void *data
, struct name
*name
, unsigned flags
)
1323 struct type
*t
= &name
->type
;
1324 if (flags
& (F_STATIC
| F_EXTERN
)) {
1325 globaldef(data
, name
, flags
);
1328 if (t
->flags
& T_ARRAY
&& !t
->ptr
&& !arrays
[t
->id
].n
)
1329 arrays
[t
->id
].n
= initsize();
1330 name
->addr
= o_mklocal(type_totsz(&name
->type
));
1332 if (flags
& F_INIT
) {
1333 if (t
->flags
& (T_ARRAY
| T_STRUCT
) && !t
->ptr
) {
1334 o_local(name
->addr
, TYPE_BT(t
));
1335 o_memset(0, type_totsz(t
));
1338 initexpr(t
, 0, &name
->addr
, localinit
);
1342 static void funcdef(char *name
, struct type
*type
, struct name
*args
,
1343 int nargs
, unsigned flags
)
1347 strcpy(global
.name
, name
);
1348 memcpy(&global
.type
, type
, sizeof(*type
));
1349 global
.addr
= o_func_beg(name
, F_GLOBAL(flags
));
1351 global_add(&global
);
1352 ret_bt
= TYPE_BT(&funcs
[type
->id
].ret
);
1353 for (i
= 0; i
< nargs
; i
++) {
1354 args
[i
].addr
= o_arg(i
, type_totsz(&args
[i
].type
));
1355 local_add(&args
[i
]);
1359 static int readargs(struct name
*args
)
1363 while (tok_see() != ')') {
1364 if (!tok_jmp(TOK3("...")))
1366 readname(&args
[nargs
].type
, args
[nargs
].name
, NULL
, 0);
1367 array2ptr(&args
[nargs
].type
);
1373 if (nargs
== 1 && !TYPE_BT(&args
[0].type
))
1378 static int readname(struct type
*main
, char *name
,
1379 struct type
*base
, unsigned flags
)
1381 struct type tpool
[3];
1383 struct type
*type
= &tpool
[npool
++];
1384 struct type
*func
= NULL
;
1385 struct type
*ret
= NULL
;
1389 memset(tpool
, 0, sizeof(tpool
));
1393 if (basetype(type
, &flags
))
1396 memcpy(type
, base
, sizeof(*base
));
1399 if (!tok_jmp('(')) {
1401 type
= &tpool
[npool
++];
1405 if (!tok_jmp(TOK_NAME
) && name
)
1406 strcpy(name
, tok_id());
1407 while (!tok_jmp('[')) {
1413 err("const expr expected\n");
1418 for (i
= nar
- 1; i
>= 0; i
--) {
1419 type
->id
= array_add(type
, arsz
[i
]);
1420 if (type
->flags
& T_FUNC
)
1421 func
= &arrays
[type
->id
].type
;
1422 type
->flags
= T_ARRAY
;
1428 if (tok_see() == '(') {
1429 struct name args
[MAXARGS
];
1430 int nargs
= readargs(args
);
1434 type
= &tpool
[npool
++];
1437 func
->flags
= T_FUNC
;
1439 func
->id
= func_create(ret
, args
, nargs
);
1440 if (fdef
&& tok_see() == '{') {
1441 funcdef(name
, func
, args
, nargs
, flags
);
1445 memcpy(main
, type
, sizeof(*type
));
1449 static int readdefs(void (*def
)(void *data
, struct name
*name
, unsigned flags
),
1454 if (basetype(&base
, &flags
))
1456 while (tok_see() != ';' && tok_see() != '{') {
1459 if (readname(&name
.type
, name
.name
, &base
, flags
))
1463 def(data
, &name
, flags
);
1469 static void typedefdef(void *data
, struct name
*name
, unsigned flags
)
1471 typedef_add(name
->name
, &name
->type
);
1474 static void readstmt(void);
1476 #define MAXCASES (1 << 7)
1478 static void readswitch(void)
1480 int break_beg
= nbreaks
;
1481 long val_addr
= o_mklocal(8);
1482 long matched
[MAXCASES
];
1491 o_local(val_addr
, TYPE_BT(&t
));
1493 o_assign(TYPE_BT(&t
));
1497 while (tok_jmp('}')) {
1499 while (tok_see() == TOK_CASE
|| tok_see() == TOK_DEFAULT
) {
1501 matched
[nmatched
++] = o_jmp(0);
1504 if (!tok_jmp(TOK_CASE
)) {
1508 o_local(val_addr
, TYPE_BT(&t
));
1517 if (!tok_jmp(TOK_DEFAULT
)) {
1522 for (i
= 0; i
< nmatched
; i
++)
1523 o_filljmp(matched
[i
]);
1527 o_rmlocal(val_addr
, 8);
1530 break_fill(o_mklabel(), break_beg
);
1533 #define MAXGOTO (1 << 10)
1535 static struct gotoinfo
{
1541 static struct labelinfo
{
1547 static void goto_add(char *name
)
1549 strcpy(gotos
[ngotos
].name
, name
);
1550 gotos
[ngotos
++].addr
= o_jmp(0);
1553 static void label_add(char *name
)
1555 strcpy(labels
[nlabels
].name
, name
);
1556 labels
[nlabels
++].addr
= o_mklabel();
1559 static void goto_fill(void)
1562 for (i
= 0; i
< ngotos
; i
++)
1563 for (j
= 0; j
< nlabels
; j
++)
1564 if (!strcmp(gotos
[i
].name
, labels
[j
].name
)) {
1565 o_filljmp2(gotos
[i
].addr
, labels
[j
].addr
);
1570 static void readstmt(void)
1574 if (!tok_jmp('{')) {
1575 int _nlocals
= nlocals
;
1576 int _nglobals
= nglobals
;
1577 int _nenums
= nenums
;
1578 int _ntypedefs
= ntypedefs
;
1579 int _nstructs
= nstructs
;
1580 int _nfuncs
= nfuncs
;
1581 int _narrays
= narrays
;
1582 while (tok_jmp('}'))
1586 ntypedefs
= _ntypedefs
;
1587 nstructs
= _nstructs
;
1590 nglobals
= _nglobals
;
1593 if (!readdefs(localdef
, NULL
)) {
1597 if (!tok_jmp(TOK_TYPEDEF
)) {
1598 readdefs(typedefdef
, NULL
);
1602 if (!tok_jmp(TOK_IF
)) {
1609 if (!tok_jmp(TOK_ELSE
)) {
1619 if (!tok_jmp(TOK_WHILE
)) {
1621 int break_beg
= nbreaks
;
1622 int continue_beg
= ncontinues
;
1631 break_fill(o_mklabel(), break_beg
);
1632 continue_fill(l1
, continue_beg
);
1635 if (!tok_jmp(TOK_DO
)) {
1637 int break_beg
= nbreaks
;
1638 int continue_beg
= ncontinues
;
1641 tok_expect(TOK_WHILE
);
1647 break_fill(o_mklabel(), break_beg
);
1648 continue_fill(l2
, continue_beg
);
1651 if (!tok_jmp(TOK_FOR
)) {
1652 long l_check
, l_jump
, j_fail
, j_pass
;
1653 int break_beg
= nbreaks
;
1654 int continue_beg
= ncontinues
;
1657 if (tok_see() != ';')
1660 l_check
= o_mklabel();
1661 if (tok_see() != ';') {
1668 l_jump
= o_mklabel();
1669 if (tok_see() != ')')
1678 break_fill(o_mklabel(), break_beg
);
1679 continue_fill(l_jump
, continue_beg
);
1682 if (!tok_jmp(TOK_SWITCH
)) {
1686 if (!tok_jmp(TOK_RETURN
)) {
1687 int ret
= tok_see() != ';';
1694 if (!tok_jmp(TOK_BREAK
)) {
1696 breaks
[nbreaks
++] = o_jmp(0);
1699 if (!tok_jmp(TOK_CONTINUE
)) {
1701 continues
[ncontinues
++] = o_jmp(0);
1704 if (!tok_jmp(TOK_GOTO
)) {
1705 tok_expect(TOK_NAME
);
1712 if (!tok_jmp(':')) {
1713 label_add(tok_id());
1719 static void readdecl(void)
1721 if (!tok_jmp(TOK_TYPEDEF
)) {
1722 readdefs(typedefdef
, NULL
);
1726 readdefs(globaldef
, NULL
);
1727 if (tok_see() == '{') {
1739 static void parse(void)
1741 while (tok_see() != TOK_EOF
)
1745 int main(int argc
, char *argv
[])
1750 while (i
< argc
&& argv
[i
][0] == '-') {
1751 if (argv
[i
][1] == 'I')
1752 cpp_addpath(argv
[i
][2] ? argv
[i
] + 2 : argv
[++i
]);
1753 if (argv
[i
][1] == 'D') {
1754 char *name
= argv
[i
] + 2;
1756 char *eq
= strchr(name
, '=');
1761 cpp_define(name
, def
);
1766 die("neatcc: no file given\n");
1767 if (cpp_init(argv
[i
]))
1768 die("neatcc: cannot open input file\n");
1770 strcpy(obj
, argv
[i
]);
1771 obj
[strlen(obj
) - 1] = 'o';
1772 ofd
= open(obj
, O_WRONLY
| O_TRUNC
| O_CREAT
, 0600);