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;
356 static void emit_arm_SymConst(ir_node *irn, void *env) {
357 arm_emit_env_t *emit_env = env;
358 FILE *F = emit_env->out;
359 SymConstEntry *entry = obstack_alloc(&emit_env->obst, sizeof(*entry));
360 const lc_arg_env_t *arg_env = arm_get_arg_env();
363 entry->label = get_unique_label();
364 entry->symconst = irn;
365 entry->next = emit_env->symbols;
366 emit_env->symbols = entry;
368 lc_esnprintf(arg_env, cmd_buf, 256, "ldr %1D, .L%u", irn, entry->label);
369 arm_fprintf_format(F, cmd_buf, "/* indirect SymConst */", irn);
373 * Returns the next block in a block schedule.
375 static ir_node *sched_next_block(ir_node *block) {
376 return get_irn_link(block);
380 * Emit a conditional jump.
382 static void emit_arm_CondJmp(ir_node *irn, void *env) {
383 arm_emit_env_t *emit_env = env;
384 FILE *out = emit_env->out;
385 const ir_edge_t *edge;
386 ir_node *true_block = NULL;
387 ir_node *false_block = NULL;
388 ir_node *op1 = get_irn_n(irn, 0);
389 ir_mode *opmode = get_irn_mode(op1);
391 int proj_num = get_arm_proj_num(irn);
392 char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
395 foreach_out_edge(irn, edge) {
396 ir_node *proj = get_edge_src_irn(edge);
397 long nr = get_Proj_proj(proj);
398 ir_node *block = get_irn_link(proj);
399 if ( nr == pn_Cond_true) {
401 } else if (nr == pn_Cond_false) {
404 assert(0 && "tertium non datur! (CondJmp)");
408 if (proj_num == pn_Cmp_False) {
409 /* always false: should not happen */
410 fprintf(out, "\tb BLOCK_%ld\t\t\t/* false case */\n", get_irn_node_nr(false_block));
411 } else if (proj_num == pn_Cmp_True) {
412 /* always true: should not happen */
413 fprintf(out, "\tb BLOCK_%ld\t\t\t/* true case */\n", get_irn_node_nr(true_block));
415 ir_node *block = get_nodes_block(irn);
417 if (mode_is_float(opmode)) {
418 suffix = "ICHWILLIMPLEMENTIERTWERDEN";
420 lc_esnprintf(arm_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "fcmp %1S, %2S", irn, irn);
421 lc_esnprintf(arm_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* Compare(%1S, %2S) -> FCPSR */", irn, irn );
422 arm_fprintf_format(out, cmd_buf, cmnt_buf, irn);
424 lc_esnprintf(arm_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "fmstat", irn, irn);
425 lc_esnprintf(arm_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* FCSPR -> CPSR */");
426 arm_fprintf_format(out, cmd_buf, cmnt_buf, irn);
428 if (true_block == sched_next_block(block)) {
430 proj_num = get_negated_pnc(proj_num, opmode);
433 case pn_Cmp_Eq: suffix = "eq"; break;
434 case pn_Cmp_Lt: suffix = "lt"; break;
435 case pn_Cmp_Le: suffix = "le"; break;
436 case pn_Cmp_Gt: suffix = "gt"; break;
437 case pn_Cmp_Ge: suffix = "ge"; break;
438 case pn_Cmp_Lg: suffix = "ne"; break;
439 case pn_Cmp_Leg: suffix = "al"; break;
440 default: assert(0 && "komische Dinge geschehen"); suffix = "al";
443 lc_esnprintf(arm_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "cmp %1S, %2S", irn, irn);
444 lc_esnprintf(arm_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* Compare(%1S, %2S) -> CPSR */", irn, irn );
445 arm_fprintf_format(out, cmd_buf, cmnt_buf, irn);
448 if (true_block == sched_next_block(block)) {
449 lc_esnprintf(arm_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "b%s BLOCK_%d", suffix, get_irn_node_nr(true_block));
450 lc_esnprintf(arm_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* false case */");
451 arm_fprintf_format(out, cmd_buf, cmnt_buf, irn);
453 lc_esnprintf(arm_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* fallthrough BLOCK_%d */", get_irn_node_nr(false_block));
454 arm_fprintf_format(out, "", cmnt_buf, irn);
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, "/* true case */");
459 arm_fprintf_format(out, cmd_buf, cmnt_buf, irn);
461 if (false_block == sched_next_block(block)) {
462 lc_esnprintf(arm_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* fallthrough BLOCK_%d */", get_irn_node_nr(false_block));
463 arm_fprintf_format(out, "", cmnt_buf, irn);
466 lc_esnprintf(arm_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "b BLOCK_%d", get_irn_node_nr(false_block));
467 lc_esnprintf(arm_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* false case */");
468 arm_fprintf_format(out, cmd_buf, cmnt_buf, irn);
474 static void emit_arm_CopyB(ir_node *irn, void *env) {
475 arm_emit_env_t *emit_env = env;
476 FILE *out = emit_env->out;
477 char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
478 unsigned int size = get_tarval_long(get_arm_value(irn));
479 const lc_arg_env_t *arg_env = arm_get_arg_env();
481 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);
483 assert ( size > 0 && "CopyB needs size > 0" );
491 lc_esnprintf(arg_env, cmd_buf, SNPRINTF_BUF_LEN, "ldr %%r12, [%2S, #0]!", irn);
492 arm_fprintf_format(out, cmd_buf, cmnt_buf, irn);
493 lc_esnprintf(arg_env, cmd_buf, SNPRINTF_BUF_LEN, "str %%r12, [%1S, #0]!", irn);
494 arm_fprintf_format(out, cmd_buf, cmnt_buf, irn);
497 lc_esnprintf(arg_env, cmd_buf, SNPRINTF_BUF_LEN, "ldmia %2S!, {%%r12, %3S}", irn, irn);
498 arm_fprintf_format(out, cmd_buf, cmnt_buf, irn);
499 lc_esnprintf(arg_env, cmd_buf, SNPRINTF_BUF_LEN, "stmia %1S!, {%%r12, %3S}", irn, irn);
500 arm_fprintf_format(out, cmd_buf, cmnt_buf, irn);
503 lc_esnprintf(arg_env, cmd_buf, SNPRINTF_BUF_LEN, "ldmia %2S!, {%%r12, %3S, %4S}", irn, irn, irn);
504 arm_fprintf_format(out, cmd_buf, cmnt_buf, irn);
505 lc_esnprintf(arg_env, cmd_buf, SNPRINTF_BUF_LEN, "stmia %1S!, {%%r12, %3S, %4S}", irn, irn, irn);
506 arm_fprintf_format(out, cmd_buf, cmnt_buf, irn);
511 lc_esnprintf(arg_env, cmd_buf, SNPRINTF_BUF_LEN, "ldmia %2S!, {%%r12, %3S, %4S, %5S}", irn, 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, %5S}", irn, irn, irn, irn);
514 arm_fprintf_format(out, cmd_buf, cmnt_buf, irn);
519 static void emit_arm_SwitchJmp(ir_node *irn, void *env) {
520 char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
521 const ir_edge_t *edge;
523 arm_emit_env_t *emit_env = env;
524 FILE *out = emit_env->out;
529 int default_block_num;
531 block_nr = get_irn_node_nr(irn);
532 n_projs = get_arm_n_projs(irn);
534 projs = xcalloc(n_projs , sizeof(ir_node*));
536 foreach_out_edge(irn, edge) {
537 proj = get_edge_src_irn(edge);
538 assert(is_Proj(proj) && "Only proj allowed at SwitchJmp");
540 if (get_Proj_proj(proj) == get_arm_default_proj_num(irn))
541 default_block_num = get_irn_node_nr(get_irn_link(proj));
543 projs[get_Proj_proj(proj)] = proj;
546 // CMP %1S, n_projs - 1
551 lc_esnprintf(arm_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "cmp %1S, #%u", irn, n_projs - 1);
552 lc_esnprintf(arm_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "", irn);
553 arm_fprintf_format(out, cmd_buf, cmnt_buf, irn);
555 lc_esnprintf(arm_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "bhi BLOCK_%d", default_block_num);
556 lc_esnprintf(arm_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "", irn);
557 arm_fprintf_format(out, cmd_buf, cmnt_buf, irn);
560 lc_esnprintf(arm_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "ldr %%r12, TABLE_%d_START", block_nr);
561 lc_esnprintf(arm_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "", irn);
562 arm_fprintf_format(out, cmd_buf, cmnt_buf, irn);
564 lc_esnprintf(arm_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "add %%r12, %%r12, %1S, LSL #2", irn);
565 lc_esnprintf(arm_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "", irn);
566 arm_fprintf_format(out, cmd_buf, cmnt_buf, irn);
568 lc_esnprintf(arm_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "ldr %%r15, [%%r12, #0]");
569 lc_esnprintf(arm_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "", irn);
570 arm_fprintf_format(out, cmd_buf, cmnt_buf, irn);
572 // LDR %r12, .TABLE_X_START
573 // ADD %r12, %r12, [%1S, LSL #2]
576 fprintf(out, "TABLE_%d_START:\n\t.word\tTABLE_%d\n", block_nr, block_nr);
577 fprintf(out, "\t.align 2\n");
578 fprintf(out, "TABLE_%d:\n", block_nr);
581 for ( i=0; i<n_projs; i++) {
585 block = get_irn_link(proj);
587 block = get_irn_link(projs[get_arm_default_proj_num(irn)]);
589 fprintf(out, "\t.word\tBLOCK_%ld\n",get_irn_node_nr(block));
591 fprintf(out, "\t.align 2\n");
596 /************************************************************************/
598 /************************************************************************/
600 static void emit_be_Call(ir_node *irn, void *env) {
601 arm_emit_env_t *emit_env = env;
602 FILE *F = emit_env->out;
603 ir_entity *ent = be_Call_get_entity(irn);
604 char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
607 lc_esnprintf(arm_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "bl %s", get_entity_ld_name(ent));
609 lc_esnprintf(arm_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "%1D", get_irn_n(irn, be_pos_Call_ptr));
610 lc_esnprintf(arm_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* %+F (be_Call) */", irn);
611 arm_fprintf_format(F, cmd_buf, cmnt_buf, irn);
614 /** Emit an IncSP node */
615 static void emit_be_IncSP(const ir_node *irn, arm_emit_env_t *emit_env) {
616 FILE *F = emit_env->out;
617 int offs = be_get_IncSP_offset(irn);
620 char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
621 lc_esnprintf(arm_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "add %1D, %1S, #%O", irn, irn, irn );
622 lc_esnprintf(arm_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* IncSP(%O) */", irn);
623 arm_fprintf_format(F, cmd_buf, cmnt_buf, irn);
625 char cmnt_buf[SNPRINTF_BUF_LEN];
626 lc_esnprintf(arm_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* omitted IncSP(%O) */", irn);
627 arm_fprintf_format(F, "", cmnt_buf, irn);
631 // void emit_be_AddSP(const ir_node *irn, arm_emit_env_t *emit_env) {
632 // FILE *F = emit_env->out;
633 // char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
634 // lc_esnprintf(arm_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "ADD %1D, %1S, %2S", irn, irn, irn );
635 // lc_esnprintf(arm_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* AddSP(%2S) */", irn);
636 // lc_efprintf(arm_get_arg_env(), F, "\t%-35s %-60s /* %+F */\n", cmd_buf, cmnt_buf, irn);
639 static void emit_be_Copy(const ir_node *irn, arm_emit_env_t *emit_env) {
640 FILE *F = emit_env->out;
641 ir_mode *mode = get_irn_mode(irn);
642 const lc_arg_env_t *arm_env = arm_get_arg_env();
643 char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
645 if (get_in_reg(irn, 0) == get_out_reg(irn, 0)) {
646 lc_esnprintf(arm_env, cmnt_buf, SNPRINTF_BUF_LEN, "/* omitted Copy: %1S -> %1D */", irn, irn);
647 lc_efprintf(arm_env, F, "\t%-35s %-60s /* %+F */\n", "", cmnt_buf, irn);
651 if (mode_is_float(mode)) {
652 if (USE_FPA(emit_env->cg->isa)) {
653 lc_esnprintf(arm_env, cmd_buf, SNPRINTF_BUF_LEN, "mvf%M %1D, %1S", irn, irn, irn);
654 lc_esnprintf(arm_env, cmnt_buf, SNPRINTF_BUF_LEN, "/* Copy: %1S -> %1D */", irn, irn);
655 arm_fprintf_format(F, cmd_buf, cmnt_buf, irn);
658 assert(0 && "move not supported for this mode");
660 } else if (mode_is_numP(mode)) {
661 lc_esnprintf(arm_env, cmd_buf, SNPRINTF_BUF_LEN, "mov %1D, %1S", 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);
665 assert(0 && "move not supported for this mode");
667 // emit_arm_Copy(irn, emit_env);
671 * Emit code for a Spill.
673 static void emit_be_Spill(const ir_node *irn, arm_emit_env_t *emit_env) {
674 FILE *F = emit_env->out;
675 ir_mode *mode = get_irn_mode(irn);
676 const lc_arg_env_t *arm_env = arm_get_arg_env();
677 char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
679 if (mode_is_float(mode)) {
680 if (USE_FPA(emit_env->cg->isa)) {
681 lc_esnprintf(arm_env, cmd_buf, SNPRINTF_BUF_LEN, "stf %2S, [%1S, #%O]", irn, irn, irn );
682 lc_esnprintf(arm_env, cmnt_buf, SNPRINTF_BUF_LEN, "/* Spill(%2S) -> (%1S) */", irn, irn);
683 arm_fprintf_format(F, cmd_buf, cmnt_buf, irn);
686 assert(0 && "move not supported for this mode");
688 } else if (mode_is_dataM(mode)) {
689 lc_esnprintf(arm_env, cmd_buf, SNPRINTF_BUF_LEN, "str %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);
693 assert(0 && "spill not supported for this mode");
698 * Emit code for a Reload.
700 static void emit_be_Reload(const ir_node* irn, arm_emit_env_t *emit_env) {
701 FILE *F = emit_env->out;
702 ir_mode *mode = get_irn_mode(irn);
703 const lc_arg_env_t *arm_env = arm_get_arg_env();
704 char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
706 if (mode_is_float(mode)) {
707 if (USE_FPA(emit_env->cg->isa)) {
708 lc_esnprintf(arm_env, cmd_buf, SNPRINTF_BUF_LEN, "ldf %1D, [%1S, #%O]", irn, irn, irn );
709 lc_esnprintf(arm_env, cmnt_buf, SNPRINTF_BUF_LEN, "/* Reload(%1S) -> (%1D) */", irn, irn);
710 arm_fprintf_format(F, cmd_buf, cmnt_buf, irn);
713 assert(0 && "move not supported for this mode");
715 } else if (mode_is_dataM(mode)) {
716 lc_esnprintf(arm_env, cmd_buf, SNPRINTF_BUF_LEN, "ldr %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);
720 assert(0 && "reload not supported for this mode");
724 static void emit_be_Perm(const ir_node* irn, arm_emit_env_t *emit_env) {
725 FILE *F = emit_env->out;
726 const lc_arg_env_t *arm_env = arm_get_arg_env();
727 char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
729 lc_esnprintf(arm_env, cmd_buf, SNPRINTF_BUF_LEN, "eor %1S, %1S, %2S", irn, irn, irn);
730 lc_esnprintf(arm_env, cmnt_buf, SNPRINTF_BUF_LEN, "/* begin Perm(%1S, %2S) */", irn, irn);
731 arm_fprintf_format(F, cmd_buf, cmnt_buf, irn);
733 lc_esnprintf(arm_env, cmd_buf, SNPRINTF_BUF_LEN, "eor %2S, %1S, %2S", irn, irn, irn);
734 lc_esnprintf(arm_env, cmnt_buf, SNPRINTF_BUF_LEN, " ");
735 arm_fprintf_format(F, cmd_buf, cmnt_buf, irn);
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, "/* end Perm(%1S, %2S) */", irn, irn);
739 arm_fprintf_format(F, cmd_buf, cmnt_buf, irn);
742 static void emit_be_StackParam(const ir_node *irn, arm_emit_env_t *emit_env) {
743 FILE *F = emit_env->out;
744 ir_mode *mode = get_irn_mode(irn);
745 const lc_arg_env_t *arm_env = arm_get_arg_env();
746 char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
748 if (mode_is_float(mode)) {
749 if (USE_FPA(emit_env->cg->isa)) {
750 lc_esnprintf(arm_env, cmd_buf, SNPRINTF_BUF_LEN, "ldf %1D, [%1S, #%O]", irn, irn, irn );
751 lc_esnprintf(arm_env, cmnt_buf, SNPRINTF_BUF_LEN, "/* StackParam: (%1S + %O) -> %1D */",irn , irn, irn, get_irn_n(irn, 0));
752 arm_fprintf_format(F, cmd_buf, cmnt_buf, irn);
755 assert(0 && "move not supported for this mode");
758 lc_esnprintf(arm_env, cmd_buf, SNPRINTF_BUF_LEN, "ldr %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);
764 /************************************************************************/
766 /************************************************************************/
768 static void emit_Jmp(ir_node *irn, void *env) {
769 char cmd_buf[SNPRINTF_BUF_LEN];
770 arm_emit_env_t *emit_env = env;
771 FILE *F = emit_env->out;
772 const ir_edge_t *edge = get_irn_out_edge_first(irn);
773 const lc_arg_env_t *arm_env = arm_get_arg_env();
774 ir_node *target_block = get_edge_src_irn(edge);
776 lc_esnprintf(arm_env, cmd_buf, SNPRINTF_BUF_LEN, "b BLOCK_%d", get_irn_node_nr(target_block));
777 arm_fprintf_format(F, cmd_buf, "/* unconditional Jump */", irn);
780 static void emit_arm_fpaDbl2GP(const ir_node *n, arm_emit_env_t *env) {
783 const lc_arg_env_t *arg_env = arm_get_arg_env();
785 lc_esnprintf(arg_env, cmd_buf, 256, "stfd %1S, [sp, #-8]! ", n);
786 arm_fprintf_format(F, cmd_buf, "/* Push fp to stack */", n);
788 lc_esnprintf(arg_env, cmd_buf, 256, "ldmfd sp!, {%2D, %1D} ", n, n);
789 arm_fprintf_format(F, cmd_buf, "/* Pop destination */", n);
792 static void emit_silence(ir_node *irn, void *env) {
797 /***********************************************************************************
800 * _ __ ___ __ _ _ _ __ | |_ _ __ __ _ _ __ ___ _____ _____ _ __| | __
801 * | '_ ` _ \ / _` | | '_ \ | _| '__/ _` | '_ ` _ \ / _ \ \ /\ / / _ \| '__| |/ /
802 * | | | | | | (_| | | | | | | | | | | (_| | | | | | | __/\ V V / (_) | | | <
803 * |_| |_| |_|\__,_|_|_| |_| |_| |_| \__,_|_| |_| |_|\___| \_/\_/ \___/|_| |_|\_\
805 ***********************************************************************************/
808 * Enters the emitter functions for handled nodes into the generic
809 * pointer of an opcode.
811 static void arm_register_emitters(void) {
813 #define ARM_EMIT(a) op_arm_##a->ops.generic = (op_func)emit_arm_##a
814 #define EMIT(a) op_##a->ops.generic = (op_func)emit_##a
815 #define BE_EMIT(a) op_be_##a->ops.generic = (op_func)emit_be_##a
816 #define SILENCE(a) op_##a->ops.generic = (op_func)emit_silence
818 /* first clear the generic function pointer for all ops */
819 clear_irp_opcodes_generic_func();
821 /* register all emitter functions defined in spec */
822 arm_register_spec_emitters();
824 /* other emitter functions */
827 // ARM_EMIT(CopyB_i);
851 SILENCE(be_CopyKeep);
860 typedef void (node_emitter)(const ir_node *, void *);
863 * Emits code for a node.
865 static void arm_emit_node(const ir_node *irn, void *env) {
866 arm_emit_env_t *emit_env = env;
867 FILE *F = emit_env->out;
868 ir_op *op = get_irn_op(irn);
869 DEBUG_ONLY(firm_dbg_module_t *mod = emit_env->mod;)
871 DBG((mod, LEVEL_1, "emitting code for %+F\n", irn));
873 if (op->ops.generic) {
874 node_emitter *emit = (node_emitter *)op->ops.generic;
878 ir_fprintf(F, "\t%-35s /* %+F %G */\n", "", irn, irn);
883 * Walks over the nodes in a block connected by scheduling edges
884 * and emits code for each node.
886 void arm_gen_block(ir_node *block, void *env) {
889 fprintf(((arm_emit_env_t *)env)->out, "BLOCK_%ld:\n", get_irn_node_nr(block));
890 sched_foreach(block, irn) {
891 arm_emit_node(irn, env);
897 * Emits code for function start.
899 void arm_emit_start(FILE *F, ir_graph *irg) {
900 ir_entity *ent = get_irg_entity(irg);
901 const char *irg_name = get_entity_ld_name(ent);
902 arm_switch_section(F, SECTION_TEXT);
903 fprintf(F, "\t.align 2\n");
905 if (get_entity_visibility(ent) == visibility_external_visible)
906 fprintf(F, "\t.global %s\n", irg_name);
907 fprintf(F, "%s:\n", irg_name);
911 * Emits code for function end
913 void arm_emit_end(FILE *F, ir_graph *irg) {
914 fprintf(F, "\t.ident \"firmcc\"\n");
918 * Sets labels for control flow nodes (jump target)
919 * TODO: Jump optimization
921 void arm_gen_labels(ir_node *block, void *env) {
923 int n = get_Block_n_cfgpreds(block);
925 for (n--; n >= 0; n--) {
926 pred = get_Block_cfgpred(block, n);
927 set_irn_link(pred, block);
933 * Main driver. Emits the code for one routine.
935 void arm_gen_routine(FILE *F, ir_graph *irg, const arm_code_gen_t *cg) {
936 SymConstEntry *entry;
937 arm_emit_env_t emit_env;
942 emit_env.arch_env = cg->arch_env;
944 emit_env.symbols = NULL;
945 obstack_init(&emit_env.obst);
946 FIRM_DBG_REGISTER(emit_env.mod, "firm.be.arm.emit");
948 /* set the global arch_env (needed by print hooks) */
949 arch_env = cg->arch_env;
951 arm_register_emitters();
953 /* create the block schedule. For now, we don't need it earlier. */
954 blk_sched = sched_create_block_schedule(cg->irg, cg->birg->exec_freq);
956 arm_emit_start(F, irg);
957 irg_block_walk_graph(irg, arm_gen_labels, NULL, &emit_env);
959 n = ARR_LEN(blk_sched);
960 for (i = 0; i < n;) {
961 ir_node *block, *next_bl;
963 block = blk_sched[i];
965 next_bl = i < n ? blk_sched[i] : NULL;
967 /* set here the link. the emitter expects to find the next block here */
968 set_irn_link(block, next_bl);
969 arm_gen_block(block, &emit_env);
972 /* emit SymConst values */
973 if (emit_env.symbols)
974 fprintf(F, "\t.align 2\n");
976 for (entry = emit_env.symbols; entry; entry = entry->next) {
977 fprintf(F, ".L%u:\n", entry->label);
978 lc_efprintf(arm_get_arg_env(), F, "\t.word\t%C\n", entry->symconst);
981 obstack_free(&emit_env.obst, NULL);