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