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