Don't kill the same pseudo twice.
[smatch.git] / inline.c
blob0e7927a6ce17d4d4ff397a96ed601fdd60529f8d
1 /*
2 * Sparse - a semantic source parser.
4 * Copyright (C) 2003 Transmeta Corp.
5 * 2003-2004 Linus Torvalds
7 * Licensed under the Open Software License version 1.1
8 */
10 #include <stdlib.h>
11 #include <stdio.h>
13 #include "lib.h"
14 #include "allocate.h"
15 #include "token.h"
16 #include "parse.h"
17 #include "symbol.h"
18 #include "expression.h"
20 static struct expression * dup_expression(struct expression *expr)
22 struct expression *dup = alloc_expression(expr->pos, expr->type);
23 *dup = *expr;
24 return dup;
27 static struct statement * dup_statement(struct statement *stmt)
29 struct statement *dup = alloc_statement(stmt->pos, stmt->type);
30 *dup = *stmt;
31 return dup;
34 static struct symbol *copy_symbol(struct position pos, struct symbol *sym)
36 if (!sym)
37 return sym;
38 if (sym->ctype.modifiers & (MOD_STATIC | MOD_EXTERN | MOD_TOPLEVEL | MOD_INLINE))
39 return sym;
40 if (!sym->replace) {
41 warning(pos, "unreplaced symbol '%s'", show_ident(sym->ident));
42 return sym;
44 return sym->replace;
47 static struct symbol_list *copy_symbol_list(struct symbol_list *src)
49 struct symbol_list *dst = NULL;
50 struct symbol *sym;
52 FOR_EACH_PTR(src, sym) {
53 struct symbol *newsym = copy_symbol(sym->pos, sym);
54 add_symbol(&dst, newsym);
55 } END_FOR_EACH_PTR(sym);
56 return dst;
59 static struct expression * copy_expression(struct expression *expr)
61 if (!expr)
62 return NULL;
64 switch (expr->type) {
66 * EXPR_SYMBOL is the interesting case, we may need to replace the
67 * symbol to the new copy.
69 case EXPR_SYMBOL: {
70 struct symbol *sym = copy_symbol(expr->pos, expr->symbol);
71 if (sym == expr->symbol)
72 break;
73 expr = dup_expression(expr);
74 expr->symbol = sym;
75 break;
78 /* Atomics, never change, just return the expression directly */
79 case EXPR_VALUE:
80 case EXPR_STRING:
81 case EXPR_FVALUE:
82 break;
84 /* Unops: check if the subexpression is unique */
85 case EXPR_PREOP:
86 case EXPR_POSTOP: {
87 struct expression *unop = copy_expression(expr->unop);
88 if (expr->unop == unop)
89 break;
90 expr = dup_expression(expr);
91 expr->unop = unop;
92 break;
95 case EXPR_SLICE: {
96 struct expression *base = copy_expression(expr->base);
97 expr = dup_expression(expr);
98 expr->base = base;
99 break;
102 /* Binops: copy left/right expressions */
103 case EXPR_BINOP:
104 case EXPR_COMMA:
105 case EXPR_COMPARE:
106 case EXPR_LOGICAL: {
107 struct expression *left = copy_expression(expr->left);
108 struct expression *right = copy_expression(expr->right);
109 if (left == expr->left && right == expr->right)
110 break;
111 expr = dup_expression(expr);
112 expr->left = left;
113 expr->right = right;
114 break;
117 case EXPR_ASSIGNMENT: {
118 struct expression *left = copy_expression(expr->left);
119 struct expression *right = copy_expression(expr->right);
120 if (expr->op == '=' && left == expr->left && right == expr->right)
121 break;
122 expr = dup_expression(expr);
123 expr->left = left;
124 expr->right = right;
125 break;
128 /* Dereference */
129 case EXPR_DEREF: {
130 struct expression *deref = copy_expression(expr->deref);
131 expr = dup_expression(expr);
132 expr->deref = deref;
133 break;
136 /* Cast/sizeof/__alignof__ */
137 case EXPR_CAST:
138 case EXPR_IMPLIED_CAST:
139 case EXPR_SIZEOF:
140 case EXPR_PTRSIZEOF:
141 case EXPR_ALIGNOF: {
142 struct expression *cast = copy_expression(expr->cast_expression);
143 if (cast == expr->cast_expression)
144 break;
145 expr = dup_expression(expr);
146 expr->cast_expression = cast;
147 break;
150 /* Conditional expression */
151 case EXPR_SELECT:
152 case EXPR_CONDITIONAL: {
153 struct expression *cond = copy_expression(expr->conditional);
154 struct expression *true = copy_expression(expr->cond_true);
155 struct expression *false = copy_expression(expr->cond_false);
156 if (cond == expr->conditional && true == expr->cond_true && false == expr->cond_false)
157 break;
158 expr = dup_expression(expr);
159 expr->conditional = cond;
160 expr->cond_true = true;
161 expr->cond_false = false;
162 break;
165 /* Statement expression */
166 case EXPR_STATEMENT: {
167 struct statement *stmt = alloc_statement(expr->pos, STMT_COMPOUND);
168 copy_statement(expr->statement, stmt);
169 expr = dup_expression(expr);
170 expr->statement = stmt;
171 break;
174 /* Call expression */
175 case EXPR_CALL: {
176 struct expression *fn = copy_expression(expr->fn);
177 struct expression_list *list = expr->args;
178 struct expression *arg;
180 expr = dup_expression(expr);
181 expr->fn = fn;
182 expr->args = NULL;
183 FOR_EACH_PTR(list, arg) {
184 add_expression(&expr->args, copy_expression(arg));
185 } END_FOR_EACH_PTR(arg);
186 break;
189 /* Initializer list statement */
190 case EXPR_INITIALIZER: {
191 struct expression_list *list = expr->expr_list;
192 struct expression *entry;
193 expr = dup_expression(expr);
194 expr->expr_list = NULL;
195 FOR_EACH_PTR(list, entry) {
196 add_expression(&expr->expr_list, copy_expression(entry));
197 } END_FOR_EACH_PTR(entry);
198 break;
201 /* Label in inline function - hmm. */
202 case EXPR_LABEL: {
203 struct symbol *label_symbol = copy_symbol(expr->pos, expr->label_symbol);
204 expr = dup_expression(expr);
205 expr->label_symbol = label_symbol;
206 break;
209 case EXPR_INDEX: {
210 struct expression *sub_expr = copy_expression(expr->idx_expression);
211 if (sub_expr == expr->idx_expression)
212 break;
213 expr = dup_expression(expr);
214 expr->idx_expression = sub_expr;
215 break;
218 case EXPR_IDENTIFIER: {
219 struct expression *sub_expr = copy_expression(expr->ident_expression);
220 if (sub_expr == expr->ident_expression)
221 break;
222 expr = dup_expression(expr);
223 expr->ident_expression = sub_expr;
224 break;
227 /* Position in initializer.. */
228 case EXPR_POS: {
229 struct expression *val = copy_expression(expr->init_expr);
230 if (val == expr->init_expr)
231 break;
232 expr = dup_expression(expr);
233 expr->init_expr = val;
234 break;
237 default:
238 warning(expr->pos, "trying to copy expression type %d", expr->type);
240 return expr;
243 static struct expression_list *copy_expression_list(struct expression_list *in)
245 struct expression_list *out = NULL;
246 struct expression *expr;
248 FOR_EACH_PTR(in, expr) {
249 add_expression(&out, copy_expression(expr));
250 } END_FOR_EACH_PTR(expr);
251 return out;
254 void set_replace(struct symbol *old, struct symbol *new)
256 new->replace = old;
257 old->replace = new;
260 void unset_replace(struct symbol *sym)
262 struct symbol *r = sym->replace;
263 if (!r) {
264 warning(sym->pos, "symbol '%s' not replaced?", show_ident(sym->ident));
265 return;
267 r->replace = NULL;
268 sym->replace = NULL;
271 static void unset_replace_list(struct symbol_list *list)
273 struct symbol *sym;
274 FOR_EACH_PTR(list, sym) {
275 unset_replace(sym);
276 } END_FOR_EACH_PTR(sym);
279 static struct statement *copy_one_statement(struct statement *stmt)
281 if (!stmt)
282 return NULL;
283 switch(stmt->type) {
284 case STMT_NONE:
285 break;
286 case STMT_INTERNAL:
287 case STMT_EXPRESSION: {
288 struct expression *expr = copy_expression(stmt->expression);
289 if (expr == stmt->expression)
290 break;
291 stmt = dup_statement(stmt);
292 stmt->expression = expr;
293 break;
295 case STMT_COMPOUND: {
296 struct statement *new = alloc_statement(stmt->pos, STMT_COMPOUND);
297 copy_statement(stmt, new);
298 stmt = new;
299 break;
301 case STMT_IF: {
302 struct expression *cond = stmt->if_conditional;
303 struct statement *true = stmt->if_true;
304 struct statement *false = stmt->if_false;
306 cond = copy_expression(cond);
307 true = copy_one_statement(true);
308 false = copy_one_statement(false);
309 if (stmt->if_conditional == cond &&
310 stmt->if_true == true &&
311 stmt->if_false == false)
312 break;
313 stmt = dup_statement(stmt);
314 stmt->if_conditional = cond;
315 stmt->if_true = true;
316 stmt->if_false = false;
317 break;
319 case STMT_RETURN: {
320 struct expression *retval = copy_expression(stmt->ret_value);
321 struct symbol *sym = copy_symbol(stmt->pos, stmt->ret_target);
323 stmt = dup_statement(stmt);
324 stmt->ret_value = retval;
325 stmt->ret_target = sym;
326 break;
328 case STMT_CASE: {
329 stmt = dup_statement(stmt);
330 stmt->case_label = copy_symbol(stmt->pos, stmt->case_label);
331 stmt->case_expression = copy_expression(stmt->case_expression);
332 stmt->case_to = copy_expression(stmt->case_to);
333 stmt->case_statement = copy_one_statement(stmt->case_statement);
334 break;
336 case STMT_SWITCH: {
337 struct symbol *switch_break = copy_symbol(stmt->pos, stmt->switch_break);
338 struct symbol *switch_case = copy_symbol(stmt->pos, stmt->switch_case);
339 struct expression *expr = copy_expression(stmt->switch_expression);
340 struct statement *switch_stmt = copy_one_statement(stmt->switch_statement);
342 stmt = dup_statement(stmt);
343 switch_case->symbol_list = copy_symbol_list(switch_case->symbol_list);
344 stmt->switch_break = switch_break;
345 stmt->switch_case = switch_case;
346 stmt->switch_expression = expr;
347 stmt->switch_statement = switch_stmt;
348 break;
350 case STMT_ITERATOR: {
351 stmt = dup_statement(stmt);
352 stmt->iterator_break = copy_symbol(stmt->pos, stmt->iterator_break);
353 stmt->iterator_continue = copy_symbol(stmt->pos, stmt->iterator_continue);
354 stmt->iterator_syms = copy_symbol_list(stmt->iterator_syms);
356 stmt->iterator_pre_statement = copy_one_statement(stmt->iterator_pre_statement);
357 stmt->iterator_pre_condition = copy_expression(stmt->iterator_pre_condition);
359 stmt->iterator_statement = copy_one_statement(stmt->iterator_statement);
361 stmt->iterator_post_statement = copy_one_statement(stmt->iterator_post_statement);
362 stmt->iterator_post_condition = copy_expression(stmt->iterator_post_condition);
363 break;
365 case STMT_LABEL: {
366 stmt = dup_statement(stmt);
367 stmt->label_identifier = copy_symbol(stmt->pos, stmt->label_identifier);
368 stmt->label_statement = copy_one_statement(stmt->label_statement);
369 break;
371 case STMT_GOTO: {
372 stmt = dup_statement(stmt);
373 stmt->goto_label = copy_symbol(stmt->pos, stmt->goto_label);
374 stmt->goto_expression = copy_expression(stmt->goto_expression);
375 stmt->target_list = copy_symbol_list(stmt->target_list);
376 break;
378 case STMT_ASM: {
379 stmt = dup_statement(stmt);
380 stmt->asm_inputs = copy_expression_list(stmt->asm_inputs);
381 stmt->asm_outputs = copy_expression_list(stmt->asm_outputs);
382 /* no need to dup "clobbers", since they are all constant strings */
383 break;
385 default:
386 warning(stmt->pos, "trying to copy statement type %d", stmt->type);
387 break;
389 return stmt;
393 * Copy a stateemnt tree from 'src' to 'dst', where both
394 * source and destination are of type STMT_COMPOUND.
396 * We do this for the tree-level inliner.
398 * This doesn't do the symbol replacement right: it's not
399 * re-entrant.
401 void copy_statement(struct statement *src, struct statement *dst)
403 struct statement *stmt;
404 struct symbol *sym;
406 FOR_EACH_PTR(src->syms, sym) {
407 struct symbol *newsym = copy_symbol(src->pos, sym);
408 newsym->initializer = copy_expression(sym->initializer);
409 add_symbol(&dst->syms, newsym);
410 } END_FOR_EACH_PTR(sym);
412 FOR_EACH_PTR(src->stmts, stmt) {
413 add_statement(&dst->stmts, copy_one_statement(stmt));
414 } END_FOR_EACH_PTR(stmt);
416 dst->ret = copy_symbol(src->pos, src->ret);
419 static struct symbol *create_copy_symbol(struct symbol *orig)
421 struct symbol *sym = orig;
422 if (orig) {
423 sym = alloc_symbol(orig->pos, orig->type);
424 *sym = *orig;
425 sym->bb_target = NULL;
426 sym->pseudo = NULL;
427 set_replace(orig, sym);
428 orig = sym;
430 return orig;
433 static struct symbol_list *create_symbol_list(struct symbol_list *src)
435 struct symbol_list *dst = NULL;
436 struct symbol *sym;
438 FOR_EACH_PTR(src, sym) {
439 struct symbol *newsym = create_copy_symbol(sym);
440 add_symbol(&dst, newsym);
441 } END_FOR_EACH_PTR(sym);
442 return dst;
445 int inline_function(struct expression *expr, struct symbol *sym)
447 struct symbol_list * fn_symbol_list;
448 struct symbol *fn = sym->ctype.base_type;
449 struct expression_list *arg_list = expr->args;
450 struct statement *stmt = alloc_statement(expr->pos, STMT_COMPOUND);
451 struct symbol_list *name_list;
452 struct symbol *name;
453 struct expression *arg;
455 if (!fn->inline_stmt) {
456 warning(fn->pos, "marked inline, but without a definition");
457 return 0;
459 if (fn->expanding)
460 return 0;
461 fn->expanding = 1;
463 name_list = fn->arguments;
465 expr->type = EXPR_STATEMENT;
466 expr->statement = stmt;
467 expr->ctype = fn->ctype.base_type;
469 fn_symbol_list = create_symbol_list(sym->inline_symbol_list);
471 PREPARE_PTR_LIST(name_list, name);
472 FOR_EACH_PTR(arg_list, arg) {
473 struct symbol *a = alloc_symbol(arg->pos, SYM_NODE);
475 a->ctype.base_type = arg->ctype;
476 if (name) {
477 *a = *name;
478 set_replace(name, a);
479 add_symbol(&fn_symbol_list, a);
481 a->initializer = arg;
482 add_symbol(&stmt->syms, a);
484 NEXT_PTR_LIST(name);
485 } END_FOR_EACH_PTR(arg);
486 FINISH_PTR_LIST(name);
488 copy_statement(fn->inline_stmt, stmt);
490 unset_replace_list(fn_symbol_list);
492 evaluate_statement(stmt);
494 fn->expanding = 0;
495 return 1;
498 void uninline(struct symbol *sym)
500 struct symbol *fn = sym->ctype.base_type;
501 struct symbol_list *arg_list = fn->arguments;
502 struct symbol *p;
504 sym->symbol_list = create_symbol_list(sym->inline_symbol_list);
505 FOR_EACH_PTR(arg_list, p) {
506 p->replace = p;
507 } END_FOR_EACH_PTR(p);
508 fn->stmt = alloc_statement(fn->pos, STMT_COMPOUND);
509 copy_statement(fn->inline_stmt, fn->stmt);
510 unset_replace_list(sym->symbol_list);
511 unset_replace_list(arg_list);