Coverage Report

Created: 2025-06-24 06:45

/src/binutils-gdb/opcodes/moxie-dis.c
Line
Count
Source (jump to first uncovered line)
1
/* Disassemble moxie instructions.
2
   Copyright (C) 2009-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 <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
9.78k
#define OP_A(i) ((i >> 4) & 0xf)
35
7.30k
#define OP_B(i) (i & 0xf)
36
4.39k
#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
49.3k
{
45
49.3k
  int length = 2;
46
49.3k
  int status;
47
49.3k
  stream = info->stream;
48
49.3k
  const moxie_opc_info_t * opcode;
49
49.3k
  bfd_byte buffer[4];
50
49.3k
  unsigned short iword;
51
49.3k
  fpr = info->fprintf_func;
52
53
49.3k
  if ((status = info->read_memory_func (addr, buffer, 2, info)))
54
50
    goto fail;
55
56
49.3k
  if (info->endian == BFD_ENDIAN_BIG)
57
22.7k
    iword = bfd_getb16 (buffer);
58
26.6k
  else
59
26.6k
    iword = bfd_getl16 (buffer);
60
61
  /* Form 1 instructions have the high bit set to 0.  */
62
49.3k
  if ((iword & (1<<15)) == 0)
63
33.1k
    {
64
      /* Extract the Form 1 opcode.  */
65
33.1k
      opcode = &moxie_form1_opc_info[iword >> 8];
66
33.1k
      switch (opcode->itype)
67
33.1k
  {
68
904
  case MOXIE_F1_NARG:
69
904
    fpr (stream, "%s", opcode->name);
70
904
    break;
71
442
  case MOXIE_F1_A:
72
442
    fpr (stream, "%s\t%s", opcode->name,
73
442
         reg_names[OP_A(iword)]);
74
442
    break;
75
4.71k
  case MOXIE_F1_AB:
76
4.71k
    fpr (stream, "%s\t%s, %s", opcode->name,
77
4.71k
         reg_names[OP_A(iword)],
78
4.71k
         reg_names[OP_B(iword)]);
79
4.71k
    break;
80
1.65k
  case MOXIE_F1_A4:
81
1.65k
    {
82
1.65k
      unsigned imm;
83
1.65k
      if ((status = info->read_memory_func (addr + 2, buffer, 4, info)))
84
5
        goto fail;
85
1.65k
      if (info->endian == BFD_ENDIAN_BIG)
86
1.13k
        imm = bfd_getb32 (buffer);
87
521
      else
88
521
        imm = bfd_getl32 (buffer);
89
1.65k
      fpr (stream, "%s\t%s, 0x%x", opcode->name,
90
1.65k
     reg_names[OP_A(iword)], imm);
91
1.65k
      length = 6;
92
1.65k
    }
93
0
    break;
94
343
  case MOXIE_F1_4:
95
343
    {
96
343
      unsigned imm;
97
343
      if ((status = info->read_memory_func (addr + 2, buffer, 4, info)))
98
2
        goto fail;
99
341
      if (info->endian == BFD_ENDIAN_BIG)
100
46
        imm = bfd_getb32 (buffer);
101
295
      else
102
295
        imm = bfd_getl32 (buffer);
103
341
      fpr (stream, "%s\t0x%x", opcode->name, imm);
104
341
      length = 6;
105
341
    }
106
0
    break;
107
471
  case MOXIE_F1_M:
108
471
    {
109
471
      unsigned imm;
110
471
      if ((status = info->read_memory_func (addr + 2, buffer, 4, info)))
111
3
        goto fail;
112
468
      if (info->endian == BFD_ENDIAN_BIG)
113
175
        imm = bfd_getb32 (buffer);
114
293
      else
115
293
        imm = bfd_getl32 (buffer);
116
468
      fpr (stream, "%s\t", opcode->name);
117
468
      info->print_address_func ((bfd_vma) imm, info);
118
468
      length = 6;
119
468
    }
120
0
    break;
121
784
  case MOXIE_F1_AiB:
122
784
    fpr (stream, "%s\t(%s), %s", opcode->name,
123
784
         reg_names[OP_A(iword)], reg_names[OP_B(iword)]);
124
784
    break;
125
786
  case MOXIE_F1_ABi:
126
786
    fpr (stream, "%s\t%s, (%s)", opcode->name,
127
786
         reg_names[OP_A(iword)], reg_names[OP_B(iword)]);
128
786
    break;
129
390
  case MOXIE_F1_4A:
130
390
    {
131
390
      unsigned imm;
132
390
      if ((status = info->read_memory_func (addr + 2, buffer, 4, info)))
133
3
        goto fail;
134
387
      if (info->endian == BFD_ENDIAN_BIG)
135
109
        imm = bfd_getb32 (buffer);
136
278
      else
137
278
        imm = bfd_getl32 (buffer);
138
387
      fpr (stream, "%s\t0x%x, %s",
139
387
     opcode->name, imm, reg_names[OP_A(iword)]);
140
387
      length = 6;
141
387
    }
142
0
    break;
143
423
  case MOXIE_F1_AiB2:
144
423
    {
145
423
      unsigned imm;
146
423
      if ((status = info->read_memory_func (addr+2, buffer, 2, info)))
147
1
        goto fail;
148
422
      if (info->endian == BFD_ENDIAN_BIG)
149
134
        imm = bfd_getb16 (buffer);
150
288
      else
151
288
        imm = bfd_getl16 (buffer);
152
422
      fpr (stream, "%s\t0x%x(%s), %s", opcode->name,
153
422
     imm,
154
422
     reg_names[OP_A(iword)],
155
422
     reg_names[OP_B(iword)]);
156
422
      length = 4;
157
422
    }
158
0
    break;
159
599
  case MOXIE_F1_ABi2:
160
599
    {
161
599
      unsigned imm;
162
599
      if ((status = info->read_memory_func (addr+2, buffer, 2, info)))
163
1
        goto fail;
164
598
      if (info->endian == BFD_ENDIAN_BIG)
165
149
        imm = bfd_getb16 (buffer);
166
449
      else
167
449
        imm = bfd_getl16 (buffer);
168
598
      fpr (stream, "%s\t%s, 0x%x(%s)",
169
598
     opcode->name,
170
598
     reg_names[OP_A(iword)],
171
598
     imm,
172
598
     reg_names[OP_B(iword)]);
173
598
      length = 4;
174
598
    }
175
0
    break;
176
21.6k
        case MOXIE_BAD:
177
21.6k
    fpr (stream, "bad");
178
21.6k
    break;
179
0
  default:
180
0
    abort();
181
33.1k
  }
182
33.1k
    }
183
16.1k
  else if ((iword & (1<<14)) == 0)
184
4.68k
    {
185
      /* Extract the Form 2 opcode.  */
186
4.68k
      opcode = &moxie_form2_opc_info[(iword >> 12) & 3];
187
4.68k
      switch (opcode->itype)
188
4.68k
  {
189
4.68k
  case MOXIE_F2_A8V:
190
4.68k
    fpr (stream, "%s\t%s, 0x%x",
191
4.68k
         opcode->name,
192
4.68k
         reg_names[(iword >> 8) & 0xf],
193
4.68k
         iword & ((1 << 8) - 1));
194
4.68k
    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
4.68k
  }
204
4.68k
    }
205
11.5k
  else
206
11.5k
    {
207
      /* Extract the Form 3 opcode.  */
208
11.5k
      opcode = &moxie_form3_opc_info[(iword >> 10) & 15];
209
11.5k
      switch (opcode->itype)
210
11.5k
  {
211
4.39k
  case MOXIE_F3_PCREL:
212
4.39k
    fpr (stream, "%s\t", opcode->name);
213
4.39k
    info->print_address_func (addr + INST2OFFSET (iword) + 2, info);
214
4.39k
    break;
215
7.12k
        case MOXIE_BAD:
216
7.12k
    fpr (stream, "bad");
217
7.12k
    break;
218
0
  default:
219
0
    abort();
220
11.5k
  }
221
11.5k
    }
222
223
49.3k
  return length;
224
225
65
 fail:
226
65
  info->memory_error_func (status, addr, info);
227
65
  return -1;
228
49.3k
}