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 * Formated print of commands and comments.
389 static void ia32_fprintf_format(FILE *F, char *cmd_buf, char *cmnt_buf) {
390 fprintf(F, "\t%-35s %-60s\n", cmd_buf, cmnt_buf);
396 * Add a number to a prefix. This number will not be used a second time.
398 static char *get_unique_label(char *buf, size_t buflen, const char *prefix) {
399 static unsigned long id = 0;
400 snprintf(buf, buflen, "%s%lu", prefix, ++id);
406 /*************************************************
409 * ___ _ __ ___ _| |_ ___ ___ _ __ __| |
410 * / _ \ '_ ` _ \| | __| / __/ _ \| '_ \ / _` |
411 * | __/ | | | | | | |_ | (_| (_) | | | | (_| |
412 * \___|_| |_| |_|_|\__| \___\___/|_| |_|\__,_|
414 *************************************************/
417 #define IA32_DO_EMIT ia32_fprintf_format(F, cmd_buf, cmnt_buf)
420 * coding of conditions
422 struct cmp2conditon_t {
428 * positive conditions for signed compares
430 static const struct cmp2conditon_t cmp2condition_s[] = {
431 { NULL, pn_Cmp_False }, /* always false */
432 { "e", pn_Cmp_Eq }, /* == */
433 { "l", pn_Cmp_Lt }, /* < */
434 { "le", pn_Cmp_Le }, /* <= */
435 { "g", pn_Cmp_Gt }, /* > */
436 { "ge", pn_Cmp_Ge }, /* >= */
437 { "ne", pn_Cmp_Lg }, /* != */
438 { "ordered", pn_Cmp_Leg }, /* Floating point: ordered */
439 { "unordered", pn_Cmp_Uo }, /* FLoting point: unordered */
440 { "unordered or ==", pn_Cmp_Ue }, /* Floating point: unordered or == */
441 { "unordered or <", pn_Cmp_Ul }, /* Floating point: unordered or < */
442 { "unordered or <=", pn_Cmp_Ule }, /* Floating point: unordered or <= */
443 { "unordered or >", pn_Cmp_Ug }, /* Floating point: unordered or > */
444 { "unordered or >=", pn_Cmp_Uge }, /* Floating point: unordered or >= */
445 { "unordered or !=", pn_Cmp_Ne }, /* Floating point: unordered or != */
446 { NULL, pn_Cmp_True }, /* always true */
450 * positive conditions for unsigned compares
452 static const struct cmp2conditon_t cmp2condition_u[] = {
453 { NULL, pn_Cmp_False }, /* always false */
454 { "e", pn_Cmp_Eq }, /* == */
455 { "b", pn_Cmp_Lt }, /* < */
456 { "be", pn_Cmp_Le }, /* <= */
457 { "a", pn_Cmp_Gt }, /* > */
458 { "ae", pn_Cmp_Ge }, /* >= */
459 { "ne", pn_Cmp_Lg }, /* != */
460 { "ordered", pn_Cmp_Leg }, /* Floating point: ordered */
461 { "unordered", pn_Cmp_Uo }, /* FLoting point: unordered */
462 { "unordered or ==", pn_Cmp_Ue }, /* Floating point: unordered or == */
463 { "unordered or <", pn_Cmp_Ul }, /* Floating point: unordered or < */
464 { "unordered or <=", pn_Cmp_Ule }, /* Floating point: unordered or <= */
465 { "unordered or >", pn_Cmp_Ug }, /* Floating point: unordered or > */
466 { "unordered or >=", pn_Cmp_Uge }, /* Floating point: unordered or >= */
467 { "unordered or !=", pn_Cmp_Ne }, /* Floating point: unordered or != */
468 { NULL, pn_Cmp_True }, /* always true */
472 * returns the condition code
474 static const char *get_cmp_suffix(int cmp_code, int unsigned_cmp)
476 assert(cmp2condition_s[cmp_code].num == cmp_code);
477 assert(cmp2condition_u[cmp_code].num == cmp_code);
479 return unsigned_cmp ? cmp2condition_u[cmp_code & 7].name : cmp2condition_s[cmp_code & 7].name;
483 * Returns the target label for a control flow node.
485 static char *get_cfop_target(const ir_node *irn, char *buf) {
486 ir_node *bl = get_irn_link(irn);
488 snprintf(buf, SNPRINTF_BUF_LEN, "BLOCK_%ld", get_irn_node_nr(bl));
493 * Emits the jump sequence for a conditional jump (cmp + jmp_true + jmp_false)
495 static void finish_CondJmp(FILE *F, const ir_node *irn) {
497 const ir_edge_t *edge;
498 char buf[SNPRINTF_BUF_LEN];
499 char cmd_buf[SNPRINTF_BUF_LEN];
500 char cmnt_buf[SNPRINTF_BUF_LEN];
502 edge = get_irn_out_edge_first(irn);
503 proj = get_edge_src_irn(edge);
504 assert(is_Proj(proj) && "CondJmp with a non-Proj");
506 if (get_Proj_proj(proj) == 1) {
507 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "j%s %s",
508 get_cmp_suffix(get_ia32_pncode(irn), !mode_is_signed(get_irn_mode(get_irn_n(irn, 0)))),
509 get_cfop_target(proj, buf));
510 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* cmp(a, b) == TRUE */");
513 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "jn%s %s",
514 get_cmp_suffix(get_ia32_pncode(irn), !mode_is_signed(get_irn_mode(get_irn_n(irn, 0)))),
515 get_cfop_target(proj, buf));
516 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* cmp(a, b) == FALSE */");
521 edge = get_irn_out_edge_next(irn, edge);
523 proj = get_edge_src_irn(edge);
524 assert(is_Proj(proj) && "CondJmp with a non-Proj");
525 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "jmp %s", get_cfop_target(proj, buf));
526 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* otherwise */");
533 * Emits code for conditional jump.
535 static void CondJmp_emitter(const ir_node *irn, emit_env_t *env) {
537 char cmd_buf[SNPRINTF_BUF_LEN];
538 char cmnt_buf[SNPRINTF_BUF_LEN];
540 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "cmp %s", ia32_emit_binop(irn));
541 lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* %+F */", irn);
543 finish_CondJmp(F, irn);
547 * Emits code for conditional jump with two variables.
549 static void emit_ia32_CondJmp(const ir_node *irn, emit_env_t *env) {
550 CondJmp_emitter(irn, env);
554 * Emits code for conditional jump with immediate.
556 void emit_ia32_CondJmp_i(const ir_node *irn, emit_env_t *env) {
557 CondJmp_emitter(irn, env);
562 /*********************************************************
565 * ___ _ __ ___ _| |_ _ _ _ _ __ ___ _ __ ___
566 * / _ \ '_ ` _ \| | __| | | | | | '_ ` _ \| '_ \/ __|
567 * | __/ | | | | | | |_ | | |_| | | | | | | |_) \__ \
568 * \___|_| |_| |_|_|\__| | |\__,_|_| |_| |_| .__/|___/
571 *********************************************************/
573 /* jump table entry (target and corresponding number) */
574 typedef struct _branch_t {
579 /* jump table for switch generation */
580 typedef struct _jmp_tbl_t {
581 ir_node *defProj; /**< default target */
582 int min_value; /**< smallest switch case */
583 int max_value; /**< largest switch case */
584 int num_branches; /**< number of jumps */
585 char *label; /**< label of the jump table */
586 branch_t *branches; /**< jump array */
590 * Compare two variables of type branch_t. Used to sort all switch cases
592 static int ia32_cmp_branch_t(const void *a, const void *b) {
593 branch_t *b1 = (branch_t *)a;
594 branch_t *b2 = (branch_t *)b;
596 if (b1->value <= b2->value)
603 * Emits code for a SwitchJmp (creates a jump table if
604 * possible otherwise a cmp-jmp cascade). Port from
607 void emit_ia32_SwitchJmp(const ir_node *irn, emit_env_t *emit_env) {
608 unsigned long interval;
609 char buf[SNPRINTF_BUF_LEN];
610 int last_value, i, pn, do_jmp_tbl = 1;
613 const ir_edge_t *edge;
614 const lc_arg_env_t *env = ia32_get_arg_env();
615 FILE *F = emit_env->out;
616 char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
618 /* fill the table structure */
619 tbl.label = xmalloc(SNPRINTF_BUF_LEN);
620 tbl.label = get_unique_label(tbl.label, SNPRINTF_BUF_LEN, "JMPTBL_");
622 tbl.num_branches = get_irn_n_edges(irn);
623 tbl.branches = xcalloc(tbl.num_branches, sizeof(tbl.branches[0]));
624 tbl.min_value = INT_MAX;
625 tbl.max_value = INT_MIN;
628 /* go over all proj's and collect them */
629 foreach_out_edge(irn, edge) {
630 proj = get_edge_src_irn(edge);
631 assert(is_Proj(proj) && "Only proj allowed at SwitchJmp");
633 pn = get_Proj_proj(proj);
635 /* create branch entry */
636 tbl.branches[i].target = proj;
637 tbl.branches[i].value = pn;
639 tbl.min_value = pn < tbl.min_value ? pn : tbl.min_value;
640 tbl.max_value = pn > tbl.max_value ? pn : tbl.max_value;
642 /* check for default proj */
643 if (pn == get_ia32_pncode(irn)) {
644 assert(tbl.defProj == NULL && "found two defProjs at SwitchJmp");
651 /* sort the branches by their number */
652 qsort(tbl.branches, tbl.num_branches, sizeof(tbl.branches[0]), ia32_cmp_branch_t);
654 /* two-complement's magic make this work without overflow */
655 interval = tbl.max_value - tbl.min_value;
657 /* check value interval */
658 if (interval > 16 * 1024) {
662 /* check ratio of value interval to number of branches */
663 if ((float)(interval + 1) / (float)tbl.num_branches > 8.0) {
669 if (tbl.min_value != 0) {
670 lc_esnprintf(env, cmd_buf, SNPRINTF_BUF_LEN, "cmpl %lu, -%d(%1S)",
671 interval, tbl.min_value, irn);
672 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* first switch value is not 0 */");
677 lc_esnprintf(env, cmd_buf, SNPRINTF_BUF_LEN, "cmpl %lu, %1S", interval, irn);
678 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* compare for switch */");
683 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "ja %s", get_cfop_target(tbl.defProj, buf));
684 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* default jump if out of range */");
687 if (tbl.num_branches > 1) {
690 lc_esnprintf(env, cmd_buf, SNPRINTF_BUF_LEN, "jmp [%1S*4+%s]", irn, tbl.label);
691 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* get jump table entry as target */");
694 fprintf(F, "\t.section\t.rodata\n");
695 fprintf(F, "\t.align 4\n");
697 fprintf(F, "%s:\n", tbl.label);
699 snprintf(cmd_buf, SNPRINTF_BUF_LEN, ".long %s", get_cfop_target(tbl.branches[0].target, buf));
700 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* case %d */\n", tbl.branches[0].value);
703 last_value = tbl.branches[0].value;
704 for (i = 1; i < tbl.num_branches; ++i) {
705 while (++last_value < tbl.branches[i].value) {
706 snprintf(cmd_buf, SNPRINTF_BUF_LEN, ".long %s", get_cfop_target(tbl.defProj, buf));
707 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* default case */");
710 snprintf(cmd_buf, SNPRINTF_BUF_LEN, ".long %s", get_cfop_target(tbl.branches[i].target, buf), last_value);
711 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* case %d */", last_value);
715 fprintf(F, "\t.text");
718 /* one jump is enough */
719 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "jmp %s", get_cfop_target(tbl.branches[0].target, buf));
720 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* only one case given */");
724 else { // no jump table
725 for (i = 0; i < tbl.num_branches; ++i) {
726 lc_esnprintf(env, cmd_buf, SNPRINTF_BUF_LEN, "cmpl %d, %1S", tbl.branches[i].value, irn);
727 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* case %d */", i);
729 fprintf(F, "\tje %s\n", get_cfop_target(tbl.branches[i].target, buf));
732 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "jmp %s", get_cfop_target(tbl.defProj, buf));
733 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* default case */");
744 * Emits code for a unconditional jump.
746 void emit_Jmp(const ir_node *irn, emit_env_t *env) {
748 char buf[SNPRINTF_BUF_LEN], cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
750 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "jmp %s", get_cfop_target(irn, buf), get_irn_link(irn));
751 lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* %+F(%+F) */", irn, get_irn_link(irn));
757 /****************************
760 * _ __ _ __ ___ _ ___
761 * | '_ \| '__/ _ \| |/ __|
762 * | |_) | | | (_) | |\__ \
763 * | .__/|_| \___/| ||___/
766 ****************************/
769 * Emits code for a proj -> node
771 void emit_Proj(const ir_node *irn, emit_env_t *env) {
772 ir_node *pred = get_Proj_pred(irn);
774 if (get_irn_op(pred) == op_Start) {
775 switch(get_Proj_proj(irn)) {
776 case pn_Start_X_initial_exec:
785 /**********************************
788 * | | ___ _ __ _ _| |_) |
789 * | | / _ \| '_ \| | | | _ <
790 * | |___| (_) | |_) | |_| | |_) |
791 * \_____\___/| .__/ \__, |____/
794 **********************************/
797 * Emit movsb/w instructions to make mov count divideable by 4
799 static void emit_CopyB_prolog(FILE *F, int rem, int size) {
800 char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
802 fprintf(F, "\t/* memcopy %d bytes*/\n", size);
804 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "cld");
805 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* copy direction forward*/");
810 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "movsb");
811 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* memcopy remainder 1 */");
814 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "movsw");
815 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* memcopy remainder 2 */");
818 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "movsb");
819 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* memcopy remainder 3 */");
821 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "movsw");
822 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* memcopy remainder 3 */");
830 * Emit rep movsd instruction for memcopy.
832 void emit_ia32_CopyB(const ir_node *irn, emit_env_t *emit_env) {
833 FILE *F = emit_env->out;
834 tarval *tv = get_ia32_Immop_tarval(irn);
835 int rem = get_tarval_long(tv);
836 int size = get_tarval_long(get_ia32_Immop_tarval(get_irn_n(irn, 2)));
837 char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
839 emit_CopyB_prolog(F, rem, size);
841 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "rep movsd");
842 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* memcopy */");
847 * Emits unrolled memcopy.
849 void emit_ia32_CopyB_i(const ir_node *irn, emit_env_t *emit_env) {
850 tarval *tv = get_ia32_Immop_tarval(irn);
851 int size = get_tarval_long(tv);
852 FILE *F = emit_env->out;
853 char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
855 emit_CopyB_prolog(F, size & 0x3, size);
859 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "movsd");
860 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* memcopy unrolled */");
867 /***************************
871 * | | / _ \| '_ \ \ / /
872 * | |___| (_) | | | \ V /
873 * \_____\___/|_| |_|\_/
875 ***************************/
878 * Emit code for conversions (I, FP), (FP, I) and (FP, FP).
880 static void emit_ia32_Conv(const ir_node *irn, emit_env_t *emit_env) {
881 FILE *F = emit_env->out;
882 const lc_arg_env_t *env = ia32_get_arg_env();
883 char *from, *to, buf[64];
884 ir_mode *src_mode, *tgt_mode;
885 char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
887 src_mode = is_ia32_AddrModeS(irn) ? get_ia32_ls_mode(irn) : get_irn_mode(get_irn_n(irn, 2));
888 tgt_mode = get_ia32_res_mode(irn);
890 from = mode_is_float(src_mode) ? (get_mode_size_bits(src_mode) == 32 ? "ss" : "sd") : "si";
891 to = mode_is_float(tgt_mode) ? (get_mode_size_bits(tgt_mode) == 32 ? "ss" : "sd") : "si";
893 switch(get_ia32_op_type(irn)) {
895 lc_esnprintf(env, buf, sizeof(buf), "%1D, %3S", irn, irn);
898 lc_esnprintf(env, buf, sizeof(buf), "%1D, %s", irn, ia32_emit_am(irn));
901 assert(0 && "unsupported op type for Conv");
904 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "cvt%s2%s %s", from, to, buf);
905 lc_esnprintf(env, cmnt_buf, SNPRINTF_BUF_LEN, "/* %+F(%+F, %+F) */", irn, src_mode, tgt_mode);
909 void emit_ia32_Conv_I2FP(const ir_node *irn, emit_env_t *emit_env) {
910 emit_ia32_Conv(irn, emit_env);
913 void emit_ia32_Conv_FP2I(const ir_node *irn, emit_env_t *emit_env) {
914 emit_ia32_Conv(irn, emit_env);
917 void emit_ia32_Conv_FP2FP(const ir_node *irn, emit_env_t *emit_env) {
918 emit_ia32_Conv(irn, emit_env);
923 /*******************************************
926 * | |__ ___ _ __ ___ __| | ___ ___
927 * | '_ \ / _ \ '_ \ / _ \ / _` |/ _ \/ __|
928 * | |_) | __/ | | | (_) | (_| | __/\__ \
929 * |_.__/ \___|_| |_|\___/ \__,_|\___||___/
931 *******************************************/
934 * Emits a backend call
936 void emit_be_Call(const ir_node *irn, emit_env_t *emit_env) {
937 FILE *F = emit_env->out;
938 entity *ent = be_Call_get_entity(irn);
939 char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
942 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "call %s", get_entity_name(ent));
945 lc_esnprintf(ia32_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "%1D", get_irn_n(irn, be_pos_Call_ptr));
948 lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* %+F (be_Call) */", irn);
954 * Emits code to increase stack pointer.
956 void emit_be_IncSP(const ir_node *irn, emit_env_t *emit_env) {
957 FILE *F = emit_env->out;
958 unsigned offs = be_get_IncSP_offset(irn);
959 be_stack_dir_t dir = be_get_IncSP_direction(irn);
960 char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
963 lc_esnprintf(ia32_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "add %1S,%s%u", irn,
964 (dir == be_stack_dir_along) ? " -" : " ", offs);
965 lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* %+F (IncSP) */", irn);
968 snprintf(cmd_buf, SNPRINTF_BUF_LEN, " ");
969 lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* omitted %+F (IncSP) with 0 */", irn);
976 * Emits code to set stack pointer.
978 void emit_be_SetSP(const ir_node *irn, emit_env_t *emit_env) {
979 FILE *F = emit_env->out;
980 char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
982 lc_esnprintf(ia32_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "mov %1D, %3S", irn, irn);
983 lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* %+F (restore SP) */", irn);
988 * Emits code for Copy.
990 void emit_be_Copy(const ir_node *irn, emit_env_t *emit_env) {
991 FILE *F = emit_env->out;
992 char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
994 lc_esnprintf(ia32_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "mov %1D, %1S", irn, irn);
995 lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* %+F */", irn);
1000 * Emits code for exchange.
1002 void emit_be_Perm(const ir_node *irn, emit_env_t *emit_env) {
1003 FILE *F = emit_env->out;
1004 char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
1006 lc_esnprintf(ia32_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "xchg %1S, %2S", irn, irn);
1007 lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* %+F(%1A, %2A) */", irn, irn, irn);
1011 /***********************************************************************************
1014 * _ __ ___ __ _ _ _ __ | |_ _ __ __ _ _ __ ___ _____ _____ _ __| | __
1015 * | '_ ` _ \ / _` | | '_ \ | _| '__/ _` | '_ ` _ \ / _ \ \ /\ / / _ \| '__| |/ /
1016 * | | | | | | (_| | | | | | | | | | | (_| | | | | | | __/\ V V / (_) | | | <
1017 * |_| |_| |_|\__,_|_|_| |_| |_| |_| \__,_|_| |_| |_|\___| \_/\_/ \___/|_| |_|\_\
1019 ***********************************************************************************/
1022 * Enters the emitter functions for handled nodes into the generic
1023 * pointer of an opcode.
1025 static void ia32_register_emitters(void) {
1027 #define IA32_EMIT(a) op_ia32_##a->ops.generic = (op_func)emit_ia32_##a
1028 #define EMIT(a) op_##a->ops.generic = (op_func)emit_##a
1029 #define BE_EMIT(a) op_be_##a->ops.generic = (op_func)emit_be_##a
1031 /* first clear the generic function pointer for all ops */
1032 clear_irp_opcodes_generic_func();
1034 /* register all emitter functions defined in spec */
1035 ia32_register_spec_emitters();
1037 /* other ia32 emitter functions */
1039 IA32_EMIT(SwitchJmp);
1042 IA32_EMIT(Conv_I2FP);
1043 IA32_EMIT(Conv_FP2I);
1044 IA32_EMIT(Conv_FP2FP);
1046 /* benode emitter */
1063 * Emits code for a node.
1065 static void ia32_emit_node(const ir_node *irn, void *env) {
1066 emit_env_t *emit_env = env;
1067 firm_dbg_module_t *mod = emit_env->mod;
1068 FILE *F = emit_env->out;
1069 ir_op *op = get_irn_op(irn);
1071 DBG((mod, LEVEL_1, "emitting code for %+F\n", irn));
1073 if (op->ops.generic) {
1074 void (*emit)(const ir_node *, void *) = (void (*)(const ir_node *, void *))op->ops.generic;
1078 ir_fprintf(F, "\t%35s /* %+F */\n", " ", irn);
1083 * Walks over the nodes in a block connected by scheduling edges
1084 * and emits code for each node.
1086 static void ia32_gen_block(ir_node *block, void *env) {
1089 if (! is_Block(block))
1092 fprintf(((emit_env_t *)env)->out, "BLOCK_%ld:\n", get_irn_node_nr(block));
1093 sched_foreach(block, irn) {
1094 ia32_emit_node(irn, env);
1100 * Emits code for function start.
1102 static void ia32_emit_func_prolog(FILE *F, ir_graph *irg) {
1103 const char *irg_name = get_entity_name(get_irg_entity(irg));
1105 fprintf(F, "\t.text\n");
1106 fprintf(F, ".globl %s\n", irg_name);
1107 fprintf(F, "\t.type\t%s, @function\n", irg_name);
1108 fprintf(F, "%s:\n", irg_name);
1112 * Emits code for function end
1114 static void ia32_emit_func_epilog(FILE *F, ir_graph *irg) {
1115 const char *irg_name = get_entity_name(get_irg_entity(irg));
1117 fprintf(F, "\tret\n");
1118 fprintf(F, "\t.size\t%s, .-%s\n\n", irg_name, irg_name);
1122 * Sets labels for control flow nodes (jump target)
1123 * TODO: Jump optimization
1125 static void ia32_gen_labels(ir_node *block, void *env) {
1127 int n = get_Block_n_cfgpreds(block);
1129 for (n--; n >= 0; n--) {
1130 pred = get_Block_cfgpred(block, n);
1131 set_irn_link(pred, block);
1136 * Main driver. Emits the code for one routine.
1138 void ia32_gen_routine(FILE *F, ir_graph *irg, const ia32_code_gen_t *cg) {
1139 emit_env_t emit_env;
1141 emit_env.mod = firm_dbg_register("ir.be.codegen.ia32");
1143 emit_env.arch_env = cg->arch_env;
1146 /* set the global arch_env (needed by print hooks) */
1147 arch_env = cg->arch_env;
1149 ia32_register_emitters();
1151 ia32_emit_func_prolog(F, irg);
1152 irg_block_walk_graph(irg, ia32_gen_labels, NULL, &emit_env);
1153 irg_walk_blkwise_graph(irg, NULL, ia32_gen_block, &emit_env);
1154 ia32_emit_func_epilog(F, irg);