Remove path name from test case
[binutils-gdb.git] / gdb / rs6000-lynx178-tdep.c
1 /* Copyright (C) 2012-2023 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 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;
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 (arg->type ());
97 len = type->length ();
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 (arg->contents ().data (), type, reg_val,
115 reg_type);
116 regcache->cooked_write (fp_regnum, reg_val);
117 ++f_argno;
118 }
119
120 if (len > reg_size)
121 {
122
123 /* Argument takes more than one register. */
124 while (argbytes < len)
125 {
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;
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. */
144 gdb_byte word[PPC_MAX_REGISTER_SIZE];
145
146 memset (word, 0, reg_size);
147 memcpy (word, arg->contents ().data (), len);
148 regcache->cooked_write (tdep->ppc_gp0_regnum + 3 +ii, word);
149 }
150 ++argno;
151 }
152
153 ran_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
187 space += align_up (val->type ()->length (), 4);
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
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. */
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
204 completely, push the rest of it into stack. */
205
206 if (argbytes)
207 {
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;
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 (arg->type ());
221 len = type->length ();
222
223
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)
227 {
228
229 gdb_assert (len <= 8);
230
231 regcache->cooked_write (tdep->ppc_fp0_regnum + 1 + f_argno,
232 arg->contents ().data ());
233 ++f_argno;
234 }
235
236 write_memory (sp + 24 + (ii * 4), arg->contents ().data (), len);
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
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)
267 {
268 ppc_gdbarch_tdep *tdep = gdbarch_tdep<ppc_gdbarch_tdep> (gdbarch);
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. */
278 if (valtype->code () == TYPE_CODE_ARRAY && valtype->is_vector ()
279 && valtype->length () == 16)
280 {
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);
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. */
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;
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. */
303 if (valtype->code () == TYPE_CODE_FLT
304 && (valtype->length () == 4 || valtype->length () == 8))
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 {
314 regcache->cooked_read (tdep->ppc_fp0_regnum + 1, regval);
315 target_float_convert (regval, regtype, readbuf, valtype);
316 }
317 if (writebuf)
318 {
319 target_float_convert (writebuf, valtype, regval, regtype);
320 regcache->cooked_write (tdep->ppc_fp0_regnum + 1, regval);
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. */
331 if (valtype->length () <= tdep->wordsize)
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);
340 store_unsigned_integer (readbuf, valtype->length (), byte_order,
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
357 if (valtype->length () == 8)
358 {
359 gdb_assert (valtype->code () != TYPE_CODE_FLT);
360 gdb_assert (tdep->wordsize == 4);
361
362 if (readbuf)
363 {
364 gdb_byte regval[8];
365
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);
369 }
370 if (writebuf)
371 {
372 regcache->cooked_write (tdep->ppc_gp0_regnum + 3, writebuf);
373 regcache->cooked_write (tdep->ppc_gp0_regnum + 4, writebuf + 4);
374 }
375
376 return RETURN_VALUE_REGISTER_CONVENTION;
377 }
378
379 return RETURN_VALUE_STRUCT_CONVENTION;
380 }
381
382 /* PowerPC Lynx178 OSABI sniffer. */
383
384 static enum gdb_osabi
385 rs6000_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
402 static void
403 rs6000_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
410 void _initialize_rs6000_lynx178_tdep ();
411 void
412 _initialize_rs6000_lynx178_tdep ()
413 {
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);
419 }
420