2 * Copyright (C) 1995-2007 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
6 * This file may be distributed and/or modified under the terms of the
7 * GNU General Public License version 2 as published by the Free Software
8 * Foundation and appearing in the file LICENSE.GPL included in the
9 * packaging of this file.
11 * Licensees holding valid libFirm Professional Edition licenses may use
12 * this file in accordance with the libFirm Commercial License.
13 * Agreement provided with the Software.
15 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
40 #include "../besched.h"
41 #include "../beblocksched.h"
42 #include "../beirg_t.h"
44 #include "arm_emitter.h"
45 #include "gen_arm_emitter.h"
46 #include "arm_nodes_attr.h"
47 #include "arm_new_nodes.h"
48 #include "arm_map_regs.h"
49 #include "gen_arm_regalloc_if.h"
51 #include "../benode_t.h"
53 #define SNPRINTF_BUF_LEN 128
55 static const arch_env_t *arch_env = NULL;
58 * Switch to a new section
60 void arm_switch_section(FILE *f, sections sec) {
61 static sections curr_sec = NO_SECTION;
73 fprintf(f, "\t.text\n");
77 fprintf(f, "\t.data\n");
85 /*************************************************************
87 * (_) | | / _| | | | |
88 * _ __ _ __ _ _ __ | |_| |_ | |__ ___| |_ __ ___ _ __
89 * | '_ \| '__| | '_ \| __| _| | '_ \ / _ \ | '_ \ / _ \ '__|
90 * | |_) | | | | | | | |_| | | | | | __/ | |_) | __/ |
91 * | .__/|_| |_|_| |_|\__|_| |_| |_|\___|_| .__/ \___|_|
94 *************************************************************/
97 * Returns non-zero if a mode has a Immediate attribute.
99 int is_immediate_node(ir_node *irn) {
100 arm_attr_t *attr = get_arm_attr(irn);
101 return ARM_GET_SHF_MOD(attr) == ARM_SHF_IMM;
105 * Return a const or SymConst as string.
107 static const char *node_const_to_str(ir_node *n, char *buf, int buflen) {
108 if (is_immediate_node(n)) {
109 snprintf(buf, buflen, "#0x%X", arm_decode_imm_w_shift(get_arm_value(n)));
112 else if (is_arm_SymConst(n))
113 return get_arm_symconst_label(n);
115 assert( 0 && "das ist gar keine Konstante");
120 * Returns node's offset as string.
122 static const char *node_offset_to_str(ir_node *n, char *buf, int buflen) {
124 ir_op *irn_op = get_irn_op(n);
126 if (irn_op == op_be_StackParam) {
127 ir_entity *ent = be_get_frame_entity(n);
128 offset = get_entity_offset(ent);
129 } else if (irn_op == op_be_Reload || irn_op == op_be_Spill) {
130 ir_entity *ent = be_get_frame_entity(n);
131 offset = get_entity_offset(ent);
132 } else if (irn_op == op_be_IncSP) {
133 offset = - be_get_IncSP_offset(n);
135 return "node_offset_to_str will fuer diesen Knotentyp noch implementiert werden";
137 snprintf(buf, buflen, "%d", offset);
141 /* We always pass the ir_node which is a pointer. */
142 static int arm_get_arg_type(const lc_arg_occ_t *occ) {
143 return lc_arg_type_ptr;
148 * Returns the register at in position pos.
150 static const arch_register_t *get_in_reg(const ir_node *irn, int pos) {
152 const arch_register_t *reg = NULL;
154 assert(get_irn_arity(irn) > pos && "Invalid IN position");
156 /* The out register of the operator at position pos is the
157 in register we need. */
158 op = get_irn_n(irn, pos);
160 reg = arch_get_irn_register(arch_env, op);
162 assert(reg && "no in register found");
167 * Returns the register at out position pos.
169 static const arch_register_t *get_out_reg(const ir_node *irn, int pos) {
171 const arch_register_t *reg = NULL;
173 assert(get_irn_n_edges(irn) > pos && "Invalid OUT position");
175 /* 1st case: irn is not of mode_T, so it has only */
176 /* one OUT register -> good */
177 /* 2nd case: irn is of mode_T -> collect all Projs and ask the */
178 /* Proj with the corresponding projnum for the register */
180 if (get_irn_mode(irn) != mode_T) {
181 reg = arch_get_irn_register(arch_env, irn);
183 else if (is_arm_irn(irn)) {
184 reg = get_arm_out_reg(irn, pos);
187 const ir_edge_t *edge;
189 foreach_out_edge(irn, edge) {
190 proj = get_edge_src_irn(edge);
191 assert(is_Proj(proj) && "non-Proj from mode_T node");
192 if (get_Proj_proj(proj) == pos) {
193 reg = arch_get_irn_register(arch_env, proj);
199 assert(reg && "no out register found");
204 * Returns the number of the in register at position pos.
206 int get_arm_reg_nr(ir_node *irn, int pos, int in_out) {
207 const arch_register_t *reg;
210 reg = get_in_reg(irn, pos);
213 reg = get_out_reg(irn, pos);
216 return arch_register_get_index(reg);
220 * Returns the name of the in register at position pos.
222 const char *get_arm_reg_name(ir_node *irn, int pos, int in_out) {
223 const arch_register_t *reg;
226 reg = get_in_reg(irn, pos);
229 reg = get_out_reg(irn, pos);
232 return arch_register_get_name(reg);
236 * Get the register name for a node.
238 static int arm_get_reg_name(lc_appendable_t *app,
239 const lc_arg_occ_t *occ, const lc_arg_value_t *arg)
242 ir_node *X = arg->v_ptr;
243 int nr = occ->width - 1;
246 return lc_appendable_snadd(app, "(null)", 6);
248 if (occ->conversion == 'S') {
249 buf = get_arm_reg_name(X, nr, 1);
252 buf = get_arm_reg_name(X, nr, 0);
255 lc_appendable_chadd(app, '%');
256 return lc_appendable_snadd(app, buf, strlen(buf));
260 * Returns the tarval or offset of an arm node as a string.
262 static int arm_const_to_str(lc_appendable_t *app,
263 const lc_arg_occ_t *occ, const lc_arg_value_t *arg)
265 char buffer[SNPRINTF_BUF_LEN];
267 ir_node *X = arg->v_ptr;
270 return lc_appendable_snadd(app, "(null)", 6);
272 if (occ->conversion == 'C') {
273 buf = node_const_to_str(X, buffer, sizeof(buffer));
276 buf = node_offset_to_str(X, buffer, sizeof(buffer));
279 return lc_appendable_snadd(app, buf, strlen(buf));
283 * Returns the tarval or offset of an arm node as a string.
285 static int arm_shift_str(lc_appendable_t *app,
286 const lc_arg_occ_t *occ, const lc_arg_value_t *arg)
288 char buffer[SNPRINTF_BUF_LEN];
289 ir_node *irn = arg->v_ptr;
290 arm_shift_modifier mod;
293 return lc_appendable_snadd(app, "(null)", 6);
295 mod = get_arm_shift_modifier(irn);
296 if (ARM_HAS_SHIFT(mod)) {
297 long v = get_tarval_long(get_arm_value(irn));
299 snprintf(buffer, sizeof(buffer), ", %s #%ld", arm_shf_mod_name(mod), v);
300 return lc_appendable_snadd(app, buffer, strlen(buffer));
306 * Determines the instruction suffix depending on the mode.
308 static int arm_get_mode_suffix(lc_appendable_t *app,
309 const lc_arg_occ_t *occ, const lc_arg_value_t *arg)
311 ir_node *irn = arg->v_ptr;
317 return lc_appendable_snadd(app, "(null)", 6);
319 attr = get_arm_attr(irn);
320 mode = attr->op_mode ? attr->op_mode : get_irn_mode(irn);
321 bits = get_mode_size_bits(mode);
324 return lc_appendable_chadd(app, 's');
326 return lc_appendable_chadd(app, 'd');
328 return lc_appendable_chadd(app, 'e');
332 * Return the arm printf arg environment.
333 * We use the firm environment with some additional handlers.
335 const lc_arg_env_t *arm_get_arg_env(void) {
336 static lc_arg_env_t *env = NULL;
338 static const lc_arg_handler_t arm_reg_handler = { arm_get_arg_type, arm_get_reg_name };
339 static const lc_arg_handler_t arm_const_handler = { arm_get_arg_type, arm_const_to_str };
340 static const lc_arg_handler_t arm_mode_handler = { arm_get_arg_type, arm_get_mode_suffix };
341 static const lc_arg_handler_t arm_shf_handler = { arm_get_arg_type, arm_shift_str };
344 /* extend the firm printer */
345 env = firm_get_arg_env();
348 lc_arg_register(env, "arm:sreg", 'S', &arm_reg_handler);
349 lc_arg_register(env, "arm:dreg", 'D', &arm_reg_handler);
350 lc_arg_register(env, "arm:cnst", 'C', &arm_const_handler);
351 lc_arg_register(env, "arm:offs", 'O', &arm_const_handler);
352 lc_arg_register(env, "arm:mode", 'M', &arm_mode_handler);
353 lc_arg_register(env, "arm:shf", 'X', &arm_shf_handler);
360 * Formated print of commands and comments.
362 static void arm_fprintf_format(FILE *F, const char *cmd_buf, const char *cmnt_buf, const ir_node *irn) {
363 lc_efprintf(arm_get_arg_env(), F, "\t%-35s %-60s /* %+F (%G) */\n", cmd_buf, cmnt_buf, irn, irn);
367 * Returns a unique label. This number will not be used a second time.
369 static unsigned get_unique_label(void) {
370 static unsigned id = 0;
377 static void emit_arm_SymConst(ir_node *irn, void *env) {
378 arm_emit_env_t *emit_env = env;
379 FILE *F = emit_env->out;
380 SymConstEntry *entry = obstack_alloc(&emit_env->obst, sizeof(*entry));
381 const lc_arg_env_t *arg_env = arm_get_arg_env();
384 entry->label = get_unique_label();
385 entry->symconst = irn;
386 entry->next = emit_env->symbols;
387 emit_env->symbols = entry;
389 lc_esnprintf(arg_env, cmd_buf, 256, "ldr %1D, .L%u", irn, entry->label);
390 arm_fprintf_format(F, cmd_buf, "/* indirect SymConst */", irn);
394 * Returns the next block in a block schedule.
396 static ir_node *sched_next_block(ir_node *block) {
397 return get_irn_link(block);
401 * Emit a conditional jump.
403 static void emit_arm_CondJmp(ir_node *irn, void *env) {
404 arm_emit_env_t *emit_env = env;
405 FILE *out = emit_env->out;
406 const ir_edge_t *edge;
407 ir_node *true_block = NULL;
408 ir_node *false_block = NULL;
409 ir_node *op1 = get_irn_n(irn, 0);
410 ir_mode *opmode = get_irn_mode(op1);
412 int proj_num = get_arm_proj_num(irn);
413 char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
416 foreach_out_edge(irn, edge) {
417 ir_node *proj = get_edge_src_irn(edge);
418 long nr = get_Proj_proj(proj);
419 ir_node *block = get_irn_link(proj);
420 if ( nr == pn_Cond_true) {
422 } else if (nr == pn_Cond_false) {
425 assert(0 && "tertium non datur! (CondJmp)");
429 if (proj_num == pn_Cmp_False) {
430 /* always false: should not happen */
431 fprintf(out, "\tb BLOCK_%ld\t\t\t/* false case */\n", get_irn_node_nr(false_block));
432 } else if (proj_num == pn_Cmp_True) {
433 /* always true: should not happen */
434 fprintf(out, "\tb BLOCK_%ld\t\t\t/* true case */\n", get_irn_node_nr(true_block));
436 ir_node *block = get_nodes_block(irn);
438 if (mode_is_float(opmode)) {
439 suffix = "ICHWILLIMPLEMENTIERTWERDEN";
441 lc_esnprintf(arm_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "fcmp %1S, %2S", irn, irn);
442 lc_esnprintf(arm_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* Compare(%1S, %2S) -> FCPSR */", irn, irn );
443 arm_fprintf_format(out, cmd_buf, cmnt_buf, irn);
445 lc_esnprintf(arm_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "fmstat", irn, irn);
446 lc_esnprintf(arm_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* FCSPR -> CPSR */");
447 arm_fprintf_format(out, cmd_buf, cmnt_buf, irn);
449 if (true_block == sched_next_block(block)) {
451 proj_num = get_negated_pnc(proj_num, opmode);
454 case pn_Cmp_Eq: suffix = "eq"; break;
455 case pn_Cmp_Lt: suffix = "lt"; break;
456 case pn_Cmp_Le: suffix = "le"; break;
457 case pn_Cmp_Gt: suffix = "gt"; break;
458 case pn_Cmp_Ge: suffix = "ge"; break;
459 case pn_Cmp_Lg: suffix = "ne"; break;
460 case pn_Cmp_Leg: suffix = "al"; break;
461 default: assert(0 && "komische Dinge geschehen"); suffix = "al";
464 lc_esnprintf(arm_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "cmp %1S, %2S", irn, irn);
465 lc_esnprintf(arm_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* Compare(%1S, %2S) -> CPSR */", irn, irn );
466 arm_fprintf_format(out, cmd_buf, cmnt_buf, irn);
469 if (true_block == sched_next_block(block)) {
470 lc_esnprintf(arm_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "b%s BLOCK_%d", suffix, get_irn_node_nr(true_block));
471 lc_esnprintf(arm_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* false case */");
472 arm_fprintf_format(out, cmd_buf, cmnt_buf, irn);
474 lc_esnprintf(arm_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* fallthrough BLOCK_%d */", get_irn_node_nr(false_block));
475 arm_fprintf_format(out, "", cmnt_buf, irn);
478 lc_esnprintf(arm_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "b%s BLOCK_%d", suffix, get_irn_node_nr(true_block));
479 lc_esnprintf(arm_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* true case */");
480 arm_fprintf_format(out, cmd_buf, cmnt_buf, irn);
482 if (false_block == sched_next_block(block)) {
483 lc_esnprintf(arm_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* fallthrough BLOCK_%d */", get_irn_node_nr(false_block));
484 arm_fprintf_format(out, "", cmnt_buf, irn);
487 lc_esnprintf(arm_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "b BLOCK_%d", get_irn_node_nr(false_block));
488 lc_esnprintf(arm_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* false case */");
489 arm_fprintf_format(out, cmd_buf, cmnt_buf, irn);
495 static void emit_arm_CopyB(ir_node *irn, void *env) {
496 arm_emit_env_t *emit_env = env;
497 FILE *out = emit_env->out;
498 char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
499 unsigned int size = get_tarval_long(get_arm_value(irn));
500 const lc_arg_env_t *arg_env = arm_get_arg_env();
502 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);
504 assert ( size > 0 && "CopyB needs size > 0" );
512 lc_esnprintf(arg_env, cmd_buf, SNPRINTF_BUF_LEN, "ldr %%r12, [%2S, #0]!", irn);
513 arm_fprintf_format(out, cmd_buf, cmnt_buf, irn);
514 lc_esnprintf(arg_env, cmd_buf, SNPRINTF_BUF_LEN, "str %%r12, [%1S, #0]!", irn);
515 arm_fprintf_format(out, cmd_buf, cmnt_buf, irn);
518 lc_esnprintf(arg_env, cmd_buf, SNPRINTF_BUF_LEN, "ldmia %2S!, {%%r12, %3S}", irn, irn);
519 arm_fprintf_format(out, cmd_buf, cmnt_buf, irn);
520 lc_esnprintf(arg_env, cmd_buf, SNPRINTF_BUF_LEN, "stmia %1S!, {%%r12, %3S}", irn, irn);
521 arm_fprintf_format(out, cmd_buf, cmnt_buf, irn);
524 lc_esnprintf(arg_env, cmd_buf, SNPRINTF_BUF_LEN, "ldmia %2S!, {%%r12, %3S, %4S}", irn, irn, irn);
525 arm_fprintf_format(out, cmd_buf, cmnt_buf, irn);
526 lc_esnprintf(arg_env, cmd_buf, SNPRINTF_BUF_LEN, "stmia %1S!, {%%r12, %3S, %4S}", irn, irn, irn);
527 arm_fprintf_format(out, cmd_buf, cmnt_buf, irn);
532 lc_esnprintf(arg_env, cmd_buf, SNPRINTF_BUF_LEN, "ldmia %2S!, {%%r12, %3S, %4S, %5S}", irn, irn, irn, irn);
533 arm_fprintf_format(out, cmd_buf, cmnt_buf, irn);
534 lc_esnprintf(arg_env, cmd_buf, SNPRINTF_BUF_LEN, "stmia %1S!, {%%r12, %3S, %4S, %5S}", irn, irn, irn, irn);
535 arm_fprintf_format(out, cmd_buf, cmnt_buf, irn);
540 static void emit_arm_SwitchJmp(ir_node *irn, void *env) {
541 char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
542 const ir_edge_t *edge;
544 arm_emit_env_t *emit_env = env;
545 FILE *out = emit_env->out;
550 int default_block_num = -1;
552 block_nr = get_irn_node_nr(irn);
553 n_projs = get_arm_n_projs(irn);
555 projs = xcalloc(n_projs , sizeof(ir_node*));
557 foreach_out_edge(irn, edge) {
558 proj = get_edge_src_irn(edge);
559 assert(is_Proj(proj) && "Only proj allowed at SwitchJmp");
561 if (get_Proj_proj(proj) == get_arm_default_proj_num(irn))
562 default_block_num = get_irn_node_nr(get_irn_link(proj));
564 projs[get_Proj_proj(proj)] = proj;
566 assert(default_block_num >= 0);
568 // CMP %1S, n_projs - 1
573 lc_esnprintf(arm_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "cmp %1S, #%u", irn, n_projs - 1);
574 lc_esnprintf(arm_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "", irn);
575 arm_fprintf_format(out, cmd_buf, cmnt_buf, irn);
577 lc_esnprintf(arm_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "bhi BLOCK_%d", default_block_num);
578 lc_esnprintf(arm_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "", irn);
579 arm_fprintf_format(out, cmd_buf, cmnt_buf, irn);
582 lc_esnprintf(arm_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "ldr %%r12, TABLE_%d_START", block_nr);
583 lc_esnprintf(arm_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "", irn);
584 arm_fprintf_format(out, cmd_buf, cmnt_buf, irn);
586 lc_esnprintf(arm_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "add %%r12, %%r12, %1S, LSL #2", irn);
587 lc_esnprintf(arm_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "", irn);
588 arm_fprintf_format(out, cmd_buf, cmnt_buf, irn);
590 lc_esnprintf(arm_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "ldr %%r15, [%%r12, #0]");
591 lc_esnprintf(arm_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "", irn);
592 arm_fprintf_format(out, cmd_buf, cmnt_buf, irn);
594 // LDR %r12, .TABLE_X_START
595 // ADD %r12, %r12, [%1S, LSL #2]
598 fprintf(out, "TABLE_%d_START:\n\t.word\tTABLE_%d\n", block_nr, block_nr);
599 fprintf(out, "\t.align 2\n");
600 fprintf(out, "TABLE_%d:\n", block_nr);
603 for ( i=0; i<n_projs; i++) {
607 block = get_irn_link(proj);
609 block = get_irn_link(projs[get_arm_default_proj_num(irn)]);
611 fprintf(out, "\t.word\tBLOCK_%ld\n",get_irn_node_nr(block));
613 fprintf(out, "\t.align 2\n");
618 /************************************************************************/
620 /************************************************************************/
622 static void emit_be_Call(ir_node *irn, void *env) {
623 arm_emit_env_t *emit_env = env;
624 FILE *F = emit_env->out;
625 ir_entity *ent = be_Call_get_entity(irn);
626 char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
629 lc_esnprintf(arm_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "bl %s", get_entity_ld_name(ent));
631 lc_esnprintf(arm_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "%1D", get_irn_n(irn, be_pos_Call_ptr));
632 lc_esnprintf(arm_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* %+F (be_Call) */", irn);
633 arm_fprintf_format(F, cmd_buf, cmnt_buf, irn);
636 /** Emit an IncSP node */
637 static void emit_be_IncSP(const ir_node *irn, arm_emit_env_t *emit_env) {
638 FILE *F = emit_env->out;
639 int offs = be_get_IncSP_offset(irn);
642 char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
643 lc_esnprintf(arm_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "add %1D, %1S, #%O", irn, irn, irn );
644 lc_esnprintf(arm_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* IncSP(%O) */", irn);
645 arm_fprintf_format(F, cmd_buf, cmnt_buf, irn);
647 char cmnt_buf[SNPRINTF_BUF_LEN];
648 lc_esnprintf(arm_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* omitted IncSP(%O) */", irn);
649 arm_fprintf_format(F, "", cmnt_buf, irn);
653 // void emit_be_AddSP(const ir_node *irn, arm_emit_env_t *emit_env) {
654 // FILE *F = emit_env->out;
655 // char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
656 // lc_esnprintf(arm_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "ADD %1D, %1S, %2S", irn, irn, irn );
657 // lc_esnprintf(arm_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* AddSP(%2S) */", irn);
658 // lc_efprintf(arm_get_arg_env(), F, "\t%-35s %-60s /* %+F */\n", cmd_buf, cmnt_buf, irn);
661 static void emit_be_Copy(const ir_node *irn, arm_emit_env_t *emit_env) {
662 FILE *F = emit_env->out;
663 ir_mode *mode = get_irn_mode(irn);
664 const lc_arg_env_t *arm_env = arm_get_arg_env();
665 char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
667 if (get_in_reg(irn, 0) == get_out_reg(irn, 0)) {
668 lc_esnprintf(arm_env, cmnt_buf, SNPRINTF_BUF_LEN, "/* omitted Copy: %1S -> %1D */", irn, irn);
669 lc_efprintf(arm_env, F, "\t%-35s %-60s /* %+F */\n", "", cmnt_buf, irn);
673 if (mode_is_float(mode)) {
674 if (USE_FPA(emit_env->cg->isa)) {
675 lc_esnprintf(arm_env, cmd_buf, SNPRINTF_BUF_LEN, "mvf%M %1D, %1S", irn, irn, irn);
676 lc_esnprintf(arm_env, cmnt_buf, SNPRINTF_BUF_LEN, "/* Copy: %1S -> %1D */", irn, irn);
677 arm_fprintf_format(F, cmd_buf, cmnt_buf, irn);
680 assert(0 && "move not supported for this mode");
682 } else if (mode_is_numP(mode)) {
683 lc_esnprintf(arm_env, cmd_buf, SNPRINTF_BUF_LEN, "mov %1D, %1S", irn, irn);
684 lc_esnprintf(arm_env, cmnt_buf, SNPRINTF_BUF_LEN, "/* Copy: %1S -> %1D */", irn, irn);
685 arm_fprintf_format(F, cmd_buf, cmnt_buf, irn);
687 assert(0 && "move not supported for this mode");
689 // emit_arm_Copy(irn, emit_env);
693 * Emit code for a Spill.
695 static void emit_be_Spill(const ir_node *irn, arm_emit_env_t *emit_env) {
696 FILE *F = emit_env->out;
697 ir_mode *mode = get_irn_mode(irn);
698 const lc_arg_env_t *arm_env = arm_get_arg_env();
699 char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
701 if (mode_is_float(mode)) {
702 if (USE_FPA(emit_env->cg->isa)) {
703 lc_esnprintf(arm_env, cmd_buf, SNPRINTF_BUF_LEN, "stf %2S, [%1S, #%O]", irn, irn, irn );
704 lc_esnprintf(arm_env, cmnt_buf, SNPRINTF_BUF_LEN, "/* Spill(%2S) -> (%1S) */", irn, irn);
705 arm_fprintf_format(F, cmd_buf, cmnt_buf, irn);
708 assert(0 && "move not supported for this mode");
710 } else if (mode_is_dataM(mode)) {
711 lc_esnprintf(arm_env, cmd_buf, SNPRINTF_BUF_LEN, "str %2S, [%1S, #%O]", irn, irn, irn );
712 lc_esnprintf(arm_env, cmnt_buf, SNPRINTF_BUF_LEN, "/* Spill(%2S) -> (%1S) */", irn, irn);
713 arm_fprintf_format(F, cmd_buf, cmnt_buf, irn);
715 assert(0 && "spill not supported for this mode");
720 * Emit code for a Reload.
722 static void emit_be_Reload(const ir_node* irn, arm_emit_env_t *emit_env) {
723 FILE *F = emit_env->out;
724 ir_mode *mode = get_irn_mode(irn);
725 const lc_arg_env_t *arm_env = arm_get_arg_env();
726 char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
728 if (mode_is_float(mode)) {
729 if (USE_FPA(emit_env->cg->isa)) {
730 lc_esnprintf(arm_env, cmd_buf, SNPRINTF_BUF_LEN, "ldf %1D, [%1S, #%O]", irn, irn, irn );
731 lc_esnprintf(arm_env, cmnt_buf, SNPRINTF_BUF_LEN, "/* Reload(%1S) -> (%1D) */", irn, irn);
732 arm_fprintf_format(F, cmd_buf, cmnt_buf, irn);
735 assert(0 && "move not supported for this mode");
737 } else if (mode_is_dataM(mode)) {
738 lc_esnprintf(arm_env, cmd_buf, SNPRINTF_BUF_LEN, "ldr %1D, [%1S, #%O]", irn, irn, irn );
739 lc_esnprintf(arm_env, cmnt_buf, SNPRINTF_BUF_LEN, "/* Reload(%1S) -> (%1D) */", irn, irn);
740 arm_fprintf_format(F, cmd_buf, cmnt_buf, irn);
742 assert(0 && "reload not supported for this mode");
746 static void emit_be_Perm(const ir_node* irn, arm_emit_env_t *emit_env) {
747 FILE *F = emit_env->out;
748 const lc_arg_env_t *arm_env = arm_get_arg_env();
749 char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
751 lc_esnprintf(arm_env, cmd_buf, SNPRINTF_BUF_LEN, "eor %1S, %1S, %2S", irn, irn, irn);
752 lc_esnprintf(arm_env, cmnt_buf, SNPRINTF_BUF_LEN, "/* begin Perm(%1S, %2S) */", irn, irn);
753 arm_fprintf_format(F, cmd_buf, cmnt_buf, irn);
755 lc_esnprintf(arm_env, cmd_buf, SNPRINTF_BUF_LEN, "eor %2S, %1S, %2S", irn, irn, irn);
756 lc_esnprintf(arm_env, cmnt_buf, SNPRINTF_BUF_LEN, " ");
757 arm_fprintf_format(F, cmd_buf, cmnt_buf, irn);
759 lc_esnprintf(arm_env, cmd_buf, SNPRINTF_BUF_LEN, "eor %1S, %1S, %2S", irn, irn, irn);
760 lc_esnprintf(arm_env, cmnt_buf, SNPRINTF_BUF_LEN, "/* end Perm(%1S, %2S) */", irn, irn);
761 arm_fprintf_format(F, cmd_buf, cmnt_buf, irn);
764 static void emit_be_StackParam(const ir_node *irn, arm_emit_env_t *emit_env) {
765 FILE *F = emit_env->out;
766 ir_mode *mode = get_irn_mode(irn);
767 const lc_arg_env_t *arm_env = arm_get_arg_env();
768 char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
770 if (mode_is_float(mode)) {
771 if (USE_FPA(emit_env->cg->isa)) {
772 lc_esnprintf(arm_env, cmd_buf, SNPRINTF_BUF_LEN, "ldf %1D, [%1S, #%O]", irn, irn, irn );
773 lc_esnprintf(arm_env, cmnt_buf, SNPRINTF_BUF_LEN, "/* StackParam: (%1S + %O) -> %1D */",irn , irn, irn, get_irn_n(irn, 0));
774 arm_fprintf_format(F, cmd_buf, cmnt_buf, irn);
777 assert(0 && "move not supported for this mode");
780 lc_esnprintf(arm_env, cmd_buf, SNPRINTF_BUF_LEN, "ldr %1D, [%1S, #%O]", irn, irn, irn );
781 lc_esnprintf(arm_env, cmnt_buf, SNPRINTF_BUF_LEN, "/* StackParam: (%1S + %O) -> %1D */",irn , irn, irn, get_irn_n(irn, 0));
782 arm_fprintf_format(F, cmd_buf, cmnt_buf, irn);
786 /************************************************************************/
788 /************************************************************************/
790 static void emit_Jmp(ir_node *irn, void *env) {
791 char cmd_buf[SNPRINTF_BUF_LEN];
792 arm_emit_env_t *emit_env = env;
793 FILE *F = emit_env->out;
794 const ir_edge_t *edge = get_irn_out_edge_first(irn);
795 const lc_arg_env_t *arm_env = arm_get_arg_env();
796 ir_node *target_block = get_edge_src_irn(edge);
798 lc_esnprintf(arm_env, cmd_buf, SNPRINTF_BUF_LEN, "b BLOCK_%d", get_irn_node_nr(target_block));
799 arm_fprintf_format(F, cmd_buf, "/* unconditional Jump */", irn);
802 static void emit_arm_fpaDbl2GP(const ir_node *n, arm_emit_env_t *env) {
805 const lc_arg_env_t *arg_env = arm_get_arg_env();
807 lc_esnprintf(arg_env, cmd_buf, 256, "stfd %1S, [sp, #-8]! ", n);
808 arm_fprintf_format(F, cmd_buf, "/* Push fp to stack */", n);
810 lc_esnprintf(arg_env, cmd_buf, 256, "ldmfd sp!, {%2D, %1D} ", n, n);
811 arm_fprintf_format(F, cmd_buf, "/* Pop destination */", n);
814 static void emit_silence(ir_node *irn, void *env) {
819 /***********************************************************************************
822 * _ __ ___ __ _ _ _ __ | |_ _ __ __ _ _ __ ___ _____ _____ _ __| | __
823 * | '_ ` _ \ / _` | | '_ \ | _| '__/ _` | '_ ` _ \ / _ \ \ /\ / / _ \| '__| |/ /
824 * | | | | | | (_| | | | | | | | | | | (_| | | | | | | __/\ V V / (_) | | | <
825 * |_| |_| |_|\__,_|_|_| |_| |_| |_| \__,_|_| |_| |_|\___| \_/\_/ \___/|_| |_|\_\
827 ***********************************************************************************/
830 * Enters the emitter functions for handled nodes into the generic
831 * pointer of an opcode.
833 static void arm_register_emitters(void) {
835 #define ARM_EMIT(a) op_arm_##a->ops.generic = (op_func)emit_arm_##a
836 #define EMIT(a) op_##a->ops.generic = (op_func)emit_##a
837 #define BE_EMIT(a) op_be_##a->ops.generic = (op_func)emit_be_##a
838 #define SILENCE(a) op_##a->ops.generic = (op_func)emit_silence
840 /* first clear the generic function pointer for all ops */
841 clear_irp_opcodes_generic_func();
843 /* register all emitter functions defined in spec */
844 arm_register_spec_emitters();
846 /* other emitter functions */
849 // ARM_EMIT(CopyB_i);
873 SILENCE(be_CopyKeep);
882 typedef void (node_emitter)(const ir_node *, void *);
885 * Emits code for a node.
887 static void arm_emit_node(const ir_node *irn, void *env) {
888 arm_emit_env_t *emit_env = env;
889 FILE *F = emit_env->out;
890 ir_op *op = get_irn_op(irn);
891 DEBUG_ONLY(firm_dbg_module_t *mod = emit_env->mod;)
893 DBG((mod, LEVEL_1, "emitting code for %+F\n", irn));
895 if (op->ops.generic) {
896 node_emitter *emit = (node_emitter *)op->ops.generic;
900 ir_fprintf(F, "\t%-35s /* %+F %G */\n", "", irn, irn);
905 * Walks over the nodes in a block connected by scheduling edges
906 * and emits code for each node.
908 void arm_gen_block(ir_node *block, void *env) {
911 fprintf(((arm_emit_env_t *)env)->out, "BLOCK_%ld:\n", get_irn_node_nr(block));
912 sched_foreach(block, irn) {
913 arm_emit_node(irn, env);
919 * Emits code for function start.
921 void arm_emit_start(FILE *F, ir_graph *irg) {
922 ir_entity *ent = get_irg_entity(irg);
923 const char *irg_name = get_entity_ld_name(ent);
924 arm_switch_section(F, SECTION_TEXT);
925 fprintf(F, "\t.align 2\n");
927 if (get_entity_visibility(ent) == visibility_external_visible)
928 fprintf(F, "\t.global %s\n", irg_name);
929 fprintf(F, "%s:\n", irg_name);
933 * Emits code for function end
935 void arm_emit_end(FILE *F, ir_graph *irg) {
936 fprintf(F, "\t.ident \"firmcc\"\n");
940 * Sets labels for control flow nodes (jump target)
941 * TODO: Jump optimization
943 void arm_gen_labels(ir_node *block, void *env) {
945 int n = get_Block_n_cfgpreds(block);
947 for (n--; n >= 0; n--) {
948 pred = get_Block_cfgpred(block, n);
949 set_irn_link(pred, block);
955 * Main driver. Emits the code for one routine.
957 void arm_gen_routine(FILE *F, ir_graph *irg, const arm_code_gen_t *cg) {
958 SymConstEntry *entry;
959 arm_emit_env_t emit_env;
964 emit_env.arch_env = cg->arch_env;
966 emit_env.symbols = NULL;
967 obstack_init(&emit_env.obst);
968 FIRM_DBG_REGISTER(emit_env.mod, "firm.be.arm.emit");
970 /* set the global arch_env (needed by print hooks) */
971 arch_env = cg->arch_env;
973 arm_register_emitters();
975 /* create the block schedule. For now, we don't need it earlier. */
976 blk_sched = be_create_block_schedule(cg->irg, cg->birg->exec_freq);
978 arm_emit_start(F, irg);
979 irg_block_walk_graph(irg, arm_gen_labels, NULL, &emit_env);
981 n = ARR_LEN(blk_sched);
982 for (i = 0; i < n;) {
983 ir_node *block, *next_bl;
985 block = blk_sched[i];
987 next_bl = i < n ? blk_sched[i] : NULL;
989 /* set here the link. the emitter expects to find the next block here */
990 set_irn_link(block, next_bl);
991 arm_gen_block(block, &emit_env);
994 /* emit SymConst values */
995 if (emit_env.symbols)
996 fprintf(F, "\t.align 2\n");
998 for (entry = emit_env.symbols; entry; entry = entry->next) {
999 fprintf(F, ".L%u:\n", entry->label);
1000 lc_efprintf(arm_get_arg_env(), F, "\t.word\t%C\n", entry->symconst);
1003 obstack_free(&emit_env.obst, NULL);