perform custom abi construction in sparc as well to handle floatingpoint
[libfirm] / ir / be / arm / arm_cconv.c
1 /*
2  * Copyright (C) 1995-2008 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   calling convention helpers
23  * @author  Matthias Braun
24  * @version $Id$
25  */
26 #include "config.h"
27
28 #include "arm_cconv.h"
29 #include "irmode.h"
30 #include "typerep.h"
31 #include "xmalloc.h"
32 #include "error.h"
33
34 calling_convention_t *arm_decide_calling_convention(ir_type *function_type)
35 {
36         int                   stack_offset = 0;
37         reg_or_stackslot_t   *params;
38         reg_or_stackslot_t   *results;
39         int                   n_param_regs
40                 = sizeof(param_regs)/sizeof(param_regs[0]);
41         int                   n_result_regs
42                 = sizeof(result_regs)/sizeof(result_regs[0]);
43         int                   n_float_result_regs
44                 = sizeof(float_result_regs)/sizeof(float_result_regs[0]);
45         int                   n_params;
46         int                   n_results;
47         int                   i;
48         int                   regnum;
49         int                   float_regnum;
50         calling_convention_t *cconv;
51
52         /* determine how parameters are passed */
53         n_params = get_method_n_params(function_type);
54         regnum   = 0;
55         params   = XMALLOCNZ(reg_or_stackslot_t, n_params);
56
57         for (i = 0; i < n_params; ++i) {
58                 ir_type            *param_type = get_method_param_type(function_type,i);
59                 ir_mode            *mode       = get_type_mode(param_type);
60                 int                 bits       = get_mode_size_bits(mode);
61                 reg_or_stackslot_t *param      = &params[i];
62                 param->type = param_type;
63
64                 if (regnum < n_param_regs) {
65                         const arch_register_t *reg = param_regs[regnum++];
66                         param->reg0 = reg;
67                 } else {
68                         param->offset = stack_offset;
69                         /* increase offset 4 bytes so everything is aligned */
70                         stack_offset += bits > 32 ? bits/8 : 4;
71                         continue;
72                 }
73
74                 /* we might need a 2nd 32bit component (for 64bit or double values) */
75                 if (bits > 32) {
76                         if (bits > 64)
77                                 panic("only 32 and 64bit modes supported in arm backend");
78
79                         if (regnum < n_param_regs) {
80                                 const arch_register_t *reg = param_regs[regnum++];
81                                 param->reg1 = reg;
82                         } else {
83                                 ir_mode *mode = param_regs[0]->reg_class->mode;
84                                 ir_type *type = get_type_for_mode(mode);
85                                 param->type   = type;
86                                 param->offset = stack_offset;
87                                 assert(get_mode_size_bits(mode) == 32);
88                                 stack_offset += 4;
89                         }
90                 }
91         }
92
93         n_results    = get_method_n_ress(function_type);
94         regnum       = 0;
95         float_regnum = 0;
96         results      = XMALLOCNZ(reg_or_stackslot_t, n_results);
97         for (i = 0; i < n_results; ++i) {
98                 ir_type            *result_type = get_method_res_type(function_type, i);
99                 ir_mode            *result_mode = get_type_mode(result_type);
100                 reg_or_stackslot_t *result      = &results[i];
101
102                 if (mode_is_float(result_mode)) {
103                         if (float_regnum >= n_float_result_regs) {
104                                 panic("Too many float results for arm backend");
105                         } else {
106                                 const arch_register_t *reg = float_result_regs[float_regnum++];
107                                 result->reg0 = reg;
108                         }
109                 } else {
110                         if (get_mode_size_bits(result_mode) > 32) {
111                                 panic("Results with more than 32bits not supported by arm backend yet");
112                         }
113
114                         if (regnum >= n_result_regs) {
115                                 panic("Too many results for arm backend");
116                         } else {
117                                 const arch_register_t *reg = result_regs[regnum++];
118                                 result->reg0 = reg;
119                         }
120                 }
121         }
122
123         cconv                   = XMALLOCZ(calling_convention_t);
124         cconv->parameters       = params;
125         cconv->param_stack_size = stack_offset;
126         cconv->results          = results;
127
128         return cconv;
129 }
130
131 void arm_free_calling_convention(calling_convention_t *cconv)
132 {
133         free(cconv->parameters);
134         free(cconv->results);
135         free(cconv);
136 }