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