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 *************************************************************/
62 /* We always pass the ir_node which is a pointer. */
63 static int ia32_get_arg_type(const lc_arg_occ_t *occ) {
64 return lc_arg_type_ptr;
69 * Returns the register at in position pos.
71 static const arch_register_t *get_in_reg(const ir_node *irn, int pos) {
73 const arch_register_t *reg = NULL;
75 assert(get_irn_arity(irn) > pos && "Invalid IN position");
77 /* The out register of the operator at position pos is the
78 in register we need. */
79 op = get_irn_n(irn, pos);
81 reg = arch_get_irn_register(arch_env, op);
83 assert(reg && "no in register found");
88 * Returns the register at out position pos.
90 static const arch_register_t *get_out_reg(const ir_node *irn, int pos) {
92 const arch_register_t *reg = NULL;
94 /* 1st case: irn is not of mode_T, so it has only */
95 /* one OUT register -> good */
96 /* 2nd case: irn is of mode_T -> collect all Projs and ask the */
97 /* Proj with the corresponding projnum for the register */
99 if (get_irn_mode(irn) != mode_T) {
100 reg = arch_get_irn_register(arch_env, irn);
102 else if (is_ia32_irn(irn)) {
103 reg = get_ia32_out_reg(irn, pos);
106 const ir_edge_t *edge;
108 foreach_out_edge(irn, edge) {
109 proj = get_edge_src_irn(edge);
110 assert(is_Proj(proj) && "non-Proj from mode_T node");
111 if (get_Proj_proj(proj) == pos) {
112 reg = arch_get_irn_register(arch_env, proj);
118 assert(reg && "no out register found");
128 * Returns the name of the in register at position pos.
130 static const char *get_ia32_reg_name(ir_node *irn, int pos, enum io_direction in_out) {
131 const arch_register_t *reg;
133 static char *buf = NULL;
136 if (in_out == IN_REG) {
137 reg = get_in_reg(irn, pos);
140 /* destination address mode nodes don't have outputs */
141 if (is_ia32_irn(irn) && get_ia32_op_type(irn) == ia32_AddrModeD) {
145 reg = get_out_reg(irn, pos);
148 name = arch_register_get_name(reg);
154 len = strlen(name) + 2;
155 buf = xcalloc(1, len);
157 snprintf(buf, len, "%%%s", name);
163 * Get the register name for a node.
165 static int ia32_get_reg_name(lc_appendable_t *app,
166 const lc_arg_occ_t *occ, const lc_arg_value_t *arg)
169 ir_node *X = arg->v_ptr;
170 int nr = occ->width - 1;
173 return lc_appendable_snadd(app, "(null)", 6);
175 buf = get_ia32_reg_name(X, nr, occ->conversion == 'S' ? IN_REG : OUT_REG);
177 return lc_appendable_snadd(app, buf, strlen(buf));
181 * Get the x87 register name for a node.
183 static int ia32_get_x87_name(lc_appendable_t *app,
184 const lc_arg_occ_t *occ, const lc_arg_value_t *arg)
187 ir_node *X = arg->v_ptr;
188 int nr = occ->width - 1;
192 return lc_appendable_snadd(app, "(null)", 6);
194 attr = get_ia32_attr(X);
195 buf = attr->x87[nr]->name;
196 return lc_appendable_snadd(app, buf, strlen(buf));
200 * Returns the tarval, offset or scale of an ia32 as a string.
202 static int ia32_const_to_str(lc_appendable_t *app,
203 const lc_arg_occ_t *occ, const lc_arg_value_t *arg)
206 ir_node *X = arg->v_ptr;
209 return lc_arg_append(app, occ, "(null)", 6);
211 if (occ->conversion == 'C') {
212 buf = get_ia32_cnst(X);
215 buf = get_ia32_am_offs(X);
218 return buf ? lc_appendable_snadd(app, buf, strlen(buf)) : 0;
222 * Determines the SSE suffix depending on the mode.
224 static int ia32_get_mode_suffix(lc_appendable_t *app,
225 const lc_arg_occ_t *occ, const lc_arg_value_t *arg)
227 ir_node *X = arg->v_ptr;
228 ir_mode *mode = get_irn_mode(X);
230 if (mode == mode_T) {
231 mode = is_ia32_AddrModeS(X) || is_ia32_AddrModeD(X) ? get_ia32_ls_mode(X) : get_ia32_res_mode(X);
235 return lc_arg_append(app, occ, "(null)", 6);
237 if (mode_is_float(mode)) {
238 return lc_appendable_chadd(app, get_mode_size_bits(mode) == 32 ? 's' : 'd');
241 return lc_appendable_chadd(app, mode_is_signed(mode) ? 's' : 'z');
246 * Return the ia32 printf arg environment.
247 * We use the firm environment with some additional handlers.
249 const lc_arg_env_t *ia32_get_arg_env(void) {
250 static lc_arg_env_t *env = NULL;
252 static const lc_arg_handler_t ia32_reg_handler = { ia32_get_arg_type, ia32_get_reg_name };
253 static const lc_arg_handler_t ia32_const_handler = { ia32_get_arg_type, ia32_const_to_str };
254 static const lc_arg_handler_t ia32_mode_handler = { ia32_get_arg_type, ia32_get_mode_suffix };
255 static const lc_arg_handler_t ia32_x87_handler = { ia32_get_arg_type, ia32_get_x87_name };
258 /* extend the firm printer */
259 env = firm_get_arg_env();
261 lc_arg_register(env, "ia32:sreg", 'S', &ia32_reg_handler);
262 lc_arg_register(env, "ia32:dreg", 'D', &ia32_reg_handler);
263 lc_arg_register(env, "ia32:cnst", 'C', &ia32_const_handler);
264 lc_arg_register(env, "ia32:offs", 'O', &ia32_const_handler);
265 lc_arg_register(env, "ia32:mode", 'M', &ia32_mode_handler);
266 lc_arg_register(env, "ia32:x87", 'X', &ia32_x87_handler);
272 static char *ia32_get_reg_name_for_mode(ia32_emit_env_t *env, ir_mode *mode, const arch_register_t *reg) {
273 switch(get_mode_size_bits(mode)) {
275 return ia32_get_mapped_reg_name(env->isa->regs_8bit, reg);
277 return ia32_get_mapped_reg_name(env->isa->regs_16bit, reg);
279 return (char *)arch_register_get_name(reg);
284 * Emits registers and/or address mode of a binary operation.
286 char *ia32_emit_binop(const ir_node *n, ia32_emit_env_t *env) {
287 static char *buf = NULL;
289 /* verify that this function is never called on non-AM supporting operations */
290 //assert(get_ia32_am_support(n) != ia32_am_None && "emit binop expects addressmode support");
292 #define PRODUCES_RESULT(n) \
293 (!(is_ia32_St(n) || \
294 is_ia32_Store8Bit(n) || \
295 is_ia32_CondJmp(n) || \
296 is_ia32_fCondJmp(n) || \
297 is_ia32_SwitchJmp(n)))
300 buf = xcalloc(1, SNPRINTF_BUF_LEN);
303 memset(buf, 0, SNPRINTF_BUF_LEN);
306 switch(get_ia32_op_type(n)) {
308 if (is_ia32_ImmConst(n) || is_ia32_ImmSymConst(n)) {
309 lc_esnprintf(ia32_get_arg_env(), buf, SNPRINTF_BUF_LEN, "%3S, %s", n, get_ia32_cnst(n));
312 const arch_register_t *in1 = get_in_reg(n, 2);
313 const arch_register_t *in2 = get_in_reg(n, 3);
314 const arch_register_t *out = PRODUCES_RESULT(n) ? get_out_reg(n, 0) : NULL;
315 const arch_register_t *in;
318 in = out ? (REGS_ARE_EQUAL(out, in2) ? in1 : in2) : in2;
319 out = out ? out : in1;
320 in_name = arch_register_get_name(in);
322 if (is_ia32_emit_cl(n)) {
323 assert(REGS_ARE_EQUAL(&ia32_gp_regs[REG_ECX], in) && "shift operation needs ecx");
327 snprintf(buf, SNPRINTF_BUF_LEN, "%%%s, %%%s", arch_register_get_name(out), in_name);
331 if (is_ia32_ImmConst(n) || is_ia32_ImmSymConst(n)) {
332 assert(! PRODUCES_RESULT(n) && "Source AM with Const must not produce result");
333 snprintf(buf, SNPRINTF_BUF_LEN, "%s, %s", get_ia32_cnst(n), ia32_emit_am(n, env));
336 lc_esnprintf(ia32_get_arg_env(), buf, SNPRINTF_BUF_LEN, "%4S, %s", n, ia32_emit_am(n, env));
340 if (is_ia32_ImmConst(n) || is_ia32_ImmSymConst(n)) {
341 lc_esnprintf(ia32_get_arg_env(), buf, SNPRINTF_BUF_LEN, "%s,%s%s",
342 ia32_emit_am(n, env),
343 is_ia32_ImmSymConst(n) ? " OFFSET FLAT:" : " ", /* In case of a symconst we must add OFFSET to */
344 get_ia32_cnst(n)); /* tell the assembler to store it's address. */
347 const arch_register_t *in1 = get_in_reg(n, 2);
348 ir_mode *mode = get_ia32_res_mode(n);
351 mode = mode ? mode : get_ia32_ls_mode(n);
352 in_name = ia32_get_reg_name_for_mode(env, mode, in1);
354 if (is_ia32_emit_cl(n)) {
355 assert(REGS_ARE_EQUAL(&ia32_gp_regs[REG_ECX], in1) && "shift operation needs ecx");
359 lc_esnprintf(ia32_get_arg_env(), buf, SNPRINTF_BUF_LEN, "%s, %%%s", ia32_emit_am(n, env), in_name);
363 assert(0 && "unsupported op type");
366 #undef PRODUCES_RESULT
372 * Emits registers and/or address mode of a unary operation.
374 char *ia32_emit_unop(const ir_node *n, ia32_emit_env_t *env) {
375 static char *buf = NULL;
378 buf = xcalloc(1, SNPRINTF_BUF_LEN);
381 memset(buf, 0, SNPRINTF_BUF_LEN);
384 switch(get_ia32_op_type(n)) {
386 if (is_ia32_ImmConst(n) || is_ia32_ImmSymConst(n)) {
387 lc_esnprintf(ia32_get_arg_env(), buf, SNPRINTF_BUF_LEN, "%C", n);
390 lc_esnprintf(ia32_get_arg_env(), buf, SNPRINTF_BUF_LEN, "%1D", n);
394 snprintf(buf, SNPRINTF_BUF_LEN, ia32_emit_am(n, env));
397 assert(0 && "unsupported op type");
404 * Emits address mode.
406 char *ia32_emit_am(const ir_node *n, ia32_emit_env_t *env) {
407 ia32_am_flavour_t am_flav = get_ia32_am_flavour(n);
411 static struct obstack *obst = NULL;
412 ir_mode *mode = get_ia32_ls_mode(n);
414 if (! is_ia32_Lea(n))
415 assert(mode && "AM node must have ls_mode attribute set.");
418 obst = xcalloc(1, sizeof(*obst));
421 obstack_free(obst, NULL);
424 /* obstack_free with NULL results in an uninitialized obstack */
428 switch (get_mode_size_bits(mode)) {
430 obstack_printf(obst, "BYTE PTR ");
433 obstack_printf(obst, "WORD PTR ");
436 obstack_printf(obst, "DWORD PTR ");
443 /* emit address mode symconst */
444 if (get_ia32_am_sc(n)) {
445 if (is_ia32_am_sc_sign(n))
446 obstack_printf(obst, "-");
447 obstack_printf(obst, "%s", get_id_str(get_ia32_am_sc(n)));
450 if (am_flav & ia32_B) {
451 obstack_printf(obst, "[");
452 lc_eoprintf(ia32_get_arg_env(), obst, "%1S", n);
456 if (am_flav & ia32_I) {
458 obstack_printf(obst, "+");
461 obstack_printf(obst, "[");
464 lc_eoprintf(ia32_get_arg_env(), obst, "%2S", n);
466 if (am_flav & ia32_S) {
467 obstack_printf(obst, "*%d", 1 << get_ia32_am_scale(n));
473 if (am_flav & ia32_O) {
474 s = get_ia32_am_offs(n);
477 /* omit explicit + if there was no base or index */
479 obstack_printf(obst, "[");
484 obstack_printf(obst, s);
490 obstack_printf(obst, "] ");
492 size = obstack_object_size(obst);
493 s = obstack_finish(obst);
502 * Formated print of commands and comments.
504 static void ia32_fprintf_format(FILE *F, const ir_node *irn, char *cmd_buf, char *cmnt_buf) {
506 const char *name = irn ? be_retrieve_dbg_info(get_irn_dbg_info((ir_node *)irn), &lineno) : NULL;
509 fprintf(F, "\t%-35s %-60s /* %s:%u */\n", cmd_buf, cmnt_buf, name, lineno);
511 fprintf(F, "\t%-35s %-60s\n", cmd_buf, cmnt_buf);
517 * Add a number to a prefix. This number will not be used a second time.
519 static char *get_unique_label(char *buf, size_t buflen, const char *prefix) {
520 static unsigned long id = 0;
521 snprintf(buf, buflen, "%s%lu", prefix, ++id);
527 /*************************************************
530 * ___ _ __ ___ _| |_ ___ ___ _ __ __| |
531 * / _ \ '_ ` _ \| | __| / __/ _ \| '_ \ / _` |
532 * | __/ | | | | | | |_ | (_| (_) | | | | (_| |
533 * \___|_| |_| |_|_|\__| \___\___/|_| |_|\__,_|
535 *************************************************/
538 #define IA32_DO_EMIT(irn) ia32_fprintf_format(F, irn, cmd_buf, cmnt_buf)
541 * coding of conditions
543 struct cmp2conditon_t {
549 * positive conditions for signed compares
551 static const struct cmp2conditon_t cmp2condition_s[] = {
552 { NULL, pn_Cmp_False }, /* always false */
553 { "e", pn_Cmp_Eq }, /* == */
554 { "l", pn_Cmp_Lt }, /* < */
555 { "le", pn_Cmp_Le }, /* <= */
556 { "g", pn_Cmp_Gt }, /* > */
557 { "ge", pn_Cmp_Ge }, /* >= */
558 { "ne", pn_Cmp_Lg }, /* != */
559 { "ordered", pn_Cmp_Leg }, /* Floating point: ordered */
560 { "unordered", pn_Cmp_Uo }, /* FLoting point: unordered */
561 { "unordered or ==", pn_Cmp_Ue }, /* Floating point: unordered or == */
562 { "unordered or <", pn_Cmp_Ul }, /* Floating point: unordered or < */
563 { "unordered or <=", pn_Cmp_Ule }, /* Floating point: unordered or <= */
564 { "unordered or >", pn_Cmp_Ug }, /* Floating point: unordered or > */
565 { "unordered or >=", pn_Cmp_Uge }, /* Floating point: unordered or >= */
566 { "unordered or !=", pn_Cmp_Ne }, /* Floating point: unordered or != */
567 { NULL, pn_Cmp_True }, /* always true */
571 * positive conditions for unsigned compares
573 static const struct cmp2conditon_t cmp2condition_u[] = {
574 { NULL, pn_Cmp_False }, /* always false */
575 { "e", pn_Cmp_Eq }, /* == */
576 { "b", pn_Cmp_Lt }, /* < */
577 { "be", pn_Cmp_Le }, /* <= */
578 { "a", pn_Cmp_Gt }, /* > */
579 { "ae", pn_Cmp_Ge }, /* >= */
580 { "ne", pn_Cmp_Lg }, /* != */
581 { "ordered", pn_Cmp_Leg }, /* Floating point: ordered */
582 { "unordered", pn_Cmp_Uo }, /* FLoting point: unordered */
583 { "unordered or ==", pn_Cmp_Ue }, /* Floating point: unordered or == */
584 { "unordered or <", pn_Cmp_Ul }, /* Floating point: unordered or < */
585 { "unordered or <=", pn_Cmp_Ule }, /* Floating point: unordered or <= */
586 { "unordered or >", pn_Cmp_Ug }, /* Floating point: unordered or > */
587 { "unordered or >=", pn_Cmp_Uge }, /* Floating point: unordered or >= */
588 { "unordered or !=", pn_Cmp_Ne }, /* Floating point: unordered or != */
589 { NULL, pn_Cmp_True }, /* always true */
593 * returns the condition code
595 static const char *get_cmp_suffix(int cmp_code, int unsigned_cmp)
597 assert(cmp2condition_s[cmp_code].num == cmp_code);
598 assert(cmp2condition_u[cmp_code].num == cmp_code);
600 return unsigned_cmp ? cmp2condition_u[cmp_code & 7].name : cmp2condition_s[cmp_code & 7].name;
604 * Returns the target block for a control flow node.
606 static ir_node *get_cfop_target_block(const ir_node *irn) {
607 return get_irn_link(irn);
611 * Returns the target label for a control flow node.
613 static char *get_cfop_target(const ir_node *irn, char *buf) {
614 ir_node *bl = get_cfop_target_block(irn);
616 snprintf(buf, SNPRINTF_BUF_LEN, BLOCK_PREFIX("%ld"), get_irn_node_nr(bl));
620 /** Return the next block in Block schedule */
621 static ir_node *next_blk_sched(const ir_node *block) {
622 return get_irn_link(block);
626 * Emits the jump sequence for a conditional jump (cmp + jmp_true + jmp_false)
628 static void finish_CondJmp(FILE *F, const ir_node *irn, ir_mode *mode) {
629 const ir_node *proj1, *proj2 = NULL;
630 const ir_node *block, *next_bl = NULL;
631 const ir_edge_t *edge;
632 char buf[SNPRINTF_BUF_LEN];
633 char cmd_buf[SNPRINTF_BUF_LEN];
634 char cmnt_buf[SNPRINTF_BUF_LEN];
636 /* get both Proj's */
637 edge = get_irn_out_edge_first(irn);
638 proj1 = get_edge_src_irn(edge);
639 assert(is_Proj(proj1) && "CondJmp with a non-Proj");
641 edge = get_irn_out_edge_next(irn, edge);
643 proj2 = get_edge_src_irn(edge);
644 assert(is_Proj(proj2) && "CondJmp with a non-Proj");
647 /* for now, the code works for scheduled and non-schedules blocks */
648 block = get_nodes_block(irn);
650 /* we have a block schedule */
651 next_bl = next_blk_sched(block);
653 if (get_cfop_target_block(proj1) == next_bl) {
654 /* exchange both proj's so the second one can be omitted */
655 const ir_node *t = proj1;
661 /* the first Proj must always be created */
662 if (get_Proj_proj(proj1) == pn_Cond_true) {
663 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "j%s %s",
664 get_cmp_suffix(get_ia32_pncode(irn), !mode_is_signed(get_irn_mode(get_irn_n(irn, 0)))),
665 get_cfop_target(proj1, buf));
666 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* cmp(a, b) == TRUE */");
669 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "j%s %s",
670 get_cmp_suffix(get_negated_pnc(get_ia32_pncode(irn), mode),
671 !mode_is_signed(get_irn_mode(get_irn_n(irn, 0)))),
672 get_cfop_target(proj1, buf));
673 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* cmp(a, b) == FALSE */");
677 /* the second Proj might be a fallthrough */
679 if (get_cfop_target_block(proj2) != next_bl) {
680 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "jmp %s", get_cfop_target(proj2, buf));
681 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* otherwise */");
685 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* fallthrogh %s */", get_cfop_target(proj2, buf));
692 * Emits code for conditional jump.
694 static void CondJmp_emitter(const ir_node *irn, ia32_emit_env_t *env) {
696 char cmd_buf[SNPRINTF_BUF_LEN];
697 char cmnt_buf[SNPRINTF_BUF_LEN];
699 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "cmp %s", ia32_emit_binop(irn, env));
700 lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* %+F */", irn);
702 finish_CondJmp(F, irn, get_ia32_res_mode(irn));
706 * Emits code for conditional jump with two variables.
708 static void emit_ia32_CondJmp(const ir_node *irn, ia32_emit_env_t *env) {
709 CondJmp_emitter(irn, env);
713 * Emits code for conditional jump with immediate.
715 static void emit_ia32_CondJmp_i(const ir_node *irn, ia32_emit_env_t *env) {
716 CondJmp_emitter(irn, env);
720 * Emits code for conditional test and jump.
722 static void TestJmp_emitter(const ir_node *irn, ia32_emit_env_t *env) {
724 #define IA32_IS_IMMOP (is_ia32_ImmConst(irn) || is_ia32_ImmSymConst(irn))
727 const char *op1 = arch_register_get_name(get_in_reg(irn, 0));
728 const char *op2 = IA32_IS_IMMOP ? get_ia32_cnst(irn) : NULL;
729 char cmd_buf[SNPRINTF_BUF_LEN];
730 char cmnt_buf[SNPRINTF_BUF_LEN];
733 op2 = arch_register_get_name(get_in_reg(irn, 1));
735 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "test %%%s,%s%s ", op1, IA32_IS_IMMOP ? " " : " %", op2);
736 lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* %+F */", irn);
739 finish_CondJmp(F, irn, get_ia32_res_mode(irn));
745 * Emits code for conditional test and jump with two variables.
747 static void emit_ia32_TestJmp(const ir_node *irn, ia32_emit_env_t *env) {
748 TestJmp_emitter(irn, env);
751 static void emit_ia32_CJmp(const ir_node *irn, ia32_emit_env_t *env) {
753 char cmd_buf[SNPRINTF_BUF_LEN];
754 char cmnt_buf[SNPRINTF_BUF_LEN];
756 snprintf(cmd_buf, SNPRINTF_BUF_LEN, " ");
757 lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* %+F omitted redundant test */", irn);
759 finish_CondJmp(F, irn, get_ia32_res_mode(irn));
762 static void emit_ia32_CJmpAM(const ir_node *irn, ia32_emit_env_t *env) {
764 char cmd_buf[SNPRINTF_BUF_LEN];
765 char cmnt_buf[SNPRINTF_BUF_LEN];
767 snprintf(cmd_buf, SNPRINTF_BUF_LEN, " ");
768 lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* %+F omitted redundant test/cmp */", irn);
770 finish_CondJmp(F, irn, get_ia32_res_mode(irn));
773 /*********************************************************
776 * ___ _ __ ___ _| |_ _ _ _ _ __ ___ _ __ ___
777 * / _ \ '_ ` _ \| | __| | | | | | '_ ` _ \| '_ \/ __|
778 * | __/ | | | | | | |_ | | |_| | | | | | | |_) \__ \
779 * \___|_| |_| |_|_|\__| | |\__,_|_| |_| |_| .__/|___/
782 *********************************************************/
784 /* jump table entry (target and corresponding number) */
785 typedef struct _branch_t {
790 /* jump table for switch generation */
791 typedef struct _jmp_tbl_t {
792 ir_node *defProj; /**< default target */
793 int min_value; /**< smallest switch case */
794 int max_value; /**< largest switch case */
795 int num_branches; /**< number of jumps */
796 char *label; /**< label of the jump table */
797 branch_t *branches; /**< jump array */
801 * Compare two variables of type branch_t. Used to sort all switch cases
803 static int ia32_cmp_branch_t(const void *a, const void *b) {
804 branch_t *b1 = (branch_t *)a;
805 branch_t *b2 = (branch_t *)b;
807 if (b1->value <= b2->value)
814 * Emits code for a SwitchJmp (creates a jump table if
815 * possible otherwise a cmp-jmp cascade). Port from
818 static void emit_ia32_SwitchJmp(const ir_node *irn, ia32_emit_env_t *emit_env) {
819 unsigned long interval;
820 char buf[SNPRINTF_BUF_LEN];
821 int last_value, i, pn, do_jmp_tbl = 1;
824 const ir_edge_t *edge;
825 const lc_arg_env_t *env = ia32_get_arg_env();
826 FILE *F = emit_env->out;
827 char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
829 /* fill the table structure */
830 tbl.label = xmalloc(SNPRINTF_BUF_LEN);
831 tbl.label = get_unique_label(tbl.label, SNPRINTF_BUF_LEN, "JMPTBL_");
833 tbl.num_branches = get_irn_n_edges(irn);
834 tbl.branches = xcalloc(tbl.num_branches, sizeof(tbl.branches[0]));
835 tbl.min_value = INT_MAX;
836 tbl.max_value = INT_MIN;
839 /* go over all proj's and collect them */
840 foreach_out_edge(irn, edge) {
841 proj = get_edge_src_irn(edge);
842 assert(is_Proj(proj) && "Only proj allowed at SwitchJmp");
844 pn = get_Proj_proj(proj);
846 /* create branch entry */
847 tbl.branches[i].target = proj;
848 tbl.branches[i].value = pn;
850 tbl.min_value = pn < tbl.min_value ? pn : tbl.min_value;
851 tbl.max_value = pn > tbl.max_value ? pn : tbl.max_value;
853 /* check for default proj */
854 if (pn == get_ia32_pncode(irn)) {
855 assert(tbl.defProj == NULL && "found two defProjs at SwitchJmp");
862 /* sort the branches by their number */
863 qsort(tbl.branches, tbl.num_branches, sizeof(tbl.branches[0]), ia32_cmp_branch_t);
865 /* two-complement's magic make this work without overflow */
866 interval = tbl.max_value - tbl.min_value;
868 /* check value interval */
869 if (interval > 16 * 1024) {
873 /* check ratio of value interval to number of branches */
874 if ((float)(interval + 1) / (float)tbl.num_branches > 8.0) {
880 if (tbl.min_value != 0) {
881 lc_esnprintf(env, cmd_buf, SNPRINTF_BUF_LEN, "cmpl %lu, -%d(%1S)",
882 interval, tbl.min_value, irn);
883 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* first switch value is not 0 */");
888 lc_esnprintf(env, cmd_buf, SNPRINTF_BUF_LEN, "cmpl %lu, %1S", interval, irn);
889 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* compare for switch */");
894 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "ja %s", get_cfop_target(tbl.defProj, buf));
895 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* default jump if out of range */");
898 if (tbl.num_branches > 1) {
901 lc_esnprintf(env, cmd_buf, SNPRINTF_BUF_LEN, "jmp [%1S*4+%s]", irn, tbl.label);
902 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* get jump table entry as target */");
905 fprintf(F, "\t.section\t.rodata\n");
906 fprintf(F, "\t.align 4\n");
908 fprintf(F, "%s:\n", tbl.label);
910 snprintf(cmd_buf, SNPRINTF_BUF_LEN, ".long %s", get_cfop_target(tbl.branches[0].target, buf));
911 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* case %d */\n", tbl.branches[0].value);
914 last_value = tbl.branches[0].value;
915 for (i = 1; i < tbl.num_branches; ++i) {
916 while (++last_value < tbl.branches[i].value) {
917 snprintf(cmd_buf, SNPRINTF_BUF_LEN, ".long %s", get_cfop_target(tbl.defProj, buf));
918 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* default case */");
921 snprintf(cmd_buf, SNPRINTF_BUF_LEN, ".long %s", get_cfop_target(tbl.branches[i].target, buf));
922 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* case %d */", last_value);
926 fprintf(F, "\t.text");
929 /* one jump is enough */
930 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "jmp %s", get_cfop_target(tbl.branches[0].target, buf));
931 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* only one case given */");
935 else { // no jump table
936 for (i = 0; i < tbl.num_branches; ++i) {
937 lc_esnprintf(env, cmd_buf, SNPRINTF_BUF_LEN, "cmpl %d, %1S", tbl.branches[i].value, irn);
938 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* case %d */", i);
940 fprintf(F, "\tje %s\n", get_cfop_target(tbl.branches[i].target, buf));
943 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "jmp %s", get_cfop_target(tbl.defProj, buf));
944 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* default case */");
955 * Emits code for a unconditional jump.
957 static void emit_Jmp(const ir_node *irn, ia32_emit_env_t *env) {
958 ir_node *block, *next_bl;
960 char buf[SNPRINTF_BUF_LEN], cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
962 /* for now, the code works for scheduled and non-schedules blocks */
963 block = get_nodes_block(irn);
965 /* we have a block schedule */
966 next_bl = next_blk_sched(block);
967 if (get_cfop_target_block(irn) != next_bl) {
968 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "jmp %s", get_cfop_target(irn, buf));
969 lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* %+F(%+F) */", irn, get_cfop_target_block(irn));
973 lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* fallthrough %s */", get_cfop_target(irn, buf));
978 /****************************
981 * _ __ _ __ ___ _ ___
982 * | '_ \| '__/ _ \| |/ __|
983 * | |_) | | | (_) | |\__ \
984 * | .__/|_| \___/| ||___/
987 ****************************/
990 * Emits code for a proj -> node
992 static void emit_Proj(const ir_node *irn, ia32_emit_env_t *env) {
993 ir_node *pred = get_Proj_pred(irn);
995 if (get_irn_op(pred) == op_Start) {
996 switch(get_Proj_proj(irn)) {
997 case pn_Start_X_initial_exec:
1006 /**********************************
1009 * | | ___ _ __ _ _| |_) |
1010 * | | / _ \| '_ \| | | | _ <
1011 * | |___| (_) | |_) | |_| | |_) |
1012 * \_____\___/| .__/ \__, |____/
1015 **********************************/
1018 * Emit movsb/w instructions to make mov count divideable by 4
1020 static void emit_CopyB_prolog(FILE *F, int rem, int size) {
1021 char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
1023 fprintf(F, "\t/* memcopy %d bytes*/\n", size);
1025 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "cld");
1026 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* copy direction forward*/");
1031 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "movsb");
1032 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* memcopy remainder 1 */");
1035 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "movsw");
1036 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* memcopy remainder 2 */");
1039 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "movsb");
1040 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* memcopy remainder 3 */");
1042 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "movsw");
1043 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* memcopy remainder 3 */");
1051 * Emit rep movsd instruction for memcopy.
1053 static void emit_ia32_CopyB(const ir_node *irn, ia32_emit_env_t *emit_env) {
1054 FILE *F = emit_env->out;
1055 tarval *tv = get_ia32_Immop_tarval(irn);
1056 int rem = get_tarval_long(tv);
1057 int size = get_tarval_long(get_ia32_Immop_tarval(get_irn_n(irn, 2)));
1058 char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
1060 emit_CopyB_prolog(F, rem, size);
1062 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "rep movsd");
1063 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* memcopy */");
1068 * Emits unrolled memcopy.
1070 static void emit_ia32_CopyB_i(const ir_node *irn, ia32_emit_env_t *emit_env) {
1071 tarval *tv = get_ia32_Immop_tarval(irn);
1072 int size = get_tarval_long(tv);
1073 FILE *F = emit_env->out;
1074 char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
1076 emit_CopyB_prolog(F, size & 0x3, size);
1080 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "movsd");
1081 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* memcopy unrolled */");
1088 /***************************
1092 * | | / _ \| '_ \ \ / /
1093 * | |___| (_) | | | \ V /
1094 * \_____\___/|_| |_|\_/
1096 ***************************/
1099 * Emit code for conversions (I, FP), (FP, I) and (FP, FP).
1101 static void emit_ia32_Conv_with_FP(const ir_node *irn, ia32_emit_env_t *emit_env) {
1102 FILE *F = emit_env->out;
1103 const lc_arg_env_t *env = ia32_get_arg_env();
1104 char *from, *to, buf[64];
1105 ir_mode *src_mode, *tgt_mode;
1106 char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
1108 src_mode = is_ia32_AddrModeS(irn) ? get_ia32_ls_mode(irn) : get_irn_mode(get_irn_n(irn, 2));
1109 tgt_mode = get_ia32_res_mode(irn);
1111 from = mode_is_float(src_mode) ? (get_mode_size_bits(src_mode) == 32 ? "ss" : "sd") : "si";
1112 to = mode_is_float(tgt_mode) ? (get_mode_size_bits(tgt_mode) == 32 ? "ss" : "sd") : "si";
1114 switch(get_ia32_op_type(irn)) {
1116 lc_esnprintf(env, buf, sizeof(buf), "%1D, %3S", irn, irn);
1118 case ia32_AddrModeS:
1119 lc_esnprintf(env, buf, sizeof(buf), "%1D, %s", irn, ia32_emit_am(irn, emit_env));
1122 assert(0 && "unsupported op type for Conv");
1125 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "cvt%s2%s %s", from, to, buf);
1126 lc_esnprintf(env, cmnt_buf, SNPRINTF_BUF_LEN, "/* %+F(%+F, %+F) */", irn, src_mode, tgt_mode);
1130 static void emit_ia32_Conv_I2FP(const ir_node *irn, ia32_emit_env_t *emit_env) {
1131 emit_ia32_Conv_with_FP(irn, emit_env);
1134 static void emit_ia32_Conv_FP2I(const ir_node *irn, ia32_emit_env_t *emit_env) {
1135 emit_ia32_Conv_with_FP(irn, emit_env);
1138 static void emit_ia32_Conv_FP2FP(const ir_node *irn, ia32_emit_env_t *emit_env) {
1139 emit_ia32_Conv_with_FP(irn, emit_env);
1143 * Emits code for an Int conversion.
1145 static void emit_ia32_Conv_I2I(const ir_node *irn, ia32_emit_env_t *emit_env) {
1146 FILE *F = emit_env->out;
1147 const lc_arg_env_t *env = ia32_get_arg_env();
1148 char *move_cmd, *conv_cmd;
1149 ir_mode *src_mode, *tgt_mode;
1151 char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
1152 const arch_register_t *in_reg, *out_reg;
1154 src_mode = is_ia32_AddrModeS(irn) ? get_ia32_ls_mode(irn) : get_irn_mode(get_irn_n(irn, 2));
1155 tgt_mode = get_ia32_res_mode(irn);
1157 n = get_mode_size_bits(src_mode);
1158 m = get_mode_size_bits(tgt_mode);
1160 if (mode_is_signed(n < m ? src_mode : tgt_mode)) {
1162 if (n == 8 || m == 8)
1164 else if (n == 16 || m == 16)
1167 assert(0 && "unsupported Conv_I2I");
1174 switch(get_ia32_op_type(irn)) {
1176 in_reg = get_in_reg(irn, 2);
1177 out_reg = get_out_reg(irn, 0);
1179 if (REGS_ARE_EQUAL(in_reg, &ia32_gp_regs[REG_EAX]) &&
1180 REGS_ARE_EQUAL(out_reg, in_reg) &&
1181 mode_is_signed(n < m ? src_mode : tgt_mode))
1183 /* argument and result are both in EAX and */
1184 /* signedness is ok: -> use converts */
1185 lc_esnprintf(env, cmd_buf, SNPRINTF_BUF_LEN, "%s", conv_cmd);
1187 else if (REGS_ARE_EQUAL(out_reg, in_reg) &&
1188 ! mode_is_signed(n < m ? src_mode : tgt_mode))
1190 /* argument and result are in the same register */
1191 /* and signedness is ok: -> use and with mask */
1192 int mask = (1 << (n < m ? n : m)) - 1;
1193 lc_esnprintf(env, cmd_buf, SNPRINTF_BUF_LEN, "and %1D, 0x%x", irn, mask);
1196 /* use move w/o sign extension */
1197 lc_esnprintf(env, cmd_buf, SNPRINTF_BUF_LEN, "%s %1D, %%%s",
1198 move_cmd, irn, ia32_get_reg_name_for_mode(emit_env, n < m ? src_mode : tgt_mode, in_reg));
1202 case ia32_AddrModeS:
1203 lc_esnprintf(env, cmd_buf, SNPRINTF_BUF_LEN, "%s %1D, %s",
1204 move_cmd, irn, ia32_emit_am(irn, emit_env));
1207 assert(0 && "unsupported op type for Conv");
1210 lc_esnprintf(env, cmnt_buf, SNPRINTF_BUF_LEN, "/* %+F(%d Bit mode_%F -> %d Bit mode_%F) */",
1211 irn, n, src_mode, m, tgt_mode);
1217 * Emits code for an 8Bit Int conversion.
1219 void emit_ia32_Conv_I2I8Bit(const ir_node *irn, ia32_emit_env_t *emit_env) {
1220 emit_ia32_Conv_I2I(irn, emit_env);
1224 /*******************************************
1227 * | |__ ___ _ __ ___ __| | ___ ___
1228 * | '_ \ / _ \ '_ \ / _ \ / _` |/ _ \/ __|
1229 * | |_) | __/ | | | (_) | (_| | __/\__ \
1230 * |_.__/ \___|_| |_|\___/ \__,_|\___||___/
1232 *******************************************/
1235 * Emits a backend call
1237 static void emit_be_Call(const ir_node *irn, ia32_emit_env_t *emit_env) {
1238 FILE *F = emit_env->out;
1239 entity *ent = be_Call_get_entity(irn);
1240 char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
1243 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "call %s", get_entity_name(ent));
1246 lc_esnprintf(ia32_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "%1D", get_irn_n(irn, be_pos_Call_ptr));
1249 lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* %+F (be_Call) */", irn);
1255 * Emits code to increase stack pointer.
1257 static void emit_be_IncSP(const ir_node *irn, ia32_emit_env_t *emit_env) {
1258 FILE *F = emit_env->out;
1259 unsigned offs = be_get_IncSP_offset(irn);
1260 be_stack_dir_t dir = be_get_IncSP_direction(irn);
1261 char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
1264 lc_esnprintf(ia32_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "add %1S,%s%u", irn,
1265 (dir == be_stack_dir_expand) ? " -" : " ", offs);
1266 lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* %+F (IncSP) */", irn);
1269 snprintf(cmd_buf, SNPRINTF_BUF_LEN, " ");
1270 lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* omitted %+F (IncSP) with 0 */", irn);
1277 * Emits code to set stack pointer.
1279 static void emit_be_SetSP(const ir_node *irn, ia32_emit_env_t *emit_env) {
1280 FILE *F = emit_env->out;
1281 char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
1283 lc_esnprintf(ia32_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "mov %1D, %3S", irn, irn);
1284 lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* %+F (restore SP) */", irn);
1289 * Emits code for Copy.
1291 static void emit_be_Copy(const ir_node *irn, ia32_emit_env_t *emit_env) {
1292 FILE *F = emit_env->out;
1293 char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
1295 lc_esnprintf(ia32_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "mov %1D, %1S", irn, irn);
1296 lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* %+F */", irn);
1301 * Emits code for exchange.
1303 static void emit_be_Perm(const ir_node *irn, ia32_emit_env_t *emit_env) {
1304 FILE *F = emit_env->out;
1305 char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
1307 lc_esnprintf(ia32_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "xchg %1S, %2S", irn, irn);
1308 lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* %+F(%1A, %2A) */", irn, irn, irn);
1314 /***********************************************************************************
1317 * _ __ ___ __ _ _ _ __ | |_ _ __ __ _ _ __ ___ _____ _____ _ __| | __
1318 * | '_ ` _ \ / _` | | '_ \ | _| '__/ _` | '_ ` _ \ / _ \ \ /\ / / _ \| '__| |/ /
1319 * | | | | | | (_| | | | | | | | | | | (_| | | | | | | __/\ V V / (_) | | | <
1320 * |_| |_| |_|\__,_|_|_| |_| |_| |_| \__,_|_| |_| |_|\___| \_/\_/ \___/|_| |_|\_\
1322 ***********************************************************************************/
1325 * Enters the emitter functions for handled nodes into the generic
1326 * pointer of an opcode.
1328 static void ia32_register_emitters(void) {
1330 #define IA32_EMIT(a) op_ia32_##a->ops.generic = (op_func)emit_ia32_##a
1331 #define EMIT(a) op_##a->ops.generic = (op_func)emit_##a
1332 #define BE_EMIT(a) op_be_##a->ops.generic = (op_func)emit_be_##a
1334 /* first clear the generic function pointer for all ops */
1335 clear_irp_opcodes_generic_func();
1337 /* register all emitter functions defined in spec */
1338 ia32_register_spec_emitters();
1340 /* other ia32 emitter functions */
1345 IA32_EMIT(SwitchJmp);
1348 IA32_EMIT(Conv_I2FP);
1349 IA32_EMIT(Conv_FP2I);
1350 IA32_EMIT(Conv_FP2FP);
1351 IA32_EMIT(Conv_I2I);
1352 IA32_EMIT(Conv_I2I8Bit);
1354 /* benode emitter */
1371 * Emits code for a node.
1373 static void ia32_emit_node(const ir_node *irn, void *env) {
1374 ia32_emit_env_t *emit_env = env;
1375 firm_dbg_module_t *mod = emit_env->mod;
1376 FILE *F = emit_env->out;
1377 ir_op *op = get_irn_op(irn);
1379 DBG((mod, LEVEL_1, "emitting code for %+F\n", irn));
1381 if (op->ops.generic) {
1382 void (*emit)(const ir_node *, void *) = (void (*)(const ir_node *, void *))op->ops.generic;
1386 ir_fprintf(F, "\t%35s /* %+F (%+G) */\n", " ", irn, irn);
1391 * Walks over the nodes in a block connected by scheduling edges
1392 * and emits code for each node.
1394 static void ia32_gen_block(ir_node *block, void *env) {
1397 if (! is_Block(block))
1400 fprintf(((ia32_emit_env_t *)env)->out, BLOCK_PREFIX("%ld:\n"), get_irn_node_nr(block));
1401 sched_foreach(block, irn) {
1402 ia32_emit_node(irn, env);
1407 * Emits code for function start.
1409 static void ia32_emit_func_prolog(FILE *F, ir_graph *irg) {
1410 entity *irg_ent = get_irg_entity(irg);
1411 const char *irg_name = get_entity_name(irg_ent);
1413 if (get_entity_visibility(irg_ent) == visibility_external_visible) {
1414 fprintf(F, ".globl %s\n", irg_name);
1416 fprintf(F, "\t.type\t%s, @function\n", irg_name);
1417 fprintf(F, "%s:\n", irg_name);
1421 * Emits code for function end
1423 static void ia32_emit_func_epilog(FILE *F, ir_graph *irg) {
1424 const char *irg_name = get_entity_name(get_irg_entity(irg));
1426 fprintf(F, "\tret\n");
1427 fprintf(F, "\t.size\t%s, .-%s\n\n", irg_name, irg_name);
1432 * Sets labels for control flow nodes (jump target)
1433 * TODO: Jump optimization
1435 static void ia32_gen_labels(ir_node *block, void *env) {
1437 int n = get_Block_n_cfgpreds(block);
1439 for (n--; n >= 0; n--) {
1440 pred = get_Block_cfgpred(block, n);
1441 set_irn_link(pred, block);
1446 * Main driver. Emits the code for one routine.
1448 void ia32_gen_routine(FILE *F, ir_graph *irg, const ia32_code_gen_t *cg) {
1449 ia32_emit_env_t emit_env;
1453 emit_env.arch_env = cg->arch_env;
1455 emit_env.isa = (ia32_isa_t *)cg->arch_env->isa;
1456 FIRM_DBG_REGISTER(emit_env.mod, "firm.be.ia32.emitter");
1458 /* set the global arch_env (needed by print hooks) */
1459 arch_env = cg->arch_env;
1461 ia32_register_emitters();
1463 ia32_emit_func_prolog(F, irg);
1464 irg_block_walk_graph(irg, ia32_gen_labels, NULL, &emit_env);
1466 if (cg->opt.extbb && cg->blk_sched) {
1467 int i, n = ARR_LEN(cg->blk_sched);
1469 for (i = 0; i < n;) {
1472 block = cg->blk_sched[i];
1474 next_bl = i < n ? cg->blk_sched[i] : NULL;
1476 /* set here the link. the emitter expects to find the next block here */
1477 set_irn_link(block, next_bl);
1478 ia32_gen_block(block, &emit_env);
1482 /* "normal" block schedule: Note the get_next_block() returns the NUMBER of the block
1483 in the block schedule. As this number should NEVER be equal the next block,
1484 we does not need a clear block link here. */
1485 irg_walk_blkwise_graph(irg, NULL, ia32_gen_block, &emit_env);
1488 ia32_emit_func_epilog(F, irg);