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