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"
36 static struct expression
* dup_expression(struct expression
*expr
)
38 struct expression
*dup
= alloc_expression(expr
->pos
, expr
->type
);
43 static struct statement
* dup_statement(struct statement
*stmt
)
45 struct statement
*dup
= alloc_statement(stmt
->pos
, stmt
->type
);
50 static struct symbol
*copy_symbol(struct position pos
, struct symbol
*sym
)
54 if (sym
->ctype
.modifiers
& (MOD_STATIC
| MOD_EXTERN
| MOD_TOPLEVEL
| MOD_INLINE
))
57 warning(pos
, "unreplaced symbol '%s'", show_ident(sym
->ident
));
63 static struct symbol_list
*copy_symbol_list(struct symbol_list
*src
)
65 struct symbol_list
*dst
= NULL
;
68 FOR_EACH_PTR(src
, sym
) {
69 struct symbol
*newsym
= copy_symbol(sym
->pos
, sym
);
70 add_symbol(&dst
, newsym
);
71 } END_FOR_EACH_PTR(sym
);
75 static struct expression
* copy_expression(struct expression
*expr
)
82 * EXPR_SYMBOL is the interesting case, we may need to replace the
83 * symbol to the new copy.
86 struct symbol
*sym
= copy_symbol(expr
->pos
, expr
->symbol
);
87 if (sym
== expr
->symbol
)
89 expr
= dup_expression(expr
);
94 /* Atomics, never change, just return the expression directly */
101 /* Unops: check if the subexpression is unique */
104 struct expression
*unop
= copy_expression(expr
->unop
);
105 if (expr
->unop
== unop
)
107 expr
= dup_expression(expr
);
113 struct expression
*base
= copy_expression(expr
->base
);
114 expr
= dup_expression(expr
);
119 /* Binops: copy left/right expressions */
124 struct expression
*left
= copy_expression(expr
->left
);
125 struct expression
*right
= copy_expression(expr
->right
);
126 if (left
== expr
->left
&& right
== expr
->right
)
128 expr
= dup_expression(expr
);
134 case EXPR_ASSIGNMENT
: {
135 struct expression
*left
= copy_expression(expr
->left
);
136 struct expression
*right
= copy_expression(expr
->right
);
137 if (expr
->op
== '=' && left
== expr
->left
&& right
== expr
->right
)
139 expr
= dup_expression(expr
);
147 struct expression
*deref
= copy_expression(expr
->deref
);
148 expr
= dup_expression(expr
);
153 /* Cast/sizeof/__alignof__ */
155 if (expr
->cast_expression
->type
== EXPR_INITIALIZER
) {
156 struct expression
*cast
= expr
->cast_expression
;
157 struct symbol
*sym
= expr
->cast_type
;
158 expr
= dup_expression(expr
);
159 expr
->cast_expression
= copy_expression(cast
);
160 expr
->cast_type
= alloc_symbol(sym
->pos
, sym
->type
);
161 *expr
->cast_type
= *sym
;
164 case EXPR_FORCE_CAST
:
165 case EXPR_IMPLIED_CAST
:
169 struct expression
*cast
= copy_expression(expr
->cast_expression
);
170 if (cast
== expr
->cast_expression
)
172 expr
= dup_expression(expr
);
173 expr
->cast_expression
= cast
;
177 /* Conditional expression */
179 case EXPR_CONDITIONAL
: {
180 struct expression
*cond
= copy_expression(expr
->conditional
);
181 struct expression
*true = copy_expression(expr
->cond_true
);
182 struct expression
*false = copy_expression(expr
->cond_false
);
183 if (cond
== expr
->conditional
&& true == expr
->cond_true
&& false == expr
->cond_false
)
185 expr
= dup_expression(expr
);
186 expr
->conditional
= cond
;
187 expr
->cond_true
= true;
188 expr
->cond_false
= false;
192 /* Statement expression */
193 case EXPR_STATEMENT
: {
194 struct statement
*stmt
= alloc_statement(expr
->pos
, STMT_COMPOUND
);
195 copy_statement(expr
->statement
, stmt
);
196 expr
= dup_expression(expr
);
197 expr
->statement
= stmt
;
201 /* Call expression */
203 struct expression
*fn
= copy_expression(expr
->fn
);
204 struct expression_list
*list
= expr
->args
;
205 struct expression
*arg
;
207 expr
= dup_expression(expr
);
210 FOR_EACH_PTR(list
, arg
) {
211 add_expression(&expr
->args
, copy_expression(arg
));
212 } END_FOR_EACH_PTR(arg
);
216 /* Initializer list statement */
217 case EXPR_INITIALIZER
: {
218 struct expression_list
*list
= expr
->expr_list
;
219 struct expression
*entry
;
220 expr
= dup_expression(expr
);
221 expr
->expr_list
= NULL
;
222 FOR_EACH_PTR(list
, entry
) {
223 add_expression(&expr
->expr_list
, copy_expression(entry
));
224 } END_FOR_EACH_PTR(entry
);
228 /* Label in inline function - hmm. */
230 struct symbol
*label_symbol
= copy_symbol(expr
->pos
, expr
->label_symbol
);
231 expr
= dup_expression(expr
);
232 expr
->label_symbol
= label_symbol
;
237 struct expression
*sub_expr
= copy_expression(expr
->idx_expression
);
238 expr
= dup_expression(expr
);
239 expr
->idx_expression
= sub_expr
;
243 case EXPR_IDENTIFIER
: {
244 struct expression
*sub_expr
= copy_expression(expr
->ident_expression
);
245 expr
= dup_expression(expr
);
246 expr
->ident_expression
= sub_expr
;
250 /* Position in initializer.. */
252 struct expression
*val
= copy_expression(expr
->init_expr
);
253 expr
= dup_expression(expr
);
254 expr
->init_expr
= val
;
257 case EXPR_OFFSETOF
: {
258 struct expression
*val
= copy_expression(expr
->down
);
259 if (expr
->op
== '.') {
260 if (expr
->down
!= val
) {
261 expr
= dup_expression(expr
);
265 struct expression
*idx
= copy_expression(expr
->index
);
266 if (expr
->down
!= val
|| expr
->index
!= idx
) {
267 expr
= dup_expression(expr
);
275 warning(expr
->pos
, "trying to copy expression type %d", expr
->type
);
280 static struct expression_list
*copy_asm_constraints(struct expression_list
*in
)
282 struct expression_list
*out
= NULL
;
283 struct expression
*expr
;
286 FOR_EACH_PTR(in
, expr
) {
288 case 0: /* identifier */
289 case 1: /* constraint */
291 add_expression(&out
, expr
);
293 case 2: /* expression */
295 add_expression(&out
, copy_expression(expr
));
298 } END_FOR_EACH_PTR(expr
);
302 static void set_replace(struct symbol
*old
, struct symbol
*new)
308 static void unset_replace(struct symbol
*sym
)
310 struct symbol
*r
= sym
->replace
;
312 warning(sym
->pos
, "symbol '%s' not replaced?", show_ident(sym
->ident
));
319 static void unset_replace_list(struct symbol_list
*list
)
322 FOR_EACH_PTR(list
, sym
) {
324 } END_FOR_EACH_PTR(sym
);
327 static struct statement
*copy_one_statement(struct statement
*stmt
)
334 case STMT_DECLARATION
: {
336 struct statement
*newstmt
= dup_statement(stmt
);
337 newstmt
->declaration
= NULL
;
338 FOR_EACH_PTR(stmt
->declaration
, sym
) {
339 struct symbol
*newsym
= copy_symbol(stmt
->pos
, sym
);
341 newsym
->initializer
= copy_expression(sym
->initializer
);
342 add_symbol(&newstmt
->declaration
, newsym
);
343 } END_FOR_EACH_PTR(sym
);
348 case STMT_EXPRESSION
: {
349 struct expression
*expr
= copy_expression(stmt
->expression
);
350 if (expr
== stmt
->expression
)
352 stmt
= dup_statement(stmt
);
353 stmt
->expression
= expr
;
357 struct expression
*expr
= copy_expression(stmt
->range_expression
);
358 if (expr
== stmt
->expression
)
360 stmt
= dup_statement(stmt
);
361 stmt
->range_expression
= expr
;
364 case STMT_COMPOUND
: {
365 struct statement
*new = alloc_statement(stmt
->pos
, STMT_COMPOUND
);
366 copy_statement(stmt
, new);
371 struct expression
*cond
= stmt
->if_conditional
;
372 struct statement
*true = stmt
->if_true
;
373 struct statement
*false = stmt
->if_false
;
375 cond
= copy_expression(cond
);
376 true = copy_one_statement(true);
377 false = copy_one_statement(false);
378 if (stmt
->if_conditional
== cond
&&
379 stmt
->if_true
== true &&
380 stmt
->if_false
== false)
382 stmt
= dup_statement(stmt
);
383 stmt
->if_conditional
= cond
;
384 stmt
->if_true
= true;
385 stmt
->if_false
= false;
389 struct expression
*retval
= copy_expression(stmt
->ret_value
);
390 struct symbol
*sym
= copy_symbol(stmt
->pos
, stmt
->ret_target
);
392 stmt
= dup_statement(stmt
);
393 stmt
->ret_value
= retval
;
394 stmt
->ret_target
= sym
;
398 stmt
= dup_statement(stmt
);
399 stmt
->case_label
= copy_symbol(stmt
->pos
, stmt
->case_label
);
400 stmt
->case_label
->stmt
= stmt
;
401 stmt
->case_expression
= copy_expression(stmt
->case_expression
);
402 stmt
->case_to
= copy_expression(stmt
->case_to
);
403 stmt
->case_statement
= copy_one_statement(stmt
->case_statement
);
407 struct symbol
*switch_break
= copy_symbol(stmt
->pos
, stmt
->switch_break
);
408 struct symbol
*switch_case
= copy_symbol(stmt
->pos
, stmt
->switch_case
);
409 struct expression
*expr
= copy_expression(stmt
->switch_expression
);
410 struct statement
*switch_stmt
= copy_one_statement(stmt
->switch_statement
);
412 stmt
= dup_statement(stmt
);
413 switch_case
->symbol_list
= copy_symbol_list(switch_case
->symbol_list
);
414 stmt
->switch_break
= switch_break
;
415 stmt
->switch_case
= switch_case
;
416 stmt
->switch_expression
= expr
;
417 stmt
->switch_statement
= switch_stmt
;
420 case STMT_ITERATOR
: {
421 stmt
= dup_statement(stmt
);
422 stmt
->iterator_break
= copy_symbol(stmt
->pos
, stmt
->iterator_break
);
423 stmt
->iterator_continue
= copy_symbol(stmt
->pos
, stmt
->iterator_continue
);
424 stmt
->iterator_syms
= copy_symbol_list(stmt
->iterator_syms
);
426 stmt
->iterator_pre_statement
= copy_one_statement(stmt
->iterator_pre_statement
);
427 stmt
->iterator_pre_condition
= copy_expression(stmt
->iterator_pre_condition
);
429 stmt
->iterator_statement
= copy_one_statement(stmt
->iterator_statement
);
431 stmt
->iterator_post_statement
= copy_one_statement(stmt
->iterator_post_statement
);
432 stmt
->iterator_post_condition
= copy_expression(stmt
->iterator_post_condition
);
436 stmt
= dup_statement(stmt
);
437 stmt
->label_identifier
= copy_symbol(stmt
->pos
, stmt
->label_identifier
);
438 stmt
->label_statement
= copy_one_statement(stmt
->label_statement
);
442 stmt
= dup_statement(stmt
);
443 stmt
->goto_label
= copy_symbol(stmt
->pos
, stmt
->goto_label
);
444 stmt
->goto_expression
= copy_expression(stmt
->goto_expression
);
445 stmt
->target_list
= copy_symbol_list(stmt
->target_list
);
449 stmt
= dup_statement(stmt
);
450 stmt
->asm_inputs
= copy_asm_constraints(stmt
->asm_inputs
);
451 stmt
->asm_outputs
= copy_asm_constraints(stmt
->asm_outputs
);
452 /* no need to dup "clobbers", since they are all constant strings */
456 warning(stmt
->pos
, "trying to copy statement type %d", stmt
->type
);
463 * Copy a statement tree from 'src' to 'dst', where both
464 * source and destination are of type STMT_COMPOUND.
466 * We do this for the tree-level inliner.
468 * This doesn't do the symbol replacement right: it's not
471 void copy_statement(struct statement
*src
, struct statement
*dst
)
473 struct statement
*stmt
;
475 FOR_EACH_PTR(src
->stmts
, stmt
) {
476 add_statement(&dst
->stmts
, copy_one_statement(stmt
));
477 } END_FOR_EACH_PTR(stmt
);
478 dst
->args
= copy_one_statement(src
->args
);
479 dst
->ret
= copy_symbol(src
->pos
, src
->ret
);
480 dst
->inline_fn
= src
->inline_fn
;
483 static struct symbol
*create_copy_symbol(struct symbol
*orig
)
485 struct symbol
*sym
= orig
;
487 sym
= alloc_symbol(orig
->pos
, orig
->type
);
489 sym
->bb_target
= NULL
;
491 set_replace(orig
, sym
);
497 static struct symbol_list
*create_symbol_list(struct symbol_list
*src
)
499 struct symbol_list
*dst
= NULL
;
502 FOR_EACH_PTR(src
, sym
) {
503 struct symbol
*newsym
= create_copy_symbol(sym
);
504 add_symbol(&dst
, newsym
);
505 } END_FOR_EACH_PTR(sym
);
509 int inline_function(struct expression
*expr
, struct symbol
*sym
)
511 struct symbol_list
* fn_symbol_list
;
512 struct symbol
*fn
= sym
->ctype
.base_type
;
513 struct expression_list
*arg_list
= expr
->args
;
514 struct statement
*stmt
= alloc_statement(expr
->pos
, STMT_COMPOUND
);
515 struct symbol_list
*name_list
, *arg_decl
;
517 struct expression
*arg
;
519 if (!fn
->inline_stmt
) {
520 sparse_error(fn
->pos
, "marked inline, but without a definition");
528 name_list
= fn
->arguments
;
530 expr
->type
= EXPR_STATEMENT
;
531 expr
->statement
= stmt
;
532 expr
->ctype
= fn
->ctype
.base_type
;
534 fn_symbol_list
= create_symbol_list(sym
->inline_symbol_list
);
537 PREPARE_PTR_LIST(name_list
, name
);
538 FOR_EACH_PTR(arg_list
, arg
) {
539 struct symbol
*a
= alloc_symbol(arg
->pos
, SYM_NODE
);
541 a
->ctype
.base_type
= arg
->ctype
;
544 set_replace(name
, a
);
545 add_symbol(&fn_symbol_list
, a
);
547 a
->initializer
= arg
;
548 add_symbol(&arg_decl
, a
);
551 } END_FOR_EACH_PTR(arg
);
552 FINISH_PTR_LIST(name
);
554 copy_statement(fn
->inline_stmt
, stmt
);
557 struct statement
*decl
= alloc_statement(expr
->pos
, STMT_DECLARATION
);
558 decl
->declaration
= arg_decl
;
561 stmt
->inline_fn
= sym
;
563 unset_replace_list(fn_symbol_list
);
565 evaluate_statement(stmt
);
571 void uninline(struct symbol
*sym
)
573 struct symbol
*fn
= sym
->ctype
.base_type
;
574 struct symbol_list
*arg_list
= fn
->arguments
;
577 sym
->symbol_list
= create_symbol_list(sym
->inline_symbol_list
);
578 FOR_EACH_PTR(arg_list
, p
) {
580 } END_FOR_EACH_PTR(p
);
581 fn
->stmt
= alloc_statement(fn
->pos
, STMT_COMPOUND
);
582 copy_statement(fn
->inline_stmt
, fn
->stmt
);
583 unset_replace_list(sym
->symbol_list
);
584 unset_replace_list(arg_list
);