Upload Tizen:Base source
[external/gdb.git] / gdb / vax-tdep.c
1 /* Target-dependent code for the VAX.
2
3    Copyright (C) 1986, 1989, 1991, 1992, 1995, 1996, 1998, 1999, 2000, 2002,
4    2003, 2004, 2005, 2007, 2008, 2009, 2010 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 3 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, see <http://www.gnu.org/licenses/>.  */
20
21 #include "defs.h"
22 #include "arch-utils.h"
23 #include "dis-asm.h"
24 #include "floatformat.h"
25 #include "frame.h"
26 #include "frame-base.h"
27 #include "frame-unwind.h"
28 #include "gdbcore.h"
29 #include "gdbtypes.h"
30 #include "osabi.h"
31 #include "regcache.h"
32 #include "regset.h"
33 #include "trad-frame.h"
34 #include "value.h"
35
36 #include "gdb_string.h"
37
38 #include "vax-tdep.h"
39
40 /* Return the name of register REGNUM.  */
41
42 static const char *
43 vax_register_name (struct gdbarch *gdbarch, int regnum)
44 {
45   static char *register_names[] =
46   {
47     "r0", "r1", "r2",  "r3",  "r4", "r5", "r6", "r7",
48     "r8", "r9", "r10", "r11", "ap", "fp", "sp", "pc",
49     "ps",
50   };
51
52   if (regnum >= 0 && regnum < ARRAY_SIZE (register_names))
53     return register_names[regnum];
54
55   return NULL;
56 }
57
58 /* Return the GDB type object for the "standard" data type of data in
59    register REGNUM. */
60
61 static struct type *
62 vax_register_type (struct gdbarch *gdbarch, int regnum)
63 {
64   return builtin_type (gdbarch)->builtin_int;
65 }
66 \f
67 /* Core file support.  */
68
69 /* Supply register REGNUM from the buffer specified by GREGS and LEN
70    in the general-purpose register set REGSET to register cache
71    REGCACHE.  If REGNUM is -1, do this for all registers in REGSET.  */
72
73 static void
74 vax_supply_gregset (const struct regset *regset, struct regcache *regcache,
75                     int regnum, const void *gregs, size_t len)
76 {
77   const gdb_byte *regs = gregs;
78   int i;
79
80   for (i = 0; i < VAX_NUM_REGS; i++)
81     {
82       if (regnum == i || regnum == -1)
83         regcache_raw_supply (regcache, i, regs + i * 4);
84     }
85 }
86
87 /* VAX register set.  */
88
89 static struct regset vax_gregset =
90 {
91   NULL,
92   vax_supply_gregset
93 };
94
95 /* Return the appropriate register set for the core section identified
96    by SECT_NAME and SECT_SIZE.  */
97
98 static const struct regset *
99 vax_regset_from_core_section (struct gdbarch *gdbarch,
100                               const char *sect_name, size_t sect_size)
101 {
102   if (strcmp (sect_name, ".reg") == 0 && sect_size >= VAX_NUM_REGS * 4)
103     return &vax_gregset;
104
105   return NULL;
106 }
107 \f
108 /* The VAX UNIX calling convention uses R1 to pass a structure return
109    value address instead of passing it as a first (hidden) argument as
110    the VMS calling convention suggests.  */
111
112 static CORE_ADDR
113 vax_store_arguments (struct regcache *regcache, int nargs,
114                      struct value **args, CORE_ADDR sp)
115 {
116   struct gdbarch *gdbarch = get_regcache_arch (regcache);
117   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
118   gdb_byte buf[4];
119   int count = 0;
120   int i;
121
122   /* We create an argument list on the stack, and make the argument
123      pointer to it.  */
124
125   /* Push arguments in reverse order.  */
126   for (i = nargs - 1; i >= 0; i--)
127     {
128       int len = TYPE_LENGTH (value_enclosing_type (args[i]));
129
130       sp -= (len + 3) & ~3;
131       count += (len + 3) / 4;
132       write_memory (sp, value_contents_all (args[i]), len);
133     }
134
135   /* Push argument count.  */
136   sp -= 4;
137   store_unsigned_integer (buf, 4, byte_order, count);
138   write_memory (sp, buf, 4);
139
140   /* Update the argument pointer.  */
141   store_unsigned_integer (buf, 4, byte_order, sp);
142   regcache_cooked_write (regcache, VAX_AP_REGNUM, buf);
143
144   return sp;
145 }
146
147 static CORE_ADDR
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)
152 {
153   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
154   CORE_ADDR fp = sp;
155   gdb_byte buf[4];
156
157   /* Set up the function arguments.  */
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);
163
164   /* Store return address in the PC slot.  */
165   sp -= 4;
166   store_unsigned_integer (buf, 4, byte_order, 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, byte_order, 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, byte_order, 0);
180   write_memory (sp, buf, 4);
181
182   /* Store condition handler.  */
183   sp -= 4;
184   store_unsigned_integer (buf, 4, byte_order, 0);
185   write_memory (sp, buf, 4);
186
187   /* Update the stack pointer and frame pointer.  */
188   store_unsigned_integer (buf, 4, byte_order, 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
196 static struct frame_id
197 vax_dummy_id (struct gdbarch *gdbarch, struct frame_info *this_frame)
198 {
199   CORE_ADDR fp;
200
201   fp = get_frame_register_unsigned (this_frame, VAX_FP_REGNUM);
202   return frame_id_build (fp, get_frame_pc (this_frame));
203 }
204 \f
205
206 static enum return_value_convention
207 vax_return_value (struct gdbarch *gdbarch, struct type *func_type,
208                   struct type *type, struct regcache *regcache,
209                   gdb_byte *readbuf, const gdb_byte *writebuf)
210 {
211   int len = TYPE_LENGTH (type);
212   gdb_byte buf[8];
213
214   if (TYPE_CODE (type) == TYPE_CODE_STRUCT
215       || TYPE_CODE (type) == TYPE_CODE_UNION
216       || TYPE_CODE (type) == TYPE_CODE_ARRAY)
217     {
218       /* The default on VAX is to return structures in static memory.
219          Consequently a function must return the address where we can
220          find the return value.  */
221
222       if (readbuf)
223         {
224           ULONGEST addr;
225
226           regcache_raw_read_unsigned (regcache, VAX_R0_REGNUM, &addr);
227           read_memory (addr, readbuf, len);
228         }
229
230       return RETURN_VALUE_ABI_RETURNS_ADDRESS;
231     }
232
233   if (readbuf)
234     {
235       /* Read the contents of R0 and (if necessary) R1.  */
236       regcache_cooked_read (regcache, VAX_R0_REGNUM, buf);
237       if (len > 4)
238         regcache_cooked_read (regcache, VAX_R1_REGNUM, buf + 4);
239       memcpy (readbuf, buf, len);
240     }
241   if (writebuf)
242     {
243       /* Read the contents to R0 and (if necessary) R1.  */
244       memcpy (buf, writebuf, len);
245       regcache_cooked_write (regcache, VAX_R0_REGNUM, buf);
246       if (len > 4)
247         regcache_cooked_write (regcache, VAX_R1_REGNUM, buf + 4);
248     }
249
250   return RETURN_VALUE_REGISTER_CONVENTION;
251 }
252 \f
253
254 /* Use the program counter to determine the contents and size of a
255    breakpoint instruction.  Return a pointer to a string of bytes that
256    encode a breakpoint instruction, store the length of the string in
257    *LEN and optionally adjust *PC to point to the correct memory
258    location for inserting the breakpoint.  */
259    
260 static const gdb_byte *
261 vax_breakpoint_from_pc (struct gdbarch *gdbarch, CORE_ADDR *pc, int *len)
262 {
263   static gdb_byte break_insn[] = { 3 };
264
265   *len = sizeof (break_insn);
266   return break_insn;
267 }
268 \f
269 /* Advance PC across any function entry prologue instructions
270    to reach some "real" code.  */
271
272 static CORE_ADDR
273 vax_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
274 {
275   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
276   gdb_byte op = read_memory_unsigned_integer (pc, 1, byte_order);
277
278   if (op == 0x11)
279     pc += 2;                    /* skip brb */
280   if (op == 0x31)
281     pc += 3;                    /* skip brw */
282   if (op == 0xC2
283       && read_memory_unsigned_integer (pc + 2, 1, byte_order) == 0x5E)
284     pc += 3;                    /* skip subl2 */
285   if (op == 0x9E
286       && read_memory_unsigned_integer (pc + 1, 1, byte_order) == 0xAE
287       && read_memory_unsigned_integer (pc + 3, 1, byte_order) == 0x5E)
288     pc += 4;                    /* skip movab */
289   if (op == 0x9E
290       && read_memory_unsigned_integer (pc + 1, 1, byte_order) == 0xCE
291       && read_memory_unsigned_integer (pc + 4, 1, byte_order) == 0x5E)
292     pc += 5;                    /* skip movab */
293   if (op == 0x9E
294       && read_memory_unsigned_integer (pc + 1, 1, byte_order) == 0xEE
295       && read_memory_unsigned_integer (pc + 6, 1, byte_order) == 0x5E)
296     pc += 7;                    /* skip movab */
297
298   return pc;
299 }
300 \f
301
302 /* Unwinding the stack is relatively easy since the VAX has a
303    dedicated frame pointer, and frames are set up automatically as the
304    result of a function call.  Most of the relevant information can be
305    inferred from the documentation of the Procedure Call Instructions
306    in the VAX MACRO and Instruction Set Reference Manual.  */
307
308 struct vax_frame_cache
309 {
310   /* Base address.  */
311   CORE_ADDR base;
312
313   /* Table of saved registers.  */
314   struct trad_frame_saved_reg *saved_regs;
315 };
316
317 static struct vax_frame_cache *
318 vax_frame_cache (struct frame_info *this_frame, void **this_cache)
319 {
320   struct vax_frame_cache *cache;
321   CORE_ADDR addr;
322   ULONGEST mask;
323   int regnum;
324
325   if (*this_cache)
326     return *this_cache;
327
328   /* Allocate a new cache.  */
329   cache = FRAME_OBSTACK_ZALLOC (struct vax_frame_cache);
330   cache->saved_regs = trad_frame_alloc_saved_regs (this_frame);
331
332   /* The frame pointer is used as the base for the frame.  */
333   cache->base = get_frame_register_unsigned (this_frame, VAX_FP_REGNUM);
334   if (cache->base == 0)
335     return cache;
336
337   /* The register save mask and control bits determine the layout of
338      the stack frame.  */
339   mask = get_frame_memory_unsigned (this_frame, cache->base + 4, 4) >> 16;
340
341   /* These are always saved.  */
342   cache->saved_regs[VAX_PC_REGNUM].addr = cache->base + 16;
343   cache->saved_regs[VAX_FP_REGNUM].addr = cache->base + 12;
344   cache->saved_regs[VAX_AP_REGNUM].addr = cache->base + 8;
345   cache->saved_regs[VAX_PS_REGNUM].addr = cache->base + 4;
346
347   /* Scan the register save mask and record the location of the saved
348      registers.  */
349   addr = cache->base + 20;
350   for (regnum = 0; regnum < VAX_AP_REGNUM; regnum++)
351     {
352       if (mask & (1 << regnum))
353         {
354           cache->saved_regs[regnum].addr = addr;
355           addr += 4;
356         }
357     }
358
359   /* The CALLS/CALLG flag determines whether this frame has a General
360      Argument List or a Stack Argument List.  */
361   if (mask & (1 << 13))
362     {
363       ULONGEST numarg;
364
365       /* This is a procedure with Stack Argument List.  Adjust the
366          stack address for the arguments that were pushed onto the
367          stack.  The return instruction will automatically pop the
368          arguments from the stack.  */
369       numarg = get_frame_memory_unsigned (this_frame, addr, 1);
370       addr += 4 + numarg * 4;
371     }
372
373   /* Bits 1:0 of the stack pointer were saved in the control bits.  */
374   trad_frame_set_value (cache->saved_regs, VAX_SP_REGNUM, addr + (mask >> 14));
375
376   return cache;
377 }
378
379 static void
380 vax_frame_this_id (struct frame_info *this_frame, void **this_cache,
381                    struct frame_id *this_id)
382 {
383   struct vax_frame_cache *cache = vax_frame_cache (this_frame, this_cache);
384
385   /* This marks the outermost frame.  */
386   if (cache->base == 0)
387     return;
388
389   (*this_id) = frame_id_build (cache->base, get_frame_func (this_frame));
390 }
391
392 static struct value *
393 vax_frame_prev_register (struct frame_info *this_frame,
394                          void **this_cache, int regnum)
395 {
396   struct vax_frame_cache *cache = vax_frame_cache (this_frame, this_cache);
397
398   return trad_frame_get_prev_register (this_frame, cache->saved_regs, regnum);
399 }
400
401 static const struct frame_unwind vax_frame_unwind =
402 {
403   NORMAL_FRAME,
404   vax_frame_this_id,
405   vax_frame_prev_register,
406   NULL,
407   default_frame_sniffer
408 };
409 \f
410
411 static CORE_ADDR
412 vax_frame_base_address (struct frame_info *this_frame, void **this_cache)
413 {
414   struct vax_frame_cache *cache = vax_frame_cache (this_frame, this_cache);
415
416   return cache->base;
417 }
418
419 static CORE_ADDR
420 vax_frame_args_address (struct frame_info *this_frame, void **this_cache)
421 {
422   return get_frame_register_unsigned (this_frame, VAX_AP_REGNUM);
423 }
424
425 static const struct frame_base vax_frame_base =
426 {
427   &vax_frame_unwind,
428   vax_frame_base_address,
429   vax_frame_base_address,
430   vax_frame_args_address
431 };
432
433 /* Return number of arguments for FRAME.  */
434
435 static int
436 vax_frame_num_args (struct frame_info *frame)
437 {
438   CORE_ADDR args;
439
440   /* Assume that the argument pointer for the outermost frame is
441      hosed, as is the case on NetBSD/vax ELF.  */
442   if (get_frame_base_address (frame) == 0)
443     return 0;
444
445   args = get_frame_register_unsigned (frame, VAX_AP_REGNUM);
446   return get_frame_memory_unsigned (frame, args, 1);
447 }
448
449 static CORE_ADDR
450 vax_unwind_pc (struct gdbarch *gdbarch, struct frame_info *next_frame)
451 {
452   return frame_unwind_register_unsigned (next_frame, VAX_PC_REGNUM);
453 }
454 \f
455
456 /* Initialize the current architecture based on INFO.  If possible, re-use an
457    architecture from ARCHES, which is a list of architectures already created
458    during this debugging session.
459
460    Called e.g. at program startup, when reading a core file, and when reading
461    a binary file.  */
462
463 static struct gdbarch *
464 vax_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
465 {
466   struct gdbarch *gdbarch;
467
468   /* If there is already a candidate, use it.  */
469   arches = gdbarch_list_lookup_by_info (arches, &info);
470   if (arches != NULL)
471     return arches->gdbarch;
472
473   gdbarch = gdbarch_alloc (&info, NULL);
474
475   set_gdbarch_float_format (gdbarch, floatformats_vax_f);
476   set_gdbarch_double_format (gdbarch, floatformats_vax_d);
477   set_gdbarch_long_double_format (gdbarch, floatformats_vax_d);
478   set_gdbarch_long_double_bit (gdbarch, 64);
479
480   /* Register info */
481   set_gdbarch_num_regs (gdbarch, VAX_NUM_REGS);
482   set_gdbarch_register_name (gdbarch, vax_register_name);
483   set_gdbarch_register_type (gdbarch, vax_register_type);
484   set_gdbarch_sp_regnum (gdbarch, VAX_SP_REGNUM);
485   set_gdbarch_pc_regnum (gdbarch, VAX_PC_REGNUM);
486   set_gdbarch_ps_regnum (gdbarch, VAX_PS_REGNUM);
487
488   set_gdbarch_regset_from_core_section
489     (gdbarch, vax_regset_from_core_section);
490
491   /* Frame and stack info */
492   set_gdbarch_skip_prologue (gdbarch, vax_skip_prologue);
493   set_gdbarch_frame_num_args (gdbarch, vax_frame_num_args);
494   set_gdbarch_frame_args_skip (gdbarch, 4);
495
496   /* Stack grows downward.  */
497   set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
498
499   /* Return value info */
500   set_gdbarch_return_value (gdbarch, vax_return_value);
501
502   /* Call dummy code.  */
503   set_gdbarch_push_dummy_call (gdbarch, vax_push_dummy_call);
504   set_gdbarch_dummy_id (gdbarch, vax_dummy_id);
505
506   /* Breakpoint info */
507   set_gdbarch_breakpoint_from_pc (gdbarch, vax_breakpoint_from_pc);
508
509   /* Misc info */
510   set_gdbarch_deprecated_function_start_offset (gdbarch, 2);
511   set_gdbarch_believe_pcc_promotion (gdbarch, 1);
512
513   set_gdbarch_print_insn (gdbarch, print_insn_vax);
514
515   set_gdbarch_unwind_pc (gdbarch, vax_unwind_pc);
516
517   frame_base_set_default (gdbarch, &vax_frame_base);
518
519   /* Hook in ABI-specific overrides, if they have been registered.  */
520   gdbarch_init_osabi (info, gdbarch);
521
522   frame_unwind_append_unwinder (gdbarch, &vax_frame_unwind);
523
524   return (gdbarch);
525 }
526
527 /* Provide a prototype to silence -Wmissing-prototypes.  */
528 void _initialize_vax_tdep (void);
529
530 void
531 _initialize_vax_tdep (void)
532 {
533   gdbarch_register (bfd_arch_vax, vax_gdbarch_init, NULL);
534 }