2001-12-18 Michael Snyder <msnyder@redhat.com>
[binutils.git] / opcodes / pdp11-dis.c
blob850248e2fc09aed6e6a3c0b28176d4445dcab492
1 /* Print DEC PDP-11 instructions.
2 Copyright 2001 Free Software Foundation, Inc.
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.
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.
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. */
18 #include "sysdep.h"
19 #include "dis-asm.h"
20 #include "opcode/pdp11.h"
22 #define AFTER_INSTRUCTION "\t"
23 #define OPERAND_SEPARATOR ", "
25 #define JUMP 0x1000 /* flag that this operand is used in a jump */
27 #define FPRINTF (*info->fprintf_func)
28 #define F info->stream
30 /* sign-extend a 16-bit number in an int */
31 #define SIGN_BITS (8 * sizeof (int) - 16)
32 #define sign_extend(x) (((x) << SIGN_BITS) >> SIGN_BITS)
34 static int read_word PARAMS ((bfd_vma memaddr, int *word,
35 disassemble_info *info));
36 static void print_signed_octal PARAMS ((int n, disassemble_info *info));
37 static void print_reg PARAMS ((int reg, disassemble_info *info));
38 static void print_freg PARAMS ((int freg, disassemble_info *info));
39 static int print_operand PARAMS ((bfd_vma *memaddr, int code,
40 disassemble_info *info));
41 int print_insn_pdp11 PARAMS ((bfd_vma memaddr, disassemble_info *info));
43 static int
44 read_word (memaddr, word, info)
45 bfd_vma memaddr;
46 int *word;
47 disassemble_info *info;
49 int status;
50 bfd_byte x[2];
52 status = (*info->read_memory_func) (memaddr, x, 2, info);
53 if (status != 0)
54 return -1;
56 *word = x[1] << 8 | x[0];
57 return 0;
60 static void
61 print_signed_octal (n, info)
62 int n;
63 disassemble_info *info;
65 if (n < 0)
66 FPRINTF (F, "-%o", -n);
67 else
68 FPRINTF (F, "%o", n);
71 static void
72 print_reg (reg, info)
73 int reg;
74 disassemble_info *info;
76 /* mask off the addressing mode, if any */
77 reg &= 7;
79 switch (reg)
81 case 0: case 1: case 2: case 3: case 4: case 5:
82 FPRINTF (F, "r%d", reg); break;
83 case 6: FPRINTF (F, "sp"); break;
84 case 7: FPRINTF (F, "pc"); break;
85 default: /* error */
89 static void
90 print_freg (freg, info)
91 int freg;
92 disassemble_info *info;
94 FPRINTF (F, "fr%d", freg);
97 static int
98 print_operand (memaddr, code, info)
99 bfd_vma *memaddr;
100 int code;
101 disassemble_info *info;
103 int mode = (code >> 3) & 7;
104 int reg = code & 7;
105 int disp;
107 switch (mode)
109 case 0:
110 print_reg (reg, info);
111 break;
112 case 1:
113 FPRINTF (F, "(");
114 print_reg (reg, info);
115 FPRINTF (F, ")");
116 break;
117 case 2:
118 if (reg == 7)
120 int data;
121 if (read_word (*memaddr, &data, info) < 0)
122 return -1;
123 FPRINTF (F, "$");
124 print_signed_octal (sign_extend (data), info);
125 *memaddr += 2;
127 else
129 FPRINTF (F, "(");
130 print_reg (reg, info);
131 FPRINTF (F, ")+");
133 break;
134 case 3:
135 if (reg == 7)
137 int address;
138 if (read_word (*memaddr, &address, info) < 0)
139 return -1;
140 FPRINTF (F, "*$%o", address);
141 *memaddr += 2;
143 else
145 FPRINTF (F, "*(");
146 print_reg (reg, info);
147 FPRINTF (F, ")+");
149 break;
150 case 4:
151 FPRINTF (F, "-(");
152 print_reg (reg, info);
153 FPRINTF (F, ")");
154 break;
155 case 5:
156 FPRINTF (F, "*-(");
157 print_reg (reg, info);
158 FPRINTF (F, ")");
159 break;
160 case 6:
161 case 7:
162 if (read_word (*memaddr, &disp, info) < 0)
163 return -1;
164 *memaddr += 2;
165 if (reg == 7)
167 bfd_vma address = *memaddr + sign_extend (disp);
168 if (!(code & JUMP))
169 FPRINTF (F, "*$");
170 (*info->print_address_func) (address, info);
172 else
174 if (mode == 7)
175 FPRINTF (F, "*");
176 print_signed_octal (sign_extend (disp), info);
177 FPRINTF (F, "(");
178 print_reg (reg, info);
179 FPRINTF (F, ")");
181 break;
184 return 0;
187 /* Print the PDP-11 instruction at address MEMADDR in debugged memory,
188 on INFO->STREAM. Returns length of the instruction, in bytes. */
191 print_insn_pdp11 (memaddr, info)
192 bfd_vma memaddr;
193 disassemble_info *info;
195 bfd_vma start_memaddr = memaddr;
196 int opcode;
197 int src, dst;
198 int i;
200 info->bytes_per_line = 6;
201 info->bytes_per_chunk = 2;
202 info->display_endian = BFD_ENDIAN_LITTLE;
204 if (read_word (memaddr, &opcode, info) != 0)
205 return -1;
206 memaddr += 2;
208 src = (opcode >> 6) & 0x3f;
209 dst = opcode & 0x3f;
211 for (i = 0; i < pdp11_num_opcodes; i++)
213 #define OP pdp11_opcodes[i]
214 if ((opcode & OP.mask) == OP.opcode)
215 switch (OP.type)
217 case PDP11_OPCODE_NO_OPS:
218 FPRINTF (F, OP.name);
219 goto done;
220 case PDP11_OPCODE_REG:
221 FPRINTF (F, OP.name);
222 FPRINTF (F, AFTER_INSTRUCTION);
223 print_reg (dst, info);
224 goto done;
225 case PDP11_OPCODE_OP:
226 FPRINTF (F, OP.name);
227 FPRINTF (F, AFTER_INSTRUCTION);
228 if (strcmp (OP.name, "jmp") == 0)
229 dst |= JUMP;
230 if (print_operand (&memaddr, dst, info) < 0)
231 return -1;
232 goto done;
233 case PDP11_OPCODE_REG_OP:
234 FPRINTF (F, OP.name);
235 FPRINTF (F, AFTER_INSTRUCTION);
236 print_reg (src, info);
237 FPRINTF (F, OPERAND_SEPARATOR);
238 if (strcmp (OP.name, "jsr") == 0)
239 dst |= JUMP;
240 if (print_operand (&memaddr, dst, info) < 0)
241 return -1;
242 goto done;
243 case PDP11_OPCODE_REG_OP_REV:
244 FPRINTF (F, OP.name);
245 FPRINTF (F, AFTER_INSTRUCTION);
246 if (print_operand (&memaddr, dst, info) < 0)
247 return -1;
248 FPRINTF (F, OPERAND_SEPARATOR);
249 print_reg (src, info);
250 goto done;
251 case PDP11_OPCODE_AC_OP:
253 int ac = (opcode & 0xe0) >> 6;
254 FPRINTF (F, OP.name);
255 FPRINTF (F, AFTER_INSTRUCTION);
256 print_freg (ac, info);
257 FPRINTF (F, OPERAND_SEPARATOR);
258 if (print_operand (&memaddr, dst, info) < 0)
259 return -1;
260 goto done;
262 case PDP11_OPCODE_OP_OP:
263 FPRINTF (F, OP.name);
264 FPRINTF (F, AFTER_INSTRUCTION);
265 if (print_operand (&memaddr, src, info) < 0)
266 return -1;
267 FPRINTF (F, OPERAND_SEPARATOR);
268 if (print_operand (&memaddr, dst, info) < 0)
269 return -1;
270 goto done;
271 case PDP11_OPCODE_DISPL:
273 int displ = (opcode & 0xff) << 8;
274 bfd_vma address = memaddr + (sign_extend (displ) >> 7);
275 FPRINTF (F, OP.name);
276 FPRINTF (F, AFTER_INSTRUCTION);
277 (*info->print_address_func) (address, info);
278 goto done;
280 case PDP11_OPCODE_REG_DISPL:
282 int displ = (opcode & 0x3f) << 10;
283 bfd_vma address = memaddr + (sign_extend (displ) >> 9);
284 FPRINTF (F, OP.name);
285 FPRINTF (F, AFTER_INSTRUCTION);
286 print_reg (src, info);
287 FPRINTF (F, OPERAND_SEPARATOR);
288 (*info->print_address_func) (address, info);
289 goto done;
291 case PDP11_OPCODE_IMM8:
293 int code = opcode & 0xff;
294 FPRINTF (F, OP.name);
295 FPRINTF (F, AFTER_INSTRUCTION);
296 FPRINTF (F, "%o", code);
297 goto done;
299 case PDP11_OPCODE_IMM6:
301 int code = opcode & 0x3f;
302 FPRINTF (F, OP.name);
303 FPRINTF (F, AFTER_INSTRUCTION);
304 FPRINTF (F, "%o", code);
305 goto done;
307 case PDP11_OPCODE_IMM3:
309 int code = opcode & 7;
310 FPRINTF (F, OP.name);
311 FPRINTF (F, AFTER_INSTRUCTION);
312 FPRINTF (F, "%o", code);
313 goto done;
315 case PDP11_OPCODE_ILLEGAL:
317 FPRINTF (F, ".word");
318 FPRINTF (F, AFTER_INSTRUCTION);
319 FPRINTF (F, "%o", opcode);
320 goto done;
322 default:
323 /* TODO: is this a proper way of signalling an error? */
324 FPRINTF (F, "<internal error: unrecognized instruction type>");
325 return -1;
327 #undef OP
329 done:
331 return memaddr - start_memaddr;