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;
38 char *ia32_emit_binop(const ir_node *n) {
39 static char *buf = NULL;
42 buf = xcalloc(1, SNPRINTF_BUF_LEN);
45 memset(buf, 0, SNPRINTF_BUF_LEN);
48 switch(get_ia32_op_type(n)) {
50 if (get_ia32_cnst(n)) {
51 lc_esnprintf(ia32_get_arg_env(), buf, SNPRINTF_BUF_LEN, "%1D, %s", n, get_ia32_cnst(n));
54 lc_esnprintf(ia32_get_arg_env(), buf, SNPRINTF_BUF_LEN, "%1D, %4S", n, n);
58 lc_esnprintf(ia32_get_arg_env(), buf, SNPRINTF_BUF_LEN, "%1D, %s", n, ia32_emit_am(n));
61 lc_esnprintf(ia32_get_arg_env(), buf, SNPRINTF_BUF_LEN, "%s, %4S", ia32_emit_am(n), n);
64 assert(0 && "unsupported op type");
70 char *ia32_emit_unop(const ir_node *n) {
71 static char *buf = NULL;
74 buf = xcalloc(1, SNPRINTF_BUF_LEN);
77 memset(buf, 0, SNPRINTF_BUF_LEN);
80 switch(get_ia32_op_type(n)) {
82 lc_esnprintf(ia32_get_arg_env(), buf, SNPRINTF_BUF_LEN, "%1D", n);
85 snprintf(buf, SNPRINTF_BUF_LEN, ia32_emit_am(n));
88 assert(0 && "unsupported op type");
94 char *ia32_emit_am(const ir_node *n) {
95 ia32_am_flavour_t am_flav = get_ia32_am_flavour(n);
99 static struct obstack *obst = NULL;
102 obst = xcalloc(1, sizeof(*obst));
105 obstack_free(obst, NULL);
108 /* obstack_free with NULL results in an uninitialized obstack */
111 obstack_printf(obst, "[");
113 if (am_flav & ia32_B) {
114 lc_eoprintf(ia32_get_arg_env(), obst, "%1S", n);
118 if (am_flav & ia32_I) {
120 obstack_printf(obst, "+");
123 lc_eoprintf(ia32_get_arg_env(), obst, "%2S", n);
125 if (am_flav & ia32_S) {
126 obstack_printf(obst, "*%d", get_ia32_am_scale(n));
132 if (am_flav & ia32_O) {
133 obstack_printf(obst, get_ia32_am_offs(n));
136 obstack_printf(obst, "] ");
138 size = obstack_object_size(obst);
139 s = obstack_finish(obst);
145 /*************************************************************
147 * (_) | | / _| | | | |
148 * _ __ _ __ _ _ __ | |_| |_ | |__ ___| |_ __ ___ _ __
149 * | '_ \| '__| | '_ \| __| _| | '_ \ / _ \ | '_ \ / _ \ '__|
150 * | |_) | | | | | | | |_| | | | | | __/ | |_) | __/ |
151 * | .__/|_| |_|_| |_|\__|_| |_| |_|\___|_| .__/ \___|_|
154 *************************************************************/
156 /* We always pass the ir_node which is a pointer. */
157 static int ia32_get_arg_type(const lc_arg_occ_t *occ) {
158 return lc_arg_type_ptr;
163 * Returns the register at in position pos.
165 static const arch_register_t *get_in_reg(ir_node *irn, int pos) {
167 const arch_register_t *reg = NULL;
169 assert(get_irn_arity(irn) > pos && "Invalid IN position");
171 /* The out register of the operator at position pos is the
172 in register we need. */
173 op = get_irn_n(irn, pos);
175 reg = arch_get_irn_register(arch_env, op);
177 assert(reg && "no in register found");
182 * Returns the register at out position pos.
184 static const arch_register_t *get_out_reg(ir_node *irn, int pos) {
186 const arch_register_t *reg = NULL;
188 assert(get_irn_n_edges(irn) > pos && "Invalid OUT position");
190 /* 1st case: irn is not of mode_T, so it has only */
191 /* one OUT register -> good */
192 /* 2nd case: irn is of mode_T -> collect all Projs and ask the */
193 /* Proj with the corresponding projnum for the register */
195 if (get_irn_mode(irn) != mode_T) {
196 reg = arch_get_irn_register(arch_env, irn);
198 else if (is_ia32_irn(irn)) {
199 reg = get_ia32_out_reg(irn, pos);
202 const ir_edge_t *edge;
204 foreach_out_edge(irn, edge) {
205 proj = get_edge_src_irn(edge);
206 assert(is_Proj(proj) && "non-Proj from mode_T node");
207 if (get_Proj_proj(proj) == pos) {
208 reg = arch_get_irn_register(arch_env, proj);
214 assert(reg && "no out register found");
219 * Returns the number of the in register at position pos.
221 int get_ia32_reg_nr(ir_node *irn, int pos, int in_out) {
222 const arch_register_t *reg;
225 reg = get_in_reg(irn, pos);
228 reg = get_out_reg(irn, pos);
231 return arch_register_get_index(reg);
240 * Returns the name of the in register at position pos.
242 static const char *get_ia32_reg_name(ir_node *irn, int pos, enum io_direction in_out) {
243 const arch_register_t *reg;
245 if (in_out == IN_REG) {
246 reg = get_in_reg(irn, pos);
249 reg = get_out_reg(irn, pos);
252 return arch_register_get_name(reg);
256 * Get the register name for a node.
258 static int ia32_get_reg_name(lc_appendable_t *app,
259 const lc_arg_occ_t *occ, const lc_arg_value_t *arg)
262 ir_node *X = arg->v_ptr;
263 int nr = occ->width - 1;
266 return lc_arg_append(app, occ, "(null)", 6);
268 buf = get_ia32_reg_name(X, nr, occ->conversion == 'S' ? IN_REG : OUT_REG);
270 lc_appendable_chadd(app, '%');
271 return lc_arg_append(app, occ, buf, strlen(buf));
275 * Returns the tarval or offset of an ia32 as a string.
277 static int ia32_const_to_str(lc_appendable_t *app,
278 const lc_arg_occ_t *occ, const lc_arg_value_t *arg)
281 ir_node *X = arg->v_ptr;
284 return lc_arg_append(app, occ, "(null)", 6);
286 if (occ->conversion == 'C') {
287 buf = get_ia32_cnst(X);
290 buf = get_ia32_am_offs(X);
293 return lc_arg_append(app, occ, buf, strlen(buf));
297 * Determines the SSE suffix depending on the mode.
299 static int ia32_get_mode_suffix(lc_appendable_t *app,
300 const lc_arg_occ_t *occ, const lc_arg_value_t *arg)
302 ir_node *X = arg->v_ptr;
305 return lc_arg_append(app, occ, "(null)", 6);
307 if (get_mode_size_bits(get_irn_mode(X)) == 32)
308 return lc_appendable_chadd(app, 's');
310 return lc_appendable_chadd(app, 'd');
314 * Return the ia32 printf arg environment.
315 * We use the firm environment with some additional handlers.
317 const lc_arg_env_t *ia32_get_arg_env(void) {
318 static lc_arg_env_t *env = NULL;
320 static const lc_arg_handler_t ia32_reg_handler = { ia32_get_arg_type, ia32_get_reg_name };
321 static const lc_arg_handler_t ia32_const_handler = { ia32_get_arg_type, ia32_const_to_str };
322 static const lc_arg_handler_t ia32_mode_handler = { ia32_get_arg_type, ia32_get_mode_suffix };
325 /* extend the firm printer */
326 env = firm_get_arg_env();
329 lc_arg_register(env, "ia32:sreg", 'S', &ia32_reg_handler);
330 lc_arg_register(env, "ia32:dreg", 'D', &ia32_reg_handler);
331 lc_arg_register(env, "ia32:cnst", 'C', &ia32_const_handler);
332 lc_arg_register(env, "ia32:offs", 'O', &ia32_const_handler);
333 lc_arg_register(env, "ia32:mode", 'M', &ia32_mode_handler);
341 * Add a number to a prefix. This number will not be used a second time.
343 static char *get_unique_label(char *buf, size_t buflen, const char *prefix) {
344 static unsigned long id = 0;
345 snprintf(buf, buflen, "%s%lu", prefix, ++id);
350 /*************************************************
353 * ___ _ __ ___ _| |_ ___ ___ _ __ __| |
354 * / _ \ '_ ` _ \| | __| / __/ _ \| '_ \ / _` |
355 * | __/ | | | | | | |_ | (_| (_) | | | | (_| |
356 * \___|_| |_| |_|_|\__| \___\___/|_| |_|\__,_|
358 *************************************************/
361 * coding of conditions
363 struct cmp2conditon_t {
369 * positive conditions for signed compares
371 static const struct cmp2conditon_t cmp2condition_s[] = {
372 { NULL, pn_Cmp_False }, /* always false */
373 { "e", pn_Cmp_Eq }, /* == */
374 { "l", pn_Cmp_Lt }, /* < */
375 { "le", pn_Cmp_Le }, /* <= */
376 { "g", pn_Cmp_Gt }, /* > */
377 { "ge", pn_Cmp_Ge }, /* >= */
378 { "ne", pn_Cmp_Lg }, /* != */
379 { "ordered", pn_Cmp_Leg }, /* Floating point: ordered */
380 { "unordered", pn_Cmp_Uo }, /* FLoting point: unordered */
381 { "unordered or ==", pn_Cmp_Ue }, /* Floating point: unordered or == */
382 { "unordered or <", pn_Cmp_Ul }, /* Floating point: unordered or < */
383 { "unordered or <=", pn_Cmp_Ule }, /* Floating point: unordered or <= */
384 { "unordered or >", pn_Cmp_Ug }, /* Floating point: unordered or > */
385 { "unordered or >=", pn_Cmp_Uge }, /* Floating point: unordered or >= */
386 { "unordered or !=", pn_Cmp_Ne }, /* Floating point: unordered or != */
387 { NULL, pn_Cmp_True }, /* always true */
391 * positive conditions for unsigned compares
393 static const struct cmp2conditon_t cmp2condition_u[] = {
394 { NULL, pn_Cmp_False }, /* always false */
395 { "e", pn_Cmp_Eq }, /* == */
396 { "b", pn_Cmp_Lt }, /* < */
397 { "be", pn_Cmp_Le }, /* <= */
398 { "a", pn_Cmp_Gt }, /* > */
399 { "ae", pn_Cmp_Ge }, /* >= */
400 { "ne", pn_Cmp_Lg }, /* != */
401 { "ordered", pn_Cmp_Leg }, /* Floating point: ordered */
402 { "unordered", pn_Cmp_Uo }, /* FLoting point: unordered */
403 { "unordered or ==", pn_Cmp_Ue }, /* Floating point: unordered or == */
404 { "unordered or <", pn_Cmp_Ul }, /* Floating point: unordered or < */
405 { "unordered or <=", pn_Cmp_Ule }, /* Floating point: unordered or <= */
406 { "unordered or >", pn_Cmp_Ug }, /* Floating point: unordered or > */
407 { "unordered or >=", pn_Cmp_Uge }, /* Floating point: unordered or >= */
408 { "unordered or !=", pn_Cmp_Ne }, /* Floating point: unordered or != */
409 { NULL, pn_Cmp_True }, /* always true */
413 * returns the condition code
415 static const char *get_cmp_suffix(int cmp_code, int unsigned_cmp)
417 assert(cmp2condition_s[cmp_code].num == cmp_code);
418 assert(cmp2condition_u[cmp_code].num == cmp_code);
420 return unsigned_cmp ? cmp2condition_u[cmp_code & 7].name : cmp2condition_s[cmp_code & 7].name;
424 * Returns the target label for a control flow node.
426 static char *get_cfop_target(const ir_node *irn, char *buf) {
427 ir_node *bl = get_irn_link(irn);
429 snprintf(buf, SNPRINTF_BUF_LEN, "BLOCK_%ld", get_irn_node_nr(bl));
434 * Emits the jump sequence for a conditional jump (cmp + jmp_true + jmp_false)
436 static void finish_CondJmp(FILE *F, ir_node *irn) {
438 const ir_edge_t *edge;
439 char buf[SNPRINTF_BUF_LEN];
441 edge = get_irn_out_edge_first(irn);
442 proj = get_edge_src_irn(edge);
443 assert(is_Proj(proj) && "CondJmp with a non-Proj");
445 if (get_Proj_proj(proj) == 1) {
446 fprintf(F, "\tj%s %s\t\t\t/* cmp(a, b) == TRUE */\n",
447 get_cmp_suffix(get_ia32_pncode(irn), !mode_is_signed(get_irn_mode(get_irn_n(irn, 0)))),
448 get_cfop_target(proj, buf));
451 fprintf(F, "\tjn%s %s\t\t\t/* cmp(a, b) == FALSE */\n",
452 get_cmp_suffix(get_ia32_pncode(irn), !mode_is_signed(get_irn_mode(get_irn_n(irn, 0)))),
453 get_cfop_target(proj, buf));
456 edge = get_irn_out_edge_next(irn, edge);
458 proj = get_edge_src_irn(edge);
459 assert(is_Proj(proj) && "CondJmp with a non-Proj");
460 fprintf(F, "\tjmp %s\t\t\t/* otherwise */\n", get_cfop_target(proj, buf));
465 * Emits code for conditional jump with two variables.
467 static void emit_ia32_CondJmp(ir_node *irn, emit_env_t *env) {
470 lc_efprintf(ia32_get_arg_env(), F, "\tcmp %2S, %1S\t\t\t/* CondJmp(%+F, %+F) */\n", irn, irn,
471 get_irn_n(irn, 0), get_irn_n(irn, 1));
472 finish_CondJmp(F, irn);
476 * Emits code for conditional jump with immediate.
478 void emit_ia32_CondJmp_i(ir_node *irn, emit_env_t *env) {
481 lc_efprintf(ia32_get_arg_env(), F, "\tcmp %C, %1S\t\t\t/* CondJmp_i(%+F) */\n", irn, irn, get_irn_n(irn, 0));
482 finish_CondJmp(F, irn);
487 /*********************************************************
490 * ___ _ __ ___ _| |_ _ _ _ _ __ ___ _ __ ___
491 * / _ \ '_ ` _ \| | __| | | | | | '_ ` _ \| '_ \/ __|
492 * | __/ | | | | | | |_ | | |_| | | | | | | |_) \__ \
493 * \___|_| |_| |_|_|\__| | |\__,_|_| |_| |_| .__/|___/
496 *********************************************************/
498 /* jump table entry (target and corresponding number) */
499 typedef struct _branch_t {
504 /* jump table for switch generation */
505 typedef struct _jmp_tbl_t {
506 ir_node *defProj; /**< default target */
507 int min_value; /**< smallest switch case */
508 int max_value; /**< largest switch case */
509 int num_branches; /**< number of jumps */
510 char *label; /**< label of the jump table */
511 branch_t *branches; /**< jump array */
515 * Compare two variables of type branch_t. Used to sort all switch cases
517 static int ia32_cmp_branch_t(const void *a, const void *b) {
518 branch_t *b1 = (branch_t *)a;
519 branch_t *b2 = (branch_t *)b;
521 if (b1->value <= b2->value)
528 * Emits code for a SwitchJmp (creates a jump table if
529 * possible otherwise a cmp-jmp cascade). Port from
532 void emit_ia32_SwitchJmp(const ir_node *irn, emit_env_t *emit_env) {
533 unsigned long interval;
534 char buf[SNPRINTF_BUF_LEN];
535 int last_value, i, pn, do_jmp_tbl = 1;
538 const ir_edge_t *edge;
539 const lc_arg_env_t *env = ia32_get_arg_env();
540 FILE *F = emit_env->out;
542 /* fill the table structure */
543 tbl.label = xmalloc(SNPRINTF_BUF_LEN);
544 tbl.label = get_unique_label(tbl.label, SNPRINTF_BUF_LEN, "JMPTBL_");
546 tbl.num_branches = get_irn_n_edges(irn);
547 tbl.branches = xcalloc(tbl.num_branches, sizeof(tbl.branches[0]));
548 tbl.min_value = INT_MAX;
549 tbl.max_value = INT_MIN;
552 /* go over all proj's and collect them */
553 foreach_out_edge(irn, edge) {
554 proj = get_edge_src_irn(edge);
555 assert(is_Proj(proj) && "Only proj allowed at SwitchJmp");
557 pn = get_Proj_proj(proj);
559 /* create branch entry */
560 tbl.branches[i].target = proj;
561 tbl.branches[i].value = pn;
563 tbl.min_value = pn < tbl.min_value ? pn : tbl.min_value;
564 tbl.max_value = pn > tbl.max_value ? pn : tbl.max_value;
566 /* check for default proj */
567 if (pn == get_ia32_pncode(irn)) {
568 assert(tbl.defProj == NULL && "found two defProjs at SwitchJmp");
575 /* sort the branches by their number */
576 qsort(tbl.branches, tbl.num_branches, sizeof(tbl.branches[0]), ia32_cmp_branch_t);
578 /* two-complement's magic make this work without overflow */
579 interval = tbl.max_value - tbl.min_value;
581 /* check value interval */
582 if (interval > 16 * 1024) {
586 /* check ratio of value interval to number of branches */
587 if ((float)(interval + 1) / (float)tbl.num_branches > 8.0) {
593 if (tbl.min_value != 0) {
594 fprintf(F, "\tcmpl %lu, -%d", interval, tbl.min_value);
595 lc_efprintf(env, F, "(%1S)\t\t/* first switch value is not 0 */\n", irn);
598 fprintf(F, "\tcmpl %lu, ", interval);
599 lc_efprintf(env, F, "%1S\t\t\t/* compare for switch */\n", irn);
602 fprintf(F, "\tja %s\t\t\t/* default jump if out of range */\n", get_cfop_target(tbl.defProj, buf));
604 if (tbl.num_branches > 1) {
607 //fprintf(F, "\tjmp *%s", tbl.label);
608 lc_efprintf(env, F, "\tjmp *%s(,%1S,4)\t\t/* get jump table entry as target */\n", tbl.label, irn);
610 fprintf(F, "\t.section\t.rodata\t\t/* start jump table */\n");
611 fprintf(F, "\t.align 4\n");
613 fprintf(F, "%s:\n", tbl.label);
614 fprintf(F, "\t.long %s\t\t\t/* case %d */\n", get_cfop_target(tbl.branches[0].target, buf), tbl.branches[0].value);
616 last_value = tbl.branches[0].value;
617 for (i = 1; i < tbl.num_branches; ++i) {
618 while (++last_value < tbl.branches[i].value) {
619 fprintf(F, "\t.long %s\t\t/* default case */\n", get_cfop_target(tbl.defProj, buf));
621 fprintf(F, "\t.long %s\t\t\t/* case %d */\n", get_cfop_target(tbl.branches[i].target, buf), last_value);
624 fprintf(F, "\t.text\t\t\t\t/* end of jump table */\n");
627 /* one jump is enough */
628 fprintf(F, "\tjmp %s\t\t/* only one case given */\n", get_cfop_target(tbl.branches[0].target, buf));
631 else { // no jump table
632 for (i = 0; i < tbl.num_branches; ++i) {
633 fprintf(F, "\tcmpl %d, ", tbl.branches[i].value);
634 lc_efprintf(env, F, "%1S", irn);
635 fprintf(F, "\t\t\t/* case %d */\n", tbl.branches[i].value);
636 fprintf(F, "\tje %s\n", get_cfop_target(tbl.branches[i].target, buf));
639 fprintf(F, "\tjmp %s\t\t\t/* default case */\n", get_cfop_target(tbl.defProj, buf));
649 * Emits code for a unconditional jump.
651 void emit_Jmp(ir_node *irn, emit_env_t *env) {
654 char buf[SNPRINTF_BUF_LEN];
655 ir_fprintf(F, "\tjmp %s\t\t\t/* Jmp(%+F) */\n", get_cfop_target(irn, buf), get_irn_link(irn));
660 /****************************
663 * _ __ _ __ ___ _ ___
664 * | '_ \| '__/ _ \| |/ __|
665 * | |_) | | | (_) | |\__ \
666 * | .__/|_| \___/| ||___/
669 ****************************/
672 * Emits code for a proj -> node
674 void emit_Proj(ir_node *irn, emit_env_t *env) {
675 ir_node *pred = get_Proj_pred(irn);
677 if (get_irn_op(pred) == op_Start) {
678 switch(get_Proj_proj(irn)) {
679 case pn_Start_X_initial_exec:
688 /**********************************
691 * | | ___ _ __ _ _| |_) |
692 * | | / _ \| '_ \| | | | _ <
693 * | |___| (_) | |_) | |_| | |_) |
694 * \_____\___/| .__/ \__, |____/
697 **********************************/
699 static void emit_CopyB_prolog(FILE *F, int rem, int size) {
700 fprintf(F, "\t/* memcopy %d bytes*/\n", size);
701 fprintf(F, "\tcld\t\t\t\t/* copy direction forward*/\n");
705 fprintf(F, "\tmovsb\t\t\t\t/* memcopy remainder 1 */\n");
708 fprintf(F, "\tmovsw\t\t\t\t/* memcopy remainder 2 */\n");
711 fprintf(F, "\tmovsb\t\t\t\t/* memcopy remainder 3 */\n");
712 fprintf(F, "\tmovsw\t\t\t\t/* memcopy remainder 3 */\n");
717 void emit_ia32_CopyB(ir_node *irn, emit_env_t *emit_env) {
718 FILE *F = emit_env->out;
719 tarval *tv = get_ia32_Immop_tarval(irn);
720 int rem = get_tarval_long(tv);
721 int size = get_tarval_long(get_ia32_Immop_tarval(get_irn_n(irn, 2)));
723 emit_CopyB_prolog(F, rem, size);
725 fprintf(F, "\trep movsd\t\t\t\t/* memcopy */\n");
728 void emit_ia32_CopyB_i(ir_node *irn, emit_env_t *emit_env) {
729 tarval *tv = get_ia32_Immop_tarval(irn);
730 int size = get_tarval_long(tv);
731 FILE *F = emit_env->out;
733 emit_CopyB_prolog(F, size & 0x3, size);
737 fprintf(F, "\tmovsd\t\t\t\t/* memcopy unrolled */\n");
741 /********************
749 ********************/
751 void emit_ia32_Call(ir_node *irn, emit_env_t *emit_env) {
756 /***********************************************************************************
759 * _ __ ___ __ _ _ _ __ | |_ _ __ __ _ _ __ ___ _____ _____ _ __| | __
760 * | '_ ` _ \ / _` | | '_ \ | _| '__/ _` | '_ ` _ \ / _ \ \ /\ / / _ \| '__| |/ /
761 * | | | | | | (_| | | | | | | | | | | (_| | | | | | | __/\ V V / (_) | | | <
762 * |_| |_| |_|\__,_|_|_| |_| |_| |_| \__,_|_| |_| |_|\___| \_/\_/ \___/|_| |_|\_\
764 ***********************************************************************************/
767 * Emits code for a node.
769 void ia32_emit_node(ir_node *irn, void *env) {
770 emit_env_t *emit_env = env;
771 firm_dbg_module_t *mod = emit_env->mod;
772 FILE *F = emit_env->out;
774 DBG((mod, LEVEL_1, "emitting code for %+F\n", irn));
776 #define IA32_EMIT(a) if (is_ia32_##a(irn)) { emit_ia32_##a(irn, emit_env); return; }
777 #define EMIT(a) if (get_irn_opcode(irn) == iro_##a) { emit_##a(irn, emit_env); return; }
779 /* generated int emitter functions */
816 /* generated floating point emitter */
831 /* other emitter functions */
833 IA32_EMIT(SwitchJmp);
839 ir_fprintf(F, "\t\t\t\t\t/* %+F */\n", irn);
843 * Walks over the nodes in a block connected by scheduling edges
844 * and emits code for each node.
846 void ia32_gen_block(ir_node *block, void *env) {
849 if (! is_Block(block))
852 fprintf(((emit_env_t *)env)->out, "BLOCK_%ld:\n", get_irn_node_nr(block));
853 sched_foreach(block, irn) {
854 ia32_emit_node(irn, env);
860 * Emits code for function start.
862 void ia32_emit_start(FILE *F, ir_graph *irg) {
863 const char *irg_name = get_entity_name(get_irg_entity(irg));
865 fprintf(F, "\t.text\n");
866 fprintf(F, ".globl %s\n", irg_name);
867 fprintf(F, "\t.type\t%s, @function\n", irg_name);
868 fprintf(F, "%s:\n", irg_name);
872 * Emits code for function end
874 void ia32_emit_end(FILE *F, ir_graph *irg) {
875 const char *irg_name = get_entity_name(get_irg_entity(irg));
877 fprintf(F, "\tret\n");
878 fprintf(F, "\t.size\t%s, .-%s\n\n", irg_name, irg_name);
882 * Sets labels for control flow nodes (jump target)
883 * TODO: Jump optimization
885 void ia32_gen_labels(ir_node *block, void *env) {
887 int n = get_Block_n_cfgpreds(block);
889 for (n--; n >= 0; n--) {
890 pred = get_Block_cfgpred(block, n);
891 set_irn_link(pred, block);
898 void ia32_gen_routine(FILE *F, ir_graph *irg, const ia32_code_gen_t *cg) {
901 emit_env.mod = firm_dbg_register("ir.be.codegen.ia32");
903 emit_env.arch_env = cg->arch_env;
906 /* set the global arch_env (needed by print hooks) */
907 arch_env = cg->arch_env;
909 ia32_emit_start(F, irg);
910 irg_block_walk_graph(irg, ia32_gen_labels, NULL, &emit_env);
911 irg_walk_blkwise_graph(irg, NULL, ia32_gen_block, &emit_env);
912 ia32_emit_end(F, irg);