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 int shifts(int n
)
315 static void ts_binop_add(void (*o_sth
)(void))
322 if (!t1
.ptr
&& !t2
.ptr
) {
324 ts_push_bt(bt_op(TYPE_BT(&t1
), TYPE_BT(&t2
)));
327 if (t1
.ptr
&& !t2
.ptr
) {
333 if (!t1
.ptr
&& t2
.ptr
)
334 if (type_szde(&t2
) > 1) {
335 o_num(shifts(type_szde(&t2
)), 1);
339 if (t1
.ptr
&& t2
.ptr
) {
340 o_num(shifts(type_szde(&t1
)), 1);
342 ts_push_bt(4 | BT_SIGNED
);
348 #define ALIGN(x, a) (((x) + (a) - 1) & ~((a) - 1))
349 #define MIN(a, b) ((a) < (b) ? (a) : (b))
351 static void structdef(void *data
, struct name
*name
, unsigned flags
)
353 struct structinfo
*si
= data
;
356 if (si
->size
< type_totsz(&name
->type
))
357 si
->size
= type_totsz(&name
->type
);
359 struct type
*t
= &name
->type
;
360 int alignment
= MIN(8, type_totsz(t
));
361 if (t
->flags
& T_ARRAY
&& !t
->ptr
)
362 alignment
= MIN(8, type_totsz(&arrays
[t
->id
].type
));
363 si
->size
= ALIGN(si
->size
, alignment
);
364 name
->addr
= si
->size
;
365 si
->size
+= type_totsz(&name
->type
);
367 memcpy(&si
->fields
[si
->nfields
++], name
, sizeof(*name
));
370 static int readdefs(void (*def
)(void *, struct name
*, unsigned f
), void *data
);
372 static int struct_create(char *name
, int isunion
)
374 int id
= struct_find(name
, isunion
);
375 struct structinfo
*si
= &structs
[id
];
377 while (tok_jmp('}')) {
378 readdefs(structdef
, si
);
384 static void readexpr(void);
386 static void enum_create(void)
390 while (tok_jmp('}')) {
392 tok_expect(TOK_NAME
);
393 strcpy(name
, tok_id());
394 if (tok_see() == '=') {
399 err("const expr expected!\n");
406 static int basetype(struct type
*type
, unsigned *flags
)
413 char name
[NAMELEN
] = "";
450 isunion
= tok_get() == TOK_UNION
;
451 if (!tok_jmp(TOK_NAME
))
452 strcpy(name
, tok_id());
453 if (tok_see() == '{')
454 type
->id
= struct_create(name
, isunion
);
456 type
->id
= struct_find(name
, isunion
);
457 type
->flags
|= T_STRUCT
;
463 if (tok_see() == '{')
465 type
->bt
= 4 | BT_SIGNED
;
468 if (tok_see() == TOK_NAME
) {
469 int id
= typedef_find(tok_id());
472 memcpy(type
, &typedefs
[id
].type
,
485 type
->bt
= size
| (sign
? BT_SIGNED
: 0);
489 static int readname(struct type
*main
, char *name
,
490 struct type
*base
, unsigned flags
);
492 static int readtype(struct type
*type
)
494 return readname(type
, NULL
, NULL
, 0);
497 static void readptrs(struct type
*type
)
499 while (!tok_jmp('*')) {
508 static void readpre(void);
510 static void readprimary(void)
513 if (!tok_jmp(TOK_NUM
)) {
514 ts_push_bt(4 | BT_SIGNED
);
515 o_num(tok_num(), 4 | BT_SIGNED
);
518 if (!tok_jmp(TOK_STR
)) {
522 t
.bt
= 1 | BT_SIGNED
;
527 o_symaddr(out_mkdat(NULL
, buf
, len
, 0), TYPE_BT(&t
));
531 if (!tok_jmp(TOK_NAME
)) {
533 char *name
= unkn
.name
;
535 strcpy(name
, tok_id());
536 /* don't search for labels here */
537 if (!ncexpr
&& tok_see() == ':')
539 for (i
= nlocals
- 1; i
>= 0; --i
) {
540 struct type
*t
= &locals
[i
].type
;
541 if (!strcmp(locals
[i
].name
, name
)) {
542 o_local(locals
[i
].addr
, TYPE_BT(t
));
547 if ((n
= global_find(name
)) != -1) {
548 struct name
*g
= &globals
[n
];
549 struct type
*t
= &g
->type
;
552 if (t
->flags
& T_FUNC
&& !t
->ptr
)
553 g
->addr
= out_mkundef(name
, 0);
555 g
->addr
= out_mkundef(name
, type_totsz(t
));
557 o_symaddr(g
->addr
, TYPE_BT(t
));
561 if (!enum_find(&n
, name
)) {
562 ts_push_bt(4 | BT_SIGNED
);
563 o_num(n
, 4 | BT_SIGNED
);
566 if (tok_see() != '(')
567 err("unknown symbol\n");
568 unkn
.addr
= out_mkundef(unkn
.name
, 0);
571 o_symaddr(unkn
.addr
, 8);
582 if (!t
.ptr
|| !o
.ptr
)
592 void arrayderef(struct type
*t
)
594 int sz
= type_totsz(t
);
603 static void inc_post(void (*op
)(void))
605 unsigned bt
= TYPE_BT(&ts
[nts
- 1]);
619 static void readfield(void)
623 tok_expect(TOK_NAME
);
626 field
= struct_field(t
.id
, tok_id());
628 o_num(field
->addr
, 4);
631 o_deref(TYPE_BT(&field
->type
));
632 ts_push(&field
->type
);
635 #define MAXFUNCS (1 << 10)
637 static struct funcinfo
{
638 struct type args
[MAXFIELDS
];
643 static unsigned ret_bt
;
645 static int func_create(struct type
*ret
, struct name
*args
, int nargs
)
647 struct funcinfo
*fi
= &funcs
[nfuncs
++];
649 if (nfuncs
>= MAXFUNCS
)
650 err("nomem: MAXFUNCS reached!\n");
651 memcpy(&fi
->ret
, ret
, sizeof(*ret
));
652 for (i
= 0; i
< nargs
; i
++)
653 memcpy(&fi
->args
[i
], &args
[i
].type
, sizeof(*ret
));
658 static void readcall(void)
661 unsigned bt
[MAXARGS
];
665 if (tok_see() != ')') {
668 bt
[argc
++] = TYPE_BT(&t
);
670 while (!tok_jmp(',')) {
673 bt
[argc
++] = TYPE_BT(&t
);
677 if (t
.flags
& T_FUNC
&& t
.ptr
> 0)
679 fi
= t
.flags
& T_FUNC
? &funcs
[t
.id
] : NULL
;
681 for (i
= 0; i
< fi
->nargs
; i
++)
682 bt
[i
] = TYPE_BT(&fi
->args
[i
]);
683 o_call(argc
, bt
, fi
? TYPE_BT(&fi
->ret
) : 4 | BT_SIGNED
);
687 ts_push_bt(4 | BT_SIGNED
);
690 static void readpost(void)
710 if (!tok_jmp(TOK2("++"))) {
714 if (!tok_jmp(TOK2("--"))) {
723 if (!tok_jmp(TOK2("->"))) {
731 static void inc_pre(void (*op
)(void))
733 unsigned bt
= TYPE_BT(&ts
[nts
- 1]);
744 static void readpre(void)
750 if (!(type
.flags
& T_FUNC
) && !type
.ptr
)
761 if (!(t
.flags
& T_FUNC
) || t
.ptr
> 0) {
763 o_deref(TYPE_BT(&t
));
773 ts_push_bt(4 | BT_SIGNED
);
786 if (!tok_jmp(TOK2("++"))) {
790 if (!tok_jmp(TOK2("--"))) {
794 if (!tok_jmp(TOK_SIZEOF
)) {
796 int op
= !tok_jmp('(');
798 int nogen
= !o_nogen();
806 o_num(type_totsz(&t
), 4);
814 static void readmul(void)
837 static void readadd(void)
855 static void shift(void (*op
)(void))
862 ts_push_bt(TYPE_BT(&t
));
865 static void readshift(void)
869 if (!tok_jmp(TOK2("<<"))) {
873 if (!tok_jmp(TOK2(">>"))) {
881 static void cmp(void (*op
)(void))
887 ts_push_bt(4 | BT_SIGNED
);
890 static void readcmp(void)
902 if (!tok_jmp(TOK2("<="))) {
906 if (!tok_jmp(TOK2(">="))) {
914 static void eq(void (*op
)(void))
920 ts_push_bt(4 | BT_SIGNED
);
923 static void readeq(void)
927 if (!tok_jmp(TOK2("=="))) {
931 if (!tok_jmp(TOK2("!="))) {
939 static void readbitand(void)
942 while (!tok_jmp('&')) {
948 static void readxor(void)
951 while (!tok_jmp('^')) {
957 static void readbitor(void)
960 while (!tok_jmp('|')) {
966 #define MAXCOND (1 << 7)
968 static void readand(void)
975 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
);
992 ts_push_bt(4 | BT_SIGNED
);
995 static void reador(void)
1002 if (tok_see() != TOK2("||"))
1004 conds
[nconds
++] = o_jnz(0);
1006 while (!tok_jmp(TOK2("||"))) {
1008 conds
[nconds
++] = o_jnz(0);
1011 o_num(0, 4 | BT_SIGNED
);
1014 for (i
= 0; i
< nconds
; i
++)
1015 o_filljmp(conds
[i
]);
1016 o_num(1, 4 | BT_SIGNED
);
1019 ts_push_bt(4 | BT_SIGNED
);
1022 static void readcexpr(void)
1031 cexpr
= !o_popnum(&c
);
1071 static void opassign(void (*bop
)(void (*op
)(void)), void (*op
)(void))
1073 unsigned bt
= TYPE_BT(&ts
[nts
- 1]);
1081 static void doassign(void)
1085 if (!t
.ptr
&& t
.flags
& T_STRUCT
)
1086 o_memcpy(type_totsz(&t
));
1088 o_assign(TYPE_BT(&ts
[nts
- 1]));
1091 static void readexpr(void)
1094 if (!tok_jmp('=')) {
1099 if (!tok_jmp(TOK2("+="))) {
1100 opassign(ts_binop_add
, o_add
);
1103 if (!tok_jmp(TOK2("-="))) {
1104 opassign(ts_binop_add
, o_sub
);
1107 if (!tok_jmp(TOK2("*="))) {
1108 opassign(ts_binop
, o_mul
);
1111 if (!tok_jmp(TOK2("/="))) {
1112 opassign(ts_binop
, o_div
);
1115 if (!tok_jmp(TOK2("%="))) {
1116 opassign(ts_binop
, o_mod
);
1119 if (!tok_jmp(TOK3("<<="))) {
1120 opassign(ts_binop
, o_shl
);
1123 if (!tok_jmp(TOK3(">>="))) {
1124 opassign(ts_binop
, o_shr
);
1127 if (!tok_jmp(TOK3("&="))) {
1128 opassign(ts_binop
, o_and
);
1131 if (!tok_jmp(TOK3("|="))) {
1132 opassign(ts_binop
, o_or
);
1135 if (!tok_jmp(TOK3("^="))) {
1136 opassign(ts_binop
, o_xor
);
1141 static void readestmt(void)
1147 } while (!tok_jmp(','));
1150 static void o_localoff(long addr
, int off
, unsigned bt
)
1161 static struct type
*innertype(struct type
*t
)
1163 if (t
->flags
& T_ARRAY
&& !t
->ptr
)
1164 return innertype(&arrays
[t
->id
].type
);
1168 static void initexpr(struct type
*t
, int off
, void *obj
,
1169 void (*set
)(void *obj
, int off
, struct type
*t
))
1175 if (!t
->ptr
&& t
->flags
& T_STRUCT
) {
1176 struct structinfo
*si
= &structs
[t
->id
];
1178 for (i
= 0; i
< si
->nfields
; i
++) {
1179 struct name
*field
= &si
->fields
[i
];
1180 if (!tok_jmp('.')) {
1181 tok_expect(TOK_NAME
);
1182 field
= struct_field(t
->id
, tok_id());
1185 initexpr(&field
->type
, off
+ field
->addr
, obj
, set
);
1186 if (tok_jmp(',') || tok_see() == '}')
1189 } else if (t
->flags
& T_ARRAY
) {
1190 struct type
*t_de
= &arrays
[t
->id
].type
;
1192 for (i
= 0; ; i
++) {
1194 struct type
*it
= t_de
;
1195 if (!tok_jmp('[')) {
1202 if (tok_see() != '{')
1203 it
= innertype(t_de
);
1204 initexpr(it
, off
+ type_totsz(it
) * idx
, obj
, set
);
1205 if (tok_jmp(',') || tok_see() == '}')
1212 static void jumpbrace(void)
1215 while (tok_see() != '}' || depth
--)
1216 if (tok_get() == '{')
1221 static int initsize(void)
1223 long addr
= tok_addr();
1225 if (!tok_jmp(TOK_STR
)) {
1232 while (tok_jmp('}')) {
1234 if (!tok_jmp('[')) {
1243 while (tok_see() != '}' && tok_see() != ',')
1244 if (tok_get() == '{')
1253 #define F_GLOBAL(flags) (!((flags) & F_STATIC))
1255 static void globalinit(void *obj
, int off
, struct type
*t
)
1257 long addr
= *(long *) obj
;
1258 if (t
->flags
& T_ARRAY
&& tok_see() == TOK_STR
) {
1259 struct type
*t_de
= &arrays
[t
->id
].type
;
1260 if (!t_de
->ptr
&& !t_de
->flags
&& TYPE_SZ(t_de
) == 1) {
1263 tok_expect(TOK_STR
);
1265 out_datcpy(addr
, off
, buf
, len
);
1270 o_datset(addr
, off
, TYPE_BT(t
));
1274 static void globaldef(void *data
, struct name
*name
, unsigned flags
)
1276 struct type
*t
= &name
->type
;
1277 char *varname
= flags
& F_STATIC
? NULL
: name
->name
;
1279 if (t
->flags
& T_ARRAY
&& !t
->ptr
&& !arrays
[t
->id
].n
)
1280 arrays
[t
->id
].n
= initsize();
1282 if (flags
& F_EXTERN
|| t
->flags
& T_FUNC
&& !t
->ptr
)
1284 else if (flags
& F_INIT
)
1285 name
->addr
= out_mkdat(varname
, NULL
, sz
, F_GLOBAL(flags
));
1287 name
->addr
= out_mkvar(varname
, sz
, F_GLOBAL(flags
));
1290 initexpr(t
, 0, &name
->addr
, globalinit
);
1293 static void localinit(void *obj
, int off
, struct type
*t
)
1295 long addr
= *(long *) obj
;
1296 if (t
->flags
& T_ARRAY
&& tok_see() == TOK_STR
) {
1297 struct type
*t_de
= &arrays
[t
->id
].type
;
1298 if (!t_de
->ptr
&& !t_de
->flags
&& TYPE_SZ(t_de
) == 1) {
1301 tok_expect(TOK_STR
);
1303 o_localoff(addr
, off
, TYPE_BT(t
));
1304 o_symaddr(out_mkdat(NULL
, buf
, len
, 0), TYPE_BT(t
));
1310 o_localoff(addr
, off
, TYPE_BT(t
));
1318 static void localdef(void *data
, struct name
*name
, unsigned flags
)
1320 struct type
*t
= &name
->type
;
1321 if (flags
& (F_STATIC
| F_EXTERN
)) {
1322 globaldef(data
, name
, flags
);
1325 if (t
->flags
& T_ARRAY
&& !t
->ptr
&& !arrays
[t
->id
].n
)
1326 arrays
[t
->id
].n
= initsize();
1327 name
->addr
= o_mklocal(type_totsz(&name
->type
));
1329 if (flags
& F_INIT
) {
1330 if (t
->flags
& (T_ARRAY
| T_STRUCT
) && !t
->ptr
) {
1331 o_local(name
->addr
, TYPE_BT(t
));
1332 o_memset(0, type_totsz(t
));
1335 initexpr(t
, 0, &name
->addr
, localinit
);
1339 static void funcdef(char *name
, struct type
*type
, struct name
*args
,
1340 int nargs
, unsigned flags
)
1344 strcpy(global
.name
, name
);
1345 memcpy(&global
.type
, type
, sizeof(*type
));
1346 global
.addr
= o_func_beg(name
, F_GLOBAL(flags
));
1348 global_add(&global
);
1349 ret_bt
= TYPE_BT(&funcs
[type
->id
].ret
);
1350 for (i
= 0; i
< nargs
; i
++) {
1351 args
[i
].addr
= o_arg(i
, type_totsz(&args
[i
].type
));
1352 local_add(&args
[i
]);
1356 static int readargs(struct name
*args
)
1360 while (tok_see() != ')') {
1361 if (!tok_jmp(TOK3("...")))
1363 readname(&args
[nargs
].type
, args
[nargs
].name
, NULL
, 0);
1364 array2ptr(&args
[nargs
].type
);
1370 if (nargs
== 1 && !TYPE_BT(&args
[0].type
))
1375 static int readname(struct type
*main
, char *name
,
1376 struct type
*base
, unsigned flags
)
1378 struct type tpool
[3];
1380 struct type
*type
= &tpool
[npool
++];
1381 struct type
*func
= NULL
;
1382 struct type
*ret
= NULL
;
1386 memset(tpool
, 0, sizeof(tpool
));
1390 if (basetype(type
, &flags
))
1393 memcpy(type
, base
, sizeof(*base
));
1396 if (!tok_jmp('(')) {
1398 type
= &tpool
[npool
++];
1402 if (!tok_jmp(TOK_NAME
) && name
)
1403 strcpy(name
, tok_id());
1404 while (!tok_jmp('[')) {
1410 err("const expr expected\n");
1415 for (i
= nar
- 1; i
>= 0; i
--) {
1416 type
->id
= array_add(type
, arsz
[i
]);
1417 if (type
->flags
& T_FUNC
)
1418 func
= &arrays
[type
->id
].type
;
1419 type
->flags
= T_ARRAY
;
1425 if (tok_see() == '(') {
1426 struct name args
[MAXARGS
];
1427 int nargs
= readargs(args
);
1431 type
= &tpool
[npool
++];
1434 func
->flags
= T_FUNC
;
1436 func
->id
= func_create(ret
, args
, nargs
);
1437 if (fdef
&& tok_see() == '{') {
1438 funcdef(name
, func
, args
, nargs
, flags
);
1442 memcpy(main
, type
, sizeof(*type
));
1446 static int readdefs(void (*def
)(void *data
, struct name
*name
, unsigned flags
),
1451 if (basetype(&base
, &flags
))
1453 while (tok_see() != ';' && tok_see() != '{') {
1456 if (readname(&name
.type
, name
.name
, &base
, flags
))
1460 def(data
, &name
, flags
);
1466 static void typedefdef(void *data
, struct name
*name
, unsigned flags
)
1468 typedef_add(name
->name
, &name
->type
);
1471 static void readstmt(void);
1473 #define MAXCASES (1 << 7)
1475 static void readswitch(void)
1477 int break_beg
= nbreaks
;
1478 long val_addr
= o_mklocal(8);
1479 long matched
[MAXCASES
];
1488 o_local(val_addr
, TYPE_BT(&t
));
1490 o_assign(TYPE_BT(&t
));
1494 while (tok_jmp('}')) {
1496 while (tok_see() == TOK_CASE
|| tok_see() == TOK_DEFAULT
) {
1498 matched
[nmatched
++] = o_jmp(0);
1501 if (!tok_jmp(TOK_CASE
)) {
1503 o_local(val_addr
, TYPE_BT(&t
));
1512 if (!tok_jmp(TOK_DEFAULT
)) {
1517 for (i
= 0; i
< nmatched
; i
++)
1518 o_filljmp(matched
[i
]);
1522 o_rmlocal(val_addr
, 8);
1525 break_fill(o_mklabel(), break_beg
);
1528 #define MAXGOTO (1 << 10)
1530 static struct gotoinfo
{
1536 static struct labelinfo
{
1542 static void goto_add(char *name
)
1544 strcpy(gotos
[ngotos
].name
, name
);
1545 gotos
[ngotos
++].addr
= o_jmp(0);
1548 static void label_add(char *name
)
1550 strcpy(labels
[nlabels
].name
, name
);
1551 labels
[nlabels
++].addr
= o_mklabel();
1554 static void goto_fill(void)
1557 for (i
= 0; i
< ngotos
; i
++)
1558 for (j
= 0; j
< nlabels
; j
++)
1559 if (!strcmp(gotos
[i
].name
, labels
[j
].name
)) {
1560 o_filljmp2(gotos
[i
].addr
, labels
[j
].addr
);
1565 static void readstmt(void)
1569 if (!tok_jmp('{')) {
1570 int _nlocals
= nlocals
;
1571 int _nglobals
= nglobals
;
1572 int _nenums
= nenums
;
1573 int _ntypedefs
= ntypedefs
;
1574 int _nstructs
= nstructs
;
1575 int _nfuncs
= nfuncs
;
1576 int _narrays
= narrays
;
1577 while (tok_jmp('}'))
1581 ntypedefs
= _ntypedefs
;
1582 nstructs
= _nstructs
;
1585 nglobals
= _nglobals
;
1588 if (!readdefs(localdef
, NULL
)) {
1592 if (!tok_jmp(TOK_TYPEDEF
)) {
1593 readdefs(typedefdef
, NULL
);
1597 if (!tok_jmp(TOK_IF
)) {
1604 if (!tok_jmp(TOK_ELSE
)) {
1614 if (!tok_jmp(TOK_WHILE
)) {
1616 int break_beg
= nbreaks
;
1617 int continue_beg
= ncontinues
;
1626 break_fill(o_mklabel(), break_beg
);
1627 continue_fill(l1
, continue_beg
);
1630 if (!tok_jmp(TOK_DO
)) {
1632 int break_beg
= nbreaks
;
1633 int continue_beg
= ncontinues
;
1636 tok_expect(TOK_WHILE
);
1642 break_fill(o_mklabel(), break_beg
);
1643 continue_fill(l2
, continue_beg
);
1646 if (!tok_jmp(TOK_FOR
)) {
1647 long l_check
, l_jump
, j_fail
, j_pass
;
1648 int break_beg
= nbreaks
;
1649 int continue_beg
= ncontinues
;
1652 if (tok_see() != ';')
1655 l_check
= o_mklabel();
1656 if (tok_see() != ';') {
1663 l_jump
= o_mklabel();
1664 if (tok_see() != ')')
1673 break_fill(o_mklabel(), break_beg
);
1674 continue_fill(l_jump
, continue_beg
);
1677 if (!tok_jmp(TOK_SWITCH
)) {
1681 if (!tok_jmp(TOK_RETURN
)) {
1682 int ret
= tok_see() != ';';
1689 if (!tok_jmp(TOK_BREAK
)) {
1691 breaks
[nbreaks
++] = o_jmp(0);
1694 if (!tok_jmp(TOK_CONTINUE
)) {
1696 continues
[ncontinues
++] = o_jmp(0);
1699 if (!tok_jmp(TOK_GOTO
)) {
1700 tok_expect(TOK_NAME
);
1707 if (!tok_jmp(':')) {
1708 label_add(tok_id());
1714 static void readdecl(void)
1716 if (!tok_jmp(TOK_TYPEDEF
)) {
1717 readdefs(typedefdef
, NULL
);
1721 readdefs(globaldef
, NULL
);
1722 if (tok_see() == '{') {
1734 static void parse(void)
1736 while (tok_see() != TOK_EOF
)
1740 int main(int argc
, char *argv
[])
1745 while (i
< argc
&& argv
[i
][0] == '-') {
1746 if (argv
[i
][1] == 'I')
1747 cpp_addpath(argv
[i
][2] ? argv
[i
] + 2 : argv
[++i
]);
1748 if (argv
[i
][1] == 'D') {
1749 char *name
= argv
[i
] + 2;
1751 char *eq
= strchr(name
, '=');
1756 cpp_define(name
, def
);
1761 die("neatcc: no file given\n");
1762 if (cpp_init(argv
[i
]))
1763 die("neatcc: cannot open input file\n");
1765 strcpy(obj
, argv
[i
]);
1766 obj
[strlen(obj
) - 1] = 'o';
1767 ofd
= open(obj
, O_WRONLY
| O_TRUNC
| O_CREAT
, 0600);