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
22 * @brief The main arm backend driver file.
23 * @author Oliver Richter, Tobias Gneist
30 #include <libcore/lc_opts.h>
31 #include <libcore/lc_opts_enum.h>
33 #include "pseudo_irg.h"
39 #include "lower_intrinsics.h"
44 #include "../bearch_t.h" /* the general register allocator interface */
45 #include "../benode_t.h"
46 #include "../belower.h"
47 #include "../besched_t.h"
50 #include "../bemachine.h"
51 #include "../beilpsched.h"
52 #include "../bemodule.h"
53 #include "../beirg_t.h"
55 #include "bearch_arm_t.h"
57 #include "arm_new_nodes.h" /* arm nodes interface */
58 #include "gen_arm_regalloc_if.h" /* the generated interface (register type and class defenitions) */
59 #include "arm_gen_decls.h" /* interface declaration emitter */
60 #include "arm_transform.h"
61 #include "arm_emitter.h"
62 #include "arm_map_regs.h"
64 #define DEBUG_MODULE "firm.be.arm.isa"
66 /* TODO: ugly, but we need it to get access to the registers assigned to Phi nodes */
67 static set *cur_reg_set = NULL;
69 /**************************************************
72 * _ __ ___ __ _ __ _| | | ___ ___ _| |_
73 * | '__/ _ \/ _` | / _` | | |/ _ \ / __| | | _|
74 * | | | __/ (_| | | (_| | | | (_) | (__ | | |
75 * |_| \___|\__, | \__,_|_|_|\___/ \___| |_|_|
78 **************************************************/
81 * Return register requirements for a arm node.
82 * If the node returns a tuple (mode_T) then the proj's
83 * will be asked for this information.
86 arch_register_req_t *arm_get_irn_reg_req(const void *self, const ir_node *node,
88 long node_pos = pos == -1 ? 0 : pos;
89 ir_mode *mode = get_irn_mode(node);
90 FIRM_DBG_REGISTER(firm_dbg_module_t *mod, DEBUG_MODULE);
92 if (is_Block(node) || mode == mode_X || mode == mode_M) {
93 DBG((mod, LEVEL_1, "ignoring mode_T, mode_M node %+F\n", node));
94 return arch_no_register_req;
97 if (mode == mode_T && pos < 0) {
98 DBG((mod, LEVEL_1, "ignoring request for OUT requirements at %+F\n", node));
99 return arch_no_register_req;
102 DBG((mod, LEVEL_1, "get requirements at pos %d for %+F ... ", pos, node));
105 /* in case of a proj, we need to get the correct OUT slot */
106 /* of the node corresponding to the proj number */
108 node_pos = arm_translate_proj_pos(node);
114 node = skip_Proj_const(node);
116 DB((mod, LEVEL_1, "skipping Proj, going to %+F at pos %d ... ", node, node_pos));
119 /* get requirements for our own nodes */
120 if (is_arm_irn(node)) {
121 const arch_register_req_t *req;
123 req = get_arm_in_req(node, pos);
125 req = get_arm_out_req(node, node_pos);
128 DB((mod, LEVEL_1, "returning reqs for %+F at pos %d\n", node, pos));
132 /* unknown should be tranformed by now */
133 assert(!is_Unknown(node));
134 DB((mod, LEVEL_1, "returning NULL for %+F (node not supported)\n", node));
136 return arch_no_register_req;
139 static void arm_set_irn_reg(const void *self, ir_node *irn, const arch_register_t *reg) {
144 if (get_irn_mode(irn) == mode_X) {
148 pos = arm_translate_proj_pos(irn);
149 irn = skip_Proj(irn);
152 if (is_arm_irn(irn)) {
153 const arch_register_t **slots;
155 slots = get_arm_slots(irn);
159 /* here we set the registers for the Phi nodes */
160 arm_set_firm_reg(irn, reg, cur_reg_set);
164 static const arch_register_t *arm_get_irn_reg(const void *self, const ir_node *irn) {
166 const arch_register_t *reg = NULL;
170 if (get_irn_mode(irn) == mode_X) {
174 pos = arm_translate_proj_pos(irn);
175 irn = skip_Proj_const(irn);
178 if (is_arm_irn(irn)) {
179 const arch_register_t **slots;
180 slots = get_arm_slots(irn);
184 reg = arm_get_firm_reg(irn, cur_reg_set);
190 static arch_irn_class_t arm_classify(const void *self, const ir_node *irn) {
191 irn = skip_Proj_const(irn);
194 return arch_irn_class_branch;
196 else if (is_arm_irn(irn)) {
197 return arch_irn_class_normal;
203 static arch_irn_flags_t arm_get_flags(const void *self, const ir_node *irn) {
204 irn = skip_Proj_const(irn);
206 if (is_arm_irn(irn)) {
207 return get_arm_flags(irn);
209 else if (is_Unknown(irn)) {
210 return arch_irn_flags_ignore;
216 static ir_entity *arm_get_frame_entity(const void *self, const ir_node *irn) {
217 /* TODO: return the entity assigned to the frame */
221 static void arm_set_frame_entity(const void *self, ir_node *irn, ir_entity *ent) {
222 /* TODO: set the entity assigned to the frame */
226 * This function is called by the generic backend to correct offsets for
227 * nodes accessing the stack.
229 static void arm_set_stack_bias(const void *self, ir_node *irn, int bias) {
230 /* TODO: correct offset if irn accesses the stack */
233 static int arm_get_sp_bias(const void *self, const ir_node *irn) {
237 /* fill register allocator interface */
239 static const arch_irn_ops_if_t arm_irn_ops_if = {
245 arm_get_frame_entity,
246 arm_set_frame_entity,
249 NULL, /* get_inverse */
250 NULL, /* get_op_estimated_cost */
251 NULL, /* possible_memory_operand */
252 NULL, /* perform_memory_operand */
255 arm_irn_ops_t arm_irn_ops = {
262 /**************************************************
265 * ___ ___ __| | ___ __ _ ___ _ __ _| |_
266 * / __/ _ \ / _` |/ _ \/ _` |/ _ \ '_ \ | | _|
267 * | (_| (_) | (_| | __/ (_| | __/ | | | | | |
268 * \___\___/ \__,_|\___|\__, |\___|_| |_| |_|_|
271 **************************************************/
274 * Transforms the standard Firm graph into
277 static void arm_prepare_graph(void *self) {
278 arm_code_gen_t *cg = self;
280 arm_register_transformers();
281 irg_walk_blkwise_graph(cg->irg, arm_move_consts, arm_transform_node, cg);
287 * Called immediately before emit phase.
289 static void arm_finish_irg(void *self) {
290 /* TODO: - fix offsets for nodes accessing stack
297 * These are some hooks which must be filled but are probably not needed.
299 static void arm_before_sched(void *self) {
300 /* Some stuff you need to do after scheduling but before register allocation */
303 static void arm_before_ra(void *self) {
304 /* Some stuff you need to do immediately after register allocation */
309 * Emits the code, closes the output file and frees
310 * the code generator interface.
312 static void arm_emit_and_done(void *self) {
313 arm_code_gen_t *cg = self;
314 ir_graph *irg = cg->irg;
315 FILE *out = cg->isa->out;
317 if (cg->emit_decls) {
322 dump_ir_block_graph_sched(irg, "-arm-finished");
323 arm_gen_routine(out, irg, cg);
327 /* de-allocate code generator */
328 del_set(cg->reg_set);
333 * Move a double floating point value into an integer register.
334 * Place the move operation into block bl.
336 * Handle some special cases here:
337 * 1.) A constant: simply split into two
338 * 2.) A load: siply split into two
340 static ir_node *convert_dbl_to_int(ir_node *bl, ir_node *arg, ir_node *mem,
341 ir_node **resH, ir_node **resL) {
343 tarval *tv = get_Const_tarval(arg);
346 /* get the upper 32 bits */
347 v = get_tarval_sub_bits(tv, 7);
348 v = (v << 8) | get_tarval_sub_bits(tv, 6);
349 v = (v << 8) | get_tarval_sub_bits(tv, 5);
350 v = (v << 8) | get_tarval_sub_bits(tv, 4);
351 *resH = new_Const_long(mode_Is, v);
353 /* get the lower 32 bits */
354 v = get_tarval_sub_bits(tv, 3);
355 v = (v << 8) | get_tarval_sub_bits(tv, 2);
356 v = (v << 8) | get_tarval_sub_bits(tv, 1);
357 v = (v << 8) | get_tarval_sub_bits(tv, 0);
358 *resL = new_Const_long(mode_Is, v);
360 else if (get_irn_op(skip_Proj(arg)) == op_Load) {
361 /* FIXME: handling of low/high depends on LE/BE here */
365 ir_graph *irg = current_ir_graph;
368 conv = new_rd_arm_fpaDbl2GP(NULL, irg, bl, arg, mem);
370 *resL = new_r_Proj(irg, bl, conv, mode_Is, pn_arm_fpaDbl2GP_low);
371 *resH = new_r_Proj(irg, bl, conv, mode_Is, pn_arm_fpaDbl2GP_high);
372 mem = new_r_Proj(irg, bl, conv, mode_M, pn_arm_fpaDbl2GP_M);
378 * Move a single floating point value into an integer register.
379 * Place the move operation into block bl.
381 * Handle some special cases here:
382 * 1.) A constant: simply move
383 * 2.) A load: siply load
385 static ir_node *convert_sng_to_int(ir_node *bl, ir_node *arg) {
387 tarval *tv = get_Const_tarval(arg);
390 /* get the lower 32 bits */
391 v = get_tarval_sub_bits(tv, 3);
392 v = (v << 8) | get_tarval_sub_bits(tv, 2);
393 v = (v << 8) | get_tarval_sub_bits(tv, 1);
394 v = (v << 8) | get_tarval_sub_bits(tv, 0);
395 return new_Const_long(mode_Is, v);
397 else if (get_irn_op(skip_Proj(arg)) == op_Load) {
400 load = skip_Proj(arg);
407 * Convert the arguments of a call to support the
408 * ARM calling convention of general purpose AND floating
411 static void handle_calls(ir_node *call, void *env)
413 arm_code_gen_t *cg = env;
414 int i, j, n, size, idx, flag, n_param, n_res;
415 ir_type *mtp, *new_mtd, *new_tp[5];
416 ir_node *new_in[5], **in;
422 /* check, if we need conversions */
423 n = get_Call_n_params(call);
424 mtp = get_Call_type(call);
425 assert(get_method_n_params(mtp) == n);
427 /* it's always enough to handle the first 4 parameters */
430 flag = size = idx = 0;
431 bl = get_nodes_block(call);
432 for (i = 0; i < n; ++i) {
433 ir_type *param_tp = get_method_param_type(mtp, i);
435 if (is_compound_type(param_tp)) {
436 /* an aggregate parameter: bad case */
440 /* a primitive parameter */
441 ir_mode *mode = get_type_mode(param_tp);
443 if (mode_is_float(mode)) {
444 if (get_mode_size_bits(mode) > 32) {
445 ir_node *mem = get_Call_mem(call);
447 /* Beware: ARM wants the high part first */
449 new_tp[idx] = cg->int_tp;
450 new_tp[idx+1] = cg->int_tp;
451 mem = convert_dbl_to_int(bl, get_Call_param(call, i), mem, &new_in[idx], &new_in[idx+1]);
453 set_Call_mem(call, mem);
457 new_tp[idx] = cg->int_tp;
458 new_in[idx] = convert_sng_to_int(bl, get_Call_param(call, i));
465 new_tp[idx] = param_tp;
466 new_in[idx] = get_Call_param(call, i);
475 /* if flag is NOT set, no need to translate the method type */
479 /* construct a new method type */
481 n_param = get_method_n_params(mtp) - n + idx;
482 n_res = get_method_n_ress(mtp);
483 new_mtd = new_d_type_method(get_type_ident(mtp), n_param, n_res, get_type_dbg_info(mtp));
485 for (i = 0; i < idx; ++i)
486 set_method_param_type(new_mtd, i, new_tp[i]);
487 for (i = n, j = idx; i < get_method_n_params(mtp); ++i)
488 set_method_param_type(new_mtd, j++, get_method_param_type(mtp, i));
489 for (i = 0; i < n_res; ++i)
490 set_method_res_type(new_mtd, i, get_method_res_type(mtp, i));
492 set_method_calling_convention(new_mtd, get_method_calling_convention(mtp));
493 set_method_first_variadic_param_index(new_mtd, get_method_first_variadic_param_index(mtp));
495 if (is_lowered_type(mtp)) {
496 mtp = get_associated_type(mtp);
498 set_lowered_type(mtp, new_mtd);
500 set_Call_type(call, new_mtd);
502 /* calculate new in array of the Call */
503 NEW_ARR_A(ir_node *, in, n_param + 2);
504 for (i = 0; i < idx; ++i)
505 in[2 + i] = new_in[i];
506 for (i = n, j = idx; i < get_method_n_params(mtp); ++i)
507 in[2 + j++] = get_Call_param(call, i);
509 in[0] = get_Call_mem(call);
510 in[1] = get_Call_ptr(call);
512 /* finally, change the call inputs */
513 set_irn_in(call, n_param + 2, in);
517 * Handle graph transformations before the abi converter does its work.
519 static void arm_before_abi(void *self) {
520 arm_code_gen_t *cg = self;
522 irg_walk_graph(cg->irg, NULL, handle_calls, cg);
525 static void *arm_cg_init(be_irg_t *birg);
527 static const arch_code_generator_if_t arm_code_gen_if = {
529 arm_before_abi, /* before abi introduce */
532 arm_before_sched, /* before scheduling hook */
533 arm_before_ra, /* before register allocation hook */
534 NULL, /* after register allocation */
540 * Initializes the code generator.
542 static void *arm_cg_init(be_irg_t *birg) {
543 static ir_type *int_tp = NULL;
544 arm_isa_t *isa = (arm_isa_t *)birg->main_env->arch_env->isa;
548 /* create an integer type with machine size */
549 int_tp = new_type_primitive(new_id_from_chars("int", 3), mode_Is);
552 cg = xmalloc(sizeof(*cg));
553 cg->impl = &arm_code_gen_if;
555 cg->reg_set = new_set(arm_cmp_irn_reg_assoc, 1024);
556 cg->arch_env = birg->main_env->arch_env;
562 FIRM_DBG_REGISTER(cg->mod, "firm.be.arm.cg");
566 if (isa->num_codegens > 1)
571 cur_reg_set = cg->reg_set;
575 /* enter the current code generator */
578 return (arch_code_generator_t *)cg;
583 * Maps all intrinsic calls that the backend support
584 * and map all instructions the backend did not support
587 static void arm_handle_intrinsics(void) {
588 ir_type *tp, *int_tp, *uint_tp;
592 #define ID(x) new_id_from_chars(x, sizeof(x)-1)
594 int_tp = new_type_primitive(ID("int"), mode_Is);
595 uint_tp = new_type_primitive(ID("uint"), mode_Iu);
597 /* ARM has neither a signed div instruction ... */
600 i_instr_record *map_Div = &records[n_records++].i_instr;
602 tp = new_type_method(ID("rt_iDiv"), 2, 1);
603 set_method_param_type(tp, 0, int_tp);
604 set_method_param_type(tp, 1, int_tp);
605 set_method_res_type(tp, 0, int_tp);
607 rt_Div.ent = new_entity(get_glob_type(), ID("__divsi3"), tp);
608 rt_Div.mode = mode_T;
609 rt_Div.mem_proj_nr = pn_Div_M;
610 rt_Div.exc_proj_nr = pn_Div_X_except;
611 rt_Div.exc_mem_proj_nr = pn_Div_M;
612 rt_Div.res_proj_nr = pn_Div_res;
614 set_entity_visibility(rt_Div.ent, visibility_external_allocated);
616 map_Div->kind = INTRINSIC_INSTR;
617 map_Div->op = op_Div;
618 map_Div->i_mapper = (i_mapper_func)i_mapper_RuntimeCall;
619 map_Div->ctx = &rt_Div;
621 /* ... nor a signed div instruction ... */
624 i_instr_record *map_Div = &records[n_records++].i_instr;
626 tp = new_type_method(ID("rt_uDiv"), 2, 1);
627 set_method_param_type(tp, 0, uint_tp);
628 set_method_param_type(tp, 1, uint_tp);
629 set_method_res_type(tp, 0, uint_tp);
631 rt_Div.ent = new_entity(get_glob_type(), ID("__udivsi3"), tp);
632 rt_Div.mode = mode_T;
633 rt_Div.mem_proj_nr = pn_Div_M;
634 rt_Div.exc_proj_nr = pn_Div_X_except;
635 rt_Div.exc_mem_proj_nr = pn_Div_M;
636 rt_Div.res_proj_nr = pn_Div_res;
638 set_entity_visibility(rt_Div.ent, visibility_external_allocated);
640 map_Div->kind = INTRINSIC_INSTR;
641 map_Div->op = op_Div;
642 map_Div->i_mapper = (i_mapper_func)i_mapper_RuntimeCall;
643 map_Div->ctx = &rt_Div;
645 /* ... nor a signed mod instruction ... */
648 i_instr_record *map_Mod = &records[n_records++].i_instr;
650 tp = new_type_method(ID("rt_iMod"), 2, 1);
651 set_method_param_type(tp, 0, int_tp);
652 set_method_param_type(tp, 1, int_tp);
653 set_method_res_type(tp, 0, int_tp);
655 rt_Mod.ent = new_entity(get_glob_type(), ID("__modsi3"), tp);
656 rt_Mod.mode = mode_T;
657 rt_Mod.mem_proj_nr = pn_Mod_M;
658 rt_Mod.exc_proj_nr = pn_Mod_X_except;
659 rt_Mod.exc_mem_proj_nr = pn_Mod_M;
660 rt_Mod.res_proj_nr = pn_Mod_res;
662 set_entity_visibility(rt_Mod.ent, visibility_external_allocated);
664 map_Mod->kind = INTRINSIC_INSTR;
665 map_Mod->op = op_Mod;
666 map_Mod->i_mapper = (i_mapper_func)i_mapper_RuntimeCall;
667 map_Mod->ctx = &rt_Mod;
669 /* ... nor a unsigned mod. */
672 i_instr_record *map_Mod = &records[n_records++].i_instr;
674 tp = new_type_method(ID("rt_uMod"), 2, 1);
675 set_method_param_type(tp, 0, uint_tp);
676 set_method_param_type(tp, 1, uint_tp);
677 set_method_res_type(tp, 0, uint_tp);
679 rt_Mod.ent = new_entity(get_glob_type(), ID("__umodsi3"), tp);
680 rt_Mod.mode = mode_T;
681 rt_Mod.mem_proj_nr = pn_Mod_M;
682 rt_Mod.exc_proj_nr = pn_Mod_X_except;
683 rt_Mod.exc_mem_proj_nr = pn_Mod_M;
684 rt_Mod.res_proj_nr = pn_Mod_res;
686 set_entity_visibility(rt_Mod.ent, visibility_external_allocated);
688 map_Mod->kind = INTRINSIC_INSTR;
689 map_Mod->op = op_Mod;
690 map_Mod->i_mapper = (i_mapper_func)i_mapper_RuntimeCall;
691 map_Mod->ctx = &rt_Mod;
695 lower_intrinsics(records, n_records);
698 /*****************************************************************
699 * ____ _ _ _____ _____
700 * | _ \ | | | | |_ _|/ ____| /\
701 * | |_) | __ _ ___| | _____ _ __ __| | | | | (___ / \
702 * | _ < / _` |/ __| |/ / _ \ '_ \ / _` | | | \___ \ / /\ \
703 * | |_) | (_| | (__| < __/ | | | (_| | _| |_ ____) / ____ \
704 * |____/ \__,_|\___|_|\_\___|_| |_|\__,_| |_____|_____/_/ \_\
706 *****************************************************************/
708 static arm_isa_t arm_isa_template = {
709 &arm_isa_if, /* isa interface */
710 &arm_gp_regs[REG_SP], /* stack pointer */
711 &arm_gp_regs[REG_R11], /* base pointer */
712 -1, /* stack direction */
713 0, /* number of codegenerator objects */
714 0, /* use generic register names instead of SP, LR, PC */
715 NULL, /* current code generator */
716 NULL, /* output file */
717 ARM_FPU_ARCH_FPE, /* FPU architecture */
721 * Initializes the backend ISA and opens the output file.
723 static void *arm_init(FILE *file_handle) {
724 static int inited = 0;
730 isa = xmalloc(sizeof(*isa));
731 memcpy(isa, &arm_isa_template, sizeof(*isa));
733 arm_register_init(isa);
736 isa->out = file_handle;
738 arm_create_opcodes();
739 arm_handle_intrinsics();
740 arm_switch_section(NULL, NO_SECTION);
749 * frees the ISA structure.
751 static void arm_done(void *self) {
757 * Report the number of register classes.
758 * If we don't have fp instructions, report only GP
759 * here to speed up register allocation (and makes dumps
760 * smaller and more readable).
762 static int arm_get_n_reg_class(const void *self) {
763 const arm_isa_t *isa = self;
765 return isa->cg->have_fp ? 2 : 1;
769 * Return the register class with requested index.
771 static const arch_register_class_t *arm_get_reg_class(const void *self, int i) {
772 return i == 0 ? &arm_reg_classes[CLASS_arm_gp] : &arm_reg_classes[CLASS_arm_fpa];
776 * Get the register class which shall be used to store a value of a given mode.
777 * @param self The this pointer.
778 * @param mode The mode in question.
779 * @return A register class which can hold values of the given mode.
781 const arch_register_class_t *arm_get_reg_class_for_mode(const void *self, const ir_mode *mode) {
782 if (mode_is_float(mode))
783 return &arm_reg_classes[CLASS_arm_fpa];
785 return &arm_reg_classes[CLASS_arm_gp];
789 * Produces the type which sits between the stack args and the locals on the stack.
790 * it will contain the return address and space to store the old base pointer.
791 * @return The Firm type modelling the ABI between type.
793 static ir_type *arm_get_between_type(void *self) {
794 static ir_type *between_type = NULL;
795 static ir_entity *old_bp_ent = NULL;
798 ir_entity *ret_addr_ent;
799 ir_type *ret_addr_type = new_type_primitive(new_id_from_str("return_addr"), mode_P);
800 ir_type *old_bp_type = new_type_primitive(new_id_from_str("bp"), mode_P);
802 between_type = new_type_class(new_id_from_str("arm_between_type"));
803 old_bp_ent = new_entity(between_type, new_id_from_str("old_bp"), old_bp_type);
804 ret_addr_ent = new_entity(between_type, new_id_from_str("old_bp"), ret_addr_type);
806 set_entity_offset(old_bp_ent, 0);
807 set_entity_offset(ret_addr_ent, get_type_size_bytes(old_bp_type));
808 set_type_size_bytes(between_type, get_type_size_bytes(old_bp_type) + get_type_size_bytes(ret_addr_type));
816 be_abi_call_flags_bits_t flags;
817 const arch_env_t *arch_env;
818 const arch_isa_t *isa;
822 static void *arm_abi_init(const be_abi_call_t *call, const arch_env_t *arch_env, ir_graph *irg)
824 arm_abi_env_t *env = xmalloc(sizeof(env[0]));
825 be_abi_call_flags_t fl = be_abi_call_get_flags(call);
826 env->flags = fl.bits;
828 env->arch_env = arch_env;
829 env->isa = arch_env->isa;
833 static void arm_abi_dont_save_regs(void *self, pset *s)
835 arm_abi_env_t *env = self;
836 if (env->flags.try_omit_fp)
837 pset_insert_ptr(s, env->isa->bp);
843 * Build the ARM prolog
845 static const arch_register_t *arm_abi_prologue(void *self, ir_node **mem, pmap *reg_map) {
846 ir_node *keep, *store;
847 arm_abi_env_t *env = self;
848 ir_graph *irg = env->irg;
849 ir_node *block = get_irg_start_block(irg);
850 // ir_node *regs[16];
852 arch_register_class_t *gp = &arm_reg_classes[CLASS_arm_gp];
854 ir_node *fp = be_abi_reg_map_get(reg_map, env->isa->bp);
855 ir_node *ip = be_abi_reg_map_get(reg_map, &arm_gp_regs[REG_R12]);
856 ir_node *sp = be_abi_reg_map_get(reg_map, env->isa->sp);
857 ir_node *lr = be_abi_reg_map_get(reg_map, &arm_gp_regs[REG_LR]);
858 ir_node *pc = be_abi_reg_map_get(reg_map, &arm_gp_regs[REG_PC]);
859 // ir_node *r0 = be_abi_reg_map_get(reg_map, &arm_gp_regs[REG_R0]);
860 // ir_node *r1 = be_abi_reg_map_get(reg_map, &arm_gp_regs[REG_R1]);
861 // ir_node *r2 = be_abi_reg_map_get(reg_map, &arm_gp_regs[REG_R2]);
862 // ir_node *r3 = be_abi_reg_map_get(reg_map, &arm_gp_regs[REG_R3]);
864 if(env->flags.try_omit_fp)
867 ip = be_new_Copy(gp, irg, block, sp );
868 arch_set_irn_register(env->arch_env, ip, &arm_gp_regs[REG_R12]);
869 be_set_constr_single_reg(ip, BE_OUT_POS(0), &arm_gp_regs[REG_R12] );
871 // if (r0) regs[n_regs++] = r0;
872 // if (r1) regs[n_regs++] = r1;
873 // if (r2) regs[n_regs++] = r2;
874 // if (r3) regs[n_regs++] = r3;
875 // sp = new_r_arm_StoreStackMInc(irg, block, *mem, sp, n_regs, regs, get_irn_mode(sp));
876 // set_arm_req_out(sp, &arm_default_req_arm_gp_sp, 0);
877 // arch_set_irn_register(env->arch_env, sp, env->isa->sp);
878 store = new_rd_arm_StoreStackM4Inc(NULL, irg, block, sp, fp, ip, lr, pc, *mem);
880 // set_arm_req_out(store, &arm_default_req_arm_gp_sp, 0);
881 // arch_set_irn_register(env->arch_env, store, env->isa->sp);
883 sp = new_r_Proj(irg, block, store, env->isa->sp->reg_class->mode, pn_arm_StoreStackM4Inc_ptr);
884 arch_set_irn_register(env->arch_env, sp, env->isa->sp);
885 *mem = new_r_Proj(irg, block, store, mode_M, pn_arm_StoreStackM4Inc_M);
887 keep = be_new_CopyKeep_single(gp, irg, block, ip, sp, get_irn_mode(ip));
888 be_node_set_reg_class(keep, 1, gp);
889 arch_set_irn_register(env->arch_env, keep, &arm_gp_regs[REG_R12]);
890 be_set_constr_single_reg(keep, BE_OUT_POS(0), &arm_gp_regs[REG_R12] );
892 fp = new_rd_arm_Sub_i(NULL, irg, block, keep, get_irn_mode(fp),
893 new_tarval_from_long(4, get_irn_mode(fp)));
895 //set_arm_req_out_all(fp, fp_req);
896 //set_arm_req_out(fp, &arm_default_req_arm_gp_r11, 0);
897 arch_set_irn_register(env->arch_env, fp, env->isa->bp);
899 // be_abi_reg_map_set(reg_map, &arm_gp_regs[REG_R0], r0);
900 // be_abi_reg_map_set(reg_map, &arm_gp_regs[REG_R1], r1);
901 // be_abi_reg_map_set(reg_map, &arm_gp_regs[REG_R2], r2);
902 // be_abi_reg_map_set(reg_map, &arm_gp_regs[REG_R3], r3);
903 be_abi_reg_map_set(reg_map, env->isa->bp, fp);
904 be_abi_reg_map_set(reg_map, &arm_gp_regs[REG_R12], keep);
905 be_abi_reg_map_set(reg_map, env->isa->sp, sp);
906 be_abi_reg_map_set(reg_map, &arm_gp_regs[REG_LR], lr);
907 be_abi_reg_map_set(reg_map, &arm_gp_regs[REG_PC], pc);
912 static void arm_abi_epilogue(void *self, ir_node *bl, ir_node **mem, pmap *reg_map) {
913 arm_abi_env_t *env = self;
914 ir_node *curr_sp = be_abi_reg_map_get(reg_map, env->isa->sp);
915 ir_node *curr_bp = be_abi_reg_map_get(reg_map, env->isa->bp);
916 ir_node *curr_pc = be_abi_reg_map_get(reg_map, &arm_gp_regs[REG_PC]);
917 ir_node *curr_lr = be_abi_reg_map_get(reg_map, &arm_gp_regs[REG_LR]);
919 // TODO: Activate Omit fp in epilogue
920 if(env->flags.try_omit_fp) {
921 curr_sp = be_new_IncSP(env->isa->sp, env->irg, bl, curr_sp, BE_STACK_FRAME_SIZE_SHRINK);
922 add_irn_dep(curr_sp, *mem);
924 curr_lr = be_new_CopyKeep_single(&arm_reg_classes[CLASS_arm_gp], env->irg, bl, curr_lr, curr_sp, get_irn_mode(curr_lr));
925 be_node_set_reg_class(curr_lr, 1, &arm_reg_classes[CLASS_arm_gp]);
926 arch_set_irn_register(env->arch_env, curr_lr, &arm_gp_regs[REG_LR]);
927 be_set_constr_single_reg(curr_lr, BE_OUT_POS(0), &arm_gp_regs[REG_LR] );
929 curr_pc = be_new_Copy(&arm_reg_classes[CLASS_arm_gp], env->irg, bl, curr_lr );
930 arch_set_irn_register(env->arch_env, curr_pc, &arm_gp_regs[REG_PC]);
931 be_set_constr_single_reg(curr_pc, BE_OUT_POS(0), &arm_gp_regs[REG_PC] );
935 tarval *tv = new_tarval_from_long(12,mode_Iu);
936 sub12_node = new_rd_arm_Sub_i(NULL, env->irg, bl, curr_bp, mode_Iu, tv);
938 //set_arm_req_out_all(sub12_node, sub12_req);
939 arch_set_irn_register(env->arch_env, sub12_node, env->isa->sp);
940 load_node = new_rd_arm_LoadStackM3( NULL, env->irg, bl, sub12_node, *mem );
942 //set_arm_req_out(load_node, &arm_default_req_arm_gp_r11, 0);
943 //set_arm_req_out(load_node, &arm_default_req_arm_gp_sp, 1);
944 //set_arm_req_out(load_node, &arm_default_req_arm_gp_pc, 2);
945 curr_bp = new_r_Proj(env->irg, bl, load_node, env->isa->bp->reg_class->mode, pn_arm_LoadStackM3_res0);
946 curr_sp = new_r_Proj(env->irg, bl, load_node, env->isa->sp->reg_class->mode, pn_arm_LoadStackM3_res1);
947 curr_pc = new_r_Proj(env->irg, bl, load_node, mode_Iu, pn_arm_LoadStackM3_res2);
948 *mem = new_r_Proj(env->irg, bl, load_node, mode_M, pn_arm_LoadStackM3_M);
949 arch_set_irn_register(env->arch_env, curr_bp, env->isa->bp);
950 arch_set_irn_register(env->arch_env, curr_sp, env->isa->sp);
951 arch_set_irn_register(env->arch_env, curr_pc, &arm_gp_regs[REG_PC]);
953 be_abi_reg_map_set(reg_map, env->isa->sp, curr_sp);
954 be_abi_reg_map_set(reg_map, env->isa->bp, curr_bp);
955 be_abi_reg_map_set(reg_map, &arm_gp_regs[REG_LR], curr_lr);
956 be_abi_reg_map_set(reg_map, &arm_gp_regs[REG_PC], curr_pc);
959 static const be_abi_callbacks_t arm_abi_callbacks = {
962 arm_get_between_type,
963 arm_abi_dont_save_regs,
970 * Get the ABI restrictions for procedure calls.
971 * @param self The this pointer.
972 * @param method_type The type of the method (procedure) in question.
973 * @param abi The abi object to be modified
975 void arm_get_call_abi(const void *self, ir_type *method_type, be_abi_call_t *abi) {
979 int n = get_method_n_params(method_type);
980 be_abi_call_flags_t flags = {
982 0, /* store from left to right */
983 0, /* store arguments sequential */
984 1, /* try to omit the frame pointer */
985 1, /* the function can use any register as frame pointer */
986 1 /* a call can take the callee's address as an immediate */
990 /* set stack parameter passing style */
991 be_abi_call_set_flags(abi, flags, &arm_abi_callbacks);
993 for (i = 0; i < n; i++) {
994 /* reg = get reg for param i; */
995 /* be_abi_call_param_reg(abi, i, reg); */
998 be_abi_call_param_reg(abi, i, arm_get_RegParam_reg(i));
1000 be_abi_call_param_stack(abi, i, 4, 0, 0);
1003 /* default: return value is in R0 resp. F0 */
1004 assert(get_method_n_ress(method_type) < 2);
1005 if (get_method_n_ress(method_type) > 0) {
1006 tp = get_method_res_type(method_type, 0);
1007 mode = get_type_mode(tp);
1009 be_abi_call_res_reg(abi, 0,
1010 mode_is_float(mode) ? &arm_fpa_regs[REG_F0] : &arm_gp_regs[REG_R0]);
1014 static const void *arm_get_irn_ops(const arch_irn_handler_t *self, const ir_node *irn) {
1015 return &arm_irn_ops;
1018 const arch_irn_handler_t arm_irn_handler = {
1022 const arch_irn_handler_t *arm_get_irn_handler(const void *self) {
1023 return &arm_irn_handler;
1026 int arm_to_appear_in_schedule(void *block_env, const ir_node *irn) {
1027 return is_arm_irn(irn);
1031 * Initializes the code generator interface.
1033 static const arch_code_generator_if_t *arm_get_code_generator_if(void *self) {
1034 return &arm_code_gen_if;
1037 list_sched_selector_t arm_sched_selector;
1040 * Returns the reg_pressure scheduler with to_appear_in_schedule() over\loaded
1042 static const list_sched_selector_t *arm_get_list_sched_selector(const void *self, list_sched_selector_t *selector) {
1043 memcpy(&arm_sched_selector, reg_pressure_selector, sizeof(list_sched_selector_t));
1044 arm_sched_selector.to_appear_in_schedule = arm_to_appear_in_schedule;
1045 return &arm_sched_selector;
1048 static const ilp_sched_selector_t *arm_get_ilp_sched_selector(const void *self) {
1053 * Returns the necessary byte alignment for storing a register of given class.
1055 static int arm_get_reg_class_alignment(const void *self, const arch_register_class_t *cls) {
1056 ir_mode *mode = arch_register_class_mode(cls);
1057 return get_mode_size_bytes(mode);
1060 static const be_execution_unit_t ***arm_get_allowed_execution_units(const void *self, const ir_node *irn) {
1066 static const be_machine_t *arm_get_machine(const void *self) {
1073 * Return irp irgs in the desired order.
1075 static ir_graph **arm_get_irg_list(const void *self, ir_graph ***irg_list) {
1080 * Returns the libFirm configuration parameter for this backend.
1082 static const backend_params *arm_get_libfirm_params(void) {
1083 static arch_dep_params_t ad = {
1085 0, /* Muls are fast enough on ARM */
1086 31, /* shift would be ok */
1087 0, /* SMUL is needed, only in Arch M*/
1088 0, /* UMUL is needed, only in Arch M */
1089 32, /* SMUL & UMUL available for 32 bit */
1091 static backend_params p = {
1092 NULL, /* no additional opcodes */
1093 NULL, /* will be set later */
1094 1, /* need dword lowering */
1095 NULL, /* but yet no creator function */
1096 NULL, /* context for create_intrinsic_fkt */
1103 /* fpu set architectures. */
1104 static const lc_opt_enum_int_items_t arm_fpu_items[] = {
1105 { "softfloat", ARM_FPU_ARCH_SOFTFLOAT },
1106 { "fpe", ARM_FPU_ARCH_FPE },
1107 { "fpa", ARM_FPU_ARCH_FPA },
1108 { "vfp1xd", ARM_FPU_ARCH_VFP_V1xD },
1109 { "vfp1", ARM_FPU_ARCH_VFP_V1 },
1110 { "vfp2", ARM_FPU_ARCH_VFP_V2 },
1114 static lc_opt_enum_int_var_t arch_fpu_var = {
1115 &arm_isa_template.fpu_arch, arm_fpu_items
1118 static const lc_opt_table_entry_t arm_options[] = {
1119 LC_OPT_ENT_ENUM_INT("fpunit", "select the floating point unit", &arch_fpu_var),
1120 LC_OPT_ENT_BOOL("gen_reg_names", "use generic register names", &arm_isa_template.gen_reg_names),
1124 const arch_isa_if_t arm_isa_if = {
1127 arm_get_n_reg_class,
1129 arm_get_reg_class_for_mode,
1131 arm_get_irn_handler,
1132 arm_get_code_generator_if,
1133 arm_get_list_sched_selector,
1134 arm_get_ilp_sched_selector,
1135 arm_get_reg_class_alignment,
1136 arm_get_libfirm_params,
1137 arm_get_allowed_execution_units,
1142 void be_init_arch_arm(void)
1144 lc_opt_entry_t *be_grp = lc_opt_get_grp(firm_opt_get_root(), "be");
1145 lc_opt_entry_t *arm_grp = lc_opt_get_grp(be_grp, "arm");
1147 lc_opt_add_table(arm_grp, arm_options);
1149 be_register_isa_if("arm", &arm_isa_if);
1152 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_arch_arm);