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