2 * Sparse - a semantic source parser.
4 * Copyright (C) 2003 Transmeta Corp.
5 * 2003-2004 Linus Torvalds
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
34 #include "expression.h"
37 static void copy_statement(struct statement
*src
, struct statement
*dst
);
39 static struct expression
* dup_expression(struct expression
*expr
)
41 struct expression
*dup
= alloc_expression(expr
->pos
, expr
->type
);
46 static struct statement
* dup_statement(struct statement
*stmt
)
48 struct statement
*dup
= alloc_statement(stmt
->pos
, stmt
->type
);
53 static struct symbol
*copy_symbol(struct position pos
, struct symbol
*sym
)
57 if (sym
->ctype
.modifiers
& (MOD_STATIC
| MOD_EXTERN
| MOD_TOPLEVEL
| MOD_INLINE
))
60 warning(pos
, "unreplaced symbol '%s'", show_ident(sym
->ident
));
66 static struct symbol_list
*copy_symbol_list(struct symbol_list
*src
)
68 struct symbol_list
*dst
= NULL
;
71 FOR_EACH_PTR(src
, sym
) {
72 struct symbol
*newsym
= copy_symbol(sym
->pos
, sym
);
73 add_symbol(&dst
, newsym
);
74 } END_FOR_EACH_PTR(sym
);
78 static struct expression
* copy_expression(struct expression
*expr
)
85 * EXPR_SYMBOL is the interesting case, we may need to replace the
86 * symbol to the new copy.
89 struct symbol
*sym
= copy_symbol(expr
->pos
, expr
->symbol
);
90 if (sym
== expr
->symbol
)
92 expr
= dup_expression(expr
);
97 /* Atomics, never change, just return the expression directly */
104 /* Unops: check if the subexpression is unique */
107 struct expression
*unop
= copy_expression(expr
->unop
);
108 if (expr
->unop
== unop
)
110 expr
= dup_expression(expr
);
116 struct expression
*base
= copy_expression(expr
->base
);
117 expr
= dup_expression(expr
);
122 /* Binops: copy left/right expressions */
127 struct expression
*left
= copy_expression(expr
->left
);
128 struct expression
*right
= copy_expression(expr
->right
);
129 if (left
== expr
->left
&& right
== expr
->right
)
131 expr
= dup_expression(expr
);
137 case EXPR_ASSIGNMENT
: {
138 struct expression
*left
= copy_expression(expr
->left
);
139 struct expression
*right
= copy_expression(expr
->right
);
140 if (expr
->op
== '=' && left
== expr
->left
&& right
== expr
->right
)
142 expr
= dup_expression(expr
);
150 struct expression
*deref
= copy_expression(expr
->deref
);
151 expr
= dup_expression(expr
);
156 /* Cast/sizeof/__alignof__ */
158 if (expr
->cast_expression
->type
== EXPR_INITIALIZER
) {
159 struct expression
*cast
= expr
->cast_expression
;
160 struct symbol
*sym
= expr
->cast_type
;
161 expr
= dup_expression(expr
);
162 expr
->cast_expression
= copy_expression(cast
);
163 expr
->cast_type
= alloc_symbol(sym
->pos
, sym
->type
);
164 *expr
->cast_type
= *sym
;
167 case EXPR_FORCE_CAST
:
168 case EXPR_IMPLIED_CAST
:
172 struct expression
*cast
= copy_expression(expr
->cast_expression
);
173 if (cast
== expr
->cast_expression
)
175 expr
= dup_expression(expr
);
176 expr
->cast_expression
= cast
;
180 /* Conditional expression */
182 case EXPR_CONDITIONAL
: {
183 struct expression
*cond
= copy_expression(expr
->conditional
);
184 struct expression
*valt
= copy_expression(expr
->cond_true
);
185 struct expression
*valf
= copy_expression(expr
->cond_false
);
186 if (cond
== expr
->conditional
&& valt
== expr
->cond_true
&& valf
== expr
->cond_false
)
188 expr
= dup_expression(expr
);
189 expr
->conditional
= cond
;
190 expr
->cond_true
= valt
;
191 expr
->cond_false
= valf
;
195 /* Statement expression */
196 case EXPR_STATEMENT
: {
197 struct statement
*stmt
= alloc_statement(expr
->pos
, STMT_COMPOUND
);
198 copy_statement(expr
->statement
, stmt
);
199 expr
= dup_expression(expr
);
200 expr
->statement
= stmt
;
204 /* Call expression */
206 struct expression
*fn
= copy_expression(expr
->fn
);
207 struct expression_list
*list
= expr
->args
;
208 struct expression
*arg
;
210 expr
= dup_expression(expr
);
213 FOR_EACH_PTR(list
, arg
) {
214 add_expression(&expr
->args
, copy_expression(arg
));
215 } END_FOR_EACH_PTR(arg
);
219 /* Initializer list statement */
220 case EXPR_INITIALIZER
: {
221 struct expression_list
*list
= expr
->expr_list
;
222 struct expression
*entry
;
223 expr
= dup_expression(expr
);
224 expr
->expr_list
= NULL
;
225 FOR_EACH_PTR(list
, entry
) {
226 add_expression(&expr
->expr_list
, copy_expression(entry
));
227 } END_FOR_EACH_PTR(entry
);
231 /* Label in inline function - hmm. */
233 struct symbol
*label_symbol
= copy_symbol(expr
->pos
, expr
->label_symbol
);
234 expr
= dup_expression(expr
);
235 expr
->label_symbol
= label_symbol
;
240 struct expression
*sub_expr
= copy_expression(expr
->idx_expression
);
241 expr
= dup_expression(expr
);
242 expr
->idx_expression
= sub_expr
;
246 case EXPR_IDENTIFIER
: {
247 struct expression
*sub_expr
= copy_expression(expr
->ident_expression
);
248 expr
= dup_expression(expr
);
249 expr
->ident_expression
= sub_expr
;
253 /* Position in initializer.. */
255 struct expression
*val
= copy_expression(expr
->init_expr
);
256 expr
= dup_expression(expr
);
257 expr
->init_expr
= val
;
260 case EXPR_OFFSETOF
: {
261 struct expression
*val
= copy_expression(expr
->down
);
262 if (expr
->op
== '.') {
263 if (expr
->down
!= val
) {
264 expr
= dup_expression(expr
);
268 struct expression
*idx
= copy_expression(expr
->index
);
269 if (expr
->down
!= val
|| expr
->index
!= idx
) {
270 expr
= dup_expression(expr
);
278 warning(expr
->pos
, "trying to copy expression type %d", expr
->type
);
283 static struct asm_operand_list
*copy_asm_operands(struct asm_operand_list
*in
)
285 struct asm_operand_list
*out
= NULL
;
286 struct asm_operand
*old
;
288 FOR_EACH_PTR(in
, old
) {
289 struct asm_operand
*new = __alloc_asm_operand(0);
290 new->name
= old
->name
;
291 new->constraint
= copy_expression(old
->constraint
);
292 new->expr
= copy_expression(old
->expr
);
293 add_ptr_list(&out
, new);
294 } END_FOR_EACH_PTR(old
);
298 static void set_replace(struct symbol
*old
, struct symbol
*new)
304 static void unset_replace(struct symbol
*sym
)
306 struct symbol
*r
= sym
->replace
;
308 warning(sym
->pos
, "symbol '%s' not replaced?", show_ident(sym
->ident
));
315 static void unset_replace_list(struct symbol_list
*list
)
318 FOR_EACH_PTR(list
, sym
) {
320 } END_FOR_EACH_PTR(sym
);
323 static struct statement
*copy_one_statement(struct statement
*stmt
)
330 case STMT_DECLARATION
: {
332 struct statement
*newstmt
= dup_statement(stmt
);
333 newstmt
->declaration
= NULL
;
334 FOR_EACH_PTR(stmt
->declaration
, sym
) {
335 struct symbol
*newsym
= copy_symbol(stmt
->pos
, sym
);
337 newsym
->initializer
= copy_expression(sym
->initializer
);
338 add_symbol(&newstmt
->declaration
, newsym
);
339 } END_FOR_EACH_PTR(sym
);
344 case STMT_EXPRESSION
: {
345 struct expression
*expr
= copy_expression(stmt
->expression
);
346 if (expr
== stmt
->expression
)
348 stmt
= dup_statement(stmt
);
349 stmt
->expression
= expr
;
353 struct expression
*expr
= copy_expression(stmt
->range_expression
);
354 if (expr
== stmt
->expression
)
356 stmt
= dup_statement(stmt
);
357 stmt
->range_expression
= expr
;
360 case STMT_COMPOUND
: {
361 struct statement
*new = alloc_statement(stmt
->pos
, STMT_COMPOUND
);
362 copy_statement(stmt
, new);
367 struct expression
*cond
= stmt
->if_conditional
;
368 struct statement
*valt
= stmt
->if_true
;
369 struct statement
*valf
= stmt
->if_false
;
371 cond
= copy_expression(cond
);
372 valt
= copy_one_statement(valt
);
373 valf
= copy_one_statement(valf
);
374 if (stmt
->if_conditional
== cond
&&
375 stmt
->if_true
== valt
&&
376 stmt
->if_false
== valf
)
378 stmt
= dup_statement(stmt
);
379 stmt
->if_conditional
= cond
;
380 stmt
->if_true
= valt
;
381 stmt
->if_false
= valf
;
385 struct expression
*retval
= copy_expression(stmt
->ret_value
);
386 struct symbol
*sym
= copy_symbol(stmt
->pos
, stmt
->ret_target
);
388 stmt
= dup_statement(stmt
);
389 stmt
->ret_value
= retval
;
390 stmt
->ret_target
= sym
;
394 stmt
= dup_statement(stmt
);
395 stmt
->case_label
= copy_symbol(stmt
->pos
, stmt
->case_label
);
396 stmt
->case_label
->stmt
= stmt
;
397 stmt
->case_expression
= copy_expression(stmt
->case_expression
);
398 stmt
->case_to
= copy_expression(stmt
->case_to
);
399 stmt
->case_statement
= copy_one_statement(stmt
->case_statement
);
403 struct symbol
*switch_break
= copy_symbol(stmt
->pos
, stmt
->switch_break
);
404 struct symbol
*switch_case
= copy_symbol(stmt
->pos
, stmt
->switch_case
);
405 struct expression
*expr
= copy_expression(stmt
->switch_expression
);
406 struct statement
*switch_stmt
= copy_one_statement(stmt
->switch_statement
);
408 stmt
= dup_statement(stmt
);
409 switch_case
->symbol_list
= copy_symbol_list(switch_case
->symbol_list
);
410 stmt
->switch_break
= switch_break
;
411 stmt
->switch_case
= switch_case
;
412 stmt
->switch_expression
= expr
;
413 stmt
->switch_statement
= switch_stmt
;
416 case STMT_ITERATOR
: {
417 stmt
= dup_statement(stmt
);
418 stmt
->iterator_break
= copy_symbol(stmt
->pos
, stmt
->iterator_break
);
419 stmt
->iterator_continue
= copy_symbol(stmt
->pos
, stmt
->iterator_continue
);
420 stmt
->iterator_syms
= copy_symbol_list(stmt
->iterator_syms
);
422 stmt
->iterator_pre_statement
= copy_one_statement(stmt
->iterator_pre_statement
);
423 stmt
->iterator_pre_condition
= copy_expression(stmt
->iterator_pre_condition
);
425 stmt
->iterator_statement
= copy_one_statement(stmt
->iterator_statement
);
427 stmt
->iterator_post_statement
= copy_one_statement(stmt
->iterator_post_statement
);
428 stmt
->iterator_post_condition
= copy_expression(stmt
->iterator_post_condition
);
432 stmt
= dup_statement(stmt
);
433 stmt
->label_identifier
= copy_symbol(stmt
->pos
, stmt
->label_identifier
);
434 stmt
->label_statement
= copy_one_statement(stmt
->label_statement
);
438 stmt
= dup_statement(stmt
);
439 stmt
->goto_label
= copy_symbol(stmt
->pos
, stmt
->goto_label
);
440 stmt
->goto_expression
= copy_expression(stmt
->goto_expression
);
441 stmt
->target_list
= copy_symbol_list(stmt
->target_list
);
445 stmt
= dup_statement(stmt
);
446 stmt
->asm_inputs
= copy_asm_operands(stmt
->asm_inputs
);
447 stmt
->asm_outputs
= copy_asm_operands(stmt
->asm_outputs
);
448 /* no need to dup "clobbers", since they are all constant strings */
452 warning(stmt
->pos
, "trying to copy statement type %d", stmt
->type
);
459 * Copy a statement tree from 'src' to 'dst', where both
460 * source and destination are of type STMT_COMPOUND.
462 * We do this for the tree-level inliner.
464 * This doesn't do the symbol replacement right: it's not
467 static void copy_statement(struct statement
*src
, struct statement
*dst
)
469 struct statement
*stmt
;
471 FOR_EACH_PTR(src
->stmts
, stmt
) {
472 add_statement(&dst
->stmts
, copy_one_statement(stmt
));
473 } END_FOR_EACH_PTR(stmt
);
474 dst
->args
= copy_one_statement(src
->args
);
475 dst
->ret
= copy_symbol(src
->pos
, src
->ret
);
476 dst
->inline_fn
= src
->inline_fn
;
479 static struct symbol
*create_copy_symbol(struct symbol
*orig
)
481 struct symbol
*sym
= orig
;
483 sym
= alloc_symbol(orig
->pos
, orig
->type
);
485 sym
->bb_target
= NULL
;
487 set_replace(orig
, sym
);
493 static struct symbol_list
*create_symbol_list(struct symbol_list
*src
)
495 struct symbol_list
*dst
= NULL
;
498 FOR_EACH_PTR(src
, sym
) {
499 struct symbol
*newsym
= create_copy_symbol(sym
);
500 add_symbol(&dst
, newsym
);
501 } END_FOR_EACH_PTR(sym
);
505 int inline_function(struct expression
*expr
, struct symbol
*sym
)
507 struct symbol_list
* fn_symbol_list
;
508 struct symbol
*fn
= sym
->ctype
.base_type
;
509 struct expression_list
*arg_list
= expr
->args
;
510 struct statement
*stmt
= alloc_statement(expr
->pos
, STMT_COMPOUND
);
511 struct symbol_list
*name_list
, *arg_decl
;
513 struct expression
*arg
;
515 if (!fn
->inline_stmt
) {
516 sparse_error(fn
->pos
, "marked inline, but without a definition");
522 name_list
= fn
->arguments
;
524 expr
->type
= EXPR_STATEMENT
;
525 expr
->statement
= stmt
;
526 expr
->ctype
= fn
->ctype
.base_type
;
528 fn_symbol_list
= create_symbol_list(sym
->inline_symbol_list
);
531 PREPARE_PTR_LIST(name_list
, name
);
532 FOR_EACH_PTR(arg_list
, arg
) {
533 struct symbol
*a
= alloc_symbol(arg
->pos
, SYM_NODE
);
535 a
->ctype
.base_type
= arg
->ctype
;
538 set_replace(name
, a
);
539 add_symbol(&fn_symbol_list
, a
);
541 a
->initializer
= arg
;
542 add_symbol(&arg_decl
, a
);
545 } END_FOR_EACH_PTR(arg
);
546 FINISH_PTR_LIST(name
);
548 copy_statement(fn
->inline_stmt
, stmt
);
551 struct statement
*decl
= alloc_statement(expr
->pos
, STMT_DECLARATION
);
552 decl
->declaration
= arg_decl
;
555 stmt
->inline_fn
= sym
;
557 unset_replace_list(fn_symbol_list
);
562 void uninline(struct symbol
*sym
)
564 struct symbol
*fn
= sym
->ctype
.base_type
;
565 struct symbol_list
*arg_list
= fn
->arguments
;
568 sym
->symbol_list
= create_symbol_list(sym
->inline_symbol_list
);
569 FOR_EACH_PTR(arg_list
, p
) {
571 } END_FOR_EACH_PTR(p
);
572 fn
->stmt
= alloc_statement(fn
->pos
, STMT_COMPOUND
);
573 copy_statement(fn
->inline_stmt
, fn
->stmt
);
574 unset_replace_list(sym
->symbol_list
);
575 unset_replace_list(arg_list
);