2006-03-07 Paul Brook <paul@codesourcery.com>
[binutils.git] / opcodes / pdp11-dis.c
blobf436e876018b6a7a257890862c88687e9a5e1ca8
1 /* Print DEC PDP-11 instructions.
2 Copyright 2001, 2002, 2004, 2005 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., 51 Franklin Street - Fifth Floor, Boston,
17 MA 02110-1301, USA. */
19 #include "sysdep.h"
20 #include "dis-asm.h"
21 #include "opcode/pdp11.h"
23 #define AFTER_INSTRUCTION "\t"
24 #define OPERAND_SEPARATOR ", "
26 #define JUMP 0x1000 /* Flag that this operand is used in a jump. */
28 #define FPRINTF (*info->fprintf_func)
29 #define F info->stream
31 /* Sign-extend a 16-bit number in an int. */
32 #define SIGN_BITS (8 * sizeof (int) - 16)
33 #define sign_extend(x) (((x) << SIGN_BITS) >> SIGN_BITS)
35 static int
36 read_word (bfd_vma memaddr, int *word, disassemble_info *info)
38 int status;
39 bfd_byte x[2];
41 status = (*info->read_memory_func) (memaddr, x, 2, info);
42 if (status != 0)
43 return -1;
45 *word = x[1] << 8 | x[0];
46 return 0;
49 static void
50 print_signed_octal (int n, disassemble_info *info)
52 if (n < 0)
53 FPRINTF (F, "-%o", -n);
54 else
55 FPRINTF (F, "%o", n);
58 static void
59 print_reg (int reg, disassemble_info *info)
61 /* Mask off the addressing mode, if any. */
62 reg &= 7;
64 switch (reg)
66 case 0: case 1: case 2: case 3: case 4: case 5:
67 FPRINTF (F, "r%d", reg); break;
68 case 6: FPRINTF (F, "sp"); break;
69 case 7: FPRINTF (F, "pc"); break;
70 default: ; /* error */
74 static void
75 print_freg (int freg, disassemble_info *info)
77 FPRINTF (F, "fr%d", freg);
80 static int
81 print_operand (bfd_vma *memaddr, int code, disassemble_info *info)
83 int mode = (code >> 3) & 7;
84 int reg = code & 7;
85 int disp;
87 switch (mode)
89 case 0:
90 print_reg (reg, info);
91 break;
92 case 1:
93 FPRINTF (F, "(");
94 print_reg (reg, info);
95 FPRINTF (F, ")");
96 break;
97 case 2:
98 if (reg == 7)
100 int data;
102 if (read_word (*memaddr, &data, info) < 0)
103 return -1;
104 FPRINTF (F, "$");
105 print_signed_octal (sign_extend (data), info);
106 *memaddr += 2;
108 else
110 FPRINTF (F, "(");
111 print_reg (reg, info);
112 FPRINTF (F, ")+");
114 break;
115 case 3:
116 if (reg == 7)
118 int address;
120 if (read_word (*memaddr, &address, info) < 0)
121 return -1;
122 FPRINTF (F, "*$%o", address);
123 *memaddr += 2;
125 else
127 FPRINTF (F, "*(");
128 print_reg (reg, info);
129 FPRINTF (F, ")+");
131 break;
132 case 4:
133 FPRINTF (F, "-(");
134 print_reg (reg, info);
135 FPRINTF (F, ")");
136 break;
137 case 5:
138 FPRINTF (F, "*-(");
139 print_reg (reg, info);
140 FPRINTF (F, ")");
141 break;
142 case 6:
143 case 7:
144 if (read_word (*memaddr, &disp, info) < 0)
145 return -1;
146 *memaddr += 2;
147 if (reg == 7)
149 bfd_vma address = *memaddr + sign_extend (disp);
151 if (mode == 7)
152 FPRINTF (F, "*");
153 if (!(code & JUMP))
154 FPRINTF (F, "$");
155 (*info->print_address_func) (address, info);
157 else
159 if (mode == 7)
160 FPRINTF (F, "*");
161 print_signed_octal (sign_extend (disp), info);
162 FPRINTF (F, "(");
163 print_reg (reg, info);
164 FPRINTF (F, ")");
166 break;
169 return 0;
172 static int
173 print_foperand (bfd_vma *memaddr, int code, disassemble_info *info)
175 int mode = (code >> 3) & 7;
176 int reg = code & 7;
178 if (mode == 0)
179 print_freg (reg, info);
180 else
181 return print_operand (memaddr, code, info);
183 return 0;
186 /* Print the PDP-11 instruction at address MEMADDR in debugged memory,
187 on INFO->STREAM. Returns length of the instruction, in bytes. */
190 print_insn_pdp11 (bfd_vma memaddr, disassemble_info *info)
192 bfd_vma start_memaddr = memaddr;
193 int opcode;
194 int src, dst;
195 int i;
197 info->bytes_per_line = 6;
198 info->bytes_per_chunk = 2;
199 info->display_endian = BFD_ENDIAN_LITTLE;
201 if (read_word (memaddr, &opcode, info) != 0)
202 return -1;
203 memaddr += 2;
205 src = (opcode >> 6) & 0x3f;
206 dst = opcode & 0x3f;
208 for (i = 0; i < pdp11_num_opcodes; i++)
210 #define OP pdp11_opcodes[i]
211 if ((opcode & OP.mask) == OP.opcode)
212 switch (OP.type)
214 case PDP11_OPCODE_NO_OPS:
215 FPRINTF (F, OP.name);
216 goto done;
217 case PDP11_OPCODE_REG:
218 FPRINTF (F, OP.name);
219 FPRINTF (F, AFTER_INSTRUCTION);
220 print_reg (dst, info);
221 goto done;
222 case PDP11_OPCODE_OP:
223 FPRINTF (F, OP.name);
224 FPRINTF (F, AFTER_INSTRUCTION);
225 if (strcmp (OP.name, "jmp") == 0)
226 dst |= JUMP;
227 if (print_operand (&memaddr, dst, info) < 0)
228 return -1;
229 goto done;
230 case PDP11_OPCODE_FOP:
231 FPRINTF (F, OP.name);
232 FPRINTF (F, AFTER_INSTRUCTION);
233 if (strcmp (OP.name, "jmp") == 0)
234 dst |= JUMP;
235 if (print_foperand (&memaddr, dst, info) < 0)
236 return -1;
237 goto done;
238 case PDP11_OPCODE_REG_OP:
239 FPRINTF (F, OP.name);
240 FPRINTF (F, AFTER_INSTRUCTION);
241 print_reg (src, info);
242 FPRINTF (F, OPERAND_SEPARATOR);
243 if (strcmp (OP.name, "jsr") == 0)
244 dst |= JUMP;
245 if (print_operand (&memaddr, dst, info) < 0)
246 return -1;
247 goto done;
248 case PDP11_OPCODE_REG_OP_REV:
249 FPRINTF (F, OP.name);
250 FPRINTF (F, AFTER_INSTRUCTION);
251 if (print_operand (&memaddr, dst, info) < 0)
252 return -1;
253 FPRINTF (F, OPERAND_SEPARATOR);
254 print_reg (src, info);
255 goto done;
256 case PDP11_OPCODE_AC_FOP:
258 int ac = (opcode & 0xe0) >> 6;
259 FPRINTF (F, OP.name);
260 FPRINTF (F, AFTER_INSTRUCTION);
261 print_freg (ac, info);
262 FPRINTF (F, OPERAND_SEPARATOR);
263 if (print_foperand (&memaddr, dst, info) < 0)
264 return -1;
265 goto done;
267 case PDP11_OPCODE_FOP_AC:
269 int ac = (opcode & 0xe0) >> 6;
270 FPRINTF (F, OP.name);
271 FPRINTF (F, AFTER_INSTRUCTION);
272 if (print_foperand (&memaddr, dst, info) < 0)
273 return -1;
274 FPRINTF (F, OPERAND_SEPARATOR);
275 print_freg (ac, info);
276 goto done;
278 case PDP11_OPCODE_AC_OP:
280 int ac = (opcode & 0xe0) >> 6;
281 FPRINTF (F, OP.name);
282 FPRINTF (F, AFTER_INSTRUCTION);
283 print_freg (ac, info);
284 FPRINTF (F, OPERAND_SEPARATOR);
285 if (print_operand (&memaddr, dst, info) < 0)
286 return -1;
287 goto done;
289 case PDP11_OPCODE_OP_AC:
291 int ac = (opcode & 0xe0) >> 6;
292 FPRINTF (F, OP.name);
293 FPRINTF (F, AFTER_INSTRUCTION);
294 if (print_operand (&memaddr, dst, info) < 0)
295 return -1;
296 FPRINTF (F, OPERAND_SEPARATOR);
297 print_freg (ac, info);
298 goto done;
300 case PDP11_OPCODE_OP_OP:
301 FPRINTF (F, OP.name);
302 FPRINTF (F, AFTER_INSTRUCTION);
303 if (print_operand (&memaddr, src, info) < 0)
304 return -1;
305 FPRINTF (F, OPERAND_SEPARATOR);
306 if (print_operand (&memaddr, dst, info) < 0)
307 return -1;
308 goto done;
309 case PDP11_OPCODE_DISPL:
311 int displ = (opcode & 0xff) << 8;
312 bfd_vma address = memaddr + (sign_extend (displ) >> 7);
313 FPRINTF (F, OP.name);
314 FPRINTF (F, AFTER_INSTRUCTION);
315 (*info->print_address_func) (address, info);
316 goto done;
318 case PDP11_OPCODE_REG_DISPL:
320 int displ = (opcode & 0x3f) << 10;
321 bfd_vma address = memaddr - (displ >> 9);
323 FPRINTF (F, OP.name);
324 FPRINTF (F, AFTER_INSTRUCTION);
325 print_reg (src, info);
326 FPRINTF (F, OPERAND_SEPARATOR);
327 (*info->print_address_func) (address, info);
328 goto done;
330 case PDP11_OPCODE_IMM8:
332 int code = opcode & 0xff;
333 FPRINTF (F, OP.name);
334 FPRINTF (F, AFTER_INSTRUCTION);
335 FPRINTF (F, "%o", code);
336 goto done;
338 case PDP11_OPCODE_IMM6:
340 int code = opcode & 0x3f;
341 FPRINTF (F, OP.name);
342 FPRINTF (F, AFTER_INSTRUCTION);
343 FPRINTF (F, "%o", code);
344 goto done;
346 case PDP11_OPCODE_IMM3:
348 int code = opcode & 7;
349 FPRINTF (F, OP.name);
350 FPRINTF (F, AFTER_INSTRUCTION);
351 FPRINTF (F, "%o", code);
352 goto done;
354 case PDP11_OPCODE_ILLEGAL:
356 FPRINTF (F, ".word");
357 FPRINTF (F, AFTER_INSTRUCTION);
358 FPRINTF (F, "%o", opcode);
359 goto done;
361 default:
362 /* TODO: is this a proper way of signalling an error? */
363 FPRINTF (F, "<internal error: unrecognized instruction type>");
364 return -1;
366 #undef OP
368 done:
370 return memaddr - start_memaddr;