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