gdb/ppc-sysv-tdep.c - gdb

Data types defined

Functions defined

Source code

  1. /* Target-dependent code for PowerPC systems using the SVR4 ABI
  2.    for GDB, the GNU debugger.

  3.    Copyright (C) 2000-2015 Free Software Foundation, Inc.

  4.    This file is part of GDB.

  5.    This program is free software; you can redistribute it and/or modify
  6.    it under the terms of the GNU General Public License as published by
  7.    the Free Software Foundation; either version 3 of the License, or
  8.    (at your option) any later version.

  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.

  13.    You should have received a copy of the GNU General Public License
  14.    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

  15. #include "defs.h"
  16. #include "gdbcore.h"
  17. #include "inferior.h"
  18. #include "regcache.h"
  19. #include "value.h"
  20. #include "ppc-tdep.h"
  21. #include "target.h"
  22. #include "objfiles.h"
  23. #include "infcall.h"
  24. #include "dwarf2.h"


  25. /* Check whether FTPYE is a (pointer to) function type that should use
  26.    the OpenCL vector ABI.  */

  27. static int
  28. ppc_sysv_use_opencl_abi (struct type *ftype)
  29. {
  30.   ftype = check_typedef (ftype);

  31.   if (TYPE_CODE (ftype) == TYPE_CODE_PTR)
  32.     ftype = check_typedef (TYPE_TARGET_TYPE (ftype));

  33.   return (TYPE_CODE (ftype) == TYPE_CODE_FUNC
  34.           && TYPE_CALLING_CONVENTION (ftype) == DW_CC_GDB_IBM_OpenCL);
  35. }

  36. /* Pass the arguments in either registers, or in the stack.  Using the
  37.    ppc sysv ABI, the first eight words of the argument list (that might
  38.    be less than eight parameters if some parameters occupy more than one
  39.    word) are passed in r3..r10 registers.  float and double parameters are
  40.    passed in fpr's, in addition to that.  Rest of the parameters if any
  41.    are passed in user stack.

  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. CORE_ADDR
  46. ppc_sysv_abi_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
  47.                               struct regcache *regcache, CORE_ADDR bp_addr,
  48.                               int nargs, struct value **args, CORE_ADDR sp,
  49.                               int struct_return, CORE_ADDR struct_addr)
  50. {
  51.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
  52.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  53.   int opencl_abi = ppc_sysv_use_opencl_abi (value_type (function));
  54.   ULONGEST saved_sp;
  55.   int argspace = 0;                /* 0 is an initial wrong guess.  */
  56.   int write_pass;

  57.   gdb_assert (tdep->wordsize == 4);

  58.   regcache_cooked_read_unsigned (regcache, gdbarch_sp_regnum (gdbarch),
  59.                                  &saved_sp);

  60.   /* Go through the argument list twice.

  61.      Pass 1: Figure out how much new stack space is required for
  62.      arguments and pushed values.  Unlike the PowerOpen ABI, the SysV
  63.      ABI doesn't reserve any extra space for parameters which are put
  64.      in registers, but does always push structures and then pass their
  65.      address.

  66.      Pass 2: Replay the same computation but this time also write the
  67.      values out to the target.  */

  68.   for (write_pass = 0; write_pass < 2; write_pass++)
  69.     {
  70.       int argno;
  71.       /* Next available floating point register for float and double
  72.          arguments.  */
  73.       int freg = 1;
  74.       /* Next available general register for non-float, non-vector
  75.          arguments.  */
  76.       int greg = 3;
  77.       /* Next available vector register for vector arguments.  */
  78.       int vreg = 2;
  79.       /* Arguments start above the "LR save word" and "Back chain".  */
  80.       int argoffset = 2 * tdep->wordsize;
  81.       /* Structures start after the arguments.  */
  82.       int structoffset = argoffset + argspace;

  83.       /* If the function is returning a `struct', then the first word
  84.          (which will be passed in r3) is used for struct return
  85.          address.  In that case we should advance one word and start
  86.          from r4 register to copy parameters.  */
  87.       if (struct_return)
  88.         {
  89.           if (write_pass)
  90.             regcache_cooked_write_signed (regcache,
  91.                                           tdep->ppc_gp0_regnum + greg,
  92.                                           struct_addr);
  93.           greg++;
  94.         }

  95.       for (argno = 0; argno < nargs; argno++)
  96.         {
  97.           struct value *arg = args[argno];
  98.           struct type *type = check_typedef (value_type (arg));
  99.           int len = TYPE_LENGTH (type);
  100.           const bfd_byte *val = value_contents (arg);

  101.           if (TYPE_CODE (type) == TYPE_CODE_FLT && len <= 8
  102.               && !tdep->soft_float)
  103.             {
  104.               /* Floating point value converted to "double" then
  105.                  passed in an FP register, when the registers run out,
  106.                  8 byte aligned stack is used.  */
  107.               if (freg <= 8)
  108.                 {
  109.                   if (write_pass)
  110.                     {
  111.                       /* Always store the floating point value using
  112.                          the register's floating-point format.  */
  113.                       gdb_byte regval[MAX_REGISTER_SIZE];
  114.                       struct type *regtype
  115.                         = register_type (gdbarch, tdep->ppc_fp0_regnum + freg);
  116.                       convert_typed_floating (val, type, regval, regtype);
  117.                       regcache_cooked_write (regcache,
  118.                                              tdep->ppc_fp0_regnum + freg,
  119.                                              regval);
  120.                     }
  121.                   freg++;
  122.                 }
  123.               else
  124.                 {
  125.                   /* The SysV ABI tells us to convert floats to
  126.                      doubles before writing them to an 8 byte aligned
  127.                      stack location.  Unfortunately GCC does not do
  128.                      that, and stores floats into 4 byte aligned
  129.                      locations without converting them to doubles.
  130.                      Since there is no know compiler that actually
  131.                      follows the ABI here, we implement the GCC
  132.                      convention.  */

  133.                   /* Align to 4 bytes or 8 bytes depending on the type of
  134.                      the argument (float or double).  */
  135.                   argoffset = align_up (argoffset, len);
  136.                   if (write_pass)
  137.                       write_memory (sp + argoffset, val, len);
  138.                   argoffset += len;
  139.                 }
  140.             }
  141.           else if (TYPE_CODE (type) == TYPE_CODE_FLT
  142.                    && len == 16
  143.                    && !tdep->soft_float
  144.                    && (gdbarch_long_double_format (gdbarch)
  145.                        == floatformats_ibm_long_double))
  146.             {
  147.               /* IBM long double passed in two FP registers if
  148.                  available, otherwise 8-byte aligned stack.  */
  149.               if (freg <= 7)
  150.                 {
  151.                   if (write_pass)
  152.                     {
  153.                       regcache_cooked_write (regcache,
  154.                                              tdep->ppc_fp0_regnum + freg,
  155.                                              val);
  156.                       regcache_cooked_write (regcache,
  157.                                              tdep->ppc_fp0_regnum + freg + 1,
  158.                                              val + 8);
  159.                     }
  160.                   freg += 2;
  161.                 }
  162.               else
  163.                 {
  164.                   argoffset = align_up (argoffset, 8);
  165.                   if (write_pass)
  166.                     write_memory (sp + argoffset, val, len);
  167.                   argoffset += 16;
  168.                 }
  169.             }
  170.           else if (len == 8
  171.                    && (TYPE_CODE (type) == TYPE_CODE_INT        /* long long */
  172.                        || TYPE_CODE (type) == TYPE_CODE_FLT        /* double */
  173.                        || (TYPE_CODE (type) == TYPE_CODE_DECFLOAT
  174.                            && tdep->soft_float)))
  175.             {
  176.               /* "long long" or soft-float "double" or "_Decimal64"
  177.                  passed in an odd/even register pair with the low
  178.                  addressed word in the odd register and the high
  179.                  addressed word in the even register, or when the
  180.                  registers run out an 8 byte aligned stack
  181.                  location.  */
  182.               if (greg > 9)
  183.                 {
  184.                   /* Just in case GREG was 10.  */
  185.                   greg = 11;
  186.                   argoffset = align_up (argoffset, 8);
  187.                   if (write_pass)
  188.                     write_memory (sp + argoffset, val, len);
  189.                   argoffset += 8;
  190.                 }
  191.               else
  192.                 {
  193.                   /* Must start on an odd register - r3/r4 etc.  */
  194.                   if ((greg & 1) == 0)
  195.                     greg++;
  196.                   if (write_pass)
  197.                     {
  198.                       regcache_cooked_write (regcache,
  199.                                              tdep->ppc_gp0_regnum + greg + 0,
  200.                                              val + 0);
  201.                       regcache_cooked_write (regcache,
  202.                                              tdep->ppc_gp0_regnum + greg + 1,
  203.                                              val + 4);
  204.                     }
  205.                   greg += 2;
  206.                 }
  207.             }
  208.           else if (len == 16
  209.                    && ((TYPE_CODE (type) == TYPE_CODE_FLT
  210.                         && (gdbarch_long_double_format (gdbarch)
  211.                             == floatformats_ibm_long_double))
  212.                        || (TYPE_CODE (type) == TYPE_CODE_DECFLOAT
  213.                            && tdep->soft_float)))
  214.             {
  215.               /* Soft-float IBM long double or _Decimal128 passed in
  216.                  four consecutive registers, or on the stack.  The
  217.                  registers are not necessarily odd/even pairs.  */
  218.               if (greg > 7)
  219.                 {
  220.                   greg = 11;
  221.                   argoffset = align_up (argoffset, 8);
  222.                   if (write_pass)
  223.                     write_memory (sp + argoffset, val, len);
  224.                   argoffset += 16;
  225.                 }
  226.               else
  227.                 {
  228.                   if (write_pass)
  229.                     {
  230.                       regcache_cooked_write (regcache,
  231.                                              tdep->ppc_gp0_regnum + greg + 0,
  232.                                              val + 0);
  233.                       regcache_cooked_write (regcache,
  234.                                              tdep->ppc_gp0_regnum + greg + 1,
  235.                                              val + 4);
  236.                       regcache_cooked_write (regcache,
  237.                                              tdep->ppc_gp0_regnum + greg + 2,
  238.                                              val + 8);
  239.                       regcache_cooked_write (regcache,
  240.                                              tdep->ppc_gp0_regnum + greg + 3,
  241.                                              val + 12);
  242.                     }
  243.                   greg += 4;
  244.                 }
  245.             }
  246.           else if (TYPE_CODE (type) == TYPE_CODE_DECFLOAT && len <= 8
  247.                    && !tdep->soft_float)
  248.             {
  249.               /* 32-bit and 64-bit decimal floats go in f1 .. f8.  They can
  250.                  end up in memory.  */

  251.               if (freg <= 8)
  252.                 {
  253.                   if (write_pass)
  254.                     {
  255.                       gdb_byte regval[MAX_REGISTER_SIZE];
  256.                       const gdb_byte *p;

  257.                       /* 32-bit decimal floats are right aligned in the
  258.                          doubleword.  */
  259.                       if (TYPE_LENGTH (type) == 4)
  260.                       {
  261.                         memcpy (regval + 4, val, 4);
  262.                         p = regval;
  263.                       }
  264.                       else
  265.                         p = val;

  266.                       regcache_cooked_write (regcache,
  267.                           tdep->ppc_fp0_regnum + freg, p);
  268.                     }

  269.                   freg++;
  270.                 }
  271.               else
  272.                 {
  273.                   argoffset = align_up (argoffset, len);

  274.                   if (write_pass)
  275.                     /* Write value in the stack's parameter save area.  */
  276.                     write_memory (sp + argoffset, val, len);

  277.                   argoffset += len;
  278.                 }
  279.             }
  280.           else if (TYPE_CODE (type) == TYPE_CODE_DECFLOAT && len == 16
  281.                    && !tdep->soft_float)
  282.             {
  283.               /* 128-bit decimal floats go in f2 .. f7, always in even/odd
  284.                  pairs.  They can end up in memory, using two doublewords.  */

  285.               if (freg <= 6)
  286.                 {
  287.                   /* Make sure freg is even.  */
  288.                   freg += freg & 1;

  289.                   if (write_pass)
  290.                     {
  291.                       regcache_cooked_write (regcache,
  292.                                              tdep->ppc_fp0_regnum + freg, val);
  293.                       regcache_cooked_write (regcache,
  294.                           tdep->ppc_fp0_regnum + freg + 1, val + 8);
  295.                     }
  296.                 }
  297.               else
  298.                 {
  299.                   argoffset = align_up (argoffset, 8);

  300.                   if (write_pass)
  301.                     write_memory (sp + argoffset, val, 16);

  302.                   argoffset += 16;
  303.                 }

  304.               /* If a 128-bit decimal float goes to the stack because only f7
  305.                  and f8 are free (thus there's no even/odd register pair
  306.                  available), these registers should be marked as occupied.
  307.                  Hence we increase freg even when writing to memory.  */
  308.               freg += 2;
  309.             }
  310.           else if (len < 16
  311.                    && TYPE_CODE (type) == TYPE_CODE_ARRAY
  312.                    && TYPE_VECTOR (type)
  313.                    && opencl_abi)
  314.             {
  315.               /* OpenCL vectors shorter than 16 bytes are passed as if
  316.                  a series of independent scalars.  */
  317.               struct type *eltype = check_typedef (TYPE_TARGET_TYPE (type));
  318.               int i, nelt = TYPE_LENGTH (type) / TYPE_LENGTH (eltype);

  319.               for (i = 0; i < nelt; i++)
  320.                 {
  321.                   const gdb_byte *elval = val + i * TYPE_LENGTH (eltype);

  322.                   if (TYPE_CODE (eltype) == TYPE_CODE_FLT && !tdep->soft_float)
  323.                     {
  324.                       if (freg <= 8)
  325.                         {
  326.                           if (write_pass)
  327.                             {
  328.                               int regnum = tdep->ppc_fp0_regnum + freg;
  329.                               gdb_byte regval[MAX_REGISTER_SIZE];
  330.                               struct type *regtype
  331.                                 = register_type (gdbarch, regnum);
  332.                               convert_typed_floating (elval, eltype,
  333.                                                       regval, regtype);
  334.                               regcache_cooked_write (regcache, regnum, regval);
  335.                             }
  336.                           freg++;
  337.                         }
  338.                       else
  339.                         {
  340.                           argoffset = align_up (argoffset, len);
  341.                           if (write_pass)
  342.                             write_memory (sp + argoffset, val, len);
  343.                           argoffset += len;
  344.                         }
  345.                     }
  346.                   else if (TYPE_LENGTH (eltype) == 8)
  347.                     {
  348.                       if (greg > 9)
  349.                         {
  350.                           /* Just in case GREG was 10.  */
  351.                           greg = 11;
  352.                           argoffset = align_up (argoffset, 8);
  353.                           if (write_pass)
  354.                             write_memory (sp + argoffset, elval,
  355.                                           TYPE_LENGTH (eltype));
  356.                           argoffset += 8;
  357.                         }
  358.                       else
  359.                         {
  360.                           /* Must start on an odd register - r3/r4 etc.  */
  361.                           if ((greg & 1) == 0)
  362.                             greg++;
  363.                           if (write_pass)
  364.                             {
  365.                               int regnum = tdep->ppc_gp0_regnum + greg;
  366.                               regcache_cooked_write (regcache,
  367.                                                      regnum + 0, elval + 0);
  368.                               regcache_cooked_write (regcache,
  369.                                                      regnum + 1, elval + 4);
  370.                             }
  371.                           greg += 2;
  372.                         }
  373.                     }
  374.                   else
  375.                     {
  376.                       gdb_byte word[MAX_REGISTER_SIZE];
  377.                       store_unsigned_integer (word, tdep->wordsize, byte_order,
  378.                                               unpack_long (eltype, elval));

  379.                       if (greg <= 10)
  380.                         {
  381.                           if (write_pass)
  382.                             regcache_cooked_write (regcache,
  383.                                                    tdep->ppc_gp0_regnum + greg,
  384.                                                    word);
  385.                           greg++;
  386.                         }
  387.                       else
  388.                         {
  389.                           argoffset = align_up (argoffset, tdep->wordsize);
  390.                           if (write_pass)
  391.                             write_memory (sp + argoffset, word, tdep->wordsize);
  392.                           argoffset += tdep->wordsize;
  393.                         }
  394.                     }
  395.                 }
  396.             }
  397.           else if (len >= 16
  398.                    && TYPE_CODE (type) == TYPE_CODE_ARRAY
  399.                    && TYPE_VECTOR (type)
  400.                    && opencl_abi)
  401.             {
  402.               /* OpenCL vectors 16 bytes or longer are passed as if
  403.                  a series of AltiVec vectors.  */
  404.               int i;

  405.               for (i = 0; i < len / 16; i++)
  406.                 {
  407.                   const gdb_byte *elval = val + i * 16;

  408.                   if (vreg <= 13)
  409.                     {
  410.                       if (write_pass)
  411.                         regcache_cooked_write (regcache,
  412.                                                tdep->ppc_vr0_regnum + vreg,
  413.                                                elval);
  414.                       vreg++;
  415.                     }
  416.                   else
  417.                     {
  418.                       argoffset = align_up (argoffset, 16);
  419.                       if (write_pass)
  420.                         write_memory (sp + argoffset, elval, 16);
  421.                       argoffset += 16;
  422.                     }
  423.                 }
  424.             }
  425.           else if (len == 16
  426.                    && TYPE_CODE (type) == TYPE_CODE_ARRAY
  427.                    && TYPE_VECTOR (type)
  428.                    && tdep->vector_abi == POWERPC_VEC_ALTIVEC)
  429.             {
  430.               /* Vector parameter passed in an Altivec register, or
  431.                  when that runs out, 16 byte aligned stack location.  */
  432.               if (vreg <= 13)
  433.                 {
  434.                   if (write_pass)
  435.                     regcache_cooked_write (regcache,
  436.                                            tdep->ppc_vr0_regnum + vreg, val);
  437.                   vreg++;
  438.                 }
  439.               else
  440.                 {
  441.                   argoffset = align_up (argoffset, 16);
  442.                   if (write_pass)
  443.                     write_memory (sp + argoffset, val, 16);
  444.                   argoffset += 16;
  445.                 }
  446.             }
  447.           else if (len == 8
  448.                    && TYPE_CODE (type) == TYPE_CODE_ARRAY
  449.                    && TYPE_VECTOR (type)
  450.                    && tdep->vector_abi == POWERPC_VEC_SPE)
  451.             {
  452.               /* Vector parameter passed in an e500 register, or when
  453.                  that runs out, 8 byte aligned stack location.  Note
  454.                  that since e500 vector and general purpose registers
  455.                  both map onto the same underlying register set, a
  456.                  "greg" and not a "vreg" is consumed here.  A cooked
  457.                  write stores the value in the correct locations
  458.                  within the raw register cache.  */
  459.               if (greg <= 10)
  460.                 {
  461.                   if (write_pass)
  462.                     regcache_cooked_write (regcache,
  463.                                            tdep->ppc_ev0_regnum + greg, val);
  464.                   greg++;
  465.                 }
  466.               else
  467.                 {
  468.                   argoffset = align_up (argoffset, 8);
  469.                   if (write_pass)
  470.                     write_memory (sp + argoffset, val, 8);
  471.                   argoffset += 8;
  472.                 }
  473.             }
  474.           else
  475.             {
  476.               /* Reduce the parameter down to something that fits in a
  477.                  "word".  */
  478.               gdb_byte word[MAX_REGISTER_SIZE];
  479.               memset (word, 0, MAX_REGISTER_SIZE);
  480.               if (len > tdep->wordsize
  481.                   || TYPE_CODE (type) == TYPE_CODE_STRUCT
  482.                   || TYPE_CODE (type) == TYPE_CODE_UNION)
  483.                 {
  484.                   /* Structs and large values are put in an
  485.                      aligned stack slot ...  */
  486.                   if (TYPE_CODE (type) == TYPE_CODE_ARRAY
  487.                       && TYPE_VECTOR (type)
  488.                       && len >= 16)
  489.                     structoffset = align_up (structoffset, 16);
  490.                   else
  491.                     structoffset = align_up (structoffset, 8);

  492.                   if (write_pass)
  493.                     write_memory (sp + structoffset, val, len);
  494.                   /* ... and then a "word" pointing to that address is
  495.                      passed as the parameter.  */
  496.                   store_unsigned_integer (word, tdep->wordsize, byte_order,
  497.                                           sp + structoffset);
  498.                   structoffset += len;
  499.                 }
  500.               else if (TYPE_CODE (type) == TYPE_CODE_INT)
  501.                 /* Sign or zero extend the "int" into a "word".  */
  502.                 store_unsigned_integer (word, tdep->wordsize, byte_order,
  503.                                         unpack_long (type, val));
  504.               else
  505.                 /* Always goes in the low address.  */
  506.                 memcpy (word, val, len);
  507.               /* Store that "word" in a register, or on the stack.
  508.                  The words have "4" byte alignment.  */
  509.               if (greg <= 10)
  510.                 {
  511.                   if (write_pass)
  512.                     regcache_cooked_write (regcache,
  513.                                            tdep->ppc_gp0_regnum + greg, word);
  514.                   greg++;
  515.                 }
  516.               else
  517.                 {
  518.                   argoffset = align_up (argoffset, tdep->wordsize);
  519.                   if (write_pass)
  520.                     write_memory (sp + argoffset, word, tdep->wordsize);
  521.                   argoffset += tdep->wordsize;
  522.                 }
  523.             }
  524.         }

  525.       /* Compute the actual stack space requirements.  */
  526.       if (!write_pass)
  527.         {
  528.           /* Remember the amount of space needed by the arguments.  */
  529.           argspace = argoffset;
  530.           /* Allocate space for both the arguments and the structures.  */
  531.           sp -= (argoffset + structoffset);
  532.           /* Ensure that the stack is still 16 byte aligned.  */
  533.           sp = align_down (sp, 16);
  534.         }

  535.       /* The psABI says that "A caller of a function that takes a
  536.          variable argument list shall set condition register bit 6 to
  537.          1 if it passes one or more arguments in the floating-point
  538.          registers.  It is strongly recommended that the caller set the
  539.          bit to 0 otherwise..."  Doing this for normal functions too
  540.          shouldn't hurt.  */
  541.       if (write_pass)
  542.         {
  543.           ULONGEST cr;

  544.           regcache_cooked_read_unsigned (regcache, tdep->ppc_cr_regnum, &cr);
  545.           if (freg > 1)
  546.             cr |= 0x02000000;
  547.           else
  548.             cr &= ~0x02000000;
  549.           regcache_cooked_write_unsigned (regcache, tdep->ppc_cr_regnum, cr);
  550.         }
  551.     }

  552.   /* Update %sp.   */
  553.   regcache_cooked_write_signed (regcache, gdbarch_sp_regnum (gdbarch), sp);

  554.   /* Write the backchain (it occupies WORDSIZED bytes).  */
  555.   write_memory_signed_integer (sp, tdep->wordsize, byte_order, saved_sp);

  556.   /* Point the inferior function call's return address at the dummy's
  557.      breakpoint.  */
  558.   regcache_cooked_write_signed (regcache, tdep->ppc_lr_regnum, bp_addr);

  559.   return sp;
  560. }

  561. /* Handle the return-value conventions for Decimal Floating Point values.  */
  562. static int
  563. get_decimal_float_return_value (struct gdbarch *gdbarch, struct type *valtype,
  564.                                 struct regcache *regcache, gdb_byte *readbuf,
  565.                                 const gdb_byte *writebuf)
  566. {
  567.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);

  568.   gdb_assert (TYPE_CODE (valtype) == TYPE_CODE_DECFLOAT);

  569.   /* 32-bit and 64-bit decimal floats in f1.  */
  570.   if (TYPE_LENGTH (valtype) <= 8)
  571.     {
  572.       if (writebuf != NULL)
  573.         {
  574.           gdb_byte regval[MAX_REGISTER_SIZE];
  575.           const gdb_byte *p;

  576.           /* 32-bit decimal float is right aligned in the doubleword.  */
  577.           if (TYPE_LENGTH (valtype) == 4)
  578.             {
  579.               memcpy (regval + 4, writebuf, 4);
  580.               p = regval;
  581.             }
  582.           else
  583.             p = writebuf;

  584.           regcache_cooked_write (regcache, tdep->ppc_fp0_regnum + 1, p);
  585.         }
  586.       if (readbuf != NULL)
  587.         {
  588.           regcache_cooked_read (regcache, tdep->ppc_fp0_regnum + 1, readbuf);

  589.           /* Left align 32-bit decimal float.  */
  590.           if (TYPE_LENGTH (valtype) == 4)
  591.             memcpy (readbuf, readbuf + 4, 4);
  592.         }
  593.     }
  594.   /* 128-bit decimal floats in f2,f3.  */
  595.   else if (TYPE_LENGTH (valtype) == 16)
  596.     {
  597.       if (writebuf != NULL || readbuf != NULL)
  598.         {
  599.           int i;

  600.           for (i = 0; i < 2; i++)
  601.             {
  602.               if (writebuf != NULL)
  603.                 regcache_cooked_write (regcache, tdep->ppc_fp0_regnum + 2 + i,
  604.                                        writebuf + i * 8);
  605.               if (readbuf != NULL)
  606.                 regcache_cooked_read (regcache, tdep->ppc_fp0_regnum + 2 + i,
  607.                                       readbuf + i * 8);
  608.             }
  609.         }
  610.     }
  611.   else
  612.     /* Can't happen.  */
  613.     internal_error (__FILE__, __LINE__, _("Unknown decimal float size."));

  614.   return RETURN_VALUE_REGISTER_CONVENTION;
  615. }

  616. /* Handle the return-value conventions specified by the SysV 32-bit
  617.    PowerPC ABI (including all the supplements):

  618.    no floating-point: floating-point values returned using 32-bit
  619.    general-purpose registers.

  620.    Altivec: 128-bit vectors returned using vector registers.

  621.    e500: 64-bit vectors returned using the full full 64 bit EV
  622.    register, floating-point values returned using 32-bit
  623.    general-purpose registers.

  624.    GCC (broken): Small struct values right (instead of left) aligned
  625.    when returned in general-purpose registers.  */

  626. static enum return_value_convention
  627. do_ppc_sysv_return_value (struct gdbarch *gdbarch, struct type *func_type,
  628.                           struct type *type, struct regcache *regcache,
  629.                           gdb_byte *readbuf, const gdb_byte *writebuf,
  630.                           int broken_gcc)
  631. {
  632.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
  633.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  634.   int opencl_abi = func_type? ppc_sysv_use_opencl_abi (func_type) : 0;

  635.   gdb_assert (tdep->wordsize == 4);

  636.   if (TYPE_CODE (type) == TYPE_CODE_FLT
  637.       && TYPE_LENGTH (type) <= 8
  638.       && !tdep->soft_float)
  639.     {
  640.       if (readbuf)
  641.         {
  642.           /* Floats and doubles stored in "f1".  Convert the value to
  643.              the required type.  */
  644.           gdb_byte regval[MAX_REGISTER_SIZE];
  645.           struct type *regtype = register_type (gdbarch,
  646.                                                 tdep->ppc_fp0_regnum + 1);
  647.           regcache_cooked_read (regcache, tdep->ppc_fp0_regnum + 1, regval);
  648.           convert_typed_floating (regval, regtype, readbuf, type);
  649.         }
  650.       if (writebuf)
  651.         {
  652.           /* Floats and doubles stored in "f1".  Convert the value to
  653.              the register's "double" type.  */
  654.           gdb_byte regval[MAX_REGISTER_SIZE];
  655.           struct type *regtype = register_type (gdbarch, tdep->ppc_fp0_regnum);
  656.           convert_typed_floating (writebuf, type, regval, regtype);
  657.           regcache_cooked_write (regcache, tdep->ppc_fp0_regnum + 1, regval);
  658.         }
  659.       return RETURN_VALUE_REGISTER_CONVENTION;
  660.     }
  661.   if (TYPE_CODE (type) == TYPE_CODE_FLT
  662.       && TYPE_LENGTH (type) == 16
  663.       && !tdep->soft_float
  664.       && (gdbarch_long_double_format (gdbarch)
  665.           == floatformats_ibm_long_double))
  666.     {
  667.       /* IBM long double stored in f1 and f2.  */
  668.       if (readbuf)
  669.         {
  670.           regcache_cooked_read (regcache, tdep->ppc_fp0_regnum + 1, readbuf);
  671.           regcache_cooked_read (regcache, tdep->ppc_fp0_regnum + 2,
  672.                                 readbuf + 8);
  673.         }
  674.       if (writebuf)
  675.         {
  676.           regcache_cooked_write (regcache, tdep->ppc_fp0_regnum + 1, writebuf);
  677.           regcache_cooked_write (regcache, tdep->ppc_fp0_regnum + 2,
  678.                                  writebuf + 8);
  679.         }
  680.       return RETURN_VALUE_REGISTER_CONVENTION;
  681.     }
  682.   if (TYPE_LENGTH (type) == 16
  683.       && ((TYPE_CODE (type) == TYPE_CODE_FLT
  684.            && (gdbarch_long_double_format (gdbarch)
  685.                == floatformats_ibm_long_double))
  686.           || (TYPE_CODE (type) == TYPE_CODE_DECFLOAT && tdep->soft_float)))
  687.     {
  688.       /* Soft-float IBM long double or _Decimal128 stored in r3, r4,
  689.          r5, r6.  */
  690.       if (readbuf)
  691.         {
  692.           regcache_cooked_read (regcache, tdep->ppc_gp0_regnum + 3, readbuf);
  693.           regcache_cooked_read (regcache, tdep->ppc_gp0_regnum + 4,
  694.                                 readbuf + 4);
  695.           regcache_cooked_read (regcache, tdep->ppc_gp0_regnum + 5,
  696.                                 readbuf + 8);
  697.           regcache_cooked_read (regcache, tdep->ppc_gp0_regnum + 6,
  698.                                 readbuf + 12);
  699.         }
  700.       if (writebuf)
  701.         {
  702.           regcache_cooked_write (regcache, tdep->ppc_gp0_regnum + 3, writebuf);
  703.           regcache_cooked_write (regcache, tdep->ppc_gp0_regnum + 4,
  704.                                  writebuf + 4);
  705.           regcache_cooked_write (regcache, tdep->ppc_gp0_regnum + 5,
  706.                                  writebuf + 8);
  707.           regcache_cooked_write (regcache, tdep->ppc_gp0_regnum + 6,
  708.                                  writebuf + 12);
  709.         }
  710.       return RETURN_VALUE_REGISTER_CONVENTION;
  711.     }
  712.   if ((TYPE_CODE (type) == TYPE_CODE_INT && TYPE_LENGTH (type) == 8)
  713.       || (TYPE_CODE (type) == TYPE_CODE_FLT && TYPE_LENGTH (type) == 8)
  714.       || (TYPE_CODE (type) == TYPE_CODE_DECFLOAT && TYPE_LENGTH (type) == 8
  715.           && tdep->soft_float))
  716.     {
  717.       if (readbuf)
  718.         {
  719.           /* A long long, double or _Decimal64 stored in the 32 bit
  720.              r3/r4.  */
  721.           regcache_cooked_read (regcache, tdep->ppc_gp0_regnum + 3,
  722.                                 readbuf + 0);
  723.           regcache_cooked_read (regcache, tdep->ppc_gp0_regnum + 4,
  724.                                 readbuf + 4);
  725.         }
  726.       if (writebuf)
  727.         {
  728.           /* A long long, double or _Decimal64 stored in the 32 bit
  729.              r3/r4.  */
  730.           regcache_cooked_write (regcache, tdep->ppc_gp0_regnum + 3,
  731.                                  writebuf + 0);
  732.           regcache_cooked_write (regcache, tdep->ppc_gp0_regnum + 4,
  733.                                  writebuf + 4);
  734.         }
  735.       return RETURN_VALUE_REGISTER_CONVENTION;
  736.     }
  737.   if (TYPE_CODE (type) == TYPE_CODE_DECFLOAT && !tdep->soft_float)
  738.     return get_decimal_float_return_value (gdbarch, type, regcache, readbuf,
  739.                                            writebuf);
  740.   else if ((TYPE_CODE (type) == TYPE_CODE_INT
  741.             || TYPE_CODE (type) == TYPE_CODE_CHAR
  742.             || TYPE_CODE (type) == TYPE_CODE_BOOL
  743.             || TYPE_CODE (type) == TYPE_CODE_PTR
  744.             || TYPE_CODE (type) == TYPE_CODE_REF
  745.             || TYPE_CODE (type) == TYPE_CODE_ENUM)
  746.            && TYPE_LENGTH (type) <= tdep->wordsize)
  747.     {
  748.       if (readbuf)
  749.         {
  750.           /* Some sort of integer stored in r3.  Since TYPE isn't
  751.              bigger than the register, sign extension isn't a problem
  752.              - just do everything unsigned.  */
  753.           ULONGEST regval;
  754.           regcache_cooked_read_unsigned (regcache, tdep->ppc_gp0_regnum + 3,
  755.                                          &regval);
  756.           store_unsigned_integer (readbuf, TYPE_LENGTH (type), byte_order,
  757.                                   regval);
  758.         }
  759.       if (writebuf)
  760.         {
  761.           /* Some sort of integer stored in r3.  Use unpack_long since
  762.              that should handle any required sign extension.  */
  763.           regcache_cooked_write_unsigned (regcache, tdep->ppc_gp0_regnum + 3,
  764.                                           unpack_long (type, writebuf));
  765.         }
  766.       return RETURN_VALUE_REGISTER_CONVENTION;
  767.     }
  768.   /* OpenCL vectors < 16 bytes are returned as distinct
  769.      scalars in f1..f2 or r3..r10.  */
  770.   if (TYPE_CODE (type) == TYPE_CODE_ARRAY
  771.       && TYPE_VECTOR (type)
  772.       && TYPE_LENGTH (type) < 16
  773.       && opencl_abi)
  774.     {
  775.       struct type *eltype = check_typedef (TYPE_TARGET_TYPE (type));
  776.       int i, nelt = TYPE_LENGTH (type) / TYPE_LENGTH (eltype);

  777.       for (i = 0; i < nelt; i++)
  778.         {
  779.           int offset = i * TYPE_LENGTH (eltype);

  780.           if (TYPE_CODE (eltype) == TYPE_CODE_FLT)
  781.             {
  782.               int regnum = tdep->ppc_fp0_regnum + 1 + i;
  783.               gdb_byte regval[MAX_REGISTER_SIZE];
  784.               struct type *regtype = register_type (gdbarch, regnum);

  785.               if (writebuf != NULL)
  786.                 {
  787.                   convert_typed_floating (writebuf + offset, eltype,
  788.                                           regval, regtype);
  789.                   regcache_cooked_write (regcache, regnum, regval);
  790.                 }
  791.               if (readbuf != NULL)
  792.                 {
  793.                   regcache_cooked_read (regcache, regnum, regval);
  794.                   convert_typed_floating (regval, regtype,
  795.                                           readbuf + offset, eltype);
  796.                 }
  797.             }
  798.           else
  799.             {
  800.               int regnum = tdep->ppc_gp0_regnum + 3 + i;
  801.               ULONGEST regval;

  802.               if (writebuf != NULL)
  803.                 {
  804.                   regval = unpack_long (eltype, writebuf + offset);
  805.                   regcache_cooked_write_unsigned (regcache, regnum, regval);
  806.                 }
  807.               if (readbuf != NULL)
  808.                 {
  809.                   regcache_cooked_read_unsigned (regcache, regnum, &regval);
  810.                   store_unsigned_integer (readbuf + offset,
  811.                                           TYPE_LENGTH (eltype), byte_order,
  812.                                           regval);
  813.                 }
  814.             }
  815.         }

  816.       return RETURN_VALUE_REGISTER_CONVENTION;
  817.     }
  818.   /* OpenCL vectors >= 16 bytes are returned in v2..v9.  */
  819.   if (TYPE_CODE (type) == TYPE_CODE_ARRAY
  820.       && TYPE_VECTOR (type)
  821.       && TYPE_LENGTH (type) >= 16
  822.       && opencl_abi)
  823.     {
  824.       int n_regs = TYPE_LENGTH (type) / 16;
  825.       int i;

  826.       for (i = 0; i < n_regs; i++)
  827.         {
  828.           int offset = i * 16;
  829.           int regnum = tdep->ppc_vr0_regnum + 2 + i;

  830.           if (writebuf != NULL)
  831.             regcache_cooked_write (regcache, regnum, writebuf + offset);
  832.           if (readbuf != NULL)
  833.             regcache_cooked_read (regcache, regnum, readbuf + offset);
  834.         }

  835.       return RETURN_VALUE_REGISTER_CONVENTION;
  836.     }
  837.   if (TYPE_LENGTH (type) == 16
  838.       && TYPE_CODE (type) == TYPE_CODE_ARRAY
  839.       && TYPE_VECTOR (type)
  840.       && tdep->vector_abi == POWERPC_VEC_ALTIVEC)
  841.     {
  842.       if (readbuf)
  843.         {
  844.           /* Altivec places the return value in "v2".  */
  845.           regcache_cooked_read (regcache, tdep->ppc_vr0_regnum + 2, readbuf);
  846.         }
  847.       if (writebuf)
  848.         {
  849.           /* Altivec places the return value in "v2".  */
  850.           regcache_cooked_write (regcache, tdep->ppc_vr0_regnum + 2, writebuf);
  851.         }
  852.       return RETURN_VALUE_REGISTER_CONVENTION;
  853.     }
  854.   if (TYPE_LENGTH (type) == 16
  855.       && TYPE_CODE (type) == TYPE_CODE_ARRAY
  856.       && TYPE_VECTOR (type)
  857.       && tdep->vector_abi == POWERPC_VEC_GENERIC)
  858.     {
  859.       /* GCC -maltivec -mabi=no-altivec returns vectors in r3/r4/r5/r6.
  860.          GCC without AltiVec returns them in memory, but it warns about
  861.          ABI risks in that case; we don't try to support it.  */
  862.       if (readbuf)
  863.         {
  864.           regcache_cooked_read (regcache, tdep->ppc_gp0_regnum + 3,
  865.                                 readbuf + 0);
  866.           regcache_cooked_read (regcache, tdep->ppc_gp0_regnum + 4,
  867.                                 readbuf + 4);
  868.           regcache_cooked_read (regcache, tdep->ppc_gp0_regnum + 5,
  869.                                 readbuf + 8);
  870.           regcache_cooked_read (regcache, tdep->ppc_gp0_regnum + 6,
  871.                                 readbuf + 12);
  872.         }
  873.       if (writebuf)
  874.         {
  875.           regcache_cooked_write (regcache, tdep->ppc_gp0_regnum + 3,
  876.                                  writebuf + 0);
  877.           regcache_cooked_write (regcache, tdep->ppc_gp0_regnum + 4,
  878.                                  writebuf + 4);
  879.           regcache_cooked_write (regcache, tdep->ppc_gp0_regnum + 5,
  880.                                  writebuf + 8);
  881.           regcache_cooked_write (regcache, tdep->ppc_gp0_regnum + 6,
  882.                                  writebuf + 12);
  883.         }
  884.       return RETURN_VALUE_REGISTER_CONVENTION;
  885.     }
  886.   if (TYPE_LENGTH (type) == 8
  887.       && TYPE_CODE (type) == TYPE_CODE_ARRAY
  888.       && TYPE_VECTOR (type)
  889.       && tdep->vector_abi == POWERPC_VEC_SPE)
  890.     {
  891.       /* The e500 ABI places return values for the 64-bit DSP types
  892.          (__ev64_opaque__) in r3.  However, in GDB-speak, ev3
  893.          corresponds to the entire r3 value for e500, whereas GDB's r3
  894.          only corresponds to the least significant 32-bits.  So place
  895.          the 64-bit DSP type's value in ev3.  */
  896.       if (readbuf)
  897.         regcache_cooked_read (regcache, tdep->ppc_ev0_regnum + 3, readbuf);
  898.       if (writebuf)
  899.         regcache_cooked_write (regcache, tdep->ppc_ev0_regnum + 3, writebuf);
  900.       return RETURN_VALUE_REGISTER_CONVENTION;
  901.     }
  902.   if (broken_gcc && TYPE_LENGTH (type) <= 8)
  903.     {
  904.       /* GCC screwed up for structures or unions whose size is less
  905.          than or equal to 8 bytes..  Instead of left-aligning, it
  906.          right-aligns the data into the buffer formed by r3, r4.  */
  907.       gdb_byte regvals[MAX_REGISTER_SIZE * 2];
  908.       int len = TYPE_LENGTH (type);
  909.       int offset = (2 * tdep->wordsize - len) % tdep->wordsize;

  910.       if (readbuf)
  911.         {
  912.           regcache_cooked_read (regcache, tdep->ppc_gp0_regnum + 3,
  913.                                 regvals + 0 * tdep->wordsize);
  914.           if (len > tdep->wordsize)
  915.             regcache_cooked_read (regcache, tdep->ppc_gp0_regnum + 4,
  916.                                   regvals + 1 * tdep->wordsize);
  917.           memcpy (readbuf, regvals + offset, len);
  918.         }
  919.       if (writebuf)
  920.         {
  921.           memset (regvals, 0, sizeof regvals);
  922.           memcpy (regvals + offset, writebuf, len);
  923.           regcache_cooked_write (regcache, tdep->ppc_gp0_regnum + 3,
  924.                                  regvals + 0 * tdep->wordsize);
  925.           if (len > tdep->wordsize)
  926.             regcache_cooked_write (regcache, tdep->ppc_gp0_regnum + 4,
  927.                                    regvals + 1 * tdep->wordsize);
  928.         }

  929.       return RETURN_VALUE_REGISTER_CONVENTION;
  930.     }
  931.   if (TYPE_LENGTH (type) <= 8)
  932.     {
  933.       if (readbuf)
  934.         {
  935.           /* This matches SVr4 PPC, it does not match GCC.  */
  936.           /* The value is right-padded to 8 bytes and then loaded, as
  937.              two "words", into r3/r4.  */
  938.           gdb_byte regvals[MAX_REGISTER_SIZE * 2];
  939.           regcache_cooked_read (regcache, tdep->ppc_gp0_regnum + 3,
  940.                                 regvals + 0 * tdep->wordsize);
  941.           if (TYPE_LENGTH (type) > tdep->wordsize)
  942.             regcache_cooked_read (regcache, tdep->ppc_gp0_regnum + 4,
  943.                                   regvals + 1 * tdep->wordsize);
  944.           memcpy (readbuf, regvals, TYPE_LENGTH (type));
  945.         }
  946.       if (writebuf)
  947.         {
  948.           /* This matches SVr4 PPC, it does not match GCC.  */
  949.           /* The value is padded out to 8 bytes and then loaded, as
  950.              two "words" into r3/r4.  */
  951.           gdb_byte regvals[MAX_REGISTER_SIZE * 2];
  952.           memset (regvals, 0, sizeof regvals);
  953.           memcpy (regvals, writebuf, TYPE_LENGTH (type));
  954.           regcache_cooked_write (regcache, tdep->ppc_gp0_regnum + 3,
  955.                                  regvals + 0 * tdep->wordsize);
  956.           if (TYPE_LENGTH (type) > tdep->wordsize)
  957.             regcache_cooked_write (regcache, tdep->ppc_gp0_regnum + 4,
  958.                                    regvals + 1 * tdep->wordsize);
  959.         }
  960.       return RETURN_VALUE_REGISTER_CONVENTION;
  961.     }
  962.   return RETURN_VALUE_STRUCT_CONVENTION;
  963. }

  964. enum return_value_convention
  965. ppc_sysv_abi_return_value (struct gdbarch *gdbarch, struct value *function,
  966.                            struct type *valtype, struct regcache *regcache,
  967.                            gdb_byte *readbuf, const gdb_byte *writebuf)
  968. {
  969.   return do_ppc_sysv_return_value (gdbarch,
  970.                                    function ? value_type (function) : NULL,
  971.                                    valtype, regcache, readbuf, writebuf, 0);
  972. }

  973. enum return_value_convention
  974. ppc_sysv_abi_broken_return_value (struct gdbarch *gdbarch,
  975.                                   struct value *function,
  976.                                   struct type *valtype,
  977.                                   struct regcache *regcache,
  978.                                   gdb_byte *readbuf, const gdb_byte *writebuf)
  979. {
  980.   return do_ppc_sysv_return_value (gdbarch,
  981.                                    function ? value_type (function) : NULL,
  982.                                    valtype, regcache, readbuf, writebuf, 1);
  983. }

  984. /* The helper function for 64-bit SYSV push_dummy_call.  Converts the
  985.    function's code address back into the function's descriptor
  986.    address.

  987.    Find a value for the TOC register.  Every symbol should have both
  988.    ".FN" and "FN" in the minimal symbol table.  "FN" points at the
  989.    FN's descriptor, while ".FN" points at the entry point (which
  990.    matches FUNC_ADDR).  Need to reverse from FUNC_ADDR back to the
  991.    FN's descriptor address (while at the same time being careful to
  992.    find "FN" in the same object file as ".FN").  */

  993. static int
  994. convert_code_addr_to_desc_addr (CORE_ADDR code_addr, CORE_ADDR *desc_addr)
  995. {
  996.   struct obj_section *dot_fn_section;
  997.   struct bound_minimal_symbol dot_fn;
  998.   struct bound_minimal_symbol fn;

  999.   /* Find the minimal symbol that corresponds to CODE_ADDR (should
  1000.      have a name of the form ".FN").  */
  1001.   dot_fn = lookup_minimal_symbol_by_pc (code_addr);
  1002.   if (dot_fn.minsym == NULL || MSYMBOL_LINKAGE_NAME (dot_fn.minsym)[0] != '.')
  1003.     return 0;
  1004.   /* Get the section that contains CODE_ADDR.  Need this for the
  1005.      "objfile" that it contains.  */
  1006.   dot_fn_section = find_pc_section (code_addr);
  1007.   if (dot_fn_section == NULL || dot_fn_section->objfile == NULL)
  1008.     return 0;
  1009.   /* Now find the corresponding "FN" (dropping ".") minimal symbol's
  1010.      address.  Only look for the minimal symbol in ".FN"'s object file
  1011.      - avoids problems when two object files (i.e., shared libraries)
  1012.      contain a minimal symbol with the same name.  */
  1013.   fn = lookup_minimal_symbol (MSYMBOL_LINKAGE_NAME (dot_fn.minsym) + 1, NULL,
  1014.                               dot_fn_section->objfile);
  1015.   if (fn.minsym == NULL)
  1016.     return 0;
  1017.   /* Found a descriptor.  */
  1018.   (*desc_addr) = BMSYMBOL_VALUE_ADDRESS (fn);
  1019.   return 1;
  1020. }

  1021. /* Walk down the type tree of TYPE counting consecutive base elements.
  1022.    If *FIELD_TYPE is NULL, then set it to the first valid floating point
  1023.    or vector type.  If a non-floating point or vector type is found, or
  1024.    if a floating point or vector type that doesn't match a non-NULL
  1025.    *FIELD_TYPE is found, then return -1, otherwise return the count in the
  1026.    sub-tree.  */

  1027. static LONGEST
  1028. ppc64_aggregate_candidate (struct type *type,
  1029.                            struct type **field_type)
  1030. {
  1031.   type = check_typedef (type);

  1032.   switch (TYPE_CODE (type))
  1033.     {
  1034.     case TYPE_CODE_FLT:
  1035.     case TYPE_CODE_DECFLOAT:
  1036.       if (!*field_type)
  1037.         *field_type = type;
  1038.       if (TYPE_CODE (*field_type) == TYPE_CODE (type)
  1039.           && TYPE_LENGTH (*field_type) == TYPE_LENGTH (type))
  1040.         return 1;
  1041.       break;

  1042.     case TYPE_CODE_COMPLEX:
  1043.       type = TYPE_TARGET_TYPE (type);
  1044.       if (TYPE_CODE (type) == TYPE_CODE_FLT
  1045.           || TYPE_CODE (type) == TYPE_CODE_DECFLOAT)
  1046.         {
  1047.           if (!*field_type)
  1048.             *field_type = type;
  1049.           if (TYPE_CODE (*field_type) == TYPE_CODE (type)
  1050.               && TYPE_LENGTH (*field_type) == TYPE_LENGTH (type))
  1051.             return 2;
  1052.         }
  1053.       break;

  1054.     case TYPE_CODE_ARRAY:
  1055.       if (TYPE_VECTOR (type))
  1056.         {
  1057.           if (!*field_type)
  1058.             *field_type = type;
  1059.           if (TYPE_CODE (*field_type) == TYPE_CODE (type)
  1060.               && TYPE_LENGTH (*field_type) == TYPE_LENGTH (type))
  1061.             return 1;
  1062.         }
  1063.       else
  1064.         {
  1065.           LONGEST count, low_bound, high_bound;

  1066.           count = ppc64_aggregate_candidate
  1067.                    (TYPE_TARGET_TYPE (type), field_type);
  1068.           if (count == -1)
  1069.             return -1;

  1070.           if (!get_array_bounds (type, &low_bound, &high_bound))
  1071.             return -1;
  1072.           count *= high_bound - low_bound;

  1073.           /* There must be no padding.  */
  1074.           if (count == 0)
  1075.             return TYPE_LENGTH (type) == 0 ? 0 : -1;
  1076.           else if (TYPE_LENGTH (type) != count * TYPE_LENGTH (*field_type))
  1077.             return -1;

  1078.           return count;
  1079.         }
  1080.       break;

  1081.     case TYPE_CODE_STRUCT:
  1082.     case TYPE_CODE_UNION:
  1083.         {
  1084.           LONGEST count = 0;
  1085.           int i;

  1086.           for (i = 0; i < TYPE_NFIELDS (type); i++)
  1087.             {
  1088.               LONGEST sub_count;

  1089.               if (field_is_static (&TYPE_FIELD (type, i)))
  1090.                 continue;

  1091.               sub_count = ppc64_aggregate_candidate
  1092.                            (TYPE_FIELD_TYPE (type, i), field_type);
  1093.               if (sub_count == -1)
  1094.                 return -1;

  1095.               if (TYPE_CODE (type) == TYPE_CODE_STRUCT)
  1096.                 count += sub_count;
  1097.               else
  1098.                 count = max (count, sub_count);
  1099.             }

  1100.           /* There must be no padding.  */
  1101.           if (count == 0)
  1102.             return TYPE_LENGTH (type) == 0 ? 0 : -1;
  1103.           else if (TYPE_LENGTH (type) != count * TYPE_LENGTH (*field_type))
  1104.             return -1;

  1105.           return count;
  1106.         }
  1107.       break;

  1108.     default:
  1109.       break;
  1110.     }

  1111.   return -1;
  1112. }

  1113. /* If an argument of type TYPE is a homogeneous float or vector aggregate
  1114.    that shall be passed in FP/vector registers according to the ELFv2 ABI,
  1115.    return the homogeneous element type in *ELT_TYPE and the number of
  1116.    elements in *N_ELTS, and return non-zero.  Otherwise, return zero.  */

  1117. static int
  1118. ppc64_elfv2_abi_homogeneous_aggregate (struct type *type,
  1119.                                        struct type **elt_type, int *n_elts)
  1120. {
  1121.   /* Complex types at the top level are treated separately.  However,
  1122.      complex types can be elements of homogeneous aggregates.  */
  1123.   if (TYPE_CODE (type) == TYPE_CODE_STRUCT
  1124.       || TYPE_CODE (type) == TYPE_CODE_UNION
  1125.       || (TYPE_CODE (type) == TYPE_CODE_ARRAY && !TYPE_VECTOR (type)))
  1126.     {
  1127.       struct type *field_type = NULL;
  1128.       LONGEST field_count = ppc64_aggregate_candidate (type, &field_type);

  1129.       if (field_count > 0)
  1130.         {
  1131.           int n_regs = ((TYPE_CODE (field_type) == TYPE_CODE_FLT
  1132.                          || TYPE_CODE (field_type) == TYPE_CODE_DECFLOAT)?
  1133.                         (TYPE_LENGTH (field_type) + 7) >> 3 : 1);

  1134.           /* The ELFv2 ABI allows homogeneous aggregates to occupy
  1135.              up to 8 registers.  */
  1136.           if (field_count * n_regs <= 8)
  1137.             {
  1138.               if (elt_type)
  1139.                 *elt_type = field_type;
  1140.               if (n_elts)
  1141.                 *n_elts = (int) field_count;
  1142.               /* Note that field_count is LONGEST since it may hold the size
  1143.                  of an array, while *n_elts is int since its value is bounded
  1144.                  by the number of registers used for argument passing.  The
  1145.                  cast cannot overflow due to the bounds checking above.  */
  1146.               return 1;
  1147.             }
  1148.         }
  1149.     }

  1150.   return 0;
  1151. }

  1152. /* Structure holding the next argument position.  */
  1153. struct ppc64_sysv_argpos
  1154.   {
  1155.     /* Register cache holding argument registers.  If this is NULL,
  1156.        we only simulate argument processing without actually updating
  1157.        any registers or memory.  */
  1158.     struct regcache *regcache;
  1159.     /* Next available general-purpose argument register.  */
  1160.     int greg;
  1161.     /* Next available floating-point argument register.  */
  1162.     int freg;
  1163.     /* Next available vector argument register.  */
  1164.     int vreg;
  1165.     /* The address, at which the next general purpose parameter
  1166.        (integer, struct, float, vector, ...) should be saved.  */
  1167.     CORE_ADDR gparam;
  1168.     /* The address, at which the next by-reference parameter
  1169.        (non-Altivec vector, variably-sized type) should be saved.  */
  1170.     CORE_ADDR refparam;
  1171.   };

  1172. /* VAL is a value of length LEN.  Store it into the argument area on the
  1173.    stack and load it into the corresponding general-purpose registers
  1174.    required by the ABI, and update ARGPOS.

  1175.    If ALIGN is nonzero, it specifies the minimum alignment required
  1176.    for the on-stack copy of the argument.  */

  1177. static void
  1178. ppc64_sysv_abi_push_val (struct gdbarch *gdbarch,
  1179.                          const bfd_byte *val, int len, int align,
  1180.                          struct ppc64_sysv_argpos *argpos)
  1181. {
  1182.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
  1183.   int offset = 0;

  1184.   /* Enforce alignment of stack location, if requested.  */
  1185.   if (align > tdep->wordsize)
  1186.     {
  1187.       CORE_ADDR aligned_gparam = align_up (argpos->gparam, align);

  1188.       argpos->greg += (aligned_gparam - argpos->gparam) / tdep->wordsize;
  1189.       argpos->gparam = aligned_gparam;
  1190.     }

  1191.   /* The ABI (version 1.9) specifies that values smaller than one
  1192.      doubleword are right-aligned and those larger are left-aligned.
  1193.      GCC versions before 3.4 implemented this incorrectly; see
  1194.      <http://gcc.gnu.org/gcc-3.4/powerpc-abi.html>.  */
  1195.   if (len < tdep->wordsize
  1196.       && gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
  1197.     offset = tdep->wordsize - len;

  1198.   if (argpos->regcache)
  1199.     write_memory (argpos->gparam + offset, val, len);
  1200.   argpos->gparam = align_up (argpos->gparam + len, tdep->wordsize);

  1201.   while (len >= tdep->wordsize)
  1202.     {
  1203.       if (argpos->regcache && argpos->greg <= 10)
  1204.         regcache_cooked_write (argpos->regcache,
  1205.                                tdep->ppc_gp0_regnum + argpos->greg, val);
  1206.       argpos->greg++;
  1207.       len -= tdep->wordsize;
  1208.       val += tdep->wordsize;
  1209.     }

  1210.   if (len > 0)
  1211.     {
  1212.       if (argpos->regcache && argpos->greg <= 10)
  1213.         regcache_cooked_write_part (argpos->regcache,
  1214.                                     tdep->ppc_gp0_regnum + argpos->greg,
  1215.                                     offset, len, val);
  1216.       argpos->greg++;
  1217.     }
  1218. }

  1219. /* The same as ppc64_sysv_abi_push_val, but using a single-word integer
  1220.    value VAL as argument.  */

  1221. static void
  1222. ppc64_sysv_abi_push_integer (struct gdbarch *gdbarch, ULONGEST val,
  1223.                              struct ppc64_sysv_argpos *argpos)
  1224. {
  1225.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
  1226.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  1227.   gdb_byte buf[MAX_REGISTER_SIZE];

  1228.   if (argpos->regcache)
  1229.     store_unsigned_integer (buf, tdep->wordsize, byte_order, val);
  1230.   ppc64_sysv_abi_push_val (gdbarch, buf, tdep->wordsize, 0, argpos);
  1231. }

  1232. /* VAL is a value of TYPE, a (binary or decimal) floating-point type.
  1233.    Load it into a floating-point register if required by the ABI,
  1234.    and update ARGPOS.  */

  1235. static void
  1236. ppc64_sysv_abi_push_freg (struct gdbarch *gdbarch,
  1237.                           struct type *type, const bfd_byte *val,
  1238.                           struct ppc64_sysv_argpos *argpos)
  1239. {
  1240.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
  1241.   if (tdep->soft_float)
  1242.     return;

  1243.   if (TYPE_LENGTH (type) <= 8
  1244.       && TYPE_CODE (type) == TYPE_CODE_FLT)
  1245.     {
  1246.       /* Floats and doubles go in f1 .. f13.  32-bit floats are converted
  1247.           to double first.  */
  1248.       if (argpos->regcache && argpos->freg <= 13)
  1249.         {
  1250.           int regnum = tdep->ppc_fp0_regnum + argpos->freg;
  1251.           struct type *regtype = register_type (gdbarch, regnum);
  1252.           gdb_byte regval[MAX_REGISTER_SIZE];

  1253.           convert_typed_floating (val, type, regval, regtype);
  1254.           regcache_cooked_write (argpos->regcache, regnum, regval);
  1255.         }

  1256.       argpos->freg++;
  1257.     }
  1258.   else if (TYPE_LENGTH (type) <= 8
  1259.            && TYPE_CODE (type) == TYPE_CODE_DECFLOAT)
  1260.     {
  1261.       /* Floats and doubles go in f1 .. f13.  32-bit decimal floats are
  1262.          placed in the least significant word.  */
  1263.       if (argpos->regcache && argpos->freg <= 13)
  1264.         {
  1265.           int regnum = tdep->ppc_fp0_regnum + argpos->freg;
  1266.           int offset = 0;

  1267.           if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
  1268.             offset = 8 - TYPE_LENGTH (type);

  1269.           regcache_cooked_write_part (argpos->regcache, regnum,
  1270.                                       offset, TYPE_LENGTH (type), val);
  1271.         }

  1272.       argpos->freg++;
  1273.     }
  1274.   else if (TYPE_LENGTH (type) == 16
  1275.            && TYPE_CODE (type) == TYPE_CODE_FLT
  1276.            && (gdbarch_long_double_format (gdbarch)
  1277.                == floatformats_ibm_long_double))
  1278.     {
  1279.       /* IBM long double stored in two consecutive FPRs.  */
  1280.       if (argpos->regcache && argpos->freg <= 13)
  1281.         {
  1282.           int regnum = tdep->ppc_fp0_regnum + argpos->freg;

  1283.           regcache_cooked_write (argpos->regcache, regnum, val);
  1284.           if (argpos->freg <= 12)
  1285.             regcache_cooked_write (argpos->regcache, regnum + 1, val + 8);
  1286.         }

  1287.       argpos->freg += 2;
  1288.     }
  1289.   else if (TYPE_LENGTH (type) == 16
  1290.            && TYPE_CODE (type) == TYPE_CODE_DECFLOAT)
  1291.     {
  1292.       /* 128-bit decimal floating-point values are stored in and even/odd
  1293.          pair of FPRs, with the even FPR holding the most significant half.  */
  1294.       argpos->freg += argpos->freg & 1;

  1295.       if (argpos->regcache && argpos->freg <= 12)
  1296.         {
  1297.           int regnum = tdep->ppc_fp0_regnum + argpos->freg;
  1298.           int lopart = gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG ? 8 : 0;
  1299.           int hipart = gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG ? 0 : 8;

  1300.           regcache_cooked_write (argpos->regcache, regnum, val + hipart);
  1301.           regcache_cooked_write (argpos->regcache, regnum + 1, val + lopart);
  1302.         }

  1303.       argpos->freg += 2;
  1304.     }
  1305. }

  1306. /* VAL is a value of AltiVec vector type.  Load it into a vector register
  1307.    if required by the ABI, and update ARGPOS.  */

  1308. static void
  1309. ppc64_sysv_abi_push_vreg (struct gdbarch *gdbarch, const bfd_byte *val,
  1310.                           struct ppc64_sysv_argpos *argpos)
  1311. {
  1312.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);

  1313.   if (argpos->regcache && argpos->vreg <= 13)
  1314.     regcache_cooked_write (argpos->regcache,
  1315.                            tdep->ppc_vr0_regnum + argpos->vreg, val);

  1316.   argpos->vreg++;
  1317. }

  1318. /* VAL is a value of TYPE.  Load it into memory and/or registers
  1319.    as required by the ABI, and update ARGPOS.  */

  1320. static void
  1321. ppc64_sysv_abi_push_param (struct gdbarch *gdbarch,
  1322.                            struct type *type, const bfd_byte *val,
  1323.                            struct ppc64_sysv_argpos *argpos)
  1324. {
  1325.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);

  1326.   if (TYPE_CODE (type) == TYPE_CODE_FLT
  1327.       || TYPE_CODE (type) == TYPE_CODE_DECFLOAT)
  1328.     {
  1329.       /* Floating-point scalars are passed in floating-point registers.  */
  1330.       ppc64_sysv_abi_push_val (gdbarch, val, TYPE_LENGTH (type), 0, argpos);
  1331.       ppc64_sysv_abi_push_freg (gdbarch, type, val, argpos);
  1332.     }
  1333.   else if (TYPE_CODE (type) == TYPE_CODE_ARRAY && TYPE_VECTOR (type)
  1334.            && tdep->vector_abi == POWERPC_VEC_ALTIVEC
  1335.            && TYPE_LENGTH (type) == 16)
  1336.     {
  1337.       /* AltiVec vectors are passed aligned, and in vector registers.  */
  1338.       ppc64_sysv_abi_push_val (gdbarch, val, TYPE_LENGTH (type), 16, argpos);
  1339.       ppc64_sysv_abi_push_vreg (gdbarch, val, argpos);
  1340.     }
  1341.   else if (TYPE_CODE (type) == TYPE_CODE_ARRAY && TYPE_VECTOR (type)
  1342.            && TYPE_LENGTH (type) >= 16)
  1343.     {
  1344.       /* Non-Altivec vectors are passed by reference.  */

  1345.       /* Copy value onto the stack ...  */
  1346.       CORE_ADDR addr = align_up (argpos->refparam, 16);
  1347.       if (argpos->regcache)
  1348.         write_memory (addr, val, TYPE_LENGTH (type));
  1349.       argpos->refparam = align_up (addr + TYPE_LENGTH (type), tdep->wordsize);

  1350.       /* ... and pass a pointer to the copy as parameter.  */
  1351.       ppc64_sysv_abi_push_integer (gdbarch, addr, argpos);
  1352.     }
  1353.   else if ((TYPE_CODE (type) == TYPE_CODE_INT
  1354.             || TYPE_CODE (type) == TYPE_CODE_ENUM
  1355.             || TYPE_CODE (type) == TYPE_CODE_BOOL
  1356.             || TYPE_CODE (type) == TYPE_CODE_CHAR
  1357.             || TYPE_CODE (type) == TYPE_CODE_PTR
  1358.             || TYPE_CODE (type) == TYPE_CODE_REF)
  1359.            && TYPE_LENGTH (type) <= tdep->wordsize)
  1360.     {
  1361.       ULONGEST word = 0;

  1362.       if (argpos->regcache)
  1363.         {
  1364.           /* Sign extend the value, then store it unsigned.  */
  1365.           word = unpack_long (type, val);

  1366.           /* Convert any function code addresses into descriptors.  */
  1367.           if (tdep->elf_abi == POWERPC_ELF_V1
  1368.               && (TYPE_CODE (type) == TYPE_CODE_PTR
  1369.                   || TYPE_CODE (type) == TYPE_CODE_REF))
  1370.             {
  1371.               struct type *target_type
  1372.                 = check_typedef (TYPE_TARGET_TYPE (type));

  1373.               if (TYPE_CODE (target_type) == TYPE_CODE_FUNC
  1374.                   || TYPE_CODE (target_type) == TYPE_CODE_METHOD)
  1375.                 {
  1376.                   CORE_ADDR desc = word;

  1377.                   convert_code_addr_to_desc_addr (word, &desc);
  1378.                   word = desc;
  1379.                 }
  1380.             }
  1381.         }

  1382.       ppc64_sysv_abi_push_integer (gdbarch, word, argpos);
  1383.     }
  1384.   else
  1385.     {
  1386.       ppc64_sysv_abi_push_val (gdbarch, val, TYPE_LENGTH (type), 0, argpos);

  1387.       /* The ABI (version 1.9) specifies that structs containing a
  1388.          single floating-point value, at any level of nesting of
  1389.          single-member structs, are passed in floating-point registers.  */
  1390.       if (TYPE_CODE (type) == TYPE_CODE_STRUCT
  1391.           && TYPE_NFIELDS (type) == 1)
  1392.         {
  1393.           while (TYPE_CODE (type) == TYPE_CODE_STRUCT
  1394.                  && TYPE_NFIELDS (type) == 1)
  1395.             type = check_typedef (TYPE_FIELD_TYPE (type, 0));

  1396.           if (TYPE_CODE (type) == TYPE_CODE_FLT)
  1397.             ppc64_sysv_abi_push_freg (gdbarch, type, val, argpos);
  1398.         }

  1399.       /* In the ELFv2 ABI, homogeneous floating-point or vector
  1400.          aggregates are passed in a series of registers.  */
  1401.       if (tdep->elf_abi == POWERPC_ELF_V2)
  1402.         {
  1403.           struct type *eltype;
  1404.           int i, nelt;

  1405.           if (ppc64_elfv2_abi_homogeneous_aggregate (type, &eltype, &nelt))
  1406.             for (i = 0; i < nelt; i++)
  1407.               {
  1408.                 const gdb_byte *elval = val + i * TYPE_LENGTH (eltype);

  1409.                 if (TYPE_CODE (eltype) == TYPE_CODE_FLT
  1410.                     || TYPE_CODE (eltype) == TYPE_CODE_DECFLOAT)
  1411.                   ppc64_sysv_abi_push_freg (gdbarch, eltype, elval, argpos);
  1412.                 else if (TYPE_CODE (eltype) == TYPE_CODE_ARRAY
  1413.                          && TYPE_VECTOR (eltype)
  1414.                          && tdep->vector_abi == POWERPC_VEC_ALTIVEC
  1415.                          && TYPE_LENGTH (eltype) == 16)
  1416.                   ppc64_sysv_abi_push_vreg (gdbarch, elval, argpos);
  1417.               }
  1418.         }
  1419.     }
  1420. }

  1421. /* Pass the arguments in either registers, or in the stack.  Using the
  1422.    ppc 64 bit SysV ABI.

  1423.    This implements a dumbed down version of the ABI.  It always writes
  1424.    values to memory, GPR and FPR, even when not necessary.  Doing this
  1425.    greatly simplifies the logic.  */

  1426. CORE_ADDR
  1427. ppc64_sysv_abi_push_dummy_call (struct gdbarch *gdbarch,
  1428.                                 struct value *function,
  1429.                                 struct regcache *regcache, CORE_ADDR bp_addr,
  1430.                                 int nargs, struct value **args, CORE_ADDR sp,
  1431.                                 int struct_return, CORE_ADDR struct_addr)
  1432. {
  1433.   CORE_ADDR func_addr = find_function_addr (function, NULL);
  1434.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
  1435.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  1436.   int opencl_abi = ppc_sysv_use_opencl_abi (value_type (function));
  1437.   ULONGEST back_chain;
  1438.   /* See for-loop comment below.  */
  1439.   int write_pass;
  1440.   /* Size of the by-reference parameter copy region, the final value is
  1441.      computed in the for-loop below.  */
  1442.   LONGEST refparam_size = 0;
  1443.   /* Size of the general parameter region, the final value is computed
  1444.      in the for-loop below.  */
  1445.   LONGEST gparam_size = 0;
  1446.   /* Kevin writes ... I don't mind seeing tdep->wordsize used in the
  1447.      calls to align_up(), align_down(), etc. because this makes it
  1448.      easier to reuse this code (in a copy/paste sense) in the future,
  1449.      but it is a 64-bit ABI and asserting that the wordsize is 8 bytes
  1450.      at some point makes it easier to verify that this function is
  1451.      correct without having to do a non-local analysis to figure out
  1452.      the possible values of tdep->wordsize.  */
  1453.   gdb_assert (tdep->wordsize == 8);

  1454.   /* This function exists to support a calling convention that
  1455.      requires floating-point registers.  It shouldn't be used on
  1456.      processors that lack them.  */
  1457.   gdb_assert (ppc_floating_point_unit_p (gdbarch));

  1458.   /* By this stage in the proceedings, SP has been decremented by "red
  1459.      zone size" + "struct return size".  Fetch the stack-pointer from
  1460.      before this and use that as the BACK_CHAIN.  */
  1461.   regcache_cooked_read_unsigned (regcache, gdbarch_sp_regnum (gdbarch),
  1462.                                  &back_chain);

  1463.   /* Go through the argument list twice.

  1464.      Pass 1: Compute the function call's stack space and register
  1465.      requirements.

  1466.      Pass 2: Replay the same computation but this time also write the
  1467.      values out to the target.  */

  1468.   for (write_pass = 0; write_pass < 2; write_pass++)
  1469.     {
  1470.       int argno;

  1471.       struct ppc64_sysv_argpos argpos;
  1472.       argpos.greg = 3;
  1473.       argpos.freg = 1;
  1474.       argpos.vreg = 2;

  1475.       if (!write_pass)
  1476.         {
  1477.           /* During the first pass, GPARAM and REFPARAM are more like
  1478.              offsets (start address zero) than addresses.  That way
  1479.              they accumulate the total stack space each region
  1480.              requires.  */
  1481.           argpos.regcache = NULL;
  1482.           argpos.gparam = 0;
  1483.           argpos.refparam = 0;
  1484.         }
  1485.       else
  1486.         {
  1487.           /* Decrement the stack pointer making space for the Altivec
  1488.              and general on-stack parameters.  Set refparam and gparam
  1489.              to their corresponding regions.  */
  1490.           argpos.regcache = regcache;
  1491.           argpos.refparam = align_down (sp - refparam_size, 16);
  1492.           argpos.gparam = align_down (argpos.refparam - gparam_size, 16);
  1493.           /* Add in space for the TOC, link editor double word (v1 only),
  1494.              compiler double word (v1 only), LR save area, CR save area,
  1495.              and backchain.  */
  1496.           if (tdep->elf_abi == POWERPC_ELF_V1)
  1497.             sp = align_down (argpos.gparam - 48, 16);
  1498.           else
  1499.             sp = align_down (argpos.gparam - 32, 16);
  1500.         }

  1501.       /* If the function is returning a `struct', then there is an
  1502.          extra hidden parameter (which will be passed in r3)
  1503.          containing the address of that struct..  In that case we
  1504.          should advance one word and start from r4 register to copy
  1505.          parameters.  This also consumes one on-stack parameter slot.  */
  1506.       if (struct_return)
  1507.         ppc64_sysv_abi_push_integer (gdbarch, struct_addr, &argpos);

  1508.       for (argno = 0; argno < nargs; argno++)
  1509.         {
  1510.           struct value *arg = args[argno];
  1511.           struct type *type = check_typedef (value_type (arg));
  1512.           const bfd_byte *val = value_contents (arg);

  1513.           if (TYPE_CODE (type) == TYPE_CODE_COMPLEX)
  1514.             {
  1515.               /* Complex types are passed as if two independent scalars.  */
  1516.               struct type *eltype = check_typedef (TYPE_TARGET_TYPE (type));

  1517.               ppc64_sysv_abi_push_param (gdbarch, eltype, val, &argpos);
  1518.               ppc64_sysv_abi_push_param (gdbarch, eltype,
  1519.                                           val + TYPE_LENGTH (eltype), &argpos);
  1520.             }
  1521.           else if (TYPE_CODE (type) == TYPE_CODE_ARRAY && TYPE_VECTOR (type)
  1522.                    && opencl_abi)
  1523.             {
  1524.               /* OpenCL vectors shorter than 16 bytes are passed as if
  1525.                  a series of independent scalars; OpenCL vectors 16 bytes
  1526.                  or longer are passed as if a series of AltiVec vectors.  */
  1527.               struct type *eltype;
  1528.               int i, nelt;

  1529.               if (TYPE_LENGTH (type) < 16)
  1530.                 eltype = check_typedef (TYPE_TARGET_TYPE (type));
  1531.               else
  1532.                 eltype = register_type (gdbarch, tdep->ppc_vr0_regnum);

  1533.               nelt = TYPE_LENGTH (type) / TYPE_LENGTH (eltype);
  1534.               for (i = 0; i < nelt; i++)
  1535.                 {
  1536.                   const gdb_byte *elval = val + i * TYPE_LENGTH (eltype);

  1537.                   ppc64_sysv_abi_push_param (gdbarch, eltype, elval, &argpos);
  1538.                 }
  1539.             }
  1540.           else
  1541.             {
  1542.               /* All other types are passed as single arguments.  */
  1543.               ppc64_sysv_abi_push_param (gdbarch, type, val, &argpos);
  1544.             }
  1545.         }

  1546.       if (!write_pass)
  1547.         {
  1548.           /* Save the true region sizes ready for the second pass.  */
  1549.           refparam_size = argpos.refparam;
  1550.           /* Make certain that the general parameter save area is at
  1551.              least the minimum 8 registers (or doublewords) in size.  */
  1552.           if (argpos.greg < 8)
  1553.             gparam_size = 8 * tdep->wordsize;
  1554.           else
  1555.             gparam_size = argpos.gparam;
  1556.         }
  1557.     }

  1558.   /* Update %sp.   */
  1559.   regcache_cooked_write_signed (regcache, gdbarch_sp_regnum (gdbarch), sp);

  1560.   /* Write the backchain (it occupies WORDSIZED bytes).  */
  1561.   write_memory_signed_integer (sp, tdep->wordsize, byte_order, back_chain);

  1562.   /* Point the inferior function call's return address at the dummy's
  1563.      breakpoint.  */
  1564.   regcache_cooked_write_signed (regcache, tdep->ppc_lr_regnum, bp_addr);

  1565.   /* In the ELFv1 ABI, use the func_addr to find the descriptor, and use
  1566.      that to find the TOC.  If we're calling via a function pointer,
  1567.      the pointer itself identifies the descriptor.  */
  1568.   if (tdep->elf_abi == POWERPC_ELF_V1)
  1569.     {
  1570.       struct type *ftype = check_typedef (value_type (function));
  1571.       CORE_ADDR desc_addr = value_as_address (function);

  1572.       if (TYPE_CODE (ftype) == TYPE_CODE_PTR
  1573.           || convert_code_addr_to_desc_addr (func_addr, &desc_addr))
  1574.         {
  1575.           /* The TOC is the second double word in the descriptor.  */
  1576.           CORE_ADDR toc =
  1577.             read_memory_unsigned_integer (desc_addr + tdep->wordsize,
  1578.                                           tdep->wordsize, byte_order);

  1579.           regcache_cooked_write_unsigned (regcache,
  1580.                                           tdep->ppc_gp0_regnum + 2, toc);
  1581.         }
  1582.     }

  1583.   /* In the ELFv2 ABI, we need to pass the target address in r12 since
  1584.      we may be calling a global entry point.  */
  1585.   if (tdep->elf_abi == POWERPC_ELF_V2)
  1586.     regcache_cooked_write_unsigned (regcache,
  1587.                                     tdep->ppc_gp0_regnum + 12, func_addr);

  1588.   return sp;
  1589. }

  1590. /* Subroutine of ppc64_sysv_abi_return_value that handles "base" types:
  1591.    integer, floating-point, and AltiVec vector types.

  1592.    This routine also handles components of aggregate return types;
  1593.    INDEX describes which part of the aggregate is to be handled.

  1594.    Returns true if VALTYPE is some such base type that could be handled,
  1595.    false otherwise.  */
  1596. static int
  1597. ppc64_sysv_abi_return_value_base (struct gdbarch *gdbarch, struct type *valtype,
  1598.                                   struct regcache *regcache, gdb_byte *readbuf,
  1599.                                   const gdb_byte *writebuf, int index)
  1600. {
  1601.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);

  1602.   /* Integers live in GPRs starting at r3.  */
  1603.   if ((TYPE_CODE (valtype) == TYPE_CODE_INT
  1604.        || TYPE_CODE (valtype) == TYPE_CODE_ENUM
  1605.        || TYPE_CODE (valtype) == TYPE_CODE_CHAR
  1606.        || TYPE_CODE (valtype) == TYPE_CODE_BOOL)
  1607.       && TYPE_LENGTH (valtype) <= 8)
  1608.     {
  1609.       int regnum = tdep->ppc_gp0_regnum + 3 + index;

  1610.       if (writebuf != NULL)
  1611.         {
  1612.           /* Be careful to sign extend the value.  */
  1613.           regcache_cooked_write_unsigned (regcache, regnum,
  1614.                                           unpack_long (valtype, writebuf));
  1615.         }
  1616.       if (readbuf != NULL)
  1617.         {
  1618.           /* Extract the integer from GPR.  Since this is truncating the
  1619.              value, there isn't a sign extension problem.  */
  1620.           ULONGEST regval;

  1621.           regcache_cooked_read_unsigned (regcache, regnum, &regval);
  1622.           store_unsigned_integer (readbuf, TYPE_LENGTH (valtype),
  1623.                                   gdbarch_byte_order (gdbarch), regval);
  1624.         }
  1625.       return 1;
  1626.     }

  1627.   /* Floats and doubles go in f1 .. f13.  32-bit floats are converted
  1628.      to double first.  */
  1629.   if (TYPE_LENGTH (valtype) <= 8
  1630.       && TYPE_CODE (valtype) == TYPE_CODE_FLT)
  1631.     {
  1632.       int regnum = tdep->ppc_fp0_regnum + 1 + index;
  1633.       struct type *regtype = register_type (gdbarch, regnum);
  1634.       gdb_byte regval[MAX_REGISTER_SIZE];

  1635.       if (writebuf != NULL)
  1636.         {
  1637.           convert_typed_floating (writebuf, valtype, regval, regtype);
  1638.           regcache_cooked_write (regcache, regnum, regval);
  1639.         }
  1640.       if (readbuf != NULL)
  1641.         {
  1642.           regcache_cooked_read (regcache, regnum, regval);
  1643.           convert_typed_floating (regval, regtype, readbuf, valtype);
  1644.         }
  1645.       return 1;
  1646.     }

  1647.   /* Floats and doubles go in f1 .. f13.  32-bit decimal floats are
  1648.      placed in the least significant word.  */
  1649.   if (TYPE_LENGTH (valtype) <= 8
  1650.       && TYPE_CODE (valtype) == TYPE_CODE_DECFLOAT)
  1651.     {
  1652.       int regnum = tdep->ppc_fp0_regnum + 1 + index;
  1653.       int offset = 0;

  1654.       if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
  1655.         offset = 8 - TYPE_LENGTH (valtype);

  1656.       if (writebuf != NULL)
  1657.         regcache_cooked_write_part (regcache, regnum,
  1658.                                     offset, TYPE_LENGTH (valtype), writebuf);
  1659.       if (readbuf != NULL)
  1660.         regcache_cooked_read_part (regcache, regnum,
  1661.                                    offset, TYPE_LENGTH (valtype), readbuf);
  1662.       return 1;
  1663.     }

  1664.   /* IBM long double stored in two consecutive FPRs.  */
  1665.   if (TYPE_LENGTH (valtype) == 16
  1666.       && TYPE_CODE (valtype) == TYPE_CODE_FLT
  1667.       && (gdbarch_long_double_format (gdbarch)
  1668.           == floatformats_ibm_long_double))
  1669.     {
  1670.       int regnum = tdep->ppc_fp0_regnum + 1 + 2 * index;

  1671.       if (writebuf != NULL)
  1672.         {
  1673.           regcache_cooked_write (regcache, regnum, writebuf);
  1674.           regcache_cooked_write (regcache, regnum + 1, writebuf + 8);
  1675.         }
  1676.       if (readbuf != NULL)
  1677.         {
  1678.           regcache_cooked_read (regcache, regnum, readbuf);
  1679.           regcache_cooked_read (regcache, regnum + 1, readbuf + 8);
  1680.         }
  1681.       return 1;
  1682.     }

  1683.   /* 128-bit decimal floating-point values are stored in an even/odd
  1684.      pair of FPRs, with the even FPR holding the most significant half.  */
  1685.   if (TYPE_LENGTH (valtype) == 16
  1686.       && TYPE_CODE (valtype) == TYPE_CODE_DECFLOAT)
  1687.     {
  1688.       int regnum = tdep->ppc_fp0_regnum + 2 + 2 * index;
  1689.       int lopart = gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG ? 8 : 0;
  1690.       int hipart = gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG ? 0 : 8;

  1691.       if (writebuf != NULL)
  1692.         {
  1693.           regcache_cooked_write (regcache, regnum, writebuf + hipart);
  1694.           regcache_cooked_write (regcache, regnum + 1, writebuf + lopart);
  1695.         }
  1696.       if (readbuf != NULL)
  1697.         {
  1698.           regcache_cooked_read (regcache, regnum, readbuf + hipart);
  1699.           regcache_cooked_read (regcache, regnum + 1, readbuf + lopart);
  1700.         }
  1701.       return 1;
  1702.     }

  1703.   /* AltiVec vectors are returned in VRs starting at v2.  */
  1704.   if (TYPE_CODE (valtype) == TYPE_CODE_ARRAY && TYPE_VECTOR (valtype)
  1705.       && tdep->vector_abi == POWERPC_VEC_ALTIVEC)
  1706.     {
  1707.       int regnum = tdep->ppc_vr0_regnum + 2 + index;

  1708.       if (writebuf != NULL)
  1709.         regcache_cooked_write (regcache, regnum, writebuf);
  1710.       if (readbuf != NULL)
  1711.         regcache_cooked_read (regcache, regnum, readbuf);
  1712.       return 1;
  1713.     }

  1714.   return 0;
  1715. }

  1716. /* The 64 bit ABI return value convention.

  1717.    Return non-zero if the return-value is stored in a register, return
  1718.    0 if the return-value is instead stored on the stack (a.k.a.,
  1719.    struct return convention).

  1720.    For a return-value stored in a register: when WRITEBUF is non-NULL,
  1721.    copy the buffer to the corresponding register return-value location
  1722.    location; when READBUF is non-NULL, fill the buffer from the
  1723.    corresponding register return-value location.  */
  1724. enum return_value_convention
  1725. ppc64_sysv_abi_return_value (struct gdbarch *gdbarch, struct value *function,
  1726.                              struct type *valtype, struct regcache *regcache,
  1727.                              gdb_byte *readbuf, const gdb_byte *writebuf)
  1728. {
  1729.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
  1730.   struct type *func_type = function ? value_type (function) : NULL;
  1731.   int opencl_abi = func_type? ppc_sysv_use_opencl_abi (func_type) : 0;
  1732.   struct type *eltype;
  1733.   int nelt, i, ok;

  1734.   /* This function exists to support a calling convention that
  1735.      requires floating-point registers.  It shouldn't be used on
  1736.      processors that lack them.  */
  1737.   gdb_assert (ppc_floating_point_unit_p (gdbarch));

  1738.   /* Complex types are returned as if two independent scalars.  */
  1739.   if (TYPE_CODE (valtype) == TYPE_CODE_COMPLEX)
  1740.     {
  1741.       eltype = check_typedef (TYPE_TARGET_TYPE (valtype));

  1742.       for (i = 0; i < 2; i++)
  1743.         {
  1744.           ok = ppc64_sysv_abi_return_value_base (gdbarch, eltype, regcache,
  1745.                                                  readbuf, writebuf, i);
  1746.           gdb_assert (ok);

  1747.           if (readbuf)
  1748.             readbuf += TYPE_LENGTH (eltype);
  1749.           if (writebuf)
  1750.             writebuf += TYPE_LENGTH (eltype);
  1751.         }
  1752.       return RETURN_VALUE_REGISTER_CONVENTION;
  1753.     }

  1754.   /* OpenCL vectors shorter than 16 bytes are returned as if
  1755.      a series of independent scalars; OpenCL vectors 16 bytes
  1756.      or longer are returned as if a series of AltiVec vectors.  */
  1757.   if (TYPE_CODE (valtype) == TYPE_CODE_ARRAY && TYPE_VECTOR (valtype)
  1758.       && opencl_abi)
  1759.     {
  1760.       if (TYPE_LENGTH (valtype) < 16)
  1761.         eltype = check_typedef (TYPE_TARGET_TYPE (valtype));
  1762.       else
  1763.         eltype = register_type (gdbarch, tdep->ppc_vr0_regnum);

  1764.       nelt = TYPE_LENGTH (valtype) / TYPE_LENGTH (eltype);
  1765.       for (i = 0; i < nelt; i++)
  1766.         {
  1767.           ok = ppc64_sysv_abi_return_value_base (gdbarch, eltype, regcache,
  1768.                                                  readbuf, writebuf, i);
  1769.           gdb_assert (ok);

  1770.           if (readbuf)
  1771.             readbuf += TYPE_LENGTH (eltype);
  1772.           if (writebuf)
  1773.             writebuf += TYPE_LENGTH (eltype);
  1774.         }
  1775.       return RETURN_VALUE_REGISTER_CONVENTION;
  1776.     }

  1777.   /* All pointers live in r3.  */
  1778.   if (TYPE_CODE (valtype) == TYPE_CODE_PTR
  1779.       || TYPE_CODE (valtype) == TYPE_CODE_REF)
  1780.     {
  1781.       int regnum = tdep->ppc_gp0_regnum + 3;

  1782.       if (writebuf != NULL)
  1783.         regcache_cooked_write (regcache, regnum, writebuf);
  1784.       if (readbuf != NULL)
  1785.         regcache_cooked_read (regcache, regnum, readbuf);
  1786.       return RETURN_VALUE_REGISTER_CONVENTION;
  1787.     }

  1788.   /* Small character arrays are returned, right justified, in r3.  */
  1789.   if (TYPE_CODE (valtype) == TYPE_CODE_ARRAY
  1790.       && TYPE_LENGTH (valtype) <= 8
  1791.       && TYPE_CODE (TYPE_TARGET_TYPE (valtype)) == TYPE_CODE_INT
  1792.       && TYPE_LENGTH (TYPE_TARGET_TYPE (valtype)) == 1)
  1793.     {
  1794.       int regnum = tdep->ppc_gp0_regnum + 3;
  1795.       int offset = (register_size (gdbarch, regnum) - TYPE_LENGTH (valtype));

  1796.       if (writebuf != NULL)
  1797.         regcache_cooked_write_part (regcache, regnum,
  1798.                                     offset, TYPE_LENGTH (valtype), writebuf);
  1799.       if (readbuf != NULL)
  1800.         regcache_cooked_read_part (regcache, regnum,
  1801.                                    offset, TYPE_LENGTH (valtype), readbuf);
  1802.       return RETURN_VALUE_REGISTER_CONVENTION;
  1803.     }

  1804.   /* In the ELFv2 ABI, homogeneous floating-point or vector
  1805.      aggregates are returned in registers.  */
  1806.   if (tdep->elf_abi == POWERPC_ELF_V2
  1807.       && ppc64_elfv2_abi_homogeneous_aggregate (valtype, &eltype, &nelt))
  1808.     {
  1809.       for (i = 0; i < nelt; i++)
  1810.         {
  1811.           ok = ppc64_sysv_abi_return_value_base (gdbarch, eltype, regcache,
  1812.                                                  readbuf, writebuf, i);
  1813.           gdb_assert (ok);

  1814.           if (readbuf)
  1815.             readbuf += TYPE_LENGTH (eltype);
  1816.           if (writebuf)
  1817.             writebuf += TYPE_LENGTH (eltype);
  1818.         }

  1819.       return RETURN_VALUE_REGISTER_CONVENTION;
  1820.     }

  1821.   /* In the ELFv2 ABI, aggregate types of up to 16 bytes are
  1822.      returned in registers r3:r4.  */
  1823.   if (tdep->elf_abi == POWERPC_ELF_V2
  1824.       && TYPE_LENGTH (valtype) <= 16
  1825.       && (TYPE_CODE (valtype) == TYPE_CODE_STRUCT
  1826.           || TYPE_CODE (valtype) == TYPE_CODE_UNION
  1827.           || (TYPE_CODE (valtype) == TYPE_CODE_ARRAY
  1828.               && !TYPE_VECTOR (valtype))))
  1829.     {
  1830.       int n_regs = ((TYPE_LENGTH (valtype) + tdep->wordsize - 1)
  1831.                     / tdep->wordsize);
  1832.       int i;

  1833.       for (i = 0; i < n_regs; i++)
  1834.         {
  1835.           gdb_byte regval[MAX_REGISTER_SIZE];
  1836.           int regnum = tdep->ppc_gp0_regnum + 3 + i;
  1837.           int offset = i * tdep->wordsize;
  1838.           int len = TYPE_LENGTH (valtype) - offset;

  1839.           if (len > tdep->wordsize)
  1840.             len = tdep->wordsize;

  1841.           if (writebuf != NULL)
  1842.             {
  1843.               memset (regval, 0, sizeof regval);
  1844.               if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG
  1845.                   && offset == 0)
  1846.                 memcpy (regval + tdep->wordsize - len, writebuf, len);
  1847.               else
  1848.                 memcpy (regval, writebuf + offset, len);
  1849.               regcache_cooked_write (regcache, regnum, regval);
  1850.             }
  1851.           if (readbuf != NULL)
  1852.             {
  1853.               regcache_cooked_read (regcache, regnum, regval);
  1854.               if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG
  1855.                   && offset == 0)
  1856.                 memcpy (readbuf, regval + tdep->wordsize - len, len);
  1857.               else
  1858.                 memcpy (readbuf + offset, regval, len);
  1859.             }
  1860.         }
  1861.       return RETURN_VALUE_REGISTER_CONVENTION;
  1862.     }

  1863.   /* Handle plain base types.  */
  1864.   if (ppc64_sysv_abi_return_value_base (gdbarch, valtype, regcache,
  1865.                                         readbuf, writebuf, 0))
  1866.     return RETURN_VALUE_REGISTER_CONVENTION;

  1867.   return RETURN_VALUE_STRUCT_CONVENTION;
  1868. }