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 /* round2 rounds x up to the nearest multiple of s assuming that s is a
36 #define round2(x,s) ((((long) (x) - 1) & ~(long)((s)-1)) + (s))
38 /* Pass the arguments in either registers, or in the stack. Using the
39 ppc sysv ABI, the first eight words of the argument list (that might
40 be less than eight parameters if some parameters occupy more than one
41 word) are passed in r3..r10 registers. float and double parameters are
42 passed in fpr's, in addition to that. Rest of the parameters if any
43 are passed in user stack.
45 If the function is returning a structure, then the return address is passed
46 in r3, then the first 7 words of the parametes can be passed in registers,
50 ppc_sysv_abi_push_dummy_call (struct gdbarch *gdbarch, CORE_ADDR func_addr,
51 struct regcache *regcache, CORE_ADDR bp_addr,
52 int nargs, struct value **args, CORE_ADDR sp,
53 int struct_return, CORE_ADDR struct_addr)
56 /* Next available general register for non-float, non-vector arguments. */
58 /* Next available floating point register for float arguments. */
60 /* Next available vector register for vector arguments. */
70 struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch);
72 greg = struct_return ? 4 : 3;
78 /* Figure out how much new stack space is required for arguments
79 which don't fit in registers. Unlike the PowerOpen ABI, the
80 SysV ABI doesn't reserve any extra space for parameters which
81 are put in registers. */
82 for (argno = 0; argno < nargs; argno++)
84 struct value *arg = args[argno];
85 type = check_typedef (VALUE_TYPE (arg));
86 len = TYPE_LENGTH (type);
88 if (TYPE_CODE (type) == TYPE_CODE_FLT
89 && ppc_floating_point_unit_p (current_gdbarch))
95 /* SysV ABI converts floats to doubles when placed in
96 memory and requires 8 byte alignment */
97 if (argstkspace & 0x4)
103 && (TYPE_CODE (type) == TYPE_CODE_INT /* long long */
104 || (!ppc_floating_point_unit_p (current_gdbarch)
105 && TYPE_CODE (type) == TYPE_CODE_FLT))) /* double */
110 if (argstkspace & 0x4)
121 else if (!TYPE_VECTOR (type))
124 || TYPE_CODE (type) == TYPE_CODE_STRUCT
125 || TYPE_CODE (type) == TYPE_CODE_UNION)
127 /* Rounding to the nearest multiple of 8 may not be necessary,
128 but it is safe. Particularly since we don't know the
129 field types of the structure */
130 structstkspace += round2 (len, 8);
140 && TYPE_CODE (type) == TYPE_CODE_ARRAY
141 && TYPE_VECTOR (type))
147 /* Vector arguments must be aligned to 16 bytes on
149 argstkspace += round2 (argstkspace, 16);
154 && TYPE_CODE (type) == TYPE_CODE_ARRAY
155 && TYPE_VECTOR (type))
161 /* Vector arguments must be aligned to 8 bytes on
163 argstkspace += round2 (argstkspace, 8);
170 /* Get current SP location */
171 saved_sp = read_sp ();
173 sp -= argstkspace + structstkspace;
175 /* Allocate space for backchain and callee's saved lr */
178 /* Make sure that we maintain 16 byte alignment */
181 /* Update %sp before proceeding any further */
182 write_register (SP_REGNUM, sp);
184 /* write the backchain */
185 store_unsigned_integer (old_sp_buf, 4, saved_sp);
186 write_memory (sp, old_sp_buf, 4);
189 structoffset = argoffset + argstkspace;
194 /* Fill in r3 with the return structure, if any */
197 write_register (tdep->ppc_gp0_regnum + greg, struct_addr);
201 /* Now fill in the registers and stack... */
202 for (argno = 0; argno < nargs; argno++)
204 struct value *arg = args[argno];
205 char *val = VALUE_CONTENTS (arg);
206 type = check_typedef (VALUE_TYPE (arg));
207 len = TYPE_LENGTH (type);
209 if (TYPE_CODE (type) == TYPE_CODE_FLT
210 && ppc_floating_point_unit_p (current_gdbarch))
217 "Fatal Error: a floating point parameter #%d with a size > 8 is found!\n", argno);
218 regval = extract_unsigned_integer (val, len);
219 write_register (FP0_REGNUM + freg, regval);
224 /* SysV ABI converts floats to doubles when placed in
225 memory and requires 8 byte alignment */
226 /* FIXME: Convert floats to doubles */
229 write_memory (sp + argoffset, val, len);
234 && (TYPE_CODE (type) == TYPE_CODE_INT /* long long */
235 || (!ppc_floating_point_unit_p (current_gdbarch)
236 && TYPE_CODE (type) == TYPE_CODE_FLT))) /* double */
243 write_memory (sp + argoffset, val, len);
251 regval = extract_unsigned_integer (val, 4);
252 write_register (tdep->ppc_gp0_regnum + greg, regval);
253 regval = extract_unsigned_integer (val + 4, 4);
254 write_register (tdep->ppc_gp0_regnum + greg + 1, regval);
258 else if (!TYPE_VECTOR (type))
262 || TYPE_CODE (type) == TYPE_CODE_STRUCT
263 || TYPE_CODE (type) == TYPE_CODE_UNION)
265 write_memory (sp + structoffset, val, len);
266 store_unsigned_integer (val_buf, 4, sp + structoffset);
267 structoffset += round2 (len, 8);
271 memset (val_buf, 0, 4);
272 memcpy (val_buf, val, len);
276 ULONGEST regval = extract_unsigned_integer (val_buf, 4);
277 write_register (tdep->ppc_gp0_regnum + greg, regval);
282 write_memory (sp + argoffset, val_buf, 4);
289 && TYPE_CODE (type) == TYPE_CODE_ARRAY
290 && TYPE_VECTOR (type))
292 char *v_val_buf = alloca (16);
293 memset (v_val_buf, 0, 16);
294 memcpy (v_val_buf, val, len);
297 regcache_cooked_write (current_regcache,
298 tdep->ppc_vr0_regnum + vreg,
304 write_memory (sp + argoffset, v_val_buf, 16);
309 && TYPE_CODE (type) == TYPE_CODE_ARRAY
310 && TYPE_VECTOR (type))
312 char *v_val_buf = alloca (8);
313 memset (v_val_buf, 0, 8);
314 memcpy (v_val_buf, val, len);
317 regcache_cooked_write (current_regcache,
318 tdep->ppc_ev0_regnum + greg,
324 write_memory (sp + argoffset, v_val_buf, 8);
331 target_store_registers (-1);
335 /* Until November 2001, gcc was not complying to the SYSV ABI for
336 returning structures less than or equal to 8 bytes in size. It was
337 returning everything in memory. When this was corrected, it wasn't
338 fixed for native platforms. */
340 ppc_sysv_abi_broken_use_struct_convention (int gcc_p, struct type *value_type)
342 if ((TYPE_LENGTH (value_type) == 16 || TYPE_LENGTH (value_type) == 8)
343 && TYPE_VECTOR (value_type))
346 return generic_use_struct_convention (gcc_p, value_type);
349 /* Structures 8 bytes or less long are returned in the r3 & r4
350 registers, according to the SYSV ABI. */
352 ppc_sysv_abi_use_struct_convention (int gcc_p, struct type *value_type)
354 if ((TYPE_LENGTH (value_type) == 16 || TYPE_LENGTH (value_type) == 8)
355 && TYPE_VECTOR (value_type))
358 return (TYPE_LENGTH (value_type) > 8);