2 * Sparse - a semantic source parser.
4 * Copyright (C) 2003 Transmeta Corp.
7 * Licensed under the Open Software License version 1.1
17 #include "expression.h"
19 static struct expression
* dup_expression(struct expression
*expr
)
21 struct expression
*dup
= alloc_expression(expr
->pos
, expr
->type
);
26 static struct statement
* dup_statement(struct statement
*stmt
)
28 struct statement
*dup
= alloc_statement(stmt
->pos
, stmt
->type
);
33 static struct symbol
*copy_symbol(struct position pos
, struct symbol
*sym
)
37 if (sym
->ctype
.modifiers
& (MOD_STATIC
| MOD_EXTERN
| MOD_TOPLEVEL
| MOD_INLINE
))
40 warn(pos
, "unreplaced symbol '%s'", show_ident(sym
->ident
));
46 static struct symbol_list
*copy_symbol_list(struct symbol_list
*src
)
48 struct symbol_list
*dst
= NULL
;
51 FOR_EACH_PTR(src
, sym
) {
52 struct symbol
*newsym
= copy_symbol(sym
->pos
, sym
);
53 add_symbol(&dst
, newsym
);
58 static struct expression
* copy_expression(struct expression
*expr
)
65 * EXPR_SYMBOL is the interesting case, we may need to replace the
66 * symbol to the new copy.
69 struct symbol
*sym
= copy_symbol(expr
->pos
, expr
->symbol
);
70 if (sym
== expr
->symbol
)
72 expr
= dup_expression(expr
);
77 /* Atomics, never change, just return the expression directly */
83 /* Unops: check if the subexpression is unique */
86 struct expression
*unop
= copy_expression(expr
->unop
);
87 if (expr
->unop
== unop
)
89 expr
= dup_expression(expr
);
94 /* Binops: copy left/right expressions */
99 case EXPR_ASSIGNMENT
: {
100 struct expression
*left
= copy_expression(expr
->left
);
101 struct expression
*right
= copy_expression(expr
->right
);
102 if (left
== expr
->left
&& right
== expr
->right
)
104 expr
= dup_expression(expr
);
112 struct expression
*deref
= copy_expression(expr
->deref
);
113 if (deref
== expr
->deref
)
115 expr
= dup_expression(expr
);
120 /* Cast/sizeof/__alignof__ */
124 struct expression
*cast
= copy_expression(expr
->cast_expression
);
125 if (cast
== expr
->cast_expression
)
127 expr
= dup_expression(expr
);
128 expr
->cast_expression
= cast
;
132 /* Conditional expression */
133 case EXPR_CONDITIONAL
: {
134 struct expression
*cond
= copy_expression(expr
->conditional
);
135 struct expression
*true = copy_expression(expr
->cond_true
);
136 struct expression
*false = copy_expression(expr
->cond_false
);
137 if (cond
== expr
->conditional
&& true == expr
->cond_true
&& false == expr
->cond_false
)
139 expr
= dup_expression(expr
);
140 expr
->conditional
= cond
;
141 expr
->cond_true
= true;
142 expr
->cond_false
= false;
146 /* Statement expression */
147 case EXPR_STATEMENT
: {
148 struct statement
*stmt
= alloc_statement(expr
->pos
, STMT_COMPOUND
);
149 copy_statement(expr
->statement
, stmt
);
150 expr
= dup_expression(expr
);
151 expr
->statement
= stmt
;
155 /* Call expression */
157 struct expression
*fn
= copy_expression(expr
->fn
);
158 struct expression_list
*list
= expr
->args
;
159 struct expression
*arg
;
161 expr
= dup_expression(expr
);
164 FOR_EACH_PTR(list
, arg
) {
165 add_expression(&expr
->args
, copy_expression(arg
));
170 /* Initializer list statement */
171 case EXPR_INITIALIZER
: {
172 struct expression_list
*list
= expr
->expr_list
;
173 struct expression
*entry
;
174 expr
= dup_expression(expr
);
175 expr
->expr_list
= NULL
;
176 FOR_EACH_PTR(list
, entry
) {
177 add_expression(&expr
->expr_list
, copy_expression(entry
));
182 /* Label in inline function - hmm. */
184 struct symbol
*label_symbol
= copy_symbol(expr
->pos
, expr
->label_symbol
);
185 expr
= dup_expression(expr
);
186 expr
->label_symbol
= label_symbol
;
190 /* Identifier in member dereference is unchanged across a fn copy */
191 /* As is an array index expression */
193 case EXPR_IDENTIFIER
:
196 /* Position in initializer.. */
198 struct expression
*val
= copy_expression(expr
->init_expr
);
199 if (val
== expr
->init_expr
)
201 expr
= dup_expression(expr
);
202 expr
->init_expr
= val
;
207 warn(expr
->pos
, "trying to copy expression type %d", expr
->type
);
212 void set_replace(struct symbol
*old
, struct symbol
*new)
218 void unset_replace(struct symbol
*sym
)
220 struct symbol
*r
= sym
->replace
;
222 warn(sym
->pos
, "symbol '%s' not replaced?", show_ident(sym
->ident
));
229 static void unset_replace_list(struct symbol_list
*list
)
232 FOR_EACH_PTR(list
, sym
) {
237 static struct statement
*copy_one_statement(struct statement
*stmt
)
244 case STMT_EXPRESSION
: {
245 struct expression
*expr
= copy_expression(stmt
->expression
);
246 if (expr
== stmt
->expression
)
248 stmt
= dup_statement(stmt
);
249 stmt
->expression
= expr
;
252 case STMT_COMPOUND
: {
253 struct statement
*new = alloc_statement(stmt
->pos
, STMT_COMPOUND
);
254 copy_statement(stmt
, new);
259 struct expression
*cond
= stmt
->if_conditional
;
260 struct statement
*true = stmt
->if_true
;
261 struct statement
*false = stmt
->if_false
;
263 cond
= copy_expression(cond
);
264 true = copy_one_statement(true);
265 false = copy_one_statement(false);
266 if (stmt
->if_conditional
== cond
&&
267 stmt
->if_true
== true &&
268 stmt
->if_false
== false)
270 stmt
= dup_statement(stmt
);
271 stmt
->if_conditional
= cond
;
272 stmt
->if_true
= true;
273 stmt
->if_false
= false;
277 struct expression
*retval
= copy_expression(stmt
->ret_value
);
278 struct symbol
*sym
= copy_symbol(stmt
->pos
, stmt
->ret_target
);
280 stmt
= dup_statement(stmt
);
281 stmt
->ret_value
= retval
;
282 stmt
->ret_target
= sym
;
286 stmt
= dup_statement(stmt
);
287 stmt
->case_label
= copy_symbol(stmt
->pos
, stmt
->case_label
);
288 stmt
->case_expression
= copy_expression(stmt
->case_expression
);
289 stmt
->case_to
= copy_expression(stmt
->case_to
);
290 stmt
->case_statement
= copy_one_statement(stmt
->case_statement
);
294 struct symbol
*switch_break
= copy_symbol(stmt
->pos
, stmt
->switch_break
);
295 struct symbol
*switch_case
= copy_symbol(stmt
->pos
, stmt
->switch_case
);
296 struct expression
*expr
= copy_expression(stmt
->switch_expression
);
297 struct statement
*switch_stmt
= copy_one_statement(stmt
->switch_statement
);
298 stmt
= dup_statement(stmt
);
299 stmt
->switch_break
= switch_break
;
300 stmt
->switch_case
= switch_case
;
301 stmt
->switch_expression
= expr
;
302 stmt
->switch_statement
= switch_stmt
;
305 case STMT_ITERATOR
: {
306 stmt
= dup_statement(stmt
);
307 stmt
->iterator_break
= copy_symbol(stmt
->pos
, stmt
->iterator_break
);
308 stmt
->iterator_continue
= copy_symbol(stmt
->pos
, stmt
->iterator_continue
);
309 stmt
->iterator_syms
= copy_symbol_list(stmt
->iterator_syms
);
311 stmt
->iterator_pre_statement
= copy_one_statement(stmt
->iterator_pre_statement
);
312 stmt
->iterator_pre_condition
= copy_expression(stmt
->iterator_pre_condition
);
314 stmt
->iterator_statement
= copy_one_statement(stmt
->iterator_statement
);
316 stmt
->iterator_post_statement
= copy_one_statement(stmt
->iterator_post_statement
);
317 stmt
->iterator_post_condition
= copy_expression(stmt
->iterator_post_condition
);
321 stmt
= dup_statement(stmt
);
322 stmt
->label_identifier
= copy_symbol(stmt
->pos
, stmt
->label_identifier
);
323 stmt
->label_statement
= copy_one_statement(stmt
->label_statement
);
327 stmt
= dup_statement(stmt
);
328 stmt
->goto_label
= copy_symbol(stmt
->pos
, stmt
->goto_label
);
329 stmt
->goto_expression
= copy_expression(stmt
->goto_expression
);
330 stmt
->target_list
= copy_symbol_list(stmt
->target_list
);
338 warn(stmt
->pos
, "trying to copy statement type %d", stmt
->type
);
345 * Copy a stateemnt tree from 'src' to 'dst', where both
346 * source and destination are of type STMT_COMPOUND.
348 * We do this for the tree-level inliner.
350 * This doesn't do the symbol replacement right: it's not
353 void copy_statement(struct statement
*src
, struct statement
*dst
)
355 struct statement
*stmt
;
358 FOR_EACH_PTR(src
->syms
, sym
) {
359 struct symbol
*newsym
= copy_symbol(src
->pos
, sym
);
360 newsym
->initializer
= copy_expression(sym
->initializer
);
361 add_symbol(&dst
->syms
, newsym
);
364 FOR_EACH_PTR(src
->stmts
, stmt
) {
365 add_statement(&dst
->stmts
, copy_one_statement(stmt
));
368 dst
->ret
= copy_symbol(src
->pos
, src
->ret
);
371 static struct symbol
*create_copy_symbol(struct symbol
*orig
)
373 struct symbol
*sym
= orig
;
375 sym
= alloc_symbol(orig
->pos
, orig
->type
);
377 set_replace(orig
, sym
);
383 static struct symbol_list
*create_symbol_list(struct symbol_list
*src
)
385 struct symbol_list
*dst
= NULL
;
388 FOR_EACH_PTR(src
, sym
) {
389 struct symbol
*newsym
= create_copy_symbol(sym
);
390 add_symbol(&dst
, newsym
);
395 int inline_function(struct expression
*expr
, struct symbol
*sym
)
397 struct symbol_list
* fn_symbol_list
;
398 struct symbol
*fn
= sym
->ctype
.base_type
;
399 struct expression_list
*arg_list
= expr
->args
;
400 struct statement
*stmt
= alloc_statement(expr
->pos
, STMT_COMPOUND
);
401 struct symbol_list
*name_list
;
403 struct expression
*arg
;
406 warn(fn
->pos
, "marked inline, but without a definition");
409 name_list
= fn
->arguments
;
411 stmt
= alloc_statement(expr
->pos
, STMT_COMPOUND
);
413 expr
->type
= EXPR_STATEMENT
;
414 expr
->statement
= stmt
;
415 expr
->ctype
= fn
->ctype
.base_type
;
417 fn_symbol_list
= create_symbol_list(sym
->symbol_list
);
419 PREPARE_PTR_LIST(name_list
, name
);
420 FOR_EACH_PTR(arg_list
, arg
) {
421 struct symbol
*a
= alloc_symbol(arg
->pos
, SYM_NODE
);
423 a
->ctype
.base_type
= arg
->ctype
;
426 set_replace(name
, a
);
427 add_symbol(&fn_symbol_list
, a
);
429 a
->initializer
= arg
;
430 add_symbol(&stmt
->syms
, a
);
434 FINISH_PTR_LIST(name
);
436 copy_statement(fn
->stmt
, stmt
);
438 unset_replace_list(fn_symbol_list
);
440 evaluate_statement(stmt
);