Use ".word 0x0012 # Entry mask: r1 r2 >" instead of just "Entry mask: < r1 ... >"
[platform/upstream/binutils.git] / opcodes / vax-dis.c
1 /* Print VAX instructions.
2    Copyright 1995, 1998, 2000, 2001, 2002, 2005
3    Free Software Foundation, Inc.
4    Contributed by Pauline Middelink <middelin@polyware.iaf.nl>
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
19
20 #include "sysdep.h"
21 #include "opcode/vax.h"
22 #include "dis-asm.h"
23
24 /* Local function prototypes */
25 static int fetch_data PARAMS ((struct disassemble_info *, bfd_byte *));
26 static int print_insn_arg
27   PARAMS ((const char *, unsigned char *, bfd_vma, disassemble_info *));
28 static int print_insn_mode
29   PARAMS ((const char *, int, unsigned char *, bfd_vma, disassemble_info *));
30
31
32 static char *reg_names[] =
33 {
34   "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
35   "r8", "r9", "r10", "r11", "ap", "fp", "sp", "pc"
36 };
37
38 /* Definitions for the function entry mask bits.  */
39 static char *entry_mask_bit[] =
40 {
41   /* Registers 0 and 1 shall not be saved, since they're used to pass back
42      a function's result to its caller...  */
43   "~r0~", "~r1~",
44   /* Registers 2 .. 11 are normal registers.  */
45   "r2", "r3", "r4", "r5", "r6", "r7", "r8", "r9", "r10", "r11",
46   /* Registers 12 and 13 are argument and frame pointer and must not
47      be saved by using the entry mask.  */
48   "~ap~", "~fp~",
49   /* Bits 14 and 15 control integer and decimal overflow.  */
50   "IntOvfl", "DecOvfl",
51 };
52
53 /* Sign-extend an (unsigned char). */
54 #if __STDC__ == 1
55 #define COERCE_SIGNED_CHAR(ch) ((signed char)(ch))
56 #else
57 #define COERCE_SIGNED_CHAR(ch) ((int)(((ch) ^ 0x80) & 0xFF) - 128)
58 #endif
59
60 /* Get a 1 byte signed integer.  */
61 #define NEXTBYTE(p)  \
62   (p += 1, FETCH_DATA (info, p), \
63   COERCE_SIGNED_CHAR(p[-1]))
64
65 /* Get a 2 byte signed integer.  */
66 #define COERCE16(x) ((int) (((x) ^ 0x8000) - 0x8000))
67 #define NEXTWORD(p)  \
68   (p += 2, FETCH_DATA (info, p), \
69    COERCE16 ((p[-1] << 8) + p[-2]))
70
71 /* Get a 4 byte signed integer.  */
72 #define COERCE32(x) ((int) (((x) ^ 0x80000000) - 0x80000000))
73 #define NEXTLONG(p)  \
74   (p += 4, FETCH_DATA (info, p), \
75    (COERCE32 ((((((p[-1] << 8) + p[-2]) << 8) + p[-3]) << 8) + p[-4])))
76
77 /* Maximum length of an instruction.  */
78 #define MAXLEN 25
79
80 #include <setjmp.h>
81
82 struct private
83 {
84   /* Points to first byte not fetched.  */
85   bfd_byte *max_fetched;
86   bfd_byte the_buffer[MAXLEN];
87   bfd_vma insn_start;
88   jmp_buf bailout;
89 };
90
91 /* Make sure that bytes from INFO->PRIVATE_DATA->BUFFER (inclusive)
92    to ADDR (exclusive) are valid.  Returns 1 for success, longjmps
93    on error.  */
94 #define FETCH_DATA(info, addr) \
95   ((addr) <= ((struct private *)(info->private_data))->max_fetched \
96    ? 1 : fetch_data ((info), (addr)))
97
98 static int
99 fetch_data (info, addr)
100      struct disassemble_info *info;
101      bfd_byte *addr;
102 {
103   int status;
104   struct private *priv = (struct private *) info->private_data;
105   bfd_vma start = priv->insn_start + (priv->max_fetched - priv->the_buffer);
106
107   status = (*info->read_memory_func) (start,
108                                       priv->max_fetched,
109                                       addr - priv->max_fetched,
110                                       info);
111   if (status != 0)
112     {
113       (*info->memory_error_func) (status, start, info);
114       longjmp (priv->bailout, 1);
115     }
116   else
117     priv->max_fetched = addr;
118
119   return 1;
120 }
121
122 /* Print the vax instruction at address MEMADDR in debugged memory,
123    on INFO->STREAM.  Returns length of the instruction, in bytes.  */
124
125 int
126 print_insn_vax (memaddr, info)
127      bfd_vma memaddr;
128      disassemble_info *info;
129 {
130   const struct vot *votp;
131   const char *argp;
132   unsigned char *arg;
133   struct private priv;
134   bfd_byte *buffer = priv.the_buffer;
135
136   info->private_data = (PTR) &priv;
137   priv.max_fetched = priv.the_buffer;
138   priv.insn_start = memaddr;
139
140   if (setjmp (priv.bailout) != 0)
141     {
142       /* Error return.  */
143       return -1;
144     }
145
146   argp = NULL;
147   /* Check if the info buffer has more than one byte left since
148      the last opcode might be a single byte with no argument data.  */
149   if (info->buffer_length - (memaddr - info->buffer_vma) > 1)
150     {
151       FETCH_DATA (info, buffer + 2);
152     }
153   else
154     {
155       FETCH_DATA (info, buffer + 1);
156       buffer[1] = 0;
157     }
158
159   /* Decode function entry mask.  */
160   if (info->symbols
161       && info->symbols[0]
162       && (info->symbols[0]->flags & BSF_FUNCTION)
163       && memaddr == bfd_asymbol_value (info->symbols[0]))
164     {
165       int i = 0;
166       int register_mask = buffer[1] << 8 | buffer[0];
167
168       (*info->fprintf_func) (info->stream, ".word 0x%04x # Entry mask: <",
169                              register_mask);
170
171       for (i = 15; i >= 0; i--)
172         if (register_mask & (1 << i))
173           (*info->fprintf_func) (info->stream, " %s", entry_mask_bit[i]);
174
175       (*info->fprintf_func) (info->stream, " >");
176
177       return 2;
178     }
179
180   for (votp = &votstrs[0]; votp->name[0]; votp++)
181     {
182       register vax_opcodeT opcode = votp->detail.code;
183
184       /* 2 byte codes match 2 buffer pos. */
185       if ((bfd_byte) opcode == buffer[0]
186           && (opcode >> 8 == 0 || opcode >> 8 == buffer[1]))
187         {
188           argp = votp->detail.args;
189           break;
190         }
191     }
192   if (argp == NULL)
193     {
194       /* Handle undefined instructions. */
195       (*info->fprintf_func) (info->stream, ".word 0x%x",
196                              (buffer[0] << 8) + buffer[1]);
197       return 2;
198     }
199
200   /* Point at first byte of argument data, and at descriptor for first
201      argument.  */
202   arg = buffer + ((votp->detail.code >> 8) ? 2 : 1);
203
204   /* Make sure we have it in mem */
205   FETCH_DATA (info, arg);
206
207   (*info->fprintf_func) (info->stream, "%s", votp->name);
208   if (*argp)
209     (*info->fprintf_func) (info->stream, " ");
210
211   while (*argp)
212     {
213       arg += print_insn_arg (argp, arg, memaddr + arg - buffer, info);
214       argp += 2;
215       if (*argp)
216         (*info->fprintf_func) (info->stream, ",");
217     }
218
219   return arg - buffer;
220 }
221
222 /* Returns number of bytes "eaten" by the operand, or return -1 if an
223    invalid operand was found, or -2 if an opcode tabel error was
224    found. */
225
226 static int
227 print_insn_arg (d, p0, addr, info)
228      const char *d;
229      unsigned char *p0;
230      bfd_vma addr;              /* PC for this arg to be relative to */
231      disassemble_info *info;
232 {
233   int arg_len;
234
235   /* check validity of addressing length */
236   switch (d[1])
237     {
238     case 'b' : arg_len = 1;     break;
239     case 'd' : arg_len = 8;     break;
240     case 'f' : arg_len = 4;     break;
241     case 'g' : arg_len = 8;     break;
242     case 'h' : arg_len = 16;    break;
243     case 'l' : arg_len = 4;     break;
244     case 'o' : arg_len = 16;    break;
245     case 'w' : arg_len = 2;     break;
246     case 'q' : arg_len = 8;     break;
247     default  : abort();
248     }
249
250   /* branches have no mode byte */
251   if (d[0] == 'b')
252     {
253       unsigned char *p = p0;
254
255       if (arg_len == 1)
256         (*info->print_address_func) (addr + 1 + NEXTBYTE (p), info);
257       else
258         (*info->print_address_func) (addr + 2 + NEXTWORD (p), info);
259
260       return p - p0;
261     }
262
263   return print_insn_mode (d, arg_len, p0, addr, info);
264 }
265
266 static int
267 print_insn_mode (d, size, p0, addr, info)
268      const char *d;
269      int size;
270      unsigned char *p0;
271      bfd_vma addr;              /* PC for this arg to be relative to */
272      disassemble_info *info;
273 {
274   unsigned char *p = p0;
275   unsigned char mode, reg;
276
277   /* fetch and interpret mode byte */
278   mode = (unsigned char) NEXTBYTE (p);
279   reg = mode & 0xF;
280   switch (mode & 0xF0)
281     {
282     case 0x00:
283     case 0x10:
284     case 0x20:
285     case 0x30: /* literal mode                  $number */
286       if (d[1] == 'd' || d[1] == 'f' || d[1] == 'g' || d[1] == 'h')
287         (*info->fprintf_func) (info->stream, "$0x%x [%c-float]", mode, d[1]);
288       else
289         (*info->fprintf_func) (info->stream, "$0x%x", mode);
290       break;
291     case 0x40: /* index:                        base-addr[Rn] */
292       p += print_insn_mode (d, size, p0 + 1, addr + 1, info);
293       (*info->fprintf_func) (info->stream, "[%s]", reg_names[reg]);
294       break;
295     case 0x50: /* register:                     Rn */
296       (*info->fprintf_func) (info->stream, "%s", reg_names[reg]);
297       break;
298     case 0x60: /* register deferred:            (Rn) */
299       (*info->fprintf_func) (info->stream, "(%s)", reg_names[reg]);
300       break;
301     case 0x70: /* autodecrement:                -(Rn) */
302       (*info->fprintf_func) (info->stream, "-(%s)", reg_names[reg]);
303       break;
304     case 0x80: /* autoincrement:                (Rn)+ */
305       if (reg == 0xF)
306         {       /* immediate? */
307           int i;
308
309           FETCH_DATA (info, p + size);
310           (*info->fprintf_func) (info->stream, "$0x");
311           if (d[1] == 'd' || d[1] == 'f' || d[1] == 'g' || d[1] == 'h')
312             {
313               int float_word;
314
315               float_word = p[0] | (p[1] << 8);
316               if ((d[1] == 'd' || d[1] == 'f')
317                   && (float_word & 0xff80) == 0x8000)
318                 {
319                   (*info->fprintf_func) (info->stream, "[invalid %c-float]",
320                                          d[1]);
321                 }
322               else
323                 {
324                   for (i = 0; i < size; i++)
325                     (*info->fprintf_func) (info->stream, "%02x",
326                                            p[size - i - 1]);
327                   (*info->fprintf_func) (info->stream, " [%c-float]", d[1]);
328                 }
329             }
330           else
331             {
332               for (i = 0; i < size; i++)
333                 (*info->fprintf_func) (info->stream, "%02x", p[size - i - 1]);
334             }
335           p += size;
336         }
337       else
338         (*info->fprintf_func) (info->stream, "(%s)+", reg_names[reg]);
339       break;
340     case 0x90: /* autoincrement deferred:       @(Rn)+ */
341       if (reg == 0xF)
342         (*info->fprintf_func) (info->stream, "*0x%x", NEXTLONG (p));
343       else
344         (*info->fprintf_func) (info->stream, "@(%s)+", reg_names[reg]);
345       break;
346     case 0xB0: /* displacement byte deferred:   *displ(Rn) */
347       (*info->fprintf_func) (info->stream, "*");
348     case 0xA0: /* displacement byte:            displ(Rn) */
349       if (reg == 0xF)
350         (*info->print_address_func) (addr + 2 + NEXTBYTE (p), info);
351       else
352         (*info->fprintf_func) (info->stream, "0x%x(%s)", NEXTBYTE (p),
353                                reg_names[reg]);
354       break;
355     case 0xD0: /* displacement word deferred:   *displ(Rn) */
356       (*info->fprintf_func) (info->stream, "*");
357     case 0xC0: /* displacement word:            displ(Rn) */
358       if (reg == 0xF)
359         (*info->print_address_func) (addr + 3 + NEXTWORD (p), info);
360       else
361         (*info->fprintf_func) (info->stream, "0x%x(%s)", NEXTWORD (p),
362                                reg_names[reg]);
363       break;
364     case 0xF0: /* displacement long deferred:   *displ(Rn) */
365       (*info->fprintf_func) (info->stream, "*");
366     case 0xE0: /* displacement long:            displ(Rn) */
367       if (reg == 0xF)
368         (*info->print_address_func) (addr + 5 + NEXTLONG (p), info);
369       else
370         (*info->fprintf_func) (info->stream, "0x%x(%s)", NEXTLONG (p),
371                                reg_names[reg]);
372       break;
373     }
374
375   return p - p0;
376 }