ctags: Use const as appropriate in cmp_sym()
[smatch.git] / inline.c
blobf80403c00368f824a973a3147c14086765470472
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 case EXPR_TYPE:
83 break;
85 /* Unops: check if the subexpression is unique */
86 case EXPR_PREOP:
87 case EXPR_POSTOP: {
88 struct expression *unop = copy_expression(expr->unop);
89 if (expr->unop == unop)
90 break;
91 expr = dup_expression(expr);
92 expr->unop = unop;
93 break;
96 case EXPR_SLICE: {
97 struct expression *base = copy_expression(expr->base);
98 expr = dup_expression(expr);
99 expr->base = base;
100 break;
103 /* Binops: copy left/right expressions */
104 case EXPR_BINOP:
105 case EXPR_COMMA:
106 case EXPR_COMPARE:
107 case EXPR_LOGICAL: {
108 struct expression *left = copy_expression(expr->left);
109 struct expression *right = copy_expression(expr->right);
110 if (left == expr->left && right == expr->right)
111 break;
112 expr = dup_expression(expr);
113 expr->left = left;
114 expr->right = right;
115 break;
118 case EXPR_ASSIGNMENT: {
119 struct expression *left = copy_expression(expr->left);
120 struct expression *right = copy_expression(expr->right);
121 if (expr->op == '=' && left == expr->left && right == expr->right)
122 break;
123 expr = dup_expression(expr);
124 expr->left = left;
125 expr->right = right;
126 break;
129 /* Dereference */
130 case EXPR_DEREF: {
131 struct expression *deref = copy_expression(expr->deref);
132 expr = dup_expression(expr);
133 expr->deref = deref;
134 break;
137 /* Cast/sizeof/__alignof__ */
138 case EXPR_CAST:
139 if (expr->cast_expression->type == EXPR_INITIALIZER) {
140 struct expression *cast = expr->cast_expression;
141 struct symbol *sym = expr->cast_type;
142 expr = dup_expression(expr);
143 expr->cast_expression = copy_expression(cast);
144 expr->cast_type = alloc_symbol(sym->pos, sym->type);
145 *expr->cast_type = *sym;
146 break;
148 case EXPR_IMPLIED_CAST:
149 case EXPR_SIZEOF:
150 case EXPR_PTRSIZEOF:
151 case EXPR_ALIGNOF: {
152 struct expression *cast = copy_expression(expr->cast_expression);
153 if (cast == expr->cast_expression)
154 break;
155 expr = dup_expression(expr);
156 expr->cast_expression = cast;
157 break;
160 /* Conditional expression */
161 case EXPR_SELECT:
162 case EXPR_CONDITIONAL: {
163 struct expression *cond = copy_expression(expr->conditional);
164 struct expression *true = copy_expression(expr->cond_true);
165 struct expression *false = copy_expression(expr->cond_false);
166 if (cond == expr->conditional && true == expr->cond_true && false == expr->cond_false)
167 break;
168 expr = dup_expression(expr);
169 expr->conditional = cond;
170 expr->cond_true = true;
171 expr->cond_false = false;
172 break;
175 /* Statement expression */
176 case EXPR_STATEMENT: {
177 struct statement *stmt = alloc_statement(expr->pos, STMT_COMPOUND);
178 copy_statement(expr->statement, stmt);
179 expr = dup_expression(expr);
180 expr->statement = stmt;
181 break;
184 /* Call expression */
185 case EXPR_CALL: {
186 struct expression *fn = copy_expression(expr->fn);
187 struct expression_list *list = expr->args;
188 struct expression *arg;
190 expr = dup_expression(expr);
191 expr->fn = fn;
192 expr->args = NULL;
193 FOR_EACH_PTR(list, arg) {
194 add_expression(&expr->args, copy_expression(arg));
195 } END_FOR_EACH_PTR(arg);
196 break;
199 /* Initializer list statement */
200 case EXPR_INITIALIZER: {
201 struct expression_list *list = expr->expr_list;
202 struct expression *entry;
203 expr = dup_expression(expr);
204 expr->expr_list = NULL;
205 FOR_EACH_PTR(list, entry) {
206 add_expression(&expr->expr_list, copy_expression(entry));
207 } END_FOR_EACH_PTR(entry);
208 break;
211 /* Label in inline function - hmm. */
212 case EXPR_LABEL: {
213 struct symbol *label_symbol = copy_symbol(expr->pos, expr->label_symbol);
214 expr = dup_expression(expr);
215 expr->label_symbol = label_symbol;
216 break;
219 case EXPR_INDEX: {
220 struct expression *sub_expr = copy_expression(expr->idx_expression);
221 expr = dup_expression(expr);
222 expr->idx_expression = sub_expr;
223 break;
226 case EXPR_IDENTIFIER: {
227 struct expression *sub_expr = copy_expression(expr->ident_expression);
228 expr = dup_expression(expr);
229 expr->ident_expression = sub_expr;
230 break;
233 /* Position in initializer.. */
234 case EXPR_POS: {
235 struct expression *val = copy_expression(expr->init_expr);
236 expr = dup_expression(expr);
237 expr->init_expr = val;
238 break;
241 default:
242 warning(expr->pos, "trying to copy expression type %d", expr->type);
244 return expr;
247 static struct expression_list *copy_asm_constraints(struct expression_list *in)
249 struct expression_list *out = NULL;
250 struct expression *expr;
251 int state = 0;
253 FOR_EACH_PTR(in, expr) {
254 switch (state) {
255 case 0: /* identifier */
256 case 1: /* constraint */
257 state++;
258 add_expression(&out, expr);
259 continue;
260 case 2: /* expression */
261 state = 0;
262 add_expression(&out, copy_expression(expr));
263 continue;
265 } END_FOR_EACH_PTR(expr);
266 return out;
269 static void set_replace(struct symbol *old, struct symbol *new)
271 new->replace = old;
272 old->replace = new;
275 static void unset_replace(struct symbol *sym)
277 struct symbol *r = sym->replace;
278 if (!r) {
279 warning(sym->pos, "symbol '%s' not replaced?", show_ident(sym->ident));
280 return;
282 r->replace = NULL;
283 sym->replace = NULL;
286 static void unset_replace_list(struct symbol_list *list)
288 struct symbol *sym;
289 FOR_EACH_PTR(list, sym) {
290 unset_replace(sym);
291 } END_FOR_EACH_PTR(sym);
294 static struct statement *copy_one_statement(struct statement *stmt)
296 if (!stmt)
297 return NULL;
298 switch(stmt->type) {
299 case STMT_NONE:
300 break;
301 case STMT_DECLARATION: {
302 struct symbol *sym;
303 struct statement *newstmt = dup_statement(stmt);
304 newstmt->declaration = NULL;
305 FOR_EACH_PTR(stmt->declaration, sym) {
306 struct symbol *newsym = copy_symbol(stmt->pos, sym);
307 if (newsym != sym)
308 newsym->initializer = copy_expression(sym->initializer);
309 add_symbol(&newstmt->declaration, newsym);
310 } END_FOR_EACH_PTR(sym);
311 stmt = newstmt;
312 break;
314 case STMT_CONTEXT:
315 case STMT_EXPRESSION: {
316 struct expression *expr = copy_expression(stmt->expression);
317 if (expr == stmt->expression)
318 break;
319 stmt = dup_statement(stmt);
320 stmt->expression = expr;
321 break;
323 case STMT_RANGE: {
324 struct expression *expr = copy_expression(stmt->range_expression);
325 if (expr == stmt->expression)
326 break;
327 stmt = dup_statement(stmt);
328 stmt->range_expression = expr;
329 break;
331 case STMT_COMPOUND: {
332 struct statement *new = alloc_statement(stmt->pos, STMT_COMPOUND);
333 copy_statement(stmt, new);
334 stmt = new;
335 break;
337 case STMT_IF: {
338 struct expression *cond = stmt->if_conditional;
339 struct statement *true = stmt->if_true;
340 struct statement *false = stmt->if_false;
342 cond = copy_expression(cond);
343 true = copy_one_statement(true);
344 false = copy_one_statement(false);
345 if (stmt->if_conditional == cond &&
346 stmt->if_true == true &&
347 stmt->if_false == false)
348 break;
349 stmt = dup_statement(stmt);
350 stmt->if_conditional = cond;
351 stmt->if_true = true;
352 stmt->if_false = false;
353 break;
355 case STMT_RETURN: {
356 struct expression *retval = copy_expression(stmt->ret_value);
357 struct symbol *sym = copy_symbol(stmt->pos, stmt->ret_target);
359 stmt = dup_statement(stmt);
360 stmt->ret_value = retval;
361 stmt->ret_target = sym;
362 break;
364 case STMT_CASE: {
365 stmt = dup_statement(stmt);
366 stmt->case_label = copy_symbol(stmt->pos, stmt->case_label);
367 stmt->case_expression = copy_expression(stmt->case_expression);
368 stmt->case_to = copy_expression(stmt->case_to);
369 stmt->case_statement = copy_one_statement(stmt->case_statement);
370 break;
372 case STMT_SWITCH: {
373 struct symbol *switch_break = copy_symbol(stmt->pos, stmt->switch_break);
374 struct symbol *switch_case = copy_symbol(stmt->pos, stmt->switch_case);
375 struct expression *expr = copy_expression(stmt->switch_expression);
376 struct statement *switch_stmt = copy_one_statement(stmt->switch_statement);
378 stmt = dup_statement(stmt);
379 switch_case->symbol_list = copy_symbol_list(switch_case->symbol_list);
380 stmt->switch_break = switch_break;
381 stmt->switch_case = switch_case;
382 stmt->switch_expression = expr;
383 stmt->switch_statement = switch_stmt;
384 break;
386 case STMT_ITERATOR: {
387 stmt = dup_statement(stmt);
388 stmt->iterator_break = copy_symbol(stmt->pos, stmt->iterator_break);
389 stmt->iterator_continue = copy_symbol(stmt->pos, stmt->iterator_continue);
390 stmt->iterator_syms = copy_symbol_list(stmt->iterator_syms);
392 stmt->iterator_pre_statement = copy_one_statement(stmt->iterator_pre_statement);
393 stmt->iterator_pre_condition = copy_expression(stmt->iterator_pre_condition);
395 stmt->iterator_statement = copy_one_statement(stmt->iterator_statement);
397 stmt->iterator_post_statement = copy_one_statement(stmt->iterator_post_statement);
398 stmt->iterator_post_condition = copy_expression(stmt->iterator_post_condition);
399 break;
401 case STMT_LABEL: {
402 stmt = dup_statement(stmt);
403 stmt->label_identifier = copy_symbol(stmt->pos, stmt->label_identifier);
404 stmt->label_statement = copy_one_statement(stmt->label_statement);
405 break;
407 case STMT_GOTO: {
408 stmt = dup_statement(stmt);
409 stmt->goto_label = copy_symbol(stmt->pos, stmt->goto_label);
410 stmt->goto_expression = copy_expression(stmt->goto_expression);
411 stmt->target_list = copy_symbol_list(stmt->target_list);
412 break;
414 case STMT_ASM: {
415 stmt = dup_statement(stmt);
416 stmt->asm_inputs = copy_asm_constraints(stmt->asm_inputs);
417 stmt->asm_outputs = copy_asm_constraints(stmt->asm_outputs);
418 /* no need to dup "clobbers", since they are all constant strings */
419 break;
421 default:
422 warning(stmt->pos, "trying to copy statement type %d", stmt->type);
423 break;
425 return stmt;
429 * Copy a statement tree from 'src' to 'dst', where both
430 * source and destination are of type STMT_COMPOUND.
432 * We do this for the tree-level inliner.
434 * This doesn't do the symbol replacement right: it's not
435 * re-entrant.
437 void copy_statement(struct statement *src, struct statement *dst)
439 struct statement *stmt;
441 FOR_EACH_PTR(src->stmts, stmt) {
442 add_statement(&dst->stmts, copy_one_statement(stmt));
443 } END_FOR_EACH_PTR(stmt);
444 dst->args = copy_one_statement(src->args);
445 dst->ret = copy_symbol(src->pos, src->ret);
446 dst->inline_fn = src->inline_fn;
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;
492 fn->expanding = 1;
494 name_list = fn->arguments;
496 expr->type = EXPR_STATEMENT;
497 expr->statement = stmt;
498 expr->ctype = fn->ctype.base_type;
500 fn_symbol_list = create_symbol_list(sym->inline_symbol_list);
502 arg_decl = NULL;
503 PREPARE_PTR_LIST(name_list, name);
504 FOR_EACH_PTR(arg_list, arg) {
505 struct symbol *a = alloc_symbol(arg->pos, SYM_NODE);
507 a->ctype.base_type = arg->ctype;
508 if (name) {
509 *a = *name;
510 set_replace(name, a);
511 add_symbol(&fn_symbol_list, a);
513 a->initializer = arg;
514 add_symbol(&arg_decl, a);
516 NEXT_PTR_LIST(name);
517 } END_FOR_EACH_PTR(arg);
518 FINISH_PTR_LIST(name);
520 copy_statement(fn->inline_stmt, stmt);
522 if (arg_decl) {
523 struct statement *decl = alloc_statement(expr->pos, STMT_DECLARATION);
524 decl->declaration = arg_decl;
525 stmt->args = decl;
527 stmt->inline_fn = sym;
529 unset_replace_list(fn_symbol_list);
531 evaluate_statement(stmt);
533 fn->expanding = 0;
534 return 1;
537 void uninline(struct symbol *sym)
539 struct symbol *fn = sym->ctype.base_type;
540 struct symbol_list *arg_list = fn->arguments;
541 struct symbol *p;
543 sym->symbol_list = create_symbol_list(sym->inline_symbol_list);
544 FOR_EACH_PTR(arg_list, p) {
545 p->replace = p;
546 } END_FOR_EACH_PTR(p);
547 fn->stmt = alloc_statement(fn->pos, STMT_COMPOUND);
548 copy_statement(fn->inline_stmt, fn->stmt);
549 unset_replace_list(sym->symbol_list);
550 unset_replace_list(arg_list);