2003-09-12 Andrew Cagney <cagney@redhat.com>
[deliverable/binutils-gdb.git] / gdb / ppc-sysv-tdep.c
1 /* Target-dependent code for PowerPC systems using the SVR4 ABI
2 for GDB, the GNU debugger.
3
4 Copyright 2000, 2001, 2002 Free Software Foundation, Inc.
5
6 This file is part of GDB.
7
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.
12
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.
17
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. */
22
23 #include "defs.h"
24 #include "gdbcore.h"
25 #include "inferior.h"
26 #include "regcache.h"
27 #include "value.h"
28 #include "gdb_string.h"
29
30 #include "ppc-tdep.h"
31
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. */
34
35 static ULONGEST
36 align_up (ULONGEST x, int s)
37 {
38 return (x + s - 1) & -s;
39 }
40
41 static ULONGEST
42 align_down (ULONGEST x, int s)
43 {
44 return (x & -s);
45 }
46
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.
53
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,
56 starting from r4. */
57
58 CORE_ADDR
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)
63 {
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. */
67 int write_pass;
68
69 /* Go through the argument list twice.
70
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
75 address.
76
77 Pass 2: Replay the same computation but this time also write the
78 values out to the target. */
79
80 for (write_pass = 0; write_pass < 2; write_pass++)
81 {
82 int argno;
83 /* Next available floating point register for float and double
84 arguments. */
85 int freg = 1;
86 /* Next available general register for non-float, non-vector
87 arguments. */
88 int greg = 3;
89 /* Next available vector register for vector arguments. */
90 int vreg = 2;
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;
95
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. */
100 if (struct_return)
101 {
102 if (write_pass)
103 regcache_cooked_write_signed (regcache,
104 tdep->ppc_gp0_regnum + greg,
105 struct_addr);
106 greg++;
107 }
108
109 for (argno = 0; argno < nargs; argno++)
110 {
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);
115
116 if (TYPE_CODE (type) == TYPE_CODE_FLT
117 && ppc_floating_point_unit_p (current_gdbarch)
118 && len <= 8)
119 {
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. */
123 if (freg <= 8)
124 {
125 if (write_pass)
126 {
127 /* Always store the floating point value using
128 the register's floating-point format. */
129 char regval[MAX_REGISTER_SIZE];
130 struct type *regtype
131 = register_type (gdbarch, FP0_REGNUM + freg);
132 convert_typed_floating (val, type, regval, regtype);
133 regcache_cooked_write (regcache, FP0_REGNUM + freg,
134 regval);
135 }
136 freg++;
137 }
138 else
139 {
140 /* SysV ABI converts floats to doubles before
141 writing them to an 8 byte aligned stack location. */
142 argoffset = align_up (argoffset, 8);
143 if (write_pass)
144 {
145 char memval[8];
146 struct type *memtype;
147 switch (TARGET_BYTE_ORDER)
148 {
149 case BFD_ENDIAN_BIG:
150 memtype = builtin_type_ieee_double_big;
151 break;
152 case BFD_ENDIAN_LITTLE:
153 memtype = builtin_type_ieee_double_little;
154 break;
155 default:
156 internal_error (__FILE__, __LINE__, "bad switch");
157 }
158 convert_typed_floating (val, type, memval, memtype);
159 write_memory (sp + argoffset, val, len);
160 }
161 argoffset += 8;
162 }
163 }
164 else if (len == 8
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 */
168 {
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. */
174 if (greg > 9)
175 {
176 /* Just in case GREG was 10. */
177 greg = 11;
178 argoffset = align_up (argoffset, 8);
179 if (write_pass)
180 write_memory (sp + argoffset, val, len);
181 argoffset += 8;
182 }
183 else if (tdep->wordsize == 8)
184 {
185 if (write_pass)
186 regcache_cooked_write (regcache,
187 tdep->ppc_gp0_regnum + greg,
188 val);
189 greg += 1;
190 }
191 else
192 {
193 /* Must start on an odd register - r3/r4 etc. */
194 if ((greg & 1) == 0)
195 greg++;
196 if (write_pass)
197 {
198 regcache_cooked_write (regcache,
199 tdep->ppc_gp0_regnum + greg + 0,
200 val + 0);
201 regcache_cooked_write (regcache,
202 tdep->ppc_gp0_regnum + greg + 1,
203 val + 4);
204 }
205 greg += 2;
206 }
207 }
208 else if (len == 16
209 && TYPE_CODE (type) == TYPE_CODE_ARRAY
210 && TYPE_VECTOR (type)
211 && tdep->ppc_vr0_regnum >= 0)
212 {
213 /* Vector parameter passed in an Altivec register, or
214 when that runs out, 16 byte aligned stack location. */
215 if (vreg <= 13)
216 {
217 if (write_pass)
218 regcache_cooked_write (current_regcache,
219 tdep->ppc_vr0_regnum + vreg,
220 val);
221 vreg++;
222 }
223 else
224 {
225 argoffset = align_up (argoffset, 16);
226 if (write_pass)
227 write_memory (sp + argoffset, val, 16);
228 argoffset += 16;
229 }
230 }
231 else if (len == 8
232 && TYPE_CODE (type) == TYPE_CODE_ARRAY
233 && TYPE_VECTOR (type)
234 && tdep->ppc_ev0_regnum >= 0)
235 {
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. */
243 if (greg <= 10)
244 {
245 if (write_pass)
246 regcache_cooked_write (current_regcache,
247 tdep->ppc_ev0_regnum + greg,
248 val);
249 greg++;
250 }
251 else
252 {
253 argoffset = align_up (argoffset, 8);
254 if (write_pass)
255 write_memory (sp + argoffset, val, 8);
256 argoffset += 8;
257 }
258 }
259 else
260 {
261 /* Reduce the parameter down to something that fits in a
262 "word". */
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)
268 {
269 /* Structs and large values are put on an 8 byte
270 aligned stack ... */
271 structoffset = align_up (structoffset, 8);
272 if (write_pass)
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,
277 sp + structoffset);
278 structoffset += len;
279 }
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));
284 else
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. */
289 if (greg <= 10)
290 {
291 if (write_pass)
292 regcache_cooked_write (regcache,
293 tdep->ppc_gp0_regnum + greg,
294 word);
295 greg++;
296 }
297 else
298 {
299 argoffset = align_up (argoffset, tdep->wordsize);
300 if (write_pass)
301 write_memory (sp + argoffset, word, tdep->wordsize);
302 argoffset += tdep->wordsize;
303 }
304 }
305 }
306
307 /* Compute the actual stack space requirements. */
308 if (!write_pass)
309 {
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);
316 }
317 }
318
319 /* Update %sp. */
320 regcache_cooked_write_signed (regcache, SP_REGNUM, sp);
321
322 /* Write the backchain (it occupies WORDSIZED bytes). */
323 write_memory_signed_integer (sp, tdep->wordsize, saved_sp);
324
325 /* Point the inferior function call's return address at the dummy's
326 breakpoint. */
327 regcache_cooked_write_signed (regcache, tdep->ppc_lr_regnum, bp_addr);
328
329 return sp;
330 }
331
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. */
336 int
337 ppc_sysv_abi_broken_use_struct_convention (int gcc_p, struct type *value_type)
338 {
339 if ((TYPE_LENGTH (value_type) == 16 || TYPE_LENGTH (value_type) == 8)
340 && TYPE_VECTOR (value_type))
341 return 0;
342
343 return generic_use_struct_convention (gcc_p, value_type);
344 }
345
346 /* Structures 8 bytes or less long are returned in the r3 & r4
347 registers, according to the SYSV ABI. */
348 int
349 ppc_sysv_abi_use_struct_convention (int gcc_p, struct type *value_type)
350 {
351 if ((TYPE_LENGTH (value_type) == 16 || TYPE_LENGTH (value_type) == 8)
352 && TYPE_VECTOR (value_type))
353 return 0;
354
355 return (TYPE_LENGTH (value_type) > 8);
356 }
This page took 0.037266 seconds and 5 git commands to generate.