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