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 /*************************************************************
53 * (_) | | / _| | | | |
54 * _ __ _ __ _ _ __ | |_| |_ | |__ ___| |_ __ ___ _ __
55 * | '_ \| '__| | '_ \| __| _| | '_ \ / _ \ | '_ \ / _ \ '__|
56 * | |_) | | | | | | | |_| | | | | | __/ | |_) | __/ |
57 * | .__/|_| |_|_| |_|\__|_| |_| |_|\___|_| .__/ \___|_|
60 *************************************************************/
63 * returns true if a node has x87 registers
65 static int has_x87_register(const ir_node *n)
67 return get_irn_op(n)->flags & (irop_flag_machine << 1);
70 /* We always pass the ir_node which is a pointer. */
71 static int ia32_get_arg_type(const lc_arg_occ_t *occ) {
72 return lc_arg_type_ptr;
77 * Returns the register at in position pos.
79 static const arch_register_t *get_in_reg(const ir_node *irn, int pos) {
81 const arch_register_t *reg = NULL;
83 assert(get_irn_arity(irn) > pos && "Invalid IN position");
85 /* The out register of the operator at position pos is the
86 in register we need. */
87 op = get_irn_n(irn, pos);
89 reg = arch_get_irn_register(arch_env, op);
91 assert(reg && "no in register found");
96 * Returns the register at out position pos.
98 static const arch_register_t *get_out_reg(const ir_node *irn, int pos) {
100 const arch_register_t *reg = NULL;
102 /* 1st case: irn is not of mode_T, so it has only */
103 /* one OUT register -> good */
104 /* 2nd case: irn is of mode_T -> collect all Projs and ask the */
105 /* Proj with the corresponding projnum for the register */
107 if (get_irn_mode(irn) != mode_T) {
108 reg = arch_get_irn_register(arch_env, irn);
110 else if (is_ia32_irn(irn)) {
111 reg = get_ia32_out_reg(irn, pos);
114 const ir_edge_t *edge;
116 foreach_out_edge(irn, edge) {
117 proj = get_edge_src_irn(edge);
118 assert(is_Proj(proj) && "non-Proj from mode_T node");
119 if (get_Proj_proj(proj) == pos) {
120 reg = arch_get_irn_register(arch_env, proj);
126 assert(reg && "no out register found");
136 * Returns the name of the in register at position pos.
138 static const char *get_ia32_reg_name(ir_node *irn, int pos, enum io_direction in_out) {
139 const arch_register_t *reg;
141 static char *buf = NULL;
144 if (in_out == IN_REG) {
145 reg = get_in_reg(irn, pos);
148 /* destination address mode nodes don't have outputs */
149 if (is_ia32_irn(irn) && get_ia32_op_type(irn) == ia32_AddrModeD) {
153 reg = get_out_reg(irn, pos);
156 name = arch_register_get_name(reg);
162 len = strlen(name) + 2;
163 buf = xcalloc(1, len);
165 snprintf(buf, len, "%%%s", name);
171 * Get the register name for a node.
173 static int ia32_get_reg_name(lc_appendable_t *app,
174 const lc_arg_occ_t *occ, const lc_arg_value_t *arg)
177 ir_node *X = arg->v_ptr;
178 int nr = occ->width - 1;
181 return lc_appendable_snadd(app, "(null)", 6);
183 if (has_x87_register(X)) {
184 ia32_attr_t *attr = get_ia32_attr(X);
186 if (occ->conversion == 'S')
187 buf = arch_register_get_name(attr->x87[nr - 2]);
189 buf = arch_register_get_name(attr->x87[2 + nr]);
192 buf = get_ia32_reg_name(X, nr, occ->conversion == 'S' ? IN_REG : OUT_REG);
194 return lc_appendable_snadd(app, buf, strlen(buf));
198 * Get the x87 register name for a node.
200 static int ia32_get_x87_name(lc_appendable_t *app,
201 const lc_arg_occ_t *occ, const lc_arg_value_t *arg)
204 ir_node *X = arg->v_ptr;
205 int nr = occ->width - 1;
209 return lc_appendable_snadd(app, "(null)", 6);
211 attr = get_ia32_attr(X);
212 buf = attr->x87[nr]->name;
213 return lc_appendable_snadd(app, buf, strlen(buf));
217 * Returns the tarval, offset or scale of an ia32 as a string.
219 static int ia32_const_to_str(lc_appendable_t *app,
220 const lc_arg_occ_t *occ, const lc_arg_value_t *arg)
223 ir_node *X = arg->v_ptr;
226 return lc_arg_append(app, occ, "(null)", 6);
228 if (occ->conversion == 'C') {
229 buf = get_ia32_cnst(X);
232 buf = get_ia32_am_offs(X);
235 return buf ? lc_appendable_snadd(app, buf, strlen(buf)) : 0;
239 * Determines the SSE suffix depending on the mode.
241 static int ia32_get_mode_suffix(lc_appendable_t *app,
242 const lc_arg_occ_t *occ, const lc_arg_value_t *arg)
244 ir_node *X = arg->v_ptr;
245 ir_mode *mode = get_irn_mode(X);
247 if (mode == mode_T) {
248 mode = is_ia32_AddrModeS(X) || is_ia32_AddrModeD(X) ? get_ia32_ls_mode(X) : get_ia32_res_mode(X);
252 return lc_arg_append(app, occ, "(null)", 6);
254 if (mode_is_float(mode)) {
255 return lc_appendable_chadd(app, get_mode_size_bits(mode) == 32 ? 's' : 'd');
258 return lc_appendable_chadd(app, mode_is_signed(mode) ? 's' : 'z');
263 * Return the ia32 printf arg environment.
264 * We use the firm environment with some additional handlers.
266 const lc_arg_env_t *ia32_get_arg_env(void) {
267 static lc_arg_env_t *env = NULL;
269 static const lc_arg_handler_t ia32_reg_handler = { ia32_get_arg_type, ia32_get_reg_name };
270 static const lc_arg_handler_t ia32_const_handler = { ia32_get_arg_type, ia32_const_to_str };
271 static const lc_arg_handler_t ia32_mode_handler = { ia32_get_arg_type, ia32_get_mode_suffix };
272 static const lc_arg_handler_t ia32_x87_handler = { ia32_get_arg_type, ia32_get_x87_name };
275 /* extend the firm printer */
276 env = firm_get_arg_env();
278 lc_arg_register(env, "ia32:sreg", 'S', &ia32_reg_handler);
279 lc_arg_register(env, "ia32:dreg", 'D', &ia32_reg_handler);
280 lc_arg_register(env, "ia32:cnst", 'C', &ia32_const_handler);
281 lc_arg_register(env, "ia32:offs", 'O', &ia32_const_handler);
282 lc_arg_register(env, "ia32:mode", 'M', &ia32_mode_handler);
283 lc_arg_register(env, "ia32:x87", 'X', &ia32_x87_handler);
289 static char *ia32_get_reg_name_for_mode(ia32_emit_env_t *env, ir_mode *mode, const arch_register_t *reg) {
290 switch(get_mode_size_bits(mode)) {
292 return ia32_get_mapped_reg_name(env->isa->regs_8bit, reg);
294 return ia32_get_mapped_reg_name(env->isa->regs_16bit, reg);
296 return (char *)arch_register_get_name(reg);
301 * Emits registers and/or address mode of a binary operation.
303 char *ia32_emit_binop(const ir_node *n, ia32_emit_env_t *env) {
304 static char *buf = NULL;
306 /* verify that this function is never called on non-AM supporting operations */
307 //assert(get_ia32_am_support(n) != ia32_am_None && "emit binop expects addressmode support");
309 #define PRODUCES_RESULT(n) \
310 (!(is_ia32_St(n) || \
311 is_ia32_Store8Bit(n) || \
312 is_ia32_CondJmp(n) || \
313 is_ia32_fCondJmp(n) || \
314 is_ia32_SwitchJmp(n)))
317 buf = xcalloc(1, SNPRINTF_BUF_LEN);
320 memset(buf, 0, SNPRINTF_BUF_LEN);
323 switch(get_ia32_op_type(n)) {
325 if (is_ia32_ImmConst(n) || is_ia32_ImmSymConst(n)) {
326 lc_esnprintf(ia32_get_arg_env(), buf, SNPRINTF_BUF_LEN, "%3S, %s", n, get_ia32_cnst(n));
329 const arch_register_t *in1 = get_in_reg(n, 2);
330 const arch_register_t *in2 = get_in_reg(n, 3);
331 const arch_register_t *out = PRODUCES_RESULT(n) ? get_out_reg(n, 0) : NULL;
332 const arch_register_t *in;
335 in = out ? (REGS_ARE_EQUAL(out, in2) ? in1 : in2) : in2;
336 out = out ? out : in1;
337 in_name = arch_register_get_name(in);
339 if (is_ia32_emit_cl(n)) {
340 assert(REGS_ARE_EQUAL(&ia32_gp_regs[REG_ECX], in) && "shift operation needs ecx");
344 snprintf(buf, SNPRINTF_BUF_LEN, "%%%s, %%%s", arch_register_get_name(out), in_name);
348 if (is_ia32_ImmConst(n) || is_ia32_ImmSymConst(n)) {
349 assert(! PRODUCES_RESULT(n) && "Source AM with Const must not produce result");
350 snprintf(buf, SNPRINTF_BUF_LEN, "%s, %s", get_ia32_cnst(n), ia32_emit_am(n, env));
353 lc_esnprintf(ia32_get_arg_env(), buf, SNPRINTF_BUF_LEN, "%4S, %s", n, ia32_emit_am(n, env));
357 if (is_ia32_ImmConst(n) || is_ia32_ImmSymConst(n)) {
358 lc_esnprintf(ia32_get_arg_env(), buf, SNPRINTF_BUF_LEN, "%s,%s%s",
359 ia32_emit_am(n, env),
360 is_ia32_ImmSymConst(n) ? " OFFSET FLAT:" : " ", /* In case of a symconst we must add OFFSET to */
361 get_ia32_cnst(n)); /* tell the assembler to store it's address. */
364 const arch_register_t *in1 = get_in_reg(n, 2);
365 ir_mode *mode = get_ia32_res_mode(n);
368 mode = mode ? mode : get_ia32_ls_mode(n);
369 in_name = ia32_get_reg_name_for_mode(env, mode, in1);
371 if (is_ia32_emit_cl(n)) {
372 assert(REGS_ARE_EQUAL(&ia32_gp_regs[REG_ECX], in1) && "shift operation needs ecx");
376 lc_esnprintf(ia32_get_arg_env(), buf, SNPRINTF_BUF_LEN, "%s, %%%s", ia32_emit_am(n, env), in_name);
380 assert(0 && "unsupported op type");
383 #undef PRODUCES_RESULT
389 * Emits registers and/or address mode of a binary operation.
391 char *ia32_emit_x87_binop(const ir_node *n, ia32_emit_env_t *env) {
392 static char *buf = NULL;
394 /* verify that this function is never called on non-AM supporting operations */
395 //assert(get_ia32_am_support(n) != ia32_am_None && "emit binop expects addressmode support");
398 buf = xcalloc(1, SNPRINTF_BUF_LEN);
401 memset(buf, 0, SNPRINTF_BUF_LEN);
404 switch(get_ia32_op_type(n)) {
406 if (is_ia32_ImmConst(n) || is_ia32_ImmSymConst(n)) {
407 lc_esnprintf(ia32_get_arg_env(), buf, SNPRINTF_BUF_LEN, "%3S, %s", n, get_ia32_cnst(n));
410 ia32_attr_t *attr = get_ia32_attr(n);
411 const arch_register_t *in1 = attr->x87[0];
412 const arch_register_t *in2 = attr->x87[1];
413 const arch_register_t *out = attr->x87[2];
414 const arch_register_t *in;
417 in = out ? (REGS_ARE_EQUAL(out, in2) ? in1 : in2) : in2;
418 out = out ? out : in1;
419 in_name = arch_register_get_name(in);
421 snprintf(buf, SNPRINTF_BUF_LEN, "%%%s, %%%s", arch_register_get_name(out), in_name);
426 lc_esnprintf(ia32_get_arg_env(), buf, SNPRINTF_BUF_LEN, "%s", ia32_emit_am(n, env));
429 assert(0 && "unsupported op type");
432 #undef PRODUCES_RESULT
438 * Emits registers and/or address mode of a unary operation.
440 char *ia32_emit_unop(const ir_node *n, ia32_emit_env_t *env) {
441 static char *buf = NULL;
444 buf = xcalloc(1, SNPRINTF_BUF_LEN);
447 memset(buf, 0, SNPRINTF_BUF_LEN);
450 switch(get_ia32_op_type(n)) {
452 if (is_ia32_ImmConst(n) || is_ia32_ImmSymConst(n)) {
453 lc_esnprintf(ia32_get_arg_env(), buf, SNPRINTF_BUF_LEN, "%C", n);
456 lc_esnprintf(ia32_get_arg_env(), buf, SNPRINTF_BUF_LEN, "%1D", n);
460 snprintf(buf, SNPRINTF_BUF_LEN, ia32_emit_am(n, env));
463 assert(0 && "unsupported op type");
470 * Emits address mode.
472 char *ia32_emit_am(const ir_node *n, ia32_emit_env_t *env) {
473 ia32_am_flavour_t am_flav = get_ia32_am_flavour(n);
477 static struct obstack *obst = NULL;
478 ir_mode *mode = get_ia32_ls_mode(n);
480 if (! is_ia32_Lea(n))
481 assert(mode && "AM node must have ls_mode attribute set.");
484 obst = xcalloc(1, sizeof(*obst));
487 obstack_free(obst, NULL);
490 /* obstack_free with NULL results in an uninitialized obstack */
494 switch (get_mode_size_bits(mode)) {
496 obstack_printf(obst, "BYTE PTR ");
499 obstack_printf(obst, "WORD PTR ");
502 obstack_printf(obst, "DWORD PTR ");
505 if (has_x87_register(n))
506 /* ARGHHH: x87 wants QWORD PTR but SSE must be WITHOUT */
507 obstack_printf(obst, "QWORD PTR ");
511 obstack_printf(obst, "XWORD PTR ");
518 /* emit address mode symconst */
519 if (get_ia32_am_sc(n)) {
520 if (is_ia32_am_sc_sign(n))
521 obstack_printf(obst, "-");
522 obstack_printf(obst, "%s", get_id_str(get_ia32_am_sc(n)));
525 if (am_flav & ia32_B) {
526 obstack_printf(obst, "[");
527 lc_eoprintf(ia32_get_arg_env(), obst, "%1S", n);
531 if (am_flav & ia32_I) {
533 obstack_printf(obst, "+");
536 obstack_printf(obst, "[");
539 lc_eoprintf(ia32_get_arg_env(), obst, "%2S", n);
541 if (am_flav & ia32_S) {
542 obstack_printf(obst, "*%d", 1 << get_ia32_am_scale(n));
548 if (am_flav & ia32_O) {
549 s = get_ia32_am_offs(n);
552 /* omit explicit + if there was no base or index */
554 obstack_printf(obst, "[");
559 obstack_printf(obst, s);
565 obstack_printf(obst, "] ");
567 size = obstack_object_size(obst);
568 s = obstack_finish(obst);
577 * Formated print of commands and comments.
579 static void ia32_fprintf_format(FILE *F, const ir_node *irn, char *cmd_buf, char *cmnt_buf) {
581 const char *name = irn ? be_retrieve_dbg_info(get_irn_dbg_info((ir_node *)irn), &lineno) : NULL;
584 fprintf(F, "\t%-35s %-60s /* %s:%u */\n", cmd_buf, cmnt_buf, name, lineno);
586 fprintf(F, "\t%-35s %-60s\n", cmd_buf, cmnt_buf);
592 * Add a number to a prefix. This number will not be used a second time.
594 static char *get_unique_label(char *buf, size_t buflen, const char *prefix) {
595 static unsigned long id = 0;
596 snprintf(buf, buflen, "%s%lu", prefix, ++id);
602 /*************************************************
605 * ___ _ __ ___ _| |_ ___ ___ _ __ __| |
606 * / _ \ '_ ` _ \| | __| / __/ _ \| '_ \ / _` |
607 * | __/ | | | | | | |_ | (_| (_) | | | | (_| |
608 * \___|_| |_| |_|_|\__| \___\___/|_| |_|\__,_|
610 *************************************************/
613 #define IA32_DO_EMIT(irn) ia32_fprintf_format(F, irn, cmd_buf, cmnt_buf)
616 * coding of conditions
618 struct cmp2conditon_t {
624 * positive conditions for signed compares
626 static const struct cmp2conditon_t cmp2condition_s[] = {
627 { NULL, pn_Cmp_False }, /* always false */
628 { "e", pn_Cmp_Eq }, /* == */
629 { "l", pn_Cmp_Lt }, /* < */
630 { "le", pn_Cmp_Le }, /* <= */
631 { "g", pn_Cmp_Gt }, /* > */
632 { "ge", pn_Cmp_Ge }, /* >= */
633 { "ne", pn_Cmp_Lg }, /* != */
634 { "ordered", pn_Cmp_Leg }, /* Floating point: ordered */
635 { "unordered", pn_Cmp_Uo }, /* FLoting point: unordered */
636 { "unordered or ==", pn_Cmp_Ue }, /* Floating point: unordered or == */
637 { "unordered or <", pn_Cmp_Ul }, /* Floating point: unordered or < */
638 { "unordered or <=", pn_Cmp_Ule }, /* Floating point: unordered or <= */
639 { "unordered or >", pn_Cmp_Ug }, /* Floating point: unordered or > */
640 { "unordered or >=", pn_Cmp_Uge }, /* Floating point: unordered or >= */
641 { "unordered or !=", pn_Cmp_Ne }, /* Floating point: unordered or != */
642 { NULL, pn_Cmp_True }, /* always true */
646 * positive conditions for unsigned compares
648 static const struct cmp2conditon_t cmp2condition_u[] = {
649 { NULL, pn_Cmp_False }, /* always false */
650 { "e", pn_Cmp_Eq }, /* == */
651 { "b", pn_Cmp_Lt }, /* < */
652 { "be", pn_Cmp_Le }, /* <= */
653 { "a", pn_Cmp_Gt }, /* > */
654 { "ae", pn_Cmp_Ge }, /* >= */
655 { "ne", pn_Cmp_Lg }, /* != */
656 { "ordered", pn_Cmp_Leg }, /* Floating point: ordered */
657 { "unordered", pn_Cmp_Uo }, /* FLoting point: unordered */
658 { "unordered or ==", pn_Cmp_Ue }, /* Floating point: unordered or == */
659 { "unordered or <", pn_Cmp_Ul }, /* Floating point: unordered or < */
660 { "unordered or <=", pn_Cmp_Ule }, /* Floating point: unordered or <= */
661 { "unordered or >", pn_Cmp_Ug }, /* Floating point: unordered or > */
662 { "unordered or >=", pn_Cmp_Uge }, /* Floating point: unordered or >= */
663 { "unordered or !=", pn_Cmp_Ne }, /* Floating point: unordered or != */
664 { NULL, pn_Cmp_True }, /* always true */
668 * returns the condition code
670 static const char *get_cmp_suffix(int cmp_code, int unsigned_cmp)
672 assert(cmp2condition_s[cmp_code].num == cmp_code);
673 assert(cmp2condition_u[cmp_code].num == cmp_code);
675 return unsigned_cmp ? cmp2condition_u[cmp_code & 7].name : cmp2condition_s[cmp_code & 7].name;
679 * Returns the target block for a control flow node.
681 static ir_node *get_cfop_target_block(const ir_node *irn) {
682 return get_irn_link(irn);
686 * Returns the target label for a control flow node.
688 static char *get_cfop_target(const ir_node *irn, char *buf) {
689 ir_node *bl = get_cfop_target_block(irn);
691 snprintf(buf, SNPRINTF_BUF_LEN, BLOCK_PREFIX("%ld"), get_irn_node_nr(bl));
695 /** Return the next block in Block schedule */
696 static ir_node *next_blk_sched(const ir_node *block) {
697 return get_irn_link(block);
701 * Emits the jump sequence for a conditional jump (cmp + jmp_true + jmp_false)
703 static void finish_CondJmp(FILE *F, const ir_node *irn, ir_mode *mode) {
704 const ir_node *proj1, *proj2 = NULL;
705 const ir_node *block, *next_bl = NULL;
706 const ir_edge_t *edge;
707 char buf[SNPRINTF_BUF_LEN];
708 char cmd_buf[SNPRINTF_BUF_LEN];
709 char cmnt_buf[SNPRINTF_BUF_LEN];
711 /* get both Proj's */
712 edge = get_irn_out_edge_first(irn);
713 proj1 = get_edge_src_irn(edge);
714 assert(is_Proj(proj1) && "CondJmp with a non-Proj");
716 edge = get_irn_out_edge_next(irn, edge);
718 proj2 = get_edge_src_irn(edge);
719 assert(is_Proj(proj2) && "CondJmp with a non-Proj");
722 /* for now, the code works for scheduled and non-schedules blocks */
723 block = get_nodes_block(irn);
725 /* we have a block schedule */
726 next_bl = next_blk_sched(block);
728 if (get_cfop_target_block(proj1) == next_bl) {
729 /* exchange both proj's so the second one can be omitted */
730 const ir_node *t = proj1;
736 /* the first Proj must always be created */
737 if (get_Proj_proj(proj1) == pn_Cond_true) {
738 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "j%s %s",
739 get_cmp_suffix(get_ia32_pncode(irn), !mode_is_signed(get_irn_mode(get_irn_n(irn, 0)))),
740 get_cfop_target(proj1, buf));
741 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* cmp(a, b) == TRUE */");
744 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "j%s %s",
745 get_cmp_suffix(get_negated_pnc(get_ia32_pncode(irn), mode),
746 !mode_is_signed(get_irn_mode(get_irn_n(irn, 0)))),
747 get_cfop_target(proj1, buf));
748 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* cmp(a, b) == FALSE */");
752 /* the second Proj might be a fallthrough */
754 if (get_cfop_target_block(proj2) != next_bl) {
755 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "jmp %s", get_cfop_target(proj2, buf));
756 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* otherwise */");
760 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* fallthrogh %s */", get_cfop_target(proj2, buf));
767 * Emits code for conditional jump.
769 static void CondJmp_emitter(const ir_node *irn, ia32_emit_env_t *env) {
771 char cmd_buf[SNPRINTF_BUF_LEN];
772 char cmnt_buf[SNPRINTF_BUF_LEN];
774 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "cmp %s", ia32_emit_binop(irn, env));
775 lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* %+F */", irn);
777 finish_CondJmp(F, irn, get_ia32_res_mode(irn));
781 * Emits code for conditional jump with two variables.
783 static void emit_ia32_CondJmp(const ir_node *irn, ia32_emit_env_t *env) {
784 CondJmp_emitter(irn, env);
788 * Emits code for conditional jump with immediate.
790 static void emit_ia32_CondJmp_i(const ir_node *irn, ia32_emit_env_t *env) {
791 CondJmp_emitter(irn, env);
795 * Emits code for conditional test and jump.
797 static void TestJmp_emitter(const ir_node *irn, ia32_emit_env_t *env) {
799 #define IA32_IS_IMMOP (is_ia32_ImmConst(irn) || is_ia32_ImmSymConst(irn))
802 const char *op1 = arch_register_get_name(get_in_reg(irn, 0));
803 const char *op2 = IA32_IS_IMMOP ? get_ia32_cnst(irn) : NULL;
804 char cmd_buf[SNPRINTF_BUF_LEN];
805 char cmnt_buf[SNPRINTF_BUF_LEN];
808 op2 = arch_register_get_name(get_in_reg(irn, 1));
810 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "test %%%s,%s%s ", op1, IA32_IS_IMMOP ? " " : " %", op2);
811 lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* %+F */", irn);
814 finish_CondJmp(F, irn, get_ia32_res_mode(irn));
820 * Emits code for conditional test and jump with two variables.
822 static void emit_ia32_TestJmp(const ir_node *irn, ia32_emit_env_t *env) {
823 TestJmp_emitter(irn, env);
826 static void emit_ia32_CJmp(const ir_node *irn, ia32_emit_env_t *env) {
828 char cmd_buf[SNPRINTF_BUF_LEN];
829 char cmnt_buf[SNPRINTF_BUF_LEN];
831 snprintf(cmd_buf, SNPRINTF_BUF_LEN, " ");
832 lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* %+F omitted redundant test */", irn);
834 finish_CondJmp(F, irn, get_ia32_res_mode(irn));
837 static void emit_ia32_CJmpAM(const ir_node *irn, ia32_emit_env_t *env) {
839 char cmd_buf[SNPRINTF_BUF_LEN];
840 char cmnt_buf[SNPRINTF_BUF_LEN];
842 snprintf(cmd_buf, SNPRINTF_BUF_LEN, " ");
843 lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* %+F omitted redundant test/cmp */", irn);
845 finish_CondJmp(F, irn, get_ia32_res_mode(irn));
848 /*********************************************************
851 * ___ _ __ ___ _| |_ _ _ _ _ __ ___ _ __ ___
852 * / _ \ '_ ` _ \| | __| | | | | | '_ ` _ \| '_ \/ __|
853 * | __/ | | | | | | |_ | | |_| | | | | | | |_) \__ \
854 * \___|_| |_| |_|_|\__| | |\__,_|_| |_| |_| .__/|___/
857 *********************************************************/
859 /* jump table entry (target and corresponding number) */
860 typedef struct _branch_t {
865 /* jump table for switch generation */
866 typedef struct _jmp_tbl_t {
867 ir_node *defProj; /**< default target */
868 int min_value; /**< smallest switch case */
869 int max_value; /**< largest switch case */
870 int num_branches; /**< number of jumps */
871 char *label; /**< label of the jump table */
872 branch_t *branches; /**< jump array */
876 * Compare two variables of type branch_t. Used to sort all switch cases
878 static int ia32_cmp_branch_t(const void *a, const void *b) {
879 branch_t *b1 = (branch_t *)a;
880 branch_t *b2 = (branch_t *)b;
882 if (b1->value <= b2->value)
889 * Emits code for a SwitchJmp (creates a jump table if
890 * possible otherwise a cmp-jmp cascade). Port from
893 static void emit_ia32_SwitchJmp(const ir_node *irn, ia32_emit_env_t *emit_env) {
894 unsigned long interval;
895 char buf[SNPRINTF_BUF_LEN];
896 int last_value, i, pn, do_jmp_tbl = 1;
899 const ir_edge_t *edge;
900 const lc_arg_env_t *env = ia32_get_arg_env();
901 FILE *F = emit_env->out;
902 char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
904 /* fill the table structure */
905 tbl.label = xmalloc(SNPRINTF_BUF_LEN);
906 tbl.label = get_unique_label(tbl.label, SNPRINTF_BUF_LEN, "JMPTBL_");
908 tbl.num_branches = get_irn_n_edges(irn);
909 tbl.branches = xcalloc(tbl.num_branches, sizeof(tbl.branches[0]));
910 tbl.min_value = INT_MAX;
911 tbl.max_value = INT_MIN;
914 /* go over all proj's and collect them */
915 foreach_out_edge(irn, edge) {
916 proj = get_edge_src_irn(edge);
917 assert(is_Proj(proj) && "Only proj allowed at SwitchJmp");
919 pn = get_Proj_proj(proj);
921 /* create branch entry */
922 tbl.branches[i].target = proj;
923 tbl.branches[i].value = pn;
925 tbl.min_value = pn < tbl.min_value ? pn : tbl.min_value;
926 tbl.max_value = pn > tbl.max_value ? pn : tbl.max_value;
928 /* check for default proj */
929 if (pn == get_ia32_pncode(irn)) {
930 assert(tbl.defProj == NULL && "found two defProjs at SwitchJmp");
937 /* sort the branches by their number */
938 qsort(tbl.branches, tbl.num_branches, sizeof(tbl.branches[0]), ia32_cmp_branch_t);
940 /* two-complement's magic make this work without overflow */
941 interval = tbl.max_value - tbl.min_value;
943 /* check value interval */
944 if (interval > 16 * 1024) {
948 /* check ratio of value interval to number of branches */
949 if ((float)(interval + 1) / (float)tbl.num_branches > 8.0) {
955 if (tbl.min_value != 0) {
956 lc_esnprintf(env, cmd_buf, SNPRINTF_BUF_LEN, "cmpl %lu, -%d(%1S)",
957 interval, tbl.min_value, irn);
958 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* first switch value is not 0 */");
963 lc_esnprintf(env, cmd_buf, SNPRINTF_BUF_LEN, "cmpl %lu, %1S", interval, irn);
964 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* compare for switch */");
969 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "ja %s", get_cfop_target(tbl.defProj, buf));
970 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* default jump if out of range */");
973 if (tbl.num_branches > 1) {
976 lc_esnprintf(env, cmd_buf, SNPRINTF_BUF_LEN, "jmp [%1S*4+%s]", irn, tbl.label);
977 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* get jump table entry as target */");
980 fprintf(F, "\t.section\t.rodata\n");
981 fprintf(F, "\t.align 4\n");
983 fprintf(F, "%s:\n", tbl.label);
985 snprintf(cmd_buf, SNPRINTF_BUF_LEN, ".long %s", get_cfop_target(tbl.branches[0].target, buf));
986 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* case %d */\n", tbl.branches[0].value);
989 last_value = tbl.branches[0].value;
990 for (i = 1; i < tbl.num_branches; ++i) {
991 while (++last_value < tbl.branches[i].value) {
992 snprintf(cmd_buf, SNPRINTF_BUF_LEN, ".long %s", get_cfop_target(tbl.defProj, buf));
993 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* default case */");
996 snprintf(cmd_buf, SNPRINTF_BUF_LEN, ".long %s", get_cfop_target(tbl.branches[i].target, buf));
997 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* case %d */", last_value);
1001 fprintf(F, "\t.text");
1004 /* one jump is enough */
1005 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "jmp %s", get_cfop_target(tbl.branches[0].target, buf));
1006 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* only one case given */");
1010 else { // no jump table
1011 for (i = 0; i < tbl.num_branches; ++i) {
1012 lc_esnprintf(env, cmd_buf, SNPRINTF_BUF_LEN, "cmpl %d, %1S", tbl.branches[i].value, irn);
1013 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* case %d */", i);
1015 fprintf(F, "\tje %s\n", get_cfop_target(tbl.branches[i].target, buf));
1018 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "jmp %s", get_cfop_target(tbl.defProj, buf));
1019 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* default case */");
1030 * Emits code for a unconditional jump.
1032 static void emit_Jmp(const ir_node *irn, ia32_emit_env_t *env) {
1033 ir_node *block, *next_bl;
1035 char buf[SNPRINTF_BUF_LEN], cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
1037 /* for now, the code works for scheduled and non-schedules blocks */
1038 block = get_nodes_block(irn);
1040 /* we have a block schedule */
1041 next_bl = next_blk_sched(block);
1042 if (get_cfop_target_block(irn) != next_bl) {
1043 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "jmp %s", get_cfop_target(irn, buf));
1044 lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* %+F(%+F) */", irn, get_cfop_target_block(irn));
1048 lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* fallthrough %s */", get_cfop_target(irn, buf));
1053 /****************************
1056 * _ __ _ __ ___ _ ___
1057 * | '_ \| '__/ _ \| |/ __|
1058 * | |_) | | | (_) | |\__ \
1059 * | .__/|_| \___/| ||___/
1062 ****************************/
1065 * Emits code for a proj -> node
1067 static void emit_Proj(const ir_node *irn, ia32_emit_env_t *env) {
1068 ir_node *pred = get_Proj_pred(irn);
1070 if (get_irn_op(pred) == op_Start) {
1071 switch(get_Proj_proj(irn)) {
1072 case pn_Start_X_initial_exec:
1081 /**********************************
1084 * | | ___ _ __ _ _| |_) |
1085 * | | / _ \| '_ \| | | | _ <
1086 * | |___| (_) | |_) | |_| | |_) |
1087 * \_____\___/| .__/ \__, |____/
1090 **********************************/
1093 * Emit movsb/w instructions to make mov count divideable by 4
1095 static void emit_CopyB_prolog(FILE *F, int rem, int size) {
1096 char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
1098 fprintf(F, "\t/* memcopy %d bytes*/\n", size);
1100 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "cld");
1101 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* copy direction forward*/");
1106 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "movsb");
1107 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* memcopy remainder 1 */");
1110 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "movsw");
1111 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* memcopy remainder 2 */");
1114 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "movsb");
1115 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* memcopy remainder 3 */");
1117 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "movsw");
1118 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* memcopy remainder 3 */");
1126 * Emit rep movsd instruction for memcopy.
1128 static void emit_ia32_CopyB(const ir_node *irn, ia32_emit_env_t *emit_env) {
1129 FILE *F = emit_env->out;
1130 tarval *tv = get_ia32_Immop_tarval(irn);
1131 int rem = get_tarval_long(tv);
1132 int size = get_tarval_long(get_ia32_Immop_tarval(get_irn_n(irn, 2)));
1133 char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
1135 emit_CopyB_prolog(F, rem, size);
1137 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "rep movsd");
1138 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* memcopy */");
1143 * Emits unrolled memcopy.
1145 static void emit_ia32_CopyB_i(const ir_node *irn, ia32_emit_env_t *emit_env) {
1146 tarval *tv = get_ia32_Immop_tarval(irn);
1147 int size = get_tarval_long(tv);
1148 FILE *F = emit_env->out;
1149 char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
1151 emit_CopyB_prolog(F, size & 0x3, size);
1155 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "movsd");
1156 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* memcopy unrolled */");
1163 /***************************
1167 * | | / _ \| '_ \ \ / /
1168 * | |___| (_) | | | \ V /
1169 * \_____\___/|_| |_|\_/
1171 ***************************/
1174 * Emit code for conversions (I, FP), (FP, I) and (FP, FP).
1176 static void emit_ia32_Conv_with_FP(const ir_node *irn, ia32_emit_env_t *emit_env) {
1177 FILE *F = emit_env->out;
1178 const lc_arg_env_t *env = ia32_get_arg_env();
1179 char *from, *to, buf[64];
1180 ir_mode *src_mode, *tgt_mode;
1181 char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
1183 src_mode = is_ia32_AddrModeS(irn) ? get_ia32_ls_mode(irn) : get_irn_mode(get_irn_n(irn, 2));
1184 tgt_mode = get_ia32_res_mode(irn);
1186 from = mode_is_float(src_mode) ? (get_mode_size_bits(src_mode) == 32 ? "ss" : "sd") : "si";
1187 to = mode_is_float(tgt_mode) ? (get_mode_size_bits(tgt_mode) == 32 ? "ss" : "sd") : "si";
1189 switch(get_ia32_op_type(irn)) {
1191 lc_esnprintf(env, buf, sizeof(buf), "%1D, %3S", irn, irn);
1193 case ia32_AddrModeS:
1194 lc_esnprintf(env, buf, sizeof(buf), "%1D, %s", irn, ia32_emit_am(irn, emit_env));
1197 assert(0 && "unsupported op type for Conv");
1200 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "cvt%s2%s %s", from, to, buf);
1201 lc_esnprintf(env, cmnt_buf, SNPRINTF_BUF_LEN, "/* %+F(%+F, %+F) */", irn, src_mode, tgt_mode);
1205 static void emit_ia32_Conv_I2FP(const ir_node *irn, ia32_emit_env_t *emit_env) {
1206 emit_ia32_Conv_with_FP(irn, emit_env);
1209 static void emit_ia32_Conv_FP2I(const ir_node *irn, ia32_emit_env_t *emit_env) {
1210 emit_ia32_Conv_with_FP(irn, emit_env);
1213 static void emit_ia32_Conv_FP2FP(const ir_node *irn, ia32_emit_env_t *emit_env) {
1214 emit_ia32_Conv_with_FP(irn, emit_env);
1218 * Emits code for an Int conversion.
1220 static void emit_ia32_Conv_I2I(const ir_node *irn, ia32_emit_env_t *emit_env) {
1221 FILE *F = emit_env->out;
1222 const lc_arg_env_t *env = ia32_get_arg_env();
1223 char *move_cmd, *conv_cmd;
1224 ir_mode *src_mode, *tgt_mode;
1226 char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
1227 const arch_register_t *in_reg, *out_reg;
1229 src_mode = is_ia32_AddrModeS(irn) ? get_ia32_ls_mode(irn) : get_irn_mode(get_irn_n(irn, 2));
1230 tgt_mode = get_ia32_res_mode(irn);
1232 n = get_mode_size_bits(src_mode);
1233 m = get_mode_size_bits(tgt_mode);
1235 if (mode_is_signed(n < m ? src_mode : tgt_mode)) {
1237 if (n == 8 || m == 8)
1239 else if (n == 16 || m == 16)
1242 assert(0 && "unsupported Conv_I2I");
1249 switch(get_ia32_op_type(irn)) {
1251 in_reg = get_in_reg(irn, 2);
1252 out_reg = get_out_reg(irn, 0);
1254 if (REGS_ARE_EQUAL(in_reg, &ia32_gp_regs[REG_EAX]) &&
1255 REGS_ARE_EQUAL(out_reg, in_reg) &&
1256 mode_is_signed(n < m ? src_mode : tgt_mode))
1258 /* argument and result are both in EAX and */
1259 /* signedness is ok: -> use converts */
1260 lc_esnprintf(env, cmd_buf, SNPRINTF_BUF_LEN, "%s", conv_cmd);
1262 else if (REGS_ARE_EQUAL(out_reg, in_reg) &&
1263 ! mode_is_signed(n < m ? src_mode : tgt_mode))
1265 /* argument and result are in the same register */
1266 /* and signedness is ok: -> use and with mask */
1267 int mask = (1 << (n < m ? n : m)) - 1;
1268 lc_esnprintf(env, cmd_buf, SNPRINTF_BUF_LEN, "and %1D, 0x%x", irn, mask);
1271 /* use move w/o sign extension */
1272 lc_esnprintf(env, cmd_buf, SNPRINTF_BUF_LEN, "%s %1D, %%%s",
1273 move_cmd, irn, ia32_get_reg_name_for_mode(emit_env, n < m ? src_mode : tgt_mode, in_reg));
1277 case ia32_AddrModeS:
1278 lc_esnprintf(env, cmd_buf, SNPRINTF_BUF_LEN, "%s %1D, %s",
1279 move_cmd, irn, ia32_emit_am(irn, emit_env));
1282 assert(0 && "unsupported op type for Conv");
1285 lc_esnprintf(env, cmnt_buf, SNPRINTF_BUF_LEN, "/* %+F(%d Bit mode_%F -> %d Bit mode_%F) */",
1286 irn, n, src_mode, m, tgt_mode);
1292 * Emits code for an 8Bit Int conversion.
1294 void emit_ia32_Conv_I2I8Bit(const ir_node *irn, ia32_emit_env_t *emit_env) {
1295 emit_ia32_Conv_I2I(irn, emit_env);
1299 /*******************************************
1302 * | |__ ___ _ __ ___ __| | ___ ___
1303 * | '_ \ / _ \ '_ \ / _ \ / _` |/ _ \/ __|
1304 * | |_) | __/ | | | (_) | (_| | __/\__ \
1305 * |_.__/ \___|_| |_|\___/ \__,_|\___||___/
1307 *******************************************/
1310 * Emits a backend call
1312 static void emit_be_Call(const ir_node *irn, ia32_emit_env_t *emit_env) {
1313 FILE *F = emit_env->out;
1314 entity *ent = be_Call_get_entity(irn);
1315 char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
1318 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "call %s", get_entity_name(ent));
1321 lc_esnprintf(ia32_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "%1D", get_irn_n(irn, be_pos_Call_ptr));
1324 lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* %+F (be_Call) */", irn);
1330 * Emits code to increase stack pointer.
1332 static void emit_be_IncSP(const ir_node *irn, ia32_emit_env_t *emit_env) {
1333 FILE *F = emit_env->out;
1334 unsigned offs = be_get_IncSP_offset(irn);
1335 be_stack_dir_t dir = be_get_IncSP_direction(irn);
1336 char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
1339 lc_esnprintf(ia32_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "add %1S,%s%u", irn,
1340 (dir == be_stack_dir_expand) ? " -" : " ", offs);
1341 lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* %+F (IncSP) */", irn);
1344 snprintf(cmd_buf, SNPRINTF_BUF_LEN, " ");
1345 lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* omitted %+F (IncSP) with 0 */", irn);
1352 * Emits code to set stack pointer.
1354 static void emit_be_SetSP(const ir_node *irn, ia32_emit_env_t *emit_env) {
1355 FILE *F = emit_env->out;
1356 char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
1358 lc_esnprintf(ia32_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "mov %1D, %3S", irn, irn);
1359 lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* %+F (restore SP) */", irn);
1364 * Emits code for Copy.
1366 static void emit_be_Copy(const ir_node *irn, ia32_emit_env_t *emit_env) {
1367 FILE *F = emit_env->out;
1368 char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
1370 if (mode_is_float(get_irn_mode(irn)))
1371 lc_esnprintf(ia32_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "movs%M %1D, %1S", irn, irn, irn);
1373 lc_esnprintf(ia32_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "mov %1D, %1S", irn, irn);
1374 lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* %+F */", irn);
1379 * Emits code for exchange.
1381 static void emit_be_Perm(const ir_node *irn, ia32_emit_env_t *emit_env) {
1382 FILE *F = emit_env->out;
1383 char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
1385 lc_esnprintf(ia32_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "xchg %1S, %2S", irn, irn);
1386 lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* %+F(%1A, %2A) */", irn, irn, irn);
1392 /***********************************************************************************
1395 * _ __ ___ __ _ _ _ __ | |_ _ __ __ _ _ __ ___ _____ _____ _ __| | __
1396 * | '_ ` _ \ / _` | | '_ \ | _| '__/ _` | '_ ` _ \ / _ \ \ /\ / / _ \| '__| |/ /
1397 * | | | | | | (_| | | | | | | | | | | (_| | | | | | | __/\ V V / (_) | | | <
1398 * |_| |_| |_|\__,_|_|_| |_| |_| |_| \__,_|_| |_| |_|\___| \_/\_/ \___/|_| |_|\_\
1400 ***********************************************************************************/
1403 * Enters the emitter functions for handled nodes into the generic
1404 * pointer of an opcode.
1406 static void ia32_register_emitters(void) {
1408 #define IA32_EMIT(a) op_ia32_##a->ops.generic = (op_func)emit_ia32_##a
1409 #define EMIT(a) op_##a->ops.generic = (op_func)emit_##a
1410 #define BE_EMIT(a) op_be_##a->ops.generic = (op_func)emit_be_##a
1412 /* first clear the generic function pointer for all ops */
1413 clear_irp_opcodes_generic_func();
1415 /* register all emitter functions defined in spec */
1416 ia32_register_spec_emitters();
1418 /* other ia32 emitter functions */
1423 IA32_EMIT(SwitchJmp);
1426 IA32_EMIT(Conv_I2FP);
1427 IA32_EMIT(Conv_FP2I);
1428 IA32_EMIT(Conv_FP2FP);
1429 IA32_EMIT(Conv_I2I);
1430 IA32_EMIT(Conv_I2I8Bit);
1432 /* benode emitter */
1449 * Emits code for a node.
1451 static void ia32_emit_node(const ir_node *irn, void *env) {
1452 ia32_emit_env_t *emit_env = env;
1453 firm_dbg_module_t *mod = emit_env->mod;
1454 FILE *F = emit_env->out;
1455 ir_op *op = get_irn_op(irn);
1457 DBG((mod, LEVEL_1, "emitting code for %+F\n", irn));
1459 if (op->ops.generic) {
1460 void (*emit)(const ir_node *, void *) = (void (*)(const ir_node *, void *))op->ops.generic;
1464 ir_fprintf(F, "\t%35s /* %+F (%+G) */\n", " ", irn, irn);
1469 * Walks over the nodes in a block connected by scheduling edges
1470 * and emits code for each node.
1472 static void ia32_gen_block(ir_node *block, void *env) {
1475 if (! is_Block(block))
1478 fprintf(((ia32_emit_env_t *)env)->out, BLOCK_PREFIX("%ld:\n"), get_irn_node_nr(block));
1479 sched_foreach(block, irn) {
1480 ia32_emit_node(irn, env);
1485 * Emits code for function start.
1487 static void ia32_emit_func_prolog(FILE *F, ir_graph *irg) {
1488 entity *irg_ent = get_irg_entity(irg);
1489 const char *irg_name = get_entity_name(irg_ent);
1491 if (get_entity_visibility(irg_ent) == visibility_external_visible) {
1492 fprintf(F, ".globl %s\n", irg_name);
1494 fprintf(F, "\t.type\t%s, @function\n", irg_name);
1495 fprintf(F, "%s:\n", irg_name);
1499 * Emits code for function end
1501 static void ia32_emit_func_epilog(FILE *F, ir_graph *irg) {
1502 const char *irg_name = get_entity_name(get_irg_entity(irg));
1504 fprintf(F, "\tret\n");
1505 fprintf(F, "\t.size\t%s, .-%s\n\n", irg_name, irg_name);
1510 * Sets labels for control flow nodes (jump target)
1511 * TODO: Jump optimization
1513 static void ia32_gen_labels(ir_node *block, void *env) {
1515 int n = get_Block_n_cfgpreds(block);
1517 for (n--; n >= 0; n--) {
1518 pred = get_Block_cfgpred(block, n);
1519 set_irn_link(pred, block);
1524 * Main driver. Emits the code for one routine.
1526 void ia32_gen_routine(FILE *F, ir_graph *irg, const ia32_code_gen_t *cg) {
1527 ia32_emit_env_t emit_env;
1531 emit_env.arch_env = cg->arch_env;
1533 emit_env.isa = (ia32_isa_t *)cg->arch_env->isa;
1534 FIRM_DBG_REGISTER(emit_env.mod, "firm.be.ia32.emitter");
1536 /* set the global arch_env (needed by print hooks) */
1537 arch_env = cg->arch_env;
1539 ia32_register_emitters();
1541 ia32_emit_func_prolog(F, irg);
1542 irg_block_walk_graph(irg, ia32_gen_labels, NULL, &emit_env);
1544 if (cg->opt.extbb && cg->blk_sched) {
1545 int i, n = ARR_LEN(cg->blk_sched);
1547 for (i = 0; i < n;) {
1550 block = cg->blk_sched[i];
1552 next_bl = i < n ? cg->blk_sched[i] : NULL;
1554 /* set here the link. the emitter expects to find the next block here */
1555 set_irn_link(block, next_bl);
1556 ia32_gen_block(block, &emit_env);
1560 /* "normal" block schedule: Note the get_next_block() returns the NUMBER of the block
1561 in the block schedule. As this number should NEVER be equal the next block,
1562 we does not need a clear block link here. */
1563 irg_walk_blkwise_graph(irg, NULL, ia32_gen_block, &emit_env);
1566 ia32_emit_func_epilog(F, irg);