/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-2024 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 | 151k | #define AFTER_INSTRUCTION "\t" |
26 | 109k | #define OPERAND_SEPARATOR ", " |
27 | | |
28 | 19.6k | #define JUMP 0x1000 /* Flag that this operand is used in a jump. */ |
29 | | |
30 | 1.04M | #define FPRINTF (*info->fprintf_func) |
31 | 1.04M | #define F info->stream |
32 | | |
33 | | /* Sign-extend a 16-bit number in an int. */ |
34 | 68.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 | 246k | { |
39 | 246k | int status; |
40 | 246k | bfd_byte x[2]; |
41 | | |
42 | 246k | status = (*info->read_memory_func) (memaddr, x, 2, info); |
43 | 246k | if (status != 0) |
44 | 214 | return -1; |
45 | | |
46 | 245k | *word = x[1] << 8 | x[0]; |
47 | 245k | return 0; |
48 | 246k | } |
49 | | |
50 | | static void |
51 | | print_signed_octal (int n, disassemble_info *info) |
52 | 36.9k | { |
53 | 36.9k | if (n < 0) |
54 | 13.0k | FPRINTF (F, "-%o", -n); |
55 | 23.9k | else |
56 | 23.9k | FPRINTF (F, "%o", n); |
57 | 36.9k | } |
58 | | |
59 | | static void |
60 | | print_reg (int reg, disassemble_info *info) |
61 | 193k | { |
62 | | /* Mask off the addressing mode, if any. */ |
63 | 193k | reg &= 7; |
64 | | |
65 | 193k | switch (reg) |
66 | 193k | { |
67 | 163k | case 0: case 1: case 2: case 3: case 4: case 5: |
68 | 163k | FPRINTF (F, "r%d", reg); break; |
69 | 17.6k | case 6: FPRINTF (F, "sp"); break; |
70 | 11.9k | case 7: FPRINTF (F, "pc"); break; |
71 | 0 | default: ; /* error */ |
72 | 193k | } |
73 | 193k | } |
74 | | |
75 | | static void |
76 | | print_freg (int freg, disassemble_info *info) |
77 | 18.4k | { |
78 | 18.4k | FPRINTF (F, "fr%d", freg); |
79 | 18.4k | } |
80 | | |
81 | | static int |
82 | | print_operand (bfd_vma *memaddr, int code, disassemble_info *info) |
83 | 200k | { |
84 | 200k | int mode = (code >> 3) & 7; |
85 | 200k | int reg = code & 7; |
86 | 200k | int disp; |
87 | | |
88 | 200k | switch (mode) |
89 | 200k | { |
90 | 40.1k | case 0: |
91 | 40.1k | print_reg (reg, info); |
92 | 40.1k | break; |
93 | 24.6k | case 1: |
94 | 24.6k | FPRINTF (F, "("); |
95 | 24.6k | print_reg (reg, info); |
96 | 24.6k | FPRINTF (F, ")"); |
97 | 24.6k | break; |
98 | 22.5k | case 2: |
99 | 22.5k | if (reg == 7) |
100 | 3.32k | { |
101 | 3.32k | int data; |
102 | | |
103 | 3.32k | if (read_word (*memaddr, &data, info) < 0) |
104 | 6 | return -1; |
105 | 3.31k | FPRINTF (F, "$"); |
106 | 3.31k | print_signed_octal (sign_extend (data), info); |
107 | 3.31k | *memaddr += 2; |
108 | 3.31k | } |
109 | 19.2k | else |
110 | 19.2k | { |
111 | 19.2k | FPRINTF (F, "("); |
112 | 19.2k | print_reg (reg, info); |
113 | 19.2k | FPRINTF (F, ")+"); |
114 | 19.2k | } |
115 | 22.5k | break; |
116 | 22.5k | case 3: |
117 | 20.2k | if (reg == 7) |
118 | 1.74k | { |
119 | 1.74k | int address; |
120 | | |
121 | 1.74k | if (read_word (*memaddr, &address, info) < 0) |
122 | 5 | return -1; |
123 | 1.74k | FPRINTF (F, "*$%o", address); |
124 | 1.74k | *memaddr += 2; |
125 | 1.74k | } |
126 | 18.4k | else |
127 | 18.4k | { |
128 | 18.4k | FPRINTF (F, "*("); |
129 | 18.4k | print_reg (reg, info); |
130 | 18.4k | FPRINTF (F, ")+"); |
131 | 18.4k | } |
132 | 20.2k | break; |
133 | 26.4k | case 4: |
134 | 26.4k | FPRINTF (F, "-("); |
135 | 26.4k | print_reg (reg, info); |
136 | 26.4k | FPRINTF (F, ")"); |
137 | 26.4k | break; |
138 | 19.1k | case 5: |
139 | 19.1k | FPRINTF (F, "*-("); |
140 | 19.1k | print_reg (reg, info); |
141 | 19.1k | FPRINTF (F, ")"); |
142 | 19.1k | break; |
143 | 19.5k | case 6: |
144 | 46.9k | case 7: |
145 | 46.9k | if (read_word (*memaddr, &disp, info) < 0) |
146 | 81 | return -1; |
147 | 46.8k | *memaddr += 2; |
148 | 46.8k | if (reg == 7) |
149 | 13.1k | { |
150 | 13.1k | bfd_vma address = *memaddr + sign_extend (disp); |
151 | | |
152 | 13.1k | if (mode == 7) |
153 | 11.3k | FPRINTF (F, "*"); |
154 | 13.1k | if (!(code & JUMP)) |
155 | 12.8k | FPRINTF (F, "$"); |
156 | 13.1k | (*info->print_address_func) (address, info); |
157 | 13.1k | } |
158 | 33.6k | else |
159 | 33.6k | { |
160 | 33.6k | if (mode == 7) |
161 | 15.9k | FPRINTF (F, "*"); |
162 | 33.6k | print_signed_octal (sign_extend (disp), info); |
163 | 33.6k | FPRINTF (F, "("); |
164 | 33.6k | print_reg (reg, info); |
165 | 33.6k | FPRINTF (F, ")"); |
166 | 33.6k | } |
167 | 46.8k | break; |
168 | 200k | } |
169 | | |
170 | 199k | return 0; |
171 | 200k | } |
172 | | |
173 | | static int |
174 | | print_foperand (bfd_vma *memaddr, int code, disassemble_info *info) |
175 | 14.6k | { |
176 | 14.6k | int mode = (code >> 3) & 7; |
177 | 14.6k | int reg = code & 7; |
178 | | |
179 | 14.6k | if (mode == 0) |
180 | 2.22k | print_freg (reg, info); |
181 | 12.4k | else |
182 | 12.4k | return print_operand (memaddr, code, info); |
183 | | |
184 | 2.22k | return 0; |
185 | 14.6k | } |
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 | 194k | { |
193 | 194k | bfd_vma start_memaddr = memaddr; |
194 | 194k | int opcode; |
195 | 194k | int src, dst; |
196 | 194k | int i; |
197 | | |
198 | 194k | info->bytes_per_line = 6; |
199 | 194k | info->bytes_per_chunk = 2; |
200 | 194k | info->display_endian = BFD_ENDIAN_LITTLE; |
201 | | |
202 | 194k | if (read_word (memaddr, &opcode, info) != 0) |
203 | 122 | return -1; |
204 | 194k | memaddr += 2; |
205 | | |
206 | 194k | src = (opcode >> 6) & 0x3f; |
207 | 194k | dst = opcode & 0x3f; |
208 | | |
209 | 17.7M | for (i = 0; i < pdp11_num_opcodes; i++) |
210 | 17.7M | { |
211 | 35.9M | #define OP pdp11_opcodes[i] |
212 | 17.7M | if ((opcode & OP.mask) == OP.opcode) |
213 | 194k | switch (OP.type) |
214 | 194k | { |
215 | 42.9k | case PDP11_OPCODE_NO_OPS: |
216 | 42.9k | FPRINTF (F, "%s", OP.name); |
217 | 42.9k | goto done; |
218 | 661 | case PDP11_OPCODE_REG: |
219 | 661 | FPRINTF (F, "%s", OP.name); |
220 | 661 | FPRINTF (F, AFTER_INSTRUCTION); |
221 | 661 | print_reg (dst, info); |
222 | 661 | goto done; |
223 | 11.7k | case PDP11_OPCODE_OP: |
224 | 11.7k | FPRINTF (F, "%s", OP.name); |
225 | 11.7k | FPRINTF (F, AFTER_INSTRUCTION); |
226 | 11.7k | if (strcmp (OP.name, "jmp") == 0) |
227 | 3.42k | dst |= JUMP; |
228 | 11.7k | if (print_operand (&memaddr, dst, info) < 0) |
229 | 11 | return -1; |
230 | 11.7k | goto done; |
231 | 11.7k | case PDP11_OPCODE_FOP: |
232 | 415 | FPRINTF (F, "%s", OP.name); |
233 | 415 | FPRINTF (F, AFTER_INSTRUCTION); |
234 | 415 | if (strcmp (OP.name, "jmp") == 0) |
235 | 0 | dst |= JUMP; |
236 | 415 | if (print_foperand (&memaddr, dst, info) < 0) |
237 | 2 | return -1; |
238 | 413 | goto done; |
239 | 3.91k | case PDP11_OPCODE_REG_OP: |
240 | 3.91k | FPRINTF (F, "%s", OP.name); |
241 | 3.91k | FPRINTF (F, AFTER_INSTRUCTION); |
242 | 3.91k | print_reg (src, info); |
243 | 3.91k | FPRINTF (F, OPERAND_SEPARATOR); |
244 | 3.91k | if (strcmp (OP.name, "jsr") == 0) |
245 | 3.09k | dst |= JUMP; |
246 | 3.91k | if (print_operand (&memaddr, dst, info) < 0) |
247 | 5 | return -1; |
248 | 3.91k | goto done; |
249 | 4.99k | case PDP11_OPCODE_REG_OP_REV: |
250 | 4.99k | FPRINTF (F, "%s", OP.name); |
251 | 4.99k | FPRINTF (F, AFTER_INSTRUCTION); |
252 | 4.99k | if (print_operand (&memaddr, dst, info) < 0) |
253 | 4 | return -1; |
254 | 4.98k | FPRINTF (F, OPERAND_SEPARATOR); |
255 | 4.98k | print_reg (src, info); |
256 | 4.98k | goto done; |
257 | 1.53k | case PDP11_OPCODE_AC_FOP: |
258 | 1.53k | { |
259 | 1.53k | int ac = (opcode & 0xe0) >> 6; |
260 | 1.53k | FPRINTF (F, "%s", OP.name); |
261 | 1.53k | FPRINTF (F, AFTER_INSTRUCTION); |
262 | 1.53k | print_freg (ac, info); |
263 | 1.53k | FPRINTF (F, OPERAND_SEPARATOR); |
264 | 1.53k | if (print_foperand (&memaddr, dst, info) < 0) |
265 | 2 | return -1; |
266 | 1.53k | goto done; |
267 | 1.53k | } |
268 | 12.7k | case PDP11_OPCODE_FOP_AC: |
269 | 12.7k | { |
270 | 12.7k | int ac = (opcode & 0xe0) >> 6; |
271 | 12.7k | FPRINTF (F, "%s", OP.name); |
272 | 12.7k | FPRINTF (F, AFTER_INSTRUCTION); |
273 | 12.7k | if (print_foperand (&memaddr, dst, info) < 0) |
274 | 15 | return -1; |
275 | 12.7k | FPRINTF (F, OPERAND_SEPARATOR); |
276 | 12.7k | print_freg (ac, info); |
277 | 12.7k | goto done; |
278 | 12.7k | } |
279 | 592 | case PDP11_OPCODE_AC_OP: |
280 | 592 | { |
281 | 592 | int ac = (opcode & 0xe0) >> 6; |
282 | 592 | FPRINTF (F, "%s", OP.name); |
283 | 592 | FPRINTF (F, AFTER_INSTRUCTION); |
284 | 592 | print_freg (ac, info); |
285 | 592 | FPRINTF (F, OPERAND_SEPARATOR); |
286 | 592 | if (print_operand (&memaddr, dst, info) < 0) |
287 | 1 | return -1; |
288 | 591 | goto done; |
289 | 592 | } |
290 | 1.33k | case PDP11_OPCODE_OP_AC: |
291 | 1.33k | { |
292 | 1.33k | int ac = (opcode & 0xe0) >> 6; |
293 | 1.33k | FPRINTF (F, "%s", OP.name); |
294 | 1.33k | FPRINTF (F, AFTER_INSTRUCTION); |
295 | 1.33k | if (print_operand (&memaddr, dst, info) < 0) |
296 | 1 | return -1; |
297 | 1.33k | FPRINTF (F, OPERAND_SEPARATOR); |
298 | 1.33k | print_freg (ac, info); |
299 | 1.33k | goto done; |
300 | 1.33k | } |
301 | 82.5k | case PDP11_OPCODE_OP_OP: |
302 | 82.5k | FPRINTF (F, "%s", OP.name); |
303 | 82.5k | FPRINTF (F, AFTER_INSTRUCTION); |
304 | 82.5k | if (print_operand (&memaddr, src, info) < 0) |
305 | 26 | return -1; |
306 | 82.5k | FPRINTF (F, OPERAND_SEPARATOR); |
307 | 82.5k | if (print_operand (&memaddr, dst, info) < 0) |
308 | 25 | return -1; |
309 | 82.4k | goto done; |
310 | 82.4k | case PDP11_OPCODE_DISPL: |
311 | 17.8k | { |
312 | 17.8k | int displ = (opcode & 0xff) << 8; |
313 | 17.8k | bfd_vma address = memaddr + (sign_extend (displ) >> 7); |
314 | 17.8k | FPRINTF (F, "%s", OP.name); |
315 | 17.8k | FPRINTF (F, AFTER_INSTRUCTION); |
316 | 17.8k | (*info->print_address_func) (address, info); |
317 | 17.8k | goto done; |
318 | 82.5k | } |
319 | 1.98k | case PDP11_OPCODE_REG_DISPL: |
320 | 1.98k | { |
321 | 1.98k | int displ = (opcode & 0x3f) << 10; |
322 | 1.98k | bfd_vma address = memaddr - (displ >> 9); |
323 | | |
324 | 1.98k | FPRINTF (F, "%s", OP.name); |
325 | 1.98k | FPRINTF (F, AFTER_INSTRUCTION); |
326 | 1.98k | print_reg (src, info); |
327 | 1.98k | FPRINTF (F, OPERAND_SEPARATOR); |
328 | 1.98k | (*info->print_address_func) (address, info); |
329 | 1.98k | goto done; |
330 | 82.5k | } |
331 | 750 | case PDP11_OPCODE_IMM8: |
332 | 750 | { |
333 | 750 | int code = opcode & 0xff; |
334 | 750 | FPRINTF (F, "%s", OP.name); |
335 | 750 | FPRINTF (F, AFTER_INSTRUCTION); |
336 | 750 | FPRINTF (F, "%o", code); |
337 | 750 | goto done; |
338 | 82.5k | } |
339 | 662 | case PDP11_OPCODE_IMM6: |
340 | 662 | { |
341 | 662 | int code = opcode & 0x3f; |
342 | 662 | FPRINTF (F, "%s", OP.name); |
343 | 662 | FPRINTF (F, AFTER_INSTRUCTION); |
344 | 662 | FPRINTF (F, "%o", code); |
345 | 662 | goto done; |
346 | 82.5k | } |
347 | 224 | case PDP11_OPCODE_IMM3: |
348 | 224 | { |
349 | 224 | int code = opcode & 7; |
350 | 224 | FPRINTF (F, "%s", OP.name); |
351 | 224 | FPRINTF (F, AFTER_INSTRUCTION); |
352 | 224 | FPRINTF (F, "%o", code); |
353 | 224 | goto done; |
354 | 82.5k | } |
355 | 9.15k | case PDP11_OPCODE_ILLEGAL: |
356 | 9.15k | { |
357 | 9.15k | FPRINTF (F, ".word"); |
358 | 9.15k | FPRINTF (F, AFTER_INSTRUCTION); |
359 | 9.15k | FPRINTF (F, "%o", opcode); |
360 | 9.15k | goto done; |
361 | 82.5k | } |
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 | 194k | } |
367 | 17.7M | #undef OP |
368 | 17.7M | } |
369 | 194k | done: |
370 | | |
371 | 194k | return memaddr - start_memaddr; |
372 | 194k | } |