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 pseudo_t
linearize_one_symbol(struct entrypoint
*ep
, struct symbol
*sym
);
33 static pseudo_t
add_load(struct entrypoint
*ep
, struct access_data
*);
34 static 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 ALLOCATOR(pseudo_user
, "pseudo_user");
42 static struct instruction
*alloc_instruction(int opcode
, int size
)
44 struct instruction
* insn
= __alloc_instruction(0);
45 insn
->opcode
= opcode
;
47 insn
->pos
= current_pos
;
51 static inline int type_size(struct symbol
*type
)
53 return type
? type
->bit_size
> 0 ? type
->bit_size
: 0 : 0;
56 static struct instruction
*alloc_typed_instruction(int opcode
, struct symbol
*type
)
58 struct instruction
*insn
= alloc_instruction(opcode
, type_size(type
));
63 static struct entrypoint
*alloc_entrypoint(void)
65 return __alloc_entrypoint(0);
68 static struct basic_block
*alloc_basic_block(struct entrypoint
*ep
, struct position pos
)
71 struct basic_block
*bb
= __alloc_basic_block(0);
79 static struct multijmp
*alloc_multijmp(struct basic_block
*target
, int begin
, int end
)
81 struct multijmp
*multijmp
= __alloc_multijmp(0);
82 multijmp
->target
= target
;
83 multijmp
->begin
= begin
;
88 static inline int regno(pseudo_t n
)
91 if (n
&& n
->type
== PSEUDO_REG
)
96 const char *show_pseudo(pseudo_t pseudo
)
99 static char buffer
[4][64];
107 buf
= buffer
[3 & ++n
];
108 switch(pseudo
->type
) {
110 struct symbol
*sym
= pseudo
->sym
;
111 struct expression
*expr
;
113 if (sym
->bb_target
) {
114 snprintf(buf
, 64, ".L%u", sym
->bb_target
->nr
);
118 snprintf(buf
, 64, "%s", show_ident(sym
->ident
));
121 expr
= sym
->initializer
;
122 snprintf(buf
, 64, "<anon symbol:%p>", sym
);
124 switch (expr
->type
) {
126 snprintf(buf
, 64, "<symbol value: %lld>", expr
->value
);
129 return show_string(expr
->string
);
137 i
= snprintf(buf
, 64, "%%r%d", pseudo
->nr
);
139 sprintf(buf
+i
, "(%s)", show_ident(pseudo
->ident
));
142 long long value
= pseudo
->value
;
143 if (value
> 1000 || value
< -1000)
144 snprintf(buf
, 64, "$%#llx", value
);
146 snprintf(buf
, 64, "$%lld", value
);
150 snprintf(buf
, 64, "%%arg%d", pseudo
->nr
);
153 i
= snprintf(buf
, 64, "%%phi%d", pseudo
->nr
);
155 sprintf(buf
+i
, "(%s)", show_ident(pseudo
->ident
));
158 snprintf(buf
, 64, "<bad pseudo type %d>", pseudo
->type
);
163 static const char *opcodes
[] = {
164 [OP_BADOP
] = "bad_op",
167 [OP_ENTRY
] = "<entry-point>",
172 [OP_SWITCH
] = "switch",
173 [OP_INVOKE
] = "invoke",
174 [OP_COMPUTEDGOTO
] = "jmp *",
175 [OP_UNWIND
] = "unwind",
194 [OP_AND_BOOL
] = "and-bool",
195 [OP_OR_BOOL
] = "or-bool",
197 /* Binary comparison */
198 [OP_SET_EQ
] = "seteq",
199 [OP_SET_NE
] = "setne",
200 [OP_SET_LE
] = "setle",
201 [OP_SET_GE
] = "setge",
202 [OP_SET_LT
] = "setlt",
203 [OP_SET_GT
] = "setgt",
206 [OP_SET_BE
] = "setbe",
207 [OP_SET_AE
] = "setae",
213 /* Special three-input */
217 [OP_MALLOC
] = "malloc",
219 [OP_ALLOCA
] = "alloca",
221 [OP_STORE
] = "store",
223 [OP_SYMADDR
] = "symaddr",
224 [OP_GET_ELEMENT_PTR
] = "getelem",
228 [OP_PHISOURCE
] = "phisrc",
230 [OP_SCAST
] = "scast",
231 [OP_FPCAST
] = "fpcast",
232 [OP_PTRCAST
] = "ptrcast",
233 [OP_INLINED_CALL
] = "# call",
235 [OP_VANEXT
] = "va_next",
236 [OP_VAARG
] = "va_arg",
237 [OP_SLICE
] = "slice",
241 [OP_DEATHNOTE
] = "dead",
244 /* Sparse tagging (line numbers, context, whatever) */
245 [OP_CONTEXT
] = "context",
246 [OP_RANGE
] = "range-check",
251 static char *show_asm_constraints(char *buf
, const char *sep
, struct asm_constraint_list
*list
)
253 struct asm_constraint
*entry
;
255 FOR_EACH_PTR(list
, entry
) {
256 buf
+= sprintf(buf
, "%s\"%s\"", sep
, entry
->constraint
);
258 buf
+= sprintf(buf
, " (%s)", show_pseudo(entry
->pseudo
));
260 buf
+= sprintf(buf
, " [%s]", show_ident(entry
->ident
));
262 } END_FOR_EACH_PTR(entry
);
266 static char *show_asm(char *buf
, struct instruction
*insn
)
268 struct asm_rules
*rules
= insn
->asm_rules
;
270 buf
+= sprintf(buf
, "\"%s\"", insn
->string
);
271 buf
= show_asm_constraints(buf
, "\n\t\tout: ", rules
->outputs
);
272 buf
= show_asm_constraints(buf
, "\n\t\tin: ", rules
->inputs
);
273 buf
= show_asm_constraints(buf
, "\n\t\tclobber: ", rules
->clobbers
);
277 const char *show_instruction(struct instruction
*insn
)
279 int opcode
= insn
->opcode
;
280 static char buffer
[4096];
285 buf
+= sprintf(buf
, "# ");
287 if (opcode
< ARRAY_SIZE(opcodes
)) {
288 const char *op
= opcodes
[opcode
];
290 buf
+= sprintf(buf
, "opcode:%d", opcode
);
292 buf
+= sprintf(buf
, "%s", op
);
294 buf
+= sprintf(buf
, ".%d", insn
->size
);
295 memset(buf
, ' ', 20);
299 if (buf
< buffer
+ 12)
303 if (insn
->src
&& insn
->src
!= VOID
)
304 buf
+= sprintf(buf
, "%s", show_pseudo(insn
->src
));
307 if (insn
->bb_true
&& insn
->bb_false
) {
308 buf
+= sprintf(buf
, "%s, .L%u, .L%u", show_pseudo(insn
->cond
), insn
->bb_true
->nr
, insn
->bb_false
->nr
);
311 buf
+= sprintf(buf
, ".L%u", insn
->bb_true
? insn
->bb_true
->nr
: insn
->bb_false
->nr
);
315 struct symbol
*sym
= insn
->symbol
->sym
;
316 buf
+= sprintf(buf
, "%s <- ", show_pseudo(insn
->target
));
318 if (sym
->bb_target
) {
319 buf
+= sprintf(buf
, ".L%u", sym
->bb_target
->nr
);
323 buf
+= sprintf(buf
, "%s", show_ident(sym
->ident
));
326 buf
+= sprintf(buf
, "<anon symbol:%p>", sym
);
331 struct expression
*expr
= insn
->val
;
332 buf
+= sprintf(buf
, "%s <- ", show_pseudo(insn
->target
));
335 buf
+= sprintf(buf
, "%s", "<none>");
339 switch (expr
->type
) {
341 buf
+= sprintf(buf
, "%lld", expr
->value
);
344 buf
+= sprintf(buf
, "%Lf", expr
->fvalue
);
347 buf
+= sprintf(buf
, "%.40s", show_string(expr
->string
));
350 buf
+= sprintf(buf
, "%s", show_ident(expr
->symbol
->ident
));
353 buf
+= sprintf(buf
, ".L%u", expr
->symbol
->bb_target
->nr
);
356 buf
+= sprintf(buf
, "SETVAL EXPR TYPE %d", expr
->type
);
361 struct multijmp
*jmp
;
362 buf
+= sprintf(buf
, "%s", show_pseudo(insn
->cond
));
363 FOR_EACH_PTR(insn
->multijmp_list
, jmp
) {
364 if (jmp
->begin
== jmp
->end
)
365 buf
+= sprintf(buf
, ", %d -> .L%u", jmp
->begin
, jmp
->target
->nr
);
366 else if (jmp
->begin
< jmp
->end
)
367 buf
+= sprintf(buf
, ", %d ... %d -> .L%u", jmp
->begin
, jmp
->end
, jmp
->target
->nr
);
369 buf
+= sprintf(buf
, ", default -> .L%u", jmp
->target
->nr
);
370 } END_FOR_EACH_PTR(jmp
);
373 case OP_COMPUTEDGOTO
: {
374 struct multijmp
*jmp
;
375 buf
+= sprintf(buf
, "%s", show_pseudo(insn
->target
));
376 FOR_EACH_PTR(insn
->multijmp_list
, jmp
) {
377 buf
+= sprintf(buf
, ", .L%u", jmp
->target
->nr
);
378 } END_FOR_EACH_PTR(jmp
);
383 struct instruction
*phi
;
384 buf
+= sprintf(buf
, "%s <- %s ", show_pseudo(insn
->target
), show_pseudo(insn
->phi_src
));
385 FOR_EACH_PTR(insn
->phi_users
, phi
) {
386 buf
+= sprintf(buf
, " (%s)", show_pseudo(phi
->target
));
387 } END_FOR_EACH_PTR(phi
);
393 const char *s
= " <-";
394 buf
+= sprintf(buf
, "%s", show_pseudo(insn
->target
));
395 FOR_EACH_PTR(insn
->phi_list
, phi
) {
396 buf
+= sprintf(buf
, "%s %s", s
, show_pseudo(phi
));
398 } END_FOR_EACH_PTR(phi
);
401 case OP_LOAD
: case OP_LNOP
:
402 buf
+= sprintf(buf
, "%s <- %d[%s]", show_pseudo(insn
->target
), insn
->offset
, show_pseudo(insn
->src
));
404 case OP_STORE
: case OP_SNOP
:
405 buf
+= sprintf(buf
, "%s -> %d[%s]", show_pseudo(insn
->target
), insn
->offset
, show_pseudo(insn
->src
));
407 case OP_INLINED_CALL
:
410 if (insn
->target
&& insn
->target
!= VOID
)
411 buf
+= sprintf(buf
, "%s <- ", show_pseudo(insn
->target
));
412 buf
+= sprintf(buf
, "%s", show_pseudo(insn
->func
));
413 FOR_EACH_PTR(insn
->arguments
, arg
) {
414 buf
+= sprintf(buf
, ", %s", show_pseudo(arg
));
415 } END_FOR_EACH_PTR(arg
);
422 buf
+= sprintf(buf
, "%s <- (%d) %s",
423 show_pseudo(insn
->target
),
424 type_size(insn
->orig_type
),
425 show_pseudo(insn
->src
));
427 case OP_BINARY
... OP_BINARY_END
:
428 case OP_BINCMP
... OP_BINCMP_END
:
429 buf
+= sprintf(buf
, "%s <- %s, %s", show_pseudo(insn
->target
), show_pseudo(insn
->src1
), show_pseudo(insn
->src2
));
433 buf
+= sprintf(buf
, "%s <- %s, %s, %s", show_pseudo(insn
->target
),
434 show_pseudo(insn
->src1
), show_pseudo(insn
->src2
), show_pseudo(insn
->src3
));
438 buf
+= sprintf(buf
, "%s <- %s, %d, %d", show_pseudo(insn
->target
), show_pseudo(insn
->base
), insn
->from
, insn
->len
);
441 case OP_NOT
: case OP_NEG
:
442 buf
+= sprintf(buf
, "%s <- %s", show_pseudo(insn
->target
), show_pseudo(insn
->src1
));
446 buf
+= sprintf(buf
, "%s%d", insn
->check
? "check: " : "", insn
->increment
);
449 buf
+= sprintf(buf
, "%s between %s..%s", show_pseudo(insn
->src1
), show_pseudo(insn
->src2
), show_pseudo(insn
->src3
));
452 buf
+= sprintf(buf
, "%s <- %s", show_pseudo(insn
->target
), show_pseudo(insn
->src1
));
455 buf
+= sprintf(buf
, "%s", show_pseudo(insn
->target
));
458 buf
= show_asm(buf
, insn
);
461 buf
+= sprintf(buf
, "%s <- %s", show_pseudo(insn
->target
), show_pseudo(insn
->src
));
467 if (buf
>= buffer
+ sizeof(buffer
))
468 die("instruction buffer overflowed %td\n", buf
- buffer
);
469 do { --buf
; } while (*buf
== ' ');
474 void show_bb(struct basic_block
*bb
)
476 struct instruction
*insn
;
478 printf(".L%u:\n", bb
->nr
);
480 pseudo_t needs
, defines
;
481 printf("%s:%d\n", stream_name(bb
->pos
.stream
), bb
->pos
.line
);
483 FOR_EACH_PTR(bb
->needs
, needs
) {
484 struct instruction
*def
= needs
->def
;
485 if (def
->opcode
!= OP_PHI
) {
486 printf(" **uses %s (from .L%u)**\n", show_pseudo(needs
), def
->bb
->nr
);
489 const char *sep
= " ";
490 printf(" **uses %s (from", show_pseudo(needs
));
491 FOR_EACH_PTR(def
->phi_list
, phi
) {
494 printf("%s(%s:.L%u)", sep
, show_pseudo(phi
), phi
->def
->bb
->nr
);
496 } END_FOR_EACH_PTR(phi
);
499 } END_FOR_EACH_PTR(needs
);
501 FOR_EACH_PTR(bb
->defines
, defines
) {
502 printf(" **defines %s **\n", show_pseudo(defines
));
503 } END_FOR_EACH_PTR(defines
);
506 struct basic_block
*from
;
507 FOR_EACH_PTR(bb
->parents
, from
) {
508 printf(" **from .L%u (%s:%d:%d)**\n", from
->nr
,
509 stream_name(from
->pos
.stream
), from
->pos
.line
, from
->pos
.pos
);
510 } END_FOR_EACH_PTR(from
);
514 struct basic_block
*to
;
515 FOR_EACH_PTR(bb
->children
, to
) {
516 printf(" **to .L%u (%s:%d:%d)**\n", to
->nr
,
517 stream_name(to
->pos
.stream
), to
->pos
.line
, to
->pos
.pos
);
518 } END_FOR_EACH_PTR(to
);
522 FOR_EACH_PTR(bb
->insns
, insn
) {
523 if (!insn
->bb
&& verbose
< 2)
525 printf("\t%s\n", show_instruction(insn
));
526 } END_FOR_EACH_PTR(insn
);
527 if (!bb_terminated(bb
))
531 static void show_symbol_usage(pseudo_t pseudo
)
533 struct pseudo_user
*pu
;
536 FOR_EACH_PTR(pseudo
->users
, pu
) {
537 printf("\t%s\n", show_instruction(pu
->insn
));
538 } END_FOR_EACH_PTR(pu
);
542 void show_entry(struct entrypoint
*ep
)
545 struct basic_block
*bb
;
547 printf("%s:\n", show_ident(ep
->name
->ident
));
550 printf("ep %p: %s\n", ep
, show_ident(ep
->name
->ident
));
552 FOR_EACH_PTR(ep
->syms
, sym
) {
555 if (!sym
->pseudo
->users
)
557 printf(" sym: %p %s\n", sym
, show_ident(sym
->ident
));
558 if (sym
->ctype
.modifiers
& (MOD_EXTERN
| MOD_STATIC
| MOD_ADDRESSABLE
))
559 printf("\texternal visibility\n");
560 show_symbol_usage(sym
->pseudo
);
561 } END_FOR_EACH_PTR(sym
);
566 FOR_EACH_PTR(ep
->bbs
, bb
) {
569 if (!bb
->parents
&& !bb
->children
&& !bb
->insns
&& verbose
< 2)
573 } END_FOR_EACH_PTR(bb
);
578 static void bind_label(struct symbol
*label
, struct basic_block
*bb
, struct position pos
)
580 if (label
->bb_target
)
581 warning(pos
, "label '%s' already bound", show_ident(label
->ident
));
582 label
->bb_target
= bb
;
585 static struct basic_block
* get_bound_block(struct entrypoint
*ep
, struct symbol
*label
)
587 struct basic_block
*bb
= label
->bb_target
;
590 bb
= alloc_basic_block(ep
, label
->pos
);
591 label
->bb_target
= bb
;
596 static void finish_block(struct entrypoint
*ep
)
598 struct basic_block
*src
= ep
->active
;
599 if (bb_reachable(src
))
603 static void add_goto(struct entrypoint
*ep
, struct basic_block
*dst
)
605 struct basic_block
*src
= ep
->active
;
606 if (bb_reachable(src
)) {
607 struct instruction
*br
= alloc_instruction(OP_BR
, 0);
609 add_bb(&dst
->parents
, src
);
610 add_bb(&src
->children
, dst
);
612 add_instruction(&src
->insns
, br
);
617 static void add_one_insn(struct entrypoint
*ep
, struct instruction
*insn
)
619 struct basic_block
*bb
= ep
->active
;
621 if (bb_reachable(bb
)) {
623 add_instruction(&bb
->insns
, insn
);
627 static void set_activeblock(struct entrypoint
*ep
, struct basic_block
*bb
)
629 if (!bb_terminated(ep
->active
))
633 if (bb_reachable(bb
))
634 add_bb(&ep
->bbs
, bb
);
637 static void remove_parent(struct basic_block
*child
, struct basic_block
*parent
)
639 remove_bb_from_list(&child
->parents
, parent
, 1);
644 /* Change a "switch" into a branch */
645 void insert_branch(struct basic_block
*bb
, struct instruction
*jmp
, struct basic_block
*target
)
647 struct instruction
*br
, *old
;
648 struct basic_block
*child
;
650 /* Remove the switch */
651 old
= delete_last_instruction(&bb
->insns
);
654 br
= alloc_instruction(OP_BR
, 0);
656 br
->bb_true
= target
;
657 add_instruction(&bb
->insns
, br
);
659 FOR_EACH_PTR(bb
->children
, child
) {
660 if (child
== target
) {
661 target
= NULL
; /* Trigger just once */
664 DELETE_CURRENT_PTR(child
);
665 remove_parent(child
, bb
);
666 } END_FOR_EACH_PTR(child
);
667 PACK_PTR_LIST(&bb
->children
);
671 void insert_select(struct basic_block
*bb
, struct instruction
*br
, struct instruction
*phi_node
, pseudo_t if_true
, pseudo_t if_false
)
674 struct instruction
*select
;
676 /* Remove the 'br' */
677 delete_last_instruction(&bb
->insns
);
679 select
= alloc_instruction(OP_SEL
, phi_node
->size
);
683 use_pseudo(select
, br
->cond
, &select
->src1
);
685 target
= phi_node
->target
;
686 assert(target
->def
== phi_node
);
687 select
->target
= target
;
688 target
->def
= select
;
690 use_pseudo(select
, if_true
, &select
->src2
);
691 use_pseudo(select
, if_false
, &select
->src3
);
693 add_instruction(&bb
->insns
, select
);
694 add_instruction(&bb
->insns
, br
);
697 static inline int bb_empty(struct basic_block
*bb
)
702 /* Add a label to the currently active block, return new active block */
703 static struct basic_block
* add_label(struct entrypoint
*ep
, struct symbol
*label
)
705 struct basic_block
*bb
= label
->bb_target
;
708 set_activeblock(ep
, bb
);
712 if (!bb_reachable(bb
) || !bb_empty(bb
)) {
713 bb
= alloc_basic_block(ep
, label
->pos
);
714 set_activeblock(ep
, bb
);
716 label
->bb_target
= bb
;
720 static void add_branch(struct entrypoint
*ep
, struct expression
*expr
, pseudo_t cond
, struct basic_block
*bb_true
, struct basic_block
*bb_false
)
722 struct basic_block
*bb
= ep
->active
;
723 struct instruction
*br
;
725 if (bb_reachable(bb
)) {
726 br
= alloc_instruction(OP_BR
, 0);
727 use_pseudo(br
, cond
, &br
->cond
);
728 br
->bb_true
= bb_true
;
729 br
->bb_false
= bb_false
;
730 add_bb(&bb_true
->parents
, bb
);
731 add_bb(&bb_false
->parents
, bb
);
732 add_bb(&bb
->children
, bb_true
);
733 add_bb(&bb
->children
, bb_false
);
734 add_one_insn(ep
, br
);
738 /* Dummy pseudo allocator */
739 pseudo_t
alloc_pseudo(struct instruction
*def
)
742 struct pseudo
* pseudo
= __alloc_pseudo(0);
743 pseudo
->type
= PSEUDO_REG
;
749 static void clear_symbol_pseudos(struct entrypoint
*ep
)
753 FOR_EACH_PTR(ep
->accesses
, pseudo
) {
754 pseudo
->sym
->pseudo
= NULL
;
755 } END_FOR_EACH_PTR(pseudo
);
758 static pseudo_t
symbol_pseudo(struct entrypoint
*ep
, struct symbol
*sym
)
765 pseudo
= sym
->pseudo
;
767 pseudo
= __alloc_pseudo(0);
769 pseudo
->type
= PSEUDO_SYM
;
771 pseudo
->ident
= sym
->ident
;
772 sym
->pseudo
= pseudo
;
773 add_pseudo(&ep
->accesses
, pseudo
);
775 /* Symbol pseudos have neither nr, usage nor def */
779 pseudo_t
value_pseudo(long long val
)
781 #define MAX_VAL_HASH 64
782 static struct pseudo_list
*prev
[MAX_VAL_HASH
];
783 int hash
= val
& (MAX_VAL_HASH
-1);
784 struct pseudo_list
**list
= prev
+ hash
;
787 FOR_EACH_PTR(*list
, pseudo
) {
788 if (pseudo
->value
== val
)
790 } END_FOR_EACH_PTR(pseudo
);
792 pseudo
= __alloc_pseudo(0);
793 pseudo
->type
= PSEUDO_VAL
;
795 add_pseudo(list
, pseudo
);
797 /* Value pseudos have neither nr, usage nor def */
801 static pseudo_t
argument_pseudo(struct entrypoint
*ep
, int nr
)
803 pseudo_t pseudo
= __alloc_pseudo(0);
804 struct instruction
*entry
= ep
->entry
;
806 pseudo
->type
= PSEUDO_ARG
;
809 add_pseudo(&entry
->arg_list
, pseudo
);
811 /* Argument pseudos have neither usage nor def */
815 pseudo_t
alloc_phi(struct basic_block
*source
, pseudo_t pseudo
, int size
)
817 struct instruction
*insn
= alloc_instruction(OP_PHISOURCE
, size
);
818 pseudo_t phi
= __alloc_pseudo(0);
821 phi
->type
= PSEUDO_PHI
;
825 use_pseudo(insn
, pseudo
, &insn
->phi_src
);
828 add_instruction(&source
->insns
, insn
);
833 * We carry the "access_data" structure around for any accesses,
834 * which simplifies things a lot. It contains all the access
835 * information in one place.
838 struct symbol
*result_type
; // result ctype
839 struct symbol
*source_type
; // source ctype
840 pseudo_t address
; // pseudo containing address ..
841 pseudo_t origval
; // pseudo for original value ..
842 unsigned int offset
, alignment
; // byte offset
843 unsigned int bit_size
, bit_offset
; // which bits
847 static void finish_address_gen(struct entrypoint
*ep
, struct access_data
*ad
)
851 static int linearize_simple_address(struct entrypoint
*ep
,
852 struct expression
*addr
,
853 struct access_data
*ad
)
855 if (addr
->type
== EXPR_SYMBOL
) {
856 linearize_one_symbol(ep
, addr
->symbol
);
857 ad
->address
= symbol_pseudo(ep
, addr
->symbol
);
860 if (addr
->type
== EXPR_BINOP
) {
861 if (addr
->right
->type
== EXPR_VALUE
) {
862 if (addr
->op
== '+') {
863 ad
->offset
+= get_expression_value(addr
->right
);
864 return linearize_simple_address(ep
, addr
->left
, ad
);
868 ad
->address
= linearize_expression(ep
, addr
);
872 static struct symbol
*base_type(struct symbol
*sym
)
874 struct symbol
*base
= sym
;
877 if (sym
->type
== SYM_NODE
)
878 base
= base
->ctype
.base_type
;
879 if (base
->type
== SYM_BITFIELD
)
880 return base
->ctype
.base_type
;
885 static int linearize_address_gen(struct entrypoint
*ep
,
886 struct expression
*expr
,
887 struct access_data
*ad
)
889 struct symbol
*ctype
= expr
->ctype
;
894 ad
->result_type
= ctype
;
895 ad
->source_type
= base_type(ctype
);
896 ad
->bit_size
= ctype
->bit_size
;
897 ad
->alignment
= ctype
->ctype
.alignment
;
898 ad
->bit_offset
= ctype
->bit_offset
;
899 if (expr
->type
== EXPR_PREOP
&& expr
->op
== '*')
900 return linearize_simple_address(ep
, expr
->unop
, ad
);
902 warning(expr
->pos
, "generating address of non-lvalue (%d)", expr
->type
);
906 static pseudo_t
add_load(struct entrypoint
*ep
, struct access_data
*ad
)
908 struct instruction
*insn
;
915 insn
= alloc_typed_instruction(OP_LOAD
, ad
->source_type
);
916 new = alloc_pseudo(insn
);
920 insn
->offset
= ad
->offset
;
921 use_pseudo(insn
, ad
->address
, &insn
->src
);
922 add_one_insn(ep
, insn
);
926 static void add_store(struct entrypoint
*ep
, struct access_data
*ad
, pseudo_t value
)
928 struct basic_block
*bb
= ep
->active
;
930 if (bb_reachable(bb
)) {
931 struct instruction
*store
= alloc_typed_instruction(OP_STORE
, ad
->source_type
);
932 store
->offset
= ad
->offset
;
933 use_pseudo(store
, value
, &store
->target
);
934 use_pseudo(store
, ad
->address
, &store
->src
);
935 add_one_insn(ep
, store
);
939 static pseudo_t
linearize_store_gen(struct entrypoint
*ep
,
941 struct access_data
*ad
)
943 pseudo_t store
= value
;
945 if (type_size(ad
->source_type
) != type_size(ad
->result_type
)) {
946 pseudo_t orig
= add_load(ep
, ad
);
947 int shift
= ad
->bit_offset
;
948 unsigned long long mask
= (1ULL << ad
->bit_size
)-1;
951 store
= add_binary_op(ep
, ad
->source_type
, OP_SHL
, value
, value_pseudo(shift
));
954 orig
= add_binary_op(ep
, ad
->source_type
, OP_AND
, orig
, value_pseudo(~mask
));
955 store
= add_binary_op(ep
, ad
->source_type
, OP_OR
, orig
, store
);
957 add_store(ep
, ad
, store
);
961 static pseudo_t
add_binary_op(struct entrypoint
*ep
, struct symbol
*ctype
, int op
, pseudo_t left
, pseudo_t right
)
963 struct instruction
*insn
= alloc_typed_instruction(op
, ctype
);
964 pseudo_t target
= alloc_pseudo(insn
);
965 insn
->target
= target
;
966 use_pseudo(insn
, left
, &insn
->src1
);
967 use_pseudo(insn
, right
, &insn
->src2
);
968 add_one_insn(ep
, insn
);
972 static pseudo_t
add_setval(struct entrypoint
*ep
, struct symbol
*ctype
, struct expression
*val
)
974 struct instruction
*insn
= alloc_typed_instruction(OP_SETVAL
, ctype
);
975 pseudo_t target
= alloc_pseudo(insn
);
976 insn
->target
= target
;
978 add_one_insn(ep
, insn
);
982 static pseudo_t
add_symbol_address(struct entrypoint
*ep
, struct symbol
*sym
)
984 struct instruction
*insn
= alloc_instruction(OP_SYMADDR
, bits_in_pointer
);
985 pseudo_t target
= alloc_pseudo(insn
);
987 insn
->target
= target
;
988 use_pseudo(insn
, symbol_pseudo(ep
, sym
), &insn
->symbol
);
989 add_one_insn(ep
, insn
);
993 static pseudo_t
linearize_load_gen(struct entrypoint
*ep
, struct access_data
*ad
)
995 pseudo_t
new = add_load(ep
, ad
);
997 if (ad
->bit_offset
) {
998 pseudo_t shift
= value_pseudo(ad
->bit_offset
);
999 pseudo_t newval
= add_binary_op(ep
, ad
->source_type
, OP_LSR
, new, shift
);
1006 static pseudo_t
linearize_access(struct entrypoint
*ep
, struct expression
*expr
)
1008 struct access_data ad
= { NULL
, };
1011 if (!linearize_address_gen(ep
, expr
, &ad
))
1013 value
= linearize_load_gen(ep
, &ad
);
1014 finish_address_gen(ep
, &ad
);
1019 static pseudo_t
linearize_inc_dec(struct entrypoint
*ep
, struct expression
*expr
, int postop
)
1021 struct access_data ad
= { NULL
, };
1022 pseudo_t old
, new, one
;
1023 int op
= expr
->op
== SPECIAL_INCREMENT
? OP_ADD
: OP_SUB
;
1025 if (!linearize_address_gen(ep
, expr
->unop
, &ad
))
1028 old
= linearize_load_gen(ep
, &ad
);
1029 one
= value_pseudo(expr
->op_value
);
1030 new = add_binary_op(ep
, expr
->ctype
, op
, old
, one
);
1031 linearize_store_gen(ep
, new, &ad
);
1032 finish_address_gen(ep
, &ad
);
1033 return postop
? old
: new;
1036 static pseudo_t
add_uniop(struct entrypoint
*ep
, struct expression
*expr
, int op
, pseudo_t src
)
1038 struct instruction
*insn
= alloc_typed_instruction(op
, expr
->ctype
);
1039 pseudo_t
new = alloc_pseudo(insn
);
1042 use_pseudo(insn
, src
, &insn
->src1
);
1043 add_one_insn(ep
, insn
);
1047 static pseudo_t
linearize_slice(struct entrypoint
*ep
, struct expression
*expr
)
1049 pseudo_t pre
= linearize_expression(ep
, expr
->base
);
1050 struct instruction
*insn
= alloc_typed_instruction(OP_SLICE
, expr
->ctype
);
1051 pseudo_t
new = alloc_pseudo(insn
);
1054 insn
->from
= expr
->r_bitpos
;
1055 insn
->len
= expr
->r_nrbits
;
1056 use_pseudo(insn
, pre
, &insn
->base
);
1057 add_one_insn(ep
, insn
);
1061 static pseudo_t
linearize_regular_preop(struct entrypoint
*ep
, struct expression
*expr
)
1063 pseudo_t pre
= linearize_expression(ep
, expr
->unop
);
1068 pseudo_t zero
= value_pseudo(0);
1069 return add_binary_op(ep
, expr
->ctype
, OP_SET_EQ
, pre
, zero
);
1072 return add_uniop(ep
, expr
, OP_NOT
, pre
);
1074 return add_uniop(ep
, expr
, OP_NEG
, pre
);
1079 static pseudo_t
linearize_preop(struct entrypoint
*ep
, struct expression
*expr
)
1082 * '*' is an lvalue access, and is fundamentally different
1083 * from an arithmetic operation. Maybe it should have an
1084 * expression type of its own..
1086 if (expr
->op
== '*')
1087 return linearize_access(ep
, expr
);
1088 if (expr
->op
== SPECIAL_INCREMENT
|| expr
->op
== SPECIAL_DECREMENT
)
1089 return linearize_inc_dec(ep
, expr
, 0);
1090 return linearize_regular_preop(ep
, expr
);
1093 static pseudo_t
linearize_postop(struct entrypoint
*ep
, struct expression
*expr
)
1095 return linearize_inc_dec(ep
, expr
, 1);
1099 * Casts to pointers are "less safe" than other casts, since
1100 * they imply type-unsafe accesses. "void *" is a special
1101 * case, since you can't access through it anyway without another
1104 static struct instruction
*alloc_cast_instruction(struct symbol
*src
, struct symbol
*ctype
)
1106 int opcode
= OP_CAST
;
1107 struct symbol
*base
= ctype
;
1109 if (src
->ctype
.modifiers
& MOD_SIGNED
)
1111 if (base
->type
== SYM_NODE
)
1112 base
= base
->ctype
.base_type
;
1113 if (base
->type
== SYM_PTR
) {
1114 base
= base
->ctype
.base_type
;
1115 if (base
!= &void_ctype
)
1116 opcode
= OP_PTRCAST
;
1118 if (base
->ctype
.base_type
== &fp_type
)
1120 return alloc_typed_instruction(opcode
, ctype
);
1123 static pseudo_t
cast_pseudo(struct entrypoint
*ep
, pseudo_t src
, struct symbol
*from
, struct symbol
*to
)
1126 struct instruction
*insn
;
1132 if (from
->bit_size
< 0 || to
->bit_size
< 0)
1134 insn
= alloc_cast_instruction(from
, to
);
1135 result
= alloc_pseudo(insn
);
1136 insn
->target
= result
;
1137 insn
->orig_type
= from
;
1138 use_pseudo(insn
, src
, &insn
->src
);
1139 add_one_insn(ep
, insn
);
1143 static int opcode_sign(int opcode
, struct symbol
*ctype
)
1145 if (ctype
&& (ctype
->ctype
.modifiers
& MOD_SIGNED
)) {
1147 case OP_MULU
: case OP_DIVU
: case OP_MODU
: case OP_LSR
:
1154 static pseudo_t
linearize_assignment(struct entrypoint
*ep
, struct expression
*expr
)
1156 struct access_data ad
= { NULL
, };
1157 struct expression
*target
= expr
->left
;
1158 struct expression
*src
= expr
->right
;
1159 struct symbol
*ctype
;
1162 value
= linearize_expression(ep
, src
);
1163 if (!target
|| !linearize_address_gen(ep
, target
, &ad
))
1165 if (expr
->op
!= '=') {
1166 pseudo_t oldvalue
= linearize_load_gen(ep
, &ad
);
1168 static const int op_trans
[] = {
1169 [SPECIAL_ADD_ASSIGN
- SPECIAL_BASE
] = OP_ADD
,
1170 [SPECIAL_SUB_ASSIGN
- SPECIAL_BASE
] = OP_SUB
,
1171 [SPECIAL_MUL_ASSIGN
- SPECIAL_BASE
] = OP_MULU
,
1172 [SPECIAL_DIV_ASSIGN
- SPECIAL_BASE
] = OP_DIVU
,
1173 [SPECIAL_MOD_ASSIGN
- SPECIAL_BASE
] = OP_MODU
,
1174 [SPECIAL_SHL_ASSIGN
- SPECIAL_BASE
] = OP_SHL
,
1175 [SPECIAL_SHR_ASSIGN
- SPECIAL_BASE
] = OP_LSR
,
1176 [SPECIAL_AND_ASSIGN
- SPECIAL_BASE
] = OP_AND
,
1177 [SPECIAL_OR_ASSIGN
- SPECIAL_BASE
] = OP_OR
,
1178 [SPECIAL_XOR_ASSIGN
- SPECIAL_BASE
] = OP_XOR
1186 oldvalue
= cast_pseudo(ep
, oldvalue
, target
->ctype
, ctype
);
1187 opcode
= opcode_sign(op_trans
[expr
->op
- SPECIAL_BASE
], ctype
);
1188 dst
= add_binary_op(ep
, ctype
, opcode
, oldvalue
, value
);
1189 value
= cast_pseudo(ep
, dst
, ctype
, expr
->ctype
);
1191 value
= linearize_store_gen(ep
, value
, &ad
);
1192 finish_address_gen(ep
, &ad
);
1196 static pseudo_t
linearize_call_expression(struct entrypoint
*ep
, struct expression
*expr
)
1198 struct expression
*arg
, *fn
;
1199 struct instruction
*insn
= alloc_typed_instruction(OP_CALL
, expr
->ctype
);
1200 pseudo_t retval
, call
;
1201 struct ctype
*ctype
= NULL
;
1202 struct symbol
*fntype
;
1203 struct context
*context
;
1206 warning(expr
->pos
, "call with no type!");
1210 FOR_EACH_PTR(expr
->args
, arg
) {
1211 pseudo_t
new = linearize_expression(ep
, arg
);
1212 use_pseudo(insn
, new, add_pseudo(&insn
->arguments
, new));
1213 } END_FOR_EACH_PTR(arg
);
1218 ctype
= &fn
->ctype
->ctype
;
1222 if (fntype
->type
== SYM_NODE
)
1223 fntype
= fntype
->ctype
.base_type
;
1225 insn
->fntype
= fntype
;
1227 if (fn
->type
== EXPR_PREOP
) {
1228 if (fn
->unop
->type
== EXPR_SYMBOL
) {
1229 struct symbol
*sym
= fn
->unop
->symbol
;
1230 if (sym
->ctype
.base_type
->type
== SYM_FN
)
1234 if (fn
->type
== EXPR_SYMBOL
) {
1235 call
= symbol_pseudo(ep
, fn
->symbol
);
1237 call
= linearize_expression(ep
, fn
);
1239 use_pseudo(insn
, call
, &insn
->func
);
1241 if (expr
->ctype
!= &void_ctype
)
1242 retval
= alloc_pseudo(insn
);
1243 insn
->target
= retval
;
1244 add_one_insn(ep
, insn
);
1247 FOR_EACH_PTR(ctype
->contexts
, context
) {
1248 int in
= context
->in
;
1249 int out
= context
->out
;
1260 context_diff
= out
- in
;
1261 if (check
|| context_diff
) {
1262 insn
= alloc_instruction(OP_CONTEXT
, 0);
1263 insn
->increment
= context_diff
;
1264 insn
->check
= check
;
1265 insn
->context_expr
= context
->context
;
1266 add_one_insn(ep
, insn
);
1268 } END_FOR_EACH_PTR(context
);
1274 static pseudo_t
linearize_binop(struct entrypoint
*ep
, struct expression
*expr
)
1276 pseudo_t src1
, src2
, dst
;
1277 static const int opcode
[] = {
1278 ['+'] = OP_ADD
, ['-'] = OP_SUB
,
1279 ['*'] = OP_MULU
, ['/'] = OP_DIVU
,
1280 ['%'] = OP_MODU
, ['&'] = OP_AND
,
1281 ['|'] = OP_OR
, ['^'] = OP_XOR
,
1282 [SPECIAL_LEFTSHIFT
] = OP_SHL
,
1283 [SPECIAL_RIGHTSHIFT
] = OP_LSR
,
1284 [SPECIAL_LOGICAL_AND
] = OP_AND_BOOL
,
1285 [SPECIAL_LOGICAL_OR
] = OP_OR_BOOL
,
1289 src1
= linearize_expression(ep
, expr
->left
);
1290 src2
= linearize_expression(ep
, expr
->right
);
1291 op
= opcode_sign(opcode
[expr
->op
], expr
->ctype
);
1292 dst
= add_binary_op(ep
, expr
->ctype
, op
, src1
, src2
);
1296 static pseudo_t
linearize_logical_branch(struct entrypoint
*ep
, struct expression
*expr
, struct basic_block
*bb_true
, struct basic_block
*bb_false
);
1298 pseudo_t
linearize_cond_branch(struct entrypoint
*ep
, struct expression
*expr
, struct basic_block
*bb_true
, struct basic_block
*bb_false
);
1300 static pseudo_t
linearize_select(struct entrypoint
*ep
, struct expression
*expr
)
1302 pseudo_t cond
, true, false, res
;
1303 struct instruction
*insn
;
1305 true = linearize_expression(ep
, expr
->cond_true
);
1306 false = linearize_expression(ep
, expr
->cond_false
);
1307 cond
= linearize_expression(ep
, expr
->conditional
);
1309 insn
= alloc_typed_instruction(OP_SEL
, expr
->ctype
);
1310 if (!expr
->cond_true
)
1312 use_pseudo(insn
, cond
, &insn
->src1
);
1313 use_pseudo(insn
, true, &insn
->src2
);
1314 use_pseudo(insn
, false, &insn
->src3
);
1316 res
= alloc_pseudo(insn
);
1318 add_one_insn(ep
, insn
);
1322 static pseudo_t
add_join_conditional(struct entrypoint
*ep
, struct expression
*expr
,
1323 pseudo_t phi1
, pseudo_t phi2
)
1326 struct instruction
*phi_node
;
1333 phi_node
= alloc_typed_instruction(OP_PHI
, expr
->ctype
);
1334 use_pseudo(phi_node
, phi1
, add_pseudo(&phi_node
->phi_list
, phi1
));
1335 use_pseudo(phi_node
, phi2
, add_pseudo(&phi_node
->phi_list
, phi2
));
1336 phi_node
->target
= target
= alloc_pseudo(phi_node
);
1337 add_one_insn(ep
, phi_node
);
1341 static pseudo_t
linearize_short_conditional(struct entrypoint
*ep
, struct expression
*expr
,
1342 struct expression
*cond
,
1343 struct expression
*expr_false
)
1345 pseudo_t src1
, src2
;
1346 struct basic_block
*bb_false
;
1347 struct basic_block
*merge
= alloc_basic_block(ep
, expr
->pos
);
1348 pseudo_t phi1
, phi2
;
1349 int size
= type_size(expr
->ctype
);
1351 if (!expr_false
|| !ep
->active
)
1354 bb_false
= alloc_basic_block(ep
, expr_false
->pos
);
1355 src1
= linearize_expression(ep
, cond
);
1356 phi1
= alloc_phi(ep
->active
, src1
, size
);
1357 add_branch(ep
, expr
, src1
, merge
, bb_false
);
1359 set_activeblock(ep
, bb_false
);
1360 src2
= linearize_expression(ep
, expr_false
);
1361 phi2
= alloc_phi(ep
->active
, src2
, size
);
1362 set_activeblock(ep
, merge
);
1364 return add_join_conditional(ep
, expr
, phi1
, phi2
);
1367 static pseudo_t
linearize_conditional(struct entrypoint
*ep
, struct expression
*expr
,
1368 struct expression
*cond
,
1369 struct expression
*expr_true
,
1370 struct expression
*expr_false
)
1372 pseudo_t src1
, src2
;
1373 pseudo_t phi1
, phi2
;
1374 struct basic_block
*bb_true
, *bb_false
, *merge
;
1375 int size
= type_size(expr
->ctype
);
1377 if (!cond
|| !expr_true
|| !expr_false
|| !ep
->active
)
1379 bb_true
= alloc_basic_block(ep
, expr_true
->pos
);
1380 bb_false
= alloc_basic_block(ep
, expr_false
->pos
);
1381 merge
= alloc_basic_block(ep
, expr
->pos
);
1383 linearize_cond_branch(ep
, cond
, bb_true
, bb_false
);
1385 set_activeblock(ep
, bb_true
);
1386 src1
= linearize_expression(ep
, expr_true
);
1387 phi1
= alloc_phi(ep
->active
, src1
, size
);
1388 add_goto(ep
, merge
);
1390 set_activeblock(ep
, bb_false
);
1391 src2
= linearize_expression(ep
, expr_false
);
1392 phi2
= alloc_phi(ep
->active
, src2
, size
);
1393 set_activeblock(ep
, merge
);
1395 return add_join_conditional(ep
, expr
, phi1
, phi2
);
1398 static pseudo_t
linearize_logical(struct entrypoint
*ep
, struct expression
*expr
)
1400 struct expression
*shortcut
;
1402 shortcut
= alloc_const_expression(expr
->pos
, expr
->op
== SPECIAL_LOGICAL_OR
);
1403 shortcut
->ctype
= expr
->ctype
;
1404 if (expr
->op
== SPECIAL_LOGICAL_OR
)
1405 return linearize_conditional(ep
, expr
, expr
->left
, shortcut
, expr
->right
);
1406 return linearize_conditional(ep
, expr
, expr
->left
, expr
->right
, shortcut
);
1409 static pseudo_t
linearize_compare(struct entrypoint
*ep
, struct expression
*expr
)
1411 static const int cmpop
[] = {
1412 ['>'] = OP_SET_GT
, ['<'] = OP_SET_LT
,
1413 [SPECIAL_EQUAL
] = OP_SET_EQ
,
1414 [SPECIAL_NOTEQUAL
] = OP_SET_NE
,
1415 [SPECIAL_GTE
] = OP_SET_GE
,
1416 [SPECIAL_LTE
] = OP_SET_LE
,
1417 [SPECIAL_UNSIGNED_LT
] = OP_SET_B
,
1418 [SPECIAL_UNSIGNED_GT
] = OP_SET_A
,
1419 [SPECIAL_UNSIGNED_LTE
] = OP_SET_BE
,
1420 [SPECIAL_UNSIGNED_GTE
] = OP_SET_AE
,
1423 pseudo_t src1
= linearize_expression(ep
, expr
->left
);
1424 pseudo_t src2
= linearize_expression(ep
, expr
->right
);
1425 pseudo_t dst
= add_binary_op(ep
, expr
->ctype
, cmpop
[expr
->op
], src1
, src2
);
1430 pseudo_t
linearize_cond_branch(struct entrypoint
*ep
, struct expression
*expr
, struct basic_block
*bb_true
, struct basic_block
*bb_false
)
1434 if (!expr
|| !bb_reachable(ep
->active
))
1437 switch (expr
->type
) {
1441 add_goto(ep
, expr
->value
? bb_true
: bb_false
);
1445 add_goto(ep
, expr
->fvalue
? bb_true
: bb_false
);
1449 linearize_logical_branch(ep
, expr
, bb_true
, bb_false
);
1453 cond
= linearize_compare(ep
, expr
);
1454 add_branch(ep
, expr
, cond
, bb_true
, bb_false
);
1458 if (expr
->op
== '!')
1459 return linearize_cond_branch(ep
, expr
->unop
, bb_false
, bb_true
);
1462 cond
= linearize_expression(ep
, expr
);
1463 add_branch(ep
, expr
, cond
, bb_true
, bb_false
);
1473 static pseudo_t
linearize_logical_branch(struct entrypoint
*ep
, struct expression
*expr
, struct basic_block
*bb_true
, struct basic_block
*bb_false
)
1475 struct basic_block
*next
= alloc_basic_block(ep
, expr
->pos
);
1477 if (expr
->op
== SPECIAL_LOGICAL_OR
)
1478 linearize_cond_branch(ep
, expr
->left
, bb_true
, next
);
1480 linearize_cond_branch(ep
, expr
->left
, next
, bb_false
);
1481 set_activeblock(ep
, next
);
1482 linearize_cond_branch(ep
, expr
->right
, bb_true
, bb_false
);
1486 static pseudo_t
linearize_cast(struct entrypoint
*ep
, struct expression
*expr
)
1489 struct expression
*orig
= expr
->cast_expression
;
1494 src
= linearize_expression(ep
, orig
);
1495 return cast_pseudo(ep
, src
, orig
->ctype
, expr
->ctype
);
1498 static pseudo_t
linearize_position(struct entrypoint
*ep
, struct expression
*pos
, struct access_data
*ad
)
1500 struct expression
*init_expr
= pos
->init_expr
;
1502 ad
->offset
= pos
->init_offset
;
1503 ad
->source_type
= base_type(init_expr
->ctype
);
1504 ad
->result_type
= init_expr
->ctype
;
1505 return linearize_initializer(ep
, init_expr
, ad
);
1508 static pseudo_t
linearize_initializer(struct entrypoint
*ep
, struct expression
*initializer
, struct access_data
*ad
)
1510 switch (initializer
->type
) {
1511 case EXPR_INITIALIZER
: {
1512 struct expression
*expr
;
1513 FOR_EACH_PTR(initializer
->expr_list
, expr
) {
1514 linearize_initializer(ep
, expr
, ad
);
1515 } END_FOR_EACH_PTR(expr
);
1519 linearize_position(ep
, initializer
, ad
);
1522 pseudo_t value
= linearize_expression(ep
, initializer
);
1523 ad
->source_type
= base_type(initializer
->ctype
);
1524 ad
->result_type
= initializer
->ctype
;
1525 linearize_store_gen(ep
, value
, ad
);
1533 static void linearize_argument(struct entrypoint
*ep
, struct symbol
*arg
, int nr
)
1535 struct access_data ad
= { NULL
, };
1537 ad
.source_type
= arg
;
1538 ad
.result_type
= arg
;
1539 ad
.address
= symbol_pseudo(ep
, arg
);
1540 linearize_store_gen(ep
, argument_pseudo(ep
, nr
), &ad
);
1541 finish_address_gen(ep
, &ad
);
1544 pseudo_t
linearize_expression(struct entrypoint
*ep
, struct expression
*expr
)
1549 current_pos
= expr
->pos
;
1550 switch (expr
->type
) {
1552 linearize_one_symbol(ep
, expr
->symbol
);
1553 return add_symbol_address(ep
, expr
->symbol
);
1556 return value_pseudo(expr
->value
);
1558 case EXPR_STRING
: case EXPR_FVALUE
: case EXPR_LABEL
:
1559 return add_setval(ep
, expr
->ctype
, expr
);
1561 case EXPR_STATEMENT
:
1562 return linearize_statement(ep
, expr
->statement
);
1565 return linearize_call_expression(ep
, expr
);
1568 return linearize_binop(ep
, expr
);
1571 return linearize_logical(ep
, expr
);
1574 return linearize_compare(ep
, expr
);
1577 return linearize_select(ep
, expr
);
1579 case EXPR_CONDITIONAL
:
1580 if (!expr
->cond_true
)
1581 return linearize_short_conditional(ep
, expr
, expr
->conditional
, expr
->cond_false
);
1583 return linearize_conditional(ep
, expr
, expr
->conditional
,
1584 expr
->cond_true
, expr
->cond_false
);
1587 linearize_expression(ep
, expr
->left
);
1588 return linearize_expression(ep
, expr
->right
);
1590 case EXPR_ASSIGNMENT
:
1591 return linearize_assignment(ep
, expr
);
1594 return linearize_preop(ep
, expr
);
1597 return linearize_postop(ep
, expr
);
1600 case EXPR_FORCE_CAST
:
1601 case EXPR_IMPLIED_CAST
:
1602 return linearize_cast(ep
, expr
);
1605 return linearize_slice(ep
, expr
);
1607 case EXPR_INITIALIZER
:
1609 warning(expr
->pos
, "unexpected initializer expression (%d %d)", expr
->type
, expr
->op
);
1612 warning(expr
->pos
, "unknown expression (%d %d)", expr
->type
, expr
->op
);
1618 static pseudo_t
linearize_one_symbol(struct entrypoint
*ep
, struct symbol
*sym
)
1620 struct access_data ad
= { NULL
, };
1623 if (!sym
|| !sym
->initializer
|| sym
->initialized
)
1626 /* We need to output these puppies some day too.. */
1627 if (sym
->ctype
.modifiers
& (MOD_STATIC
| MOD_TOPLEVEL
))
1630 sym
->initialized
= 1;
1631 ad
.address
= symbol_pseudo(ep
, sym
);
1632 value
= linearize_initializer(ep
, sym
->initializer
, &ad
);
1633 finish_address_gen(ep
, &ad
);
1637 static pseudo_t
linearize_compound_statement(struct entrypoint
*ep
, struct statement
*stmt
)
1640 struct statement
*s
;
1641 struct symbol
*ret
= stmt
->ret
;
1644 FOR_EACH_PTR(stmt
->stmts
, s
) {
1645 pseudo
= linearize_statement(ep
, s
);
1646 } END_FOR_EACH_PTR(s
);
1649 struct basic_block
*bb
= add_label(ep
, ret
);
1650 struct instruction
*phi_node
= first_instruction(bb
->insns
);
1655 if (pseudo_list_size(phi_node
->phi_list
)==1) {
1656 pseudo
= first_pseudo(phi_node
->phi_list
);
1657 assert(pseudo
->type
== PSEUDO_PHI
);
1658 return pseudo
->def
->src1
;
1660 return phi_node
->target
;
1666 static pseudo_t
linearize_inlined_call(struct entrypoint
*ep
, struct statement
*stmt
)
1668 struct instruction
*insn
= alloc_instruction(OP_INLINED_CALL
, 0);
1669 struct statement
*args
= stmt
->args
;
1670 struct basic_block
*bb
;
1676 concat_symbol_list(args
->declaration
, &ep
->syms
);
1677 FOR_EACH_PTR(args
->declaration
, sym
) {
1678 pseudo_t value
= linearize_one_symbol(ep
, sym
);
1679 use_pseudo(insn
, value
, add_pseudo(&insn
->arguments
, value
));
1680 } END_FOR_EACH_PTR(sym
);
1683 insn
->target
= pseudo
= linearize_compound_statement(ep
, stmt
);
1684 use_pseudo(insn
, symbol_pseudo(ep
, stmt
->inline_fn
), &insn
->func
);
1686 if (bb
&& !bb
->insns
)
1687 bb
->pos
= stmt
->pos
;
1688 add_one_insn(ep
, insn
);
1692 static pseudo_t
linearize_context(struct entrypoint
*ep
, struct statement
*stmt
)
1694 struct instruction
*insn
= alloc_instruction(OP_CONTEXT
, 0);
1695 struct expression
*expr
= stmt
->expression
;
1698 if (expr
->type
== EXPR_VALUE
)
1699 value
= expr
->value
;
1701 insn
->increment
= value
;
1702 insn
->context_expr
= stmt
->context
;
1703 add_one_insn(ep
, insn
);
1707 static pseudo_t
linearize_range(struct entrypoint
*ep
, struct statement
*stmt
)
1709 struct instruction
*insn
= alloc_instruction(OP_RANGE
, 0);
1711 use_pseudo(insn
, linearize_expression(ep
, stmt
->range_expression
), &insn
->src1
);
1712 use_pseudo(insn
, linearize_expression(ep
, stmt
->range_low
), &insn
->src2
);
1713 use_pseudo(insn
, linearize_expression(ep
, stmt
->range_high
), &insn
->src3
);
1714 add_one_insn(ep
, insn
);
1718 ALLOCATOR(asm_rules
, "asm rules");
1719 ALLOCATOR(asm_constraint
, "asm constraints");
1721 static void add_asm_input(struct entrypoint
*ep
, struct instruction
*insn
, struct expression
*expr
,
1722 const char *constraint
, const struct ident
*ident
)
1724 pseudo_t pseudo
= linearize_expression(ep
, expr
);
1725 struct asm_constraint
*rule
= __alloc_asm_constraint(0);
1727 rule
->ident
= ident
;
1728 rule
->constraint
= constraint
;
1729 use_pseudo(insn
, pseudo
, &rule
->pseudo
);
1730 add_ptr_list(&insn
->asm_rules
->inputs
, rule
);
1733 static void add_asm_output(struct entrypoint
*ep
, struct instruction
*insn
, struct expression
*expr
,
1734 const char *constraint
, const struct ident
*ident
)
1736 struct access_data ad
= { NULL
, };
1737 pseudo_t pseudo
= alloc_pseudo(insn
);
1738 struct asm_constraint
*rule
;
1740 if (!expr
|| !linearize_address_gen(ep
, expr
, &ad
))
1742 linearize_store_gen(ep
, pseudo
, &ad
);
1743 finish_address_gen(ep
, &ad
);
1744 rule
= __alloc_asm_constraint(0);
1745 rule
->ident
= ident
;
1746 rule
->constraint
= constraint
;
1747 use_pseudo(insn
, pseudo
, &rule
->pseudo
);
1748 add_ptr_list(&insn
->asm_rules
->outputs
, rule
);
1751 static pseudo_t
linearize_asm_statement(struct entrypoint
*ep
, struct statement
*stmt
)
1754 struct expression
*expr
;
1755 struct instruction
*insn
;
1756 struct asm_rules
*rules
;
1757 const char *constraint
;
1758 struct ident
*ident
;
1760 insn
= alloc_instruction(OP_ASM
, 0);
1761 expr
= stmt
->asm_string
;
1762 if (!expr
|| expr
->type
!= EXPR_STRING
) {
1763 warning(stmt
->pos
, "expected string in inline asm");
1766 insn
->string
= expr
->string
->data
;
1768 rules
= __alloc_asm_rules(0);
1769 insn
->asm_rules
= rules
;
1771 /* Gather the inputs.. */
1775 FOR_EACH_PTR(stmt
->asm_inputs
, expr
) {
1777 case 0: /* Identifier */
1779 ident
= (struct ident
*)expr
;
1782 case 1: /* Constraint */
1784 constraint
= expr
? expr
->string
->data
: "";
1787 case 2: /* Expression */
1789 add_asm_input(ep
, insn
, expr
, constraint
, ident
);
1791 } END_FOR_EACH_PTR(expr
);
1793 add_one_insn(ep
, insn
);
1795 /* Assign the outputs */
1799 FOR_EACH_PTR(stmt
->asm_outputs
, expr
) {
1801 case 0: /* Identifier */
1803 ident
= (struct ident
*)expr
;
1806 case 1: /* Constraint */
1808 constraint
= expr
? expr
->string
->data
: "";
1813 add_asm_output(ep
, insn
, expr
, constraint
, ident
);
1815 } END_FOR_EACH_PTR(expr
);
1820 static int multijmp_cmp(const void *_a
, const void *_b
)
1822 const struct multijmp
*a
= _a
;
1823 const struct multijmp
*b
= _b
;
1826 if (a
->begin
> a
->end
) {
1827 if (b
->begin
> b
->end
)
1831 if (b
->begin
> b
->end
)
1833 if (a
->begin
== b
->begin
) {
1834 if (a
->end
== b
->end
)
1836 return (a
->end
< b
->end
) ? -1 : 1;
1838 return a
->begin
< b
->begin
? -1 : 1;
1841 static void sort_switch_cases(struct instruction
*insn
)
1843 sort_list((struct ptr_list
**)&insn
->multijmp_list
, multijmp_cmp
);
1846 static pseudo_t
linearize_declaration(struct entrypoint
*ep
, struct statement
*stmt
)
1850 concat_symbol_list(stmt
->declaration
, &ep
->syms
);
1852 FOR_EACH_PTR(stmt
->declaration
, sym
) {
1853 linearize_one_symbol(ep
, sym
);
1854 } END_FOR_EACH_PTR(sym
);
1858 static pseudo_t
linearize_return(struct entrypoint
*ep
, struct statement
*stmt
)
1860 struct expression
*expr
= stmt
->expression
;
1861 struct basic_block
*bb_return
= get_bound_block(ep
, stmt
->ret_target
);
1862 struct basic_block
*active
;
1863 pseudo_t src
= linearize_expression(ep
, expr
);
1864 active
= ep
->active
;
1865 if (active
&& src
!= &void_pseudo
) {
1866 struct instruction
*phi_node
= first_instruction(bb_return
->insns
);
1869 phi_node
= alloc_typed_instruction(OP_PHI
, expr
->ctype
);
1870 phi_node
->target
= alloc_pseudo(phi_node
);
1871 phi_node
->bb
= bb_return
;
1872 add_instruction(&bb_return
->insns
, phi_node
);
1874 phi
= alloc_phi(active
, src
, type_size(expr
->ctype
));
1875 phi
->ident
= &return_ident
;
1876 use_pseudo(phi_node
, phi
, add_pseudo(&phi_node
->phi_list
, phi
));
1878 add_goto(ep
, bb_return
);
1882 static pseudo_t
linearize_switch(struct entrypoint
*ep
, struct statement
*stmt
)
1885 struct instruction
*switch_ins
;
1886 struct basic_block
*switch_end
= alloc_basic_block(ep
, stmt
->pos
);
1887 struct basic_block
*active
, *default_case
;
1888 struct multijmp
*jmp
;
1891 pseudo
= linearize_expression(ep
, stmt
->switch_expression
);
1893 active
= ep
->active
;
1894 if (!bb_reachable(active
))
1897 switch_ins
= alloc_instruction(OP_SWITCH
, 0);
1898 use_pseudo(switch_ins
, pseudo
, &switch_ins
->cond
);
1899 add_one_insn(ep
, switch_ins
);
1902 default_case
= NULL
;
1903 FOR_EACH_PTR(stmt
->switch_case
->symbol_list
, sym
) {
1904 struct statement
*case_stmt
= sym
->stmt
;
1905 struct basic_block
*bb_case
= get_bound_block(ep
, sym
);
1907 if (!case_stmt
->case_expression
) {
1908 default_case
= bb_case
;
1913 begin
= end
= case_stmt
->case_expression
->value
;
1914 if (case_stmt
->case_to
)
1915 end
= case_stmt
->case_to
->value
;
1917 jmp
= alloc_multijmp(bb_case
, end
, begin
);
1919 jmp
= alloc_multijmp(bb_case
, begin
, end
);
1922 add_multijmp(&switch_ins
->multijmp_list
, jmp
);
1923 add_bb(&bb_case
->parents
, active
);
1924 add_bb(&active
->children
, bb_case
);
1925 } END_FOR_EACH_PTR(sym
);
1927 bind_label(stmt
->switch_break
, switch_end
, stmt
->pos
);
1929 /* And linearize the actual statement */
1930 linearize_statement(ep
, stmt
->switch_statement
);
1931 set_activeblock(ep
, switch_end
);
1934 default_case
= switch_end
;
1936 jmp
= alloc_multijmp(default_case
, 1, 0);
1937 add_multijmp(&switch_ins
->multijmp_list
, jmp
);
1938 add_bb(&default_case
->parents
, active
);
1939 add_bb(&active
->children
, default_case
);
1940 sort_switch_cases(switch_ins
);
1945 static pseudo_t
linearize_iterator(struct entrypoint
*ep
, struct statement
*stmt
)
1947 struct statement
*pre_statement
= stmt
->iterator_pre_statement
;
1948 struct expression
*pre_condition
= stmt
->iterator_pre_condition
;
1949 struct statement
*statement
= stmt
->iterator_statement
;
1950 struct statement
*post_statement
= stmt
->iterator_post_statement
;
1951 struct expression
*post_condition
= stmt
->iterator_post_condition
;
1952 struct basic_block
*loop_top
, *loop_body
, *loop_continue
, *loop_end
;
1955 FOR_EACH_PTR(stmt
->iterator_syms
, sym
) {
1956 linearize_one_symbol(ep
, sym
);
1957 } END_FOR_EACH_PTR(sym
);
1958 concat_symbol_list(stmt
->iterator_syms
, &ep
->syms
);
1959 linearize_statement(ep
, pre_statement
);
1961 loop_body
= loop_top
= alloc_basic_block(ep
, stmt
->pos
);
1962 loop_continue
= alloc_basic_block(ep
, stmt
->pos
);
1963 loop_end
= alloc_basic_block(ep
, stmt
->pos
);
1965 /* An empty post-condition means that it's the same as the pre-condition */
1966 if (!post_condition
) {
1967 loop_top
= alloc_basic_block(ep
, stmt
->pos
);
1968 set_activeblock(ep
, loop_top
);
1972 linearize_cond_branch(ep
, pre_condition
, loop_body
, loop_end
);
1974 bind_label(stmt
->iterator_continue
, loop_continue
, stmt
->pos
);
1975 bind_label(stmt
->iterator_break
, loop_end
, stmt
->pos
);
1977 set_activeblock(ep
, loop_body
);
1978 linearize_statement(ep
, statement
);
1979 add_goto(ep
, loop_continue
);
1981 set_activeblock(ep
, loop_continue
);
1982 linearize_statement(ep
, post_statement
);
1983 if (!post_condition
)
1984 add_goto(ep
, loop_top
);
1986 linearize_cond_branch(ep
, post_condition
, loop_top
, loop_end
);
1987 set_activeblock(ep
, loop_end
);
1992 pseudo_t
linearize_statement(struct entrypoint
*ep
, struct statement
*stmt
)
1994 struct basic_block
*bb
;
2000 if (bb
&& !bb
->insns
)
2001 bb
->pos
= stmt
->pos
;
2002 current_pos
= stmt
->pos
;
2004 switch (stmt
->type
) {
2008 case STMT_DECLARATION
:
2009 return linearize_declaration(ep
, stmt
);
2012 return linearize_context(ep
, stmt
);
2015 return linearize_range(ep
, stmt
);
2017 case STMT_EXPRESSION
:
2018 return linearize_expression(ep
, stmt
->expression
);
2021 return linearize_asm_statement(ep
, stmt
);
2024 return linearize_return(ep
, stmt
);
2027 add_label(ep
, stmt
->case_label
);
2028 linearize_statement(ep
, stmt
->case_statement
);
2033 struct symbol
*label
= stmt
->label_identifier
;
2036 add_label(ep
, label
);
2038 return linearize_statement(ep
, stmt
->label_statement
);
2043 struct expression
*expr
;
2044 struct instruction
*goto_ins
;
2045 struct basic_block
*active
;
2048 active
= ep
->active
;
2049 if (!bb_reachable(active
))
2052 if (stmt
->goto_label
) {
2053 add_goto(ep
, get_bound_block(ep
, stmt
->goto_label
));
2057 expr
= stmt
->goto_expression
;
2061 /* This can happen as part of simplification */
2062 if (expr
->type
== EXPR_LABEL
) {
2063 add_goto(ep
, get_bound_block(ep
, expr
->label_symbol
));
2067 pseudo
= linearize_expression(ep
, expr
);
2068 goto_ins
= alloc_instruction(OP_COMPUTEDGOTO
, 0);
2069 use_pseudo(goto_ins
, pseudo
, &goto_ins
->target
);
2070 add_one_insn(ep
, goto_ins
);
2072 FOR_EACH_PTR(stmt
->target_list
, sym
) {
2073 struct basic_block
*bb_computed
= get_bound_block(ep
, sym
);
2074 struct multijmp
*jmp
= alloc_multijmp(bb_computed
, 1, 0);
2075 add_multijmp(&goto_ins
->multijmp_list
, jmp
);
2076 add_bb(&bb_computed
->parents
, ep
->active
);
2077 add_bb(&active
->children
, bb_computed
);
2078 } END_FOR_EACH_PTR(sym
);
2085 if (stmt
->inline_fn
)
2086 return linearize_inlined_call(ep
, stmt
);
2087 return linearize_compound_statement(ep
, stmt
);
2090 * This could take 'likely/unlikely' into account, and
2091 * switch the arms around appropriately..
2094 struct basic_block
*bb_true
, *bb_false
, *endif
;
2095 struct expression
*cond
= stmt
->if_conditional
;
2097 bb_true
= alloc_basic_block(ep
, stmt
->pos
);
2098 bb_false
= endif
= alloc_basic_block(ep
, stmt
->pos
);
2100 linearize_cond_branch(ep
, cond
, bb_true
, bb_false
);
2102 set_activeblock(ep
, bb_true
);
2103 linearize_statement(ep
, stmt
->if_true
);
2105 if (stmt
->if_false
) {
2106 endif
= alloc_basic_block(ep
, stmt
->pos
);
2107 add_goto(ep
, endif
);
2108 set_activeblock(ep
, bb_false
);
2109 linearize_statement(ep
, stmt
->if_false
);
2111 set_activeblock(ep
, endif
);
2116 return linearize_switch(ep
, stmt
);
2119 return linearize_iterator(ep
, stmt
);
2127 static struct entrypoint
*linearize_fn(struct symbol
*sym
, struct symbol
*base_type
)
2129 struct entrypoint
*ep
;
2130 struct basic_block
*bb
;
2132 struct instruction
*entry
;
2136 if (!base_type
->stmt
)
2139 ep
= alloc_entrypoint();
2140 bb
= alloc_basic_block(ep
, sym
->pos
);
2144 set_activeblock(ep
, bb
);
2146 entry
= alloc_instruction(OP_ENTRY
, 0);
2147 add_one_insn(ep
, entry
);
2150 concat_symbol_list(base_type
->arguments
, &ep
->syms
);
2152 /* FIXME!! We should do something else about varargs.. */
2154 FOR_EACH_PTR(base_type
->arguments
, arg
) {
2155 linearize_argument(ep
, arg
, ++i
);
2156 } END_FOR_EACH_PTR(arg
);
2158 result
= linearize_statement(ep
, base_type
->stmt
);
2159 if (bb_reachable(ep
->active
) && !bb_terminated(ep
->active
)) {
2160 struct symbol
*ret_type
= base_type
->ctype
.base_type
;
2161 struct instruction
*insn
= alloc_typed_instruction(OP_RET
, ret_type
);
2163 if (type_size(ret_type
) > 0)
2164 use_pseudo(insn
, result
, &insn
->src
);
2165 add_one_insn(ep
, insn
);
2169 * Do trivial flow simplification - branches to
2170 * branches, kill dead basicblocks etc
2172 kill_unreachable_bbs(ep
);
2175 * Turn symbols into pseudos
2177 simplify_symbol_usage(ep
);
2181 * Remove trivial instructions, and try to CSE
2185 cleanup_and_cse(ep
);
2186 pack_basic_blocks(ep
);
2187 } while (repeat_phase
& REPEAT_CSE
);
2189 kill_unreachable_bbs(ep
);
2193 clear_symbol_pseudos(ep
);
2195 /* And track pseudo register usage */
2196 track_pseudo_liveness(ep
);
2199 * Some flow optimizations can only effectively
2200 * be done when we've done liveness analysis. But
2201 * if they trigger, we need to start all over
2204 if (simplify_flow(ep
)) {
2209 /* Finally, add deathnotes to pseudos now that we have them */
2211 track_pseudo_death(ep
);
2216 struct entrypoint
*linearize_symbol(struct symbol
*sym
)
2218 struct symbol
*base_type
;
2222 current_pos
= sym
->pos
;
2223 base_type
= sym
->ctype
.base_type
;
2226 if (base_type
->type
== SYM_FN
)
2227 return linearize_fn(sym
, base_type
);