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