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