OSDN Git Service

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