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