1 /* Target-dependent code for the VAX.
3 Copyright 1986, 1989, 1991, 1992, 1995, 1996, 1998, 1999, 2000,
4 2002, 2003, 2004 Free Software Foundation, Inc.
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
24 #include "arch-utils.h"
27 #include "frame-base.h"
28 #include "frame-unwind.h"
34 #include "trad-frame.h"
37 #include "gdb_string.h"
41 /* Return the name of register REGNUM. */
44 vax_register_name (int regnum)
46 static char *register_names[] =
48 "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
49 "r8", "r9", "r10", "r11", "ap", "fp", "sp", "pc",
53 if (regnum >= 0 && regnum < ARRAY_SIZE (register_names))
54 return register_names[regnum];
59 /* Return the GDB type object for the "standard" data type of data in
63 vax_register_type (struct gdbarch *gdbarch, int regnum)
65 return builtin_type_int;
68 /* Core file support. */
70 /* Supply register REGNUM from the buffer specified by GREGS and LEN
71 in the general-purpose register set REGSET to register cache
72 REGCACHE. If REGNUM is -1, do this for all registers in REGSET. */
75 vax_supply_gregset (const struct regset *regset, struct regcache *regcache,
76 int regnum, const void *gregs, size_t len)
78 const char *regs = gregs;
81 for (i = 0; i < VAX_NUM_REGS; i++)
83 if (regnum == i || regnum == -1)
84 regcache_raw_supply (regcache, i, regs + i * 4);
88 /* VAX register set. */
90 static struct regset vax_gregset =
96 /* Return the appropriate register set for the core section identified
97 by SECT_NAME and SECT_SIZE. */
99 static const struct regset *
100 vax_regset_from_core_section (struct gdbarch *gdbarch,
101 const char *sect_name, size_t sect_size)
103 if (strcmp (sect_name, ".reg") == 0 && sect_size >= VAX_NUM_REGS * 4)
109 /* The VAX Unix calling convention uses R1 to pass a structure return
110 value address instead of passing it as a first (hidden) argument as
111 the VMS calling convention suggests. */
114 vax_store_arguments (struct regcache *regcache, int nargs,
115 struct value **args, CORE_ADDR sp)
121 /* We create an argument list on the stack, and make the argument
124 /* Push arguments in reverse order. */
125 for (i = nargs - 1; i >= 0; i--)
127 int len = TYPE_LENGTH (VALUE_ENCLOSING_TYPE (args[i]));
129 sp -= (len + 3) & ~3;
130 count += (len + 3) / 4;
131 write_memory (sp, VALUE_CONTENTS_ALL (args[i]), len);
134 /* Push argument count. */
136 store_unsigned_integer (buf, 4, count);
137 write_memory (sp, buf, 4);
139 /* Update the argument pointer. */
140 store_unsigned_integer (buf, 4, sp);
141 regcache_cooked_write (regcache, VAX_AP_REGNUM, buf);
147 vax_push_dummy_call (struct gdbarch *gdbarch, CORE_ADDR func_addr,
148 struct regcache *regcache, CORE_ADDR bp_addr, int nargs,
149 struct value **args, CORE_ADDR sp, int struct_return,
150 CORE_ADDR struct_addr)
155 /* Set up the function arguments. */
156 sp = vax_store_arguments (regcache, nargs, args, sp);
158 /* Store return value address. */
160 regcache_cooked_write_unsigned (regcache, VAX_R1_REGNUM, struct_addr);
162 /* Store return address in the PC slot. */
164 store_unsigned_integer (buf, 4, bp_addr);
165 write_memory (sp, buf, 4);
167 /* Store the (fake) frame pointer in the FP slot. */
169 store_unsigned_integer (buf, 4, fp);
170 write_memory (sp, buf, 4);
172 /* Skip the AP slot. */
175 /* Store register save mask and control bits. */
177 store_unsigned_integer (buf, 4, 0);
178 write_memory (sp, buf, 4);
180 /* Store condition handler. */
182 store_unsigned_integer (buf, 4, 0);
183 write_memory (sp, buf, 4);
185 /* Update the stack pointer and frame pointer. */
186 store_unsigned_integer (buf, 4, sp);
187 regcache_cooked_write (regcache, VAX_SP_REGNUM, buf);
188 regcache_cooked_write (regcache, VAX_FP_REGNUM, buf);
190 /* Return the saved (fake) frame pointer. */
194 static struct frame_id
195 vax_unwind_dummy_id (struct gdbarch *gdbarch, struct frame_info *next_frame)
199 fp = frame_unwind_register_unsigned (next_frame, VAX_FP_REGNUM);
200 return frame_id_build (fp, frame_pc_unwind (next_frame));
204 static enum return_value_convention
205 vax_return_value (struct gdbarch *gdbarch, struct type *type,
206 struct regcache *regcache, void *readbuf,
207 const void *writebuf)
209 int len = TYPE_LENGTH (type);
212 if (TYPE_CODE (type) == TYPE_CODE_STRUCT
213 || TYPE_CODE (type) == TYPE_CODE_STRUCT
214 || TYPE_CODE (type) == TYPE_CODE_ARRAY)
215 return RETURN_VALUE_STRUCT_CONVENTION;
219 /* Read the contents of R0 and (if necessary) R1. */
220 regcache_cooked_read (regcache, VAX_R0_REGNUM, buf);
222 regcache_cooked_read (regcache, VAX_R1_REGNUM, buf + 4);
223 memcpy (readbuf, buf, len);
227 /* Read the contents to R0 and (if necessary) R1. */
228 memcpy (buf, writebuf, len);
229 regcache_cooked_write (regcache, VAX_R0_REGNUM, buf);
231 regcache_cooked_write (regcache, VAX_R1_REGNUM, buf + 4);
234 return RETURN_VALUE_REGISTER_CONVENTION;
238 /* Use the program counter to determine the contents and size of a
239 breakpoint instruction. Return a pointer to a string of bytes that
240 encode a breakpoint instruction, store the length of the string in
241 *LEN and optionally adjust *PC to point to the correct memory
242 location for inserting the breakpoint. */
244 static const unsigned char *
245 vax_breakpoint_from_pc (CORE_ADDR *pc, int *len)
247 static unsigned char break_insn[] = { 3 };
249 *len = sizeof (break_insn);
253 /* Advance PC across any function entry prologue instructions
254 to reach some "real" code. */
257 vax_skip_prologue (CORE_ADDR pc)
259 unsigned char op = read_memory_unsigned_integer (pc, 1);
262 pc += 2; /* skip brb */
264 pc += 3; /* skip brw */
266 && (read_memory_unsigned_integer (pc + 2, 1)) == 0x5E)
267 pc += 3; /* skip subl2 */
269 && (read_memory_unsigned_integer (pc + 1, 1)) == 0xAE
270 && (read_memory_unsigned_integer (pc + 3, 1)) == 0x5E)
271 pc += 4; /* skip movab */
273 && (read_memory_unsigned_integer (pc + 1, 1)) == 0xCE
274 && (read_memory_unsigned_integer (pc + 4, 1)) == 0x5E)
275 pc += 5; /* skip movab */
277 && (read_memory_unsigned_integer (pc + 1, 1)) == 0xEE
278 && (read_memory_unsigned_integer (pc + 6, 1)) == 0x5E)
279 pc += 7; /* skip movab */
285 /* Unwinding the stack is relatively easy since the VAX has a
286 dedicated frame pointer, and frames are set up automatically as the
287 result of a function call. Most of the relevant information can be
288 inferred from the documentation of the Procedure Call Instructions
289 in the VAX MACRO and Instruction Set Reference Manual. */
291 struct vax_frame_cache
296 /* Table of saved registers. */
297 struct trad_frame_saved_reg *saved_regs;
300 struct vax_frame_cache *
301 vax_frame_cache (struct frame_info *next_frame, void **this_cache)
303 struct vax_frame_cache *cache;
311 /* Allocate a new cache. */
312 cache = FRAME_OBSTACK_ZALLOC (struct vax_frame_cache);
313 cache->saved_regs = trad_frame_alloc_saved_regs (next_frame);
315 /* The frame pointer is used as the base for the frame. */
316 cache->base = frame_unwind_register_unsigned (next_frame, VAX_FP_REGNUM);
317 if (cache->base == 0)
320 /* The register save mask and control bits determine the layout of
322 mask = get_frame_memory_unsigned (next_frame, cache->base + 4, 4) >> 16;
324 /* These are always saved. */
325 cache->saved_regs[VAX_PC_REGNUM].addr = cache->base + 16;
326 cache->saved_regs[VAX_FP_REGNUM].addr = cache->base + 12;
327 cache->saved_regs[VAX_AP_REGNUM].addr = cache->base + 8;
328 cache->saved_regs[VAX_PS_REGNUM].addr = cache->base + 4;
330 /* Scan the register save mask and record the location of the saved
332 addr = cache->base + 20;
333 for (regnum = 0; regnum < VAX_AP_REGNUM; regnum++)
335 if (mask & (1 << regnum))
337 cache->saved_regs[regnum].addr = addr;
342 /* The CALLS/CALLG flag determines whether this frame has a General
343 Argument List or a Stack Argument List. */
344 if (mask & (1 << 13))
348 /* This is a procedure with Stack Argument List. Adjust the
349 stack address for the arguments that were pushed onto the
350 stack. The return instruction will automatically pop the
351 arguments from the stack. */
352 numarg = get_frame_memory_unsigned (next_frame, addr, 1);
353 addr += 4 + numarg * 4;
356 /* Bits 1:0 of the stack pointer were saved in the control bits. */
357 trad_frame_set_value (cache->saved_regs, VAX_SP_REGNUM, addr + (mask >> 14));
363 vax_frame_this_id (struct frame_info *next_frame, void **this_cache,
364 struct frame_id *this_id)
366 struct vax_frame_cache *cache = vax_frame_cache (next_frame, this_cache);
368 /* This marks the outermost frame. */
369 if (cache->base == 0)
372 (*this_id) = frame_id_build (cache->base, frame_pc_unwind (next_frame));
376 vax_frame_prev_register (struct frame_info *next_frame, void **this_cache,
377 int regnum, int *optimizedp,
378 enum lval_type *lvalp, CORE_ADDR *addrp,
379 int *realnump, void *valuep)
381 struct vax_frame_cache *cache = vax_frame_cache (next_frame, this_cache);
383 trad_frame_prev_register (next_frame, cache->saved_regs, regnum,
384 optimizedp, lvalp, addrp, realnump, valuep);
387 static const struct frame_unwind vax_frame_unwind =
391 vax_frame_prev_register
394 static const struct frame_unwind *
395 vax_frame_sniffer (struct frame_info *next_frame)
397 return &vax_frame_unwind;
402 vax_frame_base_address (struct frame_info *next_frame, void **this_cache)
404 struct vax_frame_cache *cache = vax_frame_cache (next_frame, this_cache);
410 vax_frame_args_address (struct frame_info *next_frame, void **this_cache)
412 return frame_unwind_register_unsigned (next_frame, VAX_AP_REGNUM);
415 static const struct frame_base vax_frame_base =
418 vax_frame_base_address,
419 vax_frame_base_address,
420 vax_frame_args_address
423 /* Return number of arguments for FRAME. */
426 vax_frame_num_args (struct frame_info *frame)
430 /* Assume that the argument pointer for the outermost frame is
431 hosed, as is the case on NetBSD/vax ELF. */
432 if (get_frame_base_address (frame) == 0)
435 args = get_frame_register_unsigned (frame, VAX_AP_REGNUM);
436 return get_frame_memory_unsigned (frame, args, 1);
440 vax_unwind_pc (struct gdbarch *gdbarch, struct frame_info *next_frame)
442 return frame_unwind_register_unsigned (next_frame, VAX_PC_REGNUM);
446 /* Initialize the current architecture based on INFO. If possible, re-use an
447 architecture from ARCHES, which is a list of architectures already created
448 during this debugging session.
450 Called e.g. at program startup, when reading a core file, and when reading
453 static struct gdbarch *
454 vax_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
456 struct gdbarch *gdbarch;
458 /* If there is already a candidate, use it. */
459 arches = gdbarch_list_lookup_by_info (arches, &info);
461 return arches->gdbarch;
463 gdbarch = gdbarch_alloc (&info, NULL);
466 set_gdbarch_num_regs (gdbarch, VAX_NUM_REGS);
467 set_gdbarch_register_name (gdbarch, vax_register_name);
468 set_gdbarch_register_type (gdbarch, vax_register_type);
469 set_gdbarch_sp_regnum (gdbarch, VAX_SP_REGNUM);
470 set_gdbarch_pc_regnum (gdbarch, VAX_PC_REGNUM);
471 set_gdbarch_ps_regnum (gdbarch, VAX_PS_REGNUM);
473 set_gdbarch_regset_from_core_section
474 (gdbarch, vax_regset_from_core_section);
476 /* Frame and stack info */
477 set_gdbarch_skip_prologue (gdbarch, vax_skip_prologue);
478 set_gdbarch_frame_num_args (gdbarch, vax_frame_num_args);
479 set_gdbarch_frame_args_skip (gdbarch, 4);
481 /* Stack grows downward. */
482 set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
484 /* Return value info */
485 set_gdbarch_return_value (gdbarch, vax_return_value);
487 /* Call dummy code. */
488 set_gdbarch_push_dummy_call (gdbarch, vax_push_dummy_call);
489 set_gdbarch_unwind_dummy_id (gdbarch, vax_unwind_dummy_id);
491 /* Breakpoint info */
492 set_gdbarch_breakpoint_from_pc (gdbarch, vax_breakpoint_from_pc);
495 set_gdbarch_function_start_offset (gdbarch, 2);
496 set_gdbarch_believe_pcc_promotion (gdbarch, 1);
498 set_gdbarch_print_insn (gdbarch, print_insn_vax);
500 set_gdbarch_unwind_pc (gdbarch, vax_unwind_pc);
502 frame_base_set_default (gdbarch, &vax_frame_base);
504 /* Hook in ABI-specific overrides, if they have been registered. */
505 gdbarch_init_osabi (info, gdbarch);
507 frame_unwind_append_sniffer (gdbarch, vax_frame_sniffer);
512 /* Provide a prototype to silence -Wmissing-prototypes. */
513 void _initialize_vax_tdep (void);
516 _initialize_vax_tdep (void)
518 gdbarch_register (bfd_arch_vax, vax_gdbarch_init, NULL);