1/* Function return value location for Linux/TILE-Gx ABI.
2   Copyright (C) 2012 Tilera Corporation
3   Copyright (C) 2014 Red Hat, Inc.
4   This file is part of elfutils.
5
6   This file is free software; you can redistribute it and/or modify
7   it under the terms of either
8
9     * the GNU Lesser General Public License as published by the Free
10       Software Foundation; either version 3 of the License, or (at
11       your option) any later version
12
13   or
14
15     * the GNU General Public License as published by the Free
16       Software Foundation; either version 2 of the License, or (at
17       your option) any later version
18
19   or both in parallel, as here.
20
21   elfutils is distributed in the hope that it will be useful, but
22   WITHOUT ANY WARRANTY; without even the implied warranty of
23   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
24   General Public License for more details.
25
26   You should have received copies of the GNU General Public License and
27   the GNU Lesser General Public License along with this program.  If
28   not, see <http://www.gnu.org/licenses/>.  */
29
30
31#ifdef HAVE_CONFIG_H
32# include <config.h>
33#endif
34
35#include <assert.h>
36#include <dwarf.h>
37
38#define BACKEND tilegx_
39#include "libebl_CPU.h"
40
41
42/* r0.  */
43static const Dwarf_Op loc_intreg[] =
44  {
45    { .atom = DW_OP_reg0 }
46  };
47#define nloc_intreg	1
48
49/* The return value is a structure and is actually stored in stack space
50   passed in a hidden argument by the caller.  But, the compiler
51   helpfully returns the address of that space in r0.  */
52static const Dwarf_Op loc_aggregate[] =
53  {
54    { .atom = DW_OP_breg0, .number = 0 }
55  };
56#define nloc_aggregate 1
57
58int
59tilegx_return_value_location (Dwarf_Die *functypedie, const Dwarf_Op **locp)
60{
61  /* Start with the function's type, and get the DW_AT_type attribute,
62     which is the type of the return value.  */
63  Dwarf_Die die_mem, *typedie = &die_mem;
64  int tag = dwarf_peeled_die_type (functypedie, typedie);
65  if (tag <= 0)
66    return tag;
67
68  Dwarf_Word size;
69  switch (tag)
70    {
71    case -1:
72      return -1;
73
74    case DW_TAG_subrange_type:
75      if (! dwarf_hasattr_integrate (typedie, DW_AT_byte_size))
76	{
77	  Dwarf_Attribute attr_mem, *attr;
78	  attr = dwarf_attr_integrate (typedie, DW_AT_type, &attr_mem);
79	  typedie = dwarf_formref_die (attr, &die_mem);
80	  tag = DWARF_TAG_OR_RETURN (typedie);
81	}
82      /* Fall through.  */
83
84    case DW_TAG_base_type:
85    case DW_TAG_enumeration_type:
86    case DW_TAG_pointer_type:
87    case DW_TAG_ptr_to_member_type:
88      {
89	Dwarf_Attribute attr_mem;
90	if (dwarf_formudata (dwarf_attr_integrate (typedie, DW_AT_byte_size,
91						   &attr_mem), &size) != 0)
92	  {
93	    if (tag == DW_TAG_pointer_type || tag == DW_TAG_ptr_to_member_type)
94	      size = 8;
95	    else
96	      return -1;
97	  }
98	if (tag == DW_TAG_base_type)
99	  {
100	    Dwarf_Word encoding;
101	    if (dwarf_formudata (dwarf_attr_integrate (typedie, DW_AT_encoding,
102						       &attr_mem),
103				 &encoding) != 0)
104	      return -1;
105	  }
106      }
107
108      /* Small enough structs are passed directly in registers R0 ... R7.  */
109      if (size <= 8)
110	{
111	intreg:
112	  *locp = loc_intreg;
113	  return nloc_intreg;
114	}
115
116      /* Else fall through.  */
117    case DW_TAG_structure_type:
118    case DW_TAG_class_type:
119    case DW_TAG_union_type:
120    aggregate:
121      *locp = loc_aggregate;
122      return nloc_aggregate;
123
124    case DW_TAG_array_type:
125    case DW_TAG_string_type:
126      if (dwarf_aggregate_size (typedie, &size) == 0 && size <= 8)
127	{
128	  if (tag == DW_TAG_array_type)
129	    {
130	      Dwarf_Attribute attr_mem, *attr;
131	      /* Check if it's a character array.  */
132	      attr = dwarf_attr_integrate (typedie, DW_AT_type, &attr_mem);
133	      typedie = dwarf_formref_die (attr, &die_mem);
134	      tag = DWARF_TAG_OR_RETURN (typedie);
135	      if (tag != DW_TAG_base_type)
136		goto aggregate;
137	      if (dwarf_formudata (dwarf_attr_integrate (typedie,
138							 DW_AT_byte_size,
139							 &attr_mem),
140				   &size) != 0)
141		return -1;
142	      if (size != 1)
143		goto aggregate;
144	    }
145	  goto intreg;
146	}
147      goto aggregate;
148    }
149
150  /* XXX We don't have a good way to return specific errors from ebl calls.
151     This value means we do not understand the type, but it is well-formed
152     DWARF and might be valid.  */
153  return -2;
154}
155