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