2 * This file implements the node emitter.
22 #include "iredges_t.h"
24 #include "../besched.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 extern int obstack_printf(struct obstack *obst, char *fmt, ...);
43 #define SNPRINTF_BUF_LEN 128
45 static const arch_env_t *arch_env = NULL;
47 /*************************************************************
49 * (_) | | / _| | | | |
50 * _ __ _ __ _ _ __ | |_| |_ | |__ ___| |_ __ ___ _ __
51 * | '_ \| '__| | '_ \| __| _| | '_ \ / _ \ | '_ \ / _ \ '__|
52 * | |_) | | | | | | | |_| | | | | | __/ | |_) | __/ |
53 * | .__/|_| |_|_| |_|\__|_| |_| |_|\___|_| .__/ \___|_|
56 *************************************************************/
58 /* We always pass the ir_node which is a pointer. */
59 static int ia32_get_arg_type(const lc_arg_occ_t *occ) {
60 return lc_arg_type_ptr;
65 * Returns the register at in position pos.
67 static const arch_register_t *get_in_reg(const ir_node *irn, int pos) {
69 const arch_register_t *reg = NULL;
71 assert(get_irn_arity(irn) > pos && "Invalid IN position");
73 /* The out register of the operator at position pos is the
74 in register we need. */
75 op = get_irn_n(irn, pos);
77 reg = arch_get_irn_register(arch_env, op);
79 assert(reg && "no in register found");
84 * Returns the register at out position pos.
86 static const arch_register_t *get_out_reg(const ir_node *irn, int pos) {
88 const arch_register_t *reg = NULL;
90 /* 1st case: irn is not of mode_T, so it has only */
91 /* one OUT register -> good */
92 /* 2nd case: irn is of mode_T -> collect all Projs and ask the */
93 /* Proj with the corresponding projnum for the register */
95 if (get_irn_mode(irn) != mode_T) {
96 reg = arch_get_irn_register(arch_env, irn);
98 else if (is_ia32_irn(irn)) {
99 reg = get_ia32_out_reg(irn, pos);
102 const ir_edge_t *edge;
104 foreach_out_edge(irn, edge) {
105 proj = get_edge_src_irn(edge);
106 assert(is_Proj(proj) && "non-Proj from mode_T node");
107 if (get_Proj_proj(proj) == pos) {
108 reg = arch_get_irn_register(arch_env, proj);
114 assert(reg && "no out register found");
124 * Returns the name of the in register at position pos.
126 static const char *get_ia32_reg_name(ir_node *irn, int pos, enum io_direction in_out) {
127 const arch_register_t *reg;
129 if (in_out == IN_REG) {
130 reg = get_in_reg(irn, pos);
133 /* destination address mode nodes don't have outputs */
134 if (is_ia32_irn(irn) && get_ia32_op_type(irn) == ia32_AddrModeD) {
138 reg = get_out_reg(irn, pos);
141 return arch_register_get_name(reg);
145 * Get the register name for a node.
147 static int ia32_get_reg_name(lc_appendable_t *app,
148 const lc_arg_occ_t *occ, const lc_arg_value_t *arg)
151 ir_node *X = arg->v_ptr;
152 int nr = occ->width - 1;
155 return lc_arg_append(app, occ, "(null)", 6);
157 buf = get_ia32_reg_name(X, nr, occ->conversion == 'S' ? IN_REG : OUT_REG);
159 return lc_arg_append(app, occ, buf, strlen(buf));
163 * Returns the tarval, offset or scale of an ia32 as a string.
165 static int ia32_const_to_str(lc_appendable_t *app,
166 const lc_arg_occ_t *occ, const lc_arg_value_t *arg)
169 ir_node *X = arg->v_ptr;
172 return lc_arg_append(app, occ, "(null)", 6);
174 if (occ->conversion == 'C') {
175 buf = get_ia32_cnst(X);
178 buf = get_ia32_am_offs(X);
181 return buf ? lc_arg_append(app, occ, buf, strlen(buf)) : 0;
185 * Determines the SSE suffix depending on the mode.
187 static int ia32_get_mode_suffix(lc_appendable_t *app,
188 const lc_arg_occ_t *occ, const lc_arg_value_t *arg)
190 ir_node *X = arg->v_ptr;
191 ir_mode *mode = get_irn_mode(X);
193 if (mode == mode_T) {
194 mode = is_ia32_AddrModeS(X) || is_ia32_AddrModeD(X) ? get_ia32_ls_mode(X) : get_ia32_res_mode(X);
198 return lc_arg_append(app, occ, "(null)", 6);
200 if (mode_is_float(mode)) {
201 return lc_appendable_chadd(app, get_mode_size_bits(mode) == 32 ? 's' : 'd');
205 return lc_appendable_chadd(app, mode_is_signed(mode) ? 's' : 'z');
210 * Return the ia32 printf arg environment.
211 * We use the firm environment with some additional handlers.
213 const lc_arg_env_t *ia32_get_arg_env(void) {
214 static lc_arg_env_t *env = NULL;
216 static const lc_arg_handler_t ia32_reg_handler = { ia32_get_arg_type, ia32_get_reg_name };
217 static const lc_arg_handler_t ia32_const_handler = { ia32_get_arg_type, ia32_const_to_str };
218 static const lc_arg_handler_t ia32_mode_handler = { ia32_get_arg_type, ia32_get_mode_suffix };
221 /* extend the firm printer */
222 env = firm_get_arg_env();
224 lc_arg_register(env, "ia32:sreg", 'S', &ia32_reg_handler);
225 lc_arg_register(env, "ia32:dreg", 'D', &ia32_reg_handler);
226 lc_arg_register(env, "ia32:cnst", 'C', &ia32_const_handler);
227 lc_arg_register(env, "ia32:offs", 'O', &ia32_const_handler);
228 lc_arg_register(env, "ia32:mode", 'M', &ia32_mode_handler);
235 * Emits registers and/or address mode of a binary operation.
237 char *ia32_emit_binop(const ir_node *n, ia32_emit_env_t *env) {
238 static char *buf = NULL;
240 /* verify that this function is never called on non-AM supporting operations */
241 //assert(get_ia32_am_support(n) != ia32_am_None && "emit binop expects addressmode support");
243 #define PRODUCES_RESULT(n) \
244 (!(is_ia32_St(n) || \
245 is_ia32_Store8Bit(n) || \
246 is_ia32_CondJmp(n) || \
247 is_ia32_fCondJmp(n) || \
248 is_ia32_SwitchJmp(n)))
251 buf = xcalloc(1, SNPRINTF_BUF_LEN);
254 memset(buf, 0, SNPRINTF_BUF_LEN);
257 switch(get_ia32_op_type(n)) {
259 if (get_ia32_cnst(n)) {
260 lc_esnprintf(ia32_get_arg_env(), buf, SNPRINTF_BUF_LEN, "%3S, %s", n, get_ia32_cnst(n));
263 const arch_register_t *in1 = get_in_reg(n, 2);
264 const arch_register_t *in2 = get_in_reg(n, 3);
265 const arch_register_t *out = PRODUCES_RESULT(n) ? get_out_reg(n, 0) : NULL;
266 const arch_register_t *in;
268 in = out ? (REGS_ARE_EQUAL(out, in2) ? in1 : in2) : in2;
269 out = out ? out : in1;
271 snprintf(buf, SNPRINTF_BUF_LEN, "%s, %s", \
272 arch_register_get_name(out), arch_register_get_name(in));
276 lc_esnprintf(ia32_get_arg_env(), buf, SNPRINTF_BUF_LEN, "%4S, %s", n, ia32_emit_am(n, env));
279 if (get_ia32_cnst(n)) {
280 lc_esnprintf(ia32_get_arg_env(), buf, SNPRINTF_BUF_LEN, "%s, %s", ia32_emit_am(n, env), get_ia32_cnst(n));
283 const arch_register_t *in1 = get_in_reg(n, 2);
284 const char *reg_name;
285 ir_mode *mode = get_ia32_res_mode(n);
287 mode = mode ? mode : get_ia32_ls_mode(n);
289 switch(get_mode_size_bits(mode)) {
291 reg_name = ia32_get_mapped_reg_name(env->isa->regs_8bit, in1);
294 reg_name = ia32_get_mapped_reg_name(env->isa->regs_16bit, in1);
297 reg_name = arch_register_get_name(in1);
300 assert(0 && "unsupported mode size");
304 lc_esnprintf(ia32_get_arg_env(), buf, SNPRINTF_BUF_LEN, "%s, %s", ia32_emit_am(n, env), reg_name);
308 assert(0 && "unsupported op type");
311 #undef PRODUCES_RESULT
317 * Emits registers and/or address mode of a unary operation.
319 char *ia32_emit_unop(const ir_node *n, ia32_emit_env_t *env) {
320 static char *buf = NULL;
323 buf = xcalloc(1, SNPRINTF_BUF_LEN);
326 memset(buf, 0, SNPRINTF_BUF_LEN);
329 switch(get_ia32_op_type(n)) {
331 lc_esnprintf(ia32_get_arg_env(), buf, SNPRINTF_BUF_LEN, "%1D", n);
334 snprintf(buf, SNPRINTF_BUF_LEN, ia32_emit_am(n, env));
337 assert(0 && "unsupported op type");
344 * Emits address mode.
346 char *ia32_emit_am(const ir_node *n, ia32_emit_env_t *env) {
347 ia32_am_flavour_t am_flav = get_ia32_am_flavour(n);
351 static struct obstack *obst = NULL;
352 ir_mode *mode = get_ia32_ls_mode(n);
354 if (! is_ia32_Lea(n))
355 assert(mode && "AM node must have ls_mode attribute set.");
358 obst = xcalloc(1, sizeof(*obst));
361 obstack_free(obst, NULL);
364 /* obstack_free with NULL results in an uninitialized obstack */
368 switch (get_mode_size_bits(mode)) {
370 obstack_printf(obst, "BYTE ");
373 obstack_printf(obst, "WORD ");
380 obstack_printf(obst, "[");
382 if (am_flav & ia32_B) {
383 lc_eoprintf(ia32_get_arg_env(), obst, "%1S", n);
387 if (am_flav & ia32_I) {
389 obstack_printf(obst, "+");
392 lc_eoprintf(ia32_get_arg_env(), obst, "%2S", n);
394 if (am_flav & ia32_S) {
395 obstack_printf(obst, "*%d", 1 << get_ia32_am_scale(n));
401 if (am_flav & ia32_O) {
402 obstack_printf(obst, get_ia32_am_offs(n));
405 obstack_printf(obst, "] ");
407 size = obstack_object_size(obst);
408 s = obstack_finish(obst);
417 * Formated print of commands and comments.
419 static void ia32_fprintf_format(FILE *F, char *cmd_buf, char *cmnt_buf) {
420 fprintf(F, "\t%-35s %-60s\n", cmd_buf, cmnt_buf);
426 * Add a number to a prefix. This number will not be used a second time.
428 static char *get_unique_label(char *buf, size_t buflen, const char *prefix) {
429 static unsigned long id = 0;
430 snprintf(buf, buflen, "%s%lu", prefix, ++id);
436 /*************************************************
439 * ___ _ __ ___ _| |_ ___ ___ _ __ __| |
440 * / _ \ '_ ` _ \| | __| / __/ _ \| '_ \ / _` |
441 * | __/ | | | | | | |_ | (_| (_) | | | | (_| |
442 * \___|_| |_| |_|_|\__| \___\___/|_| |_|\__,_|
444 *************************************************/
447 #define IA32_DO_EMIT ia32_fprintf_format(F, cmd_buf, cmnt_buf)
450 * coding of conditions
452 struct cmp2conditon_t {
458 * positive conditions for signed compares
460 static const struct cmp2conditon_t cmp2condition_s[] = {
461 { NULL, pn_Cmp_False }, /* always false */
462 { "e", pn_Cmp_Eq }, /* == */
463 { "l", pn_Cmp_Lt }, /* < */
464 { "le", pn_Cmp_Le }, /* <= */
465 { "g", pn_Cmp_Gt }, /* > */
466 { "ge", pn_Cmp_Ge }, /* >= */
467 { "ne", pn_Cmp_Lg }, /* != */
468 { "ordered", pn_Cmp_Leg }, /* Floating point: ordered */
469 { "unordered", pn_Cmp_Uo }, /* FLoting point: unordered */
470 { "unordered or ==", pn_Cmp_Ue }, /* Floating point: unordered or == */
471 { "unordered or <", pn_Cmp_Ul }, /* Floating point: unordered or < */
472 { "unordered or <=", pn_Cmp_Ule }, /* Floating point: unordered or <= */
473 { "unordered or >", pn_Cmp_Ug }, /* Floating point: unordered or > */
474 { "unordered or >=", pn_Cmp_Uge }, /* Floating point: unordered or >= */
475 { "unordered or !=", pn_Cmp_Ne }, /* Floating point: unordered or != */
476 { NULL, pn_Cmp_True }, /* always true */
480 * positive conditions for unsigned compares
482 static const struct cmp2conditon_t cmp2condition_u[] = {
483 { NULL, pn_Cmp_False }, /* always false */
484 { "e", pn_Cmp_Eq }, /* == */
485 { "b", pn_Cmp_Lt }, /* < */
486 { "be", pn_Cmp_Le }, /* <= */
487 { "a", pn_Cmp_Gt }, /* > */
488 { "ae", pn_Cmp_Ge }, /* >= */
489 { "ne", pn_Cmp_Lg }, /* != */
490 { "ordered", pn_Cmp_Leg }, /* Floating point: ordered */
491 { "unordered", pn_Cmp_Uo }, /* FLoting point: unordered */
492 { "unordered or ==", pn_Cmp_Ue }, /* Floating point: unordered or == */
493 { "unordered or <", pn_Cmp_Ul }, /* Floating point: unordered or < */
494 { "unordered or <=", pn_Cmp_Ule }, /* Floating point: unordered or <= */
495 { "unordered or >", pn_Cmp_Ug }, /* Floating point: unordered or > */
496 { "unordered or >=", pn_Cmp_Uge }, /* Floating point: unordered or >= */
497 { "unordered or !=", pn_Cmp_Ne }, /* Floating point: unordered or != */
498 { NULL, pn_Cmp_True }, /* always true */
502 * returns the condition code
504 static const char *get_cmp_suffix(int cmp_code, int unsigned_cmp)
506 assert(cmp2condition_s[cmp_code].num == cmp_code);
507 assert(cmp2condition_u[cmp_code].num == cmp_code);
509 return unsigned_cmp ? cmp2condition_u[cmp_code & 7].name : cmp2condition_s[cmp_code & 7].name;
513 * Returns the target label for a control flow node.
515 static char *get_cfop_target(const ir_node *irn, char *buf) {
516 ir_node *bl = get_irn_link(irn);
518 snprintf(buf, SNPRINTF_BUF_LEN, "BLOCK_%ld", get_irn_node_nr(bl));
523 * Emits the jump sequence for a conditional jump (cmp + jmp_true + jmp_false)
525 static void finish_CondJmp(FILE *F, const ir_node *irn) {
527 const ir_edge_t *edge;
528 char buf[SNPRINTF_BUF_LEN];
529 char cmd_buf[SNPRINTF_BUF_LEN];
530 char cmnt_buf[SNPRINTF_BUF_LEN];
532 edge = get_irn_out_edge_first(irn);
533 proj = get_edge_src_irn(edge);
534 assert(is_Proj(proj) && "CondJmp with a non-Proj");
536 if (get_Proj_proj(proj) == 1) {
537 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "j%s %s",
538 get_cmp_suffix(get_ia32_pncode(irn), !mode_is_signed(get_irn_mode(get_irn_n(irn, 0)))),
539 get_cfop_target(proj, buf));
540 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "; cmp(a, b) == TRUE");
543 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "jn%s %s",
544 get_cmp_suffix(get_ia32_pncode(irn), !mode_is_signed(get_irn_mode(get_irn_n(irn, 0)))),
545 get_cfop_target(proj, buf));
546 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "; cmp(a, b) == FALSE");
551 edge = get_irn_out_edge_next(irn, edge);
553 proj = get_edge_src_irn(edge);
554 assert(is_Proj(proj) && "CondJmp with a non-Proj");
555 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "jmp %s", get_cfop_target(proj, buf));
556 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "; otherwise");
563 * Emits code for conditional jump.
565 static void CondJmp_emitter(const ir_node *irn, ia32_emit_env_t *env) {
567 char cmd_buf[SNPRINTF_BUF_LEN];
568 char cmnt_buf[SNPRINTF_BUF_LEN];
570 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "cmp %s", ia32_emit_binop(irn, env));
571 lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "; %+F", irn);
573 finish_CondJmp(F, irn);
577 * Emits code for conditional jump with two variables.
579 static void emit_ia32_CondJmp(const ir_node *irn, ia32_emit_env_t *env) {
580 CondJmp_emitter(irn, env);
584 * Emits code for conditional jump with immediate.
586 void emit_ia32_CondJmp_i(const ir_node *irn, ia32_emit_env_t *env) {
587 CondJmp_emitter(irn, env);
591 * Emits code for conditional test and jump.
593 static void TestJmp_emitter(const ir_node *irn, ia32_emit_env_t *env) {
595 char cmd_buf[SNPRINTF_BUF_LEN];
596 char cmnt_buf[SNPRINTF_BUF_LEN];
597 const arch_register_t *in1 = get_in_reg(irn, 0);
598 const arch_register_t *in2 = get_in_reg(irn, 1);
600 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "test %s, %s ", arch_register_get_name(in1), arch_register_get_name(in2));
601 lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "; %+F", irn);
603 finish_CondJmp(F, irn);
607 * Emits code for conditional test and jump with two variables.
609 static void emit_ia32_TestJmp(const ir_node *irn, ia32_emit_env_t *env) {
610 TestJmp_emitter(irn, env);
614 * Emits code for conditional test and jump with immediate.
616 static void emit_ia32_TestJmp_i(const ir_node *irn, ia32_emit_env_t *env) {
617 TestJmp_emitter(irn, env);
620 /*********************************************************
623 * ___ _ __ ___ _| |_ _ _ _ _ __ ___ _ __ ___
624 * / _ \ '_ ` _ \| | __| | | | | | '_ ` _ \| '_ \/ __|
625 * | __/ | | | | | | |_ | | |_| | | | | | | |_) \__ \
626 * \___|_| |_| |_|_|\__| | |\__,_|_| |_| |_| .__/|___/
629 *********************************************************/
631 /* jump table entry (target and corresponding number) */
632 typedef struct _branch_t {
637 /* jump table for switch generation */
638 typedef struct _jmp_tbl_t {
639 ir_node *defProj; /**< default target */
640 int min_value; /**< smallest switch case */
641 int max_value; /**< largest switch case */
642 int num_branches; /**< number of jumps */
643 char *label; /**< label of the jump table */
644 branch_t *branches; /**< jump array */
648 * Compare two variables of type branch_t. Used to sort all switch cases
650 static int ia32_cmp_branch_t(const void *a, const void *b) {
651 branch_t *b1 = (branch_t *)a;
652 branch_t *b2 = (branch_t *)b;
654 if (b1->value <= b2->value)
661 * Emits code for a SwitchJmp (creates a jump table if
662 * possible otherwise a cmp-jmp cascade). Port from
665 void emit_ia32_SwitchJmp(const ir_node *irn, ia32_emit_env_t *emit_env) {
666 unsigned long interval;
667 char buf[SNPRINTF_BUF_LEN];
668 int last_value, i, pn, do_jmp_tbl = 1;
671 const ir_edge_t *edge;
672 const lc_arg_env_t *env = ia32_get_arg_env();
673 FILE *F = emit_env->out;
674 char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
676 /* fill the table structure */
677 tbl.label = xmalloc(SNPRINTF_BUF_LEN);
678 tbl.label = get_unique_label(tbl.label, SNPRINTF_BUF_LEN, "JMPTBL_");
680 tbl.num_branches = get_irn_n_edges(irn);
681 tbl.branches = xcalloc(tbl.num_branches, sizeof(tbl.branches[0]));
682 tbl.min_value = INT_MAX;
683 tbl.max_value = INT_MIN;
686 /* go over all proj's and collect them */
687 foreach_out_edge(irn, edge) {
688 proj = get_edge_src_irn(edge);
689 assert(is_Proj(proj) && "Only proj allowed at SwitchJmp");
691 pn = get_Proj_proj(proj);
693 /* create branch entry */
694 tbl.branches[i].target = proj;
695 tbl.branches[i].value = pn;
697 tbl.min_value = pn < tbl.min_value ? pn : tbl.min_value;
698 tbl.max_value = pn > tbl.max_value ? pn : tbl.max_value;
700 /* check for default proj */
701 if (pn == get_ia32_pncode(irn)) {
702 assert(tbl.defProj == NULL && "found two defProjs at SwitchJmp");
709 /* sort the branches by their number */
710 qsort(tbl.branches, tbl.num_branches, sizeof(tbl.branches[0]), ia32_cmp_branch_t);
712 /* two-complement's magic make this work without overflow */
713 interval = tbl.max_value - tbl.min_value;
715 /* check value interval */
716 if (interval > 16 * 1024) {
720 /* check ratio of value interval to number of branches */
721 if ((float)(interval + 1) / (float)tbl.num_branches > 8.0) {
727 if (tbl.min_value != 0) {
728 lc_esnprintf(env, cmd_buf, SNPRINTF_BUF_LEN, "cmpl %lu, -%d(%1S)",
729 interval, tbl.min_value, irn);
730 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "; first switch value is not 0");
735 lc_esnprintf(env, cmd_buf, SNPRINTF_BUF_LEN, "cmpl %lu, %1S", interval, irn);
736 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "; compare for switch");
741 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "ja %s", get_cfop_target(tbl.defProj, buf));
742 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "; default jump if out of range ");
745 if (tbl.num_branches > 1) {
748 lc_esnprintf(env, cmd_buf, SNPRINTF_BUF_LEN, "jmp [%1S*4+%s]", irn, tbl.label);
749 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "; get jump table entry as target");
752 fprintf(F, "\t.section\t.rodata\n");
753 fprintf(F, "\t.align 4\n");
755 fprintf(F, "%s:\n", tbl.label);
757 snprintf(cmd_buf, SNPRINTF_BUF_LEN, ".long %s", get_cfop_target(tbl.branches[0].target, buf));
758 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* case %d */\n", tbl.branches[0].value);
761 last_value = tbl.branches[0].value;
762 for (i = 1; i < tbl.num_branches; ++i) {
763 while (++last_value < tbl.branches[i].value) {
764 snprintf(cmd_buf, SNPRINTF_BUF_LEN, ".long %s", get_cfop_target(tbl.defProj, buf));
765 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "; default case");
768 snprintf(cmd_buf, SNPRINTF_BUF_LEN, ".long %s", get_cfop_target(tbl.branches[i].target, buf), last_value);
769 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "; case %d", last_value);
773 fprintf(F, "\t.text");
776 /* one jump is enough */
777 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "jmp %s", get_cfop_target(tbl.branches[0].target, buf));
778 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "; only one case given");
782 else { // no jump table
783 for (i = 0; i < tbl.num_branches; ++i) {
784 lc_esnprintf(env, cmd_buf, SNPRINTF_BUF_LEN, "cmpl %d, %1S", tbl.branches[i].value, irn);
785 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "; case %d", i);
787 fprintf(F, "\tje %s\n", get_cfop_target(tbl.branches[i].target, buf));
790 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "jmp %s", get_cfop_target(tbl.defProj, buf));
791 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "; default case");
802 * Emits code for a unconditional jump.
804 void emit_Jmp(const ir_node *irn, ia32_emit_env_t *env) {
806 char buf[SNPRINTF_BUF_LEN], cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
808 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "jmp %s", get_cfop_target(irn, buf), get_irn_link(irn));
809 lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "; %+F(%+F)", irn, get_irn_link(irn));
815 /****************************
818 * _ __ _ __ ___ _ ___
819 * | '_ \| '__/ _ \| |/ __|
820 * | |_) | | | (_) | |\__ \
821 * | .__/|_| \___/| ||___/
824 ****************************/
827 * Emits code for a proj -> node
829 void emit_Proj(const ir_node *irn, ia32_emit_env_t *env) {
830 ir_node *pred = get_Proj_pred(irn);
832 if (get_irn_op(pred) == op_Start) {
833 switch(get_Proj_proj(irn)) {
834 case pn_Start_X_initial_exec:
843 /**********************************
846 * | | ___ _ __ _ _| |_) |
847 * | | / _ \| '_ \| | | | _ <
848 * | |___| (_) | |_) | |_| | |_) |
849 * \_____\___/| .__/ \__, |____/
852 **********************************/
855 * Emit movsb/w instructions to make mov count divideable by 4
857 static void emit_CopyB_prolog(FILE *F, int rem, int size) {
858 char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
860 fprintf(F, "\t/* memcopy %d bytes*/\n", size);
862 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "cld");
863 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* copy direction forward*/");
868 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "movsb");
869 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "; memcopy remainder 1");
872 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "movsw");
873 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "; memcopy remainder 2");
876 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "movsb");
877 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "; memcopy remainder 3");
879 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "movsw");
880 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "; memcopy remainder 3");
888 * Emit rep movsd instruction for memcopy.
890 void emit_ia32_CopyB(const ir_node *irn, ia32_emit_env_t *emit_env) {
891 FILE *F = emit_env->out;
892 tarval *tv = get_ia32_Immop_tarval(irn);
893 int rem = get_tarval_long(tv);
894 int size = get_tarval_long(get_ia32_Immop_tarval(get_irn_n(irn, 2)));
895 char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
897 emit_CopyB_prolog(F, rem, size);
899 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "rep movsd");
900 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "; memcopy");
905 * Emits unrolled memcopy.
907 void emit_ia32_CopyB_i(const ir_node *irn, ia32_emit_env_t *emit_env) {
908 tarval *tv = get_ia32_Immop_tarval(irn);
909 int size = get_tarval_long(tv);
910 FILE *F = emit_env->out;
911 char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
913 emit_CopyB_prolog(F, size & 0x3, size);
917 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "movsd");
918 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "; memcopy unrolled");
925 /***************************
929 * | | / _ \| '_ \ \ / /
930 * | |___| (_) | | | \ V /
931 * \_____\___/|_| |_|\_/
933 ***************************/
936 * Emit code for conversions (I, FP), (FP, I) and (FP, FP).
938 static void emit_ia32_Conv(const ir_node *irn, ia32_emit_env_t *emit_env) {
939 FILE *F = emit_env->out;
940 const lc_arg_env_t *env = ia32_get_arg_env();
941 char *from, *to, buf[64];
942 ir_mode *src_mode, *tgt_mode;
943 char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
945 src_mode = is_ia32_AddrModeS(irn) ? get_ia32_ls_mode(irn) : get_irn_mode(get_irn_n(irn, 2));
946 tgt_mode = get_ia32_res_mode(irn);
948 from = mode_is_float(src_mode) ? (get_mode_size_bits(src_mode) == 32 ? "ss" : "sd") : "si";
949 to = mode_is_float(tgt_mode) ? (get_mode_size_bits(tgt_mode) == 32 ? "ss" : "sd") : "si";
951 switch(get_ia32_op_type(irn)) {
953 lc_esnprintf(env, buf, sizeof(buf), "%1D, %3S", irn, irn);
956 lc_esnprintf(env, buf, sizeof(buf), "%1D, %s", irn, ia32_emit_am(irn, emit_env));
959 assert(0 && "unsupported op type for Conv");
962 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "cvt%s2%s %s", from, to, buf);
963 lc_esnprintf(env, cmnt_buf, SNPRINTF_BUF_LEN, "; %+F(%+F, %+F)", irn, src_mode, tgt_mode);
967 void emit_ia32_Conv_I2FP(const ir_node *irn, ia32_emit_env_t *emit_env) {
968 emit_ia32_Conv(irn, emit_env);
971 void emit_ia32_Conv_FP2I(const ir_node *irn, ia32_emit_env_t *emit_env) {
972 emit_ia32_Conv(irn, emit_env);
975 void emit_ia32_Conv_FP2FP(const ir_node *irn, ia32_emit_env_t *emit_env) {
976 emit_ia32_Conv(irn, emit_env);
981 /*******************************************
984 * | |__ ___ _ __ ___ __| | ___ ___
985 * | '_ \ / _ \ '_ \ / _ \ / _` |/ _ \/ __|
986 * | |_) | __/ | | | (_) | (_| | __/\__ \
987 * |_.__/ \___|_| |_|\___/ \__,_|\___||___/
989 *******************************************/
992 * Emits a backend call
994 void emit_be_Call(const ir_node *irn, ia32_emit_env_t *emit_env) {
995 FILE *F = emit_env->out;
996 entity *ent = be_Call_get_entity(irn);
997 char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
1000 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "call %s", get_entity_name(ent));
1003 lc_esnprintf(ia32_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "%1D", get_irn_n(irn, be_pos_Call_ptr));
1006 lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "; %+F (be_Call)", irn);
1012 * Emits code to increase stack pointer.
1014 void emit_be_IncSP(const ir_node *irn, ia32_emit_env_t *emit_env) {
1015 FILE *F = emit_env->out;
1016 unsigned offs = be_get_IncSP_offset(irn);
1017 be_stack_dir_t dir = be_get_IncSP_direction(irn);
1018 char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
1021 lc_esnprintf(ia32_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "add %1S,%s%u", irn,
1022 (dir == be_stack_dir_along) ? " -" : " ", offs);
1023 lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "; %+F (IncSP)", irn);
1026 snprintf(cmd_buf, SNPRINTF_BUF_LEN, " ");
1027 lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "; omitted %+F (IncSP) with 0", irn);
1034 * Emits code to set stack pointer.
1036 void emit_be_SetSP(const ir_node *irn, ia32_emit_env_t *emit_env) {
1037 FILE *F = emit_env->out;
1038 char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
1040 lc_esnprintf(ia32_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "mov %1D, %3S", irn, irn);
1041 lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "; %+F (restore SP)", irn);
1046 * Emits code for Copy.
1048 void emit_be_Copy(const ir_node *irn, ia32_emit_env_t *emit_env) {
1049 FILE *F = emit_env->out;
1050 char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
1052 lc_esnprintf(ia32_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "mov %1D, %1S", irn, irn);
1053 lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "; %+F", irn);
1058 * Emits code for exchange.
1060 void emit_be_Perm(const ir_node *irn, ia32_emit_env_t *emit_env) {
1061 FILE *F = emit_env->out;
1062 char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
1064 lc_esnprintf(ia32_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "xchg %1S, %2S", irn, irn);
1065 lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "; %+F(%1A, %2A)", irn, irn, irn);
1069 /***********************************************************************************
1072 * _ __ ___ __ _ _ _ __ | |_ _ __ __ _ _ __ ___ _____ _____ _ __| | __
1073 * | '_ ` _ \ / _` | | '_ \ | _| '__/ _` | '_ ` _ \ / _ \ \ /\ / / _ \| '__| |/ /
1074 * | | | | | | (_| | | | | | | | | | | (_| | | | | | | __/\ V V / (_) | | | <
1075 * |_| |_| |_|\__,_|_|_| |_| |_| |_| \__,_|_| |_| |_|\___| \_/\_/ \___/|_| |_|\_\
1077 ***********************************************************************************/
1080 * Enters the emitter functions for handled nodes into the generic
1081 * pointer of an opcode.
1083 static void ia32_register_emitters(void) {
1085 #define IA32_EMIT(a) op_ia32_##a->ops.generic = (op_func)emit_ia32_##a
1086 #define EMIT(a) op_##a->ops.generic = (op_func)emit_##a
1087 #define BE_EMIT(a) op_be_##a->ops.generic = (op_func)emit_be_##a
1089 /* first clear the generic function pointer for all ops */
1090 clear_irp_opcodes_generic_func();
1092 /* register all emitter functions defined in spec */
1093 ia32_register_spec_emitters();
1095 /* other ia32 emitter functions */
1098 IA32_EMIT(SwitchJmp);
1101 IA32_EMIT(Conv_I2FP);
1102 IA32_EMIT(Conv_FP2I);
1103 IA32_EMIT(Conv_FP2FP);
1105 /* benode emitter */
1122 * Emits code for a node.
1124 static void ia32_emit_node(const ir_node *irn, void *env) {
1125 ia32_emit_env_t *emit_env = env;
1126 firm_dbg_module_t *mod = emit_env->mod;
1127 FILE *F = emit_env->out;
1128 ir_op *op = get_irn_op(irn);
1130 DBG((mod, LEVEL_1, "emitting code for %+F\n", irn));
1132 if (op->ops.generic) {
1133 void (*emit)(const ir_node *, void *) = (void (*)(const ir_node *, void *))op->ops.generic;
1137 ir_fprintf(F, "\t%35s ; %+F \n", " ", irn);
1142 * Walks over the nodes in a block connected by scheduling edges
1143 * and emits code for each node.
1145 static void ia32_gen_block(ir_node *block, void *env) {
1148 if (! is_Block(block))
1151 fprintf(((ia32_emit_env_t *)env)->out, "BLOCK_%ld:\n", get_irn_node_nr(block));
1152 sched_foreach(block, irn) {
1153 ia32_emit_node(irn, env);
1159 * Emits code for function start.
1161 static void ia32_emit_func_prolog(FILE *F, ir_graph *irg) {
1162 entity *irg_ent = get_irg_entity(irg);
1163 const char *irg_name = get_entity_name(irg_ent);
1165 fprintf(F, "\tsection .text\n");
1166 if (get_entity_visibility(irg_ent) == visibility_external_visible) {
1167 fprintf(F, "global %s\n", irg_name);
1169 // fprintf(F, "\t.type\t%s, @function\n", irg_name);
1170 fprintf(F, "%s:\n", irg_name);
1174 * Emits code for function end
1176 static void ia32_emit_func_epilog(FILE *F, ir_graph *irg) {
1177 const char *irg_name = get_entity_name(get_irg_entity(irg));
1179 fprintf(F, "\tret\n\n");
1180 //printf(F, "\t.size\t%s, .-%s\n\n", irg_name, irg_name);
1184 * Sets labels for control flow nodes (jump target)
1185 * TODO: Jump optimization
1187 static void ia32_gen_labels(ir_node *block, void *env) {
1189 int n = get_Block_n_cfgpreds(block);
1191 for (n--; n >= 0; n--) {
1192 pred = get_Block_cfgpred(block, n);
1193 set_irn_link(pred, block);
1198 * Main driver. Emits the code for one routine.
1200 void ia32_gen_routine(FILE *F, ir_graph *irg, const ia32_code_gen_t *cg) {
1201 ia32_emit_env_t emit_env;
1203 emit_env.mod = firm_dbg_register("ir.be.codegen.ia32");
1205 emit_env.arch_env = cg->arch_env;
1207 emit_env.isa = (ia32_isa_t *)cg->arch_env->isa;
1209 /* set the global arch_env (needed by print hooks) */
1210 arch_env = cg->arch_env;
1212 ia32_register_emitters();
1214 ia32_emit_func_prolog(F, irg);
1215 irg_block_walk_graph(irg, ia32_gen_labels, NULL, &emit_env);
1216 irg_walk_blkwise_graph(irg, NULL, ia32_gen_block, &emit_env);
1217 ia32_emit_func_epilog(F, irg);