/src/binutils-gdb/opcodes/pdp11-dis.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* Print DEC PDP-11 instructions. |
2 | | Copyright (C) 2001-2023 Free Software Foundation, Inc. |
3 | | |
4 | | This file is part of the GNU opcodes library. |
5 | | |
6 | | This library 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 3, or (at your option) |
9 | | any later version. |
10 | | |
11 | | It is distributed in the hope that it will be useful, but WITHOUT |
12 | | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
13 | | or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public |
14 | | 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., 51 Franklin Street - Fifth Floor, Boston, |
19 | | MA 02110-1301, USA. */ |
20 | | |
21 | | #include "sysdep.h" |
22 | | #include "disassemble.h" |
23 | | #include "opcode/pdp11.h" |
24 | | |
25 | 86.8k | #define AFTER_INSTRUCTION "\t" |
26 | 60.6k | #define OPERAND_SEPARATOR ", " |
27 | | |
28 | 8.80k | #define JUMP 0x1000 /* Flag that this operand is used in a jump. */ |
29 | | |
30 | 599k | #define FPRINTF (*info->fprintf_func) |
31 | 599k | #define F info->stream |
32 | | |
33 | | /* Sign-extend a 16-bit number in an int. */ |
34 | 39.0k | #define sign_extend(x) ((((x) & 0xffff) ^ 0x8000) - 0x8000) |
35 | | |
36 | | static int |
37 | | read_word (bfd_vma memaddr, int *word, disassemble_info *info) |
38 | 146k | { |
39 | 146k | int status; |
40 | 146k | bfd_byte x[2]; |
41 | | |
42 | 146k | status = (*info->read_memory_func) (memaddr, x, 2, info); |
43 | 146k | if (status != 0) |
44 | 247 | return -1; |
45 | | |
46 | 145k | *word = x[1] << 8 | x[0]; |
47 | 145k | return 0; |
48 | 146k | } |
49 | | |
50 | | static void |
51 | | print_signed_octal (int n, disassemble_info *info) |
52 | 23.2k | { |
53 | 23.2k | if (n < 0) |
54 | 8.16k | FPRINTF (F, "-%o", -n); |
55 | 15.0k | else |
56 | 15.0k | FPRINTF (F, "%o", n); |
57 | 23.2k | } |
58 | | |
59 | | static void |
60 | | print_reg (int reg, disassemble_info *info) |
61 | 113k | { |
62 | | /* Mask off the addressing mode, if any. */ |
63 | 113k | reg &= 7; |
64 | | |
65 | 113k | switch (reg) |
66 | 113k | { |
67 | 97.3k | case 0: case 1: case 2: case 3: case 4: case 5: |
68 | 97.3k | FPRINTF (F, "r%d", reg); break; |
69 | 10.3k | case 6: FPRINTF (F, "sp"); break; |
70 | 5.82k | case 7: FPRINTF (F, "pc"); break; |
71 | 0 | default: ; /* error */ |
72 | 113k | } |
73 | 113k | } |
74 | | |
75 | | static void |
76 | | print_freg (int freg, disassemble_info *info) |
77 | 6.83k | { |
78 | 6.83k | FPRINTF (F, "fr%d", freg); |
79 | 6.83k | } |
80 | | |
81 | | static int |
82 | | print_operand (bfd_vma *memaddr, int code, disassemble_info *info) |
83 | 114k | { |
84 | 114k | int mode = (code >> 3) & 7; |
85 | 114k | int reg = code & 7; |
86 | 114k | int disp; |
87 | | |
88 | 114k | switch (mode) |
89 | 114k | { |
90 | 25.2k | case 0: |
91 | 25.2k | print_reg (reg, info); |
92 | 25.2k | break; |
93 | 12.4k | case 1: |
94 | 12.4k | FPRINTF (F, "("); |
95 | 12.4k | print_reg (reg, info); |
96 | 12.4k | FPRINTF (F, ")"); |
97 | 12.4k | break; |
98 | 11.3k | case 2: |
99 | 11.3k | if (reg == 7) |
100 | 1.02k | { |
101 | 1.02k | int data; |
102 | | |
103 | 1.02k | if (read_word (*memaddr, &data, info) < 0) |
104 | 8 | return -1; |
105 | 1.02k | FPRINTF (F, "$"); |
106 | 1.02k | print_signed_octal (sign_extend (data), info); |
107 | 1.02k | *memaddr += 2; |
108 | 1.02k | } |
109 | 10.3k | else |
110 | 10.3k | { |
111 | 10.3k | FPRINTF (F, "("); |
112 | 10.3k | print_reg (reg, info); |
113 | 10.3k | FPRINTF (F, ")+"); |
114 | 10.3k | } |
115 | 11.3k | break; |
116 | 11.3k | case 3: |
117 | 10.5k | if (reg == 7) |
118 | 1.98k | { |
119 | 1.98k | int address; |
120 | | |
121 | 1.98k | if (read_word (*memaddr, &address, info) < 0) |
122 | 10 | return -1; |
123 | 1.97k | FPRINTF (F, "*$%o", address); |
124 | 1.97k | *memaddr += 2; |
125 | 1.97k | } |
126 | 8.51k | else |
127 | 8.51k | { |
128 | 8.51k | FPRINTF (F, "*("); |
129 | 8.51k | print_reg (reg, info); |
130 | 8.51k | FPRINTF (F, ")+"); |
131 | 8.51k | } |
132 | 10.4k | break; |
133 | 15.8k | case 4: |
134 | 15.8k | FPRINTF (F, "-("); |
135 | 15.8k | print_reg (reg, info); |
136 | 15.8k | FPRINTF (F, ")"); |
137 | 15.8k | break; |
138 | 12.3k | case 5: |
139 | 12.3k | FPRINTF (F, "*-("); |
140 | 12.3k | print_reg (reg, info); |
141 | 12.3k | FPRINTF (F, ")"); |
142 | 12.3k | break; |
143 | 13.1k | case 6: |
144 | 27.1k | case 7: |
145 | 27.1k | if (read_word (*memaddr, &disp, info) < 0) |
146 | 107 | return -1; |
147 | 27.0k | *memaddr += 2; |
148 | 27.0k | if (reg == 7) |
149 | 4.80k | { |
150 | 4.80k | bfd_vma address = *memaddr + sign_extend (disp); |
151 | | |
152 | 4.80k | if (mode == 7) |
153 | 3.81k | FPRINTF (F, "*"); |
154 | 4.80k | if (!(code & JUMP)) |
155 | 4.57k | FPRINTF (F, "$"); |
156 | 4.80k | (*info->print_address_func) (address, info); |
157 | 4.80k | } |
158 | 22.2k | else |
159 | 22.2k | { |
160 | 22.2k | if (mode == 7) |
161 | 10.0k | FPRINTF (F, "*"); |
162 | 22.2k | print_signed_octal (sign_extend (disp), info); |
163 | 22.2k | FPRINTF (F, "("); |
164 | 22.2k | print_reg (reg, info); |
165 | 22.2k | FPRINTF (F, ")"); |
166 | 22.2k | } |
167 | 27.0k | break; |
168 | 114k | } |
169 | | |
170 | 114k | return 0; |
171 | 114k | } |
172 | | |
173 | | static int |
174 | | print_foperand (bfd_vma *memaddr, int code, disassemble_info *info) |
175 | 5.12k | { |
176 | 5.12k | int mode = (code >> 3) & 7; |
177 | 5.12k | int reg = code & 7; |
178 | | |
179 | 5.12k | if (mode == 0) |
180 | 1.29k | print_freg (reg, info); |
181 | 3.83k | else |
182 | 3.83k | return print_operand (memaddr, code, info); |
183 | | |
184 | 1.29k | return 0; |
185 | 5.12k | } |
186 | | |
187 | | /* Print the PDP-11 instruction at address MEMADDR in debugged memory, |
188 | | on INFO->STREAM. Returns length of the instruction, in bytes. */ |
189 | | |
190 | | int |
191 | | print_insn_pdp11 (bfd_vma memaddr, disassemble_info *info) |
192 | 115k | { |
193 | 115k | bfd_vma start_memaddr = memaddr; |
194 | 115k | int opcode; |
195 | 115k | int src, dst; |
196 | 115k | int i; |
197 | | |
198 | 115k | info->bytes_per_line = 6; |
199 | 115k | info->bytes_per_chunk = 2; |
200 | 115k | info->display_endian = BFD_ENDIAN_LITTLE; |
201 | | |
202 | 115k | if (read_word (memaddr, &opcode, info) != 0) |
203 | 122 | return -1; |
204 | 115k | memaddr += 2; |
205 | | |
206 | 115k | src = (opcode >> 6) & 0x3f; |
207 | 115k | dst = opcode & 0x3f; |
208 | | |
209 | 9.93M | for (i = 0; i < pdp11_num_opcodes; i++) |
210 | 9.93M | { |
211 | 20.0M | #define OP pdp11_opcodes[i] |
212 | 9.93M | if ((opcode & OP.mask) == OP.opcode) |
213 | 115k | switch (OP.type) |
214 | 115k | { |
215 | 28.9k | case PDP11_OPCODE_NO_OPS: |
216 | 28.9k | FPRINTF (F, "%s", OP.name); |
217 | 28.9k | goto done; |
218 | 338 | case PDP11_OPCODE_REG: |
219 | 338 | FPRINTF (F, "%s", OP.name); |
220 | 338 | FPRINTF (F, AFTER_INSTRUCTION); |
221 | 338 | print_reg (dst, info); |
222 | 338 | goto done; |
223 | 7.18k | case PDP11_OPCODE_OP: |
224 | 7.18k | FPRINTF (F, "%s", OP.name); |
225 | 7.18k | FPRINTF (F, AFTER_INSTRUCTION); |
226 | 7.18k | if (strcmp (OP.name, "jmp") == 0) |
227 | 2.94k | dst |= JUMP; |
228 | 7.18k | if (print_operand (&memaddr, dst, info) < 0) |
229 | 6 | return -1; |
230 | 7.18k | goto done; |
231 | 7.18k | case PDP11_OPCODE_FOP: |
232 | 271 | FPRINTF (F, "%s", OP.name); |
233 | 271 | FPRINTF (F, AFTER_INSTRUCTION); |
234 | 271 | if (strcmp (OP.name, "jmp") == 0) |
235 | 0 | dst |= JUMP; |
236 | 271 | if (print_foperand (&memaddr, dst, info) < 0) |
237 | 2 | return -1; |
238 | 269 | goto done; |
239 | 1.84k | case PDP11_OPCODE_REG_OP: |
240 | 1.84k | FPRINTF (F, "%s", OP.name); |
241 | 1.84k | FPRINTF (F, AFTER_INSTRUCTION); |
242 | 1.84k | print_reg (src, info); |
243 | 1.84k | FPRINTF (F, OPERAND_SEPARATOR); |
244 | 1.84k | if (strcmp (OP.name, "jsr") == 0) |
245 | 1.05k | dst |= JUMP; |
246 | 1.84k | if (print_operand (&memaddr, dst, info) < 0) |
247 | 5 | return -1; |
248 | 1.84k | goto done; |
249 | 3.45k | case PDP11_OPCODE_REG_OP_REV: |
250 | 3.45k | FPRINTF (F, "%s", OP.name); |
251 | 3.45k | FPRINTF (F, AFTER_INSTRUCTION); |
252 | 3.45k | if (print_operand (&memaddr, dst, info) < 0) |
253 | 4 | return -1; |
254 | 3.44k | FPRINTF (F, OPERAND_SEPARATOR); |
255 | 3.44k | print_reg (src, info); |
256 | 3.44k | goto done; |
257 | 529 | case PDP11_OPCODE_AC_FOP: |
258 | 529 | { |
259 | 529 | int ac = (opcode & 0xe0) >> 6; |
260 | 529 | FPRINTF (F, "%s", OP.name); |
261 | 529 | FPRINTF (F, AFTER_INSTRUCTION); |
262 | 529 | print_freg (ac, info); |
263 | 529 | FPRINTF (F, OPERAND_SEPARATOR); |
264 | 529 | if (print_foperand (&memaddr, dst, info) < 0) |
265 | 1 | return -1; |
266 | 528 | goto done; |
267 | 529 | } |
268 | 4.32k | case PDP11_OPCODE_FOP_AC: |
269 | 4.32k | { |
270 | 4.32k | int ac = (opcode & 0xe0) >> 6; |
271 | 4.32k | FPRINTF (F, "%s", OP.name); |
272 | 4.32k | FPRINTF (F, AFTER_INSTRUCTION); |
273 | 4.32k | if (print_foperand (&memaddr, dst, info) < 0) |
274 | 10 | return -1; |
275 | 4.31k | FPRINTF (F, OPERAND_SEPARATOR); |
276 | 4.31k | print_freg (ac, info); |
277 | 4.31k | goto done; |
278 | 4.32k | } |
279 | 314 | case PDP11_OPCODE_AC_OP: |
280 | 314 | { |
281 | 314 | int ac = (opcode & 0xe0) >> 6; |
282 | 314 | FPRINTF (F, "%s", OP.name); |
283 | 314 | FPRINTF (F, AFTER_INSTRUCTION); |
284 | 314 | print_freg (ac, info); |
285 | 314 | FPRINTF (F, OPERAND_SEPARATOR); |
286 | 314 | if (print_operand (&memaddr, dst, info) < 0) |
287 | 1 | return -1; |
288 | 313 | goto done; |
289 | 314 | } |
290 | 374 | case PDP11_OPCODE_OP_AC: |
291 | 374 | { |
292 | 374 | int ac = (opcode & 0xe0) >> 6; |
293 | 374 | FPRINTF (F, "%s", OP.name); |
294 | 374 | FPRINTF (F, AFTER_INSTRUCTION); |
295 | 374 | if (print_operand (&memaddr, dst, info) < 0) |
296 | 1 | return -1; |
297 | 373 | FPRINTF (F, OPERAND_SEPARATOR); |
298 | 373 | print_freg (ac, info); |
299 | 373 | goto done; |
300 | 374 | } |
301 | 48.9k | case PDP11_OPCODE_OP_OP: |
302 | 48.9k | FPRINTF (F, "%s", OP.name); |
303 | 48.9k | FPRINTF (F, AFTER_INSTRUCTION); |
304 | 48.9k | if (print_operand (&memaddr, src, info) < 0) |
305 | 46 | return -1; |
306 | 48.9k | FPRINTF (F, OPERAND_SEPARATOR); |
307 | 48.9k | if (print_operand (&memaddr, dst, info) < 0) |
308 | 49 | return -1; |
309 | 48.8k | goto done; |
310 | 48.8k | case PDP11_OPCODE_DISPL: |
311 | 11.0k | { |
312 | 11.0k | int displ = (opcode & 0xff) << 8; |
313 | 11.0k | bfd_vma address = memaddr + (sign_extend (displ) >> 7); |
314 | 11.0k | FPRINTF (F, "%s", OP.name); |
315 | 11.0k | FPRINTF (F, AFTER_INSTRUCTION); |
316 | 11.0k | (*info->print_address_func) (address, info); |
317 | 11.0k | goto done; |
318 | 48.9k | } |
319 | 897 | case PDP11_OPCODE_REG_DISPL: |
320 | 897 | { |
321 | 897 | int displ = (opcode & 0x3f) << 10; |
322 | 897 | bfd_vma address = memaddr - (displ >> 9); |
323 | | |
324 | 897 | FPRINTF (F, "%s", OP.name); |
325 | 897 | FPRINTF (F, AFTER_INSTRUCTION); |
326 | 897 | print_reg (src, info); |
327 | 897 | FPRINTF (F, OPERAND_SEPARATOR); |
328 | 897 | (*info->print_address_func) (address, info); |
329 | 897 | goto done; |
330 | 48.9k | } |
331 | 1.22k | case PDP11_OPCODE_IMM8: |
332 | 1.22k | { |
333 | 1.22k | int code = opcode & 0xff; |
334 | 1.22k | FPRINTF (F, "%s", OP.name); |
335 | 1.22k | FPRINTF (F, AFTER_INSTRUCTION); |
336 | 1.22k | FPRINTF (F, "%o", code); |
337 | 1.22k | goto done; |
338 | 48.9k | } |
339 | 210 | case PDP11_OPCODE_IMM6: |
340 | 210 | { |
341 | 210 | int code = opcode & 0x3f; |
342 | 210 | FPRINTF (F, "%s", OP.name); |
343 | 210 | FPRINTF (F, AFTER_INSTRUCTION); |
344 | 210 | FPRINTF (F, "%o", code); |
345 | 210 | goto done; |
346 | 48.9k | } |
347 | 169 | case PDP11_OPCODE_IMM3: |
348 | 169 | { |
349 | 169 | int code = opcode & 7; |
350 | 169 | FPRINTF (F, "%s", OP.name); |
351 | 169 | FPRINTF (F, AFTER_INSTRUCTION); |
352 | 169 | FPRINTF (F, "%o", code); |
353 | 169 | goto done; |
354 | 48.9k | } |
355 | 5.74k | case PDP11_OPCODE_ILLEGAL: |
356 | 5.74k | { |
357 | 5.74k | FPRINTF (F, ".word"); |
358 | 5.74k | FPRINTF (F, AFTER_INSTRUCTION); |
359 | 5.74k | FPRINTF (F, "%o", opcode); |
360 | 5.74k | goto done; |
361 | 48.9k | } |
362 | 0 | default: |
363 | | /* TODO: is this a proper way of signalling an error? */ |
364 | 0 | FPRINTF (F, "<internal error: unrecognized instruction type>"); |
365 | 0 | return -1; |
366 | 115k | } |
367 | 9.93M | #undef OP |
368 | 9.93M | } |
369 | 115k | done: |
370 | | |
371 | 115k | return memaddr - start_memaddr; |
372 | 115k | } |