Blame backends/riscv_retval.c

Packit 032894
/* Function return value location for Linux/RISC-V ABI.
Packit 032894
   Copyright (C) 2018 Sifive, Inc.
Packit 032894
   Copyright (C) 2013 Red Hat, Inc.
Packit 032894
   This file is part of elfutils.
Packit 032894
Packit 032894
   This file is free software; you can redistribute it and/or modify
Packit 032894
   it under the terms of either
Packit 032894
Packit 032894
     * the GNU Lesser General Public License as published by the Free
Packit 032894
       Software Foundation; either version 3 of the License, or (at
Packit 032894
       your option) any later version
Packit 032894
Packit 032894
   or
Packit 032894
Packit 032894
     * the GNU General Public License as published by the Free
Packit 032894
       Software Foundation; either version 2 of the License, or (at
Packit 032894
       your option) any later version
Packit 032894
Packit 032894
   or both in parallel, as here.
Packit 032894
Packit 032894
   elfutils is distributed in the hope that it will be useful, but
Packit 032894
   WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 032894
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 032894
   General Public License for more details.
Packit 032894
Packit 032894
   You should have received copies of the GNU General Public License and
Packit 032894
   the GNU Lesser General Public License along with this program.  If
Packit 032894
   not, see <http://www.gnu.org/licenses/>.  */
Packit 032894
Packit 032894
#ifdef HAVE_CONFIG_H
Packit 032894
# include <config.h>
Packit 032894
#endif
Packit 032894
Packit 032894
#include <stdio.h>
Packit 032894
#include <inttypes.h>
Packit 032894
Packit 032894
#include <assert.h>
Packit 032894
#include <dwarf.h>
Packit 032894
Packit 032894
#define BACKEND riscv_
Packit 032894
#include "libebl_CPU.h"
Packit 032894
Packit 032894
static int
Packit 032894
dwarf_bytesize_aux (Dwarf_Die *die, Dwarf_Word *sizep)
Packit 032894
{
Packit 032894
  int bits;
Packit 032894
  if (((bits = 8 * dwarf_bytesize (die)) < 0
Packit 032894
       && (bits = dwarf_bitsize (die)) < 0)
Packit 032894
      || bits % 8 != 0)
Packit 032894
    return -1;
Packit 032894
Packit 032894
  *sizep = bits / 8;
Packit 032894
  return 0;
Packit 032894
}
Packit 032894
Packit 032894
static int
Packit 032894
pass_in_gpr_lp64 (const Dwarf_Op **locp, Dwarf_Word size)
Packit 032894
{
Packit 032894
  static const Dwarf_Op loc[] =
Packit 032894
    {
Packit 032894
      { .atom = DW_OP_reg10 }, { .atom = DW_OP_piece, .number = 8 },
Packit 032894
      { .atom = DW_OP_reg11 }, { .atom = DW_OP_piece, .number = 8 }
Packit 032894
    };
Packit 032894
Packit 032894
  *locp = loc;
Packit 032894
  return size <= 8 ? 1 : 4;
Packit 032894
}
Packit 032894
Packit 032894
static int
Packit 032894
pass_by_ref (const Dwarf_Op **locp)
Packit 032894
{
Packit 032894
  static const Dwarf_Op loc[] = { { .atom = DW_OP_breg10 } };
Packit 032894
Packit 032894
  *locp = loc;
Packit 032894
  return 1;
Packit 032894
}
Packit 032894
Packit 032894
static int
Packit 032894
pass_in_fpr_lp64f (const Dwarf_Op **locp, Dwarf_Word size)
Packit 032894
{
Packit 032894
  static const Dwarf_Op loc[] =
Packit 032894
    {
Packit 032894
      { .atom = DW_OP_regx, .number = 42 },
Packit 032894
      { .atom = DW_OP_piece, .number = 4 },
Packit 032894
      { .atom = DW_OP_regx, .number = 43 },
Packit 032894
      { .atom = DW_OP_piece, .number = 4 }
Packit 032894
    };
Packit 032894
Packit 032894
  *locp = loc;
Packit 032894
  return size <= 4 ? 1 : 4;
Packit 032894
}
Packit 032894
Packit 032894
static int
Packit 032894
pass_in_fpr_lp64d (const Dwarf_Op **locp, Dwarf_Word size)
Packit 032894
{
Packit 032894
  static const Dwarf_Op loc[] =
Packit 032894
    {
Packit 032894
      { .atom = DW_OP_regx, .number = 42 },
Packit 032894
      { .atom = DW_OP_piece, .number = 8 },
Packit 032894
      { .atom = DW_OP_regx, .number = 43 },
Packit 032894
      { .atom = DW_OP_piece, .number = 8 }
Packit 032894
    };
Packit 032894
Packit 032894
  *locp = loc;
Packit 032894
  return size <= 8 ? 1 : 4;
Packit 032894
}
Packit 032894
Packit 032894
static int
Packit 032894
flatten_aggregate_arg (Dwarf_Die *typedie __attribute__ ((unused)),
Packit 032894
		       Dwarf_Die *arg0 __attribute__ ((unused)),
Packit 032894
		       Dwarf_Die *arg1 __attribute__ ((unused)))
Packit 032894
{
Packit 032894
  /* ??? */
Packit 032894
  return 1;
Packit 032894
}
Packit 032894
Packit 032894
static int
Packit 032894
pass_by_flattened_arg (const Dwarf_Op **locp __attribute__ ((unused)),
Packit 032894
		       Dwarf_Word size __attribute__ ((unused)),
Packit 032894
		       Dwarf_Die *arg0 __attribute__ ((unused)),
Packit 032894
		       Dwarf_Die *arg1 __attribute__ ((unused)))
Packit 032894
{
Packit 032894
  /* ??? */
Packit 032894
  return -2;
Packit 032894
}
Packit 032894
Packit 032894
int
Packit 032894
riscv_return_value_location_lp64d (Dwarf_Die *functypedie,
Packit 032894
				   const Dwarf_Op **locp)
Packit 032894
{
Packit 032894
  /* Start with the function's type, and get the DW_AT_type attribute,
Packit 032894
     which is the type of the return value.  */
Packit 032894
  Dwarf_Die typedie;
Packit 032894
  int tag = dwarf_peeled_die_type (functypedie, &typedie);
Packit 032894
  if (tag <= 0)
Packit 032894
    return tag;
Packit 032894
Packit 032894
  Dwarf_Word size = (Dwarf_Word)-1;
Packit 032894
Packit 032894
  /* If the argument type is a Composite Type that is larger than 16
Packit 032894
     bytes, then the argument is copied to memory allocated by the
Packit 032894
     caller and the argument is replaced by a pointer to the copy.  */
Packit 032894
  if (tag == DW_TAG_structure_type || tag == DW_TAG_union_type
Packit 032894
      || tag == DW_TAG_class_type || tag == DW_TAG_array_type)
Packit 032894
    {
Packit 032894
      Dwarf_Die arg0, arg1;
Packit 032894
Packit 032894
      if (dwarf_aggregate_size (&typedie, &size) < 0)
Packit 032894
	return -1;
Packit 032894
      /* A struct containing just one floating-point real is passed as though
Packit 032894
	 it were a standalone floating-point real.  A struct containing two
Packit 032894
	 floating-point reals is passed in two floating-point registers, if
Packit 032894
	 neither is more than FLEN bits wide.  A struct containing just one
Packit 032894
	 complex floating-point number is passed as though it were a struct
Packit 032894
	 containing two floating-point reals.  A struct containing one
Packit 032894
	 floating-point real and one integer (or bitfield), in either order,
Packit 032894
	 is passed in a floating-point register and an integer register,
Packit 032894
	 provided the floating-point real is no more than FLEN bits wide and
Packit 032894
	 the integer is no more than XLEN bits wide.  */
Packit 032894
      if (tag == DW_TAG_structure_type
Packit 032894
	  && flatten_aggregate_arg (&typedie, &arg0, &arg1))
Packit 032894
	return pass_by_flattened_arg (locp, size, &arg0, &arg1);
Packit 032894
      /* Aggregates larger than 2*XLEN bits are passed by reference.  */
Packit 032894
      else if (size > 16)
Packit 032894
	return pass_by_ref (locp);
Packit 032894
      /* Aggregates whose total size is no more than XLEN bits are passed in
Packit 032894
	 a register.  Aggregates whose total size is no more than 2*XLEN bits
Packit 032894
	 are passed in a pair of registers.  */
Packit 032894
      else
Packit 032894
	return pass_in_gpr_lp64 (locp, size);
Packit 032894
    }
Packit 032894
Packit 032894
  if (tag == DW_TAG_base_type
Packit 032894
      || tag == DW_TAG_pointer_type || tag == DW_TAG_ptr_to_member_type)
Packit 032894
    {
Packit 032894
      if (dwarf_bytesize_aux (&typedie, &size) < 0)
Packit 032894
	{
Packit 032894
	  if (tag == DW_TAG_pointer_type || tag == DW_TAG_ptr_to_member_type)
Packit 032894
	    size = 8;
Packit 032894
	  else
Packit 032894
	    return -1;
Packit 032894
	}
Packit 032894
Packit 032894
      Dwarf_Attribute attr_mem;
Packit 032894
      if (tag == DW_TAG_base_type)
Packit 032894
	{
Packit 032894
	  Dwarf_Word encoding;
Packit 032894
	  if (dwarf_formudata (dwarf_attr_integrate (&typedie, DW_AT_encoding,
Packit 032894
						     &attr_mem),
Packit 032894
			       &encoding) != 0)
Packit 032894
	    return -1;
Packit 032894
Packit 032894
	  switch (encoding)
Packit 032894
	    {
Packit 032894
	    case DW_ATE_boolean:
Packit 032894
	    case DW_ATE_signed:
Packit 032894
	    case DW_ATE_unsigned:
Packit 032894
	    case DW_ATE_unsigned_char:
Packit 032894
	    case DW_ATE_signed_char:
Packit 032894
	      /* Scalars that are at most XLEN bits wide are passed in a single
Packit 032894
		 argument register.  Scalars that are 2*XLEN bits wide are
Packit 032894
		 passed in a pair of argument registers.  Scalars wider than
Packit 032894
		 2*XLEN are passed by reference; there are none for LP64D.  */
Packit 032894
	      return pass_in_gpr_lp64 (locp, size);
Packit 032894
Packit 032894
	    case DW_ATE_float:
Packit 032894
	      /* A real floating-point argument is passed in a floating-point
Packit 032894
		 argument register if it is no more than FLEN bits wide,
Packit 032894
		 otherwise it is passed according to the integer calling
Packit 032894
		 convention.  */
Packit 032894
	      switch (size)
Packit 032894
		{
Packit 032894
		case 4: /* single */
Packit 032894
		case 8: /* double */
Packit 032894
		  return pass_in_fpr_lp64d (locp, size);
Packit 032894
Packit 032894
		case 16: /* quad */
Packit 032894
		  return pass_in_gpr_lp64 (locp, size);
Packit 032894
Packit 032894
		default:
Packit 032894
		  return -2;
Packit 032894
		}
Packit 032894
Packit 032894
	    case DW_ATE_complex_float:
Packit 032894
	      /* A complex floating-point number is passed as though it were a
Packit 032894
		 struct containing two floating-point reals.  */
Packit 032894
	      switch (size)
Packit 032894
		{
Packit 032894
		case 8: /* float _Complex */
Packit 032894
		  return pass_in_fpr_lp64f (locp, size);
Packit 032894
Packit 032894
		case 16: /* double _Complex */
Packit 032894
		  return pass_in_fpr_lp64d (locp, size);
Packit 032894
Packit 032894
		case 32: /* long double _Complex */
Packit 032894
		  return pass_by_ref (locp);
Packit 032894
Packit 032894
		default:
Packit 032894
		  return -2;
Packit 032894
		}
Packit 032894
	    }
Packit 032894
Packit 032894
	  return -2;
Packit 032894
	}
Packit 032894
      else
Packit 032894
	return pass_in_gpr_lp64 (locp, size);
Packit 032894
    }
Packit 032894
Packit 032894
  *locp = NULL;
Packit 032894
  return 0;
Packit 032894
}