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