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