Coverage Report

Created: 2023-08-28 06:23

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