an old floating point benchmark
[libfirm] / ir / be / arm / bearch_arm.c
1 /* The main arm backend driver file. */
2 /* $Id$ */
3
4 #ifdef HAVE_CONFIG_H
5 #include "config.h"
6 #endif
7
8 #ifdef WITH_LIBCORE
9 #include <libcore/lc_opts.h>
10 #include <libcore/lc_opts_enum.h>
11 #endif /* WITH_LIBCORE */
12
13 #include "pseudo_irg.h"
14 #include "irgwalk.h"
15 #include "irprog.h"
16 #include "irprintf.h"
17 #include "ircons.h"
18 #include "irgmod.h"
19 #include "lower_intrinsics.h"
20
21 #include "bitset.h"
22 #include "debug.h"
23
24 #include "../bearch.h"                /* the general register allocator interface */
25 #include "../benode_t.h"
26 #include "../belower.h"
27 #include "../besched_t.h"
28 #include "../be.h"
29 #include "../beabi.h"
30
31 #include "bearch_arm_t.h"
32
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"
39
40 #define DEBUG_MODULE "firm.be.arm.isa"
41
42 /* TODO: ugly, but we need it to get access to the registers assigned to Phi nodes */
43 static set *cur_reg_set = NULL;
44
45 /**************************************************
46  *                         _ _              _  __
47  *                        | | |            (_)/ _|
48  *  _ __ ___  __ _    __ _| | | ___   ___   _| |_
49  * | '__/ _ \/ _` |  / _` | | |/ _ \ / __| | |  _|
50  * | | |  __/ (_| | | (_| | | | (_) | (__  | | |
51  * |_|  \___|\__, |  \__,_|_|_|\___/ \___| |_|_|
52  *            __/ |
53  *           |___/
54  **************************************************/
55
56 static ir_node *my_skip_proj(const ir_node *n) {
57         while (is_Proj(n))
58                 n = get_Proj_pred(n);
59         return (ir_node *)n;
60 }
61
62 /**
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.
66  */
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);
72
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));
75                 return NULL;
76         }
77
78         if (mode == mode_T && pos < 0) {
79                 DBG((mod, LEVEL_1, "ignoring request for OUT requirements at %+F\n", irn));
80                 return NULL;
81         }
82
83         DBG((mod, LEVEL_1, "get requirements at pos %d for %+F ... ", pos, irn));
84
85         if (is_Proj(irn)) {
86                 /* in case of a proj, we need to get the correct OUT slot */
87                 /* of the node corresponding to the proj number */
88                 if (pos == -1) {
89                         node_pos = arm_translate_proj_pos(irn);
90                 }
91                 else {
92                         node_pos = pos;
93                 }
94
95                 irn = my_skip_proj(irn);
96
97                 DB((mod, LEVEL_1, "skipping Proj, going to %+F at pos %d ... ", irn, node_pos));
98         }
99
100         /* get requirements for our own nodes */
101         if (is_arm_irn(irn)) {
102                 if (pos >= 0) {
103                         irn_req = get_arm_in_req(irn, pos);
104                 }
105                 else {
106                         irn_req = get_arm_out_req(irn, node_pos);
107                 }
108
109                 DB((mod, LEVEL_1, "returning reqs for %+F at pos %d\n", irn, pos));
110
111                 memcpy(req, &(irn_req->req), sizeof(*req));
112
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);
116                 }
117
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);
121                 }
122         }
123         /* get requirements for FIRM nodes */
124         else {
125                 /* treat Phi like Const with default requirements */
126                 if (is_Phi(irn)) {
127                         DB((mod, LEVEL_1, "returning standard reqs for %+F\n", irn));
128
129                         if (mode_is_float(mode)) {
130                                 memcpy(req, &(arm_default_req_arm_fpa.req), sizeof(*req));
131                         }
132                         else if (mode_is_int(mode) || mode_is_reference(mode)) {
133                                 memcpy(req, &(arm_default_req_arm_gp.req), sizeof(*req));
134                         }
135                         else if (mode == mode_T || mode == mode_M) {
136                                 DBG((mod, LEVEL_1, "ignoring Phi node %+F\n", irn));
137                                 return NULL;
138                         }
139                         else {
140                                 assert(0 && "unsupported Phi-Mode");
141                         }
142                 }
143                 else {
144                         DB((mod, LEVEL_1, "returning NULL for %+F (node not supported)\n", irn));
145                         req = NULL;
146                 }
147         }
148
149         return req;
150 }
151
152 static void arm_set_irn_reg(const void *self, ir_node *irn, const arch_register_t *reg) {
153         int pos = 0;
154
155         if (is_Proj(irn)) {
156
157                 if (get_irn_mode(irn) == mode_X) {
158                         return;
159                 }
160
161                 pos = arm_translate_proj_pos(irn);
162                 irn = my_skip_proj(irn);
163         }
164
165         if (is_arm_irn(irn)) {
166                 const arch_register_t **slots;
167
168                 slots      = get_arm_slots(irn);
169                 slots[pos] = reg;
170         }
171         else {
172                 /* here we set the registers for the Phi nodes */
173                 arm_set_firm_reg(irn, reg, cur_reg_set);
174         }
175 }
176
177 static const arch_register_t *arm_get_irn_reg(const void *self, const ir_node *irn) {
178         int pos = 0;
179         const arch_register_t *reg = NULL;
180
181         if (is_Proj(irn)) {
182
183                 if (get_irn_mode(irn) == mode_X) {
184                         return NULL;
185                 }
186
187                 pos = arm_translate_proj_pos(irn);
188                 irn = my_skip_proj(irn);
189         }
190
191         if (is_arm_irn(irn)) {
192                 const arch_register_t **slots;
193                 slots = get_arm_slots(irn);
194                 reg   = slots[pos];
195         }
196         else {
197                 reg = arm_get_firm_reg(irn, cur_reg_set);
198         }
199
200         return reg;
201 }
202
203 static arch_irn_class_t arm_classify(const void *self, const ir_node *irn) {
204         irn = my_skip_proj(irn);
205
206         if (is_cfop(irn)) {
207                 return arch_irn_class_branch;
208         }
209         else if (is_arm_irn(irn)) {
210                 return arch_irn_class_normal;
211         }
212
213         return 0;
214 }
215
216 static arch_irn_flags_t arm_get_flags(const void *self, const ir_node *irn) {
217         irn = my_skip_proj(irn);
218
219         if (is_arm_irn(irn)) {
220                 return get_arm_flags(irn);
221         }
222         else if (is_Unknown(irn)) {
223                 return arch_irn_flags_ignore;
224         }
225
226         return 0;
227 }
228
229 static entity *arm_get_frame_entity(const void *self, const ir_node *irn) {
230         /* TODO: return the entity assigned to the frame */
231         return NULL;
232 }
233
234 /**
235  * This function is called by the generic backend to correct offsets for
236  * nodes accessing the stack.
237  */
238 static void arm_set_stack_bias(const void *self, ir_node *irn, int bias) {
239         /* TODO: correct offset if irn accesses the stack */
240 }
241
242 /* fill register allocator interface */
243
244 static const arch_irn_ops_if_t arm_irn_ops_if = {
245         arm_get_irn_reg_req,
246         arm_set_irn_reg,
247         arm_get_irn_reg,
248         arm_classify,
249         arm_get_flags,
250         arm_get_frame_entity,
251         arm_set_stack_bias,
252         NULL
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(ir_graph *irg, arm_code_gen_t *cg) {
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         arm_finish_irg(irg, cg);
323         dump_ir_block_graph_sched(irg, "-arm-finished");
324         arm_gen_routine(out, irg, cg);
325
326         cur_reg_set = NULL;
327
328         /* de-allocate code generator */
329         del_set(cg->reg_set);
330         free(self);
331 }
332
333 /**
334  * Move a double floating point value into an integer register.
335  * Place the move operation into block bl.
336  *
337  * Handle some special cases here:
338  * 1.) A constant: simply split into two
339  * 2.) A load: siply split into two
340  */
341 static ir_node *convert_dbl_to_int(ir_node *bl, ir_node *arg, ir_node *mem,
342                                    ir_node **resH, ir_node **resL) {
343         if (is_Const(arg)) {
344                 tarval *tv = get_Const_tarval(arg);
345                 unsigned v;
346
347                 /* get the upper 32 bits */
348                 v =            get_tarval_sub_bits(tv, 7);
349                 v = (v << 8) | get_tarval_sub_bits(tv, 6);
350                 v = (v << 8) | get_tarval_sub_bits(tv, 5);
351                 v = (v << 8) | get_tarval_sub_bits(tv, 4);
352                 *resH = new_Const_long(mode_Is, v);
353
354                 /* get the lower 32 bits */
355                 v =            get_tarval_sub_bits(tv, 3);
356                 v = (v << 8) | get_tarval_sub_bits(tv, 2);
357                 v = (v << 8) | get_tarval_sub_bits(tv, 1);
358                 v = (v << 8) | get_tarval_sub_bits(tv, 0);
359                 *resL = new_Const_long(mode_Is, v);
360         }
361         else if (get_irn_op(skip_Proj(arg)) == op_Load) {
362                 /* FIXME: handling of low/high depends on LE/BE here */
363                 assert(0);
364         }
365         else {
366                 ir_graph *irg = current_ir_graph;
367                 ir_node *conv;
368
369                 conv = new_rd_arm_fpaDbl2GP(NULL, irg, bl, arg, mem);
370                 /* move high/low */
371                 *resL = new_r_Proj(irg, bl, conv, mode_Is, pn_arm_fpaDbl2GP_low);
372                 *resH = new_r_Proj(irg, bl, conv, mode_Is, pn_arm_fpaDbl2GP_high);
373                 mem   = new_r_Proj(irg, bl, conv, mode_M,  pn_arm_fpaDbl2GP_M);
374         }
375         return mem;
376 }
377
378 /**
379  * Move a single floating point value into an integer register.
380  * Place the move operation into block bl.
381  *
382  * Handle some special cases here:
383  * 1.) A constant: simply move
384  * 2.) A load: siply load
385  */
386 static ir_node *convert_sng_to_int(ir_node *bl, ir_node *arg) {
387         if (is_Const(arg)) {
388                 tarval *tv = get_Const_tarval(arg);
389                 unsigned v;
390
391                 /* get the lower 32 bits */
392                 v =            get_tarval_sub_bits(tv, 3);
393                 v = (v << 8) | get_tarval_sub_bits(tv, 2);
394                 v = (v << 8) | get_tarval_sub_bits(tv, 1);
395                 v = (v << 8) | get_tarval_sub_bits(tv, 0);
396                 return new_Const_long(mode_Is, v);
397         }
398         else if (get_irn_op(skip_Proj(arg)) == op_Load) {
399                 ir_node *load;
400
401                 load = skip_Proj(arg);
402         }
403         assert(0);
404         return NULL;
405 }
406
407 /**
408  * Convert the arguments of a call to support the
409  * ARM calling convention of general purpose AND floating
410  * point arguments.
411  */
412 static void handle_calls(ir_node *call, void *env)
413 {
414         arm_code_gen_t *cg = env;
415         int i, j, n, size, idx, flag, n_param, n_res;
416         ir_type *mtp, *new_mtd, *new_tp[5];
417         ir_node *new_in[5], **in;
418         ir_node *bl;
419
420         if (! is_Call(call))
421                 return;
422
423         /* check, if we need conversions */
424         n = get_Call_n_params(call);
425         mtp = get_Call_type(call);
426         assert(get_method_n_params(mtp) == n);
427
428         /* it's always enough to handle the first 4 parameters */
429         if (n > 4)
430                 n = 4;
431         flag = size = idx = 0;
432         bl = get_nodes_block(call);
433         for (i = 0; i < n; ++i) {
434                 ir_type *param_tp = get_method_param_type(mtp, i);
435
436                 if (is_compound_type(param_tp)) {
437                         /* an aggregate parameter: bad case */
438                         assert(0);
439                 }
440                 else {
441                         /* a primitive parameter */
442                         ir_mode *mode = get_type_mode(param_tp);
443
444                         if (mode_is_float(mode)) {
445                                 if (get_mode_size_bits(mode) > 32) {
446                                         ir_node *mem = get_Call_mem(call);
447
448                                         /* Beware: ARM wants the high part first */
449                                         size += 2 * 4;
450                                         new_tp[idx]   = cg->int_tp;
451                                         new_tp[idx+1] = cg->int_tp;
452                                         mem = convert_dbl_to_int(bl, get_Call_param(call, i), mem, &new_in[idx], &new_in[idx+1]);
453                                         idx += 2;
454                                         set_Call_mem(call, mem);
455                                 }
456                                 else {
457                                         size += 4;
458                                         new_tp[idx] = cg->int_tp;
459                                         new_in[idx] = convert_sng_to_int(bl, get_Call_param(call, i));
460                                         ++idx;
461                                 }
462                                 flag = 1;
463                         }
464                         else {
465                                 size += 4;
466                                 new_tp[idx] = param_tp;
467                                 new_in[idx] = get_Call_param(call, i);
468                                 ++idx;
469                         }
470                 }
471
472                 if (size >= 16)
473                         break;
474         }
475
476         /* if flag is NOT set, no need to translate the method type */
477         if (! flag)
478                 return;
479
480         /* construct a new method type */
481         n       = i;
482         n_param = get_method_n_params(mtp) - n + idx;
483         n_res   = get_method_n_ress(mtp);
484         new_mtd = new_d_type_method(get_type_ident(mtp), n_param, n_res, get_type_dbg_info(mtp));
485
486         for (i = 0; i < idx; ++i)
487                 set_method_param_type(new_mtd, i, new_tp[i]);
488         for (i = n, j = idx; i < get_method_n_params(mtp); ++i)
489                 set_method_param_type(new_mtd, j++, get_method_param_type(mtp, i));
490         for (i = 0; i < n_res; ++i)
491                 set_method_res_type(new_mtd, i, get_method_res_type(mtp, i));
492
493         set_method_calling_convention(new_mtd, get_method_calling_convention(mtp));
494         set_method_first_variadic_param_index(new_mtd, get_method_first_variadic_param_index(mtp));
495
496         if (is_lowered_type(mtp)) {
497                 mtp = get_associated_type(mtp);
498         }
499         set_lowered_type(mtp, new_mtd);
500
501         set_Call_type(call, new_mtd);
502
503         /* calculate new in array of the Call */
504         NEW_ARR_A(ir_node *, in, n_param + 2);
505         for (i = 0; i < idx; ++i)
506                 in[2 + i] = new_in[i];
507         for (i = n, j = idx; i < get_method_n_params(mtp); ++i)
508                 in[2 + j++] = get_Call_param(call, i);
509
510         in[0] = get_Call_mem(call);
511         in[1] = get_Call_ptr(call);
512
513         /* finally, change the call inputs */
514         set_irn_in(call, n_param + 2, in);
515 }
516
517 /**
518  * Handle graph transformations before the abi converter does its work.
519  */
520 static void arm_before_abi(void *self) {
521         arm_code_gen_t *cg = self;
522
523         irg_walk_graph(cg->irg, NULL, handle_calls, cg);
524 }
525
526 static void *arm_cg_init(const be_irg_t *birg);
527
528 static const arch_code_generator_if_t arm_code_gen_if = {
529         arm_cg_init,
530         arm_before_abi,     /* before abi introduce */
531         arm_prepare_graph,
532         arm_before_sched,   /* before scheduling hook */
533         arm_before_ra,      /* before register allocation hook */
534         NULL,               /* after register allocation */
535         arm_emit_and_done,
536 };
537
538 /**
539  * Initializes the code generator.
540  */
541 static void *arm_cg_init(const be_irg_t *birg) {
542         static ir_type *int_tp = NULL;
543         arm_isa_t      *isa = (arm_isa_t *)birg->main_env->arch_env->isa;
544         arm_code_gen_t *cg;
545
546         if (! int_tp) {
547                 /* create an integer type with machine size */
548                 int_tp = new_type_primitive(new_id_from_chars("int", 3), mode_Is);
549         }
550
551         cg = xmalloc(sizeof(*cg));
552         cg->impl     = &arm_code_gen_if;
553         cg->irg      = birg->irg;
554         cg->reg_set  = new_set(arm_cmp_irn_reg_assoc, 1024);
555         cg->arch_env = birg->main_env->arch_env;
556         cg->isa      = isa;
557         cg->birg     = birg;
558         cg->int_tp   = int_tp;
559         cg->have_fp  = 0;
560
561         FIRM_DBG_REGISTER(cg->mod, "firm.be.arm.cg");
562
563         isa->num_codegens++;
564
565         if (isa->num_codegens > 1)
566                 cg->emit_decls = 0;
567         else
568                 cg->emit_decls = 1;
569
570         cur_reg_set = cg->reg_set;
571
572         arm_irn_ops.cg = cg;
573
574         /* enter the current code generator */
575         isa->cg = cg;
576
577         return (arch_code_generator_t *)cg;
578 }
579
580
581 /**
582  * Maps all intrinsic calls that the backend support
583  * and map all instructions the backend did not support
584  * to runtime calls.
585  */
586 static void arm_handle_intrinsics(void) {
587   ir_type *tp, *int_tp, *uint_tp;
588   i_record records[8];
589   int n_records = 0;
590
591 #define ID(x) new_id_from_chars(x, sizeof(x)-1)
592
593   int_tp  = new_type_primitive(ID("int"), mode_Is);
594   uint_tp = new_type_primitive(ID("uint"), mode_Iu);
595
596         /* ARM has neither a signed div instruction ... */
597   {
598     runtime_rt rt_Div;
599     i_instr_record *map_Div = &records[n_records++].i_instr;
600
601     tp = new_type_method(ID("rt_iDiv"), 2, 1);
602     set_method_param_type(tp, 0, int_tp);
603     set_method_param_type(tp, 1, int_tp);
604     set_method_res_type(tp, 0, int_tp);
605
606     rt_Div.ent             = new_entity(get_glob_type(), ID("__divsi3"), tp);
607     rt_Div.mode            = mode_T;
608     rt_Div.mem_proj_nr     = pn_Div_M;
609     rt_Div.exc_proj_nr     = pn_Div_X_except;
610     rt_Div.exc_mem_proj_nr = pn_Div_M;
611     rt_Div.res_proj_nr     = pn_Div_res;
612
613     set_entity_visibility(rt_Div.ent, visibility_external_allocated);
614
615     map_Div->kind     = INTRINSIC_INSTR;
616     map_Div->op       = op_Div;
617     map_Div->i_mapper = (i_mapper_func)i_mapper_RuntimeCall;
618     map_Div->ctx      = &rt_Div;
619   }
620         /* ... nor a signed div instruction ... */
621   {
622     runtime_rt rt_Div;
623     i_instr_record *map_Div = &records[n_records++].i_instr;
624
625     tp = new_type_method(ID("rt_uDiv"), 2, 1);
626     set_method_param_type(tp, 0, uint_tp);
627     set_method_param_type(tp, 1, uint_tp);
628     set_method_res_type(tp, 0, uint_tp);
629
630     rt_Div.ent             = new_entity(get_glob_type(), ID("__udivsi3"), tp);
631     rt_Div.mode            = mode_T;
632     rt_Div.mem_proj_nr     = pn_Div_M;
633     rt_Div.exc_proj_nr     = pn_Div_X_except;
634     rt_Div.exc_mem_proj_nr = pn_Div_M;
635     rt_Div.res_proj_nr     = pn_Div_res;
636
637     set_entity_visibility(rt_Div.ent, visibility_external_allocated);
638
639     map_Div->kind     = INTRINSIC_INSTR;
640     map_Div->op       = op_Div;
641     map_Div->i_mapper = (i_mapper_func)i_mapper_RuntimeCall;
642     map_Div->ctx      = &rt_Div;
643   }
644         /* ... nor a signed mod instruction ... */
645   {
646     runtime_rt rt_Mod;
647     i_instr_record *map_Mod = &records[n_records++].i_instr;
648
649     tp = new_type_method(ID("rt_iMod"), 2, 1);
650     set_method_param_type(tp, 0, int_tp);
651     set_method_param_type(tp, 1, int_tp);
652     set_method_res_type(tp, 0, int_tp);
653
654     rt_Mod.ent             = new_entity(get_glob_type(), ID("__modsi3"), tp);
655     rt_Mod.mode            = mode_T;
656     rt_Mod.mem_proj_nr     = pn_Mod_M;
657     rt_Mod.exc_proj_nr     = pn_Mod_X_except;
658     rt_Mod.exc_mem_proj_nr = pn_Mod_M;
659     rt_Mod.res_proj_nr     = pn_Mod_res;
660
661     set_entity_visibility(rt_Mod.ent, visibility_external_allocated);
662
663     map_Mod->kind     = INTRINSIC_INSTR;
664     map_Mod->op       = op_Mod;
665     map_Mod->i_mapper = (i_mapper_func)i_mapper_RuntimeCall;
666     map_Mod->ctx      = &rt_Mod;
667   }
668         /* ... nor a unsigned mod. */
669   {
670     runtime_rt rt_Mod;
671     i_instr_record *map_Mod = &records[n_records++].i_instr;
672
673     tp = new_type_method(ID("rt_uMod"), 2, 1);
674     set_method_param_type(tp, 0, uint_tp);
675     set_method_param_type(tp, 1, uint_tp);
676     set_method_res_type(tp, 0, uint_tp);
677
678     rt_Mod.ent             = new_entity(get_glob_type(), ID("__umodsi3"), tp);
679     rt_Mod.mode            = mode_T;
680     rt_Mod.mem_proj_nr     = pn_Mod_M;
681     rt_Mod.exc_proj_nr     = pn_Mod_X_except;
682     rt_Mod.exc_mem_proj_nr = pn_Mod_M;
683     rt_Mod.res_proj_nr     = pn_Mod_res;
684
685     set_entity_visibility(rt_Mod.ent, visibility_external_allocated);
686
687     map_Mod->kind     = INTRINSIC_INSTR;
688     map_Mod->op       = op_Mod;
689     map_Mod->i_mapper = (i_mapper_func)i_mapper_RuntimeCall;
690     map_Mod->ctx      = &rt_Mod;
691   }
692
693   if (n_records > 0)
694     lower_intrinsics(records, n_records);
695 }
696
697 /*****************************************************************
698  *  ____             _                  _   _____  _____
699  * |  _ \           | |                | | |_   _|/ ____|  /\
700  * | |_) | __ _  ___| | _____ _ __   __| |   | | | (___   /  \
701  * |  _ < / _` |/ __| |/ / _ \ '_ \ / _` |   | |  \___ \ / /\ \
702  * | |_) | (_| | (__|   <  __/ | | | (_| |  _| |_ ____) / ____ \
703  * |____/ \__,_|\___|_|\_\___|_| |_|\__,_| |_____|_____/_/    \_\
704  *
705  *****************************************************************/
706
707 static arm_isa_t arm_isa_template = {
708         &arm_isa_if,           /* isa interface */
709         &arm_gp_regs[REG_SP],  /* stack pointer */
710         &arm_gp_regs[REG_R11], /* base pointer */
711         -1,                    /* stack direction */
712         0,                     /* number of codegenerator objects */
713         0,                     /* use generic register names instead of SP, LR, PC */
714         NULL,                  /* current code generator */
715         NULL,                  /* output file */
716         ARM_FPU_ARCH_FPE,      /* FPU architecture */
717 };
718
719 /**
720  * Initializes the backend ISA and opens the output file.
721  */
722 static void *arm_init(FILE *file_handle) {
723         static int inited = 0;
724         arm_isa_t *isa;
725
726         if(inited)
727                 return NULL;
728
729         isa = xmalloc(sizeof(*isa));
730         memcpy(isa, &arm_isa_template, sizeof(*isa));
731
732         arm_register_init(isa);
733         if (isa->gen_reg_names) {
734                 /* patch register names */
735                 arm_gp_regs[REG_R11].name = "r11";
736                 arm_gp_regs[REG_SP].name  = "r13";
737                 arm_gp_regs[REG_LR].name  = "r14";
738                 arm_gp_regs[REG_PC].name  = "r15";
739         }
740
741         isa->cg  = NULL;
742         isa->out = file_handle;
743
744         arm_create_opcodes();
745         arm_handle_intrinsics();
746         arm_switch_section(NULL, NO_SECTION);
747
748         inited = 1;
749         return isa;
750 }
751
752
753
754 /**
755  * frees the ISA structure.
756  */
757 static void arm_done(void *self) {
758         free(self);
759 }
760
761
762 /**
763  * Report the number of register classes.
764  * If we don't have fp instructions, report only GP
765  * here to speed up register allocation (and makes dumps
766  * smaller and more readable).
767  */
768 static int arm_get_n_reg_class(const void *self) {
769         const arm_isa_t *isa = self;
770
771         return isa->cg->have_fp ? 2 : 1;
772 }
773
774 /**
775  * Return the register class with requested index.
776  */
777 static const arch_register_class_t *arm_get_reg_class(const void *self, int i) {
778         return i == 0 ? &arm_reg_classes[CLASS_arm_gp] : &arm_reg_classes[CLASS_arm_fpa];
779 }
780
781 /**
782  * Get the register class which shall be used to store a value of a given mode.
783  * @param self The this pointer.
784  * @param mode The mode in question.
785  * @return A register class which can hold values of the given mode.
786  */
787 const arch_register_class_t *arm_get_reg_class_for_mode(const void *self, const ir_mode *mode) {
788         if (mode_is_float(mode))
789                 return &arm_reg_classes[CLASS_arm_fpa];
790         else
791                 return &arm_reg_classes[CLASS_arm_gp];
792 }
793
794 /**
795  * Produces the type which sits between the stack args and the locals on the stack.
796  * it will contain the return address and space to store the old base pointer.
797  * @return The Firm type modelling the ABI between type.
798  */
799 static ir_type *arm_get_between_type(void *self) {
800         static ir_type *between_type = NULL;
801         static entity *old_bp_ent    = NULL;
802
803         if(!between_type) {
804                 entity *ret_addr_ent;
805                 ir_type *ret_addr_type = new_type_primitive(new_id_from_str("return_addr"), mode_P);
806                 ir_type *old_bp_type   = new_type_primitive(new_id_from_str("bp"), mode_P);
807
808                 between_type           = new_type_class(new_id_from_str("arm_between_type"));
809                 old_bp_ent             = new_entity(between_type, new_id_from_str("old_bp"), old_bp_type);
810                 ret_addr_ent           = new_entity(between_type, new_id_from_str("old_bp"), ret_addr_type);
811
812                 set_entity_offset_bytes(old_bp_ent, 0);
813                 set_entity_offset_bytes(ret_addr_ent, get_type_size_bytes(old_bp_type));
814                 set_type_size_bytes(between_type, get_type_size_bytes(old_bp_type) + get_type_size_bytes(ret_addr_type));
815         }
816
817         return between_type;
818 }
819
820
821 typedef struct {
822         be_abi_call_flags_bits_t flags;
823         const arch_env_t *arch_env;
824         const arch_isa_t *isa;
825         ir_graph *irg;
826 } arm_abi_env_t;
827
828 static void *arm_abi_init(const be_abi_call_t *call, const arch_env_t *arch_env, ir_graph *irg)
829 {
830         arm_abi_env_t *env     = xmalloc(sizeof(env[0]));
831         be_abi_call_flags_t fl = be_abi_call_get_flags(call);
832         env->flags    = fl.bits;
833         env->irg      = irg;
834         env->arch_env = arch_env;
835         env->isa      = arch_env->isa;
836         return env;
837 }
838
839 static void arm_abi_dont_save_regs(void *self, pset *s)
840 {
841         arm_abi_env_t *env = self;
842         if (env->flags.try_omit_fp)
843                 pset_insert_ptr(s, env->isa->bp);
844 }
845
846
847
848 /**
849  * Build the ARM prolog
850  */
851 static const arch_register_t *arm_abi_prologue(void *self, ir_node **mem, pmap *reg_map) {
852         ir_node *keep, *store;
853         arm_abi_env_t *env = self;
854         ir_graph *irg = env->irg;
855         ir_node *block = get_irg_start_block(irg);
856 //      ir_node *regs[16];
857 //      int n_regs = 0;
858         arch_register_class_t *gp = &arm_reg_classes[CLASS_arm_gp];
859         static const arm_register_req_t *fp_req[] = {
860                 &arm_default_req_arm_gp_r11
861         };
862
863         ir_node *fp = be_abi_reg_map_get(reg_map, env->isa->bp);
864         ir_node *ip = be_abi_reg_map_get(reg_map, &arm_gp_regs[REG_R12]);
865         ir_node *sp = be_abi_reg_map_get(reg_map, env->isa->sp);
866         ir_node *lr = be_abi_reg_map_get(reg_map, &arm_gp_regs[REG_LR]);
867         ir_node *pc = be_abi_reg_map_get(reg_map, &arm_gp_regs[REG_PC]);
868 //      ir_node *r0 = be_abi_reg_map_get(reg_map, &arm_gp_regs[REG_R0]);
869 //      ir_node *r1 = be_abi_reg_map_get(reg_map, &arm_gp_regs[REG_R1]);
870 //      ir_node *r2 = be_abi_reg_map_get(reg_map, &arm_gp_regs[REG_R2]);
871 //      ir_node *r3 = be_abi_reg_map_get(reg_map, &arm_gp_regs[REG_R3]);
872
873         if(env->flags.try_omit_fp)
874                 return env->isa->sp;
875
876         ip = be_new_Copy(gp, irg, block, sp );
877         arch_set_irn_register(env->arch_env, ip, &arm_gp_regs[REG_R12]);
878         be_set_constr_single_reg(ip, BE_OUT_POS(0), &arm_gp_regs[REG_R12] );
879
880 //      if (r0) regs[n_regs++] = r0;
881 //      if (r1) regs[n_regs++] = r1;
882 //      if (r2) regs[n_regs++] = r2;
883 //      if (r3) regs[n_regs++] = r3;
884 //      sp = new_r_arm_StoreStackMInc(irg, block, *mem, sp, n_regs, regs, get_irn_mode(sp));
885 //              set_arm_req_out(sp, &arm_default_req_arm_gp_sp, 0);
886 //              arch_set_irn_register(env->arch_env, sp, env->isa->sp);
887         store = new_rd_arm_StoreStackM4Inc(NULL, irg, block, sp, fp, ip, lr, pc, *mem);
888                 set_arm_req_out(store, &arm_default_req_arm_gp_sp, 0);
889 //              arch_set_irn_register(env->arch_env, store, env->isa->sp);
890
891         sp = new_r_Proj(irg, block, store, env->isa->sp->reg_class->mode, pn_arm_StoreStackM4Inc_ptr);
892                 arch_set_irn_register(env->arch_env, sp, env->isa->sp);
893         *mem = new_r_Proj(irg, block, store, mode_M, pn_arm_StoreStackM4Inc_M);
894
895         keep = be_new_CopyKeep_single(gp, irg, block, ip, sp, get_irn_mode(ip));
896                 be_node_set_reg_class(keep, 1, gp);
897                 arch_set_irn_register(env->arch_env, keep, &arm_gp_regs[REG_R12]);
898                 be_set_constr_single_reg(keep, BE_OUT_POS(0), &arm_gp_regs[REG_R12] );
899
900         fp = new_rd_arm_Sub_i(NULL, irg, block, keep, get_irn_mode(fp),
901                               new_tarval_from_long(4, get_irn_mode(fp)));
902                 set_arm_req_out_all(fp, fp_req);
903                 //set_arm_req_out(fp, &arm_default_req_arm_gp_r11, 0);
904                 arch_set_irn_register(env->arch_env, fp, env->isa->bp);
905
906 //      be_abi_reg_map_set(reg_map, &arm_gp_regs[REG_R0], r0);
907 //      be_abi_reg_map_set(reg_map, &arm_gp_regs[REG_R1], r1);
908 //      be_abi_reg_map_set(reg_map, &arm_gp_regs[REG_R2], r2);
909 //      be_abi_reg_map_set(reg_map, &arm_gp_regs[REG_R3], r3);
910         be_abi_reg_map_set(reg_map, env->isa->bp, fp);
911         be_abi_reg_map_set(reg_map, &arm_gp_regs[REG_R12], keep);
912         be_abi_reg_map_set(reg_map, env->isa->sp, sp);
913         be_abi_reg_map_set(reg_map, &arm_gp_regs[REG_LR], lr);
914         be_abi_reg_map_set(reg_map, &arm_gp_regs[REG_PC], pc);
915
916         return env->isa->bp;
917 }
918
919 static void arm_abi_epilogue(void *self, ir_node *bl, ir_node **mem, pmap *reg_map) {
920         arm_abi_env_t *env = self;
921         ir_node *curr_sp = be_abi_reg_map_get(reg_map, env->isa->sp);
922         ir_node *curr_bp = be_abi_reg_map_get(reg_map, env->isa->bp);
923         ir_node *curr_pc = be_abi_reg_map_get(reg_map, &arm_gp_regs[REG_PC]);
924         ir_node *curr_lr = be_abi_reg_map_get(reg_map, &arm_gp_regs[REG_LR]);
925         static const arm_register_req_t *sub12_req[] = {
926                 &arm_default_req_arm_gp_sp
927         };
928
929 //      TODO: Activate Omit fp in epilogue
930         if(env->flags.try_omit_fp) {
931                 curr_sp = be_new_IncSP(env->isa->sp, env->irg, bl, curr_sp, *mem, BE_STACK_FRAME_SIZE, be_stack_dir_shrink);
932
933                 curr_lr = be_new_CopyKeep_single(&arm_reg_classes[CLASS_arm_gp], env->irg, bl, curr_lr, curr_sp, get_irn_mode(curr_lr));
934                 be_node_set_reg_class(curr_lr, 1, &arm_reg_classes[CLASS_arm_gp]);
935                 arch_set_irn_register(env->arch_env, curr_lr, &arm_gp_regs[REG_LR]);
936                 be_set_constr_single_reg(curr_lr, BE_OUT_POS(0), &arm_gp_regs[REG_LR] );
937
938                 curr_pc = be_new_Copy(&arm_reg_classes[CLASS_arm_gp], env->irg, bl, curr_lr );
939                 arch_set_irn_register(env->arch_env, curr_pc, &arm_gp_regs[REG_PC]);
940                 be_set_constr_single_reg(curr_pc, BE_OUT_POS(0), &arm_gp_regs[REG_PC] );
941         } else {
942                 ir_node *sub12_node;
943                 ir_node *load_node;
944                 tarval *tv = new_tarval_from_long(12,mode_Iu);
945                 sub12_node = new_rd_arm_Sub_i(NULL, env->irg, bl, curr_bp, mode_Iu, tv);
946                 set_arm_req_out_all(sub12_node, sub12_req);
947                 arch_set_irn_register(env->arch_env, sub12_node, env->isa->sp);
948                 load_node = new_rd_arm_LoadStackM3( NULL, env->irg, bl, sub12_node, *mem );
949                 set_arm_req_out(load_node, &arm_default_req_arm_gp_r11, 0);
950                 set_arm_req_out(load_node, &arm_default_req_arm_gp_sp, 1);
951                 set_arm_req_out(load_node, &arm_default_req_arm_gp_pc, 2);
952                 curr_bp = new_r_Proj(env->irg, bl, load_node, env->isa->bp->reg_class->mode, pn_arm_LoadStackM3_res0);
953                 curr_sp = new_r_Proj(env->irg, bl, load_node, env->isa->sp->reg_class->mode, pn_arm_LoadStackM3_res1);
954                 curr_pc = new_r_Proj(env->irg, bl, load_node, mode_Iu, pn_arm_LoadStackM3_res2);
955                 *mem    = new_r_Proj(env->irg, bl, load_node, mode_M, pn_arm_LoadStackM3_M);
956                 arch_set_irn_register(env->arch_env, curr_bp, env->isa->bp);
957                 arch_set_irn_register(env->arch_env, curr_sp, env->isa->sp);
958                 arch_set_irn_register(env->arch_env, curr_pc, &arm_gp_regs[REG_PC]);
959         }
960         be_abi_reg_map_set(reg_map, env->isa->sp, curr_sp);
961         be_abi_reg_map_set(reg_map, env->isa->bp, curr_bp);
962         be_abi_reg_map_set(reg_map, &arm_gp_regs[REG_LR], curr_lr);
963         be_abi_reg_map_set(reg_map, &arm_gp_regs[REG_PC], curr_pc);
964 }
965
966 static const be_abi_callbacks_t arm_abi_callbacks = {
967         arm_abi_init,
968         free,
969         arm_get_between_type,
970         arm_abi_dont_save_regs,
971         arm_abi_prologue,
972         arm_abi_epilogue,
973 };
974
975
976 /**
977  * Get the ABI restrictions for procedure calls.
978  * @param self        The this pointer.
979  * @param method_type The type of the method (procedure) in question.
980  * @param abi         The abi object to be modified
981  */
982 void arm_get_call_abi(const void *self, ir_type *method_type, be_abi_call_t *abi) {
983         ir_type  *tp;
984         ir_mode  *mode;
985         int       i;
986         int       n = get_method_n_params(method_type);
987         be_abi_call_flags_t flags = {
988                 {
989                         0, /* store from left to right */
990                         0, /* store arguments sequential */
991                         1, /* try to omit the frame pointer */
992                         1, /* the function can use any register as frame pointer */
993                         1  /* a call can take the callee's address as an immediate */
994                 }
995         };
996
997         /* set stack parameter passing style */
998         be_abi_call_set_flags(abi, flags, &arm_abi_callbacks);
999
1000         for (i = 0; i < n; i++) {
1001                 /* reg = get reg for param i;          */
1002                 /* be_abi_call_param_reg(abi, i, reg); */
1003                 if (i < 4)
1004
1005                         be_abi_call_param_reg(abi, i, arm_get_RegParam_reg(i));
1006                 else
1007                         be_abi_call_param_stack(abi, i, 4, 0, 0);
1008         }
1009
1010         /* default: return value is in R0 resp. F0 */
1011         assert(get_method_n_ress(method_type) < 2);
1012         if (get_method_n_ress(method_type) > 0) {
1013                 tp   = get_method_res_type(method_type, 0);
1014                 mode = get_type_mode(tp);
1015
1016                 be_abi_call_res_reg(abi, 0,
1017                         mode_is_float(mode) ? &arm_fpa_regs[REG_F0] : &arm_gp_regs[REG_R0]);
1018         }
1019 }
1020
1021 static const void *arm_get_irn_ops(const arch_irn_handler_t *self, const ir_node *irn) {
1022         return &arm_irn_ops;
1023 }
1024
1025 const arch_irn_handler_t arm_irn_handler = {
1026         arm_get_irn_ops
1027 };
1028
1029 const arch_irn_handler_t *arm_get_irn_handler(const void *self) {
1030         return &arm_irn_handler;
1031 }
1032
1033 int arm_to_appear_in_schedule(void *block_env, const ir_node *irn) {
1034         return is_arm_irn(irn);
1035 }
1036
1037 /**
1038  * Initializes the code generator interface.
1039  */
1040 static const arch_code_generator_if_t *arm_get_code_generator_if(void *self) {
1041         return &arm_code_gen_if;
1042 }
1043
1044 list_sched_selector_t arm_sched_selector;
1045
1046 /**
1047  * Returns the reg_pressure scheduler with to_appear_in_schedule() over\loaded
1048  */
1049 static const list_sched_selector_t *arm_get_list_sched_selector(const void *self) {
1050         memcpy(&arm_sched_selector, reg_pressure_selector, sizeof(list_sched_selector_t));
1051         arm_sched_selector.to_appear_in_schedule = arm_to_appear_in_schedule;
1052         return &arm_sched_selector;
1053 }
1054
1055 /**
1056  * Returns the necessary byte alignment for storing a register of given class.
1057  */
1058 static int arm_get_reg_class_alignment(const void *self, const arch_register_class_t *cls) {
1059         ir_mode *mode = arch_register_class_mode(cls);
1060         return get_mode_size_bytes(mode);
1061 }
1062
1063 /**
1064  * Returns the libFirm configuration parameter for this backend.
1065  */
1066 static const backend_params *arm_get_libfirm_params(void) {
1067         static arch_dep_params_t ad = {
1068                 1,  /* allow subs */
1069                 0,      /* Muls are fast enough on ARM */
1070                 31, /* shift would be ok */
1071                 0,  /* SMUL is needed, only in Arch M*/
1072                 0,  /* UMUL is needed, only in Arch M */
1073                 32, /* SMUL & UMUL available for 32 bit */
1074         };
1075         static backend_params p = {
1076                 NULL,  /* no additional opcodes */
1077                 NULL,  /* will be set later */
1078                 1,     /* need dword lowering */
1079                 NULL,  /* but yet no creator function */
1080                 NULL,  /* context for create_intrinsic_fkt */
1081         };
1082
1083         p.dep_param = &ad;
1084         return &p;
1085 }
1086
1087 #ifdef WITH_LIBCORE
1088
1089 /* fpu set architectures. */
1090 static const lc_opt_enum_int_items_t arm_fpu_items[] = {
1091         { "softfloat", ARM_FPU_ARCH_SOFTFLOAT },
1092         { "fpe",       ARM_FPU_ARCH_FPE },
1093         { "fpa",       ARM_FPU_ARCH_FPA },
1094         { "vfp1xd",    ARM_FPU_ARCH_VFP_V1xD },
1095         { "vfp1",      ARM_FPU_ARCH_VFP_V1 },
1096         { "vfp2",      ARM_FPU_ARCH_VFP_V2 },
1097         { NULL,        0 }
1098 };
1099
1100 static lc_opt_enum_int_var_t arch_fpu_var = {
1101         &arm_isa_template.fpu_arch, arm_fpu_items
1102 };
1103
1104 static const lc_opt_table_entry_t arm_options[] = {
1105         LC_OPT_ENT_ENUM_INT("fpunit",    "select the floating point unit", &arch_fpu_var),
1106         LC_OPT_ENT_BOOL("gen_reg_names", "use generic register names", &arm_isa_template.gen_reg_names),
1107         { NULL }
1108 };
1109
1110 /**
1111  * Register command line options for the ARM backend.
1112  *
1113  * Options so far:
1114  *
1115  * arm-fpuunit=unit     select the floating point unit
1116  * arm-gen_reg_names    use generic register names instead of SP, LR, PC
1117  */
1118 static void arm_register_options(lc_opt_entry_t *ent)
1119 {
1120         lc_opt_entry_t *be_grp_arm = lc_opt_get_grp(ent, "arm");
1121         lc_opt_add_table(be_grp_arm, arm_options);
1122 }
1123 #endif /* WITH_LIBCORE */
1124
1125 const arch_isa_if_t arm_isa_if = {
1126         arm_init,
1127         arm_done,
1128         arm_get_n_reg_class,
1129         arm_get_reg_class,
1130         arm_get_reg_class_for_mode,
1131         arm_get_call_abi,
1132         arm_get_irn_handler,
1133         arm_get_code_generator_if,
1134         arm_get_list_sched_selector,
1135         arm_get_reg_class_alignment,
1136         arm_get_libfirm_params,
1137 #ifdef WITH_LIBCORE
1138         arm_register_options
1139 #endif
1140 };