sparc: implement omit-fp mode
[libfirm] / ir / be / sparc / sparc_cconv.c
1 /*
2  * Copyright (C) 1995-2010 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 "irgwalk.h"
31 #include "typerep.h"
32 #include "xmalloc.h"
33 #include "error.h"
34
35 /**
36  * Maps an input register representing the i'th register input
37  * to the i'th register output.
38  */
39 static const arch_register_t *map_i_to_o_reg(const arch_register_t *reg)
40 {
41         unsigned idx = reg->global_index;
42         assert(REG_I0 <= idx && idx <= REG_I7);
43         idx += REG_O0 - REG_I0;
44         assert(REG_O0 <= idx && idx <= REG_O7);
45         return &sparc_registers[idx];
46 }
47
48 static void check_omit_fp(ir_node *node, void *env)
49 {
50         bool *can_omit_fp = (bool*) env;
51
52         /* omit-fp is not possible if:
53          *  - we have allocations on the stack
54          *  - we have calls (with the exception of tail-calls once we support them)
55          */
56         if ((is_Alloc(node) && get_Alloc_where(node) == stack_alloc)
57                         || (is_Free(node) && get_Free_where(node) == stack_alloc)
58                         || is_Call(node)) {
59                 *can_omit_fp = false;
60         }
61 }
62
63 calling_convention_t *sparc_decide_calling_convention(ir_type *function_type,
64                                                       ir_graph *irg)
65 {
66         int                   stack_offset = 0;
67         reg_or_stackslot_t   *params;
68         reg_or_stackslot_t   *results;
69         int                   n_param_regs
70                 = sizeof(param_regs)/sizeof(param_regs[0]);
71         int                   n_float_result_regs
72                 = sizeof(float_result_regs)/sizeof(float_result_regs[0]);
73         int                   n_params;
74         int                   n_results;
75         int                   i;
76         int                   regnum;
77         int                   float_regnum;
78         calling_convention_t *cconv;
79         bool                  omit_fp = false;
80
81         if (irg != NULL) {
82                 const be_options_t *options = be_get_irg_options(irg);
83                 omit_fp = options->omit_fp;
84                 irg_walk_graph(irg, check_omit_fp, NULL, &omit_fp);
85         }
86
87         /* determine how parameters are passed */
88         n_params = get_method_n_params(function_type);
89         regnum   = 0;
90         params   = XMALLOCNZ(reg_or_stackslot_t, n_params);
91
92         for (i = 0; i < n_params; ++i) {
93                 ir_type            *param_type = get_method_param_type(function_type,i);
94                 ir_mode            *mode       = get_type_mode(param_type);
95                 int                 bits       = get_mode_size_bits(mode);
96                 reg_or_stackslot_t *param      = &params[i];
97
98                 if (regnum < n_param_regs) {
99                         const arch_register_t *reg = param_regs[regnum++];
100                         if (irg == NULL || omit_fp)
101                                 reg = map_i_to_o_reg(reg);
102                         param->reg0 = reg;
103                 } else {
104                         param->type   = param_type;
105                         param->offset = stack_offset;
106                         /* increase offset 4 bytes so everything is aligned */
107                         stack_offset += bits > 32 ? bits/8 : 4;
108                         continue;
109                 }
110
111                 /* we might need a 2nd 32bit component (for 64bit or double values) */
112                 if (bits > 32) {
113                         if (bits > 64)
114                                 panic("only 32 and 64bit modes supported in sparc backend");
115
116                         if (regnum < n_param_regs) {
117                                 const arch_register_t *reg = param_regs[regnum++];
118                                 if (irg == NULL || omit_fp)
119                                         reg = map_i_to_o_reg(reg);
120                                 param->reg1 = reg;
121                         } else {
122                                 ir_mode *mode = param_regs[0]->reg_class->mode;
123                                 ir_type *type = get_type_for_mode(mode);
124                                 param->type   = type;
125                                 param->offset = stack_offset;
126                                 assert(get_mode_size_bits(mode) == 32);
127                                 stack_offset += 4;
128                         }
129                 }
130         }
131
132         /* determine how results are passed */
133         n_results    = get_method_n_ress(function_type);
134         regnum       = 0;
135         float_regnum = 0;
136         results      = XMALLOCNZ(reg_or_stackslot_t, n_results);
137         for (i = 0; i < n_results; ++i) {
138                 ir_type            *result_type = get_method_res_type(function_type, i);
139                 ir_mode            *result_mode = get_type_mode(result_type);
140                 reg_or_stackslot_t *result      = &results[i];
141
142                 if (mode_is_float(result_mode)) {
143                         if (float_regnum >= n_float_result_regs) {
144                                 panic("Too many float results for sparc backend");
145                         } else {
146                                 const arch_register_t *reg = float_result_regs[float_regnum++];
147                                 result->reg0 = reg;
148                         }
149                 } else {
150                         if (get_mode_size_bits(result_mode) > 32) {
151                                 panic("Results with more than 32bits not supported by sparc backend yet");
152                         }
153
154                         if (regnum >= n_param_regs) {
155                                 panic("Too many results for sparc backend");
156                         } else {
157                                 const arch_register_t *reg = param_regs[regnum++];
158                                 if (irg == NULL || omit_fp)
159                                         reg = map_i_to_o_reg(reg);
160                                 result->reg0 = reg;
161                         }
162                 }
163         }
164
165         cconv                   = XMALLOCZ(calling_convention_t);
166         cconv->parameters       = params;
167         cconv->param_stack_size = stack_offset;
168         cconv->results          = results;
169         cconv->omit_fp          = omit_fp;
170
171         return cconv;
172 }
173
174 void sparc_free_calling_convention(calling_convention_t *cconv)
175 {
176         free(cconv->parameters);
177         free(cconv->results);
178         free(cconv);
179 }