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%s",
301 ia32_emit_am(n, env),
302 get_ia32_sc(n) ? " OFFSET FLAT:" : " ", /* In case of a symconst we must add OFFSET to */
303 get_ia32_cnst(n)); /* tell the assembler to store it's address. */
306 const arch_register_t *in1 = get_in_reg(n, 2);
307 const char *reg_name;
308 ir_mode *mode = get_ia32_res_mode(n);
310 mode = mode ? mode : get_ia32_ls_mode(n);
312 switch(get_mode_size_bits(mode)) {
314 reg_name = ia32_get_mapped_reg_name(env->isa->regs_8bit, in1);
317 reg_name = ia32_get_mapped_reg_name(env->isa->regs_16bit, in1);
320 reg_name = arch_register_get_name(in1);
323 assert(0 && "unsupported mode size");
327 lc_esnprintf(ia32_get_arg_env(), buf, SNPRINTF_BUF_LEN, "%s, %%%s", ia32_emit_am(n, env), reg_name);
331 assert(0 && "unsupported op type");
334 #undef PRODUCES_RESULT
340 * Emits registers and/or address mode of a unary operation.
342 char *ia32_emit_unop(const ir_node *n, ia32_emit_env_t *env) {
343 static char *buf = NULL;
346 buf = xcalloc(1, SNPRINTF_BUF_LEN);
349 memset(buf, 0, SNPRINTF_BUF_LEN);
352 switch(get_ia32_op_type(n)) {
354 lc_esnprintf(ia32_get_arg_env(), buf, SNPRINTF_BUF_LEN, "%1D", n);
357 snprintf(buf, SNPRINTF_BUF_LEN, ia32_emit_am(n, env));
360 assert(0 && "unsupported op type");
367 * Emits address mode.
369 char *ia32_emit_am(const ir_node *n, ia32_emit_env_t *env) {
370 ia32_am_flavour_t am_flav = get_ia32_am_flavour(n);
374 static struct obstack *obst = NULL;
375 ir_mode *mode = get_ia32_ls_mode(n);
377 if (! is_ia32_Lea(n))
378 assert(mode && "AM node must have ls_mode attribute set.");
381 obst = xcalloc(1, sizeof(*obst));
384 obstack_free(obst, NULL);
387 /* obstack_free with NULL results in an uninitialized obstack */
391 switch (get_mode_size_bits(mode)) {
393 obstack_printf(obst, "BYTE PTR ");
396 obstack_printf(obst, "WORD PTR ");
399 obstack_printf(obst, "DWORD PTR ");
406 obstack_printf(obst, "[");
408 if (am_flav & ia32_B) {
409 lc_eoprintf(ia32_get_arg_env(), obst, "%1S", n);
413 if (am_flav & ia32_I) {
415 obstack_printf(obst, "+");
418 lc_eoprintf(ia32_get_arg_env(), obst, "%2S", n);
420 if (am_flav & ia32_S) {
421 obstack_printf(obst, "*%d", 1 << get_ia32_am_scale(n));
427 if (am_flav & ia32_O) {
428 obstack_printf(obst, get_ia32_am_offs(n));
431 obstack_printf(obst, "] ");
433 size = obstack_object_size(obst);
434 s = obstack_finish(obst);
443 * Formated print of commands and comments.
445 static void ia32_fprintf_format(FILE *F, char *cmd_buf, char *cmnt_buf) {
446 fprintf(F, "\t%-35s %-60s\n", cmd_buf, cmnt_buf);
452 * Add a number to a prefix. This number will not be used a second time.
454 static char *get_unique_label(char *buf, size_t buflen, const char *prefix) {
455 static unsigned long id = 0;
456 snprintf(buf, buflen, "%s%lu", prefix, ++id);
462 /*************************************************
465 * ___ _ __ ___ _| |_ ___ ___ _ __ __| |
466 * / _ \ '_ ` _ \| | __| / __/ _ \| '_ \ / _` |
467 * | __/ | | | | | | |_ | (_| (_) | | | | (_| |
468 * \___|_| |_| |_|_|\__| \___\___/|_| |_|\__,_|
470 *************************************************/
473 #define IA32_DO_EMIT ia32_fprintf_format(F, cmd_buf, cmnt_buf)
476 * coding of conditions
478 struct cmp2conditon_t {
484 * positive conditions for signed compares
486 static const struct cmp2conditon_t cmp2condition_s[] = {
487 { NULL, pn_Cmp_False }, /* always false */
488 { "e", pn_Cmp_Eq }, /* == */
489 { "l", pn_Cmp_Lt }, /* < */
490 { "le", pn_Cmp_Le }, /* <= */
491 { "g", pn_Cmp_Gt }, /* > */
492 { "ge", pn_Cmp_Ge }, /* >= */
493 { "ne", pn_Cmp_Lg }, /* != */
494 { "ordered", pn_Cmp_Leg }, /* Floating point: ordered */
495 { "unordered", pn_Cmp_Uo }, /* FLoting point: unordered */
496 { "unordered or ==", pn_Cmp_Ue }, /* Floating point: unordered or == */
497 { "unordered or <", pn_Cmp_Ul }, /* Floating point: unordered or < */
498 { "unordered or <=", pn_Cmp_Ule }, /* Floating point: unordered or <= */
499 { "unordered or >", pn_Cmp_Ug }, /* Floating point: unordered or > */
500 { "unordered or >=", pn_Cmp_Uge }, /* Floating point: unordered or >= */
501 { "unordered or !=", pn_Cmp_Ne }, /* Floating point: unordered or != */
502 { NULL, pn_Cmp_True }, /* always true */
506 * positive conditions for unsigned compares
508 static const struct cmp2conditon_t cmp2condition_u[] = {
509 { NULL, pn_Cmp_False }, /* always false */
510 { "e", pn_Cmp_Eq }, /* == */
511 { "b", pn_Cmp_Lt }, /* < */
512 { "be", pn_Cmp_Le }, /* <= */
513 { "a", pn_Cmp_Gt }, /* > */
514 { "ae", pn_Cmp_Ge }, /* >= */
515 { "ne", pn_Cmp_Lg }, /* != */
516 { "ordered", pn_Cmp_Leg }, /* Floating point: ordered */
517 { "unordered", pn_Cmp_Uo }, /* FLoting point: unordered */
518 { "unordered or ==", pn_Cmp_Ue }, /* Floating point: unordered or == */
519 { "unordered or <", pn_Cmp_Ul }, /* Floating point: unordered or < */
520 { "unordered or <=", pn_Cmp_Ule }, /* Floating point: unordered or <= */
521 { "unordered or >", pn_Cmp_Ug }, /* Floating point: unordered or > */
522 { "unordered or >=", pn_Cmp_Uge }, /* Floating point: unordered or >= */
523 { "unordered or !=", pn_Cmp_Ne }, /* Floating point: unordered or != */
524 { NULL, pn_Cmp_True }, /* always true */
528 * returns the condition code
530 static const char *get_cmp_suffix(int cmp_code, int unsigned_cmp)
532 assert(cmp2condition_s[cmp_code].num == cmp_code);
533 assert(cmp2condition_u[cmp_code].num == cmp_code);
535 return unsigned_cmp ? cmp2condition_u[cmp_code & 7].name : cmp2condition_s[cmp_code & 7].name;
539 * Returns the target block for a control flow node.
541 static ir_node *get_cfop_target_block(const ir_node *irn) {
542 return get_irn_link(irn);
546 * Returns the target label for a control flow node.
548 static char *get_cfop_target(const ir_node *irn, char *buf) {
549 ir_node *bl = get_cfop_target_block(irn);
551 snprintf(buf, SNPRINTF_BUF_LEN, BLOCK_PREFIX("%ld"), get_irn_node_nr(bl));
555 /** Return the next block in Block schedule */
556 static ir_node *next_blk_sched(const ir_node *block) {
557 return have_block_sched ? get_irn_link(block) : NULL;
561 * Emits the jump sequence for a conditional jump (cmp + jmp_true + jmp_false)
563 static void finish_CondJmp(FILE *F, const ir_node *irn, ir_mode *mode) {
564 const ir_node *proj1, *proj2 = NULL;
565 const ir_node *block, *next_bl = NULL;
566 const ir_edge_t *edge;
567 char buf[SNPRINTF_BUF_LEN];
568 char cmd_buf[SNPRINTF_BUF_LEN];
569 char cmnt_buf[SNPRINTF_BUF_LEN];
571 /* get both Proj's */
572 edge = get_irn_out_edge_first(irn);
573 proj1 = get_edge_src_irn(edge);
574 assert(is_Proj(proj1) && "CondJmp with a non-Proj");
576 edge = get_irn_out_edge_next(irn, edge);
578 proj2 = get_edge_src_irn(edge);
579 assert(is_Proj(proj2) && "CondJmp with a non-Proj");
582 /* for now, the code works for scheduled and non-schedules blocks */
583 block = get_nodes_block(irn);
585 /* we have a block schedule */
586 next_bl = next_blk_sched(block);
588 if (get_cfop_target_block(proj1) == next_bl) {
589 /* exchange both proj's so the second one can be omitted */
590 const ir_node *t = proj1;
596 /* the first Proj must always be created */
597 if (get_Proj_proj(proj1) == pn_Cond_true) {
598 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "j%s %s",
599 get_cmp_suffix(get_ia32_pncode(irn), !mode_is_signed(get_irn_mode(get_irn_n(irn, 0)))),
600 get_cfop_target(proj1, buf));
601 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* cmp(a, b) == TRUE */");
604 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "j%s %s",
605 get_cmp_suffix(get_negated_pnc(get_ia32_pncode(irn), mode),
606 !mode_is_signed(get_irn_mode(get_irn_n(irn, 0)))),
607 get_cfop_target(proj1, buf));
608 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* cmp(a, b) == FALSE */");
612 /* the second Proj might be a fallthrough */
614 if (get_cfop_target_block(proj2) != next_bl) {
615 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "jmp %s", get_cfop_target(proj2, buf));
616 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* otherwise */");
620 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* fallthrogh %s */", get_cfop_target(proj2, buf));
627 * Emits code for conditional jump.
629 static void CondJmp_emitter(const ir_node *irn, ia32_emit_env_t *env) {
631 char cmd_buf[SNPRINTF_BUF_LEN];
632 char cmnt_buf[SNPRINTF_BUF_LEN];
634 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "cmp %s", ia32_emit_binop(irn, env));
635 lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* %+F */", irn);
637 finish_CondJmp(F, irn, get_irn_mode(get_irn_n(irn, 2)));
641 * Emits code for conditional jump with two variables.
643 static void emit_ia32_CondJmp(const ir_node *irn, ia32_emit_env_t *env) {
644 CondJmp_emitter(irn, env);
648 * Emits code for conditional jump with immediate.
650 void emit_ia32_CondJmp_i(const ir_node *irn, ia32_emit_env_t *env) {
651 CondJmp_emitter(irn, env);
655 * Emits code for conditional test and jump.
657 static void TestJmp_emitter(const ir_node *irn, ia32_emit_env_t *env) {
659 const char *op1 = arch_register_get_name(get_in_reg(irn, 0));
660 const char *op2 = get_ia32_cnst(irn);
661 char cmd_buf[SNPRINTF_BUF_LEN];
662 char cmnt_buf[SNPRINTF_BUF_LEN];
665 op2 = arch_register_get_name(get_in_reg(irn, 1));
667 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "test %%%s,%s%s ", op1, get_ia32_cnst(irn) ? " " : " %", op2);
668 lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* %+F */", irn);
670 finish_CondJmp(F, irn, get_irn_mode(get_irn_n(irn, 0)));
674 * Emits code for conditional test and jump with two variables.
676 static void emit_ia32_TestJmp(const ir_node *irn, ia32_emit_env_t *env) {
677 TestJmp_emitter(irn, env);
682 /*********************************************************
685 * ___ _ __ ___ _| |_ _ _ _ _ __ ___ _ __ ___
686 * / _ \ '_ ` _ \| | __| | | | | | '_ ` _ \| '_ \/ __|
687 * | __/ | | | | | | |_ | | |_| | | | | | | |_) \__ \
688 * \___|_| |_| |_|_|\__| | |\__,_|_| |_| |_| .__/|___/
691 *********************************************************/
693 /* jump table entry (target and corresponding number) */
694 typedef struct _branch_t {
699 /* jump table for switch generation */
700 typedef struct _jmp_tbl_t {
701 ir_node *defProj; /**< default target */
702 int min_value; /**< smallest switch case */
703 int max_value; /**< largest switch case */
704 int num_branches; /**< number of jumps */
705 char *label; /**< label of the jump table */
706 branch_t *branches; /**< jump array */
710 * Compare two variables of type branch_t. Used to sort all switch cases
712 static int ia32_cmp_branch_t(const void *a, const void *b) {
713 branch_t *b1 = (branch_t *)a;
714 branch_t *b2 = (branch_t *)b;
716 if (b1->value <= b2->value)
723 * Emits code for a SwitchJmp (creates a jump table if
724 * possible otherwise a cmp-jmp cascade). Port from
727 void emit_ia32_SwitchJmp(const ir_node *irn, ia32_emit_env_t *emit_env) {
728 unsigned long interval;
729 char buf[SNPRINTF_BUF_LEN];
730 int last_value, i, pn, do_jmp_tbl = 1;
733 const ir_edge_t *edge;
734 const lc_arg_env_t *env = ia32_get_arg_env();
735 FILE *F = emit_env->out;
736 char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
738 /* fill the table structure */
739 tbl.label = xmalloc(SNPRINTF_BUF_LEN);
740 tbl.label = get_unique_label(tbl.label, SNPRINTF_BUF_LEN, "JMPTBL_");
742 tbl.num_branches = get_irn_n_edges(irn);
743 tbl.branches = xcalloc(tbl.num_branches, sizeof(tbl.branches[0]));
744 tbl.min_value = INT_MAX;
745 tbl.max_value = INT_MIN;
748 /* go over all proj's and collect them */
749 foreach_out_edge(irn, edge) {
750 proj = get_edge_src_irn(edge);
751 assert(is_Proj(proj) && "Only proj allowed at SwitchJmp");
753 pn = get_Proj_proj(proj);
755 /* create branch entry */
756 tbl.branches[i].target = proj;
757 tbl.branches[i].value = pn;
759 tbl.min_value = pn < tbl.min_value ? pn : tbl.min_value;
760 tbl.max_value = pn > tbl.max_value ? pn : tbl.max_value;
762 /* check for default proj */
763 if (pn == get_ia32_pncode(irn)) {
764 assert(tbl.defProj == NULL && "found two defProjs at SwitchJmp");
771 /* sort the branches by their number */
772 qsort(tbl.branches, tbl.num_branches, sizeof(tbl.branches[0]), ia32_cmp_branch_t);
774 /* two-complement's magic make this work without overflow */
775 interval = tbl.max_value - tbl.min_value;
777 /* check value interval */
778 if (interval > 16 * 1024) {
782 /* check ratio of value interval to number of branches */
783 if ((float)(interval + 1) / (float)tbl.num_branches > 8.0) {
789 if (tbl.min_value != 0) {
790 lc_esnprintf(env, cmd_buf, SNPRINTF_BUF_LEN, "cmpl %lu, -%d(%1S)",
791 interval, tbl.min_value, irn);
792 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* first switch value is not 0 */");
797 lc_esnprintf(env, cmd_buf, SNPRINTF_BUF_LEN, "cmpl %lu, %1S", interval, irn);
798 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* compare for switch */");
803 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "ja %s", get_cfop_target(tbl.defProj, buf));
804 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* default jump if out of range */");
807 if (tbl.num_branches > 1) {
810 lc_esnprintf(env, cmd_buf, SNPRINTF_BUF_LEN, "jmp [%1S*4+%s]", irn, tbl.label);
811 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* get jump table entry as target */");
814 fprintf(F, "\t.section\t.rodata\n");
815 fprintf(F, "\t.align 4\n");
817 fprintf(F, "%s:\n", tbl.label);
819 snprintf(cmd_buf, SNPRINTF_BUF_LEN, ".long %s", get_cfop_target(tbl.branches[0].target, buf));
820 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* case %d */\n", tbl.branches[0].value);
823 last_value = tbl.branches[0].value;
824 for (i = 1; i < tbl.num_branches; ++i) {
825 while (++last_value < tbl.branches[i].value) {
826 snprintf(cmd_buf, SNPRINTF_BUF_LEN, ".long %s", get_cfop_target(tbl.defProj, buf));
827 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* default case */");
830 snprintf(cmd_buf, SNPRINTF_BUF_LEN, ".long %s", get_cfop_target(tbl.branches[i].target, buf));
831 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* case %d */", last_value);
835 fprintf(F, "\t.text");
838 /* one jump is enough */
839 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "jmp %s", get_cfop_target(tbl.branches[0].target, buf));
840 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* only one case given */");
844 else { // no jump table
845 for (i = 0; i < tbl.num_branches; ++i) {
846 lc_esnprintf(env, cmd_buf, SNPRINTF_BUF_LEN, "cmpl %d, %1S", tbl.branches[i].value, irn);
847 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* case %d */", i);
849 fprintf(F, "\tje %s\n", get_cfop_target(tbl.branches[i].target, buf));
852 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "jmp %s", get_cfop_target(tbl.defProj, buf));
853 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* default case */");
864 * Emits code for a unconditional jump.
866 void emit_Jmp(const ir_node *irn, ia32_emit_env_t *env) {
867 ir_node *block, *next_bl;
869 char buf[SNPRINTF_BUF_LEN], cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
871 /* for now, the code works for scheduled and non-schedules blocks */
872 block = get_nodes_block(irn);
874 /* we have a block schedule */
875 next_bl = next_blk_sched(block);
876 if (get_cfop_target_block(irn) != next_bl) {
877 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "jmp %s", get_cfop_target(irn, buf));
878 lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* %+F(%+F) */", irn, get_cfop_target_block(irn));
882 lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* fallthrough %s */", get_cfop_target(irn, buf));
887 /****************************
890 * _ __ _ __ ___ _ ___
891 * | '_ \| '__/ _ \| |/ __|
892 * | |_) | | | (_) | |\__ \
893 * | .__/|_| \___/| ||___/
896 ****************************/
899 * Emits code for a proj -> node
901 void emit_Proj(const ir_node *irn, ia32_emit_env_t *env) {
902 ir_node *pred = get_Proj_pred(irn);
904 if (get_irn_op(pred) == op_Start) {
905 switch(get_Proj_proj(irn)) {
906 case pn_Start_X_initial_exec:
915 /**********************************
918 * | | ___ _ __ _ _| |_) |
919 * | | / _ \| '_ \| | | | _ <
920 * | |___| (_) | |_) | |_| | |_) |
921 * \_____\___/| .__/ \__, |____/
924 **********************************/
927 * Emit movsb/w instructions to make mov count divideable by 4
929 static void emit_CopyB_prolog(FILE *F, int rem, int size) {
930 char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
932 fprintf(F, "\t/* memcopy %d bytes*/\n", size);
934 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "cld");
935 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* copy direction forward*/");
940 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "movsb");
941 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* memcopy remainder 1 */");
944 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "movsw");
945 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* memcopy remainder 2 */");
948 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "movsb");
949 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* memcopy remainder 3 */");
951 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "movsw");
952 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* memcopy remainder 3 */");
960 * Emit rep movsd instruction for memcopy.
962 void emit_ia32_CopyB(const ir_node *irn, ia32_emit_env_t *emit_env) {
963 FILE *F = emit_env->out;
964 tarval *tv = get_ia32_Immop_tarval(irn);
965 int rem = get_tarval_long(tv);
966 int size = get_tarval_long(get_ia32_Immop_tarval(get_irn_n(irn, 2)));
967 char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
969 emit_CopyB_prolog(F, rem, size);
971 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "rep movsd");
972 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* memcopy */");
977 * Emits unrolled memcopy.
979 void emit_ia32_CopyB_i(const ir_node *irn, ia32_emit_env_t *emit_env) {
980 tarval *tv = get_ia32_Immop_tarval(irn);
981 int size = get_tarval_long(tv);
982 FILE *F = emit_env->out;
983 char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
985 emit_CopyB_prolog(F, size & 0x3, size);
989 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "movsd");
990 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* memcopy unrolled */");
997 /***************************
1001 * | | / _ \| '_ \ \ / /
1002 * | |___| (_) | | | \ V /
1003 * \_____\___/|_| |_|\_/
1005 ***************************/
1008 * Emit code for conversions (I, FP), (FP, I) and (FP, FP).
1010 static void emit_ia32_Conv(const ir_node *irn, ia32_emit_env_t *emit_env) {
1011 FILE *F = emit_env->out;
1012 const lc_arg_env_t *env = ia32_get_arg_env();
1013 char *from, *to, buf[64];
1014 ir_mode *src_mode, *tgt_mode;
1015 char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
1017 src_mode = is_ia32_AddrModeS(irn) ? get_ia32_ls_mode(irn) : get_irn_mode(get_irn_n(irn, 2));
1018 tgt_mode = get_ia32_res_mode(irn);
1020 from = mode_is_float(src_mode) ? (get_mode_size_bits(src_mode) == 32 ? "ss" : "sd") : "si";
1021 to = mode_is_float(tgt_mode) ? (get_mode_size_bits(tgt_mode) == 32 ? "ss" : "sd") : "si";
1023 switch(get_ia32_op_type(irn)) {
1025 lc_esnprintf(env, buf, sizeof(buf), "%1D, %3S", irn, irn);
1027 case ia32_AddrModeS:
1028 lc_esnprintf(env, buf, sizeof(buf), "%1D, %s", irn, ia32_emit_am(irn, emit_env));
1031 assert(0 && "unsupported op type for Conv");
1034 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "cvt%s2%s %s", from, to, buf);
1035 lc_esnprintf(env, cmnt_buf, SNPRINTF_BUF_LEN, "/* %+F(%+F, %+F) */", irn, src_mode, tgt_mode);
1039 void emit_ia32_Conv_I2FP(const ir_node *irn, ia32_emit_env_t *emit_env) {
1040 emit_ia32_Conv(irn, emit_env);
1043 void emit_ia32_Conv_FP2I(const ir_node *irn, ia32_emit_env_t *emit_env) {
1044 emit_ia32_Conv(irn, emit_env);
1047 void emit_ia32_Conv_FP2FP(const ir_node *irn, ia32_emit_env_t *emit_env) {
1048 emit_ia32_Conv(irn, emit_env);
1053 /*******************************************
1056 * | |__ ___ _ __ ___ __| | ___ ___
1057 * | '_ \ / _ \ '_ \ / _ \ / _` |/ _ \/ __|
1058 * | |_) | __/ | | | (_) | (_| | __/\__ \
1059 * |_.__/ \___|_| |_|\___/ \__,_|\___||___/
1061 *******************************************/
1064 * Emits a backend call
1066 void emit_be_Call(const ir_node *irn, ia32_emit_env_t *emit_env) {
1067 FILE *F = emit_env->out;
1068 entity *ent = be_Call_get_entity(irn);
1069 char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
1072 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "call %s", get_entity_name(ent));
1075 lc_esnprintf(ia32_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "%1D", get_irn_n(irn, be_pos_Call_ptr));
1078 lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* %+F (be_Call) */", irn);
1084 * Emits code to increase stack pointer.
1086 void emit_be_IncSP(const ir_node *irn, ia32_emit_env_t *emit_env) {
1087 FILE *F = emit_env->out;
1088 unsigned offs = be_get_IncSP_offset(irn);
1089 be_stack_dir_t dir = be_get_IncSP_direction(irn);
1090 char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
1093 lc_esnprintf(ia32_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "add %1S,%s%u", irn,
1094 (dir == be_stack_dir_along) ? " -" : " ", offs);
1095 lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* %+F (IncSP) */", irn);
1098 snprintf(cmd_buf, SNPRINTF_BUF_LEN, " ");
1099 lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* omitted %+F (IncSP) with 0 */", irn);
1106 * Emits code to set stack pointer.
1108 void emit_be_SetSP(const ir_node *irn, ia32_emit_env_t *emit_env) {
1109 FILE *F = emit_env->out;
1110 char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
1112 lc_esnprintf(ia32_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "mov %1D, %3S", irn, irn);
1113 lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* %+F (restore SP) */", irn);
1118 * Emits code for Copy.
1120 void emit_be_Copy(const ir_node *irn, ia32_emit_env_t *emit_env) {
1121 FILE *F = emit_env->out;
1122 char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
1124 lc_esnprintf(ia32_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "mov %1D, %1S", irn, irn);
1125 lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* %+F */", irn);
1130 * Emits code for exchange.
1132 void emit_be_Perm(const ir_node *irn, ia32_emit_env_t *emit_env) {
1133 FILE *F = emit_env->out;
1134 char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
1136 lc_esnprintf(ia32_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "xchg %1S, %2S", irn, irn);
1137 lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* %+F(%1A, %2A) */", irn, irn, irn);
1141 /***********************************************************************************
1144 * _ __ ___ __ _ _ _ __ | |_ _ __ __ _ _ __ ___ _____ _____ _ __| | __
1145 * | '_ ` _ \ / _` | | '_ \ | _| '__/ _` | '_ ` _ \ / _ \ \ /\ / / _ \| '__| |/ /
1146 * | | | | | | (_| | | | | | | | | | | (_| | | | | | | __/\ V V / (_) | | | <
1147 * |_| |_| |_|\__,_|_|_| |_| |_| |_| \__,_|_| |_| |_|\___| \_/\_/ \___/|_| |_|\_\
1149 ***********************************************************************************/
1152 * Enters the emitter functions for handled nodes into the generic
1153 * pointer of an opcode.
1155 static void ia32_register_emitters(void) {
1157 #define IA32_EMIT(a) op_ia32_##a->ops.generic = (op_func)emit_ia32_##a
1158 #define EMIT(a) op_##a->ops.generic = (op_func)emit_##a
1159 #define BE_EMIT(a) op_be_##a->ops.generic = (op_func)emit_be_##a
1161 /* first clear the generic function pointer for all ops */
1162 clear_irp_opcodes_generic_func();
1164 /* register all emitter functions defined in spec */
1165 ia32_register_spec_emitters();
1167 /* other ia32 emitter functions */
1170 IA32_EMIT(SwitchJmp);
1173 IA32_EMIT(Conv_I2FP);
1174 IA32_EMIT(Conv_FP2I);
1175 IA32_EMIT(Conv_FP2FP);
1177 /* benode emitter */
1194 * Emits code for a node.
1196 static void ia32_emit_node(const ir_node *irn, void *env) {
1197 ia32_emit_env_t *emit_env = env;
1198 firm_dbg_module_t *mod = emit_env->mod;
1199 FILE *F = emit_env->out;
1200 ir_op *op = get_irn_op(irn);
1202 DBG((mod, LEVEL_1, "emitting code for %+F\n", irn));
1204 if (op->ops.generic) {
1205 void (*emit)(const ir_node *, void *) = (void (*)(const ir_node *, void *))op->ops.generic;
1209 ir_fprintf(F, "\t%35s /* %+F */\n", " ", irn);
1214 * Walks over the nodes in a block connected by scheduling edges
1215 * and emits code for each node.
1217 static void ia32_gen_block(ir_node *block, void *env) {
1220 if (! is_Block(block))
1223 fprintf(((ia32_emit_env_t *)env)->out, BLOCK_PREFIX("%ld:\n"), get_irn_node_nr(block));
1224 sched_foreach(block, irn) {
1225 ia32_emit_node(irn, env);
1230 * Emits code for function start.
1232 static void ia32_emit_func_prolog(FILE *F, ir_graph *irg) {
1233 entity *irg_ent = get_irg_entity(irg);
1234 const char *irg_name = get_entity_name(irg_ent);
1236 fprintf(F, "\t.text\n");
1237 if (get_entity_visibility(irg_ent) == visibility_external_visible) {
1238 fprintf(F, ".globl %s\n", irg_name);
1240 fprintf(F, "\t.type\t%s, @function\n", irg_name);
1241 fprintf(F, "%s:\n", irg_name);
1245 * Emits code for function end
1247 static void ia32_emit_func_epilog(FILE *F, ir_graph *irg) {
1248 const char *irg_name = get_entity_name(get_irg_entity(irg));
1250 fprintf(F, "\tret\n");
1251 fprintf(F, "\t.size\t%s, .-%s\n\n", irg_name, irg_name);
1256 * Sets labels for control flow nodes (jump target)
1257 * TODO: Jump optimization
1259 static void ia32_gen_labels(ir_node *block, void *env) {
1261 int n = get_Block_n_cfgpreds(block);
1263 for (n--; n >= 0; n--) {
1264 pred = get_Block_cfgpred(block, n);
1265 set_irn_link(pred, block);
1275 * Ext-Block walker: create a block schedule
1277 static void create_block_list(ir_extblk *blk, void *env) {
1281 for (i = 0, n = get_extbb_n_blocks(blk); i < n; ++i) {
1282 ir_node *block = get_extbb_block(blk, i);
1284 set_irn_link(block, NULL);
1286 set_irn_link(list->end, block);
1288 list->start = block;
1295 * Main driver. Emits the code for one routine.
1297 void ia32_gen_routine(FILE *F, ir_graph *irg, const ia32_code_gen_t *cg) {
1298 ia32_emit_env_t emit_env;
1302 emit_env.mod = firm_dbg_register("firm.be.ia32.emitter");
1304 emit_env.arch_env = cg->arch_env;
1306 emit_env.isa = (ia32_isa_t *)cg->arch_env->isa;
1308 /* set the global arch_env (needed by print hooks) */
1309 arch_env = cg->arch_env;
1311 ia32_register_emitters();
1313 ia32_emit_func_prolog(F, irg);
1314 irg_block_walk_graph(irg, ia32_gen_labels, NULL, &emit_env);
1316 if (cg->opt.extbb) {
1317 /* schedule extended basic blocks */
1323 irg_extblock_walk_graph(irg, NULL, create_block_list, &list);
1325 have_block_sched = 1;
1326 for (block = list.start; block; block = get_irn_link(block))
1327 ia32_gen_block(block, &emit_env);
1330 /* "normal" block schedule */
1332 have_block_sched = 0;
1333 irg_walk_blkwise_graph(irg, NULL, ia32_gen_block, &emit_env);
1336 ia32_emit_func_epilog(F, irg);