]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/vax-tdep.c
Copyright updates for 2007.
[thirdparty/binutils-gdb.git] / gdb / vax-tdep.c
CommitLineData
0543f387 1/* Target-dependent code for the VAX.
464e0365 2
6aba47ca
DJ
3 Copyright (C) 1986, 1989, 1991, 1992, 1995, 1996, 1998, 1999, 2000, 2002,
4 2003, 2004, 2005, 2007 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
197e01b6
EZ
20 Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 Boston, MA 02110-1301, USA. */
c906108c
SS
22
23#include "defs.h"
0543f387
MK
24#include "arch-utils.h"
25#include "dis-asm.h"
0a3e99f6 26#include "floatformat.h"
c11c3a98 27#include "frame.h"
7def7fef
MK
28#include "frame-base.h"
29#include "frame-unwind.h"
0543f387
MK
30#include "gdbcore.h"
31#include "gdbtypes.h"
4be87837 32#include "osabi.h"
0543f387 33#include "regcache.h"
8b910bab 34#include "regset.h"
0543f387
MK
35#include "trad-frame.h"
36#include "value.h"
8b910bab
MK
37
38#include "gdb_string.h"
f267bd6a
JT
39
40#include "vax-tdep.h"
41
5e6b39ff
MK
42/* Return the name of register REGNUM. */
43
fa88f677 44static const char *
5e6b39ff 45vax_register_name (int regnum)
51eb8b08
JT
46{
47 static char *register_names[] =
48 {
5e6b39ff
MK
49 "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
50 "r8", "r9", "r10", "r11", "ap", "fp", "sp", "pc",
51eb8b08
JT
51 "ps",
52 };
53
5e6b39ff
MK
54 if (regnum >= 0 && regnum < ARRAY_SIZE (register_names))
55 return register_names[regnum];
51eb8b08 56
5e6b39ff 57 return NULL;
51eb8b08
JT
58}
59
5e6b39ff
MK
60/* Return the GDB type object for the "standard" data type of data in
61 register REGNUM. */
51eb8b08 62
f267bd6a 63static struct type *
5e6b39ff 64vax_register_type (struct gdbarch *gdbarch, int regnum)
51eb8b08 65{
5e6b39ff 66 return builtin_type_int;
51eb8b08
JT
67}
68\f
8b910bab
MK
69/* Core file support. */
70
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. */
74
75static void
76vax_supply_gregset (const struct regset *regset, struct regcache *regcache,
77 int regnum, const void *gregs, size_t len)
78{
8cc49c48 79 const gdb_byte *regs = gregs;
8b910bab
MK
80 int i;
81
82 for (i = 0; i < VAX_NUM_REGS; i++)
83 {
84 if (regnum == i || regnum == -1)
85 regcache_raw_supply (regcache, i, regs + i * 4);
86 }
87}
52efde73 88
8b910bab
MK
89/* VAX register set. */
90
91static struct regset vax_gregset =
92{
93 NULL,
94 vax_supply_gregset
95};
96
97/* Return the appropriate register set for the core section identified
98 by SECT_NAME and SECT_SIZE. */
99
100static const struct regset *
101vax_regset_from_core_section (struct gdbarch *gdbarch,
102 const char *sect_name, size_t sect_size)
103{
104 if (strcmp (sect_name, ".reg") == 0 && sect_size >= VAX_NUM_REGS * 4)
105 return &vax_gregset;
106
107 return NULL;
108}
109\f
4cd80476 110/* The VAX UNIX calling convention uses R1 to pass a structure return
67b441e1
MK
111 value address instead of passing it as a first (hidden) argument as
112 the VMS calling convention suggests. */
113
7346c184
MK
114static CORE_ADDR
115vax_store_arguments (struct regcache *regcache, int nargs,
67b441e1 116 struct value **args, CORE_ADDR sp)
52efde73 117{
8cc49c48 118 gdb_byte buf[4];
7346c184
MK
119 int count = 0;
120 int i;
52efde73 121
7346c184
MK
122 /* We create an argument list on the stack, and make the argument
123 pointer to it. */
52efde73 124
7346c184
MK
125 /* Push arguments in reverse order. */
126 for (i = nargs - 1; i >= 0; i--)
52efde73 127 {
4754a64e 128 int len = TYPE_LENGTH (value_enclosing_type (args[i]));
7346c184
MK
129
130 sp -= (len + 3) & ~3;
131 count += (len + 3) / 4;
46615f07 132 write_memory (sp, value_contents_all (args[i]), len);
52efde73 133 }
a33f7558 134
7346c184
MK
135 /* Push argument count. */
136 sp -= 4;
137 store_unsigned_integer (buf, 4, count);
138 write_memory (sp, buf, 4);
a33f7558 139
7346c184
MK
140 /* Update the argument pointer. */
141 store_unsigned_integer (buf, 4, sp);
142 regcache_cooked_write (regcache, VAX_AP_REGNUM, buf);
a33f7558 143
7346c184
MK
144 return sp;
145}
146
147static CORE_ADDR
7d9b040b 148vax_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
7346c184
MK
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)
152{
153 CORE_ADDR fp = sp;
8cc49c48 154 gdb_byte buf[4];
7346c184
MK
155
156 /* Set up the function arguments. */
67b441e1
MK
157 sp = vax_store_arguments (regcache, nargs, args, sp);
158
159 /* Store return value address. */
160 if (struct_return)
161 regcache_cooked_write_unsigned (regcache, VAX_R1_REGNUM, struct_addr);
7346c184
MK
162
163 /* Store return address in the PC slot. */
164 sp -= 4;
165 store_unsigned_integer (buf, 4, bp_addr);
166 write_memory (sp, buf, 4);
167
168 /* Store the (fake) frame pointer in the FP slot. */
169 sp -= 4;
170 store_unsigned_integer (buf, 4, fp);
171 write_memory (sp, buf, 4);
172
173 /* Skip the AP slot. */
174 sp -= 4;
175
176 /* Store register save mask and control bits. */
177 sp -= 4;
178 store_unsigned_integer (buf, 4, 0);
179 write_memory (sp, buf, 4);
180
181 /* Store condition handler. */
182 sp -= 4;
183 store_unsigned_integer (buf, 4, 0);
184 write_memory (sp, buf, 4);
185
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);
190
191 /* Return the saved (fake) frame pointer. */
192 return fp;
193}
194
195static struct frame_id
196vax_unwind_dummy_id (struct gdbarch *gdbarch, struct frame_info *next_frame)
a33f7558 197{
7346c184
MK
198 CORE_ADDR fp;
199
200 fp = frame_unwind_register_unsigned (next_frame, VAX_FP_REGNUM);
201 return frame_id_build (fp, frame_pc_unwind (next_frame));
a33f7558 202}
ab62c900 203\f
7346c184 204
67b441e1
MK
205static enum return_value_convention
206vax_return_value (struct gdbarch *gdbarch, struct type *type,
459f20b9
MK
207 struct regcache *regcache, gdb_byte *readbuf,
208 const gdb_byte *writebuf)
ea74468c 209{
67b441e1 210 int len = TYPE_LENGTH (type);
8cc49c48 211 gdb_byte buf[8];
ea74468c 212
67b441e1 213 if (TYPE_CODE (type) == TYPE_CODE_STRUCT
caed1a45 214 || TYPE_CODE (type) == TYPE_CODE_UNION
67b441e1 215 || TYPE_CODE (type) == TYPE_CODE_ARRAY)
e5483145
MK
216 {
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. */
220
221 if (readbuf)
222 {
223 ULONGEST addr;
224
225 regcache_raw_read_unsigned (regcache, VAX_R0_REGNUM, &addr);
226 read_memory (addr, readbuf, len);
227 }
228
229 return RETURN_VALUE_ABI_RETURNS_ADDRESS;
230 }
ea74468c 231
67b441e1
MK
232 if (readbuf)
233 {
234 /* Read the contents of R0 and (if necessary) R1. */
235 regcache_cooked_read (regcache, VAX_R0_REGNUM, buf);
236 if (len > 4)
237 regcache_cooked_read (regcache, VAX_R1_REGNUM, buf + 4);
238 memcpy (readbuf, buf, len);
239 }
240 if (writebuf)
241 {
242 /* Read the contents to R0 and (if necessary) R1. */
243 memcpy (buf, writebuf, len);
244 regcache_cooked_write (regcache, VAX_R0_REGNUM, buf);
245 if (len > 4)
246 regcache_cooked_write (regcache, VAX_R1_REGNUM, buf + 4);
247 }
248
249 return RETURN_VALUE_REGISTER_CONVENTION;
ea74468c 250}
ea74468c 251\f
5e6b39ff
MK
252
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. */
258
8cc49c48 259static const gdb_byte *
5e6b39ff 260vax_breakpoint_from_pc (CORE_ADDR *pc, int *len)
1d049c5e 261{
8cc49c48 262 static gdb_byte break_insn[] = { 3 };
1d049c5e 263
5e6b39ff
MK
264 *len = sizeof (break_insn);
265 return break_insn;
1d049c5e
JT
266}
267\f
b83266a0
SS
268/* Advance PC across any function entry prologue instructions
269 to reach some "real" code. */
270
f267bd6a 271static CORE_ADDR
fba45db2 272vax_skip_prologue (CORE_ADDR pc)
b83266a0 273{
8cc49c48 274 gdb_byte op = read_memory_unsigned_integer (pc, 1);
0543f387 275
b83266a0 276 if (op == 0x11)
c5aa993b 277 pc += 2; /* skip brb */
b83266a0 278 if (op == 0x31)
c5aa993b 279 pc += 3; /* skip brw */
b83266a0 280 if (op == 0xC2
0543f387 281 && (read_memory_unsigned_integer (pc + 2, 1)) == 0x5E)
c5aa993b 282 pc += 3; /* skip subl2 */
b83266a0 283 if (op == 0x9E
0543f387
MK
284 && (read_memory_unsigned_integer (pc + 1, 1)) == 0xAE
285 && (read_memory_unsigned_integer (pc + 3, 1)) == 0x5E)
c5aa993b 286 pc += 4; /* skip movab */
b83266a0 287 if (op == 0x9E
0543f387
MK
288 && (read_memory_unsigned_integer (pc + 1, 1)) == 0xCE
289 && (read_memory_unsigned_integer (pc + 4, 1)) == 0x5E)
c5aa993b 290 pc += 5; /* skip movab */
b83266a0 291 if (op == 0x9E
0543f387
MK
292 && (read_memory_unsigned_integer (pc + 1, 1)) == 0xEE
293 && (read_memory_unsigned_integer (pc + 6, 1)) == 0x5E)
c5aa993b 294 pc += 7; /* skip movab */
0543f387 295
b83266a0
SS
296 return pc;
297}
7def7fef
MK
298\f
299
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. */
305
306struct vax_frame_cache
307{
308 /* Base address. */
309 CORE_ADDR base;
310
311 /* Table of saved registers. */
312 struct trad_frame_saved_reg *saved_regs;
313};
314
315struct vax_frame_cache *
316vax_frame_cache (struct frame_info *next_frame, void **this_cache)
317{
318 struct vax_frame_cache *cache;
319 CORE_ADDR addr;
320 ULONGEST mask;
321 int regnum;
322
323 if (*this_cache)
324 return *this_cache;
325
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);
329
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)
333 return cache;
334
335 /* The register save mask and control bits determine the layout of
336 the stack frame. */
337 mask = get_frame_memory_unsigned (next_frame, cache->base + 4, 4) >> 16;
338
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;
344
345 /* Scan the register save mask and record the location of the saved
346 registers. */
347 addr = cache->base + 20;
348 for (regnum = 0; regnum < VAX_AP_REGNUM; regnum++)
349 {
350 if (mask & (1 << regnum))
351 {
352 cache->saved_regs[regnum].addr = addr;
353 addr += 4;
354 }
355 }
356
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))
360 {
361 ULONGEST numarg;
362
363 /* This is a procedure with Stack Argument List. Adjust the
e36ad527 364 stack address for the arguments that were pushed onto the
7def7fef
MK
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;
369 }
370
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));
373
374 return cache;
375}
376
377static void
378vax_frame_this_id (struct frame_info *next_frame, void **this_cache,
379 struct frame_id *this_id)
380{
381 struct vax_frame_cache *cache = vax_frame_cache (next_frame, this_cache);
382
383 /* This marks the outermost frame. */
384 if (cache->base == 0)
385 return;
386
17777823 387 (*this_id) = frame_id_build (cache->base, frame_func_unwind (next_frame));
7def7fef
MK
388}
389
390static void
391vax_frame_prev_register (struct frame_info *next_frame, void **this_cache,
392 int regnum, int *optimizedp,
393 enum lval_type *lvalp, CORE_ADDR *addrp,
81e51e70 394 int *realnump, gdb_byte *valuep)
7def7fef
MK
395{
396 struct vax_frame_cache *cache = vax_frame_cache (next_frame, this_cache);
397
1f67027d
AC
398 trad_frame_get_prev_register (next_frame, cache->saved_regs, regnum,
399 optimizedp, lvalp, addrp, realnump, valuep);
7def7fef
MK
400}
401
402static const struct frame_unwind vax_frame_unwind =
403{
404 NORMAL_FRAME,
405 vax_frame_this_id,
406 vax_frame_prev_register
407};
408
409static const struct frame_unwind *
410vax_frame_sniffer (struct frame_info *next_frame)
411{
412 return &vax_frame_unwind;
413}
414\f
415
416static CORE_ADDR
417vax_frame_base_address (struct frame_info *next_frame, void **this_cache)
418{
419 struct vax_frame_cache *cache = vax_frame_cache (next_frame, this_cache);
420
421 return cache->base;
422}
b83266a0 423
f267bd6a 424static CORE_ADDR
7def7fef
MK
425vax_frame_args_address (struct frame_info *next_frame, void **this_cache)
426{
427 return frame_unwind_register_unsigned (next_frame, VAX_AP_REGNUM);
428}
429
430static const struct frame_base vax_frame_base =
a33f7558 431{
7def7fef
MK
432 &vax_frame_unwind,
433 vax_frame_base_address,
434 vax_frame_base_address,
435 vax_frame_args_address
436};
437
438/* Return number of arguments for FRAME. */
439
440static int
441vax_frame_num_args (struct frame_info *frame)
442{
443 CORE_ADDR args;
444
445 /* Assume that the argument pointer for the outermost frame is
446 hosed, as is the case on NetBSD/vax ELF. */
a54f9a00 447 if (get_frame_base_address (frame) == 0)
7def7fef
MK
448 return 0;
449
450 args = get_frame_register_unsigned (frame, VAX_AP_REGNUM);
451 return get_frame_memory_unsigned (frame, args, 1);
452}
453
454static CORE_ADDR
455vax_unwind_pc (struct gdbarch *gdbarch, struct frame_info *next_frame)
456{
457 return frame_unwind_register_unsigned (next_frame, VAX_PC_REGNUM);
a33f7558
JT
458}
459\f
7def7fef 460
f267bd6a
JT
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.
464
465 Called e.g. at program startup, when reading a core file, and when reading
466 a binary file. */
467
468static struct gdbarch *
469vax_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
470{
471 struct gdbarch *gdbarch;
472
4be87837
DJ
473 /* If there is already a candidate, use it. */
474 arches = gdbarch_list_lookup_by_info (arches, &info);
475 if (arches != NULL)
476 return arches->gdbarch;
f267bd6a 477
4be87837 478 gdbarch = gdbarch_alloc (&info, NULL);
4791e091 479
0a3e99f6
MK
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);
484
f267bd6a
JT
485 /* Register info */
486 set_gdbarch_num_regs (gdbarch, VAX_NUM_REGS);
5e6b39ff
MK
487 set_gdbarch_register_name (gdbarch, vax_register_name);
488 set_gdbarch_register_type (gdbarch, vax_register_type);
f267bd6a 489 set_gdbarch_sp_regnum (gdbarch, VAX_SP_REGNUM);
f267bd6a
JT
490 set_gdbarch_pc_regnum (gdbarch, VAX_PC_REGNUM);
491 set_gdbarch_ps_regnum (gdbarch, VAX_PS_REGNUM);
492
8b910bab
MK
493 set_gdbarch_regset_from_core_section
494 (gdbarch, vax_regset_from_core_section);
495
f267bd6a
JT
496 /* Frame and stack info */
497 set_gdbarch_skip_prologue (gdbarch, vax_skip_prologue);
f267bd6a 498 set_gdbarch_frame_num_args (gdbarch, vax_frame_num_args);
f267bd6a
JT
499 set_gdbarch_frame_args_skip (gdbarch, 4);
500
5e6b39ff 501 /* Stack grows downward. */
f267bd6a
JT
502 set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
503
504 /* Return value info */
67b441e1 505 set_gdbarch_return_value (gdbarch, vax_return_value);
f267bd6a 506
7346c184
MK
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);
f267bd6a
JT
510
511 /* Breakpoint info */
1d049c5e 512 set_gdbarch_breakpoint_from_pc (gdbarch, vax_breakpoint_from_pc);
f267bd6a
JT
513
514 /* Misc info */
782263ab 515 set_gdbarch_deprecated_function_start_offset (gdbarch, 2);
1d049c5e 516 set_gdbarch_believe_pcc_promotion (gdbarch, 1);
f267bd6a 517
0543f387
MK
518 set_gdbarch_print_insn (gdbarch, print_insn_vax);
519
7def7fef
MK
520 set_gdbarch_unwind_pc (gdbarch, vax_unwind_pc);
521
522 frame_base_set_default (gdbarch, &vax_frame_base);
523
4791e091 524 /* Hook in ABI-specific overrides, if they have been registered. */
4be87837 525 gdbarch_init_osabi (info, gdbarch);
4791e091 526
7def7fef
MK
527 frame_unwind_append_sniffer (gdbarch, vax_frame_sniffer);
528
f267bd6a
JT
529 return (gdbarch);
530}
c906108c 531
0543f387
MK
532/* Provide a prototype to silence -Wmissing-prototypes. */
533void _initialize_vax_tdep (void);
a78f21af 534
c906108c 535void
fba45db2 536_initialize_vax_tdep (void)
c906108c 537{
4be87837 538 gdbarch_register (bfd_arch_vax, vax_gdbarch_init, NULL);
c906108c 539}