971dd632f24d37a2a5878be42267ef4087c6ea54
[external/binutils.git] / gdb / alpha-tdep.c
1 /* Target-dependent code for the ALPHA architecture, for GDB, the GNU Debugger.
2    Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002
3    Free Software Foundation, Inc.
4
5    This file is part of GDB.
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 59 Temple Place - Suite 330,
20    Boston, MA 02111-1307, USA.  */
21
22 #include "defs.h"
23 #include "frame.h"
24 #include "inferior.h"
25 #include "symtab.h"
26 #include "value.h"
27 #include "gdbcmd.h"
28 #include "gdbcore.h"
29 #include "dis-asm.h"
30 #include "symfile.h"
31 #include "objfiles.h"
32 #include "gdb_string.h"
33 #include "linespec.h"
34 #include "regcache.h"
35 #include "doublest.h"
36
37 struct frame_extra_info
38   {
39     alpha_extra_func_info_t proc_desc;
40     int localoff;
41     int pc_reg;
42   };
43
44 /* FIXME: Some of this code should perhaps be merged with mips-tdep.c.  */
45
46 /* Prototypes for local functions. */
47
48 static void alpha_find_saved_regs (struct frame_info *);
49
50 static alpha_extra_func_info_t push_sigtramp_desc (CORE_ADDR low_addr);
51
52 static CORE_ADDR read_next_frame_reg (struct frame_info *, int);
53
54 static CORE_ADDR heuristic_proc_start (CORE_ADDR);
55
56 static alpha_extra_func_info_t heuristic_proc_desc (CORE_ADDR,
57                                                     CORE_ADDR,
58                                                     struct frame_info *);
59
60 static alpha_extra_func_info_t find_proc_desc (CORE_ADDR,
61                                                struct frame_info *);
62
63 #if 0
64 static int alpha_in_lenient_prologue (CORE_ADDR, CORE_ADDR);
65 #endif
66
67 static void reinit_frame_cache_sfunc (char *, int, struct cmd_list_element *);
68
69 static CORE_ADDR after_prologue (CORE_ADDR pc,
70                                  alpha_extra_func_info_t proc_desc);
71
72 static int alpha_in_prologue (CORE_ADDR pc,
73                               alpha_extra_func_info_t proc_desc);
74
75 static int alpha_about_to_return (CORE_ADDR pc);
76
77 void _initialize_alpha_tdep (void);
78
79 /* Heuristic_proc_start may hunt through the text section for a long
80    time across a 2400 baud serial line.  Allows the user to limit this
81    search.  */
82 static unsigned int heuristic_fence_post = 0;
83 /* *INDENT-OFF* */
84 /* Layout of a stack frame on the alpha:
85
86                 |                               |
87  pdr members:   |  7th ... nth arg,             |
88                 |  `pushed' by caller.          |
89                 |                               |
90 ----------------|-------------------------------|<--  old_sp == vfp
91    ^  ^  ^  ^   |                               |
92    |  |  |  |   |                               |
93    |  |localoff |  Copies of 1st .. 6th         |
94    |  |  |  |   |  argument if necessary.       |
95    |  |  |  v   |                               |
96    |  |  |  --- |-------------------------------|<-- FRAME_LOCALS_ADDRESS
97    |  |  |      |                               |
98    |  |  |      |  Locals and temporaries.      |
99    |  |  |      |                               |
100    |  |  |      |-------------------------------|
101    |  |  |      |                               |
102    |-fregoffset |  Saved float registers.       |
103    |  |  |      |  F9                           |
104    |  |  |      |   .                           |
105    |  |  |      |   .                           |
106    |  |  |      |  F2                           |
107    |  |  v      |                               |
108    |  |  -------|-------------------------------|
109    |  |         |                               |
110    |  |         |  Saved registers.             |
111    |  |         |  S6                           |
112    |-regoffset  |   .                           |
113    |  |         |   .                           |
114    |  |         |  S0                           |
115    |  |         |  pdr.pcreg                    |
116    |  v         |                               |
117    |  ----------|-------------------------------|
118    |            |                               |
119  frameoffset    |  Argument build area, gets    |
120    |            |  7th ... nth arg for any      |
121    |            |  called procedure.            |
122    v            |                               |
123    -------------|-------------------------------|<-- sp
124                 |                               |
125 */
126 /* *INDENT-ON* */
127
128
129
130 #define PROC_LOW_ADDR(proc) ((proc)->pdr.adr)   /* least address */
131 /* These next two fields are kind of being hijacked.  I wonder if
132    iline is too small for the values it needs to hold, if GDB is
133    running on a 32-bit host.  */
134 #define PROC_HIGH_ADDR(proc) ((proc)->pdr.iline)        /* upper address bound */
135 #define PROC_DUMMY_FRAME(proc) ((proc)->pdr.cbLineOffset)       /*CALL_DUMMY frame */
136 #define PROC_FRAME_OFFSET(proc) ((proc)->pdr.frameoffset)
137 #define PROC_FRAME_REG(proc) ((proc)->pdr.framereg)
138 #define PROC_REG_MASK(proc) ((proc)->pdr.regmask)
139 #define PROC_FREG_MASK(proc) ((proc)->pdr.fregmask)
140 #define PROC_REG_OFFSET(proc) ((proc)->pdr.regoffset)
141 #define PROC_FREG_OFFSET(proc) ((proc)->pdr.fregoffset)
142 #define PROC_PC_REG(proc) ((proc)->pdr.pcreg)
143 #define PROC_LOCALOFF(proc) ((proc)->pdr.localoff)
144 #define PROC_SYMBOL(proc) (*(struct symbol**)&(proc)->pdr.isym)
145 #define _PROC_MAGIC_ 0x0F0F0F0F
146 #define PROC_DESC_IS_DUMMY(proc) ((proc)->pdr.isym == _PROC_MAGIC_)
147 #define SET_PROC_DESC_IS_DUMMY(proc) ((proc)->pdr.isym = _PROC_MAGIC_)
148
149 struct linked_proc_info
150   {
151     struct alpha_extra_func_info info;
152     struct linked_proc_info *next;
153   }
154  *linked_proc_desc_table = NULL;
155 \f
156 int
157 alpha_osf_in_sigtramp (CORE_ADDR pc, char *func_name)
158 {
159   return (func_name != NULL && STREQ ("__sigtramp", func_name));
160 }
161
162 /* Under GNU/Linux, signal handler invocations can be identified by the
163    designated code sequence that is used to return from a signal
164    handler.  In particular, the return address of a signal handler
165    points to the following sequence (the first instruction is quadword
166    aligned):
167
168    bis $30,$30,$16
169    addq $31,0x67,$0
170    call_pal callsys
171
172    Each instruction has a unique encoding, so we simply attempt to
173    match the instruction the pc is pointing to with any of the above
174    instructions.  If there is a hit, we know the offset to the start
175    of the designated sequence and can then check whether we really are
176    executing in a designated sequence.  If not, -1 is returned,
177    otherwise the offset from the start of the desingated sequence is
178    returned.
179
180    There is a slight chance of false hits: code could jump into the
181    middle of the designated sequence, in which case there is no
182    guarantee that we are in the middle of a sigreturn syscall.  Don't
183    think this will be a problem in praxis, though.
184  */
185
186 #ifndef TM_LINUXALPHA_H
187 /* HACK: Provide a prototype when compiling this file for non
188    linuxalpha targets. */
189 long alpha_linux_sigtramp_offset (CORE_ADDR pc);
190 #endif
191 long
192 alpha_linux_sigtramp_offset (CORE_ADDR pc)
193 {
194   unsigned int i[3], w;
195   long off;
196
197   if (read_memory_nobpt (pc, (char *) &w, 4) != 0)
198     return -1;
199
200   off = -1;
201   switch (w)
202     {
203     case 0x47de0410:
204       off = 0;
205       break;                    /* bis $30,$30,$16 */
206     case 0x43ecf400:
207       off = 4;
208       break;                    /* addq $31,0x67,$0 */
209     case 0x00000083:
210       off = 8;
211       break;                    /* call_pal callsys */
212     default:
213       return -1;
214     }
215   pc -= off;
216   if (pc & 0x7)
217     {
218       /* designated sequence is not quadword aligned */
219       return -1;
220     }
221
222   if (read_memory_nobpt (pc, (char *) i, sizeof (i)) != 0)
223     return -1;
224
225   if (i[0] == 0x47de0410 && i[1] == 0x43ecf400 && i[2] == 0x00000083)
226     return off;
227
228   return -1;
229 }
230 \f
231
232 /* Under OSF/1, the __sigtramp routine is frameless and has a frame
233    size of zero, but we are able to backtrace through it.  */
234 CORE_ADDR
235 alpha_osf_skip_sigtramp_frame (struct frame_info *frame, CORE_ADDR pc)
236 {
237   char *name;
238   find_pc_partial_function (pc, &name, (CORE_ADDR *) NULL, (CORE_ADDR *) NULL);
239   if (IN_SIGTRAMP (pc, name))
240     return frame->frame;
241   else
242     return 0;
243 }
244 \f
245
246 /* Dynamically create a signal-handler caller procedure descriptor for
247    the signal-handler return code starting at address LOW_ADDR.  The
248    descriptor is added to the linked_proc_desc_table.  */
249
250 static alpha_extra_func_info_t
251 push_sigtramp_desc (CORE_ADDR low_addr)
252 {
253   struct linked_proc_info *link;
254   alpha_extra_func_info_t proc_desc;
255
256   link = (struct linked_proc_info *)
257     xmalloc (sizeof (struct linked_proc_info));
258   link->next = linked_proc_desc_table;
259   linked_proc_desc_table = link;
260
261   proc_desc = &link->info;
262
263   proc_desc->numargs = 0;
264   PROC_LOW_ADDR (proc_desc) = low_addr;
265   PROC_HIGH_ADDR (proc_desc) = low_addr + 3 * 4;
266   PROC_DUMMY_FRAME (proc_desc) = 0;
267   PROC_FRAME_OFFSET (proc_desc) = 0x298;        /* sizeof(struct sigcontext_struct) */
268   PROC_FRAME_REG (proc_desc) = SP_REGNUM;
269   PROC_REG_MASK (proc_desc) = 0xffff;
270   PROC_FREG_MASK (proc_desc) = 0xffff;
271   PROC_PC_REG (proc_desc) = 26;
272   PROC_LOCALOFF (proc_desc) = 0;
273   SET_PROC_DESC_IS_DYN_SIGTRAMP (proc_desc);
274   return (proc_desc);
275 }
276 \f
277
278 char *
279 alpha_register_name (int regno)
280 {
281   static char *register_names[] =
282   {
283     "v0",   "t0",   "t1",   "t2",   "t3",   "t4",   "t5",   "t6",
284     "t7",   "s0",   "s1",   "s2",   "s3",   "s4",   "s5",   "fp",
285     "a0",   "a1",   "a2",   "a3",   "a4",   "a5",   "t8",   "t9",
286     "t10",  "t11",  "ra",   "t12",  "at",   "gp",   "sp",   "zero",
287     "f0",   "f1",   "f2",   "f3",   "f4",   "f5",   "f6",   "f7",
288     "f8",   "f9",   "f10",  "f11",  "f12",  "f13",  "f14",  "f15",
289     "f16",  "f17",  "f18",  "f19",  "f20",  "f21",  "f22",  "f23",
290     "f24",  "f25",  "f26",  "f27",  "f28",  "f29",  "f30",  "fpcr",
291     "pc",   "vfp",
292   };
293
294   if (regno < 0)
295     return (NULL);
296   if (regno >= (sizeof(register_names) / sizeof(*register_names)))
297     return (NULL);
298   return (register_names[regno]);
299 }
300
301 int
302 alpha_cannot_fetch_register (int regno)
303 {
304   return (regno == FP_REGNUM || regno == ZERO_REGNUM);
305 }
306
307 int
308 alpha_cannot_store_register (int regno)
309 {
310   return (regno == FP_REGNUM || regno == ZERO_REGNUM);
311 }
312
313 int
314 alpha_register_convertible (int regno)
315 {
316   return (regno >= FP0_REGNUM && regno <= FP0_REGNUM + 31);
317 }
318
319 struct type *
320 alpha_register_virtual_type (int regno)
321 {
322   return ((regno >= FP0_REGNUM && regno < (FP0_REGNUM+31))
323           ? builtin_type_double : builtin_type_long);
324 }
325
326 int
327 alpha_register_byte (int regno)
328 {
329   return (regno * 8);
330 }
331
332 int
333 alpha_register_raw_size (int regno)
334 {
335   return 8;
336 }
337
338 int
339 alpha_register_virtual_size (int regno)
340 {
341   return 8;
342 }
343 \f
344
345 /* Guaranteed to set frame->saved_regs to some values (it never leaves it
346    NULL).  */
347
348 static void
349 alpha_find_saved_regs (struct frame_info *frame)
350 {
351   int ireg;
352   CORE_ADDR reg_position;
353   unsigned long mask;
354   alpha_extra_func_info_t proc_desc;
355   int returnreg;
356
357   frame_saved_regs_zalloc (frame);
358
359   /* If it is the frame for __sigtramp, the saved registers are located
360      in a sigcontext structure somewhere on the stack. __sigtramp
361      passes a pointer to the sigcontext structure on the stack.
362      If the stack layout for __sigtramp changes, or if sigcontext offsets
363      change, we might have to update this code.  */
364 #ifndef SIGFRAME_PC_OFF
365 #define SIGFRAME_PC_OFF         (2 * 8)
366 #define SIGFRAME_REGSAVE_OFF    (4 * 8)
367 #define SIGFRAME_FPREGSAVE_OFF  (SIGFRAME_REGSAVE_OFF + 32 * 8 + 8)
368 #endif
369   if (frame->signal_handler_caller)
370     {
371       CORE_ADDR sigcontext_addr;
372
373       sigcontext_addr = SIGCONTEXT_ADDR (frame);
374       for (ireg = 0; ireg < 32; ireg++)
375         {
376           reg_position = sigcontext_addr + SIGFRAME_REGSAVE_OFF + ireg * 8;
377           frame->saved_regs[ireg] = reg_position;
378         }
379       for (ireg = 0; ireg < 32; ireg++)
380         {
381           reg_position = sigcontext_addr + SIGFRAME_FPREGSAVE_OFF + ireg * 8;
382           frame->saved_regs[FP0_REGNUM + ireg] = reg_position;
383         }
384       frame->saved_regs[PC_REGNUM] = sigcontext_addr + SIGFRAME_PC_OFF;
385       return;
386     }
387
388   proc_desc = frame->extra_info->proc_desc;
389   if (proc_desc == NULL)
390     /* I'm not sure how/whether this can happen.  Normally when we can't
391        find a proc_desc, we "synthesize" one using heuristic_proc_desc
392        and set the saved_regs right away.  */
393     return;
394
395   /* Fill in the offsets for the registers which gen_mask says
396      were saved.  */
397
398   reg_position = frame->frame + PROC_REG_OFFSET (proc_desc);
399   mask = PROC_REG_MASK (proc_desc);
400
401   returnreg = PROC_PC_REG (proc_desc);
402
403   /* Note that RA is always saved first, regardless of its actual
404      register number.  */
405   if (mask & (1 << returnreg))
406     {
407       frame->saved_regs[returnreg] = reg_position;
408       reg_position += 8;
409       mask &= ~(1 << returnreg);        /* Clear bit for RA so we
410                                            don't save again later. */
411     }
412
413   for (ireg = 0; ireg <= 31; ++ireg)
414     if (mask & (1 << ireg))
415       {
416         frame->saved_regs[ireg] = reg_position;
417         reg_position += 8;
418       }
419
420   /* Fill in the offsets for the registers which float_mask says
421      were saved.  */
422
423   reg_position = frame->frame + PROC_FREG_OFFSET (proc_desc);
424   mask = PROC_FREG_MASK (proc_desc);
425
426   for (ireg = 0; ireg <= 31; ++ireg)
427     if (mask & (1 << ireg))
428       {
429         frame->saved_regs[FP0_REGNUM + ireg] = reg_position;
430         reg_position += 8;
431       }
432
433   frame->saved_regs[PC_REGNUM] = frame->saved_regs[returnreg];
434 }
435
436 void
437 alpha_frame_init_saved_regs (struct frame_info *fi)
438 {
439   if (fi->saved_regs == NULL)
440     alpha_find_saved_regs (fi);
441   fi->saved_regs[SP_REGNUM] = fi->frame;
442 }
443
444 void
445 alpha_init_frame_pc_first (int fromleaf, struct frame_info *prev)
446 {
447   prev->pc = (fromleaf ? SAVED_PC_AFTER_CALL (prev->next) :
448               prev->next ? FRAME_SAVED_PC (prev->next) : read_pc ());
449 }
450
451 static CORE_ADDR
452 read_next_frame_reg (struct frame_info *fi, int regno)
453 {
454   for (; fi; fi = fi->next)
455     {
456       /* We have to get the saved sp from the sigcontext
457          if it is a signal handler frame.  */
458       if (regno == SP_REGNUM && !fi->signal_handler_caller)
459         return fi->frame;
460       else
461         {
462           if (fi->saved_regs == NULL)
463             alpha_find_saved_regs (fi);
464           if (fi->saved_regs[regno])
465             return read_memory_integer (fi->saved_regs[regno], 8);
466         }
467     }
468   return read_register (regno);
469 }
470
471 CORE_ADDR
472 alpha_frame_saved_pc (struct frame_info *frame)
473 {
474   alpha_extra_func_info_t proc_desc = frame->extra_info->proc_desc;
475   /* We have to get the saved pc from the sigcontext
476      if it is a signal handler frame.  */
477   int pcreg = frame->signal_handler_caller ? PC_REGNUM
478                                            : frame->extra_info->pc_reg;
479
480   if (proc_desc && PROC_DESC_IS_DUMMY (proc_desc))
481     return read_memory_integer (frame->frame - 8, 8);
482
483   return read_next_frame_reg (frame, pcreg);
484 }
485
486 CORE_ADDR
487 alpha_saved_pc_after_call (struct frame_info *frame)
488 {
489   CORE_ADDR pc = frame->pc;
490   CORE_ADDR tmp;
491   alpha_extra_func_info_t proc_desc;
492   int pcreg;
493
494   /* Skip over shared library trampoline if necessary.  */
495   tmp = SKIP_TRAMPOLINE_CODE (pc);
496   if (tmp != 0)
497     pc = tmp;
498
499   proc_desc = find_proc_desc (pc, frame->next);
500   pcreg = proc_desc ? PROC_PC_REG (proc_desc) : RA_REGNUM;
501
502   if (frame->signal_handler_caller)
503     return alpha_frame_saved_pc (frame);
504   else
505     return read_register (pcreg);
506 }
507
508
509 static struct alpha_extra_func_info temp_proc_desc;
510 static CORE_ADDR temp_saved_regs[NUM_REGS];
511
512 /* Nonzero if instruction at PC is a return instruction.  "ret
513    $zero,($ra),1" on alpha. */
514
515 static int
516 alpha_about_to_return (CORE_ADDR pc)
517 {
518   return read_memory_integer (pc, 4) == 0x6bfa8001;
519 }
520
521
522
523 /* This fencepost looks highly suspicious to me.  Removing it also
524    seems suspicious as it could affect remote debugging across serial
525    lines.  */
526
527 static CORE_ADDR
528 heuristic_proc_start (CORE_ADDR pc)
529 {
530   CORE_ADDR start_pc = pc;
531   CORE_ADDR fence = start_pc - heuristic_fence_post;
532
533   if (start_pc == 0)
534     return 0;
535
536   if (heuristic_fence_post == UINT_MAX
537       || fence < VM_MIN_ADDRESS)
538     fence = VM_MIN_ADDRESS;
539
540   /* search back for previous return */
541   for (start_pc -= 4;; start_pc -= 4)
542     if (start_pc < fence)
543       {
544         /* It's not clear to me why we reach this point when
545            stop_soon_quietly, but with this test, at least we
546            don't print out warnings for every child forked (eg, on
547            decstation).  22apr93 rich@cygnus.com.  */
548         if (!stop_soon_quietly)
549           {
550             static int blurb_printed = 0;
551
552             if (fence == VM_MIN_ADDRESS)
553               warning ("Hit beginning of text section without finding");
554             else
555               warning ("Hit heuristic-fence-post without finding");
556
557             warning ("enclosing function for address 0x%s", paddr_nz (pc));
558             if (!blurb_printed)
559               {
560                 printf_filtered ("\
561 This warning occurs if you are debugging a function without any symbols\n\
562 (for example, in a stripped executable).  In that case, you may wish to\n\
563 increase the size of the search with the `set heuristic-fence-post' command.\n\
564 \n\
565 Otherwise, you told GDB there was a function where there isn't one, or\n\
566 (more likely) you have encountered a bug in GDB.\n");
567                 blurb_printed = 1;
568               }
569           }
570
571         return 0;
572       }
573     else if (alpha_about_to_return (start_pc))
574       break;
575
576   start_pc += 4;                /* skip return */
577   return start_pc;
578 }
579
580 static alpha_extra_func_info_t
581 heuristic_proc_desc (CORE_ADDR start_pc, CORE_ADDR limit_pc,
582                      struct frame_info *next_frame)
583 {
584   CORE_ADDR sp = read_next_frame_reg (next_frame, SP_REGNUM);
585   CORE_ADDR cur_pc;
586   int frame_size;
587   int has_frame_reg = 0;
588   unsigned long reg_mask = 0;
589   int pcreg = -1;
590
591   if (start_pc == 0)
592     return NULL;
593   memset (&temp_proc_desc, '\0', sizeof (temp_proc_desc));
594   memset (&temp_saved_regs, '\0', SIZEOF_FRAME_SAVED_REGS);
595   PROC_LOW_ADDR (&temp_proc_desc) = start_pc;
596
597   if (start_pc + 200 < limit_pc)
598     limit_pc = start_pc + 200;
599   frame_size = 0;
600   for (cur_pc = start_pc; cur_pc < limit_pc; cur_pc += 4)
601     {
602       char buf[4];
603       unsigned long word;
604       int status;
605
606       status = read_memory_nobpt (cur_pc, buf, 4);
607       if (status)
608         memory_error (status, cur_pc);
609       word = extract_unsigned_integer (buf, 4);
610
611       if ((word & 0xffff0000) == 0x23de0000)    /* lda $sp,n($sp) */
612         {
613           if (word & 0x8000)
614             frame_size += (-word) & 0xffff;
615           else
616             /* Exit loop if a positive stack adjustment is found, which
617                usually means that the stack cleanup code in the function
618                epilogue is reached.  */
619             break;
620         }
621       else if ((word & 0xfc1f0000) == 0xb41e0000        /* stq reg,n($sp) */
622                && (word & 0xffff0000) != 0xb7fe0000)    /* reg != $zero */
623         {
624           int reg = (word & 0x03e00000) >> 21;
625           reg_mask |= 1 << reg;
626           temp_saved_regs[reg] = sp + (short) word;
627
628           /* Starting with OSF/1-3.2C, the system libraries are shipped
629              without local symbols, but they still contain procedure
630              descriptors without a symbol reference. GDB is currently
631              unable to find these procedure descriptors and uses
632              heuristic_proc_desc instead.
633              As some low level compiler support routines (__div*, __add*)
634              use a non-standard return address register, we have to
635              add some heuristics to determine the return address register,
636              or stepping over these routines will fail.
637              Usually the return address register is the first register
638              saved on the stack, but assembler optimization might
639              rearrange the register saves.
640              So we recognize only a few registers (t7, t9, ra) within
641              the procedure prologue as valid return address registers.
642              If we encounter a return instruction, we extract the
643              the return address register from it.
644
645              FIXME: Rewriting GDB to access the procedure descriptors,
646              e.g. via the minimal symbol table, might obviate this hack.  */
647           if (pcreg == -1
648               && cur_pc < (start_pc + 80)
649               && (reg == T7_REGNUM || reg == T9_REGNUM || reg == RA_REGNUM))
650             pcreg = reg;
651         }
652       else if ((word & 0xffe0ffff) == 0x6be08001)       /* ret zero,reg,1 */
653         pcreg = (word >> 16) & 0x1f;
654       else if (word == 0x47de040f)      /* bis sp,sp fp */
655         has_frame_reg = 1;
656     }
657   if (pcreg == -1)
658     {
659       /* If we haven't found a valid return address register yet,
660          keep searching in the procedure prologue.  */
661       while (cur_pc < (limit_pc + 80) && cur_pc < (start_pc + 80))
662         {
663           char buf[4];
664           unsigned long word;
665
666           if (read_memory_nobpt (cur_pc, buf, 4))
667             break;
668           cur_pc += 4;
669           word = extract_unsigned_integer (buf, 4);
670
671           if ((word & 0xfc1f0000) == 0xb41e0000         /* stq reg,n($sp) */
672               && (word & 0xffff0000) != 0xb7fe0000)     /* reg != $zero */
673             {
674               int reg = (word & 0x03e00000) >> 21;
675               if (reg == T7_REGNUM || reg == T9_REGNUM || reg == RA_REGNUM)
676                 {
677                   pcreg = reg;
678                   break;
679                 }
680             }
681           else if ((word & 0xffe0ffff) == 0x6be08001)   /* ret zero,reg,1 */
682             {
683               pcreg = (word >> 16) & 0x1f;
684               break;
685             }
686         }
687     }
688
689   if (has_frame_reg)
690     PROC_FRAME_REG (&temp_proc_desc) = GCC_FP_REGNUM;
691   else
692     PROC_FRAME_REG (&temp_proc_desc) = SP_REGNUM;
693   PROC_FRAME_OFFSET (&temp_proc_desc) = frame_size;
694   PROC_REG_MASK (&temp_proc_desc) = reg_mask;
695   PROC_PC_REG (&temp_proc_desc) = (pcreg == -1) ? RA_REGNUM : pcreg;
696   PROC_LOCALOFF (&temp_proc_desc) = 0;  /* XXX - bogus */
697   return &temp_proc_desc;
698 }
699
700 /* This returns the PC of the first inst after the prologue.  If we can't
701    find the prologue, then return 0.  */
702
703 static CORE_ADDR
704 after_prologue (CORE_ADDR pc, alpha_extra_func_info_t proc_desc)
705 {
706   struct symtab_and_line sal;
707   CORE_ADDR func_addr, func_end;
708
709   if (!proc_desc)
710     proc_desc = find_proc_desc (pc, NULL);
711
712   if (proc_desc)
713     {
714       if (PROC_DESC_IS_DYN_SIGTRAMP (proc_desc))
715         return PROC_LOW_ADDR (proc_desc);       /* "prologue" is in kernel */
716
717       /* If function is frameless, then we need to do it the hard way.  I
718          strongly suspect that frameless always means prologueless... */
719       if (PROC_FRAME_REG (proc_desc) == SP_REGNUM
720           && PROC_FRAME_OFFSET (proc_desc) == 0)
721         return 0;
722     }
723
724   if (!find_pc_partial_function (pc, NULL, &func_addr, &func_end))
725     return 0;                   /* Unknown */
726
727   sal = find_pc_line (func_addr, 0);
728
729   if (sal.end < func_end)
730     return sal.end;
731
732   /* The line after the prologue is after the end of the function.  In this
733      case, tell the caller to find the prologue the hard way.  */
734
735   return 0;
736 }
737
738 /* Return non-zero if we *might* be in a function prologue.  Return zero if we
739    are definitively *not* in a function prologue.  */
740
741 static int
742 alpha_in_prologue (CORE_ADDR pc, alpha_extra_func_info_t proc_desc)
743 {
744   CORE_ADDR after_prologue_pc;
745
746   after_prologue_pc = after_prologue (pc, proc_desc);
747
748   if (after_prologue_pc == 0
749       || pc < after_prologue_pc)
750     return 1;
751   else
752     return 0;
753 }
754
755 static alpha_extra_func_info_t
756 find_proc_desc (CORE_ADDR pc, struct frame_info *next_frame)
757 {
758   alpha_extra_func_info_t proc_desc;
759   struct block *b;
760   struct symbol *sym;
761   CORE_ADDR startaddr;
762
763   /* Try to get the proc_desc from the linked call dummy proc_descs
764      if the pc is in the call dummy.
765      This is hairy. In the case of nested dummy calls we have to find the
766      right proc_desc, but we might not yet know the frame for the dummy
767      as it will be contained in the proc_desc we are searching for.
768      So we have to find the proc_desc whose frame is closest to the current
769      stack pointer.  */
770
771   if (PC_IN_CALL_DUMMY (pc, 0, 0))
772     {
773       struct linked_proc_info *link;
774       CORE_ADDR sp = read_next_frame_reg (next_frame, SP_REGNUM);
775       alpha_extra_func_info_t found_proc_desc = NULL;
776       long min_distance = LONG_MAX;
777
778       for (link = linked_proc_desc_table; link; link = link->next)
779         {
780           long distance = (CORE_ADDR) PROC_DUMMY_FRAME (&link->info) - sp;
781           if (distance > 0 && distance < min_distance)
782             {
783               min_distance = distance;
784               found_proc_desc = &link->info;
785             }
786         }
787       if (found_proc_desc != NULL)
788         return found_proc_desc;
789     }
790
791   b = block_for_pc (pc);
792
793   find_pc_partial_function (pc, NULL, &startaddr, NULL);
794   if (b == NULL)
795     sym = NULL;
796   else
797     {
798       if (startaddr > BLOCK_START (b))
799         /* This is the "pathological" case referred to in a comment in
800            print_frame_info.  It might be better to move this check into
801            symbol reading.  */
802         sym = NULL;
803       else
804         sym = lookup_symbol (MIPS_EFI_SYMBOL_NAME, b, LABEL_NAMESPACE,
805                              0, NULL);
806     }
807
808   /* If we never found a PDR for this function in symbol reading, then
809      examine prologues to find the information.  */
810   if (sym && ((mips_extra_func_info_t) SYMBOL_VALUE (sym))->pdr.framereg == -1)
811     sym = NULL;
812
813   if (sym)
814     {
815       /* IF this is the topmost frame AND
816        * (this proc does not have debugging information OR
817        * the PC is in the procedure prologue)
818        * THEN create a "heuristic" proc_desc (by analyzing
819        * the actual code) to replace the "official" proc_desc.
820        */
821       proc_desc = (alpha_extra_func_info_t) SYMBOL_VALUE (sym);
822       if (next_frame == NULL)
823         {
824           if (PROC_DESC_IS_DUMMY (proc_desc) || alpha_in_prologue (pc, proc_desc))
825             {
826               alpha_extra_func_info_t found_heuristic =
827               heuristic_proc_desc (PROC_LOW_ADDR (proc_desc),
828                                    pc, next_frame);
829               if (found_heuristic)
830                 {
831                   PROC_LOCALOFF (found_heuristic) =
832                     PROC_LOCALOFF (proc_desc);
833                   PROC_PC_REG (found_heuristic) = PROC_PC_REG (proc_desc);
834                   proc_desc = found_heuristic;
835                 }
836             }
837         }
838     }
839   else
840     {
841       long offset;
842
843       /* Is linked_proc_desc_table really necessary?  It only seems to be used
844          by procedure call dummys.  However, the procedures being called ought
845          to have their own proc_descs, and even if they don't,
846          heuristic_proc_desc knows how to create them! */
847
848       register struct linked_proc_info *link;
849       for (link = linked_proc_desc_table; link; link = link->next)
850         if (PROC_LOW_ADDR (&link->info) <= pc
851             && PROC_HIGH_ADDR (&link->info) > pc)
852           return &link->info;
853
854       /* If PC is inside a dynamically generated sigtramp handler,
855          create and push a procedure descriptor for that code: */
856       offset = DYNAMIC_SIGTRAMP_OFFSET (pc);
857       if (offset >= 0)
858         return push_sigtramp_desc (pc - offset);
859
860       /* If heuristic_fence_post is non-zero, determine the procedure
861          start address by examining the instructions.
862          This allows us to find the start address of static functions which
863          have no symbolic information, as startaddr would have been set to
864          the preceding global function start address by the
865          find_pc_partial_function call above.  */
866       if (startaddr == 0 || heuristic_fence_post != 0)
867         startaddr = heuristic_proc_start (pc);
868
869       proc_desc =
870         heuristic_proc_desc (startaddr, pc, next_frame);
871     }
872   return proc_desc;
873 }
874
875 alpha_extra_func_info_t cached_proc_desc;
876
877 CORE_ADDR
878 alpha_frame_chain (struct frame_info *frame)
879 {
880   alpha_extra_func_info_t proc_desc;
881   CORE_ADDR saved_pc = FRAME_SAVED_PC (frame);
882
883   if (saved_pc == 0 || inside_entry_file (saved_pc))
884     return 0;
885
886   proc_desc = find_proc_desc (saved_pc, frame);
887   if (!proc_desc)
888     return 0;
889
890   cached_proc_desc = proc_desc;
891
892   /* Fetch the frame pointer for a dummy frame from the procedure
893      descriptor.  */
894   if (PROC_DESC_IS_DUMMY (proc_desc))
895     return (CORE_ADDR) PROC_DUMMY_FRAME (proc_desc);
896
897   /* If no frame pointer and frame size is zero, we must be at end
898      of stack (or otherwise hosed).  If we don't check frame size,
899      we loop forever if we see a zero size frame.  */
900   if (PROC_FRAME_REG (proc_desc) == SP_REGNUM
901       && PROC_FRAME_OFFSET (proc_desc) == 0
902   /* The previous frame from a sigtramp frame might be frameless
903      and have frame size zero.  */
904       && !frame->signal_handler_caller)
905     return FRAME_PAST_SIGTRAMP_FRAME (frame, saved_pc);
906   else
907     return read_next_frame_reg (frame, PROC_FRAME_REG (proc_desc))
908       + PROC_FRAME_OFFSET (proc_desc);
909 }
910
911 void
912 alpha_print_extra_frame_info (struct frame_info *fi)
913 {
914   if (fi
915       && fi->extra_info
916       && fi->extra_info->proc_desc
917       && fi->extra_info->proc_desc->pdr.framereg < NUM_REGS)
918     printf_filtered (" frame pointer is at %s+%s\n",
919                      REGISTER_NAME (fi->extra_info->proc_desc->pdr.framereg),
920                      paddr_d (fi->extra_info->proc_desc->pdr.frameoffset));
921 }
922
923 void
924 alpha_init_extra_frame_info (int fromleaf, struct frame_info *frame)
925 {
926   /* Use proc_desc calculated in frame_chain */
927   alpha_extra_func_info_t proc_desc =
928   frame->next ? cached_proc_desc : find_proc_desc (frame->pc, frame->next);
929
930   frame->extra_info = (struct frame_extra_info *)
931     frame_obstack_alloc (sizeof (struct frame_extra_info));
932
933   frame->saved_regs = NULL;
934   frame->extra_info->localoff = 0;
935   frame->extra_info->pc_reg = RA_REGNUM;
936   frame->extra_info->proc_desc = proc_desc == &temp_proc_desc ? 0 : proc_desc;
937   if (proc_desc)
938     {
939       /* Get the locals offset and the saved pc register from the
940          procedure descriptor, they are valid even if we are in the
941          middle of the prologue.  */
942       frame->extra_info->localoff = PROC_LOCALOFF (proc_desc);
943       frame->extra_info->pc_reg = PROC_PC_REG (proc_desc);
944
945       /* Fixup frame-pointer - only needed for top frame */
946
947       /* Fetch the frame pointer for a dummy frame from the procedure
948          descriptor.  */
949       if (PROC_DESC_IS_DUMMY (proc_desc))
950         frame->frame = (CORE_ADDR) PROC_DUMMY_FRAME (proc_desc);
951
952       /* This may not be quite right, if proc has a real frame register.
953          Get the value of the frame relative sp, procedure might have been
954          interrupted by a signal at it's very start.  */
955       else if (frame->pc == PROC_LOW_ADDR (proc_desc)
956                && !PROC_DESC_IS_DYN_SIGTRAMP (proc_desc))
957         frame->frame = read_next_frame_reg (frame->next, SP_REGNUM);
958       else
959         frame->frame = read_next_frame_reg (frame->next, PROC_FRAME_REG (proc_desc))
960           + PROC_FRAME_OFFSET (proc_desc);
961
962       if (proc_desc == &temp_proc_desc)
963         {
964           char *name;
965
966           /* Do not set the saved registers for a sigtramp frame,
967              alpha_find_saved_registers will do that for us.
968              We can't use frame->signal_handler_caller, it is not yet set.  */
969           find_pc_partial_function (frame->pc, &name,
970                                     (CORE_ADDR *) NULL, (CORE_ADDR *) NULL);
971           if (!IN_SIGTRAMP (frame->pc, name))
972             {
973               frame->saved_regs = (CORE_ADDR *)
974                 frame_obstack_alloc (SIZEOF_FRAME_SAVED_REGS);
975               memcpy (frame->saved_regs, temp_saved_regs,
976                       SIZEOF_FRAME_SAVED_REGS);
977               frame->saved_regs[PC_REGNUM]
978                 = frame->saved_regs[RA_REGNUM];
979             }
980         }
981     }
982 }
983
984 CORE_ADDR
985 alpha_frame_locals_address (struct frame_info *fi)
986 {
987   return (fi->frame - fi->extra_info->localoff);
988 }
989
990 CORE_ADDR
991 alpha_frame_args_address (struct frame_info *fi)
992 {
993   return (fi->frame - (ALPHA_NUM_ARG_REGS * 8));
994 }
995
996 /* ALPHA stack frames are almost impenetrable.  When execution stops,
997    we basically have to look at symbol information for the function
998    that we stopped in, which tells us *which* register (if any) is
999    the base of the frame pointer, and what offset from that register
1000    the frame itself is at.  
1001
1002    This presents a problem when trying to examine a stack in memory
1003    (that isn't executing at the moment), using the "frame" command.  We
1004    don't have a PC, nor do we have any registers except SP.
1005
1006    This routine takes two arguments, SP and PC, and tries to make the
1007    cached frames look as if these two arguments defined a frame on the
1008    cache.  This allows the rest of info frame to extract the important
1009    arguments without difficulty.  */
1010
1011 struct frame_info *
1012 setup_arbitrary_frame (int argc, CORE_ADDR *argv)
1013 {
1014   if (argc != 2)
1015     error ("ALPHA frame specifications require two arguments: sp and pc");
1016
1017   return create_new_frame (argv[0], argv[1]);
1018 }
1019
1020 /* The alpha passes the first six arguments in the registers, the rest on
1021    the stack. The register arguments are eventually transferred to the
1022    argument transfer area immediately below the stack by the called function
1023    anyway. So we `push' at least six arguments on the stack, `reload' the
1024    argument registers and then adjust the stack pointer to point past the
1025    sixth argument. This algorithm simplifies the passing of a large struct
1026    which extends from the registers to the stack.
1027    If the called function is returning a structure, the address of the
1028    structure to be returned is passed as a hidden first argument.  */
1029
1030 CORE_ADDR
1031 alpha_push_arguments (int nargs, struct value **args, CORE_ADDR sp,
1032                       int struct_return, CORE_ADDR struct_addr)
1033 {
1034   int i;
1035   int accumulate_size = struct_return ? 8 : 0;
1036   int arg_regs_size = ALPHA_NUM_ARG_REGS * 8;
1037   struct alpha_arg
1038     {
1039       char *contents;
1040       int len;
1041       int offset;
1042     };
1043   struct alpha_arg *alpha_args =
1044   (struct alpha_arg *) alloca (nargs * sizeof (struct alpha_arg));
1045   register struct alpha_arg *m_arg;
1046   char raw_buffer[sizeof (CORE_ADDR)];
1047   int required_arg_regs;
1048
1049   for (i = 0, m_arg = alpha_args; i < nargs; i++, m_arg++)
1050     {
1051       struct value *arg = args[i];
1052       struct type *arg_type = check_typedef (VALUE_TYPE (arg));
1053       /* Cast argument to long if necessary as the compiler does it too.  */
1054       switch (TYPE_CODE (arg_type))
1055         {
1056         case TYPE_CODE_INT:
1057         case TYPE_CODE_BOOL:
1058         case TYPE_CODE_CHAR:
1059         case TYPE_CODE_RANGE:
1060         case TYPE_CODE_ENUM:
1061           if (TYPE_LENGTH (arg_type) < TYPE_LENGTH (builtin_type_long))
1062             {
1063               arg_type = builtin_type_long;
1064               arg = value_cast (arg_type, arg);
1065             }
1066           break;
1067         default:
1068           break;
1069         }
1070       m_arg->len = TYPE_LENGTH (arg_type);
1071       m_arg->offset = accumulate_size;
1072       accumulate_size = (accumulate_size + m_arg->len + 7) & ~7;
1073       m_arg->contents = VALUE_CONTENTS (arg);
1074     }
1075
1076   /* Determine required argument register loads, loading an argument register
1077      is expensive as it uses three ptrace calls.  */
1078   required_arg_regs = accumulate_size / 8;
1079   if (required_arg_regs > ALPHA_NUM_ARG_REGS)
1080     required_arg_regs = ALPHA_NUM_ARG_REGS;
1081
1082   /* Make room for the arguments on the stack.  */
1083   if (accumulate_size < arg_regs_size)
1084     accumulate_size = arg_regs_size;
1085   sp -= accumulate_size;
1086
1087   /* Keep sp aligned to a multiple of 16 as the compiler does it too.  */
1088   sp &= ~15;
1089
1090   /* `Push' arguments on the stack.  */
1091   for (i = nargs; m_arg--, --i >= 0;)
1092     write_memory (sp + m_arg->offset, m_arg->contents, m_arg->len);
1093   if (struct_return)
1094     {
1095       store_address (raw_buffer, sizeof (CORE_ADDR), struct_addr);
1096       write_memory (sp, raw_buffer, sizeof (CORE_ADDR));
1097     }
1098
1099   /* Load the argument registers.  */
1100   for (i = 0; i < required_arg_regs; i++)
1101     {
1102       LONGEST val;
1103
1104       val = read_memory_integer (sp + i * 8, 8);
1105       write_register (A0_REGNUM + i, val);
1106       write_register (FPA0_REGNUM + i, val);
1107     }
1108
1109   return sp + arg_regs_size;
1110 }
1111
1112 void
1113 alpha_push_dummy_frame (void)
1114 {
1115   int ireg;
1116   struct linked_proc_info *link;
1117   alpha_extra_func_info_t proc_desc;
1118   CORE_ADDR sp = read_register (SP_REGNUM);
1119   CORE_ADDR save_address;
1120   char raw_buffer[MAX_REGISTER_RAW_SIZE];
1121   unsigned long mask;
1122
1123   link = (struct linked_proc_info *) xmalloc (sizeof (struct linked_proc_info));
1124   link->next = linked_proc_desc_table;
1125   linked_proc_desc_table = link;
1126
1127   proc_desc = &link->info;
1128
1129   /*
1130    * The registers we must save are all those not preserved across
1131    * procedure calls.
1132    * In addition, we must save the PC and RA.
1133    *
1134    * Dummy frame layout:
1135    *  (high memory)
1136    *    Saved PC
1137    *    Saved F30
1138    *    ...
1139    *    Saved F0
1140    *    Saved R29
1141    *    ...
1142    *    Saved R0
1143    *    Saved R26 (RA)
1144    *    Parameter build area
1145    *  (low memory)
1146    */
1147
1148 /* MASK(i,j) == (1<<i) + (1<<(i+1)) + ... + (1<<j)). Assume i<=j<31. */
1149 #define MASK(i,j) ((((LONGEST)1 << ((j)+1)) - 1) ^ (((LONGEST)1 << (i)) - 1))
1150 #define GEN_REG_SAVE_MASK (MASK(0,8) | MASK(16,29))
1151 #define GEN_REG_SAVE_COUNT 24
1152 #define FLOAT_REG_SAVE_MASK (MASK(0,1) | MASK(10,30))
1153 #define FLOAT_REG_SAVE_COUNT 23
1154   /* The special register is the PC as we have no bit for it in the save masks.
1155      alpha_frame_saved_pc knows where the pc is saved in a dummy frame.  */
1156 #define SPECIAL_REG_SAVE_COUNT 1
1157
1158   PROC_REG_MASK (proc_desc) = GEN_REG_SAVE_MASK;
1159   PROC_FREG_MASK (proc_desc) = FLOAT_REG_SAVE_MASK;
1160   /* PROC_REG_OFFSET is the offset from the dummy frame to the saved RA,
1161      but keep SP aligned to a multiple of 16.  */
1162   PROC_REG_OFFSET (proc_desc) =
1163     -((8 * (SPECIAL_REG_SAVE_COUNT
1164             + GEN_REG_SAVE_COUNT
1165             + FLOAT_REG_SAVE_COUNT)
1166        + 15) & ~15);
1167   PROC_FREG_OFFSET (proc_desc) =
1168     PROC_REG_OFFSET (proc_desc) + 8 * GEN_REG_SAVE_COUNT;
1169
1170   /* Save general registers.
1171      The return address register is the first saved register, all other
1172      registers follow in ascending order.
1173      The PC is saved immediately below the SP.  */
1174   save_address = sp + PROC_REG_OFFSET (proc_desc);
1175   store_address (raw_buffer, 8, read_register (RA_REGNUM));
1176   write_memory (save_address, raw_buffer, 8);
1177   save_address += 8;
1178   mask = PROC_REG_MASK (proc_desc) & 0xffffffffL;
1179   for (ireg = 0; mask; ireg++, mask >>= 1)
1180     if (mask & 1)
1181       {
1182         if (ireg == RA_REGNUM)
1183           continue;
1184         store_address (raw_buffer, 8, read_register (ireg));
1185         write_memory (save_address, raw_buffer, 8);
1186         save_address += 8;
1187       }
1188
1189   store_address (raw_buffer, 8, read_register (PC_REGNUM));
1190   write_memory (sp - 8, raw_buffer, 8);
1191
1192   /* Save floating point registers.  */
1193   save_address = sp + PROC_FREG_OFFSET (proc_desc);
1194   mask = PROC_FREG_MASK (proc_desc) & 0xffffffffL;
1195   for (ireg = 0; mask; ireg++, mask >>= 1)
1196     if (mask & 1)
1197       {
1198         store_address (raw_buffer, 8, read_register (ireg + FP0_REGNUM));
1199         write_memory (save_address, raw_buffer, 8);
1200         save_address += 8;
1201       }
1202
1203   /* Set and save the frame address for the dummy.  
1204      This is tricky. The only registers that are suitable for a frame save
1205      are those that are preserved across procedure calls (s0-s6). But if
1206      a read system call is interrupted and then a dummy call is made
1207      (see testsuite/gdb.t17/interrupt.exp) the dummy call hangs till the read
1208      is satisfied. Then it returns with the s0-s6 registers set to the values
1209      on entry to the read system call and our dummy frame pointer would be
1210      destroyed. So we save the dummy frame in the proc_desc and handle the
1211      retrieval of the frame pointer of a dummy specifically. The frame register
1212      is set to the virtual frame (pseudo) register, it's value will always
1213      be read as zero and will help us to catch any errors in the dummy frame
1214      retrieval code.  */
1215   PROC_DUMMY_FRAME (proc_desc) = sp;
1216   PROC_FRAME_REG (proc_desc) = FP_REGNUM;
1217   PROC_FRAME_OFFSET (proc_desc) = 0;
1218   sp += PROC_REG_OFFSET (proc_desc);
1219   write_register (SP_REGNUM, sp);
1220
1221   PROC_LOW_ADDR (proc_desc) = CALL_DUMMY_ADDRESS ();
1222   PROC_HIGH_ADDR (proc_desc) = PROC_LOW_ADDR (proc_desc) + 4;
1223
1224   SET_PROC_DESC_IS_DUMMY (proc_desc);
1225   PROC_PC_REG (proc_desc) = RA_REGNUM;
1226 }
1227
1228 void
1229 alpha_pop_frame (void)
1230 {
1231   register int regnum;
1232   struct frame_info *frame = get_current_frame ();
1233   CORE_ADDR new_sp = frame->frame;
1234
1235   alpha_extra_func_info_t proc_desc = frame->extra_info->proc_desc;
1236
1237   /* we need proc_desc to know how to restore the registers;
1238      if it is NULL, construct (a temporary) one */
1239   if (proc_desc == NULL)
1240     proc_desc = find_proc_desc (frame->pc, frame->next);
1241
1242   /* Question: should we copy this proc_desc and save it in
1243      frame->proc_desc?  If we do, who will free it?
1244      For now, we don't save a copy... */
1245
1246   write_register (PC_REGNUM, FRAME_SAVED_PC (frame));
1247   if (frame->saved_regs == NULL)
1248     alpha_find_saved_regs (frame);
1249   if (proc_desc)
1250     {
1251       for (regnum = 32; --regnum >= 0;)
1252         if (PROC_REG_MASK (proc_desc) & (1 << regnum))
1253           write_register (regnum,
1254                           read_memory_integer (frame->saved_regs[regnum],
1255                                                8));
1256       for (regnum = 32; --regnum >= 0;)
1257         if (PROC_FREG_MASK (proc_desc) & (1 << regnum))
1258           write_register (regnum + FP0_REGNUM,
1259            read_memory_integer (frame->saved_regs[regnum + FP0_REGNUM], 8));
1260     }
1261   write_register (SP_REGNUM, new_sp);
1262   flush_cached_frames ();
1263
1264   if (proc_desc && (PROC_DESC_IS_DUMMY (proc_desc)
1265                     || PROC_DESC_IS_DYN_SIGTRAMP (proc_desc)))
1266     {
1267       struct linked_proc_info *pi_ptr, *prev_ptr;
1268
1269       for (pi_ptr = linked_proc_desc_table, prev_ptr = NULL;
1270            pi_ptr != NULL;
1271            prev_ptr = pi_ptr, pi_ptr = pi_ptr->next)
1272         {
1273           if (&pi_ptr->info == proc_desc)
1274             break;
1275         }
1276
1277       if (pi_ptr == NULL)
1278         error ("Can't locate dummy extra frame info\n");
1279
1280       if (prev_ptr != NULL)
1281         prev_ptr->next = pi_ptr->next;
1282       else
1283         linked_proc_desc_table = pi_ptr->next;
1284
1285       xfree (pi_ptr);
1286     }
1287 }
1288 \f
1289 /* To skip prologues, I use this predicate.  Returns either PC itself
1290    if the code at PC does not look like a function prologue; otherwise
1291    returns an address that (if we're lucky) follows the prologue.  If
1292    LENIENT, then we must skip everything which is involved in setting
1293    up the frame (it's OK to skip more, just so long as we don't skip
1294    anything which might clobber the registers which are being saved.
1295    Currently we must not skip more on the alpha, but we might need the
1296    lenient stuff some day.  */
1297
1298 static CORE_ADDR
1299 alpha_skip_prologue_internal (CORE_ADDR pc, int lenient)
1300 {
1301   unsigned long inst;
1302   int offset;
1303   CORE_ADDR post_prologue_pc;
1304   char buf[4];
1305
1306 #ifdef GDB_TARGET_HAS_SHARED_LIBS
1307   /* Silently return the unaltered pc upon memory errors.
1308      This could happen on OSF/1 if decode_line_1 tries to skip the
1309      prologue for quickstarted shared library functions when the
1310      shared library is not yet mapped in.
1311      Reading target memory is slow over serial lines, so we perform
1312      this check only if the target has shared libraries.  */
1313   if (target_read_memory (pc, buf, 4))
1314     return pc;
1315 #endif
1316
1317   /* See if we can determine the end of the prologue via the symbol table.
1318      If so, then return either PC, or the PC after the prologue, whichever
1319      is greater.  */
1320
1321   post_prologue_pc = after_prologue (pc, NULL);
1322
1323   if (post_prologue_pc != 0)
1324     return max (pc, post_prologue_pc);
1325
1326   /* Can't determine prologue from the symbol table, need to examine
1327      instructions.  */
1328
1329   /* Skip the typical prologue instructions. These are the stack adjustment
1330      instruction and the instructions that save registers on the stack
1331      or in the gcc frame.  */
1332   for (offset = 0; offset < 100; offset += 4)
1333     {
1334       int status;
1335
1336       status = read_memory_nobpt (pc + offset, buf, 4);
1337       if (status)
1338         memory_error (status, pc + offset);
1339       inst = extract_unsigned_integer (buf, 4);
1340
1341       /* The alpha has no delay slots. But let's keep the lenient stuff,
1342          we might need it for something else in the future.  */
1343       if (lenient && 0)
1344         continue;
1345
1346       if ((inst & 0xffff0000) == 0x27bb0000)    /* ldah $gp,n($t12) */
1347         continue;
1348       if ((inst & 0xffff0000) == 0x23bd0000)    /* lda $gp,n($gp) */
1349         continue;
1350       if ((inst & 0xffff0000) == 0x23de0000)    /* lda $sp,n($sp) */
1351         continue;
1352       if ((inst & 0xffe01fff) == 0x43c0153e)    /* subq $sp,n,$sp */
1353         continue;
1354
1355       if ((inst & 0xfc1f0000) == 0xb41e0000
1356           && (inst & 0xffff0000) != 0xb7fe0000)
1357         continue;               /* stq reg,n($sp) */
1358       /* reg != $zero */
1359       if ((inst & 0xfc1f0000) == 0x9c1e0000
1360           && (inst & 0xffff0000) != 0x9ffe0000)
1361         continue;               /* stt reg,n($sp) */
1362       /* reg != $zero */
1363       if (inst == 0x47de040f)   /* bis sp,sp,fp */
1364         continue;
1365
1366       break;
1367     }
1368   return pc + offset;
1369 }
1370
1371 CORE_ADDR
1372 alpha_skip_prologue (CORE_ADDR addr)
1373 {
1374   return (alpha_skip_prologue_internal (addr, 0));
1375 }
1376
1377 #if 0
1378 /* Is address PC in the prologue (loosely defined) for function at
1379    STARTADDR?  */
1380
1381 static int
1382 alpha_in_lenient_prologue (CORE_ADDR startaddr, CORE_ADDR pc)
1383 {
1384   CORE_ADDR end_prologue = alpha_skip_prologue_internal (startaddr, 1);
1385   return pc >= startaddr && pc < end_prologue;
1386 }
1387 #endif
1388
1389 /* The alpha needs a conversion between register and memory format if
1390    the register is a floating point register and
1391    memory format is float, as the register format must be double
1392    or
1393    memory format is an integer with 4 bytes or less, as the representation
1394    of integers in floating point registers is different. */
1395 void
1396 alpha_register_convert_to_virtual (int regnum, struct type *valtype,
1397                                    char *raw_buffer, char *virtual_buffer)
1398 {
1399   if (TYPE_LENGTH (valtype) >= REGISTER_RAW_SIZE (regnum))
1400     {
1401       memcpy (virtual_buffer, raw_buffer, REGISTER_VIRTUAL_SIZE (regnum));
1402       return;
1403     }
1404
1405   if (TYPE_CODE (valtype) == TYPE_CODE_FLT)
1406     {
1407       double d = extract_floating (raw_buffer, REGISTER_RAW_SIZE (regnum));
1408       store_floating (virtual_buffer, TYPE_LENGTH (valtype), d);
1409     }
1410   else if (TYPE_CODE (valtype) == TYPE_CODE_INT && TYPE_LENGTH (valtype) <= 4)
1411     {
1412       ULONGEST l;
1413       l = extract_unsigned_integer (raw_buffer, REGISTER_RAW_SIZE (regnum));
1414       l = ((l >> 32) & 0xc0000000) | ((l >> 29) & 0x3fffffff);
1415       store_unsigned_integer (virtual_buffer, TYPE_LENGTH (valtype), l);
1416     }
1417   else
1418     error ("Cannot retrieve value from floating point register");
1419 }
1420
1421 void
1422 alpha_register_convert_to_raw (struct type *valtype, int regnum,
1423                                char *virtual_buffer, char *raw_buffer)
1424 {
1425   if (TYPE_LENGTH (valtype) >= REGISTER_RAW_SIZE (regnum))
1426     {
1427       memcpy (raw_buffer, virtual_buffer, REGISTER_RAW_SIZE (regnum));
1428       return;
1429     }
1430
1431   if (TYPE_CODE (valtype) == TYPE_CODE_FLT)
1432     {
1433       double d = extract_floating (virtual_buffer, TYPE_LENGTH (valtype));
1434       store_floating (raw_buffer, REGISTER_RAW_SIZE (regnum), d);
1435     }
1436   else if (TYPE_CODE (valtype) == TYPE_CODE_INT && TYPE_LENGTH (valtype) <= 4)
1437     {
1438       ULONGEST l;
1439       if (TYPE_UNSIGNED (valtype))
1440         l = extract_unsigned_integer (virtual_buffer, TYPE_LENGTH (valtype));
1441       else
1442         l = extract_signed_integer (virtual_buffer, TYPE_LENGTH (valtype));
1443       l = ((l & 0xc0000000) << 32) | ((l & 0x3fffffff) << 29);
1444       store_unsigned_integer (raw_buffer, REGISTER_RAW_SIZE (regnum), l);
1445     }
1446   else
1447     error ("Cannot store value in floating point register");
1448 }
1449
1450 /* Given a return value in `regbuf' with a type `valtype', 
1451    extract and copy its value into `valbuf'.  */
1452
1453 void
1454 alpha_extract_return_value (struct type *valtype,
1455                             char regbuf[REGISTER_BYTES], char *valbuf)
1456 {
1457   if (TYPE_CODE (valtype) == TYPE_CODE_FLT)
1458     alpha_register_convert_to_virtual (FP0_REGNUM, valtype,
1459                                        regbuf + REGISTER_BYTE (FP0_REGNUM),
1460                                        valbuf);
1461   else
1462     memcpy (valbuf, regbuf + REGISTER_BYTE (V0_REGNUM), TYPE_LENGTH (valtype));
1463 }
1464
1465 /* Given a return value in `regbuf' with a type `valtype', 
1466    write its value into the appropriate register.  */
1467
1468 void
1469 alpha_store_return_value (struct type *valtype, char *valbuf)
1470 {
1471   char raw_buffer[MAX_REGISTER_RAW_SIZE];
1472   int regnum = V0_REGNUM;
1473   int length = TYPE_LENGTH (valtype);
1474
1475   if (TYPE_CODE (valtype) == TYPE_CODE_FLT)
1476     {
1477       regnum = FP0_REGNUM;
1478       length = REGISTER_RAW_SIZE (regnum);
1479       alpha_register_convert_to_raw (valtype, regnum, valbuf, raw_buffer);
1480     }
1481   else
1482     memcpy (raw_buffer, valbuf, length);
1483
1484   write_register_bytes (REGISTER_BYTE (regnum), raw_buffer, length);
1485 }
1486
1487 /* Just like reinit_frame_cache, but with the right arguments to be
1488    callable as an sfunc.  */
1489
1490 static void
1491 reinit_frame_cache_sfunc (char *args, int from_tty, struct cmd_list_element *c)
1492 {
1493   reinit_frame_cache ();
1494 }
1495
1496 /* This is the definition of CALL_DUMMY_ADDRESS.  It's a heuristic that is used
1497    to find a convenient place in the text segment to stick a breakpoint to
1498    detect the completion of a target function call (ala call_function_by_hand).
1499  */
1500
1501 CORE_ADDR
1502 alpha_call_dummy_address (void)
1503 {
1504   CORE_ADDR entry;
1505   struct minimal_symbol *sym;
1506
1507   entry = entry_point_address ();
1508
1509   if (entry != 0)
1510     return entry;
1511
1512   sym = lookup_minimal_symbol ("_Prelude", NULL, symfile_objfile);
1513
1514   if (!sym || MSYMBOL_TYPE (sym) != mst_text)
1515     return 0;
1516   else
1517     return SYMBOL_VALUE_ADDRESS (sym) + 4;
1518 }
1519
1520 void
1521 alpha_fix_call_dummy (char *dummy, CORE_ADDR pc, CORE_ADDR fun, int nargs,
1522                       struct value **args, struct type *type, int gcc_p)
1523 {
1524   CORE_ADDR bp_address = CALL_DUMMY_ADDRESS ();
1525
1526   if (bp_address == 0)
1527     error ("no place to put call");
1528   write_register (RA_REGNUM, bp_address);
1529   write_register (T12_REGNUM, fun);
1530 }
1531
1532 /* On the Alpha, the call dummy code is nevery copied to user space
1533    (see alpha_fix_call_dummy() above).  The contents of this do not
1534    matter.  */
1535 LONGEST alpha_call_dummy_words[] = { 0 };
1536
1537 int
1538 alpha_use_struct_convention (int gcc_p, struct type *type)
1539 {
1540   /* Structures are returned by ref in extra arg0.  */
1541   return 1;
1542 }
1543
1544 void
1545 alpha_store_struct_return (CORE_ADDR addr, CORE_ADDR sp)
1546 {
1547   /* Store the address of the place in which to copy the structure the
1548      subroutine will return.  Handled by alpha_push_arguments.  */
1549 }
1550
1551 CORE_ADDR
1552 alpha_extract_struct_value_address (char *regbuf)
1553 {
1554   return (extract_address (regbuf + REGISTER_BYTE (V0_REGNUM),
1555                            REGISTER_RAW_SIZE (V0_REGNUM)));
1556 }
1557
1558 /* alpha_software_single_step() is called just before we want to resume
1559    the inferior, if we want to single-step it but there is no hardware
1560    or kernel single-step support (NetBSD on Alpha, for example).  We find
1561    the target of the coming instruction and breakpoint it.
1562
1563    single_step is also called just after the inferior stops.  If we had
1564    set up a simulated single-step, we undo our damage.  */
1565
1566 static CORE_ADDR
1567 alpha_next_pc (CORE_ADDR pc)
1568 {
1569   unsigned int insn;
1570   unsigned int op;
1571   int offset;
1572   LONGEST rav;
1573
1574   insn = read_memory_unsigned_integer (pc, sizeof (insn));
1575
1576   /* Opcode is top 6 bits. */
1577   op = (insn >> 26) & 0x3f;
1578
1579   if (op == 0x1a)
1580     {
1581       /* Jump format: target PC is:
1582          RB & ~3  */
1583       return (read_register ((insn >> 16) & 0x1f) & ~3);
1584     }
1585
1586   if ((op & 0x30) == 0x30)
1587     {
1588       /* Branch format: target PC is:
1589          (new PC) + (4 * sext(displacement))  */
1590       if (op == 0x30 ||         /* BR */
1591           op == 0x34)           /* BSR */
1592         {
1593  branch_taken:
1594           offset = (insn & 0x001fffff);
1595           if (offset & 0x00100000)
1596             offset  |= 0xffe00000;
1597           offset *= 4;
1598           return (pc + 4 + offset);
1599         }
1600
1601       /* Need to determine if branch is taken; read RA.  */
1602       rav = (LONGEST) read_register ((insn >> 21) & 0x1f);
1603       switch (op)
1604         {
1605         case 0x38:              /* BLBC */
1606           if ((rav & 1) == 0)
1607             goto branch_taken;
1608           break;
1609         case 0x3c:              /* BLBS */
1610           if (rav & 1)
1611             goto branch_taken;
1612           break;
1613         case 0x39:              /* BEQ */
1614           if (rav == 0)
1615             goto branch_taken;
1616           break;
1617         case 0x3d:              /* BNE */
1618           if (rav != 0)
1619             goto branch_taken;
1620           break;
1621         case 0x3a:              /* BLT */
1622           if (rav < 0)
1623             goto branch_taken;
1624           break;
1625         case 0x3b:              /* BLE */
1626           if (rav <= 0)
1627             goto branch_taken;
1628           break;
1629         case 0x3f:              /* BGT */
1630           if (rav > 0)
1631             goto branch_taken;
1632           break;
1633         case 0x3e:              /* BGE */
1634           if (rav >= 0)
1635             goto branch_taken;
1636           break;
1637         }
1638     }
1639
1640   /* Not a branch or branch not taken; target PC is:
1641      pc + 4  */
1642   return (pc + 4);
1643 }
1644
1645 void
1646 alpha_software_single_step (enum target_signal sig, int insert_breakpoints_p)
1647 {
1648   static CORE_ADDR next_pc;
1649   typedef char binsn_quantum[BREAKPOINT_MAX];
1650   static binsn_quantum break_mem;
1651   CORE_ADDR pc;
1652
1653   if (insert_breakpoints_p)
1654     {
1655       pc = read_pc ();
1656       next_pc = alpha_next_pc (pc);
1657
1658       target_insert_breakpoint (next_pc, break_mem);
1659     }
1660   else
1661     {
1662       target_remove_breakpoint (next_pc, break_mem);
1663       write_pc (next_pc);
1664     }
1665 }
1666
1667 void
1668 _initialize_alpha_tdep (void)
1669 {
1670   struct cmd_list_element *c;
1671
1672   tm_print_insn = print_insn_alpha;
1673
1674   /* Let the user set the fence post for heuristic_proc_start.  */
1675
1676   /* We really would like to have both "0" and "unlimited" work, but
1677      command.c doesn't deal with that.  So make it a var_zinteger
1678      because the user can always use "999999" or some such for unlimited.  */
1679   c = add_set_cmd ("heuristic-fence-post", class_support, var_zinteger,
1680                    (char *) &heuristic_fence_post,
1681                    "\
1682 Set the distance searched for the start of a function.\n\
1683 If you are debugging a stripped executable, GDB needs to search through the\n\
1684 program for the start of a function.  This command sets the distance of the\n\
1685 search.  The only need to set it is when debugging a stripped executable.",
1686                    &setlist);
1687   /* We need to throw away the frame cache when we set this, since it
1688      might change our ability to get backtraces.  */
1689   c->function.sfunc = reinit_frame_cache_sfunc;
1690   add_show_from_set (c, &showlist);
1691 }