2003-10-06 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 /* Pass the arguments in either registers, or in the stack. Using the
33 ppc sysv ABI, the first eight words of the argument list (that might
34 be less than eight parameters if some parameters occupy more than one
35 word) are passed in r3..r10 registers. float and double parameters are
36 passed in fpr's, in addition to that. Rest of the parameters if any
37 are passed in user stack.
38
39 If the function is returning a structure, then the return address is passed
40 in r3, then the first 7 words of the parametes can be passed in registers,
41 starting from r4. */
42
43 CORE_ADDR
44 ppc_sysv_abi_push_dummy_call (struct gdbarch *gdbarch, CORE_ADDR func_addr,
45 struct regcache *regcache, CORE_ADDR bp_addr,
46 int nargs, struct value **args, CORE_ADDR sp,
47 int struct_return, CORE_ADDR struct_addr)
48 {
49 struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch);
50 const CORE_ADDR saved_sp = read_sp ();
51 int argspace = 0; /* 0 is an initial wrong guess. */
52 int write_pass;
53
54 /* Go through the argument list twice.
55
56 Pass 1: Figure out how much new stack space is required for
57 arguments and pushed values. Unlike the PowerOpen ABI, the SysV
58 ABI doesn't reserve any extra space for parameters which are put
59 in registers, but does always push structures and then pass their
60 address.
61
62 Pass 2: Replay the same computation but this time also write the
63 values out to the target. */
64
65 for (write_pass = 0; write_pass < 2; write_pass++)
66 {
67 int argno;
68 /* Next available floating point register for float and double
69 arguments. */
70 int freg = 1;
71 /* Next available general register for non-float, non-vector
72 arguments. */
73 int greg = 3;
74 /* Next available vector register for vector arguments. */
75 int vreg = 2;
76 /* Arguments start above the "LR save word" and "Back chain". */
77 int argoffset = 2 * tdep->wordsize;
78 /* Structures start after the arguments. */
79 int structoffset = argoffset + argspace;
80
81 /* If the function is returning a `struct', then the first word
82 (which will be passed in r3) is used for struct return
83 address. In that case we should advance one word and start
84 from r4 register to copy parameters. */
85 if (struct_return)
86 {
87 if (write_pass)
88 regcache_cooked_write_signed (regcache,
89 tdep->ppc_gp0_regnum + greg,
90 struct_addr);
91 greg++;
92 }
93
94 for (argno = 0; argno < nargs; argno++)
95 {
96 struct value *arg = args[argno];
97 struct type *type = check_typedef (VALUE_TYPE (arg));
98 int len = TYPE_LENGTH (type);
99 char *val = VALUE_CONTENTS (arg);
100
101 if (TYPE_CODE (type) == TYPE_CODE_FLT
102 && ppc_floating_point_unit_p (current_gdbarch)
103 && len <= 8)
104 {
105 /* Floating point value converted to "double" then
106 passed in an FP register, when the registers run out,
107 8 byte aligned stack is used. */
108 if (freg <= 8)
109 {
110 if (write_pass)
111 {
112 /* Always store the floating point value using
113 the register's floating-point format. */
114 char regval[MAX_REGISTER_SIZE];
115 struct type *regtype
116 = register_type (gdbarch, FP0_REGNUM + freg);
117 convert_typed_floating (val, type, regval, regtype);
118 regcache_cooked_write (regcache, FP0_REGNUM + freg,
119 regval);
120 }
121 freg++;
122 }
123 else
124 {
125 /* SysV ABI converts floats to doubles before
126 writing them to an 8 byte aligned stack location. */
127 argoffset = align_up (argoffset, 8);
128 if (write_pass)
129 {
130 char memval[8];
131 struct type *memtype;
132 switch (TARGET_BYTE_ORDER)
133 {
134 case BFD_ENDIAN_BIG:
135 memtype = builtin_type_ieee_double_big;
136 break;
137 case BFD_ENDIAN_LITTLE:
138 memtype = builtin_type_ieee_double_little;
139 break;
140 default:
141 internal_error (__FILE__, __LINE__, "bad switch");
142 }
143 convert_typed_floating (val, type, memval, memtype);
144 write_memory (sp + argoffset, val, len);
145 }
146 argoffset += 8;
147 }
148 }
149 else if (len == 8
150 && (TYPE_CODE (type) == TYPE_CODE_INT /* long long */
151 || (!ppc_floating_point_unit_p (current_gdbarch)
152 && TYPE_CODE (type) == TYPE_CODE_FLT))) /* double */
153 {
154 /* "long long" or "double" passed in an odd/even
155 register pair with the low addressed word in the odd
156 register and the high addressed word in the even
157 register, or when the registers run out an 8 byte
158 aligned stack location. */
159 if (greg > 9)
160 {
161 /* Just in case GREG was 10. */
162 greg = 11;
163 argoffset = align_up (argoffset, 8);
164 if (write_pass)
165 write_memory (sp + argoffset, val, len);
166 argoffset += 8;
167 }
168 else if (tdep->wordsize == 8)
169 {
170 if (write_pass)
171 regcache_cooked_write (regcache,
172 tdep->ppc_gp0_regnum + greg,
173 val);
174 greg += 1;
175 }
176 else
177 {
178 /* Must start on an odd register - r3/r4 etc. */
179 if ((greg & 1) == 0)
180 greg++;
181 if (write_pass)
182 {
183 regcache_cooked_write (regcache,
184 tdep->ppc_gp0_regnum + greg + 0,
185 val + 0);
186 regcache_cooked_write (regcache,
187 tdep->ppc_gp0_regnum + greg + 1,
188 val + 4);
189 }
190 greg += 2;
191 }
192 }
193 else if (len == 16
194 && TYPE_CODE (type) == TYPE_CODE_ARRAY
195 && TYPE_VECTOR (type)
196 && tdep->ppc_vr0_regnum >= 0)
197 {
198 /* Vector parameter passed in an Altivec register, or
199 when that runs out, 16 byte aligned stack location. */
200 if (vreg <= 13)
201 {
202 if (write_pass)
203 regcache_cooked_write (current_regcache,
204 tdep->ppc_vr0_regnum + vreg,
205 val);
206 vreg++;
207 }
208 else
209 {
210 argoffset = align_up (argoffset, 16);
211 if (write_pass)
212 write_memory (sp + argoffset, val, 16);
213 argoffset += 16;
214 }
215 }
216 else if (len == 8
217 && TYPE_CODE (type) == TYPE_CODE_ARRAY
218 && TYPE_VECTOR (type)
219 && tdep->ppc_ev0_regnum >= 0)
220 {
221 /* Vector parameter passed in an e500 register, or when
222 that runs out, 8 byte aligned stack location. Note
223 that since e500 vector and general purpose registers
224 both map onto the same underlying register set, a
225 "greg" and not a "vreg" is consumed here. A cooked
226 write stores the value in the correct locations
227 within the raw register cache. */
228 if (greg <= 10)
229 {
230 if (write_pass)
231 regcache_cooked_write (current_regcache,
232 tdep->ppc_ev0_regnum + greg,
233 val);
234 greg++;
235 }
236 else
237 {
238 argoffset = align_up (argoffset, 8);
239 if (write_pass)
240 write_memory (sp + argoffset, val, 8);
241 argoffset += 8;
242 }
243 }
244 else
245 {
246 /* Reduce the parameter down to something that fits in a
247 "word". */
248 char word[MAX_REGISTER_SIZE];
249 memset (word, 0, MAX_REGISTER_SIZE);
250 if (len > tdep->wordsize
251 || TYPE_CODE (type) == TYPE_CODE_STRUCT
252 || TYPE_CODE (type) == TYPE_CODE_UNION)
253 {
254 /* Structs and large values are put on an 8 byte
255 aligned stack ... */
256 structoffset = align_up (structoffset, 8);
257 if (write_pass)
258 write_memory (sp + structoffset, val, len);
259 /* ... and then a "word" pointing to that address is
260 passed as the parameter. */
261 store_unsigned_integer (word, tdep->wordsize,
262 sp + structoffset);
263 structoffset += len;
264 }
265 else if (TYPE_CODE (type) == TYPE_CODE_INT)
266 /* Sign or zero extend the "int" into a "word". */
267 store_unsigned_integer (word, tdep->wordsize,
268 unpack_long (type, val));
269 else
270 /* Always goes in the low address. */
271 memcpy (word, val, len);
272 /* Store that "word" in a register, or on the stack.
273 The words have "4" byte alignment. */
274 if (greg <= 10)
275 {
276 if (write_pass)
277 regcache_cooked_write (regcache,
278 tdep->ppc_gp0_regnum + greg,
279 word);
280 greg++;
281 }
282 else
283 {
284 argoffset = align_up (argoffset, tdep->wordsize);
285 if (write_pass)
286 write_memory (sp + argoffset, word, tdep->wordsize);
287 argoffset += tdep->wordsize;
288 }
289 }
290 }
291
292 /* Compute the actual stack space requirements. */
293 if (!write_pass)
294 {
295 /* Remember the amount of space needed by the arguments. */
296 argspace = argoffset;
297 /* Allocate space for both the arguments and the structures. */
298 sp -= (argoffset + structoffset);
299 /* Ensure that the stack is still 16 byte aligned. */
300 sp = align_down (sp, 16);
301 }
302 }
303
304 /* Update %sp. */
305 regcache_cooked_write_signed (regcache, SP_REGNUM, sp);
306
307 /* Write the backchain (it occupies WORDSIZED bytes). */
308 write_memory_signed_integer (sp, tdep->wordsize, saved_sp);
309
310 /* Point the inferior function call's return address at the dummy's
311 breakpoint. */
312 regcache_cooked_write_signed (regcache, tdep->ppc_lr_regnum, bp_addr);
313
314 return sp;
315 }
316
317 /* Structures 8 bytes or less long are returned in the r3 & r4
318 registers, according to the SYSV ABI. */
319 int
320 ppc_sysv_abi_use_struct_convention (int gcc_p, struct type *value_type)
321 {
322 if ((TYPE_LENGTH (value_type) == 16 || TYPE_LENGTH (value_type) == 8)
323 && TYPE_VECTOR (value_type))
324 return 0;
325
326 return (TYPE_LENGTH (value_type) > 8);
327 }
328
329
330 /* The 64 bit ABI retun value convention.
331
332 Return non-zero if the return-value is stored in a register, return
333 0 if the return-value is instead stored on the stack (a.k.a.,
334 struct return convention).
335
336 For a return-value stored in a register: when INVAL is non-NULL,
337 copy the buffer to the corresponding register return-value location
338 location; when OUTVAL is non-NULL, fill the buffer from the
339 corresponding register return-value location. */
340
341 /* Potential ways that a function can return a value of a given type. */
342 enum return_value_convention
343 {
344 /* Where the return value has been squeezed into one or more
345 registers. */
346 RETURN_VALUE_REGISTER_CONVENTION,
347 /* Commonly known as the "struct return convention". The caller
348 passes an additional hidden first parameter to the caller. That
349 parameter contains the address at which the value being returned
350 should be stored. While typically, and historically, used for
351 large structs, this is convention is applied to values of many
352 different types. */
353 RETURN_VALUE_STRUCT_CONVENTION
354 };
355
356 static enum return_value_convention
357 ppc64_sysv_abi_return_value (struct type *valtype, struct regcache *regcache,
358 const void *inval, void *outval)
359 {
360 struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch);
361 /* Floats and doubles in F1. */
362 if (TYPE_CODE (valtype) == TYPE_CODE_FLT
363 && TYPE_LENGTH (valtype) <= 8)
364 {
365 char regval[MAX_REGISTER_SIZE];
366 struct type *regtype = register_type (current_gdbarch, FP0_REGNUM);
367 if (inval != NULL)
368 {
369 convert_typed_floating (inval, valtype, regval, regtype);
370 regcache_cooked_write (regcache, FP0_REGNUM + 1, regval);
371 }
372 if (outval != NULL)
373 {
374 regcache_cooked_read (regcache, FP0_REGNUM + 1, regval);
375 convert_typed_floating (regval, regtype, outval, valtype);
376 }
377 return RETURN_VALUE_REGISTER_CONVENTION;
378 }
379 if (TYPE_CODE (valtype) == TYPE_CODE_INT
380 && TYPE_LENGTH (valtype) <= 8)
381 {
382 /* Integers in r3. */
383 if (inval != NULL)
384 {
385 /* Be careful to sign extend the value. */
386 regcache_cooked_write_unsigned (regcache, tdep->ppc_gp0_regnum + 3,
387 unpack_long (valtype, inval));
388 }
389 if (outval != NULL)
390 {
391 /* Extract the integer from r3. Since this is truncating the
392 value, there isn't a sign extension problem. */
393 ULONGEST regval;
394 regcache_cooked_read_unsigned (regcache, tdep->ppc_gp0_regnum + 3,
395 &regval);
396 store_unsigned_integer (outval, TYPE_LENGTH (valtype), regval);
397 }
398 return RETURN_VALUE_REGISTER_CONVENTION;
399 }
400 /* All pointers live in r3. */
401 if (TYPE_CODE (valtype) == TYPE_CODE_PTR)
402 {
403 /* All pointers live in r3. */
404 if (inval != NULL)
405 regcache_cooked_write (regcache, tdep->ppc_gp0_regnum + 3, inval);
406 if (outval != NULL)
407 regcache_cooked_read (regcache, tdep->ppc_gp0_regnum + 3, outval);
408 return RETURN_VALUE_REGISTER_CONVENTION;
409 }
410 if (TYPE_CODE (valtype) == TYPE_CODE_ARRAY
411 && TYPE_LENGTH (valtype) <= 8
412 && TYPE_CODE (TYPE_TARGET_TYPE (valtype)) == TYPE_CODE_INT
413 && TYPE_LENGTH (TYPE_TARGET_TYPE (valtype)) == 1)
414 {
415 /* Small character arrays are returned, right justified, in r3. */
416 int offset = (register_size (current_gdbarch, tdep->ppc_gp0_regnum + 3)
417 - TYPE_LENGTH (valtype));
418 if (inval != NULL)
419 regcache_cooked_write_part (regcache, tdep->ppc_gp0_regnum + 3,
420 offset, TYPE_LENGTH (valtype), inval);
421 if (outval != NULL)
422 regcache_cooked_read_part (regcache, tdep->ppc_gp0_regnum + 3,
423 offset, TYPE_LENGTH (valtype), outval);
424 return RETURN_VALUE_REGISTER_CONVENTION;
425 }
426 /* Big floating point values get stored in adjacent floating
427 point registers. */
428 if (TYPE_CODE (valtype) == TYPE_CODE_FLT
429 && (TYPE_LENGTH (valtype) == 16
430 || TYPE_LENGTH (valtype) == 32))
431 {
432 if (inval || outval != NULL)
433 {
434 int i;
435 for (i = 0; i < TYPE_LENGTH (valtype) / 8; i++)
436 {
437 if (inval != NULL)
438 regcache_cooked_write (regcache, FP0_REGNUM + 1 + i,
439 (const bfd_byte *) inval + i * 8);
440 if (outval != NULL)
441 regcache_cooked_read (regcache, FP0_REGNUM + 1 + i,
442 (bfd_byte *) outval + i * 8);
443 }
444 }
445 return RETURN_VALUE_REGISTER_CONVENTION;
446 }
447 /* Complex values get returned in f1:f2, need to convert. */
448 if (TYPE_CODE (valtype) == TYPE_CODE_COMPLEX
449 && (TYPE_LENGTH (valtype) == 8 || TYPE_LENGTH (valtype) == 16))
450 {
451 if (regcache != NULL)
452 {
453 int i;
454 for (i = 0; i < 2; i++)
455 {
456 char regval[MAX_REGISTER_SIZE];
457 struct type *regtype = register_type (current_gdbarch, FP0_REGNUM);
458 if (inval != NULL)
459 {
460 convert_typed_floating ((const bfd_byte *) inval + i * (TYPE_LENGTH (valtype) / 2),
461 valtype, regval, regtype);
462 regcache_cooked_write (regcache, FP0_REGNUM + 1 + i, regval);
463 }
464 if (outval != NULL)
465 {
466 regcache_cooked_read (regcache, FP0_REGNUM + 1 + i, regval);
467 convert_typed_floating (regval, regtype,
468 (bfd_byte *) outval + i * (TYPE_LENGTH (valtype) / 2),
469 valtype);
470 }
471 }
472 }
473 return RETURN_VALUE_REGISTER_CONVENTION;
474 }
475 /* Big complex values get stored in f1:f4. */
476 if (TYPE_CODE (valtype) == TYPE_CODE_COMPLEX
477 && TYPE_LENGTH (valtype) == 32)
478 {
479 if (regcache != NULL)
480 {
481 int i;
482 for (i = 0; i < 4; i++)
483 {
484 if (inval != NULL)
485 regcache_cooked_write (regcache, FP0_REGNUM + 1 + i,
486 (const bfd_byte *) inval + i * 8);
487 if (outval != NULL)
488 regcache_cooked_read (regcache, FP0_REGNUM + 1 + i,
489 (bfd_byte *) outval + i * 8);
490 }
491 }
492 return RETURN_VALUE_REGISTER_CONVENTION;
493 }
494 return RETURN_VALUE_STRUCT_CONVENTION;
495 }
496
497 int
498 ppc64_sysv_abi_use_struct_convention (int gcc_p, struct type *value_type)
499 {
500 return (ppc64_sysv_abi_return_value (value_type, NULL, NULL, NULL)
501 == RETURN_VALUE_STRUCT_CONVENTION);
502 }
503
504 void
505 ppc64_sysv_abi_extract_return_value (struct type *valtype,
506 struct regcache *regbuf,
507 void *valbuf)
508 {
509 if (ppc64_sysv_abi_return_value (valtype, regbuf, NULL, valbuf)
510 != RETURN_VALUE_REGISTER_CONVENTION)
511 error ("Function return value unknown");
512 }
513
514 void
515 ppc64_sysv_abi_store_return_value (struct type *valtype,
516 struct regcache *regbuf,
517 const void *valbuf)
518 {
519 if (!ppc64_sysv_abi_return_value (valtype, regbuf, valbuf, NULL))
520 error ("Function return value location unknown");
521 }
This page took 0.045173 seconds and 4 git commands to generate.