Automatic date update in version.in
[deliverable/binutils-gdb.git] / gdb / ppc-sysv-tdep.c
CommitLineData
7b112f9c
JT
1/* Target-dependent code for PowerPC systems using the SVR4 ABI
2 for GDB, the GNU debugger.
3
32d0add0 4 Copyright (C) 2000-2015 Free Software Foundation, Inc.
7b112f9c
JT
5
6 This file is part of GDB.
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
a9762ec7 10 the Free Software Foundation; either version 3 of the License, or
7b112f9c
JT
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
a9762ec7 19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
7b112f9c
JT
20
21#include "defs.h"
22#include "gdbcore.h"
23#include "inferior.h"
24#include "regcache.h"
25#include "value.h"
7b112f9c 26#include "ppc-tdep.h"
6066c3de 27#include "target.h"
0a90bcdd 28#include "objfiles.h"
7d9b040b 29#include "infcall.h"
54fcddd0 30#include "dwarf2.h"
7b112f9c 31
88aed45e
UW
32
33/* Check whether FTPYE is a (pointer to) function type that should use
34 the OpenCL vector ABI. */
35
36static int
37ppc_sysv_use_opencl_abi (struct type *ftype)
38{
39 ftype = check_typedef (ftype);
40
41 if (TYPE_CODE (ftype) == TYPE_CODE_PTR)
42 ftype = check_typedef (TYPE_TARGET_TYPE (ftype));
43
44 return (TYPE_CODE (ftype) == TYPE_CODE_FUNC
45 && TYPE_CALLING_CONVENTION (ftype) == DW_CC_GDB_IBM_OpenCL);
46}
47
0df8b418 48/* Pass the arguments in either registers, or in the stack. Using the
7b112f9c
JT
49 ppc sysv ABI, the first eight words of the argument list (that might
50 be less than eight parameters if some parameters occupy more than one
51 word) are passed in r3..r10 registers. float and double parameters are
0df8b418
MS
52 passed in fpr's, in addition to that. Rest of the parameters if any
53 are passed in user stack.
7b112f9c
JT
54
55 If the function is returning a structure, then the return address is passed
56 in r3, then the first 7 words of the parametes can be passed in registers,
0df8b418 57 starting from r4. */
7b112f9c
JT
58
59CORE_ADDR
7d9b040b 60ppc_sysv_abi_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
77b2b6d4
AC
61 struct regcache *regcache, CORE_ADDR bp_addr,
62 int nargs, struct value **args, CORE_ADDR sp,
63 int struct_return, CORE_ADDR struct_addr)
7b112f9c 64{
40a6adc1 65 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
e17a4113 66 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
88aed45e 67 int opencl_abi = ppc_sysv_use_opencl_abi (value_type (function));
fb4443d8 68 ULONGEST saved_sp;
68856ea3
AC
69 int argspace = 0; /* 0 is an initial wrong guess. */
70 int write_pass;
7b112f9c 71
b14d30e1
JM
72 gdb_assert (tdep->wordsize == 4);
73
40a6adc1 74 regcache_cooked_read_unsigned (regcache, gdbarch_sp_regnum (gdbarch),
3e8c568d 75 &saved_sp);
fb4443d8 76
68856ea3 77 /* Go through the argument list twice.
7b112f9c 78
68856ea3
AC
79 Pass 1: Figure out how much new stack space is required for
80 arguments and pushed values. Unlike the PowerOpen ABI, the SysV
81 ABI doesn't reserve any extra space for parameters which are put
82 in registers, but does always push structures and then pass their
83 address.
7a41266b 84
68856ea3
AC
85 Pass 2: Replay the same computation but this time also write the
86 values out to the target. */
7b112f9c 87
68856ea3
AC
88 for (write_pass = 0; write_pass < 2; write_pass++)
89 {
90 int argno;
91 /* Next available floating point register for float and double
92 arguments. */
93 int freg = 1;
94 /* Next available general register for non-float, non-vector
95 arguments. */
96 int greg = 3;
97 /* Next available vector register for vector arguments. */
98 int vreg = 2;
99 /* Arguments start above the "LR save word" and "Back chain". */
100 int argoffset = 2 * tdep->wordsize;
101 /* Structures start after the arguments. */
102 int structoffset = argoffset + argspace;
103
104 /* If the function is returning a `struct', then the first word
944fcfab
AC
105 (which will be passed in r3) is used for struct return
106 address. In that case we should advance one word and start
107 from r4 register to copy parameters. */
68856ea3 108 if (struct_return)
7b112f9c 109 {
68856ea3
AC
110 if (write_pass)
111 regcache_cooked_write_signed (regcache,
112 tdep->ppc_gp0_regnum + greg,
113 struct_addr);
114 greg++;
7b112f9c 115 }
68856ea3
AC
116
117 for (argno = 0; argno < nargs; argno++)
7b112f9c 118 {
68856ea3 119 struct value *arg = args[argno];
df407dfe 120 struct type *type = check_typedef (value_type (arg));
68856ea3 121 int len = TYPE_LENGTH (type);
0fd88904 122 const bfd_byte *val = value_contents (arg);
68856ea3 123
55eddb0f
DJ
124 if (TYPE_CODE (type) == TYPE_CODE_FLT && len <= 8
125 && !tdep->soft_float)
7b112f9c 126 {
68856ea3 127 /* Floating point value converted to "double" then
944fcfab
AC
128 passed in an FP register, when the registers run out,
129 8 byte aligned stack is used. */
68856ea3
AC
130 if (freg <= 8)
131 {
132 if (write_pass)
133 {
134 /* Always store the floating point value using
944fcfab 135 the register's floating-point format. */
50fd1280 136 gdb_byte regval[MAX_REGISTER_SIZE];
68856ea3 137 struct type *regtype
366f009f 138 = register_type (gdbarch, tdep->ppc_fp0_regnum + freg);
68856ea3 139 convert_typed_floating (val, type, regval, regtype);
366f009f
JB
140 regcache_cooked_write (regcache,
141 tdep->ppc_fp0_regnum + freg,
68856ea3
AC
142 regval);
143 }
144 freg++;
145 }
7b112f9c
JT
146 else
147 {
f964a756
MK
148 /* The SysV ABI tells us to convert floats to
149 doubles before writing them to an 8 byte aligned
150 stack location. Unfortunately GCC does not do
151 that, and stores floats into 4 byte aligned
152 locations without converting them to doubles.
153 Since there is no know compiler that actually
154 follows the ABI here, we implement the GCC
155 convention. */
156
157 /* Align to 4 bytes or 8 bytes depending on the type of
158 the argument (float or double). */
159 argoffset = align_up (argoffset, len);
68856ea3 160 if (write_pass)
68856ea3 161 write_memory (sp + argoffset, val, len);
f964a756 162 argoffset += len;
7b112f9c
JT
163 }
164 }
b14d30e1
JM
165 else if (TYPE_CODE (type) == TYPE_CODE_FLT
166 && len == 16
167 && !tdep->soft_float
40a6adc1 168 && (gdbarch_long_double_format (gdbarch)
b14d30e1
JM
169 == floatformats_ibm_long_double))
170 {
171 /* IBM long double passed in two FP registers if
172 available, otherwise 8-byte aligned stack. */
173 if (freg <= 7)
174 {
175 if (write_pass)
176 {
177 regcache_cooked_write (regcache,
178 tdep->ppc_fp0_regnum + freg,
179 val);
180 regcache_cooked_write (regcache,
181 tdep->ppc_fp0_regnum + freg + 1,
182 val + 8);
183 }
184 freg += 2;
185 }
186 else
187 {
188 argoffset = align_up (argoffset, 8);
189 if (write_pass)
190 write_memory (sp + argoffset, val, len);
191 argoffset += 16;
192 }
193 }
55eddb0f
DJ
194 else if (len == 8
195 && (TYPE_CODE (type) == TYPE_CODE_INT /* long long */
00fbcec4
JM
196 || TYPE_CODE (type) == TYPE_CODE_FLT /* double */
197 || (TYPE_CODE (type) == TYPE_CODE_DECFLOAT
198 && tdep->soft_float)))
7b112f9c 199 {
00fbcec4
JM
200 /* "long long" or soft-float "double" or "_Decimal64"
201 passed in an odd/even register pair with the low
202 addressed word in the odd register and the high
203 addressed word in the even register, or when the
204 registers run out an 8 byte aligned stack
205 location. */
68856ea3
AC
206 if (greg > 9)
207 {
208 /* Just in case GREG was 10. */
209 greg = 11;
210 argoffset = align_up (argoffset, 8);
211 if (write_pass)
212 write_memory (sp + argoffset, val, len);
213 argoffset += 8;
214 }
68856ea3
AC
215 else
216 {
217 /* Must start on an odd register - r3/r4 etc. */
218 if ((greg & 1) == 0)
219 greg++;
220 if (write_pass)
221 {
222 regcache_cooked_write (regcache,
223 tdep->ppc_gp0_regnum + greg + 0,
224 val + 0);
225 regcache_cooked_write (regcache,
226 tdep->ppc_gp0_regnum + greg + 1,
227 val + 4);
228 }
229 greg += 2;
230 }
7b112f9c 231 }
00fbcec4
JM
232 else if (len == 16
233 && ((TYPE_CODE (type) == TYPE_CODE_FLT
234 && (gdbarch_long_double_format (gdbarch)
235 == floatformats_ibm_long_double))
236 || (TYPE_CODE (type) == TYPE_CODE_DECFLOAT
237 && tdep->soft_float)))
b14d30e1 238 {
00fbcec4
JM
239 /* Soft-float IBM long double or _Decimal128 passed in
240 four consecutive registers, or on the stack. The
241 registers are not necessarily odd/even pairs. */
b14d30e1
JM
242 if (greg > 7)
243 {
244 greg = 11;
245 argoffset = align_up (argoffset, 8);
246 if (write_pass)
247 write_memory (sp + argoffset, val, len);
248 argoffset += 16;
249 }
250 else
251 {
252 if (write_pass)
253 {
254 regcache_cooked_write (regcache,
255 tdep->ppc_gp0_regnum + greg + 0,
256 val + 0);
257 regcache_cooked_write (regcache,
258 tdep->ppc_gp0_regnum + greg + 1,
259 val + 4);
260 regcache_cooked_write (regcache,
261 tdep->ppc_gp0_regnum + greg + 2,
262 val + 8);
263 regcache_cooked_write (regcache,
264 tdep->ppc_gp0_regnum + greg + 3,
265 val + 12);
266 }
267 greg += 4;
268 }
269 }
1300a2f4
TJB
270 else if (TYPE_CODE (type) == TYPE_CODE_DECFLOAT && len <= 8
271 && !tdep->soft_float)
272 {
273 /* 32-bit and 64-bit decimal floats go in f1 .. f8. They can
274 end up in memory. */
275
276 if (freg <= 8)
277 {
278 if (write_pass)
279 {
280 gdb_byte regval[MAX_REGISTER_SIZE];
281 const gdb_byte *p;
282
283 /* 32-bit decimal floats are right aligned in the
284 doubleword. */
285 if (TYPE_LENGTH (type) == 4)
286 {
287 memcpy (regval + 4, val, 4);
288 p = regval;
289 }
290 else
291 p = val;
292
293 regcache_cooked_write (regcache,
294 tdep->ppc_fp0_regnum + freg, p);
295 }
296
297 freg++;
298 }
299 else
300 {
301 argoffset = align_up (argoffset, len);
302
303 if (write_pass)
304 /* Write value in the stack's parameter save area. */
305 write_memory (sp + argoffset, val, len);
306
307 argoffset += len;
308 }
309 }
310 else if (TYPE_CODE (type) == TYPE_CODE_DECFLOAT && len == 16
311 && !tdep->soft_float)
312 {
313 /* 128-bit decimal floats go in f2 .. f7, always in even/odd
314 pairs. They can end up in memory, using two doublewords. */
315
316 if (freg <= 6)
317 {
318 /* Make sure freg is even. */
319 freg += freg & 1;
320
321 if (write_pass)
322 {
323 regcache_cooked_write (regcache,
324 tdep->ppc_fp0_regnum + freg, val);
325 regcache_cooked_write (regcache,
326 tdep->ppc_fp0_regnum + freg + 1, val + 8);
327 }
328 }
329 else
330 {
331 argoffset = align_up (argoffset, 8);
332
333 if (write_pass)
334 write_memory (sp + argoffset, val, 16);
335
336 argoffset += 16;
337 }
338
339 /* If a 128-bit decimal float goes to the stack because only f7
340 and f8 are free (thus there's no even/odd register pair
341 available), these registers should be marked as occupied.
342 Hence we increase freg even when writing to memory. */
343 freg += 2;
344 }
54fcddd0
UW
345 else if (len < 16
346 && TYPE_CODE (type) == TYPE_CODE_ARRAY
347 && TYPE_VECTOR (type)
348 && opencl_abi)
349 {
350 /* OpenCL vectors shorter than 16 bytes are passed as if
351 a series of independent scalars. */
352 struct type *eltype = check_typedef (TYPE_TARGET_TYPE (type));
353 int i, nelt = TYPE_LENGTH (type) / TYPE_LENGTH (eltype);
354
355 for (i = 0; i < nelt; i++)
356 {
357 const gdb_byte *elval = val + i * TYPE_LENGTH (eltype);
358
359 if (TYPE_CODE (eltype) == TYPE_CODE_FLT && !tdep->soft_float)
360 {
361 if (freg <= 8)
362 {
363 if (write_pass)
364 {
365 int regnum = tdep->ppc_fp0_regnum + freg;
366 gdb_byte regval[MAX_REGISTER_SIZE];
367 struct type *regtype
368 = register_type (gdbarch, regnum);
369 convert_typed_floating (elval, eltype,
370 regval, regtype);
371 regcache_cooked_write (regcache, regnum, regval);
372 }
373 freg++;
374 }
375 else
376 {
377 argoffset = align_up (argoffset, len);
378 if (write_pass)
379 write_memory (sp + argoffset, val, len);
380 argoffset += len;
381 }
382 }
383 else if (TYPE_LENGTH (eltype) == 8)
384 {
385 if (greg > 9)
386 {
387 /* Just in case GREG was 10. */
388 greg = 11;
389 argoffset = align_up (argoffset, 8);
390 if (write_pass)
391 write_memory (sp + argoffset, elval,
392 TYPE_LENGTH (eltype));
393 argoffset += 8;
394 }
395 else
396 {
397 /* Must start on an odd register - r3/r4 etc. */
398 if ((greg & 1) == 0)
399 greg++;
400 if (write_pass)
401 {
402 int regnum = tdep->ppc_gp0_regnum + greg;
403 regcache_cooked_write (regcache,
404 regnum + 0, elval + 0);
405 regcache_cooked_write (regcache,
406 regnum + 1, elval + 4);
407 }
408 greg += 2;
409 }
410 }
411 else
412 {
413 gdb_byte word[MAX_REGISTER_SIZE];
414 store_unsigned_integer (word, tdep->wordsize, byte_order,
415 unpack_long (eltype, elval));
416
417 if (greg <= 10)
418 {
419 if (write_pass)
420 regcache_cooked_write (regcache,
421 tdep->ppc_gp0_regnum + greg,
422 word);
423 greg++;
424 }
425 else
426 {
427 argoffset = align_up (argoffset, tdep->wordsize);
428 if (write_pass)
429 write_memory (sp + argoffset, word, tdep->wordsize);
430 argoffset += tdep->wordsize;
431 }
432 }
433 }
434 }
435 else if (len >= 16
436 && TYPE_CODE (type) == TYPE_CODE_ARRAY
437 && TYPE_VECTOR (type)
438 && opencl_abi)
439 {
440 /* OpenCL vectors 16 bytes or longer are passed as if
441 a series of AltiVec vectors. */
442 int i;
443
444 for (i = 0; i < len / 16; i++)
445 {
446 const gdb_byte *elval = val + i * 16;
447
448 if (vreg <= 13)
449 {
450 if (write_pass)
451 regcache_cooked_write (regcache,
452 tdep->ppc_vr0_regnum + vreg,
453 elval);
454 vreg++;
455 }
456 else
457 {
458 argoffset = align_up (argoffset, 16);
459 if (write_pass)
460 write_memory (sp + argoffset, elval, 16);
461 argoffset += 16;
462 }
463 }
464 }
68856ea3
AC
465 else if (len == 16
466 && TYPE_CODE (type) == TYPE_CODE_ARRAY
55eddb0f
DJ
467 && TYPE_VECTOR (type)
468 && tdep->vector_abi == POWERPC_VEC_ALTIVEC)
7b112f9c 469 {
68856ea3 470 /* Vector parameter passed in an Altivec register, or
944fcfab 471 when that runs out, 16 byte aligned stack location. */
7b112f9c
JT
472 if (vreg <= 13)
473 {
68856ea3 474 if (write_pass)
9c9acae0 475 regcache_cooked_write (regcache,
944fcfab 476 tdep->ppc_vr0_regnum + vreg, val);
7b112f9c
JT
477 vreg++;
478 }
479 else
480 {
68856ea3
AC
481 argoffset = align_up (argoffset, 16);
482 if (write_pass)
483 write_memory (sp + argoffset, val, 16);
7b112f9c
JT
484 argoffset += 16;
485 }
486 }
944fcfab 487 else if (len == 8
0a613259 488 && TYPE_CODE (type) == TYPE_CODE_ARRAY
55eddb0f
DJ
489 && TYPE_VECTOR (type)
490 && tdep->vector_abi == POWERPC_VEC_SPE)
944fcfab 491 {
68856ea3 492 /* Vector parameter passed in an e500 register, or when
944fcfab
AC
493 that runs out, 8 byte aligned stack location. Note
494 that since e500 vector and general purpose registers
495 both map onto the same underlying register set, a
496 "greg" and not a "vreg" is consumed here. A cooked
497 write stores the value in the correct locations
498 within the raw register cache. */
499 if (greg <= 10)
500 {
68856ea3 501 if (write_pass)
9c9acae0 502 regcache_cooked_write (regcache,
944fcfab
AC
503 tdep->ppc_ev0_regnum + greg, val);
504 greg++;
505 }
506 else
507 {
68856ea3
AC
508 argoffset = align_up (argoffset, 8);
509 if (write_pass)
510 write_memory (sp + argoffset, val, 8);
944fcfab
AC
511 argoffset += 8;
512 }
513 }
68856ea3
AC
514 else
515 {
516 /* Reduce the parameter down to something that fits in a
944fcfab 517 "word". */
50fd1280 518 gdb_byte word[MAX_REGISTER_SIZE];
68856ea3
AC
519 memset (word, 0, MAX_REGISTER_SIZE);
520 if (len > tdep->wordsize
521 || TYPE_CODE (type) == TYPE_CODE_STRUCT
522 || TYPE_CODE (type) == TYPE_CODE_UNION)
523 {
55eddb0f 524 /* Structs and large values are put in an
0df8b418 525 aligned stack slot ... */
55eddb0f
DJ
526 if (TYPE_CODE (type) == TYPE_CODE_ARRAY
527 && TYPE_VECTOR (type)
528 && len >= 16)
529 structoffset = align_up (structoffset, 16);
530 else
531 structoffset = align_up (structoffset, 8);
532
68856ea3
AC
533 if (write_pass)
534 write_memory (sp + structoffset, val, len);
535 /* ... and then a "word" pointing to that address is
944fcfab 536 passed as the parameter. */
e17a4113 537 store_unsigned_integer (word, tdep->wordsize, byte_order,
68856ea3
AC
538 sp + structoffset);
539 structoffset += len;
540 }
541 else if (TYPE_CODE (type) == TYPE_CODE_INT)
542 /* Sign or zero extend the "int" into a "word". */
e17a4113 543 store_unsigned_integer (word, tdep->wordsize, byte_order,
68856ea3
AC
544 unpack_long (type, val));
545 else
546 /* Always goes in the low address. */
547 memcpy (word, val, len);
548 /* Store that "word" in a register, or on the stack.
944fcfab 549 The words have "4" byte alignment. */
68856ea3
AC
550 if (greg <= 10)
551 {
552 if (write_pass)
553 regcache_cooked_write (regcache,
944fcfab 554 tdep->ppc_gp0_regnum + greg, word);
68856ea3
AC
555 greg++;
556 }
557 else
558 {
559 argoffset = align_up (argoffset, tdep->wordsize);
560 if (write_pass)
561 write_memory (sp + argoffset, word, tdep->wordsize);
562 argoffset += tdep->wordsize;
563 }
564 }
565 }
566
567 /* Compute the actual stack space requirements. */
568 if (!write_pass)
569 {
570 /* Remember the amount of space needed by the arguments. */
571 argspace = argoffset;
572 /* Allocate space for both the arguments and the structures. */
573 sp -= (argoffset + structoffset);
574 /* Ensure that the stack is still 16 byte aligned. */
575 sp = align_down (sp, 16);
576 }
65ada037
MK
577
578 /* The psABI says that "A caller of a function that takes a
579 variable argument list shall set condition register bit 6 to
580 1 if it passes one or more arguments in the floating-point
0df8b418 581 registers. It is strongly recommended that the caller set the
65ada037
MK
582 bit to 0 otherwise..." Doing this for normal functions too
583 shouldn't hurt. */
584 if (write_pass)
585 {
586 ULONGEST cr;
587
588 regcache_cooked_read_unsigned (regcache, tdep->ppc_cr_regnum, &cr);
589 if (freg > 1)
590 cr |= 0x02000000;
591 else
592 cr &= ~0x02000000;
593 regcache_cooked_write_unsigned (regcache, tdep->ppc_cr_regnum, cr);
594 }
7b112f9c
JT
595 }
596
68856ea3 597 /* Update %sp. */
40a6adc1 598 regcache_cooked_write_signed (regcache, gdbarch_sp_regnum (gdbarch), sp);
68856ea3
AC
599
600 /* Write the backchain (it occupies WORDSIZED bytes). */
e17a4113 601 write_memory_signed_integer (sp, tdep->wordsize, byte_order, saved_sp);
68856ea3 602
e56a0ecc
AC
603 /* Point the inferior function call's return address at the dummy's
604 breakpoint. */
68856ea3 605 regcache_cooked_write_signed (regcache, tdep->ppc_lr_regnum, bp_addr);
e56a0ecc 606
7b112f9c
JT
607 return sp;
608}
609
e765b44c 610/* Handle the return-value conventions for Decimal Floating Point values. */
1300a2f4
TJB
611static int
612get_decimal_float_return_value (struct gdbarch *gdbarch, struct type *valtype,
613 struct regcache *regcache, gdb_byte *readbuf,
614 const gdb_byte *writebuf)
615{
616 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
617
618 gdb_assert (TYPE_CODE (valtype) == TYPE_CODE_DECFLOAT);
619
620 /* 32-bit and 64-bit decimal floats in f1. */
621 if (TYPE_LENGTH (valtype) <= 8)
622 {
623 if (writebuf != NULL)
624 {
625 gdb_byte regval[MAX_REGISTER_SIZE];
626 const gdb_byte *p;
627
628 /* 32-bit decimal float is right aligned in the doubleword. */
629 if (TYPE_LENGTH (valtype) == 4)
630 {
631 memcpy (regval + 4, writebuf, 4);
632 p = regval;
633 }
634 else
635 p = writebuf;
636
637 regcache_cooked_write (regcache, tdep->ppc_fp0_regnum + 1, p);
638 }
639 if (readbuf != NULL)
640 {
641 regcache_cooked_read (regcache, tdep->ppc_fp0_regnum + 1, readbuf);
642
643 /* Left align 32-bit decimal float. */
644 if (TYPE_LENGTH (valtype) == 4)
645 memcpy (readbuf, readbuf + 4, 4);
646 }
647 }
648 /* 128-bit decimal floats in f2,f3. */
649 else if (TYPE_LENGTH (valtype) == 16)
650 {
651 if (writebuf != NULL || readbuf != NULL)
652 {
653 int i;
654
655 for (i = 0; i < 2; i++)
656 {
657 if (writebuf != NULL)
658 regcache_cooked_write (regcache, tdep->ppc_fp0_regnum + 2 + i,
659 writebuf + i * 8);
660 if (readbuf != NULL)
661 regcache_cooked_read (regcache, tdep->ppc_fp0_regnum + 2 + i,
662 readbuf + i * 8);
663 }
664 }
665 }
666 else
667 /* Can't happen. */
9b20d036 668 internal_error (__FILE__, __LINE__, _("Unknown decimal float size."));
1300a2f4
TJB
669
670 return RETURN_VALUE_REGISTER_CONVENTION;
671}
672
e754ae69
AC
673/* Handle the return-value conventions specified by the SysV 32-bit
674 PowerPC ABI (including all the supplements):
675
676 no floating-point: floating-point values returned using 32-bit
677 general-purpose registers.
678
679 Altivec: 128-bit vectors returned using vector registers.
680
681 e500: 64-bit vectors returned using the full full 64 bit EV
682 register, floating-point values returned using 32-bit
683 general-purpose registers.
684
685 GCC (broken): Small struct values right (instead of left) aligned
686 when returned in general-purpose registers. */
687
688static enum return_value_convention
54fcddd0
UW
689do_ppc_sysv_return_value (struct gdbarch *gdbarch, struct type *func_type,
690 struct type *type, struct regcache *regcache,
691 gdb_byte *readbuf, const gdb_byte *writebuf,
692 int broken_gcc)
e754ae69 693{
05580c65 694 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
e17a4113 695 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
88aed45e 696 int opencl_abi = func_type? ppc_sysv_use_opencl_abi (func_type) : 0;
54fcddd0 697
e754ae69 698 gdb_assert (tdep->wordsize == 4);
54fcddd0 699
e754ae69
AC
700 if (TYPE_CODE (type) == TYPE_CODE_FLT
701 && TYPE_LENGTH (type) <= 8
55eddb0f 702 && !tdep->soft_float)
e754ae69 703 {
963e2bb7 704 if (readbuf)
e754ae69
AC
705 {
706 /* Floats and doubles stored in "f1". Convert the value to
707 the required type. */
50fd1280 708 gdb_byte regval[MAX_REGISTER_SIZE];
366f009f
JB
709 struct type *regtype = register_type (gdbarch,
710 tdep->ppc_fp0_regnum + 1);
711 regcache_cooked_read (regcache, tdep->ppc_fp0_regnum + 1, regval);
963e2bb7 712 convert_typed_floating (regval, regtype, readbuf, type);
e754ae69 713 }
963e2bb7 714 if (writebuf)
e754ae69
AC
715 {
716 /* Floats and doubles stored in "f1". Convert the value to
717 the register's "double" type. */
50fd1280 718 gdb_byte regval[MAX_REGISTER_SIZE];
366f009f 719 struct type *regtype = register_type (gdbarch, tdep->ppc_fp0_regnum);
963e2bb7 720 convert_typed_floating (writebuf, type, regval, regtype);
366f009f 721 regcache_cooked_write (regcache, tdep->ppc_fp0_regnum + 1, regval);
e754ae69
AC
722 }
723 return RETURN_VALUE_REGISTER_CONVENTION;
724 }
b14d30e1
JM
725 if (TYPE_CODE (type) == TYPE_CODE_FLT
726 && TYPE_LENGTH (type) == 16
727 && !tdep->soft_float
0df8b418
MS
728 && (gdbarch_long_double_format (gdbarch)
729 == floatformats_ibm_long_double))
b14d30e1
JM
730 {
731 /* IBM long double stored in f1 and f2. */
732 if (readbuf)
733 {
734 regcache_cooked_read (regcache, tdep->ppc_fp0_regnum + 1, readbuf);
735 regcache_cooked_read (regcache, tdep->ppc_fp0_regnum + 2,
736 readbuf + 8);
737 }
738 if (writebuf)
739 {
740 regcache_cooked_write (regcache, tdep->ppc_fp0_regnum + 1, writebuf);
741 regcache_cooked_write (regcache, tdep->ppc_fp0_regnum + 2,
742 writebuf + 8);
743 }
744 return RETURN_VALUE_REGISTER_CONVENTION;
745 }
00fbcec4
JM
746 if (TYPE_LENGTH (type) == 16
747 && ((TYPE_CODE (type) == TYPE_CODE_FLT
0df8b418
MS
748 && (gdbarch_long_double_format (gdbarch)
749 == floatformats_ibm_long_double))
00fbcec4 750 || (TYPE_CODE (type) == TYPE_CODE_DECFLOAT && tdep->soft_float)))
b14d30e1 751 {
00fbcec4
JM
752 /* Soft-float IBM long double or _Decimal128 stored in r3, r4,
753 r5, r6. */
b14d30e1
JM
754 if (readbuf)
755 {
756 regcache_cooked_read (regcache, tdep->ppc_gp0_regnum + 3, readbuf);
757 regcache_cooked_read (regcache, tdep->ppc_gp0_regnum + 4,
758 readbuf + 4);
759 regcache_cooked_read (regcache, tdep->ppc_gp0_regnum + 5,
760 readbuf + 8);
761 regcache_cooked_read (regcache, tdep->ppc_gp0_regnum + 6,
762 readbuf + 12);
763 }
764 if (writebuf)
765 {
766 regcache_cooked_write (regcache, tdep->ppc_gp0_regnum + 3, writebuf);
767 regcache_cooked_write (regcache, tdep->ppc_gp0_regnum + 4,
768 writebuf + 4);
769 regcache_cooked_write (regcache, tdep->ppc_gp0_regnum + 5,
770 writebuf + 8);
771 regcache_cooked_write (regcache, tdep->ppc_gp0_regnum + 6,
772 writebuf + 12);
773 }
774 return RETURN_VALUE_REGISTER_CONVENTION;
775 }
e754ae69 776 if ((TYPE_CODE (type) == TYPE_CODE_INT && TYPE_LENGTH (type) == 8)
00fbcec4
JM
777 || (TYPE_CODE (type) == TYPE_CODE_FLT && TYPE_LENGTH (type) == 8)
778 || (TYPE_CODE (type) == TYPE_CODE_DECFLOAT && TYPE_LENGTH (type) == 8
779 && tdep->soft_float))
e754ae69 780 {
963e2bb7 781 if (readbuf)
e754ae69 782 {
00fbcec4
JM
783 /* A long long, double or _Decimal64 stored in the 32 bit
784 r3/r4. */
e754ae69 785 regcache_cooked_read (regcache, tdep->ppc_gp0_regnum + 3,
55eddb0f 786 readbuf + 0);
e754ae69 787 regcache_cooked_read (regcache, tdep->ppc_gp0_regnum + 4,
55eddb0f 788 readbuf + 4);
e754ae69 789 }
963e2bb7 790 if (writebuf)
e754ae69 791 {
00fbcec4
JM
792 /* A long long, double or _Decimal64 stored in the 32 bit
793 r3/r4. */
e754ae69 794 regcache_cooked_write (regcache, tdep->ppc_gp0_regnum + 3,
55eddb0f 795 writebuf + 0);
e754ae69 796 regcache_cooked_write (regcache, tdep->ppc_gp0_regnum + 4,
55eddb0f 797 writebuf + 4);
e754ae69
AC
798 }
799 return RETURN_VALUE_REGISTER_CONVENTION;
800 }
1300a2f4
TJB
801 if (TYPE_CODE (type) == TYPE_CODE_DECFLOAT && !tdep->soft_float)
802 return get_decimal_float_return_value (gdbarch, type, regcache, readbuf,
803 writebuf);
f0027ce2
DJ
804 else if ((TYPE_CODE (type) == TYPE_CODE_INT
805 || TYPE_CODE (type) == TYPE_CODE_CHAR
806 || TYPE_CODE (type) == TYPE_CODE_BOOL
807 || TYPE_CODE (type) == TYPE_CODE_PTR
808 || TYPE_CODE (type) == TYPE_CODE_REF
809 || TYPE_CODE (type) == TYPE_CODE_ENUM)
810 && TYPE_LENGTH (type) <= tdep->wordsize)
e754ae69 811 {
963e2bb7 812 if (readbuf)
e754ae69
AC
813 {
814 /* Some sort of integer stored in r3. Since TYPE isn't
815 bigger than the register, sign extension isn't a problem
816 - just do everything unsigned. */
817 ULONGEST regval;
818 regcache_cooked_read_unsigned (regcache, tdep->ppc_gp0_regnum + 3,
819 &regval);
e17a4113
UW
820 store_unsigned_integer (readbuf, TYPE_LENGTH (type), byte_order,
821 regval);
e754ae69 822 }
963e2bb7 823 if (writebuf)
e754ae69
AC
824 {
825 /* Some sort of integer stored in r3. Use unpack_long since
826 that should handle any required sign extension. */
827 regcache_cooked_write_unsigned (regcache, tdep->ppc_gp0_regnum + 3,
963e2bb7 828 unpack_long (type, writebuf));
e754ae69
AC
829 }
830 return RETURN_VALUE_REGISTER_CONVENTION;
831 }
54fcddd0
UW
832 /* OpenCL vectors < 16 bytes are returned as distinct
833 scalars in f1..f2 or r3..r10. */
834 if (TYPE_CODE (type) == TYPE_CODE_ARRAY
835 && TYPE_VECTOR (type)
836 && TYPE_LENGTH (type) < 16
837 && opencl_abi)
838 {
839 struct type *eltype = check_typedef (TYPE_TARGET_TYPE (type));
840 int i, nelt = TYPE_LENGTH (type) / TYPE_LENGTH (eltype);
841
842 for (i = 0; i < nelt; i++)
843 {
844 int offset = i * TYPE_LENGTH (eltype);
845
846 if (TYPE_CODE (eltype) == TYPE_CODE_FLT)
847 {
848 int regnum = tdep->ppc_fp0_regnum + 1 + i;
849 gdb_byte regval[MAX_REGISTER_SIZE];
850 struct type *regtype = register_type (gdbarch, regnum);
851
852 if (writebuf != NULL)
853 {
854 convert_typed_floating (writebuf + offset, eltype,
855 regval, regtype);
856 regcache_cooked_write (regcache, regnum, regval);
857 }
858 if (readbuf != NULL)
859 {
860 regcache_cooked_read (regcache, regnum, regval);
861 convert_typed_floating (regval, regtype,
862 readbuf + offset, eltype);
863 }
864 }
865 else
866 {
867 int regnum = tdep->ppc_gp0_regnum + 3 + i;
868 ULONGEST regval;
869
870 if (writebuf != NULL)
871 {
872 regval = unpack_long (eltype, writebuf + offset);
873 regcache_cooked_write_unsigned (regcache, regnum, regval);
874 }
875 if (readbuf != NULL)
876 {
877 regcache_cooked_read_unsigned (regcache, regnum, &regval);
878 store_unsigned_integer (readbuf + offset,
879 TYPE_LENGTH (eltype), byte_order,
880 regval);
881 }
882 }
883 }
884
885 return RETURN_VALUE_REGISTER_CONVENTION;
886 }
887 /* OpenCL vectors >= 16 bytes are returned in v2..v9. */
888 if (TYPE_CODE (type) == TYPE_CODE_ARRAY
889 && TYPE_VECTOR (type)
890 && TYPE_LENGTH (type) >= 16
891 && opencl_abi)
892 {
893 int n_regs = TYPE_LENGTH (type) / 16;
894 int i;
895
896 for (i = 0; i < n_regs; i++)
897 {
898 int offset = i * 16;
899 int regnum = tdep->ppc_vr0_regnum + 2 + i;
900
901 if (writebuf != NULL)
902 regcache_cooked_write (regcache, regnum, writebuf + offset);
903 if (readbuf != NULL)
904 regcache_cooked_read (regcache, regnum, readbuf + offset);
905 }
906
907 return RETURN_VALUE_REGISTER_CONVENTION;
908 }
e754ae69
AC
909 if (TYPE_LENGTH (type) == 16
910 && TYPE_CODE (type) == TYPE_CODE_ARRAY
55eddb0f
DJ
911 && TYPE_VECTOR (type)
912 && tdep->vector_abi == POWERPC_VEC_ALTIVEC)
e754ae69 913 {
963e2bb7 914 if (readbuf)
e754ae69
AC
915 {
916 /* Altivec places the return value in "v2". */
963e2bb7 917 regcache_cooked_read (regcache, tdep->ppc_vr0_regnum + 2, readbuf);
e754ae69 918 }
963e2bb7 919 if (writebuf)
e754ae69
AC
920 {
921 /* Altivec places the return value in "v2". */
963e2bb7 922 regcache_cooked_write (regcache, tdep->ppc_vr0_regnum + 2, writebuf);
e754ae69
AC
923 }
924 return RETURN_VALUE_REGISTER_CONVENTION;
925 }
55eddb0f
DJ
926 if (TYPE_LENGTH (type) == 16
927 && TYPE_CODE (type) == TYPE_CODE_ARRAY
928 && TYPE_VECTOR (type)
929 && tdep->vector_abi == POWERPC_VEC_GENERIC)
930 {
931 /* GCC -maltivec -mabi=no-altivec returns vectors in r3/r4/r5/r6.
932 GCC without AltiVec returns them in memory, but it warns about
933 ABI risks in that case; we don't try to support it. */
934 if (readbuf)
935 {
936 regcache_cooked_read (regcache, tdep->ppc_gp0_regnum + 3,
937 readbuf + 0);
938 regcache_cooked_read (regcache, tdep->ppc_gp0_regnum + 4,
939 readbuf + 4);
940 regcache_cooked_read (regcache, tdep->ppc_gp0_regnum + 5,
941 readbuf + 8);
942 regcache_cooked_read (regcache, tdep->ppc_gp0_regnum + 6,
943 readbuf + 12);
944 }
945 if (writebuf)
946 {
947 regcache_cooked_write (regcache, tdep->ppc_gp0_regnum + 3,
948 writebuf + 0);
949 regcache_cooked_write (regcache, tdep->ppc_gp0_regnum + 4,
950 writebuf + 4);
951 regcache_cooked_write (regcache, tdep->ppc_gp0_regnum + 5,
952 writebuf + 8);
953 regcache_cooked_write (regcache, tdep->ppc_gp0_regnum + 6,
954 writebuf + 12);
955 }
956 return RETURN_VALUE_REGISTER_CONVENTION;
957 }
e754ae69
AC
958 if (TYPE_LENGTH (type) == 8
959 && TYPE_CODE (type) == TYPE_CODE_ARRAY
55eddb0f
DJ
960 && TYPE_VECTOR (type)
961 && tdep->vector_abi == POWERPC_VEC_SPE)
e754ae69
AC
962 {
963 /* The e500 ABI places return values for the 64-bit DSP types
964 (__ev64_opaque__) in r3. However, in GDB-speak, ev3
965 corresponds to the entire r3 value for e500, whereas GDB's r3
966 only corresponds to the least significant 32-bits. So place
967 the 64-bit DSP type's value in ev3. */
963e2bb7
AC
968 if (readbuf)
969 regcache_cooked_read (regcache, tdep->ppc_ev0_regnum + 3, readbuf);
970 if (writebuf)
971 regcache_cooked_write (regcache, tdep->ppc_ev0_regnum + 3, writebuf);
e754ae69
AC
972 return RETURN_VALUE_REGISTER_CONVENTION;
973 }
974 if (broken_gcc && TYPE_LENGTH (type) <= 8)
975 {
61bf9ae0
MK
976 /* GCC screwed up for structures or unions whose size is less
977 than or equal to 8 bytes.. Instead of left-aligning, it
978 right-aligns the data into the buffer formed by r3, r4. */
979 gdb_byte regvals[MAX_REGISTER_SIZE * 2];
980 int len = TYPE_LENGTH (type);
981 int offset = (2 * tdep->wordsize - len) % tdep->wordsize;
982
963e2bb7 983 if (readbuf)
e754ae69 984 {
61bf9ae0
MK
985 regcache_cooked_read (regcache, tdep->ppc_gp0_regnum + 3,
986 regvals + 0 * tdep->wordsize);
987 if (len > tdep->wordsize)
988 regcache_cooked_read (regcache, tdep->ppc_gp0_regnum + 4,
989 regvals + 1 * tdep->wordsize);
990 memcpy (readbuf, regvals + offset, len);
e754ae69 991 }
963e2bb7 992 if (writebuf)
e754ae69 993 {
61bf9ae0
MK
994 memset (regvals, 0, sizeof regvals);
995 memcpy (regvals + offset, writebuf, len);
996 regcache_cooked_write (regcache, tdep->ppc_gp0_regnum + 3,
997 regvals + 0 * tdep->wordsize);
998 if (len > tdep->wordsize)
999 regcache_cooked_write (regcache, tdep->ppc_gp0_regnum + 4,
1000 regvals + 1 * tdep->wordsize);
e754ae69 1001 }
61bf9ae0 1002
e754ae69
AC
1003 return RETURN_VALUE_REGISTER_CONVENTION;
1004 }
1005 if (TYPE_LENGTH (type) <= 8)
1006 {
963e2bb7 1007 if (readbuf)
e754ae69
AC
1008 {
1009 /* This matches SVr4 PPC, it does not match GCC. */
1010 /* The value is right-padded to 8 bytes and then loaded, as
1011 two "words", into r3/r4. */
50fd1280 1012 gdb_byte regvals[MAX_REGISTER_SIZE * 2];
e754ae69
AC
1013 regcache_cooked_read (regcache, tdep->ppc_gp0_regnum + 3,
1014 regvals + 0 * tdep->wordsize);
1015 if (TYPE_LENGTH (type) > tdep->wordsize)
1016 regcache_cooked_read (regcache, tdep->ppc_gp0_regnum + 4,
1017 regvals + 1 * tdep->wordsize);
963e2bb7 1018 memcpy (readbuf, regvals, TYPE_LENGTH (type));
e754ae69 1019 }
963e2bb7 1020 if (writebuf)
e754ae69
AC
1021 {
1022 /* This matches SVr4 PPC, it does not match GCC. */
1023 /* The value is padded out to 8 bytes and then loaded, as
1024 two "words" into r3/r4. */
50fd1280 1025 gdb_byte regvals[MAX_REGISTER_SIZE * 2];
e754ae69 1026 memset (regvals, 0, sizeof regvals);
963e2bb7 1027 memcpy (regvals, writebuf, TYPE_LENGTH (type));
e754ae69
AC
1028 regcache_cooked_write (regcache, tdep->ppc_gp0_regnum + 3,
1029 regvals + 0 * tdep->wordsize);
1030 if (TYPE_LENGTH (type) > tdep->wordsize)
1031 regcache_cooked_write (regcache, tdep->ppc_gp0_regnum + 4,
1032 regvals + 1 * tdep->wordsize);
1033 }
1034 return RETURN_VALUE_REGISTER_CONVENTION;
1035 }
1036 return RETURN_VALUE_STRUCT_CONVENTION;
1037}
1038
05580c65 1039enum return_value_convention
6a3a010b 1040ppc_sysv_abi_return_value (struct gdbarch *gdbarch, struct value *function,
c055b101
CV
1041 struct type *valtype, struct regcache *regcache,
1042 gdb_byte *readbuf, const gdb_byte *writebuf)
e754ae69 1043{
6a3a010b
MR
1044 return do_ppc_sysv_return_value (gdbarch,
1045 function ? value_type (function) : NULL,
1046 valtype, regcache, readbuf, writebuf, 0);
e754ae69
AC
1047}
1048
05580c65 1049enum return_value_convention
963e2bb7 1050ppc_sysv_abi_broken_return_value (struct gdbarch *gdbarch,
6a3a010b 1051 struct value *function,
963e2bb7
AC
1052 struct type *valtype,
1053 struct regcache *regcache,
50fd1280 1054 gdb_byte *readbuf, const gdb_byte *writebuf)
e754ae69 1055{
6a3a010b
MR
1056 return do_ppc_sysv_return_value (gdbarch,
1057 function ? value_type (function) : NULL,
1058 valtype, regcache, readbuf, writebuf, 1);
944fcfab 1059}
afd48b75 1060
b6e1c027
AC
1061/* The helper function for 64-bit SYSV push_dummy_call. Converts the
1062 function's code address back into the function's descriptor
1063 address.
1064
1065 Find a value for the TOC register. Every symbol should have both
1066 ".FN" and "FN" in the minimal symbol table. "FN" points at the
1067 FN's descriptor, while ".FN" points at the entry point (which
1068 matches FUNC_ADDR). Need to reverse from FUNC_ADDR back to the
1069 FN's descriptor address (while at the same time being careful to
1070 find "FN" in the same object file as ".FN"). */
1071
1072static int
1073convert_code_addr_to_desc_addr (CORE_ADDR code_addr, CORE_ADDR *desc_addr)
1074{
1075 struct obj_section *dot_fn_section;
7cbd4a93 1076 struct bound_minimal_symbol dot_fn;
3b7344d5 1077 struct bound_minimal_symbol fn;
7cbd4a93 1078
b6e1c027
AC
1079 /* Find the minimal symbol that corresponds to CODE_ADDR (should
1080 have a name of the form ".FN"). */
1081 dot_fn = lookup_minimal_symbol_by_pc (code_addr);
efd66ac6 1082 if (dot_fn.minsym == NULL || MSYMBOL_LINKAGE_NAME (dot_fn.minsym)[0] != '.')
b6e1c027
AC
1083 return 0;
1084 /* Get the section that contains CODE_ADDR. Need this for the
1085 "objfile" that it contains. */
1086 dot_fn_section = find_pc_section (code_addr);
1087 if (dot_fn_section == NULL || dot_fn_section->objfile == NULL)
1088 return 0;
1089 /* Now find the corresponding "FN" (dropping ".") minimal symbol's
1090 address. Only look for the minimal symbol in ".FN"'s object file
1091 - avoids problems when two object files (i.e., shared libraries)
1092 contain a minimal symbol with the same name. */
efd66ac6 1093 fn = lookup_minimal_symbol (MSYMBOL_LINKAGE_NAME (dot_fn.minsym) + 1, NULL,
b6e1c027 1094 dot_fn_section->objfile);
3b7344d5 1095 if (fn.minsym == NULL)
b6e1c027
AC
1096 return 0;
1097 /* Found a descriptor. */
77e371c0 1098 (*desc_addr) = BMSYMBOL_VALUE_ADDRESS (fn);
b6e1c027
AC
1099 return 1;
1100}
1101
cc0e89c5
UW
1102/* Walk down the type tree of TYPE counting consecutive base elements.
1103 If *FIELD_TYPE is NULL, then set it to the first valid floating point
1104 or vector type. If a non-floating point or vector type is found, or
1105 if a floating point or vector type that doesn't match a non-NULL
1106 *FIELD_TYPE is found, then return -1, otherwise return the count in the
1107 sub-tree. */
1108
1109static LONGEST
1110ppc64_aggregate_candidate (struct type *type,
1111 struct type **field_type)
1112{
1113 type = check_typedef (type);
1114
1115 switch (TYPE_CODE (type))
1116 {
1117 case TYPE_CODE_FLT:
1118 case TYPE_CODE_DECFLOAT:
1119 if (!*field_type)
1120 *field_type = type;
1121 if (TYPE_CODE (*field_type) == TYPE_CODE (type)
1122 && TYPE_LENGTH (*field_type) == TYPE_LENGTH (type))
1123 return 1;
1124 break;
1125
1126 case TYPE_CODE_COMPLEX:
1127 type = TYPE_TARGET_TYPE (type);
1128 if (TYPE_CODE (type) == TYPE_CODE_FLT
1129 || TYPE_CODE (type) == TYPE_CODE_DECFLOAT)
1130 {
1131 if (!*field_type)
1132 *field_type = type;
1133 if (TYPE_CODE (*field_type) == TYPE_CODE (type)
1134 && TYPE_LENGTH (*field_type) == TYPE_LENGTH (type))
1135 return 2;
1136 }
1137 break;
1138
1139 case TYPE_CODE_ARRAY:
1140 if (TYPE_VECTOR (type))
1141 {
1142 if (!*field_type)
1143 *field_type = type;
1144 if (TYPE_CODE (*field_type) == TYPE_CODE (type)
1145 && TYPE_LENGTH (*field_type) == TYPE_LENGTH (type))
1146 return 1;
1147 }
1148 else
1149 {
1150 LONGEST count, low_bound, high_bound;
1151
1152 count = ppc64_aggregate_candidate
1153 (TYPE_TARGET_TYPE (type), field_type);
1154 if (count == -1)
1155 return -1;
1156
1157 if (!get_array_bounds (type, &low_bound, &high_bound))
1158 return -1;
1159 count *= high_bound - low_bound;
1160
1161 /* There must be no padding. */
1162 if (count == 0)
1163 return TYPE_LENGTH (type) == 0 ? 0 : -1;
1164 else if (TYPE_LENGTH (type) != count * TYPE_LENGTH (*field_type))
1165 return -1;
1166
1167 return count;
1168 }
1169 break;
1170
1171 case TYPE_CODE_STRUCT:
1172 case TYPE_CODE_UNION:
1173 {
1174 LONGEST count = 0;
1175 int i;
1176
1177 for (i = 0; i < TYPE_NFIELDS (type); i++)
1178 {
1179 LONGEST sub_count;
1180
1181 if (field_is_static (&TYPE_FIELD (type, i)))
1182 continue;
1183
1184 sub_count = ppc64_aggregate_candidate
1185 (TYPE_FIELD_TYPE (type, i), field_type);
1186 if (sub_count == -1)
1187 return -1;
1188
1189 if (TYPE_CODE (type) == TYPE_CODE_STRUCT)
1190 count += sub_count;
1191 else
1192 count = max (count, sub_count);
1193 }
1194
1195 /* There must be no padding. */
1196 if (count == 0)
1197 return TYPE_LENGTH (type) == 0 ? 0 : -1;
1198 else if (TYPE_LENGTH (type) != count * TYPE_LENGTH (*field_type))
1199 return -1;
1200
1201 return count;
1202 }
1203 break;
1204
1205 default:
1206 break;
1207 }
1208
1209 return -1;
1210}
1211
1212/* If an argument of type TYPE is a homogeneous float or vector aggregate
1213 that shall be passed in FP/vector registers according to the ELFv2 ABI,
1214 return the homogeneous element type in *ELT_TYPE and the number of
1215 elements in *N_ELTS, and return non-zero. Otherwise, return zero. */
1216
1217static int
1218ppc64_elfv2_abi_homogeneous_aggregate (struct type *type,
1219 struct type **elt_type, int *n_elts)
1220{
1221 /* Complex types at the top level are treated separately. However,
1222 complex types can be elements of homogeneous aggregates. */
1223 if (TYPE_CODE (type) == TYPE_CODE_STRUCT
1224 || TYPE_CODE (type) == TYPE_CODE_UNION
1225 || (TYPE_CODE (type) == TYPE_CODE_ARRAY && !TYPE_VECTOR (type)))
1226 {
1227 struct type *field_type = NULL;
1228 LONGEST field_count = ppc64_aggregate_candidate (type, &field_type);
1229
1230 if (field_count > 0)
1231 {
1232 int n_regs = ((TYPE_CODE (field_type) == TYPE_CODE_FLT
1233 || TYPE_CODE (field_type) == TYPE_CODE_DECFLOAT)?
1234 (TYPE_LENGTH (field_type) + 7) >> 3 : 1);
1235
1236 /* The ELFv2 ABI allows homogeneous aggregates to occupy
1237 up to 8 registers. */
1238 if (field_count * n_regs <= 8)
1239 {
1240 if (elt_type)
1241 *elt_type = field_type;
1242 if (n_elts)
1243 *n_elts = (int) field_count;
1244 /* Note that field_count is LONGEST since it may hold the size
1245 of an array, while *n_elts is int since its value is bounded
1246 by the number of registers used for argument passing. The
1247 cast cannot overflow due to the bounds checking above. */
1248 return 1;
1249 }
1250 }
1251 }
1252
1253 return 0;
1254}
1255
e765b44c
UW
1256/* Structure holding the next argument position. */
1257struct ppc64_sysv_argpos
1258 {
1259 /* Register cache holding argument registers. If this is NULL,
1260 we only simulate argument processing without actually updating
1261 any registers or memory. */
1262 struct regcache *regcache;
1263 /* Next available general-purpose argument register. */
1264 int greg;
1265 /* Next available floating-point argument register. */
1266 int freg;
1267 /* Next available vector argument register. */
1268 int vreg;
1269 /* The address, at which the next general purpose parameter
1270 (integer, struct, float, vector, ...) should be saved. */
1271 CORE_ADDR gparam;
1272 /* The address, at which the next by-reference parameter
1273 (non-Altivec vector, variably-sized type) should be saved. */
1274 CORE_ADDR refparam;
1275 };
1276
1277/* VAL is a value of length LEN. Store it into the argument area on the
1278 stack and load it into the corresponding general-purpose registers
1279 required by the ABI, and update ARGPOS.
1280
1281 If ALIGN is nonzero, it specifies the minimum alignment required
1282 for the on-stack copy of the argument. */
d81e75c0 1283
e765b44c
UW
1284static void
1285ppc64_sysv_abi_push_val (struct gdbarch *gdbarch,
1286 const bfd_byte *val, int len, int align,
1287 struct ppc64_sysv_argpos *argpos)
1288{
1289 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1290 int offset = 0;
1291
1292 /* Enforce alignment of stack location, if requested. */
1293 if (align > tdep->wordsize)
1294 {
1295 CORE_ADDR aligned_gparam = align_up (argpos->gparam, align);
1296
1297 argpos->greg += (aligned_gparam - argpos->gparam) / tdep->wordsize;
1298 argpos->gparam = aligned_gparam;
1299 }
1300
1301 /* The ABI (version 1.9) specifies that values smaller than one
1302 doubleword are right-aligned and those larger are left-aligned.
1303 GCC versions before 3.4 implemented this incorrectly; see
1304 <http://gcc.gnu.org/gcc-3.4/powerpc-abi.html>. */
d63167af
UW
1305 if (len < tdep->wordsize
1306 && gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
e765b44c
UW
1307 offset = tdep->wordsize - len;
1308
1309 if (argpos->regcache)
1310 write_memory (argpos->gparam + offset, val, len);
1311 argpos->gparam = align_up (argpos->gparam + len, tdep->wordsize);
1312
1313 while (len >= tdep->wordsize)
1314 {
1315 if (argpos->regcache && argpos->greg <= 10)
1316 regcache_cooked_write (argpos->regcache,
1317 tdep->ppc_gp0_regnum + argpos->greg, val);
1318 argpos->greg++;
1319 len -= tdep->wordsize;
1320 val += tdep->wordsize;
1321 }
1322
1323 if (len > 0)
1324 {
1325 if (argpos->regcache && argpos->greg <= 10)
1326 regcache_cooked_write_part (argpos->regcache,
1327 tdep->ppc_gp0_regnum + argpos->greg,
1328 offset, len, val);
1329 argpos->greg++;
1330 }
1331}
1332
1333/* The same as ppc64_sysv_abi_push_val, but using a single-word integer
1334 value VAL as argument. */
d81e75c0
TD
1335
1336static void
e765b44c
UW
1337ppc64_sysv_abi_push_integer (struct gdbarch *gdbarch, ULONGEST val,
1338 struct ppc64_sysv_argpos *argpos)
d81e75c0 1339{
e765b44c
UW
1340 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1341 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1342 gdb_byte buf[MAX_REGISTER_SIZE];
d81e75c0 1343
e765b44c
UW
1344 if (argpos->regcache)
1345 store_unsigned_integer (buf, tdep->wordsize, byte_order, val);
1346 ppc64_sysv_abi_push_val (gdbarch, buf, tdep->wordsize, 0, argpos);
1347}
1348
1349/* VAL is a value of TYPE, a (binary or decimal) floating-point type.
1350 Load it into a floating-point register if required by the ABI,
1351 and update ARGPOS. */
1352
1353static void
1354ppc64_sysv_abi_push_freg (struct gdbarch *gdbarch,
1355 struct type *type, const bfd_byte *val,
1356 struct ppc64_sysv_argpos *argpos)
1357{
1358 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1359 if (tdep->soft_float)
1360 return;
1361
1362 if (TYPE_LENGTH (type) <= 8
1363 && TYPE_CODE (type) == TYPE_CODE_FLT)
d81e75c0 1364 {
e765b44c
UW
1365 /* Floats and doubles go in f1 .. f13. 32-bit floats are converted
1366 to double first. */
1367 if (argpos->regcache && argpos->freg <= 13)
1368 {
1369 int regnum = tdep->ppc_fp0_regnum + argpos->freg;
1370 struct type *regtype = register_type (gdbarch, regnum);
1371 gdb_byte regval[MAX_REGISTER_SIZE];
d81e75c0 1372
e765b44c
UW
1373 convert_typed_floating (val, type, regval, regtype);
1374 regcache_cooked_write (argpos->regcache, regnum, regval);
1375 }
d81e75c0 1376
e765b44c
UW
1377 argpos->freg++;
1378 }
1379 else if (TYPE_LENGTH (type) <= 8
1380 && TYPE_CODE (type) == TYPE_CODE_DECFLOAT)
1381 {
1382 /* Floats and doubles go in f1 .. f13. 32-bit decimal floats are
1383 placed in the least significant word. */
1384 if (argpos->regcache && argpos->freg <= 13)
1385 {
1386 int regnum = tdep->ppc_fp0_regnum + argpos->freg;
5b757e5d
UW
1387 int offset = 0;
1388
1389 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
1390 offset = 8 - TYPE_LENGTH (type);
d81e75c0 1391
e765b44c
UW
1392 regcache_cooked_write_part (argpos->regcache, regnum,
1393 offset, TYPE_LENGTH (type), val);
1394 }
d81e75c0 1395
e765b44c
UW
1396 argpos->freg++;
1397 }
1398 else if (TYPE_LENGTH (type) == 16
1399 && TYPE_CODE (type) == TYPE_CODE_FLT
1400 && (gdbarch_long_double_format (gdbarch)
1401 == floatformats_ibm_long_double))
1402 {
1403 /* IBM long double stored in two consecutive FPRs. */
1404 if (argpos->regcache && argpos->freg <= 13)
d81e75c0 1405 {
e765b44c
UW
1406 int regnum = tdep->ppc_fp0_regnum + argpos->freg;
1407
1408 regcache_cooked_write (argpos->regcache, regnum, val);
1409 if (argpos->freg <= 12)
1410 regcache_cooked_write (argpos->regcache, regnum + 1, val + 8);
d81e75c0 1411 }
d81e75c0 1412
e765b44c
UW
1413 argpos->freg += 2;
1414 }
1415 else if (TYPE_LENGTH (type) == 16
1416 && TYPE_CODE (type) == TYPE_CODE_DECFLOAT)
1417 {
1418 /* 128-bit decimal floating-point values are stored in and even/odd
1419 pair of FPRs, with the even FPR holding the most significant half. */
1420 argpos->freg += argpos->freg & 1;
d81e75c0 1421
e765b44c 1422 if (argpos->regcache && argpos->freg <= 12)
d81e75c0 1423 {
e765b44c 1424 int regnum = tdep->ppc_fp0_regnum + argpos->freg;
0ff3e01f
UW
1425 int lopart = gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG ? 8 : 0;
1426 int hipart = gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG ? 0 : 8;
d81e75c0 1427
0ff3e01f
UW
1428 regcache_cooked_write (argpos->regcache, regnum, val + hipart);
1429 regcache_cooked_write (argpos->regcache, regnum + 1, val + lopart);
d81e75c0 1430 }
e765b44c
UW
1431
1432 argpos->freg += 2;
d81e75c0 1433 }
e765b44c
UW
1434}
1435
1436/* VAL is a value of AltiVec vector type. Load it into a vector register
1437 if required by the ABI, and update ARGPOS. */
1438
1439static void
1440ppc64_sysv_abi_push_vreg (struct gdbarch *gdbarch, const bfd_byte *val,
1441 struct ppc64_sysv_argpos *argpos)
1442{
1443 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1444
1445 if (argpos->regcache && argpos->vreg <= 13)
1446 regcache_cooked_write (argpos->regcache,
1447 tdep->ppc_vr0_regnum + argpos->vreg, val);
1448
1449 argpos->vreg++;
1450}
1451
1452/* VAL is a value of TYPE. Load it into memory and/or registers
1453 as required by the ABI, and update ARGPOS. */
1454
1455static void
1456ppc64_sysv_abi_push_param (struct gdbarch *gdbarch,
1457 struct type *type, const bfd_byte *val,
1458 struct ppc64_sysv_argpos *argpos)
1459{
1460 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1461
1462 if (TYPE_CODE (type) == TYPE_CODE_FLT
1463 || TYPE_CODE (type) == TYPE_CODE_DECFLOAT)
1464 {
1465 /* Floating-point scalars are passed in floating-point registers. */
1466 ppc64_sysv_abi_push_val (gdbarch, val, TYPE_LENGTH (type), 0, argpos);
1467 ppc64_sysv_abi_push_freg (gdbarch, type, val, argpos);
1468 }
1469 else if (TYPE_CODE (type) == TYPE_CODE_ARRAY && TYPE_VECTOR (type)
1470 && tdep->vector_abi == POWERPC_VEC_ALTIVEC
1471 && TYPE_LENGTH (type) == 16)
1472 {
1473 /* AltiVec vectors are passed aligned, and in vector registers. */
1474 ppc64_sysv_abi_push_val (gdbarch, val, TYPE_LENGTH (type), 16, argpos);
1475 ppc64_sysv_abi_push_vreg (gdbarch, val, argpos);
1476 }
1477 else if (TYPE_CODE (type) == TYPE_CODE_ARRAY && TYPE_VECTOR (type)
1478 && TYPE_LENGTH (type) >= 16)
d81e75c0 1479 {
e765b44c
UW
1480 /* Non-Altivec vectors are passed by reference. */
1481
1482 /* Copy value onto the stack ... */
1483 CORE_ADDR addr = align_up (argpos->refparam, 16);
1484 if (argpos->regcache)
1485 write_memory (addr, val, TYPE_LENGTH (type));
1486 argpos->refparam = align_up (addr + TYPE_LENGTH (type), tdep->wordsize);
1487
1488 /* ... and pass a pointer to the copy as parameter. */
1489 ppc64_sysv_abi_push_integer (gdbarch, addr, argpos);
1490 }
1491 else if ((TYPE_CODE (type) == TYPE_CODE_INT
1492 || TYPE_CODE (type) == TYPE_CODE_ENUM
1493 || TYPE_CODE (type) == TYPE_CODE_BOOL
1494 || TYPE_CODE (type) == TYPE_CODE_CHAR
1495 || TYPE_CODE (type) == TYPE_CODE_PTR
1496 || TYPE_CODE (type) == TYPE_CODE_REF)
1497 && TYPE_LENGTH (type) <= tdep->wordsize)
1498 {
1499 ULONGEST word = 0;
1500
1501 if (argpos->regcache)
d81e75c0 1502 {
e765b44c
UW
1503 /* Sign extend the value, then store it unsigned. */
1504 word = unpack_long (type, val);
1505
1506 /* Convert any function code addresses into descriptors. */
d4094b6a
UW
1507 if (tdep->elf_abi == POWERPC_ELF_V1
1508 && (TYPE_CODE (type) == TYPE_CODE_PTR
1509 || TYPE_CODE (type) == TYPE_CODE_REF))
e765b44c
UW
1510 {
1511 struct type *target_type
1512 = check_typedef (TYPE_TARGET_TYPE (type));
1513
1514 if (TYPE_CODE (target_type) == TYPE_CODE_FUNC
1515 || TYPE_CODE (target_type) == TYPE_CODE_METHOD)
1516 {
1517 CORE_ADDR desc = word;
1518
1519 convert_code_addr_to_desc_addr (word, &desc);
1520 word = desc;
1521 }
1522 }
d81e75c0 1523 }
e765b44c
UW
1524
1525 ppc64_sysv_abi_push_integer (gdbarch, word, argpos);
1526 }
1527 else
1528 {
1529 ppc64_sysv_abi_push_val (gdbarch, val, TYPE_LENGTH (type), 0, argpos);
1530
1531 /* The ABI (version 1.9) specifies that structs containing a
1532 single floating-point value, at any level of nesting of
1533 single-member structs, are passed in floating-point registers. */
1534 if (TYPE_CODE (type) == TYPE_CODE_STRUCT
1535 && TYPE_NFIELDS (type) == 1)
d81e75c0 1536 {
e765b44c
UW
1537 while (TYPE_CODE (type) == TYPE_CODE_STRUCT
1538 && TYPE_NFIELDS (type) == 1)
1539 type = check_typedef (TYPE_FIELD_TYPE (type, 0));
1540
1541 if (TYPE_CODE (type) == TYPE_CODE_FLT)
1542 ppc64_sysv_abi_push_freg (gdbarch, type, val, argpos);
d81e75c0 1543 }
cc0e89c5
UW
1544
1545 /* In the ELFv2 ABI, homogeneous floating-point or vector
1546 aggregates are passed in a series of registers. */
1547 if (tdep->elf_abi == POWERPC_ELF_V2)
1548 {
1549 struct type *eltype;
1550 int i, nelt;
1551
1552 if (ppc64_elfv2_abi_homogeneous_aggregate (type, &eltype, &nelt))
1553 for (i = 0; i < nelt; i++)
1554 {
1555 const gdb_byte *elval = val + i * TYPE_LENGTH (eltype);
1556
1557 if (TYPE_CODE (eltype) == TYPE_CODE_FLT
1558 || TYPE_CODE (eltype) == TYPE_CODE_DECFLOAT)
1559 ppc64_sysv_abi_push_freg (gdbarch, eltype, elval, argpos);
1560 else if (TYPE_CODE (eltype) == TYPE_CODE_ARRAY
1561 && TYPE_VECTOR (eltype)
1562 && tdep->vector_abi == POWERPC_VEC_ALTIVEC
1563 && TYPE_LENGTH (eltype) == 16)
1564 ppc64_sysv_abi_push_vreg (gdbarch, elval, argpos);
1565 }
1566 }
d81e75c0
TD
1567 }
1568}
1569
0df8b418 1570/* Pass the arguments in either registers, or in the stack. Using the
8be9034a
AC
1571 ppc 64 bit SysV ABI.
1572
1573 This implements a dumbed down version of the ABI. It always writes
1574 values to memory, GPR and FPR, even when not necessary. Doing this
0df8b418 1575 greatly simplifies the logic. */
8be9034a
AC
1576
1577CORE_ADDR
0df8b418
MS
1578ppc64_sysv_abi_push_dummy_call (struct gdbarch *gdbarch,
1579 struct value *function,
8be9034a
AC
1580 struct regcache *regcache, CORE_ADDR bp_addr,
1581 int nargs, struct value **args, CORE_ADDR sp,
1582 int struct_return, CORE_ADDR struct_addr)
1583{
7d9b040b 1584 CORE_ADDR func_addr = find_function_addr (function, NULL);
40a6adc1 1585 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
e17a4113 1586 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
88aed45e 1587 int opencl_abi = ppc_sysv_use_opencl_abi (value_type (function));
fb4443d8 1588 ULONGEST back_chain;
8be9034a
AC
1589 /* See for-loop comment below. */
1590 int write_pass;
24e9cda0
UW
1591 /* Size of the by-reference parameter copy region, the final value is
1592 computed in the for-loop below. */
1593 LONGEST refparam_size = 0;
8be9034a
AC
1594 /* Size of the general parameter region, the final value is computed
1595 in the for-loop below. */
1596 LONGEST gparam_size = 0;
1597 /* Kevin writes ... I don't mind seeing tdep->wordsize used in the
0df8b418 1598 calls to align_up(), align_down(), etc. because this makes it
8be9034a
AC
1599 easier to reuse this code (in a copy/paste sense) in the future,
1600 but it is a 64-bit ABI and asserting that the wordsize is 8 bytes
1601 at some point makes it easier to verify that this function is
1602 correct without having to do a non-local analysis to figure out
1603 the possible values of tdep->wordsize. */
1604 gdb_assert (tdep->wordsize == 8);
1605
55eddb0f
DJ
1606 /* This function exists to support a calling convention that
1607 requires floating-point registers. It shouldn't be used on
1608 processors that lack them. */
1609 gdb_assert (ppc_floating_point_unit_p (gdbarch));
1610
fb4443d8
UW
1611 /* By this stage in the proceedings, SP has been decremented by "red
1612 zone size" + "struct return size". Fetch the stack-pointer from
1613 before this and use that as the BACK_CHAIN. */
40a6adc1 1614 regcache_cooked_read_unsigned (regcache, gdbarch_sp_regnum (gdbarch),
3e8c568d 1615 &back_chain);
fb4443d8 1616
8be9034a
AC
1617 /* Go through the argument list twice.
1618
1619 Pass 1: Compute the function call's stack space and register
1620 requirements.
1621
1622 Pass 2: Replay the same computation but this time also write the
1623 values out to the target. */
1624
1625 for (write_pass = 0; write_pass < 2; write_pass++)
1626 {
1627 int argno;
e765b44c
UW
1628
1629 struct ppc64_sysv_argpos argpos;
1630 argpos.greg = 3;
1631 argpos.freg = 1;
1632 argpos.vreg = 2;
8be9034a
AC
1633
1634 if (!write_pass)
1635 {
24e9cda0
UW
1636 /* During the first pass, GPARAM and REFPARAM are more like
1637 offsets (start address zero) than addresses. That way
1638 they accumulate the total stack space each region
1639 requires. */
e765b44c
UW
1640 argpos.regcache = NULL;
1641 argpos.gparam = 0;
1642 argpos.refparam = 0;
8be9034a
AC
1643 }
1644 else
1645 {
24e9cda0
UW
1646 /* Decrement the stack pointer making space for the Altivec
1647 and general on-stack parameters. Set refparam and gparam
1648 to their corresponding regions. */
e765b44c
UW
1649 argpos.regcache = regcache;
1650 argpos.refparam = align_down (sp - refparam_size, 16);
1651 argpos.gparam = align_down (argpos.refparam - gparam_size, 16);
52f548e4
UW
1652 /* Add in space for the TOC, link editor double word (v1 only),
1653 compiler double word (v1 only), LR save area, CR save area,
1654 and backchain. */
1655 if (tdep->elf_abi == POWERPC_ELF_V1)
1656 sp = align_down (argpos.gparam - 48, 16);
1657 else
1658 sp = align_down (argpos.gparam - 32, 16);
8be9034a
AC
1659 }
1660
1661 /* If the function is returning a `struct', then there is an
1662 extra hidden parameter (which will be passed in r3)
1663 containing the address of that struct.. In that case we
1664 should advance one word and start from r4 register to copy
1665 parameters. This also consumes one on-stack parameter slot. */
1666 if (struct_return)
e765b44c 1667 ppc64_sysv_abi_push_integer (gdbarch, struct_addr, &argpos);
8be9034a
AC
1668
1669 for (argno = 0; argno < nargs; argno++)
1670 {
1671 struct value *arg = args[argno];
df407dfe 1672 struct type *type = check_typedef (value_type (arg));
0fd88904 1673 const bfd_byte *val = value_contents (arg);
ce0451ad 1674
e765b44c 1675 if (TYPE_CODE (type) == TYPE_CODE_COMPLEX)
8be9034a 1676 {
e765b44c
UW
1677 /* Complex types are passed as if two independent scalars. */
1678 struct type *eltype = check_typedef (TYPE_TARGET_TYPE (type));
1300a2f4 1679
e765b44c
UW
1680 ppc64_sysv_abi_push_param (gdbarch, eltype, val, &argpos);
1681 ppc64_sysv_abi_push_param (gdbarch, eltype,
1682 val + TYPE_LENGTH (eltype), &argpos);
1300a2f4 1683 }
e765b44c 1684 else if (TYPE_CODE (type) == TYPE_CODE_ARRAY && TYPE_VECTOR (type)
54fcddd0
UW
1685 && opencl_abi)
1686 {
1687 /* OpenCL vectors shorter than 16 bytes are passed as if
e765b44c
UW
1688 a series of independent scalars; OpenCL vectors 16 bytes
1689 or longer are passed as if a series of AltiVec vectors. */
1690 struct type *eltype;
1691 int i, nelt;
54fcddd0 1692
e765b44c
UW
1693 if (TYPE_LENGTH (type) < 16)
1694 eltype = check_typedef (TYPE_TARGET_TYPE (type));
1695 else
1696 eltype = register_type (gdbarch, tdep->ppc_vr0_regnum);
1697
1698 nelt = TYPE_LENGTH (type) / TYPE_LENGTH (eltype);
54fcddd0
UW
1699 for (i = 0; i < nelt; i++)
1700 {
1701 const gdb_byte *elval = val + i * TYPE_LENGTH (eltype);
1702
e765b44c 1703 ppc64_sysv_abi_push_param (gdbarch, eltype, elval, &argpos);
8be9034a 1704 }
8be9034a
AC
1705 }
1706 else
1707 {
e765b44c
UW
1708 /* All other types are passed as single arguments. */
1709 ppc64_sysv_abi_push_param (gdbarch, type, val, &argpos);
8be9034a
AC
1710 }
1711 }
1712
1713 if (!write_pass)
1714 {
24e9cda0 1715 /* Save the true region sizes ready for the second pass. */
e765b44c 1716 refparam_size = argpos.refparam;
24e9cda0 1717 /* Make certain that the general parameter save area is at
8be9034a 1718 least the minimum 8 registers (or doublewords) in size. */
e765b44c 1719 if (argpos.greg < 8)
8be9034a
AC
1720 gparam_size = 8 * tdep->wordsize;
1721 else
e765b44c 1722 gparam_size = argpos.gparam;
8be9034a
AC
1723 }
1724 }
1725
1726 /* Update %sp. */
40a6adc1 1727 regcache_cooked_write_signed (regcache, gdbarch_sp_regnum (gdbarch), sp);
8be9034a
AC
1728
1729 /* Write the backchain (it occupies WORDSIZED bytes). */
e17a4113 1730 write_memory_signed_integer (sp, tdep->wordsize, byte_order, back_chain);
8be9034a
AC
1731
1732 /* Point the inferior function call's return address at the dummy's
1733 breakpoint. */
1734 regcache_cooked_write_signed (regcache, tdep->ppc_lr_regnum, bp_addr);
1735
d4094b6a
UW
1736 /* In the ELFv1 ABI, use the func_addr to find the descriptor, and use
1737 that to find the TOC. If we're calling via a function pointer,
1738 the pointer itself identifies the descriptor. */
1739 if (tdep->elf_abi == POWERPC_ELF_V1)
1740 {
1741 struct type *ftype = check_typedef (value_type (function));
1742 CORE_ADDR desc_addr = value_as_address (function);
1743
1744 if (TYPE_CODE (ftype) == TYPE_CODE_PTR
1745 || convert_code_addr_to_desc_addr (func_addr, &desc_addr))
1746 {
1747 /* The TOC is the second double word in the descriptor. */
1748 CORE_ADDR toc =
1749 read_memory_unsigned_integer (desc_addr + tdep->wordsize,
1750 tdep->wordsize, byte_order);
1751
1752 regcache_cooked_write_unsigned (regcache,
1753 tdep->ppc_gp0_regnum + 2, toc);
1754 }
1755 }
1756
1757 /* In the ELFv2 ABI, we need to pass the target address in r12 since
1758 we may be calling a global entry point. */
1759 if (tdep->elf_abi == POWERPC_ELF_V2)
1760 regcache_cooked_write_unsigned (regcache,
1761 tdep->ppc_gp0_regnum + 12, func_addr);
8be9034a
AC
1762
1763 return sp;
1764}
1765
e765b44c
UW
1766/* Subroutine of ppc64_sysv_abi_return_value that handles "base" types:
1767 integer, floating-point, and AltiVec vector types.
afd48b75 1768
e765b44c
UW
1769 This routine also handles components of aggregate return types;
1770 INDEX describes which part of the aggregate is to be handled.
afd48b75 1771
e765b44c
UW
1772 Returns true if VALTYPE is some such base type that could be handled,
1773 false otherwise. */
1774static int
1775ppc64_sysv_abi_return_value_base (struct gdbarch *gdbarch, struct type *valtype,
1776 struct regcache *regcache, gdb_byte *readbuf,
1777 const gdb_byte *writebuf, int index)
afd48b75 1778{
05580c65 1779 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
16796152 1780
e765b44c 1781 /* Integers live in GPRs starting at r3. */
b6e1c027 1782 if ((TYPE_CODE (valtype) == TYPE_CODE_INT
93d4208d
UW
1783 || TYPE_CODE (valtype) == TYPE_CODE_ENUM
1784 || TYPE_CODE (valtype) == TYPE_CODE_CHAR
1785 || TYPE_CODE (valtype) == TYPE_CODE_BOOL)
b6e1c027 1786 && TYPE_LENGTH (valtype) <= 8)
afd48b75 1787 {
e765b44c
UW
1788 int regnum = tdep->ppc_gp0_regnum + 3 + index;
1789
963e2bb7 1790 if (writebuf != NULL)
afd48b75
AC
1791 {
1792 /* Be careful to sign extend the value. */
e765b44c 1793 regcache_cooked_write_unsigned (regcache, regnum,
963e2bb7 1794 unpack_long (valtype, writebuf));
afd48b75 1795 }
963e2bb7 1796 if (readbuf != NULL)
afd48b75 1797 {
e765b44c 1798 /* Extract the integer from GPR. Since this is truncating the
afd48b75
AC
1799 value, there isn't a sign extension problem. */
1800 ULONGEST regval;
e765b44c
UW
1801
1802 regcache_cooked_read_unsigned (regcache, regnum, &regval);
1803 store_unsigned_integer (readbuf, TYPE_LENGTH (valtype),
1804 gdbarch_byte_order (gdbarch), regval);
afd48b75 1805 }
e765b44c 1806 return 1;
afd48b75 1807 }
e765b44c
UW
1808
1809 /* Floats and doubles go in f1 .. f13. 32-bit floats are converted
1810 to double first. */
1811 if (TYPE_LENGTH (valtype) <= 8
1812 && TYPE_CODE (valtype) == TYPE_CODE_FLT)
afd48b75 1813 {
e765b44c
UW
1814 int regnum = tdep->ppc_fp0_regnum + 1 + index;
1815 struct type *regtype = register_type (gdbarch, regnum);
1816 gdb_byte regval[MAX_REGISTER_SIZE];
1817
963e2bb7 1818 if (writebuf != NULL)
e765b44c
UW
1819 {
1820 convert_typed_floating (writebuf, valtype, regval, regtype);
1821 regcache_cooked_write (regcache, regnum, regval);
1822 }
963e2bb7 1823 if (readbuf != NULL)
e765b44c
UW
1824 {
1825 regcache_cooked_read (regcache, regnum, regval);
1826 convert_typed_floating (regval, regtype, readbuf, valtype);
1827 }
1828 return 1;
afd48b75 1829 }
54fcddd0 1830
e765b44c
UW
1831 /* Floats and doubles go in f1 .. f13. 32-bit decimal floats are
1832 placed in the least significant word. */
1833 if (TYPE_LENGTH (valtype) <= 8
1834 && TYPE_CODE (valtype) == TYPE_CODE_DECFLOAT)
1835 {
1836 int regnum = tdep->ppc_fp0_regnum + 1 + index;
5b757e5d
UW
1837 int offset = 0;
1838
1839 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
1840 offset = 8 - TYPE_LENGTH (valtype);
54fcddd0 1841
e765b44c
UW
1842 if (writebuf != NULL)
1843 regcache_cooked_write_part (regcache, regnum,
1844 offset, TYPE_LENGTH (valtype), writebuf);
1845 if (readbuf != NULL)
1846 regcache_cooked_read_part (regcache, regnum,
1847 offset, TYPE_LENGTH (valtype), readbuf);
1848 return 1;
1849 }
54fcddd0 1850
e765b44c
UW
1851 /* IBM long double stored in two consecutive FPRs. */
1852 if (TYPE_LENGTH (valtype) == 16
1853 && TYPE_CODE (valtype) == TYPE_CODE_FLT
1854 && (gdbarch_long_double_format (gdbarch)
1855 == floatformats_ibm_long_double))
1856 {
1857 int regnum = tdep->ppc_fp0_regnum + 1 + 2 * index;
54fcddd0 1858
e765b44c
UW
1859 if (writebuf != NULL)
1860 {
1861 regcache_cooked_write (regcache, regnum, writebuf);
1862 regcache_cooked_write (regcache, regnum + 1, writebuf + 8);
54fcddd0 1863 }
e765b44c
UW
1864 if (readbuf != NULL)
1865 {
1866 regcache_cooked_read (regcache, regnum, readbuf);
1867 regcache_cooked_read (regcache, regnum + 1, readbuf + 8);
1868 }
1869 return 1;
54fcddd0 1870 }
e765b44c
UW
1871
1872 /* 128-bit decimal floating-point values are stored in an even/odd
1873 pair of FPRs, with the even FPR holding the most significant half. */
1874 if (TYPE_LENGTH (valtype) == 16
1875 && TYPE_CODE (valtype) == TYPE_CODE_DECFLOAT)
54fcddd0 1876 {
e765b44c 1877 int regnum = tdep->ppc_fp0_regnum + 2 + 2 * index;
0ff3e01f
UW
1878 int lopart = gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG ? 8 : 0;
1879 int hipart = gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG ? 0 : 8;
54fcddd0 1880
e765b44c 1881 if (writebuf != NULL)
54fcddd0 1882 {
0ff3e01f
UW
1883 regcache_cooked_write (regcache, regnum, writebuf + hipart);
1884 regcache_cooked_write (regcache, regnum + 1, writebuf + lopart);
54fcddd0 1885 }
e765b44c
UW
1886 if (readbuf != NULL)
1887 {
0ff3e01f
UW
1888 regcache_cooked_read (regcache, regnum, readbuf + hipart);
1889 regcache_cooked_read (regcache, regnum + 1, readbuf + lopart);
e765b44c
UW
1890 }
1891 return 1;
54fcddd0 1892 }
e765b44c
UW
1893
1894 /* AltiVec vectors are returned in VRs starting at v2. */
a1da2672
UW
1895 if (TYPE_LENGTH (valtype) == 16
1896 && TYPE_CODE (valtype) == TYPE_CODE_ARRAY && TYPE_VECTOR (valtype)
e765b44c 1897 && tdep->vector_abi == POWERPC_VEC_ALTIVEC)
afd48b75 1898 {
e765b44c
UW
1899 int regnum = tdep->ppc_vr0_regnum + 2 + index;
1900
1901 if (writebuf != NULL)
1902 regcache_cooked_write (regcache, regnum, writebuf);
1903 if (readbuf != NULL)
1904 regcache_cooked_read (regcache, regnum, readbuf);
1905 return 1;
afd48b75 1906 }
e765b44c 1907
a1da2672
UW
1908 /* Short vectors are returned in GPRs starting at r3. */
1909 if (TYPE_LENGTH (valtype) <= 8
1910 && TYPE_CODE (valtype) == TYPE_CODE_ARRAY && TYPE_VECTOR (valtype))
1911 {
1912 int regnum = tdep->ppc_gp0_regnum + 3 + index;
1913 int offset = 0;
1914
1915 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
1916 offset = 8 - TYPE_LENGTH (valtype);
1917
1918 if (writebuf != NULL)
1919 regcache_cooked_write_part (regcache, regnum,
1920 offset, TYPE_LENGTH (valtype), writebuf);
1921 if (readbuf != NULL)
1922 regcache_cooked_read_part (regcache, regnum,
1923 offset, TYPE_LENGTH (valtype), readbuf);
1924 return 1;
1925 }
1926
e765b44c
UW
1927 return 0;
1928}
1929
1930/* The 64 bit ABI return value convention.
1931
1932 Return non-zero if the return-value is stored in a register, return
1933 0 if the return-value is instead stored on the stack (a.k.a.,
1934 struct return convention).
1935
1936 For a return-value stored in a register: when WRITEBUF is non-NULL,
1937 copy the buffer to the corresponding register return-value location
1938 location; when READBUF is non-NULL, fill the buffer from the
1939 corresponding register return-value location. */
1940enum return_value_convention
1941ppc64_sysv_abi_return_value (struct gdbarch *gdbarch, struct value *function,
1942 struct type *valtype, struct regcache *regcache,
1943 gdb_byte *readbuf, const gdb_byte *writebuf)
1944{
1945 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1946 struct type *func_type = function ? value_type (function) : NULL;
1947 int opencl_abi = func_type? ppc_sysv_use_opencl_abi (func_type) : 0;
1948 struct type *eltype;
1949 int nelt, i, ok;
1950
1951 /* This function exists to support a calling convention that
1952 requires floating-point registers. It shouldn't be used on
1953 processors that lack them. */
1954 gdb_assert (ppc_floating_point_unit_p (gdbarch));
1955
1956 /* Complex types are returned as if two independent scalars. */
1957 if (TYPE_CODE (valtype) == TYPE_CODE_COMPLEX)
afd48b75 1958 {
e765b44c
UW
1959 eltype = check_typedef (TYPE_TARGET_TYPE (valtype));
1960
1961 for (i = 0; i < 2; i++)
afd48b75 1962 {
e765b44c
UW
1963 ok = ppc64_sysv_abi_return_value_base (gdbarch, eltype, regcache,
1964 readbuf, writebuf, i);
1965 gdb_assert (ok);
1966
1967 if (readbuf)
1968 readbuf += TYPE_LENGTH (eltype);
1969 if (writebuf)
1970 writebuf += TYPE_LENGTH (eltype);
afd48b75
AC
1971 }
1972 return RETURN_VALUE_REGISTER_CONVENTION;
1973 }
e765b44c
UW
1974
1975 /* OpenCL vectors shorter than 16 bytes are returned as if
1976 a series of independent scalars; OpenCL vectors 16 bytes
1977 or longer are returned as if a series of AltiVec vectors. */
1978 if (TYPE_CODE (valtype) == TYPE_CODE_ARRAY && TYPE_VECTOR (valtype)
1979 && opencl_abi)
afd48b75 1980 {
e765b44c
UW
1981 if (TYPE_LENGTH (valtype) < 16)
1982 eltype = check_typedef (TYPE_TARGET_TYPE (valtype));
1983 else
1984 eltype = register_type (gdbarch, tdep->ppc_vr0_regnum);
1985
1986 nelt = TYPE_LENGTH (valtype) / TYPE_LENGTH (eltype);
1987 for (i = 0; i < nelt; i++)
afd48b75 1988 {
e765b44c
UW
1989 ok = ppc64_sysv_abi_return_value_base (gdbarch, eltype, regcache,
1990 readbuf, writebuf, i);
1991 gdb_assert (ok);
1992
1993 if (readbuf)
1994 readbuf += TYPE_LENGTH (eltype);
1995 if (writebuf)
1996 writebuf += TYPE_LENGTH (eltype);
afd48b75
AC
1997 }
1998 return RETURN_VALUE_REGISTER_CONVENTION;
1999 }
e765b44c
UW
2000
2001 /* All pointers live in r3. */
2002 if (TYPE_CODE (valtype) == TYPE_CODE_PTR
2003 || TYPE_CODE (valtype) == TYPE_CODE_REF)
afd48b75 2004 {
e765b44c
UW
2005 int regnum = tdep->ppc_gp0_regnum + 3;
2006
2007 if (writebuf != NULL)
2008 regcache_cooked_write (regcache, regnum, writebuf);
2009 if (readbuf != NULL)
2010 regcache_cooked_read (regcache, regnum, readbuf);
afd48b75
AC
2011 return RETURN_VALUE_REGISTER_CONVENTION;
2012 }
e765b44c
UW
2013
2014 /* Small character arrays are returned, right justified, in r3. */
2015 if (TYPE_CODE (valtype) == TYPE_CODE_ARRAY
a1da2672 2016 && !TYPE_VECTOR (valtype)
e765b44c
UW
2017 && TYPE_LENGTH (valtype) <= 8
2018 && TYPE_CODE (TYPE_TARGET_TYPE (valtype)) == TYPE_CODE_INT
2019 && TYPE_LENGTH (TYPE_TARGET_TYPE (valtype)) == 1)
2020 {
2021 int regnum = tdep->ppc_gp0_regnum + 3;
2022 int offset = (register_size (gdbarch, regnum) - TYPE_LENGTH (valtype));
2023
2024 if (writebuf != NULL)
2025 regcache_cooked_write_part (regcache, regnum,
2026 offset, TYPE_LENGTH (valtype), writebuf);
2027 if (readbuf != NULL)
2028 regcache_cooked_read_part (regcache, regnum,
2029 offset, TYPE_LENGTH (valtype), readbuf);
2030 return RETURN_VALUE_REGISTER_CONVENTION;
2031 }
2032
cc0e89c5
UW
2033 /* In the ELFv2 ABI, homogeneous floating-point or vector
2034 aggregates are returned in registers. */
2035 if (tdep->elf_abi == POWERPC_ELF_V2
a1da2672
UW
2036 && ppc64_elfv2_abi_homogeneous_aggregate (valtype, &eltype, &nelt)
2037 && (TYPE_CODE (eltype) == TYPE_CODE_FLT
2038 || TYPE_CODE (eltype) == TYPE_CODE_DECFLOAT
2039 || (TYPE_CODE (eltype) == TYPE_CODE_ARRAY
2040 && TYPE_VECTOR (eltype)
2041 && tdep->vector_abi == POWERPC_VEC_ALTIVEC
2042 && TYPE_LENGTH (eltype) == 16)))
cc0e89c5
UW
2043 {
2044 for (i = 0; i < nelt; i++)
2045 {
2046 ok = ppc64_sysv_abi_return_value_base (gdbarch, eltype, regcache,
2047 readbuf, writebuf, i);
2048 gdb_assert (ok);
2049
2050 if (readbuf)
2051 readbuf += TYPE_LENGTH (eltype);
2052 if (writebuf)
2053 writebuf += TYPE_LENGTH (eltype);
2054 }
2055
2056 return RETURN_VALUE_REGISTER_CONVENTION;
2057 }
2058
2059 /* In the ELFv2 ABI, aggregate types of up to 16 bytes are
2060 returned in registers r3:r4. */
2061 if (tdep->elf_abi == POWERPC_ELF_V2
2062 && TYPE_LENGTH (valtype) <= 16
2063 && (TYPE_CODE (valtype) == TYPE_CODE_STRUCT
2064 || TYPE_CODE (valtype) == TYPE_CODE_UNION
2065 || (TYPE_CODE (valtype) == TYPE_CODE_ARRAY
2066 && !TYPE_VECTOR (valtype))))
2067 {
2068 int n_regs = ((TYPE_LENGTH (valtype) + tdep->wordsize - 1)
2069 / tdep->wordsize);
2070 int i;
2071
2072 for (i = 0; i < n_regs; i++)
2073 {
2074 gdb_byte regval[MAX_REGISTER_SIZE];
2075 int regnum = tdep->ppc_gp0_regnum + 3 + i;
2076 int offset = i * tdep->wordsize;
2077 int len = TYPE_LENGTH (valtype) - offset;
2078
2079 if (len > tdep->wordsize)
2080 len = tdep->wordsize;
2081
2082 if (writebuf != NULL)
2083 {
2084 memset (regval, 0, sizeof regval);
2085 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG
2086 && offset == 0)
2087 memcpy (regval + tdep->wordsize - len, writebuf, len);
2088 else
2089 memcpy (regval, writebuf + offset, len);
2090 regcache_cooked_write (regcache, regnum, regval);
2091 }
2092 if (readbuf != NULL)
2093 {
2094 regcache_cooked_read (regcache, regnum, regval);
2095 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG
2096 && offset == 0)
2097 memcpy (readbuf, regval + tdep->wordsize - len, len);
2098 else
2099 memcpy (readbuf + offset, regval, len);
2100 }
2101 }
2102 return RETURN_VALUE_REGISTER_CONVENTION;
2103 }
2104
e765b44c
UW
2105 /* Handle plain base types. */
2106 if (ppc64_sysv_abi_return_value_base (gdbarch, valtype, regcache,
2107 readbuf, writebuf, 0))
2108 return RETURN_VALUE_REGISTER_CONVENTION;
2109
afd48b75
AC
2110 return RETURN_VALUE_STRUCT_CONVENTION;
2111}
2112
This page took 1.41332 seconds and 4 git commands to generate.