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