OSDN Git Service

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