2003-09-09 Andrew Cagney <cagney@redhat.com>
[external/binutils.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 /* round2 rounds x up to the nearest multiple of s assuming that s is a
33    power of 2 */
34
35 #undef round2
36 #define round2(x,s) ((((long) (x) - 1) & ~(long)((s)-1)) + (s))
37
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. 
44
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,
47    starting from r4. */
48
49 CORE_ADDR
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)
54 {
55   int argno;
56   /* Next available general register for non-float, non-vector arguments. */
57   int greg;
58   /* Next available floating point register for float arguments. */
59   int freg;
60   /* Next available vector register for vector arguments. */
61   int vreg;
62   int argstkspace;
63   int structstkspace;
64   int argoffset;
65   int structoffset;
66   struct type *type;
67   int len;
68   char old_sp_buf[4];
69   CORE_ADDR saved_sp;
70   struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch);
71
72   greg = 3;
73   freg = 1;
74   vreg = 2;
75   argstkspace = 0;
76   structstkspace = 0;
77
78   /* If the function is returning a `struct', then the first word
79      (which will be passed in r3) is used for struct return address.
80      In that case we should advance one word and start from r4
81      register to copy parameters.  */
82   if (struct_return)
83     {
84       regcache_raw_write_signed (regcache, tdep->ppc_gp0_regnum + greg,
85                                  struct_addr);
86       greg++;
87     }
88
89   /* Figure out how much new stack space is required for arguments
90      which don't fit in registers.  Unlike the PowerOpen ABI, the
91      SysV ABI doesn't reserve any extra space for parameters which
92      are put in registers. */
93   for (argno = 0; argno < nargs; argno++)
94     {
95       struct value *arg = args[argno];
96       type = check_typedef (VALUE_TYPE (arg));
97       len = TYPE_LENGTH (type);
98
99       if (TYPE_CODE (type) == TYPE_CODE_FLT
100           && ppc_floating_point_unit_p (current_gdbarch))
101         {
102           if (freg <= 8)
103             freg++;
104           else
105             {
106               /* SysV ABI converts floats to doubles when placed in
107                  memory and requires 8 byte alignment */
108               if (argstkspace & 0x4)
109                 argstkspace += 4;
110               argstkspace += 8;
111             }
112         }
113       else if (len == 8 
114                && (TYPE_CODE (type) == TYPE_CODE_INT /* long long */
115                    || (!ppc_floating_point_unit_p (current_gdbarch)
116                        && TYPE_CODE (type) == TYPE_CODE_FLT))) /* double */
117         {
118           if (greg > 9)
119             {
120               greg = 11;
121               if (argstkspace & 0x4)
122                 argstkspace += 4;
123               argstkspace += 8;
124             }
125           else
126             {
127               if ((greg & 1) == 0)
128                 greg++;
129               greg += 2;
130             }
131         }
132       else if (!TYPE_VECTOR (type))
133         {
134           if (len > 4
135               || TYPE_CODE (type) == TYPE_CODE_STRUCT
136               || TYPE_CODE (type) == TYPE_CODE_UNION)
137             {
138               /* Rounding to the nearest multiple of 8 may not be necessary,
139                  but it is safe.  Particularly since we don't know the
140                  field types of the structure */
141               structstkspace += round2 (len, 8);
142             }
143           if (greg <= 10)
144             greg++;
145           else
146             argstkspace += 4;
147         }
148       else
149         {
150           if (len == 16
151               && TYPE_CODE (type) == TYPE_CODE_ARRAY
152               && TYPE_VECTOR (type))
153             {
154               if (vreg <= 13)
155                 vreg++;
156               else
157                 {
158                   /* Vector arguments must be aligned to 16 bytes on
159                      the stack. */
160                   argstkspace += round2 (argstkspace, 16);
161                   argstkspace += 16;
162                 }
163             }
164           else if (len == 8 
165                    && TYPE_CODE (type) == TYPE_CODE_ARRAY
166                    && TYPE_VECTOR (type))
167             {
168               if (greg <= 10)
169                 greg++;
170               else
171                 {
172                   /* Vector arguments must be aligned to 8 bytes on
173                      the stack. */
174                   argstkspace += round2 (argstkspace, 8);
175                   argstkspace += 8;
176                 }
177             }
178         }
179     }
180
181   /* Get current SP location */
182   saved_sp = read_sp ();
183
184   sp -= argstkspace + structstkspace;
185
186   /* Allocate space for backchain and callee's saved lr */
187   sp -= 8;
188
189   /* Make sure that we maintain 16 byte alignment */
190   sp &= ~0x0f;
191
192   /* Update %sp before proceeding any further */
193   write_register (SP_REGNUM, sp);
194
195   /* write the backchain */
196   store_unsigned_integer (old_sp_buf, 4, saved_sp);
197   write_memory (sp, old_sp_buf, 4);
198
199   argoffset = 8;
200   structoffset = argoffset + argstkspace;
201   freg = 1;
202   greg = 3;
203   vreg = 2;
204
205   /* Fill in r3 with the return structure, if any */
206   if (struct_return)
207     {
208       write_register (tdep->ppc_gp0_regnum + greg, struct_addr);
209       greg++;
210     }
211
212   /* Now fill in the registers and stack... */
213   for (argno = 0; argno < nargs; argno++)
214     {
215       struct value *arg = args[argno];
216       char *val = VALUE_CONTENTS (arg);
217       type = check_typedef (VALUE_TYPE (arg));
218       len = TYPE_LENGTH (type);
219
220       if (TYPE_CODE (type) == TYPE_CODE_FLT
221           && ppc_floating_point_unit_p (current_gdbarch))
222         {
223           if (freg <= 8)
224             {
225               ULONGEST regval;
226               if (len > 8)
227                 printf_unfiltered (
228                                    "Fatal Error: a floating point parameter #%d with a size > 8 is found!\n", argno);
229               regval = extract_unsigned_integer (val, len);
230               write_register (FP0_REGNUM + freg, regval);
231               freg++;
232             }
233           else
234             {
235               /* SysV ABI converts floats to doubles when placed in
236                  memory and requires 8 byte alignment */
237               /* FIXME: Convert floats to doubles */
238               if (argoffset & 0x4)
239                 argoffset += 4;
240               write_memory (sp + argoffset, val, len);
241               argoffset += 8;
242             }
243         }
244       else if (len == 8 
245                && (TYPE_CODE (type) == TYPE_CODE_INT /* long long */
246                    || (!ppc_floating_point_unit_p (current_gdbarch)
247                         && TYPE_CODE (type) == TYPE_CODE_FLT))) /* double */
248         {
249           if (greg > 9)
250             {
251               greg = 11;
252               if (argoffset & 0x4)
253                 argoffset += 4;
254               write_memory (sp + argoffset, val, len);
255               argoffset += 8;
256             }
257           else
258             {
259               ULONGEST regval;
260               if ((greg & 1) == 0)
261                 greg++;
262               regval = extract_unsigned_integer (val, 4);
263               write_register (tdep->ppc_gp0_regnum + greg, regval);
264               regval = extract_unsigned_integer (val + 4, 4);
265               write_register (tdep->ppc_gp0_regnum + greg + 1, regval);
266               greg += 2;
267             }
268         }
269       else if (!TYPE_VECTOR (type))
270         {
271           char val_buf[4];
272           if (len > 4
273               || TYPE_CODE (type) == TYPE_CODE_STRUCT
274               || TYPE_CODE (type) == TYPE_CODE_UNION)
275             {
276               write_memory (sp + structoffset, val, len);
277               store_unsigned_integer (val_buf, 4, sp + structoffset);
278               structoffset += round2 (len, 8);
279             }
280           else
281             {
282               memset (val_buf, 0, 4);
283               memcpy (val_buf, val, len);
284             }
285           if (greg <= 10)
286             {
287               ULONGEST regval = extract_unsigned_integer (val_buf, 4);
288               write_register (tdep->ppc_gp0_regnum + greg, regval);
289               greg++;
290             }
291           else
292             {
293               write_memory (sp + argoffset, val_buf, 4);
294               argoffset += 4;
295             }
296         }
297       else
298         {
299           if (len == 16
300               && TYPE_CODE (type) == TYPE_CODE_ARRAY
301               && TYPE_VECTOR (type))
302             {
303               char *v_val_buf = alloca (16);
304               memset (v_val_buf, 0, 16);
305               memcpy (v_val_buf, val, len);
306               if (vreg <= 13)
307                 {
308                   regcache_cooked_write (current_regcache,
309                                          tdep->ppc_vr0_regnum + vreg,
310                                          v_val_buf);
311                   vreg++;
312                 }
313               else
314                 {
315                   write_memory (sp + argoffset, v_val_buf, 16);
316                   argoffset += 16;
317                 }
318             }
319           else if (len == 8 
320                    && TYPE_CODE (type) == TYPE_CODE_ARRAY
321                    && TYPE_VECTOR (type))
322             {
323               char *v_val_buf = alloca (8);
324               memset (v_val_buf, 0, 8);
325               memcpy (v_val_buf, val, len);
326               if (greg <= 10)
327                 {
328                   regcache_cooked_write (current_regcache,
329                                          tdep->ppc_ev0_regnum + greg,
330                                          v_val_buf);
331                   greg++;
332                 }
333               else
334                 {
335                   write_memory (sp + argoffset, v_val_buf, 8);
336                   argoffset += 8;
337                 }
338             }
339         }
340     }
341
342   /* Point the inferior function call's return address at the dummy's
343      breakpoint.  */
344   regcache_raw_write_signed (regcache, tdep->ppc_lr_regnum, bp_addr);
345
346   target_store_registers (-1);
347   return sp;
348 }
349
350 /* Until November 2001, gcc was not complying to the SYSV ABI for 
351    returning structures less than or equal to 8 bytes in size.  It was
352    returning everything in memory.  When this was corrected, it wasn't
353    fixed for native platforms.  */
354 int     
355 ppc_sysv_abi_broken_use_struct_convention (int gcc_p, struct type *value_type)
356 {  
357   if ((TYPE_LENGTH (value_type) == 16 || TYPE_LENGTH (value_type) == 8)
358       && TYPE_VECTOR (value_type))
359     return 0;                            
360
361   return generic_use_struct_convention (gcc_p, value_type);
362 }
363
364 /* Structures 8 bytes or less long are returned in the r3 & r4
365    registers, according to the SYSV ABI. */
366 int
367 ppc_sysv_abi_use_struct_convention (int gcc_p, struct type *value_type)
368 {
369   if ((TYPE_LENGTH (value_type) == 16 || TYPE_LENGTH (value_type) == 8)
370       && TYPE_VECTOR (value_type))
371     return 0;
372
373   return (TYPE_LENGTH (value_type) > 8);
374 }