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
= src
;
1109 if (base
->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
;
1161 value
= linearize_expression(ep
, src
);
1162 if (!target
|| !linearize_address_gen(ep
, target
, &ad
))
1164 if (expr
->op
!= '=') {
1165 pseudo_t oldvalue
= linearize_load_gen(ep
, &ad
);
1167 static const int op_trans
[] = {
1168 [SPECIAL_ADD_ASSIGN
- SPECIAL_BASE
] = OP_ADD
,
1169 [SPECIAL_SUB_ASSIGN
- SPECIAL_BASE
] = OP_SUB
,
1170 [SPECIAL_MUL_ASSIGN
- SPECIAL_BASE
] = OP_MULU
,
1171 [SPECIAL_DIV_ASSIGN
- SPECIAL_BASE
] = OP_DIVU
,
1172 [SPECIAL_MOD_ASSIGN
- SPECIAL_BASE
] = OP_MODU
,
1173 [SPECIAL_SHL_ASSIGN
- SPECIAL_BASE
] = OP_SHL
,
1174 [SPECIAL_SHR_ASSIGN
- SPECIAL_BASE
] = OP_LSR
,
1175 [SPECIAL_AND_ASSIGN
- SPECIAL_BASE
] = OP_AND
,
1176 [SPECIAL_OR_ASSIGN
- SPECIAL_BASE
] = OP_OR
,
1177 [SPECIAL_XOR_ASSIGN
- SPECIAL_BASE
] = OP_XOR
1184 oldvalue
= cast_pseudo(ep
, oldvalue
, src
->ctype
, expr
->ctype
);
1185 opcode
= opcode_sign(op_trans
[expr
->op
- SPECIAL_BASE
], src
->ctype
);
1186 dst
= add_binary_op(ep
, src
->ctype
, opcode
, oldvalue
, value
);
1187 value
= cast_pseudo(ep
, dst
, expr
->ctype
, src
->ctype
);
1189 value
= linearize_store_gen(ep
, value
, &ad
);
1190 finish_address_gen(ep
, &ad
);
1194 static pseudo_t
linearize_call_expression(struct entrypoint
*ep
, struct expression
*expr
)
1196 struct expression
*arg
, *fn
;
1197 struct instruction
*insn
= alloc_typed_instruction(OP_CALL
, expr
->ctype
);
1198 pseudo_t retval
, call
;
1199 struct ctype
*ctype
= NULL
;
1200 struct symbol
*fntype
;
1201 struct context
*context
;
1204 warning(expr
->pos
, "call with no type!");
1208 FOR_EACH_PTR(expr
->args
, arg
) {
1209 pseudo_t
new = linearize_expression(ep
, arg
);
1210 use_pseudo(insn
, new, add_pseudo(&insn
->arguments
, new));
1211 } END_FOR_EACH_PTR(arg
);
1216 ctype
= &fn
->ctype
->ctype
;
1220 if (fntype
->type
== SYM_NODE
)
1221 fntype
= fntype
->ctype
.base_type
;
1223 insn
->fntype
= fntype
;
1225 if (fn
->type
== EXPR_PREOP
) {
1226 if (fn
->unop
->type
== EXPR_SYMBOL
) {
1227 struct symbol
*sym
= fn
->unop
->symbol
;
1228 if (sym
->ctype
.base_type
->type
== SYM_FN
)
1232 if (fn
->type
== EXPR_SYMBOL
) {
1233 call
= symbol_pseudo(ep
, fn
->symbol
);
1235 call
= linearize_expression(ep
, fn
);
1237 use_pseudo(insn
, call
, &insn
->func
);
1239 if (expr
->ctype
!= &void_ctype
)
1240 retval
= alloc_pseudo(insn
);
1241 insn
->target
= retval
;
1242 add_one_insn(ep
, insn
);
1245 FOR_EACH_PTR(ctype
->contexts
, context
) {
1246 int in
= context
->in
;
1247 int out
= context
->out
;
1258 context_diff
= out
- in
;
1259 if (check
|| context_diff
) {
1260 insn
= alloc_instruction(OP_CONTEXT
, 0);
1261 insn
->increment
= context_diff
;
1262 insn
->check
= check
;
1263 insn
->context_expr
= context
->context
;
1264 add_one_insn(ep
, insn
);
1266 } END_FOR_EACH_PTR(context
);
1272 static pseudo_t
linearize_binop(struct entrypoint
*ep
, struct expression
*expr
)
1274 pseudo_t src1
, src2
, dst
;
1275 static const int opcode
[] = {
1276 ['+'] = OP_ADD
, ['-'] = OP_SUB
,
1277 ['*'] = OP_MULU
, ['/'] = OP_DIVU
,
1278 ['%'] = OP_MODU
, ['&'] = OP_AND
,
1279 ['|'] = OP_OR
, ['^'] = OP_XOR
,
1280 [SPECIAL_LEFTSHIFT
] = OP_SHL
,
1281 [SPECIAL_RIGHTSHIFT
] = OP_LSR
,
1282 [SPECIAL_LOGICAL_AND
] = OP_AND_BOOL
,
1283 [SPECIAL_LOGICAL_OR
] = OP_OR_BOOL
,
1287 src1
= linearize_expression(ep
, expr
->left
);
1288 src2
= linearize_expression(ep
, expr
->right
);
1289 op
= opcode_sign(opcode
[expr
->op
], expr
->ctype
);
1290 dst
= add_binary_op(ep
, expr
->ctype
, op
, src1
, src2
);
1294 static pseudo_t
linearize_logical_branch(struct entrypoint
*ep
, struct expression
*expr
, struct basic_block
*bb_true
, struct basic_block
*bb_false
);
1296 pseudo_t
linearize_cond_branch(struct entrypoint
*ep
, struct expression
*expr
, struct basic_block
*bb_true
, struct basic_block
*bb_false
);
1298 static pseudo_t
linearize_select(struct entrypoint
*ep
, struct expression
*expr
)
1300 pseudo_t cond
, true, false, res
;
1301 struct instruction
*insn
;
1303 true = linearize_expression(ep
, expr
->cond_true
);
1304 false = linearize_expression(ep
, expr
->cond_false
);
1305 cond
= linearize_expression(ep
, expr
->conditional
);
1307 insn
= alloc_typed_instruction(OP_SEL
, expr
->ctype
);
1308 if (!expr
->cond_true
)
1310 use_pseudo(insn
, cond
, &insn
->src1
);
1311 use_pseudo(insn
, true, &insn
->src2
);
1312 use_pseudo(insn
, false, &insn
->src3
);
1314 res
= alloc_pseudo(insn
);
1316 add_one_insn(ep
, insn
);
1320 static pseudo_t
add_join_conditional(struct entrypoint
*ep
, struct expression
*expr
,
1321 pseudo_t phi1
, pseudo_t phi2
)
1324 struct instruction
*phi_node
;
1331 phi_node
= alloc_typed_instruction(OP_PHI
, expr
->ctype
);
1332 use_pseudo(phi_node
, phi1
, add_pseudo(&phi_node
->phi_list
, phi1
));
1333 use_pseudo(phi_node
, phi2
, add_pseudo(&phi_node
->phi_list
, phi2
));
1334 phi_node
->target
= target
= alloc_pseudo(phi_node
);
1335 add_one_insn(ep
, phi_node
);
1339 static pseudo_t
linearize_short_conditional(struct entrypoint
*ep
, struct expression
*expr
,
1340 struct expression
*cond
,
1341 struct expression
*expr_false
)
1343 pseudo_t src1
, src2
;
1344 struct basic_block
*bb_false
;
1345 struct basic_block
*merge
= alloc_basic_block(ep
, expr
->pos
);
1346 pseudo_t phi1
, phi2
;
1347 int size
= type_size(expr
->ctype
);
1349 if (!expr_false
|| !ep
->active
)
1352 bb_false
= alloc_basic_block(ep
, expr_false
->pos
);
1353 src1
= linearize_expression(ep
, cond
);
1354 phi1
= alloc_phi(ep
->active
, src1
, size
);
1355 add_branch(ep
, expr
, src1
, merge
, bb_false
);
1357 set_activeblock(ep
, bb_false
);
1358 src2
= linearize_expression(ep
, expr_false
);
1359 phi2
= alloc_phi(ep
->active
, src2
, size
);
1360 set_activeblock(ep
, merge
);
1362 return add_join_conditional(ep
, expr
, phi1
, phi2
);
1365 static pseudo_t
linearize_conditional(struct entrypoint
*ep
, struct expression
*expr
,
1366 struct expression
*cond
,
1367 struct expression
*expr_true
,
1368 struct expression
*expr_false
)
1370 pseudo_t src1
, src2
;
1371 pseudo_t phi1
, phi2
;
1372 struct basic_block
*bb_true
, *bb_false
, *merge
;
1373 int size
= type_size(expr
->ctype
);
1375 if (!cond
|| !expr_true
|| !expr_false
|| !ep
->active
)
1377 bb_true
= alloc_basic_block(ep
, expr_true
->pos
);
1378 bb_false
= alloc_basic_block(ep
, expr_false
->pos
);
1379 merge
= alloc_basic_block(ep
, expr
->pos
);
1381 linearize_cond_branch(ep
, cond
, bb_true
, bb_false
);
1383 set_activeblock(ep
, bb_true
);
1384 src1
= linearize_expression(ep
, expr_true
);
1385 phi1
= alloc_phi(ep
->active
, src1
, size
);
1386 add_goto(ep
, merge
);
1388 set_activeblock(ep
, bb_false
);
1389 src2
= linearize_expression(ep
, expr_false
);
1390 phi2
= alloc_phi(ep
->active
, src2
, size
);
1391 set_activeblock(ep
, merge
);
1393 return add_join_conditional(ep
, expr
, phi1
, phi2
);
1396 static pseudo_t
linearize_logical(struct entrypoint
*ep
, struct expression
*expr
)
1398 struct expression
*shortcut
;
1400 shortcut
= alloc_const_expression(expr
->pos
, expr
->op
== SPECIAL_LOGICAL_OR
);
1401 shortcut
->ctype
= expr
->ctype
;
1402 if (expr
->op
== SPECIAL_LOGICAL_OR
)
1403 return linearize_conditional(ep
, expr
, expr
->left
, shortcut
, expr
->right
);
1404 return linearize_conditional(ep
, expr
, expr
->left
, expr
->right
, shortcut
);
1407 static pseudo_t
linearize_compare(struct entrypoint
*ep
, struct expression
*expr
)
1409 static const int cmpop
[] = {
1410 ['>'] = OP_SET_GT
, ['<'] = OP_SET_LT
,
1411 [SPECIAL_EQUAL
] = OP_SET_EQ
,
1412 [SPECIAL_NOTEQUAL
] = OP_SET_NE
,
1413 [SPECIAL_GTE
] = OP_SET_GE
,
1414 [SPECIAL_LTE
] = OP_SET_LE
,
1415 [SPECIAL_UNSIGNED_LT
] = OP_SET_B
,
1416 [SPECIAL_UNSIGNED_GT
] = OP_SET_A
,
1417 [SPECIAL_UNSIGNED_LTE
] = OP_SET_BE
,
1418 [SPECIAL_UNSIGNED_GTE
] = OP_SET_AE
,
1421 pseudo_t src1
= linearize_expression(ep
, expr
->left
);
1422 pseudo_t src2
= linearize_expression(ep
, expr
->right
);
1423 pseudo_t dst
= add_binary_op(ep
, expr
->ctype
, cmpop
[expr
->op
], src1
, src2
);
1428 pseudo_t
linearize_cond_branch(struct entrypoint
*ep
, struct expression
*expr
, struct basic_block
*bb_true
, struct basic_block
*bb_false
)
1432 if (!expr
|| !bb_reachable(ep
->active
))
1435 switch (expr
->type
) {
1439 add_goto(ep
, expr
->value
? bb_true
: bb_false
);
1443 add_goto(ep
, expr
->fvalue
? bb_true
: bb_false
);
1447 linearize_logical_branch(ep
, expr
, bb_true
, bb_false
);
1451 cond
= linearize_compare(ep
, expr
);
1452 add_branch(ep
, expr
, cond
, bb_true
, bb_false
);
1456 if (expr
->op
== '!')
1457 return linearize_cond_branch(ep
, expr
->unop
, bb_false
, bb_true
);
1460 cond
= linearize_expression(ep
, expr
);
1461 add_branch(ep
, expr
, cond
, bb_true
, bb_false
);
1471 static pseudo_t
linearize_logical_branch(struct entrypoint
*ep
, struct expression
*expr
, struct basic_block
*bb_true
, struct basic_block
*bb_false
)
1473 struct basic_block
*next
= alloc_basic_block(ep
, expr
->pos
);
1475 if (expr
->op
== SPECIAL_LOGICAL_OR
)
1476 linearize_cond_branch(ep
, expr
->left
, bb_true
, next
);
1478 linearize_cond_branch(ep
, expr
->left
, next
, bb_false
);
1479 set_activeblock(ep
, next
);
1480 linearize_cond_branch(ep
, expr
->right
, bb_true
, bb_false
);
1484 static pseudo_t
linearize_cast(struct entrypoint
*ep
, struct expression
*expr
)
1487 struct expression
*orig
= expr
->cast_expression
;
1492 src
= linearize_expression(ep
, orig
);
1493 return cast_pseudo(ep
, src
, orig
->ctype
, expr
->ctype
);
1496 static pseudo_t
linearize_position(struct entrypoint
*ep
, struct expression
*pos
, struct access_data
*ad
)
1498 struct expression
*init_expr
= pos
->init_expr
;
1500 ad
->offset
= pos
->init_offset
;
1501 ad
->source_type
= base_type(init_expr
->ctype
);
1502 ad
->result_type
= init_expr
->ctype
;
1503 return linearize_initializer(ep
, init_expr
, ad
);
1506 static pseudo_t
linearize_initializer(struct entrypoint
*ep
, struct expression
*initializer
, struct access_data
*ad
)
1508 switch (initializer
->type
) {
1509 case EXPR_INITIALIZER
: {
1510 struct expression
*expr
;
1511 FOR_EACH_PTR(initializer
->expr_list
, expr
) {
1512 linearize_initializer(ep
, expr
, ad
);
1513 } END_FOR_EACH_PTR(expr
);
1517 linearize_position(ep
, initializer
, ad
);
1520 pseudo_t value
= linearize_expression(ep
, initializer
);
1521 ad
->source_type
= base_type(initializer
->ctype
);
1522 ad
->result_type
= initializer
->ctype
;
1523 linearize_store_gen(ep
, value
, ad
);
1531 static void linearize_argument(struct entrypoint
*ep
, struct symbol
*arg
, int nr
)
1533 struct access_data ad
= { NULL
, };
1535 ad
.source_type
= arg
;
1536 ad
.result_type
= arg
;
1537 ad
.address
= symbol_pseudo(ep
, arg
);
1538 linearize_store_gen(ep
, argument_pseudo(ep
, nr
), &ad
);
1539 finish_address_gen(ep
, &ad
);
1542 pseudo_t
linearize_expression(struct entrypoint
*ep
, struct expression
*expr
)
1547 current_pos
= expr
->pos
;
1548 switch (expr
->type
) {
1550 linearize_one_symbol(ep
, expr
->symbol
);
1551 return add_symbol_address(ep
, expr
->symbol
);
1554 return value_pseudo(expr
->value
);
1556 case EXPR_STRING
: case EXPR_FVALUE
: case EXPR_LABEL
:
1557 return add_setval(ep
, expr
->ctype
, expr
);
1559 case EXPR_STATEMENT
:
1560 return linearize_statement(ep
, expr
->statement
);
1563 return linearize_call_expression(ep
, expr
);
1566 return linearize_binop(ep
, expr
);
1569 return linearize_logical(ep
, expr
);
1572 return linearize_compare(ep
, expr
);
1575 return linearize_select(ep
, expr
);
1577 case EXPR_CONDITIONAL
:
1578 if (!expr
->cond_true
)
1579 return linearize_short_conditional(ep
, expr
, expr
->conditional
, expr
->cond_false
);
1581 return linearize_conditional(ep
, expr
, expr
->conditional
,
1582 expr
->cond_true
, expr
->cond_false
);
1585 linearize_expression(ep
, expr
->left
);
1586 return linearize_expression(ep
, expr
->right
);
1588 case EXPR_ASSIGNMENT
:
1589 return linearize_assignment(ep
, expr
);
1592 return linearize_preop(ep
, expr
);
1595 return linearize_postop(ep
, expr
);
1598 case EXPR_FORCE_CAST
:
1599 case EXPR_IMPLIED_CAST
:
1600 return linearize_cast(ep
, expr
);
1603 return linearize_slice(ep
, expr
);
1605 case EXPR_INITIALIZER
:
1607 warning(expr
->pos
, "unexpected initializer expression (%d %d)", expr
->type
, expr
->op
);
1610 warning(expr
->pos
, "unknown expression (%d %d)", expr
->type
, expr
->op
);
1616 static pseudo_t
linearize_one_symbol(struct entrypoint
*ep
, struct symbol
*sym
)
1618 struct access_data ad
= { NULL
, };
1621 if (!sym
|| !sym
->initializer
|| sym
->initialized
)
1624 /* We need to output these puppies some day too.. */
1625 if (sym
->ctype
.modifiers
& (MOD_STATIC
| MOD_TOPLEVEL
))
1628 sym
->initialized
= 1;
1629 ad
.address
= symbol_pseudo(ep
, sym
);
1630 value
= linearize_initializer(ep
, sym
->initializer
, &ad
);
1631 finish_address_gen(ep
, &ad
);
1635 static pseudo_t
linearize_compound_statement(struct entrypoint
*ep
, struct statement
*stmt
)
1638 struct statement
*s
;
1639 struct symbol
*ret
= stmt
->ret
;
1642 FOR_EACH_PTR(stmt
->stmts
, s
) {
1643 pseudo
= linearize_statement(ep
, s
);
1644 } END_FOR_EACH_PTR(s
);
1647 struct basic_block
*bb
= add_label(ep
, ret
);
1648 struct instruction
*phi_node
= first_instruction(bb
->insns
);
1653 if (pseudo_list_size(phi_node
->phi_list
)==1) {
1654 pseudo
= first_pseudo(phi_node
->phi_list
);
1655 assert(pseudo
->type
== PSEUDO_PHI
);
1656 return pseudo
->def
->src1
;
1658 return phi_node
->target
;
1664 static pseudo_t
linearize_inlined_call(struct entrypoint
*ep
, struct statement
*stmt
)
1666 struct instruction
*insn
= alloc_instruction(OP_INLINED_CALL
, 0);
1667 struct statement
*args
= stmt
->args
;
1668 struct basic_block
*bb
;
1674 concat_symbol_list(args
->declaration
, &ep
->syms
);
1675 FOR_EACH_PTR(args
->declaration
, sym
) {
1676 pseudo_t value
= linearize_one_symbol(ep
, sym
);
1677 use_pseudo(insn
, value
, add_pseudo(&insn
->arguments
, value
));
1678 } END_FOR_EACH_PTR(sym
);
1681 insn
->target
= pseudo
= linearize_compound_statement(ep
, stmt
);
1682 use_pseudo(insn
, symbol_pseudo(ep
, stmt
->inline_fn
), &insn
->func
);
1684 if (bb
&& !bb
->insns
)
1685 bb
->pos
= stmt
->pos
;
1686 add_one_insn(ep
, insn
);
1690 static pseudo_t
linearize_context(struct entrypoint
*ep
, struct statement
*stmt
)
1692 struct instruction
*insn
= alloc_instruction(OP_CONTEXT
, 0);
1693 struct expression
*expr
= stmt
->expression
;
1696 if (expr
->type
== EXPR_VALUE
)
1697 value
= expr
->value
;
1699 insn
->increment
= value
;
1700 insn
->context_expr
= stmt
->context
;
1701 add_one_insn(ep
, insn
);
1705 static pseudo_t
linearize_range(struct entrypoint
*ep
, struct statement
*stmt
)
1707 struct instruction
*insn
= alloc_instruction(OP_RANGE
, 0);
1709 use_pseudo(insn
, linearize_expression(ep
, stmt
->range_expression
), &insn
->src1
);
1710 use_pseudo(insn
, linearize_expression(ep
, stmt
->range_low
), &insn
->src2
);
1711 use_pseudo(insn
, linearize_expression(ep
, stmt
->range_high
), &insn
->src3
);
1712 add_one_insn(ep
, insn
);
1716 ALLOCATOR(asm_rules
, "asm rules");
1717 ALLOCATOR(asm_constraint
, "asm constraints");
1719 static void add_asm_input(struct entrypoint
*ep
, struct instruction
*insn
, struct expression
*expr
,
1720 const char *constraint
, const struct ident
*ident
)
1722 pseudo_t pseudo
= linearize_expression(ep
, expr
);
1723 struct asm_constraint
*rule
= __alloc_asm_constraint(0);
1725 rule
->ident
= ident
;
1726 rule
->constraint
= constraint
;
1727 use_pseudo(insn
, pseudo
, &rule
->pseudo
);
1728 add_ptr_list(&insn
->asm_rules
->inputs
, rule
);
1731 static void add_asm_output(struct entrypoint
*ep
, struct instruction
*insn
, struct expression
*expr
,
1732 const char *constraint
, const struct ident
*ident
)
1734 struct access_data ad
= { NULL
, };
1735 pseudo_t pseudo
= alloc_pseudo(insn
);
1736 struct asm_constraint
*rule
;
1738 if (!expr
|| !linearize_address_gen(ep
, expr
, &ad
))
1740 linearize_store_gen(ep
, pseudo
, &ad
);
1741 finish_address_gen(ep
, &ad
);
1742 rule
= __alloc_asm_constraint(0);
1743 rule
->ident
= ident
;
1744 rule
->constraint
= constraint
;
1745 use_pseudo(insn
, pseudo
, &rule
->pseudo
);
1746 add_ptr_list(&insn
->asm_rules
->outputs
, rule
);
1749 static pseudo_t
linearize_asm_statement(struct entrypoint
*ep
, struct statement
*stmt
)
1752 struct expression
*expr
;
1753 struct instruction
*insn
;
1754 struct asm_rules
*rules
;
1755 const char *constraint
;
1756 struct ident
*ident
;
1758 insn
= alloc_instruction(OP_ASM
, 0);
1759 expr
= stmt
->asm_string
;
1760 if (!expr
|| expr
->type
!= EXPR_STRING
) {
1761 warning(stmt
->pos
, "expected string in inline asm");
1764 insn
->string
= expr
->string
->data
;
1766 rules
= __alloc_asm_rules(0);
1767 insn
->asm_rules
= rules
;
1769 /* Gather the inputs.. */
1773 FOR_EACH_PTR(stmt
->asm_inputs
, expr
) {
1775 case 0: /* Identifier */
1777 ident
= (struct ident
*)expr
;
1780 case 1: /* Constraint */
1782 constraint
= expr
? expr
->string
->data
: "";
1785 case 2: /* Expression */
1787 add_asm_input(ep
, insn
, expr
, constraint
, ident
);
1789 } END_FOR_EACH_PTR(expr
);
1791 add_one_insn(ep
, insn
);
1793 /* Assign the outputs */
1797 FOR_EACH_PTR(stmt
->asm_outputs
, expr
) {
1799 case 0: /* Identifier */
1801 ident
= (struct ident
*)expr
;
1804 case 1: /* Constraint */
1806 constraint
= expr
? expr
->string
->data
: "";
1811 add_asm_output(ep
, insn
, expr
, constraint
, ident
);
1813 } END_FOR_EACH_PTR(expr
);
1818 static int multijmp_cmp(const void *_a
, const void *_b
)
1820 const struct multijmp
*a
= _a
;
1821 const struct multijmp
*b
= _b
;
1824 if (a
->begin
> a
->end
) {
1825 if (b
->begin
> b
->end
)
1829 if (b
->begin
> b
->end
)
1831 if (a
->begin
== b
->begin
) {
1832 if (a
->end
== b
->end
)
1834 return (a
->end
< b
->end
) ? -1 : 1;
1836 return a
->begin
< b
->begin
? -1 : 1;
1839 static void sort_switch_cases(struct instruction
*insn
)
1841 sort_list((struct ptr_list
**)&insn
->multijmp_list
, multijmp_cmp
);
1844 static pseudo_t
linearize_declaration(struct entrypoint
*ep
, struct statement
*stmt
)
1848 concat_symbol_list(stmt
->declaration
, &ep
->syms
);
1850 FOR_EACH_PTR(stmt
->declaration
, sym
) {
1851 linearize_one_symbol(ep
, sym
);
1852 } END_FOR_EACH_PTR(sym
);
1856 static pseudo_t
linearize_return(struct entrypoint
*ep
, struct statement
*stmt
)
1858 struct expression
*expr
= stmt
->expression
;
1859 struct basic_block
*bb_return
= get_bound_block(ep
, stmt
->ret_target
);
1860 struct basic_block
*active
;
1861 pseudo_t src
= linearize_expression(ep
, expr
);
1862 active
= ep
->active
;
1863 if (active
&& src
!= &void_pseudo
) {
1864 struct instruction
*phi_node
= first_instruction(bb_return
->insns
);
1867 phi_node
= alloc_typed_instruction(OP_PHI
, expr
->ctype
);
1868 phi_node
->target
= alloc_pseudo(phi_node
);
1869 phi_node
->bb
= bb_return
;
1870 add_instruction(&bb_return
->insns
, phi_node
);
1872 phi
= alloc_phi(active
, src
, type_size(expr
->ctype
));
1873 phi
->ident
= &return_ident
;
1874 use_pseudo(phi_node
, phi
, add_pseudo(&phi_node
->phi_list
, phi
));
1876 add_goto(ep
, bb_return
);
1880 static pseudo_t
linearize_switch(struct entrypoint
*ep
, struct statement
*stmt
)
1883 struct instruction
*switch_ins
;
1884 struct basic_block
*switch_end
= alloc_basic_block(ep
, stmt
->pos
);
1885 struct basic_block
*active
, *default_case
;
1886 struct multijmp
*jmp
;
1889 pseudo
= linearize_expression(ep
, stmt
->switch_expression
);
1891 active
= ep
->active
;
1892 if (!bb_reachable(active
))
1895 switch_ins
= alloc_instruction(OP_SWITCH
, 0);
1896 use_pseudo(switch_ins
, pseudo
, &switch_ins
->cond
);
1897 add_one_insn(ep
, switch_ins
);
1900 default_case
= NULL
;
1901 FOR_EACH_PTR(stmt
->switch_case
->symbol_list
, sym
) {
1902 struct statement
*case_stmt
= sym
->stmt
;
1903 struct basic_block
*bb_case
= get_bound_block(ep
, sym
);
1905 if (!case_stmt
->case_expression
) {
1906 default_case
= bb_case
;
1911 begin
= end
= case_stmt
->case_expression
->value
;
1912 if (case_stmt
->case_to
)
1913 end
= case_stmt
->case_to
->value
;
1915 jmp
= alloc_multijmp(bb_case
, end
, begin
);
1917 jmp
= alloc_multijmp(bb_case
, begin
, end
);
1920 add_multijmp(&switch_ins
->multijmp_list
, jmp
);
1921 add_bb(&bb_case
->parents
, active
);
1922 add_bb(&active
->children
, bb_case
);
1923 } END_FOR_EACH_PTR(sym
);
1925 bind_label(stmt
->switch_break
, switch_end
, stmt
->pos
);
1927 /* And linearize the actual statement */
1928 linearize_statement(ep
, stmt
->switch_statement
);
1929 set_activeblock(ep
, switch_end
);
1932 default_case
= switch_end
;
1934 jmp
= alloc_multijmp(default_case
, 1, 0);
1935 add_multijmp(&switch_ins
->multijmp_list
, jmp
);
1936 add_bb(&default_case
->parents
, active
);
1937 add_bb(&active
->children
, default_case
);
1938 sort_switch_cases(switch_ins
);
1943 static pseudo_t
linearize_iterator(struct entrypoint
*ep
, struct statement
*stmt
)
1945 struct statement
*pre_statement
= stmt
->iterator_pre_statement
;
1946 struct expression
*pre_condition
= stmt
->iterator_pre_condition
;
1947 struct statement
*statement
= stmt
->iterator_statement
;
1948 struct statement
*post_statement
= stmt
->iterator_post_statement
;
1949 struct expression
*post_condition
= stmt
->iterator_post_condition
;
1950 struct basic_block
*loop_top
, *loop_body
, *loop_continue
, *loop_end
;
1953 FOR_EACH_PTR(stmt
->iterator_syms
, sym
) {
1954 linearize_one_symbol(ep
, sym
);
1955 } END_FOR_EACH_PTR(sym
);
1956 concat_symbol_list(stmt
->iterator_syms
, &ep
->syms
);
1957 linearize_statement(ep
, pre_statement
);
1959 loop_body
= loop_top
= alloc_basic_block(ep
, stmt
->pos
);
1960 loop_continue
= alloc_basic_block(ep
, stmt
->pos
);
1961 loop_end
= alloc_basic_block(ep
, stmt
->pos
);
1963 /* An empty post-condition means that it's the same as the pre-condition */
1964 if (!post_condition
) {
1965 loop_top
= alloc_basic_block(ep
, stmt
->pos
);
1966 set_activeblock(ep
, loop_top
);
1970 linearize_cond_branch(ep
, pre_condition
, loop_body
, loop_end
);
1972 bind_label(stmt
->iterator_continue
, loop_continue
, stmt
->pos
);
1973 bind_label(stmt
->iterator_break
, loop_end
, stmt
->pos
);
1975 set_activeblock(ep
, loop_body
);
1976 linearize_statement(ep
, statement
);
1977 add_goto(ep
, loop_continue
);
1979 set_activeblock(ep
, loop_continue
);
1980 linearize_statement(ep
, post_statement
);
1981 if (!post_condition
)
1982 add_goto(ep
, loop_top
);
1984 linearize_cond_branch(ep
, post_condition
, loop_top
, loop_end
);
1985 set_activeblock(ep
, loop_end
);
1990 pseudo_t
linearize_statement(struct entrypoint
*ep
, struct statement
*stmt
)
1992 struct basic_block
*bb
;
1998 if (bb
&& !bb
->insns
)
1999 bb
->pos
= stmt
->pos
;
2000 current_pos
= stmt
->pos
;
2002 switch (stmt
->type
) {
2006 case STMT_DECLARATION
:
2007 return linearize_declaration(ep
, stmt
);
2010 return linearize_context(ep
, stmt
);
2013 return linearize_range(ep
, stmt
);
2015 case STMT_EXPRESSION
:
2016 return linearize_expression(ep
, stmt
->expression
);
2019 return linearize_asm_statement(ep
, stmt
);
2022 return linearize_return(ep
, stmt
);
2025 add_label(ep
, stmt
->case_label
);
2026 linearize_statement(ep
, stmt
->case_statement
);
2031 struct symbol
*label
= stmt
->label_identifier
;
2034 add_label(ep
, label
);
2036 linearize_statement(ep
, stmt
->label_statement
);
2042 struct expression
*expr
;
2043 struct instruction
*goto_ins
;
2044 struct basic_block
*active
;
2047 active
= ep
->active
;
2048 if (!bb_reachable(active
))
2051 if (stmt
->goto_label
) {
2052 add_goto(ep
, get_bound_block(ep
, stmt
->goto_label
));
2056 expr
= stmt
->goto_expression
;
2060 /* This can happen as part of simplification */
2061 if (expr
->type
== EXPR_LABEL
) {
2062 add_goto(ep
, get_bound_block(ep
, expr
->label_symbol
));
2066 pseudo
= linearize_expression(ep
, expr
);
2067 goto_ins
= alloc_instruction(OP_COMPUTEDGOTO
, 0);
2068 use_pseudo(goto_ins
, pseudo
, &goto_ins
->target
);
2069 add_one_insn(ep
, goto_ins
);
2071 FOR_EACH_PTR(stmt
->target_list
, sym
) {
2072 struct basic_block
*bb_computed
= get_bound_block(ep
, sym
);
2073 struct multijmp
*jmp
= alloc_multijmp(bb_computed
, 1, 0);
2074 add_multijmp(&goto_ins
->multijmp_list
, jmp
);
2075 add_bb(&bb_computed
->parents
, ep
->active
);
2076 add_bb(&active
->children
, bb_computed
);
2077 } END_FOR_EACH_PTR(sym
);
2084 if (stmt
->inline_fn
)
2085 return linearize_inlined_call(ep
, stmt
);
2086 return linearize_compound_statement(ep
, stmt
);
2089 * This could take 'likely/unlikely' into account, and
2090 * switch the arms around appropriately..
2093 struct basic_block
*bb_true
, *bb_false
, *endif
;
2094 struct expression
*cond
= stmt
->if_conditional
;
2096 bb_true
= alloc_basic_block(ep
, stmt
->pos
);
2097 bb_false
= endif
= alloc_basic_block(ep
, stmt
->pos
);
2099 linearize_cond_branch(ep
, cond
, bb_true
, bb_false
);
2101 set_activeblock(ep
, bb_true
);
2102 linearize_statement(ep
, stmt
->if_true
);
2104 if (stmt
->if_false
) {
2105 endif
= alloc_basic_block(ep
, stmt
->pos
);
2106 add_goto(ep
, endif
);
2107 set_activeblock(ep
, bb_false
);
2108 linearize_statement(ep
, stmt
->if_false
);
2110 set_activeblock(ep
, endif
);
2115 return linearize_switch(ep
, stmt
);
2118 return linearize_iterator(ep
, stmt
);
2126 static struct entrypoint
*linearize_fn(struct symbol
*sym
, struct symbol
*base_type
)
2128 struct entrypoint
*ep
;
2129 struct basic_block
*bb
;
2131 struct instruction
*entry
;
2135 if (!base_type
->stmt
)
2138 ep
= alloc_entrypoint();
2139 bb
= alloc_basic_block(ep
, sym
->pos
);
2143 set_activeblock(ep
, bb
);
2145 entry
= alloc_instruction(OP_ENTRY
, 0);
2146 add_one_insn(ep
, entry
);
2149 concat_symbol_list(base_type
->arguments
, &ep
->syms
);
2151 /* FIXME!! We should do something else about varargs.. */
2153 FOR_EACH_PTR(base_type
->arguments
, arg
) {
2154 linearize_argument(ep
, arg
, ++i
);
2155 } END_FOR_EACH_PTR(arg
);
2157 result
= linearize_statement(ep
, base_type
->stmt
);
2158 if (bb_reachable(ep
->active
) && !bb_terminated(ep
->active
)) {
2159 struct symbol
*ret_type
= base_type
->ctype
.base_type
;
2160 struct instruction
*insn
= alloc_typed_instruction(OP_RET
, ret_type
);
2162 if (type_size(ret_type
) > 0)
2163 use_pseudo(insn
, result
, &insn
->src
);
2164 add_one_insn(ep
, insn
);
2168 * Do trivial flow simplification - branches to
2169 * branches, kill dead basicblocks etc
2171 kill_unreachable_bbs(ep
);
2174 * Turn symbols into pseudos
2176 simplify_symbol_usage(ep
);
2180 * Remove trivial instructions, and try to CSE
2184 cleanup_and_cse(ep
);
2185 pack_basic_blocks(ep
);
2186 } while (repeat_phase
& REPEAT_CSE
);
2188 kill_unreachable_bbs(ep
);
2192 clear_symbol_pseudos(ep
);
2194 /* And track pseudo register usage */
2195 track_pseudo_liveness(ep
);
2198 * Some flow optimizations can only effectively
2199 * be done when we've done liveness analysis. But
2200 * if they trigger, we need to start all over
2203 if (simplify_flow(ep
)) {
2208 /* Finally, add deathnotes to pseudos now that we have them */
2210 track_pseudo_death(ep
);
2215 struct entrypoint
*linearize_symbol(struct symbol
*sym
)
2217 struct symbol
*base_type
;
2221 current_pos
= sym
->pos
;
2222 base_type
= sym
->ctype
.base_type
;
2225 if (base_type
->type
== SYM_FN
)
2226 return linearize_fn(sym
, base_type
);