21 #include "../besched.h"
23 #include "arm_emitter.h"
24 #include "gen_arm_emitter.h"
25 #include "arm_nodes_attr.h"
26 #include "arm_new_nodes.h"
27 #include "arm_map_regs.h"
28 #include "gen_arm_regalloc_if.h"
30 #include "../benode_t.h"
32 #define SNPRINTF_BUF_LEN 128
34 static const arch_env_t *arch_env = NULL;
37 * Switch to a new section
39 void arm_switch_section(FILE *f, sections sec) {
40 static sections curr_sec = NO_SECTION;
52 fprintf(f, "\t.text\n");
56 fprintf(f, "\t.data\n");
64 /*************************************************************
66 * (_) | | / _| | | | |
67 * _ __ _ __ _ _ __ | |_| |_ | |__ ___| |_ __ ___ _ __
68 * | '_ \| '__| | '_ \| __| _| | '_ \ / _ \ | '_ \ / _ \ '__|
69 * | |_) | | | | | | | |_| | | | | | __/ | |_) | __/ |
70 * | .__/|_| |_|_| |_|\__|_| |_| |_|\___|_| .__/ \___|_|
73 *************************************************************/
76 * Returns non-zero if a mode has a Immediate attribute.
78 int is_immediate_node(ir_node *irn) {
79 arm_attr_t *attr = get_arm_attr(irn);
80 return ARM_GET_SHF_MOD(attr) == ARM_SHF_IMM;
84 * Return a const or SymConst as string.
86 static const char *node_const_to_str(ir_node *n, char *buf, int buflen) {
87 if (is_immediate_node(n)) {
88 snprintf(buf, buflen, "#0x%X", arm_decode_imm_w_shift(get_arm_value(n)));
91 else if (is_arm_SymConst(n))
92 return get_arm_symconst_label(n);
94 assert( 0 && "das ist gar keine Konstante");
99 * Returns node's offset as string.
101 static const char *node_offset_to_str(ir_node *n, char *buf, int buflen) {
103 ir_op *irn_op = get_irn_op(n);
105 if (irn_op == op_be_StackParam) {
106 ir_entity *ent = be_get_frame_entity(n);
107 offset = get_entity_offset(ent);
108 } else if (irn_op == op_be_Reload || irn_op == op_be_Spill) {
109 ir_entity *ent = be_get_frame_entity(n);
110 offset = get_entity_offset(ent);
111 } else if (irn_op == op_be_IncSP) {
112 offset = - be_get_IncSP_offset(n);
114 return "node_offset_to_str will fuer diesen Knotentyp noch implementiert werden";
116 snprintf(buf, buflen, "%d", offset);
120 /* We always pass the ir_node which is a pointer. */
121 static int arm_get_arg_type(const lc_arg_occ_t *occ) {
122 return lc_arg_type_ptr;
127 * Returns the register at in position pos.
129 static const arch_register_t *get_in_reg(const ir_node *irn, int pos) {
131 const arch_register_t *reg = NULL;
133 assert(get_irn_arity(irn) > pos && "Invalid IN position");
135 /* The out register of the operator at position pos is the
136 in register we need. */
137 op = get_irn_n(irn, pos);
139 reg = arch_get_irn_register(arch_env, op);
141 assert(reg && "no in register found");
146 * Returns the register at out position pos.
148 static const arch_register_t *get_out_reg(const ir_node *irn, int pos) {
150 const arch_register_t *reg = NULL;
152 assert(get_irn_n_edges(irn) > pos && "Invalid OUT position");
154 /* 1st case: irn is not of mode_T, so it has only */
155 /* one OUT register -> good */
156 /* 2nd case: irn is of mode_T -> collect all Projs and ask the */
157 /* Proj with the corresponding projnum for the register */
159 if (get_irn_mode(irn) != mode_T) {
160 reg = arch_get_irn_register(arch_env, irn);
162 else if (is_arm_irn(irn)) {
163 reg = get_arm_out_reg(irn, pos);
166 const ir_edge_t *edge;
168 foreach_out_edge(irn, edge) {
169 proj = get_edge_src_irn(edge);
170 assert(is_Proj(proj) && "non-Proj from mode_T node");
171 if (get_Proj_proj(proj) == pos) {
172 reg = arch_get_irn_register(arch_env, proj);
178 assert(reg && "no out register found");
183 * Returns the number of the in register at position pos.
185 int get_arm_reg_nr(ir_node *irn, int pos, int in_out) {
186 const arch_register_t *reg;
189 reg = get_in_reg(irn, pos);
192 reg = get_out_reg(irn, pos);
195 return arch_register_get_index(reg);
199 * Returns the name of the in register at position pos.
201 const char *get_arm_reg_name(ir_node *irn, int pos, int in_out) {
202 const arch_register_t *reg;
205 reg = get_in_reg(irn, pos);
208 reg = get_out_reg(irn, pos);
211 return arch_register_get_name(reg);
215 * Get the register name for a node.
217 static int arm_get_reg_name(lc_appendable_t *app,
218 const lc_arg_occ_t *occ, const lc_arg_value_t *arg)
221 ir_node *X = arg->v_ptr;
222 int nr = occ->width - 1;
225 return lc_appendable_snadd(app, "(null)", 6);
227 if (occ->conversion == 'S') {
228 buf = get_arm_reg_name(X, nr, 1);
231 buf = get_arm_reg_name(X, nr, 0);
234 lc_appendable_chadd(app, '%');
235 return lc_appendable_snadd(app, buf, strlen(buf));
239 * Returns the tarval or offset of an arm node as a string.
241 static int arm_const_to_str(lc_appendable_t *app,
242 const lc_arg_occ_t *occ, const lc_arg_value_t *arg)
244 char buffer[SNPRINTF_BUF_LEN];
246 ir_node *X = arg->v_ptr;
249 return lc_appendable_snadd(app, "(null)", 6);
251 if (occ->conversion == 'C') {
252 buf = node_const_to_str(X, buffer, sizeof(buffer));
255 buf = node_offset_to_str(X, buffer, sizeof(buffer));
258 return lc_appendable_snadd(app, buf, strlen(buf));
262 * Returns the tarval or offset of an arm node as a string.
264 static int arm_shift_str(lc_appendable_t *app,
265 const lc_arg_occ_t *occ, const lc_arg_value_t *arg)
267 char buffer[SNPRINTF_BUF_LEN];
268 ir_node *irn = arg->v_ptr;
269 arm_shift_modifier mod;
272 return lc_appendable_snadd(app, "(null)", 6);
274 mod = get_arm_shift_modifier(irn);
275 if (ARM_HAS_SHIFT(mod)) {
276 long v = get_tarval_long(get_arm_value(irn));
278 snprintf(buffer, sizeof(buffer), ", %s #%ld", arm_shf_mod_name(mod), v);
279 return lc_appendable_snadd(app, buffer, strlen(buffer));
285 * Determines the instruction suffix depending on the mode.
287 static int arm_get_mode_suffix(lc_appendable_t *app,
288 const lc_arg_occ_t *occ, const lc_arg_value_t *arg)
290 ir_node *irn = arg->v_ptr;
296 return lc_appendable_snadd(app, "(null)", 6);
298 attr = get_arm_attr(irn);
299 mode = attr->op_mode ? attr->op_mode : get_irn_mode(irn);
300 bits = get_mode_size_bits(mode);
303 return lc_appendable_chadd(app, 's');
305 return lc_appendable_chadd(app, 'd');
307 return lc_appendable_chadd(app, 'e');
311 * Return the arm printf arg environment.
312 * We use the firm environment with some additional handlers.
314 const lc_arg_env_t *arm_get_arg_env(void) {
315 static lc_arg_env_t *env = NULL;
317 static const lc_arg_handler_t arm_reg_handler = { arm_get_arg_type, arm_get_reg_name };
318 static const lc_arg_handler_t arm_const_handler = { arm_get_arg_type, arm_const_to_str };
319 static const lc_arg_handler_t arm_mode_handler = { arm_get_arg_type, arm_get_mode_suffix };
320 static const lc_arg_handler_t arm_shf_handler = { arm_get_arg_type, arm_shift_str };
323 /* extend the firm printer */
324 env = firm_get_arg_env();
327 lc_arg_register(env, "arm:sreg", 'S', &arm_reg_handler);
328 lc_arg_register(env, "arm:dreg", 'D', &arm_reg_handler);
329 lc_arg_register(env, "arm:cnst", 'C', &arm_const_handler);
330 lc_arg_register(env, "arm:offs", 'O', &arm_const_handler);
331 lc_arg_register(env, "arm:mode", 'M', &arm_mode_handler);
332 lc_arg_register(env, "arm:shf", 'X', &arm_shf_handler);
339 * Formated print of commands and comments.
341 static void arm_fprintf_format(FILE *F, const char *cmd_buf, const char *cmnt_buf, const ir_node *irn) {
342 lc_efprintf(arm_get_arg_env(), F, "\t%-35s %-60s /* %+F (%G) */\n", cmd_buf, cmnt_buf, irn, irn);
346 * Returns a unique label. This number will not be used a second time.
348 static unsigned get_unique_label(void) {
349 static unsigned id = 0;
355 * Returns the target label for a control flow node.
357 static char *get_cfop_target(const ir_node *irn, char *buf) {
358 ir_node *bl = get_irn_link(irn);
360 snprintf(buf, SNPRINTF_BUF_LEN, "BLOCK_%ld", get_irn_node_nr(bl));
367 static void emit_arm_SymConst(ir_node *irn, void *env) {
368 arm_emit_env_t *emit_env = env;
369 FILE *F = emit_env->out;
370 SymConstEntry *entry = obstack_alloc(&emit_env->obst, sizeof(*entry));
371 const lc_arg_env_t *arg_env = arm_get_arg_env();
374 entry->label = get_unique_label();
375 entry->symconst = irn;
376 entry->next = emit_env->symbols;
377 emit_env->symbols = entry;
379 lc_esnprintf(arg_env, cmd_buf, 256, "ldr %1D, .L%u", irn, entry->label);
380 arm_fprintf_format(F, cmd_buf, "/* indirect SymConst */", irn);
384 * Returns the next block in a block schedule.
386 static ir_node *sched_next_block(ir_node *block) {
387 return get_irn_link(block);
391 * Emit a conditional jump.
393 static void emit_arm_CondJmp(ir_node *irn, void *env) {
394 arm_emit_env_t *emit_env = env;
395 FILE *out = emit_env->out;
396 const ir_edge_t *edge;
397 ir_node *true_block = NULL;
398 ir_node *false_block = NULL;
399 ir_node *op1 = get_irn_n(irn, 0);
400 ir_mode *opmode = get_irn_mode(op1);
402 int proj_num = get_arm_proj_num(irn);
403 char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
406 foreach_out_edge(irn, edge) {
407 ir_node *proj = get_edge_src_irn(edge);
408 long nr = get_Proj_proj(proj);
409 ir_node *block = get_irn_link(proj);
410 if ( nr == pn_Cond_true) {
412 } else if (nr == pn_Cond_false) {
415 assert(0 && "tertium non datur! (CondJmp)");
419 if (proj_num == pn_Cmp_False) {
420 /* always false: should not happen */
421 fprintf(out, "\tb BLOCK_%ld\t\t\t/* false case */\n", get_irn_node_nr(false_block));
422 } else if (proj_num == pn_Cmp_True) {
423 /* always true: should not happen */
424 fprintf(out, "\tb BLOCK_%ld\t\t\t/* true case */\n", get_irn_node_nr(true_block));
426 ir_node *block = get_nodes_block(irn);
428 if (mode_is_float(opmode)) {
429 suffix = "ICHWILLIMPLEMENTIERTWERDEN";
431 lc_esnprintf(arm_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "fcmp %1S, %2S", irn, irn);
432 lc_esnprintf(arm_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* Compare(%1S, %2S) -> FCPSR */", irn, irn );
433 arm_fprintf_format(out, cmd_buf, cmnt_buf, irn);
435 lc_esnprintf(arm_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "fmstat", irn, irn);
436 lc_esnprintf(arm_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* FCSPR -> CPSR */");
437 arm_fprintf_format(out, cmd_buf, cmnt_buf, irn);
439 if (true_block == sched_next_block(block)) {
441 proj_num = get_negated_pnc(proj_num, opmode);
444 case pn_Cmp_Eq: suffix = "eq"; break;
445 case pn_Cmp_Lt: suffix = "lt"; break;
446 case pn_Cmp_Le: suffix = "le"; break;
447 case pn_Cmp_Gt: suffix = "gt"; break;
448 case pn_Cmp_Ge: suffix = "ge"; break;
449 case pn_Cmp_Lg: suffix = "ne"; break;
450 case pn_Cmp_Leg: suffix = "al"; break;
451 default: assert(0 && "komische Dinge geschehen"); suffix = "al";
454 lc_esnprintf(arm_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "cmp %1S, %2S", irn, irn);
455 lc_esnprintf(arm_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* Compare(%1S, %2S) -> CPSR */", irn, irn );
456 arm_fprintf_format(out, cmd_buf, cmnt_buf, irn);
459 if (true_block == sched_next_block(block)) {
460 lc_esnprintf(arm_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "b%s BLOCK_%d", suffix, get_irn_node_nr(true_block));
461 lc_esnprintf(arm_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* false case */");
462 arm_fprintf_format(out, cmd_buf, cmnt_buf, irn);
464 lc_esnprintf(arm_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* fallthrough BLOCK_%d */", get_irn_node_nr(false_block));
465 arm_fprintf_format(out, "", cmnt_buf, irn);
468 lc_esnprintf(arm_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "b%s BLOCK_%d", suffix, get_irn_node_nr(true_block));
469 lc_esnprintf(arm_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* true case */");
470 arm_fprintf_format(out, cmd_buf, cmnt_buf, irn);
472 if (false_block == sched_next_block(block)) {
473 lc_esnprintf(arm_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* fallthrough BLOCK_%d */", get_irn_node_nr(false_block));
474 arm_fprintf_format(out, "", cmnt_buf, irn);
477 lc_esnprintf(arm_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "b BLOCK_%d", get_irn_node_nr(false_block));
478 lc_esnprintf(arm_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* false case */");
479 arm_fprintf_format(out, cmd_buf, cmnt_buf, irn);
485 static void emit_arm_CopyB(ir_node *irn, void *env) {
486 arm_emit_env_t *emit_env = env;
487 FILE *out = emit_env->out;
488 char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
489 unsigned int size = get_tarval_long(get_arm_value(irn));
490 const lc_arg_env_t *arg_env = arm_get_arg_env();
492 lc_esnprintf(arg_env, cmnt_buf, SNPRINTF_BUF_LEN, "/* MemCopy (%2S)->(%1S) [%d bytes], Use %3S, %4S, %5S and %%r12 */", irn, irn, size, irn, irn, irn);
494 assert ( size > 0 && "CopyB needs size > 0" );
502 lc_esnprintf(arg_env, cmd_buf, SNPRINTF_BUF_LEN, "ldr %%r12, [%2S, #0]!", irn);
503 arm_fprintf_format(out, cmd_buf, cmnt_buf, irn);
504 lc_esnprintf(arg_env, cmd_buf, SNPRINTF_BUF_LEN, "str %%r12, [%1S, #0]!", irn);
505 arm_fprintf_format(out, cmd_buf, cmnt_buf, irn);
508 lc_esnprintf(arg_env, cmd_buf, SNPRINTF_BUF_LEN, "ldmia %2S!, {%%r12, %3S}", irn, irn);
509 arm_fprintf_format(out, cmd_buf, cmnt_buf, irn);
510 lc_esnprintf(arg_env, cmd_buf, SNPRINTF_BUF_LEN, "stmia %1S!, {%%r12, %3S}", irn, irn);
511 arm_fprintf_format(out, cmd_buf, cmnt_buf, irn);
514 lc_esnprintf(arg_env, cmd_buf, SNPRINTF_BUF_LEN, "ldmia %2S!, {%%r12, %3S, %4S}", irn, irn, irn);
515 arm_fprintf_format(out, cmd_buf, cmnt_buf, irn);
516 lc_esnprintf(arg_env, cmd_buf, SNPRINTF_BUF_LEN, "stmia %1S!, {%%r12, %3S, %4S}", irn, irn, irn);
517 arm_fprintf_format(out, cmd_buf, cmnt_buf, irn);
522 lc_esnprintf(arg_env, cmd_buf, SNPRINTF_BUF_LEN, "ldmia %2S!, {%%r12, %3S, %4S, %5S}", irn, irn, irn, irn);
523 arm_fprintf_format(out, cmd_buf, cmnt_buf, irn);
524 lc_esnprintf(arg_env, cmd_buf, SNPRINTF_BUF_LEN, "stmia %1S!, {%%r12, %3S, %4S, %5S}", irn, irn, irn, irn);
525 arm_fprintf_format(out, cmd_buf, cmnt_buf, irn);
530 static void emit_arm_SwitchJmp(ir_node *irn, void *env) {
531 char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
532 const ir_edge_t *edge;
534 arm_emit_env_t *emit_env = env;
535 FILE *out = emit_env->out;
540 int default_block_num;
542 block_nr = get_irn_node_nr(irn);
543 n_projs = get_arm_n_projs(irn);
545 projs = xcalloc(n_projs , sizeof(ir_node*));
547 foreach_out_edge(irn, edge) {
548 proj = get_edge_src_irn(edge);
549 assert(is_Proj(proj) && "Only proj allowed at SwitchJmp");
551 if (get_Proj_proj(proj) == get_arm_default_proj_num(irn))
552 default_block_num = get_irn_node_nr(get_irn_link(proj));
554 projs[get_Proj_proj(proj)] = proj;
557 // CMP %1S, n_projs - 1
562 lc_esnprintf(arm_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "cmp %1S, #%u", irn, n_projs - 1);
563 lc_esnprintf(arm_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "", irn);
564 arm_fprintf_format(out, cmd_buf, cmnt_buf, irn);
566 lc_esnprintf(arm_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "bhi BLOCK_%d", default_block_num);
567 lc_esnprintf(arm_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "", irn);
568 arm_fprintf_format(out, cmd_buf, cmnt_buf, irn);
571 lc_esnprintf(arm_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "ldr %%r12, TABLE_%d_START", block_nr);
572 lc_esnprintf(arm_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "", irn);
573 arm_fprintf_format(out, cmd_buf, cmnt_buf, irn);
575 lc_esnprintf(arm_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "add %%r12, %%r12, %1S, LSL #2", irn);
576 lc_esnprintf(arm_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "", irn);
577 arm_fprintf_format(out, cmd_buf, cmnt_buf, irn);
579 lc_esnprintf(arm_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "ldr %%r15, [%%r12, #0]");
580 lc_esnprintf(arm_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "", irn);
581 arm_fprintf_format(out, cmd_buf, cmnt_buf, irn);
583 // LDR %r12, .TABLE_X_START
584 // ADD %r12, %r12, [%1S, LSL #2]
587 fprintf(out, "TABLE_%d_START:\n\t.word\tTABLE_%d\n", block_nr, block_nr);
588 fprintf(out, "\t.align 2\n");
589 fprintf(out, "TABLE_%d:\n", block_nr);
592 for ( i=0; i<n_projs; i++) {
596 block = get_irn_link(proj);
598 block = get_irn_link(projs[get_arm_default_proj_num(irn)]);
600 fprintf(out, "\t.word\tBLOCK_%ld\n",get_irn_node_nr(block));
602 fprintf(out, "\t.align 2\n");
607 /************************************************************************/
609 /************************************************************************/
611 static void emit_be_Call(ir_node *irn, void *env) {
612 arm_emit_env_t *emit_env = env;
613 FILE *F = emit_env->out;
614 ir_entity *ent = be_Call_get_entity(irn);
615 char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
618 lc_esnprintf(arm_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "bl %s", get_entity_ld_name(ent));
620 lc_esnprintf(arm_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "%1D", get_irn_n(irn, be_pos_Call_ptr));
621 lc_esnprintf(arm_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* %+F (be_Call) */", irn);
622 arm_fprintf_format(F, cmd_buf, cmnt_buf, irn);
625 /** Emit an IncSP node */
626 static void emit_be_IncSP(const ir_node *irn, arm_emit_env_t *emit_env) {
627 FILE *F = emit_env->out;
628 int offs = be_get_IncSP_offset(irn);
631 char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
632 lc_esnprintf(arm_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "add %1D, %1S, #%O", irn, irn, irn );
633 lc_esnprintf(arm_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* IncSP(%O) */", irn);
634 arm_fprintf_format(F, cmd_buf, cmnt_buf, irn);
636 char cmnt_buf[SNPRINTF_BUF_LEN];
637 lc_esnprintf(arm_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* omitted IncSP(%O) */", irn);
638 arm_fprintf_format(F, "", cmnt_buf, irn);
642 // void emit_be_AddSP(const ir_node *irn, arm_emit_env_t *emit_env) {
643 // FILE *F = emit_env->out;
644 // char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
645 // lc_esnprintf(arm_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "ADD %1D, %1S, %2S", irn, irn, irn );
646 // lc_esnprintf(arm_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* AddSP(%2S) */", irn);
647 // lc_efprintf(arm_get_arg_env(), F, "\t%-35s %-60s /* %+F */\n", cmd_buf, cmnt_buf, irn);
650 static void emit_be_Copy(const ir_node *irn, arm_emit_env_t *emit_env) {
651 FILE *F = emit_env->out;
652 ir_mode *mode = get_irn_mode(irn);
653 const lc_arg_env_t *arm_env = arm_get_arg_env();
654 char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
656 if (get_in_reg(irn, 0) == get_out_reg(irn, 0)) {
657 lc_esnprintf(arm_env, cmnt_buf, SNPRINTF_BUF_LEN, "/* omitted Copy: %1S -> %1D */", irn, irn);
658 lc_efprintf(arm_env, F, "\t%-35s %-60s /* %+F */\n", "", cmnt_buf, irn);
662 if (mode_is_float(mode)) {
663 if (USE_FPA(emit_env->cg->isa)) {
664 lc_esnprintf(arm_env, cmd_buf, SNPRINTF_BUF_LEN, "mvf%M %1D, %1S", irn, irn, irn);
665 lc_esnprintf(arm_env, cmnt_buf, SNPRINTF_BUF_LEN, "/* Copy: %1S -> %1D */", irn, irn);
666 arm_fprintf_format(F, cmd_buf, cmnt_buf, irn);
669 assert(0 && "move not supported for this mode");
671 } else if (mode_is_numP(mode)) {
672 lc_esnprintf(arm_env, cmd_buf, SNPRINTF_BUF_LEN, "mov %1D, %1S", irn, irn);
673 lc_esnprintf(arm_env, cmnt_buf, SNPRINTF_BUF_LEN, "/* Copy: %1S -> %1D */", irn, irn);
674 arm_fprintf_format(F, cmd_buf, cmnt_buf, irn);
676 assert(0 && "move not supported for this mode");
678 // emit_arm_Copy(irn, emit_env);
682 * Emit code for a Spill.
684 static void emit_be_Spill(const ir_node *irn, arm_emit_env_t *emit_env) {
685 FILE *F = emit_env->out;
686 ir_mode *mode = get_irn_mode(irn);
687 const lc_arg_env_t *arm_env = arm_get_arg_env();
688 char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
690 if (mode_is_float(mode)) {
691 if (USE_FPA(emit_env->cg->isa)) {
692 lc_esnprintf(arm_env, cmd_buf, SNPRINTF_BUF_LEN, "stf %2S, [%1S, #%O]", irn, irn, irn );
693 lc_esnprintf(arm_env, cmnt_buf, SNPRINTF_BUF_LEN, "/* Spill(%2S) -> (%1S) */", irn, irn);
694 arm_fprintf_format(F, cmd_buf, cmnt_buf, irn);
697 assert(0 && "move not supported for this mode");
699 } else if (mode_is_dataM(mode)) {
700 lc_esnprintf(arm_env, cmd_buf, SNPRINTF_BUF_LEN, "str %2S, [%1S, #%O]", irn, irn, irn );
701 lc_esnprintf(arm_env, cmnt_buf, SNPRINTF_BUF_LEN, "/* Spill(%2S) -> (%1S) */", irn, irn);
702 arm_fprintf_format(F, cmd_buf, cmnt_buf, irn);
704 assert(0 && "spill not supported for this mode");
709 * Emit code for a Reload.
711 static void emit_be_Reload(const ir_node* irn, arm_emit_env_t *emit_env) {
712 FILE *F = emit_env->out;
713 ir_mode *mode = get_irn_mode(irn);
714 const lc_arg_env_t *arm_env = arm_get_arg_env();
715 char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
717 if (mode_is_float(mode)) {
718 if (USE_FPA(emit_env->cg->isa)) {
719 lc_esnprintf(arm_env, cmd_buf, SNPRINTF_BUF_LEN, "ldf %1D, [%1S, #%O]", irn, irn, irn );
720 lc_esnprintf(arm_env, cmnt_buf, SNPRINTF_BUF_LEN, "/* Reload(%1S) -> (%1D) */", irn, irn);
721 arm_fprintf_format(F, cmd_buf, cmnt_buf, irn);
724 assert(0 && "move not supported for this mode");
726 } else if (mode_is_dataM(mode)) {
727 lc_esnprintf(arm_env, cmd_buf, SNPRINTF_BUF_LEN, "ldr %1D, [%1S, #%O]", irn, irn, irn );
728 lc_esnprintf(arm_env, cmnt_buf, SNPRINTF_BUF_LEN, "/* Reload(%1S) -> (%1D) */", irn, irn);
729 arm_fprintf_format(F, cmd_buf, cmnt_buf, irn);
731 assert(0 && "reload not supported for this mode");
735 static void emit_be_Perm(const ir_node* irn, arm_emit_env_t *emit_env) {
736 FILE *F = emit_env->out;
737 const lc_arg_env_t *arm_env = arm_get_arg_env();
738 char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
740 lc_esnprintf(arm_env, cmd_buf, SNPRINTF_BUF_LEN, "eor %1S, %1S, %2S", irn, irn, irn);
741 lc_esnprintf(arm_env, cmnt_buf, SNPRINTF_BUF_LEN, "/* begin Perm(%1S, %2S) */", irn, irn);
742 arm_fprintf_format(F, cmd_buf, cmnt_buf, irn);
744 lc_esnprintf(arm_env, cmd_buf, SNPRINTF_BUF_LEN, "eor %2S, %1S, %2S", irn, irn, irn);
745 lc_esnprintf(arm_env, cmnt_buf, SNPRINTF_BUF_LEN, " ");
746 arm_fprintf_format(F, cmd_buf, cmnt_buf, irn);
748 lc_esnprintf(arm_env, cmd_buf, SNPRINTF_BUF_LEN, "eor %1S, %1S, %2S", irn, irn, irn);
749 lc_esnprintf(arm_env, cmnt_buf, SNPRINTF_BUF_LEN, "/* end Perm(%1S, %2S) */", irn, irn);
750 arm_fprintf_format(F, cmd_buf, cmnt_buf, irn);
753 static void emit_be_StackParam(const ir_node *irn, arm_emit_env_t *emit_env) {
754 FILE *F = emit_env->out;
755 ir_mode *mode = get_irn_mode(irn);
756 const lc_arg_env_t *arm_env = arm_get_arg_env();
757 char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
759 if (mode_is_float(mode)) {
760 if (USE_FPA(emit_env->cg->isa)) {
761 lc_esnprintf(arm_env, cmd_buf, SNPRINTF_BUF_LEN, "ldf %1D, [%1S, #%O]", irn, irn, irn );
762 lc_esnprintf(arm_env, cmnt_buf, SNPRINTF_BUF_LEN, "/* StackParam: (%1S + %O) -> %1D */",irn , irn, irn, get_irn_n(irn, 0));
763 arm_fprintf_format(F, cmd_buf, cmnt_buf, irn);
766 assert(0 && "move not supported for this mode");
769 lc_esnprintf(arm_env, cmd_buf, SNPRINTF_BUF_LEN, "ldr %1D, [%1S, #%O]", irn, irn, irn );
770 lc_esnprintf(arm_env, cmnt_buf, SNPRINTF_BUF_LEN, "/* StackParam: (%1S + %O) -> %1D */",irn , irn, irn, get_irn_n(irn, 0));
771 arm_fprintf_format(F, cmd_buf, cmnt_buf, irn);
775 /************************************************************************/
777 /************************************************************************/
779 static void emit_Jmp(ir_node *irn, void *env) {
780 char cmd_buf[SNPRINTF_BUF_LEN];
781 arm_emit_env_t *emit_env = env;
782 FILE *F = emit_env->out;
783 const ir_edge_t *edge = get_irn_out_edge_first(irn);
784 const lc_arg_env_t *arm_env = arm_get_arg_env();
785 ir_node *target_block = get_edge_src_irn(edge);
787 lc_esnprintf(arm_env, cmd_buf, SNPRINTF_BUF_LEN, "b BLOCK_%d", get_irn_node_nr(target_block));
788 arm_fprintf_format(F, cmd_buf, "/* unconditional Jump */", irn);
791 static void emit_arm_fpaDbl2GP(const ir_node *n, arm_emit_env_t *env) {
794 const lc_arg_env_t *arg_env = arm_get_arg_env();
796 lc_esnprintf(arg_env, cmd_buf, 256, "stfd %1S, [sp, #-8]! ", n);
797 arm_fprintf_format(F, cmd_buf, "/* Push fp to stack */", n);
799 lc_esnprintf(arg_env, cmd_buf, 256, "ldmfd sp!, {%2D, %1D} ", n, n);
800 arm_fprintf_format(F, cmd_buf, "/* Pop destination */", n);
803 static void emit_silence(ir_node *irn, void *env) {
808 /***********************************************************************************
811 * _ __ ___ __ _ _ _ __ | |_ _ __ __ _ _ __ ___ _____ _____ _ __| | __
812 * | '_ ` _ \ / _` | | '_ \ | _| '__/ _` | '_ ` _ \ / _ \ \ /\ / / _ \| '__| |/ /
813 * | | | | | | (_| | | | | | | | | | | (_| | | | | | | __/\ V V / (_) | | | <
814 * |_| |_| |_|\__,_|_|_| |_| |_| |_| \__,_|_| |_| |_|\___| \_/\_/ \___/|_| |_|\_\
816 ***********************************************************************************/
819 * Enters the emitter functions for handled nodes into the generic
820 * pointer of an opcode.
822 static void arm_register_emitters(void) {
824 #define ARM_EMIT(a) op_arm_##a->ops.generic = (op_func)emit_arm_##a
825 #define EMIT(a) op_##a->ops.generic = (op_func)emit_##a
826 #define BE_EMIT(a) op_be_##a->ops.generic = (op_func)emit_be_##a
827 #define SILENCE(a) op_##a->ops.generic = (op_func)emit_silence
829 /* first clear the generic function pointer for all ops */
830 clear_irp_opcodes_generic_func();
832 /* register all emitter functions defined in spec */
833 arm_register_spec_emitters();
835 /* other emitter functions */
838 // ARM_EMIT(CopyB_i);
862 SILENCE(be_CopyKeep);
871 typedef void (node_emitter)(const ir_node *, void *);
874 * Emits code for a node.
876 static void arm_emit_node(const ir_node *irn, void *env) {
877 arm_emit_env_t *emit_env = env;
878 FILE *F = emit_env->out;
879 ir_op *op = get_irn_op(irn);
880 DEBUG_ONLY(firm_dbg_module_t *mod = emit_env->mod;)
882 DBG((mod, LEVEL_1, "emitting code for %+F\n", irn));
884 if (op->ops.generic) {
885 node_emitter *emit = (node_emitter *)op->ops.generic;
889 ir_fprintf(F, "\t%-35s /* %+F %G */\n", "", irn, irn);
894 * Walks over the nodes in a block connected by scheduling edges
895 * and emits code for each node.
897 void arm_gen_block(ir_node *block, void *env) {
900 fprintf(((arm_emit_env_t *)env)->out, "BLOCK_%ld:\n", get_irn_node_nr(block));
901 sched_foreach(block, irn) {
902 arm_emit_node(irn, env);
908 * Emits code for function start.
910 void arm_emit_start(FILE *F, ir_graph *irg) {
911 ir_entity *ent = get_irg_entity(irg);
912 const char *irg_name = get_entity_ld_name(ent);
913 arm_switch_section(F, SECTION_TEXT);
914 fprintf(F, "\t.align 2\n");
916 if (get_entity_visibility(ent) == visibility_external_visible)
917 fprintf(F, "\t.global %s\n", irg_name);
918 fprintf(F, "%s:\n", irg_name);
922 * Emits code for function end
924 void arm_emit_end(FILE *F, ir_graph *irg) {
925 fprintf(F, "\t.ident \"firmcc\"\n");
929 * Sets labels for control flow nodes (jump target)
930 * TODO: Jump optimization
932 void arm_gen_labels(ir_node *block, void *env) {
934 int n = get_Block_n_cfgpreds(block);
936 for (n--; n >= 0; n--) {
937 pred = get_Block_cfgpred(block, n);
938 set_irn_link(pred, block);
944 * Main driver. Emits the code for one routine.
946 void arm_gen_routine(FILE *F, ir_graph *irg, const arm_code_gen_t *cg) {
947 SymConstEntry *entry;
948 arm_emit_env_t emit_env;
953 emit_env.arch_env = cg->arch_env;
955 emit_env.symbols = NULL;
956 obstack_init(&emit_env.obst);
957 FIRM_DBG_REGISTER(emit_env.mod, "firm.be.arm.emit");
959 /* set the global arch_env (needed by print hooks) */
960 arch_env = cg->arch_env;
962 arm_register_emitters();
964 /* create the block schedule. For now, we don't need it earlier. */
965 blk_sched = sched_create_block_schedule(cg->irg, cg->birg->exec_freq);
967 arm_emit_start(F, irg);
968 irg_block_walk_graph(irg, arm_gen_labels, NULL, &emit_env);
970 n = ARR_LEN(blk_sched);
971 for (i = 0; i < n;) {
972 ir_node *block, *next_bl;
974 block = blk_sched[i];
976 next_bl = i < n ? blk_sched[i] : NULL;
978 /* set here the link. the emitter expects to find the next block here */
979 set_irn_link(block, next_bl);
980 arm_gen_block(block, &emit_env);
983 /* emit SymConst values */
984 if (emit_env.symbols)
985 fprintf(F, "\t.align 2\n");
987 for (entry = emit_env.symbols; entry; entry = entry->next) {
988 fprintf(F, ".L%u:\n", entry->label);
989 lc_efprintf(arm_get_arg_env(), F, "\t.word\t%C\n", entry->symconst);
992 obstack_free(&emit_env.obst, NULL);