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