Add generated source files and fix thinko in aarch64-asm.c
[binutils-gdb.git] / gdb / rs6000-lynx178-tdep.c
blobc92c71dcb8818f09ad0966fa0da636bf99f8ae4f
1 /* Copyright (C) 2012-2024 Free Software Foundation, Inc.
3 This file is part of GDB.
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.
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.
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/>. */
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"
29 /* Implement the "push_dummy_call" gdbarch method. */
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)
39 ppc_gdbarch_tdep *tdep = gdbarch_tdep<ppc_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 = tdep->wordsize;
49 struct value *arg = 0;
50 struct type *type;
52 ULONGEST saved_sp;
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));
59 /* The first eight words of ther arguments are passed in registers.
60 Copy them appropriately. */
61 ii = 0;
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)
69 regcache_raw_write_unsigned (regcache, tdep->ppc_gp0_regnum + 3,
70 struct_addr);
71 ii++;
74 /* Effectively indirect call... gcc does...
76 return_val example( float, int);
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. */
91 for (argno = 0, argbytes = 0; argno < nargs && ii < 8; ++ii)
93 int reg_size = register_size (gdbarch, ii + 3);
95 arg = args[argno];
96 type = check_typedef (arg->type ());
97 len = type->length ();
99 if (type->code () == TYPE_CODE_FLT)
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.
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);
112 gdb_assert (len <= 8);
114 target_float_convert (arg->contents ().data (), type, reg_val,
115 reg_type);
116 regcache->cooked_write (fp_regnum, reg_val);
117 ++f_argno;
120 if (len > reg_size)
123 /* Argument takes more than one register. */
124 while (argbytes < len)
126 gdb_byte word[PPC_MAX_REGISTER_SIZE];
127 memset (word, 0, reg_size);
128 memcpy (word,
129 ((char *) arg->contents ().data ()) + argbytes,
130 (len - argbytes) > reg_size
131 ? reg_size : len - argbytes);
132 regcache->cooked_write (tdep->ppc_gp0_regnum + 3 + ii, word);
133 ++ii, argbytes += reg_size;
135 if (ii >= 8)
136 goto ran_out_of_registers_for_arguments;
138 argbytes = 0;
139 --ii;
141 else
143 /* Argument can fit in one register. No problem. */
144 gdb_byte word[PPC_MAX_REGISTER_SIZE];
146 memset (word, 0, reg_size);
147 memcpy (word, arg->contents ().data (), len);
148 regcache->cooked_write (tdep->ppc_gp0_regnum + 3 +ii, word);
150 ++argno;
153 ran_out_of_registers_for_arguments:
155 regcache_cooked_read_unsigned (regcache,
156 gdbarch_sp_regnum (gdbarch),
157 &saved_sp);
159 /* Location for 8 parameters are always reserved. */
160 sp -= wordsize * 8;
162 /* Another six words for back chain, TOC register, link register, etc. */
163 sp -= wordsize * 6;
165 /* Stack pointer must be quadword aligned. */
166 sp = align_down (sp, 16);
168 /* If there are more arguments, allocate space for them in
169 the stack, then push them starting from the ninth one. */
171 if ((argno < nargs) || argbytes)
173 int space = 0, jj;
175 if (argbytes)
177 space += align_up (len - argbytes, 4);
178 jj = argno + 1;
180 else
181 jj = argno;
183 for (; jj < nargs; ++jj)
185 struct value *val = args[jj];
187 space += align_up (val->type ()->length (), 4);
190 /* Add location required for the rest of the parameters. */
191 space = align_up (space, 16);
192 sp -= space;
194 /* This is another instance we need to be concerned about
195 securing our stack space. If we write anything underneath %sp
196 (r1), we might conflict with the kernel who thinks he is free
197 to use this area. So, update %sp first before doing anything
198 else. */
200 regcache_raw_write_signed (regcache,
201 gdbarch_sp_regnum (gdbarch), sp);
203 /* If the last argument copied into the registers didn't fit there
204 completely, push the rest of it into stack. */
206 if (argbytes)
208 write_memory (sp + 24 + (ii * 4),
209 arg->contents ().data () + argbytes,
210 len - argbytes);
211 ++argno;
212 ii += align_up (len - argbytes, 4) / 4;
215 /* Push the rest of the arguments into stack. */
216 for (; argno < nargs; ++argno)
219 arg = args[argno];
220 type = check_typedef (arg->type ());
221 len = type->length ();
224 /* Float types should be passed in fpr's, as well as in the
225 stack. */
226 if (type->code () == TYPE_CODE_FLT && f_argno < 13)
229 gdb_assert (len <= 8);
231 regcache->cooked_write (tdep->ppc_fp0_regnum + 1 + f_argno,
232 arg->contents ().data ());
233 ++f_argno;
236 write_memory (sp + 24 + (ii * 4), arg->contents ().data (), len);
237 ii += align_up (len, 4) / 4;
241 /* Set the stack pointer. According to the ABI, the SP is meant to
242 be set _before_ the corresponding stack space is used. On AIX,
243 this even applies when the target has been completely stopped!
244 Not doing this can lead to conflicts with the kernel which thinks
245 that it still has control over this not-yet-allocated stack
246 region. */
247 regcache_raw_write_signed (regcache, gdbarch_sp_regnum (gdbarch), sp);
249 /* Set back chain properly. */
250 store_unsigned_integer (tmp_buffer, wordsize, byte_order, saved_sp);
251 write_memory (sp, tmp_buffer, wordsize);
253 /* Point the inferior function call's return address at the dummy's
254 breakpoint. */
255 regcache_raw_write_signed (regcache, tdep->ppc_lr_regnum, bp_addr);
257 target_store_registers (regcache, -1);
258 return sp;
261 /* Implement the "return_value" gdbarch method. */
263 static enum return_value_convention
264 rs6000_lynx178_return_value (struct gdbarch *gdbarch, struct value *function,
265 struct type *valtype, struct regcache *regcache,
266 gdb_byte *readbuf, const gdb_byte *writebuf)
268 ppc_gdbarch_tdep *tdep = gdbarch_tdep<ppc_gdbarch_tdep> (gdbarch);
269 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
271 /* The calling convention this function implements assumes the
272 processor has floating-point registers. We shouldn't be using it
273 on PowerPC variants that lack them. */
274 gdb_assert (ppc_floating_point_unit_p (gdbarch));
276 /* AltiVec extension: Functions that declare a vector data type as a
277 return value place that return value in VR2. */
278 if (valtype->code () == TYPE_CODE_ARRAY && valtype->is_vector ()
279 && valtype->length () == 16)
281 if (readbuf)
282 regcache->cooked_read (tdep->ppc_vr0_regnum + 2, readbuf);
283 if (writebuf)
284 regcache->cooked_write (tdep->ppc_vr0_regnum + 2, writebuf);
286 return RETURN_VALUE_REGISTER_CONVENTION;
289 /* If the called subprogram returns an aggregate, there exists an
290 implicit first argument, whose value is the address of a caller-
291 allocated buffer into which the callee is assumed to store its
292 return value. All explicit parameters are appropriately
293 relabeled. */
294 if (valtype->code () == TYPE_CODE_STRUCT
295 || valtype->code () == TYPE_CODE_UNION
296 || valtype->code () == TYPE_CODE_ARRAY)
297 return RETURN_VALUE_STRUCT_CONVENTION;
299 /* Scalar floating-point values are returned in FPR1 for float or
300 double, and in FPR1:FPR2 for quadword precision. Fortran
301 complex*8 and complex*16 are returned in FPR1:FPR2, and
302 complex*32 is returned in FPR1:FPR4. */
303 if (valtype->code () == TYPE_CODE_FLT
304 && (valtype->length () == 4 || valtype->length () == 8))
306 struct type *regtype = register_type (gdbarch, tdep->ppc_fp0_regnum);
307 gdb_byte regval[8];
309 /* FIXME: kettenis/2007-01-01: Add support for quadword
310 precision and complex. */
312 if (readbuf)
314 regcache->cooked_read (tdep->ppc_fp0_regnum + 1, regval);
315 target_float_convert (regval, regtype, readbuf, valtype);
317 if (writebuf)
319 target_float_convert (writebuf, valtype, regval, regtype);
320 regcache->cooked_write (tdep->ppc_fp0_regnum + 1, regval);
323 return RETURN_VALUE_REGISTER_CONVENTION;
326 /* Values of the types int, long, short, pointer, and char (length
327 is less than or equal to four bytes), as well as bit values of
328 lengths less than or equal to 32 bits, must be returned right
329 justified in GPR3 with signed values sign extended and unsigned
330 values zero extended, as necessary. */
331 if (valtype->length () <= tdep->wordsize)
333 if (readbuf)
335 ULONGEST regval;
337 /* For reading we don't have to worry about sign extension. */
338 regcache_cooked_read_unsigned (regcache, tdep->ppc_gp0_regnum + 3,
339 &regval);
340 store_unsigned_integer (readbuf, valtype->length (), byte_order,
341 regval);
343 if (writebuf)
345 /* For writing, use unpack_long since that should handle any
346 required sign extension. */
347 regcache_cooked_write_unsigned (regcache, tdep->ppc_gp0_regnum + 3,
348 unpack_long (valtype, writebuf));
351 return RETURN_VALUE_REGISTER_CONVENTION;
354 /* Eight-byte non-floating-point scalar values must be returned in
355 GPR3:GPR4. */
357 if (valtype->length () == 8)
359 gdb_assert (valtype->code () != TYPE_CODE_FLT);
360 gdb_assert (tdep->wordsize == 4);
362 if (readbuf)
364 gdb_byte regval[8];
366 regcache->cooked_read (tdep->ppc_gp0_regnum + 3, regval);
367 regcache->cooked_read (tdep->ppc_gp0_regnum + 4, regval + 4);
368 memcpy (readbuf, regval, 8);
370 if (writebuf)
372 regcache->cooked_write (tdep->ppc_gp0_regnum + 3, writebuf);
373 regcache->cooked_write (tdep->ppc_gp0_regnum + 4, writebuf + 4);
376 return RETURN_VALUE_REGISTER_CONVENTION;
379 return RETURN_VALUE_STRUCT_CONVENTION;
382 /* PowerPC Lynx178 OSABI sniffer. */
384 static enum gdb_osabi
385 rs6000_lynx178_osabi_sniffer (bfd *abfd)
387 if (bfd_get_flavour (abfd) != bfd_target_xcoff_flavour)
388 return GDB_OSABI_UNKNOWN;
390 /* The only noticeable difference between Lynx178 XCOFF files and
391 AIX XCOFF files comes from the fact that there are no shared
392 libraries on Lynx178. So if the number of import files is
393 different from zero, it cannot be a Lynx178 binary. */
394 if (xcoff_get_n_import_files (abfd) != 0)
395 return GDB_OSABI_UNKNOWN;
397 return GDB_OSABI_LYNXOS178;
400 /* Callback for powerpc-lynx178 initialization. */
402 static void
403 rs6000_lynx178_init_osabi (struct gdbarch_info info, struct gdbarch *gdbarch)
405 set_gdbarch_push_dummy_call (gdbarch, rs6000_lynx178_push_dummy_call);
406 set_gdbarch_return_value (gdbarch, rs6000_lynx178_return_value);
407 set_gdbarch_long_double_bit (gdbarch, 8 * TARGET_CHAR_BIT);
410 void _initialize_rs6000_lynx178_tdep ();
411 void
412 _initialize_rs6000_lynx178_tdep ()
414 gdbarch_register_osabi_sniffer (bfd_arch_rs6000,
415 bfd_target_xcoff_flavour,
416 rs6000_lynx178_osabi_sniffer);
417 gdbarch_register_osabi (bfd_arch_rs6000, 0, GDB_OSABI_LYNXOS178,
418 rs6000_lynx178_init_osabi);