becopyopt: Remove the unnecessary attribute name from struct copy_opt_t.
[libfirm] / ir / be / arm / arm_cconv.c
1 /*
2  * This file is part of libFirm.
3  * Copyright (C) 2012 University of Karlsruhe.
4  */
5
6 /**
7  * @file
8  * @brief   calling convention helpers
9  * @author  Matthias Braun
10  */
11 #include "config.h"
12
13 #include "arm_cconv.h"
14 #include "beirg.h"
15 #include "irmode.h"
16 #include "typerep.h"
17 #include "xmalloc.h"
18 #include "error.h"
19 #include "util.h"
20
21 static const unsigned ignore_regs[] = {
22         REG_R12,
23         REG_SP,
24         REG_PC,
25         REG_FL,
26 };
27
28 static const arch_register_t* const param_regs[] = {
29         &arm_registers[REG_R0],
30         &arm_registers[REG_R1],
31         &arm_registers[REG_R2],
32         &arm_registers[REG_R3]
33 };
34
35 static const arch_register_t* const result_regs[] = {
36         &arm_registers[REG_R0],
37         &arm_registers[REG_R1],
38         &arm_registers[REG_R2],
39         &arm_registers[REG_R3]
40 };
41
42 static const arch_register_t* const float_result_regs[] = {
43         &arm_registers[REG_F0],
44         &arm_registers[REG_F1]
45 };
46
47 calling_convention_t *arm_decide_calling_convention(const ir_graph *irg,
48                                                     ir_type *function_type)
49 {
50         unsigned              stack_offset      = 0;
51         unsigned              n_param_regs_used = 0;
52         reg_or_stackslot_t   *params;
53         reg_or_stackslot_t   *results;
54         size_t const          n_param_regs        = ARRAY_SIZE(param_regs);
55         size_t const          n_result_regs       = ARRAY_SIZE(result_regs);
56         size_t const          n_float_result_regs = ARRAY_SIZE(float_result_regs);
57         size_t                n_params;
58         size_t                n_results;
59         size_t                i;
60         size_t                regnum;
61         size_t                float_regnum;
62         calling_convention_t *cconv;
63
64         /* determine how parameters are passed */
65         n_params = get_method_n_params(function_type);
66         regnum   = 0;
67         params   = XMALLOCNZ(reg_or_stackslot_t, n_params);
68
69         for (i = 0; i < n_params; ++i) {
70                 ir_type            *param_type = get_method_param_type(function_type,i);
71                 ir_mode            *mode       = get_type_mode(param_type);
72                 int                 bits       = get_mode_size_bits(mode);
73                 reg_or_stackslot_t *param      = &params[i];
74                 param->type = param_type;
75
76                 if (regnum < n_param_regs) {
77                         const arch_register_t *reg = param_regs[regnum++];
78                         param->reg0 = reg;
79                 } else {
80                         param->offset = stack_offset;
81                         /* increase offset 4 bytes so everything is aligned */
82                         stack_offset += bits > 32 ? bits/8 : 4;
83                         continue;
84                 }
85
86                 /* we might need a 2nd 32bit component (for 64bit or double values) */
87                 if (bits > 32) {
88                         if (bits > 64)
89                                 panic("only 32 and 64bit modes supported");
90
91                         if (regnum < n_param_regs) {
92                                 const arch_register_t *reg = param_regs[regnum++];
93                                 param->reg1 = reg;
94                         } else {
95                                 ir_mode *pmode = param_regs[0]->reg_class->mode;
96                                 ir_type *type  = get_type_for_mode(pmode);
97                                 param->type    = type;
98                                 param->offset  = stack_offset;
99                                 assert(get_mode_size_bits(pmode) == 32);
100                                 stack_offset += 4;
101                         }
102                 }
103         }
104         n_param_regs_used = regnum;
105
106         n_results    = get_method_n_ress(function_type);
107         regnum       = 0;
108         float_regnum = 0;
109         results      = XMALLOCNZ(reg_or_stackslot_t, n_results);
110         for (i = 0; i < n_results; ++i) {
111                 ir_type            *result_type = get_method_res_type(function_type, i);
112                 ir_mode            *result_mode = get_type_mode(result_type);
113                 reg_or_stackslot_t *result      = &results[i];
114
115                 if (mode_is_float(result_mode)) {
116                         if (float_regnum >= n_float_result_regs) {
117                                 panic("Too many float results");
118                         } else {
119                                 const arch_register_t *reg = float_result_regs[float_regnum++];
120                                 result->reg0 = reg;
121                         }
122                 } else {
123                         if (get_mode_size_bits(result_mode) > 32) {
124                                 panic("Results with more than 32bits not supported yet");
125                         }
126
127                         if (regnum >= n_result_regs) {
128                                 panic("Too many results");
129                         } else {
130                                 const arch_register_t *reg = result_regs[regnum++];
131                                 result->reg0 = reg;
132                         }
133                 }
134         }
135
136         cconv                   = XMALLOCZ(calling_convention_t);
137         cconv->parameters       = params;
138         cconv->param_stack_size = stack_offset;
139         cconv->n_reg_params     = n_param_regs_used;
140         cconv->results          = results;
141
142         /* setup allocatable registers */
143         if (irg != NULL) {
144                 be_irg_t       *birg      = be_birg_from_irg(irg);
145                 size_t          n_ignores = ARRAY_SIZE(ignore_regs);
146                 struct obstack *obst      = &birg->obst;
147                 size_t          r;
148
149                 assert(birg->allocatable_regs == NULL);
150                 birg->allocatable_regs = rbitset_obstack_alloc(obst, N_ARM_REGISTERS);
151                 rbitset_set_all(birg->allocatable_regs, N_ARM_REGISTERS);
152                 for (r = 0; r < n_ignores; ++r) {
153                         rbitset_clear(birg->allocatable_regs, ignore_regs[r]);
154                 }
155         }
156
157         return cconv;
158 }
159
160 void arm_free_calling_convention(calling_convention_t *cconv)
161 {
162         free(cconv->parameters);
163         free(cconv->results);
164         free(cconv);
165 }