-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathbody.c
419 lines (369 loc) · 8.5 KB
/
body.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
#include <stdio.h>
#include "compiler.h"
void statement_block(unsigned brack);
static void statement(void);
static unsigned next_tag;
static unsigned func_tag;
static unsigned break_tag;
static unsigned cont_tag;
static unsigned switch_tag;
static unsigned switch_count;
static unsigned switch_type;
static unsigned switch_default;
static unsigned func_type;
unsigned func_flags;
unsigned arg_flags;
unsigned func_argframe;
/* C keyword statements */
static void if_statement(void)
{
struct node *n;
unsigned tag = next_tag++;
unsigned t;
next_token();
n = logic_expression(&t);
header(H_IF, tag, t);
write_logic_tree(n, t);
statement_block(0);
if (token == T_ELSE) {
next_token();
header(H_ELSE, tag, t);
statement_block(0);
footer(H_IF, tag, 1);
} else
footer(H_IF, tag, 0);
}
static void while_statement(void)
{
unsigned oldbrk = break_tag;
unsigned oldcont = cont_tag;
struct node *n;
unsigned t;
break_tag = next_tag++;
cont_tag = break_tag;
next_token();
n = logic_expression(&t);
header(H_WHILE, cont_tag, t);
write_logic_tree(n, t);
statement_block(0);
footer(H_WHILE, cont_tag, t);
break_tag = oldbrk;
cont_tag = oldcont;
}
static void do_statement(void)
{
struct node *n;
unsigned oldbrk = break_tag;
unsigned oldcont = cont_tag;
unsigned t;
break_tag = next_tag++;
cont_tag = break_tag;
next_token();
header(H_DO, cont_tag, 0);
statement_block(0);
require(T_WHILE);
n = logic_expression(&t);
header(H_DOWHILE, cont_tag, t);
require(T_SEMICOLON);
write_logic_tree(n, t);
footer(H_DOWHILE, cont_tag, t);
break_tag = oldbrk;
cont_tag = oldcont;
}
/* TODO: optimize the cases where the for loop condition is 0 or 1 */
static void for_statement(void)
{
unsigned oldbrk = break_tag;
unsigned oldcont = cont_tag;
break_tag = next_tag++;
cont_tag = break_tag;
next_token();
header(H_FOR, cont_tag, break_tag);
require(T_LPAREN);
expression_or_null(0, NORETURN);
require(T_SEMICOLON);
expression_or_null(1, CCONLY);
require(T_SEMICOLON);
expression_or_null(0, NORETURN);
require(T_RPAREN);
statement_block(0);
footer(H_FOR, cont_tag, break_tag);
break_tag = oldbrk;
cont_tag = oldcont;
}
static void return_statement(void)
{
next_token();
header(H_RETURN, func_tag, 0);
expression_typed(func_type);
footer(H_RETURN, func_tag, 0);
}
static void break_statement(void)
{
next_token();
if (break_tag == 0)
error("break outside of block");
header(H_BREAK, break_tag, 0);
}
static void continue_statement(void)
{
next_token();
if (cont_tag == 0)
error("continue outside of block");
header(H_CONTINUE, cont_tag, 0);
}
static void switch_statement(void)
{
unsigned oldbrk = break_tag;
unsigned oldswt = switch_tag;
unsigned oldswc = switch_count;
unsigned oldswtype = switch_type;
unsigned olddefault = switch_default;
unsigned long *swptr;
switch_tag = next_tag++;
break_tag = next_tag++;
switch_count = 0;
next_token();
header(H_SWITCH, switch_tag, break_tag);
switch_type = bracketed_expression(0);
/* Only integral types */
if (!IS_INTARITH(switch_type)) {
error("bad type");
switch_type = CINT;
}
swptr = switch_alloc();
statement_block(0);
footer(H_SWITCH, switch_tag, break_tag);
/* No default means non matched cases fall through to the end */
if (!switch_default)
header(H_DEFAULT, switch_tag, 0);
switch_done(switch_tag, swptr, switch_type);
switch_type = oldswtype;
break_tag = oldbrk;
switch_tag = oldswt;
switch_count = oldswc;
switch_default = olddefault;
}
static void case_statement(void)
{
struct node *n;
if (switch_tag == 0)
error("case outside of switch");
next_token();
/* FIXME: type check range... */
n = expression_tree(0);
if (!is_constant(n))
notconst();
else
switch_add_node(n->value);
free_tree(n);
header(H_CASE, switch_tag, ++switch_count);
require(T_COLON);
}
static void default_statement(void)
{
if (switch_tag == 0)
error("default outside of switch");
if (switch_default)
error("two default cases");
switch_default = 1;
header(H_DEFAULT, switch_tag, 0);
next_token();
require(T_COLON);
}
static void goto_statement(void)
{
unsigned n;
next_token();
if ((n = symname()) == 0)
error("label required");
/* We will work out if the label existed later */
use_label(n);
header(H_GOTO, func_tag, n);
}
/*
* C statements.
*
* This can be a declaration, in which case it starts with a token that
* describes storage properties, a keyword, a name followed by a colon
* (which is a label), or an expression. A null expression is also allowed.
*/
static void statement(void)
{
/* It's valid to have a {} block */
if (token == T_RCURLY)
return;
#if 0 /* C99 for later if we want it */
declaration_block();
#endif
/* Check for keywords */
switch (token) {
case T_IF:
if_statement();
return;
case T_WHILE:
while_statement();
return;
case T_SWITCH:
switch_statement();
return;
case T_DO:
do_statement();
return;
case T_FOR:
for_statement();
return;
case T_RETURN:
return_statement();
break;
case T_BREAK:
break_statement();
break;
case T_CONTINUE:
continue_statement();
break;
case T_GOTO:
goto_statement();
break;
case T_CASE:
case_statement();
return;
case T_DEFAULT:
default_statement();
return;
case T_SEMICOLON:
next_token();
return;
default:
/* It is valid to follow a label with just ; */
if (token != T_SEMICOLON) {
struct node *n = expression_tree(1);
/* A statement top node need not worry about
generating the correct result */
n->flags |= NORETURN;
write_tree(n);
}
break;
}
require(T_SEMICOLON);
}
static void declaration_block(void)
{
while (is_modifier() || is_storage_word() || is_type_word() ||
is_typedef()) {
declaration(S_AUTO);
}
}
/*
* Either a statement or a sequence of statements enclosed in { }. In
* some cases the sequence is mandatory (eg a function) so we pass in
* need_brack to tell us what to do.
*/
void statement_block(unsigned need_brack)
{
struct symbol *ltop;
if (token == T_EOF) {
fatal("unexpected EOF");
return;
}
/* We could write this not to push back a token but it's
actually much cleaner to push back */
while (token >= T_SYMBOL) {
unsigned name = token;
next_token();
if (token == T_COLON) {
next_token();
/* We found a label */
add_label(name);
header(H_LABEL, func_tag, name);
} else {
push_token(name);
break;
}
}
if (token != T_LCURLY) {
if (need_brack)
require(T_LCURLY);
statement();
return;
}
next_token();
ltop = mark_local_symbols();
/* declarations */
declaration_block();
while (token != T_RCURLY) {
/* statements */
statement_block(0);
}
pop_local_symbols(ltop);
next_token();
}
static void load_registers(void)
{
register struct node *n;
register unsigned i = 1;
unsigned t;
while(i <= NUM_REG) {
if (reg_load[i]) {
t = reg_load[i]->type;
/* Expression tree to load the register */
/* EQ (T_REG:i, T_DEREF(T_ARGUMENT:offset)) */
/* Hand build a symbol less reference to an argument */
n = new_node();
n->op = T_ARGUMENT;
n->value = reg_offset[i];
n->type = t;
n = tree(T_EQ, make_symbol(reg_load[i]), tree(T_DEREF,NULL, n));
/* Set the assignment to the type of the symbol */
n->type = t;
/* And we don't care about the return value after assignment */
n->flags |= NORETURN|SIDEEFFECT;
write_tree(n);
}
i++;
}
}
/*
* We have parsed the declaration part of a function and found it
* is followed by a body. Set up the headersfor the backend and turn
* the contents into expressions and headers.
*/
void function_body(unsigned st, unsigned name, unsigned type)
{
/* This makes me sad, but there isn't a nice way to work out
the frame size ahead of time */
unsigned long hrw;
register unsigned *p;
register unsigned n;
/* We are using both the ones allocated for locals and those registers */
func_flags = arg_flags & F_REGMASK;
/* Pass useful information flags to the backend */
func_type = func_return(type);
if (func_type == VOID)
func_flags |= F_VOIDRET;
p = func_args(type);
n = *p++;
if (n == 1 && *p == VOID)
func_flags |= F_VOID;
while(n--) {
if (*p++ == ELLIPSIS) {
func_flags |= F_VARARG;
break;
}
}
if (st == S_AUTO || st == S_EXTERN)
error("invalid storage class");
func_tag = next_tag++;
header(H_FUNCTION, func_tag, name);
header(H_ARGFRAME, func_argframe, 0);
hrw = mark_header();
header(H_FRAME, 0, 0);
/* Register arguments need loading into registers */
if (arg_flags)
load_registers();
init_labels();
statement_block(1);
footer(H_FUNCTION, func_tag, name);
rewrite_header(hrw, H_FRAME, frame_size(), func_flags);
check_labels();
}