added doxygen comments, credited backend to Oliver Richter and Tobias Gneist
[libfirm] / ir / be / arm / bearch_arm.c
1 /*
2  * Copyright (C) 1995-2007 University of Karlsruhe.  All right reserved.
3  *
4  * This file is part of libFirm.
5  *
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.
10  *
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.
14  *
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
17  * PURPOSE.
18  */
19
20 /**
21  * @file
22  * @brief   The main arm backend driver file.
23  * @author  Oliver Richter, Tobias Gneist
24  * @version $Id$
25  */
26 #ifdef HAVE_CONFIG_H
27 #include "config.h"
28 #endif
29
30 #include <libcore/lc_opts.h>
31 #include <libcore/lc_opts_enum.h>
32
33 #include "pseudo_irg.h"
34 #include "irgwalk.h"
35 #include "irprog.h"
36 #include "irprintf.h"
37 #include "ircons.h"
38 #include "irgmod.h"
39 #include "lower_intrinsics.h"
40
41 #include "bitset.h"
42 #include "debug.h"
43
44 #include "../bearch_t.h"                /* the general register allocator interface */
45 #include "../benode_t.h"
46 #include "../belower.h"
47 #include "../besched_t.h"
48 #include "../be.h"
49 #include "../beabi.h"
50 #include "../bemachine.h"
51 #include "../beilpsched.h"
52 #include "../bemodule.h"
53 #include "../beirg_t.h"
54
55 #include "bearch_arm_t.h"
56
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"
63
64 #define DEBUG_MODULE "firm.be.arm.isa"
65
66 /* TODO: ugly, but we need it to get access to the registers assigned to Phi nodes */
67 static set *cur_reg_set = NULL;
68
69 /**************************************************
70  *                         _ _              _  __
71  *                        | | |            (_)/ _|
72  *  _ __ ___  __ _    __ _| | | ___   ___   _| |_
73  * | '__/ _ \/ _` |  / _` | | |/ _ \ / __| | |  _|
74  * | | |  __/ (_| | | (_| | | | (_) | (__  | | |
75  * |_|  \___|\__, |  \__,_|_|_|\___/ \___| |_|_|
76  *            __/ |
77  *           |___/
78  **************************************************/
79
80 /**
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.
84  */
85 static const
86 arch_register_req_t *arm_get_irn_reg_req(const void *self, const ir_node *node,
87                                          int pos) {
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);
91
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;
95         }
96
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;
100         }
101
102         DBG((mod, LEVEL_1, "get requirements at pos %d for %+F ... ", pos, node));
103
104         if (is_Proj(node)) {
105                 /* in case of a proj, we need to get the correct OUT slot */
106                 /* of the node corresponding to the proj number */
107                 if (pos == -1) {
108                         node_pos = arm_translate_proj_pos(node);
109                 }
110                 else {
111                         node_pos = pos;
112                 }
113
114                 node = skip_Proj_const(node);
115
116                 DB((mod, LEVEL_1, "skipping Proj, going to %+F at pos %d ... ", node, node_pos));
117         }
118
119         /* get requirements for our own nodes */
120         if (is_arm_irn(node)) {
121                 const arch_register_req_t *req;
122                 if (pos >= 0) {
123                         req = get_arm_in_req(node, pos);
124                 } else {
125                         req = get_arm_out_req(node, node_pos);
126                 }
127
128                 DB((mod, LEVEL_1, "returning reqs for %+F at pos %d\n", node, pos));
129                 return req;
130         }
131
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));
135
136         return arch_no_register_req;
137 }
138
139 static void arm_set_irn_reg(const void *self, ir_node *irn, const arch_register_t *reg) {
140         int pos = 0;
141
142         if (is_Proj(irn)) {
143
144                 if (get_irn_mode(irn) == mode_X) {
145                         return;
146                 }
147
148                 pos = arm_translate_proj_pos(irn);
149                 irn = skip_Proj(irn);
150         }
151
152         if (is_arm_irn(irn)) {
153                 const arch_register_t **slots;
154
155                 slots      = get_arm_slots(irn);
156                 slots[pos] = reg;
157         }
158         else {
159                 /* here we set the registers for the Phi nodes */
160                 arm_set_firm_reg(irn, reg, cur_reg_set);
161         }
162 }
163
164 static const arch_register_t *arm_get_irn_reg(const void *self, const ir_node *irn) {
165         int pos = 0;
166         const arch_register_t *reg = NULL;
167
168         if (is_Proj(irn)) {
169
170                 if (get_irn_mode(irn) == mode_X) {
171                         return NULL;
172                 }
173
174                 pos = arm_translate_proj_pos(irn);
175                 irn = skip_Proj_const(irn);
176         }
177
178         if (is_arm_irn(irn)) {
179                 const arch_register_t **slots;
180                 slots = get_arm_slots(irn);
181                 reg   = slots[pos];
182         }
183         else {
184                 reg = arm_get_firm_reg(irn, cur_reg_set);
185         }
186
187         return reg;
188 }
189
190 static arch_irn_class_t arm_classify(const void *self, const ir_node *irn) {
191         irn = skip_Proj_const(irn);
192
193         if (is_cfop(irn)) {
194                 return arch_irn_class_branch;
195         }
196         else if (is_arm_irn(irn)) {
197                 return arch_irn_class_normal;
198         }
199
200         return 0;
201 }
202
203 static arch_irn_flags_t arm_get_flags(const void *self, const ir_node *irn) {
204         irn = skip_Proj_const(irn);
205
206         if (is_arm_irn(irn)) {
207                 return get_arm_flags(irn);
208         }
209         else if (is_Unknown(irn)) {
210                 return arch_irn_flags_ignore;
211         }
212
213         return 0;
214 }
215
216 static ir_entity *arm_get_frame_entity(const void *self, const ir_node *irn) {
217         /* TODO: return the entity assigned to the frame */
218         return NULL;
219 }
220
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 */
223 }
224
225 /**
226  * This function is called by the generic backend to correct offsets for
227  * nodes accessing the stack.
228  */
229 static void arm_set_stack_bias(const void *self, ir_node *irn, int bias) {
230         /* TODO: correct offset if irn accesses the stack */
231 }
232
233 static int arm_get_sp_bias(const void *self, const ir_node *irn) {
234         return 0;
235 }
236
237 /* fill register allocator interface */
238
239 static const arch_irn_ops_if_t arm_irn_ops_if = {
240         arm_get_irn_reg_req,
241         arm_set_irn_reg,
242         arm_get_irn_reg,
243         arm_classify,
244         arm_get_flags,
245         arm_get_frame_entity,
246         arm_set_frame_entity,
247         arm_set_stack_bias,
248         arm_get_sp_bias,
249         NULL,    /* get_inverse             */
250         NULL,    /* get_op_estimated_cost   */
251         NULL,    /* possible_memory_operand */
252         NULL,    /* perform_memory_operand  */
253 };
254
255 arm_irn_ops_t arm_irn_ops = {
256         &arm_irn_ops_if,
257         NULL
258 };
259
260
261
262 /**************************************************
263  *                _                         _  __
264  *               | |                       (_)/ _|
265  *   ___ ___   __| | ___  __ _  ___ _ __    _| |_
266  *  / __/ _ \ / _` |/ _ \/ _` |/ _ \ '_ \  | |  _|
267  * | (_| (_) | (_| |  __/ (_| |  __/ | | | | | |
268  *  \___\___/ \__,_|\___|\__, |\___|_| |_| |_|_|
269  *                        __/ |
270  *                       |___/
271  **************************************************/
272
273 /**
274  * Transforms the standard Firm graph into
275  * a ARM firm graph.
276  */
277 static void arm_prepare_graph(void *self) {
278         arm_code_gen_t *cg = self;
279
280         arm_register_transformers();
281         irg_walk_blkwise_graph(cg->irg, arm_move_consts, arm_transform_node, cg);
282 }
283
284
285
286 /**
287  * Called immediately before emit phase.
288  */
289 static void arm_finish_irg(void *self) {
290         /* TODO: - fix offsets for nodes accessing stack
291                          - ...
292         */
293 }
294
295
296 /**
297  * These are some hooks which must be filled but are probably not needed.
298  */
299 static void arm_before_sched(void *self) {
300         /* Some stuff you need to do after scheduling but before register allocation */
301 }
302
303 static void arm_before_ra(void *self) {
304         /* Some stuff you need to do immediately after register allocation */
305 }
306
307
308 /**
309  * Emits the code, closes the output file and frees
310  * the code generator interface.
311  */
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;
316
317         if (cg->emit_decls) {
318                 arm_gen_decls(out);
319                 cg->emit_decls = 0;
320         }
321
322         dump_ir_block_graph_sched(irg, "-arm-finished");
323         arm_gen_routine(out, irg, cg);
324
325         cur_reg_set = NULL;
326
327         /* de-allocate code generator */
328         del_set(cg->reg_set);
329         free(self);
330 }
331
332 /**
333  * Move a double floating point value into an integer register.
334  * Place the move operation into block bl.
335  *
336  * Handle some special cases here:
337  * 1.) A constant: simply split into two
338  * 2.) A load: siply split into two
339  */
340 static ir_node *convert_dbl_to_int(ir_node *bl, ir_node *arg, ir_node *mem,
341                                    ir_node **resH, ir_node **resL) {
342         if (is_Const(arg)) {
343                 tarval *tv = get_Const_tarval(arg);
344                 unsigned v;
345
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);
352
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);
359         }
360         else if (get_irn_op(skip_Proj(arg)) == op_Load) {
361                 /* FIXME: handling of low/high depends on LE/BE here */
362                 assert(0);
363         }
364         else {
365                 ir_graph *irg = current_ir_graph;
366                 ir_node *conv;
367
368                 conv = new_rd_arm_fpaDbl2GP(NULL, irg, bl, arg, mem);
369                 /* move high/low */
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);
373         }
374         return mem;
375 }
376
377 /**
378  * Move a single floating point value into an integer register.
379  * Place the move operation into block bl.
380  *
381  * Handle some special cases here:
382  * 1.) A constant: simply move
383  * 2.) A load: siply load
384  */
385 static ir_node *convert_sng_to_int(ir_node *bl, ir_node *arg) {
386         if (is_Const(arg)) {
387                 tarval *tv = get_Const_tarval(arg);
388                 unsigned v;
389
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);
396         }
397         else if (get_irn_op(skip_Proj(arg)) == op_Load) {
398                 ir_node *load;
399
400                 load = skip_Proj(arg);
401         }
402         assert(0);
403         return NULL;
404 }
405
406 /**
407  * Convert the arguments of a call to support the
408  * ARM calling convention of general purpose AND floating
409  * point arguments.
410  */
411 static void handle_calls(ir_node *call, void *env)
412 {
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;
417         ir_node *bl;
418
419         if (! is_Call(call))
420                 return;
421
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);
426
427         /* it's always enough to handle the first 4 parameters */
428         if (n > 4)
429                 n = 4;
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);
434
435                 if (is_compound_type(param_tp)) {
436                         /* an aggregate parameter: bad case */
437                         assert(0);
438                 }
439                 else {
440                         /* a primitive parameter */
441                         ir_mode *mode = get_type_mode(param_tp);
442
443                         if (mode_is_float(mode)) {
444                                 if (get_mode_size_bits(mode) > 32) {
445                                         ir_node *mem = get_Call_mem(call);
446
447                                         /* Beware: ARM wants the high part first */
448                                         size += 2 * 4;
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]);
452                                         idx += 2;
453                                         set_Call_mem(call, mem);
454                                 }
455                                 else {
456                                         size += 4;
457                                         new_tp[idx] = cg->int_tp;
458                                         new_in[idx] = convert_sng_to_int(bl, get_Call_param(call, i));
459                                         ++idx;
460                                 }
461                                 flag = 1;
462                         }
463                         else {
464                                 size += 4;
465                                 new_tp[idx] = param_tp;
466                                 new_in[idx] = get_Call_param(call, i);
467                                 ++idx;
468                         }
469                 }
470
471                 if (size >= 16)
472                         break;
473         }
474
475         /* if flag is NOT set, no need to translate the method type */
476         if (! flag)
477                 return;
478
479         /* construct a new method type */
480         n       = i;
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));
484
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));
491
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));
494
495         if (is_lowered_type(mtp)) {
496                 mtp = get_associated_type(mtp);
497         }
498         set_lowered_type(mtp, new_mtd);
499
500         set_Call_type(call, new_mtd);
501
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);
508
509         in[0] = get_Call_mem(call);
510         in[1] = get_Call_ptr(call);
511
512         /* finally, change the call inputs */
513         set_irn_in(call, n_param + 2, in);
514 }
515
516 /**
517  * Handle graph transformations before the abi converter does its work.
518  */
519 static void arm_before_abi(void *self) {
520         arm_code_gen_t *cg = self;
521
522         irg_walk_graph(cg->irg, NULL, handle_calls, cg);
523 }
524
525 static void *arm_cg_init(be_irg_t *birg);
526
527 static const arch_code_generator_if_t arm_code_gen_if = {
528         arm_cg_init,
529         arm_before_abi,     /* before abi introduce */
530         arm_prepare_graph,
531         NULL,               /* spill */
532         arm_before_sched,   /* before scheduling hook */
533         arm_before_ra,      /* before register allocation hook */
534         NULL,               /* after register allocation */
535         arm_finish_irg,
536         arm_emit_and_done,
537 };
538
539 /**
540  * Initializes the code generator.
541  */
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;
545         arm_code_gen_t *cg;
546
547         if (! int_tp) {
548                 /* create an integer type with machine size */
549                 int_tp = new_type_primitive(new_id_from_chars("int", 3), mode_Is);
550         }
551
552         cg = xmalloc(sizeof(*cg));
553         cg->impl     = &arm_code_gen_if;
554         cg->irg      = birg->irg;
555         cg->reg_set  = new_set(arm_cmp_irn_reg_assoc, 1024);
556         cg->arch_env = birg->main_env->arch_env;
557         cg->isa      = isa;
558         cg->birg     = birg;
559         cg->int_tp   = int_tp;
560         cg->have_fp  = 0;
561
562         FIRM_DBG_REGISTER(cg->mod, "firm.be.arm.cg");
563
564         isa->num_codegens++;
565
566         if (isa->num_codegens > 1)
567                 cg->emit_decls = 0;
568         else
569                 cg->emit_decls = 1;
570
571         cur_reg_set = cg->reg_set;
572
573         arm_irn_ops.cg = cg;
574
575         /* enter the current code generator */
576         isa->cg = cg;
577
578         return (arch_code_generator_t *)cg;
579 }
580
581
582 /**
583  * Maps all intrinsic calls that the backend support
584  * and map all instructions the backend did not support
585  * to runtime calls.
586  */
587 static void arm_handle_intrinsics(void) {
588   ir_type *tp, *int_tp, *uint_tp;
589   i_record records[8];
590   int n_records = 0;
591
592 #define ID(x) new_id_from_chars(x, sizeof(x)-1)
593
594   int_tp  = new_type_primitive(ID("int"), mode_Is);
595   uint_tp = new_type_primitive(ID("uint"), mode_Iu);
596
597         /* ARM has neither a signed div instruction ... */
598   {
599     runtime_rt rt_Div;
600     i_instr_record *map_Div = &records[n_records++].i_instr;
601
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);
606
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;
613
614     set_entity_visibility(rt_Div.ent, visibility_external_allocated);
615
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;
620   }
621         /* ... nor a signed div instruction ... */
622   {
623     runtime_rt rt_Div;
624     i_instr_record *map_Div = &records[n_records++].i_instr;
625
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);
630
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;
637
638     set_entity_visibility(rt_Div.ent, visibility_external_allocated);
639
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;
644   }
645         /* ... nor a signed mod instruction ... */
646   {
647     runtime_rt rt_Mod;
648     i_instr_record *map_Mod = &records[n_records++].i_instr;
649
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);
654
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;
661
662     set_entity_visibility(rt_Mod.ent, visibility_external_allocated);
663
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;
668   }
669         /* ... nor a unsigned mod. */
670   {
671     runtime_rt rt_Mod;
672     i_instr_record *map_Mod = &records[n_records++].i_instr;
673
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);
678
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;
685
686     set_entity_visibility(rt_Mod.ent, visibility_external_allocated);
687
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;
692   }
693
694   if (n_records > 0)
695     lower_intrinsics(records, n_records);
696 }
697
698 /*****************************************************************
699  *  ____             _                  _   _____  _____
700  * |  _ \           | |                | | |_   _|/ ____|  /\
701  * | |_) | __ _  ___| | _____ _ __   __| |   | | | (___   /  \
702  * |  _ < / _` |/ __| |/ / _ \ '_ \ / _` |   | |  \___ \ / /\ \
703  * | |_) | (_| | (__|   <  __/ | | | (_| |  _| |_ ____) / ____ \
704  * |____/ \__,_|\___|_|\_\___|_| |_|\__,_| |_____|_____/_/    \_\
705  *
706  *****************************************************************/
707
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 */
718 };
719
720 /**
721  * Initializes the backend ISA and opens the output file.
722  */
723 static void *arm_init(FILE *file_handle) {
724         static int inited = 0;
725         arm_isa_t *isa;
726
727         if(inited)
728                 return NULL;
729
730         isa = xmalloc(sizeof(*isa));
731         memcpy(isa, &arm_isa_template, sizeof(*isa));
732
733         arm_register_init(isa);
734
735         isa->cg  = NULL;
736         isa->out = file_handle;
737
738         arm_create_opcodes();
739         arm_handle_intrinsics();
740         arm_switch_section(NULL, NO_SECTION);
741
742         inited = 1;
743         return isa;
744 }
745
746
747
748 /**
749  * frees the ISA structure.
750  */
751 static void arm_done(void *self) {
752         free(self);
753 }
754
755
756 /**
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).
761  */
762 static int arm_get_n_reg_class(const void *self) {
763         const arm_isa_t *isa = self;
764
765         return isa->cg->have_fp ? 2 : 1;
766 }
767
768 /**
769  * Return the register class with requested index.
770  */
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];
773 }
774
775 /**
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.
780  */
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];
784         else
785                 return &arm_reg_classes[CLASS_arm_gp];
786 }
787
788 /**
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.
792  */
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;
796
797         if(!between_type) {
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);
801
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);
805
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));
809         }
810
811         return between_type;
812 }
813
814
815 typedef struct {
816         be_abi_call_flags_bits_t flags;
817         const arch_env_t *arch_env;
818         const arch_isa_t *isa;
819         ir_graph *irg;
820 } arm_abi_env_t;
821
822 static void *arm_abi_init(const be_abi_call_t *call, const arch_env_t *arch_env, ir_graph *irg)
823 {
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;
827         env->irg      = irg;
828         env->arch_env = arch_env;
829         env->isa      = arch_env->isa;
830         return env;
831 }
832
833 static void arm_abi_dont_save_regs(void *self, pset *s)
834 {
835         arm_abi_env_t *env = self;
836         if (env->flags.try_omit_fp)
837                 pset_insert_ptr(s, env->isa->bp);
838 }
839
840
841
842 /**
843  * Build the ARM prolog
844  */
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];
851 //      int n_regs = 0;
852         arch_register_class_t *gp = &arm_reg_classes[CLASS_arm_gp];
853
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]);
863
864         if(env->flags.try_omit_fp)
865                 return env->isa->sp;
866
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] );
870
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);
879         // TODO
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);
882
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);
886
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] );
891
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)));
894         // TODO...
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);
898
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);
908
909         return env->isa->bp;
910 }
911
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]);
918
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);
923
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] );
928
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] );
932         } else {
933                 ir_node *sub12_node;
934                 ir_node *load_node;
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);
937                 // FIXME
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 );
941                 // FIXME
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]);
952         }
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);
957 }
958
959 static const be_abi_callbacks_t arm_abi_callbacks = {
960         arm_abi_init,
961         free,
962         arm_get_between_type,
963         arm_abi_dont_save_regs,
964         arm_abi_prologue,
965         arm_abi_epilogue,
966 };
967
968
969 /**
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
974  */
975 void arm_get_call_abi(const void *self, ir_type *method_type, be_abi_call_t *abi) {
976         ir_type  *tp;
977         ir_mode  *mode;
978         int       i;
979         int       n = get_method_n_params(method_type);
980         be_abi_call_flags_t flags = {
981                 {
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 */
987                 }
988         };
989
990         /* set stack parameter passing style */
991         be_abi_call_set_flags(abi, flags, &arm_abi_callbacks);
992
993         for (i = 0; i < n; i++) {
994                 /* reg = get reg for param i;          */
995                 /* be_abi_call_param_reg(abi, i, reg); */
996                 if (i < 4)
997
998                         be_abi_call_param_reg(abi, i, arm_get_RegParam_reg(i));
999                 else
1000                         be_abi_call_param_stack(abi, i, 4, 0, 0);
1001         }
1002
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);
1008
1009                 be_abi_call_res_reg(abi, 0,
1010                         mode_is_float(mode) ? &arm_fpa_regs[REG_F0] : &arm_gp_regs[REG_R0]);
1011         }
1012 }
1013
1014 static const void *arm_get_irn_ops(const arch_irn_handler_t *self, const ir_node *irn) {
1015         return &arm_irn_ops;
1016 }
1017
1018 const arch_irn_handler_t arm_irn_handler = {
1019         arm_get_irn_ops
1020 };
1021
1022 const arch_irn_handler_t *arm_get_irn_handler(const void *self) {
1023         return &arm_irn_handler;
1024 }
1025
1026 int arm_to_appear_in_schedule(void *block_env, const ir_node *irn) {
1027         return is_arm_irn(irn);
1028 }
1029
1030 /**
1031  * Initializes the code generator interface.
1032  */
1033 static const arch_code_generator_if_t *arm_get_code_generator_if(void *self) {
1034         return &arm_code_gen_if;
1035 }
1036
1037 list_sched_selector_t arm_sched_selector;
1038
1039 /**
1040  * Returns the reg_pressure scheduler with to_appear_in_schedule() over\loaded
1041  */
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;
1046 }
1047
1048 static const ilp_sched_selector_t *arm_get_ilp_sched_selector(const void *self) {
1049         return NULL;
1050 }
1051
1052 /**
1053  * Returns the necessary byte alignment for storing a register of given class.
1054  */
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);
1058 }
1059
1060 static const be_execution_unit_t ***arm_get_allowed_execution_units(const void *self, const ir_node *irn) {
1061         /* TODO */
1062         assert(0);
1063         return NULL;
1064 }
1065
1066 static const be_machine_t *arm_get_machine(const void *self) {
1067         /* TODO */
1068         assert(0);
1069         return NULL;
1070 }
1071
1072 /**
1073  * Return irp irgs in the desired order.
1074  */
1075 static ir_graph **arm_get_irg_list(const void *self, ir_graph ***irg_list) {
1076         return NULL;
1077 }
1078
1079 /**
1080  * Returns the libFirm configuration parameter for this backend.
1081  */
1082 static const backend_params *arm_get_libfirm_params(void) {
1083         static arch_dep_params_t ad = {
1084                 1,  /* allow subs */
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 */
1090         };
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 */
1097         };
1098
1099         p.dep_param = &ad;
1100         return &p;
1101 }
1102
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 },
1111         { NULL,        0 }
1112 };
1113
1114 static lc_opt_enum_int_var_t arch_fpu_var = {
1115         &arm_isa_template.fpu_arch, arm_fpu_items
1116 };
1117
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),
1121         { NULL }
1122 };
1123
1124 const arch_isa_if_t arm_isa_if = {
1125         arm_init,
1126         arm_done,
1127         arm_get_n_reg_class,
1128         arm_get_reg_class,
1129         arm_get_reg_class_for_mode,
1130         arm_get_call_abi,
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,
1138         arm_get_machine,
1139         arm_get_irg_list,
1140 };
1141
1142 void be_init_arch_arm(void)
1143 {
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");
1146
1147         lc_opt_add_table(arm_grp, arm_options);
1148
1149         be_register_isa_if("arm", &arm_isa_if);
1150 }
1151
1152 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_arch_arm);