xref: /netbsd-src/external/gpl3/gdb.old/dist/gdb/rs6000-lynx178-tdep.c (revision 70f7362772ba52b749c976fb5e86e39a8b2c9afc)
1 /* Copyright (C) 2012-2020 Free Software Foundation, Inc.
2 
3    This file is part of GDB.
4 
5    This program is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 3 of the License, or
8    (at your option) any later version.
9 
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14 
15    You should have received a copy of the GNU General Public License
16    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
17 
18 #include "defs.h"
19 #include "osabi.h"
20 #include "regcache.h"
21 #include "gdbcore.h"
22 #include "gdbtypes.h"
23 #include "infcall.h"
24 #include "ppc-tdep.h"
25 #include "target-float.h"
26 #include "value.h"
27 #include "xcoffread.h"
28 
29 /* Implement the "push_dummy_call" gdbarch method.  */
30 
31 static CORE_ADDR
32 rs6000_lynx178_push_dummy_call (struct gdbarch *gdbarch,
33 				struct value *function,
34 				struct regcache *regcache, CORE_ADDR bp_addr,
35 				int nargs, struct value **args, CORE_ADDR sp,
36 				function_call_return_method return_method,
37 				CORE_ADDR struct_addr)
38 {
39   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
40   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
41   int ii;
42   int len = 0;
43   int argno;			/* current argument number */
44   int argbytes;			/* current argument byte */
45   gdb_byte tmp_buffer[50];
46   int f_argno = 0;		/* current floating point argno */
47   int wordsize = gdbarch_tdep (gdbarch)->wordsize;
48 
49   struct value *arg = 0;
50   struct type *type;
51 
52   ULONGEST saved_sp;
53 
54   /* The calling convention this function implements assumes the
55      processor has floating-point registers.  We shouldn't be using it
56      on PPC variants that lack them.  */
57   gdb_assert (ppc_floating_point_unit_p (gdbarch));
58 
59   /* The first eight words of ther arguments are passed in registers.
60      Copy them appropriately.  */
61   ii = 0;
62 
63   /* If the function is returning a `struct', then the first word
64      (which will be passed in r3) is used for struct return address.
65      In that case we should advance one word and start from r4
66      register to copy parameters.  */
67   if (return_method == return_method_struct)
68     {
69       regcache_raw_write_unsigned (regcache, tdep->ppc_gp0_regnum + 3,
70 				   struct_addr);
71       ii++;
72     }
73 
74   /* Effectively indirect call... gcc does...
75 
76      return_val example( float, int);
77 
78      eabi:
79      float in fp0, int in r3
80      offset of stack on overflow 8/16
81      for varargs, must go by type.
82      power open:
83      float in r3&r4, int in r5
84      offset of stack on overflow different
85      both:
86      return in r3 or f0.  If no float, must study how gcc emulates floats;
87      pay attention to arg promotion.
88      User may have to cast\args to handle promotion correctly
89      since gdb won't know if prototype supplied or not.  */
90 
91   for (argno = 0, argbytes = 0; argno < nargs && ii < 8; ++ii)
92     {
93       int reg_size = register_size (gdbarch, ii + 3);
94 
95       arg = args[argno];
96       type = check_typedef (value_type (arg));
97       len = TYPE_LENGTH (type);
98 
99       if (type->code () == TYPE_CODE_FLT)
100 	{
101 
102 	  /* Floating point arguments are passed in fpr's, as well as gpr's.
103 	     There are 13 fpr's reserved for passing parameters.  At this point
104 	     there is no way we would run out of them.
105 
106 	     Always store the floating point value using the register's
107 	     floating-point format.  */
108 	  const int fp_regnum = tdep->ppc_fp0_regnum + 1 + f_argno;
109 	  gdb_byte reg_val[PPC_MAX_REGISTER_SIZE];
110 	  struct type *reg_type = register_type (gdbarch, fp_regnum);
111 
112 	  gdb_assert (len <= 8);
113 
114 	  target_float_convert (value_contents (arg), type, reg_val, reg_type);
115 	  regcache->cooked_write (fp_regnum, reg_val);
116 	  ++f_argno;
117 	}
118 
119       if (len > reg_size)
120 	{
121 
122 	  /* Argument takes more than one register.  */
123 	  while (argbytes < len)
124 	    {
125 	      gdb_byte word[PPC_MAX_REGISTER_SIZE];
126 	      memset (word, 0, reg_size);
127 	      memcpy (word,
128 		      ((char *) value_contents (arg)) + argbytes,
129 		      (len - argbytes) > reg_size
130 		        ? reg_size : len - argbytes);
131 	      regcache->cooked_write (tdep->ppc_gp0_regnum + 3 + ii, word);
132 	      ++ii, argbytes += reg_size;
133 
134 	      if (ii >= 8)
135 		goto ran_out_of_registers_for_arguments;
136 	    }
137 	  argbytes = 0;
138 	  --ii;
139 	}
140       else
141 	{
142 	  /* Argument can fit in one register.  No problem.  */
143 	  gdb_byte word[PPC_MAX_REGISTER_SIZE];
144 
145 	  memset (word, 0, reg_size);
146 	  memcpy (word, value_contents (arg), len);
147 	  regcache->cooked_write (tdep->ppc_gp0_regnum + 3 +ii, word);
148 	}
149       ++argno;
150     }
151 
152 ran_out_of_registers_for_arguments:
153 
154   regcache_cooked_read_unsigned (regcache,
155 				 gdbarch_sp_regnum (gdbarch),
156 				 &saved_sp);
157 
158   /* Location for 8 parameters are always reserved.  */
159   sp -= wordsize * 8;
160 
161   /* Another six words for back chain, TOC register, link register, etc.  */
162   sp -= wordsize * 6;
163 
164   /* Stack pointer must be quadword aligned.  */
165   sp = align_down (sp, 16);
166 
167   /* If there are more arguments, allocate space for them in
168      the stack, then push them starting from the ninth one.  */
169 
170   if ((argno < nargs) || argbytes)
171     {
172       int space = 0, jj;
173 
174       if (argbytes)
175 	{
176 	  space += align_up (len - argbytes, 4);
177 	  jj = argno + 1;
178 	}
179       else
180 	jj = argno;
181 
182       for (; jj < nargs; ++jj)
183 	{
184 	  struct value *val = args[jj];
185 
186 	  space += align_up (TYPE_LENGTH (value_type (val)), 4);
187 	}
188 
189       /* Add location required for the rest of the parameters.  */
190       space = align_up (space, 16);
191       sp -= space;
192 
193       /* This is another instance we need to be concerned about
194          securing our stack space.  If we write anything underneath %sp
195          (r1), we might conflict with the kernel who thinks he is free
196          to use this area.  So, update %sp first before doing anything
197          else.  */
198 
199       regcache_raw_write_signed (regcache,
200 				 gdbarch_sp_regnum (gdbarch), sp);
201 
202       /* If the last argument copied into the registers didn't fit there
203          completely, push the rest of it into stack.  */
204 
205       if (argbytes)
206 	{
207 	  write_memory (sp + 24 + (ii * 4),
208 			value_contents (arg) + argbytes,
209 			len - argbytes);
210 	  ++argno;
211 	  ii += align_up (len - argbytes, 4) / 4;
212 	}
213 
214       /* Push the rest of the arguments into stack.  */
215       for (; argno < nargs; ++argno)
216 	{
217 
218 	  arg = args[argno];
219 	  type = check_typedef (value_type (arg));
220 	  len = TYPE_LENGTH (type);
221 
222 
223 	  /* Float types should be passed in fpr's, as well as in the
224              stack.  */
225 	  if (type->code () == TYPE_CODE_FLT && f_argno < 13)
226 	    {
227 
228 	      gdb_assert (len <= 8);
229 
230 	      regcache->cooked_write (tdep->ppc_fp0_regnum + 1 + f_argno,
231 				      value_contents (arg));
232 	      ++f_argno;
233 	    }
234 
235 	  write_memory (sp + 24 + (ii * 4), value_contents (arg), len);
236 	  ii += align_up (len, 4) / 4;
237 	}
238     }
239 
240   /* Set the stack pointer.  According to the ABI, the SP is meant to
241      be set _before_ the corresponding stack space is used.  On AIX,
242      this even applies when the target has been completely stopped!
243      Not doing this can lead to conflicts with the kernel which thinks
244      that it still has control over this not-yet-allocated stack
245      region.  */
246   regcache_raw_write_signed (regcache, gdbarch_sp_regnum (gdbarch), sp);
247 
248   /* Set back chain properly.  */
249   store_unsigned_integer (tmp_buffer, wordsize, byte_order, saved_sp);
250   write_memory (sp, tmp_buffer, wordsize);
251 
252   /* Point the inferior function call's return address at the dummy's
253      breakpoint.  */
254   regcache_raw_write_signed (regcache, tdep->ppc_lr_regnum, bp_addr);
255 
256   target_store_registers (regcache, -1);
257   return sp;
258 }
259 
260 /* Implement the "return_value" gdbarch method.  */
261 
262 static enum return_value_convention
263 rs6000_lynx178_return_value (struct gdbarch *gdbarch, struct value *function,
264 			     struct type *valtype, struct regcache *regcache,
265 			     gdb_byte *readbuf, const gdb_byte *writebuf)
266 {
267   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
268   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
269 
270   /* The calling convention this function implements assumes the
271      processor has floating-point registers.  We shouldn't be using it
272      on PowerPC variants that lack them.  */
273   gdb_assert (ppc_floating_point_unit_p (gdbarch));
274 
275   /* AltiVec extension: Functions that declare a vector data type as a
276      return value place that return value in VR2.  */
277   if (valtype->code () == TYPE_CODE_ARRAY && TYPE_VECTOR (valtype)
278       && TYPE_LENGTH (valtype) == 16)
279     {
280       if (readbuf)
281 	regcache->cooked_read (tdep->ppc_vr0_regnum + 2, readbuf);
282       if (writebuf)
283 	regcache->cooked_write (tdep->ppc_vr0_regnum + 2, writebuf);
284 
285       return RETURN_VALUE_REGISTER_CONVENTION;
286     }
287 
288   /* If the called subprogram returns an aggregate, there exists an
289      implicit first argument, whose value is the address of a caller-
290      allocated buffer into which the callee is assumed to store its
291      return value.  All explicit parameters are appropriately
292      relabeled.  */
293   if (valtype->code () == TYPE_CODE_STRUCT
294       || valtype->code () == TYPE_CODE_UNION
295       || valtype->code () == TYPE_CODE_ARRAY)
296     return RETURN_VALUE_STRUCT_CONVENTION;
297 
298   /* Scalar floating-point values are returned in FPR1 for float or
299      double, and in FPR1:FPR2 for quadword precision.  Fortran
300      complex*8 and complex*16 are returned in FPR1:FPR2, and
301      complex*32 is returned in FPR1:FPR4.  */
302   if (valtype->code () == TYPE_CODE_FLT
303       && (TYPE_LENGTH (valtype) == 4 || TYPE_LENGTH (valtype) == 8))
304     {
305       struct type *regtype = register_type (gdbarch, tdep->ppc_fp0_regnum);
306       gdb_byte regval[8];
307 
308       /* FIXME: kettenis/2007-01-01: Add support for quadword
309 	 precision and complex.  */
310 
311       if (readbuf)
312 	{
313 	  regcache->cooked_read (tdep->ppc_fp0_regnum + 1, regval);
314 	  target_float_convert (regval, regtype, readbuf, valtype);
315 	}
316       if (writebuf)
317 	{
318 	  target_float_convert (writebuf, valtype, regval, regtype);
319 	  regcache->cooked_write (tdep->ppc_fp0_regnum + 1, regval);
320 	}
321 
322       return RETURN_VALUE_REGISTER_CONVENTION;
323   }
324 
325   /* Values of the types int, long, short, pointer, and char (length
326      is less than or equal to four bytes), as well as bit values of
327      lengths less than or equal to 32 bits, must be returned right
328      justified in GPR3 with signed values sign extended and unsigned
329      values zero extended, as necessary.  */
330   if (TYPE_LENGTH (valtype) <= tdep->wordsize)
331     {
332       if (readbuf)
333 	{
334 	  ULONGEST regval;
335 
336 	  /* For reading we don't have to worry about sign extension.  */
337 	  regcache_cooked_read_unsigned (regcache, tdep->ppc_gp0_regnum + 3,
338 					 &regval);
339 	  store_unsigned_integer (readbuf, TYPE_LENGTH (valtype), byte_order,
340 				  regval);
341 	}
342       if (writebuf)
343 	{
344 	  /* For writing, use unpack_long since that should handle any
345 	     required sign extension.  */
346 	  regcache_cooked_write_unsigned (regcache, tdep->ppc_gp0_regnum + 3,
347 					  unpack_long (valtype, writebuf));
348 	}
349 
350       return RETURN_VALUE_REGISTER_CONVENTION;
351     }
352 
353   /* Eight-byte non-floating-point scalar values must be returned in
354      GPR3:GPR4.  */
355 
356   if (TYPE_LENGTH (valtype) == 8)
357     {
358       gdb_assert (valtype->code () != TYPE_CODE_FLT);
359       gdb_assert (tdep->wordsize == 4);
360 
361       if (readbuf)
362 	{
363 	  gdb_byte regval[8];
364 
365 	  regcache->cooked_read (tdep->ppc_gp0_regnum + 3, regval);
366 	  regcache->cooked_read (tdep->ppc_gp0_regnum + 4, regval + 4);
367 	  memcpy (readbuf, regval, 8);
368 	}
369       if (writebuf)
370 	{
371 	  regcache->cooked_write (tdep->ppc_gp0_regnum + 3, writebuf);
372 	  regcache->cooked_write (tdep->ppc_gp0_regnum + 4, writebuf + 4);
373 	}
374 
375       return RETURN_VALUE_REGISTER_CONVENTION;
376     }
377 
378   return RETURN_VALUE_STRUCT_CONVENTION;
379 }
380 
381 /* PowerPC Lynx178 OSABI sniffer.  */
382 
383 static enum gdb_osabi
384 rs6000_lynx178_osabi_sniffer (bfd *abfd)
385 {
386   if (bfd_get_flavour (abfd) != bfd_target_xcoff_flavour)
387     return GDB_OSABI_UNKNOWN;
388 
389   /* The only noticeable difference between Lynx178 XCOFF files and
390      AIX XCOFF files comes from the fact that there are no shared
391      libraries on Lynx178.  So if the number of import files is
392      different from zero, it cannot be a Lynx178 binary.  */
393   if (xcoff_get_n_import_files (abfd) != 0)
394     return GDB_OSABI_UNKNOWN;
395 
396   return GDB_OSABI_LYNXOS178;
397 }
398 
399 /* Callback for powerpc-lynx178 initialization.  */
400 
401 static void
402 rs6000_lynx178_init_osabi (struct gdbarch_info info, struct gdbarch *gdbarch)
403 {
404   set_gdbarch_push_dummy_call (gdbarch, rs6000_lynx178_push_dummy_call);
405   set_gdbarch_return_value (gdbarch, rs6000_lynx178_return_value);
406   set_gdbarch_long_double_bit (gdbarch, 8 * TARGET_CHAR_BIT);
407 }
408 
409 void _initialize_rs6000_lynx178_tdep ();
410 void
411 _initialize_rs6000_lynx178_tdep ()
412 {
413   gdbarch_register_osabi_sniffer (bfd_arch_rs6000,
414                                   bfd_target_xcoff_flavour,
415                                   rs6000_lynx178_osabi_sniffer);
416   gdbarch_register_osabi (bfd_arch_rs6000, 0, GDB_OSABI_LYNXOS178,
417                           rs6000_lynx178_init_osabi);
418 }
419 
420