| 1 | /* Print DEC PDP-11 instructions. |
| 2 | Copyright 2001 Free Software Foundation, Inc. |
| 3 | |
| 4 | This file is free software; you can redistribute it and/or modify |
| 5 | it under the terms of the GNU General Public License as published by |
| 6 | the Free Software Foundation; either version 2 of the License, or |
| 7 | (at your option) any later version. |
| 8 | |
| 9 | This program is distributed in the hope that it will be useful, |
| 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | GNU General Public License for more details. |
| 13 | |
| 14 | You should have received a copy of the GNU General Public License |
| 15 | along with this program; if not, write to the Free Software |
| 16 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ |
| 17 | |
| 18 | #include "dis-asm.h" |
| 19 | #include "opcode/pdp11.h" |
| 20 | |
| 21 | #define AFTER_INSTRUCTION "\t" |
| 22 | #define OPERAND_SEPARATOR ", " |
| 23 | |
| 24 | #define JUMP 0x1000 /* flag that this operand is used in a jump */ |
| 25 | |
| 26 | #define FPRINTF (*info->fprintf_func) |
| 27 | #define F info->stream |
| 28 | |
| 29 | /* sign-extend a 16-bit number in an int */ |
| 30 | #define SIGN_BITS (8 * sizeof (int) - 16) |
| 31 | #define sign_extend(x) (((x) << SIGN_BITS) >> SIGN_BITS) |
| 32 | |
| 33 | static int read_word PARAMS ((bfd_vma memaddr, int *word, |
| 34 | disassemble_info *info)); |
| 35 | static void print_signed_octal PARAMS ((int n, disassemble_info *info)); |
| 36 | static void print_reg PARAMS ((int reg, disassemble_info *info)); |
| 37 | static void print_freg PARAMS ((int freg, disassemble_info *info)); |
| 38 | static int print_operand PARAMS ((bfd_vma *memaddr, int code, |
| 39 | disassemble_info *info)); |
| 40 | int print_insn_pdp11 PARAMS ((bfd_vma memaddr, disassemble_info *info)); |
| 41 | |
| 42 | static int |
| 43 | read_word (memaddr, word, info) |
| 44 | bfd_vma memaddr; |
| 45 | int *word; |
| 46 | disassemble_info *info; |
| 47 | { |
| 48 | int status; |
| 49 | bfd_byte x[2]; |
| 50 | |
| 51 | status = (*info->read_memory_func) (memaddr, x, 2, info); |
| 52 | if (status != 0) |
| 53 | return -1; |
| 54 | |
| 55 | *word = x[1] << 8 | x[0]; |
| 56 | return 0; |
| 57 | } |
| 58 | |
| 59 | static void |
| 60 | print_signed_octal (n, info) |
| 61 | int n; |
| 62 | disassemble_info *info; |
| 63 | { |
| 64 | if (n < 0) |
| 65 | FPRINTF (F, "-%o", -n); |
| 66 | else |
| 67 | FPRINTF (F, "%o", n); |
| 68 | } |
| 69 | |
| 70 | static void |
| 71 | print_reg (reg, info) |
| 72 | int reg; |
| 73 | disassemble_info *info; |
| 74 | { |
| 75 | /* mask off the addressing mode, if any */ |
| 76 | reg &= 7; |
| 77 | |
| 78 | switch (reg) |
| 79 | { |
| 80 | case 0: case 1: case 2: case 3: case 4: case 5: |
| 81 | FPRINTF (F, "r%d", reg); break; |
| 82 | case 6: FPRINTF (F, "sp"); break; |
| 83 | case 7: FPRINTF (F, "pc"); break; |
| 84 | default: /* error */ |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | static void |
| 89 | print_freg (freg, info) |
| 90 | int freg; |
| 91 | disassemble_info *info; |
| 92 | { |
| 93 | FPRINTF (F, "fr%d", freg); |
| 94 | } |
| 95 | |
| 96 | static int |
| 97 | print_operand (memaddr, code, info) |
| 98 | bfd_vma *memaddr; |
| 99 | int code; |
| 100 | disassemble_info *info; |
| 101 | { |
| 102 | int mode = (code >> 3) & 7; |
| 103 | int reg = code & 7; |
| 104 | int disp; |
| 105 | |
| 106 | switch (mode) |
| 107 | { |
| 108 | case 0: |
| 109 | print_reg (reg, info); |
| 110 | break; |
| 111 | case 1: |
| 112 | FPRINTF (F, "("); |
| 113 | print_reg (reg, info); |
| 114 | FPRINTF (F, ")"); |
| 115 | break; |
| 116 | case 2: |
| 117 | if (reg == 7) |
| 118 | { |
| 119 | int data; |
| 120 | if (read_word (*memaddr, &data, info) < 0) |
| 121 | return -1; |
| 122 | FPRINTF (F, "$"); |
| 123 | print_signed_octal (sign_extend (data), info); |
| 124 | *memaddr += 2; |
| 125 | } |
| 126 | else |
| 127 | { |
| 128 | FPRINTF (F, "("); |
| 129 | print_reg (reg, info); |
| 130 | FPRINTF (F, ")+"); |
| 131 | } |
| 132 | break; |
| 133 | case 3: |
| 134 | if (reg == 7) |
| 135 | { |
| 136 | int address; |
| 137 | if (read_word (*memaddr, &address, info) < 0) |
| 138 | return -1; |
| 139 | FPRINTF (F, "*$%o", address); |
| 140 | *memaddr += 2; |
| 141 | } |
| 142 | else |
| 143 | { |
| 144 | FPRINTF (F, "*("); |
| 145 | print_reg (reg, info); |
| 146 | FPRINTF (F, ")+"); |
| 147 | } |
| 148 | break; |
| 149 | case 4: |
| 150 | FPRINTF (F, "-("); |
| 151 | print_reg (reg, info); |
| 152 | FPRINTF (F, ")"); |
| 153 | break; |
| 154 | case 5: |
| 155 | FPRINTF (F, "*-("); |
| 156 | print_reg (reg, info); |
| 157 | FPRINTF (F, ")"); |
| 158 | break; |
| 159 | case 6: |
| 160 | case 7: |
| 161 | if (read_word (*memaddr, &disp, info) < 0) |
| 162 | return -1; |
| 163 | *memaddr += 2; |
| 164 | if (reg == 7) |
| 165 | { |
| 166 | bfd_vma address = *memaddr + sign_extend (disp); |
| 167 | if (!(code & JUMP)) |
| 168 | FPRINTF (F, "*$"); |
| 169 | (*info->print_address_func) (address, info); |
| 170 | } |
| 171 | else |
| 172 | { |
| 173 | if (mode == 7) |
| 174 | FPRINTF (F, "*"); |
| 175 | print_signed_octal (sign_extend (disp), info); |
| 176 | FPRINTF (F, "("); |
| 177 | print_reg (reg, info); |
| 178 | FPRINTF (F, ")"); |
| 179 | } |
| 180 | break; |
| 181 | } |
| 182 | |
| 183 | return 0; |
| 184 | } |
| 185 | |
| 186 | /* Print the PDP-11 instruction at address MEMADDR in debugged memory, |
| 187 | on INFO->STREAM. Returns length of the instruction, in bytes. */ |
| 188 | |
| 189 | int |
| 190 | print_insn_pdp11 (memaddr, info) |
| 191 | bfd_vma memaddr; |
| 192 | disassemble_info *info; |
| 193 | { |
| 194 | bfd_vma start_memaddr = memaddr; |
| 195 | int opcode; |
| 196 | int src, dst; |
| 197 | int i; |
| 198 | |
| 199 | info->bytes_per_line = 6; |
| 200 | info->bytes_per_chunk = 2; |
| 201 | info->display_endian = BFD_ENDIAN_LITTLE; |
| 202 | |
| 203 | if (read_word (memaddr, &opcode, info) != 0) |
| 204 | return -1; |
| 205 | memaddr += 2; |
| 206 | |
| 207 | src = (opcode >> 6) & 0x3f; |
| 208 | dst = opcode & 0x3f; |
| 209 | |
| 210 | for (i = 0; i < pdp11_num_opcodes; i++) |
| 211 | { |
| 212 | #define OP pdp11_opcodes[i] |
| 213 | if ((opcode & OP.mask) == OP.opcode) |
| 214 | switch (OP.type) |
| 215 | { |
| 216 | case PDP11_OPCODE_NO_OPS: |
| 217 | FPRINTF (F, OP.name); |
| 218 | break; |
| 219 | case PDP11_OPCODE_REG: |
| 220 | FPRINTF (F, OP.name); |
| 221 | FPRINTF (F, AFTER_INSTRUCTION); |
| 222 | print_reg (dst, info); |
| 223 | break; |
| 224 | case PDP11_OPCODE_OP: |
| 225 | FPRINTF (F, OP.name); |
| 226 | FPRINTF (F, AFTER_INSTRUCTION); |
| 227 | if (strcmp (OP.name, "jmp") == 0) |
| 228 | dst |= JUMP; |
| 229 | if (print_operand (&memaddr, dst, info) < 0) |
| 230 | return -1; |
| 231 | break; |
| 232 | case PDP11_OPCODE_REG_OP: |
| 233 | FPRINTF (F, OP.name); |
| 234 | FPRINTF (F, AFTER_INSTRUCTION); |
| 235 | print_reg (src, info); |
| 236 | FPRINTF (F, OPERAND_SEPARATOR); |
| 237 | if (strcmp (OP.name, "jsr") == 0) |
| 238 | dst |= JUMP; |
| 239 | if (print_operand (&memaddr, dst, info) < 0) |
| 240 | return -1; |
| 241 | break; |
| 242 | case PDP11_OPCODE_REG_OP_REV: |
| 243 | FPRINTF (F, OP.name); |
| 244 | FPRINTF (F, AFTER_INSTRUCTION); |
| 245 | if (print_operand (&memaddr, dst, info) < 0) |
| 246 | return -1; |
| 247 | FPRINTF (F, OPERAND_SEPARATOR); |
| 248 | print_reg (src, info); |
| 249 | break; |
| 250 | case PDP11_OPCODE_AC_OP: |
| 251 | { |
| 252 | int ac = (opcode & 0xe0) >> 6; |
| 253 | FPRINTF (F, OP.name); |
| 254 | FPRINTF (F, AFTER_INSTRUCTION); |
| 255 | print_freg (ac, info); |
| 256 | FPRINTF (F, OPERAND_SEPARATOR); |
| 257 | if (print_operand (&memaddr, dst, info) < 0) |
| 258 | return -1; |
| 259 | break; |
| 260 | } |
| 261 | case PDP11_OPCODE_OP_OP: |
| 262 | FPRINTF (F, OP.name); |
| 263 | FPRINTF (F, AFTER_INSTRUCTION); |
| 264 | if (print_operand (&memaddr, src, info) < 0) |
| 265 | return -1; |
| 266 | FPRINTF (F, OPERAND_SEPARATOR); |
| 267 | if (print_operand (&memaddr, dst, info) < 0) |
| 268 | return -1; |
| 269 | break; |
| 270 | case PDP11_OPCODE_DISPL: |
| 271 | { |
| 272 | int displ = (opcode & 0xff) << 8; |
| 273 | bfd_vma address = memaddr + (sign_extend (displ) >> 7); |
| 274 | FPRINTF (F, OP.name); |
| 275 | FPRINTF (F, AFTER_INSTRUCTION); |
| 276 | (*info->print_address_func) (address, info); |
| 277 | break; |
| 278 | } |
| 279 | case PDP11_OPCODE_REG_DISPL: |
| 280 | { |
| 281 | int displ = (opcode & 0x3f) << 10; |
| 282 | bfd_vma address = memaddr + (sign_extend (displ) >> 9); |
| 283 | FPRINTF (F, OP.name); |
| 284 | FPRINTF (F, AFTER_INSTRUCTION); |
| 285 | print_reg (src, info); |
| 286 | FPRINTF (F, OPERAND_SEPARATOR); |
| 287 | (*info->print_address_func) (address, info); |
| 288 | break; |
| 289 | } |
| 290 | case PDP11_OPCODE_IMM8: |
| 291 | { |
| 292 | int code = opcode & 0xff; |
| 293 | FPRINTF (F, OP.name); |
| 294 | FPRINTF (F, AFTER_INSTRUCTION); |
| 295 | FPRINTF (F, "%o", code); |
| 296 | break; |
| 297 | } |
| 298 | case PDP11_OPCODE_IMM6: |
| 299 | { |
| 300 | int code = opcode & 0x3f; |
| 301 | FPRINTF (F, OP.name); |
| 302 | FPRINTF (F, AFTER_INSTRUCTION); |
| 303 | FPRINTF (F, "%o", code); |
| 304 | break; |
| 305 | } |
| 306 | case PDP11_OPCODE_IMM3: |
| 307 | { |
| 308 | int code = opcode & 7; |
| 309 | FPRINTF (F, OP.name); |
| 310 | FPRINTF (F, AFTER_INSTRUCTION); |
| 311 | FPRINTF (F, "%o", code); |
| 312 | break; |
| 313 | } |
| 314 | default: |
| 315 | /* TODO: is this a proper way of signalling an error? */ |
| 316 | FPRINTF (F, "<internal error: unrecognized instruction type>"); |
| 317 | return -1; |
| 318 | } |
| 319 | #undef OP |
| 320 | } |
| 321 | |
| 322 | return memaddr - start_memaddr; |
| 323 | } |