2004-12-05 Randolph Chung <tausq@debian.org>
[external/binutils.git] / gdb / hppa-tdep.c
1 /* Target-dependent code for the HP PA architecture, for GDB.
2
3    Copyright 1986, 1987, 1989, 1990, 1991, 1992, 1993, 1994, 1995,
4    1996, 1998, 1999, 2000, 2001, 2002, 2003, 2004 Free Software
5    Foundation, Inc.
6
7    Contributed by the Center for Software Science at the
8    University of Utah (pa-gdb-bugs@cs.utah.edu).
9
10    This file is part of GDB.
11
12    This program is free software; you can redistribute it and/or modify
13    it under the terms of the GNU General Public License as published by
14    the Free Software Foundation; either version 2 of the License, or
15    (at your option) any later version.
16
17    This program is distributed in the hope that it will be useful,
18    but WITHOUT ANY WARRANTY; without even the implied warranty of
19    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20    GNU General Public License for more details.
21
22    You should have received a copy of the GNU General Public License
23    along with this program; if not, write to the Free Software
24    Foundation, Inc., 59 Temple Place - Suite 330,
25    Boston, MA 02111-1307, USA.  */
26
27 #include "defs.h"
28 #include "bfd.h"
29 #include "inferior.h"
30 #include "regcache.h"
31 #include "completer.h"
32 #include "osabi.h"
33 #include "gdb_assert.h"
34 #include "arch-utils.h"
35 /* For argument passing to the inferior */
36 #include "symtab.h"
37 #include "dis-asm.h"
38 #include "trad-frame.h"
39 #include "frame-unwind.h"
40 #include "frame-base.h"
41
42 #include "gdbcore.h"
43 #include "gdbcmd.h"
44 #include "objfiles.h"
45 #include "hppa-tdep.h"
46
47 static int hppa_debug = 0;
48
49 /* Some local constants.  */
50 static const int hppa32_num_regs = 128;
51 static const int hppa64_num_regs = 96;
52
53 /* hppa-specific object data -- unwind and solib info.
54    TODO/maybe: think about splitting this into two parts; the unwind data is 
55    common to all hppa targets, but is only used in this file; we can register 
56    that separately and make this static. The solib data is probably hpux-
57    specific, so we can create a separate extern objfile_data that is registered
58    by hppa-hpux-tdep.c and shared with pa64solib.c and somsolib.c.  */
59 const struct objfile_data *hppa_objfile_priv_data = NULL;
60
61 /* Get at various relevent fields of an instruction word. */
62 #define MASK_5 0x1f
63 #define MASK_11 0x7ff
64 #define MASK_14 0x3fff
65 #define MASK_21 0x1fffff
66
67 /* Sizes (in bytes) of the native unwind entries.  */
68 #define UNWIND_ENTRY_SIZE 16
69 #define STUB_UNWIND_ENTRY_SIZE 8
70
71 /* FIXME: brobecker 2002-11-07: We will likely be able to make the
72    following functions static, once we hppa is partially multiarched.  */
73 int hppa_pc_requires_run_before_use (CORE_ADDR pc);
74
75 /* Handle 32/64-bit struct return conventions.  */
76
77 static enum return_value_convention
78 hppa32_return_value (struct gdbarch *gdbarch,
79                      struct type *type, struct regcache *regcache,
80                      void *readbuf, const void *writebuf)
81 {
82   if (TYPE_LENGTH (type) <= 2 * 4)
83     {
84       /* The value always lives in the right hand end of the register
85          (or register pair)?  */
86       int b;
87       int reg = TYPE_CODE (type) == TYPE_CODE_FLT ? HPPA_FP4_REGNUM : 28;
88       int part = TYPE_LENGTH (type) % 4;
89       /* The left hand register contains only part of the value,
90          transfer that first so that the rest can be xfered as entire
91          4-byte registers.  */
92       if (part > 0)
93         {
94           if (readbuf != NULL)
95             regcache_cooked_read_part (regcache, reg, 4 - part,
96                                        part, readbuf);
97           if (writebuf != NULL)
98             regcache_cooked_write_part (regcache, reg, 4 - part,
99                                         part, writebuf);
100           reg++;
101         }
102       /* Now transfer the remaining register values.  */
103       for (b = part; b < TYPE_LENGTH (type); b += 4)
104         {
105           if (readbuf != NULL)
106             regcache_cooked_read (regcache, reg, (char *) readbuf + b);
107           if (writebuf != NULL)
108             regcache_cooked_write (regcache, reg, (const char *) writebuf + b);
109           reg++;
110         }
111       return RETURN_VALUE_REGISTER_CONVENTION;
112     }
113   else
114     return RETURN_VALUE_STRUCT_CONVENTION;
115 }
116
117 static enum return_value_convention
118 hppa64_return_value (struct gdbarch *gdbarch,
119                      struct type *type, struct regcache *regcache,
120                      void *readbuf, const void *writebuf)
121 {
122   /* RM: Floats are returned in FR4R, doubles in FR4.  Integral values
123      are in r28, padded on the left.  Aggregates less that 65 bits are
124      in r28, right padded.  Aggregates upto 128 bits are in r28 and
125      r29, right padded.  */ 
126   if (TYPE_CODE (type) == TYPE_CODE_FLT
127       && TYPE_LENGTH (type) <= 8)
128     {
129       /* Floats are right aligned?  */
130       int offset = register_size (gdbarch, HPPA_FP4_REGNUM) - TYPE_LENGTH (type);
131       if (readbuf != NULL)
132         regcache_cooked_read_part (regcache, HPPA_FP4_REGNUM, offset,
133                                    TYPE_LENGTH (type), readbuf);
134       if (writebuf != NULL)
135         regcache_cooked_write_part (regcache, HPPA_FP4_REGNUM, offset,
136                                     TYPE_LENGTH (type), writebuf);
137       return RETURN_VALUE_REGISTER_CONVENTION;
138     }
139   else if (TYPE_LENGTH (type) <= 8 && is_integral_type (type))
140     {
141       /* Integrals are right aligned.  */
142       int offset = register_size (gdbarch, HPPA_FP4_REGNUM) - TYPE_LENGTH (type);
143       if (readbuf != NULL)
144         regcache_cooked_read_part (regcache, 28, offset,
145                                    TYPE_LENGTH (type), readbuf);
146       if (writebuf != NULL)
147         regcache_cooked_write_part (regcache, 28, offset,
148                                     TYPE_LENGTH (type), writebuf);
149       return RETURN_VALUE_REGISTER_CONVENTION;
150     }
151   else if (TYPE_LENGTH (type) <= 2 * 8)
152     {
153       /* Composite values are left aligned.  */
154       int b;
155       for (b = 0; b < TYPE_LENGTH (type); b += 8)
156         {
157           int part = min (8, TYPE_LENGTH (type) - b);
158           if (readbuf != NULL)
159             regcache_cooked_read_part (regcache, 28 + b / 8, 0, part,
160                                        (char *) readbuf + b);
161           if (writebuf != NULL)
162             regcache_cooked_write_part (regcache, 28 + b / 8, 0, part,
163                                         (const char *) writebuf + b);
164         }
165       return RETURN_VALUE_REGISTER_CONVENTION;
166     }
167   else
168     return RETURN_VALUE_STRUCT_CONVENTION;
169 }
170
171 /* Routines to extract various sized constants out of hppa 
172    instructions. */
173
174 /* This assumes that no garbage lies outside of the lower bits of 
175    value. */
176
177 int
178 hppa_sign_extend (unsigned val, unsigned bits)
179 {
180   return (int) (val >> (bits - 1) ? (-1 << bits) | val : val);
181 }
182
183 /* For many immediate values the sign bit is the low bit! */
184
185 int
186 hppa_low_hppa_sign_extend (unsigned val, unsigned bits)
187 {
188   return (int) ((val & 0x1 ? (-1 << (bits - 1)) : 0) | val >> 1);
189 }
190
191 /* Extract the bits at positions between FROM and TO, using HP's numbering
192    (MSB = 0). */
193
194 int
195 hppa_get_field (unsigned word, int from, int to)
196 {
197   return ((word) >> (31 - (to)) & ((1 << ((to) - (from) + 1)) - 1));
198 }
199
200 /* extract the immediate field from a ld{bhw}s instruction */
201
202 int
203 hppa_extract_5_load (unsigned word)
204 {
205   return hppa_low_hppa_sign_extend (word >> 16 & MASK_5, 5);
206 }
207
208 /* extract the immediate field from a break instruction */
209
210 unsigned
211 hppa_extract_5r_store (unsigned word)
212 {
213   return (word & MASK_5);
214 }
215
216 /* extract the immediate field from a {sr}sm instruction */
217
218 unsigned
219 hppa_extract_5R_store (unsigned word)
220 {
221   return (word >> 16 & MASK_5);
222 }
223
224 /* extract a 14 bit immediate field */
225
226 int
227 hppa_extract_14 (unsigned word)
228 {
229   return hppa_low_hppa_sign_extend (word & MASK_14, 14);
230 }
231
232 /* extract a 21 bit constant */
233
234 int
235 hppa_extract_21 (unsigned word)
236 {
237   int val;
238
239   word &= MASK_21;
240   word <<= 11;
241   val = hppa_get_field (word, 20, 20);
242   val <<= 11;
243   val |= hppa_get_field (word, 9, 19);
244   val <<= 2;
245   val |= hppa_get_field (word, 5, 6);
246   val <<= 5;
247   val |= hppa_get_field (word, 0, 4);
248   val <<= 2;
249   val |= hppa_get_field (word, 7, 8);
250   return hppa_sign_extend (val, 21) << 11;
251 }
252
253 /* extract a 17 bit constant from branch instructions, returning the
254    19 bit signed value. */
255
256 int
257 hppa_extract_17 (unsigned word)
258 {
259   return hppa_sign_extend (hppa_get_field (word, 19, 28) |
260                       hppa_get_field (word, 29, 29) << 10 |
261                       hppa_get_field (word, 11, 15) << 11 |
262                       (word & 0x1) << 16, 17) << 2;
263 }
264
265 CORE_ADDR 
266 hppa_symbol_address(const char *sym)
267 {
268   struct minimal_symbol *minsym;
269
270   minsym = lookup_minimal_symbol (sym, NULL, NULL);
271   if (minsym)
272     return SYMBOL_VALUE_ADDRESS (minsym);
273   else
274     return (CORE_ADDR)-1;
275 }
276 \f
277
278 /* Compare the start address for two unwind entries returning 1 if 
279    the first address is larger than the second, -1 if the second is
280    larger than the first, and zero if they are equal.  */
281
282 static int
283 compare_unwind_entries (const void *arg1, const void *arg2)
284 {
285   const struct unwind_table_entry *a = arg1;
286   const struct unwind_table_entry *b = arg2;
287
288   if (a->region_start > b->region_start)
289     return 1;
290   else if (a->region_start < b->region_start)
291     return -1;
292   else
293     return 0;
294 }
295
296 static void
297 record_text_segment_lowaddr (bfd *abfd, asection *section, void *data)
298 {
299   if ((section->flags & (SEC_ALLOC | SEC_LOAD | SEC_READONLY))
300        == (SEC_ALLOC | SEC_LOAD | SEC_READONLY))
301     {
302       bfd_vma value = section->vma - section->filepos;
303       CORE_ADDR *low_text_segment_address = (CORE_ADDR *)data;
304
305       if (value < *low_text_segment_address)
306           *low_text_segment_address = value;
307     }
308 }
309
310 static void
311 internalize_unwinds (struct objfile *objfile, struct unwind_table_entry *table,
312                      asection *section, unsigned int entries, unsigned int size,
313                      CORE_ADDR text_offset)
314 {
315   /* We will read the unwind entries into temporary memory, then
316      fill in the actual unwind table.  */
317
318   if (size > 0)
319     {
320       unsigned long tmp;
321       unsigned i;
322       char *buf = alloca (size);
323       CORE_ADDR low_text_segment_address;
324
325       /* For ELF targets, then unwinds are supposed to
326          be segment relative offsets instead of absolute addresses. 
327
328          Note that when loading a shared library (text_offset != 0) the
329          unwinds are already relative to the text_offset that will be
330          passed in.  */
331       if (gdbarch_tdep (current_gdbarch)->is_elf && text_offset == 0)
332         {
333           low_text_segment_address = -1;
334
335           bfd_map_over_sections (objfile->obfd,
336                                  record_text_segment_lowaddr, 
337                                  &low_text_segment_address);
338
339           text_offset = low_text_segment_address;
340         }
341
342       bfd_get_section_contents (objfile->obfd, section, buf, 0, size);
343
344       /* Now internalize the information being careful to handle host/target
345          endian issues.  */
346       for (i = 0; i < entries; i++)
347         {
348           table[i].region_start = bfd_get_32 (objfile->obfd,
349                                               (bfd_byte *) buf);
350           table[i].region_start += text_offset;
351           buf += 4;
352           table[i].region_end = bfd_get_32 (objfile->obfd, (bfd_byte *) buf);
353           table[i].region_end += text_offset;
354           buf += 4;
355           tmp = bfd_get_32 (objfile->obfd, (bfd_byte *) buf);
356           buf += 4;
357           table[i].Cannot_unwind = (tmp >> 31) & 0x1;
358           table[i].Millicode = (tmp >> 30) & 0x1;
359           table[i].Millicode_save_sr0 = (tmp >> 29) & 0x1;
360           table[i].Region_description = (tmp >> 27) & 0x3;
361           table[i].reserved1 = (tmp >> 26) & 0x1;
362           table[i].Entry_SR = (tmp >> 25) & 0x1;
363           table[i].Entry_FR = (tmp >> 21) & 0xf;
364           table[i].Entry_GR = (tmp >> 16) & 0x1f;
365           table[i].Args_stored = (tmp >> 15) & 0x1;
366           table[i].Variable_Frame = (tmp >> 14) & 0x1;
367           table[i].Separate_Package_Body = (tmp >> 13) & 0x1;
368           table[i].Frame_Extension_Millicode = (tmp >> 12) & 0x1;
369           table[i].Stack_Overflow_Check = (tmp >> 11) & 0x1;
370           table[i].Two_Instruction_SP_Increment = (tmp >> 10) & 0x1;
371           table[i].Ada_Region = (tmp >> 9) & 0x1;
372           table[i].cxx_info = (tmp >> 8) & 0x1;
373           table[i].cxx_try_catch = (tmp >> 7) & 0x1;
374           table[i].sched_entry_seq = (tmp >> 6) & 0x1;
375           table[i].reserved2 = (tmp >> 5) & 0x1;
376           table[i].Save_SP = (tmp >> 4) & 0x1;
377           table[i].Save_RP = (tmp >> 3) & 0x1;
378           table[i].Save_MRP_in_frame = (tmp >> 2) & 0x1;
379           table[i].extn_ptr_defined = (tmp >> 1) & 0x1;
380           table[i].Cleanup_defined = tmp & 0x1;
381           tmp = bfd_get_32 (objfile->obfd, (bfd_byte *) buf);
382           buf += 4;
383           table[i].MPE_XL_interrupt_marker = (tmp >> 31) & 0x1;
384           table[i].HP_UX_interrupt_marker = (tmp >> 30) & 0x1;
385           table[i].Large_frame = (tmp >> 29) & 0x1;
386           table[i].Pseudo_SP_Set = (tmp >> 28) & 0x1;
387           table[i].reserved4 = (tmp >> 27) & 0x1;
388           table[i].Total_frame_size = tmp & 0x7ffffff;
389
390           /* Stub unwinds are handled elsewhere. */
391           table[i].stub_unwind.stub_type = 0;
392           table[i].stub_unwind.padding = 0;
393         }
394     }
395 }
396
397 /* Read in the backtrace information stored in the `$UNWIND_START$' section of
398    the object file.  This info is used mainly by find_unwind_entry() to find
399    out the stack frame size and frame pointer used by procedures.  We put
400    everything on the psymbol obstack in the objfile so that it automatically
401    gets freed when the objfile is destroyed.  */
402
403 static void
404 read_unwind_info (struct objfile *objfile)
405 {
406   asection *unwind_sec, *stub_unwind_sec;
407   unsigned unwind_size, stub_unwind_size, total_size;
408   unsigned index, unwind_entries;
409   unsigned stub_entries, total_entries;
410   CORE_ADDR text_offset;
411   struct hppa_unwind_info *ui;
412   struct hppa_objfile_private *obj_private;
413
414   text_offset = ANOFFSET (objfile->section_offsets, 0);
415   ui = (struct hppa_unwind_info *) obstack_alloc (&objfile->objfile_obstack,
416                                            sizeof (struct hppa_unwind_info));
417
418   ui->table = NULL;
419   ui->cache = NULL;
420   ui->last = -1;
421
422   /* For reasons unknown the HP PA64 tools generate multiple unwinder
423      sections in a single executable.  So we just iterate over every
424      section in the BFD looking for unwinder sections intead of trying
425      to do a lookup with bfd_get_section_by_name. 
426
427      First determine the total size of the unwind tables so that we
428      can allocate memory in a nice big hunk.  */
429   total_entries = 0;
430   for (unwind_sec = objfile->obfd->sections;
431        unwind_sec;
432        unwind_sec = unwind_sec->next)
433     {
434       if (strcmp (unwind_sec->name, "$UNWIND_START$") == 0
435           || strcmp (unwind_sec->name, ".PARISC.unwind") == 0)
436         {
437           unwind_size = bfd_section_size (objfile->obfd, unwind_sec);
438           unwind_entries = unwind_size / UNWIND_ENTRY_SIZE;
439
440           total_entries += unwind_entries;
441         }
442     }
443
444   /* Now compute the size of the stub unwinds.  Note the ELF tools do not
445      use stub unwinds at the curren time.  */
446   stub_unwind_sec = bfd_get_section_by_name (objfile->obfd, "$UNWIND_END$");
447
448   if (stub_unwind_sec)
449     {
450       stub_unwind_size = bfd_section_size (objfile->obfd, stub_unwind_sec);
451       stub_entries = stub_unwind_size / STUB_UNWIND_ENTRY_SIZE;
452     }
453   else
454     {
455       stub_unwind_size = 0;
456       stub_entries = 0;
457     }
458
459   /* Compute total number of unwind entries and their total size.  */
460   total_entries += stub_entries;
461   total_size = total_entries * sizeof (struct unwind_table_entry);
462
463   /* Allocate memory for the unwind table.  */
464   ui->table = (struct unwind_table_entry *)
465     obstack_alloc (&objfile->objfile_obstack, total_size);
466   ui->last = total_entries - 1;
467
468   /* Now read in each unwind section and internalize the standard unwind
469      entries.  */
470   index = 0;
471   for (unwind_sec = objfile->obfd->sections;
472        unwind_sec;
473        unwind_sec = unwind_sec->next)
474     {
475       if (strcmp (unwind_sec->name, "$UNWIND_START$") == 0
476           || strcmp (unwind_sec->name, ".PARISC.unwind") == 0)
477         {
478           unwind_size = bfd_section_size (objfile->obfd, unwind_sec);
479           unwind_entries = unwind_size / UNWIND_ENTRY_SIZE;
480
481           internalize_unwinds (objfile, &ui->table[index], unwind_sec,
482                                unwind_entries, unwind_size, text_offset);
483           index += unwind_entries;
484         }
485     }
486
487   /* Now read in and internalize the stub unwind entries.  */
488   if (stub_unwind_size > 0)
489     {
490       unsigned int i;
491       char *buf = alloca (stub_unwind_size);
492
493       /* Read in the stub unwind entries.  */
494       bfd_get_section_contents (objfile->obfd, stub_unwind_sec, buf,
495                                 0, stub_unwind_size);
496
497       /* Now convert them into regular unwind entries.  */
498       for (i = 0; i < stub_entries; i++, index++)
499         {
500           /* Clear out the next unwind entry.  */
501           memset (&ui->table[index], 0, sizeof (struct unwind_table_entry));
502
503           /* Convert offset & size into region_start and region_end.  
504              Stuff away the stub type into "reserved" fields.  */
505           ui->table[index].region_start = bfd_get_32 (objfile->obfd,
506                                                       (bfd_byte *) buf);
507           ui->table[index].region_start += text_offset;
508           buf += 4;
509           ui->table[index].stub_unwind.stub_type = bfd_get_8 (objfile->obfd,
510                                                           (bfd_byte *) buf);
511           buf += 2;
512           ui->table[index].region_end
513             = ui->table[index].region_start + 4 *
514             (bfd_get_16 (objfile->obfd, (bfd_byte *) buf) - 1);
515           buf += 2;
516         }
517
518     }
519
520   /* Unwind table needs to be kept sorted.  */
521   qsort (ui->table, total_entries, sizeof (struct unwind_table_entry),
522          compare_unwind_entries);
523
524   /* Keep a pointer to the unwind information.  */
525   obj_private = (struct hppa_objfile_private *) 
526                 objfile_data (objfile, hppa_objfile_priv_data);
527   if (obj_private == NULL)
528     {
529       obj_private = (struct hppa_objfile_private *)
530         obstack_alloc (&objfile->objfile_obstack, 
531                        sizeof (struct hppa_objfile_private));
532       set_objfile_data (objfile, hppa_objfile_priv_data, obj_private);
533       obj_private->unwind_info = NULL;
534       obj_private->so_info = NULL;
535       obj_private->dp = 0;
536     }
537   obj_private->unwind_info = ui;
538 }
539
540 /* Lookup the unwind (stack backtrace) info for the given PC.  We search all
541    of the objfiles seeking the unwind table entry for this PC.  Each objfile
542    contains a sorted list of struct unwind_table_entry.  Since we do a binary
543    search of the unwind tables, we depend upon them to be sorted.  */
544
545 struct unwind_table_entry *
546 find_unwind_entry (CORE_ADDR pc)
547 {
548   int first, middle, last;
549   struct objfile *objfile;
550   struct hppa_objfile_private *priv;
551
552   if (hppa_debug)
553     fprintf_unfiltered (gdb_stdlog, "{ find_unwind_entry 0x%s -> ",
554                         paddr_nz (pc));
555
556   /* A function at address 0?  Not in HP-UX! */
557   if (pc == (CORE_ADDR) 0)
558     {
559       if (hppa_debug)
560         fprintf_unfiltered (gdb_stdlog, "NULL }\n");
561       return NULL;
562     }
563
564   ALL_OBJFILES (objfile)
565   {
566     struct hppa_unwind_info *ui;
567     ui = NULL;
568     priv = objfile_data (objfile, hppa_objfile_priv_data);
569     if (priv)
570       ui = ((struct hppa_objfile_private *) priv)->unwind_info;
571
572     if (!ui)
573       {
574         read_unwind_info (objfile);
575         priv = objfile_data (objfile, hppa_objfile_priv_data);
576         if (priv == NULL)
577           error ("Internal error reading unwind information.");
578         ui = ((struct hppa_objfile_private *) priv)->unwind_info;
579       }
580
581     /* First, check the cache */
582
583     if (ui->cache
584         && pc >= ui->cache->region_start
585         && pc <= ui->cache->region_end)
586       {
587         if (hppa_debug)
588           fprintf_unfiltered (gdb_stdlog, "0x%s (cached) }\n",
589             paddr_nz ((CORE_ADDR) ui->cache));
590         return ui->cache;
591       }
592
593     /* Not in the cache, do a binary search */
594
595     first = 0;
596     last = ui->last;
597
598     while (first <= last)
599       {
600         middle = (first + last) / 2;
601         if (pc >= ui->table[middle].region_start
602             && pc <= ui->table[middle].region_end)
603           {
604             ui->cache = &ui->table[middle];
605             if (hppa_debug)
606               fprintf_unfiltered (gdb_stdlog, "0x%s }\n",
607                 paddr_nz ((CORE_ADDR) ui->cache));
608             return &ui->table[middle];
609           }
610
611         if (pc < ui->table[middle].region_start)
612           last = middle - 1;
613         else
614           first = middle + 1;
615       }
616   }                             /* ALL_OBJFILES() */
617
618   if (hppa_debug)
619     fprintf_unfiltered (gdb_stdlog, "NULL (not found) }\n");
620
621   return NULL;
622 }
623
624 static const unsigned char *
625 hppa_breakpoint_from_pc (CORE_ADDR *pc, int *len)
626 {
627   static const unsigned char breakpoint[] = {0x00, 0x01, 0x00, 0x04};
628   (*len) = sizeof (breakpoint);
629   return breakpoint;
630 }
631
632 /* Return the name of a register.  */
633
634 static const char *
635 hppa32_register_name (int i)
636 {
637   static char *names[] = {
638     "flags",  "r1",      "rp",     "r3",
639     "r4",     "r5",      "r6",     "r7",
640     "r8",     "r9",      "r10",    "r11",
641     "r12",    "r13",     "r14",    "r15",
642     "r16",    "r17",     "r18",    "r19",
643     "r20",    "r21",     "r22",    "r23",
644     "r24",    "r25",     "r26",    "dp",
645     "ret0",   "ret1",    "sp",     "r31",
646     "sar",    "pcoqh",   "pcsqh",  "pcoqt",
647     "pcsqt",  "eiem",    "iir",    "isr",
648     "ior",    "ipsw",    "goto",   "sr4",
649     "sr0",    "sr1",     "sr2",    "sr3",
650     "sr5",    "sr6",     "sr7",    "cr0",
651     "cr8",    "cr9",     "ccr",    "cr12",
652     "cr13",   "cr24",    "cr25",   "cr26",
653     "mpsfu_high","mpsfu_low","mpsfu_ovflo","pad",
654     "fpsr",    "fpe1",   "fpe2",   "fpe3",
655     "fpe4",   "fpe5",    "fpe6",   "fpe7",
656     "fr4",     "fr4R",   "fr5",    "fr5R",
657     "fr6",    "fr6R",    "fr7",    "fr7R",
658     "fr8",     "fr8R",   "fr9",    "fr9R",
659     "fr10",   "fr10R",   "fr11",   "fr11R",
660     "fr12",    "fr12R",  "fr13",   "fr13R",
661     "fr14",   "fr14R",   "fr15",   "fr15R",
662     "fr16",    "fr16R",  "fr17",   "fr17R",
663     "fr18",   "fr18R",   "fr19",   "fr19R",
664     "fr20",    "fr20R",  "fr21",   "fr21R",
665     "fr22",   "fr22R",   "fr23",   "fr23R",
666     "fr24",    "fr24R",  "fr25",   "fr25R",
667     "fr26",   "fr26R",   "fr27",   "fr27R",
668     "fr28",    "fr28R",  "fr29",   "fr29R",
669     "fr30",   "fr30R",   "fr31",   "fr31R"
670   };
671   if (i < 0 || i >= (sizeof (names) / sizeof (*names)))
672     return NULL;
673   else
674     return names[i];
675 }
676
677 static const char *
678 hppa64_register_name (int i)
679 {
680   static char *names[] = {
681     "flags",  "r1",      "rp",     "r3",
682     "r4",     "r5",      "r6",     "r7",
683     "r8",     "r9",      "r10",    "r11",
684     "r12",    "r13",     "r14",    "r15",
685     "r16",    "r17",     "r18",    "r19",
686     "r20",    "r21",     "r22",    "r23",
687     "r24",    "r25",     "r26",    "dp",
688     "ret0",   "ret1",    "sp",     "r31",
689     "sar",    "pcoqh",   "pcsqh",  "pcoqt",
690     "pcsqt",  "eiem",    "iir",    "isr",
691     "ior",    "ipsw",    "goto",   "sr4",
692     "sr0",    "sr1",     "sr2",    "sr3",
693     "sr5",    "sr6",     "sr7",    "cr0",
694     "cr8",    "cr9",     "ccr",    "cr12",
695     "cr13",   "cr24",    "cr25",   "cr26",
696     "mpsfu_high","mpsfu_low","mpsfu_ovflo","pad",
697     "fpsr",    "fpe1",   "fpe2",   "fpe3",
698     "fr4",    "fr5",     "fr6",    "fr7",
699     "fr8",     "fr9",    "fr10",   "fr11",
700     "fr12",   "fr13",    "fr14",   "fr15",
701     "fr16",    "fr17",   "fr18",   "fr19",
702     "fr20",   "fr21",    "fr22",   "fr23",
703     "fr24",    "fr25",   "fr26",   "fr27",
704     "fr28",  "fr29",    "fr30",   "fr31"
705   };
706   if (i < 0 || i >= (sizeof (names) / sizeof (*names)))
707     return NULL;
708   else
709     return names[i];
710 }
711
712 /* This function pushes a stack frame with arguments as part of the
713    inferior function calling mechanism.
714
715    This is the version of the function for the 32-bit PA machines, in
716    which later arguments appear at lower addresses.  (The stack always
717    grows towards higher addresses.)
718
719    We simply allocate the appropriate amount of stack space and put
720    arguments into their proper slots.  */
721    
722 static CORE_ADDR
723 hppa32_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
724                         struct regcache *regcache, CORE_ADDR bp_addr,
725                         int nargs, struct value **args, CORE_ADDR sp,
726                         int struct_return, CORE_ADDR struct_addr)
727 {
728   /* Stack base address at which any pass-by-reference parameters are
729      stored.  */
730   CORE_ADDR struct_end = 0;
731   /* Stack base address at which the first parameter is stored.  */
732   CORE_ADDR param_end = 0;
733
734   /* The inner most end of the stack after all the parameters have
735      been pushed.  */
736   CORE_ADDR new_sp = 0;
737
738   /* Two passes.  First pass computes the location of everything,
739      second pass writes the bytes out.  */
740   int write_pass;
741
742   /* Global pointer (r19) of the function we are trying to call.  */
743   CORE_ADDR gp;
744
745   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
746
747   for (write_pass = 0; write_pass < 2; write_pass++)
748     {
749       CORE_ADDR struct_ptr = 0;
750       /* The first parameter goes into sp-36, each stack slot is 4-bytes.  
751          struct_ptr is adjusted for each argument below, so the first
752          argument will end up at sp-36.  */
753       CORE_ADDR param_ptr = 32;
754       int i;
755       int small_struct = 0;
756
757       for (i = 0; i < nargs; i++)
758         {
759           struct value *arg = args[i];
760           struct type *type = check_typedef (value_type (arg));
761           /* The corresponding parameter that is pushed onto the
762              stack, and [possibly] passed in a register.  */
763           char param_val[8];
764           int param_len;
765           memset (param_val, 0, sizeof param_val);
766           if (TYPE_LENGTH (type) > 8)
767             {
768               /* Large parameter, pass by reference.  Store the value
769                  in "struct" area and then pass its address.  */
770               param_len = 4;
771               struct_ptr += align_up (TYPE_LENGTH (type), 8);
772               if (write_pass)
773                 write_memory (struct_end - struct_ptr, VALUE_CONTENTS (arg),
774                               TYPE_LENGTH (type));
775               store_unsigned_integer (param_val, 4, struct_end - struct_ptr);
776             }
777           else if (TYPE_CODE (type) == TYPE_CODE_INT
778                    || TYPE_CODE (type) == TYPE_CODE_ENUM)
779             {
780               /* Integer value store, right aligned.  "unpack_long"
781                  takes care of any sign-extension problems.  */
782               param_len = align_up (TYPE_LENGTH (type), 4);
783               store_unsigned_integer (param_val, param_len,
784                                       unpack_long (type,
785                                                    VALUE_CONTENTS (arg)));
786             }
787           else if (TYPE_CODE (type) == TYPE_CODE_FLT)
788             {
789               /* Floating point value store, right aligned.  */
790               param_len = align_up (TYPE_LENGTH (type), 4);
791               memcpy (param_val, VALUE_CONTENTS (arg), param_len);
792             }
793           else
794             {
795               param_len = align_up (TYPE_LENGTH (type), 4);
796
797               /* Small struct value are stored right-aligned.  */
798               memcpy (param_val + param_len - TYPE_LENGTH (type),
799                       VALUE_CONTENTS (arg), TYPE_LENGTH (type));
800
801               /* Structures of size 5, 6 and 7 bytes are special in that
802                  the higher-ordered word is stored in the lower-ordered
803                  argument, and even though it is a 8-byte quantity the
804                  registers need not be 8-byte aligned.  */
805               if (param_len > 4 && param_len < 8)
806                 small_struct = 1;
807             }
808
809           param_ptr += param_len;
810           if (param_len == 8 && !small_struct)
811             param_ptr = align_up (param_ptr, 8);
812
813           /* First 4 non-FP arguments are passed in gr26-gr23.
814              First 4 32-bit FP arguments are passed in fr4L-fr7L.
815              First 2 64-bit FP arguments are passed in fr5 and fr7.
816
817              The rest go on the stack, starting at sp-36, towards lower
818              addresses.  8-byte arguments must be aligned to a 8-byte
819              stack boundary.  */
820           if (write_pass)
821             {
822               write_memory (param_end - param_ptr, param_val, param_len);
823
824               /* There are some cases when we don't know the type
825                  expected by the callee (e.g. for variadic functions), so 
826                  pass the parameters in both general and fp regs.  */
827               if (param_ptr <= 48)
828                 {
829                   int grreg = 26 - (param_ptr - 36) / 4;
830                   int fpLreg = 72 + (param_ptr - 36) / 4 * 2;
831                   int fpreg = 74 + (param_ptr - 32) / 8 * 4;
832
833                   regcache_cooked_write (regcache, grreg, param_val);
834                   regcache_cooked_write (regcache, fpLreg, param_val);
835
836                   if (param_len > 4)
837                     {
838                       regcache_cooked_write (regcache, grreg + 1, 
839                                              param_val + 4);
840
841                       regcache_cooked_write (regcache, fpreg, param_val);
842                       regcache_cooked_write (regcache, fpreg + 1, 
843                                              param_val + 4);
844                     }
845                 }
846             }
847         }
848
849       /* Update the various stack pointers.  */
850       if (!write_pass)
851         {
852           struct_end = sp + align_up (struct_ptr, 64);
853           /* PARAM_PTR already accounts for all the arguments passed
854              by the user.  However, the ABI mandates minimum stack
855              space allocations for outgoing arguments.  The ABI also
856              mandates minimum stack alignments which we must
857              preserve.  */
858           param_end = struct_end + align_up (param_ptr, 64);
859         }
860     }
861
862   /* If a structure has to be returned, set up register 28 to hold its
863      address */
864   if (struct_return)
865     write_register (28, struct_addr);
866
867   gp = tdep->find_global_pointer (function);
868
869   if (gp != 0)
870     write_register (19, gp);
871
872   /* Set the return address.  */
873   regcache_cooked_write_unsigned (regcache, HPPA_RP_REGNUM, bp_addr);
874
875   /* Update the Stack Pointer.  */
876   regcache_cooked_write_unsigned (regcache, HPPA_SP_REGNUM, param_end);
877
878   return param_end;
879 }
880
881 /* This function pushes a stack frame with arguments as part of the
882    inferior function calling mechanism.
883
884    This is the version for the PA64, in which later arguments appear
885    at higher addresses.  (The stack always grows towards higher
886    addresses.)
887
888    We simply allocate the appropriate amount of stack space and put
889    arguments into their proper slots.
890
891    This ABI also requires that the caller provide an argument pointer
892    to the callee, so we do that too.  */
893    
894 static CORE_ADDR
895 hppa64_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
896                         struct regcache *regcache, CORE_ADDR bp_addr,
897                         int nargs, struct value **args, CORE_ADDR sp,
898                         int struct_return, CORE_ADDR struct_addr)
899 {
900   /* NOTE: cagney/2004-02-27: This is a guess - its implemented by
901      reverse engineering testsuite failures.  */
902
903   /* Stack base address at which any pass-by-reference parameters are
904      stored.  */
905   CORE_ADDR struct_end = 0;
906   /* Stack base address at which the first parameter is stored.  */
907   CORE_ADDR param_end = 0;
908
909   /* The inner most end of the stack after all the parameters have
910      been pushed.  */
911   CORE_ADDR new_sp = 0;
912
913   /* Two passes.  First pass computes the location of everything,
914      second pass writes the bytes out.  */
915   int write_pass;
916   for (write_pass = 0; write_pass < 2; write_pass++)
917     {
918       CORE_ADDR struct_ptr = 0;
919       CORE_ADDR param_ptr = 0;
920       int i;
921       for (i = 0; i < nargs; i++)
922         {
923           struct value *arg = args[i];
924           struct type *type = check_typedef (value_type (arg));
925           if ((TYPE_CODE (type) == TYPE_CODE_INT
926                || TYPE_CODE (type) == TYPE_CODE_ENUM)
927               && TYPE_LENGTH (type) <= 8)
928             {
929               /* Integer value store, right aligned.  "unpack_long"
930                  takes care of any sign-extension problems.  */
931               param_ptr += 8;
932               if (write_pass)
933                 {
934                   ULONGEST val = unpack_long (type, VALUE_CONTENTS (arg));
935                   int reg = 27 - param_ptr / 8;
936                   write_memory_unsigned_integer (param_end - param_ptr,
937                                                  val, 8);
938                   if (reg >= 19)
939                     regcache_cooked_write_unsigned (regcache, reg, val);
940                 }
941             }
942           else
943             {
944               /* Small struct value, store left aligned?  */
945               int reg;
946               if (TYPE_LENGTH (type) > 8)
947                 {
948                   param_ptr = align_up (param_ptr, 16);
949                   reg = 26 - param_ptr / 8;
950                   param_ptr += align_up (TYPE_LENGTH (type), 16);
951                 }
952               else
953                 {
954                   param_ptr = align_up (param_ptr, 8);
955                   reg = 26 - param_ptr / 8;
956                   param_ptr += align_up (TYPE_LENGTH (type), 8);
957                 }
958               if (write_pass)
959                 {
960                   int byte;
961                   write_memory (param_end - param_ptr, VALUE_CONTENTS (arg),
962                                 TYPE_LENGTH (type));
963                   for (byte = 0; byte < TYPE_LENGTH (type); byte += 8)
964                     {
965                       if (reg >= 19)
966                         {
967                           int len = min (8, TYPE_LENGTH (type) - byte);
968                           regcache_cooked_write_part (regcache, reg, 0, len,
969                                                       VALUE_CONTENTS (arg) + byte);
970                         }
971                       reg--;
972                     }
973                 }
974             }
975         }
976       /* Update the various stack pointers.  */
977       if (!write_pass)
978         {
979           struct_end = sp + struct_ptr;
980           /* PARAM_PTR already accounts for all the arguments passed
981              by the user.  However, the ABI mandates minimum stack
982              space allocations for outgoing arguments.  The ABI also
983              mandates minimum stack alignments which we must
984              preserve.  */
985           param_end = struct_end + max (align_up (param_ptr, 16), 64);
986         }
987     }
988
989   /* If a structure has to be returned, set up register 28 to hold its
990      address */
991   if (struct_return)
992     write_register (28, struct_addr);
993
994   /* Set the return address.  */
995   regcache_cooked_write_unsigned (regcache, HPPA_RP_REGNUM, bp_addr);
996
997   /* Update the Stack Pointer.  */
998   regcache_cooked_write_unsigned (regcache, HPPA_SP_REGNUM, param_end + 64);
999
1000   /* The stack will have 32 bytes of additional space for a frame marker.  */
1001   return param_end + 64;
1002 }
1003
1004 static CORE_ADDR
1005 hppa32_convert_from_func_ptr_addr (struct gdbarch *gdbarch,
1006                                    CORE_ADDR addr,
1007                                    struct target_ops *targ)
1008 {
1009   if (addr & 2)
1010     {
1011       CORE_ADDR plabel;
1012
1013       plabel = addr & ~3;
1014       target_read_memory(plabel, (char *)&addr, 4);
1015     }
1016
1017   return addr;
1018 }
1019
1020 static CORE_ADDR
1021 hppa32_frame_align (struct gdbarch *gdbarch, CORE_ADDR addr)
1022 {
1023   /* HP frames are 64-byte (or cache line) aligned (yes that's _byte_
1024      and not _bit_)!  */
1025   return align_up (addr, 64);
1026 }
1027
1028 /* Force all frames to 16-byte alignment.  Better safe than sorry.  */
1029
1030 static CORE_ADDR
1031 hppa64_frame_align (struct gdbarch *gdbarch, CORE_ADDR addr)
1032 {
1033   /* Just always 16-byte align.  */
1034   return align_up (addr, 16);
1035 }
1036
1037
1038 /* Get the PC from %r31 if currently in a syscall.  Also mask out privilege
1039    bits.  */
1040
1041 static CORE_ADDR
1042 hppa_target_read_pc (ptid_t ptid)
1043 {
1044   int flags = read_register_pid (HPPA_FLAGS_REGNUM, ptid);
1045   ULONGEST ipsw = read_register_pid (HPPA_IPSW_REGNUM, ptid);
1046   CORE_ADDR pc;
1047
1048   /* The following test does not belong here.  It is OS-specific, and belongs
1049      in native code.  */
1050   /* Test SS_INSYSCALL */
1051   if (flags & 2)
1052     return read_register_pid (31, ptid) & ~0x3;
1053
1054   pc = read_register_pid (HPPA_PCOQ_HEAD_REGNUM, ptid) & ~0x3;
1055
1056   /* If the current instruction is nullified, then we are effectively
1057      still executing the previous instruction.  Pretend we are still
1058      there.  This is needed when single stepping; if the nullified instruction
1059      is on a different line, we don't want gdb to think we've stepped onto
1060      that line.  */
1061   if (ipsw & 0x00200000)
1062     pc -= 4;
1063
1064   return pc;
1065 }
1066
1067 /* Write out the PC.  If currently in a syscall, then also write the new
1068    PC value into %r31.  */
1069
1070 static void
1071 hppa_target_write_pc (CORE_ADDR v, ptid_t ptid)
1072 {
1073   int flags = read_register_pid (HPPA_FLAGS_REGNUM, ptid);
1074
1075   /* The following test does not belong here.  It is OS-specific, and belongs
1076      in native code.  */
1077   /* If in a syscall, then set %r31.  Also make sure to get the 
1078      privilege bits set correctly.  */
1079   /* Test SS_INSYSCALL */
1080   if (flags & 2)
1081     write_register_pid (31, v | 0x3, ptid);
1082
1083   write_register_pid (HPPA_PCOQ_HEAD_REGNUM, v, ptid);
1084   write_register_pid (HPPA_PCOQ_TAIL_REGNUM, v + 4, ptid);
1085 }
1086
1087 /* return the alignment of a type in bytes. Structures have the maximum
1088    alignment required by their fields. */
1089
1090 static int
1091 hppa_alignof (struct type *type)
1092 {
1093   int max_align, align, i;
1094   CHECK_TYPEDEF (type);
1095   switch (TYPE_CODE (type))
1096     {
1097     case TYPE_CODE_PTR:
1098     case TYPE_CODE_INT:
1099     case TYPE_CODE_FLT:
1100       return TYPE_LENGTH (type);
1101     case TYPE_CODE_ARRAY:
1102       return hppa_alignof (TYPE_FIELD_TYPE (type, 0));
1103     case TYPE_CODE_STRUCT:
1104     case TYPE_CODE_UNION:
1105       max_align = 1;
1106       for (i = 0; i < TYPE_NFIELDS (type); i++)
1107         {
1108           /* Bit fields have no real alignment. */
1109           /* if (!TYPE_FIELD_BITPOS (type, i)) */
1110           if (!TYPE_FIELD_BITSIZE (type, i))    /* elz: this should be bitsize */
1111             {
1112               align = hppa_alignof (TYPE_FIELD_TYPE (type, i));
1113               max_align = max (max_align, align);
1114             }
1115         }
1116       return max_align;
1117     default:
1118       return 4;
1119     }
1120 }
1121
1122 /* For the given instruction (INST), return any adjustment it makes
1123    to the stack pointer or zero for no adjustment. 
1124
1125    This only handles instructions commonly found in prologues.  */
1126
1127 static int
1128 prologue_inst_adjust_sp (unsigned long inst)
1129 {
1130   /* This must persist across calls.  */
1131   static int save_high21;
1132
1133   /* The most common way to perform a stack adjustment ldo X(sp),sp */
1134   if ((inst & 0xffffc000) == 0x37de0000)
1135     return hppa_extract_14 (inst);
1136
1137   /* stwm X,D(sp) */
1138   if ((inst & 0xffe00000) == 0x6fc00000)
1139     return hppa_extract_14 (inst);
1140
1141   /* std,ma X,D(sp) */
1142   if ((inst & 0xffe00008) == 0x73c00008)
1143     return (inst & 0x1 ? -1 << 13 : 0) | (((inst >> 4) & 0x3ff) << 3);
1144
1145   /* addil high21,%r1; ldo low11,(%r1),%r30)
1146      save high bits in save_high21 for later use.  */
1147   if ((inst & 0xffe00000) == 0x28200000)
1148     {
1149       save_high21 = hppa_extract_21 (inst);
1150       return 0;
1151     }
1152
1153   if ((inst & 0xffff0000) == 0x343e0000)
1154     return save_high21 + hppa_extract_14 (inst);
1155
1156   /* fstws as used by the HP compilers.  */
1157   if ((inst & 0xffffffe0) == 0x2fd01220)
1158     return hppa_extract_5_load (inst);
1159
1160   /* No adjustment.  */
1161   return 0;
1162 }
1163
1164 /* Return nonzero if INST is a branch of some kind, else return zero.  */
1165
1166 static int
1167 is_branch (unsigned long inst)
1168 {
1169   switch (inst >> 26)
1170     {
1171     case 0x20:
1172     case 0x21:
1173     case 0x22:
1174     case 0x23:
1175     case 0x27:
1176     case 0x28:
1177     case 0x29:
1178     case 0x2a:
1179     case 0x2b:
1180     case 0x2f:
1181     case 0x30:
1182     case 0x31:
1183     case 0x32:
1184     case 0x33:
1185     case 0x38:
1186     case 0x39:
1187     case 0x3a:
1188     case 0x3b:
1189       return 1;
1190
1191     default:
1192       return 0;
1193     }
1194 }
1195
1196 /* Return the register number for a GR which is saved by INST or
1197    zero it INST does not save a GR.  */
1198
1199 static int
1200 inst_saves_gr (unsigned long inst)
1201 {
1202   /* Does it look like a stw?  */
1203   if ((inst >> 26) == 0x1a || (inst >> 26) == 0x1b
1204       || (inst >> 26) == 0x1f
1205       || ((inst >> 26) == 0x1f
1206           && ((inst >> 6) == 0xa)))
1207     return hppa_extract_5R_store (inst);
1208
1209   /* Does it look like a std?  */
1210   if ((inst >> 26) == 0x1c
1211       || ((inst >> 26) == 0x03
1212           && ((inst >> 6) & 0xf) == 0xb))
1213     return hppa_extract_5R_store (inst);
1214
1215   /* Does it look like a stwm?  GCC & HPC may use this in prologues. */
1216   if ((inst >> 26) == 0x1b)
1217     return hppa_extract_5R_store (inst);
1218
1219   /* Does it look like sth or stb?  HPC versions 9.0 and later use these
1220      too.  */
1221   if ((inst >> 26) == 0x19 || (inst >> 26) == 0x18
1222       || ((inst >> 26) == 0x3
1223           && (((inst >> 6) & 0xf) == 0x8
1224               || (inst >> 6) & 0xf) == 0x9))
1225     return hppa_extract_5R_store (inst);
1226
1227   return 0;
1228 }
1229
1230 /* Return the register number for a FR which is saved by INST or
1231    zero it INST does not save a FR.
1232
1233    Note we only care about full 64bit register stores (that's the only
1234    kind of stores the prologue will use).
1235
1236    FIXME: What about argument stores with the HP compiler in ANSI mode? */
1237
1238 static int
1239 inst_saves_fr (unsigned long inst)
1240 {
1241   /* is this an FSTD ? */
1242   if ((inst & 0xfc00dfc0) == 0x2c001200)
1243     return hppa_extract_5r_store (inst);
1244   if ((inst & 0xfc000002) == 0x70000002)
1245     return hppa_extract_5R_store (inst);
1246   /* is this an FSTW ? */
1247   if ((inst & 0xfc00df80) == 0x24001200)
1248     return hppa_extract_5r_store (inst);
1249   if ((inst & 0xfc000002) == 0x7c000000)
1250     return hppa_extract_5R_store (inst);
1251   return 0;
1252 }
1253
1254 /* Advance PC across any function entry prologue instructions
1255    to reach some "real" code. 
1256
1257    Use information in the unwind table to determine what exactly should
1258    be in the prologue.  */
1259
1260
1261 static CORE_ADDR
1262 skip_prologue_hard_way (CORE_ADDR pc, int stop_before_branch)
1263 {
1264   char buf[4];
1265   CORE_ADDR orig_pc = pc;
1266   unsigned long inst, stack_remaining, save_gr, save_fr, save_rp, save_sp;
1267   unsigned long args_stored, status, i, restart_gr, restart_fr;
1268   struct unwind_table_entry *u;
1269   int final_iteration;
1270
1271   restart_gr = 0;
1272   restart_fr = 0;
1273
1274 restart:
1275   u = find_unwind_entry (pc);
1276   if (!u)
1277     return pc;
1278
1279   /* If we are not at the beginning of a function, then return now. */
1280   if ((pc & ~0x3) != u->region_start)
1281     return pc;
1282
1283   /* This is how much of a frame adjustment we need to account for.  */
1284   stack_remaining = u->Total_frame_size << 3;
1285
1286   /* Magic register saves we want to know about.  */
1287   save_rp = u->Save_RP;
1288   save_sp = u->Save_SP;
1289
1290   /* An indication that args may be stored into the stack.  Unfortunately
1291      the HPUX compilers tend to set this in cases where no args were
1292      stored too!.  */
1293   args_stored = 1;
1294
1295   /* Turn the Entry_GR field into a bitmask.  */
1296   save_gr = 0;
1297   for (i = 3; i < u->Entry_GR + 3; i++)
1298     {
1299       /* Frame pointer gets saved into a special location.  */
1300       if (u->Save_SP && i == HPPA_FP_REGNUM)
1301         continue;
1302
1303       save_gr |= (1 << i);
1304     }
1305   save_gr &= ~restart_gr;
1306
1307   /* Turn the Entry_FR field into a bitmask too.  */
1308   save_fr = 0;
1309   for (i = 12; i < u->Entry_FR + 12; i++)
1310     save_fr |= (1 << i);
1311   save_fr &= ~restart_fr;
1312
1313   final_iteration = 0;
1314
1315   /* Loop until we find everything of interest or hit a branch.
1316
1317      For unoptimized GCC code and for any HP CC code this will never ever
1318      examine any user instructions.
1319
1320      For optimzied GCC code we're faced with problems.  GCC will schedule
1321      its prologue and make prologue instructions available for delay slot
1322      filling.  The end result is user code gets mixed in with the prologue
1323      and a prologue instruction may be in the delay slot of the first branch
1324      or call.
1325
1326      Some unexpected things are expected with debugging optimized code, so
1327      we allow this routine to walk past user instructions in optimized
1328      GCC code.  */
1329   while (save_gr || save_fr || save_rp || save_sp || stack_remaining > 0
1330          || args_stored)
1331     {
1332       unsigned int reg_num;
1333       unsigned long old_stack_remaining, old_save_gr, old_save_fr;
1334       unsigned long old_save_rp, old_save_sp, next_inst;
1335
1336       /* Save copies of all the triggers so we can compare them later
1337          (only for HPC).  */
1338       old_save_gr = save_gr;
1339       old_save_fr = save_fr;
1340       old_save_rp = save_rp;
1341       old_save_sp = save_sp;
1342       old_stack_remaining = stack_remaining;
1343
1344       status = deprecated_read_memory_nobpt (pc, buf, 4);
1345       inst = extract_unsigned_integer (buf, 4);
1346
1347       /* Yow! */
1348       if (status != 0)
1349         return pc;
1350
1351       /* Note the interesting effects of this instruction.  */
1352       stack_remaining -= prologue_inst_adjust_sp (inst);
1353
1354       /* There are limited ways to store the return pointer into the
1355          stack.  */
1356       if (inst == 0x6bc23fd9 || inst == 0x0fc212c1)
1357         save_rp = 0;
1358
1359       /* These are the only ways we save SP into the stack.  At this time
1360          the HP compilers never bother to save SP into the stack.  */
1361       if ((inst & 0xffffc000) == 0x6fc10000
1362           || (inst & 0xffffc00c) == 0x73c10008)
1363         save_sp = 0;
1364
1365       /* Are we loading some register with an offset from the argument
1366          pointer?  */
1367       if ((inst & 0xffe00000) == 0x37a00000
1368           || (inst & 0xffffffe0) == 0x081d0240)
1369         {
1370           pc += 4;
1371           continue;
1372         }
1373
1374       /* Account for general and floating-point register saves.  */
1375       reg_num = inst_saves_gr (inst);
1376       save_gr &= ~(1 << reg_num);
1377
1378       /* Ugh.  Also account for argument stores into the stack.
1379          Unfortunately args_stored only tells us that some arguments
1380          where stored into the stack.  Not how many or what kind!
1381
1382          This is a kludge as on the HP compiler sets this bit and it
1383          never does prologue scheduling.  So once we see one, skip past
1384          all of them.   We have similar code for the fp arg stores below.
1385
1386          FIXME.  Can still die if we have a mix of GR and FR argument
1387          stores!  */
1388       if (reg_num >= (TARGET_PTR_BIT == 64 ? 19 : 23) && reg_num <= 26)
1389         {
1390           while (reg_num >= (TARGET_PTR_BIT == 64 ? 19 : 23) && reg_num <= 26)
1391             {
1392               pc += 4;
1393               status = deprecated_read_memory_nobpt (pc, buf, 4);
1394               inst = extract_unsigned_integer (buf, 4);
1395               if (status != 0)
1396                 return pc;
1397               reg_num = inst_saves_gr (inst);
1398             }
1399           args_stored = 0;
1400           continue;
1401         }
1402
1403       reg_num = inst_saves_fr (inst);
1404       save_fr &= ~(1 << reg_num);
1405
1406       status = deprecated_read_memory_nobpt (pc + 4, buf, 4);
1407       next_inst = extract_unsigned_integer (buf, 4);
1408
1409       /* Yow! */
1410       if (status != 0)
1411         return pc;
1412
1413       /* We've got to be read to handle the ldo before the fp register
1414          save.  */
1415       if ((inst & 0xfc000000) == 0x34000000
1416           && inst_saves_fr (next_inst) >= 4
1417           && inst_saves_fr (next_inst) <= (TARGET_PTR_BIT == 64 ? 11 : 7))
1418         {
1419           /* So we drop into the code below in a reasonable state.  */
1420           reg_num = inst_saves_fr (next_inst);
1421           pc -= 4;
1422         }
1423
1424       /* Ugh.  Also account for argument stores into the stack.
1425          This is a kludge as on the HP compiler sets this bit and it
1426          never does prologue scheduling.  So once we see one, skip past
1427          all of them.  */
1428       if (reg_num >= 4 && reg_num <= (TARGET_PTR_BIT == 64 ? 11 : 7))
1429         {
1430           while (reg_num >= 4 && reg_num <= (TARGET_PTR_BIT == 64 ? 11 : 7))
1431             {
1432               pc += 8;
1433               status = deprecated_read_memory_nobpt (pc, buf, 4);
1434               inst = extract_unsigned_integer (buf, 4);
1435               if (status != 0)
1436                 return pc;
1437               if ((inst & 0xfc000000) != 0x34000000)
1438                 break;
1439               status = deprecated_read_memory_nobpt (pc + 4, buf, 4);
1440               next_inst = extract_unsigned_integer (buf, 4);
1441               if (status != 0)
1442                 return pc;
1443               reg_num = inst_saves_fr (next_inst);
1444             }
1445           args_stored = 0;
1446           continue;
1447         }
1448
1449       /* Quit if we hit any kind of branch.  This can happen if a prologue
1450          instruction is in the delay slot of the first call/branch.  */
1451       if (is_branch (inst) && stop_before_branch)
1452         break;
1453
1454       /* What a crock.  The HP compilers set args_stored even if no
1455          arguments were stored into the stack (boo hiss).  This could
1456          cause this code to then skip a bunch of user insns (up to the
1457          first branch).
1458
1459          To combat this we try to identify when args_stored was bogusly
1460          set and clear it.   We only do this when args_stored is nonzero,
1461          all other resources are accounted for, and nothing changed on
1462          this pass.  */
1463       if (args_stored
1464        && !(save_gr || save_fr || save_rp || save_sp || stack_remaining > 0)
1465           && old_save_gr == save_gr && old_save_fr == save_fr
1466           && old_save_rp == save_rp && old_save_sp == save_sp
1467           && old_stack_remaining == stack_remaining)
1468         break;
1469
1470       /* Bump the PC.  */
1471       pc += 4;
1472
1473       /* !stop_before_branch, so also look at the insn in the delay slot 
1474          of the branch.  */
1475       if (final_iteration)
1476         break;
1477       if (is_branch (inst))
1478         final_iteration = 1;
1479     }
1480
1481   /* We've got a tenative location for the end of the prologue.  However
1482      because of limitations in the unwind descriptor mechanism we may
1483      have went too far into user code looking for the save of a register
1484      that does not exist.  So, if there registers we expected to be saved
1485      but never were, mask them out and restart.
1486
1487      This should only happen in optimized code, and should be very rare.  */
1488   if (save_gr || (save_fr && !(restart_fr || restart_gr)))
1489     {
1490       pc = orig_pc;
1491       restart_gr = save_gr;
1492       restart_fr = save_fr;
1493       goto restart;
1494     }
1495
1496   return pc;
1497 }
1498
1499
1500 /* Return the address of the PC after the last prologue instruction if
1501    we can determine it from the debug symbols.  Else return zero.  */
1502
1503 static CORE_ADDR
1504 after_prologue (CORE_ADDR pc)
1505 {
1506   struct symtab_and_line sal;
1507   CORE_ADDR func_addr, func_end;
1508   struct symbol *f;
1509
1510   /* If we can not find the symbol in the partial symbol table, then
1511      there is no hope we can determine the function's start address
1512      with this code.  */
1513   if (!find_pc_partial_function (pc, NULL, &func_addr, &func_end))
1514     return 0;
1515
1516   /* Get the line associated with FUNC_ADDR.  */
1517   sal = find_pc_line (func_addr, 0);
1518
1519   /* There are only two cases to consider.  First, the end of the source line
1520      is within the function bounds.  In that case we return the end of the
1521      source line.  Second is the end of the source line extends beyond the
1522      bounds of the current function.  We need to use the slow code to
1523      examine instructions in that case. 
1524
1525      Anything else is simply a bug elsewhere.  Fixing it here is absolutely
1526      the wrong thing to do.  In fact, it should be entirely possible for this
1527      function to always return zero since the slow instruction scanning code
1528      is supposed to *always* work.  If it does not, then it is a bug.  */
1529   if (sal.end < func_end)
1530     return sal.end;
1531   else
1532     return 0;
1533 }
1534
1535 /* To skip prologues, I use this predicate.  Returns either PC itself
1536    if the code at PC does not look like a function prologue; otherwise
1537    returns an address that (if we're lucky) follows the prologue.  
1538    
1539    hppa_skip_prologue is called by gdb to place a breakpoint in a function.
1540    It doesn't necessarily skips all the insns in the prologue. In fact
1541    we might not want to skip all the insns because a prologue insn may
1542    appear in the delay slot of the first branch, and we don't want to
1543    skip over the branch in that case.  */
1544
1545 static CORE_ADDR
1546 hppa_skip_prologue (CORE_ADDR pc)
1547 {
1548   unsigned long inst;
1549   int offset;
1550   CORE_ADDR post_prologue_pc;
1551   char buf[4];
1552
1553   /* See if we can determine the end of the prologue via the symbol table.
1554      If so, then return either PC, or the PC after the prologue, whichever
1555      is greater.  */
1556
1557   post_prologue_pc = after_prologue (pc);
1558
1559   /* If after_prologue returned a useful address, then use it.  Else
1560      fall back on the instruction skipping code.
1561
1562      Some folks have claimed this causes problems because the breakpoint
1563      may be the first instruction of the prologue.  If that happens, then
1564      the instruction skipping code has a bug that needs to be fixed.  */
1565   if (post_prologue_pc != 0)
1566     return max (pc, post_prologue_pc);
1567   else
1568     return (skip_prologue_hard_way (pc, 1));
1569 }
1570
1571 struct hppa_frame_cache
1572 {
1573   CORE_ADDR base;
1574   struct trad_frame_saved_reg *saved_regs;
1575 };
1576
1577 static struct hppa_frame_cache *
1578 hppa_frame_cache (struct frame_info *next_frame, void **this_cache)
1579 {
1580   struct hppa_frame_cache *cache;
1581   long saved_gr_mask;
1582   long saved_fr_mask;
1583   CORE_ADDR this_sp;
1584   long frame_size;
1585   struct unwind_table_entry *u;
1586   CORE_ADDR prologue_end;
1587   int fp_in_r1 = 0;
1588   int i;
1589
1590   if (hppa_debug)
1591     fprintf_unfiltered (gdb_stdlog, "{ hppa_frame_cache (frame=%d) -> ",
1592       frame_relative_level(next_frame));
1593
1594   if ((*this_cache) != NULL)
1595     {
1596       if (hppa_debug)
1597         fprintf_unfiltered (gdb_stdlog, "base=0x%s (cached) }", 
1598           paddr_nz (((struct hppa_frame_cache *)*this_cache)->base));
1599       return (*this_cache);
1600     }
1601   cache = FRAME_OBSTACK_ZALLOC (struct hppa_frame_cache);
1602   (*this_cache) = cache;
1603   cache->saved_regs = trad_frame_alloc_saved_regs (next_frame);
1604
1605   /* Yow! */
1606   u = find_unwind_entry (frame_pc_unwind (next_frame));
1607   if (!u)
1608     {
1609       if (hppa_debug)
1610         fprintf_unfiltered (gdb_stdlog, "base=NULL (no unwind entry) }");
1611       return (*this_cache);
1612     }
1613
1614   /* Turn the Entry_GR field into a bitmask.  */
1615   saved_gr_mask = 0;
1616   for (i = 3; i < u->Entry_GR + 3; i++)
1617     {
1618       /* Frame pointer gets saved into a special location.  */
1619       if (u->Save_SP && i == HPPA_FP_REGNUM)
1620         continue;
1621         
1622       saved_gr_mask |= (1 << i);
1623     }
1624
1625   /* Turn the Entry_FR field into a bitmask too.  */
1626   saved_fr_mask = 0;
1627   for (i = 12; i < u->Entry_FR + 12; i++)
1628     saved_fr_mask |= (1 << i);
1629
1630   /* Loop until we find everything of interest or hit a branch.
1631
1632      For unoptimized GCC code and for any HP CC code this will never ever
1633      examine any user instructions.
1634
1635      For optimized GCC code we're faced with problems.  GCC will schedule
1636      its prologue and make prologue instructions available for delay slot
1637      filling.  The end result is user code gets mixed in with the prologue
1638      and a prologue instruction may be in the delay slot of the first branch
1639      or call.
1640
1641      Some unexpected things are expected with debugging optimized code, so
1642      we allow this routine to walk past user instructions in optimized
1643      GCC code.  */
1644   {
1645     int final_iteration = 0;
1646     CORE_ADDR pc, end_pc;
1647     int looking_for_sp = u->Save_SP;
1648     int looking_for_rp = u->Save_RP;
1649     int fp_loc = -1;
1650
1651     /* We have to use skip_prologue_hard_way instead of just 
1652        skip_prologue_using_sal, in case we stepped into a function without
1653        symbol information.  hppa_skip_prologue also bounds the returned
1654        pc by the passed in pc, so it will not return a pc in the next
1655        function.  
1656        
1657        We used to call hppa_skip_prologue to find the end of the prologue,
1658        but if some non-prologue instructions get scheduled into the prologue,
1659        and the program is compiled with debug information, the "easy" way
1660        in hppa_skip_prologue will return a prologue end that is too early
1661        for us to notice any potential frame adjustments.  */
1662
1663     /* We used to use frame_func_unwind () to locate the beginning of the
1664        function to pass to skip_prologue ().  However, when objects are 
1665        compiled without debug symbols, frame_func_unwind can return the wrong 
1666        function (or 0).  We can do better than that by using unwind records.  */
1667
1668     prologue_end = skip_prologue_hard_way (u->region_start, 0);
1669     end_pc = frame_pc_unwind (next_frame);
1670
1671     if (prologue_end != 0 && end_pc > prologue_end)
1672       end_pc = prologue_end;
1673
1674     frame_size = 0;
1675
1676     for (pc = u->region_start;
1677          ((saved_gr_mask || saved_fr_mask
1678            || looking_for_sp || looking_for_rp
1679            || frame_size < (u->Total_frame_size << 3))
1680           && pc < end_pc);
1681          pc += 4)
1682       {
1683         int reg;
1684         char buf4[4];
1685         long inst;
1686
1687         if (!safe_frame_unwind_memory (next_frame, pc, buf4, 
1688                                        sizeof buf4)) 
1689           {
1690             error ("Cannot read instruction at 0x%s\n", paddr_nz (pc));
1691             return (*this_cache);
1692           }
1693
1694         inst = extract_unsigned_integer (buf4, sizeof buf4);
1695
1696         /* Note the interesting effects of this instruction.  */
1697         frame_size += prologue_inst_adjust_sp (inst);
1698         
1699         /* There are limited ways to store the return pointer into the
1700            stack.  */
1701         if (inst == 0x6bc23fd9) /* stw rp,-0x14(sr0,sp) */
1702           {
1703             looking_for_rp = 0;
1704             cache->saved_regs[HPPA_RP_REGNUM].addr = -20;
1705           }
1706         else if (inst == 0x6bc23fd1) /* stw rp,-0x18(sr0,sp) */
1707           {
1708             looking_for_rp = 0;
1709             cache->saved_regs[HPPA_RP_REGNUM].addr = -24;
1710           }
1711         else if (inst == 0x0fc212c1) /* std rp,-0x10(sr0,sp) */
1712           {
1713             looking_for_rp = 0;
1714             cache->saved_regs[HPPA_RP_REGNUM].addr = -16;
1715           }
1716         
1717         /* Check to see if we saved SP into the stack.  This also
1718            happens to indicate the location of the saved frame
1719            pointer.  */
1720         if ((inst & 0xffffc000) == 0x6fc10000  /* stw,ma r1,N(sr0,sp) */
1721             || (inst & 0xffffc00c) == 0x73c10008) /* std,ma r1,N(sr0,sp) */
1722           {
1723             looking_for_sp = 0;
1724             cache->saved_regs[HPPA_FP_REGNUM].addr = 0;
1725           }
1726         else if (inst == 0x08030241) /* copy %r3, %r1 */
1727           {
1728             fp_in_r1 = 1;
1729           }
1730         
1731         /* Account for general and floating-point register saves.  */
1732         reg = inst_saves_gr (inst);
1733         if (reg >= 3 && reg <= 18
1734             && (!u->Save_SP || reg != HPPA_FP_REGNUM))
1735           {
1736             saved_gr_mask &= ~(1 << reg);
1737             if ((inst >> 26) == 0x1b && hppa_extract_14 (inst) >= 0)
1738               /* stwm with a positive displacement is a _post_
1739                  _modify_.  */
1740               cache->saved_regs[reg].addr = 0;
1741             else if ((inst & 0xfc00000c) == 0x70000008)
1742               /* A std has explicit post_modify forms.  */
1743               cache->saved_regs[reg].addr = 0;
1744             else
1745               {
1746                 CORE_ADDR offset;
1747                 
1748                 if ((inst >> 26) == 0x1c)
1749                   offset = (inst & 0x1 ? -1 << 13 : 0) | (((inst >> 4) & 0x3ff) << 3);
1750                 else if ((inst >> 26) == 0x03)
1751                   offset = hppa_low_hppa_sign_extend (inst & 0x1f, 5);
1752                 else
1753                   offset = hppa_extract_14 (inst);
1754                 
1755                 /* Handle code with and without frame pointers.  */
1756                 if (u->Save_SP)
1757                   cache->saved_regs[reg].addr = offset;
1758                 else
1759                   cache->saved_regs[reg].addr = (u->Total_frame_size << 3) + offset;
1760               }
1761           }
1762
1763         /* GCC handles callee saved FP regs a little differently.  
1764            
1765            It emits an instruction to put the value of the start of
1766            the FP store area into %r1.  It then uses fstds,ma with a
1767            basereg of %r1 for the stores.
1768
1769            HP CC emits them at the current stack pointer modifying the
1770            stack pointer as it stores each register.  */
1771         
1772         /* ldo X(%r3),%r1 or ldo X(%r30),%r1.  */
1773         if ((inst & 0xffffc000) == 0x34610000
1774             || (inst & 0xffffc000) == 0x37c10000)
1775           fp_loc = hppa_extract_14 (inst);
1776         
1777         reg = inst_saves_fr (inst);
1778         if (reg >= 12 && reg <= 21)
1779           {
1780             /* Note +4 braindamage below is necessary because the FP
1781                status registers are internally 8 registers rather than
1782                the expected 4 registers.  */
1783             saved_fr_mask &= ~(1 << reg);
1784             if (fp_loc == -1)
1785               {
1786                 /* 1st HP CC FP register store.  After this
1787                    instruction we've set enough state that the GCC and
1788                    HPCC code are both handled in the same manner.  */
1789                 cache->saved_regs[reg + HPPA_FP4_REGNUM + 4].addr = 0;
1790                 fp_loc = 8;
1791               }
1792             else
1793               {
1794                 cache->saved_regs[reg + HPPA_FP0_REGNUM + 4].addr = fp_loc;
1795                 fp_loc += 8;
1796               }
1797           }
1798         
1799         /* Quit if we hit any kind of branch the previous iteration. */
1800         if (final_iteration)
1801           break;
1802         /* We want to look precisely one instruction beyond the branch
1803            if we have not found everything yet.  */
1804         if (is_branch (inst))
1805           final_iteration = 1;
1806       }
1807   }
1808
1809   {
1810     /* The frame base always represents the value of %sp at entry to
1811        the current function (and is thus equivalent to the "saved"
1812        stack pointer.  */
1813     CORE_ADDR this_sp = frame_unwind_register_unsigned (next_frame, HPPA_SP_REGNUM);
1814     CORE_ADDR fp;
1815
1816     if (hppa_debug)
1817       fprintf_unfiltered (gdb_stdlog, " (this_sp=0x%s, pc=0x%s, "
1818                           "prologue_end=0x%s) ",
1819                           paddr_nz (this_sp),
1820                           paddr_nz (frame_pc_unwind (next_frame)),
1821                           paddr_nz (prologue_end));
1822
1823      /* Check to see if a frame pointer is available, and use it for
1824         frame unwinding if it is.
1825  
1826         There are some situations where we need to rely on the frame
1827         pointer to do stack unwinding.  For example, if a function calls
1828         alloca (), the stack pointer can get adjusted inside the body of
1829         the function.  In this case, the ABI requires that the compiler
1830         maintain a frame pointer for the function.
1831  
1832         The unwind record has a flag (alloca_frame) that indicates that
1833         a function has a variable frame; unfortunately, gcc/binutils 
1834         does not set this flag.  Instead, whenever a frame pointer is used
1835         and saved on the stack, the Save_SP flag is set.  We use this to
1836         decide whether to use the frame pointer for unwinding.
1837         
1838         TODO: For the HP compiler, maybe we should use the alloca_frame flag 
1839         instead of Save_SP.  */
1840  
1841      fp = frame_unwind_register_unsigned (next_frame, HPPA_FP_REGNUM);
1842  
1843      if (frame_pc_unwind (next_frame) >= prologue_end
1844          && u->Save_SP && fp != 0)
1845       {
1846         cache->base = fp;
1847  
1848         if (hppa_debug)
1849           fprintf_unfiltered (gdb_stdlog, " (base=0x%s) [frame pointer] }",
1850             paddr_nz (cache->base));
1851       }
1852      else if (u->Save_SP 
1853               && trad_frame_addr_p (cache->saved_regs, HPPA_SP_REGNUM))
1854       {
1855             /* Both we're expecting the SP to be saved and the SP has been
1856                saved.  The entry SP value is saved at this frame's SP
1857                address.  */
1858             cache->base = read_memory_integer (this_sp, TARGET_PTR_BIT / 8);
1859
1860             if (hppa_debug)
1861               fprintf_unfiltered (gdb_stdlog, " (base=0x%s) [saved] }",
1862                                   paddr_nz (cache->base));
1863       }
1864     else
1865       {
1866         /* The prologue has been slowly allocating stack space.  Adjust
1867            the SP back.  */
1868         cache->base = this_sp - frame_size;
1869         if (hppa_debug)
1870           fprintf_unfiltered (gdb_stdlog, " (base=0x%s) [unwind adjust] } ",
1871                               paddr_nz (cache->base));
1872
1873       }
1874     trad_frame_set_value (cache->saved_regs, HPPA_SP_REGNUM, cache->base);
1875   }
1876
1877   /* The PC is found in the "return register", "Millicode" uses "r31"
1878      as the return register while normal code uses "rp".  */
1879   if (u->Millicode)
1880     {
1881       if (trad_frame_addr_p (cache->saved_regs, 31))
1882         cache->saved_regs[HPPA_PCOQ_HEAD_REGNUM] = cache->saved_regs[31];
1883       else
1884         {
1885           ULONGEST r31 = frame_unwind_register_unsigned (next_frame, 31);
1886           trad_frame_set_value (cache->saved_regs, HPPA_PCOQ_HEAD_REGNUM, r31);
1887         }
1888     }
1889   else
1890     {
1891       if (trad_frame_addr_p (cache->saved_regs, HPPA_RP_REGNUM))
1892         cache->saved_regs[HPPA_PCOQ_HEAD_REGNUM] = cache->saved_regs[HPPA_RP_REGNUM];
1893       else
1894         {
1895           ULONGEST rp = frame_unwind_register_unsigned (next_frame, HPPA_RP_REGNUM);
1896           trad_frame_set_value (cache->saved_regs, HPPA_PCOQ_HEAD_REGNUM, rp);
1897         }
1898     }
1899
1900   /* If Save_SP is set, then we expect the frame pointer to be saved in the
1901      frame.  However, there is a one-insn window where we haven't saved it
1902      yet, but we've already clobbered it.  Detect this case and fix it up.
1903
1904      The prologue sequence for frame-pointer functions is:
1905         0: stw %rp, -20(%sp)
1906         4: copy %r3, %r1
1907         8: copy %sp, %r3
1908         c: stw,ma %r1, XX(%sp)
1909
1910      So if we are at offset c, the r3 value that we want is not yet saved
1911      on the stack, but it's been overwritten.  The prologue analyzer will
1912      set fp_in_r1 when it sees the copy insn so we know to get the value 
1913      from r1 instead.  */
1914   if (u->Save_SP && !trad_frame_addr_p (cache->saved_regs, HPPA_FP_REGNUM)
1915       && fp_in_r1)
1916     {
1917       ULONGEST r1 = frame_unwind_register_unsigned (next_frame, 1);
1918       trad_frame_set_value (cache->saved_regs, HPPA_FP_REGNUM, r1);
1919     }
1920
1921   {
1922     /* Convert all the offsets into addresses.  */
1923     int reg;
1924     for (reg = 0; reg < NUM_REGS; reg++)
1925       {
1926         if (trad_frame_addr_p (cache->saved_regs, reg))
1927           cache->saved_regs[reg].addr += cache->base;
1928       }
1929   }
1930
1931   if (hppa_debug)
1932     fprintf_unfiltered (gdb_stdlog, "base=0x%s }", 
1933       paddr_nz (((struct hppa_frame_cache *)*this_cache)->base));
1934   return (*this_cache);
1935 }
1936
1937 static void
1938 hppa_frame_this_id (struct frame_info *next_frame, void **this_cache,
1939                            struct frame_id *this_id)
1940 {
1941   struct hppa_frame_cache *info;
1942   CORE_ADDR pc = frame_pc_unwind (next_frame);
1943   struct unwind_table_entry *u;
1944
1945   info = hppa_frame_cache (next_frame, this_cache);
1946   u = find_unwind_entry (pc);
1947
1948   (*this_id) = frame_id_build (info->base, u->region_start);
1949 }
1950
1951 static void
1952 hppa_frame_prev_register (struct frame_info *next_frame,
1953                           void **this_cache,
1954                           int regnum, int *optimizedp,
1955                           enum lval_type *lvalp, CORE_ADDR *addrp,
1956                           int *realnump, void *valuep)
1957 {
1958   struct hppa_frame_cache *info = hppa_frame_cache (next_frame, this_cache);
1959   hppa_frame_prev_register_helper (next_frame, info->saved_regs, regnum,
1960                                    optimizedp, lvalp, addrp, realnump, valuep);
1961 }
1962
1963 static const struct frame_unwind hppa_frame_unwind =
1964 {
1965   NORMAL_FRAME,
1966   hppa_frame_this_id,
1967   hppa_frame_prev_register
1968 };
1969
1970 static const struct frame_unwind *
1971 hppa_frame_unwind_sniffer (struct frame_info *next_frame)
1972 {
1973   CORE_ADDR pc = frame_pc_unwind (next_frame);
1974
1975   if (find_unwind_entry (pc))
1976     return &hppa_frame_unwind;
1977
1978   return NULL;
1979 }
1980
1981 /* This is a generic fallback frame unwinder that kicks in if we fail all
1982    the other ones.  Normally we would expect the stub and regular unwinder
1983    to work, but in some cases we might hit a function that just doesn't
1984    have any unwind information available.  In this case we try to do
1985    unwinding solely based on code reading.  This is obviously going to be
1986    slow, so only use this as a last resort.  Currently this will only
1987    identify the stack and pc for the frame.  */
1988
1989 static struct hppa_frame_cache *
1990 hppa_fallback_frame_cache (struct frame_info *next_frame, void **this_cache)
1991 {
1992   struct hppa_frame_cache *cache;
1993   unsigned int frame_size;
1994   int found_rp;
1995   CORE_ADDR pc, start_pc, end_pc, cur_pc;
1996
1997   if (hppa_debug)
1998     fprintf_unfiltered (gdb_stdlog, "{ hppa_fallback_frame_cache (frame=%d)-> ",
1999       frame_relative_level(next_frame));
2000
2001   cache = FRAME_OBSTACK_ZALLOC (struct hppa_frame_cache);
2002   (*this_cache) = cache;
2003   cache->saved_regs = trad_frame_alloc_saved_regs (next_frame);
2004
2005   pc = frame_func_unwind (next_frame);
2006   cur_pc = frame_pc_unwind (next_frame);
2007   frame_size = 0;
2008   found_rp = 0;
2009
2010   find_pc_partial_function (pc, NULL, &start_pc, &end_pc);
2011
2012   if (start_pc == 0 || end_pc == 0)
2013     {
2014       error ("Cannot find bounds of current function (@0x%s), unwinding will "
2015              "fail.", paddr_nz (pc));
2016       return cache;
2017     }
2018
2019   if (end_pc > cur_pc)
2020     end_pc = cur_pc;
2021
2022   for (pc = start_pc; pc < end_pc; pc += 4)
2023     {
2024       unsigned int insn;
2025
2026       insn = read_memory_unsigned_integer (pc, 4);
2027
2028       frame_size += prologue_inst_adjust_sp (insn);
2029
2030       /* There are limited ways to store the return pointer into the
2031          stack.  */
2032       if (insn == 0x6bc23fd9) /* stw rp,-0x14(sr0,sp) */
2033          {
2034            cache->saved_regs[HPPA_RP_REGNUM].addr = -20;
2035            found_rp = 1;
2036          }
2037       else if (insn == 0x0fc212c1) /* std rp,-0x10(sr0,sp) */
2038          {
2039            cache->saved_regs[HPPA_RP_REGNUM].addr = -16;
2040            found_rp = 1;
2041          }
2042     }
2043
2044   if (hppa_debug)
2045     fprintf_unfiltered (gdb_stdlog, " frame_size = %d, found_rp = %d }\n",
2046       frame_size, found_rp);
2047
2048   cache->base = frame_unwind_register_unsigned (next_frame, HPPA_SP_REGNUM) - frame_size;
2049   trad_frame_set_value (cache->saved_regs, HPPA_SP_REGNUM, cache->base);
2050
2051   if (trad_frame_addr_p (cache->saved_regs, HPPA_RP_REGNUM))
2052     {
2053       cache->saved_regs[HPPA_RP_REGNUM].addr += cache->base;
2054       cache->saved_regs[HPPA_PCOQ_HEAD_REGNUM] = cache->saved_regs[HPPA_RP_REGNUM];
2055     }
2056   else
2057     {
2058       ULONGEST rp = frame_unwind_register_unsigned (next_frame, HPPA_RP_REGNUM);
2059       trad_frame_set_value (cache->saved_regs, HPPA_PCOQ_HEAD_REGNUM, rp);
2060     }
2061
2062   return cache;
2063 }
2064
2065 static void
2066 hppa_fallback_frame_this_id (struct frame_info *next_frame, void **this_cache,
2067                              struct frame_id *this_id)
2068 {
2069   struct hppa_frame_cache *info = 
2070     hppa_fallback_frame_cache (next_frame, this_cache);
2071   (*this_id) = frame_id_build (info->base, frame_func_unwind (next_frame));
2072 }
2073
2074 static void
2075 hppa_fallback_frame_prev_register (struct frame_info *next_frame,
2076                           void **this_cache,
2077                           int regnum, int *optimizedp,
2078                           enum lval_type *lvalp, CORE_ADDR *addrp,
2079                           int *realnump, void *valuep)
2080 {
2081   struct hppa_frame_cache *info = 
2082     hppa_fallback_frame_cache (next_frame, this_cache);
2083   hppa_frame_prev_register_helper (next_frame, info->saved_regs, regnum,
2084                                    optimizedp, lvalp, addrp, realnump, valuep);
2085 }
2086
2087 static const struct frame_unwind hppa_fallback_frame_unwind =
2088 {
2089   NORMAL_FRAME,
2090   hppa_fallback_frame_this_id,
2091   hppa_fallback_frame_prev_register
2092 };
2093
2094 static const struct frame_unwind *
2095 hppa_fallback_unwind_sniffer (struct frame_info *next_frame)
2096 {
2097   return &hppa_fallback_frame_unwind;
2098 }
2099
2100 /* Stub frames, used for all kinds of call stubs.  */
2101 struct hppa_stub_unwind_cache
2102 {
2103   CORE_ADDR base;
2104   struct trad_frame_saved_reg *saved_regs;
2105 };
2106
2107 static struct hppa_stub_unwind_cache *
2108 hppa_stub_frame_unwind_cache (struct frame_info *next_frame,
2109                               void **this_cache)
2110 {
2111   struct gdbarch *gdbarch = get_frame_arch (next_frame);
2112   struct hppa_stub_unwind_cache *info;
2113   struct unwind_table_entry *u;
2114
2115   if (*this_cache)
2116     return *this_cache;
2117
2118   info = FRAME_OBSTACK_ZALLOC (struct hppa_stub_unwind_cache);
2119   *this_cache = info;
2120   info->saved_regs = trad_frame_alloc_saved_regs (next_frame);
2121
2122   info->base = frame_unwind_register_unsigned (next_frame, HPPA_SP_REGNUM);
2123
2124   if (gdbarch_osabi (gdbarch) == GDB_OSABI_HPUX_SOM)
2125     {
2126       /* HPUX uses export stubs in function calls; the export stub clobbers
2127          the return value of the caller, and, later restores it from the
2128          stack.  */
2129       u = find_unwind_entry (frame_pc_unwind (next_frame));
2130
2131       if (u && u->stub_unwind.stub_type == EXPORT)
2132         {
2133           info->saved_regs[HPPA_PCOQ_HEAD_REGNUM].addr = info->base - 24;
2134
2135           return info;
2136         }
2137     }
2138
2139   /* By default we assume that stubs do not change the rp.  */
2140   info->saved_regs[HPPA_PCOQ_HEAD_REGNUM].realreg = HPPA_RP_REGNUM;
2141
2142   return info;
2143 }
2144
2145 static void
2146 hppa_stub_frame_this_id (struct frame_info *next_frame,
2147                          void **this_prologue_cache,
2148                          struct frame_id *this_id)
2149 {
2150   struct hppa_stub_unwind_cache *info
2151     = hppa_stub_frame_unwind_cache (next_frame, this_prologue_cache);
2152   *this_id = frame_id_build (info->base, frame_pc_unwind (next_frame));
2153 }
2154
2155 static void
2156 hppa_stub_frame_prev_register (struct frame_info *next_frame,
2157                                void **this_prologue_cache,
2158                                int regnum, int *optimizedp,
2159                                enum lval_type *lvalp, CORE_ADDR *addrp,
2160                                int *realnump, void *valuep)
2161 {
2162   struct hppa_stub_unwind_cache *info
2163     = hppa_stub_frame_unwind_cache (next_frame, this_prologue_cache);
2164   hppa_frame_prev_register_helper (next_frame, info->saved_regs, regnum,
2165                                    optimizedp, lvalp, addrp, realnump, valuep);
2166 }
2167
2168 static const struct frame_unwind hppa_stub_frame_unwind = {
2169   NORMAL_FRAME,
2170   hppa_stub_frame_this_id,
2171   hppa_stub_frame_prev_register
2172 };
2173
2174 static const struct frame_unwind *
2175 hppa_stub_unwind_sniffer (struct frame_info *next_frame)
2176 {
2177   CORE_ADDR pc = frame_pc_unwind (next_frame);
2178   struct gdbarch *gdbarch = get_frame_arch (next_frame);
2179   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
2180
2181   if (pc == 0
2182       || (tdep->in_solib_call_trampoline != NULL
2183           && tdep->in_solib_call_trampoline (pc, NULL))
2184       || IN_SOLIB_RETURN_TRAMPOLINE (pc, NULL))
2185     return &hppa_stub_frame_unwind;
2186   return NULL;
2187 }
2188
2189 static struct frame_id
2190 hppa_unwind_dummy_id (struct gdbarch *gdbarch, struct frame_info *next_frame)
2191 {
2192   return frame_id_build (frame_unwind_register_unsigned (next_frame,
2193                                                          HPPA_SP_REGNUM),
2194                          frame_pc_unwind (next_frame));
2195 }
2196
2197 static CORE_ADDR
2198 hppa_unwind_pc (struct gdbarch *gdbarch, struct frame_info *next_frame)
2199 {
2200   ULONGEST ipsw;
2201   CORE_ADDR pc;
2202
2203   ipsw = frame_unwind_register_signed (next_frame, HPPA_IPSW_REGNUM);
2204   pc = frame_unwind_register_signed (next_frame, HPPA_PCOQ_HEAD_REGNUM) & ~3;
2205
2206   /* If the current instruction is nullified, then we are effectively
2207      still executing the previous instruction.  Pretend we are still
2208      there.  This is needed when single stepping; if the nullified instruction
2209      is on a different line, we don't want gdb to think we've stepped onto
2210      that line.  */
2211   if (ipsw & 0x00200000)
2212     pc -= 4;
2213
2214   return pc;
2215 }
2216
2217 /* Instead of this nasty cast, add a method pvoid() that prints out a
2218    host VOID data type (remember %p isn't portable).  */
2219
2220 static CORE_ADDR
2221 hppa_pointer_to_address_hack (void *ptr)
2222 {
2223   gdb_assert (sizeof (ptr) == TYPE_LENGTH (builtin_type_void_data_ptr));
2224   return POINTER_TO_ADDRESS (builtin_type_void_data_ptr, &ptr);
2225 }
2226
2227 static void
2228 unwind_command (char *exp, int from_tty)
2229 {
2230   CORE_ADDR address;
2231   struct unwind_table_entry *u;
2232
2233   /* If we have an expression, evaluate it and use it as the address.  */
2234
2235   if (exp != 0 && *exp != 0)
2236     address = parse_and_eval_address (exp);
2237   else
2238     return;
2239
2240   u = find_unwind_entry (address);
2241
2242   if (!u)
2243     {
2244       printf_unfiltered ("Can't find unwind table entry for %s\n", exp);
2245       return;
2246     }
2247
2248   printf_unfiltered ("unwind_table_entry (0x%s):\n",
2249                      paddr_nz (hppa_pointer_to_address_hack (u)));
2250
2251   printf_unfiltered ("\tregion_start = ");
2252   print_address (u->region_start, gdb_stdout);
2253   gdb_flush (gdb_stdout);
2254
2255   printf_unfiltered ("\n\tregion_end = ");
2256   print_address (u->region_end, gdb_stdout);
2257   gdb_flush (gdb_stdout);
2258
2259 #define pif(FLD) if (u->FLD) printf_unfiltered (" "#FLD);
2260
2261   printf_unfiltered ("\n\tflags =");
2262   pif (Cannot_unwind);
2263   pif (Millicode);
2264   pif (Millicode_save_sr0);
2265   pif (Entry_SR);
2266   pif (Args_stored);
2267   pif (Variable_Frame);
2268   pif (Separate_Package_Body);
2269   pif (Frame_Extension_Millicode);
2270   pif (Stack_Overflow_Check);
2271   pif (Two_Instruction_SP_Increment);
2272   pif (Ada_Region);
2273   pif (Save_SP);
2274   pif (Save_RP);
2275   pif (Save_MRP_in_frame);
2276   pif (extn_ptr_defined);
2277   pif (Cleanup_defined);
2278   pif (MPE_XL_interrupt_marker);
2279   pif (HP_UX_interrupt_marker);
2280   pif (Large_frame);
2281
2282   putchar_unfiltered ('\n');
2283
2284 #define pin(FLD) printf_unfiltered ("\t"#FLD" = 0x%x\n", u->FLD);
2285
2286   pin (Region_description);
2287   pin (Entry_FR);
2288   pin (Entry_GR);
2289   pin (Total_frame_size);
2290
2291   if (u->stub_unwind.stub_type)
2292     {
2293       printf_unfiltered ("\tstub type = ");
2294       switch (u->stub_unwind.stub_type)
2295         {
2296           case LONG_BRANCH:
2297             printf_unfiltered ("long branch\n");
2298             break;
2299           case PARAMETER_RELOCATION:
2300             printf_unfiltered ("parameter relocation\n");
2301             break;
2302           case EXPORT:
2303             printf_unfiltered ("export\n");
2304             break;
2305           case IMPORT:
2306             printf_unfiltered ("import\n");
2307             break;
2308           case IMPORT_SHLIB:
2309             printf_unfiltered ("import shlib\n");
2310             break;
2311           default:
2312             printf_unfiltered ("unknown (%d)\n", u->stub_unwind.stub_type);
2313         }
2314     }
2315 }
2316
2317 int
2318 hppa_pc_requires_run_before_use (CORE_ADDR pc)
2319 {
2320   /* Sometimes we may pluck out a minimal symbol that has a negative address.
2321   
2322      An example of this occurs when an a.out is linked against a foo.sl.
2323      The foo.sl defines a global bar(), and the a.out declares a signature
2324      for bar().  However, the a.out doesn't directly call bar(), but passes
2325      its address in another call.
2326   
2327      If you have this scenario and attempt to "break bar" before running,
2328      gdb will find a minimal symbol for bar() in the a.out.  But that
2329      symbol's address will be negative.  What this appears to denote is
2330      an index backwards from the base of the procedure linkage table (PLT)
2331      into the data linkage table (DLT), the end of which is contiguous
2332      with the start of the PLT.  This is clearly not a valid address for
2333      us to set a breakpoint on.
2334   
2335      Note that one must be careful in how one checks for a negative address.
2336      0xc0000000 is a legitimate address of something in a shared text
2337      segment, for example.  Since I don't know what the possible range
2338      is of these "really, truly negative" addresses that come from the
2339      minimal symbols, I'm resorting to the gross hack of checking the
2340      top byte of the address for all 1's.  Sigh.  */
2341
2342   return (!target_has_stack && (pc & 0xFF000000));
2343 }
2344
2345 /* Return the GDB type object for the "standard" data type of data
2346    in register N.  */
2347
2348 static struct type *
2349 hppa32_register_type (struct gdbarch *gdbarch, int reg_nr)
2350 {
2351    if (reg_nr < HPPA_FP4_REGNUM)
2352      return builtin_type_uint32;
2353    else
2354      return builtin_type_ieee_single_big;
2355 }
2356
2357 /* Return the GDB type object for the "standard" data type of data
2358    in register N.  hppa64 version.  */
2359
2360 static struct type *
2361 hppa64_register_type (struct gdbarch *gdbarch, int reg_nr)
2362 {
2363    if (reg_nr < HPPA_FP4_REGNUM)
2364      return builtin_type_uint64;
2365    else
2366      return builtin_type_ieee_double_big;
2367 }
2368
2369 /* Return True if REGNUM is not a register available to the user
2370    through ptrace().  */
2371
2372 static int
2373 hppa_cannot_store_register (int regnum)
2374 {
2375   return (regnum == 0
2376           || regnum == HPPA_PCSQ_HEAD_REGNUM
2377           || (regnum >= HPPA_PCSQ_TAIL_REGNUM && regnum < HPPA_IPSW_REGNUM)
2378           || (regnum > HPPA_IPSW_REGNUM && regnum < HPPA_FP4_REGNUM));
2379
2380 }
2381
2382 static CORE_ADDR
2383 hppa_smash_text_address (CORE_ADDR addr)
2384 {
2385   /* The low two bits of the PC on the PA contain the privilege level.
2386      Some genius implementing a (non-GCC) compiler apparently decided
2387      this means that "addresses" in a text section therefore include a
2388      privilege level, and thus symbol tables should contain these bits.
2389      This seems like a bonehead thing to do--anyway, it seems to work
2390      for our purposes to just ignore those bits.  */
2391
2392   return (addr &= ~0x3);
2393 }
2394
2395 /* Get the ith function argument for the current function.  */
2396 static CORE_ADDR
2397 hppa_fetch_pointer_argument (struct frame_info *frame, int argi, 
2398                              struct type *type)
2399 {
2400   CORE_ADDR addr;
2401   get_frame_register (frame, HPPA_R0_REGNUM + 26 - argi, &addr);
2402   return addr;
2403 }
2404
2405 static void
2406 hppa_pseudo_register_read (struct gdbarch *gdbarch, struct regcache *regcache,
2407                            int regnum, void *buf)
2408 {
2409     ULONGEST tmp;
2410
2411     regcache_raw_read_unsigned (regcache, regnum, &tmp);
2412     if (regnum == HPPA_PCOQ_HEAD_REGNUM || regnum == HPPA_PCOQ_TAIL_REGNUM)
2413       tmp &= ~0x3;
2414     store_unsigned_integer (buf, sizeof(tmp), tmp);
2415 }
2416
2417 static CORE_ADDR
2418 hppa_find_global_pointer (struct value *function)
2419 {
2420   return 0;
2421 }
2422
2423 void
2424 hppa_frame_prev_register_helper (struct frame_info *next_frame,
2425                                  struct trad_frame_saved_reg saved_regs[],
2426                                  int regnum, int *optimizedp,
2427                                  enum lval_type *lvalp, CORE_ADDR *addrp,
2428                                  int *realnump, void *valuep)
2429 {
2430   if (regnum == HPPA_PCOQ_TAIL_REGNUM)
2431     {
2432       if (valuep)
2433         {
2434           CORE_ADDR pc;
2435
2436           trad_frame_get_prev_register (next_frame, saved_regs,
2437                                         HPPA_PCOQ_HEAD_REGNUM, optimizedp,
2438                                         lvalp, addrp, realnump, valuep);
2439
2440           pc = extract_unsigned_integer (valuep, 4);
2441           store_unsigned_integer (valuep, 4, pc + 4);
2442         }
2443
2444       /* It's a computed value.  */
2445       *optimizedp = 0;
2446       *lvalp = not_lval;
2447       *addrp = 0;
2448       *realnump = -1;
2449       return;
2450     }
2451
2452   trad_frame_get_prev_register (next_frame, saved_regs, regnum,
2453                                 optimizedp, lvalp, addrp, realnump, valuep);
2454 }
2455 \f
2456
2457 /* Here is a table of C type sizes on hppa with various compiles
2458    and options.  I measured this on PA 9000/800 with HP-UX 11.11
2459    and these compilers:
2460
2461      /usr/ccs/bin/cc    HP92453-01 A.11.01.21
2462      /opt/ansic/bin/cc  HP92453-01 B.11.11.28706.GP
2463      /opt/aCC/bin/aCC   B3910B A.03.45
2464      gcc                gcc 3.3.2 native hppa2.0w-hp-hpux11.11
2465
2466      cc            : 1 2 4 4 8 : 4 8 -- : 4 4
2467      ansic +DA1.1  : 1 2 4 4 8 : 4 8 16 : 4 4
2468      ansic +DA2.0  : 1 2 4 4 8 : 4 8 16 : 4 4
2469      ansic +DA2.0W : 1 2 4 8 8 : 4 8 16 : 8 8
2470      acc   +DA1.1  : 1 2 4 4 8 : 4 8 16 : 4 4
2471      acc   +DA2.0  : 1 2 4 4 8 : 4 8 16 : 4 4
2472      acc   +DA2.0W : 1 2 4 8 8 : 4 8 16 : 8 8
2473      gcc           : 1 2 4 4 8 : 4 8 16 : 4 4
2474
2475    Each line is:
2476
2477      compiler and options
2478      char, short, int, long, long long
2479      float, double, long double
2480      char *, void (*)()
2481
2482    So all these compilers use either ILP32 or LP64 model.
2483    TODO: gcc has more options so it needs more investigation.
2484
2485    For floating point types, see:
2486
2487      http://docs.hp.com/hpux/pdf/B3906-90006.pdf
2488      HP-UX floating-point guide, hpux 11.00
2489
2490    -- chastain 2003-12-18  */
2491
2492 static struct gdbarch *
2493 hppa_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
2494 {
2495   struct gdbarch_tdep *tdep;
2496   struct gdbarch *gdbarch;
2497   
2498   /* Try to determine the ABI of the object we are loading.  */
2499   if (info.abfd != NULL && info.osabi == GDB_OSABI_UNKNOWN)
2500     {
2501       /* If it's a SOM file, assume it's HP/UX SOM.  */
2502       if (bfd_get_flavour (info.abfd) == bfd_target_som_flavour)
2503         info.osabi = GDB_OSABI_HPUX_SOM;
2504     }
2505
2506   /* find a candidate among the list of pre-declared architectures.  */
2507   arches = gdbarch_list_lookup_by_info (arches, &info);
2508   if (arches != NULL)
2509     return (arches->gdbarch);
2510
2511   /* If none found, then allocate and initialize one.  */
2512   tdep = XZALLOC (struct gdbarch_tdep);
2513   gdbarch = gdbarch_alloc (&info, tdep);
2514
2515   /* Determine from the bfd_arch_info structure if we are dealing with
2516      a 32 or 64 bits architecture.  If the bfd_arch_info is not available,
2517      then default to a 32bit machine.  */
2518   if (info.bfd_arch_info != NULL)
2519     tdep->bytes_per_address =
2520       info.bfd_arch_info->bits_per_address / info.bfd_arch_info->bits_per_byte;
2521   else
2522     tdep->bytes_per_address = 4;
2523
2524   tdep->find_global_pointer = hppa_find_global_pointer;
2525
2526   /* Some parts of the gdbarch vector depend on whether we are running
2527      on a 32 bits or 64 bits target.  */
2528   switch (tdep->bytes_per_address)
2529     {
2530       case 4:
2531         set_gdbarch_num_regs (gdbarch, hppa32_num_regs);
2532         set_gdbarch_register_name (gdbarch, hppa32_register_name);
2533         set_gdbarch_register_type (gdbarch, hppa32_register_type);
2534         break;
2535       case 8:
2536         set_gdbarch_num_regs (gdbarch, hppa64_num_regs);
2537         set_gdbarch_register_name (gdbarch, hppa64_register_name);
2538         set_gdbarch_register_type (gdbarch, hppa64_register_type);
2539         break;
2540       default:
2541         internal_error (__FILE__, __LINE__, "Unsupported address size: %d",
2542                         tdep->bytes_per_address);
2543     }
2544
2545   set_gdbarch_long_bit (gdbarch, tdep->bytes_per_address * TARGET_CHAR_BIT);
2546   set_gdbarch_ptr_bit (gdbarch, tdep->bytes_per_address * TARGET_CHAR_BIT);
2547
2548   /* The following gdbarch vector elements are the same in both ILP32
2549      and LP64, but might show differences some day.  */
2550   set_gdbarch_long_long_bit (gdbarch, 64);
2551   set_gdbarch_long_double_bit (gdbarch, 128);
2552   set_gdbarch_long_double_format (gdbarch, &floatformat_ia64_quad_big);
2553
2554   /* The following gdbarch vector elements do not depend on the address
2555      size, or in any other gdbarch element previously set.  */
2556   set_gdbarch_skip_prologue (gdbarch, hppa_skip_prologue);
2557   set_gdbarch_inner_than (gdbarch, core_addr_greaterthan);
2558   set_gdbarch_sp_regnum (gdbarch, HPPA_SP_REGNUM);
2559   set_gdbarch_fp0_regnum (gdbarch, HPPA_FP0_REGNUM);
2560   set_gdbarch_cannot_store_register (gdbarch, hppa_cannot_store_register);
2561   set_gdbarch_cannot_fetch_register (gdbarch, hppa_cannot_store_register);
2562   set_gdbarch_addr_bits_remove (gdbarch, hppa_smash_text_address);
2563   set_gdbarch_smash_text_address (gdbarch, hppa_smash_text_address);
2564   set_gdbarch_believe_pcc_promotion (gdbarch, 1);
2565   set_gdbarch_read_pc (gdbarch, hppa_target_read_pc);
2566   set_gdbarch_write_pc (gdbarch, hppa_target_write_pc);
2567
2568   /* Helper for function argument information.  */
2569   set_gdbarch_fetch_pointer_argument (gdbarch, hppa_fetch_pointer_argument);
2570
2571   set_gdbarch_print_insn (gdbarch, print_insn_hppa);
2572
2573   /* When a hardware watchpoint triggers, we'll move the inferior past
2574      it by removing all eventpoints; stepping past the instruction
2575      that caused the trigger; reinserting eventpoints; and checking
2576      whether any watched location changed.  */
2577   set_gdbarch_have_nonsteppable_watchpoint (gdbarch, 1);
2578
2579   /* Inferior function call methods.  */
2580   switch (tdep->bytes_per_address)
2581     {
2582     case 4:
2583       set_gdbarch_push_dummy_call (gdbarch, hppa32_push_dummy_call);
2584       set_gdbarch_frame_align (gdbarch, hppa32_frame_align);
2585       set_gdbarch_convert_from_func_ptr_addr
2586         (gdbarch, hppa32_convert_from_func_ptr_addr);
2587       break;
2588     case 8:
2589       set_gdbarch_push_dummy_call (gdbarch, hppa64_push_dummy_call);
2590       set_gdbarch_frame_align (gdbarch, hppa64_frame_align);
2591       break;
2592     default:
2593       internal_error (__FILE__, __LINE__, "bad switch");
2594     }
2595       
2596   /* Struct return methods.  */
2597   switch (tdep->bytes_per_address)
2598     {
2599     case 4:
2600       set_gdbarch_return_value (gdbarch, hppa32_return_value);
2601       break;
2602     case 8:
2603       set_gdbarch_return_value (gdbarch, hppa64_return_value);
2604       break;
2605     default:
2606       internal_error (__FILE__, __LINE__, "bad switch");
2607     }
2608       
2609   set_gdbarch_breakpoint_from_pc (gdbarch, hppa_breakpoint_from_pc);
2610   set_gdbarch_pseudo_register_read (gdbarch, hppa_pseudo_register_read);
2611
2612   /* Frame unwind methods.  */
2613   set_gdbarch_unwind_dummy_id (gdbarch, hppa_unwind_dummy_id);
2614   set_gdbarch_unwind_pc (gdbarch, hppa_unwind_pc);
2615
2616   /* Hook in ABI-specific overrides, if they have been registered.  */
2617   gdbarch_init_osabi (info, gdbarch);
2618
2619   /* Hook in the default unwinders.  */
2620   frame_unwind_append_sniffer (gdbarch, hppa_stub_unwind_sniffer);
2621   frame_unwind_append_sniffer (gdbarch, hppa_frame_unwind_sniffer);
2622   frame_unwind_append_sniffer (gdbarch, hppa_fallback_unwind_sniffer);
2623
2624   return gdbarch;
2625 }
2626
2627 static void
2628 hppa_dump_tdep (struct gdbarch *current_gdbarch, struct ui_file *file)
2629 {
2630   struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch);
2631
2632   fprintf_unfiltered (file, "bytes_per_address = %d\n", 
2633                       tdep->bytes_per_address);
2634   fprintf_unfiltered (file, "elf = %s\n", tdep->is_elf ? "yes" : "no");
2635 }
2636
2637 void
2638 _initialize_hppa_tdep (void)
2639 {
2640   struct cmd_list_element *c;
2641
2642   gdbarch_register (bfd_arch_hppa, hppa_gdbarch_init, hppa_dump_tdep);
2643
2644   hppa_objfile_priv_data = register_objfile_data ();
2645
2646   add_cmd ("unwind", class_maintenance, unwind_command,
2647            "Print unwind table entry at given address.",
2648            &maintenanceprintlist);
2649
2650   /* Debug this files internals. */
2651   add_setshow_boolean_cmd ("hppa", class_maintenance, &hppa_debug, "\
2652 Set whether hppa target specific debugging information should be displayed.", "\
2653 Show whether hppa target specific debugging information is displayed.", "\
2654 This flag controls whether hppa target specific debugging information is\n\
2655 displayed.  This information is particularly useful for debugging frame\n\
2656 unwinding problems.", "hppa debug flag is %s.",
2657                            NULL, NULL, &setdebuglist, &showdebuglist);
2658 }