Mostly merged in changes from IBM (Metin); see ChangeLog.
[binutils-gdb.git] / gdb / rs6000-tdep.c
1 /* Target-dependent code for GDB, the GNU debugger.
2 Copyright (C) 1986, 1987, 1989, 1991 Free Software Foundation, Inc.
3
4 This file is part of GDB.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
19
20 #include "defs.h"
21 #include "frame.h"
22 #include "inferior.h"
23 #include "symtab.h"
24 #include "target.h"
25
26 #include <sys/param.h>
27 #include <sys/dir.h>
28 #include <sys/user.h>
29 #include <signal.h>
30 #include <sys/ioctl.h>
31 #include <fcntl.h>
32
33 #include <sys/ptrace.h>
34 #include <sys/reg.h>
35
36 #include <a.out.h>
37 #include <sys/file.h>
38 #include <sys/stat.h>
39 #include <sys/core.h>
40
41 extern int errno;
42 extern int attach_flag;
43
44 /* Nonzero if we just simulated a single step break. */
45 int one_stepped;
46
47
48 /* Breakpoint shadows for the single step instructions will be kept here. */
49
50 static struct sstep_breaks {
51 int address;
52 int data;
53 } stepBreaks[2];
54
55
56 /*
57 * Calculate the destination of a branch/jump. Return -1 if not a branch.
58 */
59 static int
60 branch_dest (opcode, instr, pc, safety)
61 int opcode, instr, pc, safety;
62 {
63 register long offset;
64 unsigned dest;
65 int immediate;
66 int absolute;
67 int ext_op;
68
69 absolute = (int) ((instr >> 1) & 1);
70
71 switch (opcode) {
72 case 18 :
73 immediate = ((instr & ~3) << 6) >> 6; /* br unconditionl */
74
75 case 16 :
76 if (opcode != 18) /* br conditional */
77 immediate = ((instr & ~3) << 16) >> 16;
78 if (absolute)
79 dest = immediate;
80 else
81 dest = pc + immediate;
82 break;
83
84 case 19 :
85 ext_op = (instr>>1) & 0x3ff;
86
87 if (ext_op == 16) /* br conditional register */
88 dest = read_register (LR_REGNUM) & ~3;
89
90 else if (ext_op == 528) /* br cond to count reg */
91 dest = read_register (CTR_REGNUM) & ~3;
92
93 else return -1;
94 break;
95
96 default: return -1;
97 }
98 return (dest < TEXT_SEGMENT_BASE) ? safety : dest;
99 }
100
101
102
103 /* AIX does not support PT_STEP. Simulate it. */
104
105 int
106 single_step (signal)
107 int signal;
108 {
109 #define INSNLEN(OPCODE) 4
110
111 static char breakp[] = BREAKPOINT;
112 int ii, insn, ret, loc;
113 int breaks[2], opcode;
114
115 if (!one_stepped) {
116 extern CORE_ADDR text_start;
117 loc = read_pc ();
118
119 ret = read_memory (loc, &insn, sizeof (int));
120 if (ret)
121 printf ("Error in single_step()!!\n");
122
123 breaks[0] = loc + INSNLEN(insn);
124 opcode = insn >> 26;
125 breaks[1] = branch_dest (opcode, insn, loc, breaks[0]);
126
127 /* Don't put two breakpoints on the same address. */
128 if (breaks[1] == breaks[0])
129 breaks[1] = -1;
130
131 stepBreaks[1].address = -1;
132
133 for (ii=0; ii < 2; ++ii) {
134
135 /* ignore invalid breakpoint. */
136 if ( breaks[ii] == -1)
137 continue;
138
139 read_memory (breaks[ii], &(stepBreaks[ii].data), sizeof(int));
140
141 ret = write_memory (breaks[ii], breakp, sizeof(int));
142 stepBreaks[ii].address = breaks[ii];
143 }
144
145 one_stepped = 1;
146 ptrace (PT_CONTINUE, inferior_pid, 1, signal, 0);
147 }
148 else {
149
150 /* remove step breakpoints. */
151 for (ii=0; ii < 2; ++ii)
152 if (stepBreaks[ii].address != -1)
153 write_memory
154 (stepBreaks[ii].address, &(stepBreaks[ii].data), sizeof(int));
155
156 one_stepped = 0;
157 }
158 errno = 0;
159 return 1;
160 }
161
162
163 /* return pc value after skipping a function prologue. */
164
165 skip_prologue (pc)
166 int pc;
167 {
168 unsigned int tmp;
169 unsigned int op;
170
171 if (target_read_memory (pc, (char *)&op, sizeof (op)))
172 return pc; /* Can't access it -- assume no prologue. */
173 SWAP_TARGET_AND_HOST (&op, sizeof (op));
174
175 /* Assume that subsequent fetches can fail with low probability. */
176
177 if (op == 0x7c0802a6) { /* mflr r0 */
178 pc += 4;
179 op = read_memory_integer (pc, 4);
180 }
181
182 if ((op & 0xfc00003e) == 0x7c000026) { /* mfcr Rx */
183 pc += 4;
184 op = read_memory_integer (pc, 4);
185 }
186
187 if ((op & 0xfc000000) == 0x48000000) { /* bl foo, to save fprs??? */
188 pc += 4;
189 op = read_memory_integer (pc, 4);
190 }
191
192 if ((op & 0xfc1f0000) == 0xbc010000) { /* stm Rx, NUM(r1) */
193 pc += 4;
194 op = read_memory_integer (pc, 4);
195 }
196
197 while (((tmp = op >> 16) == 0x9001) || /* st r0, NUM(r1) */
198 (tmp == 0x9421) || /* stu r1, NUM(r1) */
199 (op == 0x93e1fffc)) /* st r31,-4(r1) */
200 {
201 pc += 4;
202 op = read_memory_integer (pc, 4);
203 }
204
205 while ((tmp = (op >> 22)) == 0x20f) { /* l r31, ... or */
206 pc += 4; /* l r30, ... */
207 op = read_memory_integer (pc, 4);
208 }
209
210 /* store parameters into stack */
211 while(
212 (op & 0xfc1f0000) == 0xd8010000 || /* stfd Rx,NUM(r1) */
213 (op & 0xfc1f0000) == 0x90010000 || /* st r?, NUM(r1) */
214 (op & 0xfc000000) == 0xfc000000 || /* frsp, fp?, .. */
215 (op & 0xd0000000) == 0xd0000000) /* stfs, fp?, .. */
216 {
217 pc += 4; /* store fpr double */
218 op = read_memory_integer (pc, 4);
219 }
220
221 if (op == 0x603f0000) { /* oril r31, r1, 0x0 */
222 pc += 4; /* this happens if r31 is used as */
223 op = read_memory_integer (pc, 4); /* frame ptr. (gcc does that) */
224
225 tmp = 0;
226 while ((op >> 16) == (0x907f + tmp)) { /* st r3, NUM(r31) */
227 pc += 4; /* st r4, NUM(r31), ... */
228 op = read_memory_integer (pc, 4);
229 tmp += 0x20;
230 }
231 }
232 #if 0
233 /* I have problems with skipping over __main() that I need to address
234 * sometime. Previously, I used to use misc_function_vector which
235 * didn't work as well as I wanted to be. -MGO */
236
237 /* If the first thing after skipping a prolog is a branch to a function,
238 this might be a call to an initializer in main(), introduced by gcc2.
239 We'd like to skip over it as well. Fortunately, xlc does some extra
240 work before calling a function right after a prologue, thus we can
241 single out such gcc2 behaviour. */
242
243
244 if ((op & 0xfc000001) == 0x48000001) { /* bl foo, an initializer function? */
245 op = read_memory_integer (pc+4, 4);
246
247 if (op == 0x4def7b82) { /* cror 0xf, 0xf, 0xf (nop) */
248
249 /* check and see if we are in main. If so, skip over this initializer
250 function as well. */
251
252 tmp = find_pc_misc_function (pc);
253 if (tmp >= 0 && !strcmp (misc_function_vector [tmp].name, "main"))
254 return pc + 8;
255 }
256 }
257 #endif /* 0 */
258
259 return pc;
260 }
261
262
263 /* text start and end addresses in virtual memory. */
264
265 CORE_ADDR text_start;
266 CORE_ADDR text_end;
267
268
269 /*************************************************************************
270 Support for creating pushind a dummy frame into the stack, and popping
271 frames, etc.
272 *************************************************************************/
273
274 /* The total size of dummy frame is 436, which is;
275
276 32 gpr's - 128 bytes
277 32 fpr's - 256 "
278 7 the rest - 28 "
279 and 24 extra bytes for the callee's link area. The last 24 bytes
280 for the link area might not be necessary, since it will be taken
281 care of by push_arguments(). */
282
283 #define DUMMY_FRAME_SIZE 436
284
285 #define DUMMY_FRAME_ADDR_SIZE 10
286
287 /* Make sure you initialize these in somewhere, in case gdb gives up what it
288 was debugging and starts debugging something else. FIXMEibm */
289
290 static int dummy_frame_count = 0;
291 static int dummy_frame_size = 0;
292 static CORE_ADDR *dummy_frame_addr = 0;
293
294 extern int stop_stack_dummy;
295
296 /* push a dummy frame into stack, save all register. Currently we are saving
297 only gpr's and fpr's, which is not good enough! FIXMEmgo */
298
299 push_dummy_frame ()
300 {
301 int sp, pc; /* stack pointer and link register */
302 int ii;
303
304 fetch_inferior_registers (-1);
305
306 if (dummy_frame_count >= dummy_frame_size) {
307 dummy_frame_size += DUMMY_FRAME_ADDR_SIZE;
308 if (dummy_frame_addr)
309 dummy_frame_addr = (CORE_ADDR*) xrealloc
310 (dummy_frame_addr, sizeof(CORE_ADDR) * (dummy_frame_size));
311 else
312 dummy_frame_addr = (CORE_ADDR*)
313 xmalloc (sizeof(CORE_ADDR) * (dummy_frame_size));
314 }
315
316 sp = read_register(SP_REGNUM);
317 pc = read_register(PC_REGNUM);
318
319 dummy_frame_addr [dummy_frame_count++] = sp;
320
321 /* Be careful! If the stack pointer is not decremented first, then kernel
322 thinks he is free to use the space underneath it. And kernel actually
323 uses that area for IPC purposes when executing ptrace(2) calls. So
324 before writing register values into the new frame, decrement and update
325 %sp first in order to secure your frame. */
326
327 write_register (SP_REGNUM, sp-DUMMY_FRAME_SIZE);
328
329 /* gdb relies on the state of current_frame. We'd better update it,
330 otherwise things like do_registers_info() wouldn't work properly! */
331
332 flush_cached_frames ();
333 set_current_frame (create_new_frame (sp-DUMMY_FRAME_SIZE, pc));
334
335 /* save program counter in link register's space. */
336 write_memory (sp+8, &pc, 4);
337
338 /* save all floating point and general purpose registers here. */
339
340 /* fpr's, f0..f31 */
341 for (ii = 0; ii < 32; ++ii)
342 write_memory (sp-8-(ii*8), &registers[REGISTER_BYTE (31-ii+FP0_REGNUM)], 8);
343
344 /* gpr's r0..r31 */
345 for (ii=1; ii <=32; ++ii)
346 write_memory (sp-256-(ii*4), &registers[REGISTER_BYTE (32-ii)], 4);
347
348 /* so far, 32*2 + 32 words = 384 bytes have been written.
349 7 extra registers in our register set: pc, ps, cnd, lr, cnt, xer, mq */
350
351 for (ii=1; ii <= (LAST_SP_REGNUM-FIRST_SP_REGNUM+1); ++ii) {
352 write_memory (sp-384-(ii*4),
353 &registers[REGISTER_BYTE (FPLAST_REGNUM + ii)], 4);
354 }
355
356 /* Save sp or so called back chain right here. */
357 write_memory (sp-DUMMY_FRAME_SIZE, &sp, 4);
358 sp -= DUMMY_FRAME_SIZE;
359
360 /* And finally, this is the back chain. */
361 write_memory (sp+8, &pc, 4);
362 }
363
364
365 /* Pop a dummy frame.
366
367 In rs6000 when we push a dummy frame, we save all of the registers. This
368 is usually done before user calls a function explicitly.
369
370 After a dummy frame is pushed, some instructions are copied into stack,
371 and stack pointer is decremented even more. Since we don't have a frame
372 pointer to get back to the parent frame of the dummy, we start having
373 trouble poping it. Therefore, we keep a dummy frame stack, keeping
374 addresses of dummy frames as such. When poping happens and when we
375 detect that was a dummy frame, we pop it back to its parent by using
376 dummy frame stack (`dummy_frame_addr' array).
377 */
378
379 pop_dummy_frame ()
380 {
381 CORE_ADDR sp, pc;
382 int ii;
383 sp = dummy_frame_addr [--dummy_frame_count];
384
385 /* restore all fpr's. */
386 for (ii = 1; ii <= 32; ++ii)
387 read_memory (sp-(ii*8), &registers[REGISTER_BYTE (32-ii+FP0_REGNUM)], 8);
388
389 /* restore all gpr's */
390 for (ii=1; ii <= 32; ++ii) {
391 read_memory (sp-256-(ii*4), &registers[REGISTER_BYTE (32-ii)], 4);
392 }
393
394 /* restore the rest of the registers. */
395 for (ii=1; ii <=(LAST_SP_REGNUM-FIRST_SP_REGNUM+1); ++ii)
396 read_memory (sp-384-(ii*4),
397 &registers[REGISTER_BYTE (FPLAST_REGNUM + ii)], 4);
398
399 read_memory (sp-(DUMMY_FRAME_SIZE-8),
400 &registers [REGISTER_BYTE(PC_REGNUM)], 4);
401
402 /* when a dummy frame was being pushed, we had to decrement %sp first, in
403 order to secure astack space. Thus, saved %sp (or %r1) value, is not the
404 one we should restore. Change it with the one we need. */
405
406 *(int*)&registers [REGISTER_BYTE(FP_REGNUM)] = sp;
407
408 /* Now we can restore all registers. */
409
410 store_inferior_registers (-1);
411 pc = read_pc ();
412 flush_cached_frames ();
413 set_current_frame (create_new_frame (sp, pc));
414 }
415
416
417 /* pop the innermost frame, go back to the caller. */
418
419 pop_frame ()
420 {
421 int pc, lr, sp, prev_sp; /* %pc, %lr, %sp */
422 struct aix_framedata fdata;
423 FRAME fr = get_current_frame ();
424 int addr, ii;
425
426 pc = read_pc ();
427 sp = FRAME_FP (fr);
428
429 if (stop_stack_dummy && dummy_frame_count) {
430 pop_dummy_frame ();
431 return;
432 }
433
434 /* figure out previous %pc value. If the function is frameless, it is
435 still in the link register, otherwise walk the frames and retrieve the
436 saved %pc value in the previous frame. */
437
438 addr = get_pc_function_start (fr->pc) + FUNCTION_START_OFFSET;
439 function_frame_info (addr, &fdata);
440
441 read_memory (sp, &prev_sp, 4);
442 if (fdata.frameless)
443 lr = read_register (LR_REGNUM);
444 else
445 read_memory (prev_sp+8, &lr, 4);
446
447 /* reset %pc value. */
448 write_register (PC_REGNUM, lr);
449
450 /* reset register values if any was saved earlier. */
451 addr = prev_sp - fdata.offset;
452
453 if (fdata.saved_gpr != -1)
454 for (ii=fdata.saved_gpr; ii <= 31; ++ii) {
455 read_memory (addr, &registers [REGISTER_BYTE (ii)], 4);
456 addr += sizeof (int);
457 }
458
459 if (fdata.saved_fpr != -1)
460 for (ii=fdata.saved_fpr; ii <= 31; ++ii) {
461 read_memory (addr, &registers [REGISTER_BYTE (ii+FP0_REGNUM)], 8);
462 addr += 8;
463 }
464
465 write_register (SP_REGNUM, prev_sp);
466 store_inferior_registers (-1);
467 flush_cached_frames ();
468 set_current_frame (create_new_frame (prev_sp, lr));
469 }
470
471
472 /* fixup the call sequence of a dummy function, with the real function address.
473 its argumets will be passed by gdb. */
474
475 fix_call_dummy(dummyname, pc, fun, nargs, type)
476 char *dummyname;
477 int pc;
478 int fun;
479 int nargs; /* not used */
480 int type; /* not used */
481
482 {
483 #define TOC_ADDR_OFFSET 20
484 #define TARGET_ADDR_OFFSET 28
485
486 int ii;
487 unsigned long target_addr;
488 unsigned long tocvalue;
489
490 target_addr = fun;
491 tocvalue = find_toc_address (target_addr);
492
493 ii = *(int*)((char*)dummyname + TOC_ADDR_OFFSET);
494 ii = (ii & 0xffff0000) | (tocvalue >> 16);
495 *(int*)((char*)dummyname + TOC_ADDR_OFFSET) = ii;
496
497 ii = *(int*)((char*)dummyname + TOC_ADDR_OFFSET+4);
498 ii = (ii & 0xffff0000) | (tocvalue & 0x0000ffff);
499 *(int*)((char*)dummyname + TOC_ADDR_OFFSET+4) = ii;
500
501 ii = *(int*)((char*)dummyname + TARGET_ADDR_OFFSET);
502 ii = (ii & 0xffff0000) | (target_addr >> 16);
503 *(int*)((char*)dummyname + TARGET_ADDR_OFFSET) = ii;
504
505 ii = *(int*)((char*)dummyname + TARGET_ADDR_OFFSET+4);
506 ii = (ii & 0xffff0000) | (target_addr & 0x0000ffff);
507 *(int*)((char*)dummyname + TARGET_ADDR_OFFSET+4) = ii;
508 }
509
510
511
512 /* return information about a function frame.
513 in struct aix_frameinfo fdata:
514 - frameless is TRUE, if function does not save %pc value in its frame.
515 - offset is the number of bytes used in the frame to save registers.
516 - saved_gpr is the number of the first saved gpr.
517 - saved_fpr is the number of the first saved fpr.
518 - alloca_reg is the number of the register used for alloca() handling.
519 Otherwise -1.
520 */
521 function_frame_info (pc, fdata)
522 int pc;
523 struct aix_framedata *fdata;
524 {
525 unsigned int tmp;
526 register unsigned int op;
527
528 fdata->offset = 0;
529 fdata->saved_gpr = fdata->saved_fpr = fdata->alloca_reg = -1;
530
531 op = read_memory_integer (pc, 4);
532 if (op == 0x7c0802a6) { /* mflr r0 */
533 pc += 4;
534 op = read_memory_integer (pc, 4);
535 fdata->frameless = 0;
536 }
537 else /* else, this is a frameless invocation */
538 fdata->frameless = 1;
539
540
541 if ((op & 0xfc00003e) == 0x7c000026) { /* mfcr Rx */
542 pc += 4;
543 op = read_memory_integer (pc, 4);
544 }
545
546 if ((op & 0xfc000000) == 0x48000000) { /* bl foo, to save fprs??? */
547 pc += 4;
548 op = read_memory_integer (pc, 4);
549 }
550
551 if ((op & 0xfc1f0000) == 0xd8010000) { /* stfd Rx,NUM(r1) */
552 pc += 4; /* store floating register double */
553 op = read_memory_integer (pc, 4);
554 }
555
556 if ((op & 0xfc1f0000) == 0xbc010000) { /* stm Rx, NUM(r1) */
557 int tmp2;
558 fdata->saved_gpr = (op >> 21) & 0x1f;
559 tmp2 = op & 0xffff;
560 if (tmp2 > 0x7fff)
561 tmp2 = 0xffff0000 | tmp2;
562
563 if (tmp2 < 0) {
564 tmp2 = tmp2 * -1;
565 fdata->saved_fpr = (tmp2 - ((32 - fdata->saved_gpr) * 4)) / 8;
566 if ( fdata->saved_fpr > 0)
567 fdata->saved_fpr = 32 - fdata->saved_fpr;
568 else
569 fdata->saved_fpr = -1;
570 }
571 fdata->offset = tmp2;
572 pc += 4;
573 op = read_memory_integer (pc, 4);
574 }
575
576 while (((tmp = op >> 16) == 0x9001) || /* st r0, NUM(r1) */
577 (tmp == 0x9421) || /* stu r1, NUM(r1) */
578 (op == 0x93e1fffc)) /* st r31,-4(r1) */
579 {
580 /* gcc takes a short cut and uses this instruction to save r31 only. */
581
582 if (op == 0x93e1fffc) {
583 if (fdata->offset)
584 /* fatal ("Unrecognized prolog."); */
585 printf ("Unrecognized prolog!\n");
586
587 fdata->saved_gpr = 31;
588 fdata->offset = 4;
589 }
590 pc += 4;
591 op = read_memory_integer (pc, 4);
592 }
593
594 while ((tmp = (op >> 22)) == 0x20f) { /* l r31, ... or */
595 pc += 4; /* l r30, ... */
596 op = read_memory_integer (pc, 4);
597 }
598
599 /* store parameters into stack */
600 while(
601 (op & 0xfc1f0000) == 0xd8010000 || /* stfd Rx,NUM(r1) */
602 (op & 0xfc1f0000) == 0x90010000 || /* st r?, NUM(r1) */
603 (op & 0xfc000000) == 0xfc000000 || /* frsp, fp?, .. */
604 (op & 0xd0000000) == 0xd0000000) /* stfs, fp?, .. */
605 {
606 pc += 4; /* store fpr double */
607 op = read_memory_integer (pc, 4);
608 }
609
610 if (op == 0x603f0000) /* oril r31, r1, 0x0 */
611 fdata->alloca_reg = 31;
612 }
613
614
615 /* Pass the arguments in either registers, or in the stack. In RS6000, the first
616 eight words of the argument list (that might be less than eight parameters if
617 some parameters occupy more than one word) are passed in r3..r11 registers.
618 float and double parameters are passed in fpr's, in addition to that. Rest of
619 the parameters if any are passed in user stack. There might be cases in which
620 half of the parameter is copied into registers, the other half is pushed into
621 stack.
622
623 If the function is returning a structure, then the return address is passed
624 in r3, then the first 7 words of the parametes can be passed in registers,
625 starting from r4. */
626
627 CORE_ADDR
628 push_arguments (nargs, args, sp, struct_return, struct_addr)
629 int nargs;
630 value *args;
631 CORE_ADDR sp;
632 int struct_return;
633 CORE_ADDR struct_addr;
634 {
635 int ii, len;
636 int argno; /* current argument number */
637 int argbytes; /* current argument byte */
638 char tmp_buffer [50];
639 value arg;
640 int f_argno = 0; /* current floating point argno */
641
642 CORE_ADDR saved_sp, pc;
643
644 if ( dummy_frame_count <= 0)
645 printf ("FATAL ERROR -push_arguments()! frame not found!!\n");
646
647 /* The first eight words of ther arguments are passed in registers. Copy
648 them appropriately.
649
650 If the function is returning a `struct', then the first word (which
651 will be passed in r3) is used for struct return address. In that
652 case we should advance one word and start from r4 register to copy
653 parameters. */
654
655 ii = struct_return ? 1 : 0;
656
657 for (argno=0, argbytes=0; argno < nargs && ii<8; ++ii) {
658
659 arg = value_arg_coerce (args[argno]);
660 len = TYPE_LENGTH (VALUE_TYPE (arg));
661
662 if (TYPE_CODE (VALUE_TYPE (arg)) == TYPE_CODE_FLT) {
663
664 /* floating point arguments are passed in fpr's, as well as gpr's.
665 There are 13 fpr's reserved for passing parameters. At this point
666 there is no way we would run out of them. */
667
668 if (len > 8)
669 printf (
670 "Fatal Error: a floating point parameter #%d with a size > 8 is found!\n", argno);
671
672 bcopy (VALUE_CONTENTS (arg),
673 &registers[REGISTER_BYTE(FP0_REGNUM + 1 + f_argno)], len);
674 ++f_argno;
675 }
676
677 if (len > 4) {
678
679 /* Argument takes more than one register. */
680 while (argbytes < len) {
681
682 *(int*)&registers[REGISTER_BYTE(ii+3)] = 0;
683 bcopy ( ((char*)VALUE_CONTENTS (arg))+argbytes,
684 &registers[REGISTER_BYTE(ii+3)],
685 (len - argbytes) > 4 ? 4 : len - argbytes);
686 ++ii, argbytes += 4;
687
688 if (ii >= 8)
689 goto ran_out_of_registers_for_arguments;
690 }
691 argbytes = 0;
692 --ii;
693 }
694 else { /* Argument can fit in one register. No problem. */
695 *(int*)&registers[REGISTER_BYTE(ii+3)] = 0;
696 bcopy (VALUE_CONTENTS (arg), &registers[REGISTER_BYTE(ii+3)], len);
697 }
698 ++argno;
699 }
700
701 ran_out_of_registers_for_arguments:
702
703 /* location for 8 parameters are always reserved. */
704 sp -= 4 * 8;
705
706 /* another six words for back chain, TOC register, link register, etc. */
707 sp -= 24;
708
709 /* if there are more arguments, allocate space for them in
710 the stack, then push them starting from the ninth one. */
711
712 if ((argno < nargs) || argbytes) {
713 int space = 0, jj;
714 value val;
715
716 if (argbytes) {
717 space += ((len - argbytes + 3) & -4);
718 jj = argno + 1;
719 }
720 else
721 jj = argno;
722
723 for (; jj < nargs; ++jj) {
724 val = value_arg_coerce (args[jj]);
725 space += ((TYPE_LENGTH (VALUE_TYPE (val))) + 3) & -4;
726 }
727
728 /* add location required for the rest of the parameters */
729 space = (space + 7) & -8;
730 sp -= space;
731
732 /* This is another instance we need to be concerned about securing our
733 stack space. If we write anything underneath %sp (r1), we might conflict
734 with the kernel who thinks he is free to use this area. So, update %sp
735 first before doing anything else. */
736
737 write_register (SP_REGNUM, sp);
738
739 /* if the last argument copied into the registers didn't fit there
740 completely, push the rest of it into stack. */
741
742 if (argbytes) {
743 write_memory (
744 sp+24+(ii*4), ((char*)VALUE_CONTENTS (arg))+argbytes, len - argbytes);
745 ++argno;
746 ii += ((len - argbytes + 3) & -4) / 4;
747 }
748
749 /* push the rest of the arguments into stack. */
750 for (; argno < nargs; ++argno) {
751
752 arg = value_arg_coerce (args[argno]);
753 len = TYPE_LENGTH (VALUE_TYPE (arg));
754
755
756 /* float types should be passed in fpr's, as well as in the stack. */
757 if (TYPE_CODE (VALUE_TYPE (arg)) == TYPE_CODE_FLT && f_argno < 13) {
758
759 if (len > 8)
760 printf (
761 "Fatal Error: a floating point parameter #%d with a size > 8 is found!\n", argno);
762
763 bcopy (VALUE_CONTENTS (arg),
764 &registers[REGISTER_BYTE(FP0_REGNUM + 1 + f_argno)], len);
765 ++f_argno;
766 }
767
768 write_memory (sp+24+(ii*4), VALUE_CONTENTS (arg), len);
769 ii += ((len + 3) & -4) / 4;
770 }
771 }
772 else
773 /* Secure stack areas first, before doing anything else. */
774 write_register (SP_REGNUM, sp);
775
776 saved_sp = dummy_frame_addr [dummy_frame_count - 1];
777 read_memory (saved_sp, tmp_buffer, 24);
778 write_memory (sp, tmp_buffer, 24);
779
780 write_memory (sp, &saved_sp, 4); /* set back chain properly */
781
782 store_inferior_registers (-1);
783 return sp;
784 }
785
786 /* a given return value in `regbuf' with a type `valtype', extract and copy its
787 value into `valbuf' */
788
789 extract_return_value (valtype, regbuf, valbuf)
790 struct type *valtype;
791 char regbuf[REGISTER_BYTES];
792 char *valbuf;
793 {
794
795 if (TYPE_CODE (valtype) == TYPE_CODE_FLT) {
796
797 double dd; float ff;
798 /* floats and doubles are returned in fpr1. fpr's have a size of 8 bytes.
799 We need to truncate the return value into float size (4 byte) if
800 necessary. */
801
802 if (TYPE_LENGTH (valtype) > 4) /* this is a double */
803 bcopy (&regbuf[REGISTER_BYTE (FP0_REGNUM + 1)], valbuf,
804 TYPE_LENGTH (valtype));
805 else { /* float */
806 bcopy (&regbuf[REGISTER_BYTE (FP0_REGNUM + 1)], &dd, 8);
807 ff = (float)dd;
808 bcopy (&ff, valbuf, sizeof(float));
809 }
810 }
811 else
812 /* return value is copied starting from r3. */
813 bcopy (&regbuf[REGISTER_BYTE (3)], valbuf, TYPE_LENGTH (valtype));
814 }
815
816
817 /* keep keep structure return address in this variable. */
818
819 CORE_ADDR rs6000_struct_return_address;
820
821
822 /* Throw away this debugging code. FIXMEmgo. */
823 print_frame(fram)
824 int fram;
825 {
826 int ii, val;
827 for (ii=0; ii<40; ++ii) {
828 if ((ii % 4) == 0)
829 printf ("\n");
830 val = read_memory_integer (fram + ii * 4, 4);
831 printf ("0x%08x\t", val);
832 }
833 printf ("\n");
834 }
835
836
837
838 /* Indirect function calls use a piece of trampoline code do co context switching,
839 i.e. to set the new TOC table. Skip such code if exists. */
840
841 skip_trampoline_code (pc)
842 int pc;
843 {
844 register unsigned int ii, op;
845
846 static unsigned trampoline_code[] = {
847 0x800b0000, /* l r0,0x0(r11) */
848 0x90410014, /* st r2,0x14(r1) */
849 0x7c0903a6, /* mtctr r0 */
850 0x804b0004, /* l r2,0x4(r11) */
851 0x816b0008, /* l r11,0x8(r11) */
852 0x4e800420, /* bctr */
853 0x4e800020, /* br */
854 0
855 };
856
857 for (ii=0; trampoline_code[ii]; ++ii) {
858 op = read_memory_integer (pc + (ii*4), 4);
859 if (op != trampoline_code [ii])
860 return NULL;
861 }
862 ii = read_register (11); /* r11 holds destination addr */
863 pc = read_memory_integer (ii, 4); /* (r11) value */
864 return pc;
865 }
866