OSDN Git Service

* vax-tdep.c: Include "frame-base.h", "frame-unwind.h" and
[pf3gnuchains/sourceware.git] / gdb / vax-tdep.c
1 /* Print VAX instructions for GDB, the GNU debugger.
2
3    Copyright 1986, 1989, 1991, 1992, 1995, 1996, 1998, 1999, 2000,
4    2002, 2003, 2004 Free Software Foundation, Inc.
5
6    This file is part of GDB.
7
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.
12
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.
17
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.  */
22
23 #include "defs.h"
24 #include "symtab.h"
25 #include "opcode/vax.h"
26 #include "gdbcore.h"
27 #include "inferior.h"
28 #include "regcache.h"
29 #include "frame.h"
30 #include "frame-base.h"
31 #include "frame-unwind.h"
32 #include "trad-frame.h"
33 #include "value.h"
34 #include "arch-utils.h"
35 #include "gdb_string.h"
36 #include "osabi.h"
37 #include "dis-asm.h"
38
39 #include "vax-tdep.h"
40
41 /* Return the name of register REGNUM.  */
42
43 static const char *
44 vax_register_name (int regnum)
45 {
46   static char *register_names[] =
47   {
48     "r0", "r1", "r2",  "r3",  "r4", "r5", "r6", "r7",
49     "r8", "r9", "r10", "r11", "ap", "fp", "sp", "pc",
50     "ps",
51   };
52
53   if (regnum >= 0 && regnum < ARRAY_SIZE (register_names))
54     return register_names[regnum];
55
56   return NULL;
57 }
58
59 /* Return the GDB type object for the "standard" data type of data in
60    register REGNUM. */
61
62 static struct type *
63 vax_register_type (struct gdbarch *gdbarch, int regnum)
64 {
65   return builtin_type_int;
66 }
67 \f
68
69 static void
70 vax_push_dummy_frame (void)
71 {
72   CORE_ADDR sp = read_register (SP_REGNUM);
73   int regnum;
74
75   sp = push_word (sp, 0);       /* arglist */
76   for (regnum = 11; regnum >= 0; regnum--)
77     sp = push_word (sp, read_register (regnum));
78   sp = push_word (sp, read_register (PC_REGNUM));
79   sp = push_word (sp, read_register (VAX_FP_REGNUM));
80   sp = push_word (sp, read_register (VAX_AP_REGNUM));
81   sp = push_word (sp, (read_register (PS_REGNUM) & 0xffef) + 0x2fff0000);
82   sp = push_word (sp, 0);
83   write_register (SP_REGNUM, sp);
84   write_register (VAX_FP_REGNUM, sp);
85   write_register (VAX_AP_REGNUM, sp + (17 * 4));
86 }
87
88 static void
89 vax_pop_frame (void)
90 {
91   CORE_ADDR fp = read_register (VAX_FP_REGNUM);
92   int regnum;
93   int regmask = read_memory_integer (fp + 4, 4);
94
95   write_register (PS_REGNUM,
96                   (regmask & 0xffff)
97                   | (read_register (PS_REGNUM) & 0xffff0000));
98   write_register (PC_REGNUM, read_memory_integer (fp + 16, 4));
99   write_register (VAX_FP_REGNUM, read_memory_integer (fp + 12, 4));
100   write_register (VAX_AP_REGNUM, read_memory_integer (fp + 8, 4));
101   fp += 16;
102   for (regnum = 0; regnum < 12; regnum++)
103     if (regmask & (0x10000 << regnum))
104       write_register (regnum, read_memory_integer (fp += 4, 4));
105   fp = fp + 4 + ((regmask >> 30) & 3);
106   if (regmask & 0x20000000)
107     {
108       regnum = read_memory_integer (fp, 4);
109       fp += (regnum + 1) * 4;
110     }
111   write_register (SP_REGNUM, fp);
112   flush_cached_frames ();
113 }
114
115 /* The VAX call dummy sequence:
116
117         calls #69, @#32323232
118         bpt
119
120    It is 8 bytes long.  The address and argc are patched by
121    vax_fix_call_dummy().  */
122 static LONGEST vax_call_dummy_words[] = { 0x329f69fb, 0x03323232 };
123 static int sizeof_vax_call_dummy_words = sizeof(vax_call_dummy_words);
124
125 static void
126 vax_fix_call_dummy (char *dummy, CORE_ADDR pc, CORE_ADDR fun, int nargs,
127                     struct value **args, struct type *type, int gcc_p)
128 {
129   dummy[1] = nargs;
130   store_unsigned_integer (dummy + 3, 4, fun);
131 }
132 \f
133 static void
134 vax_store_struct_return (CORE_ADDR addr, CORE_ADDR sp)
135 {
136   write_register (1, addr);
137 }
138
139 static void
140 vax_extract_return_value (struct type *valtype, char *regbuf, char *valbuf)
141 {
142   memcpy (valbuf, regbuf + DEPRECATED_REGISTER_BYTE (0), TYPE_LENGTH (valtype));
143 }
144
145 static void
146 vax_store_return_value (struct type *valtype, char *valbuf)
147 {
148   deprecated_write_register_bytes (0, valbuf, TYPE_LENGTH (valtype));
149 }
150 \f
151
152 /* Use the program counter to determine the contents and size of a
153    breakpoint instruction.  Return a pointer to a string of bytes that
154    encode a breakpoint instruction, store the length of the string in
155    *LEN and optionally adjust *PC to point to the correct memory
156    location for inserting the breakpoint.  */
157    
158 static const unsigned char *
159 vax_breakpoint_from_pc (CORE_ADDR *pc, int *len)
160 {
161   static unsigned char break_insn[] = { 3 };
162
163   *len = sizeof (break_insn);
164   return break_insn;
165 }
166 \f
167 /* Advance PC across any function entry prologue instructions
168    to reach some "real" code.  */
169
170 static CORE_ADDR
171 vax_skip_prologue (CORE_ADDR pc)
172 {
173   int op = (unsigned char) read_memory_integer (pc, 1);
174   if (op == 0x11)
175     pc += 2;                    /* skip brb */
176   if (op == 0x31)
177     pc += 3;                    /* skip brw */
178   if (op == 0xC2
179       && ((unsigned char) read_memory_integer (pc + 2, 1)) == 0x5E)
180     pc += 3;                    /* skip subl2 */
181   if (op == 0x9E
182       && ((unsigned char) read_memory_integer (pc + 1, 1)) == 0xAE
183       && ((unsigned char) read_memory_integer (pc + 3, 1)) == 0x5E)
184     pc += 4;                    /* skip movab */
185   if (op == 0x9E
186       && ((unsigned char) read_memory_integer (pc + 1, 1)) == 0xCE
187       && ((unsigned char) read_memory_integer (pc + 4, 1)) == 0x5E)
188     pc += 5;                    /* skip movab */
189   if (op == 0x9E
190       && ((unsigned char) read_memory_integer (pc + 1, 1)) == 0xEE
191       && ((unsigned char) read_memory_integer (pc + 6, 1)) == 0x5E)
192     pc += 7;                    /* skip movab */
193   return pc;
194 }
195 \f
196
197 /* Unwinding the stack is relatively easy since the VAX has a
198    dedicated frame pointer, and frames are set up automatically as the
199    result of a function call.  Most of the relevant information can be
200    inferred from the documentation of the Procedure Call Instructions
201    in the VAX MACRO and Instruction Set Reference Manual.  */
202
203 struct vax_frame_cache
204 {
205   /* Base address.  */
206   CORE_ADDR base;
207
208   /* Table of saved registers.  */
209   struct trad_frame_saved_reg *saved_regs;
210 };
211
212 struct vax_frame_cache *
213 vax_frame_cache (struct frame_info *next_frame, void **this_cache)
214 {
215   struct vax_frame_cache *cache;
216   CORE_ADDR addr;
217   ULONGEST mask;
218   int regnum;
219
220   if (*this_cache)
221     return *this_cache;
222
223   /* Allocate a new cache.  */
224   cache = FRAME_OBSTACK_ZALLOC (struct vax_frame_cache);
225   cache->saved_regs = trad_frame_alloc_saved_regs (next_frame);
226
227   /* The frame pointer is used as the base for the frame.  */
228   cache->base = frame_unwind_register_unsigned (next_frame, VAX_FP_REGNUM);
229   if (cache->base == 0)
230     return cache;
231
232   /* The register save mask and control bits determine the layout of
233      the stack frame.  */
234   mask = get_frame_memory_unsigned (next_frame, cache->base + 4, 4) >> 16;
235
236   /* These are always saved.  */
237   cache->saved_regs[VAX_PC_REGNUM].addr = cache->base + 16;
238   cache->saved_regs[VAX_FP_REGNUM].addr = cache->base + 12;
239   cache->saved_regs[VAX_AP_REGNUM].addr = cache->base + 8;
240   cache->saved_regs[VAX_PS_REGNUM].addr = cache->base + 4;
241
242   /* Scan the register save mask and record the location of the saved
243      registers.  */
244   addr = cache->base + 20;
245   for (regnum = 0; regnum < VAX_AP_REGNUM; regnum++)
246     {
247       if (mask & (1 << regnum))
248         {
249           cache->saved_regs[regnum].addr = addr;
250           addr += 4;
251         }
252     }
253
254   /* The CALLS/CALLG flag determines whether this frame has a General
255      Argument List or a Stack Argument List.  */
256   if (mask & (1 << 13))
257     {
258       ULONGEST numarg;
259
260       /* This is a procedure with Stack Argument List.  Adjust the
261          stack address for the arguments thet were pushed onto the
262          stack.  The return instruction will automatically pop the
263          arguments from the stack.  */
264       numarg = get_frame_memory_unsigned (next_frame, addr, 1);
265       addr += 4 + numarg * 4;
266     }
267
268   /* Bits 1:0 of the stack pointer were saved in the control bits.  */
269   trad_frame_set_value (cache->saved_regs, VAX_SP_REGNUM, addr + (mask >> 14));
270
271   return cache;
272 }
273
274 static void
275 vax_frame_this_id (struct frame_info *next_frame, void **this_cache,
276                    struct frame_id *this_id)
277 {
278   struct vax_frame_cache *cache = vax_frame_cache (next_frame, this_cache);
279
280   /* This marks the outermost frame.  */
281   if (cache->base == 0)
282     return;
283
284   (*this_id) = frame_id_build (cache->base, frame_pc_unwind (next_frame));
285 }
286
287 static void
288 vax_frame_prev_register (struct frame_info *next_frame, void **this_cache,
289                          int regnum, int *optimizedp,
290                          enum lval_type *lvalp, CORE_ADDR *addrp,
291                          int *realnump, void *valuep)
292 {
293   struct vax_frame_cache *cache = vax_frame_cache (next_frame, this_cache);
294
295   trad_frame_prev_register (next_frame, cache->saved_regs, regnum,
296                             optimizedp, lvalp, addrp, realnump, valuep);
297 }
298
299 static const struct frame_unwind vax_frame_unwind =
300 {
301   NORMAL_FRAME,
302   vax_frame_this_id,
303   vax_frame_prev_register
304 };
305
306 static const struct frame_unwind *
307 vax_frame_sniffer (struct frame_info *next_frame)
308 {
309   return &vax_frame_unwind;
310 }
311 \f
312
313 static CORE_ADDR
314 vax_frame_base_address (struct frame_info *next_frame, void **this_cache)
315 {
316   struct vax_frame_cache *cache = vax_frame_cache (next_frame, this_cache);
317
318   return cache->base;
319 }
320
321 static CORE_ADDR
322 vax_frame_args_address (struct frame_info *next_frame, void **this_cache)
323 {
324   return frame_unwind_register_unsigned (next_frame, VAX_AP_REGNUM);
325 }
326
327 static const struct frame_base vax_frame_base =
328 {
329   &vax_frame_unwind,
330   vax_frame_base_address,
331   vax_frame_base_address,
332   vax_frame_args_address
333 };
334
335 /* Return number of arguments for FRAME.  */
336
337 static int
338 vax_frame_num_args (struct frame_info *frame)
339 {
340   CORE_ADDR args;
341
342   /* Assume that the argument pointer for the outermost frame is
343      hosed, as is the case on NetBSD/vax ELF.  */
344   if (get_frame_base (frame) == 0)
345     return 0;
346
347   args = get_frame_register_unsigned (frame, VAX_AP_REGNUM);
348   return get_frame_memory_unsigned (frame, args, 1);
349 }
350
351 static CORE_ADDR
352 vax_unwind_pc (struct gdbarch *gdbarch, struct frame_info *next_frame)
353 {
354   return frame_unwind_register_unsigned (next_frame, VAX_PC_REGNUM);
355 }
356 \f
357
358 /* Initialize the current architecture based on INFO.  If possible, re-use an
359    architecture from ARCHES, which is a list of architectures already created
360    during this debugging session.
361
362    Called e.g. at program startup, when reading a core file, and when reading
363    a binary file.  */
364
365 static struct gdbarch *
366 vax_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
367 {
368   struct gdbarch *gdbarch;
369
370   /* If there is already a candidate, use it.  */
371   arches = gdbarch_list_lookup_by_info (arches, &info);
372   if (arches != NULL)
373     return arches->gdbarch;
374
375   gdbarch = gdbarch_alloc (&info, NULL);
376
377   /* Register info */
378   set_gdbarch_num_regs (gdbarch, VAX_NUM_REGS);
379   set_gdbarch_register_name (gdbarch, vax_register_name);
380   set_gdbarch_register_type (gdbarch, vax_register_type);
381   set_gdbarch_sp_regnum (gdbarch, VAX_SP_REGNUM);
382   set_gdbarch_pc_regnum (gdbarch, VAX_PC_REGNUM);
383   set_gdbarch_ps_regnum (gdbarch, VAX_PS_REGNUM);
384
385   /* Frame and stack info */
386   set_gdbarch_skip_prologue (gdbarch, vax_skip_prologue);
387   set_gdbarch_frame_num_args (gdbarch, vax_frame_num_args);
388
389   set_gdbarch_frame_args_skip (gdbarch, 4);
390
391   /* Stack grows downward.  */
392   set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
393
394   /* Return value info */
395   set_gdbarch_deprecated_store_struct_return (gdbarch, vax_store_struct_return);
396   set_gdbarch_deprecated_extract_return_value (gdbarch, vax_extract_return_value);
397   set_gdbarch_deprecated_store_return_value (gdbarch, vax_store_return_value);
398
399   /* Call dummy info */
400   set_gdbarch_deprecated_push_dummy_frame (gdbarch, vax_push_dummy_frame);
401   set_gdbarch_deprecated_pop_frame (gdbarch, vax_pop_frame);
402   set_gdbarch_call_dummy_location (gdbarch, ON_STACK);
403   set_gdbarch_deprecated_call_dummy_words (gdbarch, vax_call_dummy_words);
404   set_gdbarch_deprecated_sizeof_call_dummy_words (gdbarch, sizeof_vax_call_dummy_words);
405   set_gdbarch_deprecated_fix_call_dummy (gdbarch, vax_fix_call_dummy);
406   set_gdbarch_deprecated_call_dummy_breakpoint_offset (gdbarch, 7);
407   set_gdbarch_deprecated_use_generic_dummy_frames (gdbarch, 0);
408   set_gdbarch_deprecated_pc_in_call_dummy (gdbarch, deprecated_pc_in_call_dummy_on_stack);
409
410   /* Breakpoint info */
411   set_gdbarch_breakpoint_from_pc (gdbarch, vax_breakpoint_from_pc);
412
413   /* Misc info */
414   set_gdbarch_function_start_offset (gdbarch, 2);
415   set_gdbarch_believe_pcc_promotion (gdbarch, 1);
416
417   /* Should be using push_dummy_call.  */
418   set_gdbarch_deprecated_dummy_write_sp (gdbarch, deprecated_write_sp);
419
420   set_gdbarch_unwind_pc (gdbarch, vax_unwind_pc);
421
422   frame_base_set_default (gdbarch, &vax_frame_base);
423
424   /* Hook in ABI-specific overrides, if they have been registered.  */
425   gdbarch_init_osabi (info, gdbarch);
426
427   frame_unwind_append_sniffer (gdbarch, vax_frame_sniffer);
428
429   set_gdbarch_print_insn (gdbarch, print_insn_vax);
430
431   return (gdbarch);
432 }
433
434 extern initialize_file_ftype _initialize_vax_tdep; /* -Wmissing-prototypes */
435
436 void
437 _initialize_vax_tdep (void)
438 {
439   gdbarch_register (bfd_arch_vax, vax_gdbarch_init, NULL);
440 }