Commit | Line | Data |
---|---|---|
7b112f9c JT |
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" | |
bdf64bac | 28 | #include "gdb_string.h" |
8be9034a | 29 | #include "gdb_assert.h" |
7b112f9c JT |
30 | #include "ppc-tdep.h" |
31 | ||
7b112f9c JT |
32 | /* Pass the arguments in either registers, or in the stack. Using the |
33 | ppc sysv ABI, the first eight words of the argument list (that might | |
34 | be less than eight parameters if some parameters occupy more than one | |
35 | word) are passed in r3..r10 registers. float and double parameters are | |
36 | passed in fpr's, in addition to that. Rest of the parameters if any | |
37 | are passed in user stack. | |
38 | ||
39 | If the function is returning a structure, then the return address is passed | |
40 | in r3, then the first 7 words of the parametes can be passed in registers, | |
41 | starting from r4. */ | |
42 | ||
43 | CORE_ADDR | |
77b2b6d4 AC |
44 | ppc_sysv_abi_push_dummy_call (struct gdbarch *gdbarch, CORE_ADDR func_addr, |
45 | struct regcache *regcache, CORE_ADDR bp_addr, | |
46 | int nargs, struct value **args, CORE_ADDR sp, | |
47 | int struct_return, CORE_ADDR struct_addr) | |
7b112f9c | 48 | { |
0a613259 | 49 | struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch); |
68856ea3 AC |
50 | const CORE_ADDR saved_sp = read_sp (); |
51 | int argspace = 0; /* 0 is an initial wrong guess. */ | |
52 | int write_pass; | |
7b112f9c | 53 | |
68856ea3 | 54 | /* Go through the argument list twice. |
7b112f9c | 55 | |
68856ea3 AC |
56 | Pass 1: Figure out how much new stack space is required for |
57 | arguments and pushed values. Unlike the PowerOpen ABI, the SysV | |
58 | ABI doesn't reserve any extra space for parameters which are put | |
59 | in registers, but does always push structures and then pass their | |
60 | address. | |
7a41266b | 61 | |
68856ea3 AC |
62 | Pass 2: Replay the same computation but this time also write the |
63 | values out to the target. */ | |
7b112f9c | 64 | |
68856ea3 AC |
65 | for (write_pass = 0; write_pass < 2; write_pass++) |
66 | { | |
67 | int argno; | |
68 | /* Next available floating point register for float and double | |
69 | arguments. */ | |
70 | int freg = 1; | |
71 | /* Next available general register for non-float, non-vector | |
72 | arguments. */ | |
73 | int greg = 3; | |
74 | /* Next available vector register for vector arguments. */ | |
75 | int vreg = 2; | |
76 | /* Arguments start above the "LR save word" and "Back chain". */ | |
77 | int argoffset = 2 * tdep->wordsize; | |
78 | /* Structures start after the arguments. */ | |
79 | int structoffset = argoffset + argspace; | |
80 | ||
81 | /* If the function is returning a `struct', then the first word | |
944fcfab AC |
82 | (which will be passed in r3) is used for struct return |
83 | address. In that case we should advance one word and start | |
84 | from r4 register to copy parameters. */ | |
68856ea3 | 85 | if (struct_return) |
7b112f9c | 86 | { |
68856ea3 AC |
87 | if (write_pass) |
88 | regcache_cooked_write_signed (regcache, | |
89 | tdep->ppc_gp0_regnum + greg, | |
90 | struct_addr); | |
91 | greg++; | |
7b112f9c | 92 | } |
68856ea3 AC |
93 | |
94 | for (argno = 0; argno < nargs; argno++) | |
7b112f9c | 95 | { |
68856ea3 AC |
96 | struct value *arg = args[argno]; |
97 | struct type *type = check_typedef (VALUE_TYPE (arg)); | |
98 | int len = TYPE_LENGTH (type); | |
99 | char *val = VALUE_CONTENTS (arg); | |
100 | ||
101 | if (TYPE_CODE (type) == TYPE_CODE_FLT | |
944fcfab | 102 | && ppc_floating_point_unit_p (current_gdbarch) && len <= 8) |
7b112f9c | 103 | { |
68856ea3 | 104 | /* Floating point value converted to "double" then |
944fcfab AC |
105 | passed in an FP register, when the registers run out, |
106 | 8 byte aligned stack is used. */ | |
68856ea3 AC |
107 | if (freg <= 8) |
108 | { | |
109 | if (write_pass) | |
110 | { | |
111 | /* Always store the floating point value using | |
944fcfab | 112 | the register's floating-point format. */ |
68856ea3 AC |
113 | char regval[MAX_REGISTER_SIZE]; |
114 | struct type *regtype | |
115 | = register_type (gdbarch, FP0_REGNUM + freg); | |
116 | convert_typed_floating (val, type, regval, regtype); | |
117 | regcache_cooked_write (regcache, FP0_REGNUM + freg, | |
118 | regval); | |
119 | } | |
120 | freg++; | |
121 | } | |
7b112f9c JT |
122 | else |
123 | { | |
68856ea3 | 124 | /* SysV ABI converts floats to doubles before |
944fcfab | 125 | writing them to an 8 byte aligned stack location. */ |
68856ea3 AC |
126 | argoffset = align_up (argoffset, 8); |
127 | if (write_pass) | |
128 | { | |
129 | char memval[8]; | |
130 | struct type *memtype; | |
131 | switch (TARGET_BYTE_ORDER) | |
132 | { | |
133 | case BFD_ENDIAN_BIG: | |
134 | memtype = builtin_type_ieee_double_big; | |
135 | break; | |
136 | case BFD_ENDIAN_LITTLE: | |
137 | memtype = builtin_type_ieee_double_little; | |
138 | break; | |
139 | default: | |
140 | internal_error (__FILE__, __LINE__, "bad switch"); | |
141 | } | |
142 | convert_typed_floating (val, type, memval, memtype); | |
143 | write_memory (sp + argoffset, val, len); | |
144 | } | |
145 | argoffset += 8; | |
7b112f9c JT |
146 | } |
147 | } | |
944fcfab AC |
148 | else if (len == 8 && (TYPE_CODE (type) == TYPE_CODE_INT /* long long */ |
149 | || (!ppc_floating_point_unit_p (current_gdbarch) && TYPE_CODE (type) == TYPE_CODE_FLT))) /* double */ | |
7b112f9c | 150 | { |
68856ea3 | 151 | /* "long long" or "double" passed in an odd/even |
944fcfab AC |
152 | register pair with the low addressed word in the odd |
153 | register and the high addressed word in the even | |
154 | register, or when the registers run out an 8 byte | |
155 | aligned stack location. */ | |
68856ea3 AC |
156 | if (greg > 9) |
157 | { | |
158 | /* Just in case GREG was 10. */ | |
159 | greg = 11; | |
160 | argoffset = align_up (argoffset, 8); | |
161 | if (write_pass) | |
162 | write_memory (sp + argoffset, val, len); | |
163 | argoffset += 8; | |
164 | } | |
165 | else if (tdep->wordsize == 8) | |
166 | { | |
167 | if (write_pass) | |
168 | regcache_cooked_write (regcache, | |
944fcfab | 169 | tdep->ppc_gp0_regnum + greg, val); |
68856ea3 AC |
170 | greg += 1; |
171 | } | |
172 | else | |
173 | { | |
174 | /* Must start on an odd register - r3/r4 etc. */ | |
175 | if ((greg & 1) == 0) | |
176 | greg++; | |
177 | if (write_pass) | |
178 | { | |
179 | regcache_cooked_write (regcache, | |
180 | tdep->ppc_gp0_regnum + greg + 0, | |
181 | val + 0); | |
182 | regcache_cooked_write (regcache, | |
183 | tdep->ppc_gp0_regnum + greg + 1, | |
184 | val + 4); | |
185 | } | |
186 | greg += 2; | |
187 | } | |
7b112f9c | 188 | } |
68856ea3 AC |
189 | else if (len == 16 |
190 | && TYPE_CODE (type) == TYPE_CODE_ARRAY | |
944fcfab | 191 | && TYPE_VECTOR (type) && tdep->ppc_vr0_regnum >= 0) |
7b112f9c | 192 | { |
68856ea3 | 193 | /* Vector parameter passed in an Altivec register, or |
944fcfab | 194 | when that runs out, 16 byte aligned stack location. */ |
7b112f9c JT |
195 | if (vreg <= 13) |
196 | { | |
68856ea3 AC |
197 | if (write_pass) |
198 | regcache_cooked_write (current_regcache, | |
944fcfab | 199 | tdep->ppc_vr0_regnum + vreg, val); |
7b112f9c JT |
200 | vreg++; |
201 | } | |
202 | else | |
203 | { | |
68856ea3 AC |
204 | argoffset = align_up (argoffset, 16); |
205 | if (write_pass) | |
206 | write_memory (sp + argoffset, val, 16); | |
7b112f9c JT |
207 | argoffset += 16; |
208 | } | |
209 | } | |
944fcfab | 210 | else if (len == 8 |
0a613259 | 211 | && TYPE_CODE (type) == TYPE_CODE_ARRAY |
944fcfab AC |
212 | && TYPE_VECTOR (type) && tdep->ppc_ev0_regnum >= 0) |
213 | { | |
68856ea3 | 214 | /* Vector parameter passed in an e500 register, or when |
944fcfab AC |
215 | that runs out, 8 byte aligned stack location. Note |
216 | that since e500 vector and general purpose registers | |
217 | both map onto the same underlying register set, a | |
218 | "greg" and not a "vreg" is consumed here. A cooked | |
219 | write stores the value in the correct locations | |
220 | within the raw register cache. */ | |
221 | if (greg <= 10) | |
222 | { | |
68856ea3 AC |
223 | if (write_pass) |
224 | regcache_cooked_write (current_regcache, | |
944fcfab AC |
225 | tdep->ppc_ev0_regnum + greg, val); |
226 | greg++; | |
227 | } | |
228 | else | |
229 | { | |
68856ea3 AC |
230 | argoffset = align_up (argoffset, 8); |
231 | if (write_pass) | |
232 | write_memory (sp + argoffset, val, 8); | |
944fcfab AC |
233 | argoffset += 8; |
234 | } | |
235 | } | |
68856ea3 AC |
236 | else |
237 | { | |
238 | /* Reduce the parameter down to something that fits in a | |
944fcfab | 239 | "word". */ |
68856ea3 AC |
240 | char word[MAX_REGISTER_SIZE]; |
241 | memset (word, 0, MAX_REGISTER_SIZE); | |
242 | if (len > tdep->wordsize | |
243 | || TYPE_CODE (type) == TYPE_CODE_STRUCT | |
244 | || TYPE_CODE (type) == TYPE_CODE_UNION) | |
245 | { | |
246 | /* Structs and large values are put on an 8 byte | |
944fcfab | 247 | aligned stack ... */ |
68856ea3 AC |
248 | structoffset = align_up (structoffset, 8); |
249 | if (write_pass) | |
250 | write_memory (sp + structoffset, val, len); | |
251 | /* ... and then a "word" pointing to that address is | |
944fcfab | 252 | passed as the parameter. */ |
68856ea3 AC |
253 | store_unsigned_integer (word, tdep->wordsize, |
254 | sp + structoffset); | |
255 | structoffset += len; | |
256 | } | |
257 | else if (TYPE_CODE (type) == TYPE_CODE_INT) | |
258 | /* Sign or zero extend the "int" into a "word". */ | |
259 | store_unsigned_integer (word, tdep->wordsize, | |
260 | unpack_long (type, val)); | |
261 | else | |
262 | /* Always goes in the low address. */ | |
263 | memcpy (word, val, len); | |
264 | /* Store that "word" in a register, or on the stack. | |
944fcfab | 265 | The words have "4" byte alignment. */ |
68856ea3 AC |
266 | if (greg <= 10) |
267 | { | |
268 | if (write_pass) | |
269 | regcache_cooked_write (regcache, | |
944fcfab | 270 | tdep->ppc_gp0_regnum + greg, word); |
68856ea3 AC |
271 | greg++; |
272 | } | |
273 | else | |
274 | { | |
275 | argoffset = align_up (argoffset, tdep->wordsize); | |
276 | if (write_pass) | |
277 | write_memory (sp + argoffset, word, tdep->wordsize); | |
278 | argoffset += tdep->wordsize; | |
279 | } | |
280 | } | |
281 | } | |
282 | ||
283 | /* Compute the actual stack space requirements. */ | |
284 | if (!write_pass) | |
285 | { | |
286 | /* Remember the amount of space needed by the arguments. */ | |
287 | argspace = argoffset; | |
288 | /* Allocate space for both the arguments and the structures. */ | |
289 | sp -= (argoffset + structoffset); | |
290 | /* Ensure that the stack is still 16 byte aligned. */ | |
291 | sp = align_down (sp, 16); | |
292 | } | |
7b112f9c JT |
293 | } |
294 | ||
68856ea3 AC |
295 | /* Update %sp. */ |
296 | regcache_cooked_write_signed (regcache, SP_REGNUM, sp); | |
297 | ||
298 | /* Write the backchain (it occupies WORDSIZED bytes). */ | |
299 | write_memory_signed_integer (sp, tdep->wordsize, saved_sp); | |
300 | ||
e56a0ecc AC |
301 | /* Point the inferior function call's return address at the dummy's |
302 | breakpoint. */ | |
68856ea3 | 303 | regcache_cooked_write_signed (regcache, tdep->ppc_lr_regnum, bp_addr); |
e56a0ecc | 304 | |
7b112f9c JT |
305 | return sp; |
306 | } | |
307 | ||
7b112f9c JT |
308 | /* Structures 8 bytes or less long are returned in the r3 & r4 |
309 | registers, according to the SYSV ABI. */ | |
310 | int | |
311 | ppc_sysv_abi_use_struct_convention (int gcc_p, struct type *value_type) | |
312 | { | |
0a613259 | 313 | if ((TYPE_LENGTH (value_type) == 16 || TYPE_LENGTH (value_type) == 8) |
7b112f9c JT |
314 | && TYPE_VECTOR (value_type)) |
315 | return 0; | |
316 | ||
317 | return (TYPE_LENGTH (value_type) > 8); | |
944fcfab | 318 | } |
afd48b75 | 319 | |
8be9034a AC |
320 | /* Pass the arguments in either registers, or in the stack. Using the |
321 | ppc 64 bit SysV ABI. | |
322 | ||
323 | This implements a dumbed down version of the ABI. It always writes | |
324 | values to memory, GPR and FPR, even when not necessary. Doing this | |
325 | greatly simplifies the logic. */ | |
326 | ||
327 | CORE_ADDR | |
328 | ppc64_sysv_abi_push_dummy_call (struct gdbarch *gdbarch, CORE_ADDR func_addr, | |
329 | struct regcache *regcache, CORE_ADDR bp_addr, | |
330 | int nargs, struct value **args, CORE_ADDR sp, | |
331 | int struct_return, CORE_ADDR struct_addr) | |
332 | { | |
333 | struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch); | |
334 | /* By this stage in the proceedings, SP has been decremented by "red | |
335 | zone size" + "struct return size". Fetch the stack-pointer from | |
336 | before this and use that as the BACK_CHAIN. */ | |
337 | const CORE_ADDR back_chain = read_sp (); | |
338 | /* See for-loop comment below. */ | |
339 | int write_pass; | |
340 | /* Size of the Altivec's vector parameter region, the final value is | |
341 | computed in the for-loop below. */ | |
342 | LONGEST vparam_size = 0; | |
343 | /* Size of the general parameter region, the final value is computed | |
344 | in the for-loop below. */ | |
345 | LONGEST gparam_size = 0; | |
346 | /* Kevin writes ... I don't mind seeing tdep->wordsize used in the | |
347 | calls to align_up(), align_down(), etc. because this makes it | |
348 | easier to reuse this code (in a copy/paste sense) in the future, | |
349 | but it is a 64-bit ABI and asserting that the wordsize is 8 bytes | |
350 | at some point makes it easier to verify that this function is | |
351 | correct without having to do a non-local analysis to figure out | |
352 | the possible values of tdep->wordsize. */ | |
353 | gdb_assert (tdep->wordsize == 8); | |
354 | ||
355 | /* Go through the argument list twice. | |
356 | ||
357 | Pass 1: Compute the function call's stack space and register | |
358 | requirements. | |
359 | ||
360 | Pass 2: Replay the same computation but this time also write the | |
361 | values out to the target. */ | |
362 | ||
363 | for (write_pass = 0; write_pass < 2; write_pass++) | |
364 | { | |
365 | int argno; | |
366 | /* Next available floating point register for float and double | |
367 | arguments. */ | |
368 | int freg = 1; | |
369 | /* Next available general register for non-vector (but possibly | |
370 | float) arguments. */ | |
371 | int greg = 3; | |
372 | /* Next available vector register for vector arguments. */ | |
373 | int vreg = 2; | |
374 | /* The address, at which the next general purpose parameter | |
375 | (integer, struct, float, ...) should be saved. */ | |
376 | CORE_ADDR gparam; | |
377 | /* Address, at which the next Altivec vector parameter should be | |
378 | saved. */ | |
379 | CORE_ADDR vparam; | |
380 | ||
381 | if (!write_pass) | |
382 | { | |
383 | /* During the first pass, GPARAM and VPARAM are more like | |
384 | offsets (start address zero) than addresses. That way | |
385 | the accumulate the total stack space each region | |
386 | requires. */ | |
387 | gparam = 0; | |
388 | vparam = 0; | |
389 | } | |
390 | else | |
391 | { | |
392 | /* Decrement the stack pointer making space for the Altivec | |
393 | and general on-stack parameters. Set vparam and gparam | |
394 | to their corresponding regions. */ | |
395 | vparam = align_down (sp - vparam_size, 16); | |
396 | gparam = align_down (vparam - gparam_size, 16); | |
397 | /* Add in space for the TOC, link editor double word, | |
398 | compiler double word, LR save area, CR save area. */ | |
399 | sp = align_down (gparam - 48, 16); | |
400 | } | |
401 | ||
402 | /* If the function is returning a `struct', then there is an | |
403 | extra hidden parameter (which will be passed in r3) | |
404 | containing the address of that struct.. In that case we | |
405 | should advance one word and start from r4 register to copy | |
406 | parameters. This also consumes one on-stack parameter slot. */ | |
407 | if (struct_return) | |
408 | { | |
409 | if (write_pass) | |
410 | regcache_cooked_write_signed (regcache, | |
411 | tdep->ppc_gp0_regnum + greg, | |
412 | struct_addr); | |
413 | greg++; | |
414 | gparam = align_up (gparam + tdep->wordsize, tdep->wordsize); | |
415 | } | |
416 | ||
417 | for (argno = 0; argno < nargs; argno++) | |
418 | { | |
419 | struct value *arg = args[argno]; | |
420 | struct type *type = check_typedef (VALUE_TYPE (arg)); | |
421 | char *val = VALUE_CONTENTS (arg); | |
422 | if (TYPE_CODE (type) == TYPE_CODE_FLT && TYPE_LENGTH (type) <= 8) | |
423 | { | |
424 | /* Floats and Doubles go in f1 .. f13. They also | |
425 | consume a left aligned GREG,, and can end up in | |
426 | memory. */ | |
427 | if (write_pass) | |
428 | { | |
429 | if (ppc_floating_point_unit_p (current_gdbarch) | |
430 | && freg <= 13) | |
431 | { | |
432 | char regval[MAX_REGISTER_SIZE]; | |
433 | struct type *regtype = register_type (gdbarch, | |
434 | FP0_REGNUM); | |
435 | convert_typed_floating (val, type, regval, regtype); | |
436 | regcache_cooked_write (regcache, FP0_REGNUM + freg, | |
437 | regval); | |
438 | } | |
439 | if (greg <= 10) | |
440 | { | |
441 | /* The ABI states "Single precision floating | |
442 | point values are mapped to the first word in | |
443 | a single doubleword" and "... floating point | |
444 | values mapped to the first eight doublewords | |
445 | of the parameter save area are also passed in | |
446 | general registers"). | |
447 | ||
448 | This code interprets that to mean: store it, | |
449 | left aligned, in the general register. */ | |
450 | char regval[MAX_REGISTER_SIZE]; | |
451 | memset (regval, 0, sizeof regval); | |
452 | memcpy (regval, val, TYPE_LENGTH (type)); | |
453 | regcache_cooked_write (regcache, | |
454 | tdep->ppc_gp0_regnum + greg, | |
455 | regval); | |
456 | } | |
457 | write_memory (gparam, val, TYPE_LENGTH (type)); | |
458 | } | |
459 | /* Always consume parameter stack space. */ | |
460 | freg++; | |
461 | greg++; | |
462 | gparam = align_up (gparam + TYPE_LENGTH (type), tdep->wordsize); | |
463 | } | |
464 | else if (TYPE_LENGTH (type) == 16 && TYPE_VECTOR (type) | |
465 | && TYPE_CODE (type) == TYPE_CODE_ARRAY | |
466 | && tdep->ppc_vr0_regnum >= 0) | |
467 | { | |
468 | /* In the Altivec ABI, vectors go in the vector | |
469 | registers v2 .. v13, or when that runs out, a vector | |
470 | annex which goes above all the normal parameters. | |
471 | NOTE: cagney/2003-09-21: This is a guess based on the | |
472 | PowerOpen Altivec ABI. */ | |
473 | if (vreg <= 13) | |
474 | { | |
475 | if (write_pass) | |
476 | regcache_cooked_write (regcache, | |
477 | tdep->ppc_vr0_regnum + vreg, val); | |
478 | vreg++; | |
479 | } | |
480 | else | |
481 | { | |
482 | if (write_pass) | |
483 | write_memory (vparam, val, TYPE_LENGTH (type)); | |
484 | vparam = align_up (vparam + TYPE_LENGTH (type), 16); | |
485 | } | |
486 | } | |
487 | else if ((TYPE_CODE (type) == TYPE_CODE_INT | |
488 | || TYPE_CODE (type) == TYPE_CODE_ENUM) | |
489 | && TYPE_LENGTH (type) <= 8) | |
490 | { | |
491 | /* Scalars get sign[un]extended and go in gpr3 .. gpr10. | |
492 | They can also end up in memory. */ | |
493 | if (write_pass) | |
494 | { | |
495 | /* Sign extend the value, then store it unsigned. */ | |
496 | ULONGEST word = unpack_long (type, val); | |
497 | if (greg <= 10) | |
498 | regcache_cooked_write_unsigned (regcache, | |
499 | tdep->ppc_gp0_regnum + | |
500 | greg, word); | |
501 | write_memory_unsigned_integer (gparam, tdep->wordsize, | |
502 | word); | |
503 | } | |
504 | greg++; | |
505 | gparam = align_up (gparam + TYPE_LENGTH (type), tdep->wordsize); | |
506 | } | |
507 | else | |
508 | { | |
509 | int byte; | |
510 | for (byte = 0; byte < TYPE_LENGTH (type); | |
511 | byte += tdep->wordsize) | |
512 | { | |
513 | if (write_pass && greg <= 10) | |
514 | { | |
515 | char regval[MAX_REGISTER_SIZE]; | |
516 | int len = TYPE_LENGTH (type) - byte; | |
517 | if (len > tdep->wordsize) | |
518 | len = tdep->wordsize; | |
519 | memset (regval, 0, sizeof regval); | |
520 | /* WARNING: cagney/2003-09-21: As best I can | |
521 | tell, the ABI specifies that the value should | |
522 | be left aligned. Unfortunately, GCC doesn't | |
523 | do this - it instead right aligns even sized | |
524 | values and puts odd sized values on the | |
525 | stack. Work around that by putting both a | |
526 | left and right aligned value into the | |
527 | register (hopefully no one notices :-^). | |
528 | Arrrgh! */ | |
529 | /* Left aligned (8 byte values such as pointers | |
530 | fill the buffer). */ | |
531 | memcpy (regval, val + byte, len); | |
532 | /* Right aligned (but only if even). */ | |
533 | if (len == 1 || len == 2 || len == 4) | |
534 | memcpy (regval + tdep->wordsize - len, | |
535 | val + byte, len); | |
536 | regcache_cooked_write (regcache, greg, regval); | |
537 | } | |
538 | greg++; | |
539 | } | |
540 | if (write_pass) | |
541 | /* WARNING: cagney/2003-09-21: Strictly speaking, this | |
542 | isn't necessary, unfortunately, GCC appears to get | |
543 | "struct convention" parameter passing wrong putting | |
544 | odd sized structures in memory instead of in a | |
545 | register. Work around this by always writing the | |
546 | value to memory. Fortunately, doing this | |
547 | simplifies the code. */ | |
548 | write_memory (gparam, val, TYPE_LENGTH (type)); | |
549 | /* Always consume parameter stack space. */ | |
550 | gparam = align_up (gparam + TYPE_LENGTH (type), tdep->wordsize); | |
551 | } | |
552 | } | |
553 | ||
554 | if (!write_pass) | |
555 | { | |
556 | /* Save the true region sizes ready for the second pass. */ | |
557 | vparam_size = vparam; | |
558 | /* Make certain that the general parameter save area is at | |
559 | least the minimum 8 registers (or doublewords) in size. */ | |
560 | if (greg < 8) | |
561 | gparam_size = 8 * tdep->wordsize; | |
562 | else | |
563 | gparam_size = gparam; | |
564 | } | |
565 | } | |
566 | ||
567 | /* Update %sp. */ | |
568 | regcache_cooked_write_signed (regcache, SP_REGNUM, sp); | |
569 | ||
570 | /* Write the backchain (it occupies WORDSIZED bytes). */ | |
571 | write_memory_signed_integer (sp, tdep->wordsize, back_chain); | |
572 | ||
573 | /* Point the inferior function call's return address at the dummy's | |
574 | breakpoint. */ | |
575 | regcache_cooked_write_signed (regcache, tdep->ppc_lr_regnum, bp_addr); | |
576 | ||
577 | /* Find a value for the TOC register. Every symbol should have both | |
578 | ".FN" and "FN" in the minimal symbol table. "FN" points at the | |
579 | FN's descriptor, while ".FN" points at the entry point (which | |
580 | matches FUNC_ADDR). Need to reverse from FUNC_ADDR back to the | |
581 | FN's descriptor address. */ | |
582 | { | |
583 | /* Find the minimal symbol that corresponds to FUNC_ADDR (should | |
584 | have the name ".FN"). */ | |
585 | struct minimal_symbol *dot_fn = lookup_minimal_symbol_by_pc (func_addr); | |
586 | if (dot_fn != NULL && SYMBOL_LINKAGE_NAME (dot_fn)[0] == '.') | |
587 | { | |
588 | /* Now find the corresponding "FN" (dropping ".") minimal | |
589 | symbol's address. */ | |
590 | struct minimal_symbol *fn = | |
591 | lookup_minimal_symbol (SYMBOL_LINKAGE_NAME (dot_fn) + 1, NULL, | |
592 | NULL); | |
593 | if (fn != NULL) | |
594 | { | |
595 | /* Got the address of that descriptor. The TOC is the | |
596 | second double word. */ | |
597 | CORE_ADDR toc = | |
598 | read_memory_unsigned_integer (SYMBOL_VALUE_ADDRESS (fn) + | |
599 | tdep->wordsize, tdep->wordsize); | |
600 | regcache_cooked_write_unsigned (regcache, | |
601 | tdep->ppc_gp0_regnum + 2, toc); | |
602 | } | |
603 | } | |
604 | } | |
605 | ||
606 | return sp; | |
607 | } | |
608 | ||
afd48b75 AC |
609 | |
610 | /* The 64 bit ABI retun value convention. | |
611 | ||
612 | Return non-zero if the return-value is stored in a register, return | |
613 | 0 if the return-value is instead stored on the stack (a.k.a., | |
614 | struct return convention). | |
615 | ||
616 | For a return-value stored in a register: when INVAL is non-NULL, | |
617 | copy the buffer to the corresponding register return-value location | |
618 | location; when OUTVAL is non-NULL, fill the buffer from the | |
619 | corresponding register return-value location. */ | |
620 | ||
621 | /* Potential ways that a function can return a value of a given type. */ | |
622 | enum return_value_convention | |
623 | { | |
624 | /* Where the return value has been squeezed into one or more | |
625 | registers. */ | |
626 | RETURN_VALUE_REGISTER_CONVENTION, | |
627 | /* Commonly known as the "struct return convention". The caller | |
628 | passes an additional hidden first parameter to the caller. That | |
629 | parameter contains the address at which the value being returned | |
630 | should be stored. While typically, and historically, used for | |
631 | large structs, this is convention is applied to values of many | |
632 | different types. */ | |
633 | RETURN_VALUE_STRUCT_CONVENTION | |
634 | }; | |
635 | ||
636 | static enum return_value_convention | |
637 | ppc64_sysv_abi_return_value (struct type *valtype, struct regcache *regcache, | |
638 | const void *inval, void *outval) | |
639 | { | |
640 | struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch); | |
641 | /* Floats and doubles in F1. */ | |
944fcfab | 642 | if (TYPE_CODE (valtype) == TYPE_CODE_FLT && TYPE_LENGTH (valtype) <= 8) |
afd48b75 AC |
643 | { |
644 | char regval[MAX_REGISTER_SIZE]; | |
645 | struct type *regtype = register_type (current_gdbarch, FP0_REGNUM); | |
646 | if (inval != NULL) | |
647 | { | |
648 | convert_typed_floating (inval, valtype, regval, regtype); | |
649 | regcache_cooked_write (regcache, FP0_REGNUM + 1, regval); | |
650 | } | |
651 | if (outval != NULL) | |
652 | { | |
653 | regcache_cooked_read (regcache, FP0_REGNUM + 1, regval); | |
654 | convert_typed_floating (regval, regtype, outval, valtype); | |
655 | } | |
656 | return RETURN_VALUE_REGISTER_CONVENTION; | |
657 | } | |
944fcfab | 658 | if (TYPE_CODE (valtype) == TYPE_CODE_INT && TYPE_LENGTH (valtype) <= 8) |
afd48b75 AC |
659 | { |
660 | /* Integers in r3. */ | |
661 | if (inval != NULL) | |
662 | { | |
663 | /* Be careful to sign extend the value. */ | |
664 | regcache_cooked_write_unsigned (regcache, tdep->ppc_gp0_regnum + 3, | |
665 | unpack_long (valtype, inval)); | |
666 | } | |
667 | if (outval != NULL) | |
668 | { | |
669 | /* Extract the integer from r3. Since this is truncating the | |
670 | value, there isn't a sign extension problem. */ | |
671 | ULONGEST regval; | |
672 | regcache_cooked_read_unsigned (regcache, tdep->ppc_gp0_regnum + 3, | |
673 | ®val); | |
674 | store_unsigned_integer (outval, TYPE_LENGTH (valtype), regval); | |
675 | } | |
676 | return RETURN_VALUE_REGISTER_CONVENTION; | |
677 | } | |
678 | /* All pointers live in r3. */ | |
679 | if (TYPE_CODE (valtype) == TYPE_CODE_PTR) | |
680 | { | |
681 | /* All pointers live in r3. */ | |
682 | if (inval != NULL) | |
683 | regcache_cooked_write (regcache, tdep->ppc_gp0_regnum + 3, inval); | |
684 | if (outval != NULL) | |
685 | regcache_cooked_read (regcache, tdep->ppc_gp0_regnum + 3, outval); | |
686 | return RETURN_VALUE_REGISTER_CONVENTION; | |
687 | } | |
688 | if (TYPE_CODE (valtype) == TYPE_CODE_ARRAY | |
689 | && TYPE_LENGTH (valtype) <= 8 | |
690 | && TYPE_CODE (TYPE_TARGET_TYPE (valtype)) == TYPE_CODE_INT | |
691 | && TYPE_LENGTH (TYPE_TARGET_TYPE (valtype)) == 1) | |
692 | { | |
693 | /* Small character arrays are returned, right justified, in r3. */ | |
694 | int offset = (register_size (current_gdbarch, tdep->ppc_gp0_regnum + 3) | |
695 | - TYPE_LENGTH (valtype)); | |
696 | if (inval != NULL) | |
697 | regcache_cooked_write_part (regcache, tdep->ppc_gp0_regnum + 3, | |
698 | offset, TYPE_LENGTH (valtype), inval); | |
699 | if (outval != NULL) | |
700 | regcache_cooked_read_part (regcache, tdep->ppc_gp0_regnum + 3, | |
701 | offset, TYPE_LENGTH (valtype), outval); | |
702 | return RETURN_VALUE_REGISTER_CONVENTION; | |
703 | } | |
704 | /* Big floating point values get stored in adjacent floating | |
705 | point registers. */ | |
706 | if (TYPE_CODE (valtype) == TYPE_CODE_FLT | |
944fcfab | 707 | && (TYPE_LENGTH (valtype) == 16 || TYPE_LENGTH (valtype) == 32)) |
afd48b75 AC |
708 | { |
709 | if (inval || outval != NULL) | |
710 | { | |
711 | int i; | |
712 | for (i = 0; i < TYPE_LENGTH (valtype) / 8; i++) | |
713 | { | |
714 | if (inval != NULL) | |
715 | regcache_cooked_write (regcache, FP0_REGNUM + 1 + i, | |
716 | (const bfd_byte *) inval + i * 8); | |
717 | if (outval != NULL) | |
718 | regcache_cooked_read (regcache, FP0_REGNUM + 1 + i, | |
719 | (bfd_byte *) outval + i * 8); | |
720 | } | |
721 | } | |
722 | return RETURN_VALUE_REGISTER_CONVENTION; | |
723 | } | |
724 | /* Complex values get returned in f1:f2, need to convert. */ | |
725 | if (TYPE_CODE (valtype) == TYPE_CODE_COMPLEX | |
726 | && (TYPE_LENGTH (valtype) == 8 || TYPE_LENGTH (valtype) == 16)) | |
727 | { | |
728 | if (regcache != NULL) | |
729 | { | |
730 | int i; | |
731 | for (i = 0; i < 2; i++) | |
732 | { | |
733 | char regval[MAX_REGISTER_SIZE]; | |
944fcfab AC |
734 | struct type *regtype = |
735 | register_type (current_gdbarch, FP0_REGNUM); | |
afd48b75 AC |
736 | if (inval != NULL) |
737 | { | |
944fcfab AC |
738 | convert_typed_floating ((const bfd_byte *) inval + |
739 | i * (TYPE_LENGTH (valtype) / 2), | |
afd48b75 | 740 | valtype, regval, regtype); |
944fcfab AC |
741 | regcache_cooked_write (regcache, FP0_REGNUM + 1 + i, |
742 | regval); | |
afd48b75 AC |
743 | } |
744 | if (outval != NULL) | |
745 | { | |
746 | regcache_cooked_read (regcache, FP0_REGNUM + 1 + i, regval); | |
747 | convert_typed_floating (regval, regtype, | |
944fcfab AC |
748 | (bfd_byte *) outval + |
749 | i * (TYPE_LENGTH (valtype) / 2), | |
afd48b75 AC |
750 | valtype); |
751 | } | |
752 | } | |
753 | } | |
754 | return RETURN_VALUE_REGISTER_CONVENTION; | |
755 | } | |
756 | /* Big complex values get stored in f1:f4. */ | |
944fcfab | 757 | if (TYPE_CODE (valtype) == TYPE_CODE_COMPLEX && TYPE_LENGTH (valtype) == 32) |
afd48b75 AC |
758 | { |
759 | if (regcache != NULL) | |
760 | { | |
761 | int i; | |
762 | for (i = 0; i < 4; i++) | |
763 | { | |
764 | if (inval != NULL) | |
765 | regcache_cooked_write (regcache, FP0_REGNUM + 1 + i, | |
766 | (const bfd_byte *) inval + i * 8); | |
767 | if (outval != NULL) | |
768 | regcache_cooked_read (regcache, FP0_REGNUM + 1 + i, | |
769 | (bfd_byte *) outval + i * 8); | |
770 | } | |
771 | } | |
772 | return RETURN_VALUE_REGISTER_CONVENTION; | |
773 | } | |
774 | return RETURN_VALUE_STRUCT_CONVENTION; | |
775 | } | |
776 | ||
777 | int | |
778 | ppc64_sysv_abi_use_struct_convention (int gcc_p, struct type *value_type) | |
779 | { | |
780 | return (ppc64_sysv_abi_return_value (value_type, NULL, NULL, NULL) | |
781 | == RETURN_VALUE_STRUCT_CONVENTION); | |
782 | } | |
783 | ||
784 | void | |
785 | ppc64_sysv_abi_extract_return_value (struct type *valtype, | |
944fcfab | 786 | struct regcache *regbuf, void *valbuf) |
afd48b75 AC |
787 | { |
788 | if (ppc64_sysv_abi_return_value (valtype, regbuf, NULL, valbuf) | |
789 | != RETURN_VALUE_REGISTER_CONVENTION) | |
790 | error ("Function return value unknown"); | |
791 | } | |
792 | ||
793 | void | |
794 | ppc64_sysv_abi_store_return_value (struct type *valtype, | |
795 | struct regcache *regbuf, | |
796 | const void *valbuf) | |
797 | { | |
798 | if (!ppc64_sysv_abi_return_value (valtype, regbuf, valbuf, NULL)) | |
799 | error ("Function return value location unknown"); | |
800 | } |