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 */
85 /* Unops: check if the subexpression is unique */
88 struct expression
*unop
= copy_expression(expr
->unop
);
89 if (expr
->unop
== unop
)
91 expr
= dup_expression(expr
);
97 struct expression
*base
= copy_expression(expr
->base
);
98 expr
= dup_expression(expr
);
103 /* Binops: copy left/right expressions */
108 struct expression
*left
= copy_expression(expr
->left
);
109 struct expression
*right
= copy_expression(expr
->right
);
110 if (left
== expr
->left
&& right
== expr
->right
)
112 expr
= dup_expression(expr
);
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
)
123 expr
= dup_expression(expr
);
131 struct expression
*deref
= copy_expression(expr
->deref
);
132 expr
= dup_expression(expr
);
137 /* Cast/sizeof/__alignof__ */
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
;
148 case EXPR_FORCE_CAST
:
149 case EXPR_IMPLIED_CAST
:
153 struct expression
*cast
= copy_expression(expr
->cast_expression
);
154 if (cast
== expr
->cast_expression
)
156 expr
= dup_expression(expr
);
157 expr
->cast_expression
= cast
;
161 /* Conditional expression */
163 case EXPR_CONDITIONAL
: {
164 struct expression
*cond
= copy_expression(expr
->conditional
);
165 struct expression
*true = copy_expression(expr
->cond_true
);
166 struct expression
*false = copy_expression(expr
->cond_false
);
167 if (cond
== expr
->conditional
&& true == expr
->cond_true
&& false == expr
->cond_false
)
169 expr
= dup_expression(expr
);
170 expr
->conditional
= cond
;
171 expr
->cond_true
= true;
172 expr
->cond_false
= false;
176 /* Statement expression */
177 case EXPR_STATEMENT
: {
178 struct statement
*stmt
= alloc_statement(expr
->pos
, STMT_COMPOUND
);
179 copy_statement(expr
->statement
, stmt
);
180 expr
= dup_expression(expr
);
181 expr
->statement
= stmt
;
185 /* Call expression */
187 struct expression
*fn
= copy_expression(expr
->fn
);
188 struct expression_list
*list
= expr
->args
;
189 struct expression
*arg
;
191 expr
= dup_expression(expr
);
194 FOR_EACH_PTR(list
, arg
) {
195 add_expression(&expr
->args
, copy_expression(arg
));
196 } END_FOR_EACH_PTR(arg
);
200 /* Initializer list statement */
201 case EXPR_INITIALIZER
: {
202 struct expression_list
*list
= expr
->expr_list
;
203 struct expression
*entry
;
204 expr
= dup_expression(expr
);
205 expr
->expr_list
= NULL
;
206 FOR_EACH_PTR(list
, entry
) {
207 add_expression(&expr
->expr_list
, copy_expression(entry
));
208 } END_FOR_EACH_PTR(entry
);
212 /* Label in inline function - hmm. */
214 struct symbol
*label_symbol
= copy_symbol(expr
->pos
, expr
->label_symbol
);
215 expr
= dup_expression(expr
);
216 expr
->label_symbol
= label_symbol
;
221 struct expression
*sub_expr
= copy_expression(expr
->idx_expression
);
222 expr
= dup_expression(expr
);
223 expr
->idx_expression
= sub_expr
;
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
;
234 /* Position in initializer.. */
236 struct expression
*val
= copy_expression(expr
->init_expr
);
237 expr
= dup_expression(expr
);
238 expr
->init_expr
= val
;
241 case EXPR_OFFSETOF
: {
242 struct expression
*val
= copy_expression(expr
->down
);
243 if (expr
->op
== '.') {
244 if (expr
->down
!= val
) {
245 expr
= dup_expression(expr
);
249 struct expression
*idx
= copy_expression(expr
->index
);
250 if (expr
->down
!= val
|| expr
->index
!= idx
) {
251 expr
= dup_expression(expr
);
259 warning(expr
->pos
, "trying to copy expression type %d", expr
->type
);
264 static struct expression_list
*copy_asm_constraints(struct expression_list
*in
)
266 struct expression_list
*out
= NULL
;
267 struct expression
*expr
;
270 FOR_EACH_PTR(in
, expr
) {
272 case 0: /* identifier */
273 case 1: /* constraint */
275 add_expression(&out
, expr
);
277 case 2: /* expression */
279 add_expression(&out
, copy_expression(expr
));
282 } END_FOR_EACH_PTR(expr
);
286 static void set_replace(struct symbol
*old
, struct symbol
*new)
292 static void unset_replace(struct symbol
*sym
)
294 struct symbol
*r
= sym
->replace
;
296 warning(sym
->pos
, "symbol '%s' not replaced?", show_ident(sym
->ident
));
303 static void unset_replace_list(struct symbol_list
*list
)
306 FOR_EACH_PTR(list
, sym
) {
308 } END_FOR_EACH_PTR(sym
);
311 static struct statement
*copy_one_statement(struct statement
*stmt
)
318 case STMT_DECLARATION
: {
320 struct statement
*newstmt
= dup_statement(stmt
);
321 newstmt
->declaration
= NULL
;
322 FOR_EACH_PTR(stmt
->declaration
, sym
) {
323 struct symbol
*newsym
= copy_symbol(stmt
->pos
, sym
);
325 newsym
->initializer
= copy_expression(sym
->initializer
);
326 add_symbol(&newstmt
->declaration
, newsym
);
327 } END_FOR_EACH_PTR(sym
);
332 case STMT_EXPRESSION
: {
333 struct expression
*expr
= copy_expression(stmt
->expression
);
334 if (expr
== stmt
->expression
)
336 stmt
= dup_statement(stmt
);
337 stmt
->expression
= expr
;
341 struct expression
*expr
= copy_expression(stmt
->range_expression
);
342 if (expr
== stmt
->expression
)
344 stmt
= dup_statement(stmt
);
345 stmt
->range_expression
= expr
;
348 case STMT_COMPOUND
: {
349 struct statement
*new = alloc_statement(stmt
->pos
, STMT_COMPOUND
);
350 copy_statement(stmt
, new);
355 struct expression
*cond
= stmt
->if_conditional
;
356 struct statement
*true = stmt
->if_true
;
357 struct statement
*false = stmt
->if_false
;
359 cond
= copy_expression(cond
);
360 true = copy_one_statement(true);
361 false = copy_one_statement(false);
362 if (stmt
->if_conditional
== cond
&&
363 stmt
->if_true
== true &&
364 stmt
->if_false
== false)
366 stmt
= dup_statement(stmt
);
367 stmt
->if_conditional
= cond
;
368 stmt
->if_true
= true;
369 stmt
->if_false
= false;
373 struct expression
*retval
= copy_expression(stmt
->ret_value
);
374 struct symbol
*sym
= copy_symbol(stmt
->pos
, stmt
->ret_target
);
376 stmt
= dup_statement(stmt
);
377 stmt
->ret_value
= retval
;
378 stmt
->ret_target
= sym
;
382 stmt
= dup_statement(stmt
);
383 stmt
->case_label
= copy_symbol(stmt
->pos
, stmt
->case_label
);
384 stmt
->case_label
->stmt
= stmt
;
385 stmt
->case_expression
= copy_expression(stmt
->case_expression
);
386 stmt
->case_to
= copy_expression(stmt
->case_to
);
387 stmt
->case_statement
= copy_one_statement(stmt
->case_statement
);
391 struct symbol
*switch_break
= copy_symbol(stmt
->pos
, stmt
->switch_break
);
392 struct symbol
*switch_case
= copy_symbol(stmt
->pos
, stmt
->switch_case
);
393 struct expression
*expr
= copy_expression(stmt
->switch_expression
);
394 struct statement
*switch_stmt
= copy_one_statement(stmt
->switch_statement
);
396 stmt
= dup_statement(stmt
);
397 switch_case
->symbol_list
= copy_symbol_list(switch_case
->symbol_list
);
398 stmt
->switch_break
= switch_break
;
399 stmt
->switch_case
= switch_case
;
400 stmt
->switch_expression
= expr
;
401 stmt
->switch_statement
= switch_stmt
;
404 case STMT_ITERATOR
: {
405 stmt
= dup_statement(stmt
);
406 stmt
->iterator_break
= copy_symbol(stmt
->pos
, stmt
->iterator_break
);
407 stmt
->iterator_continue
= copy_symbol(stmt
->pos
, stmt
->iterator_continue
);
408 stmt
->iterator_syms
= copy_symbol_list(stmt
->iterator_syms
);
410 stmt
->iterator_pre_statement
= copy_one_statement(stmt
->iterator_pre_statement
);
411 stmt
->iterator_pre_condition
= copy_expression(stmt
->iterator_pre_condition
);
413 stmt
->iterator_statement
= copy_one_statement(stmt
->iterator_statement
);
415 stmt
->iterator_post_statement
= copy_one_statement(stmt
->iterator_post_statement
);
416 stmt
->iterator_post_condition
= copy_expression(stmt
->iterator_post_condition
);
420 stmt
= dup_statement(stmt
);
421 stmt
->label_identifier
= copy_symbol(stmt
->pos
, stmt
->label_identifier
);
422 stmt
->label_statement
= copy_one_statement(stmt
->label_statement
);
426 stmt
= dup_statement(stmt
);
427 stmt
->goto_label
= copy_symbol(stmt
->pos
, stmt
->goto_label
);
428 stmt
->goto_expression
= copy_expression(stmt
->goto_expression
);
429 stmt
->target_list
= copy_symbol_list(stmt
->target_list
);
433 stmt
= dup_statement(stmt
);
434 stmt
->asm_inputs
= copy_asm_constraints(stmt
->asm_inputs
);
435 stmt
->asm_outputs
= copy_asm_constraints(stmt
->asm_outputs
);
436 /* no need to dup "clobbers", since they are all constant strings */
440 warning(stmt
->pos
, "trying to copy statement type %d", stmt
->type
);
447 * Copy a statement tree from 'src' to 'dst', where both
448 * source and destination are of type STMT_COMPOUND.
450 * We do this for the tree-level inliner.
452 * This doesn't do the symbol replacement right: it's not
455 void copy_statement(struct statement
*src
, struct statement
*dst
)
457 struct statement
*stmt
;
459 FOR_EACH_PTR(src
->stmts
, stmt
) {
460 add_statement(&dst
->stmts
, copy_one_statement(stmt
));
461 } END_FOR_EACH_PTR(stmt
);
462 dst
->args
= copy_one_statement(src
->args
);
463 dst
->ret
= copy_symbol(src
->pos
, src
->ret
);
464 dst
->inline_fn
= src
->inline_fn
;
467 static struct symbol
*create_copy_symbol(struct symbol
*orig
)
469 struct symbol
*sym
= orig
;
471 sym
= alloc_symbol(orig
->pos
, orig
->type
);
473 sym
->bb_target
= NULL
;
475 set_replace(orig
, sym
);
481 static struct symbol_list
*create_symbol_list(struct symbol_list
*src
)
483 struct symbol_list
*dst
= NULL
;
486 FOR_EACH_PTR(src
, sym
) {
487 struct symbol
*newsym
= create_copy_symbol(sym
);
488 add_symbol(&dst
, newsym
);
489 } END_FOR_EACH_PTR(sym
);
493 int inline_function(struct expression
*expr
, struct symbol
*sym
)
495 struct symbol_list
* fn_symbol_list
;
496 struct symbol
*fn
= sym
->ctype
.base_type
;
497 struct expression_list
*arg_list
= expr
->args
;
498 struct statement
*stmt
= alloc_statement(expr
->pos
, STMT_COMPOUND
);
499 struct symbol_list
*name_list
, *arg_decl
;
501 struct expression
*arg
;
503 if (!fn
->inline_stmt
) {
504 sparse_error(fn
->pos
, "marked inline, but without a definition");
512 name_list
= fn
->arguments
;
514 expr
->type
= EXPR_STATEMENT
;
515 expr
->statement
= stmt
;
516 expr
->ctype
= fn
->ctype
.base_type
;
518 fn_symbol_list
= create_symbol_list(sym
->inline_symbol_list
);
521 PREPARE_PTR_LIST(name_list
, name
);
522 FOR_EACH_PTR(arg_list
, arg
) {
523 struct symbol
*a
= alloc_symbol(arg
->pos
, SYM_NODE
);
525 a
->ctype
.base_type
= arg
->ctype
;
528 set_replace(name
, a
);
529 add_symbol(&fn_symbol_list
, a
);
531 a
->initializer
= arg
;
532 add_symbol(&arg_decl
, a
);
535 } END_FOR_EACH_PTR(arg
);
536 FINISH_PTR_LIST(name
);
538 copy_statement(fn
->inline_stmt
, stmt
);
541 struct statement
*decl
= alloc_statement(expr
->pos
, STMT_DECLARATION
);
542 decl
->declaration
= arg_decl
;
545 stmt
->inline_fn
= sym
;
547 unset_replace_list(fn_symbol_list
);
549 evaluate_statement(stmt
);
555 void uninline(struct symbol
*sym
)
557 struct symbol
*fn
= sym
->ctype
.base_type
;
558 struct symbol_list
*arg_list
= fn
->arguments
;
561 sym
->symbol_list
= create_symbol_list(sym
->inline_symbol_list
);
562 FOR_EACH_PTR(arg_list
, p
) {
564 } END_FOR_EACH_PTR(p
);
565 fn
->stmt
= alloc_statement(fn
->pos
, STMT_COMPOUND
);
566 copy_statement(fn
->inline_stmt
, fn
->stmt
);
567 unset_replace_list(sym
->symbol_list
);
568 unset_replace_list(arg_list
);