blob: cfde0d2fb8ffc37c4430cd3b8d3fe69a5cb48c6f [file] [log] [blame]
Roland McGrathc373d852006-10-10 00:25:21 +00001/* Function return value location for SPARC.
2 Copyright (C) 2006 Red Hat, Inc.
3
4 This program is Open Source software; you can redistribute it and/or
5 modify it under the terms of the Open Software License version 1.0 as
6 published by the Open Source Initiative.
7
8 You should have received a copy of the Open Software License along
9 with this program; if not, you may obtain a copy of the Open Software
10 License version 1.0 from http://www.opensource.org/licenses/osl.php or
11 by writing the Open Source Initiative c/o Lawrence Rosen, Esq.,
12 3001 King Ranch Road, Ukiah, CA 95482. */
13
14#ifdef HAVE_CONFIG_H
15# include <config.h>
16#endif
17
18#include <assert.h>
19#include <dwarf.h>
20
21#define BACKEND sparc_
22#include "libebl_CPU.h"
23
24
25/* %o0, or pair %o0, %o1. */
26static const Dwarf_Op loc_intreg[] =
27 {
28 { .atom = DW_OP_reg8 }, { .atom = DW_OP_piece, .number = 4 },
29 { .atom = DW_OP_reg9 }, { .atom = DW_OP_piece, .number = 4 },
30 };
31#define nloc_intreg 1
32#define nloc_intregpair 4
33
34/* %f0 or pair %f0, %f1, or quad %f0..%f3. */
35static const Dwarf_Op loc_fpreg[] =
36 {
37 { .atom = DW_OP_regx, .number = 32 }, { .atom = DW_OP_piece, .number = 4 },
38 { .atom = DW_OP_regx, .number = 33 }, { .atom = DW_OP_piece, .number = 4 },
39 { .atom = DW_OP_regx, .number = 34 }, { .atom = DW_OP_piece, .number = 4 },
40 { .atom = DW_OP_regx, .number = 35 }, { .atom = DW_OP_piece, .number = 4 },
41 };
42#define nloc_fpreg 1
43#define nloc_fpregpair 4
44#define nloc_fpregquad 8
45
46/* The return value is a structure and is actually stored in stack space
47 passed in a hidden argument by the caller. But, the compiler
48 helpfully returns the address of that space in %o0. */
49static const Dwarf_Op loc_aggregate[] =
50 {
51 { .atom = DW_OP_breg8, .number = 0 }
52 };
53#define nloc_aggregate 1
54
55int
56sparc_return_value_location (Dwarf_Die *functypedie, const Dwarf_Op **locp)
57{
58 /* Start with the function's type, and get the DW_AT_type attribute,
59 which is the type of the return value. */
60
61 Dwarf_Attribute attr_mem;
62 Dwarf_Attribute *attr = dwarf_attr (functypedie, DW_AT_type, &attr_mem);
63 if (attr == NULL)
64 /* The function has no return value, like a `void' function in C. */
65 return 0;
66
67 Dwarf_Die die_mem;
68 Dwarf_Die *typedie = dwarf_formref_die (attr, &die_mem);
69 int tag = dwarf_tag (typedie);
70
71 /* Follow typedefs and qualifiers to get to the actual type. */
72 while (tag == DW_TAG_typedef
73 || tag == DW_TAG_const_type || tag == DW_TAG_volatile_type
74 || tag == DW_TAG_restrict_type || tag == DW_TAG_mutable_type)
75 {
76 attr = dwarf_attr (typedie, DW_AT_type, &attr_mem);
77 typedie = dwarf_formref_die (attr, &die_mem);
78 tag = dwarf_tag (typedie);
79 }
80
81 Dwarf_Word size;
82 switch (tag)
83 {
84 case -1:
85 return -1;
86
87 case DW_TAG_subrange_type:
88 if (! dwarf_hasattr (typedie, DW_AT_byte_size))
89 {
90 attr = dwarf_attr (typedie, DW_AT_type, &attr_mem);
91 typedie = dwarf_formref_die (attr, &die_mem);
92 tag = dwarf_tag (typedie);
93 }
94 /* Fall through. */
95
96 case DW_TAG_base_type:
97 case DW_TAG_enumeration_type:
98 case DW_TAG_pointer_type:
99 case DW_TAG_ptr_to_member_type:
100 if (dwarf_formudata (dwarf_attr (typedie, DW_AT_byte_size,
101 &attr_mem), &size) != 0)
102 {
103 uint8_t asize;
104 Dwarf_Die cudie;
105 if ((tag == DW_TAG_pointer_type || tag == DW_TAG_ptr_to_member_type)
106 && dwarf_diecu (typedie, &cudie, &asize, NULL) != NULL)
107 size = asize;
108 else
109 return -1;
110 }
111 if (tag == DW_TAG_base_type)
112 {
113 Dwarf_Word encoding;
114 if (dwarf_formudata (dwarf_attr (typedie, DW_AT_encoding,
115 &attr_mem), &encoding) != 0)
116 return -1;
117 if (encoding == DW_ATE_float)
118 {
119 *locp = loc_fpreg;
120 if (size <= 4)
121 return nloc_fpreg;
122 if (size <= 8)
123 return nloc_fpregpair;
124 if (size <= 16)
125 return nloc_fpregquad;
126 }
127 }
128 if (size <= 8)
129 {
130 intreg:
131 *locp = loc_intreg;
132 return size <= 4 ? nloc_intreg : nloc_intregpair;
133 }
134
135 aggregate:
136 *locp = loc_aggregate;
137 return nloc_aggregate;
138
139 case DW_TAG_structure_type:
140 case DW_TAG_class_type:
141 case DW_TAG_union_type:
142 case DW_TAG_array_type:
143 if (dwarf_formudata (dwarf_attr (typedie, DW_AT_byte_size,
144 &attr_mem), &size) == 0
145 && size > 0 && size <= 8)
146 goto intreg;
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}