1 /* Target-dependent code for PowerPC systems using the SVR4 ABI
2 for GDB, the GNU debugger.
4 Copyright (C) 2000-2015 Free Software Foundation, Inc.
6 This file is part of GDB.
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
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
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.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
33 /* Check whether FTPYE is a (pointer to) function type that should use
34 the OpenCL vector ABI. */
37 ppc_sysv_use_opencl_abi (struct type *ftype)
39 ftype = check_typedef (ftype);
41 if (TYPE_CODE (ftype) == TYPE_CODE_PTR)
42 ftype = check_typedef (TYPE_TARGET_TYPE (ftype));
44 return (TYPE_CODE (ftype) == TYPE_CODE_FUNC
45 && TYPE_CALLING_CONVENTION (ftype) == DW_CC_GDB_IBM_OpenCL);
48 /* Pass the arguments in either registers, or in the stack. Using the
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
52 passed in fpr's, in addition to that. Rest of the parameters if any
53 are passed in user stack.
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,
60 ppc_sysv_abi_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
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)
65 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
66 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
67 int opencl_abi = ppc_sysv_use_opencl_abi (value_type (function));
69 int argspace = 0; /* 0 is an initial wrong guess. */
72 gdb_assert (tdep->wordsize == 4);
74 regcache_cooked_read_unsigned (regcache, gdbarch_sp_regnum (gdbarch),
77 /* Go through the argument list twice.
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
85 Pass 2: Replay the same computation but this time also write the
86 values out to the target. */
88 for (write_pass = 0; write_pass < 2; write_pass++)
91 /* Next available floating point register for float and double
94 /* Next available general register for non-float, non-vector
97 /* Next available vector register for vector arguments. */
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;
104 /* If the function is returning a `struct', then the first word
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. */
111 regcache_cooked_write_signed (regcache,
112 tdep->ppc_gp0_regnum + greg,
117 for (argno = 0; argno < nargs; argno++)
119 struct value *arg = args[argno];
120 struct type *type = check_typedef (value_type (arg));
121 int len = TYPE_LENGTH (type);
122 const bfd_byte *val = value_contents (arg);
124 if (TYPE_CODE (type) == TYPE_CODE_FLT && len <= 8
125 && !tdep->soft_float)
127 /* Floating point value converted to "double" then
128 passed in an FP register, when the registers run out,
129 8 byte aligned stack is used. */
134 /* Always store the floating point value using
135 the register's floating-point format. */
136 gdb_byte regval[MAX_REGISTER_SIZE];
138 = register_type (gdbarch, tdep->ppc_fp0_regnum + freg);
139 convert_typed_floating (val, type, regval, regtype);
140 regcache_cooked_write (regcache,
141 tdep->ppc_fp0_regnum + freg,
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
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);
161 write_memory (sp + argoffset, val, len);
165 else if (TYPE_CODE (type) == TYPE_CODE_FLT
168 && (gdbarch_long_double_format (gdbarch)
169 == floatformats_ibm_long_double))
171 /* IBM long double passed in two FP registers if
172 available, otherwise 8-byte aligned stack. */
177 regcache_cooked_write (regcache,
178 tdep->ppc_fp0_regnum + freg,
180 regcache_cooked_write (regcache,
181 tdep->ppc_fp0_regnum + freg + 1,
188 argoffset = align_up (argoffset, 8);
190 write_memory (sp + argoffset, val, len);
195 && (TYPE_CODE (type) == TYPE_CODE_INT /* long long */
196 || TYPE_CODE (type) == TYPE_CODE_FLT /* double */
197 || (TYPE_CODE (type) == TYPE_CODE_DECFLOAT
198 && tdep->soft_float)))
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
208 /* Just in case GREG was 10. */
210 argoffset = align_up (argoffset, 8);
212 write_memory (sp + argoffset, val, len);
217 /* Must start on an odd register - r3/r4 etc. */
222 regcache_cooked_write (regcache,
223 tdep->ppc_gp0_regnum + greg + 0,
225 regcache_cooked_write (regcache,
226 tdep->ppc_gp0_regnum + greg + 1,
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)))
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. */
245 argoffset = align_up (argoffset, 8);
247 write_memory (sp + argoffset, val, len);
254 regcache_cooked_write (regcache,
255 tdep->ppc_gp0_regnum + greg + 0,
257 regcache_cooked_write (regcache,
258 tdep->ppc_gp0_regnum + greg + 1,
260 regcache_cooked_write (regcache,
261 tdep->ppc_gp0_regnum + greg + 2,
263 regcache_cooked_write (regcache,
264 tdep->ppc_gp0_regnum + greg + 3,
270 else if (TYPE_CODE (type) == TYPE_CODE_DECFLOAT && len <= 8
271 && !tdep->soft_float)
273 /* 32-bit and 64-bit decimal floats go in f1 .. f8. They can
280 gdb_byte regval[MAX_REGISTER_SIZE];
283 /* 32-bit decimal floats are right aligned in the
285 if (TYPE_LENGTH (type) == 4)
287 memcpy (regval + 4, val, 4);
293 regcache_cooked_write (regcache,
294 tdep->ppc_fp0_regnum + freg, p);
301 argoffset = align_up (argoffset, len);
304 /* Write value in the stack's parameter save area. */
305 write_memory (sp + argoffset, val, len);
310 else if (TYPE_CODE (type) == TYPE_CODE_DECFLOAT && len == 16
311 && !tdep->soft_float)
313 /* 128-bit decimal floats go in f2 .. f7, always in even/odd
314 pairs. They can end up in memory, using two doublewords. */
318 /* Make sure freg is even. */
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);
331 argoffset = align_up (argoffset, 8);
334 write_memory (sp + argoffset, val, 16);
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. */
346 && TYPE_CODE (type) == TYPE_CODE_ARRAY
347 && TYPE_VECTOR (type)
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);
355 for (i = 0; i < nelt; i++)
357 const gdb_byte *elval = val + i * TYPE_LENGTH (eltype);
359 if (TYPE_CODE (eltype) == TYPE_CODE_FLT && !tdep->soft_float)
365 int regnum = tdep->ppc_fp0_regnum + freg;
366 gdb_byte regval[MAX_REGISTER_SIZE];
368 = register_type (gdbarch, regnum);
369 convert_typed_floating (elval, eltype,
371 regcache_cooked_write (regcache, regnum, regval);
377 argoffset = align_up (argoffset, len);
379 write_memory (sp + argoffset, val, len);
383 else if (TYPE_LENGTH (eltype) == 8)
387 /* Just in case GREG was 10. */
389 argoffset = align_up (argoffset, 8);
391 write_memory (sp + argoffset, elval,
392 TYPE_LENGTH (eltype));
397 /* Must start on an odd register - r3/r4 etc. */
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);
413 gdb_byte word[MAX_REGISTER_SIZE];
414 store_unsigned_integer (word, tdep->wordsize, byte_order,
415 unpack_long (eltype, elval));
420 regcache_cooked_write (regcache,
421 tdep->ppc_gp0_regnum + greg,
427 argoffset = align_up (argoffset, tdep->wordsize);
429 write_memory (sp + argoffset, word, tdep->wordsize);
430 argoffset += tdep->wordsize;
436 && TYPE_CODE (type) == TYPE_CODE_ARRAY
437 && TYPE_VECTOR (type)
440 /* OpenCL vectors 16 bytes or longer are passed as if
441 a series of AltiVec vectors. */
444 for (i = 0; i < len / 16; i++)
446 const gdb_byte *elval = val + i * 16;
451 regcache_cooked_write (regcache,
452 tdep->ppc_vr0_regnum + vreg,
458 argoffset = align_up (argoffset, 16);
460 write_memory (sp + argoffset, elval, 16);
466 && TYPE_CODE (type) == TYPE_CODE_ARRAY
467 && TYPE_VECTOR (type)
468 && tdep->vector_abi == POWERPC_VEC_ALTIVEC)
470 /* Vector parameter passed in an Altivec register, or
471 when that runs out, 16 byte aligned stack location. */
475 regcache_cooked_write (regcache,
476 tdep->ppc_vr0_regnum + vreg, val);
481 argoffset = align_up (argoffset, 16);
483 write_memory (sp + argoffset, val, 16);
488 && TYPE_CODE (type) == TYPE_CODE_ARRAY
489 && TYPE_VECTOR (type)
490 && tdep->vector_abi == POWERPC_VEC_SPE)
492 /* Vector parameter passed in an e500 register, or when
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. */
502 regcache_cooked_write (regcache,
503 tdep->ppc_ev0_regnum + greg, val);
508 argoffset = align_up (argoffset, 8);
510 write_memory (sp + argoffset, val, 8);
516 /* Reduce the parameter down to something that fits in a
518 gdb_byte word[MAX_REGISTER_SIZE];
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)
524 /* Structs and large values are put in an
525 aligned stack slot ... */
526 if (TYPE_CODE (type) == TYPE_CODE_ARRAY
527 && TYPE_VECTOR (type)
529 structoffset = align_up (structoffset, 16);
531 structoffset = align_up (structoffset, 8);
534 write_memory (sp + structoffset, val, len);
535 /* ... and then a "word" pointing to that address is
536 passed as the parameter. */
537 store_unsigned_integer (word, tdep->wordsize, byte_order,
541 else if (TYPE_CODE (type) == TYPE_CODE_INT)
542 /* Sign or zero extend the "int" into a "word". */
543 store_unsigned_integer (word, tdep->wordsize, byte_order,
544 unpack_long (type, val));
546 /* Always goes in the low address. */
547 memcpy (word, val, len);
548 /* Store that "word" in a register, or on the stack.
549 The words have "4" byte alignment. */
553 regcache_cooked_write (regcache,
554 tdep->ppc_gp0_regnum + greg, word);
559 argoffset = align_up (argoffset, tdep->wordsize);
561 write_memory (sp + argoffset, word, tdep->wordsize);
562 argoffset += tdep->wordsize;
567 /* Compute the actual stack space requirements. */
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);
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
581 registers. It is strongly recommended that the caller set the
582 bit to 0 otherwise..." Doing this for normal functions too
588 regcache_cooked_read_unsigned (regcache, tdep->ppc_cr_regnum, &cr);
593 regcache_cooked_write_unsigned (regcache, tdep->ppc_cr_regnum, cr);
598 regcache_cooked_write_signed (regcache, gdbarch_sp_regnum (gdbarch), sp);
600 /* Write the backchain (it occupies WORDSIZED bytes). */
601 write_memory_signed_integer (sp, tdep->wordsize, byte_order, saved_sp);
603 /* Point the inferior function call's return address at the dummy's
605 regcache_cooked_write_signed (regcache, tdep->ppc_lr_regnum, bp_addr);
610 /* Handle the return-value conventions for Decimal Floating Point values. */
611 static enum return_value_convention
612 get_decimal_float_return_value (struct gdbarch *gdbarch, struct type *valtype,
613 struct regcache *regcache, gdb_byte *readbuf,
614 const gdb_byte *writebuf)
616 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
618 gdb_assert (TYPE_CODE (valtype) == TYPE_CODE_DECFLOAT);
620 /* 32-bit and 64-bit decimal floats in f1. */
621 if (TYPE_LENGTH (valtype) <= 8)
623 if (writebuf != NULL)
625 gdb_byte regval[MAX_REGISTER_SIZE];
628 /* 32-bit decimal float is right aligned in the doubleword. */
629 if (TYPE_LENGTH (valtype) == 4)
631 memcpy (regval + 4, writebuf, 4);
637 regcache_cooked_write (regcache, tdep->ppc_fp0_regnum + 1, p);
641 regcache_cooked_read (regcache, tdep->ppc_fp0_regnum + 1, readbuf);
643 /* Left align 32-bit decimal float. */
644 if (TYPE_LENGTH (valtype) == 4)
645 memcpy (readbuf, readbuf + 4, 4);
648 /* 128-bit decimal floats in f2,f3. */
649 else if (TYPE_LENGTH (valtype) == 16)
651 if (writebuf != NULL || readbuf != NULL)
655 for (i = 0; i < 2; i++)
657 if (writebuf != NULL)
658 regcache_cooked_write (regcache, tdep->ppc_fp0_regnum + 2 + i,
661 regcache_cooked_read (regcache, tdep->ppc_fp0_regnum + 2 + i,
668 internal_error (__FILE__, __LINE__, _("Unknown decimal float size."));
670 return RETURN_VALUE_REGISTER_CONVENTION;
673 /* Handle the return-value conventions specified by the SysV 32-bit
674 PowerPC ABI (including all the supplements):
676 no floating-point: floating-point values returned using 32-bit
677 general-purpose registers.
679 Altivec: 128-bit vectors returned using vector registers.
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.
685 GCC (broken): Small struct values right (instead of left) aligned
686 when returned in general-purpose registers. */
688 static enum return_value_convention
689 do_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,
694 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
695 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
696 int opencl_abi = func_type? ppc_sysv_use_opencl_abi (func_type) : 0;
698 gdb_assert (tdep->wordsize == 4);
700 if (TYPE_CODE (type) == TYPE_CODE_FLT
701 && TYPE_LENGTH (type) <= 8
702 && !tdep->soft_float)
706 /* Floats and doubles stored in "f1". Convert the value to
707 the required type. */
708 gdb_byte regval[MAX_REGISTER_SIZE];
709 struct type *regtype = register_type (gdbarch,
710 tdep->ppc_fp0_regnum + 1);
711 regcache_cooked_read (regcache, tdep->ppc_fp0_regnum + 1, regval);
712 convert_typed_floating (regval, regtype, readbuf, type);
716 /* Floats and doubles stored in "f1". Convert the value to
717 the register's "double" type. */
718 gdb_byte regval[MAX_REGISTER_SIZE];
719 struct type *regtype = register_type (gdbarch, tdep->ppc_fp0_regnum);
720 convert_typed_floating (writebuf, type, regval, regtype);
721 regcache_cooked_write (regcache, tdep->ppc_fp0_regnum + 1, regval);
723 return RETURN_VALUE_REGISTER_CONVENTION;
725 if (TYPE_CODE (type) == TYPE_CODE_FLT
726 && TYPE_LENGTH (type) == 16
728 && (gdbarch_long_double_format (gdbarch)
729 == floatformats_ibm_long_double))
731 /* IBM long double stored in f1 and f2. */
734 regcache_cooked_read (regcache, tdep->ppc_fp0_regnum + 1, readbuf);
735 regcache_cooked_read (regcache, tdep->ppc_fp0_regnum + 2,
740 regcache_cooked_write (regcache, tdep->ppc_fp0_regnum + 1, writebuf);
741 regcache_cooked_write (regcache, tdep->ppc_fp0_regnum + 2,
744 return RETURN_VALUE_REGISTER_CONVENTION;
746 if (TYPE_LENGTH (type) == 16
747 && ((TYPE_CODE (type) == TYPE_CODE_FLT
748 && (gdbarch_long_double_format (gdbarch)
749 == floatformats_ibm_long_double))
750 || (TYPE_CODE (type) == TYPE_CODE_DECFLOAT && tdep->soft_float)))
752 /* Soft-float IBM long double or _Decimal128 stored in r3, r4,
756 regcache_cooked_read (regcache, tdep->ppc_gp0_regnum + 3, readbuf);
757 regcache_cooked_read (regcache, tdep->ppc_gp0_regnum + 4,
759 regcache_cooked_read (regcache, tdep->ppc_gp0_regnum + 5,
761 regcache_cooked_read (regcache, tdep->ppc_gp0_regnum + 6,
766 regcache_cooked_write (regcache, tdep->ppc_gp0_regnum + 3, writebuf);
767 regcache_cooked_write (regcache, tdep->ppc_gp0_regnum + 4,
769 regcache_cooked_write (regcache, tdep->ppc_gp0_regnum + 5,
771 regcache_cooked_write (regcache, tdep->ppc_gp0_regnum + 6,
774 return RETURN_VALUE_REGISTER_CONVENTION;
776 if ((TYPE_CODE (type) == TYPE_CODE_INT && TYPE_LENGTH (type) == 8)
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))
783 /* A long long, double or _Decimal64 stored in the 32 bit
785 regcache_cooked_read (regcache, tdep->ppc_gp0_regnum + 3,
787 regcache_cooked_read (regcache, tdep->ppc_gp0_regnum + 4,
792 /* A long long, double or _Decimal64 stored in the 32 bit
794 regcache_cooked_write (regcache, tdep->ppc_gp0_regnum + 3,
796 regcache_cooked_write (regcache, tdep->ppc_gp0_regnum + 4,
799 return RETURN_VALUE_REGISTER_CONVENTION;
801 if (TYPE_CODE (type) == TYPE_CODE_DECFLOAT && !tdep->soft_float)
802 return get_decimal_float_return_value (gdbarch, type, regcache, readbuf,
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)
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. */
818 regcache_cooked_read_unsigned (regcache, tdep->ppc_gp0_regnum + 3,
820 store_unsigned_integer (readbuf, TYPE_LENGTH (type), byte_order,
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,
828 unpack_long (type, writebuf));
830 return RETURN_VALUE_REGISTER_CONVENTION;
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
839 struct type *eltype = check_typedef (TYPE_TARGET_TYPE (type));
840 int i, nelt = TYPE_LENGTH (type) / TYPE_LENGTH (eltype);
842 for (i = 0; i < nelt; i++)
844 int offset = i * TYPE_LENGTH (eltype);
846 if (TYPE_CODE (eltype) == TYPE_CODE_FLT)
848 int regnum = tdep->ppc_fp0_regnum + 1 + i;
849 gdb_byte regval[MAX_REGISTER_SIZE];
850 struct type *regtype = register_type (gdbarch, regnum);
852 if (writebuf != NULL)
854 convert_typed_floating (writebuf + offset, eltype,
856 regcache_cooked_write (regcache, regnum, regval);
860 regcache_cooked_read (regcache, regnum, regval);
861 convert_typed_floating (regval, regtype,
862 readbuf + offset, eltype);
867 int regnum = tdep->ppc_gp0_regnum + 3 + i;
870 if (writebuf != NULL)
872 regval = unpack_long (eltype, writebuf + offset);
873 regcache_cooked_write_unsigned (regcache, regnum, regval);
877 regcache_cooked_read_unsigned (regcache, regnum, ®val);
878 store_unsigned_integer (readbuf + offset,
879 TYPE_LENGTH (eltype), byte_order,
885 return RETURN_VALUE_REGISTER_CONVENTION;
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
893 int n_regs = TYPE_LENGTH (type) / 16;
896 for (i = 0; i < n_regs; i++)
899 int regnum = tdep->ppc_vr0_regnum + 2 + i;
901 if (writebuf != NULL)
902 regcache_cooked_write (regcache, regnum, writebuf + offset);
904 regcache_cooked_read (regcache, regnum, readbuf + offset);
907 return RETURN_VALUE_REGISTER_CONVENTION;
909 if (TYPE_LENGTH (type) == 16
910 && TYPE_CODE (type) == TYPE_CODE_ARRAY
911 && TYPE_VECTOR (type)
912 && tdep->vector_abi == POWERPC_VEC_ALTIVEC)
916 /* Altivec places the return value in "v2". */
917 regcache_cooked_read (regcache, tdep->ppc_vr0_regnum + 2, readbuf);
921 /* Altivec places the return value in "v2". */
922 regcache_cooked_write (regcache, tdep->ppc_vr0_regnum + 2, writebuf);
924 return RETURN_VALUE_REGISTER_CONVENTION;
926 if (TYPE_LENGTH (type) == 16
927 && TYPE_CODE (type) == TYPE_CODE_ARRAY
928 && TYPE_VECTOR (type)
929 && tdep->vector_abi == POWERPC_VEC_GENERIC)
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. */
936 regcache_cooked_read (regcache, tdep->ppc_gp0_regnum + 3,
938 regcache_cooked_read (regcache, tdep->ppc_gp0_regnum + 4,
940 regcache_cooked_read (regcache, tdep->ppc_gp0_regnum + 5,
942 regcache_cooked_read (regcache, tdep->ppc_gp0_regnum + 6,
947 regcache_cooked_write (regcache, tdep->ppc_gp0_regnum + 3,
949 regcache_cooked_write (regcache, tdep->ppc_gp0_regnum + 4,
951 regcache_cooked_write (regcache, tdep->ppc_gp0_regnum + 5,
953 regcache_cooked_write (regcache, tdep->ppc_gp0_regnum + 6,
956 return RETURN_VALUE_REGISTER_CONVENTION;
958 if (TYPE_LENGTH (type) == 8
959 && TYPE_CODE (type) == TYPE_CODE_ARRAY
960 && TYPE_VECTOR (type)
961 && tdep->vector_abi == POWERPC_VEC_SPE)
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. */
969 regcache_cooked_read (regcache, tdep->ppc_ev0_regnum + 3, readbuf);
971 regcache_cooked_write (regcache, tdep->ppc_ev0_regnum + 3, writebuf);
972 return RETURN_VALUE_REGISTER_CONVENTION;
974 if (broken_gcc && TYPE_LENGTH (type) <= 8)
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;
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);
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);
1003 return RETURN_VALUE_REGISTER_CONVENTION;
1005 if (TYPE_LENGTH (type) <= 8)
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. */
1012 gdb_byte regvals[MAX_REGISTER_SIZE * 2];
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);
1018 memcpy (readbuf, regvals, TYPE_LENGTH (type));
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. */
1025 gdb_byte regvals[MAX_REGISTER_SIZE * 2];
1026 memset (regvals, 0, sizeof regvals);
1027 memcpy (regvals, writebuf, TYPE_LENGTH (type));
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);
1034 return RETURN_VALUE_REGISTER_CONVENTION;
1036 return RETURN_VALUE_STRUCT_CONVENTION;
1039 enum return_value_convention
1040 ppc_sysv_abi_return_value (struct gdbarch *gdbarch, struct value *function,
1041 struct type *valtype, struct regcache *regcache,
1042 gdb_byte *readbuf, const gdb_byte *writebuf)
1044 return do_ppc_sysv_return_value (gdbarch,
1045 function ? value_type (function) : NULL,
1046 valtype, regcache, readbuf, writebuf, 0);
1049 enum return_value_convention
1050 ppc_sysv_abi_broken_return_value (struct gdbarch *gdbarch,
1051 struct value *function,
1052 struct type *valtype,
1053 struct regcache *regcache,
1054 gdb_byte *readbuf, const gdb_byte *writebuf)
1056 return do_ppc_sysv_return_value (gdbarch,
1057 function ? value_type (function) : NULL,
1058 valtype, regcache, readbuf, writebuf, 1);
1061 /* The helper function for 64-bit SYSV push_dummy_call. Converts the
1062 function's code address back into the function's descriptor
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"). */
1073 convert_code_addr_to_desc_addr (CORE_ADDR code_addr, CORE_ADDR *desc_addr)
1075 struct obj_section *dot_fn_section;
1076 struct bound_minimal_symbol dot_fn;
1077 struct bound_minimal_symbol fn;
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);
1082 if (dot_fn.minsym == NULL || MSYMBOL_LINKAGE_NAME (dot_fn.minsym)[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)
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. */
1093 fn = lookup_minimal_symbol (MSYMBOL_LINKAGE_NAME (dot_fn.minsym) + 1, NULL,
1094 dot_fn_section->objfile);
1095 if (fn.minsym == NULL)
1097 /* Found a descriptor. */
1098 (*desc_addr) = BMSYMBOL_VALUE_ADDRESS (fn);
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
1110 ppc64_aggregate_candidate (struct type *type,
1111 struct type **field_type)
1113 type = check_typedef (type);
1115 switch (TYPE_CODE (type))
1118 case TYPE_CODE_DECFLOAT:
1121 if (TYPE_CODE (*field_type) == TYPE_CODE (type)
1122 && TYPE_LENGTH (*field_type) == TYPE_LENGTH (type))
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)
1133 if (TYPE_CODE (*field_type) == TYPE_CODE (type)
1134 && TYPE_LENGTH (*field_type) == TYPE_LENGTH (type))
1139 case TYPE_CODE_ARRAY:
1140 if (TYPE_VECTOR (type))
1144 if (TYPE_CODE (*field_type) == TYPE_CODE (type)
1145 && TYPE_LENGTH (*field_type) == TYPE_LENGTH (type))
1150 LONGEST count, low_bound, high_bound;
1152 count = ppc64_aggregate_candidate
1153 (TYPE_TARGET_TYPE (type), field_type);
1157 if (!get_array_bounds (type, &low_bound, &high_bound))
1159 count *= high_bound - low_bound;
1161 /* There must be no padding. */
1163 return TYPE_LENGTH (type) == 0 ? 0 : -1;
1164 else if (TYPE_LENGTH (type) != count * TYPE_LENGTH (*field_type))
1171 case TYPE_CODE_STRUCT:
1172 case TYPE_CODE_UNION:
1177 for (i = 0; i < TYPE_NFIELDS (type); i++)
1181 if (field_is_static (&TYPE_FIELD (type, i)))
1184 sub_count = ppc64_aggregate_candidate
1185 (TYPE_FIELD_TYPE (type, i), field_type);
1186 if (sub_count == -1)
1189 if (TYPE_CODE (type) == TYPE_CODE_STRUCT)
1192 count = max (count, sub_count);
1195 /* There must be no padding. */
1197 return TYPE_LENGTH (type) == 0 ? 0 : -1;
1198 else if (TYPE_LENGTH (type) != count * TYPE_LENGTH (*field_type))
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. */
1218 ppc64_elfv2_abi_homogeneous_aggregate (struct type *type,
1219 struct type **elt_type, int *n_elts)
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)))
1227 struct type *field_type = NULL;
1228 LONGEST field_count = ppc64_aggregate_candidate (type, &field_type);
1230 if (field_count > 0)
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);
1236 /* The ELFv2 ABI allows homogeneous aggregates to occupy
1237 up to 8 registers. */
1238 if (field_count * n_regs <= 8)
1241 *elt_type = field_type;
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. */
1256 /* Structure holding the next argument position. */
1257 struct ppc64_sysv_argpos
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. */
1265 /* Next available floating-point argument register. */
1267 /* Next available vector argument register. */
1269 /* The address, at which the next general purpose parameter
1270 (integer, struct, float, vector, ...) should be saved. */
1272 /* The address, at which the next by-reference parameter
1273 (non-Altivec vector, variably-sized type) should be saved. */
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.
1281 If ALIGN is nonzero, it specifies the minimum alignment required
1282 for the on-stack copy of the argument. */
1285 ppc64_sysv_abi_push_val (struct gdbarch *gdbarch,
1286 const bfd_byte *val, int len, int align,
1287 struct ppc64_sysv_argpos *argpos)
1289 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1292 /* Enforce alignment of stack location, if requested. */
1293 if (align > tdep->wordsize)
1295 CORE_ADDR aligned_gparam = align_up (argpos->gparam, align);
1297 argpos->greg += (aligned_gparam - argpos->gparam) / tdep->wordsize;
1298 argpos->gparam = aligned_gparam;
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>. */
1305 if (len < tdep->wordsize
1306 && gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
1307 offset = tdep->wordsize - len;
1309 if (argpos->regcache)
1310 write_memory (argpos->gparam + offset, val, len);
1311 argpos->gparam = align_up (argpos->gparam + len, tdep->wordsize);
1313 while (len >= tdep->wordsize)
1315 if (argpos->regcache && argpos->greg <= 10)
1316 regcache_cooked_write (argpos->regcache,
1317 tdep->ppc_gp0_regnum + argpos->greg, val);
1319 len -= tdep->wordsize;
1320 val += tdep->wordsize;
1325 if (argpos->regcache && argpos->greg <= 10)
1326 regcache_cooked_write_part (argpos->regcache,
1327 tdep->ppc_gp0_regnum + argpos->greg,
1333 /* The same as ppc64_sysv_abi_push_val, but using a single-word integer
1334 value VAL as argument. */
1337 ppc64_sysv_abi_push_integer (struct gdbarch *gdbarch, ULONGEST val,
1338 struct ppc64_sysv_argpos *argpos)
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];
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);
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. */
1354 ppc64_sysv_abi_push_freg (struct gdbarch *gdbarch,
1355 struct type *type, const bfd_byte *val,
1356 struct ppc64_sysv_argpos *argpos)
1358 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1359 if (tdep->soft_float)
1362 if (TYPE_LENGTH (type) <= 8
1363 && TYPE_CODE (type) == TYPE_CODE_FLT)
1365 /* Floats and doubles go in f1 .. f13. 32-bit floats are converted
1367 if (argpos->regcache && argpos->freg <= 13)
1369 int regnum = tdep->ppc_fp0_regnum + argpos->freg;
1370 struct type *regtype = register_type (gdbarch, regnum);
1371 gdb_byte regval[MAX_REGISTER_SIZE];
1373 convert_typed_floating (val, type, regval, regtype);
1374 regcache_cooked_write (argpos->regcache, regnum, regval);
1379 else if (TYPE_LENGTH (type) <= 8
1380 && TYPE_CODE (type) == TYPE_CODE_DECFLOAT)
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)
1386 int regnum = tdep->ppc_fp0_regnum + argpos->freg;
1389 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
1390 offset = 8 - TYPE_LENGTH (type);
1392 regcache_cooked_write_part (argpos->regcache, regnum,
1393 offset, TYPE_LENGTH (type), val);
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))
1403 /* IBM long double stored in two consecutive FPRs. */
1404 if (argpos->regcache && argpos->freg <= 13)
1406 int regnum = tdep->ppc_fp0_regnum + argpos->freg;
1408 regcache_cooked_write (argpos->regcache, regnum, val);
1409 if (argpos->freg <= 12)
1410 regcache_cooked_write (argpos->regcache, regnum + 1, val + 8);
1415 else if (TYPE_LENGTH (type) == 16
1416 && TYPE_CODE (type) == TYPE_CODE_DECFLOAT)
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;
1422 if (argpos->regcache && argpos->freg <= 12)
1424 int regnum = tdep->ppc_fp0_regnum + argpos->freg;
1425 int lopart = gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG ? 8 : 0;
1426 int hipart = gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG ? 0 : 8;
1428 regcache_cooked_write (argpos->regcache, regnum, val + hipart);
1429 regcache_cooked_write (argpos->regcache, regnum + 1, val + lopart);
1436 /* VAL is a value of AltiVec vector type. Load it into a vector register
1437 if required by the ABI, and update ARGPOS. */
1440 ppc64_sysv_abi_push_vreg (struct gdbarch *gdbarch, const bfd_byte *val,
1441 struct ppc64_sysv_argpos *argpos)
1443 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1445 if (argpos->regcache && argpos->vreg <= 13)
1446 regcache_cooked_write (argpos->regcache,
1447 tdep->ppc_vr0_regnum + argpos->vreg, val);
1452 /* VAL is a value of TYPE. Load it into memory and/or registers
1453 as required by the ABI, and update ARGPOS. */
1456 ppc64_sysv_abi_push_param (struct gdbarch *gdbarch,
1457 struct type *type, const bfd_byte *val,
1458 struct ppc64_sysv_argpos *argpos)
1460 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1462 if (TYPE_CODE (type) == TYPE_CODE_FLT
1463 || TYPE_CODE (type) == TYPE_CODE_DECFLOAT)
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);
1469 else if (TYPE_CODE (type) == TYPE_CODE_ARRAY && TYPE_VECTOR (type)
1470 && tdep->vector_abi == POWERPC_VEC_ALTIVEC
1471 && TYPE_LENGTH (type) == 16)
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);
1477 else if (TYPE_CODE (type) == TYPE_CODE_ARRAY && TYPE_VECTOR (type)
1478 && TYPE_LENGTH (type) >= 16)
1480 /* Non-Altivec vectors are passed by reference. */
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);
1488 /* ... and pass a pointer to the copy as parameter. */
1489 ppc64_sysv_abi_push_integer (gdbarch, addr, argpos);
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)
1501 if (argpos->regcache)
1503 /* Sign extend the value, then store it unsigned. */
1504 word = unpack_long (type, val);
1506 /* Convert any function code addresses into descriptors. */
1507 if (tdep->elf_abi == POWERPC_ELF_V1
1508 && (TYPE_CODE (type) == TYPE_CODE_PTR
1509 || TYPE_CODE (type) == TYPE_CODE_REF))
1511 struct type *target_type
1512 = check_typedef (TYPE_TARGET_TYPE (type));
1514 if (TYPE_CODE (target_type) == TYPE_CODE_FUNC
1515 || TYPE_CODE (target_type) == TYPE_CODE_METHOD)
1517 CORE_ADDR desc = word;
1519 convert_code_addr_to_desc_addr (word, &desc);
1525 ppc64_sysv_abi_push_integer (gdbarch, word, argpos);
1529 ppc64_sysv_abi_push_val (gdbarch, val, TYPE_LENGTH (type), 0, argpos);
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)
1537 while (TYPE_CODE (type) == TYPE_CODE_STRUCT
1538 && TYPE_NFIELDS (type) == 1)
1539 type = check_typedef (TYPE_FIELD_TYPE (type, 0));
1541 if (TYPE_CODE (type) == TYPE_CODE_FLT)
1542 ppc64_sysv_abi_push_freg (gdbarch, type, val, argpos);
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)
1549 struct type *eltype;
1552 if (ppc64_elfv2_abi_homogeneous_aggregate (type, &eltype, &nelt))
1553 for (i = 0; i < nelt; i++)
1555 const gdb_byte *elval = val + i * TYPE_LENGTH (eltype);
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);
1570 /* Pass the arguments in either registers, or in the stack. Using the
1571 ppc 64 bit SysV ABI.
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
1575 greatly simplifies the logic. */
1578 ppc64_sysv_abi_push_dummy_call (struct gdbarch *gdbarch,
1579 struct value *function,
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)
1584 CORE_ADDR func_addr = find_function_addr (function, NULL);
1585 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1586 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1587 int opencl_abi = ppc_sysv_use_opencl_abi (value_type (function));
1588 ULONGEST back_chain;
1589 /* See for-loop comment below. */
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;
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
1598 calls to align_up(), align_down(), etc. because this makes it
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);
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));
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. */
1614 regcache_cooked_read_unsigned (regcache, gdbarch_sp_regnum (gdbarch),
1617 /* Go through the argument list twice.
1619 Pass 1: Compute the function call's stack space and register
1622 Pass 2: Replay the same computation but this time also write the
1623 values out to the target. */
1625 for (write_pass = 0; write_pass < 2; write_pass++)
1629 struct ppc64_sysv_argpos argpos;
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
1640 argpos.regcache = NULL;
1642 argpos.refparam = 0;
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. */
1649 argpos.regcache = regcache;
1650 argpos.refparam = align_down (sp - refparam_size, 16);
1651 argpos.gparam = align_down (argpos.refparam - gparam_size, 16);
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,
1655 if (tdep->elf_abi == POWERPC_ELF_V1)
1656 sp = align_down (argpos.gparam - 48, 16);
1658 sp = align_down (argpos.gparam - 32, 16);
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. */
1667 ppc64_sysv_abi_push_integer (gdbarch, struct_addr, &argpos);
1669 for (argno = 0; argno < nargs; argno++)
1671 struct value *arg = args[argno];
1672 struct type *type = check_typedef (value_type (arg));
1673 const bfd_byte *val = value_contents (arg);
1675 if (TYPE_CODE (type) == TYPE_CODE_COMPLEX)
1677 /* Complex types are passed as if two independent scalars. */
1678 struct type *eltype = check_typedef (TYPE_TARGET_TYPE (type));
1680 ppc64_sysv_abi_push_param (gdbarch, eltype, val, &argpos);
1681 ppc64_sysv_abi_push_param (gdbarch, eltype,
1682 val + TYPE_LENGTH (eltype), &argpos);
1684 else if (TYPE_CODE (type) == TYPE_CODE_ARRAY && TYPE_VECTOR (type)
1687 /* OpenCL vectors shorter than 16 bytes are passed as if
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;
1693 if (TYPE_LENGTH (type) < 16)
1694 eltype = check_typedef (TYPE_TARGET_TYPE (type));
1696 eltype = register_type (gdbarch, tdep->ppc_vr0_regnum);
1698 nelt = TYPE_LENGTH (type) / TYPE_LENGTH (eltype);
1699 for (i = 0; i < nelt; i++)
1701 const gdb_byte *elval = val + i * TYPE_LENGTH (eltype);
1703 ppc64_sysv_abi_push_param (gdbarch, eltype, elval, &argpos);
1708 /* All other types are passed as single arguments. */
1709 ppc64_sysv_abi_push_param (gdbarch, type, val, &argpos);
1715 /* Save the true region sizes ready for the second pass. */
1716 refparam_size = argpos.refparam;
1717 /* Make certain that the general parameter save area is at
1718 least the minimum 8 registers (or doublewords) in size. */
1719 if (argpos.greg < 8)
1720 gparam_size = 8 * tdep->wordsize;
1722 gparam_size = argpos.gparam;
1727 regcache_cooked_write_signed (regcache, gdbarch_sp_regnum (gdbarch), sp);
1729 /* Write the backchain (it occupies WORDSIZED bytes). */
1730 write_memory_signed_integer (sp, tdep->wordsize, byte_order, back_chain);
1732 /* Point the inferior function call's return address at the dummy's
1734 regcache_cooked_write_signed (regcache, tdep->ppc_lr_regnum, bp_addr);
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)
1741 struct type *ftype = check_typedef (value_type (function));
1742 CORE_ADDR desc_addr = value_as_address (function);
1744 if (TYPE_CODE (ftype) == TYPE_CODE_PTR
1745 || convert_code_addr_to_desc_addr (func_addr, &desc_addr))
1747 /* The TOC is the second double word in the descriptor. */
1749 read_memory_unsigned_integer (desc_addr + tdep->wordsize,
1750 tdep->wordsize, byte_order);
1752 regcache_cooked_write_unsigned (regcache,
1753 tdep->ppc_gp0_regnum + 2, toc);
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);
1766 /* Subroutine of ppc64_sysv_abi_return_value that handles "base" types:
1767 integer, floating-point, and AltiVec vector types.
1769 This routine also handles components of aggregate return types;
1770 INDEX describes which part of the aggregate is to be handled.
1772 Returns true if VALTYPE is some such base type that could be handled,
1775 ppc64_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)
1779 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1781 /* Integers live in GPRs starting at r3. */
1782 if ((TYPE_CODE (valtype) == TYPE_CODE_INT
1783 || TYPE_CODE (valtype) == TYPE_CODE_ENUM
1784 || TYPE_CODE (valtype) == TYPE_CODE_CHAR
1785 || TYPE_CODE (valtype) == TYPE_CODE_BOOL)
1786 && TYPE_LENGTH (valtype) <= 8)
1788 int regnum = tdep->ppc_gp0_regnum + 3 + index;
1790 if (writebuf != NULL)
1792 /* Be careful to sign extend the value. */
1793 regcache_cooked_write_unsigned (regcache, regnum,
1794 unpack_long (valtype, writebuf));
1796 if (readbuf != NULL)
1798 /* Extract the integer from GPR. Since this is truncating the
1799 value, there isn't a sign extension problem. */
1802 regcache_cooked_read_unsigned (regcache, regnum, ®val);
1803 store_unsigned_integer (readbuf, TYPE_LENGTH (valtype),
1804 gdbarch_byte_order (gdbarch), regval);
1809 /* Floats and doubles go in f1 .. f13. 32-bit floats are converted
1811 if (TYPE_LENGTH (valtype) <= 8
1812 && TYPE_CODE (valtype) == TYPE_CODE_FLT)
1814 int regnum = tdep->ppc_fp0_regnum + 1 + index;
1815 struct type *regtype = register_type (gdbarch, regnum);
1816 gdb_byte regval[MAX_REGISTER_SIZE];
1818 if (writebuf != NULL)
1820 convert_typed_floating (writebuf, valtype, regval, regtype);
1821 regcache_cooked_write (regcache, regnum, regval);
1823 if (readbuf != NULL)
1825 regcache_cooked_read (regcache, regnum, regval);
1826 convert_typed_floating (regval, regtype, readbuf, valtype);
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)
1836 int regnum = tdep->ppc_fp0_regnum + 1 + index;
1839 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
1840 offset = 8 - TYPE_LENGTH (valtype);
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);
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))
1857 int regnum = tdep->ppc_fp0_regnum + 1 + 2 * index;
1859 if (writebuf != NULL)
1861 regcache_cooked_write (regcache, regnum, writebuf);
1862 regcache_cooked_write (regcache, regnum + 1, writebuf + 8);
1864 if (readbuf != NULL)
1866 regcache_cooked_read (regcache, regnum, readbuf);
1867 regcache_cooked_read (regcache, regnum + 1, readbuf + 8);
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)
1877 int regnum = tdep->ppc_fp0_regnum + 2 + 2 * index;
1878 int lopart = gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG ? 8 : 0;
1879 int hipart = gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG ? 0 : 8;
1881 if (writebuf != NULL)
1883 regcache_cooked_write (regcache, regnum, writebuf + hipart);
1884 regcache_cooked_write (regcache, regnum + 1, writebuf + lopart);
1886 if (readbuf != NULL)
1888 regcache_cooked_read (regcache, regnum, readbuf + hipart);
1889 regcache_cooked_read (regcache, regnum + 1, readbuf + lopart);
1894 /* AltiVec vectors are returned in VRs starting at v2. */
1895 if (TYPE_LENGTH (valtype) == 16
1896 && TYPE_CODE (valtype) == TYPE_CODE_ARRAY && TYPE_VECTOR (valtype)
1897 && tdep->vector_abi == POWERPC_VEC_ALTIVEC)
1899 int regnum = tdep->ppc_vr0_regnum + 2 + index;
1901 if (writebuf != NULL)
1902 regcache_cooked_write (regcache, regnum, writebuf);
1903 if (readbuf != NULL)
1904 regcache_cooked_read (regcache, regnum, readbuf);
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))
1912 int regnum = tdep->ppc_gp0_regnum + 3 + index;
1915 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
1916 offset = 8 - TYPE_LENGTH (valtype);
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);
1930 /* The 64 bit ABI return value convention.
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).
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. */
1940 enum return_value_convention
1941 ppc64_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)
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;
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));
1956 /* Complex types are returned as if two independent scalars. */
1957 if (TYPE_CODE (valtype) == TYPE_CODE_COMPLEX)
1959 eltype = check_typedef (TYPE_TARGET_TYPE (valtype));
1961 for (i = 0; i < 2; i++)
1963 ok = ppc64_sysv_abi_return_value_base (gdbarch, eltype, regcache,
1964 readbuf, writebuf, i);
1968 readbuf += TYPE_LENGTH (eltype);
1970 writebuf += TYPE_LENGTH (eltype);
1972 return RETURN_VALUE_REGISTER_CONVENTION;
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)
1981 if (TYPE_LENGTH (valtype) < 16)
1982 eltype = check_typedef (TYPE_TARGET_TYPE (valtype));
1984 eltype = register_type (gdbarch, tdep->ppc_vr0_regnum);
1986 nelt = TYPE_LENGTH (valtype) / TYPE_LENGTH (eltype);
1987 for (i = 0; i < nelt; i++)
1989 ok = ppc64_sysv_abi_return_value_base (gdbarch, eltype, regcache,
1990 readbuf, writebuf, i);
1994 readbuf += TYPE_LENGTH (eltype);
1996 writebuf += TYPE_LENGTH (eltype);
1998 return RETURN_VALUE_REGISTER_CONVENTION;
2001 /* All pointers live in r3. */
2002 if (TYPE_CODE (valtype) == TYPE_CODE_PTR
2003 || TYPE_CODE (valtype) == TYPE_CODE_REF)
2005 int regnum = tdep->ppc_gp0_regnum + 3;
2007 if (writebuf != NULL)
2008 regcache_cooked_write (regcache, regnum, writebuf);
2009 if (readbuf != NULL)
2010 regcache_cooked_read (regcache, regnum, readbuf);
2011 return RETURN_VALUE_REGISTER_CONVENTION;
2014 /* Small character arrays are returned, right justified, in r3. */
2015 if (TYPE_CODE (valtype) == TYPE_CODE_ARRAY
2016 && !TYPE_VECTOR (valtype)
2017 && TYPE_LENGTH (valtype) <= 8
2018 && TYPE_CODE (TYPE_TARGET_TYPE (valtype)) == TYPE_CODE_INT
2019 && TYPE_LENGTH (TYPE_TARGET_TYPE (valtype)) == 1)
2021 int regnum = tdep->ppc_gp0_regnum + 3;
2022 int offset = (register_size (gdbarch, regnum) - TYPE_LENGTH (valtype));
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;
2033 /* In the ELFv2 ABI, homogeneous floating-point or vector
2034 aggregates are returned in registers. */
2035 if (tdep->elf_abi == POWERPC_ELF_V2
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)))
2044 for (i = 0; i < nelt; i++)
2046 ok = ppc64_sysv_abi_return_value_base (gdbarch, eltype, regcache,
2047 readbuf, writebuf, i);
2051 readbuf += TYPE_LENGTH (eltype);
2053 writebuf += TYPE_LENGTH (eltype);
2056 return RETURN_VALUE_REGISTER_CONVENTION;
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))))
2068 int n_regs = ((TYPE_LENGTH (valtype) + tdep->wordsize - 1)
2072 for (i = 0; i < n_regs; i++)
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;
2079 if (len > tdep->wordsize)
2080 len = tdep->wordsize;
2082 if (writebuf != NULL)
2084 memset (regval, 0, sizeof regval);
2085 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG
2087 memcpy (regval + tdep->wordsize - len, writebuf, len);
2089 memcpy (regval, writebuf + offset, len);
2090 regcache_cooked_write (regcache, regnum, regval);
2092 if (readbuf != NULL)
2094 regcache_cooked_read (regcache, regnum, regval);
2095 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG
2097 memcpy (readbuf, regval + tdep->wordsize - len, len);
2099 memcpy (readbuf + offset, regval, len);
2102 return RETURN_VALUE_REGISTER_CONVENTION;
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;
2110 return RETURN_VALUE_STRUCT_CONVENTION;