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)
38 int id
; /* for structs, functions and arrays */
42 static struct type ts
[MAXTMP
];
45 static void ts_push_bt(unsigned bt
)
52 static void ts_push(struct type
*t
)
54 memcpy(&ts
[nts
++], t
, sizeof(*t
));
55 if (t
->flags
& (T_FUNC
| T_ARRAY
) && !t
->ptr
)
59 static void ts_pop(struct type
*type
)
72 static struct name locals
[MAXLOCALS
];
74 static struct name globals
[MAXGLOBALS
];
77 static void local_add(struct name
*name
)
79 memcpy(&locals
[nlocals
++], name
, sizeof(*name
));
82 static int global_find(char *name
)
85 for (i
= 0; i
< nglobals
; i
++)
86 if (!strcmp(name
, globals
[i
].name
))
91 static void global_add(struct name
*name
)
93 int found
= global_find(name
->name
);
94 int i
= found
== -1 ? nglobals
++ : found
;
95 memcpy(&globals
[i
], name
, sizeof(*name
));
98 static void die(char *s
)
104 #define MAXENUMS (1 << 10)
106 static struct enumval
{
112 static void enum_add(char *name
, int val
)
114 struct enumval
*ev
= &enums
[nenums
++];
115 strcpy(ev
->name
, name
);
119 static int enum_find(int *val
, char *name
)
122 for (i
= nenums
- 1; i
>= 0; --i
)
123 if (!strcmp(name
, enums
[i
].name
)) {
130 #define MAXTYPEDEFS (1 << 10)
132 static struct typdefinfo
{
135 } typedefs
[MAXTYPEDEFS
];
136 static int ntypedefs
;
138 static void typedef_add(char *name
, struct type
*type
)
140 struct typdefinfo
*ti
= &typedefs
[ntypedefs
++];
141 strcpy(ti
->name
, name
);
142 memcpy(&ti
->type
, type
, sizeof(*type
));
145 static int typedef_find(char *name
)
148 for (i
= ntypedefs
- 1; i
>= 0; --i
)
149 if (!strcmp(name
, typedefs
[i
].name
))
154 #define MAXARRAYS (1 << 10)
156 static struct array
{
162 static int array_add(struct type
*type
, int n
)
164 struct array
*a
= &arrays
[narrays
++];
165 memcpy(&a
->type
, type
, sizeof(*type
));
170 static void array2ptr(struct type
*t
)
172 if (!(t
->flags
& T_ARRAY
) || t
->ptr
)
174 memcpy(t
, &arrays
[t
->id
].type
, sizeof(*t
));
178 #define MAXTYPES (1 << 10)
179 #define MAXFIELDS (1 << 7)
181 static struct structinfo
{
183 struct name fields
[MAXFIELDS
];
190 static int struct_find(char *name
, int isunion
)
193 for (i
= nstructs
- 1; i
>= 0; --i
)
194 if (*structs
[i
].name
&& !strcmp(name
, structs
[i
].name
) &&
195 structs
[i
].isunion
== isunion
)
198 memset(&structs
[i
], 0, sizeof(structs
[i
]));
199 strcpy(structs
[i
].name
, name
);
200 structs
[i
].isunion
= isunion
;
204 static struct name
*struct_field(int id
, char *name
)
206 struct structinfo
*si
= &structs
[id
];
208 for (i
= 0; i
< si
->nfields
; i
++)
209 if (!strcmp(name
, si
->fields
[i
].name
))
210 return &si
->fields
[i
];
211 die("field not found\n");
214 #define MAXBREAK (1 << 7)
216 static long breaks
[MAXBREAK
];
218 static long continues
[MAXBREAK
];
219 static int ncontinues
;
221 static void break_fill(long addr
, int till
)
224 for (i
= till
; i
< nbreaks
; i
++)
225 o_filljmp2(breaks
[i
], addr
);
229 static void continue_fill(long addr
, int till
)
232 for (i
= till
; i
< ncontinues
; i
++)
233 o_filljmp2(continues
[i
], addr
);
237 static int type_totsz(struct type
*t
)
241 if (t
->flags
& T_ARRAY
)
242 return arrays
[t
->id
].n
* type_totsz(&arrays
[t
->id
].type
);
243 return t
->flags
& T_STRUCT
? structs
[t
->id
].size
: BT_SZ(t
->bt
);
246 static unsigned type_szde(struct type
*t
)
248 if (t
->flags
& T_ARRAY
)
249 return t
->ptr
> 0 ? 8 : TYPE_SZ(&arrays
[t
->id
].type
);
251 return t
->ptr
> 1 ? 8 : BT_SZ(t
->bt
);
254 static int tok_jmp(int tok
)
256 if (tok_see() != tok
)
262 static void tok_expect(int tok
)
264 if (tok_get() != tok
)
265 die("syntax error\n");
268 static unsigned bt_op(unsigned bt1
, unsigned bt2
)
270 unsigned s1
= BT_SZ(bt1
);
271 unsigned s2
= BT_SZ(bt2
);
272 return (bt1
| bt2
) & BT_SIGNED
| (s1
> s2
? s1
: s2
);
275 static void ts_binop(void (*o_sth
)(void))
281 ts_push_bt(bt_op(TYPE_BT(&t1
), TYPE_BT(&t2
)));
284 static int shifts(int n
)
293 static void ts_binop_add(void (*o_sth
)(void))
300 if (!t1
.ptr
&& !t2
.ptr
) {
302 ts_push_bt(bt_op(TYPE_BT(&t1
), TYPE_BT(&t2
)));
305 if (t1
.ptr
&& !t2
.ptr
) {
311 if (!t1
.ptr
&& t2
.ptr
)
312 if (type_szde(&t2
) > 1) {
313 o_num(shifts(type_szde(&t2
)), 1);
317 if (t1
.ptr
&& t2
.ptr
) {
318 o_num(shifts(type_szde(&t1
)), 1);
320 ts_push_bt(4 | BT_SIGNED
);
326 static void structdef(void *data
, struct name
*name
, unsigned flags
)
328 struct structinfo
*si
= data
;
331 if (si
->size
< type_totsz(&name
->type
))
332 si
->size
= type_totsz(&name
->type
);
334 name
->addr
= si
->size
;
335 si
->size
+= type_totsz(&name
->type
);
337 memcpy(&si
->fields
[si
->nfields
++], name
, sizeof(*name
));
340 static int readdefs(void (*def
)(void *, struct name
*, unsigned f
), void *data
);
342 static int struct_create(char *name
, int isunion
)
344 int id
= struct_find(name
, isunion
);
345 struct structinfo
*si
= &structs
[id
];
347 while (tok_jmp('}')) {
348 readdefs(structdef
, si
);
354 static void readexpr(void);
356 static void enum_create(void)
360 while (tok_jmp('}')) {
362 tok_expect(TOK_NAME
);
363 strcpy(name
, tok_id());
364 if (tok_see() == '=') {
369 die("const expr expected!\n");
376 static int basetype(struct type
*type
, unsigned *flags
)
383 char name
[NAMELEN
] = "";
417 isunion
= tok_get() == TOK_UNION
;
418 if (!tok_jmp(TOK_NAME
))
419 strcpy(name
, tok_id());
420 if (tok_see() == '{')
421 type
->id
= struct_create(name
, isunion
);
423 type
->id
= struct_find(name
, isunion
);
424 type
->flags
|= T_STRUCT
;
430 if (tok_see() == '{')
432 type
->bt
= 4 | BT_SIGNED
;
435 if (tok_see() == TOK_NAME
) {
436 int id
= typedef_find(tok_id());
439 memcpy(type
, &typedefs
[id
].type
,
452 type
->bt
= size
| (sign
? BT_SIGNED
: 0);
456 static int readname(struct type
*main
, char *name
,
457 struct type
*base
, unsigned flags
);
459 static int readtype(struct type
*type
)
461 return readname(type
, NULL
, NULL
, 0);
464 static void readptrs(struct type
*type
)
466 while (!tok_jmp('*')) {
473 static void readpre(void);
475 static void readprimary(void)
479 if (!tok_jmp(TOK_NUM
)) {
480 ts_push_bt(4 | BT_SIGNED
);
481 o_num(tok_num(), 4 | BT_SIGNED
);
484 if (!tok_jmp(TOK_STR
)) {
488 t
.bt
= 1 | BT_SIGNED
;
493 o_symaddr(o_mkdat(NULL
, buf
, len
, 0), TYPE_BT(&t
));
497 if (!tok_jmp(TOK_NAME
)) {
499 for (i
= nlocals
- 1; i
>= 0; --i
) {
500 struct type
*t
= &locals
[i
].type
;
501 if (!strcmp(locals
[i
].name
, tok_id())) {
502 o_local(locals
[i
].addr
, TYPE_BT(t
));
507 if ((n
= global_find(tok_id())) != -1) {
508 struct type
*t
= &globals
[n
].type
;
509 o_symaddr(globals
[n
].addr
, TYPE_BT(t
));
513 if (!enum_find(&n
, tok_id())) {
514 ts_push_bt(4 | BT_SIGNED
);
515 o_num(n
, 4 | BT_SIGNED
);
518 strcpy(name
.name
, tok_id());
519 name
.addr
= o_mkundef(name
.name
);
522 o_symaddr(name
.addr
, 8);
533 if (!t
.ptr
|| !o
.ptr
)
543 void arrayderef(struct type
*t
)
545 int sz
= type_totsz(t
);
554 static void inc_post(void (*op
)(void))
556 unsigned bt
= TYPE_BT(&ts
[nts
- 1]);
570 static void readfield(void)
574 tok_expect(TOK_NAME
);
576 field
= struct_field(t
.id
, tok_id());
578 o_num(field
->addr
, 4);
581 o_deref(TYPE_BT(&field
->type
));
582 ts_push(&field
->type
);
585 #define MAXFUNCS (1 << 10)
587 static struct funcinfo
{
588 struct type args
[MAXFIELDS
];
593 static unsigned ret_bt
;
595 static int func_create(struct type
*ret
, struct name
*args
, int nargs
)
597 struct funcinfo
*fi
= &funcs
[nfuncs
++];
599 memcpy(&fi
->ret
, ret
, sizeof(*ret
));
600 for (i
= 0; i
< nargs
; i
++)
601 memcpy(&fi
->args
[i
], &args
[i
].type
, sizeof(*ret
));
606 static void readcall(void)
609 unsigned bt
[MAXARGS
];
613 if (tok_see() != ')') {
616 bt
[argc
++] = TYPE_BT(&t
);
618 while (!tok_jmp(',')) {
621 bt
[argc
++] = TYPE_BT(&t
);
625 if (t
.flags
& T_FUNC
&& t
.ptr
> 0)
627 fi
= t
.flags
& T_FUNC
? &funcs
[t
.id
] : NULL
;
629 for (i
= 0; i
< fi
->nargs
; i
++)
630 bt
[i
] = TYPE_BT(&fi
->args
[i
]);
631 o_call(argc
, bt
, fi
? TYPE_BT(&fi
->ret
) : 4 | BT_SIGNED
);
635 ts_push_bt(4 | BT_SIGNED
);
638 static void readpost(void)
658 if (!tok_jmp(TOK2("++"))) {
662 if (!tok_jmp(TOK2("--"))) {
671 if (!tok_jmp(TOK2("->"))) {
679 static void inc_pre(void (*op
)(void))
681 unsigned bt
= TYPE_BT(&ts
[nts
- 1]);
692 static void readpre(void)
698 if (!(type
.flags
& T_FUNC
) && !type
.ptr
)
709 if (!(t
.flags
& T_FUNC
) || t
.ptr
> 0) {
711 o_deref(TYPE_BT(&t
));
721 ts_push_bt(4 | BT_SIGNED
);
734 if (!tok_jmp(TOK2("++"))) {
738 if (!tok_jmp(TOK2("--"))) {
742 if (!tok_jmp(TOK_SIZEOF
)) {
744 int op
= !tok_jmp('(');
746 int nogen
= !o_nogen();
754 o_num(type_totsz(&t
), 4);
762 static void readmul(void)
785 static void readadd(void)
803 static void shift(void (*op
)(void))
810 ts_push_bt(TYPE_BT(&t
));
813 static void readshift(void)
817 if (!tok_jmp(TOK2("<<"))) {
821 if (!tok_jmp(TOK2(">>"))) {
829 static void cmp(void (*op
)(void))
835 ts_push_bt(4 | BT_SIGNED
);
838 static void readcmp(void)
850 if (!tok_jmp(TOK2("<="))) {
854 if (!tok_jmp(TOK2(">="))) {
862 static void eq(void (*op
)(void))
868 ts_push_bt(4 | BT_SIGNED
);
871 static void readeq(void)
875 if (!tok_jmp(TOK2("=="))) {
879 if (!tok_jmp(TOK2("!="))) {
887 static void readbitand(void)
890 while (!tok_jmp('&')) {
896 static void readxor(void)
899 while (!tok_jmp('^')) {
905 static void readbitor(void)
908 while (!tok_jmp('|')) {
914 #define MAXCOND (1 << 7)
916 static void readand(void)
923 if (tok_see() != TOK2("&&"))
925 conds
[nconds
++] = o_jz(0);
927 while (!tok_jmp(TOK2("&&"))) {
929 conds
[nconds
++] = o_jz(0);
932 o_num(1, 4 | BT_SIGNED
);
935 for (i
= 0; i
< nconds
; i
++)
937 o_num(0, 4 | BT_SIGNED
);
940 ts_push_bt(4 | BT_SIGNED
);
943 static void reador(void)
950 if (tok_see() != TOK2("||"))
952 conds
[nconds
++] = o_jnz(0);
954 while (!tok_jmp(TOK2("||"))) {
956 conds
[nconds
++] = o_jnz(0);
959 o_num(0, 4 | BT_SIGNED
);
962 for (i
= 0; i
< nconds
; i
++)
964 o_num(1, 4 | BT_SIGNED
);
967 ts_push_bt(4 | BT_SIGNED
);
970 static void readcexpr(void)
978 cexpr
= !o_popnum(&c
);
1017 static void opassign(void (*bop
)(void (*op
)(void)), void (*op
)(void))
1019 unsigned bt
= TYPE_BT(&ts
[nts
- 1]);
1027 static void doassign(void)
1031 if (!t
.ptr
&& t
.flags
& T_STRUCT
)
1032 o_memcpy(type_totsz(&t
));
1034 o_assign(TYPE_BT(&ts
[nts
- 1]));
1037 static void readexpr(void)
1040 if (!tok_jmp('=')) {
1045 if (!tok_jmp(TOK2("+="))) {
1046 opassign(ts_binop_add
, o_add
);
1049 if (!tok_jmp(TOK2("-="))) {
1050 opassign(ts_binop_add
, o_sub
);
1053 if (!tok_jmp(TOK2("*="))) {
1054 opassign(ts_binop
, o_mul
);
1057 if (!tok_jmp(TOK2("/="))) {
1058 opassign(ts_binop
, o_div
);
1061 if (!tok_jmp(TOK2("%="))) {
1062 opassign(ts_binop
, o_mod
);
1065 if (!tok_jmp(TOK3("<<="))) {
1066 opassign(ts_binop
, o_shl
);
1069 if (!tok_jmp(TOK3(">>="))) {
1070 opassign(ts_binop
, o_shr
);
1073 if (!tok_jmp(TOK3("&="))) {
1074 opassign(ts_binop
, o_and
);
1077 if (!tok_jmp(TOK3("|="))) {
1078 opassign(ts_binop
, o_or
);
1081 if (!tok_jmp(TOK3("^="))) {
1082 opassign(ts_binop
, o_xor
);
1087 static void readestmt(void)
1093 } while (!tok_jmp(','));
1096 #define F_GLOBAL(flags) (!((flags) & F_STATIC))
1098 static void globaldef(void *data
, struct name
*name
, unsigned flags
)
1100 char *varname
= flags
& F_STATIC
? NULL
: name
->name
;
1101 name
->addr
= o_mkvar(varname
, type_totsz(&name
->type
), F_GLOBAL(flags
));
1105 static void o_localoff(long addr
, int off
, unsigned bt
)
1116 static struct type
*innertype(struct type
*t
)
1118 if (t
->flags
& T_ARRAY
&& !t
->ptr
)
1119 return innertype(&arrays
[t
->id
].type
);
1123 static void initexpr(struct type
*t
, long addr
, int off
)
1125 if (t
->flags
& T_ARRAY
&& tok_see() == TOK_STR
) {
1126 struct type
*t_de
= &arrays
[t
->id
].type
;
1127 if (!t_de
->ptr
&& !t_de
->flags
&& TYPE_SZ(t_de
) == 1) {
1130 tok_expect(TOK_STR
);
1132 o_localoff(addr
, off
, TYPE_BT(t
));
1133 o_symaddr(o_mkdat(NULL
, buf
, len
, 0), TYPE_BT(t
));
1136 o_localoff(addr
, off
+ len
, TYPE_BT(t
));
1137 o_memset(0, arrays
[t
->id
].n
- len
);
1143 o_localoff(addr
, off
, TYPE_BT(t
));
1151 if (!t
->ptr
&& t
->flags
& T_STRUCT
) {
1152 struct structinfo
*si
= &structs
[t
->id
];
1154 for (i
= 0; i
< si
->nfields
; i
++) {
1155 struct name
*field
= &si
->fields
[i
];
1156 if (!tok_jmp('.')) {
1157 tok_expect(TOK_NAME
);
1158 field
= struct_field(t
->id
, tok_id());
1161 initexpr(&field
->type
, addr
, off
+ field
->addr
);
1162 if (tok_jmp(',') || tok_see() == '}')
1165 } else if (t
->flags
& T_ARRAY
) {
1166 struct type
*t_de
= &arrays
[t
->id
].type
;
1168 for (i
= 0; ; i
++) {
1170 struct type
*it
= t_de
;
1171 if (!tok_jmp('[')) {
1178 if (tok_see() != '{')
1179 it
= innertype(t_de
);
1180 initexpr(it
, addr
, off
+ type_totsz(it
) * idx
);
1181 if (tok_jmp(',') || tok_see() == '}')
1188 static void localdef(void *data
, struct name
*name
, unsigned flags
)
1190 if (flags
& F_STATIC
) {
1191 globaldef(data
, name
, flags
);
1194 name
->addr
= o_mklocal(type_totsz(&name
->type
));
1196 if (flags
& F_INIT
) {
1197 struct type
*t
= &name
->type
;
1198 if (tok_see() == '{') {
1199 o_local(name
->addr
, TYPE_BT(t
));
1200 o_memset(0, type_totsz(t
));
1203 initexpr(t
, name
->addr
, 0);
1207 static void funcdef(char *name
, struct type
*type
, struct name
*args
,
1208 int nargs
, unsigned flags
)
1212 strcpy(global
.name
, name
);
1213 memcpy(&global
.type
, type
, sizeof(*type
));
1214 global
.addr
= o_func_beg(name
, F_GLOBAL(flags
));
1215 global_add(&global
);
1216 ret_bt
= TYPE_BT(&funcs
[type
->id
].ret
);
1217 for (i
= 0; i
< nargs
; i
++) {
1218 args
[i
].addr
= o_arg(i
, type_totsz(&args
[i
].type
));
1219 local_add(&args
[i
]);
1223 static int readargs(struct name
*args
)
1227 while (tok_see() != ')') {
1228 readname(&args
[nargs
].type
, args
[nargs
].name
, NULL
, 0);
1234 if (nargs
== 1 && !TYPE_BT(&args
[0].type
))
1239 static int readname(struct type
*main
, char *name
,
1240 struct type
*base
, unsigned flags
)
1242 struct type tpool
[3];
1244 struct type
*type
= &tpool
[npool
++];
1245 struct type
*func
= NULL
;
1246 struct type
*ret
= NULL
;
1247 memset(tpool
, 0, sizeof(tpool
));
1251 if (basetype(type
, &flags
))
1254 memcpy(type
, base
, sizeof(*base
));
1257 if (!tok_jmp('(')) {
1259 type
= &tpool
[npool
++];
1263 if (!tok_jmp(TOK_NAME
) && name
)
1264 strcpy(name
, tok_id());
1265 while (!tok_jmp('[')) {
1270 die("const expr expected\n");
1271 type
->id
= array_add(type
, n
);
1272 if (type
->flags
& T_FUNC
)
1273 func
= &arrays
[type
->id
].type
;
1274 type
->flags
= T_ARRAY
;
1281 if (tok_see() == '(') {
1282 struct name args
[MAXARGS
];
1283 int nargs
= readargs(args
);
1287 type
= &tpool
[npool
++];
1290 func
->flags
= T_FUNC
;
1292 func
->id
= func_create(ret
, args
, nargs
);
1293 if (fdef
&& tok_see() == '{') {
1294 funcdef(name
, func
, args
, nargs
, flags
);
1298 memcpy(main
, type
, sizeof(*type
));
1302 static int readdefs(void (*def
)(void *data
, struct name
*name
, unsigned flags
),
1307 if (basetype(&base
, &flags
))
1309 while (tok_see() != ';' && tok_see() != '{') {
1311 if (readname(&name
.type
, name
.name
, &base
, flags
))
1315 def(data
, &name
, flags
);
1321 static void typedefdef(void *data
, struct name
*name
, unsigned flags
)
1323 typedef_add(name
->name
, &name
->type
);
1326 static void readstmt(void);
1328 #define MAXCASES (1 << 7)
1330 static void readswitch(void)
1332 int break_beg
= nbreaks
;
1333 long val_addr
= o_mklocal(8);
1334 long matched
[MAXCASES
];
1343 o_local(val_addr
, TYPE_BT(&t
));
1345 o_assign(TYPE_BT(&t
));
1349 while (tok_jmp('}')) {
1351 while (tok_see() == TOK_CASE
|| tok_see() == TOK_DEFAULT
) {
1353 matched
[nmatched
++] = o_jmp(0);
1356 if (!tok_jmp(TOK_CASE
)) {
1358 o_local(val_addr
, TYPE_BT(&t
));
1367 if (!tok_jmp(TOK_DEFAULT
)) {
1372 for (i
= 0; i
< nmatched
; i
++)
1373 o_filljmp(matched
[i
]);
1377 o_rmlocal(val_addr
, 8);
1380 break_fill(o_mklabel(), break_beg
);
1383 #define MAXGOTO (1 << 10)
1385 static struct gotoinfo
{
1391 static struct labelinfo
{
1397 static void goto_add(char *name
)
1399 strcpy(gotos
[ngotos
].name
, name
);
1400 gotos
[ngotos
++].addr
= o_jmp(0);
1403 static void label_add(char *name
)
1405 strcpy(labels
[nlabels
].name
, name
);
1406 labels
[nlabels
++].addr
= o_mklabel();
1409 static void goto_fill(void)
1412 for (i
= 0; i
< ngotos
; i
++)
1413 for (j
= 0; j
< nlabels
; j
++)
1414 if (!strcmp(gotos
[i
].name
, labels
[j
].name
)) {
1415 o_filljmp2(gotos
[i
].addr
, labels
[j
].addr
);
1420 static void readstmt(void)
1424 if (!tok_jmp('{')) {
1425 int _nlocals
= nlocals
;
1426 int _nglobals
= nglobals
;
1427 int _nenums
= nenums
;
1428 int _ntypedefs
= ntypedefs
;
1429 int _nstructs
= nstructs
;
1430 int _nfuncs
= nfuncs
;
1431 int _narrays
= narrays
;
1432 while (tok_jmp('}'))
1436 ntypedefs
= _ntypedefs
;
1437 nstructs
= _nstructs
;
1440 nglobals
= _nglobals
;
1443 if (!readdefs(localdef
, NULL
)) {
1447 if (!tok_jmp(TOK_TYPEDEF
)) {
1448 readdefs(typedefdef
, NULL
);
1452 if (!tok_jmp(TOK_IF
)) {
1459 if (!tok_jmp(TOK_ELSE
)) {
1469 if (!tok_jmp(TOK_WHILE
)) {
1471 int break_beg
= nbreaks
;
1472 int continue_beg
= ncontinues
;
1481 break_fill(o_mklabel(), break_beg
);
1482 continue_fill(l1
, continue_beg
);
1485 if (!tok_jmp(TOK_DO
)) {
1487 int break_beg
= nbreaks
;
1488 int continue_beg
= ncontinues
;
1491 tok_expect(TOK_WHILE
);
1497 break_fill(o_mklabel(), break_beg
);
1498 continue_fill(l2
, continue_beg
);
1501 if (!tok_jmp(TOK_FOR
)) {
1502 long check
, jump
, end
, body
;
1503 int break_beg
= nbreaks
;
1504 int continue_beg
= ncontinues
;
1506 if (tok_see() != ';')
1509 check
= o_mklabel();
1510 if (tok_see() != ';')
1516 if (tok_see() != ')')
1524 break_fill(o_mklabel(), break_beg
);
1525 continue_fill(jump
, continue_beg
);
1528 if (!tok_jmp(TOK_SWITCH
)) {
1532 if (!tok_jmp(TOK_RETURN
)) {
1533 int ret
= tok_see() != ';';
1540 if (!tok_jmp(TOK_BREAK
)) {
1542 breaks
[nbreaks
++] = o_jmp(0);
1545 if (!tok_jmp(TOK_CONTINUE
)) {
1547 continues
[ncontinues
++] = o_jmp(0);
1550 if (!tok_jmp(TOK_GOTO
)) {
1551 tok_expect(TOK_NAME
);
1558 if (!tok_jmp(':')) {
1559 label_add(tok_id());
1565 static void readdecl(void)
1567 if (!tok_jmp(TOK_TYPEDEF
)) {
1568 readdefs(typedefdef
, NULL
);
1572 readdefs(globaldef
, NULL
);
1573 if (tok_see() == '{') {
1585 static void parse(void)
1587 while (tok_see() != TOK_EOF
)
1591 int main(int argc
, char *argv
[])
1596 while (i
< argc
&& argv
[i
][0] == '-')
1599 die("no file given\n");
1600 ifd
= open(argv
[i
], O_RDONLY
);
1605 strcpy(obj
, argv
[i
]);
1606 obj
[strlen(obj
) - 1] = 'o';
1607 ofd
= open(obj
, O_WRONLY
| O_TRUNC
| O_CREAT
, 0600);