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"
54 #include "../begnuas.h"
56 #include "bearch_arm_t.h"
58 #include "arm_new_nodes.h" /* arm nodes interface */
59 #include "gen_arm_regalloc_if.h" /* the generated interface (register type and class defenitions) */
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;
316 dump_ir_block_graph_sched(irg, "-arm-finished");
317 arm_gen_routine(cg, irg);
321 /* de-allocate code generator */
322 del_set(cg->reg_set);
327 * Move a double floating point value into an integer register.
328 * Place the move operation into block bl.
330 * Handle some special cases here:
331 * 1.) A constant: simply split into two
332 * 2.) A load: simply split into two
334 static ir_node *convert_dbl_to_int(ir_node *bl, ir_node *arg, ir_node *mem,
335 ir_node **resH, ir_node **resL) {
337 tarval *tv = get_Const_tarval(arg);
340 /* get the upper 32 bits */
341 v = get_tarval_sub_bits(tv, 7);
342 v = (v << 8) | get_tarval_sub_bits(tv, 6);
343 v = (v << 8) | get_tarval_sub_bits(tv, 5);
344 v = (v << 8) | get_tarval_sub_bits(tv, 4);
345 *resH = new_Const_long(mode_Is, v);
347 /* get the lower 32 bits */
348 v = get_tarval_sub_bits(tv, 3);
349 v = (v << 8) | get_tarval_sub_bits(tv, 2);
350 v = (v << 8) | get_tarval_sub_bits(tv, 1);
351 v = (v << 8) | get_tarval_sub_bits(tv, 0);
352 *resL = new_Const_long(mode_Is, v);
354 else if (get_irn_op(skip_Proj(arg)) == op_Load) {
355 /* FIXME: handling of low/high depends on LE/BE here */
359 ir_graph *irg = current_ir_graph;
362 conv = new_rd_arm_fpaDbl2GP(NULL, irg, bl, arg, mem);
364 *resL = new_r_Proj(irg, bl, conv, mode_Is, pn_arm_fpaDbl2GP_low);
365 *resH = new_r_Proj(irg, bl, conv, mode_Is, pn_arm_fpaDbl2GP_high);
366 mem = new_r_Proj(irg, bl, conv, mode_M, pn_arm_fpaDbl2GP_M);
372 * Move a single floating point value into an integer register.
373 * Place the move operation into block bl.
375 * Handle some special cases here:
376 * 1.) A constant: simply move
377 * 2.) A load: simply load
379 static ir_node *convert_sng_to_int(ir_node *bl, ir_node *arg) {
381 tarval *tv = get_Const_tarval(arg);
384 /* get the lower 32 bits */
385 v = get_tarval_sub_bits(tv, 3);
386 v = (v << 8) | get_tarval_sub_bits(tv, 2);
387 v = (v << 8) | get_tarval_sub_bits(tv, 1);
388 v = (v << 8) | get_tarval_sub_bits(tv, 0);
389 return new_Const_long(mode_Is, v);
391 else if (get_irn_op(skip_Proj(arg)) == op_Load) {
394 load = skip_Proj(arg);
401 * Convert the arguments of a call to support the
402 * ARM calling convention of general purpose AND floating
405 static void handle_calls(ir_node *call, void *env)
407 arm_code_gen_t *cg = env;
408 int i, j, n, size, idx, flag, n_param, n_res;
409 ir_type *mtp, *new_mtd, *new_tp[5];
410 ir_node *new_in[5], **in;
416 /* check, if we need conversions */
417 n = get_Call_n_params(call);
418 mtp = get_Call_type(call);
419 assert(get_method_n_params(mtp) == n);
421 /* it's always enough to handle the first 4 parameters */
424 flag = size = idx = 0;
425 bl = get_nodes_block(call);
426 for (i = 0; i < n; ++i) {
427 ir_type *param_tp = get_method_param_type(mtp, i);
429 if (is_compound_type(param_tp)) {
430 /* an aggregate parameter: bad case */
434 /* a primitive parameter */
435 ir_mode *mode = get_type_mode(param_tp);
437 if (mode_is_float(mode)) {
438 if (get_mode_size_bits(mode) > 32) {
439 ir_node *mem = get_Call_mem(call);
441 /* Beware: ARM wants the high part first */
443 new_tp[idx] = cg->int_tp;
444 new_tp[idx+1] = cg->int_tp;
445 mem = convert_dbl_to_int(bl, get_Call_param(call, i), mem, &new_in[idx], &new_in[idx+1]);
447 set_Call_mem(call, mem);
451 new_tp[idx] = cg->int_tp;
452 new_in[idx] = convert_sng_to_int(bl, get_Call_param(call, i));
459 new_tp[idx] = param_tp;
460 new_in[idx] = get_Call_param(call, i);
469 /* if flag is NOT set, no need to translate the method type */
473 /* construct a new method type */
475 n_param = get_method_n_params(mtp) - n + idx;
476 n_res = get_method_n_ress(mtp);
477 new_mtd = new_d_type_method(get_type_ident(mtp), n_param, n_res, get_type_dbg_info(mtp));
479 for (i = 0; i < idx; ++i)
480 set_method_param_type(new_mtd, i, new_tp[i]);
481 for (i = n, j = idx; i < get_method_n_params(mtp); ++i)
482 set_method_param_type(new_mtd, j++, get_method_param_type(mtp, i));
483 for (i = 0; i < n_res; ++i)
484 set_method_res_type(new_mtd, i, get_method_res_type(mtp, i));
486 set_method_calling_convention(new_mtd, get_method_calling_convention(mtp));
487 set_method_first_variadic_param_index(new_mtd, get_method_first_variadic_param_index(mtp));
489 if (is_lowered_type(mtp)) {
490 mtp = get_associated_type(mtp);
492 set_lowered_type(mtp, new_mtd);
494 set_Call_type(call, new_mtd);
496 /* calculate new in array of the Call */
497 NEW_ARR_A(ir_node *, in, n_param + 2);
498 for (i = 0; i < idx; ++i)
499 in[2 + i] = new_in[i];
500 for (i = n, j = idx; i < get_method_n_params(mtp); ++i)
501 in[2 + j++] = get_Call_param(call, i);
503 in[0] = get_Call_mem(call);
504 in[1] = get_Call_ptr(call);
506 /* finally, change the call inputs */
507 set_irn_in(call, n_param + 2, in);
511 * Handle graph transformations before the abi converter does its work.
513 static void arm_before_abi(void *self) {
514 arm_code_gen_t *cg = self;
516 irg_walk_graph(cg->irg, NULL, handle_calls, cg);
519 static void *arm_cg_init(be_irg_t *birg);
521 static const arch_code_generator_if_t arm_code_gen_if = {
523 arm_before_abi, /* before abi introduce */
526 arm_before_sched, /* before scheduling hook */
527 arm_before_ra, /* before register allocation hook */
528 NULL, /* after register allocation */
534 * Initializes the code generator.
536 static void *arm_cg_init(be_irg_t *birg) {
537 static ir_type *int_tp = NULL;
538 arm_isa_t *isa = (arm_isa_t *)birg->main_env->arch_env->isa;
542 /* create an integer type with machine size */
543 int_tp = new_type_primitive(new_id_from_chars("int", 3), mode_Is);
546 cg = xmalloc(sizeof(*cg));
547 cg->impl = &arm_code_gen_if;
549 cg->reg_set = new_set(arm_cmp_irn_reg_assoc, 1024);
550 cg->arch_env = birg->main_env->arch_env;
556 FIRM_DBG_REGISTER(cg->mod, "firm.be.arm.cg");
558 cur_reg_set = cg->reg_set;
562 /* enter the current code generator */
565 return (arch_code_generator_t *)cg;
570 * Maps all intrinsic calls that the backend support
571 * and map all instructions the backend did not support
574 static void arm_handle_intrinsics(void) {
575 ir_type *tp, *int_tp, *uint_tp;
579 #define ID(x) new_id_from_chars(x, sizeof(x)-1)
581 int_tp = new_type_primitive(ID("int"), mode_Is);
582 uint_tp = new_type_primitive(ID("uint"), mode_Iu);
584 /* ARM has neither a signed div instruction ... */
587 i_instr_record *map_Div = &records[n_records++].i_instr;
589 tp = new_type_method(ID("rt_iDiv"), 2, 1);
590 set_method_param_type(tp, 0, int_tp);
591 set_method_param_type(tp, 1, int_tp);
592 set_method_res_type(tp, 0, int_tp);
594 rt_Div.ent = new_entity(get_glob_type(), ID("__divsi3"), tp);
595 rt_Div.mode = mode_T;
596 rt_Div.mem_proj_nr = pn_Div_M;
597 rt_Div.exc_proj_nr = pn_Div_X_except;
598 rt_Div.exc_mem_proj_nr = pn_Div_M;
599 rt_Div.res_proj_nr = pn_Div_res;
601 set_entity_visibility(rt_Div.ent, visibility_external_allocated);
603 map_Div->kind = INTRINSIC_INSTR;
604 map_Div->op = op_Div;
605 map_Div->i_mapper = (i_mapper_func)i_mapper_RuntimeCall;
606 map_Div->ctx = &rt_Div;
608 /* ... nor a signed div instruction ... */
611 i_instr_record *map_Div = &records[n_records++].i_instr;
613 tp = new_type_method(ID("rt_uDiv"), 2, 1);
614 set_method_param_type(tp, 0, uint_tp);
615 set_method_param_type(tp, 1, uint_tp);
616 set_method_res_type(tp, 0, uint_tp);
618 rt_Div.ent = new_entity(get_glob_type(), ID("__udivsi3"), tp);
619 rt_Div.mode = mode_T;
620 rt_Div.mem_proj_nr = pn_Div_M;
621 rt_Div.exc_proj_nr = pn_Div_X_except;
622 rt_Div.exc_mem_proj_nr = pn_Div_M;
623 rt_Div.res_proj_nr = pn_Div_res;
625 set_entity_visibility(rt_Div.ent, visibility_external_allocated);
627 map_Div->kind = INTRINSIC_INSTR;
628 map_Div->op = op_Div;
629 map_Div->i_mapper = (i_mapper_func)i_mapper_RuntimeCall;
630 map_Div->ctx = &rt_Div;
632 /* ... nor a signed mod instruction ... */
635 i_instr_record *map_Mod = &records[n_records++].i_instr;
637 tp = new_type_method(ID("rt_iMod"), 2, 1);
638 set_method_param_type(tp, 0, int_tp);
639 set_method_param_type(tp, 1, int_tp);
640 set_method_res_type(tp, 0, int_tp);
642 rt_Mod.ent = new_entity(get_glob_type(), ID("__modsi3"), tp);
643 rt_Mod.mode = mode_T;
644 rt_Mod.mem_proj_nr = pn_Mod_M;
645 rt_Mod.exc_proj_nr = pn_Mod_X_except;
646 rt_Mod.exc_mem_proj_nr = pn_Mod_M;
647 rt_Mod.res_proj_nr = pn_Mod_res;
649 set_entity_visibility(rt_Mod.ent, visibility_external_allocated);
651 map_Mod->kind = INTRINSIC_INSTR;
652 map_Mod->op = op_Mod;
653 map_Mod->i_mapper = (i_mapper_func)i_mapper_RuntimeCall;
654 map_Mod->ctx = &rt_Mod;
656 /* ... nor a unsigned mod. */
659 i_instr_record *map_Mod = &records[n_records++].i_instr;
661 tp = new_type_method(ID("rt_uMod"), 2, 1);
662 set_method_param_type(tp, 0, uint_tp);
663 set_method_param_type(tp, 1, uint_tp);
664 set_method_res_type(tp, 0, uint_tp);
666 rt_Mod.ent = new_entity(get_glob_type(), ID("__umodsi3"), tp);
667 rt_Mod.mode = mode_T;
668 rt_Mod.mem_proj_nr = pn_Mod_M;
669 rt_Mod.exc_proj_nr = pn_Mod_X_except;
670 rt_Mod.exc_mem_proj_nr = pn_Mod_M;
671 rt_Mod.res_proj_nr = pn_Mod_res;
673 set_entity_visibility(rt_Mod.ent, visibility_external_allocated);
675 map_Mod->kind = INTRINSIC_INSTR;
676 map_Mod->op = op_Mod;
677 map_Mod->i_mapper = (i_mapper_func)i_mapper_RuntimeCall;
678 map_Mod->ctx = &rt_Mod;
682 lower_intrinsics(records, n_records);
685 /*****************************************************************
686 * ____ _ _ _____ _____
687 * | _ \ | | | | |_ _|/ ____| /\
688 * | |_) | __ _ ___| | _____ _ __ __| | | | | (___ / \
689 * | _ < / _` |/ __| |/ / _ \ '_ \ / _` | | | \___ \ / /\ \
690 * | |_) | (_| | (__| < __/ | | | (_| | _| |_ ____) / ____ \
691 * |____/ \__,_|\___|_|\_\___|_| |_|\__,_| |_____|_____/_/ \_\
693 *****************************************************************/
695 static arm_isa_t arm_isa_template = {
697 &arm_isa_if, /* isa interface */
698 &arm_gp_regs[REG_SP], /* stack pointer */
699 &arm_gp_regs[REG_R11], /* base pointer */
700 -1, /* stack direction */
701 NULL, /* main environment */
703 0, /* use generic register names instead of SP, LR, PC */
704 ARM_FPU_ARCH_FPE, /* FPU architecture */
705 NULL, /* current code generator */
706 { NULL, }, /* emitter environment */
710 * Initializes the backend ISA and opens the output file.
712 static void *arm_init(FILE *file_handle) {
713 static int inited = 0;
719 isa = xmalloc(sizeof(*isa));
720 memcpy(isa, &arm_isa_template, sizeof(*isa));
722 arm_register_init(isa);
725 be_emit_init_env(&isa->emit, file_handle);
727 arm_create_opcodes();
728 arm_handle_intrinsics();
730 /* we mark referenced global entities, so we can only emit those which
731 * are actually referenced. (Note: you mustn't use the type visited flag
732 * elsewhere in the backend)
734 inc_master_type_visited();
743 * Closes the output file and frees the ISA structure.
745 static void arm_done(void *self) {
746 arm_isa_t *isa = self;
748 be_gas_emit_decls(&isa->emit, isa->arch_isa.main_env, 1);
750 be_emit_destroy_env(&isa->emit);
756 * Report the number of register classes.
757 * If we don't have fp instructions, report only GP
758 * here to speed up register allocation (and makes dumps
759 * smaller and more readable).
761 static int arm_get_n_reg_class(const void *self) {
762 const arm_isa_t *isa = self;
764 return isa->cg->have_fp ? 2 : 1;
768 * Return the register class with requested index.
770 static const arch_register_class_t *arm_get_reg_class(const void *self, int i) {
771 return i == 0 ? &arm_reg_classes[CLASS_arm_gp] : &arm_reg_classes[CLASS_arm_fpa];
775 * Get the register class which shall be used to store a value of a given mode.
776 * @param self The this pointer.
777 * @param mode The mode in question.
778 * @return A register class which can hold values of the given mode.
780 const arch_register_class_t *arm_get_reg_class_for_mode(const void *self, const ir_mode *mode) {
781 if (mode_is_float(mode))
782 return &arm_reg_classes[CLASS_arm_fpa];
784 return &arm_reg_classes[CLASS_arm_gp];
788 * Produces the type which sits between the stack args and the locals on the stack.
789 * it will contain the return address and space to store the old base pointer.
790 * @return The Firm type modelling the ABI between type.
792 static ir_type *arm_get_between_type(void *self) {
793 static ir_type *between_type = NULL;
794 static ir_entity *old_bp_ent = NULL;
797 ir_entity *ret_addr_ent;
798 ir_type *ret_addr_type = new_type_primitive(new_id_from_str("return_addr"), mode_P);
799 ir_type *old_bp_type = new_type_primitive(new_id_from_str("bp"), mode_P);
801 between_type = new_type_class(new_id_from_str("arm_between_type"));
802 old_bp_ent = new_entity(between_type, new_id_from_str("old_bp"), old_bp_type);
803 ret_addr_ent = new_entity(between_type, new_id_from_str("old_bp"), ret_addr_type);
805 set_entity_offset(old_bp_ent, 0);
806 set_entity_offset(ret_addr_ent, get_type_size_bytes(old_bp_type));
807 set_type_size_bytes(between_type, get_type_size_bytes(old_bp_type) + get_type_size_bytes(ret_addr_type));
815 be_abi_call_flags_bits_t flags;
816 const arch_env_t *arch_env;
817 const arch_isa_t *isa;
821 static void *arm_abi_init(const be_abi_call_t *call, const arch_env_t *arch_env, ir_graph *irg)
823 arm_abi_env_t *env = xmalloc(sizeof(env[0]));
824 be_abi_call_flags_t fl = be_abi_call_get_flags(call);
825 env->flags = fl.bits;
827 env->arch_env = arch_env;
828 env->isa = arch_env->isa;
832 static void arm_abi_dont_save_regs(void *self, pset *s)
834 arm_abi_env_t *env = self;
835 if (env->flags.try_omit_fp)
836 pset_insert_ptr(s, env->isa->bp);
842 * Build the ARM prolog
844 static const arch_register_t *arm_abi_prologue(void *self, ir_node **mem, pmap *reg_map) {
845 ir_node *keep, *store;
846 arm_abi_env_t *env = self;
847 ir_graph *irg = env->irg;
848 ir_node *block = get_irg_start_block(irg);
849 // ir_node *regs[16];
851 arch_register_class_t *gp = &arm_reg_classes[CLASS_arm_gp];
853 ir_node *fp = be_abi_reg_map_get(reg_map, env->isa->bp);
854 ir_node *ip = be_abi_reg_map_get(reg_map, &arm_gp_regs[REG_R12]);
855 ir_node *sp = be_abi_reg_map_get(reg_map, env->isa->sp);
856 ir_node *lr = be_abi_reg_map_get(reg_map, &arm_gp_regs[REG_LR]);
857 ir_node *pc = be_abi_reg_map_get(reg_map, &arm_gp_regs[REG_PC]);
858 // ir_node *r0 = be_abi_reg_map_get(reg_map, &arm_gp_regs[REG_R0]);
859 // ir_node *r1 = be_abi_reg_map_get(reg_map, &arm_gp_regs[REG_R1]);
860 // ir_node *r2 = be_abi_reg_map_get(reg_map, &arm_gp_regs[REG_R2]);
861 // ir_node *r3 = be_abi_reg_map_get(reg_map, &arm_gp_regs[REG_R3]);
863 if(env->flags.try_omit_fp)
866 ip = be_new_Copy(gp, irg, block, sp );
867 arch_set_irn_register(env->arch_env, ip, &arm_gp_regs[REG_R12]);
868 be_set_constr_single_reg(ip, BE_OUT_POS(0), &arm_gp_regs[REG_R12] );
870 // if (r0) regs[n_regs++] = r0;
871 // if (r1) regs[n_regs++] = r1;
872 // if (r2) regs[n_regs++] = r2;
873 // if (r3) regs[n_regs++] = r3;
874 // sp = new_r_arm_StoreStackMInc(irg, block, *mem, sp, n_regs, regs, get_irn_mode(sp));
875 // set_arm_req_out(sp, &arm_default_req_arm_gp_sp, 0);
876 // arch_set_irn_register(env->arch_env, sp, env->isa->sp);
877 store = new_rd_arm_StoreStackM4Inc(NULL, irg, block, sp, fp, ip, lr, pc, *mem);
879 // set_arm_req_out(store, &arm_default_req_arm_gp_sp, 0);
880 // arch_set_irn_register(env->arch_env, store, env->isa->sp);
882 sp = new_r_Proj(irg, block, store, env->isa->sp->reg_class->mode, pn_arm_StoreStackM4Inc_ptr);
883 arch_set_irn_register(env->arch_env, sp, env->isa->sp);
884 *mem = new_r_Proj(irg, block, store, mode_M, pn_arm_StoreStackM4Inc_M);
886 keep = be_new_CopyKeep_single(gp, irg, block, ip, sp, get_irn_mode(ip));
887 be_node_set_reg_class(keep, 1, gp);
888 arch_set_irn_register(env->arch_env, keep, &arm_gp_regs[REG_R12]);
889 be_set_constr_single_reg(keep, BE_OUT_POS(0), &arm_gp_regs[REG_R12] );
891 fp = new_rd_arm_Sub_i(NULL, irg, block, keep, get_irn_mode(fp),
892 new_tarval_from_long(4, get_irn_mode(fp)));
894 //set_arm_req_out_all(fp, fp_req);
895 //set_arm_req_out(fp, &arm_default_req_arm_gp_r11, 0);
896 arch_set_irn_register(env->arch_env, fp, env->isa->bp);
898 // be_abi_reg_map_set(reg_map, &arm_gp_regs[REG_R0], r0);
899 // be_abi_reg_map_set(reg_map, &arm_gp_regs[REG_R1], r1);
900 // be_abi_reg_map_set(reg_map, &arm_gp_regs[REG_R2], r2);
901 // be_abi_reg_map_set(reg_map, &arm_gp_regs[REG_R3], r3);
902 be_abi_reg_map_set(reg_map, env->isa->bp, fp);
903 be_abi_reg_map_set(reg_map, &arm_gp_regs[REG_R12], keep);
904 be_abi_reg_map_set(reg_map, env->isa->sp, sp);
905 be_abi_reg_map_set(reg_map, &arm_gp_regs[REG_LR], lr);
906 be_abi_reg_map_set(reg_map, &arm_gp_regs[REG_PC], pc);
911 static void arm_abi_epilogue(void *self, ir_node *bl, ir_node **mem, pmap *reg_map) {
912 arm_abi_env_t *env = self;
913 ir_node *curr_sp = be_abi_reg_map_get(reg_map, env->isa->sp);
914 ir_node *curr_bp = be_abi_reg_map_get(reg_map, env->isa->bp);
915 ir_node *curr_pc = be_abi_reg_map_get(reg_map, &arm_gp_regs[REG_PC]);
916 ir_node *curr_lr = be_abi_reg_map_get(reg_map, &arm_gp_regs[REG_LR]);
918 // TODO: Activate Omit fp in epilogue
919 if(env->flags.try_omit_fp) {
920 curr_sp = be_new_IncSP(env->isa->sp, env->irg, bl, curr_sp, BE_STACK_FRAME_SIZE_SHRINK);
921 add_irn_dep(curr_sp, *mem);
923 curr_lr = be_new_CopyKeep_single(&arm_reg_classes[CLASS_arm_gp], env->irg, bl, curr_lr, curr_sp, get_irn_mode(curr_lr));
924 be_node_set_reg_class(curr_lr, 1, &arm_reg_classes[CLASS_arm_gp]);
925 arch_set_irn_register(env->arch_env, curr_lr, &arm_gp_regs[REG_LR]);
926 be_set_constr_single_reg(curr_lr, BE_OUT_POS(0), &arm_gp_regs[REG_LR] );
928 curr_pc = be_new_Copy(&arm_reg_classes[CLASS_arm_gp], env->irg, bl, curr_lr );
929 arch_set_irn_register(env->arch_env, curr_pc, &arm_gp_regs[REG_PC]);
930 be_set_constr_single_reg(curr_pc, BE_OUT_POS(0), &arm_gp_regs[REG_PC] );
934 tarval *tv = new_tarval_from_long(12,mode_Iu);
935 sub12_node = new_rd_arm_Sub_i(NULL, env->irg, bl, curr_bp, mode_Iu, tv);
937 //set_arm_req_out_all(sub12_node, sub12_req);
938 arch_set_irn_register(env->arch_env, sub12_node, env->isa->sp);
939 load_node = new_rd_arm_LoadStackM3( NULL, env->irg, bl, sub12_node, *mem );
941 //set_arm_req_out(load_node, &arm_default_req_arm_gp_r11, 0);
942 //set_arm_req_out(load_node, &arm_default_req_arm_gp_sp, 1);
943 //set_arm_req_out(load_node, &arm_default_req_arm_gp_pc, 2);
944 curr_bp = new_r_Proj(env->irg, bl, load_node, env->isa->bp->reg_class->mode, pn_arm_LoadStackM3_res0);
945 curr_sp = new_r_Proj(env->irg, bl, load_node, env->isa->sp->reg_class->mode, pn_arm_LoadStackM3_res1);
946 curr_pc = new_r_Proj(env->irg, bl, load_node, mode_Iu, pn_arm_LoadStackM3_res2);
947 *mem = new_r_Proj(env->irg, bl, load_node, mode_M, pn_arm_LoadStackM3_M);
948 arch_set_irn_register(env->arch_env, curr_bp, env->isa->bp);
949 arch_set_irn_register(env->arch_env, curr_sp, env->isa->sp);
950 arch_set_irn_register(env->arch_env, curr_pc, &arm_gp_regs[REG_PC]);
952 be_abi_reg_map_set(reg_map, env->isa->sp, curr_sp);
953 be_abi_reg_map_set(reg_map, env->isa->bp, curr_bp);
954 be_abi_reg_map_set(reg_map, &arm_gp_regs[REG_LR], curr_lr);
955 be_abi_reg_map_set(reg_map, &arm_gp_regs[REG_PC], curr_pc);
958 static const be_abi_callbacks_t arm_abi_callbacks = {
961 arm_get_between_type,
962 arm_abi_dont_save_regs,
969 * Get the ABI restrictions for procedure calls.
970 * @param self The this pointer.
971 * @param method_type The type of the method (procedure) in question.
972 * @param abi The abi object to be modified
974 void arm_get_call_abi(const void *self, ir_type *method_type, be_abi_call_t *abi) {
978 int n = get_method_n_params(method_type);
979 be_abi_call_flags_t flags = {
981 0, /* store from left to right */
982 0, /* store arguments sequential */
983 1, /* try to omit the frame pointer */
984 1, /* the function can use any register as frame pointer */
985 1 /* a call can take the callee's address as an immediate */
989 /* set stack parameter passing style */
990 be_abi_call_set_flags(abi, flags, &arm_abi_callbacks);
992 for (i = 0; i < n; i++) {
993 /* reg = get reg for param i; */
994 /* be_abi_call_param_reg(abi, i, reg); */
997 be_abi_call_param_reg(abi, i, arm_get_RegParam_reg(i));
999 be_abi_call_param_stack(abi, i, 4, 0, 0);
1002 /* default: return value is in R0 resp. F0 */
1003 assert(get_method_n_ress(method_type) < 2);
1004 if (get_method_n_ress(method_type) > 0) {
1005 tp = get_method_res_type(method_type, 0);
1006 mode = get_type_mode(tp);
1008 be_abi_call_res_reg(abi, 0,
1009 mode_is_float(mode) ? &arm_fpa_regs[REG_F0] : &arm_gp_regs[REG_R0]);
1013 static const void *arm_get_irn_ops(const arch_irn_handler_t *self, const ir_node *irn) {
1014 return &arm_irn_ops;
1017 const arch_irn_handler_t arm_irn_handler = {
1021 const arch_irn_handler_t *arm_get_irn_handler(const void *self) {
1022 return &arm_irn_handler;
1025 int arm_to_appear_in_schedule(void *block_env, const ir_node *irn) {
1026 if(!is_arm_irn(irn))
1033 * Initializes the code generator interface.
1035 static const arch_code_generator_if_t *arm_get_code_generator_if(void *self) {
1036 return &arm_code_gen_if;
1039 list_sched_selector_t arm_sched_selector;
1042 * Returns the reg_pressure scheduler with to_appear_in_schedule() over\loaded
1044 static const list_sched_selector_t *arm_get_list_sched_selector(const void *self, list_sched_selector_t *selector) {
1045 memcpy(&arm_sched_selector, reg_pressure_selector, sizeof(list_sched_selector_t));
1046 arm_sched_selector.to_appear_in_schedule = arm_to_appear_in_schedule;
1047 return &arm_sched_selector;
1050 static const ilp_sched_selector_t *arm_get_ilp_sched_selector(const void *self) {
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);
1062 static const be_execution_unit_t ***arm_get_allowed_execution_units(const void *self, const ir_node *irn) {
1068 static const be_machine_t *arm_get_machine(const void *self) {
1075 * Return irp irgs in the desired order.
1077 static ir_graph **arm_get_irg_list(const void *self, ir_graph ***irg_list) {
1082 * Returns the libFirm configuration parameter for this backend.
1084 static const backend_params *arm_get_libfirm_params(void) {
1085 static arch_dep_params_t ad = {
1087 0, /* Muls are fast enough on ARM */
1088 31, /* shift would be ok */
1089 0, /* SMUL is needed, only in Arch M*/
1090 0, /* UMUL is needed, only in Arch M */
1091 32, /* SMUL & UMUL available for 32 bit */
1093 static backend_params p = {
1094 NULL, /* no additional opcodes */
1095 NULL, /* will be set later */
1096 1, /* need dword lowering */
1097 NULL, /* but yet no creator function */
1098 NULL, /* context for create_intrinsic_fkt */
1105 /* fpu set architectures. */
1106 static const lc_opt_enum_int_items_t arm_fpu_items[] = {
1107 { "softfloat", ARM_FPU_ARCH_SOFTFLOAT },
1108 { "fpe", ARM_FPU_ARCH_FPE },
1109 { "fpa", ARM_FPU_ARCH_FPA },
1110 { "vfp1xd", ARM_FPU_ARCH_VFP_V1xD },
1111 { "vfp1", ARM_FPU_ARCH_VFP_V1 },
1112 { "vfp2", ARM_FPU_ARCH_VFP_V2 },
1116 static lc_opt_enum_int_var_t arch_fpu_var = {
1117 &arm_isa_template.fpu_arch, arm_fpu_items
1120 static const lc_opt_table_entry_t arm_options[] = {
1121 LC_OPT_ENT_ENUM_INT("fpunit", "select the floating point unit", &arch_fpu_var),
1122 LC_OPT_ENT_BOOL("gen_reg_names", "use generic register names", &arm_isa_template.gen_reg_names),
1126 const arch_isa_if_t arm_isa_if = {
1129 arm_get_n_reg_class,
1131 arm_get_reg_class_for_mode,
1133 arm_get_irn_handler,
1134 arm_get_code_generator_if,
1135 arm_get_list_sched_selector,
1136 arm_get_ilp_sched_selector,
1137 arm_get_reg_class_alignment,
1138 arm_get_libfirm_params,
1139 arm_get_allowed_execution_units,
1144 void be_init_arch_arm(void)
1146 lc_opt_entry_t *be_grp = lc_opt_get_grp(firm_opt_get_root(), "be");
1147 lc_opt_entry_t *arm_grp = lc_opt_get_grp(be_grp, "arm");
1149 lc_opt_add_table(arm_grp, arm_options);
1151 be_register_isa_if("arm", &arm_isa_if);
1154 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_arch_arm);