perform custom abi construction in sparc as well to handle floatingpoint
[libfirm] / ir / be / sparc / sparc_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 "sparc_cconv.h"
29 #include "irmode.h"
30 #include "typerep.h"
31 #include "xmalloc.h"
32 #include "error.h"
33
34 static const arch_register_t *map_i_to_o_reg(const arch_register_t *reg)
35 {
36         unsigned idx = arch_register_get_index(reg);
37         assert(REG_I0 <= idx && idx <= REG_I7);
38         idx += REG_O0 - REG_I0;
39         assert(REG_O0 <= idx && idx <= REG_O7);
40         return &sparc_gp_regs[idx];
41 }
42
43 calling_convention_t *sparc_decide_calling_convention(ir_type *function_type,
44                                                       bool caller)
45 {
46         int                   stack_offset = 0;
47         reg_or_stackslot_t   *params;
48         reg_or_stackslot_t   *results;
49         int                   n_param_regs
50                 = sizeof(param_regs)/sizeof(param_regs[0]);
51         int                   n_float_result_regs
52                 = sizeof(float_result_regs)/sizeof(float_result_regs[0]);
53         int                   n_params;
54         int                   n_results;
55         int                   i;
56         int                   regnum;
57         int                   float_regnum;
58         calling_convention_t *cconv;
59
60         /* determine how parameters are passed */
61         n_params = get_method_n_params(function_type);
62         regnum   = 0;
63         params   = XMALLOCNZ(reg_or_stackslot_t, n_params);
64
65         for (i = 0; i < n_params; ++i) {
66                 ir_type            *param_type = get_method_param_type(function_type,i);
67                 ir_mode            *mode       = get_type_mode(param_type);
68                 int                 bits       = get_mode_size_bits(mode);
69                 reg_or_stackslot_t *param      = &params[i];
70                 param->type = param_type;
71
72                 if (regnum < n_param_regs) {
73                         const arch_register_t *reg = param_regs[regnum++];
74                         if (caller)
75                                 reg = map_i_to_o_reg(reg);
76                         param->reg0 = reg;
77                 } else {
78                         param->offset = stack_offset;
79                         /* increase offset 4 bytes so everything is aligned */
80                         stack_offset += bits > 32 ? bits/8 : 4;
81                         continue;
82                 }
83
84                 /* we might need a 2nd 32bit component (for 64bit or double values) */
85                 if (bits > 32) {
86                         if (bits > 64)
87                                 panic("only 32 and 64bit modes supported in sparc backend");
88
89                         if (regnum < n_param_regs) {
90                                 const arch_register_t *reg = param_regs[regnum++];
91                                 if (caller)
92                                         reg = map_i_to_o_reg(reg);
93                                 param->reg1 = reg;
94                         } else {
95                                 ir_mode *mode = param_regs[0]->reg_class->mode;
96                                 ir_type *type = get_type_for_mode(mode);
97                                 param->type   = type;
98                                 param->offset = stack_offset;
99                                 assert(get_mode_size_bits(mode) == 32);
100                                 stack_offset += 4;
101                         }
102                 }
103         }
104
105         /* determine how results are passed */
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 for sparc backend");
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 by sparc backend yet");
125                         }
126
127                         if (regnum >= n_param_regs) {
128                                 panic("Too many results for sparc backend");
129                         } else {
130                                 const arch_register_t *reg = param_regs[regnum++];
131                                 if (caller)
132                                         reg = map_i_to_o_reg(reg);
133                                 result->reg0 = reg;
134                         }
135                 }
136         }
137
138         cconv                   = XMALLOCZ(calling_convention_t);
139         cconv->parameters       = params;
140         cconv->param_stack_size = stack_offset;
141         cconv->results          = results;
142
143         return cconv;
144 }
145
146 void sparc_free_calling_convention(calling_convention_t *cconv)
147 {
148         free(cconv->parameters);
149         free(cconv->results);
150         free(cconv);
151 }