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