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 pseudo_t
add_binary_op(struct entrypoint
*ep
, struct expression
*expr
, int op
, pseudo_t left
, pseudo_t right
);
26 static pseudo_t
add_setval(struct entrypoint
*ep
, struct symbol
*ctype
, struct expression
*val
);
27 static pseudo_t
add_const_value(struct entrypoint
*ep
, struct position pos
, struct symbol
*ctype
, int val
);
28 static pseudo_t
add_load(struct entrypoint
*ep
, struct expression
*expr
, pseudo_t addr
);
31 struct pseudo void_pseudo
= {};
33 static struct instruction
*alloc_instruction(int opcode
, struct symbol
*type
)
35 struct instruction
* insn
= __alloc_instruction(0);
37 insn
->opcode
= opcode
;
41 static struct entrypoint
*alloc_entrypoint(void)
43 return __alloc_entrypoint(0);
46 static struct basic_block
*alloc_basic_block(void)
48 return __alloc_basic_block(0);
51 static struct multijmp
* alloc_multijmp(struct basic_block
*target
, int begin
, int end
)
53 struct multijmp
*multijmp
= __alloc_multijmp(0);
54 multijmp
->target
= target
;
55 multijmp
->begin
= begin
;
60 static struct phi
* alloc_phi(struct basic_block
*source
, pseudo_t pseudo
)
62 struct phi
*phi
= __alloc_phi(0);
68 static void show_instruction(struct instruction
*insn
)
70 int op
= insn
->opcode
;
74 printf("\tAIEEE! (%d %d)\n", insn
->target
->nr
, insn
->src
->nr
);
77 if (insn
->type
&& insn
->type
!= &void_ctype
)
78 printf("\tret %%r%d\n", insn
->src
->nr
);
83 if (insn
->bb_true
&& insn
->bb_false
) {
84 printf("\tbr\t%%r%d, .L%p, .L%p\n", insn
->cond
->nr
, insn
->bb_true
, insn
->bb_false
);
87 printf("\tbr\t.L%p\n", insn
->bb_true
? insn
->bb_true
: insn
->bb_false
);
91 struct expression
*expr
= insn
->val
;
94 printf("\t%%r%d <- %lld\n",
95 insn
->target
->nr
, expr
->value
);
98 printf("\t%%r%d <- %Lf\n",
99 insn
->target
->nr
, expr
->fvalue
);
102 printf("\t%%r%d <- %s\n",
103 insn
->target
->nr
, show_string(expr
->string
));
106 printf("\t%%r%d <- %s\n",
107 insn
->target
->nr
, show_ident(expr
->symbol
->ident
));
110 printf("\t%%r%d <- .L%p\n",
111 insn
->target
->nr
, expr
->symbol
->bb_target
);
114 printf("\t%%r%d <- SETVAL EXPR TYPE %d\n",
115 insn
->target
->nr
, expr
->type
);
120 struct multijmp
*jmp
;
121 printf("\tswitch %%r%d", insn
->cond
->nr
);
122 FOR_EACH_PTR(insn
->multijmp_list
, jmp
) {
123 if (jmp
->begin
== jmp
->end
)
124 printf(", %d -> .L%p", jmp
->begin
, jmp
->target
);
125 else if (jmp
->begin
< jmp
->end
)
126 printf(", %d ... %d -> .L%p", jmp
->begin
, jmp
->end
, jmp
->target
);
128 printf(", default -> .L%p\n", jmp
->target
);
133 case OP_COMPUTEDGOTO
: {
134 struct multijmp
*jmp
;
135 printf("\tjmp *%%r%d", insn
->target
->nr
);
136 FOR_EACH_PTR(insn
->multijmp_list
, jmp
) {
137 printf(", .L%p", jmp
->target
);
146 printf("\t%%r%d <- phi", insn
->target
->nr
);
147 FOR_EACH_PTR(insn
->phi_list
, phi
) {
148 printf("%s(%%r%d, .L%p)", s
, phi
->pseudo
->nr
, phi
->source
);
155 printf("\tload %%r%d <- [%%r%d]\n", insn
->target
->nr
, insn
->src
->nr
);
158 printf("\tstore %%r%d -> [%%r%d]\n", insn
->target
->nr
, insn
->src
->nr
);
162 printf("\t%%r%d <- CALL %%r%d", insn
->target
->nr
, insn
->func
->nr
);
163 FOR_EACH_PTR(insn
->arguments
, arg
) {
164 printf(", %%r%d", arg
->nr
);
170 printf("\t%%r%d <- CAST(%d->%d) %%r%d\n",
172 insn
->orig_type
->bit_size
, insn
->type
->bit_size
,
175 case OP_BINARY
... OP_BINARY_END
: {
176 static const char *opname
[] = {
177 [OP_ADD
- OP_BINARY
] = "add", [OP_SUB
- OP_BINARY
] = "sub",
178 [OP_MUL
- OP_BINARY
] = "mul", [OP_DIV
- OP_BINARY
] = "div",
179 [OP_MOD
- OP_BINARY
] = "mod", [OP_AND
- OP_BINARY
] = "and",
180 [OP_OR
- OP_BINARY
] = "or", [OP_XOR
- OP_BINARY
] = "xor",
181 [OP_SHL
- OP_BINARY
] = "shl", [OP_SHR
- OP_BINARY
] = "shr",
182 [OP_AND_BOOL
- OP_BINARY
] = "and-bool",
183 [OP_OR_BOOL
- OP_BINARY
] = "or-bool",
185 printf("\t%%r%d <- %s %%r%d, %%r%d\n",
187 opname
[op
- OP_BINARY
], insn
->src1
->nr
, insn
->src2
->nr
);
191 case OP_BINCMP
... OP_BINCMP_END
: {
192 static const char *opname
[] = {
193 [OP_SET_EQ
- OP_BINCMP
] = "seteq",
194 [OP_SET_NE
- OP_BINCMP
] = "setne",
195 [OP_SET_LE
- OP_BINCMP
] = "setle",
196 [OP_SET_GE
- OP_BINCMP
] = "setge",
197 [OP_SET_LT
- OP_BINCMP
] = "setlt",
198 [OP_SET_GT
- OP_BINCMP
] = "setgt",
199 [OP_SET_BE
- OP_BINCMP
] = "setbe",
200 [OP_SET_AE
- OP_BINCMP
] = "setae",
201 [OP_SET_A
- OP_BINCMP
] = "seta",
202 [OP_SET_B
- OP_BINCMP
] = "setb",
204 printf("\t%%r%d <- %s %%r%d, %%r%d\n",
206 opname
[op
- OP_BINCMP
], insn
->src1
->nr
, insn
->src2
->nr
);
210 case OP_NOT
: case OP_NEG
:
211 printf("\t%%r%d <- %s %%r%d\n",
213 op
== OP_NOT
? "not" : "neg", insn
->src1
->nr
);
216 printf("\top %d ???\n", op
);
220 static void show_bb(struct basic_block
*bb
)
222 struct instruction
*insn
;
224 printf("bb: %p\n", bb
);
226 struct basic_block
*from
;
227 FOR_EACH_PTR(bb
->parents
, from
) {
228 printf(" **from %p**\n", from
);
231 FOR_EACH_PTR(bb
->insns
, insn
) {
232 show_instruction(insn
);
234 if (!bb_terminated(bb
))
239 void show_entry(struct entrypoint
*ep
)
242 struct basic_block
*bb
;
244 printf("ep %p: %s\n", ep
, show_ident(ep
->name
->ident
));
246 FOR_EACH_PTR(ep
->syms
, sym
) {
247 printf(" sym: %p %s\n", sym
, show_ident(sym
->ident
));
252 FOR_EACH_PTR(ep
->bbs
, bb
) {
259 static void bind_label(struct symbol
*label
, struct basic_block
*bb
, struct position pos
)
261 if (label
->bb_target
)
262 warn(pos
, "label '%s' already bound", show_ident(label
->ident
));
263 label
->bb_target
= bb
;
266 static struct basic_block
* get_bound_block(struct entrypoint
*ep
, struct symbol
*label
)
268 struct basic_block
*bb
= label
->bb_target
;
271 label
->bb_target
= bb
= alloc_basic_block();
272 bb
->flags
|= BB_REACHABLE
;
277 static void finish_block(struct entrypoint
*ep
)
279 struct basic_block
*src
= ep
->active
;
280 if (bb_reachable(src
))
284 static void add_goto(struct entrypoint
*ep
, struct basic_block
*dst
)
286 struct basic_block
*src
= ep
->active
;
287 if (bb_reachable(src
)) {
288 struct instruction
*br
= alloc_instruction(OP_BR
, NULL
);
290 add_bb(&dst
->parents
, src
);
291 add_instruction(&src
->insns
, br
);
296 static void add_one_insn(struct entrypoint
*ep
, struct position pos
, struct instruction
*insn
)
298 struct basic_block
*bb
= ep
->active
;
300 if (bb_reachable(bb
))
301 add_instruction(&bb
->insns
, insn
);
304 static void set_activeblock(struct entrypoint
*ep
, struct basic_block
*bb
)
306 if (!bb_terminated(ep
->active
))
310 if (bb_reachable(bb
))
311 add_bb(&ep
->bbs
, bb
);
314 static void add_branch(struct entrypoint
*ep
, struct expression
*expr
, pseudo_t cond
, struct basic_block
*bb_true
, struct basic_block
*bb_false
)
316 struct basic_block
*bb
= ep
->active
;
317 struct instruction
*br
;
319 if (bb_reachable(bb
)) {
320 br
= alloc_instruction(OP_BR
, expr
->ctype
);
322 br
->bb_true
= bb_true
;
323 br
->bb_false
= bb_false
;
324 add_bb(&bb_true
->parents
, bb
);
325 add_bb(&bb_false
->parents
, bb
);
326 add_one_insn(ep
, expr
->pos
, br
);
330 /* Dummy pseudo allocator */
331 static pseudo_t
alloc_pseudo(void)
334 struct pseudo
* pseudo
= __alloc_pseudo(0);
340 * FIXME! Not all accesses are memory loads. We should
341 * check what kind of symbol is behind the dereference.
343 static pseudo_t
linearize_address_gen(struct entrypoint
*ep
, struct expression
*expr
)
345 if (expr
->type
== EXPR_PREOP
)
346 return linearize_expression(ep
, expr
->unop
);
347 if (expr
->type
== EXPR_BITFIELD
)
348 return linearize_expression(ep
, expr
->address
);
349 warn(expr
->pos
, "generating address of non-lvalue");
353 static void linearize_store_gen(struct entrypoint
*ep
, pseudo_t value
, struct expression
*expr
, pseudo_t addr
)
355 struct instruction
*store
= alloc_instruction(OP_STORE
, expr
->ctype
);
357 if (expr
->type
== EXPR_BITFIELD
) {
358 unsigned long mask
= ((1<<expr
->nrbits
)-1) << expr
->bitpos
;
359 pseudo_t andmask
, ormask
, shift
, orig
;
361 shift
= add_const_value(ep
, expr
->pos
, &uint_ctype
, expr
->bitpos
);
362 value
= add_binary_op(ep
, expr
, OP_SHL
, value
, shift
);
364 orig
= add_load(ep
, expr
, addr
);
365 andmask
= add_const_value(ep
, expr
->pos
, &uint_ctype
, ~mask
);
366 value
= add_binary_op(ep
, expr
, OP_AND
, orig
, andmask
);
367 ormask
= add_const_value(ep
, expr
->pos
, &uint_ctype
, mask
);
368 value
= add_binary_op(ep
, expr
, OP_OR
, orig
, ormask
);
371 store
->target
= value
;
373 add_one_insn(ep
, expr
->pos
, store
);
376 static pseudo_t
add_binary_op(struct entrypoint
*ep
, struct expression
*expr
, int op
, pseudo_t left
, pseudo_t right
)
378 struct instruction
*insn
= alloc_instruction(op
, expr
->ctype
);
379 pseudo_t target
= alloc_pseudo();
380 insn
->target
= target
;
383 add_one_insn(ep
, expr
->pos
, insn
);
387 static pseudo_t
add_setval(struct entrypoint
*ep
, struct symbol
*ctype
, struct expression
*val
)
389 struct instruction
*insn
= alloc_instruction(OP_SETVAL
, ctype
);
390 pseudo_t target
= alloc_pseudo();
391 insn
->target
= target
;
393 add_one_insn(ep
, val
->pos
, insn
);
397 static pseudo_t
add_const_value(struct entrypoint
*ep
, struct position pos
, struct symbol
*ctype
, int val
)
399 struct expression
*expr
= alloc_const_expression(pos
, val
);
400 return add_setval(ep
, ctype
, expr
);
403 static pseudo_t
add_load(struct entrypoint
*ep
, struct expression
*expr
, pseudo_t addr
)
405 pseudo_t
new = alloc_pseudo();
406 struct instruction
*insn
= alloc_instruction(OP_LOAD
, expr
->ctype
);
410 add_one_insn(ep
, expr
->pos
, insn
);
414 static pseudo_t
linearize_load_gen(struct entrypoint
*ep
, struct expression
*expr
, pseudo_t addr
)
416 pseudo_t
new = add_load(ep
, expr
, addr
);
417 if (expr
->type
== EXPR_PREOP
)
420 if (expr
->type
== EXPR_BITFIELD
) {
423 pseudo_t shift
= add_const_value(ep
, expr
->pos
, &uint_ctype
, expr
->bitpos
);
424 new = add_binary_op(ep
, expr
, OP_SHR
, new, shift
);
426 mask
= add_const_value(ep
, expr
->pos
, &uint_ctype
, (1<<expr
->nrbits
)-1);
427 return add_binary_op(ep
, expr
, OP_AND
, new, mask
);
430 warn(expr
->pos
, "loading unknown expression");
434 static pseudo_t
linearize_access(struct entrypoint
*ep
, struct expression
*expr
)
436 pseudo_t addr
= linearize_address_gen(ep
, expr
);
437 return linearize_load_gen(ep
, expr
, addr
);
441 static pseudo_t
linearize_inc_dec(struct entrypoint
*ep
, struct expression
*expr
, int postop
)
443 pseudo_t addr
= linearize_address_gen(ep
, expr
->unop
);
444 pseudo_t old
, new, one
;
445 int op
= expr
->op
== SPECIAL_INCREMENT
? OP_ADD
: OP_SUB
;
447 old
= linearize_load_gen(ep
, expr
->unop
, addr
);
448 one
= add_const_value(ep
, expr
->pos
, expr
->ctype
, 1);
449 new = add_binary_op(ep
, expr
, op
, old
, one
);
450 linearize_store_gen(ep
, new, expr
->unop
, addr
);
451 return postop
? old
: new;
454 static pseudo_t
add_uniop(struct entrypoint
*ep
, struct expression
*expr
, int op
, pseudo_t src
)
456 pseudo_t
new = alloc_pseudo();
457 struct instruction
*insn
= alloc_instruction(op
, expr
->ctype
);
460 add_one_insn(ep
, expr
->pos
, insn
);
464 static pseudo_t
linearize_regular_preop(struct entrypoint
*ep
, struct expression
*expr
)
466 pseudo_t pre
= linearize_expression(ep
, expr
->unop
);
471 pseudo_t zero
= add_const_value(ep
, expr
->pos
, expr
->ctype
, 0);
472 return add_binary_op(ep
, expr
, OP_SET_EQ
, pre
, zero
);
475 return add_uniop(ep
, expr
, OP_NOT
, pre
);
477 return add_uniop(ep
, expr
, OP_NEG
, pre
);
482 static pseudo_t
linearize_preop(struct entrypoint
*ep
, struct expression
*expr
)
485 * '*' is an lvalue access, and is fundamentally different
486 * from an arithmetic operation. Maybe it should have an
487 * expression type of its own..
490 return linearize_access(ep
, expr
);
491 if (expr
->op
== SPECIAL_INCREMENT
|| expr
->op
== SPECIAL_DECREMENT
)
492 return linearize_inc_dec(ep
, expr
, 0);
493 return linearize_regular_preop(ep
, expr
);
496 static pseudo_t
linearize_postop(struct entrypoint
*ep
, struct expression
*expr
)
498 return linearize_inc_dec(ep
, expr
, 1);
501 static pseudo_t
linearize_assignment(struct entrypoint
*ep
, struct expression
*expr
)
503 struct expression
*target
= expr
->left
;
504 pseudo_t value
, address
;
506 value
= linearize_expression(ep
, expr
->right
);
507 address
= linearize_address_gen(ep
, target
);
508 if (expr
->op
!= '=') {
509 static const int opcode
[] = {
510 [SPECIAL_ADD_ASSIGN
- SPECIAL_BASE
] = OP_ADD
,
511 [SPECIAL_SUB_ASSIGN
- SPECIAL_BASE
] = OP_SUB
,
512 [SPECIAL_MUL_ASSIGN
- SPECIAL_BASE
] = OP_MUL
,
513 [SPECIAL_DIV_ASSIGN
- SPECIAL_BASE
] = OP_DIV
,
514 [SPECIAL_MOD_ASSIGN
- SPECIAL_BASE
] = OP_MOD
,
515 [SPECIAL_SHL_ASSIGN
- SPECIAL_BASE
] = OP_SHL
,
516 [SPECIAL_SHR_ASSIGN
- SPECIAL_BASE
] = OP_SHR
,
517 [SPECIAL_AND_ASSIGN
- SPECIAL_BASE
] = OP_AND
,
518 [SPECIAL_OR_ASSIGN
- SPECIAL_BASE
] = OP_OR
,
519 [SPECIAL_XOR_ASSIGN
- SPECIAL_BASE
] = OP_XOR
521 pseudo_t left
= linearize_load_gen(ep
, target
, address
);
522 value
= add_binary_op(ep
, expr
, opcode
[expr
->op
- SPECIAL_BASE
], left
, value
);
524 linearize_store_gen(ep
, value
, target
, address
);
528 static pseudo_t
linearize_call_expression(struct entrypoint
*ep
, struct expression
*expr
)
530 struct expression
*arg
, *fn
;
531 struct instruction
*insn
= alloc_instruction(OP_CALL
, expr
->ctype
);
535 warn(expr
->pos
, "call with no type!");
539 FOR_EACH_PTR(expr
->args
, arg
) {
540 pseudo_t
new = linearize_expression(ep
, arg
);
541 add_pseudo(&insn
->arguments
, new);
545 if (fn
->type
== EXPR_PREOP
) {
546 if (fn
->unop
->type
== EXPR_SYMBOL
) {
547 struct symbol
*sym
= fn
->unop
->symbol
;
548 if (sym
->ctype
.base_type
->type
== SYM_FN
)
552 insn
->func
= linearize_expression(ep
, fn
);
553 insn
->target
= retval
= alloc_pseudo();
554 add_one_insn(ep
, expr
->pos
, insn
);
559 static pseudo_t
linearize_binop(struct entrypoint
*ep
, struct expression
*expr
)
562 static const int opcode
[] = {
563 ['+'] = OP_ADD
, ['-'] = OP_SUB
,
564 ['*'] = OP_MUL
, ['/'] = OP_DIV
,
565 ['%'] = OP_MOD
, ['&'] = OP_AND
,
566 ['|'] = OP_OR
, ['^'] = OP_XOR
,
567 [SPECIAL_LEFTSHIFT
] = OP_SHL
,
568 [SPECIAL_RIGHTSHIFT
] = OP_SHR
,
569 [SPECIAL_LOGICAL_AND
] = OP_AND_BOOL
,
570 [SPECIAL_LOGICAL_OR
] = OP_OR_BOOL
,
573 src1
= linearize_expression(ep
, expr
->left
);
574 src2
= linearize_expression(ep
, expr
->right
);
575 return add_binary_op(ep
, expr
, opcode
[expr
->op
], src1
, src2
);
578 static pseudo_t
linearize_logical_branch(struct entrypoint
*ep
, struct expression
*expr
, struct basic_block
*bb_true
, struct basic_block
*bb_false
);
580 pseudo_t
linearize_cond_branch(struct entrypoint
*ep
, struct expression
*expr
, struct basic_block
*bb_true
, struct basic_block
*bb_false
);
582 static pseudo_t
linearize_select(struct entrypoint
*ep
, struct expression
*expr
)
584 pseudo_t cond
, true, false;
588 true = linearize_expression(ep
, expr
->cond_true
);
589 false = linearize_expression(ep
, expr
->cond_false
);
590 cond
= linearize_expression(ep
, expr
->conditional
);
594 /* FIXME! This needs a ternary operation */
595 return add_binary_op(ep
, expr
, OP_SEL
, true, false);
598 static pseudo_t
linearize_conditional(struct entrypoint
*ep
, struct expression
*expr
,
599 struct expression
*cond
, struct expression
*expr_true
,
600 struct expression
*expr_false
)
602 pseudo_t src1
, src2
, target
;
603 struct basic_block
*bb_true
= alloc_basic_block();
604 struct basic_block
*bb_false
= alloc_basic_block();
605 struct basic_block
*merge
= alloc_basic_block();
608 linearize_cond_branch(ep
, cond
, bb_true
, bb_false
);
610 set_activeblock(ep
, bb_true
);
611 src1
= linearize_expression(ep
, expr_true
);
612 bb_true
= ep
->active
;
615 src1
= linearize_expression(ep
, cond
);
616 add_branch(ep
, expr
, src1
, merge
, bb_false
);
619 set_activeblock(ep
, bb_false
);
620 src2
= linearize_expression(ep
, expr_false
);
621 bb_false
= ep
->active
;
622 set_activeblock(ep
, merge
);
624 if (src1
!= VOID
&& src2
!= VOID
) {
625 struct instruction
*phi_node
= alloc_instruction(OP_PHI
, expr
->ctype
);
626 add_phi(&phi_node
->phi_list
, alloc_phi(bb_true
, src1
));
627 add_phi(&phi_node
->phi_list
, alloc_phi(bb_false
, src2
));
628 phi_node
->target
= target
= alloc_pseudo();
629 add_one_insn(ep
, expr
->pos
, phi_node
);
630 set_activeblock(ep
, alloc_basic_block());
634 return src1
!= VOID
? src1
: src2
;
637 static pseudo_t
linearize_logical(struct entrypoint
*ep
, struct expression
*expr
)
639 struct expression
*shortcut
;
641 shortcut
= alloc_const_expression(expr
->pos
, expr
->op
== SPECIAL_LOGICAL_OR
);
642 shortcut
->ctype
= expr
->ctype
;
643 return linearize_conditional(ep
, expr
, expr
->left
, shortcut
, expr
->right
);
646 static pseudo_t
linearize_compare(struct entrypoint
*ep
, struct expression
*expr
)
648 static const int cmpop
[] = {
649 ['>'] = OP_SET_GT
, ['<'] = OP_SET_LT
,
650 [SPECIAL_EQUAL
] = OP_SET_EQ
,
651 [SPECIAL_NOTEQUAL
] = OP_SET_NE
,
652 [SPECIAL_GTE
] = OP_SET_GE
,
653 [SPECIAL_LTE
] = OP_SET_LE
,
654 [SPECIAL_UNSIGNED_LT
] = OP_SET_B
,
655 [SPECIAL_UNSIGNED_GT
] = OP_SET_A
,
656 [SPECIAL_UNSIGNED_LTE
] = OP_SET_BE
,
657 [SPECIAL_UNSIGNED_GTE
] = OP_SET_AE
,
660 pseudo_t src1
= linearize_expression(ep
, expr
->left
);
661 pseudo_t src2
= linearize_expression(ep
, expr
->right
);
662 return add_binary_op(ep
, expr
, cmpop
[expr
->op
], src1
, src2
);
666 pseudo_t
linearize_cond_branch(struct entrypoint
*ep
, struct expression
*expr
, struct basic_block
*bb_true
, struct basic_block
*bb_false
)
670 if (!expr
|| !bb_reachable(ep
->active
))
673 switch (expr
->type
) {
677 add_goto(ep
, expr
->value
? bb_true
: bb_false
);
681 add_goto(ep
, expr
->fvalue
? bb_true
: bb_false
);
685 linearize_logical_branch(ep
, expr
, bb_true
, bb_false
);
689 cond
= linearize_compare(ep
, expr
);
690 add_branch(ep
, expr
, cond
, bb_true
, bb_false
);
695 return linearize_cond_branch(ep
, expr
->unop
, bb_false
, bb_true
);
698 cond
= linearize_expression(ep
, expr
);
699 add_branch(ep
, expr
, cond
, bb_true
, bb_false
);
709 static pseudo_t
linearize_logical_branch(struct entrypoint
*ep
, struct expression
*expr
, struct basic_block
*bb_true
, struct basic_block
*bb_false
)
711 struct basic_block
*next
= alloc_basic_block();
713 if (expr
->op
== SPECIAL_LOGICAL_OR
)
714 linearize_cond_branch(ep
, expr
->left
, bb_true
, next
);
716 linearize_cond_branch(ep
, expr
->left
, next
, bb_false
);
717 set_activeblock(ep
, next
);
718 linearize_cond_branch(ep
, expr
->right
, bb_true
, bb_false
);
722 pseudo_t
linearize_cast(struct entrypoint
*ep
, struct expression
*expr
)
724 pseudo_t src
, result
;
725 struct instruction
*insn
;
727 src
= linearize_expression(ep
, expr
->cast_expression
);
730 insn
= alloc_instruction(OP_CAST
, expr
->ctype
);
731 result
= alloc_pseudo();
732 insn
->target
= result
;
734 insn
->orig_type
= expr
->cast_expression
->ctype
;
735 add_one_insn(ep
, expr
->pos
, insn
);
739 pseudo_t
linearize_expression(struct entrypoint
*ep
, struct expression
*expr
)
744 switch (expr
->type
) {
745 case EXPR_VALUE
: case EXPR_STRING
: case EXPR_SYMBOL
: case EXPR_FVALUE
: case EXPR_LABEL
:
746 return add_setval(ep
, expr
->ctype
, expr
);
749 return linearize_statement(ep
, expr
->statement
);
752 return linearize_call_expression(ep
, expr
);
755 return linearize_binop(ep
, expr
);
758 return linearize_logical(ep
, expr
);
761 return linearize_compare(ep
, expr
);
764 return linearize_select(ep
, expr
);
766 case EXPR_CONDITIONAL
:
767 return linearize_conditional(ep
, expr
, expr
->conditional
,
768 expr
->cond_true
, expr
->cond_false
);
771 linearize_expression(ep
, expr
->left
);
772 return linearize_expression(ep
, expr
->right
);
775 case EXPR_ASSIGNMENT
:
776 return linearize_assignment(ep
, expr
);
779 return linearize_preop(ep
, expr
);
782 return linearize_postop(ep
, expr
);
785 return linearize_cast(ep
, expr
);
788 return linearize_access(ep
, expr
);
791 warn(expr
->pos
, "unknown expression (%d %d)", expr
->type
, expr
->op
);
797 pseudo_t
linearize_statement(struct entrypoint
*ep
, struct statement
*stmt
)
802 switch (stmt
->type
) {
806 case STMT_EXPRESSION
:
807 return linearize_expression(ep
, stmt
->expression
);
814 struct expression
*expr
= stmt
->expression
;
815 struct basic_block
*bb_return
= stmt
->ret_target
->bb_target
;
816 struct basic_block
*active
;
817 pseudo_t src
= linearize_expression(ep
, expr
);
819 add_goto(ep
, bb_return
);
820 if (src
!= &void_pseudo
) {
821 struct instruction
*phi_node
= first_instruction(bb_return
->insns
);
823 phi_node
= alloc_instruction(OP_PHI
, expr
->ctype
);
824 phi_node
->target
= alloc_pseudo();
825 add_instruction(&bb_return
->insns
, phi_node
);
827 add_phi(&phi_node
->phi_list
, alloc_phi(active
, src
));
833 struct basic_block
*bb
= get_bound_block(ep
, stmt
->case_label
);
834 set_activeblock(ep
, bb
);
835 linearize_statement(ep
, stmt
->case_statement
);
840 struct symbol
*label
= stmt
->label_identifier
;
841 struct basic_block
*bb
;
844 bb
= get_bound_block(ep
, stmt
->label_identifier
);
845 set_activeblock(ep
, bb
);
846 linearize_statement(ep
, stmt
->label_statement
);
853 struct expression
*expr
;
854 struct instruction
*goto_ins
;
857 if (stmt
->goto_label
) {
858 add_goto(ep
, get_bound_block(ep
, stmt
->goto_label
));
862 /* This can happen as part of simplification */
863 expr
= stmt
->goto_expression
;
864 if (expr
->type
== EXPR_LABEL
) {
865 add_goto(ep
, get_bound_block(ep
, expr
->label_symbol
));
869 pseudo
= linearize_expression(ep
, expr
);
870 goto_ins
= alloc_instruction(OP_COMPUTEDGOTO
, NULL
);
871 add_one_insn(ep
, stmt
->pos
, goto_ins
);
872 goto_ins
->target
= pseudo
;
874 FOR_EACH_PTR(stmt
->target_list
, sym
) {
875 struct basic_block
*bb_computed
= get_bound_block(ep
, sym
);
876 struct multijmp
*jmp
= alloc_multijmp(bb_computed
, 1, 0);
877 add_multijmp(&goto_ins
->multijmp_list
, jmp
);
878 add_bb(&bb_computed
->parents
, ep
->active
);
885 case STMT_COMPOUND
: {
886 pseudo_t pseudo
= NULL
;
888 struct symbol
*ret
= stmt
->ret
;
889 concat_symbol_list(stmt
->syms
, &ep
->syms
);
891 ret
->bb_target
= alloc_basic_block();
892 FOR_EACH_PTR(stmt
->stmts
, s
) {
893 pseudo
= linearize_statement(ep
, s
);
896 struct basic_block
*bb
= ret
->bb_target
;
897 struct instruction
*phi
= first_instruction(bb
->insns
);
902 set_activeblock(ep
, bb
);
903 if (phi_list_size(phi
->phi_list
)==1) {
904 pseudo
= first_phi(phi
->phi_list
)->pseudo
;
905 delete_last_instruction(&bb
->insns
);
914 * This could take 'likely/unlikely' into account, and
915 * switch the arms around appropriately..
918 struct basic_block
*bb_true
, *bb_false
, *endif
;
919 struct expression
*cond
= stmt
->if_conditional
;
921 bb_true
= alloc_basic_block();
922 bb_false
= endif
= alloc_basic_block();
924 linearize_cond_branch(ep
, cond
, bb_true
, bb_false
);
926 set_activeblock(ep
, bb_true
);
927 linearize_statement(ep
, stmt
->if_true
);
929 if (stmt
->if_false
) {
930 endif
= alloc_basic_block();
932 set_activeblock(ep
, bb_false
);
933 linearize_statement(ep
, stmt
->if_false
);
935 set_activeblock(ep
, endif
);
941 struct instruction
*switch_ins
;
942 struct basic_block
*switch_end
= alloc_basic_block();
945 pseudo
= linearize_expression(ep
, stmt
->switch_expression
);
946 switch_ins
= alloc_instruction(OP_SWITCH
, NULL
);
947 switch_ins
->cond
= pseudo
;
948 add_one_insn(ep
, stmt
->pos
, switch_ins
);
950 FOR_EACH_PTR(stmt
->switch_case
->symbol_list
, sym
) {
951 struct statement
*case_stmt
= sym
->stmt
;
952 struct basic_block
*bb_case
= get_bound_block(ep
, sym
);
953 struct multijmp
*jmp
;
955 if (!case_stmt
->case_expression
) {
956 jmp
= alloc_multijmp(bb_case
, 1, 0);
960 begin
= end
= case_stmt
->case_expression
->value
;
961 if (case_stmt
->case_to
)
962 end
= case_stmt
->case_to
->value
;
964 jmp
= alloc_multijmp(bb_case
, end
, begin
);
966 jmp
= alloc_multijmp(bb_case
, begin
, end
);
969 add_multijmp(&switch_ins
->multijmp_list
, jmp
);
970 add_bb(&bb_case
->parents
, ep
->active
);
973 bind_label(stmt
->switch_break
, switch_end
, stmt
->pos
);
975 /* And linearize the actual statement */
976 linearize_statement(ep
, stmt
->switch_statement
);
977 set_activeblock(ep
, switch_end
);
982 case STMT_ITERATOR
: {
983 struct statement
*pre_statement
= stmt
->iterator_pre_statement
;
984 struct expression
*pre_condition
= stmt
->iterator_pre_condition
;
985 struct statement
*statement
= stmt
->iterator_statement
;
986 struct statement
*post_statement
= stmt
->iterator_post_statement
;
987 struct expression
*post_condition
= stmt
->iterator_post_condition
;
988 struct basic_block
*loop_top
, *loop_body
, *loop_continue
, *loop_end
;
990 concat_symbol_list(stmt
->iterator_syms
, &ep
->syms
);
991 linearize_statement(ep
, pre_statement
);
993 loop_body
= loop_top
= alloc_basic_block();
994 loop_continue
= alloc_basic_block();
995 loop_end
= alloc_basic_block();
997 if (pre_condition
== post_condition
) {
998 loop_top
= alloc_basic_block();
999 loop_top
->flags
|= BB_REACHABLE
;
1000 set_activeblock(ep
, loop_top
);
1003 loop_top
->flags
|= BB_REACHABLE
;
1005 linearize_cond_branch(ep
, pre_condition
, loop_body
, loop_end
);
1007 bind_label(stmt
->iterator_continue
, loop_continue
, stmt
->pos
);
1008 bind_label(stmt
->iterator_break
, loop_end
, stmt
->pos
);
1010 set_activeblock(ep
, loop_body
);
1011 linearize_statement(ep
, statement
);
1012 add_goto(ep
, loop_continue
);
1014 if (post_condition
) {
1015 set_activeblock(ep
, loop_continue
);
1016 linearize_statement(ep
, post_statement
);
1017 if (pre_condition
== post_condition
)
1018 add_goto(ep
, loop_top
);
1020 linearize_cond_branch(ep
, post_condition
, loop_top
, loop_end
);
1023 set_activeblock(ep
, loop_end
);
1033 void mark_bb_reachable(struct basic_block
*bb
)
1035 struct basic_block
*child
;
1036 struct terminator_iterator term
;
1037 struct basic_block_list
*bbstack
= NULL
;
1039 if (!bb
|| bb
->flags
& BB_REACHABLE
)
1042 add_bb(&bbstack
, bb
);
1044 bb
= delete_last_basic_block(&bbstack
);
1045 if (bb
->flags
& BB_REACHABLE
)
1047 bb
->flags
|= BB_REACHABLE
;
1048 init_terminator_iterator(last_instruction(bb
->insns
), &term
);
1049 while ((child
=next_terminator_bb(&term
)) != NULL
) {
1050 if (!(child
->flags
& BB_REACHABLE
))
1051 add_bb(&bbstack
, child
);
1056 void remove_unreachable_bbs(struct basic_block_list
**bblist
)
1058 struct basic_block
*bb
, *child
;
1059 struct list_iterator iterator
;
1060 struct terminator_iterator term
;
1062 init_iterator((struct ptr_list
**) bblist
, &iterator
, 0);
1063 while((bb
=next_basic_block(&iterator
)) != NULL
)
1064 bb
->flags
&= ~BB_REACHABLE
;
1066 init_iterator((struct ptr_list
**) bblist
, &iterator
, 0);
1067 mark_bb_reachable(next_basic_block(&iterator
));
1068 while((bb
=next_basic_block(&iterator
)) != NULL
) {
1069 if (bb
->flags
& BB_REACHABLE
)
1071 init_terminator_iterator(last_instruction(bb
->insns
), &term
);
1072 while ((child
=next_terminator_bb(&term
)) != NULL
)
1073 replace_basic_block_list(&child
->parents
, bb
, NULL
);
1074 delete_iterator(&iterator
);
1078 void pack_basic_blocks(struct basic_block_list
**bblist
)
1080 struct basic_block
*bb
;
1081 struct list_iterator iterator
;
1083 remove_unreachable_bbs(bblist
);
1084 init_bb_iterator(bblist
, &iterator
, 0);
1085 while((bb
=next_basic_block(&iterator
)) != NULL
) {
1086 struct list_iterator it_parents
;
1087 struct terminator_iterator term
;
1088 struct instruction
*jmp
;
1089 struct basic_block
*target
, *sibling
, *parent
;
1091 if (!is_branch_goto(jmp
=last_instruction(bb
->insns
)))
1094 target
= jmp
->bb_true
? jmp
->bb_true
: jmp
->bb_false
;
1097 if (bb_list_size(target
->parents
) != 1 && jmp
!= first_instruction(bb
->insns
))
1100 /* Transfer the parents' terminator to target directly. */
1101 replace_basic_block_list(&target
->parents
, bb
, NULL
);
1102 init_bb_iterator(&bb
->parents
, &it_parents
, 0);
1103 while((parent
=next_basic_block(&it_parents
)) != NULL
) {
1104 init_terminator_iterator(last_instruction(parent
->insns
), &term
);
1105 while ((sibling
=next_terminator_bb(&term
)) != NULL
) {
1106 if (sibling
== bb
) {
1107 replace_terminator_bb(&term
, target
);
1108 add_bb(&target
->parents
, parent
);
1113 /* Move the instructions to the target block. */
1114 delete_last_instruction(&bb
->insns
);
1116 concat_instruction_list(target
->insns
, &bb
->insns
);
1117 free_instruction_list(&target
->insns
);
1118 target
->insns
= bb
->insns
;
1120 delete_iterator(&iterator
);
1124 struct entrypoint
*linearize_symbol(struct symbol
*sym
)
1126 struct symbol
*base_type
;
1127 struct entrypoint
*ret_ep
= NULL
;
1131 base_type
= sym
->ctype
.base_type
;
1134 if (base_type
->type
== SYM_FN
) {
1135 if (base_type
->stmt
) {
1136 struct entrypoint
*ep
= alloc_entrypoint();
1137 struct basic_block
*bb
= alloc_basic_block();
1141 bb
->flags
|= BB_REACHABLE
;
1142 set_activeblock(ep
, bb
);
1143 concat_symbol_list(base_type
->arguments
, &ep
->syms
);
1144 result
= linearize_statement(ep
, base_type
->stmt
);
1145 if (bb_reachable(ep
->active
) && !bb_terminated(ep
->active
)) {
1146 struct symbol
*ret_type
= base_type
->ctype
.base_type
;
1147 struct instruction
*insn
= alloc_instruction(OP_RET
, ret_type
);
1148 struct position pos
= base_type
->stmt
->pos
;
1151 add_one_insn(ep
, pos
, insn
);
1153 pack_basic_blocks(&ep
->bbs
);