2002-11-14 Andrew Cagney <ac131313@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 value *arg;
65   struct type *type;
66   int len;
67   char old_sp_buf[4];
68   CORE_ADDR saved_sp;
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       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         {
88           if (freg <= 8)
89             freg++;
90           else
91             {
92               /* SysV ABI converts floats to doubles when placed in
93                  memory and requires 8 byte alignment */
94               if (argstkspace & 0x4)
95                 argstkspace += 4;
96               argstkspace += 8;
97             }
98         }
99       else if (TYPE_CODE (type) == TYPE_CODE_INT && len == 8)   /* long long */
100         {
101           if (greg > 9)
102             {
103               greg = 11;
104               if (argstkspace & 0x4)
105                 argstkspace += 4;
106               argstkspace += 8;
107             }
108           else
109             {
110               if ((greg & 1) == 0)
111                 greg++;
112               greg += 2;
113             }
114         }
115       else if (!TYPE_VECTOR (type))
116         {
117           if (len > 4
118               || TYPE_CODE (type) == TYPE_CODE_STRUCT
119               || TYPE_CODE (type) == TYPE_CODE_UNION)
120             {
121               /* Rounding to the nearest multiple of 8 may not be necessary,
122                  but it is safe.  Particularly since we don't know the
123                  field types of the structure */
124               structstkspace += round2 (len, 8);
125             }
126           if (greg <= 10)
127             greg++;
128           else
129             argstkspace += 4;
130         }
131       else
132         {
133           if (len == 16
134               && TYPE_CODE (type) == TYPE_CODE_ARRAY
135               && TYPE_VECTOR (type))
136             {
137               if (vreg <= 13)
138                 vreg++;
139               else
140                 {
141                   /* Vector arguments must be aligned to 16 bytes on
142                      the stack. */
143                   argstkspace += round2 (argstkspace, 16);
144                   argstkspace += 16;
145                 }
146             }
147         }
148     }
149
150   /* Get current SP location */
151   saved_sp = read_sp ();
152
153   sp -= argstkspace + structstkspace;
154
155   /* Allocate space for backchain and callee's saved lr */
156   sp -= 8;
157
158   /* Make sure that we maintain 16 byte alignment */
159   sp &= ~0x0f;
160
161   /* Update %sp before proceeding any further */
162   write_register (SP_REGNUM, sp);
163
164   /* write the backchain */
165   store_address (old_sp_buf, 4, saved_sp);
166   write_memory (sp, old_sp_buf, 4);
167
168   argoffset = 8;
169   structoffset = argoffset + argstkspace;
170   freg = 1;
171   greg = 3;
172   vreg = 2;
173   /* Fill in r3 with the return structure, if any */
174   if (struct_return)
175     {
176       char val_buf[4];
177       store_address (val_buf, 4, struct_addr);
178       memcpy (&deprecated_registers[REGISTER_BYTE (greg)], val_buf, 4);
179       greg++;
180     }
181   /* Now fill in the registers and stack... */
182   for (argno = 0; argno < nargs; argno++)
183     {
184       arg = args[argno];
185       type = check_typedef (VALUE_TYPE (arg));
186       len = TYPE_LENGTH (type);
187
188       if (TYPE_CODE (type) == TYPE_CODE_FLT)
189         {
190           if (freg <= 8)
191             {
192               if (len > 8)
193                 printf_unfiltered (
194                                    "Fatal Error: a floating point parameter #%d with a size > 8 is found!\n", argno);
195               memcpy (&deprecated_registers[REGISTER_BYTE (FP0_REGNUM + freg)],
196                       VALUE_CONTENTS (arg), len);
197               freg++;
198             }
199           else
200             {
201               /* SysV ABI converts floats to doubles when placed in
202                  memory and requires 8 byte alignment */
203               /* FIXME: Convert floats to doubles */
204               if (argoffset & 0x4)
205                 argoffset += 4;
206               write_memory (sp + argoffset, (char *) VALUE_CONTENTS (arg), len);
207               argoffset += 8;
208             }
209         }
210       else if (TYPE_CODE (type) == TYPE_CODE_INT && len == 8)   /* long long */
211         {
212           if (greg > 9)
213             {
214               greg = 11;
215               if (argoffset & 0x4)
216                 argoffset += 4;
217               write_memory (sp + argoffset, (char *) VALUE_CONTENTS (arg), len);
218               argoffset += 8;
219             }
220           else
221             {
222               if ((greg & 1) == 0)
223                 greg++;
224
225               memcpy (&deprecated_registers[REGISTER_BYTE (greg)],
226                       VALUE_CONTENTS (arg), 4);
227               memcpy (&deprecated_registers[REGISTER_BYTE (greg + 1)],
228                       VALUE_CONTENTS (arg) + 4, 4);
229               greg += 2;
230             }
231         }
232       else if (!TYPE_VECTOR (type))
233         {
234           char val_buf[4];
235           if (len > 4
236               || TYPE_CODE (type) == TYPE_CODE_STRUCT
237               || TYPE_CODE (type) == TYPE_CODE_UNION)
238             {
239               write_memory (sp + structoffset, VALUE_CONTENTS (arg), len);
240               store_address (val_buf, 4, sp + structoffset);
241               structoffset += round2 (len, 8);
242             }
243           else
244             {
245               memset (val_buf, 0, 4);
246               memcpy (val_buf, VALUE_CONTENTS (arg), len);
247             }
248           if (greg <= 10)
249             {
250               memcpy (&deprecated_registers[REGISTER_BYTE (greg)], val_buf, 4);
251               greg++;
252             }
253           else
254             {
255               write_memory (sp + argoffset, val_buf, 4);
256               argoffset += 4;
257             }
258         }
259       else
260         {
261           if (len == 16
262               && TYPE_CODE (type) == TYPE_CODE_ARRAY
263               && TYPE_VECTOR (type))
264             {
265               struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch);
266               char *v_val_buf = alloca (16);
267               memset (v_val_buf, 0, 16);
268               memcpy (v_val_buf, VALUE_CONTENTS (arg), len);
269               if (vreg <= 13)
270                 {
271                   memcpy (&deprecated_registers[REGISTER_BYTE (tdep->ppc_vr0_regnum
272                                                     + vreg)],
273                           v_val_buf, 16);
274                   vreg++;
275                 }
276               else
277                 {
278                   write_memory (sp + argoffset, v_val_buf, 16);
279                   argoffset += 16;
280                 }
281             }
282         }
283     }
284
285   target_store_registers (-1);
286   return sp;
287 }
288
289 /* Until November 2001, gcc was not complying to the SYSV ABI for 
290    returning structures less than or equal to 8 bytes in size.  It was
291    returning everything in memory.  When this was corrected, it wasn't
292    fixed for native platforms.  */
293 int     
294 ppc_sysv_abi_broken_use_struct_convention (int gcc_p, struct type *value_type)
295 {  
296   if (TYPE_LENGTH (value_type) == 16 
297       && TYPE_VECTOR (value_type))
298     return 0;                            
299
300   return generic_use_struct_convention (gcc_p, value_type);
301 }
302
303 /* Structures 8 bytes or less long are returned in the r3 & r4
304    registers, according to the SYSV ABI. */
305 int
306 ppc_sysv_abi_use_struct_convention (int gcc_p, struct type *value_type)
307 {
308   if (TYPE_LENGTH (value_type) == 16
309       && TYPE_VECTOR (value_type))
310     return 0;
311
312   return (TYPE_LENGTH (value_type) > 8);
313 }