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);
238 * Returns the name of the in register at position pos.
240 static const char *get_ia32_reg_name(ir_node *irn, int pos, enum io_direction in_out) {
241 const arch_register_t *reg;
243 if (in_out == IN_REG) {
244 reg = get_in_reg(irn, pos);
247 reg = get_out_reg(irn, pos);
250 return arch_register_get_name(reg);
254 * Get the register name for a node.
256 static int ia32_get_reg_name(lc_appendable_t *app,
257 const lc_arg_occ_t *occ, const lc_arg_value_t *arg)
260 ir_node *X = arg->v_ptr;
261 int nr = occ->width - 1;
264 return lc_arg_append(app, occ, "(null)", 6);
266 buf = get_ia32_reg_name(X, nr, occ->conversion == 'S' ? IN_REG : OUT_REG);
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);
339 * Add a number to a prefix. This number will not be used a second time.
341 static char *get_unique_label(char *buf, size_t buflen, const char *prefix) {
342 static unsigned long id = 0;
343 snprintf(buf, buflen, "%s%lu", prefix, ++id);
348 /*************************************************
351 * ___ _ __ ___ _| |_ ___ ___ _ __ __| |
352 * / _ \ '_ ` _ \| | __| / __/ _ \| '_ \ / _` |
353 * | __/ | | | | | | |_ | (_| (_) | | | | (_| |
354 * \___|_| |_| |_|_|\__| \___\___/|_| |_|\__,_|
356 *************************************************/
359 * coding of conditions
361 struct cmp2conditon_t {
367 * positive conditions for signed compares
369 static const struct cmp2conditon_t cmp2condition_s[] = {
370 { NULL, pn_Cmp_False }, /* always false */
371 { "e", pn_Cmp_Eq }, /* == */
372 { "l", pn_Cmp_Lt }, /* < */
373 { "le", pn_Cmp_Le }, /* <= */
374 { "g", pn_Cmp_Gt }, /* > */
375 { "ge", pn_Cmp_Ge }, /* >= */
376 { "ne", pn_Cmp_Lg }, /* != */
377 { "ordered", pn_Cmp_Leg }, /* Floating point: ordered */
378 { "unordered", pn_Cmp_Uo }, /* FLoting point: unordered */
379 { "unordered or ==", pn_Cmp_Ue }, /* Floating point: unordered or == */
380 { "unordered or <", pn_Cmp_Ul }, /* Floating point: unordered or < */
381 { "unordered or <=", pn_Cmp_Ule }, /* Floating point: unordered or <= */
382 { "unordered or >", pn_Cmp_Ug }, /* Floating point: unordered or > */
383 { "unordered or >=", pn_Cmp_Uge }, /* Floating point: unordered or >= */
384 { "unordered or !=", pn_Cmp_Ne }, /* Floating point: unordered or != */
385 { NULL, pn_Cmp_True }, /* always true */
389 * positive conditions for unsigned compares
391 static const struct cmp2conditon_t cmp2condition_u[] = {
392 { NULL, pn_Cmp_False }, /* always false */
393 { "e", pn_Cmp_Eq }, /* == */
394 { "b", pn_Cmp_Lt }, /* < */
395 { "be", pn_Cmp_Le }, /* <= */
396 { "a", pn_Cmp_Gt }, /* > */
397 { "ae", pn_Cmp_Ge }, /* >= */
398 { "ne", pn_Cmp_Lg }, /* != */
399 { "ordered", pn_Cmp_Leg }, /* Floating point: ordered */
400 { "unordered", pn_Cmp_Uo }, /* FLoting point: unordered */
401 { "unordered or ==", pn_Cmp_Ue }, /* Floating point: unordered or == */
402 { "unordered or <", pn_Cmp_Ul }, /* Floating point: unordered or < */
403 { "unordered or <=", pn_Cmp_Ule }, /* Floating point: unordered or <= */
404 { "unordered or >", pn_Cmp_Ug }, /* Floating point: unordered or > */
405 { "unordered or >=", pn_Cmp_Uge }, /* Floating point: unordered or >= */
406 { "unordered or !=", pn_Cmp_Ne }, /* Floating point: unordered or != */
407 { NULL, pn_Cmp_True }, /* always true */
411 * returns the condition code
413 static const char *get_cmp_suffix(int cmp_code, int unsigned_cmp)
415 assert(cmp2condition_s[cmp_code].num == cmp_code);
416 assert(cmp2condition_u[cmp_code].num == cmp_code);
418 return unsigned_cmp ? cmp2condition_u[cmp_code & 7].name : cmp2condition_s[cmp_code & 7].name;
422 * Returns the target label for a control flow node.
424 static char *get_cfop_target(const ir_node *irn, char *buf) {
425 ir_node *bl = get_irn_link(irn);
427 snprintf(buf, SNPRINTF_BUF_LEN, "BLOCK_%ld", get_irn_node_nr(bl));
432 * Emits the jump sequence for a conditional jump (cmp + jmp_true + jmp_false)
434 static void finish_CondJmp(FILE *F, ir_node *irn) {
436 const ir_edge_t *edge;
437 char buf[SNPRINTF_BUF_LEN];
439 edge = get_irn_out_edge_first(irn);
440 proj = get_edge_src_irn(edge);
441 assert(is_Proj(proj) && "CondJmp with a non-Proj");
443 if (get_Proj_proj(proj) == 1) {
444 fprintf(F, "\tj%s %s\t\t\t/* cmp(a, b) == TRUE */\n",
445 get_cmp_suffix(get_ia32_pncode(irn), !mode_is_signed(get_irn_mode(get_irn_n(irn, 0)))),
446 get_cfop_target(proj, buf));
449 fprintf(F, "\tjn%s %s\t\t\t/* cmp(a, b) == FALSE */\n",
450 get_cmp_suffix(get_ia32_pncode(irn), !mode_is_signed(get_irn_mode(get_irn_n(irn, 0)))),
451 get_cfop_target(proj, buf));
454 edge = get_irn_out_edge_next(irn, edge);
456 proj = get_edge_src_irn(edge);
457 assert(is_Proj(proj) && "CondJmp with a non-Proj");
458 fprintf(F, "\tjmp %s\t\t\t/* otherwise */\n", get_cfop_target(proj, buf));
463 * Emits code for conditional jump with two variables.
465 static void emit_ia32_CondJmp(ir_node *irn, emit_env_t *env) {
468 lc_efprintf(ia32_get_arg_env(), F, "\tcmp %2S, %1S\t\t\t/* CondJmp(%+F, %+F) */\n", irn, irn,
469 get_irn_n(irn, 0), get_irn_n(irn, 1));
470 finish_CondJmp(F, irn);
474 * Emits code for conditional jump with immediate.
476 void emit_ia32_CondJmp_i(ir_node *irn, emit_env_t *env) {
479 lc_efprintf(ia32_get_arg_env(), F, "\tcmp %C, %1S\t\t\t/* CondJmp_i(%+F) */\n", irn, irn, get_irn_n(irn, 0));
480 finish_CondJmp(F, irn);
485 /*********************************************************
488 * ___ _ __ ___ _| |_ _ _ _ _ __ ___ _ __ ___
489 * / _ \ '_ ` _ \| | __| | | | | | '_ ` _ \| '_ \/ __|
490 * | __/ | | | | | | |_ | | |_| | | | | | | |_) \__ \
491 * \___|_| |_| |_|_|\__| | |\__,_|_| |_| |_| .__/|___/
494 *********************************************************/
496 /* jump table entry (target and corresponding number) */
497 typedef struct _branch_t {
502 /* jump table for switch generation */
503 typedef struct _jmp_tbl_t {
504 ir_node *defProj; /**< default target */
505 int min_value; /**< smallest switch case */
506 int max_value; /**< largest switch case */
507 int num_branches; /**< number of jumps */
508 char *label; /**< label of the jump table */
509 branch_t *branches; /**< jump array */
513 * Compare two variables of type branch_t. Used to sort all switch cases
515 static int ia32_cmp_branch_t(const void *a, const void *b) {
516 branch_t *b1 = (branch_t *)a;
517 branch_t *b2 = (branch_t *)b;
519 if (b1->value <= b2->value)
526 * Emits code for a SwitchJmp (creates a jump table if
527 * possible otherwise a cmp-jmp cascade). Port from
530 void emit_ia32_SwitchJmp(const ir_node *irn, emit_env_t *emit_env) {
531 unsigned long interval;
532 char buf[SNPRINTF_BUF_LEN];
533 int last_value, i, pn, do_jmp_tbl = 1;
536 const ir_edge_t *edge;
537 const lc_arg_env_t *env = ia32_get_arg_env();
538 FILE *F = emit_env->out;
540 /* fill the table structure */
541 tbl.label = xmalloc(SNPRINTF_BUF_LEN);
542 tbl.label = get_unique_label(tbl.label, SNPRINTF_BUF_LEN, "JMPTBL_");
544 tbl.num_branches = get_irn_n_edges(irn);
545 tbl.branches = xcalloc(tbl.num_branches, sizeof(tbl.branches[0]));
546 tbl.min_value = INT_MAX;
547 tbl.max_value = INT_MIN;
550 /* go over all proj's and collect them */
551 foreach_out_edge(irn, edge) {
552 proj = get_edge_src_irn(edge);
553 assert(is_Proj(proj) && "Only proj allowed at SwitchJmp");
555 pn = get_Proj_proj(proj);
557 /* create branch entry */
558 tbl.branches[i].target = proj;
559 tbl.branches[i].value = pn;
561 tbl.min_value = pn < tbl.min_value ? pn : tbl.min_value;
562 tbl.max_value = pn > tbl.max_value ? pn : tbl.max_value;
564 /* check for default proj */
565 if (pn == get_ia32_pncode(irn)) {
566 assert(tbl.defProj == NULL && "found two defProjs at SwitchJmp");
573 /* sort the branches by their number */
574 qsort(tbl.branches, tbl.num_branches, sizeof(tbl.branches[0]), ia32_cmp_branch_t);
576 /* two-complement's magic make this work without overflow */
577 interval = tbl.max_value - tbl.min_value;
579 /* check value interval */
580 if (interval > 16 * 1024) {
584 /* check ratio of value interval to number of branches */
585 if ((float)(interval + 1) / (float)tbl.num_branches > 8.0) {
591 if (tbl.min_value != 0) {
592 fprintf(F, "\tcmpl %lu, -%d", interval, tbl.min_value);
593 lc_efprintf(env, F, "(%1S)\t\t/* first switch value is not 0 */\n", irn);
596 fprintf(F, "\tcmpl %lu, ", interval);
597 lc_efprintf(env, F, "%1S\t\t\t/* compare for switch */\n", irn);
600 fprintf(F, "\tja %s\t\t\t/* default jump if out of range */\n", get_cfop_target(tbl.defProj, buf));
602 if (tbl.num_branches > 1) {
605 //fprintf(F, "\tjmp *%s", tbl.label);
606 lc_efprintf(env, F, "\tjmp *%s(,%1S,4)\t\t/* get jump table entry as target */\n", tbl.label, irn);
608 fprintf(F, "\t.section\t.rodata\t\t/* start jump table */\n");
609 fprintf(F, "\t.align 4\n");
611 fprintf(F, "%s:\n", tbl.label);
612 fprintf(F, "\t.long %s\t\t\t/* case %d */\n", get_cfop_target(tbl.branches[0].target, buf), tbl.branches[0].value);
614 last_value = tbl.branches[0].value;
615 for (i = 1; i < tbl.num_branches; ++i) {
616 while (++last_value < tbl.branches[i].value) {
617 fprintf(F, "\t.long %s\t\t/* default case */\n", get_cfop_target(tbl.defProj, buf));
619 fprintf(F, "\t.long %s\t\t\t/* case %d */\n", get_cfop_target(tbl.branches[i].target, buf), last_value);
622 fprintf(F, "\t.text\t\t\t\t/* end of jump table */\n");
625 /* one jump is enough */
626 fprintf(F, "\tjmp %s\t\t/* only one case given */\n", get_cfop_target(tbl.branches[0].target, buf));
629 else { // no jump table
630 for (i = 0; i < tbl.num_branches; ++i) {
631 fprintf(F, "\tcmpl %d, ", tbl.branches[i].value);
632 lc_efprintf(env, F, "%1S", irn);
633 fprintf(F, "\t\t\t/* case %d */\n", tbl.branches[i].value);
634 fprintf(F, "\tje %s\n", get_cfop_target(tbl.branches[i].target, buf));
637 fprintf(F, "\tjmp %s\t\t\t/* default case */\n", get_cfop_target(tbl.defProj, buf));
647 * Emits code for a unconditional jump.
649 void emit_Jmp(ir_node *irn, emit_env_t *env) {
652 char buf[SNPRINTF_BUF_LEN];
653 ir_fprintf(F, "\tjmp %s\t\t\t/* Jmp(%+F) */\n", get_cfop_target(irn, buf), get_irn_link(irn));
658 /****************************
661 * _ __ _ __ ___ _ ___
662 * | '_ \| '__/ _ \| |/ __|
663 * | |_) | | | (_) | |\__ \
664 * | .__/|_| \___/| ||___/
667 ****************************/
670 * Emits code for a proj -> node
672 void emit_Proj(ir_node *irn, emit_env_t *env) {
673 ir_node *pred = get_Proj_pred(irn);
675 if (get_irn_op(pred) == op_Start) {
676 switch(get_Proj_proj(irn)) {
677 case pn_Start_X_initial_exec:
686 /**********************************
689 * | | ___ _ __ _ _| |_) |
690 * | | / _ \| '_ \| | | | _ <
691 * | |___| (_) | |_) | |_| | |_) |
692 * \_____\___/| .__/ \__, |____/
695 **********************************/
697 static void emit_CopyB_prolog(FILE *F, int rem, int size) {
698 fprintf(F, "\t/* memcopy %d bytes*/\n", size);
699 fprintf(F, "\tcld\t\t\t\t/* copy direction forward*/\n");
703 fprintf(F, "\tmovsb\t\t\t\t/* memcopy remainder 1 */\n");
706 fprintf(F, "\tmovsw\t\t\t\t/* memcopy remainder 2 */\n");
709 fprintf(F, "\tmovsb\t\t\t\t/* memcopy remainder 3 */\n");
710 fprintf(F, "\tmovsw\t\t\t\t/* memcopy remainder 3 */\n");
715 void emit_ia32_CopyB(ir_node *irn, emit_env_t *emit_env) {
716 FILE *F = emit_env->out;
717 tarval *tv = get_ia32_Immop_tarval(irn);
718 int rem = get_tarval_long(tv);
719 int size = get_tarval_long(get_ia32_Immop_tarval(get_irn_n(irn, 2)));
721 emit_CopyB_prolog(F, rem, size);
723 fprintf(F, "\trep movsd\t\t\t\t/* memcopy */\n");
726 void emit_ia32_CopyB_i(ir_node *irn, emit_env_t *emit_env) {
727 tarval *tv = get_ia32_Immop_tarval(irn);
728 int size = get_tarval_long(tv);
729 FILE *F = emit_env->out;
731 emit_CopyB_prolog(F, size & 0x3, size);
735 fprintf(F, "\tmovsd\t\t\t\t/* memcopy unrolled */\n");
739 /********************
747 ********************/
749 void emit_ia32_Call(ir_node *irn, emit_env_t *emit_env) {
754 /***********************************************************************************
757 * _ __ ___ __ _ _ _ __ | |_ _ __ __ _ _ __ ___ _____ _____ _ __| | __
758 * | '_ ` _ \ / _` | | '_ \ | _| '__/ _` | '_ ` _ \ / _ \ \ /\ / / _ \| '__| |/ /
759 * | | | | | | (_| | | | | | | | | | | (_| | | | | | | __/\ V V / (_) | | | <
760 * |_| |_| |_|\__,_|_|_| |_| |_| |_| \__,_|_| |_| |_|\___| \_/\_/ \___/|_| |_|\_\
762 ***********************************************************************************/
765 * Emits code for a node.
767 void ia32_emit_node(ir_node *irn, void *env) {
768 emit_env_t *emit_env = env;
769 firm_dbg_module_t *mod = emit_env->mod;
770 FILE *F = emit_env->out;
772 DBG((mod, LEVEL_1, "emitting code for %+F\n", irn));
774 #define IA32_EMIT(a) if (is_ia32_##a(irn)) { emit_ia32_##a(irn, emit_env); return; }
775 #define EMIT(a) if (get_irn_opcode(irn) == iro_##a) { emit_##a(irn, emit_env); return; }
777 /* generated int emitter functions */
814 /* generated floating point emitter */
829 /* other emitter functions */
831 IA32_EMIT(SwitchJmp);
837 ir_fprintf(F, "\t\t\t\t\t/* %+F */\n", irn);
841 * Walks over the nodes in a block connected by scheduling edges
842 * and emits code for each node.
844 void ia32_gen_block(ir_node *block, void *env) {
847 if (! is_Block(block))
850 fprintf(((emit_env_t *)env)->out, "BLOCK_%ld:\n", get_irn_node_nr(block));
851 sched_foreach(block, irn) {
852 ia32_emit_node(irn, env);
858 * Emits code for function start.
860 void ia32_emit_start(FILE *F, ir_graph *irg) {
861 const char *irg_name = get_entity_name(get_irg_entity(irg));
863 fprintf(F, "\t.text\n");
864 fprintf(F, ".globl %s\n", irg_name);
865 fprintf(F, "\t.type\t%s, @function\n", irg_name);
866 fprintf(F, "%s:\n", irg_name);
870 * Emits code for function end
872 void ia32_emit_end(FILE *F, ir_graph *irg) {
873 const char *irg_name = get_entity_name(get_irg_entity(irg));
875 fprintf(F, "\tret\n");
876 fprintf(F, "\t.size\t%s, .-%s\n\n", irg_name, irg_name);
880 * Sets labels for control flow nodes (jump target)
881 * TODO: Jump optimization
883 void ia32_gen_labels(ir_node *block, void *env) {
885 int n = get_Block_n_cfgpreds(block);
887 for (n--; n >= 0; n--) {
888 pred = get_Block_cfgpred(block, n);
889 set_irn_link(pred, block);
896 void ia32_gen_routine(FILE *F, ir_graph *irg, const ia32_code_gen_t *cg) {
899 emit_env.mod = firm_dbg_register("ir.be.codegen.ia32");
901 emit_env.arch_env = cg->arch_env;
904 /* set the global arch_env (needed by print hooks) */
905 arch_env = cg->arch_env;
907 ia32_emit_start(F, irg);
908 irg_block_walk_graph(irg, ia32_gen_labels, NULL, &emit_env);
909 irg_walk_blkwise_graph(irg, NULL, ia32_gen_block, &emit_env);
910 ia32_emit_end(F, irg);