2 * Linearize - walk the statement tree (but _not_ the expressions)
3 * to generate a linear version of it and the basic blocks.
5 * NOTE! We're not interested in the actual sub-expressions yet,
6 * even though they can generate conditional branches and
7 * subroutine calls. That's all "local" behaviour.
9 * Copyright (C) 2004 Linus Torvalds
10 * Copyright (C) 2004 Christopher Li
19 #include "expression.h"
20 #include "linearize.h"
22 pseudo_t
linearize_statement(struct entrypoint
*ep
, struct statement
*stmt
);
23 pseudo_t
linearize_expression(struct entrypoint
*ep
, struct expression
*expr
);
25 static void add_setcc(struct entrypoint
*ep
, struct expression
*expr
, pseudo_t val
);
26 static pseudo_t
add_binary_op(struct entrypoint
*ep
, struct expression
*expr
, int op
, pseudo_t left
, pseudo_t right
);
27 static pseudo_t
add_setval(struct entrypoint
*ep
, struct symbol
*ctype
, struct expression
*val
);
28 static pseudo_t
add_const_value(struct entrypoint
*ep
, struct position pos
, struct symbol
*ctype
, int val
);
29 static pseudo_t
add_load(struct entrypoint
*ep
, struct expression
*expr
, pseudo_t addr
);
32 struct pseudo void_pseudo
= {};
34 static struct instruction
*alloc_instruction(int opcode
, struct symbol
*type
)
36 struct instruction
* insn
= __alloc_instruction(0);
38 insn
->opcode
= opcode
;
42 static struct entrypoint
*alloc_entrypoint(void)
44 return __alloc_entrypoint(0);
47 static struct basic_block
*alloc_basic_block(void)
49 return __alloc_basic_block(0);
52 static struct multijmp
* alloc_multijmp(struct basic_block
*target
, int begin
, int end
)
54 struct multijmp
*multijmp
= __alloc_multijmp(0);
55 multijmp
->target
= target
;
56 multijmp
->begin
= begin
;
61 static struct phi
* alloc_phi(struct basic_block
*source
, pseudo_t pseudo
)
63 struct phi
*phi
= __alloc_phi(0);
69 static void show_instruction(struct instruction
*insn
)
71 int op
= insn
->opcode
;
75 printf("\tAIEEE! (%d %d)\n", insn
->target
->nr
, insn
->src
->nr
);
78 if (insn
->type
&& insn
->type
!= &void_ctype
)
79 printf("\tret %%r%d\n", insn
->src
->nr
);
84 if (insn
->bb_true
&& insn
->bb_false
) {
85 printf("\tbr\t%%r%d, .L%p, .L%p\n", insn
->cond
->nr
, insn
->bb_true
, insn
->bb_false
);
88 printf("\tbr\t.L%p\n", insn
->bb_true
? insn
->bb_true
: insn
->bb_false
);
92 struct expression
*expr
= insn
->val
;
95 printf("\t%%r%d <- %lld\n",
96 insn
->target
->nr
, expr
->value
);
99 printf("\t%%r%d <- %Lf\n",
100 insn
->target
->nr
, expr
->fvalue
);
103 printf("\t%%r%d <- %s\n",
104 insn
->target
->nr
, show_string(expr
->string
));
107 printf("\t%%r%d <- %s\n",
108 insn
->target
->nr
, show_ident(expr
->symbol
->ident
));
111 printf("\t%%r%d <- .L%p\n",
112 insn
->target
->nr
, expr
->symbol
->bb_target
);
115 printf("\t%%r%d <- SETVAL EXPR TYPE %d\n",
116 insn
->target
->nr
, expr
->type
);
121 struct multijmp
*jmp
;
122 printf("\tswitch %%r%d", insn
->cond
->nr
);
123 FOR_EACH_PTR(insn
->multijmp_list
, jmp
) {
124 if (jmp
->begin
== jmp
->end
)
125 printf(", %d -> .L%p", jmp
->begin
, jmp
->target
);
126 else if (jmp
->begin
< jmp
->end
)
127 printf(", %d ... %d -> .L%p", jmp
->begin
, jmp
->end
, jmp
->target
);
129 printf(", default -> .L%p\n", jmp
->target
);
134 case OP_COMPUTEDGOTO
: {
135 struct multijmp
*jmp
;
136 printf("\tjmp *%%r%d", insn
->target
->nr
);
137 FOR_EACH_PTR(insn
->multijmp_list
, jmp
) {
138 printf(", .L%p", jmp
->target
);
147 printf("\t%%r%d <- phi", insn
->target
->nr
);
148 FOR_EACH_PTR(insn
->phi_list
, phi
) {
149 printf("%s(%%r%d, .L%p)", s
, phi
->pseudo
->nr
, phi
->source
);
156 printf("\tload %%r%d <- [%%r%d]\n", insn
->target
->nr
, insn
->src
->nr
);
159 printf("\tstore %%r%d -> [%%r%d]\n", insn
->target
->nr
, insn
->src
->nr
);
163 printf("\t%%r%d <- CALL %%r%d", insn
->target
->nr
, insn
->func
->nr
);
164 FOR_EACH_PTR(insn
->arguments
, arg
) {
165 printf(", %%r%d", arg
->nr
);
171 printf("\t%%r%d <- CAST(%d->%d) %%r%d\n",
173 insn
->orig_type
->bit_size
, insn
->type
->bit_size
,
176 case OP_BINARY
... OP_BINARY_END
: {
177 static const char *opname
[] = {
178 [OP_ADD
- OP_BINARY
] = "add", [OP_SUB
- OP_BINARY
] = "sub",
179 [OP_MUL
- OP_BINARY
] = "mul", [OP_DIV
- OP_BINARY
] = "div",
180 [OP_MOD
- OP_BINARY
] = "mod", [OP_AND
- OP_BINARY
] = "and",
181 [OP_OR
- OP_BINARY
] = "or", [OP_XOR
- OP_BINARY
] = "xor",
182 [OP_SHL
- OP_BINARY
] = "shl", [OP_SHR
- OP_BINARY
] = "shr",
183 [OP_AND_BOOL
- OP_BINARY
] = "and-bool",
184 [OP_OR_BOOL
- OP_BINARY
] = "or-bool",
185 [OP_SEL
- OP_BINARY
] = "select",
187 printf("\t%%r%d <- %s %%r%d, %%r%d\n",
189 opname
[op
- OP_BINARY
], insn
->src1
->nr
, insn
->src2
->nr
);
193 case OP_BINCMP
... OP_BINCMP_END
: {
194 static const char *opname
[] = {
195 [OP_SET_EQ
- OP_BINCMP
] = "seteq",
196 [OP_SET_NE
- OP_BINCMP
] = "setne",
197 [OP_SET_LE
- OP_BINCMP
] = "setle",
198 [OP_SET_GE
- OP_BINCMP
] = "setge",
199 [OP_SET_LT
- OP_BINCMP
] = "setlt",
200 [OP_SET_GT
- OP_BINCMP
] = "setgt",
201 [OP_SET_BE
- OP_BINCMP
] = "setbe",
202 [OP_SET_AE
- OP_BINCMP
] = "setae",
203 [OP_SET_A
- OP_BINCMP
] = "seta",
204 [OP_SET_B
- OP_BINCMP
] = "setb",
206 printf("\t%%r%d <- %s %%r%d, %%r%d\n",
208 opname
[op
- OP_BINCMP
], insn
->src1
->nr
, insn
->src2
->nr
);
212 case OP_NOT
: case OP_NEG
:
213 printf("\t%%r%d <- %s %%r%d\n",
215 op
== OP_NOT
? "not" : "neg", insn
->src1
->nr
);
218 printf("\tsetcc %%r%d\n", insn
->src
->nr
);
221 printf("\top %d ???\n", op
);
225 static void show_bb(struct basic_block
*bb
)
227 struct instruction
*insn
;
229 printf("bb: %p\n", bb
);
231 struct basic_block
*from
;
232 FOR_EACH_PTR(bb
->parents
, from
) {
233 printf(" **from %p**\n", from
);
236 FOR_EACH_PTR(bb
->insns
, insn
) {
237 show_instruction(insn
);
239 if (!bb_terminated(bb
))
244 void show_entry(struct entrypoint
*ep
)
247 struct basic_block
*bb
;
249 printf("ep %p: %s\n", ep
, show_ident(ep
->name
->ident
));
251 FOR_EACH_PTR(ep
->syms
, sym
) {
252 printf(" sym: %p %s\n", sym
, show_ident(sym
->ident
));
257 FOR_EACH_PTR(ep
->bbs
, bb
) {
264 static void bind_label(struct symbol
*label
, struct basic_block
*bb
, struct position pos
)
266 if (label
->bb_target
)
267 warn(pos
, "label '%s' already bound", show_ident(label
->ident
));
268 label
->bb_target
= bb
;
271 static struct basic_block
* get_bound_block(struct entrypoint
*ep
, struct symbol
*label
)
273 struct basic_block
*bb
= label
->bb_target
;
276 label
->bb_target
= bb
= alloc_basic_block();
277 bb
->flags
|= BB_REACHABLE
;
282 static void finish_block(struct entrypoint
*ep
)
284 struct basic_block
*src
= ep
->active
;
285 if (bb_reachable(src
))
289 static void add_goto(struct entrypoint
*ep
, struct basic_block
*dst
)
291 struct basic_block
*src
= ep
->active
;
292 if (bb_reachable(src
)) {
293 struct instruction
*br
= alloc_instruction(OP_BR
, NULL
);
295 add_bb(&dst
->parents
, src
);
296 add_instruction(&src
->insns
, br
);
301 static void add_one_insn(struct entrypoint
*ep
, struct position pos
, struct instruction
*insn
)
303 struct basic_block
*bb
= ep
->active
;
305 if (bb_reachable(bb
))
306 add_instruction(&bb
->insns
, insn
);
309 static void set_activeblock(struct entrypoint
*ep
, struct basic_block
*bb
)
311 if (!bb_terminated(ep
->active
))
315 if (bb_reachable(bb
))
316 add_bb(&ep
->bbs
, bb
);
319 static void add_setcc(struct entrypoint
*ep
, struct expression
*expr
, pseudo_t val
)
321 struct basic_block
*bb
= ep
->active
;
323 if (bb_reachable(bb
)) {
324 struct instruction
*cc
= alloc_instruction(OP_SETCC
, &bool_ctype
);
326 add_one_insn(ep
, expr
->pos
, cc
);
330 static void add_branch(struct entrypoint
*ep
, struct expression
*expr
, pseudo_t cond
, struct basic_block
*bb_true
, struct basic_block
*bb_false
)
332 struct basic_block
*bb
= ep
->active
;
333 struct instruction
*br
;
335 if (bb_reachable(bb
)) {
336 br
= alloc_instruction(OP_BR
, expr
->ctype
);
338 br
->bb_true
= bb_true
;
339 br
->bb_false
= bb_false
;
340 add_bb(&bb_true
->parents
, bb
);
341 add_bb(&bb_false
->parents
, bb
);
342 add_one_insn(ep
, expr
->pos
, br
);
346 /* Dummy pseudo allocator */
347 static pseudo_t
alloc_pseudo(void)
350 struct pseudo
* pseudo
= __alloc_pseudo(0);
356 * FIXME! Not all accesses are memory loads. We should
357 * check what kind of symbol is behind the dereference.
359 static pseudo_t
linearize_address_gen(struct entrypoint
*ep
, struct expression
*expr
)
361 if (expr
->type
== EXPR_PREOP
)
362 return linearize_expression(ep
, expr
->unop
);
363 if (expr
->type
== EXPR_BITFIELD
)
364 return linearize_expression(ep
, expr
->address
);
365 warn(expr
->pos
, "generating address of non-lvalue");
369 static void linearize_store_gen(struct entrypoint
*ep
, pseudo_t value
, struct expression
*expr
, pseudo_t addr
)
371 struct instruction
*store
= alloc_instruction(OP_STORE
, expr
->ctype
);
373 if (expr
->type
== EXPR_BITFIELD
) {
374 unsigned long mask
= ((1<<expr
->nrbits
)-1) << expr
->bitpos
;
375 pseudo_t andmask
, ormask
, shift
, orig
;
377 shift
= add_const_value(ep
, expr
->pos
, &uint_ctype
, expr
->bitpos
);
378 value
= add_binary_op(ep
, expr
, OP_SHL
, value
, shift
);
380 orig
= add_load(ep
, expr
, addr
);
381 andmask
= add_const_value(ep
, expr
->pos
, &uint_ctype
, ~mask
);
382 value
= add_binary_op(ep
, expr
, OP_AND
, orig
, andmask
);
383 ormask
= add_const_value(ep
, expr
->pos
, &uint_ctype
, mask
);
384 value
= add_binary_op(ep
, expr
, OP_OR
, orig
, ormask
);
387 store
->target
= value
;
389 add_one_insn(ep
, expr
->pos
, store
);
392 static pseudo_t
add_binary_op(struct entrypoint
*ep
, struct expression
*expr
, int op
, pseudo_t left
, pseudo_t right
)
394 struct instruction
*insn
= alloc_instruction(op
, expr
->ctype
);
395 pseudo_t target
= alloc_pseudo();
396 insn
->target
= target
;
399 add_one_insn(ep
, expr
->pos
, insn
);
403 static pseudo_t
add_setval(struct entrypoint
*ep
, struct symbol
*ctype
, struct expression
*val
)
405 struct instruction
*insn
= alloc_instruction(OP_SETVAL
, ctype
);
406 pseudo_t target
= alloc_pseudo();
407 insn
->target
= target
;
409 add_one_insn(ep
, val
->pos
, insn
);
413 static pseudo_t
add_const_value(struct entrypoint
*ep
, struct position pos
, struct symbol
*ctype
, int val
)
415 struct expression
*expr
= alloc_const_expression(pos
, val
);
416 return add_setval(ep
, ctype
, expr
);
419 static pseudo_t
add_load(struct entrypoint
*ep
, struct expression
*expr
, pseudo_t addr
)
421 pseudo_t
new = alloc_pseudo();
422 struct instruction
*insn
= alloc_instruction(OP_LOAD
, expr
->ctype
);
426 add_one_insn(ep
, expr
->pos
, insn
);
430 static pseudo_t
linearize_load_gen(struct entrypoint
*ep
, struct expression
*expr
, pseudo_t addr
)
432 pseudo_t
new = add_load(ep
, expr
, addr
);
433 if (expr
->type
== EXPR_PREOP
)
436 if (expr
->type
== EXPR_BITFIELD
) {
439 pseudo_t shift
= add_const_value(ep
, expr
->pos
, &uint_ctype
, expr
->bitpos
);
440 new = add_binary_op(ep
, expr
, OP_SHR
, new, shift
);
442 mask
= add_const_value(ep
, expr
->pos
, &uint_ctype
, (1<<expr
->nrbits
)-1);
443 return add_binary_op(ep
, expr
, OP_AND
, new, mask
);
446 warn(expr
->pos
, "loading unknown expression");
450 static pseudo_t
linearize_access(struct entrypoint
*ep
, struct expression
*expr
)
452 pseudo_t addr
= linearize_address_gen(ep
, expr
);
453 return linearize_load_gen(ep
, expr
, addr
);
457 static pseudo_t
linearize_inc_dec(struct entrypoint
*ep
, struct expression
*expr
, int postop
)
459 pseudo_t addr
= linearize_address_gen(ep
, expr
->unop
);
460 pseudo_t old
, new, one
;
461 int op
= expr
->op
== SPECIAL_INCREMENT
? OP_ADD
: OP_SUB
;
463 old
= linearize_load_gen(ep
, expr
->unop
, addr
);
464 one
= add_const_value(ep
, expr
->pos
, expr
->ctype
, 1);
465 new = add_binary_op(ep
, expr
, op
, old
, one
);
466 linearize_store_gen(ep
, new, expr
->unop
, addr
);
467 return postop
? old
: new;
470 static pseudo_t
add_uniop(struct entrypoint
*ep
, struct expression
*expr
, int op
, pseudo_t src
)
472 pseudo_t
new = alloc_pseudo();
473 struct instruction
*insn
= alloc_instruction(op
, expr
->ctype
);
476 add_one_insn(ep
, expr
->pos
, insn
);
480 static pseudo_t
linearize_regular_preop(struct entrypoint
*ep
, struct expression
*expr
)
482 pseudo_t pre
= linearize_expression(ep
, expr
->unop
);
487 pseudo_t zero
= add_const_value(ep
, expr
->pos
, expr
->ctype
, 0);
488 return add_binary_op(ep
, expr
, OP_SET_EQ
, pre
, zero
);
491 return add_uniop(ep
, expr
, OP_NOT
, pre
);
493 return add_uniop(ep
, expr
, OP_NEG
, pre
);
498 static pseudo_t
linearize_preop(struct entrypoint
*ep
, struct expression
*expr
)
501 * '*' is an lvalue access, and is fundamentally different
502 * from an arithmetic operation. Maybe it should have an
503 * expression type of its own..
506 return linearize_access(ep
, expr
);
507 if (expr
->op
== SPECIAL_INCREMENT
|| expr
->op
== SPECIAL_DECREMENT
)
508 return linearize_inc_dec(ep
, expr
, 0);
509 return linearize_regular_preop(ep
, expr
);
512 static pseudo_t
linearize_postop(struct entrypoint
*ep
, struct expression
*expr
)
514 return linearize_inc_dec(ep
, expr
, 1);
517 static pseudo_t
linearize_assignment(struct entrypoint
*ep
, struct expression
*expr
)
519 struct expression
*target
= expr
->left
;
520 pseudo_t value
, address
;
522 value
= linearize_expression(ep
, expr
->right
);
523 address
= linearize_address_gen(ep
, target
);
524 if (expr
->op
!= '=') {
525 static const int opcode
[] = {
526 [SPECIAL_ADD_ASSIGN
- SPECIAL_BASE
] = OP_ADD
,
527 [SPECIAL_SUB_ASSIGN
- SPECIAL_BASE
] = OP_SUB
,
528 [SPECIAL_MUL_ASSIGN
- SPECIAL_BASE
] = OP_MUL
,
529 [SPECIAL_DIV_ASSIGN
- SPECIAL_BASE
] = OP_DIV
,
530 [SPECIAL_MOD_ASSIGN
- SPECIAL_BASE
] = OP_MOD
,
531 [SPECIAL_SHL_ASSIGN
- SPECIAL_BASE
] = OP_SHL
,
532 [SPECIAL_SHR_ASSIGN
- SPECIAL_BASE
] = OP_SHR
,
533 [SPECIAL_AND_ASSIGN
- SPECIAL_BASE
] = OP_AND
,
534 [SPECIAL_OR_ASSIGN
- SPECIAL_BASE
] = OP_OR
,
535 [SPECIAL_XOR_ASSIGN
- SPECIAL_BASE
] = OP_XOR
537 pseudo_t left
= linearize_load_gen(ep
, target
, address
);
538 value
= add_binary_op(ep
, expr
, opcode
[expr
->op
- SPECIAL_BASE
], left
, value
);
540 linearize_store_gen(ep
, value
, target
, address
);
544 static pseudo_t
linearize_call_expression(struct entrypoint
*ep
, struct expression
*expr
)
546 struct expression
*arg
, *fn
;
547 struct instruction
*insn
= alloc_instruction(OP_CALL
, expr
->ctype
);
551 warn(expr
->pos
, "call with no type!");
555 FOR_EACH_PTR(expr
->args
, arg
) {
556 pseudo_t
new = linearize_expression(ep
, arg
);
557 add_pseudo(&insn
->arguments
, new);
561 if (fn
->type
== EXPR_PREOP
) {
562 if (fn
->unop
->type
== EXPR_SYMBOL
) {
563 struct symbol
*sym
= fn
->unop
->symbol
;
564 if (sym
->ctype
.base_type
->type
== SYM_FN
)
568 insn
->func
= linearize_expression(ep
, fn
);
569 insn
->target
= retval
= alloc_pseudo();
570 add_one_insn(ep
, expr
->pos
, insn
);
575 static pseudo_t
linearize_binop(struct entrypoint
*ep
, struct expression
*expr
)
578 static const int opcode
[] = {
579 ['+'] = OP_ADD
, ['-'] = OP_SUB
,
580 ['*'] = OP_MUL
, ['/'] = OP_DIV
,
581 ['%'] = OP_MOD
, ['&'] = OP_AND
,
582 ['|'] = OP_OR
, ['^'] = OP_XOR
,
583 [SPECIAL_LEFTSHIFT
] = OP_SHL
,
584 [SPECIAL_RIGHTSHIFT
] = OP_SHR
,
585 [SPECIAL_LOGICAL_AND
] = OP_AND_BOOL
,
586 [SPECIAL_LOGICAL_OR
] = OP_OR_BOOL
,
589 src1
= linearize_expression(ep
, expr
->left
);
590 src2
= linearize_expression(ep
, expr
->right
);
591 return add_binary_op(ep
, expr
, opcode
[expr
->op
], src1
, src2
);
594 static pseudo_t
linearize_logical_branch(struct entrypoint
*ep
, struct expression
*expr
, struct basic_block
*bb_true
, struct basic_block
*bb_false
);
596 pseudo_t
linearize_cond_branch(struct entrypoint
*ep
, struct expression
*expr
, struct basic_block
*bb_true
, struct basic_block
*bb_false
);
598 static pseudo_t
linearize_select(struct entrypoint
*ep
, struct expression
*expr
)
600 pseudo_t cond
, true, false;
604 true = linearize_expression(ep
, expr
->cond_true
);
605 false = linearize_expression(ep
, expr
->cond_false
);
606 cond
= linearize_expression(ep
, expr
->conditional
);
610 add_setcc(ep
, expr
, cond
);
611 return add_binary_op(ep
, expr
, OP_SEL
, true, false);
614 static pseudo_t
linearize_conditional(struct entrypoint
*ep
, struct expression
*expr
,
615 struct expression
*cond
, struct expression
*expr_true
,
616 struct expression
*expr_false
)
618 pseudo_t src1
, src2
, target
;
619 struct basic_block
*bb_true
= alloc_basic_block();
620 struct basic_block
*bb_false
= alloc_basic_block();
621 struct basic_block
*merge
= alloc_basic_block();
624 linearize_cond_branch(ep
, cond
, bb_true
, bb_false
);
626 set_activeblock(ep
, bb_true
);
627 src1
= linearize_expression(ep
, expr_true
);
628 bb_true
= ep
->active
;
631 src1
= linearize_expression(ep
, cond
);
632 add_branch(ep
, expr
, src1
, merge
, bb_false
);
635 set_activeblock(ep
, bb_false
);
636 src2
= linearize_expression(ep
, expr_false
);
637 bb_false
= ep
->active
;
638 set_activeblock(ep
, merge
);
640 if (src1
!= VOID
&& src2
!= VOID
) {
641 struct instruction
*phi_node
= alloc_instruction(OP_PHI
, expr
->ctype
);
642 add_phi(&phi_node
->phi_list
, alloc_phi(bb_true
, src1
));
643 add_phi(&phi_node
->phi_list
, alloc_phi(bb_false
, src2
));
644 phi_node
->target
= target
= alloc_pseudo();
645 add_one_insn(ep
, expr
->pos
, phi_node
);
646 set_activeblock(ep
, alloc_basic_block());
650 return src1
!= VOID
? src1
: src2
;
653 static pseudo_t
linearize_logical(struct entrypoint
*ep
, struct expression
*expr
)
655 struct expression
*shortcut
;
657 shortcut
= alloc_const_expression(expr
->pos
, expr
->op
== SPECIAL_LOGICAL_OR
);
658 shortcut
->ctype
= expr
->ctype
;
659 return linearize_conditional(ep
, expr
, expr
->left
, shortcut
, expr
->right
);
662 static pseudo_t
linearize_compare(struct entrypoint
*ep
, struct expression
*expr
)
664 static const int cmpop
[] = {
665 ['>'] = OP_SET_GT
, ['<'] = OP_SET_LT
,
666 [SPECIAL_EQUAL
] = OP_SET_EQ
,
667 [SPECIAL_NOTEQUAL
] = OP_SET_NE
,
668 [SPECIAL_GTE
] = OP_SET_GE
,
669 [SPECIAL_LTE
] = OP_SET_LE
,
670 [SPECIAL_UNSIGNED_LT
] = OP_SET_B
,
671 [SPECIAL_UNSIGNED_GT
] = OP_SET_A
,
672 [SPECIAL_UNSIGNED_LTE
] = OP_SET_BE
,
673 [SPECIAL_UNSIGNED_GTE
] = OP_SET_AE
,
676 pseudo_t src1
= linearize_expression(ep
, expr
->left
);
677 pseudo_t src2
= linearize_expression(ep
, expr
->right
);
678 return add_binary_op(ep
, expr
, cmpop
[expr
->op
], src1
, src2
);
682 pseudo_t
linearize_cond_branch(struct entrypoint
*ep
, struct expression
*expr
, struct basic_block
*bb_true
, struct basic_block
*bb_false
)
686 if (!expr
|| !bb_reachable(ep
->active
))
689 switch (expr
->type
) {
693 add_goto(ep
, expr
->value
? bb_true
: bb_false
);
697 add_goto(ep
, expr
->fvalue
? bb_true
: bb_false
);
701 linearize_logical_branch(ep
, expr
, bb_true
, bb_false
);
705 cond
= linearize_compare(ep
, expr
);
706 add_branch(ep
, expr
, cond
, bb_true
, bb_false
);
711 return linearize_cond_branch(ep
, expr
->unop
, bb_false
, bb_true
);
714 cond
= linearize_expression(ep
, expr
);
715 add_branch(ep
, expr
, cond
, bb_true
, bb_false
);
725 static pseudo_t
linearize_logical_branch(struct entrypoint
*ep
, struct expression
*expr
, struct basic_block
*bb_true
, struct basic_block
*bb_false
)
727 struct basic_block
*next
= alloc_basic_block();
729 if (expr
->op
== SPECIAL_LOGICAL_OR
)
730 linearize_cond_branch(ep
, expr
->left
, bb_true
, next
);
732 linearize_cond_branch(ep
, expr
->left
, next
, bb_false
);
733 set_activeblock(ep
, next
);
734 linearize_cond_branch(ep
, expr
->right
, bb_true
, bb_false
);
738 pseudo_t
linearize_cast(struct entrypoint
*ep
, struct expression
*expr
)
740 pseudo_t src
, result
;
741 struct instruction
*insn
;
743 src
= linearize_expression(ep
, expr
->cast_expression
);
746 insn
= alloc_instruction(OP_CAST
, expr
->ctype
);
747 result
= alloc_pseudo();
748 insn
->target
= result
;
750 insn
->orig_type
= expr
->cast_expression
->ctype
;
751 add_one_insn(ep
, expr
->pos
, insn
);
755 pseudo_t
linearize_expression(struct entrypoint
*ep
, struct expression
*expr
)
760 switch (expr
->type
) {
761 case EXPR_VALUE
: case EXPR_STRING
: case EXPR_SYMBOL
: case EXPR_FVALUE
: case EXPR_LABEL
:
762 return add_setval(ep
, expr
->ctype
, expr
);
765 return linearize_statement(ep
, expr
->statement
);
768 return linearize_call_expression(ep
, expr
);
771 return linearize_binop(ep
, expr
);
774 return linearize_logical(ep
, expr
);
777 return linearize_compare(ep
, expr
);
780 return linearize_select(ep
, expr
);
782 case EXPR_CONDITIONAL
:
783 return linearize_conditional(ep
, expr
, expr
->conditional
,
784 expr
->cond_true
, expr
->cond_false
);
787 linearize_expression(ep
, expr
->left
);
788 return linearize_expression(ep
, expr
->right
);
791 case EXPR_ASSIGNMENT
:
792 return linearize_assignment(ep
, expr
);
795 return linearize_preop(ep
, expr
);
798 return linearize_postop(ep
, expr
);
801 return linearize_cast(ep
, expr
);
804 return linearize_access(ep
, expr
);
807 warn(expr
->pos
, "unknown expression (%d %d)", expr
->type
, expr
->op
);
813 pseudo_t
linearize_statement(struct entrypoint
*ep
, struct statement
*stmt
)
818 switch (stmt
->type
) {
822 case STMT_EXPRESSION
:
823 return linearize_expression(ep
, stmt
->expression
);
830 struct expression
*expr
= stmt
->expression
;
831 struct basic_block
*bb_return
= stmt
->ret_target
->bb_target
;
832 struct basic_block
*active
;
833 pseudo_t src
= linearize_expression(ep
, expr
);
835 add_goto(ep
, bb_return
);
836 if (src
!= &void_pseudo
) {
837 struct instruction
*phi_node
= first_instruction(bb_return
->insns
);
839 phi_node
= alloc_instruction(OP_PHI
, expr
->ctype
);
840 phi_node
->target
= alloc_pseudo();
841 add_instruction(&bb_return
->insns
, phi_node
);
843 add_phi(&phi_node
->phi_list
, alloc_phi(active
, src
));
849 struct basic_block
*bb
= get_bound_block(ep
, stmt
->case_label
);
850 set_activeblock(ep
, bb
);
851 linearize_statement(ep
, stmt
->case_statement
);
856 struct symbol
*label
= stmt
->label_identifier
;
857 struct basic_block
*bb
;
860 bb
= get_bound_block(ep
, stmt
->label_identifier
);
861 set_activeblock(ep
, bb
);
862 linearize_statement(ep
, stmt
->label_statement
);
869 struct expression
*expr
;
870 struct instruction
*goto_ins
;
873 if (stmt
->goto_label
) {
874 add_goto(ep
, get_bound_block(ep
, stmt
->goto_label
));
878 /* This can happen as part of simplification */
879 expr
= stmt
->goto_expression
;
880 if (expr
->type
== EXPR_LABEL
) {
881 add_goto(ep
, get_bound_block(ep
, expr
->label_symbol
));
885 pseudo
= linearize_expression(ep
, expr
);
886 goto_ins
= alloc_instruction(OP_COMPUTEDGOTO
, NULL
);
887 add_one_insn(ep
, stmt
->pos
, goto_ins
);
888 goto_ins
->target
= pseudo
;
890 FOR_EACH_PTR(stmt
->target_list
, sym
) {
891 struct basic_block
*bb_computed
= get_bound_block(ep
, sym
);
892 struct multijmp
*jmp
= alloc_multijmp(bb_computed
, 1, 0);
893 add_multijmp(&goto_ins
->multijmp_list
, jmp
);
894 add_bb(&bb_computed
->parents
, ep
->active
);
901 case STMT_COMPOUND
: {
902 pseudo_t pseudo
= NULL
;
904 struct symbol
*ret
= stmt
->ret
;
905 concat_symbol_list(stmt
->syms
, &ep
->syms
);
907 ret
->bb_target
= alloc_basic_block();
908 FOR_EACH_PTR(stmt
->stmts
, s
) {
909 pseudo
= linearize_statement(ep
, s
);
912 struct basic_block
*bb
= ret
->bb_target
;
913 struct instruction
*phi
= first_instruction(bb
->insns
);
918 set_activeblock(ep
, bb
);
919 if (phi_list_size(phi
->phi_list
)==1) {
920 pseudo
= first_phi(phi
->phi_list
)->pseudo
;
921 delete_last_instruction(&bb
->insns
);
930 * This could take 'likely/unlikely' into account, and
931 * switch the arms around appropriately..
934 struct basic_block
*bb_true
, *bb_false
, *endif
;
935 struct expression
*cond
= stmt
->if_conditional
;
937 bb_true
= alloc_basic_block();
938 bb_false
= endif
= alloc_basic_block();
940 linearize_cond_branch(ep
, cond
, bb_true
, bb_false
);
942 set_activeblock(ep
, bb_true
);
943 linearize_statement(ep
, stmt
->if_true
);
945 if (stmt
->if_false
) {
946 endif
= alloc_basic_block();
948 set_activeblock(ep
, bb_false
);
949 linearize_statement(ep
, stmt
->if_false
);
951 set_activeblock(ep
, endif
);
957 struct instruction
*switch_ins
;
958 struct basic_block
*switch_end
= alloc_basic_block();
961 pseudo
= linearize_expression(ep
, stmt
->switch_expression
);
962 switch_ins
= alloc_instruction(OP_SWITCH
, NULL
);
963 switch_ins
->cond
= pseudo
;
964 add_one_insn(ep
, stmt
->pos
, switch_ins
);
966 FOR_EACH_PTR(stmt
->switch_case
->symbol_list
, sym
) {
967 struct statement
*case_stmt
= sym
->stmt
;
968 struct basic_block
*bb_case
= get_bound_block(ep
, sym
);
969 struct multijmp
*jmp
;
971 if (!case_stmt
->case_expression
) {
972 jmp
= alloc_multijmp(bb_case
, 1, 0);
976 begin
= end
= case_stmt
->case_expression
->value
;
977 if (case_stmt
->case_to
)
978 end
= case_stmt
->case_to
->value
;
980 jmp
= alloc_multijmp(bb_case
, end
, begin
);
982 jmp
= alloc_multijmp(bb_case
, begin
, end
);
985 add_multijmp(&switch_ins
->multijmp_list
, jmp
);
986 add_bb(&bb_case
->parents
, ep
->active
);
989 bind_label(stmt
->switch_break
, switch_end
, stmt
->pos
);
991 /* And linearize the actual statement */
992 linearize_statement(ep
, stmt
->switch_statement
);
993 set_activeblock(ep
, switch_end
);
998 case STMT_ITERATOR
: {
999 struct statement
*pre_statement
= stmt
->iterator_pre_statement
;
1000 struct expression
*pre_condition
= stmt
->iterator_pre_condition
;
1001 struct statement
*statement
= stmt
->iterator_statement
;
1002 struct statement
*post_statement
= stmt
->iterator_post_statement
;
1003 struct expression
*post_condition
= stmt
->iterator_post_condition
;
1004 struct basic_block
*loop_top
, *loop_body
, *loop_continue
, *loop_end
;
1006 concat_symbol_list(stmt
->iterator_syms
, &ep
->syms
);
1007 linearize_statement(ep
, pre_statement
);
1009 loop_body
= loop_top
= alloc_basic_block();
1010 loop_continue
= alloc_basic_block();
1011 loop_end
= alloc_basic_block();
1013 if (pre_condition
== post_condition
) {
1014 loop_top
= alloc_basic_block();
1015 loop_top
->flags
|= BB_REACHABLE
;
1016 set_activeblock(ep
, loop_top
);
1019 loop_top
->flags
|= BB_REACHABLE
;
1021 linearize_cond_branch(ep
, pre_condition
, loop_body
, loop_end
);
1023 bind_label(stmt
->iterator_continue
, loop_continue
, stmt
->pos
);
1024 bind_label(stmt
->iterator_break
, loop_end
, stmt
->pos
);
1026 set_activeblock(ep
, loop_body
);
1027 linearize_statement(ep
, statement
);
1028 add_goto(ep
, loop_continue
);
1030 if (post_condition
) {
1031 set_activeblock(ep
, loop_continue
);
1032 linearize_statement(ep
, post_statement
);
1033 if (pre_condition
== post_condition
)
1034 add_goto(ep
, loop_top
);
1036 linearize_cond_branch(ep
, post_condition
, loop_top
, loop_end
);
1039 set_activeblock(ep
, loop_end
);
1049 void mark_bb_reachable(struct basic_block
*bb
)
1051 struct basic_block
*child
;
1052 struct terminator_iterator term
;
1053 struct basic_block_list
*bbstack
= NULL
;
1055 if (!bb
|| bb
->flags
& BB_REACHABLE
)
1058 add_bb(&bbstack
, bb
);
1060 bb
= delete_last_basic_block(&bbstack
);
1061 if (bb
->flags
& BB_REACHABLE
)
1063 bb
->flags
|= BB_REACHABLE
;
1064 init_terminator_iterator(last_instruction(bb
->insns
), &term
);
1065 while ((child
=next_terminator_bb(&term
)) != NULL
) {
1066 if (!(child
->flags
& BB_REACHABLE
))
1067 add_bb(&bbstack
, child
);
1072 void remove_unreachable_bbs(struct basic_block_list
**bblist
)
1074 struct basic_block
*bb
, *child
;
1075 struct list_iterator iterator
;
1076 struct terminator_iterator term
;
1078 init_iterator((struct ptr_list
**) bblist
, &iterator
, 0);
1079 while((bb
=next_basic_block(&iterator
)) != NULL
)
1080 bb
->flags
&= ~BB_REACHABLE
;
1082 init_iterator((struct ptr_list
**) bblist
, &iterator
, 0);
1083 mark_bb_reachable(next_basic_block(&iterator
));
1084 while((bb
=next_basic_block(&iterator
)) != NULL
) {
1085 if (bb
->flags
& BB_REACHABLE
)
1087 init_terminator_iterator(last_instruction(bb
->insns
), &term
);
1088 while ((child
=next_terminator_bb(&term
)) != NULL
)
1089 replace_basic_block_list(&child
->parents
, bb
, NULL
);
1090 delete_iterator(&iterator
);
1094 void pack_basic_blocks(struct basic_block_list
**bblist
)
1096 struct basic_block
*bb
;
1097 struct list_iterator iterator
;
1099 remove_unreachable_bbs(bblist
);
1100 init_bb_iterator(bblist
, &iterator
, 0);
1101 while((bb
=next_basic_block(&iterator
)) != NULL
) {
1102 struct list_iterator it_parents
;
1103 struct terminator_iterator term
;
1104 struct instruction
*jmp
;
1105 struct basic_block
*target
, *sibling
, *parent
;
1107 if (!is_branch_goto(jmp
=last_instruction(bb
->insns
)))
1110 target
= jmp
->bb_true
? jmp
->bb_true
: jmp
->bb_false
;
1113 if (bb_list_size(target
->parents
) != 1 && jmp
!= first_instruction(bb
->insns
))
1116 /* Transfer the parents' terminator to target directly. */
1117 replace_basic_block_list(&target
->parents
, bb
, NULL
);
1118 init_bb_iterator(&bb
->parents
, &it_parents
, 0);
1119 while((parent
=next_basic_block(&it_parents
)) != NULL
) {
1120 init_terminator_iterator(last_instruction(parent
->insns
), &term
);
1121 while ((sibling
=next_terminator_bb(&term
)) != NULL
) {
1122 if (sibling
== bb
) {
1123 replace_terminator_bb(&term
, target
);
1124 add_bb(&target
->parents
, parent
);
1129 /* Move the instructions to the target block. */
1130 delete_last_instruction(&bb
->insns
);
1132 concat_instruction_list(target
->insns
, &bb
->insns
);
1133 free_instruction_list(&target
->insns
);
1134 target
->insns
= bb
->insns
;
1136 delete_iterator(&iterator
);
1140 struct entrypoint
*linearize_symbol(struct symbol
*sym
)
1142 struct symbol
*base_type
;
1143 struct entrypoint
*ret_ep
= NULL
;
1147 base_type
= sym
->ctype
.base_type
;
1150 if (base_type
->type
== SYM_FN
) {
1151 if (base_type
->stmt
) {
1152 struct entrypoint
*ep
= alloc_entrypoint();
1153 struct basic_block
*bb
= alloc_basic_block();
1157 bb
->flags
|= BB_REACHABLE
;
1158 set_activeblock(ep
, bb
);
1159 concat_symbol_list(base_type
->arguments
, &ep
->syms
);
1160 result
= linearize_statement(ep
, base_type
->stmt
);
1161 if (bb_reachable(ep
->active
) && !bb_terminated(ep
->active
)) {
1162 struct symbol
*ret_type
= base_type
->ctype
.base_type
;
1163 struct instruction
*insn
= alloc_instruction(OP_RET
, ret_type
);
1164 struct position pos
= base_type
->stmt
->pos
;
1167 add_one_insn(ep
, pos
, insn
);
1169 pack_basic_blocks(&ep
->bbs
);