xref: /aosp_15_r20/external/elfutils/backends/riscv_retval.c (revision 7304104da70ce23c86437a01be71edd1a2d7f37e)
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 || dwarf_is_pointer (tag))
174     {
175       if (dwarf_bytesize_aux (&typedie, &size) < 0)
176 	{
177 	  if (dwarf_is_pointer (tag))
178 	    size = 8;
179 	  else
180 	    return -1;
181 	}
182 
183       Dwarf_Attribute attr_mem;
184       if (tag == DW_TAG_base_type)
185 	{
186 	  Dwarf_Word encoding;
187 	  if (dwarf_formudata (dwarf_attr_integrate (&typedie, DW_AT_encoding,
188 						     &attr_mem),
189 			       &encoding) != 0)
190 	    return -1;
191 
192 	  switch (encoding)
193 	    {
194 	    case DW_ATE_boolean:
195 	    case DW_ATE_signed:
196 	    case DW_ATE_unsigned:
197 	    case DW_ATE_unsigned_char:
198 	    case DW_ATE_signed_char:
199 	      /* Scalars that are at most XLEN bits wide are passed in a single
200 		 argument register.  Scalars that are 2*XLEN bits wide are
201 		 passed in a pair of argument registers.  Scalars wider than
202 		 2*XLEN are passed by reference; there are none for LP64D.  */
203 	      return pass_in_gpr_lp64 (locp, size);
204 
205 	    case DW_ATE_float:
206 	      /* A real floating-point argument is passed in a floating-point
207 		 argument register if it is no more than FLEN bits wide,
208 		 otherwise it is passed according to the integer calling
209 		 convention.  */
210 	      switch (size)
211 		{
212 		case 4: /* single */
213                   switch (fp)
214                     {
215                     case EF_RISCV_FLOAT_ABI_DOUBLE:
216                     case EF_RISCV_FLOAT_ABI_SINGLE:
217                       return pass_in_fpr_lp64d (locp, size);
218                     case EF_RISCV_FLOAT_ABI_SOFT:
219                       return pass_in_gpr_lp64 (locp, size);
220                     default:
221                       return -2;
222                     }
223                 case 8: /* double */
224                   switch (fp)
225                     {
226                     case EF_RISCV_FLOAT_ABI_DOUBLE:
227                       return pass_in_fpr_lp64d (locp, size);
228                     case EF_RISCV_FLOAT_ABI_SINGLE:
229                     case EF_RISCV_FLOAT_ABI_SOFT:
230                       return pass_in_gpr_lp64 (locp, size);
231                     default:
232                       return -2;
233                     }
234 
235                 case 16: /* quad */
236 		  return pass_in_gpr_lp64 (locp, size);
237 
238 		default:
239 		  return -2;
240 		}
241 
242 	    case DW_ATE_complex_float:
243 	      /* A complex floating-point number is passed as though it were a
244 		 struct containing two floating-point reals.  */
245 	      switch (size)
246 		{
247 		case 8: /* float _Complex */
248                   switch (fp)
249                     {
250                     case EF_RISCV_FLOAT_ABI_DOUBLE:
251                     case EF_RISCV_FLOAT_ABI_SINGLE:
252                       return pass_in_fpr_lp64f (locp, size);
253                     case EF_RISCV_FLOAT_ABI_SOFT:
254                       /* Double the size so the vals are two registers. */
255                       return pass_in_gpr_lp64 (locp, size * 2);
256                     default:
257                       return -2;
258                     }
259 
260                 case 16: /* double _Complex */
261                   switch (fp)
262                     {
263                     case EF_RISCV_FLOAT_ABI_DOUBLE:
264                       return pass_in_fpr_lp64d (locp, size);
265                     case EF_RISCV_FLOAT_ABI_SINGLE:
266                     case EF_RISCV_FLOAT_ABI_SOFT:
267                       return pass_in_gpr_lp64 (locp, size);
268                     default:
269                       return -2;
270                     }
271 
272                 case 32: /* long double _Complex */
273 		  return pass_by_ref (locp);
274 
275 		default:
276 		  return -2;
277 		}
278 	    }
279 
280 	  return -2;
281 	}
282       else
283 	return pass_in_gpr_lp64 (locp, size);
284     }
285 
286   *locp = NULL;
287   return 0;
288 }
289 
290 int
riscv_return_value_location_lp64d(Dwarf_Die * functypedie,const Dwarf_Op ** locp)291 riscv_return_value_location_lp64d (Dwarf_Die *functypedie,
292                                    const Dwarf_Op **locp)
293 {
294   return riscv_return_value_location_lp64ifd (EF_RISCV_FLOAT_ABI_DOUBLE,
295                                               functypedie, locp);
296 }
297 
298 int
riscv_return_value_location_lp64f(Dwarf_Die * functypedie,const Dwarf_Op ** locp)299 riscv_return_value_location_lp64f (Dwarf_Die *functypedie,
300                                    const Dwarf_Op **locp)
301 {
302   return riscv_return_value_location_lp64ifd (EF_RISCV_FLOAT_ABI_SINGLE,
303                                               functypedie, locp);
304 }
305 
306 int
riscv_return_value_location_lp64(Dwarf_Die * functypedie,const Dwarf_Op ** locp)307 riscv_return_value_location_lp64 (Dwarf_Die *functypedie,
308                                   const Dwarf_Op **locp)
309 {
310   return riscv_return_value_location_lp64ifd (EF_RISCV_FLOAT_ABI_SOFT,
311                                               functypedie, locp);
312 }
313