2 * This file implements the node emitter.
22 #include "iredges_t.h"
24 #include "../besched_t.h"
25 #include "../benode_t.h"
27 #include "ia32_emitter.h"
28 #include "gen_ia32_emitter.h"
29 #include "ia32_nodes_attr.h"
30 #include "ia32_new_nodes.h"
31 #include "ia32_map_regs.h"
33 #ifdef obstack_chunk_alloc
34 # undef obstack_chunk_alloc
35 # define obstack_chunk_alloc xmalloc
37 # define obstack_chunk_alloc xmalloc
38 # define obstack_chunk_free free
41 #define BLOCK_PREFIX(x) ".L" x
43 extern int obstack_printf(struct obstack *obst, char *fmt, ...);
45 #define SNPRINTF_BUF_LEN 128
47 /* global arch_env for lc_printf functions */
48 static const arch_env_t *arch_env = NULL;
50 /* indicates whether blocks are scheduled or not
51 (this variable is set automatically) */
52 static int have_block_sched = 0;
54 /*************************************************************
56 * (_) | | / _| | | | |
57 * _ __ _ __ _ _ __ | |_| |_ | |__ ___| |_ __ ___ _ __
58 * | '_ \| '__| | '_ \| __| _| | '_ \ / _ \ | '_ \ / _ \ '__|
59 * | |_) | | | | | | | |_| | | | | | __/ | |_) | __/ |
60 * | .__/|_| |_|_| |_|\__|_| |_| |_|\___|_| .__/ \___|_|
63 *************************************************************/
65 /* We always pass the ir_node which is a pointer. */
66 static int ia32_get_arg_type(const lc_arg_occ_t *occ) {
67 return lc_arg_type_ptr;
72 * Returns the register at in position pos.
74 static const arch_register_t *get_in_reg(const ir_node *irn, int pos) {
76 const arch_register_t *reg = NULL;
78 assert(get_irn_arity(irn) > pos && "Invalid IN position");
80 /* The out register of the operator at position pos is the
81 in register we need. */
82 op = get_irn_n(irn, pos);
84 reg = arch_get_irn_register(arch_env, op);
86 assert(reg && "no in register found");
91 * Returns the register at out position pos.
93 static const arch_register_t *get_out_reg(const ir_node *irn, int pos) {
95 const arch_register_t *reg = NULL;
97 /* 1st case: irn is not of mode_T, so it has only */
98 /* one OUT register -> good */
99 /* 2nd case: irn is of mode_T -> collect all Projs and ask the */
100 /* Proj with the corresponding projnum for the register */
102 if (get_irn_mode(irn) != mode_T) {
103 reg = arch_get_irn_register(arch_env, irn);
105 else if (is_ia32_irn(irn)) {
106 reg = get_ia32_out_reg(irn, pos);
109 const ir_edge_t *edge;
111 foreach_out_edge(irn, edge) {
112 proj = get_edge_src_irn(edge);
113 assert(is_Proj(proj) && "non-Proj from mode_T node");
114 if (get_Proj_proj(proj) == pos) {
115 reg = arch_get_irn_register(arch_env, proj);
121 assert(reg && "no out register found");
131 * Returns the name of the in register at position pos.
133 static const char *get_ia32_reg_name(ir_node *irn, int pos, enum io_direction in_out) {
134 const arch_register_t *reg;
136 static char *buf = NULL;
139 if (in_out == IN_REG) {
140 reg = get_in_reg(irn, pos);
143 /* destination address mode nodes don't have outputs */
144 if (is_ia32_irn(irn) && get_ia32_op_type(irn) == ia32_AddrModeD) {
148 reg = get_out_reg(irn, pos);
151 name = arch_register_get_name(reg);
157 len = strlen(name) + 2;
158 buf = xcalloc(1, len);
160 snprintf(buf, len, "%%%s", name);
166 * Get the register name for a node.
168 static int ia32_get_reg_name(lc_appendable_t *app,
169 const lc_arg_occ_t *occ, const lc_arg_value_t *arg)
172 ir_node *X = arg->v_ptr;
173 int nr = occ->width - 1;
176 return lc_appendable_snadd(app, "(null)", 6);
178 buf = get_ia32_reg_name(X, nr, occ->conversion == 'S' ? IN_REG : OUT_REG);
180 return lc_appendable_snadd(app, buf, strlen(buf));
184 * Returns the tarval, offset or scale of an ia32 as a string.
186 static int ia32_const_to_str(lc_appendable_t *app,
187 const lc_arg_occ_t *occ, const lc_arg_value_t *arg)
190 ir_node *X = arg->v_ptr;
193 return lc_arg_append(app, occ, "(null)", 6);
195 if (occ->conversion == 'C') {
196 buf = get_ia32_cnst(X);
199 buf = get_ia32_am_offs(X);
202 return buf ? lc_appendable_snadd(app, buf, strlen(buf)) : 0;
206 * Determines the SSE suffix depending on the mode.
208 static int ia32_get_mode_suffix(lc_appendable_t *app,
209 const lc_arg_occ_t *occ, const lc_arg_value_t *arg)
211 ir_node *X = arg->v_ptr;
212 ir_mode *mode = get_irn_mode(X);
214 if (mode == mode_T) {
215 mode = is_ia32_AddrModeS(X) || is_ia32_AddrModeD(X) ? get_ia32_ls_mode(X) : get_ia32_res_mode(X);
219 return lc_arg_append(app, occ, "(null)", 6);
221 if (mode_is_float(mode)) {
222 return lc_appendable_chadd(app, get_mode_size_bits(mode) == 32 ? 's' : 'd');
225 return lc_appendable_chadd(app, mode_is_signed(mode) ? 's' : 'z');
230 * Return the ia32 printf arg environment.
231 * We use the firm environment with some additional handlers.
233 const lc_arg_env_t *ia32_get_arg_env(void) {
234 static lc_arg_env_t *env = NULL;
236 static const lc_arg_handler_t ia32_reg_handler = { ia32_get_arg_type, ia32_get_reg_name };
237 static const lc_arg_handler_t ia32_const_handler = { ia32_get_arg_type, ia32_const_to_str };
238 static const lc_arg_handler_t ia32_mode_handler = { ia32_get_arg_type, ia32_get_mode_suffix };
241 /* extend the firm printer */
242 env = firm_get_arg_env();
244 lc_arg_register(env, "ia32:sreg", 'S', &ia32_reg_handler);
245 lc_arg_register(env, "ia32:dreg", 'D', &ia32_reg_handler);
246 lc_arg_register(env, "ia32:cnst", 'C', &ia32_const_handler);
247 lc_arg_register(env, "ia32:offs", 'O', &ia32_const_handler);
248 lc_arg_register(env, "ia32:mode", 'M', &ia32_mode_handler);
255 * Emits registers and/or address mode of a binary operation.
257 char *ia32_emit_binop(const ir_node *n, ia32_emit_env_t *env) {
258 static char *buf = NULL;
260 /* verify that this function is never called on non-AM supporting operations */
261 //assert(get_ia32_am_support(n) != ia32_am_None && "emit binop expects addressmode support");
263 #define PRODUCES_RESULT(n) \
264 (!(is_ia32_St(n) || \
265 is_ia32_Store8Bit(n) || \
266 is_ia32_CondJmp(n) || \
267 is_ia32_fCondJmp(n) || \
268 is_ia32_SwitchJmp(n)))
271 buf = xcalloc(1, SNPRINTF_BUF_LEN);
274 memset(buf, 0, SNPRINTF_BUF_LEN);
277 switch(get_ia32_op_type(n)) {
279 if (get_ia32_cnst(n)) {
280 lc_esnprintf(ia32_get_arg_env(), buf, SNPRINTF_BUF_LEN, "%3S, %s", n, get_ia32_cnst(n));
283 const arch_register_t *in1 = get_in_reg(n, 2);
284 const arch_register_t *in2 = get_in_reg(n, 3);
285 const arch_register_t *out = PRODUCES_RESULT(n) ? get_out_reg(n, 0) : NULL;
286 const arch_register_t *in;
288 in = out ? (REGS_ARE_EQUAL(out, in2) ? in1 : in2) : in2;
289 out = out ? out : in1;
291 snprintf(buf, SNPRINTF_BUF_LEN, "%%%s, %%%s", \
292 arch_register_get_name(out), arch_register_get_name(in));
296 lc_esnprintf(ia32_get_arg_env(), buf, SNPRINTF_BUF_LEN, "%4S, %s", n, ia32_emit_am(n, env));
299 if (get_ia32_cnst(n)) {
300 lc_esnprintf(ia32_get_arg_env(), buf, SNPRINTF_BUF_LEN, "%s, %s", ia32_emit_am(n, env), get_ia32_cnst(n));
303 const arch_register_t *in1 = get_in_reg(n, 2);
304 const char *reg_name;
305 ir_mode *mode = get_ia32_res_mode(n);
307 mode = mode ? mode : get_ia32_ls_mode(n);
309 switch(get_mode_size_bits(mode)) {
311 reg_name = ia32_get_mapped_reg_name(env->isa->regs_8bit, in1);
314 reg_name = ia32_get_mapped_reg_name(env->isa->regs_16bit, in1);
317 reg_name = arch_register_get_name(in1);
320 assert(0 && "unsupported mode size");
324 lc_esnprintf(ia32_get_arg_env(), buf, SNPRINTF_BUF_LEN, "%s, %%%s", ia32_emit_am(n, env), reg_name);
328 assert(0 && "unsupported op type");
331 #undef PRODUCES_RESULT
337 * Emits registers and/or address mode of a unary operation.
339 char *ia32_emit_unop(const ir_node *n, ia32_emit_env_t *env) {
340 static char *buf = NULL;
343 buf = xcalloc(1, SNPRINTF_BUF_LEN);
346 memset(buf, 0, SNPRINTF_BUF_LEN);
349 switch(get_ia32_op_type(n)) {
351 lc_esnprintf(ia32_get_arg_env(), buf, SNPRINTF_BUF_LEN, "%1D", n);
354 snprintf(buf, SNPRINTF_BUF_LEN, ia32_emit_am(n, env));
357 assert(0 && "unsupported op type");
364 * Emits address mode.
366 char *ia32_emit_am(const ir_node *n, ia32_emit_env_t *env) {
367 ia32_am_flavour_t am_flav = get_ia32_am_flavour(n);
371 static struct obstack *obst = NULL;
372 ir_mode *mode = get_ia32_ls_mode(n);
374 if (! is_ia32_Lea(n))
375 assert(mode && "AM node must have ls_mode attribute set.");
378 obst = xcalloc(1, sizeof(*obst));
381 obstack_free(obst, NULL);
384 /* obstack_free with NULL results in an uninitialized obstack */
388 switch (get_mode_size_bits(mode)) {
390 obstack_printf(obst, "BYTE PTR ");
393 obstack_printf(obst, "WORD PTR ");
396 obstack_printf(obst, "DWORD PTR ");
403 obstack_printf(obst, "[");
405 if (am_flav & ia32_B) {
406 lc_eoprintf(ia32_get_arg_env(), obst, "%1S", n);
410 if (am_flav & ia32_I) {
412 obstack_printf(obst, "+");
415 lc_eoprintf(ia32_get_arg_env(), obst, "%2S", n);
417 if (am_flav & ia32_S) {
418 obstack_printf(obst, "*%d", 1 << get_ia32_am_scale(n));
424 if (am_flav & ia32_O) {
425 obstack_printf(obst, get_ia32_am_offs(n));
428 obstack_printf(obst, "] ");
430 size = obstack_object_size(obst);
431 s = obstack_finish(obst);
440 * Formated print of commands and comments.
442 static void ia32_fprintf_format(FILE *F, char *cmd_buf, char *cmnt_buf) {
443 fprintf(F, "\t%-35s %-60s\n", cmd_buf, cmnt_buf);
449 * Add a number to a prefix. This number will not be used a second time.
451 static char *get_unique_label(char *buf, size_t buflen, const char *prefix) {
452 static unsigned long id = 0;
453 snprintf(buf, buflen, "%s%lu", prefix, ++id);
459 /*************************************************
462 * ___ _ __ ___ _| |_ ___ ___ _ __ __| |
463 * / _ \ '_ ` _ \| | __| / __/ _ \| '_ \ / _` |
464 * | __/ | | | | | | |_ | (_| (_) | | | | (_| |
465 * \___|_| |_| |_|_|\__| \___\___/|_| |_|\__,_|
467 *************************************************/
470 #define IA32_DO_EMIT ia32_fprintf_format(F, cmd_buf, cmnt_buf)
473 * coding of conditions
475 struct cmp2conditon_t {
481 * positive conditions for signed compares
483 static const struct cmp2conditon_t cmp2condition_s[] = {
484 { NULL, pn_Cmp_False }, /* always false */
485 { "e", pn_Cmp_Eq }, /* == */
486 { "l", pn_Cmp_Lt }, /* < */
487 { "le", pn_Cmp_Le }, /* <= */
488 { "g", pn_Cmp_Gt }, /* > */
489 { "ge", pn_Cmp_Ge }, /* >= */
490 { "ne", pn_Cmp_Lg }, /* != */
491 { "ordered", pn_Cmp_Leg }, /* Floating point: ordered */
492 { "unordered", pn_Cmp_Uo }, /* FLoting point: unordered */
493 { "unordered or ==", pn_Cmp_Ue }, /* Floating point: unordered or == */
494 { "unordered or <", pn_Cmp_Ul }, /* Floating point: unordered or < */
495 { "unordered or <=", pn_Cmp_Ule }, /* Floating point: unordered or <= */
496 { "unordered or >", pn_Cmp_Ug }, /* Floating point: unordered or > */
497 { "unordered or >=", pn_Cmp_Uge }, /* Floating point: unordered or >= */
498 { "unordered or !=", pn_Cmp_Ne }, /* Floating point: unordered or != */
499 { NULL, pn_Cmp_True }, /* always true */
503 * positive conditions for unsigned compares
505 static const struct cmp2conditon_t cmp2condition_u[] = {
506 { NULL, pn_Cmp_False }, /* always false */
507 { "e", pn_Cmp_Eq }, /* == */
508 { "b", pn_Cmp_Lt }, /* < */
509 { "be", pn_Cmp_Le }, /* <= */
510 { "a", pn_Cmp_Gt }, /* > */
511 { "ae", pn_Cmp_Ge }, /* >= */
512 { "ne", pn_Cmp_Lg }, /* != */
513 { "ordered", pn_Cmp_Leg }, /* Floating point: ordered */
514 { "unordered", pn_Cmp_Uo }, /* FLoting point: unordered */
515 { "unordered or ==", pn_Cmp_Ue }, /* Floating point: unordered or == */
516 { "unordered or <", pn_Cmp_Ul }, /* Floating point: unordered or < */
517 { "unordered or <=", pn_Cmp_Ule }, /* Floating point: unordered or <= */
518 { "unordered or >", pn_Cmp_Ug }, /* Floating point: unordered or > */
519 { "unordered or >=", pn_Cmp_Uge }, /* Floating point: unordered or >= */
520 { "unordered or !=", pn_Cmp_Ne }, /* Floating point: unordered or != */
521 { NULL, pn_Cmp_True }, /* always true */
525 * returns the condition code
527 static const char *get_cmp_suffix(int cmp_code, int unsigned_cmp)
529 assert(cmp2condition_s[cmp_code].num == cmp_code);
530 assert(cmp2condition_u[cmp_code].num == cmp_code);
532 return unsigned_cmp ? cmp2condition_u[cmp_code & 7].name : cmp2condition_s[cmp_code & 7].name;
536 * Returns the target block for a control flow node.
538 static ir_node *get_cfop_target_block(const ir_node *irn) {
539 return get_irn_link(irn);
543 * Returns the target label for a control flow node.
545 static char *get_cfop_target(const ir_node *irn, char *buf) {
546 ir_node *bl = get_cfop_target_block(irn);
548 snprintf(buf, SNPRINTF_BUF_LEN, BLOCK_PREFIX("%ld"), get_irn_node_nr(bl));
552 /** Return the next block in Block schedule */
553 static ir_node *next_blk_sched(const ir_node *block) {
554 return have_block_sched ? get_irn_link(block) : NULL;
558 * Emits the jump sequence for a conditional jump (cmp + jmp_true + jmp_false)
560 static void finish_CondJmp(FILE *F, const ir_node *irn, ir_mode *mode) {
561 const ir_node *proj1, *proj2 = NULL;
562 const ir_node *block, *next_bl = NULL;
563 const ir_edge_t *edge;
564 char buf[SNPRINTF_BUF_LEN];
565 char cmd_buf[SNPRINTF_BUF_LEN];
566 char cmnt_buf[SNPRINTF_BUF_LEN];
568 /* get both Proj's */
569 edge = get_irn_out_edge_first(irn);
570 proj1 = get_edge_src_irn(edge);
571 assert(is_Proj(proj1) && "CondJmp with a non-Proj");
573 edge = get_irn_out_edge_next(irn, edge);
575 proj2 = get_edge_src_irn(edge);
576 assert(is_Proj(proj2) && "CondJmp with a non-Proj");
579 /* for now, the code works for scheduled and non-schedules blocks */
580 block = get_nodes_block(irn);
582 /* we have a block schedule */
583 next_bl = next_blk_sched(block);
585 if (get_cfop_target_block(proj1) == next_bl) {
586 /* exchange both proj's so the second one can be omitted */
587 const ir_node *t = proj1;
593 /* the first Proj must always be created */
594 if (get_Proj_proj(proj1) == pn_Cond_true) {
595 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "j%s %s",
596 get_cmp_suffix(get_ia32_pncode(irn), !mode_is_signed(get_irn_mode(get_irn_n(irn, 0)))),
597 get_cfop_target(proj1, buf));
598 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* cmp(a, b) == TRUE */");
601 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "j%s %s",
602 get_cmp_suffix(get_negated_pnc(get_ia32_pncode(irn), mode),
603 !mode_is_signed(get_irn_mode(get_irn_n(irn, 0)))),
604 get_cfop_target(proj1, buf));
605 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* cmp(a, b) == FALSE */");
609 /* the second Proj might be a fallthrough */
611 if (get_cfop_target_block(proj2) != next_bl) {
612 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "jmp %s", get_cfop_target(proj2, buf));
613 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* otherwise */");
617 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* fallthrogh %s */", get_cfop_target(proj2, buf));
624 * Emits code for conditional jump.
626 static void CondJmp_emitter(const ir_node *irn, ia32_emit_env_t *env) {
628 char cmd_buf[SNPRINTF_BUF_LEN];
629 char cmnt_buf[SNPRINTF_BUF_LEN];
631 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "cmp %s", ia32_emit_binop(irn, env));
632 lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* %+F */", irn);
634 finish_CondJmp(F, irn, get_irn_mode(get_irn_n(irn, 2)));
638 * Emits code for conditional jump with two variables.
640 static void emit_ia32_CondJmp(const ir_node *irn, ia32_emit_env_t *env) {
641 CondJmp_emitter(irn, env);
645 * Emits code for conditional jump with immediate.
647 void emit_ia32_CondJmp_i(const ir_node *irn, ia32_emit_env_t *env) {
648 CondJmp_emitter(irn, env);
652 * Emits code for conditional test and jump.
654 static void TestJmp_emitter(const ir_node *irn, ia32_emit_env_t *env) {
656 const char *op1 = arch_register_get_name(get_in_reg(irn, 0));
657 const char *op2 = get_ia32_cnst(irn);
658 char cmd_buf[SNPRINTF_BUF_LEN];
659 char cmnt_buf[SNPRINTF_BUF_LEN];
662 op2 = arch_register_get_name(get_in_reg(irn, 1));
664 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "test %%%s,%s%s ", op1, get_ia32_cnst(irn) ? " " : " %", op2);
665 lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* %+F */", irn);
667 finish_CondJmp(F, irn, get_irn_mode(get_irn_n(irn, 0)));
671 * Emits code for conditional test and jump with two variables.
673 static void emit_ia32_TestJmp(const ir_node *irn, ia32_emit_env_t *env) {
674 TestJmp_emitter(irn, env);
679 /*********************************************************
682 * ___ _ __ ___ _| |_ _ _ _ _ __ ___ _ __ ___
683 * / _ \ '_ ` _ \| | __| | | | | | '_ ` _ \| '_ \/ __|
684 * | __/ | | | | | | |_ | | |_| | | | | | | |_) \__ \
685 * \___|_| |_| |_|_|\__| | |\__,_|_| |_| |_| .__/|___/
688 *********************************************************/
690 /* jump table entry (target and corresponding number) */
691 typedef struct _branch_t {
696 /* jump table for switch generation */
697 typedef struct _jmp_tbl_t {
698 ir_node *defProj; /**< default target */
699 int min_value; /**< smallest switch case */
700 int max_value; /**< largest switch case */
701 int num_branches; /**< number of jumps */
702 char *label; /**< label of the jump table */
703 branch_t *branches; /**< jump array */
707 * Compare two variables of type branch_t. Used to sort all switch cases
709 static int ia32_cmp_branch_t(const void *a, const void *b) {
710 branch_t *b1 = (branch_t *)a;
711 branch_t *b2 = (branch_t *)b;
713 if (b1->value <= b2->value)
720 * Emits code for a SwitchJmp (creates a jump table if
721 * possible otherwise a cmp-jmp cascade). Port from
724 void emit_ia32_SwitchJmp(const ir_node *irn, ia32_emit_env_t *emit_env) {
725 unsigned long interval;
726 char buf[SNPRINTF_BUF_LEN];
727 int last_value, i, pn, do_jmp_tbl = 1;
730 const ir_edge_t *edge;
731 const lc_arg_env_t *env = ia32_get_arg_env();
732 FILE *F = emit_env->out;
733 char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
735 /* fill the table structure */
736 tbl.label = xmalloc(SNPRINTF_BUF_LEN);
737 tbl.label = get_unique_label(tbl.label, SNPRINTF_BUF_LEN, "JMPTBL_");
739 tbl.num_branches = get_irn_n_edges(irn);
740 tbl.branches = xcalloc(tbl.num_branches, sizeof(tbl.branches[0]));
741 tbl.min_value = INT_MAX;
742 tbl.max_value = INT_MIN;
745 /* go over all proj's and collect them */
746 foreach_out_edge(irn, edge) {
747 proj = get_edge_src_irn(edge);
748 assert(is_Proj(proj) && "Only proj allowed at SwitchJmp");
750 pn = get_Proj_proj(proj);
752 /* create branch entry */
753 tbl.branches[i].target = proj;
754 tbl.branches[i].value = pn;
756 tbl.min_value = pn < tbl.min_value ? pn : tbl.min_value;
757 tbl.max_value = pn > tbl.max_value ? pn : tbl.max_value;
759 /* check for default proj */
760 if (pn == get_ia32_pncode(irn)) {
761 assert(tbl.defProj == NULL && "found two defProjs at SwitchJmp");
768 /* sort the branches by their number */
769 qsort(tbl.branches, tbl.num_branches, sizeof(tbl.branches[0]), ia32_cmp_branch_t);
771 /* two-complement's magic make this work without overflow */
772 interval = tbl.max_value - tbl.min_value;
774 /* check value interval */
775 if (interval > 16 * 1024) {
779 /* check ratio of value interval to number of branches */
780 if ((float)(interval + 1) / (float)tbl.num_branches > 8.0) {
786 if (tbl.min_value != 0) {
787 lc_esnprintf(env, cmd_buf, SNPRINTF_BUF_LEN, "cmpl %lu, -%d(%1S)",
788 interval, tbl.min_value, irn);
789 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* first switch value is not 0 */");
794 lc_esnprintf(env, cmd_buf, SNPRINTF_BUF_LEN, "cmpl %lu, %1S", interval, irn);
795 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* compare for switch */");
800 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "ja %s", get_cfop_target(tbl.defProj, buf));
801 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* default jump if out of range */");
804 if (tbl.num_branches > 1) {
807 lc_esnprintf(env, cmd_buf, SNPRINTF_BUF_LEN, "jmp [%1S*4+%s]", irn, tbl.label);
808 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* get jump table entry as target */");
811 fprintf(F, "\t.section\t.rodata\n");
812 fprintf(F, "\t.align 4\n");
814 fprintf(F, "%s:\n", tbl.label);
816 snprintf(cmd_buf, SNPRINTF_BUF_LEN, ".long %s", get_cfop_target(tbl.branches[0].target, buf));
817 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* case %d */\n", tbl.branches[0].value);
820 last_value = tbl.branches[0].value;
821 for (i = 1; i < tbl.num_branches; ++i) {
822 while (++last_value < tbl.branches[i].value) {
823 snprintf(cmd_buf, SNPRINTF_BUF_LEN, ".long %s", get_cfop_target(tbl.defProj, buf));
824 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* default case */");
827 snprintf(cmd_buf, SNPRINTF_BUF_LEN, ".long %s", get_cfop_target(tbl.branches[i].target, buf));
828 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* case %d */", last_value);
832 fprintf(F, "\t.text");
835 /* one jump is enough */
836 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "jmp %s", get_cfop_target(tbl.branches[0].target, buf));
837 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* only one case given */");
841 else { // no jump table
842 for (i = 0; i < tbl.num_branches; ++i) {
843 lc_esnprintf(env, cmd_buf, SNPRINTF_BUF_LEN, "cmpl %d, %1S", tbl.branches[i].value, irn);
844 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* case %d */", i);
846 fprintf(F, "\tje %s\n", get_cfop_target(tbl.branches[i].target, buf));
849 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "jmp %s", get_cfop_target(tbl.defProj, buf));
850 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* default case */");
861 * Emits code for a unconditional jump.
863 void emit_Jmp(const ir_node *irn, ia32_emit_env_t *env) {
864 ir_node *block, *next_bl;
866 char buf[SNPRINTF_BUF_LEN], cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
868 /* for now, the code works for scheduled and non-schedules blocks */
869 block = get_nodes_block(irn);
871 /* we have a block schedule */
872 next_bl = next_blk_sched(block);
873 if (get_cfop_target_block(irn) != next_bl) {
874 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "jmp %s", get_cfop_target(irn, buf));
875 lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* %+F(%+F) */", irn, get_cfop_target_block(irn));
879 lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* fallthrough %s */", get_cfop_target(irn, buf));
884 /****************************
887 * _ __ _ __ ___ _ ___
888 * | '_ \| '__/ _ \| |/ __|
889 * | |_) | | | (_) | |\__ \
890 * | .__/|_| \___/| ||___/
893 ****************************/
896 * Emits code for a proj -> node
898 void emit_Proj(const ir_node *irn, ia32_emit_env_t *env) {
899 ir_node *pred = get_Proj_pred(irn);
901 if (get_irn_op(pred) == op_Start) {
902 switch(get_Proj_proj(irn)) {
903 case pn_Start_X_initial_exec:
912 /**********************************
915 * | | ___ _ __ _ _| |_) |
916 * | | / _ \| '_ \| | | | _ <
917 * | |___| (_) | |_) | |_| | |_) |
918 * \_____\___/| .__/ \__, |____/
921 **********************************/
924 * Emit movsb/w instructions to make mov count divideable by 4
926 static void emit_CopyB_prolog(FILE *F, int rem, int size) {
927 char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
929 fprintf(F, "\t/* memcopy %d bytes*/\n", size);
931 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "cld");
932 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* copy direction forward*/");
937 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "movsb");
938 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* memcopy remainder 1 */");
941 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "movsw");
942 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* memcopy remainder 2 */");
945 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "movsb");
946 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* memcopy remainder 3 */");
948 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "movsw");
949 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* memcopy remainder 3 */");
957 * Emit rep movsd instruction for memcopy.
959 void emit_ia32_CopyB(const ir_node *irn, ia32_emit_env_t *emit_env) {
960 FILE *F = emit_env->out;
961 tarval *tv = get_ia32_Immop_tarval(irn);
962 int rem = get_tarval_long(tv);
963 int size = get_tarval_long(get_ia32_Immop_tarval(get_irn_n(irn, 2)));
964 char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
966 emit_CopyB_prolog(F, rem, size);
968 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "rep movsd");
969 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* memcopy */");
974 * Emits unrolled memcopy.
976 void emit_ia32_CopyB_i(const ir_node *irn, ia32_emit_env_t *emit_env) {
977 tarval *tv = get_ia32_Immop_tarval(irn);
978 int size = get_tarval_long(tv);
979 FILE *F = emit_env->out;
980 char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
982 emit_CopyB_prolog(F, size & 0x3, size);
986 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "movsd");
987 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* memcopy unrolled */");
994 /***************************
998 * | | / _ \| '_ \ \ / /
999 * | |___| (_) | | | \ V /
1000 * \_____\___/|_| |_|\_/
1002 ***************************/
1005 * Emit code for conversions (I, FP), (FP, I) and (FP, FP).
1007 static void emit_ia32_Conv(const ir_node *irn, ia32_emit_env_t *emit_env) {
1008 FILE *F = emit_env->out;
1009 const lc_arg_env_t *env = ia32_get_arg_env();
1010 char *from, *to, buf[64];
1011 ir_mode *src_mode, *tgt_mode;
1012 char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
1014 src_mode = is_ia32_AddrModeS(irn) ? get_ia32_ls_mode(irn) : get_irn_mode(get_irn_n(irn, 2));
1015 tgt_mode = get_ia32_res_mode(irn);
1017 from = mode_is_float(src_mode) ? (get_mode_size_bits(src_mode) == 32 ? "ss" : "sd") : "si";
1018 to = mode_is_float(tgt_mode) ? (get_mode_size_bits(tgt_mode) == 32 ? "ss" : "sd") : "si";
1020 switch(get_ia32_op_type(irn)) {
1022 lc_esnprintf(env, buf, sizeof(buf), "%1D, %3S", irn, irn);
1024 case ia32_AddrModeS:
1025 lc_esnprintf(env, buf, sizeof(buf), "%1D, %s", irn, ia32_emit_am(irn, emit_env));
1028 assert(0 && "unsupported op type for Conv");
1031 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "cvt%s2%s %s", from, to, buf);
1032 lc_esnprintf(env, cmnt_buf, SNPRINTF_BUF_LEN, "/* %+F(%+F, %+F) */", irn, src_mode, tgt_mode);
1036 void emit_ia32_Conv_I2FP(const ir_node *irn, ia32_emit_env_t *emit_env) {
1037 emit_ia32_Conv(irn, emit_env);
1040 void emit_ia32_Conv_FP2I(const ir_node *irn, ia32_emit_env_t *emit_env) {
1041 emit_ia32_Conv(irn, emit_env);
1044 void emit_ia32_Conv_FP2FP(const ir_node *irn, ia32_emit_env_t *emit_env) {
1045 emit_ia32_Conv(irn, emit_env);
1050 /*******************************************
1053 * | |__ ___ _ __ ___ __| | ___ ___
1054 * | '_ \ / _ \ '_ \ / _ \ / _` |/ _ \/ __|
1055 * | |_) | __/ | | | (_) | (_| | __/\__ \
1056 * |_.__/ \___|_| |_|\___/ \__,_|\___||___/
1058 *******************************************/
1061 * Emits a backend call
1063 void emit_be_Call(const ir_node *irn, ia32_emit_env_t *emit_env) {
1064 FILE *F = emit_env->out;
1065 entity *ent = be_Call_get_entity(irn);
1066 char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
1069 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "call %s", get_entity_name(ent));
1072 lc_esnprintf(ia32_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "%1D", get_irn_n(irn, be_pos_Call_ptr));
1075 lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* %+F (be_Call) */", irn);
1081 * Emits code to increase stack pointer.
1083 void emit_be_IncSP(const ir_node *irn, ia32_emit_env_t *emit_env) {
1084 FILE *F = emit_env->out;
1085 unsigned offs = be_get_IncSP_offset(irn);
1086 be_stack_dir_t dir = be_get_IncSP_direction(irn);
1087 char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
1090 lc_esnprintf(ia32_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "add %1S,%s%u", irn,
1091 (dir == be_stack_dir_along) ? " -" : " ", offs);
1092 lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* %+F (IncSP) */", irn);
1095 snprintf(cmd_buf, SNPRINTF_BUF_LEN, " ");
1096 lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* omitted %+F (IncSP) with 0 */", irn);
1103 * Emits code to set stack pointer.
1105 void emit_be_SetSP(const ir_node *irn, ia32_emit_env_t *emit_env) {
1106 FILE *F = emit_env->out;
1107 char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
1109 lc_esnprintf(ia32_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "mov %1D, %3S", irn, irn);
1110 lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* %+F (restore SP) */", irn);
1115 * Emits code for Copy.
1117 void emit_be_Copy(const ir_node *irn, ia32_emit_env_t *emit_env) {
1118 FILE *F = emit_env->out;
1119 char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
1121 lc_esnprintf(ia32_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "mov %1D, %1S", irn, irn);
1122 lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* %+F */", irn);
1127 * Emits code for exchange.
1129 void emit_be_Perm(const ir_node *irn, ia32_emit_env_t *emit_env) {
1130 FILE *F = emit_env->out;
1131 char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
1133 lc_esnprintf(ia32_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "xchg %1S, %2S", irn, irn);
1134 lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* %+F(%1A, %2A) */", irn, irn, irn);
1138 /***********************************************************************************
1141 * _ __ ___ __ _ _ _ __ | |_ _ __ __ _ _ __ ___ _____ _____ _ __| | __
1142 * | '_ ` _ \ / _` | | '_ \ | _| '__/ _` | '_ ` _ \ / _ \ \ /\ / / _ \| '__| |/ /
1143 * | | | | | | (_| | | | | | | | | | | (_| | | | | | | __/\ V V / (_) | | | <
1144 * |_| |_| |_|\__,_|_|_| |_| |_| |_| \__,_|_| |_| |_|\___| \_/\_/ \___/|_| |_|\_\
1146 ***********************************************************************************/
1149 * Enters the emitter functions for handled nodes into the generic
1150 * pointer of an opcode.
1152 static void ia32_register_emitters(void) {
1154 #define IA32_EMIT(a) op_ia32_##a->ops.generic = (op_func)emit_ia32_##a
1155 #define EMIT(a) op_##a->ops.generic = (op_func)emit_##a
1156 #define BE_EMIT(a) op_be_##a->ops.generic = (op_func)emit_be_##a
1158 /* first clear the generic function pointer for all ops */
1159 clear_irp_opcodes_generic_func();
1161 /* register all emitter functions defined in spec */
1162 ia32_register_spec_emitters();
1164 /* other ia32 emitter functions */
1167 IA32_EMIT(SwitchJmp);
1170 IA32_EMIT(Conv_I2FP);
1171 IA32_EMIT(Conv_FP2I);
1172 IA32_EMIT(Conv_FP2FP);
1174 /* benode emitter */
1191 * Emits code for a node.
1193 static void ia32_emit_node(const ir_node *irn, void *env) {
1194 ia32_emit_env_t *emit_env = env;
1195 firm_dbg_module_t *mod = emit_env->mod;
1196 FILE *F = emit_env->out;
1197 ir_op *op = get_irn_op(irn);
1199 DBG((mod, LEVEL_1, "emitting code for %+F\n", irn));
1201 if (op->ops.generic) {
1202 void (*emit)(const ir_node *, void *) = (void (*)(const ir_node *, void *))op->ops.generic;
1206 ir_fprintf(F, "\t%35s /* %+F */\n", " ", irn);
1211 * Walks over the nodes in a block connected by scheduling edges
1212 * and emits code for each node.
1214 static void ia32_gen_block(ir_node *block, void *env) {
1217 if (! is_Block(block))
1220 fprintf(((ia32_emit_env_t *)env)->out, BLOCK_PREFIX("%ld:\n"), get_irn_node_nr(block));
1221 sched_foreach(block, irn) {
1222 ia32_emit_node(irn, env);
1227 * Emits code for function start.
1229 static void ia32_emit_func_prolog(FILE *F, ir_graph *irg) {
1230 entity *irg_ent = get_irg_entity(irg);
1231 const char *irg_name = get_entity_name(irg_ent);
1233 fprintf(F, "\t.text\n");
1234 if (get_entity_visibility(irg_ent) == visibility_external_visible) {
1235 fprintf(F, ".globl %s\n", irg_name);
1237 fprintf(F, "\t.type\t%s, @function\n", irg_name);
1238 fprintf(F, "%s:\n", irg_name);
1242 * Emits code for function end
1244 static void ia32_emit_func_epilog(FILE *F, ir_graph *irg) {
1245 const char *irg_name = get_entity_name(get_irg_entity(irg));
1247 fprintf(F, "\tret\n");
1248 fprintf(F, "\t.size\t%s, .-%s\n\n", irg_name, irg_name);
1253 * Sets labels for control flow nodes (jump target)
1254 * TODO: Jump optimization
1256 static void ia32_gen_labels(ir_node *block, void *env) {
1258 int n = get_Block_n_cfgpreds(block);
1260 for (n--; n >= 0; n--) {
1261 pred = get_Block_cfgpred(block, n);
1262 set_irn_link(pred, block);
1272 * Ext-Block walker: create a block schedule
1274 static void create_block_list(ir_extblk *blk, void *env) {
1278 for (i = 0, n = get_extbb_n_blocks(blk); i < n; ++i) {
1279 ir_node *block = get_extbb_block(blk, i);
1281 set_irn_link(block, NULL);
1283 set_irn_link(list->end, block);
1285 list->start = block;
1292 * Main driver. Emits the code for one routine.
1294 void ia32_gen_routine(FILE *F, ir_graph *irg, const ia32_code_gen_t *cg) {
1295 ia32_emit_env_t emit_env;
1299 emit_env.mod = firm_dbg_register("firm.be.ia32.emitter");
1301 emit_env.arch_env = cg->arch_env;
1303 emit_env.isa = (ia32_isa_t *)cg->arch_env->isa;
1305 /* set the global arch_env (needed by print hooks) */
1306 arch_env = cg->arch_env;
1308 ia32_register_emitters();
1310 ia32_emit_func_prolog(F, irg);
1311 irg_block_walk_graph(irg, ia32_gen_labels, NULL, &emit_env);
1313 if (cg->opt.extbb) {
1314 /* schedule extended basic blocks */
1320 irg_extblock_walk_graph(irg, NULL, create_block_list, &list);
1322 have_block_sched = 1;
1323 for (block = list.start; block; block = get_irn_link(block))
1324 ia32_gen_block(block, &emit_env);
1327 /* "normal" block schedule */
1329 have_block_sched = 0;
1330 irg_walk_blkwise_graph(irg, NULL, ia32_gen_block, &emit_env);
1333 ia32_emit_func_epilog(F, irg);