]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/alpha-tdep.c
*** empty log message ***
[thirdparty/binutils-gdb.git] / gdb / alpha-tdep.c
CommitLineData
c906108c 1/* Target-dependent code for the ALPHA architecture, for GDB, the GNU Debugger.
1e698235 2 Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
b6ba6518 3 Free Software Foundation, Inc.
c906108c 4
c5aa993b 5 This file is part of GDB.
c906108c 6
c5aa993b
JM
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
c906108c 11
c5aa993b
JM
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
c906108c 16
c5aa993b
JM
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
c906108c
SS
21
22#include "defs.h"
23#include "frame.h"
24#include "inferior.h"
25#include "symtab.h"
26#include "value.h"
27#include "gdbcmd.h"
28#include "gdbcore.h"
29#include "dis-asm.h"
30#include "symfile.h"
31#include "objfiles.h"
32#include "gdb_string.h"
c5f0f3d0 33#include "linespec.h"
4e052eda 34#include "regcache.h"
d16aafd8 35#include "doublest.h"
dc129d82 36#include "arch-utils.h"
4be87837 37#include "osabi.h"
dc129d82
JT
38
39#include "elf-bfd.h"
40
41#include "alpha-tdep.h"
42
43static gdbarch_init_ftype alpha_gdbarch_init;
44
45static gdbarch_register_name_ftype alpha_register_name;
46static gdbarch_register_raw_size_ftype alpha_register_raw_size;
47static gdbarch_register_virtual_size_ftype alpha_register_virtual_size;
48static gdbarch_register_virtual_type_ftype alpha_register_virtual_type;
49static gdbarch_register_byte_ftype alpha_register_byte;
50static gdbarch_cannot_fetch_register_ftype alpha_cannot_fetch_register;
51static gdbarch_cannot_store_register_ftype alpha_cannot_store_register;
52static gdbarch_register_convertible_ftype alpha_register_convertible;
53static gdbarch_register_convert_to_virtual_ftype
54 alpha_register_convert_to_virtual;
55static gdbarch_register_convert_to_raw_ftype alpha_register_convert_to_raw;
56static gdbarch_store_struct_return_ftype alpha_store_struct_return;
26e9b323 57static gdbarch_deprecated_extract_return_value_ftype alpha_extract_return_value;
26e9b323 58static gdbarch_deprecated_extract_struct_value_address_ftype
dc129d82
JT
59 alpha_extract_struct_value_address;
60static gdbarch_use_struct_convention_ftype alpha_use_struct_convention;
61
95b80706
JT
62static gdbarch_breakpoint_from_pc_ftype alpha_breakpoint_from_pc;
63
dc129d82
JT
64static gdbarch_frame_args_address_ftype alpha_frame_args_address;
65static gdbarch_frame_locals_address_ftype alpha_frame_locals_address;
66
67static gdbarch_skip_prologue_ftype alpha_skip_prologue;
dc129d82
JT
68static gdbarch_saved_pc_after_call_ftype alpha_saved_pc_after_call;
69static gdbarch_frame_chain_ftype alpha_frame_chain;
70static gdbarch_frame_saved_pc_ftype alpha_frame_saved_pc;
71static gdbarch_frame_init_saved_regs_ftype alpha_frame_init_saved_regs;
72
73static gdbarch_push_arguments_ftype alpha_push_arguments;
74static gdbarch_push_dummy_frame_ftype alpha_push_dummy_frame;
75static gdbarch_pop_frame_ftype alpha_pop_frame;
76static gdbarch_fix_call_dummy_ftype alpha_fix_call_dummy;
dc129d82 77static gdbarch_init_extra_frame_info_ftype alpha_init_extra_frame_info;
c906108c 78
accc6d1f
JT
79static gdbarch_get_longjmp_target_ftype alpha_get_longjmp_target;
80
140f9984
JT
81struct frame_extra_info
82 {
83 alpha_extra_func_info_t proc_desc;
84 int localoff;
85 int pc_reg;
86 };
87
c906108c
SS
88/* FIXME: Some of this code should perhaps be merged with mips-tdep.c. */
89
90/* Prototypes for local functions. */
91
140f9984
JT
92static void alpha_find_saved_regs (struct frame_info *);
93
a14ed312 94static alpha_extra_func_info_t push_sigtramp_desc (CORE_ADDR low_addr);
c906108c 95
a14ed312 96static CORE_ADDR read_next_frame_reg (struct frame_info *, int);
c906108c 97
a14ed312 98static CORE_ADDR heuristic_proc_start (CORE_ADDR);
c906108c 99
a14ed312
KB
100static alpha_extra_func_info_t heuristic_proc_desc (CORE_ADDR,
101 CORE_ADDR,
102 struct frame_info *);
c906108c 103
a14ed312
KB
104static alpha_extra_func_info_t find_proc_desc (CORE_ADDR,
105 struct frame_info *);
c906108c
SS
106
107#if 0
a14ed312 108static int alpha_in_lenient_prologue (CORE_ADDR, CORE_ADDR);
c906108c
SS
109#endif
110
a14ed312 111static void reinit_frame_cache_sfunc (char *, int, struct cmd_list_element *);
c906108c 112
a14ed312
KB
113static CORE_ADDR after_prologue (CORE_ADDR pc,
114 alpha_extra_func_info_t proc_desc);
c906108c 115
a14ed312
KB
116static int alpha_in_prologue (CORE_ADDR pc,
117 alpha_extra_func_info_t proc_desc);
c906108c 118
a14ed312 119static int alpha_about_to_return (CORE_ADDR pc);
392a587b 120
a14ed312 121void _initialize_alpha_tdep (void);
392a587b 122
c906108c
SS
123/* Heuristic_proc_start may hunt through the text section for a long
124 time across a 2400 baud serial line. Allows the user to limit this
125 search. */
126static unsigned int heuristic_fence_post = 0;
c5aa993b 127/* *INDENT-OFF* */
c906108c
SS
128/* Layout of a stack frame on the alpha:
129
130 | |
131 pdr members: | 7th ... nth arg, |
132 | `pushed' by caller. |
133 | |
134----------------|-------------------------------|<-- old_sp == vfp
135 ^ ^ ^ ^ | |
136 | | | | | |
137 | |localoff | Copies of 1st .. 6th |
138 | | | | | argument if necessary. |
139 | | | v | |
140 | | | --- |-------------------------------|<-- FRAME_LOCALS_ADDRESS
141 | | | | |
142 | | | | Locals and temporaries. |
143 | | | | |
144 | | | |-------------------------------|
145 | | | | |
146 |-fregoffset | Saved float registers. |
147 | | | | F9 |
148 | | | | . |
149 | | | | . |
150 | | | | F2 |
151 | | v | |
152 | | -------|-------------------------------|
153 | | | |
154 | | | Saved registers. |
155 | | | S6 |
156 |-regoffset | . |
157 | | | . |
158 | | | S0 |
159 | | | pdr.pcreg |
160 | v | |
161 | ----------|-------------------------------|
162 | | |
163 frameoffset | Argument build area, gets |
164 | | 7th ... nth arg for any |
165 | | called procedure. |
166 v | |
167 -------------|-------------------------------|<-- sp
168 | |
169*/
c5aa993b
JM
170/* *INDENT-ON* */
171
c5aa993b 172#define PROC_LOW_ADDR(proc) ((proc)->pdr.adr) /* least address */
b83266a0
SS
173/* These next two fields are kind of being hijacked. I wonder if
174 iline is too small for the values it needs to hold, if GDB is
175 running on a 32-bit host. */
c5aa993b
JM
176#define PROC_HIGH_ADDR(proc) ((proc)->pdr.iline) /* upper address bound */
177#define PROC_DUMMY_FRAME(proc) ((proc)->pdr.cbLineOffset) /*CALL_DUMMY frame */
c906108c
SS
178#define PROC_FRAME_OFFSET(proc) ((proc)->pdr.frameoffset)
179#define PROC_FRAME_REG(proc) ((proc)->pdr.framereg)
180#define PROC_REG_MASK(proc) ((proc)->pdr.regmask)
181#define PROC_FREG_MASK(proc) ((proc)->pdr.fregmask)
182#define PROC_REG_OFFSET(proc) ((proc)->pdr.regoffset)
183#define PROC_FREG_OFFSET(proc) ((proc)->pdr.fregoffset)
184#define PROC_PC_REG(proc) ((proc)->pdr.pcreg)
185#define PROC_LOCALOFF(proc) ((proc)->pdr.localoff)
186#define PROC_SYMBOL(proc) (*(struct symbol**)&(proc)->pdr.isym)
187#define _PROC_MAGIC_ 0x0F0F0F0F
188#define PROC_DESC_IS_DUMMY(proc) ((proc)->pdr.isym == _PROC_MAGIC_)
189#define SET_PROC_DESC_IS_DUMMY(proc) ((proc)->pdr.isym = _PROC_MAGIC_)
190
191struct linked_proc_info
c5aa993b
JM
192 {
193 struct alpha_extra_func_info info;
194 struct linked_proc_info *next;
195 }
196 *linked_proc_desc_table = NULL;
c906108c 197\f
36a6271d
JT
198static CORE_ADDR
199alpha_frame_past_sigtramp_frame (struct frame_info *frame, CORE_ADDR pc)
c906108c 200{
36a6271d
JT
201 struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch);
202
203 if (tdep->skip_sigtramp_frame != NULL)
204 return (tdep->skip_sigtramp_frame (frame, pc));
205
206 return (0);
207}
208
209static LONGEST
210alpha_dynamic_sigtramp_offset (CORE_ADDR pc)
211{
212 struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch);
213
214 /* Must be provided by OS/ABI variant code if supported. */
215 if (tdep->dynamic_sigtramp_offset != NULL)
216 return (tdep->dynamic_sigtramp_offset (pc));
217
218 return (-1);
219}
220
221#define ALPHA_PROC_SIGTRAMP_MAGIC 0x0e0f0f0f
222
223/* Return TRUE if the procedure descriptor PROC is a procedure
224 descriptor that refers to a dynamically generated signal
225 trampoline routine. */
226static int
227alpha_proc_desc_is_dyn_sigtramp (struct alpha_extra_func_info *proc)
228{
229 struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch);
230
231 if (tdep->dynamic_sigtramp_offset != NULL)
232 return (proc->pdr.isym == ALPHA_PROC_SIGTRAMP_MAGIC);
233
234 return (0);
235}
236
237static void
238alpha_set_proc_desc_is_dyn_sigtramp (struct alpha_extra_func_info *proc)
239{
240 struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch);
241
242 if (tdep->dynamic_sigtramp_offset != NULL)
243 proc->pdr.isym = ALPHA_PROC_SIGTRAMP_MAGIC;
c906108c 244}
c5aa993b 245
c906108c
SS
246/* Dynamically create a signal-handler caller procedure descriptor for
247 the signal-handler return code starting at address LOW_ADDR. The
248 descriptor is added to the linked_proc_desc_table. */
249
250static alpha_extra_func_info_t
fba45db2 251push_sigtramp_desc (CORE_ADDR low_addr)
c906108c
SS
252{
253 struct linked_proc_info *link;
254 alpha_extra_func_info_t proc_desc;
255
256 link = (struct linked_proc_info *)
257 xmalloc (sizeof (struct linked_proc_info));
258 link->next = linked_proc_desc_table;
259 linked_proc_desc_table = link;
260
261 proc_desc = &link->info;
262
263 proc_desc->numargs = 0;
c5aa993b
JM
264 PROC_LOW_ADDR (proc_desc) = low_addr;
265 PROC_HIGH_ADDR (proc_desc) = low_addr + 3 * 4;
266 PROC_DUMMY_FRAME (proc_desc) = 0;
267 PROC_FRAME_OFFSET (proc_desc) = 0x298; /* sizeof(struct sigcontext_struct) */
268 PROC_FRAME_REG (proc_desc) = SP_REGNUM;
269 PROC_REG_MASK (proc_desc) = 0xffff;
270 PROC_FREG_MASK (proc_desc) = 0xffff;
271 PROC_PC_REG (proc_desc) = 26;
272 PROC_LOCALOFF (proc_desc) = 0;
36a6271d 273 alpha_set_proc_desc_is_dyn_sigtramp (proc_desc);
c906108c
SS
274 return (proc_desc);
275}
c906108c 276\f
c5aa993b 277
fa88f677 278static const char *
636a6dfc
JT
279alpha_register_name (int regno)
280{
281 static char *register_names[] =
282 {
283 "v0", "t0", "t1", "t2", "t3", "t4", "t5", "t6",
284 "t7", "s0", "s1", "s2", "s3", "s4", "s5", "fp",
285 "a0", "a1", "a2", "a3", "a4", "a5", "t8", "t9",
286 "t10", "t11", "ra", "t12", "at", "gp", "sp", "zero",
287 "f0", "f1", "f2", "f3", "f4", "f5", "f6", "f7",
288 "f8", "f9", "f10", "f11", "f12", "f13", "f14", "f15",
289 "f16", "f17", "f18", "f19", "f20", "f21", "f22", "f23",
290 "f24", "f25", "f26", "f27", "f28", "f29", "f30", "fpcr",
87d1b352 291 "pc", "vfp", "unique",
636a6dfc
JT
292 };
293
294 if (regno < 0)
295 return (NULL);
296 if (regno >= (sizeof(register_names) / sizeof(*register_names)))
297 return (NULL);
298 return (register_names[regno]);
299}
d734c450 300
dc129d82 301static int
d734c450
JT
302alpha_cannot_fetch_register (int regno)
303{
dc129d82 304 return (regno == FP_REGNUM || regno == ALPHA_ZERO_REGNUM);
d734c450
JT
305}
306
dc129d82 307static int
d734c450
JT
308alpha_cannot_store_register (int regno)
309{
dc129d82 310 return (regno == FP_REGNUM || regno == ALPHA_ZERO_REGNUM);
d734c450
JT
311}
312
dc129d82 313static int
d734c450
JT
314alpha_register_convertible (int regno)
315{
316 return (regno >= FP0_REGNUM && regno <= FP0_REGNUM + 31);
317}
0d056799 318
dc129d82 319static struct type *
0d056799
JT
320alpha_register_virtual_type (int regno)
321{
322 return ((regno >= FP0_REGNUM && regno < (FP0_REGNUM+31))
323 ? builtin_type_double : builtin_type_long);
324}
f8453e34 325
dc129d82 326static int
f8453e34
JT
327alpha_register_byte (int regno)
328{
329 return (regno * 8);
330}
331
dc129d82 332static int
f8453e34
JT
333alpha_register_raw_size (int regno)
334{
335 return 8;
336}
337
dc129d82 338static int
f8453e34
JT
339alpha_register_virtual_size (int regno)
340{
341 return 8;
342}
636a6dfc
JT
343\f
344
5868c862
JT
345static CORE_ADDR
346alpha_sigcontext_addr (struct frame_info *fi)
347{
348 struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch);
349
350 if (tdep->sigcontext_addr)
351 return (tdep->sigcontext_addr (fi));
352
353 return (0);
354}
355
c906108c
SS
356/* Guaranteed to set frame->saved_regs to some values (it never leaves it
357 NULL). */
358
140f9984 359static void
fba45db2 360alpha_find_saved_regs (struct frame_info *frame)
c906108c
SS
361{
362 int ireg;
363 CORE_ADDR reg_position;
364 unsigned long mask;
365 alpha_extra_func_info_t proc_desc;
366 int returnreg;
367
368 frame_saved_regs_zalloc (frame);
369
370 /* If it is the frame for __sigtramp, the saved registers are located
371 in a sigcontext structure somewhere on the stack. __sigtramp
372 passes a pointer to the sigcontext structure on the stack.
373 If the stack layout for __sigtramp changes, or if sigcontext offsets
374 change, we might have to update this code. */
375#ifndef SIGFRAME_PC_OFF
376#define SIGFRAME_PC_OFF (2 * 8)
377#define SIGFRAME_REGSAVE_OFF (4 * 8)
378#define SIGFRAME_FPREGSAVE_OFF (SIGFRAME_REGSAVE_OFF + 32 * 8 + 8)
379#endif
5a203e44 380 if ((get_frame_type (frame) == SIGTRAMP_FRAME))
c906108c
SS
381 {
382 CORE_ADDR sigcontext_addr;
383
5868c862
JT
384 sigcontext_addr = alpha_sigcontext_addr (frame);
385 if (sigcontext_addr == 0)
386 {
387 /* Don't know where the sigcontext is; just bail. */
388 return;
389 }
c906108c
SS
390 for (ireg = 0; ireg < 32; ireg++)
391 {
c5aa993b 392 reg_position = sigcontext_addr + SIGFRAME_REGSAVE_OFF + ireg * 8;
b2fb4676 393 get_frame_saved_regs (frame)[ireg] = reg_position;
c906108c
SS
394 }
395 for (ireg = 0; ireg < 32; ireg++)
396 {
c5aa993b 397 reg_position = sigcontext_addr + SIGFRAME_FPREGSAVE_OFF + ireg * 8;
b2fb4676 398 get_frame_saved_regs (frame)[FP0_REGNUM + ireg] = reg_position;
c906108c 399 }
b2fb4676 400 get_frame_saved_regs (frame)[PC_REGNUM] = sigcontext_addr + SIGFRAME_PC_OFF;
c906108c
SS
401 return;
402 }
403
da50a4b7 404 proc_desc = get_frame_extra_info (frame)->proc_desc;
c906108c
SS
405 if (proc_desc == NULL)
406 /* I'm not sure how/whether this can happen. Normally when we can't
407 find a proc_desc, we "synthesize" one using heuristic_proc_desc
408 and set the saved_regs right away. */
409 return;
410
411 /* Fill in the offsets for the registers which gen_mask says
412 were saved. */
413
1e2330ba 414 reg_position = get_frame_base (frame) + PROC_REG_OFFSET (proc_desc);
c906108c
SS
415 mask = PROC_REG_MASK (proc_desc);
416
417 returnreg = PROC_PC_REG (proc_desc);
418
419 /* Note that RA is always saved first, regardless of its actual
420 register number. */
421 if (mask & (1 << returnreg))
422 {
b2fb4676 423 get_frame_saved_regs (frame)[returnreg] = reg_position;
c906108c 424 reg_position += 8;
c5aa993b
JM
425 mask &= ~(1 << returnreg); /* Clear bit for RA so we
426 don't save again later. */
c906108c
SS
427 }
428
c5aa993b 429 for (ireg = 0; ireg <= 31; ++ireg)
c906108c
SS
430 if (mask & (1 << ireg))
431 {
b2fb4676 432 get_frame_saved_regs (frame)[ireg] = reg_position;
c906108c
SS
433 reg_position += 8;
434 }
435
436 /* Fill in the offsets for the registers which float_mask says
437 were saved. */
438
1e2330ba 439 reg_position = get_frame_base (frame) + PROC_FREG_OFFSET (proc_desc);
c906108c
SS
440 mask = PROC_FREG_MASK (proc_desc);
441
c5aa993b 442 for (ireg = 0; ireg <= 31; ++ireg)
c906108c
SS
443 if (mask & (1 << ireg))
444 {
b2fb4676 445 get_frame_saved_regs (frame)[FP0_REGNUM + ireg] = reg_position;
c906108c
SS
446 reg_position += 8;
447 }
448
b2fb4676 449 get_frame_saved_regs (frame)[PC_REGNUM] = get_frame_saved_regs (frame)[returnreg];
c906108c
SS
450}
451
dc129d82 452static void
140f9984
JT
453alpha_frame_init_saved_regs (struct frame_info *fi)
454{
b2fb4676 455 if (get_frame_saved_regs (fi) == NULL)
140f9984 456 alpha_find_saved_regs (fi);
1e2330ba 457 get_frame_saved_regs (fi)[SP_REGNUM] = get_frame_base (fi);
140f9984
JT
458}
459
97f46953 460static CORE_ADDR
0d056799
JT
461alpha_init_frame_pc_first (int fromleaf, struct frame_info *prev)
462{
97f46953 463 return (fromleaf ? SAVED_PC_AFTER_CALL (get_next_frame (prev))
11c02a10 464 : get_next_frame (prev) ? FRAME_SAVED_PC (get_next_frame (prev))
97f46953 465 : read_pc ());
0d056799
JT
466}
467
c906108c 468static CORE_ADDR
fba45db2 469read_next_frame_reg (struct frame_info *fi, int regno)
c906108c 470{
11c02a10 471 for (; fi; fi = get_next_frame (fi))
c906108c
SS
472 {
473 /* We have to get the saved sp from the sigcontext
c5aa993b 474 if it is a signal handler frame. */
5a203e44 475 if (regno == SP_REGNUM && !(get_frame_type (fi) == SIGTRAMP_FRAME))
1e2330ba 476 return get_frame_base (fi);
c906108c
SS
477 else
478 {
b2fb4676 479 if (get_frame_saved_regs (fi) == NULL)
c906108c 480 alpha_find_saved_regs (fi);
b2fb4676
AC
481 if (get_frame_saved_regs (fi)[regno])
482 return read_memory_integer (get_frame_saved_regs (fi)[regno], 8);
c906108c
SS
483 }
484 }
c5aa993b 485 return read_register (regno);
c906108c
SS
486}
487
dc129d82 488static CORE_ADDR
fba45db2 489alpha_frame_saved_pc (struct frame_info *frame)
c906108c 490{
da50a4b7 491 alpha_extra_func_info_t proc_desc = get_frame_extra_info (frame)->proc_desc;
c906108c
SS
492 /* We have to get the saved pc from the sigcontext
493 if it is a signal handler frame. */
da50a4b7
AC
494 int pcreg = ((get_frame_type (frame) == SIGTRAMP_FRAME)
495 ? PC_REGNUM
496 : get_frame_extra_info (frame)->pc_reg);
c906108c 497
c5aa993b 498 if (proc_desc && PROC_DESC_IS_DUMMY (proc_desc))
1e2330ba 499 return read_memory_integer (get_frame_base (frame) - 8, 8);
c906108c 500
c5aa993b 501 return read_next_frame_reg (frame, pcreg);
c906108c
SS
502}
503
dc129d82 504static CORE_ADDR
fba45db2 505alpha_saved_pc_after_call (struct frame_info *frame)
c906108c 506{
50abf9e5 507 CORE_ADDR pc = get_frame_pc (frame);
c906108c
SS
508 CORE_ADDR tmp;
509 alpha_extra_func_info_t proc_desc;
510 int pcreg;
511
512 /* Skip over shared library trampoline if necessary. */
513 tmp = SKIP_TRAMPOLINE_CODE (pc);
514 if (tmp != 0)
515 pc = tmp;
516
11c02a10 517 proc_desc = find_proc_desc (pc, get_next_frame (frame));
dc129d82 518 pcreg = proc_desc ? PROC_PC_REG (proc_desc) : ALPHA_RA_REGNUM;
c906108c 519
5a203e44 520 if ((get_frame_type (frame) == SIGTRAMP_FRAME))
c906108c
SS
521 return alpha_frame_saved_pc (frame);
522 else
523 return read_register (pcreg);
524}
525
526
527static struct alpha_extra_func_info temp_proc_desc;
dc129d82 528static CORE_ADDR temp_saved_regs[ALPHA_NUM_REGS];
c906108c
SS
529
530/* Nonzero if instruction at PC is a return instruction. "ret
531 $zero,($ra),1" on alpha. */
532
533static int
fba45db2 534alpha_about_to_return (CORE_ADDR pc)
c906108c
SS
535{
536 return read_memory_integer (pc, 4) == 0x6bfa8001;
537}
538
539
540
541/* This fencepost looks highly suspicious to me. Removing it also
542 seems suspicious as it could affect remote debugging across serial
543 lines. */
544
545static CORE_ADDR
fba45db2 546heuristic_proc_start (CORE_ADDR pc)
c906108c 547{
d9b023cc 548 struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch);
c5aa993b
JM
549 CORE_ADDR start_pc = pc;
550 CORE_ADDR fence = start_pc - heuristic_fence_post;
c906108c 551
c5aa993b
JM
552 if (start_pc == 0)
553 return 0;
c906108c 554
c5aa993b 555 if (heuristic_fence_post == UINT_MAX
d9b023cc
JT
556 || fence < tdep->vm_min_address)
557 fence = tdep->vm_min_address;
c906108c 558
c5aa993b
JM
559 /* search back for previous return */
560 for (start_pc -= 4;; start_pc -= 4)
561 if (start_pc < fence)
562 {
563 /* It's not clear to me why we reach this point when
564 stop_soon_quietly, but with this test, at least we
565 don't print out warnings for every child forked (eg, on
566 decstation). 22apr93 rich@cygnus.com. */
567 if (!stop_soon_quietly)
c906108c 568 {
c5aa993b
JM
569 static int blurb_printed = 0;
570
d9b023cc 571 if (fence == tdep->vm_min_address)
c5aa993b
JM
572 warning ("Hit beginning of text section without finding");
573 else
574 warning ("Hit heuristic-fence-post without finding");
575
d4f3574e 576 warning ("enclosing function for address 0x%s", paddr_nz (pc));
c5aa993b 577 if (!blurb_printed)
c906108c 578 {
c5aa993b 579 printf_filtered ("\
c906108c
SS
580This warning occurs if you are debugging a function without any symbols\n\
581(for example, in a stripped executable). In that case, you may wish to\n\
582increase the size of the search with the `set heuristic-fence-post' command.\n\
583\n\
584Otherwise, you told GDB there was a function where there isn't one, or\n\
585(more likely) you have encountered a bug in GDB.\n");
c5aa993b 586 blurb_printed = 1;
c906108c 587 }
c906108c 588 }
c906108c 589
c5aa993b
JM
590 return 0;
591 }
592 else if (alpha_about_to_return (start_pc))
593 break;
594
595 start_pc += 4; /* skip return */
596 return start_pc;
c906108c
SS
597}
598
599static alpha_extra_func_info_t
fba45db2
KB
600heuristic_proc_desc (CORE_ADDR start_pc, CORE_ADDR limit_pc,
601 struct frame_info *next_frame)
c906108c 602{
c5aa993b 603 CORE_ADDR sp = read_next_frame_reg (next_frame, SP_REGNUM);
dc1b0db2 604 CORE_ADDR vfp = sp;
c5aa993b
JM
605 CORE_ADDR cur_pc;
606 int frame_size;
607 int has_frame_reg = 0;
608 unsigned long reg_mask = 0;
609 int pcreg = -1;
dc1b0db2 610 int regno;
c5aa993b
JM
611
612 if (start_pc == 0)
613 return NULL;
614 memset (&temp_proc_desc, '\0', sizeof (temp_proc_desc));
140f9984 615 memset (&temp_saved_regs, '\0', SIZEOF_FRAME_SAVED_REGS);
c5aa993b
JM
616 PROC_LOW_ADDR (&temp_proc_desc) = start_pc;
617
618 if (start_pc + 200 < limit_pc)
619 limit_pc = start_pc + 200;
620 frame_size = 0;
621 for (cur_pc = start_pc; cur_pc < limit_pc; cur_pc += 4)
622 {
623 char buf[4];
624 unsigned long word;
625 int status;
c906108c 626
c5aa993b
JM
627 status = read_memory_nobpt (cur_pc, buf, 4);
628 if (status)
629 memory_error (status, cur_pc);
630 word = extract_unsigned_integer (buf, 4);
c906108c 631
c5aa993b
JM
632 if ((word & 0xffff0000) == 0x23de0000) /* lda $sp,n($sp) */
633 {
634 if (word & 0x8000)
dc1b0db2
JB
635 {
636 /* Consider only the first stack allocation instruction
637 to contain the static size of the frame. */
638 if (frame_size == 0)
639 frame_size += (-word) & 0xffff;
640 }
c5aa993b
JM
641 else
642 /* Exit loop if a positive stack adjustment is found, which
643 usually means that the stack cleanup code in the function
644 epilogue is reached. */
645 break;
646 }
647 else if ((word & 0xfc1f0000) == 0xb41e0000 /* stq reg,n($sp) */
648 && (word & 0xffff0000) != 0xb7fe0000) /* reg != $zero */
649 {
650 int reg = (word & 0x03e00000) >> 21;
651 reg_mask |= 1 << reg;
dc1b0db2
JB
652
653 /* Do not compute the address where the register was saved yet,
654 because we don't know yet if the offset will need to be
655 relative to $sp or $fp (we can not compute the address relative
656 to $sp if $sp is updated during the execution of the current
657 subroutine, for instance when doing some alloca). So just store
658 the offset for the moment, and compute the address later
659 when we know whether this frame has a frame pointer or not.
660 */
661 temp_saved_regs[reg] = (short) word;
c5aa993b
JM
662
663 /* Starting with OSF/1-3.2C, the system libraries are shipped
664 without local symbols, but they still contain procedure
665 descriptors without a symbol reference. GDB is currently
666 unable to find these procedure descriptors and uses
667 heuristic_proc_desc instead.
668 As some low level compiler support routines (__div*, __add*)
669 use a non-standard return address register, we have to
670 add some heuristics to determine the return address register,
671 or stepping over these routines will fail.
672 Usually the return address register is the first register
673 saved on the stack, but assembler optimization might
674 rearrange the register saves.
675 So we recognize only a few registers (t7, t9, ra) within
676 the procedure prologue as valid return address registers.
677 If we encounter a return instruction, we extract the
678 the return address register from it.
679
680 FIXME: Rewriting GDB to access the procedure descriptors,
681 e.g. via the minimal symbol table, might obviate this hack. */
682 if (pcreg == -1
683 && cur_pc < (start_pc + 80)
dc129d82
JT
684 && (reg == ALPHA_T7_REGNUM || reg == ALPHA_T9_REGNUM
685 || reg == ALPHA_RA_REGNUM))
c5aa993b
JM
686 pcreg = reg;
687 }
688 else if ((word & 0xffe0ffff) == 0x6be08001) /* ret zero,reg,1 */
689 pcreg = (word >> 16) & 0x1f;
dc1b0db2
JB
690 else if (word == 0x47de040f || word == 0x47fe040f) /* bis sp,sp fp */
691 {
692 /* ??? I am not sure what instruction is 0x47fe040f, and I
693 am suspecting that there was a typo and should have been
694 0x47fe040f. I'm keeping it in the test above until further
695 investigation */
696 has_frame_reg = 1;
697 vfp = read_next_frame_reg (next_frame, ALPHA_GCC_FP_REGNUM);
698 }
c5aa993b
JM
699 }
700 if (pcreg == -1)
701 {
702 /* If we haven't found a valid return address register yet,
703 keep searching in the procedure prologue. */
704 while (cur_pc < (limit_pc + 80) && cur_pc < (start_pc + 80))
705 {
706 char buf[4];
707 unsigned long word;
c906108c 708
c5aa993b
JM
709 if (read_memory_nobpt (cur_pc, buf, 4))
710 break;
711 cur_pc += 4;
712 word = extract_unsigned_integer (buf, 4);
c906108c 713
c5aa993b
JM
714 if ((word & 0xfc1f0000) == 0xb41e0000 /* stq reg,n($sp) */
715 && (word & 0xffff0000) != 0xb7fe0000) /* reg != $zero */
716 {
717 int reg = (word & 0x03e00000) >> 21;
dc129d82
JT
718 if (reg == ALPHA_T7_REGNUM || reg == ALPHA_T9_REGNUM
719 || reg == ALPHA_RA_REGNUM)
c5aa993b
JM
720 {
721 pcreg = reg;
722 break;
723 }
724 }
725 else if ((word & 0xffe0ffff) == 0x6be08001) /* ret zero,reg,1 */
726 {
727 pcreg = (word >> 16) & 0x1f;
728 break;
729 }
730 }
731 }
c906108c 732
c5aa993b 733 if (has_frame_reg)
dc129d82 734 PROC_FRAME_REG (&temp_proc_desc) = ALPHA_GCC_FP_REGNUM;
c5aa993b
JM
735 else
736 PROC_FRAME_REG (&temp_proc_desc) = SP_REGNUM;
dc1b0db2
JB
737
738 /* At this point, we know which of the Stack Pointer or the Frame Pointer
739 to use as the reference address to compute the saved registers address.
740 But in both cases, the processing above has set vfp to this reference
741 address, so just need to increment the offset of each saved register
742 by this address. */
743 for (regno = 0; regno < NUM_REGS; regno++)
744 {
745 if (reg_mask & 1 << regno)
746 temp_saved_regs[regno] += vfp;
747 }
748
c5aa993b
JM
749 PROC_FRAME_OFFSET (&temp_proc_desc) = frame_size;
750 PROC_REG_MASK (&temp_proc_desc) = reg_mask;
dc129d82 751 PROC_PC_REG (&temp_proc_desc) = (pcreg == -1) ? ALPHA_RA_REGNUM : pcreg;
c5aa993b
JM
752 PROC_LOCALOFF (&temp_proc_desc) = 0; /* XXX - bogus */
753 return &temp_proc_desc;
c906108c
SS
754}
755
756/* This returns the PC of the first inst after the prologue. If we can't
757 find the prologue, then return 0. */
758
759static CORE_ADDR
fba45db2 760after_prologue (CORE_ADDR pc, alpha_extra_func_info_t proc_desc)
c906108c
SS
761{
762 struct symtab_and_line sal;
763 CORE_ADDR func_addr, func_end;
764
765 if (!proc_desc)
766 proc_desc = find_proc_desc (pc, NULL);
767
768 if (proc_desc)
769 {
36a6271d 770 if (alpha_proc_desc_is_dyn_sigtramp (proc_desc))
c906108c
SS
771 return PROC_LOW_ADDR (proc_desc); /* "prologue" is in kernel */
772
773 /* If function is frameless, then we need to do it the hard way. I
c5aa993b 774 strongly suspect that frameless always means prologueless... */
c906108c
SS
775 if (PROC_FRAME_REG (proc_desc) == SP_REGNUM
776 && PROC_FRAME_OFFSET (proc_desc) == 0)
777 return 0;
778 }
779
780 if (!find_pc_partial_function (pc, NULL, &func_addr, &func_end))
781 return 0; /* Unknown */
782
783 sal = find_pc_line (func_addr, 0);
784
785 if (sal.end < func_end)
786 return sal.end;
787
788 /* The line after the prologue is after the end of the function. In this
789 case, tell the caller to find the prologue the hard way. */
790
791 return 0;
792}
793
794/* Return non-zero if we *might* be in a function prologue. Return zero if we
795 are definitively *not* in a function prologue. */
796
797static int
fba45db2 798alpha_in_prologue (CORE_ADDR pc, alpha_extra_func_info_t proc_desc)
c906108c
SS
799{
800 CORE_ADDR after_prologue_pc;
801
802 after_prologue_pc = after_prologue (pc, proc_desc);
803
804 if (after_prologue_pc == 0
805 || pc < after_prologue_pc)
806 return 1;
807 else
808 return 0;
809}
810
811static alpha_extra_func_info_t
fba45db2 812find_proc_desc (CORE_ADDR pc, struct frame_info *next_frame)
c906108c
SS
813{
814 alpha_extra_func_info_t proc_desc;
815 struct block *b;
816 struct symbol *sym;
817 CORE_ADDR startaddr;
818
819 /* Try to get the proc_desc from the linked call dummy proc_descs
820 if the pc is in the call dummy.
821 This is hairy. In the case of nested dummy calls we have to find the
822 right proc_desc, but we might not yet know the frame for the dummy
823 as it will be contained in the proc_desc we are searching for.
824 So we have to find the proc_desc whose frame is closest to the current
825 stack pointer. */
826
ae45cd16 827 if (DEPRECATED_PC_IN_CALL_DUMMY (pc, 0, 0))
c906108c
SS
828 {
829 struct linked_proc_info *link;
830 CORE_ADDR sp = read_next_frame_reg (next_frame, SP_REGNUM);
831 alpha_extra_func_info_t found_proc_desc = NULL;
832 long min_distance = LONG_MAX;
833
834 for (link = linked_proc_desc_table; link; link = link->next)
835 {
836 long distance = (CORE_ADDR) PROC_DUMMY_FRAME (&link->info) - sp;
837 if (distance > 0 && distance < min_distance)
838 {
839 min_distance = distance;
840 found_proc_desc = &link->info;
841 }
842 }
843 if (found_proc_desc != NULL)
844 return found_proc_desc;
845 }
846
c5aa993b 847 b = block_for_pc (pc);
c906108c
SS
848
849 find_pc_partial_function (pc, NULL, &startaddr, NULL);
850 if (b == NULL)
851 sym = NULL;
852 else
853 {
854 if (startaddr > BLOCK_START (b))
855 /* This is the "pathological" case referred to in a comment in
856 print_frame_info. It might be better to move this check into
857 symbol reading. */
858 sym = NULL;
859 else
860 sym = lookup_symbol (MIPS_EFI_SYMBOL_NAME, b, LABEL_NAMESPACE,
861 0, NULL);
862 }
863
864 /* If we never found a PDR for this function in symbol reading, then
865 examine prologues to find the information. */
866 if (sym && ((mips_extra_func_info_t) SYMBOL_VALUE (sym))->pdr.framereg == -1)
867 sym = NULL;
868
869 if (sym)
870 {
c5aa993b
JM
871 /* IF this is the topmost frame AND
872 * (this proc does not have debugging information OR
873 * the PC is in the procedure prologue)
874 * THEN create a "heuristic" proc_desc (by analyzing
875 * the actual code) to replace the "official" proc_desc.
876 */
877 proc_desc = (alpha_extra_func_info_t) SYMBOL_VALUE (sym);
878 if (next_frame == NULL)
879 {
880 if (PROC_DESC_IS_DUMMY (proc_desc) || alpha_in_prologue (pc, proc_desc))
881 {
882 alpha_extra_func_info_t found_heuristic =
883 heuristic_proc_desc (PROC_LOW_ADDR (proc_desc),
884 pc, next_frame);
885 if (found_heuristic)
886 {
887 PROC_LOCALOFF (found_heuristic) =
888 PROC_LOCALOFF (proc_desc);
889 PROC_PC_REG (found_heuristic) = PROC_PC_REG (proc_desc);
890 proc_desc = found_heuristic;
891 }
892 }
893 }
c906108c
SS
894 }
895 else
896 {
897 long offset;
898
899 /* Is linked_proc_desc_table really necessary? It only seems to be used
c5aa993b
JM
900 by procedure call dummys. However, the procedures being called ought
901 to have their own proc_descs, and even if they don't,
902 heuristic_proc_desc knows how to create them! */
c906108c
SS
903
904 register struct linked_proc_info *link;
905 for (link = linked_proc_desc_table; link; link = link->next)
c5aa993b
JM
906 if (PROC_LOW_ADDR (&link->info) <= pc
907 && PROC_HIGH_ADDR (&link->info) > pc)
908 return &link->info;
c906108c
SS
909
910 /* If PC is inside a dynamically generated sigtramp handler,
c5aa993b 911 create and push a procedure descriptor for that code: */
36a6271d 912 offset = alpha_dynamic_sigtramp_offset (pc);
c906108c
SS
913 if (offset >= 0)
914 return push_sigtramp_desc (pc - offset);
915
916 /* If heuristic_fence_post is non-zero, determine the procedure
c5aa993b
JM
917 start address by examining the instructions.
918 This allows us to find the start address of static functions which
919 have no symbolic information, as startaddr would have been set to
920 the preceding global function start address by the
921 find_pc_partial_function call above. */
c906108c
SS
922 if (startaddr == 0 || heuristic_fence_post != 0)
923 startaddr = heuristic_proc_start (pc);
924
925 proc_desc =
926 heuristic_proc_desc (startaddr, pc, next_frame);
927 }
928 return proc_desc;
929}
930
931alpha_extra_func_info_t cached_proc_desc;
932
dc129d82 933static CORE_ADDR
fba45db2 934alpha_frame_chain (struct frame_info *frame)
c906108c 935{
c5aa993b
JM
936 alpha_extra_func_info_t proc_desc;
937 CORE_ADDR saved_pc = FRAME_SAVED_PC (frame);
938
939 if (saved_pc == 0 || inside_entry_file (saved_pc))
940 return 0;
941
942 proc_desc = find_proc_desc (saved_pc, frame);
943 if (!proc_desc)
944 return 0;
945
946 cached_proc_desc = proc_desc;
947
948 /* Fetch the frame pointer for a dummy frame from the procedure
949 descriptor. */
950 if (PROC_DESC_IS_DUMMY (proc_desc))
951 return (CORE_ADDR) PROC_DUMMY_FRAME (proc_desc);
952
953 /* If no frame pointer and frame size is zero, we must be at end
954 of stack (or otherwise hosed). If we don't check frame size,
955 we loop forever if we see a zero size frame. */
956 if (PROC_FRAME_REG (proc_desc) == SP_REGNUM
957 && PROC_FRAME_OFFSET (proc_desc) == 0
958 /* The previous frame from a sigtramp frame might be frameless
959 and have frame size zero. */
5a203e44 960 && !(get_frame_type (frame) == SIGTRAMP_FRAME))
36a6271d 961 return alpha_frame_past_sigtramp_frame (frame, saved_pc);
c5aa993b
JM
962 else
963 return read_next_frame_reg (frame, PROC_FRAME_REG (proc_desc))
964 + PROC_FRAME_OFFSET (proc_desc);
c906108c
SS
965}
966
967void
140f9984
JT
968alpha_print_extra_frame_info (struct frame_info *fi)
969{
970 if (fi
da50a4b7
AC
971 && get_frame_extra_info (fi)
972 && get_frame_extra_info (fi)->proc_desc
973 && get_frame_extra_info (fi)->proc_desc->pdr.framereg < NUM_REGS)
140f9984 974 printf_filtered (" frame pointer is at %s+%s\n",
da50a4b7
AC
975 REGISTER_NAME (get_frame_extra_info (fi)->proc_desc->pdr.framereg),
976 paddr_d (get_frame_extra_info (fi)->proc_desc->pdr.frameoffset));
140f9984
JT
977}
978
dc129d82 979static void
140f9984 980alpha_init_extra_frame_info (int fromleaf, struct frame_info *frame)
c906108c
SS
981{
982 /* Use proc_desc calculated in frame_chain */
983 alpha_extra_func_info_t proc_desc =
11c02a10
AC
984 get_next_frame (frame)
985 ? cached_proc_desc
986 : find_proc_desc (get_frame_pc (frame), get_next_frame (frame));
c906108c 987
a00a19e9 988 frame_extra_info_zalloc (frame, sizeof (struct frame_extra_info));
140f9984 989
b2fb4676
AC
990 /* NOTE: cagney/2003-01-03: No need to set saved_regs to NULL,
991 always NULL by default. */
992 /* frame->saved_regs = NULL; */
da50a4b7
AC
993 get_frame_extra_info (frame)->localoff = 0;
994 get_frame_extra_info (frame)->pc_reg = ALPHA_RA_REGNUM;
995 get_frame_extra_info (frame)->proc_desc = proc_desc == &temp_proc_desc ? 0 : proc_desc;
c906108c
SS
996 if (proc_desc)
997 {
998 /* Get the locals offset and the saved pc register from the
c5aa993b
JM
999 procedure descriptor, they are valid even if we are in the
1000 middle of the prologue. */
da50a4b7
AC
1001 get_frame_extra_info (frame)->localoff = PROC_LOCALOFF (proc_desc);
1002 get_frame_extra_info (frame)->pc_reg = PROC_PC_REG (proc_desc);
c906108c
SS
1003
1004 /* Fixup frame-pointer - only needed for top frame */
1005
1006 /* Fetch the frame pointer for a dummy frame from the procedure
c5aa993b
JM
1007 descriptor. */
1008 if (PROC_DESC_IS_DUMMY (proc_desc))
8ccd593b 1009 deprecated_update_frame_base_hack (frame, (CORE_ADDR) PROC_DUMMY_FRAME (proc_desc));
c906108c
SS
1010
1011 /* This may not be quite right, if proc has a real frame register.
c5aa993b
JM
1012 Get the value of the frame relative sp, procedure might have been
1013 interrupted by a signal at it's very start. */
50abf9e5 1014 else if (get_frame_pc (frame) == PROC_LOW_ADDR (proc_desc)
36a6271d 1015 && !alpha_proc_desc_is_dyn_sigtramp (proc_desc))
11c02a10 1016 deprecated_update_frame_base_hack (frame, read_next_frame_reg (get_next_frame (frame), SP_REGNUM));
c906108c 1017 else
11c02a10 1018 deprecated_update_frame_base_hack (frame, read_next_frame_reg (get_next_frame (frame), PROC_FRAME_REG (proc_desc))
8ccd593b 1019 + PROC_FRAME_OFFSET (proc_desc));
c906108c
SS
1020
1021 if (proc_desc == &temp_proc_desc)
1022 {
1023 char *name;
1024
1025 /* Do not set the saved registers for a sigtramp frame,
5a203e44
AC
1026 alpha_find_saved_registers will do that for us. We can't
1027 use (get_frame_type (frame) == SIGTRAMP_FRAME), it is not
1028 yet set. */
1029 /* FIXME: cagney/2002-11-18: This problem will go away once
1030 frame.c:get_prev_frame() is modified to set the frame's
1031 type before calling functions like this. */
50abf9e5 1032 find_pc_partial_function (get_frame_pc (frame), &name,
c5aa993b 1033 (CORE_ADDR *) NULL, (CORE_ADDR *) NULL);
50abf9e5 1034 if (!PC_IN_SIGTRAMP (get_frame_pc (frame), name))
c906108c 1035 {
b2fb4676
AC
1036 frame_saved_regs_zalloc (frame);
1037 memcpy (get_frame_saved_regs (frame), temp_saved_regs,
140f9984 1038 SIZEOF_FRAME_SAVED_REGS);
b2fb4676
AC
1039 get_frame_saved_regs (frame)[PC_REGNUM]
1040 = get_frame_saved_regs (frame)[ALPHA_RA_REGNUM];
c906108c
SS
1041 }
1042 }
1043 }
1044}
1045
dc129d82 1046static CORE_ADDR
140f9984
JT
1047alpha_frame_locals_address (struct frame_info *fi)
1048{
da50a4b7 1049 return (get_frame_base (fi) - get_frame_extra_info (fi)->localoff);
140f9984
JT
1050}
1051
dc129d82 1052static CORE_ADDR
140f9984
JT
1053alpha_frame_args_address (struct frame_info *fi)
1054{
1e2330ba 1055 return (get_frame_base (fi) - (ALPHA_NUM_ARG_REGS * 8));
140f9984
JT
1056}
1057
c906108c
SS
1058/* ALPHA stack frames are almost impenetrable. When execution stops,
1059 we basically have to look at symbol information for the function
1060 that we stopped in, which tells us *which* register (if any) is
1061 the base of the frame pointer, and what offset from that register
1062 the frame itself is at.
1063
1064 This presents a problem when trying to examine a stack in memory
1065 (that isn't executing at the moment), using the "frame" command. We
1066 don't have a PC, nor do we have any registers except SP.
1067
1068 This routine takes two arguments, SP and PC, and tries to make the
1069 cached frames look as if these two arguments defined a frame on the
1070 cache. This allows the rest of info frame to extract the important
1071 arguments without difficulty. */
1072
1073struct frame_info *
a57f9e49 1074alpha_setup_arbitrary_frame (int argc, CORE_ADDR *argv)
c906108c
SS
1075{
1076 if (argc != 2)
1077 error ("ALPHA frame specifications require two arguments: sp and pc");
1078
1079 return create_new_frame (argv[0], argv[1]);
1080}
1081
1082/* The alpha passes the first six arguments in the registers, the rest on
1083 the stack. The register arguments are eventually transferred to the
1084 argument transfer area immediately below the stack by the called function
1085 anyway. So we `push' at least six arguments on the stack, `reload' the
1086 argument registers and then adjust the stack pointer to point past the
1087 sixth argument. This algorithm simplifies the passing of a large struct
1088 which extends from the registers to the stack.
1089 If the called function is returning a structure, the address of the
1090 structure to be returned is passed as a hidden first argument. */
1091
dc129d82 1092static CORE_ADDR
ea7c478f 1093alpha_push_arguments (int nargs, struct value **args, CORE_ADDR sp,
fba45db2 1094 int struct_return, CORE_ADDR struct_addr)
c906108c 1095{
7a292a7a 1096 int i;
c906108c
SS
1097 int accumulate_size = struct_return ? 8 : 0;
1098 int arg_regs_size = ALPHA_NUM_ARG_REGS * 8;
c5aa993b
JM
1099 struct alpha_arg
1100 {
1101 char *contents;
1102 int len;
1103 int offset;
1104 };
c906108c 1105 struct alpha_arg *alpha_args =
c5aa993b 1106 (struct alpha_arg *) alloca (nargs * sizeof (struct alpha_arg));
c906108c
SS
1107 register struct alpha_arg *m_arg;
1108 char raw_buffer[sizeof (CORE_ADDR)];
1109 int required_arg_regs;
1110
1111 for (i = 0, m_arg = alpha_args; i < nargs; i++, m_arg++)
1112 {
ea7c478f 1113 struct value *arg = args[i];
c906108c
SS
1114 struct type *arg_type = check_typedef (VALUE_TYPE (arg));
1115 /* Cast argument to long if necessary as the compiler does it too. */
1116 switch (TYPE_CODE (arg_type))
1117 {
1118 case TYPE_CODE_INT:
1119 case TYPE_CODE_BOOL:
1120 case TYPE_CODE_CHAR:
1121 case TYPE_CODE_RANGE:
1122 case TYPE_CODE_ENUM:
1123 if (TYPE_LENGTH (arg_type) < TYPE_LENGTH (builtin_type_long))
1124 {
1125 arg_type = builtin_type_long;
1126 arg = value_cast (arg_type, arg);
1127 }
1128 break;
1129 default:
1130 break;
1131 }
1132 m_arg->len = TYPE_LENGTH (arg_type);
1133 m_arg->offset = accumulate_size;
1134 accumulate_size = (accumulate_size + m_arg->len + 7) & ~7;
c5aa993b 1135 m_arg->contents = VALUE_CONTENTS (arg);
c906108c
SS
1136 }
1137
1138 /* Determine required argument register loads, loading an argument register
1139 is expensive as it uses three ptrace calls. */
1140 required_arg_regs = accumulate_size / 8;
1141 if (required_arg_regs > ALPHA_NUM_ARG_REGS)
1142 required_arg_regs = ALPHA_NUM_ARG_REGS;
1143
1144 /* Make room for the arguments on the stack. */
1145 if (accumulate_size < arg_regs_size)
c5aa993b 1146 accumulate_size = arg_regs_size;
c906108c
SS
1147 sp -= accumulate_size;
1148
1149 /* Keep sp aligned to a multiple of 16 as the compiler does it too. */
1150 sp &= ~15;
1151
1152 /* `Push' arguments on the stack. */
c5aa993b
JM
1153 for (i = nargs; m_arg--, --i >= 0;)
1154 write_memory (sp + m_arg->offset, m_arg->contents, m_arg->len);
c906108c
SS
1155 if (struct_return)
1156 {
1157 store_address (raw_buffer, sizeof (CORE_ADDR), struct_addr);
1158 write_memory (sp, raw_buffer, sizeof (CORE_ADDR));
1159 }
1160
1161 /* Load the argument registers. */
1162 for (i = 0; i < required_arg_regs; i++)
1163 {
1164 LONGEST val;
1165
1166 val = read_memory_integer (sp + i * 8, 8);
dc129d82
JT
1167 write_register (ALPHA_A0_REGNUM + i, val);
1168 write_register (ALPHA_FPA0_REGNUM + i, val);
c906108c
SS
1169 }
1170
1171 return sp + arg_regs_size;
1172}
1173
dc129d82 1174static void
fba45db2 1175alpha_push_dummy_frame (void)
c906108c
SS
1176{
1177 int ireg;
1178 struct linked_proc_info *link;
1179 alpha_extra_func_info_t proc_desc;
1180 CORE_ADDR sp = read_register (SP_REGNUM);
1181 CORE_ADDR save_address;
dc129d82 1182 char raw_buffer[ALPHA_MAX_REGISTER_RAW_SIZE];
c906108c
SS
1183 unsigned long mask;
1184
c5aa993b 1185 link = (struct linked_proc_info *) xmalloc (sizeof (struct linked_proc_info));
c906108c
SS
1186 link->next = linked_proc_desc_table;
1187 linked_proc_desc_table = link;
c5aa993b 1188
c906108c
SS
1189 proc_desc = &link->info;
1190
1191 /*
1192 * The registers we must save are all those not preserved across
1193 * procedure calls.
1194 * In addition, we must save the PC and RA.
1195 *
1196 * Dummy frame layout:
1197 * (high memory)
c5aa993b 1198 * Saved PC
c906108c
SS
1199 * Saved F30
1200 * ...
1201 * Saved F0
c5aa993b
JM
1202 * Saved R29
1203 * ...
1204 * Saved R0
1205 * Saved R26 (RA)
1206 * Parameter build area
c906108c
SS
1207 * (low memory)
1208 */
1209
1210/* MASK(i,j) == (1<<i) + (1<<(i+1)) + ... + (1<<j)). Assume i<=j<31. */
1211#define MASK(i,j) ((((LONGEST)1 << ((j)+1)) - 1) ^ (((LONGEST)1 << (i)) - 1))
1212#define GEN_REG_SAVE_MASK (MASK(0,8) | MASK(16,29))
1213#define GEN_REG_SAVE_COUNT 24
1214#define FLOAT_REG_SAVE_MASK (MASK(0,1) | MASK(10,30))
1215#define FLOAT_REG_SAVE_COUNT 23
1216 /* The special register is the PC as we have no bit for it in the save masks.
1217 alpha_frame_saved_pc knows where the pc is saved in a dummy frame. */
1218#define SPECIAL_REG_SAVE_COUNT 1
1219
c5aa993b
JM
1220 PROC_REG_MASK (proc_desc) = GEN_REG_SAVE_MASK;
1221 PROC_FREG_MASK (proc_desc) = FLOAT_REG_SAVE_MASK;
c906108c
SS
1222 /* PROC_REG_OFFSET is the offset from the dummy frame to the saved RA,
1223 but keep SP aligned to a multiple of 16. */
c5aa993b
JM
1224 PROC_REG_OFFSET (proc_desc) =
1225 -((8 * (SPECIAL_REG_SAVE_COUNT
c906108c
SS
1226 + GEN_REG_SAVE_COUNT
1227 + FLOAT_REG_SAVE_COUNT)
c5aa993b
JM
1228 + 15) & ~15);
1229 PROC_FREG_OFFSET (proc_desc) =
1230 PROC_REG_OFFSET (proc_desc) + 8 * GEN_REG_SAVE_COUNT;
c906108c
SS
1231
1232 /* Save general registers.
1233 The return address register is the first saved register, all other
1234 registers follow in ascending order.
1235 The PC is saved immediately below the SP. */
c5aa993b 1236 save_address = sp + PROC_REG_OFFSET (proc_desc);
dc129d82 1237 store_address (raw_buffer, 8, read_register (ALPHA_RA_REGNUM));
c906108c
SS
1238 write_memory (save_address, raw_buffer, 8);
1239 save_address += 8;
c5aa993b 1240 mask = PROC_REG_MASK (proc_desc) & 0xffffffffL;
c906108c
SS
1241 for (ireg = 0; mask; ireg++, mask >>= 1)
1242 if (mask & 1)
1243 {
dc129d82 1244 if (ireg == ALPHA_RA_REGNUM)
c906108c
SS
1245 continue;
1246 store_address (raw_buffer, 8, read_register (ireg));
1247 write_memory (save_address, raw_buffer, 8);
1248 save_address += 8;
1249 }
1250
1251 store_address (raw_buffer, 8, read_register (PC_REGNUM));
1252 write_memory (sp - 8, raw_buffer, 8);
1253
1254 /* Save floating point registers. */
c5aa993b
JM
1255 save_address = sp + PROC_FREG_OFFSET (proc_desc);
1256 mask = PROC_FREG_MASK (proc_desc) & 0xffffffffL;
c906108c
SS
1257 for (ireg = 0; mask; ireg++, mask >>= 1)
1258 if (mask & 1)
1259 {
1260 store_address (raw_buffer, 8, read_register (ireg + FP0_REGNUM));
1261 write_memory (save_address, raw_buffer, 8);
1262 save_address += 8;
1263 }
1264
1265 /* Set and save the frame address for the dummy.
1266 This is tricky. The only registers that are suitable for a frame save
1267 are those that are preserved across procedure calls (s0-s6). But if
1268 a read system call is interrupted and then a dummy call is made
1269 (see testsuite/gdb.t17/interrupt.exp) the dummy call hangs till the read
1270 is satisfied. Then it returns with the s0-s6 registers set to the values
1271 on entry to the read system call and our dummy frame pointer would be
1272 destroyed. So we save the dummy frame in the proc_desc and handle the
1273 retrieval of the frame pointer of a dummy specifically. The frame register
1274 is set to the virtual frame (pseudo) register, it's value will always
1275 be read as zero and will help us to catch any errors in the dummy frame
1276 retrieval code. */
c5aa993b
JM
1277 PROC_DUMMY_FRAME (proc_desc) = sp;
1278 PROC_FRAME_REG (proc_desc) = FP_REGNUM;
1279 PROC_FRAME_OFFSET (proc_desc) = 0;
1280 sp += PROC_REG_OFFSET (proc_desc);
c906108c
SS
1281 write_register (SP_REGNUM, sp);
1282
c5aa993b
JM
1283 PROC_LOW_ADDR (proc_desc) = CALL_DUMMY_ADDRESS ();
1284 PROC_HIGH_ADDR (proc_desc) = PROC_LOW_ADDR (proc_desc) + 4;
c906108c 1285
c5aa993b 1286 SET_PROC_DESC_IS_DUMMY (proc_desc);
dc129d82 1287 PROC_PC_REG (proc_desc) = ALPHA_RA_REGNUM;
c906108c
SS
1288}
1289
dc129d82 1290static void
fba45db2 1291alpha_pop_frame (void)
c906108c
SS
1292{
1293 register int regnum;
1294 struct frame_info *frame = get_current_frame ();
1e2330ba 1295 CORE_ADDR new_sp = get_frame_base (frame);
c906108c 1296
da50a4b7 1297 alpha_extra_func_info_t proc_desc = get_frame_extra_info (frame)->proc_desc;
c906108c 1298
9e0b60a8
JM
1299 /* we need proc_desc to know how to restore the registers;
1300 if it is NULL, construct (a temporary) one */
1301 if (proc_desc == NULL)
11c02a10 1302 proc_desc = find_proc_desc (get_frame_pc (frame), get_next_frame (frame));
9e0b60a8
JM
1303
1304 /* Question: should we copy this proc_desc and save it in
1305 frame->proc_desc? If we do, who will free it?
1306 For now, we don't save a copy... */
1307
c5aa993b 1308 write_register (PC_REGNUM, FRAME_SAVED_PC (frame));
b2fb4676 1309 if (get_frame_saved_regs (frame) == NULL)
c906108c
SS
1310 alpha_find_saved_regs (frame);
1311 if (proc_desc)
1312 {
c5aa993b
JM
1313 for (regnum = 32; --regnum >= 0;)
1314 if (PROC_REG_MASK (proc_desc) & (1 << regnum))
c906108c 1315 write_register (regnum,
b2fb4676 1316 read_memory_integer (get_frame_saved_regs (frame)[regnum],
c906108c 1317 8));
c5aa993b
JM
1318 for (regnum = 32; --regnum >= 0;)
1319 if (PROC_FREG_MASK (proc_desc) & (1 << regnum))
c906108c 1320 write_register (regnum + FP0_REGNUM,
b2fb4676 1321 read_memory_integer (get_frame_saved_regs (frame)[regnum + FP0_REGNUM], 8));
c906108c
SS
1322 }
1323 write_register (SP_REGNUM, new_sp);
1324 flush_cached_frames ();
1325
c5aa993b 1326 if (proc_desc && (PROC_DESC_IS_DUMMY (proc_desc)
36a6271d 1327 || alpha_proc_desc_is_dyn_sigtramp (proc_desc)))
c906108c
SS
1328 {
1329 struct linked_proc_info *pi_ptr, *prev_ptr;
1330
1331 for (pi_ptr = linked_proc_desc_table, prev_ptr = NULL;
1332 pi_ptr != NULL;
1333 prev_ptr = pi_ptr, pi_ptr = pi_ptr->next)
1334 {
1335 if (&pi_ptr->info == proc_desc)
1336 break;
1337 }
1338
1339 if (pi_ptr == NULL)
1340 error ("Can't locate dummy extra frame info\n");
1341
1342 if (prev_ptr != NULL)
1343 prev_ptr->next = pi_ptr->next;
1344 else
1345 linked_proc_desc_table = pi_ptr->next;
1346
b8c9b27d 1347 xfree (pi_ptr);
c906108c
SS
1348 }
1349}
1350\f
1351/* To skip prologues, I use this predicate. Returns either PC itself
1352 if the code at PC does not look like a function prologue; otherwise
1353 returns an address that (if we're lucky) follows the prologue. If
1354 LENIENT, then we must skip everything which is involved in setting
1355 up the frame (it's OK to skip more, just so long as we don't skip
1356 anything which might clobber the registers which are being saved.
0fb34c3a
MS
1357 Currently we must not skip more on the alpha, but we might need the
1358 lenient stuff some day. */
c906108c 1359
f8453e34
JT
1360static CORE_ADDR
1361alpha_skip_prologue_internal (CORE_ADDR pc, int lenient)
c906108c 1362{
c5aa993b
JM
1363 unsigned long inst;
1364 int offset;
1365 CORE_ADDR post_prologue_pc;
1366 char buf[4];
c906108c 1367
c5aa993b
JM
1368 /* Silently return the unaltered pc upon memory errors.
1369 This could happen on OSF/1 if decode_line_1 tries to skip the
1370 prologue for quickstarted shared library functions when the
1371 shared library is not yet mapped in.
1372 Reading target memory is slow over serial lines, so we perform
15d72a92
JT
1373 this check only if the target has shared libraries (which all
1374 Alpha targets do). */
c5aa993b
JM
1375 if (target_read_memory (pc, buf, 4))
1376 return pc;
c906108c 1377
c5aa993b
JM
1378 /* See if we can determine the end of the prologue via the symbol table.
1379 If so, then return either PC, or the PC after the prologue, whichever
1380 is greater. */
c906108c 1381
c5aa993b 1382 post_prologue_pc = after_prologue (pc, NULL);
c906108c 1383
c5aa993b
JM
1384 if (post_prologue_pc != 0)
1385 return max (pc, post_prologue_pc);
c906108c 1386
c5aa993b
JM
1387 /* Can't determine prologue from the symbol table, need to examine
1388 instructions. */
c906108c 1389
c5aa993b
JM
1390 /* Skip the typical prologue instructions. These are the stack adjustment
1391 instruction and the instructions that save registers on the stack
1392 or in the gcc frame. */
1393 for (offset = 0; offset < 100; offset += 4)
1394 {
1395 int status;
1396
1397 status = read_memory_nobpt (pc + offset, buf, 4);
1398 if (status)
1399 memory_error (status, pc + offset);
1400 inst = extract_unsigned_integer (buf, 4);
1401
1402 /* The alpha has no delay slots. But let's keep the lenient stuff,
1403 we might need it for something else in the future. */
1404 if (lenient && 0)
1405 continue;
1406
1407 if ((inst & 0xffff0000) == 0x27bb0000) /* ldah $gp,n($t12) */
1408 continue;
1409 if ((inst & 0xffff0000) == 0x23bd0000) /* lda $gp,n($gp) */
1410 continue;
1411 if ((inst & 0xffff0000) == 0x23de0000) /* lda $sp,n($sp) */
1412 continue;
1413 if ((inst & 0xffe01fff) == 0x43c0153e) /* subq $sp,n,$sp */
1414 continue;
1415
1416 if ((inst & 0xfc1f0000) == 0xb41e0000
1417 && (inst & 0xffff0000) != 0xb7fe0000)
1418 continue; /* stq reg,n($sp) */
1419 /* reg != $zero */
1420 if ((inst & 0xfc1f0000) == 0x9c1e0000
1421 && (inst & 0xffff0000) != 0x9ffe0000)
1422 continue; /* stt reg,n($sp) */
1423 /* reg != $zero */
1424 if (inst == 0x47de040f) /* bis sp,sp,fp */
1425 continue;
1426
1427 break;
c906108c 1428 }
c5aa993b 1429 return pc + offset;
c906108c
SS
1430}
1431
dc129d82 1432static CORE_ADDR
f8453e34
JT
1433alpha_skip_prologue (CORE_ADDR addr)
1434{
1435 return (alpha_skip_prologue_internal (addr, 0));
1436}
1437
c906108c
SS
1438#if 0
1439/* Is address PC in the prologue (loosely defined) for function at
1440 STARTADDR? */
1441
1442static int
fba45db2 1443alpha_in_lenient_prologue (CORE_ADDR startaddr, CORE_ADDR pc)
c906108c 1444{
f8453e34 1445 CORE_ADDR end_prologue = alpha_skip_prologue_internal (startaddr, 1);
c906108c
SS
1446 return pc >= startaddr && pc < end_prologue;
1447}
1448#endif
1449
1450/* The alpha needs a conversion between register and memory format if
1451 the register is a floating point register and
c5aa993b 1452 memory format is float, as the register format must be double
c906108c 1453 or
c5aa993b
JM
1454 memory format is an integer with 4 bytes or less, as the representation
1455 of integers in floating point registers is different. */
dc129d82 1456static void
fba45db2
KB
1457alpha_register_convert_to_virtual (int regnum, struct type *valtype,
1458 char *raw_buffer, char *virtual_buffer)
c906108c
SS
1459{
1460 if (TYPE_LENGTH (valtype) >= REGISTER_RAW_SIZE (regnum))
1461 {
1462 memcpy (virtual_buffer, raw_buffer, REGISTER_VIRTUAL_SIZE (regnum));
1463 return;
1464 }
1465
1466 if (TYPE_CODE (valtype) == TYPE_CODE_FLT)
1467 {
1468 double d = extract_floating (raw_buffer, REGISTER_RAW_SIZE (regnum));
1469 store_floating (virtual_buffer, TYPE_LENGTH (valtype), d);
1470 }
1471 else if (TYPE_CODE (valtype) == TYPE_CODE_INT && TYPE_LENGTH (valtype) <= 4)
1472 {
1473 ULONGEST l;
1474 l = extract_unsigned_integer (raw_buffer, REGISTER_RAW_SIZE (regnum));
1475 l = ((l >> 32) & 0xc0000000) | ((l >> 29) & 0x3fffffff);
1476 store_unsigned_integer (virtual_buffer, TYPE_LENGTH (valtype), l);
1477 }
1478 else
1479 error ("Cannot retrieve value from floating point register");
1480}
1481
dc129d82 1482static void
fba45db2
KB
1483alpha_register_convert_to_raw (struct type *valtype, int regnum,
1484 char *virtual_buffer, char *raw_buffer)
c906108c
SS
1485{
1486 if (TYPE_LENGTH (valtype) >= REGISTER_RAW_SIZE (regnum))
1487 {
1488 memcpy (raw_buffer, virtual_buffer, REGISTER_RAW_SIZE (regnum));
1489 return;
1490 }
1491
1492 if (TYPE_CODE (valtype) == TYPE_CODE_FLT)
1493 {
1494 double d = extract_floating (virtual_buffer, TYPE_LENGTH (valtype));
1495 store_floating (raw_buffer, REGISTER_RAW_SIZE (regnum), d);
1496 }
1497 else if (TYPE_CODE (valtype) == TYPE_CODE_INT && TYPE_LENGTH (valtype) <= 4)
1498 {
1499 ULONGEST l;
1500 if (TYPE_UNSIGNED (valtype))
1501 l = extract_unsigned_integer (virtual_buffer, TYPE_LENGTH (valtype));
1502 else
1503 l = extract_signed_integer (virtual_buffer, TYPE_LENGTH (valtype));
1504 l = ((l & 0xc0000000) << 32) | ((l & 0x3fffffff) << 29);
1505 store_unsigned_integer (raw_buffer, REGISTER_RAW_SIZE (regnum), l);
1506 }
1507 else
1508 error ("Cannot store value in floating point register");
1509}
1510
95b80706
JT
1511static const unsigned char *
1512alpha_breakpoint_from_pc (CORE_ADDR *pcptr, int *lenptr)
1513{
1514 static const unsigned char alpha_breakpoint[] =
1515 { 0x80, 0, 0, 0 }; /* call_pal bpt */
1516
1517 *lenptr = sizeof(alpha_breakpoint);
1518 return (alpha_breakpoint);
1519}
1520
c906108c
SS
1521/* Given a return value in `regbuf' with a type `valtype',
1522 extract and copy its value into `valbuf'. */
1523
dc129d82 1524static void
732a6b2d 1525alpha_extract_return_value (struct type *valtype,
997b20b8 1526 char regbuf[ALPHA_REGISTER_BYTES], char *valbuf)
c906108c
SS
1527{
1528 if (TYPE_CODE (valtype) == TYPE_CODE_FLT)
1529 alpha_register_convert_to_virtual (FP0_REGNUM, valtype,
1530 regbuf + REGISTER_BYTE (FP0_REGNUM),
1531 valbuf);
1532 else
dc129d82
JT
1533 memcpy (valbuf, regbuf + REGISTER_BYTE (ALPHA_V0_REGNUM),
1534 TYPE_LENGTH (valtype));
c906108c
SS
1535}
1536
1537/* Given a return value in `regbuf' with a type `valtype',
1538 write its value into the appropriate register. */
1539
dc129d82 1540static void
fba45db2 1541alpha_store_return_value (struct type *valtype, char *valbuf)
c906108c 1542{
dc129d82
JT
1543 char raw_buffer[ALPHA_MAX_REGISTER_RAW_SIZE];
1544 int regnum = ALPHA_V0_REGNUM;
c906108c 1545 int length = TYPE_LENGTH (valtype);
c5aa993b 1546
c906108c
SS
1547 if (TYPE_CODE (valtype) == TYPE_CODE_FLT)
1548 {
1549 regnum = FP0_REGNUM;
1550 length = REGISTER_RAW_SIZE (regnum);
1551 alpha_register_convert_to_raw (valtype, regnum, valbuf, raw_buffer);
1552 }
1553 else
1554 memcpy (raw_buffer, valbuf, length);
1555
73937e03 1556 deprecated_write_register_bytes (REGISTER_BYTE (regnum), raw_buffer, length);
c906108c
SS
1557}
1558
1559/* Just like reinit_frame_cache, but with the right arguments to be
1560 callable as an sfunc. */
1561
1562static void
fba45db2 1563reinit_frame_cache_sfunc (char *args, int from_tty, struct cmd_list_element *c)
c906108c
SS
1564{
1565 reinit_frame_cache ();
1566}
1567
1568/* This is the definition of CALL_DUMMY_ADDRESS. It's a heuristic that is used
1569 to find a convenient place in the text segment to stick a breakpoint to
1570 detect the completion of a target function call (ala call_function_by_hand).
1571 */
1572
1573CORE_ADDR
fba45db2 1574alpha_call_dummy_address (void)
c906108c
SS
1575{
1576 CORE_ADDR entry;
1577 struct minimal_symbol *sym;
1578
1579 entry = entry_point_address ();
1580
1581 if (entry != 0)
1582 return entry;
1583
1584 sym = lookup_minimal_symbol ("_Prelude", NULL, symfile_objfile);
1585
1586 if (!sym || MSYMBOL_TYPE (sym) != mst_text)
1587 return 0;
1588 else
1589 return SYMBOL_VALUE_ADDRESS (sym) + 4;
ec32e4be
JT
1590}
1591
dc129d82 1592static void
0d056799
JT
1593alpha_fix_call_dummy (char *dummy, CORE_ADDR pc, CORE_ADDR fun, int nargs,
1594 struct value **args, struct type *type, int gcc_p)
1595{
1596 CORE_ADDR bp_address = CALL_DUMMY_ADDRESS ();
1597
1598 if (bp_address == 0)
1599 error ("no place to put call");
dc129d82
JT
1600 write_register (ALPHA_RA_REGNUM, bp_address);
1601 write_register (ALPHA_T12_REGNUM, fun);
0d056799
JT
1602}
1603
ee1f65f0
JT
1604/* On the Alpha, the call dummy code is nevery copied to user space
1605 (see alpha_fix_call_dummy() above). The contents of this do not
1606 matter. */
1607LONGEST alpha_call_dummy_words[] = { 0 };
1608
dc129d82 1609static int
d734c450
JT
1610alpha_use_struct_convention (int gcc_p, struct type *type)
1611{
1612 /* Structures are returned by ref in extra arg0. */
1613 return 1;
1614}
1615
dc129d82 1616static void
0d056799
JT
1617alpha_store_struct_return (CORE_ADDR addr, CORE_ADDR sp)
1618{
1619 /* Store the address of the place in which to copy the structure the
1620 subroutine will return. Handled by alpha_push_arguments. */
1621}
1622
dc129d82 1623static CORE_ADDR
0d056799
JT
1624alpha_extract_struct_value_address (char *regbuf)
1625{
dc129d82
JT
1626 return (extract_address (regbuf + REGISTER_BYTE (ALPHA_V0_REGNUM),
1627 REGISTER_RAW_SIZE (ALPHA_V0_REGNUM)));
0d056799
JT
1628}
1629
accc6d1f
JT
1630/* Figure out where the longjmp will land.
1631 We expect the first arg to be a pointer to the jmp_buf structure from
1632 which we extract the PC (JB_PC) that we will land at. The PC is copied
1633 into the "pc". This routine returns true on success. */
1634
1635static int
1636alpha_get_longjmp_target (CORE_ADDR *pc)
1637{
1638 struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch);
1639 CORE_ADDR jb_addr;
1640 char raw_buffer[ALPHA_MAX_REGISTER_RAW_SIZE];
1641
1642 jb_addr = read_register (ALPHA_A0_REGNUM);
1643
1644 if (target_read_memory (jb_addr + (tdep->jb_pc * tdep->jb_elt_size),
1645 raw_buffer, tdep->jb_elt_size))
1646 return 0;
1647
1648 *pc = extract_address (raw_buffer, tdep->jb_elt_size);
1649 return 1;
1650}
1651
ec32e4be
JT
1652/* alpha_software_single_step() is called just before we want to resume
1653 the inferior, if we want to single-step it but there is no hardware
1654 or kernel single-step support (NetBSD on Alpha, for example). We find
1655 the target of the coming instruction and breakpoint it.
1656
1657 single_step is also called just after the inferior stops. If we had
1658 set up a simulated single-step, we undo our damage. */
1659
1660static CORE_ADDR
1661alpha_next_pc (CORE_ADDR pc)
1662{
1663 unsigned int insn;
1664 unsigned int op;
1665 int offset;
1666 LONGEST rav;
1667
1668 insn = read_memory_unsigned_integer (pc, sizeof (insn));
1669
1670 /* Opcode is top 6 bits. */
1671 op = (insn >> 26) & 0x3f;
1672
1673 if (op == 0x1a)
1674 {
1675 /* Jump format: target PC is:
1676 RB & ~3 */
1677 return (read_register ((insn >> 16) & 0x1f) & ~3);
1678 }
1679
1680 if ((op & 0x30) == 0x30)
1681 {
1682 /* Branch format: target PC is:
1683 (new PC) + (4 * sext(displacement)) */
1684 if (op == 0x30 || /* BR */
1685 op == 0x34) /* BSR */
1686 {
1687 branch_taken:
1688 offset = (insn & 0x001fffff);
1689 if (offset & 0x00100000)
1690 offset |= 0xffe00000;
1691 offset *= 4;
1692 return (pc + 4 + offset);
1693 }
1694
1695 /* Need to determine if branch is taken; read RA. */
1696 rav = (LONGEST) read_register ((insn >> 21) & 0x1f);
1697 switch (op)
1698 {
1699 case 0x38: /* BLBC */
1700 if ((rav & 1) == 0)
1701 goto branch_taken;
1702 break;
1703 case 0x3c: /* BLBS */
1704 if (rav & 1)
1705 goto branch_taken;
1706 break;
1707 case 0x39: /* BEQ */
1708 if (rav == 0)
1709 goto branch_taken;
1710 break;
1711 case 0x3d: /* BNE */
1712 if (rav != 0)
1713 goto branch_taken;
1714 break;
1715 case 0x3a: /* BLT */
1716 if (rav < 0)
1717 goto branch_taken;
1718 break;
1719 case 0x3b: /* BLE */
1720 if (rav <= 0)
1721 goto branch_taken;
1722 break;
1723 case 0x3f: /* BGT */
1724 if (rav > 0)
1725 goto branch_taken;
1726 break;
1727 case 0x3e: /* BGE */
1728 if (rav >= 0)
1729 goto branch_taken;
1730 break;
1731 }
1732 }
1733
1734 /* Not a branch or branch not taken; target PC is:
1735 pc + 4 */
1736 return (pc + 4);
1737}
1738
1739void
1740alpha_software_single_step (enum target_signal sig, int insert_breakpoints_p)
1741{
1742 static CORE_ADDR next_pc;
1743 typedef char binsn_quantum[BREAKPOINT_MAX];
1744 static binsn_quantum break_mem;
1745 CORE_ADDR pc;
1746
1747 if (insert_breakpoints_p)
1748 {
1749 pc = read_pc ();
1750 next_pc = alpha_next_pc (pc);
1751
1752 target_insert_breakpoint (next_pc, break_mem);
1753 }
1754 else
1755 {
1756 target_remove_breakpoint (next_pc, break_mem);
1757 write_pc (next_pc);
1758 }
c906108c
SS
1759}
1760
dc129d82 1761\f
44dffaac 1762
dc129d82
JT
1763/* Initialize the current architecture based on INFO. If possible, re-use an
1764 architecture from ARCHES, which is a list of architectures already created
1765 during this debugging session.
1766
1767 Called e.g. at program startup, when reading a core file, and when reading
1768 a binary file. */
1769
1770static struct gdbarch *
1771alpha_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
1772{
1773 struct gdbarch_tdep *tdep;
1774 struct gdbarch *gdbarch;
dc129d82
JT
1775
1776 /* Try to determine the ABI of the object we are loading. */
4be87837 1777 if (info.abfd != NULL && info.osabi == GDB_OSABI_UNKNOWN)
dc129d82 1778 {
4be87837
DJ
1779 /* If it's an ECOFF file, assume it's OSF/1. */
1780 if (bfd_get_flavour (info.abfd) == bfd_target_ecoff_flavour)
aff87235 1781 info.osabi = GDB_OSABI_OSF1;
dc129d82
JT
1782 }
1783
1784 /* Find a candidate among extant architectures. */
4be87837
DJ
1785 arches = gdbarch_list_lookup_by_info (arches, &info);
1786 if (arches != NULL)
1787 return arches->gdbarch;
dc129d82
JT
1788
1789 tdep = xmalloc (sizeof (struct gdbarch_tdep));
1790 gdbarch = gdbarch_alloc (&info, tdep);
1791
d9b023cc
JT
1792 /* Lowest text address. This is used by heuristic_proc_start() to
1793 decide when to stop looking. */
1794 tdep->vm_min_address = (CORE_ADDR) 0x120000000;
1795
36a6271d
JT
1796 tdep->dynamic_sigtramp_offset = NULL;
1797 tdep->skip_sigtramp_frame = NULL;
5868c862 1798 tdep->sigcontext_addr = NULL;
36a6271d 1799
accc6d1f
JT
1800 tdep->jb_pc = -1; /* longjmp support not enabled by default */
1801
dc129d82
JT
1802 /* Type sizes */
1803 set_gdbarch_short_bit (gdbarch, 16);
1804 set_gdbarch_int_bit (gdbarch, 32);
1805 set_gdbarch_long_bit (gdbarch, 64);
1806 set_gdbarch_long_long_bit (gdbarch, 64);
1807 set_gdbarch_float_bit (gdbarch, 32);
1808 set_gdbarch_double_bit (gdbarch, 64);
1809 set_gdbarch_long_double_bit (gdbarch, 64);
1810 set_gdbarch_ptr_bit (gdbarch, 64);
1811
1812 /* Register info */
1813 set_gdbarch_num_regs (gdbarch, ALPHA_NUM_REGS);
1814 set_gdbarch_sp_regnum (gdbarch, ALPHA_SP_REGNUM);
1815 set_gdbarch_fp_regnum (gdbarch, ALPHA_FP_REGNUM);
1816 set_gdbarch_pc_regnum (gdbarch, ALPHA_PC_REGNUM);
1817 set_gdbarch_fp0_regnum (gdbarch, ALPHA_FP0_REGNUM);
1818
1819 set_gdbarch_register_name (gdbarch, alpha_register_name);
1820 set_gdbarch_register_size (gdbarch, ALPHA_REGISTER_SIZE);
1821 set_gdbarch_register_bytes (gdbarch, ALPHA_REGISTER_BYTES);
1822 set_gdbarch_register_byte (gdbarch, alpha_register_byte);
1823 set_gdbarch_register_raw_size (gdbarch, alpha_register_raw_size);
1824 set_gdbarch_max_register_raw_size (gdbarch, ALPHA_MAX_REGISTER_RAW_SIZE);
1825 set_gdbarch_register_virtual_size (gdbarch, alpha_register_virtual_size);
1826 set_gdbarch_max_register_virtual_size (gdbarch,
1827 ALPHA_MAX_REGISTER_VIRTUAL_SIZE);
1828 set_gdbarch_register_virtual_type (gdbarch, alpha_register_virtual_type);
1829
1830 set_gdbarch_cannot_fetch_register (gdbarch, alpha_cannot_fetch_register);
1831 set_gdbarch_cannot_store_register (gdbarch, alpha_cannot_store_register);
1832
1833 set_gdbarch_register_convertible (gdbarch, alpha_register_convertible);
1834 set_gdbarch_register_convert_to_virtual (gdbarch,
1835 alpha_register_convert_to_virtual);
1836 set_gdbarch_register_convert_to_raw (gdbarch, alpha_register_convert_to_raw);
1837
1838 set_gdbarch_skip_prologue (gdbarch, alpha_skip_prologue);
1839
1840 set_gdbarch_frame_num_args (gdbarch, frame_num_args_unknown);
1841 set_gdbarch_frameless_function_invocation (gdbarch,
1842 generic_frameless_function_invocation_not);
1843
1844 set_gdbarch_saved_pc_after_call (gdbarch, alpha_saved_pc_after_call);
1845
1846 set_gdbarch_frame_chain (gdbarch, alpha_frame_chain);
dc129d82
JT
1847 set_gdbarch_frame_saved_pc (gdbarch, alpha_frame_saved_pc);
1848
1849 set_gdbarch_frame_init_saved_regs (gdbarch, alpha_frame_init_saved_regs);
dc129d82
JT
1850
1851 set_gdbarch_use_struct_convention (gdbarch, alpha_use_struct_convention);
26e9b323 1852 set_gdbarch_deprecated_extract_return_value (gdbarch, alpha_extract_return_value);
dc129d82
JT
1853
1854 set_gdbarch_store_struct_return (gdbarch, alpha_store_struct_return);
ebba8386 1855 set_gdbarch_deprecated_store_return_value (gdbarch, alpha_store_return_value);
26e9b323 1856 set_gdbarch_deprecated_extract_struct_value_address (gdbarch,
dc129d82
JT
1857 alpha_extract_struct_value_address);
1858
1859 /* Settings for calling functions in the inferior. */
07555a72 1860 set_gdbarch_deprecated_use_generic_dummy_frames (gdbarch, 0);
dc129d82
JT
1861 set_gdbarch_call_dummy_length (gdbarch, 0);
1862 set_gdbarch_push_arguments (gdbarch, alpha_push_arguments);
1863 set_gdbarch_pop_frame (gdbarch, alpha_pop_frame);
1864
1865 /* On the Alpha, the call dummy code is never copied to user space,
1866 stopping the user call is achieved via a bp_call_dummy breakpoint.
1867 But we need a fake CALL_DUMMY definition to enable the proper
1868 call_function_by_hand and to avoid zero length array warnings. */
1869 set_gdbarch_call_dummy_p (gdbarch, 1);
1870 set_gdbarch_call_dummy_words (gdbarch, alpha_call_dummy_words);
1871 set_gdbarch_sizeof_call_dummy_words (gdbarch, 0);
1872 set_gdbarch_frame_args_address (gdbarch, alpha_frame_args_address);
1873 set_gdbarch_frame_locals_address (gdbarch, alpha_frame_locals_address);
1874 set_gdbarch_init_extra_frame_info (gdbarch, alpha_init_extra_frame_info);
1875
1876 /* Alpha OSF/1 inhibits execution of code on the stack. But there is
1877 no need for a dummy on the Alpha. PUSH_ARGUMENTS takes care of all
1878 argument handling and bp_call_dummy takes care of stopping the dummy. */
dc129d82
JT
1879 set_gdbarch_call_dummy_address (gdbarch, alpha_call_dummy_address);
1880 set_gdbarch_call_dummy_breakpoint_offset_p (gdbarch, 1);
1881 set_gdbarch_call_dummy_breakpoint_offset (gdbarch, 0);
1882 set_gdbarch_call_dummy_start_offset (gdbarch, 0);
ae45cd16 1883 set_gdbarch_deprecated_pc_in_call_dummy (gdbarch, deprecated_pc_in_call_dummy_at_entry_point);
dc129d82
JT
1884 set_gdbarch_call_dummy_stack_adjust_p (gdbarch, 0);
1885 set_gdbarch_push_dummy_frame (gdbarch, alpha_push_dummy_frame);
1886 set_gdbarch_fix_call_dummy (gdbarch, alpha_fix_call_dummy);
a5afb99f 1887 set_gdbarch_deprecated_init_frame_pc (gdbarch, init_frame_pc_noop);
2ca6c561 1888 set_gdbarch_deprecated_init_frame_pc_first (gdbarch, alpha_init_frame_pc_first);
dc129d82
JT
1889
1890 set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
36a6271d 1891 set_gdbarch_skip_trampoline_code (gdbarch, find_solib_trampoline_target);
dc129d82 1892
95b80706 1893 set_gdbarch_breakpoint_from_pc (gdbarch, alpha_breakpoint_from_pc);
dc129d82 1894 set_gdbarch_decr_pc_after_break (gdbarch, 4);
95b80706
JT
1895
1896 set_gdbarch_function_start_offset (gdbarch, 0);
dc129d82
JT
1897 set_gdbarch_frame_args_skip (gdbarch, 0);
1898
44dffaac 1899 /* Hook in ABI-specific overrides, if they have been registered. */
4be87837 1900 gdbarch_init_osabi (info, gdbarch);
44dffaac 1901
accc6d1f
JT
1902 /* Now that we have tuned the configuration, set a few final things
1903 based on what the OS ABI has told us. */
1904
1905 if (tdep->jb_pc >= 0)
1906 set_gdbarch_get_longjmp_target (gdbarch, alpha_get_longjmp_target);
1907
dc129d82
JT
1908 return gdbarch;
1909}
1910
1911static void
1912alpha_dump_tdep (struct gdbarch *current_gdbarch, struct ui_file *file)
1913{
1914 struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch);
1915
1916 if (tdep == NULL)
1917 return;
1918
d9b023cc
JT
1919 fprintf_unfiltered (file,
1920 "alpha_dump_tdep: vm_min_address = 0x%lx\n",
1921 (long) tdep->vm_min_address);
accc6d1f
JT
1922
1923 fprintf_unfiltered (file,
1924 "alpha_dump_tdep: jb_pc = %d\n",
1925 tdep->jb_pc);
1926 fprintf_unfiltered (file,
1927 "alpha_dump_tdep: jb_elt_size = %ld\n",
1928 (long) tdep->jb_elt_size);
dc129d82
JT
1929}
1930
c906108c 1931void
fba45db2 1932_initialize_alpha_tdep (void)
c906108c
SS
1933{
1934 struct cmd_list_element *c;
1935
dc129d82
JT
1936 gdbarch_register (bfd_arch_alpha, alpha_gdbarch_init, alpha_dump_tdep);
1937
c906108c
SS
1938 tm_print_insn = print_insn_alpha;
1939
1940 /* Let the user set the fence post for heuristic_proc_start. */
1941
1942 /* We really would like to have both "0" and "unlimited" work, but
1943 command.c doesn't deal with that. So make it a var_zinteger
1944 because the user can always use "999999" or some such for unlimited. */
1945 c = add_set_cmd ("heuristic-fence-post", class_support, var_zinteger,
1946 (char *) &heuristic_fence_post,
1947 "\
1948Set the distance searched for the start of a function.\n\
1949If you are debugging a stripped executable, GDB needs to search through the\n\
1950program for the start of a function. This command sets the distance of the\n\
1951search. The only need to set it is when debugging a stripped executable.",
1952 &setlist);
1953 /* We need to throw away the frame cache when we set this, since it
1954 might change our ability to get backtraces. */
9f60d481 1955 set_cmd_sfunc (c, reinit_frame_cache_sfunc);
c906108c
SS
1956 add_show_from_set (c, &showlist);
1957}