1 /* Target-dependent code for PowerPC systems using the SVR4 ABI
2 for GDB, the GNU debugger.
4 Copyright (C) 2000-2016 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/>. */
34 /* Check whether FTPYE is a (pointer to) function type that should use
35 the OpenCL vector ABI. */
38 ppc_sysv_use_opencl_abi (struct type *ftype)
40 ftype = check_typedef (ftype);
42 if (TYPE_CODE (ftype) == TYPE_CODE_PTR)
43 ftype = check_typedef (TYPE_TARGET_TYPE (ftype));
45 return (TYPE_CODE (ftype) == TYPE_CODE_FUNC
46 && TYPE_CALLING_CONVENTION (ftype) == DW_CC_GDB_IBM_OpenCL);
49 /* Pass the arguments in either registers, or in the stack. Using the
50 ppc sysv ABI, the first eight words of the argument list (that might
51 be less than eight parameters if some parameters occupy more than one
52 word) are passed in r3..r10 registers. float and double parameters are
53 passed in fpr's, in addition to that. Rest of the parameters if any
54 are passed in user stack.
56 If the function is returning a structure, then the return address is passed
57 in r3, then the first 7 words of the parametes can be passed in registers,
61 ppc_sysv_abi_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
62 struct regcache *regcache, CORE_ADDR bp_addr,
63 int nargs, struct value **args, CORE_ADDR sp,
64 int struct_return, CORE_ADDR struct_addr)
66 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
67 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
68 int opencl_abi = ppc_sysv_use_opencl_abi (value_type (function));
70 int argspace = 0; /* 0 is an initial wrong guess. */
73 gdb_assert (tdep->wordsize == 4);
75 regcache_cooked_read_unsigned (regcache, gdbarch_sp_regnum (gdbarch),
78 /* Go through the argument list twice.
80 Pass 1: Figure out how much new stack space is required for
81 arguments and pushed values. Unlike the PowerOpen ABI, the SysV
82 ABI doesn't reserve any extra space for parameters which are put
83 in registers, but does always push structures and then pass their
86 Pass 2: Replay the same computation but this time also write the
87 values out to the target. */
89 for (write_pass = 0; write_pass < 2; write_pass++)
92 /* Next available floating point register for float and double
95 /* Next available general register for non-float, non-vector
98 /* Next available vector register for vector arguments. */
100 /* Arguments start above the "LR save word" and "Back chain". */
101 int argoffset = 2 * tdep->wordsize;
102 /* Structures start after the arguments. */
103 int structoffset = argoffset + argspace;
105 /* If the function is returning a `struct', then the first word
106 (which will be passed in r3) is used for struct return
107 address. In that case we should advance one word and start
108 from r4 register to copy parameters. */
112 regcache_cooked_write_signed (regcache,
113 tdep->ppc_gp0_regnum + greg,
118 for (argno = 0; argno < nargs; argno++)
120 struct value *arg = args[argno];
121 struct type *type = check_typedef (value_type (arg));
122 int len = TYPE_LENGTH (type);
123 const bfd_byte *val = value_contents (arg);
125 if (TYPE_CODE (type) == TYPE_CODE_FLT && len <= 8
126 && !tdep->soft_float)
128 /* Floating point value converted to "double" then
129 passed in an FP register, when the registers run out,
130 8 byte aligned stack is used. */
135 /* Always store the floating point value using
136 the register's floating-point format. */
137 gdb_byte regval[MAX_REGISTER_SIZE];
139 = register_type (gdbarch, tdep->ppc_fp0_regnum + freg);
140 convert_typed_floating (val, type, regval, regtype);
141 regcache_cooked_write (regcache,
142 tdep->ppc_fp0_regnum + freg,
149 /* The SysV ABI tells us to convert floats to
150 doubles before writing them to an 8 byte aligned
151 stack location. Unfortunately GCC does not do
152 that, and stores floats into 4 byte aligned
153 locations without converting them to doubles.
154 Since there is no know compiler that actually
155 follows the ABI here, we implement the GCC
158 /* Align to 4 bytes or 8 bytes depending on the type of
159 the argument (float or double). */
160 argoffset = align_up (argoffset, len);
162 write_memory (sp + argoffset, val, len);
166 else if (TYPE_CODE (type) == TYPE_CODE_FLT
169 && (gdbarch_long_double_format (gdbarch)
170 == floatformats_ibm_long_double))
172 /* IBM long double passed in two FP registers if
173 available, otherwise 8-byte aligned stack. */
178 regcache_cooked_write (regcache,
179 tdep->ppc_fp0_regnum + freg,
181 regcache_cooked_write (regcache,
182 tdep->ppc_fp0_regnum + freg + 1,
189 argoffset = align_up (argoffset, 8);
191 write_memory (sp + argoffset, val, len);
196 && (TYPE_CODE (type) == TYPE_CODE_INT /* long long */
197 || TYPE_CODE (type) == TYPE_CODE_FLT /* double */
198 || (TYPE_CODE (type) == TYPE_CODE_DECFLOAT
199 && tdep->soft_float)))
201 /* "long long" or soft-float "double" or "_Decimal64"
202 passed in an odd/even register pair with the low
203 addressed word in the odd register and the high
204 addressed word in the even register, or when the
205 registers run out an 8 byte aligned stack
209 /* Just in case GREG was 10. */
211 argoffset = align_up (argoffset, 8);
213 write_memory (sp + argoffset, val, len);
218 /* Must start on an odd register - r3/r4 etc. */
223 regcache_cooked_write (regcache,
224 tdep->ppc_gp0_regnum + greg + 0,
226 regcache_cooked_write (regcache,
227 tdep->ppc_gp0_regnum + greg + 1,
234 && ((TYPE_CODE (type) == TYPE_CODE_FLT
235 && (gdbarch_long_double_format (gdbarch)
236 == floatformats_ibm_long_double))
237 || (TYPE_CODE (type) == TYPE_CODE_DECFLOAT
238 && tdep->soft_float)))
240 /* Soft-float IBM long double or _Decimal128 passed in
241 four consecutive registers, or on the stack. The
242 registers are not necessarily odd/even pairs. */
246 argoffset = align_up (argoffset, 8);
248 write_memory (sp + argoffset, val, len);
255 regcache_cooked_write (regcache,
256 tdep->ppc_gp0_regnum + greg + 0,
258 regcache_cooked_write (regcache,
259 tdep->ppc_gp0_regnum + greg + 1,
261 regcache_cooked_write (regcache,
262 tdep->ppc_gp0_regnum + greg + 2,
264 regcache_cooked_write (regcache,
265 tdep->ppc_gp0_regnum + greg + 3,
271 else if (TYPE_CODE (type) == TYPE_CODE_DECFLOAT && len <= 8
272 && !tdep->soft_float)
274 /* 32-bit and 64-bit decimal floats go in f1 .. f8. They can
281 gdb_byte regval[MAX_REGISTER_SIZE];
284 /* 32-bit decimal floats are right aligned in the
286 if (TYPE_LENGTH (type) == 4)
288 memcpy (regval + 4, val, 4);
294 regcache_cooked_write (regcache,
295 tdep->ppc_fp0_regnum + freg, p);
302 argoffset = align_up (argoffset, len);
305 /* Write value in the stack's parameter save area. */
306 write_memory (sp + argoffset, val, len);
311 else if (TYPE_CODE (type) == TYPE_CODE_DECFLOAT && len == 16
312 && !tdep->soft_float)
314 /* 128-bit decimal floats go in f2 .. f7, always in even/odd
315 pairs. They can end up in memory, using two doublewords. */
319 /* Make sure freg is even. */
324 regcache_cooked_write (regcache,
325 tdep->ppc_fp0_regnum + freg, val);
326 regcache_cooked_write (regcache,
327 tdep->ppc_fp0_regnum + freg + 1, val + 8);
332 argoffset = align_up (argoffset, 8);
335 write_memory (sp + argoffset, val, 16);
340 /* If a 128-bit decimal float goes to the stack because only f7
341 and f8 are free (thus there's no even/odd register pair
342 available), these registers should be marked as occupied.
343 Hence we increase freg even when writing to memory. */
347 && TYPE_CODE (type) == TYPE_CODE_ARRAY
348 && TYPE_VECTOR (type)
351 /* OpenCL vectors shorter than 16 bytes are passed as if
352 a series of independent scalars. */
353 struct type *eltype = check_typedef (TYPE_TARGET_TYPE (type));
354 int i, nelt = TYPE_LENGTH (type) / TYPE_LENGTH (eltype);
356 for (i = 0; i < nelt; i++)
358 const gdb_byte *elval = val + i * TYPE_LENGTH (eltype);
360 if (TYPE_CODE (eltype) == TYPE_CODE_FLT && !tdep->soft_float)
366 int regnum = tdep->ppc_fp0_regnum + freg;
367 gdb_byte regval[MAX_REGISTER_SIZE];
369 = register_type (gdbarch, regnum);
370 convert_typed_floating (elval, eltype,
372 regcache_cooked_write (regcache, regnum, regval);
378 argoffset = align_up (argoffset, len);
380 write_memory (sp + argoffset, val, len);
384 else if (TYPE_LENGTH (eltype) == 8)
388 /* Just in case GREG was 10. */
390 argoffset = align_up (argoffset, 8);
392 write_memory (sp + argoffset, elval,
393 TYPE_LENGTH (eltype));
398 /* Must start on an odd register - r3/r4 etc. */
403 int regnum = tdep->ppc_gp0_regnum + greg;
404 regcache_cooked_write (regcache,
405 regnum + 0, elval + 0);
406 regcache_cooked_write (regcache,
407 regnum + 1, elval + 4);
414 gdb_byte word[MAX_REGISTER_SIZE];
415 store_unsigned_integer (word, tdep->wordsize, byte_order,
416 unpack_long (eltype, elval));
421 regcache_cooked_write (regcache,
422 tdep->ppc_gp0_regnum + greg,
428 argoffset = align_up (argoffset, tdep->wordsize);
430 write_memory (sp + argoffset, word, tdep->wordsize);
431 argoffset += tdep->wordsize;
437 && TYPE_CODE (type) == TYPE_CODE_ARRAY
438 && TYPE_VECTOR (type)
441 /* OpenCL vectors 16 bytes or longer are passed as if
442 a series of AltiVec vectors. */
445 for (i = 0; i < len / 16; i++)
447 const gdb_byte *elval = val + i * 16;
452 regcache_cooked_write (regcache,
453 tdep->ppc_vr0_regnum + vreg,
459 argoffset = align_up (argoffset, 16);
461 write_memory (sp + argoffset, elval, 16);
467 && TYPE_CODE (type) == TYPE_CODE_ARRAY
468 && TYPE_VECTOR (type)
469 && tdep->vector_abi == POWERPC_VEC_ALTIVEC)
471 /* Vector parameter passed in an Altivec register, or
472 when that runs out, 16 byte aligned stack location. */
476 regcache_cooked_write (regcache,
477 tdep->ppc_vr0_regnum + vreg, val);
482 argoffset = align_up (argoffset, 16);
484 write_memory (sp + argoffset, val, 16);
489 && TYPE_CODE (type) == TYPE_CODE_ARRAY
490 && TYPE_VECTOR (type)
491 && tdep->vector_abi == POWERPC_VEC_SPE)
493 /* Vector parameter passed in an e500 register, or when
494 that runs out, 8 byte aligned stack location. Note
495 that since e500 vector and general purpose registers
496 both map onto the same underlying register set, a
497 "greg" and not a "vreg" is consumed here. A cooked
498 write stores the value in the correct locations
499 within the raw register cache. */
503 regcache_cooked_write (regcache,
504 tdep->ppc_ev0_regnum + greg, val);
509 argoffset = align_up (argoffset, 8);
511 write_memory (sp + argoffset, val, 8);
517 /* Reduce the parameter down to something that fits in a
519 gdb_byte word[MAX_REGISTER_SIZE];
520 memset (word, 0, MAX_REGISTER_SIZE);
521 if (len > tdep->wordsize
522 || TYPE_CODE (type) == TYPE_CODE_STRUCT
523 || TYPE_CODE (type) == TYPE_CODE_UNION)
525 /* Structs and large values are put in an
526 aligned stack slot ... */
527 if (TYPE_CODE (type) == TYPE_CODE_ARRAY
528 && TYPE_VECTOR (type)
530 structoffset = align_up (structoffset, 16);
532 structoffset = align_up (structoffset, 8);
535 write_memory (sp + structoffset, val, len);
536 /* ... and then a "word" pointing to that address is
537 passed as the parameter. */
538 store_unsigned_integer (word, tdep->wordsize, byte_order,
542 else if (TYPE_CODE (type) == TYPE_CODE_INT)
543 /* Sign or zero extend the "int" into a "word". */
544 store_unsigned_integer (word, tdep->wordsize, byte_order,
545 unpack_long (type, val));
547 /* Always goes in the low address. */
548 memcpy (word, val, len);
549 /* Store that "word" in a register, or on the stack.
550 The words have "4" byte alignment. */
554 regcache_cooked_write (regcache,
555 tdep->ppc_gp0_regnum + greg, word);
560 argoffset = align_up (argoffset, tdep->wordsize);
562 write_memory (sp + argoffset, word, tdep->wordsize);
563 argoffset += tdep->wordsize;
568 /* Compute the actual stack space requirements. */
571 /* Remember the amount of space needed by the arguments. */
572 argspace = argoffset;
573 /* Allocate space for both the arguments and the structures. */
574 sp -= (argoffset + structoffset);
575 /* Ensure that the stack is still 16 byte aligned. */
576 sp = align_down (sp, 16);
579 /* The psABI says that "A caller of a function that takes a
580 variable argument list shall set condition register bit 6 to
581 1 if it passes one or more arguments in the floating-point
582 registers. It is strongly recommended that the caller set the
583 bit to 0 otherwise..." Doing this for normal functions too
589 regcache_cooked_read_unsigned (regcache, tdep->ppc_cr_regnum, &cr);
594 regcache_cooked_write_unsigned (regcache, tdep->ppc_cr_regnum, cr);
599 regcache_cooked_write_signed (regcache, gdbarch_sp_regnum (gdbarch), sp);
601 /* Write the backchain (it occupies WORDSIZED bytes). */
602 write_memory_signed_integer (sp, tdep->wordsize, byte_order, saved_sp);
604 /* Point the inferior function call's return address at the dummy's
606 regcache_cooked_write_signed (regcache, tdep->ppc_lr_regnum, bp_addr);
611 /* Handle the return-value conventions for Decimal Floating Point values. */
612 static enum return_value_convention
613 get_decimal_float_return_value (struct gdbarch *gdbarch, struct type *valtype,
614 struct regcache *regcache, gdb_byte *readbuf,
615 const gdb_byte *writebuf)
617 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
619 gdb_assert (TYPE_CODE (valtype) == TYPE_CODE_DECFLOAT);
621 /* 32-bit and 64-bit decimal floats in f1. */
622 if (TYPE_LENGTH (valtype) <= 8)
624 if (writebuf != NULL)
626 gdb_byte regval[MAX_REGISTER_SIZE];
629 /* 32-bit decimal float is right aligned in the doubleword. */
630 if (TYPE_LENGTH (valtype) == 4)
632 memcpy (regval + 4, writebuf, 4);
638 regcache_cooked_write (regcache, tdep->ppc_fp0_regnum + 1, p);
642 regcache_cooked_read (regcache, tdep->ppc_fp0_regnum + 1, readbuf);
644 /* Left align 32-bit decimal float. */
645 if (TYPE_LENGTH (valtype) == 4)
646 memcpy (readbuf, readbuf + 4, 4);
649 /* 128-bit decimal floats in f2,f3. */
650 else if (TYPE_LENGTH (valtype) == 16)
652 if (writebuf != NULL || readbuf != NULL)
656 for (i = 0; i < 2; i++)
658 if (writebuf != NULL)
659 regcache_cooked_write (regcache, tdep->ppc_fp0_regnum + 2 + i,
662 regcache_cooked_read (regcache, tdep->ppc_fp0_regnum + 2 + i,
669 internal_error (__FILE__, __LINE__, _("Unknown decimal float size."));
671 return RETURN_VALUE_REGISTER_CONVENTION;
674 /* Handle the return-value conventions specified by the SysV 32-bit
675 PowerPC ABI (including all the supplements):
677 no floating-point: floating-point values returned using 32-bit
678 general-purpose registers.
680 Altivec: 128-bit vectors returned using vector registers.
682 e500: 64-bit vectors returned using the full full 64 bit EV
683 register, floating-point values returned using 32-bit
684 general-purpose registers.
686 GCC (broken): Small struct values right (instead of left) aligned
687 when returned in general-purpose registers. */
689 static enum return_value_convention
690 do_ppc_sysv_return_value (struct gdbarch *gdbarch, struct type *func_type,
691 struct type *type, struct regcache *regcache,
692 gdb_byte *readbuf, const gdb_byte *writebuf,
695 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
696 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
697 int opencl_abi = func_type? ppc_sysv_use_opencl_abi (func_type) : 0;
699 gdb_assert (tdep->wordsize == 4);
701 if (TYPE_CODE (type) == TYPE_CODE_FLT
702 && TYPE_LENGTH (type) <= 8
703 && !tdep->soft_float)
707 /* Floats and doubles stored in "f1". Convert the value to
708 the required type. */
709 gdb_byte regval[MAX_REGISTER_SIZE];
710 struct type *regtype = register_type (gdbarch,
711 tdep->ppc_fp0_regnum + 1);
712 regcache_cooked_read (regcache, tdep->ppc_fp0_regnum + 1, regval);
713 convert_typed_floating (regval, regtype, readbuf, type);
717 /* Floats and doubles stored in "f1". Convert the value to
718 the register's "double" type. */
719 gdb_byte regval[MAX_REGISTER_SIZE];
720 struct type *regtype = register_type (gdbarch, tdep->ppc_fp0_regnum);
721 convert_typed_floating (writebuf, type, regval, regtype);
722 regcache_cooked_write (regcache, tdep->ppc_fp0_regnum + 1, regval);
724 return RETURN_VALUE_REGISTER_CONVENTION;
726 if (TYPE_CODE (type) == TYPE_CODE_FLT
727 && TYPE_LENGTH (type) == 16
729 && (gdbarch_long_double_format (gdbarch)
730 == floatformats_ibm_long_double))
732 /* IBM long double stored in f1 and f2. */
735 regcache_cooked_read (regcache, tdep->ppc_fp0_regnum + 1, readbuf);
736 regcache_cooked_read (regcache, tdep->ppc_fp0_regnum + 2,
741 regcache_cooked_write (regcache, tdep->ppc_fp0_regnum + 1, writebuf);
742 regcache_cooked_write (regcache, tdep->ppc_fp0_regnum + 2,
745 return RETURN_VALUE_REGISTER_CONVENTION;
747 if (TYPE_LENGTH (type) == 16
748 && ((TYPE_CODE (type) == TYPE_CODE_FLT
749 && (gdbarch_long_double_format (gdbarch)
750 == floatformats_ibm_long_double))
751 || (TYPE_CODE (type) == TYPE_CODE_DECFLOAT && tdep->soft_float)))
753 /* Soft-float IBM long double or _Decimal128 stored in r3, r4,
757 regcache_cooked_read (regcache, tdep->ppc_gp0_regnum + 3, readbuf);
758 regcache_cooked_read (regcache, tdep->ppc_gp0_regnum + 4,
760 regcache_cooked_read (regcache, tdep->ppc_gp0_regnum + 5,
762 regcache_cooked_read (regcache, tdep->ppc_gp0_regnum + 6,
767 regcache_cooked_write (regcache, tdep->ppc_gp0_regnum + 3, writebuf);
768 regcache_cooked_write (regcache, tdep->ppc_gp0_regnum + 4,
770 regcache_cooked_write (regcache, tdep->ppc_gp0_regnum + 5,
772 regcache_cooked_write (regcache, tdep->ppc_gp0_regnum + 6,
775 return RETURN_VALUE_REGISTER_CONVENTION;
777 if ((TYPE_CODE (type) == TYPE_CODE_INT && TYPE_LENGTH (type) == 8)
778 || (TYPE_CODE (type) == TYPE_CODE_FLT && TYPE_LENGTH (type) == 8)
779 || (TYPE_CODE (type) == TYPE_CODE_DECFLOAT && TYPE_LENGTH (type) == 8
780 && tdep->soft_float))
784 /* A long long, double or _Decimal64 stored in the 32 bit
786 regcache_cooked_read (regcache, tdep->ppc_gp0_regnum + 3,
788 regcache_cooked_read (regcache, tdep->ppc_gp0_regnum + 4,
793 /* A long long, double or _Decimal64 stored in the 32 bit
795 regcache_cooked_write (regcache, tdep->ppc_gp0_regnum + 3,
797 regcache_cooked_write (regcache, tdep->ppc_gp0_regnum + 4,
800 return RETURN_VALUE_REGISTER_CONVENTION;
802 if (TYPE_CODE (type) == TYPE_CODE_DECFLOAT && !tdep->soft_float)
803 return get_decimal_float_return_value (gdbarch, type, regcache, readbuf,
805 else if ((TYPE_CODE (type) == TYPE_CODE_INT
806 || TYPE_CODE (type) == TYPE_CODE_CHAR
807 || TYPE_CODE (type) == TYPE_CODE_BOOL
808 || TYPE_CODE (type) == TYPE_CODE_PTR
809 || TYPE_CODE (type) == TYPE_CODE_REF
810 || TYPE_CODE (type) == TYPE_CODE_ENUM)
811 && TYPE_LENGTH (type) <= tdep->wordsize)
815 /* Some sort of integer stored in r3. Since TYPE isn't
816 bigger than the register, sign extension isn't a problem
817 - just do everything unsigned. */
819 regcache_cooked_read_unsigned (regcache, tdep->ppc_gp0_regnum + 3,
821 store_unsigned_integer (readbuf, TYPE_LENGTH (type), byte_order,
826 /* Some sort of integer stored in r3. Use unpack_long since
827 that should handle any required sign extension. */
828 regcache_cooked_write_unsigned (regcache, tdep->ppc_gp0_regnum + 3,
829 unpack_long (type, writebuf));
831 return RETURN_VALUE_REGISTER_CONVENTION;
833 /* OpenCL vectors < 16 bytes are returned as distinct
834 scalars in f1..f2 or r3..r10. */
835 if (TYPE_CODE (type) == TYPE_CODE_ARRAY
836 && TYPE_VECTOR (type)
837 && TYPE_LENGTH (type) < 16
840 struct type *eltype = check_typedef (TYPE_TARGET_TYPE (type));
841 int i, nelt = TYPE_LENGTH (type) / TYPE_LENGTH (eltype);
843 for (i = 0; i < nelt; i++)
845 int offset = i * TYPE_LENGTH (eltype);
847 if (TYPE_CODE (eltype) == TYPE_CODE_FLT)
849 int regnum = tdep->ppc_fp0_regnum + 1 + i;
850 gdb_byte regval[MAX_REGISTER_SIZE];
851 struct type *regtype = register_type (gdbarch, regnum);
853 if (writebuf != NULL)
855 convert_typed_floating (writebuf + offset, eltype,
857 regcache_cooked_write (regcache, regnum, regval);
861 regcache_cooked_read (regcache, regnum, regval);
862 convert_typed_floating (regval, regtype,
863 readbuf + offset, eltype);
868 int regnum = tdep->ppc_gp0_regnum + 3 + i;
871 if (writebuf != NULL)
873 regval = unpack_long (eltype, writebuf + offset);
874 regcache_cooked_write_unsigned (regcache, regnum, regval);
878 regcache_cooked_read_unsigned (regcache, regnum, ®val);
879 store_unsigned_integer (readbuf + offset,
880 TYPE_LENGTH (eltype), byte_order,
886 return RETURN_VALUE_REGISTER_CONVENTION;
888 /* OpenCL vectors >= 16 bytes are returned in v2..v9. */
889 if (TYPE_CODE (type) == TYPE_CODE_ARRAY
890 && TYPE_VECTOR (type)
891 && TYPE_LENGTH (type) >= 16
894 int n_regs = TYPE_LENGTH (type) / 16;
897 for (i = 0; i < n_regs; i++)
900 int regnum = tdep->ppc_vr0_regnum + 2 + i;
902 if (writebuf != NULL)
903 regcache_cooked_write (regcache, regnum, writebuf + offset);
905 regcache_cooked_read (regcache, regnum, readbuf + offset);
908 return RETURN_VALUE_REGISTER_CONVENTION;
910 if (TYPE_LENGTH (type) == 16
911 && TYPE_CODE (type) == TYPE_CODE_ARRAY
912 && TYPE_VECTOR (type)
913 && tdep->vector_abi == POWERPC_VEC_ALTIVEC)
917 /* Altivec places the return value in "v2". */
918 regcache_cooked_read (regcache, tdep->ppc_vr0_regnum + 2, readbuf);
922 /* Altivec places the return value in "v2". */
923 regcache_cooked_write (regcache, tdep->ppc_vr0_regnum + 2, writebuf);
925 return RETURN_VALUE_REGISTER_CONVENTION;
927 if (TYPE_LENGTH (type) == 16
928 && TYPE_CODE (type) == TYPE_CODE_ARRAY
929 && TYPE_VECTOR (type)
930 && tdep->vector_abi == POWERPC_VEC_GENERIC)
932 /* GCC -maltivec -mabi=no-altivec returns vectors in r3/r4/r5/r6.
933 GCC without AltiVec returns them in memory, but it warns about
934 ABI risks in that case; we don't try to support it. */
937 regcache_cooked_read (regcache, tdep->ppc_gp0_regnum + 3,
939 regcache_cooked_read (regcache, tdep->ppc_gp0_regnum + 4,
941 regcache_cooked_read (regcache, tdep->ppc_gp0_regnum + 5,
943 regcache_cooked_read (regcache, tdep->ppc_gp0_regnum + 6,
948 regcache_cooked_write (regcache, tdep->ppc_gp0_regnum + 3,
950 regcache_cooked_write (regcache, tdep->ppc_gp0_regnum + 4,
952 regcache_cooked_write (regcache, tdep->ppc_gp0_regnum + 5,
954 regcache_cooked_write (regcache, tdep->ppc_gp0_regnum + 6,
957 return RETURN_VALUE_REGISTER_CONVENTION;
959 if (TYPE_LENGTH (type) == 8
960 && TYPE_CODE (type) == TYPE_CODE_ARRAY
961 && TYPE_VECTOR (type)
962 && tdep->vector_abi == POWERPC_VEC_SPE)
964 /* The e500 ABI places return values for the 64-bit DSP types
965 (__ev64_opaque__) in r3. However, in GDB-speak, ev3
966 corresponds to the entire r3 value for e500, whereas GDB's r3
967 only corresponds to the least significant 32-bits. So place
968 the 64-bit DSP type's value in ev3. */
970 regcache_cooked_read (regcache, tdep->ppc_ev0_regnum + 3, readbuf);
972 regcache_cooked_write (regcache, tdep->ppc_ev0_regnum + 3, writebuf);
973 return RETURN_VALUE_REGISTER_CONVENTION;
975 if (broken_gcc && TYPE_LENGTH (type) <= 8)
977 /* GCC screwed up for structures or unions whose size is less
978 than or equal to 8 bytes.. Instead of left-aligning, it
979 right-aligns the data into the buffer formed by r3, r4. */
980 gdb_byte regvals[MAX_REGISTER_SIZE * 2];
981 int len = TYPE_LENGTH (type);
982 int offset = (2 * tdep->wordsize - len) % tdep->wordsize;
986 regcache_cooked_read (regcache, tdep->ppc_gp0_regnum + 3,
987 regvals + 0 * tdep->wordsize);
988 if (len > tdep->wordsize)
989 regcache_cooked_read (regcache, tdep->ppc_gp0_regnum + 4,
990 regvals + 1 * tdep->wordsize);
991 memcpy (readbuf, regvals + offset, len);
995 memset (regvals, 0, sizeof regvals);
996 memcpy (regvals + offset, writebuf, len);
997 regcache_cooked_write (regcache, tdep->ppc_gp0_regnum + 3,
998 regvals + 0 * tdep->wordsize);
999 if (len > tdep->wordsize)
1000 regcache_cooked_write (regcache, tdep->ppc_gp0_regnum + 4,
1001 regvals + 1 * tdep->wordsize);
1004 return RETURN_VALUE_REGISTER_CONVENTION;
1006 if (TYPE_LENGTH (type) <= 8)
1010 /* This matches SVr4 PPC, it does not match GCC. */
1011 /* The value is right-padded to 8 bytes and then loaded, as
1012 two "words", into r3/r4. */
1013 gdb_byte regvals[MAX_REGISTER_SIZE * 2];
1014 regcache_cooked_read (regcache, tdep->ppc_gp0_regnum + 3,
1015 regvals + 0 * tdep->wordsize);
1016 if (TYPE_LENGTH (type) > tdep->wordsize)
1017 regcache_cooked_read (regcache, tdep->ppc_gp0_regnum + 4,
1018 regvals + 1 * tdep->wordsize);
1019 memcpy (readbuf, regvals, TYPE_LENGTH (type));
1023 /* This matches SVr4 PPC, it does not match GCC. */
1024 /* The value is padded out to 8 bytes and then loaded, as
1025 two "words" into r3/r4. */
1026 gdb_byte regvals[MAX_REGISTER_SIZE * 2];
1027 memset (regvals, 0, sizeof regvals);
1028 memcpy (regvals, writebuf, TYPE_LENGTH (type));
1029 regcache_cooked_write (regcache, tdep->ppc_gp0_regnum + 3,
1030 regvals + 0 * tdep->wordsize);
1031 if (TYPE_LENGTH (type) > tdep->wordsize)
1032 regcache_cooked_write (regcache, tdep->ppc_gp0_regnum + 4,
1033 regvals + 1 * tdep->wordsize);
1035 return RETURN_VALUE_REGISTER_CONVENTION;
1037 return RETURN_VALUE_STRUCT_CONVENTION;
1040 enum return_value_convention
1041 ppc_sysv_abi_return_value (struct gdbarch *gdbarch, struct value *function,
1042 struct type *valtype, struct regcache *regcache,
1043 gdb_byte *readbuf, const gdb_byte *writebuf)
1045 return do_ppc_sysv_return_value (gdbarch,
1046 function ? value_type (function) : NULL,
1047 valtype, regcache, readbuf, writebuf, 0);
1050 enum return_value_convention
1051 ppc_sysv_abi_broken_return_value (struct gdbarch *gdbarch,
1052 struct value *function,
1053 struct type *valtype,
1054 struct regcache *regcache,
1055 gdb_byte *readbuf, const gdb_byte *writebuf)
1057 return do_ppc_sysv_return_value (gdbarch,
1058 function ? value_type (function) : NULL,
1059 valtype, regcache, readbuf, writebuf, 1);
1062 /* The helper function for 64-bit SYSV push_dummy_call. Converts the
1063 function's code address back into the function's descriptor
1066 Find a value for the TOC register. Every symbol should have both
1067 ".FN" and "FN" in the minimal symbol table. "FN" points at the
1068 FN's descriptor, while ".FN" points at the entry point (which
1069 matches FUNC_ADDR). Need to reverse from FUNC_ADDR back to the
1070 FN's descriptor address (while at the same time being careful to
1071 find "FN" in the same object file as ".FN"). */
1074 convert_code_addr_to_desc_addr (CORE_ADDR code_addr, CORE_ADDR *desc_addr)
1076 struct obj_section *dot_fn_section;
1077 struct bound_minimal_symbol dot_fn;
1078 struct bound_minimal_symbol fn;
1080 /* Find the minimal symbol that corresponds to CODE_ADDR (should
1081 have a name of the form ".FN"). */
1082 dot_fn = lookup_minimal_symbol_by_pc (code_addr);
1083 if (dot_fn.minsym == NULL || MSYMBOL_LINKAGE_NAME (dot_fn.minsym)[0] != '.')
1085 /* Get the section that contains CODE_ADDR. Need this for the
1086 "objfile" that it contains. */
1087 dot_fn_section = find_pc_section (code_addr);
1088 if (dot_fn_section == NULL || dot_fn_section->objfile == NULL)
1090 /* Now find the corresponding "FN" (dropping ".") minimal symbol's
1091 address. Only look for the minimal symbol in ".FN"'s object file
1092 - avoids problems when two object files (i.e., shared libraries)
1093 contain a minimal symbol with the same name. */
1094 fn = lookup_minimal_symbol (MSYMBOL_LINKAGE_NAME (dot_fn.minsym) + 1, NULL,
1095 dot_fn_section->objfile);
1096 if (fn.minsym == NULL)
1098 /* Found a descriptor. */
1099 (*desc_addr) = BMSYMBOL_VALUE_ADDRESS (fn);
1103 /* Walk down the type tree of TYPE counting consecutive base elements.
1104 If *FIELD_TYPE is NULL, then set it to the first valid floating point
1105 or vector type. If a non-floating point or vector type is found, or
1106 if a floating point or vector type that doesn't match a non-NULL
1107 *FIELD_TYPE is found, then return -1, otherwise return the count in the
1111 ppc64_aggregate_candidate (struct type *type,
1112 struct type **field_type)
1114 type = check_typedef (type);
1116 switch (TYPE_CODE (type))
1119 case TYPE_CODE_DECFLOAT:
1122 if (TYPE_CODE (*field_type) == TYPE_CODE (type)
1123 && TYPE_LENGTH (*field_type) == TYPE_LENGTH (type))
1127 case TYPE_CODE_COMPLEX:
1128 type = TYPE_TARGET_TYPE (type);
1129 if (TYPE_CODE (type) == TYPE_CODE_FLT
1130 || TYPE_CODE (type) == TYPE_CODE_DECFLOAT)
1134 if (TYPE_CODE (*field_type) == TYPE_CODE (type)
1135 && TYPE_LENGTH (*field_type) == TYPE_LENGTH (type))
1140 case TYPE_CODE_ARRAY:
1141 if (TYPE_VECTOR (type))
1145 if (TYPE_CODE (*field_type) == TYPE_CODE (type)
1146 && TYPE_LENGTH (*field_type) == TYPE_LENGTH (type))
1151 LONGEST count, low_bound, high_bound;
1153 count = ppc64_aggregate_candidate
1154 (TYPE_TARGET_TYPE (type), field_type);
1158 if (!get_array_bounds (type, &low_bound, &high_bound))
1160 count *= high_bound - low_bound;
1162 /* There must be no padding. */
1164 return TYPE_LENGTH (type) == 0 ? 0 : -1;
1165 else if (TYPE_LENGTH (type) != count * TYPE_LENGTH (*field_type))
1172 case TYPE_CODE_STRUCT:
1173 case TYPE_CODE_UNION:
1178 for (i = 0; i < TYPE_NFIELDS (type); i++)
1182 if (field_is_static (&TYPE_FIELD (type, i)))
1185 sub_count = ppc64_aggregate_candidate
1186 (TYPE_FIELD_TYPE (type, i), field_type);
1187 if (sub_count == -1)
1190 if (TYPE_CODE (type) == TYPE_CODE_STRUCT)
1193 count = std::max (count, sub_count);
1196 /* There must be no padding. */
1198 return TYPE_LENGTH (type) == 0 ? 0 : -1;
1199 else if (TYPE_LENGTH (type) != count * TYPE_LENGTH (*field_type))
1213 /* If an argument of type TYPE is a homogeneous float or vector aggregate
1214 that shall be passed in FP/vector registers according to the ELFv2 ABI,
1215 return the homogeneous element type in *ELT_TYPE and the number of
1216 elements in *N_ELTS, and return non-zero. Otherwise, return zero. */
1219 ppc64_elfv2_abi_homogeneous_aggregate (struct type *type,
1220 struct type **elt_type, int *n_elts)
1222 /* Complex types at the top level are treated separately. However,
1223 complex types can be elements of homogeneous aggregates. */
1224 if (TYPE_CODE (type) == TYPE_CODE_STRUCT
1225 || TYPE_CODE (type) == TYPE_CODE_UNION
1226 || (TYPE_CODE (type) == TYPE_CODE_ARRAY && !TYPE_VECTOR (type)))
1228 struct type *field_type = NULL;
1229 LONGEST field_count = ppc64_aggregate_candidate (type, &field_type);
1231 if (field_count > 0)
1233 int n_regs = ((TYPE_CODE (field_type) == TYPE_CODE_FLT
1234 || TYPE_CODE (field_type) == TYPE_CODE_DECFLOAT)?
1235 (TYPE_LENGTH (field_type) + 7) >> 3 : 1);
1237 /* The ELFv2 ABI allows homogeneous aggregates to occupy
1238 up to 8 registers. */
1239 if (field_count * n_regs <= 8)
1242 *elt_type = field_type;
1244 *n_elts = (int) field_count;
1245 /* Note that field_count is LONGEST since it may hold the size
1246 of an array, while *n_elts is int since its value is bounded
1247 by the number of registers used for argument passing. The
1248 cast cannot overflow due to the bounds checking above. */
1257 /* Structure holding the next argument position. */
1258 struct ppc64_sysv_argpos
1260 /* Register cache holding argument registers. If this is NULL,
1261 we only simulate argument processing without actually updating
1262 any registers or memory. */
1263 struct regcache *regcache;
1264 /* Next available general-purpose argument register. */
1266 /* Next available floating-point argument register. */
1268 /* Next available vector argument register. */
1270 /* The address, at which the next general purpose parameter
1271 (integer, struct, float, vector, ...) should be saved. */
1273 /* The address, at which the next by-reference parameter
1274 (non-Altivec vector, variably-sized type) should be saved. */
1278 /* VAL is a value of length LEN. Store it into the argument area on the
1279 stack and load it into the corresponding general-purpose registers
1280 required by the ABI, and update ARGPOS.
1282 If ALIGN is nonzero, it specifies the minimum alignment required
1283 for the on-stack copy of the argument. */
1286 ppc64_sysv_abi_push_val (struct gdbarch *gdbarch,
1287 const bfd_byte *val, int len, int align,
1288 struct ppc64_sysv_argpos *argpos)
1290 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1293 /* Enforce alignment of stack location, if requested. */
1294 if (align > tdep->wordsize)
1296 CORE_ADDR aligned_gparam = align_up (argpos->gparam, align);
1298 argpos->greg += (aligned_gparam - argpos->gparam) / tdep->wordsize;
1299 argpos->gparam = aligned_gparam;
1302 /* The ABI (version 1.9) specifies that values smaller than one
1303 doubleword are right-aligned and those larger are left-aligned.
1304 GCC versions before 3.4 implemented this incorrectly; see
1305 <http://gcc.gnu.org/gcc-3.4/powerpc-abi.html>. */
1306 if (len < tdep->wordsize
1307 && gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
1308 offset = tdep->wordsize - len;
1310 if (argpos->regcache)
1311 write_memory (argpos->gparam + offset, val, len);
1312 argpos->gparam = align_up (argpos->gparam + len, tdep->wordsize);
1314 while (len >= tdep->wordsize)
1316 if (argpos->regcache && argpos->greg <= 10)
1317 regcache_cooked_write (argpos->regcache,
1318 tdep->ppc_gp0_regnum + argpos->greg, val);
1320 len -= tdep->wordsize;
1321 val += tdep->wordsize;
1326 if (argpos->regcache && argpos->greg <= 10)
1327 regcache_cooked_write_part (argpos->regcache,
1328 tdep->ppc_gp0_regnum + argpos->greg,
1334 /* The same as ppc64_sysv_abi_push_val, but using a single-word integer
1335 value VAL as argument. */
1338 ppc64_sysv_abi_push_integer (struct gdbarch *gdbarch, ULONGEST val,
1339 struct ppc64_sysv_argpos *argpos)
1341 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1342 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1343 gdb_byte buf[MAX_REGISTER_SIZE];
1345 if (argpos->regcache)
1346 store_unsigned_integer (buf, tdep->wordsize, byte_order, val);
1347 ppc64_sysv_abi_push_val (gdbarch, buf, tdep->wordsize, 0, argpos);
1350 /* VAL is a value of TYPE, a (binary or decimal) floating-point type.
1351 Load it into a floating-point register if required by the ABI,
1352 and update ARGPOS. */
1355 ppc64_sysv_abi_push_freg (struct gdbarch *gdbarch,
1356 struct type *type, const bfd_byte *val,
1357 struct ppc64_sysv_argpos *argpos)
1359 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1360 if (tdep->soft_float)
1363 if (TYPE_LENGTH (type) <= 8
1364 && TYPE_CODE (type) == TYPE_CODE_FLT)
1366 /* Floats and doubles go in f1 .. f13. 32-bit floats are converted
1368 if (argpos->regcache && argpos->freg <= 13)
1370 int regnum = tdep->ppc_fp0_regnum + argpos->freg;
1371 struct type *regtype = register_type (gdbarch, regnum);
1372 gdb_byte regval[MAX_REGISTER_SIZE];
1374 convert_typed_floating (val, type, regval, regtype);
1375 regcache_cooked_write (argpos->regcache, regnum, regval);
1380 else if (TYPE_LENGTH (type) <= 8
1381 && TYPE_CODE (type) == TYPE_CODE_DECFLOAT)
1383 /* Floats and doubles go in f1 .. f13. 32-bit decimal floats are
1384 placed in the least significant word. */
1385 if (argpos->regcache && argpos->freg <= 13)
1387 int regnum = tdep->ppc_fp0_regnum + argpos->freg;
1390 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
1391 offset = 8 - TYPE_LENGTH (type);
1393 regcache_cooked_write_part (argpos->regcache, regnum,
1394 offset, TYPE_LENGTH (type), val);
1399 else if (TYPE_LENGTH (type) == 16
1400 && TYPE_CODE (type) == TYPE_CODE_FLT
1401 && (gdbarch_long_double_format (gdbarch)
1402 == floatformats_ibm_long_double))
1404 /* IBM long double stored in two consecutive FPRs. */
1405 if (argpos->regcache && argpos->freg <= 13)
1407 int regnum = tdep->ppc_fp0_regnum + argpos->freg;
1409 regcache_cooked_write (argpos->regcache, regnum, val);
1410 if (argpos->freg <= 12)
1411 regcache_cooked_write (argpos->regcache, regnum + 1, val + 8);
1416 else if (TYPE_LENGTH (type) == 16
1417 && TYPE_CODE (type) == TYPE_CODE_DECFLOAT)
1419 /* 128-bit decimal floating-point values are stored in and even/odd
1420 pair of FPRs, with the even FPR holding the most significant half. */
1421 argpos->freg += argpos->freg & 1;
1423 if (argpos->regcache && argpos->freg <= 12)
1425 int regnum = tdep->ppc_fp0_regnum + argpos->freg;
1426 int lopart = gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG ? 8 : 0;
1427 int hipart = gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG ? 0 : 8;
1429 regcache_cooked_write (argpos->regcache, regnum, val + hipart);
1430 regcache_cooked_write (argpos->regcache, regnum + 1, val + lopart);
1437 /* VAL is a value of AltiVec vector type. Load it into a vector register
1438 if required by the ABI, and update ARGPOS. */
1441 ppc64_sysv_abi_push_vreg (struct gdbarch *gdbarch, const bfd_byte *val,
1442 struct ppc64_sysv_argpos *argpos)
1444 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1446 if (argpos->regcache && argpos->vreg <= 13)
1447 regcache_cooked_write (argpos->regcache,
1448 tdep->ppc_vr0_regnum + argpos->vreg, val);
1453 /* VAL is a value of TYPE. Load it into memory and/or registers
1454 as required by the ABI, and update ARGPOS. */
1457 ppc64_sysv_abi_push_param (struct gdbarch *gdbarch,
1458 struct type *type, const bfd_byte *val,
1459 struct ppc64_sysv_argpos *argpos)
1461 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1463 if (TYPE_CODE (type) == TYPE_CODE_FLT
1464 || TYPE_CODE (type) == TYPE_CODE_DECFLOAT)
1466 /* Floating-point scalars are passed in floating-point registers. */
1467 ppc64_sysv_abi_push_val (gdbarch, val, TYPE_LENGTH (type), 0, argpos);
1468 ppc64_sysv_abi_push_freg (gdbarch, type, val, argpos);
1470 else if (TYPE_CODE (type) == TYPE_CODE_ARRAY && TYPE_VECTOR (type)
1471 && tdep->vector_abi == POWERPC_VEC_ALTIVEC
1472 && TYPE_LENGTH (type) == 16)
1474 /* AltiVec vectors are passed aligned, and in vector registers. */
1475 ppc64_sysv_abi_push_val (gdbarch, val, TYPE_LENGTH (type), 16, argpos);
1476 ppc64_sysv_abi_push_vreg (gdbarch, val, argpos);
1478 else if (TYPE_CODE (type) == TYPE_CODE_ARRAY && TYPE_VECTOR (type)
1479 && TYPE_LENGTH (type) >= 16)
1481 /* Non-Altivec vectors are passed by reference. */
1483 /* Copy value onto the stack ... */
1484 CORE_ADDR addr = align_up (argpos->refparam, 16);
1485 if (argpos->regcache)
1486 write_memory (addr, val, TYPE_LENGTH (type));
1487 argpos->refparam = align_up (addr + TYPE_LENGTH (type), tdep->wordsize);
1489 /* ... and pass a pointer to the copy as parameter. */
1490 ppc64_sysv_abi_push_integer (gdbarch, addr, argpos);
1492 else if ((TYPE_CODE (type) == TYPE_CODE_INT
1493 || TYPE_CODE (type) == TYPE_CODE_ENUM
1494 || TYPE_CODE (type) == TYPE_CODE_BOOL
1495 || TYPE_CODE (type) == TYPE_CODE_CHAR
1496 || TYPE_CODE (type) == TYPE_CODE_PTR
1497 || TYPE_CODE (type) == TYPE_CODE_REF)
1498 && TYPE_LENGTH (type) <= tdep->wordsize)
1502 if (argpos->regcache)
1504 /* Sign extend the value, then store it unsigned. */
1505 word = unpack_long (type, val);
1507 /* Convert any function code addresses into descriptors. */
1508 if (tdep->elf_abi == POWERPC_ELF_V1
1509 && (TYPE_CODE (type) == TYPE_CODE_PTR
1510 || TYPE_CODE (type) == TYPE_CODE_REF))
1512 struct type *target_type
1513 = check_typedef (TYPE_TARGET_TYPE (type));
1515 if (TYPE_CODE (target_type) == TYPE_CODE_FUNC
1516 || TYPE_CODE (target_type) == TYPE_CODE_METHOD)
1518 CORE_ADDR desc = word;
1520 convert_code_addr_to_desc_addr (word, &desc);
1526 ppc64_sysv_abi_push_integer (gdbarch, word, argpos);
1530 ppc64_sysv_abi_push_val (gdbarch, val, TYPE_LENGTH (type), 0, argpos);
1532 /* The ABI (version 1.9) specifies that structs containing a
1533 single floating-point value, at any level of nesting of
1534 single-member structs, are passed in floating-point registers. */
1535 if (TYPE_CODE (type) == TYPE_CODE_STRUCT
1536 && TYPE_NFIELDS (type) == 1)
1538 while (TYPE_CODE (type) == TYPE_CODE_STRUCT
1539 && TYPE_NFIELDS (type) == 1)
1540 type = check_typedef (TYPE_FIELD_TYPE (type, 0));
1542 if (TYPE_CODE (type) == TYPE_CODE_FLT)
1543 ppc64_sysv_abi_push_freg (gdbarch, type, val, argpos);
1546 /* In the ELFv2 ABI, homogeneous floating-point or vector
1547 aggregates are passed in a series of registers. */
1548 if (tdep->elf_abi == POWERPC_ELF_V2)
1550 struct type *eltype;
1553 if (ppc64_elfv2_abi_homogeneous_aggregate (type, &eltype, &nelt))
1554 for (i = 0; i < nelt; i++)
1556 const gdb_byte *elval = val + i * TYPE_LENGTH (eltype);
1558 if (TYPE_CODE (eltype) == TYPE_CODE_FLT
1559 || TYPE_CODE (eltype) == TYPE_CODE_DECFLOAT)
1560 ppc64_sysv_abi_push_freg (gdbarch, eltype, elval, argpos);
1561 else if (TYPE_CODE (eltype) == TYPE_CODE_ARRAY
1562 && TYPE_VECTOR (eltype)
1563 && tdep->vector_abi == POWERPC_VEC_ALTIVEC
1564 && TYPE_LENGTH (eltype) == 16)
1565 ppc64_sysv_abi_push_vreg (gdbarch, elval, argpos);
1571 /* Pass the arguments in either registers, or in the stack. Using the
1572 ppc 64 bit SysV ABI.
1574 This implements a dumbed down version of the ABI. It always writes
1575 values to memory, GPR and FPR, even when not necessary. Doing this
1576 greatly simplifies the logic. */
1579 ppc64_sysv_abi_push_dummy_call (struct gdbarch *gdbarch,
1580 struct value *function,
1581 struct regcache *regcache, CORE_ADDR bp_addr,
1582 int nargs, struct value **args, CORE_ADDR sp,
1583 int struct_return, CORE_ADDR struct_addr)
1585 CORE_ADDR func_addr = find_function_addr (function, NULL);
1586 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1587 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1588 int opencl_abi = ppc_sysv_use_opencl_abi (value_type (function));
1589 ULONGEST back_chain;
1590 /* See for-loop comment below. */
1592 /* Size of the by-reference parameter copy region, the final value is
1593 computed in the for-loop below. */
1594 LONGEST refparam_size = 0;
1595 /* Size of the general parameter region, the final value is computed
1596 in the for-loop below. */
1597 LONGEST gparam_size = 0;
1598 /* Kevin writes ... I don't mind seeing tdep->wordsize used in the
1599 calls to align_up(), align_down(), etc. because this makes it
1600 easier to reuse this code (in a copy/paste sense) in the future,
1601 but it is a 64-bit ABI and asserting that the wordsize is 8 bytes
1602 at some point makes it easier to verify that this function is
1603 correct without having to do a non-local analysis to figure out
1604 the possible values of tdep->wordsize. */
1605 gdb_assert (tdep->wordsize == 8);
1607 /* This function exists to support a calling convention that
1608 requires floating-point registers. It shouldn't be used on
1609 processors that lack them. */
1610 gdb_assert (ppc_floating_point_unit_p (gdbarch));
1612 /* By this stage in the proceedings, SP has been decremented by "red
1613 zone size" + "struct return size". Fetch the stack-pointer from
1614 before this and use that as the BACK_CHAIN. */
1615 regcache_cooked_read_unsigned (regcache, gdbarch_sp_regnum (gdbarch),
1618 /* Go through the argument list twice.
1620 Pass 1: Compute the function call's stack space and register
1623 Pass 2: Replay the same computation but this time also write the
1624 values out to the target. */
1626 for (write_pass = 0; write_pass < 2; write_pass++)
1630 struct ppc64_sysv_argpos argpos;
1637 /* During the first pass, GPARAM and REFPARAM are more like
1638 offsets (start address zero) than addresses. That way
1639 they accumulate the total stack space each region
1641 argpos.regcache = NULL;
1643 argpos.refparam = 0;
1647 /* Decrement the stack pointer making space for the Altivec
1648 and general on-stack parameters. Set refparam and gparam
1649 to their corresponding regions. */
1650 argpos.regcache = regcache;
1651 argpos.refparam = align_down (sp - refparam_size, 16);
1652 argpos.gparam = align_down (argpos.refparam - gparam_size, 16);
1653 /* Add in space for the TOC, link editor double word (v1 only),
1654 compiler double word (v1 only), LR save area, CR save area,
1656 if (tdep->elf_abi == POWERPC_ELF_V1)
1657 sp = align_down (argpos.gparam - 48, 16);
1659 sp = align_down (argpos.gparam - 32, 16);
1662 /* If the function is returning a `struct', then there is an
1663 extra hidden parameter (which will be passed in r3)
1664 containing the address of that struct.. In that case we
1665 should advance one word and start from r4 register to copy
1666 parameters. This also consumes one on-stack parameter slot. */
1668 ppc64_sysv_abi_push_integer (gdbarch, struct_addr, &argpos);
1670 for (argno = 0; argno < nargs; argno++)
1672 struct value *arg = args[argno];
1673 struct type *type = check_typedef (value_type (arg));
1674 const bfd_byte *val = value_contents (arg);
1676 if (TYPE_CODE (type) == TYPE_CODE_COMPLEX)
1678 /* Complex types are passed as if two independent scalars. */
1679 struct type *eltype = check_typedef (TYPE_TARGET_TYPE (type));
1681 ppc64_sysv_abi_push_param (gdbarch, eltype, val, &argpos);
1682 ppc64_sysv_abi_push_param (gdbarch, eltype,
1683 val + TYPE_LENGTH (eltype), &argpos);
1685 else if (TYPE_CODE (type) == TYPE_CODE_ARRAY && TYPE_VECTOR (type)
1688 /* OpenCL vectors shorter than 16 bytes are passed as if
1689 a series of independent scalars; OpenCL vectors 16 bytes
1690 or longer are passed as if a series of AltiVec vectors. */
1691 struct type *eltype;
1694 if (TYPE_LENGTH (type) < 16)
1695 eltype = check_typedef (TYPE_TARGET_TYPE (type));
1697 eltype = register_type (gdbarch, tdep->ppc_vr0_regnum);
1699 nelt = TYPE_LENGTH (type) / TYPE_LENGTH (eltype);
1700 for (i = 0; i < nelt; i++)
1702 const gdb_byte *elval = val + i * TYPE_LENGTH (eltype);
1704 ppc64_sysv_abi_push_param (gdbarch, eltype, elval, &argpos);
1709 /* All other types are passed as single arguments. */
1710 ppc64_sysv_abi_push_param (gdbarch, type, val, &argpos);
1716 /* Save the true region sizes ready for the second pass. */
1717 refparam_size = argpos.refparam;
1718 /* Make certain that the general parameter save area is at
1719 least the minimum 8 registers (or doublewords) in size. */
1720 if (argpos.greg < 8)
1721 gparam_size = 8 * tdep->wordsize;
1723 gparam_size = argpos.gparam;
1728 regcache_cooked_write_signed (regcache, gdbarch_sp_regnum (gdbarch), sp);
1730 /* Write the backchain (it occupies WORDSIZED bytes). */
1731 write_memory_signed_integer (sp, tdep->wordsize, byte_order, back_chain);
1733 /* Point the inferior function call's return address at the dummy's
1735 regcache_cooked_write_signed (regcache, tdep->ppc_lr_regnum, bp_addr);
1737 /* In the ELFv1 ABI, use the func_addr to find the descriptor, and use
1738 that to find the TOC. If we're calling via a function pointer,
1739 the pointer itself identifies the descriptor. */
1740 if (tdep->elf_abi == POWERPC_ELF_V1)
1742 struct type *ftype = check_typedef (value_type (function));
1743 CORE_ADDR desc_addr = value_as_address (function);
1745 if (TYPE_CODE (ftype) == TYPE_CODE_PTR
1746 || convert_code_addr_to_desc_addr (func_addr, &desc_addr))
1748 /* The TOC is the second double word in the descriptor. */
1750 read_memory_unsigned_integer (desc_addr + tdep->wordsize,
1751 tdep->wordsize, byte_order);
1753 regcache_cooked_write_unsigned (regcache,
1754 tdep->ppc_gp0_regnum + 2, toc);
1758 /* In the ELFv2 ABI, we need to pass the target address in r12 since
1759 we may be calling a global entry point. */
1760 if (tdep->elf_abi == POWERPC_ELF_V2)
1761 regcache_cooked_write_unsigned (regcache,
1762 tdep->ppc_gp0_regnum + 12, func_addr);
1767 /* Subroutine of ppc64_sysv_abi_return_value that handles "base" types:
1768 integer, floating-point, and AltiVec vector types.
1770 This routine also handles components of aggregate return types;
1771 INDEX describes which part of the aggregate is to be handled.
1773 Returns true if VALTYPE is some such base type that could be handled,
1776 ppc64_sysv_abi_return_value_base (struct gdbarch *gdbarch, struct type *valtype,
1777 struct regcache *regcache, gdb_byte *readbuf,
1778 const gdb_byte *writebuf, int index)
1780 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1782 /* Integers live in GPRs starting at r3. */
1783 if ((TYPE_CODE (valtype) == TYPE_CODE_INT
1784 || TYPE_CODE (valtype) == TYPE_CODE_ENUM
1785 || TYPE_CODE (valtype) == TYPE_CODE_CHAR
1786 || TYPE_CODE (valtype) == TYPE_CODE_BOOL)
1787 && TYPE_LENGTH (valtype) <= 8)
1789 int regnum = tdep->ppc_gp0_regnum + 3 + index;
1791 if (writebuf != NULL)
1793 /* Be careful to sign extend the value. */
1794 regcache_cooked_write_unsigned (regcache, regnum,
1795 unpack_long (valtype, writebuf));
1797 if (readbuf != NULL)
1799 /* Extract the integer from GPR. Since this is truncating the
1800 value, there isn't a sign extension problem. */
1803 regcache_cooked_read_unsigned (regcache, regnum, ®val);
1804 store_unsigned_integer (readbuf, TYPE_LENGTH (valtype),
1805 gdbarch_byte_order (gdbarch), regval);
1810 /* Floats and doubles go in f1 .. f13. 32-bit floats are converted
1812 if (TYPE_LENGTH (valtype) <= 8
1813 && TYPE_CODE (valtype) == TYPE_CODE_FLT)
1815 int regnum = tdep->ppc_fp0_regnum + 1 + index;
1816 struct type *regtype = register_type (gdbarch, regnum);
1817 gdb_byte regval[MAX_REGISTER_SIZE];
1819 if (writebuf != NULL)
1821 convert_typed_floating (writebuf, valtype, regval, regtype);
1822 regcache_cooked_write (regcache, regnum, regval);
1824 if (readbuf != NULL)
1826 regcache_cooked_read (regcache, regnum, regval);
1827 convert_typed_floating (regval, regtype, readbuf, valtype);
1832 /* Floats and doubles go in f1 .. f13. 32-bit decimal floats are
1833 placed in the least significant word. */
1834 if (TYPE_LENGTH (valtype) <= 8
1835 && TYPE_CODE (valtype) == TYPE_CODE_DECFLOAT)
1837 int regnum = tdep->ppc_fp0_regnum + 1 + index;
1840 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
1841 offset = 8 - TYPE_LENGTH (valtype);
1843 if (writebuf != NULL)
1844 regcache_cooked_write_part (regcache, regnum,
1845 offset, TYPE_LENGTH (valtype), writebuf);
1846 if (readbuf != NULL)
1847 regcache_cooked_read_part (regcache, regnum,
1848 offset, TYPE_LENGTH (valtype), readbuf);
1852 /* IBM long double stored in two consecutive FPRs. */
1853 if (TYPE_LENGTH (valtype) == 16
1854 && TYPE_CODE (valtype) == TYPE_CODE_FLT
1855 && (gdbarch_long_double_format (gdbarch)
1856 == floatformats_ibm_long_double))
1858 int regnum = tdep->ppc_fp0_regnum + 1 + 2 * index;
1860 if (writebuf != NULL)
1862 regcache_cooked_write (regcache, regnum, writebuf);
1863 regcache_cooked_write (regcache, regnum + 1, writebuf + 8);
1865 if (readbuf != NULL)
1867 regcache_cooked_read (regcache, regnum, readbuf);
1868 regcache_cooked_read (regcache, regnum + 1, readbuf + 8);
1873 /* 128-bit decimal floating-point values are stored in an even/odd
1874 pair of FPRs, with the even FPR holding the most significant half. */
1875 if (TYPE_LENGTH (valtype) == 16
1876 && TYPE_CODE (valtype) == TYPE_CODE_DECFLOAT)
1878 int regnum = tdep->ppc_fp0_regnum + 2 + 2 * index;
1879 int lopart = gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG ? 8 : 0;
1880 int hipart = gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG ? 0 : 8;
1882 if (writebuf != NULL)
1884 regcache_cooked_write (regcache, regnum, writebuf + hipart);
1885 regcache_cooked_write (regcache, regnum + 1, writebuf + lopart);
1887 if (readbuf != NULL)
1889 regcache_cooked_read (regcache, regnum, readbuf + hipart);
1890 regcache_cooked_read (regcache, regnum + 1, readbuf + lopart);
1895 /* AltiVec vectors are returned in VRs starting at v2. */
1896 if (TYPE_LENGTH (valtype) == 16
1897 && TYPE_CODE (valtype) == TYPE_CODE_ARRAY && TYPE_VECTOR (valtype)
1898 && tdep->vector_abi == POWERPC_VEC_ALTIVEC)
1900 int regnum = tdep->ppc_vr0_regnum + 2 + index;
1902 if (writebuf != NULL)
1903 regcache_cooked_write (regcache, regnum, writebuf);
1904 if (readbuf != NULL)
1905 regcache_cooked_read (regcache, regnum, readbuf);
1909 /* Short vectors are returned in GPRs starting at r3. */
1910 if (TYPE_LENGTH (valtype) <= 8
1911 && TYPE_CODE (valtype) == TYPE_CODE_ARRAY && TYPE_VECTOR (valtype))
1913 int regnum = tdep->ppc_gp0_regnum + 3 + index;
1916 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
1917 offset = 8 - TYPE_LENGTH (valtype);
1919 if (writebuf != NULL)
1920 regcache_cooked_write_part (regcache, regnum,
1921 offset, TYPE_LENGTH (valtype), writebuf);
1922 if (readbuf != NULL)
1923 regcache_cooked_read_part (regcache, regnum,
1924 offset, TYPE_LENGTH (valtype), readbuf);
1931 /* The 64 bit ABI return value convention.
1933 Return non-zero if the return-value is stored in a register, return
1934 0 if the return-value is instead stored on the stack (a.k.a.,
1935 struct return convention).
1937 For a return-value stored in a register: when WRITEBUF is non-NULL,
1938 copy the buffer to the corresponding register return-value location
1939 location; when READBUF is non-NULL, fill the buffer from the
1940 corresponding register return-value location. */
1941 enum return_value_convention
1942 ppc64_sysv_abi_return_value (struct gdbarch *gdbarch, struct value *function,
1943 struct type *valtype, struct regcache *regcache,
1944 gdb_byte *readbuf, const gdb_byte *writebuf)
1946 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1947 struct type *func_type = function ? value_type (function) : NULL;
1948 int opencl_abi = func_type? ppc_sysv_use_opencl_abi (func_type) : 0;
1949 struct type *eltype;
1952 /* This function exists to support a calling convention that
1953 requires floating-point registers. It shouldn't be used on
1954 processors that lack them. */
1955 gdb_assert (ppc_floating_point_unit_p (gdbarch));
1957 /* Complex types are returned as if two independent scalars. */
1958 if (TYPE_CODE (valtype) == TYPE_CODE_COMPLEX)
1960 eltype = check_typedef (TYPE_TARGET_TYPE (valtype));
1962 for (i = 0; i < 2; i++)
1964 ok = ppc64_sysv_abi_return_value_base (gdbarch, eltype, regcache,
1965 readbuf, writebuf, i);
1969 readbuf += TYPE_LENGTH (eltype);
1971 writebuf += TYPE_LENGTH (eltype);
1973 return RETURN_VALUE_REGISTER_CONVENTION;
1976 /* OpenCL vectors shorter than 16 bytes are returned as if
1977 a series of independent scalars; OpenCL vectors 16 bytes
1978 or longer are returned as if a series of AltiVec vectors. */
1979 if (TYPE_CODE (valtype) == TYPE_CODE_ARRAY && TYPE_VECTOR (valtype)
1982 if (TYPE_LENGTH (valtype) < 16)
1983 eltype = check_typedef (TYPE_TARGET_TYPE (valtype));
1985 eltype = register_type (gdbarch, tdep->ppc_vr0_regnum);
1987 nelt = TYPE_LENGTH (valtype) / TYPE_LENGTH (eltype);
1988 for (i = 0; i < nelt; i++)
1990 ok = ppc64_sysv_abi_return_value_base (gdbarch, eltype, regcache,
1991 readbuf, writebuf, i);
1995 readbuf += TYPE_LENGTH (eltype);
1997 writebuf += TYPE_LENGTH (eltype);
1999 return RETURN_VALUE_REGISTER_CONVENTION;
2002 /* All pointers live in r3. */
2003 if (TYPE_CODE (valtype) == TYPE_CODE_PTR
2004 || TYPE_CODE (valtype) == TYPE_CODE_REF)
2006 int regnum = tdep->ppc_gp0_regnum + 3;
2008 if (writebuf != NULL)
2009 regcache_cooked_write (regcache, regnum, writebuf);
2010 if (readbuf != NULL)
2011 regcache_cooked_read (regcache, regnum, readbuf);
2012 return RETURN_VALUE_REGISTER_CONVENTION;
2015 /* Small character arrays are returned, right justified, in r3. */
2016 if (TYPE_CODE (valtype) == TYPE_CODE_ARRAY
2017 && !TYPE_VECTOR (valtype)
2018 && TYPE_LENGTH (valtype) <= 8
2019 && TYPE_CODE (TYPE_TARGET_TYPE (valtype)) == TYPE_CODE_INT
2020 && TYPE_LENGTH (TYPE_TARGET_TYPE (valtype)) == 1)
2022 int regnum = tdep->ppc_gp0_regnum + 3;
2023 int offset = (register_size (gdbarch, regnum) - TYPE_LENGTH (valtype));
2025 if (writebuf != NULL)
2026 regcache_cooked_write_part (regcache, regnum,
2027 offset, TYPE_LENGTH (valtype), writebuf);
2028 if (readbuf != NULL)
2029 regcache_cooked_read_part (regcache, regnum,
2030 offset, TYPE_LENGTH (valtype), readbuf);
2031 return RETURN_VALUE_REGISTER_CONVENTION;
2034 /* In the ELFv2 ABI, homogeneous floating-point or vector
2035 aggregates are returned in registers. */
2036 if (tdep->elf_abi == POWERPC_ELF_V2
2037 && ppc64_elfv2_abi_homogeneous_aggregate (valtype, &eltype, &nelt)
2038 && (TYPE_CODE (eltype) == TYPE_CODE_FLT
2039 || TYPE_CODE (eltype) == TYPE_CODE_DECFLOAT
2040 || (TYPE_CODE (eltype) == TYPE_CODE_ARRAY
2041 && TYPE_VECTOR (eltype)
2042 && tdep->vector_abi == POWERPC_VEC_ALTIVEC
2043 && TYPE_LENGTH (eltype) == 16)))
2045 for (i = 0; i < nelt; i++)
2047 ok = ppc64_sysv_abi_return_value_base (gdbarch, eltype, regcache,
2048 readbuf, writebuf, i);
2052 readbuf += TYPE_LENGTH (eltype);
2054 writebuf += TYPE_LENGTH (eltype);
2057 return RETURN_VALUE_REGISTER_CONVENTION;
2060 /* In the ELFv2 ABI, aggregate types of up to 16 bytes are
2061 returned in registers r3:r4. */
2062 if (tdep->elf_abi == POWERPC_ELF_V2
2063 && TYPE_LENGTH (valtype) <= 16
2064 && (TYPE_CODE (valtype) == TYPE_CODE_STRUCT
2065 || TYPE_CODE (valtype) == TYPE_CODE_UNION
2066 || (TYPE_CODE (valtype) == TYPE_CODE_ARRAY
2067 && !TYPE_VECTOR (valtype))))
2069 int n_regs = ((TYPE_LENGTH (valtype) + tdep->wordsize - 1)
2073 for (i = 0; i < n_regs; i++)
2075 gdb_byte regval[MAX_REGISTER_SIZE];
2076 int regnum = tdep->ppc_gp0_regnum + 3 + i;
2077 int offset = i * tdep->wordsize;
2078 int len = TYPE_LENGTH (valtype) - offset;
2080 if (len > tdep->wordsize)
2081 len = tdep->wordsize;
2083 if (writebuf != NULL)
2085 memset (regval, 0, sizeof regval);
2086 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG
2088 memcpy (regval + tdep->wordsize - len, writebuf, len);
2090 memcpy (regval, writebuf + offset, len);
2091 regcache_cooked_write (regcache, regnum, regval);
2093 if (readbuf != NULL)
2095 regcache_cooked_read (regcache, regnum, regval);
2096 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG
2098 memcpy (readbuf, regval + tdep->wordsize - len, len);
2100 memcpy (readbuf + offset, regval, len);
2103 return RETURN_VALUE_REGISTER_CONVENTION;
2106 /* Handle plain base types. */
2107 if (ppc64_sysv_abi_return_value_base (gdbarch, valtype, regcache,
2108 readbuf, writebuf, 0))
2109 return RETURN_VALUE_REGISTER_CONVENTION;
2111 return RETURN_VALUE_STRUCT_CONVENTION;