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