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