Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
wine-mirror
GitHub Repository: wine-mirror/wine
Path: blob/master/libs/capstone/Mapping.h
4387 views
1
/* Capstone Disassembly Engine */
2
/* By Nguyen Anh Quynh <[email protected]>, 2013-2019 */
3
/* Rot127 <[email protected]>, 2022-2023 */
4
5
#ifndef CS_MAPPING_H
6
#define CS_MAPPING_H
7
8
#if defined(CAPSTONE_HAS_OSXKERNEL)
9
#include <libkern/libkern.h>
10
#else
11
#include "include/capstone/capstone.h"
12
#include <stddef.h>
13
#endif
14
#include "cs_priv.h"
15
#include <assert.h>
16
#include <string.h>
17
18
// map instruction to its characteristics
19
typedef struct insn_map {
20
unsigned short id; // The LLVM instruction id
21
unsigned short mapid; // The Capstone instruction id
22
#ifndef CAPSTONE_DIET
23
uint16_t regs_use[MAX_IMPL_R_REGS]; ///< list of implicit registers used by
24
///< this instruction
25
uint16_t regs_mod[MAX_IMPL_W_REGS]; ///< list of implicit registers modified
26
///< by this instruction
27
unsigned char groups
28
[MAX_NUM_GROUPS]; ///< list of group this instruction belong to
29
bool branch; // branch instruction?
30
bool indirect_branch; // indirect branch instruction?
31
#endif
32
} insn_map;
33
34
// look for @id in @m, given its size in @max. first time call will update
35
// @cache. return 0 if not found
36
unsigned short insn_find(const insn_map *m, unsigned int max, unsigned int id,
37
unsigned short **cache);
38
39
unsigned int find_cs_id(unsigned MC_Opcode, const insn_map *imap,
40
unsigned imap_size);
41
42
#define MAX_NO_DATA_TYPES 10
43
44
///< A LLVM<->CS Mapping entry of an MCOperand.
45
typedef struct {
46
uint8_t /* cs_op_type */ type; ///< Operand type (e.g.: reg, imm, mem)
47
uint8_t /* cs_ac_type */ access; ///< The access type (read, write)
48
uint8_t /* cs_data_type */
49
dtypes[MAX_NO_DATA_TYPES]; ///< List of op types. Terminated by
50
///< CS_DATA_TYPE_LAST
51
} mapping_op;
52
53
#define MAX_NO_INSN_MAP_OPS 16
54
55
///< MCOperands of an instruction.
56
typedef struct {
57
mapping_op
58
ops[MAX_NO_INSN_MAP_OPS]; ///< NULL terminated array of insn_op.
59
} map_insn_ops;
60
61
/// Only usable by `auto-sync` archs!
62
const cs_op_type mapping_get_op_type(MCInst *MI, unsigned OpNum,
63
const map_insn_ops *insn_ops_map,
64
size_t map_size);
65
66
/// Only usable by `auto-sync` archs!
67
const cs_ac_type mapping_get_op_access(MCInst *MI, unsigned OpNum,
68
const map_insn_ops *insn_ops_map,
69
size_t map_size);
70
71
/// Macro for easier access of operand types from the map.
72
/// Assumes the istruction operands map is called "insn_operands"
73
/// Only usable by `auto-sync` archs!
74
#define map_get_op_type(MI, OpNum) \
75
mapping_get_op_type(MI, OpNum, (const map_insn_ops *)insn_operands, \
76
sizeof(insn_operands) / sizeof(insn_operands[0]))
77
78
/// Macro for easier access of operand access flags from the map.
79
/// Assumes the istruction operands map is called "insn_operands"
80
/// Only usable by `auto-sync` archs!
81
#define map_get_op_access(MI, OpNum) \
82
mapping_get_op_access(MI, OpNum, (const map_insn_ops *)insn_operands, \
83
sizeof(insn_operands) / \
84
sizeof(insn_operands[0]))
85
86
///< Map for ids to their string
87
typedef struct name_map {
88
unsigned int id;
89
const char *name;
90
} name_map;
91
92
// map a name to its ID
93
// return 0 if not found
94
int name2id(const name_map *map, int max, const char *name);
95
96
// map ID to a name
97
// return NULL if not found
98
const char *id2name(const name_map *map, int max, const unsigned int id);
99
100
void map_add_implicit_write(MCInst *MI, uint32_t Reg);
101
102
void map_implicit_reads(MCInst *MI, const insn_map *imap);
103
104
void map_implicit_writes(MCInst *MI, const insn_map *imap);
105
106
void map_groups(MCInst *MI, const insn_map *imap);
107
108
void map_cs_id(MCInst *MI, const insn_map *imap, unsigned int imap_size);
109
110
#define DECL_get_detail_op(arch, ARCH) \
111
cs_##arch##_op *ARCH##_get_detail_op(MCInst *MI, int offset);
112
113
DECL_get_detail_op(arm, ARM);
114
DECL_get_detail_op(ppc, PPC);
115
DECL_get_detail_op(tricore, TriCore);
116
117
/// Increments the detail->arch.op_count by one.
118
#define DEFINE_inc_detail_op_count(arch, ARCH) \
119
static inline void ARCH##_inc_op_count(MCInst *MI) \
120
{ \
121
MI->flat_insn->detail->arch.op_count++; \
122
}
123
124
/// Decrements the detail->arch.op_count by one.
125
#define DEFINE_dec_detail_op_count(arch, ARCH) \
126
static inline void ARCH##_dec_op_count(MCInst *MI) \
127
{ \
128
MI->flat_insn->detail->arch.op_count--; \
129
}
130
131
DEFINE_inc_detail_op_count(arm, ARM);
132
DEFINE_dec_detail_op_count(arm, ARM);
133
DEFINE_inc_detail_op_count(ppc, PPC);
134
DEFINE_dec_detail_op_count(ppc, PPC);
135
DEFINE_inc_detail_op_count(tricore, TriCore);
136
DEFINE_dec_detail_op_count(tricore, TriCore);
137
138
/// Returns true if a memory operand is currently edited.
139
static inline bool doing_mem(const MCInst *MI)
140
{
141
return MI->csh->doing_mem;
142
}
143
144
/// Sets the doing_mem flag to @status.
145
static inline void set_doing_mem(const MCInst *MI, bool status)
146
{
147
MI->csh->doing_mem = status;
148
}
149
150
/// Returns detail->arch
151
#define DEFINE_get_arch_detail(arch, ARCH) \
152
static inline cs_##arch *ARCH##_get_detail(const MCInst *MI) \
153
{ \
154
assert(MI && MI->flat_insn && MI->flat_insn->detail); \
155
return &MI->flat_insn->detail->arch; \
156
}
157
158
DEFINE_get_arch_detail(arm, ARM);
159
DEFINE_get_arch_detail(ppc, PPC);
160
DEFINE_get_arch_detail(tricore, TriCore);
161
162
static inline bool detail_is_set(const MCInst *MI)
163
{
164
assert(MI && MI->flat_insn);
165
return MI->flat_insn->detail != NULL;
166
}
167
168
static inline cs_detail *get_detail(const MCInst *MI)
169
{
170
assert(MI && MI->flat_insn);
171
return MI->flat_insn->detail;
172
}
173
174
#endif // CS_MAPPING_H
175
176