1 /* Target-dependent code for PowerPC systems using the SVR4 ABI
2 for GDB, the GNU debugger.
4 Copyright 2000, 2001, 2002 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 2 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, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
28 #include "gdb_string.h"
32 /* Ensure that X is aligned to an S byte boundary (assuming that S is
33 a power of 2) rounding up/down where necessary. */
36 align_up (ULONGEST x, int s)
38 return (x + s - 1) & -s;
42 align_down (ULONGEST x, int s)
47 /* Pass the arguments in either registers, or in the stack. Using the
48 ppc sysv ABI, the first eight words of the argument list (that might
49 be less than eight parameters if some parameters occupy more than one
50 word) are passed in r3..r10 registers. float and double parameters are
51 passed in fpr's, in addition to that. Rest of the parameters if any
52 are passed in user stack.
54 If the function is returning a structure, then the return address is passed
55 in r3, then the first 7 words of the parametes can be passed in registers,
59 ppc_sysv_abi_push_dummy_call (struct gdbarch *gdbarch, CORE_ADDR func_addr,
60 struct regcache *regcache, CORE_ADDR bp_addr,
61 int nargs, struct value **args, CORE_ADDR sp,
62 int struct_return, CORE_ADDR struct_addr)
64 struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch);
65 const CORE_ADDR saved_sp = read_sp ();
66 int argspace = 0; /* 0 is an initial wrong guess. */
69 /* Go through the argument list twice.
71 Pass 1: Figure out how much new stack space is required for
72 arguments and pushed values. Unlike the PowerOpen ABI, the SysV
73 ABI doesn't reserve any extra space for parameters which are put
74 in registers, but does always push structures and then pass their
77 Pass 2: Replay the same computation but this time also write the
78 values out to the target. */
80 for (write_pass = 0; write_pass < 2; write_pass++)
83 /* Next available floating point register for float and double
86 /* Next available general register for non-float, non-vector
89 /* Next available vector register for vector arguments. */
91 /* Arguments start above the "LR save word" and "Back chain". */
92 int argoffset = 2 * tdep->wordsize;
93 /* Structures start after the arguments. */
94 int structoffset = argoffset + argspace;
96 /* If the function is returning a `struct', then the first word
97 (which will be passed in r3) is used for struct return
98 address. In that case we should advance one word and start
99 from r4 register to copy parameters. */
103 regcache_cooked_write_signed (regcache,
104 tdep->ppc_gp0_regnum + greg,
109 for (argno = 0; argno < nargs; argno++)
111 struct value *arg = args[argno];
112 struct type *type = check_typedef (VALUE_TYPE (arg));
113 int len = TYPE_LENGTH (type);
114 char *val = VALUE_CONTENTS (arg);
116 if (TYPE_CODE (type) == TYPE_CODE_FLT
117 && ppc_floating_point_unit_p (current_gdbarch)
120 /* Floating point value converted to "double" then
121 passed in an FP register, when the registers run out,
122 8 byte aligned stack is used. */
127 /* Always store the floating point value using
128 the register's floating-point format. */
129 char regval[MAX_REGISTER_SIZE];
131 = register_type (gdbarch, FP0_REGNUM + freg);
132 convert_typed_floating (val, type, regval, regtype);
133 regcache_cooked_write (regcache, FP0_REGNUM + freg,
140 /* SysV ABI converts floats to doubles before
141 writing them to an 8 byte aligned stack location. */
142 argoffset = align_up (argoffset, 8);
146 struct type *memtype;
147 switch (TARGET_BYTE_ORDER)
150 memtype = builtin_type_ieee_double_big;
152 case BFD_ENDIAN_LITTLE:
153 memtype = builtin_type_ieee_double_little;
156 internal_error (__FILE__, __LINE__, "bad switch");
158 convert_typed_floating (val, type, memval, memtype);
159 write_memory (sp + argoffset, val, len);
165 && (TYPE_CODE (type) == TYPE_CODE_INT /* long long */
166 || (!ppc_floating_point_unit_p (current_gdbarch)
167 && TYPE_CODE (type) == TYPE_CODE_FLT))) /* double */
169 /* "long long" or "double" passed in an odd/even
170 register pair with the low addressed word in the odd
171 register and the high addressed word in the even
172 register, or when the registers run out an 8 byte
173 aligned stack location. */
176 /* Just in case GREG was 10. */
178 argoffset = align_up (argoffset, 8);
180 write_memory (sp + argoffset, val, len);
183 else if (tdep->wordsize == 8)
186 regcache_cooked_write (regcache,
187 tdep->ppc_gp0_regnum + greg,
193 /* Must start on an odd register - r3/r4 etc. */
198 regcache_cooked_write (regcache,
199 tdep->ppc_gp0_regnum + greg + 0,
201 regcache_cooked_write (regcache,
202 tdep->ppc_gp0_regnum + greg + 1,
209 && TYPE_CODE (type) == TYPE_CODE_ARRAY
210 && TYPE_VECTOR (type)
211 && tdep->ppc_vr0_regnum >= 0)
213 /* Vector parameter passed in an Altivec register, or
214 when that runs out, 16 byte aligned stack location. */
218 regcache_cooked_write (current_regcache,
219 tdep->ppc_vr0_regnum + vreg,
225 argoffset = align_up (argoffset, 16);
227 write_memory (sp + argoffset, val, 16);
232 && TYPE_CODE (type) == TYPE_CODE_ARRAY
233 && TYPE_VECTOR (type)
234 && tdep->ppc_ev0_regnum >= 0)
236 /* Vector parameter passed in an e500 register, or when
237 that runs out, 8 byte aligned stack location. Note
238 that since e500 vector and general purpose registers
239 both map onto the same underlying register set, a
240 "greg" and not a "vreg" is consumed here. A cooked
241 write stores the value in the correct locations
242 within the raw register cache. */
246 regcache_cooked_write (current_regcache,
247 tdep->ppc_ev0_regnum + greg,
253 argoffset = align_up (argoffset, 8);
255 write_memory (sp + argoffset, val, 8);
261 /* Reduce the parameter down to something that fits in a
263 char word[MAX_REGISTER_SIZE];
264 memset (word, 0, MAX_REGISTER_SIZE);
265 if (len > tdep->wordsize
266 || TYPE_CODE (type) == TYPE_CODE_STRUCT
267 || TYPE_CODE (type) == TYPE_CODE_UNION)
269 /* Structs and large values are put on an 8 byte
271 structoffset = align_up (structoffset, 8);
273 write_memory (sp + structoffset, val, len);
274 /* ... and then a "word" pointing to that address is
275 passed as the parameter. */
276 store_unsigned_integer (word, tdep->wordsize,
280 else if (TYPE_CODE (type) == TYPE_CODE_INT)
281 /* Sign or zero extend the "int" into a "word". */
282 store_unsigned_integer (word, tdep->wordsize,
283 unpack_long (type, val));
285 /* Always goes in the low address. */
286 memcpy (word, val, len);
287 /* Store that "word" in a register, or on the stack.
288 The words have "4" byte alignment. */
292 regcache_cooked_write (regcache,
293 tdep->ppc_gp0_regnum + greg,
299 argoffset = align_up (argoffset, tdep->wordsize);
301 write_memory (sp + argoffset, word, tdep->wordsize);
302 argoffset += tdep->wordsize;
307 /* Compute the actual stack space requirements. */
310 /* Remember the amount of space needed by the arguments. */
311 argspace = argoffset;
312 /* Allocate space for both the arguments and the structures. */
313 sp -= (argoffset + structoffset);
314 /* Ensure that the stack is still 16 byte aligned. */
315 sp = align_down (sp, 16);
320 regcache_cooked_write_signed (regcache, SP_REGNUM, sp);
322 /* Write the backchain (it occupies WORDSIZED bytes). */
323 write_memory_signed_integer (sp, tdep->wordsize, saved_sp);
325 /* Point the inferior function call's return address at the dummy's
327 regcache_cooked_write_signed (regcache, tdep->ppc_lr_regnum, bp_addr);
332 /* Until November 2001, gcc was not complying to the SYSV ABI for
333 returning structures less than or equal to 8 bytes in size. It was
334 returning everything in memory. When this was corrected, it wasn't
335 fixed for native platforms. */
337 ppc_sysv_abi_broken_use_struct_convention (int gcc_p, struct type *value_type)
339 if ((TYPE_LENGTH (value_type) == 16 || TYPE_LENGTH (value_type) == 8)
340 && TYPE_VECTOR (value_type))
343 return generic_use_struct_convention (gcc_p, value_type);
346 /* Structures 8 bytes or less long are returned in the r3 & r4
347 registers, according to the SYSV ABI. */
349 ppc_sysv_abi_use_struct_convention (int gcc_p, struct type *value_type)
351 if ((TYPE_LENGTH (value_type) == 16 || TYPE_LENGTH (value_type) == 8)
352 && TYPE_VECTOR (value_type))
355 return (TYPE_LENGTH (value_type) > 8);