| 1 | /* Target-dependent code for PowerPC systems using the SVR4 ABI |
| 2 | for GDB, the GNU debugger. |
| 3 | |
| 4 | Copyright 2000, 2001, 2002 Free Software Foundation, Inc. |
| 5 | |
| 6 | This file is part of GDB. |
| 7 | |
| 8 | This program is free software; you can redistribute it and/or modify |
| 9 | it under the terms of the GNU General Public License as published by |
| 10 | the Free Software Foundation; either version 2 of the License, or |
| 11 | (at your option) any later version. |
| 12 | |
| 13 | This program is distributed in the hope that it will be useful, |
| 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | GNU General Public License for more details. |
| 17 | |
| 18 | You should have received a copy of the GNU General Public License |
| 19 | along with this program; if not, write to the Free Software |
| 20 | Foundation, Inc., 59 Temple Place - Suite 330, |
| 21 | Boston, MA 02111-1307, USA. */ |
| 22 | |
| 23 | #include "defs.h" |
| 24 | #include "gdbcore.h" |
| 25 | #include "inferior.h" |
| 26 | #include "regcache.h" |
| 27 | #include "value.h" |
| 28 | |
| 29 | #include "ppc-tdep.h" |
| 30 | |
| 31 | /* round2 rounds x up to the nearest multiple of s assuming that s is a |
| 32 | power of 2 */ |
| 33 | |
| 34 | #undef round2 |
| 35 | #define round2(x,s) ((((long) (x) - 1) & ~(long)((s)-1)) + (s)) |
| 36 | |
| 37 | /* Pass the arguments in either registers, or in the stack. Using the |
| 38 | ppc sysv ABI, the first eight words of the argument list (that might |
| 39 | be less than eight parameters if some parameters occupy more than one |
| 40 | word) are passed in r3..r10 registers. float and double parameters are |
| 41 | passed in fpr's, in addition to that. Rest of the parameters if any |
| 42 | are passed in user stack. |
| 43 | |
| 44 | If the function is returning a structure, then the return address is passed |
| 45 | in r3, then the first 7 words of the parametes can be passed in registers, |
| 46 | starting from r4. */ |
| 47 | |
| 48 | CORE_ADDR |
| 49 | ppc_sysv_abi_push_arguments (int nargs, struct value **args, CORE_ADDR sp, |
| 50 | int struct_return, CORE_ADDR struct_addr) |
| 51 | { |
| 52 | int argno; |
| 53 | /* Next available general register for non-float, non-vector arguments. */ |
| 54 | int greg; |
| 55 | /* Next available floating point register for float arguments. */ |
| 56 | int freg; |
| 57 | /* Next available vector register for vector arguments. */ |
| 58 | int vreg; |
| 59 | int argstkspace; |
| 60 | int structstkspace; |
| 61 | int argoffset; |
| 62 | int structoffset; |
| 63 | struct value *arg; |
| 64 | struct type *type; |
| 65 | int len; |
| 66 | char old_sp_buf[4]; |
| 67 | CORE_ADDR saved_sp; |
| 68 | |
| 69 | greg = struct_return ? 4 : 3; |
| 70 | freg = 1; |
| 71 | vreg = 2; |
| 72 | argstkspace = 0; |
| 73 | structstkspace = 0; |
| 74 | |
| 75 | /* Figure out how much new stack space is required for arguments |
| 76 | which don't fit in registers. Unlike the PowerOpen ABI, the |
| 77 | SysV ABI doesn't reserve any extra space for parameters which |
| 78 | are put in registers. */ |
| 79 | for (argno = 0; argno < nargs; argno++) |
| 80 | { |
| 81 | arg = args[argno]; |
| 82 | type = check_typedef (VALUE_TYPE (arg)); |
| 83 | len = TYPE_LENGTH (type); |
| 84 | |
| 85 | if (TYPE_CODE (type) == TYPE_CODE_FLT) |
| 86 | { |
| 87 | if (freg <= 8) |
| 88 | freg++; |
| 89 | else |
| 90 | { |
| 91 | /* SysV ABI converts floats to doubles when placed in |
| 92 | memory and requires 8 byte alignment */ |
| 93 | if (argstkspace & 0x4) |
| 94 | argstkspace += 4; |
| 95 | argstkspace += 8; |
| 96 | } |
| 97 | } |
| 98 | else if (TYPE_CODE (type) == TYPE_CODE_INT && len == 8) /* long long */ |
| 99 | { |
| 100 | if (greg > 9) |
| 101 | { |
| 102 | greg = 11; |
| 103 | if (argstkspace & 0x4) |
| 104 | argstkspace += 4; |
| 105 | argstkspace += 8; |
| 106 | } |
| 107 | else |
| 108 | { |
| 109 | if ((greg & 1) == 0) |
| 110 | greg++; |
| 111 | greg += 2; |
| 112 | } |
| 113 | } |
| 114 | else if (!TYPE_VECTOR (type)) |
| 115 | { |
| 116 | if (len > 4 |
| 117 | || TYPE_CODE (type) == TYPE_CODE_STRUCT |
| 118 | || TYPE_CODE (type) == TYPE_CODE_UNION) |
| 119 | { |
| 120 | /* Rounding to the nearest multiple of 8 may not be necessary, |
| 121 | but it is safe. Particularly since we don't know the |
| 122 | field types of the structure */ |
| 123 | structstkspace += round2 (len, 8); |
| 124 | } |
| 125 | if (greg <= 10) |
| 126 | greg++; |
| 127 | else |
| 128 | argstkspace += 4; |
| 129 | } |
| 130 | else |
| 131 | { |
| 132 | if (len == 16 |
| 133 | && TYPE_CODE (type) == TYPE_CODE_ARRAY |
| 134 | && TYPE_VECTOR (type)) |
| 135 | { |
| 136 | if (vreg <= 13) |
| 137 | vreg++; |
| 138 | else |
| 139 | { |
| 140 | /* Vector arguments must be aligned to 16 bytes on |
| 141 | the stack. */ |
| 142 | argstkspace += round2 (argstkspace, 16); |
| 143 | argstkspace += 16; |
| 144 | } |
| 145 | } |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | /* Get current SP location */ |
| 150 | saved_sp = read_sp (); |
| 151 | |
| 152 | sp -= argstkspace + structstkspace; |
| 153 | |
| 154 | /* Allocate space for backchain and callee's saved lr */ |
| 155 | sp -= 8; |
| 156 | |
| 157 | /* Make sure that we maintain 16 byte alignment */ |
| 158 | sp &= ~0x0f; |
| 159 | |
| 160 | /* Update %sp before proceeding any further */ |
| 161 | write_register (SP_REGNUM, sp); |
| 162 | |
| 163 | /* write the backchain */ |
| 164 | store_address (old_sp_buf, 4, saved_sp); |
| 165 | write_memory (sp, old_sp_buf, 4); |
| 166 | |
| 167 | argoffset = 8; |
| 168 | structoffset = argoffset + argstkspace; |
| 169 | freg = 1; |
| 170 | greg = 3; |
| 171 | vreg = 2; |
| 172 | /* Fill in r3 with the return structure, if any */ |
| 173 | if (struct_return) |
| 174 | { |
| 175 | char val_buf[4]; |
| 176 | store_address (val_buf, 4, struct_addr); |
| 177 | memcpy (®isters[REGISTER_BYTE (greg)], val_buf, 4); |
| 178 | greg++; |
| 179 | } |
| 180 | /* Now fill in the registers and stack... */ |
| 181 | for (argno = 0; argno < nargs; argno++) |
| 182 | { |
| 183 | arg = args[argno]; |
| 184 | type = check_typedef (VALUE_TYPE (arg)); |
| 185 | len = TYPE_LENGTH (type); |
| 186 | |
| 187 | if (TYPE_CODE (type) == TYPE_CODE_FLT) |
| 188 | { |
| 189 | if (freg <= 8) |
| 190 | { |
| 191 | if (len > 8) |
| 192 | printf_unfiltered ( |
| 193 | "Fatal Error: a floating point parameter #%d with a size > 8 is found!\n", argno); |
| 194 | memcpy (®isters[REGISTER_BYTE (FP0_REGNUM + freg)], |
| 195 | VALUE_CONTENTS (arg), len); |
| 196 | freg++; |
| 197 | } |
| 198 | else |
| 199 | { |
| 200 | /* SysV ABI converts floats to doubles when placed in |
| 201 | memory and requires 8 byte alignment */ |
| 202 | /* FIXME: Convert floats to doubles */ |
| 203 | if (argoffset & 0x4) |
| 204 | argoffset += 4; |
| 205 | write_memory (sp + argoffset, (char *) VALUE_CONTENTS (arg), len); |
| 206 | argoffset += 8; |
| 207 | } |
| 208 | } |
| 209 | else if (TYPE_CODE (type) == TYPE_CODE_INT && len == 8) /* long long */ |
| 210 | { |
| 211 | if (greg > 9) |
| 212 | { |
| 213 | greg = 11; |
| 214 | if (argoffset & 0x4) |
| 215 | argoffset += 4; |
| 216 | write_memory (sp + argoffset, (char *) VALUE_CONTENTS (arg), len); |
| 217 | argoffset += 8; |
| 218 | } |
| 219 | else |
| 220 | { |
| 221 | if ((greg & 1) == 0) |
| 222 | greg++; |
| 223 | |
| 224 | memcpy (®isters[REGISTER_BYTE (greg)], |
| 225 | VALUE_CONTENTS (arg), 4); |
| 226 | memcpy (®isters[REGISTER_BYTE (greg + 1)], |
| 227 | VALUE_CONTENTS (arg) + 4, 4); |
| 228 | greg += 2; |
| 229 | } |
| 230 | } |
| 231 | else if (!TYPE_VECTOR (type)) |
| 232 | { |
| 233 | char val_buf[4]; |
| 234 | if (len > 4 |
| 235 | || TYPE_CODE (type) == TYPE_CODE_STRUCT |
| 236 | || TYPE_CODE (type) == TYPE_CODE_UNION) |
| 237 | { |
| 238 | write_memory (sp + structoffset, VALUE_CONTENTS (arg), len); |
| 239 | store_address (val_buf, 4, sp + structoffset); |
| 240 | structoffset += round2 (len, 8); |
| 241 | } |
| 242 | else |
| 243 | { |
| 244 | memset (val_buf, 0, 4); |
| 245 | memcpy (val_buf, VALUE_CONTENTS (arg), len); |
| 246 | } |
| 247 | if (greg <= 10) |
| 248 | { |
| 249 | memcpy (®isters[REGISTER_BYTE (greg)], val_buf, 4); |
| 250 | greg++; |
| 251 | } |
| 252 | else |
| 253 | { |
| 254 | write_memory (sp + argoffset, val_buf, 4); |
| 255 | argoffset += 4; |
| 256 | } |
| 257 | } |
| 258 | else |
| 259 | { |
| 260 | if (len == 16 |
| 261 | && TYPE_CODE (type) == TYPE_CODE_ARRAY |
| 262 | && TYPE_VECTOR (type)) |
| 263 | { |
| 264 | struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch); |
| 265 | char *v_val_buf = alloca (16); |
| 266 | memset (v_val_buf, 0, 16); |
| 267 | memcpy (v_val_buf, VALUE_CONTENTS (arg), len); |
| 268 | if (vreg <= 13) |
| 269 | { |
| 270 | memcpy (®isters[REGISTER_BYTE (tdep->ppc_vr0_regnum |
| 271 | + vreg)], |
| 272 | v_val_buf, 16); |
| 273 | vreg++; |
| 274 | } |
| 275 | else |
| 276 | { |
| 277 | write_memory (sp + argoffset, v_val_buf, 16); |
| 278 | argoffset += 16; |
| 279 | } |
| 280 | } |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | target_store_registers (-1); |
| 285 | return sp; |
| 286 | } |
| 287 | |
| 288 | /* Until November 2001, gcc was not complying to the SYSV ABI for |
| 289 | returning structures less than or equal to 8 bytes in size. It was |
| 290 | returning everything in memory. When this was corrected, it wasn't |
| 291 | fixed for native platforms. */ |
| 292 | int |
| 293 | ppc_sysv_abi_broken_use_struct_convention (int gcc_p, struct type *value_type) |
| 294 | { |
| 295 | if (TYPE_LENGTH (value_type) == 16 |
| 296 | && TYPE_VECTOR (value_type)) |
| 297 | return 0; |
| 298 | |
| 299 | return generic_use_struct_convention (gcc_p, value_type); |
| 300 | } |
| 301 | |
| 302 | /* Structures 8 bytes or less long are returned in the r3 & r4 |
| 303 | registers, according to the SYSV ABI. */ |
| 304 | int |
| 305 | ppc_sysv_abi_use_struct_convention (int gcc_p, struct type *value_type) |
| 306 | { |
| 307 | if (TYPE_LENGTH (value_type) == 16 |
| 308 | && TYPE_VECTOR (value_type)) |
| 309 | return 0; |
| 310 | |
| 311 | return (TYPE_LENGTH (value_type) > 8); |
| 312 | } |