/src/capstonenext/arch/RISCV/RISCVMapping.c
Line | Count | Source |
1 | | #include "capstone/cs_operand.h" |
2 | | #include "capstone/riscv.h" |
3 | | #include <stdint.h> |
4 | | #include <float.h> |
5 | | #include <math.h> |
6 | | #ifdef CAPSTONE_HAS_RISCV |
7 | | |
8 | | #include <string.h> |
9 | | |
10 | | #include "../../Mapping.h" |
11 | | #include "../../cs_simple_types.h" |
12 | | #include "../../utils.h" |
13 | | |
14 | | #include "RISCVMapping.h" |
15 | | |
16 | | #define GET_INSTRINFO_ENUM |
17 | | #include "RISCVGenInstrInfo.inc" |
18 | | |
19 | | #define GET_REGINFO_ENUM |
20 | | #define GET_REGINFO_MC_DESC |
21 | | #include "RISCVGenRegisterInfo.inc" |
22 | | |
23 | | #include "RISCVInstPrinter.h" |
24 | | |
25 | | const char *RISCV_reg_name(csh handle, unsigned int reg) |
26 | 9.08k | { |
27 | 9.08k | int syntax_opt = ((cs_struct *)(uintptr_t)handle)->syntax; |
28 | | |
29 | 9.08k | if (syntax_opt & CS_OPT_SYNTAX_NOREGNAME) { |
30 | 0 | return RISCV_LLVM_getRegisterName(reg, RISCV_NoRegAltName); |
31 | 0 | } |
32 | 9.08k | return RISCV_LLVM_getRegisterName(reg, RISCV_ABIRegAltName); |
33 | 9.08k | } |
34 | | |
35 | | static const insn_map insns[] = { |
36 | | #include "RISCVGenCSMappingInsn.inc" |
37 | | }; |
38 | | |
39 | | const insn_map *RISCV_insns = insns; |
40 | | const unsigned int RISCV_insn_count = ARR_SIZE(insns); |
41 | | |
42 | | #ifndef CAPSTONE_DIET |
43 | | static const map_insn_ops insn_operands[] = { |
44 | | #include "RISCVGenCSMappingInsnOp.inc" |
45 | | }; |
46 | | |
47 | | static const name_map insn_alias_mnem_map[] = { |
48 | | #include "RISCVGenCSAliasMnemMap.inc" |
49 | | }; |
50 | | #endif |
51 | | |
52 | | void RISCV_add_cs_detail_0(MCInst *MI, riscv_op_group opgroup, unsigned OpNum) |
53 | 92.4k | { |
54 | 92.4k | if (!detail_is_set(MI)) |
55 | 0 | return; |
56 | | // are not "true" arguments and has no Capstone equivalent |
57 | 92.4k | if (opgroup == RISCV_OP_GROUP_FRMArg || |
58 | 91.2k | opgroup == RISCV_OP_GROUP_FRMArgLegacy) |
59 | 1.46k | return; |
60 | | |
61 | 90.9k | if (opgroup == RISCV_OP_GROUP_FPImmOperand) { |
62 | 505 | unsigned Imm = (unsigned)MCInst_getOperand(MI, OpNum)->ImmVal; |
63 | 505 | cs_riscv_op *op = RISCV_get_detail_op_at(MI, OpNum); |
64 | 505 | op->type = RISCV_OP_FP; |
65 | 505 | op->access = (cs_ac_type)map_get_op_access(MI, OpNum); |
66 | 505 | switch (Imm) { |
67 | 34 | case 1: // min |
68 | 34 | switch (MI->Opcode) { |
69 | 21 | case RISCV_FLI_S: |
70 | 21 | op->dimm = (double)FLT_MIN; |
71 | 21 | break; |
72 | 0 | case RISCV_FLI_D: |
73 | 0 | op->dimm = (double)DBL_MIN; |
74 | 0 | break; |
75 | 13 | case RISCV_FLI_H: |
76 | 13 | op->dimm = 6.103515625e-05; |
77 | 13 | break; |
78 | 0 | default: |
79 | 0 | op->dimm = 0.0; |
80 | 0 | break; |
81 | 34 | } |
82 | 34 | break; |
83 | 52 | case 30: // inf |
84 | 52 | op->dimm = INFINITY; |
85 | 52 | break; |
86 | 155 | case 31: // nan |
87 | 155 | op->dimm = NAN; |
88 | 155 | break; |
89 | 264 | default: |
90 | 264 | op->dimm = (double)getFPImm(Imm); |
91 | 264 | break; |
92 | 505 | } |
93 | 505 | RISCV_inc_op_count(MI); |
94 | 505 | return; |
95 | 505 | } |
96 | 90.4k | cs_riscv_op *op = RISCV_get_detail_op_at(MI, OpNum); |
97 | 90.4k | op->type = (riscv_op_type)map_get_op_type(MI, OpNum); |
98 | 90.4k | op->access = (cs_ac_type)map_get_op_access(MI, OpNum); |
99 | 90.4k | switch (map_get_op_type(MI, OpNum)) { |
100 | 56.7k | case CS_OP_REG: |
101 | 56.7k | op->reg = MCInst_getOperand(MI, OpNum)->RegVal; |
102 | 56.7k | break; |
103 | 0 | case CS_OP_MEM: |
104 | 0 | op->mem.base = 0; |
105 | 0 | op->mem.disp = MCInst_getOperand(MI, OpNum)->ImmVal; |
106 | 0 | break; |
107 | 18.2k | case CS_OP_IMM: { |
108 | 18.2k | uint64_t val = MCInst_getOperand(MI, OpNum)->ImmVal; |
109 | 18.2k | if (opgroup != RISCV_OP_GROUP_CSRSystemRegister) { |
110 | 17.2k | op->imm = val; |
111 | 17.2k | if (opgroup == RISCV_OP_GROUP_BranchOperand) { |
112 | 2.80k | op->imm += MI->address; |
113 | 2.80k | } |
114 | 17.2k | } else /* system register read-write */ { |
115 | 986 | op->type = RISCV_OP_CSR; |
116 | 986 | op->csr = val; |
117 | | // CSR instruction always read-writes the system operand |
118 | 986 | op->access = CS_AC_READ_WRITE; |
119 | 986 | } |
120 | 18.2k | break; |
121 | 0 | } |
122 | 8.48k | case CS_OP_MEM_REG: |
123 | 8.48k | op->type = (riscv_op_type)CS_OP_MEM; |
124 | 8.48k | op->mem.base = MCInst_getOperand(MI, OpNum)->RegVal; |
125 | 8.48k | break; |
126 | 6.88k | case CS_OP_MEM_IMM: |
127 | | // fill in the disp in the last operand |
128 | 6.88k | op = RISCV_get_detail_op_at(MI, OpNum - 1); |
129 | 6.88k | op->type = (riscv_op_type)CS_OP_MEM; |
130 | 6.88k | op->mem.disp = MCInst_getOperand(MI, OpNum)->ImmVal; |
131 | 6.88k | RISCV_dec_op_count( |
132 | 6.88k | MI); // don't increase the count, cancel the coming increment |
133 | 6.88k | break; |
134 | 181 | case CS_OP_INVALID: |
135 | 181 | break; |
136 | 0 | default: { |
137 | 0 | CS_ASSERT(0 && "unhandled operand type"); |
138 | 0 | } |
139 | 90.4k | } |
140 | 90.4k | RISCV_inc_op_count(MI); |
141 | 90.4k | } |
142 | | |
143 | | static inline void RISCV_add_adhoc_groups(MCInst *MI); |
144 | | |
145 | | void RISCV_add_groups(MCInst *MI) |
146 | 37.0k | { |
147 | 37.0k | if (!detail_is_set(MI)) |
148 | 0 | return; |
149 | | |
150 | 37.0k | get_detail(MI)->groups_count = 0; |
151 | | |
152 | 37.0k | #ifndef CAPSTONE_DIET |
153 | 37.0k | int i = 0; |
154 | 83.5k | while (insns[MI->Opcode].groups[i] != 0) { |
155 | 46.5k | add_group(MI, insns[MI->Opcode].groups[i]); |
156 | 46.5k | i++; |
157 | 46.5k | } |
158 | 37.0k | #endif |
159 | | |
160 | 37.0k | RISCV_add_adhoc_groups(MI); |
161 | 37.0k | } |
162 | | |
163 | | enum { |
164 | | #define GET_ENUM_VALUES_RISCVOpcode |
165 | | #include "RISCVGenCSSystemOperandsEnum.inc" |
166 | | }; |
167 | | |
168 | | static inline void RISCV_add_privileged_group(MCInst *MI) |
169 | 37.0k | { |
170 | 37.0k | const uint8_t *bytes = MI->flat_insn->bytes; |
171 | 37.0k | uint8_t opcode = bytes[0] & 0x80; |
172 | | // no privileged instruction has a major opcode other than SYSTEM |
173 | 37.0k | if (opcode != RISCV_RISCVOPCODE_SYSTEM) { |
174 | 37.0k | return; |
175 | 37.0k | } |
176 | 0 | uint8_t func3 = (bytes[1] >> 4) & 0x7; |
177 | | // no privileged instruction has a minor opcode other than PRIV or PRIVM |
178 | 0 | if (func3 != 0 && func3 != 0x4) { |
179 | 0 | return; |
180 | 0 | } |
181 | 0 | uint16_t func12 = readBytes16(MI, &(bytes[2])) >> 4; |
182 | | // ecall and ebreak has SYSTEM and PRIV but aren't privileged |
183 | 0 | if (func12 == 0 || func12 == 1) { |
184 | 0 | return; |
185 | 0 | } |
186 | 0 | uint8_t func6 = func12 >> 6; |
187 | | // a subspace under extension-defined custom SYSTEM instructions that is not privileged |
188 | 0 | if (func6 == 0x23 || func6 == 0x33) { |
189 | 0 | return; |
190 | 0 | } |
191 | 0 | add_group(MI, RISCV_GRP_PRIVILEGE); |
192 | 0 | } |
193 | | |
194 | | static inline void RISCV_add_interrupt_group(MCInst *MI) |
195 | 37.0k | { |
196 | 37.0k | if (MI->Opcode == RISCV_ECALL || MI->Opcode == RISCV_EBREAK) { |
197 | 32 | add_group(MI, RISCV_GRP_INT); |
198 | 32 | } |
199 | 37.0k | } |
200 | | |
201 | | static inline void RISCV_add_interrupt_ret_group(MCInst *MI) |
202 | 37.0k | { |
203 | 37.0k | if (MI->Opcode == RISCV_MRET || MI->Opcode == RISCV_SRET) { |
204 | 10 | add_group(MI, RISCV_GRP_IRET); |
205 | 10 | } |
206 | 37.0k | } |
207 | | |
208 | | // calls are implemented in RISCV as plain jumps that happen to set a link register containing the return address |
209 | | // but this link register could be given as the null register x0, discarding the return address and making them jumps |
210 | | static inline void RISCV_add_call_group(MCInst *MI) |
211 | 37.0k | { |
212 | 37.0k | if (MI->Opcode == RISCV_JAL || MI->Opcode == RISCV_JALR) { |
213 | 1.17k | cs_riscv_op *op = RISCV_get_detail_op_at(MI, 0); |
214 | 1.17k | if ((op->type == (riscv_op_type)CS_OP_REG) && |
215 | 593 | op->reg != RISCV_REG_X0 && (op->access & CS_AC_WRITE)) { |
216 | 593 | add_group(MI, RISCV_GRP_CALL); |
217 | 593 | } |
218 | 1.17k | if (MI->Opcode == RISCV_JAL) { |
219 | 338 | add_group(MI, RISCV_GRP_BRANCH_RELATIVE); |
220 | 338 | } |
221 | 1.17k | } |
222 | 37.0k | } |
223 | | |
224 | | // returns are implemented in RISCV as a plain indirect jump that happen to reference the return address register ra == x1 |
225 | | static inline void RISCV_add_ret_group(MCInst *MI) |
226 | 37.0k | { |
227 | 37.0k | if (MI->Opcode == RISCV_C_JR) { |
228 | | // indirect jumps whose source is ra |
229 | 163 | cs_riscv_op *op = RISCV_get_detail_op_at(MI, 0); |
230 | 163 | if ((op->type == (riscv_op_type)CS_OP_REG) && |
231 | 0 | op->reg == RISCV_REG_X1) { |
232 | 0 | add_group(MI, RISCV_GRP_RET); |
233 | 163 | } else { |
234 | 163 | add_group(MI, RISCV_GRP_JUMP); |
235 | 163 | } |
236 | 163 | } |
237 | 37.0k | if (MI->Opcode == RISCV_JALR) { |
238 | | // indirect jumps whose source is ra |
239 | 839 | cs_riscv_op *dstreg = RISCV_get_detail_op_at(MI, 0); |
240 | 839 | cs_riscv_op *op = RISCV_get_detail_op_at(MI, 1); |
241 | 839 | cs_riscv_op *op2 = RISCV_get_detail_op_at(MI, 2); |
242 | 839 | if ((op->type == (riscv_op_type)CS_OP_REG) && |
243 | 664 | op->reg == RISCV_REG_X1 && |
244 | 277 | op2->type == (riscv_op_type)CS_OP_IMM && op2->imm == 0 && |
245 | 0 | dstreg->type == (riscv_op_type)CS_OP_REG && |
246 | 0 | dstreg->reg == RISCV_REG_X0) { |
247 | 0 | add_group(MI, RISCV_GRP_RET); |
248 | 839 | } else { |
249 | 839 | if (!((dstreg->type == (riscv_op_type)CS_OP_REG) && |
250 | 300 | dstreg->reg != RISCV_REG_X0 && |
251 | 539 | (dstreg->access & CS_AC_WRITE))) { |
252 | 539 | add_group(MI, RISCV_GRP_JUMP); |
253 | 539 | } |
254 | 839 | } |
255 | 839 | } |
256 | 37.0k | } |
257 | | |
258 | | static inline void RISCV_add_adhoc_groups(MCInst *MI) |
259 | 37.0k | { |
260 | 37.0k | RISCV_add_privileged_group(MI); |
261 | 37.0k | RISCV_add_interrupt_group(MI); |
262 | 37.0k | RISCV_add_interrupt_ret_group(MI); |
263 | 37.0k | RISCV_add_call_group(MI); |
264 | 37.0k | RISCV_add_ret_group(MI); |
265 | 37.0k | } |
266 | | |
267 | | // memset all stalled values in the detail struct to 0 before disassembling any next instruction |
268 | | void RISCV_init_cs_detail(MCInst *MI) |
269 | 37.6k | { |
270 | 37.6k | if (detail_is_set(MI)) |
271 | 37.6k | memset(get_detail(MI), 0, |
272 | 37.6k | offsetof(cs_detail, riscv) + sizeof(cs_riscv)); |
273 | 37.6k | } |
274 | | |
275 | | // for weird reasons some instructions end up with valid operands that are |
276 | | // interspersed with invalid operands, i.e. the operands array is an "island" |
277 | | // of valid operands with invalid gaps between them, this function will compactify |
278 | | // all the valid operands and pad the rest of the array to invalid |
279 | | void RISCV_compact_operands(MCInst *MI) |
280 | 37.0k | { |
281 | 37.0k | if (!detail_is_set(MI)) |
282 | 0 | return; |
283 | 37.0k | cs_riscv_op *ops = RISCV_get_detail(MI)->operands; |
284 | 37.0k | unsigned int write_pos = 0; |
285 | | |
286 | | // Move valid elements to front |
287 | 333k | for (unsigned int read_pos = 0; read_pos < NUM_RISCV_OPS; read_pos++) { |
288 | 296k | if (ops[read_pos].type != (riscv_op_type)CS_OP_INVALID) { |
289 | 90.7k | if (write_pos != read_pos) { |
290 | 10.8k | ops[write_pos] = ops[read_pos]; |
291 | 10.8k | } |
292 | 90.7k | write_pos++; |
293 | 90.7k | } |
294 | 296k | } |
295 | | // fill the rest, if any, with invalid |
296 | 37.0k | memset((void *)(&ops[write_pos]), CS_OP_INVALID, |
297 | 37.0k | (NUM_RISCV_OPS - write_pos) * sizeof(cs_riscv_op)); |
298 | 37.0k | } |
299 | | |
300 | | // given internal insn id, return public instruction info |
301 | | void RISCV_get_insn_id(cs_struct *h, cs_insn *insn, unsigned int id) |
302 | 37.0k | { |
303 | 37.0k | insn_map const *insn_map = NULL; |
304 | | |
305 | 37.0k | if ((insn_map = lookup_insn_map(h, id))) { |
306 | 37.0k | insn->id = insn_map->mapid; |
307 | | |
308 | 37.0k | if (h->detail_opt) { |
309 | 37.0k | #ifndef CAPSTONE_DIET |
310 | 37.0k | memcpy(insn->detail->regs_read, insn_map->regs_use, |
311 | 37.0k | sizeof(insn_map->regs_use)); |
312 | 37.0k | insn->detail->regs_read_count = |
313 | 37.0k | (uint8_t)count_positive(insn_map->regs_use); |
314 | | |
315 | 37.0k | memcpy(insn->detail->regs_write, insn_map->regs_mod, |
316 | 37.0k | sizeof(insn_map->regs_mod)); |
317 | 37.0k | insn->detail->regs_write_count = |
318 | 37.0k | (uint8_t)count_positive(insn_map->regs_mod); |
319 | | |
320 | 37.0k | memcpy(insn->detail->groups, insn_map->groups, |
321 | 37.0k | sizeof(insn_map->groups)); |
322 | 37.0k | insn->detail->groups_count = |
323 | 37.0k | (uint8_t)count_positive8(insn_map->groups); |
324 | | |
325 | 37.0k | if (insn_map->branch || insn_map->indirect_branch) { |
326 | | // this insn also belongs to JUMP group. add JUMP group |
327 | 1.83k | insn->detail |
328 | 1.83k | ->groups[insn->detail->groups_count] = |
329 | 1.83k | RISCV_GRP_JUMP; |
330 | 1.83k | insn->detail->groups_count++; |
331 | 1.83k | } |
332 | 37.0k | #endif |
333 | 37.0k | } |
334 | 37.0k | } |
335 | 37.0k | } |
336 | | |
337 | | static const char *const insn_name_maps[] = { |
338 | | #include "RISCVGenCSMappingInsnName.inc" |
339 | | }; |
340 | | |
341 | | // called from RISCV_LLVM_printInstruction() to avoid exporting |
342 | | // insn_alias_mnem_map and its size via extern declarations |
343 | | void RISCV_set_alias_id(MCInst *MI, SStream *O) |
344 | 37.0k | { |
345 | 37.0k | #ifndef CAPSTONE_DIET |
346 | 37.0k | map_set_alias_id(MI, O, insn_alias_mnem_map, |
347 | 37.0k | ARR_SIZE(insn_alias_mnem_map)); |
348 | 37.0k | #endif |
349 | 37.0k | } |
350 | | |
351 | | const char *RISCV_insn_name(csh handle, unsigned int id) |
352 | 37.0k | { |
353 | 37.0k | #ifndef CAPSTONE_DIET |
354 | 37.0k | if (id < RISCV_INS_ENDING) |
355 | 37.0k | return insn_name_maps[id]; |
356 | | |
357 | 0 | if (id > RISCV_INS_ALIAS_BEGIN && id < RISCV_INS_ALIAS_END) |
358 | 0 | return insn_alias_mnem_map[id - RISCV_INS_ALIAS_BEGIN - 1].name; |
359 | 0 | #endif |
360 | 0 | return NULL; |
361 | 0 | } |
362 | | |
363 | | #ifndef CAPSTONE_DIET |
364 | | static const name_map group_name_maps[] = { |
365 | | // generic groups |
366 | | { RISCV_GRP_INVALID, NULL }, |
367 | | { RISCV_GRP_JUMP, "jump" }, |
368 | | { RISCV_GRP_CALL, "call" }, |
369 | | { RISCV_GRP_RET, "ret" }, |
370 | | { RISCV_GRP_INT, "int" }, |
371 | | { RISCV_GRP_IRET, "iret" }, |
372 | | { RISCV_GRP_PRIVILEGE, "privileged" }, |
373 | | { RISCV_GRP_BRANCH_RELATIVE, "branch_relative" }, |
374 | | |
375 | | // architecture specific |
376 | | #include "RISCVGenCSFeatureName.inc" |
377 | | |
378 | | { RISCV_GRP_ENDING, NULL } |
379 | | }; |
380 | | #endif |
381 | | |
382 | | const char *RISCV_group_name(csh handle, unsigned int id) |
383 | 95.8k | { |
384 | 95.8k | #ifndef CAPSTONE_DIET |
385 | | // verify group id |
386 | | // if past the end |
387 | 95.8k | if (id >= RISCV_GRP_ENDING || |
388 | | // or in the encoding gap between generic groups and arch-specific groups |
389 | 95.8k | (id > RISCV_GRP_BRANCH_RELATIVE && id < RISCV_FEATURE_HASSTDEXTI)) |
390 | 0 | return NULL; |
391 | 95.8k | return id2name(group_name_maps, ARR_SIZE(group_name_maps), id); |
392 | | #else |
393 | | return NULL; |
394 | | #endif |
395 | 95.8k | } |
396 | | |
397 | | // map instruction name to public instruction ID |
398 | | riscv_insn RISCV_map_insn(const char *name) |
399 | 0 | { |
400 | 0 | unsigned int i; |
401 | 0 | for (i = 1; i < ARR_SIZE(insn_name_maps); i++) { |
402 | 0 | if (!strcmp(name, insn_name_maps[i])) |
403 | 0 | return i; |
404 | 0 | } |
405 | 0 | #ifndef CAPSTONE_DIET |
406 | 0 | for (i = 0; i < ARR_SIZE(insn_alias_mnem_map); i++) { |
407 | 0 | if (!strcmp(name, insn_alias_mnem_map[i].name)) |
408 | 0 | return insn_alias_mnem_map[i].id; |
409 | 0 | } |
410 | 0 | #endif |
411 | 0 | return RISCV_INS_INVALID; |
412 | 0 | } |
413 | | |
414 | | void RISCV_reg_access(const cs_insn *insn, cs_regs regs_read, |
415 | | uint8_t *regs_read_count, cs_regs regs_write, |
416 | | uint8_t *regs_write_count) |
417 | 0 | { |
418 | 0 | const cs_riscv *riscv = &(insn->detail->riscv); |
419 | 0 | uint8_t read_count = 0; |
420 | 0 | uint8_t write_count = 0; |
421 | |
|
422 | 0 | for (int j = 0; j < riscv->op_count; j++) { |
423 | 0 | const cs_riscv_op *op = &riscv->operands[j]; |
424 | |
|
425 | 0 | if (op->type == RISCV_OP_REG) { |
426 | 0 | if ((op->access & CS_AC_WRITE) && |
427 | 0 | !arr_exist(regs_write, write_count, op->reg)) { |
428 | 0 | regs_write[write_count++] = (uint16_t)op->reg; |
429 | 0 | } |
430 | 0 | if ((op->access & CS_AC_READ) && |
431 | 0 | !arr_exist(regs_read, read_count, op->reg)) { |
432 | 0 | regs_read[read_count++] = (uint16_t)op->reg; |
433 | 0 | } |
434 | 0 | } else if (op->type == RISCV_OP_MEM) { |
435 | 0 | if (op->mem.base != RISCV_REG_INVALID && |
436 | 0 | !arr_exist(regs_read, read_count, op->mem.base)) { |
437 | 0 | regs_read[read_count++] = |
438 | 0 | (uint16_t)op->mem.base; |
439 | 0 | } |
440 | 0 | } |
441 | 0 | } |
442 | |
|
443 | 0 | *regs_read_count = read_count; |
444 | 0 | *regs_write_count = write_count; |
445 | 0 | } |
446 | | |
447 | | void RISCV_init(MCRegisterInfo *MRI) |
448 | 3.53k | { |
449 | 3.53k | MCRegisterInfo_InitMCRegisterInfo(MRI, RISCVRegDesc, RISCV_REG_ENDING, |
450 | 3.53k | 0, 0, RISCVMCRegisterClasses, |
451 | 3.53k | ARR_SIZE(RISCVMCRegisterClasses), 0, |
452 | 3.53k | 0, RISCVRegDiffLists, 0, |
453 | 3.53k | RISCVSubRegIdxLists, |
454 | 3.53k | ARR_SIZE(RISCVSubRegIdxLists), 0); |
455 | 3.53k | } |
456 | | |
457 | | #endif |