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