Commit | Line | Data |
---|---|---|
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" |
9bbe19fb | 35 | #include "gdb_string.h" |
4be87837 | 36 | #include "osabi.h" |
a89aa300 | 37 | #include "dis-asm.h" |
f267bd6a JT |
38 | |
39 | #include "vax-tdep.h" | |
40 | ||
5e6b39ff MK |
41 | /* Return the name of register REGNUM. */ |
42 | ||
fa88f677 | 43 | static const char * |
5e6b39ff | 44 | vax_register_name (int regnum) |
51eb8b08 JT |
45 | { |
46 | static char *register_names[] = | |
47 | { | |
5e6b39ff MK |
48 | "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7", |
49 | "r8", "r9", "r10", "r11", "ap", "fp", "sp", "pc", | |
51eb8b08 JT |
50 | "ps", |
51 | }; | |
52 | ||
5e6b39ff MK |
53 | if (regnum >= 0 && regnum < ARRAY_SIZE (register_names)) |
54 | return register_names[regnum]; | |
51eb8b08 | 55 | |
5e6b39ff | 56 | return NULL; |
51eb8b08 JT |
57 | } |
58 | ||
5e6b39ff MK |
59 | /* Return the GDB type object for the "standard" data type of data in |
60 | register REGNUM. */ | |
51eb8b08 | 61 | |
f267bd6a | 62 | static struct type * |
5e6b39ff | 63 | vax_register_type (struct gdbarch *gdbarch, int regnum) |
51eb8b08 | 64 | { |
5e6b39ff | 65 | return builtin_type_int; |
51eb8b08 JT |
66 | } |
67 | \f | |
52efde73 | 68 | |
67b441e1 MK |
69 | /* The VAX Unix calling convention uses R1 to pass a structure return |
70 | value address instead of passing it as a first (hidden) argument as | |
71 | the VMS calling convention suggests. */ | |
72 | ||
7346c184 MK |
73 | static CORE_ADDR |
74 | vax_store_arguments (struct regcache *regcache, int nargs, | |
67b441e1 | 75 | struct value **args, CORE_ADDR sp) |
52efde73 | 76 | { |
7346c184 MK |
77 | char buf[4]; |
78 | int count = 0; | |
79 | int i; | |
52efde73 | 80 | |
7346c184 MK |
81 | /* We create an argument list on the stack, and make the argument |
82 | pointer to it. */ | |
52efde73 | 83 | |
7346c184 MK |
84 | /* Push arguments in reverse order. */ |
85 | for (i = nargs - 1; i >= 0; i--) | |
52efde73 | 86 | { |
7346c184 MK |
87 | int len = TYPE_LENGTH (VALUE_ENCLOSING_TYPE (args[i])); |
88 | ||
89 | sp -= (len + 3) & ~3; | |
90 | count += (len + 3) / 4; | |
91 | write_memory (sp, VALUE_CONTENTS_ALL (args[i]), len); | |
52efde73 | 92 | } |
a33f7558 | 93 | |
7346c184 MK |
94 | /* Push argument count. */ |
95 | sp -= 4; | |
96 | store_unsigned_integer (buf, 4, count); | |
97 | write_memory (sp, buf, 4); | |
a33f7558 | 98 | |
7346c184 MK |
99 | /* Update the argument pointer. */ |
100 | store_unsigned_integer (buf, 4, sp); | |
101 | regcache_cooked_write (regcache, VAX_AP_REGNUM, buf); | |
a33f7558 | 102 | |
7346c184 MK |
103 | return sp; |
104 | } | |
105 | ||
106 | static CORE_ADDR | |
107 | vax_push_dummy_call (struct gdbarch *gdbarch, CORE_ADDR func_addr, | |
108 | struct regcache *regcache, CORE_ADDR bp_addr, int nargs, | |
109 | struct value **args, CORE_ADDR sp, int struct_return, | |
110 | CORE_ADDR struct_addr) | |
111 | { | |
112 | CORE_ADDR fp = sp; | |
113 | char buf[4]; | |
114 | ||
115 | /* Set up the function arguments. */ | |
67b441e1 MK |
116 | sp = vax_store_arguments (regcache, nargs, args, sp); |
117 | ||
118 | /* Store return value address. */ | |
119 | if (struct_return) | |
120 | regcache_cooked_write_unsigned (regcache, VAX_R1_REGNUM, struct_addr); | |
7346c184 MK |
121 | |
122 | /* Store return address in the PC slot. */ | |
123 | sp -= 4; | |
124 | store_unsigned_integer (buf, 4, bp_addr); | |
125 | write_memory (sp, buf, 4); | |
126 | ||
127 | /* Store the (fake) frame pointer in the FP slot. */ | |
128 | sp -= 4; | |
129 | store_unsigned_integer (buf, 4, fp); | |
130 | write_memory (sp, buf, 4); | |
131 | ||
132 | /* Skip the AP slot. */ | |
133 | sp -= 4; | |
134 | ||
135 | /* Store register save mask and control bits. */ | |
136 | sp -= 4; | |
137 | store_unsigned_integer (buf, 4, 0); | |
138 | write_memory (sp, buf, 4); | |
139 | ||
140 | /* Store condition handler. */ | |
141 | sp -= 4; | |
142 | store_unsigned_integer (buf, 4, 0); | |
143 | write_memory (sp, buf, 4); | |
144 | ||
145 | /* Update the stack pointer and frame pointer. */ | |
146 | store_unsigned_integer (buf, 4, sp); | |
147 | regcache_cooked_write (regcache, VAX_SP_REGNUM, buf); | |
148 | regcache_cooked_write (regcache, VAX_FP_REGNUM, buf); | |
149 | ||
150 | /* Return the saved (fake) frame pointer. */ | |
151 | return fp; | |
152 | } | |
153 | ||
154 | static struct frame_id | |
155 | vax_unwind_dummy_id (struct gdbarch *gdbarch, struct frame_info *next_frame) | |
a33f7558 | 156 | { |
7346c184 MK |
157 | CORE_ADDR fp; |
158 | ||
159 | fp = frame_unwind_register_unsigned (next_frame, VAX_FP_REGNUM); | |
160 | return frame_id_build (fp, frame_pc_unwind (next_frame)); | |
a33f7558 | 161 | } |
ab62c900 | 162 | \f |
7346c184 | 163 | |
67b441e1 MK |
164 | static enum return_value_convention |
165 | vax_return_value (struct gdbarch *gdbarch, struct type *type, | |
166 | struct regcache *regcache, void *readbuf, | |
167 | const void *writebuf) | |
ea74468c | 168 | { |
67b441e1 MK |
169 | int len = TYPE_LENGTH (type); |
170 | char buf[8]; | |
ea74468c | 171 | |
67b441e1 MK |
172 | if (TYPE_CODE (type) == TYPE_CODE_STRUCT |
173 | || TYPE_CODE (type) == TYPE_CODE_STRUCT | |
174 | || TYPE_CODE (type) == TYPE_CODE_ARRAY) | |
175 | return RETURN_VALUE_STRUCT_CONVENTION; | |
ea74468c | 176 | |
67b441e1 MK |
177 | if (readbuf) |
178 | { | |
179 | /* Read the contents of R0 and (if necessary) R1. */ | |
180 | regcache_cooked_read (regcache, VAX_R0_REGNUM, buf); | |
181 | if (len > 4) | |
182 | regcache_cooked_read (regcache, VAX_R1_REGNUM, buf + 4); | |
183 | memcpy (readbuf, buf, len); | |
184 | } | |
185 | if (writebuf) | |
186 | { | |
187 | /* Read the contents to R0 and (if necessary) R1. */ | |
188 | memcpy (buf, writebuf, len); | |
189 | regcache_cooked_write (regcache, VAX_R0_REGNUM, buf); | |
190 | if (len > 4) | |
191 | regcache_cooked_write (regcache, VAX_R1_REGNUM, buf + 4); | |
192 | } | |
193 | ||
194 | return RETURN_VALUE_REGISTER_CONVENTION; | |
ea74468c | 195 | } |
ea74468c | 196 | \f |
5e6b39ff MK |
197 | |
198 | /* Use the program counter to determine the contents and size of a | |
199 | breakpoint instruction. Return a pointer to a string of bytes that | |
200 | encode a breakpoint instruction, store the length of the string in | |
201 | *LEN and optionally adjust *PC to point to the correct memory | |
202 | location for inserting the breakpoint. */ | |
203 | ||
1d049c5e | 204 | static const unsigned char * |
5e6b39ff | 205 | vax_breakpoint_from_pc (CORE_ADDR *pc, int *len) |
1d049c5e | 206 | { |
5e6b39ff | 207 | static unsigned char break_insn[] = { 3 }; |
1d049c5e | 208 | |
5e6b39ff MK |
209 | *len = sizeof (break_insn); |
210 | return break_insn; | |
1d049c5e JT |
211 | } |
212 | \f | |
b83266a0 SS |
213 | /* Advance PC across any function entry prologue instructions |
214 | to reach some "real" code. */ | |
215 | ||
f267bd6a | 216 | static CORE_ADDR |
fba45db2 | 217 | vax_skip_prologue (CORE_ADDR pc) |
b83266a0 | 218 | { |
52f0bd74 | 219 | int op = (unsigned char) read_memory_integer (pc, 1); |
b83266a0 | 220 | if (op == 0x11) |
c5aa993b | 221 | pc += 2; /* skip brb */ |
b83266a0 | 222 | if (op == 0x31) |
c5aa993b | 223 | pc += 3; /* skip brw */ |
b83266a0 | 224 | if (op == 0xC2 |
c5aa993b JM |
225 | && ((unsigned char) read_memory_integer (pc + 2, 1)) == 0x5E) |
226 | pc += 3; /* skip subl2 */ | |
b83266a0 | 227 | if (op == 0x9E |
c5aa993b JM |
228 | && ((unsigned char) read_memory_integer (pc + 1, 1)) == 0xAE |
229 | && ((unsigned char) read_memory_integer (pc + 3, 1)) == 0x5E) | |
230 | pc += 4; /* skip movab */ | |
b83266a0 | 231 | if (op == 0x9E |
c5aa993b JM |
232 | && ((unsigned char) read_memory_integer (pc + 1, 1)) == 0xCE |
233 | && ((unsigned char) read_memory_integer (pc + 4, 1)) == 0x5E) | |
234 | pc += 5; /* skip movab */ | |
b83266a0 | 235 | if (op == 0x9E |
c5aa993b JM |
236 | && ((unsigned char) read_memory_integer (pc + 1, 1)) == 0xEE |
237 | && ((unsigned char) read_memory_integer (pc + 6, 1)) == 0x5E) | |
238 | pc += 7; /* skip movab */ | |
b83266a0 SS |
239 | return pc; |
240 | } | |
7def7fef MK |
241 | \f |
242 | ||
243 | /* Unwinding the stack is relatively easy since the VAX has a | |
244 | dedicated frame pointer, and frames are set up automatically as the | |
245 | result of a function call. Most of the relevant information can be | |
246 | inferred from the documentation of the Procedure Call Instructions | |
247 | in the VAX MACRO and Instruction Set Reference Manual. */ | |
248 | ||
249 | struct vax_frame_cache | |
250 | { | |
251 | /* Base address. */ | |
252 | CORE_ADDR base; | |
253 | ||
254 | /* Table of saved registers. */ | |
255 | struct trad_frame_saved_reg *saved_regs; | |
256 | }; | |
257 | ||
258 | struct vax_frame_cache * | |
259 | vax_frame_cache (struct frame_info *next_frame, void **this_cache) | |
260 | { | |
261 | struct vax_frame_cache *cache; | |
262 | CORE_ADDR addr; | |
263 | ULONGEST mask; | |
264 | int regnum; | |
265 | ||
266 | if (*this_cache) | |
267 | return *this_cache; | |
268 | ||
269 | /* Allocate a new cache. */ | |
270 | cache = FRAME_OBSTACK_ZALLOC (struct vax_frame_cache); | |
271 | cache->saved_regs = trad_frame_alloc_saved_regs (next_frame); | |
272 | ||
273 | /* The frame pointer is used as the base for the frame. */ | |
274 | cache->base = frame_unwind_register_unsigned (next_frame, VAX_FP_REGNUM); | |
275 | if (cache->base == 0) | |
276 | return cache; | |
277 | ||
278 | /* The register save mask and control bits determine the layout of | |
279 | the stack frame. */ | |
280 | mask = get_frame_memory_unsigned (next_frame, cache->base + 4, 4) >> 16; | |
281 | ||
282 | /* These are always saved. */ | |
283 | cache->saved_regs[VAX_PC_REGNUM].addr = cache->base + 16; | |
284 | cache->saved_regs[VAX_FP_REGNUM].addr = cache->base + 12; | |
285 | cache->saved_regs[VAX_AP_REGNUM].addr = cache->base + 8; | |
286 | cache->saved_regs[VAX_PS_REGNUM].addr = cache->base + 4; | |
287 | ||
288 | /* Scan the register save mask and record the location of the saved | |
289 | registers. */ | |
290 | addr = cache->base + 20; | |
291 | for (regnum = 0; regnum < VAX_AP_REGNUM; regnum++) | |
292 | { | |
293 | if (mask & (1 << regnum)) | |
294 | { | |
295 | cache->saved_regs[regnum].addr = addr; | |
296 | addr += 4; | |
297 | } | |
298 | } | |
299 | ||
300 | /* The CALLS/CALLG flag determines whether this frame has a General | |
301 | Argument List or a Stack Argument List. */ | |
302 | if (mask & (1 << 13)) | |
303 | { | |
304 | ULONGEST numarg; | |
305 | ||
306 | /* This is a procedure with Stack Argument List. Adjust the | |
307 | stack address for the arguments thet were pushed onto the | |
308 | stack. The return instruction will automatically pop the | |
309 | arguments from the stack. */ | |
310 | numarg = get_frame_memory_unsigned (next_frame, addr, 1); | |
311 | addr += 4 + numarg * 4; | |
312 | } | |
313 | ||
314 | /* Bits 1:0 of the stack pointer were saved in the control bits. */ | |
315 | trad_frame_set_value (cache->saved_regs, VAX_SP_REGNUM, addr + (mask >> 14)); | |
316 | ||
317 | return cache; | |
318 | } | |
319 | ||
320 | static void | |
321 | vax_frame_this_id (struct frame_info *next_frame, void **this_cache, | |
322 | struct frame_id *this_id) | |
323 | { | |
324 | struct vax_frame_cache *cache = vax_frame_cache (next_frame, this_cache); | |
325 | ||
326 | /* This marks the outermost frame. */ | |
327 | if (cache->base == 0) | |
328 | return; | |
329 | ||
330 | (*this_id) = frame_id_build (cache->base, frame_pc_unwind (next_frame)); | |
331 | } | |
332 | ||
333 | static void | |
334 | vax_frame_prev_register (struct frame_info *next_frame, void **this_cache, | |
335 | int regnum, int *optimizedp, | |
336 | enum lval_type *lvalp, CORE_ADDR *addrp, | |
337 | int *realnump, void *valuep) | |
338 | { | |
339 | struct vax_frame_cache *cache = vax_frame_cache (next_frame, this_cache); | |
340 | ||
341 | trad_frame_prev_register (next_frame, cache->saved_regs, regnum, | |
342 | optimizedp, lvalp, addrp, realnump, valuep); | |
343 | } | |
344 | ||
345 | static const struct frame_unwind vax_frame_unwind = | |
346 | { | |
347 | NORMAL_FRAME, | |
348 | vax_frame_this_id, | |
349 | vax_frame_prev_register | |
350 | }; | |
351 | ||
352 | static const struct frame_unwind * | |
353 | vax_frame_sniffer (struct frame_info *next_frame) | |
354 | { | |
355 | return &vax_frame_unwind; | |
356 | } | |
357 | \f | |
358 | ||
359 | static CORE_ADDR | |
360 | vax_frame_base_address (struct frame_info *next_frame, void **this_cache) | |
361 | { | |
362 | struct vax_frame_cache *cache = vax_frame_cache (next_frame, this_cache); | |
363 | ||
364 | return cache->base; | |
365 | } | |
b83266a0 | 366 | |
f267bd6a | 367 | static CORE_ADDR |
7def7fef MK |
368 | vax_frame_args_address (struct frame_info *next_frame, void **this_cache) |
369 | { | |
370 | return frame_unwind_register_unsigned (next_frame, VAX_AP_REGNUM); | |
371 | } | |
372 | ||
373 | static const struct frame_base vax_frame_base = | |
a33f7558 | 374 | { |
7def7fef MK |
375 | &vax_frame_unwind, |
376 | vax_frame_base_address, | |
377 | vax_frame_base_address, | |
378 | vax_frame_args_address | |
379 | }; | |
380 | ||
381 | /* Return number of arguments for FRAME. */ | |
382 | ||
383 | static int | |
384 | vax_frame_num_args (struct frame_info *frame) | |
385 | { | |
386 | CORE_ADDR args; | |
387 | ||
388 | /* Assume that the argument pointer for the outermost frame is | |
389 | hosed, as is the case on NetBSD/vax ELF. */ | |
390 | if (get_frame_base (frame) == 0) | |
391 | return 0; | |
392 | ||
393 | args = get_frame_register_unsigned (frame, VAX_AP_REGNUM); | |
394 | return get_frame_memory_unsigned (frame, args, 1); | |
395 | } | |
396 | ||
397 | static CORE_ADDR | |
398 | vax_unwind_pc (struct gdbarch *gdbarch, struct frame_info *next_frame) | |
399 | { | |
400 | return frame_unwind_register_unsigned (next_frame, VAX_PC_REGNUM); | |
a33f7558 JT |
401 | } |
402 | \f | |
7def7fef | 403 | |
f267bd6a JT |
404 | /* Initialize the current architecture based on INFO. If possible, re-use an |
405 | architecture from ARCHES, which is a list of architectures already created | |
406 | during this debugging session. | |
407 | ||
408 | Called e.g. at program startup, when reading a core file, and when reading | |
409 | a binary file. */ | |
410 | ||
411 | static struct gdbarch * | |
412 | vax_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches) | |
413 | { | |
414 | struct gdbarch *gdbarch; | |
415 | ||
4be87837 DJ |
416 | /* If there is already a candidate, use it. */ |
417 | arches = gdbarch_list_lookup_by_info (arches, &info); | |
418 | if (arches != NULL) | |
419 | return arches->gdbarch; | |
f267bd6a | 420 | |
4be87837 | 421 | gdbarch = gdbarch_alloc (&info, NULL); |
4791e091 | 422 | |
f267bd6a JT |
423 | /* Register info */ |
424 | set_gdbarch_num_regs (gdbarch, VAX_NUM_REGS); | |
5e6b39ff MK |
425 | set_gdbarch_register_name (gdbarch, vax_register_name); |
426 | set_gdbarch_register_type (gdbarch, vax_register_type); | |
f267bd6a | 427 | set_gdbarch_sp_regnum (gdbarch, VAX_SP_REGNUM); |
f267bd6a JT |
428 | set_gdbarch_pc_regnum (gdbarch, VAX_PC_REGNUM); |
429 | set_gdbarch_ps_regnum (gdbarch, VAX_PS_REGNUM); | |
430 | ||
f267bd6a JT |
431 | /* Frame and stack info */ |
432 | set_gdbarch_skip_prologue (gdbarch, vax_skip_prologue); | |
f267bd6a | 433 | set_gdbarch_frame_num_args (gdbarch, vax_frame_num_args); |
f267bd6a | 434 | |
f267bd6a JT |
435 | set_gdbarch_frame_args_skip (gdbarch, 4); |
436 | ||
5e6b39ff | 437 | /* Stack grows downward. */ |
f267bd6a JT |
438 | set_gdbarch_inner_than (gdbarch, core_addr_lessthan); |
439 | ||
440 | /* Return value info */ | |
67b441e1 | 441 | set_gdbarch_return_value (gdbarch, vax_return_value); |
f267bd6a | 442 | |
7346c184 MK |
443 | /* Call dummy code. */ |
444 | set_gdbarch_push_dummy_call (gdbarch, vax_push_dummy_call); | |
445 | set_gdbarch_unwind_dummy_id (gdbarch, vax_unwind_dummy_id); | |
f267bd6a JT |
446 | |
447 | /* Breakpoint info */ | |
1d049c5e | 448 | set_gdbarch_breakpoint_from_pc (gdbarch, vax_breakpoint_from_pc); |
f267bd6a JT |
449 | |
450 | /* Misc info */ | |
451 | set_gdbarch_function_start_offset (gdbarch, 2); | |
1d049c5e | 452 | set_gdbarch_believe_pcc_promotion (gdbarch, 1); |
f267bd6a | 453 | |
7def7fef MK |
454 | set_gdbarch_unwind_pc (gdbarch, vax_unwind_pc); |
455 | ||
456 | frame_base_set_default (gdbarch, &vax_frame_base); | |
457 | ||
4791e091 | 458 | /* Hook in ABI-specific overrides, if they have been registered. */ |
4be87837 | 459 | gdbarch_init_osabi (info, gdbarch); |
4791e091 | 460 | |
7def7fef MK |
461 | frame_unwind_append_sniffer (gdbarch, vax_frame_sniffer); |
462 | ||
ee2842e2 ILT |
463 | set_gdbarch_print_insn (gdbarch, print_insn_vax); |
464 | ||
f267bd6a JT |
465 | return (gdbarch); |
466 | } | |
c906108c | 467 | |
a78f21af AC |
468 | extern initialize_file_ftype _initialize_vax_tdep; /* -Wmissing-prototypes */ |
469 | ||
c906108c | 470 | void |
fba45db2 | 471 | _initialize_vax_tdep (void) |
c906108c | 472 | { |
4be87837 | 473 | gdbarch_register (bfd_arch_vax, vax_gdbarch_init, NULL); |
c906108c | 474 | } |