OSDN Git Service

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