Coverage Report

Created: 2024-05-21 06:29

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