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