17 #include "../besched.h"
19 #include "ia32_emitter.h"
20 #include "gen_ia32_emitter.h"
21 #include "ia32_nodes_attr.h"
22 #include "ia32_new_nodes.h"
23 #include "ia32_map_regs.h"
25 #ifdef obstack_chunk_alloc
26 # undef obstack_chunk_alloc
27 # define obstack_chunk_alloc xmalloc
29 # define obstack_chunk_alloc xmalloc
30 # define obstack_chunk_free free
33 extern int obstack_printf(struct obstack *obst, char *fmt, ...);
35 #define SNPRINTF_BUF_LEN 128
37 static const arch_env_t *arch_env = NULL;
40 * Emits registers and/or address mode of a binary operation.
42 char *ia32_emit_binop(const ir_node *n) {
43 static char *buf = NULL;
46 buf = xcalloc(1, SNPRINTF_BUF_LEN);
49 memset(buf, 0, SNPRINTF_BUF_LEN);
52 switch(get_ia32_op_type(n)) {
54 if (get_ia32_cnst(n)) {
55 lc_esnprintf(ia32_get_arg_env(), buf, SNPRINTF_BUF_LEN, "%1D, %s", n, get_ia32_cnst(n));
58 lc_esnprintf(ia32_get_arg_env(), buf, SNPRINTF_BUF_LEN, "%1D, %4S", n, n);
62 lc_esnprintf(ia32_get_arg_env(), buf, SNPRINTF_BUF_LEN, "%1D, %s", n, ia32_emit_am(n));
65 lc_esnprintf(ia32_get_arg_env(), buf, SNPRINTF_BUF_LEN, "%s, %4S", ia32_emit_am(n), n);
68 assert(0 && "unsupported op type");
75 * Emits registers and/or address mode of a unary operation.
77 char *ia32_emit_unop(const ir_node *n) {
78 static char *buf = NULL;
81 buf = xcalloc(1, SNPRINTF_BUF_LEN);
84 memset(buf, 0, SNPRINTF_BUF_LEN);
87 switch(get_ia32_op_type(n)) {
89 lc_esnprintf(ia32_get_arg_env(), buf, SNPRINTF_BUF_LEN, "%1D", n);
92 snprintf(buf, SNPRINTF_BUF_LEN, ia32_emit_am(n));
95 assert(0 && "unsupported op type");
104 char *ia32_emit_am(const ir_node *n) {
105 ia32_am_flavour_t am_flav = get_ia32_am_flavour(n);
109 static struct obstack *obst = NULL;
112 obst = xcalloc(1, sizeof(*obst));
115 obstack_free(obst, NULL);
118 /* obstack_free with NULL results in an uninitialized obstack */
121 obstack_printf(obst, "[");
123 if (am_flav & ia32_B) {
124 lc_eoprintf(ia32_get_arg_env(), obst, "%1S", n);
128 if (am_flav & ia32_I) {
130 obstack_printf(obst, "+");
133 lc_eoprintf(ia32_get_arg_env(), obst, "%2S", n);
135 if (am_flav & ia32_S) {
136 obstack_printf(obst, "*%d", 1 << get_ia32_am_scale(n));
142 if (am_flav & ia32_O) {
143 obstack_printf(obst, get_ia32_am_offs(n));
146 obstack_printf(obst, "] ");
148 size = obstack_object_size(obst);
149 s = obstack_finish(obst);
155 /*************************************************************
157 * (_) | | / _| | | | |
158 * _ __ _ __ _ _ __ | |_| |_ | |__ ___| |_ __ ___ _ __
159 * | '_ \| '__| | '_ \| __| _| | '_ \ / _ \ | '_ \ / _ \ '__|
160 * | |_) | | | | | | | |_| | | | | | __/ | |_) | __/ |
161 * | .__/|_| |_|_| |_|\__|_| |_| |_|\___|_| .__/ \___|_|
164 *************************************************************/
166 /* We always pass the ir_node which is a pointer. */
167 static int ia32_get_arg_type(const lc_arg_occ_t *occ) {
168 return lc_arg_type_ptr;
173 * Returns the register at in position pos.
175 static const arch_register_t *get_in_reg(ir_node *irn, int pos) {
177 const arch_register_t *reg = NULL;
179 assert(get_irn_arity(irn) > pos && "Invalid IN position");
181 /* The out register of the operator at position pos is the
182 in register we need. */
183 op = get_irn_n(irn, pos);
185 reg = arch_get_irn_register(arch_env, op);
187 assert(reg && "no in register found");
192 * Returns the register at out position pos.
194 static const arch_register_t *get_out_reg(ir_node *irn, int pos) {
196 const arch_register_t *reg = NULL;
198 assert(get_irn_n_edges(irn) > pos && "Invalid OUT position");
200 /* 1st case: irn is not of mode_T, so it has only */
201 /* one OUT register -> good */
202 /* 2nd case: irn is of mode_T -> collect all Projs and ask the */
203 /* Proj with the corresponding projnum for the register */
205 if (get_irn_mode(irn) != mode_T) {
206 reg = arch_get_irn_register(arch_env, irn);
208 else if (is_ia32_irn(irn)) {
209 reg = get_ia32_out_reg(irn, pos);
212 const ir_edge_t *edge;
214 foreach_out_edge(irn, edge) {
215 proj = get_edge_src_irn(edge);
216 assert(is_Proj(proj) && "non-Proj from mode_T node");
217 if (get_Proj_proj(proj) == pos) {
218 reg = arch_get_irn_register(arch_env, proj);
224 assert(reg && "no out register found");
229 * Returns the number of the in register at position pos.
231 int get_ia32_reg_nr(ir_node *irn, int pos, int in_out) {
232 const arch_register_t *reg;
235 reg = get_in_reg(irn, pos);
238 reg = get_out_reg(irn, pos);
241 return arch_register_get_index(reg);
250 * Returns the name of the in register at position pos.
252 static const char *get_ia32_reg_name(ir_node *irn, int pos, enum io_direction in_out) {
253 const arch_register_t *reg;
255 if (in_out == IN_REG) {
256 reg = get_in_reg(irn, pos);
259 reg = get_out_reg(irn, pos);
262 return arch_register_get_name(reg);
266 * Get the register name for a node.
268 static int ia32_get_reg_name(lc_appendable_t *app,
269 const lc_arg_occ_t *occ, const lc_arg_value_t *arg)
272 ir_node *X = arg->v_ptr;
273 int nr = occ->width - 1;
276 return lc_arg_append(app, occ, "(null)", 6);
278 buf = get_ia32_reg_name(X, nr, occ->conversion == 'S' ? IN_REG : OUT_REG);
280 lc_appendable_chadd(app, '%');
281 return lc_arg_append(app, occ, buf, strlen(buf));
285 * Returns the tarval, offset or scale of an ia32 as a string.
287 static int ia32_const_to_str(lc_appendable_t *app,
288 const lc_arg_occ_t *occ, const lc_arg_value_t *arg)
291 ir_node *X = arg->v_ptr;
294 return lc_arg_append(app, occ, "(null)", 6);
296 if (occ->conversion == 'C') {
297 buf = get_ia32_cnst(X);
300 buf = get_ia32_am_offs(X);
303 return lc_arg_append(app, occ, buf, strlen(buf));
307 * Determines the SSE suffix depending on the mode.
309 static int ia32_get_mode_suffix(lc_appendable_t *app,
310 const lc_arg_occ_t *occ, const lc_arg_value_t *arg)
312 ir_node *X = arg->v_ptr;
315 return lc_arg_append(app, occ, "(null)", 6);
317 if (get_mode_size_bits(get_irn_mode(X)) == 32)
318 return lc_appendable_chadd(app, 's');
320 return lc_appendable_chadd(app, 'd');
324 * Return the ia32 printf arg environment.
325 * We use the firm environment with some additional handlers.
327 const lc_arg_env_t *ia32_get_arg_env(void) {
328 static lc_arg_env_t *env = NULL;
330 static const lc_arg_handler_t ia32_reg_handler = { ia32_get_arg_type, ia32_get_reg_name };
331 static const lc_arg_handler_t ia32_const_handler = { ia32_get_arg_type, ia32_const_to_str };
332 static const lc_arg_handler_t ia32_mode_handler = { ia32_get_arg_type, ia32_get_mode_suffix };
335 /* extend the firm printer */
336 env = firm_get_arg_env();
339 lc_arg_register(env, "ia32:sreg", 'S', &ia32_reg_handler);
340 lc_arg_register(env, "ia32:dreg", 'D', &ia32_reg_handler);
341 lc_arg_register(env, "ia32:cnst", 'C', &ia32_const_handler);
342 lc_arg_register(env, "ia32:offs", 'O', &ia32_const_handler);
343 lc_arg_register(env, "ia32:mode", 'M', &ia32_mode_handler);
351 * Add a number to a prefix. This number will not be used a second time.
353 static char *get_unique_label(char *buf, size_t buflen, const char *prefix) {
354 static unsigned long id = 0;
355 snprintf(buf, buflen, "%s%lu", prefix, ++id);
360 /*************************************************
363 * ___ _ __ ___ _| |_ ___ ___ _ __ __| |
364 * / _ \ '_ ` _ \| | __| / __/ _ \| '_ \ / _` |
365 * | __/ | | | | | | |_ | (_| (_) | | | | (_| |
366 * \___|_| |_| |_|_|\__| \___\___/|_| |_|\__,_|
368 *************************************************/
371 * coding of conditions
373 struct cmp2conditon_t {
379 * positive conditions for signed compares
381 static const struct cmp2conditon_t cmp2condition_s[] = {
382 { NULL, pn_Cmp_False }, /* always false */
383 { "e", pn_Cmp_Eq }, /* == */
384 { "l", pn_Cmp_Lt }, /* < */
385 { "le", pn_Cmp_Le }, /* <= */
386 { "g", pn_Cmp_Gt }, /* > */
387 { "ge", pn_Cmp_Ge }, /* >= */
388 { "ne", pn_Cmp_Lg }, /* != */
389 { "ordered", pn_Cmp_Leg }, /* Floating point: ordered */
390 { "unordered", pn_Cmp_Uo }, /* FLoting point: unordered */
391 { "unordered or ==", pn_Cmp_Ue }, /* Floating point: unordered or == */
392 { "unordered or <", pn_Cmp_Ul }, /* Floating point: unordered or < */
393 { "unordered or <=", pn_Cmp_Ule }, /* Floating point: unordered or <= */
394 { "unordered or >", pn_Cmp_Ug }, /* Floating point: unordered or > */
395 { "unordered or >=", pn_Cmp_Uge }, /* Floating point: unordered or >= */
396 { "unordered or !=", pn_Cmp_Ne }, /* Floating point: unordered or != */
397 { NULL, pn_Cmp_True }, /* always true */
401 * positive conditions for unsigned compares
403 static const struct cmp2conditon_t cmp2condition_u[] = {
404 { NULL, pn_Cmp_False }, /* always false */
405 { "e", pn_Cmp_Eq }, /* == */
406 { "b", pn_Cmp_Lt }, /* < */
407 { "be", pn_Cmp_Le }, /* <= */
408 { "a", pn_Cmp_Gt }, /* > */
409 { "ae", pn_Cmp_Ge }, /* >= */
410 { "ne", pn_Cmp_Lg }, /* != */
411 { "ordered", pn_Cmp_Leg }, /* Floating point: ordered */
412 { "unordered", pn_Cmp_Uo }, /* FLoting point: unordered */
413 { "unordered or ==", pn_Cmp_Ue }, /* Floating point: unordered or == */
414 { "unordered or <", pn_Cmp_Ul }, /* Floating point: unordered or < */
415 { "unordered or <=", pn_Cmp_Ule }, /* Floating point: unordered or <= */
416 { "unordered or >", pn_Cmp_Ug }, /* Floating point: unordered or > */
417 { "unordered or >=", pn_Cmp_Uge }, /* Floating point: unordered or >= */
418 { "unordered or !=", pn_Cmp_Ne }, /* Floating point: unordered or != */
419 { NULL, pn_Cmp_True }, /* always true */
423 * returns the condition code
425 static const char *get_cmp_suffix(int cmp_code, int unsigned_cmp)
427 assert(cmp2condition_s[cmp_code].num == cmp_code);
428 assert(cmp2condition_u[cmp_code].num == cmp_code);
430 return unsigned_cmp ? cmp2condition_u[cmp_code & 7].name : cmp2condition_s[cmp_code & 7].name;
434 * Returns the target label for a control flow node.
436 static char *get_cfop_target(const ir_node *irn, char *buf) {
437 ir_node *bl = get_irn_link(irn);
439 snprintf(buf, SNPRINTF_BUF_LEN, "BLOCK_%ld", get_irn_node_nr(bl));
444 * Emits the jump sequence for a conditional jump (cmp + jmp_true + jmp_false)
446 static void finish_CondJmp(FILE *F, ir_node *irn) {
448 const ir_edge_t *edge;
449 char buf[SNPRINTF_BUF_LEN];
451 edge = get_irn_out_edge_first(irn);
452 proj = get_edge_src_irn(edge);
453 assert(is_Proj(proj) && "CondJmp with a non-Proj");
455 if (get_Proj_proj(proj) == 1) {
456 fprintf(F, "\tj%s %s\t\t\t/* cmp(a, b) == TRUE */\n",
457 get_cmp_suffix(get_ia32_pncode(irn), !mode_is_signed(get_irn_mode(get_irn_n(irn, 0)))),
458 get_cfop_target(proj, buf));
461 fprintf(F, "\tjn%s %s\t\t\t/* cmp(a, b) == FALSE */\n",
462 get_cmp_suffix(get_ia32_pncode(irn), !mode_is_signed(get_irn_mode(get_irn_n(irn, 0)))),
463 get_cfop_target(proj, buf));
466 edge = get_irn_out_edge_next(irn, edge);
468 proj = get_edge_src_irn(edge);
469 assert(is_Proj(proj) && "CondJmp with a non-Proj");
470 fprintf(F, "\tjmp %s\t\t\t/* otherwise */\n", get_cfop_target(proj, buf));
475 * Emits code for conditional jump with two variables.
477 static void emit_ia32_CondJmp(ir_node *irn, emit_env_t *env) {
480 lc_efprintf(ia32_get_arg_env(), F, "\tcmp %2S, %1S\t\t\t/* CondJmp(%+F, %+F) */\n", irn, irn,
481 get_irn_n(irn, 0), get_irn_n(irn, 1));
482 finish_CondJmp(F, irn);
486 * Emits code for conditional jump with immediate.
488 void emit_ia32_CondJmp_i(ir_node *irn, emit_env_t *env) {
491 lc_efprintf(ia32_get_arg_env(), F, "\tcmp %C, %1S\t\t\t/* CondJmp_i(%+F) */\n", irn, irn, get_irn_n(irn, 0));
492 finish_CondJmp(F, irn);
497 /*********************************************************
500 * ___ _ __ ___ _| |_ _ _ _ _ __ ___ _ __ ___
501 * / _ \ '_ ` _ \| | __| | | | | | '_ ` _ \| '_ \/ __|
502 * | __/ | | | | | | |_ | | |_| | | | | | | |_) \__ \
503 * \___|_| |_| |_|_|\__| | |\__,_|_| |_| |_| .__/|___/
506 *********************************************************/
508 /* jump table entry (target and corresponding number) */
509 typedef struct _branch_t {
514 /* jump table for switch generation */
515 typedef struct _jmp_tbl_t {
516 ir_node *defProj; /**< default target */
517 int min_value; /**< smallest switch case */
518 int max_value; /**< largest switch case */
519 int num_branches; /**< number of jumps */
520 char *label; /**< label of the jump table */
521 branch_t *branches; /**< jump array */
525 * Compare two variables of type branch_t. Used to sort all switch cases
527 static int ia32_cmp_branch_t(const void *a, const void *b) {
528 branch_t *b1 = (branch_t *)a;
529 branch_t *b2 = (branch_t *)b;
531 if (b1->value <= b2->value)
538 * Emits code for a SwitchJmp (creates a jump table if
539 * possible otherwise a cmp-jmp cascade). Port from
542 void emit_ia32_SwitchJmp(const ir_node *irn, emit_env_t *emit_env) {
543 unsigned long interval;
544 char buf[SNPRINTF_BUF_LEN];
545 int last_value, i, pn, do_jmp_tbl = 1;
548 const ir_edge_t *edge;
549 const lc_arg_env_t *env = ia32_get_arg_env();
550 FILE *F = emit_env->out;
552 /* fill the table structure */
553 tbl.label = xmalloc(SNPRINTF_BUF_LEN);
554 tbl.label = get_unique_label(tbl.label, SNPRINTF_BUF_LEN, "JMPTBL_");
556 tbl.num_branches = get_irn_n_edges(irn);
557 tbl.branches = xcalloc(tbl.num_branches, sizeof(tbl.branches[0]));
558 tbl.min_value = INT_MAX;
559 tbl.max_value = INT_MIN;
562 /* go over all proj's and collect them */
563 foreach_out_edge(irn, edge) {
564 proj = get_edge_src_irn(edge);
565 assert(is_Proj(proj) && "Only proj allowed at SwitchJmp");
567 pn = get_Proj_proj(proj);
569 /* create branch entry */
570 tbl.branches[i].target = proj;
571 tbl.branches[i].value = pn;
573 tbl.min_value = pn < tbl.min_value ? pn : tbl.min_value;
574 tbl.max_value = pn > tbl.max_value ? pn : tbl.max_value;
576 /* check for default proj */
577 if (pn == get_ia32_pncode(irn)) {
578 assert(tbl.defProj == NULL && "found two defProjs at SwitchJmp");
585 /* sort the branches by their number */
586 qsort(tbl.branches, tbl.num_branches, sizeof(tbl.branches[0]), ia32_cmp_branch_t);
588 /* two-complement's magic make this work without overflow */
589 interval = tbl.max_value - tbl.min_value;
591 /* check value interval */
592 if (interval > 16 * 1024) {
596 /* check ratio of value interval to number of branches */
597 if ((float)(interval + 1) / (float)tbl.num_branches > 8.0) {
603 if (tbl.min_value != 0) {
604 fprintf(F, "\tcmpl %lu, -%d", interval, tbl.min_value);
605 lc_efprintf(env, F, "(%1S)\t\t/* first switch value is not 0 */\n", irn);
608 fprintf(F, "\tcmpl %lu, ", interval);
609 lc_efprintf(env, F, "%1S\t\t\t/* compare for switch */\n", irn);
612 fprintf(F, "\tja %s\t\t\t/* default jump if out of range */\n", get_cfop_target(tbl.defProj, buf));
614 if (tbl.num_branches > 1) {
617 //fprintf(F, "\tjmp *%s", tbl.label);
618 lc_efprintf(env, F, "\tjmp *%s(,%1S,4)\t\t/* get jump table entry as target */\n", tbl.label, irn);
620 fprintf(F, "\t.section\t.rodata\t\t/* start jump table */\n");
621 fprintf(F, "\t.align 4\n");
623 fprintf(F, "%s:\n", tbl.label);
624 fprintf(F, "\t.long %s\t\t\t/* case %d */\n", get_cfop_target(tbl.branches[0].target, buf), tbl.branches[0].value);
626 last_value = tbl.branches[0].value;
627 for (i = 1; i < tbl.num_branches; ++i) {
628 while (++last_value < tbl.branches[i].value) {
629 fprintf(F, "\t.long %s\t\t/* default case */\n", get_cfop_target(tbl.defProj, buf));
631 fprintf(F, "\t.long %s\t\t\t/* case %d */\n", get_cfop_target(tbl.branches[i].target, buf), last_value);
634 fprintf(F, "\t.text\t\t\t\t/* end of jump table */\n");
637 /* one jump is enough */
638 fprintf(F, "\tjmp %s\t\t/* only one case given */\n", get_cfop_target(tbl.branches[0].target, buf));
641 else { // no jump table
642 for (i = 0; i < tbl.num_branches; ++i) {
643 fprintf(F, "\tcmpl %d, ", tbl.branches[i].value);
644 lc_efprintf(env, F, "%1S", irn);
645 fprintf(F, "\t\t\t/* case %d */\n", tbl.branches[i].value);
646 fprintf(F, "\tje %s\n", get_cfop_target(tbl.branches[i].target, buf));
649 fprintf(F, "\tjmp %s\t\t\t/* default case */\n", get_cfop_target(tbl.defProj, buf));
659 * Emits code for a unconditional jump.
661 void emit_Jmp(ir_node *irn, emit_env_t *env) {
664 char buf[SNPRINTF_BUF_LEN];
665 ir_fprintf(F, "\tjmp %s\t\t\t/* Jmp(%+F) */\n", get_cfop_target(irn, buf), get_irn_link(irn));
670 /****************************
673 * _ __ _ __ ___ _ ___
674 * | '_ \| '__/ _ \| |/ __|
675 * | |_) | | | (_) | |\__ \
676 * | .__/|_| \___/| ||___/
679 ****************************/
682 * Emits code for a proj -> node
684 void emit_Proj(ir_node *irn, emit_env_t *env) {
685 ir_node *pred = get_Proj_pred(irn);
687 if (get_irn_op(pred) == op_Start) {
688 switch(get_Proj_proj(irn)) {
689 case pn_Start_X_initial_exec:
698 /**********************************
701 * | | ___ _ __ _ _| |_) |
702 * | | / _ \| '_ \| | | | _ <
703 * | |___| (_) | |_) | |_| | |_) |
704 * \_____\___/| .__/ \__, |____/
707 **********************************/
709 static void emit_CopyB_prolog(FILE *F, int rem, int size) {
710 fprintf(F, "\t/* memcopy %d bytes*/\n", size);
711 fprintf(F, "\tcld\t\t\t\t/* copy direction forward*/\n");
715 fprintf(F, "\tmovsb\t\t\t\t/* memcopy remainder 1 */\n");
718 fprintf(F, "\tmovsw\t\t\t\t/* memcopy remainder 2 */\n");
721 fprintf(F, "\tmovsb\t\t\t\t/* memcopy remainder 3 */\n");
722 fprintf(F, "\tmovsw\t\t\t\t/* memcopy remainder 3 */\n");
727 void emit_ia32_CopyB(ir_node *irn, emit_env_t *emit_env) {
728 FILE *F = emit_env->out;
729 tarval *tv = get_ia32_Immop_tarval(irn);
730 int rem = get_tarval_long(tv);
731 int size = get_tarval_long(get_ia32_Immop_tarval(get_irn_n(irn, 2)));
733 emit_CopyB_prolog(F, rem, size);
735 fprintf(F, "\trep movsd\t\t\t\t/* memcopy */\n");
738 void emit_ia32_CopyB_i(ir_node *irn, emit_env_t *emit_env) {
739 tarval *tv = get_ia32_Immop_tarval(irn);
740 int size = get_tarval_long(tv);
741 FILE *F = emit_env->out;
743 emit_CopyB_prolog(F, size & 0x3, size);
747 fprintf(F, "\tmovsd\t\t\t\t/* memcopy unrolled */\n");
751 /********************
759 ********************/
761 void emit_ia32_Call(ir_node *irn, emit_env_t *emit_env) {
766 /***********************************************************************************
769 * _ __ ___ __ _ _ _ __ | |_ _ __ __ _ _ __ ___ _____ _____ _ __| | __
770 * | '_ ` _ \ / _` | | '_ \ | _| '__/ _` | '_ ` _ \ / _ \ \ /\ / / _ \| '__| |/ /
771 * | | | | | | (_| | | | | | | | | | | (_| | | | | | | __/\ V V / (_) | | | <
772 * |_| |_| |_|\__,_|_|_| |_| |_| |_| \__,_|_| |_| |_|\___| \_/\_/ \___/|_| |_|\_\
774 ***********************************************************************************/
777 * Enters the emitter functions for handled nodes into the generic
778 * pointer of an opcode.
780 void ia32_register_emitters(void) {
783 #define IA32_EMIT(a) op_ia32_##a->ops.generic = (op_func)emit_ia32_##a
784 #define EMIT(a) op_##a->ops.generic = (op_func)emit_##a
786 /* first clear all generic operations */
787 for (i = get_irp_n_opcodes() - 1; i >= 0; --i) {
788 ir_op *op = get_irp_opcode(i);
789 op->ops.generic = (op_func)NULL;
792 /* generated int emitter functions */
829 /* generated floating point emitter */
844 /* other emitter functions */
846 IA32_EMIT(SwitchJmp);
857 * Emits code for a node.
859 static void ia32_emit_node(ir_node *irn, void *env) {
860 emit_env_t *emit_env = env;
861 firm_dbg_module_t *mod = emit_env->mod;
862 FILE *F = emit_env->out;
863 ir_op *op = get_irn_op(irn);
865 DBG((mod, LEVEL_1, "emitting code for %+F\n", irn));
867 if (op->ops.generic) {
868 void (*emit)(ir_node *, void *) = (void (*)(ir_node *, void *))op->ops.generic;
872 ir_fprintf(F, "\t\t\t\t\t/* %+F */\n", irn);
876 * Walks over the nodes in a block connected by scheduling edges
877 * and emits code for each node.
879 static void ia32_gen_block(ir_node *block, void *env) {
882 if (! is_Block(block))
885 fprintf(((emit_env_t *)env)->out, "BLOCK_%ld:\n", get_irn_node_nr(block));
886 sched_foreach(block, irn) {
887 ia32_emit_node(irn, env);
893 * Emits code for function start.
895 void ia32_emit_start(FILE *F, ir_graph *irg) {
896 const char *irg_name = get_entity_name(get_irg_entity(irg));
898 fprintf(F, "\t.text\n");
899 fprintf(F, ".globl %s\n", irg_name);
900 fprintf(F, "\t.type\t%s, @function\n", irg_name);
901 fprintf(F, "%s:\n", irg_name);
905 * Emits code for function end
907 void ia32_emit_end(FILE *F, ir_graph *irg) {
908 const char *irg_name = get_entity_name(get_irg_entity(irg));
910 fprintf(F, "\tret\n");
911 fprintf(F, "\t.size\t%s, .-%s\n\n", irg_name, irg_name);
915 * Sets labels for control flow nodes (jump target)
916 * TODO: Jump optimization
918 void ia32_gen_labels(ir_node *block, void *env) {
920 int n = get_Block_n_cfgpreds(block);
922 for (n--; n >= 0; n--) {
923 pred = get_Block_cfgpred(block, n);
924 set_irn_link(pred, block);
929 * Main driver. Emits the code for one routine.
931 void ia32_gen_routine(FILE *F, ir_graph *irg, const ia32_code_gen_t *cg) {
934 emit_env.mod = firm_dbg_register("ir.be.codegen.ia32");
936 emit_env.arch_env = cg->arch_env;
939 /* set the global arch_env (needed by print hooks) */
940 arch_env = cg->arch_env;
942 ia32_emit_start(F, irg);
943 irg_block_walk_graph(irg, ia32_gen_labels, NULL, &emit_env);
944 irg_walk_blkwise_graph(irg, NULL, ia32_gen_block, &emit_env);
945 ia32_emit_end(F, irg);