/src/binutils-gdb/opcodes/pdp11-dis.c
Line | Count | Source |
1 | | /* Print DEC PDP-11 instructions. |
2 | | Copyright (C) 2001-2026 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 | 171k | #define AFTER_INSTRUCTION "\t" |
26 | 130k | #define OPERAND_SEPARATOR ", " |
27 | | |
28 | 18.4k | #define JUMP 0x1000 /* Flag that this operand is used in a jump. */ |
29 | | |
30 | 1.19M | #define FPRINTF (*info->fprintf_func) |
31 | 1.19M | #define F info->stream |
32 | | |
33 | | /* Sign-extend a 16-bit number in an int. */ |
34 | 74.1k | #define sign_extend(x) ((((x) & 0xffff) ^ 0x8000) - 0x8000) |
35 | | |
36 | | static int |
37 | | read_word (bfd_vma memaddr, int *word, disassemble_info *info) |
38 | 254k | { |
39 | 254k | int status; |
40 | 254k | bfd_byte x[2]; |
41 | | |
42 | 254k | status = (*info->read_memory_func) (memaddr, x, 2, info); |
43 | 254k | if (status != 0) |
44 | 201 | return -1; |
45 | | |
46 | 254k | *word = x[1] << 8 | x[0]; |
47 | 254k | return 0; |
48 | 254k | } |
49 | | |
50 | | static void |
51 | | print_signed_octal (int n, disassemble_info *info) |
52 | 44.8k | { |
53 | 44.8k | if (n < 0) |
54 | 16.1k | FPRINTF (F, "-%o", -n); |
55 | 28.7k | else |
56 | 28.7k | FPRINTF (F, "%o", n); |
57 | 44.8k | } |
58 | | |
59 | | static void |
60 | | print_reg (int reg, disassemble_info *info) |
61 | 236k | { |
62 | | /* Mask off the addressing mode, if any. */ |
63 | 236k | reg &= 7; |
64 | | |
65 | 236k | switch (reg) |
66 | 236k | { |
67 | 204k | case 0: case 1: case 2: case 3: case 4: case 5: |
68 | 204k | FPRINTF (F, "r%d", reg); break; |
69 | 20.7k | case 6: FPRINTF (F, "sp"); break; |
70 | 10.9k | case 7: FPRINTF (F, "pc"); break; |
71 | 0 | default: ; /* error */ |
72 | 236k | } |
73 | 236k | } |
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 | 240k | { |
84 | 240k | int mode = (code >> 3) & 7; |
85 | 240k | int reg = code & 7; |
86 | 240k | int disp; |
87 | | |
88 | 240k | switch (mode) |
89 | 240k | { |
90 | 53.5k | case 0: |
91 | 53.5k | print_reg (reg, info); |
92 | 53.5k | break; |
93 | 25.3k | case 1: |
94 | 25.3k | FPRINTF (F, "("); |
95 | 25.3k | print_reg (reg, info); |
96 | 25.3k | FPRINTF (F, ")"); |
97 | 25.3k | break; |
98 | 27.0k | case 2: |
99 | 27.0k | if (reg == 7) |
100 | 3.01k | { |
101 | 3.01k | int data; |
102 | | |
103 | 3.01k | if (read_word (*memaddr, &data, info) < 0) |
104 | 1 | return -1; |
105 | 3.01k | FPRINTF (F, "$"); |
106 | 3.01k | print_signed_octal (sign_extend (data), info); |
107 | 3.01k | *memaddr += 2; |
108 | 3.01k | } |
109 | 23.9k | else |
110 | 23.9k | { |
111 | 23.9k | FPRINTF (F, "("); |
112 | 23.9k | print_reg (reg, info); |
113 | 23.9k | FPRINTF (F, ")+"); |
114 | 23.9k | } |
115 | 27.0k | break; |
116 | 27.0k | case 3: |
117 | 21.9k | if (reg == 7) |
118 | 1.64k | { |
119 | 1.64k | int address; |
120 | | |
121 | 1.64k | if (read_word (*memaddr, &address, info) < 0) |
122 | 7 | return -1; |
123 | 1.63k | FPRINTF (F, "*$%o", address); |
124 | 1.63k | *memaddr += 2; |
125 | 1.63k | } |
126 | 20.3k | else |
127 | 20.3k | { |
128 | 20.3k | FPRINTF (F, "*("); |
129 | 20.3k | print_reg (reg, info); |
130 | 20.3k | FPRINTF (F, ")+"); |
131 | 20.3k | } |
132 | 21.9k | break; |
133 | 32.4k | case 4: |
134 | 32.4k | FPRINTF (F, "-("); |
135 | 32.4k | print_reg (reg, info); |
136 | 32.4k | FPRINTF (F, ")"); |
137 | 32.4k | break; |
138 | 25.6k | case 5: |
139 | 25.6k | FPRINTF (F, "*-("); |
140 | 25.6k | print_reg (reg, info); |
141 | 25.6k | FPRINTF (F, ")"); |
142 | 25.6k | break; |
143 | 25.3k | case 6: |
144 | 54.1k | case 7: |
145 | 54.1k | if (read_word (*memaddr, &disp, info) < 0) |
146 | 89 | return -1; |
147 | 54.0k | *memaddr += 2; |
148 | 54.0k | if (reg == 7) |
149 | 12.1k | { |
150 | 12.1k | bfd_vma address = *memaddr + sign_extend (disp); |
151 | | |
152 | 12.1k | if (mode == 7) |
153 | 9.47k | FPRINTF (F, "*"); |
154 | 12.1k | if (!(code & JUMP)) |
155 | 11.7k | FPRINTF (F, "$"); |
156 | 12.1k | (*info->print_address_func) (address, info); |
157 | 12.1k | } |
158 | 41.8k | else |
159 | 41.8k | { |
160 | 41.8k | if (mode == 7) |
161 | 19.2k | FPRINTF (F, "*"); |
162 | 41.8k | print_signed_octal (sign_extend (disp), info); |
163 | 41.8k | FPRINTF (F, "("); |
164 | 41.8k | print_reg (reg, info); |
165 | 41.8k | FPRINTF (F, ")"); |
166 | 41.8k | } |
167 | 54.0k | break; |
168 | 240k | } |
169 | | |
170 | 240k | return 0; |
171 | 240k | } |
172 | | |
173 | | static int |
174 | | print_foperand (bfd_vma *memaddr, int code, disassemble_info *info) |
175 | 13.9k | { |
176 | 13.9k | int mode = (code >> 3) & 7; |
177 | 13.9k | int reg = code & 7; |
178 | | |
179 | 13.9k | if (mode == 0) |
180 | 1.72k | print_freg (reg, info); |
181 | 12.2k | else |
182 | 12.2k | return print_operand (memaddr, code, info); |
183 | | |
184 | 1.72k | return 0; |
185 | 13.9k | } |
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 | 195k | { |
193 | 195k | bfd_vma start_memaddr = memaddr; |
194 | 195k | int opcode; |
195 | 195k | int src, dst; |
196 | 195k | int i; |
197 | | |
198 | 195k | info->bytes_per_line = 6; |
199 | 195k | info->bytes_per_chunk = 2; |
200 | 195k | info->display_endian = BFD_ENDIAN_LITTLE; |
201 | | |
202 | 195k | if (read_word (memaddr, &opcode, info) != 0) |
203 | 104 | return -1; |
204 | 195k | memaddr += 2; |
205 | | |
206 | 195k | src = (opcode >> 6) & 0x3f; |
207 | 195k | dst = opcode & 0x3f; |
208 | | |
209 | 19.9M | for (i = 0; i < pdp11_num_opcodes; i++) |
210 | 19.9M | { |
211 | 40.2M | #define OP pdp11_opcodes[i] |
212 | 19.9M | if ((opcode & OP.mask) == OP.opcode) |
213 | 195k | switch (OP.type) |
214 | 195k | { |
215 | 24.0k | case PDP11_OPCODE_NO_OPS: |
216 | 24.0k | FPRINTF (F, "%s", OP.name); |
217 | 24.0k | goto done; |
218 | 421 | case PDP11_OPCODE_REG: |
219 | 421 | FPRINTF (F, "%s", OP.name); |
220 | 421 | FPRINTF (F, AFTER_INSTRUCTION); |
221 | 421 | print_reg (dst, info); |
222 | 421 | goto done; |
223 | 12.5k | case PDP11_OPCODE_OP: |
224 | 12.5k | FPRINTF (F, "%s", OP.name); |
225 | 12.5k | FPRINTF (F, AFTER_INSTRUCTION); |
226 | 12.5k | if (strcmp (OP.name, "jmp") == 0) |
227 | 4.03k | dst |= JUMP; |
228 | 12.5k | if (print_operand (&memaddr, dst, info) < 0) |
229 | 6 | return -1; |
230 | 12.5k | goto done; |
231 | 12.5k | case PDP11_OPCODE_FOP: |
232 | 283 | FPRINTF (F, "%s", OP.name); |
233 | 283 | FPRINTF (F, AFTER_INSTRUCTION); |
234 | 283 | if (strcmp (OP.name, "jmp") == 0) |
235 | 0 | dst |= JUMP; |
236 | 283 | if (print_foperand (&memaddr, dst, info) < 0) |
237 | 1 | return -1; |
238 | 282 | goto done; |
239 | 4.40k | case PDP11_OPCODE_REG_OP: |
240 | 4.40k | FPRINTF (F, "%s", OP.name); |
241 | 4.40k | FPRINTF (F, AFTER_INSTRUCTION); |
242 | 4.40k | print_reg (src, info); |
243 | 4.40k | FPRINTF (F, OPERAND_SEPARATOR); |
244 | 4.40k | if (strcmp (OP.name, "jsr") == 0) |
245 | 2.21k | dst |= JUMP; |
246 | 4.40k | if (print_operand (&memaddr, dst, info) < 0) |
247 | 4 | return -1; |
248 | 4.40k | goto done; |
249 | 6.33k | case PDP11_OPCODE_REG_OP_REV: |
250 | 6.33k | FPRINTF (F, "%s", OP.name); |
251 | 6.33k | FPRINTF (F, AFTER_INSTRUCTION); |
252 | 6.33k | if (print_operand (&memaddr, dst, info) < 0) |
253 | 2 | return -1; |
254 | 6.33k | FPRINTF (F, OPERAND_SEPARATOR); |
255 | 6.33k | print_reg (src, info); |
256 | 6.33k | goto done; |
257 | 1.61k | case PDP11_OPCODE_AC_FOP: |
258 | 1.61k | { |
259 | 1.61k | int ac = (opcode & 0xe0) >> 6; |
260 | 1.61k | FPRINTF (F, "%s", OP.name); |
261 | 1.61k | FPRINTF (F, AFTER_INSTRUCTION); |
262 | 1.61k | print_freg (ac, info); |
263 | 1.61k | FPRINTF (F, OPERAND_SEPARATOR); |
264 | 1.61k | if (print_foperand (&memaddr, dst, info) < 0) |
265 | 7 | return -1; |
266 | 1.60k | goto done; |
267 | 1.61k | } |
268 | 12.0k | case PDP11_OPCODE_FOP_AC: |
269 | 12.0k | { |
270 | 12.0k | int ac = (opcode & 0xe0) >> 6; |
271 | 12.0k | FPRINTF (F, "%s", OP.name); |
272 | 12.0k | FPRINTF (F, AFTER_INSTRUCTION); |
273 | 12.0k | if (print_foperand (&memaddr, dst, info) < 0) |
274 | 20 | return -1; |
275 | 12.0k | FPRINTF (F, OPERAND_SEPARATOR); |
276 | 12.0k | print_freg (ac, info); |
277 | 12.0k | goto done; |
278 | 12.0k | } |
279 | 678 | case PDP11_OPCODE_AC_OP: |
280 | 678 | { |
281 | 678 | int ac = (opcode & 0xe0) >> 6; |
282 | 678 | FPRINTF (F, "%s", OP.name); |
283 | 678 | FPRINTF (F, AFTER_INSTRUCTION); |
284 | 678 | print_freg (ac, info); |
285 | 678 | FPRINTF (F, OPERAND_SEPARATOR); |
286 | 678 | if (print_operand (&memaddr, dst, info) < 0) |
287 | 2 | return -1; |
288 | 676 | goto done; |
289 | 678 | } |
290 | 2.40k | case PDP11_OPCODE_OP_AC: |
291 | 2.40k | { |
292 | 2.40k | int ac = (opcode & 0xe0) >> 6; |
293 | 2.40k | FPRINTF (F, "%s", OP.name); |
294 | 2.40k | FPRINTF (F, AFTER_INSTRUCTION); |
295 | 2.40k | if (print_operand (&memaddr, dst, info) < 0) |
296 | 2 | return -1; |
297 | 2.40k | FPRINTF (F, OPERAND_SEPARATOR); |
298 | 2.40k | print_freg (ac, info); |
299 | 2.40k | goto done; |
300 | 2.40k | } |
301 | 100k | case PDP11_OPCODE_OP_OP: |
302 | 100k | FPRINTF (F, "%s", OP.name); |
303 | 100k | FPRINTF (F, AFTER_INSTRUCTION); |
304 | 100k | if (print_operand (&memaddr, src, info) < 0) |
305 | 35 | return -1; |
306 | 100k | FPRINTF (F, OPERAND_SEPARATOR); |
307 | 100k | if (print_operand (&memaddr, dst, info) < 0) |
308 | 18 | return -1; |
309 | 100k | goto done; |
310 | 100k | case PDP11_OPCODE_DISPL: |
311 | 17.0k | { |
312 | 17.0k | int displ = (opcode & 0xff) << 8; |
313 | 17.0k | bfd_vma address = memaddr + (sign_extend (displ) >> 7); |
314 | 17.0k | FPRINTF (F, "%s", OP.name); |
315 | 17.0k | FPRINTF (F, AFTER_INSTRUCTION); |
316 | 17.0k | (*info->print_address_func) (address, info); |
317 | 17.0k | goto done; |
318 | 100k | } |
319 | 1.95k | case PDP11_OPCODE_REG_DISPL: |
320 | 1.95k | { |
321 | 1.95k | int displ = (opcode & 0x3f) << 10; |
322 | 1.95k | bfd_vma address = memaddr - (displ >> 9); |
323 | | |
324 | 1.95k | FPRINTF (F, "%s", OP.name); |
325 | 1.95k | FPRINTF (F, AFTER_INSTRUCTION); |
326 | 1.95k | print_reg (src, info); |
327 | 1.95k | FPRINTF (F, OPERAND_SEPARATOR); |
328 | 1.95k | (*info->print_address_func) (address, info); |
329 | 1.95k | goto done; |
330 | 100k | } |
331 | 1.02k | case PDP11_OPCODE_IMM8: |
332 | 1.02k | { |
333 | 1.02k | int code = opcode & 0xff; |
334 | 1.02k | FPRINTF (F, "%s", OP.name); |
335 | 1.02k | FPRINTF (F, AFTER_INSTRUCTION); |
336 | 1.02k | FPRINTF (F, "%o", code); |
337 | 1.02k | goto done; |
338 | 100k | } |
339 | 333 | case PDP11_OPCODE_IMM6: |
340 | 333 | { |
341 | 333 | int code = opcode & 0x3f; |
342 | 333 | FPRINTF (F, "%s", OP.name); |
343 | 333 | FPRINTF (F, AFTER_INSTRUCTION); |
344 | 333 | FPRINTF (F, "%o", code); |
345 | 333 | goto done; |
346 | 100k | } |
347 | 281 | case PDP11_OPCODE_IMM3: |
348 | 281 | { |
349 | 281 | int code = opcode & 7; |
350 | 281 | FPRINTF (F, "%s", OP.name); |
351 | 281 | FPRINTF (F, AFTER_INSTRUCTION); |
352 | 281 | FPRINTF (F, "%o", code); |
353 | 281 | goto done; |
354 | 100k | } |
355 | 9.37k | case PDP11_OPCODE_ILLEGAL: |
356 | 9.37k | { |
357 | 9.37k | FPRINTF (F, ".word"); |
358 | 9.37k | FPRINTF (F, AFTER_INSTRUCTION); |
359 | 9.37k | FPRINTF (F, "%o", opcode); |
360 | 9.37k | goto done; |
361 | 100k | } |
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 | 195k | } |
367 | 19.9M | #undef OP |
368 | 19.9M | } |
369 | 195k | done: |
370 | | |
371 | 195k | return memaddr - start_memaddr; |
372 | 195k | } |