* target.h (struct regcache): Add forward declaration.
[external/binutils.git] / gdb / ia64-tdep.c
1 /* Target-dependent code for the IA-64 for GDB, the GNU debugger.
2
3    Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007
4    Free Software Foundation, Inc.
5
6    This file is part of GDB.
7
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 51 Franklin Street, Fifth Floor,
21    Boston, MA 02110-1301, USA.  */
22
23 #include "defs.h"
24 #include "inferior.h"
25 #include "gdbcore.h"
26 #include "arch-utils.h"
27 #include "floatformat.h"
28 #include "gdbtypes.h"
29 #include "regcache.h"
30 #include "reggroups.h"
31 #include "frame.h"
32 #include "frame-base.h"
33 #include "frame-unwind.h"
34 #include "doublest.h"
35 #include "value.h"
36 #include "gdb_assert.h"
37 #include "objfiles.h"
38 #include "elf/common.h"         /* for DT_PLTGOT value */
39 #include "elf-bfd.h"
40 #include "dis-asm.h"
41 #include "infcall.h"
42 #include "osabi.h"
43 #include "ia64-tdep.h"
44 #include "cp-abi.h"
45
46 #ifdef HAVE_LIBUNWIND_IA64_H
47 #include "elf/ia64.h"           /* for PT_IA_64_UNWIND value */
48 #include "libunwind-frame.h"
49 #include "libunwind-ia64.h"
50
51 /* Note: KERNEL_START is supposed to be an address which is not going
52          to ever contain any valid unwind info.  For ia64 linux, the choice
53          of 0xc000000000000000 is fairly safe since that's uncached space.
54  
55          We use KERNEL_START as follows: after obtaining the kernel's
56          unwind table via getunwind(), we project its unwind data into
57          address-range KERNEL_START-(KERNEL_START+ktab_size) and then
58          when ia64_access_mem() sees a memory access to this
59          address-range, we redirect it to ktab instead.
60
61          None of this hackery is needed with a modern kernel/libcs
62          which uses the kernel virtual DSO to provide access to the
63          kernel's unwind info.  In that case, ktab_size remains 0 and
64          hence the value of KERNEL_START doesn't matter.  */
65
66 #define KERNEL_START 0xc000000000000000ULL
67
68 static size_t ktab_size = 0;
69 struct ia64_table_entry
70   {
71     uint64_t start_offset;
72     uint64_t end_offset;
73     uint64_t info_offset;
74   };
75
76 static struct ia64_table_entry *ktab = NULL;
77
78 #endif
79
80 /* An enumeration of the different IA-64 instruction types.  */
81
82 typedef enum instruction_type
83 {
84   A,                    /* Integer ALU ;    I-unit or M-unit */
85   I,                    /* Non-ALU integer; I-unit */
86   M,                    /* Memory ;         M-unit */
87   F,                    /* Floating-point ; F-unit */
88   B,                    /* Branch ;         B-unit */
89   L,                    /* Extended (L+X) ; I-unit */
90   X,                    /* Extended (L+X) ; I-unit */
91   undefined             /* undefined or reserved */
92 } instruction_type;
93
94 /* We represent IA-64 PC addresses as the value of the instruction
95    pointer or'd with some bit combination in the low nibble which
96    represents the slot number in the bundle addressed by the
97    instruction pointer.  The problem is that the Linux kernel
98    multiplies its slot numbers (for exceptions) by one while the
99    disassembler multiplies its slot numbers by 6.  In addition, I've
100    heard it said that the simulator uses 1 as the multiplier.
101    
102    I've fixed the disassembler so that the bytes_per_line field will
103    be the slot multiplier.  If bytes_per_line comes in as zero, it
104    is set to six (which is how it was set up initially). -- objdump
105    displays pretty disassembly dumps with this value.  For our purposes,
106    we'll set bytes_per_line to SLOT_MULTIPLIER. This is okay since we
107    never want to also display the raw bytes the way objdump does. */
108
109 #define SLOT_MULTIPLIER 1
110
111 /* Length in bytes of an instruction bundle */
112
113 #define BUNDLE_LEN 16
114
115 static gdbarch_init_ftype ia64_gdbarch_init;
116
117 static gdbarch_register_name_ftype ia64_register_name;
118 static gdbarch_register_type_ftype ia64_register_type;
119 static gdbarch_breakpoint_from_pc_ftype ia64_breakpoint_from_pc;
120 static gdbarch_skip_prologue_ftype ia64_skip_prologue;
121 static struct type *is_float_or_hfa_type (struct type *t);
122 static CORE_ADDR ia64_find_global_pointer (CORE_ADDR faddr);
123
124 static struct type *builtin_type_ia64_ext;
125
126 #define NUM_IA64_RAW_REGS 462
127
128 static int sp_regnum = IA64_GR12_REGNUM;
129 static int fp_regnum = IA64_VFP_REGNUM;
130 static int lr_regnum = IA64_VRAP_REGNUM;
131
132 /* NOTE: we treat the register stack registers r32-r127 as pseudo-registers because
133    they may not be accessible via the ptrace register get/set interfaces.  */
134 enum pseudo_regs { FIRST_PSEUDO_REGNUM = NUM_IA64_RAW_REGS, VBOF_REGNUM = IA64_NAT127_REGNUM + 1, V32_REGNUM, 
135                    V127_REGNUM = V32_REGNUM + 95, 
136                    VP0_REGNUM, VP16_REGNUM = VP0_REGNUM + 16, VP63_REGNUM = VP0_REGNUM + 63, LAST_PSEUDO_REGNUM };
137
138 /* Array of register names; There should be ia64_num_regs strings in
139    the initializer.  */
140
141 static char *ia64_register_names[] = 
142 { "r0",   "r1",   "r2",   "r3",   "r4",   "r5",   "r6",   "r7",
143   "r8",   "r9",   "r10",  "r11",  "r12",  "r13",  "r14",  "r15",
144   "r16",  "r17",  "r18",  "r19",  "r20",  "r21",  "r22",  "r23",
145   "r24",  "r25",  "r26",  "r27",  "r28",  "r29",  "r30",  "r31",
146   "",     "",     "",     "",     "",     "",     "",     "",
147   "",     "",     "",     "",     "",     "",     "",     "",
148   "",     "",     "",     "",     "",     "",     "",     "",
149   "",     "",     "",     "",     "",     "",     "",     "",
150   "",     "",     "",     "",     "",     "",     "",     "",
151   "",     "",     "",     "",     "",     "",     "",     "",
152   "",     "",     "",     "",     "",     "",     "",     "",
153   "",     "",     "",     "",     "",     "",     "",     "",
154   "",     "",     "",     "",     "",     "",     "",     "",
155   "",     "",     "",     "",     "",     "",     "",     "",
156   "",     "",     "",     "",     "",     "",     "",     "",
157   "",     "",     "",     "",     "",     "",     "",     "",
158
159   "f0",   "f1",   "f2",   "f3",   "f4",   "f5",   "f6",   "f7",
160   "f8",   "f9",   "f10",  "f11",  "f12",  "f13",  "f14",  "f15",
161   "f16",  "f17",  "f18",  "f19",  "f20",  "f21",  "f22",  "f23",
162   "f24",  "f25",  "f26",  "f27",  "f28",  "f29",  "f30",  "f31",
163   "f32",  "f33",  "f34",  "f35",  "f36",  "f37",  "f38",  "f39",
164   "f40",  "f41",  "f42",  "f43",  "f44",  "f45",  "f46",  "f47",
165   "f48",  "f49",  "f50",  "f51",  "f52",  "f53",  "f54",  "f55",
166   "f56",  "f57",  "f58",  "f59",  "f60",  "f61",  "f62",  "f63",
167   "f64",  "f65",  "f66",  "f67",  "f68",  "f69",  "f70",  "f71",
168   "f72",  "f73",  "f74",  "f75",  "f76",  "f77",  "f78",  "f79",
169   "f80",  "f81",  "f82",  "f83",  "f84",  "f85",  "f86",  "f87",
170   "f88",  "f89",  "f90",  "f91",  "f92",  "f93",  "f94",  "f95",
171   "f96",  "f97",  "f98",  "f99",  "f100", "f101", "f102", "f103",
172   "f104", "f105", "f106", "f107", "f108", "f109", "f110", "f111",
173   "f112", "f113", "f114", "f115", "f116", "f117", "f118", "f119",
174   "f120", "f121", "f122", "f123", "f124", "f125", "f126", "f127",
175
176   "",     "",     "",     "",     "",     "",     "",     "",
177   "",     "",     "",     "",     "",     "",     "",     "",
178   "",     "",     "",     "",     "",     "",     "",     "",
179   "",     "",     "",     "",     "",     "",     "",     "",
180   "",     "",     "",     "",     "",     "",     "",     "",
181   "",     "",     "",     "",     "",     "",     "",     "",
182   "",     "",     "",     "",     "",     "",     "",     "",
183   "",     "",     "",     "",     "",     "",     "",     "",
184
185   "b0",   "b1",   "b2",   "b3",   "b4",   "b5",   "b6",   "b7",
186
187   "vfp", "vrap",
188
189   "pr", "ip", "psr", "cfm",
190
191   "kr0",   "kr1",   "kr2",   "kr3",   "kr4",   "kr5",   "kr6",   "kr7",
192   "", "", "", "", "", "", "", "",
193   "rsc", "bsp", "bspstore", "rnat",
194   "", "fcr", "", "",
195   "eflag", "csd", "ssd", "cflg", "fsr", "fir", "fdr",  "",
196   "ccv", "", "", "", "unat", "", "", "",
197   "fpsr", "", "", "", "itc",
198   "", "", "", "", "", "", "", "", "", "",
199   "", "", "", "", "", "", "", "", "",
200   "pfs", "lc", "ec",
201   "", "", "", "", "", "", "", "", "", "",
202   "", "", "", "", "", "", "", "", "", "",
203   "", "", "", "", "", "", "", "", "", "",
204   "", "", "", "", "", "", "", "", "", "",
205   "", "", "", "", "", "", "", "", "", "",
206   "", "", "", "", "", "", "", "", "", "",
207   "",
208   "nat0",  "nat1",  "nat2",  "nat3",  "nat4",  "nat5",  "nat6",  "nat7",
209   "nat8",  "nat9",  "nat10", "nat11", "nat12", "nat13", "nat14", "nat15",
210   "nat16", "nat17", "nat18", "nat19", "nat20", "nat21", "nat22", "nat23",
211   "nat24", "nat25", "nat26", "nat27", "nat28", "nat29", "nat30", "nat31",
212   "nat32", "nat33", "nat34", "nat35", "nat36", "nat37", "nat38", "nat39",
213   "nat40", "nat41", "nat42", "nat43", "nat44", "nat45", "nat46", "nat47",
214   "nat48", "nat49", "nat50", "nat51", "nat52", "nat53", "nat54", "nat55",
215   "nat56", "nat57", "nat58", "nat59", "nat60", "nat61", "nat62", "nat63",
216   "nat64", "nat65", "nat66", "nat67", "nat68", "nat69", "nat70", "nat71",
217   "nat72", "nat73", "nat74", "nat75", "nat76", "nat77", "nat78", "nat79",
218   "nat80", "nat81", "nat82", "nat83", "nat84", "nat85", "nat86", "nat87",
219   "nat88", "nat89", "nat90", "nat91", "nat92", "nat93", "nat94", "nat95",
220   "nat96", "nat97", "nat98", "nat99", "nat100","nat101","nat102","nat103",
221   "nat104","nat105","nat106","nat107","nat108","nat109","nat110","nat111",
222   "nat112","nat113","nat114","nat115","nat116","nat117","nat118","nat119",
223   "nat120","nat121","nat122","nat123","nat124","nat125","nat126","nat127",
224
225   "bof",
226   
227   "r32",  "r33",  "r34",  "r35",  "r36",  "r37",  "r38",  "r39",   
228   "r40",  "r41",  "r42",  "r43",  "r44",  "r45",  "r46",  "r47",
229   "r48",  "r49",  "r50",  "r51",  "r52",  "r53",  "r54",  "r55",
230   "r56",  "r57",  "r58",  "r59",  "r60",  "r61",  "r62",  "r63",
231   "r64",  "r65",  "r66",  "r67",  "r68",  "r69",  "r70",  "r71",
232   "r72",  "r73",  "r74",  "r75",  "r76",  "r77",  "r78",  "r79",
233   "r80",  "r81",  "r82",  "r83",  "r84",  "r85",  "r86",  "r87",
234   "r88",  "r89",  "r90",  "r91",  "r92",  "r93",  "r94",  "r95",
235   "r96",  "r97",  "r98",  "r99",  "r100", "r101", "r102", "r103",
236   "r104", "r105", "r106", "r107", "r108", "r109", "r110", "r111",
237   "r112", "r113", "r114", "r115", "r116", "r117", "r118", "r119",
238   "r120", "r121", "r122", "r123", "r124", "r125", "r126", "r127",
239
240   "p0",   "p1",   "p2",   "p3",   "p4",   "p5",   "p6",   "p7",
241   "p8",   "p9",   "p10",  "p11",  "p12",  "p13",  "p14",  "p15",
242   "p16",  "p17",  "p18",  "p19",  "p20",  "p21",  "p22",  "p23",
243   "p24",  "p25",  "p26",  "p27",  "p28",  "p29",  "p30",  "p31",
244   "p32",  "p33",  "p34",  "p35",  "p36",  "p37",  "p38",  "p39",
245   "p40",  "p41",  "p42",  "p43",  "p44",  "p45",  "p46",  "p47",
246   "p48",  "p49",  "p50",  "p51",  "p52",  "p53",  "p54",  "p55",
247   "p56",  "p57",  "p58",  "p59",  "p60",  "p61",  "p62",  "p63",
248 };
249
250 struct ia64_frame_cache
251 {
252   CORE_ADDR base;       /* frame pointer base for frame */
253   CORE_ADDR pc;         /* function start pc for frame */
254   CORE_ADDR saved_sp;   /* stack pointer for frame */
255   CORE_ADDR bsp;        /* points at r32 for the current frame */
256   CORE_ADDR cfm;        /* cfm value for current frame */
257   CORE_ADDR prev_cfm;   /* cfm value for previous frame */
258   int   frameless;
259   int   sof;            /* Size of frame  (decoded from cfm value) */
260   int   sol;            /* Size of locals (decoded from cfm value) */
261   int   sor;            /* Number of rotating registers. (decoded from cfm value) */
262   CORE_ADDR after_prologue;
263   /* Address of first instruction after the last
264      prologue instruction;  Note that there may
265      be instructions from the function's body
266      intermingled with the prologue. */
267   int mem_stack_frame_size;
268   /* Size of the memory stack frame (may be zero),
269      or -1 if it has not been determined yet. */
270   int   fp_reg;         /* Register number (if any) used a frame pointer
271                            for this frame.  0 if no register is being used
272                            as the frame pointer. */
273   
274   /* Saved registers.  */
275   CORE_ADDR saved_regs[NUM_IA64_RAW_REGS];
276
277 };
278
279 #define SIGCONTEXT_REGISTER_ADDRESS \
280   (gdbarch_tdep (current_gdbarch)->sigcontext_register_address)
281
282 int
283 ia64_register_reggroup_p (struct gdbarch *gdbarch, int regnum,
284                           struct reggroup *group)
285 {
286   int vector_p;
287   int float_p;
288   int raw_p;
289   if (group == all_reggroup)
290     return 1;
291   vector_p = TYPE_VECTOR (register_type (gdbarch, regnum));
292   float_p = TYPE_CODE (register_type (gdbarch, regnum)) == TYPE_CODE_FLT;
293   raw_p = regnum < NUM_IA64_RAW_REGS;
294   if (group == float_reggroup)
295     return float_p;
296   if (group == vector_reggroup)
297     return vector_p;
298   if (group == general_reggroup)
299     return (!vector_p && !float_p);
300   if (group == save_reggroup || group == restore_reggroup)
301     return raw_p; 
302   return 0;
303 }
304
305 static const char *
306 ia64_register_name (int reg)
307 {
308   return ia64_register_names[reg];
309 }
310
311 struct type *
312 ia64_register_type (struct gdbarch *arch, int reg)
313 {
314   if (reg >= IA64_FR0_REGNUM && reg <= IA64_FR127_REGNUM)
315     return builtin_type_ia64_ext;
316   else
317     return builtin_type_long;
318 }
319
320 static int
321 ia64_dwarf_reg_to_regnum (int reg)
322 {
323   if (reg >= IA64_GR32_REGNUM && reg <= IA64_GR127_REGNUM)
324     return V32_REGNUM + (reg - IA64_GR32_REGNUM);
325   return reg;
326 }
327
328 static int
329 floatformat_valid (const struct floatformat *fmt, const void *from)
330 {
331   return 1;
332 }
333
334 const struct floatformat floatformat_ia64_ext =
335 {
336   floatformat_little, 82, 0, 1, 17, 65535, 0x1ffff, 18, 64,
337   floatformat_intbit_yes, "floatformat_ia64_ext", floatformat_valid
338 };
339
340 const struct floatformat *floatformats_ia64_ext[2] =
341 {
342   &floatformat_ia64_ext,
343   &floatformat_ia64_ext
344 };
345
346
347 /* Extract ``len'' bits from an instruction bundle starting at
348    bit ``from''.  */
349
350 static long long
351 extract_bit_field (char *bundle, int from, int len)
352 {
353   long long result = 0LL;
354   int to = from + len;
355   int from_byte = from / 8;
356   int to_byte = to / 8;
357   unsigned char *b = (unsigned char *) bundle;
358   unsigned char c;
359   int lshift;
360   int i;
361
362   c = b[from_byte];
363   if (from_byte == to_byte)
364     c = ((unsigned char) (c << (8 - to % 8))) >> (8 - to % 8);
365   result = c >> (from % 8);
366   lshift = 8 - (from % 8);
367
368   for (i = from_byte+1; i < to_byte; i++)
369     {
370       result |= ((long long) b[i]) << lshift;
371       lshift += 8;
372     }
373
374   if (from_byte < to_byte && (to % 8 != 0))
375     {
376       c = b[to_byte];
377       c = ((unsigned char) (c << (8 - to % 8))) >> (8 - to % 8);
378       result |= ((long long) c) << lshift;
379     }
380
381   return result;
382 }
383
384 /* Replace the specified bits in an instruction bundle */
385
386 static void
387 replace_bit_field (char *bundle, long long val, int from, int len)
388 {
389   int to = from + len;
390   int from_byte = from / 8;
391   int to_byte = to / 8;
392   unsigned char *b = (unsigned char *) bundle;
393   unsigned char c;
394
395   if (from_byte == to_byte)
396     {
397       unsigned char left, right;
398       c = b[from_byte];
399       left = (c >> (to % 8)) << (to % 8);
400       right = ((unsigned char) (c << (8 - from % 8))) >> (8 - from % 8);
401       c = (unsigned char) (val & 0xff);
402       c = (unsigned char) (c << (from % 8 + 8 - to % 8)) >> (8 - to % 8);
403       c |= right | left;
404       b[from_byte] = c;
405     }
406   else
407     {
408       int i;
409       c = b[from_byte];
410       c = ((unsigned char) (c << (8 - from % 8))) >> (8 - from % 8);
411       c = c | (val << (from % 8));
412       b[from_byte] = c;
413       val >>= 8 - from % 8;
414
415       for (i = from_byte+1; i < to_byte; i++)
416         {
417           c = val & 0xff;
418           val >>= 8;
419           b[i] = c;
420         }
421
422       if (to % 8 != 0)
423         {
424           unsigned char cv = (unsigned char) val;
425           c = b[to_byte];
426           c = c >> (to % 8) << (to % 8);
427           c |= ((unsigned char) (cv << (8 - to % 8))) >> (8 - to % 8);
428           b[to_byte] = c;
429         }
430     }
431 }
432
433 /* Return the contents of slot N (for N = 0, 1, or 2) in
434    and instruction bundle */
435
436 static long long
437 slotN_contents (char *bundle, int slotnum)
438 {
439   return extract_bit_field (bundle, 5+41*slotnum, 41);
440 }
441
442 /* Store an instruction in an instruction bundle */
443
444 static void
445 replace_slotN_contents (char *bundle, long long instr, int slotnum)
446 {
447   replace_bit_field (bundle, instr, 5+41*slotnum, 41);
448 }
449
450 static enum instruction_type template_encoding_table[32][3] =
451 {
452   { M, I, I },                          /* 00 */
453   { M, I, I },                          /* 01 */
454   { M, I, I },                          /* 02 */
455   { M, I, I },                          /* 03 */
456   { M, L, X },                          /* 04 */
457   { M, L, X },                          /* 05 */
458   { undefined, undefined, undefined },  /* 06 */
459   { undefined, undefined, undefined },  /* 07 */
460   { M, M, I },                          /* 08 */
461   { M, M, I },                          /* 09 */
462   { M, M, I },                          /* 0A */
463   { M, M, I },                          /* 0B */
464   { M, F, I },                          /* 0C */
465   { M, F, I },                          /* 0D */
466   { M, M, F },                          /* 0E */
467   { M, M, F },                          /* 0F */
468   { M, I, B },                          /* 10 */
469   { M, I, B },                          /* 11 */
470   { M, B, B },                          /* 12 */
471   { M, B, B },                          /* 13 */
472   { undefined, undefined, undefined },  /* 14 */
473   { undefined, undefined, undefined },  /* 15 */
474   { B, B, B },                          /* 16 */
475   { B, B, B },                          /* 17 */
476   { M, M, B },                          /* 18 */
477   { M, M, B },                          /* 19 */
478   { undefined, undefined, undefined },  /* 1A */
479   { undefined, undefined, undefined },  /* 1B */
480   { M, F, B },                          /* 1C */
481   { M, F, B },                          /* 1D */
482   { undefined, undefined, undefined },  /* 1E */
483   { undefined, undefined, undefined },  /* 1F */
484 };
485
486 /* Fetch and (partially) decode an instruction at ADDR and return the
487    address of the next instruction to fetch.  */
488
489 static CORE_ADDR
490 fetch_instruction (CORE_ADDR addr, instruction_type *it, long long *instr)
491 {
492   char bundle[BUNDLE_LEN];
493   int slotnum = (int) (addr & 0x0f) / SLOT_MULTIPLIER;
494   long long template;
495   int val;
496
497   /* Warn about slot numbers greater than 2.  We used to generate
498      an error here on the assumption that the user entered an invalid
499      address.  But, sometimes GDB itself requests an invalid address.
500      This can (easily) happen when execution stops in a function for
501      which there are no symbols.  The prologue scanner will attempt to
502      find the beginning of the function - if the nearest symbol
503      happens to not be aligned on a bundle boundary (16 bytes), the
504      resulting starting address will cause GDB to think that the slot
505      number is too large.
506
507      So we warn about it and set the slot number to zero.  It is
508      not necessarily a fatal condition, particularly if debugging
509      at the assembly language level.  */
510   if (slotnum > 2)
511     {
512       warning (_("Can't fetch instructions for slot numbers greater than 2.\n"
513                "Using slot 0 instead"));
514       slotnum = 0;
515     }
516
517   addr &= ~0x0f;
518
519   val = target_read_memory (addr, bundle, BUNDLE_LEN);
520
521   if (val != 0)
522     return 0;
523
524   *instr = slotN_contents (bundle, slotnum);
525   template = extract_bit_field (bundle, 0, 5);
526   *it = template_encoding_table[(int)template][slotnum];
527
528   if (slotnum == 2 || (slotnum == 1 && *it == L))
529     addr += 16;
530   else
531     addr += (slotnum + 1) * SLOT_MULTIPLIER;
532
533   return addr;
534 }
535
536 /* There are 5 different break instructions (break.i, break.b,
537    break.m, break.f, and break.x), but they all have the same
538    encoding.  (The five bit template in the low five bits of the
539    instruction bundle distinguishes one from another.)
540    
541    The runtime architecture manual specifies that break instructions
542    used for debugging purposes must have the upper two bits of the 21
543    bit immediate set to a 0 and a 1 respectively.  A breakpoint
544    instruction encodes the most significant bit of its 21 bit
545    immediate at bit 36 of the 41 bit instruction.  The penultimate msb
546    is at bit 25 which leads to the pattern below.  
547    
548    Originally, I had this set up to do, e.g, a "break.i 0x80000"  But
549    it turns out that 0x80000 was used as the syscall break in the early
550    simulators.  So I changed the pattern slightly to do "break.i 0x080001"
551    instead.  But that didn't work either (I later found out that this
552    pattern was used by the simulator that I was using.)  So I ended up
553    using the pattern seen below. */
554
555 #if 0
556 #define IA64_BREAKPOINT 0x00002000040LL
557 #endif
558 #define IA64_BREAKPOINT 0x00003333300LL
559
560 static int
561 ia64_memory_insert_breakpoint (struct bp_target_info *bp_tgt)
562 {
563   CORE_ADDR addr = bp_tgt->placed_address;
564   char bundle[BUNDLE_LEN];
565   int slotnum = (int) (addr & 0x0f) / SLOT_MULTIPLIER;
566   long long instr;
567   int val;
568   int template;
569
570   if (slotnum > 2)
571     error (_("Can't insert breakpoint for slot numbers greater than 2."));
572
573   addr &= ~0x0f;
574
575   val = target_read_memory (addr, bundle, BUNDLE_LEN);
576
577   /* Check for L type instruction in 2nd slot, if present then
578      bump up the slot number to the 3rd slot */
579   template = extract_bit_field (bundle, 0, 5);
580   if (slotnum == 1 && template_encoding_table[template][1] == L)
581     {
582       slotnum = 2;
583     }
584
585   instr = slotN_contents (bundle, slotnum);
586   memcpy (bp_tgt->shadow_contents, &instr, sizeof (instr));
587   bp_tgt->placed_size = bp_tgt->shadow_len = sizeof (instr);
588   replace_slotN_contents (bundle, IA64_BREAKPOINT, slotnum);
589   if (val == 0)
590     target_write_memory (addr, bundle, BUNDLE_LEN);
591
592   return val;
593 }
594
595 static int
596 ia64_memory_remove_breakpoint (struct bp_target_info *bp_tgt)
597 {
598   CORE_ADDR addr = bp_tgt->placed_address;
599   char bundle[BUNDLE_LEN];
600   int slotnum = (addr & 0x0f) / SLOT_MULTIPLIER;
601   long long instr;
602   int val;
603   int template;
604
605   addr &= ~0x0f;
606
607   val = target_read_memory (addr, bundle, BUNDLE_LEN);
608
609   /* Check for L type instruction in 2nd slot, if present then
610      bump up the slot number to the 3rd slot */
611   template = extract_bit_field (bundle, 0, 5);
612   if (slotnum == 1 && template_encoding_table[template][1] == L)
613     {
614       slotnum = 2;
615     }
616
617   memcpy (&instr, bp_tgt->shadow_contents, sizeof instr);
618   replace_slotN_contents (bundle, instr, slotnum);
619   if (val == 0)
620     target_write_memory (addr, bundle, BUNDLE_LEN);
621
622   return val;
623 }
624
625 /* We don't really want to use this, but remote.c needs to call it in order
626    to figure out if Z-packets are supported or not.  Oh, well. */
627 const unsigned char *
628 ia64_breakpoint_from_pc (CORE_ADDR *pcptr, int *lenptr)
629 {
630   static unsigned char breakpoint[] =
631     { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
632   *lenptr = sizeof (breakpoint);
633 #if 0
634   *pcptr &= ~0x0f;
635 #endif
636   return breakpoint;
637 }
638
639 static CORE_ADDR
640 ia64_read_pc (ptid_t ptid)
641 {
642   CORE_ADDR psr_value = read_register_pid (IA64_PSR_REGNUM, ptid);
643   CORE_ADDR pc_value   = read_register_pid (IA64_IP_REGNUM, ptid);
644   int slot_num = (psr_value >> 41) & 3;
645
646   return pc_value | (slot_num * SLOT_MULTIPLIER);
647 }
648
649 void
650 ia64_write_pc (CORE_ADDR new_pc, ptid_t ptid)
651 {
652   int slot_num = (int) (new_pc & 0xf) / SLOT_MULTIPLIER;
653   CORE_ADDR psr_value = read_register_pid (IA64_PSR_REGNUM, ptid);
654   psr_value &= ~(3LL << 41);
655   psr_value |= (CORE_ADDR)(slot_num & 0x3) << 41;
656
657   new_pc &= ~0xfLL;
658
659   write_register_pid (IA64_PSR_REGNUM, psr_value, ptid);
660   write_register_pid (IA64_IP_REGNUM, new_pc, ptid);
661 }
662
663 #define IS_NaT_COLLECTION_ADDR(addr) ((((addr) >> 3) & 0x3f) == 0x3f)
664
665 /* Returns the address of the slot that's NSLOTS slots away from
666    the address ADDR. NSLOTS may be positive or negative. */
667 static CORE_ADDR
668 rse_address_add(CORE_ADDR addr, int nslots)
669 {
670   CORE_ADDR new_addr;
671   int mandatory_nat_slots = nslots / 63;
672   int direction = nslots < 0 ? -1 : 1;
673
674   new_addr = addr + 8 * (nslots + mandatory_nat_slots);
675
676   if ((new_addr >> 9)  != ((addr + 8 * 64 * mandatory_nat_slots) >> 9))
677     new_addr += 8 * direction;
678
679   if (IS_NaT_COLLECTION_ADDR(new_addr))
680     new_addr += 8 * direction;
681
682   return new_addr;
683 }
684
685 static void
686 ia64_pseudo_register_read (struct gdbarch *gdbarch, struct regcache *regcache,
687                            int regnum, gdb_byte *buf)
688 {
689   if (regnum >= V32_REGNUM && regnum <= V127_REGNUM)
690     {
691 #ifdef HAVE_LIBUNWIND_IA64_H
692       /* First try and use the libunwind special reg accessor, otherwise fallback to
693          standard logic.  */
694       if (!libunwind_is_initialized ()
695           || libunwind_get_reg_special (gdbarch, regnum, buf) != 0)
696 #endif
697         {
698           /* The fallback position is to assume that r32-r127 are found sequentially
699              in memory starting at $bof.  This isn't always true, but without libunwind,
700              this is the best we can do.  */
701           ULONGEST cfm;
702           ULONGEST bsp;
703           CORE_ADDR reg;
704           regcache_cooked_read_unsigned (regcache, IA64_BSP_REGNUM, &bsp);
705           regcache_cooked_read_unsigned (regcache, IA64_CFM_REGNUM, &cfm);
706           
707           /* The bsp points at the end of the register frame so we
708              subtract the size of frame from it to get start of register frame.  */
709           bsp = rse_address_add (bsp, -(cfm & 0x7f));
710           
711           if ((cfm & 0x7f) > regnum - V32_REGNUM) 
712             {
713               ULONGEST reg_addr = rse_address_add (bsp, (regnum - V32_REGNUM));
714               reg = read_memory_integer ((CORE_ADDR)reg_addr, 8);
715               store_unsigned_integer (buf, register_size (current_gdbarch, regnum), reg);
716             }
717           else
718             store_unsigned_integer (buf, register_size (current_gdbarch, regnum), 0);
719         }
720     }
721   else if (IA64_NAT0_REGNUM <= regnum && regnum <= IA64_NAT31_REGNUM)
722     {
723       ULONGEST unatN_val;
724       ULONGEST unat;
725       regcache_cooked_read_unsigned (regcache, IA64_UNAT_REGNUM, &unat);
726       unatN_val = (unat & (1LL << (regnum - IA64_NAT0_REGNUM))) != 0;
727       store_unsigned_integer (buf, register_size (current_gdbarch, regnum), unatN_val);
728     }
729   else if (IA64_NAT32_REGNUM <= regnum && regnum <= IA64_NAT127_REGNUM)
730     {
731       ULONGEST natN_val = 0;
732       ULONGEST bsp;
733       ULONGEST cfm;
734       CORE_ADDR gr_addr = 0;
735       regcache_cooked_read_unsigned (regcache, IA64_BSP_REGNUM, &bsp);
736       regcache_cooked_read_unsigned (regcache, IA64_CFM_REGNUM, &cfm);
737
738       /* The bsp points at the end of the register frame so we
739          subtract the size of frame from it to get start of register frame.  */
740       bsp = rse_address_add (bsp, -(cfm & 0x7f));
741  
742       if ((cfm & 0x7f) > regnum - V32_REGNUM) 
743         gr_addr = rse_address_add (bsp, (regnum - V32_REGNUM));
744       
745       if (gr_addr != 0)
746         {
747           /* Compute address of nat collection bits.  */
748           CORE_ADDR nat_addr = gr_addr | 0x1f8;
749           CORE_ADDR nat_collection;
750           int nat_bit;
751           /* If our nat collection address is bigger than bsp, we have to get
752              the nat collection from rnat.  Otherwise, we fetch the nat
753              collection from the computed address.  */
754           if (nat_addr >= bsp)
755             regcache_cooked_read_unsigned (regcache, IA64_RNAT_REGNUM, &nat_collection);
756           else
757             nat_collection = read_memory_integer (nat_addr, 8);
758           nat_bit = (gr_addr >> 3) & 0x3f;
759           natN_val = (nat_collection >> nat_bit) & 1;
760         }
761       
762       store_unsigned_integer (buf, register_size (current_gdbarch, regnum), natN_val);
763     }
764   else if (regnum == VBOF_REGNUM)
765     {
766       /* A virtual register frame start is provided for user convenience.
767          It can be calculated as the bsp - sof (sizeof frame). */
768       ULONGEST bsp, vbsp;
769       ULONGEST cfm;
770       CORE_ADDR reg;
771       regcache_cooked_read_unsigned (regcache, IA64_BSP_REGNUM, &bsp);
772       regcache_cooked_read_unsigned (regcache, IA64_CFM_REGNUM, &cfm);
773
774       /* The bsp points at the end of the register frame so we
775          subtract the size of frame from it to get beginning of frame.  */
776       vbsp = rse_address_add (bsp, -(cfm & 0x7f));
777       store_unsigned_integer (buf, register_size (current_gdbarch, regnum), vbsp);
778     }
779   else if (VP0_REGNUM <= regnum && regnum <= VP63_REGNUM)
780     {
781       ULONGEST pr;
782       ULONGEST cfm;
783       ULONGEST prN_val;
784       CORE_ADDR reg;
785       regcache_cooked_read_unsigned (regcache, IA64_PR_REGNUM, &pr);
786       regcache_cooked_read_unsigned (regcache, IA64_CFM_REGNUM, &cfm);
787
788       if (VP16_REGNUM <= regnum && regnum <= VP63_REGNUM)
789         {
790           /* Fetch predicate register rename base from current frame
791              marker for this frame. */
792           int rrb_pr = (cfm >> 32) & 0x3f;
793
794           /* Adjust the register number to account for register rotation. */
795           regnum = VP16_REGNUM 
796                  + ((regnum - VP16_REGNUM) + rrb_pr) % 48;
797         }
798       prN_val = (pr & (1LL << (regnum - VP0_REGNUM))) != 0;
799       store_unsigned_integer (buf, register_size (current_gdbarch, regnum), prN_val);
800     }
801   else
802     memset (buf, 0, register_size (current_gdbarch, regnum));
803 }
804
805 static void
806 ia64_pseudo_register_write (struct gdbarch *gdbarch, struct regcache *regcache,
807                             int regnum, const gdb_byte *buf)
808 {
809   if (regnum >= V32_REGNUM && regnum <= V127_REGNUM)
810     {
811       ULONGEST bsp;
812       ULONGEST cfm;
813       CORE_ADDR reg;
814       regcache_cooked_read_unsigned (regcache, IA64_BSP_REGNUM, &bsp);
815       regcache_cooked_read_unsigned (regcache, IA64_CFM_REGNUM, &cfm);
816
817       bsp = rse_address_add (bsp, -(cfm & 0x7f));
818  
819       if ((cfm & 0x7f) > regnum - V32_REGNUM) 
820         {
821           ULONGEST reg_addr = rse_address_add (bsp, (regnum - V32_REGNUM));
822           write_memory (reg_addr, (void *)buf, 8);
823         }
824     }
825   else if (IA64_NAT0_REGNUM <= regnum && regnum <= IA64_NAT31_REGNUM)
826     {
827       ULONGEST unatN_val, unat, unatN_mask;
828       regcache_cooked_read_unsigned (regcache, IA64_UNAT_REGNUM, &unat);
829       unatN_val = extract_unsigned_integer (buf, register_size (current_gdbarch, regnum)); 
830       unatN_mask = (1LL << (regnum - IA64_NAT0_REGNUM));
831       if (unatN_val == 0)
832         unat &= ~unatN_mask;
833       else if (unatN_val == 1)
834         unat |= unatN_mask;
835       regcache_cooked_write_unsigned (regcache, IA64_UNAT_REGNUM, unat);
836     }
837   else if (IA64_NAT32_REGNUM <= regnum && regnum <= IA64_NAT127_REGNUM)
838     {
839       ULONGEST natN_val;
840       ULONGEST bsp;
841       ULONGEST cfm;
842       CORE_ADDR gr_addr = 0;
843       regcache_cooked_read_unsigned (regcache, IA64_BSP_REGNUM, &bsp);
844       regcache_cooked_read_unsigned (regcache, IA64_CFM_REGNUM, &cfm);
845
846       /* The bsp points at the end of the register frame so we
847          subtract the size of frame from it to get start of register frame.  */
848       bsp = rse_address_add (bsp, -(cfm & 0x7f));
849  
850       if ((cfm & 0x7f) > regnum - V32_REGNUM) 
851         gr_addr = rse_address_add (bsp, (regnum - V32_REGNUM));
852       
853       natN_val = extract_unsigned_integer (buf, register_size (current_gdbarch, regnum)); 
854
855       if (gr_addr != 0 && (natN_val == 0 || natN_val == 1))
856         {
857           /* Compute address of nat collection bits.  */
858           CORE_ADDR nat_addr = gr_addr | 0x1f8;
859           CORE_ADDR nat_collection;
860           int natN_bit = (gr_addr >> 3) & 0x3f;
861           ULONGEST natN_mask = (1LL << natN_bit);
862           /* If our nat collection address is bigger than bsp, we have to get
863              the nat collection from rnat.  Otherwise, we fetch the nat
864              collection from the computed address.  */
865           if (nat_addr >= bsp)
866             {
867               regcache_cooked_read_unsigned (regcache, IA64_RNAT_REGNUM, &nat_collection);
868               if (natN_val)
869                 nat_collection |= natN_mask;
870               else
871                 nat_collection &= ~natN_mask;
872               regcache_cooked_write_unsigned (regcache, IA64_RNAT_REGNUM, nat_collection);
873             }
874           else
875             {
876               char nat_buf[8];
877               nat_collection = read_memory_integer (nat_addr, 8);
878               if (natN_val)
879                 nat_collection |= natN_mask;
880               else
881                 nat_collection &= ~natN_mask;
882               store_unsigned_integer (nat_buf, register_size (current_gdbarch, regnum), nat_collection);
883               write_memory (nat_addr, nat_buf, 8);
884             }
885         }
886     }
887   else if (VP0_REGNUM <= regnum && regnum <= VP63_REGNUM)
888     {
889       ULONGEST pr;
890       ULONGEST cfm;
891       ULONGEST prN_val;
892       ULONGEST prN_mask;
893
894       regcache_cooked_read_unsigned (regcache, IA64_PR_REGNUM, &pr);
895       regcache_cooked_read_unsigned (regcache, IA64_CFM_REGNUM, &cfm);
896
897       if (VP16_REGNUM <= regnum && regnum <= VP63_REGNUM)
898         {
899           /* Fetch predicate register rename base from current frame
900              marker for this frame. */
901           int rrb_pr = (cfm >> 32) & 0x3f;
902
903           /* Adjust the register number to account for register rotation. */
904           regnum = VP16_REGNUM 
905                  + ((regnum - VP16_REGNUM) + rrb_pr) % 48;
906         }
907       prN_val = extract_unsigned_integer (buf, register_size (current_gdbarch, regnum)); 
908       prN_mask = (1LL << (regnum - VP0_REGNUM));
909       if (prN_val == 0)
910         pr &= ~prN_mask;
911       else if (prN_val == 1)
912         pr |= prN_mask;
913       regcache_cooked_write_unsigned (regcache, IA64_PR_REGNUM, pr);
914     }
915 }
916
917 /* The ia64 needs to convert between various ieee floating-point formats
918    and the special ia64 floating point register format.  */
919
920 static int
921 ia64_convert_register_p (int regno, struct type *type)
922 {
923   return (regno >= IA64_FR0_REGNUM && regno <= IA64_FR127_REGNUM);
924 }
925
926 static void
927 ia64_register_to_value (struct frame_info *frame, int regnum,
928                          struct type *valtype, gdb_byte *out)
929 {
930   char in[MAX_REGISTER_SIZE];
931   frame_register_read (frame, regnum, in);
932   convert_typed_floating (in, builtin_type_ia64_ext, out, valtype);
933 }
934
935 static void
936 ia64_value_to_register (struct frame_info *frame, int regnum,
937                          struct type *valtype, const gdb_byte *in)
938 {
939   char out[MAX_REGISTER_SIZE];
940   convert_typed_floating (in, valtype, out, builtin_type_ia64_ext);
941   put_frame_register (frame, regnum, out);
942 }
943
944
945 /* Limit the number of skipped non-prologue instructions since examining
946    of the prologue is expensive.  */
947 static int max_skip_non_prologue_insns = 40;
948
949 /* Given PC representing the starting address of a function, and
950    LIM_PC which is the (sloppy) limit to which to scan when looking
951    for a prologue, attempt to further refine this limit by using
952    the line data in the symbol table.  If successful, a better guess
953    on where the prologue ends is returned, otherwise the previous
954    value of lim_pc is returned.  TRUST_LIMIT is a pointer to a flag
955    which will be set to indicate whether the returned limit may be
956    used with no further scanning in the event that the function is
957    frameless.  */
958
959 /* FIXME: cagney/2004-02-14: This function and logic have largely been
960    superseded by skip_prologue_using_sal.  */
961
962 static CORE_ADDR
963 refine_prologue_limit (CORE_ADDR pc, CORE_ADDR lim_pc, int *trust_limit)
964 {
965   struct symtab_and_line prologue_sal;
966   CORE_ADDR start_pc = pc;
967
968   /* Start off not trusting the limit.  */
969   *trust_limit = 0;
970
971   prologue_sal = find_pc_line (pc, 0);
972   if (prologue_sal.line != 0)
973     {
974       int i;
975       CORE_ADDR addr = prologue_sal.end;
976
977       /* Handle the case in which compiler's optimizer/scheduler
978          has moved instructions into the prologue.  We scan ahead
979          in the function looking for address ranges whose corresponding
980          line number is less than or equal to the first one that we
981          found for the function.  (It can be less than when the
982          scheduler puts a body instruction before the first prologue
983          instruction.)  */
984       for (i = 2 * max_skip_non_prologue_insns; 
985            i > 0 && (lim_pc == 0 || addr < lim_pc);
986            i--)
987         {
988           struct symtab_and_line sal;
989
990           sal = find_pc_line (addr, 0);
991           if (sal.line == 0)
992             break;
993           if (sal.line <= prologue_sal.line 
994               && sal.symtab == prologue_sal.symtab)
995             {
996               prologue_sal = sal;
997             }
998           addr = sal.end;
999         }
1000
1001       if (lim_pc == 0 || prologue_sal.end < lim_pc)
1002         {
1003           lim_pc = prologue_sal.end;
1004           if (start_pc == get_pc_function_start (lim_pc))
1005             *trust_limit = 1;
1006         }
1007     }
1008   return lim_pc;
1009 }
1010
1011 #define isScratch(_regnum_) ((_regnum_) == 2 || (_regnum_) == 3 \
1012   || (8 <= (_regnum_) && (_regnum_) <= 11) \
1013   || (14 <= (_regnum_) && (_regnum_) <= 31))
1014 #define imm9(_instr_) \
1015   ( ((((_instr_) & 0x01000000000LL) ? -1 : 0) << 8) \
1016    | (((_instr_) & 0x00008000000LL) >> 20) \
1017    | (((_instr_) & 0x00000001fc0LL) >> 6))
1018
1019 /* Allocate and initialize a frame cache.  */
1020
1021 static struct ia64_frame_cache *
1022 ia64_alloc_frame_cache (void)
1023 {
1024   struct ia64_frame_cache *cache;
1025   int i;
1026
1027   cache = FRAME_OBSTACK_ZALLOC (struct ia64_frame_cache);
1028
1029   /* Base address.  */
1030   cache->base = 0;
1031   cache->pc = 0;
1032   cache->cfm = 0;
1033   cache->prev_cfm = 0;
1034   cache->sof = 0;
1035   cache->sol = 0;
1036   cache->sor = 0;
1037   cache->bsp = 0;
1038   cache->fp_reg = 0;
1039   cache->frameless = 1;
1040
1041   for (i = 0; i < NUM_IA64_RAW_REGS; i++)
1042     cache->saved_regs[i] = 0;
1043
1044   return cache;
1045 }
1046
1047 static CORE_ADDR
1048 examine_prologue (CORE_ADDR pc, CORE_ADDR lim_pc, struct frame_info *next_frame, struct ia64_frame_cache *cache)
1049 {
1050   CORE_ADDR next_pc;
1051   CORE_ADDR last_prologue_pc = pc;
1052   instruction_type it;
1053   long long instr;
1054   int cfm_reg  = 0;
1055   int ret_reg  = 0;
1056   int fp_reg   = 0;
1057   int unat_save_reg = 0;
1058   int pr_save_reg = 0;
1059   int mem_stack_frame_size = 0;
1060   int spill_reg   = 0;
1061   CORE_ADDR spill_addr = 0;
1062   char instores[8];
1063   char infpstores[8];
1064   char reg_contents[256];
1065   int trust_limit;
1066   int frameless = 1;
1067   int i;
1068   CORE_ADDR addr;
1069   char buf[8];
1070   CORE_ADDR bof, sor, sol, sof, cfm, rrb_gr;
1071
1072   memset (instores, 0, sizeof instores);
1073   memset (infpstores, 0, sizeof infpstores);
1074   memset (reg_contents, 0, sizeof reg_contents);
1075
1076   if (cache->after_prologue != 0
1077       && cache->after_prologue <= lim_pc)
1078     return cache->after_prologue;
1079
1080   lim_pc = refine_prologue_limit (pc, lim_pc, &trust_limit);
1081   next_pc = fetch_instruction (pc, &it, &instr);
1082
1083   /* We want to check if we have a recognizable function start before we
1084      look ahead for a prologue.  */
1085   if (pc < lim_pc && next_pc 
1086       && it == M && ((instr & 0x1ee0000003fLL) == 0x02c00000000LL))
1087     {
1088       /* alloc - start of a regular function.  */
1089       int sor = (int) ((instr & 0x00078000000LL) >> 27);
1090       int sol = (int) ((instr & 0x00007f00000LL) >> 20);
1091       int sof = (int) ((instr & 0x000000fe000LL) >> 13);
1092       int rN = (int) ((instr & 0x00000001fc0LL) >> 6);
1093
1094       /* Verify that the current cfm matches what we think is the
1095          function start.  If we have somehow jumped within a function,
1096          we do not want to interpret the prologue and calculate the
1097          addresses of various registers such as the return address.  
1098          We will instead treat the frame as frameless. */
1099       if (!next_frame ||
1100           (sof == (cache->cfm & 0x7f) &&
1101            sol == ((cache->cfm >> 7) & 0x7f)))
1102         frameless = 0;
1103
1104       cfm_reg = rN;
1105       last_prologue_pc = next_pc;
1106       pc = next_pc;
1107     }
1108   else
1109     {
1110       /* Look for a leaf routine.  */
1111       if (pc < lim_pc && next_pc
1112           && (it == I || it == M) 
1113           && ((instr & 0x1ee00000000LL) == 0x10800000000LL))
1114         {
1115           /* adds rN = imm14, rM   (or mov rN, rM  when imm14 is 0) */
1116           int imm = (int) ((((instr & 0x01000000000LL) ? -1 : 0) << 13) 
1117                            | ((instr & 0x001f8000000LL) >> 20)
1118                            | ((instr & 0x000000fe000LL) >> 13));
1119           int rM = (int) ((instr & 0x00007f00000LL) >> 20);
1120           int rN = (int) ((instr & 0x00000001fc0LL) >> 6);
1121           int qp = (int) (instr & 0x0000000003fLL);
1122           if (qp == 0 && rN == 2 && imm == 0 && rM == 12 && fp_reg == 0)
1123             {
1124               /* mov r2, r12 - beginning of leaf routine */
1125               fp_reg = rN;
1126               last_prologue_pc = next_pc;
1127             }
1128         } 
1129
1130       /* If we don't recognize a regular function or leaf routine, we are
1131          done.  */
1132       if (!fp_reg)
1133         {
1134           pc = lim_pc;  
1135           if (trust_limit)
1136             last_prologue_pc = lim_pc;
1137         }
1138     }
1139
1140   /* Loop, looking for prologue instructions, keeping track of
1141      where preserved registers were spilled. */
1142   while (pc < lim_pc)
1143     {
1144       next_pc = fetch_instruction (pc, &it, &instr);
1145       if (next_pc == 0)
1146         break;
1147
1148       if (it == B && ((instr & 0x1e1f800003fLL) != 0x04000000000LL))
1149         {
1150           /* Exit loop upon hitting a non-nop branch instruction. */ 
1151           if (trust_limit)
1152             lim_pc = pc;
1153           break;
1154         }
1155       else if (((instr & 0x3fLL) != 0LL) && 
1156                (frameless || ret_reg != 0))
1157         {
1158           /* Exit loop upon hitting a predicated instruction if
1159              we already have the return register or if we are frameless.  */ 
1160           if (trust_limit)
1161             lim_pc = pc;
1162           break;
1163         }
1164       else if (it == I && ((instr & 0x1eff8000000LL) == 0x00188000000LL))
1165         {
1166           /* Move from BR */
1167           int b2 = (int) ((instr & 0x0000000e000LL) >> 13);
1168           int rN = (int) ((instr & 0x00000001fc0LL) >> 6);
1169           int qp = (int) (instr & 0x0000000003f);
1170
1171           if (qp == 0 && b2 == 0 && rN >= 32 && ret_reg == 0)
1172             {
1173               ret_reg = rN;
1174               last_prologue_pc = next_pc;
1175             }
1176         }
1177       else if ((it == I || it == M) 
1178           && ((instr & 0x1ee00000000LL) == 0x10800000000LL))
1179         {
1180           /* adds rN = imm14, rM   (or mov rN, rM  when imm14 is 0) */
1181           int imm = (int) ((((instr & 0x01000000000LL) ? -1 : 0) << 13) 
1182                            | ((instr & 0x001f8000000LL) >> 20)
1183                            | ((instr & 0x000000fe000LL) >> 13));
1184           int rM = (int) ((instr & 0x00007f00000LL) >> 20);
1185           int rN = (int) ((instr & 0x00000001fc0LL) >> 6);
1186           int qp = (int) (instr & 0x0000000003fLL);
1187
1188           if (qp == 0 && rN >= 32 && imm == 0 && rM == 12 && fp_reg == 0)
1189             {
1190               /* mov rN, r12 */
1191               fp_reg = rN;
1192               last_prologue_pc = next_pc;
1193             }
1194           else if (qp == 0 && rN == 12 && rM == 12)
1195             {
1196               /* adds r12, -mem_stack_frame_size, r12 */
1197               mem_stack_frame_size -= imm;
1198               last_prologue_pc = next_pc;
1199             }
1200           else if (qp == 0 && rN == 2 
1201                 && ((rM == fp_reg && fp_reg != 0) || rM == 12))
1202             {
1203               char buf[MAX_REGISTER_SIZE];
1204               CORE_ADDR saved_sp = 0;
1205               /* adds r2, spilloffset, rFramePointer 
1206                    or
1207                  adds r2, spilloffset, r12
1208
1209                  Get ready for stf.spill or st8.spill instructions.
1210                  The address to start spilling at is loaded into r2. 
1211                  FIXME:  Why r2?  That's what gcc currently uses; it
1212                  could well be different for other compilers.  */
1213
1214               /* Hmm... whether or not this will work will depend on
1215                  where the pc is.  If it's still early in the prologue
1216                  this'll be wrong.  FIXME */
1217               if (next_frame)
1218                 {
1219                   frame_unwind_register (next_frame, sp_regnum, buf);
1220                   saved_sp = extract_unsigned_integer (buf, 8);
1221                 }
1222               spill_addr  = saved_sp
1223                           + (rM == 12 ? 0 : mem_stack_frame_size) 
1224                           + imm;
1225               spill_reg   = rN;
1226               last_prologue_pc = next_pc;
1227             }
1228           else if (qp == 0 && rM >= 32 && rM < 40 && !instores[rM] && 
1229                    rN < 256 && imm == 0)
1230             {
1231               /* mov rN, rM where rM is an input register */
1232               reg_contents[rN] = rM;
1233               last_prologue_pc = next_pc;
1234             }
1235           else if (frameless && qp == 0 && rN == fp_reg && imm == 0 && 
1236                    rM == 2)
1237             {
1238               /* mov r12, r2 */
1239               last_prologue_pc = next_pc;
1240               break;
1241             }
1242         }
1243       else if (it == M 
1244             && (   ((instr & 0x1efc0000000LL) == 0x0eec0000000LL)
1245                 || ((instr & 0x1ffc8000000LL) == 0x0cec0000000LL) ))
1246         {
1247           /* stf.spill [rN] = fM, imm9
1248              or
1249              stf.spill [rN] = fM  */
1250
1251           int imm = imm9(instr);
1252           int rN = (int) ((instr & 0x00007f00000LL) >> 20);
1253           int fM = (int) ((instr & 0x000000fe000LL) >> 13);
1254           int qp = (int) (instr & 0x0000000003fLL);
1255           if (qp == 0 && rN == spill_reg && spill_addr != 0
1256               && ((2 <= fM && fM <= 5) || (16 <= fM && fM <= 31)))
1257             {
1258               cache->saved_regs[IA64_FR0_REGNUM + fM] = spill_addr;
1259
1260               if ((instr & 0x1efc0000000LL) == 0x0eec0000000LL)
1261                 spill_addr += imm;
1262               else
1263                 spill_addr = 0;         /* last one; must be done */
1264               last_prologue_pc = next_pc;
1265             }
1266         }
1267       else if ((it == M && ((instr & 0x1eff8000000LL) == 0x02110000000LL))
1268             || (it == I && ((instr & 0x1eff8000000LL) == 0x00050000000LL)) )
1269         {
1270           /* mov.m rN = arM   
1271                or 
1272              mov.i rN = arM */
1273
1274           int arM = (int) ((instr & 0x00007f00000LL) >> 20);
1275           int rN  = (int) ((instr & 0x00000001fc0LL) >> 6);
1276           int qp  = (int) (instr & 0x0000000003fLL);
1277           if (qp == 0 && isScratch (rN) && arM == 36 /* ar.unat */)
1278             {
1279               /* We have something like "mov.m r3 = ar.unat".  Remember the
1280                  r3 (or whatever) and watch for a store of this register... */
1281               unat_save_reg = rN;
1282               last_prologue_pc = next_pc;
1283             }
1284         }
1285       else if (it == I && ((instr & 0x1eff8000000LL) == 0x00198000000LL))
1286         {
1287           /* mov rN = pr */
1288           int rN  = (int) ((instr & 0x00000001fc0LL) >> 6);
1289           int qp  = (int) (instr & 0x0000000003fLL);
1290           if (qp == 0 && isScratch (rN))
1291             {
1292               pr_save_reg = rN;
1293               last_prologue_pc = next_pc;
1294             }
1295         }
1296       else if (it == M 
1297             && (   ((instr & 0x1ffc8000000LL) == 0x08cc0000000LL)
1298                 || ((instr & 0x1efc0000000LL) == 0x0acc0000000LL)))
1299         {
1300           /* st8 [rN] = rM 
1301               or
1302              st8 [rN] = rM, imm9 */
1303           int rN = (int) ((instr & 0x00007f00000LL) >> 20);
1304           int rM = (int) ((instr & 0x000000fe000LL) >> 13);
1305           int qp = (int) (instr & 0x0000000003fLL);
1306           int indirect = rM < 256 ? reg_contents[rM] : 0;
1307           if (qp == 0 && rN == spill_reg && spill_addr != 0
1308               && (rM == unat_save_reg || rM == pr_save_reg))
1309             {
1310               /* We've found a spill of either the UNAT register or the PR
1311                  register.  (Well, not exactly; what we've actually found is
1312                  a spill of the register that UNAT or PR was moved to).
1313                  Record that fact and move on... */
1314               if (rM == unat_save_reg)
1315                 {
1316                   /* Track UNAT register */
1317                   cache->saved_regs[IA64_UNAT_REGNUM] = spill_addr;
1318                   unat_save_reg = 0;
1319                 }
1320               else
1321                 {
1322                   /* Track PR register */
1323                   cache->saved_regs[IA64_PR_REGNUM] = spill_addr;
1324                   pr_save_reg = 0;
1325                 }
1326               if ((instr & 0x1efc0000000LL) == 0x0acc0000000LL)
1327                 /* st8 [rN] = rM, imm9 */
1328                 spill_addr += imm9(instr);
1329               else
1330                 spill_addr = 0;         /* must be done spilling */
1331               last_prologue_pc = next_pc;
1332             }
1333           else if (qp == 0 && 32 <= rM && rM < 40 && !instores[rM-32])
1334             {
1335               /* Allow up to one store of each input register. */
1336               instores[rM-32] = 1;
1337               last_prologue_pc = next_pc;
1338             }
1339           else if (qp == 0 && 32 <= indirect && indirect < 40 && 
1340                    !instores[indirect-32])
1341             {
1342               /* Allow an indirect store of an input register.  */
1343               instores[indirect-32] = 1;
1344               last_prologue_pc = next_pc;
1345             }
1346         }
1347       else if (it == M && ((instr & 0x1ff08000000LL) == 0x08c00000000LL))
1348         {
1349           /* One of
1350                st1 [rN] = rM
1351                st2 [rN] = rM
1352                st4 [rN] = rM
1353                st8 [rN] = rM
1354              Note that the st8 case is handled in the clause above.
1355              
1356              Advance over stores of input registers. One store per input
1357              register is permitted. */
1358           int rM = (int) ((instr & 0x000000fe000LL) >> 13);
1359           int qp = (int) (instr & 0x0000000003fLL);
1360           int indirect = rM < 256 ? reg_contents[rM] : 0;
1361           if (qp == 0 && 32 <= rM && rM < 40 && !instores[rM-32])
1362             {
1363               instores[rM-32] = 1;
1364               last_prologue_pc = next_pc;
1365             }
1366           else if (qp == 0 && 32 <= indirect && indirect < 40 && 
1367                    !instores[indirect-32])
1368             {
1369               /* Allow an indirect store of an input register.  */
1370               instores[indirect-32] = 1;
1371               last_prologue_pc = next_pc;
1372             }
1373         }
1374       else if (it == M && ((instr & 0x1ff88000000LL) == 0x0cc80000000LL))
1375         {
1376           /* Either
1377                stfs [rN] = fM
1378              or
1379                stfd [rN] = fM
1380
1381              Advance over stores of floating point input registers.  Again
1382              one store per register is permitted */
1383           int fM = (int) ((instr & 0x000000fe000LL) >> 13);
1384           int qp = (int) (instr & 0x0000000003fLL);
1385           if (qp == 0 && 8 <= fM && fM < 16 && !infpstores[fM - 8])
1386             {
1387               infpstores[fM-8] = 1;
1388               last_prologue_pc = next_pc;
1389             }
1390         }
1391       else if (it == M
1392             && (   ((instr & 0x1ffc8000000LL) == 0x08ec0000000LL)
1393                 || ((instr & 0x1efc0000000LL) == 0x0aec0000000LL)))
1394         {
1395           /* st8.spill [rN] = rM
1396                or
1397              st8.spill [rN] = rM, imm9 */
1398           int rN = (int) ((instr & 0x00007f00000LL) >> 20);
1399           int rM = (int) ((instr & 0x000000fe000LL) >> 13);
1400           int qp = (int) (instr & 0x0000000003fLL);
1401           if (qp == 0 && rN == spill_reg && 4 <= rM && rM <= 7)
1402             {
1403               /* We've found a spill of one of the preserved general purpose
1404                  regs.  Record the spill address and advance the spill
1405                  register if appropriate. */
1406               cache->saved_regs[IA64_GR0_REGNUM + rM] = spill_addr;
1407               if ((instr & 0x1efc0000000LL) == 0x0aec0000000LL)
1408                 /* st8.spill [rN] = rM, imm9 */
1409                 spill_addr += imm9(instr);
1410               else
1411                 spill_addr = 0;         /* Done spilling */
1412               last_prologue_pc = next_pc;
1413             }
1414         }
1415
1416       pc = next_pc;
1417     }
1418
1419   /* If not frameless and we aren't called by skip_prologue, then we need to calculate
1420      registers for the previous frame which will be needed later.  */
1421
1422   if (!frameless && next_frame)
1423     {
1424       /* Extract the size of the rotating portion of the stack
1425          frame and the register rename base from the current
1426          frame marker. */
1427       cfm = cache->cfm;
1428       sor = cache->sor;
1429       sof = cache->sof;
1430       sol = cache->sol;
1431       rrb_gr = (cfm >> 18) & 0x7f;
1432
1433       /* Find the bof (beginning of frame).  */
1434       bof = rse_address_add (cache->bsp, -sof);
1435       
1436       for (i = 0, addr = bof;
1437            i < sof;
1438            i++, addr += 8)
1439         {
1440           if (IS_NaT_COLLECTION_ADDR (addr))
1441             {
1442               addr += 8;
1443             }
1444           if (i+32 == cfm_reg)
1445             cache->saved_regs[IA64_CFM_REGNUM] = addr;
1446           if (i+32 == ret_reg)
1447             cache->saved_regs[IA64_VRAP_REGNUM] = addr;
1448           if (i+32 == fp_reg)
1449             cache->saved_regs[IA64_VFP_REGNUM] = addr;
1450         }
1451
1452       /* For the previous argument registers we require the previous bof.  
1453          If we can't find the previous cfm, then we can do nothing.  */
1454       cfm = 0;
1455       if (cache->saved_regs[IA64_CFM_REGNUM] != 0)
1456         {
1457           cfm = read_memory_integer (cache->saved_regs[IA64_CFM_REGNUM], 8);
1458         }
1459       else if (cfm_reg != 0)
1460         {
1461           frame_unwind_register (next_frame, cfm_reg, buf);
1462           cfm = extract_unsigned_integer (buf, 8);
1463         }
1464       cache->prev_cfm = cfm;
1465       
1466       if (cfm != 0)
1467         {
1468           sor = ((cfm >> 14) & 0xf) * 8;
1469           sof = (cfm & 0x7f);
1470           sol = (cfm >> 7) & 0x7f;
1471           rrb_gr = (cfm >> 18) & 0x7f;
1472
1473           /* The previous bof only requires subtraction of the sol (size of locals)
1474              due to the overlap between output and input of subsequent frames.  */
1475           bof = rse_address_add (bof, -sol);
1476           
1477           for (i = 0, addr = bof;
1478                i < sof;
1479                i++, addr += 8)
1480             {
1481               if (IS_NaT_COLLECTION_ADDR (addr))
1482                 {
1483                   addr += 8;
1484                 }
1485               if (i < sor)
1486                 cache->saved_regs[IA64_GR32_REGNUM + ((i + (sor - rrb_gr)) % sor)] 
1487                   = addr;
1488               else
1489                 cache->saved_regs[IA64_GR32_REGNUM + i] = addr;
1490             }
1491           
1492         }
1493     }
1494       
1495   /* Try and trust the lim_pc value whenever possible.  */
1496   if (trust_limit && lim_pc >= last_prologue_pc)
1497     last_prologue_pc = lim_pc;
1498
1499   cache->frameless = frameless;
1500   cache->after_prologue = last_prologue_pc;
1501   cache->mem_stack_frame_size = mem_stack_frame_size;
1502   cache->fp_reg = fp_reg;
1503
1504   return last_prologue_pc;
1505 }
1506
1507 CORE_ADDR
1508 ia64_skip_prologue (CORE_ADDR pc)
1509 {
1510   struct ia64_frame_cache cache;
1511   cache.base = 0;
1512   cache.after_prologue = 0;
1513   cache.cfm = 0;
1514   cache.bsp = 0;
1515
1516   /* Call examine_prologue with - as third argument since we don't have a next frame pointer to send.  */
1517   return examine_prologue (pc, pc+1024, 0, &cache);
1518 }
1519
1520
1521 /* Normal frames.  */
1522
1523 static struct ia64_frame_cache *
1524 ia64_frame_cache (struct frame_info *next_frame, void **this_cache)
1525 {
1526   struct ia64_frame_cache *cache;
1527   char buf[8];
1528   CORE_ADDR cfm, sof, sol, bsp, psr;
1529   int i;
1530
1531   if (*this_cache)
1532     return *this_cache;
1533
1534   cache = ia64_alloc_frame_cache ();
1535   *this_cache = cache;
1536
1537   frame_unwind_register (next_frame, sp_regnum, buf);
1538   cache->saved_sp = extract_unsigned_integer (buf, 8);
1539
1540   /* We always want the bsp to point to the end of frame.
1541      This way, we can always get the beginning of frame (bof)
1542      by subtracting frame size.  */
1543   frame_unwind_register (next_frame, IA64_BSP_REGNUM, buf);
1544   cache->bsp = extract_unsigned_integer (buf, 8);
1545   
1546   frame_unwind_register (next_frame, IA64_PSR_REGNUM, buf);
1547   psr = extract_unsigned_integer (buf, 8);
1548
1549   frame_unwind_register (next_frame, IA64_CFM_REGNUM, buf);
1550   cfm = extract_unsigned_integer (buf, 8);
1551
1552   cache->sof = (cfm & 0x7f);
1553   cache->sol = (cfm >> 7) & 0x7f;
1554   cache->sor = ((cfm >> 14) & 0xf) * 8;
1555
1556   cache->cfm = cfm;
1557
1558   cache->pc = frame_func_unwind (next_frame, NORMAL_FRAME);
1559
1560   if (cache->pc != 0)
1561     examine_prologue (cache->pc, frame_pc_unwind (next_frame), next_frame, cache);
1562   
1563   cache->base = cache->saved_sp + cache->mem_stack_frame_size;
1564
1565   return cache;
1566 }
1567
1568 static void
1569 ia64_frame_this_id (struct frame_info *next_frame, void **this_cache,
1570                     struct frame_id *this_id)
1571 {
1572   struct ia64_frame_cache *cache =
1573     ia64_frame_cache (next_frame, this_cache);
1574
1575   /* If outermost frame, mark with null frame id.  */
1576   if (cache->base == 0)
1577     (*this_id) = null_frame_id;
1578   else
1579     (*this_id) = frame_id_build_special (cache->base, cache->pc, cache->bsp);
1580   if (gdbarch_debug >= 1)
1581     fprintf_unfiltered (gdb_stdlog,
1582                         "regular frame id: code 0x%s, stack 0x%s, special 0x%s, next_frame %p\n",
1583                         paddr_nz (this_id->code_addr), 
1584                         paddr_nz (this_id->stack_addr), 
1585                         paddr_nz (cache->bsp), next_frame);
1586 }
1587
1588 static void
1589 ia64_frame_prev_register (struct frame_info *next_frame, void **this_cache,
1590                           int regnum, int *optimizedp,
1591                           enum lval_type *lvalp, CORE_ADDR *addrp,
1592                           int *realnump, gdb_byte *valuep)
1593 {
1594   struct ia64_frame_cache *cache =
1595     ia64_frame_cache (next_frame, this_cache);
1596   char dummy_valp[MAX_REGISTER_SIZE];
1597   char buf[8];
1598
1599   gdb_assert (regnum >= 0);
1600
1601   if (!target_has_registers)
1602     error (_("No registers."));
1603
1604   *optimizedp = 0;
1605   *addrp = 0;
1606   *lvalp = not_lval;
1607   *realnump = -1;
1608
1609   /* Rather than check each time if valuep is non-null, supply a dummy buffer
1610      when valuep is not supplied.  */
1611   if (!valuep)
1612     valuep = dummy_valp;
1613   
1614   memset (valuep, 0, register_size (current_gdbarch, regnum));
1615  
1616   if (regnum == SP_REGNUM)
1617     {
1618       /* Handle SP values for all frames but the topmost. */
1619       store_unsigned_integer (valuep, register_size (current_gdbarch, regnum),
1620                               cache->base);
1621     }
1622   else if (regnum == IA64_BSP_REGNUM)
1623     {
1624       char cfm_valuep[MAX_REGISTER_SIZE];
1625       int  cfm_optim;
1626       int  cfm_realnum;
1627       enum lval_type cfm_lval;
1628       CORE_ADDR cfm_addr;
1629       CORE_ADDR bsp, prev_cfm, prev_bsp;
1630
1631       /* We want to calculate the previous bsp as the end of the previous register stack frame.
1632          This corresponds to what the hardware bsp register will be if we pop the frame
1633          back which is why we might have been called.  We know the beginning of the current
1634          frame is cache->bsp - cache->sof.  This value in the previous frame points to
1635          the start of the output registers.  We can calculate the end of that frame by adding
1636          the size of output (sof (size of frame) - sol (size of locals)).  */
1637       ia64_frame_prev_register (next_frame, this_cache, IA64_CFM_REGNUM,
1638                                 &cfm_optim, &cfm_lval, &cfm_addr, &cfm_realnum, cfm_valuep);
1639       prev_cfm = extract_unsigned_integer (cfm_valuep, 8);
1640
1641       bsp = rse_address_add (cache->bsp, -(cache->sof));
1642       prev_bsp = rse_address_add (bsp, (prev_cfm & 0x7f) - ((prev_cfm >> 7) & 0x7f));
1643
1644       store_unsigned_integer (valuep, register_size (current_gdbarch, regnum), 
1645                               prev_bsp);
1646     }
1647   else if (regnum == IA64_CFM_REGNUM)
1648     {
1649       CORE_ADDR addr = cache->saved_regs[IA64_CFM_REGNUM];
1650       
1651       if (addr != 0)
1652         {
1653           *lvalp = lval_memory;
1654           *addrp = addr;
1655           read_memory (addr, valuep, register_size (current_gdbarch, regnum));
1656         }
1657       else if (cache->prev_cfm)
1658         store_unsigned_integer (valuep, register_size (current_gdbarch, regnum), cache->prev_cfm);
1659       else if (cache->frameless)
1660         {
1661           CORE_ADDR cfm = 0;
1662           frame_unwind_register (next_frame, IA64_PFS_REGNUM, valuep);
1663         }
1664     }
1665   else if (regnum == IA64_VFP_REGNUM)
1666     {
1667       /* If the function in question uses an automatic register (r32-r127)
1668          for the frame pointer, it'll be found by ia64_find_saved_register()
1669          above.  If the function lacks one of these frame pointers, we can
1670          still provide a value since we know the size of the frame.  */
1671       CORE_ADDR vfp = cache->base;
1672       store_unsigned_integer (valuep, register_size (current_gdbarch, IA64_VFP_REGNUM), vfp);
1673     }
1674   else if (VP0_REGNUM <= regnum && regnum <= VP63_REGNUM)
1675     {
1676       char pr_valuep[MAX_REGISTER_SIZE];
1677       int  pr_optim;
1678       int  pr_realnum;
1679       enum lval_type pr_lval;
1680       CORE_ADDR pr_addr;
1681       ULONGEST prN_val;
1682       ia64_frame_prev_register (next_frame, this_cache, IA64_PR_REGNUM,
1683                                 &pr_optim, &pr_lval, &pr_addr, &pr_realnum, pr_valuep);
1684       if (VP16_REGNUM <= regnum && regnum <= VP63_REGNUM)
1685         {
1686           /* Fetch predicate register rename base from current frame
1687              marker for this frame.  */
1688           int rrb_pr = (cache->cfm >> 32) & 0x3f;
1689
1690           /* Adjust the register number to account for register rotation.  */
1691           regnum = VP16_REGNUM 
1692                  + ((regnum - VP16_REGNUM) + rrb_pr) % 48;
1693         }
1694       prN_val = extract_bit_field ((unsigned char *) pr_valuep,
1695                                    regnum - VP0_REGNUM, 1);
1696       store_unsigned_integer (valuep, register_size (current_gdbarch, regnum), prN_val);
1697     }
1698   else if (IA64_NAT0_REGNUM <= regnum && regnum <= IA64_NAT31_REGNUM)
1699     {
1700       char unat_valuep[MAX_REGISTER_SIZE];
1701       int  unat_optim;
1702       int  unat_realnum;
1703       enum lval_type unat_lval;
1704       CORE_ADDR unat_addr;
1705       ULONGEST unatN_val;
1706       ia64_frame_prev_register (next_frame, this_cache, IA64_UNAT_REGNUM,
1707                                 &unat_optim, &unat_lval, &unat_addr, &unat_realnum, unat_valuep);
1708       unatN_val = extract_bit_field ((unsigned char *) unat_valuep,
1709                                    regnum - IA64_NAT0_REGNUM, 1);
1710       store_unsigned_integer (valuep, register_size (current_gdbarch, regnum), 
1711                               unatN_val);
1712     }
1713   else if (IA64_NAT32_REGNUM <= regnum && regnum <= IA64_NAT127_REGNUM)
1714     {
1715       int natval = 0;
1716       /* Find address of general register corresponding to nat bit we're
1717          interested in.  */
1718       CORE_ADDR gr_addr;
1719
1720       gr_addr = cache->saved_regs[regnum - IA64_NAT0_REGNUM 
1721                                   + IA64_GR0_REGNUM];
1722       if (gr_addr != 0)
1723         {
1724           /* Compute address of nat collection bits.  */
1725           CORE_ADDR nat_addr = gr_addr | 0x1f8;
1726           CORE_ADDR bsp;
1727           CORE_ADDR nat_collection;
1728           int nat_bit;
1729           /* If our nat collection address is bigger than bsp, we have to get
1730              the nat collection from rnat.  Otherwise, we fetch the nat
1731              collection from the computed address.  */
1732           frame_unwind_register (next_frame, IA64_BSP_REGNUM, buf);
1733           bsp = extract_unsigned_integer (buf, 8); 
1734           if (nat_addr >= bsp)
1735             {
1736               frame_unwind_register (next_frame, IA64_RNAT_REGNUM, buf);
1737               nat_collection = extract_unsigned_integer (buf, 8);
1738             }
1739           else
1740             nat_collection = read_memory_integer (nat_addr, 8);
1741           nat_bit = (gr_addr >> 3) & 0x3f;
1742           natval = (nat_collection >> nat_bit) & 1;
1743         }
1744
1745       store_unsigned_integer (valuep, register_size (current_gdbarch, regnum), natval);
1746     }
1747   else if (regnum == IA64_IP_REGNUM)
1748     {
1749       CORE_ADDR pc = 0;
1750       CORE_ADDR addr = cache->saved_regs[IA64_VRAP_REGNUM];
1751
1752       if (addr != 0)
1753         {
1754           *lvalp = lval_memory;
1755           *addrp = addr;
1756           read_memory (addr, buf, register_size (current_gdbarch, IA64_IP_REGNUM));
1757           pc = extract_unsigned_integer (buf, 8);
1758         }
1759       else if (cache->frameless)
1760         {
1761           frame_unwind_register (next_frame, IA64_BR0_REGNUM, buf);
1762           pc = extract_unsigned_integer (buf, 8);
1763         }
1764       pc &= ~0xf;
1765       store_unsigned_integer (valuep, 8, pc);
1766     }
1767   else if (regnum == IA64_PSR_REGNUM)
1768     {
1769       /* We don't know how to get the complete previous PSR, but we need it for
1770          the slot information when we unwind the pc (pc is formed of IP register
1771          plus slot information from PSR).  To get the previous slot information, 
1772          we mask it off the return address.  */
1773       ULONGEST slot_num = 0;
1774       CORE_ADDR pc= 0;
1775       CORE_ADDR psr = 0;
1776       CORE_ADDR addr = cache->saved_regs[IA64_VRAP_REGNUM];
1777
1778       frame_unwind_register (next_frame, IA64_PSR_REGNUM, buf);
1779       psr = extract_unsigned_integer (buf, 8);
1780
1781       if (addr != 0)
1782         {
1783           *lvalp = lval_memory;
1784           *addrp = addr;
1785           read_memory (addr, buf, register_size (current_gdbarch, IA64_IP_REGNUM));
1786           pc = extract_unsigned_integer (buf, 8);
1787         }
1788       else if (cache->frameless)
1789         {
1790           CORE_ADDR pc;
1791           frame_unwind_register (next_frame, IA64_BR0_REGNUM, buf);
1792           pc = extract_unsigned_integer (buf, 8);
1793         }
1794       psr &= ~(3LL << 41);
1795       slot_num = pc & 0x3LL;
1796       psr |= (CORE_ADDR)slot_num << 41;
1797       store_unsigned_integer (valuep, 8, psr);
1798     }
1799   else if (regnum == IA64_BR0_REGNUM)
1800     {
1801       CORE_ADDR br0 = 0;
1802       CORE_ADDR addr = cache->saved_regs[IA64_BR0_REGNUM];
1803       if (addr != 0)
1804         {
1805           *lvalp = lval_memory;
1806           *addrp = addr;
1807           read_memory (addr, buf, register_size (current_gdbarch, IA64_BR0_REGNUM));
1808           br0 = extract_unsigned_integer (buf, 8);
1809         }
1810       store_unsigned_integer (valuep, 8, br0);
1811     }
1812  else if ((regnum >= IA64_GR32_REGNUM && regnum <= IA64_GR127_REGNUM) ||
1813            (regnum >= V32_REGNUM && regnum <= V127_REGNUM))
1814     {
1815       CORE_ADDR addr = 0;
1816       if (regnum >= V32_REGNUM)
1817         regnum = IA64_GR32_REGNUM + (regnum - V32_REGNUM);
1818       addr = cache->saved_regs[regnum];
1819       if (addr != 0)
1820         {
1821           *lvalp = lval_memory;
1822           *addrp = addr;
1823           read_memory (addr, valuep, register_size (current_gdbarch, regnum));
1824         }
1825       else if (cache->frameless)
1826         {
1827           char r_valuep[MAX_REGISTER_SIZE];
1828           int  r_optim;
1829           int  r_realnum;
1830           enum lval_type r_lval;
1831           CORE_ADDR r_addr;
1832           CORE_ADDR prev_cfm, prev_bsp, prev_bof;
1833           CORE_ADDR addr = 0;
1834           if (regnum >= V32_REGNUM)
1835             regnum = IA64_GR32_REGNUM + (regnum - V32_REGNUM);
1836           ia64_frame_prev_register (next_frame, this_cache, IA64_CFM_REGNUM,
1837                                     &r_optim, &r_lval, &r_addr, &r_realnum, r_valuep); 
1838           prev_cfm = extract_unsigned_integer (r_valuep, 8);
1839           ia64_frame_prev_register (next_frame, this_cache, IA64_BSP_REGNUM,
1840                                     &r_optim, &r_lval, &r_addr, &r_realnum, r_valuep);
1841           prev_bsp = extract_unsigned_integer (r_valuep, 8);
1842           prev_bof = rse_address_add (prev_bsp, -(prev_cfm & 0x7f));
1843
1844           addr = rse_address_add (prev_bof, (regnum - IA64_GR32_REGNUM));
1845           *lvalp = lval_memory;
1846           *addrp = addr;
1847           read_memory (addr, valuep, register_size (current_gdbarch, regnum));
1848         }
1849     }
1850   else
1851     {
1852       CORE_ADDR addr = 0;
1853       if (IA64_FR32_REGNUM <= regnum && regnum <= IA64_FR127_REGNUM)
1854         {
1855           /* Fetch floating point register rename base from current
1856              frame marker for this frame.  */
1857           int rrb_fr = (cache->cfm >> 25) & 0x7f;
1858
1859           /* Adjust the floating point register number to account for
1860              register rotation.  */
1861           regnum = IA64_FR32_REGNUM
1862                  + ((regnum - IA64_FR32_REGNUM) + rrb_fr) % 96;
1863         }
1864
1865       /* If we have stored a memory address, access the register.  */
1866       addr = cache->saved_regs[regnum];
1867       if (addr != 0)
1868         {
1869           *lvalp = lval_memory;
1870           *addrp = addr;
1871           read_memory (addr, valuep, register_size (current_gdbarch, regnum));
1872         }
1873       /* Otherwise, punt and get the current value of the register.  */
1874       else 
1875         frame_unwind_register (next_frame, regnum, valuep);
1876     }
1877
1878   if (gdbarch_debug >= 1)
1879     fprintf_unfiltered (gdb_stdlog,
1880                         "regular prev register <%d> <%s> is 0x%s\n", regnum, 
1881                         (((unsigned) regnum <= IA64_NAT127_REGNUM)
1882                          ? ia64_register_names[regnum] : "r??"), 
1883                         paddr_nz (extract_unsigned_integer (valuep, 8)));
1884 }
1885  
1886 static const struct frame_unwind ia64_frame_unwind =
1887 {
1888   NORMAL_FRAME,
1889   &ia64_frame_this_id,
1890   &ia64_frame_prev_register
1891 };
1892
1893 static const struct frame_unwind *
1894 ia64_frame_sniffer (struct frame_info *next_frame)
1895 {
1896   return &ia64_frame_unwind;
1897 }
1898
1899 /* Signal trampolines.  */
1900
1901 static void
1902 ia64_sigtramp_frame_init_saved_regs (struct ia64_frame_cache *cache)
1903 {
1904   if (SIGCONTEXT_REGISTER_ADDRESS)
1905     {
1906       int regno;
1907
1908       cache->saved_regs[IA64_VRAP_REGNUM] = 
1909         SIGCONTEXT_REGISTER_ADDRESS (cache->base, IA64_IP_REGNUM);
1910       cache->saved_regs[IA64_CFM_REGNUM] = 
1911         SIGCONTEXT_REGISTER_ADDRESS (cache->base, IA64_CFM_REGNUM);
1912       cache->saved_regs[IA64_PSR_REGNUM] = 
1913         SIGCONTEXT_REGISTER_ADDRESS (cache->base, IA64_PSR_REGNUM);
1914       cache->saved_regs[IA64_BSP_REGNUM] = 
1915         SIGCONTEXT_REGISTER_ADDRESS (cache->base, IA64_BSP_REGNUM);
1916       cache->saved_regs[IA64_RNAT_REGNUM] = 
1917         SIGCONTEXT_REGISTER_ADDRESS (cache->base, IA64_RNAT_REGNUM);
1918       cache->saved_regs[IA64_CCV_REGNUM] = 
1919         SIGCONTEXT_REGISTER_ADDRESS (cache->base, IA64_CCV_REGNUM);
1920       cache->saved_regs[IA64_UNAT_REGNUM] = 
1921         SIGCONTEXT_REGISTER_ADDRESS (cache->base, IA64_UNAT_REGNUM);
1922       cache->saved_regs[IA64_FPSR_REGNUM] = 
1923         SIGCONTEXT_REGISTER_ADDRESS (cache->base, IA64_FPSR_REGNUM);
1924       cache->saved_regs[IA64_PFS_REGNUM] = 
1925         SIGCONTEXT_REGISTER_ADDRESS (cache->base, IA64_PFS_REGNUM);
1926       cache->saved_regs[IA64_LC_REGNUM] = 
1927         SIGCONTEXT_REGISTER_ADDRESS (cache->base, IA64_LC_REGNUM);
1928       for (regno = IA64_GR1_REGNUM; regno <= IA64_GR31_REGNUM; regno++)
1929         cache->saved_regs[regno] =
1930           SIGCONTEXT_REGISTER_ADDRESS (cache->base, regno);
1931       for (regno = IA64_BR0_REGNUM; regno <= IA64_BR7_REGNUM; regno++)
1932         cache->saved_regs[regno] =
1933           SIGCONTEXT_REGISTER_ADDRESS (cache->base, regno);
1934       for (regno = IA64_FR2_REGNUM; regno <= IA64_FR31_REGNUM; regno++)
1935         cache->saved_regs[regno] =
1936           SIGCONTEXT_REGISTER_ADDRESS (cache->base, regno);
1937     }
1938 }
1939
1940 static struct ia64_frame_cache *
1941 ia64_sigtramp_frame_cache (struct frame_info *next_frame, void **this_cache)
1942 {
1943   struct ia64_frame_cache *cache;
1944   CORE_ADDR addr;
1945   char buf[8];
1946   int i;
1947
1948   if (*this_cache)
1949     return *this_cache;
1950
1951   cache = ia64_alloc_frame_cache ();
1952
1953   frame_unwind_register (next_frame, sp_regnum, buf);
1954   /* Note that frame size is hard-coded below.  We cannot calculate it
1955      via prologue examination.  */
1956   cache->base = extract_unsigned_integer (buf, 8) + 16;
1957
1958   frame_unwind_register (next_frame, IA64_BSP_REGNUM, buf);
1959   cache->bsp = extract_unsigned_integer (buf, 8);
1960
1961   frame_unwind_register (next_frame, IA64_CFM_REGNUM, buf);
1962   cache->cfm = extract_unsigned_integer (buf, 8);
1963   cache->sof = cache->cfm & 0x7f;
1964
1965   ia64_sigtramp_frame_init_saved_regs (cache);
1966
1967   *this_cache = cache;
1968   return cache;
1969 }
1970
1971 static void
1972 ia64_sigtramp_frame_this_id (struct frame_info *next_frame,
1973                                void **this_cache, struct frame_id *this_id)
1974 {
1975   struct ia64_frame_cache *cache =
1976     ia64_sigtramp_frame_cache (next_frame, this_cache);
1977
1978   (*this_id) = frame_id_build_special (cache->base, frame_pc_unwind (next_frame), cache->bsp);
1979   if (gdbarch_debug >= 1)
1980     fprintf_unfiltered (gdb_stdlog,
1981                         "sigtramp frame id: code 0x%s, stack 0x%s, special 0x%s, next_frame %p\n",
1982                         paddr_nz (this_id->code_addr), 
1983                         paddr_nz (this_id->stack_addr), 
1984                         paddr_nz (cache->bsp), next_frame);
1985 }
1986
1987 static void
1988 ia64_sigtramp_frame_prev_register (struct frame_info *next_frame,
1989                                    void **this_cache,
1990                                    int regnum, int *optimizedp,
1991                                    enum lval_type *lvalp, CORE_ADDR *addrp,
1992                                    int *realnump, gdb_byte *valuep)
1993 {
1994   char dummy_valp[MAX_REGISTER_SIZE];
1995   char buf[MAX_REGISTER_SIZE];
1996
1997   struct ia64_frame_cache *cache =
1998     ia64_sigtramp_frame_cache (next_frame, this_cache);
1999
2000   gdb_assert (regnum >= 0);
2001
2002   if (!target_has_registers)
2003     error (_("No registers."));
2004
2005   *optimizedp = 0;
2006   *addrp = 0;
2007   *lvalp = not_lval;
2008   *realnump = -1;
2009
2010   /* Rather than check each time if valuep is non-null, supply a dummy buffer
2011      when valuep is not supplied.  */
2012   if (!valuep)
2013     valuep = dummy_valp;
2014   
2015   memset (valuep, 0, register_size (current_gdbarch, regnum));
2016  
2017   if (regnum == IA64_IP_REGNUM)
2018     {
2019       CORE_ADDR pc = 0;
2020       CORE_ADDR addr = cache->saved_regs[IA64_VRAP_REGNUM];
2021
2022       if (addr != 0)
2023         {
2024           *lvalp = lval_memory;
2025           *addrp = addr;
2026           read_memory (addr, buf, register_size (current_gdbarch, IA64_IP_REGNUM));
2027           pc = extract_unsigned_integer (buf, 8);
2028         }
2029       pc &= ~0xf;
2030       store_unsigned_integer (valuep, 8, pc);
2031     }
2032  else if ((regnum >= IA64_GR32_REGNUM && regnum <= IA64_GR127_REGNUM) ||
2033            (regnum >= V32_REGNUM && regnum <= V127_REGNUM))
2034     {
2035       CORE_ADDR addr = 0;
2036       if (regnum >= V32_REGNUM)
2037         regnum = IA64_GR32_REGNUM + (regnum - V32_REGNUM);
2038       addr = cache->saved_regs[regnum];
2039       if (addr != 0)
2040         {
2041           *lvalp = lval_memory;
2042           *addrp = addr;
2043           read_memory (addr, valuep, register_size (current_gdbarch, regnum));
2044         }
2045     }
2046   else
2047     {
2048       /* All other registers not listed above.  */
2049       CORE_ADDR addr = cache->saved_regs[regnum];
2050       if (addr != 0)
2051         {
2052           *lvalp = lval_memory;
2053           *addrp = addr;
2054           read_memory (addr, valuep, register_size (current_gdbarch, regnum));
2055         }
2056     }
2057
2058   if (gdbarch_debug >= 1)
2059     fprintf_unfiltered (gdb_stdlog,
2060                         "sigtramp prev register <%s> is 0x%s\n",
2061                         (regnum < IA64_GR32_REGNUM
2062                          || (regnum > IA64_GR127_REGNUM
2063                              && regnum < LAST_PSEUDO_REGNUM))
2064                          ? ia64_register_names[regnum]
2065                          : (regnum < LAST_PSEUDO_REGNUM
2066                             ? ia64_register_names[regnum-IA64_GR32_REGNUM+V32_REGNUM]
2067                             : "OUT_OF_RANGE"),
2068                         paddr_nz (extract_unsigned_integer (valuep, 8)));
2069 }
2070
2071 static const struct frame_unwind ia64_sigtramp_frame_unwind =
2072 {
2073   SIGTRAMP_FRAME,
2074   ia64_sigtramp_frame_this_id,
2075   ia64_sigtramp_frame_prev_register
2076 };
2077
2078 static const struct frame_unwind *
2079 ia64_sigtramp_frame_sniffer (struct frame_info *next_frame)
2080 {
2081   struct gdbarch_tdep *tdep = gdbarch_tdep (get_frame_arch (next_frame));
2082   if (tdep->pc_in_sigtramp)
2083     {
2084       CORE_ADDR pc = frame_pc_unwind (next_frame);
2085
2086       if (tdep->pc_in_sigtramp (pc))
2087         return &ia64_sigtramp_frame_unwind;
2088     }
2089
2090   return NULL;
2091 }
2092 \f
2093
2094 static CORE_ADDR
2095 ia64_frame_base_address (struct frame_info *next_frame, void **this_cache)
2096 {
2097   struct ia64_frame_cache *cache =
2098     ia64_frame_cache (next_frame, this_cache);
2099
2100   return cache->base;
2101 }
2102
2103 static const struct frame_base ia64_frame_base =
2104 {
2105   &ia64_frame_unwind,
2106   ia64_frame_base_address,
2107   ia64_frame_base_address,
2108   ia64_frame_base_address
2109 };
2110
2111 #ifdef HAVE_LIBUNWIND_IA64_H
2112
2113 struct ia64_unwind_table_entry
2114   {
2115     unw_word_t start_offset;
2116     unw_word_t end_offset;
2117     unw_word_t info_offset;
2118   };
2119
2120 static __inline__ uint64_t
2121 ia64_rse_slot_num (uint64_t addr)
2122 {
2123   return (addr >> 3) & 0x3f;
2124 }
2125
2126 /* Skip over a designated number of registers in the backing
2127    store, remembering every 64th position is for NAT.  */
2128 static __inline__ uint64_t
2129 ia64_rse_skip_regs (uint64_t addr, long num_regs)
2130 {
2131   long delta = ia64_rse_slot_num(addr) + num_regs;
2132
2133   if (num_regs < 0)
2134     delta -= 0x3e;
2135   return addr + ((num_regs + delta/0x3f) << 3);
2136 }
2137   
2138 /* Gdb libunwind-frame callback function to convert from an ia64 gdb register 
2139    number to a libunwind register number.  */
2140 static int
2141 ia64_gdb2uw_regnum (int regnum)
2142 {
2143   if (regnum == sp_regnum)
2144     return UNW_IA64_SP;
2145   else if (regnum == IA64_BSP_REGNUM)
2146     return UNW_IA64_BSP;
2147   else if ((unsigned) (regnum - IA64_GR0_REGNUM) < 128)
2148     return UNW_IA64_GR + (regnum - IA64_GR0_REGNUM);
2149   else if ((unsigned) (regnum - V32_REGNUM) < 95)
2150     return UNW_IA64_GR + 32 + (regnum - V32_REGNUM);
2151   else if ((unsigned) (regnum - IA64_FR0_REGNUM) < 128)
2152     return UNW_IA64_FR + (regnum - IA64_FR0_REGNUM);
2153   else if ((unsigned) (regnum - IA64_PR0_REGNUM) < 64)
2154     return -1;
2155   else if ((unsigned) (regnum - IA64_BR0_REGNUM) < 8)
2156     return UNW_IA64_BR + (regnum - IA64_BR0_REGNUM);
2157   else if (regnum == IA64_PR_REGNUM)
2158     return UNW_IA64_PR;
2159   else if (regnum == IA64_IP_REGNUM)
2160     return UNW_REG_IP;
2161   else if (regnum == IA64_CFM_REGNUM)
2162     return UNW_IA64_CFM;
2163   else if ((unsigned) (regnum - IA64_AR0_REGNUM) < 128)
2164     return UNW_IA64_AR + (regnum - IA64_AR0_REGNUM);
2165   else if ((unsigned) (regnum - IA64_NAT0_REGNUM) < 128)
2166     return UNW_IA64_NAT + (regnum - IA64_NAT0_REGNUM);
2167   else
2168     return -1;
2169 }
2170   
2171 /* Gdb libunwind-frame callback function to convert from a libunwind register 
2172    number to a ia64 gdb register number.  */
2173 static int
2174 ia64_uw2gdb_regnum (int uw_regnum)
2175 {
2176   if (uw_regnum == UNW_IA64_SP)
2177     return sp_regnum;
2178   else if (uw_regnum == UNW_IA64_BSP)
2179     return IA64_BSP_REGNUM;
2180   else if ((unsigned) (uw_regnum - UNW_IA64_GR) < 32)
2181     return IA64_GR0_REGNUM + (uw_regnum - UNW_IA64_GR);
2182   else if ((unsigned) (uw_regnum - UNW_IA64_GR) < 128)
2183     return V32_REGNUM + (uw_regnum - (IA64_GR0_REGNUM + 32));
2184   else if ((unsigned) (uw_regnum - UNW_IA64_FR) < 128)
2185     return IA64_FR0_REGNUM + (uw_regnum - UNW_IA64_FR);
2186   else if ((unsigned) (uw_regnum - UNW_IA64_BR) < 8)
2187     return IA64_BR0_REGNUM + (uw_regnum - UNW_IA64_BR);
2188   else if (uw_regnum == UNW_IA64_PR)
2189     return IA64_PR_REGNUM;
2190   else if (uw_regnum == UNW_REG_IP)
2191     return IA64_IP_REGNUM;
2192   else if (uw_regnum == UNW_IA64_CFM)
2193     return IA64_CFM_REGNUM;
2194   else if ((unsigned) (uw_regnum - UNW_IA64_AR) < 128)
2195     return IA64_AR0_REGNUM + (uw_regnum - UNW_IA64_AR);
2196   else if ((unsigned) (uw_regnum - UNW_IA64_NAT) < 128)
2197     return IA64_NAT0_REGNUM + (uw_regnum - UNW_IA64_NAT);
2198   else
2199     return -1;
2200 }
2201
2202 /* Gdb libunwind-frame callback function to reveal if register is a float 
2203    register or not.  */
2204 static int
2205 ia64_is_fpreg (int uw_regnum)
2206 {
2207   return unw_is_fpreg (uw_regnum);
2208 }
2209   
2210 /* Libunwind callback accessor function for general registers.  */
2211 static int
2212 ia64_access_reg (unw_addr_space_t as, unw_regnum_t uw_regnum, unw_word_t *val, 
2213                  int write, void *arg)
2214 {
2215   int regnum = ia64_uw2gdb_regnum (uw_regnum);
2216   unw_word_t bsp, sof, sol, cfm, psr, ip;
2217   struct frame_info *next_frame = arg;
2218   long new_sof, old_sof;
2219   char buf[MAX_REGISTER_SIZE];
2220   
2221   if (write)
2222     {
2223       if (regnum < 0)
2224         /* ignore writes to pseudo-registers such as UNW_IA64_PROC_STARTI.  */
2225         return 0;
2226   
2227       switch (uw_regnum)
2228         {
2229         case UNW_REG_IP:
2230           ia64_write_pc (*val, inferior_ptid);
2231           break;
2232
2233         case UNW_IA64_AR_BSPSTORE:
2234           write_register (IA64_BSP_REGNUM, *val);
2235           break;
2236           
2237         case UNW_IA64_AR_BSP:
2238         case UNW_IA64_BSP:
2239           /* Account for the fact that ptrace() expects bsp to point
2240              after the current register frame.  */
2241           cfm = read_register (IA64_CFM_REGNUM);
2242           sof = (cfm & 0x7f);
2243           bsp = ia64_rse_skip_regs (*val, sof);
2244           write_register (IA64_BSP_REGNUM, bsp);
2245           break;
2246           
2247         case UNW_IA64_CFM:
2248           /* If we change CFM, we need to adjust ptrace's notion of
2249              bsp accordingly, so that the real bsp remains
2250              unchanged.  */
2251           bsp = read_register (IA64_BSP_REGNUM);
2252           cfm = read_register (IA64_CFM_REGNUM);
2253           old_sof = (cfm & 0x7f);
2254           new_sof = (*val & 0x7f);
2255           if (old_sof != new_sof)
2256             {
2257               bsp = ia64_rse_skip_regs (bsp, -old_sof + new_sof);
2258               write_register (IA64_BSP_REGNUM, bsp);
2259             }
2260           write_register (IA64_CFM_REGNUM, *val);
2261           break;
2262           
2263         default:
2264           write_register (regnum, *val);
2265           break;
2266         }
2267       if (gdbarch_debug >= 1)
2268         fprintf_unfiltered (gdb_stdlog, 
2269                             "  access_reg: to cache: %4s=0x%s\n",
2270                             (((unsigned) regnum <= IA64_NAT127_REGNUM)
2271                              ? ia64_register_names[regnum] : "r??"), 
2272                             paddr_nz (*val));
2273     }
2274   else
2275     {
2276       switch (uw_regnum)
2277         {
2278         case UNW_REG_IP:
2279           /* Libunwind expects to see the pc value which means the slot number
2280              from the psr must be merged with the ip word address.  */
2281           frame_unwind_register (next_frame, IA64_IP_REGNUM, buf);
2282           ip = extract_unsigned_integer (buf, 8); 
2283           frame_unwind_register (next_frame, IA64_PSR_REGNUM, buf);
2284           psr = extract_unsigned_integer (buf, 8); 
2285           *val = ip | ((psr >> 41) & 0x3);
2286           break;
2287           
2288         case UNW_IA64_AR_BSP:
2289           /* Libunwind expects to see the beginning of the current register
2290              frame so we must account for the fact that ptrace() will return a value
2291              for bsp that points *after* the current register frame.  */
2292           frame_unwind_register (next_frame, IA64_BSP_REGNUM, buf);
2293           bsp = extract_unsigned_integer (buf, 8);
2294           frame_unwind_register (next_frame, IA64_CFM_REGNUM, buf);
2295           cfm = extract_unsigned_integer (buf, 8); 
2296           sof = (cfm & 0x7f);
2297           *val = ia64_rse_skip_regs (bsp, -sof);
2298           break;
2299           
2300         case UNW_IA64_AR_BSPSTORE:
2301           /* Libunwind wants bspstore to be after the current register frame.
2302              This is what ptrace() and gdb treats as the regular bsp value.  */
2303           frame_unwind_register (next_frame, IA64_BSP_REGNUM, buf);
2304           *val = extract_unsigned_integer (buf, 8);
2305           break;
2306
2307         default:
2308           /* For all other registers, just unwind the value directly.  */
2309           frame_unwind_register (next_frame, regnum, buf);
2310           *val = extract_unsigned_integer (buf, 8); 
2311           break;
2312         }
2313       
2314       if (gdbarch_debug >= 1)
2315         fprintf_unfiltered (gdb_stdlog, 
2316                             "  access_reg: from cache: %4s=0x%s\n",
2317                             (((unsigned) regnum <= IA64_NAT127_REGNUM)
2318                              ? ia64_register_names[regnum] : "r??"), 
2319                             paddr_nz (*val));
2320     }
2321   return 0;
2322 }
2323
2324 /* Libunwind callback accessor function for floating-point registers.  */
2325 static int
2326 ia64_access_fpreg (unw_addr_space_t as, unw_regnum_t uw_regnum, unw_fpreg_t *val, 
2327                    int write, void *arg)
2328 {
2329   int regnum = ia64_uw2gdb_regnum (uw_regnum);
2330   
2331   if (write)
2332     regcache_cooked_write (current_regcache, regnum, (char *) val);
2333   else
2334     regcache_cooked_read (current_regcache, regnum, (char *) val);
2335   return 0;
2336 }
2337
2338 /* Libunwind callback accessor function for top-level rse registers.  */
2339 static int
2340 ia64_access_rse_reg (unw_addr_space_t as, unw_regnum_t uw_regnum, unw_word_t *val, 
2341                      int write, void *arg)
2342 {
2343   int regnum = ia64_uw2gdb_regnum (uw_regnum);
2344   unw_word_t bsp, sof, sol, cfm, psr, ip;
2345   long new_sof, old_sof;
2346   
2347   if (write)
2348     {
2349       if (regnum < 0)
2350         /* ignore writes to pseudo-registers such as UNW_IA64_PROC_STARTI.  */
2351         return 0;
2352   
2353       switch (uw_regnum)
2354         {
2355         case UNW_REG_IP:
2356           ia64_write_pc (*val, inferior_ptid);
2357           break;
2358
2359         case UNW_IA64_AR_BSPSTORE:
2360           write_register (IA64_BSP_REGNUM, *val);
2361           break;
2362           
2363         case UNW_IA64_AR_BSP:
2364         case UNW_IA64_BSP:
2365           /* Account for the fact that ptrace() expects bsp to point
2366              after the current register frame.  */
2367           cfm = read_register (IA64_CFM_REGNUM);
2368           sof = (cfm & 0x7f);
2369           bsp = ia64_rse_skip_regs (*val, sof);
2370           write_register (IA64_BSP_REGNUM, bsp);
2371           break;
2372           
2373         case UNW_IA64_CFM:
2374           /* If we change CFM, we need to adjust ptrace's notion of
2375              bsp accordingly, so that the real bsp remains
2376              unchanged.  */
2377           bsp = read_register (IA64_BSP_REGNUM);
2378           cfm = read_register (IA64_CFM_REGNUM);
2379           old_sof = (cfm & 0x7f);
2380           new_sof = (*val & 0x7f);
2381           if (old_sof != new_sof)
2382             {
2383               bsp = ia64_rse_skip_regs (bsp, -old_sof + new_sof);
2384               write_register (IA64_BSP_REGNUM, bsp);
2385             }
2386           write_register (IA64_CFM_REGNUM, *val);
2387           break;
2388           
2389         default:
2390           write_register (regnum, *val);
2391           break;
2392         }
2393       if (gdbarch_debug >= 1)
2394         fprintf_unfiltered (gdb_stdlog, 
2395                             "  access_rse_reg: to cache: %4s=0x%s\n",
2396                             (((unsigned) regnum <= IA64_NAT127_REGNUM)
2397                              ? ia64_register_names[regnum] : "r??"), 
2398                             paddr_nz (*val));
2399     }
2400   else
2401     {
2402       switch (uw_regnum)
2403         {
2404         case UNW_REG_IP:
2405           /* Libunwind expects to see the pc value which means the slot number
2406              from the psr must be merged with the ip word address.  */
2407           ip = read_register (IA64_IP_REGNUM); 
2408           psr = read_register (IA64_PSR_REGNUM);
2409           *val = ip | ((psr >> 41) & 0x3);
2410           break;
2411           
2412         case UNW_IA64_AR_BSP:
2413           /* Libunwind expects to see the beginning of the current register
2414              frame so we must account for the fact that ptrace() will return a value
2415              for bsp that points *after* the current register frame.  */
2416           bsp = read_register (IA64_BSP_REGNUM);
2417           cfm = read_register (IA64_CFM_REGNUM);
2418           sof = (cfm & 0x7f);
2419           *val = ia64_rse_skip_regs (bsp, -sof);
2420           break;
2421           
2422         case UNW_IA64_AR_BSPSTORE:
2423           /* Libunwind wants bspstore to be after the current register frame.
2424              This is what ptrace() and gdb treats as the regular bsp value.  */
2425           *val = read_register (IA64_BSP_REGNUM);
2426           break;
2427
2428         default:
2429           /* For all other registers, just read the value directly.  */
2430           *val = read_register (regnum);
2431           break;
2432         }
2433     }
2434       
2435   if (gdbarch_debug >= 1)
2436     fprintf_unfiltered (gdb_stdlog, 
2437                         "  access_rse_reg: from cache: %4s=0x%s\n",
2438                         (((unsigned) regnum <= IA64_NAT127_REGNUM)
2439                          ? ia64_register_names[regnum] : "r??"), 
2440                         paddr_nz (*val));
2441
2442   return 0;
2443 }
2444
2445 /* Libunwind callback accessor function for accessing memory.  */
2446 static int
2447 ia64_access_mem (unw_addr_space_t as,
2448                  unw_word_t addr, unw_word_t *val,
2449                  int write, void *arg)
2450 {
2451   if (addr - KERNEL_START < ktab_size)
2452     {
2453       unw_word_t *laddr = (unw_word_t*) ((char *) ktab
2454                           + (addr - KERNEL_START));
2455                 
2456       if (write)
2457         *laddr = *val; 
2458       else 
2459         *val = *laddr;
2460       return 0;
2461     }
2462
2463   /* XXX do we need to normalize byte-order here?  */
2464   if (write)
2465     return target_write_memory (addr, (char *) val, sizeof (unw_word_t));
2466   else
2467     return target_read_memory (addr, (char *) val, sizeof (unw_word_t));
2468 }
2469
2470 /* Call low-level function to access the kernel unwind table.  */
2471 static LONGEST
2472 getunwind_table (gdb_byte **buf_p)
2473 {
2474   LONGEST x;
2475
2476   /* FIXME drow/2005-09-10: This code used to call
2477      ia64_linux_xfer_unwind_table directly to fetch the unwind table
2478      for the currently running ia64-linux kernel.  That data should
2479      come from the core file and be accessed via the auxv vector; if
2480      we want to preserve fall back to the running kernel's table, then
2481      we should find a way to override the corefile layer's
2482      xfer_partial method.  */
2483
2484   x = target_read_alloc (&current_target, TARGET_OBJECT_UNWIND_TABLE,
2485                          NULL, buf_p);
2486
2487   return x;
2488 }
2489
2490 /* Get the kernel unwind table.  */                              
2491 static int
2492 get_kernel_table (unw_word_t ip, unw_dyn_info_t *di)
2493 {
2494   static struct ia64_table_entry *etab;
2495
2496   if (!ktab) 
2497     {
2498       gdb_byte *ktab_buf;
2499       LONGEST size;
2500
2501       size = getunwind_table (&ktab_buf);
2502       if (size <= 0)
2503         return -UNW_ENOINFO;
2504
2505       ktab = (struct ia64_table_entry *) ktab_buf;
2506       ktab_size = size;
2507
2508       for (etab = ktab; etab->start_offset; ++etab)
2509         etab->info_offset += KERNEL_START;
2510     }
2511   
2512   if (ip < ktab[0].start_offset || ip >= etab[-1].end_offset)
2513     return -UNW_ENOINFO;
2514   
2515   di->format = UNW_INFO_FORMAT_TABLE;
2516   di->gp = 0;
2517   di->start_ip = ktab[0].start_offset;
2518   di->end_ip = etab[-1].end_offset;
2519   di->u.ti.name_ptr = (unw_word_t) "<kernel>";
2520   di->u.ti.segbase = 0;
2521   di->u.ti.table_len = ((char *) etab - (char *) ktab) / sizeof (unw_word_t);
2522   di->u.ti.table_data = (unw_word_t *) ktab;
2523   
2524   if (gdbarch_debug >= 1)
2525     fprintf_unfiltered (gdb_stdlog, "get_kernel_table: found table `%s': "
2526                         "segbase=0x%s, length=%s, gp=0x%s\n",
2527                         (char *) di->u.ti.name_ptr, 
2528                         paddr_nz (di->u.ti.segbase), 
2529                         paddr_u (di->u.ti.table_len), 
2530                         paddr_nz (di->gp));
2531   return 0;
2532 }
2533
2534 /* Find the unwind table entry for a specified address.  */
2535 static int
2536 ia64_find_unwind_table (struct objfile *objfile, unw_word_t ip,
2537                         unw_dyn_info_t *dip, void **buf)
2538 {
2539   Elf_Internal_Phdr *phdr, *p_text = NULL, *p_unwind = NULL;
2540   Elf_Internal_Ehdr *ehdr;
2541   unw_word_t segbase = 0;
2542   CORE_ADDR load_base;
2543   bfd *bfd;
2544   int i;
2545
2546   bfd = objfile->obfd;
2547   
2548   ehdr = elf_tdata (bfd)->elf_header;
2549   phdr = elf_tdata (bfd)->phdr;
2550
2551   load_base = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
2552
2553   for (i = 0; i < ehdr->e_phnum; ++i)
2554     {
2555       switch (phdr[i].p_type)
2556         {
2557         case PT_LOAD:
2558           if ((unw_word_t) (ip - load_base - phdr[i].p_vaddr)
2559               < phdr[i].p_memsz)
2560             p_text = phdr + i;
2561           break;
2562
2563         case PT_IA_64_UNWIND:
2564           p_unwind = phdr + i;
2565           break;
2566
2567         default:
2568           break;
2569         }
2570     }
2571
2572   if (!p_text || !p_unwind)
2573     return -UNW_ENOINFO;
2574
2575   /* Verify that the segment that contains the IP also contains
2576      the static unwind table.  If not, we may be in the Linux kernel's
2577      DSO gate page in which case the unwind table is another segment. 
2578      Otherwise, we are dealing with runtime-generated code, for which we 
2579      have no info here.  */
2580   segbase = p_text->p_vaddr + load_base;
2581
2582   if ((p_unwind->p_vaddr - p_text->p_vaddr) >= p_text->p_memsz)
2583     {
2584       int ok = 0;
2585       for (i = 0; i < ehdr->e_phnum; ++i)
2586         {
2587           if (phdr[i].p_type == PT_LOAD
2588               && (p_unwind->p_vaddr - phdr[i].p_vaddr) < phdr[i].p_memsz)
2589             {
2590               ok = 1;
2591               /* Get the segbase from the section containing the
2592                  libunwind table.  */
2593               segbase = phdr[i].p_vaddr + load_base;
2594             }
2595         }
2596       if (!ok)
2597         return -UNW_ENOINFO;
2598     }
2599
2600   dip->start_ip = p_text->p_vaddr + load_base;
2601   dip->end_ip = dip->start_ip + p_text->p_memsz;
2602   dip->gp = ia64_find_global_pointer (ip);
2603   dip->format = UNW_INFO_FORMAT_REMOTE_TABLE;
2604   dip->u.rti.name_ptr = (unw_word_t) bfd_get_filename (bfd);
2605   dip->u.rti.segbase = segbase;
2606   dip->u.rti.table_len = p_unwind->p_memsz / sizeof (unw_word_t);
2607   dip->u.rti.table_data = p_unwind->p_vaddr + load_base;
2608
2609   return 0;
2610 }
2611
2612 /* Libunwind callback accessor function to acquire procedure unwind-info.  */
2613 static int
2614 ia64_find_proc_info_x (unw_addr_space_t as, unw_word_t ip, unw_proc_info_t *pi,
2615                        int need_unwind_info, void *arg)
2616 {
2617   struct obj_section *sec = find_pc_section (ip);
2618   unw_dyn_info_t di;
2619   int ret;
2620   void *buf = NULL;
2621
2622   if (!sec)
2623     {
2624       /* XXX This only works if the host and the target architecture are
2625          both ia64 and if the have (more or less) the same kernel
2626          version.  */
2627       if (get_kernel_table (ip, &di) < 0)
2628         return -UNW_ENOINFO;
2629
2630       if (gdbarch_debug >= 1)
2631         fprintf_unfiltered (gdb_stdlog, "ia64_find_proc_info_x: 0x%s -> "
2632                             "(name=`%s',segbase=0x%s,start=0x%s,end=0x%s,gp=0x%s,"
2633                             "length=%s,data=0x%s)\n",
2634                             paddr_nz (ip), (char *)di.u.ti.name_ptr,
2635                             paddr_nz (di.u.ti.segbase), 
2636                             paddr_nz (di.start_ip), paddr_nz (di.end_ip),
2637                             paddr_nz (di.gp), 
2638                             paddr_u (di.u.ti.table_len), 
2639                             paddr_nz ((CORE_ADDR)di.u.ti.table_data));
2640     }
2641   else
2642     {
2643       ret = ia64_find_unwind_table (sec->objfile, ip, &di, &buf);
2644       if (ret < 0)
2645         return ret;
2646
2647       if (gdbarch_debug >= 1)
2648         fprintf_unfiltered (gdb_stdlog, "ia64_find_proc_info_x: 0x%s -> "
2649                             "(name=`%s',segbase=0x%s,start=0x%s,end=0x%s,gp=0x%s,"
2650                             "length=%s,data=0x%s)\n",
2651                             paddr_nz (ip), (char *)di.u.rti.name_ptr,
2652                             paddr_nz (di.u.rti.segbase), 
2653                             paddr_nz (di.start_ip), paddr_nz (di.end_ip),
2654                             paddr_nz (di.gp), 
2655                             paddr_u (di.u.rti.table_len), 
2656                             paddr_nz (di.u.rti.table_data));
2657     }
2658
2659   ret = libunwind_search_unwind_table (&as, ip, &di, pi, need_unwind_info,
2660                                        arg);
2661
2662   /* We no longer need the dyn info storage so free it.  */
2663   xfree (buf);
2664
2665   return ret;
2666 }
2667
2668 /* Libunwind callback accessor function for cleanup.  */
2669 static void
2670 ia64_put_unwind_info (unw_addr_space_t as,
2671                       unw_proc_info_t *pip, void *arg)
2672 {
2673   /* Nothing required for now.  */
2674 }
2675
2676 /* Libunwind callback accessor function to get head of the dynamic 
2677    unwind-info registration list.  */ 
2678 static int
2679 ia64_get_dyn_info_list (unw_addr_space_t as,
2680                         unw_word_t *dilap, void *arg)
2681 {
2682   struct obj_section *text_sec;
2683   struct objfile *objfile;
2684   unw_word_t ip, addr;
2685   unw_dyn_info_t di;
2686   int ret;
2687
2688   if (!libunwind_is_initialized ())
2689     return -UNW_ENOINFO;
2690
2691   for (objfile = object_files; objfile; objfile = objfile->next)
2692     {
2693       void *buf = NULL;
2694
2695       text_sec = objfile->sections + SECT_OFF_TEXT (objfile);
2696       ip = text_sec->addr;
2697       ret = ia64_find_unwind_table (objfile, ip, &di, &buf);
2698       if (ret >= 0)
2699         {
2700           addr = libunwind_find_dyn_list (as, &di, arg);
2701           /* We no longer need the dyn info storage so free it.  */
2702           xfree (buf);
2703
2704           if (addr)
2705             {
2706               if (gdbarch_debug >= 1)
2707                 fprintf_unfiltered (gdb_stdlog,
2708                                     "dynamic unwind table in objfile %s "
2709                                     "at 0x%s (gp=0x%s)\n",
2710                                     bfd_get_filename (objfile->obfd),
2711                                     paddr_nz (addr), paddr_nz (di.gp));
2712               *dilap = addr;
2713               return 0;
2714             }
2715         }
2716     }
2717   return -UNW_ENOINFO;
2718 }
2719
2720
2721 /* Frame interface functions for libunwind.  */
2722
2723 static void
2724 ia64_libunwind_frame_this_id (struct frame_info *next_frame, void **this_cache,
2725                               struct frame_id *this_id)
2726 {
2727   char buf[8];
2728   CORE_ADDR bsp;
2729   struct frame_id id;
2730   CORE_ADDR prev_ip, addr;
2731   int realnum, optimized;
2732   enum lval_type lval;
2733
2734
2735   libunwind_frame_this_id (next_frame, this_cache, &id);
2736   if (frame_id_eq (id, null_frame_id))
2737     {
2738       (*this_id) = null_frame_id;
2739       return;
2740     }
2741
2742   /* We must add the bsp as the special address for frame comparison 
2743      purposes.  */
2744   frame_unwind_register (next_frame, IA64_BSP_REGNUM, buf);
2745   bsp = extract_unsigned_integer (buf, 8);
2746
2747   /* If the previous frame pc value is 0, then we are at the end of the stack
2748      and don't want to unwind past this frame.  We return a null frame_id to
2749      indicate this.  */
2750   libunwind_frame_prev_register (next_frame, this_cache, IA64_IP_REGNUM, 
2751                                  &optimized, &lval, &addr, &realnum, buf);
2752   prev_ip = extract_unsigned_integer (buf, 8);
2753
2754   if (prev_ip != 0)
2755     (*this_id) = frame_id_build_special (id.stack_addr, id.code_addr, bsp);
2756   else
2757     (*this_id) = null_frame_id;
2758
2759   if (gdbarch_debug >= 1)
2760     fprintf_unfiltered (gdb_stdlog,
2761                         "libunwind frame id: code 0x%s, stack 0x%s, special 0x%s, next_frame %p\n",
2762                         paddr_nz (id.code_addr), paddr_nz (id.stack_addr), 
2763                         paddr_nz (bsp), next_frame);
2764 }
2765
2766 static void
2767 ia64_libunwind_frame_prev_register (struct frame_info *next_frame,
2768                                     void **this_cache,
2769                                     int regnum, int *optimizedp,
2770                                     enum lval_type *lvalp, CORE_ADDR *addrp,
2771                                     int *realnump, gdb_byte *valuep)
2772 {
2773   int reg = regnum;
2774
2775   if (VP0_REGNUM <= regnum && regnum <= VP63_REGNUM)
2776     reg = IA64_PR_REGNUM;
2777   else if (IA64_NAT0_REGNUM <= regnum && regnum <= IA64_NAT127_REGNUM)
2778     reg = IA64_UNAT_REGNUM;
2779
2780   /* Let libunwind do most of the work.  */
2781   libunwind_frame_prev_register (next_frame, this_cache, reg,
2782                                  optimizedp, lvalp, addrp, realnump, valuep);
2783
2784   /* No more to do if the value is not supposed to be supplied.  */
2785   if (!valuep)
2786     return;
2787
2788   if (VP0_REGNUM <= regnum && regnum <= VP63_REGNUM)
2789     {
2790       ULONGEST prN_val;
2791
2792       if (VP16_REGNUM <= regnum && regnum <= VP63_REGNUM)
2793         {
2794           int rrb_pr = 0;
2795           ULONGEST cfm;
2796           unsigned char buf[MAX_REGISTER_SIZE];
2797
2798           /* Fetch predicate register rename base from current frame
2799              marker for this frame.  */
2800           frame_unwind_register (next_frame, IA64_CFM_REGNUM, buf);
2801           cfm = extract_unsigned_integer (buf, 8); 
2802           rrb_pr = (cfm >> 32) & 0x3f;
2803           
2804           /* Adjust the register number to account for register rotation.  */
2805           regnum = VP16_REGNUM 
2806             + ((regnum - VP16_REGNUM) + rrb_pr) % 48;
2807         }
2808       prN_val = extract_bit_field ((unsigned char *) valuep,
2809                                    regnum - VP0_REGNUM, 1);
2810       store_unsigned_integer (valuep, register_size (current_gdbarch, regnum), prN_val);
2811     }
2812   else if (IA64_NAT0_REGNUM <= regnum && regnum <= IA64_NAT127_REGNUM)
2813     {
2814       ULONGEST unatN_val;
2815
2816       unatN_val = extract_bit_field ((unsigned char *) valuep,
2817                                    regnum - IA64_NAT0_REGNUM, 1);
2818       store_unsigned_integer (valuep, register_size (current_gdbarch, regnum), 
2819                               unatN_val);
2820     }
2821   else if (regnum == IA64_BSP_REGNUM)
2822     {
2823       char cfm_valuep[MAX_REGISTER_SIZE];
2824       int  cfm_optim;
2825       int  cfm_realnum;
2826       enum lval_type cfm_lval;
2827       CORE_ADDR cfm_addr;
2828       CORE_ADDR bsp, prev_cfm, prev_bsp;
2829
2830       /* We want to calculate the previous bsp as the end of the previous register stack frame.
2831          This corresponds to what the hardware bsp register will be if we pop the frame
2832          back which is why we might have been called.  We know that libunwind will pass us back
2833          the beginning of the current frame so we should just add sof to it. */
2834       prev_bsp = extract_unsigned_integer (valuep, 8);
2835       libunwind_frame_prev_register (next_frame, this_cache, IA64_CFM_REGNUM,
2836                                      &cfm_optim, &cfm_lval, &cfm_addr, &cfm_realnum, cfm_valuep);
2837       prev_cfm = extract_unsigned_integer (cfm_valuep, 8);
2838       prev_bsp = rse_address_add (prev_bsp, (prev_cfm & 0x7f));
2839
2840       store_unsigned_integer (valuep, register_size (current_gdbarch, regnum), 
2841                               prev_bsp);
2842     }
2843
2844   if (gdbarch_debug >= 1)
2845     fprintf_unfiltered (gdb_stdlog,
2846                         "libunwind prev register <%s> is 0x%s\n",
2847                         (regnum < IA64_GR32_REGNUM
2848                          || (regnum > IA64_GR127_REGNUM
2849                              && regnum < LAST_PSEUDO_REGNUM))
2850                          ? ia64_register_names[regnum]
2851                          : (regnum < LAST_PSEUDO_REGNUM
2852                             ? ia64_register_names[regnum-IA64_GR32_REGNUM+V32_REGNUM]
2853                             : "OUT_OF_RANGE"),
2854                         paddr_nz (extract_unsigned_integer (valuep, 8)));
2855 }
2856
2857 static const struct frame_unwind ia64_libunwind_frame_unwind =
2858 {
2859   NORMAL_FRAME,
2860   ia64_libunwind_frame_this_id,
2861   ia64_libunwind_frame_prev_register
2862 };
2863
2864 static const struct frame_unwind *
2865 ia64_libunwind_frame_sniffer (struct frame_info *next_frame)
2866 {
2867   if (libunwind_is_initialized () && libunwind_frame_sniffer (next_frame))
2868     return &ia64_libunwind_frame_unwind;
2869
2870   return NULL;
2871 }
2872
2873 static void
2874 ia64_libunwind_sigtramp_frame_this_id (struct frame_info *next_frame, void **this_cache,
2875                                        struct frame_id *this_id)
2876 {
2877   char buf[8];
2878   CORE_ADDR bsp;
2879   struct frame_id id;
2880   CORE_ADDR prev_ip;
2881
2882   libunwind_frame_this_id (next_frame, this_cache, &id);
2883   if (frame_id_eq (id, null_frame_id))
2884     {
2885       (*this_id) = null_frame_id;
2886       return;
2887     }
2888
2889   /* We must add the bsp as the special address for frame comparison 
2890      purposes.  */
2891   frame_unwind_register (next_frame, IA64_BSP_REGNUM, buf);
2892   bsp = extract_unsigned_integer (buf, 8);
2893
2894   /* For a sigtramp frame, we don't make the check for previous ip being 0.  */
2895   (*this_id) = frame_id_build_special (id.stack_addr, id.code_addr, bsp);
2896
2897   if (gdbarch_debug >= 1)
2898     fprintf_unfiltered (gdb_stdlog,
2899                         "libunwind sigtramp frame id: code 0x%s, stack 0x%s, special 0x%s, next_frame %p\n",
2900                         paddr_nz (id.code_addr), paddr_nz (id.stack_addr), 
2901                         paddr_nz (bsp), next_frame);
2902 }
2903
2904 static void
2905 ia64_libunwind_sigtramp_frame_prev_register (struct frame_info *next_frame,
2906                                              void **this_cache,
2907                                              int regnum, int *optimizedp,
2908                                              enum lval_type *lvalp, CORE_ADDR *addrp,
2909                                              int *realnump, gdb_byte *valuep)
2910
2911 {
2912   gdb_byte buf[8];
2913   CORE_ADDR prev_ip, addr;
2914   int realnum, optimized;
2915   enum lval_type lval;
2916
2917
2918   /* If the previous frame pc value is 0, then we want to use the SIGCONTEXT
2919      method of getting previous registers.  */
2920   libunwind_frame_prev_register (next_frame, this_cache, IA64_IP_REGNUM, 
2921                                  &optimized, &lval, &addr, &realnum, buf);
2922   prev_ip = extract_unsigned_integer (buf, 8);
2923
2924   if (prev_ip == 0)
2925     {
2926       void *tmp_cache = NULL;
2927       ia64_sigtramp_frame_prev_register (next_frame, &tmp_cache, regnum, optimizedp, lvalp,
2928                                          addrp, realnump, valuep);
2929     }
2930   else
2931     ia64_libunwind_frame_prev_register (next_frame, this_cache, regnum, optimizedp, lvalp,
2932                                         addrp, realnump, valuep);
2933 }
2934
2935 static const struct frame_unwind ia64_libunwind_sigtramp_frame_unwind =
2936 {
2937   SIGTRAMP_FRAME,
2938   ia64_libunwind_sigtramp_frame_this_id,
2939   ia64_libunwind_sigtramp_frame_prev_register
2940 };
2941
2942 static const struct frame_unwind *
2943 ia64_libunwind_sigtramp_frame_sniffer (struct frame_info *next_frame)
2944 {
2945   if (libunwind_is_initialized ())
2946     {
2947       if (libunwind_sigtramp_frame_sniffer (next_frame))
2948         return &ia64_libunwind_sigtramp_frame_unwind;
2949       return NULL;
2950     }
2951   else
2952     return ia64_sigtramp_frame_sniffer (next_frame);
2953 }
2954
2955 /* Set of libunwind callback acccessor functions.  */
2956 static unw_accessors_t ia64_unw_accessors =
2957 {
2958   ia64_find_proc_info_x,
2959   ia64_put_unwind_info,
2960   ia64_get_dyn_info_list,
2961   ia64_access_mem,
2962   ia64_access_reg,
2963   ia64_access_fpreg,
2964   /* resume */
2965   /* get_proc_name */
2966 };
2967
2968 /* Set of special libunwind callback acccessor functions specific for accessing
2969    the rse registers.  At the top of the stack, we want libunwind to figure out
2970    how to read r32 - r127.  Though usually they are found sequentially in memory
2971    starting from $bof, this is not always true.  */
2972 static unw_accessors_t ia64_unw_rse_accessors =
2973 {
2974   ia64_find_proc_info_x,
2975   ia64_put_unwind_info,
2976   ia64_get_dyn_info_list,
2977   ia64_access_mem,
2978   ia64_access_rse_reg,
2979   ia64_access_fpreg,
2980   /* resume */
2981   /* get_proc_name */
2982 };
2983
2984 /* Set of ia64 gdb libunwind-frame callbacks and data for generic libunwind-frame code to use.  */
2985 static struct libunwind_descr ia64_libunwind_descr =
2986 {
2987   ia64_gdb2uw_regnum, 
2988   ia64_uw2gdb_regnum, 
2989   ia64_is_fpreg, 
2990   &ia64_unw_accessors,
2991   &ia64_unw_rse_accessors,
2992 };
2993
2994 #endif /* HAVE_LIBUNWIND_IA64_H  */
2995
2996 /* Should we use DEPRECATED_EXTRACT_STRUCT_VALUE_ADDRESS instead of
2997    EXTRACT_RETURN_VALUE?  GCC_P is true if compiled with gcc and TYPE
2998    is the type (which is known to be struct, union or array).  */
2999 int
3000 ia64_use_struct_convention (int gcc_p, struct type *type)
3001 {
3002   struct type *float_elt_type;
3003
3004   /* HFAs are structures (or arrays) consisting entirely of floating
3005      point values of the same length.  Up to 8 of these are returned
3006      in registers.  Don't use the struct convention when this is the
3007      case.  */
3008   float_elt_type = is_float_or_hfa_type (type);
3009   if (float_elt_type != NULL
3010       && TYPE_LENGTH (type) / TYPE_LENGTH (float_elt_type) <= 8)
3011     return 0;
3012
3013   /* Other structs of length 32 or less are returned in r8-r11.
3014      Don't use the struct convention for those either.  */
3015   return TYPE_LENGTH (type) > 32;
3016 }
3017
3018 void
3019 ia64_extract_return_value (struct type *type, struct regcache *regcache,
3020                            gdb_byte *valbuf)
3021 {
3022   struct type *float_elt_type;
3023
3024   float_elt_type = is_float_or_hfa_type (type);
3025   if (float_elt_type != NULL)
3026     {
3027       char from[MAX_REGISTER_SIZE];
3028       int offset = 0;
3029       int regnum = IA64_FR8_REGNUM;
3030       int n = TYPE_LENGTH (type) / TYPE_LENGTH (float_elt_type);
3031
3032       while (n-- > 0)
3033         {
3034           regcache_cooked_read (regcache, regnum, from);
3035           convert_typed_floating (from, builtin_type_ia64_ext,
3036                                   (char *)valbuf + offset, float_elt_type);       
3037           offset += TYPE_LENGTH (float_elt_type);
3038           regnum++;
3039         }
3040     }
3041   else
3042     {
3043       ULONGEST val;
3044       int offset = 0;
3045       int regnum = IA64_GR8_REGNUM;
3046       int reglen = TYPE_LENGTH (register_type (get_regcache_arch (regcache),
3047                                                IA64_GR8_REGNUM));
3048       int n = TYPE_LENGTH (type) / reglen;
3049       int m = TYPE_LENGTH (type) % reglen;
3050
3051       while (n-- > 0)
3052         {
3053           ULONGEST val;
3054           regcache_cooked_read_unsigned (regcache, regnum, &val);
3055           memcpy ((char *)valbuf + offset, &val, reglen);
3056           offset += reglen;
3057           regnum++;
3058         }
3059
3060       if (m)
3061         {
3062           regcache_cooked_read_unsigned (regcache, regnum, &val);
3063           memcpy ((char *)valbuf + offset, &val, m);
3064         }
3065     }
3066 }
3067
3068 CORE_ADDR
3069 ia64_extract_struct_value_address (struct regcache *regcache)
3070 {
3071   error (_("ia64_extract_struct_value_address called and cannot get struct value address"));
3072   return 0;
3073 }
3074
3075
3076 static int
3077 is_float_or_hfa_type_recurse (struct type *t, struct type **etp)
3078 {
3079   switch (TYPE_CODE (t))
3080     {
3081     case TYPE_CODE_FLT:
3082       if (*etp)
3083         return TYPE_LENGTH (*etp) == TYPE_LENGTH (t);
3084       else
3085         {
3086           *etp = t;
3087           return 1;
3088         }
3089       break;
3090     case TYPE_CODE_ARRAY:
3091       return
3092         is_float_or_hfa_type_recurse (check_typedef (TYPE_TARGET_TYPE (t)),
3093                                       etp);
3094       break;
3095     case TYPE_CODE_STRUCT:
3096       {
3097         int i;
3098
3099         for (i = 0; i < TYPE_NFIELDS (t); i++)
3100           if (!is_float_or_hfa_type_recurse
3101               (check_typedef (TYPE_FIELD_TYPE (t, i)), etp))
3102             return 0;
3103         return 1;
3104       }
3105       break;
3106     default:
3107       return 0;
3108       break;
3109     }
3110 }
3111
3112 /* Determine if the given type is one of the floating point types or
3113    and HFA (which is a struct, array, or combination thereof whose
3114    bottom-most elements are all of the same floating point type).  */
3115
3116 static struct type *
3117 is_float_or_hfa_type (struct type *t)
3118 {
3119   struct type *et = 0;
3120
3121   return is_float_or_hfa_type_recurse (t, &et) ? et : 0;
3122 }
3123
3124
3125 /* Return 1 if the alignment of T is such that the next even slot
3126    should be used.  Return 0, if the next available slot should
3127    be used.  (See section 8.5.1 of the IA-64 Software Conventions
3128    and Runtime manual).  */
3129
3130 static int
3131 slot_alignment_is_next_even (struct type *t)
3132 {
3133   switch (TYPE_CODE (t))
3134     {
3135     case TYPE_CODE_INT:
3136     case TYPE_CODE_FLT:
3137       if (TYPE_LENGTH (t) > 8)
3138         return 1;
3139       else
3140         return 0;
3141     case TYPE_CODE_ARRAY:
3142       return
3143         slot_alignment_is_next_even (check_typedef (TYPE_TARGET_TYPE (t)));
3144     case TYPE_CODE_STRUCT:
3145       {
3146         int i;
3147
3148         for (i = 0; i < TYPE_NFIELDS (t); i++)
3149           if (slot_alignment_is_next_even
3150               (check_typedef (TYPE_FIELD_TYPE (t, i))))
3151             return 1;
3152         return 0;
3153       }
3154     default:
3155       return 0;
3156     }
3157 }
3158
3159 /* Attempt to find (and return) the global pointer for the given
3160    function.
3161
3162    This is a rather nasty bit of code searchs for the .dynamic section
3163    in the objfile corresponding to the pc of the function we're trying
3164    to call.  Once it finds the addresses at which the .dynamic section
3165    lives in the child process, it scans the Elf64_Dyn entries for a
3166    DT_PLTGOT tag.  If it finds one of these, the corresponding
3167    d_un.d_ptr value is the global pointer.  */
3168
3169 static CORE_ADDR
3170 ia64_find_global_pointer (CORE_ADDR faddr)
3171 {
3172   struct obj_section *faddr_sect;
3173      
3174   faddr_sect = find_pc_section (faddr);
3175   if (faddr_sect != NULL)
3176     {
3177       struct obj_section *osect;
3178
3179       ALL_OBJFILE_OSECTIONS (faddr_sect->objfile, osect)
3180         {
3181           if (strcmp (osect->the_bfd_section->name, ".dynamic") == 0)
3182             break;
3183         }
3184
3185       if (osect < faddr_sect->objfile->sections_end)
3186         {
3187           CORE_ADDR addr;
3188
3189           addr = osect->addr;
3190           while (addr < osect->endaddr)
3191             {
3192               int status;
3193               LONGEST tag;
3194               char buf[8];
3195
3196               status = target_read_memory (addr, buf, sizeof (buf));
3197               if (status != 0)
3198                 break;
3199               tag = extract_signed_integer (buf, sizeof (buf));
3200
3201               if (tag == DT_PLTGOT)
3202                 {
3203                   CORE_ADDR global_pointer;
3204
3205                   status = target_read_memory (addr + 8, buf, sizeof (buf));
3206                   if (status != 0)
3207                     break;
3208                   global_pointer = extract_unsigned_integer (buf, sizeof (buf));
3209
3210                   /* The payoff... */
3211                   return global_pointer;
3212                 }
3213
3214               if (tag == DT_NULL)
3215                 break;
3216
3217               addr += 16;
3218             }
3219         }
3220     }
3221   return 0;
3222 }
3223
3224 /* Given a function's address, attempt to find (and return) the
3225    corresponding (canonical) function descriptor.  Return 0 if
3226    not found.  */
3227 static CORE_ADDR
3228 find_extant_func_descr (CORE_ADDR faddr)
3229 {
3230   struct obj_section *faddr_sect;
3231
3232   /* Return early if faddr is already a function descriptor.  */
3233   faddr_sect = find_pc_section (faddr);
3234   if (faddr_sect && strcmp (faddr_sect->the_bfd_section->name, ".opd") == 0)
3235     return faddr;
3236
3237   if (faddr_sect != NULL)
3238     {
3239       struct obj_section *osect;
3240       ALL_OBJFILE_OSECTIONS (faddr_sect->objfile, osect)
3241         {
3242           if (strcmp (osect->the_bfd_section->name, ".opd") == 0)
3243             break;
3244         }
3245
3246       if (osect < faddr_sect->objfile->sections_end)
3247         {
3248           CORE_ADDR addr;
3249
3250           addr = osect->addr;
3251           while (addr < osect->endaddr)
3252             {
3253               int status;
3254               LONGEST faddr2;
3255               char buf[8];
3256
3257               status = target_read_memory (addr, buf, sizeof (buf));
3258               if (status != 0)
3259                 break;
3260               faddr2 = extract_signed_integer (buf, sizeof (buf));
3261
3262               if (faddr == faddr2)
3263                 return addr;
3264
3265               addr += 16;
3266             }
3267         }
3268     }
3269   return 0;
3270 }
3271
3272 /* Attempt to find a function descriptor corresponding to the
3273    given address.  If none is found, construct one on the
3274    stack using the address at fdaptr.  */
3275
3276 static CORE_ADDR
3277 find_func_descr (CORE_ADDR faddr, CORE_ADDR *fdaptr)
3278 {
3279   CORE_ADDR fdesc;
3280
3281   fdesc = find_extant_func_descr (faddr);
3282
3283   if (fdesc == 0)
3284     {
3285       CORE_ADDR global_pointer;
3286       char buf[16];
3287
3288       fdesc = *fdaptr;
3289       *fdaptr += 16;
3290
3291       global_pointer = ia64_find_global_pointer (faddr);
3292
3293       if (global_pointer == 0)
3294         global_pointer = read_register (IA64_GR1_REGNUM);
3295
3296       store_unsigned_integer (buf, 8, faddr);
3297       store_unsigned_integer (buf + 8, 8, global_pointer);
3298
3299       write_memory (fdesc, buf, 16);
3300     }
3301
3302   return fdesc; 
3303 }
3304
3305 /* Use the following routine when printing out function pointers
3306    so the user can see the function address rather than just the
3307    function descriptor.  */
3308 static CORE_ADDR
3309 ia64_convert_from_func_ptr_addr (struct gdbarch *gdbarch, CORE_ADDR addr,
3310                                  struct target_ops *targ)
3311 {
3312   struct obj_section *s;
3313
3314   s = find_pc_section (addr);
3315
3316   /* check if ADDR points to a function descriptor.  */
3317   if (s && strcmp (s->the_bfd_section->name, ".opd") == 0)
3318     return read_memory_unsigned_integer (addr, 8);
3319
3320   /* There are also descriptors embedded in vtables.  */
3321   if (s)
3322     {
3323       struct minimal_symbol *minsym;
3324
3325       minsym = lookup_minimal_symbol_by_pc (addr);
3326
3327       if (minsym && is_vtable_name (SYMBOL_LINKAGE_NAME (minsym)))
3328         return read_memory_unsigned_integer (addr, 8);
3329     }
3330
3331   return addr;
3332 }
3333
3334 static CORE_ADDR
3335 ia64_frame_align (struct gdbarch *gdbarch, CORE_ADDR sp)
3336 {
3337   return sp & ~0xfLL;
3338 }
3339
3340 static CORE_ADDR
3341 ia64_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
3342                       struct regcache *regcache, CORE_ADDR bp_addr,
3343                       int nargs, struct value **args, CORE_ADDR sp,
3344                       int struct_return, CORE_ADDR struct_addr)
3345 {
3346   int argno;
3347   struct value *arg;
3348   struct type *type;
3349   int len, argoffset;
3350   int nslots, rseslots, memslots, slotnum, nfuncargs;
3351   int floatreg;
3352   CORE_ADDR bsp, cfm, pfs, new_bsp, funcdescaddr, pc, global_pointer;
3353   CORE_ADDR func_addr = find_function_addr (function, NULL);
3354
3355   nslots = 0;
3356   nfuncargs = 0;
3357   /* Count the number of slots needed for the arguments.  */
3358   for (argno = 0; argno < nargs; argno++)
3359     {
3360       arg = args[argno];
3361       type = check_typedef (value_type (arg));
3362       len = TYPE_LENGTH (type);
3363
3364       if ((nslots & 1) && slot_alignment_is_next_even (type))
3365         nslots++;
3366
3367       if (TYPE_CODE (type) == TYPE_CODE_FUNC)
3368         nfuncargs++;
3369
3370       nslots += (len + 7) / 8;
3371     }
3372
3373   /* Divvy up the slots between the RSE and the memory stack.  */
3374   rseslots = (nslots > 8) ? 8 : nslots;
3375   memslots = nslots - rseslots;
3376
3377   /* Allocate a new RSE frame.  */
3378   cfm = read_register (IA64_CFM_REGNUM);
3379
3380   bsp = read_register (IA64_BSP_REGNUM);
3381   new_bsp = rse_address_add (bsp, rseslots);
3382   write_register (IA64_BSP_REGNUM, new_bsp);
3383
3384   pfs = read_register (IA64_PFS_REGNUM);
3385   pfs &= 0xc000000000000000LL;
3386   pfs |= (cfm & 0xffffffffffffLL);
3387   write_register (IA64_PFS_REGNUM, pfs);
3388
3389   cfm &= 0xc000000000000000LL;
3390   cfm |= rseslots;
3391   write_register (IA64_CFM_REGNUM, cfm);
3392   
3393   /* We will attempt to find function descriptors in the .opd segment,
3394      but if we can't we'll construct them ourselves.  That being the
3395      case, we'll need to reserve space on the stack for them.  */
3396   funcdescaddr = sp - nfuncargs * 16;
3397   funcdescaddr &= ~0xfLL;
3398
3399   /* Adjust the stack pointer to it's new value.  The calling conventions
3400      require us to have 16 bytes of scratch, plus whatever space is
3401      necessary for the memory slots and our function descriptors.  */
3402   sp = sp - 16 - (memslots + nfuncargs) * 8;
3403   sp &= ~0xfLL;                         /* Maintain 16 byte alignment.  */
3404
3405   /* Place the arguments where they belong.  The arguments will be
3406      either placed in the RSE backing store or on the memory stack.
3407      In addition, floating point arguments or HFAs are placed in
3408      floating point registers.  */
3409   slotnum = 0;
3410   floatreg = IA64_FR8_REGNUM;
3411   for (argno = 0; argno < nargs; argno++)
3412     {
3413       struct type *float_elt_type;
3414
3415       arg = args[argno];
3416       type = check_typedef (value_type (arg));
3417       len = TYPE_LENGTH (type);
3418
3419       /* Special handling for function parameters.  */
3420       if (len == 8 
3421           && TYPE_CODE (type) == TYPE_CODE_PTR 
3422           && TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_FUNC)
3423         {
3424           char val_buf[8];
3425
3426           store_unsigned_integer (val_buf, 8,
3427                                   find_func_descr (extract_unsigned_integer (value_contents (arg), 8),
3428                                                    &funcdescaddr));
3429           if (slotnum < rseslots)
3430             write_memory (rse_address_add (bsp, slotnum), val_buf, 8);
3431           else
3432             write_memory (sp + 16 + 8 * (slotnum - rseslots), val_buf, 8);
3433           slotnum++;
3434           continue;
3435         }
3436
3437       /* Normal slots.  */
3438
3439       /* Skip odd slot if necessary...  */
3440       if ((slotnum & 1) && slot_alignment_is_next_even (type))
3441         slotnum++;
3442
3443       argoffset = 0;
3444       while (len > 0)
3445         {
3446           char val_buf[8];
3447
3448           memset (val_buf, 0, 8);
3449           memcpy (val_buf, value_contents (arg) + argoffset, (len > 8) ? 8 : len);
3450
3451           if (slotnum < rseslots)
3452             write_memory (rse_address_add (bsp, slotnum), val_buf, 8);
3453           else
3454             write_memory (sp + 16 + 8 * (slotnum - rseslots), val_buf, 8);
3455
3456           argoffset += 8;
3457           len -= 8;
3458           slotnum++;
3459         }
3460
3461       /* Handle floating point types (including HFAs).  */
3462       float_elt_type = is_float_or_hfa_type (type);
3463       if (float_elt_type != NULL)
3464         {
3465           argoffset = 0;
3466           len = TYPE_LENGTH (type);
3467           while (len > 0 && floatreg < IA64_FR16_REGNUM)
3468             {
3469               char to[MAX_REGISTER_SIZE];
3470               convert_typed_floating (value_contents (arg) + argoffset, float_elt_type,
3471                                       to, builtin_type_ia64_ext);
3472               regcache_cooked_write (regcache, floatreg, (void *)to);
3473               floatreg++;
3474               argoffset += TYPE_LENGTH (float_elt_type);
3475               len -= TYPE_LENGTH (float_elt_type);
3476             }
3477         }
3478     }
3479
3480   /* Store the struct return value in r8 if necessary.  */
3481   if (struct_return)
3482     {
3483       regcache_cooked_write_unsigned (regcache, IA64_GR8_REGNUM, (ULONGEST)struct_addr);
3484     }
3485
3486   global_pointer = ia64_find_global_pointer (func_addr);
3487
3488   if (global_pointer != 0)
3489     write_register (IA64_GR1_REGNUM, global_pointer);
3490
3491   write_register (IA64_BR0_REGNUM, bp_addr);
3492
3493   write_register (sp_regnum, sp);
3494
3495   return sp;
3496 }
3497
3498 static struct frame_id
3499 ia64_unwind_dummy_id (struct gdbarch *gdbarch, struct frame_info *next_frame)
3500 {
3501   char buf[8];
3502   CORE_ADDR sp, bsp;
3503
3504   frame_unwind_register (next_frame, sp_regnum, buf);
3505   sp = extract_unsigned_integer (buf, 8);
3506
3507   frame_unwind_register (next_frame, IA64_BSP_REGNUM, buf);
3508   bsp = extract_unsigned_integer (buf, 8);
3509
3510   if (gdbarch_debug >= 1)
3511     fprintf_unfiltered (gdb_stdlog,
3512                         "dummy frame id: code 0x%s, stack 0x%s, special 0x%s\n",
3513                         paddr_nz (frame_pc_unwind (next_frame)), 
3514                         paddr_nz (sp), paddr_nz (bsp));
3515
3516   return frame_id_build_special (sp, frame_pc_unwind (next_frame), bsp);
3517 }
3518
3519 static CORE_ADDR 
3520 ia64_unwind_pc (struct gdbarch *gdbarch, struct frame_info *next_frame)
3521 {
3522   char buf[8];
3523   CORE_ADDR ip, psr, pc;
3524
3525   frame_unwind_register (next_frame, IA64_IP_REGNUM, buf);
3526   ip = extract_unsigned_integer (buf, 8);
3527   frame_unwind_register (next_frame, IA64_PSR_REGNUM, buf);
3528   psr = extract_unsigned_integer (buf, 8);
3529  
3530   pc = (ip & ~0xf) | ((psr >> 41) & 3);
3531   return pc;
3532 }
3533
3534 static void
3535 ia64_store_return_value (struct type *type, struct regcache *regcache, 
3536                         const gdb_byte *valbuf)
3537 {
3538   if (TYPE_CODE (type) == TYPE_CODE_FLT)
3539     {
3540       char to[MAX_REGISTER_SIZE];
3541       convert_typed_floating (valbuf, type, to, builtin_type_ia64_ext);
3542       regcache_cooked_write (regcache, IA64_FR8_REGNUM, (void *)to);
3543       target_store_registers (regcache, IA64_FR8_REGNUM);
3544     }
3545   else
3546     regcache_cooked_write (regcache, IA64_GR8_REGNUM, valbuf);
3547 }
3548
3549 static void
3550 ia64_remote_translate_xfer_address (struct gdbarch *gdbarch,
3551                                     struct regcache *regcache,
3552                                     CORE_ADDR memaddr, int nr_bytes,
3553                                     CORE_ADDR *targ_addr, int *targ_len)
3554 {
3555   *targ_addr = memaddr;
3556   *targ_len  = nr_bytes;
3557 }
3558
3559 static int
3560 ia64_print_insn (bfd_vma memaddr, struct disassemble_info *info)
3561 {
3562   info->bytes_per_line = SLOT_MULTIPLIER;
3563   return print_insn_ia64 (memaddr, info);
3564 }
3565
3566 static struct gdbarch *
3567 ia64_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
3568 {
3569   struct gdbarch *gdbarch;
3570   struct gdbarch_tdep *tdep;
3571
3572   /* If there is already a candidate, use it.  */
3573   arches = gdbarch_list_lookup_by_info (arches, &info);
3574   if (arches != NULL)
3575     return arches->gdbarch;
3576
3577   tdep = xmalloc (sizeof (struct gdbarch_tdep));
3578   gdbarch = gdbarch_alloc (&info, tdep);
3579
3580   tdep->sigcontext_register_address = 0;
3581   tdep->pc_in_sigtramp = 0;
3582
3583   /* Define the ia64 floating-point format to gdb.  */
3584   builtin_type_ia64_ext =
3585     init_type (TYPE_CODE_FLT, 128 / 8,
3586                0, "builtin_type_ia64_ext", NULL);
3587   TYPE_FLOATFORMAT (builtin_type_ia64_ext) = floatformats_ia64_ext;
3588
3589   /* According to the ia64 specs, instructions that store long double
3590      floats in memory use a long-double format different than that
3591      used in the floating registers.  The memory format matches the
3592      x86 extended float format which is 80 bits.  An OS may choose to
3593      use this format (e.g. GNU/Linux) or choose to use a different
3594      format for storing long doubles (e.g. HPUX).  In the latter case,
3595      the setting of the format may be moved/overridden in an
3596      OS-specific tdep file.  */
3597   set_gdbarch_long_double_format (gdbarch, floatformats_i387_ext);
3598
3599   set_gdbarch_short_bit (gdbarch, 16);
3600   set_gdbarch_int_bit (gdbarch, 32);
3601   set_gdbarch_long_bit (gdbarch, 64);
3602   set_gdbarch_long_long_bit (gdbarch, 64);
3603   set_gdbarch_float_bit (gdbarch, 32);
3604   set_gdbarch_double_bit (gdbarch, 64);
3605   set_gdbarch_long_double_bit (gdbarch, 128);
3606   set_gdbarch_ptr_bit (gdbarch, 64);
3607
3608   set_gdbarch_num_regs (gdbarch, NUM_IA64_RAW_REGS);
3609   set_gdbarch_num_pseudo_regs (gdbarch, LAST_PSEUDO_REGNUM - FIRST_PSEUDO_REGNUM);
3610   set_gdbarch_sp_regnum (gdbarch, sp_regnum);
3611   set_gdbarch_fp0_regnum (gdbarch, IA64_FR0_REGNUM);
3612
3613   set_gdbarch_register_name (gdbarch, ia64_register_name);
3614   /* FIXME:  Following interface should not be needed, however, without it recurse.exp
3615      gets a number of extra failures.  */
3616   set_gdbarch_deprecated_register_size (gdbarch, 8);
3617   set_gdbarch_register_type (gdbarch, ia64_register_type);
3618
3619   set_gdbarch_pseudo_register_read (gdbarch, ia64_pseudo_register_read);
3620   set_gdbarch_pseudo_register_write (gdbarch, ia64_pseudo_register_write);
3621   set_gdbarch_dwarf2_reg_to_regnum (gdbarch, ia64_dwarf_reg_to_regnum);
3622   set_gdbarch_register_reggroup_p (gdbarch, ia64_register_reggroup_p);
3623   set_gdbarch_convert_register_p (gdbarch, ia64_convert_register_p);
3624   set_gdbarch_register_to_value (gdbarch, ia64_register_to_value);
3625   set_gdbarch_value_to_register (gdbarch, ia64_value_to_register);
3626
3627   set_gdbarch_skip_prologue (gdbarch, ia64_skip_prologue);
3628
3629   set_gdbarch_deprecated_use_struct_convention (gdbarch, ia64_use_struct_convention);
3630   set_gdbarch_extract_return_value (gdbarch, ia64_extract_return_value);
3631
3632   set_gdbarch_store_return_value (gdbarch, ia64_store_return_value);
3633   set_gdbarch_deprecated_extract_struct_value_address (gdbarch, ia64_extract_struct_value_address);
3634
3635   set_gdbarch_memory_insert_breakpoint (gdbarch, ia64_memory_insert_breakpoint);
3636   set_gdbarch_memory_remove_breakpoint (gdbarch, ia64_memory_remove_breakpoint);
3637   set_gdbarch_breakpoint_from_pc (gdbarch, ia64_breakpoint_from_pc);
3638   set_gdbarch_read_pc (gdbarch, ia64_read_pc);
3639   set_gdbarch_write_pc (gdbarch, ia64_write_pc);
3640
3641   /* Settings for calling functions in the inferior.  */
3642   set_gdbarch_push_dummy_call (gdbarch, ia64_push_dummy_call);
3643   set_gdbarch_frame_align (gdbarch, ia64_frame_align);
3644   set_gdbarch_unwind_dummy_id (gdbarch, ia64_unwind_dummy_id);
3645
3646   set_gdbarch_unwind_pc (gdbarch, ia64_unwind_pc);
3647 #ifdef HAVE_LIBUNWIND_IA64_H
3648   frame_unwind_append_sniffer (gdbarch, ia64_libunwind_sigtramp_frame_sniffer);
3649   frame_unwind_append_sniffer (gdbarch, ia64_libunwind_frame_sniffer);
3650   libunwind_frame_set_descr (gdbarch, &ia64_libunwind_descr);
3651 #else
3652   frame_unwind_append_sniffer (gdbarch, ia64_sigtramp_frame_sniffer);
3653 #endif
3654   frame_unwind_append_sniffer (gdbarch, ia64_frame_sniffer);
3655   frame_base_set_default (gdbarch, &ia64_frame_base);
3656
3657   /* Settings that should be unnecessary.  */
3658   set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
3659
3660   set_gdbarch_remote_translate_xfer_address (
3661     gdbarch, ia64_remote_translate_xfer_address);
3662
3663   set_gdbarch_print_insn (gdbarch, ia64_print_insn);
3664   set_gdbarch_convert_from_func_ptr_addr (gdbarch, ia64_convert_from_func_ptr_addr);
3665
3666   /* The virtual table contains 16-byte descriptors, not pointers to
3667      descriptors.  */
3668   set_gdbarch_vtable_function_descriptors (gdbarch, 1);
3669
3670   /* Hook in ABI-specific overrides, if they have been registered.  */
3671   gdbarch_init_osabi (info, gdbarch);
3672
3673   return gdbarch;
3674 }
3675
3676 extern initialize_file_ftype _initialize_ia64_tdep; /* -Wmissing-prototypes */
3677
3678 void
3679 _initialize_ia64_tdep (void)
3680 {
3681   gdbarch_register (bfd_arch_ia64, ia64_gdbarch_init, NULL);
3682 }