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
);
30 static void linearize_one_symbol(struct entrypoint
*ep
, struct symbol
*sym
);
33 static pseudo_t
add_load(struct entrypoint
*ep
, struct access_data
*);
34 pseudo_t
linearize_initializer(struct entrypoint
*ep
, struct expression
*initializer
, struct access_data
*);
36 struct pseudo void_pseudo
= {};
38 static struct position current_pos
;
40 static struct instruction
*alloc_instruction(int opcode
, int size
)
42 struct instruction
* insn
= __alloc_instruction(0);
43 insn
->opcode
= opcode
;
45 insn
->pos
= current_pos
;
49 static inline int type_size(struct symbol
*type
)
51 return type
? type
->bit_size
> 0 ? type
->bit_size
: 0 : 0;
54 static struct instruction
*alloc_typed_instruction(int opcode
, struct symbol
*type
)
56 return alloc_instruction(opcode
, type_size(type
));
59 static struct entrypoint
*alloc_entrypoint(void)
61 return __alloc_entrypoint(0);
64 static struct basic_block
*alloc_basic_block(struct entrypoint
*ep
, struct position pos
)
66 struct basic_block
*bb
= __alloc_basic_block(0);
73 static struct multijmp
* alloc_multijmp(struct basic_block
*target
, int begin
, int end
)
75 struct multijmp
*multijmp
= __alloc_multijmp(0);
76 multijmp
->target
= target
;
77 multijmp
->begin
= begin
;
82 static inline int regno(pseudo_t n
)
85 if (n
&& n
->type
== PSEUDO_REG
)
90 const char *show_pseudo(pseudo_t pseudo
)
93 static char buffer
[4][64];
101 buf
= buffer
[3 & ++n
];
102 switch(pseudo
->type
) {
104 struct symbol
*sym
= pseudo
->sym
;
105 struct expression
*expr
;
107 if (sym
->bb_target
) {
108 snprintf(buf
, 64, ".L%p", sym
->bb_target
);
112 snprintf(buf
, 64, "%s", show_ident(sym
->ident
));
115 expr
= sym
->initializer
;
116 snprintf(buf
, 64, "<anon symbol:%p>", sym
);
117 switch (expr
->type
) {
119 snprintf(buf
, 64, "<symbol value: %lld>", expr
->value
);
122 return show_string(expr
->string
);
129 i
= snprintf(buf
, 64, "%%r%d", pseudo
->nr
);
131 sprintf(buf
+i
, "(%s)", show_ident(pseudo
->ident
));
134 long long value
= pseudo
->value
;
135 if (value
> 1000 || value
< -1000)
136 snprintf(buf
, 64, "$%#llx", value
);
138 snprintf(buf
, 64, "$%lld", value
);
142 snprintf(buf
, 64, "%%arg%d", pseudo
->nr
);
145 i
= snprintf(buf
, 64, "%%phi%d", pseudo
->nr
);
147 sprintf(buf
+i
, "(%s)", show_ident(pseudo
->ident
));
150 snprintf(buf
, 64, "<bad pseudo type %d>", pseudo
->type
);
155 static const char* opcodes
[] = {
156 [OP_BADOP
] = "bad_op",
159 [OP_ENTRY
] = "<entry-point>",
164 [OP_SWITCH
] = "switch",
165 [OP_INVOKE
] = "invoke",
166 [OP_COMPUTEDGOTO
] = "jmp *",
167 [OP_UNWIND
] = "unwind",
186 [OP_AND_BOOL
] = "and-bool",
187 [OP_OR_BOOL
] = "or-bool",
189 /* Binary comparison */
190 [OP_SET_EQ
] = "seteq",
191 [OP_SET_NE
] = "setne",
192 [OP_SET_LE
] = "setle",
193 [OP_SET_GE
] = "setge",
194 [OP_SET_LT
] = "setlt",
195 [OP_SET_GT
] = "setgt",
198 [OP_SET_BE
] = "setbe",
199 [OP_SET_AE
] = "setae",
205 /* Special three-input */
209 [OP_MALLOC
] = "malloc",
211 [OP_ALLOCA
] = "alloca",
213 [OP_STORE
] = "store",
215 [OP_SYMADDR
] = "symaddr",
216 [OP_GET_ELEMENT_PTR
] = "getelem",
220 [OP_PHISOURCE
] = "phisrc",
222 [OP_SCAST
] = "scast",
223 [OP_FPCAST
] = "fpcast",
224 [OP_PTRCAST
] = "ptrcast",
226 [OP_VANEXT
] = "va_next",
227 [OP_VAARG
] = "va_arg",
228 [OP_SLICE
] = "slice",
232 [OP_DEATHNOTE
] = "dead",
235 /* Sparse tagging (line numbers, context, whatever) */
236 [OP_CONTEXT
] = "context",
237 [OP_RANGE
] = "range-check",
242 static char *show_asm_constraints(char *buf
, const char *sep
, struct asm_constraint_list
*list
)
244 struct asm_constraint
*entry
;
246 FOR_EACH_PTR(list
, entry
) {
247 buf
+= sprintf(buf
, "%s\"%s\"", sep
, entry
->constraint
);
249 buf
+= sprintf(buf
, " (%s)", show_pseudo(entry
->pseudo
));
251 buf
+= sprintf(buf
, " [%s]", show_ident(entry
->ident
));
253 } END_FOR_EACH_PTR(entry
);
257 static char *show_asm(char *buf
, struct instruction
*insn
)
259 struct asm_rules
*rules
= insn
->asm_rules
;
261 buf
+= sprintf(buf
, "\"%s\"", insn
->string
);
262 buf
= show_asm_constraints(buf
, "\n\t\tout: ", rules
->outputs
);
263 buf
= show_asm_constraints(buf
, "\n\t\tin: ", rules
->inputs
);
264 buf
= show_asm_constraints(buf
, "\n\t\tclobber: ", rules
->clobbers
);
268 const char *show_instruction(struct instruction
*insn
)
270 int opcode
= insn
->opcode
;
271 static char buffer
[1024];
276 buf
+= sprintf(buf
, "# ");
278 if (opcode
< sizeof(opcodes
)/sizeof(char *)) {
279 const char *op
= opcodes
[opcode
];
281 buf
+= sprintf(buf
, "opcode:%d", opcode
);
283 buf
+= sprintf(buf
, "%s", op
);
285 buf
+= sprintf(buf
, ".%d", insn
->size
);
286 memset(buf
, ' ', 20);
290 if (buf
< buffer
+ 12)
294 if (insn
->src
&& insn
->src
!= VOID
)
295 buf
+= sprintf(buf
, "%s", show_pseudo(insn
->src
));
298 if (insn
->bb_true
&& insn
->bb_false
) {
299 buf
+= sprintf(buf
, "%s, .L%p, .L%p", show_pseudo(insn
->cond
), insn
->bb_true
, insn
->bb_false
);
302 buf
+= sprintf(buf
, ".L%p", insn
->bb_true
? insn
->bb_true
: insn
->bb_false
);
306 struct symbol
*sym
= insn
->symbol
->sym
;
307 buf
+= sprintf(buf
, "%s <- ", show_pseudo(insn
->target
));
309 if (sym
->bb_target
) {
310 buf
+= sprintf(buf
, ".L%p", sym
->bb_target
);
314 buf
+= sprintf(buf
, "%s", show_ident(sym
->ident
));
317 buf
+= sprintf(buf
, "<anon symbol:%p>", sym
);
322 struct expression
*expr
= insn
->val
;
323 buf
+= sprintf(buf
, "%s <- ", show_pseudo(insn
->target
));
326 buf
+= sprintf(buf
, "%s", "<none>");
330 switch (expr
->type
) {
332 buf
+= sprintf(buf
, "%lld", expr
->value
);
335 buf
+= sprintf(buf
, "%Lf", expr
->fvalue
);
338 buf
+= sprintf(buf
, "%.40s", show_string(expr
->string
));
341 buf
+= sprintf(buf
, "%s", show_ident(expr
->symbol
->ident
));
344 buf
+= sprintf(buf
, ".L%p", expr
->symbol
->bb_target
);
347 buf
+= sprintf(buf
, "SETVAL EXPR TYPE %d", expr
->type
);
352 struct multijmp
*jmp
;
353 buf
+= sprintf(buf
, "%s", show_pseudo(insn
->target
));
354 FOR_EACH_PTR(insn
->multijmp_list
, jmp
) {
355 if (jmp
->begin
== jmp
->end
)
356 buf
+= sprintf(buf
, ", %d -> .L%p", jmp
->begin
, jmp
->target
);
357 else if (jmp
->begin
< jmp
->end
)
358 buf
+= sprintf(buf
, ", %d ... %d -> .L%p", jmp
->begin
, jmp
->end
, jmp
->target
);
360 buf
+= sprintf(buf
, ", default -> .L%p", jmp
->target
);
361 } END_FOR_EACH_PTR(jmp
);
364 case OP_COMPUTEDGOTO
: {
365 struct multijmp
*jmp
;
366 buf
+= sprintf(buf
, "%s", show_pseudo(insn
->target
));
367 FOR_EACH_PTR(insn
->multijmp_list
, jmp
) {
368 buf
+= sprintf(buf
, ", .L%p", jmp
->target
);
369 } END_FOR_EACH_PTR(jmp
);
374 struct instruction
*phi
;
375 buf
+= sprintf(buf
, "%s <- %s ", show_pseudo(insn
->target
), show_pseudo(insn
->phi_src
));
376 FOR_EACH_PTR(insn
->phi_users
, phi
) {
377 buf
+= sprintf(buf
, " (%s)", show_pseudo(phi
->target
));
378 } END_FOR_EACH_PTR(phi
);
384 const char *s
= " <-";
385 buf
+= sprintf(buf
, "%s", show_pseudo(insn
->target
));
386 FOR_EACH_PTR(insn
->phi_list
, phi
) {
387 buf
+= sprintf(buf
, "%s %s", s
, show_pseudo(phi
));
389 } END_FOR_EACH_PTR(phi
);
392 case OP_LOAD
: case OP_LNOP
:
393 buf
+= sprintf(buf
, "%s <- %d[%s]", show_pseudo(insn
->target
), insn
->offset
, show_pseudo(insn
->src
));
395 case OP_STORE
: case OP_SNOP
:
396 buf
+= sprintf(buf
, "%s -> %d[%s]", show_pseudo(insn
->target
), insn
->offset
, show_pseudo(insn
->src
));
400 if (insn
->target
&& insn
->target
!= VOID
)
401 buf
+= sprintf(buf
, "%s <- ", show_pseudo(insn
->target
));
402 buf
+= sprintf(buf
, "%s", show_pseudo(insn
->func
));
403 FOR_EACH_PTR(insn
->arguments
, arg
) {
404 buf
+= sprintf(buf
, ", %s", show_pseudo(arg
));
405 } END_FOR_EACH_PTR(arg
);
412 buf
+= sprintf(buf
, "%s <- (%d) %s",
413 show_pseudo(insn
->target
),
414 type_size(insn
->orig_type
),
415 show_pseudo(insn
->src
));
417 case OP_BINARY
... OP_BINARY_END
:
418 case OP_BINCMP
... OP_BINCMP_END
:
419 buf
+= sprintf(buf
, "%s <- %s, %s", show_pseudo(insn
->target
), show_pseudo(insn
->src1
), show_pseudo(insn
->src2
));
423 buf
+= sprintf(buf
, "%s <- %s, %s, %s", show_pseudo(insn
->target
),
424 show_pseudo(insn
->src1
), show_pseudo(insn
->src2
), show_pseudo(insn
->src3
));
428 buf
+= sprintf(buf
, "%s <- %s, %d, %d", show_pseudo(insn
->target
), show_pseudo(insn
->base
), insn
->from
, insn
->len
);
431 case OP_NOT
: case OP_NEG
:
432 buf
+= sprintf(buf
, "%s <- %s", show_pseudo(insn
->target
), show_pseudo(insn
->src1
));
436 buf
+= sprintf(buf
, "%s%d", insn
->check
? "check: " : "", insn
->increment
);
439 buf
+= sprintf(buf
, "%s between %s..%s", show_pseudo(insn
->src1
), show_pseudo(insn
->src2
), show_pseudo(insn
->src3
));
442 buf
+= sprintf(buf
, "%s <- %s", show_pseudo(insn
->target
), show_pseudo(insn
->src1
));
445 buf
+= sprintf(buf
, "%s", show_pseudo(insn
->target
));
448 buf
= show_asm(buf
, insn
);
451 buf
+= sprintf(buf
, "%s <- %s", show_pseudo(insn
->target
), show_pseudo(insn
->src
));
456 do { --buf
; } while (*buf
== ' ');
461 void show_bb(struct basic_block
*bb
)
463 struct instruction
*insn
;
465 printf(".L%p:\n", bb
);
467 pseudo_t needs
, defines
;
468 printf("%s:%d\n", stream_name(bb
->pos
.stream
), bb
->pos
.line
);
470 FOR_EACH_PTR(bb
->needs
, needs
) {
471 struct instruction
*def
= needs
->def
;
472 if (def
->opcode
!= OP_PHI
) {
473 printf(" **uses %s (from .L%p)**\n", show_pseudo(needs
), def
->bb
);
476 const char *sep
= " ";
477 printf(" **uses %s (from", show_pseudo(needs
));
478 FOR_EACH_PTR(def
->phi_list
, phi
) {
481 printf("%s(%s:.L%p)", sep
, show_pseudo(phi
), phi
->def
->bb
);
483 } END_FOR_EACH_PTR(phi
);
486 } END_FOR_EACH_PTR(needs
);
488 FOR_EACH_PTR(bb
->defines
, defines
) {
489 printf(" **defines %s **\n", show_pseudo(defines
));
490 } END_FOR_EACH_PTR(defines
);
493 struct basic_block
*from
;
494 FOR_EACH_PTR(bb
->parents
, from
) {
495 printf(" **from %p (%s:%d:%d)**\n", from
,
496 stream_name(from
->pos
.stream
), from
->pos
.line
, from
->pos
.pos
);
497 } END_FOR_EACH_PTR(from
);
501 struct basic_block
*to
;
502 FOR_EACH_PTR(bb
->children
, to
) {
503 printf(" **to %p (%s:%d:%d)**\n", to
,
504 stream_name(to
->pos
.stream
), to
->pos
.line
, to
->pos
.pos
);
505 } END_FOR_EACH_PTR(to
);
509 FOR_EACH_PTR(bb
->insns
, insn
) {
510 if (!insn
->bb
&& verbose
< 2)
512 printf("\t%s\n", show_instruction(insn
));
513 } END_FOR_EACH_PTR(insn
);
514 if (!bb_terminated(bb
))
518 static void show_symbol_usage(pseudo_t pseudo
)
522 FOR_EACH_PTR(pseudo
->users
, pp
) {
523 struct instruction
*insn
= container(pp
, struct instruction
, src
);
524 printf("\t%s\n", show_instruction(insn
));
525 } END_FOR_EACH_PTR(pp
);
529 void show_entry(struct entrypoint
*ep
)
532 struct basic_block
*bb
;
534 printf("%s:\n", show_ident(ep
->name
->ident
));
537 printf("ep %p: %s\n", ep
, show_ident(ep
->name
->ident
));
539 FOR_EACH_PTR(ep
->syms
, sym
) {
542 if (!sym
->pseudo
->users
)
544 printf(" sym: %p %s\n", sym
, show_ident(sym
->ident
));
545 if (sym
->ctype
.modifiers
& (MOD_EXTERN
| MOD_STATIC
| MOD_ADDRESSABLE
))
546 printf("\texternal visibility\n");
547 show_symbol_usage(sym
->pseudo
);
548 } END_FOR_EACH_PTR(sym
);
553 FOR_EACH_PTR(ep
->bbs
, bb
) {
556 if (!bb
->parents
&& !bb
->children
&& !bb
->insns
&& verbose
< 2)
560 } END_FOR_EACH_PTR(bb
);
565 static void bind_label(struct symbol
*label
, struct basic_block
*bb
, struct position pos
)
567 if (label
->bb_target
)
568 warning(pos
, "label '%s' already bound", show_ident(label
->ident
));
569 label
->bb_target
= bb
;
572 static struct basic_block
* get_bound_block(struct entrypoint
*ep
, struct symbol
*label
)
574 struct basic_block
*bb
= label
->bb_target
;
577 bb
= alloc_basic_block(ep
, label
->pos
);
578 label
->bb_target
= bb
;
583 static void finish_block(struct entrypoint
*ep
)
585 struct basic_block
*src
= ep
->active
;
586 if (bb_reachable(src
))
590 static void add_goto(struct entrypoint
*ep
, struct basic_block
*dst
)
592 struct basic_block
*src
= ep
->active
;
593 if (bb_reachable(src
)) {
594 struct instruction
*br
= alloc_instruction(OP_BR
, 0);
596 add_bb(&dst
->parents
, src
);
597 add_bb(&src
->children
, dst
);
599 add_instruction(&src
->insns
, br
);
604 static void add_one_insn(struct entrypoint
*ep
, struct instruction
*insn
)
606 struct basic_block
*bb
= ep
->active
;
608 if (bb_reachable(bb
)) {
610 add_instruction(&bb
->insns
, insn
);
614 static void set_activeblock(struct entrypoint
*ep
, struct basic_block
*bb
)
616 if (!bb_terminated(ep
->active
))
620 if (bb_reachable(bb
))
621 add_bb(&ep
->bbs
, bb
);
624 static void remove_parent(struct basic_block
*child
, struct basic_block
*parent
)
626 remove_bb_from_list(&child
->parents
, parent
, 1);
631 /* Change a "switch" into a branch */
632 void insert_branch(struct basic_block
*bb
, struct instruction
*jmp
, struct basic_block
*target
)
634 struct instruction
*br
, *old
;
635 struct basic_block
*child
;
637 /* Remove the switch */
638 old
= delete_last_instruction(&bb
->insns
);
641 br
= alloc_instruction(OP_BR
, 0);
643 br
->bb_true
= target
;
644 add_instruction(&bb
->insns
, br
);
646 FOR_EACH_PTR(bb
->children
, child
) {
647 if (child
== target
) {
648 target
= NULL
; /* Trigger just once */
651 DELETE_CURRENT_PTR(child
);
652 remove_parent(child
, bb
);
653 } END_FOR_EACH_PTR(child
);
654 PACK_PTR_LIST(&bb
->children
);
658 void insert_select(struct basic_block
*bb
, struct instruction
*br
, struct instruction
*phi_node
, pseudo_t
true, pseudo_t
false)
661 struct instruction
*select
;
663 /* Remove the 'br' */
664 delete_last_instruction(&bb
->insns
);
666 select
= alloc_instruction(OP_SEL
, phi_node
->size
);
670 use_pseudo(br
->cond
, &select
->src1
);
672 target
= phi_node
->target
;
673 assert(target
->def
== phi_node
);
674 select
->target
= target
;
675 target
->def
= select
;
677 use_pseudo(true, &select
->src2
);
678 use_pseudo(false, &select
->src3
);
680 add_instruction(&bb
->insns
, select
);
681 add_instruction(&bb
->insns
, br
);
684 static inline int bb_empty(struct basic_block
*bb
)
689 /* Add a label to the currently active block, return new active block */
690 static struct basic_block
* add_label(struct entrypoint
*ep
, struct symbol
*label
)
692 struct basic_block
*bb
= label
->bb_target
;
695 set_activeblock(ep
, bb
);
699 if (!bb_reachable(bb
) || !bb_empty(bb
)) {
700 bb
= alloc_basic_block(ep
, label
->pos
);
701 set_activeblock(ep
, bb
);
703 label
->bb_target
= bb
;
707 static void add_branch(struct entrypoint
*ep
, struct expression
*expr
, pseudo_t cond
, struct basic_block
*bb_true
, struct basic_block
*bb_false
)
709 struct basic_block
*bb
= ep
->active
;
710 struct instruction
*br
;
712 if (bb_reachable(bb
)) {
713 br
= alloc_instruction(OP_BR
, 0);
714 use_pseudo(cond
, &br
->cond
);
715 br
->bb_true
= bb_true
;
716 br
->bb_false
= bb_false
;
717 add_bb(&bb_true
->parents
, bb
);
718 add_bb(&bb_false
->parents
, bb
);
719 add_bb(&bb
->children
, bb_true
);
720 add_bb(&bb
->children
, bb_false
);
721 add_one_insn(ep
, br
);
725 /* Dummy pseudo allocator */
726 pseudo_t
alloc_pseudo(struct instruction
*def
)
729 struct pseudo
* pseudo
= __alloc_pseudo(0);
730 pseudo
->type
= PSEUDO_REG
;
736 static void clear_symbol_pseudos(struct entrypoint
*ep
)
740 FOR_EACH_PTR(ep
->accesses
, sym
) {
742 } END_FOR_EACH_PTR(sym
);
745 static pseudo_t
symbol_pseudo(struct entrypoint
*ep
, struct symbol
*sym
)
752 pseudo
= sym
->pseudo
;
754 pseudo
= __alloc_pseudo(0);
756 pseudo
->type
= PSEUDO_SYM
;
758 pseudo
->ident
= sym
->ident
;
759 sym
->pseudo
= pseudo
;
760 add_symbol(&ep
->accesses
, sym
);
762 /* Symbol pseudos have neither nr, usage nor def */
766 pseudo_t
value_pseudo(long long val
)
768 #define MAX_VAL_HASH 64
769 static struct pseudo_list
*prev
[MAX_VAL_HASH
];
770 int hash
= val
& (MAX_VAL_HASH
-1);
771 struct pseudo_list
**list
= prev
+ hash
;
774 FOR_EACH_PTR(*list
, pseudo
) {
775 if (pseudo
->value
== val
)
777 } END_FOR_EACH_PTR(pseudo
);
779 pseudo
= __alloc_pseudo(0);
780 pseudo
->type
= PSEUDO_VAL
;
782 add_pseudo(list
, pseudo
);
784 /* Value pseudos have neither nr, usage nor def */
788 static pseudo_t
argument_pseudo(struct entrypoint
*ep
, int nr
)
790 pseudo_t pseudo
= __alloc_pseudo(0);
791 struct instruction
*entry
= ep
->entry
;
793 pseudo
->type
= PSEUDO_ARG
;
796 add_pseudo(&entry
->arg_list
, pseudo
);
798 /* Argument pseudos have neither usage nor def */
802 pseudo_t
alloc_phi(struct basic_block
*source
, pseudo_t pseudo
, int size
)
804 struct instruction
*insn
= alloc_instruction(OP_PHISOURCE
, size
);
805 pseudo_t phi
= __alloc_pseudo(0);
808 phi
->type
= PSEUDO_PHI
;
812 use_pseudo(pseudo
, &insn
->phi_src
);
815 add_instruction(&source
->insns
, insn
);
820 * We carry the "access_data" structure around for any accesses,
821 * which simplifies things a lot. It contains all the access
822 * information in one place.
825 struct symbol
*result_type
; // result ctype
826 struct symbol
*source_type
; // source ctype
827 pseudo_t address
; // pseudo containing address ..
828 pseudo_t origval
; // pseudo for original value ..
829 unsigned int offset
, alignment
; // byte offset
830 unsigned int bit_size
, bit_offset
; // which bits
834 static void finish_address_gen(struct entrypoint
*ep
, struct access_data
*ad
)
838 static int linearize_simple_address(struct entrypoint
*ep
,
839 struct expression
*addr
,
840 struct access_data
*ad
)
842 if (addr
->type
== EXPR_SYMBOL
) {
843 linearize_one_symbol(ep
, addr
->symbol
);
844 ad
->address
= symbol_pseudo(ep
, addr
->symbol
);
847 if (addr
->type
== EXPR_BINOP
) {
848 if (addr
->right
->type
== EXPR_VALUE
) {
849 if (addr
->op
== '+') {
850 ad
->offset
+= get_expression_value(addr
->right
);
851 return linearize_simple_address(ep
, addr
->left
, ad
);
855 ad
->address
= linearize_expression(ep
, addr
);
859 static struct symbol
*base_type(struct symbol
*sym
)
861 struct symbol
*base
= sym
;
864 if (sym
->type
== SYM_NODE
)
865 base
= base
->ctype
.base_type
;
866 if (base
->type
== SYM_BITFIELD
)
867 return base
->ctype
.base_type
;
872 static int linearize_address_gen(struct entrypoint
*ep
,
873 struct expression
*expr
,
874 struct access_data
*ad
)
876 struct symbol
*ctype
= expr
->ctype
;
881 ad
->result_type
= ctype
;
882 ad
->source_type
= base_type(ctype
);
883 ad
->bit_size
= ctype
->bit_size
;
884 ad
->alignment
= ctype
->ctype
.alignment
;
885 ad
->bit_offset
= ctype
->bit_offset
;
886 if (expr
->type
== EXPR_PREOP
&& expr
->op
== '*')
887 return linearize_simple_address(ep
, expr
->unop
, ad
);
889 warning(expr
->pos
, "generating address of non-lvalue (%d)", expr
->type
);
893 static pseudo_t
add_load(struct entrypoint
*ep
, struct access_data
*ad
)
895 struct instruction
*insn
;
902 insn
= alloc_typed_instruction(OP_LOAD
, ad
->source_type
);
903 new = alloc_pseudo(insn
);
907 insn
->offset
= ad
->offset
;
908 use_pseudo(ad
->address
, &insn
->src
);
909 add_one_insn(ep
, insn
);
913 static void add_store(struct entrypoint
*ep
, struct access_data
*ad
, pseudo_t value
)
915 struct basic_block
*bb
= ep
->active
;
917 if (bb_reachable(bb
)) {
918 struct instruction
*store
= alloc_typed_instruction(OP_STORE
, ad
->source_type
);
919 store
->offset
= ad
->offset
;
920 use_pseudo(value
, &store
->target
);
921 use_pseudo(ad
->address
, &store
->src
);
922 add_one_insn(ep
, store
);
926 static pseudo_t
linearize_store_gen(struct entrypoint
*ep
,
928 struct access_data
*ad
)
930 pseudo_t store
= value
;
932 if (type_size(ad
->source_type
) != type_size(ad
->result_type
)) {
933 pseudo_t orig
= add_load(ep
, ad
);
934 int shift
= ad
->bit_offset
;
935 unsigned long long mask
= (1ULL << ad
->bit_size
)-1;
938 store
= add_binary_op(ep
, ad
->source_type
, OP_SHL
, value
, value_pseudo(shift
));
941 orig
= add_binary_op(ep
, ad
->source_type
, OP_AND
, orig
, value_pseudo(~mask
));
942 store
= add_binary_op(ep
, ad
->source_type
, OP_OR
, orig
, store
);
944 add_store(ep
, ad
, store
);
948 static pseudo_t
add_binary_op(struct entrypoint
*ep
, struct symbol
*ctype
, int op
, pseudo_t left
, pseudo_t right
)
950 struct instruction
*insn
= alloc_typed_instruction(op
, ctype
);
951 pseudo_t target
= alloc_pseudo(insn
);
952 insn
->target
= target
;
953 use_pseudo(left
, &insn
->src1
);
954 use_pseudo(right
, &insn
->src2
);
955 add_one_insn(ep
, insn
);
959 static pseudo_t
add_setval(struct entrypoint
*ep
, struct symbol
*ctype
, struct expression
*val
)
961 struct instruction
*insn
= alloc_typed_instruction(OP_SETVAL
, ctype
);
962 pseudo_t target
= alloc_pseudo(insn
);
963 insn
->target
= target
;
965 add_one_insn(ep
, insn
);
969 static pseudo_t
add_symbol_address(struct entrypoint
*ep
, struct symbol
*sym
)
971 struct instruction
*insn
= alloc_instruction(OP_SYMADDR
, bits_in_pointer
);
972 pseudo_t target
= alloc_pseudo(insn
);
974 insn
->target
= target
;
975 use_pseudo(symbol_pseudo(ep
, sym
), &insn
->symbol
);
976 add_one_insn(ep
, insn
);
980 static pseudo_t
linearize_load_gen(struct entrypoint
*ep
, struct access_data
*ad
)
982 pseudo_t
new = add_load(ep
, ad
);
984 if (ad
->bit_offset
) {
985 pseudo_t shift
= value_pseudo(ad
->bit_offset
);
986 pseudo_t newval
= add_binary_op(ep
, ad
->source_type
, OP_LSR
, new, shift
);
993 static pseudo_t
linearize_access(struct entrypoint
*ep
, struct expression
*expr
)
995 struct access_data ad
= { NULL
, };
998 if (!linearize_address_gen(ep
, expr
, &ad
))
1000 value
= linearize_load_gen(ep
, &ad
);
1001 finish_address_gen(ep
, &ad
);
1006 static pseudo_t
linearize_inc_dec(struct entrypoint
*ep
, struct expression
*expr
, int postop
)
1008 struct access_data ad
= { NULL
, };
1009 pseudo_t old
, new, one
;
1010 int op
= expr
->op
== SPECIAL_INCREMENT
? OP_ADD
: OP_SUB
;
1012 if (!linearize_address_gen(ep
, expr
->unop
, &ad
))
1015 old
= linearize_load_gen(ep
, &ad
);
1016 one
= value_pseudo(expr
->op_value
);
1017 new = add_binary_op(ep
, expr
->ctype
, op
, old
, one
);
1018 linearize_store_gen(ep
, new, &ad
);
1019 finish_address_gen(ep
, &ad
);
1020 return postop
? old
: new;
1023 static pseudo_t
add_uniop(struct entrypoint
*ep
, struct expression
*expr
, int op
, pseudo_t src
)
1025 struct instruction
*insn
= alloc_typed_instruction(op
, expr
->ctype
);
1026 pseudo_t
new = alloc_pseudo(insn
);
1029 use_pseudo(src
, &insn
->src1
);
1030 add_one_insn(ep
, insn
);
1034 static pseudo_t
linearize_slice(struct entrypoint
*ep
, struct expression
*expr
)
1036 pseudo_t pre
= linearize_expression(ep
, expr
->base
);
1037 struct instruction
*insn
= alloc_typed_instruction(OP_SLICE
, expr
->ctype
);
1038 pseudo_t
new = alloc_pseudo(insn
);
1041 insn
->from
= expr
->r_bitpos
;
1042 insn
->len
= expr
->r_nrbits
;
1043 use_pseudo(pre
, &insn
->base
);
1044 add_one_insn(ep
, insn
);
1048 static pseudo_t
linearize_regular_preop(struct entrypoint
*ep
, struct expression
*expr
)
1050 pseudo_t pre
= linearize_expression(ep
, expr
->unop
);
1055 pseudo_t zero
= value_pseudo(0);
1056 return add_binary_op(ep
, expr
->unop
->ctype
, OP_SET_EQ
, pre
, zero
);
1059 return add_uniop(ep
, expr
, OP_NOT
, pre
);
1061 return add_uniop(ep
, expr
, OP_NEG
, pre
);
1066 static pseudo_t
linearize_preop(struct entrypoint
*ep
, struct expression
*expr
)
1069 * '*' is an lvalue access, and is fundamentally different
1070 * from an arithmetic operation. Maybe it should have an
1071 * expression type of its own..
1073 if (expr
->op
== '*')
1074 return linearize_access(ep
, expr
);
1075 if (expr
->op
== SPECIAL_INCREMENT
|| expr
->op
== SPECIAL_DECREMENT
)
1076 return linearize_inc_dec(ep
, expr
, 0);
1077 return linearize_regular_preop(ep
, expr
);
1080 static pseudo_t
linearize_postop(struct entrypoint
*ep
, struct expression
*expr
)
1082 return linearize_inc_dec(ep
, expr
, 1);
1086 * Casts to pointers are "less safe" than other casts, since
1087 * they imply type-unsafe accesses. "void *" is a special
1088 * case, since you can't access through it anyway without another
1091 static struct instruction
*alloc_cast_instruction(struct symbol
*ctype
)
1093 int opcode
= OP_CAST
;
1094 struct symbol
*base
= ctype
;
1096 if (base
->ctype
.modifiers
& MOD_SIGNED
)
1098 if (base
->type
== SYM_NODE
)
1099 base
= base
->ctype
.base_type
;
1100 if (base
->type
== SYM_PTR
) {
1101 base
= base
->ctype
.base_type
;
1102 if (base
!= &void_ctype
)
1103 opcode
= OP_PTRCAST
;
1105 if (base
->ctype
.base_type
== &fp_type
)
1107 return alloc_typed_instruction(opcode
, ctype
);
1110 static pseudo_t
cast_pseudo(struct entrypoint
*ep
, pseudo_t src
, struct symbol
*from
, struct symbol
*to
)
1113 struct instruction
*insn
;
1119 if (from
->bit_size
< 0 || to
->bit_size
< 0)
1121 insn
= alloc_cast_instruction(to
);
1122 result
= alloc_pseudo(insn
);
1123 insn
->target
= result
;
1124 insn
->orig_type
= from
;
1125 use_pseudo(src
, &insn
->src
);
1126 add_one_insn(ep
, insn
);
1130 static int opcode_sign(int opcode
, struct symbol
*ctype
)
1132 if (ctype
&& (ctype
->ctype
.modifiers
& MOD_SIGNED
)) {
1134 case OP_MULU
: case OP_DIVU
: case OP_MODU
: case OP_LSR
:
1141 static pseudo_t
linearize_assignment(struct entrypoint
*ep
, struct expression
*expr
)
1143 struct access_data ad
= { NULL
, };
1144 struct expression
*target
= expr
->left
;
1145 struct expression
*src
= expr
->right
;
1148 value
= linearize_expression(ep
, src
);
1149 if (!target
|| !linearize_address_gen(ep
, target
, &ad
))
1151 if (expr
->op
!= '=') {
1152 pseudo_t oldvalue
= linearize_load_gen(ep
, &ad
);
1154 static const int op_trans
[] = {
1155 [SPECIAL_ADD_ASSIGN
- SPECIAL_BASE
] = OP_ADD
,
1156 [SPECIAL_SUB_ASSIGN
- SPECIAL_BASE
] = OP_SUB
,
1157 [SPECIAL_MUL_ASSIGN
- SPECIAL_BASE
] = OP_MULU
,
1158 [SPECIAL_DIV_ASSIGN
- SPECIAL_BASE
] = OP_DIVU
,
1159 [SPECIAL_MOD_ASSIGN
- SPECIAL_BASE
] = OP_MODU
,
1160 [SPECIAL_SHL_ASSIGN
- SPECIAL_BASE
] = OP_SHL
,
1161 [SPECIAL_SHR_ASSIGN
- SPECIAL_BASE
] = OP_LSR
,
1162 [SPECIAL_AND_ASSIGN
- SPECIAL_BASE
] = OP_AND
,
1163 [SPECIAL_OR_ASSIGN
- SPECIAL_BASE
] = OP_OR
,
1164 [SPECIAL_XOR_ASSIGN
- SPECIAL_BASE
] = OP_XOR
1171 oldvalue
= cast_pseudo(ep
, oldvalue
, src
->ctype
, expr
->ctype
);
1172 opcode
= opcode_sign(op_trans
[expr
->op
- SPECIAL_BASE
], src
->ctype
);
1173 dst
= add_binary_op(ep
, src
->ctype
, opcode
, oldvalue
, value
);
1174 value
= cast_pseudo(ep
, dst
, expr
->ctype
, src
->ctype
);
1176 value
= linearize_store_gen(ep
, value
, &ad
);
1177 finish_address_gen(ep
, &ad
);
1181 static pseudo_t
linearize_call_expression(struct entrypoint
*ep
, struct expression
*expr
)
1183 struct expression
*arg
, *fn
;
1184 struct instruction
*insn
= alloc_typed_instruction(OP_CALL
, expr
->ctype
);
1185 pseudo_t retval
, call
;
1186 struct ctype
*ctype
= NULL
;
1187 struct context
*context
;
1190 warning(expr
->pos
, "call with no type!");
1194 FOR_EACH_PTR(expr
->args
, arg
) {
1195 pseudo_t
new = linearize_expression(ep
, arg
);
1196 use_pseudo(new, add_pseudo(&insn
->arguments
, new));
1197 } END_FOR_EACH_PTR(arg
);
1202 ctype
= &fn
->ctype
->ctype
;
1204 if (fn
->type
== EXPR_PREOP
) {
1205 if (fn
->unop
->type
== EXPR_SYMBOL
) {
1206 struct symbol
*sym
= fn
->unop
->symbol
;
1207 if (sym
->ctype
.base_type
->type
== SYM_FN
)
1211 if (fn
->type
== EXPR_SYMBOL
) {
1212 call
= symbol_pseudo(ep
, fn
->symbol
);
1214 call
= linearize_expression(ep
, fn
);
1216 use_pseudo(call
, &insn
->func
);
1218 if (expr
->ctype
!= &void_ctype
)
1219 retval
= alloc_pseudo(insn
);
1220 insn
->target
= retval
;
1221 add_one_insn(ep
, insn
);
1224 FOR_EACH_PTR(ctype
->contexts
, context
) {
1225 int in
= context
->in
;
1226 int out
= context
->out
;
1237 context_diff
= out
- in
;
1238 if (check
|| context_diff
) {
1239 insn
= alloc_instruction(OP_CONTEXT
, 0);
1240 insn
->increment
= context_diff
;
1241 insn
->check
= check
;
1242 insn
->context_expr
= context
->context
;
1243 add_one_insn(ep
, insn
);
1245 } END_FOR_EACH_PTR(context
);
1251 static pseudo_t
linearize_binop(struct entrypoint
*ep
, struct expression
*expr
)
1253 pseudo_t src1
, src2
, dst
;
1254 static const int opcode
[] = {
1255 ['+'] = OP_ADD
, ['-'] = OP_SUB
,
1256 ['*'] = OP_MULU
, ['/'] = OP_DIVU
,
1257 ['%'] = OP_MODU
, ['&'] = OP_AND
,
1258 ['|'] = OP_OR
, ['^'] = OP_XOR
,
1259 [SPECIAL_LEFTSHIFT
] = OP_SHL
,
1260 [SPECIAL_RIGHTSHIFT
] = OP_LSR
,
1261 [SPECIAL_LOGICAL_AND
] = OP_AND_BOOL
,
1262 [SPECIAL_LOGICAL_OR
] = OP_OR_BOOL
,
1266 src1
= linearize_expression(ep
, expr
->left
);
1267 src2
= linearize_expression(ep
, expr
->right
);
1268 op
= opcode_sign(opcode
[expr
->op
], expr
->ctype
);
1269 dst
= add_binary_op(ep
, expr
->ctype
, op
, src1
, src2
);
1273 static pseudo_t
linearize_logical_branch(struct entrypoint
*ep
, struct expression
*expr
, struct basic_block
*bb_true
, struct basic_block
*bb_false
);
1275 pseudo_t
linearize_cond_branch(struct entrypoint
*ep
, struct expression
*expr
, struct basic_block
*bb_true
, struct basic_block
*bb_false
);
1277 static pseudo_t
linearize_select(struct entrypoint
*ep
, struct expression
*expr
)
1279 pseudo_t cond
, true, false, res
;
1280 struct instruction
*insn
;
1282 true = linearize_expression(ep
, expr
->cond_true
);
1283 false = linearize_expression(ep
, expr
->cond_false
);
1284 cond
= linearize_expression(ep
, expr
->conditional
);
1286 insn
= alloc_typed_instruction(OP_SEL
, expr
->ctype
);
1287 if (!expr
->cond_true
)
1289 use_pseudo(cond
, &insn
->src1
);
1290 use_pseudo(true, &insn
->src2
);
1291 use_pseudo(false, &insn
->src3
);
1293 res
= alloc_pseudo(insn
);
1295 add_one_insn(ep
, insn
);
1299 static pseudo_t
add_join_conditional(struct entrypoint
*ep
, struct expression
*expr
,
1300 pseudo_t phi1
, pseudo_t phi2
)
1303 struct instruction
*phi_node
;
1310 phi_node
= alloc_typed_instruction(OP_PHI
, expr
->ctype
);
1311 use_pseudo(phi1
, add_pseudo(&phi_node
->phi_list
, phi1
));
1312 use_pseudo(phi2
, add_pseudo(&phi_node
->phi_list
, phi2
));
1313 phi_node
->target
= target
= alloc_pseudo(phi_node
);
1314 add_one_insn(ep
, phi_node
);
1318 static pseudo_t
linearize_short_conditional(struct entrypoint
*ep
, struct expression
*expr
,
1319 struct expression
*cond
,
1320 struct expression
*expr_false
)
1322 pseudo_t src1
, src2
;
1323 struct basic_block
*bb_false
;
1324 struct basic_block
*merge
= alloc_basic_block(ep
, expr
->pos
);
1325 pseudo_t phi1
, phi2
;
1326 int size
= type_size(expr
->ctype
);
1328 if (!expr_false
|| !ep
->active
)
1331 bb_false
= alloc_basic_block(ep
, expr_false
->pos
);
1332 src1
= linearize_expression(ep
, cond
);
1333 phi1
= alloc_phi(ep
->active
, src1
, size
);
1334 add_branch(ep
, expr
, src1
, merge
, bb_false
);
1336 set_activeblock(ep
, bb_false
);
1337 src2
= linearize_expression(ep
, expr_false
);
1338 phi2
= alloc_phi(ep
->active
, src2
, size
);
1339 set_activeblock(ep
, merge
);
1341 return add_join_conditional(ep
, expr
, phi1
, phi2
);
1344 static pseudo_t
linearize_conditional(struct entrypoint
*ep
, struct expression
*expr
,
1345 struct expression
*cond
,
1346 struct expression
*expr_true
,
1347 struct expression
*expr_false
)
1349 pseudo_t src1
, src2
;
1350 pseudo_t phi1
, phi2
;
1351 struct basic_block
*bb_true
, *bb_false
, *merge
;
1352 int size
= type_size(expr
->ctype
);
1354 if (!cond
|| !expr_true
|| !expr_false
|| !ep
->active
)
1356 bb_true
= alloc_basic_block(ep
, expr_true
->pos
);
1357 bb_false
= alloc_basic_block(ep
, expr_false
->pos
);
1358 merge
= alloc_basic_block(ep
, expr
->pos
);
1360 linearize_cond_branch(ep
, cond
, bb_true
, bb_false
);
1362 set_activeblock(ep
, bb_true
);
1363 src1
= linearize_expression(ep
, expr_true
);
1364 phi1
= alloc_phi(ep
->active
, src1
, size
);
1365 add_goto(ep
, merge
);
1367 set_activeblock(ep
, bb_false
);
1368 src2
= linearize_expression(ep
, expr_false
);
1369 phi2
= alloc_phi(ep
->active
, src2
, size
);
1370 set_activeblock(ep
, merge
);
1372 return add_join_conditional(ep
, expr
, phi1
, phi2
);
1375 static pseudo_t
linearize_logical(struct entrypoint
*ep
, struct expression
*expr
)
1377 struct expression
*shortcut
;
1379 shortcut
= alloc_const_expression(expr
->pos
, expr
->op
== SPECIAL_LOGICAL_OR
);
1380 shortcut
->ctype
= expr
->ctype
;
1381 return linearize_conditional(ep
, expr
, expr
->left
, shortcut
, expr
->right
);
1384 static pseudo_t
linearize_compare(struct entrypoint
*ep
, struct expression
*expr
)
1386 static const int cmpop
[] = {
1387 ['>'] = OP_SET_GT
, ['<'] = OP_SET_LT
,
1388 [SPECIAL_EQUAL
] = OP_SET_EQ
,
1389 [SPECIAL_NOTEQUAL
] = OP_SET_NE
,
1390 [SPECIAL_GTE
] = OP_SET_GE
,
1391 [SPECIAL_LTE
] = OP_SET_LE
,
1392 [SPECIAL_UNSIGNED_LT
] = OP_SET_B
,
1393 [SPECIAL_UNSIGNED_GT
] = OP_SET_A
,
1394 [SPECIAL_UNSIGNED_LTE
] = OP_SET_BE
,
1395 [SPECIAL_UNSIGNED_GTE
] = OP_SET_AE
,
1398 pseudo_t src1
= linearize_expression(ep
, expr
->left
);
1399 pseudo_t src2
= linearize_expression(ep
, expr
->right
);
1400 pseudo_t dst
= add_binary_op(ep
, expr
->left
->ctype
, cmpop
[expr
->op
], src1
, src2
);
1405 pseudo_t
linearize_cond_branch(struct entrypoint
*ep
, struct expression
*expr
, struct basic_block
*bb_true
, struct basic_block
*bb_false
)
1409 if (!expr
|| !bb_reachable(ep
->active
))
1412 switch (expr
->type
) {
1416 add_goto(ep
, expr
->value
? bb_true
: bb_false
);
1420 add_goto(ep
, expr
->fvalue
? bb_true
: bb_false
);
1424 linearize_logical_branch(ep
, expr
, bb_true
, bb_false
);
1428 cond
= linearize_compare(ep
, expr
);
1429 add_branch(ep
, expr
, cond
, bb_true
, bb_false
);
1433 if (expr
->op
== '!')
1434 return linearize_cond_branch(ep
, expr
->unop
, bb_false
, bb_true
);
1437 cond
= linearize_expression(ep
, expr
);
1438 add_branch(ep
, expr
, cond
, bb_true
, bb_false
);
1448 static pseudo_t
linearize_logical_branch(struct entrypoint
*ep
, struct expression
*expr
, struct basic_block
*bb_true
, struct basic_block
*bb_false
)
1450 struct basic_block
*next
= alloc_basic_block(ep
, expr
->pos
);
1452 if (expr
->op
== SPECIAL_LOGICAL_OR
)
1453 linearize_cond_branch(ep
, expr
->left
, bb_true
, next
);
1455 linearize_cond_branch(ep
, expr
->left
, next
, bb_false
);
1456 set_activeblock(ep
, next
);
1457 linearize_cond_branch(ep
, expr
->right
, bb_true
, bb_false
);
1461 static pseudo_t
linearize_cast(struct entrypoint
*ep
, struct expression
*expr
)
1464 struct expression
*orig
= expr
->cast_expression
;
1469 src
= linearize_expression(ep
, orig
);
1470 return cast_pseudo(ep
, src
, orig
->ctype
, expr
->ctype
);
1473 static pseudo_t
linearize_position(struct entrypoint
*ep
, struct expression
*pos
, struct access_data
*ad
)
1475 struct expression
*init_expr
= pos
->init_expr
;
1477 ad
->offset
= pos
->init_offset
;
1478 ad
->source_type
= base_type(init_expr
->ctype
);
1479 ad
->result_type
= init_expr
->ctype
;
1480 return linearize_initializer(ep
, init_expr
, ad
);
1483 pseudo_t
linearize_initializer(struct entrypoint
*ep
, struct expression
*initializer
, struct access_data
*ad
)
1485 switch (initializer
->type
) {
1486 case EXPR_INITIALIZER
: {
1487 struct expression
*expr
;
1488 FOR_EACH_PTR(initializer
->expr_list
, expr
) {
1489 linearize_initializer(ep
, expr
, ad
);
1490 } END_FOR_EACH_PTR(expr
);
1494 linearize_position(ep
, initializer
, ad
);
1497 pseudo_t value
= linearize_expression(ep
, initializer
);
1498 ad
->source_type
= base_type(initializer
->ctype
);
1499 ad
->result_type
= initializer
->ctype
;
1500 linearize_store_gen(ep
, value
, ad
);
1507 static void linearize_argument(struct entrypoint
*ep
, struct symbol
*arg
, int nr
)
1509 struct access_data ad
= { NULL
, };
1511 ad
.source_type
= arg
;
1512 ad
.result_type
= arg
;
1513 ad
.address
= symbol_pseudo(ep
, arg
);
1514 linearize_store_gen(ep
, argument_pseudo(ep
, nr
), &ad
);
1515 finish_address_gen(ep
, &ad
);
1518 pseudo_t
linearize_expression(struct entrypoint
*ep
, struct expression
*expr
)
1523 current_pos
= expr
->pos
;
1524 switch (expr
->type
) {
1526 linearize_one_symbol(ep
, expr
->symbol
);
1527 return add_symbol_address(ep
, expr
->symbol
);
1530 return value_pseudo(expr
->value
);
1532 case EXPR_STRING
: case EXPR_FVALUE
: case EXPR_LABEL
:
1533 return add_setval(ep
, expr
->ctype
, expr
);
1535 case EXPR_STATEMENT
:
1536 return linearize_statement(ep
, expr
->statement
);
1539 return linearize_call_expression(ep
, expr
);
1542 return linearize_binop(ep
, expr
);
1545 return linearize_logical(ep
, expr
);
1548 return linearize_compare(ep
, expr
);
1551 return linearize_select(ep
, expr
);
1553 case EXPR_CONDITIONAL
:
1554 if (!expr
->cond_true
)
1555 return linearize_short_conditional(ep
, expr
, expr
->conditional
, expr
->cond_false
);
1557 return linearize_conditional(ep
, expr
, expr
->conditional
,
1558 expr
->cond_true
, expr
->cond_false
);
1561 linearize_expression(ep
, expr
->left
);
1562 return linearize_expression(ep
, expr
->right
);
1564 case EXPR_ASSIGNMENT
:
1565 return linearize_assignment(ep
, expr
);
1568 return linearize_preop(ep
, expr
);
1571 return linearize_postop(ep
, expr
);
1574 case EXPR_IMPLIED_CAST
:
1575 return linearize_cast(ep
, expr
);
1578 return linearize_slice(ep
, expr
);
1580 case EXPR_INITIALIZER
:
1582 warning(expr
->pos
, "unexpected initializer expression (%d %d)", expr
->type
, expr
->op
);
1585 warning(expr
->pos
, "unknown expression (%d %d)", expr
->type
, expr
->op
);
1591 static void linearize_one_symbol(struct entrypoint
*ep
, struct symbol
*sym
)
1593 struct access_data ad
= { NULL
, };
1595 if (!sym
|| !sym
->initializer
|| sym
->initialized
)
1598 /* We need to output these puppies some day too.. */
1599 if (sym
->ctype
.modifiers
& (MOD_STATIC
| MOD_TOPLEVEL
))
1602 sym
->initialized
= 1;
1603 ad
.address
= symbol_pseudo(ep
, sym
);
1604 linearize_initializer(ep
, sym
->initializer
, &ad
);
1605 finish_address_gen(ep
, &ad
);
1608 static pseudo_t
linearize_compound_statement(struct entrypoint
*ep
, struct statement
*stmt
)
1611 struct statement
*s
;
1612 struct symbol
*ret
= stmt
->ret
;
1615 FOR_EACH_PTR(stmt
->stmts
, s
) {
1616 pseudo
= linearize_statement(ep
, s
);
1617 } END_FOR_EACH_PTR(s
);
1620 struct basic_block
*bb
= add_label(ep
, ret
);
1621 struct instruction
*phi_node
= first_instruction(bb
->insns
);
1626 if (pseudo_list_size(phi_node
->phi_list
)==1) {
1627 pseudo
= first_pseudo(phi_node
->phi_list
);
1628 assert(pseudo
->type
== PSEUDO_PHI
);
1629 return pseudo
->def
->src1
;
1631 return phi_node
->target
;
1636 static pseudo_t
linearize_context(struct entrypoint
*ep
, struct statement
*stmt
)
1638 struct instruction
*insn
= alloc_instruction(OP_CONTEXT
, 0);
1639 struct expression
*expr
= stmt
->expression
;
1642 if (expr
->type
== EXPR_VALUE
)
1643 value
= expr
->value
;
1645 insn
->increment
= value
;
1646 insn
->context_expr
= stmt
->context
;
1647 add_one_insn(ep
, insn
);
1651 static pseudo_t
linearize_range(struct entrypoint
*ep
, struct statement
*stmt
)
1653 struct instruction
*insn
= alloc_instruction(OP_RANGE
, 0);
1655 use_pseudo(linearize_expression(ep
, stmt
->range_expression
), &insn
->src1
);
1656 use_pseudo(linearize_expression(ep
, stmt
->range_low
), &insn
->src2
);
1657 use_pseudo(linearize_expression(ep
, stmt
->range_high
), &insn
->src3
);
1658 add_one_insn(ep
, insn
);
1662 ALLOCATOR(asm_rules
, "asm rules");
1663 ALLOCATOR(asm_constraint
, "asm constraints");
1665 static void add_asm_input(struct entrypoint
*ep
, struct instruction
*insn
, struct expression
*expr
,
1666 const char *constraint
, const struct ident
*ident
)
1668 pseudo_t pseudo
= linearize_expression(ep
, expr
);
1669 struct asm_constraint
*rule
= __alloc_asm_constraint(0);
1671 rule
->ident
= ident
;
1672 rule
->constraint
= constraint
;
1673 use_pseudo(pseudo
, &rule
->pseudo
);
1674 add_ptr_list(&insn
->asm_rules
->inputs
, rule
);
1677 static void add_asm_output(struct entrypoint
*ep
, struct instruction
*insn
, struct expression
*expr
,
1678 const char *constraint
, const struct ident
*ident
)
1680 struct access_data ad
= { NULL
, };
1681 pseudo_t pseudo
= alloc_pseudo(insn
);
1682 struct asm_constraint
*rule
;
1684 if (!expr
|| !linearize_address_gen(ep
, expr
, &ad
))
1686 linearize_store_gen(ep
, pseudo
, &ad
);
1687 finish_address_gen(ep
, &ad
);
1688 rule
= __alloc_asm_constraint(0);
1689 rule
->ident
= ident
;
1690 rule
->constraint
= constraint
;
1691 use_pseudo(pseudo
, &rule
->pseudo
);
1692 add_ptr_list(&insn
->asm_rules
->outputs
, rule
);
1695 static pseudo_t
linearize_asm_statement(struct entrypoint
*ep
, struct statement
*stmt
)
1698 struct expression
*expr
;
1699 struct instruction
*insn
;
1700 struct asm_rules
*rules
;
1701 const char *constraint
;
1702 struct ident
*ident
;
1704 insn
= alloc_instruction(OP_ASM
, 0);
1705 expr
= stmt
->asm_string
;
1706 if (!expr
|| expr
->type
!= EXPR_STRING
) {
1707 warning(stmt
->pos
, "expected string in inline asm");
1710 insn
->string
= expr
->string
->data
;
1712 rules
= __alloc_asm_rules(0);
1713 insn
->asm_rules
= rules
;
1715 /* Gather the inputs.. */
1719 FOR_EACH_PTR(stmt
->asm_inputs
, expr
) {
1721 case 0: /* Identifier */
1723 ident
= (struct ident
*)expr
;
1726 case 1: /* Constraint */
1728 constraint
= expr
? expr
->string
->data
: "";
1731 case 2: /* Expression */
1733 add_asm_input(ep
, insn
, expr
, constraint
, ident
);
1735 } END_FOR_EACH_PTR(expr
);
1737 add_one_insn(ep
, insn
);
1739 /* Assign the outputs */
1743 FOR_EACH_PTR(stmt
->asm_outputs
, expr
) {
1745 case 0: /* Identifier */
1747 ident
= (struct ident
*)expr
;
1750 case 1: /* Constraint */
1752 constraint
= expr
? expr
->string
->data
: "";
1757 add_asm_output(ep
, insn
, expr
, constraint
, ident
);
1759 } END_FOR_EACH_PTR(expr
);
1764 static int multijmp_cmp(const void *_a
, const void *_b
)
1766 const struct multijmp
*a
= _a
;
1767 const struct multijmp
*b
= _b
;
1770 if (a
->begin
> a
->end
) {
1771 if (b
->begin
> b
->end
)
1775 if (b
->begin
> b
->end
)
1777 if (a
->begin
== b
->begin
) {
1778 if (a
->end
== b
->end
)
1780 return (a
->end
< b
->end
) ? -1 : 1;
1782 return a
->begin
< b
->begin
? -1 : 1;
1785 static void sort_switch_cases(struct instruction
*insn
)
1787 sort_list((struct ptr_list
**)&insn
->multijmp_list
, multijmp_cmp
);
1790 static pseudo_t
linearize_declaration(struct entrypoint
*ep
, struct statement
*stmt
)
1794 concat_symbol_list(stmt
->declaration
, &ep
->syms
);
1796 FOR_EACH_PTR(stmt
->declaration
, sym
) {
1797 linearize_one_symbol(ep
, sym
);
1798 } END_FOR_EACH_PTR(sym
);
1802 pseudo_t
linearize_statement(struct entrypoint
*ep
, struct statement
*stmt
)
1804 struct basic_block
*bb
;
1810 if (bb
&& !bb
->insns
)
1811 bb
->pos
= stmt
->pos
;
1812 current_pos
= stmt
->pos
;
1814 switch (stmt
->type
) {
1818 case STMT_DECLARATION
:
1819 return linearize_declaration(ep
, stmt
);
1822 return linearize_context(ep
, stmt
);
1825 return linearize_range(ep
, stmt
);
1827 case STMT_EXPRESSION
:
1828 return linearize_expression(ep
, stmt
->expression
);
1831 return linearize_asm_statement(ep
, stmt
);
1834 struct expression
*expr
= stmt
->expression
;
1835 struct basic_block
*bb_return
= get_bound_block(ep
, stmt
->ret_target
);
1836 struct basic_block
*active
;
1837 pseudo_t src
= linearize_expression(ep
, expr
);
1838 active
= ep
->active
;
1839 if (active
&& src
!= &void_pseudo
) {
1840 struct instruction
*phi_node
= first_instruction(bb_return
->insns
);
1843 phi_node
= alloc_typed_instruction(OP_PHI
, expr
->ctype
);
1844 phi_node
->target
= alloc_pseudo(phi_node
);
1845 phi_node
->bb
= bb_return
;
1846 add_instruction(&bb_return
->insns
, phi_node
);
1848 phi
= alloc_phi(active
, src
, type_size(expr
->ctype
));
1849 phi
->ident
= &return_ident
;
1850 use_pseudo(phi
, add_pseudo(&phi_node
->phi_list
, phi
));
1852 add_goto(ep
, bb_return
);
1857 add_label(ep
, stmt
->case_label
);
1858 linearize_statement(ep
, stmt
->case_statement
);
1863 struct symbol
*label
= stmt
->label_identifier
;
1866 add_label(ep
, label
);
1867 linearize_statement(ep
, stmt
->label_statement
);
1874 struct expression
*expr
;
1875 struct instruction
*goto_ins
;
1876 struct basic_block
*active
;
1879 active
= ep
->active
;
1880 if (!bb_reachable(active
))
1883 if (stmt
->goto_label
) {
1884 add_goto(ep
, get_bound_block(ep
, stmt
->goto_label
));
1888 expr
= stmt
->goto_expression
;
1892 /* This can happen as part of simplification */
1893 if (expr
->type
== EXPR_LABEL
) {
1894 add_goto(ep
, get_bound_block(ep
, expr
->label_symbol
));
1898 pseudo
= linearize_expression(ep
, expr
);
1899 goto_ins
= alloc_instruction(OP_COMPUTEDGOTO
, 0);
1900 use_pseudo(pseudo
, &goto_ins
->target
);
1901 add_one_insn(ep
, goto_ins
);
1903 FOR_EACH_PTR(stmt
->target_list
, sym
) {
1904 struct basic_block
*bb_computed
= get_bound_block(ep
, sym
);
1905 struct multijmp
*jmp
= alloc_multijmp(bb_computed
, 1, 0);
1906 add_multijmp(&goto_ins
->multijmp_list
, jmp
);
1907 add_bb(&bb_computed
->parents
, ep
->active
);
1908 add_bb(&active
->children
, bb_computed
);
1909 } END_FOR_EACH_PTR(sym
);
1916 return linearize_compound_statement(ep
, stmt
);
1919 * This could take 'likely/unlikely' into account, and
1920 * switch the arms around appropriately..
1923 struct basic_block
*bb_true
, *bb_false
, *endif
;
1924 struct expression
*cond
= stmt
->if_conditional
;
1926 bb_true
= alloc_basic_block(ep
, stmt
->pos
);
1927 bb_false
= endif
= alloc_basic_block(ep
, stmt
->pos
);
1929 linearize_cond_branch(ep
, cond
, bb_true
, bb_false
);
1931 set_activeblock(ep
, bb_true
);
1932 linearize_statement(ep
, stmt
->if_true
);
1934 if (stmt
->if_false
) {
1935 endif
= alloc_basic_block(ep
, stmt
->pos
);
1936 add_goto(ep
, endif
);
1937 set_activeblock(ep
, bb_false
);
1938 linearize_statement(ep
, stmt
->if_false
);
1940 set_activeblock(ep
, endif
);
1946 struct instruction
*switch_ins
;
1947 struct basic_block
*switch_end
= alloc_basic_block(ep
, stmt
->pos
);
1948 struct basic_block
*active
, *default_case
;
1949 struct multijmp
*jmp
;
1952 pseudo
= linearize_expression(ep
, stmt
->switch_expression
);
1954 active
= ep
->active
;
1955 if (!bb_reachable(active
))
1958 switch_ins
= alloc_instruction(OP_SWITCH
, 0);
1959 use_pseudo(pseudo
, &switch_ins
->cond
);
1960 add_one_insn(ep
, switch_ins
);
1963 default_case
= NULL
;
1964 FOR_EACH_PTR(stmt
->switch_case
->symbol_list
, sym
) {
1965 struct statement
*case_stmt
= sym
->stmt
;
1966 struct basic_block
*bb_case
= get_bound_block(ep
, sym
);
1968 if (!case_stmt
->case_expression
) {
1969 default_case
= bb_case
;
1974 begin
= end
= case_stmt
->case_expression
->value
;
1975 if (case_stmt
->case_to
)
1976 end
= case_stmt
->case_to
->value
;
1978 jmp
= alloc_multijmp(bb_case
, end
, begin
);
1980 jmp
= alloc_multijmp(bb_case
, begin
, end
);
1983 add_multijmp(&switch_ins
->multijmp_list
, jmp
);
1984 add_bb(&bb_case
->parents
, active
);
1985 add_bb(&active
->children
, bb_case
);
1986 } END_FOR_EACH_PTR(sym
);
1988 bind_label(stmt
->switch_break
, switch_end
, stmt
->pos
);
1990 /* And linearize the actual statement */
1991 linearize_statement(ep
, stmt
->switch_statement
);
1992 set_activeblock(ep
, switch_end
);
1995 default_case
= switch_end
;
1997 jmp
= alloc_multijmp(default_case
, 1, 0);
1998 add_multijmp(&switch_ins
->multijmp_list
, jmp
);
1999 add_bb(&default_case
->parents
, active
);
2000 add_bb(&active
->children
, default_case
);
2001 sort_switch_cases(switch_ins
);
2006 case STMT_ITERATOR
: {
2007 struct statement
*pre_statement
= stmt
->iterator_pre_statement
;
2008 struct expression
*pre_condition
= stmt
->iterator_pre_condition
;
2009 struct statement
*statement
= stmt
->iterator_statement
;
2010 struct statement
*post_statement
= stmt
->iterator_post_statement
;
2011 struct expression
*post_condition
= stmt
->iterator_post_condition
;
2012 struct basic_block
*loop_top
, *loop_body
, *loop_continue
, *loop_end
;
2014 concat_symbol_list(stmt
->iterator_syms
, &ep
->syms
);
2015 linearize_statement(ep
, pre_statement
);
2017 loop_body
= loop_top
= alloc_basic_block(ep
, stmt
->pos
);
2018 loop_continue
= alloc_basic_block(ep
, stmt
->pos
);
2019 loop_end
= alloc_basic_block(ep
, stmt
->pos
);
2021 /* An empty post-condition means that it's the same as the pre-condition */
2022 if (!post_condition
) {
2023 loop_top
= alloc_basic_block(ep
, stmt
->pos
);
2024 set_activeblock(ep
, loop_top
);
2028 linearize_cond_branch(ep
, pre_condition
, loop_body
, loop_end
);
2030 bind_label(stmt
->iterator_continue
, loop_continue
, stmt
->pos
);
2031 bind_label(stmt
->iterator_break
, loop_end
, stmt
->pos
);
2033 set_activeblock(ep
, loop_body
);
2034 linearize_statement(ep
, statement
);
2035 add_goto(ep
, loop_continue
);
2037 set_activeblock(ep
, loop_continue
);
2038 linearize_statement(ep
, post_statement
);
2039 if (!post_condition
)
2040 add_goto(ep
, loop_top
);
2042 linearize_cond_branch(ep
, post_condition
, loop_top
, loop_end
);
2043 set_activeblock(ep
, loop_end
);
2053 static struct entrypoint
*linearize_fn(struct symbol
*sym
, struct symbol
*base_type
)
2055 struct entrypoint
*ep
;
2056 struct basic_block
*bb
;
2058 struct instruction
*entry
;
2062 if (!base_type
->stmt
)
2065 ep
= alloc_entrypoint();
2066 bb
= alloc_basic_block(ep
, sym
->pos
);
2069 set_activeblock(ep
, bb
);
2071 entry
= alloc_instruction(OP_ENTRY
, 0);
2072 add_one_insn(ep
, entry
);
2075 concat_symbol_list(base_type
->arguments
, &ep
->syms
);
2077 /* FIXME!! We should do something else about varargs.. */
2079 FOR_EACH_PTR(base_type
->arguments
, arg
) {
2080 linearize_argument(ep
, arg
, ++i
);
2081 } END_FOR_EACH_PTR(arg
);
2083 result
= linearize_statement(ep
, base_type
->stmt
);
2084 if (bb_reachable(ep
->active
) && !bb_terminated(ep
->active
)) {
2085 struct symbol
*ret_type
= base_type
->ctype
.base_type
;
2086 struct instruction
*insn
= alloc_typed_instruction(OP_RET
, ret_type
);
2088 if (type_size(ret_type
) > 0)
2089 use_pseudo(result
, &insn
->src
);
2090 add_one_insn(ep
, insn
);
2094 * Do trivial flow simplification - branches to
2095 * branches, kill dead basicblocks etc
2097 kill_unreachable_bbs(ep
);
2100 * Turn symbols into pseudos
2102 simplify_symbol_usage(ep
);
2106 * Remove trivial instructions, and try to CSE
2110 cleanup_and_cse(ep
);
2111 pack_basic_blocks(ep
);
2112 } while (repeat_phase
& REPEAT_CSE
);
2114 kill_unreachable_bbs(ep
);
2118 clear_symbol_pseudos(ep
);
2120 /* And track pseudo register usage */
2121 track_pseudo_liveness(ep
);
2124 * Some flow optimizations can only effectively
2125 * be done when we've done liveness analysis. But
2126 * if they trigger, we need to start all over
2129 if (simplify_flow(ep
)) {
2134 /* Finally, add deathnotes to pseudos now that we have them */
2135 track_pseudo_death(ep
);
2140 struct entrypoint
*linearize_symbol(struct symbol
*sym
)
2142 struct symbol
*base_type
;
2146 current_pos
= sym
->pos
;
2147 base_type
= sym
->ctype
.base_type
;
2150 if (base_type
->type
== SYM_FN
)
2151 return linearize_fn(sym
, base_type
);