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