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