]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/rs6000-lynx178-tdep.c
Update copyright year range in header of all files managed by GDB
[thirdparty/binutils-gdb.git] / gdb / rs6000-lynx178-tdep.c
CommitLineData
213516ef 1/* Copyright (C) 2012-2023 Free Software Foundation, Inc.
d5367fe1
JB
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"
3b2ca824 25#include "target-float.h"
d5367fe1
JB
26#include "value.h"
27#include "xcoffread.h"
28
29/* Implement the "push_dummy_call" gdbarch method. */
30
31static CORE_ADDR
32rs6000_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,
cf84fa6b
AH
36 function_call_return_method return_method,
37 CORE_ADDR struct_addr)
d5367fe1 38{
08106042 39 ppc_gdbarch_tdep *tdep = gdbarch_tdep<ppc_gdbarch_tdep> (gdbarch);
d5367fe1
JB
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 */
345bd07c 47 int wordsize = tdep->wordsize;
d5367fe1
JB
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. */
cf84fa6b 67 if (return_method == return_method_struct)
d5367fe1
JB
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));
df86565b 97 len = type->length ();
d5367fe1 98
78134374 99 if (type->code () == TYPE_CODE_FLT)
d5367fe1
JB
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
36d1c68c
JB
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;
0f068fb5 109 gdb_byte reg_val[PPC_MAX_REGISTER_SIZE];
36d1c68c 110 struct type *reg_type = register_type (gdbarch, fp_regnum);
d5367fe1
JB
111
112 gdb_assert (len <= 8);
113
50888e42
SM
114 target_float_convert (value_contents (arg).data (), type, reg_val,
115 reg_type);
b66f5587 116 regcache->cooked_write (fp_regnum, reg_val);
d5367fe1
JB
117 ++f_argno;
118 }
119
120 if (len > reg_size)
121 {
122
123 /* Argument takes more than one register. */
124 while (argbytes < len)
125 {
0f068fb5 126 gdb_byte word[PPC_MAX_REGISTER_SIZE];
d5367fe1
JB
127 memset (word, 0, reg_size);
128 memcpy (word,
50888e42 129 ((char *) value_contents (arg).data ()) + argbytes,
d5367fe1 130 (len - argbytes) > reg_size
dda83cd7 131 ? reg_size : len - argbytes);
b66f5587 132 regcache->cooked_write (tdep->ppc_gp0_regnum + 3 + ii, word);
d5367fe1
JB
133 ++ii, argbytes += reg_size;
134
135 if (ii >= 8)
136 goto ran_out_of_registers_for_arguments;
137 }
138 argbytes = 0;
139 --ii;
140 }
141 else
142 {
143 /* Argument can fit in one register. No problem. */
0f068fb5 144 gdb_byte word[PPC_MAX_REGISTER_SIZE];
d5367fe1
JB
145
146 memset (word, 0, reg_size);
50888e42 147 memcpy (word, value_contents (arg).data (), len);
b66f5587 148 regcache->cooked_write (tdep->ppc_gp0_regnum + 3 +ii, word);
d5367fe1
JB
149 }
150 ++argno;
151 }
152
153ran_out_of_registers_for_arguments:
154
155 regcache_cooked_read_unsigned (regcache,
156 gdbarch_sp_regnum (gdbarch),
157 &saved_sp);
158
159 /* Location for 8 parameters are always reserved. */
160 sp -= wordsize * 8;
161
162 /* Another six words for back chain, TOC register, link register, etc. */
163 sp -= wordsize * 6;
164
165 /* Stack pointer must be quadword aligned. */
166 sp = align_down (sp, 16);
167
168 /* If there are more arguments, allocate space for them in
169 the stack, then push them starting from the ninth one. */
170
171 if ((argno < nargs) || argbytes)
172 {
173 int space = 0, jj;
174
175 if (argbytes)
176 {
177 space += align_up (len - argbytes, 4);
178 jj = argno + 1;
179 }
180 else
181 jj = argno;
182
183 for (; jj < nargs; ++jj)
184 {
185 struct value *val = args[jj];
186
df86565b 187 space += align_up (value_type (val)->length (), 4);
d5367fe1
JB
188 }
189
190 /* Add location required for the rest of the parameters. */
191 space = align_up (space, 16);
192 sp -= space;
193
194 /* This is another instance we need to be concerned about
dda83cd7
SM
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. */
d5367fe1
JB
199
200 regcache_raw_write_signed (regcache,
201 gdbarch_sp_regnum (gdbarch), sp);
202
203 /* If the last argument copied into the registers didn't fit there
dda83cd7 204 completely, push the rest of it into stack. */
d5367fe1
JB
205
206 if (argbytes)
207 {
208 write_memory (sp + 24 + (ii * 4),
50888e42 209 value_contents (arg).data () + argbytes,
d5367fe1
JB
210 len - argbytes);
211 ++argno;
212 ii += align_up (len - argbytes, 4) / 4;
213 }
214
215 /* Push the rest of the arguments into stack. */
216 for (; argno < nargs; ++argno)
217 {
218
219 arg = args[argno];
220 type = check_typedef (value_type (arg));
df86565b 221 len = type->length ();
d5367fe1
JB
222
223
224 /* Float types should be passed in fpr's, as well as in the
dda83cd7 225 stack. */
78134374 226 if (type->code () == TYPE_CODE_FLT && f_argno < 13)
d5367fe1
JB
227 {
228
229 gdb_assert (len <= 8);
230
b66f5587 231 regcache->cooked_write (tdep->ppc_fp0_regnum + 1 + f_argno,
50888e42 232 value_contents (arg).data ());
d5367fe1
JB
233 ++f_argno;
234 }
235
50888e42 236 write_memory (sp + 24 + (ii * 4), value_contents (arg).data (), len);
d5367fe1
JB
237 ii += align_up (len, 4) / 4;
238 }
239 }
240
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);
248
249 /* Set back chain properly. */
250 store_unsigned_integer (tmp_buffer, wordsize, byte_order, saved_sp);
251 write_memory (sp, tmp_buffer, wordsize);
252
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);
256
257 target_store_registers (regcache, -1);
258 return sp;
259}
260
261/* Implement the "return_value" gdbarch method. */
262
263static enum return_value_convention
264rs6000_lynx178_return_value (struct gdbarch *gdbarch, struct value *function,
265 struct type *valtype, struct regcache *regcache,
266 gdb_byte *readbuf, const gdb_byte *writebuf)
267{
08106042 268 ppc_gdbarch_tdep *tdep = gdbarch_tdep<ppc_gdbarch_tdep> (gdbarch);
d5367fe1
JB
269 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
270
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));
275
276 /* AltiVec extension: Functions that declare a vector data type as a
277 return value place that return value in VR2. */
bd63c870 278 if (valtype->code () == TYPE_CODE_ARRAY && valtype->is_vector ()
df86565b 279 && valtype->length () == 16)
d5367fe1
JB
280 {
281 if (readbuf)
dca08e1f 282 regcache->cooked_read (tdep->ppc_vr0_regnum + 2, readbuf);
d5367fe1 283 if (writebuf)
b66f5587 284 regcache->cooked_write (tdep->ppc_vr0_regnum + 2, writebuf);
d5367fe1
JB
285
286 return RETURN_VALUE_REGISTER_CONVENTION;
287 }
288
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. */
78134374
SM
294 if (valtype->code () == TYPE_CODE_STRUCT
295 || valtype->code () == TYPE_CODE_UNION
296 || valtype->code () == TYPE_CODE_ARRAY)
d5367fe1
JB
297 return RETURN_VALUE_STRUCT_CONVENTION;
298
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. */
78134374 303 if (valtype->code () == TYPE_CODE_FLT
df86565b 304 && (valtype->length () == 4 || valtype->length () == 8))
d5367fe1
JB
305 {
306 struct type *regtype = register_type (gdbarch, tdep->ppc_fp0_regnum);
307 gdb_byte regval[8];
308
309 /* FIXME: kettenis/2007-01-01: Add support for quadword
310 precision and complex. */
311
312 if (readbuf)
313 {
dca08e1f 314 regcache->cooked_read (tdep->ppc_fp0_regnum + 1, regval);
3b2ca824 315 target_float_convert (regval, regtype, readbuf, valtype);
d5367fe1
JB
316 }
317 if (writebuf)
318 {
3b2ca824 319 target_float_convert (writebuf, valtype, regval, regtype);
b66f5587 320 regcache->cooked_write (tdep->ppc_fp0_regnum + 1, regval);
d5367fe1
JB
321 }
322
323 return RETURN_VALUE_REGISTER_CONVENTION;
324 }
325
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. */
df86565b 331 if (valtype->length () <= tdep->wordsize)
d5367fe1
JB
332 {
333 if (readbuf)
334 {
335 ULONGEST regval;
336
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);
df86565b 340 store_unsigned_integer (readbuf, valtype->length (), byte_order,
d5367fe1
JB
341 regval);
342 }
343 if (writebuf)
344 {
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));
349 }
350
351 return RETURN_VALUE_REGISTER_CONVENTION;
352 }
353
354 /* Eight-byte non-floating-point scalar values must be returned in
355 GPR3:GPR4. */
356
df86565b 357 if (valtype->length () == 8)
d5367fe1 358 {
78134374 359 gdb_assert (valtype->code () != TYPE_CODE_FLT);
d5367fe1
JB
360 gdb_assert (tdep->wordsize == 4);
361
362 if (readbuf)
363 {
364 gdb_byte regval[8];
365
dca08e1f
SM
366 regcache->cooked_read (tdep->ppc_gp0_regnum + 3, regval);
367 regcache->cooked_read (tdep->ppc_gp0_regnum + 4, regval + 4);
d5367fe1
JB
368 memcpy (readbuf, regval, 8);
369 }
370 if (writebuf)
371 {
b66f5587
SM
372 regcache->cooked_write (tdep->ppc_gp0_regnum + 3, writebuf);
373 regcache->cooked_write (tdep->ppc_gp0_regnum + 4, writebuf + 4);
d5367fe1
JB
374 }
375
376 return RETURN_VALUE_REGISTER_CONVENTION;
377 }
378
379 return RETURN_VALUE_STRUCT_CONVENTION;
380}
381
382/* PowerPC Lynx178 OSABI sniffer. */
383
384static enum gdb_osabi
385rs6000_lynx178_osabi_sniffer (bfd *abfd)
386{
387 if (bfd_get_flavour (abfd) != bfd_target_xcoff_flavour)
388 return GDB_OSABI_UNKNOWN;
389
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;
396
397 return GDB_OSABI_LYNXOS178;
398}
399
400/* Callback for powerpc-lynx178 initialization. */
401
402static void
403rs6000_lynx178_init_osabi (struct gdbarch_info info, struct gdbarch *gdbarch)
404{
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);
408}
409
6c265988 410void _initialize_rs6000_lynx178_tdep ();
d5367fe1 411void
6c265988 412_initialize_rs6000_lynx178_tdep ()
d5367fe1
JB
413{
414 gdbarch_register_osabi_sniffer (bfd_arch_rs6000,
dda83cd7
SM
415 bfd_target_xcoff_flavour,
416 rs6000_lynx178_osabi_sniffer);
d5367fe1 417 gdbarch_register_osabi (bfd_arch_rs6000, 0, GDB_OSABI_LYNXOS178,
dda83cd7 418 rs6000_lynx178_init_osabi);
d5367fe1
JB
419}
420