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