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