1 /* Target-dependent code for the VAX.
3 Copyright (C) 1986, 1989, 1991, 1992, 1995, 1996, 1998, 1999, 2000,
4 2002, 2003, 2004, 2005 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., 51 Franklin Street, Fifth Floor,
21 Boston, MA 02110-1301, USA. */
24 #include "arch-utils.h"
26 #include "floatformat.h"
28 #include "frame-base.h"
29 #include "frame-unwind.h"
35 #include "trad-frame.h"
38 #include "gdb_string.h"
42 /* Return the name of register REGNUM. */
45 vax_register_name (int regnum)
47 static char *register_names[] =
49 "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
50 "r8", "r9", "r10", "r11", "ap", "fp", "sp", "pc",
54 if (regnum >= 0 && regnum < ARRAY_SIZE (register_names))
55 return register_names[regnum];
60 /* Return the GDB type object for the "standard" data type of data in
64 vax_register_type (struct gdbarch *gdbarch, int regnum)
66 return builtin_type_int;
69 /* Core file support. */
71 /* Supply register REGNUM from the buffer specified by GREGS and LEN
72 in the general-purpose register set REGSET to register cache
73 REGCACHE. If REGNUM is -1, do this for all registers in REGSET. */
76 vax_supply_gregset (const struct regset *regset, struct regcache *regcache,
77 int regnum, const void *gregs, size_t len)
79 const gdb_byte *regs = gregs;
82 for (i = 0; i < VAX_NUM_REGS; i++)
84 if (regnum == i || regnum == -1)
85 regcache_raw_supply (regcache, i, regs + i * 4);
89 /* VAX register set. */
91 static struct regset vax_gregset =
97 /* Return the appropriate register set for the core section identified
98 by SECT_NAME and SECT_SIZE. */
100 static const struct regset *
101 vax_regset_from_core_section (struct gdbarch *gdbarch,
102 const char *sect_name, size_t sect_size)
104 if (strcmp (sect_name, ".reg") == 0 && sect_size >= VAX_NUM_REGS * 4)
110 /* The VAX UNIX calling convention uses R1 to pass a structure return
111 value address instead of passing it as a first (hidden) argument as
112 the VMS calling convention suggests. */
115 vax_store_arguments (struct regcache *regcache, int nargs,
116 struct value **args, CORE_ADDR sp)
122 /* We create an argument list on the stack, and make the argument
125 /* Push arguments in reverse order. */
126 for (i = nargs - 1; i >= 0; i--)
128 int len = TYPE_LENGTH (value_enclosing_type (args[i]));
130 sp -= (len + 3) & ~3;
131 count += (len + 3) / 4;
132 write_memory (sp, value_contents_all (args[i]), len);
135 /* Push argument count. */
137 store_unsigned_integer (buf, 4, count);
138 write_memory (sp, buf, 4);
140 /* Update the argument pointer. */
141 store_unsigned_integer (buf, 4, sp);
142 regcache_cooked_write (regcache, VAX_AP_REGNUM, buf);
148 vax_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
149 struct regcache *regcache, CORE_ADDR bp_addr, int nargs,
150 struct value **args, CORE_ADDR sp, int struct_return,
151 CORE_ADDR struct_addr)
156 /* Set up the function arguments. */
157 sp = vax_store_arguments (regcache, nargs, args, sp);
159 /* Store return value address. */
161 regcache_cooked_write_unsigned (regcache, VAX_R1_REGNUM, struct_addr);
163 /* Store return address in the PC slot. */
165 store_unsigned_integer (buf, 4, bp_addr);
166 write_memory (sp, buf, 4);
168 /* Store the (fake) frame pointer in the FP slot. */
170 store_unsigned_integer (buf, 4, fp);
171 write_memory (sp, buf, 4);
173 /* Skip the AP slot. */
176 /* Store register save mask and control bits. */
178 store_unsigned_integer (buf, 4, 0);
179 write_memory (sp, buf, 4);
181 /* Store condition handler. */
183 store_unsigned_integer (buf, 4, 0);
184 write_memory (sp, buf, 4);
186 /* Update the stack pointer and frame pointer. */
187 store_unsigned_integer (buf, 4, sp);
188 regcache_cooked_write (regcache, VAX_SP_REGNUM, buf);
189 regcache_cooked_write (regcache, VAX_FP_REGNUM, buf);
191 /* Return the saved (fake) frame pointer. */
195 static struct frame_id
196 vax_unwind_dummy_id (struct gdbarch *gdbarch, struct frame_info *next_frame)
200 fp = frame_unwind_register_unsigned (next_frame, VAX_FP_REGNUM);
201 return frame_id_build (fp, frame_pc_unwind (next_frame));
205 static enum return_value_convention
206 vax_return_value (struct gdbarch *gdbarch, struct type *type,
207 struct regcache *regcache, gdb_byte *readbuf,
208 const gdb_byte *writebuf)
210 int len = TYPE_LENGTH (type);
213 if (TYPE_CODE (type) == TYPE_CODE_STRUCT
214 || TYPE_CODE (type) == TYPE_CODE_UNION
215 || TYPE_CODE (type) == TYPE_CODE_ARRAY)
217 /* The default on VAX is to return structures in static memory.
218 Consequently a function must return the address where we can
219 find the return value. */
225 regcache_raw_read_unsigned (regcache, VAX_R0_REGNUM, &addr);
226 read_memory (addr, readbuf, len);
229 return RETURN_VALUE_ABI_RETURNS_ADDRESS;
234 /* Read the contents of R0 and (if necessary) R1. */
235 regcache_cooked_read (regcache, VAX_R0_REGNUM, buf);
237 regcache_cooked_read (regcache, VAX_R1_REGNUM, buf + 4);
238 memcpy (readbuf, buf, len);
242 /* Read the contents to R0 and (if necessary) R1. */
243 memcpy (buf, writebuf, len);
244 regcache_cooked_write (regcache, VAX_R0_REGNUM, buf);
246 regcache_cooked_write (regcache, VAX_R1_REGNUM, buf + 4);
249 return RETURN_VALUE_REGISTER_CONVENTION;
253 /* Use the program counter to determine the contents and size of a
254 breakpoint instruction. Return a pointer to a string of bytes that
255 encode a breakpoint instruction, store the length of the string in
256 *LEN and optionally adjust *PC to point to the correct memory
257 location for inserting the breakpoint. */
259 static const gdb_byte *
260 vax_breakpoint_from_pc (CORE_ADDR *pc, int *len)
262 static gdb_byte break_insn[] = { 3 };
264 *len = sizeof (break_insn);
268 /* Advance PC across any function entry prologue instructions
269 to reach some "real" code. */
272 vax_skip_prologue (CORE_ADDR pc)
274 gdb_byte op = read_memory_unsigned_integer (pc, 1);
277 pc += 2; /* skip brb */
279 pc += 3; /* skip brw */
281 && (read_memory_unsigned_integer (pc + 2, 1)) == 0x5E)
282 pc += 3; /* skip subl2 */
284 && (read_memory_unsigned_integer (pc + 1, 1)) == 0xAE
285 && (read_memory_unsigned_integer (pc + 3, 1)) == 0x5E)
286 pc += 4; /* skip movab */
288 && (read_memory_unsigned_integer (pc + 1, 1)) == 0xCE
289 && (read_memory_unsigned_integer (pc + 4, 1)) == 0x5E)
290 pc += 5; /* skip movab */
292 && (read_memory_unsigned_integer (pc + 1, 1)) == 0xEE
293 && (read_memory_unsigned_integer (pc + 6, 1)) == 0x5E)
294 pc += 7; /* skip movab */
300 /* Unwinding the stack is relatively easy since the VAX has a
301 dedicated frame pointer, and frames are set up automatically as the
302 result of a function call. Most of the relevant information can be
303 inferred from the documentation of the Procedure Call Instructions
304 in the VAX MACRO and Instruction Set Reference Manual. */
306 struct vax_frame_cache
311 /* Table of saved registers. */
312 struct trad_frame_saved_reg *saved_regs;
315 struct vax_frame_cache *
316 vax_frame_cache (struct frame_info *next_frame, void **this_cache)
318 struct vax_frame_cache *cache;
326 /* Allocate a new cache. */
327 cache = FRAME_OBSTACK_ZALLOC (struct vax_frame_cache);
328 cache->saved_regs = trad_frame_alloc_saved_regs (next_frame);
330 /* The frame pointer is used as the base for the frame. */
331 cache->base = frame_unwind_register_unsigned (next_frame, VAX_FP_REGNUM);
332 if (cache->base == 0)
335 /* The register save mask and control bits determine the layout of
337 mask = get_frame_memory_unsigned (next_frame, cache->base + 4, 4) >> 16;
339 /* These are always saved. */
340 cache->saved_regs[VAX_PC_REGNUM].addr = cache->base + 16;
341 cache->saved_regs[VAX_FP_REGNUM].addr = cache->base + 12;
342 cache->saved_regs[VAX_AP_REGNUM].addr = cache->base + 8;
343 cache->saved_regs[VAX_PS_REGNUM].addr = cache->base + 4;
345 /* Scan the register save mask and record the location of the saved
347 addr = cache->base + 20;
348 for (regnum = 0; regnum < VAX_AP_REGNUM; regnum++)
350 if (mask & (1 << regnum))
352 cache->saved_regs[regnum].addr = addr;
357 /* The CALLS/CALLG flag determines whether this frame has a General
358 Argument List or a Stack Argument List. */
359 if (mask & (1 << 13))
363 /* This is a procedure with Stack Argument List. Adjust the
364 stack address for the arguments that were pushed onto the
365 stack. The return instruction will automatically pop the
366 arguments from the stack. */
367 numarg = get_frame_memory_unsigned (next_frame, addr, 1);
368 addr += 4 + numarg * 4;
371 /* Bits 1:0 of the stack pointer were saved in the control bits. */
372 trad_frame_set_value (cache->saved_regs, VAX_SP_REGNUM, addr + (mask >> 14));
378 vax_frame_this_id (struct frame_info *next_frame, void **this_cache,
379 struct frame_id *this_id)
381 struct vax_frame_cache *cache = vax_frame_cache (next_frame, this_cache);
383 /* This marks the outermost frame. */
384 if (cache->base == 0)
387 (*this_id) = frame_id_build (cache->base, frame_func_unwind (next_frame));
391 vax_frame_prev_register (struct frame_info *next_frame, void **this_cache,
392 int regnum, int *optimizedp,
393 enum lval_type *lvalp, CORE_ADDR *addrp,
394 int *realnump, gdb_byte *valuep)
396 struct vax_frame_cache *cache = vax_frame_cache (next_frame, this_cache);
398 trad_frame_get_prev_register (next_frame, cache->saved_regs, regnum,
399 optimizedp, lvalp, addrp, realnump, valuep);
402 static const struct frame_unwind vax_frame_unwind =
406 vax_frame_prev_register
409 static const struct frame_unwind *
410 vax_frame_sniffer (struct frame_info *next_frame)
412 return &vax_frame_unwind;
417 vax_frame_base_address (struct frame_info *next_frame, void **this_cache)
419 struct vax_frame_cache *cache = vax_frame_cache (next_frame, this_cache);
425 vax_frame_args_address (struct frame_info *next_frame, void **this_cache)
427 return frame_unwind_register_unsigned (next_frame, VAX_AP_REGNUM);
430 static const struct frame_base vax_frame_base =
433 vax_frame_base_address,
434 vax_frame_base_address,
435 vax_frame_args_address
438 /* Return number of arguments for FRAME. */
441 vax_frame_num_args (struct frame_info *frame)
445 /* Assume that the argument pointer for the outermost frame is
446 hosed, as is the case on NetBSD/vax ELF. */
447 if (get_frame_base_address (frame) == 0)
450 args = get_frame_register_unsigned (frame, VAX_AP_REGNUM);
451 return get_frame_memory_unsigned (frame, args, 1);
455 vax_unwind_pc (struct gdbarch *gdbarch, struct frame_info *next_frame)
457 return frame_unwind_register_unsigned (next_frame, VAX_PC_REGNUM);
461 /* Initialize the current architecture based on INFO. If possible, re-use an
462 architecture from ARCHES, which is a list of architectures already created
463 during this debugging session.
465 Called e.g. at program startup, when reading a core file, and when reading
468 static struct gdbarch *
469 vax_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
471 struct gdbarch *gdbarch;
473 /* If there is already a candidate, use it. */
474 arches = gdbarch_list_lookup_by_info (arches, &info);
476 return arches->gdbarch;
478 gdbarch = gdbarch_alloc (&info, NULL);
480 set_gdbarch_float_format (gdbarch, &floatformat_vax_f);
481 set_gdbarch_double_format (gdbarch, &floatformat_vax_d);
482 set_gdbarch_long_double_format (gdbarch, &floatformat_vax_d);
483 set_gdbarch_long_double_bit(gdbarch, 64);
486 set_gdbarch_num_regs (gdbarch, VAX_NUM_REGS);
487 set_gdbarch_register_name (gdbarch, vax_register_name);
488 set_gdbarch_register_type (gdbarch, vax_register_type);
489 set_gdbarch_sp_regnum (gdbarch, VAX_SP_REGNUM);
490 set_gdbarch_pc_regnum (gdbarch, VAX_PC_REGNUM);
491 set_gdbarch_ps_regnum (gdbarch, VAX_PS_REGNUM);
493 set_gdbarch_regset_from_core_section
494 (gdbarch, vax_regset_from_core_section);
496 /* Frame and stack info */
497 set_gdbarch_skip_prologue (gdbarch, vax_skip_prologue);
498 set_gdbarch_frame_num_args (gdbarch, vax_frame_num_args);
499 set_gdbarch_frame_args_skip (gdbarch, 4);
501 /* Stack grows downward. */
502 set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
504 /* Return value info */
505 set_gdbarch_return_value (gdbarch, vax_return_value);
507 /* Call dummy code. */
508 set_gdbarch_push_dummy_call (gdbarch, vax_push_dummy_call);
509 set_gdbarch_unwind_dummy_id (gdbarch, vax_unwind_dummy_id);
511 /* Breakpoint info */
512 set_gdbarch_breakpoint_from_pc (gdbarch, vax_breakpoint_from_pc);
515 set_gdbarch_deprecated_function_start_offset (gdbarch, 2);
516 set_gdbarch_believe_pcc_promotion (gdbarch, 1);
518 set_gdbarch_print_insn (gdbarch, print_insn_vax);
520 set_gdbarch_unwind_pc (gdbarch, vax_unwind_pc);
522 frame_base_set_default (gdbarch, &vax_frame_base);
524 /* Hook in ABI-specific overrides, if they have been registered. */
525 gdbarch_init_osabi (info, gdbarch);
527 frame_unwind_append_sniffer (gdbarch, vax_frame_sniffer);
532 /* Provide a prototype to silence -Wmissing-prototypes. */
533 void _initialize_vax_tdep (void);
536 _initialize_vax_tdep (void)
538 gdbarch_register (bfd_arch_vax, vax_gdbarch_init, NULL);