1 /* The main arm backend driver file. */
9 #include <libcore/lc_opts.h>
10 #include <libcore/lc_opts_enum.h>
11 #endif /* WITH_LIBCORE */
13 #include "pseudo_irg.h"
19 #include "lower_intrinsics.h"
24 #include "../bearch.h" /* the general register allocator interface */
25 #include "../benode_t.h"
26 #include "../belower.h"
27 #include "../besched_t.h"
31 #include "bearch_arm_t.h"
33 #include "arm_new_nodes.h" /* arm nodes interface */
34 #include "gen_arm_regalloc_if.h" /* the generated interface (register type and class defenitions) */
35 #include "arm_gen_decls.h" /* interface declaration emitter */
36 #include "arm_transform.h"
37 #include "arm_emitter.h"
38 #include "arm_map_regs.h"
40 #define DEBUG_MODULE "firm.be.arm.isa"
42 /* TODO: ugly, but we need it to get access to the registers assigned to Phi nodes */
43 static set *cur_reg_set = NULL;
45 /**************************************************
48 * _ __ ___ __ _ __ _| | | ___ ___ _| |_
49 * | '__/ _ \/ _` | / _` | | |/ _ \ / __| | | _|
50 * | | | __/ (_| | | (_| | | | (_) | (__ | | |
51 * |_| \___|\__, | \__,_|_|_|\___/ \___| |_|_|
54 **************************************************/
56 static ir_node *my_skip_proj(const ir_node *n) {
63 * Return register requirements for a arm node.
64 * If the node returns a tuple (mode_T) then the proj's
65 * will be asked for this information.
67 static const arch_register_req_t *arm_get_irn_reg_req(const void *self, arch_register_req_t *req, const ir_node *irn, int pos) {
68 const arm_register_req_t *irn_req;
69 long node_pos = pos == -1 ? 0 : pos;
70 ir_mode *mode = get_irn_mode(irn);
71 FIRM_DBG_REGISTER(firm_dbg_module_t *mod, DEBUG_MODULE);
73 if (is_Block(irn) || mode == mode_X || mode == mode_M) {
74 DBG((mod, LEVEL_1, "ignoring mode_T, mode_M node %+F\n", irn));
78 if (mode == mode_T && pos < 0) {
79 DBG((mod, LEVEL_1, "ignoring request for OUT requirements at %+F\n", irn));
83 DBG((mod, LEVEL_1, "get requirements at pos %d for %+F ... ", pos, irn));
86 /* in case of a proj, we need to get the correct OUT slot */
87 /* of the node corresponding to the proj number */
89 node_pos = arm_translate_proj_pos(irn);
95 irn = my_skip_proj(irn);
97 DB((mod, LEVEL_1, "skipping Proj, going to %+F at pos %d ... ", irn, node_pos));
100 /* get requirements for our own nodes */
101 if (is_arm_irn(irn)) {
103 irn_req = get_arm_in_req(irn, pos);
106 irn_req = get_arm_out_req(irn, node_pos);
109 DB((mod, LEVEL_1, "returning reqs for %+F at pos %d\n", irn, pos));
111 memcpy(req, &(irn_req->req), sizeof(*req));
113 if (arch_register_req_is(&(irn_req->req), should_be_same)) {
114 assert(irn_req->same_pos >= 0 && "should be same constraint for in -> out NYI");
115 req->other_same = get_irn_n(irn, irn_req->same_pos);
118 if (arch_register_req_is(&(irn_req->req), should_be_different)) {
119 assert(irn_req->different_pos >= 0 && "should be different constraint for in -> out NYI");
120 req->other_different = get_irn_n(irn, irn_req->different_pos);
123 /* get requirements for FIRM nodes */
125 /* treat Phi like Const with default requirements */
127 DB((mod, LEVEL_1, "returning standard reqs for %+F\n", irn));
129 if (mode_is_float(mode)) {
130 memcpy(req, &(arm_default_req_arm_fpa.req), sizeof(*req));
132 else if (mode_is_int(mode) || mode_is_reference(mode)) {
133 memcpy(req, &(arm_default_req_arm_gp.req), sizeof(*req));
135 else if (mode == mode_T || mode == mode_M) {
136 DBG((mod, LEVEL_1, "ignoring Phi node %+F\n", irn));
140 assert(0 && "unsupported Phi-Mode");
144 DB((mod, LEVEL_1, "returning NULL for %+F (node not supported)\n", irn));
152 static void arm_set_irn_reg(const void *self, ir_node *irn, const arch_register_t *reg) {
157 if (get_irn_mode(irn) == mode_X) {
161 pos = arm_translate_proj_pos(irn);
162 irn = my_skip_proj(irn);
165 if (is_arm_irn(irn)) {
166 const arch_register_t **slots;
168 slots = get_arm_slots(irn);
172 /* here we set the registers for the Phi nodes */
173 arm_set_firm_reg(irn, reg, cur_reg_set);
177 static const arch_register_t *arm_get_irn_reg(const void *self, const ir_node *irn) {
179 const arch_register_t *reg = NULL;
183 if (get_irn_mode(irn) == mode_X) {
187 pos = arm_translate_proj_pos(irn);
188 irn = my_skip_proj(irn);
191 if (is_arm_irn(irn)) {
192 const arch_register_t **slots;
193 slots = get_arm_slots(irn);
197 reg = arm_get_firm_reg(irn, cur_reg_set);
203 static arch_irn_class_t arm_classify(const void *self, const ir_node *irn) {
204 irn = my_skip_proj(irn);
207 return arch_irn_class_branch;
209 else if (is_arm_irn(irn)) {
210 return arch_irn_class_normal;
216 static arch_irn_flags_t arm_get_flags(const void *self, const ir_node *irn) {
217 irn = my_skip_proj(irn);
219 if (is_arm_irn(irn)) {
220 return get_arm_flags(irn);
222 else if (is_Unknown(irn)) {
223 return arch_irn_flags_ignore;
229 static entity *arm_get_frame_entity(const void *self, const ir_node *irn) {
230 /* TODO: return the entity assigned to the frame */
235 * This function is called by the generic backend to correct offsets for
236 * nodes accessing the stack.
238 static void arm_set_stack_bias(const void *self, ir_node *irn, int bias) {
239 /* TODO: correct offset if irn accesses the stack */
242 /* fill register allocator interface */
244 static const arch_irn_ops_if_t arm_irn_ops_if = {
250 arm_get_frame_entity,
254 arm_irn_ops_t arm_irn_ops = {
261 /**************************************************
264 * ___ ___ __| | ___ __ _ ___ _ __ _| |_
265 * / __/ _ \ / _` |/ _ \/ _` |/ _ \ '_ \ | | _|
266 * | (_| (_) | (_| | __/ (_| | __/ | | | | | |
267 * \___\___/ \__,_|\___|\__, |\___|_| |_| |_|_|
270 **************************************************/
273 * Transforms the standard Firm graph into
276 static void arm_prepare_graph(void *self) {
277 arm_code_gen_t *cg = self;
279 arm_register_transformers();
280 irg_walk_blkwise_graph(cg->irg, arm_move_consts, arm_transform_node, cg);
286 * Called immediately before emit phase.
288 static void arm_finish_irg(ir_graph *irg, arm_code_gen_t *cg) {
289 /* TODO: - fix offsets for nodes accessing stack
296 * These are some hooks which must be filled but are probably not needed.
298 static void arm_before_sched(void *self) {
299 /* Some stuff you need to do after scheduling but before register allocation */
302 static void arm_before_ra(void *self) {
303 /* Some stuff you need to do immediately after register allocation */
308 * Emits the code, closes the output file and frees
309 * the code generator interface.
311 static void arm_emit_and_done(void *self) {
312 arm_code_gen_t *cg = self;
313 ir_graph *irg = cg->irg;
314 FILE *out = cg->isa->out;
316 if (cg->emit_decls) {
321 arm_finish_irg(irg, cg);
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(const be_irg_t *birg);
527 static const arch_code_generator_if_t arm_code_gen_if = {
529 arm_before_abi, /* before abi introduce */
531 arm_before_sched, /* before scheduling hook */
532 arm_before_ra, /* before register allocation hook */
533 NULL, /* after register allocation */
538 * Initializes the code generator.
540 static void *arm_cg_init(const be_irg_t *birg) {
541 static ir_type *int_tp = NULL;
542 arm_isa_t *isa = (arm_isa_t *)birg->main_env->arch_env->isa;
546 /* create an integer type with machine size */
547 int_tp = new_type_primitive(new_id_from_chars("int", 3), mode_Is);
550 cg = xmalloc(sizeof(*cg));
551 cg->impl = &arm_code_gen_if;
553 cg->reg_set = new_set(arm_cmp_irn_reg_assoc, 1024);
554 cg->arch_env = birg->main_env->arch_env;
560 FIRM_DBG_REGISTER(cg->mod, "firm.be.arm.cg");
564 if (isa->num_codegens > 1)
569 cur_reg_set = cg->reg_set;
573 /* enter the current code generator */
576 return (arch_code_generator_t *)cg;
581 * Maps all intrinsic calls that the backend support
582 * and map all instructions the backend did not support
585 static void arm_global_init(void) {
586 ir_type *tp, *int_tp, *uint_tp;
590 #define ID(x) new_id_from_chars(x, sizeof(x)-1)
592 int_tp = new_type_primitive(ID("int"), mode_Is);
593 uint_tp = new_type_primitive(ID("uint"), mode_Iu);
595 /* ARM has neither a signed div instruction ... */
598 i_instr_record *map_Div = &records[n_records++].i_instr;
600 tp = new_type_method(ID("rt_iDiv"), 2, 1);
601 set_method_param_type(tp, 0, int_tp);
602 set_method_param_type(tp, 1, int_tp);
603 set_method_res_type(tp, 0, int_tp);
605 rt_Div.ent = new_entity(get_glob_type(), ID("__divsi3"), tp);
606 rt_Div.mode = mode_T;
607 rt_Div.mem_proj_nr = pn_Div_M;
608 rt_Div.exc_proj_nr = pn_Div_X_except;
609 rt_Div.exc_mem_proj_nr = pn_Div_M;
610 rt_Div.res_proj_nr = pn_Div_res;
612 set_entity_visibility(rt_Div.ent, visibility_external_allocated);
614 map_Div->kind = INTRINSIC_INSTR;
615 map_Div->op = op_Div;
616 map_Div->i_mapper = (i_mapper_func)i_mapper_RuntimeCall;
617 map_Div->ctx = &rt_Div;
619 /* ... nor a signed div instruction ... */
622 i_instr_record *map_Div = &records[n_records++].i_instr;
624 tp = new_type_method(ID("rt_uDiv"), 2, 1);
625 set_method_param_type(tp, 0, uint_tp);
626 set_method_param_type(tp, 1, uint_tp);
627 set_method_res_type(tp, 0, uint_tp);
629 rt_Div.ent = new_entity(get_glob_type(), ID("__udivsi3"), tp);
630 rt_Div.mode = mode_T;
631 rt_Div.mem_proj_nr = pn_Div_M;
632 rt_Div.exc_proj_nr = pn_Div_X_except;
633 rt_Div.exc_mem_proj_nr = pn_Div_M;
634 rt_Div.res_proj_nr = pn_Div_res;
636 set_entity_visibility(rt_Div.ent, visibility_external_allocated);
638 map_Div->kind = INTRINSIC_INSTR;
639 map_Div->op = op_Div;
640 map_Div->i_mapper = (i_mapper_func)i_mapper_RuntimeCall;
641 map_Div->ctx = &rt_Div;
643 /* ... nor a signed mod instruction ... */
646 i_instr_record *map_Mod = &records[n_records++].i_instr;
648 tp = new_type_method(ID("rt_iMod"), 2, 1);
649 set_method_param_type(tp, 0, int_tp);
650 set_method_param_type(tp, 1, int_tp);
651 set_method_res_type(tp, 0, int_tp);
653 rt_Mod.ent = new_entity(get_glob_type(), ID("__modsi3"), tp);
654 rt_Mod.mode = mode_T;
655 rt_Mod.mem_proj_nr = pn_Mod_M;
656 rt_Mod.exc_proj_nr = pn_Mod_X_except;
657 rt_Mod.exc_mem_proj_nr = pn_Mod_M;
658 rt_Mod.res_proj_nr = pn_Mod_res;
660 set_entity_visibility(rt_Mod.ent, visibility_external_allocated);
662 map_Mod->kind = INTRINSIC_INSTR;
663 map_Mod->op = op_Mod;
664 map_Mod->i_mapper = (i_mapper_func)i_mapper_RuntimeCall;
665 map_Mod->ctx = &rt_Mod;
667 /* ... nor a unsigned mod. */
670 i_instr_record *map_Mod = &records[n_records++].i_instr;
672 tp = new_type_method(ID("rt_uMod"), 2, 1);
673 set_method_param_type(tp, 0, uint_tp);
674 set_method_param_type(tp, 1, uint_tp);
675 set_method_res_type(tp, 0, uint_tp);
677 rt_Mod.ent = new_entity(get_glob_type(), ID("__umodsi3"), tp);
678 rt_Mod.mode = mode_T;
679 rt_Mod.mem_proj_nr = pn_Mod_M;
680 rt_Mod.exc_proj_nr = pn_Mod_X_except;
681 rt_Mod.exc_mem_proj_nr = pn_Mod_M;
682 rt_Mod.res_proj_nr = pn_Mod_res;
684 set_entity_visibility(rt_Mod.ent, visibility_external_allocated);
686 map_Mod->kind = INTRINSIC_INSTR;
687 map_Mod->op = op_Mod;
688 map_Mod->i_mapper = (i_mapper_func)i_mapper_RuntimeCall;
689 map_Mod->ctx = &rt_Mod;
693 lower_intrinsics(records, n_records);
696 /*****************************************************************
697 * ____ _ _ _____ _____
698 * | _ \ | | | | |_ _|/ ____| /\
699 * | |_) | __ _ ___| | _____ _ __ __| | | | | (___ / \
700 * | _ < / _` |/ __| |/ / _ \ '_ \ / _` | | | \___ \ / /\ \
701 * | |_) | (_| | (__| < __/ | | | (_| | _| |_ ____) / ____ \
702 * |____/ \__,_|\___|_|\_\___|_| |_|\__,_| |_____|_____/_/ \_\
704 *****************************************************************/
706 static arm_isa_t arm_isa_template = {
707 &arm_isa_if, /* isa interface */
708 &arm_gp_regs[REG_SP], /* stack pointer */
709 &arm_gp_regs[REG_R11], /* base pointer */
710 -1, /* stack direction */
711 0, /* number of codegenerator objects */
712 0, /* use generic register names instead of SP, LR, PC */
713 NULL, /* current code generator */
714 NULL, /* output file */
715 ARM_FPU_ARCH_FPE, /* FPU architecture */
719 * Initializes the backend ISA and opens the output file.
721 static void *arm_init(FILE *file_handle) {
722 static int inited = 0;
728 isa = xmalloc(sizeof(*isa));
729 memcpy(isa, &arm_isa_template, sizeof(*isa));
731 arm_register_init(isa);
732 if (isa->gen_reg_names) {
733 /* patch register names */
734 arm_gp_regs[REG_R11].name = "r11";
735 arm_gp_regs[REG_SP].name = "r13";
736 arm_gp_regs[REG_LR].name = "r14";
737 arm_gp_regs[REG_PC].name = "r15";
741 isa->out = file_handle;
743 arm_create_opcodes();
745 arm_switch_section(NULL, NO_SECTION);
754 * frees the ISA structure.
756 static void arm_done(void *self) {
762 * Report the number of register classes.
763 * If we don't have fp instructions, report only GP
764 * here to speed up register allocation (and makes dumps
765 * smaller and more readable).
767 static int arm_get_n_reg_class(const void *self) {
768 const arm_isa_t *isa = self;
770 return isa->cg->have_fp ? 2 : 1;
774 * Return the register class with requested index.
776 static const arch_register_class_t *arm_get_reg_class(const void *self, int i) {
777 return i == 0 ? &arm_reg_classes[CLASS_arm_gp] : &arm_reg_classes[CLASS_arm_fpa];
781 * Get the register class which shall be used to store a value of a given mode.
782 * @param self The this pointer.
783 * @param mode The mode in question.
784 * @return A register class which can hold values of the given mode.
786 const arch_register_class_t *arm_get_reg_class_for_mode(const void *self, const ir_mode *mode) {
787 if (mode_is_float(mode))
788 return &arm_reg_classes[CLASS_arm_fpa];
790 return &arm_reg_classes[CLASS_arm_gp];
794 * Produces the type which sits between the stack args and the locals on the stack.
795 * it will contain the return address and space to store the old base pointer.
796 * @return The Firm type modelling the ABI between type.
798 static ir_type *arm_get_between_type(void *self) {
799 static ir_type *between_type = NULL;
800 static entity *old_bp_ent = NULL;
803 entity *ret_addr_ent;
804 ir_type *ret_addr_type = new_type_primitive(new_id_from_str("return_addr"), mode_P);
805 ir_type *old_bp_type = new_type_primitive(new_id_from_str("bp"), mode_P);
807 between_type = new_type_class(new_id_from_str("arm_between_type"));
808 old_bp_ent = new_entity(between_type, new_id_from_str("old_bp"), old_bp_type);
809 ret_addr_ent = new_entity(between_type, new_id_from_str("old_bp"), ret_addr_type);
811 set_entity_offset_bytes(old_bp_ent, 0);
812 set_entity_offset_bytes(ret_addr_ent, get_type_size_bytes(old_bp_type));
813 set_type_size_bytes(between_type, get_type_size_bytes(old_bp_type) + get_type_size_bytes(ret_addr_type));
821 be_abi_call_flags_bits_t flags;
822 const arch_env_t *arch_env;
823 const arch_isa_t *isa;
827 static void *arm_abi_init(const be_abi_call_t *call, const arch_env_t *arch_env, ir_graph *irg)
829 arm_abi_env_t *env = xmalloc(sizeof(env[0]));
830 be_abi_call_flags_t fl = be_abi_call_get_flags(call);
831 env->flags = fl.bits;
833 env->arch_env = arch_env;
834 env->isa = arch_env->isa;
838 static void arm_abi_dont_save_regs(void *self, pset *s)
840 arm_abi_env_t *env = self;
841 if (env->flags.try_omit_fp)
842 pset_insert_ptr(s, env->isa->bp);
848 * Build the ARM prolog
850 static const arch_register_t *arm_abi_prologue(void *self, ir_node **mem, pmap *reg_map) {
851 ir_node *keep, *store;
852 arm_abi_env_t *env = self;
853 ir_graph *irg = env->irg;
854 ir_node *block = get_irg_start_block(irg);
855 // ir_node *regs[16];
857 arch_register_class_t *gp = &arm_reg_classes[CLASS_arm_gp];
858 static const arm_register_req_t *fp_req[] = {
859 &arm_default_req_arm_gp_r11
862 ir_node *fp = be_abi_reg_map_get(reg_map, env->isa->bp);
863 ir_node *ip = be_abi_reg_map_get(reg_map, &arm_gp_regs[REG_R12]);
864 ir_node *sp = be_abi_reg_map_get(reg_map, env->isa->sp);
865 ir_node *lr = be_abi_reg_map_get(reg_map, &arm_gp_regs[REG_LR]);
866 ir_node *pc = be_abi_reg_map_get(reg_map, &arm_gp_regs[REG_PC]);
867 // ir_node *r0 = be_abi_reg_map_get(reg_map, &arm_gp_regs[REG_R0]);
868 // ir_node *r1 = be_abi_reg_map_get(reg_map, &arm_gp_regs[REG_R1]);
869 // ir_node *r2 = be_abi_reg_map_get(reg_map, &arm_gp_regs[REG_R2]);
870 // ir_node *r3 = be_abi_reg_map_get(reg_map, &arm_gp_regs[REG_R3]);
872 if(env->flags.try_omit_fp)
875 ip = be_new_Copy(gp, irg, block, sp );
876 arch_set_irn_register(env->arch_env, ip, &arm_gp_regs[REG_R12]);
877 be_set_constr_single_reg(ip, BE_OUT_POS(0), &arm_gp_regs[REG_R12] );
879 // if (r0) regs[n_regs++] = r0;
880 // if (r1) regs[n_regs++] = r1;
881 // if (r2) regs[n_regs++] = r2;
882 // if (r3) regs[n_regs++] = r3;
883 // sp = new_r_arm_StoreStackMInc(irg, block, *mem, sp, n_regs, regs, get_irn_mode(sp));
884 // set_arm_req_out(sp, &arm_default_req_arm_gp_sp, 0);
885 // arch_set_irn_register(env->arch_env, sp, env->isa->sp);
886 store = new_rd_arm_StoreStackM4Inc(NULL, irg, block, sp, fp, ip, lr, pc, *mem);
887 set_arm_req_out(store, &arm_default_req_arm_gp_sp, 0);
888 // arch_set_irn_register(env->arch_env, store, env->isa->sp);
890 sp = new_r_Proj(irg, block, store, env->isa->sp->reg_class->mode, pn_arm_StoreStackM4Inc_ptr);
891 arch_set_irn_register(env->arch_env, sp, env->isa->sp);
892 *mem = new_r_Proj(irg, block, store, mode_M, pn_arm_StoreStackM4Inc_M);
894 keep = be_new_CopyKeep_single(gp, irg, block, ip, sp, get_irn_mode(ip));
895 be_node_set_reg_class(keep, 1, gp);
896 arch_set_irn_register(env->arch_env, keep, &arm_gp_regs[REG_R12]);
897 be_set_constr_single_reg(keep, BE_OUT_POS(0), &arm_gp_regs[REG_R12] );
899 fp = new_rd_arm_Sub_i(NULL, irg, block, keep, get_irn_mode(fp),
900 new_tarval_from_long(4, get_irn_mode(fp)));
901 set_arm_req_out_all(fp, fp_req);
902 //set_arm_req_out(fp, &arm_default_req_arm_gp_r11, 0);
903 arch_set_irn_register(env->arch_env, fp, env->isa->bp);
905 // be_abi_reg_map_set(reg_map, &arm_gp_regs[REG_R0], r0);
906 // be_abi_reg_map_set(reg_map, &arm_gp_regs[REG_R1], r1);
907 // be_abi_reg_map_set(reg_map, &arm_gp_regs[REG_R2], r2);
908 // be_abi_reg_map_set(reg_map, &arm_gp_regs[REG_R3], r3);
909 be_abi_reg_map_set(reg_map, env->isa->bp, fp);
910 be_abi_reg_map_set(reg_map, &arm_gp_regs[REG_R12], keep);
911 be_abi_reg_map_set(reg_map, env->isa->sp, sp);
912 be_abi_reg_map_set(reg_map, &arm_gp_regs[REG_LR], lr);
913 be_abi_reg_map_set(reg_map, &arm_gp_regs[REG_PC], pc);
918 static void arm_abi_epilogue(void *self, ir_node *bl, ir_node **mem, pmap *reg_map) {
919 arm_abi_env_t *env = self;
920 ir_node *curr_sp = be_abi_reg_map_get(reg_map, env->isa->sp);
921 ir_node *curr_bp = be_abi_reg_map_get(reg_map, env->isa->bp);
922 ir_node *curr_pc = be_abi_reg_map_get(reg_map, &arm_gp_regs[REG_PC]);
923 ir_node *curr_lr = be_abi_reg_map_get(reg_map, &arm_gp_regs[REG_LR]);
924 static const arm_register_req_t *sub12_req[] = {
925 &arm_default_req_arm_gp_sp
928 // TODO: Activate Omit fp in epilogue
929 if(env->flags.try_omit_fp) {
930 curr_sp = be_new_IncSP(env->isa->sp, env->irg, bl, curr_sp, *mem, BE_STACK_FRAME_SIZE, be_stack_dir_shrink);
932 curr_lr = be_new_CopyKeep_single(&arm_reg_classes[CLASS_arm_gp], env->irg, bl, curr_lr, curr_sp, get_irn_mode(curr_lr));
933 be_node_set_reg_class(curr_lr, 1, &arm_reg_classes[CLASS_arm_gp]);
934 arch_set_irn_register(env->arch_env, curr_lr, &arm_gp_regs[REG_LR]);
935 be_set_constr_single_reg(curr_lr, BE_OUT_POS(0), &arm_gp_regs[REG_LR] );
937 curr_pc = be_new_Copy(&arm_reg_classes[CLASS_arm_gp], env->irg, bl, curr_lr );
938 arch_set_irn_register(env->arch_env, curr_pc, &arm_gp_regs[REG_PC]);
939 be_set_constr_single_reg(curr_pc, BE_OUT_POS(0), &arm_gp_regs[REG_PC] );
943 tarval *tv = new_tarval_from_long(12,mode_Iu);
944 sub12_node = new_rd_arm_Sub_i(NULL, env->irg, bl, curr_bp, mode_Iu, tv);
945 set_arm_req_out_all(sub12_node, sub12_req);
946 arch_set_irn_register(env->arch_env, sub12_node, env->isa->sp);
947 load_node = new_rd_arm_LoadStackM3( NULL, env->irg, bl, sub12_node, *mem );
948 set_arm_req_out(load_node, &arm_default_req_arm_gp_r11, 0);
949 set_arm_req_out(load_node, &arm_default_req_arm_gp_sp, 1);
950 set_arm_req_out(load_node, &arm_default_req_arm_gp_pc, 2);
951 curr_bp = new_r_Proj(env->irg, bl, load_node, env->isa->bp->reg_class->mode, pn_arm_LoadStackM3_res0);
952 curr_sp = new_r_Proj(env->irg, bl, load_node, env->isa->sp->reg_class->mode, pn_arm_LoadStackM3_res1);
953 curr_pc = new_r_Proj(env->irg, bl, load_node, mode_Iu, pn_arm_LoadStackM3_res2);
954 *mem = new_r_Proj(env->irg, bl, load_node, mode_M, pn_arm_LoadStackM3_M);
955 arch_set_irn_register(env->arch_env, curr_bp, env->isa->bp);
956 arch_set_irn_register(env->arch_env, curr_sp, env->isa->sp);
957 arch_set_irn_register(env->arch_env, curr_pc, &arm_gp_regs[REG_PC]);
959 be_abi_reg_map_set(reg_map, env->isa->sp, curr_sp);
960 be_abi_reg_map_set(reg_map, env->isa->bp, curr_bp);
961 be_abi_reg_map_set(reg_map, &arm_gp_regs[REG_LR], curr_lr);
962 be_abi_reg_map_set(reg_map, &arm_gp_regs[REG_PC], curr_pc);
965 static const be_abi_callbacks_t arm_abi_callbacks = {
968 arm_get_between_type,
969 arm_abi_dont_save_regs,
976 * Get the ABI restrictions for procedure calls.
977 * @param self The this pointer.
978 * @param method_type The type of the method (procedure) in question.
979 * @param abi The abi object to be modified
981 void arm_get_call_abi(const void *self, ir_type *method_type, be_abi_call_t *abi) {
985 int n = get_method_n_params(method_type);
986 be_abi_call_flags_t flags = {
988 0, /* store from left to right */
989 0, /* store arguments sequential */
990 1, /* try to omit the frame pointer */
991 1, /* the function can use any register as frame pointer */
992 1 /* a call can take the callee's address as an immediate */
996 /* set stack parameter passing style */
997 be_abi_call_set_flags(abi, flags, &arm_abi_callbacks);
999 for (i = 0; i < n; i++) {
1000 /* reg = get reg for param i; */
1001 /* be_abi_call_param_reg(abi, i, reg); */
1004 be_abi_call_param_reg(abi, i, arm_get_RegParam_reg(i));
1006 be_abi_call_param_stack(abi, i, 4, 0, 0);
1009 /* default: return value is in R0 resp. F0 */
1010 assert(get_method_n_ress(method_type) < 2);
1011 if (get_method_n_ress(method_type) > 0) {
1012 tp = get_method_res_type(method_type, 0);
1013 mode = get_type_mode(tp);
1015 be_abi_call_res_reg(abi, 0,
1016 mode_is_float(mode) ? &arm_fpa_regs[REG_F0] : &arm_gp_regs[REG_R0]);
1020 static const void *arm_get_irn_ops(const arch_irn_handler_t *self, const ir_node *irn) {
1021 return &arm_irn_ops;
1024 const arch_irn_handler_t arm_irn_handler = {
1028 const arch_irn_handler_t *arm_get_irn_handler(const void *self) {
1029 return &arm_irn_handler;
1032 int arm_to_appear_in_schedule(void *block_env, const ir_node *irn) {
1033 return is_arm_irn(irn);
1037 * Initializes the code generator interface.
1039 static const arch_code_generator_if_t *arm_get_code_generator_if(void *self) {
1040 return &arm_code_gen_if;
1043 list_sched_selector_t arm_sched_selector;
1046 * Returns the reg_pressure scheduler with to_appear_in_schedule() over\loaded
1048 static const list_sched_selector_t *arm_get_list_sched_selector(const void *self) {
1049 memcpy(&arm_sched_selector, reg_pressure_selector, sizeof(list_sched_selector_t));
1050 arm_sched_selector.to_appear_in_schedule = arm_to_appear_in_schedule;
1051 return &arm_sched_selector;
1055 * Returns the necessary byte alignment for storing a register of given class.
1057 static int arm_get_reg_class_alignment(const void *self, const arch_register_class_t *cls) {
1058 ir_mode *mode = arch_register_class_mode(cls);
1059 return get_mode_size_bytes(mode);
1064 /* fpu set architectures. */
1065 static const lc_opt_enum_int_items_t arm_fpu_items[] = {
1066 { "softfloat", ARM_FPU_ARCH_SOFTFLOAT },
1067 { "fpe", ARM_FPU_ARCH_FPE },
1068 { "fpa", ARM_FPU_ARCH_FPA },
1069 { "vfp1xd", ARM_FPU_ARCH_VFP_V1xD },
1070 { "vfp1", ARM_FPU_ARCH_VFP_V1 },
1071 { "vfp2", ARM_FPU_ARCH_VFP_V2 },
1075 static lc_opt_enum_int_var_t arch_fpu_var = {
1076 &arm_isa_template.fpu_arch, arm_fpu_items
1079 static const lc_opt_table_entry_t arm_options[] = {
1080 LC_OPT_ENT_ENUM_INT("fpunit", "select the floating point unit", &arch_fpu_var),
1081 LC_OPT_ENT_BOOL("gen_reg_names", "use generic register names", &arm_isa_template.gen_reg_names),
1086 * Register command line options for the ARM backend.
1090 * arm-fpuunit=unit select the floating point unit
1091 * arm-gen_reg_names use generic register names instead of SP, LR, PC
1093 static void arm_register_options(lc_opt_entry_t *ent)
1095 lc_opt_entry_t *be_grp_arm = lc_opt_get_grp(ent, "arm");
1096 lc_opt_add_table(be_grp_arm, arm_options);
1098 #endif /* WITH_LIBCORE */
1100 const arch_isa_if_t arm_isa_if = {
1103 arm_get_n_reg_class,
1105 arm_get_reg_class_for_mode,
1107 arm_get_irn_handler,
1108 arm_get_code_generator_if,
1109 arm_get_list_sched_selector,
1110 arm_get_reg_class_alignment,
1112 arm_register_options