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
18 #include "expression.h"
20 static struct expression
* dup_expression(struct expression
*expr
)
22 struct expression
*dup
= alloc_expression(expr
->pos
, expr
->type
);
27 static struct statement
* dup_statement(struct statement
*stmt
)
29 struct statement
*dup
= alloc_statement(stmt
->pos
, stmt
->type
);
34 static struct symbol
*copy_symbol(struct position pos
, struct symbol
*sym
)
38 if (sym
->ctype
.modifiers
& (MOD_STATIC
| MOD_EXTERN
| MOD_TOPLEVEL
| MOD_INLINE
))
41 warning(pos
, "unreplaced symbol '%s'", show_ident(sym
->ident
));
47 static struct symbol_list
*copy_symbol_list(struct symbol_list
*src
)
49 struct symbol_list
*dst
= NULL
;
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
);
59 static struct expression
* copy_expression(struct expression
*expr
)
66 * EXPR_SYMBOL is the interesting case, we may need to replace the
67 * symbol to the new copy.
70 struct symbol
*sym
= copy_symbol(expr
->pos
, expr
->symbol
);
71 if (sym
== expr
->symbol
)
73 expr
= dup_expression(expr
);
78 /* Atomics, never change, just return the expression directly */
84 /* Unops: check if the subexpression is unique */
87 struct expression
*unop
= copy_expression(expr
->unop
);
88 if (expr
->unop
== unop
)
90 expr
= dup_expression(expr
);
96 struct expression
*base
= copy_expression(expr
->base
);
97 expr
= dup_expression(expr
);
102 /* Binops: copy left/right expressions */
107 struct expression
*left
= copy_expression(expr
->left
);
108 struct expression
*right
= copy_expression(expr
->right
);
109 if (left
== expr
->left
&& right
== expr
->right
)
111 expr
= dup_expression(expr
);
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
)
122 expr
= dup_expression(expr
);
130 struct expression
*deref
= copy_expression(expr
->deref
);
131 expr
= dup_expression(expr
);
136 /* Cast/sizeof/__alignof__ */
138 case EXPR_IMPLIED_CAST
:
142 struct expression
*cast
= copy_expression(expr
->cast_expression
);
143 if (cast
== expr
->cast_expression
)
145 expr
= dup_expression(expr
);
146 expr
->cast_expression
= cast
;
150 /* Conditional expression */
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
)
158 expr
= dup_expression(expr
);
159 expr
->conditional
= cond
;
160 expr
->cond_true
= true;
161 expr
->cond_false
= false;
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
;
174 /* Call expression */
176 struct expression
*fn
= copy_expression(expr
->fn
);
177 struct expression_list
*list
= expr
->args
;
178 struct expression
*arg
;
180 expr
= dup_expression(expr
);
183 FOR_EACH_PTR(list
, arg
) {
184 add_expression(&expr
->args
, copy_expression(arg
));
185 } END_FOR_EACH_PTR(arg
);
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
);
201 /* Label in inline function - hmm. */
203 struct symbol
*label_symbol
= copy_symbol(expr
->pos
, expr
->label_symbol
);
204 expr
= dup_expression(expr
);
205 expr
->label_symbol
= label_symbol
;
210 struct expression
*sub_expr
= copy_expression(expr
->idx_expression
);
211 if (sub_expr
== expr
->idx_expression
)
213 expr
= dup_expression(expr
);
214 expr
->idx_expression
= sub_expr
;
218 case EXPR_IDENTIFIER
: {
219 struct expression
*sub_expr
= copy_expression(expr
->ident_expression
);
220 expr
= dup_expression(expr
);
221 expr
->ident_expression
= sub_expr
;
225 /* Position in initializer.. */
227 struct expression
*val
= copy_expression(expr
->init_expr
);
228 expr
= dup_expression(expr
);
229 expr
->init_expr
= val
;
234 warning(expr
->pos
, "trying to copy expression type %d", expr
->type
);
239 static struct expression_list
*copy_asm_constraints(struct expression_list
*in
)
241 struct expression_list
*out
= NULL
;
242 struct expression
*expr
;
245 FOR_EACH_PTR(in
, expr
) {
247 case 0: /* identifier */
248 case 1: /* constraint */
250 add_expression(&out
, expr
);
252 case 2: /* expression */
254 add_expression(&out
, copy_expression(expr
));
257 } END_FOR_EACH_PTR(expr
);
261 static void set_replace(struct symbol
*old
, struct symbol
*new)
267 static void unset_replace(struct symbol
*sym
)
269 struct symbol
*r
= sym
->replace
;
271 warning(sym
->pos
, "symbol '%s' not replaced?", show_ident(sym
->ident
));
278 static void unset_replace_list(struct symbol_list
*list
)
281 FOR_EACH_PTR(list
, sym
) {
283 } END_FOR_EACH_PTR(sym
);
286 static struct statement
*copy_one_statement(struct statement
*stmt
)
294 case STMT_EXPRESSION
: {
295 struct expression
*expr
= copy_expression(stmt
->expression
);
296 if (expr
== stmt
->expression
)
298 stmt
= dup_statement(stmt
);
299 stmt
->expression
= expr
;
303 struct expression
*expr
= copy_expression(stmt
->range_expression
);
304 if (expr
== stmt
->expression
)
306 stmt
= dup_statement(stmt
);
307 stmt
->range_expression
= expr
;
310 case STMT_COMPOUND
: {
311 struct statement
*new = alloc_statement(stmt
->pos
, STMT_COMPOUND
);
312 copy_statement(stmt
, new);
317 struct expression
*cond
= stmt
->if_conditional
;
318 struct statement
*true = stmt
->if_true
;
319 struct statement
*false = stmt
->if_false
;
321 cond
= copy_expression(cond
);
322 true = copy_one_statement(true);
323 false = copy_one_statement(false);
324 if (stmt
->if_conditional
== cond
&&
325 stmt
->if_true
== true &&
326 stmt
->if_false
== false)
328 stmt
= dup_statement(stmt
);
329 stmt
->if_conditional
= cond
;
330 stmt
->if_true
= true;
331 stmt
->if_false
= false;
335 struct expression
*retval
= copy_expression(stmt
->ret_value
);
336 struct symbol
*sym
= copy_symbol(stmt
->pos
, stmt
->ret_target
);
338 stmt
= dup_statement(stmt
);
339 stmt
->ret_value
= retval
;
340 stmt
->ret_target
= sym
;
344 stmt
= dup_statement(stmt
);
345 stmt
->case_label
= copy_symbol(stmt
->pos
, stmt
->case_label
);
346 stmt
->case_expression
= copy_expression(stmt
->case_expression
);
347 stmt
->case_to
= copy_expression(stmt
->case_to
);
348 stmt
->case_statement
= copy_one_statement(stmt
->case_statement
);
352 struct symbol
*switch_break
= copy_symbol(stmt
->pos
, stmt
->switch_break
);
353 struct symbol
*switch_case
= copy_symbol(stmt
->pos
, stmt
->switch_case
);
354 struct expression
*expr
= copy_expression(stmt
->switch_expression
);
355 struct statement
*switch_stmt
= copy_one_statement(stmt
->switch_statement
);
357 stmt
= dup_statement(stmt
);
358 switch_case
->symbol_list
= copy_symbol_list(switch_case
->symbol_list
);
359 stmt
->switch_break
= switch_break
;
360 stmt
->switch_case
= switch_case
;
361 stmt
->switch_expression
= expr
;
362 stmt
->switch_statement
= switch_stmt
;
365 case STMT_ITERATOR
: {
366 stmt
= dup_statement(stmt
);
367 stmt
->iterator_break
= copy_symbol(stmt
->pos
, stmt
->iterator_break
);
368 stmt
->iterator_continue
= copy_symbol(stmt
->pos
, stmt
->iterator_continue
);
369 stmt
->iterator_syms
= copy_symbol_list(stmt
->iterator_syms
);
371 stmt
->iterator_pre_statement
= copy_one_statement(stmt
->iterator_pre_statement
);
372 stmt
->iterator_pre_condition
= copy_expression(stmt
->iterator_pre_condition
);
374 stmt
->iterator_statement
= copy_one_statement(stmt
->iterator_statement
);
376 stmt
->iterator_post_statement
= copy_one_statement(stmt
->iterator_post_statement
);
377 stmt
->iterator_post_condition
= copy_expression(stmt
->iterator_post_condition
);
381 stmt
= dup_statement(stmt
);
382 stmt
->label_identifier
= copy_symbol(stmt
->pos
, stmt
->label_identifier
);
383 stmt
->label_statement
= copy_one_statement(stmt
->label_statement
);
387 stmt
= dup_statement(stmt
);
388 stmt
->goto_label
= copy_symbol(stmt
->pos
, stmt
->goto_label
);
389 stmt
->goto_expression
= copy_expression(stmt
->goto_expression
);
390 stmt
->target_list
= copy_symbol_list(stmt
->target_list
);
394 stmt
= dup_statement(stmt
);
395 stmt
->asm_inputs
= copy_asm_constraints(stmt
->asm_inputs
);
396 stmt
->asm_outputs
= copy_asm_constraints(stmt
->asm_outputs
);
397 /* no need to dup "clobbers", since they are all constant strings */
401 warning(stmt
->pos
, "trying to copy statement type %d", stmt
->type
);
408 * Copy a stateemnt tree from 'src' to 'dst', where both
409 * source and destination are of type STMT_COMPOUND.
411 * We do this for the tree-level inliner.
413 * This doesn't do the symbol replacement right: it's not
416 void copy_statement(struct statement
*src
, struct statement
*dst
)
418 struct statement
*stmt
;
421 FOR_EACH_PTR(src
->syms
, sym
) {
422 struct symbol
*newsym
= copy_symbol(src
->pos
, sym
);
424 newsym
->initializer
= copy_expression(sym
->initializer
);
425 add_symbol(&dst
->syms
, newsym
);
426 } END_FOR_EACH_PTR(sym
);
428 FOR_EACH_PTR(src
->stmts
, stmt
) {
429 add_statement(&dst
->stmts
, copy_one_statement(stmt
));
430 } END_FOR_EACH_PTR(stmt
);
432 dst
->ret
= copy_symbol(src
->pos
, src
->ret
);
435 static struct symbol
*create_copy_symbol(struct symbol
*orig
)
437 struct symbol
*sym
= orig
;
439 sym
= alloc_symbol(orig
->pos
, orig
->type
);
441 sym
->bb_target
= NULL
;
443 set_replace(orig
, sym
);
449 static struct symbol_list
*create_symbol_list(struct symbol_list
*src
)
451 struct symbol_list
*dst
= NULL
;
454 FOR_EACH_PTR(src
, sym
) {
455 struct symbol
*newsym
= create_copy_symbol(sym
);
456 add_symbol(&dst
, newsym
);
457 } END_FOR_EACH_PTR(sym
);
461 int inline_function(struct expression
*expr
, struct symbol
*sym
)
463 struct symbol_list
* fn_symbol_list
;
464 struct symbol
*fn
= sym
->ctype
.base_type
;
465 struct expression_list
*arg_list
= expr
->args
;
466 struct statement
*stmt
= alloc_statement(expr
->pos
, STMT_COMPOUND
);
467 struct symbol_list
*name_list
;
469 struct expression
*arg
;
471 if (!fn
->inline_stmt
) {
472 error(fn
->pos
, "marked inline, but without a definition");
479 name_list
= fn
->arguments
;
481 expr
->type
= EXPR_STATEMENT
;
482 expr
->statement
= stmt
;
483 expr
->ctype
= fn
->ctype
.base_type
;
485 fn_symbol_list
= create_symbol_list(sym
->inline_symbol_list
);
487 PREPARE_PTR_LIST(name_list
, name
);
488 FOR_EACH_PTR(arg_list
, arg
) {
489 struct symbol
*a
= alloc_symbol(arg
->pos
, SYM_NODE
);
491 a
->ctype
.base_type
= arg
->ctype
;
494 set_replace(name
, a
);
495 add_symbol(&fn_symbol_list
, a
);
497 a
->initializer
= arg
;
498 add_symbol(&stmt
->syms
, a
);
501 } END_FOR_EACH_PTR(arg
);
502 FINISH_PTR_LIST(name
);
504 copy_statement(fn
->inline_stmt
, stmt
);
506 unset_replace_list(fn_symbol_list
);
508 evaluate_statement(stmt
);
514 void uninline(struct symbol
*sym
)
516 struct symbol
*fn
= sym
->ctype
.base_type
;
517 struct symbol_list
*arg_list
= fn
->arguments
;
520 sym
->symbol_list
= create_symbol_list(sym
->inline_symbol_list
);
521 FOR_EACH_PTR(arg_list
, p
) {
523 } END_FOR_EACH_PTR(p
);
524 fn
->stmt
= alloc_statement(fn
->pos
, STMT_COMPOUND
);
525 copy_statement(fn
->inline_stmt
, fn
->stmt
);
526 unset_replace_list(sym
->symbol_list
);
527 unset_replace_list(arg_list
);