Make local declarations be statements of their own
[smatch.git] / inline.c
blob1f0172eab69fcef4bf7eeeb296ccb421d0554297
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 if (expr->cast_expression->type == EXPR_INITIALIZER) {
139 struct expression *cast = expr->cast_expression;
140 struct symbol *sym = expr->cast_type;
141 expr = dup_expression(expr);
142 expr->cast_expression = copy_expression(cast);
143 expr->cast_type = alloc_symbol(sym->pos, sym->type);
144 *expr->cast_type = *sym;
145 break;
147 case EXPR_IMPLIED_CAST:
148 case EXPR_SIZEOF:
149 case EXPR_PTRSIZEOF:
150 case EXPR_ALIGNOF: {
151 struct expression *cast = copy_expression(expr->cast_expression);
152 if (cast == expr->cast_expression)
153 break;
154 expr = dup_expression(expr);
155 expr->cast_expression = cast;
156 break;
159 /* Conditional expression */
160 case EXPR_SELECT:
161 case EXPR_CONDITIONAL: {
162 struct expression *cond = copy_expression(expr->conditional);
163 struct expression *true = copy_expression(expr->cond_true);
164 struct expression *false = copy_expression(expr->cond_false);
165 if (cond == expr->conditional && true == expr->cond_true && false == expr->cond_false)
166 break;
167 expr = dup_expression(expr);
168 expr->conditional = cond;
169 expr->cond_true = true;
170 expr->cond_false = false;
171 break;
174 /* Statement expression */
175 case EXPR_STATEMENT: {
176 struct statement *stmt = alloc_statement(expr->pos, STMT_COMPOUND);
177 copy_statement(expr->statement, stmt);
178 expr = dup_expression(expr);
179 expr->statement = stmt;
180 break;
183 /* Call expression */
184 case EXPR_CALL: {
185 struct expression *fn = copy_expression(expr->fn);
186 struct expression_list *list = expr->args;
187 struct expression *arg;
189 expr = dup_expression(expr);
190 expr->fn = fn;
191 expr->args = NULL;
192 FOR_EACH_PTR(list, arg) {
193 add_expression(&expr->args, copy_expression(arg));
194 } END_FOR_EACH_PTR(arg);
195 break;
198 /* Initializer list statement */
199 case EXPR_INITIALIZER: {
200 struct expression_list *list = expr->expr_list;
201 struct expression *entry;
202 expr = dup_expression(expr);
203 expr->expr_list = NULL;
204 FOR_EACH_PTR(list, entry) {
205 add_expression(&expr->expr_list, copy_expression(entry));
206 } END_FOR_EACH_PTR(entry);
207 break;
210 /* Label in inline function - hmm. */
211 case EXPR_LABEL: {
212 struct symbol *label_symbol = copy_symbol(expr->pos, expr->label_symbol);
213 expr = dup_expression(expr);
214 expr->label_symbol = label_symbol;
215 break;
218 case EXPR_INDEX: {
219 struct expression *sub_expr = copy_expression(expr->idx_expression);
220 if (sub_expr == expr->idx_expression)
221 break;
222 expr = dup_expression(expr);
223 expr->idx_expression = sub_expr;
224 break;
227 case EXPR_IDENTIFIER: {
228 struct expression *sub_expr = copy_expression(expr->ident_expression);
229 expr = dup_expression(expr);
230 expr->ident_expression = sub_expr;
231 break;
234 /* Position in initializer.. */
235 case EXPR_POS: {
236 struct expression *val = copy_expression(expr->init_expr);
237 expr = dup_expression(expr);
238 expr->init_expr = val;
239 break;
242 default:
243 warning(expr->pos, "trying to copy expression type %d", expr->type);
245 return expr;
248 static struct expression_list *copy_asm_constraints(struct expression_list *in)
250 struct expression_list *out = NULL;
251 struct expression *expr;
252 int state = 0;
254 FOR_EACH_PTR(in, expr) {
255 switch (state) {
256 case 0: /* identifier */
257 case 1: /* constraint */
258 state++;
259 add_expression(&out, expr);
260 continue;
261 case 2: /* expression */
262 state = 0;
263 add_expression(&out, copy_expression(expr));
264 continue;
266 } END_FOR_EACH_PTR(expr);
267 return out;
270 static void set_replace(struct symbol *old, struct symbol *new)
272 new->replace = old;
273 old->replace = new;
276 static void unset_replace(struct symbol *sym)
278 struct symbol *r = sym->replace;
279 if (!r) {
280 warning(sym->pos, "symbol '%s' not replaced?", show_ident(sym->ident));
281 return;
283 r->replace = NULL;
284 sym->replace = NULL;
287 static void unset_replace_list(struct symbol_list *list)
289 struct symbol *sym;
290 FOR_EACH_PTR(list, sym) {
291 unset_replace(sym);
292 } END_FOR_EACH_PTR(sym);
295 static struct statement *copy_one_statement(struct statement *stmt)
297 if (!stmt)
298 return NULL;
299 switch(stmt->type) {
300 case STMT_NONE:
301 break;
302 case STMT_DECLARATION: {
303 struct symbol *sym;
304 struct statement *newstmt = dup_statement(stmt);
305 newstmt->declaration = NULL;
306 FOR_EACH_PTR(stmt->declaration, sym) {
307 struct symbol *newsym = copy_symbol(stmt->pos, sym);
308 if (newsym != sym)
309 newsym->initializer = copy_expression(sym->initializer);
310 add_symbol(&newstmt->declaration, newsym);
311 } END_FOR_EACH_PTR(sym);
312 stmt = newstmt;
313 break;
315 case STMT_CONTEXT:
316 case STMT_EXPRESSION: {
317 struct expression *expr = copy_expression(stmt->expression);
318 if (expr == stmt->expression)
319 break;
320 stmt = dup_statement(stmt);
321 stmt->expression = expr;
322 break;
324 case STMT_RANGE: {
325 struct expression *expr = copy_expression(stmt->range_expression);
326 if (expr == stmt->expression)
327 break;
328 stmt = dup_statement(stmt);
329 stmt->range_expression = expr;
330 break;
332 case STMT_COMPOUND: {
333 struct statement *new = alloc_statement(stmt->pos, STMT_COMPOUND);
334 copy_statement(stmt, new);
335 stmt = new;
336 break;
338 case STMT_IF: {
339 struct expression *cond = stmt->if_conditional;
340 struct statement *true = stmt->if_true;
341 struct statement *false = stmt->if_false;
343 cond = copy_expression(cond);
344 true = copy_one_statement(true);
345 false = copy_one_statement(false);
346 if (stmt->if_conditional == cond &&
347 stmt->if_true == true &&
348 stmt->if_false == false)
349 break;
350 stmt = dup_statement(stmt);
351 stmt->if_conditional = cond;
352 stmt->if_true = true;
353 stmt->if_false = false;
354 break;
356 case STMT_RETURN: {
357 struct expression *retval = copy_expression(stmt->ret_value);
358 struct symbol *sym = copy_symbol(stmt->pos, stmt->ret_target);
360 stmt = dup_statement(stmt);
361 stmt->ret_value = retval;
362 stmt->ret_target = sym;
363 break;
365 case STMT_CASE: {
366 stmt = dup_statement(stmt);
367 stmt->case_label = copy_symbol(stmt->pos, stmt->case_label);
368 stmt->case_expression = copy_expression(stmt->case_expression);
369 stmt->case_to = copy_expression(stmt->case_to);
370 stmt->case_statement = copy_one_statement(stmt->case_statement);
371 break;
373 case STMT_SWITCH: {
374 struct symbol *switch_break = copy_symbol(stmt->pos, stmt->switch_break);
375 struct symbol *switch_case = copy_symbol(stmt->pos, stmt->switch_case);
376 struct expression *expr = copy_expression(stmt->switch_expression);
377 struct statement *switch_stmt = copy_one_statement(stmt->switch_statement);
379 stmt = dup_statement(stmt);
380 switch_case->symbol_list = copy_symbol_list(switch_case->symbol_list);
381 stmt->switch_break = switch_break;
382 stmt->switch_case = switch_case;
383 stmt->switch_expression = expr;
384 stmt->switch_statement = switch_stmt;
385 break;
387 case STMT_ITERATOR: {
388 stmt = dup_statement(stmt);
389 stmt->iterator_break = copy_symbol(stmt->pos, stmt->iterator_break);
390 stmt->iterator_continue = copy_symbol(stmt->pos, stmt->iterator_continue);
391 stmt->iterator_syms = copy_symbol_list(stmt->iterator_syms);
393 stmt->iterator_pre_statement = copy_one_statement(stmt->iterator_pre_statement);
394 stmt->iterator_pre_condition = copy_expression(stmt->iterator_pre_condition);
396 stmt->iterator_statement = copy_one_statement(stmt->iterator_statement);
398 stmt->iterator_post_statement = copy_one_statement(stmt->iterator_post_statement);
399 stmt->iterator_post_condition = copy_expression(stmt->iterator_post_condition);
400 break;
402 case STMT_LABEL: {
403 stmt = dup_statement(stmt);
404 stmt->label_identifier = copy_symbol(stmt->pos, stmt->label_identifier);
405 stmt->label_statement = copy_one_statement(stmt->label_statement);
406 break;
408 case STMT_GOTO: {
409 stmt = dup_statement(stmt);
410 stmt->goto_label = copy_symbol(stmt->pos, stmt->goto_label);
411 stmt->goto_expression = copy_expression(stmt->goto_expression);
412 stmt->target_list = copy_symbol_list(stmt->target_list);
413 break;
415 case STMT_ASM: {
416 stmt = dup_statement(stmt);
417 stmt->asm_inputs = copy_asm_constraints(stmt->asm_inputs);
418 stmt->asm_outputs = copy_asm_constraints(stmt->asm_outputs);
419 /* no need to dup "clobbers", since they are all constant strings */
420 break;
422 default:
423 warning(stmt->pos, "trying to copy statement type %d", stmt->type);
424 break;
426 return stmt;
430 * Copy a stateemnt tree from 'src' to 'dst', where both
431 * source and destination are of type STMT_COMPOUND.
433 * We do this for the tree-level inliner.
435 * This doesn't do the symbol replacement right: it's not
436 * re-entrant.
438 void copy_statement(struct statement *src, struct statement *dst)
440 struct statement *stmt;
442 FOR_EACH_PTR(src->stmts, stmt) {
443 add_statement(&dst->stmts, copy_one_statement(stmt));
444 } END_FOR_EACH_PTR(stmt);
446 dst->ret = copy_symbol(src->pos, src->ret);
449 static struct symbol *create_copy_symbol(struct symbol *orig)
451 struct symbol *sym = orig;
452 if (orig) {
453 sym = alloc_symbol(orig->pos, orig->type);
454 *sym = *orig;
455 sym->bb_target = NULL;
456 sym->pseudo = NULL;
457 set_replace(orig, sym);
458 orig = sym;
460 return orig;
463 static struct symbol_list *create_symbol_list(struct symbol_list *src)
465 struct symbol_list *dst = NULL;
466 struct symbol *sym;
468 FOR_EACH_PTR(src, sym) {
469 struct symbol *newsym = create_copy_symbol(sym);
470 add_symbol(&dst, newsym);
471 } END_FOR_EACH_PTR(sym);
472 return dst;
475 int inline_function(struct expression *expr, struct symbol *sym)
477 struct symbol_list * fn_symbol_list;
478 struct symbol *fn = sym->ctype.base_type;
479 struct expression_list *arg_list = expr->args;
480 struct statement *stmt = alloc_statement(expr->pos, STMT_COMPOUND);
481 struct symbol_list *name_list, *arg_decl;
482 struct symbol *name;
483 struct expression *arg;
485 if (!fn->inline_stmt) {
486 sparse_error(fn->pos, "marked inline, but without a definition");
487 return 0;
489 if (fn->expanding)
490 return 0;
491 fn->expanding = 1;
493 name_list = fn->arguments;
495 expr->type = EXPR_STATEMENT;
496 expr->statement = stmt;
497 expr->ctype = fn->ctype.base_type;
499 fn_symbol_list = create_symbol_list(sym->inline_symbol_list);
501 arg_decl = NULL;
502 PREPARE_PTR_LIST(name_list, name);
503 FOR_EACH_PTR(arg_list, arg) {
504 struct symbol *a = alloc_symbol(arg->pos, SYM_NODE);
506 a->ctype.base_type = arg->ctype;
507 if (name) {
508 *a = *name;
509 set_replace(name, a);
510 add_symbol(&fn_symbol_list, a);
512 a->initializer = arg;
513 add_symbol(&arg_decl, a);
515 NEXT_PTR_LIST(name);
516 } END_FOR_EACH_PTR(arg);
517 FINISH_PTR_LIST(name);
519 if (arg_decl) {
520 struct statement *decl = alloc_statement(expr->pos, STMT_DECLARATION);
521 decl->declaration = arg_decl;
522 add_statement(&stmt->stmts, decl);
525 copy_statement(fn->inline_stmt, stmt);
527 unset_replace_list(fn_symbol_list);
529 evaluate_statement(stmt);
531 fn->expanding = 0;
532 return 1;
535 void uninline(struct symbol *sym)
537 struct symbol *fn = sym->ctype.base_type;
538 struct symbol_list *arg_list = fn->arguments;
539 struct symbol *p;
541 sym->symbol_list = create_symbol_list(sym->inline_symbol_list);
542 FOR_EACH_PTR(arg_list, p) {
543 p->replace = p;
544 } END_FOR_EACH_PTR(p);
545 fn->stmt = alloc_statement(fn->pos, STMT_COMPOUND);
546 copy_statement(fn->inline_stmt, fn->stmt);
547 unset_replace_list(sym->symbol_list);
548 unset_replace_list(arg_list);