1 From fdaab18a65ed2529656baa64cb6169f34d7e507b Mon Sep 17 00:00:00 2001
2 From: James Cowgill <james410@cowgill.org.uk>
3 Date: Mon, 5 Jan 2015 15:17:01 +0000
4 Subject: [PATCH 2/3] Add support for mips64 abis in mips_retval.c
6 Signed-off-by: James Cowgill <james410@cowgill.org.uk>
8 backends/mips_retval.c | 104 ++++++++++++++++++++++++++++++++++++++++++++-----
9 1 file changed, 94 insertions(+), 10 deletions(-)
11 diff --git a/backends/mips_retval.c b/backends/mips_retval.c
12 index 33f12a7..d5c6ef0 100644
13 --- a/backends/mips_retval.c
14 +++ b/backends/mips_retval.c
15 @@ -91,6 +91,8 @@ enum mips_abi find_mips_abi(Elf *elf)
17 if ((elf_flags & EF_MIPS_ABI2))
19 + else if ((ehdr->e_ident[EI_CLASS] == ELFCLASS64))
20 + return MIPS_ABI_N64;
23 /* GCC creates a pseudo-section whose name describes the ABI. */
24 @@ -195,6 +197,57 @@ static const Dwarf_Op loc_aggregate[] =
26 #define nloc_aggregate 1
28 +/* Test if a struct member is a float */
29 +static int is_float_child(Dwarf_Die *childdie)
31 + /* Test if this is actually a struct member */
32 + if (dwarf_tag(childdie) != DW_TAG_member)
35 + /* Get type of member */
36 + Dwarf_Attribute attr_mem;
37 + Dwarf_Die child_type_mem;
38 + Dwarf_Die *child_typedie =
39 + dwarf_formref_die(dwarf_attr_integrate(childdie,
41 + &attr_mem), &child_type_mem);
43 + if (dwarf_tag(child_typedie) != DW_TAG_base_type)
46 + /* Get base subtype */
47 + Dwarf_Word encoding;
48 + if (dwarf_formudata (dwarf_attr_integrate (child_typedie,
50 + &attr_mem), &encoding) != 0)
53 + return encoding == DW_ATE_float;
56 +/* Returns the number of fpregs which can be returned in the given struct */
57 +static int get_struct_fpregs(Dwarf_Die *structtypedie)
59 + Dwarf_Die child_mem;
62 + /* Get first structure member */
63 + if (dwarf_child(structtypedie, &child_mem) != 0)
68 + /* Ensure this register is a float */
69 + if (!is_float_child(&child_mem))
74 + while (dwarf_siblingof (&child_mem, &child_mem) == 0);
80 mips_return_value_location (Dwarf_Die *functypedie, const Dwarf_Op **locp)
82 @@ -240,6 +293,7 @@ mips_return_value_location (Dwarf_Die *functypedie, const Dwarf_Op **locp)
83 tag = dwarf_tag (typedie);
90 @@ -258,8 +312,6 @@ mips_return_value_location (Dwarf_Die *functypedie, const Dwarf_Op **locp)
91 case DW_TAG_enumeration_type:
92 case DW_TAG_pointer_type:
93 case DW_TAG_ptr_to_member_type:
96 if (dwarf_formudata (dwarf_attr_integrate (typedie, DW_AT_byte_size,
97 &attr_mem), &size) != 0)
99 @@ -289,7 +341,7 @@ mips_return_value_location (Dwarf_Die *functypedie, const Dwarf_Op **locp)
100 if (size <= 4*regsize && abi == MIPS_ABI_O32)
101 return nloc_fpregquad;
107 *locp = ABI_LOC(loc_intreg, regsize);
108 @@ -298,18 +350,50 @@ mips_return_value_location (Dwarf_Die *functypedie, const Dwarf_Op **locp)
109 if (size <= 2*regsize)
110 return nloc_intregpair;
112 - /* Else fall through. Shouldn't happen though (at least with gcc) */
114 + /* Else pass in memory. Shouldn't happen though (at least with gcc) */
117 case DW_TAG_structure_type:
118 case DW_TAG_class_type:
119 case DW_TAG_union_type:
120 - case DW_TAG_array_type:
122 - /* XXX TODO: Can't handle structure return with other ABI's yet :-/ */
123 - if ((abi != MIPS_ABI_O32) && (abi != MIPS_ABI_O64))
125 + /* Handle special cases for structures <= 128 bytes in newer ABIs */
126 + if (abi == MIPS_ABI_EABI32 || abi == MIPS_ABI_EABI64 ||
127 + abi == MIPS_ABI_N32 || abi == MIPS_ABI_N64)
129 + if (dwarf_aggregate_size (typedie, &size) == 0 && size <= 16)
132 + * Special case in N64 / N32 -
133 + * structures containing only floats are returned in fp regs.
134 + * Everything else is returned in integer regs.
136 + if (tag != DW_TAG_union_type &&
137 + (abi == MIPS_ABI_N32 || abi == MIPS_ABI_N64))
139 + int num_fpregs = get_struct_fpregs(typedie);
140 + if (num_fpregs == 1 || num_fpregs == 2)
143 + if (num_fpregs == 1)
146 + return nloc_fpregpair;
150 + *locp = loc_intreg;
152 + return nloc_intreg;
154 + return nloc_intregpair;
158 + /* Fallthrough to handle large types */
160 + case DW_TAG_array_type:
162 + /* Return large structures in memory */
163 *locp = loc_aggregate;
164 return nloc_aggregate;