Coverage Report

Created: 2026-04-04 08:16

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/binutils-gdb/opcodes/moxie-dis.c
Line
Count
Source
1
/* Disassemble moxie instructions.
2
   Copyright (C) 2009-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 <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
11.7k
#define OP_A(i) ((i >> 4) & 0xf)
35
8.29k
#define OP_B(i) (i & 0xf)
36
3.77k
#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
55.2k
{
45
55.2k
  int length = 2;
46
55.2k
  int status;
47
55.2k
  stream = info->stream;
48
55.2k
  const moxie_opc_info_t * opcode;
49
55.2k
  bfd_byte buffer[4];
50
55.2k
  unsigned short iword;
51
55.2k
  fpr = info->fprintf_func;
52
53
55.2k
  if ((status = info->read_memory_func (addr, buffer, 2, info)))
54
21
    goto fail;
55
56
55.2k
  if (info->endian == BFD_ENDIAN_BIG)
57
24.2k
    iword = bfd_getb16 (buffer);
58
30.9k
  else
59
30.9k
    iword = bfd_getl16 (buffer);
60
61
  /* Form 1 instructions have the high bit set to 0.  */
62
55.2k
  if ((iword & (1<<15)) == 0)
63
37.0k
    {
64
      /* Extract the Form 1 opcode.  */
65
37.0k
      opcode = &moxie_form1_opc_info[iword >> 8];
66
37.0k
      switch (opcode->itype)
67
37.0k
  {
68
979
  case MOXIE_F1_NARG:
69
979
    fpr (stream, "%s", opcode->name);
70
979
    break;
71
438
  case MOXIE_F1_A:
72
438
    fpr (stream, "%s\t%s", opcode->name,
73
438
         reg_names[OP_A(iword)]);
74
438
    break;
75
4.97k
  case MOXIE_F1_AB:
76
4.97k
    fpr (stream, "%s\t%s, %s", opcode->name,
77
4.97k
         reg_names[OP_A(iword)],
78
4.97k
         reg_names[OP_B(iword)]);
79
4.97k
    break;
80
2.29k
  case MOXIE_F1_A4:
81
2.29k
    {
82
2.29k
      unsigned imm;
83
2.29k
      if ((status = info->read_memory_func (addr + 2, buffer, 4, info)))
84
6
        goto fail;
85
2.28k
      if (info->endian == BFD_ENDIAN_BIG)
86
992
        imm = bfd_getb32 (buffer);
87
1.29k
      else
88
1.29k
        imm = bfd_getl32 (buffer);
89
2.28k
      fpr (stream, "%s\t%s, 0x%x", opcode->name,
90
2.28k
     reg_names[OP_A(iword)], imm);
91
2.28k
      length = 6;
92
2.28k
    }
93
0
    break;
94
386
  case MOXIE_F1_4:
95
386
    {
96
386
      unsigned imm;
97
386
      if ((status = info->read_memory_func (addr + 2, buffer, 4, info)))
98
2
        goto fail;
99
384
      if (info->endian == BFD_ENDIAN_BIG)
100
188
        imm = bfd_getb32 (buffer);
101
196
      else
102
196
        imm = bfd_getl32 (buffer);
103
384
      fpr (stream, "%s\t0x%x", opcode->name, imm);
104
384
      length = 6;
105
384
    }
106
0
    break;
107
434
  case MOXIE_F1_M:
108
434
    {
109
434
      unsigned imm;
110
434
      if ((status = info->read_memory_func (addr + 2, buffer, 4, info)))
111
5
        goto fail;
112
429
      if (info->endian == BFD_ENDIAN_BIG)
113
130
        imm = bfd_getb32 (buffer);
114
299
      else
115
299
        imm = bfd_getl32 (buffer);
116
429
      fpr (stream, "%s\t", opcode->name);
117
429
      info->print_address_func ((bfd_vma) imm, info);
118
429
      length = 6;
119
429
    }
120
0
    break;
121
564
  case MOXIE_F1_AiB:
122
564
    fpr (stream, "%s\t(%s), %s", opcode->name,
123
564
         reg_names[OP_A(iword)], reg_names[OP_B(iword)]);
124
564
    break;
125
774
  case MOXIE_F1_ABi:
126
774
    fpr (stream, "%s\t%s, (%s)", opcode->name,
127
774
         reg_names[OP_A(iword)], reg_names[OP_B(iword)]);
128
774
    break;
129
694
  case MOXIE_F1_4A:
130
694
    {
131
694
      unsigned imm;
132
694
      if ((status = info->read_memory_func (addr + 2, buffer, 4, info)))
133
1
        goto fail;
134
693
      if (info->endian == BFD_ENDIAN_BIG)
135
183
        imm = bfd_getb32 (buffer);
136
510
      else
137
510
        imm = bfd_getl32 (buffer);
138
693
      fpr (stream, "%s\t0x%x, %s",
139
693
     opcode->name, imm, reg_names[OP_A(iword)]);
140
693
      length = 6;
141
693
    }
142
0
    break;
143
774
  case MOXIE_F1_AiB2:
144
774
    {
145
774
      unsigned imm;
146
774
      if ((status = info->read_memory_func (addr+2, buffer, 2, info)))
147
2
        goto fail;
148
772
      if (info->endian == BFD_ENDIAN_BIG)
149
422
        imm = bfd_getb16 (buffer);
150
350
      else
151
350
        imm = bfd_getl16 (buffer);
152
772
      fpr (stream, "%s\t0x%x(%s), %s", opcode->name,
153
772
     imm,
154
772
     reg_names[OP_A(iword)],
155
772
     reg_names[OP_B(iword)]);
156
772
      length = 4;
157
772
    }
158
0
    break;
159
1.20k
  case MOXIE_F1_ABi2:
160
1.20k
    {
161
1.20k
      unsigned imm;
162
1.20k
      if ((status = info->read_memory_func (addr+2, buffer, 2, info)))
163
0
        goto fail;
164
1.20k
      if (info->endian == BFD_ENDIAN_BIG)
165
700
        imm = bfd_getb16 (buffer);
166
502
      else
167
502
        imm = bfd_getl16 (buffer);
168
1.20k
      fpr (stream, "%s\t%s, 0x%x(%s)",
169
1.20k
     opcode->name,
170
1.20k
     reg_names[OP_A(iword)],
171
1.20k
     imm,
172
1.20k
     reg_names[OP_B(iword)]);
173
1.20k
      length = 4;
174
1.20k
    }
175
0
    break;
176
23.5k
        case MOXIE_BAD:
177
23.5k
    fpr (stream, "bad");
178
23.5k
    break;
179
0
  default:
180
0
    abort();
181
37.0k
  }
182
37.0k
    }
183
18.1k
  else if ((iword & (1<<14)) == 0)
184
6.68k
    {
185
      /* Extract the Form 2 opcode.  */
186
6.68k
      opcode = &moxie_form2_opc_info[(iword >> 12) & 3];
187
6.68k
      switch (opcode->itype)
188
6.68k
  {
189
6.68k
  case MOXIE_F2_A8V:
190
6.68k
    fpr (stream, "%s\t%s, 0x%x",
191
6.68k
         opcode->name,
192
6.68k
         reg_names[(iword >> 8) & 0xf],
193
6.68k
         iword & ((1 << 8) - 1));
194
6.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
6.68k
  }
204
6.68k
    }
205
11.4k
  else
206
11.4k
    {
207
      /* Extract the Form 3 opcode.  */
208
11.4k
      opcode = &moxie_form3_opc_info[(iword >> 10) & 15];
209
11.4k
      switch (opcode->itype)
210
11.4k
  {
211
3.77k
  case MOXIE_F3_PCREL:
212
3.77k
    fpr (stream, "%s\t", opcode->name);
213
3.77k
    info->print_address_func (addr + INST2OFFSET (iword) + 2, info);
214
3.77k
    break;
215
7.70k
        case MOXIE_BAD:
216
7.70k
    fpr (stream, "bad");
217
7.70k
    break;
218
0
  default:
219
0
    abort();
220
11.4k
  }
221
11.4k
    }
222
223
55.1k
  return length;
224
225
37
 fail:
226
37
  info->memory_error_func (status, addr, info);
227
37
  return -1;
228
55.2k
}