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