2 * This file implements the node emitter.
22 #include "iredges_t.h"
24 #include "../besched_t.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 #define BLOCK_PREFIX(x) ".L" ## x
43 extern int obstack_printf(struct obstack *obst, char *fmt, ...);
45 #define SNPRINTF_BUF_LEN 128
47 static const arch_env_t *arch_env = NULL;
49 /*************************************************************
51 * (_) | | / _| | | | |
52 * _ __ _ __ _ _ __ | |_| |_ | |__ ___| |_ __ ___ _ __
53 * | '_ \| '__| | '_ \| __| _| | '_ \ / _ \ | '_ \ / _ \ '__|
54 * | |_) | | | | | | | |_| | | | | | __/ | |_) | __/ |
55 * | .__/|_| |_|_| |_|\__|_| |_| |_|\___|_| .__/ \___|_|
58 *************************************************************/
60 /* We always pass the ir_node which is a pointer. */
61 static int ia32_get_arg_type(const lc_arg_occ_t *occ) {
62 return lc_arg_type_ptr;
67 * Returns the register at in position pos.
69 static const arch_register_t *get_in_reg(const ir_node *irn, int pos) {
71 const arch_register_t *reg = NULL;
73 assert(get_irn_arity(irn) > pos && "Invalid IN position");
75 /* The out register of the operator at position pos is the
76 in register we need. */
77 op = get_irn_n(irn, pos);
79 reg = arch_get_irn_register(arch_env, op);
81 assert(reg && "no in register found");
86 * Returns the register at out position pos.
88 static const arch_register_t *get_out_reg(const ir_node *irn, int pos) {
90 const arch_register_t *reg = NULL;
92 /* 1st case: irn is not of mode_T, so it has only */
93 /* one OUT register -> good */
94 /* 2nd case: irn is of mode_T -> collect all Projs and ask the */
95 /* Proj with the corresponding projnum for the register */
97 if (get_irn_mode(irn) != mode_T) {
98 reg = arch_get_irn_register(arch_env, irn);
100 else if (is_ia32_irn(irn)) {
101 reg = get_ia32_out_reg(irn, pos);
104 const ir_edge_t *edge;
106 foreach_out_edge(irn, edge) {
107 proj = get_edge_src_irn(edge);
108 assert(is_Proj(proj) && "non-Proj from mode_T node");
109 if (get_Proj_proj(proj) == pos) {
110 reg = arch_get_irn_register(arch_env, proj);
116 assert(reg && "no out register found");
126 * Returns the name of the in register at position pos.
128 static const char *get_ia32_reg_name(ir_node *irn, int pos, enum io_direction in_out) {
129 const arch_register_t *reg;
131 if (in_out == IN_REG) {
132 reg = get_in_reg(irn, pos);
135 /* destination address mode nodes don't have outputs */
136 if (is_ia32_irn(irn) && get_ia32_op_type(irn) == ia32_AddrModeD) {
140 reg = get_out_reg(irn, pos);
143 return arch_register_get_name(reg);
147 * Get the register name for a node.
149 static int ia32_get_reg_name(lc_appendable_t *app,
150 const lc_arg_occ_t *occ, const lc_arg_value_t *arg)
153 ir_node *X = arg->v_ptr;
154 int nr = occ->width - 1;
157 return lc_arg_append(app, occ, "(null)", 6);
159 buf = get_ia32_reg_name(X, nr, occ->conversion == 'S' ? IN_REG : OUT_REG);
161 return lc_arg_append(app, occ, buf, strlen(buf));
165 * Returns the tarval, offset or scale of an ia32 as a string.
167 static int ia32_const_to_str(lc_appendable_t *app,
168 const lc_arg_occ_t *occ, const lc_arg_value_t *arg)
171 ir_node *X = arg->v_ptr;
174 return lc_arg_append(app, occ, "(null)", 6);
176 if (occ->conversion == 'C') {
177 buf = get_ia32_cnst(X);
180 buf = get_ia32_am_offs(X);
183 return buf ? lc_arg_append(app, occ, buf, strlen(buf)) : 0;
187 * Determines the SSE suffix depending on the mode.
189 static int ia32_get_mode_suffix(lc_appendable_t *app,
190 const lc_arg_occ_t *occ, const lc_arg_value_t *arg)
192 ir_node *X = arg->v_ptr;
193 ir_mode *mode = get_irn_mode(X);
195 if (mode == mode_T) {
196 mode = is_ia32_AddrModeS(X) || is_ia32_AddrModeD(X) ? get_ia32_ls_mode(X) : get_ia32_res_mode(X);
200 return lc_arg_append(app, occ, "(null)", 6);
202 if (mode_is_float(mode)) {
203 return lc_appendable_chadd(app, get_mode_size_bits(mode) == 32 ? 's' : 'd');
207 return lc_appendable_chadd(app, mode_is_signed(mode) ? 's' : 'z');
212 * Return the ia32 printf arg environment.
213 * We use the firm environment with some additional handlers.
215 const lc_arg_env_t *ia32_get_arg_env(void) {
216 static lc_arg_env_t *env = NULL;
218 static const lc_arg_handler_t ia32_reg_handler = { ia32_get_arg_type, ia32_get_reg_name };
219 static const lc_arg_handler_t ia32_const_handler = { ia32_get_arg_type, ia32_const_to_str };
220 static const lc_arg_handler_t ia32_mode_handler = { ia32_get_arg_type, ia32_get_mode_suffix };
223 /* extend the firm printer */
224 env = firm_get_arg_env();
226 lc_arg_register(env, "ia32:sreg", 'S', &ia32_reg_handler);
227 lc_arg_register(env, "ia32:dreg", 'D', &ia32_reg_handler);
228 lc_arg_register(env, "ia32:cnst", 'C', &ia32_const_handler);
229 lc_arg_register(env, "ia32:offs", 'O', &ia32_const_handler);
230 lc_arg_register(env, "ia32:mode", 'M', &ia32_mode_handler);
237 * Emits registers and/or address mode of a binary operation.
239 char *ia32_emit_binop(const ir_node *n, ia32_emit_env_t *env) {
240 static char *buf = NULL;
242 /* verify that this function is never called on non-AM supporting operations */
243 //assert(get_ia32_am_support(n) != ia32_am_None && "emit binop expects addressmode support");
245 #define PRODUCES_RESULT(n) \
246 (!(is_ia32_St(n) || \
247 is_ia32_Store8Bit(n) || \
248 is_ia32_CondJmp(n) || \
249 is_ia32_fCondJmp(n) || \
250 is_ia32_SwitchJmp(n)))
253 buf = xcalloc(1, SNPRINTF_BUF_LEN);
256 memset(buf, 0, SNPRINTF_BUF_LEN);
259 switch(get_ia32_op_type(n)) {
261 if (get_ia32_cnst(n)) {
262 lc_esnprintf(ia32_get_arg_env(), buf, SNPRINTF_BUF_LEN, "%3S, %s", n, get_ia32_cnst(n));
265 const arch_register_t *in1 = get_in_reg(n, 2);
266 const arch_register_t *in2 = get_in_reg(n, 3);
267 const arch_register_t *out = PRODUCES_RESULT(n) ? get_out_reg(n, 0) : NULL;
268 const arch_register_t *in;
270 in = out ? (REGS_ARE_EQUAL(out, in2) ? in1 : in2) : in2;
271 out = out ? out : in1;
273 snprintf(buf, SNPRINTF_BUF_LEN, "%s, %s", \
274 arch_register_get_name(out), arch_register_get_name(in));
278 lc_esnprintf(ia32_get_arg_env(), buf, SNPRINTF_BUF_LEN, "%4S, %s", n, ia32_emit_am(n, env));
281 if (get_ia32_cnst(n)) {
282 lc_esnprintf(ia32_get_arg_env(), buf, SNPRINTF_BUF_LEN, "%s, %s", ia32_emit_am(n, env), get_ia32_cnst(n));
285 const arch_register_t *in1 = get_in_reg(n, 2);
286 const char *reg_name;
287 ir_mode *mode = get_ia32_res_mode(n);
289 mode = mode ? mode : get_ia32_ls_mode(n);
291 switch(get_mode_size_bits(mode)) {
293 reg_name = ia32_get_mapped_reg_name(env->isa->regs_8bit, in1);
296 reg_name = ia32_get_mapped_reg_name(env->isa->regs_16bit, in1);
299 reg_name = arch_register_get_name(in1);
302 assert(0 && "unsupported mode size");
306 lc_esnprintf(ia32_get_arg_env(), buf, SNPRINTF_BUF_LEN, "%s, %s", ia32_emit_am(n, env), reg_name);
310 assert(0 && "unsupported op type");
313 #undef PRODUCES_RESULT
319 * Emits registers and/or address mode of a unary operation.
321 char *ia32_emit_unop(const ir_node *n, ia32_emit_env_t *env) {
322 static char *buf = NULL;
325 buf = xcalloc(1, SNPRINTF_BUF_LEN);
328 memset(buf, 0, SNPRINTF_BUF_LEN);
331 switch(get_ia32_op_type(n)) {
333 lc_esnprintf(ia32_get_arg_env(), buf, SNPRINTF_BUF_LEN, "%1D", n);
336 snprintf(buf, SNPRINTF_BUF_LEN, ia32_emit_am(n, env));
339 assert(0 && "unsupported op type");
346 * Emits address mode.
348 char *ia32_emit_am(const ir_node *n, ia32_emit_env_t *env) {
349 ia32_am_flavour_t am_flav = get_ia32_am_flavour(n);
353 static struct obstack *obst = NULL;
354 ir_mode *mode = get_ia32_ls_mode(n);
356 if (! is_ia32_Lea(n))
357 assert(mode && "AM node must have ls_mode attribute set.");
360 obst = xcalloc(1, sizeof(*obst));
363 obstack_free(obst, NULL);
366 /* obstack_free with NULL results in an uninitialized obstack */
370 switch (get_mode_size_bits(mode)) {
372 obstack_printf(obst, "BYTE ");
375 obstack_printf(obst, "WORD ");
382 obstack_printf(obst, "[");
384 if (am_flav & ia32_B) {
385 lc_eoprintf(ia32_get_arg_env(), obst, "%1S", n);
389 if (am_flav & ia32_I) {
391 obstack_printf(obst, "+");
394 lc_eoprintf(ia32_get_arg_env(), obst, "%2S", n);
396 if (am_flav & ia32_S) {
397 obstack_printf(obst, "*%d", 1 << get_ia32_am_scale(n));
403 if (am_flav & ia32_O) {
404 obstack_printf(obst, get_ia32_am_offs(n));
407 obstack_printf(obst, "] ");
409 size = obstack_object_size(obst);
410 s = obstack_finish(obst);
419 * Formated print of commands and comments.
421 static void ia32_fprintf_format(FILE *F, char *cmd_buf, char *cmnt_buf) {
422 fprintf(F, "\t%-35s %-60s\n", cmd_buf, cmnt_buf);
428 * Add a number to a prefix. This number will not be used a second time.
430 static char *get_unique_label(char *buf, size_t buflen, const char *prefix) {
431 static unsigned long id = 0;
432 snprintf(buf, buflen, "%s%lu", prefix, ++id);
438 /*************************************************
441 * ___ _ __ ___ _| |_ ___ ___ _ __ __| |
442 * / _ \ '_ ` _ \| | __| / __/ _ \| '_ \ / _` |
443 * | __/ | | | | | | |_ | (_| (_) | | | | (_| |
444 * \___|_| |_| |_|_|\__| \___\___/|_| |_|\__,_|
446 *************************************************/
449 #define IA32_DO_EMIT ia32_fprintf_format(F, cmd_buf, cmnt_buf)
452 * coding of conditions
454 struct cmp2conditon_t {
460 * positive conditions for signed compares
462 static const struct cmp2conditon_t cmp2condition_s[] = {
463 { NULL, pn_Cmp_False }, /* always false */
464 { "e", pn_Cmp_Eq }, /* == */
465 { "l", pn_Cmp_Lt }, /* < */
466 { "le", pn_Cmp_Le }, /* <= */
467 { "g", pn_Cmp_Gt }, /* > */
468 { "ge", pn_Cmp_Ge }, /* >= */
469 { "ne", pn_Cmp_Lg }, /* != */
470 { "ordered", pn_Cmp_Leg }, /* Floating point: ordered */
471 { "unordered", pn_Cmp_Uo }, /* FLoting point: unordered */
472 { "unordered or ==", pn_Cmp_Ue }, /* Floating point: unordered or == */
473 { "unordered or <", pn_Cmp_Ul }, /* Floating point: unordered or < */
474 { "unordered or <=", pn_Cmp_Ule }, /* Floating point: unordered or <= */
475 { "unordered or >", pn_Cmp_Ug }, /* Floating point: unordered or > */
476 { "unordered or >=", pn_Cmp_Uge }, /* Floating point: unordered or >= */
477 { "unordered or !=", pn_Cmp_Ne }, /* Floating point: unordered or != */
478 { NULL, pn_Cmp_True }, /* always true */
482 * positive conditions for unsigned compares
484 static const struct cmp2conditon_t cmp2condition_u[] = {
485 { NULL, pn_Cmp_False }, /* always false */
486 { "e", pn_Cmp_Eq }, /* == */
487 { "b", pn_Cmp_Lt }, /* < */
488 { "be", pn_Cmp_Le }, /* <= */
489 { "a", pn_Cmp_Gt }, /* > */
490 { "ae", pn_Cmp_Ge }, /* >= */
491 { "ne", pn_Cmp_Lg }, /* != */
492 { "ordered", pn_Cmp_Leg }, /* Floating point: ordered */
493 { "unordered", pn_Cmp_Uo }, /* FLoting point: unordered */
494 { "unordered or ==", pn_Cmp_Ue }, /* Floating point: unordered or == */
495 { "unordered or <", pn_Cmp_Ul }, /* Floating point: unordered or < */
496 { "unordered or <=", pn_Cmp_Ule }, /* Floating point: unordered or <= */
497 { "unordered or >", pn_Cmp_Ug }, /* Floating point: unordered or > */
498 { "unordered or >=", pn_Cmp_Uge }, /* Floating point: unordered or >= */
499 { "unordered or !=", pn_Cmp_Ne }, /* Floating point: unordered or != */
500 { NULL, pn_Cmp_True }, /* always true */
504 * returns the condition code
506 static const char *get_cmp_suffix(int cmp_code, int unsigned_cmp)
508 assert(cmp2condition_s[cmp_code].num == cmp_code);
509 assert(cmp2condition_u[cmp_code].num == cmp_code);
511 return unsigned_cmp ? cmp2condition_u[cmp_code & 7].name : cmp2condition_s[cmp_code & 7].name;
515 * Returns the target block for a control flow node.
517 static ir_node *get_cfop_target_block(const ir_node *irn) {
518 return get_irn_link(irn);
522 * Returns the target label for a control flow node.
524 static char *get_cfop_target(const ir_node *irn, char *buf) {
525 ir_node *bl = get_cfop_target_block(irn);
527 snprintf(buf, SNPRINTF_BUF_LEN, BLOCK_PREFIX("%ld"), get_irn_node_nr(bl));
532 * Emits the jump sequence for a conditional jump (cmp + jmp_true + jmp_false)
534 static void finish_CondJmp(FILE *F, const ir_node *irn, ir_mode *mode) {
535 const ir_node *proj1, *proj2 = NULL;
536 const ir_node *block, *next_bl = NULL;
537 const ir_edge_t *edge;
538 char buf[SNPRINTF_BUF_LEN];
539 char cmd_buf[SNPRINTF_BUF_LEN];
540 char cmnt_buf[SNPRINTF_BUF_LEN];
542 /* get both Proj's */
543 edge = get_irn_out_edge_first(irn);
544 proj1 = get_edge_src_irn(edge);
545 assert(is_Proj(proj1) && "CondJmp with a non-Proj");
547 edge = get_irn_out_edge_next(irn, edge);
549 proj2 = get_edge_src_irn(edge);
550 assert(is_Proj(proj2) && "CondJmp with a non-Proj");
553 /* for now, the code works for scheduled and non-schedules blocks */
554 block = get_nodes_block(irn);
555 if (proj2 && sched_is_scheduled(block)) {
556 /* we have a block schedule */
557 next_bl = sched_next(block);
559 if (get_cfop_target_block(proj1) == next_bl) {
560 /* exchange both proj's so the second one can be omitted */
561 const ir_node *t = proj1;
567 /* the first Proj must always be created */
568 if (get_Proj_proj(proj1) == pn_Cond_true) {
569 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "j%s %s",
570 get_cmp_suffix(get_ia32_pncode(irn), !mode_is_signed(get_irn_mode(get_irn_n(irn, 0)))),
571 get_cfop_target(proj1, buf));
572 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "; cmp(a, b) == TRUE");
575 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "j%s %s",
576 get_cmp_suffix(get_negated_pnc(get_ia32_pncode(irn), mode),
577 !mode_is_signed(get_irn_mode(get_irn_n(irn, 0)))),
578 get_cfop_target(proj1, buf));
579 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "; cmp(a, b) == FALSE");
583 /* the second Proj might be a fallthrough */
585 if (get_cfop_target_block(proj2) != next_bl) {
586 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "jmp %s", get_cfop_target(proj2, buf));
587 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "; otherwise");
591 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "; fallthrogh %s", get_cfop_target(proj2, buf));
598 * Emits code for conditional jump.
600 static void CondJmp_emitter(const ir_node *irn, ia32_emit_env_t *env) {
602 char cmd_buf[SNPRINTF_BUF_LEN];
603 char cmnt_buf[SNPRINTF_BUF_LEN];
605 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "cmp %s", ia32_emit_binop(irn, env));
606 lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "; %+F", irn);
608 finish_CondJmp(F, irn, get_irn_mode(get_irn_n(irn, 2)));
612 * Emits code for conditional jump with two variables.
614 static void emit_ia32_CondJmp(const ir_node *irn, ia32_emit_env_t *env) {
615 CondJmp_emitter(irn, env);
619 * Emits code for conditional jump with immediate.
621 void emit_ia32_CondJmp_i(const ir_node *irn, ia32_emit_env_t *env) {
622 CondJmp_emitter(irn, env);
626 * Emits code for conditional test and jump.
628 static void TestJmp_emitter(const ir_node *irn, ia32_emit_env_t *env) {
630 char cmd_buf[SNPRINTF_BUF_LEN];
631 char cmnt_buf[SNPRINTF_BUF_LEN];
632 const char *op1 = arch_register_get_name(get_in_reg(irn, 0));
633 const char *op2 = get_ia32_cnst(irn);
636 op2 = arch_register_get_name(get_in_reg(irn, 1));
638 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "test %s, %s ", op1, op2);
639 lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "; %+F", irn);
641 finish_CondJmp(F, irn, get_irn_mode(get_irn_n(irn, 0)));
645 * Emits code for conditional test and jump with two variables.
647 static void emit_ia32_TestJmp(const ir_node *irn, ia32_emit_env_t *env) {
648 TestJmp_emitter(irn, env);
652 * Emits code for conditional test and jump with immediate.
654 static void emit_ia32_TestJmp_i(const ir_node *irn, ia32_emit_env_t *env) {
655 TestJmp_emitter(irn, env);
658 /*********************************************************
661 * ___ _ __ ___ _| |_ _ _ _ _ __ ___ _ __ ___
662 * / _ \ '_ ` _ \| | __| | | | | | '_ ` _ \| '_ \/ __|
663 * | __/ | | | | | | |_ | | |_| | | | | | | |_) \__ \
664 * \___|_| |_| |_|_|\__| | |\__,_|_| |_| |_| .__/|___/
667 *********************************************************/
669 /* jump table entry (target and corresponding number) */
670 typedef struct _branch_t {
675 /* jump table for switch generation */
676 typedef struct _jmp_tbl_t {
677 ir_node *defProj; /**< default target */
678 int min_value; /**< smallest switch case */
679 int max_value; /**< largest switch case */
680 int num_branches; /**< number of jumps */
681 char *label; /**< label of the jump table */
682 branch_t *branches; /**< jump array */
686 * Compare two variables of type branch_t. Used to sort all switch cases
688 static int ia32_cmp_branch_t(const void *a, const void *b) {
689 branch_t *b1 = (branch_t *)a;
690 branch_t *b2 = (branch_t *)b;
692 if (b1->value <= b2->value)
699 * Emits code for a SwitchJmp (creates a jump table if
700 * possible otherwise a cmp-jmp cascade). Port from
703 void emit_ia32_SwitchJmp(const ir_node *irn, ia32_emit_env_t *emit_env) {
704 unsigned long interval;
705 char buf[SNPRINTF_BUF_LEN];
706 int last_value, i, pn, do_jmp_tbl = 1;
709 const ir_edge_t *edge;
710 const lc_arg_env_t *env = ia32_get_arg_env();
711 FILE *F = emit_env->out;
712 char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
714 /* fill the table structure */
715 tbl.label = xmalloc(SNPRINTF_BUF_LEN);
716 tbl.label = get_unique_label(tbl.label, SNPRINTF_BUF_LEN, "JMPTBL_");
718 tbl.num_branches = get_irn_n_edges(irn);
719 tbl.branches = xcalloc(tbl.num_branches, sizeof(tbl.branches[0]));
720 tbl.min_value = INT_MAX;
721 tbl.max_value = INT_MIN;
724 /* go over all proj's and collect them */
725 foreach_out_edge(irn, edge) {
726 proj = get_edge_src_irn(edge);
727 assert(is_Proj(proj) && "Only proj allowed at SwitchJmp");
729 pn = get_Proj_proj(proj);
731 /* create branch entry */
732 tbl.branches[i].target = proj;
733 tbl.branches[i].value = pn;
735 tbl.min_value = pn < tbl.min_value ? pn : tbl.min_value;
736 tbl.max_value = pn > tbl.max_value ? pn : tbl.max_value;
738 /* check for default proj */
739 if (pn == get_ia32_pncode(irn)) {
740 assert(tbl.defProj == NULL && "found two defProjs at SwitchJmp");
747 /* sort the branches by their number */
748 qsort(tbl.branches, tbl.num_branches, sizeof(tbl.branches[0]), ia32_cmp_branch_t);
750 /* two-complement's magic make this work without overflow */
751 interval = tbl.max_value - tbl.min_value;
753 /* check value interval */
754 if (interval > 16 * 1024) {
758 /* check ratio of value interval to number of branches */
759 if ((float)(interval + 1) / (float)tbl.num_branches > 8.0) {
765 if (tbl.min_value != 0) {
766 lc_esnprintf(env, cmd_buf, SNPRINTF_BUF_LEN, "cmpl %lu, -%d(%1S)",
767 interval, tbl.min_value, irn);
768 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "; first switch value is not 0");
773 lc_esnprintf(env, cmd_buf, SNPRINTF_BUF_LEN, "cmpl %lu, %1S", interval, irn);
774 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "; compare for switch");
779 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "ja %s", get_cfop_target(tbl.defProj, buf));
780 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "; default jump if out of range ");
783 if (tbl.num_branches > 1) {
786 lc_esnprintf(env, cmd_buf, SNPRINTF_BUF_LEN, "jmp [%1S*4+%s]", irn, tbl.label);
787 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "; get jump table entry as target");
790 fprintf(F, "\t.section\t.rodata\n");
791 fprintf(F, "\t.align 4\n");
793 fprintf(F, "%s:\n", tbl.label);
795 snprintf(cmd_buf, SNPRINTF_BUF_LEN, ".long %s", get_cfop_target(tbl.branches[0].target, buf));
796 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* case %d */\n", tbl.branches[0].value);
799 last_value = tbl.branches[0].value;
800 for (i = 1; i < tbl.num_branches; ++i) {
801 while (++last_value < tbl.branches[i].value) {
802 snprintf(cmd_buf, SNPRINTF_BUF_LEN, ".long %s", get_cfop_target(tbl.defProj, buf));
803 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "; default case");
806 snprintf(cmd_buf, SNPRINTF_BUF_LEN, ".long %s", get_cfop_target(tbl.branches[i].target, buf), last_value);
807 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "; case %d", last_value);
811 fprintf(F, "\t.text");
814 /* one jump is enough */
815 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "jmp %s", get_cfop_target(tbl.branches[0].target, buf));
816 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "; only one case given");
820 else { // no jump table
821 for (i = 0; i < tbl.num_branches; ++i) {
822 lc_esnprintf(env, cmd_buf, SNPRINTF_BUF_LEN, "cmpl %d, %1S", tbl.branches[i].value, irn);
823 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "; case %d", i);
825 fprintf(F, "\tje %s\n", get_cfop_target(tbl.branches[i].target, buf));
828 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "jmp %s", get_cfop_target(tbl.defProj, buf));
829 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "; default case");
840 * Emits code for a unconditional jump.
842 void emit_Jmp(const ir_node *irn, ia32_emit_env_t *env) {
843 ir_node *block, *next_bl = NULL;
845 char buf[SNPRINTF_BUF_LEN], cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
847 /* for now, the code works for scheduled and non-schedules blocks */
848 block = get_nodes_block(irn);
849 if (sched_is_scheduled(block)) {
850 /* we have a block schedule */
851 next_bl = sched_next(block);
854 if (get_cfop_target_block(irn) != next_bl) {
855 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "jmp %s", get_cfop_target(irn, buf));
856 lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "; %+F(%+F)", irn, get_cfop_target_block(irn));
860 lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "; fallthrough %s", get_cfop_target(irn, buf));
867 /****************************
870 * _ __ _ __ ___ _ ___
871 * | '_ \| '__/ _ \| |/ __|
872 * | |_) | | | (_) | |\__ \
873 * | .__/|_| \___/| ||___/
876 ****************************/
879 * Emits code for a proj -> node
881 void emit_Proj(const ir_node *irn, ia32_emit_env_t *env) {
882 ir_node *pred = get_Proj_pred(irn);
884 if (get_irn_op(pred) == op_Start) {
885 switch(get_Proj_proj(irn)) {
886 case pn_Start_X_initial_exec:
895 /**********************************
898 * | | ___ _ __ _ _| |_) |
899 * | | / _ \| '_ \| | | | _ <
900 * | |___| (_) | |_) | |_| | |_) |
901 * \_____\___/| .__/ \__, |____/
904 **********************************/
907 * Emit movsb/w instructions to make mov count divideable by 4
909 static void emit_CopyB_prolog(FILE *F, int rem, int size) {
910 char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
912 fprintf(F, "\t/* memcopy %d bytes*/\n", size);
914 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "cld");
915 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "/* copy direction forward*/");
920 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "movsb");
921 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "; memcopy remainder 1");
924 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "movsw");
925 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "; memcopy remainder 2");
928 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "movsb");
929 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "; memcopy remainder 3");
931 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "movsw");
932 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "; memcopy remainder 3");
940 * Emit rep movsd instruction for memcopy.
942 void emit_ia32_CopyB(const ir_node *irn, ia32_emit_env_t *emit_env) {
943 FILE *F = emit_env->out;
944 tarval *tv = get_ia32_Immop_tarval(irn);
945 int rem = get_tarval_long(tv);
946 int size = get_tarval_long(get_ia32_Immop_tarval(get_irn_n(irn, 2)));
947 char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
949 emit_CopyB_prolog(F, rem, size);
951 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "rep movsd");
952 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "; memcopy");
957 * Emits unrolled memcopy.
959 void emit_ia32_CopyB_i(const ir_node *irn, ia32_emit_env_t *emit_env) {
960 tarval *tv = get_ia32_Immop_tarval(irn);
961 int size = get_tarval_long(tv);
962 FILE *F = emit_env->out;
963 char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
965 emit_CopyB_prolog(F, size & 0x3, size);
969 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "movsd");
970 snprintf(cmnt_buf, SNPRINTF_BUF_LEN, "; memcopy unrolled");
977 /***************************
981 * | | / _ \| '_ \ \ / /
982 * | |___| (_) | | | \ V /
983 * \_____\___/|_| |_|\_/
985 ***************************/
988 * Emit code for conversions (I, FP), (FP, I) and (FP, FP).
990 static void emit_ia32_Conv(const ir_node *irn, ia32_emit_env_t *emit_env) {
991 FILE *F = emit_env->out;
992 const lc_arg_env_t *env = ia32_get_arg_env();
993 char *from, *to, buf[64];
994 ir_mode *src_mode, *tgt_mode;
995 char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
997 src_mode = is_ia32_AddrModeS(irn) ? get_ia32_ls_mode(irn) : get_irn_mode(get_irn_n(irn, 2));
998 tgt_mode = get_ia32_res_mode(irn);
1000 from = mode_is_float(src_mode) ? (get_mode_size_bits(src_mode) == 32 ? "ss" : "sd") : "si";
1001 to = mode_is_float(tgt_mode) ? (get_mode_size_bits(tgt_mode) == 32 ? "ss" : "sd") : "si";
1003 switch(get_ia32_op_type(irn)) {
1005 lc_esnprintf(env, buf, sizeof(buf), "%1D, %3S", irn, irn);
1007 case ia32_AddrModeS:
1008 lc_esnprintf(env, buf, sizeof(buf), "%1D, %s", irn, ia32_emit_am(irn, emit_env));
1011 assert(0 && "unsupported op type for Conv");
1014 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "cvt%s2%s %s", from, to, buf);
1015 lc_esnprintf(env, cmnt_buf, SNPRINTF_BUF_LEN, "; %+F(%+F, %+F)", irn, src_mode, tgt_mode);
1019 void emit_ia32_Conv_I2FP(const ir_node *irn, ia32_emit_env_t *emit_env) {
1020 emit_ia32_Conv(irn, emit_env);
1023 void emit_ia32_Conv_FP2I(const ir_node *irn, ia32_emit_env_t *emit_env) {
1024 emit_ia32_Conv(irn, emit_env);
1027 void emit_ia32_Conv_FP2FP(const ir_node *irn, ia32_emit_env_t *emit_env) {
1028 emit_ia32_Conv(irn, emit_env);
1033 /*******************************************
1036 * | |__ ___ _ __ ___ __| | ___ ___
1037 * | '_ \ / _ \ '_ \ / _ \ / _` |/ _ \/ __|
1038 * | |_) | __/ | | | (_) | (_| | __/\__ \
1039 * |_.__/ \___|_| |_|\___/ \__,_|\___||___/
1041 *******************************************/
1044 * Emits a backend call
1046 void emit_be_Call(const ir_node *irn, ia32_emit_env_t *emit_env) {
1047 FILE *F = emit_env->out;
1048 entity *ent = be_Call_get_entity(irn);
1049 char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
1052 snprintf(cmd_buf, SNPRINTF_BUF_LEN, "call %s", get_entity_name(ent));
1055 lc_esnprintf(ia32_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "%1D", get_irn_n(irn, be_pos_Call_ptr));
1058 lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "; %+F (be_Call)", irn);
1064 * Emits code to increase stack pointer.
1066 void emit_be_IncSP(const ir_node *irn, ia32_emit_env_t *emit_env) {
1067 FILE *F = emit_env->out;
1068 unsigned offs = be_get_IncSP_offset(irn);
1069 be_stack_dir_t dir = be_get_IncSP_direction(irn);
1070 char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
1073 lc_esnprintf(ia32_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "add %1S,%s%u", irn,
1074 (dir == be_stack_dir_along) ? " -" : " ", offs);
1075 lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "; %+F (IncSP)", irn);
1078 snprintf(cmd_buf, SNPRINTF_BUF_LEN, " ");
1079 lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "; omitted %+F (IncSP) with 0", irn);
1086 * Emits code to set stack pointer.
1088 void emit_be_SetSP(const ir_node *irn, ia32_emit_env_t *emit_env) {
1089 FILE *F = emit_env->out;
1090 char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
1092 lc_esnprintf(ia32_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "mov %1D, %3S", irn, irn);
1093 lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "; %+F (restore SP)", irn);
1098 * Emits code for Copy.
1100 void emit_be_Copy(const ir_node *irn, ia32_emit_env_t *emit_env) {
1101 FILE *F = emit_env->out;
1102 char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
1104 lc_esnprintf(ia32_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "mov %1D, %1S", irn, irn);
1105 lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "; %+F", irn);
1110 * Emits code for exchange.
1112 void emit_be_Perm(const ir_node *irn, ia32_emit_env_t *emit_env) {
1113 FILE *F = emit_env->out;
1114 char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
1116 lc_esnprintf(ia32_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "xchg %1S, %2S", irn, irn);
1117 lc_esnprintf(ia32_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "; %+F(%1A, %2A)", irn, irn, irn);
1121 /***********************************************************************************
1124 * _ __ ___ __ _ _ _ __ | |_ _ __ __ _ _ __ ___ _____ _____ _ __| | __
1125 * | '_ ` _ \ / _` | | '_ \ | _| '__/ _` | '_ ` _ \ / _ \ \ /\ / / _ \| '__| |/ /
1126 * | | | | | | (_| | | | | | | | | | | (_| | | | | | | __/\ V V / (_) | | | <
1127 * |_| |_| |_|\__,_|_|_| |_| |_| |_| \__,_|_| |_| |_|\___| \_/\_/ \___/|_| |_|\_\
1129 ***********************************************************************************/
1132 * Enters the emitter functions for handled nodes into the generic
1133 * pointer of an opcode.
1135 static void ia32_register_emitters(void) {
1137 #define IA32_EMIT(a) op_ia32_##a->ops.generic = (op_func)emit_ia32_##a
1138 #define EMIT(a) op_##a->ops.generic = (op_func)emit_##a
1139 #define BE_EMIT(a) op_be_##a->ops.generic = (op_func)emit_be_##a
1141 /* first clear the generic function pointer for all ops */
1142 clear_irp_opcodes_generic_func();
1144 /* register all emitter functions defined in spec */
1145 ia32_register_spec_emitters();
1147 /* other ia32 emitter functions */
1150 IA32_EMIT(SwitchJmp);
1153 IA32_EMIT(Conv_I2FP);
1154 IA32_EMIT(Conv_FP2I);
1155 IA32_EMIT(Conv_FP2FP);
1157 /* benode emitter */
1174 * Emits code for a node.
1176 static void ia32_emit_node(const ir_node *irn, void *env) {
1177 ia32_emit_env_t *emit_env = env;
1178 firm_dbg_module_t *mod = emit_env->mod;
1179 FILE *F = emit_env->out;
1180 ir_op *op = get_irn_op(irn);
1182 DBG((mod, LEVEL_1, "emitting code for %+F\n", irn));
1184 if (op->ops.generic) {
1185 void (*emit)(const ir_node *, void *) = (void (*)(const ir_node *, void *))op->ops.generic;
1189 ir_fprintf(F, "\t%35s ; %+F \n", " ", irn);
1194 * Walks over the nodes in a block connected by scheduling edges
1195 * and emits code for each node.
1197 static void ia32_gen_block(ir_node *block, void *env) {
1200 if (! is_Block(block))
1203 fprintf(((ia32_emit_env_t *)env)->out, BLOCK_PREFIX("%ld:\n"), get_irn_node_nr(block));
1204 sched_foreach(block, irn) {
1205 ia32_emit_node(irn, env);
1210 * Emits code for function start.
1212 static void ia32_emit_func_prolog(FILE *F, ir_graph *irg) {
1213 entity *irg_ent = get_irg_entity(irg);
1214 const char *irg_name = get_entity_name(irg_ent);
1216 fprintf(F, "\tsection .text\n");
1217 if (get_entity_visibility(irg_ent) == visibility_external_visible) {
1218 fprintf(F, "global %s\n", irg_name);
1220 // fprintf(F, "\t.type\t%s, @function\n", irg_name);
1221 fprintf(F, "%s:\n", irg_name);
1225 * Emits code for function end
1227 static void ia32_emit_func_epilog(FILE *F, ir_graph *irg) {
1228 const char *irg_name = get_entity_name(get_irg_entity(irg));
1230 fprintf(F, "\tret\n\n");
1231 //printf(F, "\t.size\t%s, .-%s\n\n", irg_name, irg_name);
1235 * Sets labels for control flow nodes (jump target)
1236 * TODO: Jump optimization
1238 static void ia32_gen_labels(ir_node *block, void *env) {
1240 int n = get_Block_n_cfgpreds(block);
1242 for (n--; n >= 0; n--) {
1243 pred = get_Block_cfgpred(block, n);
1244 set_irn_link(pred, block);
1249 * Main driver. Emits the code for one routine.
1251 void ia32_gen_routine(FILE *F, ir_graph *irg, const ia32_code_gen_t *cg) {
1252 ia32_emit_env_t emit_env;
1254 emit_env.mod = firm_dbg_register("ir.be.codegen.ia32");
1256 emit_env.arch_env = cg->arch_env;
1258 emit_env.isa = (ia32_isa_t *)cg->arch_env->isa;
1260 /* set the global arch_env (needed by print hooks) */
1261 arch_env = cg->arch_env;
1263 ia32_register_emitters();
1265 ia32_emit_func_prolog(F, irg);
1266 irg_block_walk_graph(irg, ia32_gen_labels, NULL, &emit_env);
1267 irg_walk_blkwise_graph(irg, NULL, ia32_gen_block, &emit_env);
1269 ia32_emit_func_epilog(F, irg);