Coverage Report

Created: 2023-08-28 06:23

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