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 */
82 /* Unops: check if the subexpression is unique */
85 struct expression
*unop
= copy_expression(expr
->unop
);
86 if (expr
->unop
== unop
)
88 expr
= dup_expression(expr
);
93 /* Binops: copy left/right expressions */
98 case EXPR_ASSIGNMENT
: {
99 struct expression
*left
= copy_expression(expr
->left
);
100 struct expression
*right
= copy_expression(expr
->right
);
101 if (left
== expr
->left
&& right
== expr
->right
)
103 expr
= dup_expression(expr
);
111 struct expression
*deref
= copy_expression(expr
->deref
);
112 if (deref
== expr
->deref
)
114 expr
= dup_expression(expr
);
122 struct expression
*cast
= copy_expression(expr
->cast_expression
);
123 if (cast
== expr
->cast_expression
)
125 expr
= dup_expression(expr
);
126 expr
->cast_expression
= cast
;
130 /* Conditional expression */
131 case EXPR_CONDITIONAL
: {
132 struct expression
*cond
= copy_expression(expr
->conditional
);
133 struct expression
*true = copy_expression(expr
->cond_true
);
134 struct expression
*false = copy_expression(expr
->cond_false
);
135 if (cond
== expr
->conditional
&& true == expr
->cond_true
&& false == expr
->cond_false
)
137 expr
= dup_expression(expr
);
138 expr
->conditional
= cond
;
139 expr
->cond_true
= true;
140 expr
->cond_false
= false;
144 /* Statement expression */
145 case EXPR_STATEMENT
: {
146 struct statement
*stmt
= alloc_statement(expr
->pos
, STMT_COMPOUND
);
147 copy_statement(expr
->statement
, stmt
);
148 expr
= dup_expression(expr
);
149 expr
->statement
= stmt
;
153 /* Call expression */
155 struct expression
*fn
= copy_expression(expr
->fn
);
156 struct expression_list
*list
= expr
->args
;
157 struct expression
*arg
;
159 expr
= dup_expression(expr
);
162 FOR_EACH_PTR(list
, arg
) {
163 add_expression(&expr
->args
, copy_expression(arg
));
168 /* Initializer list statement */
169 case EXPR_INITIALIZER
: {
170 struct expression_list
*list
= expr
->expr_list
;
171 struct expression
*entry
;
172 expr
= dup_expression(expr
);
173 expr
->expr_list
= NULL
;
174 FOR_EACH_PTR(list
, entry
) {
175 add_expression(&expr
->expr_list
, copy_expression(entry
));
182 warn(expr
->pos
, "trying to copy expression type %d", expr
->type
);
187 void set_replace(struct symbol
*old
, struct symbol
*new)
193 void unset_replace(struct symbol
*sym
)
195 struct symbol
*r
= sym
->replace
;
197 warn(sym
->pos
, "symbol '%s' not replaced?", show_ident(sym
->ident
));
204 static void unset_replace_list(struct symbol_list
*list
)
207 FOR_EACH_PTR(list
, sym
) {
212 static struct statement
*copy_one_statement(struct statement
*stmt
)
219 case STMT_EXPRESSION
: {
220 struct expression
*expr
= copy_expression(stmt
->expression
);
221 if (expr
== stmt
->expression
)
223 stmt
= dup_statement(stmt
);
224 stmt
->expression
= expr
;
227 case STMT_COMPOUND
: {
228 struct statement
*new = alloc_statement(stmt
->pos
, STMT_COMPOUND
);
229 copy_statement(stmt
, new);
234 struct expression
*cond
= stmt
->if_conditional
;
235 struct statement
*true = stmt
->if_true
;
236 struct statement
*false = stmt
->if_false
;
238 cond
= copy_expression(cond
);
239 true = copy_one_statement(true);
240 false = copy_one_statement(false);
241 if (stmt
->if_conditional
== cond
&&
242 stmt
->if_true
== true &&
243 stmt
->if_false
== false)
245 stmt
= dup_statement(stmt
);
246 stmt
->if_conditional
= cond
;
247 stmt
->if_true
= true;
248 stmt
->if_false
= false;
252 struct expression
*retval
= copy_expression(stmt
->ret_value
);
253 struct symbol
*sym
= copy_symbol(stmt
->pos
, stmt
->ret_target
);
255 stmt
= dup_statement(stmt
);
256 stmt
->ret_value
= retval
;
257 stmt
->ret_target
= sym
;
261 stmt
= dup_statement(stmt
);
262 stmt
->case_label
= copy_symbol(stmt
->pos
, stmt
->case_label
);
263 stmt
->case_expression
= copy_expression(stmt
->case_expression
);
264 stmt
->case_to
= copy_expression(stmt
->case_to
);
265 stmt
->case_statement
= copy_one_statement(stmt
->case_statement
);
269 struct symbol
*switch_break
= copy_symbol(stmt
->pos
, stmt
->switch_break
);
270 struct symbol
*switch_case
= copy_symbol(stmt
->pos
, stmt
->switch_case
);
271 struct expression
*expr
= copy_expression(stmt
->switch_expression
);
272 struct statement
*switch_stmt
= copy_one_statement(stmt
->switch_statement
);
273 stmt
= dup_statement(stmt
);
274 stmt
->switch_break
= switch_break
;
275 stmt
->switch_case
= switch_case
;
276 stmt
->switch_expression
= expr
;
277 stmt
->switch_statement
= switch_stmt
;
280 case STMT_ITERATOR
: {
281 stmt
= dup_statement(stmt
);
282 stmt
->iterator_break
= copy_symbol(stmt
->pos
, stmt
->iterator_break
);
283 stmt
->iterator_continue
= copy_symbol(stmt
->pos
, stmt
->iterator_continue
);
284 stmt
->iterator_syms
= copy_symbol_list(stmt
->iterator_syms
);
286 stmt
->iterator_pre_statement
= copy_one_statement(stmt
->iterator_pre_statement
);
287 stmt
->iterator_pre_condition
= copy_expression(stmt
->iterator_pre_condition
);
289 stmt
->iterator_statement
= copy_one_statement(stmt
->iterator_statement
);
291 stmt
->iterator_post_statement
= copy_one_statement(stmt
->iterator_post_statement
);
292 stmt
->iterator_post_condition
= copy_expression(stmt
->iterator_post_condition
);
296 stmt
= dup_statement(stmt
);
297 stmt
->label_identifier
= copy_symbol(stmt
->pos
, stmt
->label_identifier
);
298 stmt
->label_statement
= copy_one_statement(stmt
->label_statement
);
311 warn(stmt
->pos
, "trying to copy statement type %d", stmt
->type
);
318 * Copy a stateemnt tree from 'src' to 'dst', where both
319 * source and destination are of type STMT_COMPOUND.
321 * We do this for the tree-level inliner.
323 * This doesn't do the symbol replacement right: it's not
326 void copy_statement(struct statement
*src
, struct statement
*dst
)
328 struct statement
*stmt
;
331 FOR_EACH_PTR(src
->syms
, sym
) {
332 struct symbol
*newsym
= copy_symbol(src
->pos
, sym
);
333 newsym
->initializer
= copy_expression(sym
->initializer
);
334 add_symbol(&dst
->syms
, newsym
);
337 FOR_EACH_PTR(src
->stmts
, stmt
) {
338 add_statement(&dst
->stmts
, copy_one_statement(stmt
));
341 dst
->ret
= copy_symbol(src
->pos
, src
->ret
);
344 static struct symbol
*create_copy_symbol(struct symbol
*orig
)
346 struct symbol
*sym
= orig
;
348 sym
= alloc_symbol(orig
->pos
, orig
->type
);
349 sym
->ctype
= orig
->ctype
;
350 sym
->initializer
= NULL
;
351 set_replace(orig
, sym
);
356 static struct symbol_list
*create_symbol_list(struct symbol_list
*src
)
358 struct symbol_list
*dst
= NULL
;
361 FOR_EACH_PTR(src
, sym
) {
362 struct symbol
*newsym
= create_copy_symbol(sym
);
363 add_symbol(&dst
, newsym
);
368 int inline_function(struct expression
*expr
, struct symbol
*sym
)
370 struct symbol_list
* fn_symbol_list
;
371 struct symbol
*fn
= sym
->ctype
.base_type
;
372 struct expression_list
*arg_list
= expr
->args
;
373 struct statement
*stmt
= alloc_statement(expr
->pos
, STMT_COMPOUND
);
374 struct symbol_list
*name_list
;
376 struct expression
*arg
;
379 warn(fn
->pos
, "marked inline, but without a definition");
382 name_list
= fn
->arguments
;
384 stmt
= alloc_statement(expr
->pos
, STMT_COMPOUND
);
386 expr
->type
= EXPR_STATEMENT
;
387 expr
->statement
= stmt
;
388 expr
->ctype
= fn
->ctype
.base_type
;
390 fn_symbol_list
= create_symbol_list(sym
->symbol_list
);
392 PREPARE_PTR_LIST(name_list
, name
);
393 FOR_EACH_PTR(arg_list
, arg
) {
394 struct symbol
*a
= alloc_symbol(arg
->pos
, SYM_NODE
);
396 a
->ctype
.base_type
= arg
->ctype
;
398 a
->ident
= name
->ident
;
399 a
->ctype
= name
->ctype
;
400 set_replace(name
, a
);
401 add_symbol(&fn_symbol_list
, a
);
403 a
->initializer
= arg
;
404 add_symbol(&stmt
->syms
, a
);
408 FINISH_PTR_LIST(name
);
410 copy_statement(fn
->stmt
, stmt
);
412 unset_replace_list(fn_symbol_list
);
414 evaluate_statement(stmt
);