run copyright.sh for 2011.
[external/binutils.git] / gdb / alpha-tdep.c
1 /* Target-dependent code for the ALPHA architecture, for GDB, the GNU Debugger.
2
3    Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
4    2003, 2005, 2006, 2007, 2008, 2009, 2010, 2011
5    Free Software Foundation, Inc.
6
7    This file is part of GDB.
8
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 3 of the License, or
12    (at your option) any later version.
13
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18
19    You should have received a copy of the GNU General Public License
20    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
21
22 #include "defs.h"
23 #include "doublest.h"
24 #include "frame.h"
25 #include "frame-unwind.h"
26 #include "frame-base.h"
27 #include "dwarf2-frame.h"
28 #include "inferior.h"
29 #include "symtab.h"
30 #include "value.h"
31 #include "gdbcmd.h"
32 #include "gdbcore.h"
33 #include "dis-asm.h"
34 #include "symfile.h"
35 #include "objfiles.h"
36 #include "gdb_string.h"
37 #include "linespec.h"
38 #include "regcache.h"
39 #include "reggroups.h"
40 #include "arch-utils.h"
41 #include "osabi.h"
42 #include "block.h"
43 #include "infcall.h"
44 #include "trad-frame.h"
45
46 #include "elf-bfd.h"
47
48 #include "alpha-tdep.h"
49
50 /* Instruction decoding.  The notations for registers, immediates and
51    opcodes are the same as the one used in Compaq's Alpha architecture
52    handbook.  */
53
54 #define INSN_OPCODE(insn) ((insn & 0xfc000000) >> 26)
55
56 /* Memory instruction format */
57 #define MEM_RA(insn) ((insn & 0x03e00000) >> 21)
58 #define MEM_RB(insn) ((insn & 0x001f0000) >> 16)
59 #define MEM_DISP(insn) \
60   (((insn & 0x8000) == 0) ? (insn & 0xffff) : -((-insn) & 0xffff))
61
62 static const int lda_opcode = 0x08;
63 static const int stq_opcode = 0x2d;
64
65 /* Branch instruction format */
66 #define BR_RA(insn) MEM_RA(insn)
67
68 static const int bne_opcode = 0x3d;
69
70 /* Operate instruction format */
71 #define OPR_FUNCTION(insn) ((insn & 0xfe0) >> 5)
72 #define OPR_HAS_IMMEDIATE(insn) ((insn & 0x1000) == 0x1000)
73 #define OPR_RA(insn) MEM_RA(insn)
74 #define OPR_RC(insn) ((insn & 0x1f))
75 #define OPR_LIT(insn) ((insn & 0x1fe000) >> 13)
76
77 static const int subq_opcode = 0x10;
78 static const int subq_function = 0x29;
79
80 \f
81 /* Return the name of the REGNO register.
82
83    An empty name corresponds to a register number that used to
84    be used for a virtual register. That virtual register has
85    been removed, but the index is still reserved to maintain
86    compatibility with existing remote alpha targets.  */
87
88 static const char *
89 alpha_register_name (struct gdbarch *gdbarch, int regno)
90 {
91   static const char * const register_names[] =
92   {
93     "v0",   "t0",   "t1",   "t2",   "t3",   "t4",   "t5",   "t6",
94     "t7",   "s0",   "s1",   "s2",   "s3",   "s4",   "s5",   "fp",
95     "a0",   "a1",   "a2",   "a3",   "a4",   "a5",   "t8",   "t9",
96     "t10",  "t11",  "ra",   "t12",  "at",   "gp",   "sp",   "zero",
97     "f0",   "f1",   "f2",   "f3",   "f4",   "f5",   "f6",   "f7",
98     "f8",   "f9",   "f10",  "f11",  "f12",  "f13",  "f14",  "f15",
99     "f16",  "f17",  "f18",  "f19",  "f20",  "f21",  "f22",  "f23",
100     "f24",  "f25",  "f26",  "f27",  "f28",  "f29",  "f30",  "fpcr",
101     "pc",   "",     "unique"
102   };
103
104   if (regno < 0)
105     return NULL;
106   if (regno >= ARRAY_SIZE(register_names))
107     return NULL;
108   return register_names[regno];
109 }
110
111 static int
112 alpha_cannot_fetch_register (struct gdbarch *gdbarch, int regno)
113 {
114   return (regno == ALPHA_ZERO_REGNUM
115           || strlen (alpha_register_name (gdbarch, regno)) == 0);
116 }
117
118 static int
119 alpha_cannot_store_register (struct gdbarch *gdbarch, int regno)
120 {
121   return (regno == ALPHA_ZERO_REGNUM
122           || strlen (alpha_register_name (gdbarch, regno)) == 0);
123 }
124
125 static struct type *
126 alpha_register_type (struct gdbarch *gdbarch, int regno)
127 {
128   if (regno == ALPHA_SP_REGNUM || regno == ALPHA_GP_REGNUM)
129     return builtin_type (gdbarch)->builtin_data_ptr;
130   if (regno == ALPHA_PC_REGNUM)
131     return builtin_type (gdbarch)->builtin_func_ptr;
132
133   /* Don't need to worry about little vs big endian until 
134      some jerk tries to port to alpha-unicosmk.  */
135   if (regno >= ALPHA_FP0_REGNUM && regno < ALPHA_FP0_REGNUM + 31)
136     return builtin_type (gdbarch)->builtin_double;
137
138   return builtin_type (gdbarch)->builtin_int64;
139 }
140
141 /* Is REGNUM a member of REGGROUP?  */
142
143 static int
144 alpha_register_reggroup_p (struct gdbarch *gdbarch, int regnum,
145                            struct reggroup *group)
146 {
147   /* Filter out any registers eliminated, but whose regnum is 
148      reserved for backward compatibility, e.g. the vfp.  */
149   if (gdbarch_register_name (gdbarch, regnum) == NULL
150       || *gdbarch_register_name (gdbarch, regnum) == '\0')
151     return 0;
152
153   if (group == all_reggroup)
154     return 1;
155
156   /* Zero should not be saved or restored.  Technically it is a general
157      register (just as $f31 would be a float if we represented it), but
158      there's no point displaying it during "info regs", so leave it out
159      of all groups except for "all".  */
160   if (regnum == ALPHA_ZERO_REGNUM)
161     return 0;
162
163   /* All other registers are saved and restored.  */
164   if (group == save_reggroup || group == restore_reggroup)
165     return 1;
166
167   /* All other groups are non-overlapping.  */
168
169   /* Since this is really a PALcode memory slot...  */
170   if (regnum == ALPHA_UNIQUE_REGNUM)
171     return group == system_reggroup;
172
173   /* Force the FPCR to be considered part of the floating point state.  */
174   if (regnum == ALPHA_FPCR_REGNUM)
175     return group == float_reggroup;
176
177   if (regnum >= ALPHA_FP0_REGNUM && regnum < ALPHA_FP0_REGNUM + 31)
178     return group == float_reggroup;
179   else
180     return group == general_reggroup;
181 }
182
183 /* The following represents exactly the conversion performed by
184    the LDS instruction.  This applies to both single-precision
185    floating point and 32-bit integers.  */
186
187 static void
188 alpha_lds (struct gdbarch *gdbarch, void *out, const void *in)
189 {
190   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
191   ULONGEST mem     = extract_unsigned_integer (in, 4, byte_order);
192   ULONGEST frac    = (mem >>  0) & 0x7fffff;
193   ULONGEST sign    = (mem >> 31) & 1;
194   ULONGEST exp_msb = (mem >> 30) & 1;
195   ULONGEST exp_low = (mem >> 23) & 0x7f;
196   ULONGEST exp, reg;
197
198   exp = (exp_msb << 10) | exp_low;
199   if (exp_msb)
200     {
201       if (exp_low == 0x7f)
202         exp = 0x7ff;
203     }
204   else
205     {
206       if (exp_low != 0x00)
207         exp |= 0x380;
208     }
209
210   reg = (sign << 63) | (exp << 52) | (frac << 29);
211   store_unsigned_integer (out, 8, byte_order, reg);
212 }
213
214 /* Similarly, this represents exactly the conversion performed by
215    the STS instruction.  */
216
217 static void
218 alpha_sts (struct gdbarch *gdbarch, void *out, const void *in)
219 {
220   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
221   ULONGEST reg, mem;
222
223   reg = extract_unsigned_integer (in, 8, byte_order);
224   mem = ((reg >> 32) & 0xc0000000) | ((reg >> 29) & 0x3fffffff);
225   store_unsigned_integer (out, 4, byte_order, mem);
226 }
227
228 /* The alpha needs a conversion between register and memory format if the
229    register is a floating point register and memory format is float, as the
230    register format must be double or memory format is an integer with 4
231    bytes or less, as the representation of integers in floating point
232    registers is different. */
233
234 static int
235 alpha_convert_register_p (struct gdbarch *gdbarch, int regno, struct type *type)
236 {
237   return (regno >= ALPHA_FP0_REGNUM && regno < ALPHA_FP0_REGNUM + 31
238           && TYPE_LENGTH (type) != 8);
239 }
240
241 static void
242 alpha_register_to_value (struct frame_info *frame, int regnum,
243                          struct type *valtype, gdb_byte *out)
244 {
245   gdb_byte in[MAX_REGISTER_SIZE];
246
247   frame_register_read (frame, regnum, in);
248   switch (TYPE_LENGTH (valtype))
249     {
250     case 4:
251       alpha_sts (get_frame_arch (frame), out, in);
252       break;
253     default:
254       error (_("Cannot retrieve value from floating point register"));
255     }
256 }
257
258 static void
259 alpha_value_to_register (struct frame_info *frame, int regnum,
260                          struct type *valtype, const gdb_byte *in)
261 {
262   gdb_byte out[MAX_REGISTER_SIZE];
263
264   switch (TYPE_LENGTH (valtype))
265     {
266     case 4:
267       alpha_lds (get_frame_arch (frame), out, in);
268       break;
269     default:
270       error (_("Cannot store value in floating point register"));
271     }
272   put_frame_register (frame, regnum, out);
273 }
274
275 \f
276 /* The alpha passes the first six arguments in the registers, the rest on
277    the stack.  The register arguments are stored in ARG_REG_BUFFER, and
278    then moved into the register file; this simplifies the passing of a
279    large struct which extends from the registers to the stack, plus avoids
280    three ptrace invocations per word.
281
282    We don't bother tracking which register values should go in integer
283    regs or fp regs; we load the same values into both.
284
285    If the called function is returning a structure, the address of the
286    structure to be returned is passed as a hidden first argument.  */
287
288 static CORE_ADDR
289 alpha_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
290                        struct regcache *regcache, CORE_ADDR bp_addr,
291                        int nargs, struct value **args, CORE_ADDR sp,
292                        int struct_return, CORE_ADDR struct_addr)
293 {
294   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
295   int i;
296   int accumulate_size = struct_return ? 8 : 0;
297   struct alpha_arg
298     {
299       const gdb_byte *contents;
300       int len;
301       int offset;
302     };
303   struct alpha_arg *alpha_args
304     = (struct alpha_arg *) alloca (nargs * sizeof (struct alpha_arg));
305   struct alpha_arg *m_arg;
306   gdb_byte arg_reg_buffer[ALPHA_REGISTER_SIZE * ALPHA_NUM_ARG_REGS];
307   int required_arg_regs;
308   CORE_ADDR func_addr = find_function_addr (function, NULL);
309
310   /* The ABI places the address of the called function in T12.  */
311   regcache_cooked_write_signed (regcache, ALPHA_T12_REGNUM, func_addr);
312
313   /* Set the return address register to point to the entry point
314      of the program, where a breakpoint lies in wait.  */
315   regcache_cooked_write_signed (regcache, ALPHA_RA_REGNUM, bp_addr);
316
317   /* Lay out the arguments in memory.  */
318   for (i = 0, m_arg = alpha_args; i < nargs; i++, m_arg++)
319     {
320       struct value *arg = args[i];
321       struct type *arg_type = check_typedef (value_type (arg));
322
323       /* Cast argument to long if necessary as the compiler does it too.  */
324       switch (TYPE_CODE (arg_type))
325         {
326         case TYPE_CODE_INT:
327         case TYPE_CODE_BOOL:
328         case TYPE_CODE_CHAR:
329         case TYPE_CODE_RANGE:
330         case TYPE_CODE_ENUM:
331           if (TYPE_LENGTH (arg_type) == 4)
332             {
333               /* 32-bit values must be sign-extended to 64 bits
334                  even if the base data type is unsigned.  */
335               arg_type = builtin_type (gdbarch)->builtin_int32;
336               arg = value_cast (arg_type, arg);
337             }
338           if (TYPE_LENGTH (arg_type) < ALPHA_REGISTER_SIZE)
339             {
340               arg_type = builtin_type (gdbarch)->builtin_int64;
341               arg = value_cast (arg_type, arg);
342             }
343           break;
344
345         case TYPE_CODE_FLT:
346           /* "float" arguments loaded in registers must be passed in
347              register format, aka "double".  */
348           if (accumulate_size < sizeof (arg_reg_buffer)
349               && TYPE_LENGTH (arg_type) == 4)
350             {
351               arg_type = builtin_type (gdbarch)->builtin_double;
352               arg = value_cast (arg_type, arg);
353             }
354           /* Tru64 5.1 has a 128-bit long double, and passes this by
355              invisible reference.  No one else uses this data type.  */
356           else if (TYPE_LENGTH (arg_type) == 16)
357             {
358               /* Allocate aligned storage.  */
359               sp = (sp & -16) - 16;
360
361               /* Write the real data into the stack.  */
362               write_memory (sp, value_contents (arg), 16);
363
364               /* Construct the indirection.  */
365               arg_type = lookup_pointer_type (arg_type);
366               arg = value_from_pointer (arg_type, sp);
367             }
368           break;
369
370         case TYPE_CODE_COMPLEX:
371           /* ??? The ABI says that complex values are passed as two
372              separate scalar values.  This distinction only matters
373              for complex float.  However, GCC does not implement this.  */
374
375           /* Tru64 5.1 has a 128-bit long double, and passes this by
376              invisible reference.  */
377           if (TYPE_LENGTH (arg_type) == 32)
378             {
379               /* Allocate aligned storage.  */
380               sp = (sp & -16) - 16;
381
382               /* Write the real data into the stack.  */
383               write_memory (sp, value_contents (arg), 32);
384
385               /* Construct the indirection.  */
386               arg_type = lookup_pointer_type (arg_type);
387               arg = value_from_pointer (arg_type, sp);
388             }
389           break;
390
391         default:
392           break;
393         }
394       m_arg->len = TYPE_LENGTH (arg_type);
395       m_arg->offset = accumulate_size;
396       accumulate_size = (accumulate_size + m_arg->len + 7) & ~7;
397       m_arg->contents = value_contents (arg);
398     }
399
400   /* Determine required argument register loads, loading an argument register
401      is expensive as it uses three ptrace calls.  */
402   required_arg_regs = accumulate_size / 8;
403   if (required_arg_regs > ALPHA_NUM_ARG_REGS)
404     required_arg_regs = ALPHA_NUM_ARG_REGS;
405
406   /* Make room for the arguments on the stack.  */
407   if (accumulate_size < sizeof(arg_reg_buffer))
408     accumulate_size = 0;
409   else
410     accumulate_size -= sizeof(arg_reg_buffer);
411   sp -= accumulate_size;
412
413   /* Keep sp aligned to a multiple of 16 as the ABI requires.  */
414   sp &= ~15;
415
416   /* `Push' arguments on the stack.  */
417   for (i = nargs; m_arg--, --i >= 0;)
418     {
419       const gdb_byte *contents = m_arg->contents;
420       int offset = m_arg->offset;
421       int len = m_arg->len;
422
423       /* Copy the bytes destined for registers into arg_reg_buffer.  */
424       if (offset < sizeof(arg_reg_buffer))
425         {
426           if (offset + len <= sizeof(arg_reg_buffer))
427             {
428               memcpy (arg_reg_buffer + offset, contents, len);
429               continue;
430             }
431           else
432             {
433               int tlen = sizeof(arg_reg_buffer) - offset;
434               memcpy (arg_reg_buffer + offset, contents, tlen);
435               offset += tlen;
436               contents += tlen;
437               len -= tlen;
438             }
439         }
440
441       /* Everything else goes to the stack.  */
442       write_memory (sp + offset - sizeof(arg_reg_buffer), contents, len);
443     }
444   if (struct_return)
445     store_unsigned_integer (arg_reg_buffer, ALPHA_REGISTER_SIZE,
446                             byte_order, struct_addr);
447
448   /* Load the argument registers.  */
449   for (i = 0; i < required_arg_regs; i++)
450     {
451       regcache_cooked_write (regcache, ALPHA_A0_REGNUM + i,
452                              arg_reg_buffer + i*ALPHA_REGISTER_SIZE);
453       regcache_cooked_write (regcache, ALPHA_FPA0_REGNUM + i,
454                              arg_reg_buffer + i*ALPHA_REGISTER_SIZE);
455     }
456
457   /* Finally, update the stack pointer.  */
458   regcache_cooked_write_signed (regcache, ALPHA_SP_REGNUM, sp);
459
460   return sp;
461 }
462
463 /* Extract from REGCACHE the value about to be returned from a function
464    and copy it into VALBUF.  */
465
466 static void
467 alpha_extract_return_value (struct type *valtype, struct regcache *regcache,
468                             gdb_byte *valbuf)
469 {
470   struct gdbarch *gdbarch = get_regcache_arch (regcache);
471   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
472   int length = TYPE_LENGTH (valtype);
473   gdb_byte raw_buffer[ALPHA_REGISTER_SIZE];
474   ULONGEST l;
475
476   switch (TYPE_CODE (valtype))
477     {
478     case TYPE_CODE_FLT:
479       switch (length)
480         {
481         case 4:
482           regcache_cooked_read (regcache, ALPHA_FP0_REGNUM, raw_buffer);
483           alpha_sts (gdbarch, valbuf, raw_buffer);
484           break;
485
486         case 8:
487           regcache_cooked_read (regcache, ALPHA_FP0_REGNUM, valbuf);
488           break;
489
490         case 16:
491           regcache_cooked_read_unsigned (regcache, ALPHA_V0_REGNUM, &l);
492           read_memory (l, valbuf, 16);
493           break;
494
495         default:
496           internal_error (__FILE__, __LINE__, _("unknown floating point width"));
497         }
498       break;
499
500     case TYPE_CODE_COMPLEX:
501       switch (length)
502         {
503         case 8:
504           /* ??? This isn't correct wrt the ABI, but it's what GCC does.  */
505           regcache_cooked_read (regcache, ALPHA_FP0_REGNUM, valbuf);
506           break;
507
508         case 16:
509           regcache_cooked_read (regcache, ALPHA_FP0_REGNUM, valbuf);
510           regcache_cooked_read (regcache, ALPHA_FP0_REGNUM + 1, valbuf + 8);
511           break;
512
513         case 32:
514           regcache_cooked_read_signed (regcache, ALPHA_V0_REGNUM, &l);
515           read_memory (l, valbuf, 32);
516           break;
517
518         default:
519           internal_error (__FILE__, __LINE__, _("unknown floating point width"));
520         }
521       break;
522
523     default:
524       /* Assume everything else degenerates to an integer.  */
525       regcache_cooked_read_unsigned (regcache, ALPHA_V0_REGNUM, &l);
526       store_unsigned_integer (valbuf, length, byte_order, l);
527       break;
528     }
529 }
530
531 /* Insert the given value into REGCACHE as if it was being 
532    returned by a function.  */
533
534 static void
535 alpha_store_return_value (struct type *valtype, struct regcache *regcache,
536                           const gdb_byte *valbuf)
537 {
538   struct gdbarch *gdbarch = get_regcache_arch (regcache);
539   int length = TYPE_LENGTH (valtype);
540   gdb_byte raw_buffer[ALPHA_REGISTER_SIZE];
541   ULONGEST l;
542
543   switch (TYPE_CODE (valtype))
544     {
545     case TYPE_CODE_FLT:
546       switch (length)
547         {
548         case 4:
549           alpha_lds (gdbarch, raw_buffer, valbuf);
550           regcache_cooked_write (regcache, ALPHA_FP0_REGNUM, raw_buffer);
551           break;
552
553         case 8:
554           regcache_cooked_write (regcache, ALPHA_FP0_REGNUM, valbuf);
555           break;
556
557         case 16:
558           /* FIXME: 128-bit long doubles are returned like structures:
559              by writing into indirect storage provided by the caller
560              as the first argument.  */
561           error (_("Cannot set a 128-bit long double return value."));
562
563         default:
564           internal_error (__FILE__, __LINE__, _("unknown floating point width"));
565         }
566       break;
567
568     case TYPE_CODE_COMPLEX:
569       switch (length)
570         {
571         case 8:
572           /* ??? This isn't correct wrt the ABI, but it's what GCC does.  */
573           regcache_cooked_write (regcache, ALPHA_FP0_REGNUM, valbuf);
574           break;
575
576         case 16:
577           regcache_cooked_write (regcache, ALPHA_FP0_REGNUM, valbuf);
578           regcache_cooked_write (regcache, ALPHA_FP0_REGNUM + 1, valbuf + 8);
579           break;
580
581         case 32:
582           /* FIXME: 128-bit long doubles are returned like structures:
583              by writing into indirect storage provided by the caller
584              as the first argument.  */
585           error (_("Cannot set a 128-bit long double return value."));
586
587         default:
588           internal_error (__FILE__, __LINE__, _("unknown floating point width"));
589         }
590       break;
591
592     default:
593       /* Assume everything else degenerates to an integer.  */
594       /* 32-bit values must be sign-extended to 64 bits
595          even if the base data type is unsigned.  */
596       if (length == 4)
597         valtype = builtin_type (gdbarch)->builtin_int32;
598       l = unpack_long (valtype, valbuf);
599       regcache_cooked_write_unsigned (regcache, ALPHA_V0_REGNUM, l);
600       break;
601     }
602 }
603
604 static enum return_value_convention
605 alpha_return_value (struct gdbarch *gdbarch, struct type *func_type,
606                     struct type *type, struct regcache *regcache,
607                     gdb_byte *readbuf, const gdb_byte *writebuf)
608 {
609   enum type_code code = TYPE_CODE (type);
610
611   if ((code == TYPE_CODE_STRUCT
612        || code == TYPE_CODE_UNION
613        || code == TYPE_CODE_ARRAY)
614       && gdbarch_tdep (gdbarch)->return_in_memory (type))
615     {
616       if (readbuf)
617         {
618           ULONGEST addr;
619           regcache_raw_read_unsigned (regcache, ALPHA_V0_REGNUM, &addr);
620           read_memory (addr, readbuf, TYPE_LENGTH (type));
621         }
622
623       return RETURN_VALUE_ABI_RETURNS_ADDRESS;
624     }
625
626   if (readbuf)
627     alpha_extract_return_value (type, regcache, readbuf);
628   if (writebuf)
629     alpha_store_return_value (type, regcache, writebuf);
630
631   return RETURN_VALUE_REGISTER_CONVENTION;
632 }
633
634 static int
635 alpha_return_in_memory_always (struct type *type)
636 {
637   return 1;
638 }
639 \f
640 static const gdb_byte *
641 alpha_breakpoint_from_pc (struct gdbarch *gdbarch, CORE_ADDR *pc, int *len)
642 {
643   static const gdb_byte break_insn[] = { 0x80, 0, 0, 0 }; /* call_pal bpt */
644
645   *len = sizeof(break_insn);
646   return break_insn;
647 }
648
649 \f
650 /* This returns the PC of the first insn after the prologue.
651    If we can't find the prologue, then return 0.  */
652
653 CORE_ADDR
654 alpha_after_prologue (CORE_ADDR pc)
655 {
656   struct symtab_and_line sal;
657   CORE_ADDR func_addr, func_end;
658
659   if (!find_pc_partial_function (pc, NULL, &func_addr, &func_end))
660     return 0;
661
662   sal = find_pc_line (func_addr, 0);
663   if (sal.end < func_end)
664     return sal.end;
665
666   /* The line after the prologue is after the end of the function.  In this
667      case, tell the caller to find the prologue the hard way.  */
668   return 0;
669 }
670
671 /* Read an instruction from memory at PC, looking through breakpoints.  */
672
673 unsigned int
674 alpha_read_insn (struct gdbarch *gdbarch, CORE_ADDR pc)
675 {
676   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
677   gdb_byte buf[ALPHA_INSN_SIZE];
678   int status;
679
680   status = target_read_memory (pc, buf, sizeof (buf));
681   if (status)
682     memory_error (status, pc);
683   return extract_unsigned_integer (buf, sizeof (buf), byte_order);
684 }
685
686 /* To skip prologues, I use this predicate.  Returns either PC itself
687    if the code at PC does not look like a function prologue; otherwise
688    returns an address that (if we're lucky) follows the prologue.  If
689    LENIENT, then we must skip everything which is involved in setting
690    up the frame (it's OK to skip more, just so long as we don't skip
691    anything which might clobber the registers which are being saved.  */
692
693 static CORE_ADDR
694 alpha_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
695 {
696   unsigned long inst;
697   int offset;
698   CORE_ADDR post_prologue_pc;
699   gdb_byte buf[ALPHA_INSN_SIZE];
700
701   /* Silently return the unaltered pc upon memory errors.
702      This could happen on OSF/1 if decode_line_1 tries to skip the
703      prologue for quickstarted shared library functions when the
704      shared library is not yet mapped in.
705      Reading target memory is slow over serial lines, so we perform
706      this check only if the target has shared libraries (which all
707      Alpha targets do).  */
708   if (target_read_memory (pc, buf, sizeof (buf)))
709     return pc;
710
711   /* See if we can determine the end of the prologue via the symbol table.
712      If so, then return either PC, or the PC after the prologue, whichever
713      is greater.  */
714
715   post_prologue_pc = alpha_after_prologue (pc);
716   if (post_prologue_pc != 0)
717     return max (pc, post_prologue_pc);
718
719   /* Can't determine prologue from the symbol table, need to examine
720      instructions.  */
721
722   /* Skip the typical prologue instructions. These are the stack adjustment
723      instruction and the instructions that save registers on the stack
724      or in the gcc frame.  */
725   for (offset = 0; offset < 100; offset += ALPHA_INSN_SIZE)
726     {
727       inst = alpha_read_insn (gdbarch, pc + offset);
728
729       if ((inst & 0xffff0000) == 0x27bb0000)    /* ldah $gp,n($t12) */
730         continue;
731       if ((inst & 0xffff0000) == 0x23bd0000)    /* lda $gp,n($gp) */
732         continue;
733       if ((inst & 0xffff0000) == 0x23de0000)    /* lda $sp,n($sp) */
734         continue;
735       if ((inst & 0xffe01fff) == 0x43c0153e)    /* subq $sp,n,$sp */
736         continue;
737
738       if (((inst & 0xfc1f0000) == 0xb41e0000            /* stq reg,n($sp) */
739            || (inst & 0xfc1f0000) == 0x9c1e0000)        /* stt reg,n($sp) */
740           && (inst & 0x03e00000) != 0x03e00000)         /* reg != $zero */
741         continue;
742
743       if (inst == 0x47de040f)                   /* bis sp,sp,fp */
744         continue;
745       if (inst == 0x47fe040f)                   /* bis zero,sp,fp */
746         continue;
747
748       break;
749     }
750   return pc + offset;
751 }
752
753 \f
754 /* Figure out where the longjmp will land.
755    We expect the first arg to be a pointer to the jmp_buf structure from
756    which we extract the PC (JB_PC) that we will land at.  The PC is copied
757    into the "pc".  This routine returns true on success.  */
758
759 static int
760 alpha_get_longjmp_target (struct frame_info *frame, CORE_ADDR *pc)
761 {
762   struct gdbarch *gdbarch = get_frame_arch (frame);
763   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
764   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
765   CORE_ADDR jb_addr;
766   gdb_byte raw_buffer[ALPHA_REGISTER_SIZE];
767
768   jb_addr = get_frame_register_unsigned (frame, ALPHA_A0_REGNUM);
769
770   if (target_read_memory (jb_addr + (tdep->jb_pc * tdep->jb_elt_size),
771                           raw_buffer, tdep->jb_elt_size))
772     return 0;
773
774   *pc = extract_unsigned_integer (raw_buffer, tdep->jb_elt_size, byte_order);
775   return 1;
776 }
777
778 \f
779 /* Frame unwinder for signal trampolines.  We use alpha tdep bits that
780    describe the location and shape of the sigcontext structure.  After
781    that, all registers are in memory, so it's easy.  */
782 /* ??? Shouldn't we be able to do this generically, rather than with
783    OSABI data specific to Alpha?  */
784
785 struct alpha_sigtramp_unwind_cache
786 {
787   CORE_ADDR sigcontext_addr;
788 };
789
790 static struct alpha_sigtramp_unwind_cache *
791 alpha_sigtramp_frame_unwind_cache (struct frame_info *this_frame,
792                                    void **this_prologue_cache)
793 {
794   struct alpha_sigtramp_unwind_cache *info;
795   struct gdbarch_tdep *tdep;
796
797   if (*this_prologue_cache)
798     return *this_prologue_cache;
799
800   info = FRAME_OBSTACK_ZALLOC (struct alpha_sigtramp_unwind_cache);
801   *this_prologue_cache = info;
802
803   tdep = gdbarch_tdep (get_frame_arch (this_frame));
804   info->sigcontext_addr = tdep->sigcontext_addr (this_frame);
805
806   return info;
807 }
808
809 /* Return the address of REGNUM in a sigtramp frame.  Since this is
810    all arithmetic, it doesn't seem worthwhile to cache it.  */
811
812 static CORE_ADDR
813 alpha_sigtramp_register_address (struct gdbarch *gdbarch,
814                                  CORE_ADDR sigcontext_addr, int regnum)
815
816   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
817
818   if (regnum >= 0 && regnum < 32)
819     return sigcontext_addr + tdep->sc_regs_offset + regnum * 8;
820   else if (regnum >= ALPHA_FP0_REGNUM && regnum < ALPHA_FP0_REGNUM + 32)
821     return sigcontext_addr + tdep->sc_fpregs_offset + regnum * 8;
822   else if (regnum == ALPHA_PC_REGNUM)
823     return sigcontext_addr + tdep->sc_pc_offset; 
824
825   return 0;
826 }
827
828 /* Given a GDB frame, determine the address of the calling function's
829    frame.  This will be used to create a new GDB frame struct.  */
830
831 static void
832 alpha_sigtramp_frame_this_id (struct frame_info *this_frame,
833                               void **this_prologue_cache,
834                               struct frame_id *this_id)
835 {
836   struct gdbarch *gdbarch = get_frame_arch (this_frame);
837   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
838   struct alpha_sigtramp_unwind_cache *info
839     = alpha_sigtramp_frame_unwind_cache (this_frame, this_prologue_cache);
840   CORE_ADDR stack_addr, code_addr;
841
842   /* If the OSABI couldn't locate the sigcontext, give up.  */
843   if (info->sigcontext_addr == 0)
844     return;
845
846   /* If we have dynamic signal trampolines, find their start.
847      If we do not, then we must assume there is a symbol record
848      that can provide the start address.  */
849   if (tdep->dynamic_sigtramp_offset)
850     {
851       int offset;
852       code_addr = get_frame_pc (this_frame);
853       offset = tdep->dynamic_sigtramp_offset (gdbarch, code_addr);
854       if (offset >= 0)
855         code_addr -= offset;
856       else
857         code_addr = 0;
858     }
859   else
860     code_addr = get_frame_func (this_frame);
861
862   /* The stack address is trivially read from the sigcontext.  */
863   stack_addr = alpha_sigtramp_register_address (gdbarch, info->sigcontext_addr,
864                                                 ALPHA_SP_REGNUM);
865   stack_addr = get_frame_memory_unsigned (this_frame, stack_addr,
866                                           ALPHA_REGISTER_SIZE);
867
868   *this_id = frame_id_build (stack_addr, code_addr);
869 }
870
871 /* Retrieve the value of REGNUM in FRAME.  Don't give up!  */
872
873 static struct value *
874 alpha_sigtramp_frame_prev_register (struct frame_info *this_frame,
875                                     void **this_prologue_cache, int regnum)
876 {
877   struct alpha_sigtramp_unwind_cache *info
878     = alpha_sigtramp_frame_unwind_cache (this_frame, this_prologue_cache);
879   CORE_ADDR addr;
880
881   if (info->sigcontext_addr != 0)
882     {
883       /* All integer and fp registers are stored in memory.  */
884       addr = alpha_sigtramp_register_address (get_frame_arch (this_frame),
885                                               info->sigcontext_addr, regnum);
886       if (addr != 0)
887         return frame_unwind_got_memory (this_frame, regnum, addr);
888     }
889
890   /* This extra register may actually be in the sigcontext, but our
891      current description of it in alpha_sigtramp_frame_unwind_cache
892      doesn't include it.  Too bad.  Fall back on whatever's in the
893      outer frame.  */
894   return frame_unwind_got_register (this_frame, regnum, regnum);
895 }
896
897 static int
898 alpha_sigtramp_frame_sniffer (const struct frame_unwind *self,
899                               struct frame_info *this_frame,
900                               void **this_prologue_cache)
901 {
902   struct gdbarch *gdbarch = get_frame_arch (this_frame);
903   CORE_ADDR pc = get_frame_pc (this_frame);
904   char *name;
905
906   /* NOTE: cagney/2004-04-30: Do not copy/clone this code.  Instead
907      look at tramp-frame.h and other simplier per-architecture
908      sigtramp unwinders.  */
909
910   /* We shouldn't even bother to try if the OSABI didn't register a
911      sigcontext_addr handler or pc_in_sigtramp hander.  */
912   if (gdbarch_tdep (gdbarch)->sigcontext_addr == NULL)
913     return 0;
914   if (gdbarch_tdep (gdbarch)->pc_in_sigtramp == NULL)
915     return 0;
916
917   /* Otherwise we should be in a signal frame.  */
918   find_pc_partial_function (pc, &name, NULL, NULL);
919   if (gdbarch_tdep (gdbarch)->pc_in_sigtramp (gdbarch, pc, name))
920     return 1;
921
922   return 0;
923 }
924
925 static const struct frame_unwind alpha_sigtramp_frame_unwind = {
926   SIGTRAMP_FRAME,
927   alpha_sigtramp_frame_this_id,
928   alpha_sigtramp_frame_prev_register,
929   NULL,
930   alpha_sigtramp_frame_sniffer
931 };
932
933 \f
934
935 /* Heuristic_proc_start may hunt through the text section for a long
936    time across a 2400 baud serial line.  Allows the user to limit this
937    search.  */
938 static unsigned int heuristic_fence_post = 0;
939
940 /* Attempt to locate the start of the function containing PC.  We assume that
941    the previous function ends with an about_to_return insn.  Not foolproof by
942    any means, since gcc is happy to put the epilogue in the middle of a
943    function.  But we're guessing anyway...  */
944
945 static CORE_ADDR
946 alpha_heuristic_proc_start (struct gdbarch *gdbarch, CORE_ADDR pc)
947 {
948   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
949   CORE_ADDR last_non_nop = pc;
950   CORE_ADDR fence = pc - heuristic_fence_post;
951   CORE_ADDR orig_pc = pc;
952   CORE_ADDR func;
953   struct inferior *inf;
954
955   if (pc == 0)
956     return 0;
957
958   /* First see if we can find the start of the function from minimal
959      symbol information.  This can succeed with a binary that doesn't
960      have debug info, but hasn't been stripped.  */
961   func = get_pc_function_start (pc);
962   if (func)
963     return func;
964
965   if (heuristic_fence_post == UINT_MAX
966       || fence < tdep->vm_min_address)
967     fence = tdep->vm_min_address;
968
969   /* Search back for previous return; also stop at a 0, which might be
970      seen for instance before the start of a code section.  Don't include
971      nops, since this usually indicates padding between functions.  */
972   for (pc -= ALPHA_INSN_SIZE; pc >= fence; pc -= ALPHA_INSN_SIZE)
973     {
974       unsigned int insn = alpha_read_insn (gdbarch, pc);
975       switch (insn)
976         {
977         case 0:                 /* invalid insn */
978         case 0x6bfa8001:        /* ret $31,($26),1 */
979           return last_non_nop;
980
981         case 0x2ffe0000:        /* unop: ldq_u $31,0($30) */
982         case 0x47ff041f:        /* nop: bis $31,$31,$31 */
983           break;
984
985         default:
986           last_non_nop = pc;
987           break;
988         }
989     }
990
991   inf = current_inferior ();
992
993   /* It's not clear to me why we reach this point when stopping quietly,
994      but with this test, at least we don't print out warnings for every
995      child forked (eg, on decstation).  22apr93 rich@cygnus.com.  */
996   if (inf->control.stop_soon == NO_STOP_QUIETLY)
997     {
998       static int blurb_printed = 0;
999
1000       if (fence == tdep->vm_min_address)
1001         warning (_("Hit beginning of text section without finding \
1002 enclosing function for address %s"), paddress (gdbarch, orig_pc));
1003       else
1004         warning (_("Hit heuristic-fence-post without finding \
1005 enclosing function for address %s"), paddress (gdbarch, orig_pc));
1006
1007       if (!blurb_printed)
1008         {
1009           printf_filtered (_("\
1010 This warning occurs if you are debugging a function without any symbols\n\
1011 (for example, in a stripped executable).  In that case, you may wish to\n\
1012 increase the size of the search with the `set heuristic-fence-post' command.\n\
1013 \n\
1014 Otherwise, you told GDB there was a function where there isn't one, or\n\
1015 (more likely) you have encountered a bug in GDB.\n"));
1016           blurb_printed = 1;
1017         }
1018     }
1019
1020   return 0;
1021 }
1022
1023 /* Fallback alpha frame unwinder.  Uses instruction scanning and knows
1024    something about the traditional layout of alpha stack frames.  */
1025
1026 struct alpha_heuristic_unwind_cache
1027
1028   CORE_ADDR vfp;
1029   CORE_ADDR start_pc;
1030   struct trad_frame_saved_reg *saved_regs;
1031   int return_reg;
1032 };
1033
1034 /* If a probing loop sequence starts at PC, simulate it and compute
1035    FRAME_SIZE and PC after its execution.  Otherwise, return with PC and
1036    FRAME_SIZE unchanged.  */
1037
1038 static void
1039 alpha_heuristic_analyze_probing_loop (struct gdbarch *gdbarch, CORE_ADDR *pc,
1040                                       int *frame_size)
1041 {
1042   CORE_ADDR cur_pc = *pc;
1043   int cur_frame_size = *frame_size;
1044   int nb_of_iterations, reg_index, reg_probe;
1045   unsigned int insn;
1046
1047   /* The following pattern is recognized as a probing loop:
1048
1049         lda     REG_INDEX,NB_OF_ITERATIONS
1050         lda     REG_PROBE,<immediate>(sp)
1051
1052      LOOP_START:
1053         stq     zero,<immediate>(REG_PROBE)
1054         subq    REG_INDEX,0x1,REG_INDEX
1055         lda     REG_PROBE,<immediate>(REG_PROBE)
1056         bne     REG_INDEX, LOOP_START
1057  
1058         lda     sp,<immediate>(REG_PROBE)
1059
1060      If anything different is found, the function returns without
1061      changing PC and FRAME_SIZE.  Otherwise, PC will point immediately
1062      after this sequence, and FRAME_SIZE will be updated.
1063   */
1064
1065   /* lda     REG_INDEX,NB_OF_ITERATIONS */
1066
1067   insn = alpha_read_insn (gdbarch, cur_pc);
1068   if (INSN_OPCODE (insn) != lda_opcode)
1069     return;
1070   reg_index = MEM_RA (insn);
1071   nb_of_iterations = MEM_DISP (insn);
1072
1073   /* lda     REG_PROBE,<immediate>(sp) */
1074
1075   cur_pc += ALPHA_INSN_SIZE;
1076   insn = alpha_read_insn (gdbarch, cur_pc);
1077   if (INSN_OPCODE (insn) != lda_opcode
1078       || MEM_RB (insn) != ALPHA_SP_REGNUM)
1079     return;
1080   reg_probe = MEM_RA (insn);
1081   cur_frame_size -= MEM_DISP (insn);
1082
1083   /* stq     zero,<immediate>(REG_PROBE) */
1084   
1085   cur_pc += ALPHA_INSN_SIZE;
1086   insn = alpha_read_insn (gdbarch, cur_pc);
1087   if (INSN_OPCODE (insn) != stq_opcode
1088       || MEM_RA (insn) != 0x1f
1089       || MEM_RB (insn) != reg_probe)
1090     return;
1091   
1092   /* subq    REG_INDEX,0x1,REG_INDEX */
1093
1094   cur_pc += ALPHA_INSN_SIZE;
1095   insn = alpha_read_insn (gdbarch, cur_pc);
1096   if (INSN_OPCODE (insn) != subq_opcode
1097       || !OPR_HAS_IMMEDIATE (insn)
1098       || OPR_FUNCTION (insn) != subq_function
1099       || OPR_LIT(insn) != 1
1100       || OPR_RA (insn) != reg_index
1101       || OPR_RC (insn) != reg_index)
1102     return;
1103   
1104   /* lda     REG_PROBE,<immediate>(REG_PROBE) */
1105   
1106   cur_pc += ALPHA_INSN_SIZE;
1107   insn = alpha_read_insn (gdbarch, cur_pc);
1108   if (INSN_OPCODE (insn) != lda_opcode
1109       || MEM_RA (insn) != reg_probe
1110       || MEM_RB (insn) != reg_probe)
1111     return;
1112   cur_frame_size -= MEM_DISP (insn) * nb_of_iterations;
1113
1114   /* bne     REG_INDEX, LOOP_START */
1115
1116   cur_pc += ALPHA_INSN_SIZE;
1117   insn = alpha_read_insn (gdbarch, cur_pc);
1118   if (INSN_OPCODE (insn) != bne_opcode
1119       || MEM_RA (insn) != reg_index)
1120     return;
1121
1122   /* lda     sp,<immediate>(REG_PROBE) */
1123
1124   cur_pc += ALPHA_INSN_SIZE;
1125   insn = alpha_read_insn (gdbarch, cur_pc);
1126   if (INSN_OPCODE (insn) != lda_opcode
1127       || MEM_RA (insn) != ALPHA_SP_REGNUM
1128       || MEM_RB (insn) != reg_probe)
1129     return;
1130   cur_frame_size -= MEM_DISP (insn);
1131
1132   *pc = cur_pc;
1133   *frame_size = cur_frame_size;
1134 }
1135
1136 static struct alpha_heuristic_unwind_cache *
1137 alpha_heuristic_frame_unwind_cache (struct frame_info *this_frame,
1138                                     void **this_prologue_cache,
1139                                     CORE_ADDR start_pc)
1140 {
1141   struct gdbarch *gdbarch = get_frame_arch (this_frame);
1142   struct alpha_heuristic_unwind_cache *info;
1143   ULONGEST val;
1144   CORE_ADDR limit_pc, cur_pc;
1145   int frame_reg, frame_size, return_reg, reg;
1146
1147   if (*this_prologue_cache)
1148     return *this_prologue_cache;
1149
1150   info = FRAME_OBSTACK_ZALLOC (struct alpha_heuristic_unwind_cache);
1151   *this_prologue_cache = info;
1152   info->saved_regs = trad_frame_alloc_saved_regs (this_frame);
1153
1154   limit_pc = get_frame_pc (this_frame);
1155   if (start_pc == 0)
1156     start_pc = alpha_heuristic_proc_start (gdbarch, limit_pc);
1157   info->start_pc = start_pc;
1158
1159   frame_reg = ALPHA_SP_REGNUM;
1160   frame_size = 0;
1161   return_reg = -1;
1162
1163   /* If we've identified a likely place to start, do code scanning.  */
1164   if (start_pc != 0)
1165     {
1166       /* Limit the forward search to 50 instructions.  */
1167       if (start_pc + 200 < limit_pc)
1168         limit_pc = start_pc + 200;
1169
1170       for (cur_pc = start_pc; cur_pc < limit_pc; cur_pc += ALPHA_INSN_SIZE)
1171         {
1172           unsigned int word = alpha_read_insn (gdbarch, cur_pc);
1173
1174           if ((word & 0xffff0000) == 0x23de0000)        /* lda $sp,n($sp) */
1175             {
1176               if (word & 0x8000)
1177                 {
1178                   /* Consider only the first stack allocation instruction
1179                      to contain the static size of the frame. */
1180                   if (frame_size == 0)
1181                     frame_size = (-word) & 0xffff;
1182                 }
1183               else
1184                 {
1185                   /* Exit loop if a positive stack adjustment is found, which
1186                      usually means that the stack cleanup code in the function
1187                      epilogue is reached.  */
1188                   break;
1189                 }
1190             }
1191           else if ((word & 0xfc1f0000) == 0xb41e0000)   /* stq reg,n($sp) */
1192             {
1193               reg = (word & 0x03e00000) >> 21;
1194
1195               /* Ignore this instruction if we have already encountered
1196                  an instruction saving the same register earlier in the
1197                  function code.  The current instruction does not tell
1198                  us where the original value upon function entry is saved.
1199                  All it says is that the function we are scanning reused
1200                  that register for some computation of its own, and is now
1201                  saving its result.  */
1202               if (trad_frame_addr_p(info->saved_regs, reg))
1203                 continue;
1204
1205               if (reg == 31)
1206                 continue;
1207
1208               /* Do not compute the address where the register was saved yet,
1209                  because we don't know yet if the offset will need to be
1210                  relative to $sp or $fp (we can not compute the address
1211                  relative to $sp if $sp is updated during the execution of
1212                  the current subroutine, for instance when doing some alloca).
1213                  So just store the offset for the moment, and compute the
1214                  address later when we know whether this frame has a frame
1215                  pointer or not.  */
1216               /* Hack: temporarily add one, so that the offset is non-zero
1217                  and we can tell which registers have save offsets below.  */
1218               info->saved_regs[reg].addr = (word & 0xffff) + 1;
1219
1220               /* Starting with OSF/1-3.2C, the system libraries are shipped
1221                  without local symbols, but they still contain procedure
1222                  descriptors without a symbol reference. GDB is currently
1223                  unable to find these procedure descriptors and uses
1224                  heuristic_proc_desc instead.
1225                  As some low level compiler support routines (__div*, __add*)
1226                  use a non-standard return address register, we have to
1227                  add some heuristics to determine the return address register,
1228                  or stepping over these routines will fail.
1229                  Usually the return address register is the first register
1230                  saved on the stack, but assembler optimization might
1231                  rearrange the register saves.
1232                  So we recognize only a few registers (t7, t9, ra) within
1233                  the procedure prologue as valid return address registers.
1234                  If we encounter a return instruction, we extract the
1235                  the return address register from it.
1236
1237                  FIXME: Rewriting GDB to access the procedure descriptors,
1238                  e.g. via the minimal symbol table, might obviate this hack.  */
1239               if (return_reg == -1
1240                   && cur_pc < (start_pc + 80)
1241                   && (reg == ALPHA_T7_REGNUM
1242                       || reg == ALPHA_T9_REGNUM
1243                       || reg == ALPHA_RA_REGNUM))
1244                 return_reg = reg;
1245             }
1246           else if ((word & 0xffe0ffff) == 0x6be08001)   /* ret zero,reg,1 */
1247             return_reg = (word >> 16) & 0x1f;
1248           else if (word == 0x47de040f)                  /* bis sp,sp,fp */
1249             frame_reg = ALPHA_GCC_FP_REGNUM;
1250           else if (word == 0x47fe040f)                  /* bis zero,sp,fp */
1251             frame_reg = ALPHA_GCC_FP_REGNUM;
1252
1253           alpha_heuristic_analyze_probing_loop (gdbarch, &cur_pc, &frame_size);
1254         }
1255
1256       /* If we haven't found a valid return address register yet, keep
1257          searching in the procedure prologue.  */
1258       if (return_reg == -1)
1259         {
1260           while (cur_pc < (limit_pc + 80) && cur_pc < (start_pc + 80))
1261             {
1262               unsigned int word = alpha_read_insn (gdbarch, cur_pc);
1263
1264               if ((word & 0xfc1f0000) == 0xb41e0000)    /* stq reg,n($sp) */
1265                 {
1266                   reg = (word & 0x03e00000) >> 21;
1267                   if (reg == ALPHA_T7_REGNUM
1268                       || reg == ALPHA_T9_REGNUM
1269                       || reg == ALPHA_RA_REGNUM)
1270                     {
1271                       return_reg = reg;
1272                       break;
1273                     }
1274                 }
1275               else if ((word & 0xffe0ffff) == 0x6be08001) /* ret zero,reg,1 */
1276                 {
1277                   return_reg = (word >> 16) & 0x1f;
1278                   break;
1279                 }
1280
1281               cur_pc += ALPHA_INSN_SIZE;
1282             }
1283         }
1284     }
1285
1286   /* Failing that, do default to the customary RA.  */
1287   if (return_reg == -1)
1288     return_reg = ALPHA_RA_REGNUM;
1289   info->return_reg = return_reg;
1290
1291   val = get_frame_register_unsigned (this_frame, frame_reg);
1292   info->vfp = val + frame_size;
1293
1294   /* Convert offsets to absolute addresses.  See above about adding
1295      one to the offsets to make all detected offsets non-zero.  */
1296   for (reg = 0; reg < ALPHA_NUM_REGS; ++reg)
1297     if (trad_frame_addr_p(info->saved_regs, reg))
1298       info->saved_regs[reg].addr += val - 1;
1299
1300   /* The stack pointer of the previous frame is computed by popping
1301      the current stack frame.  */
1302   if (!trad_frame_addr_p (info->saved_regs, ALPHA_SP_REGNUM))
1303    trad_frame_set_value (info->saved_regs, ALPHA_SP_REGNUM, info->vfp);
1304
1305   return info;
1306 }
1307
1308 /* Given a GDB frame, determine the address of the calling function's
1309    frame.  This will be used to create a new GDB frame struct.  */
1310
1311 static void
1312 alpha_heuristic_frame_this_id (struct frame_info *this_frame,
1313                                void **this_prologue_cache,
1314                                struct frame_id *this_id)
1315 {
1316   struct alpha_heuristic_unwind_cache *info
1317     = alpha_heuristic_frame_unwind_cache (this_frame, this_prologue_cache, 0);
1318
1319   *this_id = frame_id_build (info->vfp, info->start_pc);
1320 }
1321
1322 /* Retrieve the value of REGNUM in FRAME.  Don't give up!  */
1323
1324 static struct value *
1325 alpha_heuristic_frame_prev_register (struct frame_info *this_frame,
1326                                      void **this_prologue_cache, int regnum)
1327 {
1328   struct alpha_heuristic_unwind_cache *info
1329     = alpha_heuristic_frame_unwind_cache (this_frame, this_prologue_cache, 0);
1330
1331   /* The PC of the previous frame is stored in the link register of
1332      the current frame.  Frob regnum so that we pull the value from
1333      the correct place.  */
1334   if (regnum == ALPHA_PC_REGNUM)
1335     regnum = info->return_reg;
1336   
1337   return trad_frame_get_prev_register (this_frame, info->saved_regs, regnum);
1338 }
1339
1340 static const struct frame_unwind alpha_heuristic_frame_unwind = {
1341   NORMAL_FRAME,
1342   alpha_heuristic_frame_this_id,
1343   alpha_heuristic_frame_prev_register,
1344   NULL,
1345   default_frame_sniffer
1346 };
1347
1348 static CORE_ADDR
1349 alpha_heuristic_frame_base_address (struct frame_info *this_frame,
1350                                     void **this_prologue_cache)
1351 {
1352   struct alpha_heuristic_unwind_cache *info
1353     = alpha_heuristic_frame_unwind_cache (this_frame, this_prologue_cache, 0);
1354
1355   return info->vfp;
1356 }
1357
1358 static const struct frame_base alpha_heuristic_frame_base = {
1359   &alpha_heuristic_frame_unwind,
1360   alpha_heuristic_frame_base_address,
1361   alpha_heuristic_frame_base_address,
1362   alpha_heuristic_frame_base_address
1363 };
1364
1365 /* Just like reinit_frame_cache, but with the right arguments to be
1366    callable as an sfunc.  Used by the "set heuristic-fence-post" command.  */
1367
1368 static void
1369 reinit_frame_cache_sfunc (char *args, int from_tty, struct cmd_list_element *c)
1370 {
1371   reinit_frame_cache ();
1372 }
1373
1374 \f
1375 /* Assuming NEXT_FRAME->prev is a dummy, return the frame ID of that
1376    dummy frame.  The frame ID's base needs to match the TOS value
1377    saved by save_dummy_frame_tos(), and the PC match the dummy frame's
1378    breakpoint.  */
1379
1380 static struct frame_id
1381 alpha_dummy_id (struct gdbarch *gdbarch, struct frame_info *this_frame)
1382 {
1383   ULONGEST base;
1384   base = get_frame_register_unsigned (this_frame, ALPHA_SP_REGNUM);
1385   return frame_id_build (base, get_frame_pc (this_frame));
1386 }
1387
1388 static CORE_ADDR
1389 alpha_unwind_pc (struct gdbarch *gdbarch, struct frame_info *next_frame)
1390 {
1391   ULONGEST pc;
1392   pc = frame_unwind_register_unsigned (next_frame, ALPHA_PC_REGNUM);
1393   return pc;
1394 }
1395
1396 \f
1397 /* Helper routines for alpha*-nat.c files to move register sets to and
1398    from core files.  The UNIQUE pointer is allowed to be NULL, as most
1399    targets don't supply this value in their core files.  */
1400
1401 void
1402 alpha_supply_int_regs (struct regcache *regcache, int regno,
1403                        const void *r0_r30, const void *pc, const void *unique)
1404 {
1405   const gdb_byte *regs = r0_r30;
1406   int i;
1407
1408   for (i = 0; i < 31; ++i)
1409     if (regno == i || regno == -1)
1410       regcache_raw_supply (regcache, i, regs + i * 8);
1411
1412   if (regno == ALPHA_ZERO_REGNUM || regno == -1)
1413     regcache_raw_supply (regcache, ALPHA_ZERO_REGNUM, NULL);
1414
1415   if (regno == ALPHA_PC_REGNUM || regno == -1)
1416     regcache_raw_supply (regcache, ALPHA_PC_REGNUM, pc);
1417
1418   if (regno == ALPHA_UNIQUE_REGNUM || regno == -1)
1419     regcache_raw_supply (regcache, ALPHA_UNIQUE_REGNUM, unique);
1420 }
1421
1422 void
1423 alpha_fill_int_regs (const struct regcache *regcache,
1424                      int regno, void *r0_r30, void *pc, void *unique)
1425 {
1426   gdb_byte *regs = r0_r30;
1427   int i;
1428
1429   for (i = 0; i < 31; ++i)
1430     if (regno == i || regno == -1)
1431       regcache_raw_collect (regcache, i, regs + i * 8);
1432
1433   if (regno == ALPHA_PC_REGNUM || regno == -1)
1434     regcache_raw_collect (regcache, ALPHA_PC_REGNUM, pc);
1435
1436   if (unique && (regno == ALPHA_UNIQUE_REGNUM || regno == -1))
1437     regcache_raw_collect (regcache, ALPHA_UNIQUE_REGNUM, unique);
1438 }
1439
1440 void
1441 alpha_supply_fp_regs (struct regcache *regcache, int regno,
1442                       const void *f0_f30, const void *fpcr)
1443 {
1444   const gdb_byte *regs = f0_f30;
1445   int i;
1446
1447   for (i = ALPHA_FP0_REGNUM; i < ALPHA_FP0_REGNUM + 31; ++i)
1448     if (regno == i || regno == -1)
1449       regcache_raw_supply (regcache, i,
1450                            regs + (i - ALPHA_FP0_REGNUM) * 8);
1451
1452   if (regno == ALPHA_FPCR_REGNUM || regno == -1)
1453     regcache_raw_supply (regcache, ALPHA_FPCR_REGNUM, fpcr);
1454 }
1455
1456 void
1457 alpha_fill_fp_regs (const struct regcache *regcache,
1458                     int regno, void *f0_f30, void *fpcr)
1459 {
1460   gdb_byte *regs = f0_f30;
1461   int i;
1462
1463   for (i = ALPHA_FP0_REGNUM; i < ALPHA_FP0_REGNUM + 31; ++i)
1464     if (regno == i || regno == -1)
1465       regcache_raw_collect (regcache, i,
1466                             regs + (i - ALPHA_FP0_REGNUM) * 8);
1467
1468   if (regno == ALPHA_FPCR_REGNUM || regno == -1)
1469     regcache_raw_collect (regcache, ALPHA_FPCR_REGNUM, fpcr);
1470 }
1471
1472 \f
1473
1474 /* Return nonzero if the G_floating register value in REG is equal to
1475    zero for FP control instructions.  */
1476    
1477 static int
1478 fp_register_zero_p (LONGEST reg)
1479 {
1480   /* Check that all bits except the sign bit are zero.  */
1481   const LONGEST zero_mask = ((LONGEST) 1 << 63) ^ -1;
1482
1483   return ((reg & zero_mask) == 0);
1484 }
1485
1486 /* Return the value of the sign bit for the G_floating register
1487    value held in REG.  */
1488
1489 static int
1490 fp_register_sign_bit (LONGEST reg)
1491 {
1492   const LONGEST sign_mask = (LONGEST) 1 << 63;
1493
1494   return ((reg & sign_mask) != 0);
1495 }
1496
1497 /* alpha_software_single_step() is called just before we want to resume
1498    the inferior, if we want to single-step it but there is no hardware
1499    or kernel single-step support (NetBSD on Alpha, for example).  We find
1500    the target of the coming instruction and breakpoint it.  */
1501
1502 static CORE_ADDR
1503 alpha_next_pc (struct frame_info *frame, CORE_ADDR pc)
1504 {
1505   struct gdbarch *gdbarch = get_frame_arch (frame);
1506   unsigned int insn;
1507   unsigned int op;
1508   int regno;
1509   int offset;
1510   LONGEST rav;
1511
1512   insn = alpha_read_insn (gdbarch, pc);
1513
1514   /* Opcode is top 6 bits. */
1515   op = (insn >> 26) & 0x3f;
1516
1517   if (op == 0x1a)
1518     {
1519       /* Jump format: target PC is:
1520          RB & ~3  */
1521       return (get_frame_register_unsigned (frame, (insn >> 16) & 0x1f) & ~3);
1522     }
1523
1524   if ((op & 0x30) == 0x30)
1525     {
1526       /* Branch format: target PC is:
1527          (new PC) + (4 * sext(displacement))  */
1528       if (op == 0x30            /* BR */
1529           || op == 0x34)        /* BSR */
1530         {
1531  branch_taken:
1532           offset = (insn & 0x001fffff);
1533           if (offset & 0x00100000)
1534             offset  |= 0xffe00000;
1535           offset *= ALPHA_INSN_SIZE;
1536           return (pc + ALPHA_INSN_SIZE + offset);
1537         }
1538
1539       /* Need to determine if branch is taken; read RA.  */
1540       regno = (insn >> 21) & 0x1f;
1541       switch (op)
1542         {
1543           case 0x31:              /* FBEQ */
1544           case 0x36:              /* FBGE */
1545           case 0x37:              /* FBGT */
1546           case 0x33:              /* FBLE */
1547           case 0x32:              /* FBLT */
1548           case 0x35:              /* FBNE */
1549             regno += gdbarch_fp0_regnum (gdbarch);
1550         }
1551       
1552       rav = get_frame_register_signed (frame, regno);
1553
1554       switch (op)
1555         {
1556         case 0x38:              /* BLBC */
1557           if ((rav & 1) == 0)
1558             goto branch_taken;
1559           break;
1560         case 0x3c:              /* BLBS */
1561           if (rav & 1)
1562             goto branch_taken;
1563           break;
1564         case 0x39:              /* BEQ */
1565           if (rav == 0)
1566             goto branch_taken;
1567           break;
1568         case 0x3d:              /* BNE */
1569           if (rav != 0)
1570             goto branch_taken;
1571           break;
1572         case 0x3a:              /* BLT */
1573           if (rav < 0)
1574             goto branch_taken;
1575           break;
1576         case 0x3b:              /* BLE */
1577           if (rav <= 0)
1578             goto branch_taken;
1579           break;
1580         case 0x3f:              /* BGT */
1581           if (rav > 0)
1582             goto branch_taken;
1583           break;
1584         case 0x3e:              /* BGE */
1585           if (rav >= 0)
1586             goto branch_taken;
1587           break;
1588
1589         /* Floating point branches.  */
1590         
1591         case 0x31:              /* FBEQ */
1592           if (fp_register_zero_p (rav))
1593             goto branch_taken;
1594           break;
1595         case 0x36:              /* FBGE */
1596           if (fp_register_sign_bit (rav) == 0 || fp_register_zero_p (rav))
1597             goto branch_taken;
1598           break;
1599         case 0x37:              /* FBGT */
1600           if (fp_register_sign_bit (rav) == 0 && ! fp_register_zero_p (rav))
1601             goto branch_taken;
1602           break;
1603         case 0x33:              /* FBLE */
1604           if (fp_register_sign_bit (rav) == 1 || fp_register_zero_p (rav))
1605             goto branch_taken;
1606           break;
1607         case 0x32:              /* FBLT */
1608           if (fp_register_sign_bit (rav) == 1 && ! fp_register_zero_p (rav))
1609             goto branch_taken;
1610           break;
1611         case 0x35:              /* FBNE */
1612           if (! fp_register_zero_p (rav))
1613             goto branch_taken;
1614           break;
1615         }
1616     }
1617
1618   /* Not a branch or branch not taken; target PC is:
1619      pc + 4  */
1620   return (pc + ALPHA_INSN_SIZE);
1621 }
1622
1623 int
1624 alpha_software_single_step (struct frame_info *frame)
1625 {
1626   struct gdbarch *gdbarch = get_frame_arch (frame);
1627   struct address_space *aspace = get_frame_address_space (frame);
1628   CORE_ADDR pc, next_pc;
1629
1630   pc = get_frame_pc (frame);
1631   next_pc = alpha_next_pc (frame, pc);
1632
1633   insert_single_step_breakpoint (gdbarch, aspace, next_pc);
1634   return 1;
1635 }
1636
1637 \f
1638 /* Initialize the current architecture based on INFO.  If possible, re-use an
1639    architecture from ARCHES, which is a list of architectures already created
1640    during this debugging session.
1641
1642    Called e.g. at program startup, when reading a core file, and when reading
1643    a binary file.  */
1644
1645 static struct gdbarch *
1646 alpha_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
1647 {
1648   struct gdbarch_tdep *tdep;
1649   struct gdbarch *gdbarch;
1650
1651   /* Try to determine the ABI of the object we are loading.  */
1652   if (info.abfd != NULL && info.osabi == GDB_OSABI_UNKNOWN)
1653     {
1654       /* If it's an ECOFF file, assume it's OSF/1.  */
1655       if (bfd_get_flavour (info.abfd) == bfd_target_ecoff_flavour)
1656         info.osabi = GDB_OSABI_OSF1;
1657     }
1658
1659   /* Find a candidate among extant architectures.  */
1660   arches = gdbarch_list_lookup_by_info (arches, &info);
1661   if (arches != NULL)
1662     return arches->gdbarch;
1663
1664   tdep = xmalloc (sizeof (struct gdbarch_tdep));
1665   gdbarch = gdbarch_alloc (&info, tdep);
1666
1667   /* Lowest text address.  This is used by heuristic_proc_start()
1668      to decide when to stop looking.  */
1669   tdep->vm_min_address = (CORE_ADDR) 0x120000000LL;
1670
1671   tdep->dynamic_sigtramp_offset = NULL;
1672   tdep->sigcontext_addr = NULL;
1673   tdep->sc_pc_offset = 2 * 8;
1674   tdep->sc_regs_offset = 4 * 8;
1675   tdep->sc_fpregs_offset = tdep->sc_regs_offset + 32 * 8 + 8;
1676
1677   tdep->jb_pc = -1;     /* longjmp support not enabled by default  */
1678
1679   tdep->return_in_memory = alpha_return_in_memory_always;
1680
1681   /* Type sizes */
1682   set_gdbarch_short_bit (gdbarch, 16);
1683   set_gdbarch_int_bit (gdbarch, 32);
1684   set_gdbarch_long_bit (gdbarch, 64);
1685   set_gdbarch_long_long_bit (gdbarch, 64);
1686   set_gdbarch_float_bit (gdbarch, 32);
1687   set_gdbarch_double_bit (gdbarch, 64);
1688   set_gdbarch_long_double_bit (gdbarch, 64);
1689   set_gdbarch_ptr_bit (gdbarch, 64);
1690
1691   /* Register info */
1692   set_gdbarch_num_regs (gdbarch, ALPHA_NUM_REGS);
1693   set_gdbarch_sp_regnum (gdbarch, ALPHA_SP_REGNUM);
1694   set_gdbarch_pc_regnum (gdbarch, ALPHA_PC_REGNUM);
1695   set_gdbarch_fp0_regnum (gdbarch, ALPHA_FP0_REGNUM);
1696
1697   set_gdbarch_register_name (gdbarch, alpha_register_name);
1698   set_gdbarch_register_type (gdbarch, alpha_register_type);
1699
1700   set_gdbarch_cannot_fetch_register (gdbarch, alpha_cannot_fetch_register);
1701   set_gdbarch_cannot_store_register (gdbarch, alpha_cannot_store_register);
1702
1703   set_gdbarch_convert_register_p (gdbarch, alpha_convert_register_p);
1704   set_gdbarch_register_to_value (gdbarch, alpha_register_to_value);
1705   set_gdbarch_value_to_register (gdbarch, alpha_value_to_register);
1706
1707   set_gdbarch_register_reggroup_p (gdbarch, alpha_register_reggroup_p);
1708
1709   /* Prologue heuristics.  */
1710   set_gdbarch_skip_prologue (gdbarch, alpha_skip_prologue);
1711
1712   /* Disassembler.  */
1713   set_gdbarch_print_insn (gdbarch, print_insn_alpha);
1714
1715   /* Call info.  */
1716
1717   set_gdbarch_return_value (gdbarch, alpha_return_value);
1718
1719   /* Settings for calling functions in the inferior.  */
1720   set_gdbarch_push_dummy_call (gdbarch, alpha_push_dummy_call);
1721
1722   /* Methods for saving / extracting a dummy frame's ID.  */
1723   set_gdbarch_dummy_id (gdbarch, alpha_dummy_id);
1724
1725   /* Return the unwound PC value.  */
1726   set_gdbarch_unwind_pc (gdbarch, alpha_unwind_pc);
1727
1728   set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
1729   set_gdbarch_skip_trampoline_code (gdbarch, find_solib_trampoline_target);
1730
1731   set_gdbarch_breakpoint_from_pc (gdbarch, alpha_breakpoint_from_pc);
1732   set_gdbarch_decr_pc_after_break (gdbarch, ALPHA_INSN_SIZE);
1733   set_gdbarch_cannot_step_breakpoint (gdbarch, 1);
1734
1735   /* Hook in ABI-specific overrides, if they have been registered.  */
1736   gdbarch_init_osabi (info, gdbarch);
1737
1738   /* Now that we have tuned the configuration, set a few final things
1739      based on what the OS ABI has told us.  */
1740
1741   if (tdep->jb_pc >= 0)
1742     set_gdbarch_get_longjmp_target (gdbarch, alpha_get_longjmp_target);
1743
1744   frame_unwind_append_unwinder (gdbarch, &alpha_sigtramp_frame_unwind);
1745   frame_unwind_append_unwinder (gdbarch, &alpha_heuristic_frame_unwind);
1746
1747   frame_base_set_default (gdbarch, &alpha_heuristic_frame_base);
1748
1749   return gdbarch;
1750 }
1751
1752 void
1753 alpha_dwarf2_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
1754 {
1755   dwarf2_append_unwinders (gdbarch);
1756   frame_base_append_sniffer (gdbarch, dwarf2_frame_base_sniffer);
1757 }
1758
1759 extern initialize_file_ftype _initialize_alpha_tdep; /* -Wmissing-prototypes */
1760
1761 void
1762 _initialize_alpha_tdep (void)
1763 {
1764   struct cmd_list_element *c;
1765
1766   gdbarch_register (bfd_arch_alpha, alpha_gdbarch_init, NULL);
1767
1768   /* Let the user set the fence post for heuristic_proc_start.  */
1769
1770   /* We really would like to have both "0" and "unlimited" work, but
1771      command.c doesn't deal with that.  So make it a var_zinteger
1772      because the user can always use "999999" or some such for unlimited.  */
1773   /* We need to throw away the frame cache when we set this, since it
1774      might change our ability to get backtraces.  */
1775   add_setshow_zinteger_cmd ("heuristic-fence-post", class_support,
1776                             &heuristic_fence_post, _("\
1777 Set the distance searched for the start of a function."), _("\
1778 Show the distance searched for the start of a function."), _("\
1779 If you are debugging a stripped executable, GDB needs to search through the\n\
1780 program for the start of a function.  This command sets the distance of the\n\
1781 search.  The only need to set it is when debugging a stripped executable."),
1782                             reinit_frame_cache_sfunc,
1783                             NULL, /* FIXME: i18n: The distance searched for the start of a function is \"%d\".  */
1784                             &setlist, &showlist);
1785 }