2 * Example of how to write a compiler with sparse
11 #include "expression.h"
12 #include "linearize.h"
17 static const char *opcodes
[] = {
18 [OP_BADOP
] = "bad_op",
21 [OP_ENTRY
] = "<entry-point>",
27 [OP_SWITCH
] = "switch",
28 [OP_COMPUTEDGOTO
] = "jmp *",
47 /* Binary comparison */
48 [OP_SET_EQ
] = "seteq",
49 [OP_SET_NE
] = "setne",
50 [OP_SET_LE
] = "setle",
51 [OP_SET_GE
] = "setge",
52 [OP_SET_LT
] = "setlt",
53 [OP_SET_GT
] = "setgt",
56 [OP_SET_BE
] = "setbe",
57 [OP_SET_AE
] = "setae",
63 /* Special three-input */
73 [OP_PHISOURCE
] = "phisrc",
85 [OP_PTRCAST
] = "ptrcast",
89 [OP_DEATHNOTE
] = "dead",
92 /* Sparse tagging (line numbers, context, whatever) */
93 [OP_CONTEXT
] = "context",
96 static int last_reg
, stack_offset
;
100 struct pseudo_list
*contains
;
109 /* Our "switch" generation is very very stupid. */
110 #define SWITCH_REG (1)
112 static void output_bb(struct basic_block
*bb
, unsigned long generation
);
115 * We only know about the caller-clobbered registers
118 static struct hardreg hardregs
[] = {
135 struct storage_hash_list
*inputs
;
136 struct storage_hash_list
*outputs
;
137 struct storage_hash_list
*internal
;
140 int cc_opcode
, cc_dead
;
158 struct /* OP_MEM and OP_ADDR */ {
162 struct hardreg
*base
;
163 struct hardreg
*index
;
168 static const char *show_op(struct bb_state
*state
, struct operand
*op
)
170 static char buf
[256][4];
175 nr
= (bufnr
+ 1) & 3;
183 return op
->reg
->name
;
185 sprintf(p
, "$%lld", op
->value
);
190 p
+= sprintf(p
, "%d", op
->offset
);
192 p
+= sprintf(p
, "%s%s",
193 op
->offset
? "+" : "",
194 show_ident(op
->sym
->ident
));
195 if (op
->base
|| op
->index
) {
196 p
+= sprintf(p
, "(%s%s%s",
197 op
->base
? op
->base
->name
: "",
198 (op
->base
&& op
->index
) ? "," : "",
199 op
->index
? op
->index
->name
: "");
201 p
+= sprintf(p
, ",%d", op
->scale
);
210 static struct storage_hash
*find_storage_hash(pseudo_t pseudo
, struct storage_hash_list
*list
)
212 struct storage_hash
*entry
;
213 FOR_EACH_PTR(list
, entry
) {
214 if (entry
->pseudo
== pseudo
)
216 } END_FOR_EACH_PTR(entry
);
220 static struct storage_hash
*find_or_create_hash(pseudo_t pseudo
, struct storage_hash_list
**listp
)
222 struct storage_hash
*entry
;
224 entry
= find_storage_hash(pseudo
, *listp
);
226 entry
= alloc_storage_hash(alloc_storage());
227 entry
->pseudo
= pseudo
;
228 add_ptr_list(listp
, entry
);
233 /* Eventually we should just build it up in memory */
234 static void FORMAT_ATTR(2) output_line(struct bb_state
*state
, const char *fmt
, ...)
243 static void FORMAT_ATTR(2) output_label(struct bb_state
*state
, const char *fmt
, ...)
245 static char buffer
[512];
249 vsnprintf(buffer
, sizeof(buffer
), fmt
, args
);
252 output_line(state
, "%s:\n", buffer
);
255 static void FORMAT_ATTR(2) output_insn(struct bb_state
*state
, const char *fmt
, ...)
257 static char buffer
[512];
261 vsnprintf(buffer
, sizeof(buffer
), fmt
, args
);
264 output_line(state
, "\t%s\n", buffer
);
267 #define output_insn(state, fmt, arg...) \
268 output_insn(state, fmt "\t\t# %s" , ## arg , __FUNCTION__)
270 static void FORMAT_ATTR(2) output_comment(struct bb_state
*state
, const char *fmt
, ...)
272 static char buffer
[512];
278 vsnprintf(buffer
, sizeof(buffer
), fmt
, args
);
281 output_line(state
, "\t# %s\n", buffer
);
284 static const char *show_memop(struct storage
*storage
)
286 static char buffer
[1000];
290 switch (storage
->type
) {
292 sprintf(buffer
, "%d(FP)", storage
->offset
);
295 sprintf(buffer
, "%d(SP)", storage
->offset
);
298 return hardregs
[storage
->regno
].name
;
300 return show_storage(storage
);
305 static int alloc_stack_offset(int size
)
307 int ret
= stack_offset
;
308 stack_offset
= ret
+ size
;
312 static void alloc_stack(struct bb_state
*state
, struct storage
*storage
)
314 storage
->type
= REG_STACK
;
315 storage
->offset
= alloc_stack_offset(4);
319 * Can we re-generate the pseudo, so that we don't need to
320 * flush it to memory? We can regenerate:
321 * - immediates and symbol addresses
322 * - pseudos we got as input in non-registers
323 * - pseudos we've already saved off earlier..
325 static int can_regenerate(struct bb_state
*state
, pseudo_t pseudo
)
327 struct storage_hash
*in
;
329 switch (pseudo
->type
) {
335 in
= find_storage_hash(pseudo
, state
->inputs
);
336 if (in
&& in
->storage
->type
!= REG_REG
)
338 in
= find_storage_hash(pseudo
, state
->internal
);
345 static void flush_one_pseudo(struct bb_state
*state
, struct hardreg
*hardreg
, pseudo_t pseudo
)
347 struct storage_hash
*out
;
348 struct storage
*storage
;
350 if (can_regenerate(state
, pseudo
))
353 output_comment(state
, "flushing %s from %s", show_pseudo(pseudo
), hardreg
->name
);
354 out
= find_storage_hash(pseudo
, state
->internal
);
356 out
= find_storage_hash(pseudo
, state
->outputs
);
358 out
= find_or_create_hash(pseudo
, &state
->internal
);
360 storage
= out
->storage
;
361 switch (storage
->type
) {
364 * Aieee - the next user wants it in a register, but we
365 * need to flush it to memory in between. Which means that
366 * we need to allocate an internal one, dammit..
368 out
= find_or_create_hash(pseudo
, &state
->internal
);
369 storage
= out
->storage
;
372 alloc_stack(state
, storage
);
375 output_insn(state
, "movl %s,%s", hardreg
->name
, show_memop(storage
));
380 /* Flush a hardreg out to the storage it has.. */
381 static void flush_reg(struct bb_state
*state
, struct hardreg
*reg
)
386 output_comment(state
, "reg %s flushed while busy is %d!", reg
->name
, reg
->busy
);
391 FOR_EACH_PTR_TAG(reg
->contains
, pseudo
) {
392 if (CURRENT_TAG(pseudo
) & TAG_DEAD
)
394 if (!(CURRENT_TAG(pseudo
) & TAG_DIRTY
))
396 flush_one_pseudo(state
, reg
, pseudo
);
397 } END_FOR_EACH_PTR(pseudo
);
398 free_ptr_list(®
->contains
);
401 static struct storage_hash
*find_pseudo_storage(struct bb_state
*state
, pseudo_t pseudo
, struct hardreg
*reg
)
403 struct storage_hash
*src
;
405 src
= find_storage_hash(pseudo
, state
->internal
);
407 src
= find_storage_hash(pseudo
, state
->inputs
);
409 src
= find_storage_hash(pseudo
, state
->outputs
);
410 /* Undefined? Screw it! */
415 * If we found output storage, it had better be local stack
416 * that we flushed to earlier..
418 if (src
->storage
->type
!= REG_STACK
)
424 * Incoming pseudo with out any pre-set storage allocation?
425 * We can make up our own, and obviously prefer to get it
426 * in the register we already selected (if it hasn't been
429 if (src
->storage
->type
== REG_UDEF
) {
430 if (reg
&& !reg
->used
) {
431 src
->storage
->type
= REG_REG
;
432 src
->storage
->regno
= reg
- hardregs
;
435 alloc_stack(state
, src
->storage
);
440 static void mark_reg_dead(struct bb_state
*state
, pseudo_t pseudo
, struct hardreg
*reg
)
444 FOR_EACH_PTR_TAG(reg
->contains
, p
) {
447 if (CURRENT_TAG(p
) & TAG_DEAD
)
449 output_comment(state
, "marking pseudo %s in reg %s dead", show_pseudo(pseudo
), reg
->name
);
450 TAG_CURRENT(p
, TAG_DEAD
);
452 } END_FOR_EACH_PTR(p
);
455 static void add_pseudo_reg(struct bb_state
*state
, pseudo_t pseudo
, struct hardreg
*reg
)
457 output_comment(state
, "added pseudo %s to reg %s", show_pseudo(pseudo
), reg
->name
);
458 add_ptr_list_tag(®
->contains
, pseudo
, TAG_DIRTY
);
461 static struct hardreg
*preferred_reg(struct bb_state
*state
, pseudo_t target
)
463 struct storage_hash
*dst
;
465 dst
= find_storage_hash(target
, state
->outputs
);
467 struct storage
*storage
= dst
->storage
;
468 if (storage
->type
== REG_REG
)
469 return hardregs
+ storage
->regno
;
474 static struct hardreg
*empty_reg(struct bb_state
*state
)
477 struct hardreg
*reg
= hardregs
;
479 for (i
= 0; i
< REGNO
; i
++, reg
++) {
486 static struct hardreg
*target_reg(struct bb_state
*state
, pseudo_t pseudo
, pseudo_t target
)
489 int unable_to_find_reg
= 0;
492 /* First, see if we have a preferred target register.. */
493 reg
= preferred_reg(state
, target
);
494 if (reg
&& !reg
->contains
)
497 reg
= empty_reg(state
);
508 flush_reg(state
, reg
);
512 } while (i
!= last_reg
);
513 assert(unable_to_find_reg
);
516 add_pseudo_reg(state
, pseudo
, reg
);
520 static struct hardreg
*find_in_reg(struct bb_state
*state
, pseudo_t pseudo
)
525 for (i
= 0; i
< REGNO
; i
++) {
529 FOR_EACH_PTR_TAG(reg
->contains
, p
) {
532 output_comment(state
, "found pseudo %s in reg %s (busy=%d)", show_pseudo(pseudo
), reg
->name
, reg
->busy
);
535 } END_FOR_EACH_PTR(p
);
540 static void flush_pseudo(struct bb_state
*state
, pseudo_t pseudo
, struct storage
*storage
)
542 struct hardreg
*reg
= find_in_reg(state
, pseudo
);
545 flush_reg(state
, reg
);
548 static void flush_cc_cache_to_reg(struct bb_state
*state
, pseudo_t pseudo
, struct hardreg
*reg
)
550 int opcode
= state
->cc_opcode
;
552 state
->cc_opcode
= 0;
553 state
->cc_target
= NULL
;
554 output_insn(state
, "%s %s", opcodes
[opcode
], reg
->name
);
557 static void flush_cc_cache(struct bb_state
*state
)
559 pseudo_t pseudo
= state
->cc_target
;
564 state
->cc_target
= NULL
;
566 if (!state
->cc_dead
) {
567 dst
= target_reg(state
, pseudo
, pseudo
);
568 flush_cc_cache_to_reg(state
, pseudo
, dst
);
573 static void add_cc_cache(struct bb_state
*state
, int opcode
, pseudo_t pseudo
)
575 assert(!state
->cc_target
);
576 state
->cc_target
= pseudo
;
577 state
->cc_opcode
= opcode
;
579 output_comment(state
, "caching %s", opcodes
[opcode
]);
582 /* Fill a hardreg with the pseudo it has */
583 static struct hardreg
*fill_reg(struct bb_state
*state
, struct hardreg
*hardreg
, pseudo_t pseudo
)
585 struct storage_hash
*src
;
586 struct instruction
*def
;
588 if (state
->cc_target
== pseudo
) {
589 flush_cc_cache_to_reg(state
, pseudo
, hardreg
);
593 switch (pseudo
->type
) {
595 output_insn(state
, "movl $%lld,%s", pseudo
->value
, hardreg
->name
);
598 src
= find_pseudo_storage(state
, pseudo
, NULL
);
601 output_insn(state
, "movl $<%s>,%s", show_pseudo(pseudo
), hardreg
->name
);
604 switch (src
->storage
->type
) {
606 /* Aiaiaiaiaii! Need to flush it to temporary memory */
607 src
= find_or_create_hash(pseudo
, &state
->internal
);
610 alloc_stack(state
, src
->storage
);
614 flush_pseudo(state
, pseudo
, src
->storage
);
615 output_insn(state
, "leal %s,%s", show_memop(src
->storage
), hardreg
->name
);
622 if (def
&& def
->opcode
== OP_SETVAL
) {
623 output_insn(state
, "movl $<%s>,%s", show_pseudo(def
->target
), hardreg
->name
);
626 src
= find_pseudo_storage(state
, pseudo
, hardreg
);
629 if (src
->flags
& TAG_DEAD
)
630 mark_reg_dead(state
, pseudo
, hardreg
);
631 output_insn(state
, "mov.%d %s,%s", 32, show_memop(src
->storage
), hardreg
->name
);
634 output_insn(state
, "reload %s from %s", hardreg
->name
, show_pseudo(pseudo
));
640 static struct hardreg
*getreg(struct bb_state
*state
, pseudo_t pseudo
, pseudo_t target
)
644 reg
= find_in_reg(state
, pseudo
);
647 reg
= target_reg(state
, pseudo
, target
);
648 return fill_reg(state
, reg
, pseudo
);
651 static void move_reg(struct bb_state
*state
, struct hardreg
*src
, struct hardreg
*dst
)
653 output_insn(state
, "movl %s,%s", src
->name
, dst
->name
);
656 static struct hardreg
*copy_reg(struct bb_state
*state
, struct hardreg
*src
, pseudo_t target
)
661 /* If the container has been killed off, just re-use it */
665 /* If "src" only has one user, and the contents are dead, we can re-use it */
666 if (src
->busy
== 1 && src
->dead
== 1)
669 reg
= preferred_reg(state
, target
);
670 if (reg
&& !reg
->contains
) {
671 output_comment(state
, "copying %s to preferred target %s", show_pseudo(target
), reg
->name
);
672 move_reg(state
, src
, reg
);
676 for (i
= 0; i
< REGNO
; i
++) {
678 if (!reg
->contains
) {
679 output_comment(state
, "copying %s to %s", show_pseudo(target
), reg
->name
);
680 output_insn(state
, "movl %s,%s", src
->name
, reg
->name
);
685 flush_reg(state
, src
);
689 static void put_operand(struct bb_state
*state
, struct operand
*op
)
707 static struct operand
*alloc_op(void)
709 struct operand
*op
= malloc(sizeof(*op
));
710 memset(op
, 0, sizeof(*op
));
714 static struct operand
*get_register_operand(struct bb_state
*state
, pseudo_t pseudo
, pseudo_t target
)
716 struct operand
*op
= alloc_op();
718 op
->reg
= getreg(state
, pseudo
, target
);
723 static int get_sym_frame_offset(struct bb_state
*state
, pseudo_t pseudo
)
725 int offset
= pseudo
->nr
;
727 offset
= alloc_stack_offset(4);
733 static struct operand
*get_generic_operand(struct bb_state
*state
, pseudo_t pseudo
)
737 struct storage_hash
*hash
;
738 struct operand
*op
= malloc(sizeof(*op
));
740 memset(op
, 0, sizeof(*op
));
741 switch (pseudo
->type
) {
744 op
->value
= pseudo
->value
;
748 struct symbol
*sym
= pseudo
->sym
;
750 if (sym
->ctype
.modifiers
& MOD_NONLOCAL
) {
754 op
->base
= hardregs
+ REG_EBP
;
755 op
->offset
= get_sym_frame_offset(state
, pseudo
);
760 reg
= find_in_reg(state
, pseudo
);
767 hash
= find_pseudo_storage(state
, pseudo
, NULL
);
774 op
->reg
= hardregs
+ src
->regno
;
779 op
->offset
= src
->offset
;
780 op
->base
= hardregs
+ REG_EBP
;
784 op
->offset
= src
->offset
;
785 op
->base
= hardregs
+ REG_ESP
;
794 /* Callers should be made to use the proper "operand" formats */
795 static const char *generic(struct bb_state
*state
, pseudo_t pseudo
)
798 struct operand
*op
= get_generic_operand(state
, pseudo
);
799 static char buf
[100];
804 if (!op
->offset
&& op
->base
&& !op
->sym
)
805 return op
->base
->name
;
806 if (op
->sym
&& !op
->base
) {
807 int len
= sprintf(buf
, "$ %s", show_op(state
, op
));
809 sprintf(buf
+ len
, " + %d", op
->offset
);
812 str
= show_op(state
, op
);
813 put_operand(state
, op
);
814 reg
= target_reg(state
, pseudo
, NULL
);
815 output_insn(state
, "lea %s,%s", show_op(state
, op
), reg
->name
);
819 str
= show_op(state
, op
);
821 put_operand(state
, op
);
825 static struct operand
*get_address_operand(struct bb_state
*state
, struct instruction
*memop
)
827 struct hardreg
*base
;
828 struct operand
*op
= get_generic_operand(state
, memop
->src
);
832 op
->offset
+= memop
->offset
;
835 put_operand(state
, op
);
836 base
= getreg(state
, memop
->src
, NULL
);
840 op
->offset
= memop
->offset
;
846 static const char *address(struct bb_state
*state
, struct instruction
*memop
)
848 struct operand
*op
= get_address_operand(state
, memop
);
849 const char *str
= show_op(state
, op
);
850 put_operand(state
, op
);
854 static const char *reg_or_imm(struct bb_state
*state
, pseudo_t pseudo
)
856 switch(pseudo
->type
) {
858 return show_pseudo(pseudo
);
860 return getreg(state
, pseudo
, NULL
)->name
;
864 static void kill_dead_reg(struct hardreg
*reg
)
869 FOR_EACH_PTR_TAG(reg
->contains
, p
) {
870 if (CURRENT_TAG(p
) & TAG_DEAD
) {
871 DELETE_CURRENT_PTR(p
);
874 } END_FOR_EACH_PTR(p
);
875 PACK_PTR_LIST(®
->contains
);
880 static struct hardreg
*target_copy_reg(struct bb_state
*state
, struct hardreg
*src
, pseudo_t target
)
883 return copy_reg(state
, src
, target
);
886 static void do_binop(struct bb_state
*state
, struct instruction
*insn
, pseudo_t val1
, pseudo_t val2
)
888 const char *op
= opcodes
[insn
->opcode
];
889 struct operand
*src
= get_register_operand(state
, val1
, insn
->target
);
890 struct operand
*src2
= get_generic_operand(state
, val2
);
893 dst
= target_copy_reg(state
, src
->reg
, insn
->target
);
894 output_insn(state
, "%s.%d %s,%s", op
, insn
->size
, show_op(state
, src2
), dst
->name
);
895 put_operand(state
, src
);
896 put_operand(state
, src2
);
897 add_pseudo_reg(state
, insn
->target
, dst
);
900 static void generate_binop(struct bb_state
*state
, struct instruction
*insn
)
902 flush_cc_cache(state
);
903 do_binop(state
, insn
, insn
->src1
, insn
->src2
);
906 static int is_dead_reg(struct bb_state
*state
, pseudo_t pseudo
, struct hardreg
*reg
)
909 FOR_EACH_PTR_TAG(reg
->contains
, p
) {
911 return CURRENT_TAG(p
) & TAG_DEAD
;
912 } END_FOR_EACH_PTR(p
);
917 * Commutative binops are much more flexible, since we can switch the
918 * sources around to satisfy the target register, or to avoid having
919 * to load one of them into a register..
921 static void generate_commutative_binop(struct bb_state
*state
, struct instruction
*insn
)
924 struct hardreg
*reg1
, *reg2
;
926 flush_cc_cache(state
);
929 reg2
= find_in_reg(state
, src2
);
932 reg1
= find_in_reg(state
, src1
);
935 if (!is_dead_reg(state
, src2
, reg2
))
937 if (!is_dead_reg(state
, src1
, reg1
))
940 /* Both are dead. Is one preferable? */
941 if (reg2
!= preferred_reg(state
, insn
->target
))
948 do_binop(state
, insn
, src1
, src2
);
952 * This marks a pseudo dead. It still stays on the hardreg list (the hardreg
953 * still has its value), but it's scheduled to be killed after the next
954 * "sequence point" when we call "kill_read_pseudos()"
956 static void mark_pseudo_dead(struct bb_state
*state
, pseudo_t pseudo
)
959 struct storage_hash
*src
;
961 if (state
->cc_target
== pseudo
)
963 src
= find_pseudo_storage(state
, pseudo
, NULL
);
965 src
->flags
|= TAG_DEAD
;
966 for (i
= 0; i
< REGNO
; i
++)
967 mark_reg_dead(state
, pseudo
, hardregs
+ i
);
970 static void kill_dead_pseudos(struct bb_state
*state
)
974 for (i
= 0; i
< REGNO
; i
++) {
975 kill_dead_reg(hardregs
+ i
);
979 static void generate_store(struct instruction
*insn
, struct bb_state
*state
)
981 output_insn(state
, "mov.%d %s,%s", insn
->size
, reg_or_imm(state
, insn
->target
), address(state
, insn
));
984 static void generate_load(struct instruction
*insn
, struct bb_state
*state
)
986 const char *input
= address(state
, insn
);
989 kill_dead_pseudos(state
);
990 dst
= target_reg(state
, insn
->target
, NULL
);
991 output_insn(state
, "mov.%d %s,%s", insn
->size
, input
, dst
->name
);
994 static void kill_pseudo(struct bb_state
*state
, pseudo_t pseudo
)
999 output_comment(state
, "killing pseudo %s", show_pseudo(pseudo
));
1000 for (i
= 0; i
< REGNO
; i
++) {
1004 FOR_EACH_PTR_TAG(reg
->contains
, p
) {
1007 if (CURRENT_TAG(p
) & TAG_DEAD
)
1009 output_comment(state
, "removing pseudo %s from reg %s",
1010 show_pseudo(pseudo
), reg
->name
);
1011 DELETE_CURRENT_PTR(p
);
1012 } END_FOR_EACH_PTR(p
);
1013 PACK_PTR_LIST(®
->contains
);
1017 static void generate_copy(struct bb_state
*state
, struct instruction
*insn
)
1019 struct hardreg
*src
= getreg(state
, insn
->src
, insn
->target
);
1020 kill_pseudo(state
, insn
->target
);
1021 add_pseudo_reg(state
, insn
->target
, src
);
1024 static void generate_cast(struct bb_state
*state
, struct instruction
*insn
)
1026 struct hardreg
*src
= getreg(state
, insn
->src
, insn
->target
);
1027 struct hardreg
*dst
;
1028 unsigned int old
= insn
->orig_type
? insn
->orig_type
->bit_size
: 0;
1029 unsigned int new = insn
->size
;
1032 * Cast to smaller type? Ignore the high bits, we
1033 * just keep both pseudos in the same register.
1036 add_pseudo_reg(state
, insn
->target
, src
);
1040 dst
= target_copy_reg(state
, src
, insn
->target
);
1042 if (insn
->orig_type
&& (insn
->orig_type
->ctype
.modifiers
& MOD_SIGNED
)) {
1043 output_insn(state
, "sext.%d.%d %s", old
, new, dst
->name
);
1045 unsigned long long mask
;
1046 mask
= ~(~0ULL << old
);
1047 mask
&= ~(~0ULL << new);
1048 output_insn(state
, "andl.%d $%#llx,%s", insn
->size
, mask
, dst
->name
);
1050 add_pseudo_reg(state
, insn
->target
, dst
);
1053 static void generate_output_storage(struct bb_state
*state
);
1055 static const char *conditional
[] = {
1069 static void generate_branch(struct bb_state
*state
, struct instruction
*br
)
1071 const char *cond
= "XXX";
1072 struct basic_block
*target
;
1075 if (state
->cc_target
== br
->cond
) {
1076 cond
= conditional
[state
->cc_opcode
];
1078 struct hardreg
*reg
= getreg(state
, br
->cond
, NULL
);
1079 output_insn(state
, "testl %s,%s", reg
->name
, reg
->name
);
1083 generate_output_storage(state
);
1084 target
= br
->bb_true
;
1086 output_insn(state
, "j%s .L%p", cond
, target
);
1087 target
= br
->bb_false
;
1089 output_insn(state
, "jmp .L%p", target
);
1092 /* We've made sure that there is a dummy reg live for the output */
1093 static void generate_switch(struct bb_state
*state
, struct instruction
*insn
)
1095 struct hardreg
*reg
= hardregs
+ SWITCH_REG
;
1097 generate_output_storage(state
);
1098 output_insn(state
, "switch on %s", reg
->name
);
1099 output_insn(state
, "unimplemented: %s", show_instruction(insn
));
1102 static void generate_ret(struct bb_state
*state
, struct instruction
*ret
)
1104 if (ret
->src
&& ret
->src
!= VOID
) {
1105 struct hardreg
*wants
= hardregs
+0;
1106 struct hardreg
*reg
= getreg(state
, ret
->src
, NULL
);
1108 output_insn(state
, "movl %s,%s", reg
->name
, wants
->name
);
1110 output_insn(state
, "ret");
1114 * Fake "call" linearization just as a taster..
1116 static void generate_call(struct bb_state
*state
, struct instruction
*insn
)
1121 FOR_EACH_PTR(insn
->arguments
, arg
) {
1122 output_insn(state
, "pushl %s", generic(state
, arg
));
1124 } END_FOR_EACH_PTR(arg
);
1125 flush_reg(state
, hardregs
+0);
1126 flush_reg(state
, hardregs
+1);
1127 flush_reg(state
, hardregs
+2);
1128 output_insn(state
, "call %s", show_pseudo(insn
->func
));
1130 output_insn(state
, "addl $%d,%%esp", offset
);
1131 if (insn
->target
&& insn
->target
!= VOID
)
1132 add_pseudo_reg(state
, insn
->target
, hardregs
+0);
1135 static void generate_select(struct bb_state
*state
, struct instruction
*insn
)
1138 struct hardreg
*src1
, *src2
, *dst
;
1140 src1
= getreg(state
, insn
->src2
, NULL
);
1141 dst
= copy_reg(state
, src1
, insn
->target
);
1142 add_pseudo_reg(state
, insn
->target
, dst
);
1143 src2
= getreg(state
, insn
->src3
, insn
->target
);
1145 if (state
->cc_target
== insn
->src1
) {
1146 cond
= conditional
[state
->cc_opcode
];
1148 struct hardreg
*reg
= getreg(state
, insn
->src1
, NULL
);
1149 output_insn(state
, "testl %s,%s", reg
->name
, reg
->name
);
1153 output_insn(state
, "sel%s %s,%s", cond
, src2
->name
, dst
->name
);
1157 const struct ident
*name
;
1160 struct hardreg
*reg
;
1163 static void replace_asm_arg(char **dst_p
, struct asm_arg
*arg
)
1166 int len
= strlen(arg
->value
);
1168 memcpy(dst
, arg
->value
, len
);
1172 static void replace_asm_percent(const char **src_p
, char **dst_p
, struct asm_arg
*args
, int nr
)
1174 const char *src
= *src_p
;
1183 replace_asm_arg(dst_p
, args
+index
);
1190 static void replace_asm_named(const char **src_p
, char **dst_p
, struct asm_arg
*args
, int nr
)
1192 const char *src
= *src_p
;
1193 const char *end
= src
;
1203 for (i
= 0; i
< nr
; i
++) {
1204 const struct ident
*ident
= args
[i
].name
;
1209 if (memcmp(src
, ident
->name
, len
))
1211 replace_asm_arg(dst_p
, args
+i
);
1218 static const char *replace_asm_args(const char *str
, struct asm_arg
*args
, int nr
)
1220 static char buffer
[1000];
1236 replace_asm_percent(&str
, &p
, args
, nr
);
1239 replace_asm_named(&str
, &p
, args
, nr
);
1248 #define MAX_ASM_ARG (50)
1249 static struct asm_arg asm_arguments
[MAX_ASM_ARG
];
1251 static struct asm_arg
*generate_asm_inputs(struct bb_state
*state
, struct asm_constraint_list
*list
, struct asm_arg
*arg
)
1253 struct asm_constraint
*entry
;
1255 FOR_EACH_PTR(list
, entry
) {
1256 const char *constraint
= entry
->constraint
;
1257 pseudo_t pseudo
= entry
->pseudo
;
1258 struct hardreg
*reg
, *orig
;
1263 switch (*constraint
) {
1265 string
= getreg(state
, pseudo
, NULL
)->name
;
1268 index
= *constraint
- '0';
1269 reg
= asm_arguments
[index
].reg
;
1270 orig
= find_in_reg(state
, pseudo
);
1272 move_reg(state
, orig
, reg
);
1274 fill_reg(state
, reg
, pseudo
);
1278 string
= generic(state
, pseudo
);
1282 output_insn(state
, "# asm input \"%s\": %s : %s", constraint
, show_pseudo(pseudo
), string
);
1284 arg
->name
= entry
->ident
;
1285 arg
->value
= string
;
1289 } END_FOR_EACH_PTR(entry
);
1293 static struct asm_arg
*generate_asm_outputs(struct bb_state
*state
, struct asm_constraint_list
*list
, struct asm_arg
*arg
)
1295 struct asm_constraint
*entry
;
1297 FOR_EACH_PTR(list
, entry
) {
1298 const char *constraint
= entry
->constraint
;
1299 pseudo_t pseudo
= entry
->pseudo
;
1300 struct hardreg
*reg
;
1303 while (*constraint
== '=' || *constraint
== '+')
1307 switch (*constraint
) {
1310 reg
= target_reg(state
, pseudo
, NULL
);
1311 arg
->pseudo
= pseudo
;
1317 output_insn(state
, "# asm output \"%s\": %s : %s", constraint
, show_pseudo(pseudo
), string
);
1319 arg
->name
= entry
->ident
;
1320 arg
->value
= string
;
1322 } END_FOR_EACH_PTR(entry
);
1326 static void generate_asm(struct bb_state
*state
, struct instruction
*insn
)
1328 const char *str
= insn
->string
;
1330 if (insn
->asm_rules
->outputs
|| insn
->asm_rules
->inputs
) {
1331 struct asm_arg
*arg
;
1333 arg
= generate_asm_outputs(state
, insn
->asm_rules
->outputs
, asm_arguments
);
1334 arg
= generate_asm_inputs(state
, insn
->asm_rules
->inputs
, arg
);
1335 str
= replace_asm_args(str
, asm_arguments
, arg
- asm_arguments
);
1337 output_insn(state
, "%s", str
);
1340 static void generate_compare(struct bb_state
*state
, struct instruction
*insn
)
1342 struct hardreg
*src
;
1346 flush_cc_cache(state
);
1347 opcode
= insn
->opcode
;
1350 * We should try to switch these around if necessary,
1351 * and update the opcode to match..
1353 src
= getreg(state
, insn
->src1
, insn
->target
);
1354 src2
= generic(state
, insn
->src2
);
1356 output_insn(state
, "cmp.%d %s,%s", insn
->size
, src2
, src
->name
);
1358 add_cc_cache(state
, opcode
, insn
->target
);
1361 static void generate_one_insn(struct instruction
*insn
, struct bb_state
*state
)
1364 output_comment(state
, "%s", show_instruction(insn
));
1366 switch (insn
->opcode
) {
1368 struct symbol
*sym
= insn
->bb
->ep
->name
;
1369 const char *name
= show_ident(sym
->ident
);
1370 if (sym
->ctype
.modifiers
& MOD_STATIC
)
1371 printf("\n\n%s:\n", name
);
1373 printf("\n\n.globl %s\n%s:\n", name
, name
);
1378 * OP_SETVAL likewise doesn't actually generate any
1379 * code. On use, the "def" of the pseudo will be
1386 generate_store(insn
, state
);
1390 generate_load(insn
, state
);
1394 mark_pseudo_dead(state
, insn
->target
);
1398 generate_copy(state
, insn
);
1401 case OP_ADD
: case OP_MUL
:
1402 case OP_AND
: case OP_OR
: case OP_XOR
:
1403 generate_commutative_binop(state
, insn
);
1406 case OP_SUB
: case OP_DIVU
: case OP_DIVS
:
1407 case OP_MODU
: case OP_MODS
:
1408 case OP_SHL
: case OP_LSR
: case OP_ASR
:
1409 generate_binop(state
, insn
);
1412 case OP_BINCMP
... OP_BINCMP_END
:
1413 generate_compare(state
, insn
);
1416 case OP_SEXT
: case OP_ZEXT
:
1421 case OP_FCVTU
: case OP_FCVTS
:
1422 case OP_UCVTF
: case OP_SCVTF
:
1424 generate_cast(state
, insn
);
1428 generate_select(state
, insn
);
1433 generate_branch(state
, insn
);
1437 generate_switch(state
, insn
);
1441 generate_call(state
, insn
);
1445 generate_ret(state
, insn
);
1449 generate_asm(state
, insn
);
1455 output_insn(state
, "unimplemented: %s", show_instruction(insn
));
1458 kill_dead_pseudos(state
);
1461 #define VERY_BUSY 1000
1462 #define REG_FIXED 2000
1464 static void write_reg_to_storage(struct bb_state
*state
, struct hardreg
*reg
, pseudo_t pseudo
, struct storage
*storage
)
1467 struct hardreg
*out
;
1469 switch (storage
->type
) {
1471 out
= hardregs
+ storage
->regno
;
1474 output_insn(state
, "movl %s,%s", reg
->name
, out
->name
);
1477 if (reg
->busy
< VERY_BUSY
) {
1478 storage
->type
= REG_REG
;
1479 storage
->regno
= reg
- hardregs
;
1480 reg
->busy
= REG_FIXED
;
1484 /* Try to find a non-busy register.. */
1485 for (i
= 0; i
< REGNO
; i
++) {
1489 output_insn(state
, "movl %s,%s", reg
->name
, out
->name
);
1490 storage
->type
= REG_REG
;
1492 out
->busy
= REG_FIXED
;
1496 /* Fall back on stack allocation ... */
1497 alloc_stack(state
, storage
);
1500 output_insn(state
, "movl %s,%s", reg
->name
, show_memop(storage
));
1505 static void write_val_to_storage(struct bb_state
*state
, pseudo_t src
, struct storage
*storage
)
1507 struct hardreg
*out
;
1509 switch (storage
->type
) {
1511 alloc_stack(state
, storage
);
1513 output_insn(state
, "movl %s,%s", show_pseudo(src
), show_memop(storage
));
1516 out
= hardregs
+ storage
->regno
;
1517 output_insn(state
, "movl %s,%s", show_pseudo(src
), out
->name
);
1521 static void fill_output(struct bb_state
*state
, pseudo_t pseudo
, struct storage
*out
)
1524 struct storage_hash
*in
;
1525 struct instruction
*def
;
1527 /* Is that pseudo a constant value? */
1528 switch (pseudo
->type
) {
1530 write_val_to_storage(state
, pseudo
, out
);
1534 if (def
&& def
->opcode
== OP_SETVAL
) {
1535 write_val_to_storage(state
, pseudo
, out
);
1542 /* See if we have that pseudo in a register.. */
1543 for (i
= 0; i
< REGNO
; i
++) {
1544 struct hardreg
*reg
= hardregs
+ i
;
1547 FOR_EACH_PTR_TAG(reg
->contains
, p
) {
1549 write_reg_to_storage(state
, reg
, pseudo
, out
);
1552 } END_FOR_EACH_PTR(p
);
1555 /* Do we have it in another storage? */
1556 in
= find_storage_hash(pseudo
, state
->internal
);
1558 in
= find_storage_hash(pseudo
, state
->inputs
);
1563 switch (out
->type
) {
1565 *out
= *in
->storage
;
1568 output_insn(state
, "movl %s,%s", show_memop(in
->storage
), hardregs
[out
->regno
].name
);
1571 if (out
== in
->storage
)
1573 if ((out
->type
== in
->storage
->type
) && (out
->regno
== in
->storage
->regno
))
1575 output_insn(state
, "movl %s,%s", show_memop(in
->storage
), show_memop(out
));
1581 static int final_pseudo_flush(struct bb_state
*state
, pseudo_t pseudo
, struct hardreg
*reg
)
1583 struct storage_hash
*hash
;
1584 struct storage
*out
;
1585 struct hardreg
*dst
;
1588 * Since this pseudo is live at exit, we'd better have output
1591 hash
= find_storage_hash(pseudo
, state
->outputs
);
1594 out
= hash
->storage
;
1596 /* If the output is in a register, try to get it there.. */
1597 if (out
->type
== REG_REG
) {
1598 dst
= hardregs
+ out
->regno
;
1600 * Two good cases: nobody is using the right register,
1601 * or we've already set it aside for output..
1603 if (!dst
->contains
|| dst
->busy
> VERY_BUSY
)
1606 /* Aiee. Try to keep it in a register.. */
1607 dst
= empty_reg(state
);
1614 /* If the output is undefined, let's see if we can put it in a register.. */
1615 if (out
->type
== REG_UDEF
) {
1616 dst
= empty_reg(state
);
1618 out
->type
= REG_REG
;
1619 out
->regno
= dst
- hardregs
;
1622 /* Uhhuh. Not so good. No empty registers right now */
1626 /* If we know we need to flush it, just do so already .. */
1627 output_insn(state
, "movl %s,%s", reg
->name
, show_memop(out
));
1633 output_insn(state
, "movl %s,%s", reg
->name
, dst
->name
);
1634 add_pseudo_reg(state
, pseudo
, dst
);
1639 * This tries to make sure that we put all the pseudos that are
1640 * live on exit into the proper storage
1642 static void generate_output_storage(struct bb_state
*state
)
1644 struct storage_hash
*entry
;
1646 /* Go through the fixed outputs, making sure we have those regs free */
1647 FOR_EACH_PTR(state
->outputs
, entry
) {
1648 struct storage
*out
= entry
->storage
;
1649 if (out
->type
== REG_REG
) {
1650 struct hardreg
*reg
= hardregs
+ out
->regno
;
1654 reg
->busy
= REG_FIXED
;
1655 FOR_EACH_PTR_TAG(reg
->contains
, p
) {
1656 if (p
== entry
->pseudo
) {
1660 if (CURRENT_TAG(p
) & TAG_DEAD
)
1663 /* Try to write back the pseudo to where it should go ... */
1664 if (final_pseudo_flush(state
, p
, reg
)) {
1665 DELETE_CURRENT_PTR(p
);
1669 } END_FOR_EACH_PTR(p
);
1670 PACK_PTR_LIST(®
->contains
);
1672 flush_reg(state
, reg
);
1674 } END_FOR_EACH_PTR(entry
);
1676 FOR_EACH_PTR(state
->outputs
, entry
) {
1677 fill_output(state
, entry
->pseudo
, entry
->storage
);
1678 } END_FOR_EACH_PTR(entry
);
1681 static void generate(struct basic_block
*bb
, struct bb_state
*state
)
1684 struct storage_hash
*entry
;
1685 struct instruction
*insn
;
1687 for (i
= 0; i
< REGNO
; i
++) {
1688 free_ptr_list(&hardregs
[i
].contains
);
1689 hardregs
[i
].busy
= 0;
1690 hardregs
[i
].dead
= 0;
1691 hardregs
[i
].used
= 0;
1694 FOR_EACH_PTR(state
->inputs
, entry
) {
1695 struct storage
*storage
= entry
->storage
;
1696 const char *name
= show_storage(storage
);
1697 output_comment(state
, "incoming %s in %s", show_pseudo(entry
->pseudo
), name
);
1698 if (storage
->type
== REG_REG
) {
1699 int regno
= storage
->regno
;
1700 add_pseudo_reg(state
, entry
->pseudo
, hardregs
+ regno
);
1701 name
= hardregs
[regno
].name
;
1703 } END_FOR_EACH_PTR(entry
);
1705 output_label(state
, ".L%p", bb
);
1706 FOR_EACH_PTR(bb
->insns
, insn
) {
1709 generate_one_insn(insn
, state
);
1710 } END_FOR_EACH_PTR(insn
);
1713 output_comment(state
, "--- in ---");
1714 FOR_EACH_PTR(state
->inputs
, entry
) {
1715 output_comment(state
, "%s <- %s", show_pseudo(entry
->pseudo
), show_storage(entry
->storage
));
1716 } END_FOR_EACH_PTR(entry
);
1717 output_comment(state
, "--- spill ---");
1718 FOR_EACH_PTR(state
->internal
, entry
) {
1719 output_comment(state
, "%s <-> %s", show_pseudo(entry
->pseudo
), show_storage(entry
->storage
));
1720 } END_FOR_EACH_PTR(entry
);
1721 output_comment(state
, "--- out ---");
1722 FOR_EACH_PTR(state
->outputs
, entry
) {
1723 output_comment(state
, "%s -> %s", show_pseudo(entry
->pseudo
), show_storage(entry
->storage
));
1724 } END_FOR_EACH_PTR(entry
);
1729 static void generate_list(struct basic_block_list
*list
, unsigned long generation
)
1731 struct basic_block
*bb
;
1732 FOR_EACH_PTR(list
, bb
) {
1733 if (bb
->generation
== generation
)
1735 output_bb(bb
, generation
);
1736 } END_FOR_EACH_PTR(bb
);
1740 * Mark all the output registers of all the parents
1741 * as being "used" - this does not mean that we cannot
1742 * re-use them, but it means that we cannot ask the
1743 * parents to pass in another pseudo in one of those
1744 * registers that it already uses for another child.
1746 static void mark_used_registers(struct basic_block
*bb
, struct bb_state
*state
)
1748 struct basic_block
*parent
;
1750 FOR_EACH_PTR(bb
->parents
, parent
) {
1751 struct storage_hash_list
*outputs
= gather_storage(parent
, STOR_OUT
);
1752 struct storage_hash
*entry
;
1754 FOR_EACH_PTR(outputs
, entry
) {
1755 struct storage
*s
= entry
->storage
;
1756 if (s
->type
== REG_REG
) {
1757 struct hardreg
*reg
= hardregs
+ s
->regno
;
1760 } END_FOR_EACH_PTR(entry
);
1761 } END_FOR_EACH_PTR(parent
);
1764 static void output_bb(struct basic_block
*bb
, unsigned long generation
)
1766 struct bb_state state
;
1768 bb
->generation
= generation
;
1770 /* Make sure all parents have been generated first */
1771 generate_list(bb
->parents
, generation
);
1773 state
.pos
= bb
->pos
;
1774 state
.inputs
= gather_storage(bb
, STOR_IN
);
1775 state
.outputs
= gather_storage(bb
, STOR_OUT
);
1776 state
.internal
= NULL
;
1777 state
.cc_opcode
= 0;
1778 state
.cc_target
= NULL
;
1780 /* Mark incoming registers used */
1781 mark_used_registers(bb
, &state
);
1783 generate(bb
, &state
);
1785 free_ptr_list(&state
.inputs
);
1786 free_ptr_list(&state
.outputs
);
1788 /* Generate all children... */
1789 generate_list(bb
->children
, generation
);
1793 * We should set up argument sources here..
1795 * Things like "first three arguments in registers" etc
1796 * are all for this place.
1798 * On x86, we default to stack, unless it's a static
1799 * function that doesn't have its address taken.
1801 * I should implement the -mregparm=X cmd line option.
1803 static void set_up_arch_entry(struct entrypoint
*ep
, struct instruction
*entry
)
1806 struct symbol
*sym
, *argtype
;
1807 int i
, offset
, regparm
;
1811 if (!(sym
->ctype
.modifiers
& MOD_ADDRESSABLE
))
1813 sym
= sym
->ctype
.base_type
;
1816 PREPARE_PTR_LIST(sym
->arguments
, argtype
);
1817 FOR_EACH_PTR(entry
->arg_list
, arg
) {
1818 struct storage
*in
= lookup_storage(entry
->bb
, arg
, STOR_IN
);
1820 in
= alloc_storage();
1821 add_storage(in
, entry
->bb
, arg
, STOR_IN
);
1827 int bits
= argtype
? argtype
->bit_size
: 0;
1829 if (bits
< bits_in_int
)
1832 in
->type
= REG_FRAME
;
1833 in
->offset
= offset
;
1835 offset
+= bits_to_bytes(bits
);
1838 NEXT_PTR_LIST(argtype
);
1839 } END_FOR_EACH_PTR(arg
);
1840 FINISH_PTR_LIST(argtype
);
1844 * Set up storage information for "return"
1846 * Not strictly necessary, since the code generator will
1847 * certainly move the return value to the right register,
1848 * but it can help register allocation if the allocator
1849 * sees that the target register is going to return in %eax.
1851 static void set_up_arch_exit(struct basic_block
*bb
, struct instruction
*ret
)
1853 pseudo_t pseudo
= ret
->src
;
1855 if (pseudo
&& pseudo
!= VOID
) {
1856 struct storage
*out
= lookup_storage(bb
, pseudo
, STOR_OUT
);
1858 out
= alloc_storage();
1859 add_storage(out
, bb
, pseudo
, STOR_OUT
);
1861 out
->type
= REG_REG
;
1867 * Set up dummy/silly output storage information for a switch
1868 * instruction. We need to make sure that a register is available
1869 * when we generate code for switch, so force that by creating
1870 * a dummy output rule.
1872 static void set_up_arch_switch(struct basic_block
*bb
, struct instruction
*insn
)
1874 pseudo_t pseudo
= insn
->cond
;
1875 struct storage
*out
= lookup_storage(bb
, pseudo
, STOR_OUT
);
1877 out
= alloc_storage();
1878 add_storage(out
, bb
, pseudo
, STOR_OUT
);
1880 out
->type
= REG_REG
;
1881 out
->regno
= SWITCH_REG
;
1884 static void arch_set_up_storage(struct entrypoint
*ep
)
1886 struct basic_block
*bb
;
1888 /* Argument storage etc.. */
1889 set_up_arch_entry(ep
, ep
->entry
);
1891 FOR_EACH_PTR(ep
->bbs
, bb
) {
1892 struct instruction
*insn
= last_instruction(bb
->insns
);
1895 switch (insn
->opcode
) {
1897 set_up_arch_exit(bb
, insn
);
1900 set_up_arch_switch(bb
, insn
);
1905 } END_FOR_EACH_PTR(bb
);
1908 static void output(struct entrypoint
*ep
)
1910 unsigned long generation
= ++bb_generation
;
1915 /* Get rid of SSA form (phinodes etc) */
1918 /* Set up initial inter-bb storage links */
1921 /* Architecture-specific storage rules.. */
1922 arch_set_up_storage(ep
);
1924 /* Show the results ... */
1925 output_bb(ep
->entry
->bb
, generation
);
1927 /* Clear the storage hashes for the next function.. */
1931 static int compile(struct symbol_list
*list
)
1934 FOR_EACH_PTR(list
, sym
) {
1935 struct entrypoint
*ep
;
1937 ep
= linearize_symbol(sym
);
1940 } END_FOR_EACH_PTR(sym
);
1945 int main(int argc
, char **argv
)
1947 struct string_list
*filelist
= NULL
;
1950 compile(sparse_initialize(argc
, argv
, &filelist
));
1952 FOR_EACH_PTR(filelist
, file
) {
1953 compile(sparse(file
));
1954 } END_FOR_EACH_PTR(file
);