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
)
77 int unused
; /* unreferenced external symbols */
80 static struct name locals
[MAXLOCALS
];
82 static struct name globals
[MAXGLOBALS
];
85 static void local_add(struct name
*name
)
87 if (nlocals
>= MAXLOCALS
)
88 die("nomem: MAXLOCALS reached!\n");
89 memcpy(&locals
[nlocals
++], name
, sizeof(*name
));
92 static int global_find(char *name
)
95 for (i
= 0; i
< nglobals
; i
++)
96 if (!strcmp(name
, globals
[i
].name
))
101 static void global_add(struct name
*name
)
103 int found
= global_find(name
->name
);
104 int i
= found
== -1 ? nglobals
++ : found
;
105 if (nglobals
>= MAXGLOBALS
)
106 die("nomem: MAXGLOBALS reached!\n");
107 memcpy(&globals
[i
], name
, sizeof(*name
));
110 #define MAXENUMS (1 << 10)
112 static struct enumval
{
118 static void enum_add(char *name
, int val
)
120 struct enumval
*ev
= &enums
[nenums
++];
121 if (nenums
>= MAXENUMS
)
122 die("nomem: MAXENUMS reached!\n");
123 strcpy(ev
->name
, name
);
127 static int enum_find(int *val
, char *name
)
130 for (i
= nenums
- 1; i
>= 0; --i
)
131 if (!strcmp(name
, enums
[i
].name
)) {
138 #define MAXTYPEDEFS (1 << 10)
140 static struct typdefinfo
{
143 } typedefs
[MAXTYPEDEFS
];
144 static int ntypedefs
;
146 static void typedef_add(char *name
, struct type
*type
)
148 struct typdefinfo
*ti
= &typedefs
[ntypedefs
++];
149 if (ntypedefs
>= MAXTYPEDEFS
)
150 die("nomem: MAXTYPEDEFS reached!\n");
151 strcpy(ti
->name
, name
);
152 memcpy(&ti
->type
, type
, sizeof(*type
));
155 static int typedef_find(char *name
)
158 for (i
= ntypedefs
- 1; i
>= 0; --i
)
159 if (!strcmp(name
, typedefs
[i
].name
))
164 #define MAXARRAYS (1 << 10)
166 static struct array
{
172 static int array_add(struct type
*type
, int n
)
174 struct array
*a
= &arrays
[narrays
++];
175 if (narrays
>= MAXARRAYS
)
176 die("nomem: MAXARRAYS reached!\n");
177 memcpy(&a
->type
, type
, sizeof(*type
));
182 static void array2ptr(struct type
*t
)
184 if (!(t
->flags
& T_ARRAY
) || t
->ptr
)
186 memcpy(t
, &arrays
[t
->id
].type
, sizeof(*t
));
190 #define MAXSTRUCTS (1 << 10)
191 #define MAXFIELDS (1 << 7)
193 static struct structinfo
{
195 struct name fields
[MAXFIELDS
];
199 } structs
[MAXSTRUCTS
];
202 static int struct_find(char *name
, int isunion
)
205 for (i
= nstructs
- 1; i
>= 0; --i
)
206 if (*structs
[i
].name
&& !strcmp(name
, structs
[i
].name
) &&
207 structs
[i
].isunion
== isunion
)
210 if (nstructs
>= MAXSTRUCTS
)
211 die("nomem: MAXTYPES reached!\n");
212 memset(&structs
[i
], 0, sizeof(structs
[i
]));
213 strcpy(structs
[i
].name
, name
);
214 structs
[i
].isunion
= isunion
;
218 static struct name
*struct_field(int id
, char *name
)
220 struct structinfo
*si
= &structs
[id
];
222 for (i
= 0; i
< si
->nfields
; i
++)
223 if (!strcmp(name
, si
->fields
[i
].name
))
224 return &si
->fields
[i
];
225 die("field not found\n");
228 #define MAXBREAK (1 << 7)
230 static long breaks
[MAXBREAK
];
232 static long continues
[MAXBREAK
];
233 static int ncontinues
;
235 static void break_fill(long addr
, int till
)
238 for (i
= till
; i
< nbreaks
; i
++)
239 o_filljmp2(breaks
[i
], addr
);
243 static void continue_fill(long addr
, int till
)
246 for (i
= till
; i
< ncontinues
; i
++)
247 o_filljmp2(continues
[i
], addr
);
251 static int type_totsz(struct type
*t
)
255 if (t
->flags
& T_ARRAY
)
256 return arrays
[t
->id
].n
* type_totsz(&arrays
[t
->id
].type
);
257 return t
->flags
& T_STRUCT
? structs
[t
->id
].size
: BT_SZ(t
->bt
);
260 static unsigned type_szde(struct type
*t
)
262 if (t
->flags
& T_ARRAY
)
263 return t
->ptr
> 0 ? 8 : TYPE_SZ(&arrays
[t
->id
].type
);
265 return t
->ptr
> 1 ? 8 : BT_SZ(t
->bt
);
268 static int tok_jmp(int tok
)
270 if (tok_see() != tok
)
276 static void tok_expect(int tok
)
278 if (tok_get() != tok
)
279 die("syntax error\n");
282 static unsigned bt_op(unsigned bt1
, unsigned bt2
)
284 unsigned s1
= BT_SZ(bt1
);
285 unsigned s2
= BT_SZ(bt2
);
286 return (bt1
| bt2
) & BT_SIGNED
| (s1
> s2
? s1
: s2
);
289 static void ts_binop(void (*o_sth
)(void))
295 ts_push_bt(bt_op(TYPE_BT(&t1
), TYPE_BT(&t2
)));
298 static int shifts(int n
)
307 static void ts_binop_add(void (*o_sth
)(void))
314 if (!t1
.ptr
&& !t2
.ptr
) {
316 ts_push_bt(bt_op(TYPE_BT(&t1
), TYPE_BT(&t2
)));
319 if (t1
.ptr
&& !t2
.ptr
) {
325 if (!t1
.ptr
&& t2
.ptr
)
326 if (type_szde(&t2
) > 1) {
327 o_num(shifts(type_szde(&t2
)), 1);
331 if (t1
.ptr
&& t2
.ptr
) {
332 o_num(shifts(type_szde(&t1
)), 1);
334 ts_push_bt(4 | BT_SIGNED
);
340 static void structdef(void *data
, struct name
*name
, unsigned flags
)
342 struct structinfo
*si
= data
;
345 if (si
->size
< type_totsz(&name
->type
))
346 si
->size
= type_totsz(&name
->type
);
348 name
->addr
= si
->size
;
349 si
->size
+= type_totsz(&name
->type
);
351 memcpy(&si
->fields
[si
->nfields
++], name
, sizeof(*name
));
354 static int readdefs(void (*def
)(void *, struct name
*, unsigned f
), void *data
);
356 static int struct_create(char *name
, int isunion
)
358 int id
= struct_find(name
, isunion
);
359 struct structinfo
*si
= &structs
[id
];
361 while (tok_jmp('}')) {
362 readdefs(structdef
, si
);
368 static void readexpr(void);
370 static void enum_create(void)
374 while (tok_jmp('}')) {
376 tok_expect(TOK_NAME
);
377 strcpy(name
, tok_id());
378 if (tok_see() == '=') {
383 die("const expr expected!\n");
390 static int basetype(struct type
*type
, unsigned *flags
)
397 char name
[NAMELEN
] = "";
434 isunion
= tok_get() == TOK_UNION
;
435 if (!tok_jmp(TOK_NAME
))
436 strcpy(name
, tok_id());
437 if (tok_see() == '{')
438 type
->id
= struct_create(name
, isunion
);
440 type
->id
= struct_find(name
, isunion
);
441 type
->flags
|= T_STRUCT
;
447 if (tok_see() == '{')
449 type
->bt
= 4 | BT_SIGNED
;
452 if (tok_see() == TOK_NAME
) {
453 int id
= typedef_find(tok_id());
456 memcpy(type
, &typedefs
[id
].type
,
469 type
->bt
= size
| (sign
? BT_SIGNED
: 0);
473 static int readname(struct type
*main
, char *name
,
474 struct type
*base
, unsigned flags
);
476 static int readtype(struct type
*type
)
478 return readname(type
, NULL
, NULL
, 0);
481 static void readptrs(struct type
*type
)
483 while (!tok_jmp('*')) {
492 static void readpre(void);
494 static void readprimary(void)
497 if (!tok_jmp(TOK_NUM
)) {
498 ts_push_bt(4 | BT_SIGNED
);
499 o_num(tok_num(), 4 | BT_SIGNED
);
502 if (!tok_jmp(TOK_STR
)) {
506 t
.bt
= 1 | BT_SIGNED
;
511 o_symaddr(out_mkdat(NULL
, buf
, len
, 0), TYPE_BT(&t
));
515 if (!tok_jmp(TOK_NAME
)) {
517 char *name
= unkn
.name
;
519 strcpy(name
, tok_id());
520 /* don't search for labels here */
521 if (!ncexpr
&& tok_see() == ':')
523 for (i
= nlocals
- 1; i
>= 0; --i
) {
524 struct type
*t
= &locals
[i
].type
;
525 if (!strcmp(locals
[i
].name
, name
)) {
526 o_local(locals
[i
].addr
, TYPE_BT(t
));
531 if ((n
= global_find(name
)) != -1) {
532 struct name
*g
= &globals
[n
];
533 struct type
*t
= &g
->type
;
536 if (t
->flags
& T_FUNC
&& !t
->ptr
)
537 g
->addr
= out_mkundef(name
, 0);
539 g
->addr
= out_mkundef(name
, type_totsz(t
));
541 o_symaddr(g
->addr
, TYPE_BT(t
));
545 if (!enum_find(&n
, name
)) {
546 ts_push_bt(4 | BT_SIGNED
);
547 o_num(n
, 4 | BT_SIGNED
);
550 if (tok_see() != '(')
551 die("unknown symbol\n");
552 unkn
.addr
= out_mkundef(unkn
.name
, 0);
555 o_symaddr(unkn
.addr
, 8);
566 if (!t
.ptr
|| !o
.ptr
)
576 void arrayderef(struct type
*t
)
578 int sz
= type_totsz(t
);
587 static void inc_post(void (*op
)(void))
589 unsigned bt
= TYPE_BT(&ts
[nts
- 1]);
603 static void readfield(void)
607 tok_expect(TOK_NAME
);
610 field
= struct_field(t
.id
, tok_id());
612 o_num(field
->addr
, 4);
615 o_deref(TYPE_BT(&field
->type
));
616 ts_push(&field
->type
);
619 #define MAXFUNCS (1 << 10)
621 static struct funcinfo
{
622 struct type args
[MAXFIELDS
];
627 static unsigned ret_bt
;
629 static int func_create(struct type
*ret
, struct name
*args
, int nargs
)
631 struct funcinfo
*fi
= &funcs
[nfuncs
++];
633 if (nfuncs
>= MAXFUNCS
)
634 die("nomem: MAXFUNCS reached!\n");
635 memcpy(&fi
->ret
, ret
, sizeof(*ret
));
636 for (i
= 0; i
< nargs
; i
++)
637 memcpy(&fi
->args
[i
], &args
[i
].type
, sizeof(*ret
));
642 static void readcall(void)
645 unsigned bt
[MAXARGS
];
649 if (tok_see() != ')') {
652 bt
[argc
++] = TYPE_BT(&t
);
654 while (!tok_jmp(',')) {
657 bt
[argc
++] = TYPE_BT(&t
);
661 if (t
.flags
& T_FUNC
&& t
.ptr
> 0)
663 fi
= t
.flags
& T_FUNC
? &funcs
[t
.id
] : NULL
;
665 for (i
= 0; i
< fi
->nargs
; i
++)
666 bt
[i
] = TYPE_BT(&fi
->args
[i
]);
667 o_call(argc
, bt
, fi
? TYPE_BT(&fi
->ret
) : 4 | BT_SIGNED
);
671 ts_push_bt(4 | BT_SIGNED
);
674 static void readpost(void)
694 if (!tok_jmp(TOK2("++"))) {
698 if (!tok_jmp(TOK2("--"))) {
707 if (!tok_jmp(TOK2("->"))) {
715 static void inc_pre(void (*op
)(void))
717 unsigned bt
= TYPE_BT(&ts
[nts
- 1]);
728 static void readpre(void)
734 if (!(type
.flags
& T_FUNC
) && !type
.ptr
)
745 if (!(t
.flags
& T_FUNC
) || t
.ptr
> 0) {
747 o_deref(TYPE_BT(&t
));
757 ts_push_bt(4 | BT_SIGNED
);
770 if (!tok_jmp(TOK2("++"))) {
774 if (!tok_jmp(TOK2("--"))) {
778 if (!tok_jmp(TOK_SIZEOF
)) {
780 int op
= !tok_jmp('(');
782 int nogen
= !o_nogen();
790 o_num(type_totsz(&t
), 4);
798 static void readmul(void)
821 static void readadd(void)
839 static void shift(void (*op
)(void))
846 ts_push_bt(TYPE_BT(&t
));
849 static void readshift(void)
853 if (!tok_jmp(TOK2("<<"))) {
857 if (!tok_jmp(TOK2(">>"))) {
865 static void cmp(void (*op
)(void))
871 ts_push_bt(4 | BT_SIGNED
);
874 static void readcmp(void)
886 if (!tok_jmp(TOK2("<="))) {
890 if (!tok_jmp(TOK2(">="))) {
898 static void eq(void (*op
)(void))
904 ts_push_bt(4 | BT_SIGNED
);
907 static void readeq(void)
911 if (!tok_jmp(TOK2("=="))) {
915 if (!tok_jmp(TOK2("!="))) {
923 static void readbitand(void)
926 while (!tok_jmp('&')) {
932 static void readxor(void)
935 while (!tok_jmp('^')) {
941 static void readbitor(void)
944 while (!tok_jmp('|')) {
950 #define MAXCOND (1 << 7)
952 static void readand(void)
959 if (tok_see() != TOK2("&&"))
961 conds
[nconds
++] = o_jz(0);
963 while (!tok_jmp(TOK2("&&"))) {
965 conds
[nconds
++] = o_jz(0);
968 o_num(1, 4 | BT_SIGNED
);
971 for (i
= 0; i
< nconds
; i
++)
973 o_num(0, 4 | BT_SIGNED
);
976 ts_push_bt(4 | BT_SIGNED
);
979 static void reador(void)
986 if (tok_see() != TOK2("||"))
988 conds
[nconds
++] = o_jnz(0);
990 while (!tok_jmp(TOK2("||"))) {
992 conds
[nconds
++] = o_jnz(0);
995 o_num(0, 4 | BT_SIGNED
);
998 for (i
= 0; i
< nconds
; i
++)
1000 o_num(1, 4 | BT_SIGNED
);
1003 ts_push_bt(4 | BT_SIGNED
);
1006 static void readcexpr(void)
1015 cexpr
= !o_popnum(&c
);
1055 static void opassign(void (*bop
)(void (*op
)(void)), void (*op
)(void))
1057 unsigned bt
= TYPE_BT(&ts
[nts
- 1]);
1065 static void doassign(void)
1069 if (!t
.ptr
&& t
.flags
& T_STRUCT
)
1070 o_memcpy(type_totsz(&t
));
1072 o_assign(TYPE_BT(&ts
[nts
- 1]));
1075 static void readexpr(void)
1078 if (!tok_jmp('=')) {
1083 if (!tok_jmp(TOK2("+="))) {
1084 opassign(ts_binop_add
, o_add
);
1087 if (!tok_jmp(TOK2("-="))) {
1088 opassign(ts_binop_add
, o_sub
);
1091 if (!tok_jmp(TOK2("*="))) {
1092 opassign(ts_binop
, o_mul
);
1095 if (!tok_jmp(TOK2("/="))) {
1096 opassign(ts_binop
, o_div
);
1099 if (!tok_jmp(TOK2("%="))) {
1100 opassign(ts_binop
, o_mod
);
1103 if (!tok_jmp(TOK3("<<="))) {
1104 opassign(ts_binop
, o_shl
);
1107 if (!tok_jmp(TOK3(">>="))) {
1108 opassign(ts_binop
, o_shr
);
1111 if (!tok_jmp(TOK3("&="))) {
1112 opassign(ts_binop
, o_and
);
1115 if (!tok_jmp(TOK3("|="))) {
1116 opassign(ts_binop
, o_or
);
1119 if (!tok_jmp(TOK3("^="))) {
1120 opassign(ts_binop
, o_xor
);
1125 static void readestmt(void)
1131 } while (!tok_jmp(','));
1134 static void o_localoff(long addr
, int off
, unsigned bt
)
1145 static struct type
*innertype(struct type
*t
)
1147 if (t
->flags
& T_ARRAY
&& !t
->ptr
)
1148 return innertype(&arrays
[t
->id
].type
);
1152 static void initexpr(struct type
*t
, int off
, void *obj
,
1153 void (*set
)(void *obj
, int off
, struct type
*t
))
1159 if (!t
->ptr
&& t
->flags
& T_STRUCT
) {
1160 struct structinfo
*si
= &structs
[t
->id
];
1162 for (i
= 0; i
< si
->nfields
; i
++) {
1163 struct name
*field
= &si
->fields
[i
];
1164 if (!tok_jmp('.')) {
1165 tok_expect(TOK_NAME
);
1166 field
= struct_field(t
->id
, tok_id());
1169 initexpr(&field
->type
, off
+ field
->addr
, obj
, set
);
1170 if (tok_jmp(',') || tok_see() == '}')
1173 } else if (t
->flags
& T_ARRAY
) {
1174 struct type
*t_de
= &arrays
[t
->id
].type
;
1176 for (i
= 0; ; i
++) {
1178 struct type
*it
= t_de
;
1179 if (!tok_jmp('[')) {
1186 if (tok_see() != '{')
1187 it
= innertype(t_de
);
1188 initexpr(it
, off
+ type_totsz(it
) * idx
, obj
, set
);
1189 if (tok_jmp(',') || tok_see() == '}')
1196 static void jumpbrace(void)
1199 while (tok_see() != '}' || depth
--)
1200 if (tok_get() == '{')
1205 static int initsize(void)
1207 long addr
= tok_addr();
1209 if (!tok_jmp(TOK_STR
)) {
1216 while (tok_jmp('}')) {
1218 if (!tok_jmp('[')) {
1227 while (tok_see() != '}' && tok_see() != ',')
1228 if (tok_get() == '{')
1237 #define F_GLOBAL(flags) (!((flags) & F_STATIC))
1239 static void globalinit(void *obj
, int off
, struct type
*t
)
1241 long addr
= *(long *) obj
;
1242 if (t
->flags
& T_ARRAY
&& tok_see() == TOK_STR
) {
1243 struct type
*t_de
= &arrays
[t
->id
].type
;
1244 if (!t_de
->ptr
&& !t_de
->flags
&& TYPE_SZ(t_de
) == 1) {
1247 tok_expect(TOK_STR
);
1249 out_datcpy(addr
, off
, buf
, len
);
1254 o_datset(addr
, off
, TYPE_BT(t
));
1258 static void globaldef(void *data
, struct name
*name
, unsigned flags
)
1260 struct type
*t
= &name
->type
;
1261 char *varname
= flags
& F_STATIC
? NULL
: name
->name
;
1263 if (t
->flags
& T_ARRAY
&& !t
->ptr
&& !arrays
[t
->id
].n
)
1264 arrays
[t
->id
].n
= initsize();
1266 if (flags
& F_EXTERN
|| t
->flags
& T_FUNC
&& !t
->ptr
)
1268 else if (flags
& F_INIT
)
1269 name
->addr
= out_mkdat(varname
, NULL
, sz
, F_GLOBAL(flags
));
1271 name
->addr
= out_mkvar(varname
, sz
, F_GLOBAL(flags
));
1274 initexpr(t
, 0, &name
->addr
, globalinit
);
1277 static void localinit(void *obj
, int off
, struct type
*t
)
1279 long addr
= *(long *) obj
;
1280 if (t
->flags
& T_ARRAY
&& tok_see() == TOK_STR
) {
1281 struct type
*t_de
= &arrays
[t
->id
].type
;
1282 if (!t_de
->ptr
&& !t_de
->flags
&& TYPE_SZ(t_de
) == 1) {
1285 tok_expect(TOK_STR
);
1287 o_localoff(addr
, off
, TYPE_BT(t
));
1288 o_symaddr(out_mkdat(NULL
, buf
, len
, 0), TYPE_BT(t
));
1294 o_localoff(addr
, off
, TYPE_BT(t
));
1302 static void localdef(void *data
, struct name
*name
, unsigned flags
)
1304 struct type
*t
= &name
->type
;
1305 if (flags
& (F_STATIC
| F_EXTERN
)) {
1306 globaldef(data
, name
, flags
);
1309 if (t
->flags
& T_ARRAY
&& !t
->ptr
&& !arrays
[t
->id
].n
)
1310 arrays
[t
->id
].n
= initsize();
1311 name
->addr
= o_mklocal(type_totsz(&name
->type
));
1313 if (flags
& F_INIT
) {
1314 if (t
->flags
& (T_ARRAY
| T_STRUCT
) && !t
->ptr
) {
1315 o_local(name
->addr
, TYPE_BT(t
));
1316 o_memset(0, type_totsz(t
));
1319 initexpr(t
, 0, &name
->addr
, localinit
);
1323 static void funcdef(char *name
, struct type
*type
, struct name
*args
,
1324 int nargs
, unsigned flags
)
1328 strcpy(global
.name
, name
);
1329 memcpy(&global
.type
, type
, sizeof(*type
));
1330 global
.addr
= o_func_beg(name
, F_GLOBAL(flags
));
1332 global_add(&global
);
1333 ret_bt
= TYPE_BT(&funcs
[type
->id
].ret
);
1334 for (i
= 0; i
< nargs
; i
++) {
1335 args
[i
].addr
= o_arg(i
, type_totsz(&args
[i
].type
));
1336 local_add(&args
[i
]);
1340 static int readargs(struct name
*args
)
1344 while (tok_see() != ')') {
1345 if (!tok_jmp(TOK3("...")))
1347 readname(&args
[nargs
].type
, args
[nargs
].name
, NULL
, 0);
1348 array2ptr(&args
[nargs
].type
);
1354 if (nargs
== 1 && !TYPE_BT(&args
[0].type
))
1359 static int readname(struct type
*main
, char *name
,
1360 struct type
*base
, unsigned flags
)
1362 struct type tpool
[3];
1364 struct type
*type
= &tpool
[npool
++];
1365 struct type
*func
= NULL
;
1366 struct type
*ret
= NULL
;
1367 memset(tpool
, 0, sizeof(tpool
));
1371 if (basetype(type
, &flags
))
1374 memcpy(type
, base
, sizeof(*base
));
1377 if (!tok_jmp('(')) {
1379 type
= &tpool
[npool
++];
1383 if (!tok_jmp(TOK_NAME
) && name
)
1384 strcpy(name
, tok_id());
1385 while (!tok_jmp('[')) {
1391 die("const expr expected\n");
1394 type
->id
= array_add(type
, n
);
1395 if (type
->flags
& T_FUNC
)
1396 func
= &arrays
[type
->id
].type
;
1397 type
->flags
= T_ARRAY
;
1403 if (tok_see() == '(') {
1404 struct name args
[MAXARGS
];
1405 int nargs
= readargs(args
);
1409 type
= &tpool
[npool
++];
1412 func
->flags
= T_FUNC
;
1414 func
->id
= func_create(ret
, args
, nargs
);
1415 if (fdef
&& tok_see() == '{') {
1416 funcdef(name
, func
, args
, nargs
, flags
);
1420 memcpy(main
, type
, sizeof(*type
));
1424 static int readdefs(void (*def
)(void *data
, struct name
*name
, unsigned flags
),
1429 if (basetype(&base
, &flags
))
1431 while (tok_see() != ';' && tok_see() != '{') {
1434 if (readname(&name
.type
, name
.name
, &base
, flags
))
1438 def(data
, &name
, flags
);
1444 static void typedefdef(void *data
, struct name
*name
, unsigned flags
)
1446 typedef_add(name
->name
, &name
->type
);
1449 static void readstmt(void);
1451 #define MAXCASES (1 << 7)
1453 static void readswitch(void)
1455 int break_beg
= nbreaks
;
1456 long val_addr
= o_mklocal(8);
1457 long matched
[MAXCASES
];
1466 o_local(val_addr
, TYPE_BT(&t
));
1468 o_assign(TYPE_BT(&t
));
1472 while (tok_jmp('}')) {
1474 while (tok_see() == TOK_CASE
|| tok_see() == TOK_DEFAULT
) {
1476 matched
[nmatched
++] = o_jmp(0);
1479 if (!tok_jmp(TOK_CASE
)) {
1481 o_local(val_addr
, TYPE_BT(&t
));
1490 if (!tok_jmp(TOK_DEFAULT
)) {
1495 for (i
= 0; i
< nmatched
; i
++)
1496 o_filljmp(matched
[i
]);
1500 o_rmlocal(val_addr
, 8);
1503 break_fill(o_mklabel(), break_beg
);
1506 #define MAXGOTO (1 << 10)
1508 static struct gotoinfo
{
1514 static struct labelinfo
{
1520 static void goto_add(char *name
)
1522 strcpy(gotos
[ngotos
].name
, name
);
1523 gotos
[ngotos
++].addr
= o_jmp(0);
1526 static void label_add(char *name
)
1528 strcpy(labels
[nlabels
].name
, name
);
1529 labels
[nlabels
++].addr
= o_mklabel();
1532 static void goto_fill(void)
1535 for (i
= 0; i
< ngotos
; i
++)
1536 for (j
= 0; j
< nlabels
; j
++)
1537 if (!strcmp(gotos
[i
].name
, labels
[j
].name
)) {
1538 o_filljmp2(gotos
[i
].addr
, labels
[j
].addr
);
1543 static void readstmt(void)
1547 if (!tok_jmp('{')) {
1548 int _nlocals
= nlocals
;
1549 int _nglobals
= nglobals
;
1550 int _nenums
= nenums
;
1551 int _ntypedefs
= ntypedefs
;
1552 int _nstructs
= nstructs
;
1553 int _nfuncs
= nfuncs
;
1554 int _narrays
= narrays
;
1555 while (tok_jmp('}'))
1559 ntypedefs
= _ntypedefs
;
1560 nstructs
= _nstructs
;
1563 nglobals
= _nglobals
;
1566 if (!readdefs(localdef
, NULL
)) {
1570 if (!tok_jmp(TOK_TYPEDEF
)) {
1571 readdefs(typedefdef
, NULL
);
1575 if (!tok_jmp(TOK_IF
)) {
1582 if (!tok_jmp(TOK_ELSE
)) {
1592 if (!tok_jmp(TOK_WHILE
)) {
1594 int break_beg
= nbreaks
;
1595 int continue_beg
= ncontinues
;
1604 break_fill(o_mklabel(), break_beg
);
1605 continue_fill(l1
, continue_beg
);
1608 if (!tok_jmp(TOK_DO
)) {
1610 int break_beg
= nbreaks
;
1611 int continue_beg
= ncontinues
;
1614 tok_expect(TOK_WHILE
);
1620 break_fill(o_mklabel(), break_beg
);
1621 continue_fill(l2
, continue_beg
);
1624 if (!tok_jmp(TOK_FOR
)) {
1625 long l_check
, l_jump
, j_fail
, j_pass
;
1626 int break_beg
= nbreaks
;
1627 int continue_beg
= ncontinues
;
1630 if (tok_see() != ';')
1633 l_check
= o_mklabel();
1634 if (tok_see() != ';') {
1641 l_jump
= o_mklabel();
1642 if (tok_see() != ')')
1651 break_fill(o_mklabel(), break_beg
);
1652 continue_fill(l_jump
, continue_beg
);
1655 if (!tok_jmp(TOK_SWITCH
)) {
1659 if (!tok_jmp(TOK_RETURN
)) {
1660 int ret
= tok_see() != ';';
1667 if (!tok_jmp(TOK_BREAK
)) {
1669 breaks
[nbreaks
++] = o_jmp(0);
1672 if (!tok_jmp(TOK_CONTINUE
)) {
1674 continues
[ncontinues
++] = o_jmp(0);
1677 if (!tok_jmp(TOK_GOTO
)) {
1678 tok_expect(TOK_NAME
);
1685 if (!tok_jmp(':')) {
1686 label_add(tok_id());
1692 static void readdecl(void)
1694 if (!tok_jmp(TOK_TYPEDEF
)) {
1695 readdefs(typedefdef
, NULL
);
1699 readdefs(globaldef
, NULL
);
1700 if (tok_see() == '{') {
1712 static void parse(void)
1714 while (tok_see() != TOK_EOF
)
1718 int main(int argc
, char *argv
[])
1723 while (i
< argc
&& argv
[i
][0] == '-') {
1724 if (argv
[i
][1] == 'I')
1725 cpp_addpath(argv
[i
][2] ? argv
[i
] + 2 : argv
[++i
]);
1726 if (argv
[i
][1] == 'D') {
1727 char *name
= argv
[i
] + 2;
1729 char *eq
= strchr(name
, '=');
1734 cpp_define(name
, def
);
1739 die("no file given\n");
1740 ifd
= open(argv
[i
], O_RDONLY
);
1745 strcpy(obj
, argv
[i
]);
1746 obj
[strlen(obj
) - 1] = 'o';
1747 ofd
= open(obj
, O_WRONLY
| O_TRUNC
| O_CREAT
, 0600);