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;
48 * Emits registers and/or address mode of a binary operation.
50 char *ia32_emit_binop(const ir_node *n) {
51 static char *buf = NULL;
53 /* verify that this function is never called on non-AM supporting operations */
54 assert(get_ia32_am_support(n) != ia32_am_None && "emit binop expects addressmode support");
57 buf = xcalloc(1, SNPRINTF_BUF_LEN);
60 memset(buf, 0, SNPRINTF_BUF_LEN);
63 switch(get_ia32_op_type(n)) {
65 if (get_ia32_cnst(n)) {
66 lc_esnprintf(ia32_get_arg_env(), buf, SNPRINTF_BUF_LEN, "%3S, %s", n, get_ia32_cnst(n));
69 lc_esnprintf(ia32_get_arg_env(), buf, SNPRINTF_BUF_LEN, "%3S,%4S", n, n);
73 lc_esnprintf(ia32_get_arg_env(), buf, SNPRINTF_BUF_LEN, "%4S, %s", n, ia32_emit_am(n));
76 if (get_ia32_cnst(n)) {
77 lc_esnprintf(ia32_get_arg_env(), buf, SNPRINTF_BUF_LEN, "%s, %s", ia32_emit_am(n), get_ia32_cnst(n));
80 lc_esnprintf(ia32_get_arg_env(), buf, SNPRINTF_BUF_LEN, "%s, %3S", ia32_emit_am(n), n);
84 assert(0 && "unsupported op type");
91 * Emits registers and/or address mode of a unary operation.
93 char *ia32_emit_unop(const ir_node *n) {
94 static char *buf = NULL;
97 buf = xcalloc(1, SNPRINTF_BUF_LEN);
100 memset(buf, 0, SNPRINTF_BUF_LEN);
103 switch(get_ia32_op_type(n)) {
105 lc_esnprintf(ia32_get_arg_env(), buf, SNPRINTF_BUF_LEN, "%1D", n);
108 snprintf(buf, SNPRINTF_BUF_LEN, ia32_emit_am(n));
111 assert(0 && "unsupported op type");
120 char *ia32_emit_am(const ir_node *n) {
121 ia32_am_flavour_t am_flav = get_ia32_am_flavour(n);
125 static struct obstack *obst = NULL;
126 ir_mode *mode = get_ia32_ls_mode(n);
128 if (! is_ia32_Lea(n))
129 assert(mode && "AM node must have ls_mode attribute set.");
132 obst = xcalloc(1, sizeof(*obst));
135 obstack_free(obst, NULL);
138 /* obstack_free with NULL results in an uninitialized obstack */
142 switch (get_mode_size_bits(mode)) {
144 obstack_printf(obst, "BYTE PTR ");
147 obstack_printf(obst, "WORD PTR ");
154 obstack_printf(obst, "[");
156 if (am_flav & ia32_B) {
157 lc_eoprintf(ia32_get_arg_env(), obst, "%1S", n);
161 if (am_flav & ia32_I) {
163 obstack_printf(obst, "+");
166 lc_eoprintf(ia32_get_arg_env(), obst, "%2S", n);
168 if (am_flav & ia32_S) {
169 obstack_printf(obst, "*%d", 1 << get_ia32_am_scale(n));
175 if (am_flav & ia32_O) {
176 obstack_printf(obst, get_ia32_am_offs(n));
179 obstack_printf(obst, "] ");
181 size = obstack_object_size(obst);
182 s = obstack_finish(obst);
188 /*************************************************************
190 * (_) | | / _| | | | |
191 * _ __ _ __ _ _ __ | |_| |_ | |__ ___| |_ __ ___ _ __
192 * | '_ \| '__| | '_ \| __| _| | '_ \ / _ \ | '_ \ / _ \ '__|
193 * | |_) | | | | | | | |_| | | | | | __/ | |_) | __/ |
194 * | .__/|_| |_|_| |_|\__|_| |_| |_|\___|_| .__/ \___|_|
197 *************************************************************/
199 /* We always pass the ir_node which is a pointer. */
200 static int ia32_get_arg_type(const lc_arg_occ_t *occ) {
201 return lc_arg_type_ptr;
206 * Returns the register at in position pos.
208 static const arch_register_t *get_in_reg(const ir_node *irn, int pos) {
210 const arch_register_t *reg = NULL;
212 assert(get_irn_arity(irn) > pos && "Invalid IN position");
214 /* The out register of the operator at position pos is the
215 in register we need. */
216 op = get_irn_n(irn, pos);
218 reg = arch_get_irn_register(arch_env, op);
220 assert(reg && "no in register found");
225 * Returns the register at out position pos.
227 static const arch_register_t *get_out_reg(const ir_node *irn, int pos) {
229 const arch_register_t *reg = NULL;
231 /* 1st case: irn is not of mode_T, so it has only */
232 /* one OUT register -> good */
233 /* 2nd case: irn is of mode_T -> collect all Projs and ask the */
234 /* Proj with the corresponding projnum for the register */
236 if (get_irn_mode(irn) != mode_T) {
237 reg = arch_get_irn_register(arch_env, irn);
239 else if (is_ia32_irn(irn)) {
240 reg = get_ia32_out_reg(irn, pos);
243 const ir_edge_t *edge;
245 foreach_out_edge(irn, edge) {
246 proj = get_edge_src_irn(edge);
247 assert(is_Proj(proj) && "non-Proj from mode_T node");
248 if (get_Proj_proj(proj) == pos) {
249 reg = arch_get_irn_register(arch_env, proj);
255 assert(reg && "no out register found");
265 * Returns the name of the in register at position pos.
267 static const char *get_ia32_reg_name(ir_node *irn, int pos, enum io_direction in_out) {
268 const arch_register_t *reg;
270 if (in_out == IN_REG) {
271 reg = get_in_reg(irn, pos);
274 /* destination address mode nodes don't have outputs */
275 if (is_ia32_irn(irn) && get_ia32_op_type(irn) == ia32_AddrModeD) {
279 reg = get_out_reg(irn, pos);
282 return arch_register_get_name(reg);
286 * Get the register name for a node.
288 static int ia32_get_reg_name(lc_appendable_t *app,
289 const lc_arg_occ_t *occ, const lc_arg_value_t *arg)
292 ir_node *X = arg->v_ptr;
293 int nr = occ->width - 1;
296 return lc_arg_append(app, occ, "(null)", 6);
298 buf = get_ia32_reg_name(X, nr, occ->conversion == 'S' ? IN_REG : OUT_REG);
300 return lc_arg_append(app, occ, buf, strlen(buf));
304 * Returns the tarval, offset or scale of an ia32 as a string.
306 static int ia32_const_to_str(lc_appendable_t *app,
307 const lc_arg_occ_t *occ, const lc_arg_value_t *arg)
310 ir_node *X = arg->v_ptr;
313 return lc_arg_append(app, occ, "(null)", 6);
315 if (occ->conversion == 'C') {
316 buf = get_ia32_cnst(X);
319 buf = get_ia32_am_offs(X);
322 return buf ? lc_arg_append(app, occ, buf, strlen(buf)) : 0;
326 * Determines the SSE suffix depending on the mode.
328 static int ia32_get_mode_suffix(lc_appendable_t *app,
329 const lc_arg_occ_t *occ, const lc_arg_value_t *arg)
331 ir_node *X = arg->v_ptr;
332 ir_mode *mode = get_irn_mode(X);
334 if (mode == mode_T) {
335 mode = is_ia32_AddrModeS(X) || is_ia32_AddrModeD(X) ? get_ia32_ls_mode(X) : get_ia32_res_mode(X);
339 return lc_arg_append(app, occ, "(null)", 6);
341 if (mode_is_float(mode)) {
342 return lc_appendable_chadd(app, get_mode_size_bits(mode) == 32 ? 's' : 'd');
346 return lc_appendable_chadd(app, mode_is_signed(mode) ? 's' : 'z');
351 * Return the ia32 printf arg environment.
352 * We use the firm environment with some additional handlers.
354 const lc_arg_env_t *ia32_get_arg_env(void) {
355 static lc_arg_env_t *env = NULL;
357 static const lc_arg_handler_t ia32_reg_handler = { ia32_get_arg_type, ia32_get_reg_name };
358 static const lc_arg_handler_t ia32_const_handler = { ia32_get_arg_type, ia32_const_to_str };
359 static const lc_arg_handler_t ia32_mode_handler = { ia32_get_arg_type, ia32_get_mode_suffix };
362 /* extend the firm printer */
363 env = firm_get_arg_env();
365 lc_arg_register(env, "ia32:sreg", 'S', &ia32_reg_handler);
366 lc_arg_register(env, "ia32:dreg", 'D', &ia32_reg_handler);
367 lc_arg_register(env, "ia32:cnst", 'C', &ia32_const_handler);
368 lc_arg_register(env, "ia32:offs", 'O', &ia32_const_handler);
369 lc_arg_register(env, "ia32:mode", 'M', &ia32_mode_handler);
377 * Add a number to a prefix. This number will not be used a second time.
379 static char *get_unique_label(char *buf, size_t buflen, const char *prefix) {
380 static unsigned long id = 0;
381 snprintf(buf, buflen, "%s%lu", prefix, ++id);
386 /*************************************************
389 * ___ _ __ ___ _| |_ ___ ___ _ __ __| |
390 * / _ \ '_ ` _ \| | __| / __/ _ \| '_ \ / _` |
391 * | __/ | | | | | | |_ | (_| (_) | | | | (_| |
392 * \___|_| |_| |_|_|\__| \___\___/|_| |_|\__,_|
394 *************************************************/
397 * coding of conditions
399 struct cmp2conditon_t {
405 * positive conditions for signed compares
407 static const struct cmp2conditon_t cmp2condition_s[] = {
408 { NULL, pn_Cmp_False }, /* always false */
409 { "e", pn_Cmp_Eq }, /* == */
410 { "l", pn_Cmp_Lt }, /* < */
411 { "le", pn_Cmp_Le }, /* <= */
412 { "g", pn_Cmp_Gt }, /* > */
413 { "ge", pn_Cmp_Ge }, /* >= */
414 { "ne", pn_Cmp_Lg }, /* != */
415 { "ordered", pn_Cmp_Leg }, /* Floating point: ordered */
416 { "unordered", pn_Cmp_Uo }, /* FLoting point: unordered */
417 { "unordered or ==", pn_Cmp_Ue }, /* Floating point: unordered or == */
418 { "unordered or <", pn_Cmp_Ul }, /* Floating point: unordered or < */
419 { "unordered or <=", pn_Cmp_Ule }, /* Floating point: unordered or <= */
420 { "unordered or >", pn_Cmp_Ug }, /* Floating point: unordered or > */
421 { "unordered or >=", pn_Cmp_Uge }, /* Floating point: unordered or >= */
422 { "unordered or !=", pn_Cmp_Ne }, /* Floating point: unordered or != */
423 { NULL, pn_Cmp_True }, /* always true */
427 * positive conditions for unsigned compares
429 static const struct cmp2conditon_t cmp2condition_u[] = {
430 { NULL, pn_Cmp_False }, /* always false */
431 { "e", pn_Cmp_Eq }, /* == */
432 { "b", pn_Cmp_Lt }, /* < */
433 { "be", pn_Cmp_Le }, /* <= */
434 { "a", pn_Cmp_Gt }, /* > */
435 { "ae", pn_Cmp_Ge }, /* >= */
436 { "ne", pn_Cmp_Lg }, /* != */
437 { "ordered", pn_Cmp_Leg }, /* Floating point: ordered */
438 { "unordered", pn_Cmp_Uo }, /* FLoting point: unordered */
439 { "unordered or ==", pn_Cmp_Ue }, /* Floating point: unordered or == */
440 { "unordered or <", pn_Cmp_Ul }, /* Floating point: unordered or < */
441 { "unordered or <=", pn_Cmp_Ule }, /* Floating point: unordered or <= */
442 { "unordered or >", pn_Cmp_Ug }, /* Floating point: unordered or > */
443 { "unordered or >=", pn_Cmp_Uge }, /* Floating point: unordered or >= */
444 { "unordered or !=", pn_Cmp_Ne }, /* Floating point: unordered or != */
445 { NULL, pn_Cmp_True }, /* always true */
449 * returns the condition code
451 static const char *get_cmp_suffix(int cmp_code, int unsigned_cmp)
453 assert(cmp2condition_s[cmp_code].num == cmp_code);
454 assert(cmp2condition_u[cmp_code].num == cmp_code);
456 return unsigned_cmp ? cmp2condition_u[cmp_code & 7].name : cmp2condition_s[cmp_code & 7].name;
460 * Returns the target label for a control flow node.
462 static char *get_cfop_target(const ir_node *irn, char *buf) {
463 ir_node *bl = get_irn_link(irn);
465 snprintf(buf, SNPRINTF_BUF_LEN, "BLOCK_%ld", get_irn_node_nr(bl));
470 * Emits the jump sequence for a conditional jump (cmp + jmp_true + jmp_false)
472 static void finish_CondJmp(FILE *F, const ir_node *irn) {
474 const ir_edge_t *edge;
475 char buf[SNPRINTF_BUF_LEN];
477 edge = get_irn_out_edge_first(irn);
478 proj = get_edge_src_irn(edge);
479 assert(is_Proj(proj) && "CondJmp with a non-Proj");
481 if (get_Proj_proj(proj) == 1) {
482 fprintf(F, "\tj%s %s\t\t\t/* cmp(a, b) == TRUE */\n",
483 get_cmp_suffix(get_ia32_pncode(irn), !mode_is_signed(get_irn_mode(get_irn_n(irn, 0)))),
484 get_cfop_target(proj, buf));
487 fprintf(F, "\tjn%s %s\t\t\t/* cmp(a, b) == FALSE */\n",
488 get_cmp_suffix(get_ia32_pncode(irn), !mode_is_signed(get_irn_mode(get_irn_n(irn, 0)))),
489 get_cfop_target(proj, buf));
492 edge = get_irn_out_edge_next(irn, edge);
494 proj = get_edge_src_irn(edge);
495 assert(is_Proj(proj) && "CondJmp with a non-Proj");
496 fprintf(F, "\tjmp %s\t\t\t/* otherwise */\n", get_cfop_target(proj, buf));
501 * Emits code for conditional jump with two variables.
503 static void emit_ia32_CondJmp(const ir_node *irn, emit_env_t *env) {
506 lc_efprintf(ia32_get_arg_env(), F, "\tcmp %s\t\t\t/* CondJmp(%+F, %+F) */\n",
507 ia32_emit_binop(irn), get_irn_n(irn, 0), get_irn_n(irn, 1));
508 finish_CondJmp(F, irn);
512 * Emits code for conditional jump with immediate.
514 void emit_ia32_CondJmp_i(const ir_node *irn, emit_env_t *env) {
517 lc_efprintf(ia32_get_arg_env(), F, "\tcmp %s\t\t\t/* CondJmp_i(%+F) */\n",
518 ia32_emit_binop(irn), get_irn_n(irn, 0));
519 finish_CondJmp(F, irn);
524 /*********************************************************
527 * ___ _ __ ___ _| |_ _ _ _ _ __ ___ _ __ ___
528 * / _ \ '_ ` _ \| | __| | | | | | '_ ` _ \| '_ \/ __|
529 * | __/ | | | | | | |_ | | |_| | | | | | | |_) \__ \
530 * \___|_| |_| |_|_|\__| | |\__,_|_| |_| |_| .__/|___/
533 *********************************************************/
535 /* jump table entry (target and corresponding number) */
536 typedef struct _branch_t {
541 /* jump table for switch generation */
542 typedef struct _jmp_tbl_t {
543 ir_node *defProj; /**< default target */
544 int min_value; /**< smallest switch case */
545 int max_value; /**< largest switch case */
546 int num_branches; /**< number of jumps */
547 char *label; /**< label of the jump table */
548 branch_t *branches; /**< jump array */
552 * Compare two variables of type branch_t. Used to sort all switch cases
554 static int ia32_cmp_branch_t(const void *a, const void *b) {
555 branch_t *b1 = (branch_t *)a;
556 branch_t *b2 = (branch_t *)b;
558 if (b1->value <= b2->value)
565 * Emits code for a SwitchJmp (creates a jump table if
566 * possible otherwise a cmp-jmp cascade). Port from
569 void emit_ia32_SwitchJmp(const ir_node *irn, emit_env_t *emit_env) {
570 unsigned long interval;
571 char buf[SNPRINTF_BUF_LEN];
572 int last_value, i, pn, do_jmp_tbl = 1;
575 const ir_edge_t *edge;
576 const lc_arg_env_t *env = ia32_get_arg_env();
577 FILE *F = emit_env->out;
579 /* fill the table structure */
580 tbl.label = xmalloc(SNPRINTF_BUF_LEN);
581 tbl.label = get_unique_label(tbl.label, SNPRINTF_BUF_LEN, "JMPTBL_");
583 tbl.num_branches = get_irn_n_edges(irn);
584 tbl.branches = xcalloc(tbl.num_branches, sizeof(tbl.branches[0]));
585 tbl.min_value = INT_MAX;
586 tbl.max_value = INT_MIN;
589 /* go over all proj's and collect them */
590 foreach_out_edge(irn, edge) {
591 proj = get_edge_src_irn(edge);
592 assert(is_Proj(proj) && "Only proj allowed at SwitchJmp");
594 pn = get_Proj_proj(proj);
596 /* create branch entry */
597 tbl.branches[i].target = proj;
598 tbl.branches[i].value = pn;
600 tbl.min_value = pn < tbl.min_value ? pn : tbl.min_value;
601 tbl.max_value = pn > tbl.max_value ? pn : tbl.max_value;
603 /* check for default proj */
604 if (pn == get_ia32_pncode(irn)) {
605 assert(tbl.defProj == NULL && "found two defProjs at SwitchJmp");
612 /* sort the branches by their number */
613 qsort(tbl.branches, tbl.num_branches, sizeof(tbl.branches[0]), ia32_cmp_branch_t);
615 /* two-complement's magic make this work without overflow */
616 interval = tbl.max_value - tbl.min_value;
618 /* check value interval */
619 if (interval > 16 * 1024) {
623 /* check ratio of value interval to number of branches */
624 if ((float)(interval + 1) / (float)tbl.num_branches > 8.0) {
630 if (tbl.min_value != 0) {
631 lc_efprintf(env, F, "\tcmpl %lu, -%d(%1S)\t\t/* first switch value is not 0 */\n",
632 interval, tbl.min_value, irn);
635 lc_efprintf(env, F, "\tcmpl %lu, %1S\t\t\t/* compare for switch */\n", interval, irn);
638 fprintf(F, "\tja %s\t\t\t/* default jump if out of range */\n", get_cfop_target(tbl.defProj, buf));
640 if (tbl.num_branches > 1) {
643 lc_efprintf(env, F, "\tjmp [%1S*4+%s]\t\t/* get jump table entry as target */\n", irn, tbl.label);
645 fprintf(F, "\t.section\t.rodata\t\t/* start jump table */\n");
646 fprintf(F, "\t.align 4\n");
648 fprintf(F, "%s:\n", tbl.label);
649 fprintf(F, "\t.long %s\t\t\t/* case %d */\n", get_cfop_target(tbl.branches[0].target, buf), tbl.branches[0].value);
651 last_value = tbl.branches[0].value;
652 for (i = 1; i < tbl.num_branches; ++i) {
653 while (++last_value < tbl.branches[i].value) {
654 fprintf(F, "\t.long %s\t\t/* default case */\n", get_cfop_target(tbl.defProj, buf));
656 fprintf(F, "\t.long %s\t\t\t/* case %d */\n", get_cfop_target(tbl.branches[i].target, buf), last_value);
659 fprintf(F, "\t.text\t\t\t\t/* end of jump table */\n");
662 /* one jump is enough */
663 fprintf(F, "\tjmp %s\t\t/* only one case given */\n", get_cfop_target(tbl.branches[0].target, buf));
666 else { // no jump table
667 for (i = 0; i < tbl.num_branches; ++i) {
668 lc_efprintf(env, F, "\tcmpl %d, %1S\t\t\t/* case %d */\n", tbl.branches[i].value, irn, i);
669 fprintf(F, "\tje %s\n", get_cfop_target(tbl.branches[i].target, buf));
672 fprintf(F, "\tjmp %s\t\t\t/* default case */\n", get_cfop_target(tbl.defProj, buf));
682 * Emits code for a unconditional jump.
684 void emit_Jmp(const ir_node *irn, emit_env_t *env) {
687 char buf[SNPRINTF_BUF_LEN];
688 ir_fprintf(F, "\tjmp %s\t\t\t/* Jmp(%+F) */\n", get_cfop_target(irn, buf), get_irn_link(irn));
693 /****************************
696 * _ __ _ __ ___ _ ___
697 * | '_ \| '__/ _ \| |/ __|
698 * | |_) | | | (_) | |\__ \
699 * | .__/|_| \___/| ||___/
702 ****************************/
705 * Emits code for a proj -> node
707 void emit_Proj(const ir_node *irn, emit_env_t *env) {
708 ir_node *pred = get_Proj_pred(irn);
710 if (get_irn_op(pred) == op_Start) {
711 switch(get_Proj_proj(irn)) {
712 case pn_Start_X_initial_exec:
721 /**********************************
724 * | | ___ _ __ _ _| |_) |
725 * | | / _ \| '_ \| | | | _ <
726 * | |___| (_) | |_) | |_| | |_) |
727 * \_____\___/| .__/ \__, |____/
730 **********************************/
733 * Emit movsb/w instructions to make mov count divideable by 4
735 static void emit_CopyB_prolog(FILE *F, int rem, int size) {
736 fprintf(F, "\t/* memcopy %d bytes*/\n", size);
737 fprintf(F, "\tcld\t\t\t\t/* copy direction forward*/\n");
741 fprintf(F, "\tmovsb\t\t\t\t/* memcopy remainder 1 */\n");
744 fprintf(F, "\tmovsw\t\t\t\t/* memcopy remainder 2 */\n");
747 fprintf(F, "\tmovsb\t\t\t\t/* memcopy remainder 3 */\n");
748 fprintf(F, "\tmovsw\t\t\t\t/* memcopy remainder 3 */\n");
754 * Emit rep movsd instruction for memcopy.
756 void emit_ia32_CopyB(const ir_node *irn, emit_env_t *emit_env) {
757 FILE *F = emit_env->out;
758 tarval *tv = get_ia32_Immop_tarval(irn);
759 int rem = get_tarval_long(tv);
760 int size = get_tarval_long(get_ia32_Immop_tarval(get_irn_n(irn, 2)));
762 emit_CopyB_prolog(F, rem, size);
764 fprintf(F, "\trep movsd\t\t\t\t/* memcopy */\n");
768 * Emits unrolled memcopy.
770 void emit_ia32_CopyB_i(const ir_node *irn, emit_env_t *emit_env) {
771 tarval *tv = get_ia32_Immop_tarval(irn);
772 int size = get_tarval_long(tv);
773 FILE *F = emit_env->out;
775 emit_CopyB_prolog(F, size & 0x3, size);
779 fprintf(F, "\tmovsd\t\t\t\t/* memcopy unrolled */\n");
785 /***************************
789 * | | / _ \| '_ \ \ / /
790 * | |___| (_) | | | \ V /
791 * \_____\___/|_| |_|\_/
793 ***************************/
796 * Emit code for conversions (I, FP), (FP, I) and (FP, FP).
798 static void emit_ia32_Conv(const ir_node *irn, emit_env_t *emit_env) {
799 FILE *F = emit_env->out;
800 const lc_arg_env_t *env = ia32_get_arg_env();
801 char *from, *to, buf[64];
802 ir_mode *src_mode, *tgt_mode;
804 src_mode = is_ia32_AddrModeS(irn) ? get_ia32_ls_mode(irn) : get_irn_mode(get_irn_n(irn, 2));
805 tgt_mode = get_ia32_res_mode(irn);
807 from = mode_is_float(src_mode) ? (get_mode_size_bits(src_mode) == 32 ? "ss" : "sd") : "si";
808 to = mode_is_float(tgt_mode) ? (get_mode_size_bits(tgt_mode) == 32 ? "ss" : "sd") : "si";
810 switch(get_ia32_op_type(irn)) {
812 lc_esnprintf(env, buf, sizeof(buf), "%1D, %3S", irn, irn);
815 lc_esnprintf(env, buf, sizeof(buf), "%1D, %s", irn, ia32_emit_am(irn));
818 assert(0 && "unsupported op type for Conv");
821 lc_efprintf(env, F, "\tcvt%s2%s %s\t\t\t/* %+F(%+F, %+F) */\n", from, to, buf, irn, src_mode, tgt_mode);
824 void emit_ia32_Conv_I2FP(const ir_node *irn, emit_env_t *emit_env) {
825 emit_ia32_Conv(irn, emit_env);
828 void emit_ia32_Conv_FP2I(const ir_node *irn, emit_env_t *emit_env) {
829 emit_ia32_Conv(irn, emit_env);
832 void emit_ia32_Conv_FP2FP(const ir_node *irn, emit_env_t *emit_env) {
833 emit_ia32_Conv(irn, emit_env);
838 /*******************************************
841 * | |__ ___ _ __ ___ __| | ___ ___
842 * | '_ \ / _ \ '_ \ / _ \ / _` |/ _ \/ __|
843 * | |_) | __/ | | | (_) | (_| | __/\__ \
844 * |_.__/ \___|_| |_|\___/ \__,_|\___||___/
846 *******************************************/
849 * Emits a backend call
851 void emit_be_Call(const ir_node *irn, emit_env_t *emit_env) {
852 FILE *F = emit_env->out;
853 entity *ent = be_Call_get_entity(irn);
855 fprintf(F, "\tcall ");
858 fprintf(F, "%s", get_entity_name(ent));
861 lc_efprintf(ia32_get_arg_env(), F, "%1D", get_irn_n(irn, be_pos_Call_ptr));
864 ir_fprintf(F, "\t\t\t/* %+F (be_Call) */\n", irn);
868 * Emits code to increase stack pointer.
870 void emit_be_IncSP(const ir_node *irn, emit_env_t *emit_env) {
871 FILE *F = emit_env->out;
872 unsigned offs = be_get_IncSP_offset(irn);
873 be_stack_dir_t dir = be_get_IncSP_direction(irn);
876 lc_efprintf(ia32_get_arg_env(), F, "\tadd %1S,%s%u\t\t\t/* %+F (IncSP) */\n", irn,
877 (dir == be_stack_dir_along) ? " -" : " ", offs, irn);
880 fprintf(F, "\t\t\t\t\t/* omitted IncSP with 0 */\n");
885 * Emits code to set stack pointer.
887 void emit_be_SetSP(const ir_node *irn, emit_env_t *emit_env) {
888 FILE *F = emit_env->out;
890 lc_efprintf(ia32_get_arg_env(), F, "\tmov %1D, %3S\t\t\t/* restore SP */\n", irn, irn);
894 * Emits code for Copy.
896 void emit_be_Copy(const ir_node *irn, emit_env_t *emit_env) {
897 FILE *F = emit_env->out;
899 lc_efprintf(ia32_get_arg_env(), F, "\tmov %1D, %1S\t\t\t/* %+F */\n", irn, irn, irn);
903 * Emits code for exchange.
905 void emit_be_Perm(const ir_node *irn, emit_env_t *emit_env) {
906 FILE *F = emit_env->out;
908 lc_efprintf(ia32_get_arg_env(), F, "\txchg %1S, %2S\t\t\t/* %+F(%1A, %2A) */\n", irn, irn, irn);
911 /***********************************************************************************
914 * _ __ ___ __ _ _ _ __ | |_ _ __ __ _ _ __ ___ _____ _____ _ __| | __
915 * | '_ ` _ \ / _` | | '_ \ | _| '__/ _` | '_ ` _ \ / _ \ \ /\ / / _ \| '__| |/ /
916 * | | | | | | (_| | | | | | | | | | | (_| | | | | | | __/\ V V / (_) | | | <
917 * |_| |_| |_|\__,_|_|_| |_| |_| |_| \__,_|_| |_| |_|\___| \_/\_/ \___/|_| |_|\_\
919 ***********************************************************************************/
922 * Enters the emitter functions for handled nodes into the generic
923 * pointer of an opcode.
925 static void ia32_register_emitters(void) {
927 #define IA32_EMIT(a) op_ia32_##a->ops.generic = (op_func)emit_ia32_##a
928 #define EMIT(a) op_##a->ops.generic = (op_func)emit_##a
929 #define BE_EMIT(a) op_be_##a->ops.generic = (op_func)emit_be_##a
931 /* first clear the generic function pointer for all ops */
932 clear_irp_opcodes_generic_func();
934 /* register all emitter functions defined in spec */
935 ia32_register_spec_emitters();
937 /* other ia32 emitter functions */
939 IA32_EMIT(SwitchJmp);
942 IA32_EMIT(Conv_I2FP);
943 IA32_EMIT(Conv_FP2I);
944 IA32_EMIT(Conv_FP2FP);
963 * Emits code for a node.
965 static void ia32_emit_node(const ir_node *irn, void *env) {
966 emit_env_t *emit_env = env;
967 firm_dbg_module_t *mod = emit_env->mod;
968 FILE *F = emit_env->out;
969 ir_op *op = get_irn_op(irn);
971 DBG((mod, LEVEL_1, "emitting code for %+F\n", irn));
973 if (op->ops.generic) {
974 void (*emit)(const ir_node *, void *) = (void (*)(const ir_node *, void *))op->ops.generic;
978 ir_fprintf(F, "\t\t\t\t\t/* %+F */\n", irn);
983 * Walks over the nodes in a block connected by scheduling edges
984 * and emits code for each node.
986 static void ia32_gen_block(ir_node *block, void *env) {
989 if (! is_Block(block))
992 fprintf(((emit_env_t *)env)->out, "BLOCK_%ld:\n", get_irn_node_nr(block));
993 sched_foreach(block, irn) {
994 ia32_emit_node(irn, env);
1000 * Emits code for function start.
1002 static void ia32_emit_func_prolog(FILE *F, ir_graph *irg) {
1003 const char *irg_name = get_entity_name(get_irg_entity(irg));
1005 fprintf(F, "\t.text\n");
1006 fprintf(F, ".globl %s\n", irg_name);
1007 fprintf(F, "\t.type\t%s, @function\n", irg_name);
1008 fprintf(F, "%s:\n", irg_name);
1012 * Emits code for function end
1014 static void ia32_emit_func_epilog(FILE *F, ir_graph *irg) {
1015 const char *irg_name = get_entity_name(get_irg_entity(irg));
1017 fprintf(F, "\tret\n");
1018 fprintf(F, "\t.size\t%s, .-%s\n\n", irg_name, irg_name);
1022 * Sets labels for control flow nodes (jump target)
1023 * TODO: Jump optimization
1025 static void ia32_gen_labels(ir_node *block, void *env) {
1027 int n = get_Block_n_cfgpreds(block);
1029 for (n--; n >= 0; n--) {
1030 pred = get_Block_cfgpred(block, n);
1031 set_irn_link(pred, block);
1036 * Main driver. Emits the code for one routine.
1038 void ia32_gen_routine(FILE *F, ir_graph *irg, const ia32_code_gen_t *cg) {
1039 emit_env_t emit_env;
1041 emit_env.mod = firm_dbg_register("ir.be.codegen.ia32");
1043 emit_env.arch_env = cg->arch_env;
1046 /* set the global arch_env (needed by print hooks) */
1047 arch_env = cg->arch_env;
1049 ia32_register_emitters();
1051 ia32_emit_func_prolog(F, irg);
1052 irg_block_walk_graph(irg, ia32_gen_labels, NULL, &emit_env);
1053 irg_walk_blkwise_graph(irg, NULL, ia32_gen_block, &emit_env);
1054 ia32_emit_func_epilog(F, irg);