16 #include "../besched.h"
18 #include "ia32_emitter.h"
19 #include "gen_ia32_emitter.h"
20 #include "ia32_nodes_attr.h"
21 #include "ia32_new_nodes.h"
22 #include "ia32_map_regs.h"
24 #ifdef obstack_chunk_alloc
25 # undef obstack_chunk_alloc
26 # define obstack_chunk_alloc xmalloc
28 # define obstack_chunk_alloc xmalloc
29 # define obstack_chunk_free free
32 #define SNPRINTF_BUF_LEN 128
34 static const arch_env_t *arch_env = NULL;
36 char *ia32_emit_binop(const ir_node *n) {
37 static char *buf = NULL;
40 buf = xcalloc(1, SNPRINTF_BUF_LEN);
43 memset(buf, 0, SNPRINTF_BUF_LEN);
46 switch(get_ia32_op_type(n)) {
48 if (get_ia32_cnst(n)) {
49 lc_esnprintf(ia32_get_arg_env(), buf, SNPRINTF_BUF_LEN, "%1D, %s", n, get_ia32_cnst(n));
52 lc_esnprintf(ia32_get_arg_env(), buf, SNPRINTF_BUF_LEN, "%1D, %4S", n, n);
56 lc_esnprintf(ia32_get_arg_env(), buf, SNPRINTF_BUF_LEN, "%1D, %s", n, ia32_emit_am(n));
59 lc_esnprintf(ia32_get_arg_env(), buf, SNPRINTF_BUF_LEN, "%s, %4S", ia32_emit_am(n), n);
62 assert(0 && "unsupported op type");
68 char *ia32_emit_unop(const ir_node *n) {
69 static char *buf = NULL;
72 buf = xcalloc(1, SNPRINTF_BUF_LEN);
75 memset(buf, 0, SNPRINTF_BUF_LEN);
78 switch(get_ia32_op_type(n)) {
80 lc_esnprintf(ia32_get_arg_env(), buf, SNPRINTF_BUF_LEN, "%1D", n);
83 snprintf(buf, SNPRINTF_BUF_LEN, ia32_emit_am(n));
86 assert(0 && "unsupported op type");
92 char *ia32_emit_am(const ir_node *n) {
93 ia32_am_flavour_t am_flav = get_ia32_am_flavour(n);
97 static struct obstack *obst = NULL;
100 obst = xcalloc(1, sizeof(*obst));
103 obstack_free(obst, NULL);
106 /* obstack_free with NULL results in an uninitialized obstack */
109 obstack_printf(obst, "[");
111 if (am_flav & ia32_B) {
112 lc_eoprintf(ia32_get_arg_env(), obst, "%1S", n);
116 if (am_flav & ia32_I) {
118 obstack_printf(obst, "+");
121 lc_eoprintf(ia32_get_arg_env(), obst, "%2S", n);
123 if (am_flav & ia32_S) {
124 obstack_printf(obst, "*%d", get_ia32_am_scale(n));
130 if (am_flav & ia32_O) {
131 obstack_printf(obst, get_ia32_am_offs(n));
134 obstack_printf(obst, "] ");
136 size = obstack_object_size(obst);
137 s = obstack_finish(obst);
143 /*************************************************************
145 * (_) | | / _| | | | |
146 * _ __ _ __ _ _ __ | |_| |_ | |__ ___| |_ __ ___ _ __
147 * | '_ \| '__| | '_ \| __| _| | '_ \ / _ \ | '_ \ / _ \ '__|
148 * | |_) | | | | | | | |_| | | | | | __/ | |_) | __/ |
149 * | .__/|_| |_|_| |_|\__|_| |_| |_|\___|_| .__/ \___|_|
152 *************************************************************/
154 /* We always pass the ir_node which is a pointer. */
155 static int ia32_get_arg_type(const lc_arg_occ_t *occ) {
156 return lc_arg_type_ptr;
161 * Returns the register at in position pos.
163 static const arch_register_t *get_in_reg(ir_node *irn, int pos) {
165 const arch_register_t *reg = NULL;
167 assert(get_irn_arity(irn) > pos && "Invalid IN position");
169 /* The out register of the operator at position pos is the
170 in register we need. */
171 op = get_irn_n(irn, pos);
173 reg = arch_get_irn_register(arch_env, op);
175 assert(reg && "no in register found");
180 * Returns the register at out position pos.
182 static const arch_register_t *get_out_reg(ir_node *irn, int pos) {
184 const arch_register_t *reg = NULL;
186 assert(get_irn_n_edges(irn) > pos && "Invalid OUT position");
188 /* 1st case: irn is not of mode_T, so it has only */
189 /* one OUT register -> good */
190 /* 2nd case: irn is of mode_T -> collect all Projs and ask the */
191 /* Proj with the corresponding projnum for the register */
193 if (get_irn_mode(irn) != mode_T) {
194 reg = arch_get_irn_register(arch_env, irn);
196 else if (is_ia32_irn(irn)) {
197 reg = get_ia32_out_reg(irn, pos);
200 const ir_edge_t *edge;
202 foreach_out_edge(irn, edge) {
203 proj = get_edge_src_irn(edge);
204 assert(is_Proj(proj) && "non-Proj from mode_T node");
205 if (get_Proj_proj(proj) == pos) {
206 reg = arch_get_irn_register(arch_env, proj);
212 assert(reg && "no out register found");
217 * Returns the number of the in register at position pos.
219 int get_ia32_reg_nr(ir_node *irn, int pos, int in_out) {
220 const arch_register_t *reg;
223 reg = get_in_reg(irn, pos);
226 reg = get_out_reg(irn, pos);
229 return arch_register_get_index(reg);
233 * Returns the name of the in register at position pos.
235 const char *get_ia32_reg_name(ir_node *irn, int pos, int in_out) {
236 const arch_register_t *reg;
239 reg = get_in_reg(irn, pos);
242 reg = get_out_reg(irn, pos);
245 return arch_register_get_name(reg);
249 * Get the register name for a node.
251 static int ia32_get_reg_name(lc_appendable_t *app,
252 const lc_arg_occ_t *occ, const lc_arg_value_t *arg)
255 ir_node *X = arg->v_ptr;
256 int nr = occ->width - 1;
259 return lc_arg_append(app, occ, "(null)", 6);
261 if (occ->conversion == 'S') {
262 buf = get_ia32_reg_name(X, nr, 1);
265 buf = get_ia32_reg_name(X, nr, 0);
268 lc_appendable_chadd(app, '%');
269 return lc_arg_append(app, occ, buf, strlen(buf));
273 * Returns the tarval or offset of an ia32 as a string.
275 static int ia32_const_to_str(lc_appendable_t *app,
276 const lc_arg_occ_t *occ, const lc_arg_value_t *arg)
279 ir_node *X = arg->v_ptr;
282 return lc_arg_append(app, occ, "(null)", 6);
284 if (occ->conversion == 'C') {
285 buf = get_ia32_cnst(X);
288 buf = get_ia32_am_offs(X);
291 return lc_arg_append(app, occ, buf, strlen(buf));
295 * Determines the SSE suffix depending on the mode.
297 static int ia32_get_mode_suffix(lc_appendable_t *app,
298 const lc_arg_occ_t *occ, const lc_arg_value_t *arg)
300 ir_node *X = arg->v_ptr;
303 return lc_arg_append(app, occ, "(null)", 6);
305 if (get_mode_size_bits(get_irn_mode(X)) == 32)
306 return lc_appendable_chadd(app, 's');
308 return lc_appendable_chadd(app, 'd');
312 * Return the ia32 printf arg environment.
313 * We use the firm environment with some additional handlers.
315 const lc_arg_env_t *ia32_get_arg_env(void) {
316 static lc_arg_env_t *env = NULL;
318 static const lc_arg_handler_t ia32_reg_handler = { ia32_get_arg_type, ia32_get_reg_name };
319 static const lc_arg_handler_t ia32_const_handler = { ia32_get_arg_type, ia32_const_to_str };
320 static const lc_arg_handler_t ia32_mode_handler = { ia32_get_arg_type, ia32_get_mode_suffix };
323 /* extend the firm printer */
324 env = firm_get_arg_env();
327 lc_arg_register(env, "ia32:sreg", 'S', &ia32_reg_handler);
328 lc_arg_register(env, "ia32:dreg", 'D', &ia32_reg_handler);
329 lc_arg_register(env, "ia32:cnst", 'C', &ia32_const_handler);
330 lc_arg_register(env, "ia32:offs", 'O', &ia32_const_handler);
331 lc_arg_register(env, "ia32:mode", 'M', &ia32_mode_handler);
338 * For 2-address code we need to make sure the first src reg is equal to dest reg.
340 void equalize_dest_src(FILE *F, ir_node *n) {
341 if (get_ia32_reg_nr(n, 0, 1) != get_ia32_reg_nr(n, 0, 0)) {
342 if (get_irn_arity(n) > 1 && get_ia32_reg_nr(n, 1, 1) == get_ia32_reg_nr(n, 0, 0)) {
343 if (! is_op_commutative(get_irn_op(n))) {
344 /* we only need to exchange for non-commutative ops */
345 lc_efprintf(ia32_get_arg_env(), F, "\txchg %1S, %2S\t\t\t/* xchg src1 <-> src2 for 2 address code */\n", n, n);
349 lc_efprintf(ia32_get_arg_env(), F, "\tmovl %1S, %1D\t\t\t/* src -> dest for 2 address code */\n", n, n);
355 * Add a number to a prefix. This number will not be used a second time.
357 char *get_unique_label(char *buf, size_t buflen, const char *prefix) {
358 static unsigned long id = 0;
359 snprintf(buf, buflen, "%s%lu", prefix, ++id);
364 /*************************************************
367 * ___ _ __ ___ _| |_ ___ ___ _ __ __| |
368 * / _ \ '_ ` _ \| | __| / __/ _ \| '_ \ / _` |
369 * | __/ | | | | | | |_ | (_| (_) | | | | (_| |
370 * \___|_| |_| |_|_|\__| \___\___/|_| |_|\__,_|
372 *************************************************/
375 * coding of conditions
377 struct cmp2conditon_t {
383 * positive conditions for signed compares
385 static const struct cmp2conditon_t cmp2condition_s[] = {
386 { NULL, pn_Cmp_False }, /* always false */
387 { "e", pn_Cmp_Eq }, /* == */
388 { "l", pn_Cmp_Lt }, /* < */
389 { "le", pn_Cmp_Le }, /* <= */
390 { "g", pn_Cmp_Gt }, /* > */
391 { "ge", pn_Cmp_Ge }, /* >= */
392 { "ne", pn_Cmp_Lg }, /* != */
393 { "ordered", pn_Cmp_Leg }, /* Floating point: ordered */
394 { "unordered", pn_Cmp_Uo }, /* FLoting point: unordered */
395 { "unordered or ==", pn_Cmp_Ue }, /* Floating point: unordered or == */
396 { "unordered or <", pn_Cmp_Ul }, /* Floating point: unordered or < */
397 { "unordered or <=", pn_Cmp_Ule }, /* Floating point: unordered or <= */
398 { "unordered or >", pn_Cmp_Ug }, /* Floating point: unordered or > */
399 { "unordered or >=", pn_Cmp_Uge }, /* Floating point: unordered or >= */
400 { "unordered or !=", pn_Cmp_Ne }, /* Floating point: unordered or != */
401 { NULL, pn_Cmp_True }, /* always true */
405 * positive conditions for unsigned compares
407 static const struct cmp2conditon_t cmp2condition_u[] = {
408 { NULL, pn_Cmp_False }, /* always false */
409 { "e", pn_Cmp_Eq }, /* == */
410 { "b", pn_Cmp_Lt }, /* < */
411 { "be", pn_Cmp_Le }, /* <= */
412 { "a", pn_Cmp_Gt }, /* > */
413 { "ae", pn_Cmp_Ge }, /* >= */
414 { "ne", pn_Cmp_Lg }, /* != */
415 { "ordered", pn_Cmp_Leg }, /* Floating point: ordered */
416 { "unordered", pn_Cmp_Uo }, /* FLoting point: unordered */
417 { "unordered or ==", pn_Cmp_Ue }, /* Floating point: unordered or == */
418 { "unordered or <", pn_Cmp_Ul }, /* Floating point: unordered or < */
419 { "unordered or <=", pn_Cmp_Ule }, /* Floating point: unordered or <= */
420 { "unordered or >", pn_Cmp_Ug }, /* Floating point: unordered or > */
421 { "unordered or >=", pn_Cmp_Uge }, /* Floating point: unordered or >= */
422 { "unordered or !=", pn_Cmp_Ne }, /* Floating point: unordered or != */
423 { NULL, pn_Cmp_True }, /* always true */
427 * returns the condition code
429 static const char *get_cmp_suffix(int cmp_code, int unsigned_cmp)
431 assert(cmp2condition_s[cmp_code].num == cmp_code);
432 assert(cmp2condition_u[cmp_code].num == cmp_code);
434 return unsigned_cmp ? cmp2condition_u[cmp_code & 7].name : cmp2condition_s[cmp_code & 7].name;
438 * Returns the target label for a control flow node.
440 static char *get_cfop_target(const ir_node *irn, char *buf) {
441 ir_node *bl = get_irn_link(irn);
443 snprintf(buf, SNPRINTF_BUF_LEN, "BLOCK_%ld", get_irn_node_nr(bl));
448 * Emits the jump sequence for a conditional jump (cmp + jmp_true + jmp_false)
450 static void finish_CondJmp(FILE *F, ir_node *irn) {
452 const ir_edge_t *edge;
453 char buf[SNPRINTF_BUF_LEN];
455 edge = get_irn_out_edge_first(irn);
456 proj = get_edge_src_irn(edge);
457 assert(is_Proj(proj) && "CondJmp with a non-Proj");
459 if (get_Proj_proj(proj) == 1) {
460 fprintf(F, "\tj%s %s\t\t\t/* cmp(a, b) == TRUE */\n",
461 get_cmp_suffix(get_ia32_pncode(irn), !mode_is_signed(get_irn_mode(get_irn_n(irn, 0)))),
462 get_cfop_target(proj, buf));
465 fprintf(F, "\tjn%s %s\t\t\t/* cmp(a, b) == FALSE */\n",
466 get_cmp_suffix(get_ia32_pncode(irn), !mode_is_signed(get_irn_mode(get_irn_n(irn, 0)))),
467 get_cfop_target(proj, buf));
470 edge = get_irn_out_edge_next(irn, edge);
472 proj = get_edge_src_irn(edge);
473 assert(is_Proj(proj) && "CondJmp with a non-Proj");
474 fprintf(F, "\tjmp %s\t\t\t/* otherwise */\n", get_cfop_target(proj, buf));
479 * Emits code for conditional jump with two variables.
481 static void emit_ia32_CondJmp(ir_node *irn, emit_env_t *env) {
484 lc_efprintf(ia32_get_arg_env(), F, "\tcmp %2S, %1S\t\t\t/* CondJmp(%+F, %+F) */\n", irn, irn,
485 get_irn_n(irn, 0), get_irn_n(irn, 1));
486 finish_CondJmp(F, irn);
490 * Emits code for conditional jump with immediate.
492 void emit_ia32_CondJmp_i(ir_node *irn, emit_env_t *env) {
495 lc_efprintf(ia32_get_arg_env(), F, "\tcmp %C, %1S\t\t\t/* CondJmp_i(%+F) */\n", irn, irn, get_irn_n(irn, 0));
496 finish_CondJmp(F, irn);
501 /*********************************************************
504 * ___ _ __ ___ _| |_ _ _ _ _ __ ___ _ __ ___
505 * / _ \ '_ ` _ \| | __| | | | | | '_ ` _ \| '_ \/ __|
506 * | __/ | | | | | | |_ | | |_| | | | | | | |_) \__ \
507 * \___|_| |_| |_|_|\__| | |\__,_|_| |_| |_| .__/|___/
510 *********************************************************/
512 /* jump table entry (target and corresponding number) */
513 typedef struct _branch_t {
518 /* jump table for switch generation */
519 typedef struct _jmp_tbl_t {
520 ir_node *defProj; /**< default target */
521 int min_value; /**< smallest switch case */
522 int max_value; /**< largest switch case */
523 int num_branches; /**< number of jumps */
524 char *label; /**< label of the jump table */
525 branch_t *branches; /**< jump array */
529 * Compare two variables of type branch_t. Used to sort all switch cases
531 static int ia32_cmp_branch_t(const void *a, const void *b) {
532 branch_t *b1 = (branch_t *)a;
533 branch_t *b2 = (branch_t *)b;
535 if (b1->value <= b2->value)
542 * Emits code for a SwitchJmp (creates a jump table if
543 * possible otherwise a cmp-jmp cascade). Port from
546 void emit_ia32_SwitchJmp(const ir_node *irn, emit_env_t *emit_env) {
547 unsigned long interval;
548 char buf[SNPRINTF_BUF_LEN];
549 int last_value, i, pn, do_jmp_tbl = 1;
552 const ir_edge_t *edge;
553 const lc_arg_env_t *env = ia32_get_arg_env();
554 FILE *F = emit_env->out;
556 /* fill the table structure */
557 tbl.label = xmalloc(SNPRINTF_BUF_LEN);
558 tbl.label = get_unique_label(tbl.label, SNPRINTF_BUF_LEN, "JMPTBL_");
560 tbl.num_branches = get_irn_n_edges(irn);
561 tbl.branches = xcalloc(tbl.num_branches, sizeof(tbl.branches[0]));
562 tbl.min_value = INT_MAX;
563 tbl.max_value = INT_MIN;
566 /* go over all proj's and collect them */
567 foreach_out_edge(irn, edge) {
568 proj = get_edge_src_irn(edge);
569 assert(is_Proj(proj) && "Only proj allowed at SwitchJmp");
571 pn = get_Proj_proj(proj);
573 /* create branch entry */
574 tbl.branches[i].target = proj;
575 tbl.branches[i].value = pn;
577 tbl.min_value = pn < tbl.min_value ? pn : tbl.min_value;
578 tbl.max_value = pn > tbl.max_value ? pn : tbl.max_value;
580 /* check for default proj */
581 if (pn == get_ia32_pncode(irn)) {
582 assert(tbl.defProj == NULL && "found two defProjs at SwitchJmp");
589 /* sort the branches by their number */
590 qsort(tbl.branches, tbl.num_branches, sizeof(tbl.branches[0]), ia32_cmp_branch_t);
592 /* two-complement's magic make this work without overflow */
593 interval = tbl.max_value - tbl.min_value;
595 /* check value interval */
596 if (interval > 16 * 1024) {
600 /* check ratio of value interval to number of branches */
601 if ((float)(interval + 1) / (float)tbl.num_branches > 8.0) {
607 if (tbl.min_value != 0) {
608 fprintf(F, "\tcmpl %lu, -%d", interval, tbl.min_value);
609 lc_efprintf(env, F, "(%1S)\t\t/* first switch value is not 0 */\n", irn);
612 fprintf(F, "\tcmpl %lu, ", interval);
613 lc_efprintf(env, F, "%1S\t\t\t/* compare for switch */\n", irn);
616 fprintf(F, "\tja %s\t\t\t/* default jump if out of range */\n", get_cfop_target(tbl.defProj, buf));
618 if (tbl.num_branches > 1) {
621 //fprintf(F, "\tjmp *%s", tbl.label);
622 lc_efprintf(env, F, "\tjmp *%s(,%1S,4)\t\t/* get jump table entry as target */\n", tbl.label, irn);
624 fprintf(F, "\t.section\t.rodata\t\t/* start jump table */\n");
625 fprintf(F, "\t.align 4\n");
627 fprintf(F, "%s:\n", tbl.label);
628 fprintf(F, "\t.long %s\t\t\t/* case %d */\n", get_cfop_target(tbl.branches[0].target, buf), tbl.branches[0].value);
630 last_value = tbl.branches[0].value;
631 for (i = 1; i < tbl.num_branches; ++i) {
632 while (++last_value < tbl.branches[i].value) {
633 fprintf(F, "\t.long %s\t\t/* default case */\n", get_cfop_target(tbl.defProj, buf));
635 fprintf(F, "\t.long %s\t\t\t/* case %d */\n", get_cfop_target(tbl.branches[i].target, buf), last_value);
638 fprintf(F, "\t.text\t\t\t\t/* end of jump table */\n");
641 /* one jump is enough */
642 fprintf(F, "\tjmp %s\t\t/* only one case given */\n", get_cfop_target(tbl.branches[0].target, buf));
645 else { // no jump table
646 for (i = 0; i < tbl.num_branches; ++i) {
647 fprintf(F, "\tcmpl %d, ", tbl.branches[i].value);
648 lc_efprintf(env, F, "%1S", irn);
649 fprintf(F, "\t\t\t/* case %d */\n", tbl.branches[i].value);
650 fprintf(F, "\tje %s\n", get_cfop_target(tbl.branches[i].target, buf));
653 fprintf(F, "\tjmp %s\t\t\t/* default case */\n", get_cfop_target(tbl.defProj, buf));
663 * Emits code for a unconditional jump.
665 void emit_Jmp(ir_node *irn, emit_env_t *env) {
668 char buf[SNPRINTF_BUF_LEN];
669 ir_fprintf(F, "\tjmp %s\t\t\t/* Jmp(%+F) */\n", get_cfop_target(irn, buf), get_irn_link(irn));
674 /****************************
677 * _ __ _ __ ___ _ ___
678 * | '_ \| '__/ _ \| |/ __|
679 * | |_) | | | (_) | |\__ \
680 * | .__/|_| \___/| ||___/
683 ****************************/
686 * Emits code for a proj -> node
688 void emit_Proj(ir_node *irn, emit_env_t *env) {
689 ir_node *pred = get_Proj_pred(irn);
691 if (get_irn_op(pred) == op_Start) {
692 switch(get_Proj_proj(irn)) {
693 case pn_Start_X_initial_exec:
702 /**********************************
705 * | | ___ _ __ _ _| |_) |
706 * | | / _ \| '_ \| | | | _ <
707 * | |___| (_) | |_) | |_| | |_) |
708 * \_____\___/| .__/ \__, |____/
711 **********************************/
713 static void emit_CopyB_prolog(FILE *F, int rem, int size) {
714 fprintf(F, "\t/* memcopy %d bytes*/\n", size);
715 fprintf(F, "\tcld\t\t\t\t/* copy direction forward*/\n");
719 fprintf(F, "\tmovsb\t\t\t\t/* memcopy remainder 1 */\n");
722 fprintf(F, "\tmovsw\t\t\t\t/* memcopy remainder 2 */\n");
725 fprintf(F, "\tmovsb\t\t\t\t/* memcopy remainder 3 */\n");
726 fprintf(F, "\tmovsw\t\t\t\t/* memcopy remainder 3 */\n");
731 void emit_ia32_CopyB(ir_node *irn, emit_env_t *emit_env) {
732 FILE *F = emit_env->out;
733 tarval *tv = get_ia32_Immop_tarval(irn);
734 int rem = get_tarval_long(tv);
735 int size = get_tarval_long(get_ia32_Immop_tarval(get_irn_n(irn, 2)));
737 emit_CopyB_prolog(F, rem, size);
739 fprintf(F, "\trep movsd\t\t\t\t/* memcopy */\n");
742 void emit_ia32_CopyB_i(ir_node *irn, emit_env_t *emit_env) {
743 tarval *tv = get_ia32_Immop_tarval(irn);
744 int size = get_tarval_long(tv);
745 FILE *F = emit_env->out;
747 emit_CopyB_prolog(F, size & 0x3, size);
751 fprintf(F, "\tmovsd\t\t\t\t/* memcopy unrolled */\n");
755 /********************
763 ********************/
765 void emit_ia32_Call(ir_node *irn, emit_env_t *emit_env) {
770 /***********************************************************************************
773 * _ __ ___ __ _ _ _ __ | |_ _ __ __ _ _ __ ___ _____ _____ _ __| | __
774 * | '_ ` _ \ / _` | | '_ \ | _| '__/ _` | '_ ` _ \ / _ \ \ /\ / / _ \| '__| |/ /
775 * | | | | | | (_| | | | | | | | | | | (_| | | | | | | __/\ V V / (_) | | | <
776 * |_| |_| |_|\__,_|_|_| |_| |_| |_| \__,_|_| |_| |_|\___| \_/\_/ \___/|_| |_|\_\
778 ***********************************************************************************/
781 * Emits code for a node.
783 void ia32_emit_node(ir_node *irn, void *env) {
784 emit_env_t *emit_env = env;
785 firm_dbg_module_t *mod = emit_env->mod;
786 FILE *F = emit_env->out;
788 DBG((mod, LEVEL_1, "emitting code for %+F\n", irn));
790 #define IA32_EMIT(a) if (is_ia32_##a(irn)) { emit_ia32_##a(irn, emit_env); return; }
791 #define EMIT(a) if (get_irn_opcode(irn) == iro_##a) { emit_##a(irn, emit_env); return; }
793 /* generated int emitter functions */
830 /* generated floating point emitter */
845 /* other emitter functions */
847 IA32_EMIT(SwitchJmp);
853 ir_fprintf(F, "\t\t\t\t\t/* %+F */\n", irn);
857 * Walks over the nodes in a block connected by scheduling edges
858 * and emits code for each node.
860 void ia32_gen_block(ir_node *block, void *env) {
863 if (! is_Block(block))
866 fprintf(((emit_env_t *)env)->out, "BLOCK_%ld:\n", get_irn_node_nr(block));
867 sched_foreach(block, irn) {
868 ia32_emit_node(irn, env);
874 * Emits code for function start.
876 void ia32_emit_start(FILE *F, ir_graph *irg) {
877 const char *irg_name = get_entity_name(get_irg_entity(irg));
879 fprintf(F, "\t.text\n");
880 fprintf(F, ".globl %s\n", irg_name);
881 fprintf(F, "\t.type\t%s, @function\n", irg_name);
882 fprintf(F, "%s:\n", irg_name);
886 * Emits code for function end
888 void ia32_emit_end(FILE *F, ir_graph *irg) {
889 const char *irg_name = get_entity_name(get_irg_entity(irg));
891 fprintf(F, "\tret\n");
892 fprintf(F, "\t.size\t%s, .-%s\n\n", irg_name, irg_name);
896 * Sets labels for control flow nodes (jump target)
897 * TODO: Jump optimization
899 void ia32_gen_labels(ir_node *block, void *env) {
901 int n = get_Block_n_cfgpreds(block);
903 for (n--; n >= 0; n--) {
904 pred = get_Block_cfgpred(block, n);
905 set_irn_link(pred, block);
912 void ia32_gen_routine(FILE *F, ir_graph *irg, const ia32_code_gen_t *cg) {
915 emit_env.mod = firm_dbg_register("ir.be.codegen.ia32");
917 emit_env.arch_env = cg->arch_env;
920 /* set the global arch_env (needed by print hooks) */
921 arch_env = cg->arch_env;
923 ia32_emit_start(F, irg);
924 irg_block_walk_graph(irg, ia32_gen_labels, NULL, &emit_env);
925 irg_walk_blkwise_graph(irg, NULL, ia32_gen_block, &emit_env);
926 ia32_emit_end(F, irg);