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 obstack_printf(obst, "[");
445 if (am_flav & ia32_B) {
446 lc_eoprintf(ia32_get_arg_env(), obst, "%1S", n);
450 if (am_flav & ia32_I) {
452 obstack_printf(obst, "+");
455 lc_eoprintf(ia32_get_arg_env(), obst, "%2S", n);
457 if (am_flav & ia32_S) {
458 obstack_printf(obst, "*%d", 1 << get_ia32_am_scale(n));
464 if (am_flav & ia32_O) {
465 s = get_ia32_am_offs(n);
467 /* omit explicit + if there was no base or index */
468 if (! had_output && s[0] == '+')
471 obstack_printf(obst, s);
474 obstack_printf(obst, "] ");
476 size = obstack_object_size(obst);
477 s = obstack_finish(obst);
486 * Formated print of commands and comments.
488 static void ia32_fprintf_format(FILE *F, const ir_node *irn, char *cmd_buf, char *cmnt_buf) {
490 const char *name = irn ? be_retrieve_dbg_info(get_irn_dbg_info((ir_node *)irn), &lineno) : NULL;
493 fprintf(F, "\t%-35s %-60s /* %s:%u */\n", cmd_buf, cmnt_buf, name, lineno);
495 fprintf(F, "\t%-35s %-60s\n", cmd_buf, cmnt_buf);
501 * Add a number to a prefix. This number will not be used a second time.
503 static char *get_unique_label(char *buf, size_t buflen, const char *prefix) {
504 static unsigned long id = 0;
505 snprintf(buf, buflen, "%s%lu", prefix, ++id);
511 /*************************************************
514 * ___ _ __ ___ _| |_ ___ ___ _ __ __| |
515 * / _ \ '_ ` _ \| | __| / __/ _ \| '_ \ / _` |
516 * | __/ | | | | | | |_ | (_| (_) | | | | (_| |
517 * \___|_| |_| |_|_|\__| \___\___/|_| |_|\__,_|
519 *************************************************/
522 #define IA32_DO_EMIT(irn) ia32_fprintf_format(F, irn, cmd_buf, cmnt_buf)
525 * coding of conditions
527 struct cmp2conditon_t {
533 * positive conditions for signed compares
535 static const struct cmp2conditon_t cmp2condition_s[] = {
536 { NULL, pn_Cmp_False }, /* always false */
537 { "e", pn_Cmp_Eq }, /* == */
538 { "l", pn_Cmp_Lt }, /* < */
539 { "le", pn_Cmp_Le }, /* <= */
540 { "g", pn_Cmp_Gt }, /* > */
541 { "ge", pn_Cmp_Ge }, /* >= */
542 { "ne", pn_Cmp_Lg }, /* != */
543 { "ordered", pn_Cmp_Leg }, /* Floating point: ordered */
544 { "unordered", pn_Cmp_Uo }, /* FLoting point: unordered */
545 { "unordered or ==", pn_Cmp_Ue }, /* Floating point: unordered or == */
546 { "unordered or <", pn_Cmp_Ul }, /* Floating point: unordered or < */
547 { "unordered or <=", pn_Cmp_Ule }, /* Floating point: unordered or <= */
548 { "unordered or >", pn_Cmp_Ug }, /* Floating point: unordered or > */
549 { "unordered or >=", pn_Cmp_Uge }, /* Floating point: unordered or >= */
550 { "unordered or !=", pn_Cmp_Ne }, /* Floating point: unordered or != */
551 { NULL, pn_Cmp_True }, /* always true */
555 * positive conditions for unsigned compares
557 static const struct cmp2conditon_t cmp2condition_u[] = {
558 { NULL, pn_Cmp_False }, /* always false */
559 { "e", pn_Cmp_Eq }, /* == */
560 { "b", pn_Cmp_Lt }, /* < */
561 { "be", pn_Cmp_Le }, /* <= */
562 { "a", pn_Cmp_Gt }, /* > */
563 { "ae", pn_Cmp_Ge }, /* >= */
564 { "ne", pn_Cmp_Lg }, /* != */
565 { "ordered", pn_Cmp_Leg }, /* Floating point: ordered */
566 { "unordered", pn_Cmp_Uo }, /* FLoting point: unordered */
567 { "unordered or ==", pn_Cmp_Ue }, /* Floating point: unordered or == */
568 { "unordered or <", pn_Cmp_Ul }, /* Floating point: unordered or < */
569 { "unordered or <=", pn_Cmp_Ule }, /* Floating point: unordered or <= */
570 { "unordered or >", pn_Cmp_Ug }, /* Floating point: unordered or > */
571 { "unordered or >=", pn_Cmp_Uge }, /* Floating point: unordered or >= */
572 { "unordered or !=", pn_Cmp_Ne }, /* Floating point: unordered or != */
573 { NULL, pn_Cmp_True }, /* always true */
577 * returns the condition code
579 static const char *get_cmp_suffix(int cmp_code, int unsigned_cmp)
581 assert(cmp2condition_s[cmp_code].num == cmp_code);
582 assert(cmp2condition_u[cmp_code].num == cmp_code);
584 return unsigned_cmp ? cmp2condition_u[cmp_code & 7].name : cmp2condition_s[cmp_code & 7].name;
588 * Returns the target block for a control flow node.
590 static ir_node *get_cfop_target_block(const ir_node *irn) {
591 return get_irn_link(irn);
595 * Returns the target label for a control flow node.
597 static char *get_cfop_target(const ir_node *irn, char *buf) {
598 ir_node *bl = get_cfop_target_block(irn);
600 snprintf(buf, SNPRINTF_BUF_LEN, BLOCK_PREFIX("%ld"), get_irn_node_nr(bl));
604 /** Return the next block in Block schedule */
605 static ir_node *next_blk_sched(const ir_node *block) {
606 return get_irn_link(block);
610 * Emits the jump sequence for a conditional jump (cmp + jmp_true + jmp_false)
612 static void finish_CondJmp(FILE *F, const ir_node *irn, ir_mode *mode) {
613 const ir_node *proj1, *proj2 = NULL;
614 const ir_node *block, *next_bl = NULL;
615 const ir_edge_t *edge;
616 char buf[SNPRINTF_BUF_LEN];
617 char cmd_buf[SNPRINTF_BUF_LEN];
618 char cmnt_buf[SNPRINTF_BUF_LEN];
620 /* get both Proj's */
621 edge = get_irn_out_edge_first(irn);
622 proj1 = get_edge_src_irn(edge);
623 assert(is_Proj(proj1) && "CondJmp with a non-Proj");
625 edge = get_irn_out_edge_next(irn, edge);
627 proj2 = get_edge_src_irn(edge);
628 assert(is_Proj(proj2) && "CondJmp with a non-Proj");
631 /* for now, the code works for scheduled and non-schedules blocks */
632 block = get_nodes_block(irn);
634 /* we have a block schedule */
635 next_bl = next_blk_sched(block);
637 if (get_cfop_target_block(proj1) == next_bl) {
638 /* exchange both proj's so the second one can be omitted */
639 const ir_node *t = proj1;
645 /* the first Proj must always be created */
646 if (get_Proj_proj(proj1) == pn_Cond_true) {
647 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "j%s %s",
648 get_cmp_suffix(get_ia32_pncode(irn), !mode_is_signed(get_irn_mode(get_irn_n(irn, 0)))),
649 get_cfop_target(proj1, buf));
650 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* cmp(a, b) == TRUE */");
653 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "j%s %s",
654 get_cmp_suffix(get_negated_pnc(get_ia32_pncode(irn), mode),
655 !mode_is_signed(get_irn_mode(get_irn_n(irn, 0)))),
656 get_cfop_target(proj1, buf));
657 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* cmp(a, b) == FALSE */");
661 /* the second Proj might be a fallthrough */
663 if (get_cfop_target_block(proj2) != next_bl) {
664 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "jmp %s", get_cfop_target(proj2, buf));
665 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* otherwise */");
669 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* fallthrogh %s */", get_cfop_target(proj2, buf));
676 * Emits code for conditional jump.
678 static void CondJmp_emitter(const ir_node *irn, ia32_emit_env_t *env) {
680 char cmd_buf[SNPRINTF_BUF_LEN];
681 char cmnt_buf[SNPRINTF_BUF_LEN];
683 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "cmp %s", ia32_emit_binop(irn, env));
684 lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* %+F */", irn);
686 finish_CondJmp(F, irn, get_ia32_res_mode(irn));
690 * Emits code for conditional jump with two variables.
692 static void emit_ia32_CondJmp(const ir_node *irn, ia32_emit_env_t *env) {
693 CondJmp_emitter(irn, env);
697 * Emits code for conditional jump with immediate.
699 static void emit_ia32_CondJmp_i(const ir_node *irn, ia32_emit_env_t *env) {
700 CondJmp_emitter(irn, env);
704 * Emits code for conditional test and jump.
706 static void TestJmp_emitter(const ir_node *irn, ia32_emit_env_t *env) {
708 #define IA32_IS_IMMOP (is_ia32_ImmConst(irn) || is_ia32_ImmSymConst(irn))
711 const char *op1 = arch_register_get_name(get_in_reg(irn, 0));
712 const char *op2 = IA32_IS_IMMOP ? get_ia32_cnst(irn) : NULL;
713 char cmd_buf[SNPRINTF_BUF_LEN];
714 char cmnt_buf[SNPRINTF_BUF_LEN];
717 op2 = arch_register_get_name(get_in_reg(irn, 1));
719 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "test %%%s,%s%s ", op1, IA32_IS_IMMOP ? " " : " %", op2);
720 lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* %+F */", irn);
723 finish_CondJmp(F, irn, get_ia32_res_mode(irn));
729 * Emits code for conditional test and jump with two variables.
731 static void emit_ia32_TestJmp(const ir_node *irn, ia32_emit_env_t *env) {
732 TestJmp_emitter(irn, env);
735 static void emit_ia32_CJmp(const ir_node *irn, ia32_emit_env_t *env) {
737 char cmd_buf[SNPRINTF_BUF_LEN];
738 char cmnt_buf[SNPRINTF_BUF_LEN];
740 snprintf(cmd_buf, SNPRINTF_BUF_LEN, " ");
741 lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* %+F omitted redundant test */", irn);
743 finish_CondJmp(F, irn, get_ia32_res_mode(irn));
746 static void emit_ia32_CJmpAM(const ir_node *irn, ia32_emit_env_t *env) {
748 char cmd_buf[SNPRINTF_BUF_LEN];
749 char cmnt_buf[SNPRINTF_BUF_LEN];
751 snprintf(cmd_buf, SNPRINTF_BUF_LEN, " ");
752 lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* %+F omitted redundant test/cmp */", irn);
754 finish_CondJmp(F, irn, get_ia32_res_mode(irn));
757 /*********************************************************
760 * ___ _ __ ___ _| |_ _ _ _ _ __ ___ _ __ ___
761 * / _ \ '_ ` _ \| | __| | | | | | '_ ` _ \| '_ \/ __|
762 * | __/ | | | | | | |_ | | |_| | | | | | | |_) \__ \
763 * \___|_| |_| |_|_|\__| | |\__,_|_| |_| |_| .__/|___/
766 *********************************************************/
768 /* jump table entry (target and corresponding number) */
769 typedef struct _branch_t {
774 /* jump table for switch generation */
775 typedef struct _jmp_tbl_t {
776 ir_node *defProj; /**< default target */
777 int min_value; /**< smallest switch case */
778 int max_value; /**< largest switch case */
779 int num_branches; /**< number of jumps */
780 char *label; /**< label of the jump table */
781 branch_t *branches; /**< jump array */
785 * Compare two variables of type branch_t. Used to sort all switch cases
787 static int ia32_cmp_branch_t(const void *a, const void *b) {
788 branch_t *b1 = (branch_t *)a;
789 branch_t *b2 = (branch_t *)b;
791 if (b1->value <= b2->value)
798 * Emits code for a SwitchJmp (creates a jump table if
799 * possible otherwise a cmp-jmp cascade). Port from
802 static void emit_ia32_SwitchJmp(const ir_node *irn, ia32_emit_env_t *emit_env) {
803 unsigned long interval;
804 char buf[SNPRINTF_BUF_LEN];
805 int last_value, i, pn, do_jmp_tbl = 1;
808 const ir_edge_t *edge;
809 const lc_arg_env_t *env = ia32_get_arg_env();
810 FILE *F = emit_env->out;
811 char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
813 /* fill the table structure */
814 tbl.label = xmalloc(SNPRINTF_BUF_LEN);
815 tbl.label = get_unique_label(tbl.label, SNPRINTF_BUF_LEN, "JMPTBL_");
817 tbl.num_branches = get_irn_n_edges(irn);
818 tbl.branches = xcalloc(tbl.num_branches, sizeof(tbl.branches[0]));
819 tbl.min_value = INT_MAX;
820 tbl.max_value = INT_MIN;
823 /* go over all proj's and collect them */
824 foreach_out_edge(irn, edge) {
825 proj = get_edge_src_irn(edge);
826 assert(is_Proj(proj) && "Only proj allowed at SwitchJmp");
828 pn = get_Proj_proj(proj);
830 /* create branch entry */
831 tbl.branches[i].target = proj;
832 tbl.branches[i].value = pn;
834 tbl.min_value = pn < tbl.min_value ? pn : tbl.min_value;
835 tbl.max_value = pn > tbl.max_value ? pn : tbl.max_value;
837 /* check for default proj */
838 if (pn == get_ia32_pncode(irn)) {
839 assert(tbl.defProj == NULL && "found two defProjs at SwitchJmp");
846 /* sort the branches by their number */
847 qsort(tbl.branches, tbl.num_branches, sizeof(tbl.branches[0]), ia32_cmp_branch_t);
849 /* two-complement's magic make this work without overflow */
850 interval = tbl.max_value - tbl.min_value;
852 /* check value interval */
853 if (interval > 16 * 1024) {
857 /* check ratio of value interval to number of branches */
858 if ((float)(interval + 1) / (float)tbl.num_branches > 8.0) {
864 if (tbl.min_value != 0) {
865 lc_esnprintf(env, cmd_buf, SNPRINTF_BUF_LEN, "cmpl %lu, -%d(%1S)",
866 interval, tbl.min_value, irn);
867 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* first switch value is not 0 */");
872 lc_esnprintf(env, cmd_buf, SNPRINTF_BUF_LEN, "cmpl %lu, %1S", interval, irn);
873 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* compare for switch */");
878 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "ja %s", get_cfop_target(tbl.defProj, buf));
879 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* default jump if out of range */");
882 if (tbl.num_branches > 1) {
885 lc_esnprintf(env, cmd_buf, SNPRINTF_BUF_LEN, "jmp [%1S*4+%s]", irn, tbl.label);
886 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* get jump table entry as target */");
889 fprintf(F, "\t.section\t.rodata\n");
890 fprintf(F, "\t.align 4\n");
892 fprintf(F, "%s:\n", tbl.label);
894 snprintf(cmd_buf, SNPRINTF_BUF_LEN, ".long %s", get_cfop_target(tbl.branches[0].target, buf));
895 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* case %d */\n", tbl.branches[0].value);
898 last_value = tbl.branches[0].value;
899 for (i = 1; i < tbl.num_branches; ++i) {
900 while (++last_value < tbl.branches[i].value) {
901 snprintf(cmd_buf, SNPRINTF_BUF_LEN, ".long %s", get_cfop_target(tbl.defProj, buf));
902 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* default case */");
905 snprintf(cmd_buf, SNPRINTF_BUF_LEN, ".long %s", get_cfop_target(tbl.branches[i].target, buf));
906 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* case %d */", last_value);
910 fprintf(F, "\t.text");
913 /* one jump is enough */
914 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "jmp %s", get_cfop_target(tbl.branches[0].target, buf));
915 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* only one case given */");
919 else { // no jump table
920 for (i = 0; i < tbl.num_branches; ++i) {
921 lc_esnprintf(env, cmd_buf, SNPRINTF_BUF_LEN, "cmpl %d, %1S", tbl.branches[i].value, irn);
922 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* case %d */", i);
924 fprintf(F, "\tje %s\n", get_cfop_target(tbl.branches[i].target, buf));
927 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "jmp %s", get_cfop_target(tbl.defProj, buf));
928 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* default case */");
939 * Emits code for a unconditional jump.
941 static void emit_Jmp(const ir_node *irn, ia32_emit_env_t *env) {
942 ir_node *block, *next_bl;
944 char buf[SNPRINTF_BUF_LEN], cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
946 /* for now, the code works for scheduled and non-schedules blocks */
947 block = get_nodes_block(irn);
949 /* we have a block schedule */
950 next_bl = next_blk_sched(block);
951 if (get_cfop_target_block(irn) != next_bl) {
952 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "jmp %s", get_cfop_target(irn, buf));
953 lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* %+F(%+F) */", irn, get_cfop_target_block(irn));
957 lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* fallthrough %s */", get_cfop_target(irn, buf));
962 /****************************
965 * _ __ _ __ ___ _ ___
966 * | '_ \| '__/ _ \| |/ __|
967 * | |_) | | | (_) | |\__ \
968 * | .__/|_| \___/| ||___/
971 ****************************/
974 * Emits code for a proj -> node
976 static void emit_Proj(const ir_node *irn, ia32_emit_env_t *env) {
977 ir_node *pred = get_Proj_pred(irn);
979 if (get_irn_op(pred) == op_Start) {
980 switch(get_Proj_proj(irn)) {
981 case pn_Start_X_initial_exec:
990 /**********************************
993 * | | ___ _ __ _ _| |_) |
994 * | | / _ \| '_ \| | | | _ <
995 * | |___| (_) | |_) | |_| | |_) |
996 * \_____\___/| .__/ \__, |____/
999 **********************************/
1002 * Emit movsb/w instructions to make mov count divideable by 4
1004 static void emit_CopyB_prolog(FILE *F, int rem, int size) {
1005 char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
1007 fprintf(F, "\t/* memcopy %d bytes*/\n", size);
1009 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "cld");
1010 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* copy direction forward*/");
1015 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "movsb");
1016 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* memcopy remainder 1 */");
1019 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "movsw");
1020 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* memcopy remainder 2 */");
1023 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "movsb");
1024 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* memcopy remainder 3 */");
1026 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "movsw");
1027 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* memcopy remainder 3 */");
1035 * Emit rep movsd instruction for memcopy.
1037 static void emit_ia32_CopyB(const ir_node *irn, ia32_emit_env_t *emit_env) {
1038 FILE *F = emit_env->out;
1039 tarval *tv = get_ia32_Immop_tarval(irn);
1040 int rem = get_tarval_long(tv);
1041 int size = get_tarval_long(get_ia32_Immop_tarval(get_irn_n(irn, 2)));
1042 char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
1044 emit_CopyB_prolog(F, rem, size);
1046 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "rep movsd");
1047 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* memcopy */");
1052 * Emits unrolled memcopy.
1054 static void emit_ia32_CopyB_i(const ir_node *irn, ia32_emit_env_t *emit_env) {
1055 tarval *tv = get_ia32_Immop_tarval(irn);
1056 int size = get_tarval_long(tv);
1057 FILE *F = emit_env->out;
1058 char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
1060 emit_CopyB_prolog(F, size & 0x3, size);
1064 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "movsd");
1065 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* memcopy unrolled */");
1072 /***************************
1076 * | | / _ \| '_ \ \ / /
1077 * | |___| (_) | | | \ V /
1078 * \_____\___/|_| |_|\_/
1080 ***************************/
1083 * Emit code for conversions (I, FP), (FP, I) and (FP, FP).
1085 static void emit_ia32_Conv_with_FP(const ir_node *irn, ia32_emit_env_t *emit_env) {
1086 FILE *F = emit_env->out;
1087 const lc_arg_env_t *env = ia32_get_arg_env();
1088 char *from, *to, buf[64];
1089 ir_mode *src_mode, *tgt_mode;
1090 char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
1092 src_mode = is_ia32_AddrModeS(irn) ? get_ia32_ls_mode(irn) : get_irn_mode(get_irn_n(irn, 2));
1093 tgt_mode = get_ia32_res_mode(irn);
1095 from = mode_is_float(src_mode) ? (get_mode_size_bits(src_mode) == 32 ? "ss" : "sd") : "si";
1096 to = mode_is_float(tgt_mode) ? (get_mode_size_bits(tgt_mode) == 32 ? "ss" : "sd") : "si";
1098 switch(get_ia32_op_type(irn)) {
1100 lc_esnprintf(env, buf, sizeof(buf), "%1D, %3S", irn, irn);
1102 case ia32_AddrModeS:
1103 lc_esnprintf(env, buf, sizeof(buf), "%1D, %s", irn, ia32_emit_am(irn, emit_env));
1106 assert(0 && "unsupported op type for Conv");
1109 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "cvt%s2%s %s", from, to, buf);
1110 lc_esnprintf(env, cmnt_buf, SNPRINTF_BUF_LEN, "/* %+F(%+F, %+F) */", irn, src_mode, tgt_mode);
1114 static void emit_ia32_Conv_I2FP(const ir_node *irn, ia32_emit_env_t *emit_env) {
1115 emit_ia32_Conv_with_FP(irn, emit_env);
1118 static void emit_ia32_Conv_FP2I(const ir_node *irn, ia32_emit_env_t *emit_env) {
1119 emit_ia32_Conv_with_FP(irn, emit_env);
1122 static void emit_ia32_Conv_FP2FP(const ir_node *irn, ia32_emit_env_t *emit_env) {
1123 emit_ia32_Conv_with_FP(irn, emit_env);
1127 * Emits code for an Int conversion.
1129 static void emit_ia32_Conv_I2I(const ir_node *irn, ia32_emit_env_t *emit_env) {
1130 FILE *F = emit_env->out;
1131 const lc_arg_env_t *env = ia32_get_arg_env();
1132 char *move_cmd, *conv_cmd;
1133 ir_mode *src_mode, *tgt_mode;
1135 char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
1136 const arch_register_t *in_reg, *out_reg;
1138 src_mode = is_ia32_AddrModeS(irn) ? get_ia32_ls_mode(irn) : get_irn_mode(get_irn_n(irn, 2));
1139 tgt_mode = get_ia32_res_mode(irn);
1141 n = get_mode_size_bits(src_mode);
1142 m = get_mode_size_bits(tgt_mode);
1144 if (mode_is_signed(n < m ? src_mode : tgt_mode)) {
1146 if (n == 8 || m == 8)
1148 else if (n == 16 || m == 16)
1151 assert(0 && "unsupported Conv_I2I");
1158 switch(get_ia32_op_type(irn)) {
1160 in_reg = get_in_reg(irn, 2);
1161 out_reg = get_out_reg(irn, 0);
1163 if (REGS_ARE_EQUAL(in_reg, &ia32_gp_regs[REG_EAX]) &&
1164 REGS_ARE_EQUAL(out_reg, in_reg) &&
1165 mode_is_signed(n < m ? src_mode : tgt_mode))
1167 /* argument and result are both in EAX and */
1168 /* signedness is ok: -> use converts */
1169 lc_esnprintf(env, cmd_buf, SNPRINTF_BUF_LEN, "%s", conv_cmd);
1171 else if (REGS_ARE_EQUAL(out_reg, in_reg) &&
1172 ! mode_is_signed(n < m ? src_mode : tgt_mode))
1174 /* argument and result are in the same register */
1175 /* and signedness is ok: -> use and with mask */
1176 int mask = (1 << (n < m ? n : m)) - 1;
1177 lc_esnprintf(env, cmd_buf, SNPRINTF_BUF_LEN, "and %1D, 0x%x", irn, mask);
1180 /* use move w/o sign extension */
1181 lc_esnprintf(env, cmd_buf, SNPRINTF_BUF_LEN, "%s %1D, %%%s",
1182 move_cmd, irn, ia32_get_reg_name_for_mode(emit_env, n < m ? src_mode : tgt_mode, in_reg));
1186 case ia32_AddrModeS:
1187 lc_esnprintf(env, cmd_buf, SNPRINTF_BUF_LEN, "%s %1D, %s",
1188 move_cmd, irn, ia32_emit_am(irn, emit_env));
1191 assert(0 && "unsupported op type for Conv");
1194 lc_esnprintf(env, cmnt_buf, SNPRINTF_BUF_LEN, "/* %+F(%d Bit mode_%F -> %d Bit mode_%F) */",
1195 irn, n, src_mode, m, tgt_mode);
1201 * Emits code for an 8Bit Int conversion.
1203 void emit_ia32_Conv_I2I8Bit(const ir_node *irn, ia32_emit_env_t *emit_env) {
1204 emit_ia32_Conv_I2I(irn, emit_env);
1208 /*******************************************
1211 * | |__ ___ _ __ ___ __| | ___ ___
1212 * | '_ \ / _ \ '_ \ / _ \ / _` |/ _ \/ __|
1213 * | |_) | __/ | | | (_) | (_| | __/\__ \
1214 * |_.__/ \___|_| |_|\___/ \__,_|\___||___/
1216 *******************************************/
1219 * Emits a backend call
1221 static void emit_be_Call(const ir_node *irn, ia32_emit_env_t *emit_env) {
1222 FILE *F = emit_env->out;
1223 entity *ent = be_Call_get_entity(irn);
1224 char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
1227 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "call %s", get_entity_name(ent));
1230 lc_esnprintf(ia32_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "%1D", get_irn_n(irn, be_pos_Call_ptr));
1233 lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* %+F (be_Call) */", irn);
1239 * Emits code to increase stack pointer.
1241 static void emit_be_IncSP(const ir_node *irn, ia32_emit_env_t *emit_env) {
1242 FILE *F = emit_env->out;
1243 unsigned offs = be_get_IncSP_offset(irn);
1244 be_stack_dir_t dir = be_get_IncSP_direction(irn);
1245 char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
1248 lc_esnprintf(ia32_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "add %1S,%s%u", irn,
1249 (dir == be_stack_dir_expand) ? " -" : " ", offs);
1250 lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* %+F (IncSP) */", irn);
1253 snprintf(cmd_buf, SNPRINTF_BUF_LEN, " ");
1254 lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* omitted %+F (IncSP) with 0 */", irn);
1261 * Emits code to set stack pointer.
1263 static void emit_be_SetSP(const ir_node *irn, ia32_emit_env_t *emit_env) {
1264 FILE *F = emit_env->out;
1265 char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
1267 lc_esnprintf(ia32_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "mov %1D, %3S", irn, irn);
1268 lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* %+F (restore SP) */", irn);
1273 * Emits code for Copy.
1275 static void emit_be_Copy(const ir_node *irn, ia32_emit_env_t *emit_env) {
1276 FILE *F = emit_env->out;
1277 char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
1279 lc_esnprintf(ia32_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "mov %1D, %1S", irn, irn);
1280 lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* %+F */", irn);
1285 * Emits code for exchange.
1287 static void emit_be_Perm(const ir_node *irn, ia32_emit_env_t *emit_env) {
1288 FILE *F = emit_env->out;
1289 char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
1291 lc_esnprintf(ia32_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "xchg %1S, %2S", irn, irn);
1292 lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* %+F(%1A, %2A) */", irn, irn, irn);
1298 /***********************************************************************************
1301 * _ __ ___ __ _ _ _ __ | |_ _ __ __ _ _ __ ___ _____ _____ _ __| | __
1302 * | '_ ` _ \ / _` | | '_ \ | _| '__/ _` | '_ ` _ \ / _ \ \ /\ / / _ \| '__| |/ /
1303 * | | | | | | (_| | | | | | | | | | | (_| | | | | | | __/\ V V / (_) | | | <
1304 * |_| |_| |_|\__,_|_|_| |_| |_| |_| \__,_|_| |_| |_|\___| \_/\_/ \___/|_| |_|\_\
1306 ***********************************************************************************/
1309 * Enters the emitter functions for handled nodes into the generic
1310 * pointer of an opcode.
1312 static void ia32_register_emitters(void) {
1314 #define IA32_EMIT(a) op_ia32_##a->ops.generic = (op_func)emit_ia32_##a
1315 #define EMIT(a) op_##a->ops.generic = (op_func)emit_##a
1316 #define BE_EMIT(a) op_be_##a->ops.generic = (op_func)emit_be_##a
1318 /* first clear the generic function pointer for all ops */
1319 clear_irp_opcodes_generic_func();
1321 /* register all emitter functions defined in spec */
1322 ia32_register_spec_emitters();
1324 /* other ia32 emitter functions */
1329 IA32_EMIT(SwitchJmp);
1332 IA32_EMIT(Conv_I2FP);
1333 IA32_EMIT(Conv_FP2I);
1334 IA32_EMIT(Conv_FP2FP);
1335 IA32_EMIT(Conv_I2I);
1336 IA32_EMIT(Conv_I2I8Bit);
1338 /* benode emitter */
1355 * Emits code for a node.
1357 static void ia32_emit_node(const ir_node *irn, void *env) {
1358 ia32_emit_env_t *emit_env = env;
1359 firm_dbg_module_t *mod = emit_env->mod;
1360 FILE *F = emit_env->out;
1361 ir_op *op = get_irn_op(irn);
1363 DBG((mod, LEVEL_1, "emitting code for %+F\n", irn));
1365 if (op->ops.generic) {
1366 void (*emit)(const ir_node *, void *) = (void (*)(const ir_node *, void *))op->ops.generic;
1370 ir_fprintf(F, "\t%35s /* %+F (%+G) */\n", " ", irn, irn);
1375 * Walks over the nodes in a block connected by scheduling edges
1376 * and emits code for each node.
1378 static void ia32_gen_block(ir_node *block, void *env) {
1381 if (! is_Block(block))
1384 fprintf(((ia32_emit_env_t *)env)->out, BLOCK_PREFIX("%ld:\n"), get_irn_node_nr(block));
1385 sched_foreach(block, irn) {
1386 ia32_emit_node(irn, env);
1391 * Emits code for function start.
1393 static void ia32_emit_func_prolog(FILE *F, ir_graph *irg) {
1394 entity *irg_ent = get_irg_entity(irg);
1395 const char *irg_name = get_entity_name(irg_ent);
1397 if (get_entity_visibility(irg_ent) == visibility_external_visible) {
1398 fprintf(F, ".globl %s\n", irg_name);
1400 fprintf(F, "\t.type\t%s, @function\n", irg_name);
1401 fprintf(F, "%s:\n", irg_name);
1405 * Emits code for function end
1407 static void ia32_emit_func_epilog(FILE *F, ir_graph *irg) {
1408 const char *irg_name = get_entity_name(get_irg_entity(irg));
1410 fprintf(F, "\tret\n");
1411 fprintf(F, "\t.size\t%s, .-%s\n\n", irg_name, irg_name);
1416 * Sets labels for control flow nodes (jump target)
1417 * TODO: Jump optimization
1419 static void ia32_gen_labels(ir_node *block, void *env) {
1421 int n = get_Block_n_cfgpreds(block);
1423 for (n--; n >= 0; n--) {
1424 pred = get_Block_cfgpred(block, n);
1425 set_irn_link(pred, block);
1430 * Main driver. Emits the code for one routine.
1432 void ia32_gen_routine(FILE *F, ir_graph *irg, const ia32_code_gen_t *cg) {
1433 ia32_emit_env_t emit_env;
1437 emit_env.arch_env = cg->arch_env;
1439 emit_env.isa = (ia32_isa_t *)cg->arch_env->isa;
1440 FIRM_DBG_REGISTER(emit_env.mod, "firm.be.ia32.emitter");
1442 /* set the global arch_env (needed by print hooks) */
1443 arch_env = cg->arch_env;
1445 ia32_register_emitters();
1447 ia32_emit_func_prolog(F, irg);
1448 irg_block_walk_graph(irg, ia32_gen_labels, NULL, &emit_env);
1450 if (cg->opt.extbb && cg->blk_sched) {
1451 int i, n = ARR_LEN(cg->blk_sched);
1453 for (i = 0; i < n;) {
1456 block = cg->blk_sched[i];
1458 next_bl = i < n ? cg->blk_sched[i] : NULL;
1460 /* set here the link. the emitter expects to find the next block here */
1461 set_irn_link(block, next_bl);
1462 ia32_gen_block(block, &emit_env);
1466 /* "normal" block schedule: Note the get_next_block() returns the NUMBER of the block
1467 in the block schedule. As this number should NEVER be equal the next block,
1468 we does not need a clear block link here. */
1469 irg_walk_blkwise_graph(irg, NULL, ia32_gen_block, &emit_env);
1472 ia32_emit_func_epilog(F, irg);