2004-05-08 Andrew Cagney <cagney@redhat.com>
[deliverable/binutils-gdb.git] / gdb / vax-tdep.c
CommitLineData
c906108c 1/* Print VAX instructions for GDB, the GNU debugger.
464e0365
AC
2
3 Copyright 1986, 1989, 1991, 1992, 1995, 1996, 1998, 1999, 2000,
4 2002, 2003, 2004 Free Software Foundation, Inc.
c906108c 5
c5aa993b 6 This file is part of GDB.
c906108c 7
c5aa993b
JM
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.
c906108c 12
c5aa993b
JM
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.
c906108c 17
c5aa993b
JM
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. */
c906108c
SS
22
23#include "defs.h"
24#include "symtab.h"
25#include "opcode/vax.h"
c11c3a98 26#include "gdbcore.h"
f267bd6a 27#include "inferior.h"
a33f7558 28#include "regcache.h"
c11c3a98 29#include "frame.h"
7def7fef
MK
30#include "frame-base.h"
31#include "frame-unwind.h"
32#include "trad-frame.h"
c11c3a98 33#include "value.h"
f267bd6a 34#include "arch-utils.h"
4be87837 35#include "osabi.h"
a89aa300 36#include "dis-asm.h"
8b910bab
MK
37#include "regset.h"
38
39#include "gdb_string.h"
f267bd6a
JT
40
41#include "vax-tdep.h"
42
5e6b39ff
MK
43/* Return the name of register REGNUM. */
44
fa88f677 45static const char *
5e6b39ff 46vax_register_name (int regnum)
51eb8b08
JT
47{
48 static char *register_names[] =
49 {
5e6b39ff
MK
50 "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
51 "r8", "r9", "r10", "r11", "ap", "fp", "sp", "pc",
51eb8b08
JT
52 "ps",
53 };
54
5e6b39ff
MK
55 if (regnum >= 0 && regnum < ARRAY_SIZE (register_names))
56 return register_names[regnum];
51eb8b08 57
5e6b39ff 58 return NULL;
51eb8b08
JT
59}
60
5e6b39ff
MK
61/* Return the GDB type object for the "standard" data type of data in
62 register REGNUM. */
51eb8b08 63
f267bd6a 64static struct type *
5e6b39ff 65vax_register_type (struct gdbarch *gdbarch, int regnum)
51eb8b08 66{
5e6b39ff 67 return builtin_type_int;
51eb8b08
JT
68}
69\f
8b910bab
MK
70/* Core file support. */
71
72/* Supply register REGNUM from the buffer specified by GREGS and LEN
73 in the general-purpose register set REGSET to register cache
74 REGCACHE. If REGNUM is -1, do this for all registers in REGSET. */
75
76static void
77vax_supply_gregset (const struct regset *regset, struct regcache *regcache,
78 int regnum, const void *gregs, size_t len)
79{
80 const char *regs = gregs;
81 int i;
82
83 for (i = 0; i < VAX_NUM_REGS; i++)
84 {
85 if (regnum == i || regnum == -1)
86 regcache_raw_supply (regcache, i, regs + i * 4);
87 }
88}
52efde73 89
8b910bab
MK
90/* VAX register set. */
91
92static struct regset vax_gregset =
93{
94 NULL,
95 vax_supply_gregset
96};
97
98/* Return the appropriate register set for the core section identified
99 by SECT_NAME and SECT_SIZE. */
100
101static const struct regset *
102vax_regset_from_core_section (struct gdbarch *gdbarch,
103 const char *sect_name, size_t sect_size)
104{
105 if (strcmp (sect_name, ".reg") == 0 && sect_size >= VAX_NUM_REGS * 4)
106 return &vax_gregset;
107
108 return NULL;
109}
110\f
67b441e1
MK
111/* The VAX Unix calling convention uses R1 to pass a structure return
112 value address instead of passing it as a first (hidden) argument as
113 the VMS calling convention suggests. */
114
7346c184
MK
115static CORE_ADDR
116vax_store_arguments (struct regcache *regcache, int nargs,
67b441e1 117 struct value **args, CORE_ADDR sp)
52efde73 118{
7346c184
MK
119 char buf[4];
120 int count = 0;
121 int i;
52efde73 122
7346c184
MK
123 /* We create an argument list on the stack, and make the argument
124 pointer to it. */
52efde73 125
7346c184
MK
126 /* Push arguments in reverse order. */
127 for (i = nargs - 1; i >= 0; i--)
52efde73 128 {
7346c184
MK
129 int len = TYPE_LENGTH (VALUE_ENCLOSING_TYPE (args[i]));
130
131 sp -= (len + 3) & ~3;
132 count += (len + 3) / 4;
133 write_memory (sp, VALUE_CONTENTS_ALL (args[i]), len);
52efde73 134 }
a33f7558 135
7346c184
MK
136 /* Push argument count. */
137 sp -= 4;
138 store_unsigned_integer (buf, 4, count);
139 write_memory (sp, buf, 4);
a33f7558 140
7346c184
MK
141 /* Update the argument pointer. */
142 store_unsigned_integer (buf, 4, sp);
143 regcache_cooked_write (regcache, VAX_AP_REGNUM, buf);
a33f7558 144
7346c184
MK
145 return sp;
146}
147
148static CORE_ADDR
149vax_push_dummy_call (struct gdbarch *gdbarch, CORE_ADDR func_addr,
150 struct regcache *regcache, CORE_ADDR bp_addr, int nargs,
151 struct value **args, CORE_ADDR sp, int struct_return,
152 CORE_ADDR struct_addr)
153{
154 CORE_ADDR fp = sp;
155 char buf[4];
156
157 /* Set up the function arguments. */
67b441e1
MK
158 sp = vax_store_arguments (regcache, nargs, args, sp);
159
160 /* Store return value address. */
161 if (struct_return)
162 regcache_cooked_write_unsigned (regcache, VAX_R1_REGNUM, struct_addr);
7346c184
MK
163
164 /* Store return address in the PC slot. */
165 sp -= 4;
166 store_unsigned_integer (buf, 4, bp_addr);
167 write_memory (sp, buf, 4);
168
169 /* Store the (fake) frame pointer in the FP slot. */
170 sp -= 4;
171 store_unsigned_integer (buf, 4, fp);
172 write_memory (sp, buf, 4);
173
174 /* Skip the AP slot. */
175 sp -= 4;
176
177 /* Store register save mask and control bits. */
178 sp -= 4;
179 store_unsigned_integer (buf, 4, 0);
180 write_memory (sp, buf, 4);
181
182 /* Store condition handler. */
183 sp -= 4;
184 store_unsigned_integer (buf, 4, 0);
185 write_memory (sp, buf, 4);
186
187 /* Update the stack pointer and frame pointer. */
188 store_unsigned_integer (buf, 4, sp);
189 regcache_cooked_write (regcache, VAX_SP_REGNUM, buf);
190 regcache_cooked_write (regcache, VAX_FP_REGNUM, buf);
191
192 /* Return the saved (fake) frame pointer. */
193 return fp;
194}
195
196static struct frame_id
197vax_unwind_dummy_id (struct gdbarch *gdbarch, struct frame_info *next_frame)
a33f7558 198{
7346c184
MK
199 CORE_ADDR fp;
200
201 fp = frame_unwind_register_unsigned (next_frame, VAX_FP_REGNUM);
202 return frame_id_build (fp, frame_pc_unwind (next_frame));
a33f7558 203}
ab62c900 204\f
7346c184 205
67b441e1
MK
206static enum return_value_convention
207vax_return_value (struct gdbarch *gdbarch, struct type *type,
208 struct regcache *regcache, void *readbuf,
209 const void *writebuf)
ea74468c 210{
67b441e1
MK
211 int len = TYPE_LENGTH (type);
212 char buf[8];
ea74468c 213
67b441e1
MK
214 if (TYPE_CODE (type) == TYPE_CODE_STRUCT
215 || TYPE_CODE (type) == TYPE_CODE_STRUCT
216 || TYPE_CODE (type) == TYPE_CODE_ARRAY)
217 return RETURN_VALUE_STRUCT_CONVENTION;
ea74468c 218
67b441e1
MK
219 if (readbuf)
220 {
221 /* Read the contents of R0 and (if necessary) R1. */
222 regcache_cooked_read (regcache, VAX_R0_REGNUM, buf);
223 if (len > 4)
224 regcache_cooked_read (regcache, VAX_R1_REGNUM, buf + 4);
225 memcpy (readbuf, buf, len);
226 }
227 if (writebuf)
228 {
229 /* Read the contents to R0 and (if necessary) R1. */
230 memcpy (buf, writebuf, len);
231 regcache_cooked_write (regcache, VAX_R0_REGNUM, buf);
232 if (len > 4)
233 regcache_cooked_write (regcache, VAX_R1_REGNUM, buf + 4);
234 }
235
236 return RETURN_VALUE_REGISTER_CONVENTION;
ea74468c 237}
ea74468c 238\f
5e6b39ff
MK
239
240/* Use the program counter to determine the contents and size of a
241 breakpoint instruction. Return a pointer to a string of bytes that
242 encode a breakpoint instruction, store the length of the string in
243 *LEN and optionally adjust *PC to point to the correct memory
244 location for inserting the breakpoint. */
245
1d049c5e 246static const unsigned char *
5e6b39ff 247vax_breakpoint_from_pc (CORE_ADDR *pc, int *len)
1d049c5e 248{
5e6b39ff 249 static unsigned char break_insn[] = { 3 };
1d049c5e 250
5e6b39ff
MK
251 *len = sizeof (break_insn);
252 return break_insn;
1d049c5e
JT
253}
254\f
b83266a0
SS
255/* Advance PC across any function entry prologue instructions
256 to reach some "real" code. */
257
f267bd6a 258static CORE_ADDR
fba45db2 259vax_skip_prologue (CORE_ADDR pc)
b83266a0 260{
52f0bd74 261 int op = (unsigned char) read_memory_integer (pc, 1);
b83266a0 262 if (op == 0x11)
c5aa993b 263 pc += 2; /* skip brb */
b83266a0 264 if (op == 0x31)
c5aa993b 265 pc += 3; /* skip brw */
b83266a0 266 if (op == 0xC2
c5aa993b
JM
267 && ((unsigned char) read_memory_integer (pc + 2, 1)) == 0x5E)
268 pc += 3; /* skip subl2 */
b83266a0 269 if (op == 0x9E
c5aa993b
JM
270 && ((unsigned char) read_memory_integer (pc + 1, 1)) == 0xAE
271 && ((unsigned char) read_memory_integer (pc + 3, 1)) == 0x5E)
272 pc += 4; /* skip movab */
b83266a0 273 if (op == 0x9E
c5aa993b
JM
274 && ((unsigned char) read_memory_integer (pc + 1, 1)) == 0xCE
275 && ((unsigned char) read_memory_integer (pc + 4, 1)) == 0x5E)
276 pc += 5; /* skip movab */
b83266a0 277 if (op == 0x9E
c5aa993b
JM
278 && ((unsigned char) read_memory_integer (pc + 1, 1)) == 0xEE
279 && ((unsigned char) read_memory_integer (pc + 6, 1)) == 0x5E)
280 pc += 7; /* skip movab */
b83266a0
SS
281 return pc;
282}
7def7fef
MK
283\f
284
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. */
290
291struct vax_frame_cache
292{
293 /* Base address. */
294 CORE_ADDR base;
295
296 /* Table of saved registers. */
297 struct trad_frame_saved_reg *saved_regs;
298};
299
300struct vax_frame_cache *
301vax_frame_cache (struct frame_info *next_frame, void **this_cache)
302{
303 struct vax_frame_cache *cache;
304 CORE_ADDR addr;
305 ULONGEST mask;
306 int regnum;
307
308 if (*this_cache)
309 return *this_cache;
310
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);
314
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)
318 return cache;
319
320 /* The register save mask and control bits determine the layout of
321 the stack frame. */
322 mask = get_frame_memory_unsigned (next_frame, cache->base + 4, 4) >> 16;
323
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;
329
330 /* Scan the register save mask and record the location of the saved
331 registers. */
332 addr = cache->base + 20;
333 for (regnum = 0; regnum < VAX_AP_REGNUM; regnum++)
334 {
335 if (mask & (1 << regnum))
336 {
337 cache->saved_regs[regnum].addr = addr;
338 addr += 4;
339 }
340 }
341
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))
345 {
346 ULONGEST numarg;
347
348 /* This is a procedure with Stack Argument List. Adjust the
e36ad527 349 stack address for the arguments that were pushed onto the
7def7fef
MK
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;
354 }
355
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));
358
359 return cache;
360}
361
362static void
363vax_frame_this_id (struct frame_info *next_frame, void **this_cache,
364 struct frame_id *this_id)
365{
366 struct vax_frame_cache *cache = vax_frame_cache (next_frame, this_cache);
367
368 /* This marks the outermost frame. */
369 if (cache->base == 0)
370 return;
371
372 (*this_id) = frame_id_build (cache->base, frame_pc_unwind (next_frame));
373}
374
375static void
376vax_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)
380{
381 struct vax_frame_cache *cache = vax_frame_cache (next_frame, this_cache);
382
383 trad_frame_prev_register (next_frame, cache->saved_regs, regnum,
384 optimizedp, lvalp, addrp, realnump, valuep);
385}
386
387static const struct frame_unwind vax_frame_unwind =
388{
389 NORMAL_FRAME,
390 vax_frame_this_id,
391 vax_frame_prev_register
392};
393
394static const struct frame_unwind *
395vax_frame_sniffer (struct frame_info *next_frame)
396{
397 return &vax_frame_unwind;
398}
399\f
400
401static CORE_ADDR
402vax_frame_base_address (struct frame_info *next_frame, void **this_cache)
403{
404 struct vax_frame_cache *cache = vax_frame_cache (next_frame, this_cache);
405
406 return cache->base;
407}
b83266a0 408
f267bd6a 409static CORE_ADDR
7def7fef
MK
410vax_frame_args_address (struct frame_info *next_frame, void **this_cache)
411{
412 return frame_unwind_register_unsigned (next_frame, VAX_AP_REGNUM);
413}
414
415static const struct frame_base vax_frame_base =
a33f7558 416{
7def7fef
MK
417 &vax_frame_unwind,
418 vax_frame_base_address,
419 vax_frame_base_address,
420 vax_frame_args_address
421};
422
423/* Return number of arguments for FRAME. */
424
425static int
426vax_frame_num_args (struct frame_info *frame)
427{
428 CORE_ADDR args;
429
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 (frame) == 0)
433 return 0;
434
435 args = get_frame_register_unsigned (frame, VAX_AP_REGNUM);
436 return get_frame_memory_unsigned (frame, args, 1);
437}
438
439static CORE_ADDR
440vax_unwind_pc (struct gdbarch *gdbarch, struct frame_info *next_frame)
441{
442 return frame_unwind_register_unsigned (next_frame, VAX_PC_REGNUM);
a33f7558
JT
443}
444\f
7def7fef 445
f267bd6a
JT
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.
449
450 Called e.g. at program startup, when reading a core file, and when reading
451 a binary file. */
452
453static struct gdbarch *
454vax_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
455{
456 struct gdbarch *gdbarch;
457
4be87837
DJ
458 /* If there is already a candidate, use it. */
459 arches = gdbarch_list_lookup_by_info (arches, &info);
460 if (arches != NULL)
461 return arches->gdbarch;
f267bd6a 462
4be87837 463 gdbarch = gdbarch_alloc (&info, NULL);
4791e091 464
f267bd6a
JT
465 /* Register info */
466 set_gdbarch_num_regs (gdbarch, VAX_NUM_REGS);
5e6b39ff
MK
467 set_gdbarch_register_name (gdbarch, vax_register_name);
468 set_gdbarch_register_type (gdbarch, vax_register_type);
f267bd6a 469 set_gdbarch_sp_regnum (gdbarch, VAX_SP_REGNUM);
f267bd6a
JT
470 set_gdbarch_pc_regnum (gdbarch, VAX_PC_REGNUM);
471 set_gdbarch_ps_regnum (gdbarch, VAX_PS_REGNUM);
472
8b910bab
MK
473 set_gdbarch_regset_from_core_section
474 (gdbarch, vax_regset_from_core_section);
475
f267bd6a
JT
476 /* Frame and stack info */
477 set_gdbarch_skip_prologue (gdbarch, vax_skip_prologue);
f267bd6a 478 set_gdbarch_frame_num_args (gdbarch, vax_frame_num_args);
f267bd6a 479
f267bd6a
JT
480 set_gdbarch_frame_args_skip (gdbarch, 4);
481
5e6b39ff 482 /* Stack grows downward. */
f267bd6a
JT
483 set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
484
485 /* Return value info */
67b441e1 486 set_gdbarch_return_value (gdbarch, vax_return_value);
f267bd6a 487
7346c184
MK
488 /* Call dummy code. */
489 set_gdbarch_push_dummy_call (gdbarch, vax_push_dummy_call);
490 set_gdbarch_unwind_dummy_id (gdbarch, vax_unwind_dummy_id);
f267bd6a
JT
491
492 /* Breakpoint info */
1d049c5e 493 set_gdbarch_breakpoint_from_pc (gdbarch, vax_breakpoint_from_pc);
f267bd6a
JT
494
495 /* Misc info */
496 set_gdbarch_function_start_offset (gdbarch, 2);
1d049c5e 497 set_gdbarch_believe_pcc_promotion (gdbarch, 1);
f267bd6a 498
7def7fef
MK
499 set_gdbarch_unwind_pc (gdbarch, vax_unwind_pc);
500
501 frame_base_set_default (gdbarch, &vax_frame_base);
502
4791e091 503 /* Hook in ABI-specific overrides, if they have been registered. */
4be87837 504 gdbarch_init_osabi (info, gdbarch);
4791e091 505
7def7fef
MK
506 frame_unwind_append_sniffer (gdbarch, vax_frame_sniffer);
507
ee2842e2
ILT
508 set_gdbarch_print_insn (gdbarch, print_insn_vax);
509
f267bd6a
JT
510 return (gdbarch);
511}
c906108c 512
a78f21af
AC
513extern initialize_file_ftype _initialize_vax_tdep; /* -Wmissing-prototypes */
514
c906108c 515void
fba45db2 516_initialize_vax_tdep (void)
c906108c 517{
4be87837 518 gdbarch_register (bfd_arch_vax, vax_gdbarch_init, NULL);
c906108c 519}
This page took 0.500621 seconds and 4 git commands to generate.