1 /* Target-dependent code for PowerPC systems using the SVR4 ABI
2    for GDB, the GNU debugger.
3 
4    Copyright (C) 2000-2024 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 3 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, see <http://www.gnu.org/licenses/>.  */
20 
21 #include "extract-store-integer.h"
22 #include "language.h"
23 #include "gdbcore.h"
24 #include "inferior.h"
25 #include "regcache.h"
26 #include "value.h"
27 #include "ppc-tdep.h"
28 #include "target.h"
29 #include "objfiles.h"
30 #include "infcall.h"
31 #include "dwarf2.h"
32 #include "dwarf2/loc.h"
33 #include "target-float.h"
34 #include <algorithm>
35 
36 
37 /* Check whether FTPYE is a (pointer to) function type that should use
38    the OpenCL vector ABI.  */
39 
40 static int
ppc_sysv_use_opencl_abi(struct type * ftype)41 ppc_sysv_use_opencl_abi (struct type *ftype)
42 {
43   ftype = check_typedef (ftype);
44 
45   if (ftype->code () == TYPE_CODE_PTR)
46     ftype = check_typedef (ftype->target_type ());
47 
48   return (ftype->code () == TYPE_CODE_FUNC
49             && TYPE_CALLING_CONVENTION (ftype) == DW_CC_GDB_IBM_OpenCL);
50 }
51 
52 /* Pass the arguments in either registers, or in the stack.  Using the
53    ppc sysv ABI, the first eight words of the argument list (that might
54    be less than eight parameters if some parameters occupy more than one
55    word) are passed in r3..r10 registers.  float and double parameters are
56    passed in fpr's, in addition to that.  Rest of the parameters if any
57    are passed in user stack.
58 
59    If the function is returning a structure, then the return address is passed
60    in r3, then the first 7 words of the parameters can be passed in registers,
61    starting from r4.  */
62 
63 CORE_ADDR
ppc_sysv_abi_push_dummy_call(struct gdbarch * gdbarch,struct value * function,struct regcache * regcache,CORE_ADDR bp_addr,int nargs,struct value ** args,CORE_ADDR sp,function_call_return_method return_method,CORE_ADDR struct_addr)64 ppc_sysv_abi_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
65                                     struct regcache *regcache, CORE_ADDR bp_addr,
66                                     int nargs, struct value **args, CORE_ADDR sp,
67                                     function_call_return_method return_method,
68                                     CORE_ADDR struct_addr)
69 {
70   ppc_gdbarch_tdep *tdep = gdbarch_tdep<ppc_gdbarch_tdep> (gdbarch);
71   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
72   int opencl_abi = ppc_sysv_use_opencl_abi (function->type ());
73   ULONGEST saved_sp;
74   int argspace = 0;           /* 0 is an initial wrong guess.  */
75   int write_pass;
76 
77   gdb_assert (tdep->wordsize == 4);
78 
79   regcache_cooked_read_unsigned (regcache, gdbarch_sp_regnum (gdbarch),
80                                          &saved_sp);
81 
82   /* Go through the argument list twice.
83 
84      Pass 1: Figure out how much new stack space is required for
85      arguments and pushed values.  Unlike the PowerOpen ABI, the SysV
86      ABI doesn't reserve any extra space for parameters which are put
87      in registers, but does always push structures and then pass their
88      address.
89 
90      Pass 2: Replay the same computation but this time also write the
91      values out to the target.  */
92 
93   for (write_pass = 0; write_pass < 2; write_pass++)
94     {
95       int argno;
96       /* Next available floating point register for float and double
97            arguments.  */
98       int freg = 1;
99       /* Next available general register for non-float, non-vector
100            arguments.  */
101       int greg = 3;
102       /* Next available vector register for vector arguments.  */
103       int vreg = 2;
104       /* Arguments start above the "LR save word" and "Back chain".  */
105       int argoffset = 2 * tdep->wordsize;
106       /* Structures start after the arguments.  */
107       int structoffset = argoffset + argspace;
108 
109       /* If the function is returning a `struct', then the first word
110            (which will be passed in r3) is used for struct return
111            address.  In that case we should advance one word and start
112            from r4 register to copy parameters.  */
113       if (return_method == return_method_struct)
114           {
115             if (write_pass)
116               regcache_cooked_write_signed (regcache,
117                                                     tdep->ppc_gp0_regnum + greg,
118                                                     struct_addr);
119             greg++;
120           }
121 
122       for (argno = 0; argno < nargs; argno++)
123           {
124             struct value *arg = args[argno];
125             struct type *type = check_typedef (arg->type ());
126             int len = type->length ();
127             const bfd_byte *val = arg->contents ().data ();
128 
129             if (type->code () == TYPE_CODE_FLT && len <= 8
130                 && !tdep->soft_float)
131               {
132                 /* Floating point value converted to "double" then
133                      passed in an FP register, when the registers run out,
134                      8 byte aligned stack is used.  */
135                 if (freg <= 8)
136                     {
137                       if (write_pass)
138                         {
139                           /* Always store the floating point value using
140                                the register's floating-point format.  */
141                           gdb_byte regval[PPC_MAX_REGISTER_SIZE];
142                           struct type *regtype
143                               = register_type (gdbarch, tdep->ppc_fp0_regnum + freg);
144                           target_float_convert (val, type, regval, regtype);
145                           regcache->cooked_write (tdep->ppc_fp0_regnum + freg,
146                                                         regval);
147                         }
148                       freg++;
149                     }
150                 else
151                     {
152                       /* The SysV ABI tells us to convert floats to
153                          doubles before writing them to an 8 byte aligned
154                          stack location.  Unfortunately GCC does not do
155                          that, and stores floats into 4 byte aligned
156                          locations without converting them to doubles.
157                          Since there is no know compiler that actually
158                          follows the ABI here, we implement the GCC
159                          convention.  */
160 
161                       /* Align to 4 bytes or 8 bytes depending on the type of
162                          the argument (float or double).  */
163                       argoffset = align_up (argoffset, len);
164                       if (write_pass)
165                           write_memory (sp + argoffset, val, len);
166                       argoffset += len;
167                     }
168               }
169             else if (type->code () == TYPE_CODE_FLT
170                        && len == 16
171                        && !tdep->soft_float
172                        && (gdbarch_long_double_format (gdbarch)
173                            == floatformats_ibm_long_double))
174               {
175                 /* IBM long double passed in two FP registers if
176                      available, otherwise 8-byte aligned stack.  */
177                 if (freg <= 7)
178                     {
179                       if (write_pass)
180                         {
181                           regcache->cooked_write (tdep->ppc_fp0_regnum + freg, val);
182                           regcache->cooked_write (tdep->ppc_fp0_regnum + freg + 1,
183                                                         val + 8);
184                         }
185                       freg += 2;
186                     }
187                 else
188                     {
189                       argoffset = align_up (argoffset, 8);
190                       if (write_pass)
191                         write_memory (sp + argoffset, val, len);
192                       argoffset += 16;
193                     }
194               }
195             else if (len == 8
196                        && (type->code () == TYPE_CODE_INT   /* long long */
197                            || type->code () == TYPE_CODE_FLT          /* double */
198                            || (type->code () == TYPE_CODE_DECFLOAT
199                                  && tdep->soft_float)))
200               {
201                 /* "long long" or soft-float "double" or "_Decimal64"
202                      passed in an odd/even register pair with the low
203                      addressed word in the odd register and the high
204                      addressed word in the even register, or when the
205                      registers run out an 8 byte aligned stack
206                      location.  */
207                 if (greg > 9)
208                     {
209                       /* Just in case GREG was 10.  */
210                       greg = 11;
211                       argoffset = align_up (argoffset, 8);
212                       if (write_pass)
213                         write_memory (sp + argoffset, val, len);
214                       argoffset += 8;
215                     }
216                 else
217                     {
218                       /* Must start on an odd register - r3/r4 etc.  */
219                       if ((greg & 1) == 0)
220                         greg++;
221                       if (write_pass)
222                         {
223                           regcache->cooked_write (tdep->ppc_gp0_regnum + greg + 0,
224                                                         val + 0);
225                           regcache->cooked_write (tdep->ppc_gp0_regnum + greg + 1,
226                                                         val + 4);
227                         }
228                       greg += 2;
229                     }
230               }
231             else if (len == 16
232                        && ((type->code () == TYPE_CODE_FLT
233                               && (gdbarch_long_double_format (gdbarch)
234                                   == floatformats_ibm_long_double))
235                            || (type->code () == TYPE_CODE_DECFLOAT
236                                  && tdep->soft_float)))
237               {
238                 /* Soft-float IBM long double or _Decimal128 passed in
239                      four consecutive registers, or on the stack.  The
240                      registers are not necessarily odd/even pairs.  */
241                 if (greg > 7)
242                     {
243                       greg = 11;
244                       argoffset = align_up (argoffset, 8);
245                       if (write_pass)
246                         write_memory (sp + argoffset, val, len);
247                       argoffset += 16;
248                     }
249                 else
250                     {
251                       if (write_pass)
252                         {
253                           regcache->cooked_write (tdep->ppc_gp0_regnum + greg + 0,
254                                                         val + 0);
255                           regcache->cooked_write (tdep->ppc_gp0_regnum + greg + 1,
256                                                         val + 4);
257                           regcache->cooked_write (tdep->ppc_gp0_regnum + greg + 2,
258                                                         val + 8);
259                           regcache->cooked_write (tdep->ppc_gp0_regnum + greg + 3,
260                                                         val + 12);
261                         }
262                       greg += 4;
263                     }
264               }
265             else if (type->code () == TYPE_CODE_DECFLOAT && len <= 8
266                        && !tdep->soft_float)
267               {
268                 /* 32-bit and 64-bit decimal floats go in f1 .. f8.  They can
269                      end up in memory.  */
270 
271                 if (freg <= 8)
272                     {
273                       if (write_pass)
274                         {
275                           gdb_byte regval[PPC_MAX_REGISTER_SIZE];
276                           const gdb_byte *p;
277 
278                           /* 32-bit decimal floats are right aligned in the
279                                doubleword.  */
280                           if (type->length () == 4)
281                               {
282                                 memcpy (regval + 4, val, 4);
283                                 p = regval;
284                               }
285                           else
286                               p = val;
287 
288                           regcache->cooked_write (tdep->ppc_fp0_regnum + freg, p);
289                         }
290 
291                       freg++;
292                     }
293                 else
294                     {
295                       argoffset = align_up (argoffset, len);
296 
297                       if (write_pass)
298                         /* Write value in the stack's parameter save area.  */
299                         write_memory (sp + argoffset, val, len);
300 
301                       argoffset += len;
302                     }
303               }
304             else if (type->code () == TYPE_CODE_DECFLOAT && len == 16
305                        && !tdep->soft_float)
306               {
307                 /* 128-bit decimal floats go in f2 .. f7, always in even/odd
308                      pairs.  They can end up in memory, using two doublewords.  */
309 
310                 if (freg <= 6)
311                     {
312                       /* Make sure freg is even.  */
313                       freg += freg & 1;
314 
315                       if (write_pass)
316                         {
317                           regcache->cooked_write (tdep->ppc_fp0_regnum + freg, val);
318                           regcache->cooked_write (tdep->ppc_fp0_regnum + freg + 1,
319                                                         val + 8);
320                         }
321                     }
322                 else
323                     {
324                       argoffset = align_up (argoffset, 8);
325 
326                       if (write_pass)
327                         write_memory (sp + argoffset, val, 16);
328 
329                       argoffset += 16;
330                     }
331 
332                 /* If a 128-bit decimal float goes to the stack because only f7
333                      and f8 are free (thus there's no even/odd register pair
334                      available), these registers should be marked as occupied.
335                      Hence we increase freg even when writing to memory.  */
336                 freg += 2;
337               }
338             else if (len < 16
339                        && type->code () == TYPE_CODE_ARRAY
340                        && type->is_vector ()
341                        && opencl_abi)
342               {
343                 /* OpenCL vectors shorter than 16 bytes are passed as if
344                      a series of independent scalars.  */
345                 struct type *eltype = check_typedef (type->target_type ());
346                 int i, nelt = type->length () / eltype->length ();
347 
348                 for (i = 0; i < nelt; i++)
349                     {
350                       const gdb_byte *elval = val + i * eltype->length ();
351 
352                       if (eltype->code () == TYPE_CODE_FLT && !tdep->soft_float)
353                         {
354                           if (freg <= 8)
355                               {
356                                 if (write_pass)
357                                   {
358                                     int regnum = tdep->ppc_fp0_regnum + freg;
359                                     gdb_byte regval[PPC_MAX_REGISTER_SIZE];
360                                     struct type *regtype
361                                         = register_type (gdbarch, regnum);
362                                     target_float_convert (elval, eltype,
363                                                                 regval, regtype);
364                                     regcache->cooked_write (regnum, regval);
365                                   }
366                                 freg++;
367                               }
368                           else
369                               {
370                                 argoffset = align_up (argoffset, len);
371                                 if (write_pass)
372                                   write_memory (sp + argoffset, val, len);
373                                 argoffset += len;
374                               }
375                         }
376                       else if (eltype->length () == 8)
377                         {
378                           if (greg > 9)
379                               {
380                                 /* Just in case GREG was 10.  */
381                                 greg = 11;
382                                 argoffset = align_up (argoffset, 8);
383                                 if (write_pass)
384                                   write_memory (sp + argoffset, elval,
385                                                     eltype->length ());
386                                 argoffset += 8;
387                               }
388                           else
389                               {
390                                 /* Must start on an odd register - r3/r4 etc.  */
391                                 if ((greg & 1) == 0)
392                                   greg++;
393                                 if (write_pass)
394                                   {
395                                     int regnum = tdep->ppc_gp0_regnum + greg;
396                                     regcache->cooked_write (regnum + 0, elval + 0);
397                                     regcache->cooked_write (regnum + 1, elval + 4);
398                                   }
399                                 greg += 2;
400                               }
401                         }
402                       else
403                         {
404                           gdb_byte word[PPC_MAX_REGISTER_SIZE];
405                           store_unsigned_integer (word, tdep->wordsize, byte_order,
406                                                         unpack_long (eltype, elval));
407 
408                           if (greg <= 10)
409                               {
410                                 if (write_pass)
411                                   regcache->cooked_write (tdep->ppc_gp0_regnum + greg,
412                                                                 word);
413                                 greg++;
414                               }
415                           else
416                               {
417                                 argoffset = align_up (argoffset, tdep->wordsize);
418                                 if (write_pass)
419                                   write_memory (sp + argoffset, word, tdep->wordsize);
420                                 argoffset += tdep->wordsize;
421                               }
422                         }
423                     }
424               }
425             else if (len >= 16
426                        && type->code () == TYPE_CODE_ARRAY
427                        && type->is_vector ()
428                        && opencl_abi)
429               {
430                 /* OpenCL vectors 16 bytes or longer are passed as if
431                      a series of AltiVec vectors.  */
432                 int i;
433 
434                 for (i = 0; i < len / 16; i++)
435                     {
436                       const gdb_byte *elval = val + i * 16;
437 
438                       if (vreg <= 13)
439                         {
440                           if (write_pass)
441                               regcache->cooked_write (tdep->ppc_vr0_regnum + vreg,
442                                                             elval);
443                           vreg++;
444                         }
445                       else
446                         {
447                           argoffset = align_up (argoffset, 16);
448                           if (write_pass)
449                               write_memory (sp + argoffset, elval, 16);
450                           argoffset += 16;
451                         }
452                     }
453               }
454             else if (len == 16
455                        && ((type->code () == TYPE_CODE_ARRAY
456                               && type->is_vector ()
457                               && tdep->vector_abi == POWERPC_VEC_ALTIVEC)
458                        || (type->code () == TYPE_CODE_FLT
459                            && (gdbarch_long_double_format (gdbarch)
460                                  == floatformats_ieee_quad))))
461               {
462                 /* Vector parameter passed in an Altivec register, or
463                      when that runs out, 16 byte aligned stack location.
464                      IEEE FLOAT 128-bit also passes parameters in vector
465                      registers.  */
466                 if (vreg <= 13)
467                     {
468                       if (write_pass)
469                         regcache->cooked_write (tdep->ppc_vr0_regnum + vreg, val);
470                       vreg++;
471                     }
472                 else
473                     {
474                       argoffset = align_up (argoffset, 16);
475                       if (write_pass)
476                         write_memory (sp + argoffset, val, 16);
477                       argoffset += 16;
478                     }
479               }
480             else if (len == 8
481                        && type->code () == TYPE_CODE_ARRAY
482                        && type->is_vector ()
483                        && tdep->vector_abi == POWERPC_VEC_SPE)
484               {
485                 /* Vector parameter passed in an e500 register, or when
486                      that runs out, 8 byte aligned stack location.  Note
487                      that since e500 vector and general purpose registers
488                      both map onto the same underlying register set, a
489                      "greg" and not a "vreg" is consumed here.  A cooked
490                      write stores the value in the correct locations
491                      within the raw register cache.  */
492                 if (greg <= 10)
493                     {
494                       if (write_pass)
495                         regcache->cooked_write (tdep->ppc_ev0_regnum + greg, val);
496                       greg++;
497                     }
498                 else
499                     {
500                       argoffset = align_up (argoffset, 8);
501                       if (write_pass)
502                         write_memory (sp + argoffset, val, 8);
503                       argoffset += 8;
504                     }
505               }
506             else
507               {
508                 /* Reduce the parameter down to something that fits in a
509                      "word".  */
510                 gdb_byte word[PPC_MAX_REGISTER_SIZE];
511                 memset (word, 0, PPC_MAX_REGISTER_SIZE);
512                 if (len > tdep->wordsize
513                       || type->code () == TYPE_CODE_STRUCT
514                       || type->code () == TYPE_CODE_UNION)
515                     {
516                       /* Structs and large values are put in an
517                          aligned stack slot ...  */
518                       if (type->code () == TYPE_CODE_ARRAY
519                           && type->is_vector ()
520                           && len >= 16)
521                         structoffset = align_up (structoffset, 16);
522                       else
523                         structoffset = align_up (structoffset, 8);
524 
525                       if (write_pass)
526                         write_memory (sp + structoffset, val, len);
527                       /* ... and then a "word" pointing to that address is
528                          passed as the parameter.  */
529                       store_unsigned_integer (word, tdep->wordsize, byte_order,
530                                                     sp + structoffset);
531                       structoffset += len;
532                     }
533                 else if (type->code () == TYPE_CODE_INT)
534                     /* Sign or zero extend the "int" into a "word".  */
535                     store_unsigned_integer (word, tdep->wordsize, byte_order,
536                                                   unpack_long (type, val));
537                 else
538                     /* Always goes in the low address.  */
539                     memcpy (word, val, len);
540                 /* Store that "word" in a register, or on the stack.
541                      The words have "4" byte alignment.  */
542                 if (greg <= 10)
543                     {
544                       if (write_pass)
545                         regcache->cooked_write (tdep->ppc_gp0_regnum + greg, word);
546                       greg++;
547                     }
548                 else
549                     {
550                       argoffset = align_up (argoffset, tdep->wordsize);
551                       if (write_pass)
552                         write_memory (sp + argoffset, word, tdep->wordsize);
553                       argoffset += tdep->wordsize;
554                     }
555               }
556           }
557 
558       /* Compute the actual stack space requirements.  */
559       if (!write_pass)
560           {
561             /* Remember the amount of space needed by the arguments.  */
562             argspace = argoffset;
563             /* Allocate space for both the arguments and the structures.  */
564             sp -= (argoffset + structoffset);
565             /* Ensure that the stack is still 16 byte aligned.  */
566             sp = align_down (sp, 16);
567           }
568 
569       /* The psABI says that "A caller of a function that takes a
570            variable argument list shall set condition register bit 6 to
571            1 if it passes one or more arguments in the floating-point
572            registers.  It is strongly recommended that the caller set the
573            bit to 0 otherwise..."  Doing this for normal functions too
574            shouldn't hurt.  */
575       if (write_pass)
576           {
577             ULONGEST cr;
578 
579             regcache_cooked_read_unsigned (regcache, tdep->ppc_cr_regnum, &cr);
580             if (freg > 1)
581               cr |= 0x02000000;
582             else
583               cr &= ~0x02000000;
584             regcache_cooked_write_unsigned (regcache, tdep->ppc_cr_regnum, cr);
585           }
586     }
587 
588   /* Update %sp.   */
589   regcache_cooked_write_signed (regcache, gdbarch_sp_regnum (gdbarch), sp);
590 
591   /* Write the backchain (it occupies WORDSIZED bytes).  */
592   write_memory_signed_integer (sp, tdep->wordsize, byte_order, saved_sp);
593 
594   /* Point the inferior function call's return address at the dummy's
595      breakpoint.  */
596   regcache_cooked_write_signed (regcache, tdep->ppc_lr_regnum, bp_addr);
597 
598   return sp;
599 }
600 
601 /* Handle the return-value conventions for Decimal Floating Point values.  */
602 static enum return_value_convention
get_decimal_float_return_value(struct gdbarch * gdbarch,struct type * valtype,struct regcache * regcache,gdb_byte * readbuf,const gdb_byte * writebuf)603 get_decimal_float_return_value (struct gdbarch *gdbarch, struct type *valtype,
604                                         struct regcache *regcache, gdb_byte *readbuf,
605                                         const gdb_byte *writebuf)
606 {
607   ppc_gdbarch_tdep *tdep = gdbarch_tdep<ppc_gdbarch_tdep> (gdbarch);
608 
609   gdb_assert (valtype->code () == TYPE_CODE_DECFLOAT);
610 
611   /* 32-bit and 64-bit decimal floats in f1.  */
612   if (valtype->length () <= 8)
613     {
614       if (writebuf != NULL)
615           {
616             gdb_byte regval[PPC_MAX_REGISTER_SIZE];
617             const gdb_byte *p;
618 
619             /* 32-bit decimal float is right aligned in the doubleword.  */
620             if (valtype->length () == 4)
621               {
622                 memcpy (regval + 4, writebuf, 4);
623                 p = regval;
624               }
625             else
626               p = writebuf;
627 
628             regcache->cooked_write (tdep->ppc_fp0_regnum + 1, p);
629           }
630       if (readbuf != NULL)
631           {
632             regcache->cooked_read (tdep->ppc_fp0_regnum + 1, readbuf);
633 
634             /* Left align 32-bit decimal float.  */
635             if (valtype->length () == 4)
636               memcpy (readbuf, readbuf + 4, 4);
637           }
638     }
639   /* 128-bit decimal floats in f2,f3.  */
640   else if (valtype->length () == 16)
641     {
642       if (writebuf != NULL || readbuf != NULL)
643           {
644             int i;
645 
646             for (i = 0; i < 2; i++)
647               {
648                 if (writebuf != NULL)
649                     regcache->cooked_write (tdep->ppc_fp0_regnum + 2 + i,
650                                                   writebuf + i * 8);
651                 if (readbuf != NULL)
652                     regcache->cooked_read (tdep->ppc_fp0_regnum + 2 + i,
653                                                readbuf + i * 8);
654               }
655           }
656     }
657   else
658     /* Can't happen.  */
659     internal_error (_("Unknown decimal float size."));
660 
661   return RETURN_VALUE_REGISTER_CONVENTION;
662 }
663 
664 /* Handle the return-value conventions specified by the SysV 32-bit
665    PowerPC ABI (including all the supplements):
666 
667    no floating-point: floating-point values returned using 32-bit
668    general-purpose registers.
669 
670    Altivec: 128-bit vectors returned using vector registers.
671 
672    e500: 64-bit vectors returned using the full full 64 bit EV
673    register, floating-point values returned using 32-bit
674    general-purpose registers.
675 
676    GCC (broken): Small struct values right (instead of left) aligned
677    when returned in general-purpose registers.  */
678 
679 static enum return_value_convention
do_ppc_sysv_return_value(struct gdbarch * gdbarch,struct type * func_type,struct type * type,struct regcache * regcache,gdb_byte * readbuf,const gdb_byte * writebuf,int broken_gcc)680 do_ppc_sysv_return_value (struct gdbarch *gdbarch, struct type *func_type,
681                                 struct type *type, struct regcache *regcache,
682                                 gdb_byte *readbuf, const gdb_byte *writebuf,
683                                 int broken_gcc)
684 {
685   ppc_gdbarch_tdep *tdep = gdbarch_tdep<ppc_gdbarch_tdep> (gdbarch);
686   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
687   int opencl_abi = func_type? ppc_sysv_use_opencl_abi (func_type) : 0;
688 
689   gdb_assert (tdep->wordsize == 4);
690 
691   if (type->code () == TYPE_CODE_FLT
692       && type->length () <= 8
693       && !tdep->soft_float)
694     {
695       if (readbuf)
696           {
697             /* Floats and doubles stored in "f1".  Convert the value to
698                the required type.  */
699             gdb_byte regval[PPC_MAX_REGISTER_SIZE];
700             struct type *regtype = register_type (gdbarch,
701                                                             tdep->ppc_fp0_regnum + 1);
702             regcache->cooked_read (tdep->ppc_fp0_regnum + 1, regval);
703             target_float_convert (regval, regtype, readbuf, type);
704           }
705       if (writebuf)
706           {
707             /* Floats and doubles stored in "f1".  Convert the value to
708                the register's "double" type.  */
709             gdb_byte regval[PPC_MAX_REGISTER_SIZE];
710             struct type *regtype = register_type (gdbarch, tdep->ppc_fp0_regnum);
711             target_float_convert (writebuf, type, regval, regtype);
712             regcache->cooked_write (tdep->ppc_fp0_regnum + 1, regval);
713           }
714       return RETURN_VALUE_REGISTER_CONVENTION;
715     }
716   if (type->code () == TYPE_CODE_FLT
717       && type->length () == 16
718       && !tdep->soft_float
719       && (gdbarch_long_double_format (gdbarch)
720             == floatformats_ibm_long_double))
721     {
722       /* IBM long double stored in f1 and f2.  */
723       if (readbuf)
724           {
725             regcache->cooked_read (tdep->ppc_fp0_regnum + 1, readbuf);
726             regcache->cooked_read (tdep->ppc_fp0_regnum + 2, readbuf + 8);
727           }
728       if (writebuf)
729           {
730             regcache->cooked_write (tdep->ppc_fp0_regnum + 1, writebuf);
731             regcache->cooked_write (tdep->ppc_fp0_regnum + 2, writebuf + 8);
732           }
733       return RETURN_VALUE_REGISTER_CONVENTION;
734     }
735   if (type->length () == 16
736       && ((type->code () == TYPE_CODE_FLT
737              && (gdbarch_long_double_format (gdbarch)
738                  == floatformats_ibm_long_double))
739             || (type->code () == TYPE_CODE_DECFLOAT && tdep->soft_float)))
740     {
741       /* Soft-float IBM long double or _Decimal128 stored in r3, r4,
742            r5, r6.  */
743       if (readbuf)
744           {
745             regcache->cooked_read (tdep->ppc_gp0_regnum + 3, readbuf);
746             regcache->cooked_read (tdep->ppc_gp0_regnum + 4, readbuf + 4);
747             regcache->cooked_read (tdep->ppc_gp0_regnum + 5, readbuf + 8);
748             regcache->cooked_read (tdep->ppc_gp0_regnum + 6, readbuf + 12);
749           }
750       if (writebuf)
751           {
752             regcache->cooked_write (tdep->ppc_gp0_regnum + 3, writebuf);
753             regcache->cooked_write (tdep->ppc_gp0_regnum + 4, writebuf + 4);
754             regcache->cooked_write (tdep->ppc_gp0_regnum + 5, writebuf + 8);
755             regcache->cooked_write (tdep->ppc_gp0_regnum + 6, writebuf + 12);
756           }
757       return RETURN_VALUE_REGISTER_CONVENTION;
758     }
759   if ((type->code () == TYPE_CODE_INT && type->length () == 8)
760       || (type->code () == TYPE_CODE_FLT && type->length () == 8)
761       || (type->code () == TYPE_CODE_DECFLOAT && type->length () == 8
762             && tdep->soft_float))
763     {
764       if (readbuf)
765           {
766             /* A long long, double or _Decimal64 stored in the 32 bit
767                r3/r4.  */
768             regcache->cooked_read (tdep->ppc_gp0_regnum + 3, readbuf + 0);
769             regcache->cooked_read (tdep->ppc_gp0_regnum + 4, readbuf + 4);
770           }
771       if (writebuf)
772           {
773             /* A long long, double or _Decimal64 stored in the 32 bit
774                r3/r4.  */
775             regcache->cooked_write (tdep->ppc_gp0_regnum + 3, writebuf + 0);
776             regcache->cooked_write (tdep->ppc_gp0_regnum + 4, writebuf + 4);
777           }
778       return RETURN_VALUE_REGISTER_CONVENTION;
779     }
780   if (type->code () == TYPE_CODE_DECFLOAT && !tdep->soft_float)
781     return get_decimal_float_return_value (gdbarch, type, regcache, readbuf,
782                                                      writebuf);
783   else if ((type->code () == TYPE_CODE_INT
784               || type->code () == TYPE_CODE_CHAR
785               || type->code () == TYPE_CODE_BOOL
786               || type->code () == TYPE_CODE_PTR
787               || TYPE_IS_REFERENCE (type)
788               || type->code () == TYPE_CODE_ENUM)
789              && type->length () <= tdep->wordsize)
790     {
791       if (readbuf)
792           {
793             /* Some sort of integer stored in r3.  Since TYPE isn't
794                bigger than the register, sign extension isn't a problem
795                - just do everything unsigned.  */
796             ULONGEST regval;
797             regcache_cooked_read_unsigned (regcache, tdep->ppc_gp0_regnum + 3,
798                                                    &regval);
799             store_unsigned_integer (readbuf, type->length (), byte_order,
800                                           regval);
801           }
802       if (writebuf)
803           {
804             /* Some sort of integer stored in r3.  Use unpack_long since
805                that should handle any required sign extension.  */
806             regcache_cooked_write_unsigned (regcache, tdep->ppc_gp0_regnum + 3,
807                                                     unpack_long (type, writebuf));
808           }
809       return RETURN_VALUE_REGISTER_CONVENTION;
810     }
811   /* OpenCL vectors < 16 bytes are returned as distinct
812      scalars in f1..f2 or r3..r10.  */
813   if (type->code () == TYPE_CODE_ARRAY
814       && type->is_vector ()
815       && type->length () < 16
816       && opencl_abi)
817     {
818       struct type *eltype = check_typedef (type->target_type ());
819       int i, nelt = type->length () / eltype->length ();
820 
821       for (i = 0; i < nelt; i++)
822           {
823             int offset = i * eltype->length ();
824 
825             if (eltype->code () == TYPE_CODE_FLT)
826               {
827                 int regnum = tdep->ppc_fp0_regnum + 1 + i;
828                 gdb_byte regval[PPC_MAX_REGISTER_SIZE];
829                 struct type *regtype = register_type (gdbarch, regnum);
830 
831                 if (writebuf != NULL)
832                     {
833                       target_float_convert (writebuf + offset, eltype,
834                                                   regval, regtype);
835                       regcache->cooked_write (regnum, regval);
836                     }
837                 if (readbuf != NULL)
838                     {
839                       regcache->cooked_read (regnum, regval);
840                       target_float_convert (regval, regtype,
841                                                   readbuf + offset, eltype);
842                     }
843               }
844             else
845               {
846                 int regnum = tdep->ppc_gp0_regnum + 3 + i;
847                 ULONGEST regval;
848 
849                 if (writebuf != NULL)
850                     {
851                       regval = unpack_long (eltype, writebuf + offset);
852                       regcache_cooked_write_unsigned (regcache, regnum, regval);
853                     }
854                 if (readbuf != NULL)
855                     {
856                       regcache_cooked_read_unsigned (regcache, regnum, &regval);
857                       store_unsigned_integer (readbuf + offset,
858                                                     eltype->length (), byte_order,
859                                                     regval);
860                     }
861               }
862           }
863 
864       return RETURN_VALUE_REGISTER_CONVENTION;
865     }
866   /* OpenCL vectors >= 16 bytes are returned in v2..v9.  */
867   if (type->code () == TYPE_CODE_ARRAY
868       && type->is_vector ()
869       && type->length () >= 16
870       && opencl_abi)
871     {
872       int n_regs = type->length () / 16;
873       int i;
874 
875       for (i = 0; i < n_regs; i++)
876           {
877             int offset = i * 16;
878             int regnum = tdep->ppc_vr0_regnum + 2 + i;
879 
880             if (writebuf != NULL)
881               regcache->cooked_write (regnum, writebuf + offset);
882             if (readbuf != NULL)
883               regcache->cooked_read (regnum, readbuf + offset);
884           }
885 
886       return RETURN_VALUE_REGISTER_CONVENTION;
887     }
888   if (type->length () == 16
889       && type->code () == TYPE_CODE_ARRAY
890       && type->is_vector ()
891       && tdep->vector_abi == POWERPC_VEC_ALTIVEC)
892     {
893       if (readbuf)
894           {
895             /* Altivec places the return value in "v2".  */
896             regcache->cooked_read (tdep->ppc_vr0_regnum + 2, readbuf);
897           }
898       if (writebuf)
899           {
900             /* Altivec places the return value in "v2".  */
901             regcache->cooked_write (tdep->ppc_vr0_regnum + 2, writebuf);
902           }
903       return RETURN_VALUE_REGISTER_CONVENTION;
904     }
905   if (type->length () == 16
906       && type->code () == TYPE_CODE_ARRAY
907       && type->is_vector ()
908       && tdep->vector_abi == POWERPC_VEC_GENERIC)
909     {
910       /* GCC -maltivec -mabi=no-altivec returns vectors in r3/r4/r5/r6.
911            GCC without AltiVec returns them in memory, but it warns about
912            ABI risks in that case; we don't try to support it.  */
913       if (readbuf)
914           {
915             regcache->cooked_read (tdep->ppc_gp0_regnum + 3, readbuf + 0);
916             regcache->cooked_read (tdep->ppc_gp0_regnum + 4, readbuf + 4);
917             regcache->cooked_read (tdep->ppc_gp0_regnum + 5, readbuf + 8);
918             regcache->cooked_read (tdep->ppc_gp0_regnum + 6, readbuf + 12);
919           }
920       if (writebuf)
921           {
922             regcache->cooked_write (tdep->ppc_gp0_regnum + 3, writebuf + 0);
923             regcache->cooked_write (tdep->ppc_gp0_regnum + 4, writebuf + 4);
924             regcache->cooked_write (tdep->ppc_gp0_regnum + 5, writebuf + 8);
925             regcache->cooked_write (tdep->ppc_gp0_regnum + 6, writebuf + 12);
926           }
927       return RETURN_VALUE_REGISTER_CONVENTION;
928     }
929   if (type->length () == 8
930       && type->code () == TYPE_CODE_ARRAY
931       && type->is_vector ()
932       && tdep->vector_abi == POWERPC_VEC_SPE)
933     {
934       /* The e500 ABI places return values for the 64-bit DSP types
935            (__ev64_opaque__) in r3.  However, in GDB-speak, ev3
936            corresponds to the entire r3 value for e500, whereas GDB's r3
937            only corresponds to the least significant 32-bits.  So place
938            the 64-bit DSP type's value in ev3.  */
939       if (readbuf)
940           regcache->cooked_read (tdep->ppc_ev0_regnum + 3, readbuf);
941       if (writebuf)
942           regcache->cooked_write (tdep->ppc_ev0_regnum + 3, writebuf);
943       return RETURN_VALUE_REGISTER_CONVENTION;
944     }
945   if (broken_gcc && type->length () <= 8)
946     {
947       /* GCC screwed up for structures or unions whose size is less
948            than or equal to 8 bytes..  Instead of left-aligning, it
949            right-aligns the data into the buffer formed by r3, r4.  */
950       gdb_byte regvals[PPC_MAX_REGISTER_SIZE * 2];
951       int len = type->length ();
952       int offset = (2 * tdep->wordsize - len) % tdep->wordsize;
953 
954       if (readbuf)
955           {
956             regcache->cooked_read (tdep->ppc_gp0_regnum + 3,
957                                          regvals + 0 * tdep->wordsize);
958             if (len > tdep->wordsize)
959               regcache->cooked_read (tdep->ppc_gp0_regnum + 4,
960                                            regvals + 1 * tdep->wordsize);
961             memcpy (readbuf, regvals + offset, len);
962           }
963       if (writebuf)
964           {
965             memset (regvals, 0, sizeof regvals);
966             memcpy (regvals + offset, writebuf, len);
967             regcache->cooked_write (tdep->ppc_gp0_regnum + 3,
968                                           regvals + 0 * tdep->wordsize);
969             if (len > tdep->wordsize)
970               regcache->cooked_write (tdep->ppc_gp0_regnum + 4,
971                                             regvals + 1 * tdep->wordsize);
972           }
973 
974       return RETURN_VALUE_REGISTER_CONVENTION;
975     }
976   if (type->length () <= 8)
977     {
978       if (readbuf)
979           {
980             /* This matches SVr4 PPC, it does not match GCC.  */
981             /* The value is right-padded to 8 bytes and then loaded, as
982                two "words", into r3/r4.  */
983             gdb_byte regvals[PPC_MAX_REGISTER_SIZE * 2];
984             regcache->cooked_read (tdep->ppc_gp0_regnum + 3,
985                                          regvals + 0 * tdep->wordsize);
986             if (type->length () > tdep->wordsize)
987               regcache->cooked_read (tdep->ppc_gp0_regnum + 4,
988                                            regvals + 1 * tdep->wordsize);
989             memcpy (readbuf, regvals, type->length ());
990           }
991       if (writebuf)
992           {
993             /* This matches SVr4 PPC, it does not match GCC.  */
994             /* The value is padded out to 8 bytes and then loaded, as
995                two "words" into r3/r4.  */
996             gdb_byte regvals[PPC_MAX_REGISTER_SIZE * 2];
997             memset (regvals, 0, sizeof regvals);
998             memcpy (regvals, writebuf, type->length ());
999             regcache->cooked_write (tdep->ppc_gp0_regnum + 3,
1000                                           regvals + 0 * tdep->wordsize);
1001             if (type->length () > tdep->wordsize)
1002               regcache->cooked_write (tdep->ppc_gp0_regnum + 4,
1003                                             regvals + 1 * tdep->wordsize);
1004           }
1005       return RETURN_VALUE_REGISTER_CONVENTION;
1006     }
1007   return RETURN_VALUE_STRUCT_CONVENTION;
1008 }
1009 
1010 enum return_value_convention
ppc_sysv_abi_return_value(struct gdbarch * gdbarch,struct value * function,struct type * valtype,struct regcache * regcache,gdb_byte * readbuf,const gdb_byte * writebuf)1011 ppc_sysv_abi_return_value (struct gdbarch *gdbarch, struct value *function,
1012                                  struct type *valtype, struct regcache *regcache,
1013                                  gdb_byte *readbuf, const gdb_byte *writebuf)
1014 {
1015   return do_ppc_sysv_return_value (gdbarch,
1016                                            function ? function->type () : NULL,
1017                                            valtype, regcache, readbuf, writebuf, 0);
1018 }
1019 
1020 enum return_value_convention
ppc_sysv_abi_broken_return_value(struct gdbarch * gdbarch,struct value * function,struct type * valtype,struct regcache * regcache,gdb_byte * readbuf,const gdb_byte * writebuf)1021 ppc_sysv_abi_broken_return_value (struct gdbarch *gdbarch,
1022                                           struct value *function,
1023                                           struct type *valtype,
1024                                           struct regcache *regcache,
1025                                           gdb_byte *readbuf, const gdb_byte *writebuf)
1026 {
1027   return do_ppc_sysv_return_value (gdbarch,
1028                                            function ? function->type () : NULL,
1029                                            valtype, regcache, readbuf, writebuf, 1);
1030 }
1031 
1032 /* The helper function for 64-bit SYSV push_dummy_call.  Converts the
1033    function's code address back into the function's descriptor
1034    address.
1035 
1036    Find a value for the TOC register.  Every symbol should have both
1037    ".FN" and "FN" in the minimal symbol table.  "FN" points at the
1038    FN's descriptor, while ".FN" points at the entry point (which
1039    matches FUNC_ADDR).  Need to reverse from FUNC_ADDR back to the
1040    FN's descriptor address (while at the same time being careful to
1041    find "FN" in the same object file as ".FN").  */
1042 
1043 static int
convert_code_addr_to_desc_addr(CORE_ADDR code_addr,CORE_ADDR * desc_addr)1044 convert_code_addr_to_desc_addr (CORE_ADDR code_addr, CORE_ADDR *desc_addr)
1045 {
1046   struct obj_section *dot_fn_section;
1047   struct bound_minimal_symbol dot_fn;
1048   struct bound_minimal_symbol fn;
1049 
1050   /* Find the minimal symbol that corresponds to CODE_ADDR (should
1051      have a name of the form ".FN").  */
1052   dot_fn = lookup_minimal_symbol_by_pc (code_addr);
1053   if (dot_fn.minsym == NULL || dot_fn.minsym->linkage_name ()[0] != '.')
1054     return 0;
1055   /* Get the section that contains CODE_ADDR.  Need this for the
1056      "objfile" that it contains.  */
1057   dot_fn_section = find_pc_section (code_addr);
1058   if (dot_fn_section == NULL || dot_fn_section->objfile == NULL)
1059     return 0;
1060   /* Now find the corresponding "FN" (dropping ".") minimal symbol's
1061      address.  Only look for the minimal symbol in ".FN"'s object file
1062      - avoids problems when two object files (i.e., shared libraries)
1063      contain a minimal symbol with the same name.  */
1064   fn = lookup_minimal_symbol (dot_fn.minsym->linkage_name () + 1, NULL,
1065                                     dot_fn_section->objfile);
1066   if (fn.minsym == NULL)
1067     return 0;
1068   /* Found a descriptor.  */
1069   (*desc_addr) = fn.value_address ();
1070   return 1;
1071 }
1072 
1073 /* Walk down the type tree of TYPE counting consecutive base elements.
1074    If *FIELD_TYPE is NULL, then set it to the first valid floating point
1075    or vector type.  If a non-floating point or vector type is found, or
1076    if a floating point or vector type that doesn't match a non-NULL
1077    *FIELD_TYPE is found, then return -1, otherwise return the count in the
1078    sub-tree.  */
1079 
1080 static LONGEST
ppc64_aggregate_candidate(struct type * type,struct type ** field_type)1081 ppc64_aggregate_candidate (struct type *type,
1082                                  struct type **field_type)
1083 {
1084   type = check_typedef (type);
1085 
1086   switch (type->code ())
1087     {
1088     case TYPE_CODE_FLT:
1089     case TYPE_CODE_DECFLOAT:
1090       if (!*field_type)
1091           *field_type = type;
1092       if ((*field_type)->code () == type->code ()
1093             && (*field_type)->length () == type->length ())
1094           return 1;
1095       break;
1096 
1097     case TYPE_CODE_COMPLEX:
1098       type = type->target_type ();
1099       if (type->code () == TYPE_CODE_FLT
1100             || type->code () == TYPE_CODE_DECFLOAT)
1101           {
1102             if (!*field_type)
1103               *field_type = type;
1104             if ((*field_type)->code () == type->code ()
1105                 && (*field_type)->length () == type->length ())
1106               return 2;
1107           }
1108       break;
1109 
1110     case TYPE_CODE_ARRAY:
1111       if (type->is_vector ())
1112           {
1113             if (!*field_type)
1114               *field_type = type;
1115             if ((*field_type)->code () == type->code ()
1116                 && (*field_type)->length () == type->length ())
1117               return 1;
1118           }
1119       else
1120           {
1121             LONGEST count, low_bound, high_bound;
1122 
1123             count = ppc64_aggregate_candidate
1124                        (type->target_type (), field_type);
1125             if (count == -1)
1126               return -1;
1127 
1128             if (!get_array_bounds (type, &low_bound, &high_bound))
1129               return -1;
1130 
1131             LONGEST nr_array_elements = (low_bound > high_bound
1132                                                ? 0
1133                                                : (high_bound - low_bound + 1));
1134             count *= nr_array_elements;
1135 
1136             /* There must be no padding.  */
1137             if (count == 0)
1138               return type->length () == 0 ? 0 : -1;
1139             else if (type->length () != count * (*field_type)->length ())
1140               return -1;
1141 
1142             return count;
1143           }
1144       break;
1145 
1146     case TYPE_CODE_STRUCT:
1147     case TYPE_CODE_UNION:
1148           {
1149             LONGEST count = 0;
1150             int i;
1151 
1152             for (i = 0; i < type->num_fields (); i++)
1153               {
1154                 LONGEST sub_count;
1155 
1156                 if (type->field (i).is_static ())
1157                     continue;
1158 
1159                 sub_count = ppc64_aggregate_candidate
1160                                  (type->field (i).type (), field_type);
1161                 if (sub_count == -1)
1162                     return -1;
1163 
1164                 if (type->code () == TYPE_CODE_STRUCT)
1165                     count += sub_count;
1166                 else
1167                     count = std::max (count, sub_count);
1168               }
1169 
1170             /* There must be no padding.  */
1171             if (count == 0)
1172               return type->length () == 0 ? 0 : -1;
1173             else if (type->length () != count * (*field_type)->length ())
1174               return -1;
1175 
1176             return count;
1177           }
1178       break;
1179 
1180     default:
1181       break;
1182     }
1183 
1184   return -1;
1185 }
1186 
1187 /* If an argument of type TYPE is a homogeneous float or vector aggregate
1188    that shall be passed in FP/vector registers according to the ELFv2 ABI,
1189    return the homogeneous element type in *ELT_TYPE and the number of
1190    elements in *N_ELTS, and return non-zero.  Otherwise, return zero.  */
1191 
1192 static int
ppc64_elfv2_abi_homogeneous_aggregate(struct type * type,struct type ** elt_type,int * n_elts,struct gdbarch * gdbarch)1193 ppc64_elfv2_abi_homogeneous_aggregate (struct type *type,
1194                                                struct type **elt_type, int *n_elts,
1195                                                struct gdbarch *gdbarch)
1196 {
1197   /* Complex types at the top level are treated separately.  However,
1198      complex types can be elements of homogeneous aggregates.  */
1199   if (type->code () == TYPE_CODE_STRUCT
1200       || type->code () == TYPE_CODE_UNION
1201       || (type->code () == TYPE_CODE_ARRAY && !type->is_vector ()))
1202     {
1203       struct type *field_type = NULL;
1204       LONGEST field_count = ppc64_aggregate_candidate (type, &field_type);
1205 
1206       if (field_count > 0)
1207           {
1208             int n_regs;
1209 
1210             if (field_type->code () == TYPE_CODE_FLT
1211                 && (gdbarch_long_double_format (gdbarch)
1212                       == floatformats_ieee_quad))
1213               /* IEEE Float 128-bit uses one vector register.  */
1214               n_regs = 1;
1215 
1216             else if (field_type->code () == TYPE_CODE_FLT
1217                        || field_type->code () == TYPE_CODE_DECFLOAT)
1218               n_regs = (field_type->length () + 7) >> 3;
1219 
1220             else
1221               n_regs = 1;
1222 
1223             /* The ELFv2 ABI allows homogeneous aggregates to occupy
1224                up to 8 registers.  */
1225             if (field_count * n_regs <= 8)
1226               {
1227                 if (elt_type)
1228                     *elt_type = field_type;
1229                 if (n_elts)
1230                     *n_elts = (int) field_count;
1231                 /* Note that field_count is LONGEST since it may hold the size
1232                      of an array, while *n_elts is int since its value is bounded
1233                      by the number of registers used for argument passing.  The
1234                      cast cannot overflow due to the bounds checking above.  */
1235                 return 1;
1236               }
1237           }
1238     }
1239 
1240   return 0;
1241 }
1242 
1243 /* Structure holding the next argument position.  */
1244 struct ppc64_sysv_argpos
1245   {
1246     /* Register cache holding argument registers.  If this is NULL,
1247        we only simulate argument processing without actually updating
1248        any registers or memory.  */
1249     struct regcache *regcache;
1250     /* Next available general-purpose argument register.  */
1251     int greg;
1252     /* Next available floating-point argument register.  */
1253     int freg;
1254     /* Next available vector argument register.  */
1255     int vreg;
1256     /* The address, at which the next general purpose parameter
1257        (integer, struct, float, vector, ...) should be saved.  */
1258     CORE_ADDR gparam;
1259     /* The address, at which the next by-reference parameter
1260        (non-Altivec vector, variably-sized type) should be saved.  */
1261     CORE_ADDR refparam;
1262   };
1263 
1264 /* VAL is a value of length LEN.  Store it into the argument area on the
1265    stack and load it into the corresponding general-purpose registers
1266    required by the ABI, and update ARGPOS.
1267 
1268    If ALIGN is nonzero, it specifies the minimum alignment required
1269    for the on-stack copy of the argument.  */
1270 
1271 static void
ppc64_sysv_abi_push_val(struct gdbarch * gdbarch,const bfd_byte * val,int len,int align,struct ppc64_sysv_argpos * argpos)1272 ppc64_sysv_abi_push_val (struct gdbarch *gdbarch,
1273                                const bfd_byte *val, int len, int align,
1274                                struct ppc64_sysv_argpos *argpos)
1275 {
1276   ppc_gdbarch_tdep *tdep = gdbarch_tdep<ppc_gdbarch_tdep> (gdbarch);
1277   int offset = 0;
1278 
1279   /* Enforce alignment of stack location, if requested.  */
1280   if (align > tdep->wordsize)
1281     {
1282       CORE_ADDR aligned_gparam = align_up (argpos->gparam, align);
1283 
1284       argpos->greg += (aligned_gparam - argpos->gparam) / tdep->wordsize;
1285       argpos->gparam = aligned_gparam;
1286     }
1287 
1288   /* The ABI (version 1.9) specifies that values smaller than one
1289      doubleword are right-aligned and those larger are left-aligned.
1290      GCC versions before 3.4 implemented this incorrectly; see
1291      <http://gcc.gnu.org/gcc-3.4/powerpc-abi.html>.  */
1292   if (len < tdep->wordsize
1293       && gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
1294     offset = tdep->wordsize - len;
1295 
1296   if (argpos->regcache)
1297     write_memory (argpos->gparam + offset, val, len);
1298   argpos->gparam = align_up (argpos->gparam + len, tdep->wordsize);
1299 
1300   while (len >= tdep->wordsize)
1301     {
1302       if (argpos->regcache && argpos->greg <= 10)
1303           argpos->regcache->cooked_write (tdep->ppc_gp0_regnum + argpos->greg,
1304                                                   val);
1305       argpos->greg++;
1306       len -= tdep->wordsize;
1307       val += tdep->wordsize;
1308     }
1309 
1310   if (len > 0)
1311     {
1312       if (argpos->regcache && argpos->greg <= 10)
1313           argpos->regcache->cooked_write_part
1314             (tdep->ppc_gp0_regnum + argpos->greg, offset, len, val);
1315       argpos->greg++;
1316     }
1317 }
1318 
1319 /* The same as ppc64_sysv_abi_push_val, but using a single-word integer
1320    value VAL as argument.  */
1321 
1322 static void
ppc64_sysv_abi_push_integer(struct gdbarch * gdbarch,ULONGEST val,struct ppc64_sysv_argpos * argpos)1323 ppc64_sysv_abi_push_integer (struct gdbarch *gdbarch, ULONGEST val,
1324                                    struct ppc64_sysv_argpos *argpos)
1325 {
1326   ppc_gdbarch_tdep *tdep = gdbarch_tdep<ppc_gdbarch_tdep> (gdbarch);
1327   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1328   gdb_byte buf[PPC_MAX_REGISTER_SIZE];
1329 
1330   if (argpos->regcache)
1331     store_unsigned_integer (buf, tdep->wordsize, byte_order, val);
1332   ppc64_sysv_abi_push_val (gdbarch, buf, tdep->wordsize, 0, argpos);
1333 }
1334 
1335 /* VAL is a value of TYPE, a (binary or decimal) floating-point type.
1336    Load it into a floating-point register if required by the ABI,
1337    and update ARGPOS.  */
1338 
1339 static void
ppc64_sysv_abi_push_freg(struct gdbarch * gdbarch,struct type * type,const bfd_byte * val,struct ppc64_sysv_argpos * argpos)1340 ppc64_sysv_abi_push_freg (struct gdbarch *gdbarch,
1341                                 struct type *type, const bfd_byte *val,
1342                                 struct ppc64_sysv_argpos *argpos)
1343 {
1344   ppc_gdbarch_tdep *tdep = gdbarch_tdep<ppc_gdbarch_tdep> (gdbarch);
1345   if (tdep->soft_float)
1346     return;
1347 
1348   if (type->length () <= 8
1349       && type->code () == TYPE_CODE_FLT)
1350     {
1351       /* Floats and doubles go in f1 .. f13.  32-bit floats are converted
1352            to double first.  */
1353       if (argpos->regcache && argpos->freg <= 13)
1354           {
1355             int regnum = tdep->ppc_fp0_regnum + argpos->freg;
1356             struct type *regtype = register_type (gdbarch, regnum);
1357             gdb_byte regval[PPC_MAX_REGISTER_SIZE];
1358 
1359             target_float_convert (val, type, regval, regtype);
1360             argpos->regcache->cooked_write (regnum, regval);
1361           }
1362 
1363       argpos->freg++;
1364     }
1365   else if (type->length () <= 8
1366              && type->code () == TYPE_CODE_DECFLOAT)
1367     {
1368       /* Floats and doubles go in f1 .. f13.  32-bit decimal floats are
1369            placed in the least significant word.  */
1370       if (argpos->regcache && argpos->freg <= 13)
1371           {
1372             int regnum = tdep->ppc_fp0_regnum + argpos->freg;
1373             int offset = 0;
1374 
1375             if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
1376               offset = 8 - type->length ();
1377 
1378             argpos->regcache->cooked_write_part (regnum, offset,
1379                                                          type->length (), val);
1380           }
1381 
1382       argpos->freg++;
1383     }
1384   else if (type->length () == 16
1385              && type->code () == TYPE_CODE_FLT
1386              && (gdbarch_long_double_format (gdbarch)
1387                  == floatformats_ibm_long_double))
1388     {
1389       /* IBM long double stored in two consecutive FPRs.  */
1390       if (argpos->regcache && argpos->freg <= 13)
1391           {
1392             int regnum = tdep->ppc_fp0_regnum + argpos->freg;
1393 
1394             argpos->regcache->cooked_write (regnum, val);
1395             if (argpos->freg <= 12)
1396               argpos->regcache->cooked_write (regnum + 1, val + 8);
1397           }
1398 
1399       argpos->freg += 2;
1400     }
1401   else if (type->length () == 16
1402              && type->code () == TYPE_CODE_DECFLOAT)
1403     {
1404       /* 128-bit decimal floating-point values are stored in and even/odd
1405            pair of FPRs, with the even FPR holding the most significant half.  */
1406       argpos->freg += argpos->freg & 1;
1407 
1408       if (argpos->regcache && argpos->freg <= 12)
1409           {
1410             int regnum = tdep->ppc_fp0_regnum + argpos->freg;
1411             int lopart = gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG ? 8 : 0;
1412             int hipart = gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG ? 0 : 8;
1413 
1414             argpos->regcache->cooked_write (regnum, val + hipart);
1415             argpos->regcache->cooked_write (regnum + 1, val + lopart);
1416           }
1417 
1418       argpos->freg += 2;
1419     }
1420 }
1421 
1422 /* VAL is a value of AltiVec vector type.  Load it into a vector register
1423    if required by the ABI, and update ARGPOS.  */
1424 
1425 static void
ppc64_sysv_abi_push_vreg(struct gdbarch * gdbarch,const bfd_byte * val,struct ppc64_sysv_argpos * argpos)1426 ppc64_sysv_abi_push_vreg (struct gdbarch *gdbarch, const bfd_byte *val,
1427                                 struct ppc64_sysv_argpos *argpos)
1428 {
1429   ppc_gdbarch_tdep *tdep = gdbarch_tdep<ppc_gdbarch_tdep> (gdbarch);
1430 
1431   if (argpos->regcache && argpos->vreg <= 13)
1432     argpos->regcache->cooked_write (tdep->ppc_vr0_regnum + argpos->vreg, val);
1433 
1434   argpos->vreg++;
1435 }
1436 
1437 /* VAL is a value of TYPE.  Load it into memory and/or registers
1438    as required by the ABI, and update ARGPOS.  */
1439 
1440 static void
ppc64_sysv_abi_push_param(struct gdbarch * gdbarch,struct type * type,const bfd_byte * val,struct ppc64_sysv_argpos * argpos)1441 ppc64_sysv_abi_push_param (struct gdbarch *gdbarch,
1442                                  struct type *type, const bfd_byte *val,
1443                                  struct ppc64_sysv_argpos *argpos)
1444 {
1445   ppc_gdbarch_tdep *tdep = gdbarch_tdep<ppc_gdbarch_tdep> (gdbarch);
1446 
1447   if (type->code () == TYPE_CODE_FLT
1448       && type->length () == 16
1449       && (gdbarch_long_double_format (gdbarch)
1450             == floatformats_ieee_quad))
1451     {
1452       /* IEEE FLOAT128, args in vector registers.  */
1453       ppc64_sysv_abi_push_val (gdbarch, val, type->length (), 16, argpos);
1454       ppc64_sysv_abi_push_vreg (gdbarch, val, argpos);
1455     }
1456   else if (type->code () == TYPE_CODE_FLT
1457              || type->code () == TYPE_CODE_DECFLOAT)
1458     {
1459       /* Floating-point scalars are passed in floating-point registers.  */
1460       ppc64_sysv_abi_push_val (gdbarch, val, type->length (), 0, argpos);
1461       ppc64_sysv_abi_push_freg (gdbarch, type, val, argpos);
1462     }
1463   else if (type->code () == TYPE_CODE_ARRAY && type->is_vector ()
1464              && tdep->vector_abi == POWERPC_VEC_ALTIVEC
1465              && type->length () == 16)
1466     {
1467       /* AltiVec vectors are passed aligned, and in vector registers.  */
1468       ppc64_sysv_abi_push_val (gdbarch, val, type->length (), 16, argpos);
1469       ppc64_sysv_abi_push_vreg (gdbarch, val, argpos);
1470     }
1471   else if (type->code () == TYPE_CODE_ARRAY && type->is_vector ()
1472              && type->length () >= 16)
1473     {
1474       /* Non-Altivec vectors are passed by reference.  */
1475 
1476       /* Copy value onto the stack ...  */
1477       CORE_ADDR addr = align_up (argpos->refparam, 16);
1478       if (argpos->regcache)
1479           write_memory (addr, val, type->length ());
1480       argpos->refparam = align_up (addr + type->length (), tdep->wordsize);
1481 
1482       /* ... and pass a pointer to the copy as parameter.  */
1483       ppc64_sysv_abi_push_integer (gdbarch, addr, argpos);
1484     }
1485   else if ((type->code () == TYPE_CODE_INT
1486               || type->code () == TYPE_CODE_ENUM
1487               || type->code () == TYPE_CODE_BOOL
1488               || type->code () == TYPE_CODE_CHAR
1489               || type->code () == TYPE_CODE_PTR
1490               || TYPE_IS_REFERENCE (type))
1491              && type->length () <= tdep->wordsize)
1492     {
1493       ULONGEST word = 0;
1494 
1495       if (argpos->regcache)
1496           {
1497             /* Sign extend the value, then store it unsigned.  */
1498             word = unpack_long (type, val);
1499 
1500             /* Convert any function code addresses into descriptors.  */
1501             if (tdep->elf_abi == POWERPC_ELF_V1
1502                 && (type->code () == TYPE_CODE_PTR
1503                       || type->code () == TYPE_CODE_REF))
1504               {
1505                 struct type *target_type
1506                     = check_typedef (type->target_type ());
1507 
1508                 if (target_type->code () == TYPE_CODE_FUNC
1509                       || target_type->code () == TYPE_CODE_METHOD)
1510                     {
1511                       CORE_ADDR desc = word;
1512 
1513                       convert_code_addr_to_desc_addr (word, &desc);
1514                       word = desc;
1515                     }
1516               }
1517           }
1518 
1519       ppc64_sysv_abi_push_integer (gdbarch, word, argpos);
1520     }
1521   else
1522     {
1523       /* Align == 0 is correct for ppc64_sysv_abi_push_freg,
1524            Align == 16 is correct for ppc64_sysv_abi_push_vreg.
1525            Default to 0.      */
1526       int align = 0;
1527 
1528       /* The ABI (version 1.9) specifies that structs containing a
1529            single floating-point value, at any level of nesting of
1530            single-member structs, are passed in floating-point registers.  */
1531       if (type->code () == TYPE_CODE_STRUCT
1532             && type->num_fields () == 1 && tdep->elf_abi == POWERPC_ELF_V1)
1533           {
1534             while (type->code () == TYPE_CODE_STRUCT
1535                      && type->num_fields () == 1)
1536               type = check_typedef (type->field (0).type ());
1537 
1538             if (type->code () == TYPE_CODE_FLT) {
1539               /* Handle the case of 128-bit floats for both IEEE and IBM long double
1540                  formats.  */
1541               if (type->length () == 16
1542                     && (gdbarch_long_double_format (gdbarch)
1543                         == floatformats_ieee_quad))
1544                 {
1545                     ppc64_sysv_abi_push_vreg (gdbarch, val, argpos);
1546                     align = 16;
1547                 }
1548               else
1549                 ppc64_sysv_abi_push_freg (gdbarch, type, val, argpos);
1550             }
1551           }
1552 
1553       /* In the ELFv2 ABI, homogeneous floating-point or vector
1554            aggregates are passed in a series of registers.  */
1555       if (tdep->elf_abi == POWERPC_ELF_V2)
1556           {
1557             struct type *eltype;
1558             int i, nelt;
1559 
1560             if (ppc64_elfv2_abi_homogeneous_aggregate (type, &eltype, &nelt,
1561                                                                  gdbarch))
1562               for (i = 0; i < nelt; i++)
1563                 {
1564                     const gdb_byte *elval = val + i * eltype->length ();
1565 
1566                     if (eltype->code () == TYPE_CODE_FLT
1567                         && eltype->length () == 16
1568                         && (gdbarch_long_double_format (gdbarch)
1569                               == floatformats_ieee_quad))
1570                       /* IEEE FLOAT128, args in vector registers.  */
1571                       {
1572                         ppc64_sysv_abi_push_vreg (gdbarch, elval, argpos);
1573                         align = 16;
1574                       }
1575                     else if (eltype->code () == TYPE_CODE_FLT
1576                                || eltype->code () == TYPE_CODE_DECFLOAT)
1577                         /* IBM long double and all other floats and decfloats, args
1578                            are in a pair of floating point registers.  */
1579                       ppc64_sysv_abi_push_freg (gdbarch, eltype, elval, argpos);
1580                     else if (eltype->code () == TYPE_CODE_ARRAY
1581                                && eltype->is_vector ()
1582                                && tdep->vector_abi == POWERPC_VEC_ALTIVEC
1583                                && eltype->length () == 16)
1584                       {
1585                         ppc64_sysv_abi_push_vreg (gdbarch, elval, argpos);
1586                         align = 16;
1587                       }
1588                 }
1589           }
1590 
1591       ppc64_sysv_abi_push_val (gdbarch, val, type->length (), align, argpos);
1592     }
1593 }
1594 
1595 /* Pass the arguments in either registers, or in the stack.  Using the
1596    ppc 64 bit SysV ABI.
1597 
1598    This implements a dumbed down version of the ABI.  It always writes
1599    values to memory, GPR and FPR, even when not necessary.  Doing this
1600    greatly simplifies the logic.  */
1601 
1602 CORE_ADDR
ppc64_sysv_abi_push_dummy_call(struct gdbarch * gdbarch,struct value * function,struct regcache * regcache,CORE_ADDR bp_addr,int nargs,struct value ** args,CORE_ADDR sp,function_call_return_method return_method,CORE_ADDR struct_addr)1603 ppc64_sysv_abi_push_dummy_call (struct gdbarch *gdbarch,
1604                                         struct value *function,
1605                                         struct regcache *regcache, CORE_ADDR bp_addr,
1606                                         int nargs, struct value **args, CORE_ADDR sp,
1607                                         function_call_return_method return_method,
1608                                         CORE_ADDR struct_addr)
1609 {
1610   CORE_ADDR func_addr = find_function_addr (function, NULL);
1611   ppc_gdbarch_tdep *tdep = gdbarch_tdep<ppc_gdbarch_tdep> (gdbarch);
1612   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1613   int opencl_abi = ppc_sysv_use_opencl_abi (function->type ());
1614   ULONGEST back_chain;
1615   /* See for-loop comment below.  */
1616   int write_pass;
1617   /* Size of the by-reference parameter copy region, the final value is
1618      computed in the for-loop below.  */
1619   LONGEST refparam_size = 0;
1620   /* Size of the general parameter region, the final value is computed
1621      in the for-loop below.  */
1622   LONGEST gparam_size = 0;
1623   /* Kevin writes ... I don't mind seeing tdep->wordsize used in the
1624      calls to align_up(), align_down(), etc. because this makes it
1625      easier to reuse this code (in a copy/paste sense) in the future,
1626      but it is a 64-bit ABI and asserting that the wordsize is 8 bytes
1627      at some point makes it easier to verify that this function is
1628      correct without having to do a non-local analysis to figure out
1629      the possible values of tdep->wordsize.  */
1630   gdb_assert (tdep->wordsize == 8);
1631 
1632   /* This function exists to support a calling convention that
1633      requires floating-point registers.  It shouldn't be used on
1634      processors that lack them.  */
1635   gdb_assert (ppc_floating_point_unit_p (gdbarch));
1636 
1637   /* By this stage in the proceedings, SP has been decremented by "red
1638      zone size" + "struct return size".  Fetch the stack-pointer from
1639      before this and use that as the BACK_CHAIN.  */
1640   regcache_cooked_read_unsigned (regcache, gdbarch_sp_regnum (gdbarch),
1641                                          &back_chain);
1642 
1643   /* Go through the argument list twice.
1644 
1645      Pass 1: Compute the function call's stack space and register
1646      requirements.
1647 
1648      Pass 2: Replay the same computation but this time also write the
1649      values out to the target.  */
1650 
1651   for (write_pass = 0; write_pass < 2; write_pass++)
1652     {
1653       int argno;
1654 
1655       struct ppc64_sysv_argpos argpos;
1656       argpos.greg = 3;
1657       argpos.freg = 1;
1658       argpos.vreg = 2;
1659 
1660       if (!write_pass)
1661           {
1662             /* During the first pass, GPARAM and REFPARAM are more like
1663                offsets (start address zero) than addresses.  That way
1664                they accumulate the total stack space each region
1665                requires.  */
1666             argpos.regcache = NULL;
1667             argpos.gparam = 0;
1668             argpos.refparam = 0;
1669           }
1670       else
1671           {
1672             /* Decrement the stack pointer making space for the Altivec
1673                and general on-stack parameters.  Set refparam and gparam
1674                to their corresponding regions.  */
1675             argpos.regcache = regcache;
1676             argpos.refparam = align_down (sp - refparam_size, 16);
1677             argpos.gparam = align_down (argpos.refparam - gparam_size, 16);
1678             /* Add in space for the TOC, link editor double word (v1 only),
1679                compiler double word (v1 only), LR save area, CR save area,
1680                and backchain.  */
1681             if (tdep->elf_abi == POWERPC_ELF_V1)
1682               sp = align_down (argpos.gparam - 48, 16);
1683             else
1684               sp = align_down (argpos.gparam - 32, 16);
1685           }
1686 
1687       /* If the function is returning a `struct', then there is an
1688            extra hidden parameter (which will be passed in r3)
1689            containing the address of that struct..  In that case we
1690            should advance one word and start from r4 register to copy
1691            parameters.  This also consumes one on-stack parameter slot.  */
1692       if (return_method == return_method_struct)
1693           ppc64_sysv_abi_push_integer (gdbarch, struct_addr, &argpos);
1694 
1695       for (argno = 0; argno < nargs; argno++)
1696           {
1697             struct value *arg = args[argno];
1698             struct type *type = check_typedef (arg->type ());
1699             const bfd_byte *val = arg->contents ().data ();
1700 
1701             if (type->code () == TYPE_CODE_COMPLEX)
1702               {
1703                 /* Complex types are passed as if two independent scalars.  */
1704                 struct type *eltype = check_typedef (type->target_type ());
1705 
1706                 ppc64_sysv_abi_push_param (gdbarch, eltype, val, &argpos);
1707                 ppc64_sysv_abi_push_param (gdbarch, eltype,
1708                                                    val + eltype->length (), &argpos);
1709               }
1710             else if (type->code () == TYPE_CODE_ARRAY && type->is_vector ()
1711                        && opencl_abi)
1712               {
1713                 /* OpenCL vectors shorter than 16 bytes are passed as if
1714                      a series of independent scalars; OpenCL vectors 16 bytes
1715                      or longer are passed as if a series of AltiVec vectors.  */
1716                 struct type *eltype;
1717                 int i, nelt;
1718 
1719                 if (type->length () < 16)
1720                     eltype = check_typedef (type->target_type ());
1721                 else
1722                     eltype = register_type (gdbarch, tdep->ppc_vr0_regnum);
1723 
1724                 nelt = type->length () / eltype->length ();
1725                 for (i = 0; i < nelt; i++)
1726                     {
1727                       const gdb_byte *elval = val + i * eltype->length ();
1728 
1729                       ppc64_sysv_abi_push_param (gdbarch, eltype, elval, &argpos);
1730                     }
1731               }
1732             else
1733               {
1734                 /* All other types are passed as single arguments.  */
1735                 ppc64_sysv_abi_push_param (gdbarch, type, val, &argpos);
1736               }
1737           }
1738 
1739       if (!write_pass)
1740           {
1741             /* Save the true region sizes ready for the second pass.  */
1742             refparam_size = argpos.refparam;
1743             /* Make certain that the general parameter save area is at
1744                least the minimum 8 registers (or doublewords) in size.  */
1745             if (argpos.greg < 8)
1746               gparam_size = 8 * tdep->wordsize;
1747             else
1748               gparam_size = argpos.gparam;
1749           }
1750     }
1751 
1752   /* Update %sp.   */
1753   regcache_cooked_write_signed (regcache, gdbarch_sp_regnum (gdbarch), sp);
1754 
1755   /* Write the backchain (it occupies WORDSIZED bytes).  */
1756   write_memory_signed_integer (sp, tdep->wordsize, byte_order, back_chain);
1757 
1758   /* Point the inferior function call's return address at the dummy's
1759      breakpoint.  */
1760   regcache_cooked_write_signed (regcache, tdep->ppc_lr_regnum, bp_addr);
1761 
1762   /* In the ELFv1 ABI, use the func_addr to find the descriptor, and use
1763      that to find the TOC.  If we're calling via a function pointer,
1764      the pointer itself identifies the descriptor.  */
1765   if (tdep->elf_abi == POWERPC_ELF_V1)
1766     {
1767       struct type *ftype = check_typedef (function->type ());
1768       CORE_ADDR desc_addr = value_as_address (function);
1769 
1770       if (ftype->code () == TYPE_CODE_PTR
1771             || convert_code_addr_to_desc_addr (func_addr, &desc_addr))
1772           {
1773             /* The TOC is the second double word in the descriptor.  */
1774             CORE_ADDR toc =
1775               read_memory_unsigned_integer (desc_addr + tdep->wordsize,
1776                                                     tdep->wordsize, byte_order);
1777 
1778             regcache_cooked_write_unsigned (regcache,
1779                                                     tdep->ppc_gp0_regnum + 2, toc);
1780           }
1781     }
1782 
1783   /* In the ELFv2 ABI, we need to pass the target address in r12 since
1784      we may be calling a global entry point.  */
1785   if (tdep->elf_abi == POWERPC_ELF_V2)
1786     regcache_cooked_write_unsigned (regcache,
1787                                             tdep->ppc_gp0_regnum + 12, func_addr);
1788 
1789   return sp;
1790 }
1791 
1792 /* Subroutine of ppc64_sysv_abi_return_value that handles "base" types:
1793    integer, floating-point, and AltiVec vector types.
1794 
1795    This routine also handles components of aggregate return types;
1796    INDEX describes which part of the aggregate is to be handled.
1797 
1798    Returns true if VALTYPE is some such base type that could be handled,
1799    false otherwise.  */
1800 static int
ppc64_sysv_abi_return_value_base(struct gdbarch * gdbarch,struct type * valtype,struct regcache * regcache,gdb_byte * readbuf,const gdb_byte * writebuf,int index)1801 ppc64_sysv_abi_return_value_base (struct gdbarch *gdbarch, struct type *valtype,
1802                                           struct regcache *regcache, gdb_byte *readbuf,
1803                                           const gdb_byte *writebuf, int index)
1804 {
1805   ppc_gdbarch_tdep *tdep = gdbarch_tdep<ppc_gdbarch_tdep> (gdbarch);
1806 
1807   /* Integers live in GPRs starting at r3.  */
1808   if ((valtype->code () == TYPE_CODE_INT
1809        || valtype->code () == TYPE_CODE_ENUM
1810        || valtype->code () == TYPE_CODE_CHAR
1811        || valtype->code () == TYPE_CODE_BOOL
1812        || valtype->code () == TYPE_CODE_RANGE
1813        || is_fixed_point_type (valtype))
1814       && valtype->length () <= 8)
1815     {
1816       int regnum = tdep->ppc_gp0_regnum + 3 + index;
1817 
1818       if (writebuf != NULL)
1819           {
1820             LONGEST return_val;
1821 
1822             if (is_fixed_point_type (valtype))
1823               {
1824                 /* Fixed point type values need to be returned unscaled.  */
1825                 gdb_mpz unscaled;
1826 
1827                 unscaled.read (gdb::make_array_view (writebuf,
1828                                                                valtype->length ()),
1829                                    type_byte_order (valtype),
1830                                    valtype->is_unsigned ());
1831                 return_val = unscaled.as_integer<LONGEST> ();
1832               }
1833             else
1834               return_val = unpack_long (valtype, writebuf);
1835 
1836             /* Be careful to sign extend the value.  */
1837             regcache_cooked_write_unsigned (regcache, regnum, return_val);
1838           }
1839       if (readbuf != NULL)
1840           {
1841             /* Extract the integer from GPR.  Since this is truncating the
1842                value, there isn't a sign extension problem.  */
1843             ULONGEST regval;
1844 
1845             regcache_cooked_read_unsigned (regcache, regnum, &regval);
1846             store_unsigned_integer (readbuf, valtype->length (),
1847                                           gdbarch_byte_order (gdbarch), regval);
1848           }
1849       return 1;
1850     }
1851 
1852   /* Floats and doubles go in f1 .. f13.  32-bit floats are converted
1853      to double first.  */
1854   if (valtype->length () <= 8
1855       && valtype->code () == TYPE_CODE_FLT)
1856     {
1857       int regnum = tdep->ppc_fp0_regnum + 1 + index;
1858       struct type *regtype = register_type (gdbarch, regnum);
1859       gdb_byte regval[PPC_MAX_REGISTER_SIZE];
1860 
1861       if (writebuf != NULL)
1862           {
1863             target_float_convert (writebuf, valtype, regval, regtype);
1864             regcache->cooked_write (regnum, regval);
1865           }
1866       if (readbuf != NULL)
1867           {
1868             regcache->cooked_read (regnum, regval);
1869             target_float_convert (regval, regtype, readbuf, valtype);
1870           }
1871       return 1;
1872     }
1873 
1874   /* Floats and doubles go in f1 .. f13.  32-bit decimal floats are
1875      placed in the least significant word.  */
1876   if (valtype->length () <= 8
1877       && valtype->code () == TYPE_CODE_DECFLOAT)
1878     {
1879       int regnum = tdep->ppc_fp0_regnum + 1 + index;
1880       int offset = 0;
1881 
1882       if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
1883           offset = 8 - valtype->length ();
1884 
1885       if (writebuf != NULL)
1886           regcache->cooked_write_part (regnum, offset, valtype->length (),
1887                                              writebuf);
1888       if (readbuf != NULL)
1889           regcache->cooked_read_part (regnum, offset, valtype->length (),
1890                                             readbuf);
1891       return 1;
1892     }
1893 
1894   /* IBM long double stored in two consecutive FPRs.  */
1895   if (valtype->length () == 16
1896       && valtype->code () == TYPE_CODE_FLT
1897       && (gdbarch_long_double_format (gdbarch)
1898             == floatformats_ibm_long_double))
1899     {
1900       int regnum = tdep->ppc_fp0_regnum + 1 + 2 * index;
1901 
1902       if (writebuf != NULL)
1903           {
1904             regcache->cooked_write (regnum, writebuf);
1905             regcache->cooked_write (regnum + 1, writebuf + 8);
1906           }
1907       if (readbuf != NULL)
1908           {
1909             regcache->cooked_read (regnum, readbuf);
1910             regcache->cooked_read (regnum + 1, readbuf + 8);
1911           }
1912       return 1;
1913     }
1914 
1915   /* 128-bit decimal floating-point values are stored in an even/odd
1916      pair of FPRs, with the even FPR holding the most significant half.  */
1917   if (valtype->length () == 16
1918       && valtype->code () == TYPE_CODE_DECFLOAT)
1919     {
1920       int regnum = tdep->ppc_fp0_regnum + 2 + 2 * index;
1921       int lopart = gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG ? 8 : 0;
1922       int hipart = gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG ? 0 : 8;
1923 
1924       if (writebuf != NULL)
1925           {
1926             regcache->cooked_write (regnum, writebuf + hipart);
1927             regcache->cooked_write (regnum + 1, writebuf + lopart);
1928           }
1929       if (readbuf != NULL)
1930           {
1931             regcache->cooked_read (regnum, readbuf + hipart);
1932             regcache->cooked_read (regnum + 1, readbuf + lopart);
1933           }
1934       return 1;
1935     }
1936 
1937   /* AltiVec vectors are returned in VRs starting at v2.
1938      IEEE FLOAT 128-bit are stored in vector register.  */
1939 
1940   if (valtype->length () == 16
1941       && ((valtype->code () == TYPE_CODE_ARRAY
1942              && valtype->is_vector ()
1943              && tdep->vector_abi == POWERPC_VEC_ALTIVEC)
1944             || (valtype->code () == TYPE_CODE_FLT
1945                 && (gdbarch_long_double_format (gdbarch)
1946                       == floatformats_ieee_quad))))
1947     {
1948       int regnum = tdep->ppc_vr0_regnum + 2 + index;
1949 
1950       if (writebuf != NULL)
1951           regcache->cooked_write (regnum, writebuf);
1952       if (readbuf != NULL)
1953           regcache->cooked_read (regnum, readbuf);
1954       return 1;
1955     }
1956 
1957   /* Short vectors are returned in GPRs starting at r3.  */
1958   if (valtype->length () <= 8
1959       && valtype->code () == TYPE_CODE_ARRAY && valtype->is_vector ())
1960     {
1961       int regnum = tdep->ppc_gp0_regnum + 3 + index;
1962       int offset = 0;
1963 
1964       if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
1965           offset = 8 - valtype->length ();
1966 
1967       if (writebuf != NULL)
1968           regcache->cooked_write_part (regnum, offset, valtype->length (),
1969                                              writebuf);
1970       if (readbuf != NULL)
1971           regcache->cooked_read_part (regnum, offset, valtype->length (),
1972                                             readbuf);
1973       return 1;
1974     }
1975 
1976   return 0;
1977 }
1978 
1979 /* The 64 bit ABI return value convention.
1980 
1981    Return non-zero if the return-value is stored in a register, return
1982    0 if the return-value is instead stored on the stack (a.k.a.,
1983    struct return convention).
1984 
1985    For a return-value stored in a register: when WRITEBUF is non-NULL,
1986    copy the buffer to the corresponding register return-value location
1987    location; when READBUF is non-NULL, fill the buffer from the
1988    corresponding register return-value location.  */
1989 enum return_value_convention
ppc64_sysv_abi_return_value(struct gdbarch * gdbarch,struct value * function,struct type * valtype,struct regcache * regcache,gdb_byte * readbuf,const gdb_byte * writebuf)1990 ppc64_sysv_abi_return_value (struct gdbarch *gdbarch, struct value *function,
1991                                    struct type *valtype, struct regcache *regcache,
1992                                    gdb_byte *readbuf, const gdb_byte *writebuf)
1993 {
1994   ppc_gdbarch_tdep *tdep = gdbarch_tdep<ppc_gdbarch_tdep> (gdbarch);
1995   struct type *func_type = function ? function->type () : NULL;
1996   int opencl_abi = func_type? ppc_sysv_use_opencl_abi (func_type) : 0;
1997   struct type *eltype;
1998   int nelt, ok;
1999 
2000   /* This function exists to support a calling convention that
2001      requires floating-point registers.  It shouldn't be used on
2002      processors that lack them.  */
2003   gdb_assert (ppc_floating_point_unit_p (gdbarch));
2004 
2005   /* Complex types are returned as if two independent scalars.  */
2006   if (valtype->code () == TYPE_CODE_COMPLEX)
2007     {
2008       eltype = check_typedef (valtype->target_type ());
2009 
2010       for (int i = 0; i < 2; i++)
2011           {
2012             ok = ppc64_sysv_abi_return_value_base (gdbarch, eltype, regcache,
2013                                                              readbuf, writebuf, i);
2014             gdb_assert (ok);
2015 
2016             if (readbuf)
2017               readbuf += eltype->length ();
2018             if (writebuf)
2019               writebuf += eltype->length ();
2020           }
2021       return RETURN_VALUE_REGISTER_CONVENTION;
2022     }
2023 
2024   /* OpenCL vectors shorter than 16 bytes are returned as if
2025      a series of independent scalars; OpenCL vectors 16 bytes
2026      or longer are returned as if a series of AltiVec vectors.  */
2027   if (valtype->code () == TYPE_CODE_ARRAY && valtype->is_vector ()
2028       && opencl_abi)
2029     {
2030       if (valtype->length () < 16)
2031           eltype = check_typedef (valtype->target_type ());
2032       else
2033           eltype = register_type (gdbarch, tdep->ppc_vr0_regnum);
2034 
2035       nelt = valtype->length () / eltype->length ();
2036       for (int i = 0; i < nelt; i++)
2037           {
2038             ok = ppc64_sysv_abi_return_value_base (gdbarch, eltype, regcache,
2039                                                              readbuf, writebuf, i);
2040             gdb_assert (ok);
2041 
2042             if (readbuf)
2043               readbuf += eltype->length ();
2044             if (writebuf)
2045               writebuf += eltype->length ();
2046           }
2047       return RETURN_VALUE_REGISTER_CONVENTION;
2048     }
2049 
2050   /* All pointers live in r3.  */
2051   if (valtype->code () == TYPE_CODE_PTR || TYPE_IS_REFERENCE (valtype))
2052     {
2053       int regnum = tdep->ppc_gp0_regnum + 3;
2054 
2055       if (writebuf != NULL)
2056           regcache->cooked_write (regnum, writebuf);
2057       if (readbuf != NULL)
2058           regcache->cooked_read (regnum, readbuf);
2059       return RETURN_VALUE_REGISTER_CONVENTION;
2060     }
2061 
2062   /* Small character arrays are returned, right justified, in r3.  */
2063   if (valtype->code () == TYPE_CODE_ARRAY
2064       && !valtype->is_vector ()
2065       && valtype->length () <= 8
2066       && valtype->target_type ()->code () == TYPE_CODE_INT
2067       && valtype->target_type ()->length () == 1)
2068     {
2069       int regnum = tdep->ppc_gp0_regnum + 3;
2070       int offset = (register_size (gdbarch, regnum) - valtype->length ());
2071 
2072       if (writebuf != NULL)
2073           regcache->cooked_write_part (regnum, offset, valtype->length (),
2074                                              writebuf);
2075       if (readbuf != NULL)
2076           regcache->cooked_read_part (regnum, offset, valtype->length (),
2077                                             readbuf);
2078       return RETURN_VALUE_REGISTER_CONVENTION;
2079     }
2080 
2081   /* In the ELFv2 ABI, homogeneous floating-point or vector
2082      aggregates are returned in registers.  */
2083   if (tdep->elf_abi == POWERPC_ELF_V2
2084       && ppc64_elfv2_abi_homogeneous_aggregate (valtype, &eltype, &nelt,
2085                                                             gdbarch)
2086       && (eltype->code () == TYPE_CODE_FLT
2087             || eltype->code () == TYPE_CODE_DECFLOAT
2088             || (eltype->code () == TYPE_CODE_ARRAY
2089                 && eltype->is_vector ()
2090                 && tdep->vector_abi == POWERPC_VEC_ALTIVEC
2091                 && eltype->length () == 16)))
2092     {
2093       for (int i = 0; i < nelt; i++)
2094           {
2095             ok = ppc64_sysv_abi_return_value_base (gdbarch, eltype, regcache,
2096                                                              readbuf, writebuf, i);
2097             gdb_assert (ok);
2098 
2099             if (readbuf)
2100               readbuf += eltype->length ();
2101             if (writebuf)
2102               writebuf += eltype->length ();
2103           }
2104 
2105       return RETURN_VALUE_REGISTER_CONVENTION;
2106     }
2107 
2108   if (!language_pass_by_reference (valtype).trivially_copyable
2109       && valtype->code () == TYPE_CODE_STRUCT)
2110     return RETURN_VALUE_STRUCT_CONVENTION;
2111 
2112   /* In the ELFv2 ABI, aggregate types of up to 16 bytes are
2113      returned in registers r3:r4.  */
2114   if (tdep->elf_abi == POWERPC_ELF_V2
2115       && valtype->length () <= 16
2116       && (valtype->code () == TYPE_CODE_STRUCT
2117             || valtype->code () == TYPE_CODE_UNION
2118             || (valtype->code () == TYPE_CODE_ARRAY
2119                 && !valtype->is_vector ())))
2120     {
2121       int n_regs = ((valtype->length () + tdep->wordsize - 1)
2122                         / tdep->wordsize);
2123 
2124       for (int i = 0; i < n_regs; i++)
2125           {
2126             gdb_byte regval[PPC_MAX_REGISTER_SIZE];
2127             int regnum = tdep->ppc_gp0_regnum + 3 + i;
2128             int offset = i * tdep->wordsize;
2129             int len = valtype->length () - offset;
2130 
2131             if (len > tdep->wordsize)
2132               len = tdep->wordsize;
2133 
2134             if (writebuf != NULL)
2135               {
2136                 memset (regval, 0, sizeof regval);
2137                 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG
2138                       && offset == 0)
2139                     memcpy (regval + tdep->wordsize - len, writebuf, len);
2140                 else
2141                     memcpy (regval, writebuf + offset, len);
2142                 regcache->cooked_write (regnum, regval);
2143               }
2144             if (readbuf != NULL)
2145               {
2146                 regcache->cooked_read (regnum, regval);
2147                 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG
2148                       && offset == 0)
2149                     memcpy (readbuf, regval + tdep->wordsize - len, len);
2150                 else
2151                     memcpy (readbuf + offset, regval, len);
2152               }
2153           }
2154       return RETURN_VALUE_REGISTER_CONVENTION;
2155     }
2156 
2157   /* Handle plain base types.  */
2158   if (ppc64_sysv_abi_return_value_base (gdbarch, valtype, regcache,
2159                                                   readbuf, writebuf, 0))
2160     return RETURN_VALUE_REGISTER_CONVENTION;
2161 
2162   return RETURN_VALUE_STRUCT_CONVENTION;
2163 }
2164 
2165 CORE_ADDR
ppc_sysv_get_return_buf_addr(struct type * val_type,const frame_info_ptr & cur_frame)2166 ppc_sysv_get_return_buf_addr (struct type *val_type,
2167                                     const frame_info_ptr &cur_frame)
2168 {
2169   /* The PowerPC ABI specifies aggregates that are not returned by value
2170      are returned in a storage buffer provided by the caller.  The
2171      address of the storage buffer is provided as a hidden first input
2172      argument in register r3.  The PowerPC ABI does not guarantee that
2173      register r3 will not be changed while executing the function.  Hence, it
2174      cannot be assumed that r3 will still contain the address of the storage
2175      buffer when execution reaches the end of the function.
2176 
2177      This function attempts to determine the value of r3 on entry to the
2178      function using the DW_OP_entry_value DWARF entries.  This requires
2179      compiling the user program with -fvar-tracking to resolve the
2180      DW_TAG_call_sites in the binary file.  */
2181 
2182   union call_site_parameter_u kind_u;
2183   enum call_site_parameter_kind kind;
2184   CORE_ADDR return_val = 0;
2185 
2186   kind_u.dwarf_reg = 3;  /* First passed arg/return value is in r3.  */
2187   kind = CALL_SITE_PARAMETER_DWARF_REG;
2188 
2189   /* val_type is the type of the return value.  Need the pointer type
2190      to the return value.  */
2191   val_type = lookup_pointer_type (val_type);
2192 
2193   try
2194     {
2195       return_val = value_as_address (value_of_dwarf_reg_entry (val_type,
2196                                                                              cur_frame,
2197                                                                              kind, kind_u));
2198     }
2199   catch (const gdb_exception_error &e)
2200     {
2201       warning ("Cannot determine the function return value.\n"
2202                  "Try compiling with -fvar-tracking.");
2203     }
2204   return return_val;
2205 }
2206