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");
61 /*************************************************************
63 * (_) | | / _| | | | |
64 * _ __ _ __ _ _ __ | |_| |_ | |__ ___| |_ __ ___ _ __
65 * | '_ \| '__| | '_ \| __| _| | '_ \ / _ \ | '_ \ / _ \ '__|
66 * | |_) | | | | | | | |_| | | | | | __/ | |_) | __/ |
67 * | .__/|_| |_|_| |_|\__|_| |_| |_|\___|_| .__/ \___|_|
70 *************************************************************/
73 * Returns non-zero if a mode has a Immediate attribute.
75 int is_immediate_node(ir_node *irn) {
76 arm_attr_t *attr = get_arm_attr(irn);
77 return ARM_GET_SHF_MOD(attr) == ARM_SHF_IMM;
81 * Return a const or SymConst as string.
83 static const char *node_const_to_str(ir_node *n, char *buf, int buflen) {
84 if (is_immediate_node(n)) {
85 snprintf(buf, buflen, "#0x%X", arm_decode_imm_w_shift(get_arm_value(n)));
88 else if (is_arm_SymConst(n))
89 return get_arm_symconst_label(n);
91 assert( 0 && "das ist gar keine Konstante");
96 * Returns node's offset as string.
98 static const char *node_offset_to_str(ir_node *n, char *buf, int buflen) {
100 ir_op *irn_op = get_irn_op(n);
102 if (irn_op == op_be_StackParam) {
103 entity *ent = be_get_frame_entity(n);
104 offset = get_entity_offset_bytes(ent);
105 } else if (irn_op == op_be_Reload || irn_op == op_be_Spill) {
106 entity *ent = be_get_frame_entity(n);
107 offset = get_entity_offset_bytes(ent);
108 } else if (irn_op == op_be_IncSP) {
109 offset = - be_get_IncSP_offset(n);
111 return "node_offset_to_str will fuer diesen Knotentyp noch implementiert werden";
113 snprintf(buf, buflen, "%d", offset);
117 /* We always pass the ir_node which is a pointer. */
118 static int arm_get_arg_type(const lc_arg_occ_t *occ) {
119 return lc_arg_type_ptr;
124 * Returns the register at in position pos.
126 static const arch_register_t *get_in_reg(const ir_node *irn, int pos) {
128 const arch_register_t *reg = NULL;
130 assert(get_irn_arity(irn) > pos && "Invalid IN position");
132 /* The out register of the operator at position pos is the
133 in register we need. */
134 op = get_irn_n(irn, pos);
136 reg = arch_get_irn_register(arch_env, op);
138 assert(reg && "no in register found");
143 * Returns the register at out position pos.
145 static const arch_register_t *get_out_reg(const ir_node *irn, int pos) {
147 const arch_register_t *reg = NULL;
149 assert(get_irn_n_edges(irn) > pos && "Invalid OUT position");
151 /* 1st case: irn is not of mode_T, so it has only */
152 /* one OUT register -> good */
153 /* 2nd case: irn is of mode_T -> collect all Projs and ask the */
154 /* Proj with the corresponding projnum for the register */
156 if (get_irn_mode(irn) != mode_T) {
157 reg = arch_get_irn_register(arch_env, irn);
159 else if (is_arm_irn(irn)) {
160 reg = get_arm_out_reg(irn, pos);
163 const ir_edge_t *edge;
165 foreach_out_edge(irn, edge) {
166 proj = get_edge_src_irn(edge);
167 assert(is_Proj(proj) && "non-Proj from mode_T node");
168 if (get_Proj_proj(proj) == pos) {
169 reg = arch_get_irn_register(arch_env, proj);
175 assert(reg && "no out register found");
180 * Returns the number of the in register at position pos.
182 int get_arm_reg_nr(ir_node *irn, int pos, int in_out) {
183 const arch_register_t *reg;
186 reg = get_in_reg(irn, pos);
189 reg = get_out_reg(irn, pos);
192 return arch_register_get_index(reg);
196 * Returns the name of the in register at position pos.
198 const char *get_arm_reg_name(ir_node *irn, int pos, int in_out) {
199 const arch_register_t *reg;
202 reg = get_in_reg(irn, pos);
205 reg = get_out_reg(irn, pos);
208 return arch_register_get_name(reg);
212 * Get the register name for a node.
214 static int arm_get_reg_name(lc_appendable_t *app,
215 const lc_arg_occ_t *occ, const lc_arg_value_t *arg)
218 ir_node *X = arg->v_ptr;
219 int nr = occ->width - 1;
222 return lc_appendable_snadd(app, "(null)", 6);
224 if (occ->conversion == 'S') {
225 buf = get_arm_reg_name(X, nr, 1);
228 buf = get_arm_reg_name(X, nr, 0);
231 lc_appendable_chadd(app, '%');
232 return lc_appendable_snadd(app, buf, strlen(buf));
236 * Returns the tarval or offset of an arm node as a string.
238 static int arm_const_to_str(lc_appendable_t *app,
239 const lc_arg_occ_t *occ, const lc_arg_value_t *arg)
241 char buffer[SNPRINTF_BUF_LEN];
243 ir_node *X = arg->v_ptr;
246 return lc_appendable_snadd(app, "(null)", 6);
248 if (occ->conversion == 'C') {
249 buf = node_const_to_str(X, buffer, sizeof(buffer));
252 buf = node_offset_to_str(X, buffer, sizeof(buffer));
255 return lc_appendable_snadd(app, buf, strlen(buf));
259 * Returns the tarval or offset of an arm node as a string.
261 static int arm_shift_str(lc_appendable_t *app,
262 const lc_arg_occ_t *occ, const lc_arg_value_t *arg)
264 char buffer[SNPRINTF_BUF_LEN];
265 ir_node *irn = arg->v_ptr;
266 arm_shift_modifier mod;
269 return lc_appendable_snadd(app, "(null)", 6);
271 mod = get_arm_shift_modifier(irn);
272 if (ARM_HAS_SHIFT(mod)) {
273 long v = get_tarval_long(get_arm_value(irn));
275 snprintf(buffer, sizeof(buffer), ", %s #%ld", arm_shf_mod_name(mod), v);
276 return lc_appendable_snadd(app, buffer, strlen(buffer));
282 * Determines the instruction suffix depending on the mode.
284 static int arm_get_mode_suffix(lc_appendable_t *app,
285 const lc_arg_occ_t *occ, const lc_arg_value_t *arg)
287 ir_node *irn = arg->v_ptr;
293 return lc_appendable_snadd(app, "(null)", 6);
295 attr = get_arm_attr(irn);
296 mode = attr->op_mode ? attr->op_mode : get_irn_mode(irn);
297 bits = get_mode_size_bits(mode);
300 return lc_appendable_chadd(app, 's');
302 return lc_appendable_chadd(app, 'd');
304 return lc_appendable_chadd(app, 'e');
308 * Return the arm printf arg environment.
309 * We use the firm environment with some additional handlers.
311 const lc_arg_env_t *arm_get_arg_env(void) {
312 static lc_arg_env_t *env = NULL;
314 static const lc_arg_handler_t arm_reg_handler = { arm_get_arg_type, arm_get_reg_name };
315 static const lc_arg_handler_t arm_const_handler = { arm_get_arg_type, arm_const_to_str };
316 static const lc_arg_handler_t arm_mode_handler = { arm_get_arg_type, arm_get_mode_suffix };
317 static const lc_arg_handler_t arm_shf_handler = { arm_get_arg_type, arm_shift_str };
320 /* extend the firm printer */
321 env = firm_get_arg_env();
324 lc_arg_register(env, "arm:sreg", 'S', &arm_reg_handler);
325 lc_arg_register(env, "arm:dreg", 'D', &arm_reg_handler);
326 lc_arg_register(env, "arm:cnst", 'C', &arm_const_handler);
327 lc_arg_register(env, "arm:offs", 'O', &arm_const_handler);
328 lc_arg_register(env, "arm:mode", 'M', &arm_mode_handler);
329 lc_arg_register(env, "arm:shf", 'X', &arm_shf_handler);
336 * Formated print of commands and comments.
338 static void arm_fprintf_format(FILE *F, const char *cmd_buf, const char *cmnt_buf, const ir_node *irn) {
339 lc_efprintf(arm_get_arg_env(), F, "\t%-35s %-60s /* %+F (%G) */\n", cmd_buf, cmnt_buf, irn, irn);
343 * Returns a unique label. This number will not be used a second time.
345 static unsigned get_unique_label(void) {
346 static unsigned id = 0;
352 * Returns the target label for a control flow node.
354 static char *get_cfop_target(const ir_node *irn, char *buf) {
355 ir_node *bl = get_irn_link(irn);
357 snprintf(buf, SNPRINTF_BUF_LEN, "BLOCK_%ld", get_irn_node_nr(bl));
364 static void emit_arm_SymConst(ir_node *irn, void *env) {
365 arm_emit_env_t *emit_env = env;
366 FILE *F = emit_env->out;
367 SymConstEntry *entry = obstack_alloc(&emit_env->obst, sizeof(*entry));
368 const lc_arg_env_t *arg_env = arm_get_arg_env();
371 entry->label = get_unique_label();
372 entry->symconst = irn;
373 entry->next = emit_env->symbols;
374 emit_env->symbols = entry;
376 lc_esnprintf(arg_env, cmd_buf, 256, "ldr %1D, .L%u", irn, entry->label);
377 arm_fprintf_format(F, cmd_buf, "/* indirect SymConst */", irn);
381 * Returns the next block in a block schedule.
383 static ir_node *sched_next_block(ir_node *block) {
384 return get_irn_link(block);
388 * Emit a conditional jump.
390 static void emit_arm_CondJmp(ir_node *irn, void *env) {
391 arm_emit_env_t *emit_env = env;
392 FILE *out = emit_env->out;
393 const ir_edge_t *edge;
394 ir_node *true_block = NULL;
395 ir_node *false_block = NULL;
396 ir_node *op1 = get_irn_n(irn, 0);
397 ir_mode *opmode = get_irn_mode(op1);
399 int proj_num = get_arm_proj_num(irn);
400 char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
403 foreach_out_edge(irn, edge) {
404 ir_node *proj = get_edge_src_irn(edge);
405 long nr = get_Proj_proj(proj);
406 ir_node *block = get_irn_link(proj);
407 if ( nr == pn_Cond_true) {
409 } else if (nr == pn_Cond_false) {
412 assert(0 && "tertium non datur! (CondJmp)");
416 if (proj_num == pn_Cmp_False) {
417 /* always false: should not happen */
418 fprintf(out, "\tb BLOCK_%ld\t\t\t/* false case */\n", get_irn_node_nr(false_block));
419 } else if (proj_num == pn_Cmp_True) {
420 /* always true: should not happen */
421 fprintf(out, "\tb BLOCK_%ld\t\t\t/* true case */\n", get_irn_node_nr(true_block));
423 ir_node *block = get_nodes_block(irn);
425 if (mode_is_float(opmode)) {
426 suffix = "ICHWILLIMPLEMENTIERTWERDEN";
428 lc_esnprintf(arm_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "fcmp %1S, %2S", irn, irn);
429 lc_esnprintf(arm_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* Compare(%1S, %2S) -> FCPSR */", irn, irn );
430 arm_fprintf_format(out, cmd_buf, cmnt_buf, irn);
432 lc_esnprintf(arm_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "fmstat", irn, irn);
433 lc_esnprintf(arm_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* FCSPR -> CPSR */");
434 arm_fprintf_format(out, cmd_buf, cmnt_buf, irn);
436 if (true_block == sched_next_block(block)) {
438 proj_num = get_negated_pnc(proj_num, opmode);
441 case pn_Cmp_Eq: suffix = "eq"; break;
442 case pn_Cmp_Lt: suffix = "lt"; break;
443 case pn_Cmp_Le: suffix = "le"; break;
444 case pn_Cmp_Gt: suffix = "gt"; break;
445 case pn_Cmp_Ge: suffix = "ge"; break;
446 case pn_Cmp_Lg: suffix = "ne"; break;
447 case pn_Cmp_Leg: suffix = "al"; break;
448 default: assert(0 && "komische Dinge geschehen"); suffix = "al";
451 lc_esnprintf(arm_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "cmp %1S, %2S", irn, irn);
452 lc_esnprintf(arm_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* Compare(%1S, %2S) -> CPSR */", irn, irn );
453 arm_fprintf_format(out, cmd_buf, cmnt_buf, irn);
456 if (true_block == sched_next_block(block)) {
457 lc_esnprintf(arm_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "b%s BLOCK_%d", suffix, get_irn_node_nr(true_block));
458 lc_esnprintf(arm_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* false case */");
459 arm_fprintf_format(out, cmd_buf, cmnt_buf, irn);
461 lc_esnprintf(arm_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* fallthrough BLOCK_%d */", get_irn_node_nr(false_block));
462 arm_fprintf_format(out, "", cmnt_buf, irn);
465 lc_esnprintf(arm_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "b%s BLOCK_%d", suffix, get_irn_node_nr(true_block));
466 lc_esnprintf(arm_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* true case */");
467 arm_fprintf_format(out, cmd_buf, cmnt_buf, irn);
469 if (false_block == sched_next_block(block)) {
470 lc_esnprintf(arm_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* fallthrough BLOCK_%d */", get_irn_node_nr(false_block));
471 arm_fprintf_format(out, "", cmnt_buf, irn);
474 lc_esnprintf(arm_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "b BLOCK_%d", get_irn_node_nr(false_block));
475 lc_esnprintf(arm_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* false case */");
476 arm_fprintf_format(out, cmd_buf, cmnt_buf, irn);
482 static void emit_arm_CopyB(ir_node *irn, void *env) {
483 arm_emit_env_t *emit_env = env;
484 FILE *out = emit_env->out;
485 char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
486 unsigned int size = get_tarval_long(get_arm_value(irn));
487 const lc_arg_env_t *arg_env = arm_get_arg_env();
489 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);
491 assert ( size > 0 && "CopyB needs size > 0" );
499 lc_esnprintf(arg_env, cmd_buf, SNPRINTF_BUF_LEN, "ldr %%r12, [%2S, #0]!", irn);
500 arm_fprintf_format(out, cmd_buf, cmnt_buf, irn);
501 lc_esnprintf(arg_env, cmd_buf, SNPRINTF_BUF_LEN, "str %%r12, [%1S, #0]!", irn);
502 arm_fprintf_format(out, cmd_buf, cmnt_buf, irn);
505 lc_esnprintf(arg_env, cmd_buf, SNPRINTF_BUF_LEN, "ldmia %2S!, {%%r12, %3S}", irn, irn);
506 arm_fprintf_format(out, cmd_buf, cmnt_buf, irn);
507 lc_esnprintf(arg_env, cmd_buf, SNPRINTF_BUF_LEN, "stmia %1S!, {%%r12, %3S}", irn, irn);
508 arm_fprintf_format(out, cmd_buf, cmnt_buf, irn);
511 lc_esnprintf(arg_env, cmd_buf, SNPRINTF_BUF_LEN, "ldmia %2S!, {%%r12, %3S, %4S}", irn, irn, irn);
512 arm_fprintf_format(out, cmd_buf, cmnt_buf, irn);
513 lc_esnprintf(arg_env, cmd_buf, SNPRINTF_BUF_LEN, "stmia %1S!, {%%r12, %3S, %4S}", irn, irn, irn);
514 arm_fprintf_format(out, cmd_buf, cmnt_buf, irn);
519 lc_esnprintf(arg_env, cmd_buf, SNPRINTF_BUF_LEN, "ldmia %2S!, {%%r12, %3S, %4S, %5S}", irn, irn, irn, irn);
520 arm_fprintf_format(out, cmd_buf, cmnt_buf, irn);
521 lc_esnprintf(arg_env, cmd_buf, SNPRINTF_BUF_LEN, "stmia %1S!, {%%r12, %3S, %4S, %5S}", irn, irn, irn, irn);
522 arm_fprintf_format(out, cmd_buf, cmnt_buf, irn);
527 static void emit_arm_SwitchJmp(ir_node *irn, void *env) {
528 char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
529 const ir_edge_t *edge;
531 arm_emit_env_t *emit_env = env;
532 FILE *out = emit_env->out;
537 int default_block_num;
539 block_nr = get_irn_node_nr(irn);
540 n_projs = get_arm_n_projs(irn);
542 projs = xcalloc(n_projs , sizeof(ir_node*));
544 foreach_out_edge(irn, edge) {
545 proj = get_edge_src_irn(edge);
546 assert(is_Proj(proj) && "Only proj allowed at SwitchJmp");
548 if (get_Proj_proj(proj) == get_arm_default_proj_num(irn))
549 default_block_num = get_irn_node_nr(get_irn_link(proj));
551 projs[get_Proj_proj(proj)] = proj;
554 // CMP %1S, n_projs - 1
559 lc_esnprintf(arm_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "cmp %1S, #%u", irn, n_projs - 1);
560 lc_esnprintf(arm_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "", irn);
561 arm_fprintf_format(out, cmd_buf, cmnt_buf, irn);
563 lc_esnprintf(arm_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "bhi BLOCK_%d", default_block_num);
564 lc_esnprintf(arm_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "", irn);
565 arm_fprintf_format(out, cmd_buf, cmnt_buf, irn);
568 lc_esnprintf(arm_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "ldr %%r12, TABLE_%d_START", block_nr);
569 lc_esnprintf(arm_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "", irn);
570 arm_fprintf_format(out, cmd_buf, cmnt_buf, irn);
572 lc_esnprintf(arm_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "add %%r12, %%r12, %1S, LSL #2", irn);
573 lc_esnprintf(arm_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "", irn);
574 arm_fprintf_format(out, cmd_buf, cmnt_buf, irn);
576 lc_esnprintf(arm_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "ldr %%r15, [%%r12, #0]");
577 lc_esnprintf(arm_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "", irn);
578 arm_fprintf_format(out, cmd_buf, cmnt_buf, irn);
580 // LDR %r12, .TABLE_X_START
581 // ADD %r12, %r12, [%1S, LSL #2]
584 fprintf(out, "TABLE_%d_START:\n\t.word\tTABLE_%d\n", block_nr, block_nr);
585 fprintf(out, "\t.align 2\n");
586 fprintf(out, "TABLE_%d:\n", block_nr);
589 for ( i=0; i<n_projs; i++) {
593 block = get_irn_link(proj);
595 block = get_irn_link(projs[get_arm_default_proj_num(irn)]);
597 fprintf(out, "\t.word\tBLOCK_%ld\n",get_irn_node_nr(block));
599 fprintf(out, "\t.align 2\n");
604 /************************************************************************/
606 /************************************************************************/
608 static void emit_be_Call(ir_node *irn, void *env) {
609 arm_emit_env_t *emit_env = env;
610 FILE *F = emit_env->out;
611 entity *ent = be_Call_get_entity(irn);
612 char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
615 lc_esnprintf(arm_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "bl %s", get_entity_ld_name(ent));
617 lc_esnprintf(arm_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "%1D", get_irn_n(irn, be_pos_Call_ptr));
618 lc_esnprintf(arm_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* %+F (be_Call) */", irn);
619 arm_fprintf_format(F, cmd_buf, cmnt_buf, irn);
622 /** Emit an IncSP node */
623 static void emit_be_IncSP(const ir_node *irn, arm_emit_env_t *emit_env) {
624 FILE *F = emit_env->out;
625 int offs = be_get_IncSP_offset(irn);
628 char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
629 lc_esnprintf(arm_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "add %1D, %1S, #%O", irn, irn, irn );
630 lc_esnprintf(arm_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* IncSP(%O) */", irn);
631 arm_fprintf_format(F, cmd_buf, cmnt_buf, irn);
633 char cmnt_buf[SNPRINTF_BUF_LEN];
634 lc_esnprintf(arm_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* omitted IncSP(%O) */", irn);
635 arm_fprintf_format(F, "", cmnt_buf, irn);
639 // void emit_be_AddSP(const ir_node *irn, arm_emit_env_t *emit_env) {
640 // FILE *F = emit_env->out;
641 // char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
642 // lc_esnprintf(arm_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "ADD %1D, %1S, %2S", irn, irn, irn );
643 // lc_esnprintf(arm_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* AddSP(%2S) */", irn);
644 // lc_efprintf(arm_get_arg_env(), F, "\t%-35s %-60s /* %+F */\n", cmd_buf, cmnt_buf, irn);
647 static void emit_be_Copy(const ir_node *irn, arm_emit_env_t *emit_env) {
648 FILE *F = emit_env->out;
649 ir_mode *mode = get_irn_mode(irn);
650 const lc_arg_env_t *arm_env = arm_get_arg_env();
651 char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
653 if (get_in_reg(irn, 0) == get_out_reg(irn, 0)) {
654 lc_esnprintf(arm_env, cmnt_buf, SNPRINTF_BUF_LEN, "/* omitted Copy: %1S -> %1D */", irn, irn);
655 lc_efprintf(arm_env, F, "\t%-35s %-60s /* %+F */\n", "", cmnt_buf, irn);
659 if (mode_is_float(mode)) {
660 if (USE_FPA(emit_env->cg->isa)) {
661 lc_esnprintf(arm_env, cmd_buf, SNPRINTF_BUF_LEN, "mvf%M %1D, %1S", irn, irn, irn);
662 lc_esnprintf(arm_env, cmnt_buf, SNPRINTF_BUF_LEN, "/* Copy: %1S -> %1D */", irn, irn);
663 arm_fprintf_format(F, cmd_buf, cmnt_buf, irn);
666 assert(0 && "move not supported for this mode");
668 } else if (mode_is_numP(mode)) {
669 lc_esnprintf(arm_env, cmd_buf, SNPRINTF_BUF_LEN, "mov %1D, %1S", irn, irn);
670 lc_esnprintf(arm_env, cmnt_buf, SNPRINTF_BUF_LEN, "/* Copy: %1S -> %1D */", irn, irn);
671 arm_fprintf_format(F, cmd_buf, cmnt_buf, irn);
673 assert(0 && "move not supported for this mode");
675 // emit_arm_Copy(irn, emit_env);
679 * Emit code for a Spill.
681 static void emit_be_Spill(const ir_node *irn, arm_emit_env_t *emit_env) {
682 FILE *F = emit_env->out;
683 ir_mode *mode = get_irn_mode(irn);
684 const lc_arg_env_t *arm_env = arm_get_arg_env();
685 char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
687 if (mode_is_float(mode)) {
688 if (USE_FPA(emit_env->cg->isa)) {
689 lc_esnprintf(arm_env, cmd_buf, SNPRINTF_BUF_LEN, "stf %2S, [%1S, #%O]", irn, irn, irn );
690 lc_esnprintf(arm_env, cmnt_buf, SNPRINTF_BUF_LEN, "/* Spill(%2S) -> (%1S) */", irn, irn);
691 arm_fprintf_format(F, cmd_buf, cmnt_buf, irn);
694 assert(0 && "move not supported for this mode");
696 } else if (mode_is_dataM(mode)) {
697 lc_esnprintf(arm_env, cmd_buf, SNPRINTF_BUF_LEN, "str %2S, [%1S, #%O]", irn, irn, irn );
698 lc_esnprintf(arm_env, cmnt_buf, SNPRINTF_BUF_LEN, "/* Spill(%2S) -> (%1S) */", irn, irn);
699 arm_fprintf_format(F, cmd_buf, cmnt_buf, irn);
701 assert(0 && "spill not supported for this mode");
706 * Emit code for a Reload.
708 static void emit_be_Reload(const ir_node* irn, arm_emit_env_t *emit_env) {
709 FILE *F = emit_env->out;
710 ir_mode *mode = get_irn_mode(irn);
711 const lc_arg_env_t *arm_env = arm_get_arg_env();
712 char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
714 if (mode_is_float(mode)) {
715 if (USE_FPA(emit_env->cg->isa)) {
716 lc_esnprintf(arm_env, cmd_buf, SNPRINTF_BUF_LEN, "ldf %1D, [%1S, #%O]", irn, irn, irn );
717 lc_esnprintf(arm_env, cmnt_buf, SNPRINTF_BUF_LEN, "/* Reload(%1S) -> (%1D) */", irn, irn);
718 arm_fprintf_format(F, cmd_buf, cmnt_buf, irn);
721 assert(0 && "move not supported for this mode");
723 } else if (mode_is_dataM(mode)) {
724 lc_esnprintf(arm_env, cmd_buf, SNPRINTF_BUF_LEN, "ldr %1D, [%1S, #%O]", irn, irn, irn );
725 lc_esnprintf(arm_env, cmnt_buf, SNPRINTF_BUF_LEN, "/* Reload(%1S) -> (%1D) */", irn, irn);
726 arm_fprintf_format(F, cmd_buf, cmnt_buf, irn);
728 assert(0 && "reload not supported for this mode");
732 static void emit_be_Perm(const ir_node* irn, arm_emit_env_t *emit_env) {
733 FILE *F = emit_env->out;
734 const lc_arg_env_t *arm_env = arm_get_arg_env();
735 char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
737 lc_esnprintf(arm_env, cmd_buf, SNPRINTF_BUF_LEN, "eor %1S, %1S, %2S", irn, irn, irn);
738 lc_esnprintf(arm_env, cmnt_buf, SNPRINTF_BUF_LEN, "/* begin Perm(%1S, %2S) */", irn, irn);
739 arm_fprintf_format(F, cmd_buf, cmnt_buf, irn);
741 lc_esnprintf(arm_env, cmd_buf, SNPRINTF_BUF_LEN, "eor %2S, %1S, %2S", irn, irn, irn);
742 lc_esnprintf(arm_env, cmnt_buf, SNPRINTF_BUF_LEN, " ");
743 arm_fprintf_format(F, cmd_buf, cmnt_buf, irn);
745 lc_esnprintf(arm_env, cmd_buf, SNPRINTF_BUF_LEN, "eor %1S, %1S, %2S", irn, irn, irn);
746 lc_esnprintf(arm_env, cmnt_buf, SNPRINTF_BUF_LEN, "/* end Perm(%1S, %2S) */", irn, irn);
747 arm_fprintf_format(F, cmd_buf, cmnt_buf, irn);
750 static void emit_be_StackParam(const ir_node *irn, arm_emit_env_t *emit_env) {
751 FILE *F = emit_env->out;
752 ir_mode *mode = get_irn_mode(irn);
753 const lc_arg_env_t *arm_env = arm_get_arg_env();
754 char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
756 if (mode_is_float(mode)) {
757 if (USE_FPA(emit_env->cg->isa)) {
758 lc_esnprintf(arm_env, cmd_buf, SNPRINTF_BUF_LEN, "ldf %1D, [%1S, #%O]", irn, irn, irn );
759 lc_esnprintf(arm_env, cmnt_buf, SNPRINTF_BUF_LEN, "/* StackParam: (%1S + %O) -> %1D */",irn , irn, irn, get_irn_n(irn, 0));
760 arm_fprintf_format(F, cmd_buf, cmnt_buf, irn);
763 assert(0 && "move not supported for this mode");
766 lc_esnprintf(arm_env, cmd_buf, SNPRINTF_BUF_LEN, "ldr %1D, [%1S, #%O]", irn, irn, irn );
767 lc_esnprintf(arm_env, cmnt_buf, SNPRINTF_BUF_LEN, "/* StackParam: (%1S + %O) -> %1D */",irn , irn, irn, get_irn_n(irn, 0));
768 arm_fprintf_format(F, cmd_buf, cmnt_buf, irn);
772 /************************************************************************/
774 /************************************************************************/
776 static void emit_Jmp(ir_node *irn, void *env) {
777 char cmd_buf[SNPRINTF_BUF_LEN];
778 arm_emit_env_t *emit_env = env;
779 FILE *F = emit_env->out;
780 const ir_edge_t *edge = get_irn_out_edge_first(irn);
781 const lc_arg_env_t *arm_env = arm_get_arg_env();
782 ir_node *target_block = get_edge_src_irn(edge);
784 lc_esnprintf(arm_env, cmd_buf, SNPRINTF_BUF_LEN, "b BLOCK_%d", get_irn_node_nr(target_block));
785 arm_fprintf_format(F, cmd_buf, "/* unconditional Jump */", irn);
788 static void emit_arm_fpaDbl2GP(const ir_node *n, arm_emit_env_t *env) {
791 const lc_arg_env_t *arg_env = arm_get_arg_env();
793 lc_esnprintf(arg_env, cmd_buf, 256, "stfd %1S, [sp, #-8]! ", n);
794 arm_fprintf_format(F, cmd_buf, "/* Push fp to stack */", n);
796 lc_esnprintf(arg_env, cmd_buf, 256, "ldmfd sp!, {%2D, %1D} ", n, n);
797 arm_fprintf_format(F, cmd_buf, "/* Pop destination */", n);
800 static void emit_silence(ir_node *irn, void *env) {
805 /***********************************************************************************
808 * _ __ ___ __ _ _ _ __ | |_ _ __ __ _ _ __ ___ _____ _____ _ __| | __
809 * | '_ ` _ \ / _` | | '_ \ | _| '__/ _` | '_ ` _ \ / _ \ \ /\ / / _ \| '__| |/ /
810 * | | | | | | (_| | | | | | | | | | | (_| | | | | | | __/\ V V / (_) | | | <
811 * |_| |_| |_|\__,_|_|_| |_| |_| |_| \__,_|_| |_| |_|\___| \_/\_/ \___/|_| |_|\_\
813 ***********************************************************************************/
816 * Enters the emitter functions for handled nodes into the generic
817 * pointer of an opcode.
819 static void arm_register_emitters(void) {
821 #define ARM_EMIT(a) op_arm_##a->ops.generic = (op_func)emit_arm_##a
822 #define EMIT(a) op_##a->ops.generic = (op_func)emit_##a
823 #define BE_EMIT(a) op_be_##a->ops.generic = (op_func)emit_be_##a
824 #define SILENCE(a) op_##a->ops.generic = (op_func)emit_silence
826 /* first clear the generic function pointer for all ops */
827 clear_irp_opcodes_generic_func();
829 /* register all emitter functions defined in spec */
830 arm_register_spec_emitters();
832 /* other emitter functions */
835 // ARM_EMIT(CopyB_i);
859 SILENCE(be_CopyKeep);
868 typedef void (node_emitter)(const ir_node *, void *);
871 * Emits code for a node.
873 static void arm_emit_node(const ir_node *irn, void *env) {
874 arm_emit_env_t *emit_env = env;
875 FILE *F = emit_env->out;
876 ir_op *op = get_irn_op(irn);
877 DEBUG_ONLY(firm_dbg_module_t *mod = emit_env->mod;)
879 DBG((mod, LEVEL_1, "emitting code for %+F\n", irn));
881 if (op->ops.generic) {
882 node_emitter *emit = (node_emitter *)op->ops.generic;
886 ir_fprintf(F, "\t%-35s /* %+F %G */\n", "", irn, irn);
891 * Walks over the nodes in a block connected by scheduling edges
892 * and emits code for each node.
894 void arm_gen_block(ir_node *block, void *env) {
897 fprintf(((arm_emit_env_t *)env)->out, "BLOCK_%ld:\n", get_irn_node_nr(block));
898 sched_foreach(block, irn) {
899 arm_emit_node(irn, env);
905 * Emits code for function start.
907 void arm_emit_start(FILE *F, ir_graph *irg) {
908 entity *ent = get_irg_entity(irg);
909 const char *irg_name = get_entity_ld_name(ent);
910 arm_switch_section(F, SECTION_TEXT);
911 fprintf(F, "\t.align 2\n");
913 if (get_entity_visibility(ent) == visibility_external_visible)
914 fprintf(F, "\t.global %s\n", irg_name);
915 fprintf(F, "%s:\n", irg_name);
919 * Emits code for function end
921 void arm_emit_end(FILE *F, ir_graph *irg) {
922 fprintf(F, "\t.ident \"firmcc\"\n");
926 * Sets labels for control flow nodes (jump target)
927 * TODO: Jump optimization
929 void arm_gen_labels(ir_node *block, void *env) {
931 int n = get_Block_n_cfgpreds(block);
933 for (n--; n >= 0; n--) {
934 pred = get_Block_cfgpred(block, n);
935 set_irn_link(pred, block);
941 * Main driver. Emits the code for one routine.
943 void arm_gen_routine(FILE *F, ir_graph *irg, const arm_code_gen_t *cg) {
944 SymConstEntry *entry;
945 arm_emit_env_t emit_env;
950 emit_env.arch_env = cg->arch_env;
952 emit_env.symbols = NULL;
953 obstack_init(&emit_env.obst);
954 FIRM_DBG_REGISTER(emit_env.mod, "firm.be.arm.emit");
956 /* set the global arch_env (needed by print hooks) */
957 arch_env = cg->arch_env;
959 arm_register_emitters();
961 /* create the block schedule. For now, we don't need it earlier. */
962 blk_sched = sched_create_block_schedule(cg->irg, cg->birg->execfreqs);
964 arm_emit_start(F, irg);
965 irg_block_walk_graph(irg, arm_gen_labels, NULL, &emit_env);
967 n = ARR_LEN(blk_sched);
968 for (i = 0; i < n;) {
969 ir_node *block, *next_bl;
971 block = blk_sched[i];
973 next_bl = i < n ? blk_sched[i] : NULL;
975 /* set here the link. the emitter expects to find the next block here */
976 set_irn_link(block, next_bl);
977 arm_gen_block(block, &emit_env);
980 /* emit SymConst values */
981 if (emit_env.symbols)
982 fprintf(F, "\t.align 2\n");
984 for (entry = emit_env.symbols; entry; entry = entry->next) {
985 fprintf(F, ".L%u:\n", entry->label);
986 lc_efprintf(arm_get_arg_env(), F, "\t.word\t%C\n", entry->symconst);
989 obstack_free(&emit_env.obst, NULL);