1 /* Target-dependent code for PowerPC systems using the SVR4 ABI
2 for GDB, the GNU debugger.
4 Copyright (C) 2000-2018 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/>. */
31 #include "target-float.h"
35 /* Check whether FTPYE is a (pointer to) function type that should use
36 the OpenCL vector ABI. */
39 ppc_sysv_use_opencl_abi (struct type *ftype)
41 ftype = check_typedef (ftype);
43 if (TYPE_CODE (ftype) == TYPE_CODE_PTR)
44 ftype = check_typedef (TYPE_TARGET_TYPE (ftype));
46 return (TYPE_CODE (ftype) == TYPE_CODE_FUNC
47 && TYPE_CALLING_CONVENTION (ftype) == DW_CC_GDB_IBM_OpenCL);
50 /* Pass the arguments in either registers, or in the stack. Using the
51 ppc sysv ABI, the first eight words of the argument list (that might
52 be less than eight parameters if some parameters occupy more than one
53 word) are passed in r3..r10 registers. float and double parameters are
54 passed in fpr's, in addition to that. Rest of the parameters if any
55 are passed in user stack.
57 If the function is returning a structure, then the return address is passed
58 in r3, then the first 7 words of the parametes can be passed in registers,
62 ppc_sysv_abi_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
63 struct regcache *regcache, CORE_ADDR bp_addr,
64 int nargs, struct value **args, CORE_ADDR sp,
65 function_call_return_method return_method,
66 CORE_ADDR struct_addr)
68 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
69 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
70 int opencl_abi = ppc_sysv_use_opencl_abi (value_type (function));
72 int argspace = 0; /* 0 is an initial wrong guess. */
75 gdb_assert (tdep->wordsize == 4);
77 regcache_cooked_read_unsigned (regcache, gdbarch_sp_regnum (gdbarch),
80 /* Go through the argument list twice.
82 Pass 1: Figure out how much new stack space is required for
83 arguments and pushed values. Unlike the PowerOpen ABI, the SysV
84 ABI doesn't reserve any extra space for parameters which are put
85 in registers, but does always push structures and then pass their
88 Pass 2: Replay the same computation but this time also write the
89 values out to the target. */
91 for (write_pass = 0; write_pass < 2; write_pass++)
94 /* Next available floating point register for float and double
97 /* Next available general register for non-float, non-vector
100 /* Next available vector register for vector arguments. */
102 /* Arguments start above the "LR save word" and "Back chain". */
103 int argoffset = 2 * tdep->wordsize;
104 /* Structures start after the arguments. */
105 int structoffset = argoffset + argspace;
107 /* If the function is returning a `struct', then the first word
108 (which will be passed in r3) is used for struct return
109 address. In that case we should advance one word and start
110 from r4 register to copy parameters. */
111 if (return_method == return_method_struct)
114 regcache_cooked_write_signed (regcache,
115 tdep->ppc_gp0_regnum + greg,
120 for (argno = 0; argno < nargs; argno++)
122 struct value *arg = args[argno];
123 struct type *type = check_typedef (value_type (arg));
124 int len = TYPE_LENGTH (type);
125 const bfd_byte *val = value_contents (arg);
127 if (TYPE_CODE (type) == TYPE_CODE_FLT && len <= 8
128 && !tdep->soft_float)
130 /* Floating point value converted to "double" then
131 passed in an FP register, when the registers run out,
132 8 byte aligned stack is used. */
137 /* Always store the floating point value using
138 the register's floating-point format. */
139 gdb_byte regval[PPC_MAX_REGISTER_SIZE];
141 = register_type (gdbarch, tdep->ppc_fp0_regnum + freg);
142 target_float_convert (val, type, regval, regtype);
143 regcache->cooked_write (tdep->ppc_fp0_regnum + freg,
150 /* The SysV ABI tells us to convert floats to
151 doubles before writing them to an 8 byte aligned
152 stack location. Unfortunately GCC does not do
153 that, and stores floats into 4 byte aligned
154 locations without converting them to doubles.
155 Since there is no know compiler that actually
156 follows the ABI here, we implement the GCC
159 /* Align to 4 bytes or 8 bytes depending on the type of
160 the argument (float or double). */
161 argoffset = align_up (argoffset, len);
163 write_memory (sp + argoffset, val, len);
167 else if (TYPE_CODE (type) == TYPE_CODE_FLT
170 && (gdbarch_long_double_format (gdbarch)
171 == floatformats_ibm_long_double))
173 /* IBM long double passed in two FP registers if
174 available, otherwise 8-byte aligned stack. */
179 regcache->cooked_write (tdep->ppc_fp0_regnum + freg, val);
180 regcache->cooked_write (tdep->ppc_fp0_regnum + freg + 1,
187 argoffset = align_up (argoffset, 8);
189 write_memory (sp + argoffset, val, len);
194 && (TYPE_CODE (type) == TYPE_CODE_INT /* long long */
195 || TYPE_CODE (type) == TYPE_CODE_FLT /* double */
196 || (TYPE_CODE (type) == TYPE_CODE_DECFLOAT
197 && tdep->soft_float)))
199 /* "long long" or soft-float "double" or "_Decimal64"
200 passed in an odd/even register pair with the low
201 addressed word in the odd register and the high
202 addressed word in the even register, or when the
203 registers run out an 8 byte aligned stack
207 /* Just in case GREG was 10. */
209 argoffset = align_up (argoffset, 8);
211 write_memory (sp + argoffset, val, len);
216 /* Must start on an odd register - r3/r4 etc. */
221 regcache->cooked_write (tdep->ppc_gp0_regnum + greg + 0,
223 regcache->cooked_write (tdep->ppc_gp0_regnum + greg + 1,
230 && ((TYPE_CODE (type) == TYPE_CODE_FLT
231 && (gdbarch_long_double_format (gdbarch)
232 == floatformats_ibm_long_double))
233 || (TYPE_CODE (type) == TYPE_CODE_DECFLOAT
234 && tdep->soft_float)))
236 /* Soft-float IBM long double or _Decimal128 passed in
237 four consecutive registers, or on the stack. The
238 registers are not necessarily odd/even pairs. */
242 argoffset = align_up (argoffset, 8);
244 write_memory (sp + argoffset, val, len);
251 regcache->cooked_write (tdep->ppc_gp0_regnum + greg + 0,
253 regcache->cooked_write (tdep->ppc_gp0_regnum + greg + 1,
255 regcache->cooked_write (tdep->ppc_gp0_regnum + greg + 2,
257 regcache->cooked_write (tdep->ppc_gp0_regnum + greg + 3,
263 else if (TYPE_CODE (type) == TYPE_CODE_DECFLOAT && len <= 8
264 && !tdep->soft_float)
266 /* 32-bit and 64-bit decimal floats go in f1 .. f8. They can
273 gdb_byte regval[PPC_MAX_REGISTER_SIZE];
276 /* 32-bit decimal floats are right aligned in the
278 if (TYPE_LENGTH (type) == 4)
280 memcpy (regval + 4, val, 4);
286 regcache->cooked_write (tdep->ppc_fp0_regnum + freg, p);
293 argoffset = align_up (argoffset, len);
296 /* Write value in the stack's parameter save area. */
297 write_memory (sp + argoffset, val, len);
302 else if (TYPE_CODE (type) == TYPE_CODE_DECFLOAT && len == 16
303 && !tdep->soft_float)
305 /* 128-bit decimal floats go in f2 .. f7, always in even/odd
306 pairs. They can end up in memory, using two doublewords. */
310 /* Make sure freg is even. */
315 regcache->cooked_write (tdep->ppc_fp0_regnum + freg, val);
316 regcache->cooked_write (tdep->ppc_fp0_regnum + freg + 1,
322 argoffset = align_up (argoffset, 8);
325 write_memory (sp + argoffset, val, 16);
330 /* If a 128-bit decimal float goes to the stack because only f7
331 and f8 are free (thus there's no even/odd register pair
332 available), these registers should be marked as occupied.
333 Hence we increase freg even when writing to memory. */
337 && TYPE_CODE (type) == TYPE_CODE_ARRAY
338 && TYPE_VECTOR (type)
341 /* OpenCL vectors shorter than 16 bytes are passed as if
342 a series of independent scalars. */
343 struct type *eltype = check_typedef (TYPE_TARGET_TYPE (type));
344 int i, nelt = TYPE_LENGTH (type) / TYPE_LENGTH (eltype);
346 for (i = 0; i < nelt; i++)
348 const gdb_byte *elval = val + i * TYPE_LENGTH (eltype);
350 if (TYPE_CODE (eltype) == TYPE_CODE_FLT && !tdep->soft_float)
356 int regnum = tdep->ppc_fp0_regnum + freg;
357 gdb_byte regval[PPC_MAX_REGISTER_SIZE];
359 = register_type (gdbarch, regnum);
360 target_float_convert (elval, eltype,
362 regcache->cooked_write (regnum, regval);
368 argoffset = align_up (argoffset, len);
370 write_memory (sp + argoffset, val, len);
374 else if (TYPE_LENGTH (eltype) == 8)
378 /* Just in case GREG was 10. */
380 argoffset = align_up (argoffset, 8);
382 write_memory (sp + argoffset, elval,
383 TYPE_LENGTH (eltype));
388 /* Must start on an odd register - r3/r4 etc. */
393 int regnum = tdep->ppc_gp0_regnum + greg;
394 regcache->cooked_write (regnum + 0, elval + 0);
395 regcache->cooked_write (regnum + 1, elval + 4);
402 gdb_byte word[PPC_MAX_REGISTER_SIZE];
403 store_unsigned_integer (word, tdep->wordsize, byte_order,
404 unpack_long (eltype, elval));
409 regcache->cooked_write (tdep->ppc_gp0_regnum + greg,
415 argoffset = align_up (argoffset, tdep->wordsize);
417 write_memory (sp + argoffset, word, tdep->wordsize);
418 argoffset += tdep->wordsize;
424 && TYPE_CODE (type) == TYPE_CODE_ARRAY
425 && TYPE_VECTOR (type)
428 /* OpenCL vectors 16 bytes or longer are passed as if
429 a series of AltiVec vectors. */
432 for (i = 0; i < len / 16; i++)
434 const gdb_byte *elval = val + i * 16;
439 regcache->cooked_write (tdep->ppc_vr0_regnum + vreg,
445 argoffset = align_up (argoffset, 16);
447 write_memory (sp + argoffset, elval, 16);
453 && TYPE_CODE (type) == TYPE_CODE_ARRAY
454 && TYPE_VECTOR (type)
455 && tdep->vector_abi == POWERPC_VEC_ALTIVEC)
457 /* Vector parameter passed in an Altivec register, or
458 when that runs out, 16 byte aligned stack location. */
462 regcache->cooked_write (tdep->ppc_vr0_regnum + vreg, val);
467 argoffset = align_up (argoffset, 16);
469 write_memory (sp + argoffset, val, 16);
474 && TYPE_CODE (type) == TYPE_CODE_ARRAY
475 && TYPE_VECTOR (type)
476 && tdep->vector_abi == POWERPC_VEC_SPE)
478 /* Vector parameter passed in an e500 register, or when
479 that runs out, 8 byte aligned stack location. Note
480 that since e500 vector and general purpose registers
481 both map onto the same underlying register set, a
482 "greg" and not a "vreg" is consumed here. A cooked
483 write stores the value in the correct locations
484 within the raw register cache. */
488 regcache->cooked_write (tdep->ppc_ev0_regnum + greg, val);
493 argoffset = align_up (argoffset, 8);
495 write_memory (sp + argoffset, val, 8);
501 /* Reduce the parameter down to something that fits in a
503 gdb_byte word[PPC_MAX_REGISTER_SIZE];
504 memset (word, 0, PPC_MAX_REGISTER_SIZE);
505 if (len > tdep->wordsize
506 || TYPE_CODE (type) == TYPE_CODE_STRUCT
507 || TYPE_CODE (type) == TYPE_CODE_UNION)
509 /* Structs and large values are put in an
510 aligned stack slot ... */
511 if (TYPE_CODE (type) == TYPE_CODE_ARRAY
512 && TYPE_VECTOR (type)
514 structoffset = align_up (structoffset, 16);
516 structoffset = align_up (structoffset, 8);
519 write_memory (sp + structoffset, val, len);
520 /* ... and then a "word" pointing to that address is
521 passed as the parameter. */
522 store_unsigned_integer (word, tdep->wordsize, byte_order,
526 else if (TYPE_CODE (type) == TYPE_CODE_INT)
527 /* Sign or zero extend the "int" into a "word". */
528 store_unsigned_integer (word, tdep->wordsize, byte_order,
529 unpack_long (type, val));
531 /* Always goes in the low address. */
532 memcpy (word, val, len);
533 /* Store that "word" in a register, or on the stack.
534 The words have "4" byte alignment. */
538 regcache->cooked_write (tdep->ppc_gp0_regnum + greg, word);
543 argoffset = align_up (argoffset, tdep->wordsize);
545 write_memory (sp + argoffset, word, tdep->wordsize);
546 argoffset += tdep->wordsize;
551 /* Compute the actual stack space requirements. */
554 /* Remember the amount of space needed by the arguments. */
555 argspace = argoffset;
556 /* Allocate space for both the arguments and the structures. */
557 sp -= (argoffset + structoffset);
558 /* Ensure that the stack is still 16 byte aligned. */
559 sp = align_down (sp, 16);
562 /* The psABI says that "A caller of a function that takes a
563 variable argument list shall set condition register bit 6 to
564 1 if it passes one or more arguments in the floating-point
565 registers. It is strongly recommended that the caller set the
566 bit to 0 otherwise..." Doing this for normal functions too
572 regcache_cooked_read_unsigned (regcache, tdep->ppc_cr_regnum, &cr);
577 regcache_cooked_write_unsigned (regcache, tdep->ppc_cr_regnum, cr);
582 regcache_cooked_write_signed (regcache, gdbarch_sp_regnum (gdbarch), sp);
584 /* Write the backchain (it occupies WORDSIZED bytes). */
585 write_memory_signed_integer (sp, tdep->wordsize, byte_order, saved_sp);
587 /* Point the inferior function call's return address at the dummy's
589 regcache_cooked_write_signed (regcache, tdep->ppc_lr_regnum, bp_addr);
594 /* Handle the return-value conventions for Decimal Floating Point values. */
595 static enum return_value_convention
596 get_decimal_float_return_value (struct gdbarch *gdbarch, struct type *valtype,
597 struct regcache *regcache, gdb_byte *readbuf,
598 const gdb_byte *writebuf)
600 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
602 gdb_assert (TYPE_CODE (valtype) == TYPE_CODE_DECFLOAT);
604 /* 32-bit and 64-bit decimal floats in f1. */
605 if (TYPE_LENGTH (valtype) <= 8)
607 if (writebuf != NULL)
609 gdb_byte regval[PPC_MAX_REGISTER_SIZE];
612 /* 32-bit decimal float is right aligned in the doubleword. */
613 if (TYPE_LENGTH (valtype) == 4)
615 memcpy (regval + 4, writebuf, 4);
621 regcache->cooked_write (tdep->ppc_fp0_regnum + 1, p);
625 regcache->cooked_read (tdep->ppc_fp0_regnum + 1, readbuf);
627 /* Left align 32-bit decimal float. */
628 if (TYPE_LENGTH (valtype) == 4)
629 memcpy (readbuf, readbuf + 4, 4);
632 /* 128-bit decimal floats in f2,f3. */
633 else if (TYPE_LENGTH (valtype) == 16)
635 if (writebuf != NULL || readbuf != NULL)
639 for (i = 0; i < 2; i++)
641 if (writebuf != NULL)
642 regcache->cooked_write (tdep->ppc_fp0_regnum + 2 + i,
645 regcache->cooked_read (tdep->ppc_fp0_regnum + 2 + i,
652 internal_error (__FILE__, __LINE__, _("Unknown decimal float size."));
654 return RETURN_VALUE_REGISTER_CONVENTION;
657 /* Handle the return-value conventions specified by the SysV 32-bit
658 PowerPC ABI (including all the supplements):
660 no floating-point: floating-point values returned using 32-bit
661 general-purpose registers.
663 Altivec: 128-bit vectors returned using vector registers.
665 e500: 64-bit vectors returned using the full full 64 bit EV
666 register, floating-point values returned using 32-bit
667 general-purpose registers.
669 GCC (broken): Small struct values right (instead of left) aligned
670 when returned in general-purpose registers. */
672 static enum return_value_convention
673 do_ppc_sysv_return_value (struct gdbarch *gdbarch, struct type *func_type,
674 struct type *type, struct regcache *regcache,
675 gdb_byte *readbuf, const gdb_byte *writebuf,
678 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
679 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
680 int opencl_abi = func_type? ppc_sysv_use_opencl_abi (func_type) : 0;
682 gdb_assert (tdep->wordsize == 4);
684 if (TYPE_CODE (type) == TYPE_CODE_FLT
685 && TYPE_LENGTH (type) <= 8
686 && !tdep->soft_float)
690 /* Floats and doubles stored in "f1". Convert the value to
691 the required type. */
692 gdb_byte regval[PPC_MAX_REGISTER_SIZE];
693 struct type *regtype = register_type (gdbarch,
694 tdep->ppc_fp0_regnum + 1);
695 regcache->cooked_read (tdep->ppc_fp0_regnum + 1, regval);
696 target_float_convert (regval, regtype, readbuf, type);
700 /* Floats and doubles stored in "f1". Convert the value to
701 the register's "double" type. */
702 gdb_byte regval[PPC_MAX_REGISTER_SIZE];
703 struct type *regtype = register_type (gdbarch, tdep->ppc_fp0_regnum);
704 target_float_convert (writebuf, type, regval, regtype);
705 regcache->cooked_write (tdep->ppc_fp0_regnum + 1, regval);
707 return RETURN_VALUE_REGISTER_CONVENTION;
709 if (TYPE_CODE (type) == TYPE_CODE_FLT
710 && TYPE_LENGTH (type) == 16
712 && (gdbarch_long_double_format (gdbarch)
713 == floatformats_ibm_long_double))
715 /* IBM long double stored in f1 and f2. */
718 regcache->cooked_read (tdep->ppc_fp0_regnum + 1, readbuf);
719 regcache->cooked_read (tdep->ppc_fp0_regnum + 2, readbuf + 8);
723 regcache->cooked_write (tdep->ppc_fp0_regnum + 1, writebuf);
724 regcache->cooked_write (tdep->ppc_fp0_regnum + 2, writebuf + 8);
726 return RETURN_VALUE_REGISTER_CONVENTION;
728 if (TYPE_LENGTH (type) == 16
729 && ((TYPE_CODE (type) == TYPE_CODE_FLT
730 && (gdbarch_long_double_format (gdbarch)
731 == floatformats_ibm_long_double))
732 || (TYPE_CODE (type) == TYPE_CODE_DECFLOAT && tdep->soft_float)))
734 /* Soft-float IBM long double or _Decimal128 stored in r3, r4,
738 regcache->cooked_read (tdep->ppc_gp0_regnum + 3, readbuf);
739 regcache->cooked_read (tdep->ppc_gp0_regnum + 4, readbuf + 4);
740 regcache->cooked_read (tdep->ppc_gp0_regnum + 5, readbuf + 8);
741 regcache->cooked_read (tdep->ppc_gp0_regnum + 6, readbuf + 12);
745 regcache->cooked_write (tdep->ppc_gp0_regnum + 3, writebuf);
746 regcache->cooked_write (tdep->ppc_gp0_regnum + 4, writebuf + 4);
747 regcache->cooked_write (tdep->ppc_gp0_regnum + 5, writebuf + 8);
748 regcache->cooked_write (tdep->ppc_gp0_regnum + 6, writebuf + 12);
750 return RETURN_VALUE_REGISTER_CONVENTION;
752 if ((TYPE_CODE (type) == TYPE_CODE_INT && TYPE_LENGTH (type) == 8)
753 || (TYPE_CODE (type) == TYPE_CODE_FLT && TYPE_LENGTH (type) == 8)
754 || (TYPE_CODE (type) == TYPE_CODE_DECFLOAT && TYPE_LENGTH (type) == 8
755 && tdep->soft_float))
759 /* A long long, double or _Decimal64 stored in the 32 bit
761 regcache->cooked_read (tdep->ppc_gp0_regnum + 3, readbuf + 0);
762 regcache->cooked_read (tdep->ppc_gp0_regnum + 4, readbuf + 4);
766 /* A long long, double or _Decimal64 stored in the 32 bit
768 regcache->cooked_write (tdep->ppc_gp0_regnum + 3, writebuf + 0);
769 regcache->cooked_write (tdep->ppc_gp0_regnum + 4, writebuf + 4);
771 return RETURN_VALUE_REGISTER_CONVENTION;
773 if (TYPE_CODE (type) == TYPE_CODE_DECFLOAT && !tdep->soft_float)
774 return get_decimal_float_return_value (gdbarch, type, regcache, readbuf,
776 else if ((TYPE_CODE (type) == TYPE_CODE_INT
777 || TYPE_CODE (type) == TYPE_CODE_CHAR
778 || TYPE_CODE (type) == TYPE_CODE_BOOL
779 || TYPE_CODE (type) == TYPE_CODE_PTR
780 || TYPE_IS_REFERENCE (type)
781 || TYPE_CODE (type) == TYPE_CODE_ENUM)
782 && TYPE_LENGTH (type) <= tdep->wordsize)
786 /* Some sort of integer stored in r3. Since TYPE isn't
787 bigger than the register, sign extension isn't a problem
788 - just do everything unsigned. */
790 regcache_cooked_read_unsigned (regcache, tdep->ppc_gp0_regnum + 3,
792 store_unsigned_integer (readbuf, TYPE_LENGTH (type), byte_order,
797 /* Some sort of integer stored in r3. Use unpack_long since
798 that should handle any required sign extension. */
799 regcache_cooked_write_unsigned (regcache, tdep->ppc_gp0_regnum + 3,
800 unpack_long (type, writebuf));
802 return RETURN_VALUE_REGISTER_CONVENTION;
804 /* OpenCL vectors < 16 bytes are returned as distinct
805 scalars in f1..f2 or r3..r10. */
806 if (TYPE_CODE (type) == TYPE_CODE_ARRAY
807 && TYPE_VECTOR (type)
808 && TYPE_LENGTH (type) < 16
811 struct type *eltype = check_typedef (TYPE_TARGET_TYPE (type));
812 int i, nelt = TYPE_LENGTH (type) / TYPE_LENGTH (eltype);
814 for (i = 0; i < nelt; i++)
816 int offset = i * TYPE_LENGTH (eltype);
818 if (TYPE_CODE (eltype) == TYPE_CODE_FLT)
820 int regnum = tdep->ppc_fp0_regnum + 1 + i;
821 gdb_byte regval[PPC_MAX_REGISTER_SIZE];
822 struct type *regtype = register_type (gdbarch, regnum);
824 if (writebuf != NULL)
826 target_float_convert (writebuf + offset, eltype,
828 regcache->cooked_write (regnum, regval);
832 regcache->cooked_read (regnum, regval);
833 target_float_convert (regval, regtype,
834 readbuf + offset, eltype);
839 int regnum = tdep->ppc_gp0_regnum + 3 + i;
842 if (writebuf != NULL)
844 regval = unpack_long (eltype, writebuf + offset);
845 regcache_cooked_write_unsigned (regcache, regnum, regval);
849 regcache_cooked_read_unsigned (regcache, regnum, ®val);
850 store_unsigned_integer (readbuf + offset,
851 TYPE_LENGTH (eltype), byte_order,
857 return RETURN_VALUE_REGISTER_CONVENTION;
859 /* OpenCL vectors >= 16 bytes are returned in v2..v9. */
860 if (TYPE_CODE (type) == TYPE_CODE_ARRAY
861 && TYPE_VECTOR (type)
862 && TYPE_LENGTH (type) >= 16
865 int n_regs = TYPE_LENGTH (type) / 16;
868 for (i = 0; i < n_regs; i++)
871 int regnum = tdep->ppc_vr0_regnum + 2 + i;
873 if (writebuf != NULL)
874 regcache->cooked_write (regnum, writebuf + offset);
876 regcache->cooked_read (regnum, readbuf + offset);
879 return RETURN_VALUE_REGISTER_CONVENTION;
881 if (TYPE_LENGTH (type) == 16
882 && TYPE_CODE (type) == TYPE_CODE_ARRAY
883 && TYPE_VECTOR (type)
884 && tdep->vector_abi == POWERPC_VEC_ALTIVEC)
888 /* Altivec places the return value in "v2". */
889 regcache->cooked_read (tdep->ppc_vr0_regnum + 2, readbuf);
893 /* Altivec places the return value in "v2". */
894 regcache->cooked_write (tdep->ppc_vr0_regnum + 2, writebuf);
896 return RETURN_VALUE_REGISTER_CONVENTION;
898 if (TYPE_LENGTH (type) == 16
899 && TYPE_CODE (type) == TYPE_CODE_ARRAY
900 && TYPE_VECTOR (type)
901 && tdep->vector_abi == POWERPC_VEC_GENERIC)
903 /* GCC -maltivec -mabi=no-altivec returns vectors in r3/r4/r5/r6.
904 GCC without AltiVec returns them in memory, but it warns about
905 ABI risks in that case; we don't try to support it. */
908 regcache->cooked_read (tdep->ppc_gp0_regnum + 3, readbuf + 0);
909 regcache->cooked_read (tdep->ppc_gp0_regnum + 4, readbuf + 4);
910 regcache->cooked_read (tdep->ppc_gp0_regnum + 5, readbuf + 8);
911 regcache->cooked_read (tdep->ppc_gp0_regnum + 6, readbuf + 12);
915 regcache->cooked_write (tdep->ppc_gp0_regnum + 3, writebuf + 0);
916 regcache->cooked_write (tdep->ppc_gp0_regnum + 4, writebuf + 4);
917 regcache->cooked_write (tdep->ppc_gp0_regnum + 5, writebuf + 8);
918 regcache->cooked_write (tdep->ppc_gp0_regnum + 6, writebuf + 12);
920 return RETURN_VALUE_REGISTER_CONVENTION;
922 if (TYPE_LENGTH (type) == 8
923 && TYPE_CODE (type) == TYPE_CODE_ARRAY
924 && TYPE_VECTOR (type)
925 && tdep->vector_abi == POWERPC_VEC_SPE)
927 /* The e500 ABI places return values for the 64-bit DSP types
928 (__ev64_opaque__) in r3. However, in GDB-speak, ev3
929 corresponds to the entire r3 value for e500, whereas GDB's r3
930 only corresponds to the least significant 32-bits. So place
931 the 64-bit DSP type's value in ev3. */
933 regcache->cooked_read (tdep->ppc_ev0_regnum + 3, readbuf);
935 regcache->cooked_write (tdep->ppc_ev0_regnum + 3, writebuf);
936 return RETURN_VALUE_REGISTER_CONVENTION;
938 if (broken_gcc && TYPE_LENGTH (type) <= 8)
940 /* GCC screwed up for structures or unions whose size is less
941 than or equal to 8 bytes.. Instead of left-aligning, it
942 right-aligns the data into the buffer formed by r3, r4. */
943 gdb_byte regvals[PPC_MAX_REGISTER_SIZE * 2];
944 int len = TYPE_LENGTH (type);
945 int offset = (2 * tdep->wordsize - len) % tdep->wordsize;
949 regcache->cooked_read (tdep->ppc_gp0_regnum + 3,
950 regvals + 0 * tdep->wordsize);
951 if (len > tdep->wordsize)
952 regcache->cooked_read (tdep->ppc_gp0_regnum + 4,
953 regvals + 1 * tdep->wordsize);
954 memcpy (readbuf, regvals + offset, len);
958 memset (regvals, 0, sizeof regvals);
959 memcpy (regvals + offset, writebuf, len);
960 regcache->cooked_write (tdep->ppc_gp0_regnum + 3,
961 regvals + 0 * tdep->wordsize);
962 if (len > tdep->wordsize)
963 regcache->cooked_write (tdep->ppc_gp0_regnum + 4,
964 regvals + 1 * tdep->wordsize);
967 return RETURN_VALUE_REGISTER_CONVENTION;
969 if (TYPE_LENGTH (type) <= 8)
973 /* This matches SVr4 PPC, it does not match GCC. */
974 /* The value is right-padded to 8 bytes and then loaded, as
975 two "words", into r3/r4. */
976 gdb_byte regvals[PPC_MAX_REGISTER_SIZE * 2];
977 regcache->cooked_read (tdep->ppc_gp0_regnum + 3,
978 regvals + 0 * tdep->wordsize);
979 if (TYPE_LENGTH (type) > tdep->wordsize)
980 regcache->cooked_read (tdep->ppc_gp0_regnum + 4,
981 regvals + 1 * tdep->wordsize);
982 memcpy (readbuf, regvals, TYPE_LENGTH (type));
986 /* This matches SVr4 PPC, it does not match GCC. */
987 /* The value is padded out to 8 bytes and then loaded, as
988 two "words" into r3/r4. */
989 gdb_byte regvals[PPC_MAX_REGISTER_SIZE * 2];
990 memset (regvals, 0, sizeof regvals);
991 memcpy (regvals, writebuf, TYPE_LENGTH (type));
992 regcache->cooked_write (tdep->ppc_gp0_regnum + 3,
993 regvals + 0 * tdep->wordsize);
994 if (TYPE_LENGTH (type) > tdep->wordsize)
995 regcache->cooked_write (tdep->ppc_gp0_regnum + 4,
996 regvals + 1 * tdep->wordsize);
998 return RETURN_VALUE_REGISTER_CONVENTION;
1000 return RETURN_VALUE_STRUCT_CONVENTION;
1003 enum return_value_convention
1004 ppc_sysv_abi_return_value (struct gdbarch *gdbarch, struct value *function,
1005 struct type *valtype, struct regcache *regcache,
1006 gdb_byte *readbuf, const gdb_byte *writebuf)
1008 return do_ppc_sysv_return_value (gdbarch,
1009 function ? value_type (function) : NULL,
1010 valtype, regcache, readbuf, writebuf, 0);
1013 enum return_value_convention
1014 ppc_sysv_abi_broken_return_value (struct gdbarch *gdbarch,
1015 struct value *function,
1016 struct type *valtype,
1017 struct regcache *regcache,
1018 gdb_byte *readbuf, const gdb_byte *writebuf)
1020 return do_ppc_sysv_return_value (gdbarch,
1021 function ? value_type (function) : NULL,
1022 valtype, regcache, readbuf, writebuf, 1);
1025 /* The helper function for 64-bit SYSV push_dummy_call. Converts the
1026 function's code address back into the function's descriptor
1029 Find a value for the TOC register. Every symbol should have both
1030 ".FN" and "FN" in the minimal symbol table. "FN" points at the
1031 FN's descriptor, while ".FN" points at the entry point (which
1032 matches FUNC_ADDR). Need to reverse from FUNC_ADDR back to the
1033 FN's descriptor address (while at the same time being careful to
1034 find "FN" in the same object file as ".FN"). */
1037 convert_code_addr_to_desc_addr (CORE_ADDR code_addr, CORE_ADDR *desc_addr)
1039 struct obj_section *dot_fn_section;
1040 struct bound_minimal_symbol dot_fn;
1041 struct bound_minimal_symbol fn;
1043 /* Find the minimal symbol that corresponds to CODE_ADDR (should
1044 have a name of the form ".FN"). */
1045 dot_fn = lookup_minimal_symbol_by_pc (code_addr);
1046 if (dot_fn.minsym == NULL || MSYMBOL_LINKAGE_NAME (dot_fn.minsym)[0] != '.')
1048 /* Get the section that contains CODE_ADDR. Need this for the
1049 "objfile" that it contains. */
1050 dot_fn_section = find_pc_section (code_addr);
1051 if (dot_fn_section == NULL || dot_fn_section->objfile == NULL)
1053 /* Now find the corresponding "FN" (dropping ".") minimal symbol's
1054 address. Only look for the minimal symbol in ".FN"'s object file
1055 - avoids problems when two object files (i.e., shared libraries)
1056 contain a minimal symbol with the same name. */
1057 fn = lookup_minimal_symbol (MSYMBOL_LINKAGE_NAME (dot_fn.minsym) + 1, NULL,
1058 dot_fn_section->objfile);
1059 if (fn.minsym == NULL)
1061 /* Found a descriptor. */
1062 (*desc_addr) = BMSYMBOL_VALUE_ADDRESS (fn);
1066 /* Walk down the type tree of TYPE counting consecutive base elements.
1067 If *FIELD_TYPE is NULL, then set it to the first valid floating point
1068 or vector type. If a non-floating point or vector type is found, or
1069 if a floating point or vector type that doesn't match a non-NULL
1070 *FIELD_TYPE is found, then return -1, otherwise return the count in the
1074 ppc64_aggregate_candidate (struct type *type,
1075 struct type **field_type)
1077 type = check_typedef (type);
1079 switch (TYPE_CODE (type))
1082 case TYPE_CODE_DECFLOAT:
1085 if (TYPE_CODE (*field_type) == TYPE_CODE (type)
1086 && TYPE_LENGTH (*field_type) == TYPE_LENGTH (type))
1090 case TYPE_CODE_COMPLEX:
1091 type = TYPE_TARGET_TYPE (type);
1092 if (TYPE_CODE (type) == TYPE_CODE_FLT
1093 || TYPE_CODE (type) == TYPE_CODE_DECFLOAT)
1097 if (TYPE_CODE (*field_type) == TYPE_CODE (type)
1098 && TYPE_LENGTH (*field_type) == TYPE_LENGTH (type))
1103 case TYPE_CODE_ARRAY:
1104 if (TYPE_VECTOR (type))
1108 if (TYPE_CODE (*field_type) == TYPE_CODE (type)
1109 && TYPE_LENGTH (*field_type) == TYPE_LENGTH (type))
1114 LONGEST count, low_bound, high_bound;
1116 count = ppc64_aggregate_candidate
1117 (TYPE_TARGET_TYPE (type), field_type);
1121 if (!get_array_bounds (type, &low_bound, &high_bound))
1123 count *= high_bound - low_bound;
1125 /* There must be no padding. */
1127 return TYPE_LENGTH (type) == 0 ? 0 : -1;
1128 else if (TYPE_LENGTH (type) != count * TYPE_LENGTH (*field_type))
1135 case TYPE_CODE_STRUCT:
1136 case TYPE_CODE_UNION:
1141 for (i = 0; i < TYPE_NFIELDS (type); i++)
1145 if (field_is_static (&TYPE_FIELD (type, i)))
1148 sub_count = ppc64_aggregate_candidate
1149 (TYPE_FIELD_TYPE (type, i), field_type);
1150 if (sub_count == -1)
1153 if (TYPE_CODE (type) == TYPE_CODE_STRUCT)
1156 count = std::max (count, sub_count);
1159 /* There must be no padding. */
1161 return TYPE_LENGTH (type) == 0 ? 0 : -1;
1162 else if (TYPE_LENGTH (type) != count * TYPE_LENGTH (*field_type))
1176 /* If an argument of type TYPE is a homogeneous float or vector aggregate
1177 that shall be passed in FP/vector registers according to the ELFv2 ABI,
1178 return the homogeneous element type in *ELT_TYPE and the number of
1179 elements in *N_ELTS, and return non-zero. Otherwise, return zero. */
1182 ppc64_elfv2_abi_homogeneous_aggregate (struct type *type,
1183 struct type **elt_type, int *n_elts)
1185 /* Complex types at the top level are treated separately. However,
1186 complex types can be elements of homogeneous aggregates. */
1187 if (TYPE_CODE (type) == TYPE_CODE_STRUCT
1188 || TYPE_CODE (type) == TYPE_CODE_UNION
1189 || (TYPE_CODE (type) == TYPE_CODE_ARRAY && !TYPE_VECTOR (type)))
1191 struct type *field_type = NULL;
1192 LONGEST field_count = ppc64_aggregate_candidate (type, &field_type);
1194 if (field_count > 0)
1196 int n_regs = ((TYPE_CODE (field_type) == TYPE_CODE_FLT
1197 || TYPE_CODE (field_type) == TYPE_CODE_DECFLOAT)?
1198 (TYPE_LENGTH (field_type) + 7) >> 3 : 1);
1200 /* The ELFv2 ABI allows homogeneous aggregates to occupy
1201 up to 8 registers. */
1202 if (field_count * n_regs <= 8)
1205 *elt_type = field_type;
1207 *n_elts = (int) field_count;
1208 /* Note that field_count is LONGEST since it may hold the size
1209 of an array, while *n_elts is int since its value is bounded
1210 by the number of registers used for argument passing. The
1211 cast cannot overflow due to the bounds checking above. */
1220 /* Structure holding the next argument position. */
1221 struct ppc64_sysv_argpos
1223 /* Register cache holding argument registers. If this is NULL,
1224 we only simulate argument processing without actually updating
1225 any registers or memory. */
1226 struct regcache *regcache;
1227 /* Next available general-purpose argument register. */
1229 /* Next available floating-point argument register. */
1231 /* Next available vector argument register. */
1233 /* The address, at which the next general purpose parameter
1234 (integer, struct, float, vector, ...) should be saved. */
1236 /* The address, at which the next by-reference parameter
1237 (non-Altivec vector, variably-sized type) should be saved. */
1241 /* VAL is a value of length LEN. Store it into the argument area on the
1242 stack and load it into the corresponding general-purpose registers
1243 required by the ABI, and update ARGPOS.
1245 If ALIGN is nonzero, it specifies the minimum alignment required
1246 for the on-stack copy of the argument. */
1249 ppc64_sysv_abi_push_val (struct gdbarch *gdbarch,
1250 const bfd_byte *val, int len, int align,
1251 struct ppc64_sysv_argpos *argpos)
1253 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1256 /* Enforce alignment of stack location, if requested. */
1257 if (align > tdep->wordsize)
1259 CORE_ADDR aligned_gparam = align_up (argpos->gparam, align);
1261 argpos->greg += (aligned_gparam - argpos->gparam) / tdep->wordsize;
1262 argpos->gparam = aligned_gparam;
1265 /* The ABI (version 1.9) specifies that values smaller than one
1266 doubleword are right-aligned and those larger are left-aligned.
1267 GCC versions before 3.4 implemented this incorrectly; see
1268 <http://gcc.gnu.org/gcc-3.4/powerpc-abi.html>. */
1269 if (len < tdep->wordsize
1270 && gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
1271 offset = tdep->wordsize - len;
1273 if (argpos->regcache)
1274 write_memory (argpos->gparam + offset, val, len);
1275 argpos->gparam = align_up (argpos->gparam + len, tdep->wordsize);
1277 while (len >= tdep->wordsize)
1279 if (argpos->regcache && argpos->greg <= 10)
1280 argpos->regcache->cooked_write (tdep->ppc_gp0_regnum + argpos->greg,
1283 len -= tdep->wordsize;
1284 val += tdep->wordsize;
1289 if (argpos->regcache && argpos->greg <= 10)
1290 argpos->regcache->cooked_write_part
1291 (tdep->ppc_gp0_regnum + argpos->greg, offset, len, val);
1296 /* The same as ppc64_sysv_abi_push_val, but using a single-word integer
1297 value VAL as argument. */
1300 ppc64_sysv_abi_push_integer (struct gdbarch *gdbarch, ULONGEST val,
1301 struct ppc64_sysv_argpos *argpos)
1303 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1304 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1305 gdb_byte buf[PPC_MAX_REGISTER_SIZE];
1307 if (argpos->regcache)
1308 store_unsigned_integer (buf, tdep->wordsize, byte_order, val);
1309 ppc64_sysv_abi_push_val (gdbarch, buf, tdep->wordsize, 0, argpos);
1312 /* VAL is a value of TYPE, a (binary or decimal) floating-point type.
1313 Load it into a floating-point register if required by the ABI,
1314 and update ARGPOS. */
1317 ppc64_sysv_abi_push_freg (struct gdbarch *gdbarch,
1318 struct type *type, const bfd_byte *val,
1319 struct ppc64_sysv_argpos *argpos)
1321 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1322 if (tdep->soft_float)
1325 if (TYPE_LENGTH (type) <= 8
1326 && TYPE_CODE (type) == TYPE_CODE_FLT)
1328 /* Floats and doubles go in f1 .. f13. 32-bit floats are converted
1330 if (argpos->regcache && argpos->freg <= 13)
1332 int regnum = tdep->ppc_fp0_regnum + argpos->freg;
1333 struct type *regtype = register_type (gdbarch, regnum);
1334 gdb_byte regval[PPC_MAX_REGISTER_SIZE];
1336 target_float_convert (val, type, regval, regtype);
1337 argpos->regcache->cooked_write (regnum, regval);
1342 else if (TYPE_LENGTH (type) <= 8
1343 && TYPE_CODE (type) == TYPE_CODE_DECFLOAT)
1345 /* Floats and doubles go in f1 .. f13. 32-bit decimal floats are
1346 placed in the least significant word. */
1347 if (argpos->regcache && argpos->freg <= 13)
1349 int regnum = tdep->ppc_fp0_regnum + argpos->freg;
1352 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
1353 offset = 8 - TYPE_LENGTH (type);
1355 argpos->regcache->cooked_write_part (regnum, offset,
1356 TYPE_LENGTH (type), val);
1361 else if (TYPE_LENGTH (type) == 16
1362 && TYPE_CODE (type) == TYPE_CODE_FLT
1363 && (gdbarch_long_double_format (gdbarch)
1364 == floatformats_ibm_long_double))
1366 /* IBM long double stored in two consecutive FPRs. */
1367 if (argpos->regcache && argpos->freg <= 13)
1369 int regnum = tdep->ppc_fp0_regnum + argpos->freg;
1371 argpos->regcache->cooked_write (regnum, val);
1372 if (argpos->freg <= 12)
1373 argpos->regcache->cooked_write (regnum + 1, val + 8);
1378 else if (TYPE_LENGTH (type) == 16
1379 && TYPE_CODE (type) == TYPE_CODE_DECFLOAT)
1381 /* 128-bit decimal floating-point values are stored in and even/odd
1382 pair of FPRs, with the even FPR holding the most significant half. */
1383 argpos->freg += argpos->freg & 1;
1385 if (argpos->regcache && argpos->freg <= 12)
1387 int regnum = tdep->ppc_fp0_regnum + argpos->freg;
1388 int lopart = gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG ? 8 : 0;
1389 int hipart = gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG ? 0 : 8;
1391 argpos->regcache->cooked_write (regnum, val + hipart);
1392 argpos->regcache->cooked_write (regnum + 1, val + lopart);
1399 /* VAL is a value of AltiVec vector type. Load it into a vector register
1400 if required by the ABI, and update ARGPOS. */
1403 ppc64_sysv_abi_push_vreg (struct gdbarch *gdbarch, const bfd_byte *val,
1404 struct ppc64_sysv_argpos *argpos)
1406 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1408 if (argpos->regcache && argpos->vreg <= 13)
1409 argpos->regcache->cooked_write (tdep->ppc_vr0_regnum + argpos->vreg, val);
1414 /* VAL is a value of TYPE. Load it into memory and/or registers
1415 as required by the ABI, and update ARGPOS. */
1418 ppc64_sysv_abi_push_param (struct gdbarch *gdbarch,
1419 struct type *type, const bfd_byte *val,
1420 struct ppc64_sysv_argpos *argpos)
1422 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1424 if (TYPE_CODE (type) == TYPE_CODE_FLT
1425 || TYPE_CODE (type) == TYPE_CODE_DECFLOAT)
1427 /* Floating-point scalars are passed in floating-point registers. */
1428 ppc64_sysv_abi_push_val (gdbarch, val, TYPE_LENGTH (type), 0, argpos);
1429 ppc64_sysv_abi_push_freg (gdbarch, type, val, argpos);
1431 else if (TYPE_CODE (type) == TYPE_CODE_ARRAY && TYPE_VECTOR (type)
1432 && tdep->vector_abi == POWERPC_VEC_ALTIVEC
1433 && TYPE_LENGTH (type) == 16)
1435 /* AltiVec vectors are passed aligned, and in vector registers. */
1436 ppc64_sysv_abi_push_val (gdbarch, val, TYPE_LENGTH (type), 16, argpos);
1437 ppc64_sysv_abi_push_vreg (gdbarch, val, argpos);
1439 else if (TYPE_CODE (type) == TYPE_CODE_ARRAY && TYPE_VECTOR (type)
1440 && TYPE_LENGTH (type) >= 16)
1442 /* Non-Altivec vectors are passed by reference. */
1444 /* Copy value onto the stack ... */
1445 CORE_ADDR addr = align_up (argpos->refparam, 16);
1446 if (argpos->regcache)
1447 write_memory (addr, val, TYPE_LENGTH (type));
1448 argpos->refparam = align_up (addr + TYPE_LENGTH (type), tdep->wordsize);
1450 /* ... and pass a pointer to the copy as parameter. */
1451 ppc64_sysv_abi_push_integer (gdbarch, addr, argpos);
1453 else if ((TYPE_CODE (type) == TYPE_CODE_INT
1454 || TYPE_CODE (type) == TYPE_CODE_ENUM
1455 || TYPE_CODE (type) == TYPE_CODE_BOOL
1456 || TYPE_CODE (type) == TYPE_CODE_CHAR
1457 || TYPE_CODE (type) == TYPE_CODE_PTR
1458 || TYPE_IS_REFERENCE (type))
1459 && TYPE_LENGTH (type) <= tdep->wordsize)
1463 if (argpos->regcache)
1465 /* Sign extend the value, then store it unsigned. */
1466 word = unpack_long (type, val);
1468 /* Convert any function code addresses into descriptors. */
1469 if (tdep->elf_abi == POWERPC_ELF_V1
1470 && (TYPE_CODE (type) == TYPE_CODE_PTR
1471 || TYPE_CODE (type) == TYPE_CODE_REF))
1473 struct type *target_type
1474 = check_typedef (TYPE_TARGET_TYPE (type));
1476 if (TYPE_CODE (target_type) == TYPE_CODE_FUNC
1477 || TYPE_CODE (target_type) == TYPE_CODE_METHOD)
1479 CORE_ADDR desc = word;
1481 convert_code_addr_to_desc_addr (word, &desc);
1487 ppc64_sysv_abi_push_integer (gdbarch, word, argpos);
1491 ppc64_sysv_abi_push_val (gdbarch, val, TYPE_LENGTH (type), 0, argpos);
1493 /* The ABI (version 1.9) specifies that structs containing a
1494 single floating-point value, at any level of nesting of
1495 single-member structs, are passed in floating-point registers. */
1496 if (TYPE_CODE (type) == TYPE_CODE_STRUCT
1497 && TYPE_NFIELDS (type) == 1)
1499 while (TYPE_CODE (type) == TYPE_CODE_STRUCT
1500 && TYPE_NFIELDS (type) == 1)
1501 type = check_typedef (TYPE_FIELD_TYPE (type, 0));
1503 if (TYPE_CODE (type) == TYPE_CODE_FLT)
1504 ppc64_sysv_abi_push_freg (gdbarch, type, val, argpos);
1507 /* In the ELFv2 ABI, homogeneous floating-point or vector
1508 aggregates are passed in a series of registers. */
1509 if (tdep->elf_abi == POWERPC_ELF_V2)
1511 struct type *eltype;
1514 if (ppc64_elfv2_abi_homogeneous_aggregate (type, &eltype, &nelt))
1515 for (i = 0; i < nelt; i++)
1517 const gdb_byte *elval = val + i * TYPE_LENGTH (eltype);
1519 if (TYPE_CODE (eltype) == TYPE_CODE_FLT
1520 || TYPE_CODE (eltype) == TYPE_CODE_DECFLOAT)
1521 ppc64_sysv_abi_push_freg (gdbarch, eltype, elval, argpos);
1522 else if (TYPE_CODE (eltype) == TYPE_CODE_ARRAY
1523 && TYPE_VECTOR (eltype)
1524 && tdep->vector_abi == POWERPC_VEC_ALTIVEC
1525 && TYPE_LENGTH (eltype) == 16)
1526 ppc64_sysv_abi_push_vreg (gdbarch, elval, argpos);
1532 /* Pass the arguments in either registers, or in the stack. Using the
1533 ppc 64 bit SysV ABI.
1535 This implements a dumbed down version of the ABI. It always writes
1536 values to memory, GPR and FPR, even when not necessary. Doing this
1537 greatly simplifies the logic. */
1540 ppc64_sysv_abi_push_dummy_call (struct gdbarch *gdbarch,
1541 struct value *function,
1542 struct regcache *regcache, CORE_ADDR bp_addr,
1543 int nargs, struct value **args, CORE_ADDR sp,
1544 function_call_return_method return_method,
1545 CORE_ADDR struct_addr)
1547 CORE_ADDR func_addr = find_function_addr (function, NULL);
1548 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1549 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1550 int opencl_abi = ppc_sysv_use_opencl_abi (value_type (function));
1551 ULONGEST back_chain;
1552 /* See for-loop comment below. */
1554 /* Size of the by-reference parameter copy region, the final value is
1555 computed in the for-loop below. */
1556 LONGEST refparam_size = 0;
1557 /* Size of the general parameter region, the final value is computed
1558 in the for-loop below. */
1559 LONGEST gparam_size = 0;
1560 /* Kevin writes ... I don't mind seeing tdep->wordsize used in the
1561 calls to align_up(), align_down(), etc. because this makes it
1562 easier to reuse this code (in a copy/paste sense) in the future,
1563 but it is a 64-bit ABI and asserting that the wordsize is 8 bytes
1564 at some point makes it easier to verify that this function is
1565 correct without having to do a non-local analysis to figure out
1566 the possible values of tdep->wordsize. */
1567 gdb_assert (tdep->wordsize == 8);
1569 /* This function exists to support a calling convention that
1570 requires floating-point registers. It shouldn't be used on
1571 processors that lack them. */
1572 gdb_assert (ppc_floating_point_unit_p (gdbarch));
1574 /* By this stage in the proceedings, SP has been decremented by "red
1575 zone size" + "struct return size". Fetch the stack-pointer from
1576 before this and use that as the BACK_CHAIN. */
1577 regcache_cooked_read_unsigned (regcache, gdbarch_sp_regnum (gdbarch),
1580 /* Go through the argument list twice.
1582 Pass 1: Compute the function call's stack space and register
1585 Pass 2: Replay the same computation but this time also write the
1586 values out to the target. */
1588 for (write_pass = 0; write_pass < 2; write_pass++)
1592 struct ppc64_sysv_argpos argpos;
1599 /* During the first pass, GPARAM and REFPARAM are more like
1600 offsets (start address zero) than addresses. That way
1601 they accumulate the total stack space each region
1603 argpos.regcache = NULL;
1605 argpos.refparam = 0;
1609 /* Decrement the stack pointer making space for the Altivec
1610 and general on-stack parameters. Set refparam and gparam
1611 to their corresponding regions. */
1612 argpos.regcache = regcache;
1613 argpos.refparam = align_down (sp - refparam_size, 16);
1614 argpos.gparam = align_down (argpos.refparam - gparam_size, 16);
1615 /* Add in space for the TOC, link editor double word (v1 only),
1616 compiler double word (v1 only), LR save area, CR save area,
1618 if (tdep->elf_abi == POWERPC_ELF_V1)
1619 sp = align_down (argpos.gparam - 48, 16);
1621 sp = align_down (argpos.gparam - 32, 16);
1624 /* If the function is returning a `struct', then there is an
1625 extra hidden parameter (which will be passed in r3)
1626 containing the address of that struct.. In that case we
1627 should advance one word and start from r4 register to copy
1628 parameters. This also consumes one on-stack parameter slot. */
1629 if (return_method == return_method_struct)
1630 ppc64_sysv_abi_push_integer (gdbarch, struct_addr, &argpos);
1632 for (argno = 0; argno < nargs; argno++)
1634 struct value *arg = args[argno];
1635 struct type *type = check_typedef (value_type (arg));
1636 const bfd_byte *val = value_contents (arg);
1638 if (TYPE_CODE (type) == TYPE_CODE_COMPLEX)
1640 /* Complex types are passed as if two independent scalars. */
1641 struct type *eltype = check_typedef (TYPE_TARGET_TYPE (type));
1643 ppc64_sysv_abi_push_param (gdbarch, eltype, val, &argpos);
1644 ppc64_sysv_abi_push_param (gdbarch, eltype,
1645 val + TYPE_LENGTH (eltype), &argpos);
1647 else if (TYPE_CODE (type) == TYPE_CODE_ARRAY && TYPE_VECTOR (type)
1650 /* OpenCL vectors shorter than 16 bytes are passed as if
1651 a series of independent scalars; OpenCL vectors 16 bytes
1652 or longer are passed as if a series of AltiVec vectors. */
1653 struct type *eltype;
1656 if (TYPE_LENGTH (type) < 16)
1657 eltype = check_typedef (TYPE_TARGET_TYPE (type));
1659 eltype = register_type (gdbarch, tdep->ppc_vr0_regnum);
1661 nelt = TYPE_LENGTH (type) / TYPE_LENGTH (eltype);
1662 for (i = 0; i < nelt; i++)
1664 const gdb_byte *elval = val + i * TYPE_LENGTH (eltype);
1666 ppc64_sysv_abi_push_param (gdbarch, eltype, elval, &argpos);
1671 /* All other types are passed as single arguments. */
1672 ppc64_sysv_abi_push_param (gdbarch, type, val, &argpos);
1678 /* Save the true region sizes ready for the second pass. */
1679 refparam_size = argpos.refparam;
1680 /* Make certain that the general parameter save area is at
1681 least the minimum 8 registers (or doublewords) in size. */
1682 if (argpos.greg < 8)
1683 gparam_size = 8 * tdep->wordsize;
1685 gparam_size = argpos.gparam;
1690 regcache_cooked_write_signed (regcache, gdbarch_sp_regnum (gdbarch), sp);
1692 /* Write the backchain (it occupies WORDSIZED bytes). */
1693 write_memory_signed_integer (sp, tdep->wordsize, byte_order, back_chain);
1695 /* Point the inferior function call's return address at the dummy's
1697 regcache_cooked_write_signed (regcache, tdep->ppc_lr_regnum, bp_addr);
1699 /* In the ELFv1 ABI, use the func_addr to find the descriptor, and use
1700 that to find the TOC. If we're calling via a function pointer,
1701 the pointer itself identifies the descriptor. */
1702 if (tdep->elf_abi == POWERPC_ELF_V1)
1704 struct type *ftype = check_typedef (value_type (function));
1705 CORE_ADDR desc_addr = value_as_address (function);
1707 if (TYPE_CODE (ftype) == TYPE_CODE_PTR
1708 || convert_code_addr_to_desc_addr (func_addr, &desc_addr))
1710 /* The TOC is the second double word in the descriptor. */
1712 read_memory_unsigned_integer (desc_addr + tdep->wordsize,
1713 tdep->wordsize, byte_order);
1715 regcache_cooked_write_unsigned (regcache,
1716 tdep->ppc_gp0_regnum + 2, toc);
1720 /* In the ELFv2 ABI, we need to pass the target address in r12 since
1721 we may be calling a global entry point. */
1722 if (tdep->elf_abi == POWERPC_ELF_V2)
1723 regcache_cooked_write_unsigned (regcache,
1724 tdep->ppc_gp0_regnum + 12, func_addr);
1729 /* Subroutine of ppc64_sysv_abi_return_value that handles "base" types:
1730 integer, floating-point, and AltiVec vector types.
1732 This routine also handles components of aggregate return types;
1733 INDEX describes which part of the aggregate is to be handled.
1735 Returns true if VALTYPE is some such base type that could be handled,
1738 ppc64_sysv_abi_return_value_base (struct gdbarch *gdbarch, struct type *valtype,
1739 struct regcache *regcache, gdb_byte *readbuf,
1740 const gdb_byte *writebuf, int index)
1742 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1744 /* Integers live in GPRs starting at r3. */
1745 if ((TYPE_CODE (valtype) == TYPE_CODE_INT
1746 || TYPE_CODE (valtype) == TYPE_CODE_ENUM
1747 || TYPE_CODE (valtype) == TYPE_CODE_CHAR
1748 || TYPE_CODE (valtype) == TYPE_CODE_BOOL)
1749 && TYPE_LENGTH (valtype) <= 8)
1751 int regnum = tdep->ppc_gp0_regnum + 3 + index;
1753 if (writebuf != NULL)
1755 /* Be careful to sign extend the value. */
1756 regcache_cooked_write_unsigned (regcache, regnum,
1757 unpack_long (valtype, writebuf));
1759 if (readbuf != NULL)
1761 /* Extract the integer from GPR. Since this is truncating the
1762 value, there isn't a sign extension problem. */
1765 regcache_cooked_read_unsigned (regcache, regnum, ®val);
1766 store_unsigned_integer (readbuf, TYPE_LENGTH (valtype),
1767 gdbarch_byte_order (gdbarch), regval);
1772 /* Floats and doubles go in f1 .. f13. 32-bit floats are converted
1774 if (TYPE_LENGTH (valtype) <= 8
1775 && TYPE_CODE (valtype) == TYPE_CODE_FLT)
1777 int regnum = tdep->ppc_fp0_regnum + 1 + index;
1778 struct type *regtype = register_type (gdbarch, regnum);
1779 gdb_byte regval[PPC_MAX_REGISTER_SIZE];
1781 if (writebuf != NULL)
1783 target_float_convert (writebuf, valtype, regval, regtype);
1784 regcache->cooked_write (regnum, regval);
1786 if (readbuf != NULL)
1788 regcache->cooked_read (regnum, regval);
1789 target_float_convert (regval, regtype, readbuf, valtype);
1794 /* Floats and doubles go in f1 .. f13. 32-bit decimal floats are
1795 placed in the least significant word. */
1796 if (TYPE_LENGTH (valtype) <= 8
1797 && TYPE_CODE (valtype) == TYPE_CODE_DECFLOAT)
1799 int regnum = tdep->ppc_fp0_regnum + 1 + index;
1802 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
1803 offset = 8 - TYPE_LENGTH (valtype);
1805 if (writebuf != NULL)
1806 regcache->cooked_write_part (regnum, offset, TYPE_LENGTH (valtype),
1808 if (readbuf != NULL)
1809 regcache->cooked_read_part (regnum, offset, TYPE_LENGTH (valtype),
1814 /* IBM long double stored in two consecutive FPRs. */
1815 if (TYPE_LENGTH (valtype) == 16
1816 && TYPE_CODE (valtype) == TYPE_CODE_FLT
1817 && (gdbarch_long_double_format (gdbarch)
1818 == floatformats_ibm_long_double))
1820 int regnum = tdep->ppc_fp0_regnum + 1 + 2 * index;
1822 if (writebuf != NULL)
1824 regcache->cooked_write (regnum, writebuf);
1825 regcache->cooked_write (regnum + 1, writebuf + 8);
1827 if (readbuf != NULL)
1829 regcache->cooked_read (regnum, readbuf);
1830 regcache->cooked_read (regnum + 1, readbuf + 8);
1835 /* 128-bit decimal floating-point values are stored in an even/odd
1836 pair of FPRs, with the even FPR holding the most significant half. */
1837 if (TYPE_LENGTH (valtype) == 16
1838 && TYPE_CODE (valtype) == TYPE_CODE_DECFLOAT)
1840 int regnum = tdep->ppc_fp0_regnum + 2 + 2 * index;
1841 int lopart = gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG ? 8 : 0;
1842 int hipart = gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG ? 0 : 8;
1844 if (writebuf != NULL)
1846 regcache->cooked_write (regnum, writebuf + hipart);
1847 regcache->cooked_write (regnum + 1, writebuf + lopart);
1849 if (readbuf != NULL)
1851 regcache->cooked_read (regnum, readbuf + hipart);
1852 regcache->cooked_read (regnum + 1, readbuf + lopart);
1857 /* AltiVec vectors are returned in VRs starting at v2. */
1858 if (TYPE_LENGTH (valtype) == 16
1859 && TYPE_CODE (valtype) == TYPE_CODE_ARRAY && TYPE_VECTOR (valtype)
1860 && tdep->vector_abi == POWERPC_VEC_ALTIVEC)
1862 int regnum = tdep->ppc_vr0_regnum + 2 + index;
1864 if (writebuf != NULL)
1865 regcache->cooked_write (regnum, writebuf);
1866 if (readbuf != NULL)
1867 regcache->cooked_read (regnum, readbuf);
1871 /* Short vectors are returned in GPRs starting at r3. */
1872 if (TYPE_LENGTH (valtype) <= 8
1873 && TYPE_CODE (valtype) == TYPE_CODE_ARRAY && TYPE_VECTOR (valtype))
1875 int regnum = tdep->ppc_gp0_regnum + 3 + index;
1878 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
1879 offset = 8 - TYPE_LENGTH (valtype);
1881 if (writebuf != NULL)
1882 regcache->cooked_write_part (regnum, offset, TYPE_LENGTH (valtype),
1884 if (readbuf != NULL)
1885 regcache->cooked_read_part (regnum, offset, TYPE_LENGTH (valtype),
1893 /* The 64 bit ABI return value convention.
1895 Return non-zero if the return-value is stored in a register, return
1896 0 if the return-value is instead stored on the stack (a.k.a.,
1897 struct return convention).
1899 For a return-value stored in a register: when WRITEBUF is non-NULL,
1900 copy the buffer to the corresponding register return-value location
1901 location; when READBUF is non-NULL, fill the buffer from the
1902 corresponding register return-value location. */
1903 enum return_value_convention
1904 ppc64_sysv_abi_return_value (struct gdbarch *gdbarch, struct value *function,
1905 struct type *valtype, struct regcache *regcache,
1906 gdb_byte *readbuf, const gdb_byte *writebuf)
1908 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1909 struct type *func_type = function ? value_type (function) : NULL;
1910 int opencl_abi = func_type? ppc_sysv_use_opencl_abi (func_type) : 0;
1911 struct type *eltype;
1914 /* This function exists to support a calling convention that
1915 requires floating-point registers. It shouldn't be used on
1916 processors that lack them. */
1917 gdb_assert (ppc_floating_point_unit_p (gdbarch));
1919 /* Complex types are returned as if two independent scalars. */
1920 if (TYPE_CODE (valtype) == TYPE_CODE_COMPLEX)
1922 eltype = check_typedef (TYPE_TARGET_TYPE (valtype));
1924 for (int i = 0; i < 2; i++)
1926 ok = ppc64_sysv_abi_return_value_base (gdbarch, eltype, regcache,
1927 readbuf, writebuf, i);
1931 readbuf += TYPE_LENGTH (eltype);
1933 writebuf += TYPE_LENGTH (eltype);
1935 return RETURN_VALUE_REGISTER_CONVENTION;
1938 /* OpenCL vectors shorter than 16 bytes are returned as if
1939 a series of independent scalars; OpenCL vectors 16 bytes
1940 or longer are returned as if a series of AltiVec vectors. */
1941 if (TYPE_CODE (valtype) == TYPE_CODE_ARRAY && TYPE_VECTOR (valtype)
1944 if (TYPE_LENGTH (valtype) < 16)
1945 eltype = check_typedef (TYPE_TARGET_TYPE (valtype));
1947 eltype = register_type (gdbarch, tdep->ppc_vr0_regnum);
1949 nelt = TYPE_LENGTH (valtype) / TYPE_LENGTH (eltype);
1950 for (int i = 0; i < nelt; i++)
1952 ok = ppc64_sysv_abi_return_value_base (gdbarch, eltype, regcache,
1953 readbuf, writebuf, i);
1957 readbuf += TYPE_LENGTH (eltype);
1959 writebuf += TYPE_LENGTH (eltype);
1961 return RETURN_VALUE_REGISTER_CONVENTION;
1964 /* All pointers live in r3. */
1965 if (TYPE_CODE (valtype) == TYPE_CODE_PTR || TYPE_IS_REFERENCE (valtype))
1967 int regnum = tdep->ppc_gp0_regnum + 3;
1969 if (writebuf != NULL)
1970 regcache->cooked_write (regnum, writebuf);
1971 if (readbuf != NULL)
1972 regcache->cooked_read (regnum, readbuf);
1973 return RETURN_VALUE_REGISTER_CONVENTION;
1976 /* Small character arrays are returned, right justified, in r3. */
1977 if (TYPE_CODE (valtype) == TYPE_CODE_ARRAY
1978 && !TYPE_VECTOR (valtype)
1979 && TYPE_LENGTH (valtype) <= 8
1980 && TYPE_CODE (TYPE_TARGET_TYPE (valtype)) == TYPE_CODE_INT
1981 && TYPE_LENGTH (TYPE_TARGET_TYPE (valtype)) == 1)
1983 int regnum = tdep->ppc_gp0_regnum + 3;
1984 int offset = (register_size (gdbarch, regnum) - TYPE_LENGTH (valtype));
1986 if (writebuf != NULL)
1987 regcache->cooked_write_part (regnum, offset, TYPE_LENGTH (valtype),
1989 if (readbuf != NULL)
1990 regcache->cooked_read_part (regnum, offset, TYPE_LENGTH (valtype),
1992 return RETURN_VALUE_REGISTER_CONVENTION;
1995 /* In the ELFv2 ABI, homogeneous floating-point or vector
1996 aggregates are returned in registers. */
1997 if (tdep->elf_abi == POWERPC_ELF_V2
1998 && ppc64_elfv2_abi_homogeneous_aggregate (valtype, &eltype, &nelt)
1999 && (TYPE_CODE (eltype) == TYPE_CODE_FLT
2000 || TYPE_CODE (eltype) == TYPE_CODE_DECFLOAT
2001 || (TYPE_CODE (eltype) == TYPE_CODE_ARRAY
2002 && TYPE_VECTOR (eltype)
2003 && tdep->vector_abi == POWERPC_VEC_ALTIVEC
2004 && TYPE_LENGTH (eltype) == 16)))
2006 for (int i = 0; i < nelt; i++)
2008 ok = ppc64_sysv_abi_return_value_base (gdbarch, eltype, regcache,
2009 readbuf, writebuf, i);
2013 readbuf += TYPE_LENGTH (eltype);
2015 writebuf += TYPE_LENGTH (eltype);
2018 return RETURN_VALUE_REGISTER_CONVENTION;
2021 /* In the ELFv2 ABI, aggregate types of up to 16 bytes are
2022 returned in registers r3:r4. */
2023 if (tdep->elf_abi == POWERPC_ELF_V2
2024 && TYPE_LENGTH (valtype) <= 16
2025 && (TYPE_CODE (valtype) == TYPE_CODE_STRUCT
2026 || TYPE_CODE (valtype) == TYPE_CODE_UNION
2027 || (TYPE_CODE (valtype) == TYPE_CODE_ARRAY
2028 && !TYPE_VECTOR (valtype))))
2030 int n_regs = ((TYPE_LENGTH (valtype) + tdep->wordsize - 1)
2033 for (int i = 0; i < n_regs; i++)
2035 gdb_byte regval[PPC_MAX_REGISTER_SIZE];
2036 int regnum = tdep->ppc_gp0_regnum + 3 + i;
2037 int offset = i * tdep->wordsize;
2038 int len = TYPE_LENGTH (valtype) - offset;
2040 if (len > tdep->wordsize)
2041 len = tdep->wordsize;
2043 if (writebuf != NULL)
2045 memset (regval, 0, sizeof regval);
2046 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG
2048 memcpy (regval + tdep->wordsize - len, writebuf, len);
2050 memcpy (regval, writebuf + offset, len);
2051 regcache->cooked_write (regnum, regval);
2053 if (readbuf != NULL)
2055 regcache->cooked_read (regnum, regval);
2056 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG
2058 memcpy (readbuf, regval + tdep->wordsize - len, len);
2060 memcpy (readbuf + offset, regval, len);
2063 return RETURN_VALUE_REGISTER_CONVENTION;
2066 /* Handle plain base types. */
2067 if (ppc64_sysv_abi_return_value_base (gdbarch, valtype, regcache,
2068 readbuf, writebuf, 0))
2069 return RETURN_VALUE_REGISTER_CONVENTION;
2071 return RETURN_VALUE_STRUCT_CONVENTION;