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
20 #include "expression.h"
21 #include "linearize.h"
25 pseudo_t
linearize_statement(struct entrypoint
*ep
, struct statement
*stmt
);
26 pseudo_t
linearize_expression(struct entrypoint
*ep
, struct expression
*expr
);
28 static pseudo_t
add_binary_op(struct entrypoint
*ep
, struct symbol
*ctype
, int op
, pseudo_t left
, pseudo_t right
);
29 static pseudo_t
add_setval(struct entrypoint
*ep
, struct symbol
*ctype
, struct expression
*val
);
32 static pseudo_t
add_load(struct entrypoint
*ep
, struct access_data
*);
33 pseudo_t
linearize_initializer(struct entrypoint
*ep
, struct expression
*initializer
, struct access_data
*);
35 struct pseudo void_pseudo
= {};
37 static struct instruction
*alloc_instruction(int opcode
, int size
)
39 struct instruction
* insn
= __alloc_instruction(0);
40 insn
->opcode
= opcode
;
45 static inline int type_size(struct symbol
*type
)
47 return type
? type
->bit_size
> 0 ? type
->bit_size
: 0 : 0;
50 static struct instruction
*alloc_typed_instruction(int opcode
, struct symbol
*type
)
52 return alloc_instruction(opcode
, type_size(type
));
55 static struct entrypoint
*alloc_entrypoint(void)
57 return __alloc_entrypoint(0);
60 static struct basic_block
*alloc_basic_block(struct entrypoint
*ep
, struct position pos
)
62 struct basic_block
*bb
= __alloc_basic_block(0);
69 static struct multijmp
* alloc_multijmp(struct basic_block
*target
, int begin
, int end
)
71 struct multijmp
*multijmp
= __alloc_multijmp(0);
72 multijmp
->target
= target
;
73 multijmp
->begin
= begin
;
78 static inline int regno(pseudo_t n
)
81 if (n
&& n
->type
== PSEUDO_REG
)
86 const char *show_pseudo(pseudo_t pseudo
)
89 static char buffer
[4][64];
97 buf
= buffer
[3 & ++n
];
98 switch(pseudo
->type
) {
100 struct symbol
*sym
= pseudo
->sym
;
101 struct expression
*expr
;
103 if (sym
->bb_target
) {
104 snprintf(buf
, 64, ".L%p", sym
->bb_target
);
108 snprintf(buf
, 64, "%s", show_ident(sym
->ident
));
111 expr
= sym
->initializer
;
113 snprintf(buf
, 64, "<anon sym: %d>", pseudo
->nr
);
116 switch (expr
->type
) {
118 snprintf(buf
, 64, "<symbol value: %lld>", expr
->value
);
121 return show_string(expr
->string
);
123 snprintf(buf
, 64, "<symbol expression: %d>", pseudo
->nr
);
128 i
= snprintf(buf
, 64, "%%r%d", pseudo
->nr
);
130 sprintf(buf
+i
, "(%s)", show_ident(pseudo
->ident
));
133 long long value
= pseudo
->value
;
134 if (value
> 1000 || value
< -1000)
135 snprintf(buf
, 64, "$%#llx", value
);
137 snprintf(buf
, 64, "$%lld", value
);
141 snprintf(buf
, 64, "%%arg%d", pseudo
->nr
);
144 i
= snprintf(buf
, 64, "%%phi%d", pseudo
->nr
);
146 sprintf(buf
+i
, "(%s)", show_ident(pseudo
->ident
));
149 snprintf(buf
, 64, "<bad pseudo type %d>", pseudo
->type
);
154 static const char* opcodes
[] = {
155 [OP_BADOP
] = "bad_op",
158 [OP_ENTRY
] = "<entry-point>",
163 [OP_SWITCH
] = "switch",
164 [OP_INVOKE
] = "invoke",
165 [OP_COMPUTEDGOTO
] = "jmp *",
166 [OP_UNWIND
] = "unwind",
181 [OP_AND_BOOL
] = "and-bool",
182 [OP_OR_BOOL
] = "or-bool",
184 /* Binary comparison */
185 [OP_SET_EQ
] = "seteq",
186 [OP_SET_NE
] = "setne",
187 [OP_SET_LE
] = "setle",
188 [OP_SET_GE
] = "setge",
189 [OP_SET_LT
] = "setlt",
190 [OP_SET_GT
] = "setgt",
193 [OP_SET_BE
] = "setbe",
194 [OP_SET_AE
] = "setae",
200 /* Special three-input */
204 [OP_MALLOC
] = "malloc",
206 [OP_ALLOCA
] = "alloca",
208 [OP_STORE
] = "store",
210 [OP_GET_ELEMENT_PTR
] = "getelem",
214 [OP_PHISOURCE
] = "phisrc",
216 [OP_PTRCAST
] = "ptrcast",
218 [OP_VANEXT
] = "va_next",
219 [OP_VAARG
] = "va_arg",
220 [OP_SLICE
] = "slice",
224 [OP_DEATHNOTE
] = "dead",
227 /* Sparse tagging (line numbers, context, whatever) */
228 [OP_CONTEXT
] = "context",
231 void show_instruction(struct instruction
*insn
)
233 int opcode
= insn
->opcode
;
234 static char buffer
[1024] = "\t";
241 buf
+= sprintf(buf
, "# ");
244 if (opcode
< sizeof(opcodes
)/sizeof(char *)) {
245 const char *op
= opcodes
[opcode
];
247 buf
+= sprintf(buf
, "opcode:%d", opcode
);
249 buf
+= sprintf(buf
, "%s", op
);
251 buf
+= sprintf(buf
, ".%d", insn
->size
);
252 memset(buf
, ' ', 20);
256 if (buf
< buffer
+ 12)
260 if (insn
->src
&& insn
->src
!= VOID
)
261 buf
+= sprintf(buf
, "%s", show_pseudo(insn
->src
));
264 if (insn
->bb_true
&& insn
->bb_false
) {
265 buf
+= sprintf(buf
, "%s, .L%p, .L%p", show_pseudo(insn
->cond
), insn
->bb_true
, insn
->bb_false
);
268 buf
+= sprintf(buf
, ".L%p", insn
->bb_true
? insn
->bb_true
: insn
->bb_false
);
272 struct expression
*expr
= insn
->val
;
273 pseudo_t pseudo
= insn
->symbol
;
274 buf
+= sprintf(buf
, "%s <- ", show_pseudo(insn
->target
));
276 struct symbol
*sym
= pseudo
->sym
;
278 buf
+= sprintf(buf
, "%s", show_pseudo(pseudo
));
281 if (sym
->bb_target
) {
282 buf
+= sprintf(buf
, ".L%p", sym
->bb_target
);
286 buf
+= sprintf(buf
, "%s", show_ident(sym
->ident
));
289 expr
= sym
->initializer
;
291 buf
+= sprintf(buf
, "%s", "anon symbol");
297 buf
+= sprintf(buf
, "%s", "<none>");
301 switch (expr
->type
) {
303 buf
+= sprintf(buf
, "%lld", expr
->value
);
306 buf
+= sprintf(buf
, "%Lf", expr
->fvalue
);
309 buf
+= sprintf(buf
, "%.40s", show_string(expr
->string
));
312 buf
+= sprintf(buf
, "%s", show_ident(expr
->symbol
->ident
));
315 buf
+= sprintf(buf
, ".L%p", expr
->symbol
->bb_target
);
318 buf
+= sprintf(buf
, "SETVAL EXPR TYPE %d", expr
->type
);
323 struct multijmp
*jmp
;
324 buf
+= sprintf(buf
, "%s", show_pseudo(insn
->target
));
325 FOR_EACH_PTR(insn
->multijmp_list
, jmp
) {
326 if (jmp
->begin
== jmp
->end
)
327 buf
+= sprintf(buf
, ", %d -> .L%p", jmp
->begin
, jmp
->target
);
328 else if (jmp
->begin
< jmp
->end
)
329 buf
+= sprintf(buf
, ", %d ... %d -> .L%p", jmp
->begin
, jmp
->end
, jmp
->target
);
331 buf
+= sprintf(buf
, ", default -> .L%p", jmp
->target
);
332 } END_FOR_EACH_PTR(jmp
);
335 case OP_COMPUTEDGOTO
: {
336 struct multijmp
*jmp
;
337 buf
+= sprintf(buf
, "%s", show_pseudo(insn
->target
));
338 FOR_EACH_PTR(insn
->multijmp_list
, jmp
) {
339 buf
+= sprintf(buf
, ", .L%p", jmp
->target
);
340 } END_FOR_EACH_PTR(jmp
);
345 buf
+= sprintf(buf
, "%s <- %s", show_pseudo(insn
->target
), show_pseudo(insn
->src1
));
350 const char *s
= " <-";
351 buf
+= sprintf(buf
, "%s", show_pseudo(insn
->target
));
352 FOR_EACH_PTR(insn
->phi_list
, phi
) {
353 buf
+= sprintf(buf
, "%s %s", s
, show_pseudo(phi
));
355 } END_FOR_EACH_PTR(phi
);
358 case OP_LOAD
: case OP_LNOP
:
359 buf
+= sprintf(buf
, "%s <- %d[%s]", show_pseudo(insn
->target
), insn
->offset
, show_pseudo(insn
->src
));
361 case OP_STORE
: case OP_SNOP
:
362 buf
+= sprintf(buf
, "%s -> %d[%s]", show_pseudo(insn
->target
), insn
->offset
, show_pseudo(insn
->src
));
366 if (insn
->target
&& insn
->target
!= VOID
)
367 buf
+= sprintf(buf
, "%s <- ", show_pseudo(insn
->target
));
368 buf
+= sprintf(buf
, "%s", show_pseudo(insn
->func
));
369 FOR_EACH_PTR(insn
->arguments
, arg
) {
370 buf
+= sprintf(buf
, ", %s", show_pseudo(arg
));
371 } END_FOR_EACH_PTR(arg
);
376 buf
+= sprintf(buf
, "%s <- (%d) %s",
377 show_pseudo(insn
->target
),
378 type_size(insn
->orig_type
),
379 show_pseudo(insn
->src
));
381 case OP_BINARY
... OP_BINARY_END
:
382 case OP_BINCMP
... OP_BINCMP_END
:
383 buf
+= sprintf(buf
, "%s <- %s, %s", show_pseudo(insn
->target
), show_pseudo(insn
->src1
), show_pseudo(insn
->src2
));
387 buf
+= sprintf(buf
, "%s <- %s, %s, %s", show_pseudo(insn
->target
),
388 show_pseudo(insn
->src1
), show_pseudo(insn
->src2
), show_pseudo(insn
->src3
));
392 buf
+= sprintf(buf
, "%s <- %s, %d, %d", show_pseudo(insn
->target
), show_pseudo(insn
->base
), insn
->from
, insn
->len
);
395 case OP_NOT
: case OP_NEG
:
396 buf
+= sprintf(buf
, "%s <- %s", show_pseudo(insn
->target
), show_pseudo(insn
->src1
));
400 buf
+= sprintf(buf
, "%d", insn
->increment
);
403 buf
+= sprintf(buf
, "%s <- %s", show_pseudo(insn
->target
), show_pseudo(insn
->src1
));
406 buf
+= sprintf(buf
, "%s", show_pseudo(insn
->target
));
409 buf
+= sprintf(buf
, "\"%s\"", insn
->string
);
412 buf
+= sprintf(buf
, " (");
413 FOR_EACH_PTR(insn
->outputs
, pseudo
) {
414 buf
+= sprintf(buf
, " %s", show_pseudo(pseudo
));
415 } END_FOR_EACH_PTR(pseudo
);
416 buf
+= sprintf(buf
, " ) <-");
420 buf
+= sprintf(buf
, " (");
421 FOR_EACH_PTR(insn
->inputs
, pseudo
) {
422 buf
+= sprintf(buf
, " %s", show_pseudo(pseudo
));
423 } END_FOR_EACH_PTR(pseudo
);
424 buf
+= sprintf(buf
, " )");
430 do { --buf
; } while (*buf
== ' ');
432 printf("%s\n", buffer
);
435 static void show_bb(struct basic_block
*bb
)
437 struct instruction
*insn
;
439 printf(".L%p:\n", bb
);
441 pseudo_t needs
, defines
;
442 printf("%s:%d\n", input_streams
[bb
->pos
.stream
].name
, bb
->pos
.line
);
444 FOR_EACH_PTR(bb
->needs
, needs
) {
445 struct instruction
*def
= needs
->def
;
446 if (def
->opcode
!= OP_PHI
) {
447 printf(" **uses %s (from .L%p)**\n", show_pseudo(needs
), def
->bb
);
450 const char *sep
= " ";
451 printf(" **uses %s (from", show_pseudo(needs
));
452 FOR_EACH_PTR(def
->phi_list
, phi
) {
455 printf("%s(%s:.L%p)", sep
, show_pseudo(phi
), phi
->def
->bb
);
457 } END_FOR_EACH_PTR(phi
);
460 } END_FOR_EACH_PTR(needs
);
462 FOR_EACH_PTR(bb
->defines
, defines
) {
463 printf(" **defines %s **\n", show_pseudo(defines
));
464 } END_FOR_EACH_PTR(defines
);
467 struct basic_block
*from
;
468 FOR_EACH_PTR(bb
->parents
, from
) {
469 printf(" **from %p (%s:%d:%d)**\n", from
,
470 input_streams
[from
->pos
.stream
].name
, from
->pos
.line
, from
->pos
.pos
);
471 } END_FOR_EACH_PTR(from
);
475 struct basic_block
*to
;
476 FOR_EACH_PTR(bb
->children
, to
) {
477 printf(" **to %p (%s:%d:%d)**\n", to
,
478 input_streams
[to
->pos
.stream
].name
, to
->pos
.line
, to
->pos
.pos
);
479 } END_FOR_EACH_PTR(to
);
483 FOR_EACH_PTR(bb
->insns
, insn
) {
484 show_instruction(insn
);
485 } END_FOR_EACH_PTR(insn
);
486 if (!bb_terminated(bb
))
491 static void show_symbol_usage(pseudo_t pseudo
)
495 FOR_EACH_PTR(pseudo
->users
, pp
) {
496 struct instruction
*insn
= container(pp
, struct instruction
, src
);
497 show_instruction(insn
);
498 } END_FOR_EACH_PTR(pp
);
502 void show_entry(struct entrypoint
*ep
)
505 struct basic_block
*bb
;
507 printf("%s:\n", show_ident(ep
->name
->ident
));
510 printf("ep %p: %s\n", ep
, show_ident(ep
->name
->ident
));
512 FOR_EACH_PTR(ep
->syms
, sym
) {
515 if (!sym
->pseudo
->users
)
517 printf(" sym: %p %s\n", sym
, show_ident(sym
->ident
));
518 if (sym
->ctype
.modifiers
& (MOD_EXTERN
| MOD_STATIC
| MOD_ADDRESSABLE
))
519 printf("\texternal visibility\n");
520 show_symbol_usage(sym
->pseudo
);
521 } END_FOR_EACH_PTR(sym
);
526 FOR_EACH_PTR(ep
->bbs
, bb
) {
529 if (!bb
->parents
&& !bb
->children
&& !bb
->insns
&& verbose
< 2)
532 } END_FOR_EACH_PTR(bb
);
537 static void bind_label(struct symbol
*label
, struct basic_block
*bb
, struct position pos
)
539 if (label
->bb_target
)
540 warning(pos
, "label '%s' already bound", show_ident(label
->ident
));
541 label
->bb_target
= bb
;
544 static struct basic_block
* get_bound_block(struct entrypoint
*ep
, struct symbol
*label
)
546 struct basic_block
*bb
= label
->bb_target
;
549 bb
= alloc_basic_block(ep
, label
->pos
);
550 label
->bb_target
= bb
;
555 static void finish_block(struct entrypoint
*ep
)
557 struct basic_block
*src
= ep
->active
;
558 if (bb_reachable(src
))
562 static void add_goto(struct entrypoint
*ep
, struct basic_block
*dst
)
564 struct basic_block
*src
= ep
->active
;
565 if (bb_reachable(src
)) {
566 struct instruction
*br
= alloc_instruction(OP_BR
, 0);
568 add_bb(&dst
->parents
, src
);
569 add_bb(&src
->children
, dst
);
571 add_instruction(&src
->insns
, br
);
576 static void add_one_insn(struct entrypoint
*ep
, struct instruction
*insn
)
578 struct basic_block
*bb
= ep
->active
;
580 if (bb_reachable(bb
)) {
582 add_instruction(&bb
->insns
, insn
);
586 static void set_activeblock(struct entrypoint
*ep
, struct basic_block
*bb
)
588 if (!bb_terminated(ep
->active
))
592 if (bb_reachable(bb
))
593 add_bb(&ep
->bbs
, bb
);
596 static void remove_parent(struct basic_block
*child
, struct basic_block
*parent
)
598 remove_bb_from_list(&child
->parents
, parent
, 1);
603 /* Change a "switch" into a branch */
604 void insert_branch(struct basic_block
*bb
, struct instruction
*jmp
, struct basic_block
*target
)
606 struct instruction
*br
, *old
;
607 struct basic_block
*child
;
609 /* Remove the switch */
610 old
= delete_last_instruction(&bb
->insns
);
613 br
= alloc_instruction(OP_BR
, 0);
615 br
->bb_true
= target
;
616 add_instruction(&bb
->insns
, br
);
618 FOR_EACH_PTR(bb
->children
, child
) {
619 if (child
== target
) {
620 target
= NULL
; /* Trigger just once */
623 DELETE_CURRENT_PTR(child
);
624 remove_parent(child
, bb
);
625 } END_FOR_EACH_PTR(child
);
626 PACK_PTR_LIST(&bb
->children
);
630 void insert_select(struct basic_block
*bb
, struct instruction
*br
, struct instruction
*phi_node
, pseudo_t
true, pseudo_t
false)
633 struct instruction
*select
;
635 /* Remove the 'br' */
636 delete_last_instruction(&bb
->insns
);
638 select
= alloc_instruction(OP_SEL
, phi_node
->size
);
642 use_pseudo(br
->cond
, &select
->src1
);
644 target
= phi_node
->target
;
645 assert(target
->def
== phi_node
);
646 select
->target
= target
;
647 target
->def
= select
;
649 use_pseudo(true, &select
->src2
);
650 use_pseudo(false, &select
->src3
);
652 add_instruction(&bb
->insns
, select
);
653 add_instruction(&bb
->insns
, br
);
656 static inline int bb_empty(struct basic_block
*bb
)
661 /* Add a label to the currently active block, return new active block */
662 static struct basic_block
* add_label(struct entrypoint
*ep
, struct symbol
*label
)
664 struct basic_block
*bb
= label
->bb_target
;
667 set_activeblock(ep
, bb
);
671 if (!bb_reachable(bb
) || !bb_empty(bb
)) {
672 bb
= alloc_basic_block(ep
, label
->pos
);
673 set_activeblock(ep
, bb
);
675 label
->bb_target
= bb
;
679 static void add_branch(struct entrypoint
*ep
, struct expression
*expr
, pseudo_t cond
, struct basic_block
*bb_true
, struct basic_block
*bb_false
)
681 struct basic_block
*bb
= ep
->active
;
682 struct instruction
*br
;
684 if (bb_reachable(bb
)) {
685 br
= alloc_instruction(OP_BR
, 0);
686 use_pseudo(cond
, &br
->cond
);
687 br
->bb_true
= bb_true
;
688 br
->bb_false
= bb_false
;
689 add_bb(&bb_true
->parents
, bb
);
690 add_bb(&bb_false
->parents
, bb
);
691 add_bb(&bb
->children
, bb_true
);
692 add_bb(&bb
->children
, bb_false
);
693 add_one_insn(ep
, br
);
697 /* Dummy pseudo allocator */
698 pseudo_t
alloc_pseudo(struct instruction
*def
)
701 struct pseudo
* pseudo
= __alloc_pseudo(0);
702 pseudo
->type
= PSEUDO_REG
;
708 static void clear_symbol_pseudos(struct entrypoint
*ep
)
712 FOR_EACH_PTR(ep
->accesses
, sym
) {
714 } END_FOR_EACH_PTR(sym
);
717 static pseudo_t
symbol_pseudo(struct entrypoint
*ep
, struct symbol
*sym
)
724 pseudo
= sym
->pseudo
;
726 pseudo
= __alloc_pseudo(0);
727 pseudo
->type
= PSEUDO_SYM
;
729 pseudo
->ident
= sym
->ident
;
730 sym
->pseudo
= pseudo
;
731 add_symbol(&ep
->accesses
, sym
);
733 /* Symbol pseudos have neither nr, usage nor def */
737 pseudo_t
value_pseudo(long long val
)
739 #define MAX_VAL_HASH 64
740 static struct pseudo_list
*prev
[MAX_VAL_HASH
];
741 int hash
= val
& (MAX_VAL_HASH
-1);
742 struct pseudo_list
**list
= prev
+ hash
;
745 FOR_EACH_PTR(*list
, pseudo
) {
746 if (pseudo
->value
== val
)
748 } END_FOR_EACH_PTR(pseudo
);
750 pseudo
= __alloc_pseudo(0);
751 pseudo
->type
= PSEUDO_VAL
;
753 add_pseudo(list
, pseudo
);
755 /* Value pseudos have neither nr, usage nor def */
759 static pseudo_t
argument_pseudo(struct entrypoint
*ep
, int nr
)
761 pseudo_t pseudo
= __alloc_pseudo(0);
762 pseudo
->type
= PSEUDO_ARG
;
764 pseudo
->def
= ep
->entry
;
765 /* Argument pseudos have neither usage nor def */
769 pseudo_t
alloc_phi(struct basic_block
*source
, pseudo_t pseudo
, int size
)
771 struct instruction
*insn
= alloc_instruction(OP_PHISOURCE
, size
);
772 pseudo_t phi
= __alloc_pseudo(0);
775 phi
->type
= PSEUDO_PHI
;
779 use_pseudo(pseudo
, &insn
->src1
);
782 add_instruction(&source
->insns
, insn
);
787 * We carry the "access_data" structure around for any accesses,
788 * which simplifies things a lot. It contains all the access
789 * information in one place.
792 struct symbol
*result_type
; // result ctype
793 struct symbol
*source_type
; // source ctype
794 pseudo_t address
; // pseudo containing address ..
795 pseudo_t origval
; // pseudo for original value ..
796 unsigned int offset
, alignment
; // byte offset
797 unsigned int bit_size
, bit_offset
; // which bits
801 static void finish_address_gen(struct entrypoint
*ep
, struct access_data
*ad
)
805 static int linearize_simple_address(struct entrypoint
*ep
,
806 struct expression
*addr
,
807 struct access_data
*ad
)
809 if (addr
->type
== EXPR_SYMBOL
) {
810 ad
->address
= symbol_pseudo(ep
, addr
->symbol
);
813 if (addr
->type
== EXPR_BINOP
) {
814 if (addr
->right
->type
== EXPR_VALUE
) {
815 if (addr
->op
== '+') {
816 ad
->offset
+= get_expression_value(addr
->right
);
817 return linearize_simple_address(ep
, addr
->left
, ad
);
821 ad
->address
= linearize_expression(ep
, addr
);
825 static struct symbol
*base_type(struct symbol
*sym
)
827 struct symbol
*base
= sym
;
830 if (sym
->type
== SYM_NODE
)
831 base
= base
->ctype
.base_type
;
832 if (base
->type
== SYM_BITFIELD
)
833 return base
->ctype
.base_type
;
838 static int linearize_address_gen(struct entrypoint
*ep
,
839 struct expression
*expr
,
840 struct access_data
*ad
)
842 struct symbol
*ctype
= expr
->ctype
;
847 ad
->result_type
= ctype
;
848 ad
->source_type
= base_type(ctype
);
849 ad
->bit_size
= ctype
->bit_size
;
850 ad
->alignment
= ctype
->ctype
.alignment
;
851 ad
->bit_offset
= ctype
->bit_offset
;
852 if (expr
->type
== EXPR_PREOP
&& expr
->op
== '*')
853 return linearize_simple_address(ep
, expr
->unop
, ad
);
855 warning(expr
->pos
, "generating address of non-lvalue (%d)", expr
->type
);
859 static pseudo_t
add_load(struct entrypoint
*ep
, struct access_data
*ad
)
861 struct instruction
*insn
;
868 insn
= alloc_typed_instruction(OP_LOAD
, ad
->source_type
);
869 new = alloc_pseudo(insn
);
873 insn
->offset
= ad
->offset
;
874 use_pseudo(ad
->address
, &insn
->src
);
875 add_one_insn(ep
, insn
);
879 static void add_store(struct entrypoint
*ep
, struct access_data
*ad
, pseudo_t value
)
881 struct basic_block
*bb
= ep
->active
;
883 if (bb_reachable(bb
)) {
884 struct instruction
*store
= alloc_typed_instruction(OP_STORE
, ad
->source_type
);
885 store
->offset
= ad
->offset
;
886 use_pseudo(value
, &store
->target
);
887 use_pseudo(ad
->address
, &store
->src
);
888 add_one_insn(ep
, store
);
892 static pseudo_t
linearize_store_gen(struct entrypoint
*ep
,
894 struct access_data
*ad
)
896 pseudo_t store
= value
;
898 if (type_size(ad
->source_type
) != type_size(ad
->result_type
)) {
899 pseudo_t orig
= add_load(ep
, ad
);
900 int shift
= ad
->bit_offset
;
901 unsigned long long mask
= (1ULL << ad
->bit_size
)-1;
904 store
= add_binary_op(ep
, ad
->source_type
, OP_SHL
, value
, value_pseudo(shift
));
907 orig
= add_binary_op(ep
, ad
->source_type
, OP_AND
, orig
, value_pseudo(~mask
));
908 store
= add_binary_op(ep
, ad
->source_type
, OP_OR
, orig
, store
);
910 add_store(ep
, ad
, store
);
914 static pseudo_t
add_binary_op(struct entrypoint
*ep
, struct symbol
*ctype
, int op
, pseudo_t left
, pseudo_t right
)
916 struct instruction
*insn
= alloc_typed_instruction(op
, ctype
);
917 pseudo_t target
= alloc_pseudo(insn
);
918 insn
->target
= target
;
919 use_pseudo(left
, &insn
->src1
);
920 use_pseudo(right
, &insn
->src2
);
921 add_one_insn(ep
, insn
);
925 static pseudo_t
add_setval(struct entrypoint
*ep
, struct symbol
*ctype
, struct expression
*val
)
927 struct instruction
*insn
= alloc_typed_instruction(OP_SETVAL
, ctype
);
928 pseudo_t target
= alloc_pseudo(insn
);
929 insn
->target
= target
;
932 pseudo_t addr
= symbol_pseudo(ep
, ctype
);
933 use_pseudo(addr
, &insn
->symbol
);
934 insn
->size
= bits_in_pointer
;
936 add_one_insn(ep
, insn
);
940 static pseudo_t
linearize_load_gen(struct entrypoint
*ep
, struct access_data
*ad
)
942 pseudo_t
new = add_load(ep
, ad
);
944 if (ad
->bit_offset
) {
945 pseudo_t shift
= value_pseudo(ad
->bit_offset
);
946 pseudo_t newval
= add_binary_op(ep
, ad
->source_type
, OP_SHR
, new, shift
);
953 static pseudo_t
linearize_access(struct entrypoint
*ep
, struct expression
*expr
)
955 struct access_data ad
= { NULL
, };
958 if (!linearize_address_gen(ep
, expr
, &ad
))
960 value
= linearize_load_gen(ep
, &ad
);
961 finish_address_gen(ep
, &ad
);
966 static pseudo_t
linearize_inc_dec(struct entrypoint
*ep
, struct expression
*expr
, int postop
)
968 struct access_data ad
= { NULL
, };
969 pseudo_t old
, new, one
;
970 int op
= expr
->op
== SPECIAL_INCREMENT
? OP_ADD
: OP_SUB
;
972 if (!linearize_address_gen(ep
, expr
->unop
, &ad
))
975 old
= linearize_load_gen(ep
, &ad
);
976 one
= value_pseudo(expr
->op_value
);
977 new = add_binary_op(ep
, expr
->ctype
, op
, old
, one
);
978 linearize_store_gen(ep
, new, &ad
);
979 finish_address_gen(ep
, &ad
);
980 return postop
? old
: new;
983 static pseudo_t
add_uniop(struct entrypoint
*ep
, struct expression
*expr
, int op
, pseudo_t src
)
985 struct instruction
*insn
= alloc_typed_instruction(op
, expr
->ctype
);
986 pseudo_t
new = alloc_pseudo(insn
);
989 use_pseudo(src
, &insn
->src1
);
990 add_one_insn(ep
, insn
);
994 static pseudo_t
linearize_slice(struct entrypoint
*ep
, struct expression
*expr
)
996 pseudo_t pre
= linearize_expression(ep
, expr
->base
);
997 struct instruction
*insn
= alloc_typed_instruction(OP_SLICE
, expr
->ctype
);
998 pseudo_t
new = alloc_pseudo(insn
);
1001 insn
->from
= expr
->r_bitpos
;
1002 insn
->len
= expr
->r_nrbits
;
1003 use_pseudo(pre
, &insn
->base
);
1004 add_one_insn(ep
, insn
);
1008 static pseudo_t
linearize_regular_preop(struct entrypoint
*ep
, struct expression
*expr
)
1010 pseudo_t pre
= linearize_expression(ep
, expr
->unop
);
1015 pseudo_t zero
= value_pseudo(0);
1016 return add_binary_op(ep
, expr
->ctype
, OP_SET_EQ
, pre
, zero
);
1019 return add_uniop(ep
, expr
, OP_NOT
, pre
);
1021 return add_uniop(ep
, expr
, OP_NEG
, pre
);
1026 static pseudo_t
linearize_preop(struct entrypoint
*ep
, struct expression
*expr
)
1029 * '*' is an lvalue access, and is fundamentally different
1030 * from an arithmetic operation. Maybe it should have an
1031 * expression type of its own..
1033 if (expr
->op
== '*')
1034 return linearize_access(ep
, expr
);
1035 if (expr
->op
== SPECIAL_INCREMENT
|| expr
->op
== SPECIAL_DECREMENT
)
1036 return linearize_inc_dec(ep
, expr
, 0);
1037 return linearize_regular_preop(ep
, expr
);
1040 static pseudo_t
linearize_postop(struct entrypoint
*ep
, struct expression
*expr
)
1042 return linearize_inc_dec(ep
, expr
, 1);
1045 static pseudo_t
linearize_assignment(struct entrypoint
*ep
, struct expression
*expr
)
1047 struct access_data ad
= { NULL
, };
1048 struct expression
*target
= expr
->left
;
1051 value
= linearize_expression(ep
, expr
->right
);
1052 if (!linearize_address_gen(ep
, target
, &ad
))
1054 if (expr
->op
!= '=') {
1055 pseudo_t oldvalue
= linearize_load_gen(ep
, &ad
);
1057 static const int op_trans
[] = {
1058 [SPECIAL_ADD_ASSIGN
- SPECIAL_BASE
] = OP_ADD
,
1059 [SPECIAL_SUB_ASSIGN
- SPECIAL_BASE
] = OP_SUB
,
1060 [SPECIAL_MUL_ASSIGN
- SPECIAL_BASE
] = OP_MUL
,
1061 [SPECIAL_DIV_ASSIGN
- SPECIAL_BASE
] = OP_DIV
,
1062 [SPECIAL_MOD_ASSIGN
- SPECIAL_BASE
] = OP_MOD
,
1063 [SPECIAL_SHL_ASSIGN
- SPECIAL_BASE
] = OP_SHL
,
1064 [SPECIAL_SHR_ASSIGN
- SPECIAL_BASE
] = OP_SHR
,
1065 [SPECIAL_AND_ASSIGN
- SPECIAL_BASE
] = OP_AND
,
1066 [SPECIAL_OR_ASSIGN
- SPECIAL_BASE
] = OP_OR
,
1067 [SPECIAL_XOR_ASSIGN
- SPECIAL_BASE
] = OP_XOR
1069 dst
= add_binary_op(ep
, expr
->ctype
, op_trans
[expr
->op
- SPECIAL_BASE
], oldvalue
, value
);
1072 value
= linearize_store_gen(ep
, value
, &ad
);
1073 finish_address_gen(ep
, &ad
);
1077 static pseudo_t
linearize_call_expression(struct entrypoint
*ep
, struct expression
*expr
)
1079 struct expression
*arg
, *fn
;
1080 struct instruction
*insn
= alloc_typed_instruction(OP_CALL
, expr
->ctype
);
1081 pseudo_t retval
, call
;
1085 warning(expr
->pos
, "call with no type!");
1089 FOR_EACH_PTR(expr
->args
, arg
) {
1090 pseudo_t
new = linearize_expression(ep
, arg
);
1091 use_pseudo(new, add_pseudo(&insn
->arguments
, new));
1092 } END_FOR_EACH_PTR(arg
);
1098 int in
= fn
->ctype
->ctype
.in_context
;
1099 int out
= fn
->ctype
->ctype
.out_context
;
1100 if (in
< 0 || out
< 0)
1102 context_diff
= out
- in
;
1105 if (fn
->type
== EXPR_PREOP
) {
1106 if (fn
->unop
->type
== EXPR_SYMBOL
) {
1107 struct symbol
*sym
= fn
->unop
->symbol
;
1108 if (sym
->ctype
.base_type
->type
== SYM_FN
)
1112 if (fn
->type
== EXPR_SYMBOL
) {
1113 call
= symbol_pseudo(ep
, fn
->symbol
);
1115 call
= linearize_expression(ep
, fn
);
1117 use_pseudo(call
, &insn
->func
);
1119 if (expr
->ctype
!= &void_ctype
)
1120 retval
= alloc_pseudo(insn
);
1121 insn
->target
= retval
;
1122 add_one_insn(ep
, insn
);
1125 insn
= alloc_instruction(OP_CONTEXT
, 0);
1126 insn
->increment
= context_diff
;
1127 add_one_insn(ep
, insn
);
1133 static pseudo_t
linearize_binop(struct entrypoint
*ep
, struct expression
*expr
)
1135 pseudo_t src1
, src2
, dst
;
1136 static const int opcode
[] = {
1137 ['+'] = OP_ADD
, ['-'] = OP_SUB
,
1138 ['*'] = OP_MUL
, ['/'] = OP_DIV
,
1139 ['%'] = OP_MOD
, ['&'] = OP_AND
,
1140 ['|'] = OP_OR
, ['^'] = OP_XOR
,
1141 [SPECIAL_LEFTSHIFT
] = OP_SHL
,
1142 [SPECIAL_RIGHTSHIFT
] = OP_SHR
,
1143 [SPECIAL_LOGICAL_AND
] = OP_AND_BOOL
,
1144 [SPECIAL_LOGICAL_OR
] = OP_OR_BOOL
,
1147 src1
= linearize_expression(ep
, expr
->left
);
1148 src2
= linearize_expression(ep
, expr
->right
);
1149 dst
= add_binary_op(ep
, expr
->ctype
, opcode
[expr
->op
], src1
, src2
);
1153 static pseudo_t
linearize_logical_branch(struct entrypoint
*ep
, struct expression
*expr
, struct basic_block
*bb_true
, struct basic_block
*bb_false
);
1155 pseudo_t
linearize_cond_branch(struct entrypoint
*ep
, struct expression
*expr
, struct basic_block
*bb_true
, struct basic_block
*bb_false
);
1157 static pseudo_t
linearize_select(struct entrypoint
*ep
, struct expression
*expr
)
1159 pseudo_t cond
, true, false, res
;
1160 struct instruction
*insn
;
1162 true = linearize_expression(ep
, expr
->cond_true
);
1163 false = linearize_expression(ep
, expr
->cond_false
);
1164 cond
= linearize_expression(ep
, expr
->conditional
);
1166 insn
= alloc_typed_instruction(OP_SEL
, expr
->ctype
);
1167 if (!expr
->cond_true
)
1169 use_pseudo(cond
, &insn
->src1
);
1170 use_pseudo(true, &insn
->src2
);
1171 use_pseudo(false, &insn
->src3
);
1173 res
= alloc_pseudo(insn
);
1175 add_one_insn(ep
, insn
);
1179 static pseudo_t
add_join_conditional(struct entrypoint
*ep
, struct expression
*expr
,
1180 pseudo_t phi1
, pseudo_t phi2
)
1183 struct instruction
*phi_node
;
1190 phi_node
= alloc_typed_instruction(OP_PHI
, expr
->ctype
);
1191 use_pseudo(phi1
, add_pseudo(&phi_node
->phi_list
, phi1
));
1192 use_pseudo(phi2
, add_pseudo(&phi_node
->phi_list
, phi2
));
1193 phi_node
->target
= target
= alloc_pseudo(phi_node
);
1194 add_one_insn(ep
, phi_node
);
1198 static pseudo_t
linearize_short_conditional(struct entrypoint
*ep
, struct expression
*expr
,
1199 struct expression
*cond
,
1200 struct expression
*expr_false
)
1202 pseudo_t src1
, src2
;
1203 struct basic_block
*bb_false
= alloc_basic_block(ep
, expr_false
->pos
);
1204 struct basic_block
*merge
= alloc_basic_block(ep
, expr
->pos
);
1205 pseudo_t phi1
, phi2
;
1206 int size
= type_size(expr
->ctype
);
1208 src1
= linearize_expression(ep
, cond
);
1209 phi1
= alloc_phi(ep
->active
, src1
, size
);
1210 add_branch(ep
, expr
, src1
, merge
, bb_false
);
1212 set_activeblock(ep
, bb_false
);
1213 src2
= linearize_expression(ep
, expr_false
);
1214 phi2
= alloc_phi(ep
->active
, src2
, size
);
1215 set_activeblock(ep
, merge
);
1217 return add_join_conditional(ep
, expr
, phi1
, phi2
);
1220 static pseudo_t
linearize_conditional(struct entrypoint
*ep
, struct expression
*expr
,
1221 struct expression
*cond
,
1222 struct expression
*expr_true
,
1223 struct expression
*expr_false
)
1225 pseudo_t src1
, src2
;
1226 pseudo_t phi1
, phi2
;
1227 struct basic_block
*bb_true
= alloc_basic_block(ep
, expr_true
->pos
);
1228 struct basic_block
*bb_false
= alloc_basic_block(ep
, expr_false
->pos
);
1229 struct basic_block
*merge
= alloc_basic_block(ep
, expr
->pos
);
1230 int size
= type_size(expr
->ctype
);
1232 linearize_cond_branch(ep
, cond
, bb_true
, bb_false
);
1234 set_activeblock(ep
, bb_true
);
1235 src1
= linearize_expression(ep
, expr_true
);
1236 phi1
= alloc_phi(ep
->active
, src1
, size
);
1237 add_goto(ep
, merge
);
1239 set_activeblock(ep
, bb_false
);
1240 src2
= linearize_expression(ep
, expr_false
);
1241 phi2
= alloc_phi(ep
->active
, src2
, size
);
1242 set_activeblock(ep
, merge
);
1244 return add_join_conditional(ep
, expr
, phi1
, phi2
);
1247 static pseudo_t
linearize_logical(struct entrypoint
*ep
, struct expression
*expr
)
1249 struct expression
*shortcut
;
1251 shortcut
= alloc_const_expression(expr
->pos
, expr
->op
== SPECIAL_LOGICAL_OR
);
1252 shortcut
->ctype
= expr
->ctype
;
1253 return linearize_conditional(ep
, expr
, expr
->left
, shortcut
, expr
->right
);
1256 static pseudo_t
linearize_compare(struct entrypoint
*ep
, struct expression
*expr
)
1258 static const int cmpop
[] = {
1259 ['>'] = OP_SET_GT
, ['<'] = OP_SET_LT
,
1260 [SPECIAL_EQUAL
] = OP_SET_EQ
,
1261 [SPECIAL_NOTEQUAL
] = OP_SET_NE
,
1262 [SPECIAL_GTE
] = OP_SET_GE
,
1263 [SPECIAL_LTE
] = OP_SET_LE
,
1264 [SPECIAL_UNSIGNED_LT
] = OP_SET_B
,
1265 [SPECIAL_UNSIGNED_GT
] = OP_SET_A
,
1266 [SPECIAL_UNSIGNED_LTE
] = OP_SET_BE
,
1267 [SPECIAL_UNSIGNED_GTE
] = OP_SET_AE
,
1270 pseudo_t src1
= linearize_expression(ep
, expr
->left
);
1271 pseudo_t src2
= linearize_expression(ep
, expr
->right
);
1272 pseudo_t dst
= add_binary_op(ep
, expr
->ctype
, cmpop
[expr
->op
], src1
, src2
);
1277 pseudo_t
linearize_cond_branch(struct entrypoint
*ep
, struct expression
*expr
, struct basic_block
*bb_true
, struct basic_block
*bb_false
)
1281 if (!expr
|| !bb_reachable(ep
->active
))
1284 switch (expr
->type
) {
1288 add_goto(ep
, expr
->value
? bb_true
: bb_false
);
1292 add_goto(ep
, expr
->fvalue
? bb_true
: bb_false
);
1296 linearize_logical_branch(ep
, expr
, bb_true
, bb_false
);
1300 cond
= linearize_compare(ep
, expr
);
1301 add_branch(ep
, expr
, cond
, bb_true
, bb_false
);
1305 if (expr
->op
== '!')
1306 return linearize_cond_branch(ep
, expr
->unop
, bb_false
, bb_true
);
1309 cond
= linearize_expression(ep
, expr
);
1310 add_branch(ep
, expr
, cond
, bb_true
, bb_false
);
1320 static pseudo_t
linearize_logical_branch(struct entrypoint
*ep
, struct expression
*expr
, struct basic_block
*bb_true
, struct basic_block
*bb_false
)
1322 struct basic_block
*next
= alloc_basic_block(ep
, expr
->pos
);
1324 if (expr
->op
== SPECIAL_LOGICAL_OR
)
1325 linearize_cond_branch(ep
, expr
->left
, bb_true
, next
);
1327 linearize_cond_branch(ep
, expr
->left
, next
, bb_false
);
1328 set_activeblock(ep
, next
);
1329 linearize_cond_branch(ep
, expr
->right
, bb_true
, bb_false
);
1334 * Casts to pointers are "less safe" than other casts, since
1335 * they imply type-unsafe accesses. "void *" is a special
1336 * case, since you can't access through it anyway without another
1339 static struct instruction
*alloc_cast_instruction(struct symbol
*ctype
)
1341 int opcode
= OP_CAST
;
1342 struct symbol
*base
= ctype
;
1344 if (base
->type
== SYM_NODE
)
1345 base
= base
->ctype
.base_type
;
1346 if (base
->type
== SYM_PTR
) {
1347 base
= base
->ctype
.base_type
;
1348 if (base
!= &void_ctype
)
1349 opcode
= OP_PTRCAST
;
1351 return alloc_typed_instruction(opcode
, ctype
);
1354 pseudo_t
linearize_cast(struct entrypoint
*ep
, struct expression
*expr
)
1356 pseudo_t src
, result
;
1357 struct instruction
*insn
;
1359 src
= linearize_expression(ep
, expr
->cast_expression
);
1364 if (expr
->ctype
->bit_size
< 0)
1367 insn
= alloc_cast_instruction(expr
->ctype
);
1368 result
= alloc_pseudo(insn
);
1369 insn
->target
= result
;
1370 insn
->orig_type
= expr
->cast_expression
->ctype
;
1371 use_pseudo(src
, &insn
->src
);
1372 add_one_insn(ep
, insn
);
1376 pseudo_t
linearize_position(struct entrypoint
*ep
, struct expression
*pos
, struct access_data
*ad
)
1378 struct expression
*init_expr
= pos
->init_expr
;
1379 pseudo_t value
= linearize_expression(ep
, init_expr
);
1381 ad
->offset
= pos
->init_offset
;
1382 ad
->source_type
= base_type(init_expr
->ctype
);
1383 ad
->result_type
= init_expr
->ctype
;
1384 linearize_store_gen(ep
, value
, ad
);
1388 pseudo_t
linearize_initializer(struct entrypoint
*ep
, struct expression
*initializer
, struct access_data
*ad
)
1390 switch (initializer
->type
) {
1391 case EXPR_INITIALIZER
: {
1392 struct expression
*expr
;
1393 FOR_EACH_PTR(initializer
->expr_list
, expr
) {
1394 linearize_initializer(ep
, expr
, ad
);
1395 } END_FOR_EACH_PTR(expr
);
1399 linearize_position(ep
, initializer
, ad
);
1402 pseudo_t value
= linearize_expression(ep
, initializer
);
1403 ad
->source_type
= base_type(initializer
->ctype
);
1404 ad
->result_type
= initializer
->ctype
;
1405 linearize_store_gen(ep
, value
, ad
);
1412 void linearize_argument(struct entrypoint
*ep
, struct symbol
*arg
, int nr
)
1414 struct access_data ad
= { NULL
, };
1416 ad
.source_type
= arg
;
1417 ad
.result_type
= arg
;
1418 ad
.address
= symbol_pseudo(ep
, arg
);
1419 linearize_store_gen(ep
, argument_pseudo(ep
, nr
), &ad
);
1420 finish_address_gen(ep
, &ad
);
1423 pseudo_t
linearize_expression(struct entrypoint
*ep
, struct expression
*expr
)
1428 switch (expr
->type
) {
1430 return add_setval(ep
, expr
->symbol
, NULL
);
1433 return value_pseudo(expr
->value
);
1435 case EXPR_STRING
: case EXPR_FVALUE
: case EXPR_LABEL
:
1436 return add_setval(ep
, expr
->ctype
, expr
);
1438 case EXPR_STATEMENT
:
1439 return linearize_statement(ep
, expr
->statement
);
1442 return linearize_call_expression(ep
, expr
);
1445 return linearize_binop(ep
, expr
);
1448 return linearize_logical(ep
, expr
);
1451 return linearize_compare(ep
, expr
);
1454 return linearize_select(ep
, expr
);
1456 case EXPR_CONDITIONAL
:
1457 if (!expr
->cond_true
)
1458 return linearize_short_conditional(ep
, expr
, expr
->conditional
, expr
->cond_false
);
1460 return linearize_conditional(ep
, expr
, expr
->conditional
,
1461 expr
->cond_true
, expr
->cond_false
);
1464 linearize_expression(ep
, expr
->left
);
1465 return linearize_expression(ep
, expr
->right
);
1467 case EXPR_ASSIGNMENT
:
1468 return linearize_assignment(ep
, expr
);
1471 return linearize_preop(ep
, expr
);
1474 return linearize_postop(ep
, expr
);
1477 case EXPR_IMPLIED_CAST
:
1478 return linearize_cast(ep
, expr
);
1481 return linearize_slice(ep
, expr
);
1483 case EXPR_INITIALIZER
:
1485 warning(expr
->pos
, "unexpected initializer expression (%d %d)", expr
->type
, expr
->op
);
1488 warning(expr
->pos
, "unknown expression (%d %d)", expr
->type
, expr
->op
);
1494 static void linearize_one_symbol(struct entrypoint
*ep
, struct symbol
*sym
)
1496 struct access_data ad
= { NULL
, };
1498 if (!sym
->initializer
)
1501 ad
.address
= symbol_pseudo(ep
, sym
);
1502 linearize_initializer(ep
, sym
->initializer
, &ad
);
1503 finish_address_gen(ep
, &ad
);
1506 static pseudo_t
linearize_compound_statement(struct entrypoint
*ep
, struct statement
*stmt
)
1509 struct statement
*s
;
1511 struct symbol
*ret
= stmt
->ret
;
1513 concat_symbol_list(stmt
->syms
, &ep
->syms
);
1515 FOR_EACH_PTR(stmt
->syms
, sym
) {
1516 linearize_one_symbol(ep
, sym
);
1517 } END_FOR_EACH_PTR(sym
);
1520 FOR_EACH_PTR(stmt
->stmts
, s
) {
1521 pseudo
= linearize_statement(ep
, s
);
1522 } END_FOR_EACH_PTR(s
);
1525 struct basic_block
*bb
= add_label(ep
, ret
);
1526 struct instruction
*phi_node
= first_instruction(bb
->insns
);
1531 if (pseudo_list_size(phi_node
->phi_list
)==1) {
1532 pseudo
= first_pseudo(phi_node
->phi_list
);
1533 assert(pseudo
->type
== PSEUDO_PHI
);
1534 return pseudo
->def
->src1
;
1536 return phi_node
->target
;
1541 pseudo_t
linearize_internal(struct entrypoint
*ep
, struct statement
*stmt
)
1543 struct instruction
*insn
= alloc_instruction(OP_CONTEXT
, 0);
1544 struct expression
*expr
= stmt
->expression
;
1547 if (expr
->type
== EXPR_VALUE
)
1548 value
= expr
->value
;
1550 insn
->increment
= value
;
1551 add_one_insn(ep
, insn
);
1555 static void add_asm_input(struct entrypoint
*ep
, struct instruction
*insn
, struct expression
*expr
)
1557 pseudo_t pseudo
= linearize_expression(ep
, expr
);
1559 use_pseudo(pseudo
, add_pseudo(&insn
->inputs
, pseudo
));
1562 static void add_asm_output(struct entrypoint
*ep
, struct instruction
*insn
, struct expression
*expr
)
1564 struct access_data ad
= { NULL
, };
1565 pseudo_t pseudo
= alloc_pseudo(insn
);
1567 if (!linearize_address_gen(ep
, expr
, &ad
))
1569 linearize_store_gen(ep
, pseudo
, &ad
);
1570 finish_address_gen(ep
, &ad
);
1571 add_pseudo(&insn
->outputs
, pseudo
);
1574 pseudo_t
linearize_asm_statement(struct entrypoint
*ep
, struct statement
*stmt
)
1577 struct expression
*expr
;
1578 struct instruction
*insn
;
1580 insn
= alloc_instruction(OP_ASM
, 0);
1581 expr
= stmt
->asm_string
;
1582 if (!expr
|| expr
->type
!= EXPR_STRING
) {
1583 warning(stmt
->pos
, "expected string in inline asm");
1586 insn
->string
= expr
->string
->data
;
1588 /* Gather the inputs.. */
1590 FOR_EACH_PTR(stmt
->asm_inputs
, expr
) {
1591 even_odd
= 1 - even_odd
;
1593 /* FIXME! We ignore the constraints for now.. */
1596 add_asm_input(ep
, insn
, expr
);
1597 } END_FOR_EACH_PTR(expr
);
1599 add_one_insn(ep
, insn
);
1601 /* Assign the outputs */
1603 FOR_EACH_PTR(stmt
->asm_outputs
, expr
) {
1604 even_odd
= 1 - even_odd
;
1606 /* FIXME! We ignore the constraints for now.. */
1609 add_asm_output(ep
, insn
, expr
);
1610 } END_FOR_EACH_PTR(expr
);
1615 pseudo_t
linearize_statement(struct entrypoint
*ep
, struct statement
*stmt
)
1617 struct basic_block
*bb
;
1623 if (bb
&& !bb
->insns
)
1624 bb
->pos
= stmt
->pos
;
1626 switch (stmt
->type
) {
1631 return linearize_internal(ep
, stmt
);
1633 case STMT_EXPRESSION
:
1634 return linearize_expression(ep
, stmt
->expression
);
1637 return linearize_asm_statement(ep
, stmt
);
1640 struct expression
*expr
= stmt
->expression
;
1641 struct basic_block
*bb_return
= get_bound_block(ep
, stmt
->ret_target
);
1642 struct basic_block
*active
;
1643 pseudo_t src
= linearize_expression(ep
, expr
);
1644 active
= ep
->active
;
1645 if (active
&& src
!= &void_pseudo
) {
1646 struct instruction
*phi_node
= first_instruction(bb_return
->insns
);
1649 phi_node
= alloc_typed_instruction(OP_PHI
, expr
->ctype
);
1650 phi_node
->target
= alloc_pseudo(phi_node
);
1651 phi_node
->bb
= bb_return
;
1652 add_instruction(&bb_return
->insns
, phi_node
);
1654 phi
= alloc_phi(active
, src
, type_size(expr
->ctype
));
1655 phi
->ident
= &return_ident
;
1656 use_pseudo(phi
, add_pseudo(&phi_node
->phi_list
, phi
));
1658 add_goto(ep
, bb_return
);
1663 add_label(ep
, stmt
->case_label
);
1664 linearize_statement(ep
, stmt
->case_statement
);
1669 struct symbol
*label
= stmt
->label_identifier
;
1672 add_label(ep
, label
);
1673 linearize_statement(ep
, stmt
->label_statement
);
1680 struct expression
*expr
;
1681 struct instruction
*goto_ins
;
1682 struct basic_block
*active
;
1685 active
= ep
->active
;
1686 if (!bb_reachable(active
))
1689 if (stmt
->goto_label
) {
1690 add_goto(ep
, get_bound_block(ep
, stmt
->goto_label
));
1694 expr
= stmt
->goto_expression
;
1698 /* This can happen as part of simplification */
1699 if (expr
->type
== EXPR_LABEL
) {
1700 add_goto(ep
, get_bound_block(ep
, expr
->label_symbol
));
1704 pseudo
= linearize_expression(ep
, expr
);
1705 goto_ins
= alloc_instruction(OP_COMPUTEDGOTO
, 0);
1706 use_pseudo(pseudo
, &goto_ins
->target
);
1707 add_one_insn(ep
, goto_ins
);
1709 FOR_EACH_PTR(stmt
->target_list
, sym
) {
1710 struct basic_block
*bb_computed
= get_bound_block(ep
, sym
);
1711 struct multijmp
*jmp
= alloc_multijmp(bb_computed
, 1, 0);
1712 add_multijmp(&goto_ins
->multijmp_list
, jmp
);
1713 add_bb(&bb_computed
->parents
, ep
->active
);
1714 add_bb(&active
->children
, bb_computed
);
1715 } END_FOR_EACH_PTR(sym
);
1722 return linearize_compound_statement(ep
, stmt
);
1725 * This could take 'likely/unlikely' into account, and
1726 * switch the arms around appropriately..
1729 struct basic_block
*bb_true
, *bb_false
, *endif
;
1730 struct expression
*cond
= stmt
->if_conditional
;
1732 bb_true
= alloc_basic_block(ep
, stmt
->pos
);
1733 bb_false
= endif
= alloc_basic_block(ep
, stmt
->pos
);
1735 linearize_cond_branch(ep
, cond
, bb_true
, bb_false
);
1737 set_activeblock(ep
, bb_true
);
1738 linearize_statement(ep
, stmt
->if_true
);
1740 if (stmt
->if_false
) {
1741 endif
= alloc_basic_block(ep
, stmt
->pos
);
1742 add_goto(ep
, endif
);
1743 set_activeblock(ep
, bb_false
);
1744 linearize_statement(ep
, stmt
->if_false
);
1746 set_activeblock(ep
, endif
);
1752 struct instruction
*switch_ins
;
1753 struct basic_block
*switch_end
= alloc_basic_block(ep
, stmt
->pos
);
1754 struct basic_block
*active
, *default_case
;
1755 struct multijmp
*jmp
;
1758 pseudo
= linearize_expression(ep
, stmt
->switch_expression
);
1760 active
= ep
->active
;
1761 if (!bb_reachable(active
))
1764 switch_ins
= alloc_instruction(OP_SWITCH
, 0);
1765 use_pseudo(pseudo
, &switch_ins
->cond
);
1766 add_one_insn(ep
, switch_ins
);
1769 default_case
= NULL
;
1770 FOR_EACH_PTR(stmt
->switch_case
->symbol_list
, sym
) {
1771 struct statement
*case_stmt
= sym
->stmt
;
1772 struct basic_block
*bb_case
= get_bound_block(ep
, sym
);
1774 if (!case_stmt
->case_expression
) {
1775 default_case
= bb_case
;
1780 begin
= end
= case_stmt
->case_expression
->value
;
1781 if (case_stmt
->case_to
)
1782 end
= case_stmt
->case_to
->value
;
1784 jmp
= alloc_multijmp(bb_case
, end
, begin
);
1786 jmp
= alloc_multijmp(bb_case
, begin
, end
);
1789 add_multijmp(&switch_ins
->multijmp_list
, jmp
);
1790 add_bb(&bb_case
->parents
, active
);
1791 add_bb(&active
->children
, bb_case
);
1792 } END_FOR_EACH_PTR(sym
);
1794 bind_label(stmt
->switch_break
, switch_end
, stmt
->pos
);
1796 /* And linearize the actual statement */
1797 linearize_statement(ep
, stmt
->switch_statement
);
1798 set_activeblock(ep
, switch_end
);
1801 default_case
= switch_end
;
1803 jmp
= alloc_multijmp(default_case
, 1, 0);
1804 add_multijmp(&switch_ins
->multijmp_list
, jmp
);
1805 add_bb(&default_case
->parents
, active
);
1806 add_bb(&active
->children
, default_case
);
1811 case STMT_ITERATOR
: {
1812 struct statement
*pre_statement
= stmt
->iterator_pre_statement
;
1813 struct expression
*pre_condition
= stmt
->iterator_pre_condition
;
1814 struct statement
*statement
= stmt
->iterator_statement
;
1815 struct statement
*post_statement
= stmt
->iterator_post_statement
;
1816 struct expression
*post_condition
= stmt
->iterator_post_condition
;
1817 struct basic_block
*loop_top
, *loop_body
, *loop_continue
, *loop_end
;
1819 concat_symbol_list(stmt
->iterator_syms
, &ep
->syms
);
1820 linearize_statement(ep
, pre_statement
);
1822 loop_body
= loop_top
= alloc_basic_block(ep
, stmt
->pos
);
1823 loop_continue
= alloc_basic_block(ep
, stmt
->pos
);
1824 loop_end
= alloc_basic_block(ep
, stmt
->pos
);
1826 if (pre_condition
== post_condition
) {
1827 loop_top
= alloc_basic_block(ep
, stmt
->pos
);
1828 set_activeblock(ep
, loop_top
);
1832 linearize_cond_branch(ep
, pre_condition
, loop_body
, loop_end
);
1834 bind_label(stmt
->iterator_continue
, loop_continue
, stmt
->pos
);
1835 bind_label(stmt
->iterator_break
, loop_end
, stmt
->pos
);
1837 set_activeblock(ep
, loop_body
);
1838 linearize_statement(ep
, statement
);
1839 add_goto(ep
, loop_continue
);
1841 set_activeblock(ep
, loop_continue
);
1842 linearize_statement(ep
, post_statement
);
1843 if (!post_condition
|| pre_condition
== post_condition
)
1844 add_goto(ep
, loop_top
);
1846 linearize_cond_branch(ep
, post_condition
, loop_top
, loop_end
);
1847 set_activeblock(ep
, loop_end
);
1857 static struct entrypoint
*linearize_fn(struct symbol
*sym
, struct symbol
*base_type
)
1859 struct entrypoint
*ep
;
1860 struct basic_block
*bb
;
1862 struct instruction
*entry
;
1866 if (!base_type
->stmt
)
1869 ep
= alloc_entrypoint();
1870 bb
= alloc_basic_block(ep
, sym
->pos
);
1873 set_activeblock(ep
, bb
);
1875 entry
= alloc_instruction(OP_ENTRY
, 0);
1876 add_one_insn(ep
, entry
);
1879 concat_symbol_list(base_type
->arguments
, &ep
->syms
);
1881 /* FIXME!! We should do something else about varargs.. */
1883 FOR_EACH_PTR(base_type
->arguments
, arg
) {
1884 linearize_argument(ep
, arg
, ++i
);
1885 } END_FOR_EACH_PTR(arg
);
1887 result
= linearize_statement(ep
, base_type
->stmt
);
1888 if (bb_reachable(ep
->active
) && !bb_terminated(ep
->active
)) {
1889 struct symbol
*ret_type
= base_type
->ctype
.base_type
;
1890 struct instruction
*insn
= alloc_typed_instruction(OP_RET
, ret_type
);
1892 if (type_size(ret_type
) > 0)
1893 use_pseudo(result
, &insn
->src
);
1894 add_one_insn(ep
, insn
);
1897 merge_phi_sources
= 1;
1901 * Do trivial flow simplification - branches to
1902 * branches, kill dead basicblocks etc
1904 kill_unreachable_bbs(ep
);
1907 * Turn symbols into pseudos
1909 simplify_symbol_usage(ep
);
1912 * Remove trivial instructions, and try to CSE
1916 cleanup_and_cse(ep
);
1917 pack_basic_blocks(ep
);
1918 } while (repeat_phase
& REPEAT_CSE
);
1923 clear_symbol_pseudos(ep
);
1925 /* And track pseudo register usage */
1926 track_pseudo_liveness(ep
);
1929 * Some flow optimizations can only effectively
1930 * be done when we've done liveness analysis. But
1931 * if they trigger, we need to start all over
1934 if (simplify_flow(ep
)) {
1939 /* Finally, add deathnotes to pseudos now that we have them */
1940 track_pseudo_death(ep
);
1945 struct entrypoint
*linearize_symbol(struct symbol
*sym
)
1947 struct symbol
*base_type
;
1951 base_type
= sym
->ctype
.base_type
;
1954 if (base_type
->type
== SYM_FN
)
1955 return linearize_fn(sym
, base_type
);