* mdebugread.c (parse_procedure): Remove _sigtramp kludges for
[platform/upstream/binutils.git] / gdb / alpha-tdep.c
1 /* Target-dependent code for the ALPHA architecture, for GDB, the GNU Debugger.
2    Copyright 1993, 1994 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., 675 Mass Ave, Cambridge, MA 02139, USA.  */
19
20 #include "defs.h"
21 #include "frame.h"
22 #include "inferior.h"
23 #include "symtab.h"
24 #include "value.h"
25 #include "gdbcmd.h"
26 #include "gdbcore.h"
27 #include "dis-asm.h"
28
29 /* FIXME: Some of this code should perhaps be merged with mips-tdep.c.  */
30
31 #define VM_MIN_ADDRESS (CORE_ADDR)0x120000000
32 \f
33
34 /* Forward declarations.  */
35
36 static CORE_ADDR
37 read_next_frame_reg PARAMS ((FRAME, int));
38
39 static CORE_ADDR
40 heuristic_proc_start PARAMS ((CORE_ADDR));
41
42 static alpha_extra_func_info_t
43 heuristic_proc_desc PARAMS ((CORE_ADDR, CORE_ADDR, FRAME));
44
45 static alpha_extra_func_info_t
46 find_proc_desc PARAMS ((CORE_ADDR, FRAME));
47
48 static int
49 alpha_in_lenient_prologue PARAMS ((CORE_ADDR, CORE_ADDR));
50
51 static void
52 reinit_frame_cache_sfunc PARAMS ((char *, int, struct cmd_list_element *));
53
54 /* Heuristic_proc_start may hunt through the text section for a long
55    time across a 2400 baud serial line.  Allows the user to limit this
56    search.  */
57 static unsigned int heuristic_fence_post = 0;
58
59 /* Layout of a stack frame on the alpha:
60
61                 |                               |
62  pdr members:   |  7th ... nth arg,             |
63                 |  `pushed' by caller.          |
64                 |                               |
65 ----------------|-------------------------------|<--  old_sp == vfp
66    ^  ^  ^  ^   |                               |
67    |  |  |  |   |                               |
68    |  |localoff |  Copies of 1st .. 6th         |
69    |  |  |  |   |  argument if necessary.       |
70    |  |  |  v   |                               |
71    |  |  |  --- |-------------------------------|<-- FRAME_LOCALS_ADDRESS
72    |  |  |      |                               |
73    |  |  |      |  Locals and temporaries.      |
74    |  |  |      |                               |
75    |  |  |      |-------------------------------|
76    |  |  |      |                               |
77    |-fregoffset |  Saved float registers.       |
78    |  |  |      |  F9                           |
79    |  |  |      |   .                           |
80    |  |  |      |   .                           |
81    |  |  |      |  F2                           |
82    |  |  v      |                               |
83    |  |  -------|-------------------------------|
84    |  |         |                               |
85    |  |         |  Saved registers.             |
86    |  |         |  S6                           |
87    |-regoffset  |   .                           |
88    |  |         |   .                           |
89    |  |         |  S0                           |
90    |  |         |  pdr.pcreg                    |
91    |  v         |                               |
92    |  ----------|-------------------------------|
93    |            |                               |
94  frameoffset    |  Argument build area, gets    |
95    |            |  7th ... nth arg for any      |
96    |            |  called procedure.            |
97    v            |                               |
98    -------------|-------------------------------|<-- sp
99                 |                               |
100 */
101
102 #define PROC_LOW_ADDR(proc) ((proc)->pdr.adr) /* least address */
103 #define PROC_HIGH_ADDR(proc) ((proc)->pdr.iline) /* upper address bound */
104 #define PROC_DUMMY_FRAME(proc) ((proc)->pdr.iopt) /* frame for CALL_DUMMY */
105 #define PROC_FRAME_OFFSET(proc) ((proc)->pdr.frameoffset)
106 #define PROC_FRAME_REG(proc) ((proc)->pdr.framereg)
107 #define PROC_REG_MASK(proc) ((proc)->pdr.regmask)
108 #define PROC_FREG_MASK(proc) ((proc)->pdr.fregmask)
109 #define PROC_REG_OFFSET(proc) ((proc)->pdr.regoffset)
110 #define PROC_FREG_OFFSET(proc) ((proc)->pdr.fregoffset)
111 #define PROC_PC_REG(proc) ((proc)->pdr.pcreg)
112 #define PROC_LOCALOFF(proc) ((proc)->pdr.localoff)
113 #define PROC_SYMBOL(proc) (*(struct symbol**)&(proc)->pdr.isym)
114 #define _PROC_MAGIC_ 0x0F0F0F0F
115 #define PROC_DESC_IS_DUMMY(proc) ((proc)->pdr.isym == _PROC_MAGIC_)
116 #define SET_PROC_DESC_IS_DUMMY(proc) ((proc)->pdr.isym = _PROC_MAGIC_)
117
118 struct linked_proc_info
119 {
120   struct alpha_extra_func_info info;
121   struct linked_proc_info *next;
122 } *linked_proc_desc_table = NULL;
123
124 \f
125 #define READ_FRAME_REG(fi, regno) read_next_frame_reg((fi)->next, regno)
126
127 static CORE_ADDR
128 read_next_frame_reg(fi, regno)
129      FRAME fi;
130      int regno;
131 {
132   /* If it is the frame for sigtramp we have a pointer to the sigcontext
133      on the stack.
134      If the stack layout for __sigtramp changes or if sigcontext offsets
135      change we might have to update this code.  */
136 #ifndef SIGFRAME_PC_OFF
137 #define SIGFRAME_PC_OFF         (2 * 8)
138 #define SIGFRAME_REGSAVE_OFF    (4 * 8)
139 #endif
140   for (; fi; fi = fi->next)
141     {
142       if (fi->signal_handler_caller)
143         {
144           int offset;
145           CORE_ADDR sigcontext_addr = read_memory_integer(fi->frame, 8);
146
147           if (regno == PC_REGNUM)
148             offset = SIGFRAME_PC_OFF;
149           else if (regno < 32)
150             offset = SIGFRAME_REGSAVE_OFF + regno * 8;
151           else
152             return 0;
153           return read_memory_integer(sigcontext_addr + offset, 8);
154         }
155       else if (regno == SP_REGNUM)
156         return fi->frame;
157       else if (fi->saved_regs->regs[regno])
158         return read_memory_integer(fi->saved_regs->regs[regno], 8);
159     }
160   return read_register(regno);
161 }
162
163 CORE_ADDR
164 alpha_frame_saved_pc(frame)
165      FRAME frame;
166 {
167   alpha_extra_func_info_t proc_desc = frame->proc_desc;
168   /* We have to get the saved pc from the sigcontext
169      if it is a signal handler frame.  */
170   int pcreg = frame->signal_handler_caller ? PC_REGNUM
171               : (proc_desc ? PROC_PC_REG(proc_desc) : RA_REGNUM);
172
173   if (proc_desc && PROC_DESC_IS_DUMMY(proc_desc))
174       return read_memory_integer(frame->frame - 8, 8);
175
176   return read_next_frame_reg(frame, pcreg);
177 }
178
179 CORE_ADDR
180 alpha_saved_pc_after_call (frame)
181      FRAME frame;
182 {
183   alpha_extra_func_info_t proc_desc = find_proc_desc (frame->pc, frame->next);
184   int pcreg = proc_desc ? PROC_PC_REG (proc_desc) : RA_REGNUM;
185
186   return read_register (pcreg);
187 }
188
189
190 static struct alpha_extra_func_info temp_proc_desc;
191 static struct frame_saved_regs temp_saved_regs;
192
193 /* This fencepost looks highly suspicious to me.  Removing it also
194    seems suspicious as it could affect remote debugging across serial
195    lines.  */
196
197 static CORE_ADDR
198 heuristic_proc_start(pc)
199     CORE_ADDR pc;
200 {
201     CORE_ADDR start_pc = pc;
202     CORE_ADDR fence = start_pc - heuristic_fence_post;
203
204     if (start_pc == 0)  return 0;
205
206     if (heuristic_fence_post == UINT_MAX
207         || fence < VM_MIN_ADDRESS)
208       fence = VM_MIN_ADDRESS;
209
210     /* search back for previous return */
211     for (start_pc -= 4; ; start_pc -= 4)
212         if (start_pc < fence)
213           {
214             /* It's not clear to me why we reach this point when
215                stop_soon_quietly, but with this test, at least we
216                don't print out warnings for every child forked (eg, on
217                decstation).  22apr93 rich@cygnus.com.  */
218             if (!stop_soon_quietly)
219               {
220                 static int blurb_printed = 0;
221
222                 if (fence == VM_MIN_ADDRESS)
223                   warning("Hit beginning of text section without finding");
224                 else
225                   warning("Hit heuristic-fence-post without finding");
226                 
227                 warning("enclosing function for address 0x%lx", pc);
228                 if (!blurb_printed)
229                   {
230                     printf_filtered ("\
231 This warning occurs if you are debugging a function without any symbols\n\
232 (for example, in a stripped executable).  In that case, you may wish to\n\
233 increase the size of the search with the `set heuristic-fence-post' command.\n\
234 \n\
235 Otherwise, you told GDB there was a function where there isn't one, or\n\
236 (more likely) you have encountered a bug in GDB.\n");
237                     blurb_printed = 1;
238                   }
239               }
240
241             return 0; 
242           }
243         else if (ABOUT_TO_RETURN(start_pc))
244             break;
245
246     start_pc += 4; /* skip return */
247     return start_pc;
248 }
249
250 static alpha_extra_func_info_t
251 heuristic_proc_desc(start_pc, limit_pc, next_frame)
252     CORE_ADDR start_pc, limit_pc;
253     FRAME next_frame;
254 {
255     CORE_ADDR sp = next_frame ? next_frame->frame : read_register (SP_REGNUM);
256     CORE_ADDR cur_pc;
257     int frame_size;
258     int has_frame_reg = 0;
259     unsigned long reg_mask = 0;
260
261     if (start_pc == 0)
262       return NULL;
263     memset(&temp_proc_desc, '\0', sizeof(temp_proc_desc));
264     memset(&temp_saved_regs, '\0', sizeof(struct frame_saved_regs));
265     PROC_LOW_ADDR(&temp_proc_desc) = start_pc;
266
267     if (start_pc + 200 < limit_pc)
268       limit_pc = start_pc + 200;
269     frame_size = 0;
270     for (cur_pc = start_pc; cur_pc < limit_pc; cur_pc += 4)
271       {
272         char buf[4];
273         unsigned long word;
274         int status;
275
276         status = read_memory_nobpt (cur_pc, buf, 4); 
277         if (status)
278           memory_error (status, cur_pc);
279         word = extract_unsigned_integer (buf, 4);
280
281         if ((word & 0xffff0000) == 0x23de0000)          /* lda $sp,n($sp) */
282           frame_size += (-word) & 0xffff;
283         else if ((word & 0xfc1f0000) == 0xb41e0000      /* stq reg,n($sp) */
284                  && (word & 0xffff0000) != 0xb7fe0000)  /* reg != $zero */
285           {
286             int reg = (word & 0x03e00000) >> 21;
287             reg_mask |= 1 << reg;
288             temp_saved_regs.regs[reg] = sp + (short)word;
289           }
290         else if (word == 0x47de040f)                    /* bis sp,sp fp */
291           has_frame_reg = 1;
292       }
293     if (has_frame_reg)
294       PROC_FRAME_REG(&temp_proc_desc) = GCC_FP_REGNUM;
295     else
296       PROC_FRAME_REG(&temp_proc_desc) = SP_REGNUM;
297     PROC_FRAME_OFFSET(&temp_proc_desc) = frame_size;
298     PROC_REG_MASK(&temp_proc_desc) = reg_mask;
299     PROC_PC_REG(&temp_proc_desc) = RA_REGNUM;
300     return &temp_proc_desc;
301 }
302
303 static alpha_extra_func_info_t
304 find_proc_desc(pc, next_frame)
305     CORE_ADDR pc;
306     FRAME next_frame;
307 {
308   alpha_extra_func_info_t proc_desc;
309   struct block *b;
310   struct symbol *sym;
311   CORE_ADDR startaddr;
312
313   /* Try to get the proc_desc from the linked call dummy proc_descs
314      if the pc is in the call dummy.
315      This is hairy. In the case of nested dummy calls we have to find the
316      right proc_desc, but we might not yet know the frame for the dummy
317      as it will be contained in the proc_desc we are searching for.
318      So we have to find the proc_desc whose frame is closest to the current
319      stack pointer.  */
320   if (PC_IN_CALL_DUMMY (pc, 0, 0))
321     {
322       struct linked_proc_info *link;
323       CORE_ADDR sp = next_frame ? next_frame->frame : read_register (SP_REGNUM);
324       alpha_extra_func_info_t found_proc_desc = NULL;
325       long min_distance = LONG_MAX;
326
327       for (link = linked_proc_desc_table; link; link = link->next)
328         {
329           long distance = (CORE_ADDR) PROC_DUMMY_FRAME (&link->info) - sp;
330           if (distance > 0 && distance < min_distance)
331             {
332               min_distance = distance;
333               found_proc_desc = &link->info;
334             }
335         }
336       if (found_proc_desc != NULL)
337         return found_proc_desc;
338     }
339
340   b = block_for_pc(pc);
341   find_pc_partial_function (pc, NULL, &startaddr, NULL);
342   if (b == NULL)
343     sym = NULL;
344   else
345     {
346       if (startaddr > BLOCK_START (b))
347         /* This is the "pathological" case referred to in a comment in
348            print_frame_info.  It might be better to move this check into
349            symbol reading.  */
350         sym = NULL;
351       else
352         sym = lookup_symbol (MIPS_EFI_SYMBOL_NAME, b, LABEL_NAMESPACE,
353                              0, NULL);
354     }
355
356   if (sym)
357     {
358         /* IF (this is the topmost frame OR a frame interrupted by a signal)
359          * AND (this proc does not have debugging information OR
360          * the PC is in the procedure prologue)
361          * THEN create a "heuristic" proc_desc (by analyzing
362          * the actual code) to replace the "official" proc_desc.
363          */
364         proc_desc = (alpha_extra_func_info_t)SYMBOL_VALUE(sym);
365         if (next_frame == NULL || next_frame->signal_handler_caller) {
366             struct symtab_and_line val;
367             struct symbol *proc_symbol =
368                 PROC_DESC_IS_DUMMY(proc_desc) ? 0 : PROC_SYMBOL(proc_desc);
369
370             if (proc_symbol) {
371                 val = find_pc_line (BLOCK_START
372                                     (SYMBOL_BLOCK_VALUE(proc_symbol)),
373                                     0);
374                 val.pc = val.end ? val.end : pc;
375             }
376             if (!proc_symbol || pc < val.pc) {
377                 alpha_extra_func_info_t found_heuristic =
378                     heuristic_proc_desc(PROC_LOW_ADDR(proc_desc),
379                                         pc, next_frame);
380                 if (found_heuristic)
381                   {
382                     /* The call to heuristic_proc_desc determines
383                        which registers have been saved so far and if the
384                        frame is already set up.
385                        The heuristic algorithm doesn't work well for other
386                        information in the procedure descriptor, so copy
387                        it from the found procedure descriptor.  */
388                     PROC_LOCALOFF(found_heuristic) = PROC_LOCALOFF(proc_desc);
389                     PROC_PC_REG(found_heuristic) = PROC_PC_REG(proc_desc);
390                     proc_desc = found_heuristic;
391                   }
392             }
393         }
394     }
395   else
396     {
397       if (startaddr == 0)
398         startaddr = heuristic_proc_start (pc);
399
400       proc_desc =
401         heuristic_proc_desc (startaddr, pc, next_frame);
402     }
403   return proc_desc;
404 }
405
406 alpha_extra_func_info_t cached_proc_desc;
407
408 FRAME_ADDR
409 alpha_frame_chain(frame)
410     FRAME frame;
411 {
412     alpha_extra_func_info_t proc_desc;
413     CORE_ADDR saved_pc = FRAME_SAVED_PC(frame);
414
415     if (saved_pc == 0 || inside_entry_file (saved_pc))
416       return 0;
417
418     proc_desc = find_proc_desc(saved_pc, frame);
419     if (!proc_desc)
420       return 0;
421
422     cached_proc_desc = proc_desc;
423
424     /* Fetch the frame pointer for a dummy frame from the procedure
425        descriptor.  */
426     if (PROC_DESC_IS_DUMMY(proc_desc))
427       return (FRAME_ADDR) PROC_DUMMY_FRAME(proc_desc);
428
429     /* If no frame pointer and frame size is zero, we must be at end
430        of stack (or otherwise hosed).  If we don't check frame size,
431        we loop forever if we see a zero size frame.  */
432     if (PROC_FRAME_REG (proc_desc) == SP_REGNUM
433         && PROC_FRAME_OFFSET (proc_desc) == 0
434         /* The previous frame from a sigtramp frame might be frameless
435            and have frame size zero.  */
436         && !frame->signal_handler_caller)
437       {
438         /* The alpha __sigtramp routine is frameless and has a frame size
439            of zero, but we are able to backtrace through it. */
440         char *name;
441         find_pc_partial_function (saved_pc, &name,
442                                   (CORE_ADDR *)NULL, (CORE_ADDR *)NULL);
443         if (IN_SIGTRAMP (saved_pc, name))
444           return frame->frame;
445         else
446           return 0;
447       }
448     else
449       return read_next_frame_reg(frame, PROC_FRAME_REG(proc_desc))
450              + PROC_FRAME_OFFSET(proc_desc);
451 }
452
453 void
454 init_extra_frame_info(fci)
455      struct frame_info *fci;
456 {
457   extern struct obstack frame_cache_obstack;
458   /* Use proc_desc calculated in frame_chain */
459   alpha_extra_func_info_t proc_desc =
460     fci->next ? cached_proc_desc : find_proc_desc(fci->pc, fci->next);
461
462   fci->saved_regs = (struct frame_saved_regs*)
463     obstack_alloc (&frame_cache_obstack, sizeof(struct frame_saved_regs));
464   memset (fci->saved_regs, 0, sizeof (struct frame_saved_regs));
465   fci->proc_desc =
466     proc_desc == &temp_proc_desc ? 0 : proc_desc;
467   if (proc_desc)
468     {
469       int ireg;
470       CORE_ADDR reg_position;
471       unsigned long mask;
472       int returnreg;
473
474       /* Get the locals offset from the procedure descriptor, it is valid
475          even if we are in the middle of the prologue.  */
476       fci->localoff = PROC_LOCALOFF(proc_desc);
477
478       /* Fixup frame-pointer - only needed for top frame */
479       /* Fetch the frame pointer for a dummy frame from the procedure
480          descriptor.  */
481       if (PROC_DESC_IS_DUMMY(proc_desc))
482         fci->frame = (FRAME_ADDR) PROC_DUMMY_FRAME(proc_desc);
483       /* This may not be quite right, if proc has a real frame register.
484          Get the value of the frame relative sp, procedure might have been
485          interrupted by a signal at it's very start.  */
486       else if (fci->pc == PROC_LOW_ADDR(proc_desc))
487         fci->frame = READ_FRAME_REG(fci, SP_REGNUM);
488       else
489         fci->frame = READ_FRAME_REG(fci, PROC_FRAME_REG(proc_desc))
490                       + PROC_FRAME_OFFSET(proc_desc);
491
492       /* If this is the innermost frame, and we are still in the
493          prologue (loosely defined), then the registers may not have
494          been saved yet.  */
495       if (fci->next == NULL
496           && !PROC_DESC_IS_DUMMY(proc_desc)
497           && alpha_in_lenient_prologue (PROC_LOW_ADDR (proc_desc), fci->pc))
498         {
499           /* Can't just say that the registers are not saved, because they
500              might get clobbered halfway through the prologue.
501              heuristic_proc_desc already has the right code to figure out
502              exactly what has been saved, so use it.  As far as I know we
503              could be doing this (as we do on the 68k, for example)
504              regardless of whether we are in the prologue; I'm leaving in
505              the check for being in the prologue only out of conservatism
506              (I'm not sure whether heuristic_proc_desc handles all cases,
507              for example).
508
509              This stuff is ugly (and getting uglier by the minute).  Probably
510              the best way to clean it up is to ignore the proc_desc's from
511              the symbols altogher, and get all the information we need by
512              examining the prologue (provided we can make the prologue
513              examining code good enough to get all the cases...).  */
514           proc_desc =
515             heuristic_proc_desc (PROC_LOW_ADDR (proc_desc),
516                                  fci->pc,
517                                  fci->next);
518         }
519
520       if (proc_desc == &temp_proc_desc)
521         *fci->saved_regs = temp_saved_regs;
522       else
523         {
524           /* Find which general-purpose registers were saved.
525              The return address register is the first saved register,
526              the other registers follow in ascending order.  */
527           reg_position = fci->frame + PROC_REG_OFFSET(proc_desc);
528           mask = PROC_REG_MASK(proc_desc) & 0xffffffffL;
529           returnreg = PROC_PC_REG(proc_desc);
530           if (mask & (1 << returnreg))
531             {
532               fci->saved_regs->regs[returnreg] = reg_position;
533               reg_position += 8;
534             }
535           for (ireg = 0; mask; ireg++, mask >>= 1)
536             if (mask & 1)
537               {
538                 if (ireg == returnreg)
539                   continue;
540                 fci->saved_regs->regs[ireg] = reg_position;
541                 reg_position += 8;
542               }
543           /* find which floating-point registers were saved */
544           reg_position = fci->frame + PROC_FREG_OFFSET(proc_desc);
545           mask = PROC_FREG_MASK(proc_desc) & 0xffffffffL;
546           for (ireg = 0; mask; ireg++, mask >>= 1)
547             if (mask & 1)
548               {
549                 fci->saved_regs->regs[FP0_REGNUM+ireg] = reg_position;
550                 reg_position += 8;
551               }
552         }
553
554       fci->saved_regs->regs[PC_REGNUM] = fci->saved_regs->regs[PROC_PC_REG(proc_desc)];
555     }
556 }
557
558 /* ALPHA stack frames are almost impenetrable.  When execution stops,
559    we basically have to look at symbol information for the function
560    that we stopped in, which tells us *which* register (if any) is
561    the base of the frame pointer, and what offset from that register
562    the frame itself is at.  
563
564    This presents a problem when trying to examine a stack in memory
565    (that isn't executing at the moment), using the "frame" command.  We
566    don't have a PC, nor do we have any registers except SP.
567
568    This routine takes two arguments, SP and PC, and tries to make the
569    cached frames look as if these two arguments defined a frame on the
570    cache.  This allows the rest of info frame to extract the important
571    arguments without difficulty.  */
572
573 FRAME
574 setup_arbitrary_frame (argc, argv)
575      int argc;
576      FRAME_ADDR *argv;
577 {
578   if (argc != 2)
579     error ("ALPHA frame specifications require two arguments: sp and pc");
580
581   return create_new_frame (argv[0], argv[1]);
582 }
583
584 /* The alpha passes the first six arguments in the registers, the rest on
585    the stack. The register arguments are eventually transferred to the
586    argument transfer area immediately below the stack by the called function
587    anyway. So we `push' at least six arguments on the stack, `reload' the
588    argument registers and then adjust the stack pointer to point past the
589    sixth argument. This algorithm simplifies the passing of a large struct
590    which extends from the registers to the stack.
591    If the called function is returning a structure, the address of the
592    structure to be returned is passed as a hidden first argument.  */
593
594 CORE_ADDR
595 alpha_push_arguments (nargs, args, sp, struct_return, struct_addr)
596   int nargs;
597   value *args;
598   CORE_ADDR sp;
599   int struct_return;
600   CORE_ADDR struct_addr;
601 {
602   register i;
603   int accumulate_size = struct_return ? 8 : 0;
604   int arg_regs_size = ALPHA_NUM_ARG_REGS * 8;
605   struct alpha_arg { char *contents; int len; int offset; };
606   struct alpha_arg *alpha_args =
607       (struct alpha_arg*)alloca (nargs * sizeof (struct alpha_arg));
608   register struct alpha_arg *m_arg;
609   char raw_buffer[sizeof (CORE_ADDR)];
610   int required_arg_regs;
611
612   for (i = 0, m_arg = alpha_args; i < nargs; i++, m_arg++)
613     {
614       value arg = value_arg_coerce (args[i]);
615       /* Cast argument to long if necessary as the compiler does it too.  */
616       if (TYPE_LENGTH (VALUE_TYPE (arg)) < TYPE_LENGTH (builtin_type_long))
617         arg = value_cast (builtin_type_long, arg);
618       m_arg->len = TYPE_LENGTH (VALUE_TYPE (arg));
619       m_arg->offset = accumulate_size;
620       accumulate_size = (accumulate_size + m_arg->len + 7) & ~7;
621       m_arg->contents = VALUE_CONTENTS(arg);
622     }
623
624   /* Determine required argument register loads, loading an argument register
625      is expensive as it uses three ptrace calls.  */
626   required_arg_regs = accumulate_size / 8;
627   if (required_arg_regs > ALPHA_NUM_ARG_REGS)
628     required_arg_regs = ALPHA_NUM_ARG_REGS;
629
630   /* Make room for the arguments on the stack.  */
631   if (accumulate_size < arg_regs_size)
632     accumulate_size = arg_regs_size; 
633   sp -= accumulate_size;
634
635   /* Keep sp aligned to a multiple of 16 as the compiler does it too.  */
636   sp &= ~15;
637
638   /* `Push' arguments on the stack.  */
639   for (i = nargs; m_arg--, --i >= 0; )
640     write_memory(sp + m_arg->offset, m_arg->contents, m_arg->len);
641   if (struct_return)
642     {
643       store_address (raw_buffer, sizeof (CORE_ADDR), struct_addr);
644       write_memory (sp, raw_buffer, sizeof (CORE_ADDR));
645     }
646
647   /* Load the argument registers.  */
648   for (i = 0; i < required_arg_regs; i++)
649     {
650       LONGEST val;
651
652       val = read_memory_integer (sp + i * 8, 8);
653       write_register (A0_REGNUM + i, val);
654       write_register (FPA0_REGNUM + i, val);
655     }
656
657   return sp + arg_regs_size;
658 }
659
660 void
661 alpha_push_dummy_frame()
662 {
663   int ireg;
664   struct linked_proc_info *link = (struct linked_proc_info*)
665       xmalloc(sizeof (struct linked_proc_info));
666   alpha_extra_func_info_t proc_desc = &link->info;
667   CORE_ADDR sp = read_register (SP_REGNUM);
668   CORE_ADDR save_address;
669   char raw_buffer[MAX_REGISTER_RAW_SIZE];
670   unsigned long mask;
671
672   link->next = linked_proc_desc_table;
673   linked_proc_desc_table = link;
674
675   /*
676    * The registers we must save are all those not preserved across
677    * procedure calls.
678    * In addition, we must save the PC and RA.
679    *
680    * Dummy frame layout:
681    *  (high memory)
682    *    Saved PC
683    *    Saved F30
684    *    ...
685    *    Saved F0
686    *    Saved R29
687    *    ...
688    *    Saved R0
689    *    Saved R26 (RA)
690    *    Parameter build area
691    *  (low memory)
692    */
693
694 /* MASK(i,j) == (1<<i) + (1<<(i+1)) + ... + (1<<j)). Assume i<=j<31. */
695 #define MASK(i,j) (((1L << ((j)+1)) - 1) ^ ((1L << (i)) - 1))
696 #define GEN_REG_SAVE_MASK (MASK(0,8) | MASK(16,29))
697 #define GEN_REG_SAVE_COUNT 24
698 #define FLOAT_REG_SAVE_MASK (MASK(0,1) | MASK(10,30))
699 #define FLOAT_REG_SAVE_COUNT 23
700   /* The special register is the PC as we have no bit for it in the save masks.
701      alpha_frame_saved_pc knows where the pc is saved in a dummy frame.  */
702 #define SPECIAL_REG_SAVE_COUNT 1
703
704   PROC_REG_MASK(proc_desc) = GEN_REG_SAVE_MASK;
705   PROC_FREG_MASK(proc_desc) = FLOAT_REG_SAVE_MASK;
706   /* PROC_REG_OFFSET is the offset from the dummy frame to the saved RA,
707      but keep SP aligned to a multiple of 16.  */
708   PROC_REG_OFFSET(proc_desc) =
709     - ((8 * (SPECIAL_REG_SAVE_COUNT
710             + GEN_REG_SAVE_COUNT
711             + FLOAT_REG_SAVE_COUNT)
712         + 15) & ~15);
713   PROC_FREG_OFFSET(proc_desc) =
714     PROC_REG_OFFSET(proc_desc) + 8 * GEN_REG_SAVE_COUNT;
715
716   /* Save general registers.
717      The return address register is the first saved register, all other
718      registers follow in ascending order.
719      The PC is saved immediately below the SP.  */
720   save_address = sp + PROC_REG_OFFSET(proc_desc);
721   store_address (raw_buffer, 8, read_register (RA_REGNUM));
722   write_memory (save_address, raw_buffer, 8);
723   save_address += 8;
724   mask = PROC_REG_MASK(proc_desc) & 0xffffffffL;
725   for (ireg = 0; mask; ireg++, mask >>= 1)
726     if (mask & 1)
727       {
728         if (ireg == RA_REGNUM)
729           continue;
730         store_address (raw_buffer, 8, read_register (ireg));
731         write_memory (save_address, raw_buffer, 8);
732         save_address += 8;
733       }
734
735   store_address (raw_buffer, 8, read_register (PC_REGNUM));
736   write_memory (sp - 8, raw_buffer, 8);
737
738   /* Save floating point registers.  */
739   save_address = sp + PROC_FREG_OFFSET(proc_desc);
740   mask = PROC_FREG_MASK(proc_desc) & 0xffffffffL;
741   for (ireg = 0; mask; ireg++, mask >>= 1)
742     if (mask & 1)
743       {
744         store_address (raw_buffer, 8, read_register (ireg + FP0_REGNUM));
745         write_memory (save_address, raw_buffer, 8);
746         save_address += 8;
747       }
748
749   /* Set and save the frame address for the dummy.  
750      This is tricky. The only registers that are suitable for a frame save
751      are those that are preserved across procedure calls (s0-s6). But if
752      a read system call is interrupted and then a dummy call is made
753      (see testsuite/gdb.t17/interrupt.exp) the dummy call hangs till the read
754      is satisfied. Then it returns with the s0-s6 registers set to the values
755      on entry to the read system call and our dummy frame pointer would be
756      destroyed. So we save the dummy frame in the proc_desc and handle the
757      retrieval of the frame pointer of a dummy specifically. The frame register
758      is set to the virtual frame (pseudo) register, it's value will always
759      be read as zero and will help us to catch any errors in the dummy frame
760      retrieval code.  */
761   PROC_DUMMY_FRAME(proc_desc) = sp;
762   PROC_FRAME_REG(proc_desc) = FP_REGNUM;
763   PROC_FRAME_OFFSET(proc_desc) = 0;
764   sp += PROC_REG_OFFSET(proc_desc);
765   write_register (SP_REGNUM, sp);
766
767   PROC_LOW_ADDR(proc_desc) = entry_point_address ();
768   PROC_HIGH_ADDR(proc_desc) = PROC_LOW_ADDR(proc_desc) + 4;
769
770   SET_PROC_DESC_IS_DUMMY(proc_desc);
771   PROC_PC_REG(proc_desc) = RA_REGNUM;
772 }
773
774 void
775 alpha_pop_frame()
776 {
777   register int regnum;
778   FRAME frame = get_current_frame ();
779   CORE_ADDR new_sp = frame->frame;
780
781   alpha_extra_func_info_t proc_desc = frame->proc_desc;
782
783   write_register (PC_REGNUM, FRAME_SAVED_PC(frame));
784   if (proc_desc)
785     {
786       for (regnum = 32; --regnum >= 0; )
787         if (PROC_REG_MASK(proc_desc) & (1 << regnum))
788           write_register (regnum,
789                           read_memory_integer (frame->saved_regs->regs[regnum],
790                                                8));
791       for (regnum = 32; --regnum >= 0; )
792         if (PROC_FREG_MASK(proc_desc) & (1 << regnum))
793           write_register (regnum + FP0_REGNUM,
794                           read_memory_integer (frame->saved_regs->regs[regnum + FP0_REGNUM], 8));
795     }
796   write_register (SP_REGNUM, new_sp);
797   flush_cached_frames ();
798   /* We let init_extra_frame_info figure out the frame pointer */
799   set_current_frame (create_new_frame (0, read_pc ()));
800
801   if (proc_desc && PROC_DESC_IS_DUMMY(proc_desc))
802     {
803       struct linked_proc_info *pi_ptr, *prev_ptr;
804
805       for (pi_ptr = linked_proc_desc_table, prev_ptr = NULL;
806            pi_ptr != NULL;
807            prev_ptr = pi_ptr, pi_ptr = pi_ptr->next)
808         {
809           if (&pi_ptr->info == proc_desc)
810             break;
811         }
812
813       if (pi_ptr == NULL)
814         error ("Can't locate dummy extra frame info\n");
815
816       if (prev_ptr != NULL)
817         prev_ptr->next = pi_ptr->next;
818       else
819         linked_proc_desc_table = pi_ptr->next;
820
821       free (pi_ptr);
822     }
823 }
824 \f
825 /* To skip prologues, I use this predicate.  Returns either PC itself
826    if the code at PC does not look like a function prologue; otherwise
827    returns an address that (if we're lucky) follows the prologue.  If
828    LENIENT, then we must skip everything which is involved in setting
829    up the frame (it's OK to skip more, just so long as we don't skip
830    anything which might clobber the registers which are being saved.
831    Currently we must not skip more on the alpha, but we might the lenient
832    stuff some day.  */
833
834 CORE_ADDR
835 alpha_skip_prologue (pc, lenient)
836      CORE_ADDR pc;
837      int lenient;
838 {
839     unsigned long inst;
840     int offset;
841
842     /* Skip the typical prologue instructions. These are the stack adjustment
843        instruction and the instructions that save registers on the stack
844        or in the gcc frame.  */
845     for (offset = 0; offset < 100; offset += 4)
846       {
847         char buf[4];
848         int status;
849
850         status = read_memory_nobpt (pc + offset, buf, 4);
851         if (status)
852           memory_error (status, pc + offset);
853         inst = extract_unsigned_integer (buf, 4);
854
855         /* The alpha has no delay slots. But let's keep the lenient stuff,
856            we might need it for something else in the future.  */
857         if (lenient && 0)
858           continue;
859
860         if ((inst & 0xffff0000) == 0x27bb0000)  /* ldah $gp,n($t12) */
861             continue;
862         if ((inst & 0xffff0000) == 0x23bd0000)  /* lda $gp,n($gp) */
863             continue;
864         if ((inst & 0xffff0000) == 0x23de0000)  /* lda $sp,n($sp) */
865             continue;
866         else if ((inst & 0xfc1f0000) == 0xb41e0000
867                  && (inst & 0xffff0000) != 0xb7fe0000)
868             continue;                           /* stq reg,n($sp) */
869                                                 /* reg != $zero */
870         else if ((inst & 0xfc1f0000) == 0x9c1e0000
871                  && (inst & 0xffff0000) != 0x9ffe0000)
872             continue;                           /* stt reg,n($sp) */
873                                                 /* reg != $zero */
874         else if (inst == 0x47de040f)            /* bis sp,sp,fp */
875             continue;
876         else
877             break;
878     }
879     return pc + offset;
880 }
881
882 /* Is address PC in the prologue (loosely defined) for function at
883    STARTADDR?  */
884
885 static int
886 alpha_in_lenient_prologue (startaddr, pc)
887      CORE_ADDR startaddr;
888      CORE_ADDR pc;
889 {
890   CORE_ADDR end_prologue = alpha_skip_prologue (startaddr, 1);
891   return pc >= startaddr && pc < end_prologue;
892 }
893
894 /* The alpha needs a conversion between register and memory format if
895    the register is a floating point register and
896       memory format is float, as the register format must be double
897    or
898       memory format is an integer with 4 bytes or less, as the representation
899       of integers in floating point registers is different. */
900 void
901 alpha_register_convert_to_virtual (regnum, valtype, raw_buffer, virtual_buffer)
902     int regnum;
903     struct type *valtype;
904     char *raw_buffer;
905     char *virtual_buffer;
906 {
907   if (TYPE_LENGTH (valtype) >= REGISTER_RAW_SIZE (regnum))
908     {
909       memcpy (virtual_buffer, raw_buffer, REGISTER_VIRTUAL_SIZE (regnum));
910       return;
911     }
912
913   if (TYPE_CODE (valtype) == TYPE_CODE_FLT)
914     {
915       double d = extract_floating (raw_buffer, REGISTER_RAW_SIZE (regnum));
916       store_floating (virtual_buffer, TYPE_LENGTH (valtype), d);
917     }
918   else if (TYPE_CODE (valtype) == TYPE_CODE_INT && TYPE_LENGTH (valtype) <= 4)
919     {
920       unsigned LONGEST l;
921       l = extract_unsigned_integer (raw_buffer, REGISTER_RAW_SIZE (regnum));
922       l = ((l >> 32) & 0xc0000000) | ((l >> 29) & 0x3fffffff);
923       store_unsigned_integer (virtual_buffer, TYPE_LENGTH (valtype), l);
924     }
925   else
926     error ("Cannot retrieve value from floating point register");
927 }
928
929 void
930 alpha_register_convert_to_raw (valtype, regnum, virtual_buffer, raw_buffer)
931     struct type *valtype;
932     int regnum;
933     char *virtual_buffer;
934     char *raw_buffer;
935 {
936   if (TYPE_LENGTH (valtype) >= REGISTER_RAW_SIZE (regnum))
937     {
938       memcpy (raw_buffer, virtual_buffer, REGISTER_RAW_SIZE (regnum));
939       return;
940     }
941
942   if (TYPE_CODE (valtype) == TYPE_CODE_FLT)
943     {
944       double d = extract_floating (virtual_buffer, TYPE_LENGTH (valtype));
945       store_floating (raw_buffer, REGISTER_RAW_SIZE (regnum), d);
946     }
947   else if (TYPE_CODE (valtype) == TYPE_CODE_INT && TYPE_LENGTH (valtype) <= 4)
948     {
949       unsigned LONGEST l;
950       if (TYPE_UNSIGNED (valtype))
951         l = extract_unsigned_integer (virtual_buffer, TYPE_LENGTH (valtype));
952       else
953         l = extract_signed_integer (virtual_buffer, TYPE_LENGTH (valtype));
954       l = ((l & 0xc0000000) << 32) | ((l & 0x3fffffff) << 29);
955       store_unsigned_integer (raw_buffer, REGISTER_RAW_SIZE (regnum), l);
956     }
957   else
958     error ("Cannot store value in floating point register");
959 }
960
961 /* Given a return value in `regbuf' with a type `valtype', 
962    extract and copy its value into `valbuf'.  */
963 void
964 alpha_extract_return_value (valtype, regbuf, valbuf)
965     struct type *valtype;
966     char regbuf[REGISTER_BYTES];
967     char *valbuf;
968 {
969   int regnum;
970   
971   regnum = TYPE_CODE (valtype) == TYPE_CODE_FLT ? FP0_REGNUM : V0_REGNUM;
972
973   memcpy (valbuf, regbuf + REGISTER_BYTE (regnum), TYPE_LENGTH (valtype));
974 }
975
976 /* Given a return value in `regbuf' with a type `valtype', 
977    write it's value into the appropriate register.  */
978 void
979 alpha_store_return_value (valtype, valbuf)
980     struct type *valtype;
981     char *valbuf;
982 {
983   int regnum;
984   char raw_buffer[MAX_REGISTER_RAW_SIZE];
985   
986   regnum = TYPE_CODE (valtype) == TYPE_CODE_FLT ? FP0_REGNUM : V0_REGNUM;
987   memcpy(raw_buffer, valbuf, TYPE_LENGTH (valtype));
988
989   write_register_bytes(REGISTER_BYTE (regnum), raw_buffer, TYPE_LENGTH (valtype));
990 }
991
992 /* Print the instruction at address MEMADDR in debugged memory,
993    on STREAM.  Returns length of the instruction, in bytes.  */
994
995 int
996 print_insn (memaddr, stream)
997      CORE_ADDR memaddr;
998      GDB_FILE *stream;
999 {
1000   disassemble_info info;
1001
1002   GDB_INIT_DISASSEMBLE_INFO(info, stream);
1003
1004   return print_insn_alpha (memaddr, &info);
1005 }
1006
1007 /* Just like reinit_frame_cache, but with the right arguments to be
1008    callable as an sfunc.  */
1009 static void
1010 reinit_frame_cache_sfunc (args, from_tty, c)
1011      char *args;
1012      int from_tty;
1013      struct cmd_list_element *c;
1014 {
1015   reinit_frame_cache ();
1016 }
1017
1018 void
1019 _initialize_alpha_tdep ()
1020 {
1021   struct cmd_list_element *c;
1022
1023   /* Let the user set the fence post for heuristic_proc_start.  */
1024
1025   /* We really would like to have both "0" and "unlimited" work, but
1026      command.c doesn't deal with that.  So make it a var_zinteger
1027      because the user can always use "999999" or some such for unlimited.  */
1028   c = add_set_cmd ("heuristic-fence-post", class_support, var_zinteger,
1029                    (char *) &heuristic_fence_post,
1030                    "\
1031 Set the distance searched for the start of a function.\n\
1032 If you are debugging a stripped executable, GDB needs to search through the\n\
1033 program for the start of a function.  This command sets the distance of the\n\
1034 search.  The only need to set it is when debugging a stripped executable.",
1035                    &setlist);
1036   /* We need to throw away the frame cache when we set this, since it
1037      might change our ability to get backtraces.  */
1038   c->function.sfunc = reinit_frame_cache_sfunc;
1039   add_show_from_set (c, &showlist);
1040 }