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