C99 feature removed.
[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 "typerep.h"
31 #include "xmalloc.h"
32 #include "error.h"
33
34 /**
35  * Maps an input register representing the i'th register input
36  * to the i'th register output.
37  */
38 static const arch_register_t *map_i_to_o_reg(const arch_register_t *reg)
39 {
40         unsigned idx = arch_register_get_index(reg);
41         assert(REG_I0 <= idx && idx <= REG_I7);
42         idx += REG_O0 - REG_I0;
43         assert(REG_O0 <= idx && idx <= REG_O7);
44         return &sparc_gp_regs[idx];
45 }
46
47 calling_convention_t *sparc_decide_calling_convention(ir_type *function_type,
48                                                       bool caller)
49 {
50         int                   stack_offset = 0;
51         reg_or_stackslot_t   *params;
52         reg_or_stackslot_t   *results;
53         int                   n_param_regs
54                 = sizeof(param_regs)/sizeof(param_regs[0]);
55         int                   n_float_result_regs
56                 = sizeof(float_result_regs)/sizeof(float_result_regs[0]);
57         int                   n_params;
58         int                   n_results;
59         int                   i;
60         int                   regnum;
61         int                   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
75                 if (regnum < n_param_regs) {
76                         const arch_register_t *reg = param_regs[regnum++];
77                         if (caller)
78                                 reg = map_i_to_o_reg(reg);
79                         param->reg0 = reg;
80                 } else {
81                         param->type   = param_type;
82                         param->offset = stack_offset;
83                         /* increase offset 4 bytes so everything is aligned */
84                         stack_offset += bits > 32 ? bits/8 : 4;
85                         continue;
86                 }
87
88                 /* we might need a 2nd 32bit component (for 64bit or double values) */
89                 if (bits > 32) {
90                         if (bits > 64)
91                                 panic("only 32 and 64bit modes supported in sparc backend");
92
93                         if (regnum < n_param_regs) {
94                                 const arch_register_t *reg = param_regs[regnum++];
95                                 if (caller)
96                                         reg = map_i_to_o_reg(reg);
97                                 param->reg1 = reg;
98                         } else {
99                                 ir_mode *mode = param_regs[0]->reg_class->mode;
100                                 ir_type *type = get_type_for_mode(mode);
101                                 param->type   = type;
102                                 param->offset = stack_offset;
103                                 assert(get_mode_size_bits(mode) == 32);
104                                 stack_offset += 4;
105                         }
106                 }
107         }
108
109         /* determine how results are passed */
110         n_results    = get_method_n_ress(function_type);
111         regnum       = 0;
112         float_regnum = 0;
113         results      = XMALLOCNZ(reg_or_stackslot_t, n_results);
114         for (i = 0; i < n_results; ++i) {
115                 ir_type            *result_type = get_method_res_type(function_type, i);
116                 ir_mode            *result_mode = get_type_mode(result_type);
117                 reg_or_stackslot_t *result      = &results[i];
118
119                 if (mode_is_float(result_mode)) {
120                         if (float_regnum >= n_float_result_regs) {
121                                 panic("Too many float results for sparc backend");
122                         } else {
123                                 const arch_register_t *reg = float_result_regs[float_regnum++];
124                                 result->reg0 = reg;
125                         }
126                 } else {
127                         if (get_mode_size_bits(result_mode) > 32) {
128                                 panic("Results with more than 32bits not supported by sparc backend yet");
129                         }
130
131                         if (regnum >= n_param_regs) {
132                                 panic("Too many results for sparc backend");
133                         } else {
134                                 const arch_register_t *reg = param_regs[regnum++];
135                                 if (caller)
136                                         reg = map_i_to_o_reg(reg);
137                                 result->reg0 = reg;
138                         }
139                 }
140         }
141
142         cconv                   = XMALLOCZ(calling_convention_t);
143         cconv->parameters       = params;
144         cconv->param_stack_size = stack_offset;
145         cconv->results          = results;
146
147         return cconv;
148 }
149
150 void sparc_free_calling_convention(calling_convention_t *cconv)
151 {
152         free(cconv->parameters);
153         free(cconv->results);
154         free(cconv);
155 }