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
|
/*
* Sparse - a semantic source parser.
*
* Copyright (C) 2003 Transmeta Corp.
* 2003 Linus Torvalds
*
* Licensed under the Open Software License version 1.1
*/
#include <stdlib.h>
#include <stdio.h>
#include "lib.h"
#include "token.h"
#include "parse.h"
#include "symbol.h"
#include "expression.h"
static struct expression * dup_expression(struct expression *expr)
{
struct expression *dup = alloc_expression(expr->pos, expr->type);
*dup = *expr;
return dup;
}
static struct statement * dup_statement(struct statement *stmt)
{
struct statement *dup = alloc_statement(stmt->pos, stmt->type);
*dup = *stmt;
return dup;
}
static struct symbol *copy_symbol(struct position pos, struct symbol *sym)
{
if (!sym)
return sym;
if (sym->ctype.modifiers & (MOD_STATIC | MOD_EXTERN | MOD_TOPLEVEL | MOD_INLINE))
return sym;
if (!sym->replace) {
warn(pos, "unreplaced symbol '%s'", show_ident(sym->ident));
return sym;
}
return sym->replace;
}
static struct symbol_list *copy_symbol_list(struct symbol_list *src)
{
struct symbol_list *dst = NULL;
struct symbol *sym;
FOR_EACH_PTR(src, sym) {
struct symbol *newsym = copy_symbol(sym->pos, sym);
add_symbol(&dst, newsym);
} END_FOR_EACH_PTR;
return dst;
}
static struct expression * copy_expression(struct expression *expr)
{
if (!expr)
return NULL;
switch (expr->type) {
/*
* EXPR_SYMBOL is the interesting case, we may need to replace the
* symbol to the new copy.
*/
case EXPR_SYMBOL: {
struct symbol *sym = copy_symbol(expr->pos, expr->symbol);
if (sym == expr->symbol)
break;
expr = dup_expression(expr);
expr->symbol = sym;
break;
}
/* Atomics, never change, just return the expression directly */
case EXPR_VALUE:
case EXPR_STRING:
break;
/* Unops: check if the subexpression is unique */
case EXPR_PREOP:
case EXPR_POSTOP: {
struct expression *unop = copy_expression(expr->unop);
if (expr->unop == unop)
break;
expr = dup_expression(expr);
expr->unop = unop;
break;
}
/* Binops: copy left/right expressions */
case EXPR_BINOP:
case EXPR_COMMA:
case EXPR_COMPARE:
case EXPR_LOGICAL:
case EXPR_ASSIGNMENT: {
struct expression *left = copy_expression(expr->left);
struct expression *right = copy_expression(expr->right);
if (left == expr->left && right == expr->right)
break;
expr = dup_expression(expr);
expr->left = left;
expr->right = right;
break;
}
/* Dereference */
case EXPR_DEREF: {
struct expression *deref = copy_expression(expr->deref);
if (deref == expr->deref)
break;
expr = dup_expression(expr);
expr->deref = deref;
break;
}
/* Cast/sizeof */
case EXPR_CAST:
case EXPR_SIZEOF: {
struct expression *cast = copy_expression(expr->cast_expression);
if (cast == expr->cast_expression)
break;
expr = dup_expression(expr);
expr->cast_expression = cast;
break;
}
/* Conditional expression */
case EXPR_CONDITIONAL: {
struct expression *cond = copy_expression(expr->conditional);
struct expression *true = copy_expression(expr->cond_true);
struct expression *false = copy_expression(expr->cond_false);
if (cond == expr->conditional && true == expr->cond_true && false == expr->cond_false)
break;
expr = dup_expression(expr);
expr->conditional = cond;
expr->cond_true = true;
expr->cond_false = false;
break;
}
/* Statement expression */
case EXPR_STATEMENT: {
struct statement *stmt = alloc_statement(expr->pos, STMT_COMPOUND);
copy_statement(expr->statement, stmt);
expr = dup_expression(expr);
expr->statement = stmt;
break;
}
/* Call expression */
case EXPR_CALL: {
struct expression *fn = copy_expression(expr->fn);
struct expression_list *list = expr->args;
struct expression *arg;
expr = dup_expression(expr);
expr->fn = fn;
expr->args = NULL;
FOR_EACH_PTR(list, arg) {
add_expression(&expr->args, copy_expression(arg));
} END_FOR_EACH_PTR;
break;
}
/* Initializer list statement */
case EXPR_INITIALIZER: {
struct expression_list *list = expr->expr_list;
struct expression *entry;
expr = dup_expression(expr);
expr->expr_list = NULL;
FOR_EACH_PTR(list, entry) {
add_expression(&expr->expr_list, copy_expression(entry));
} END_FOR_EACH_PTR;
break;
}
default:
if (verbose)
warn(expr->pos, "trying to copy expression type %d", expr->type);
}
return expr;
}
void set_replace(struct symbol *old, struct symbol *new)
{
new->replace = old;
old->replace = new;
}
void unset_replace(struct symbol *sym)
{
struct symbol *r = sym->replace;
if (!r) {
warn(sym->pos, "symbol '%s' not replaced?", show_ident(sym->ident));
return;
}
r->replace = NULL;
sym->replace = NULL;
}
static void unset_replace_list(struct symbol_list *list)
{
struct symbol *sym;
FOR_EACH_PTR(list, sym) {
unset_replace(sym);
} END_FOR_EACH_PTR;
}
static struct statement *copy_one_statement(struct statement *stmt)
{
if (!stmt)
return NULL;
switch(stmt->type) {
case STMT_NONE:
break;
case STMT_EXPRESSION: {
struct expression *expr = copy_expression(stmt->expression);
if (expr == stmt->expression)
break;
stmt = dup_statement(stmt);
stmt->expression = expr;
break;
}
case STMT_COMPOUND: {
struct statement *new = alloc_statement(stmt->pos, STMT_COMPOUND);
copy_statement(stmt, new);
stmt = new;
break;
}
case STMT_IF: {
struct expression *cond = stmt->if_conditional;
struct statement *true = stmt->if_true;
struct statement *false = stmt->if_false;
cond = copy_expression(cond);
true = copy_one_statement(true);
false = copy_one_statement(false);
if (stmt->if_conditional == cond &&
stmt->if_true == true &&
stmt->if_false == false)
break;
stmt = dup_statement(stmt);
stmt->if_conditional = cond;
stmt->if_true = true;
stmt->if_false = false;
break;
}
case STMT_RETURN: {
struct expression *retval = copy_expression(stmt->ret_value);
struct symbol *sym = copy_symbol(stmt->pos, stmt->ret_target);
stmt = dup_statement(stmt);
stmt->ret_value = retval;
stmt->ret_target = sym;
break;
}
case STMT_CASE: {
stmt = dup_statement(stmt);
stmt->case_label = copy_symbol(stmt->pos, stmt->case_label);
stmt->case_expression = copy_expression(stmt->case_expression);
stmt->case_to = copy_expression(stmt->case_to);
stmt->case_statement = copy_one_statement(stmt->case_statement);
break;
}
case STMT_SWITCH: {
struct symbol *switch_break = copy_symbol(stmt->pos, stmt->switch_break);
struct symbol *switch_case = copy_symbol(stmt->pos, stmt->switch_case);
struct expression *expr = copy_expression(stmt->switch_expression);
struct statement *switch_stmt = copy_one_statement(stmt->switch_statement);
stmt = dup_statement(stmt);
stmt->switch_break = switch_break;
stmt->switch_case = switch_case;
stmt->switch_expression = expr;
stmt->switch_statement = switch_stmt;
break;
}
case STMT_ITERATOR: {
stmt = dup_statement(stmt);
stmt->iterator_break = copy_symbol(stmt->pos, stmt->iterator_break);
stmt->iterator_continue = copy_symbol(stmt->pos, stmt->iterator_continue);
stmt->iterator_syms = copy_symbol_list(stmt->iterator_syms);
stmt->iterator_pre_statement = copy_one_statement(stmt->iterator_pre_statement);
stmt->iterator_pre_condition = copy_expression(stmt->iterator_pre_condition);
stmt->iterator_statement = copy_one_statement(stmt->iterator_statement);
stmt->iterator_post_statement = copy_one_statement(stmt->iterator_post_statement);
stmt->iterator_post_condition = copy_expression(stmt->iterator_post_condition);
break;
}
case STMT_LABEL: {
stmt = dup_statement(stmt);
stmt->label_identifier = copy_symbol(stmt->pos, stmt->label_identifier);
stmt->label_statement = copy_one_statement(stmt->label_statement);
break;
}
case STMT_GOTO: {
/* FIXME! */
break;
}
case STMT_ASM: {
/* FIXME! */
break;
}
default:
if (verbose)
warn(stmt->pos, "trying to copy statement type %d", stmt->type);
break;
}
return stmt;
}
/*
* Copy a stateemnt tree from 'src' to 'dst', where both
* source and destination are of type STMT_COMPOUND.
*
* We do this for the tree-level inliner.
*
* This doesn't do the symbol replacement right: it's not
* re-entrant.
*/
void copy_statement(struct statement *src, struct statement *dst)
{
struct statement *stmt;
struct symbol *sym;
FOR_EACH_PTR(src->syms, sym) {
struct symbol *newsym = copy_symbol(src->pos, sym);
newsym->initializer = copy_expression(sym->initializer);
add_symbol(&dst->syms, newsym);
} END_FOR_EACH_PTR;
FOR_EACH_PTR(src->stmts, stmt) {
add_statement(&dst->stmts, copy_one_statement(stmt));
} END_FOR_EACH_PTR;
dst->ret = copy_symbol(src->pos, src->ret);
}
static struct symbol *create_copy_symbol(struct symbol *orig)
{
struct symbol *sym = orig;
if (orig) {
sym = alloc_symbol(orig->pos, orig->type);
sym->ctype = orig->ctype;
sym->initializer = NULL;
set_replace(orig, sym);
}
return orig;
}
static struct symbol_list *create_symbol_list(struct symbol_list *src)
{
struct symbol_list *dst = NULL;
struct symbol *sym;
FOR_EACH_PTR(src, sym) {
struct symbol *newsym = create_copy_symbol(sym);
add_symbol(&dst, newsym);
} END_FOR_EACH_PTR;
return dst;
}
int inline_function(struct expression *expr, struct symbol *sym)
{
struct symbol_list * fn_symbol_list;
struct symbol *fn = sym->ctype.base_type;
struct expression_list *arg_list = expr->args;
struct statement *stmt = alloc_statement(expr->pos, STMT_COMPOUND);
struct symbol_list *name_list;
struct symbol *name;
struct expression *arg;
if (!fn->stmt) {
warn(fn->pos, "marked inline, but without a definition");
return 0;
}
name_list = fn->arguments;
stmt = alloc_statement(expr->pos, STMT_COMPOUND);
expr->type = EXPR_STATEMENT;
expr->statement = stmt;
expr->ctype = fn->ctype.base_type;
fn_symbol_list = create_symbol_list(sym->symbol_list);
PREPARE_PTR_LIST(name_list, name);
FOR_EACH_PTR(arg_list, arg) {
struct symbol *a = alloc_symbol(arg->pos, SYM_NODE);
a->ctype.base_type = arg->ctype;
if (name) {
a->ident = name->ident;
a->ctype = name->ctype;
set_replace(name, a);
add_symbol(&fn_symbol_list, a);
}
a->initializer = arg;
add_symbol(&stmt->syms, a);
NEXT_PTR_LIST(name);
} END_FOR_EACH_PTR;
FINISH_PTR_LIST(name);
copy_statement(fn->stmt, stmt);
unset_replace_list(fn_symbol_list);
evaluate_statement(stmt);
return 1;
}
|