summaryrefslogtreecommitdiffstats
path: root/src/backends/aarch64_retval.c
blob: 68de307e17110a9958e309933625e7d939bc74e5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
/* Function return value location for Linux/AArch64 ABI.
   Copyright (C) 2013 Red Hat, Inc.
   This file is part of elfutils.

   This file is free software; you can redistribute it and/or modify
   it under the terms of either

     * the GNU Lesser General Public License as published by the Free
       Software Foundation; either version 3 of the License, or (at
       your option) any later version

   or

     * the GNU General Public License as published by the Free
       Software Foundation; either version 2 of the License, or (at
       your option) any later version

   or both in parallel, as here.

   elfutils is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should have received copies of the GNU General Public License and
   the GNU Lesser General Public License along with this program.  If
   not, see <http://www.gnu.org/licenses/>.  */

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include <stdio.h>
#include <inttypes.h>

#include <assert.h>
#include <dwarf.h>

#define BACKEND aarch64_
#include "libebl_CPU.h"

static int
skip_until (Dwarf_Die *child, int tag)
{
  int i;
  while (DWARF_TAG_OR_RETURN (child) != tag)
    if ((i = dwarf_siblingof (child, child)) != 0)
      /* If there are no members, then this is not a HFA.  Errors
	 are propagated.  */
      return i;
  return 0;
}

static int
dwarf_bytesize_aux (Dwarf_Die *die, Dwarf_Word *sizep)
{
  int bits;
  if (((bits = 8 * dwarf_bytesize (die)) < 0
       && (bits = dwarf_bitsize (die)) < 0)
      || bits % 8 != 0)
    return -1;

  *sizep = bits / 8;
  return 0;
}

/* HFA (Homogeneous Floating-point Aggregate) is an aggregate type
   whose members are all of the same floating-point type, which is
   then base type of this HFA.  Instead of being floating-point types
   directly, members can instead themselves be HFA.  Such HFA fields
   are handled as if their type were HFA base type.

   This function returns 0 if TYPEDIE is HFA, 1 if it is not, or -1 if
   there were errors.  In the former case, *SIZEP contains byte size
   of the base type (e.g. 8 for IEEE double).  *COUNT is set to the
   number of leaf members of the HFA.  */
static int hfa_type (Dwarf_Die *ftypedie, int tag,
		     Dwarf_Word *sizep, Dwarf_Word *countp);

/* Return 0 if MEMBDIE refers to a member with a floating-point or HFA
   type, or 1 if it's not.  Return -1 for errors.  The meaning of the
   remaining arguments is as documented at hfa_type.  */
static int
member_is_fp (Dwarf_Die *membdie, Dwarf_Word *sizep, Dwarf_Word *countp)
{
  Dwarf_Die typedie;
  int tag = dwarf_peeled_die_type (membdie, &typedie);
  switch (tag)
    {
    case DW_TAG_base_type:;
      Dwarf_Word encoding;
      Dwarf_Attribute attr_mem;
      if (dwarf_attr_integrate (&typedie, DW_AT_encoding, &attr_mem) == NULL
	  || dwarf_formudata (&attr_mem, &encoding) != 0)
	return -1;

      switch (encoding)
	{
	case DW_ATE_complex_float:
	  *countp = 2;
	  break;

	case DW_ATE_float:
	  *countp = 1;
	  break;

	default:
	  return 1;
	}

      if (dwarf_bytesize_aux (&typedie, sizep) < 0)
	return -1;

      *sizep /= *countp;
      return 0;

    case DW_TAG_structure_type:
    case DW_TAG_union_type:
    case DW_TAG_array_type:
      return hfa_type (&typedie, tag, sizep, countp);
    }

  return 1;
}

static int
hfa_type (Dwarf_Die *ftypedie, int tag, Dwarf_Word *sizep, Dwarf_Word *countp)
{
  assert (tag == DW_TAG_structure_type || tag == DW_TAG_class_type
	  || tag == DW_TAG_union_type || tag == DW_TAG_array_type);

  int i;
  if (tag == DW_TAG_array_type)
    {
      Dwarf_Word tot_size;
      if (dwarf_aggregate_size (ftypedie, &tot_size) < 0)
	return -1;

      /* For vector types, we don't care about the underlying
	 type, but only about the vector type itself.  */
      bool vec;
      Dwarf_Attribute attr_mem;
      if (dwarf_formflag (dwarf_attr_integrate (ftypedie, DW_AT_GNU_vector,
						&attr_mem), &vec) == 0
	  && vec)
	{
	  *sizep = tot_size;
	  *countp = 1;

	  return 0;
	}

      if ((i = member_is_fp (ftypedie, sizep, countp)) == 0)
	{
	  *countp = tot_size / *sizep;
	  return 0;
	}

      return i;
    }

  /* Find first DW_TAG_member and determine its type.  */
  Dwarf_Die member;
  if ((i = dwarf_child (ftypedie, &member) != 0))
    return i;

  if ((i = skip_until (&member, DW_TAG_member)) != 0)
    return i;

  *countp = 0;
  if ((i = member_is_fp (&member, sizep, countp)) != 0)
    return i;

  while ((i = dwarf_siblingof (&member, &member)) == 0
	 && (i = skip_until (&member, DW_TAG_member)) == 0)
    {
      Dwarf_Word size, count;
      if ((i = member_is_fp (&member, &size, &count)) != 0)
	return i;

      if (*sizep != size)
	return 1;

      *countp += count;
    }

  /* At this point we already have at least one FP member, which means
     FTYPEDIE is an HFA.  So either return 0, or propagate error.  */
  return i < 0 ? i : 0;
}

static int
pass_in_gpr (const Dwarf_Op **locp, Dwarf_Word size)
{
  static const Dwarf_Op loc[] =
    {
      { .atom = DW_OP_reg0 }, { .atom = DW_OP_piece, .number = 8 },
      { .atom = DW_OP_reg1 }, { .atom = DW_OP_piece, .number = 8 }
    };

  *locp = loc;
  return size <= 8 ? 1 : 4;
}

static int
pass_by_ref (const Dwarf_Op **locp)
{
  static const Dwarf_Op loc[] = { { .atom = DW_OP_breg0 } };

  *locp = loc;
  return 1;
}

static int
pass_hfa (const Dwarf_Op **locp, Dwarf_Word size, Dwarf_Word count)
{
  assert (count >= 1 && count <= 4);
  assert (size == 2 || size == 4 || size == 8 || size == 16);

#define DEFINE_FPREG(NAME, SIZE)		\
  static const Dwarf_Op NAME[] = {		\
    { .atom = DW_OP_regx, .number = 64 },	\
    { .atom = DW_OP_piece, .number = SIZE },	\
    { .atom = DW_OP_regx, .number = 65 },	\
    { .atom = DW_OP_piece, .number = SIZE },	\
    { .atom = DW_OP_regx, .number = 66 },	\
    { .atom = DW_OP_piece, .number = SIZE },	\
    { .atom = DW_OP_regx, .number = 67 },	\
    { .atom = DW_OP_piece, .number = SIZE }	\
  }

  switch (size)
    {
    case 2:;
      DEFINE_FPREG (loc_hfa_2, 2);
      *locp = loc_hfa_2;
      break;

    case 4:;
      DEFINE_FPREG (loc_hfa_4, 4);
      *locp = loc_hfa_4;
      break;

    case 8:;
      DEFINE_FPREG (loc_hfa_8, 8);
      *locp = loc_hfa_8;
      break;

    case 16:;
      DEFINE_FPREG (loc_hfa_16, 16);
      *locp = loc_hfa_16;
      break;
    }
#undef DEFINE_FPREG

  return count == 1 ? 1 : 2 * count;
}

static int
pass_in_simd (const Dwarf_Op **locp)
{
  /* This is like passing single-element HFA.  Size doesn't matter, so
     pretend it's for example double.  */
  return pass_hfa (locp, 8, 1);
}

int
aarch64_return_value_location (Dwarf_Die *functypedie, const Dwarf_Op **locp)
{
  /* Start with the function's type, and get the DW_AT_type attribute,
     which is the type of the return value.  */
  Dwarf_Die typedie;
  int tag = dwarf_peeled_die_type (functypedie, &typedie);
  if (tag <= 0)
    return tag;

  Dwarf_Word size = (Dwarf_Word)-1;

  /* If the argument type is a Composite Type that is larger than 16
     bytes, then the argument is copied to memory allocated by the
     caller and the argument is replaced by a pointer to the copy.  */
  if (tag == DW_TAG_structure_type || tag == DW_TAG_union_type
      || tag == DW_TAG_class_type || tag == DW_TAG_array_type)
    {
      Dwarf_Word base_size, count;
      switch (hfa_type (&typedie, tag, &base_size, &count))
	{
	default:
	  return -1;

	case 0:
	  assert (count > 0);
	  if (count <= 4)
	    return pass_hfa (locp, base_size, count);
	  /* Fall through.  */

	case 1:
	  /* Not a HFA.  */
	  if (dwarf_aggregate_size (&typedie, &size) < 0)
	    return -1;
	  if (size > 16)
	    return pass_by_ref (locp);
	}
    }

  if (tag == DW_TAG_base_type
      || tag == DW_TAG_pointer_type || tag == DW_TAG_ptr_to_member_type)
    {
      if (dwarf_bytesize_aux (&typedie, &size) < 0)
	{
	  if (tag == DW_TAG_pointer_type || tag == DW_TAG_ptr_to_member_type)
	    size = 8;
	  else
	    return -1;
	}

      Dwarf_Attribute attr_mem;
      if (tag == DW_TAG_base_type)
	{
	  Dwarf_Word encoding;
	  if (dwarf_formudata (dwarf_attr_integrate (&typedie, DW_AT_encoding,
						     &attr_mem),
			       &encoding) != 0)
	    return -1;

	  switch (encoding)
	    {
	      /* If the argument is a Half-, Single-, Double- or Quad-
		 precision Floating-point [...] the argument is allocated
		 to the least significant bits of register v[NSRN].  */
	    case DW_ATE_float:
	      switch (size)
		{
		case 2: /* half */
		case 4: /* sigle */
		case 8: /* double */
		case 16: /* quad */
		  return pass_in_simd (locp);

		default:
		  return -2;
		}

	    case DW_ATE_complex_float:
	      switch (size)
		{
		case 8: /* float _Complex */
		case 16: /* double _Complex */
		case 32: /* long double _Complex */
		  return pass_hfa (locp, size / 2, 2);

		default:
		  return -2;
		}

	      /* If the argument is an Integral or Pointer Type, the
		 size of the argument is less than or equal to 8 bytes
		 [...] the argument is copied to the least significant
		 bits in x[NGRN].  */
	    case DW_ATE_boolean:
	    case DW_ATE_signed:
	    case DW_ATE_unsigned:
	    case DW_ATE_unsigned_char:
	    case DW_ATE_signed_char:
	      return pass_in_gpr (locp, size);
	    }

	  return -2;
	}
      else
	return pass_in_gpr (locp, size);
    }

  *locp = NULL;
  return 0;
}