rs6000: Add cast for int to enum conversion
[external/binutils.git] / gdb / rs6000-tdep.c
1 /* Target-dependent code for GDB, the GNU debugger.
2
3    Copyright (C) 1986-2015 Free Software Foundation, Inc.
4
5    This file is part of GDB.
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
19
20 #include "defs.h"
21 #include "frame.h"
22 #include "inferior.h"
23 #include "infrun.h"
24 #include "symtab.h"
25 #include "target.h"
26 #include "gdbcore.h"
27 #include "gdbcmd.h"
28 #include "objfiles.h"
29 #include "arch-utils.h"
30 #include "regcache.h"
31 #include "regset.h"
32 #include "doublest.h"
33 #include "value.h"
34 #include "parser-defs.h"
35 #include "osabi.h"
36 #include "infcall.h"
37 #include "sim-regno.h"
38 #include "gdb/sim-ppc.h"
39 #include "reggroups.h"
40 #include "dwarf2-frame.h"
41 #include "target-descriptions.h"
42 #include "user-regs.h"
43 #include "record-full.h"
44 #include "auxv.h"
45
46 #include "libbfd.h"             /* for bfd_default_set_arch_mach */
47 #include "coff/internal.h"      /* for libcoff.h */
48 #include "libcoff.h"            /* for xcoff_data */
49 #include "coff/xcoff.h"
50 #include "libxcoff.h"
51
52 #include "elf-bfd.h"
53 #include "elf/ppc.h"
54 #include "elf/ppc64.h"
55
56 #include "solib-svr4.h"
57 #include "ppc-tdep.h"
58 #include "ppc-ravenscar-thread.h"
59
60 #include "dis-asm.h"
61
62 #include "trad-frame.h"
63 #include "frame-unwind.h"
64 #include "frame-base.h"
65
66 #include "features/rs6000/powerpc-32.c"
67 #include "features/rs6000/powerpc-altivec32.c"
68 #include "features/rs6000/powerpc-vsx32.c"
69 #include "features/rs6000/powerpc-403.c"
70 #include "features/rs6000/powerpc-403gc.c"
71 #include "features/rs6000/powerpc-405.c"
72 #include "features/rs6000/powerpc-505.c"
73 #include "features/rs6000/powerpc-601.c"
74 #include "features/rs6000/powerpc-602.c"
75 #include "features/rs6000/powerpc-603.c"
76 #include "features/rs6000/powerpc-604.c"
77 #include "features/rs6000/powerpc-64.c"
78 #include "features/rs6000/powerpc-altivec64.c"
79 #include "features/rs6000/powerpc-vsx64.c"
80 #include "features/rs6000/powerpc-7400.c"
81 #include "features/rs6000/powerpc-750.c"
82 #include "features/rs6000/powerpc-860.c"
83 #include "features/rs6000/powerpc-e500.c"
84 #include "features/rs6000/rs6000.c"
85
86 /* Determine if regnum is an SPE pseudo-register.  */
87 #define IS_SPE_PSEUDOREG(tdep, regnum) ((tdep)->ppc_ev0_regnum >= 0 \
88     && (regnum) >= (tdep)->ppc_ev0_regnum \
89     && (regnum) < (tdep)->ppc_ev0_regnum + 32)
90
91 /* Determine if regnum is a decimal float pseudo-register.  */
92 #define IS_DFP_PSEUDOREG(tdep, regnum) ((tdep)->ppc_dl0_regnum >= 0 \
93     && (regnum) >= (tdep)->ppc_dl0_regnum \
94     && (regnum) < (tdep)->ppc_dl0_regnum + 16)
95
96 /* Determine if regnum is a POWER7 VSX register.  */
97 #define IS_VSX_PSEUDOREG(tdep, regnum) ((tdep)->ppc_vsr0_regnum >= 0 \
98     && (regnum) >= (tdep)->ppc_vsr0_regnum \
99     && (regnum) < (tdep)->ppc_vsr0_regnum + ppc_num_vsrs)
100
101 /* Determine if regnum is a POWER7 Extended FP register.  */
102 #define IS_EFP_PSEUDOREG(tdep, regnum) ((tdep)->ppc_efpr0_regnum >= 0 \
103     && (regnum) >= (tdep)->ppc_efpr0_regnum \
104     && (regnum) < (tdep)->ppc_efpr0_regnum + ppc_num_efprs)
105
106 /* The list of available "set powerpc ..." and "show powerpc ..."
107    commands.  */
108 static struct cmd_list_element *setpowerpccmdlist = NULL;
109 static struct cmd_list_element *showpowerpccmdlist = NULL;
110
111 static enum auto_boolean powerpc_soft_float_global = AUTO_BOOLEAN_AUTO;
112
113 /* The vector ABI to use.  Keep this in sync with powerpc_vector_abi.  */
114 static const char *const powerpc_vector_strings[] =
115 {
116   "auto",
117   "generic",
118   "altivec",
119   "spe",
120   NULL
121 };
122
123 /* A variable that can be configured by the user.  */
124 static enum powerpc_vector_abi powerpc_vector_abi_global = POWERPC_VEC_AUTO;
125 static const char *powerpc_vector_abi_string = "auto";
126
127 /* To be used by skip_prologue.  */
128
129 struct rs6000_framedata
130   {
131     int offset;                 /* total size of frame --- the distance
132                                    by which we decrement sp to allocate
133                                    the frame */
134     int saved_gpr;              /* smallest # of saved gpr */
135     unsigned int gpr_mask;      /* Each bit is an individual saved GPR.  */
136     int saved_fpr;              /* smallest # of saved fpr */
137     int saved_vr;               /* smallest # of saved vr */
138     int saved_ev;               /* smallest # of saved ev */
139     int alloca_reg;             /* alloca register number (frame ptr) */
140     char frameless;             /* true if frameless functions.  */
141     char nosavedpc;             /* true if pc not saved.  */
142     char used_bl;               /* true if link register clobbered */
143     int gpr_offset;             /* offset of saved gprs from prev sp */
144     int fpr_offset;             /* offset of saved fprs from prev sp */
145     int vr_offset;              /* offset of saved vrs from prev sp */
146     int ev_offset;              /* offset of saved evs from prev sp */
147     int lr_offset;              /* offset of saved lr */
148     int lr_register;            /* register of saved lr, if trustworthy */
149     int cr_offset;              /* offset of saved cr */
150     int vrsave_offset;          /* offset of saved vrsave register */
151   };
152
153
154 /* Is REGNO a VSX register? Return 1 if so, 0 otherwise.  */
155 int
156 vsx_register_p (struct gdbarch *gdbarch, int regno)
157 {
158   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
159   if (tdep->ppc_vsr0_regnum < 0)
160     return 0;
161   else
162     return (regno >= tdep->ppc_vsr0_upper_regnum && regno
163             <= tdep->ppc_vsr0_upper_regnum + 31);
164 }
165
166 /* Is REGNO an AltiVec register?  Return 1 if so, 0 otherwise.  */
167 int
168 altivec_register_p (struct gdbarch *gdbarch, int regno)
169 {
170   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
171   if (tdep->ppc_vr0_regnum < 0 || tdep->ppc_vrsave_regnum < 0)
172     return 0;
173   else
174     return (regno >= tdep->ppc_vr0_regnum && regno <= tdep->ppc_vrsave_regnum);
175 }
176
177
178 /* Return true if REGNO is an SPE register, false otherwise.  */
179 int
180 spe_register_p (struct gdbarch *gdbarch, int regno)
181 {
182   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
183   
184   /* Is it a reference to EV0 -- EV31, and do we have those?  */
185   if (IS_SPE_PSEUDOREG (tdep, regno))
186     return 1;
187
188   /* Is it a reference to one of the raw upper GPR halves?  */
189   if (tdep->ppc_ev0_upper_regnum >= 0
190       && tdep->ppc_ev0_upper_regnum <= regno
191       && regno < tdep->ppc_ev0_upper_regnum + ppc_num_gprs)
192     return 1;
193
194   /* Is it a reference to the 64-bit accumulator, and do we have that?  */
195   if (tdep->ppc_acc_regnum >= 0
196       && tdep->ppc_acc_regnum == regno)
197     return 1;
198
199   /* Is it a reference to the SPE floating-point status and control register,
200      and do we have that?  */
201   if (tdep->ppc_spefscr_regnum >= 0
202       && tdep->ppc_spefscr_regnum == regno)
203     return 1;
204
205   return 0;
206 }
207
208
209 /* Return non-zero if the architecture described by GDBARCH has
210    floating-point registers (f0 --- f31 and fpscr).  */
211 int
212 ppc_floating_point_unit_p (struct gdbarch *gdbarch)
213 {
214   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
215
216   return (tdep->ppc_fp0_regnum >= 0
217           && tdep->ppc_fpscr_regnum >= 0);
218 }
219
220 /* Return non-zero if the architecture described by GDBARCH has
221    VSX registers (vsr0 --- vsr63).  */
222 static int
223 ppc_vsx_support_p (struct gdbarch *gdbarch)
224 {
225   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
226
227   return tdep->ppc_vsr0_regnum >= 0;
228 }
229
230 /* Return non-zero if the architecture described by GDBARCH has
231    Altivec registers (vr0 --- vr31, vrsave and vscr).  */
232 int
233 ppc_altivec_support_p (struct gdbarch *gdbarch)
234 {
235   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
236
237   return (tdep->ppc_vr0_regnum >= 0
238           && tdep->ppc_vrsave_regnum >= 0);
239 }
240
241 /* Check that TABLE[GDB_REGNO] is not already initialized, and then
242    set it to SIM_REGNO.
243
244    This is a helper function for init_sim_regno_table, constructing
245    the table mapping GDB register numbers to sim register numbers; we
246    initialize every element in that table to -1 before we start
247    filling it in.  */
248 static void
249 set_sim_regno (int *table, int gdb_regno, int sim_regno)
250 {
251   /* Make sure we don't try to assign any given GDB register a sim
252      register number more than once.  */
253   gdb_assert (table[gdb_regno] == -1);
254   table[gdb_regno] = sim_regno;
255 }
256
257
258 /* Initialize ARCH->tdep->sim_regno, the table mapping GDB register
259    numbers to simulator register numbers, based on the values placed
260    in the ARCH->tdep->ppc_foo_regnum members.  */
261 static void
262 init_sim_regno_table (struct gdbarch *arch)
263 {
264   struct gdbarch_tdep *tdep = gdbarch_tdep (arch);
265   int total_regs = gdbarch_num_regs (arch);
266   int *sim_regno = GDBARCH_OBSTACK_CALLOC (arch, total_regs, int);
267   int i;
268   static const char *const segment_regs[] = {
269     "sr0", "sr1", "sr2", "sr3", "sr4", "sr5", "sr6", "sr7",
270     "sr8", "sr9", "sr10", "sr11", "sr12", "sr13", "sr14", "sr15"
271   };
272
273   /* Presume that all registers not explicitly mentioned below are
274      unavailable from the sim.  */
275   for (i = 0; i < total_regs; i++)
276     sim_regno[i] = -1;
277
278   /* General-purpose registers.  */
279   for (i = 0; i < ppc_num_gprs; i++)
280     set_sim_regno (sim_regno, tdep->ppc_gp0_regnum + i, sim_ppc_r0_regnum + i);
281   
282   /* Floating-point registers.  */
283   if (tdep->ppc_fp0_regnum >= 0)
284     for (i = 0; i < ppc_num_fprs; i++)
285       set_sim_regno (sim_regno,
286                      tdep->ppc_fp0_regnum + i,
287                      sim_ppc_f0_regnum + i);
288   if (tdep->ppc_fpscr_regnum >= 0)
289     set_sim_regno (sim_regno, tdep->ppc_fpscr_regnum, sim_ppc_fpscr_regnum);
290
291   set_sim_regno (sim_regno, gdbarch_pc_regnum (arch), sim_ppc_pc_regnum);
292   set_sim_regno (sim_regno, tdep->ppc_ps_regnum, sim_ppc_ps_regnum);
293   set_sim_regno (sim_regno, tdep->ppc_cr_regnum, sim_ppc_cr_regnum);
294
295   /* Segment registers.  */
296   for (i = 0; i < ppc_num_srs; i++)
297     {
298       int gdb_regno;
299
300       gdb_regno = user_reg_map_name_to_regnum (arch, segment_regs[i], -1);
301       if (gdb_regno >= 0)
302         set_sim_regno (sim_regno, gdb_regno, sim_ppc_sr0_regnum + i);
303     }
304
305   /* Altivec registers.  */
306   if (tdep->ppc_vr0_regnum >= 0)
307     {
308       for (i = 0; i < ppc_num_vrs; i++)
309         set_sim_regno (sim_regno,
310                        tdep->ppc_vr0_regnum + i,
311                        sim_ppc_vr0_regnum + i);
312
313       /* FIXME: jimb/2004-07-15: when we have tdep->ppc_vscr_regnum,
314          we can treat this more like the other cases.  */
315       set_sim_regno (sim_regno,
316                      tdep->ppc_vr0_regnum + ppc_num_vrs,
317                      sim_ppc_vscr_regnum);
318     }
319   /* vsave is a special-purpose register, so the code below handles it.  */
320
321   /* SPE APU (E500) registers.  */
322   if (tdep->ppc_ev0_upper_regnum >= 0)
323     for (i = 0; i < ppc_num_gprs; i++)
324       set_sim_regno (sim_regno,
325                      tdep->ppc_ev0_upper_regnum + i,
326                      sim_ppc_rh0_regnum + i);
327   if (tdep->ppc_acc_regnum >= 0)
328     set_sim_regno (sim_regno, tdep->ppc_acc_regnum, sim_ppc_acc_regnum);
329   /* spefscr is a special-purpose register, so the code below handles it.  */
330
331 #ifdef WITH_SIM
332   /* Now handle all special-purpose registers.  Verify that they
333      haven't mistakenly been assigned numbers by any of the above
334      code.  */
335   for (i = 0; i < sim_ppc_num_sprs; i++)
336     {
337       const char *spr_name = sim_spr_register_name (i);
338       int gdb_regno = -1;
339
340       if (spr_name != NULL)
341         gdb_regno = user_reg_map_name_to_regnum (arch, spr_name, -1);
342
343       if (gdb_regno != -1)
344         set_sim_regno (sim_regno, gdb_regno, sim_ppc_spr0_regnum + i);
345     }
346 #endif
347
348   /* Drop the initialized array into place.  */
349   tdep->sim_regno = sim_regno;
350 }
351
352
353 /* Given a GDB register number REG, return the corresponding SIM
354    register number.  */
355 static int
356 rs6000_register_sim_regno (struct gdbarch *gdbarch, int reg)
357 {
358   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
359   int sim_regno;
360
361   if (tdep->sim_regno == NULL)
362     init_sim_regno_table (gdbarch);
363
364   gdb_assert (0 <= reg 
365               && reg <= gdbarch_num_regs (gdbarch)
366                         + gdbarch_num_pseudo_regs (gdbarch));
367   sim_regno = tdep->sim_regno[reg];
368
369   if (sim_regno >= 0)
370     return sim_regno;
371   else
372     return LEGACY_SIM_REGNO_IGNORE;
373 }
374
375 \f
376
377 /* Register set support functions.  */
378
379 /* REGS + OFFSET contains register REGNUM in a field REGSIZE wide.
380    Write the register to REGCACHE.  */
381
382 void
383 ppc_supply_reg (struct regcache *regcache, int regnum, 
384                 const gdb_byte *regs, size_t offset, int regsize)
385 {
386   if (regnum != -1 && offset != -1)
387     {
388       if (regsize > 4)
389         {
390           struct gdbarch *gdbarch = get_regcache_arch (regcache);
391           int gdb_regsize = register_size (gdbarch, regnum);
392           if (gdb_regsize < regsize
393               && gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
394             offset += regsize - gdb_regsize;
395         }
396       regcache_raw_supply (regcache, regnum, regs + offset);
397     }
398 }
399
400 /* Read register REGNUM from REGCACHE and store to REGS + OFFSET
401    in a field REGSIZE wide.  Zero pad as necessary.  */
402
403 void
404 ppc_collect_reg (const struct regcache *regcache, int regnum,
405                  gdb_byte *regs, size_t offset, int regsize)
406 {
407   if (regnum != -1 && offset != -1)
408     {
409       if (regsize > 4)
410         {
411           struct gdbarch *gdbarch = get_regcache_arch (regcache);
412           int gdb_regsize = register_size (gdbarch, regnum);
413           if (gdb_regsize < regsize)
414             {
415               if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
416                 {
417                   memset (regs + offset, 0, regsize - gdb_regsize);
418                   offset += regsize - gdb_regsize;
419                 }
420               else
421                 memset (regs + offset + regsize - gdb_regsize, 0,
422                         regsize - gdb_regsize);
423             }
424         }
425       regcache_raw_collect (regcache, regnum, regs + offset);
426     }
427 }
428     
429 static int
430 ppc_greg_offset (struct gdbarch *gdbarch,
431                  struct gdbarch_tdep *tdep,
432                  const struct ppc_reg_offsets *offsets,
433                  int regnum,
434                  int *regsize)
435 {
436   *regsize = offsets->gpr_size;
437   if (regnum >= tdep->ppc_gp0_regnum
438       && regnum < tdep->ppc_gp0_regnum + ppc_num_gprs)
439     return (offsets->r0_offset
440             + (regnum - tdep->ppc_gp0_regnum) * offsets->gpr_size);
441
442   if (regnum == gdbarch_pc_regnum (gdbarch))
443     return offsets->pc_offset;
444
445   if (regnum == tdep->ppc_ps_regnum)
446     return offsets->ps_offset;
447
448   if (regnum == tdep->ppc_lr_regnum)
449     return offsets->lr_offset;
450
451   if (regnum == tdep->ppc_ctr_regnum)
452     return offsets->ctr_offset;
453
454   *regsize = offsets->xr_size;
455   if (regnum == tdep->ppc_cr_regnum)
456     return offsets->cr_offset;
457
458   if (regnum == tdep->ppc_xer_regnum)
459     return offsets->xer_offset;
460
461   if (regnum == tdep->ppc_mq_regnum)
462     return offsets->mq_offset;
463
464   return -1;
465 }
466
467 static int
468 ppc_fpreg_offset (struct gdbarch_tdep *tdep,
469                   const struct ppc_reg_offsets *offsets,
470                   int regnum)
471 {
472   if (regnum >= tdep->ppc_fp0_regnum
473       && regnum < tdep->ppc_fp0_regnum + ppc_num_fprs)
474     return offsets->f0_offset + (regnum - tdep->ppc_fp0_regnum) * 8;
475
476   if (regnum == tdep->ppc_fpscr_regnum)
477     return offsets->fpscr_offset;
478
479   return -1;
480 }
481
482 static int
483 ppc_vrreg_offset (struct gdbarch_tdep *tdep,
484                   const struct ppc_reg_offsets *offsets,
485                   int regnum)
486 {
487   if (regnum >= tdep->ppc_vr0_regnum
488       && regnum < tdep->ppc_vr0_regnum + ppc_num_vrs)
489     return offsets->vr0_offset + (regnum - tdep->ppc_vr0_regnum) * 16;
490
491   if (regnum == tdep->ppc_vrsave_regnum - 1)
492     return offsets->vscr_offset;
493
494   if (regnum == tdep->ppc_vrsave_regnum)
495     return offsets->vrsave_offset;
496
497   return -1;
498 }
499
500 /* Supply register REGNUM in the general-purpose register set REGSET
501    from the buffer specified by GREGS and LEN to register cache
502    REGCACHE.  If REGNUM is -1, do this for all registers in REGSET.  */
503
504 void
505 ppc_supply_gregset (const struct regset *regset, struct regcache *regcache,
506                     int regnum, const void *gregs, size_t len)
507 {
508   struct gdbarch *gdbarch = get_regcache_arch (regcache);
509   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
510   const struct ppc_reg_offsets *offsets
511     = (const struct ppc_reg_offsets *) regset->regmap;
512   size_t offset;
513   int regsize;
514
515   if (regnum == -1)
516     {
517       int i;
518       int gpr_size = offsets->gpr_size;
519
520       for (i = tdep->ppc_gp0_regnum, offset = offsets->r0_offset;
521            i < tdep->ppc_gp0_regnum + ppc_num_gprs;
522            i++, offset += gpr_size)
523         ppc_supply_reg (regcache, i, (const gdb_byte *) gregs, offset,
524                         gpr_size);
525
526       ppc_supply_reg (regcache, gdbarch_pc_regnum (gdbarch),
527                       (const gdb_byte *) gregs, offsets->pc_offset, gpr_size);
528       ppc_supply_reg (regcache, tdep->ppc_ps_regnum,
529                       (const gdb_byte *) gregs, offsets->ps_offset, gpr_size);
530       ppc_supply_reg (regcache, tdep->ppc_lr_regnum,
531                       (const gdb_byte *) gregs, offsets->lr_offset, gpr_size);
532       ppc_supply_reg (regcache, tdep->ppc_ctr_regnum,
533                       (const gdb_byte *) gregs, offsets->ctr_offset, gpr_size);
534       ppc_supply_reg (regcache, tdep->ppc_cr_regnum,
535                       (const gdb_byte *) gregs, offsets->cr_offset,
536                       offsets->xr_size);
537       ppc_supply_reg (regcache, tdep->ppc_xer_regnum,
538                       (const gdb_byte *) gregs, offsets->xer_offset,
539                       offsets->xr_size);
540       ppc_supply_reg (regcache, tdep->ppc_mq_regnum,
541                       (const gdb_byte *) gregs, offsets->mq_offset,
542                       offsets->xr_size);
543       return;
544     }
545
546   offset = ppc_greg_offset (gdbarch, tdep, offsets, regnum, &regsize);
547   ppc_supply_reg (regcache, regnum, (const gdb_byte *) gregs, offset, regsize);
548 }
549
550 /* Supply register REGNUM in the floating-point register set REGSET
551    from the buffer specified by FPREGS and LEN to register cache
552    REGCACHE.  If REGNUM is -1, do this for all registers in REGSET.  */
553
554 void
555 ppc_supply_fpregset (const struct regset *regset, struct regcache *regcache,
556                      int regnum, const void *fpregs, size_t len)
557 {
558   struct gdbarch *gdbarch = get_regcache_arch (regcache);
559   struct gdbarch_tdep *tdep;
560   const struct ppc_reg_offsets *offsets;
561   size_t offset;
562
563   if (!ppc_floating_point_unit_p (gdbarch))
564     return;
565
566   tdep = gdbarch_tdep (gdbarch);
567   offsets = (const struct ppc_reg_offsets *) regset->regmap;
568   if (regnum == -1)
569     {
570       int i;
571
572       for (i = tdep->ppc_fp0_regnum, offset = offsets->f0_offset;
573            i < tdep->ppc_fp0_regnum + ppc_num_fprs;
574            i++, offset += 8)
575         ppc_supply_reg (regcache, i, (const gdb_byte *) fpregs, offset, 8);
576
577       ppc_supply_reg (regcache, tdep->ppc_fpscr_regnum,
578                       (const gdb_byte *) fpregs, offsets->fpscr_offset,
579                       offsets->fpscr_size);
580       return;
581     }
582
583   offset = ppc_fpreg_offset (tdep, offsets, regnum);
584   ppc_supply_reg (regcache, regnum, (const gdb_byte *) fpregs, offset,
585                   regnum == tdep->ppc_fpscr_regnum ? offsets->fpscr_size : 8);
586 }
587
588 /* Supply register REGNUM in the VSX register set REGSET
589    from the buffer specified by VSXREGS and LEN to register cache
590    REGCACHE.  If REGNUM is -1, do this for all registers in REGSET.  */
591
592 void
593 ppc_supply_vsxregset (const struct regset *regset, struct regcache *regcache,
594                      int regnum, const void *vsxregs, size_t len)
595 {
596   struct gdbarch *gdbarch = get_regcache_arch (regcache);
597   struct gdbarch_tdep *tdep;
598
599   if (!ppc_vsx_support_p (gdbarch))
600     return;
601
602   tdep = gdbarch_tdep (gdbarch);
603
604   if (regnum == -1)
605     {
606       int i;
607
608       for (i = tdep->ppc_vsr0_upper_regnum;
609            i < tdep->ppc_vsr0_upper_regnum + 32;
610            i++)
611         ppc_supply_reg (regcache, i, (const gdb_byte *) vsxregs, 0, 8);
612
613       return;
614     }
615   else
616     ppc_supply_reg (regcache, regnum, (const gdb_byte *) vsxregs, 0, 8);
617 }
618
619 /* Supply register REGNUM in the Altivec register set REGSET
620    from the buffer specified by VRREGS and LEN to register cache
621    REGCACHE.  If REGNUM is -1, do this for all registers in REGSET.  */
622
623 void
624 ppc_supply_vrregset (const struct regset *regset, struct regcache *regcache,
625                      int regnum, const void *vrregs, size_t len)
626 {
627   struct gdbarch *gdbarch = get_regcache_arch (regcache);
628   struct gdbarch_tdep *tdep;
629   const struct ppc_reg_offsets *offsets;
630   size_t offset;
631
632   if (!ppc_altivec_support_p (gdbarch))
633     return;
634
635   tdep = gdbarch_tdep (gdbarch);
636   offsets = (const struct ppc_reg_offsets *) regset->regmap;
637   if (regnum == -1)
638     {
639       int i;
640
641       for (i = tdep->ppc_vr0_regnum, offset = offsets->vr0_offset;
642            i < tdep->ppc_vr0_regnum + ppc_num_vrs;
643            i++, offset += 16)
644         ppc_supply_reg (regcache, i, (const gdb_byte *) vrregs, offset, 16);
645
646       ppc_supply_reg (regcache, (tdep->ppc_vrsave_regnum - 1),
647                       (const gdb_byte *) vrregs, offsets->vscr_offset, 4);
648
649       ppc_supply_reg (regcache, tdep->ppc_vrsave_regnum,
650                       (const gdb_byte *) vrregs, offsets->vrsave_offset, 4);
651       return;
652     }
653
654   offset = ppc_vrreg_offset (tdep, offsets, regnum);
655   if (regnum != tdep->ppc_vrsave_regnum
656       && regnum != tdep->ppc_vrsave_regnum - 1)
657     ppc_supply_reg (regcache, regnum, (const gdb_byte *) vrregs, offset, 16);
658   else
659     ppc_supply_reg (regcache, regnum,
660                     (const gdb_byte *) vrregs, offset, 4);
661 }
662
663 /* Collect register REGNUM in the general-purpose register set
664    REGSET from register cache REGCACHE into the buffer specified by
665    GREGS and LEN.  If REGNUM is -1, do this for all registers in
666    REGSET.  */
667
668 void
669 ppc_collect_gregset (const struct regset *regset,
670                      const struct regcache *regcache,
671                      int regnum, void *gregs, size_t len)
672 {
673   struct gdbarch *gdbarch = get_regcache_arch (regcache);
674   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
675   const struct ppc_reg_offsets *offsets
676     = (const struct ppc_reg_offsets *) regset->regmap;
677   size_t offset;
678   int regsize;
679
680   if (regnum == -1)
681     {
682       int i;
683       int gpr_size = offsets->gpr_size;
684
685       for (i = tdep->ppc_gp0_regnum, offset = offsets->r0_offset;
686            i < tdep->ppc_gp0_regnum + ppc_num_gprs;
687            i++, offset += gpr_size)
688         ppc_collect_reg (regcache, i, (gdb_byte *) gregs, offset, gpr_size);
689
690       ppc_collect_reg (regcache, gdbarch_pc_regnum (gdbarch),
691                        (gdb_byte *) gregs, offsets->pc_offset, gpr_size);
692       ppc_collect_reg (regcache, tdep->ppc_ps_regnum,
693                        (gdb_byte *) gregs, offsets->ps_offset, gpr_size);
694       ppc_collect_reg (regcache, tdep->ppc_lr_regnum,
695                        (gdb_byte *) gregs, offsets->lr_offset, gpr_size);
696       ppc_collect_reg (regcache, tdep->ppc_ctr_regnum,
697                        (gdb_byte *) gregs, offsets->ctr_offset, gpr_size);
698       ppc_collect_reg (regcache, tdep->ppc_cr_regnum,
699                        (gdb_byte *) gregs, offsets->cr_offset,
700                        offsets->xr_size);
701       ppc_collect_reg (regcache, tdep->ppc_xer_regnum,
702                        (gdb_byte *) gregs, offsets->xer_offset,
703                        offsets->xr_size);
704       ppc_collect_reg (regcache, tdep->ppc_mq_regnum,
705                        (gdb_byte *) gregs, offsets->mq_offset,
706                        offsets->xr_size);
707       return;
708     }
709
710   offset = ppc_greg_offset (gdbarch, tdep, offsets, regnum, &regsize);
711   ppc_collect_reg (regcache, regnum, (gdb_byte *) gregs, offset, regsize);
712 }
713
714 /* Collect register REGNUM in the floating-point register set
715    REGSET from register cache REGCACHE into the buffer specified by
716    FPREGS and LEN.  If REGNUM is -1, do this for all registers in
717    REGSET.  */
718
719 void
720 ppc_collect_fpregset (const struct regset *regset,
721                       const struct regcache *regcache,
722                       int regnum, void *fpregs, size_t len)
723 {
724   struct gdbarch *gdbarch = get_regcache_arch (regcache);
725   struct gdbarch_tdep *tdep;
726   const struct ppc_reg_offsets *offsets;
727   size_t offset;
728
729   if (!ppc_floating_point_unit_p (gdbarch))
730     return;
731
732   tdep = gdbarch_tdep (gdbarch);
733   offsets = (const struct ppc_reg_offsets *) regset->regmap;
734   if (regnum == -1)
735     {
736       int i;
737
738       for (i = tdep->ppc_fp0_regnum, offset = offsets->f0_offset;
739            i < tdep->ppc_fp0_regnum + ppc_num_fprs;
740            i++, offset += 8)
741         ppc_collect_reg (regcache, i, (gdb_byte *) fpregs, offset, 8);
742
743       ppc_collect_reg (regcache, tdep->ppc_fpscr_regnum,
744                        (gdb_byte *) fpregs, offsets->fpscr_offset,
745                        offsets->fpscr_size);
746       return;
747     }
748
749   offset = ppc_fpreg_offset (tdep, offsets, regnum);
750   ppc_collect_reg (regcache, regnum, (gdb_byte *) fpregs, offset,
751                    regnum == tdep->ppc_fpscr_regnum ? offsets->fpscr_size : 8);
752 }
753
754 /* Collect register REGNUM in the VSX register set
755    REGSET from register cache REGCACHE into the buffer specified by
756    VSXREGS and LEN.  If REGNUM is -1, do this for all registers in
757    REGSET.  */
758
759 void
760 ppc_collect_vsxregset (const struct regset *regset,
761                       const struct regcache *regcache,
762                       int regnum, void *vsxregs, size_t len)
763 {
764   struct gdbarch *gdbarch = get_regcache_arch (regcache);
765   struct gdbarch_tdep *tdep;
766
767   if (!ppc_vsx_support_p (gdbarch))
768     return;
769
770   tdep = gdbarch_tdep (gdbarch);
771
772   if (regnum == -1)
773     {
774       int i;
775
776       for (i = tdep->ppc_vsr0_upper_regnum;
777            i < tdep->ppc_vsr0_upper_regnum + 32;
778            i++)
779         ppc_collect_reg (regcache, i, (gdb_byte *) vsxregs, 0, 8);
780
781       return;
782     }
783   else
784     ppc_collect_reg (regcache, regnum, (gdb_byte *) vsxregs, 0, 8);
785 }
786
787
788 /* Collect register REGNUM in the Altivec register set
789    REGSET from register cache REGCACHE into the buffer specified by
790    VRREGS and LEN.  If REGNUM is -1, do this for all registers in
791    REGSET.  */
792
793 void
794 ppc_collect_vrregset (const struct regset *regset,
795                       const struct regcache *regcache,
796                       int regnum, void *vrregs, size_t len)
797 {
798   struct gdbarch *gdbarch = get_regcache_arch (regcache);
799   struct gdbarch_tdep *tdep;
800   const struct ppc_reg_offsets *offsets;
801   size_t offset;
802
803   if (!ppc_altivec_support_p (gdbarch))
804     return;
805
806   tdep = gdbarch_tdep (gdbarch);
807   offsets = (const struct ppc_reg_offsets *) regset->regmap;
808   if (regnum == -1)
809     {
810       int i;
811
812       for (i = tdep->ppc_vr0_regnum, offset = offsets->vr0_offset;
813            i < tdep->ppc_vr0_regnum + ppc_num_vrs;
814            i++, offset += 16)
815         ppc_collect_reg (regcache, i, (gdb_byte *) vrregs, offset, 16);
816
817       ppc_collect_reg (regcache, (tdep->ppc_vrsave_regnum - 1),
818                        (gdb_byte *) vrregs, offsets->vscr_offset, 4);
819
820       ppc_collect_reg (regcache, tdep->ppc_vrsave_regnum,
821                        (gdb_byte *) vrregs, offsets->vrsave_offset, 4);
822       return;
823     }
824
825   offset = ppc_vrreg_offset (tdep, offsets, regnum);
826   if (regnum != tdep->ppc_vrsave_regnum
827       && regnum != tdep->ppc_vrsave_regnum - 1)
828     ppc_collect_reg (regcache, regnum, (gdb_byte *) vrregs, offset, 16);
829   else
830     ppc_collect_reg (regcache, regnum,
831                     (gdb_byte *) vrregs, offset, 4);
832 }
833 \f
834
835 static int
836 insn_changes_sp_or_jumps (unsigned long insn)
837 {
838   int opcode = (insn >> 26) & 0x03f;
839   int sd = (insn >> 21) & 0x01f;
840   int a = (insn >> 16) & 0x01f;
841   int subcode = (insn >> 1) & 0x3ff;
842
843   /* Changes the stack pointer.  */
844
845   /* NOTE: There are many ways to change the value of a given register.
846            The ways below are those used when the register is R1, the SP,
847            in a funtion's epilogue.  */
848
849   if (opcode == 31 && subcode == 444 && a == 1)
850     return 1;  /* mr R1,Rn */
851   if (opcode == 14 && sd == 1)
852     return 1;  /* addi R1,Rn,simm */
853   if (opcode == 58 && sd == 1)
854     return 1;  /* ld R1,ds(Rn) */
855
856   /* Transfers control.  */
857
858   if (opcode == 18)
859     return 1;  /* b */
860   if (opcode == 16)
861     return 1;  /* bc */
862   if (opcode == 19 && subcode == 16)
863     return 1;  /* bclr */
864   if (opcode == 19 && subcode == 528)
865     return 1;  /* bcctr */
866
867   return 0;
868 }
869
870 /* Return true if we are in the function's epilogue, i.e. after the
871    instruction that destroyed the function's stack frame.
872
873    1) scan forward from the point of execution:
874        a) If you find an instruction that modifies the stack pointer
875           or transfers control (except a return), execution is not in
876           an epilogue, return.
877        b) Stop scanning if you find a return instruction or reach the
878           end of the function or reach the hard limit for the size of
879           an epilogue.
880    2) scan backward from the point of execution:
881         a) If you find an instruction that modifies the stack pointer,
882             execution *is* in an epilogue, return.
883         b) Stop scanning if you reach an instruction that transfers
884            control or the beginning of the function or reach the hard
885            limit for the size of an epilogue.  */
886
887 static int
888 rs6000_in_function_epilogue_frame_p (struct frame_info *curfrm,
889                                      struct gdbarch *gdbarch, CORE_ADDR pc)
890 {
891   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
892   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
893   bfd_byte insn_buf[PPC_INSN_SIZE];
894   CORE_ADDR scan_pc, func_start, func_end, epilogue_start, epilogue_end;
895   unsigned long insn;
896
897   /* Find the search limits based on function boundaries and hard limit.  */
898
899   if (!find_pc_partial_function (pc, NULL, &func_start, &func_end))
900     return 0;
901
902   epilogue_start = pc - PPC_MAX_EPILOGUE_INSTRUCTIONS * PPC_INSN_SIZE;
903   if (epilogue_start < func_start) epilogue_start = func_start;
904
905   epilogue_end = pc + PPC_MAX_EPILOGUE_INSTRUCTIONS * PPC_INSN_SIZE;
906   if (epilogue_end > func_end) epilogue_end = func_end;
907
908   /* Scan forward until next 'blr'.  */
909
910   for (scan_pc = pc; scan_pc < epilogue_end; scan_pc += PPC_INSN_SIZE)
911     {
912       if (!safe_frame_unwind_memory (curfrm, scan_pc, insn_buf, PPC_INSN_SIZE))
913         return 0;
914       insn = extract_unsigned_integer (insn_buf, PPC_INSN_SIZE, byte_order);
915       if (insn == 0x4e800020)
916         break;
917       /* Assume a bctr is a tail call unless it points strictly within
918          this function.  */
919       if (insn == 0x4e800420)
920         {
921           CORE_ADDR ctr = get_frame_register_unsigned (curfrm,
922                                                        tdep->ppc_ctr_regnum);
923           if (ctr > func_start && ctr < func_end)
924             return 0;
925           else
926             break;
927         }
928       if (insn_changes_sp_or_jumps (insn))
929         return 0;
930     }
931
932   /* Scan backward until adjustment to stack pointer (R1).  */
933
934   for (scan_pc = pc - PPC_INSN_SIZE;
935        scan_pc >= epilogue_start;
936        scan_pc -= PPC_INSN_SIZE)
937     {
938       if (!safe_frame_unwind_memory (curfrm, scan_pc, insn_buf, PPC_INSN_SIZE))
939         return 0;
940       insn = extract_unsigned_integer (insn_buf, PPC_INSN_SIZE, byte_order);
941       if (insn_changes_sp_or_jumps (insn))
942         return 1;
943     }
944
945   return 0;
946 }
947
948 /* Implement the stack_frame_destroyed_p gdbarch method.  */
949
950 static int
951 rs6000_stack_frame_destroyed_p (struct gdbarch *gdbarch, CORE_ADDR pc)
952 {
953   return rs6000_in_function_epilogue_frame_p (get_current_frame (),
954                                               gdbarch, pc);
955 }
956
957 /* Get the ith function argument for the current function.  */
958 static CORE_ADDR
959 rs6000_fetch_pointer_argument (struct frame_info *frame, int argi, 
960                                struct type *type)
961 {
962   return get_frame_register_unsigned (frame, 3 + argi);
963 }
964
965 /* Sequence of bytes for breakpoint instruction.  */
966
967 static const unsigned char *
968 rs6000_breakpoint_from_pc (struct gdbarch *gdbarch, CORE_ADDR *bp_addr,
969                            int *bp_size)
970 {
971   static unsigned char big_breakpoint[] = { 0x7d, 0x82, 0x10, 0x08 };
972   static unsigned char little_breakpoint[] = { 0x08, 0x10, 0x82, 0x7d };
973   *bp_size = 4;
974   if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
975     return big_breakpoint;
976   else
977     return little_breakpoint;
978 }
979
980 /* Instruction masks for displaced stepping.  */
981 #define BRANCH_MASK 0xfc000000
982 #define BP_MASK 0xFC0007FE
983 #define B_INSN 0x48000000
984 #define BC_INSN 0x40000000
985 #define BXL_INSN 0x4c000000
986 #define BP_INSN 0x7C000008
987
988 /* Instruction masks used during single-stepping of atomic
989    sequences.  */
990 #define LWARX_MASK 0xfc0007fe
991 #define LWARX_INSTRUCTION 0x7c000028
992 #define LDARX_INSTRUCTION 0x7c0000A8
993 #define STWCX_MASK 0xfc0007ff
994 #define STWCX_INSTRUCTION 0x7c00012d
995 #define STDCX_INSTRUCTION 0x7c0001ad
996
997 /* We can't displaced step atomic sequences.  Otherwise this is just
998    like simple_displaced_step_copy_insn.  */
999
1000 static struct displaced_step_closure *
1001 ppc_displaced_step_copy_insn (struct gdbarch *gdbarch,
1002                               CORE_ADDR from, CORE_ADDR to,
1003                               struct regcache *regs)
1004 {
1005   size_t len = gdbarch_max_insn_length (gdbarch);
1006   gdb_byte *buf = (gdb_byte *) xmalloc (len);
1007   struct cleanup *old_chain = make_cleanup (xfree, buf);
1008   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1009   int insn;
1010
1011   read_memory (from, buf, len);
1012
1013   insn = extract_signed_integer (buf, PPC_INSN_SIZE, byte_order);
1014
1015   /* Assume all atomic sequences start with a lwarx/ldarx instruction.  */
1016   if ((insn & LWARX_MASK) == LWARX_INSTRUCTION
1017       || (insn & LWARX_MASK) == LDARX_INSTRUCTION)
1018     {
1019       if (debug_displaced)
1020         {
1021           fprintf_unfiltered (gdb_stdlog,
1022                               "displaced: can't displaced step "
1023                               "atomic sequence at %s\n",
1024                               paddress (gdbarch, from));
1025         }
1026       do_cleanups (old_chain);
1027       return NULL;
1028     }
1029
1030   write_memory (to, buf, len);
1031
1032   if (debug_displaced)
1033     {
1034       fprintf_unfiltered (gdb_stdlog, "displaced: copy %s->%s: ",
1035                           paddress (gdbarch, from), paddress (gdbarch, to));
1036       displaced_step_dump_bytes (gdb_stdlog, buf, len);
1037     }
1038
1039   discard_cleanups (old_chain);
1040   return (struct displaced_step_closure *) buf;
1041 }
1042
1043 /* Fix up the state of registers and memory after having single-stepped
1044    a displaced instruction.  */
1045 static void
1046 ppc_displaced_step_fixup (struct gdbarch *gdbarch,
1047                           struct displaced_step_closure *closure,
1048                           CORE_ADDR from, CORE_ADDR to,
1049                           struct regcache *regs)
1050 {
1051   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1052   /* Our closure is a copy of the instruction.  */
1053   ULONGEST insn  = extract_unsigned_integer ((gdb_byte *) closure,
1054                                               PPC_INSN_SIZE, byte_order);
1055   ULONGEST opcode = 0;
1056   /* Offset for non PC-relative instructions.  */
1057   LONGEST offset = PPC_INSN_SIZE;
1058
1059   opcode = insn & BRANCH_MASK;
1060
1061   if (debug_displaced)
1062     fprintf_unfiltered (gdb_stdlog,
1063                         "displaced: (ppc) fixup (%s, %s)\n",
1064                         paddress (gdbarch, from), paddress (gdbarch, to));
1065
1066
1067   /* Handle PC-relative branch instructions.  */
1068   if (opcode == B_INSN || opcode == BC_INSN || opcode == BXL_INSN)
1069     {
1070       ULONGEST current_pc;
1071
1072       /* Read the current PC value after the instruction has been executed
1073          in a displaced location.  Calculate the offset to be applied to the
1074          original PC value before the displaced stepping.  */
1075       regcache_cooked_read_unsigned (regs, gdbarch_pc_regnum (gdbarch),
1076                                       &current_pc);
1077       offset = current_pc - to;
1078
1079       if (opcode != BXL_INSN)
1080         {
1081           /* Check for AA bit indicating whether this is an absolute
1082              addressing or PC-relative (1: absolute, 0: relative).  */
1083           if (!(insn & 0x2))
1084             {
1085               /* PC-relative addressing is being used in the branch.  */
1086               if (debug_displaced)
1087                 fprintf_unfiltered
1088                   (gdb_stdlog,
1089                    "displaced: (ppc) branch instruction: %s\n"
1090                    "displaced: (ppc) adjusted PC from %s to %s\n",
1091                    paddress (gdbarch, insn), paddress (gdbarch, current_pc),
1092                    paddress (gdbarch, from + offset));
1093
1094               regcache_cooked_write_unsigned (regs,
1095                                               gdbarch_pc_regnum (gdbarch),
1096                                               from + offset);
1097             }
1098         }
1099       else
1100         {
1101           /* If we're here, it means we have a branch to LR or CTR.  If the
1102              branch was taken, the offset is probably greater than 4 (the next
1103              instruction), so it's safe to assume that an offset of 4 means we
1104              did not take the branch.  */
1105           if (offset == PPC_INSN_SIZE)
1106             regcache_cooked_write_unsigned (regs, gdbarch_pc_regnum (gdbarch),
1107                                             from + PPC_INSN_SIZE);
1108         }
1109
1110       /* Check for LK bit indicating whether we should set the link
1111          register to point to the next instruction
1112          (1: Set, 0: Don't set).  */
1113       if (insn & 0x1)
1114         {
1115           /* Link register needs to be set to the next instruction's PC.  */
1116           regcache_cooked_write_unsigned (regs,
1117                                           gdbarch_tdep (gdbarch)->ppc_lr_regnum,
1118                                           from + PPC_INSN_SIZE);
1119           if (debug_displaced)
1120                 fprintf_unfiltered (gdb_stdlog,
1121                                     "displaced: (ppc) adjusted LR to %s\n",
1122                                     paddress (gdbarch, from + PPC_INSN_SIZE));
1123
1124         }
1125     }
1126   /* Check for breakpoints in the inferior.  If we've found one, place the PC
1127      right at the breakpoint instruction.  */
1128   else if ((insn & BP_MASK) == BP_INSN)
1129     regcache_cooked_write_unsigned (regs, gdbarch_pc_regnum (gdbarch), from);
1130   else
1131   /* Handle any other instructions that do not fit in the categories above.  */
1132     regcache_cooked_write_unsigned (regs, gdbarch_pc_regnum (gdbarch),
1133                                     from + offset);
1134 }
1135
1136 /* Always use hardware single-stepping to execute the
1137    displaced instruction.  */
1138 static int
1139 ppc_displaced_step_hw_singlestep (struct gdbarch *gdbarch,
1140                                   struct displaced_step_closure *closure)
1141 {
1142   return 1;
1143 }
1144
1145 /* Checks for an atomic sequence of instructions beginning with a LWARX/LDARX
1146    instruction and ending with a STWCX/STDCX instruction.  If such a sequence
1147    is found, attempt to step through it.  A breakpoint is placed at the end of 
1148    the sequence.  */
1149
1150 int 
1151 ppc_deal_with_atomic_sequence (struct frame_info *frame)
1152 {
1153   struct gdbarch *gdbarch = get_frame_arch (frame);
1154   struct address_space *aspace = get_frame_address_space (frame);
1155   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1156   CORE_ADDR pc = get_frame_pc (frame);
1157   CORE_ADDR breaks[2] = {-1, -1};
1158   CORE_ADDR loc = pc;
1159   CORE_ADDR closing_insn; /* Instruction that closes the atomic sequence.  */
1160   int insn = read_memory_integer (loc, PPC_INSN_SIZE, byte_order);
1161   int insn_count;
1162   int index;
1163   int last_breakpoint = 0; /* Defaults to 0 (no breakpoints placed).  */  
1164   const int atomic_sequence_length = 16; /* Instruction sequence length.  */
1165   int opcode; /* Branch instruction's OPcode.  */
1166   int bc_insn_count = 0; /* Conditional branch instruction count.  */
1167
1168   /* Assume all atomic sequences start with a lwarx/ldarx instruction.  */
1169   if ((insn & LWARX_MASK) != LWARX_INSTRUCTION
1170       && (insn & LWARX_MASK) != LDARX_INSTRUCTION)
1171     return 0;
1172
1173   /* Assume that no atomic sequence is longer than "atomic_sequence_length" 
1174      instructions.  */
1175   for (insn_count = 0; insn_count < atomic_sequence_length; ++insn_count)
1176     {
1177       loc += PPC_INSN_SIZE;
1178       insn = read_memory_integer (loc, PPC_INSN_SIZE, byte_order);
1179
1180       /* Assume that there is at most one conditional branch in the atomic
1181          sequence.  If a conditional branch is found, put a breakpoint in 
1182          its destination address.  */
1183       if ((insn & BRANCH_MASK) == BC_INSN)
1184         {
1185           int immediate = ((insn & 0xfffc) ^ 0x8000) - 0x8000;
1186           int absolute = insn & 2;
1187
1188           if (bc_insn_count >= 1)
1189             return 0; /* More than one conditional branch found, fallback 
1190                          to the standard single-step code.  */
1191  
1192           if (absolute)
1193             breaks[1] = immediate;
1194           else
1195             breaks[1] = loc + immediate;
1196
1197           bc_insn_count++;
1198           last_breakpoint++;
1199         }
1200
1201       if ((insn & STWCX_MASK) == STWCX_INSTRUCTION
1202           || (insn & STWCX_MASK) == STDCX_INSTRUCTION)
1203         break;
1204     }
1205
1206   /* Assume that the atomic sequence ends with a stwcx/stdcx instruction.  */
1207   if ((insn & STWCX_MASK) != STWCX_INSTRUCTION
1208       && (insn & STWCX_MASK) != STDCX_INSTRUCTION)
1209     return 0;
1210
1211   closing_insn = loc;
1212   loc += PPC_INSN_SIZE;
1213   insn = read_memory_integer (loc, PPC_INSN_SIZE, byte_order);
1214
1215   /* Insert a breakpoint right after the end of the atomic sequence.  */
1216   breaks[0] = loc;
1217
1218   /* Check for duplicated breakpoints.  Check also for a breakpoint
1219      placed (branch instruction's destination) anywhere in sequence.  */
1220   if (last_breakpoint
1221       && (breaks[1] == breaks[0]
1222           || (breaks[1] >= pc && breaks[1] <= closing_insn)))
1223     last_breakpoint = 0;
1224
1225   /* Effectively inserts the breakpoints.  */
1226   for (index = 0; index <= last_breakpoint; index++)
1227     insert_single_step_breakpoint (gdbarch, aspace, breaks[index]);
1228
1229   return 1;
1230 }
1231
1232
1233 #define SIGNED_SHORT(x)                                                 \
1234   ((sizeof (short) == 2)                                                \
1235    ? ((int)(short)(x))                                                  \
1236    : ((int)((((x) & 0xffff) ^ 0x8000) - 0x8000)))
1237
1238 #define GET_SRC_REG(x) (((x) >> 21) & 0x1f)
1239
1240 /* Limit the number of skipped non-prologue instructions, as the examining
1241    of the prologue is expensive.  */
1242 static int max_skip_non_prologue_insns = 10;
1243
1244 /* Return nonzero if the given instruction OP can be part of the prologue
1245    of a function and saves a parameter on the stack.  FRAMEP should be
1246    set if one of the previous instructions in the function has set the
1247    Frame Pointer.  */
1248
1249 static int
1250 store_param_on_stack_p (unsigned long op, int framep, int *r0_contains_arg)
1251 {
1252   /* Move parameters from argument registers to temporary register.  */
1253   if ((op & 0xfc0007fe) == 0x7c000378)         /* mr(.)  Rx,Ry */
1254     {
1255       /* Rx must be scratch register r0.  */
1256       const int rx_regno = (op >> 16) & 31;
1257       /* Ry: Only r3 - r10 are used for parameter passing.  */
1258       const int ry_regno = GET_SRC_REG (op);
1259
1260       if (rx_regno == 0 && ry_regno >= 3 && ry_regno <= 10)
1261         {
1262           *r0_contains_arg = 1;
1263           return 1;
1264         }
1265       else
1266         return 0;
1267     }
1268
1269   /* Save a General Purpose Register on stack.  */
1270
1271   if ((op & 0xfc1f0003) == 0xf8010000 ||       /* std  Rx,NUM(r1) */
1272       (op & 0xfc1f0000) == 0xd8010000)         /* stfd Rx,NUM(r1) */
1273     {
1274       /* Rx: Only r3 - r10 are used for parameter passing.  */
1275       const int rx_regno = GET_SRC_REG (op);
1276
1277       return (rx_regno >= 3 && rx_regno <= 10);
1278     }
1279            
1280   /* Save a General Purpose Register on stack via the Frame Pointer.  */
1281
1282   if (framep &&
1283       ((op & 0xfc1f0000) == 0x901f0000 ||     /* st rx,NUM(r31) */
1284        (op & 0xfc1f0000) == 0x981f0000 ||     /* stb Rx,NUM(r31) */
1285        (op & 0xfc1f0000) == 0xd81f0000))      /* stfd Rx,NUM(r31) */
1286     {
1287       /* Rx: Usually, only r3 - r10 are used for parameter passing.
1288          However, the compiler sometimes uses r0 to hold an argument.  */
1289       const int rx_regno = GET_SRC_REG (op);
1290
1291       return ((rx_regno >= 3 && rx_regno <= 10)
1292               || (rx_regno == 0 && *r0_contains_arg));
1293     }
1294
1295   if ((op & 0xfc1f0000) == 0xfc010000)         /* frsp, fp?,NUM(r1) */
1296     {
1297       /* Only f2 - f8 are used for parameter passing.  */
1298       const int src_regno = GET_SRC_REG (op);
1299
1300       return (src_regno >= 2 && src_regno <= 8);
1301     }
1302
1303   if (framep && ((op & 0xfc1f0000) == 0xfc1f0000))  /* frsp, fp?,NUM(r31) */
1304     {
1305       /* Only f2 - f8 are used for parameter passing.  */
1306       const int src_regno = GET_SRC_REG (op);
1307
1308       return (src_regno >= 2 && src_regno <= 8);
1309     }
1310
1311   /* Not an insn that saves a parameter on stack.  */
1312   return 0;
1313 }
1314
1315 /* Assuming that INSN is a "bl" instruction located at PC, return
1316    nonzero if the destination of the branch is a "blrl" instruction.
1317    
1318    This sequence is sometimes found in certain function prologues.
1319    It allows the function to load the LR register with a value that
1320    they can use to access PIC data using PC-relative offsets.  */
1321
1322 static int
1323 bl_to_blrl_insn_p (CORE_ADDR pc, int insn, enum bfd_endian byte_order)
1324 {
1325   CORE_ADDR dest;
1326   int immediate;
1327   int absolute;
1328   int dest_insn;
1329
1330   absolute = (int) ((insn >> 1) & 1);
1331   immediate = ((insn & ~3) << 6) >> 6;
1332   if (absolute)
1333     dest = immediate;
1334   else
1335     dest = pc + immediate;
1336
1337   dest_insn = read_memory_integer (dest, 4, byte_order);
1338   if ((dest_insn & 0xfc00ffff) == 0x4c000021) /* blrl */
1339     return 1;
1340
1341   return 0;
1342 }
1343
1344 /* Masks for decoding a branch-and-link (bl) instruction.
1345
1346    BL_MASK and BL_INSTRUCTION are used in combination with each other.
1347    The former is anded with the opcode in question; if the result of
1348    this masking operation is equal to BL_INSTRUCTION, then the opcode in
1349    question is a ``bl'' instruction.
1350    
1351    BL_DISPLACMENT_MASK is anded with the opcode in order to extract
1352    the branch displacement.  */
1353
1354 #define BL_MASK 0xfc000001
1355 #define BL_INSTRUCTION 0x48000001
1356 #define BL_DISPLACEMENT_MASK 0x03fffffc
1357
1358 static unsigned long
1359 rs6000_fetch_instruction (struct gdbarch *gdbarch, const CORE_ADDR pc)
1360 {
1361   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1362   gdb_byte buf[4];
1363   unsigned long op;
1364
1365   /* Fetch the instruction and convert it to an integer.  */
1366   if (target_read_memory (pc, buf, 4))
1367     return 0;
1368   op = extract_unsigned_integer (buf, 4, byte_order);
1369
1370   return op;
1371 }
1372
1373 /* GCC generates several well-known sequences of instructions at the begining
1374    of each function prologue when compiling with -fstack-check.  If one of
1375    such sequences starts at START_PC, then return the address of the
1376    instruction immediately past this sequence.  Otherwise, return START_PC.  */
1377    
1378 static CORE_ADDR
1379 rs6000_skip_stack_check (struct gdbarch *gdbarch, const CORE_ADDR start_pc)
1380 {
1381   CORE_ADDR pc = start_pc;
1382   unsigned long op = rs6000_fetch_instruction (gdbarch, pc);
1383
1384   /* First possible sequence: A small number of probes.
1385          stw 0, -<some immediate>(1)
1386          [repeat this instruction any (small) number of times].  */
1387   
1388   if ((op & 0xffff0000) == 0x90010000)
1389     {
1390       while ((op & 0xffff0000) == 0x90010000)
1391         {
1392           pc = pc + 4;
1393           op = rs6000_fetch_instruction (gdbarch, pc);
1394         }
1395       return pc;
1396     }
1397
1398   /* Second sequence: A probing loop.
1399          addi 12,1,-<some immediate>
1400          lis 0,-<some immediate>
1401          [possibly ori 0,0,<some immediate>]
1402          add 0,12,0
1403          cmpw 0,12,0
1404          beq 0,<disp>
1405          addi 12,12,-<some immediate>
1406          stw 0,0(12)
1407          b <disp>
1408          [possibly one last probe: stw 0,<some immediate>(12)].  */
1409
1410   while (1)
1411     {
1412       /* addi 12,1,-<some immediate> */
1413       if ((op & 0xffff0000) != 0x39810000)
1414         break;
1415
1416       /* lis 0,-<some immediate> */
1417       pc = pc + 4;
1418       op = rs6000_fetch_instruction (gdbarch, pc);
1419       if ((op & 0xffff0000) != 0x3c000000)
1420         break;
1421
1422       pc = pc + 4;
1423       op = rs6000_fetch_instruction (gdbarch, pc);
1424       /* [possibly ori 0,0,<some immediate>] */
1425       if ((op & 0xffff0000) == 0x60000000)
1426         {
1427           pc = pc + 4;
1428           op = rs6000_fetch_instruction (gdbarch, pc);
1429         }
1430       /* add 0,12,0 */
1431       if (op != 0x7c0c0214)
1432         break;
1433
1434       /* cmpw 0,12,0 */
1435       pc = pc + 4;
1436       op = rs6000_fetch_instruction (gdbarch, pc);
1437       if (op != 0x7c0c0000)
1438         break;
1439
1440       /* beq 0,<disp> */
1441       pc = pc + 4;
1442       op = rs6000_fetch_instruction (gdbarch, pc);
1443       if ((op & 0xff9f0001) != 0x41820000)
1444         break;
1445
1446       /* addi 12,12,-<some immediate> */
1447       pc = pc + 4;
1448       op = rs6000_fetch_instruction (gdbarch, pc);
1449       if ((op & 0xffff0000) != 0x398c0000)
1450         break;
1451
1452       /* stw 0,0(12) */
1453       pc = pc + 4;
1454       op = rs6000_fetch_instruction (gdbarch, pc);
1455       if (op != 0x900c0000)
1456         break;
1457
1458       /* b <disp> */
1459       pc = pc + 4;
1460       op = rs6000_fetch_instruction (gdbarch, pc);
1461       if ((op & 0xfc000001) != 0x48000000)
1462         break;
1463
1464       /* [possibly one last probe: stw 0,<some immediate>(12)].  */
1465       pc = pc + 4;
1466       op = rs6000_fetch_instruction (gdbarch, pc);
1467       if ((op & 0xffff0000) == 0x900c0000)
1468         {
1469           pc = pc + 4;
1470           op = rs6000_fetch_instruction (gdbarch, pc);
1471         }
1472
1473       /* We found a valid stack-check sequence, return the new PC.  */
1474       return pc;
1475     }
1476
1477   /* Third sequence: No probe; instead, a comparizon between the stack size
1478      limit (saved in a run-time global variable) and the current stack
1479      pointer:
1480
1481         addi 0,1,-<some immediate>
1482         lis 12,__gnat_stack_limit@ha
1483         lwz 12,__gnat_stack_limit@l(12)
1484         twllt 0,12
1485
1486      or, with a small variant in the case of a bigger stack frame:
1487         addis 0,1,<some immediate>
1488         addic 0,0,-<some immediate>
1489         lis 12,__gnat_stack_limit@ha
1490         lwz 12,__gnat_stack_limit@l(12)
1491         twllt 0,12
1492   */
1493   while (1)
1494     {
1495       /* addi 0,1,-<some immediate> */
1496       if ((op & 0xffff0000) != 0x38010000)
1497         {
1498           /* small stack frame variant not recognized; try the
1499              big stack frame variant: */
1500
1501           /* addis 0,1,<some immediate> */
1502           if ((op & 0xffff0000) != 0x3c010000)
1503             break;
1504
1505           /* addic 0,0,-<some immediate> */
1506           pc = pc + 4;
1507           op = rs6000_fetch_instruction (gdbarch, pc);
1508           if ((op & 0xffff0000) != 0x30000000)
1509             break;
1510         }
1511
1512       /* lis 12,<some immediate> */
1513       pc = pc + 4;
1514       op = rs6000_fetch_instruction (gdbarch, pc);
1515       if ((op & 0xffff0000) != 0x3d800000)
1516         break;
1517       
1518       /* lwz 12,<some immediate>(12) */
1519       pc = pc + 4;
1520       op = rs6000_fetch_instruction (gdbarch, pc);
1521       if ((op & 0xffff0000) != 0x818c0000)
1522         break;
1523
1524       /* twllt 0,12 */
1525       pc = pc + 4;
1526       op = rs6000_fetch_instruction (gdbarch, pc);
1527       if ((op & 0xfffffffe) != 0x7c406008)
1528         break;
1529
1530       /* We found a valid stack-check sequence, return the new PC.  */
1531       return pc;
1532     }
1533
1534   /* No stack check code in our prologue, return the start_pc.  */
1535   return start_pc;
1536 }
1537
1538 /* return pc value after skipping a function prologue and also return
1539    information about a function frame.
1540
1541    in struct rs6000_framedata fdata:
1542    - frameless is TRUE, if function does not have a frame.
1543    - nosavedpc is TRUE, if function does not save %pc value in its frame.
1544    - offset is the initial size of this stack frame --- the amount by
1545    which we decrement the sp to allocate the frame.
1546    - saved_gpr is the number of the first saved gpr.
1547    - saved_fpr is the number of the first saved fpr.
1548    - saved_vr is the number of the first saved vr.
1549    - saved_ev is the number of the first saved ev.
1550    - alloca_reg is the number of the register used for alloca() handling.
1551    Otherwise -1.
1552    - gpr_offset is the offset of the first saved gpr from the previous frame.
1553    - fpr_offset is the offset of the first saved fpr from the previous frame.
1554    - vr_offset is the offset of the first saved vr from the previous frame.
1555    - ev_offset is the offset of the first saved ev from the previous frame.
1556    - lr_offset is the offset of the saved lr
1557    - cr_offset is the offset of the saved cr
1558    - vrsave_offset is the offset of the saved vrsave register.  */
1559
1560 static CORE_ADDR
1561 skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc, CORE_ADDR lim_pc,
1562                struct rs6000_framedata *fdata)
1563 {
1564   CORE_ADDR orig_pc = pc;
1565   CORE_ADDR last_prologue_pc = pc;
1566   CORE_ADDR li_found_pc = 0;
1567   gdb_byte buf[4];
1568   unsigned long op;
1569   long offset = 0;
1570   long vr_saved_offset = 0;
1571   int lr_reg = -1;
1572   int cr_reg = -1;
1573   int vr_reg = -1;
1574   int ev_reg = -1;
1575   long ev_offset = 0;
1576   int vrsave_reg = -1;
1577   int reg;
1578   int framep = 0;
1579   int minimal_toc_loaded = 0;
1580   int prev_insn_was_prologue_insn = 1;
1581   int num_skip_non_prologue_insns = 0;
1582   int r0_contains_arg = 0;
1583   const struct bfd_arch_info *arch_info = gdbarch_bfd_arch_info (gdbarch);
1584   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1585   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1586
1587   memset (fdata, 0, sizeof (struct rs6000_framedata));
1588   fdata->saved_gpr = -1;
1589   fdata->saved_fpr = -1;
1590   fdata->saved_vr = -1;
1591   fdata->saved_ev = -1;
1592   fdata->alloca_reg = -1;
1593   fdata->frameless = 1;
1594   fdata->nosavedpc = 1;
1595   fdata->lr_register = -1;
1596
1597   pc = rs6000_skip_stack_check (gdbarch, pc);
1598   if (pc >= lim_pc)
1599     pc = lim_pc;
1600
1601   for (;; pc += 4)
1602     {
1603       /* Sometimes it isn't clear if an instruction is a prologue
1604          instruction or not.  When we encounter one of these ambiguous
1605          cases, we'll set prev_insn_was_prologue_insn to 0 (false).
1606          Otherwise, we'll assume that it really is a prologue instruction.  */
1607       if (prev_insn_was_prologue_insn)
1608         last_prologue_pc = pc;
1609
1610       /* Stop scanning if we've hit the limit.  */
1611       if (pc >= lim_pc)
1612         break;
1613
1614       prev_insn_was_prologue_insn = 1;
1615
1616       /* Fetch the instruction and convert it to an integer.  */
1617       if (target_read_memory (pc, buf, 4))
1618         break;
1619       op = extract_unsigned_integer (buf, 4, byte_order);
1620
1621       if ((op & 0xfc1fffff) == 0x7c0802a6)
1622         {                       /* mflr Rx */
1623           /* Since shared library / PIC code, which needs to get its
1624              address at runtime, can appear to save more than one link
1625              register vis:
1626
1627              *INDENT-OFF*
1628              stwu r1,-304(r1)
1629              mflr r3
1630              bl 0xff570d0 (blrl)
1631              stw r30,296(r1)
1632              mflr r30
1633              stw r31,300(r1)
1634              stw r3,308(r1);
1635              ...
1636              *INDENT-ON*
1637
1638              remember just the first one, but skip over additional
1639              ones.  */
1640           if (lr_reg == -1)
1641             lr_reg = (op & 0x03e00000) >> 21;
1642           if (lr_reg == 0)
1643             r0_contains_arg = 0;
1644           continue;
1645         }
1646       else if ((op & 0xfc1fffff) == 0x7c000026)
1647         {                       /* mfcr Rx */
1648           cr_reg = (op & 0x03e00000);
1649           if (cr_reg == 0)
1650             r0_contains_arg = 0;
1651           continue;
1652
1653         }
1654       else if ((op & 0xfc1f0000) == 0xd8010000)
1655         {                       /* stfd Rx,NUM(r1) */
1656           reg = GET_SRC_REG (op);
1657           if (fdata->saved_fpr == -1 || fdata->saved_fpr > reg)
1658             {
1659               fdata->saved_fpr = reg;
1660               fdata->fpr_offset = SIGNED_SHORT (op) + offset;
1661             }
1662           continue;
1663
1664         }
1665       else if (((op & 0xfc1f0000) == 0xbc010000) ||     /* stm Rx, NUM(r1) */
1666                (((op & 0xfc1f0000) == 0x90010000 ||     /* st rx,NUM(r1) */
1667                  (op & 0xfc1f0003) == 0xf8010000) &&    /* std rx,NUM(r1) */
1668                 (op & 0x03e00000) >= 0x01a00000))       /* rx >= r13 */
1669         {
1670
1671           reg = GET_SRC_REG (op);
1672           if ((op & 0xfc1f0000) == 0xbc010000)
1673             fdata->gpr_mask |= ~((1U << reg) - 1);
1674           else
1675             fdata->gpr_mask |= 1U << reg;
1676           if (fdata->saved_gpr == -1 || fdata->saved_gpr > reg)
1677             {
1678               fdata->saved_gpr = reg;
1679               if ((op & 0xfc1f0003) == 0xf8010000)
1680                 op &= ~3UL;
1681               fdata->gpr_offset = SIGNED_SHORT (op) + offset;
1682             }
1683           continue;
1684
1685         }
1686       else if ((op & 0xffff0000) == 0x3c4c0000
1687                || (op & 0xffff0000) == 0x3c400000
1688                || (op & 0xffff0000) == 0x38420000)
1689         {
1690           /* .  0:      addis 2,12,.TOC.-0b@ha
1691              .          addi 2,2,.TOC.-0b@l
1692              or
1693              .          lis 2,.TOC.@ha
1694              .          addi 2,2,.TOC.@l
1695              used by ELFv2 global entry points to set up r2.  */
1696           continue;
1697         }
1698       else if (op == 0x60000000)
1699         {
1700           /* nop */
1701           /* Allow nops in the prologue, but do not consider them to
1702              be part of the prologue unless followed by other prologue
1703              instructions.  */
1704           prev_insn_was_prologue_insn = 0;
1705           continue;
1706
1707         }
1708       else if ((op & 0xffff0000) == 0x3c000000)
1709         {                       /* addis 0,0,NUM, used for >= 32k frames */
1710           fdata->offset = (op & 0x0000ffff) << 16;
1711           fdata->frameless = 0;
1712           r0_contains_arg = 0;
1713           continue;
1714
1715         }
1716       else if ((op & 0xffff0000) == 0x60000000)
1717         {                       /* ori 0,0,NUM, 2nd half of >= 32k frames */
1718           fdata->offset |= (op & 0x0000ffff);
1719           fdata->frameless = 0;
1720           r0_contains_arg = 0;
1721           continue;
1722
1723         }
1724       else if (lr_reg >= 0 &&
1725                /* std Rx, NUM(r1) || stdu Rx, NUM(r1) */
1726                (((op & 0xffff0000) == (lr_reg | 0xf8010000)) ||
1727                 /* stw Rx, NUM(r1) */
1728                 ((op & 0xffff0000) == (lr_reg | 0x90010000)) ||
1729                 /* stwu Rx, NUM(r1) */
1730                 ((op & 0xffff0000) == (lr_reg | 0x94010000))))
1731         {       /* where Rx == lr */
1732           fdata->lr_offset = offset;
1733           fdata->nosavedpc = 0;
1734           /* Invalidate lr_reg, but don't set it to -1.
1735              That would mean that it had never been set.  */
1736           lr_reg = -2;
1737           if ((op & 0xfc000003) == 0xf8000000 ||        /* std */
1738               (op & 0xfc000000) == 0x90000000)          /* stw */
1739             {
1740               /* Does not update r1, so add displacement to lr_offset.  */
1741               fdata->lr_offset += SIGNED_SHORT (op);
1742             }
1743           continue;
1744
1745         }
1746       else if (cr_reg >= 0 &&
1747                /* std Rx, NUM(r1) || stdu Rx, NUM(r1) */
1748                (((op & 0xffff0000) == (cr_reg | 0xf8010000)) ||
1749                 /* stw Rx, NUM(r1) */
1750                 ((op & 0xffff0000) == (cr_reg | 0x90010000)) ||
1751                 /* stwu Rx, NUM(r1) */
1752                 ((op & 0xffff0000) == (cr_reg | 0x94010000))))
1753         {       /* where Rx == cr */
1754           fdata->cr_offset = offset;
1755           /* Invalidate cr_reg, but don't set it to -1.
1756              That would mean that it had never been set.  */
1757           cr_reg = -2;
1758           if ((op & 0xfc000003) == 0xf8000000 ||
1759               (op & 0xfc000000) == 0x90000000)
1760             {
1761               /* Does not update r1, so add displacement to cr_offset.  */
1762               fdata->cr_offset += SIGNED_SHORT (op);
1763             }
1764           continue;
1765
1766         }
1767       else if ((op & 0xfe80ffff) == 0x42800005 && lr_reg != -1)
1768         {
1769           /* bcl 20,xx,.+4 is used to get the current PC, with or without
1770              prediction bits.  If the LR has already been saved, we can
1771              skip it.  */
1772           continue;
1773         }
1774       else if (op == 0x48000005)
1775         {                       /* bl .+4 used in 
1776                                    -mrelocatable */
1777           fdata->used_bl = 1;
1778           continue;
1779
1780         }
1781       else if (op == 0x48000004)
1782         {                       /* b .+4 (xlc) */
1783           break;
1784
1785         }
1786       else if ((op & 0xffff0000) == 0x3fc00000 ||  /* addis 30,0,foo@ha, used
1787                                                       in V.4 -mminimal-toc */
1788                (op & 0xffff0000) == 0x3bde0000)
1789         {                       /* addi 30,30,foo@l */
1790           continue;
1791
1792         }
1793       else if ((op & 0xfc000001) == 0x48000001)
1794         {                       /* bl foo, 
1795                                    to save fprs???  */
1796
1797           fdata->frameless = 0;
1798
1799           /* If the return address has already been saved, we can skip
1800              calls to blrl (for PIC).  */
1801           if (lr_reg != -1 && bl_to_blrl_insn_p (pc, op, byte_order))
1802             {
1803               fdata->used_bl = 1;
1804               continue;
1805             }
1806
1807           /* Don't skip over the subroutine call if it is not within
1808              the first three instructions of the prologue and either
1809              we have no line table information or the line info tells
1810              us that the subroutine call is not part of the line
1811              associated with the prologue.  */
1812           if ((pc - orig_pc) > 8)
1813             {
1814               struct symtab_and_line prologue_sal = find_pc_line (orig_pc, 0);
1815               struct symtab_and_line this_sal = find_pc_line (pc, 0);
1816
1817               if ((prologue_sal.line == 0)
1818                   || (prologue_sal.line != this_sal.line))
1819                 break;
1820             }
1821
1822           op = read_memory_integer (pc + 4, 4, byte_order);
1823
1824           /* At this point, make sure this is not a trampoline
1825              function (a function that simply calls another functions,
1826              and nothing else).  If the next is not a nop, this branch
1827              was part of the function prologue.  */
1828
1829           if (op == 0x4def7b82 || op == 0)      /* crorc 15, 15, 15 */
1830             break;              /* Don't skip over 
1831                                    this branch.  */
1832
1833           fdata->used_bl = 1;
1834           continue;
1835         }
1836       /* update stack pointer */
1837       else if ((op & 0xfc1f0000) == 0x94010000)
1838         {               /* stu rX,NUM(r1) ||  stwu rX,NUM(r1) */
1839           fdata->frameless = 0;
1840           fdata->offset = SIGNED_SHORT (op);
1841           offset = fdata->offset;
1842           continue;
1843         }
1844       else if ((op & 0xfc1f016a) == 0x7c01016e)
1845         {                       /* stwux rX,r1,rY */
1846           /* No way to figure out what r1 is going to be.  */
1847           fdata->frameless = 0;
1848           offset = fdata->offset;
1849           continue;
1850         }
1851       else if ((op & 0xfc1f0003) == 0xf8010001)
1852         {                       /* stdu rX,NUM(r1) */
1853           fdata->frameless = 0;
1854           fdata->offset = SIGNED_SHORT (op & ~3UL);
1855           offset = fdata->offset;
1856           continue;
1857         }
1858       else if ((op & 0xfc1f016a) == 0x7c01016a)
1859         {                       /* stdux rX,r1,rY */
1860           /* No way to figure out what r1 is going to be.  */
1861           fdata->frameless = 0;
1862           offset = fdata->offset;
1863           continue;
1864         }
1865       else if ((op & 0xffff0000) == 0x38210000)
1866         {                       /* addi r1,r1,SIMM */
1867           fdata->frameless = 0;
1868           fdata->offset += SIGNED_SHORT (op);
1869           offset = fdata->offset;
1870           continue;
1871         }
1872       /* Load up minimal toc pointer.  Do not treat an epilogue restore
1873          of r31 as a minimal TOC load.  */
1874       else if (((op >> 22) == 0x20f     ||      /* l r31,... or l r30,...  */
1875                (op >> 22) == 0x3af)             /* ld r31,... or ld r30,...  */
1876                && !framep
1877                && !minimal_toc_loaded)
1878         {
1879           minimal_toc_loaded = 1;
1880           continue;
1881
1882           /* move parameters from argument registers to local variable
1883              registers */
1884         }
1885       else if ((op & 0xfc0007fe) == 0x7c000378 &&       /* mr(.)  Rx,Ry */
1886                (((op >> 21) & 31) >= 3) &&              /* R3 >= Ry >= R10 */
1887                (((op >> 21) & 31) <= 10) &&
1888                ((long) ((op >> 16) & 31)
1889                 >= fdata->saved_gpr)) /* Rx: local var reg */
1890         {
1891           continue;
1892
1893           /* store parameters in stack */
1894         }
1895       /* Move parameters from argument registers to temporary register.  */
1896       else if (store_param_on_stack_p (op, framep, &r0_contains_arg))
1897         {
1898           continue;
1899
1900           /* Set up frame pointer */
1901         }
1902       else if (op == 0x603d0000)       /* oril r29, r1, 0x0 */
1903         {
1904           fdata->frameless = 0;
1905           framep = 1;
1906           fdata->alloca_reg = (tdep->ppc_gp0_regnum + 29);
1907           continue;
1908
1909           /* Another way to set up the frame pointer.  */
1910         }
1911       else if (op == 0x603f0000 /* oril r31, r1, 0x0 */
1912                || op == 0x7c3f0b78)
1913         {                       /* mr r31, r1 */
1914           fdata->frameless = 0;
1915           framep = 1;
1916           fdata->alloca_reg = (tdep->ppc_gp0_regnum + 31);
1917           continue;
1918
1919           /* Another way to set up the frame pointer.  */
1920         }
1921       else if ((op & 0xfc1fffff) == 0x38010000)
1922         {                       /* addi rX, r1, 0x0 */
1923           fdata->frameless = 0;
1924           framep = 1;
1925           fdata->alloca_reg = (tdep->ppc_gp0_regnum
1926                                + ((op & ~0x38010000) >> 21));
1927           continue;
1928         }
1929       /* AltiVec related instructions.  */
1930       /* Store the vrsave register (spr 256) in another register for
1931          later manipulation, or load a register into the vrsave
1932          register.  2 instructions are used: mfvrsave and
1933          mtvrsave.  They are shorthand notation for mfspr Rn, SPR256
1934          and mtspr SPR256, Rn.  */
1935       /* mfspr Rn SPR256 == 011111 nnnnn 0000001000 01010100110
1936          mtspr SPR256 Rn == 011111 nnnnn 0000001000 01110100110  */
1937       else if ((op & 0xfc1fffff) == 0x7c0042a6)    /* mfvrsave Rn */
1938         {
1939           vrsave_reg = GET_SRC_REG (op);
1940           continue;
1941         }
1942       else if ((op & 0xfc1fffff) == 0x7c0043a6)     /* mtvrsave Rn */
1943         {
1944           continue;
1945         }
1946       /* Store the register where vrsave was saved to onto the stack:
1947          rS is the register where vrsave was stored in a previous
1948          instruction.  */
1949       /* 100100 sssss 00001 dddddddd dddddddd */
1950       else if ((op & 0xfc1f0000) == 0x90010000)     /* stw rS, d(r1) */
1951         {
1952           if (vrsave_reg == GET_SRC_REG (op))
1953             {
1954               fdata->vrsave_offset = SIGNED_SHORT (op) + offset;
1955               vrsave_reg = -1;
1956             }
1957           continue;
1958         }
1959       /* Compute the new value of vrsave, by modifying the register
1960          where vrsave was saved to.  */
1961       else if (((op & 0xfc000000) == 0x64000000)    /* oris Ra, Rs, UIMM */
1962                || ((op & 0xfc000000) == 0x60000000))/* ori Ra, Rs, UIMM */
1963         {
1964           continue;
1965         }
1966       /* li r0, SIMM (short for addi r0, 0, SIMM).  This is the first
1967          in a pair of insns to save the vector registers on the
1968          stack.  */
1969       /* 001110 00000 00000 iiii iiii iiii iiii  */
1970       /* 001110 01110 00000 iiii iiii iiii iiii  */
1971       else if ((op & 0xffff0000) == 0x38000000         /* li r0, SIMM */
1972                || (op & 0xffff0000) == 0x39c00000)     /* li r14, SIMM */
1973         {
1974           if ((op & 0xffff0000) == 0x38000000)
1975             r0_contains_arg = 0;
1976           li_found_pc = pc;
1977           vr_saved_offset = SIGNED_SHORT (op);
1978
1979           /* This insn by itself is not part of the prologue, unless
1980              if part of the pair of insns mentioned above.  So do not
1981              record this insn as part of the prologue yet.  */
1982           prev_insn_was_prologue_insn = 0;
1983         }
1984       /* Store vector register S at (r31+r0) aligned to 16 bytes.  */      
1985       /* 011111 sssss 11111 00000 00111001110 */
1986       else if ((op & 0xfc1fffff) == 0x7c1f01ce)   /* stvx Vs, R31, R0 */
1987         {
1988           if (pc == (li_found_pc + 4))
1989             {
1990               vr_reg = GET_SRC_REG (op);
1991               /* If this is the first vector reg to be saved, or if
1992                  it has a lower number than others previously seen,
1993                  reupdate the frame info.  */
1994               if (fdata->saved_vr == -1 || fdata->saved_vr > vr_reg)
1995                 {
1996                   fdata->saved_vr = vr_reg;
1997                   fdata->vr_offset = vr_saved_offset + offset;
1998                 }
1999               vr_saved_offset = -1;
2000               vr_reg = -1;
2001               li_found_pc = 0;
2002             }
2003         }
2004       /* End AltiVec related instructions.  */
2005
2006       /* Start BookE related instructions.  */
2007       /* Store gen register S at (r31+uimm).
2008          Any register less than r13 is volatile, so we don't care.  */
2009       /* 000100 sssss 11111 iiiii 01100100001 */
2010       else if (arch_info->mach == bfd_mach_ppc_e500
2011                && (op & 0xfc1f07ff) == 0x101f0321)    /* evstdd Rs,uimm(R31) */
2012         {
2013           if ((op & 0x03e00000) >= 0x01a00000)  /* Rs >= r13 */
2014             {
2015               unsigned int imm;
2016               ev_reg = GET_SRC_REG (op);
2017               imm = (op >> 11) & 0x1f;
2018               ev_offset = imm * 8;
2019               /* If this is the first vector reg to be saved, or if
2020                  it has a lower number than others previously seen,
2021                  reupdate the frame info.  */
2022               if (fdata->saved_ev == -1 || fdata->saved_ev > ev_reg)
2023                 {
2024                   fdata->saved_ev = ev_reg;
2025                   fdata->ev_offset = ev_offset + offset;
2026                 }
2027             }
2028           continue;
2029         }
2030       /* Store gen register rS at (r1+rB).  */
2031       /* 000100 sssss 00001 bbbbb 01100100000 */
2032       else if (arch_info->mach == bfd_mach_ppc_e500
2033                && (op & 0xffe007ff) == 0x13e00320)     /* evstddx RS,R1,Rb */
2034         {
2035           if (pc == (li_found_pc + 4))
2036             {
2037               ev_reg = GET_SRC_REG (op);
2038               /* If this is the first vector reg to be saved, or if
2039                  it has a lower number than others previously seen,
2040                  reupdate the frame info.  */
2041               /* We know the contents of rB from the previous instruction.  */
2042               if (fdata->saved_ev == -1 || fdata->saved_ev > ev_reg)
2043                 {
2044                   fdata->saved_ev = ev_reg;
2045                   fdata->ev_offset = vr_saved_offset + offset;
2046                 }
2047               vr_saved_offset = -1;
2048               ev_reg = -1;
2049               li_found_pc = 0;
2050             }
2051           continue;
2052         }
2053       /* Store gen register r31 at (rA+uimm).  */
2054       /* 000100 11111 aaaaa iiiii 01100100001 */
2055       else if (arch_info->mach == bfd_mach_ppc_e500
2056                && (op & 0xffe007ff) == 0x13e00321)   /* evstdd R31,Ra,UIMM */
2057         {
2058           /* Wwe know that the source register is 31 already, but
2059              it can't hurt to compute it.  */
2060           ev_reg = GET_SRC_REG (op);
2061           ev_offset = ((op >> 11) & 0x1f) * 8;
2062           /* If this is the first vector reg to be saved, or if
2063              it has a lower number than others previously seen,
2064              reupdate the frame info.  */
2065           if (fdata->saved_ev == -1 || fdata->saved_ev > ev_reg)
2066             {
2067               fdata->saved_ev = ev_reg;
2068               fdata->ev_offset = ev_offset + offset;
2069             }
2070
2071           continue;
2072         }
2073       /* Store gen register S at (r31+r0).
2074          Store param on stack when offset from SP bigger than 4 bytes.  */
2075       /* 000100 sssss 11111 00000 01100100000 */
2076       else if (arch_info->mach == bfd_mach_ppc_e500
2077                && (op & 0xfc1fffff) == 0x101f0320)     /* evstddx Rs,R31,R0 */
2078         {
2079           if (pc == (li_found_pc + 4))
2080             {
2081               if ((op & 0x03e00000) >= 0x01a00000)
2082                 {
2083                   ev_reg = GET_SRC_REG (op);
2084                   /* If this is the first vector reg to be saved, or if
2085                      it has a lower number than others previously seen,
2086                      reupdate the frame info.  */
2087                   /* We know the contents of r0 from the previous
2088                      instruction.  */
2089                   if (fdata->saved_ev == -1 || fdata->saved_ev > ev_reg)
2090                     {
2091                       fdata->saved_ev = ev_reg;
2092                       fdata->ev_offset = vr_saved_offset + offset;
2093                     }
2094                   ev_reg = -1;
2095                 }
2096               vr_saved_offset = -1;
2097               li_found_pc = 0;
2098               continue;
2099             }
2100         }
2101       /* End BookE related instructions.  */
2102
2103       else
2104         {
2105           unsigned int all_mask = ~((1U << fdata->saved_gpr) - 1);
2106
2107           /* Not a recognized prologue instruction.
2108              Handle optimizer code motions into the prologue by continuing
2109              the search if we have no valid frame yet or if the return
2110              address is not yet saved in the frame.  Also skip instructions
2111              if some of the GPRs expected to be saved are not yet saved.  */
2112           if (fdata->frameless == 0 && fdata->nosavedpc == 0
2113               && (fdata->gpr_mask & all_mask) == all_mask)
2114             break;
2115
2116           if (op == 0x4e800020          /* blr */
2117               || op == 0x4e800420)      /* bctr */
2118             /* Do not scan past epilogue in frameless functions or
2119                trampolines.  */
2120             break;
2121           if ((op & 0xf4000000) == 0x40000000) /* bxx */
2122             /* Never skip branches.  */
2123             break;
2124
2125           if (num_skip_non_prologue_insns++ > max_skip_non_prologue_insns)
2126             /* Do not scan too many insns, scanning insns is expensive with
2127                remote targets.  */
2128             break;
2129
2130           /* Continue scanning.  */
2131           prev_insn_was_prologue_insn = 0;
2132           continue;
2133         }
2134     }
2135
2136 #if 0
2137 /* I have problems with skipping over __main() that I need to address
2138  * sometime.  Previously, I used to use misc_function_vector which
2139  * didn't work as well as I wanted to be.  -MGO */
2140
2141   /* If the first thing after skipping a prolog is a branch to a function,
2142      this might be a call to an initializer in main(), introduced by gcc2.
2143      We'd like to skip over it as well.  Fortunately, xlc does some extra
2144      work before calling a function right after a prologue, thus we can
2145      single out such gcc2 behaviour.  */
2146
2147
2148   if ((op & 0xfc000001) == 0x48000001)
2149     {                           /* bl foo, an initializer function?  */
2150       op = read_memory_integer (pc + 4, 4, byte_order);
2151
2152       if (op == 0x4def7b82)
2153         {                       /* cror 0xf, 0xf, 0xf (nop) */
2154
2155           /* Check and see if we are in main.  If so, skip over this
2156              initializer function as well.  */
2157
2158           tmp = find_pc_misc_function (pc);
2159           if (tmp >= 0
2160               && strcmp (misc_function_vector[tmp].name, main_name ()) == 0)
2161             return pc + 8;
2162         }
2163     }
2164 #endif /* 0 */
2165
2166   if (pc == lim_pc && lr_reg >= 0)
2167     fdata->lr_register = lr_reg;
2168
2169   fdata->offset = -fdata->offset;
2170   return last_prologue_pc;
2171 }
2172
2173 static CORE_ADDR
2174 rs6000_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
2175 {
2176   struct rs6000_framedata frame;
2177   CORE_ADDR limit_pc, func_addr, func_end_addr = 0;
2178
2179   /* See if we can determine the end of the prologue via the symbol table.
2180      If so, then return either PC, or the PC after the prologue, whichever
2181      is greater.  */
2182   if (find_pc_partial_function (pc, NULL, &func_addr, &func_end_addr))
2183     {
2184       CORE_ADDR post_prologue_pc
2185         = skip_prologue_using_sal (gdbarch, func_addr);
2186       if (post_prologue_pc != 0)
2187         return max (pc, post_prologue_pc);
2188     }
2189
2190   /* Can't determine prologue from the symbol table, need to examine
2191      instructions.  */
2192
2193   /* Find an upper limit on the function prologue using the debug
2194      information.  If the debug information could not be used to provide
2195      that bound, then use an arbitrary large number as the upper bound.  */
2196   limit_pc = skip_prologue_using_sal (gdbarch, pc);
2197   if (limit_pc == 0)
2198     limit_pc = pc + 100;          /* Magic.  */
2199
2200   /* Do not allow limit_pc to be past the function end, if we know
2201      where that end is...  */
2202   if (func_end_addr && limit_pc > func_end_addr)
2203     limit_pc = func_end_addr;
2204
2205   pc = skip_prologue (gdbarch, pc, limit_pc, &frame);
2206   return pc;
2207 }
2208
2209 /* When compiling for EABI, some versions of GCC emit a call to __eabi
2210    in the prologue of main().
2211
2212    The function below examines the code pointed at by PC and checks to
2213    see if it corresponds to a call to __eabi.  If so, it returns the
2214    address of the instruction following that call.  Otherwise, it simply
2215    returns PC.  */
2216
2217 static CORE_ADDR
2218 rs6000_skip_main_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
2219 {
2220   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
2221   gdb_byte buf[4];
2222   unsigned long op;
2223
2224   if (target_read_memory (pc, buf, 4))
2225     return pc;
2226   op = extract_unsigned_integer (buf, 4, byte_order);
2227
2228   if ((op & BL_MASK) == BL_INSTRUCTION)
2229     {
2230       CORE_ADDR displ = op & BL_DISPLACEMENT_MASK;
2231       CORE_ADDR call_dest = pc + 4 + displ;
2232       struct bound_minimal_symbol s = lookup_minimal_symbol_by_pc (call_dest);
2233
2234       /* We check for ___eabi (three leading underscores) in addition
2235          to __eabi in case the GCC option "-fleading-underscore" was
2236          used to compile the program.  */
2237       if (s.minsym != NULL
2238           && MSYMBOL_LINKAGE_NAME (s.minsym) != NULL
2239           && (strcmp (MSYMBOL_LINKAGE_NAME (s.minsym), "__eabi") == 0
2240               || strcmp (MSYMBOL_LINKAGE_NAME (s.minsym), "___eabi") == 0))
2241         pc += 4;
2242     }
2243   return pc;
2244 }
2245
2246 /* All the ABI's require 16 byte alignment.  */
2247 static CORE_ADDR
2248 rs6000_frame_align (struct gdbarch *gdbarch, CORE_ADDR addr)
2249 {
2250   return (addr & -16);
2251 }
2252
2253 /* Return whether handle_inferior_event() should proceed through code
2254    starting at PC in function NAME when stepping.
2255
2256    The AIX -bbigtoc linker option generates functions @FIX0, @FIX1, etc. to
2257    handle memory references that are too distant to fit in instructions
2258    generated by the compiler.  For example, if 'foo' in the following
2259    instruction:
2260
2261      lwz r9,foo(r2)
2262
2263    is greater than 32767, the linker might replace the lwz with a branch to
2264    somewhere in @FIX1 that does the load in 2 instructions and then branches
2265    back to where execution should continue.
2266
2267    GDB should silently step over @FIX code, just like AIX dbx does.
2268    Unfortunately, the linker uses the "b" instruction for the
2269    branches, meaning that the link register doesn't get set.
2270    Therefore, GDB's usual step_over_function () mechanism won't work.
2271
2272    Instead, use the gdbarch_skip_trampoline_code and
2273    gdbarch_skip_trampoline_code hooks in handle_inferior_event() to skip past
2274    @FIX code.  */
2275
2276 static int
2277 rs6000_in_solib_return_trampoline (struct gdbarch *gdbarch,
2278                                    CORE_ADDR pc, const char *name)
2279 {
2280   return name && startswith (name, "@FIX");
2281 }
2282
2283 /* Skip code that the user doesn't want to see when stepping:
2284
2285    1. Indirect function calls use a piece of trampoline code to do context
2286    switching, i.e. to set the new TOC table.  Skip such code if we are on
2287    its first instruction (as when we have single-stepped to here).
2288
2289    2. Skip shared library trampoline code (which is different from
2290    indirect function call trampolines).
2291
2292    3. Skip bigtoc fixup code.
2293
2294    Result is desired PC to step until, or NULL if we are not in
2295    code that should be skipped.  */
2296
2297 static CORE_ADDR
2298 rs6000_skip_trampoline_code (struct frame_info *frame, CORE_ADDR pc)
2299 {
2300   struct gdbarch *gdbarch = get_frame_arch (frame);
2301   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
2302   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
2303   unsigned int ii, op;
2304   int rel;
2305   CORE_ADDR solib_target_pc;
2306   struct bound_minimal_symbol msymbol;
2307
2308   static unsigned trampoline_code[] =
2309   {
2310     0x800b0000,                 /*     l   r0,0x0(r11)  */
2311     0x90410014,                 /*    st   r2,0x14(r1)  */
2312     0x7c0903a6,                 /* mtctr   r0           */
2313     0x804b0004,                 /*     l   r2,0x4(r11)  */
2314     0x816b0008,                 /*     l  r11,0x8(r11)  */
2315     0x4e800420,                 /*  bctr                */
2316     0x4e800020,                 /*    br                */
2317     0
2318   };
2319
2320   /* Check for bigtoc fixup code.  */
2321   msymbol = lookup_minimal_symbol_by_pc (pc);
2322   if (msymbol.minsym
2323       && rs6000_in_solib_return_trampoline (gdbarch, pc,
2324                                             MSYMBOL_LINKAGE_NAME (msymbol.minsym)))
2325     {
2326       /* Double-check that the third instruction from PC is relative "b".  */
2327       op = read_memory_integer (pc + 8, 4, byte_order);
2328       if ((op & 0xfc000003) == 0x48000000)
2329         {
2330           /* Extract bits 6-29 as a signed 24-bit relative word address and
2331              add it to the containing PC.  */
2332           rel = ((int)(op << 6) >> 6);
2333           return pc + 8 + rel;
2334         }
2335     }
2336
2337   /* If pc is in a shared library trampoline, return its target.  */
2338   solib_target_pc = find_solib_trampoline_target (frame, pc);
2339   if (solib_target_pc)
2340     return solib_target_pc;
2341
2342   for (ii = 0; trampoline_code[ii]; ++ii)
2343     {
2344       op = read_memory_integer (pc + (ii * 4), 4, byte_order);
2345       if (op != trampoline_code[ii])
2346         return 0;
2347     }
2348   ii = get_frame_register_unsigned (frame, 11); /* r11 holds destination
2349                                                    addr.  */
2350   pc = read_memory_unsigned_integer (ii, tdep->wordsize, byte_order);
2351   return pc;
2352 }
2353
2354 /* ISA-specific vector types.  */
2355
2356 static struct type *
2357 rs6000_builtin_type_vec64 (struct gdbarch *gdbarch)
2358 {
2359   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
2360
2361   if (!tdep->ppc_builtin_type_vec64)
2362     {
2363       const struct builtin_type *bt = builtin_type (gdbarch);
2364
2365       /* The type we're building is this: */
2366 #if 0
2367       union __gdb_builtin_type_vec64
2368         {
2369           int64_t uint64;
2370           float v2_float[2];
2371           int32_t v2_int32[2];
2372           int16_t v4_int16[4];
2373           int8_t v8_int8[8];
2374         };
2375 #endif
2376
2377       struct type *t;
2378
2379       t = arch_composite_type (gdbarch,
2380                                "__ppc_builtin_type_vec64", TYPE_CODE_UNION);
2381       append_composite_type_field (t, "uint64", bt->builtin_int64);
2382       append_composite_type_field (t, "v2_float",
2383                                    init_vector_type (bt->builtin_float, 2));
2384       append_composite_type_field (t, "v2_int32",
2385                                    init_vector_type (bt->builtin_int32, 2));
2386       append_composite_type_field (t, "v4_int16",
2387                                    init_vector_type (bt->builtin_int16, 4));
2388       append_composite_type_field (t, "v8_int8",
2389                                    init_vector_type (bt->builtin_int8, 8));
2390
2391       TYPE_VECTOR (t) = 1;
2392       TYPE_NAME (t) = "ppc_builtin_type_vec64";
2393       tdep->ppc_builtin_type_vec64 = t;
2394     }
2395
2396   return tdep->ppc_builtin_type_vec64;
2397 }
2398
2399 /* Vector 128 type.  */
2400
2401 static struct type *
2402 rs6000_builtin_type_vec128 (struct gdbarch *gdbarch)
2403 {
2404   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
2405
2406   if (!tdep->ppc_builtin_type_vec128)
2407     {
2408       const struct builtin_type *bt = builtin_type (gdbarch);
2409
2410       /* The type we're building is this
2411
2412          type = union __ppc_builtin_type_vec128 {
2413              uint128_t uint128;
2414              double v2_double[2];
2415              float v4_float[4];
2416              int32_t v4_int32[4];
2417              int16_t v8_int16[8];
2418              int8_t v16_int8[16];
2419          }
2420       */
2421
2422       struct type *t;
2423
2424       t = arch_composite_type (gdbarch,
2425                                "__ppc_builtin_type_vec128", TYPE_CODE_UNION);
2426       append_composite_type_field (t, "uint128", bt->builtin_uint128);
2427       append_composite_type_field (t, "v2_double",
2428                                    init_vector_type (bt->builtin_double, 2));
2429       append_composite_type_field (t, "v4_float",
2430                                    init_vector_type (bt->builtin_float, 4));
2431       append_composite_type_field (t, "v4_int32",
2432                                    init_vector_type (bt->builtin_int32, 4));
2433       append_composite_type_field (t, "v8_int16",
2434                                    init_vector_type (bt->builtin_int16, 8));
2435       append_composite_type_field (t, "v16_int8",
2436                                    init_vector_type (bt->builtin_int8, 16));
2437
2438       TYPE_VECTOR (t) = 1;
2439       TYPE_NAME (t) = "ppc_builtin_type_vec128";
2440       tdep->ppc_builtin_type_vec128 = t;
2441     }
2442
2443   return tdep->ppc_builtin_type_vec128;
2444 }
2445
2446 /* Return the name of register number REGNO, or the empty string if it
2447    is an anonymous register.  */
2448
2449 static const char *
2450 rs6000_register_name (struct gdbarch *gdbarch, int regno)
2451 {
2452   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
2453
2454   /* The upper half "registers" have names in the XML description,
2455      but we present only the low GPRs and the full 64-bit registers
2456      to the user.  */
2457   if (tdep->ppc_ev0_upper_regnum >= 0
2458       && tdep->ppc_ev0_upper_regnum <= regno
2459       && regno < tdep->ppc_ev0_upper_regnum + ppc_num_gprs)
2460     return "";
2461
2462   /* Hide the upper halves of the vs0~vs31 registers.  */
2463   if (tdep->ppc_vsr0_regnum >= 0
2464       && tdep->ppc_vsr0_upper_regnum <= regno
2465       && regno < tdep->ppc_vsr0_upper_regnum + ppc_num_gprs)
2466     return "";
2467
2468   /* Check if the SPE pseudo registers are available.  */
2469   if (IS_SPE_PSEUDOREG (tdep, regno))
2470     {
2471       static const char *const spe_regnames[] = {
2472         "ev0", "ev1", "ev2", "ev3", "ev4", "ev5", "ev6", "ev7",
2473         "ev8", "ev9", "ev10", "ev11", "ev12", "ev13", "ev14", "ev15",
2474         "ev16", "ev17", "ev18", "ev19", "ev20", "ev21", "ev22", "ev23",
2475         "ev24", "ev25", "ev26", "ev27", "ev28", "ev29", "ev30", "ev31",
2476       };
2477       return spe_regnames[regno - tdep->ppc_ev0_regnum];
2478     }
2479
2480   /* Check if the decimal128 pseudo-registers are available.  */
2481   if (IS_DFP_PSEUDOREG (tdep, regno))
2482     {
2483       static const char *const dfp128_regnames[] = {
2484         "dl0", "dl1", "dl2", "dl3",
2485         "dl4", "dl5", "dl6", "dl7",
2486         "dl8", "dl9", "dl10", "dl11",
2487         "dl12", "dl13", "dl14", "dl15"
2488       };
2489       return dfp128_regnames[regno - tdep->ppc_dl0_regnum];
2490     }
2491
2492   /* Check if this is a VSX pseudo-register.  */
2493   if (IS_VSX_PSEUDOREG (tdep, regno))
2494     {
2495       static const char *const vsx_regnames[] = {
2496         "vs0", "vs1", "vs2", "vs3", "vs4", "vs5", "vs6", "vs7",
2497         "vs8", "vs9", "vs10", "vs11", "vs12", "vs13", "vs14",
2498         "vs15", "vs16", "vs17", "vs18", "vs19", "vs20", "vs21",
2499         "vs22", "vs23", "vs24", "vs25", "vs26", "vs27", "vs28",
2500         "vs29", "vs30", "vs31", "vs32", "vs33", "vs34", "vs35",
2501         "vs36", "vs37", "vs38", "vs39", "vs40", "vs41", "vs42",
2502         "vs43", "vs44", "vs45", "vs46", "vs47", "vs48", "vs49",
2503         "vs50", "vs51", "vs52", "vs53", "vs54", "vs55", "vs56",
2504         "vs57", "vs58", "vs59", "vs60", "vs61", "vs62", "vs63"
2505       };
2506       return vsx_regnames[regno - tdep->ppc_vsr0_regnum];
2507     }
2508
2509   /* Check if the this is a Extended FP pseudo-register.  */
2510   if (IS_EFP_PSEUDOREG (tdep, regno))
2511     {
2512       static const char *const efpr_regnames[] = {
2513         "f32", "f33", "f34", "f35", "f36", "f37", "f38",
2514         "f39", "f40", "f41", "f42", "f43", "f44", "f45",
2515         "f46", "f47", "f48", "f49", "f50", "f51",
2516         "f52", "f53", "f54", "f55", "f56", "f57",
2517         "f58", "f59", "f60", "f61", "f62", "f63"
2518       };
2519       return efpr_regnames[regno - tdep->ppc_efpr0_regnum];
2520     }
2521
2522   return tdesc_register_name (gdbarch, regno);
2523 }
2524
2525 /* Return the GDB type object for the "standard" data type of data in
2526    register N.  */
2527
2528 static struct type *
2529 rs6000_pseudo_register_type (struct gdbarch *gdbarch, int regnum)
2530 {
2531   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
2532
2533   /* These are the only pseudo-registers we support.  */
2534   gdb_assert (IS_SPE_PSEUDOREG (tdep, regnum)
2535               || IS_DFP_PSEUDOREG (tdep, regnum)
2536               || IS_VSX_PSEUDOREG (tdep, regnum)
2537               || IS_EFP_PSEUDOREG (tdep, regnum));
2538
2539   /* These are the e500 pseudo-registers.  */
2540   if (IS_SPE_PSEUDOREG (tdep, regnum))
2541     return rs6000_builtin_type_vec64 (gdbarch);
2542   else if (IS_DFP_PSEUDOREG (tdep, regnum))
2543     /* PPC decimal128 pseudo-registers.  */
2544     return builtin_type (gdbarch)->builtin_declong;
2545   else if (IS_VSX_PSEUDOREG (tdep, regnum))
2546     /* POWER7 VSX pseudo-registers.  */
2547     return rs6000_builtin_type_vec128 (gdbarch);
2548   else
2549     /* POWER7 Extended FP pseudo-registers.  */
2550     return builtin_type (gdbarch)->builtin_double;
2551 }
2552
2553 /* Is REGNUM a member of REGGROUP?  */
2554 static int
2555 rs6000_pseudo_register_reggroup_p (struct gdbarch *gdbarch, int regnum,
2556                                    struct reggroup *group)
2557 {
2558   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
2559
2560   /* These are the only pseudo-registers we support.  */
2561   gdb_assert (IS_SPE_PSEUDOREG (tdep, regnum)
2562               || IS_DFP_PSEUDOREG (tdep, regnum)
2563               || IS_VSX_PSEUDOREG (tdep, regnum)
2564               || IS_EFP_PSEUDOREG (tdep, regnum));
2565
2566   /* These are the e500 pseudo-registers or the POWER7 VSX registers.  */
2567   if (IS_SPE_PSEUDOREG (tdep, regnum) || IS_VSX_PSEUDOREG (tdep, regnum))
2568     return group == all_reggroup || group == vector_reggroup;
2569   else
2570     /* PPC decimal128 or Extended FP pseudo-registers.  */
2571     return group == all_reggroup || group == float_reggroup;
2572 }
2573
2574 /* The register format for RS/6000 floating point registers is always
2575    double, we need a conversion if the memory format is float.  */
2576
2577 static int
2578 rs6000_convert_register_p (struct gdbarch *gdbarch, int regnum,
2579                            struct type *type)
2580 {
2581   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
2582
2583   return (tdep->ppc_fp0_regnum >= 0
2584           && regnum >= tdep->ppc_fp0_regnum
2585           && regnum < tdep->ppc_fp0_regnum + ppc_num_fprs
2586           && TYPE_CODE (type) == TYPE_CODE_FLT
2587           && TYPE_LENGTH (type)
2588              != TYPE_LENGTH (builtin_type (gdbarch)->builtin_double));
2589 }
2590
2591 static int
2592 rs6000_register_to_value (struct frame_info *frame,
2593                           int regnum,
2594                           struct type *type,
2595                           gdb_byte *to,
2596                           int *optimizedp, int *unavailablep)
2597 {
2598   struct gdbarch *gdbarch = get_frame_arch (frame);
2599   gdb_byte from[MAX_REGISTER_SIZE];
2600   
2601   gdb_assert (TYPE_CODE (type) == TYPE_CODE_FLT);
2602
2603   if (!get_frame_register_bytes (frame, regnum, 0,
2604                                  register_size (gdbarch, regnum),
2605                                  from, optimizedp, unavailablep))
2606     return 0;
2607
2608   convert_typed_floating (from, builtin_type (gdbarch)->builtin_double,
2609                           to, type);
2610   *optimizedp = *unavailablep = 0;
2611   return 1;
2612 }
2613
2614 static void
2615 rs6000_value_to_register (struct frame_info *frame,
2616                           int regnum,
2617                           struct type *type,
2618                           const gdb_byte *from)
2619 {
2620   struct gdbarch *gdbarch = get_frame_arch (frame);
2621   gdb_byte to[MAX_REGISTER_SIZE];
2622
2623   gdb_assert (TYPE_CODE (type) == TYPE_CODE_FLT);
2624
2625   convert_typed_floating (from, type,
2626                           to, builtin_type (gdbarch)->builtin_double);
2627   put_frame_register (frame, regnum, to);
2628 }
2629
2630  /* The type of a function that moves the value of REG between CACHE
2631     or BUF --- in either direction.  */
2632 typedef enum register_status (*move_ev_register_func) (struct regcache *,
2633                                                        int, void *);
2634
2635 /* Move SPE vector register values between a 64-bit buffer and the two
2636    32-bit raw register halves in a regcache.  This function handles
2637    both splitting a 64-bit value into two 32-bit halves, and joining
2638    two halves into a whole 64-bit value, depending on the function
2639    passed as the MOVE argument.
2640
2641    EV_REG must be the number of an SPE evN vector register --- a
2642    pseudoregister.  REGCACHE must be a regcache, and BUFFER must be a
2643    64-bit buffer.
2644
2645    Call MOVE once for each 32-bit half of that register, passing
2646    REGCACHE, the number of the raw register corresponding to that
2647    half, and the address of the appropriate half of BUFFER.
2648
2649    For example, passing 'regcache_raw_read' as the MOVE function will
2650    fill BUFFER with the full 64-bit contents of EV_REG.  Or, passing
2651    'regcache_raw_supply' will supply the contents of BUFFER to the
2652    appropriate pair of raw registers in REGCACHE.
2653
2654    You may need to cast away some 'const' qualifiers when passing
2655    MOVE, since this function can't tell at compile-time which of
2656    REGCACHE or BUFFER is acting as the source of the data.  If C had
2657    co-variant type qualifiers, ...  */
2658
2659 static enum register_status
2660 e500_move_ev_register (move_ev_register_func move,
2661                        struct regcache *regcache, int ev_reg, void *buffer)
2662 {
2663   struct gdbarch *arch = get_regcache_arch (regcache);
2664   struct gdbarch_tdep *tdep = gdbarch_tdep (arch); 
2665   int reg_index;
2666   gdb_byte *byte_buffer = (gdb_byte *) buffer;
2667   enum register_status status;
2668
2669   gdb_assert (IS_SPE_PSEUDOREG (tdep, ev_reg));
2670
2671   reg_index = ev_reg - tdep->ppc_ev0_regnum;
2672
2673   if (gdbarch_byte_order (arch) == BFD_ENDIAN_BIG)
2674     {
2675       status = move (regcache, tdep->ppc_ev0_upper_regnum + reg_index,
2676                      byte_buffer);
2677       if (status == REG_VALID)
2678         status = move (regcache, tdep->ppc_gp0_regnum + reg_index,
2679                        byte_buffer + 4);
2680     }
2681   else
2682     {
2683       status = move (regcache, tdep->ppc_gp0_regnum + reg_index, byte_buffer);
2684       if (status == REG_VALID)
2685         status = move (regcache, tdep->ppc_ev0_upper_regnum + reg_index,
2686                        byte_buffer + 4);
2687     }
2688
2689   return status;
2690 }
2691
2692 static enum register_status
2693 do_regcache_raw_read (struct regcache *regcache, int regnum, void *buffer)
2694 {
2695   return regcache_raw_read (regcache, regnum, (gdb_byte *) buffer);
2696 }
2697
2698 static enum register_status
2699 do_regcache_raw_write (struct regcache *regcache, int regnum, void *buffer)
2700 {
2701   regcache_raw_write (regcache, regnum, (const gdb_byte *) buffer);
2702
2703   return REG_VALID;
2704 }
2705
2706 static enum register_status
2707 e500_pseudo_register_read (struct gdbarch *gdbarch, struct regcache *regcache,
2708                            int reg_nr, gdb_byte *buffer)
2709 {
2710   return e500_move_ev_register (do_regcache_raw_read, regcache, reg_nr, buffer);
2711 }
2712
2713 static void
2714 e500_pseudo_register_write (struct gdbarch *gdbarch, struct regcache *regcache,
2715                             int reg_nr, const gdb_byte *buffer)
2716 {
2717   e500_move_ev_register (do_regcache_raw_write, regcache,
2718                          reg_nr, (void *) buffer);
2719 }
2720
2721 /* Read method for DFP pseudo-registers.  */
2722 static enum register_status
2723 dfp_pseudo_register_read (struct gdbarch *gdbarch, struct regcache *regcache,
2724                            int reg_nr, gdb_byte *buffer)
2725 {
2726   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
2727   int reg_index = reg_nr - tdep->ppc_dl0_regnum;
2728   enum register_status status;
2729
2730   if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
2731     {
2732       /* Read two FP registers to form a whole dl register.  */
2733       status = regcache_raw_read (regcache, tdep->ppc_fp0_regnum +
2734                                   2 * reg_index, buffer);
2735       if (status == REG_VALID)
2736         status = regcache_raw_read (regcache, tdep->ppc_fp0_regnum +
2737                                     2 * reg_index + 1, buffer + 8);
2738     }
2739   else
2740     {
2741       status = regcache_raw_read (regcache, tdep->ppc_fp0_regnum +
2742                                   2 * reg_index + 1, buffer);
2743       if (status == REG_VALID)
2744         status = regcache_raw_read (regcache, tdep->ppc_fp0_regnum +
2745                                     2 * reg_index, buffer + 8);
2746     }
2747
2748   return status;
2749 }
2750
2751 /* Write method for DFP pseudo-registers.  */
2752 static void
2753 dfp_pseudo_register_write (struct gdbarch *gdbarch, struct regcache *regcache,
2754                             int reg_nr, const gdb_byte *buffer)
2755 {
2756   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
2757   int reg_index = reg_nr - tdep->ppc_dl0_regnum;
2758
2759   if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
2760     {
2761       /* Write each half of the dl register into a separate
2762       FP register.  */
2763       regcache_raw_write (regcache, tdep->ppc_fp0_regnum +
2764                           2 * reg_index, buffer);
2765       regcache_raw_write (regcache, tdep->ppc_fp0_regnum +
2766                           2 * reg_index + 1, buffer + 8);
2767     }
2768   else
2769     {
2770       regcache_raw_write (regcache, tdep->ppc_fp0_regnum +
2771                           2 * reg_index + 1, buffer);
2772       regcache_raw_write (regcache, tdep->ppc_fp0_regnum +
2773                           2 * reg_index, buffer + 8);
2774     }
2775 }
2776
2777 /* Read method for POWER7 VSX pseudo-registers.  */
2778 static enum register_status
2779 vsx_pseudo_register_read (struct gdbarch *gdbarch, struct regcache *regcache,
2780                            int reg_nr, gdb_byte *buffer)
2781 {
2782   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
2783   int reg_index = reg_nr - tdep->ppc_vsr0_regnum;
2784   enum register_status status;
2785
2786   /* Read the portion that overlaps the VMX registers.  */
2787   if (reg_index > 31)
2788     status = regcache_raw_read (regcache, tdep->ppc_vr0_regnum +
2789                                 reg_index - 32, buffer);
2790   else
2791     /* Read the portion that overlaps the FPR registers.  */
2792     if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
2793       {
2794         status = regcache_raw_read (regcache, tdep->ppc_fp0_regnum +
2795                                     reg_index, buffer);
2796         if (status == REG_VALID)
2797           status = regcache_raw_read (regcache, tdep->ppc_vsr0_upper_regnum +
2798                                       reg_index, buffer + 8);
2799       }
2800     else
2801       {
2802         status = regcache_raw_read (regcache, tdep->ppc_fp0_regnum +
2803                                     reg_index, buffer + 8);
2804         if (status == REG_VALID)
2805           status = regcache_raw_read (regcache, tdep->ppc_vsr0_upper_regnum +
2806                                       reg_index, buffer);
2807       }
2808
2809   return status;
2810 }
2811
2812 /* Write method for POWER7 VSX pseudo-registers.  */
2813 static void
2814 vsx_pseudo_register_write (struct gdbarch *gdbarch, struct regcache *regcache,
2815                             int reg_nr, const gdb_byte *buffer)
2816 {
2817   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
2818   int reg_index = reg_nr - tdep->ppc_vsr0_regnum;
2819
2820   /* Write the portion that overlaps the VMX registers.  */
2821   if (reg_index > 31)
2822     regcache_raw_write (regcache, tdep->ppc_vr0_regnum +
2823                         reg_index - 32, buffer);
2824   else
2825     /* Write the portion that overlaps the FPR registers.  */
2826     if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
2827       {
2828         regcache_raw_write (regcache, tdep->ppc_fp0_regnum +
2829                         reg_index, buffer);
2830         regcache_raw_write (regcache, tdep->ppc_vsr0_upper_regnum +
2831                         reg_index, buffer + 8);
2832       }
2833     else
2834       {
2835         regcache_raw_write (regcache, tdep->ppc_fp0_regnum +
2836                         reg_index, buffer + 8);
2837         regcache_raw_write (regcache, tdep->ppc_vsr0_upper_regnum +
2838                         reg_index, buffer);
2839       }
2840 }
2841
2842 /* Read method for POWER7 Extended FP pseudo-registers.  */
2843 static enum register_status
2844 efpr_pseudo_register_read (struct gdbarch *gdbarch, struct regcache *regcache,
2845                            int reg_nr, gdb_byte *buffer)
2846 {
2847   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
2848   int reg_index = reg_nr - tdep->ppc_efpr0_regnum;
2849   int offset = gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG ? 0 : 8;
2850
2851   /* Read the portion that overlaps the VMX register.  */
2852   return regcache_raw_read_part (regcache, tdep->ppc_vr0_regnum + reg_index,
2853                                  offset, register_size (gdbarch, reg_nr),
2854                                  buffer);
2855 }
2856
2857 /* Write method for POWER7 Extended FP pseudo-registers.  */
2858 static void
2859 efpr_pseudo_register_write (struct gdbarch *gdbarch, struct regcache *regcache,
2860                             int reg_nr, const gdb_byte *buffer)
2861 {
2862   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
2863   int reg_index = reg_nr - tdep->ppc_efpr0_regnum;
2864   int offset = gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG ? 0 : 8;
2865
2866   /* Write the portion that overlaps the VMX register.  */
2867   regcache_raw_write_part (regcache, tdep->ppc_vr0_regnum + reg_index,
2868                            offset, register_size (gdbarch, reg_nr),
2869                            buffer);
2870 }
2871
2872 static enum register_status
2873 rs6000_pseudo_register_read (struct gdbarch *gdbarch,
2874                              struct regcache *regcache,
2875                              int reg_nr, gdb_byte *buffer)
2876 {
2877   struct gdbarch *regcache_arch = get_regcache_arch (regcache);
2878   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch); 
2879
2880   gdb_assert (regcache_arch == gdbarch);
2881
2882   if (IS_SPE_PSEUDOREG (tdep, reg_nr))
2883     return e500_pseudo_register_read (gdbarch, regcache, reg_nr, buffer);
2884   else if (IS_DFP_PSEUDOREG (tdep, reg_nr))
2885     return dfp_pseudo_register_read (gdbarch, regcache, reg_nr, buffer);
2886   else if (IS_VSX_PSEUDOREG (tdep, reg_nr))
2887     return vsx_pseudo_register_read (gdbarch, regcache, reg_nr, buffer);
2888   else if (IS_EFP_PSEUDOREG (tdep, reg_nr))
2889     return efpr_pseudo_register_read (gdbarch, regcache, reg_nr, buffer);
2890   else
2891     internal_error (__FILE__, __LINE__,
2892                     _("rs6000_pseudo_register_read: "
2893                     "called on unexpected register '%s' (%d)"),
2894                     gdbarch_register_name (gdbarch, reg_nr), reg_nr);
2895 }
2896
2897 static void
2898 rs6000_pseudo_register_write (struct gdbarch *gdbarch,
2899                               struct regcache *regcache,
2900                               int reg_nr, const gdb_byte *buffer)
2901 {
2902   struct gdbarch *regcache_arch = get_regcache_arch (regcache);
2903   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch); 
2904
2905   gdb_assert (regcache_arch == gdbarch);
2906
2907   if (IS_SPE_PSEUDOREG (tdep, reg_nr))
2908     e500_pseudo_register_write (gdbarch, regcache, reg_nr, buffer);
2909   else if (IS_DFP_PSEUDOREG (tdep, reg_nr))
2910     dfp_pseudo_register_write (gdbarch, regcache, reg_nr, buffer);
2911   else if (IS_VSX_PSEUDOREG (tdep, reg_nr))
2912     vsx_pseudo_register_write (gdbarch, regcache, reg_nr, buffer);
2913   else if (IS_EFP_PSEUDOREG (tdep, reg_nr))
2914     efpr_pseudo_register_write (gdbarch, regcache, reg_nr, buffer);
2915   else
2916     internal_error (__FILE__, __LINE__,
2917                     _("rs6000_pseudo_register_write: "
2918                     "called on unexpected register '%s' (%d)"),
2919                     gdbarch_register_name (gdbarch, reg_nr), reg_nr);
2920 }
2921
2922 /* Convert a DBX STABS register number to a GDB register number.  */
2923 static int
2924 rs6000_stab_reg_to_regnum (struct gdbarch *gdbarch, int num)
2925 {
2926   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
2927
2928   if (0 <= num && num <= 31)
2929     return tdep->ppc_gp0_regnum + num;
2930   else if (32 <= num && num <= 63)
2931     /* FIXME: jimb/2004-05-05: What should we do when the debug info
2932        specifies registers the architecture doesn't have?  Our
2933        callers don't check the value we return.  */
2934     return tdep->ppc_fp0_regnum + (num - 32);
2935   else if (77 <= num && num <= 108)
2936     return tdep->ppc_vr0_regnum + (num - 77);
2937   else if (1200 <= num && num < 1200 + 32)
2938     return tdep->ppc_ev0_upper_regnum + (num - 1200);
2939   else
2940     switch (num)
2941       {
2942       case 64: 
2943         return tdep->ppc_mq_regnum;
2944       case 65:
2945         return tdep->ppc_lr_regnum;
2946       case 66: 
2947         return tdep->ppc_ctr_regnum;
2948       case 76: 
2949         return tdep->ppc_xer_regnum;
2950       case 109:
2951         return tdep->ppc_vrsave_regnum;
2952       case 110:
2953         return tdep->ppc_vrsave_regnum - 1; /* vscr */
2954       case 111:
2955         return tdep->ppc_acc_regnum;
2956       case 112:
2957         return tdep->ppc_spefscr_regnum;
2958       default: 
2959         return num;
2960       }
2961 }
2962
2963
2964 /* Convert a Dwarf 2 register number to a GDB register number.  */
2965 static int
2966 rs6000_dwarf2_reg_to_regnum (struct gdbarch *gdbarch, int num)
2967 {
2968   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
2969
2970   if (0 <= num && num <= 31)
2971     return tdep->ppc_gp0_regnum + num;
2972   else if (32 <= num && num <= 63)
2973     /* FIXME: jimb/2004-05-05: What should we do when the debug info
2974        specifies registers the architecture doesn't have?  Our
2975        callers don't check the value we return.  */
2976     return tdep->ppc_fp0_regnum + (num - 32);
2977   else if (1124 <= num && num < 1124 + 32)
2978     return tdep->ppc_vr0_regnum + (num - 1124);
2979   else if (1200 <= num && num < 1200 + 32)
2980     return tdep->ppc_ev0_upper_regnum + (num - 1200);
2981   else
2982     switch (num)
2983       {
2984       case 64:
2985         return tdep->ppc_cr_regnum;
2986       case 67:
2987         return tdep->ppc_vrsave_regnum - 1; /* vscr */
2988       case 99:
2989         return tdep->ppc_acc_regnum;
2990       case 100:
2991         return tdep->ppc_mq_regnum;
2992       case 101:
2993         return tdep->ppc_xer_regnum;
2994       case 108:
2995         return tdep->ppc_lr_regnum;
2996       case 109:
2997         return tdep->ppc_ctr_regnum;
2998       case 356:
2999         return tdep->ppc_vrsave_regnum;
3000       case 612:
3001         return tdep->ppc_spefscr_regnum;
3002       default:
3003         return num;
3004       }
3005 }
3006
3007 /* Translate a .eh_frame register to DWARF register, or adjust a
3008    .debug_frame register.  */
3009
3010 static int
3011 rs6000_adjust_frame_regnum (struct gdbarch *gdbarch, int num, int eh_frame_p)
3012 {
3013   /* GCC releases before 3.4 use GCC internal register numbering in
3014      .debug_frame (and .debug_info, et cetera).  The numbering is
3015      different from the standard SysV numbering for everything except
3016      for GPRs and FPRs.  We can not detect this problem in most cases
3017      - to get accurate debug info for variables living in lr, ctr, v0,
3018      et cetera, use a newer version of GCC.  But we must detect
3019      one important case - lr is in column 65 in .debug_frame output,
3020      instead of 108.
3021
3022      GCC 3.4, and the "hammer" branch, have a related problem.  They
3023      record lr register saves in .debug_frame as 108, but still record
3024      the return column as 65.  We fix that up too.
3025
3026      We can do this because 65 is assigned to fpsr, and GCC never
3027      generates debug info referring to it.  To add support for
3028      handwritten debug info that restores fpsr, we would need to add a
3029      producer version check to this.  */
3030   if (!eh_frame_p)
3031     {
3032       if (num == 65)
3033         return 108;
3034       else
3035         return num;
3036     }
3037
3038   /* .eh_frame is GCC specific.  For binary compatibility, it uses GCC
3039      internal register numbering; translate that to the standard DWARF2
3040      register numbering.  */
3041   if (0 <= num && num <= 63)    /* r0-r31,fp0-fp31 */
3042     return num;
3043   else if (68 <= num && num <= 75) /* cr0-cr8 */
3044     return num - 68 + 86;
3045   else if (77 <= num && num <= 108) /* vr0-vr31 */
3046     return num - 77 + 1124;
3047   else
3048     switch (num)
3049       {
3050       case 64: /* mq */
3051         return 100;
3052       case 65: /* lr */
3053         return 108;
3054       case 66: /* ctr */
3055         return 109;
3056       case 76: /* xer */
3057         return 101;
3058       case 109: /* vrsave */
3059         return 356;
3060       case 110: /* vscr */
3061         return 67;
3062       case 111: /* spe_acc */
3063         return 99;
3064       case 112: /* spefscr */
3065         return 612;
3066       default:
3067         return num;
3068       }
3069 }
3070 \f
3071
3072 /* Handling the various POWER/PowerPC variants.  */
3073
3074 /* Information about a particular processor variant.  */
3075
3076 struct variant
3077   {
3078     /* Name of this variant.  */
3079     char *name;
3080
3081     /* English description of the variant.  */
3082     char *description;
3083
3084     /* bfd_arch_info.arch corresponding to variant.  */
3085     enum bfd_architecture arch;
3086
3087     /* bfd_arch_info.mach corresponding to variant.  */
3088     unsigned long mach;
3089
3090     /* Target description for this variant.  */
3091     struct target_desc **tdesc;
3092   };
3093
3094 static struct variant variants[] =
3095 {
3096   {"powerpc", "PowerPC user-level", bfd_arch_powerpc,
3097    bfd_mach_ppc, &tdesc_powerpc_altivec32},
3098   {"power", "POWER user-level", bfd_arch_rs6000,
3099    bfd_mach_rs6k, &tdesc_rs6000},
3100   {"403", "IBM PowerPC 403", bfd_arch_powerpc,
3101    bfd_mach_ppc_403, &tdesc_powerpc_403},
3102   {"405", "IBM PowerPC 405", bfd_arch_powerpc,
3103    bfd_mach_ppc_405, &tdesc_powerpc_405},
3104   {"601", "Motorola PowerPC 601", bfd_arch_powerpc,
3105    bfd_mach_ppc_601, &tdesc_powerpc_601},
3106   {"602", "Motorola PowerPC 602", bfd_arch_powerpc,
3107    bfd_mach_ppc_602, &tdesc_powerpc_602},
3108   {"603", "Motorola/IBM PowerPC 603 or 603e", bfd_arch_powerpc,
3109    bfd_mach_ppc_603, &tdesc_powerpc_603},
3110   {"604", "Motorola PowerPC 604 or 604e", bfd_arch_powerpc,
3111    604, &tdesc_powerpc_604},
3112   {"403GC", "IBM PowerPC 403GC", bfd_arch_powerpc,
3113    bfd_mach_ppc_403gc, &tdesc_powerpc_403gc},
3114   {"505", "Motorola PowerPC 505", bfd_arch_powerpc,
3115    bfd_mach_ppc_505, &tdesc_powerpc_505},
3116   {"860", "Motorola PowerPC 860 or 850", bfd_arch_powerpc,
3117    bfd_mach_ppc_860, &tdesc_powerpc_860},
3118   {"750", "Motorola/IBM PowerPC 750 or 740", bfd_arch_powerpc,
3119    bfd_mach_ppc_750, &tdesc_powerpc_750},
3120   {"7400", "Motorola/IBM PowerPC 7400 (G4)", bfd_arch_powerpc,
3121    bfd_mach_ppc_7400, &tdesc_powerpc_7400},
3122   {"e500", "Motorola PowerPC e500", bfd_arch_powerpc,
3123    bfd_mach_ppc_e500, &tdesc_powerpc_e500},
3124
3125   /* 64-bit */
3126   {"powerpc64", "PowerPC 64-bit user-level", bfd_arch_powerpc,
3127    bfd_mach_ppc64, &tdesc_powerpc_altivec64},
3128   {"620", "Motorola PowerPC 620", bfd_arch_powerpc,
3129    bfd_mach_ppc_620, &tdesc_powerpc_64},
3130   {"630", "Motorola PowerPC 630", bfd_arch_powerpc,
3131    bfd_mach_ppc_630, &tdesc_powerpc_64},
3132   {"a35", "PowerPC A35", bfd_arch_powerpc,
3133    bfd_mach_ppc_a35, &tdesc_powerpc_64},
3134   {"rs64ii", "PowerPC rs64ii", bfd_arch_powerpc,
3135    bfd_mach_ppc_rs64ii, &tdesc_powerpc_64},
3136   {"rs64iii", "PowerPC rs64iii", bfd_arch_powerpc,
3137    bfd_mach_ppc_rs64iii, &tdesc_powerpc_64},
3138
3139   /* FIXME: I haven't checked the register sets of the following.  */
3140   {"rs1", "IBM POWER RS1", bfd_arch_rs6000,
3141    bfd_mach_rs6k_rs1, &tdesc_rs6000},
3142   {"rsc", "IBM POWER RSC", bfd_arch_rs6000,
3143    bfd_mach_rs6k_rsc, &tdesc_rs6000},
3144   {"rs2", "IBM POWER RS2", bfd_arch_rs6000,
3145    bfd_mach_rs6k_rs2, &tdesc_rs6000},
3146
3147   {0, 0, (enum bfd_architecture) 0, 0, 0}
3148 };
3149
3150 /* Return the variant corresponding to architecture ARCH and machine number
3151    MACH.  If no such variant exists, return null.  */
3152
3153 static const struct variant *
3154 find_variant_by_arch (enum bfd_architecture arch, unsigned long mach)
3155 {
3156   const struct variant *v;
3157
3158   for (v = variants; v->name; v++)
3159     if (arch == v->arch && mach == v->mach)
3160       return v;
3161
3162   return NULL;
3163 }
3164
3165 static int
3166 gdb_print_insn_powerpc (bfd_vma memaddr, disassemble_info *info)
3167 {
3168   if (info->endian == BFD_ENDIAN_BIG)
3169     return print_insn_big_powerpc (memaddr, info);
3170   else
3171     return print_insn_little_powerpc (memaddr, info);
3172 }
3173 \f
3174 static CORE_ADDR
3175 rs6000_unwind_pc (struct gdbarch *gdbarch, struct frame_info *next_frame)
3176 {
3177   return frame_unwind_register_unsigned (next_frame,
3178                                          gdbarch_pc_regnum (gdbarch));
3179 }
3180
3181 static struct frame_id
3182 rs6000_dummy_id (struct gdbarch *gdbarch, struct frame_info *this_frame)
3183 {
3184   return frame_id_build (get_frame_register_unsigned
3185                           (this_frame, gdbarch_sp_regnum (gdbarch)),
3186                          get_frame_pc (this_frame));
3187 }
3188
3189 struct rs6000_frame_cache
3190 {
3191   CORE_ADDR base;
3192   CORE_ADDR initial_sp;
3193   struct trad_frame_saved_reg *saved_regs;
3194 };
3195
3196 static struct rs6000_frame_cache *
3197 rs6000_frame_cache (struct frame_info *this_frame, void **this_cache)
3198 {
3199   struct rs6000_frame_cache *cache;
3200   struct gdbarch *gdbarch = get_frame_arch (this_frame);
3201   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
3202   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
3203   struct rs6000_framedata fdata;
3204   int wordsize = tdep->wordsize;
3205   CORE_ADDR func, pc;
3206
3207   if ((*this_cache) != NULL)
3208     return (struct rs6000_frame_cache *) (*this_cache);
3209   cache = FRAME_OBSTACK_ZALLOC (struct rs6000_frame_cache);
3210   (*this_cache) = cache;
3211   cache->saved_regs = trad_frame_alloc_saved_regs (this_frame);
3212
3213   func = get_frame_func (this_frame);
3214   pc = get_frame_pc (this_frame);
3215   skip_prologue (gdbarch, func, pc, &fdata);
3216
3217   /* Figure out the parent's stack pointer.  */
3218
3219   /* NOTE: cagney/2002-04-14: The ->frame points to the inner-most
3220      address of the current frame.  Things might be easier if the
3221      ->frame pointed to the outer-most address of the frame.  In
3222      the mean time, the address of the prev frame is used as the
3223      base address of this frame.  */
3224   cache->base = get_frame_register_unsigned
3225                 (this_frame, gdbarch_sp_regnum (gdbarch));
3226
3227   /* If the function appears to be frameless, check a couple of likely
3228      indicators that we have simply failed to find the frame setup.
3229      Two common cases of this are missing symbols (i.e.
3230      get_frame_func returns the wrong address or 0), and assembly
3231      stubs which have a fast exit path but set up a frame on the slow
3232      path.
3233
3234      If the LR appears to return to this function, then presume that
3235      we have an ABI compliant frame that we failed to find.  */
3236   if (fdata.frameless && fdata.lr_offset == 0)
3237     {
3238       CORE_ADDR saved_lr;
3239       int make_frame = 0;
3240
3241       saved_lr = get_frame_register_unsigned (this_frame, tdep->ppc_lr_regnum);
3242       if (func == 0 && saved_lr == pc)
3243         make_frame = 1;
3244       else if (func != 0)
3245         {
3246           CORE_ADDR saved_func = get_pc_function_start (saved_lr);
3247           if (func == saved_func)
3248             make_frame = 1;
3249         }
3250
3251       if (make_frame)
3252         {
3253           fdata.frameless = 0;
3254           fdata.lr_offset = tdep->lr_frame_offset;
3255         }
3256     }
3257
3258   if (!fdata.frameless)
3259     {
3260       /* Frameless really means stackless.  */
3261       LONGEST backchain;
3262
3263       if (safe_read_memory_integer (cache->base, wordsize,
3264                                     byte_order, &backchain))
3265         cache->base = (CORE_ADDR) backchain;
3266     }
3267
3268   trad_frame_set_value (cache->saved_regs,
3269                         gdbarch_sp_regnum (gdbarch), cache->base);
3270
3271   /* if != -1, fdata.saved_fpr is the smallest number of saved_fpr.
3272      All fpr's from saved_fpr to fp31 are saved.  */
3273
3274   if (fdata.saved_fpr >= 0)
3275     {
3276       int i;
3277       CORE_ADDR fpr_addr = cache->base + fdata.fpr_offset;
3278
3279       /* If skip_prologue says floating-point registers were saved,
3280          but the current architecture has no floating-point registers,
3281          then that's strange.  But we have no indices to even record
3282          the addresses under, so we just ignore it.  */
3283       if (ppc_floating_point_unit_p (gdbarch))
3284         for (i = fdata.saved_fpr; i < ppc_num_fprs; i++)
3285           {
3286             cache->saved_regs[tdep->ppc_fp0_regnum + i].addr = fpr_addr;
3287             fpr_addr += 8;
3288           }
3289     }
3290
3291   /* if != -1, fdata.saved_gpr is the smallest number of saved_gpr.
3292      All gpr's from saved_gpr to gpr31 are saved (except during the
3293      prologue).  */
3294
3295   if (fdata.saved_gpr >= 0)
3296     {
3297       int i;
3298       CORE_ADDR gpr_addr = cache->base + fdata.gpr_offset;
3299       for (i = fdata.saved_gpr; i < ppc_num_gprs; i++)
3300         {
3301           if (fdata.gpr_mask & (1U << i))
3302             cache->saved_regs[tdep->ppc_gp0_regnum + i].addr = gpr_addr;
3303           gpr_addr += wordsize;
3304         }
3305     }
3306
3307   /* if != -1, fdata.saved_vr is the smallest number of saved_vr.
3308      All vr's from saved_vr to vr31 are saved.  */
3309   if (tdep->ppc_vr0_regnum != -1 && tdep->ppc_vrsave_regnum != -1)
3310     {
3311       if (fdata.saved_vr >= 0)
3312         {
3313           int i;
3314           CORE_ADDR vr_addr = cache->base + fdata.vr_offset;
3315           for (i = fdata.saved_vr; i < 32; i++)
3316             {
3317               cache->saved_regs[tdep->ppc_vr0_regnum + i].addr = vr_addr;
3318               vr_addr += register_size (gdbarch, tdep->ppc_vr0_regnum);
3319             }
3320         }
3321     }
3322
3323   /* if != -1, fdata.saved_ev is the smallest number of saved_ev.
3324      All vr's from saved_ev to ev31 are saved. ?????  */
3325   if (tdep->ppc_ev0_regnum != -1)
3326     {
3327       if (fdata.saved_ev >= 0)
3328         {
3329           int i;
3330           CORE_ADDR ev_addr = cache->base + fdata.ev_offset;
3331           CORE_ADDR off = (byte_order == BFD_ENDIAN_BIG ? 4 : 0);
3332
3333           for (i = fdata.saved_ev; i < ppc_num_gprs; i++)
3334             {
3335               cache->saved_regs[tdep->ppc_ev0_regnum + i].addr = ev_addr;
3336               cache->saved_regs[tdep->ppc_gp0_regnum + i].addr = ev_addr + off;
3337               ev_addr += register_size (gdbarch, tdep->ppc_ev0_regnum);
3338             }
3339         }
3340     }
3341
3342   /* If != 0, fdata.cr_offset is the offset from the frame that
3343      holds the CR.  */
3344   if (fdata.cr_offset != 0)
3345     cache->saved_regs[tdep->ppc_cr_regnum].addr
3346       = cache->base + fdata.cr_offset;
3347
3348   /* If != 0, fdata.lr_offset is the offset from the frame that
3349      holds the LR.  */
3350   if (fdata.lr_offset != 0)
3351     cache->saved_regs[tdep->ppc_lr_regnum].addr
3352       = cache->base + fdata.lr_offset;
3353   else if (fdata.lr_register != -1)
3354     cache->saved_regs[tdep->ppc_lr_regnum].realreg = fdata.lr_register;
3355   /* The PC is found in the link register.  */
3356   cache->saved_regs[gdbarch_pc_regnum (gdbarch)] =
3357     cache->saved_regs[tdep->ppc_lr_regnum];
3358
3359   /* If != 0, fdata.vrsave_offset is the offset from the frame that
3360      holds the VRSAVE.  */
3361   if (fdata.vrsave_offset != 0)
3362     cache->saved_regs[tdep->ppc_vrsave_regnum].addr
3363       = cache->base + fdata.vrsave_offset;
3364
3365   if (fdata.alloca_reg < 0)
3366     /* If no alloca register used, then fi->frame is the value of the
3367        %sp for this frame, and it is good enough.  */
3368     cache->initial_sp
3369       = get_frame_register_unsigned (this_frame, gdbarch_sp_regnum (gdbarch));
3370   else
3371     cache->initial_sp
3372       = get_frame_register_unsigned (this_frame, fdata.alloca_reg);
3373
3374   return cache;
3375 }
3376
3377 static void
3378 rs6000_frame_this_id (struct frame_info *this_frame, void **this_cache,
3379                       struct frame_id *this_id)
3380 {
3381   struct rs6000_frame_cache *info = rs6000_frame_cache (this_frame,
3382                                                         this_cache);
3383   /* This marks the outermost frame.  */
3384   if (info->base == 0)
3385     return;
3386
3387   (*this_id) = frame_id_build (info->base, get_frame_func (this_frame));
3388 }
3389
3390 static struct value *
3391 rs6000_frame_prev_register (struct frame_info *this_frame,
3392                             void **this_cache, int regnum)
3393 {
3394   struct rs6000_frame_cache *info = rs6000_frame_cache (this_frame,
3395                                                         this_cache);
3396   return trad_frame_get_prev_register (this_frame, info->saved_regs, regnum);
3397 }
3398
3399 static const struct frame_unwind rs6000_frame_unwind =
3400 {
3401   NORMAL_FRAME,
3402   default_frame_unwind_stop_reason,
3403   rs6000_frame_this_id,
3404   rs6000_frame_prev_register,
3405   NULL,
3406   default_frame_sniffer
3407 };
3408
3409 /* Allocate and initialize a frame cache for an epilogue frame.
3410    SP is restored and prev-PC is stored in LR.  */
3411
3412 static struct rs6000_frame_cache *
3413 rs6000_epilogue_frame_cache (struct frame_info *this_frame, void **this_cache)
3414 {
3415   struct rs6000_frame_cache *cache;
3416   struct gdbarch *gdbarch = get_frame_arch (this_frame);
3417   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
3418
3419   if (*this_cache)
3420     return (struct rs6000_frame_cache *) *this_cache;
3421
3422   cache = FRAME_OBSTACK_ZALLOC (struct rs6000_frame_cache);
3423   (*this_cache) = cache;
3424   cache->saved_regs = trad_frame_alloc_saved_regs (this_frame);
3425
3426   TRY
3427     {
3428       /* At this point the stack looks as if we just entered the
3429          function, and the return address is stored in LR.  */
3430       CORE_ADDR sp, lr;
3431
3432       sp = get_frame_register_unsigned (this_frame, gdbarch_sp_regnum (gdbarch));
3433       lr = get_frame_register_unsigned (this_frame, tdep->ppc_lr_regnum);
3434
3435       cache->base = sp;
3436       cache->initial_sp = sp;
3437
3438       trad_frame_set_value (cache->saved_regs,
3439                             gdbarch_pc_regnum (gdbarch), lr);
3440     }
3441   CATCH (ex, RETURN_MASK_ERROR)
3442     {
3443       if (ex.error != NOT_AVAILABLE_ERROR)
3444         throw_exception (ex);
3445     }
3446   END_CATCH
3447
3448   return cache;
3449 }
3450
3451 /* Implementation of frame_unwind.this_id, as defined in frame_unwind.h.
3452    Return the frame ID of an epilogue frame.  */
3453
3454 static void
3455 rs6000_epilogue_frame_this_id (struct frame_info *this_frame,
3456                                void **this_cache, struct frame_id *this_id)
3457 {
3458   CORE_ADDR pc;
3459   struct rs6000_frame_cache *info =
3460     rs6000_epilogue_frame_cache (this_frame, this_cache);
3461
3462   pc = get_frame_func (this_frame);
3463   if (info->base == 0)
3464     (*this_id) = frame_id_build_unavailable_stack (pc);
3465   else
3466     (*this_id) = frame_id_build (info->base, pc);
3467 }
3468
3469 /* Implementation of frame_unwind.prev_register, as defined in frame_unwind.h.
3470    Return the register value of REGNUM in previous frame.  */
3471
3472 static struct value *
3473 rs6000_epilogue_frame_prev_register (struct frame_info *this_frame,
3474                                      void **this_cache, int regnum)
3475 {
3476   struct rs6000_frame_cache *info =
3477     rs6000_epilogue_frame_cache (this_frame, this_cache);
3478   return trad_frame_get_prev_register (this_frame, info->saved_regs, regnum);
3479 }
3480
3481 /* Implementation of frame_unwind.sniffer, as defined in frame_unwind.h.
3482    Check whether this an epilogue frame.  */
3483
3484 static int
3485 rs6000_epilogue_frame_sniffer (const struct frame_unwind *self,
3486                                struct frame_info *this_frame,
3487                                void **this_prologue_cache)
3488 {
3489   if (frame_relative_level (this_frame) == 0)
3490     return rs6000_in_function_epilogue_frame_p (this_frame,
3491                                                 get_frame_arch (this_frame),
3492                                                 get_frame_pc (this_frame));
3493   else
3494     return 0;
3495 }
3496
3497 /* Frame unwinder for epilogue frame.  This is required for reverse step-over
3498    a function without debug information.  */
3499
3500 static const struct frame_unwind rs6000_epilogue_frame_unwind =
3501 {
3502   NORMAL_FRAME,
3503   default_frame_unwind_stop_reason,
3504   rs6000_epilogue_frame_this_id, rs6000_epilogue_frame_prev_register,
3505   NULL,
3506   rs6000_epilogue_frame_sniffer
3507 };
3508 \f
3509
3510 static CORE_ADDR
3511 rs6000_frame_base_address (struct frame_info *this_frame, void **this_cache)
3512 {
3513   struct rs6000_frame_cache *info = rs6000_frame_cache (this_frame,
3514                                                         this_cache);
3515   return info->initial_sp;
3516 }
3517
3518 static const struct frame_base rs6000_frame_base = {
3519   &rs6000_frame_unwind,
3520   rs6000_frame_base_address,
3521   rs6000_frame_base_address,
3522   rs6000_frame_base_address
3523 };
3524
3525 static const struct frame_base *
3526 rs6000_frame_base_sniffer (struct frame_info *this_frame)
3527 {
3528   return &rs6000_frame_base;
3529 }
3530
3531 /* DWARF-2 frame support.  Used to handle the detection of
3532   clobbered registers during function calls.  */
3533
3534 static void
3535 ppc_dwarf2_frame_init_reg (struct gdbarch *gdbarch, int regnum,
3536                             struct dwarf2_frame_state_reg *reg,
3537                             struct frame_info *this_frame)
3538 {
3539   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
3540
3541   /* PPC32 and PPC64 ABI's are the same regarding volatile and
3542      non-volatile registers.  We will use the same code for both.  */
3543
3544   /* Call-saved GP registers.  */
3545   if ((regnum >= tdep->ppc_gp0_regnum + 14
3546       && regnum <= tdep->ppc_gp0_regnum + 31)
3547       || (regnum == tdep->ppc_gp0_regnum + 1))
3548     reg->how = DWARF2_FRAME_REG_SAME_VALUE;
3549
3550   /* Call-clobbered GP registers.  */
3551   if ((regnum >= tdep->ppc_gp0_regnum + 3
3552       && regnum <= tdep->ppc_gp0_regnum + 12)
3553       || (regnum == tdep->ppc_gp0_regnum))
3554     reg->how = DWARF2_FRAME_REG_UNDEFINED;
3555
3556   /* Deal with FP registers, if supported.  */
3557   if (tdep->ppc_fp0_regnum >= 0)
3558     {
3559       /* Call-saved FP registers.  */
3560       if ((regnum >= tdep->ppc_fp0_regnum + 14
3561           && regnum <= tdep->ppc_fp0_regnum + 31))
3562         reg->how = DWARF2_FRAME_REG_SAME_VALUE;
3563
3564       /* Call-clobbered FP registers.  */
3565       if ((regnum >= tdep->ppc_fp0_regnum
3566           && regnum <= tdep->ppc_fp0_regnum + 13))
3567         reg->how = DWARF2_FRAME_REG_UNDEFINED;
3568     }
3569
3570   /* Deal with ALTIVEC registers, if supported.  */
3571   if (tdep->ppc_vr0_regnum > 0 && tdep->ppc_vrsave_regnum > 0)
3572     {
3573       /* Call-saved Altivec registers.  */
3574       if ((regnum >= tdep->ppc_vr0_regnum + 20
3575           && regnum <= tdep->ppc_vr0_regnum + 31)
3576           || regnum == tdep->ppc_vrsave_regnum)
3577         reg->how = DWARF2_FRAME_REG_SAME_VALUE;
3578
3579       /* Call-clobbered Altivec registers.  */
3580       if ((regnum >= tdep->ppc_vr0_regnum
3581           && regnum <= tdep->ppc_vr0_regnum + 19))
3582         reg->how = DWARF2_FRAME_REG_UNDEFINED;
3583     }
3584
3585   /* Handle PC register and Stack Pointer correctly.  */
3586   if (regnum == gdbarch_pc_regnum (gdbarch))
3587     reg->how = DWARF2_FRAME_REG_RA;
3588   else if (regnum == gdbarch_sp_regnum (gdbarch))
3589     reg->how = DWARF2_FRAME_REG_CFA;
3590 }
3591
3592
3593 /* Return true if a .gnu_attributes section exists in BFD and it
3594    indicates we are using SPE extensions OR if a .PPC.EMB.apuinfo
3595    section exists in BFD and it indicates that SPE extensions are in
3596    use.  Check the .gnu.attributes section first, as the binary might be
3597    compiled for SPE, but not actually using SPE instructions.  */
3598
3599 static int
3600 bfd_uses_spe_extensions (bfd *abfd)
3601 {
3602   asection *sect;
3603   gdb_byte *contents = NULL;
3604   bfd_size_type size;
3605   gdb_byte *ptr;
3606   int success = 0;
3607   int vector_abi;
3608
3609   if (!abfd)
3610     return 0;
3611
3612 #ifdef HAVE_ELF
3613   /* Using Tag_GNU_Power_ABI_Vector here is a bit of a hack, as the user
3614      could be using the SPE vector abi without actually using any spe
3615      bits whatsoever.  But it's close enough for now.  */
3616   vector_abi = bfd_elf_get_obj_attr_int (abfd, OBJ_ATTR_GNU,
3617                                          Tag_GNU_Power_ABI_Vector);
3618   if (vector_abi == 3)
3619     return 1;
3620 #endif
3621
3622   sect = bfd_get_section_by_name (abfd, ".PPC.EMB.apuinfo");
3623   if (!sect)
3624     return 0;
3625
3626   size = bfd_get_section_size (sect);
3627   contents = (gdb_byte *) xmalloc (size);
3628   if (!bfd_get_section_contents (abfd, sect, contents, 0, size))
3629     {
3630       xfree (contents);
3631       return 0;
3632     }
3633
3634   /* Parse the .PPC.EMB.apuinfo section.  The layout is as follows:
3635
3636      struct {
3637        uint32 name_len;
3638        uint32 data_len;
3639        uint32 type;
3640        char name[name_len rounded up to 4-byte alignment];
3641        char data[data_len];
3642      };
3643
3644      Technically, there's only supposed to be one such structure in a
3645      given apuinfo section, but the linker is not always vigilant about
3646      merging apuinfo sections from input files.  Just go ahead and parse
3647      them all, exiting early when we discover the binary uses SPE
3648      insns.
3649
3650      It's not specified in what endianness the information in this
3651      section is stored.  Assume that it's the endianness of the BFD.  */
3652   ptr = contents;
3653   while (1)
3654     {
3655       unsigned int name_len;
3656       unsigned int data_len;
3657       unsigned int type;
3658
3659       /* If we can't read the first three fields, we're done.  */
3660       if (size < 12)
3661         break;
3662
3663       name_len = bfd_get_32 (abfd, ptr);
3664       name_len = (name_len + 3) & ~3U; /* Round to 4 bytes.  */
3665       data_len = bfd_get_32 (abfd, ptr + 4);
3666       type = bfd_get_32 (abfd, ptr + 8);
3667       ptr += 12;
3668
3669       /* The name must be "APUinfo\0".  */
3670       if (name_len != 8
3671           && strcmp ((const char *) ptr, "APUinfo") != 0)
3672         break;
3673       ptr += name_len;
3674
3675       /* The type must be 2.  */
3676       if (type != 2)
3677         break;
3678
3679       /* The data is stored as a series of uint32.  The upper half of
3680          each uint32 indicates the particular APU used and the lower
3681          half indicates the revision of that APU.  We just care about
3682          the upper half.  */
3683
3684       /* Not 4-byte quantities.  */
3685       if (data_len & 3U)
3686         break;
3687
3688       while (data_len)
3689         {
3690           unsigned int apuinfo = bfd_get_32 (abfd, ptr);
3691           unsigned int apu = apuinfo >> 16;
3692           ptr += 4;
3693           data_len -= 4;
3694
3695           /* The SPE APU is 0x100; the SPEFP APU is 0x101.  Accept
3696              either.  */
3697           if (apu == 0x100 || apu == 0x101)
3698             {
3699               success = 1;
3700               data_len = 0;
3701             }
3702         }
3703
3704       if (success)
3705         break;
3706     }
3707
3708   xfree (contents);
3709   return success;
3710 }
3711
3712 /* These are macros for parsing instruction fields (I.1.6.28)  */
3713
3714 #define PPC_FIELD(value, from, len) \
3715         (((value) >> (32 - (from) - (len))) & ((1 << (len)) - 1))
3716 #define PPC_SEXT(v, bs) \
3717         ((((CORE_ADDR) (v) & (((CORE_ADDR) 1 << (bs)) - 1)) \
3718           ^ ((CORE_ADDR) 1 << ((bs) - 1))) \
3719          - ((CORE_ADDR) 1 << ((bs) - 1)))
3720 #define PPC_OP6(insn)   PPC_FIELD (insn, 0, 6)
3721 #define PPC_EXTOP(insn) PPC_FIELD (insn, 21, 10)
3722 #define PPC_RT(insn)    PPC_FIELD (insn, 6, 5)
3723 #define PPC_RS(insn)    PPC_FIELD (insn, 6, 5)
3724 #define PPC_RA(insn)    PPC_FIELD (insn, 11, 5)
3725 #define PPC_RB(insn)    PPC_FIELD (insn, 16, 5)
3726 #define PPC_NB(insn)    PPC_FIELD (insn, 16, 5)
3727 #define PPC_VRT(insn)   PPC_FIELD (insn, 6, 5)
3728 #define PPC_FRT(insn)   PPC_FIELD (insn, 6, 5)
3729 #define PPC_SPR(insn)   (PPC_FIELD (insn, 11, 5) \
3730                         | (PPC_FIELD (insn, 16, 5) << 5))
3731 #define PPC_BO(insn)    PPC_FIELD (insn, 6, 5)
3732 #define PPC_T(insn)     PPC_FIELD (insn, 6, 5)
3733 #define PPC_D(insn)     PPC_SEXT (PPC_FIELD (insn, 16, 16), 16)
3734 #define PPC_DS(insn)    PPC_SEXT (PPC_FIELD (insn, 16, 14), 14)
3735 #define PPC_BIT(insn,n) ((insn & (1 << (31 - (n)))) ? 1 : 0)
3736 #define PPC_OE(insn)    PPC_BIT (insn, 21)
3737 #define PPC_RC(insn)    PPC_BIT (insn, 31)
3738 #define PPC_Rc(insn)    PPC_BIT (insn, 21)
3739 #define PPC_LK(insn)    PPC_BIT (insn, 31)
3740 #define PPC_TX(insn)    PPC_BIT (insn, 31)
3741 #define PPC_LEV(insn)   PPC_FIELD (insn, 20, 7)
3742
3743 #define PPC_XT(insn)    ((PPC_TX (insn) << 5) | PPC_T (insn))
3744 #define PPC_XER_NB(xer) (xer & 0x7f)
3745
3746 /* Record Vector-Scalar Registers.
3747    For VSR less than 32, it's represented by an FPR and an VSR-upper register.
3748    Otherwise, it's just a VR register.  Record them accordingly.  */
3749
3750 static int
3751 ppc_record_vsr (struct regcache *regcache, struct gdbarch_tdep *tdep, int vsr)
3752 {
3753   if (vsr < 0 || vsr >= 64)
3754     return -1;
3755
3756   if (vsr >= 32)
3757     {
3758       if (tdep->ppc_vr0_regnum >= 0)
3759         record_full_arch_list_add_reg (regcache, tdep->ppc_vr0_regnum + vsr - 32);
3760     }
3761   else
3762     {
3763       if (tdep->ppc_fp0_regnum >= 0)
3764         record_full_arch_list_add_reg (regcache, tdep->ppc_fp0_regnum + vsr);
3765       if (tdep->ppc_vsr0_upper_regnum >= 0)
3766         record_full_arch_list_add_reg (regcache,
3767                                        tdep->ppc_vsr0_upper_regnum + vsr);
3768     }
3769
3770   return 0;
3771 }
3772
3773 /* Parse and record instructions primary opcode-4 at ADDR.
3774    Return 0 if successful.  */
3775
3776 static int
3777 ppc_process_record_op4 (struct gdbarch *gdbarch, struct regcache *regcache,
3778                         CORE_ADDR addr, uint32_t insn)
3779 {
3780   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
3781   int ext = PPC_FIELD (insn, 21, 11);
3782
3783   switch (ext & 0x3f)
3784     {
3785     case 32:            /* Vector Multiply-High-Add Signed Halfword Saturate */
3786     case 33:            /* Vector Multiply-High-Round-Add Signed Halfword Saturate */
3787     case 39:            /* Vector Multiply-Sum Unsigned Halfword Saturate */
3788     case 41:            /* Vector Multiply-Sum Signed Halfword Saturate */
3789       record_full_arch_list_add_reg (regcache, PPC_VSCR_REGNUM);
3790       /* FALL-THROUGH */
3791     case 42:            /* Vector Select */
3792     case 43:            /* Vector Permute */
3793     case 44:            /* Vector Shift Left Double by Octet Immediate */
3794     case 45:            /* Vector Permute and Exclusive-OR */
3795     case 60:            /* Vector Add Extended Unsigned Quadword Modulo */
3796     case 61:            /* Vector Add Extended & write Carry Unsigned Quadword */
3797     case 62:            /* Vector Subtract Extended Unsigned Quadword Modulo */
3798     case 63:            /* Vector Subtract Extended & write Carry Unsigned Quadword */
3799     case 34:            /* Vector Multiply-Low-Add Unsigned Halfword Modulo */
3800     case 36:            /* Vector Multiply-Sum Unsigned Byte Modulo */
3801     case 37:            /* Vector Multiply-Sum Mixed Byte Modulo */
3802     case 38:            /* Vector Multiply-Sum Unsigned Halfword Modulo */
3803     case 40:            /* Vector Multiply-Sum Signed Halfword Modulo */
3804     case 46:            /* Vector Multiply-Add Single-Precision */
3805     case 47:            /* Vector Negative Multiply-Subtract Single-Precision */
3806       record_full_arch_list_add_reg (regcache,
3807                                      tdep->ppc_vr0_regnum + PPC_VRT (insn));
3808       return 0;
3809     }
3810
3811   switch ((ext & 0x1ff))
3812     {
3813                         /* 5.16 Decimal Integer Arithmetic Instructions */
3814     case 1:             /* Decimal Add Modulo */
3815     case 65:            /* Decimal Subtract Modulo */
3816
3817       /* Bit-21 should be set.  */
3818       if (!PPC_BIT (insn, 21))
3819         break;
3820
3821       record_full_arch_list_add_reg (regcache,
3822                                      tdep->ppc_vr0_regnum + PPC_VRT (insn));
3823       record_full_arch_list_add_reg (regcache, tdep->ppc_cr_regnum);
3824       return 0;
3825     }
3826
3827   /* Bit-21 is used for RC */
3828   switch (ext & 0x3ff)
3829     {
3830     case 6:             /* Vector Compare Equal To Unsigned Byte */
3831     case 70:            /* Vector Compare Equal To Unsigned Halfword */
3832     case 134:           /* Vector Compare Equal To Unsigned Word */
3833     case 199:           /* Vector Compare Equal To Unsigned Doubleword */
3834     case 774:           /* Vector Compare Greater Than Signed Byte */
3835     case 838:           /* Vector Compare Greater Than Signed Halfword */
3836     case 902:           /* Vector Compare Greater Than Signed Word */
3837     case 967:           /* Vector Compare Greater Than Signed Doubleword */
3838     case 518:           /* Vector Compare Greater Than Unsigned Byte */
3839     case 646:           /* Vector Compare Greater Than Unsigned Word */
3840     case 582:           /* Vector Compare Greater Than Unsigned Halfword */
3841     case 711:           /* Vector Compare Greater Than Unsigned Doubleword */
3842     case 966:           /* Vector Compare Bounds Single-Precision */
3843     case 198:           /* Vector Compare Equal To Single-Precision */
3844     case 454:           /* Vector Compare Greater Than or Equal To Single-Precision */
3845     case 710:           /* Vector Compare Greater Than Single-Precision */
3846       if (PPC_Rc (insn))
3847         record_full_arch_list_add_reg (regcache, tdep->ppc_cr_regnum);
3848       record_full_arch_list_add_reg (regcache,
3849                                      tdep->ppc_vr0_regnum + PPC_VRT (insn));
3850       return 0;
3851     }
3852
3853   switch (ext)
3854     {
3855     case 142:           /* Vector Pack Unsigned Halfword Unsigned Saturate */
3856     case 206:           /* Vector Pack Unsigned Word Unsigned Saturate */
3857     case 270:           /* Vector Pack Signed Halfword Unsigned Saturate */
3858     case 334:           /* Vector Pack Signed Word Unsigned Saturate */
3859     case 398:           /* Vector Pack Signed Halfword Signed Saturate */
3860     case 462:           /* Vector Pack Signed Word Signed Saturate */
3861     case 1230:          /* Vector Pack Unsigned Doubleword Unsigned Saturate */
3862     case 1358:          /* Vector Pack Signed Doubleword Unsigned Saturate */
3863     case 1486:          /* Vector Pack Signed Doubleword Signed Saturate */
3864     case 512:           /* Vector Add Unsigned Byte Saturate */
3865     case 576:           /* Vector Add Unsigned Halfword Saturate */
3866     case 640:           /* Vector Add Unsigned Word Saturate */
3867     case 768:           /* Vector Add Signed Byte Saturate */
3868     case 832:           /* Vector Add Signed Halfword Saturate */
3869     case 896:           /* Vector Add Signed Word Saturate */
3870     case 1536:          /* Vector Subtract Unsigned Byte Saturate */
3871     case 1600:          /* Vector Subtract Unsigned Halfword Saturate */
3872     case 1664:          /* Vector Subtract Unsigned Word Saturate */
3873     case 1792:          /* Vector Subtract Signed Byte Saturate */
3874     case 1856:          /* Vector Subtract Signed Halfword Saturate */
3875     case 1920:          /* Vector Subtract Signed Word Saturate */
3876
3877     case 1544:          /* Vector Sum across Quarter Unsigned Byte Saturate */
3878     case 1800:          /* Vector Sum across Quarter Signed Byte Saturate */
3879     case 1608:          /* Vector Sum across Quarter Signed Halfword Saturate */
3880     case 1672:          /* Vector Sum across Half Signed Word Saturate */
3881     case 1928:          /* Vector Sum across Signed Word Saturate */
3882     case 970:           /* Vector Convert To Signed Fixed-Point Word Saturate */
3883     case 906:           /* Vector Convert To Unsigned Fixed-Point Word Saturate */
3884       record_full_arch_list_add_reg (regcache, PPC_VSCR_REGNUM);
3885       /* FALL-THROUGH */
3886     case 12:            /* Vector Merge High Byte */
3887     case 14:            /* Vector Pack Unsigned Halfword Unsigned Modulo */
3888     case 76:            /* Vector Merge High Halfword */
3889     case 78:            /* Vector Pack Unsigned Word Unsigned Modulo */
3890     case 140:           /* Vector Merge High Word */
3891     case 268:           /* Vector Merge Low Byte */
3892     case 332:           /* Vector Merge Low Halfword */
3893     case 396:           /* Vector Merge Low Word */
3894     case 526:           /* Vector Unpack High Signed Byte */
3895     case 590:           /* Vector Unpack High Signed Halfword */
3896     case 654:           /* Vector Unpack Low Signed Byte */
3897     case 718:           /* Vector Unpack Low Signed Halfword */
3898     case 782:           /* Vector Pack Pixel */
3899     case 846:           /* Vector Unpack High Pixel */
3900     case 974:           /* Vector Unpack Low Pixel */
3901     case 1102:          /* Vector Pack Unsigned Doubleword Unsigned Modulo */
3902     case 1614:          /* Vector Unpack High Signed Word */
3903     case 1676:          /* Vector Merge Odd Word */
3904     case 1742:          /* Vector Unpack Low Signed Word */
3905     case 1932:          /* Vector Merge Even Word */
3906     case 524:           /* Vector Splat Byte */
3907     case 588:           /* Vector Splat Halfword */
3908     case 652:           /* Vector Splat Word */
3909     case 780:           /* Vector Splat Immediate Signed Byte */
3910     case 844:           /* Vector Splat Immediate Signed Halfword */
3911     case 908:           /* Vector Splat Immediate Signed Word */
3912     case 452:           /* Vector Shift Left */
3913     case 708:           /* Vector Shift Right */
3914     case 1036:          /* Vector Shift Left by Octet */
3915     case 1100:          /* Vector Shift Right by Octet */
3916     case 0:             /* Vector Add Unsigned Byte Modulo */
3917     case 64:            /* Vector Add Unsigned Halfword Modulo */
3918     case 128:           /* Vector Add Unsigned Word Modulo */
3919     case 192:           /* Vector Add Unsigned Doubleword Modulo */
3920     case 256:           /* Vector Add Unsigned Quadword Modulo */
3921     case 320:           /* Vector Add & write Carry Unsigned Quadword */
3922     case 384:           /* Vector Add and Write Carry-Out Unsigned Word */
3923     case 8:             /* Vector Multiply Odd Unsigned Byte */
3924     case 72:            /* Vector Multiply Odd Unsigned Halfword */
3925     case 136:           /* Vector Multiply Odd Unsigned Word */
3926     case 264:           /* Vector Multiply Odd Signed Byte */
3927     case 328:           /* Vector Multiply Odd Signed Halfword */
3928     case 392:           /* Vector Multiply Odd Signed Word */
3929     case 520:           /* Vector Multiply Even Unsigned Byte */
3930     case 584:           /* Vector Multiply Even Unsigned Halfword */
3931     case 648:           /* Vector Multiply Even Unsigned Word */
3932     case 776:           /* Vector Multiply Even Signed Byte */
3933     case 840:           /* Vector Multiply Even Signed Halfword */
3934     case 904:           /* Vector Multiply Even Signed Word */
3935     case 137:           /* Vector Multiply Unsigned Word Modulo */
3936     case 1024:          /* Vector Subtract Unsigned Byte Modulo */
3937     case 1088:          /* Vector Subtract Unsigned Halfword Modulo */
3938     case 1152:          /* Vector Subtract Unsigned Word Modulo */
3939     case 1216:          /* Vector Subtract Unsigned Doubleword Modulo */
3940     case 1280:          /* Vector Subtract Unsigned Quadword Modulo */
3941     case 1344:          /* Vector Subtract & write Carry Unsigned Quadword */
3942     case 1408:          /* Vector Subtract and Write Carry-Out Unsigned Word */
3943     case 1282:          /* Vector Average Signed Byte */
3944     case 1346:          /* Vector Average Signed Halfword */
3945     case 1410:          /* Vector Average Signed Word */
3946     case 1026:          /* Vector Average Unsigned Byte */
3947     case 1090:          /* Vector Average Unsigned Halfword */
3948     case 1154:          /* Vector Average Unsigned Word */
3949     case 258:           /* Vector Maximum Signed Byte */
3950     case 322:           /* Vector Maximum Signed Halfword */
3951     case 386:           /* Vector Maximum Signed Word */
3952     case 450:           /* Vector Maximum Signed Doubleword */
3953     case 2:             /* Vector Maximum Unsigned Byte */
3954     case 66:            /* Vector Maximum Unsigned Halfword */
3955     case 130:           /* Vector Maximum Unsigned Word */
3956     case 194:           /* Vector Maximum Unsigned Doubleword */
3957     case 770:           /* Vector Minimum Signed Byte */
3958     case 834:           /* Vector Minimum Signed Halfword */
3959     case 898:           /* Vector Minimum Signed Word */
3960     case 962:           /* Vector Minimum Signed Doubleword */
3961     case 514:           /* Vector Minimum Unsigned Byte */
3962     case 578:           /* Vector Minimum Unsigned Halfword */
3963     case 642:           /* Vector Minimum Unsigned Word */
3964     case 706:           /* Vector Minimum Unsigned Doubleword */
3965     case 1028:          /* Vector Logical AND */
3966     case 1668:          /* Vector Logical Equivalent */
3967     case 1092:          /* Vector Logical AND with Complement */
3968     case 1412:          /* Vector Logical NAND */
3969     case 1348:          /* Vector Logical OR with Complement */
3970     case 1156:          /* Vector Logical OR */
3971     case 1284:          /* Vector Logical NOR */
3972     case 1220:          /* Vector Logical XOR */
3973     case 4:             /* Vector Rotate Left Byte */
3974     case 132:           /* Vector Rotate Left Word VX-form */
3975     case 68:            /* Vector Rotate Left Halfword */
3976     case 196:           /* Vector Rotate Left Doubleword */
3977     case 260:           /* Vector Shift Left Byte */
3978     case 388:           /* Vector Shift Left Word */
3979     case 324:           /* Vector Shift Left Halfword */
3980     case 1476:          /* Vector Shift Left Doubleword */
3981     case 516:           /* Vector Shift Right Byte */
3982     case 644:           /* Vector Shift Right Word */
3983     case 580:           /* Vector Shift Right Halfword */
3984     case 1732:          /* Vector Shift Right Doubleword */
3985     case 772:           /* Vector Shift Right Algebraic Byte */
3986     case 900:           /* Vector Shift Right Algebraic Word */
3987     case 836:           /* Vector Shift Right Algebraic Halfword */
3988     case 964:           /* Vector Shift Right Algebraic Doubleword */
3989     case 10:            /* Vector Add Single-Precision */
3990     case 74:            /* Vector Subtract Single-Precision */
3991     case 1034:          /* Vector Maximum Single-Precision */
3992     case 1098:          /* Vector Minimum Single-Precision */
3993     case 842:           /* Vector Convert From Signed Fixed-Point Word */
3994     case 778:           /* Vector Convert From Unsigned Fixed-Point Word */
3995     case 714:           /* Vector Round to Single-Precision Integer toward -Infinity */
3996     case 522:           /* Vector Round to Single-Precision Integer Nearest */
3997     case 650:           /* Vector Round to Single-Precision Integer toward +Infinity */
3998     case 586:           /* Vector Round to Single-Precision Integer toward Zero */
3999     case 394:           /* Vector 2 Raised to the Exponent Estimate Floating-Point */
4000     case 458:           /* Vector Log Base 2 Estimate Floating-Point */
4001     case 266:           /* Vector Reciprocal Estimate Single-Precision */
4002     case 330:           /* Vector Reciprocal Square Root Estimate Single-Precision */
4003     case 1288:          /* Vector AES Cipher */
4004     case 1289:          /* Vector AES Cipher Last */
4005     case 1352:          /* Vector AES Inverse Cipher */
4006     case 1353:          /* Vector AES Inverse Cipher Last */
4007     case 1480:          /* Vector AES SubBytes */
4008     case 1730:          /* Vector SHA-512 Sigma Doubleword */
4009     case 1666:          /* Vector SHA-256 Sigma Word */
4010     case 1032:          /* Vector Polynomial Multiply-Sum Byte */
4011     case 1160:          /* Vector Polynomial Multiply-Sum Word */
4012     case 1096:          /* Vector Polynomial Multiply-Sum Halfword */
4013     case 1224:          /* Vector Polynomial Multiply-Sum Doubleword */
4014     case 1292:          /* Vector Gather Bits by Bytes by Doubleword */
4015     case 1794:          /* Vector Count Leading Zeros Byte */
4016     case 1858:          /* Vector Count Leading Zeros Halfword */
4017     case 1922:          /* Vector Count Leading Zeros Word */
4018     case 1986:          /* Vector Count Leading Zeros Doubleword */
4019     case 1795:          /* Vector Population Count Byte */
4020     case 1859:          /* Vector Population Count Halfword */
4021     case 1923:          /* Vector Population Count Word */
4022     case 1987:          /* Vector Population Count Doubleword */
4023     case 1356:          /* Vector Bit Permute Quadword */
4024       record_full_arch_list_add_reg (regcache,
4025                                      tdep->ppc_vr0_regnum + PPC_VRT (insn));
4026       return 0;
4027
4028     case 1604:          /* Move To Vector Status and Control Register */
4029       record_full_arch_list_add_reg (regcache, PPC_VSCR_REGNUM);
4030       return 0;
4031     case 1540:          /* Move From Vector Status and Control Register */
4032       record_full_arch_list_add_reg (regcache,
4033                                      tdep->ppc_vr0_regnum + PPC_VRT (insn));
4034       return 0;
4035     }
4036
4037   fprintf_unfiltered (gdb_stdlog, "Warning: Don't know how to record %08x "
4038                       "at %s, 4-%d.\n", insn, paddress (gdbarch, addr), ext);
4039   return -1;
4040 }
4041
4042 /* Parse and record instructions of primary opcode-19 at ADDR.
4043    Return 0 if successful.  */
4044
4045 static int
4046 ppc_process_record_op19 (struct gdbarch *gdbarch, struct regcache *regcache,
4047                            CORE_ADDR addr, uint32_t insn)
4048 {
4049   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
4050   int ext = PPC_EXTOP (insn);
4051
4052   switch (ext)
4053     {
4054     case 0:             /* Move Condition Register Field */
4055     case 33:            /* Condition Register NOR */
4056     case 129:           /* Condition Register AND with Complement */
4057     case 193:           /* Condition Register XOR */
4058     case 225:           /* Condition Register NAND */
4059     case 257:           /* Condition Register AND */
4060     case 289:           /* Condition Register Equivalent */
4061     case 417:           /* Condition Register OR with Complement */
4062     case 449:           /* Condition Register OR */
4063       record_full_arch_list_add_reg (regcache, tdep->ppc_cr_regnum);
4064       return 0;
4065
4066     case 16:            /* Branch Conditional */
4067     case 560:           /* Branch Conditional to Branch Target Address Register */
4068       if ((PPC_BO (insn) & 0x4) == 0)
4069         record_full_arch_list_add_reg (regcache, tdep->ppc_ctr_regnum);
4070       /* FALL-THROUGH */
4071     case 528:           /* Branch Conditional to Count Register */
4072       if (PPC_LK (insn))
4073         record_full_arch_list_add_reg (regcache, tdep->ppc_lr_regnum);
4074       return 0;
4075
4076     case 150:           /* Instruction Synchronize */
4077       /* Do nothing.  */
4078       return 0;
4079     }
4080
4081   fprintf_unfiltered (gdb_stdlog, "Warning: Don't know how to record %08x "
4082                       "at %s, 19-%d.\n", insn, paddress (gdbarch, addr), ext);
4083   return -1;
4084 }
4085
4086 /* Parse and record instructions of primary opcode-31 at ADDR.
4087    Return 0 if successful.  */
4088
4089 static int
4090 ppc_process_record_op31 (struct gdbarch *gdbarch, struct regcache *regcache,
4091                            CORE_ADDR addr, uint32_t insn)
4092 {
4093   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
4094   int ext = PPC_EXTOP (insn);
4095   int tmp, nr, nb, i;
4096   CORE_ADDR at_dcsz, ea = 0;
4097   ULONGEST rb, ra, xer;
4098   int size = 0;
4099
4100   /* These instructions have OE bit.  */
4101   switch (ext & 0x1ff)
4102     {
4103     /* These write RT and XER.  Update CR if RC is set.  */
4104     case 8:             /* Subtract from carrying */
4105     case 10:            /* Add carrying */
4106     case 136:           /* Subtract from extended */
4107     case 138:           /* Add extended */
4108     case 200:           /* Subtract from zero extended */
4109     case 202:           /* Add to zero extended */
4110     case 232:           /* Subtract from minus one extended */
4111     case 234:           /* Add to minus one extended */
4112       /* CA is always altered, but SO/OV are only altered when OE=1.
4113          In any case, XER is always altered.  */
4114       record_full_arch_list_add_reg (regcache, tdep->ppc_xer_regnum);
4115       if (PPC_RC (insn))
4116         record_full_arch_list_add_reg (regcache, tdep->ppc_cr_regnum);
4117       record_full_arch_list_add_reg (regcache,
4118                                      tdep->ppc_gp0_regnum + PPC_RT (insn));
4119       return 0;
4120
4121     /* These write RT.  Update CR if RC is set and update XER if OE is set.  */
4122     case 40:            /* Subtract from */
4123     case 104:           /* Negate */
4124     case 233:           /* Multiply low doubleword */
4125     case 235:           /* Multiply low word */
4126     case 266:           /* Add */
4127     case 393:           /* Divide Doubleword Extended Unsigned */
4128     case 395:           /* Divide Word Extended Unsigned */
4129     case 425:           /* Divide Doubleword Extended */
4130     case 427:           /* Divide Word Extended */
4131     case 457:           /* Divide Doubleword Unsigned */
4132     case 459:           /* Divide Word Unsigned */
4133     case 489:           /* Divide Doubleword */
4134     case 491:           /* Divide Word */
4135       if (PPC_OE (insn))
4136         record_full_arch_list_add_reg (regcache, tdep->ppc_xer_regnum);
4137       /* FALL-THROUGH */
4138     case 9:             /* Multiply High Doubleword Unsigned */
4139     case 11:            /* Multiply High Word Unsigned */
4140     case 73:            /* Multiply High Doubleword */
4141     case 75:            /* Multiply High Word */
4142       if (PPC_RC (insn))
4143         record_full_arch_list_add_reg (regcache, tdep->ppc_cr_regnum);
4144       record_full_arch_list_add_reg (regcache,
4145                                      tdep->ppc_gp0_regnum + PPC_RT (insn));
4146       return 0;
4147     }
4148
4149   if ((ext & 0x1f) == 15)
4150     {
4151       /* Integer Select. bit[16:20] is used for BC.  */
4152       record_full_arch_list_add_reg (regcache,
4153                                      tdep->ppc_gp0_regnum + PPC_RT (insn));
4154       return 0;
4155     }
4156
4157   switch (ext)
4158     {
4159     case 78:            /* Determine Leftmost Zero Byte */
4160       if (PPC_RC (insn))
4161         record_full_arch_list_add_reg (regcache, tdep->ppc_cr_regnum);
4162       record_full_arch_list_add_reg (regcache, tdep->ppc_xer_regnum);
4163       record_full_arch_list_add_reg (regcache,
4164                                      tdep->ppc_gp0_regnum + PPC_RT (insn));
4165       return 0;
4166
4167     /* These only write RT.  */
4168     case 19:            /* Move from condition register */
4169                         /* Move From One Condition Register Field */
4170     case 74:            /* Add and Generate Sixes */
4171     case 74 | 0x200:    /* Add and Generate Sixes (bit-21 dont-care) */
4172     case 302:           /* Move From Branch History Rolling Buffer */
4173     case 339:           /* Move From Special Purpose Register */
4174     case 371:           /* Move From Time Base [Phased-Out]  */
4175       record_full_arch_list_add_reg (regcache,
4176                                      tdep->ppc_gp0_regnum + PPC_RT (insn));
4177       return 0;
4178
4179     /* These only write to RA.  */
4180     case 51:            /* Move From VSR Doubleword */
4181     case 115:           /* Move From VSR Word and Zero */
4182     case 122:           /* Population count bytes */
4183     case 378:           /* Population count words */
4184     case 506:           /* Population count doublewords */
4185     case 154:           /* Parity Word */
4186     case 186:           /* Parity Doubleword */
4187     case 252:           /* Bit Permute Doubleword */
4188     case 282:           /* Convert Declets To Binary Coded Decimal */
4189     case 314:           /* Convert Binary Coded Decimal To Declets */
4190     case 508:           /* Compare bytes */
4191       record_full_arch_list_add_reg (regcache,
4192                                      tdep->ppc_gp0_regnum + PPC_RA (insn));
4193       return 0;
4194
4195     /* These write CR and optional RA.  */
4196     case 792:           /* Shift Right Algebraic Word */
4197     case 794:           /* Shift Right Algebraic Doubleword */
4198     case 824:           /* Shift Right Algebraic Word Immediate */
4199     case 826:           /* Shift Right Algebraic Doubleword Immediate (413) */
4200     case 826 | 1:       /* Shift Right Algebraic Doubleword Immediate (413) */
4201       record_full_arch_list_add_reg (regcache, tdep->ppc_xer_regnum);
4202       record_full_arch_list_add_reg (regcache,
4203                                      tdep->ppc_gp0_regnum + PPC_RA (insn));
4204       /* FALL-THROUGH */
4205     case 0:             /* Compare */
4206     case 32:            /* Compare logical */
4207     case 144:           /* Move To Condition Register Fields */
4208                         /* Move To One Condition Register Field */
4209       record_full_arch_list_add_reg (regcache, tdep->ppc_cr_regnum);
4210       return 0;
4211
4212     /* These write to RT.  Update RA if 'update indexed.'  */
4213     case 53:            /* Load Doubleword with Update Indexed */
4214     case 119:           /* Load Byte and Zero with Update Indexed */
4215     case 311:           /* Load Halfword and Zero with Update Indexed */
4216     case 55:            /* Load Word and Zero with Update Indexed */
4217     case 375:           /* Load Halfword Algebraic with Update Indexed */
4218     case 373:           /* Load Word Algebraic with Update Indexed */
4219       record_full_arch_list_add_reg (regcache,
4220                                      tdep->ppc_gp0_regnum + PPC_RA (insn));
4221       /* FALL-THROUGH */
4222     case 21:            /* Load Doubleword Indexed */
4223     case 52:            /* Load Byte And Reserve Indexed */
4224     case 116:           /* Load Halfword And Reserve Indexed */
4225     case 20:            /* Load Word And Reserve Indexed */
4226     case 84:            /* Load Doubleword And Reserve Indexed */
4227     case 87:            /* Load Byte and Zero Indexed */
4228     case 279:           /* Load Halfword and Zero Indexed */
4229     case 23:            /* Load Word and Zero Indexed */
4230     case 343:           /* Load Halfword Algebraic Indexed */
4231     case 341:           /* Load Word Algebraic Indexed */
4232     case 790:           /* Load Halfword Byte-Reverse Indexed */
4233     case 534:           /* Load Word Byte-Reverse Indexed */
4234     case 532:           /* Load Doubleword Byte-Reverse Indexed */
4235       record_full_arch_list_add_reg (regcache,
4236                                      tdep->ppc_gp0_regnum + PPC_RT (insn));
4237       return 0;
4238
4239     case 597:           /* Load String Word Immediate */
4240     case 533:           /* Load String Word Indexed */
4241       if (ext == 597)
4242         {
4243         nr = PPC_NB (insn);
4244         if (nr == 0)
4245           nr = 32;
4246         }
4247       else
4248         {
4249           regcache_raw_read_unsigned (regcache, tdep->ppc_xer_regnum, &xer);
4250           nr = PPC_XER_NB (xer);
4251         }
4252
4253       nr = (nr + 3) >> 2;
4254
4255       /* If n=0, the contents of register RT are undefined.  */
4256       if (nr == 0)
4257         nr = 1;
4258
4259       for (i = 0; i < nr; i++)
4260         record_full_arch_list_add_reg (regcache,
4261                                        tdep->ppc_gp0_regnum
4262                                        + ((PPC_RT (insn) + i) & 0x1f));
4263       return 0;
4264
4265     case 276:           /* Load Quadword And Reserve Indexed */
4266       tmp = tdep->ppc_gp0_regnum + (PPC_RT (insn) & ~1);
4267       record_full_arch_list_add_reg (regcache, tmp);
4268       record_full_arch_list_add_reg (regcache, tmp + 1);
4269       return 0;
4270
4271     /* These write VRT.  */
4272     case 6:             /* Load Vector for Shift Left Indexed */
4273     case 38:            /* Load Vector for Shift Right Indexed */
4274     case 7:             /* Load Vector Element Byte Indexed */
4275     case 39:            /* Load Vector Element Halfword Indexed */
4276     case 71:            /* Load Vector Element Word Indexed */
4277     case 103:           /* Load Vector Indexed */
4278     case 359:           /* Load Vector Indexed LRU */
4279       record_full_arch_list_add_reg (regcache,
4280                                      tdep->ppc_vr0_regnum + PPC_VRT (insn));
4281       return 0;
4282
4283     /* These write FRT.  Update RA if 'update indexed.'  */
4284     case 567:           /* Load Floating-Point Single with Update Indexed */
4285     case 631:           /* Load Floating-Point Double with Update Indexed */
4286       record_full_arch_list_add_reg (regcache,
4287                                      tdep->ppc_gp0_regnum + PPC_RA (insn));
4288       /* FALL-THROUGH */
4289     case 535:           /* Load Floating-Point Single Indexed */
4290     case 599:           /* Load Floating-Point Double Indexed */
4291     case 855:           /* Load Floating-Point as Integer Word Algebraic Indexed */
4292     case 887:           /* Load Floating-Point as Integer Word and Zero Indexed */
4293       record_full_arch_list_add_reg (regcache,
4294                                      tdep->ppc_fp0_regnum + PPC_FRT (insn));
4295       return 0;
4296
4297     case 791:           /* Load Floating-Point Double Pair Indexed */
4298       tmp = tdep->ppc_fp0_regnum + (PPC_FRT (insn) & ~1);
4299       record_full_arch_list_add_reg (regcache, tmp);
4300       record_full_arch_list_add_reg (regcache, tmp + 1);
4301       return 0;
4302
4303     case 179:           /* Move To VSR Doubleword */
4304     case 211:           /* Move To VSR Word Algebraic */
4305     case 243:           /* Move To VSR Word and Zero */
4306     case 588:           /* Load VSX Scalar Doubleword Indexed */
4307     case 524:           /* Load VSX Scalar Single-Precision Indexed */
4308     case 76:            /* Load VSX Scalar as Integer Word Algebraic Indexed */
4309     case 12:            /* Load VSX Scalar as Integer Word and Zero Indexed */
4310     case 844:           /* Load VSX Vector Doubleword*2 Indexed */
4311     case 332:           /* Load VSX Vector Doubleword & Splat Indexed */
4312     case 780:           /* Load VSX Vector Word*4 Indexed */
4313       ppc_record_vsr (regcache, tdep, PPC_XT (insn));
4314       return 0;
4315
4316     /* These write RA.  Update CR if RC is set.  */
4317     case 24:            /* Shift Left Word */
4318     case 26:            /* Count Leading Zeros Word */
4319     case 27:            /* Shift Left Doubleword */
4320     case 28:            /* AND */
4321     case 58:            /* Count Leading Zeros Doubleword */
4322     case 60:            /* AND with Complement */
4323     case 124:           /* NOR */
4324     case 284:           /* Equivalent */
4325     case 316:           /* XOR */
4326     case 476:           /* NAND */
4327     case 412:           /* OR with Complement */
4328     case 444:           /* OR */
4329     case 536:           /* Shift Right Word */
4330     case 539:           /* Shift Right Doubleword */
4331     case 922:           /* Extend Sign Halfword */
4332     case 954:           /* Extend Sign Byte */
4333     case 986:           /* Extend Sign Word */
4334       if (PPC_RC (insn))
4335         record_full_arch_list_add_reg (regcache, tdep->ppc_cr_regnum);
4336       record_full_arch_list_add_reg (regcache,
4337                                      tdep->ppc_gp0_regnum + PPC_RA (insn));
4338       return 0;
4339
4340     /* Store memory.  */
4341     case 181:           /* Store Doubleword with Update Indexed */
4342     case 183:           /* Store Word with Update Indexed */
4343     case 247:           /* Store Byte with Update Indexed */
4344     case 439:           /* Store Half Word with Update Indexed */
4345     case 695:           /* Store Floating-Point Single with Update Indexed */
4346     case 759:           /* Store Floating-Point Double with Update Indexed */
4347       record_full_arch_list_add_reg (regcache,
4348                                      tdep->ppc_gp0_regnum + PPC_RA (insn));
4349       /* FALL-THROUGH */
4350     case 135:           /* Store Vector Element Byte Indexed */
4351     case 167:           /* Store Vector Element Halfword Indexed */
4352     case 199:           /* Store Vector Element Word Indexed */
4353     case 231:           /* Store Vector Indexed */
4354     case 487:           /* Store Vector Indexed LRU */
4355     case 716:           /* Store VSX Scalar Doubleword Indexed */
4356     case 140:           /* Store VSX Scalar as Integer Word Indexed */
4357     case 652:           /* Store VSX Scalar Single-Precision Indexed */
4358     case 972:           /* Store VSX Vector Doubleword*2 Indexed */
4359     case 908:           /* Store VSX Vector Word*4 Indexed */
4360     case 149:           /* Store Doubleword Indexed */
4361     case 151:           /* Store Word Indexed */
4362     case 215:           /* Store Byte Indexed */
4363     case 407:           /* Store Half Word Indexed */
4364     case 694:           /* Store Byte Conditional Indexed */
4365     case 726:           /* Store Halfword Conditional Indexed */
4366     case 150:           /* Store Word Conditional Indexed */
4367     case 214:           /* Store Doubleword Conditional Indexed */
4368     case 182:           /* Store Quadword Conditional Indexed */
4369     case 662:           /* Store Word Byte-Reverse Indexed */
4370     case 918:           /* Store Halfword Byte-Reverse Indexed */
4371     case 660:           /* Store Doubleword Byte-Reverse Indexed */
4372     case 663:           /* Store Floating-Point Single Indexed */
4373     case 727:           /* Store Floating-Point Double Indexed */
4374     case 919:           /* Store Floating-Point Double Pair Indexed */
4375     case 983:           /* Store Floating-Point as Integer Word Indexed */
4376       if (ext == 694 || ext == 726 || ext == 150 || ext == 214 || ext == 182)
4377         record_full_arch_list_add_reg (regcache, tdep->ppc_cr_regnum);
4378
4379       ra = 0;
4380       if (PPC_RA (insn) != 0)
4381         regcache_raw_read_unsigned (regcache,
4382                                     tdep->ppc_gp0_regnum + PPC_RA (insn), &ra);
4383       regcache_raw_read_unsigned (regcache,
4384                                   tdep->ppc_gp0_regnum + PPC_RB (insn), &rb);
4385       ea = ra + rb;
4386
4387       switch (ext)
4388         {
4389         case 183:       /* Store Word with Update Indexed */
4390         case 199:       /* Store Vector Element Word Indexed */
4391         case 140:       /* Store VSX Scalar as Integer Word Indexed */
4392         case 652:       /* Store VSX Scalar Single-Precision Indexed */
4393         case 151:       /* Store Word Indexed */
4394         case 150:       /* Store Word Conditional Indexed */
4395         case 662:       /* Store Word Byte-Reverse Indexed */
4396         case 663:       /* Store Floating-Point Single Indexed */
4397         case 695:       /* Store Floating-Point Single with Update Indexed */
4398         case 983:       /* Store Floating-Point as Integer Word Indexed */
4399           size = 4;
4400           break;
4401         case 247:       /* Store Byte with Update Indexed */
4402         case 135:       /* Store Vector Element Byte Indexed */
4403         case 215:       /* Store Byte Indexed */
4404         case 694:       /* Store Byte Conditional Indexed */
4405           size = 1;
4406           break;
4407         case 439:       /* Store Halfword with Update Indexed */
4408         case 167:       /* Store Vector Element Halfword Indexed */
4409         case 407:       /* Store Halfword Indexed */
4410         case 726:       /* Store Halfword Conditional Indexed */
4411         case 918:       /* Store Halfword Byte-Reverse Indexed */
4412           size = 2;
4413           break;
4414         case 181:       /* Store Doubleword with Update Indexed */
4415         case 716:       /* Store VSX Scalar Doubleword Indexed */
4416         case 149:       /* Store Doubleword Indexed */
4417         case 214:       /* Store Doubleword Conditional Indexed */
4418         case 660:       /* Store Doubleword Byte-Reverse Indexed */
4419         case 727:       /* Store Floating-Point Double Indexed */
4420         case 759:       /* Store Floating-Point Double with Update Indexed */
4421           size = 8;
4422           break;
4423         case 972:       /* Store VSX Vector Doubleword*2 Indexed */
4424         case 908:       /* Store VSX Vector Word*4 Indexed */
4425         case 182:       /* Store Quadword Conditional Indexed */
4426         case 231:       /* Store Vector Indexed */
4427         case 487:       /* Store Vector Indexed LRU */
4428         case 919:       /* Store Floating-Point Double Pair Indexed */
4429           size = 16;
4430           break;
4431         default:
4432           gdb_assert (0);
4433         }
4434
4435       /* Align address for Store Vector instructions.  */
4436       switch (ext)
4437         {
4438         case 167:       /* Store Vector Element Halfword Indexed */
4439           addr = addr & ~0x1ULL;
4440           break;
4441
4442         case 199:       /* Store Vector Element Word Indexed */
4443           addr = addr & ~0x3ULL;
4444           break;
4445
4446         case 231:       /* Store Vector Indexed */
4447         case 487:       /* Store Vector Indexed LRU */
4448           addr = addr & ~0xfULL;
4449           break;
4450         }
4451
4452       record_full_arch_list_add_mem (addr, size);
4453       return 0;
4454
4455     case 725:           /* Store String Word Immediate */
4456       ra = 0;
4457       if (PPC_RA (insn) != 0)
4458         regcache_raw_read_unsigned (regcache, tdep->ppc_xer_regnum, &ra);
4459       ea += ra;
4460
4461       nb = PPC_NB (insn);
4462       if (nb == 0)
4463         nb = 32;
4464
4465       record_full_arch_list_add_mem (ea, nb);
4466
4467       return 0;
4468
4469     case 661:           /* Store String Word Indexed */
4470       ra = 0;
4471       if (PPC_RA (insn) != 0)
4472         regcache_raw_read_unsigned (regcache, tdep->ppc_xer_regnum, &ra);
4473       ea += ra;
4474
4475       regcache_raw_read_unsigned (regcache, tdep->ppc_xer_regnum, &xer);
4476       nb = PPC_XER_NB (xer);
4477
4478       if (nb != 0)
4479         {
4480           regcache_raw_read_unsigned (regcache, tdep->ppc_xer_regnum, &rb);
4481           ea += rb;
4482           record_full_arch_list_add_mem (ea, nb);
4483         }
4484
4485       return 0;
4486
4487     case 467:           /* Move To Special Purpose Register */
4488       switch (PPC_SPR (insn))
4489         {
4490         case 1:                 /* XER */
4491           record_full_arch_list_add_reg (regcache, tdep->ppc_xer_regnum);
4492           return 0;
4493         case 8:                 /* LR */
4494           record_full_arch_list_add_reg (regcache, tdep->ppc_lr_regnum);
4495           return 0;
4496         case 9:                 /* CTR */
4497           record_full_arch_list_add_reg (regcache, tdep->ppc_ctr_regnum);
4498           return 0;
4499         case 256:               /* VRSAVE */
4500           record_full_arch_list_add_reg (regcache, tdep->ppc_vrsave_regnum);
4501           return 0;
4502         }
4503
4504       goto UNKNOWN_OP;
4505
4506     case 147:           /* Move To Split Little Endian */
4507       record_full_arch_list_add_reg (regcache, tdep->ppc_ps_regnum);
4508       return 0;
4509
4510     case 512:           /* Move to Condition Register from XER */
4511       record_full_arch_list_add_reg (regcache, tdep->ppc_cr_regnum);
4512       record_full_arch_list_add_reg (regcache, tdep->ppc_xer_regnum);
4513       return 0;
4514
4515     case 4:             /* Trap Word */
4516     case 68:            /* Trap Doubleword */
4517     case 430:           /* Clear BHRB */
4518     case 598:           /* Synchronize */
4519     case 62:            /* Wait for Interrupt */
4520     case 22:            /* Instruction Cache Block Touch */
4521     case 854:           /* Enforce In-order Execution of I/O */
4522     case 246:           /* Data Cache Block Touch for Store */
4523     case 54:            /* Data Cache Block Store */
4524     case 86:            /* Data Cache Block Flush */
4525     case 278:           /* Data Cache Block Touch */
4526     case 758:           /* Data Cache Block Allocate */
4527     case 982:           /* Instruction Cache Block Invalidate */
4528       return 0;
4529
4530     case 654:           /* Transaction Begin */
4531     case 686:           /* Transaction End */
4532     case 718:           /* Transaction Check */
4533     case 750:           /* Transaction Suspend or Resume */
4534     case 782:           /* Transaction Abort Word Conditional */
4535     case 814:           /* Transaction Abort Doubleword Conditional */
4536     case 846:           /* Transaction Abort Word Conditional Immediate */
4537     case 878:           /* Transaction Abort Doubleword Conditional Immediate */
4538     case 910:           /* Transaction Abort */
4539       fprintf_unfiltered (gdb_stdlog, "Cannot record Transaction instructions. "
4540                           "%08x at %s, 31-%d.\n",
4541                           insn, paddress (gdbarch, addr), ext);
4542       return -1;
4543
4544     case 1014:          /* Data Cache Block set to Zero */
4545       if (target_auxv_search (&current_target, AT_DCACHEBSIZE, &at_dcsz) <= 0
4546           || at_dcsz == 0)
4547         at_dcsz = 128; /* Assume 128-byte cache line size (POWER8)  */
4548
4549       if (PPC_RA (insn) != 0)
4550         regcache_raw_read_unsigned (regcache,
4551                                     tdep->ppc_gp0_regnum + PPC_RA (insn), &ra);
4552       regcache_raw_read_unsigned (regcache,
4553                                   tdep->ppc_gp0_regnum + PPC_RB (insn), &rb);
4554       ea = (ra + rb) & ~((ULONGEST) (at_dcsz - 1));
4555       record_full_arch_list_add_mem (ea, at_dcsz);
4556       return 0;
4557     }
4558
4559 UNKNOWN_OP:
4560   fprintf_unfiltered (gdb_stdlog, "Warning: Don't know how to record %08x "
4561                       "at %s, 31-%d.\n", insn, paddress (gdbarch, addr), ext);
4562   return -1;
4563 }
4564
4565 /* Parse and record instructions of primary opcode-59 at ADDR.
4566    Return 0 if successful.  */
4567
4568 static int
4569 ppc_process_record_op59 (struct gdbarch *gdbarch, struct regcache *regcache,
4570                            CORE_ADDR addr, uint32_t insn)
4571 {
4572   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
4573   int ext = PPC_EXTOP (insn);
4574
4575   switch (ext & 0x1f)
4576     {
4577     case 18:            /* Floating Divide */
4578     case 20:            /* Floating Subtract */
4579     case 21:            /* Floating Add */
4580     case 22:            /* Floating Square Root */
4581     case 24:            /* Floating Reciprocal Estimate */
4582     case 25:            /* Floating Multiply */
4583     case 26:            /* Floating Reciprocal Square Root Estimate */
4584     case 28:            /* Floating Multiply-Subtract */
4585     case 29:            /* Floating Multiply-Add */
4586     case 30:            /* Floating Negative Multiply-Subtract */
4587     case 31:            /* Floating Negative Multiply-Add */
4588       record_full_arch_list_add_reg (regcache,
4589                                      tdep->ppc_fp0_regnum + PPC_FRT (insn));
4590       if (PPC_RC (insn))
4591         record_full_arch_list_add_reg (regcache, tdep->ppc_cr_regnum);
4592       record_full_arch_list_add_reg (regcache, tdep->ppc_fpscr_regnum);
4593
4594       return 0;
4595     }
4596
4597   switch (ext)
4598     {
4599     case 2:             /* DFP Add */
4600     case 3:             /* DFP Quantize */
4601     case 34:            /* DFP Multiply */
4602     case 35:            /* DFP Reround */
4603     case 67:            /* DFP Quantize Immediate */
4604     case 99:            /* DFP Round To FP Integer With Inexact */
4605     case 227:           /* DFP Round To FP Integer Without Inexact */
4606     case 258:           /* DFP Convert To DFP Long! */
4607     case 290:           /* DFP Convert To Fixed */
4608     case 514:           /* DFP Subtract */
4609     case 546:           /* DFP Divide */
4610     case 770:           /* DFP Round To DFP Short! */
4611     case 802:           /* DFP Convert From Fixed */
4612     case 834:           /* DFP Encode BCD To DPD */
4613       if (PPC_RC (insn))
4614         record_full_arch_list_add_reg (regcache, tdep->ppc_cr_regnum);
4615       record_full_arch_list_add_reg (regcache,
4616                                      tdep->ppc_fp0_regnum + PPC_FRT (insn));
4617       record_full_arch_list_add_reg (regcache, tdep->ppc_fpscr_regnum);
4618       return 0;
4619
4620     case 130:           /* DFP Compare Ordered */
4621     case 162:           /* DFP Test Exponent */
4622     case 194:           /* DFP Test Data Class */
4623     case 226:           /* DFP Test Data Group */
4624     case 642:           /* DFP Compare Unordered */
4625     case 674:           /* DFP Test Significance */
4626       record_full_arch_list_add_reg (regcache, tdep->ppc_cr_regnum);
4627       record_full_arch_list_add_reg (regcache, tdep->ppc_fpscr_regnum);
4628       return 0;
4629
4630     case 66:            /* DFP Shift Significand Left Immediate */
4631     case 98:            /* DFP Shift Significand Right Immediate */
4632     case 322:           /* DFP Decode DPD To BCD */
4633     case 354:           /* DFP Extract Biased Exponent */
4634     case 866:           /* DFP Insert Biased Exponent */
4635       record_full_arch_list_add_reg (regcache,
4636                                      tdep->ppc_fp0_regnum + PPC_FRT (insn));
4637       if (PPC_RC (insn))
4638         record_full_arch_list_add_reg (regcache, tdep->ppc_cr_regnum);
4639       return 0;
4640
4641     case 846:           /* Floating Convert From Integer Doubleword Single */
4642     case 974:           /* Floating Convert From Integer Doubleword Unsigned
4643                            Single */
4644       record_full_arch_list_add_reg (regcache,
4645                                      tdep->ppc_fp0_regnum + PPC_FRT (insn));
4646       if (PPC_RC (insn))
4647         record_full_arch_list_add_reg (regcache, tdep->ppc_cr_regnum);
4648       record_full_arch_list_add_reg (regcache, tdep->ppc_fpscr_regnum);
4649
4650       return 0;
4651     }
4652
4653   fprintf_unfiltered (gdb_stdlog, "Warning: Don't know how to record %08x "
4654                       "at %s, 59-%d.\n", insn, paddress (gdbarch, addr), ext);
4655   return -1;
4656 }
4657
4658 /* Parse and record instructions of primary opcode-60 at ADDR.
4659    Return 0 if successful.  */
4660
4661 static int
4662 ppc_process_record_op60 (struct gdbarch *gdbarch, struct regcache *regcache,
4663                            CORE_ADDR addr, uint32_t insn)
4664 {
4665   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
4666   int ext = PPC_EXTOP (insn);
4667
4668   switch (ext >> 2)
4669     {
4670     case 0:             /* VSX Scalar Add Single-Precision */
4671     case 32:            /* VSX Scalar Add Double-Precision */
4672     case 24:            /* VSX Scalar Divide Single-Precision */
4673     case 56:            /* VSX Scalar Divide Double-Precision */
4674     case 176:           /* VSX Scalar Copy Sign Double-Precision */
4675     case 33:            /* VSX Scalar Multiply-Add Double-Precision */
4676     case 41:            /* ditto */
4677     case 1:             /* VSX Scalar Multiply-Add Single-Precision */
4678     case 9:             /* ditto */
4679     case 160:           /* VSX Scalar Maximum Double-Precision */
4680     case 168:           /* VSX Scalar Minimum Double-Precision */
4681     case 49:            /* VSX Scalar Multiply-Subtract Double-Precision */
4682     case 57:            /* ditto */
4683     case 17:            /* VSX Scalar Multiply-Subtract Single-Precision */
4684     case 25:            /* ditto */
4685     case 48:            /* VSX Scalar Multiply Double-Precision */
4686     case 16:            /* VSX Scalar Multiply Single-Precision */
4687     case 161:           /* VSX Scalar Negative Multiply-Add Double-Precision */
4688     case 169:           /* ditto */
4689     case 129:           /* VSX Scalar Negative Multiply-Add Single-Precision */
4690     case 137:           /* ditto */
4691     case 177:           /* VSX Scalar Negative Multiply-Subtract Double-Precision */
4692     case 185:           /* ditto */
4693     case 145:           /* VSX Scalar Negative Multiply-Subtract Single-Precision */
4694     case 153:           /* ditto */
4695     case 40:            /* VSX Scalar Subtract Double-Precision */
4696     case 8:             /* VSX Scalar Subtract Single-Precision */
4697     case 96:            /* VSX Vector Add Double-Precision */
4698     case 64:            /* VSX Vector Add Single-Precision */
4699     case 120:           /* VSX Vector Divide Double-Precision */
4700     case 88:            /* VSX Vector Divide Single-Precision */
4701     case 97:            /* VSX Vector Multiply-Add Double-Precision */
4702     case 105:           /* ditto */
4703     case 65:            /* VSX Vector Multiply-Add Single-Precision */
4704     case 73:            /* ditto */
4705     case 224:           /* VSX Vector Maximum Double-Precision */
4706     case 192:           /* VSX Vector Maximum Single-Precision */
4707     case 232:           /* VSX Vector Minimum Double-Precision */
4708     case 200:           /* VSX Vector Minimum Single-Precision */
4709     case 113:           /* VSX Vector Multiply-Subtract Double-Precision */
4710     case 121:           /* ditto */
4711     case 81:            /* VSX Vector Multiply-Subtract Single-Precision */
4712     case 89:            /* ditto */
4713     case 112:           /* VSX Vector Multiply Double-Precision */
4714     case 80:            /* VSX Vector Multiply Single-Precision */
4715     case 225:           /* VSX Vector Negative Multiply-Add Double-Precision */
4716     case 233:           /* ditto */
4717     case 193:           /* VSX Vector Negative Multiply-Add Single-Precision */
4718     case 201:           /* ditto */
4719     case 241:           /* VSX Vector Negative Multiply-Subtract Double-Precision */
4720     case 249:           /* ditto */
4721     case 209:           /* VSX Vector Negative Multiply-Subtract Single-Precision */
4722     case 217:           /* ditto */
4723     case 104:           /* VSX Vector Subtract Double-Precision */
4724     case 72:            /* VSX Vector Subtract Single-Precision */
4725       record_full_arch_list_add_reg (regcache, tdep->ppc_fpscr_regnum);
4726     case 240:           /* VSX Vector Copy Sign Double-Precision */
4727     case 208:           /* VSX Vector Copy Sign Single-Precision */
4728     case 130:           /* VSX Logical AND */
4729     case 138:           /* VSX Logical AND with Complement */
4730     case 186:           /* VSX Logical Equivalence */
4731     case 178:           /* VSX Logical NAND */
4732     case 170:           /* VSX Logical OR with Complement */
4733     case 162:           /* VSX Logical NOR */
4734     case 146:           /* VSX Logical OR */
4735     case 154:           /* VSX Logical XOR */
4736     case 18:            /* VSX Merge High Word */
4737     case 50:            /* VSX Merge Low Word */
4738     case 10:            /* VSX Permute Doubleword Immediate (DM=0) */
4739     case 10 | 0x20:     /* VSX Permute Doubleword Immediate (DM=1) */
4740     case 10 | 0x40:     /* VSX Permute Doubleword Immediate (DM=2) */
4741     case 10 | 0x60:     /* VSX Permute Doubleword Immediate (DM=3) */
4742     case 2:             /* VSX Shift Left Double by Word Immediate (SHW=0) */
4743     case 2 | 0x20:      /* VSX Shift Left Double by Word Immediate (SHW=1) */
4744     case 2 | 0x40:      /* VSX Shift Left Double by Word Immediate (SHW=2) */
4745     case 2 | 0x60:      /* VSX Shift Left Double by Word Immediate (SHW=3) */
4746       ppc_record_vsr (regcache, tdep, PPC_XT (insn));
4747       return 0;
4748
4749     case 61:            /* VSX Scalar Test for software Divide Double-Precision */
4750     case 125:           /* VSX Vector Test for software Divide Double-Precision */
4751     case 93:            /* VSX Vector Test for software Divide Single-Precision */
4752       record_full_arch_list_add_reg (regcache, tdep->ppc_cr_regnum);
4753       return 0;
4754
4755     case 35:            /* VSX Scalar Compare Unordered Double-Precision */
4756     case 43:            /* VSX Scalar Compare Ordered Double-Precision */
4757       record_full_arch_list_add_reg (regcache, tdep->ppc_cr_regnum);
4758       record_full_arch_list_add_reg (regcache, tdep->ppc_fpscr_regnum);
4759       return 0;
4760     }
4761
4762   switch ((ext >> 2) & 0x7f) /* Mask out Rc-bit.  */
4763     {
4764     case 99:            /* VSX Vector Compare Equal To Double-Precision */
4765     case 67:            /* VSX Vector Compare Equal To Single-Precision */
4766     case 115:           /* VSX Vector Compare Greater Than or
4767                            Equal To Double-Precision */
4768     case 83:            /* VSX Vector Compare Greater Than or
4769                            Equal To Single-Precision */
4770     case 107:           /* VSX Vector Compare Greater Than Double-Precision */
4771     case 75:            /* VSX Vector Compare Greater Than Single-Precision */
4772       if (PPC_Rc (insn))
4773         record_full_arch_list_add_reg (regcache, tdep->ppc_cr_regnum);
4774       record_full_arch_list_add_reg (regcache, tdep->ppc_fpscr_regnum);
4775       ppc_record_vsr (regcache, tdep, PPC_XT (insn));
4776       return 0;
4777     }
4778
4779   switch (ext >> 1)
4780     {
4781     case 265:           /* VSX Scalar round Double-Precision to
4782                            Single-Precision and Convert to
4783                            Single-Precision format */
4784     case 344:           /* VSX Scalar truncate Double-Precision to
4785                            Integer and Convert to Signed Integer
4786                            Doubleword format with Saturate */
4787     case 88:            /* VSX Scalar truncate Double-Precision to
4788                            Integer and Convert to Signed Integer Word
4789                            Format with Saturate */
4790     case 328:           /* VSX Scalar truncate Double-Precision integer
4791                            and Convert to Unsigned Integer Doubleword
4792                            Format with Saturate */
4793     case 72:            /* VSX Scalar truncate Double-Precision to
4794                            Integer and Convert to Unsigned Integer Word
4795                            Format with Saturate */
4796     case 329:           /* VSX Scalar Convert Single-Precision to
4797                            Double-Precision format */
4798     case 376:           /* VSX Scalar Convert Signed Integer
4799                            Doubleword to floating-point format and
4800                            Round to Double-Precision format */
4801     case 312:           /* VSX Scalar Convert Signed Integer
4802                            Doubleword to floating-point format and
4803                            round to Single-Precision */
4804     case 360:           /* VSX Scalar Convert Unsigned Integer
4805                            Doubleword to floating-point format and
4806                            Round to Double-Precision format */
4807     case 296:           /* VSX Scalar Convert Unsigned Integer
4808                            Doubleword to floating-point format and
4809                            Round to Single-Precision */
4810     case 73:            /* VSX Scalar Round to Double-Precision Integer
4811                            Using Round to Nearest Away */
4812     case 107:           /* VSX Scalar Round to Double-Precision Integer
4813                            Exact using Current rounding mode */
4814     case 121:           /* VSX Scalar Round to Double-Precision Integer
4815                            Using Round toward -Infinity */
4816     case 105:           /* VSX Scalar Round to Double-Precision Integer
4817                            Using Round toward +Infinity */
4818     case 89:            /* VSX Scalar Round to Double-Precision Integer
4819                            Using Round toward Zero */
4820     case 90:            /* VSX Scalar Reciprocal Estimate Double-Precision */
4821     case 26:            /* VSX Scalar Reciprocal Estimate Single-Precision */
4822     case 281:           /* VSX Scalar Round to Single-Precision */
4823     case 74:            /* VSX Scalar Reciprocal Square Root Estimate
4824                            Double-Precision */
4825     case 10:            /* VSX Scalar Reciprocal Square Root Estimate
4826                            Single-Precision */
4827     case 75:            /* VSX Scalar Square Root Double-Precision */
4828     case 11:            /* VSX Scalar Square Root Single-Precision */
4829     case 393:           /* VSX Vector round Double-Precision to
4830                            Single-Precision and Convert to
4831                            Single-Precision format */
4832     case 472:           /* VSX Vector truncate Double-Precision to
4833                            Integer and Convert to Signed Integer
4834                            Doubleword format with Saturate */
4835     case 216:           /* VSX Vector truncate Double-Precision to
4836                            Integer and Convert to Signed Integer Word
4837                            Format with Saturate */
4838     case 456:           /* VSX Vector truncate Double-Precision to
4839                            Integer and Convert to Unsigned Integer
4840                            Doubleword format with Saturate */
4841     case 200:           /* VSX Vector truncate Double-Precision to
4842                            Integer and Convert to Unsigned Integer Word
4843                            Format with Saturate */
4844     case 457:           /* VSX Vector Convert Single-Precision to
4845                            Double-Precision format */
4846     case 408:           /* VSX Vector truncate Single-Precision to
4847                            Integer and Convert to Signed Integer
4848                            Doubleword format with Saturate */
4849     case 152:           /* VSX Vector truncate Single-Precision to
4850                            Integer and Convert to Signed Integer Word
4851                            Format with Saturate */
4852     case 392:           /* VSX Vector truncate Single-Precision to
4853                            Integer and Convert to Unsigned Integer
4854                            Doubleword format with Saturate */
4855     case 136:           /* VSX Vector truncate Single-Precision to
4856                            Integer and Convert to Unsigned Integer Word
4857                            Format with Saturate */
4858     case 504:           /* VSX Vector Convert and round Signed Integer
4859                            Doubleword to Double-Precision format */
4860     case 440:           /* VSX Vector Convert and round Signed Integer
4861                            Doubleword to Single-Precision format */
4862     case 248:           /* VSX Vector Convert Signed Integer Word to
4863                            Double-Precision format */
4864     case 184:           /* VSX Vector Convert and round Signed Integer
4865                            Word to Single-Precision format */
4866     case 488:           /* VSX Vector Convert and round Unsigned
4867                            Integer Doubleword to Double-Precision format */
4868     case 424:           /* VSX Vector Convert and round Unsigned
4869                            Integer Doubleword to Single-Precision format */
4870     case 232:           /* VSX Vector Convert and round Unsigned
4871                            Integer Word to Double-Precision format */
4872     case 168:           /* VSX Vector Convert and round Unsigned
4873                            Integer Word to Single-Precision format */
4874     case 201:           /* VSX Vector Round to Double-Precision
4875                            Integer using round to Nearest Away */
4876     case 235:           /* VSX Vector Round to Double-Precision
4877                            Integer Exact using Current rounding mode */
4878     case 249:           /* VSX Vector Round to Double-Precision
4879                            Integer using round toward -Infinity */
4880     case 233:           /* VSX Vector Round to Double-Precision
4881                            Integer using round toward +Infinity */
4882     case 217:           /* VSX Vector Round to Double-Precision
4883                            Integer using round toward Zero */
4884     case 218:           /* VSX Vector Reciprocal Estimate Double-Precision */
4885     case 154:           /* VSX Vector Reciprocal Estimate Single-Precision */
4886     case 137:           /* VSX Vector Round to Single-Precision Integer
4887                            Using Round to Nearest Away */
4888     case 171:           /* VSX Vector Round to Single-Precision Integer
4889                            Exact Using Current rounding mode */
4890     case 185:           /* VSX Vector Round to Single-Precision Integer
4891                            Using Round toward -Infinity */
4892     case 169:           /* VSX Vector Round to Single-Precision Integer
4893                            Using Round toward +Infinity */
4894     case 153:           /* VSX Vector Round to Single-Precision Integer
4895                            Using round toward Zero */
4896     case 202:           /* VSX Vector Reciprocal Square Root Estimate
4897                            Double-Precision */
4898     case 138:           /* VSX Vector Reciprocal Square Root Estimate
4899                            Single-Precision */
4900     case 203:           /* VSX Vector Square Root Double-Precision */
4901     case 139:           /* VSX Vector Square Root Single-Precision */
4902       record_full_arch_list_add_reg (regcache, tdep->ppc_fpscr_regnum);
4903     case 345:           /* VSX Scalar Absolute Value Double-Precision */
4904     case 267:           /* VSX Scalar Convert Scalar Single-Precision to
4905                            Vector Single-Precision format Non-signalling */
4906     case 331:           /* VSX Scalar Convert Single-Precision to
4907                            Double-Precision format Non-signalling */
4908     case 361:           /* VSX Scalar Negative Absolute Value Double-Precision */
4909     case 377:           /* VSX Scalar Negate Double-Precision */
4910     case 473:           /* VSX Vector Absolute Value Double-Precision */
4911     case 409:           /* VSX Vector Absolute Value Single-Precision */
4912     case 489:           /* VSX Vector Negative Absolute Value Double-Precision */
4913     case 425:           /* VSX Vector Negative Absolute Value Single-Precision */
4914     case 505:           /* VSX Vector Negate Double-Precision */
4915     case 441:           /* VSX Vector Negate Single-Precision */
4916     case 164:           /* VSX Splat Word */
4917       ppc_record_vsr (regcache, tdep, PPC_XT (insn));
4918       return 0;
4919
4920     case 106:           /* VSX Scalar Test for software Square Root
4921                            Double-Precision */
4922     case 234:           /* VSX Vector Test for software Square Root
4923                            Double-Precision */
4924     case 170:           /* VSX Vector Test for software Square Root
4925                            Single-Precision */
4926       record_full_arch_list_add_reg (regcache, tdep->ppc_cr_regnum);
4927       return 0;
4928     }
4929
4930   if (((ext >> 3) & 0x3) == 3)  /* VSX Select */
4931     {
4932       ppc_record_vsr (regcache, tdep, PPC_XT (insn));
4933       return 0;
4934     }
4935
4936   fprintf_unfiltered (gdb_stdlog, "Warning: Don't know how to record %08x "
4937                       "at %s, 60-%d.\n", insn, paddress (gdbarch, addr), ext);
4938   return -1;
4939 }
4940
4941 /* Parse and record instructions of primary opcode-63 at ADDR.
4942    Return 0 if successful.  */
4943
4944 static int
4945 ppc_process_record_op63 (struct gdbarch *gdbarch, struct regcache *regcache,
4946                            CORE_ADDR addr, uint32_t insn)
4947 {
4948   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
4949   int ext = PPC_EXTOP (insn);
4950   int tmp;
4951
4952   switch (ext & 0x1f)
4953     {
4954     case 18:            /* Floating Divide */
4955     case 20:            /* Floating Subtract */
4956     case 21:            /* Floating Add */
4957     case 22:            /* Floating Square Root */
4958     case 24:            /* Floating Reciprocal Estimate */
4959     case 25:            /* Floating Multiply */
4960     case 26:            /* Floating Reciprocal Square Root Estimate */
4961     case 28:            /* Floating Multiply-Subtract */
4962     case 29:            /* Floating Multiply-Add */
4963     case 30:            /* Floating Negative Multiply-Subtract */
4964     case 31:            /* Floating Negative Multiply-Add */
4965       record_full_arch_list_add_reg (regcache,
4966                                      tdep->ppc_fp0_regnum + PPC_FRT (insn));
4967       if (PPC_RC (insn))
4968         record_full_arch_list_add_reg (regcache, tdep->ppc_cr_regnum);
4969       record_full_arch_list_add_reg (regcache, tdep->ppc_fpscr_regnum);
4970       return 0;
4971
4972     case 23:            /* Floating Select */
4973       record_full_arch_list_add_reg (regcache,
4974                                      tdep->ppc_fp0_regnum + PPC_FRT (insn));
4975       if (PPC_RC (insn))
4976         record_full_arch_list_add_reg (regcache, tdep->ppc_cr_regnum);
4977     }
4978
4979   switch (ext)
4980     {
4981     case 2:             /* DFP Add Quad */
4982     case 3:             /* DFP Quantize Quad */
4983     case 34:            /* DFP Multiply Quad */
4984     case 35:            /* DFP Reround Quad */
4985     case 67:            /* DFP Quantize Immediate Quad */
4986     case 99:            /* DFP Round To FP Integer With Inexact Quad */
4987     case 227:           /* DFP Round To FP Integer Without Inexact Quad */
4988     case 258:           /* DFP Convert To DFP Extended Quad */
4989     case 514:           /* DFP Subtract Quad */
4990     case 546:           /* DFP Divide Quad */
4991     case 770:           /* DFP Round To DFP Long Quad */
4992     case 802:           /* DFP Convert From Fixed Quad */
4993     case 834:           /* DFP Encode BCD To DPD Quad */
4994       if (PPC_RC (insn))
4995         record_full_arch_list_add_reg (regcache, tdep->ppc_cr_regnum);
4996       tmp = tdep->ppc_fp0_regnum + (PPC_FRT (insn) & ~1);
4997       record_full_arch_list_add_reg (regcache, tmp);
4998       record_full_arch_list_add_reg (regcache, tmp + 1);
4999       record_full_arch_list_add_reg (regcache, tdep->ppc_fpscr_regnum);
5000       return 0;
5001
5002     case 130:           /* DFP Compare Ordered Quad */
5003     case 162:           /* DFP Test Exponent Quad */
5004     case 194:           /* DFP Test Data Class Quad */
5005     case 226:           /* DFP Test Data Group Quad */
5006     case 642:           /* DFP Compare Unordered Quad */
5007     case 674:           /* DFP Test Significance Quad */
5008       record_full_arch_list_add_reg (regcache, tdep->ppc_cr_regnum);
5009       record_full_arch_list_add_reg (regcache, tdep->ppc_fpscr_regnum);
5010       return 0;
5011
5012     case 66:            /* DFP Shift Significand Left Immediate Quad */
5013     case 98:            /* DFP Shift Significand Right Immediate Quad */
5014     case 322:           /* DFP Decode DPD To BCD Quad */
5015     case 866:           /* DFP Insert Biased Exponent Quad */
5016       tmp = tdep->ppc_fp0_regnum + (PPC_FRT (insn) & ~1);
5017       record_full_arch_list_add_reg (regcache, tmp);
5018       record_full_arch_list_add_reg (regcache, tmp + 1);
5019       if (PPC_RC (insn))
5020         record_full_arch_list_add_reg (regcache, tdep->ppc_cr_regnum);
5021       return 0;
5022
5023     case 290:           /* DFP Convert To Fixed Quad */
5024       record_full_arch_list_add_reg (regcache,
5025                                      tdep->ppc_fp0_regnum + PPC_FRT (insn));
5026       if (PPC_RC (insn))
5027         record_full_arch_list_add_reg (regcache, tdep->ppc_cr_regnum);
5028       record_full_arch_list_add_reg (regcache, tdep->ppc_fpscr_regnum);
5029       break;
5030
5031     case 354:           /* DFP Extract Biased Exponent Quad */
5032       record_full_arch_list_add_reg (regcache,
5033                                      tdep->ppc_fp0_regnum + PPC_FRT (insn));
5034       if (PPC_RC (insn))
5035         record_full_arch_list_add_reg (regcache, tdep->ppc_cr_regnum);
5036       return 0;
5037
5038     case 12:            /* Floating Round to Single-Precision */
5039     case 14:            /* Floating Convert To Integer Word */
5040     case 15:            /* Floating Convert To Integer Word
5041                            with round toward Zero */
5042     case 142:           /* Floating Convert To Integer Word Unsigned */
5043     case 143:           /* Floating Convert To Integer Word Unsigned
5044                            with round toward Zero */
5045     case 392:           /* Floating Round to Integer Nearest */
5046     case 424:           /* Floating Round to Integer Toward Zero */
5047     case 456:           /* Floating Round to Integer Plus */
5048     case 488:           /* Floating Round to Integer Minus */
5049     case 814:           /* Floating Convert To Integer Doubleword */
5050     case 815:           /* Floating Convert To Integer Doubleword
5051                            with round toward Zero */
5052     case 846:           /* Floating Convert From Integer Doubleword */
5053     case 942:           /* Floating Convert To Integer Doubleword Unsigned */
5054     case 943:           /* Floating Convert To Integer Doubleword Unsigned
5055                            with round toward Zero */
5056     case 974:           /* Floating Convert From Integer Doubleword Unsigned */
5057       record_full_arch_list_add_reg (regcache,
5058                                      tdep->ppc_fp0_regnum + PPC_FRT (insn));
5059       if (PPC_RC (insn))
5060         record_full_arch_list_add_reg (regcache, tdep->ppc_cr_regnum);
5061       record_full_arch_list_add_reg (regcache, tdep->ppc_fpscr_regnum);
5062       return 0;
5063
5064     case 583:           /* Move From FPSCR */
5065     case 8:             /* Floating Copy Sign */
5066     case 40:            /* Floating Negate */
5067     case 72:            /* Floating Move Register */
5068     case 136:           /* Floating Negative Absolute Value */
5069     case 264:           /* Floating Absolute Value */
5070       record_full_arch_list_add_reg (regcache,
5071                                      tdep->ppc_fp0_regnum + PPC_FRT (insn));
5072       if (PPC_RC (insn))
5073         record_full_arch_list_add_reg (regcache, tdep->ppc_cr_regnum);
5074       return 0;
5075
5076     case 838:           /* Floating Merge Odd Word */
5077     case 966:           /* Floating Merge Even Word */
5078       record_full_arch_list_add_reg (regcache,
5079                                      tdep->ppc_fp0_regnum + PPC_FRT (insn));
5080       return 0;
5081
5082     case 38:            /* Move To FPSCR Bit 1 */
5083     case 70:            /* Move To FPSCR Bit 0 */
5084     case 134:           /* Move To FPSCR Field Immediate */
5085     case 711:           /* Move To FPSCR Fields */
5086       if (PPC_RC (insn))
5087         record_full_arch_list_add_reg (regcache, tdep->ppc_cr_regnum);
5088       record_full_arch_list_add_reg (regcache, tdep->ppc_fpscr_regnum);
5089       break;
5090
5091     case 0:             /* Floating Compare Unordered */
5092     case 32:            /* Floating Compare Ordered */
5093     case 64:            /* Move to Condition Register from FPSCR */
5094       record_full_arch_list_add_reg (regcache, tdep->ppc_fpscr_regnum);
5095       /* FALL-THROUGH */
5096     case 128:           /* Floating Test for software Divide */
5097     case 160:           /* Floating Test for software Square Root */
5098       record_full_arch_list_add_reg (regcache, tdep->ppc_cr_regnum);
5099       return 0;
5100
5101     }
5102
5103   fprintf_unfiltered (gdb_stdlog, "Warning: Don't know how to record %08x "
5104                       "at %s, 59-%d.\n", insn, paddress (gdbarch, addr), ext);
5105   return -1;
5106 }
5107
5108 /* Parse the current instruction and record the values of the registers and
5109    memory that will be changed in current instruction to "record_arch_list".
5110    Return -1 if something wrong.  */
5111
5112 int
5113 ppc_process_record (struct gdbarch *gdbarch, struct regcache *regcache,
5114                       CORE_ADDR addr)
5115 {
5116   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
5117   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
5118   uint32_t insn;
5119   int op6, tmp, i;
5120
5121   insn = read_memory_unsigned_integer (addr, 4, byte_order);
5122   op6 = PPC_OP6 (insn);
5123
5124   switch (op6)
5125     {
5126     case 2:             /* Trap Doubleword Immediate */
5127     case 3:             /* Trap Word Immediate */
5128       /* Do nothing.  */
5129       break;
5130
5131     case 4:
5132       if (ppc_process_record_op4 (gdbarch, regcache, addr, insn) != 0)
5133         return -1;
5134       break;
5135
5136     case 17:            /* System call */
5137       if (PPC_LEV (insn) != 0)
5138         goto UNKNOWN_OP;
5139
5140       if (tdep->ppc_syscall_record != NULL)
5141         {
5142           if (tdep->ppc_syscall_record (regcache) != 0)
5143             return -1;
5144         }
5145       else
5146         {
5147           printf_unfiltered (_("no syscall record support\n"));
5148           return -1;
5149         }
5150       break;
5151
5152     case 7:             /* Multiply Low Immediate */
5153       record_full_arch_list_add_reg (regcache,
5154                                      tdep->ppc_gp0_regnum + PPC_RT (insn));
5155       break;
5156
5157     case 8:             /* Subtract From Immediate Carrying */
5158       record_full_arch_list_add_reg (regcache, tdep->ppc_xer_regnum);
5159       record_full_arch_list_add_reg (regcache,
5160                                      tdep->ppc_gp0_regnum + PPC_RT (insn));
5161       break;
5162
5163     case 10:            /* Compare Logical Immediate  */
5164     case 11:            /* Compare Immediate */
5165       record_full_arch_list_add_reg (regcache, tdep->ppc_cr_regnum);
5166       break;
5167
5168     case 13:            /* Add Immediate Carrying and Record */
5169       record_full_arch_list_add_reg (regcache, tdep->ppc_cr_regnum);
5170       /* FALL-THROUGH */
5171     case 12:            /* Add Immediate Carrying */
5172       record_full_arch_list_add_reg (regcache, tdep->ppc_xer_regnum);
5173       /* FALL-THROUGH */
5174     case 14:            /* Add Immediate */
5175     case 15:            /* Add Immediate Shifted */
5176       record_full_arch_list_add_reg (regcache,
5177                                      tdep->ppc_gp0_regnum + PPC_RT (insn));
5178       break;
5179
5180     case 16:            /* Branch Conditional */
5181       if ((PPC_BO (insn) & 0x4) == 0)
5182         record_full_arch_list_add_reg (regcache, tdep->ppc_ctr_regnum);
5183       /* FALL-THROUGH */
5184     case 18:            /* Branch */
5185       if (PPC_LK (insn))
5186         record_full_arch_list_add_reg (regcache, tdep->ppc_lr_regnum);
5187       break;
5188
5189     case 19:
5190       if (ppc_process_record_op19 (gdbarch, regcache, addr, insn) != 0)
5191         return -1;
5192       break;
5193
5194     case 20:            /* Rotate Left Word Immediate then Mask Insert */
5195     case 21:            /* Rotate Left Word Immediate then AND with Mask */
5196     case 23:            /* Rotate Left Word then AND with Mask */
5197     case 30:            /* Rotate Left Doubleword Immediate then Clear Left */
5198                         /* Rotate Left Doubleword Immediate then Clear Right */
5199                         /* Rotate Left Doubleword Immediate then Clear */
5200                         /* Rotate Left Doubleword then Clear Left */
5201                         /* Rotate Left Doubleword then Clear Right */
5202                         /* Rotate Left Doubleword Immediate then Mask Insert */
5203       if (PPC_RC (insn))
5204         record_full_arch_list_add_reg (regcache, tdep->ppc_cr_regnum);
5205       record_full_arch_list_add_reg (regcache,
5206                                      tdep->ppc_gp0_regnum + PPC_RA (insn));
5207       break;
5208
5209     case 28:            /* AND Immediate */
5210     case 29:            /* AND Immediate Shifted */
5211       record_full_arch_list_add_reg (regcache, tdep->ppc_cr_regnum);
5212       /* FALL-THROUGH */
5213     case 24:            /* OR Immediate */
5214     case 25:            /* OR Immediate Shifted */
5215     case 26:            /* XOR Immediate */
5216     case 27:            /* XOR Immediate Shifted */
5217       record_full_arch_list_add_reg (regcache,
5218                                      tdep->ppc_gp0_regnum + PPC_RA (insn));
5219       break;
5220
5221     case 31:
5222       if (ppc_process_record_op31 (gdbarch, regcache, addr, insn) != 0)
5223         return -1;
5224       break;
5225
5226     case 33:            /* Load Word and Zero with Update */
5227     case 35:            /* Load Byte and Zero with Update */
5228     case 41:            /* Load Halfword and Zero with Update */
5229     case 43:            /* Load Halfword Algebraic with Update */
5230       record_full_arch_list_add_reg (regcache,
5231                                      tdep->ppc_gp0_regnum + PPC_RA (insn));
5232       /* FALL-THROUGH */
5233     case 32:            /* Load Word and Zero */
5234     case 34:            /* Load Byte and Zero */
5235     case 40:            /* Load Halfword and Zero */
5236     case 42:            /* Load Halfword Algebraic */
5237       record_full_arch_list_add_reg (regcache,
5238                                      tdep->ppc_gp0_regnum + PPC_RT (insn));
5239       break;
5240
5241     case 46:            /* Load Multiple Word */
5242       for (i = PPC_RT (insn); i < 32; i++)
5243         record_full_arch_list_add_reg (regcache, tdep->ppc_gp0_regnum + i);
5244       break;
5245
5246     case 56:            /* Load Quadword */
5247       tmp = tdep->ppc_gp0_regnum + (PPC_RT (insn) & ~1);
5248       record_full_arch_list_add_reg (regcache, tmp);
5249       record_full_arch_list_add_reg (regcache, tmp + 1);
5250       break;
5251
5252     case 49:            /* Load Floating-Point Single with Update */
5253     case 51:            /* Load Floating-Point Double with Update */
5254       record_full_arch_list_add_reg (regcache,
5255                                      tdep->ppc_gp0_regnum + PPC_RA (insn));
5256       /* FALL-THROUGH */
5257     case 48:            /* Load Floating-Point Single */
5258     case 50:            /* Load Floating-Point Double */
5259       record_full_arch_list_add_reg (regcache,
5260                                      tdep->ppc_fp0_regnum + PPC_FRT (insn));
5261       break;
5262
5263     case 47:            /* Store Multiple Word */
5264         {
5265           ULONGEST addr = 0;
5266
5267           if (PPC_RA (insn) != 0)
5268             regcache_raw_read_unsigned (regcache,
5269                                         tdep->ppc_gp0_regnum + PPC_RA (insn),
5270                                         &addr);
5271
5272           addr += PPC_D (insn);
5273           record_full_arch_list_add_mem (addr, 4 * (32 - PPC_RS (insn)));
5274         }
5275       break;
5276
5277     case 37:            /* Store Word with Update */
5278     case 39:            /* Store Byte with Update */
5279     case 45:            /* Store Halfword with Update */
5280     case 53:            /* Store Floating-Point Single with Update */
5281     case 55:            /* Store Floating-Point Double with Update */
5282       record_full_arch_list_add_reg (regcache,
5283                                      tdep->ppc_gp0_regnum + PPC_RA (insn));
5284       /* FALL-THROUGH */
5285     case 36:            /* Store Word */
5286     case 38:            /* Store Byte */
5287     case 44:            /* Store Halfword */
5288     case 52:            /* Store Floating-Point Single */
5289     case 54:            /* Store Floating-Point Double */
5290         {
5291           ULONGEST addr = 0;
5292           int size = -1;
5293
5294           if (PPC_RA (insn) != 0)
5295             regcache_raw_read_unsigned (regcache,
5296                                         tdep->ppc_gp0_regnum + PPC_RA (insn),
5297                                         &addr);
5298           addr += PPC_D (insn);
5299
5300           if (op6 == 36 || op6 == 37 || op6 == 52 || op6 == 53)
5301             size = 4;
5302           else if (op6 == 54 || op6 == 55)
5303             size = 8;
5304           else if (op6 == 44 || op6 == 45)
5305             size = 2;
5306           else if (op6 == 38 || op6 == 39)
5307             size = 1;
5308           else
5309             gdb_assert (0);
5310
5311           record_full_arch_list_add_mem (addr, size);
5312         }
5313       break;
5314
5315     case 57:            /* Load Floating-Point Double Pair */
5316       if (PPC_FIELD (insn, 30, 2) != 0)
5317         goto UNKNOWN_OP;
5318       tmp = tdep->ppc_fp0_regnum + (PPC_RT (insn) & ~1);
5319       record_full_arch_list_add_reg (regcache, tmp);
5320       record_full_arch_list_add_reg (regcache, tmp + 1);
5321       break;
5322
5323     case 58:            /* Load Doubleword */
5324                         /* Load Doubleword with Update */
5325                         /* Load Word Algebraic */
5326       if (PPC_FIELD (insn, 30, 2) > 2)
5327         goto UNKNOWN_OP;
5328
5329       record_full_arch_list_add_reg (regcache,
5330                                      tdep->ppc_gp0_regnum + PPC_RT (insn));
5331       if (PPC_BIT (insn, 31))
5332         record_full_arch_list_add_reg (regcache,
5333                                        tdep->ppc_gp0_regnum + PPC_RA (insn));
5334       break;
5335
5336     case 59:
5337       if (ppc_process_record_op59 (gdbarch, regcache, addr, insn) != 0)
5338         return -1;
5339       break;
5340
5341     case 60:
5342       if (ppc_process_record_op60 (gdbarch, regcache, addr, insn) != 0)
5343         return -1;
5344       break;
5345
5346     case 61:            /* Store Floating-Point Double Pair */
5347     case 62:            /* Store Doubleword */
5348                         /* Store Doubleword with Update */
5349                         /* Store Quadword with Update */
5350         {
5351           ULONGEST addr = 0;
5352           int size;
5353           int sub2 = PPC_FIELD (insn, 30, 2);
5354
5355           if ((op6 == 61 && sub2 != 0) || (op6 == 62 && sub2 > 2))
5356             goto UNKNOWN_OP;
5357
5358           if (PPC_RA (insn) != 0)
5359             regcache_raw_read_unsigned (regcache,
5360                                         tdep->ppc_gp0_regnum + PPC_RA (insn),
5361                                         &addr);
5362
5363           size = ((op6 == 61) || sub2 == 2) ? 16 : 8;
5364
5365           addr += PPC_DS (insn) << 2;
5366           record_full_arch_list_add_mem (addr, size);
5367
5368           if (op6 == 62 && sub2 == 1)
5369             record_full_arch_list_add_reg (regcache,
5370                                            tdep->ppc_gp0_regnum +
5371                                            PPC_RA (insn));
5372
5373           break;
5374         }
5375
5376     case 63:
5377       if (ppc_process_record_op63 (gdbarch, regcache, addr, insn) != 0)
5378         return -1;
5379       break;
5380
5381     default:
5382 UNKNOWN_OP:
5383       fprintf_unfiltered (gdb_stdlog, "Warning: Don't know how to record %08x "
5384                           "at %s, %d.\n", insn, paddress (gdbarch, addr), op6);
5385       return -1;
5386     }
5387
5388   if (record_full_arch_list_add_reg (regcache, PPC_PC_REGNUM))
5389     return -1;
5390   if (record_full_arch_list_add_end ())
5391     return -1;
5392   return 0;
5393 }
5394
5395 /* Initialize the current architecture based on INFO.  If possible, re-use an
5396    architecture from ARCHES, which is a list of architectures already created
5397    during this debugging session.
5398
5399    Called e.g. at program startup, when reading a core file, and when reading
5400    a binary file.  */
5401
5402 static struct gdbarch *
5403 rs6000_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
5404 {
5405   struct gdbarch *gdbarch;
5406   struct gdbarch_tdep *tdep;
5407   int wordsize, from_xcoff_exec, from_elf_exec;
5408   enum bfd_architecture arch;
5409   unsigned long mach;
5410   bfd abfd;
5411   enum auto_boolean soft_float_flag = powerpc_soft_float_global;
5412   int soft_float;
5413   enum powerpc_vector_abi vector_abi = powerpc_vector_abi_global;
5414   enum powerpc_elf_abi elf_abi = POWERPC_ELF_AUTO;
5415   int have_fpu = 1, have_spe = 0, have_mq = 0, have_altivec = 0, have_dfp = 0,
5416       have_vsx = 0;
5417   int tdesc_wordsize = -1;
5418   const struct target_desc *tdesc = info.target_desc;
5419   struct tdesc_arch_data *tdesc_data = NULL;
5420   int num_pseudoregs = 0;
5421   int cur_reg;
5422
5423   /* INFO may refer to a binary that is not of the PowerPC architecture,
5424      e.g. when debugging a stand-alone SPE executable on a Cell/B.E. system.
5425      In this case, we must not attempt to infer properties of the (PowerPC
5426      side) of the target system from properties of that executable.  Trust
5427      the target description instead.  */
5428   if (info.abfd
5429       && bfd_get_arch (info.abfd) != bfd_arch_powerpc
5430       && bfd_get_arch (info.abfd) != bfd_arch_rs6000)
5431     info.abfd = NULL;
5432
5433   from_xcoff_exec = info.abfd && info.abfd->format == bfd_object &&
5434     bfd_get_flavour (info.abfd) == bfd_target_xcoff_flavour;
5435
5436   from_elf_exec = info.abfd && info.abfd->format == bfd_object &&
5437     bfd_get_flavour (info.abfd) == bfd_target_elf_flavour;
5438
5439   /* Check word size.  If INFO is from a binary file, infer it from
5440      that, else choose a likely default.  */
5441   if (from_xcoff_exec)
5442     {
5443       if (bfd_xcoff_is_xcoff64 (info.abfd))
5444         wordsize = 8;
5445       else
5446         wordsize = 4;
5447     }
5448   else if (from_elf_exec)
5449     {
5450       if (elf_elfheader (info.abfd)->e_ident[EI_CLASS] == ELFCLASS64)
5451         wordsize = 8;
5452       else
5453         wordsize = 4;
5454     }
5455   else if (tdesc_has_registers (tdesc))
5456     wordsize = -1;
5457   else
5458     {
5459       if (info.bfd_arch_info != NULL && info.bfd_arch_info->bits_per_word != 0)
5460         wordsize = (info.bfd_arch_info->bits_per_word
5461                     / info.bfd_arch_info->bits_per_byte);
5462       else
5463         wordsize = 4;
5464     }
5465
5466   /* Get the architecture and machine from the BFD.  */
5467   arch = info.bfd_arch_info->arch;
5468   mach = info.bfd_arch_info->mach;
5469
5470   /* For e500 executables, the apuinfo section is of help here.  Such
5471      section contains the identifier and revision number of each
5472      Application-specific Processing Unit that is present on the
5473      chip.  The content of the section is determined by the assembler
5474      which looks at each instruction and determines which unit (and
5475      which version of it) can execute it.  Grovel through the section
5476      looking for relevant e500 APUs.  */
5477
5478   if (bfd_uses_spe_extensions (info.abfd))
5479     {
5480       arch = info.bfd_arch_info->arch;
5481       mach = bfd_mach_ppc_e500;
5482       bfd_default_set_arch_mach (&abfd, arch, mach);
5483       info.bfd_arch_info = bfd_get_arch_info (&abfd);
5484     }
5485
5486   /* Find a default target description which describes our register
5487      layout, if we do not already have one.  */
5488   if (! tdesc_has_registers (tdesc))
5489     {
5490       const struct variant *v;
5491
5492       /* Choose variant.  */
5493       v = find_variant_by_arch (arch, mach);
5494       if (!v)
5495         return NULL;
5496
5497       tdesc = *v->tdesc;
5498     }
5499
5500   gdb_assert (tdesc_has_registers (tdesc));
5501
5502   /* Check any target description for validity.  */
5503   if (tdesc_has_registers (tdesc))
5504     {
5505       static const char *const gprs[] = {
5506         "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
5507         "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
5508         "r16", "r17", "r18", "r19", "r20", "r21", "r22", "r23",
5509         "r24", "r25", "r26", "r27", "r28", "r29", "r30", "r31"
5510       };
5511       const struct tdesc_feature *feature;
5512       int i, valid_p;
5513       static const char *const msr_names[] = { "msr", "ps" };
5514       static const char *const cr_names[] = { "cr", "cnd" };
5515       static const char *const ctr_names[] = { "ctr", "cnt" };
5516
5517       feature = tdesc_find_feature (tdesc,
5518                                     "org.gnu.gdb.power.core");
5519       if (feature == NULL)
5520         return NULL;
5521
5522       tdesc_data = tdesc_data_alloc ();
5523
5524       valid_p = 1;
5525       for (i = 0; i < ppc_num_gprs; i++)
5526         valid_p &= tdesc_numbered_register (feature, tdesc_data, i, gprs[i]);
5527       valid_p &= tdesc_numbered_register (feature, tdesc_data, PPC_PC_REGNUM,
5528                                           "pc");
5529       valid_p &= tdesc_numbered_register (feature, tdesc_data, PPC_LR_REGNUM,
5530                                           "lr");
5531       valid_p &= tdesc_numbered_register (feature, tdesc_data, PPC_XER_REGNUM,
5532                                           "xer");
5533
5534       /* Allow alternate names for these registers, to accomodate GDB's
5535          historic naming.  */
5536       valid_p &= tdesc_numbered_register_choices (feature, tdesc_data,
5537                                                   PPC_MSR_REGNUM, msr_names);
5538       valid_p &= tdesc_numbered_register_choices (feature, tdesc_data,
5539                                                   PPC_CR_REGNUM, cr_names);
5540       valid_p &= tdesc_numbered_register_choices (feature, tdesc_data,
5541                                                   PPC_CTR_REGNUM, ctr_names);
5542
5543       if (!valid_p)
5544         {
5545           tdesc_data_cleanup (tdesc_data);
5546           return NULL;
5547         }
5548
5549       have_mq = tdesc_numbered_register (feature, tdesc_data, PPC_MQ_REGNUM,
5550                                          "mq");
5551
5552       tdesc_wordsize = tdesc_register_size (feature, "pc") / 8;
5553       if (wordsize == -1)
5554         wordsize = tdesc_wordsize;
5555
5556       feature = tdesc_find_feature (tdesc,
5557                                     "org.gnu.gdb.power.fpu");
5558       if (feature != NULL)
5559         {
5560           static const char *const fprs[] = {
5561             "f0", "f1", "f2", "f3", "f4", "f5", "f6", "f7",
5562             "f8", "f9", "f10", "f11", "f12", "f13", "f14", "f15",
5563             "f16", "f17", "f18", "f19", "f20", "f21", "f22", "f23",
5564             "f24", "f25", "f26", "f27", "f28", "f29", "f30", "f31"
5565           };
5566           valid_p = 1;
5567           for (i = 0; i < ppc_num_fprs; i++)
5568             valid_p &= tdesc_numbered_register (feature, tdesc_data,
5569                                                 PPC_F0_REGNUM + i, fprs[i]);
5570           valid_p &= tdesc_numbered_register (feature, tdesc_data,
5571                                               PPC_FPSCR_REGNUM, "fpscr");
5572
5573           if (!valid_p)
5574             {
5575               tdesc_data_cleanup (tdesc_data);
5576               return NULL;
5577             }
5578           have_fpu = 1;
5579         }
5580       else
5581         have_fpu = 0;
5582
5583       /* The DFP pseudo-registers will be available when there are floating
5584          point registers.  */
5585       have_dfp = have_fpu;
5586
5587       feature = tdesc_find_feature (tdesc,
5588                                     "org.gnu.gdb.power.altivec");
5589       if (feature != NULL)
5590         {
5591           static const char *const vector_regs[] = {
5592             "vr0", "vr1", "vr2", "vr3", "vr4", "vr5", "vr6", "vr7",
5593             "vr8", "vr9", "vr10", "vr11", "vr12", "vr13", "vr14", "vr15",
5594             "vr16", "vr17", "vr18", "vr19", "vr20", "vr21", "vr22", "vr23",
5595             "vr24", "vr25", "vr26", "vr27", "vr28", "vr29", "vr30", "vr31"
5596           };
5597
5598           valid_p = 1;
5599           for (i = 0; i < ppc_num_gprs; i++)
5600             valid_p &= tdesc_numbered_register (feature, tdesc_data,
5601                                                 PPC_VR0_REGNUM + i,
5602                                                 vector_regs[i]);
5603           valid_p &= tdesc_numbered_register (feature, tdesc_data,
5604                                               PPC_VSCR_REGNUM, "vscr");
5605           valid_p &= tdesc_numbered_register (feature, tdesc_data,
5606                                               PPC_VRSAVE_REGNUM, "vrsave");
5607
5608           if (have_spe || !valid_p)
5609             {
5610               tdesc_data_cleanup (tdesc_data);
5611               return NULL;
5612             }
5613           have_altivec = 1;
5614         }
5615       else
5616         have_altivec = 0;
5617
5618       /* Check for POWER7 VSX registers support.  */
5619       feature = tdesc_find_feature (tdesc,
5620                                     "org.gnu.gdb.power.vsx");
5621
5622       if (feature != NULL)
5623         {
5624           static const char *const vsx_regs[] = {
5625             "vs0h", "vs1h", "vs2h", "vs3h", "vs4h", "vs5h",
5626             "vs6h", "vs7h", "vs8h", "vs9h", "vs10h", "vs11h",
5627             "vs12h", "vs13h", "vs14h", "vs15h", "vs16h", "vs17h",
5628             "vs18h", "vs19h", "vs20h", "vs21h", "vs22h", "vs23h",
5629             "vs24h", "vs25h", "vs26h", "vs27h", "vs28h", "vs29h",
5630             "vs30h", "vs31h"
5631           };
5632
5633           valid_p = 1;
5634
5635           for (i = 0; i < ppc_num_vshrs; i++)
5636             valid_p &= tdesc_numbered_register (feature, tdesc_data,
5637                                                 PPC_VSR0_UPPER_REGNUM + i,
5638                                                 vsx_regs[i]);
5639           if (!valid_p)
5640             {
5641               tdesc_data_cleanup (tdesc_data);
5642               return NULL;
5643             }
5644
5645           have_vsx = 1;
5646         }
5647       else
5648         have_vsx = 0;
5649
5650       /* On machines supporting the SPE APU, the general-purpose registers
5651          are 64 bits long.  There are SIMD vector instructions to treat them
5652          as pairs of floats, but the rest of the instruction set treats them
5653          as 32-bit registers, and only operates on their lower halves.
5654
5655          In the GDB regcache, we treat their high and low halves as separate
5656          registers.  The low halves we present as the general-purpose
5657          registers, and then we have pseudo-registers that stitch together
5658          the upper and lower halves and present them as pseudo-registers.
5659
5660          Thus, the target description is expected to supply the upper
5661          halves separately.  */
5662
5663       feature = tdesc_find_feature (tdesc,
5664                                     "org.gnu.gdb.power.spe");
5665       if (feature != NULL)
5666         {
5667           static const char *const upper_spe[] = {
5668             "ev0h", "ev1h", "ev2h", "ev3h",
5669             "ev4h", "ev5h", "ev6h", "ev7h",
5670             "ev8h", "ev9h", "ev10h", "ev11h",
5671             "ev12h", "ev13h", "ev14h", "ev15h",
5672             "ev16h", "ev17h", "ev18h", "ev19h",
5673             "ev20h", "ev21h", "ev22h", "ev23h",
5674             "ev24h", "ev25h", "ev26h", "ev27h",
5675             "ev28h", "ev29h", "ev30h", "ev31h"
5676           };
5677
5678           valid_p = 1;
5679           for (i = 0; i < ppc_num_gprs; i++)
5680             valid_p &= tdesc_numbered_register (feature, tdesc_data,
5681                                                 PPC_SPE_UPPER_GP0_REGNUM + i,
5682                                                 upper_spe[i]);
5683           valid_p &= tdesc_numbered_register (feature, tdesc_data,
5684                                               PPC_SPE_ACC_REGNUM, "acc");
5685           valid_p &= tdesc_numbered_register (feature, tdesc_data,
5686                                               PPC_SPE_FSCR_REGNUM, "spefscr");
5687
5688           if (have_mq || have_fpu || !valid_p)
5689             {
5690               tdesc_data_cleanup (tdesc_data);
5691               return NULL;
5692             }
5693           have_spe = 1;
5694         }
5695       else
5696         have_spe = 0;
5697     }
5698
5699   /* If we have a 64-bit binary on a 32-bit target, complain.  Also
5700      complain for a 32-bit binary on a 64-bit target; we do not yet
5701      support that.  For instance, the 32-bit ABI routines expect
5702      32-bit GPRs.
5703
5704      As long as there isn't an explicit target description, we'll
5705      choose one based on the BFD architecture and get a word size
5706      matching the binary (probably powerpc:common or
5707      powerpc:common64).  So there is only trouble if a 64-bit target
5708      supplies a 64-bit description while debugging a 32-bit
5709      binary.  */
5710   if (tdesc_wordsize != -1 && tdesc_wordsize != wordsize)
5711     {
5712       tdesc_data_cleanup (tdesc_data);
5713       return NULL;
5714     }
5715
5716 #ifdef HAVE_ELF
5717   if (from_elf_exec)
5718     {
5719       switch (elf_elfheader (info.abfd)->e_flags & EF_PPC64_ABI)
5720         {
5721         case 1:
5722           elf_abi = POWERPC_ELF_V1;
5723           break;
5724         case 2:
5725           elf_abi = POWERPC_ELF_V2;
5726           break;
5727         default:
5728           break;
5729         }
5730     }
5731
5732   if (soft_float_flag == AUTO_BOOLEAN_AUTO && from_elf_exec)
5733     {
5734       switch (bfd_elf_get_obj_attr_int (info.abfd, OBJ_ATTR_GNU,
5735                                         Tag_GNU_Power_ABI_FP))
5736         {
5737         case 1:
5738           soft_float_flag = AUTO_BOOLEAN_FALSE;
5739           break;
5740         case 2:
5741           soft_float_flag = AUTO_BOOLEAN_TRUE;
5742           break;
5743         default:
5744           break;
5745         }
5746     }
5747
5748   if (vector_abi == POWERPC_VEC_AUTO && from_elf_exec)
5749     {
5750       switch (bfd_elf_get_obj_attr_int (info.abfd, OBJ_ATTR_GNU,
5751                                         Tag_GNU_Power_ABI_Vector))
5752         {
5753         case 1:
5754           vector_abi = POWERPC_VEC_GENERIC;
5755           break;
5756         case 2:
5757           vector_abi = POWERPC_VEC_ALTIVEC;
5758           break;
5759         case 3:
5760           vector_abi = POWERPC_VEC_SPE;
5761           break;
5762         default:
5763           break;
5764         }
5765     }
5766 #endif
5767
5768   /* At this point, the only supported ELF-based 64-bit little-endian
5769      operating system is GNU/Linux, and this uses the ELFv2 ABI by
5770      default.  All other supported ELF-based operating systems use the
5771      ELFv1 ABI by default.  Therefore, if the ABI marker is missing,
5772      e.g. because we run a legacy binary, or have attached to a process
5773      and have not found any associated binary file, set the default
5774      according to this heuristic.  */
5775   if (elf_abi == POWERPC_ELF_AUTO)
5776     {
5777       if (wordsize == 8 && info.byte_order == BFD_ENDIAN_LITTLE)
5778         elf_abi = POWERPC_ELF_V2;
5779       else
5780         elf_abi = POWERPC_ELF_V1;
5781     }
5782
5783   if (soft_float_flag == AUTO_BOOLEAN_TRUE)
5784     soft_float = 1;
5785   else if (soft_float_flag == AUTO_BOOLEAN_FALSE)
5786     soft_float = 0;
5787   else
5788     soft_float = !have_fpu;
5789
5790   /* If we have a hard float binary or setting but no floating point
5791      registers, downgrade to soft float anyway.  We're still somewhat
5792      useful in this scenario.  */
5793   if (!soft_float && !have_fpu)
5794     soft_float = 1;
5795
5796   /* Similarly for vector registers.  */
5797   if (vector_abi == POWERPC_VEC_ALTIVEC && !have_altivec)
5798     vector_abi = POWERPC_VEC_GENERIC;
5799
5800   if (vector_abi == POWERPC_VEC_SPE && !have_spe)
5801     vector_abi = POWERPC_VEC_GENERIC;
5802
5803   if (vector_abi == POWERPC_VEC_AUTO)
5804     {
5805       if (have_altivec)
5806         vector_abi = POWERPC_VEC_ALTIVEC;
5807       else if (have_spe)
5808         vector_abi = POWERPC_VEC_SPE;
5809       else
5810         vector_abi = POWERPC_VEC_GENERIC;
5811     }
5812
5813   /* Do not limit the vector ABI based on available hardware, since we
5814      do not yet know what hardware we'll decide we have.  Yuck!  FIXME!  */
5815
5816   /* Find a candidate among extant architectures.  */
5817   for (arches = gdbarch_list_lookup_by_info (arches, &info);
5818        arches != NULL;
5819        arches = gdbarch_list_lookup_by_info (arches->next, &info))
5820     {
5821       /* Word size in the various PowerPC bfd_arch_info structs isn't
5822          meaningful, because 64-bit CPUs can run in 32-bit mode.  So, perform
5823          separate word size check.  */
5824       tdep = gdbarch_tdep (arches->gdbarch);
5825       if (tdep && tdep->elf_abi != elf_abi)
5826         continue;
5827       if (tdep && tdep->soft_float != soft_float)
5828         continue;
5829       if (tdep && tdep->vector_abi != vector_abi)
5830         continue;
5831       if (tdep && tdep->wordsize == wordsize)
5832         {
5833           if (tdesc_data != NULL)
5834             tdesc_data_cleanup (tdesc_data);
5835           return arches->gdbarch;
5836         }
5837     }
5838
5839   /* None found, create a new architecture from INFO, whose bfd_arch_info
5840      validity depends on the source:
5841        - executable             useless
5842        - rs6000_host_arch()     good
5843        - core file              good
5844        - "set arch"             trust blindly
5845        - GDB startup            useless but harmless */
5846
5847   tdep = XCNEW (struct gdbarch_tdep);
5848   tdep->wordsize = wordsize;
5849   tdep->elf_abi = elf_abi;
5850   tdep->soft_float = soft_float;
5851   tdep->vector_abi = vector_abi;
5852
5853   gdbarch = gdbarch_alloc (&info, tdep);
5854
5855   tdep->ppc_gp0_regnum = PPC_R0_REGNUM;
5856   tdep->ppc_toc_regnum = PPC_R0_REGNUM + 2;
5857   tdep->ppc_ps_regnum = PPC_MSR_REGNUM;
5858   tdep->ppc_cr_regnum = PPC_CR_REGNUM;
5859   tdep->ppc_lr_regnum = PPC_LR_REGNUM;
5860   tdep->ppc_ctr_regnum = PPC_CTR_REGNUM;
5861   tdep->ppc_xer_regnum = PPC_XER_REGNUM;
5862   tdep->ppc_mq_regnum = have_mq ? PPC_MQ_REGNUM : -1;
5863
5864   tdep->ppc_fp0_regnum = have_fpu ? PPC_F0_REGNUM : -1;
5865   tdep->ppc_fpscr_regnum = have_fpu ? PPC_FPSCR_REGNUM : -1;
5866   tdep->ppc_vsr0_upper_regnum = have_vsx ? PPC_VSR0_UPPER_REGNUM : -1;
5867   tdep->ppc_vr0_regnum = have_altivec ? PPC_VR0_REGNUM : -1;
5868   tdep->ppc_vrsave_regnum = have_altivec ? PPC_VRSAVE_REGNUM : -1;
5869   tdep->ppc_ev0_upper_regnum = have_spe ? PPC_SPE_UPPER_GP0_REGNUM : -1;
5870   tdep->ppc_acc_regnum = have_spe ? PPC_SPE_ACC_REGNUM : -1;
5871   tdep->ppc_spefscr_regnum = have_spe ? PPC_SPE_FSCR_REGNUM : -1;
5872
5873   set_gdbarch_pc_regnum (gdbarch, PPC_PC_REGNUM);
5874   set_gdbarch_sp_regnum (gdbarch, PPC_R0_REGNUM + 1);
5875   set_gdbarch_deprecated_fp_regnum (gdbarch, PPC_R0_REGNUM + 1);
5876   set_gdbarch_fp0_regnum (gdbarch, tdep->ppc_fp0_regnum);
5877   set_gdbarch_register_sim_regno (gdbarch, rs6000_register_sim_regno);
5878
5879   /* The XML specification for PowerPC sensibly calls the MSR "msr".
5880      GDB traditionally called it "ps", though, so let GDB add an
5881      alias.  */
5882   set_gdbarch_ps_regnum (gdbarch, tdep->ppc_ps_regnum);
5883
5884   if (wordsize == 8)
5885     set_gdbarch_return_value (gdbarch, ppc64_sysv_abi_return_value);
5886   else
5887     set_gdbarch_return_value (gdbarch, ppc_sysv_abi_return_value);
5888
5889   /* Set lr_frame_offset.  */
5890   if (wordsize == 8)
5891     tdep->lr_frame_offset = 16;
5892   else
5893     tdep->lr_frame_offset = 4;
5894
5895   if (have_spe || have_dfp || have_vsx)
5896     {
5897       set_gdbarch_pseudo_register_read (gdbarch, rs6000_pseudo_register_read);
5898       set_gdbarch_pseudo_register_write (gdbarch,
5899                                          rs6000_pseudo_register_write);
5900     }
5901
5902   set_gdbarch_have_nonsteppable_watchpoint (gdbarch, 1);
5903
5904   /* Select instruction printer.  */
5905   if (arch == bfd_arch_rs6000)
5906     set_gdbarch_print_insn (gdbarch, print_insn_rs6000);
5907   else
5908     set_gdbarch_print_insn (gdbarch, gdb_print_insn_powerpc);
5909
5910   set_gdbarch_num_regs (gdbarch, PPC_NUM_REGS);
5911
5912   if (have_spe)
5913     num_pseudoregs += 32;
5914   if (have_dfp)
5915     num_pseudoregs += 16;
5916   if (have_vsx)
5917     /* Include both VSX and Extended FP registers.  */
5918     num_pseudoregs += 96;
5919
5920   set_gdbarch_num_pseudo_regs (gdbarch, num_pseudoregs);
5921
5922   set_gdbarch_ptr_bit (gdbarch, wordsize * TARGET_CHAR_BIT);
5923   set_gdbarch_short_bit (gdbarch, 2 * TARGET_CHAR_BIT);
5924   set_gdbarch_int_bit (gdbarch, 4 * TARGET_CHAR_BIT);
5925   set_gdbarch_long_bit (gdbarch, wordsize * TARGET_CHAR_BIT);
5926   set_gdbarch_long_long_bit (gdbarch, 8 * TARGET_CHAR_BIT);
5927   set_gdbarch_float_bit (gdbarch, 4 * TARGET_CHAR_BIT);
5928   set_gdbarch_double_bit (gdbarch, 8 * TARGET_CHAR_BIT);
5929   set_gdbarch_long_double_bit (gdbarch, 16 * TARGET_CHAR_BIT);
5930   set_gdbarch_char_signed (gdbarch, 0);
5931
5932   set_gdbarch_frame_align (gdbarch, rs6000_frame_align);
5933   if (wordsize == 8)
5934     /* PPC64 SYSV.  */
5935     set_gdbarch_frame_red_zone_size (gdbarch, 288);
5936
5937   set_gdbarch_convert_register_p (gdbarch, rs6000_convert_register_p);
5938   set_gdbarch_register_to_value (gdbarch, rs6000_register_to_value);
5939   set_gdbarch_value_to_register (gdbarch, rs6000_value_to_register);
5940
5941   set_gdbarch_stab_reg_to_regnum (gdbarch, rs6000_stab_reg_to_regnum);
5942   set_gdbarch_dwarf2_reg_to_regnum (gdbarch, rs6000_dwarf2_reg_to_regnum);
5943
5944   if (wordsize == 4)
5945     set_gdbarch_push_dummy_call (gdbarch, ppc_sysv_abi_push_dummy_call);
5946   else if (wordsize == 8)
5947     set_gdbarch_push_dummy_call (gdbarch, ppc64_sysv_abi_push_dummy_call);
5948
5949   set_gdbarch_skip_prologue (gdbarch, rs6000_skip_prologue);
5950   set_gdbarch_stack_frame_destroyed_p (gdbarch, rs6000_stack_frame_destroyed_p);
5951   set_gdbarch_skip_main_prologue (gdbarch, rs6000_skip_main_prologue);
5952
5953   set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
5954   set_gdbarch_breakpoint_from_pc (gdbarch, rs6000_breakpoint_from_pc);
5955
5956   /* The value of symbols of type N_SO and N_FUN maybe null when
5957      it shouldn't be.  */
5958   set_gdbarch_sofun_address_maybe_missing (gdbarch, 1);
5959
5960   /* Handles single stepping of atomic sequences.  */
5961   set_gdbarch_software_single_step (gdbarch, ppc_deal_with_atomic_sequence);
5962   
5963   /* Not sure on this.  FIXMEmgo */
5964   set_gdbarch_frame_args_skip (gdbarch, 8);
5965
5966   /* Helpers for function argument information.  */
5967   set_gdbarch_fetch_pointer_argument (gdbarch, rs6000_fetch_pointer_argument);
5968
5969   /* Trampoline.  */
5970   set_gdbarch_in_solib_return_trampoline
5971     (gdbarch, rs6000_in_solib_return_trampoline);
5972   set_gdbarch_skip_trampoline_code (gdbarch, rs6000_skip_trampoline_code);
5973
5974   /* Hook in the DWARF CFI frame unwinder.  */
5975   dwarf2_append_unwinders (gdbarch);
5976   dwarf2_frame_set_adjust_regnum (gdbarch, rs6000_adjust_frame_regnum);
5977
5978   /* Frame handling.  */
5979   dwarf2_frame_set_init_reg (gdbarch, ppc_dwarf2_frame_init_reg);
5980
5981   /* Setup displaced stepping.  */
5982   set_gdbarch_displaced_step_copy_insn (gdbarch,
5983                                         ppc_displaced_step_copy_insn);
5984   set_gdbarch_displaced_step_hw_singlestep (gdbarch,
5985                                             ppc_displaced_step_hw_singlestep);
5986   set_gdbarch_displaced_step_fixup (gdbarch, ppc_displaced_step_fixup);
5987   set_gdbarch_displaced_step_free_closure (gdbarch,
5988                                            simple_displaced_step_free_closure);
5989   set_gdbarch_displaced_step_location (gdbarch,
5990                                        displaced_step_at_entry_point);
5991
5992   set_gdbarch_max_insn_length (gdbarch, PPC_INSN_SIZE);
5993
5994   /* Hook in ABI-specific overrides, if they have been registered.  */
5995   info.target_desc = tdesc;
5996   info.tdep_info = tdesc_data;
5997   gdbarch_init_osabi (info, gdbarch);
5998
5999   switch (info.osabi)
6000     {
6001     case GDB_OSABI_LINUX:
6002     case GDB_OSABI_NETBSD_AOUT:
6003     case GDB_OSABI_NETBSD_ELF:
6004     case GDB_OSABI_UNKNOWN:
6005       set_gdbarch_unwind_pc (gdbarch, rs6000_unwind_pc);
6006       frame_unwind_append_unwinder (gdbarch, &rs6000_epilogue_frame_unwind);
6007       frame_unwind_append_unwinder (gdbarch, &rs6000_frame_unwind);
6008       set_gdbarch_dummy_id (gdbarch, rs6000_dummy_id);
6009       frame_base_append_sniffer (gdbarch, rs6000_frame_base_sniffer);
6010       break;
6011     default:
6012       set_gdbarch_believe_pcc_promotion (gdbarch, 1);
6013
6014       set_gdbarch_unwind_pc (gdbarch, rs6000_unwind_pc);
6015       frame_unwind_append_unwinder (gdbarch, &rs6000_epilogue_frame_unwind);
6016       frame_unwind_append_unwinder (gdbarch, &rs6000_frame_unwind);
6017       set_gdbarch_dummy_id (gdbarch, rs6000_dummy_id);
6018       frame_base_append_sniffer (gdbarch, rs6000_frame_base_sniffer);
6019     }
6020
6021   set_tdesc_pseudo_register_type (gdbarch, rs6000_pseudo_register_type);
6022   set_tdesc_pseudo_register_reggroup_p (gdbarch,
6023                                         rs6000_pseudo_register_reggroup_p);
6024   tdesc_use_registers (gdbarch, tdesc, tdesc_data);
6025
6026   /* Override the normal target description method to make the SPE upper
6027      halves anonymous.  */
6028   set_gdbarch_register_name (gdbarch, rs6000_register_name);
6029
6030   /* Choose register numbers for all supported pseudo-registers.  */
6031   tdep->ppc_ev0_regnum = -1;
6032   tdep->ppc_dl0_regnum = -1;
6033   tdep->ppc_vsr0_regnum = -1;
6034   tdep->ppc_efpr0_regnum = -1;
6035
6036   cur_reg = gdbarch_num_regs (gdbarch);
6037
6038   if (have_spe)
6039     {
6040       tdep->ppc_ev0_regnum = cur_reg;
6041       cur_reg += 32;
6042     }
6043   if (have_dfp)
6044     {
6045       tdep->ppc_dl0_regnum = cur_reg;
6046       cur_reg += 16;
6047     }
6048   if (have_vsx)
6049     {
6050       tdep->ppc_vsr0_regnum = cur_reg;
6051       cur_reg += 64;
6052       tdep->ppc_efpr0_regnum = cur_reg;
6053       cur_reg += 32;
6054     }
6055
6056   gdb_assert (gdbarch_num_regs (gdbarch)
6057               + gdbarch_num_pseudo_regs (gdbarch) == cur_reg);
6058
6059   /* Register the ravenscar_arch_ops.  */
6060   if (mach == bfd_mach_ppc_e500)
6061     register_e500_ravenscar_ops (gdbarch);
6062   else
6063     register_ppc_ravenscar_ops (gdbarch);
6064
6065   return gdbarch;
6066 }
6067
6068 static void
6069 rs6000_dump_tdep (struct gdbarch *gdbarch, struct ui_file *file)
6070 {
6071   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
6072
6073   if (tdep == NULL)
6074     return;
6075
6076   /* FIXME: Dump gdbarch_tdep.  */
6077 }
6078
6079 /* PowerPC-specific commands.  */
6080
6081 static void
6082 set_powerpc_command (char *args, int from_tty)
6083 {
6084   printf_unfiltered (_("\
6085 \"set powerpc\" must be followed by an appropriate subcommand.\n"));
6086   help_list (setpowerpccmdlist, "set powerpc ", all_commands, gdb_stdout);
6087 }
6088
6089 static void
6090 show_powerpc_command (char *args, int from_tty)
6091 {
6092   cmd_show_list (showpowerpccmdlist, from_tty, "");
6093 }
6094
6095 static void
6096 powerpc_set_soft_float (char *args, int from_tty,
6097                         struct cmd_list_element *c)
6098 {
6099   struct gdbarch_info info;
6100
6101   /* Update the architecture.  */
6102   gdbarch_info_init (&info);
6103   if (!gdbarch_update_p (info))
6104     internal_error (__FILE__, __LINE__, _("could not update architecture"));
6105 }
6106
6107 static void
6108 powerpc_set_vector_abi (char *args, int from_tty,
6109                         struct cmd_list_element *c)
6110 {
6111   struct gdbarch_info info;
6112   int vector_abi;
6113
6114   for (vector_abi = POWERPC_VEC_AUTO;
6115        vector_abi != POWERPC_VEC_LAST;
6116        vector_abi++)
6117     if (strcmp (powerpc_vector_abi_string,
6118                 powerpc_vector_strings[vector_abi]) == 0)
6119       {
6120         powerpc_vector_abi_global = (enum powerpc_vector_abi) vector_abi;
6121         break;
6122       }
6123
6124   if (vector_abi == POWERPC_VEC_LAST)
6125     internal_error (__FILE__, __LINE__, _("Invalid vector ABI accepted: %s."),
6126                     powerpc_vector_abi_string);
6127
6128   /* Update the architecture.  */
6129   gdbarch_info_init (&info);
6130   if (!gdbarch_update_p (info))
6131     internal_error (__FILE__, __LINE__, _("could not update architecture"));
6132 }
6133
6134 /* Show the current setting of the exact watchpoints flag.  */
6135
6136 static void
6137 show_powerpc_exact_watchpoints (struct ui_file *file, int from_tty,
6138                                 struct cmd_list_element *c,
6139                                 const char *value)
6140 {
6141   fprintf_filtered (file, _("Use of exact watchpoints is %s.\n"), value);
6142 }
6143
6144 /* Read a PPC instruction from memory.  */
6145
6146 static unsigned int
6147 read_insn (struct frame_info *frame, CORE_ADDR pc)
6148 {
6149   struct gdbarch *gdbarch = get_frame_arch (frame);
6150   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
6151
6152   return read_memory_unsigned_integer (pc, 4, byte_order);
6153 }
6154
6155 /* Return non-zero if the instructions at PC match the series
6156    described in PATTERN, or zero otherwise.  PATTERN is an array of
6157    'struct ppc_insn_pattern' objects, terminated by an entry whose
6158    mask is zero.
6159
6160    When the match is successful, fill INSN[i] with what PATTERN[i]
6161    matched.  If PATTERN[i] is optional, and the instruction wasn't
6162    present, set INSN[i] to 0 (which is not a valid PPC instruction).
6163    INSN should have as many elements as PATTERN.  Note that, if
6164    PATTERN contains optional instructions which aren't present in
6165    memory, then INSN will have holes, so INSN[i] isn't necessarily the
6166    i'th instruction in memory.  */
6167
6168 int
6169 ppc_insns_match_pattern (struct frame_info *frame, CORE_ADDR pc,
6170                          struct ppc_insn_pattern *pattern,
6171                          unsigned int *insns)
6172 {
6173   int i;
6174   unsigned int insn;
6175
6176   for (i = 0, insn = 0; pattern[i].mask; i++)
6177     {
6178       if (insn == 0)
6179         insn = read_insn (frame, pc);
6180       insns[i] = 0;
6181       if ((insn & pattern[i].mask) == pattern[i].data)
6182         {
6183           insns[i] = insn;
6184           pc += 4;
6185           insn = 0;
6186         }
6187       else if (!pattern[i].optional)
6188         return 0;
6189     }
6190
6191   return 1;
6192 }
6193
6194 /* Return the 'd' field of the d-form instruction INSN, properly
6195    sign-extended.  */
6196
6197 CORE_ADDR
6198 ppc_insn_d_field (unsigned int insn)
6199 {
6200   return ((((CORE_ADDR) insn & 0xffff) ^ 0x8000) - 0x8000);
6201 }
6202
6203 /* Return the 'ds' field of the ds-form instruction INSN, with the two
6204    zero bits concatenated at the right, and properly
6205    sign-extended.  */
6206
6207 CORE_ADDR
6208 ppc_insn_ds_field (unsigned int insn)
6209 {
6210   return ((((CORE_ADDR) insn & 0xfffc) ^ 0x8000) - 0x8000);
6211 }
6212
6213 /* Initialization code.  */
6214
6215 /* -Wmissing-prototypes */
6216 extern initialize_file_ftype _initialize_rs6000_tdep;
6217
6218 void
6219 _initialize_rs6000_tdep (void)
6220 {
6221   gdbarch_register (bfd_arch_rs6000, rs6000_gdbarch_init, rs6000_dump_tdep);
6222   gdbarch_register (bfd_arch_powerpc, rs6000_gdbarch_init, rs6000_dump_tdep);
6223
6224   /* Initialize the standard target descriptions.  */
6225   initialize_tdesc_powerpc_32 ();
6226   initialize_tdesc_powerpc_altivec32 ();
6227   initialize_tdesc_powerpc_vsx32 ();
6228   initialize_tdesc_powerpc_403 ();
6229   initialize_tdesc_powerpc_403gc ();
6230   initialize_tdesc_powerpc_405 ();
6231   initialize_tdesc_powerpc_505 ();
6232   initialize_tdesc_powerpc_601 ();
6233   initialize_tdesc_powerpc_602 ();
6234   initialize_tdesc_powerpc_603 ();
6235   initialize_tdesc_powerpc_604 ();
6236   initialize_tdesc_powerpc_64 ();
6237   initialize_tdesc_powerpc_altivec64 ();
6238   initialize_tdesc_powerpc_vsx64 ();
6239   initialize_tdesc_powerpc_7400 ();
6240   initialize_tdesc_powerpc_750 ();
6241   initialize_tdesc_powerpc_860 ();
6242   initialize_tdesc_powerpc_e500 ();
6243   initialize_tdesc_rs6000 ();
6244
6245   /* Add root prefix command for all "set powerpc"/"show powerpc"
6246      commands.  */
6247   add_prefix_cmd ("powerpc", no_class, set_powerpc_command,
6248                   _("Various PowerPC-specific commands."),
6249                   &setpowerpccmdlist, "set powerpc ", 0, &setlist);
6250
6251   add_prefix_cmd ("powerpc", no_class, show_powerpc_command,
6252                   _("Various PowerPC-specific commands."),
6253                   &showpowerpccmdlist, "show powerpc ", 0, &showlist);
6254
6255   /* Add a command to allow the user to force the ABI.  */
6256   add_setshow_auto_boolean_cmd ("soft-float", class_support,
6257                                 &powerpc_soft_float_global,
6258                                 _("Set whether to use a soft-float ABI."),
6259                                 _("Show whether to use a soft-float ABI."),
6260                                 NULL,
6261                                 powerpc_set_soft_float, NULL,
6262                                 &setpowerpccmdlist, &showpowerpccmdlist);
6263
6264   add_setshow_enum_cmd ("vector-abi", class_support, powerpc_vector_strings,
6265                         &powerpc_vector_abi_string,
6266                         _("Set the vector ABI."),
6267                         _("Show the vector ABI."),
6268                         NULL, powerpc_set_vector_abi, NULL,
6269                         &setpowerpccmdlist, &showpowerpccmdlist);
6270
6271   add_setshow_boolean_cmd ("exact-watchpoints", class_support,
6272                            &target_exact_watchpoints,
6273                            _("\
6274 Set whether to use just one debug register for watchpoints on scalars."),
6275                            _("\
6276 Show whether to use just one debug register for watchpoints on scalars."),
6277                            _("\
6278 If true, GDB will use only one debug register when watching a variable of\n\
6279 scalar type, thus assuming that the variable is accessed through the address\n\
6280 of its first byte."),
6281                            NULL, show_powerpc_exact_watchpoints,
6282                            &setpowerpccmdlist, &showpowerpccmdlist);
6283 }