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