1 /* Function return value location for Linux/RISC-V ABI.
2 Copyright (C) 2018 Sifive, Inc.
3 Copyright (C) 2013 Red Hat, Inc.
4 This file is part of elfutils.
5
6 This file is free software; you can redistribute it and/or modify
7 it under the terms of either
8
9 * the GNU Lesser General Public License as published by the Free
10 Software Foundation; either version 3 of the License, or (at
11 your option) any later version
12
13 or
14
15 * the GNU General Public License as published by the Free
16 Software Foundation; either version 2 of the License, or (at
17 your option) any later version
18
19 or both in parallel, as here.
20
21 elfutils is distributed in the hope that it will be useful, but
22 WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
24 General Public License for more details.
25
26 You should have received copies of the GNU General Public License and
27 the GNU Lesser General Public License along with this program. If
28 not, see <http://www.gnu.org/licenses/>. */
29
30 #ifdef HAVE_CONFIG_H
31 # include <config.h>
32 #endif
33
34 #include <stdio.h>
35 #include <inttypes.h>
36
37 #include <assert.h>
38 #include <dwarf.h>
39
40 #define BACKEND riscv_
41 #include "libebl_CPU.h"
42
43 static int
dwarf_bytesize_aux(Dwarf_Die * die,Dwarf_Word * sizep)44 dwarf_bytesize_aux (Dwarf_Die *die, Dwarf_Word *sizep)
45 {
46 int bits;
47 if (((bits = 8 * dwarf_bytesize (die)) < 0
48 && (bits = dwarf_bitsize (die)) < 0)
49 || bits % 8 != 0)
50 return -1;
51
52 *sizep = bits / 8;
53 return 0;
54 }
55
56 static int
pass_in_gpr_lp64(const Dwarf_Op ** locp,Dwarf_Word size)57 pass_in_gpr_lp64 (const Dwarf_Op **locp, Dwarf_Word size)
58 {
59 static const Dwarf_Op loc[] =
60 {
61 { .atom = DW_OP_reg10 }, { .atom = DW_OP_piece, .number = 8 },
62 { .atom = DW_OP_reg11 }, { .atom = DW_OP_piece, .number = 8 }
63 };
64
65 *locp = loc;
66 return size <= 8 ? 1 : 4;
67 }
68
69 static int
pass_by_ref(const Dwarf_Op ** locp)70 pass_by_ref (const Dwarf_Op **locp)
71 {
72 static const Dwarf_Op loc[] = { { .atom = DW_OP_breg10 } };
73
74 *locp = loc;
75 return 1;
76 }
77
78 static int
pass_in_fpr_lp64f(const Dwarf_Op ** locp,Dwarf_Word size)79 pass_in_fpr_lp64f (const Dwarf_Op **locp, Dwarf_Word size)
80 {
81 static const Dwarf_Op loc[] =
82 {
83 { .atom = DW_OP_regx, .number = 42 },
84 { .atom = DW_OP_piece, .number = 4 },
85 { .atom = DW_OP_regx, .number = 43 },
86 { .atom = DW_OP_piece, .number = 4 }
87 };
88
89 *locp = loc;
90 return size <= 4 ? 1 : 4;
91 }
92
93 static int
pass_in_fpr_lp64d(const Dwarf_Op ** locp,Dwarf_Word size)94 pass_in_fpr_lp64d (const Dwarf_Op **locp, Dwarf_Word size)
95 {
96 static const Dwarf_Op loc[] =
97 {
98 { .atom = DW_OP_regx, .number = 42 },
99 { .atom = DW_OP_piece, .number = 8 },
100 { .atom = DW_OP_regx, .number = 43 },
101 { .atom = DW_OP_piece, .number = 8 }
102 };
103
104 *locp = loc;
105 return size <= 8 ? 1 : 4;
106 }
107
108 static int
flatten_aggregate_arg(Dwarf_Die * typedie,Dwarf_Die * arg0,Dwarf_Die * arg1)109 flatten_aggregate_arg (Dwarf_Die *typedie __attribute__ ((unused)),
110 Dwarf_Die *arg0 __attribute__ ((unused)),
111 Dwarf_Die *arg1 __attribute__ ((unused)))
112 {
113 /* ??? */
114 return 1;
115 }
116
117 static int
pass_by_flattened_arg(const Dwarf_Op ** locp,Dwarf_Word size,Dwarf_Die * arg0,Dwarf_Die * arg1)118 pass_by_flattened_arg (const Dwarf_Op **locp __attribute__ ((unused)),
119 Dwarf_Word size __attribute__ ((unused)),
120 Dwarf_Die *arg0 __attribute__ ((unused)),
121 Dwarf_Die *arg1 __attribute__ ((unused)))
122 {
123 /* ??? */
124 return -2;
125 }
126
127 int
riscv_return_value_location_lp64ifd(int fp,Dwarf_Die * functypedie,const Dwarf_Op ** locp)128 riscv_return_value_location_lp64ifd (int fp, Dwarf_Die *functypedie,
129 const Dwarf_Op **locp)
130 {
131 /* Start with the function's type, and get the DW_AT_type attribute,
132 which is the type of the return value. */
133 Dwarf_Die typedie;
134 int tag = dwarf_peeled_die_type (functypedie, &typedie);
135 if (tag <= 0)
136 return tag;
137
138 Dwarf_Word size = (Dwarf_Word)-1;
139
140 /* If the argument type is a Composite Type that is larger than 16
141 bytes, then the argument is copied to memory allocated by the
142 caller and the argument is replaced by a pointer to the copy. */
143 if (tag == DW_TAG_structure_type || tag == DW_TAG_union_type
144 || tag == DW_TAG_class_type || tag == DW_TAG_array_type)
145 {
146 Dwarf_Die arg0, arg1;
147
148 if (dwarf_aggregate_size (&typedie, &size) < 0)
149 return -1;
150 /* A struct containing just one floating-point real is passed as though
151 it were a standalone floating-point real. A struct containing two
152 floating-point reals is passed in two floating-point registers, if
153 neither is more than FLEN bits wide. A struct containing just one
154 complex floating-point number is passed as though it were a struct
155 containing two floating-point reals. A struct containing one
156 floating-point real and one integer (or bitfield), in either order,
157 is passed in a floating-point register and an integer register,
158 provided the floating-point real is no more than FLEN bits wide and
159 the integer is no more than XLEN bits wide. */
160 if (tag == DW_TAG_structure_type
161 && flatten_aggregate_arg (&typedie, &arg0, &arg1))
162 return pass_by_flattened_arg (locp, size, &arg0, &arg1);
163 /* Aggregates larger than 2*XLEN bits are passed by reference. */
164 else if (size > 16)
165 return pass_by_ref (locp);
166 /* Aggregates whose total size is no more than XLEN bits are passed in
167 a register. Aggregates whose total size is no more than 2*XLEN bits
168 are passed in a pair of registers. */
169 else
170 return pass_in_gpr_lp64 (locp, size);
171 }
172
173 if (tag == DW_TAG_base_type
174 || tag == DW_TAG_pointer_type || tag == DW_TAG_ptr_to_member_type)
175 {
176 if (dwarf_bytesize_aux (&typedie, &size) < 0)
177 {
178 if (tag == DW_TAG_pointer_type || tag == DW_TAG_ptr_to_member_type)
179 size = 8;
180 else
181 return -1;
182 }
183
184 Dwarf_Attribute attr_mem;
185 if (tag == DW_TAG_base_type)
186 {
187 Dwarf_Word encoding;
188 if (dwarf_formudata (dwarf_attr_integrate (&typedie, DW_AT_encoding,
189 &attr_mem),
190 &encoding) != 0)
191 return -1;
192
193 switch (encoding)
194 {
195 case DW_ATE_boolean:
196 case DW_ATE_signed:
197 case DW_ATE_unsigned:
198 case DW_ATE_unsigned_char:
199 case DW_ATE_signed_char:
200 /* Scalars that are at most XLEN bits wide are passed in a single
201 argument register. Scalars that are 2*XLEN bits wide are
202 passed in a pair of argument registers. Scalars wider than
203 2*XLEN are passed by reference; there are none for LP64D. */
204 return pass_in_gpr_lp64 (locp, size);
205
206 case DW_ATE_float:
207 /* A real floating-point argument is passed in a floating-point
208 argument register if it is no more than FLEN bits wide,
209 otherwise it is passed according to the integer calling
210 convention. */
211 switch (size)
212 {
213 case 4: /* single */
214 switch (fp)
215 {
216 case EF_RISCV_FLOAT_ABI_DOUBLE:
217 case EF_RISCV_FLOAT_ABI_SINGLE:
218 return pass_in_fpr_lp64d (locp, size);
219 case EF_RISCV_FLOAT_ABI_SOFT:
220 return pass_in_gpr_lp64 (locp, size);
221 default:
222 return -2;
223 }
224 case 8: /* double */
225 switch (fp)
226 {
227 case EF_RISCV_FLOAT_ABI_DOUBLE:
228 return pass_in_fpr_lp64d (locp, size);
229 case EF_RISCV_FLOAT_ABI_SINGLE:
230 case EF_RISCV_FLOAT_ABI_SOFT:
231 return pass_in_gpr_lp64 (locp, size);
232 default:
233 return -2;
234 }
235
236 case 16: /* quad */
237 return pass_in_gpr_lp64 (locp, size);
238
239 default:
240 return -2;
241 }
242
243 case DW_ATE_complex_float:
244 /* A complex floating-point number is passed as though it were a
245 struct containing two floating-point reals. */
246 switch (size)
247 {
248 case 8: /* float _Complex */
249 switch (fp)
250 {
251 case EF_RISCV_FLOAT_ABI_DOUBLE:
252 case EF_RISCV_FLOAT_ABI_SINGLE:
253 return pass_in_fpr_lp64f (locp, size);
254 case EF_RISCV_FLOAT_ABI_SOFT:
255 /* Double the size so the vals are two registers. */
256 return pass_in_gpr_lp64 (locp, size * 2);
257 default:
258 return -2;
259 }
260
261 case 16: /* double _Complex */
262 switch (fp)
263 {
264 case EF_RISCV_FLOAT_ABI_DOUBLE:
265 return pass_in_fpr_lp64d (locp, size);
266 case EF_RISCV_FLOAT_ABI_SINGLE:
267 case EF_RISCV_FLOAT_ABI_SOFT:
268 return pass_in_gpr_lp64 (locp, size);
269 default:
270 return -2;
271 }
272
273 case 32: /* long double _Complex */
274 return pass_by_ref (locp);
275
276 default:
277 return -2;
278 }
279 }
280
281 return -2;
282 }
283 else
284 return pass_in_gpr_lp64 (locp, size);
285 }
286
287 *locp = NULL;
288 return 0;
289 }
290
291 int
riscv_return_value_location_lp64d(Dwarf_Die * functypedie,const Dwarf_Op ** locp)292 riscv_return_value_location_lp64d (Dwarf_Die *functypedie,
293 const Dwarf_Op **locp)
294 {
295 return riscv_return_value_location_lp64ifd (EF_RISCV_FLOAT_ABI_DOUBLE,
296 functypedie, locp);
297 }
298
299 int
riscv_return_value_location_lp64f(Dwarf_Die * functypedie,const Dwarf_Op ** locp)300 riscv_return_value_location_lp64f (Dwarf_Die *functypedie,
301 const Dwarf_Op **locp)
302 {
303 return riscv_return_value_location_lp64ifd (EF_RISCV_FLOAT_ABI_SINGLE,
304 functypedie, locp);
305 }
306
307 int
riscv_return_value_location_lp64(Dwarf_Die * functypedie,const Dwarf_Op ** locp)308 riscv_return_value_location_lp64 (Dwarf_Die *functypedie,
309 const Dwarf_Op **locp)
310 {
311 return riscv_return_value_location_lp64ifd (EF_RISCV_FLOAT_ABI_SOFT,
312 functypedie, locp);
313 }
314