Coverage Report

Created: 2024-08-21 06:24

/src/capstonenext/arch/XCore/XCoreMapping.c
Line
Count
Source (jump to first uncovered line)
1
/* Capstone Disassembly Engine */
2
/* By Nguyen Anh Quynh <aquynh@gmail.com>, 2013-2015 */
3
4
#ifdef CAPSTONE_HAS_XCORE
5
6
#include <stdio.h>  // debug
7
#include <string.h>
8
9
#include "../../Mapping.h"
10
#include "../../utils.h"
11
12
#include "XCoreMapping.h"
13
14
#define GET_INSTRINFO_ENUM
15
#include "XCoreGenInstrInfo.inc"
16
17
static const name_map reg_name_maps[] = {
18
  { XCORE_REG_INVALID, NULL },
19
20
  { XCORE_REG_CP, "cp" },
21
  { XCORE_REG_DP, "dp" },
22
  { XCORE_REG_LR, "lr" },
23
  { XCORE_REG_SP, "sp" },
24
  { XCORE_REG_R0, "r0" },
25
  { XCORE_REG_R1, "r1" },
26
  { XCORE_REG_R2, "r2" },
27
  { XCORE_REG_R3, "r3" },
28
  { XCORE_REG_R4, "r4" },
29
  { XCORE_REG_R5, "r5" },
30
  { XCORE_REG_R6, "r6" },
31
  { XCORE_REG_R7, "r7" },
32
  { XCORE_REG_R8, "r8" },
33
  { XCORE_REG_R9, "r9" },
34
  { XCORE_REG_R10, "r10" },
35
  { XCORE_REG_R11, "r11" },
36
37
  // pseudo registers
38
  { XCORE_REG_PC, "pc" },
39
40
  { XCORE_REG_SCP, "scp" },
41
  { XCORE_REG_SSR, "ssr" },
42
  { XCORE_REG_ET, "et" },
43
  { XCORE_REG_ED, "ed" },
44
  { XCORE_REG_SED, "sed" },
45
  { XCORE_REG_KEP, "kep" },
46
  { XCORE_REG_KSP, "ksp" },
47
  { XCORE_REG_ID, "id" },
48
};
49
50
const char *XCore_reg_name(csh handle, unsigned int reg)
51
27.9k
{
52
27.9k
#ifndef CAPSTONE_DIET
53
27.9k
  if (reg >= ARR_SIZE(reg_name_maps))
54
0
    return NULL;
55
56
27.9k
  return reg_name_maps[reg].name;
57
#else
58
  return NULL;
59
#endif
60
27.9k
}
61
62
xcore_reg XCore_reg_id(char *name)
63
7.60k
{
64
7.60k
  int i;
65
66
134k
  for(i = 1; i < ARR_SIZE(reg_name_maps); i++) {
67
131k
    if (!strcmp(name, reg_name_maps[i].name))
68
4.81k
      return reg_name_maps[i].id;
69
131k
  }
70
71
  // not found
72
2.79k
  return 0;
73
7.60k
}
74
75
static const insn_map insns[] = {
76
  // dummy item
77
  {
78
    0, 0,
79
#ifndef CAPSTONE_DIET
80
    { 0 }, { 0 }, { 0 }, 0, 0
81
#endif
82
  },
83
84
#include "XCoreMappingInsn.inc"
85
};
86
87
// given internal insn id, return public instruction info
88
void XCore_get_insn_id(cs_struct *h, cs_insn *insn, unsigned int id)
89
84.0k
{
90
84.0k
  unsigned short i;
91
92
84.0k
  i = insn_find(insns, ARR_SIZE(insns), id, &h->insn_cache);
93
84.0k
  if (i != 0) {
94
84.0k
    insn->id = insns[i].mapid;
95
96
84.0k
    if (h->detail_opt) {
97
84.0k
#ifndef CAPSTONE_DIET
98
84.0k
      memcpy(insn->detail->regs_read, insns[i].regs_use, sizeof(insns[i].regs_use));
99
84.0k
      insn->detail->regs_read_count = (uint8_t)count_positive(insns[i].regs_use);
100
101
84.0k
      memcpy(insn->detail->regs_write, insns[i].regs_mod, sizeof(insns[i].regs_mod));
102
84.0k
      insn->detail->regs_write_count = (uint8_t)count_positive(insns[i].regs_mod);
103
104
84.0k
      memcpy(insn->detail->groups, insns[i].groups, sizeof(insns[i].groups));
105
84.0k
      insn->detail->groups_count = (uint8_t)count_positive8(insns[i].groups);
106
107
84.0k
      if (insns[i].branch || insns[i].indirect_branch) {
108
        // this insn also belongs to JUMP group. add JUMP group
109
4.81k
        insn->detail->groups[insn->detail->groups_count] = XCORE_GRP_JUMP;
110
4.81k
        insn->detail->groups_count++;
111
4.81k
      }
112
84.0k
#endif
113
84.0k
    }
114
84.0k
  }
115
84.0k
}
116
117
#ifndef CAPSTONE_DIET
118
static const name_map insn_name_maps[] = {
119
  { XCORE_INS_INVALID, NULL },
120
121
  { XCORE_INS_ADD, "add" },
122
  { XCORE_INS_ANDNOT, "andnot" },
123
  { XCORE_INS_AND, "and" },
124
  { XCORE_INS_ASHR, "ashr" },
125
  { XCORE_INS_BAU, "bau" },
126
  { XCORE_INS_BITREV, "bitrev" },
127
  { XCORE_INS_BLA, "bla" },
128
  { XCORE_INS_BLAT, "blat" },
129
  { XCORE_INS_BL, "bl" },
130
  { XCORE_INS_BF, "bf" },
131
  { XCORE_INS_BT, "bt" },
132
  { XCORE_INS_BU, "bu" },
133
  { XCORE_INS_BRU, "bru" },
134
  { XCORE_INS_BYTEREV, "byterev" },
135
  { XCORE_INS_CHKCT, "chkct" },
136
  { XCORE_INS_CLRE, "clre" },
137
  { XCORE_INS_CLRPT, "clrpt" },
138
  { XCORE_INS_CLRSR, "clrsr" },
139
  { XCORE_INS_CLZ, "clz" },
140
  { XCORE_INS_CRC8, "crc8" },
141
  { XCORE_INS_CRC32, "crc32" },
142
  { XCORE_INS_DCALL, "dcall" },
143
  { XCORE_INS_DENTSP, "dentsp" },
144
  { XCORE_INS_DGETREG, "dgetreg" },
145
  { XCORE_INS_DIVS, "divs" },
146
  { XCORE_INS_DIVU, "divu" },
147
  { XCORE_INS_DRESTSP, "drestsp" },
148
  { XCORE_INS_DRET, "dret" },
149
  { XCORE_INS_ECALLF, "ecallf" },
150
  { XCORE_INS_ECALLT, "ecallt" },
151
  { XCORE_INS_EDU, "edu" },
152
  { XCORE_INS_EEF, "eef" },
153
  { XCORE_INS_EET, "eet" },
154
  { XCORE_INS_EEU, "eeu" },
155
  { XCORE_INS_ENDIN, "endin" },
156
  { XCORE_INS_ENTSP, "entsp" },
157
  { XCORE_INS_EQ, "eq" },
158
  { XCORE_INS_EXTDP, "extdp" },
159
  { XCORE_INS_EXTSP, "extsp" },
160
  { XCORE_INS_FREER, "freer" },
161
  { XCORE_INS_FREET, "freet" },
162
  { XCORE_INS_GETD, "getd" },
163
  { XCORE_INS_GET, "get" },
164
  { XCORE_INS_GETN, "getn" },
165
  { XCORE_INS_GETR, "getr" },
166
  { XCORE_INS_GETSR, "getsr" },
167
  { XCORE_INS_GETST, "getst" },
168
  { XCORE_INS_GETTS, "getts" },
169
  { XCORE_INS_INCT, "inct" },
170
  { XCORE_INS_INIT, "init" },
171
  { XCORE_INS_INPW, "inpw" },
172
  { XCORE_INS_INSHR, "inshr" },
173
  { XCORE_INS_INT, "int" },
174
  { XCORE_INS_IN, "in" },
175
  { XCORE_INS_KCALL, "kcall" },
176
  { XCORE_INS_KENTSP, "kentsp" },
177
  { XCORE_INS_KRESTSP, "krestsp" },
178
  { XCORE_INS_KRET, "kret" },
179
  { XCORE_INS_LADD, "ladd" },
180
  { XCORE_INS_LD16S, "ld16s" },
181
  { XCORE_INS_LD8U, "ld8u" },
182
  { XCORE_INS_LDA16, "lda16" },
183
  { XCORE_INS_LDAP, "ldap" },
184
  { XCORE_INS_LDAW, "ldaw" },
185
  { XCORE_INS_LDC, "ldc" },
186
  { XCORE_INS_LDW, "ldw" },
187
  { XCORE_INS_LDIVU, "ldivu" },
188
  { XCORE_INS_LMUL, "lmul" },
189
  { XCORE_INS_LSS, "lss" },
190
  { XCORE_INS_LSUB, "lsub" },
191
  { XCORE_INS_LSU, "lsu" },
192
  { XCORE_INS_MACCS, "maccs" },
193
  { XCORE_INS_MACCU, "maccu" },
194
  { XCORE_INS_MJOIN, "mjoin" },
195
  { XCORE_INS_MKMSK, "mkmsk" },
196
  { XCORE_INS_MSYNC, "msync" },
197
  { XCORE_INS_MUL, "mul" },
198
  { XCORE_INS_NEG, "neg" },
199
  { XCORE_INS_NOT, "not" },
200
  { XCORE_INS_OR, "or" },
201
  { XCORE_INS_OUTCT, "outct" },
202
  { XCORE_INS_OUTPW, "outpw" },
203
  { XCORE_INS_OUTSHR, "outshr" },
204
  { XCORE_INS_OUTT, "outt" },
205
  { XCORE_INS_OUT, "out" },
206
  { XCORE_INS_PEEK, "peek" },
207
  { XCORE_INS_REMS, "rems" },
208
  { XCORE_INS_REMU, "remu" },
209
  { XCORE_INS_RETSP, "retsp" },
210
  { XCORE_INS_SETCLK, "setclk" },
211
  { XCORE_INS_SET, "set" },
212
  { XCORE_INS_SETC, "setc" },
213
  { XCORE_INS_SETD, "setd" },
214
  { XCORE_INS_SETEV, "setev" },
215
  { XCORE_INS_SETN, "setn" },
216
  { XCORE_INS_SETPSC, "setpsc" },
217
  { XCORE_INS_SETPT, "setpt" },
218
  { XCORE_INS_SETRDY, "setrdy" },
219
  { XCORE_INS_SETSR, "setsr" },
220
  { XCORE_INS_SETTW, "settw" },
221
  { XCORE_INS_SETV, "setv" },
222
  { XCORE_INS_SEXT, "sext" },
223
  { XCORE_INS_SHL, "shl" },
224
  { XCORE_INS_SHR, "shr" },
225
  { XCORE_INS_SSYNC, "ssync" },
226
  { XCORE_INS_ST16, "st16" },
227
  { XCORE_INS_ST8, "st8" },
228
  { XCORE_INS_STW, "stw" },
229
  { XCORE_INS_SUB, "sub" },
230
  { XCORE_INS_SYNCR, "syncr" },
231
  { XCORE_INS_TESTCT, "testct" },
232
  { XCORE_INS_TESTLCL, "testlcl" },
233
  { XCORE_INS_TESTWCT, "testwct" },
234
  { XCORE_INS_TSETMR, "tsetmr" },
235
  { XCORE_INS_START, "start" },
236
  { XCORE_INS_WAITEF, "waitef" },
237
  { XCORE_INS_WAITET, "waitet" },
238
  { XCORE_INS_WAITEU, "waiteu" },
239
  { XCORE_INS_XOR, "xor" },
240
  { XCORE_INS_ZEXT, "zext" },
241
};
242
243
// special alias insn
244
static const name_map alias_insn_names[] = {
245
  { 0, NULL }
246
};
247
#endif
248
249
const char *XCore_insn_name(csh handle, unsigned int id)
250
84.0k
{
251
84.0k
#ifndef CAPSTONE_DIET
252
84.0k
  unsigned int i;
253
254
84.0k
  if (id >= XCORE_INS_ENDING)
255
0
    return NULL;
256
257
  // handle special alias first
258
168k
  for (i = 0; i < ARR_SIZE(alias_insn_names); i++) {
259
84.0k
    if (alias_insn_names[i].id == id)
260
0
      return alias_insn_names[i].name;
261
84.0k
  }
262
263
84.0k
  return insn_name_maps[id].name;
264
#else
265
  return NULL;
266
#endif
267
84.0k
}
268
269
#ifndef CAPSTONE_DIET
270
static const name_map group_name_maps[] = {
271
  { XCORE_GRP_INVALID, NULL },
272
  { XCORE_GRP_JUMP, "jump" },
273
};
274
#endif
275
276
const char *XCore_group_name(csh handle, unsigned int id)
277
4.81k
{
278
4.81k
#ifndef CAPSTONE_DIET
279
4.81k
  return id2name(group_name_maps, ARR_SIZE(group_name_maps), id);
280
#else
281
  return NULL;
282
#endif
283
4.81k
}
284
285
// map internal raw register to 'public' register
286
xcore_reg XCore_map_register(unsigned int r)
287
0
{
288
0
  static const unsigned int map[] = { 0,
289
0
  };
290
291
0
  if (r < ARR_SIZE(map))
292
0
    return map[r];
293
294
  // cannot find this register
295
0
  return 0;
296
0
}
297
298
#endif