blob: 34cae064ae6d6b5e492c7ddd49107790d7caceb8 [file] [log] [blame]
Roland McGrathe47ab762005-11-17 03:16:00 +00001/* Function return value location for Linux/i386 ABI.
2 Copyright (C) 2005 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 i386_
22#include "libebl_CPU.h"
23
24
25/* %eax, or pair %eax, %edx. */
26static const Dwarf_Op loc_intreg[] =
27 {
28 { .atom = DW_OP_reg0 }, { .atom = DW_OP_piece, .number = 4 },
29 { .atom = DW_OP_reg1 }, { .atom = DW_OP_piece, .number = 4 },
30 };
31#define nloc_intreg 1
32#define nloc_intregpair 4
33
34/* %st(0). */
35static const Dwarf_Op loc_fpreg[] =
36 {
37 { .atom = DW_OP_reg11 }
38 };
39#define nloc_fpreg 1
40
41/* The return value is a structure and is actually stored in stack space
42 passed in a hidden argument by the caller. But, the compiler
43 helpfully returns the address of that space in %eax. */
44static const Dwarf_Op loc_aggregate[] =
45 {
46 { .atom = DW_OP_breg0, .number = 0 }
47 };
48#define nloc_aggregate 1
49
50int
51i386_return_value_location (Dwarf_Die *functypedie, const Dwarf_Op **locp)
52{
53 /* Start with the function's type, and get the DW_AT_type attribute,
54 which is the type of the return value. */
55
56 Dwarf_Attribute attr_mem;
57 Dwarf_Attribute *attr = dwarf_attr (functypedie, DW_AT_type, &attr_mem);
58 if (attr == NULL)
59 /* The function has no return value, like a `void' function in C. */
60 return 0;
61
62 Dwarf_Die die_mem;
63 Dwarf_Die *typedie = dwarf_formref_die (attr, &die_mem);
64 int tag = dwarf_tag (typedie);
65
66 /* Follow typedefs and qualifiers to get to the actual type. */
67 while (tag == DW_TAG_typedef
68 || tag == DW_TAG_const_type || tag == DW_TAG_volatile_type
69 || tag == DW_TAG_restrict_type || tag == DW_TAG_mutable_type)
70 {
71 attr = dwarf_attr (typedie, DW_AT_type, &attr_mem);
72 typedie = dwarf_formref_die (attr, &die_mem);
73 tag = dwarf_tag (typedie);
74 }
75
76 switch (tag)
77 {
78 case -1:
79 return -1;
80
81 case DW_TAG_subrange_type:
82 if (! dwarf_hasattr (typedie, DW_AT_byte_size))
83 {
84 attr = dwarf_attr (typedie, DW_AT_type, &attr_mem);
85 typedie = dwarf_formref_die (attr, &die_mem);
86 tag = dwarf_tag (typedie);
87 }
88 /* Fall through. */
89
90 case DW_TAG_base_type:
91 case DW_TAG_enumeration_type:
92 case DW_TAG_pointer_type:
93 case DW_TAG_ptr_to_member_type:
94 {
95 Dwarf_Word size;
96 if (dwarf_formudata (dwarf_attr (typedie, DW_AT_byte_size,
97 &attr_mem), &size) != 0)
98 {
99 if (tag == DW_TAG_pointer_type || tag == DW_TAG_ptr_to_member_type)
100 size = 4;
101 else
102 return -1;
103 }
104 if (tag == DW_TAG_base_type)
105 {
106 Dwarf_Word encoding;
107 if (dwarf_formudata (dwarf_attr (typedie, DW_AT_encoding,
108 &attr_mem), &encoding) != 0)
109 return -1;
110 if (encoding == DW_ATE_float)
111 {
112 if (size > 16)
113 return -2;
114 *locp = loc_fpreg;
115 return nloc_fpreg;
116 }
117 }
118 *locp = loc_intreg;
119 if (size <= 4)
120 return nloc_intreg;
121 if (size <= 8)
122 return nloc_intregpair;
123
124 /* Else fall through. */
125 }
126
127 case DW_TAG_structure_type:
128 case DW_TAG_class_type:
129 case DW_TAG_union_type:
130 case DW_TAG_array_type:
131 *locp = loc_aggregate;
132 return nloc_aggregate;
133 }
134
135 /* XXX We don't have a good way to return specific errors from ebl calls.
136 This value means we do not understand the type, but it is well-formed
137 DWARF and might be valid. */
138 return -2;
139}