* gdbarch.sh (sofun_address_maybe_missing): New gdbarch variable.
[external/binutils.git] / gdb / rs6000-tdep.c
1 /* Target-dependent code for GDB, the GNU debugger.
2
3    Copyright (C) 1986, 1987, 1989, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
4    1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007
5    Free Software Foundation, Inc.
6
7    This file is part of GDB.
8
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 3 of the License, or
12    (at your option) any later version.
13
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18
19    You should have received a copy of the GNU General Public License
20    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
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 "objfiles.h"
30 #include "arch-utils.h"
31 #include "regcache.h"
32 #include "regset.h"
33 #include "doublest.h"
34 #include "value.h"
35 #include "parser-defs.h"
36 #include "osabi.h"
37 #include "infcall.h"
38 #include "sim-regno.h"
39 #include "gdb/sim-ppc.h"
40 #include "reggroups.h"
41 #include "dwarf2-frame.h"
42 #include "target-descriptions.h"
43 #include "user-regs.h"
44
45 #include "libbfd.h"             /* for bfd_default_set_arch_mach */
46 #include "coff/internal.h"      /* for libcoff.h */
47 #include "libcoff.h"            /* for xcoff_data */
48 #include "coff/xcoff.h"
49 #include "libxcoff.h"
50
51 #include "elf-bfd.h"
52
53 #include "solib-svr4.h"
54 #include "ppc-tdep.h"
55
56 #include "gdb_assert.h"
57 #include "dis-asm.h"
58
59 #include "trad-frame.h"
60 #include "frame-unwind.h"
61 #include "frame-base.h"
62
63 #include "rs6000-tdep.h"
64
65 #include "features/rs6000/powerpc-32.c"
66 #include "features/rs6000/powerpc-403.c"
67 #include "features/rs6000/powerpc-403gc.c"
68 #include "features/rs6000/powerpc-505.c"
69 #include "features/rs6000/powerpc-601.c"
70 #include "features/rs6000/powerpc-602.c"
71 #include "features/rs6000/powerpc-603.c"
72 #include "features/rs6000/powerpc-604.c"
73 #include "features/rs6000/powerpc-64.c"
74 #include "features/rs6000/powerpc-7400.c"
75 #include "features/rs6000/powerpc-750.c"
76 #include "features/rs6000/powerpc-860.c"
77 #include "features/rs6000/powerpc-e500.c"
78 #include "features/rs6000/rs6000.c"
79
80 /* If the kernel has to deliver a signal, it pushes a sigcontext
81    structure on the stack and then calls the signal handler, passing
82    the address of the sigcontext in an argument register. Usually
83    the signal handler doesn't save this register, so we have to
84    access the sigcontext structure via an offset from the signal handler
85    frame.
86    The following constants were determined by experimentation on AIX 3.2.  */
87 #define SIG_FRAME_PC_OFFSET 96
88 #define SIG_FRAME_LR_OFFSET 108
89 #define SIG_FRAME_FP_OFFSET 284
90
91 /* To be used by skip_prologue. */
92
93 struct rs6000_framedata
94   {
95     int offset;                 /* total size of frame --- the distance
96                                    by which we decrement sp to allocate
97                                    the frame */
98     int saved_gpr;              /* smallest # of saved gpr */
99     int saved_fpr;              /* smallest # of saved fpr */
100     int saved_vr;               /* smallest # of saved vr */
101     int saved_ev;               /* smallest # of saved ev */
102     int alloca_reg;             /* alloca register number (frame ptr) */
103     char frameless;             /* true if frameless functions. */
104     char nosavedpc;             /* true if pc not saved. */
105     int gpr_offset;             /* offset of saved gprs from prev sp */
106     int fpr_offset;             /* offset of saved fprs from prev sp */
107     int vr_offset;              /* offset of saved vrs from prev sp */
108     int ev_offset;              /* offset of saved evs from prev sp */
109     int lr_offset;              /* offset of saved lr */
110     int cr_offset;              /* offset of saved cr */
111     int vrsave_offset;          /* offset of saved vrsave register */
112   };
113
114 /* Description of a single register. */
115
116 struct reg
117   {
118     char *name;                 /* name of register */
119     unsigned char sz32;         /* size on 32-bit arch, 0 if nonexistent */
120     unsigned char sz64;         /* size on 64-bit arch, 0 if nonexistent */
121     unsigned char fpr;          /* whether register is floating-point */
122     unsigned char pseudo;       /* whether register is pseudo */
123     int spr_num;                /* PowerPC SPR number, or -1 if not an SPR.
124                                    This is an ISA SPR number, not a GDB
125                                    register number.  */
126   };
127
128 /* Hook for determining the TOC address when calling functions in the
129    inferior under AIX. The initialization code in rs6000-nat.c sets
130    this hook to point to find_toc_address.  */
131
132 CORE_ADDR (*rs6000_find_toc_address_hook) (CORE_ADDR) = NULL;
133
134 /* Static function prototypes */
135
136 static CORE_ADDR branch_dest (struct frame_info *frame, int opcode,
137                               int instr, CORE_ADDR pc, CORE_ADDR safety);
138 static CORE_ADDR skip_prologue (CORE_ADDR, CORE_ADDR,
139                                 struct rs6000_framedata *);
140
141 /* Is REGNO an AltiVec register?  Return 1 if so, 0 otherwise.  */
142 int
143 altivec_register_p (int regno)
144 {
145   struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch);
146   if (tdep->ppc_vr0_regnum < 0 || tdep->ppc_vrsave_regnum < 0)
147     return 0;
148   else
149     return (regno >= tdep->ppc_vr0_regnum && regno <= tdep->ppc_vrsave_regnum);
150 }
151
152
153 /* Return true if REGNO is an SPE register, false otherwise.  */
154 int
155 spe_register_p (int regno)
156 {
157   struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch);
158   
159   /* Is it a reference to EV0 -- EV31, and do we have those?  */
160   if (tdep->ppc_ev0_regnum >= 0
161       && tdep->ppc_ev31_regnum >= 0
162       && tdep->ppc_ev0_regnum <= regno && regno <= tdep->ppc_ev31_regnum)
163     return 1;
164
165   /* Is it a reference to one of the raw upper GPR halves?  */
166   if (tdep->ppc_ev0_upper_regnum >= 0
167       && tdep->ppc_ev0_upper_regnum <= regno
168       && regno < tdep->ppc_ev0_upper_regnum + ppc_num_gprs)
169     return 1;
170
171   /* Is it a reference to the 64-bit accumulator, and do we have that?  */
172   if (tdep->ppc_acc_regnum >= 0
173       && tdep->ppc_acc_regnum == regno)
174     return 1;
175
176   /* Is it a reference to the SPE floating-point status and control register,
177      and do we have that?  */
178   if (tdep->ppc_spefscr_regnum >= 0
179       && tdep->ppc_spefscr_regnum == regno)
180     return 1;
181
182   return 0;
183 }
184
185
186 /* Return non-zero if the architecture described by GDBARCH has
187    floating-point registers (f0 --- f31 and fpscr).  */
188 int
189 ppc_floating_point_unit_p (struct gdbarch *gdbarch)
190 {
191   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
192
193   return (tdep->ppc_fp0_regnum >= 0
194           && tdep->ppc_fpscr_regnum >= 0);
195 }
196
197
198 /* Check that TABLE[GDB_REGNO] is not already initialized, and then
199    set it to SIM_REGNO.
200
201    This is a helper function for init_sim_regno_table, constructing
202    the table mapping GDB register numbers to sim register numbers; we
203    initialize every element in that table to -1 before we start
204    filling it in.  */
205 static void
206 set_sim_regno (int *table, int gdb_regno, int sim_regno)
207 {
208   /* Make sure we don't try to assign any given GDB register a sim
209      register number more than once.  */
210   gdb_assert (table[gdb_regno] == -1);
211   table[gdb_regno] = sim_regno;
212 }
213
214
215 /* Initialize ARCH->tdep->sim_regno, the table mapping GDB register
216    numbers to simulator register numbers, based on the values placed
217    in the ARCH->tdep->ppc_foo_regnum members.  */
218 static void
219 init_sim_regno_table (struct gdbarch *arch)
220 {
221   struct gdbarch_tdep *tdep = gdbarch_tdep (arch);
222   int total_regs = gdbarch_num_regs (arch);
223   int *sim_regno = GDBARCH_OBSTACK_CALLOC (arch, total_regs, int);
224   int i;
225   static const char *const segment_regs[] = {
226     "sr0", "sr1", "sr2", "sr3", "sr4", "sr5", "sr6", "sr7",
227     "sr8", "sr9", "sr10", "sr11", "sr12", "sr13", "sr14", "sr15"
228   };
229
230   /* Presume that all registers not explicitly mentioned below are
231      unavailable from the sim.  */
232   for (i = 0; i < total_regs; i++)
233     sim_regno[i] = -1;
234
235   /* General-purpose registers.  */
236   for (i = 0; i < ppc_num_gprs; i++)
237     set_sim_regno (sim_regno, tdep->ppc_gp0_regnum + i, sim_ppc_r0_regnum + i);
238   
239   /* Floating-point registers.  */
240   if (tdep->ppc_fp0_regnum >= 0)
241     for (i = 0; i < ppc_num_fprs; i++)
242       set_sim_regno (sim_regno,
243                      tdep->ppc_fp0_regnum + i,
244                      sim_ppc_f0_regnum + i);
245   if (tdep->ppc_fpscr_regnum >= 0)
246     set_sim_regno (sim_regno, tdep->ppc_fpscr_regnum, sim_ppc_fpscr_regnum);
247
248   set_sim_regno (sim_regno, gdbarch_pc_regnum (arch), sim_ppc_pc_regnum);
249   set_sim_regno (sim_regno, tdep->ppc_ps_regnum, sim_ppc_ps_regnum);
250   set_sim_regno (sim_regno, tdep->ppc_cr_regnum, sim_ppc_cr_regnum);
251
252   /* Segment registers.  */
253   for (i = 0; i < ppc_num_srs; i++)
254     {
255       int gdb_regno;
256
257       gdb_regno = user_reg_map_name_to_regnum (arch, segment_regs[i], -1);
258       if (gdb_regno >= 0)
259         set_sim_regno (sim_regno, gdb_regno, sim_ppc_sr0_regnum + i);
260     }
261
262   /* Altivec registers.  */
263   if (tdep->ppc_vr0_regnum >= 0)
264     {
265       for (i = 0; i < ppc_num_vrs; i++)
266         set_sim_regno (sim_regno,
267                        tdep->ppc_vr0_regnum + i,
268                        sim_ppc_vr0_regnum + i);
269
270       /* FIXME: jimb/2004-07-15: when we have tdep->ppc_vscr_regnum,
271          we can treat this more like the other cases.  */
272       set_sim_regno (sim_regno,
273                      tdep->ppc_vr0_regnum + ppc_num_vrs,
274                      sim_ppc_vscr_regnum);
275     }
276   /* vsave is a special-purpose register, so the code below handles it.  */
277
278   /* SPE APU (E500) registers.  */
279   if (tdep->ppc_ev0_upper_regnum >= 0)
280     for (i = 0; i < ppc_num_gprs; i++)
281       set_sim_regno (sim_regno,
282                      tdep->ppc_ev0_upper_regnum + i,
283                      sim_ppc_rh0_regnum + i);
284   if (tdep->ppc_acc_regnum >= 0)
285     set_sim_regno (sim_regno, tdep->ppc_acc_regnum, sim_ppc_acc_regnum);
286   /* spefscr is a special-purpose register, so the code below handles it.  */
287
288 #ifdef WITH_SIM
289   /* Now handle all special-purpose registers.  Verify that they
290      haven't mistakenly been assigned numbers by any of the above
291      code.  */
292   for (i = 0; i < sim_ppc_num_sprs; i++)
293     {
294       const char *spr_name = sim_spr_register_name (i);
295       int gdb_regno = -1;
296
297       if (spr_name != NULL)
298         gdb_regno = user_reg_map_name_to_regnum (arch, spr_name, -1);
299
300       if (gdb_regno != -1)
301         set_sim_regno (sim_regno, gdb_regno, sim_ppc_spr0_regnum + i);
302     }
303 #endif
304
305   /* Drop the initialized array into place.  */
306   tdep->sim_regno = sim_regno;
307 }
308
309
310 /* Given a GDB register number REG, return the corresponding SIM
311    register number.  */
312 static int
313 rs6000_register_sim_regno (int reg)
314 {
315   struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch);
316   int sim_regno;
317
318   if (tdep->sim_regno == NULL)
319     init_sim_regno_table (current_gdbarch);
320
321   gdb_assert (0 <= reg 
322               && reg <= gdbarch_num_regs (current_gdbarch)
323                         + gdbarch_num_pseudo_regs (current_gdbarch));
324   sim_regno = tdep->sim_regno[reg];
325
326   if (sim_regno >= 0)
327     return sim_regno;
328   else
329     return LEGACY_SIM_REGNO_IGNORE;
330 }
331
332 \f
333
334 /* Register set support functions.  */
335
336 /* REGS + OFFSET contains register REGNUM in a field REGSIZE wide.
337    Write the register to REGCACHE.  */
338
339 static void
340 ppc_supply_reg (struct regcache *regcache, int regnum, 
341                 const gdb_byte *regs, size_t offset, int regsize)
342 {
343   if (regnum != -1 && offset != -1)
344     {
345       if (regsize > 4)
346         {
347           struct gdbarch *gdbarch = get_regcache_arch (regcache);
348           int gdb_regsize = register_size (gdbarch, regnum);
349           if (gdb_regsize < regsize
350               && gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
351             offset += regsize - gdb_regsize;
352         }
353       regcache_raw_supply (regcache, regnum, regs + offset);
354     }
355 }
356
357 /* Read register REGNUM from REGCACHE and store to REGS + OFFSET
358    in a field REGSIZE wide.  Zero pad as necessary.  */
359
360 static void
361 ppc_collect_reg (const struct regcache *regcache, int regnum,
362                  gdb_byte *regs, size_t offset, int regsize)
363 {
364   if (regnum != -1 && offset != -1)
365     {
366       if (regsize > 4)
367         {
368           struct gdbarch *gdbarch = get_regcache_arch (regcache);
369           int gdb_regsize = register_size (gdbarch, regnum);
370           if (gdb_regsize < regsize)
371             {
372               if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
373                 {
374                   memset (regs + offset, 0, regsize - gdb_regsize);
375                   offset += regsize - gdb_regsize;
376                 }
377               else
378                 memset (regs + offset + regsize - gdb_regsize, 0,
379                         regsize - gdb_regsize);
380             }
381         }
382       regcache_raw_collect (regcache, regnum, regs + offset);
383     }
384 }
385     
386 static int
387 ppc_greg_offset (struct gdbarch *gdbarch,
388                  struct gdbarch_tdep *tdep,
389                  const struct ppc_reg_offsets *offsets,
390                  int regnum,
391                  int *regsize)
392 {
393   *regsize = offsets->gpr_size;
394   if (regnum >= tdep->ppc_gp0_regnum
395       && regnum < tdep->ppc_gp0_regnum + ppc_num_gprs)
396     return (offsets->r0_offset
397             + (regnum - tdep->ppc_gp0_regnum) * offsets->gpr_size);
398
399   if (regnum == gdbarch_pc_regnum (gdbarch))
400     return offsets->pc_offset;
401
402   if (regnum == tdep->ppc_ps_regnum)
403     return offsets->ps_offset;
404
405   if (regnum == tdep->ppc_lr_regnum)
406     return offsets->lr_offset;
407
408   if (regnum == tdep->ppc_ctr_regnum)
409     return offsets->ctr_offset;
410
411   *regsize = offsets->xr_size;
412   if (regnum == tdep->ppc_cr_regnum)
413     return offsets->cr_offset;
414
415   if (regnum == tdep->ppc_xer_regnum)
416     return offsets->xer_offset;
417
418   if (regnum == tdep->ppc_mq_regnum)
419     return offsets->mq_offset;
420
421   return -1;
422 }
423
424 static int
425 ppc_fpreg_offset (struct gdbarch_tdep *tdep,
426                   const struct ppc_reg_offsets *offsets,
427                   int regnum)
428 {
429   if (regnum >= tdep->ppc_fp0_regnum
430       && regnum < tdep->ppc_fp0_regnum + ppc_num_fprs)
431     return offsets->f0_offset + (regnum - tdep->ppc_fp0_regnum) * 8;
432
433   if (regnum == tdep->ppc_fpscr_regnum)
434     return offsets->fpscr_offset;
435
436   return -1;
437 }
438
439 /* Supply register REGNUM in the general-purpose register set REGSET
440    from the buffer specified by GREGS and LEN to register cache
441    REGCACHE.  If REGNUM is -1, do this for all registers in REGSET.  */
442
443 void
444 ppc_supply_gregset (const struct regset *regset, struct regcache *regcache,
445                     int regnum, const void *gregs, size_t len)
446 {
447   struct gdbarch *gdbarch = get_regcache_arch (regcache);
448   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
449   const struct ppc_reg_offsets *offsets = regset->descr;
450   size_t offset;
451   int regsize;
452
453   if (regnum == -1)
454     {
455       int i;
456       int gpr_size = offsets->gpr_size;
457
458       for (i = tdep->ppc_gp0_regnum, offset = offsets->r0_offset;
459            i < tdep->ppc_gp0_regnum + ppc_num_gprs;
460            i++, offset += gpr_size)
461         ppc_supply_reg (regcache, i, gregs, offset, gpr_size);
462
463       ppc_supply_reg (regcache, gdbarch_pc_regnum (gdbarch),
464                       gregs, offsets->pc_offset, gpr_size);
465       ppc_supply_reg (regcache, tdep->ppc_ps_regnum,
466                       gregs, offsets->ps_offset, gpr_size);
467       ppc_supply_reg (regcache, tdep->ppc_lr_regnum,
468                       gregs, offsets->lr_offset, gpr_size);
469       ppc_supply_reg (regcache, tdep->ppc_ctr_regnum,
470                       gregs, offsets->ctr_offset, gpr_size);
471       ppc_supply_reg (regcache, tdep->ppc_cr_regnum,
472                       gregs, offsets->cr_offset, offsets->xr_size);
473       ppc_supply_reg (regcache, tdep->ppc_xer_regnum,
474                       gregs, offsets->xer_offset, offsets->xr_size);
475       ppc_supply_reg (regcache, tdep->ppc_mq_regnum,
476                       gregs, offsets->mq_offset, offsets->xr_size);
477       return;
478     }
479
480   offset = ppc_greg_offset (gdbarch, tdep, offsets, regnum, &regsize);
481   ppc_supply_reg (regcache, regnum, gregs, offset, regsize);
482 }
483
484 /* Supply register REGNUM in the floating-point register set REGSET
485    from the buffer specified by FPREGS and LEN to register cache
486    REGCACHE.  If REGNUM is -1, do this for all registers in REGSET.  */
487
488 void
489 ppc_supply_fpregset (const struct regset *regset, struct regcache *regcache,
490                      int regnum, const void *fpregs, size_t len)
491 {
492   struct gdbarch *gdbarch = get_regcache_arch (regcache);
493   struct gdbarch_tdep *tdep;
494   const struct ppc_reg_offsets *offsets;
495   size_t offset;
496
497   if (!ppc_floating_point_unit_p (gdbarch))
498     return;
499
500   tdep = gdbarch_tdep (gdbarch);
501   offsets = regset->descr;
502   if (regnum == -1)
503     {
504       int i;
505
506       for (i = tdep->ppc_fp0_regnum, offset = offsets->f0_offset;
507            i < tdep->ppc_fp0_regnum + ppc_num_fprs;
508            i++, offset += 8)
509         ppc_supply_reg (regcache, i, fpregs, offset, 8);
510
511       ppc_supply_reg (regcache, tdep->ppc_fpscr_regnum,
512                       fpregs, offsets->fpscr_offset, offsets->fpscr_size);
513       return;
514     }
515
516   offset = ppc_fpreg_offset (tdep, offsets, regnum);
517   ppc_supply_reg (regcache, regnum, fpregs, offset,
518                   regnum == tdep->ppc_fpscr_regnum ? offsets->fpscr_size : 8);
519 }
520
521 /* Collect register REGNUM in the general-purpose register set
522    REGSET from register cache REGCACHE into the buffer specified by
523    GREGS and LEN.  If REGNUM is -1, do this for all registers in
524    REGSET.  */
525
526 void
527 ppc_collect_gregset (const struct regset *regset,
528                      const struct regcache *regcache,
529                      int regnum, void *gregs, size_t len)
530 {
531   struct gdbarch *gdbarch = get_regcache_arch (regcache);
532   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
533   const struct ppc_reg_offsets *offsets = regset->descr;
534   size_t offset;
535   int regsize;
536
537   if (regnum == -1)
538     {
539       int i;
540       int gpr_size = offsets->gpr_size;
541
542       for (i = tdep->ppc_gp0_regnum, offset = offsets->r0_offset;
543            i < tdep->ppc_gp0_regnum + ppc_num_gprs;
544            i++, offset += gpr_size)
545         ppc_collect_reg (regcache, i, gregs, offset, gpr_size);
546
547       ppc_collect_reg (regcache, gdbarch_pc_regnum (gdbarch),
548                        gregs, offsets->pc_offset, gpr_size);
549       ppc_collect_reg (regcache, tdep->ppc_ps_regnum,
550                        gregs, offsets->ps_offset, gpr_size);
551       ppc_collect_reg (regcache, tdep->ppc_lr_regnum,
552                        gregs, offsets->lr_offset, gpr_size);
553       ppc_collect_reg (regcache, tdep->ppc_ctr_regnum,
554                        gregs, offsets->ctr_offset, gpr_size);
555       ppc_collect_reg (regcache, tdep->ppc_cr_regnum,
556                        gregs, offsets->cr_offset, offsets->xr_size);
557       ppc_collect_reg (regcache, tdep->ppc_xer_regnum,
558                        gregs, offsets->xer_offset, offsets->xr_size);
559       ppc_collect_reg (regcache, tdep->ppc_mq_regnum,
560                        gregs, offsets->mq_offset, offsets->xr_size);
561       return;
562     }
563
564   offset = ppc_greg_offset (gdbarch, tdep, offsets, regnum, &regsize);
565   ppc_collect_reg (regcache, regnum, gregs, offset, regsize);
566 }
567
568 /* Collect register REGNUM in the floating-point register set
569    REGSET from register cache REGCACHE into the buffer specified by
570    FPREGS and LEN.  If REGNUM is -1, do this for all registers in
571    REGSET.  */
572
573 void
574 ppc_collect_fpregset (const struct regset *regset,
575                       const struct regcache *regcache,
576                       int regnum, void *fpregs, size_t len)
577 {
578   struct gdbarch *gdbarch = get_regcache_arch (regcache);
579   struct gdbarch_tdep *tdep;
580   const struct ppc_reg_offsets *offsets;
581   size_t offset;
582
583   if (!ppc_floating_point_unit_p (gdbarch))
584     return;
585
586   tdep = gdbarch_tdep (gdbarch);
587   offsets = regset->descr;
588   if (regnum == -1)
589     {
590       int i;
591
592       for (i = tdep->ppc_fp0_regnum, offset = offsets->f0_offset;
593            i < tdep->ppc_fp0_regnum + ppc_num_fprs;
594            i++, offset += 8)
595         ppc_collect_reg (regcache, i, fpregs, offset, 8);
596
597       ppc_collect_reg (regcache, tdep->ppc_fpscr_regnum,
598                        fpregs, offsets->fpscr_offset, offsets->fpscr_size);
599       return;
600     }
601
602   offset = ppc_fpreg_offset (tdep, offsets, regnum);
603   ppc_collect_reg (regcache, regnum, fpregs, offset,
604                    regnum == tdep->ppc_fpscr_regnum ? offsets->fpscr_size : 8);
605 }
606 \f
607
608 /* Read a LEN-byte address from debugged memory address MEMADDR. */
609
610 static CORE_ADDR
611 read_memory_addr (CORE_ADDR memaddr, int len)
612 {
613   return read_memory_unsigned_integer (memaddr, len);
614 }
615
616 static CORE_ADDR
617 rs6000_skip_prologue (CORE_ADDR pc)
618 {
619   struct rs6000_framedata frame;
620   CORE_ADDR limit_pc, func_addr;
621
622   /* See if we can determine the end of the prologue via the symbol table.
623      If so, then return either PC, or the PC after the prologue, whichever
624      is greater.  */
625   if (find_pc_partial_function (pc, NULL, &func_addr, NULL))
626     {
627       CORE_ADDR post_prologue_pc = skip_prologue_using_sal (func_addr);
628       if (post_prologue_pc != 0)
629         return max (pc, post_prologue_pc);
630     }
631
632   /* Can't determine prologue from the symbol table, need to examine
633      instructions.  */
634
635   /* Find an upper limit on the function prologue using the debug
636      information.  If the debug information could not be used to provide
637      that bound, then use an arbitrary large number as the upper bound.  */
638   limit_pc = skip_prologue_using_sal (pc);
639   if (limit_pc == 0)
640     limit_pc = pc + 100;          /* Magic.  */
641
642   pc = skip_prologue (pc, limit_pc, &frame);
643   return pc;
644 }
645
646 static int
647 insn_changes_sp_or_jumps (unsigned long insn)
648 {
649   int opcode = (insn >> 26) & 0x03f;
650   int sd = (insn >> 21) & 0x01f;
651   int a = (insn >> 16) & 0x01f;
652   int subcode = (insn >> 1) & 0x3ff;
653
654   /* Changes the stack pointer.  */
655
656   /* NOTE: There are many ways to change the value of a given register.
657            The ways below are those used when the register is R1, the SP,
658            in a funtion's epilogue.  */
659
660   if (opcode == 31 && subcode == 444 && a == 1)
661     return 1;  /* mr R1,Rn */
662   if (opcode == 14 && sd == 1)
663     return 1;  /* addi R1,Rn,simm */
664   if (opcode == 58 && sd == 1)
665     return 1;  /* ld R1,ds(Rn) */
666
667   /* Transfers control.  */
668
669   if (opcode == 18)
670     return 1;  /* b */
671   if (opcode == 16)
672     return 1;  /* bc */
673   if (opcode == 19 && subcode == 16)
674     return 1;  /* bclr */
675   if (opcode == 19 && subcode == 528)
676     return 1;  /* bcctr */
677
678   return 0;
679 }
680
681 /* Return true if we are in the function's epilogue, i.e. after the
682    instruction that destroyed the function's stack frame.
683
684    1) scan forward from the point of execution:
685        a) If you find an instruction that modifies the stack pointer
686           or transfers control (except a return), execution is not in
687           an epilogue, return.
688        b) Stop scanning if you find a return instruction or reach the
689           end of the function or reach the hard limit for the size of
690           an epilogue.
691    2) scan backward from the point of execution:
692         a) If you find an instruction that modifies the stack pointer,
693             execution *is* in an epilogue, return.
694         b) Stop scanning if you reach an instruction that transfers
695            control or the beginning of the function or reach the hard
696            limit for the size of an epilogue.  */
697
698 static int
699 rs6000_in_function_epilogue_p (struct gdbarch *gdbarch, CORE_ADDR pc)
700 {
701   bfd_byte insn_buf[PPC_INSN_SIZE];
702   CORE_ADDR scan_pc, func_start, func_end, epilogue_start, epilogue_end;
703   unsigned long insn;
704   struct frame_info *curfrm;
705
706   /* Find the search limits based on function boundaries and hard limit.  */
707
708   if (!find_pc_partial_function (pc, NULL, &func_start, &func_end))
709     return 0;
710
711   epilogue_start = pc - PPC_MAX_EPILOGUE_INSTRUCTIONS * PPC_INSN_SIZE;
712   if (epilogue_start < func_start) epilogue_start = func_start;
713
714   epilogue_end = pc + PPC_MAX_EPILOGUE_INSTRUCTIONS * PPC_INSN_SIZE;
715   if (epilogue_end > func_end) epilogue_end = func_end;
716
717   curfrm = get_current_frame ();
718
719   /* Scan forward until next 'blr'.  */
720
721   for (scan_pc = pc; scan_pc < epilogue_end; scan_pc += PPC_INSN_SIZE)
722     {
723       if (!safe_frame_unwind_memory (curfrm, scan_pc, insn_buf, PPC_INSN_SIZE))
724         return 0;
725       insn = extract_unsigned_integer (insn_buf, PPC_INSN_SIZE);
726       if (insn == 0x4e800020)
727         break;
728       if (insn_changes_sp_or_jumps (insn))
729         return 0;
730     }
731
732   /* Scan backward until adjustment to stack pointer (R1).  */
733
734   for (scan_pc = pc - PPC_INSN_SIZE;
735        scan_pc >= epilogue_start;
736        scan_pc -= PPC_INSN_SIZE)
737     {
738       if (!safe_frame_unwind_memory (curfrm, scan_pc, insn_buf, PPC_INSN_SIZE))
739         return 0;
740       insn = extract_unsigned_integer (insn_buf, PPC_INSN_SIZE);
741       if (insn_changes_sp_or_jumps (insn))
742         return 1;
743     }
744
745   return 0;
746 }
747
748 /* Get the ith function argument for the current function.  */
749 static CORE_ADDR
750 rs6000_fetch_pointer_argument (struct frame_info *frame, int argi, 
751                                struct type *type)
752 {
753   return get_frame_register_unsigned (frame, 3 + argi);
754 }
755
756 /* Calculate the destination of a branch/jump.  Return -1 if not a branch.  */
757
758 static CORE_ADDR
759 branch_dest (struct frame_info *frame, int opcode, int instr,
760              CORE_ADDR pc, CORE_ADDR safety)
761 {
762   struct gdbarch_tdep *tdep = gdbarch_tdep (get_frame_arch (frame));
763   CORE_ADDR dest;
764   int immediate;
765   int absolute;
766   int ext_op;
767
768   absolute = (int) ((instr >> 1) & 1);
769
770   switch (opcode)
771     {
772     case 18:
773       immediate = ((instr & ~3) << 6) >> 6;     /* br unconditional */
774       if (absolute)
775         dest = immediate;
776       else
777         dest = pc + immediate;
778       break;
779
780     case 16:
781       immediate = ((instr & ~3) << 16) >> 16;   /* br conditional */
782       if (absolute)
783         dest = immediate;
784       else
785         dest = pc + immediate;
786       break;
787
788     case 19:
789       ext_op = (instr >> 1) & 0x3ff;
790
791       if (ext_op == 16)         /* br conditional register */
792         {
793           dest = get_frame_register_unsigned (frame, tdep->ppc_lr_regnum) & ~3;
794
795           /* If we are about to return from a signal handler, dest is
796              something like 0x3c90.  The current frame is a signal handler
797              caller frame, upon completion of the sigreturn system call
798              execution will return to the saved PC in the frame.  */
799           if (dest < tdep->text_segment_base)
800             dest = read_memory_addr (get_frame_base (frame) + SIG_FRAME_PC_OFFSET,
801                                      tdep->wordsize);
802         }
803
804       else if (ext_op == 528)   /* br cond to count reg */
805         {
806           dest = get_frame_register_unsigned (frame, tdep->ppc_ctr_regnum) & ~3;
807
808           /* If we are about to execute a system call, dest is something
809              like 0x22fc or 0x3b00.  Upon completion the system call
810              will return to the address in the link register.  */
811           if (dest < tdep->text_segment_base)
812             dest = get_frame_register_unsigned (frame, tdep->ppc_lr_regnum) & ~3;
813         }
814       else
815         return -1;
816       break;
817
818     default:
819       return -1;
820     }
821   return (dest < tdep->text_segment_base) ? safety : dest;
822 }
823
824
825 /* Sequence of bytes for breakpoint instruction.  */
826
827 const static unsigned char *
828 rs6000_breakpoint_from_pc (CORE_ADDR *bp_addr, int *bp_size)
829 {
830   static unsigned char big_breakpoint[] = { 0x7d, 0x82, 0x10, 0x08 };
831   static unsigned char little_breakpoint[] = { 0x08, 0x10, 0x82, 0x7d };
832   *bp_size = 4;
833   if (gdbarch_byte_order (current_gdbarch) == BFD_ENDIAN_BIG)
834     return big_breakpoint;
835   else
836     return little_breakpoint;
837 }
838
839
840 /* Instruction masks used during single-stepping of atomic sequences.  */
841 #define LWARX_MASK 0xfc0007fe
842 #define LWARX_INSTRUCTION 0x7c000028
843 #define LDARX_INSTRUCTION 0x7c0000A8
844 #define STWCX_MASK 0xfc0007ff
845 #define STWCX_INSTRUCTION 0x7c00012d
846 #define STDCX_INSTRUCTION 0x7c0001ad
847 #define BC_MASK 0xfc000000
848 #define BC_INSTRUCTION 0x40000000
849
850 /* Checks for an atomic sequence of instructions beginning with a LWARX/LDARX
851    instruction and ending with a STWCX/STDCX instruction.  If such a sequence
852    is found, attempt to step through it.  A breakpoint is placed at the end of 
853    the sequence.  */
854
855 static int 
856 deal_with_atomic_sequence (struct frame_info *frame)
857 {
858   CORE_ADDR pc = get_frame_pc (frame);
859   CORE_ADDR breaks[2] = {-1, -1};
860   CORE_ADDR loc = pc;
861   CORE_ADDR branch_bp; /* Breakpoint at branch instruction's destination.  */
862   CORE_ADDR closing_insn; /* Instruction that closes the atomic sequence.  */
863   int insn = read_memory_integer (loc, PPC_INSN_SIZE);
864   int insn_count;
865   int index;
866   int last_breakpoint = 0; /* Defaults to 0 (no breakpoints placed).  */  
867   const int atomic_sequence_length = 16; /* Instruction sequence length.  */
868   int opcode; /* Branch instruction's OPcode.  */
869   int bc_insn_count = 0; /* Conditional branch instruction count.  */
870
871   /* Assume all atomic sequences start with a lwarx/ldarx instruction.  */
872   if ((insn & LWARX_MASK) != LWARX_INSTRUCTION
873       && (insn & LWARX_MASK) != LDARX_INSTRUCTION)
874     return 0;
875
876   /* Assume that no atomic sequence is longer than "atomic_sequence_length" 
877      instructions.  */
878   for (insn_count = 0; insn_count < atomic_sequence_length; ++insn_count)
879     {
880       loc += PPC_INSN_SIZE;
881       insn = read_memory_integer (loc, PPC_INSN_SIZE);
882
883       /* Assume that there is at most one conditional branch in the atomic
884          sequence.  If a conditional branch is found, put a breakpoint in 
885          its destination address.  */
886       if ((insn & BC_MASK) == BC_INSTRUCTION)
887         {
888           if (bc_insn_count >= 1)
889             return 0; /* More than one conditional branch found, fallback 
890                          to the standard single-step code.  */
891           
892           opcode = insn >> 26;
893           branch_bp = branch_dest (frame, opcode, insn, pc, breaks[0]);
894           
895           if (branch_bp != -1)
896             {
897               breaks[1] = branch_bp;
898               bc_insn_count++;
899               last_breakpoint++;
900             }
901         }
902
903       if ((insn & STWCX_MASK) == STWCX_INSTRUCTION
904           || (insn & STWCX_MASK) == STDCX_INSTRUCTION)
905         break;
906     }
907
908   /* Assume that the atomic sequence ends with a stwcx/stdcx instruction.  */
909   if ((insn & STWCX_MASK) != STWCX_INSTRUCTION
910       && (insn & STWCX_MASK) != STDCX_INSTRUCTION)
911     return 0;
912
913   closing_insn = loc;
914   loc += PPC_INSN_SIZE;
915   insn = read_memory_integer (loc, PPC_INSN_SIZE);
916
917   /* Insert a breakpoint right after the end of the atomic sequence.  */
918   breaks[0] = loc;
919
920   /* Check for duplicated breakpoints.  Check also for a breakpoint
921      placed (branch instruction's destination) at the stwcx/stdcx 
922      instruction, this resets the reservation and take us back to the 
923      lwarx/ldarx instruction at the beginning of the atomic sequence.  */
924   if (last_breakpoint && ((breaks[1] == breaks[0]) 
925       || (breaks[1] == closing_insn)))
926     last_breakpoint = 0;
927
928   /* Effectively inserts the breakpoints.  */
929   for (index = 0; index <= last_breakpoint; index++)
930     insert_single_step_breakpoint (breaks[index]);
931
932   return 1;
933 }
934
935 /* AIX does not support PT_STEP.  Simulate it.  */
936
937 int
938 rs6000_software_single_step (struct frame_info *frame)
939 {
940   CORE_ADDR dummy;
941   int breakp_sz;
942   const gdb_byte *breakp = rs6000_breakpoint_from_pc (&dummy, &breakp_sz);
943   int ii, insn;
944   CORE_ADDR loc;
945   CORE_ADDR breaks[2];
946   int opcode;
947
948   loc = get_frame_pc (frame);
949
950   insn = read_memory_integer (loc, 4);
951
952   if (deal_with_atomic_sequence (frame))
953     return 1;
954   
955   breaks[0] = loc + breakp_sz;
956   opcode = insn >> 26;
957   breaks[1] = branch_dest (frame, opcode, insn, loc, breaks[0]);
958
959   /* Don't put two breakpoints on the same address. */
960   if (breaks[1] == breaks[0])
961     breaks[1] = -1;
962
963   for (ii = 0; ii < 2; ++ii)
964     {
965       /* ignore invalid breakpoint. */
966       if (breaks[ii] == -1)
967         continue;
968       insert_single_step_breakpoint (breaks[ii]);
969     }
970
971   errno = 0;                    /* FIXME, don't ignore errors! */
972   /* What errors?  {read,write}_memory call error().  */
973   return 1;
974 }
975
976
977 #define SIGNED_SHORT(x)                                                 \
978   ((sizeof (short) == 2)                                                \
979    ? ((int)(short)(x))                                                  \
980    : ((int)((((x) & 0xffff) ^ 0x8000) - 0x8000)))
981
982 #define GET_SRC_REG(x) (((x) >> 21) & 0x1f)
983
984 /* Limit the number of skipped non-prologue instructions, as the examining
985    of the prologue is expensive.  */
986 static int max_skip_non_prologue_insns = 10;
987
988 /* Return nonzero if the given instruction OP can be part of the prologue
989    of a function and saves a parameter on the stack.  FRAMEP should be
990    set if one of the previous instructions in the function has set the
991    Frame Pointer.  */
992
993 static int
994 store_param_on_stack_p (unsigned long op, int framep, int *r0_contains_arg)
995 {
996   /* Move parameters from argument registers to temporary register.  */
997   if ((op & 0xfc0007fe) == 0x7c000378)         /* mr(.)  Rx,Ry */
998     {
999       /* Rx must be scratch register r0.  */
1000       const int rx_regno = (op >> 16) & 31;
1001       /* Ry: Only r3 - r10 are used for parameter passing.  */
1002       const int ry_regno = GET_SRC_REG (op);
1003
1004       if (rx_regno == 0 && ry_regno >= 3 && ry_regno <= 10)
1005         {
1006           *r0_contains_arg = 1;
1007           return 1;
1008         }
1009       else
1010         return 0;
1011     }
1012
1013   /* Save a General Purpose Register on stack.  */
1014
1015   if ((op & 0xfc1f0003) == 0xf8010000 ||       /* std  Rx,NUM(r1) */
1016       (op & 0xfc1f0000) == 0xd8010000)         /* stfd Rx,NUM(r1) */
1017     {
1018       /* Rx: Only r3 - r10 are used for parameter passing.  */
1019       const int rx_regno = GET_SRC_REG (op);
1020
1021       return (rx_regno >= 3 && rx_regno <= 10);
1022     }
1023            
1024   /* Save a General Purpose Register on stack via the Frame Pointer.  */
1025
1026   if (framep &&
1027       ((op & 0xfc1f0000) == 0x901f0000 ||     /* st rx,NUM(r31) */
1028        (op & 0xfc1f0000) == 0x981f0000 ||     /* stb Rx,NUM(r31) */
1029        (op & 0xfc1f0000) == 0xd81f0000))      /* stfd Rx,NUM(r31) */
1030     {
1031       /* Rx: Usually, only r3 - r10 are used for parameter passing.
1032          However, the compiler sometimes uses r0 to hold an argument.  */
1033       const int rx_regno = GET_SRC_REG (op);
1034
1035       return ((rx_regno >= 3 && rx_regno <= 10)
1036               || (rx_regno == 0 && *r0_contains_arg));
1037     }
1038
1039   if ((op & 0xfc1f0000) == 0xfc010000)         /* frsp, fp?,NUM(r1) */
1040     {
1041       /* Only f2 - f8 are used for parameter passing.  */
1042       const int src_regno = GET_SRC_REG (op);
1043
1044       return (src_regno >= 2 && src_regno <= 8);
1045     }
1046
1047   if (framep && ((op & 0xfc1f0000) == 0xfc1f0000))  /* frsp, fp?,NUM(r31) */
1048     {
1049       /* Only f2 - f8 are used for parameter passing.  */
1050       const int src_regno = GET_SRC_REG (op);
1051
1052       return (src_regno >= 2 && src_regno <= 8);
1053     }
1054
1055   /* Not an insn that saves a parameter on stack.  */
1056   return 0;
1057 }
1058
1059 /* Assuming that INSN is a "bl" instruction located at PC, return
1060    nonzero if the destination of the branch is a "blrl" instruction.
1061    
1062    This sequence is sometimes found in certain function prologues.
1063    It allows the function to load the LR register with a value that
1064    they can use to access PIC data using PC-relative offsets.  */
1065
1066 static int
1067 bl_to_blrl_insn_p (CORE_ADDR pc, int insn)
1068 {
1069   CORE_ADDR dest;
1070   int immediate;
1071   int absolute;
1072   int dest_insn;
1073
1074   absolute = (int) ((insn >> 1) & 1);
1075   immediate = ((insn & ~3) << 6) >> 6;
1076   if (absolute)
1077     dest = immediate;
1078   else
1079     dest = pc + immediate;
1080
1081   dest_insn = read_memory_integer (dest, 4);
1082   if ((dest_insn & 0xfc00ffff) == 0x4c000021) /* blrl */
1083     return 1;
1084
1085   return 0;
1086 }
1087
1088 /* return pc value after skipping a function prologue and also return
1089    information about a function frame.
1090
1091    in struct rs6000_framedata fdata:
1092    - frameless is TRUE, if function does not have a frame.
1093    - nosavedpc is TRUE, if function does not save %pc value in its frame.
1094    - offset is the initial size of this stack frame --- the amount by
1095    which we decrement the sp to allocate the frame.
1096    - saved_gpr is the number of the first saved gpr.
1097    - saved_fpr is the number of the first saved fpr.
1098    - saved_vr is the number of the first saved vr.
1099    - saved_ev is the number of the first saved ev.
1100    - alloca_reg is the number of the register used for alloca() handling.
1101    Otherwise -1.
1102    - gpr_offset is the offset of the first saved gpr from the previous frame.
1103    - fpr_offset is the offset of the first saved fpr from the previous frame.
1104    - vr_offset is the offset of the first saved vr from the previous frame.
1105    - ev_offset is the offset of the first saved ev from the previous frame.
1106    - lr_offset is the offset of the saved lr
1107    - cr_offset is the offset of the saved cr
1108    - vrsave_offset is the offset of the saved vrsave register
1109  */
1110
1111 static CORE_ADDR
1112 skip_prologue (CORE_ADDR pc, CORE_ADDR lim_pc, struct rs6000_framedata *fdata)
1113 {
1114   CORE_ADDR orig_pc = pc;
1115   CORE_ADDR last_prologue_pc = pc;
1116   CORE_ADDR li_found_pc = 0;
1117   gdb_byte buf[4];
1118   unsigned long op;
1119   long offset = 0;
1120   long vr_saved_offset = 0;
1121   int lr_reg = -1;
1122   int cr_reg = -1;
1123   int vr_reg = -1;
1124   int ev_reg = -1;
1125   long ev_offset = 0;
1126   int vrsave_reg = -1;
1127   int reg;
1128   int framep = 0;
1129   int minimal_toc_loaded = 0;
1130   int prev_insn_was_prologue_insn = 1;
1131   int num_skip_non_prologue_insns = 0;
1132   int r0_contains_arg = 0;
1133   const struct bfd_arch_info *arch_info = gdbarch_bfd_arch_info (current_gdbarch);
1134   struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch);
1135
1136   memset (fdata, 0, sizeof (struct rs6000_framedata));
1137   fdata->saved_gpr = -1;
1138   fdata->saved_fpr = -1;
1139   fdata->saved_vr = -1;
1140   fdata->saved_ev = -1;
1141   fdata->alloca_reg = -1;
1142   fdata->frameless = 1;
1143   fdata->nosavedpc = 1;
1144
1145   for (;; pc += 4)
1146     {
1147       /* Sometimes it isn't clear if an instruction is a prologue
1148          instruction or not.  When we encounter one of these ambiguous
1149          cases, we'll set prev_insn_was_prologue_insn to 0 (false).
1150          Otherwise, we'll assume that it really is a prologue instruction. */
1151       if (prev_insn_was_prologue_insn)
1152         last_prologue_pc = pc;
1153
1154       /* Stop scanning if we've hit the limit.  */
1155       if (pc >= lim_pc)
1156         break;
1157
1158       prev_insn_was_prologue_insn = 1;
1159
1160       /* Fetch the instruction and convert it to an integer.  */
1161       if (target_read_memory (pc, buf, 4))
1162         break;
1163       op = extract_unsigned_integer (buf, 4);
1164
1165       if ((op & 0xfc1fffff) == 0x7c0802a6)
1166         {                       /* mflr Rx */
1167           /* Since shared library / PIC code, which needs to get its
1168              address at runtime, can appear to save more than one link
1169              register vis:
1170
1171              *INDENT-OFF*
1172              stwu r1,-304(r1)
1173              mflr r3
1174              bl 0xff570d0 (blrl)
1175              stw r30,296(r1)
1176              mflr r30
1177              stw r31,300(r1)
1178              stw r3,308(r1);
1179              ...
1180              *INDENT-ON*
1181
1182              remember just the first one, but skip over additional
1183              ones.  */
1184           if (lr_reg == -1)
1185             lr_reg = (op & 0x03e00000);
1186           if (lr_reg == 0)
1187             r0_contains_arg = 0;
1188           continue;
1189         }
1190       else if ((op & 0xfc1fffff) == 0x7c000026)
1191         {                       /* mfcr Rx */
1192           cr_reg = (op & 0x03e00000);
1193           if (cr_reg == 0)
1194             r0_contains_arg = 0;
1195           continue;
1196
1197         }
1198       else if ((op & 0xfc1f0000) == 0xd8010000)
1199         {                       /* stfd Rx,NUM(r1) */
1200           reg = GET_SRC_REG (op);
1201           if (fdata->saved_fpr == -1 || fdata->saved_fpr > reg)
1202             {
1203               fdata->saved_fpr = reg;
1204               fdata->fpr_offset = SIGNED_SHORT (op) + offset;
1205             }
1206           continue;
1207
1208         }
1209       else if (((op & 0xfc1f0000) == 0xbc010000) ||     /* stm Rx, NUM(r1) */
1210                (((op & 0xfc1f0000) == 0x90010000 ||     /* st rx,NUM(r1) */
1211                  (op & 0xfc1f0003) == 0xf8010000) &&    /* std rx,NUM(r1) */
1212                 (op & 0x03e00000) >= 0x01a00000))       /* rx >= r13 */
1213         {
1214
1215           reg = GET_SRC_REG (op);
1216           if (fdata->saved_gpr == -1 || fdata->saved_gpr > reg)
1217             {
1218               fdata->saved_gpr = reg;
1219               if ((op & 0xfc1f0003) == 0xf8010000)
1220                 op &= ~3UL;
1221               fdata->gpr_offset = SIGNED_SHORT (op) + offset;
1222             }
1223           continue;
1224
1225         }
1226       else if ((op & 0xffff0000) == 0x60000000)
1227         {
1228           /* nop */
1229           /* Allow nops in the prologue, but do not consider them to
1230              be part of the prologue unless followed by other prologue
1231              instructions. */
1232           prev_insn_was_prologue_insn = 0;
1233           continue;
1234
1235         }
1236       else if ((op & 0xffff0000) == 0x3c000000)
1237         {                       /* addis 0,0,NUM, used
1238                                    for >= 32k frames */
1239           fdata->offset = (op & 0x0000ffff) << 16;
1240           fdata->frameless = 0;
1241           r0_contains_arg = 0;
1242           continue;
1243
1244         }
1245       else if ((op & 0xffff0000) == 0x60000000)
1246         {                       /* ori 0,0,NUM, 2nd ha
1247                                    lf of >= 32k frames */
1248           fdata->offset |= (op & 0x0000ffff);
1249           fdata->frameless = 0;
1250           r0_contains_arg = 0;
1251           continue;
1252
1253         }
1254       else if (lr_reg >= 0 &&
1255                /* std Rx, NUM(r1) || stdu Rx, NUM(r1) */
1256                (((op & 0xffff0000) == (lr_reg | 0xf8010000)) ||
1257                 /* stw Rx, NUM(r1) */
1258                 ((op & 0xffff0000) == (lr_reg | 0x90010000)) ||
1259                 /* stwu Rx, NUM(r1) */
1260                 ((op & 0xffff0000) == (lr_reg | 0x94010000))))
1261         {       /* where Rx == lr */
1262           fdata->lr_offset = offset;
1263           fdata->nosavedpc = 0;
1264           /* Invalidate lr_reg, but don't set it to -1.
1265              That would mean that it had never been set.  */
1266           lr_reg = -2;
1267           if ((op & 0xfc000003) == 0xf8000000 ||        /* std */
1268               (op & 0xfc000000) == 0x90000000)          /* stw */
1269             {
1270               /* Does not update r1, so add displacement to lr_offset.  */
1271               fdata->lr_offset += SIGNED_SHORT (op);
1272             }
1273           continue;
1274
1275         }
1276       else if (cr_reg >= 0 &&
1277                /* std Rx, NUM(r1) || stdu Rx, NUM(r1) */
1278                (((op & 0xffff0000) == (cr_reg | 0xf8010000)) ||
1279                 /* stw Rx, NUM(r1) */
1280                 ((op & 0xffff0000) == (cr_reg | 0x90010000)) ||
1281                 /* stwu Rx, NUM(r1) */
1282                 ((op & 0xffff0000) == (cr_reg | 0x94010000))))
1283         {       /* where Rx == cr */
1284           fdata->cr_offset = offset;
1285           /* Invalidate cr_reg, but don't set it to -1.
1286              That would mean that it had never been set.  */
1287           cr_reg = -2;
1288           if ((op & 0xfc000003) == 0xf8000000 ||
1289               (op & 0xfc000000) == 0x90000000)
1290             {
1291               /* Does not update r1, so add displacement to cr_offset.  */
1292               fdata->cr_offset += SIGNED_SHORT (op);
1293             }
1294           continue;
1295
1296         }
1297       else if ((op & 0xfe80ffff) == 0x42800005 && lr_reg != -1)
1298         {
1299           /* bcl 20,xx,.+4 is used to get the current PC, with or without
1300              prediction bits.  If the LR has already been saved, we can
1301              skip it.  */
1302           continue;
1303         }
1304       else if (op == 0x48000005)
1305         {                       /* bl .+4 used in 
1306                                    -mrelocatable */
1307           continue;
1308
1309         }
1310       else if (op == 0x48000004)
1311         {                       /* b .+4 (xlc) */
1312           break;
1313
1314         }
1315       else if ((op & 0xffff0000) == 0x3fc00000 ||  /* addis 30,0,foo@ha, used
1316                                                       in V.4 -mminimal-toc */
1317                (op & 0xffff0000) == 0x3bde0000)
1318         {                       /* addi 30,30,foo@l */
1319           continue;
1320
1321         }
1322       else if ((op & 0xfc000001) == 0x48000001)
1323         {                       /* bl foo, 
1324                                    to save fprs??? */
1325
1326           fdata->frameless = 0;
1327
1328           /* If the return address has already been saved, we can skip
1329              calls to blrl (for PIC).  */
1330           if (lr_reg != -1 && bl_to_blrl_insn_p (pc, op))
1331             continue;
1332
1333           /* Don't skip over the subroutine call if it is not within
1334              the first three instructions of the prologue and either
1335              we have no line table information or the line info tells
1336              us that the subroutine call is not part of the line
1337              associated with the prologue.  */
1338           if ((pc - orig_pc) > 8)
1339             {
1340               struct symtab_and_line prologue_sal = find_pc_line (orig_pc, 0);
1341               struct symtab_and_line this_sal = find_pc_line (pc, 0);
1342
1343               if ((prologue_sal.line == 0) || (prologue_sal.line != this_sal.line))
1344                 break;
1345             }
1346
1347           op = read_memory_integer (pc + 4, 4);
1348
1349           /* At this point, make sure this is not a trampoline
1350              function (a function that simply calls another functions,
1351              and nothing else).  If the next is not a nop, this branch
1352              was part of the function prologue. */
1353
1354           if (op == 0x4def7b82 || op == 0)      /* crorc 15, 15, 15 */
1355             break;              /* don't skip over 
1356                                    this branch */
1357           continue;
1358
1359         }
1360       /* update stack pointer */
1361       else if ((op & 0xfc1f0000) == 0x94010000)
1362         {               /* stu rX,NUM(r1) ||  stwu rX,NUM(r1) */
1363           fdata->frameless = 0;
1364           fdata->offset = SIGNED_SHORT (op);
1365           offset = fdata->offset;
1366           continue;
1367         }
1368       else if ((op & 0xfc1f016a) == 0x7c01016e)
1369         {                       /* stwux rX,r1,rY */
1370           /* no way to figure out what r1 is going to be */
1371           fdata->frameless = 0;
1372           offset = fdata->offset;
1373           continue;
1374         }
1375       else if ((op & 0xfc1f0003) == 0xf8010001)
1376         {                       /* stdu rX,NUM(r1) */
1377           fdata->frameless = 0;
1378           fdata->offset = SIGNED_SHORT (op & ~3UL);
1379           offset = fdata->offset;
1380           continue;
1381         }
1382       else if ((op & 0xfc1f016a) == 0x7c01016a)
1383         {                       /* stdux rX,r1,rY */
1384           /* no way to figure out what r1 is going to be */
1385           fdata->frameless = 0;
1386           offset = fdata->offset;
1387           continue;
1388         }
1389       else if ((op & 0xffff0000) == 0x38210000)
1390         {                       /* addi r1,r1,SIMM */
1391           fdata->frameless = 0;
1392           fdata->offset += SIGNED_SHORT (op);
1393           offset = fdata->offset;
1394           continue;
1395         }
1396       /* Load up minimal toc pointer.  Do not treat an epilogue restore
1397          of r31 as a minimal TOC load.  */
1398       else if (((op >> 22) == 0x20f     ||      /* l r31,... or l r30,... */
1399                (op >> 22) == 0x3af)             /* ld r31,... or ld r30,... */
1400                && !framep
1401                && !minimal_toc_loaded)
1402         {
1403           minimal_toc_loaded = 1;
1404           continue;
1405
1406           /* move parameters from argument registers to local variable
1407              registers */
1408         }
1409       else if ((op & 0xfc0007fe) == 0x7c000378 &&       /* mr(.)  Rx,Ry */
1410                (((op >> 21) & 31) >= 3) &&              /* R3 >= Ry >= R10 */
1411                (((op >> 21) & 31) <= 10) &&
1412                ((long) ((op >> 16) & 31) >= fdata->saved_gpr)) /* Rx: local var reg */
1413         {
1414           continue;
1415
1416           /* store parameters in stack */
1417         }
1418       /* Move parameters from argument registers to temporary register.  */
1419       else if (store_param_on_stack_p (op, framep, &r0_contains_arg))
1420         {
1421           continue;
1422
1423           /* Set up frame pointer */
1424         }
1425       else if (op == 0x603f0000 /* oril r31, r1, 0x0 */
1426                || op == 0x7c3f0b78)
1427         {                       /* mr r31, r1 */
1428           fdata->frameless = 0;
1429           framep = 1;
1430           fdata->alloca_reg = (tdep->ppc_gp0_regnum + 31);
1431           continue;
1432
1433           /* Another way to set up the frame pointer.  */
1434         }
1435       else if ((op & 0xfc1fffff) == 0x38010000)
1436         {                       /* addi rX, r1, 0x0 */
1437           fdata->frameless = 0;
1438           framep = 1;
1439           fdata->alloca_reg = (tdep->ppc_gp0_regnum
1440                                + ((op & ~0x38010000) >> 21));
1441           continue;
1442         }
1443       /* AltiVec related instructions.  */
1444       /* Store the vrsave register (spr 256) in another register for
1445          later manipulation, or load a register into the vrsave
1446          register.  2 instructions are used: mfvrsave and
1447          mtvrsave.  They are shorthand notation for mfspr Rn, SPR256
1448          and mtspr SPR256, Rn.  */
1449       /* mfspr Rn SPR256 == 011111 nnnnn 0000001000 01010100110
1450          mtspr SPR256 Rn == 011111 nnnnn 0000001000 01110100110  */
1451       else if ((op & 0xfc1fffff) == 0x7c0042a6)    /* mfvrsave Rn */
1452         {
1453           vrsave_reg = GET_SRC_REG (op);
1454           continue;
1455         }
1456       else if ((op & 0xfc1fffff) == 0x7c0043a6)     /* mtvrsave Rn */
1457         {
1458           continue;
1459         }
1460       /* Store the register where vrsave was saved to onto the stack:
1461          rS is the register where vrsave was stored in a previous
1462          instruction.  */
1463       /* 100100 sssss 00001 dddddddd dddddddd */
1464       else if ((op & 0xfc1f0000) == 0x90010000)     /* stw rS, d(r1) */
1465         {
1466           if (vrsave_reg == GET_SRC_REG (op))
1467             {
1468               fdata->vrsave_offset = SIGNED_SHORT (op) + offset;
1469               vrsave_reg = -1;
1470             }
1471           continue;
1472         }
1473       /* Compute the new value of vrsave, by modifying the register
1474          where vrsave was saved to.  */
1475       else if (((op & 0xfc000000) == 0x64000000)    /* oris Ra, Rs, UIMM */
1476                || ((op & 0xfc000000) == 0x60000000))/* ori Ra, Rs, UIMM */
1477         {
1478           continue;
1479         }
1480       /* li r0, SIMM (short for addi r0, 0, SIMM).  This is the first
1481          in a pair of insns to save the vector registers on the
1482          stack.  */
1483       /* 001110 00000 00000 iiii iiii iiii iiii  */
1484       /* 001110 01110 00000 iiii iiii iiii iiii  */
1485       else if ((op & 0xffff0000) == 0x38000000         /* li r0, SIMM */
1486                || (op & 0xffff0000) == 0x39c00000)     /* li r14, SIMM */
1487         {
1488           if ((op & 0xffff0000) == 0x38000000)
1489             r0_contains_arg = 0;
1490           li_found_pc = pc;
1491           vr_saved_offset = SIGNED_SHORT (op);
1492
1493           /* This insn by itself is not part of the prologue, unless
1494              if part of the pair of insns mentioned above. So do not
1495              record this insn as part of the prologue yet.  */
1496           prev_insn_was_prologue_insn = 0;
1497         }
1498       /* Store vector register S at (r31+r0) aligned to 16 bytes.  */      
1499       /* 011111 sssss 11111 00000 00111001110 */
1500       else if ((op & 0xfc1fffff) == 0x7c1f01ce)   /* stvx Vs, R31, R0 */
1501         {
1502           if (pc == (li_found_pc + 4))
1503             {
1504               vr_reg = GET_SRC_REG (op);
1505               /* If this is the first vector reg to be saved, or if
1506                  it has a lower number than others previously seen,
1507                  reupdate the frame info.  */
1508               if (fdata->saved_vr == -1 || fdata->saved_vr > vr_reg)
1509                 {
1510                   fdata->saved_vr = vr_reg;
1511                   fdata->vr_offset = vr_saved_offset + offset;
1512                 }
1513               vr_saved_offset = -1;
1514               vr_reg = -1;
1515               li_found_pc = 0;
1516             }
1517         }
1518       /* End AltiVec related instructions.  */
1519
1520       /* Start BookE related instructions.  */
1521       /* Store gen register S at (r31+uimm).
1522          Any register less than r13 is volatile, so we don't care.  */
1523       /* 000100 sssss 11111 iiiii 01100100001 */
1524       else if (arch_info->mach == bfd_mach_ppc_e500
1525                && (op & 0xfc1f07ff) == 0x101f0321)    /* evstdd Rs,uimm(R31) */
1526         {
1527           if ((op & 0x03e00000) >= 0x01a00000)  /* Rs >= r13 */
1528             {
1529               unsigned int imm;
1530               ev_reg = GET_SRC_REG (op);
1531               imm = (op >> 11) & 0x1f;
1532               ev_offset = imm * 8;
1533               /* If this is the first vector reg to be saved, or if
1534                  it has a lower number than others previously seen,
1535                  reupdate the frame info.  */
1536               if (fdata->saved_ev == -1 || fdata->saved_ev > ev_reg)
1537                 {
1538                   fdata->saved_ev = ev_reg;
1539                   fdata->ev_offset = ev_offset + offset;
1540                 }
1541             }
1542           continue;
1543         }
1544       /* Store gen register rS at (r1+rB).  */
1545       /* 000100 sssss 00001 bbbbb 01100100000 */
1546       else if (arch_info->mach == bfd_mach_ppc_e500
1547                && (op & 0xffe007ff) == 0x13e00320)     /* evstddx RS,R1,Rb */
1548         {
1549           if (pc == (li_found_pc + 4))
1550             {
1551               ev_reg = GET_SRC_REG (op);
1552               /* If this is the first vector reg to be saved, or if
1553                  it has a lower number than others previously seen,
1554                  reupdate the frame info.  */
1555               /* We know the contents of rB from the previous instruction.  */
1556               if (fdata->saved_ev == -1 || fdata->saved_ev > ev_reg)
1557                 {
1558                   fdata->saved_ev = ev_reg;
1559                   fdata->ev_offset = vr_saved_offset + offset;
1560                 }
1561               vr_saved_offset = -1;
1562               ev_reg = -1;
1563               li_found_pc = 0;
1564             }
1565           continue;
1566         }
1567       /* Store gen register r31 at (rA+uimm).  */
1568       /* 000100 11111 aaaaa iiiii 01100100001 */
1569       else if (arch_info->mach == bfd_mach_ppc_e500
1570                && (op & 0xffe007ff) == 0x13e00321)   /* evstdd R31,Ra,UIMM */
1571         {
1572           /* Wwe know that the source register is 31 already, but
1573              it can't hurt to compute it.  */
1574           ev_reg = GET_SRC_REG (op);
1575           ev_offset = ((op >> 11) & 0x1f) * 8;
1576           /* If this is the first vector reg to be saved, or if
1577              it has a lower number than others previously seen,
1578              reupdate the frame info.  */
1579           if (fdata->saved_ev == -1 || fdata->saved_ev > ev_reg)
1580             {
1581               fdata->saved_ev = ev_reg;
1582               fdata->ev_offset = ev_offset + offset;
1583             }
1584
1585           continue;
1586         }
1587       /* Store gen register S at (r31+r0).
1588          Store param on stack when offset from SP bigger than 4 bytes.  */
1589       /* 000100 sssss 11111 00000 01100100000 */
1590       else if (arch_info->mach == bfd_mach_ppc_e500
1591                && (op & 0xfc1fffff) == 0x101f0320)     /* evstddx Rs,R31,R0 */
1592         {
1593           if (pc == (li_found_pc + 4))
1594             {
1595               if ((op & 0x03e00000) >= 0x01a00000)
1596                 {
1597                   ev_reg = GET_SRC_REG (op);
1598                   /* If this is the first vector reg to be saved, or if
1599                      it has a lower number than others previously seen,
1600                      reupdate the frame info.  */
1601                   /* We know the contents of r0 from the previous
1602                      instruction.  */
1603                   if (fdata->saved_ev == -1 || fdata->saved_ev > ev_reg)
1604                     {
1605                       fdata->saved_ev = ev_reg;
1606                       fdata->ev_offset = vr_saved_offset + offset;
1607                     }
1608                   ev_reg = -1;
1609                 }
1610               vr_saved_offset = -1;
1611               li_found_pc = 0;
1612               continue;
1613             }
1614         }
1615       /* End BookE related instructions.  */
1616
1617       else
1618         {
1619           /* Not a recognized prologue instruction.
1620              Handle optimizer code motions into the prologue by continuing
1621              the search if we have no valid frame yet or if the return
1622              address is not yet saved in the frame.  */
1623           if (fdata->frameless == 0 && fdata->nosavedpc == 0)
1624             break;
1625
1626           if (op == 0x4e800020          /* blr */
1627               || op == 0x4e800420)      /* bctr */
1628             /* Do not scan past epilogue in frameless functions or
1629                trampolines.  */
1630             break;
1631           if ((op & 0xf4000000) == 0x40000000) /* bxx */
1632             /* Never skip branches.  */
1633             break;
1634
1635           if (num_skip_non_prologue_insns++ > max_skip_non_prologue_insns)
1636             /* Do not scan too many insns, scanning insns is expensive with
1637                remote targets.  */
1638             break;
1639
1640           /* Continue scanning.  */
1641           prev_insn_was_prologue_insn = 0;
1642           continue;
1643         }
1644     }
1645
1646 #if 0
1647 /* I have problems with skipping over __main() that I need to address
1648  * sometime. Previously, I used to use misc_function_vector which
1649  * didn't work as well as I wanted to be.  -MGO */
1650
1651   /* If the first thing after skipping a prolog is a branch to a function,
1652      this might be a call to an initializer in main(), introduced by gcc2.
1653      We'd like to skip over it as well.  Fortunately, xlc does some extra
1654      work before calling a function right after a prologue, thus we can
1655      single out such gcc2 behaviour.  */
1656
1657
1658   if ((op & 0xfc000001) == 0x48000001)
1659     {                           /* bl foo, an initializer function? */
1660       op = read_memory_integer (pc + 4, 4);
1661
1662       if (op == 0x4def7b82)
1663         {                       /* cror 0xf, 0xf, 0xf (nop) */
1664
1665           /* Check and see if we are in main.  If so, skip over this
1666              initializer function as well.  */
1667
1668           tmp = find_pc_misc_function (pc);
1669           if (tmp >= 0
1670               && strcmp (misc_function_vector[tmp].name, main_name ()) == 0)
1671             return pc + 8;
1672         }
1673     }
1674 #endif /* 0 */
1675
1676   fdata->offset = -fdata->offset;
1677   return last_prologue_pc;
1678 }
1679
1680
1681 /*************************************************************************
1682   Support for creating pushing a dummy frame into the stack, and popping
1683   frames, etc. 
1684 *************************************************************************/
1685
1686
1687 /* All the ABI's require 16 byte alignment.  */
1688 static CORE_ADDR
1689 rs6000_frame_align (struct gdbarch *gdbarch, CORE_ADDR addr)
1690 {
1691   return (addr & -16);
1692 }
1693
1694 /* Pass the arguments in either registers, or in the stack. In RS/6000,
1695    the first eight words of the argument list (that might be less than
1696    eight parameters if some parameters occupy more than one word) are
1697    passed in r3..r10 registers.  float and double parameters are
1698    passed in fpr's, in addition to that.  Rest of the parameters if any
1699    are passed in user stack.  There might be cases in which half of the
1700    parameter is copied into registers, the other half is pushed into
1701    stack.
1702
1703    Stack must be aligned on 64-bit boundaries when synthesizing
1704    function calls.
1705
1706    If the function is returning a structure, then the return address is passed
1707    in r3, then the first 7 words of the parameters can be passed in registers,
1708    starting from r4.  */
1709
1710 static CORE_ADDR
1711 rs6000_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
1712                         struct regcache *regcache, CORE_ADDR bp_addr,
1713                         int nargs, struct value **args, CORE_ADDR sp,
1714                         int struct_return, CORE_ADDR struct_addr)
1715 {
1716   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1717   int ii;
1718   int len = 0;
1719   int argno;                    /* current argument number */
1720   int argbytes;                 /* current argument byte */
1721   gdb_byte tmp_buffer[50];
1722   int f_argno = 0;              /* current floating point argno */
1723   int wordsize = gdbarch_tdep (gdbarch)->wordsize;
1724   CORE_ADDR func_addr = find_function_addr (function, NULL);
1725
1726   struct value *arg = 0;
1727   struct type *type;
1728
1729   ULONGEST saved_sp;
1730
1731   /* The calling convention this function implements assumes the
1732      processor has floating-point registers.  We shouldn't be using it
1733      on PPC variants that lack them.  */
1734   gdb_assert (ppc_floating_point_unit_p (gdbarch));
1735
1736   /* The first eight words of ther arguments are passed in registers.
1737      Copy them appropriately.  */
1738   ii = 0;
1739
1740   /* If the function is returning a `struct', then the first word
1741      (which will be passed in r3) is used for struct return address.
1742      In that case we should advance one word and start from r4
1743      register to copy parameters.  */
1744   if (struct_return)
1745     {
1746       regcache_raw_write_unsigned (regcache, tdep->ppc_gp0_regnum + 3,
1747                                    struct_addr);
1748       ii++;
1749     }
1750
1751 /* 
1752    effectively indirect call... gcc does...
1753
1754    return_val example( float, int);
1755
1756    eabi: 
1757    float in fp0, int in r3
1758    offset of stack on overflow 8/16
1759    for varargs, must go by type.
1760    power open:
1761    float in r3&r4, int in r5
1762    offset of stack on overflow different 
1763    both: 
1764    return in r3 or f0.  If no float, must study how gcc emulates floats;
1765    pay attention to arg promotion.  
1766    User may have to cast\args to handle promotion correctly 
1767    since gdb won't know if prototype supplied or not.
1768  */
1769
1770   for (argno = 0, argbytes = 0; argno < nargs && ii < 8; ++ii)
1771     {
1772       int reg_size = register_size (gdbarch, ii + 3);
1773
1774       arg = args[argno];
1775       type = check_typedef (value_type (arg));
1776       len = TYPE_LENGTH (type);
1777
1778       if (TYPE_CODE (type) == TYPE_CODE_FLT)
1779         {
1780
1781           /* Floating point arguments are passed in fpr's, as well as gpr's.
1782              There are 13 fpr's reserved for passing parameters. At this point
1783              there is no way we would run out of them.  */
1784
1785           gdb_assert (len <= 8);
1786
1787           regcache_cooked_write (regcache,
1788                                  tdep->ppc_fp0_regnum + 1 + f_argno,
1789                                  value_contents (arg));
1790           ++f_argno;
1791         }
1792
1793       if (len > reg_size)
1794         {
1795
1796           /* Argument takes more than one register.  */
1797           while (argbytes < len)
1798             {
1799               gdb_byte word[MAX_REGISTER_SIZE];
1800               memset (word, 0, reg_size);
1801               memcpy (word,
1802                       ((char *) value_contents (arg)) + argbytes,
1803                       (len - argbytes) > reg_size
1804                         ? reg_size : len - argbytes);
1805               regcache_cooked_write (regcache,
1806                                     tdep->ppc_gp0_regnum + 3 + ii,
1807                                     word);
1808               ++ii, argbytes += reg_size;
1809
1810               if (ii >= 8)
1811                 goto ran_out_of_registers_for_arguments;
1812             }
1813           argbytes = 0;
1814           --ii;
1815         }
1816       else
1817         {
1818           /* Argument can fit in one register.  No problem.  */
1819           int adj = gdbarch_byte_order (gdbarch)
1820                     == BFD_ENDIAN_BIG ? reg_size - len : 0;
1821           gdb_byte word[MAX_REGISTER_SIZE];
1822
1823           memset (word, 0, reg_size);
1824           memcpy (word, value_contents (arg), len);
1825           regcache_cooked_write (regcache, tdep->ppc_gp0_regnum + 3 +ii, word);
1826         }
1827       ++argno;
1828     }
1829
1830 ran_out_of_registers_for_arguments:
1831
1832   regcache_cooked_read_unsigned (regcache,
1833                                  gdbarch_sp_regnum (gdbarch),
1834                                  &saved_sp);
1835
1836   /* Location for 8 parameters are always reserved.  */
1837   sp -= wordsize * 8;
1838
1839   /* Another six words for back chain, TOC register, link register, etc.  */
1840   sp -= wordsize * 6;
1841
1842   /* Stack pointer must be quadword aligned.  */
1843   sp &= -16;
1844
1845   /* If there are more arguments, allocate space for them in 
1846      the stack, then push them starting from the ninth one.  */
1847
1848   if ((argno < nargs) || argbytes)
1849     {
1850       int space = 0, jj;
1851
1852       if (argbytes)
1853         {
1854           space += ((len - argbytes + 3) & -4);
1855           jj = argno + 1;
1856         }
1857       else
1858         jj = argno;
1859
1860       for (; jj < nargs; ++jj)
1861         {
1862           struct value *val = args[jj];
1863           space += ((TYPE_LENGTH (value_type (val))) + 3) & -4;
1864         }
1865
1866       /* Add location required for the rest of the parameters.  */
1867       space = (space + 15) & -16;
1868       sp -= space;
1869
1870       /* This is another instance we need to be concerned about
1871          securing our stack space. If we write anything underneath %sp
1872          (r1), we might conflict with the kernel who thinks he is free
1873          to use this area.  So, update %sp first before doing anything
1874          else.  */
1875
1876       regcache_raw_write_signed (regcache,
1877                                  gdbarch_sp_regnum (gdbarch), sp);
1878
1879       /* If the last argument copied into the registers didn't fit there 
1880          completely, push the rest of it into stack.  */
1881
1882       if (argbytes)
1883         {
1884           write_memory (sp + 24 + (ii * 4),
1885                         value_contents (arg) + argbytes,
1886                         len - argbytes);
1887           ++argno;
1888           ii += ((len - argbytes + 3) & -4) / 4;
1889         }
1890
1891       /* Push the rest of the arguments into stack.  */
1892       for (; argno < nargs; ++argno)
1893         {
1894
1895           arg = args[argno];
1896           type = check_typedef (value_type (arg));
1897           len = TYPE_LENGTH (type);
1898
1899
1900           /* Float types should be passed in fpr's, as well as in the
1901              stack.  */
1902           if (TYPE_CODE (type) == TYPE_CODE_FLT && f_argno < 13)
1903             {
1904
1905               gdb_assert (len <= 8);
1906
1907               regcache_cooked_write (regcache,
1908                                      tdep->ppc_fp0_regnum + 1 + f_argno,
1909                                      value_contents (arg));
1910               ++f_argno;
1911             }
1912
1913           write_memory (sp + 24 + (ii * 4), value_contents (arg), len);
1914           ii += ((len + 3) & -4) / 4;
1915         }
1916     }
1917
1918   /* Set the stack pointer.  According to the ABI, the SP is meant to
1919      be set _before_ the corresponding stack space is used.  On AIX,
1920      this even applies when the target has been completely stopped!
1921      Not doing this can lead to conflicts with the kernel which thinks
1922      that it still has control over this not-yet-allocated stack
1923      region.  */
1924   regcache_raw_write_signed (regcache, gdbarch_sp_regnum (gdbarch), sp);
1925
1926   /* Set back chain properly.  */
1927   store_unsigned_integer (tmp_buffer, wordsize, saved_sp);
1928   write_memory (sp, tmp_buffer, wordsize);
1929
1930   /* Point the inferior function call's return address at the dummy's
1931      breakpoint.  */
1932   regcache_raw_write_signed (regcache, tdep->ppc_lr_regnum, bp_addr);
1933
1934   /* Set the TOC register, get the value from the objfile reader
1935      which, in turn, gets it from the VMAP table.  */
1936   if (rs6000_find_toc_address_hook != NULL)
1937     {
1938       CORE_ADDR tocvalue = (*rs6000_find_toc_address_hook) (func_addr);
1939       regcache_raw_write_signed (regcache, tdep->ppc_toc_regnum, tocvalue);
1940     }
1941
1942   target_store_registers (regcache, -1);
1943   return sp;
1944 }
1945
1946 static enum return_value_convention
1947 rs6000_return_value (struct gdbarch *gdbarch, struct type *valtype,
1948                      struct regcache *regcache, gdb_byte *readbuf,
1949                      const gdb_byte *writebuf)
1950 {
1951   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1952   gdb_byte buf[8];
1953
1954   /* The calling convention this function implements assumes the
1955      processor has floating-point registers.  We shouldn't be using it
1956      on PowerPC variants that lack them.  */
1957   gdb_assert (ppc_floating_point_unit_p (gdbarch));
1958
1959   /* AltiVec extension: Functions that declare a vector data type as a
1960      return value place that return value in VR2.  */
1961   if (TYPE_CODE (valtype) == TYPE_CODE_ARRAY && TYPE_VECTOR (valtype)
1962       && TYPE_LENGTH (valtype) == 16)
1963     {
1964       if (readbuf)
1965         regcache_cooked_read (regcache, tdep->ppc_vr0_regnum + 2, readbuf);
1966       if (writebuf)
1967         regcache_cooked_write (regcache, tdep->ppc_vr0_regnum + 2, writebuf);
1968
1969       return RETURN_VALUE_REGISTER_CONVENTION;
1970     }
1971
1972   /* If the called subprogram returns an aggregate, there exists an
1973      implicit first argument, whose value is the address of a caller-
1974      allocated buffer into which the callee is assumed to store its
1975      return value. All explicit parameters are appropriately
1976      relabeled.  */
1977   if (TYPE_CODE (valtype) == TYPE_CODE_STRUCT
1978       || TYPE_CODE (valtype) == TYPE_CODE_UNION
1979       || TYPE_CODE (valtype) == TYPE_CODE_ARRAY)
1980     return RETURN_VALUE_STRUCT_CONVENTION;
1981
1982   /* Scalar floating-point values are returned in FPR1 for float or
1983      double, and in FPR1:FPR2 for quadword precision.  Fortran
1984      complex*8 and complex*16 are returned in FPR1:FPR2, and
1985      complex*32 is returned in FPR1:FPR4.  */
1986   if (TYPE_CODE (valtype) == TYPE_CODE_FLT
1987       && (TYPE_LENGTH (valtype) == 4 || TYPE_LENGTH (valtype) == 8))
1988     {
1989       struct type *regtype = register_type (gdbarch, tdep->ppc_fp0_regnum);
1990       gdb_byte regval[8];
1991
1992       /* FIXME: kettenis/2007-01-01: Add support for quadword
1993          precision and complex.  */
1994
1995       if (readbuf)
1996         {
1997           regcache_cooked_read (regcache, tdep->ppc_fp0_regnum + 1, regval);
1998           convert_typed_floating (regval, regtype, readbuf, valtype);
1999         }
2000       if (writebuf)
2001         {
2002           convert_typed_floating (writebuf, valtype, regval, regtype);
2003           regcache_cooked_write (regcache, tdep->ppc_fp0_regnum + 1, regval);
2004         }
2005
2006       return RETURN_VALUE_REGISTER_CONVENTION;
2007   }
2008
2009   /* Values of the types int, long, short, pointer, and char (length
2010      is less than or equal to four bytes), as well as bit values of
2011      lengths less than or equal to 32 bits, must be returned right
2012      justified in GPR3 with signed values sign extended and unsigned
2013      values zero extended, as necessary.  */
2014   if (TYPE_LENGTH (valtype) <= tdep->wordsize)
2015     {
2016       if (readbuf)
2017         {
2018           ULONGEST regval;
2019
2020           /* For reading we don't have to worry about sign extension.  */
2021           regcache_cooked_read_unsigned (regcache, tdep->ppc_gp0_regnum + 3,
2022                                          &regval);
2023           store_unsigned_integer (readbuf, TYPE_LENGTH (valtype), regval);
2024         }
2025       if (writebuf)
2026         {
2027           /* For writing, use unpack_long since that should handle any
2028              required sign extension.  */
2029           regcache_cooked_write_unsigned (regcache, tdep->ppc_gp0_regnum + 3,
2030                                           unpack_long (valtype, writebuf));
2031         }
2032
2033       return RETURN_VALUE_REGISTER_CONVENTION;
2034     }
2035
2036   /* Eight-byte non-floating-point scalar values must be returned in
2037      GPR3:GPR4.  */
2038
2039   if (TYPE_LENGTH (valtype) == 8)
2040     {
2041       gdb_assert (TYPE_CODE (valtype) != TYPE_CODE_FLT);
2042       gdb_assert (tdep->wordsize == 4);
2043
2044       if (readbuf)
2045         {
2046           gdb_byte regval[8];
2047
2048           regcache_cooked_read (regcache, tdep->ppc_gp0_regnum + 3, regval);
2049           regcache_cooked_read (regcache, tdep->ppc_gp0_regnum + 4,
2050                                 regval + 4);
2051           memcpy (readbuf, regval, 8);
2052         }
2053       if (writebuf)
2054         {
2055           regcache_cooked_write (regcache, tdep->ppc_gp0_regnum + 3, writebuf);
2056           regcache_cooked_write (regcache, tdep->ppc_gp0_regnum + 4,
2057                                  writebuf + 4);
2058         }
2059
2060       return RETURN_VALUE_REGISTER_CONVENTION;
2061     }
2062
2063   return RETURN_VALUE_STRUCT_CONVENTION;
2064 }
2065
2066 /* Return whether handle_inferior_event() should proceed through code
2067    starting at PC in function NAME when stepping.
2068
2069    The AIX -bbigtoc linker option generates functions @FIX0, @FIX1, etc. to
2070    handle memory references that are too distant to fit in instructions
2071    generated by the compiler.  For example, if 'foo' in the following
2072    instruction:
2073
2074      lwz r9,foo(r2)
2075
2076    is greater than 32767, the linker might replace the lwz with a branch to
2077    somewhere in @FIX1 that does the load in 2 instructions and then branches
2078    back to where execution should continue.
2079
2080    GDB should silently step over @FIX code, just like AIX dbx does.
2081    Unfortunately, the linker uses the "b" instruction for the
2082    branches, meaning that the link register doesn't get set.
2083    Therefore, GDB's usual step_over_function () mechanism won't work.
2084
2085    Instead, use the gdbarch_skip_trampoline_code and
2086    gdbarch_skip_trampoline_code hooks in handle_inferior_event() to skip past
2087    @FIX code.  */
2088
2089 int
2090 rs6000_in_solib_return_trampoline (CORE_ADDR pc, char *name)
2091 {
2092   return name && !strncmp (name, "@FIX", 4);
2093 }
2094
2095 /* Skip code that the user doesn't want to see when stepping:
2096
2097    1. Indirect function calls use a piece of trampoline code to do context
2098    switching, i.e. to set the new TOC table.  Skip such code if we are on
2099    its first instruction (as when we have single-stepped to here).
2100
2101    2. Skip shared library trampoline code (which is different from
2102    indirect function call trampolines).
2103
2104    3. Skip bigtoc fixup code.
2105
2106    Result is desired PC to step until, or NULL if we are not in
2107    code that should be skipped.  */
2108
2109 CORE_ADDR
2110 rs6000_skip_trampoline_code (struct frame_info *frame, CORE_ADDR pc)
2111 {
2112   unsigned int ii, op;
2113   int rel;
2114   CORE_ADDR solib_target_pc;
2115   struct minimal_symbol *msymbol;
2116
2117   static unsigned trampoline_code[] =
2118   {
2119     0x800b0000,                 /*     l   r0,0x0(r11)  */
2120     0x90410014,                 /*    st   r2,0x14(r1)  */
2121     0x7c0903a6,                 /* mtctr   r0           */
2122     0x804b0004,                 /*     l   r2,0x4(r11)  */
2123     0x816b0008,                 /*     l  r11,0x8(r11)  */
2124     0x4e800420,                 /*  bctr                */
2125     0x4e800020,                 /*    br                */
2126     0
2127   };
2128
2129   /* Check for bigtoc fixup code.  */
2130   msymbol = lookup_minimal_symbol_by_pc (pc);
2131   if (msymbol 
2132       && rs6000_in_solib_return_trampoline (pc, 
2133                                             DEPRECATED_SYMBOL_NAME (msymbol)))
2134     {
2135       /* Double-check that the third instruction from PC is relative "b".  */
2136       op = read_memory_integer (pc + 8, 4);
2137       if ((op & 0xfc000003) == 0x48000000)
2138         {
2139           /* Extract bits 6-29 as a signed 24-bit relative word address and
2140              add it to the containing PC.  */
2141           rel = ((int)(op << 6) >> 6);
2142           return pc + 8 + rel;
2143         }
2144     }
2145
2146   /* If pc is in a shared library trampoline, return its target.  */
2147   solib_target_pc = find_solib_trampoline_target (frame, pc);
2148   if (solib_target_pc)
2149     return solib_target_pc;
2150
2151   for (ii = 0; trampoline_code[ii]; ++ii)
2152     {
2153       op = read_memory_integer (pc + (ii * 4), 4);
2154       if (op != trampoline_code[ii])
2155         return 0;
2156     }
2157   ii = get_frame_register_unsigned (frame, 11); /* r11 holds destination addr   */
2158   pc = read_memory_addr (ii,
2159                          gdbarch_tdep (get_frame_arch (frame))->wordsize); /* (r11) value */
2160   return pc;
2161 }
2162
2163 /* ISA-specific vector types.  */
2164
2165 static struct type *
2166 rs6000_builtin_type_vec64 (struct gdbarch *gdbarch)
2167 {
2168   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
2169
2170   if (!tdep->ppc_builtin_type_vec64)
2171     {
2172       /* The type we're building is this: */
2173 #if 0
2174       union __gdb_builtin_type_vec64
2175         {
2176           int64_t uint64;
2177           float v2_float[2];
2178           int32_t v2_int32[2];
2179           int16_t v4_int16[4];
2180           int8_t v8_int8[8];
2181         };
2182 #endif
2183
2184       struct type *t;
2185
2186       t = init_composite_type ("__ppc_builtin_type_vec64", TYPE_CODE_UNION);
2187       append_composite_type_field (t, "uint64", builtin_type_int64);
2188       append_composite_type_field (t, "v2_float",
2189                                    init_vector_type (builtin_type_float, 2));
2190       append_composite_type_field (t, "v2_int32",
2191                                    init_vector_type (builtin_type_int32, 2));
2192       append_composite_type_field (t, "v4_int16",
2193                                    init_vector_type (builtin_type_int16, 4));
2194       append_composite_type_field (t, "v8_int8",
2195                                    init_vector_type (builtin_type_int8, 8));
2196
2197       TYPE_FLAGS (t) |= TYPE_FLAG_VECTOR;
2198       TYPE_NAME (t) = "ppc_builtin_type_vec64";
2199       tdep->ppc_builtin_type_vec64 = t;
2200     }
2201
2202   return tdep->ppc_builtin_type_vec64;
2203 }
2204
2205 /* Return the size of register REG when words are WORDSIZE bytes long.  If REG
2206    isn't available with that word size, return 0.  */
2207
2208 static int
2209 regsize (const struct reg *reg, int wordsize)
2210 {
2211   return wordsize == 8 ? reg->sz64 : reg->sz32;
2212 }
2213
2214 /* Return the name of register number REGNO, or the empty string if it
2215    is an anonymous register.  */
2216
2217 static const char *
2218 rs6000_register_name (int regno)
2219 {
2220   struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch);
2221
2222   /* The upper half "registers" have names in the XML description,
2223      but we present only the low GPRs and the full 64-bit registers
2224      to the user.  */
2225   if (tdep->ppc_ev0_upper_regnum >= 0
2226       && tdep->ppc_ev0_upper_regnum <= regno
2227       && regno < tdep->ppc_ev0_upper_regnum + ppc_num_gprs)
2228     return "";
2229
2230   /* Check if the SPE pseudo registers are available.  */
2231   if (tdep->ppc_ev0_regnum >= 0
2232       && tdep->ppc_ev0_regnum <= regno
2233       && regno < tdep->ppc_ev0_regnum + ppc_num_gprs)
2234     {
2235       static const char *const spe_regnames[] = {
2236         "ev0", "ev1", "ev2", "ev3", "ev4", "ev5", "ev6", "ev7",
2237         "ev8", "ev9", "ev10", "ev11", "ev12", "ev13", "ev14", "ev15",
2238         "ev16", "ev17", "ev18", "ev19", "ev20", "ev21", "ev22", "ev23",
2239         "ev24", "ev25", "ev26", "ev27", "ev28", "ev29", "ev30", "ev31",
2240       };
2241       return spe_regnames[regno - tdep->ppc_ev0_regnum];
2242     }
2243
2244   return tdesc_register_name (regno);
2245 }
2246
2247 /* Return the GDB type object for the "standard" data type of data in
2248    register N.  */
2249
2250 static struct type *
2251 rs6000_pseudo_register_type (struct gdbarch *gdbarch, int regnum)
2252 {
2253   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
2254
2255   /* These are the only pseudo-registers we support.  */
2256   gdb_assert (tdep->ppc_ev0_regnum >= 0
2257               && regnum >= tdep->ppc_ev0_regnum
2258               && regnum < tdep->ppc_ev0_regnum + 32);
2259
2260   return rs6000_builtin_type_vec64 (gdbarch);
2261 }
2262
2263 /* Is REGNUM a member of REGGROUP?  */
2264 static int
2265 rs6000_pseudo_register_reggroup_p (struct gdbarch *gdbarch, int regnum,
2266                                    struct reggroup *group)
2267 {
2268   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
2269
2270   /* These are the only pseudo-registers we support.  */
2271   gdb_assert (tdep->ppc_ev0_regnum >= 0
2272               && regnum >= tdep->ppc_ev0_regnum
2273               && regnum < tdep->ppc_ev0_regnum + 32);
2274
2275   if (group == all_reggroup || group == vector_reggroup)
2276     return 1;
2277   else
2278     return 0;
2279 }
2280
2281 /* The register format for RS/6000 floating point registers is always
2282    double, we need a conversion if the memory format is float.  */
2283
2284 static int
2285 rs6000_convert_register_p (int regnum, struct type *type)
2286 {
2287   struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch);
2288
2289   return (tdep->ppc_fp0_regnum >= 0
2290           && regnum >= tdep->ppc_fp0_regnum
2291           && regnum < tdep->ppc_fp0_regnum + ppc_num_fprs
2292           && TYPE_CODE (type) == TYPE_CODE_FLT
2293           && TYPE_LENGTH (type) != TYPE_LENGTH (builtin_type_double));
2294 }
2295
2296 static void
2297 rs6000_register_to_value (struct frame_info *frame,
2298                           int regnum,
2299                           struct type *type,
2300                           gdb_byte *to)
2301 {
2302   gdb_byte from[MAX_REGISTER_SIZE];
2303   
2304   gdb_assert (TYPE_CODE (type) == TYPE_CODE_FLT);
2305
2306   get_frame_register (frame, regnum, from);
2307   convert_typed_floating (from, builtin_type_double, to, type);
2308 }
2309
2310 static void
2311 rs6000_value_to_register (struct frame_info *frame,
2312                           int regnum,
2313                           struct type *type,
2314                           const gdb_byte *from)
2315 {
2316   gdb_byte to[MAX_REGISTER_SIZE];
2317
2318   gdb_assert (TYPE_CODE (type) == TYPE_CODE_FLT);
2319
2320   convert_typed_floating (from, type, to, builtin_type_double);
2321   put_frame_register (frame, regnum, to);
2322 }
2323
2324 /* Move SPE vector register values between a 64-bit buffer and the two
2325    32-bit raw register halves in a regcache.  This function handles
2326    both splitting a 64-bit value into two 32-bit halves, and joining
2327    two halves into a whole 64-bit value, depending on the function
2328    passed as the MOVE argument.
2329
2330    EV_REG must be the number of an SPE evN vector register --- a
2331    pseudoregister.  REGCACHE must be a regcache, and BUFFER must be a
2332    64-bit buffer.
2333
2334    Call MOVE once for each 32-bit half of that register, passing
2335    REGCACHE, the number of the raw register corresponding to that
2336    half, and the address of the appropriate half of BUFFER.
2337
2338    For example, passing 'regcache_raw_read' as the MOVE function will
2339    fill BUFFER with the full 64-bit contents of EV_REG.  Or, passing
2340    'regcache_raw_supply' will supply the contents of BUFFER to the
2341    appropriate pair of raw registers in REGCACHE.
2342
2343    You may need to cast away some 'const' qualifiers when passing
2344    MOVE, since this function can't tell at compile-time which of
2345    REGCACHE or BUFFER is acting as the source of the data.  If C had
2346    co-variant type qualifiers, ...  */
2347 static void
2348 e500_move_ev_register (void (*move) (struct regcache *regcache,
2349                                      int regnum, gdb_byte *buf),
2350                        struct regcache *regcache, int ev_reg,
2351                        gdb_byte *buffer)
2352 {
2353   struct gdbarch *arch = get_regcache_arch (regcache);
2354   struct gdbarch_tdep *tdep = gdbarch_tdep (arch); 
2355   int reg_index;
2356   gdb_byte *byte_buffer = buffer;
2357
2358   gdb_assert (tdep->ppc_ev0_regnum <= ev_reg
2359               && ev_reg < tdep->ppc_ev0_regnum + ppc_num_gprs);
2360
2361   reg_index = ev_reg - tdep->ppc_ev0_regnum;
2362
2363   if (gdbarch_byte_order (arch) == BFD_ENDIAN_BIG)
2364     {
2365       move (regcache, tdep->ppc_ev0_upper_regnum + reg_index, byte_buffer);
2366       move (regcache, tdep->ppc_gp0_regnum + reg_index, byte_buffer + 4);
2367     }
2368   else
2369     {
2370       move (regcache, tdep->ppc_gp0_regnum + reg_index, byte_buffer);
2371       move (regcache, tdep->ppc_ev0_upper_regnum + reg_index, byte_buffer + 4);
2372     }
2373 }
2374
2375 static void
2376 e500_pseudo_register_read (struct gdbarch *gdbarch, struct regcache *regcache,
2377                            int reg_nr, gdb_byte *buffer)
2378 {
2379   struct gdbarch *regcache_arch = get_regcache_arch (regcache);
2380   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch); 
2381
2382   gdb_assert (regcache_arch == gdbarch);
2383  
2384   if (tdep->ppc_ev0_regnum <= reg_nr
2385       && reg_nr < tdep->ppc_ev0_regnum + ppc_num_gprs)
2386     e500_move_ev_register (regcache_raw_read, regcache, reg_nr, buffer);
2387   else
2388     internal_error (__FILE__, __LINE__,
2389                     _("e500_pseudo_register_read: "
2390                     "called on unexpected register '%s' (%d)"),
2391                     gdbarch_register_name (gdbarch, reg_nr), reg_nr);
2392 }
2393
2394 static void
2395 e500_pseudo_register_write (struct gdbarch *gdbarch, struct regcache *regcache,
2396                             int reg_nr, const gdb_byte *buffer)
2397 {
2398   struct gdbarch *regcache_arch = get_regcache_arch (regcache);
2399   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch); 
2400
2401   gdb_assert (regcache_arch == gdbarch);
2402  
2403   if (tdep->ppc_ev0_regnum <= reg_nr
2404       && reg_nr < tdep->ppc_ev0_regnum + ppc_num_gprs)
2405     e500_move_ev_register ((void (*) (struct regcache *, int, gdb_byte *))
2406                            regcache_raw_write,
2407                            regcache, reg_nr, (gdb_byte *) buffer);
2408   else
2409     internal_error (__FILE__, __LINE__,
2410                     _("e500_pseudo_register_read: "
2411                     "called on unexpected register '%s' (%d)"),
2412                     gdbarch_register_name (gdbarch, reg_nr), reg_nr);
2413 }
2414
2415 /* Convert a DBX STABS register number to a GDB register number.  */
2416 static int
2417 rs6000_stab_reg_to_regnum (int num)
2418 {
2419   struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch);
2420
2421   if (0 <= num && num <= 31)
2422     return tdep->ppc_gp0_regnum + num;
2423   else if (32 <= num && num <= 63)
2424     /* FIXME: jimb/2004-05-05: What should we do when the debug info
2425        specifies registers the architecture doesn't have?  Our
2426        callers don't check the value we return.  */
2427     return tdep->ppc_fp0_regnum + (num - 32);
2428   else if (77 <= num && num <= 108)
2429     return tdep->ppc_vr0_regnum + (num - 77);
2430   else if (1200 <= num && num < 1200 + 32)
2431     return tdep->ppc_ev0_regnum + (num - 1200);
2432   else
2433     switch (num)
2434       {
2435       case 64: 
2436         return tdep->ppc_mq_regnum;
2437       case 65:
2438         return tdep->ppc_lr_regnum;
2439       case 66: 
2440         return tdep->ppc_ctr_regnum;
2441       case 76: 
2442         return tdep->ppc_xer_regnum;
2443       case 109:
2444         return tdep->ppc_vrsave_regnum;
2445       case 110:
2446         return tdep->ppc_vrsave_regnum - 1; /* vscr */
2447       case 111:
2448         return tdep->ppc_acc_regnum;
2449       case 112:
2450         return tdep->ppc_spefscr_regnum;
2451       default: 
2452         return num;
2453       }
2454 }
2455
2456
2457 /* Convert a Dwarf 2 register number to a GDB register number.  */
2458 static int
2459 rs6000_dwarf2_reg_to_regnum (int num)
2460 {
2461   struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch);
2462
2463   if (0 <= num && num <= 31)
2464     return tdep->ppc_gp0_regnum + num;
2465   else if (32 <= num && num <= 63)
2466     /* FIXME: jimb/2004-05-05: What should we do when the debug info
2467        specifies registers the architecture doesn't have?  Our
2468        callers don't check the value we return.  */
2469     return tdep->ppc_fp0_regnum + (num - 32);
2470   else if (1124 <= num && num < 1124 + 32)
2471     return tdep->ppc_vr0_regnum + (num - 1124);
2472   else if (1200 <= num && num < 1200 + 32)
2473     return tdep->ppc_ev0_regnum + (num - 1200);
2474   else
2475     switch (num)
2476       {
2477       case 64:
2478         return tdep->ppc_cr_regnum;
2479       case 67:
2480         return tdep->ppc_vrsave_regnum - 1; /* vscr */
2481       case 99:
2482         return tdep->ppc_acc_regnum;
2483       case 100:
2484         return tdep->ppc_mq_regnum;
2485       case 101:
2486         return tdep->ppc_xer_regnum;
2487       case 108:
2488         return tdep->ppc_lr_regnum;
2489       case 109:
2490         return tdep->ppc_ctr_regnum;
2491       case 356:
2492         return tdep->ppc_vrsave_regnum;
2493       case 612:
2494         return tdep->ppc_spefscr_regnum;
2495       default:
2496         return num;
2497       }
2498 }
2499
2500 /* Translate a .eh_frame register to DWARF register, or adjust a
2501    .debug_frame register.  */
2502
2503 static int
2504 rs6000_adjust_frame_regnum (struct gdbarch *gdbarch, int num, int eh_frame_p)
2505 {
2506   /* GCC releases before 3.4 use GCC internal register numbering in
2507      .debug_frame (and .debug_info, et cetera).  The numbering is
2508      different from the standard SysV numbering for everything except
2509      for GPRs and FPRs.  We can not detect this problem in most cases
2510      - to get accurate debug info for variables living in lr, ctr, v0,
2511      et cetera, use a newer version of GCC.  But we must detect
2512      one important case - lr is in column 65 in .debug_frame output,
2513      instead of 108.
2514
2515      GCC 3.4, and the "hammer" branch, have a related problem.  They
2516      record lr register saves in .debug_frame as 108, but still record
2517      the return column as 65.  We fix that up too.
2518
2519      We can do this because 65 is assigned to fpsr, and GCC never
2520      generates debug info referring to it.  To add support for
2521      handwritten debug info that restores fpsr, we would need to add a
2522      producer version check to this.  */
2523   if (!eh_frame_p)
2524     {
2525       if (num == 65)
2526         return 108;
2527       else
2528         return num;
2529     }
2530
2531   /* .eh_frame is GCC specific.  For binary compatibility, it uses GCC
2532      internal register numbering; translate that to the standard DWARF2
2533      register numbering.  */
2534   if (0 <= num && num <= 63)    /* r0-r31,fp0-fp31 */
2535     return num;
2536   else if (68 <= num && num <= 75) /* cr0-cr8 */
2537     return num - 68 + 86;
2538   else if (77 <= num && num <= 108) /* vr0-vr31 */
2539     return num - 77 + 1124;
2540   else
2541     switch (num)
2542       {
2543       case 64: /* mq */
2544         return 100;
2545       case 65: /* lr */
2546         return 108;
2547       case 66: /* ctr */
2548         return 109;
2549       case 76: /* xer */
2550         return 101;
2551       case 109: /* vrsave */
2552         return 356;
2553       case 110: /* vscr */
2554         return 67;
2555       case 111: /* spe_acc */
2556         return 99;
2557       case 112: /* spefscr */
2558         return 612;
2559       default:
2560         return num;
2561       }
2562 }
2563 \f
2564 /* Support for CONVERT_FROM_FUNC_PTR_ADDR (ARCH, ADDR, TARG).
2565
2566    Usually a function pointer's representation is simply the address
2567    of the function. On the RS/6000 however, a function pointer is
2568    represented by a pointer to an OPD entry. This OPD entry contains
2569    three words, the first word is the address of the function, the
2570    second word is the TOC pointer (r2), and the third word is the
2571    static chain value.  Throughout GDB it is currently assumed that a
2572    function pointer contains the address of the function, which is not
2573    easy to fix.  In addition, the conversion of a function address to
2574    a function pointer would require allocation of an OPD entry in the
2575    inferior's memory space, with all its drawbacks.  To be able to
2576    call C++ virtual methods in the inferior (which are called via
2577    function pointers), find_function_addr uses this function to get the
2578    function address from a function pointer.  */
2579
2580 /* Return real function address if ADDR (a function pointer) is in the data
2581    space and is therefore a special function pointer.  */
2582
2583 static CORE_ADDR
2584 rs6000_convert_from_func_ptr_addr (struct gdbarch *gdbarch,
2585                                    CORE_ADDR addr,
2586                                    struct target_ops *targ)
2587 {
2588   struct obj_section *s;
2589
2590   s = find_pc_section (addr);
2591   if (s && s->the_bfd_section->flags & SEC_CODE)
2592     return addr;
2593
2594   /* ADDR is in the data space, so it's a special function pointer. */
2595   return read_memory_addr (addr, gdbarch_tdep (gdbarch)->wordsize);
2596 }
2597 \f
2598
2599 /* Handling the various POWER/PowerPC variants.  */
2600
2601 /* Information about a particular processor variant.  */
2602
2603 struct variant
2604   {
2605     /* Name of this variant.  */
2606     char *name;
2607
2608     /* English description of the variant.  */
2609     char *description;
2610
2611     /* bfd_arch_info.arch corresponding to variant.  */
2612     enum bfd_architecture arch;
2613
2614     /* bfd_arch_info.mach corresponding to variant.  */
2615     unsigned long mach;
2616
2617     /* Target description for this variant.  */
2618     struct target_desc **tdesc;
2619   };
2620
2621 static struct variant variants[] =
2622 {
2623   {"powerpc", "PowerPC user-level", bfd_arch_powerpc,
2624    bfd_mach_ppc, &tdesc_powerpc_32},
2625   {"power", "POWER user-level", bfd_arch_rs6000,
2626    bfd_mach_rs6k, &tdesc_rs6000},
2627   {"403", "IBM PowerPC 403", bfd_arch_powerpc,
2628    bfd_mach_ppc_403, &tdesc_powerpc_403},
2629   {"601", "Motorola PowerPC 601", bfd_arch_powerpc,
2630    bfd_mach_ppc_601, &tdesc_powerpc_601},
2631   {"602", "Motorola PowerPC 602", bfd_arch_powerpc,
2632    bfd_mach_ppc_602, &tdesc_powerpc_602},
2633   {"603", "Motorola/IBM PowerPC 603 or 603e", bfd_arch_powerpc,
2634    bfd_mach_ppc_603, &tdesc_powerpc_603},
2635   {"604", "Motorola PowerPC 604 or 604e", bfd_arch_powerpc,
2636    604, &tdesc_powerpc_604},
2637   {"403GC", "IBM PowerPC 403GC", bfd_arch_powerpc,
2638    bfd_mach_ppc_403gc, &tdesc_powerpc_403gc},
2639   {"505", "Motorola PowerPC 505", bfd_arch_powerpc,
2640    bfd_mach_ppc_505, &tdesc_powerpc_505},
2641   {"860", "Motorola PowerPC 860 or 850", bfd_arch_powerpc,
2642    bfd_mach_ppc_860, &tdesc_powerpc_860},
2643   {"750", "Motorola/IBM PowerPC 750 or 740", bfd_arch_powerpc,
2644    bfd_mach_ppc_750, &tdesc_powerpc_750},
2645   {"7400", "Motorola/IBM PowerPC 7400 (G4)", bfd_arch_powerpc,
2646    bfd_mach_ppc_7400, &tdesc_powerpc_7400},
2647   {"e500", "Motorola PowerPC e500", bfd_arch_powerpc,
2648    bfd_mach_ppc_e500, &tdesc_powerpc_e500},
2649
2650   /* 64-bit */
2651   {"powerpc64", "PowerPC 64-bit user-level", bfd_arch_powerpc,
2652    bfd_mach_ppc64, &tdesc_powerpc_64},
2653   {"620", "Motorola PowerPC 620", bfd_arch_powerpc,
2654    bfd_mach_ppc_620, &tdesc_powerpc_64},
2655   {"630", "Motorola PowerPC 630", bfd_arch_powerpc,
2656    bfd_mach_ppc_630, &tdesc_powerpc_64},
2657   {"a35", "PowerPC A35", bfd_arch_powerpc,
2658    bfd_mach_ppc_a35, &tdesc_powerpc_64},
2659   {"rs64ii", "PowerPC rs64ii", bfd_arch_powerpc,
2660    bfd_mach_ppc_rs64ii, &tdesc_powerpc_64},
2661   {"rs64iii", "PowerPC rs64iii", bfd_arch_powerpc,
2662    bfd_mach_ppc_rs64iii, &tdesc_powerpc_64},
2663
2664   /* FIXME: I haven't checked the register sets of the following.  */
2665   {"rs1", "IBM POWER RS1", bfd_arch_rs6000,
2666    bfd_mach_rs6k_rs1, &tdesc_rs6000},
2667   {"rsc", "IBM POWER RSC", bfd_arch_rs6000,
2668    bfd_mach_rs6k_rsc, &tdesc_rs6000},
2669   {"rs2", "IBM POWER RS2", bfd_arch_rs6000,
2670    bfd_mach_rs6k_rs2, &tdesc_rs6000},
2671
2672   {0, 0, 0, 0, 0}
2673 };
2674
2675 /* Return the variant corresponding to architecture ARCH and machine number
2676    MACH.  If no such variant exists, return null.  */
2677
2678 static const struct variant *
2679 find_variant_by_arch (enum bfd_architecture arch, unsigned long mach)
2680 {
2681   const struct variant *v;
2682
2683   for (v = variants; v->name; v++)
2684     if (arch == v->arch && mach == v->mach)
2685       return v;
2686
2687   return NULL;
2688 }
2689
2690 static int
2691 gdb_print_insn_powerpc (bfd_vma memaddr, disassemble_info *info)
2692 {
2693   if (!info->disassembler_options)
2694     info->disassembler_options = "any";
2695
2696   if (gdbarch_byte_order (current_gdbarch) == BFD_ENDIAN_BIG)
2697     return print_insn_big_powerpc (memaddr, info);
2698   else
2699     return print_insn_little_powerpc (memaddr, info);
2700 }
2701 \f
2702 static CORE_ADDR
2703 rs6000_unwind_pc (struct gdbarch *gdbarch, struct frame_info *next_frame)
2704 {
2705   return frame_unwind_register_unsigned (next_frame,
2706                                          gdbarch_pc_regnum (gdbarch));
2707 }
2708
2709 static struct frame_id
2710 rs6000_unwind_dummy_id (struct gdbarch *gdbarch, struct frame_info *next_frame)
2711 {
2712   return frame_id_build (frame_unwind_register_unsigned
2713                          (next_frame, gdbarch_sp_regnum (gdbarch)),
2714                         frame_pc_unwind (next_frame));
2715 }
2716
2717 struct rs6000_frame_cache
2718 {
2719   CORE_ADDR base;
2720   CORE_ADDR initial_sp;
2721   struct trad_frame_saved_reg *saved_regs;
2722 };
2723
2724 static struct rs6000_frame_cache *
2725 rs6000_frame_cache (struct frame_info *next_frame, void **this_cache)
2726 {
2727   struct rs6000_frame_cache *cache;
2728   struct gdbarch *gdbarch = get_frame_arch (next_frame);
2729   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
2730   struct rs6000_framedata fdata;
2731   int wordsize = tdep->wordsize;
2732   CORE_ADDR func, pc;
2733
2734   if ((*this_cache) != NULL)
2735     return (*this_cache);
2736   cache = FRAME_OBSTACK_ZALLOC (struct rs6000_frame_cache);
2737   (*this_cache) = cache;
2738   cache->saved_regs = trad_frame_alloc_saved_regs (next_frame);
2739
2740   func = frame_func_unwind (next_frame, NORMAL_FRAME);
2741   pc = frame_pc_unwind (next_frame);
2742   skip_prologue (func, pc, &fdata);
2743
2744   /* Figure out the parent's stack pointer.  */
2745
2746   /* NOTE: cagney/2002-04-14: The ->frame points to the inner-most
2747      address of the current frame.  Things might be easier if the
2748      ->frame pointed to the outer-most address of the frame.  In
2749      the mean time, the address of the prev frame is used as the
2750      base address of this frame.  */
2751   cache->base = frame_unwind_register_unsigned
2752                 (next_frame, gdbarch_sp_regnum (gdbarch));
2753
2754   /* If the function appears to be frameless, check a couple of likely
2755      indicators that we have simply failed to find the frame setup.
2756      Two common cases of this are missing symbols (i.e.
2757      frame_func_unwind returns the wrong address or 0), and assembly
2758      stubs which have a fast exit path but set up a frame on the slow
2759      path.
2760
2761      If the LR appears to return to this function, then presume that
2762      we have an ABI compliant frame that we failed to find.  */
2763   if (fdata.frameless && fdata.lr_offset == 0)
2764     {
2765       CORE_ADDR saved_lr;
2766       int make_frame = 0;
2767
2768       saved_lr = frame_unwind_register_unsigned (next_frame,
2769                                                  tdep->ppc_lr_regnum);
2770       if (func == 0 && saved_lr == pc)
2771         make_frame = 1;
2772       else if (func != 0)
2773         {
2774           CORE_ADDR saved_func = get_pc_function_start (saved_lr);
2775           if (func == saved_func)
2776             make_frame = 1;
2777         }
2778
2779       if (make_frame)
2780         {
2781           fdata.frameless = 0;
2782           fdata.lr_offset = tdep->lr_frame_offset;
2783         }
2784     }
2785
2786   if (!fdata.frameless)
2787     /* Frameless really means stackless.  */
2788     cache->base = read_memory_addr (cache->base, wordsize);
2789
2790   trad_frame_set_value (cache->saved_regs,
2791                         gdbarch_sp_regnum (gdbarch), cache->base);
2792
2793   /* if != -1, fdata.saved_fpr is the smallest number of saved_fpr.
2794      All fpr's from saved_fpr to fp31 are saved.  */
2795
2796   if (fdata.saved_fpr >= 0)
2797     {
2798       int i;
2799       CORE_ADDR fpr_addr = cache->base + fdata.fpr_offset;
2800
2801       /* If skip_prologue says floating-point registers were saved,
2802          but the current architecture has no floating-point registers,
2803          then that's strange.  But we have no indices to even record
2804          the addresses under, so we just ignore it.  */
2805       if (ppc_floating_point_unit_p (gdbarch))
2806         for (i = fdata.saved_fpr; i < ppc_num_fprs; i++)
2807           {
2808             cache->saved_regs[tdep->ppc_fp0_regnum + i].addr = fpr_addr;
2809             fpr_addr += 8;
2810           }
2811     }
2812
2813   /* if != -1, fdata.saved_gpr is the smallest number of saved_gpr.
2814      All gpr's from saved_gpr to gpr31 are saved.  */
2815
2816   if (fdata.saved_gpr >= 0)
2817     {
2818       int i;
2819       CORE_ADDR gpr_addr = cache->base + fdata.gpr_offset;
2820       for (i = fdata.saved_gpr; i < ppc_num_gprs; i++)
2821         {
2822           cache->saved_regs[tdep->ppc_gp0_regnum + i].addr = gpr_addr;
2823           gpr_addr += wordsize;
2824         }
2825     }
2826
2827   /* if != -1, fdata.saved_vr is the smallest number of saved_vr.
2828      All vr's from saved_vr to vr31 are saved.  */
2829   if (tdep->ppc_vr0_regnum != -1 && tdep->ppc_vrsave_regnum != -1)
2830     {
2831       if (fdata.saved_vr >= 0)
2832         {
2833           int i;
2834           CORE_ADDR vr_addr = cache->base + fdata.vr_offset;
2835           for (i = fdata.saved_vr; i < 32; i++)
2836             {
2837               cache->saved_regs[tdep->ppc_vr0_regnum + i].addr = vr_addr;
2838               vr_addr += register_size (gdbarch, tdep->ppc_vr0_regnum);
2839             }
2840         }
2841     }
2842
2843   /* if != -1, fdata.saved_ev is the smallest number of saved_ev.
2844      All vr's from saved_ev to ev31 are saved. ????? */
2845   if (tdep->ppc_ev0_regnum != -1 && tdep->ppc_ev31_regnum != -1)
2846     {
2847       if (fdata.saved_ev >= 0)
2848         {
2849           int i;
2850           CORE_ADDR ev_addr = cache->base + fdata.ev_offset;
2851           for (i = fdata.saved_ev; i < ppc_num_gprs; i++)
2852             {
2853               cache->saved_regs[tdep->ppc_ev0_regnum + i].addr = ev_addr;
2854               cache->saved_regs[tdep->ppc_gp0_regnum + i].addr = ev_addr + 4;
2855               ev_addr += register_size (gdbarch, tdep->ppc_ev0_regnum);
2856             }
2857         }
2858     }
2859
2860   /* If != 0, fdata.cr_offset is the offset from the frame that
2861      holds the CR.  */
2862   if (fdata.cr_offset != 0)
2863     cache->saved_regs[tdep->ppc_cr_regnum].addr = cache->base + fdata.cr_offset;
2864
2865   /* If != 0, fdata.lr_offset is the offset from the frame that
2866      holds the LR.  */
2867   if (fdata.lr_offset != 0)
2868     cache->saved_regs[tdep->ppc_lr_regnum].addr = cache->base + fdata.lr_offset;
2869   /* The PC is found in the link register.  */
2870   cache->saved_regs[gdbarch_pc_regnum (gdbarch)] =
2871     cache->saved_regs[tdep->ppc_lr_regnum];
2872
2873   /* If != 0, fdata.vrsave_offset is the offset from the frame that
2874      holds the VRSAVE.  */
2875   if (fdata.vrsave_offset != 0)
2876     cache->saved_regs[tdep->ppc_vrsave_regnum].addr = cache->base + fdata.vrsave_offset;
2877
2878   if (fdata.alloca_reg < 0)
2879     /* If no alloca register used, then fi->frame is the value of the
2880        %sp for this frame, and it is good enough.  */
2881     cache->initial_sp = frame_unwind_register_unsigned
2882                         (next_frame, gdbarch_sp_regnum (gdbarch));
2883   else
2884     cache->initial_sp = frame_unwind_register_unsigned (next_frame,
2885                                                         fdata.alloca_reg);
2886
2887   return cache;
2888 }
2889
2890 static void
2891 rs6000_frame_this_id (struct frame_info *next_frame, void **this_cache,
2892                       struct frame_id *this_id)
2893 {
2894   struct rs6000_frame_cache *info = rs6000_frame_cache (next_frame,
2895                                                         this_cache);
2896   (*this_id) = frame_id_build (info->base,
2897                                frame_func_unwind (next_frame, NORMAL_FRAME));
2898 }
2899
2900 static void
2901 rs6000_frame_prev_register (struct frame_info *next_frame,
2902                                  void **this_cache,
2903                                  int regnum, int *optimizedp,
2904                                  enum lval_type *lvalp, CORE_ADDR *addrp,
2905                                  int *realnump, gdb_byte *valuep)
2906 {
2907   struct rs6000_frame_cache *info = rs6000_frame_cache (next_frame,
2908                                                         this_cache);
2909   trad_frame_get_prev_register (next_frame, info->saved_regs, regnum,
2910                                 optimizedp, lvalp, addrp, realnump, valuep);
2911 }
2912
2913 static const struct frame_unwind rs6000_frame_unwind =
2914 {
2915   NORMAL_FRAME,
2916   rs6000_frame_this_id,
2917   rs6000_frame_prev_register
2918 };
2919
2920 static const struct frame_unwind *
2921 rs6000_frame_sniffer (struct frame_info *next_frame)
2922 {
2923   return &rs6000_frame_unwind;
2924 }
2925
2926 \f
2927
2928 static CORE_ADDR
2929 rs6000_frame_base_address (struct frame_info *next_frame,
2930                                 void **this_cache)
2931 {
2932   struct rs6000_frame_cache *info = rs6000_frame_cache (next_frame,
2933                                                         this_cache);
2934   return info->initial_sp;
2935 }
2936
2937 static const struct frame_base rs6000_frame_base = {
2938   &rs6000_frame_unwind,
2939   rs6000_frame_base_address,
2940   rs6000_frame_base_address,
2941   rs6000_frame_base_address
2942 };
2943
2944 static const struct frame_base *
2945 rs6000_frame_base_sniffer (struct frame_info *next_frame)
2946 {
2947   return &rs6000_frame_base;
2948 }
2949
2950 /* Initialize the current architecture based on INFO.  If possible, re-use an
2951    architecture from ARCHES, which is a list of architectures already created
2952    during this debugging session.
2953
2954    Called e.g. at program startup, when reading a core file, and when reading
2955    a binary file.  */
2956
2957 static struct gdbarch *
2958 rs6000_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
2959 {
2960   struct gdbarch *gdbarch;
2961   struct gdbarch_tdep *tdep;
2962   int wordsize, from_xcoff_exec, from_elf_exec;
2963   enum bfd_architecture arch;
2964   unsigned long mach;
2965   bfd abfd;
2966   int sysv_abi;
2967   asection *sect;
2968   int have_fpu = 1, have_spe = 0, have_mq = 0, have_altivec = 0;
2969   int tdesc_wordsize = -1;
2970   const struct target_desc *tdesc = info.target_desc;
2971   struct tdesc_arch_data *tdesc_data = NULL;
2972   int num_sprs = 0;
2973
2974   from_xcoff_exec = info.abfd && info.abfd->format == bfd_object &&
2975     bfd_get_flavour (info.abfd) == bfd_target_xcoff_flavour;
2976
2977   from_elf_exec = info.abfd && info.abfd->format == bfd_object &&
2978     bfd_get_flavour (info.abfd) == bfd_target_elf_flavour;
2979
2980   sysv_abi = info.abfd && bfd_get_flavour (info.abfd) == bfd_target_elf_flavour;
2981
2982   /* Check word size.  If INFO is from a binary file, infer it from
2983      that, else choose a likely default.  */
2984   if (from_xcoff_exec)
2985     {
2986       if (bfd_xcoff_is_xcoff64 (info.abfd))
2987         wordsize = 8;
2988       else
2989         wordsize = 4;
2990     }
2991   else if (from_elf_exec)
2992     {
2993       if (elf_elfheader (info.abfd)->e_ident[EI_CLASS] == ELFCLASS64)
2994         wordsize = 8;
2995       else
2996         wordsize = 4;
2997     }
2998   else if (tdesc_has_registers (tdesc))
2999     wordsize = -1;
3000   else
3001     {
3002       if (info.bfd_arch_info != NULL && info.bfd_arch_info->bits_per_word != 0)
3003         wordsize = info.bfd_arch_info->bits_per_word /
3004           info.bfd_arch_info->bits_per_byte;
3005       else
3006         wordsize = 4;
3007     }
3008
3009   if (!from_xcoff_exec)
3010     {
3011       arch = info.bfd_arch_info->arch;
3012       mach = info.bfd_arch_info->mach;
3013     }
3014   else
3015     {
3016       arch = bfd_arch_powerpc;
3017       bfd_default_set_arch_mach (&abfd, arch, 0);
3018       info.bfd_arch_info = bfd_get_arch_info (&abfd);
3019       mach = info.bfd_arch_info->mach;
3020     }
3021
3022   /* For e500 executables, the apuinfo section is of help here.  Such
3023      section contains the identifier and revision number of each
3024      Application-specific Processing Unit that is present on the
3025      chip.  The content of the section is determined by the assembler
3026      which looks at each instruction and determines which unit (and
3027      which version of it) can execute it. In our case we just look for
3028      the existance of the section.  */
3029
3030   if (info.abfd)
3031     {
3032       sect = bfd_get_section_by_name (info.abfd, ".PPC.EMB.apuinfo");
3033       if (sect)
3034         {
3035           arch = info.bfd_arch_info->arch;
3036           mach = bfd_mach_ppc_e500;
3037           bfd_default_set_arch_mach (&abfd, arch, mach);
3038           info.bfd_arch_info = bfd_get_arch_info (&abfd);
3039         }
3040     }
3041
3042   /* Find a default target description which describes our register
3043      layout, if we do not already have one.  */
3044   if (! tdesc_has_registers (tdesc))
3045     {
3046       const struct variant *v;
3047
3048       /* Choose variant.  */
3049       v = find_variant_by_arch (arch, mach);
3050       if (!v)
3051         return NULL;
3052
3053       tdesc = *v->tdesc;
3054     }
3055
3056   gdb_assert (tdesc_has_registers (tdesc));
3057
3058   /* Check any target description for validity.  */
3059   if (tdesc_has_registers (tdesc))
3060     {
3061       static const char *const gprs[] = {
3062         "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
3063         "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
3064         "r16", "r17", "r18", "r19", "r20", "r21", "r22", "r23",
3065         "r24", "r25", "r26", "r27", "r28", "r29", "r30", "r31"
3066       };
3067       static const char *const segment_regs[] = {
3068         "sr0", "sr1", "sr2", "sr3", "sr4", "sr5", "sr6", "sr7",
3069         "sr8", "sr9", "sr10", "sr11", "sr12", "sr13", "sr14", "sr15"
3070       };
3071       const struct tdesc_feature *feature;
3072       int i, valid_p;
3073       static const char *const msr_names[] = { "msr", "ps" };
3074       static const char *const cr_names[] = { "cr", "cnd" };
3075       static const char *const ctr_names[] = { "ctr", "cnt" };
3076
3077       feature = tdesc_find_feature (tdesc,
3078                                     "org.gnu.gdb.power.core");
3079       if (feature == NULL)
3080         return NULL;
3081
3082       tdesc_data = tdesc_data_alloc ();
3083
3084       valid_p = 1;
3085       for (i = 0; i < ppc_num_gprs; i++)
3086         valid_p &= tdesc_numbered_register (feature, tdesc_data, i, gprs[i]);
3087       valid_p &= tdesc_numbered_register (feature, tdesc_data, PPC_PC_REGNUM,
3088                                           "pc");
3089       valid_p &= tdesc_numbered_register (feature, tdesc_data, PPC_LR_REGNUM,
3090                                           "lr");
3091       valid_p &= tdesc_numbered_register (feature, tdesc_data, PPC_XER_REGNUM,
3092                                           "xer");
3093
3094       /* Allow alternate names for these registers, to accomodate GDB's
3095          historic naming.  */
3096       valid_p &= tdesc_numbered_register_choices (feature, tdesc_data,
3097                                                   PPC_MSR_REGNUM, msr_names);
3098       valid_p &= tdesc_numbered_register_choices (feature, tdesc_data,
3099                                                   PPC_CR_REGNUM, cr_names);
3100       valid_p &= tdesc_numbered_register_choices (feature, tdesc_data,
3101                                                   PPC_CTR_REGNUM, ctr_names);
3102
3103       if (!valid_p)
3104         {
3105           tdesc_data_cleanup (tdesc_data);
3106           return NULL;
3107         }
3108
3109       have_mq = tdesc_numbered_register (feature, tdesc_data, PPC_MQ_REGNUM,
3110                                          "mq");
3111
3112       tdesc_wordsize = tdesc_register_size (feature, "pc") / 8;
3113       if (wordsize == -1)
3114         wordsize = tdesc_wordsize;
3115
3116       feature = tdesc_find_feature (tdesc,
3117                                     "org.gnu.gdb.power.fpu");
3118       if (feature != NULL)
3119         {
3120           static const char *const fprs[] = {
3121             "f0", "f1", "f2", "f3", "f4", "f5", "f6", "f7",
3122             "f8", "f9", "f10", "f11", "f12", "f13", "f14", "f15",
3123             "f16", "f17", "f18", "f19", "f20", "f21", "f22", "f23",
3124             "f24", "f25", "f26", "f27", "f28", "f29", "f30", "f31"
3125           };
3126           valid_p = 1;
3127           for (i = 0; i < ppc_num_fprs; i++)
3128             valid_p &= tdesc_numbered_register (feature, tdesc_data,
3129                                                 PPC_F0_REGNUM + i, fprs[i]);
3130           valid_p &= tdesc_numbered_register (feature, tdesc_data,
3131                                               PPC_FPSCR_REGNUM, "fpscr");
3132
3133           if (!valid_p)
3134             {
3135               tdesc_data_cleanup (tdesc_data);
3136               return NULL;
3137             }
3138           have_fpu = 1;
3139         }
3140       else
3141         have_fpu = 0;
3142
3143       feature = tdesc_find_feature (tdesc,
3144                                     "org.gnu.gdb.power.altivec");
3145       if (feature != NULL)
3146         {
3147           static const char *const vector_regs[] = {
3148             "vr0", "vr1", "vr2", "vr3", "vr4", "vr5", "vr6", "vr7",
3149             "vr8", "vr9", "vr10", "vr11", "vr12", "vr13", "vr14", "vr15",
3150             "vr16", "vr17", "vr18", "vr19", "vr20", "vr21", "vr22", "vr23",
3151             "vr24", "vr25", "vr26", "vr27", "vr28", "vr29", "vr30", "vr31"
3152           };
3153
3154           valid_p = 1;
3155           for (i = 0; i < ppc_num_gprs; i++)
3156             valid_p &= tdesc_numbered_register (feature, tdesc_data,
3157                                                 PPC_VR0_REGNUM + i,
3158                                                 vector_regs[i]);
3159           valid_p &= tdesc_numbered_register (feature, tdesc_data,
3160                                               PPC_VSCR_REGNUM, "vscr");
3161           valid_p &= tdesc_numbered_register (feature, tdesc_data,
3162                                               PPC_VRSAVE_REGNUM, "vrsave");
3163
3164           if (have_spe || !valid_p)
3165             {
3166               tdesc_data_cleanup (tdesc_data);
3167               return NULL;
3168             }
3169           have_altivec = 1;
3170         }
3171       else
3172         have_altivec = 0;
3173
3174       /* On machines supporting the SPE APU, the general-purpose registers
3175          are 64 bits long.  There are SIMD vector instructions to treat them
3176          as pairs of floats, but the rest of the instruction set treats them
3177          as 32-bit registers, and only operates on their lower halves.
3178
3179          In the GDB regcache, we treat their high and low halves as separate
3180          registers.  The low halves we present as the general-purpose
3181          registers, and then we have pseudo-registers that stitch together
3182          the upper and lower halves and present them as pseudo-registers.
3183
3184          Thus, the target description is expected to supply the upper
3185          halves separately.  */
3186
3187       feature = tdesc_find_feature (tdesc,
3188                                     "org.gnu.gdb.power.spe");
3189       if (feature != NULL)
3190         {
3191           static const char *const upper_spe[] = {
3192             "ev0h", "ev1h", "ev2h", "ev3h",
3193             "ev4h", "ev5h", "ev6h", "ev7h",
3194             "ev8h", "ev9h", "ev10h", "ev11h",
3195             "ev12h", "ev13h", "ev14h", "ev15h",
3196             "ev16h", "ev17h", "ev18h", "ev19h",
3197             "ev20h", "ev21h", "ev22h", "ev23h",
3198             "ev24h", "ev25h", "ev26h", "ev27h",
3199             "ev28h", "ev29h", "ev30h", "ev31h"
3200           };
3201
3202           valid_p = 1;
3203           for (i = 0; i < ppc_num_gprs; i++)
3204             valid_p &= tdesc_numbered_register (feature, tdesc_data,
3205                                                 PPC_SPE_UPPER_GP0_REGNUM + i,
3206                                                 upper_spe[i]);
3207           valid_p &= tdesc_numbered_register (feature, tdesc_data,
3208                                               PPC_SPE_ACC_REGNUM, "acc");
3209           valid_p &= tdesc_numbered_register (feature, tdesc_data,
3210                                               PPC_SPE_FSCR_REGNUM, "spefscr");
3211
3212           if (have_mq || have_fpu || !valid_p)
3213             {
3214               tdesc_data_cleanup (tdesc_data);
3215               return NULL;
3216             }
3217           have_spe = 1;
3218         }
3219       else
3220         have_spe = 0;
3221     }
3222
3223   /* If we have a 64-bit binary on a 32-bit target, complain.  Also
3224      complain for a 32-bit binary on a 64-bit target; we do not yet
3225      support that.  For instance, the 32-bit ABI routines expect
3226      32-bit GPRs.
3227
3228      As long as there isn't an explicit target description, we'll
3229      choose one based on the BFD architecture and get a word size
3230      matching the binary (probably powerpc:common or
3231      powerpc:common64).  So there is only trouble if a 64-bit target
3232      supplies a 64-bit description while debugging a 32-bit
3233      binary.  */
3234   if (tdesc_wordsize != -1 && tdesc_wordsize != wordsize)
3235     {
3236       tdesc_data_cleanup (tdesc_data);
3237       return NULL;
3238     }
3239
3240   /* Find a candidate among extant architectures.  */
3241   for (arches = gdbarch_list_lookup_by_info (arches, &info);
3242        arches != NULL;
3243        arches = gdbarch_list_lookup_by_info (arches->next, &info))
3244     {
3245       /* Word size in the various PowerPC bfd_arch_info structs isn't
3246          meaningful, because 64-bit CPUs can run in 32-bit mode.  So, perform
3247          separate word size check.  */
3248       tdep = gdbarch_tdep (arches->gdbarch);
3249       if (tdep && tdep->wordsize == wordsize)
3250         {
3251           if (tdesc_data != NULL)
3252             tdesc_data_cleanup (tdesc_data);
3253           return arches->gdbarch;
3254         }
3255     }
3256
3257   /* None found, create a new architecture from INFO, whose bfd_arch_info
3258      validity depends on the source:
3259        - executable             useless
3260        - rs6000_host_arch()     good
3261        - core file              good
3262        - "set arch"             trust blindly
3263        - GDB startup            useless but harmless */
3264
3265   tdep = XCALLOC (1, struct gdbarch_tdep);
3266   tdep->wordsize = wordsize;
3267
3268   gdbarch = gdbarch_alloc (&info, tdep);
3269
3270   tdep->ppc_gp0_regnum = PPC_R0_REGNUM;
3271   tdep->ppc_toc_regnum = PPC_R0_REGNUM + 2;
3272   tdep->ppc_ps_regnum = PPC_MSR_REGNUM;
3273   tdep->ppc_cr_regnum = PPC_CR_REGNUM;
3274   tdep->ppc_lr_regnum = PPC_LR_REGNUM;
3275   tdep->ppc_ctr_regnum = PPC_CTR_REGNUM;
3276   tdep->ppc_xer_regnum = PPC_XER_REGNUM;
3277   tdep->ppc_mq_regnum = have_mq ? PPC_MQ_REGNUM : -1;
3278
3279   tdep->ppc_fp0_regnum = have_fpu ? PPC_F0_REGNUM : -1;
3280   tdep->ppc_fpscr_regnum = have_fpu ? PPC_FPSCR_REGNUM : -1;
3281   tdep->ppc_vr0_regnum = have_altivec ? PPC_VR0_REGNUM : -1;
3282   tdep->ppc_vrsave_regnum = have_altivec ? PPC_VRSAVE_REGNUM : -1;
3283   tdep->ppc_ev0_upper_regnum = have_spe ? PPC_SPE_UPPER_GP0_REGNUM : -1;
3284   tdep->ppc_acc_regnum = have_spe ? PPC_SPE_ACC_REGNUM : -1;
3285   tdep->ppc_spefscr_regnum = have_spe ? PPC_SPE_FSCR_REGNUM : -1;
3286
3287   set_gdbarch_pc_regnum (gdbarch, PPC_PC_REGNUM);
3288   set_gdbarch_sp_regnum (gdbarch, PPC_R0_REGNUM + 1);
3289   set_gdbarch_deprecated_fp_regnum (gdbarch, PPC_R0_REGNUM + 1);
3290   set_gdbarch_fp0_regnum (gdbarch, tdep->ppc_fp0_regnum);
3291   set_gdbarch_register_sim_regno (gdbarch, rs6000_register_sim_regno);
3292
3293   /* The XML specification for PowerPC sensibly calls the MSR "msr".
3294      GDB traditionally called it "ps", though, so let GDB add an
3295      alias.  */
3296   set_gdbarch_ps_regnum (gdbarch, tdep->ppc_ps_regnum);
3297
3298   if (sysv_abi && wordsize == 8)
3299     set_gdbarch_return_value (gdbarch, ppc64_sysv_abi_return_value);
3300   else if (sysv_abi && wordsize == 4)
3301     set_gdbarch_return_value (gdbarch, ppc_sysv_abi_return_value);
3302   else
3303     set_gdbarch_return_value (gdbarch, rs6000_return_value);
3304
3305   /* Set lr_frame_offset.  */
3306   if (wordsize == 8)
3307     tdep->lr_frame_offset = 16;
3308   else if (sysv_abi)
3309     tdep->lr_frame_offset = 4;
3310   else
3311     tdep->lr_frame_offset = 8;
3312
3313   if (have_spe)
3314     {
3315       set_gdbarch_pseudo_register_read (gdbarch, e500_pseudo_register_read);
3316       set_gdbarch_pseudo_register_write (gdbarch, e500_pseudo_register_write);
3317     }
3318
3319   set_gdbarch_have_nonsteppable_watchpoint (gdbarch, 1);
3320
3321   /* Select instruction printer.  */
3322   if (arch == bfd_arch_rs6000)
3323     set_gdbarch_print_insn (gdbarch, print_insn_rs6000);
3324   else
3325     set_gdbarch_print_insn (gdbarch, gdb_print_insn_powerpc);
3326
3327   set_gdbarch_num_regs (gdbarch, PPC_NUM_REGS + num_sprs);
3328   set_gdbarch_num_pseudo_regs (gdbarch, have_spe ? 32 : 0);
3329
3330   set_gdbarch_ptr_bit (gdbarch, wordsize * TARGET_CHAR_BIT);
3331   set_gdbarch_short_bit (gdbarch, 2 * TARGET_CHAR_BIT);
3332   set_gdbarch_int_bit (gdbarch, 4 * TARGET_CHAR_BIT);
3333   set_gdbarch_long_bit (gdbarch, wordsize * TARGET_CHAR_BIT);
3334   set_gdbarch_long_long_bit (gdbarch, 8 * TARGET_CHAR_BIT);
3335   set_gdbarch_float_bit (gdbarch, 4 * TARGET_CHAR_BIT);
3336   set_gdbarch_double_bit (gdbarch, 8 * TARGET_CHAR_BIT);
3337   if (sysv_abi)
3338     set_gdbarch_long_double_bit (gdbarch, 16 * TARGET_CHAR_BIT);
3339   else
3340     set_gdbarch_long_double_bit (gdbarch, 8 * TARGET_CHAR_BIT);
3341   set_gdbarch_char_signed (gdbarch, 0);
3342
3343   set_gdbarch_frame_align (gdbarch, rs6000_frame_align);
3344   if (sysv_abi && wordsize == 8)
3345     /* PPC64 SYSV.  */
3346     set_gdbarch_frame_red_zone_size (gdbarch, 288);
3347   else if (!sysv_abi && wordsize == 4)
3348     /* PowerOpen / AIX 32 bit.  The saved area or red zone consists of
3349        19 4 byte GPRS + 18 8 byte FPRs giving a total of 220 bytes.
3350        Problem is, 220 isn't frame (16 byte) aligned.  Round it up to
3351        224.  */
3352     set_gdbarch_frame_red_zone_size (gdbarch, 224);
3353
3354   set_gdbarch_convert_register_p (gdbarch, rs6000_convert_register_p);
3355   set_gdbarch_register_to_value (gdbarch, rs6000_register_to_value);
3356   set_gdbarch_value_to_register (gdbarch, rs6000_value_to_register);
3357
3358   set_gdbarch_stab_reg_to_regnum (gdbarch, rs6000_stab_reg_to_regnum);
3359   set_gdbarch_dwarf2_reg_to_regnum (gdbarch, rs6000_dwarf2_reg_to_regnum);
3360
3361   if (sysv_abi && wordsize == 4)
3362     set_gdbarch_push_dummy_call (gdbarch, ppc_sysv_abi_push_dummy_call);
3363   else if (sysv_abi && wordsize == 8)
3364     set_gdbarch_push_dummy_call (gdbarch, ppc64_sysv_abi_push_dummy_call);
3365   else
3366     set_gdbarch_push_dummy_call (gdbarch, rs6000_push_dummy_call);
3367
3368   set_gdbarch_skip_prologue (gdbarch, rs6000_skip_prologue);
3369   set_gdbarch_in_function_epilogue_p (gdbarch, rs6000_in_function_epilogue_p);
3370
3371   set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
3372   set_gdbarch_breakpoint_from_pc (gdbarch, rs6000_breakpoint_from_pc);
3373
3374   /* The value of symbols of type N_SO and N_FUN maybe null when
3375      it shouldn't be. */
3376   set_gdbarch_sofun_address_maybe_missing (gdbarch, 1);
3377
3378   /* Handles single stepping of atomic sequences.  */
3379   set_gdbarch_software_single_step (gdbarch, deal_with_atomic_sequence);
3380   
3381   /* Handle the 64-bit SVR4 minimal-symbol convention of using "FN"
3382      for the descriptor and ".FN" for the entry-point -- a user
3383      specifying "break FN" will unexpectedly end up with a breakpoint
3384      on the descriptor and not the function.  This architecture method
3385      transforms any breakpoints on descriptors into breakpoints on the
3386      corresponding entry point.  */
3387   if (sysv_abi && wordsize == 8)
3388     set_gdbarch_adjust_breakpoint_address (gdbarch, ppc64_sysv_abi_adjust_breakpoint_address);
3389
3390   /* Not sure on this. FIXMEmgo */
3391   set_gdbarch_frame_args_skip (gdbarch, 8);
3392
3393   if (!sysv_abi)
3394     {
3395       /* Handle RS/6000 function pointers (which are really function
3396          descriptors).  */
3397       set_gdbarch_convert_from_func_ptr_addr (gdbarch,
3398         rs6000_convert_from_func_ptr_addr);
3399     }
3400
3401   /* Helpers for function argument information.  */
3402   set_gdbarch_fetch_pointer_argument (gdbarch, rs6000_fetch_pointer_argument);
3403
3404   /* Trampoline.  */
3405   set_gdbarch_in_solib_return_trampoline
3406     (gdbarch, rs6000_in_solib_return_trampoline);
3407   set_gdbarch_skip_trampoline_code (gdbarch, rs6000_skip_trampoline_code);
3408
3409   /* Hook in the DWARF CFI frame unwinder.  */
3410   frame_unwind_append_sniffer (gdbarch, dwarf2_frame_sniffer);
3411   dwarf2_frame_set_adjust_regnum (gdbarch, rs6000_adjust_frame_regnum);
3412
3413   /* Hook in ABI-specific overrides, if they have been registered.  */
3414   gdbarch_init_osabi (info, gdbarch);
3415
3416   switch (info.osabi)
3417     {
3418     case GDB_OSABI_LINUX:
3419     case GDB_OSABI_NETBSD_AOUT:
3420     case GDB_OSABI_NETBSD_ELF:
3421     case GDB_OSABI_UNKNOWN:
3422       set_gdbarch_unwind_pc (gdbarch, rs6000_unwind_pc);
3423       frame_unwind_append_sniffer (gdbarch, rs6000_frame_sniffer);
3424       set_gdbarch_unwind_dummy_id (gdbarch, rs6000_unwind_dummy_id);
3425       frame_base_append_sniffer (gdbarch, rs6000_frame_base_sniffer);
3426       break;
3427     default:
3428       set_gdbarch_believe_pcc_promotion (gdbarch, 1);
3429
3430       set_gdbarch_unwind_pc (gdbarch, rs6000_unwind_pc);
3431       frame_unwind_append_sniffer (gdbarch, rs6000_frame_sniffer);
3432       set_gdbarch_unwind_dummy_id (gdbarch, rs6000_unwind_dummy_id);
3433       frame_base_append_sniffer (gdbarch, rs6000_frame_base_sniffer);
3434     }
3435
3436   set_tdesc_pseudo_register_type (gdbarch, rs6000_pseudo_register_type);
3437   set_tdesc_pseudo_register_reggroup_p (gdbarch,
3438                                         rs6000_pseudo_register_reggroup_p);
3439   tdesc_use_registers (gdbarch, tdesc, tdesc_data);
3440
3441   /* Override the normal target description method to make the SPE upper
3442      halves anonymous.  */
3443   set_gdbarch_register_name (gdbarch, rs6000_register_name);
3444
3445   /* Recording the numbering of pseudo registers.  */
3446   tdep->ppc_ev0_regnum = have_spe ? gdbarch_num_regs (gdbarch) : -1;
3447   tdep->ppc_ev31_regnum = have_spe ? tdep->ppc_ev0_regnum + 31 : -1;
3448
3449   return gdbarch;
3450 }
3451
3452 static void
3453 rs6000_dump_tdep (struct gdbarch *gdbarch, struct ui_file *file)
3454 {
3455   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
3456
3457   if (tdep == NULL)
3458     return;
3459
3460   /* FIXME: Dump gdbarch_tdep.  */
3461 }
3462
3463 /* Initialization code.  */
3464
3465 extern initialize_file_ftype _initialize_rs6000_tdep; /* -Wmissing-prototypes */
3466
3467 void
3468 _initialize_rs6000_tdep (void)
3469 {
3470   gdbarch_register (bfd_arch_rs6000, rs6000_gdbarch_init, rs6000_dump_tdep);
3471   gdbarch_register (bfd_arch_powerpc, rs6000_gdbarch_init, rs6000_dump_tdep);
3472
3473   /* Initialize the standard target descriptions.  */
3474   initialize_tdesc_powerpc_32 ();
3475   initialize_tdesc_powerpc_403 ();
3476   initialize_tdesc_powerpc_403gc ();
3477   initialize_tdesc_powerpc_505 ();
3478   initialize_tdesc_powerpc_601 ();
3479   initialize_tdesc_powerpc_602 ();
3480   initialize_tdesc_powerpc_603 ();
3481   initialize_tdesc_powerpc_604 ();
3482   initialize_tdesc_powerpc_64 ();
3483   initialize_tdesc_powerpc_7400 ();
3484   initialize_tdesc_powerpc_750 ();
3485   initialize_tdesc_powerpc_860 ();
3486   initialize_tdesc_powerpc_e500 ();
3487   initialize_tdesc_rs6000 ();
3488 }