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 extern int obstack_printf(struct obstack *obst, char *fmt, ...);
34 #define SNPRINTF_BUF_LEN 128
36 static const arch_env_t *arch_env = NULL;
39 * Emits registers and/or address mode of a binary operation.
41 char *ia32_emit_binop(const ir_node *n) {
42 static char *buf = NULL;
45 buf = xcalloc(1, SNPRINTF_BUF_LEN);
48 memset(buf, 0, SNPRINTF_BUF_LEN);
51 switch(get_ia32_op_type(n)) {
53 if (get_ia32_cnst(n)) {
54 lc_esnprintf(ia32_get_arg_env(), buf, SNPRINTF_BUF_LEN, "%1D, %s", n, get_ia32_cnst(n));
57 lc_esnprintf(ia32_get_arg_env(), buf, SNPRINTF_BUF_LEN, "%1D, %4S", n, n);
61 lc_esnprintf(ia32_get_arg_env(), buf, SNPRINTF_BUF_LEN, "%1D, %s", n, ia32_emit_am(n));
64 lc_esnprintf(ia32_get_arg_env(), buf, SNPRINTF_BUF_LEN, "%s, %4S", ia32_emit_am(n), n);
67 assert(0 && "unsupported op type");
74 * Emits registers and/or address mode of a unary operation.
76 char *ia32_emit_unop(const ir_node *n) {
77 static char *buf = NULL;
80 buf = xcalloc(1, SNPRINTF_BUF_LEN);
83 memset(buf, 0, SNPRINTF_BUF_LEN);
86 switch(get_ia32_op_type(n)) {
88 lc_esnprintf(ia32_get_arg_env(), buf, SNPRINTF_BUF_LEN, "%1D", n);
91 snprintf(buf, SNPRINTF_BUF_LEN, ia32_emit_am(n));
94 assert(0 && "unsupported op type");
103 char *ia32_emit_am(const ir_node *n) {
104 ia32_am_flavour_t am_flav = get_ia32_am_flavour(n);
108 static struct obstack *obst = NULL;
111 obst = xcalloc(1, sizeof(*obst));
114 obstack_free(obst, NULL);
117 /* obstack_free with NULL results in an uninitialized obstack */
120 obstack_printf(obst, "[");
122 if (am_flav & ia32_B) {
123 lc_eoprintf(ia32_get_arg_env(), obst, "%1S", n);
127 if (am_flav & ia32_I) {
129 obstack_printf(obst, "+");
132 lc_eoprintf(ia32_get_arg_env(), obst, "%2S", n);
134 if (am_flav & ia32_S) {
135 obstack_printf(obst, "*%d", 1 << get_ia32_am_scale(n));
141 if (am_flav & ia32_O) {
142 obstack_printf(obst, get_ia32_am_offs(n));
145 obstack_printf(obst, "] ");
147 size = obstack_object_size(obst);
148 s = obstack_finish(obst);
154 /*************************************************************
156 * (_) | | / _| | | | |
157 * _ __ _ __ _ _ __ | |_| |_ | |__ ___| |_ __ ___ _ __
158 * | '_ \| '__| | '_ \| __| _| | '_ \ / _ \ | '_ \ / _ \ '__|
159 * | |_) | | | | | | | |_| | | | | | __/ | |_) | __/ |
160 * | .__/|_| |_|_| |_|\__|_| |_| |_|\___|_| .__/ \___|_|
163 *************************************************************/
165 /* We always pass the ir_node which is a pointer. */
166 static int ia32_get_arg_type(const lc_arg_occ_t *occ) {
167 return lc_arg_type_ptr;
172 * Returns the register at in position pos.
174 static const arch_register_t *get_in_reg(ir_node *irn, int pos) {
176 const arch_register_t *reg = NULL;
178 assert(get_irn_arity(irn) > pos && "Invalid IN position");
180 /* The out register of the operator at position pos is the
181 in register we need. */
182 op = get_irn_n(irn, pos);
184 reg = arch_get_irn_register(arch_env, op);
186 assert(reg && "no in register found");
191 * Returns the register at out position pos.
193 static const arch_register_t *get_out_reg(ir_node *irn, int pos) {
195 const arch_register_t *reg = NULL;
197 assert(get_irn_n_edges(irn) > pos && "Invalid OUT position");
199 /* 1st case: irn is not of mode_T, so it has only */
200 /* one OUT register -> good */
201 /* 2nd case: irn is of mode_T -> collect all Projs and ask the */
202 /* Proj with the corresponding projnum for the register */
204 if (get_irn_mode(irn) != mode_T) {
205 reg = arch_get_irn_register(arch_env, irn);
207 else if (is_ia32_irn(irn)) {
208 reg = get_ia32_out_reg(irn, pos);
211 const ir_edge_t *edge;
213 foreach_out_edge(irn, edge) {
214 proj = get_edge_src_irn(edge);
215 assert(is_Proj(proj) && "non-Proj from mode_T node");
216 if (get_Proj_proj(proj) == pos) {
217 reg = arch_get_irn_register(arch_env, proj);
223 assert(reg && "no out register found");
228 * Returns the number of the in register at position pos.
230 int get_ia32_reg_nr(ir_node *irn, int pos, int in_out) {
231 const arch_register_t *reg;
234 reg = get_in_reg(irn, pos);
237 reg = get_out_reg(irn, pos);
240 return arch_register_get_index(reg);
249 * Returns the name of the in register at position pos.
251 static const char *get_ia32_reg_name(ir_node *irn, int pos, enum io_direction in_out) {
252 const arch_register_t *reg;
254 if (in_out == IN_REG) {
255 reg = get_in_reg(irn, pos);
258 reg = get_out_reg(irn, pos);
261 return arch_register_get_name(reg);
265 * Get the register name for a node.
267 static int ia32_get_reg_name(lc_appendable_t *app,
268 const lc_arg_occ_t *occ, const lc_arg_value_t *arg)
271 ir_node *X = arg->v_ptr;
272 int nr = occ->width - 1;
275 return lc_arg_append(app, occ, "(null)", 6);
277 buf = get_ia32_reg_name(X, nr, occ->conversion == 'S' ? IN_REG : OUT_REG);
279 lc_appendable_chadd(app, '%');
280 return lc_arg_append(app, occ, buf, strlen(buf));
284 * Returns the tarval or offset of an ia32 as a string.
286 static int ia32_const_to_str(lc_appendable_t *app,
287 const lc_arg_occ_t *occ, const lc_arg_value_t *arg)
290 ir_node *X = arg->v_ptr;
293 return lc_arg_append(app, occ, "(null)", 6);
295 if (occ->conversion == 'C') {
296 buf = get_ia32_cnst(X);
299 buf = get_ia32_am_offs(X);
302 return lc_arg_append(app, occ, buf, strlen(buf));
306 * Determines the SSE suffix depending on the mode.
308 static int ia32_get_mode_suffix(lc_appendable_t *app,
309 const lc_arg_occ_t *occ, const lc_arg_value_t *arg)
311 ir_node *X = arg->v_ptr;
314 return lc_arg_append(app, occ, "(null)", 6);
316 if (get_mode_size_bits(get_irn_mode(X)) == 32)
317 return lc_appendable_chadd(app, 's');
319 return lc_appendable_chadd(app, 'd');
323 * Return the ia32 printf arg environment.
324 * We use the firm environment with some additional handlers.
326 const lc_arg_env_t *ia32_get_arg_env(void) {
327 static lc_arg_env_t *env = NULL;
329 static const lc_arg_handler_t ia32_reg_handler = { ia32_get_arg_type, ia32_get_reg_name };
330 static const lc_arg_handler_t ia32_const_handler = { ia32_get_arg_type, ia32_const_to_str };
331 static const lc_arg_handler_t ia32_mode_handler = { ia32_get_arg_type, ia32_get_mode_suffix };
334 /* extend the firm printer */
335 env = firm_get_arg_env();
338 lc_arg_register(env, "ia32:sreg", 'S', &ia32_reg_handler);
339 lc_arg_register(env, "ia32:dreg", 'D', &ia32_reg_handler);
340 lc_arg_register(env, "ia32:cnst", 'C', &ia32_const_handler);
341 lc_arg_register(env, "ia32:offs", 'O', &ia32_const_handler);
342 lc_arg_register(env, "ia32:mode", 'M', &ia32_mode_handler);
350 * Add a number to a prefix. This number will not be used a second time.
352 static char *get_unique_label(char *buf, size_t buflen, const char *prefix) {
353 static unsigned long id = 0;
354 snprintf(buf, buflen, "%s%lu", prefix, ++id);
359 /*************************************************
362 * ___ _ __ ___ _| |_ ___ ___ _ __ __| |
363 * / _ \ '_ ` _ \| | __| / __/ _ \| '_ \ / _` |
364 * | __/ | | | | | | |_ | (_| (_) | | | | (_| |
365 * \___|_| |_| |_|_|\__| \___\___/|_| |_|\__,_|
367 *************************************************/
370 * coding of conditions
372 struct cmp2conditon_t {
378 * positive conditions for signed compares
380 static const struct cmp2conditon_t cmp2condition_s[] = {
381 { NULL, pn_Cmp_False }, /* always false */
382 { "e", pn_Cmp_Eq }, /* == */
383 { "l", pn_Cmp_Lt }, /* < */
384 { "le", pn_Cmp_Le }, /* <= */
385 { "g", pn_Cmp_Gt }, /* > */
386 { "ge", pn_Cmp_Ge }, /* >= */
387 { "ne", pn_Cmp_Lg }, /* != */
388 { "ordered", pn_Cmp_Leg }, /* Floating point: ordered */
389 { "unordered", pn_Cmp_Uo }, /* FLoting point: unordered */
390 { "unordered or ==", pn_Cmp_Ue }, /* Floating point: unordered or == */
391 { "unordered or <", pn_Cmp_Ul }, /* Floating point: unordered or < */
392 { "unordered or <=", pn_Cmp_Ule }, /* Floating point: unordered or <= */
393 { "unordered or >", pn_Cmp_Ug }, /* Floating point: unordered or > */
394 { "unordered or >=", pn_Cmp_Uge }, /* Floating point: unordered or >= */
395 { "unordered or !=", pn_Cmp_Ne }, /* Floating point: unordered or != */
396 { NULL, pn_Cmp_True }, /* always true */
400 * positive conditions for unsigned compares
402 static const struct cmp2conditon_t cmp2condition_u[] = {
403 { NULL, pn_Cmp_False }, /* always false */
404 { "e", pn_Cmp_Eq }, /* == */
405 { "b", pn_Cmp_Lt }, /* < */
406 { "be", pn_Cmp_Le }, /* <= */
407 { "a", pn_Cmp_Gt }, /* > */
408 { "ae", pn_Cmp_Ge }, /* >= */
409 { "ne", pn_Cmp_Lg }, /* != */
410 { "ordered", pn_Cmp_Leg }, /* Floating point: ordered */
411 { "unordered", pn_Cmp_Uo }, /* FLoting point: unordered */
412 { "unordered or ==", pn_Cmp_Ue }, /* Floating point: unordered or == */
413 { "unordered or <", pn_Cmp_Ul }, /* Floating point: unordered or < */
414 { "unordered or <=", pn_Cmp_Ule }, /* Floating point: unordered or <= */
415 { "unordered or >", pn_Cmp_Ug }, /* Floating point: unordered or > */
416 { "unordered or >=", pn_Cmp_Uge }, /* Floating point: unordered or >= */
417 { "unordered or !=", pn_Cmp_Ne }, /* Floating point: unordered or != */
418 { NULL, pn_Cmp_True }, /* always true */
422 * returns the condition code
424 static const char *get_cmp_suffix(int cmp_code, int unsigned_cmp)
426 assert(cmp2condition_s[cmp_code].num == cmp_code);
427 assert(cmp2condition_u[cmp_code].num == cmp_code);
429 return unsigned_cmp ? cmp2condition_u[cmp_code & 7].name : cmp2condition_s[cmp_code & 7].name;
433 * Returns the target label for a control flow node.
435 static char *get_cfop_target(const ir_node *irn, char *buf) {
436 ir_node *bl = get_irn_link(irn);
438 snprintf(buf, SNPRINTF_BUF_LEN, "BLOCK_%ld", get_irn_node_nr(bl));
443 * Emits the jump sequence for a conditional jump (cmp + jmp_true + jmp_false)
445 static void finish_CondJmp(FILE *F, ir_node *irn) {
447 const ir_edge_t *edge;
448 char buf[SNPRINTF_BUF_LEN];
450 edge = get_irn_out_edge_first(irn);
451 proj = get_edge_src_irn(edge);
452 assert(is_Proj(proj) && "CondJmp with a non-Proj");
454 if (get_Proj_proj(proj) == 1) {
455 fprintf(F, "\tj%s %s\t\t\t/* cmp(a, b) == TRUE */\n",
456 get_cmp_suffix(get_ia32_pncode(irn), !mode_is_signed(get_irn_mode(get_irn_n(irn, 0)))),
457 get_cfop_target(proj, buf));
460 fprintf(F, "\tjn%s %s\t\t\t/* cmp(a, b) == FALSE */\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 edge = get_irn_out_edge_next(irn, edge);
467 proj = get_edge_src_irn(edge);
468 assert(is_Proj(proj) && "CondJmp with a non-Proj");
469 fprintf(F, "\tjmp %s\t\t\t/* otherwise */\n", get_cfop_target(proj, buf));
474 * Emits code for conditional jump with two variables.
476 static void emit_ia32_CondJmp(ir_node *irn, emit_env_t *env) {
479 lc_efprintf(ia32_get_arg_env(), F, "\tcmp %2S, %1S\t\t\t/* CondJmp(%+F, %+F) */\n", irn, irn,
480 get_irn_n(irn, 0), get_irn_n(irn, 1));
481 finish_CondJmp(F, irn);
485 * Emits code for conditional jump with immediate.
487 void emit_ia32_CondJmp_i(ir_node *irn, emit_env_t *env) {
490 lc_efprintf(ia32_get_arg_env(), F, "\tcmp %C, %1S\t\t\t/* CondJmp_i(%+F) */\n", irn, irn, get_irn_n(irn, 0));
491 finish_CondJmp(F, irn);
496 /*********************************************************
499 * ___ _ __ ___ _| |_ _ _ _ _ __ ___ _ __ ___
500 * / _ \ '_ ` _ \| | __| | | | | | '_ ` _ \| '_ \/ __|
501 * | __/ | | | | | | |_ | | |_| | | | | | | |_) \__ \
502 * \___|_| |_| |_|_|\__| | |\__,_|_| |_| |_| .__/|___/
505 *********************************************************/
507 /* jump table entry (target and corresponding number) */
508 typedef struct _branch_t {
513 /* jump table for switch generation */
514 typedef struct _jmp_tbl_t {
515 ir_node *defProj; /**< default target */
516 int min_value; /**< smallest switch case */
517 int max_value; /**< largest switch case */
518 int num_branches; /**< number of jumps */
519 char *label; /**< label of the jump table */
520 branch_t *branches; /**< jump array */
524 * Compare two variables of type branch_t. Used to sort all switch cases
526 static int ia32_cmp_branch_t(const void *a, const void *b) {
527 branch_t *b1 = (branch_t *)a;
528 branch_t *b2 = (branch_t *)b;
530 if (b1->value <= b2->value)
537 * Emits code for a SwitchJmp (creates a jump table if
538 * possible otherwise a cmp-jmp cascade). Port from
541 void emit_ia32_SwitchJmp(const ir_node *irn, emit_env_t *emit_env) {
542 unsigned long interval;
543 char buf[SNPRINTF_BUF_LEN];
544 int last_value, i, pn, do_jmp_tbl = 1;
547 const ir_edge_t *edge;
548 const lc_arg_env_t *env = ia32_get_arg_env();
549 FILE *F = emit_env->out;
551 /* fill the table structure */
552 tbl.label = xmalloc(SNPRINTF_BUF_LEN);
553 tbl.label = get_unique_label(tbl.label, SNPRINTF_BUF_LEN, "JMPTBL_");
555 tbl.num_branches = get_irn_n_edges(irn);
556 tbl.branches = xcalloc(tbl.num_branches, sizeof(tbl.branches[0]));
557 tbl.min_value = INT_MAX;
558 tbl.max_value = INT_MIN;
561 /* go over all proj's and collect them */
562 foreach_out_edge(irn, edge) {
563 proj = get_edge_src_irn(edge);
564 assert(is_Proj(proj) && "Only proj allowed at SwitchJmp");
566 pn = get_Proj_proj(proj);
568 /* create branch entry */
569 tbl.branches[i].target = proj;
570 tbl.branches[i].value = pn;
572 tbl.min_value = pn < tbl.min_value ? pn : tbl.min_value;
573 tbl.max_value = pn > tbl.max_value ? pn : tbl.max_value;
575 /* check for default proj */
576 if (pn == get_ia32_pncode(irn)) {
577 assert(tbl.defProj == NULL && "found two defProjs at SwitchJmp");
584 /* sort the branches by their number */
585 qsort(tbl.branches, tbl.num_branches, sizeof(tbl.branches[0]), ia32_cmp_branch_t);
587 /* two-complement's magic make this work without overflow */
588 interval = tbl.max_value - tbl.min_value;
590 /* check value interval */
591 if (interval > 16 * 1024) {
595 /* check ratio of value interval to number of branches */
596 if ((float)(interval + 1) / (float)tbl.num_branches > 8.0) {
602 if (tbl.min_value != 0) {
603 fprintf(F, "\tcmpl %lu, -%d", interval, tbl.min_value);
604 lc_efprintf(env, F, "(%1S)\t\t/* first switch value is not 0 */\n", irn);
607 fprintf(F, "\tcmpl %lu, ", interval);
608 lc_efprintf(env, F, "%1S\t\t\t/* compare for switch */\n", irn);
611 fprintf(F, "\tja %s\t\t\t/* default jump if out of range */\n", get_cfop_target(tbl.defProj, buf));
613 if (tbl.num_branches > 1) {
616 //fprintf(F, "\tjmp *%s", tbl.label);
617 lc_efprintf(env, F, "\tjmp *%s(,%1S,4)\t\t/* get jump table entry as target */\n", tbl.label, irn);
619 fprintf(F, "\t.section\t.rodata\t\t/* start jump table */\n");
620 fprintf(F, "\t.align 4\n");
622 fprintf(F, "%s:\n", tbl.label);
623 fprintf(F, "\t.long %s\t\t\t/* case %d */\n", get_cfop_target(tbl.branches[0].target, buf), tbl.branches[0].value);
625 last_value = tbl.branches[0].value;
626 for (i = 1; i < tbl.num_branches; ++i) {
627 while (++last_value < tbl.branches[i].value) {
628 fprintf(F, "\t.long %s\t\t/* default case */\n", get_cfop_target(tbl.defProj, buf));
630 fprintf(F, "\t.long %s\t\t\t/* case %d */\n", get_cfop_target(tbl.branches[i].target, buf), last_value);
633 fprintf(F, "\t.text\t\t\t\t/* end of jump table */\n");
636 /* one jump is enough */
637 fprintf(F, "\tjmp %s\t\t/* only one case given */\n", get_cfop_target(tbl.branches[0].target, buf));
640 else { // no jump table
641 for (i = 0; i < tbl.num_branches; ++i) {
642 fprintf(F, "\tcmpl %d, ", tbl.branches[i].value);
643 lc_efprintf(env, F, "%1S", irn);
644 fprintf(F, "\t\t\t/* case %d */\n", tbl.branches[i].value);
645 fprintf(F, "\tje %s\n", get_cfop_target(tbl.branches[i].target, buf));
648 fprintf(F, "\tjmp %s\t\t\t/* default case */\n", get_cfop_target(tbl.defProj, buf));
658 * Emits code for a unconditional jump.
660 void emit_Jmp(ir_node *irn, emit_env_t *env) {
663 char buf[SNPRINTF_BUF_LEN];
664 ir_fprintf(F, "\tjmp %s\t\t\t/* Jmp(%+F) */\n", get_cfop_target(irn, buf), get_irn_link(irn));
669 /****************************
672 * _ __ _ __ ___ _ ___
673 * | '_ \| '__/ _ \| |/ __|
674 * | |_) | | | (_) | |\__ \
675 * | .__/|_| \___/| ||___/
678 ****************************/
681 * Emits code for a proj -> node
683 void emit_Proj(ir_node *irn, emit_env_t *env) {
684 ir_node *pred = get_Proj_pred(irn);
686 if (get_irn_op(pred) == op_Start) {
687 switch(get_Proj_proj(irn)) {
688 case pn_Start_X_initial_exec:
697 /**********************************
700 * | | ___ _ __ _ _| |_) |
701 * | | / _ \| '_ \| | | | _ <
702 * | |___| (_) | |_) | |_| | |_) |
703 * \_____\___/| .__/ \__, |____/
706 **********************************/
708 static void emit_CopyB_prolog(FILE *F, int rem, int size) {
709 fprintf(F, "\t/* memcopy %d bytes*/\n", size);
710 fprintf(F, "\tcld\t\t\t\t/* copy direction forward*/\n");
714 fprintf(F, "\tmovsb\t\t\t\t/* memcopy remainder 1 */\n");
717 fprintf(F, "\tmovsw\t\t\t\t/* memcopy remainder 2 */\n");
720 fprintf(F, "\tmovsb\t\t\t\t/* memcopy remainder 3 */\n");
721 fprintf(F, "\tmovsw\t\t\t\t/* memcopy remainder 3 */\n");
726 void emit_ia32_CopyB(ir_node *irn, emit_env_t *emit_env) {
727 FILE *F = emit_env->out;
728 tarval *tv = get_ia32_Immop_tarval(irn);
729 int rem = get_tarval_long(tv);
730 int size = get_tarval_long(get_ia32_Immop_tarval(get_irn_n(irn, 2)));
732 emit_CopyB_prolog(F, rem, size);
734 fprintf(F, "\trep movsd\t\t\t\t/* memcopy */\n");
737 void emit_ia32_CopyB_i(ir_node *irn, emit_env_t *emit_env) {
738 tarval *tv = get_ia32_Immop_tarval(irn);
739 int size = get_tarval_long(tv);
740 FILE *F = emit_env->out;
742 emit_CopyB_prolog(F, size & 0x3, size);
746 fprintf(F, "\tmovsd\t\t\t\t/* memcopy unrolled */\n");
750 /********************
758 ********************/
760 void emit_ia32_Call(ir_node *irn, emit_env_t *emit_env) {
765 /***********************************************************************************
768 * _ __ ___ __ _ _ _ __ | |_ _ __ __ _ _ __ ___ _____ _____ _ __| | __
769 * | '_ ` _ \ / _` | | '_ \ | _| '__/ _` | '_ ` _ \ / _ \ \ /\ / / _ \| '__| |/ /
770 * | | | | | | (_| | | | | | | | | | | (_| | | | | | | __/\ V V / (_) | | | <
771 * |_| |_| |_|\__,_|_|_| |_| |_| |_| \__,_|_| |_| |_|\___| \_/\_/ \___/|_| |_|\_\
773 ***********************************************************************************/
776 * Emits code for a node.
778 void ia32_emit_node(ir_node *irn, void *env) {
779 emit_env_t *emit_env = env;
780 firm_dbg_module_t *mod = emit_env->mod;
781 FILE *F = emit_env->out;
783 DBG((mod, LEVEL_1, "emitting code for %+F\n", irn));
785 #define IA32_EMIT(a) if (is_ia32_##a(irn)) { emit_ia32_##a(irn, emit_env); return; }
786 #define EMIT(a) if (get_irn_opcode(irn) == iro_##a) { emit_##a(irn, emit_env); return; }
788 /* generated int emitter functions */
825 /* generated floating point emitter */
840 /* other emitter functions */
842 IA32_EMIT(SwitchJmp);
848 ir_fprintf(F, "\t\t\t\t\t/* %+F */\n", irn);
852 * Walks over the nodes in a block connected by scheduling edges
853 * and emits code for each node.
855 void ia32_gen_block(ir_node *block, void *env) {
858 if (! is_Block(block))
861 fprintf(((emit_env_t *)env)->out, "BLOCK_%ld:\n", get_irn_node_nr(block));
862 sched_foreach(block, irn) {
863 ia32_emit_node(irn, env);
869 * Emits code for function start.
871 void ia32_emit_start(FILE *F, ir_graph *irg) {
872 const char *irg_name = get_entity_name(get_irg_entity(irg));
874 fprintf(F, "\t.text\n");
875 fprintf(F, ".globl %s\n", irg_name);
876 fprintf(F, "\t.type\t%s, @function\n", irg_name);
877 fprintf(F, "%s:\n", irg_name);
881 * Emits code for function end
883 void ia32_emit_end(FILE *F, ir_graph *irg) {
884 const char *irg_name = get_entity_name(get_irg_entity(irg));
886 fprintf(F, "\tret\n");
887 fprintf(F, "\t.size\t%s, .-%s\n\n", irg_name, irg_name);
891 * Sets labels for control flow nodes (jump target)
892 * TODO: Jump optimization
894 void ia32_gen_labels(ir_node *block, void *env) {
896 int n = get_Block_n_cfgpreds(block);
898 for (n--; n >= 0; n--) {
899 pred = get_Block_cfgpred(block, n);
900 set_irn_link(pred, block);
907 void ia32_gen_routine(FILE *F, ir_graph *irg, const ia32_code_gen_t *cg) {
910 emit_env.mod = firm_dbg_register("ir.be.codegen.ia32");
912 emit_env.arch_env = cg->arch_env;
915 /* set the global arch_env (needed by print hooks) */
916 arch_env = cg->arch_env;
918 ia32_emit_start(F, irg);
919 irg_block_walk_graph(irg, ia32_gen_labels, NULL, &emit_env);
920 irg_walk_blkwise_graph(irg, NULL, ia32_gen_block, &emit_env);
921 ia32_emit_end(F, irg);