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