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 "gen_ia32_regalloc_if.h"
30 #include "ia32_nodes_attr.h"
31 #include "ia32_new_nodes.h"
32 #include "ia32_map_regs.h"
34 #ifdef obstack_chunk_alloc
35 # undef obstack_chunk_alloc
36 # define obstack_chunk_alloc xmalloc
38 # define obstack_chunk_alloc xmalloc
39 # define obstack_chunk_free free
42 #define BLOCK_PREFIX(x) ".L" x
44 extern int obstack_printf(struct obstack *obst, char *fmt, ...);
46 #define SNPRINTF_BUF_LEN 128
48 /* global arch_env for lc_printf functions */
49 static const arch_env_t *arch_env = NULL;
51 /* indicates whether blocks are scheduled or not
52 (this variable is set automatically) */
53 static int have_block_sched = 0;
55 /*************************************************************
57 * (_) | | / _| | | | |
58 * _ __ _ __ _ _ __ | |_| |_ | |__ ___| |_ __ ___ _ __
59 * | '_ \| '__| | '_ \| __| _| | '_ \ / _ \ | '_ \ / _ \ '__|
60 * | |_) | | | | | | | |_| | | | | | __/ | |_) | __/ |
61 * | .__/|_| |_|_| |_|\__|_| |_| |_|\___|_| .__/ \___|_|
64 *************************************************************/
66 /* We always pass the ir_node which is a pointer. */
67 static int ia32_get_arg_type(const lc_arg_occ_t *occ) {
68 return lc_arg_type_ptr;
73 * Returns the register at in position pos.
75 static const arch_register_t *get_in_reg(const ir_node *irn, int pos) {
77 const arch_register_t *reg = NULL;
79 assert(get_irn_arity(irn) > pos && "Invalid IN position");
81 /* The out register of the operator at position pos is the
82 in register we need. */
83 op = get_irn_n(irn, pos);
85 reg = arch_get_irn_register(arch_env, op);
87 assert(reg && "no in register found");
92 * Returns the register at out position pos.
94 static const arch_register_t *get_out_reg(const ir_node *irn, int pos) {
96 const arch_register_t *reg = NULL;
98 /* 1st case: irn is not of mode_T, so it has only */
99 /* one OUT register -> good */
100 /* 2nd case: irn is of mode_T -> collect all Projs and ask the */
101 /* Proj with the corresponding projnum for the register */
103 if (get_irn_mode(irn) != mode_T) {
104 reg = arch_get_irn_register(arch_env, irn);
106 else if (is_ia32_irn(irn)) {
107 reg = get_ia32_out_reg(irn, pos);
110 const ir_edge_t *edge;
112 foreach_out_edge(irn, edge) {
113 proj = get_edge_src_irn(edge);
114 assert(is_Proj(proj) && "non-Proj from mode_T node");
115 if (get_Proj_proj(proj) == pos) {
116 reg = arch_get_irn_register(arch_env, proj);
122 assert(reg && "no out register found");
132 * Returns the name of the in register at position pos.
134 static const char *get_ia32_reg_name(ir_node *irn, int pos, enum io_direction in_out) {
135 const arch_register_t *reg;
137 static char *buf = NULL;
140 if (in_out == IN_REG) {
141 reg = get_in_reg(irn, pos);
144 /* destination address mode nodes don't have outputs */
145 if (is_ia32_irn(irn) && get_ia32_op_type(irn) == ia32_AddrModeD) {
149 reg = get_out_reg(irn, pos);
152 name = arch_register_get_name(reg);
158 len = strlen(name) + 2;
159 buf = xcalloc(1, len);
161 snprintf(buf, len, "%%%s", name);
167 * Get the register name for a node.
169 static int ia32_get_reg_name(lc_appendable_t *app,
170 const lc_arg_occ_t *occ, const lc_arg_value_t *arg)
173 ir_node *X = arg->v_ptr;
174 int nr = occ->width - 1;
177 return lc_appendable_snadd(app, "(null)", 6);
179 buf = get_ia32_reg_name(X, nr, occ->conversion == 'S' ? IN_REG : OUT_REG);
181 return lc_appendable_snadd(app, buf, strlen(buf));
185 * Returns the tarval, offset or scale of an ia32 as a string.
187 static int ia32_const_to_str(lc_appendable_t *app,
188 const lc_arg_occ_t *occ, const lc_arg_value_t *arg)
191 ir_node *X = arg->v_ptr;
194 return lc_arg_append(app, occ, "(null)", 6);
196 if (occ->conversion == 'C') {
197 buf = get_ia32_cnst(X);
200 buf = get_ia32_am_offs(X);
203 return buf ? lc_appendable_snadd(app, buf, strlen(buf)) : 0;
207 * Determines the SSE suffix depending on the mode.
209 static int ia32_get_mode_suffix(lc_appendable_t *app,
210 const lc_arg_occ_t *occ, const lc_arg_value_t *arg)
212 ir_node *X = arg->v_ptr;
213 ir_mode *mode = get_irn_mode(X);
215 if (mode == mode_T) {
216 mode = is_ia32_AddrModeS(X) || is_ia32_AddrModeD(X) ? get_ia32_ls_mode(X) : get_ia32_res_mode(X);
220 return lc_arg_append(app, occ, "(null)", 6);
222 if (mode_is_float(mode)) {
223 return lc_appendable_chadd(app, get_mode_size_bits(mode) == 32 ? 's' : 'd');
226 return lc_appendable_chadd(app, mode_is_signed(mode) ? 's' : 'z');
231 * Return the ia32 printf arg environment.
232 * We use the firm environment with some additional handlers.
234 const lc_arg_env_t *ia32_get_arg_env(void) {
235 static lc_arg_env_t *env = NULL;
237 static const lc_arg_handler_t ia32_reg_handler = { ia32_get_arg_type, ia32_get_reg_name };
238 static const lc_arg_handler_t ia32_const_handler = { ia32_get_arg_type, ia32_const_to_str };
239 static const lc_arg_handler_t ia32_mode_handler = { ia32_get_arg_type, ia32_get_mode_suffix };
242 /* extend the firm printer */
243 env = firm_get_arg_env();
245 lc_arg_register(env, "ia32:sreg", 'S', &ia32_reg_handler);
246 lc_arg_register(env, "ia32:dreg", 'D', &ia32_reg_handler);
247 lc_arg_register(env, "ia32:cnst", 'C', &ia32_const_handler);
248 lc_arg_register(env, "ia32:offs", 'O', &ia32_const_handler);
249 lc_arg_register(env, "ia32:mode", 'M', &ia32_mode_handler);
255 static char *ia32_get_reg_name_for_mode(ia32_emit_env_t *env, ir_mode *mode, const arch_register_t *reg) {
256 switch(get_mode_size_bits(mode)) {
258 return ia32_get_mapped_reg_name(env->isa->regs_8bit, reg);
260 return ia32_get_mapped_reg_name(env->isa->regs_16bit, reg);
262 return (char *)arch_register_get_name(reg);
267 * Emits registers and/or address mode of a binary operation.
269 char *ia32_emit_binop(const ir_node *n, ia32_emit_env_t *env) {
270 static char *buf = NULL;
272 /* verify that this function is never called on non-AM supporting operations */
273 //assert(get_ia32_am_support(n) != ia32_am_None && "emit binop expects addressmode support");
275 #define PRODUCES_RESULT(n) \
276 (!(is_ia32_St(n) || \
277 is_ia32_Store8Bit(n) || \
278 is_ia32_CondJmp(n) || \
279 is_ia32_fCondJmp(n) || \
280 is_ia32_SwitchJmp(n)))
283 buf = xcalloc(1, SNPRINTF_BUF_LEN);
286 memset(buf, 0, SNPRINTF_BUF_LEN);
289 switch(get_ia32_op_type(n)) {
291 if (get_ia32_cnst(n)) {
292 lc_esnprintf(ia32_get_arg_env(), buf, SNPRINTF_BUF_LEN, "%3S, %s", n, get_ia32_cnst(n));
295 const arch_register_t *in1 = get_in_reg(n, 2);
296 const arch_register_t *in2 = get_in_reg(n, 3);
297 const arch_register_t *out = PRODUCES_RESULT(n) ? get_out_reg(n, 0) : NULL;
298 const arch_register_t *in;
300 in = out ? (REGS_ARE_EQUAL(out, in2) ? in1 : in2) : in2;
301 out = out ? out : in1;
303 snprintf(buf, SNPRINTF_BUF_LEN, "%%%s, %%%s", \
304 arch_register_get_name(out), arch_register_get_name(in));
308 lc_esnprintf(ia32_get_arg_env(), buf, SNPRINTF_BUF_LEN, "%4S, %s", n, ia32_emit_am(n, env));
311 if (get_ia32_cnst(n)) {
312 lc_esnprintf(ia32_get_arg_env(), buf, SNPRINTF_BUF_LEN, "%s,%s%s",
313 ia32_emit_am(n, env),
314 get_ia32_sc(n) ? " OFFSET FLAT:" : " ", /* In case of a symconst we must add OFFSET to */
315 get_ia32_cnst(n)); /* tell the assembler to store it's address. */
318 const arch_register_t *in1 = get_in_reg(n, 2);
319 ir_mode *mode = get_ia32_res_mode(n);
321 mode = mode ? mode : get_ia32_ls_mode(n);
322 lc_esnprintf(ia32_get_arg_env(), buf, SNPRINTF_BUF_LEN, "%s, %%%s",
323 ia32_emit_am(n, env), ia32_get_reg_name_for_mode(env, mode, in1));
327 assert(0 && "unsupported op type");
330 #undef PRODUCES_RESULT
336 * Emits registers and/or address mode of a unary operation.
338 char *ia32_emit_unop(const ir_node *n, ia32_emit_env_t *env) {
339 static char *buf = NULL;
342 buf = xcalloc(1, SNPRINTF_BUF_LEN);
345 memset(buf, 0, SNPRINTF_BUF_LEN);
348 switch(get_ia32_op_type(n)) {
350 lc_esnprintf(ia32_get_arg_env(), buf, SNPRINTF_BUF_LEN, "%1D", n);
353 snprintf(buf, SNPRINTF_BUF_LEN, ia32_emit_am(n, env));
356 assert(0 && "unsupported op type");
363 * Emits address mode.
365 char *ia32_emit_am(const ir_node *n, ia32_emit_env_t *env) {
366 ia32_am_flavour_t am_flav = get_ia32_am_flavour(n);
370 static struct obstack *obst = NULL;
371 ir_mode *mode = get_ia32_ls_mode(n);
373 if (! is_ia32_Lea(n))
374 assert(mode && "AM node must have ls_mode attribute set.");
377 obst = xcalloc(1, sizeof(*obst));
380 obstack_free(obst, NULL);
383 /* obstack_free with NULL results in an uninitialized obstack */
387 switch (get_mode_size_bits(mode)) {
389 obstack_printf(obst, "BYTE PTR ");
392 obstack_printf(obst, "WORD PTR ");
395 obstack_printf(obst, "DWORD PTR ");
402 obstack_printf(obst, "[");
404 if (am_flav & ia32_B) {
405 lc_eoprintf(ia32_get_arg_env(), obst, "%1S", n);
409 if (am_flav & ia32_I) {
411 obstack_printf(obst, "+");
414 lc_eoprintf(ia32_get_arg_env(), obst, "%2S", n);
416 if (am_flav & ia32_S) {
417 obstack_printf(obst, "*%d", 1 << get_ia32_am_scale(n));
423 if (am_flav & ia32_O) {
424 obstack_printf(obst, get_ia32_am_offs(n));
427 obstack_printf(obst, "] ");
429 size = obstack_object_size(obst);
430 s = obstack_finish(obst);
439 * Formated print of commands and comments.
441 static void ia32_fprintf_format(FILE *F, const ir_node *irn, char *cmd_buf, char *cmnt_buf) {
443 const char *name = irn ? be_retrieve_dbg_info(get_irn_dbg_info((ir_node *)irn), &lineno) : NULL;
446 fprintf(F, "\t%-35s %-60s /* %s:%u */\n", cmd_buf, cmnt_buf, name, lineno);
448 fprintf(F, "\t%-35s %-60s\n", cmd_buf, cmnt_buf);
454 * Add a number to a prefix. This number will not be used a second time.
456 static char *get_unique_label(char *buf, size_t buflen, const char *prefix) {
457 static unsigned long id = 0;
458 snprintf(buf, buflen, "%s%lu", prefix, ++id);
464 /*************************************************
467 * ___ _ __ ___ _| |_ ___ ___ _ __ __| |
468 * / _ \ '_ ` _ \| | __| / __/ _ \| '_ \ / _` |
469 * | __/ | | | | | | |_ | (_| (_) | | | | (_| |
470 * \___|_| |_| |_|_|\__| \___\___/|_| |_|\__,_|
472 *************************************************/
475 #define IA32_DO_EMIT(irn) ia32_fprintf_format(F, irn, cmd_buf, cmnt_buf)
478 * coding of conditions
480 struct cmp2conditon_t {
486 * positive conditions for signed compares
488 static const struct cmp2conditon_t cmp2condition_s[] = {
489 { NULL, pn_Cmp_False }, /* always false */
490 { "e", pn_Cmp_Eq }, /* == */
491 { "l", pn_Cmp_Lt }, /* < */
492 { "le", pn_Cmp_Le }, /* <= */
493 { "g", pn_Cmp_Gt }, /* > */
494 { "ge", pn_Cmp_Ge }, /* >= */
495 { "ne", pn_Cmp_Lg }, /* != */
496 { "ordered", pn_Cmp_Leg }, /* Floating point: ordered */
497 { "unordered", pn_Cmp_Uo }, /* FLoting point: unordered */
498 { "unordered or ==", pn_Cmp_Ue }, /* Floating point: unordered or == */
499 { "unordered or <", pn_Cmp_Ul }, /* Floating point: unordered or < */
500 { "unordered or <=", pn_Cmp_Ule }, /* Floating point: unordered or <= */
501 { "unordered or >", pn_Cmp_Ug }, /* Floating point: unordered or > */
502 { "unordered or >=", pn_Cmp_Uge }, /* Floating point: unordered or >= */
503 { "unordered or !=", pn_Cmp_Ne }, /* Floating point: unordered or != */
504 { NULL, pn_Cmp_True }, /* always true */
508 * positive conditions for unsigned compares
510 static const struct cmp2conditon_t cmp2condition_u[] = {
511 { NULL, pn_Cmp_False }, /* always false */
512 { "e", pn_Cmp_Eq }, /* == */
513 { "b", pn_Cmp_Lt }, /* < */
514 { "be", pn_Cmp_Le }, /* <= */
515 { "a", pn_Cmp_Gt }, /* > */
516 { "ae", pn_Cmp_Ge }, /* >= */
517 { "ne", pn_Cmp_Lg }, /* != */
518 { "ordered", pn_Cmp_Leg }, /* Floating point: ordered */
519 { "unordered", pn_Cmp_Uo }, /* FLoting point: unordered */
520 { "unordered or ==", pn_Cmp_Ue }, /* Floating point: unordered or == */
521 { "unordered or <", pn_Cmp_Ul }, /* Floating point: unordered or < */
522 { "unordered or <=", pn_Cmp_Ule }, /* Floating point: unordered or <= */
523 { "unordered or >", pn_Cmp_Ug }, /* Floating point: unordered or > */
524 { "unordered or >=", pn_Cmp_Uge }, /* Floating point: unordered or >= */
525 { "unordered or !=", pn_Cmp_Ne }, /* Floating point: unordered or != */
526 { NULL, pn_Cmp_True }, /* always true */
530 * returns the condition code
532 static const char *get_cmp_suffix(int cmp_code, int unsigned_cmp)
534 assert(cmp2condition_s[cmp_code].num == cmp_code);
535 assert(cmp2condition_u[cmp_code].num == cmp_code);
537 return unsigned_cmp ? cmp2condition_u[cmp_code & 7].name : cmp2condition_s[cmp_code & 7].name;
541 * Returns the target block for a control flow node.
543 static ir_node *get_cfop_target_block(const ir_node *irn) {
544 return get_irn_link(irn);
548 * Returns the target label for a control flow node.
550 static char *get_cfop_target(const ir_node *irn, char *buf) {
551 ir_node *bl = get_cfop_target_block(irn);
553 snprintf(buf, SNPRINTF_BUF_LEN, BLOCK_PREFIX("%ld"), get_irn_node_nr(bl));
557 /** Return the next block in Block schedule */
558 static ir_node *next_blk_sched(const ir_node *block) {
559 return have_block_sched ? get_irn_link(block) : NULL;
563 * Emits the jump sequence for a conditional jump (cmp + jmp_true + jmp_false)
565 static void finish_CondJmp(FILE *F, const ir_node *irn, ir_mode *mode) {
566 const ir_node *proj1, *proj2 = NULL;
567 const ir_node *block, *next_bl = NULL;
568 const ir_edge_t *edge;
569 char buf[SNPRINTF_BUF_LEN];
570 char cmd_buf[SNPRINTF_BUF_LEN];
571 char cmnt_buf[SNPRINTF_BUF_LEN];
573 /* get both Proj's */
574 edge = get_irn_out_edge_first(irn);
575 proj1 = get_edge_src_irn(edge);
576 assert(is_Proj(proj1) && "CondJmp with a non-Proj");
578 edge = get_irn_out_edge_next(irn, edge);
580 proj2 = get_edge_src_irn(edge);
581 assert(is_Proj(proj2) && "CondJmp with a non-Proj");
584 /* for now, the code works for scheduled and non-schedules blocks */
585 block = get_nodes_block(irn);
587 /* we have a block schedule */
588 next_bl = next_blk_sched(block);
590 if (get_cfop_target_block(proj1) == next_bl) {
591 /* exchange both proj's so the second one can be omitted */
592 const ir_node *t = proj1;
598 /* the first Proj must always be created */
599 if (get_Proj_proj(proj1) == pn_Cond_true) {
600 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "j%s %s",
601 get_cmp_suffix(get_ia32_pncode(irn), !mode_is_signed(get_irn_mode(get_irn_n(irn, 0)))),
602 get_cfop_target(proj1, buf));
603 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* cmp(a, b) == TRUE */");
606 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "j%s %s",
607 get_cmp_suffix(get_negated_pnc(get_ia32_pncode(irn), mode),
608 !mode_is_signed(get_irn_mode(get_irn_n(irn, 0)))),
609 get_cfop_target(proj1, buf));
610 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* cmp(a, b) == FALSE */");
614 /* the second Proj might be a fallthrough */
616 if (get_cfop_target_block(proj2) != next_bl) {
617 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "jmp %s", get_cfop_target(proj2, buf));
618 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* otherwise */");
622 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* fallthrogh %s */", get_cfop_target(proj2, buf));
629 * Emits code for conditional jump.
631 static void CondJmp_emitter(const ir_node *irn, ia32_emit_env_t *env) {
633 char cmd_buf[SNPRINTF_BUF_LEN];
634 char cmnt_buf[SNPRINTF_BUF_LEN];
636 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "cmp %s", ia32_emit_binop(irn, env));
637 lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* %+F */", irn);
639 finish_CondJmp(F, irn, get_irn_mode(get_irn_n(irn, 2)));
643 * Emits code for conditional jump with two variables.
645 static void emit_ia32_CondJmp(const ir_node *irn, ia32_emit_env_t *env) {
646 CondJmp_emitter(irn, env);
650 * Emits code for conditional jump with immediate.
652 void emit_ia32_CondJmp_i(const ir_node *irn, ia32_emit_env_t *env) {
653 CondJmp_emitter(irn, env);
657 * Emits code for conditional test and jump.
659 static void TestJmp_emitter(const ir_node *irn, ia32_emit_env_t *env) {
661 const char *op1 = arch_register_get_name(get_in_reg(irn, 0));
662 const char *op2 = get_ia32_cnst(irn);
663 char cmd_buf[SNPRINTF_BUF_LEN];
664 char cmnt_buf[SNPRINTF_BUF_LEN];
667 op2 = arch_register_get_name(get_in_reg(irn, 1));
669 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "test %%%s,%s%s ", op1, get_ia32_cnst(irn) ? " " : " %", op2);
670 lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* %+F */", irn);
673 finish_CondJmp(F, irn, get_irn_mode(get_irn_n(irn, 0)));
677 * Emits code for conditional test and jump with two variables.
679 static void emit_ia32_TestJmp(const ir_node *irn, ia32_emit_env_t *env) {
680 TestJmp_emitter(irn, env);
683 static void emit_ia32_CJmp(const ir_node *irn, ia32_emit_env_t *env) {
685 char cmd_buf[SNPRINTF_BUF_LEN];
686 char cmnt_buf[SNPRINTF_BUF_LEN];
688 snprintf(cmd_buf, SNPRINTF_BUF_LEN, " ");
689 lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* %+F omitted redundant test/cmp */", irn);
691 finish_CondJmp(F, irn, get_irn_mode(get_irn_n(irn, 0)));
694 static void emit_ia32_CJmpAM(const ir_node *irn, ia32_emit_env_t *env) {
696 char cmd_buf[SNPRINTF_BUF_LEN];
697 char cmnt_buf[SNPRINTF_BUF_LEN];
699 snprintf(cmd_buf, SNPRINTF_BUF_LEN, " ");
700 lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* %+F omitted redundant test/cmp */", irn);
702 finish_CondJmp(F, irn, get_irn_mode(get_irn_n(irn, 2)));
705 /*********************************************************
708 * ___ _ __ ___ _| |_ _ _ _ _ __ ___ _ __ ___
709 * / _ \ '_ ` _ \| | __| | | | | | '_ ` _ \| '_ \/ __|
710 * | __/ | | | | | | |_ | | |_| | | | | | | |_) \__ \
711 * \___|_| |_| |_|_|\__| | |\__,_|_| |_| |_| .__/|___/
714 *********************************************************/
716 /* jump table entry (target and corresponding number) */
717 typedef struct _branch_t {
722 /* jump table for switch generation */
723 typedef struct _jmp_tbl_t {
724 ir_node *defProj; /**< default target */
725 int min_value; /**< smallest switch case */
726 int max_value; /**< largest switch case */
727 int num_branches; /**< number of jumps */
728 char *label; /**< label of the jump table */
729 branch_t *branches; /**< jump array */
733 * Compare two variables of type branch_t. Used to sort all switch cases
735 static int ia32_cmp_branch_t(const void *a, const void *b) {
736 branch_t *b1 = (branch_t *)a;
737 branch_t *b2 = (branch_t *)b;
739 if (b1->value <= b2->value)
746 * Emits code for a SwitchJmp (creates a jump table if
747 * possible otherwise a cmp-jmp cascade). Port from
750 void emit_ia32_SwitchJmp(const ir_node *irn, ia32_emit_env_t *emit_env) {
751 unsigned long interval;
752 char buf[SNPRINTF_BUF_LEN];
753 int last_value, i, pn, do_jmp_tbl = 1;
756 const ir_edge_t *edge;
757 const lc_arg_env_t *env = ia32_get_arg_env();
758 FILE *F = emit_env->out;
759 char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
761 /* fill the table structure */
762 tbl.label = xmalloc(SNPRINTF_BUF_LEN);
763 tbl.label = get_unique_label(tbl.label, SNPRINTF_BUF_LEN, "JMPTBL_");
765 tbl.num_branches = get_irn_n_edges(irn);
766 tbl.branches = xcalloc(tbl.num_branches, sizeof(tbl.branches[0]));
767 tbl.min_value = INT_MAX;
768 tbl.max_value = INT_MIN;
771 /* go over all proj's and collect them */
772 foreach_out_edge(irn, edge) {
773 proj = get_edge_src_irn(edge);
774 assert(is_Proj(proj) && "Only proj allowed at SwitchJmp");
776 pn = get_Proj_proj(proj);
778 /* create branch entry */
779 tbl.branches[i].target = proj;
780 tbl.branches[i].value = pn;
782 tbl.min_value = pn < tbl.min_value ? pn : tbl.min_value;
783 tbl.max_value = pn > tbl.max_value ? pn : tbl.max_value;
785 /* check for default proj */
786 if (pn == get_ia32_pncode(irn)) {
787 assert(tbl.defProj == NULL && "found two defProjs at SwitchJmp");
794 /* sort the branches by their number */
795 qsort(tbl.branches, tbl.num_branches, sizeof(tbl.branches[0]), ia32_cmp_branch_t);
797 /* two-complement's magic make this work without overflow */
798 interval = tbl.max_value - tbl.min_value;
800 /* check value interval */
801 if (interval > 16 * 1024) {
805 /* check ratio of value interval to number of branches */
806 if ((float)(interval + 1) / (float)tbl.num_branches > 8.0) {
812 if (tbl.min_value != 0) {
813 lc_esnprintf(env, cmd_buf, SNPRINTF_BUF_LEN, "cmpl %lu, -%d(%1S)",
814 interval, tbl.min_value, irn);
815 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* first switch value is not 0 */");
820 lc_esnprintf(env, cmd_buf, SNPRINTF_BUF_LEN, "cmpl %lu, %1S", interval, irn);
821 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* compare for switch */");
826 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "ja %s", get_cfop_target(tbl.defProj, buf));
827 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* default jump if out of range */");
830 if (tbl.num_branches > 1) {
833 lc_esnprintf(env, cmd_buf, SNPRINTF_BUF_LEN, "jmp [%1S*4+%s]", irn, tbl.label);
834 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* get jump table entry as target */");
837 fprintf(F, "\t.section\t.rodata\n");
838 fprintf(F, "\t.align 4\n");
840 fprintf(F, "%s:\n", tbl.label);
842 snprintf(cmd_buf, SNPRINTF_BUF_LEN, ".long %s", get_cfop_target(tbl.branches[0].target, buf));
843 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* case %d */\n", tbl.branches[0].value);
846 last_value = tbl.branches[0].value;
847 for (i = 1; i < tbl.num_branches; ++i) {
848 while (++last_value < tbl.branches[i].value) {
849 snprintf(cmd_buf, SNPRINTF_BUF_LEN, ".long %s", get_cfop_target(tbl.defProj, buf));
850 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* default case */");
853 snprintf(cmd_buf, SNPRINTF_BUF_LEN, ".long %s", get_cfop_target(tbl.branches[i].target, buf));
854 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* case %d */", last_value);
858 fprintf(F, "\t.text");
861 /* one jump is enough */
862 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "jmp %s", get_cfop_target(tbl.branches[0].target, buf));
863 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* only one case given */");
867 else { // no jump table
868 for (i = 0; i < tbl.num_branches; ++i) {
869 lc_esnprintf(env, cmd_buf, SNPRINTF_BUF_LEN, "cmpl %d, %1S", tbl.branches[i].value, irn);
870 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* case %d */", i);
872 fprintf(F, "\tje %s\n", get_cfop_target(tbl.branches[i].target, buf));
875 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "jmp %s", get_cfop_target(tbl.defProj, buf));
876 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* default case */");
887 * Emits code for a unconditional jump.
889 void emit_Jmp(const ir_node *irn, ia32_emit_env_t *env) {
890 ir_node *block, *next_bl;
892 char buf[SNPRINTF_BUF_LEN], cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
894 /* for now, the code works for scheduled and non-schedules blocks */
895 block = get_nodes_block(irn);
897 /* we have a block schedule */
898 next_bl = next_blk_sched(block);
899 if (get_cfop_target_block(irn) != next_bl) {
900 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "jmp %s", get_cfop_target(irn, buf));
901 lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* %+F(%+F) */", irn, get_cfop_target_block(irn));
905 lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* fallthrough %s */", get_cfop_target(irn, buf));
910 /****************************
913 * _ __ _ __ ___ _ ___
914 * | '_ \| '__/ _ \| |/ __|
915 * | |_) | | | (_) | |\__ \
916 * | .__/|_| \___/| ||___/
919 ****************************/
922 * Emits code for a proj -> node
924 void emit_Proj(const ir_node *irn, ia32_emit_env_t *env) {
925 ir_node *pred = get_Proj_pred(irn);
927 if (get_irn_op(pred) == op_Start) {
928 switch(get_Proj_proj(irn)) {
929 case pn_Start_X_initial_exec:
938 /**********************************
941 * | | ___ _ __ _ _| |_) |
942 * | | / _ \| '_ \| | | | _ <
943 * | |___| (_) | |_) | |_| | |_) |
944 * \_____\___/| .__/ \__, |____/
947 **********************************/
950 * Emit movsb/w instructions to make mov count divideable by 4
952 static void emit_CopyB_prolog(FILE *F, int rem, int size) {
953 char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
955 fprintf(F, "\t/* memcopy %d bytes*/\n", size);
957 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "cld");
958 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* copy direction forward*/");
963 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "movsb");
964 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* memcopy remainder 1 */");
967 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "movsw");
968 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* memcopy remainder 2 */");
971 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "movsb");
972 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* memcopy remainder 3 */");
974 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "movsw");
975 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* memcopy remainder 3 */");
983 * Emit rep movsd instruction for memcopy.
985 void emit_ia32_CopyB(const ir_node *irn, ia32_emit_env_t *emit_env) {
986 FILE *F = emit_env->out;
987 tarval *tv = get_ia32_Immop_tarval(irn);
988 int rem = get_tarval_long(tv);
989 int size = get_tarval_long(get_ia32_Immop_tarval(get_irn_n(irn, 2)));
990 char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
992 emit_CopyB_prolog(F, rem, size);
994 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "rep movsd");
995 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* memcopy */");
1000 * Emits unrolled memcopy.
1002 void emit_ia32_CopyB_i(const ir_node *irn, ia32_emit_env_t *emit_env) {
1003 tarval *tv = get_ia32_Immop_tarval(irn);
1004 int size = get_tarval_long(tv);
1005 FILE *F = emit_env->out;
1006 char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
1008 emit_CopyB_prolog(F, size & 0x3, size);
1012 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "movsd");
1013 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* memcopy unrolled */");
1020 /***************************
1024 * | | / _ \| '_ \ \ / /
1025 * | |___| (_) | | | \ V /
1026 * \_____\___/|_| |_|\_/
1028 ***************************/
1031 * Emit code for conversions (I, FP), (FP, I) and (FP, FP).
1033 static void emit_ia32_Conv_with_FP(const ir_node *irn, ia32_emit_env_t *emit_env) {
1034 FILE *F = emit_env->out;
1035 const lc_arg_env_t *env = ia32_get_arg_env();
1036 char *from, *to, buf[64];
1037 ir_mode *src_mode, *tgt_mode;
1038 char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
1040 src_mode = is_ia32_AddrModeS(irn) ? get_ia32_ls_mode(irn) : get_irn_mode(get_irn_n(irn, 2));
1041 tgt_mode = get_ia32_res_mode(irn);
1043 from = mode_is_float(src_mode) ? (get_mode_size_bits(src_mode) == 32 ? "ss" : "sd") : "si";
1044 to = mode_is_float(tgt_mode) ? (get_mode_size_bits(tgt_mode) == 32 ? "ss" : "sd") : "si";
1046 switch(get_ia32_op_type(irn)) {
1048 lc_esnprintf(env, buf, sizeof(buf), "%1D, %3S", irn, irn);
1050 case ia32_AddrModeS:
1051 lc_esnprintf(env, buf, sizeof(buf), "%1D, %s", irn, ia32_emit_am(irn, emit_env));
1054 assert(0 && "unsupported op type for Conv");
1057 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "cvt%s2%s %s", from, to, buf);
1058 lc_esnprintf(env, cmnt_buf, SNPRINTF_BUF_LEN, "/* %+F(%+F, %+F) */", irn, src_mode, tgt_mode);
1062 void emit_ia32_Conv_I2FP(const ir_node *irn, ia32_emit_env_t *emit_env) {
1063 emit_ia32_Conv_with_FP(irn, emit_env);
1066 void emit_ia32_Conv_FP2I(const ir_node *irn, ia32_emit_env_t *emit_env) {
1067 emit_ia32_Conv_with_FP(irn, emit_env);
1070 void emit_ia32_Conv_FP2FP(const ir_node *irn, ia32_emit_env_t *emit_env) {
1071 emit_ia32_Conv_with_FP(irn, emit_env);
1075 * Emits code for an Int conversion.
1077 void emit_ia32_Conv_I2I(const ir_node *irn, ia32_emit_env_t *emit_env) {
1078 FILE *F = emit_env->out;
1079 const lc_arg_env_t *env = ia32_get_arg_env();
1080 char *move_cmd, *conv_cmd;
1081 ir_mode *src_mode, *tgt_mode;
1083 char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
1084 const arch_register_t *in_reg, *out_reg;
1086 src_mode = is_ia32_AddrModeS(irn) ? get_ia32_ls_mode(irn) : get_irn_mode(get_irn_n(irn, 2));
1087 tgt_mode = get_ia32_res_mode(irn);
1089 n = get_mode_size_bits(src_mode);
1090 m = get_mode_size_bits(tgt_mode);
1092 if (mode_is_signed(n < m ? src_mode : tgt_mode)) {
1094 if (n == 8 || m == 8)
1096 else if (n == 16 || m == 16)
1099 assert(0 && "unsupported Conv_I2I");
1106 switch(get_ia32_op_type(irn)) {
1108 in_reg = get_in_reg(irn, 2);
1109 out_reg = get_out_reg(irn, 0);
1111 if (REGS_ARE_EQUAL(in_reg, &ia32_gp_regs[REG_EAX]) &&
1112 REGS_ARE_EQUAL(out_reg, in_reg) &&
1113 mode_is_signed(n < m ? src_mode : tgt_mode))
1115 /* argument and result are both in EAX and */
1116 /* signedness is ok: -> use converts */
1117 lc_esnprintf(env, cmd_buf, SNPRINTF_BUF_LEN, "%s", conv_cmd);
1119 else if (REGS_ARE_EQUAL(out_reg, in_reg) &&
1120 ! mode_is_signed(n < m ? src_mode : tgt_mode))
1122 /* argument and result are in the same register */
1123 /* and signedness is ok: -> use and with mask */
1124 int mask = (1 << (n < m ? n : m)) - 1;
1125 lc_esnprintf(env, cmd_buf, SNPRINTF_BUF_LEN, "and %1D, 0x%x", irn, mask);
1128 /* use move w/o sign extension */
1129 lc_esnprintf(env, cmd_buf, SNPRINTF_BUF_LEN, "%s %1D, %%%s",
1130 move_cmd, irn, ia32_get_reg_name_for_mode(emit_env, n < m ? src_mode : tgt_mode, in_reg));
1134 case ia32_AddrModeS:
1135 lc_esnprintf(env, cmd_buf, SNPRINTF_BUF_LEN, "%s %1D, %s",
1136 move_cmd, irn, ia32_emit_am(irn, emit_env));
1139 assert(0 && "unsupported op type for Conv");
1142 lc_esnprintf(env, cmnt_buf, SNPRINTF_BUF_LEN, "/* %+F(%d Bit mode_%F -> %d Bit mode_%F) */",
1143 irn, n, src_mode, m, tgt_mode);
1148 /*******************************************
1151 * | |__ ___ _ __ ___ __| | ___ ___
1152 * | '_ \ / _ \ '_ \ / _ \ / _` |/ _ \/ __|
1153 * | |_) | __/ | | | (_) | (_| | __/\__ \
1154 * |_.__/ \___|_| |_|\___/ \__,_|\___||___/
1156 *******************************************/
1159 * Emits a backend call
1161 void emit_be_Call(const ir_node *irn, ia32_emit_env_t *emit_env) {
1162 FILE *F = emit_env->out;
1163 entity *ent = be_Call_get_entity(irn);
1164 char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
1167 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "call %s", get_entity_name(ent));
1170 lc_esnprintf(ia32_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "%1D", get_irn_n(irn, be_pos_Call_ptr));
1173 lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* %+F (be_Call) */", irn);
1179 * Emits code to increase stack pointer.
1181 void emit_be_IncSP(const ir_node *irn, ia32_emit_env_t *emit_env) {
1182 FILE *F = emit_env->out;
1183 unsigned offs = be_get_IncSP_offset(irn);
1184 be_stack_dir_t dir = be_get_IncSP_direction(irn);
1185 char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
1188 lc_esnprintf(ia32_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "add %1S,%s%u", irn,
1189 (dir == be_stack_dir_along) ? " -" : " ", offs);
1190 lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* %+F (IncSP) */", irn);
1193 snprintf(cmd_buf, SNPRINTF_BUF_LEN, " ");
1194 lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* omitted %+F (IncSP) with 0 */", irn);
1201 * Emits code to set stack pointer.
1203 void emit_be_SetSP(const ir_node *irn, ia32_emit_env_t *emit_env) {
1204 FILE *F = emit_env->out;
1205 char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
1207 lc_esnprintf(ia32_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "mov %1D, %3S", irn, irn);
1208 lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* %+F (restore SP) */", irn);
1213 * Emits code for Copy.
1215 void emit_be_Copy(const ir_node *irn, ia32_emit_env_t *emit_env) {
1216 FILE *F = emit_env->out;
1217 char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
1219 lc_esnprintf(ia32_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "mov %1D, %1S", irn, irn);
1220 lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* %+F */", irn);
1225 * Emits code for exchange.
1227 void emit_be_Perm(const ir_node *irn, ia32_emit_env_t *emit_env) {
1228 FILE *F = emit_env->out;
1229 char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
1231 lc_esnprintf(ia32_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "xchg %1S, %2S", irn, irn);
1232 lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* %+F(%1A, %2A) */", irn, irn, irn);
1236 /***********************************************************************************
1239 * _ __ ___ __ _ _ _ __ | |_ _ __ __ _ _ __ ___ _____ _____ _ __| | __
1240 * | '_ ` _ \ / _` | | '_ \ | _| '__/ _` | '_ ` _ \ / _ \ \ /\ / / _ \| '__| |/ /
1241 * | | | | | | (_| | | | | | | | | | | (_| | | | | | | __/\ V V / (_) | | | <
1242 * |_| |_| |_|\__,_|_|_| |_| |_| |_| \__,_|_| |_| |_|\___| \_/\_/ \___/|_| |_|\_\
1244 ***********************************************************************************/
1247 * Enters the emitter functions for handled nodes into the generic
1248 * pointer of an opcode.
1250 static void ia32_register_emitters(void) {
1252 #define IA32_EMIT(a) op_ia32_##a->ops.generic = (op_func)emit_ia32_##a
1253 #define EMIT(a) op_##a->ops.generic = (op_func)emit_##a
1254 #define BE_EMIT(a) op_be_##a->ops.generic = (op_func)emit_be_##a
1256 /* first clear the generic function pointer for all ops */
1257 clear_irp_opcodes_generic_func();
1259 /* register all emitter functions defined in spec */
1260 ia32_register_spec_emitters();
1262 /* other ia32 emitter functions */
1267 IA32_EMIT(SwitchJmp);
1270 IA32_EMIT(Conv_I2FP);
1271 IA32_EMIT(Conv_FP2I);
1272 IA32_EMIT(Conv_FP2FP);
1273 IA32_EMIT(Conv_I2I);
1275 /* benode emitter */
1292 * Emits code for a node.
1294 static void ia32_emit_node(const ir_node *irn, void *env) {
1295 ia32_emit_env_t *emit_env = env;
1296 firm_dbg_module_t *mod = emit_env->mod;
1297 FILE *F = emit_env->out;
1298 ir_op *op = get_irn_op(irn);
1300 DBG((mod, LEVEL_1, "emitting code for %+F\n", irn));
1302 if (op->ops.generic) {
1303 void (*emit)(const ir_node *, void *) = (void (*)(const ir_node *, void *))op->ops.generic;
1307 ir_fprintf(F, "\t%35s /* %+F */\n", " ", irn);
1312 * Walks over the nodes in a block connected by scheduling edges
1313 * and emits code for each node.
1315 static void ia32_gen_block(ir_node *block, void *env) {
1318 if (! is_Block(block))
1321 fprintf(((ia32_emit_env_t *)env)->out, BLOCK_PREFIX("%ld:\n"), get_irn_node_nr(block));
1322 sched_foreach(block, irn) {
1323 ia32_emit_node(irn, env);
1328 * Emits code for function start.
1330 static void ia32_emit_func_prolog(FILE *F, ir_graph *irg) {
1331 entity *irg_ent = get_irg_entity(irg);
1332 const char *irg_name = get_entity_name(irg_ent);
1334 fprintf(F, "\t.text\n");
1335 if (get_entity_visibility(irg_ent) == visibility_external_visible) {
1336 fprintf(F, ".globl %s\n", irg_name);
1338 fprintf(F, "\t.type\t%s, @function\n", irg_name);
1339 fprintf(F, "%s:\n", irg_name);
1343 * Emits code for function end
1345 static void ia32_emit_func_epilog(FILE *F, ir_graph *irg) {
1346 const char *irg_name = get_entity_name(get_irg_entity(irg));
1348 fprintf(F, "\tret\n");
1349 fprintf(F, "\t.size\t%s, .-%s\n\n", irg_name, irg_name);
1354 * Sets labels for control flow nodes (jump target)
1355 * TODO: Jump optimization
1357 static void ia32_gen_labels(ir_node *block, void *env) {
1359 int n = get_Block_n_cfgpreds(block);
1361 for (n--; n >= 0; n--) {
1362 pred = get_Block_cfgpred(block, n);
1363 set_irn_link(pred, block);
1373 * Ext-Block walker: create a block schedule
1375 static void create_block_list(ir_extblk *blk, void *env) {
1379 for (i = 0, n = get_extbb_n_blocks(blk); i < n; ++i) {
1380 ir_node *block = get_extbb_block(blk, i);
1382 set_irn_link(block, NULL);
1384 set_irn_link(list->end, block);
1386 list->start = block;
1393 * Main driver. Emits the code for one routine.
1395 void ia32_gen_routine(FILE *F, ir_graph *irg, const ia32_code_gen_t *cg) {
1396 ia32_emit_env_t emit_env;
1400 emit_env.mod = firm_dbg_register("firm.be.ia32.emitter");
1402 emit_env.arch_env = cg->arch_env;
1404 emit_env.isa = (ia32_isa_t *)cg->arch_env->isa;
1406 /* set the global arch_env (needed by print hooks) */
1407 arch_env = cg->arch_env;
1409 ia32_register_emitters();
1411 ia32_emit_func_prolog(F, irg);
1412 irg_block_walk_graph(irg, ia32_gen_labels, NULL, &emit_env);
1414 if (cg->opt.extbb) {
1415 /* schedule extended basic blocks */
1421 irg_extblock_walk_graph(irg, NULL, create_block_list, &list);
1423 have_block_sched = 1;
1424 for (block = list.start; block; block = get_irn_link(block))
1425 ia32_gen_block(block, &emit_env);
1428 /* "normal" block schedule */
1430 have_block_sched = 0;
1431 irg_walk_blkwise_graph(irg, NULL, ia32_gen_block, &emit_env);
1434 ia32_emit_func_epilog(F, irg);