8fc299763de4ba263df46100a8929ffee6aaaad4
[external/binutils.git] / gdb / vax-tdep.c
1 /* Print VAX instructions for GDB, the GNU debugger.
2    Copyright 1986, 1989, 1991, 1992, 1995, 1996, 1998, 1999, 2000, 2002
3    Free Software Foundation, Inc.
4
5    This file is part of GDB.
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 59 Temple Place - Suite 330,
20    Boston, MA 02111-1307, USA.  */
21
22 #include "defs.h"
23 #include "symtab.h"
24 #include "opcode/vax.h"
25 #include "gdbcore.h"
26 #include "regcache.h"
27 #include "frame.h"
28 #include "value.h"
29
30 /* Return 1 if P points to an invalid floating point value.
31    LEN is the length in bytes -- not relevant on the Vax.  */
32
33 /* FIXME: cagney/2002-01-19: The macro below was originally defined in
34    tm-vax.h and used in values.c.  Two problems.  Firstly this is a
35    very non-portable and secondly it is wrong.  The VAX should be
36    using floatformat and associated methods to identify and handle
37    invalid floating-point values.  Adding to the poor target's woes
38    there is no floatformat_vax_{f,d} and no TARGET_FLOAT_FORMAT
39    et.al..  */
40
41 /* FIXME: cagney/2002-01-19: It turns out that the only thing that
42    uses this macro is the vax disassembler code (so how old is this
43    target?).  This target should instead be using the opcodes
44    disassembler.  That allowing the macro to be eliminated.  */
45
46 #define INVALID_FLOAT(p, len) ((*(short *) p & 0xff80) == 0x8000)
47
48 /* Vax instructions are never longer than this.  */
49 #define MAXLEN 62
50
51 /* Number of elements in the opcode table.  */
52 #define NOPCODES (sizeof votstrs / sizeof votstrs[0])
53
54 static unsigned char *print_insn_arg ();
55 \f
56 char *
57 vax_register_name (int regno)
58 {
59   static char *register_names[] =
60   {
61     "r0",  "r1",  "r2",  "r3", "r4", "r5", "r6", "r7",
62     "r8",  "r9", "r10", "r11", "ap", "fp", "sp", "pc",
63     "ps",
64   };
65
66   if (regno < 0)
67     return (NULL);
68   if (regno >= (sizeof(register_names) / sizeof(*register_names)))
69     return (NULL);
70   return (register_names[regno]);
71 }
72
73 int
74 vax_register_byte (int regno)
75 {
76   return (regno * 4);
77 }
78
79 int
80 vax_register_raw_size (int regno)
81 {
82   return (4);
83 }
84
85 int
86 vax_register_virtual_size (int regno)
87 {
88   return (4);
89 }
90
91 struct type *
92 vax_register_virtual_type (int regno)
93 {
94   return (builtin_type_int);
95 }
96 \f
97 void
98 vax_frame_init_saved_regs (struct frame_info *frame)
99 {
100   int regnum, regmask;
101   CORE_ADDR next_addr;
102
103   if (frame->saved_regs)
104     return;
105
106   frame_saved_regs_zalloc (frame);
107
108   regmask = read_memory_integer (frame->frame + 4, 4) >> 16;
109
110   next_addr = frame->frame + 16;
111
112   /* regmask's low bit is for register 0, which is the first one
113      what would be pushed.  */
114   for (regnum = 0; regnum < AP_REGNUM; regnum++)
115     {
116       if (regmask & (1 << regnum))
117         frame->saved_regs[regnum] = next_addr += 4;
118     }
119
120   frame->saved_regs[SP_REGNUM] = next_addr + 4;
121   if (regmask & (1 << FP_REGNUM))
122     frame->saved_regs[SP_REGNUM] +=
123       4 + (4 * read_memory_integer (next_addr + 4, 4));
124
125   frame->saved_regs[PC_REGNUM] = frame->frame + 16;
126   frame->saved_regs[FP_REGNUM] = frame->frame + 12;
127   frame->saved_regs[AP_REGNUM] = frame->frame + 8;
128   frame->saved_regs[PS_REGNUM] = frame->frame + 4;
129 }
130
131 CORE_ADDR
132 vax_frame_saved_pc (struct frame_info *frame)
133 {
134   if (frame->signal_handler_caller)
135     return (sigtramp_saved_pc (frame)); /* XXXJRT */
136
137   return (read_memory_integer (frame->frame + 16, 4));
138 }
139
140 CORE_ADDR
141 vax_frame_args_address_correct (struct frame_info *frame)
142 {
143   /* Cannot find the AP register value directly from the FP value.  Must
144      find it saved in the frame called by this one, or in the AP register
145      for the innermost frame.  However, there is no way to tell the
146      difference between the innermost frame and a frame for which we
147      just don't know the frame that it called (e.g. "info frame 0x7ffec789").
148      For the sake of argument, suppose that the stack is somewhat trashed
149      (which is one reason that "info frame" exists).  So, return 0 (indicating
150      we don't know the address of the arglist) if we don't know what frame
151      this frame calls.  */
152   if (frame->next)
153     return (read_memory_integer (frame->next->frame + 8, 4));
154
155   return (0);
156 }
157
158 CORE_ADDR
159 vax_frame_args_address (struct frame_info *frame)
160 {
161   /* In most of GDB, getting the args address is too important to
162      just say "I don't know".  This is sometimes wrong for functions
163      that aren't on top of the stack, but c'est la vie.  */
164   if (frame->next)
165     return (read_memory_integer (frame->next->frame + 8, 4));
166
167   return (read_register (AP_REGNUM));
168 }
169
170 CORE_ADDR
171 vax_frame_locals_address (struct frame_info *frame)
172 {
173   return (frame->frame);
174 }
175
176 int
177 vax_frame_num_args (struct frame_info *fi)
178 {
179   return (0xff & read_memory_integer (FRAME_ARGS_ADDRESS (fi), 1));
180 }
181
182 CORE_ADDR
183 vax_frame_chain (struct frame_info *frame)
184 {
185   /* In the case of the VAX, the frame's nominal address is the FP value,
186      and 12 bytes later comes the saved previous FP value as a 4-byte word.  */
187   if (inside_entry_file (frame->pc))
188     return (0);
189
190   return (read_memory_integer (frame->frame + 12, 4));
191 }
192 \f
193 void
194 vax_push_dummy_frame (void)
195 {
196   CORE_ADDR sp = read_register (SP_REGNUM);
197   int regnum;
198
199   sp = push_word (sp, 0);       /* arglist */
200   for (regnum = 11; regnum >= 0; regnum--)
201     sp = push_word (sp, read_register (regnum));
202   sp = push_word (sp, read_register (PC_REGNUM));
203   sp = push_word (sp, read_register (FP_REGNUM));
204   sp = push_word (sp, read_register (AP_REGNUM));
205   sp = push_word (sp, (read_register (PS_REGNUM) & 0xffef) + 0x2fff0000);
206   sp = push_word (sp, 0);
207   write_register (SP_REGNUM, sp);
208   write_register (FP_REGNUM, sp);
209   write_register (AP_REGNUM, sp + (17 * 4));
210 }
211
212 void
213 vax_pop_frame (void)
214 {
215   CORE_ADDR fp = read_register (FP_REGNUM);
216   int regnum;
217   int regmask = read_memory_integer (fp + 4, 4);
218
219   write_register (PS_REGNUM,
220                   (regmask & 0xffff)
221                   | (read_register (PS_REGNUM) & 0xffff0000));
222   write_register (PC_REGNUM, read_memory_integer (fp + 16, 4));
223   write_register (FP_REGNUM, read_memory_integer (fp + 12, 4));
224   write_register (AP_REGNUM, read_memory_integer (fp + 8, 4));
225   fp += 16;
226   for (regnum = 0; regnum < 12; regnum++)
227     if (regmask & (0x10000 << regnum))
228       write_register (regnum, read_memory_integer (fp += 4, 4));
229   fp = fp + 4 + ((regmask >> 30) & 3);
230   if (regmask & 0x20000000)
231     {
232       regnum = read_memory_integer (fp, 4);
233       fp += (regnum + 1) * 4;
234     }
235   write_register (SP_REGNUM, fp);
236   flush_cached_frames ();
237 }
238
239 /* The VAX call dummy sequence:
240
241         calls #69, @#32323232
242         bpt
243
244    It is 8 bytes long.  The address and argc are patched by
245    vax_fix_call_dummy().  */
246 LONGEST vax_call_dummy_words[] = { 0x329f69fb, 0x03323232 };
247 int sizeof_vax_call_dummy_words = sizeof(vax_call_dummy_words);
248
249 void
250 vax_fix_call_dummy (char *dummy, CORE_ADDR pc, CORE_ADDR fun, int nargs,
251                     struct value **args, struct type *type, int gcc_p)
252 {
253   dummy[1] = nargs;
254   store_unsigned_integer (dummy + 3, 4, fun);
255 }
256 \f
257 void
258 vax_store_struct_return (CORE_ADDR addr, CORE_ADDR sp)
259 {
260   write_register (1, addr);
261 }
262
263 void
264 vax_extract_return_value (struct type *valtype, char *regbuf, char *valbuf)
265 {
266   memcpy (valbuf, regbuf + REGISTER_BYTE (0), TYPE_LENGTH (valtype));
267 }
268
269 void
270 vax_store_return_value (struct type *valtype, char *valbuf)
271 {
272   write_register_bytes (0, valbuf, TYPE_LENGTH (valtype));
273 }
274
275 CORE_ADDR
276 vax_extract_struct_value_address (char *regbuf)
277 {
278   return (extract_address (regbuf + REGISTER_BYTE (0), REGISTER_RAW_SIZE (0)));
279 }
280 \f
281 /* Advance PC across any function entry prologue instructions
282    to reach some "real" code.  */
283
284 CORE_ADDR
285 vax_skip_prologue (CORE_ADDR pc)
286 {
287   register int op = (unsigned char) read_memory_integer (pc, 1);
288   if (op == 0x11)
289     pc += 2;                    /* skip brb */
290   if (op == 0x31)
291     pc += 3;                    /* skip brw */
292   if (op == 0xC2
293       && ((unsigned char) read_memory_integer (pc + 2, 1)) == 0x5E)
294     pc += 3;                    /* skip subl2 */
295   if (op == 0x9E
296       && ((unsigned char) read_memory_integer (pc + 1, 1)) == 0xAE
297       && ((unsigned char) read_memory_integer (pc + 3, 1)) == 0x5E)
298     pc += 4;                    /* skip movab */
299   if (op == 0x9E
300       && ((unsigned char) read_memory_integer (pc + 1, 1)) == 0xCE
301       && ((unsigned char) read_memory_integer (pc + 4, 1)) == 0x5E)
302     pc += 5;                    /* skip movab */
303   if (op == 0x9E
304       && ((unsigned char) read_memory_integer (pc + 1, 1)) == 0xEE
305       && ((unsigned char) read_memory_integer (pc + 6, 1)) == 0x5E)
306     pc += 7;                    /* skip movab */
307   return pc;
308 }
309
310 CORE_ADDR
311 vax_saved_pc_after_call (struct frame_info *frame)
312 {
313   return (FRAME_SAVED_PC(frame));
314 }
315 \f
316 /* Print the vax instruction at address MEMADDR in debugged memory,
317    from disassembler info INFO.
318    Returns length of the instruction, in bytes.  */
319
320 static int
321 vax_print_insn (CORE_ADDR memaddr, disassemble_info *info)
322 {
323   unsigned char buffer[MAXLEN];
324   register int i;
325   register unsigned char *p;
326   const char *d;
327
328   int status = (*info->read_memory_func) (memaddr, buffer, MAXLEN, info);
329   if (status != 0)
330     {
331       (*info->memory_error_func) (status, memaddr, info);
332       return -1;
333     }
334
335   for (i = 0; i < NOPCODES; i++)
336     if (votstrs[i].detail.code == buffer[0]
337         || votstrs[i].detail.code == *(unsigned short *) buffer)
338       break;
339
340   /* Handle undefined instructions.  */
341   if (i == NOPCODES)
342     {
343       (*info->fprintf_func) (info->stream, "0%o", buffer[0]);
344       return 1;
345     }
346
347   (*info->fprintf_func) (info->stream, "%s", votstrs[i].name);
348
349   /* Point at first byte of argument data,
350      and at descriptor for first argument.  */
351   p = buffer + 1 + (votstrs[i].detail.code >= 0x100);
352   d = votstrs[i].detail.args;
353
354   if (*d)
355     (*info->fprintf_func) (info->stream, " ");
356
357   while (*d)
358     {
359       p = print_insn_arg (d, p, memaddr + (p - buffer), info);
360       d += 2;
361       if (*d)
362         (*info->fprintf_func) (info->stream, ",");
363     }
364   return p - buffer;
365 }
366
367 static unsigned char *
368 print_insn_arg (char *d, register char *p, CORE_ADDR addr,
369                 disassemble_info *info)
370 {
371   register int regnum = *p & 0xf;
372   float floatlitbuf;
373
374   if (*d == 'b')
375     {
376       if (d[1] == 'b')
377         (*info->fprintf_func) (info->stream, "0x%x", addr + *p++ + 1);
378       else
379         {
380           (*info->fprintf_func) (info->stream, "0x%x", addr + *(short *) p + 2);
381           p += 2;
382         }
383     }
384   else
385     switch ((*p++ >> 4) & 0xf)
386       {
387       case 0:
388       case 1:
389       case 2:
390       case 3:                   /* Literal mode */
391         if (d[1] == 'd' || d[1] == 'f' || d[1] == 'g' || d[1] == 'h')
392           {
393             *(int *) &floatlitbuf = 0x4000 + ((p[-1] & 0x3f) << 4);
394             (*info->fprintf_func) (info->stream, "$%f", floatlitbuf);
395           }
396         else
397           (*info->fprintf_func) (info->stream, "$%d", p[-1] & 0x3f);
398         break;
399
400       case 4:                   /* Indexed */
401         p = (char *) print_insn_arg (d, p, addr + 1, info);
402         (*info->fprintf_func) (info->stream, "[%s]", REGISTER_NAME (regnum));
403         break;
404
405       case 5:                   /* Register */
406         (*info->fprintf_func) (info->stream, REGISTER_NAME (regnum));
407         break;
408
409       case 7:                   /* Autodecrement */
410         (*info->fprintf_func) (info->stream, "-");
411       case 6:                   /* Register deferred */
412         (*info->fprintf_func) (info->stream, "(%s)", REGISTER_NAME (regnum));
413         break;
414
415       case 9:                   /* Autoincrement deferred */
416         (*info->fprintf_func) (info->stream, "@");
417         if (regnum == PC_REGNUM)
418           {
419             (*info->fprintf_func) (info->stream, "#");
420             info->target = *(long *) p;
421             (*info->print_address_func) (info->target, info);
422             p += 4;
423             break;
424           }
425       case 8:                   /* Autoincrement */
426         if (regnum == PC_REGNUM)
427           {
428             (*info->fprintf_func) (info->stream, "#");
429             switch (d[1])
430               {
431               case 'b':
432                 (*info->fprintf_func) (info->stream, "%d", *p++);
433                 break;
434
435               case 'w':
436                 (*info->fprintf_func) (info->stream, "%d", *(short *) p);
437                 p += 2;
438                 break;
439
440               case 'l':
441                 (*info->fprintf_func) (info->stream, "%d", *(long *) p);
442                 p += 4;
443                 break;
444
445               case 'q':
446                 (*info->fprintf_func) (info->stream, "0x%x%08x",
447                                        ((long *) p)[1], ((long *) p)[0]);
448                 p += 8;
449                 break;
450
451               case 'o':
452                 (*info->fprintf_func) (info->stream, "0x%x%08x%08x%08x",
453                                        ((long *) p)[3], ((long *) p)[2],
454                                        ((long *) p)[1], ((long *) p)[0]);
455                 p += 16;
456                 break;
457
458               case 'f':
459                 if (INVALID_FLOAT (p, 4))
460                   (*info->fprintf_func) (info->stream,
461                                          "<<invalid float 0x%x>>",
462                                          *(int *) p);
463                 else
464                   (*info->fprintf_func) (info->stream, "%f", *(float *) p);
465                 p += 4;
466                 break;
467
468               case 'd':
469                 if (INVALID_FLOAT (p, 8))
470                   (*info->fprintf_func) (info->stream,
471                                          "<<invalid float 0x%x%08x>>",
472                                          ((long *) p)[1], ((long *) p)[0]);
473                 else
474                   (*info->fprintf_func) (info->stream, "%f", *(double *) p);
475                 p += 8;
476                 break;
477
478               case 'g':
479                 (*info->fprintf_func) (info->stream, "g-float");
480                 p += 8;
481                 break;
482
483               case 'h':
484                 (*info->fprintf_func) (info->stream, "h-float");
485                 p += 16;
486                 break;
487
488               }
489           }
490         else
491           (*info->fprintf_func) (info->stream, "(%s)+", REGISTER_NAME (regnum));
492         break;
493
494       case 11:                  /* Byte displacement deferred */
495         (*info->fprintf_func) (info->stream, "@");
496       case 10:                  /* Byte displacement */
497         if (regnum == PC_REGNUM)
498           {
499             info->target = addr + *p + 2;
500             (*info->print_address_func) (info->target, info);
501           }
502         else
503           (*info->fprintf_func) (info->stream, "%d(%s)", *p, REGISTER_NAME (regnum));
504         p += 1;
505         break;
506
507       case 13:                  /* Word displacement deferred */
508         (*info->fprintf_func) (info->stream, "@");
509       case 12:                  /* Word displacement */
510         if (regnum == PC_REGNUM)
511           {
512             info->target = addr + *(short *) p + 3;
513             (*info->print_address_func) (info->target, info);
514           }
515         else
516           (*info->fprintf_func) (info->stream, "%d(%s)",
517                                  *(short *) p, REGISTER_NAME (regnum));
518         p += 2;
519         break;
520
521       case 15:                  /* Long displacement deferred */
522         (*info->fprintf_func) (info->stream, "@");
523       case 14:                  /* Long displacement */
524         if (regnum == PC_REGNUM)
525           {
526             info->target = addr + *(short *) p + 5;
527             (*info->print_address_func) (info->target, info);
528           }
529         else
530           (*info->fprintf_func) (info->stream, "%d(%s)",
531                                  *(long *) p, REGISTER_NAME (regnum));
532         p += 4;
533       }
534
535   return (unsigned char *) p;
536 }
537
538 void
539 _initialize_vax_tdep (void)
540 {
541   tm_print_insn = vax_print_insn;
542 }