Coverage Report

Created: 2023-06-29 07:13

/src/binutils-gdb/opcodes/moxie-dis.c
Line
Count
Source (jump to first uncovered line)
1
/* Disassemble moxie instructions.
2
   Copyright (C) 2009-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 <stdio.h>
23
24
#define STATIC_TABLE
25
#define DEFINE_TABLE
26
27
#include "opcode/moxie.h"
28
#include "disassemble.h"
29
30
static fprintf_ftype fpr;
31
static void *stream;
32
33
/* Macros to extract operands from the instruction word.  */
34
13.0k
#define OP_A(i) ((i >> 4) & 0xf)
35
9.18k
#define OP_B(i) (i & 0xf)
36
7.16k
#define INST2OFFSET(o) (((((o) & 0x3ff) ^ 0x200) - 0x200) * 2)
37
38
static const char * reg_names[16] =
39
  { "$fp", "$sp", "$r0", "$r1", "$r2", "$r3", "$r4", "$r5",
40
    "$r6", "$r7", "$r8", "$r9", "$r10", "$r11", "$r12", "$r13" };
41
42
int
43
print_insn_moxie (bfd_vma addr, struct disassemble_info * info)
44
67.1k
{
45
67.1k
  int length = 2;
46
67.1k
  int status;
47
67.1k
  stream = info->stream;
48
67.1k
  const moxie_opc_info_t * opcode;
49
67.1k
  bfd_byte buffer[4];
50
67.1k
  unsigned short iword;
51
67.1k
  fpr = info->fprintf_func;
52
53
67.1k
  if ((status = info->read_memory_func (addr, buffer, 2, info)))
54
28
    goto fail;
55
56
67.1k
  if (info->endian == BFD_ENDIAN_BIG)
57
5.35k
    iword = bfd_getb16 (buffer);
58
61.7k
  else
59
61.7k
    iword = bfd_getl16 (buffer);
60
61
  /* Form 1 instructions have the high bit set to 0.  */
62
67.1k
  if ((iword & (1<<15)) == 0)
63
39.7k
    {
64
      /* Extract the Form 1 opcode.  */
65
39.7k
      opcode = &moxie_form1_opc_info[iword >> 8];
66
39.7k
      switch (opcode->itype)
67
39.7k
  {
68
1.06k
  case MOXIE_F1_NARG:
69
1.06k
    fpr (stream, "%s", opcode->name);
70
1.06k
    break;
71
711
  case MOXIE_F1_A:
72
711
    fpr (stream, "%s\t%s", opcode->name,
73
711
         reg_names[OP_A(iword)]);
74
711
    break;
75
6.60k
  case MOXIE_F1_AB:
76
6.60k
    fpr (stream, "%s\t%s, %s", opcode->name,
77
6.60k
         reg_names[OP_A(iword)],
78
6.60k
         reg_names[OP_B(iword)]);
79
6.60k
    break;
80
1.84k
  case MOXIE_F1_A4:
81
1.84k
    {
82
1.84k
      unsigned imm;
83
1.84k
      if ((status = info->read_memory_func (addr + 2, buffer, 4, info)))
84
6
        goto fail;
85
1.83k
      if (info->endian == BFD_ENDIAN_BIG)
86
365
        imm = bfd_getb32 (buffer);
87
1.47k
      else
88
1.47k
        imm = bfd_getl32 (buffer);
89
1.83k
      fpr (stream, "%s\t%s, 0x%x", opcode->name,
90
1.83k
     reg_names[OP_A(iword)], imm);
91
1.83k
      length = 6;
92
1.83k
    }
93
0
    break;
94
453
  case MOXIE_F1_4:
95
453
    {
96
453
      unsigned imm;
97
453
      if ((status = info->read_memory_func (addr + 2, buffer, 4, info)))
98
4
        goto fail;
99
449
      if (info->endian == BFD_ENDIAN_BIG)
100
28
        imm = bfd_getb32 (buffer);
101
421
      else
102
421
        imm = bfd_getl32 (buffer);
103
449
      fpr (stream, "%s\t0x%x", opcode->name, imm);
104
449
      length = 6;
105
449
    }
106
0
    break;
107
407
  case MOXIE_F1_M:
108
407
    {
109
407
      unsigned imm;
110
407
      if ((status = info->read_memory_func (addr + 2, buffer, 4, info)))
111
4
        goto fail;
112
403
      if (info->endian == BFD_ENDIAN_BIG)
113
71
        imm = bfd_getb32 (buffer);
114
332
      else
115
332
        imm = bfd_getl32 (buffer);
116
403
      fpr (stream, "%s\t", opcode->name);
117
403
      info->print_address_func ((bfd_vma) imm, info);
118
403
      length = 6;
119
403
    }
120
0
    break;
121
664
  case MOXIE_F1_AiB:
122
664
    fpr (stream, "%s\t(%s), %s", opcode->name,
123
664
         reg_names[OP_A(iword)], reg_names[OP_B(iword)]);
124
664
    break;
125
418
  case MOXIE_F1_ABi:
126
418
    fpr (stream, "%s\t%s, (%s)", opcode->name,
127
418
         reg_names[OP_A(iword)], reg_names[OP_B(iword)]);
128
418
    break;
129
1.34k
  case MOXIE_F1_4A:
130
1.34k
    {
131
1.34k
      unsigned imm;
132
1.34k
      if ((status = info->read_memory_func (addr + 2, buffer, 4, info)))
133
6
        goto fail;
134
1.34k
      if (info->endian == BFD_ENDIAN_BIG)
135
47
        imm = bfd_getb32 (buffer);
136
1.29k
      else
137
1.29k
        imm = bfd_getl32 (buffer);
138
1.34k
      fpr (stream, "%s\t0x%x, %s",
139
1.34k
     opcode->name, imm, reg_names[OP_A(iword)]);
140
1.34k
      length = 6;
141
1.34k
    }
142
0
    break;
143
752
  case MOXIE_F1_AiB2:
144
752
    {
145
752
      unsigned imm;
146
752
      if ((status = info->read_memory_func (addr+2, buffer, 2, info)))
147
2
        goto fail;
148
750
      if (info->endian == BFD_ENDIAN_BIG)
149
40
        imm = bfd_getb16 (buffer);
150
710
      else
151
710
        imm = bfd_getl16 (buffer);
152
750
      fpr (stream, "%s\t0x%x(%s), %s", opcode->name,
153
750
     imm,
154
750
     reg_names[OP_A(iword)],
155
750
     reg_names[OP_B(iword)]);
156
750
      length = 4;
157
750
    }
158
0
    break;
159
745
  case MOXIE_F1_ABi2:
160
745
    {
161
745
      unsigned imm;
162
745
      if ((status = info->read_memory_func (addr+2, buffer, 2, info)))
163
1
        goto fail;
164
744
      if (info->endian == BFD_ENDIAN_BIG)
165
39
        imm = bfd_getb16 (buffer);
166
705
      else
167
705
        imm = bfd_getl16 (buffer);
168
744
      fpr (stream, "%s\t%s, 0x%x(%s)",
169
744
     opcode->name,
170
744
     reg_names[OP_A(iword)],
171
744
     imm,
172
744
     reg_names[OP_B(iword)]);
173
744
      length = 4;
174
744
    }
175
0
    break;
176
24.7k
        case MOXIE_BAD:
177
24.7k
    fpr (stream, "bad");
178
24.7k
    break;
179
0
  default:
180
0
    abort();
181
39.7k
  }
182
39.7k
    }
183
27.3k
  else if ((iword & (1<<14)) == 0)
184
7.74k
    {
185
      /* Extract the Form 2 opcode.  */
186
7.74k
      opcode = &moxie_form2_opc_info[(iword >> 12) & 3];
187
7.74k
      switch (opcode->itype)
188
7.74k
  {
189
7.74k
  case MOXIE_F2_A8V:
190
7.74k
    fpr (stream, "%s\t%s, 0x%x",
191
7.74k
         opcode->name,
192
7.74k
         reg_names[(iword >> 8) & 0xf],
193
7.74k
         iword & ((1 << 8) - 1));
194
7.74k
    break;
195
0
  case MOXIE_F2_NARG:
196
0
    fpr (stream, "%s", opcode->name);
197
0
    break;
198
0
        case MOXIE_BAD:
199
0
    fpr (stream, "bad");
200
0
    break;
201
0
  default:
202
0
    abort();
203
7.74k
  }
204
7.74k
    }
205
19.6k
  else
206
19.6k
    {
207
      /* Extract the Form 3 opcode.  */
208
19.6k
      opcode = &moxie_form3_opc_info[(iword >> 10) & 15];
209
19.6k
      switch (opcode->itype)
210
19.6k
  {
211
7.16k
  case MOXIE_F3_PCREL:
212
7.16k
    fpr (stream, "%s\t", opcode->name);
213
7.16k
    info->print_address_func (addr + INST2OFFSET (iword) + 2, info);
214
7.16k
    break;
215
12.4k
        case MOXIE_BAD:
216
12.4k
    fpr (stream, "bad");
217
12.4k
    break;
218
0
  default:
219
0
    abort();
220
19.6k
  }
221
19.6k
    }
222
223
67.1k
  return length;
224
225
51
 fail:
226
51
  info->memory_error_func (status, addr, info);
227
51
  return -1;
228
67.1k
}