2 * This file implements the node emitter.
22 #include "iredges_t.h"
24 #include "../besched.h"
25 #include "../benode_t.h"
27 #include "ia32_emitter.h"
28 #include "gen_ia32_emitter.h"
29 #include "ia32_nodes_attr.h"
30 #include "ia32_new_nodes.h"
31 #include "ia32_map_regs.h"
33 #ifdef obstack_chunk_alloc
34 # undef obstack_chunk_alloc
35 # define obstack_chunk_alloc xmalloc
37 # define obstack_chunk_alloc xmalloc
38 # define obstack_chunk_free free
41 extern int obstack_printf(struct obstack *obst, char *fmt, ...);
43 #define SNPRINTF_BUF_LEN 128
45 static const arch_env_t *arch_env = NULL;
47 /*************************************************************
49 * (_) | | / _| | | | |
50 * _ __ _ __ _ _ __ | |_| |_ | |__ ___| |_ __ ___ _ __
51 * | '_ \| '__| | '_ \| __| _| | '_ \ / _ \ | '_ \ / _ \ '__|
52 * | |_) | | | | | | | |_| | | | | | __/ | |_) | __/ |
53 * | .__/|_| |_|_| |_|\__|_| |_| |_|\___|_| .__/ \___|_|
56 *************************************************************/
58 /* We always pass the ir_node which is a pointer. */
59 static int ia32_get_arg_type(const lc_arg_occ_t *occ) {
60 return lc_arg_type_ptr;
65 * Returns the register at in position pos.
67 static const arch_register_t *get_in_reg(const ir_node *irn, int pos) {
69 const arch_register_t *reg = NULL;
71 assert(get_irn_arity(irn) > pos && "Invalid IN position");
73 /* The out register of the operator at position pos is the
74 in register we need. */
75 op = get_irn_n(irn, pos);
77 reg = arch_get_irn_register(arch_env, op);
79 assert(reg && "no in register found");
84 * Returns the register at out position pos.
86 static const arch_register_t *get_out_reg(const ir_node *irn, int pos) {
88 const arch_register_t *reg = NULL;
90 /* 1st case: irn is not of mode_T, so it has only */
91 /* one OUT register -> good */
92 /* 2nd case: irn is of mode_T -> collect all Projs and ask the */
93 /* Proj with the corresponding projnum for the register */
95 if (get_irn_mode(irn) != mode_T) {
96 reg = arch_get_irn_register(arch_env, irn);
98 else if (is_ia32_irn(irn)) {
99 reg = get_ia32_out_reg(irn, pos);
102 const ir_edge_t *edge;
104 foreach_out_edge(irn, edge) {
105 proj = get_edge_src_irn(edge);
106 assert(is_Proj(proj) && "non-Proj from mode_T node");
107 if (get_Proj_proj(proj) == pos) {
108 reg = arch_get_irn_register(arch_env, proj);
114 assert(reg && "no out register found");
124 * Returns the name of the in register at position pos.
126 static const char *get_ia32_reg_name(ir_node *irn, int pos, enum io_direction in_out) {
127 const arch_register_t *reg;
129 if (in_out == IN_REG) {
130 reg = get_in_reg(irn, pos);
133 /* destination address mode nodes don't have outputs */
134 if (is_ia32_irn(irn) && get_ia32_op_type(irn) == ia32_AddrModeD) {
138 reg = get_out_reg(irn, pos);
141 return arch_register_get_name(reg);
145 * Get the register name for a node.
147 static int ia32_get_reg_name(lc_appendable_t *app,
148 const lc_arg_occ_t *occ, const lc_arg_value_t *arg)
151 ir_node *X = arg->v_ptr;
152 int nr = occ->width - 1;
155 return lc_arg_append(app, occ, "(null)", 6);
157 buf = get_ia32_reg_name(X, nr, occ->conversion == 'S' ? IN_REG : OUT_REG);
159 return lc_arg_append(app, occ, buf, strlen(buf));
163 * Returns the tarval, offset or scale of an ia32 as a string.
165 static int ia32_const_to_str(lc_appendable_t *app,
166 const lc_arg_occ_t *occ, const lc_arg_value_t *arg)
169 ir_node *X = arg->v_ptr;
172 return lc_arg_append(app, occ, "(null)", 6);
174 if (occ->conversion == 'C') {
175 buf = get_ia32_cnst(X);
178 buf = get_ia32_am_offs(X);
181 return buf ? lc_arg_append(app, occ, buf, strlen(buf)) : 0;
185 * Determines the SSE suffix depending on the mode.
187 static int ia32_get_mode_suffix(lc_appendable_t *app,
188 const lc_arg_occ_t *occ, const lc_arg_value_t *arg)
190 ir_node *X = arg->v_ptr;
191 ir_mode *mode = get_irn_mode(X);
193 if (mode == mode_T) {
194 mode = is_ia32_AddrModeS(X) || is_ia32_AddrModeD(X) ? get_ia32_ls_mode(X) : get_ia32_res_mode(X);
198 return lc_arg_append(app, occ, "(null)", 6);
200 if (mode_is_float(mode)) {
201 return lc_appendable_chadd(app, get_mode_size_bits(mode) == 32 ? 's' : 'd');
205 return lc_appendable_chadd(app, mode_is_signed(mode) ? 's' : 'z');
210 * Return the ia32 printf arg environment.
211 * We use the firm environment with some additional handlers.
213 const lc_arg_env_t *ia32_get_arg_env(void) {
214 static lc_arg_env_t *env = NULL;
216 static const lc_arg_handler_t ia32_reg_handler = { ia32_get_arg_type, ia32_get_reg_name };
217 static const lc_arg_handler_t ia32_const_handler = { ia32_get_arg_type, ia32_const_to_str };
218 static const lc_arg_handler_t ia32_mode_handler = { ia32_get_arg_type, ia32_get_mode_suffix };
221 /* extend the firm printer */
222 env = firm_get_arg_env();
224 lc_arg_register(env, "ia32:sreg", 'S', &ia32_reg_handler);
225 lc_arg_register(env, "ia32:dreg", 'D', &ia32_reg_handler);
226 lc_arg_register(env, "ia32:cnst", 'C', &ia32_const_handler);
227 lc_arg_register(env, "ia32:offs", 'O', &ia32_const_handler);
228 lc_arg_register(env, "ia32:mode", 'M', &ia32_mode_handler);
235 * Emits registers and/or address mode of a binary operation.
237 char *ia32_emit_binop(const ir_node *n) {
238 static char *buf = NULL;
240 /* verify that this function is never called on non-AM supporting operations */
241 assert(get_ia32_am_support(n) != ia32_am_None && "emit binop expects addressmode support");
244 buf = xcalloc(1, SNPRINTF_BUF_LEN);
247 memset(buf, 0, SNPRINTF_BUF_LEN);
250 switch(get_ia32_op_type(n)) {
252 if (get_ia32_cnst(n)) {
253 lc_esnprintf(ia32_get_arg_env(), buf, SNPRINTF_BUF_LEN, "%3S, %s", n, get_ia32_cnst(n));
256 const arch_register_t *in1 = get_in_reg(n, 2);
257 const arch_register_t *in2 = get_in_reg(n, 3);
258 const arch_register_t *out = get_ia32_n_res(n) > 0 ? get_out_reg(n, 0) : NULL;
259 const arch_register_t *in;
261 in = out ? (REGS_ARE_EQUAL(out, in2) ? in1 : in2) : in2;
262 out = out ? out : in1;
264 snprintf(buf, SNPRINTF_BUF_LEN, "%s, %s", \
265 arch_register_get_name(out), arch_register_get_name(in));
269 lc_esnprintf(ia32_get_arg_env(), buf, SNPRINTF_BUF_LEN, "%4S, %s", n, ia32_emit_am(n));
272 if (get_ia32_cnst(n)) {
273 lc_esnprintf(ia32_get_arg_env(), buf, SNPRINTF_BUF_LEN, "%s, %s", ia32_emit_am(n), get_ia32_cnst(n));
276 lc_esnprintf(ia32_get_arg_env(), buf, SNPRINTF_BUF_LEN, "%s, %3S", ia32_emit_am(n), n);
280 assert(0 && "unsupported op type");
287 * Emits registers and/or address mode of a unary operation.
289 char *ia32_emit_unop(const ir_node *n) {
290 static char *buf = NULL;
293 buf = xcalloc(1, SNPRINTF_BUF_LEN);
296 memset(buf, 0, SNPRINTF_BUF_LEN);
299 switch(get_ia32_op_type(n)) {
301 lc_esnprintf(ia32_get_arg_env(), buf, SNPRINTF_BUF_LEN, "%1D", n);
304 snprintf(buf, SNPRINTF_BUF_LEN, ia32_emit_am(n));
307 assert(0 && "unsupported op type");
316 char *ia32_emit_am(const ir_node *n) {
317 ia32_am_flavour_t am_flav = get_ia32_am_flavour(n);
321 static struct obstack *obst = NULL;
322 ir_mode *mode = get_ia32_ls_mode(n);
324 if (! is_ia32_Lea(n))
325 assert(mode && "AM node must have ls_mode attribute set.");
328 obst = xcalloc(1, sizeof(*obst));
331 obstack_free(obst, NULL);
334 /* obstack_free with NULL results in an uninitialized obstack */
338 switch (get_mode_size_bits(mode)) {
340 obstack_printf(obst, "BYTE PTR ");
343 obstack_printf(obst, "WORD PTR ");
350 obstack_printf(obst, "[");
352 if (am_flav & ia32_B) {
353 lc_eoprintf(ia32_get_arg_env(), obst, "%1S", n);
357 if (am_flav & ia32_I) {
359 obstack_printf(obst, "+");
362 lc_eoprintf(ia32_get_arg_env(), obst, "%2S", n);
364 if (am_flav & ia32_S) {
365 obstack_printf(obst, "*%d", 1 << get_ia32_am_scale(n));
371 if (am_flav & ia32_O) {
372 obstack_printf(obst, get_ia32_am_offs(n));
375 obstack_printf(obst, "] ");
377 size = obstack_object_size(obst);
378 s = obstack_finish(obst);
387 * Add a number to a prefix. This number will not be used a second time.
389 static char *get_unique_label(char *buf, size_t buflen, const char *prefix) {
390 static unsigned long id = 0;
391 snprintf(buf, buflen, "%s%lu", prefix, ++id);
397 /*************************************************
400 * ___ _ __ ___ _| |_ ___ ___ _ __ __| |
401 * / _ \ '_ ` _ \| | __| / __/ _ \| '_ \ / _` |
402 * | __/ | | | | | | |_ | (_| (_) | | | | (_| |
403 * \___|_| |_| |_|_|\__| \___\___/|_| |_|\__,_|
405 *************************************************/
408 * coding of conditions
410 struct cmp2conditon_t {
416 * positive conditions for signed compares
418 static const struct cmp2conditon_t cmp2condition_s[] = {
419 { NULL, pn_Cmp_False }, /* always false */
420 { "e", pn_Cmp_Eq }, /* == */
421 { "l", pn_Cmp_Lt }, /* < */
422 { "le", pn_Cmp_Le }, /* <= */
423 { "g", pn_Cmp_Gt }, /* > */
424 { "ge", pn_Cmp_Ge }, /* >= */
425 { "ne", pn_Cmp_Lg }, /* != */
426 { "ordered", pn_Cmp_Leg }, /* Floating point: ordered */
427 { "unordered", pn_Cmp_Uo }, /* FLoting point: unordered */
428 { "unordered or ==", pn_Cmp_Ue }, /* Floating point: unordered or == */
429 { "unordered or <", pn_Cmp_Ul }, /* Floating point: unordered or < */
430 { "unordered or <=", pn_Cmp_Ule }, /* Floating point: unordered or <= */
431 { "unordered or >", pn_Cmp_Ug }, /* Floating point: unordered or > */
432 { "unordered or >=", pn_Cmp_Uge }, /* Floating point: unordered or >= */
433 { "unordered or !=", pn_Cmp_Ne }, /* Floating point: unordered or != */
434 { NULL, pn_Cmp_True }, /* always true */
438 * positive conditions for unsigned compares
440 static const struct cmp2conditon_t cmp2condition_u[] = {
441 { NULL, pn_Cmp_False }, /* always false */
442 { "e", pn_Cmp_Eq }, /* == */
443 { "b", pn_Cmp_Lt }, /* < */
444 { "be", pn_Cmp_Le }, /* <= */
445 { "a", pn_Cmp_Gt }, /* > */
446 { "ae", pn_Cmp_Ge }, /* >= */
447 { "ne", pn_Cmp_Lg }, /* != */
448 { "ordered", pn_Cmp_Leg }, /* Floating point: ordered */
449 { "unordered", pn_Cmp_Uo }, /* FLoting point: unordered */
450 { "unordered or ==", pn_Cmp_Ue }, /* Floating point: unordered or == */
451 { "unordered or <", pn_Cmp_Ul }, /* Floating point: unordered or < */
452 { "unordered or <=", pn_Cmp_Ule }, /* Floating point: unordered or <= */
453 { "unordered or >", pn_Cmp_Ug }, /* Floating point: unordered or > */
454 { "unordered or >=", pn_Cmp_Uge }, /* Floating point: unordered or >= */
455 { "unordered or !=", pn_Cmp_Ne }, /* Floating point: unordered or != */
456 { NULL, pn_Cmp_True }, /* always true */
460 * returns the condition code
462 static const char *get_cmp_suffix(int cmp_code, int unsigned_cmp)
464 assert(cmp2condition_s[cmp_code].num == cmp_code);
465 assert(cmp2condition_u[cmp_code].num == cmp_code);
467 return unsigned_cmp ? cmp2condition_u[cmp_code & 7].name : cmp2condition_s[cmp_code & 7].name;
471 * Returns the target label for a control flow node.
473 static char *get_cfop_target(const ir_node *irn, char *buf) {
474 ir_node *bl = get_irn_link(irn);
476 snprintf(buf, SNPRINTF_BUF_LEN, "BLOCK_%ld", get_irn_node_nr(bl));
481 * Emits the jump sequence for a conditional jump (cmp + jmp_true + jmp_false)
483 static void finish_CondJmp(FILE *F, const ir_node *irn) {
485 const ir_edge_t *edge;
486 char buf[SNPRINTF_BUF_LEN];
488 edge = get_irn_out_edge_first(irn);
489 proj = get_edge_src_irn(edge);
490 assert(is_Proj(proj) && "CondJmp with a non-Proj");
492 if (get_Proj_proj(proj) == 1) {
493 fprintf(F, "\tj%s %s\t\t\t/* cmp(a, b) == TRUE */\n",
494 get_cmp_suffix(get_ia32_pncode(irn), !mode_is_signed(get_irn_mode(get_irn_n(irn, 0)))),
495 get_cfop_target(proj, buf));
498 fprintf(F, "\tjn%s %s\t\t\t/* cmp(a, b) == FALSE */\n",
499 get_cmp_suffix(get_ia32_pncode(irn), !mode_is_signed(get_irn_mode(get_irn_n(irn, 0)))),
500 get_cfop_target(proj, buf));
503 edge = get_irn_out_edge_next(irn, edge);
505 proj = get_edge_src_irn(edge);
506 assert(is_Proj(proj) && "CondJmp with a non-Proj");
507 fprintf(F, "\tjmp %s\t\t\t/* otherwise */\n", get_cfop_target(proj, buf));
512 * Emits code for conditional jump with two variables.
514 static void emit_ia32_CondJmp(const ir_node *irn, emit_env_t *env) {
517 lc_efprintf(ia32_get_arg_env(), F, "\tcmp %s\t\t\t/* CondJmp(%+F, %+F) */\n",
518 ia32_emit_binop(irn), get_irn_n(irn, 0), get_irn_n(irn, 1));
519 finish_CondJmp(F, irn);
523 * Emits code for conditional jump with immediate.
525 void emit_ia32_CondJmp_i(const ir_node *irn, emit_env_t *env) {
528 lc_efprintf(ia32_get_arg_env(), F, "\tcmp %s\t\t\t/* CondJmp_i(%+F) */\n",
529 ia32_emit_binop(irn), get_irn_n(irn, 0));
530 finish_CondJmp(F, irn);
535 /*********************************************************
538 * ___ _ __ ___ _| |_ _ _ _ _ __ ___ _ __ ___
539 * / _ \ '_ ` _ \| | __| | | | | | '_ ` _ \| '_ \/ __|
540 * | __/ | | | | | | |_ | | |_| | | | | | | |_) \__ \
541 * \___|_| |_| |_|_|\__| | |\__,_|_| |_| |_| .__/|___/
544 *********************************************************/
546 /* jump table entry (target and corresponding number) */
547 typedef struct _branch_t {
552 /* jump table for switch generation */
553 typedef struct _jmp_tbl_t {
554 ir_node *defProj; /**< default target */
555 int min_value; /**< smallest switch case */
556 int max_value; /**< largest switch case */
557 int num_branches; /**< number of jumps */
558 char *label; /**< label of the jump table */
559 branch_t *branches; /**< jump array */
563 * Compare two variables of type branch_t. Used to sort all switch cases
565 static int ia32_cmp_branch_t(const void *a, const void *b) {
566 branch_t *b1 = (branch_t *)a;
567 branch_t *b2 = (branch_t *)b;
569 if (b1->value <= b2->value)
576 * Emits code for a SwitchJmp (creates a jump table if
577 * possible otherwise a cmp-jmp cascade). Port from
580 void emit_ia32_SwitchJmp(const ir_node *irn, emit_env_t *emit_env) {
581 unsigned long interval;
582 char buf[SNPRINTF_BUF_LEN];
583 int last_value, i, pn, do_jmp_tbl = 1;
586 const ir_edge_t *edge;
587 const lc_arg_env_t *env = ia32_get_arg_env();
588 FILE *F = emit_env->out;
590 /* fill the table structure */
591 tbl.label = xmalloc(SNPRINTF_BUF_LEN);
592 tbl.label = get_unique_label(tbl.label, SNPRINTF_BUF_LEN, "JMPTBL_");
594 tbl.num_branches = get_irn_n_edges(irn);
595 tbl.branches = xcalloc(tbl.num_branches, sizeof(tbl.branches[0]));
596 tbl.min_value = INT_MAX;
597 tbl.max_value = INT_MIN;
600 /* go over all proj's and collect them */
601 foreach_out_edge(irn, edge) {
602 proj = get_edge_src_irn(edge);
603 assert(is_Proj(proj) && "Only proj allowed at SwitchJmp");
605 pn = get_Proj_proj(proj);
607 /* create branch entry */
608 tbl.branches[i].target = proj;
609 tbl.branches[i].value = pn;
611 tbl.min_value = pn < tbl.min_value ? pn : tbl.min_value;
612 tbl.max_value = pn > tbl.max_value ? pn : tbl.max_value;
614 /* check for default proj */
615 if (pn == get_ia32_pncode(irn)) {
616 assert(tbl.defProj == NULL && "found two defProjs at SwitchJmp");
623 /* sort the branches by their number */
624 qsort(tbl.branches, tbl.num_branches, sizeof(tbl.branches[0]), ia32_cmp_branch_t);
626 /* two-complement's magic make this work without overflow */
627 interval = tbl.max_value - tbl.min_value;
629 /* check value interval */
630 if (interval > 16 * 1024) {
634 /* check ratio of value interval to number of branches */
635 if ((float)(interval + 1) / (float)tbl.num_branches > 8.0) {
641 if (tbl.min_value != 0) {
642 lc_efprintf(env, F, "\tcmpl %lu, -%d(%1S)\t\t/* first switch value is not 0 */\n",
643 interval, tbl.min_value, irn);
646 lc_efprintf(env, F, "\tcmpl %lu, %1S\t\t\t/* compare for switch */\n", interval, irn);
649 fprintf(F, "\tja %s\t\t\t/* default jump if out of range */\n", get_cfop_target(tbl.defProj, buf));
651 if (tbl.num_branches > 1) {
654 lc_efprintf(env, F, "\tjmp [%1S*4+%s]\t\t/* get jump table entry as target */\n", irn, tbl.label);
656 fprintf(F, "\t.section\t.rodata\t\t/* start jump table */\n");
657 fprintf(F, "\t.align 4\n");
659 fprintf(F, "%s:\n", tbl.label);
660 fprintf(F, "\t.long %s\t\t\t/* case %d */\n", get_cfop_target(tbl.branches[0].target, buf), tbl.branches[0].value);
662 last_value = tbl.branches[0].value;
663 for (i = 1; i < tbl.num_branches; ++i) {
664 while (++last_value < tbl.branches[i].value) {
665 fprintf(F, "\t.long %s\t\t/* default case */\n", get_cfop_target(tbl.defProj, buf));
667 fprintf(F, "\t.long %s\t\t\t/* case %d */\n", get_cfop_target(tbl.branches[i].target, buf), last_value);
670 fprintf(F, "\t.text\t\t\t\t/* end of jump table */\n");
673 /* one jump is enough */
674 fprintf(F, "\tjmp %s\t\t/* only one case given */\n", get_cfop_target(tbl.branches[0].target, buf));
677 else { // no jump table
678 for (i = 0; i < tbl.num_branches; ++i) {
679 lc_efprintf(env, F, "\tcmpl %d, %1S\t\t\t/* case %d */\n", tbl.branches[i].value, irn, i);
680 fprintf(F, "\tje %s\n", get_cfop_target(tbl.branches[i].target, buf));
683 fprintf(F, "\tjmp %s\t\t\t/* default case */\n", get_cfop_target(tbl.defProj, buf));
693 * Emits code for a unconditional jump.
695 void emit_Jmp(const ir_node *irn, emit_env_t *env) {
698 char buf[SNPRINTF_BUF_LEN];
699 ir_fprintf(F, "\tjmp %s\t\t\t/* Jmp(%+F) */\n", get_cfop_target(irn, buf), get_irn_link(irn));
704 /****************************
707 * _ __ _ __ ___ _ ___
708 * | '_ \| '__/ _ \| |/ __|
709 * | |_) | | | (_) | |\__ \
710 * | .__/|_| \___/| ||___/
713 ****************************/
716 * Emits code for a proj -> node
718 void emit_Proj(const ir_node *irn, emit_env_t *env) {
719 ir_node *pred = get_Proj_pred(irn);
721 if (get_irn_op(pred) == op_Start) {
722 switch(get_Proj_proj(irn)) {
723 case pn_Start_X_initial_exec:
732 /**********************************
735 * | | ___ _ __ _ _| |_) |
736 * | | / _ \| '_ \| | | | _ <
737 * | |___| (_) | |_) | |_| | |_) |
738 * \_____\___/| .__/ \__, |____/
741 **********************************/
744 * Emit movsb/w instructions to make mov count divideable by 4
746 static void emit_CopyB_prolog(FILE *F, int rem, int size) {
747 fprintf(F, "\t/* memcopy %d bytes*/\n", size);
748 fprintf(F, "\tcld\t\t\t\t/* copy direction forward*/\n");
752 fprintf(F, "\tmovsb\t\t\t\t/* memcopy remainder 1 */\n");
755 fprintf(F, "\tmovsw\t\t\t\t/* memcopy remainder 2 */\n");
758 fprintf(F, "\tmovsb\t\t\t\t/* memcopy remainder 3 */\n");
759 fprintf(F, "\tmovsw\t\t\t\t/* memcopy remainder 3 */\n");
765 * Emit rep movsd instruction for memcopy.
767 void emit_ia32_CopyB(const ir_node *irn, emit_env_t *emit_env) {
768 FILE *F = emit_env->out;
769 tarval *tv = get_ia32_Immop_tarval(irn);
770 int rem = get_tarval_long(tv);
771 int size = get_tarval_long(get_ia32_Immop_tarval(get_irn_n(irn, 2)));
773 emit_CopyB_prolog(F, rem, size);
775 fprintf(F, "\trep movsd\t\t\t\t/* memcopy */\n");
779 * Emits unrolled memcopy.
781 void emit_ia32_CopyB_i(const ir_node *irn, emit_env_t *emit_env) {
782 tarval *tv = get_ia32_Immop_tarval(irn);
783 int size = get_tarval_long(tv);
784 FILE *F = emit_env->out;
786 emit_CopyB_prolog(F, size & 0x3, size);
790 fprintf(F, "\tmovsd\t\t\t\t/* memcopy unrolled */\n");
796 /***************************
800 * | | / _ \| '_ \ \ / /
801 * | |___| (_) | | | \ V /
802 * \_____\___/|_| |_|\_/
804 ***************************/
807 * Emit code for conversions (I, FP), (FP, I) and (FP, FP).
809 static void emit_ia32_Conv(const ir_node *irn, emit_env_t *emit_env) {
810 FILE *F = emit_env->out;
811 const lc_arg_env_t *env = ia32_get_arg_env();
812 char *from, *to, buf[64];
813 ir_mode *src_mode, *tgt_mode;
815 src_mode = is_ia32_AddrModeS(irn) ? get_ia32_ls_mode(irn) : get_irn_mode(get_irn_n(irn, 2));
816 tgt_mode = get_ia32_res_mode(irn);
818 from = mode_is_float(src_mode) ? (get_mode_size_bits(src_mode) == 32 ? "ss" : "sd") : "si";
819 to = mode_is_float(tgt_mode) ? (get_mode_size_bits(tgt_mode) == 32 ? "ss" : "sd") : "si";
821 switch(get_ia32_op_type(irn)) {
823 lc_esnprintf(env, buf, sizeof(buf), "%1D, %3S", irn, irn);
826 lc_esnprintf(env, buf, sizeof(buf), "%1D, %s", irn, ia32_emit_am(irn));
829 assert(0 && "unsupported op type for Conv");
832 lc_efprintf(env, F, "\tcvt%s2%s %s\t\t\t/* %+F(%+F, %+F) */\n", from, to, buf, irn, src_mode, tgt_mode);
835 void emit_ia32_Conv_I2FP(const ir_node *irn, emit_env_t *emit_env) {
836 emit_ia32_Conv(irn, emit_env);
839 void emit_ia32_Conv_FP2I(const ir_node *irn, emit_env_t *emit_env) {
840 emit_ia32_Conv(irn, emit_env);
843 void emit_ia32_Conv_FP2FP(const ir_node *irn, emit_env_t *emit_env) {
844 emit_ia32_Conv(irn, emit_env);
849 /*******************************************
852 * | |__ ___ _ __ ___ __| | ___ ___
853 * | '_ \ / _ \ '_ \ / _ \ / _` |/ _ \/ __|
854 * | |_) | __/ | | | (_) | (_| | __/\__ \
855 * |_.__/ \___|_| |_|\___/ \__,_|\___||___/
857 *******************************************/
860 * Emits a backend call
862 void emit_be_Call(const ir_node *irn, emit_env_t *emit_env) {
863 FILE *F = emit_env->out;
864 entity *ent = be_Call_get_entity(irn);
866 fprintf(F, "\tcall ");
869 fprintf(F, "%s", get_entity_name(ent));
872 lc_efprintf(ia32_get_arg_env(), F, "%1D", get_irn_n(irn, be_pos_Call_ptr));
875 ir_fprintf(F, "\t\t\t/* %+F (be_Call) */\n", irn);
879 * Emits code to increase stack pointer.
881 void emit_be_IncSP(const ir_node *irn, emit_env_t *emit_env) {
882 FILE *F = emit_env->out;
883 unsigned offs = be_get_IncSP_offset(irn);
884 be_stack_dir_t dir = be_get_IncSP_direction(irn);
887 lc_efprintf(ia32_get_arg_env(), F, "\tadd %1S,%s%u\t\t\t/* %+F (IncSP) */\n", irn,
888 (dir == be_stack_dir_along) ? " -" : " ", offs, irn);
891 fprintf(F, "\t\t\t\t\t/* omitted IncSP with 0 */\n");
896 * Emits code to set stack pointer.
898 void emit_be_SetSP(const ir_node *irn, emit_env_t *emit_env) {
899 FILE *F = emit_env->out;
901 lc_efprintf(ia32_get_arg_env(), F, "\tmov %1D, %3S\t\t\t/* restore SP */\n", irn, irn);
905 * Emits code for Copy.
907 void emit_be_Copy(const ir_node *irn, emit_env_t *emit_env) {
908 FILE *F = emit_env->out;
910 lc_efprintf(ia32_get_arg_env(), F, "\tmov %1D, %1S\t\t\t/* %+F */\n", irn, irn, irn);
914 * Emits code for exchange.
916 void emit_be_Perm(const ir_node *irn, emit_env_t *emit_env) {
917 FILE *F = emit_env->out;
919 lc_efprintf(ia32_get_arg_env(), F, "\txchg %1S, %2S\t\t\t/* %+F(%1A, %2A) */\n", irn, irn, irn);
922 /***********************************************************************************
925 * _ __ ___ __ _ _ _ __ | |_ _ __ __ _ _ __ ___ _____ _____ _ __| | __
926 * | '_ ` _ \ / _` | | '_ \ | _| '__/ _` | '_ ` _ \ / _ \ \ /\ / / _ \| '__| |/ /
927 * | | | | | | (_| | | | | | | | | | | (_| | | | | | | __/\ V V / (_) | | | <
928 * |_| |_| |_|\__,_|_|_| |_| |_| |_| \__,_|_| |_| |_|\___| \_/\_/ \___/|_| |_|\_\
930 ***********************************************************************************/
933 * Enters the emitter functions for handled nodes into the generic
934 * pointer of an opcode.
936 static void ia32_register_emitters(void) {
938 #define IA32_EMIT(a) op_ia32_##a->ops.generic = (op_func)emit_ia32_##a
939 #define EMIT(a) op_##a->ops.generic = (op_func)emit_##a
940 #define BE_EMIT(a) op_be_##a->ops.generic = (op_func)emit_be_##a
942 /* first clear the generic function pointer for all ops */
943 clear_irp_opcodes_generic_func();
945 /* register all emitter functions defined in spec */
946 ia32_register_spec_emitters();
948 /* other ia32 emitter functions */
950 IA32_EMIT(SwitchJmp);
953 IA32_EMIT(Conv_I2FP);
954 IA32_EMIT(Conv_FP2I);
955 IA32_EMIT(Conv_FP2FP);
974 * Emits code for a node.
976 static void ia32_emit_node(const ir_node *irn, void *env) {
977 emit_env_t *emit_env = env;
978 firm_dbg_module_t *mod = emit_env->mod;
979 FILE *F = emit_env->out;
980 ir_op *op = get_irn_op(irn);
982 DBG((mod, LEVEL_1, "emitting code for %+F\n", irn));
984 if (op->ops.generic) {
985 void (*emit)(const ir_node *, void *) = (void (*)(const ir_node *, void *))op->ops.generic;
989 ir_fprintf(F, "\t\t\t\t\t/* %+F */\n", irn);
993 * Walks over the nodes in a block connected by scheduling edges
994 * and emits code for each node.
996 static void ia32_gen_block(ir_node *block, void *env) {
999 if (! is_Block(block))
1002 fprintf(((emit_env_t *)env)->out, "BLOCK_%ld:\n", get_irn_node_nr(block));
1003 sched_foreach(block, irn) {
1004 ia32_emit_node(irn, env);
1010 * Emits code for function start.
1012 static void ia32_emit_func_prolog(FILE *F, ir_graph *irg) {
1013 const char *irg_name = get_entity_name(get_irg_entity(irg));
1015 fprintf(F, "\t.text\n");
1016 fprintf(F, ".globl %s\n", irg_name);
1017 fprintf(F, "\t.type\t%s, @function\n", irg_name);
1018 fprintf(F, "%s:\n", irg_name);
1022 * Emits code for function end
1024 static void ia32_emit_func_epilog(FILE *F, ir_graph *irg) {
1025 const char *irg_name = get_entity_name(get_irg_entity(irg));
1027 fprintf(F, "\tret\n");
1028 fprintf(F, "\t.size\t%s, .-%s\n\n", irg_name, irg_name);
1032 * Sets labels for control flow nodes (jump target)
1033 * TODO: Jump optimization
1035 static void ia32_gen_labels(ir_node *block, void *env) {
1037 int n = get_Block_n_cfgpreds(block);
1039 for (n--; n >= 0; n--) {
1040 pred = get_Block_cfgpred(block, n);
1041 set_irn_link(pred, block);
1046 * Main driver. Emits the code for one routine.
1048 void ia32_gen_routine(FILE *F, ir_graph *irg, const ia32_code_gen_t *cg) {
1049 emit_env_t emit_env;
1051 emit_env.mod = firm_dbg_register("ir.be.codegen.ia32");
1053 emit_env.arch_env = cg->arch_env;
1056 /* set the global arch_env (needed by print hooks) */
1057 arch_env = cg->arch_env;
1059 ia32_register_emitters();
1061 ia32_emit_func_prolog(F, irg);
1062 irg_block_walk_graph(irg, ia32_gen_labels, NULL, &emit_env);
1063 irg_walk_blkwise_graph(irg, NULL, ia32_gen_block, &emit_env);
1064 ia32_emit_func_epilog(F, irg);