This commit was generated by cvs2svn to track changes on a CVS vendor
[external/binutils.git] / gdb / rs6000-tdep.c
1 /* Target-dependent code for GDB, the GNU debugger.
2    Copyright 1986, 1987, 1989, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 2000
3    Free Software Foundation, Inc.
4
5    This file is part of GDB.
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 59 Temple Place - Suite 330,
20    Boston, MA 02111-1307, USA.  */
21
22 #include "defs.h"
23 #include "frame.h"
24 #include "inferior.h"
25 #include "symtab.h"
26 #include "target.h"
27 #include "gdbcore.h"
28 #include "gdbcmd.h"
29 #include "symfile.h"
30 #include "objfiles.h"
31 #include "xcoffsolib.h"
32 #include "arch-utils.h"
33
34 #include "bfd/libbfd.h"         /* for bfd_default_set_arch_mach */
35 #include "coff/internal.h"      /* for libcoff.h */
36 #include "bfd/libcoff.h"        /* for xcoff_data */
37
38 /* Some important register numbers.  Keep these in the same order as in
39    /usr/mstsave.h `mstsave' structure, for easier processing. */
40
41 #define GP0_REGNUM 0            /* GPR register 0 */
42 #define TOC_REGNUM 2            /* TOC register */
43 #define PS_REGNUM 65            /* Processor (or machine) status (%msr) */
44 #define CR_REGNUM 66            /* Condition register */
45 #define LR_REGNUM 67            /* Link register */
46 #define CTR_REGNUM 68           /* Count register */
47
48 /* If the kernel has to deliver a signal, it pushes a sigcontext
49    structure on the stack and then calls the signal handler, passing
50    the address of the sigcontext in an argument register. Usually
51    the signal handler doesn't save this register, so we have to
52    access the sigcontext structure via an offset from the signal handler
53    frame.
54    The following constants were determined by experimentation on AIX 3.2.  */
55 #define SIG_FRAME_PC_OFFSET 96
56 #define SIG_FRAME_LR_OFFSET 108
57 #define SIG_FRAME_FP_OFFSET 284
58
59 /* Default offset from SP where the LR is stored */
60 #define DEFAULT_LR_SAVE 8
61
62 /* To be used by skip_prologue. */
63
64 struct rs6000_framedata
65   {
66     int offset;                 /* total size of frame --- the distance
67                                    by which we decrement sp to allocate
68                                    the frame */
69     int saved_gpr;              /* smallest # of saved gpr */
70     int saved_fpr;              /* smallest # of saved fpr */
71     int alloca_reg;             /* alloca register number (frame ptr) */
72     char frameless;             /* true if frameless functions. */
73     char nosavedpc;             /* true if pc not saved. */
74     int gpr_offset;             /* offset of saved gprs from prev sp */
75     int fpr_offset;             /* offset of saved fprs from prev sp */
76     int lr_offset;              /* offset of saved lr */
77     int cr_offset;              /* offset of saved cr */
78   };
79
80 /* Description of a single register. */
81
82 struct reg
83   {
84     char *name;                 /* name of register */
85     unsigned char sz32;         /* size on 32-bit arch, 0 if nonextant */
86     unsigned char sz64;         /* size on 64-bit arch, 0 if nonextant */
87     unsigned char fpr;          /* whether register is floating-point */
88   };
89
90 /* Private data that this module attaches to struct gdbarch. */
91
92 struct gdbarch_tdep
93   {
94     int wordsize;               /* size in bytes of fixed-point word */
95     int *regoff;                /* byte offsets in register arrays */
96     const struct reg *regs;     /* from current variant */
97   };
98
99 /* Return the current architecture's gdbarch_tdep structure. */
100
101 #define TDEP    gdbarch_tdep (current_gdbarch)
102
103 /* Breakpoint shadows for the single step instructions will be kept here. */
104
105 static struct sstep_breaks
106   {
107     /* Address, or 0 if this is not in use.  */
108     CORE_ADDR address;
109     /* Shadow contents.  */
110     char data[4];
111   }
112 stepBreaks[2];
113
114 /* Hook for determining the TOC address when calling functions in the
115    inferior under AIX. The initialization code in rs6000-nat.c sets
116    this hook to point to find_toc_address.  */
117
118 CORE_ADDR (*rs6000_find_toc_address_hook) (CORE_ADDR) = NULL;
119
120 /* Hook to set the current architecture when starting a child process. 
121    rs6000-nat.c sets this. */
122
123 void (*rs6000_set_host_arch_hook) (int) = NULL;
124
125 /* Static function prototypes */
126
127 static CORE_ADDR branch_dest (int opcode, int instr, CORE_ADDR pc,
128                               CORE_ADDR safety);
129 static CORE_ADDR skip_prologue (CORE_ADDR, struct rs6000_framedata *);
130 static void frame_get_saved_regs (struct frame_info * fi,
131                                   struct rs6000_framedata * fdatap);
132 static CORE_ADDR frame_initial_stack_address (struct frame_info *);
133
134 /* Read a LEN-byte address from debugged memory address MEMADDR. */
135
136 static CORE_ADDR
137 read_memory_addr (CORE_ADDR memaddr, int len)
138 {
139   return read_memory_unsigned_integer (memaddr, len);
140 }
141
142 static CORE_ADDR
143 rs6000_skip_prologue (CORE_ADDR pc)
144 {
145   struct rs6000_framedata frame;
146   pc = skip_prologue (pc, &frame);
147   return pc;
148 }
149
150
151 /* Fill in fi->saved_regs */
152
153 struct frame_extra_info
154 {
155   /* Functions calling alloca() change the value of the stack
156      pointer. We need to use initial stack pointer (which is saved in
157      r31 by gcc) in such cases. If a compiler emits traceback table,
158      then we should use the alloca register specified in traceback
159      table. FIXME. */
160   CORE_ADDR initial_sp;         /* initial stack pointer. */
161 };
162
163 static void
164 rs6000_init_extra_frame_info (int fromleaf, struct frame_info *fi)
165 {
166   fi->extra_info = (struct frame_extra_info *)
167     frame_obstack_alloc (sizeof (struct frame_extra_info));
168   fi->extra_info->initial_sp = 0;
169   if (fi->next != (CORE_ADDR) 0
170       && fi->pc < TEXT_SEGMENT_BASE)
171     /* We're in get_prev_frame */
172     /* and this is a special signal frame.  */
173     /* (fi->pc will be some low address in the kernel, */
174     /*  to which the signal handler returns).  */
175     fi->signal_handler_caller = 1;
176 }
177
178 /* Put here the code to store, into a struct frame_saved_regs,
179    the addresses of the saved registers of frame described by FRAME_INFO.
180    This includes special registers such as pc and fp saved in special
181    ways in the stack frame.  sp is even more special:
182    the address we return for it IS the sp for the next frame.  */
183
184 /* In this implementation for RS/6000, we do *not* save sp. I am
185    not sure if it will be needed. The following function takes care of gpr's
186    and fpr's only. */
187
188 static void
189 rs6000_frame_init_saved_regs (struct frame_info *fi)
190 {
191   frame_get_saved_regs (fi, NULL);
192 }
193
194 static CORE_ADDR
195 rs6000_frame_args_address (struct frame_info *fi)
196 {
197   if (fi->extra_info->initial_sp != 0)
198     return fi->extra_info->initial_sp;
199   else
200     return frame_initial_stack_address (fi);
201 }
202
203 /* Immediately after a function call, return the saved pc.
204    Can't go through the frames for this because on some machines
205    the new frame is not set up until the new function executes
206    some instructions.  */
207
208 static CORE_ADDR
209 rs6000_saved_pc_after_call (struct frame_info *fi)
210 {
211   return read_register (LR_REGNUM);
212 }
213
214 /* Calculate the destination of a branch/jump.  Return -1 if not a branch.  */
215
216 static CORE_ADDR
217 branch_dest (int opcode, int instr, CORE_ADDR pc, CORE_ADDR safety)
218 {
219   CORE_ADDR dest;
220   int immediate;
221   int absolute;
222   int ext_op;
223
224   absolute = (int) ((instr >> 1) & 1);
225
226   switch (opcode)
227     {
228     case 18:
229       immediate = ((instr & ~3) << 6) >> 6;     /* br unconditional */
230       if (absolute)
231         dest = immediate;
232       else
233         dest = pc + immediate;
234       break;
235
236     case 16:
237       immediate = ((instr & ~3) << 16) >> 16;   /* br conditional */
238       if (absolute)
239         dest = immediate;
240       else
241         dest = pc + immediate;
242       break;
243
244     case 19:
245       ext_op = (instr >> 1) & 0x3ff;
246
247       if (ext_op == 16)         /* br conditional register */
248         {
249           dest = read_register (LR_REGNUM) & ~3;
250
251           /* If we are about to return from a signal handler, dest is
252              something like 0x3c90.  The current frame is a signal handler
253              caller frame, upon completion of the sigreturn system call
254              execution will return to the saved PC in the frame.  */
255           if (dest < TEXT_SEGMENT_BASE)
256             {
257               struct frame_info *fi;
258
259               fi = get_current_frame ();
260               if (fi != NULL)
261                 dest = read_memory_addr (fi->frame + SIG_FRAME_PC_OFFSET,
262                                          TDEP->wordsize);
263             }
264         }
265
266       else if (ext_op == 528)   /* br cond to count reg */
267         {
268           dest = read_register (CTR_REGNUM) & ~3;
269
270           /* If we are about to execute a system call, dest is something
271              like 0x22fc or 0x3b00.  Upon completion the system call
272              will return to the address in the link register.  */
273           if (dest < TEXT_SEGMENT_BASE)
274             dest = read_register (LR_REGNUM) & ~3;
275         }
276       else
277         return -1;
278       break;
279
280     default:
281       return -1;
282     }
283   return (dest < TEXT_SEGMENT_BASE) ? safety : dest;
284 }
285
286
287 /* Sequence of bytes for breakpoint instruction.  */
288
289 #define BIG_BREAKPOINT { 0x7d, 0x82, 0x10, 0x08 }
290 #define LITTLE_BREAKPOINT { 0x08, 0x10, 0x82, 0x7d }
291
292 static unsigned char *
293 rs6000_breakpoint_from_pc (CORE_ADDR *bp_addr, int *bp_size)
294 {
295   static unsigned char big_breakpoint[] = BIG_BREAKPOINT;
296   static unsigned char little_breakpoint[] = LITTLE_BREAKPOINT;
297   *bp_size = 4;
298   if (TARGET_BYTE_ORDER == BIG_ENDIAN)
299     return big_breakpoint;
300   else
301     return little_breakpoint;
302 }
303
304
305 /* AIX does not support PT_STEP. Simulate it. */
306
307 void
308 rs6000_software_single_step (unsigned int signal, int insert_breakpoints_p)
309 {
310 #define INSNLEN(OPCODE)  4
311
312   static char le_breakp[] = LITTLE_BREAKPOINT;
313   static char be_breakp[] = BIG_BREAKPOINT;
314   char *breakp = TARGET_BYTE_ORDER == BIG_ENDIAN ? be_breakp : le_breakp;
315   int ii, insn;
316   CORE_ADDR loc;
317   CORE_ADDR breaks[2];
318   int opcode;
319
320   if (insert_breakpoints_p)
321     {
322
323       loc = read_pc ();
324
325       insn = read_memory_integer (loc, 4);
326
327       breaks[0] = loc + INSNLEN (insn);
328       opcode = insn >> 26;
329       breaks[1] = branch_dest (opcode, insn, loc, breaks[0]);
330
331       /* Don't put two breakpoints on the same address. */
332       if (breaks[1] == breaks[0])
333         breaks[1] = -1;
334
335       stepBreaks[1].address = 0;
336
337       for (ii = 0; ii < 2; ++ii)
338         {
339
340           /* ignore invalid breakpoint. */
341           if (breaks[ii] == -1)
342             continue;
343
344           read_memory (breaks[ii], stepBreaks[ii].data, 4);
345
346           write_memory (breaks[ii], breakp, 4);
347           stepBreaks[ii].address = breaks[ii];
348         }
349
350     }
351   else
352     {
353
354       /* remove step breakpoints. */
355       for (ii = 0; ii < 2; ++ii)
356         if (stepBreaks[ii].address != 0)
357           write_memory
358             (stepBreaks[ii].address, stepBreaks[ii].data, 4);
359
360     }
361   errno = 0;                    /* FIXME, don't ignore errors! */
362   /* What errors?  {read,write}_memory call error().  */
363 }
364
365
366 /* return pc value after skipping a function prologue and also return
367    information about a function frame.
368
369    in struct rs6000_framedata fdata:
370    - frameless is TRUE, if function does not have a frame.
371    - nosavedpc is TRUE, if function does not save %pc value in its frame.
372    - offset is the initial size of this stack frame --- the amount by
373    which we decrement the sp to allocate the frame.
374    - saved_gpr is the number of the first saved gpr.
375    - saved_fpr is the number of the first saved fpr.
376    - alloca_reg is the number of the register used for alloca() handling.
377    Otherwise -1.
378    - gpr_offset is the offset of the first saved gpr from the previous frame.
379    - fpr_offset is the offset of the first saved fpr from the previous frame.
380    - lr_offset is the offset of the saved lr
381    - cr_offset is the offset of the saved cr
382  */
383
384 #define SIGNED_SHORT(x)                                                 \
385   ((sizeof (short) == 2)                                                \
386    ? ((int)(short)(x))                                                  \
387    : ((int)((((x) & 0xffff) ^ 0x8000) - 0x8000)))
388
389 #define GET_SRC_REG(x) (((x) >> 21) & 0x1f)
390
391 static CORE_ADDR
392 skip_prologue (CORE_ADDR pc, struct rs6000_framedata *fdata)
393 {
394   CORE_ADDR orig_pc = pc;
395   CORE_ADDR last_prologue_pc;
396   char buf[4];
397   unsigned long op;
398   long offset = 0;
399   int lr_reg = -1;
400   int cr_reg = -1;
401   int reg;
402   int framep = 0;
403   int minimal_toc_loaded = 0;
404   int prev_insn_was_prologue_insn = 1;
405
406   memset (fdata, 0, sizeof (struct rs6000_framedata));
407   fdata->saved_gpr = -1;
408   fdata->saved_fpr = -1;
409   fdata->alloca_reg = -1;
410   fdata->frameless = 1;
411   fdata->nosavedpc = 1;
412
413   pc -= 4;
414   for (;;)
415     {
416       pc += 4;
417
418       /* Sometimes it isn't clear if an instruction is a prologue
419          instruction or not.  When we encounter one of these ambiguous
420          cases, we'll set prev_insn_was_prologue_insn to 0 (false).
421          Otherwise, we'll assume that it really is a prologue instruction. */
422       if (prev_insn_was_prologue_insn)
423         last_prologue_pc = pc;
424       prev_insn_was_prologue_insn = 1;
425
426       if (target_read_memory (pc, buf, 4))
427         break;
428       op = extract_signed_integer (buf, 4);
429
430       if ((op & 0xfc1fffff) == 0x7c0802a6)
431         {                       /* mflr Rx */
432           lr_reg = (op & 0x03e00000) | 0x90010000;
433           continue;
434
435         }
436       else if ((op & 0xfc1fffff) == 0x7c000026)
437         {                       /* mfcr Rx */
438           cr_reg = (op & 0x03e00000) | 0x90010000;
439           continue;
440
441         }
442       else if ((op & 0xfc1f0000) == 0xd8010000)
443         {                       /* stfd Rx,NUM(r1) */
444           reg = GET_SRC_REG (op);
445           if (fdata->saved_fpr == -1 || fdata->saved_fpr > reg)
446             {
447               fdata->saved_fpr = reg;
448               fdata->fpr_offset = SIGNED_SHORT (op) + offset;
449             }
450           continue;
451
452         }
453       else if (((op & 0xfc1f0000) == 0xbc010000) ||     /* stm Rx, NUM(r1) */
454                (((op & 0xfc1f0000) == 0x90010000 ||     /* st rx,NUM(r1) */
455                  (op & 0xfc1f0003) == 0xf8010000) &&    /* std rx,NUM(r1) */
456                 (op & 0x03e00000) >= 0x01a00000))       /* rx >= r13 */
457         {
458
459           reg = GET_SRC_REG (op);
460           if (fdata->saved_gpr == -1 || fdata->saved_gpr > reg)
461             {
462               fdata->saved_gpr = reg;
463               if ((op & 0xfc1f0003) == 0xf8010000)
464                 op = (op >> 1) << 1;
465               fdata->gpr_offset = SIGNED_SHORT (op) + offset;
466             }
467           continue;
468
469         }
470       else if ((op & 0xffff0000) == 0x60000000)
471         {
472                                 /* nop */
473           /* Allow nops in the prologue, but do not consider them to
474              be part of the prologue unless followed by other prologue
475              instructions. */
476           prev_insn_was_prologue_insn = 0;
477           continue;
478
479         }
480       else if ((op & 0xffff0000) == 0x3c000000)
481         {                       /* addis 0,0,NUM, used
482                                    for >= 32k frames */
483           fdata->offset = (op & 0x0000ffff) << 16;
484           fdata->frameless = 0;
485           continue;
486
487         }
488       else if ((op & 0xffff0000) == 0x60000000)
489         {                       /* ori 0,0,NUM, 2nd ha
490                                    lf of >= 32k frames */
491           fdata->offset |= (op & 0x0000ffff);
492           fdata->frameless = 0;
493           continue;
494
495         }
496       else if (lr_reg != -1 && (op & 0xffff0000) == lr_reg)
497         {                       /* st Rx,NUM(r1) 
498                                    where Rx == lr */
499           fdata->lr_offset = SIGNED_SHORT (op) + offset;
500           fdata->nosavedpc = 0;
501           lr_reg = 0;
502           continue;
503
504         }
505       else if (cr_reg != -1 && (op & 0xffff0000) == cr_reg)
506         {                       /* st Rx,NUM(r1) 
507                                    where Rx == cr */
508           fdata->cr_offset = SIGNED_SHORT (op) + offset;
509           cr_reg = 0;
510           continue;
511
512         }
513       else if (op == 0x48000005)
514         {                       /* bl .+4 used in 
515                                    -mrelocatable */
516           continue;
517
518         }
519       else if (op == 0x48000004)
520         {                       /* b .+4 (xlc) */
521           break;
522
523         }
524       else if (((op & 0xffff0000) == 0x801e0000 ||      /* lwz 0,NUM(r30), used
525                                                            in V.4 -mrelocatable */
526                 op == 0x7fc0f214) &&    /* add r30,r0,r30, used
527                                            in V.4 -mrelocatable */
528                lr_reg == 0x901e0000)
529         {
530           continue;
531
532         }
533       else if ((op & 0xffff0000) == 0x3fc00000 ||       /* addis 30,0,foo@ha, used
534                                                            in V.4 -mminimal-toc */
535                (op & 0xffff0000) == 0x3bde0000)
536         {                       /* addi 30,30,foo@l */
537           continue;
538
539         }
540       else if ((op & 0xfc000001) == 0x48000001)
541         {                       /* bl foo, 
542                                    to save fprs??? */
543
544           fdata->frameless = 0;
545           /* Don't skip over the subroutine call if it is not within the first
546              three instructions of the prologue.  */
547           if ((pc - orig_pc) > 8)
548             break;
549
550           op = read_memory_integer (pc + 4, 4);
551
552           /* At this point, make sure this is not a trampoline function
553              (a function that simply calls another functions, and nothing else).
554              If the next is not a nop, this branch was part of the function
555              prologue. */
556
557           if (op == 0x4def7b82 || op == 0)      /* crorc 15, 15, 15 */
558             break;              /* don't skip over 
559                                    this branch */
560           continue;
561
562           /* update stack pointer */
563         }
564       else if ((op & 0xffff0000) == 0x94210000 ||       /* stu r1,NUM(r1) */
565                (op & 0xffff0003) == 0xf8210001)         /* stdu r1,NUM(r1) */
566         {
567           fdata->frameless = 0;
568           if ((op & 0xffff0003) == 0xf8210001)
569             op = (op >> 1) << 1;
570           fdata->offset = SIGNED_SHORT (op);
571           offset = fdata->offset;
572           continue;
573
574         }
575       else if (op == 0x7c21016e)
576         {                       /* stwux 1,1,0 */
577           fdata->frameless = 0;
578           offset = fdata->offset;
579           continue;
580
581           /* Load up minimal toc pointer */
582         }
583       else if ((op >> 22) == 0x20f
584                && !minimal_toc_loaded)
585         {                       /* l r31,... or l r30,... */
586           minimal_toc_loaded = 1;
587           continue;
588
589           /* move parameters from argument registers to local variable
590              registers */
591         }
592       else if ((op & 0xfc0007fe) == 0x7c000378 &&       /* mr(.)  Rx,Ry */
593                (((op >> 21) & 31) >= 3) &&              /* R3 >= Ry >= R10 */
594                (((op >> 21) & 31) <= 10) &&
595                (((op >> 16) & 31) >= fdata->saved_gpr)) /* Rx: local var reg */
596         {
597           continue;
598
599           /* store parameters in stack */
600         }
601       else if ((op & 0xfc1f0000) == 0x90010000 ||       /* st rx,NUM(r1) */
602                (op & 0xfc1f0003) == 0xf8010000 ||       /* std rx,NUM(r1) */
603                (op & 0xfc1f0000) == 0xd8010000 ||       /* stfd Rx,NUM(r1) */
604                (op & 0xfc1f0000) == 0xfc010000)         /* frsp, fp?,NUM(r1) */
605         {
606           continue;
607
608           /* store parameters in stack via frame pointer */
609         }
610       else if (framep &&
611                ((op & 0xfc1f0000) == 0x901f0000 ||      /* st rx,NUM(r1) */
612                 (op & 0xfc1f0000) == 0xd81f0000 ||      /* stfd Rx,NUM(r1) */
613                 (op & 0xfc1f0000) == 0xfc1f0000))
614         {                       /* frsp, fp?,NUM(r1) */
615           continue;
616
617           /* Set up frame pointer */
618         }
619       else if (op == 0x603f0000 /* oril r31, r1, 0x0 */
620                || op == 0x7c3f0b78)
621         {                       /* mr r31, r1 */
622           fdata->frameless = 0;
623           framep = 1;
624           fdata->alloca_reg = 31;
625           continue;
626
627           /* Another way to set up the frame pointer.  */
628         }
629       else if ((op & 0xfc1fffff) == 0x38010000)
630         {                       /* addi rX, r1, 0x0 */
631           fdata->frameless = 0;
632           framep = 1;
633           fdata->alloca_reg = (op & ~0x38010000) >> 21;
634           continue;
635
636         }
637       else
638         {
639           break;
640         }
641     }
642
643 #if 0
644 /* I have problems with skipping over __main() that I need to address
645  * sometime. Previously, I used to use misc_function_vector which
646  * didn't work as well as I wanted to be.  -MGO */
647
648   /* If the first thing after skipping a prolog is a branch to a function,
649      this might be a call to an initializer in main(), introduced by gcc2.
650      We'd like to skip over it as well. Fortunately, xlc does some extra
651      work before calling a function right after a prologue, thus we can
652      single out such gcc2 behaviour. */
653
654
655   if ((op & 0xfc000001) == 0x48000001)
656     {                           /* bl foo, an initializer function? */
657       op = read_memory_integer (pc + 4, 4);
658
659       if (op == 0x4def7b82)
660         {                       /* cror 0xf, 0xf, 0xf (nop) */
661
662           /* check and see if we are in main. If so, skip over this initializer
663              function as well. */
664
665           tmp = find_pc_misc_function (pc);
666           if (tmp >= 0 && STREQ (misc_function_vector[tmp].name, "main"))
667             return pc + 8;
668         }
669     }
670 #endif /* 0 */
671
672   fdata->offset = -fdata->offset;
673   return last_prologue_pc;
674 }
675
676
677 /*************************************************************************
678   Support for creating pushing a dummy frame into the stack, and popping
679   frames, etc. 
680 *************************************************************************/
681
682 extern int stop_stack_dummy;
683
684
685 /* Pop the innermost frame, go back to the caller. */
686
687 static void
688 rs6000_pop_frame (void)
689 {
690   CORE_ADDR pc, lr, sp, prev_sp;        /* %pc, %lr, %sp */
691   struct rs6000_framedata fdata;
692   struct frame_info *frame = get_current_frame ();
693   int addr, ii, wordsize;
694
695   pc = read_pc ();
696   sp = FRAME_FP (frame);
697
698   if (stop_stack_dummy)
699     {
700       generic_pop_dummy_frame ();
701       flush_cached_frames ();
702       return;
703     }
704
705   /* Make sure that all registers are valid.  */
706   read_register_bytes (0, NULL, REGISTER_BYTES);
707
708   /* figure out previous %pc value. If the function is frameless, it is 
709      still in the link register, otherwise walk the frames and retrieve the
710      saved %pc value in the previous frame. */
711
712   addr = get_pc_function_start (frame->pc);
713   (void) skip_prologue (addr, &fdata);
714
715   wordsize = TDEP->wordsize;
716   if (fdata.frameless)
717     prev_sp = sp;
718   else
719     prev_sp = read_memory_addr (sp, wordsize);
720   if (fdata.lr_offset == 0)
721     lr = read_register (LR_REGNUM);
722   else
723     lr = read_memory_addr (prev_sp + fdata.lr_offset, wordsize);
724
725   /* reset %pc value. */
726   write_register (PC_REGNUM, lr);
727
728   /* reset register values if any was saved earlier. */
729
730   if (fdata.saved_gpr != -1)
731     {
732       addr = prev_sp + fdata.gpr_offset;
733       for (ii = fdata.saved_gpr; ii <= 31; ++ii)
734         {
735           read_memory (addr, &registers[REGISTER_BYTE (ii)], wordsize);
736           addr += wordsize;
737         }
738     }
739
740   if (fdata.saved_fpr != -1)
741     {
742       addr = prev_sp + fdata.fpr_offset;
743       for (ii = fdata.saved_fpr; ii <= 31; ++ii)
744         {
745           read_memory (addr, &registers[REGISTER_BYTE (ii + FP0_REGNUM)], 8);
746           addr += 8;
747         }
748     }
749
750   write_register (SP_REGNUM, prev_sp);
751   target_store_registers (-1);
752   flush_cached_frames ();
753 }
754
755 /* Fixup the call sequence of a dummy function, with the real function
756    address.  Its arguments will be passed by gdb. */
757
758 static void
759 rs6000_fix_call_dummy (char *dummyname, CORE_ADDR pc, CORE_ADDR fun,
760                        int nargs, value_ptr *args, struct type *type,
761                        int gcc_p)
762 {
763 #define TOC_ADDR_OFFSET         20
764 #define TARGET_ADDR_OFFSET      28
765
766   int ii;
767   CORE_ADDR target_addr;
768
769   if (rs6000_find_toc_address_hook != NULL)
770     {
771       CORE_ADDR tocvalue = (*rs6000_find_toc_address_hook) (fun);
772       write_register (TOC_REGNUM, tocvalue);
773     }
774 }
775
776 /* Pass the arguments in either registers, or in the stack. In RS/6000,
777    the first eight words of the argument list (that might be less than
778    eight parameters if some parameters occupy more than one word) are
779    passed in r3..r10 registers.  float and double parameters are
780    passed in fpr's, in addition to that. Rest of the parameters if any
781    are passed in user stack. There might be cases in which half of the
782    parameter is copied into registers, the other half is pushed into
783    stack.
784
785    Stack must be aligned on 64-bit boundaries when synthesizing
786    function calls.
787
788    If the function is returning a structure, then the return address is passed
789    in r3, then the first 7 words of the parameters can be passed in registers,
790    starting from r4. */
791
792 static CORE_ADDR
793 rs6000_push_arguments (int nargs, value_ptr *args, CORE_ADDR sp,
794                        int struct_return, CORE_ADDR struct_addr)
795 {
796   int ii;
797   int len = 0;
798   int argno;                    /* current argument number */
799   int argbytes;                 /* current argument byte */
800   char tmp_buffer[50];
801   int f_argno = 0;              /* current floating point argno */
802   int wordsize = TDEP->wordsize;
803
804   value_ptr arg = 0;
805   struct type *type;
806
807   CORE_ADDR saved_sp;
808
809   /* The first eight words of ther arguments are passed in registers. Copy
810      them appropriately.
811
812      If the function is returning a `struct', then the first word (which 
813      will be passed in r3) is used for struct return address. In that
814      case we should advance one word and start from r4 register to copy 
815      parameters. */
816
817   ii = struct_return ? 1 : 0;
818
819 /* 
820    effectively indirect call... gcc does...
821
822    return_val example( float, int);
823
824    eabi: 
825    float in fp0, int in r3
826    offset of stack on overflow 8/16
827    for varargs, must go by type.
828    power open:
829    float in r3&r4, int in r5
830    offset of stack on overflow different 
831    both: 
832    return in r3 or f0.  If no float, must study how gcc emulates floats;
833    pay attention to arg promotion.  
834    User may have to cast\args to handle promotion correctly 
835    since gdb won't know if prototype supplied or not.
836  */
837
838   for (argno = 0, argbytes = 0; argno < nargs && ii < 8; ++ii)
839     {
840       int reg_size = REGISTER_RAW_SIZE (ii + 3);
841
842       arg = args[argno];
843       type = check_typedef (VALUE_TYPE (arg));
844       len = TYPE_LENGTH (type);
845
846       if (TYPE_CODE (type) == TYPE_CODE_FLT)
847         {
848
849           /* floating point arguments are passed in fpr's, as well as gpr's.
850              There are 13 fpr's reserved for passing parameters. At this point
851              there is no way we would run out of them. */
852
853           if (len > 8)
854             printf_unfiltered (
855                                 "Fatal Error: a floating point parameter #%d with a size > 8 is found!\n", argno);
856
857           memcpy (&registers[REGISTER_BYTE (FP0_REGNUM + 1 + f_argno)],
858                   VALUE_CONTENTS (arg),
859                   len);
860           ++f_argno;
861         }
862
863       if (len > reg_size)
864         {
865
866           /* Argument takes more than one register. */
867           while (argbytes < len)
868             {
869               memset (&registers[REGISTER_BYTE (ii + 3)], 0, reg_size);
870               memcpy (&registers[REGISTER_BYTE (ii + 3)],
871                       ((char *) VALUE_CONTENTS (arg)) + argbytes,
872                       (len - argbytes) > reg_size
873                         ? reg_size : len - argbytes);
874               ++ii, argbytes += reg_size;
875
876               if (ii >= 8)
877                 goto ran_out_of_registers_for_arguments;
878             }
879           argbytes = 0;
880           --ii;
881         }
882       else
883         {                       /* Argument can fit in one register. No problem. */
884           int adj = TARGET_BYTE_ORDER == BIG_ENDIAN ? reg_size - len : 0;
885           memset (&registers[REGISTER_BYTE (ii + 3)], 0, reg_size);
886           memcpy ((char *)&registers[REGISTER_BYTE (ii + 3)] + adj, 
887                   VALUE_CONTENTS (arg), len);
888         }
889       ++argno;
890     }
891
892 ran_out_of_registers_for_arguments:
893
894   saved_sp = read_sp ();
895 #ifndef ELF_OBJECT_FORMAT
896   /* location for 8 parameters are always reserved. */
897   sp -= wordsize * 8;
898
899   /* another six words for back chain, TOC register, link register, etc. */
900   sp -= wordsize * 6;
901
902   /* stack pointer must be quadword aligned */
903   sp &= -16;
904 #endif
905
906   /* if there are more arguments, allocate space for them in 
907      the stack, then push them starting from the ninth one. */
908
909   if ((argno < nargs) || argbytes)
910     {
911       int space = 0, jj;
912
913       if (argbytes)
914         {
915           space += ((len - argbytes + 3) & -4);
916           jj = argno + 1;
917         }
918       else
919         jj = argno;
920
921       for (; jj < nargs; ++jj)
922         {
923           value_ptr val = args[jj];
924           space += ((TYPE_LENGTH (VALUE_TYPE (val))) + 3) & -4;
925         }
926
927       /* add location required for the rest of the parameters */
928       space = (space + 15) & -16;
929       sp -= space;
930
931       /* This is another instance we need to be concerned about securing our
932          stack space. If we write anything underneath %sp (r1), we might conflict
933          with the kernel who thinks he is free to use this area. So, update %sp
934          first before doing anything else. */
935
936       write_register (SP_REGNUM, sp);
937
938       /* if the last argument copied into the registers didn't fit there 
939          completely, push the rest of it into stack. */
940
941       if (argbytes)
942         {
943           write_memory (sp + 24 + (ii * 4),
944                         ((char *) VALUE_CONTENTS (arg)) + argbytes,
945                         len - argbytes);
946           ++argno;
947           ii += ((len - argbytes + 3) & -4) / 4;
948         }
949
950       /* push the rest of the arguments into stack. */
951       for (; argno < nargs; ++argno)
952         {
953
954           arg = args[argno];
955           type = check_typedef (VALUE_TYPE (arg));
956           len = TYPE_LENGTH (type);
957
958
959           /* float types should be passed in fpr's, as well as in the stack. */
960           if (TYPE_CODE (type) == TYPE_CODE_FLT && f_argno < 13)
961             {
962
963               if (len > 8)
964                 printf_unfiltered (
965                                     "Fatal Error: a floating point parameter #%d with a size > 8 is found!\n", argno);
966
967               memcpy (&registers[REGISTER_BYTE (FP0_REGNUM + 1 + f_argno)],
968                       VALUE_CONTENTS (arg),
969                       len);
970               ++f_argno;
971             }
972
973           write_memory (sp + 24 + (ii * 4), (char *) VALUE_CONTENTS (arg), len);
974           ii += ((len + 3) & -4) / 4;
975         }
976     }
977   else
978     /* Secure stack areas first, before doing anything else. */
979     write_register (SP_REGNUM, sp);
980
981   /* set back chain properly */
982   store_address (tmp_buffer, 4, saved_sp);
983   write_memory (sp, tmp_buffer, 4);
984
985   target_store_registers (-1);
986   return sp;
987 }
988
989 /* Function: ppc_push_return_address (pc, sp)
990    Set up the return address for the inferior function call. */
991
992 static CORE_ADDR
993 ppc_push_return_address (CORE_ADDR pc, CORE_ADDR sp)
994 {
995   write_register (LR_REGNUM, CALL_DUMMY_ADDRESS ());
996   return sp;
997 }
998
999 /* Extract a function return value of type TYPE from raw register array
1000    REGBUF, and copy that return value into VALBUF in virtual format. */
1001
1002 static void
1003 rs6000_extract_return_value (struct type *valtype, char *regbuf, char *valbuf)
1004 {
1005   int offset = 0;
1006
1007   if (TYPE_CODE (valtype) == TYPE_CODE_FLT)
1008     {
1009
1010       double dd;
1011       float ff;
1012       /* floats and doubles are returned in fpr1. fpr's have a size of 8 bytes.
1013          We need to truncate the return value into float size (4 byte) if
1014          necessary. */
1015
1016       if (TYPE_LENGTH (valtype) > 4)    /* this is a double */
1017         memcpy (valbuf,
1018                 &regbuf[REGISTER_BYTE (FP0_REGNUM + 1)],
1019                 TYPE_LENGTH (valtype));
1020       else
1021         {                       /* float */
1022           memcpy (&dd, &regbuf[REGISTER_BYTE (FP0_REGNUM + 1)], 8);
1023           ff = (float) dd;
1024           memcpy (valbuf, &ff, sizeof (float));
1025         }
1026     }
1027   else
1028     {
1029       /* return value is copied starting from r3. */
1030       if (TARGET_BYTE_ORDER == BIG_ENDIAN
1031           && TYPE_LENGTH (valtype) < REGISTER_RAW_SIZE (3))
1032         offset = REGISTER_RAW_SIZE (3) - TYPE_LENGTH (valtype);
1033
1034       memcpy (valbuf,
1035               regbuf + REGISTER_BYTE (3) + offset,
1036               TYPE_LENGTH (valtype));
1037     }
1038 }
1039
1040 /* Keep structure return address in this variable.
1041    FIXME:  This is a horrid kludge which should not be allowed to continue
1042    living.  This only allows a single nested call to a structure-returning
1043    function.  Come on, guys!  -- gnu@cygnus.com, Aug 92  */
1044
1045 static CORE_ADDR rs6000_struct_return_address;
1046
1047 /* Indirect function calls use a piece of trampoline code to do context
1048    switching, i.e. to set the new TOC table. Skip such code if we are on
1049    its first instruction (as when we have single-stepped to here). 
1050    Also skip shared library trampoline code (which is different from
1051    indirect function call trampolines).
1052    Result is desired PC to step until, or NULL if we are not in
1053    trampoline code.  */
1054
1055 CORE_ADDR
1056 rs6000_skip_trampoline_code (CORE_ADDR pc)
1057 {
1058   register unsigned int ii, op;
1059   CORE_ADDR solib_target_pc;
1060
1061   static unsigned trampoline_code[] =
1062   {
1063     0x800b0000,                 /*     l   r0,0x0(r11)  */
1064     0x90410014,                 /*    st   r2,0x14(r1)  */
1065     0x7c0903a6,                 /* mtctr   r0           */
1066     0x804b0004,                 /*     l   r2,0x4(r11)  */
1067     0x816b0008,                 /*     l  r11,0x8(r11)  */
1068     0x4e800420,                 /*  bctr                */
1069     0x4e800020,                 /*    br                */
1070     0
1071   };
1072
1073   /* If pc is in a shared library trampoline, return its target.  */
1074   solib_target_pc = find_solib_trampoline_target (pc);
1075   if (solib_target_pc)
1076     return solib_target_pc;
1077
1078   for (ii = 0; trampoline_code[ii]; ++ii)
1079     {
1080       op = read_memory_integer (pc + (ii * 4), 4);
1081       if (op != trampoline_code[ii])
1082         return 0;
1083     }
1084   ii = read_register (11);      /* r11 holds destination addr   */
1085   pc = read_memory_addr (ii, TDEP->wordsize); /* (r11) value */
1086   return pc;
1087 }
1088
1089 /* Determines whether the function FI has a frame on the stack or not.  */
1090
1091 static int
1092 rs6000_frameless_function_invocation (struct frame_info *fi)
1093 {
1094   CORE_ADDR func_start;
1095   struct rs6000_framedata fdata;
1096
1097   /* Don't even think about framelessness except on the innermost frame
1098      or if the function was interrupted by a signal.  */
1099   if (fi->next != NULL && !fi->next->signal_handler_caller)
1100     return 0;
1101
1102   func_start = get_pc_function_start (fi->pc);
1103
1104   /* If we failed to find the start of the function, it is a mistake
1105      to inspect the instructions. */
1106
1107   if (!func_start)
1108     {
1109       /* A frame with a zero PC is usually created by dereferencing a NULL
1110          function pointer, normally causing an immediate core dump of the
1111          inferior. Mark function as frameless, as the inferior has no chance
1112          of setting up a stack frame.  */
1113       if (fi->pc == 0)
1114         return 1;
1115       else
1116         return 0;
1117     }
1118
1119   (void) skip_prologue (func_start, &fdata);
1120   return fdata.frameless;
1121 }
1122
1123 /* Return the PC saved in a frame */
1124
1125 static CORE_ADDR
1126 rs6000_frame_saved_pc (struct frame_info *fi)
1127 {
1128   CORE_ADDR func_start;
1129   struct rs6000_framedata fdata;
1130   int wordsize = TDEP->wordsize;
1131
1132   if (fi->signal_handler_caller)
1133     return read_memory_addr (fi->frame + SIG_FRAME_PC_OFFSET, wordsize);
1134
1135   if (PC_IN_CALL_DUMMY (fi->pc, fi->frame, fi->frame))
1136     return generic_read_register_dummy (fi->pc, fi->frame, PC_REGNUM);
1137
1138   func_start = get_pc_function_start (fi->pc);
1139
1140   /* If we failed to find the start of the function, it is a mistake
1141      to inspect the instructions. */
1142   if (!func_start)
1143     return 0;
1144
1145   (void) skip_prologue (func_start, &fdata);
1146
1147   if (fdata.lr_offset == 0 && fi->next != NULL)
1148     {
1149       if (fi->next->signal_handler_caller)
1150         return read_memory_addr (fi->next->frame + SIG_FRAME_LR_OFFSET,
1151                                  wordsize);
1152       else
1153         return read_memory_addr (FRAME_CHAIN (fi) + DEFAULT_LR_SAVE,
1154                                  wordsize);
1155     }
1156
1157   if (fdata.lr_offset == 0)
1158     return read_register (LR_REGNUM);
1159
1160   return read_memory_addr (FRAME_CHAIN (fi) + fdata.lr_offset, wordsize);
1161 }
1162
1163 /* If saved registers of frame FI are not known yet, read and cache them.
1164    &FDATAP contains rs6000_framedata; TDATAP can be NULL,
1165    in which case the framedata are read.  */
1166
1167 static void
1168 frame_get_saved_regs (struct frame_info *fi, struct rs6000_framedata *fdatap)
1169 {
1170   CORE_ADDR frame_addr;
1171   struct rs6000_framedata work_fdata;
1172   int wordsize = TDEP->wordsize;
1173
1174   if (fi->saved_regs)
1175     return;
1176
1177   if (fdatap == NULL)
1178     {
1179       fdatap = &work_fdata;
1180       (void) skip_prologue (get_pc_function_start (fi->pc), fdatap);
1181     }
1182
1183   frame_saved_regs_zalloc (fi);
1184
1185   /* If there were any saved registers, figure out parent's stack
1186      pointer. */
1187   /* The following is true only if the frame doesn't have a call to
1188      alloca(), FIXME. */
1189
1190   if (fdatap->saved_fpr == 0 && fdatap->saved_gpr == 0
1191       && fdatap->lr_offset == 0 && fdatap->cr_offset == 0)
1192     frame_addr = 0;
1193   else if (fi->prev && fi->prev->frame)
1194     frame_addr = fi->prev->frame;
1195   else
1196     frame_addr = read_memory_addr (fi->frame, wordsize);
1197
1198   /* if != -1, fdatap->saved_fpr is the smallest number of saved_fpr.
1199      All fpr's from saved_fpr to fp31 are saved.  */
1200
1201   if (fdatap->saved_fpr >= 0)
1202     {
1203       int i;
1204       CORE_ADDR fpr_addr = frame_addr + fdatap->fpr_offset;
1205       for (i = fdatap->saved_fpr; i < 32; i++)
1206         {
1207           fi->saved_regs[FP0_REGNUM + i] = fpr_addr;
1208           fpr_addr += 8;
1209         }
1210     }
1211
1212   /* if != -1, fdatap->saved_gpr is the smallest number of saved_gpr.
1213      All gpr's from saved_gpr to gpr31 are saved.  */
1214
1215   if (fdatap->saved_gpr >= 0)
1216     {
1217       int i;
1218       CORE_ADDR gpr_addr = frame_addr + fdatap->gpr_offset;
1219       for (i = fdatap->saved_gpr; i < 32; i++)
1220         {
1221           fi->saved_regs[i] = gpr_addr;
1222           gpr_addr += wordsize;
1223         }
1224     }
1225
1226   /* If != 0, fdatap->cr_offset is the offset from the frame that holds
1227      the CR.  */
1228   if (fdatap->cr_offset != 0)
1229     fi->saved_regs[CR_REGNUM] = frame_addr + fdatap->cr_offset;
1230
1231   /* If != 0, fdatap->lr_offset is the offset from the frame that holds
1232      the LR.  */
1233   if (fdatap->lr_offset != 0)
1234     fi->saved_regs[LR_REGNUM] = frame_addr + fdatap->lr_offset;
1235 }
1236
1237 /* Return the address of a frame. This is the inital %sp value when the frame
1238    was first allocated. For functions calling alloca(), it might be saved in
1239    an alloca register. */
1240
1241 static CORE_ADDR
1242 frame_initial_stack_address (struct frame_info *fi)
1243 {
1244   CORE_ADDR tmpaddr;
1245   struct rs6000_framedata fdata;
1246   struct frame_info *callee_fi;
1247
1248   /* if the initial stack pointer (frame address) of this frame is known,
1249      just return it. */
1250
1251   if (fi->extra_info->initial_sp)
1252     return fi->extra_info->initial_sp;
1253
1254   /* find out if this function is using an alloca register.. */
1255
1256   (void) skip_prologue (get_pc_function_start (fi->pc), &fdata);
1257
1258   /* if saved registers of this frame are not known yet, read and cache them. */
1259
1260   if (!fi->saved_regs)
1261     frame_get_saved_regs (fi, &fdata);
1262
1263   /* If no alloca register used, then fi->frame is the value of the %sp for
1264      this frame, and it is good enough. */
1265
1266   if (fdata.alloca_reg < 0)
1267     {
1268       fi->extra_info->initial_sp = fi->frame;
1269       return fi->extra_info->initial_sp;
1270     }
1271
1272   /* This function has an alloca register. If this is the top-most frame
1273      (with the lowest address), the value in alloca register is good. */
1274
1275   if (!fi->next)
1276     return fi->extra_info->initial_sp = read_register (fdata.alloca_reg);
1277
1278   /* Otherwise, this is a caller frame. Callee has usually already saved
1279      registers, but there are exceptions (such as when the callee
1280      has no parameters). Find the address in which caller's alloca
1281      register is saved. */
1282
1283   for (callee_fi = fi->next; callee_fi; callee_fi = callee_fi->next)
1284     {
1285
1286       if (!callee_fi->saved_regs)
1287         frame_get_saved_regs (callee_fi, NULL);
1288
1289       /* this is the address in which alloca register is saved. */
1290
1291       tmpaddr = callee_fi->saved_regs[fdata.alloca_reg];
1292       if (tmpaddr)
1293         {
1294           fi->extra_info->initial_sp =
1295             read_memory_addr (tmpaddr, TDEP->wordsize);
1296           return fi->extra_info->initial_sp;
1297         }
1298
1299       /* Go look into deeper levels of the frame chain to see if any one of
1300          the callees has saved alloca register. */
1301     }
1302
1303   /* If alloca register was not saved, by the callee (or any of its callees)
1304      then the value in the register is still good. */
1305
1306   fi->extra_info->initial_sp = read_register (fdata.alloca_reg);
1307   return fi->extra_info->initial_sp;
1308 }
1309
1310 /* Describe the pointer in each stack frame to the previous stack frame
1311    (its caller).  */
1312
1313 /* FRAME_CHAIN takes a frame's nominal address
1314    and produces the frame's chain-pointer. */
1315
1316 /* In the case of the RS/6000, the frame's nominal address
1317    is the address of a 4-byte word containing the calling frame's address.  */
1318
1319 static CORE_ADDR
1320 rs6000_frame_chain (struct frame_info *thisframe)
1321 {
1322   CORE_ADDR fp, fpp, lr;
1323   int wordsize = TDEP->wordsize;
1324
1325   if (PC_IN_CALL_DUMMY (thisframe->pc, thisframe->frame, thisframe->frame))
1326     return thisframe->frame;    /* dummy frame same as caller's frame */
1327
1328   if (inside_entry_file (thisframe->pc) ||
1329       thisframe->pc == entry_point_address ())
1330     return 0;
1331
1332   if (thisframe->signal_handler_caller)
1333     fp = read_memory_addr (thisframe->frame + SIG_FRAME_FP_OFFSET,
1334                               wordsize);
1335   else if (thisframe->next != NULL
1336            && thisframe->next->signal_handler_caller
1337            && FRAMELESS_FUNCTION_INVOCATION (thisframe))
1338     /* A frameless function interrupted by a signal did not change the
1339        frame pointer.  */
1340     fp = FRAME_FP (thisframe);
1341   else
1342     fp = read_memory_addr ((thisframe)->frame, wordsize);
1343
1344   lr = read_register (LR_REGNUM);
1345   if (lr == entry_point_address ())
1346     if (fp != 0 && (fpp = read_memory_addr (fp, wordsize)) != 0)
1347       if (PC_IN_CALL_DUMMY (lr, fpp, fpp))
1348         return fpp;
1349
1350   return fp;
1351 }
1352
1353 /* Return the size of register REG when words are WORDSIZE bytes long.  If REG
1354    isn't available with that word size, return 0. */
1355
1356 static int
1357 regsize (const struct reg *reg, int wordsize)
1358 {
1359   return wordsize == 8 ? reg->sz64 : reg->sz32;
1360 }
1361
1362 /* Return the name of register number N, or null if no such register exists
1363    in the current architecture. */
1364
1365 static char *
1366 rs6000_register_name (int n)
1367 {
1368   struct gdbarch_tdep *tdep = TDEP;
1369   const struct reg *reg = tdep->regs + n;
1370
1371   if (!regsize (reg, tdep->wordsize))
1372     return NULL;
1373   return reg->name;
1374 }
1375
1376 /* Index within `registers' of the first byte of the space for
1377    register N.  */
1378
1379 static int
1380 rs6000_register_byte (int n)
1381 {
1382   return TDEP->regoff[n];
1383 }
1384
1385 /* Return the number of bytes of storage in the actual machine representation
1386    for register N if that register is available, else return 0. */
1387
1388 static int
1389 rs6000_register_raw_size (int n)
1390 {
1391   struct gdbarch_tdep *tdep = TDEP;
1392   const struct reg *reg = tdep->regs + n;
1393   return regsize (reg, tdep->wordsize);
1394 }
1395
1396 /* Number of bytes of storage in the program's representation
1397    for register N.  */
1398
1399 static int
1400 rs6000_register_virtual_size (int n)
1401 {
1402   return TYPE_LENGTH (REGISTER_VIRTUAL_TYPE (n));
1403 }
1404
1405 /* Return the GDB type object for the "standard" data type
1406    of data in register N.  */
1407
1408 static struct type *
1409 rs6000_register_virtual_type (n)
1410 {
1411   struct gdbarch_tdep *tdep = TDEP;
1412   const struct reg *reg = tdep->regs + n;
1413
1414   return reg->fpr ? builtin_type_double :
1415     regsize (reg, tdep->wordsize) == 8 ? builtin_type_int64 :
1416       builtin_type_int32;
1417 }
1418
1419 /* For the PowerPC, it appears that the debug info marks float parameters as
1420    floats regardless of whether the function is prototyped, but the actual
1421    values are always passed in as doubles.  Tell gdb to always assume that
1422    floats are passed as doubles and then converted in the callee. */
1423
1424 static int
1425 rs6000_coerce_float_to_double (struct type *formal, struct type *actual)
1426 {
1427   return 1;
1428 }
1429
1430 /* Return whether register N requires conversion when moving from raw format
1431    to virtual format.
1432
1433    The register format for RS/6000 floating point registers is always
1434    double, we need a conversion if the memory format is float. */
1435
1436 static int
1437 rs6000_register_convertible (int n)
1438 {
1439   const struct reg *reg = TDEP->regs + n;
1440   return reg->fpr;
1441 }
1442
1443 /* Convert data from raw format for register N in buffer FROM
1444    to virtual format with type TYPE in buffer TO. */
1445
1446 static void
1447 rs6000_register_convert_to_virtual (int n, struct type *type,
1448                                     char *from, char *to)
1449 {
1450   if (TYPE_LENGTH (type) != REGISTER_RAW_SIZE (n))
1451     {
1452       double val = extract_floating (from, REGISTER_RAW_SIZE (n));
1453       store_floating (to, TYPE_LENGTH (type), val);
1454     }
1455   else
1456     memcpy (to, from, REGISTER_RAW_SIZE (n));
1457 }
1458
1459 /* Convert data from virtual format with type TYPE in buffer FROM
1460    to raw format for register N in buffer TO. */
1461
1462 static void
1463 rs6000_register_convert_to_raw (struct type *type, int n,
1464                                 char *from, char *to)
1465 {
1466   if (TYPE_LENGTH (type) != REGISTER_RAW_SIZE (n))
1467     {
1468       double val = extract_floating (from, TYPE_LENGTH (type));
1469       store_floating (to, REGISTER_RAW_SIZE (n), val);
1470     }
1471   else
1472     memcpy (to, from, REGISTER_RAW_SIZE (n));
1473 }
1474
1475 /* Store the address of the place in which to copy the structure the
1476    subroutine will return.  This is called from call_function.
1477
1478    In RS/6000, struct return addresses are passed as an extra parameter in r3.
1479    In function return, callee is not responsible of returning this address
1480    back.  Since gdb needs to find it, we will store in a designated variable
1481    `rs6000_struct_return_address'. */
1482
1483 static void
1484 rs6000_store_struct_return (CORE_ADDR addr, CORE_ADDR sp)
1485 {
1486   write_register (3, addr);
1487   rs6000_struct_return_address = addr;
1488 }
1489
1490 /* Write into appropriate registers a function return value
1491    of type TYPE, given in virtual format.  */
1492
1493 static void
1494 rs6000_store_return_value (struct type *type, char *valbuf)
1495 {
1496   if (TYPE_CODE (type) == TYPE_CODE_FLT)
1497
1498     /* Floating point values are returned starting from FPR1 and up.
1499        Say a double_double_double type could be returned in
1500        FPR1/FPR2/FPR3 triple. */
1501
1502     write_register_bytes (REGISTER_BYTE (FP0_REGNUM + 1), valbuf,
1503                           TYPE_LENGTH (type));
1504   else
1505     /* Everything else is returned in GPR3 and up. */
1506     write_register_bytes (REGISTER_BYTE (GP0_REGNUM + 3), valbuf,
1507                           TYPE_LENGTH (type));
1508 }
1509
1510 /* Extract from an array REGBUF containing the (raw) register state
1511    the address in which a function should return its structure value,
1512    as a CORE_ADDR (or an expression that can be used as one).  */
1513
1514 static CORE_ADDR
1515 rs6000_extract_struct_value_address (char *regbuf)
1516 {
1517   return rs6000_struct_return_address;
1518 }
1519
1520 /* Return whether PC is in a dummy function call.
1521
1522    FIXME: This just checks for the end of the stack, which is broken
1523    for things like stepping through gcc nested function stubs. */
1524
1525 static int
1526 rs6000_pc_in_call_dummy (CORE_ADDR pc, CORE_ADDR sp, CORE_ADDR fp)
1527 {
1528   return sp < pc && pc < fp;
1529 }
1530
1531 /* Hook called when a new child process is started. */
1532
1533 void
1534 rs6000_create_inferior (int pid)
1535 {
1536   if (rs6000_set_host_arch_hook)
1537     rs6000_set_host_arch_hook (pid);
1538 }
1539 \f
1540 /* Support for CONVERT_FROM_FUNC_PTR_ADDR(ADDR).
1541
1542    Usually a function pointer's representation is simply the address
1543    of the function. On the RS/6000 however, a function pointer is
1544    represented by a pointer to a TOC entry. This TOC entry contains
1545    three words, the first word is the address of the function, the
1546    second word is the TOC pointer (r2), and the third word is the
1547    static chain value.  Throughout GDB it is currently assumed that a
1548    function pointer contains the address of the function, which is not
1549    easy to fix.  In addition, the conversion of a function address to
1550    a function pointer would require allocation of a TOC entry in the
1551    inferior's memory space, with all its drawbacks.  To be able to
1552    call C++ virtual methods in the inferior (which are called via
1553    function pointers), find_function_addr uses this macro to get the
1554    function address from a function pointer.  */
1555
1556 /* Return nonzero if ADDR (a function pointer) is in the data space and
1557    is therefore a special function pointer.  */
1558
1559 CORE_ADDR
1560 rs6000_convert_from_func_ptr_addr (CORE_ADDR addr)
1561 {
1562   struct obj_section *s;
1563
1564   s = find_pc_section (addr);
1565   if (s && s->the_bfd_section->flags & SEC_CODE)
1566     return addr;
1567
1568   /* ADDR is in the data space, so it's a special function pointer. */
1569   return read_memory_addr (addr, TDEP->wordsize);
1570 }
1571 \f
1572
1573 /* Handling the various POWER/PowerPC variants.  */
1574
1575
1576 /* The arrays here called registers_MUMBLE hold information about available
1577    registers.
1578
1579    For each family of PPC variants, I've tried to isolate out the
1580    common registers and put them up front, so that as long as you get
1581    the general family right, GDB will correctly identify the registers
1582    common to that family.  The common register sets are:
1583
1584    For the 60x family: hid0 hid1 iabr dabr pir
1585
1586    For the 505 and 860 family: eie eid nri
1587
1588    For the 403 and 403GC: icdbdr esr dear evpr cdbcr tsr tcr pit tbhi
1589    tblo srr2 srr3 dbsr dbcr iac1 iac2 dac1 dac2 dccr iccr pbl1
1590    pbu1 pbl2 pbu2
1591
1592    Most of these register groups aren't anything formal.  I arrived at
1593    them by looking at the registers that occurred in more than one
1594    processor. */
1595
1596 /* Convenience macros for populating register arrays. */
1597
1598 /* Within another macro, convert S to a string. */
1599
1600 #define STR(s)  #s
1601
1602 /* Return a struct reg defining register NAME that's 32 bits on 32-bit systems
1603    and 64 bits on 64-bit systems. */
1604 #define R(name)         { STR(name), 4, 8, 0 }
1605
1606 /* Return a struct reg defining register NAME that's 32 bits on all
1607    systems. */
1608 #define R4(name)        { STR(name), 4, 4, 0 }
1609
1610 /* Return a struct reg defining register NAME that's 64 bits on all
1611    systems. */
1612 #define R8(name)        { STR(name), 8, 8, 0 }
1613
1614 /* Return a struct reg defining floating-point register NAME. */
1615 #define F(name)         { STR(name), 8, 8, 1 }
1616
1617 /* Return a struct reg defining register NAME that's 32 bits on 32-bit
1618    systems and that doesn't exist on 64-bit systems. */
1619 #define R32(name)       { STR(name), 4, 0, 0 }
1620
1621 /* Return a struct reg defining register NAME that's 64 bits on 64-bit
1622    systems and that doesn't exist on 32-bit systems. */
1623 #define R64(name)       { STR(name), 0, 8, 0 }
1624
1625 /* Return a struct reg placeholder for a register that doesn't exist. */
1626 #define R0              { 0, 0, 0, 0 }
1627
1628 /* UISA registers common across all architectures, including POWER.  */
1629
1630 #define COMMON_UISA_REGS \
1631   /*  0 */ R(r0), R(r1), R(r2), R(r3), R(r4), R(r5), R(r6), R(r7),  \
1632   /*  8 */ R(r8), R(r9), R(r10),R(r11),R(r12),R(r13),R(r14),R(r15), \
1633   /* 16 */ R(r16),R(r17),R(r18),R(r19),R(r20),R(r21),R(r22),R(r23), \
1634   /* 24 */ R(r24),R(r25),R(r26),R(r27),R(r28),R(r29),R(r30),R(r31), \
1635   /* 32 */ F(f0), F(f1), F(f2), F(f3), F(f4), F(f5), F(f6), F(f7),  \
1636   /* 40 */ F(f8), F(f9), F(f10),F(f11),F(f12),F(f13),F(f14),F(f15), \
1637   /* 48 */ F(f16),F(f17),F(f18),F(f19),F(f20),F(f21),F(f22),F(f23), \
1638   /* 56 */ F(f24),F(f25),F(f26),F(f27),F(f28),F(f29),F(f30),F(f31), \
1639   /* 64 */ R(pc), R(ps)
1640
1641 /* UISA-level SPRs for PowerPC.  */
1642 #define PPC_UISA_SPRS \
1643   /* 66 */ R4(cr),  R(lr), R(ctr), R4(xer), R0
1644
1645 /* Segment registers, for PowerPC.  */
1646 #define PPC_SEGMENT_REGS \
1647   /* 71 */ R32(sr0),  R32(sr1),  R32(sr2),  R32(sr3),  \
1648   /* 75 */ R32(sr4),  R32(sr5),  R32(sr6),  R32(sr7),  \
1649   /* 79 */ R32(sr8),  R32(sr9),  R32(sr10), R32(sr11), \
1650   /* 83 */ R32(sr12), R32(sr13), R32(sr14), R32(sr15)
1651
1652 /* OEA SPRs for PowerPC.  */
1653 #define PPC_OEA_SPRS \
1654   /*  87 */ R4(pvr), \
1655   /*  88 */ R(ibat0u), R(ibat0l), R(ibat1u), R(ibat1l), \
1656   /*  92 */ R(ibat2u), R(ibat2l), R(ibat3u), R(ibat3l), \
1657   /*  96 */ R(dbat0u), R(dbat0l), R(dbat1u), R(dbat1l), \
1658   /* 100 */ R(dbat2u), R(dbat2l), R(dbat3u), R(dbat3l), \
1659   /* 104 */ R(sdr1),   R64(asr),  R(dar),    R4(dsisr), \
1660   /* 108 */ R(sprg0),  R(sprg1),  R(sprg2),  R(sprg3),  \
1661   /* 112 */ R(srr0),   R(srr1),   R(tbl),    R(tbu),    \
1662   /* 116 */ R4(dec),   R(dabr),   R4(ear)
1663
1664 /* IBM POWER (pre-PowerPC) architecture, user-level view.  We only cover
1665    user-level SPR's. */
1666 static const struct reg registers_power[] =
1667 {
1668   COMMON_UISA_REGS,
1669   /* 66 */ R4(cnd), R(lr), R(cnt), R4(xer), R4(mq)
1670 };
1671
1672 /* PowerPC UISA - a PPC processor as viewed by user-level code.  A UISA-only
1673    view of the PowerPC. */
1674 static const struct reg registers_powerpc[] =
1675 {
1676   COMMON_UISA_REGS,
1677   PPC_UISA_SPRS
1678 };
1679
1680 /* IBM PowerPC 403. */
1681 static const struct reg registers_403[] =
1682 {
1683   COMMON_UISA_REGS,
1684   PPC_UISA_SPRS,
1685   PPC_SEGMENT_REGS,
1686   PPC_OEA_SPRS,
1687   /* 119 */ R(icdbdr), R(esr),  R(dear), R(evpr),
1688   /* 123 */ R(cdbcr),  R(tsr),  R(tcr),  R(pit),
1689   /* 127 */ R(tbhi),   R(tblo), R(srr2), R(srr3),
1690   /* 131 */ R(dbsr),   R(dbcr), R(iac1), R(iac2),
1691   /* 135 */ R(dac1),   R(dac2), R(dccr), R(iccr),
1692   /* 139 */ R(pbl1),   R(pbu1), R(pbl2), R(pbu2)
1693 };
1694
1695 /* IBM PowerPC 403GC. */
1696 static const struct reg registers_403GC[] =
1697 {
1698   COMMON_UISA_REGS,
1699   PPC_UISA_SPRS,
1700   PPC_SEGMENT_REGS,
1701   PPC_OEA_SPRS,
1702   /* 119 */ R(icdbdr), R(esr),  R(dear), R(evpr),
1703   /* 123 */ R(cdbcr),  R(tsr),  R(tcr),  R(pit),
1704   /* 127 */ R(tbhi),   R(tblo), R(srr2), R(srr3),
1705   /* 131 */ R(dbsr),   R(dbcr), R(iac1), R(iac2),
1706   /* 135 */ R(dac1),   R(dac2), R(dccr), R(iccr),
1707   /* 139 */ R(pbl1),   R(pbu1), R(pbl2), R(pbu2),
1708   /* 143 */ R(zpr),    R(pid),  R(sgr),  R(dcwr),
1709   /* 147 */ R(tbhu),   R(tblu)
1710 };
1711
1712 /* Motorola PowerPC 505. */
1713 static const struct reg registers_505[] =
1714 {
1715   COMMON_UISA_REGS,
1716   PPC_UISA_SPRS,
1717   PPC_SEGMENT_REGS,
1718   PPC_OEA_SPRS,
1719   /* 119 */ R(eie), R(eid), R(nri)
1720 };
1721
1722 /* Motorola PowerPC 860 or 850. */
1723 static const struct reg registers_860[] =
1724 {
1725   COMMON_UISA_REGS,
1726   PPC_UISA_SPRS,
1727   PPC_SEGMENT_REGS,
1728   PPC_OEA_SPRS,
1729   /* 119 */ R(eie), R(eid), R(nri), R(cmpa),
1730   /* 123 */ R(cmpb), R(cmpc), R(cmpd), R(icr),
1731   /* 127 */ R(der), R(counta), R(countb), R(cmpe),
1732   /* 131 */ R(cmpf), R(cmpg), R(cmph), R(lctrl1),
1733   /* 135 */ R(lctrl2), R(ictrl), R(bar), R(ic_cst),
1734   /* 139 */ R(ic_adr), R(ic_dat), R(dc_cst), R(dc_adr),
1735   /* 143 */ R(dc_dat), R(dpdr), R(dpir), R(immr),
1736   /* 147 */ R(mi_ctr), R(mi_ap), R(mi_epn), R(mi_twc),
1737   /* 151 */ R(mi_rpn), R(md_ctr), R(m_casid), R(md_ap),
1738   /* 155 */ R(md_epn), R(md_twb), R(md_twc), R(md_rpn),
1739   /* 159 */ R(m_tw), R(mi_dbcam), R(mi_dbram0), R(mi_dbram1),
1740   /* 163 */ R(md_dbcam), R(md_dbram0), R(md_dbram1)
1741 };
1742
1743 /* Motorola PowerPC 601.  Note that the 601 has different register numbers
1744    for reading and writing RTCU and RTCL.  However, how one reads and writes a
1745    register is the stub's problem.  */
1746 static const struct reg registers_601[] =
1747 {
1748   COMMON_UISA_REGS,
1749   PPC_UISA_SPRS,
1750   PPC_SEGMENT_REGS,
1751   PPC_OEA_SPRS,
1752   /* 119 */ R(hid0), R(hid1), R(iabr), R(dabr),
1753   /* 123 */ R(pir), R(mq), R(rtcu), R(rtcl)
1754 };
1755
1756 /* Motorola PowerPC 602. */
1757 static const struct reg registers_602[] =
1758 {
1759   COMMON_UISA_REGS,
1760   PPC_UISA_SPRS,
1761   PPC_SEGMENT_REGS,
1762   PPC_OEA_SPRS,
1763   /* 119 */ R(hid0), R(hid1), R(iabr), R0,
1764   /* 123 */ R0, R(tcr), R(ibr), R(esassr),
1765   /* 127 */ R(sebr), R(ser), R(sp), R(lt)
1766 };
1767
1768 /* Motorola/IBM PowerPC 603 or 603e. */
1769 static const struct reg registers_603[] =
1770 {
1771   COMMON_UISA_REGS,
1772   PPC_UISA_SPRS,
1773   PPC_SEGMENT_REGS,
1774   PPC_OEA_SPRS,
1775   /* 119 */ R(hid0), R(hid1), R(iabr), R0,
1776   /* 123 */ R0, R(dmiss), R(dcmp), R(hash1),
1777   /* 127 */ R(hash2), R(imiss), R(icmp), R(rpa)
1778 };
1779
1780 /* Motorola PowerPC 604 or 604e. */
1781 static const struct reg registers_604[] =
1782 {
1783   COMMON_UISA_REGS,
1784   PPC_UISA_SPRS,
1785   PPC_SEGMENT_REGS,
1786   PPC_OEA_SPRS,
1787   /* 119 */ R(hid0), R(hid1), R(iabr), R(dabr),
1788   /* 123 */ R(pir), R(mmcr0), R(pmc1), R(pmc2),
1789   /* 127 */ R(sia), R(sda)
1790 };
1791
1792 /* Motorola/IBM PowerPC 750 or 740. */
1793 static const struct reg registers_750[] =
1794 {
1795   COMMON_UISA_REGS,
1796   PPC_UISA_SPRS,
1797   PPC_SEGMENT_REGS,
1798   PPC_OEA_SPRS,
1799   /* 119 */ R(hid0), R(hid1), R(iabr), R(dabr),
1800   /* 123 */ R0, R(ummcr0), R(upmc1), R(upmc2),
1801   /* 127 */ R(usia), R(ummcr1), R(upmc3), R(upmc4),
1802   /* 131 */ R(mmcr0), R(pmc1), R(pmc2), R(sia),
1803   /* 135 */ R(mmcr1), R(pmc3), R(pmc4), R(l2cr),
1804   /* 139 */ R(ictc), R(thrm1), R(thrm2), R(thrm3)
1805 };
1806
1807
1808 /* Information about a particular processor variant.  */
1809
1810 struct variant
1811   {
1812     /* Name of this variant.  */
1813     char *name;
1814
1815     /* English description of the variant.  */
1816     char *description;
1817
1818     /* bfd_arch_info.arch corresponding to variant. */
1819     enum bfd_architecture arch;
1820
1821     /* bfd_arch_info.mach corresponding to variant. */
1822     unsigned long mach;
1823
1824     /* Table of register names; registers[R] is the name of the register
1825        number R.  */
1826     int nregs;
1827     const struct reg *regs;
1828   };
1829
1830 #define num_registers(list) (sizeof (list) / sizeof((list)[0]))
1831
1832
1833 /* Information in this table comes from the following web sites:
1834    IBM:       http://www.chips.ibm.com:80/products/embedded/
1835    Motorola:  http://www.mot.com/SPS/PowerPC/
1836
1837    I'm sure I've got some of the variant descriptions not quite right.
1838    Please report any inaccuracies you find to GDB's maintainer.
1839
1840    If you add entries to this table, please be sure to allow the new
1841    value as an argument to the --with-cpu flag, in configure.in.  */
1842
1843 static const struct variant variants[] =
1844 {
1845   {"powerpc", "PowerPC user-level", bfd_arch_powerpc,
1846    bfd_mach_ppc, num_registers (registers_powerpc), registers_powerpc},
1847   {"power", "POWER user-level", bfd_arch_rs6000,
1848    bfd_mach_rs6k, num_registers (registers_power), registers_power},
1849   {"403", "IBM PowerPC 403", bfd_arch_powerpc,
1850    bfd_mach_ppc_403, num_registers (registers_403), registers_403},
1851   {"601", "Motorola PowerPC 601", bfd_arch_powerpc,
1852    bfd_mach_ppc_601, num_registers (registers_601), registers_601},
1853   {"602", "Motorola PowerPC 602", bfd_arch_powerpc,
1854    bfd_mach_ppc_602, num_registers (registers_602), registers_602},
1855   {"603", "Motorola/IBM PowerPC 603 or 603e", bfd_arch_powerpc,
1856    bfd_mach_ppc_603, num_registers (registers_603), registers_603},
1857   {"604", "Motorola PowerPC 604 or 604e", bfd_arch_powerpc,
1858    604, num_registers (registers_604), registers_604},
1859   {"403GC", "IBM PowerPC 403GC", bfd_arch_powerpc,
1860    bfd_mach_ppc_403gc, num_registers (registers_403GC), registers_403GC},
1861   {"505", "Motorola PowerPC 505", bfd_arch_powerpc,
1862    bfd_mach_ppc_505, num_registers (registers_505), registers_505},
1863   {"860", "Motorola PowerPC 860 or 850", bfd_arch_powerpc,
1864    bfd_mach_ppc_860, num_registers (registers_860), registers_860},
1865   {"750", "Motorola/IBM PowerPC 750 or 740", bfd_arch_powerpc,
1866    bfd_mach_ppc_750, num_registers (registers_750), registers_750},
1867
1868   /* FIXME: I haven't checked the register sets of the following. */
1869   {"620", "Motorola PowerPC 620", bfd_arch_powerpc,
1870    bfd_mach_ppc_620, num_registers (registers_powerpc), registers_powerpc},
1871   {"a35", "PowerPC A35", bfd_arch_powerpc,
1872    bfd_mach_ppc_a35, num_registers (registers_powerpc), registers_powerpc},
1873   {"rs1", "IBM POWER RS1", bfd_arch_rs6000,
1874    bfd_mach_rs6k_rs1, num_registers (registers_power), registers_power},
1875   {"rsc", "IBM POWER RSC", bfd_arch_rs6000,
1876    bfd_mach_rs6k_rsc, num_registers (registers_power), registers_power},
1877   {"rs2", "IBM POWER RS2", bfd_arch_rs6000,
1878    bfd_mach_rs6k_rs2, num_registers (registers_power), registers_power},
1879
1880   {0, 0, 0, 0}
1881 };
1882
1883 #undef num_registers
1884
1885 /* Look up the variant named NAME in the `variants' table.  Return a
1886    pointer to the struct variant, or null if we couldn't find it.  */
1887
1888 static const struct variant *
1889 find_variant_by_name (char *name)
1890 {
1891   const struct variant *v;
1892
1893   for (v = variants; v->name; v++)
1894     if (!strcmp (name, v->name))
1895       return v;
1896
1897   return NULL;
1898 }
1899
1900 /* Return the variant corresponding to architecture ARCH and machine number
1901    MACH.  If no such variant exists, return null. */
1902
1903 static const struct variant *
1904 find_variant_by_arch (enum bfd_architecture arch, unsigned long mach)
1905 {
1906   const struct variant *v;
1907
1908   for (v = variants; v->name; v++)
1909     if (arch == v->arch && mach == v->mach)
1910       return v;
1911
1912   return NULL;
1913 }
1914
1915 \f
1916
1917 /* Initialize the current architecture based on INFO.  If possible, re-use an
1918    architecture from ARCHES, which is a list of architectures already created
1919    during this debugging session.
1920
1921    Called e.g. at program startup, when reading a core file, and when reading
1922    a binary file. */
1923
1924 static struct gdbarch *
1925 rs6000_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
1926 {
1927   struct gdbarch *gdbarch;
1928   struct gdbarch_tdep *tdep;
1929   int wordsize, fromexec, power, i, off;
1930   struct reg *regs;
1931   const struct variant *v;
1932   enum bfd_architecture arch;
1933   unsigned long mach;
1934   bfd abfd;
1935
1936   fromexec = info.abfd && info.abfd->format == bfd_object &&
1937     bfd_get_flavour (info.abfd) == bfd_target_xcoff_flavour;
1938
1939   /* Check word size.  If INFO is from a binary file, infer it from that,
1940      else use the previously-inferred size. */
1941   if (fromexec)
1942     {
1943       if (xcoff_data (info.abfd)->xcoff64)
1944         wordsize = 8;
1945       else
1946         wordsize = 4;
1947     }
1948   else
1949     {
1950       tdep = TDEP;
1951       if (tdep)
1952         wordsize = tdep->wordsize;
1953       else
1954         wordsize = 4;
1955     }
1956
1957   /* Find a candidate among extant architectures. */
1958   for (arches = gdbarch_list_lookup_by_info (arches, &info);
1959        arches != NULL;
1960        arches = gdbarch_list_lookup_by_info (arches->next, &info))
1961     {
1962       /* Word size in the various PowerPC bfd_arch_info structs isn't
1963          meaningful, because 64-bit CPUs can run in 32-bit mode.  So, perform
1964          separate word size check. */
1965       tdep = gdbarch_tdep (arches->gdbarch);
1966       if (tdep && tdep->wordsize == wordsize)
1967         return arches->gdbarch;
1968     }
1969
1970   /* None found, create a new architecture from INFO, whose bfd_arch_info
1971      validity depends on the source:
1972        - executable             useless
1973        - rs6000_host_arch()     good
1974        - core file              good
1975        - "set arch"             trust blindly
1976        - GDB startup            useless but harmless */
1977
1978   if (!fromexec)
1979     {
1980       arch = info.bfd_architecture;
1981       mach = info.bfd_arch_info->mach;
1982     }
1983   else
1984     {
1985       arch = bfd_arch_powerpc;
1986       mach = 0;
1987       bfd_default_set_arch_mach (&abfd, arch, mach);
1988       info.bfd_arch_info = bfd_get_arch_info (&abfd);
1989     }
1990   tdep = xmalloc (sizeof (struct gdbarch_tdep));
1991   tdep->wordsize = wordsize;
1992   gdbarch = gdbarch_alloc (&info, tdep);
1993   power = arch == bfd_arch_rs6000;
1994
1995   /* Select instruction printer. */
1996   tm_print_insn = arch == power ? print_insn_rs6000 :
1997     info.byte_order == BIG_ENDIAN ? print_insn_big_powerpc :
1998       print_insn_little_powerpc;
1999
2000   /* Choose variant. */
2001   v = find_variant_by_arch (arch, mach);
2002   if (!v)
2003     v = find_variant_by_name (power ? "power" : "powerpc");
2004   tdep->regs = v->regs;
2005
2006   /* Calculate byte offsets in raw register array. */
2007   tdep->regoff = xmalloc (v->nregs * sizeof (int));
2008   for (i = off = 0; i < v->nregs; i++)
2009     {
2010       tdep->regoff[i] = off;
2011       off += regsize (v->regs + i, wordsize);
2012     }
2013
2014   set_gdbarch_read_pc (gdbarch, generic_target_read_pc);
2015   set_gdbarch_write_pc (gdbarch, generic_target_write_pc);
2016   set_gdbarch_read_fp (gdbarch, generic_target_read_fp);
2017   set_gdbarch_write_fp (gdbarch, generic_target_write_fp);
2018   set_gdbarch_read_sp (gdbarch, generic_target_read_sp);
2019   set_gdbarch_write_sp (gdbarch, generic_target_write_sp);
2020
2021   set_gdbarch_num_regs (gdbarch, v->nregs);
2022   set_gdbarch_sp_regnum (gdbarch, 1);
2023   set_gdbarch_fp_regnum (gdbarch, 1);
2024   set_gdbarch_pc_regnum (gdbarch, 64);
2025   set_gdbarch_register_name (gdbarch, rs6000_register_name);
2026   set_gdbarch_register_size (gdbarch, wordsize);
2027   set_gdbarch_register_bytes (gdbarch, off);
2028   set_gdbarch_register_byte (gdbarch, rs6000_register_byte);
2029   set_gdbarch_register_raw_size (gdbarch, rs6000_register_raw_size);
2030   set_gdbarch_max_register_raw_size (gdbarch, 8);
2031   set_gdbarch_register_virtual_size (gdbarch, rs6000_register_virtual_size);
2032   set_gdbarch_max_register_virtual_size (gdbarch, 8);
2033   set_gdbarch_register_virtual_type (gdbarch, rs6000_register_virtual_type);
2034
2035   set_gdbarch_ptr_bit (gdbarch, wordsize * TARGET_CHAR_BIT);
2036   set_gdbarch_short_bit (gdbarch, 2 * TARGET_CHAR_BIT);
2037   set_gdbarch_int_bit (gdbarch, 4 * TARGET_CHAR_BIT);
2038   set_gdbarch_long_bit (gdbarch, wordsize * TARGET_CHAR_BIT);
2039   set_gdbarch_long_long_bit (gdbarch, 8 * TARGET_CHAR_BIT);
2040   set_gdbarch_float_bit (gdbarch, 4 * TARGET_CHAR_BIT);
2041   set_gdbarch_double_bit (gdbarch, 8 * TARGET_CHAR_BIT);
2042   set_gdbarch_long_double_bit (gdbarch, 8 * TARGET_CHAR_BIT);
2043
2044   set_gdbarch_use_generic_dummy_frames (gdbarch, 1);
2045   set_gdbarch_call_dummy_length (gdbarch, 0);
2046   set_gdbarch_call_dummy_location (gdbarch, AT_ENTRY_POINT);
2047   set_gdbarch_call_dummy_address (gdbarch, entry_point_address);
2048   set_gdbarch_call_dummy_breakpoint_offset_p (gdbarch, 1);
2049   set_gdbarch_call_dummy_breakpoint_offset (gdbarch, 0);
2050   set_gdbarch_call_dummy_start_offset (gdbarch, 0);
2051   set_gdbarch_pc_in_call_dummy (gdbarch, rs6000_pc_in_call_dummy);
2052   set_gdbarch_call_dummy_p (gdbarch, 1);
2053   set_gdbarch_call_dummy_stack_adjust_p (gdbarch, 0);
2054   set_gdbarch_get_saved_register (gdbarch, generic_get_saved_register);
2055   set_gdbarch_fix_call_dummy (gdbarch, rs6000_fix_call_dummy);
2056   set_gdbarch_push_dummy_frame (gdbarch, generic_push_dummy_frame);
2057   set_gdbarch_push_return_address (gdbarch, ppc_push_return_address);
2058   set_gdbarch_believe_pcc_promotion (gdbarch, 1);
2059   set_gdbarch_coerce_float_to_double (gdbarch, rs6000_coerce_float_to_double);
2060
2061   set_gdbarch_register_convertible (gdbarch, rs6000_register_convertible);
2062   set_gdbarch_register_convert_to_virtual (gdbarch, rs6000_register_convert_to_virtual);
2063   set_gdbarch_register_convert_to_raw (gdbarch, rs6000_register_convert_to_raw);
2064
2065   set_gdbarch_extract_return_value (gdbarch, rs6000_extract_return_value);
2066   set_gdbarch_push_arguments (gdbarch, rs6000_push_arguments);
2067
2068   set_gdbarch_store_struct_return (gdbarch, rs6000_store_struct_return);
2069   set_gdbarch_store_return_value (gdbarch, rs6000_store_return_value);
2070   set_gdbarch_extract_struct_value_address (gdbarch, rs6000_extract_struct_value_address);
2071   set_gdbarch_use_struct_convention (gdbarch, generic_use_struct_convention);
2072
2073   set_gdbarch_frame_init_saved_regs (gdbarch, rs6000_frame_init_saved_regs);
2074   set_gdbarch_init_extra_frame_info (gdbarch, rs6000_init_extra_frame_info);
2075
2076   set_gdbarch_pop_frame (gdbarch, rs6000_pop_frame);
2077
2078   set_gdbarch_skip_prologue (gdbarch, rs6000_skip_prologue);
2079   set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
2080   set_gdbarch_decr_pc_after_break (gdbarch, 0);
2081   set_gdbarch_function_start_offset (gdbarch, 0);
2082   set_gdbarch_breakpoint_from_pc (gdbarch, rs6000_breakpoint_from_pc);
2083
2084   /* Not sure on this. FIXMEmgo */
2085   set_gdbarch_frame_args_skip (gdbarch, 8);
2086
2087   set_gdbarch_frameless_function_invocation (gdbarch, rs6000_frameless_function_invocation);
2088   set_gdbarch_frame_chain (gdbarch, rs6000_frame_chain);
2089   set_gdbarch_frame_chain_valid (gdbarch, file_frame_chain_valid);
2090   set_gdbarch_frame_saved_pc (gdbarch, rs6000_frame_saved_pc);
2091   set_gdbarch_frame_args_address (gdbarch, rs6000_frame_args_address);
2092   set_gdbarch_frame_locals_address (gdbarch, rs6000_frame_args_address);
2093   set_gdbarch_saved_pc_after_call (gdbarch, rs6000_saved_pc_after_call);
2094
2095   /* We can't tell how many args there are
2096      now that the C compiler delays popping them.  */
2097   set_gdbarch_frame_num_args (gdbarch, frame_num_args_unknown);
2098
2099   return gdbarch;
2100 }
2101
2102 /* Initialization code.  */
2103
2104 void
2105 _initialize_rs6000_tdep ()
2106 {
2107   register_gdbarch_init (bfd_arch_rs6000, rs6000_gdbarch_init);
2108   register_gdbarch_init (bfd_arch_powerpc, rs6000_gdbarch_init);
2109 }