1 /* Capstone Disassembly Engine */
2 /* By Spike <spikeinhouse@gmail.com>, xwings 2019 */
3 
4 #ifndef CAPSTONE_WASM_H
5 #define CAPSTONE_WASM_H
6 
7 #ifdef __cplusplus
8 extern "C" {
9 #endif
10 
11 #include "platform.h"
12 
13 #ifdef _MSC_VER
14 #pragma warning(disable:4201)
15 #endif
16 
17 typedef enum wasm_op_type {
18 	WASM_OP_INVALID = 0,
19 	WASM_OP_NONE,
20 	WASM_OP_INT7,
21 	WASM_OP_VARUINT32,
22 	WASM_OP_VARUINT64,
23 	WASM_OP_UINT32,
24 	WASM_OP_UINT64,
25 	WASM_OP_IMM,
26 	WASM_OP_BRTABLE,
27 } wasm_op_type;
28 
29 typedef struct cs_wasm_brtable {
30 	uint32_t length;
31 	uint64_t address;
32 	uint32_t default_target;
33 } cs_wasm_brtable;
34 
35 typedef struct cs_wasm_op {
36 	wasm_op_type type;
37 	uint32_t size;
38 	union {
39 		int8_t int7;
40 		uint32_t varuint32;
41 		uint64_t varuint64;
42 		uint32_t uint32;
43 		uint64_t uint64;
44 		uint32_t immediate[2];
45 		cs_wasm_brtable brtable;
46 	};
47 } cs_wasm_op;
48 
49 /// Instruction structure
50 typedef struct cs_wasm {
51 	uint8_t op_count;
52 	cs_wasm_op operands[2];
53 } cs_wasm;
54 
55 /// WASM instruction
56 typedef enum wasm_insn {
57 	WASM_INS_UNREACHABLE = 0x0,
58 	WASM_INS_NOP = 0x1,
59 	WASM_INS_BLOCK = 0x2,
60 	WASM_INS_LOOP = 0x3,
61 	WASM_INS_IF = 0x4,
62 	WASM_INS_ELSE = 0x5,
63 	WASM_INS_END = 0xb,
64 	WASM_INS_BR = 0xc,
65 	WASM_INS_BR_IF = 0xd,
66 	WASM_INS_BR_TABLE = 0xe,
67 	WASM_INS_RETURN = 0xf,
68 	WASM_INS_CALL = 0x10,
69 	WASM_INS_CALL_INDIRECT = 0x11,
70 	WASM_INS_DROP = 0x1a,
71 	WASM_INS_SELECT = 0x1b,
72 	WASM_INS_GET_LOCAL = 0x20,
73 	WASM_INS_SET_LOCAL = 0x21,
74 	WASM_INS_TEE_LOCAL = 0x22,
75 	WASM_INS_GET_GLOBAL = 0x23,
76 	WASM_INS_SET_GLOBAL = 0x24,
77 	WASM_INS_I32_LOAD = 0x28,
78 	WASM_INS_I64_LOAD = 0x29,
79 	WASM_INS_F32_LOAD = 0x2a,
80 	WASM_INS_F64_LOAD = 0x2b,
81 	WASM_INS_I32_LOAD8_S = 0x2c,
82 	WASM_INS_I32_LOAD8_U = 0x2d,
83 	WASM_INS_I32_LOAD16_S = 0x2e,
84 	WASM_INS_I32_LOAD16_U = 0x2f,
85 	WASM_INS_I64_LOAD8_S = 0x30,
86 	WASM_INS_I64_LOAD8_U = 0x31,
87 	WASM_INS_I64_LOAD16_S = 0x32,
88 	WASM_INS_I64_LOAD16_U = 0x33,
89 	WASM_INS_I64_LOAD32_S = 0x34,
90 	WASM_INS_I64_LOAD32_U = 0x35,
91 	WASM_INS_I32_STORE = 0x36,
92 	WASM_INS_I64_STORE = 0x37,
93 	WASM_INS_F32_STORE = 0x38,
94 	WASM_INS_F64_STORE = 0x39,
95 	WASM_INS_I32_STORE8 = 0x3a,
96 	WASM_INS_I32_STORE16 = 0x3b,
97 	WASM_INS_I64_STORE8 = 0x3c,
98 	WASM_INS_I64_STORE16 = 0x3d,
99 	WASM_INS_I64_STORE32 = 0x3e,
100 	WASM_INS_CURRENT_MEMORY = 0x3f,
101 	WASM_INS_GROW_MEMORY = 0x40,
102 	WASM_INS_I32_CONST = 0x41,
103 	WASM_INS_I64_CONST = 0x42,
104 	WASM_INS_F32_CONST = 0x43,
105 	WASM_INS_F64_CONST = 0x44,
106 	WASM_INS_I32_EQZ = 0x45,
107 	WASM_INS_I32_EQ = 0x46,
108 	WASM_INS_I32_NE = 0x47,
109 	WASM_INS_I32_LT_S = 0x48,
110 	WASM_INS_I32_LT_U = 0x49,
111 	WASM_INS_I32_GT_S = 0x4a,
112 	WASM_INS_I32_GT_U = 0x4b,
113 	WASM_INS_I32_LE_S = 0x4c,
114 	WASM_INS_I32_LE_U = 0x4d,
115 	WASM_INS_I32_GE_S = 0x4e,
116 	WASM_INS_I32_GE_U = 0x4f,
117 	WASM_INS_I64_EQZ = 0x50,
118 	WASM_INS_I64_EQ = 0x51,
119 	WASM_INS_I64_NE = 0x52,
120 	WASM_INS_I64_LT_S = 0x53,
121 	WASM_INS_I64_LT_U = 0x54,
122 	WASN_INS_I64_GT_S = 0x55,
123 	WASM_INS_I64_GT_U = 0x56,
124 	WASM_INS_I64_LE_S = 0x57,
125 	WASM_INS_I64_LE_U = 0x58,
126 	WASM_INS_I64_GE_S = 0x59,
127 	WASM_INS_I64_GE_U = 0x5a,
128 	WASM_INS_F32_EQ = 0x5b,
129 	WASM_INS_F32_NE = 0x5c,
130 	WASM_INS_F32_LT = 0x5d,
131 	WASM_INS_F32_GT = 0x5e,
132 	WASM_INS_F32_LE = 0x5f,
133 	WASM_INS_F32_GE = 0x60,
134 	WASM_INS_F64_EQ = 0x61,
135 	WASM_INS_F64_NE = 0x62,
136 	WASM_INS_F64_LT = 0x63,
137 	WASM_INS_F64_GT = 0x64,
138 	WASM_INS_F64_LE = 0x65,
139 	WASM_INS_F64_GE = 0x66,
140 	WASM_INS_I32_CLZ = 0x67,
141 	WASM_INS_I32_CTZ = 0x68,
142 	WASM_INS_I32_POPCNT = 0x69,
143 	WASM_INS_I32_ADD = 0x6a,
144 	WASM_INS_I32_SUB = 0x6b,
145 	WASM_INS_I32_MUL = 0x6c,
146 	WASM_INS_I32_DIV_S = 0x6d,
147 	WASM_INS_I32_DIV_U = 0x6e,
148 	WASM_INS_I32_REM_S = 0x6f,
149 	WASM_INS_I32_REM_U = 0x70,
150 	WASM_INS_I32_AND = 0x71,
151 	WASM_INS_I32_OR = 0x72,
152 	WASM_INS_I32_XOR = 0x73,
153 	WASM_INS_I32_SHL = 0x74,
154 	WASM_INS_I32_SHR_S = 0x75,
155 	WASM_INS_I32_SHR_U = 0x76,
156 	WASM_INS_I32_ROTL = 0x77,
157 	WASM_INS_I32_ROTR = 0x78,
158 	WASM_INS_I64_CLZ = 0x79,
159 	WASM_INS_I64_CTZ = 0x7a,
160 	WASM_INS_I64_POPCNT = 0x7b,
161 	WASM_INS_I64_ADD = 0x7c,
162 	WASM_INS_I64_SUB = 0x7d,
163 	WASM_INS_I64_MUL = 0x7e,
164 	WASM_INS_I64_DIV_S = 0x7f,
165 	WASM_INS_I64_DIV_U = 0x80,
166 	WASM_INS_I64_REM_S = 0x81,
167 	WASM_INS_I64_REM_U = 0x82,
168 	WASM_INS_I64_AND = 0x83,
169 	WASM_INS_I64_OR = 0x84,
170 	WASM_INS_I64_XOR = 0x85,
171 	WASM_INS_I64_SHL = 0x86,
172 	WASM_INS_I64_SHR_S = 0x87,
173 	WASM_INS_I64_SHR_U = 0x88,
174 	WASM_INS_I64_ROTL = 0x89,
175 	WASM_INS_I64_ROTR = 0x8a,
176 	WASM_INS_F32_ABS = 0x8b,
177 	WASM_INS_F32_NEG = 0x8c,
178 	WASM_INS_F32_CEIL = 0x8d,
179 	WASM_INS_F32_FLOOR = 0x8e,
180 	WASM_INS_F32_TRUNC = 0x8f,
181 	WASM_INS_F32_NEAREST = 0x90,
182 	WASM_INS_F32_SQRT = 0x91,
183 	WASM_INS_F32_ADD = 0x92,
184 	WASM_INS_F32_SUB = 0x93,
185 	WASM_INS_F32_MUL = 0x94,
186 	WASM_INS_F32_DIV = 0x95,
187 	WASM_INS_F32_MIN = 0x96,
188 	WASM_INS_F32_MAX = 0x97,
189 	WASM_INS_F32_COPYSIGN = 0x98,
190 	WASM_INS_F64_ABS = 0x99,
191 	WASM_INS_F64_NEG = 0x9a,
192 	WASM_INS_F64_CEIL = 0x9b,
193 	WASM_INS_F64_FLOOR = 0x9c,
194 	WASM_INS_F64_TRUNC = 0x9d,
195 	WASM_INS_F64_NEAREST = 0x9e,
196 	WASM_INS_F64_SQRT = 0x9f,
197 	WASM_INS_F64_ADD = 0xa0,
198 	WASM_INS_F64_SUB = 0xa1,
199 	WASM_INS_F64_MUL = 0xa2,
200 	WASM_INS_F64_DIV = 0xa3,
201 	WASM_INS_F64_MIN = 0xa4,
202 	WASM_INS_F64_MAX = 0xa5,
203 	WASM_INS_F64_COPYSIGN = 0xa6,
204 	WASM_INS_I32_WARP_I64 = 0xa7,
205 	WASP_INS_I32_TRUNC_S_F32 = 0xa8,
206 	WASM_INS_I32_TRUNC_U_F32 = 0xa9,
207 	WASM_INS_I32_TRUNC_S_F64 = 0xaa,
208 	WASM_INS_I32_TRUNC_U_F64 = 0xab,
209 	WASM_INS_I64_EXTEND_S_I32 = 0xac,
210 	WASM_INS_I64_EXTEND_U_I32 = 0xad,
211 	WASM_INS_I64_TRUNC_S_F32 = 0xae,
212 	WASM_INS_I64_TRUNC_U_F32 = 0xaf,
213 	WASM_INS_I64_TRUNC_S_F64 = 0xb0,
214 	WASM_INS_I64_TRUNC_U_F64 = 0xb1,
215 	WASM_INS_F32_CONVERT_S_I32 = 0xb2,
216 	WASM_INS_F32_CONVERT_U_I32 = 0xb3,
217 	WASM_INS_F32_CONVERT_S_I64 = 0xb4,
218 	WASM_INS_F32_CONVERT_U_I64 = 0xb5,
219 	WASM_INS_F32_DEMOTE_F64 = 0xb6,
220 	WASM_INS_F64_CONVERT_S_I32 = 0xb7,
221 	WASM_INS_F64_CONVERT_U_I32 = 0xb8,
222 	WASM_INS_F64_CONVERT_S_I64 = 0xb9,
223 	WASM_INS_F64_CONVERT_U_I64 = 0xba,
224 	WASM_INS_F64_PROMOTE_F32 = 0xbb,
225 	WASM_INS_I32_REINTERPRET_F32 = 0xbc,
226 	WASM_INS_I64_REINTERPRET_F64 = 0xbd,
227 	WASM_INS_F32_REINTERPRET_I32 = 0xbe,
228 	WASM_INS_F64_REINTERPRET_I64 = 0xbf,
229 	WASM_INS_INVALID = 512,
230 	WASM_INS_ENDING,
231 } wasm_insn;
232 
233 /// Group of WASM instructions
234 typedef enum wasm_insn_group {
235 	WASM_GRP_INVALID = 0, ///< = CS_GRP_INVALID
236 
237 	WASM_GRP_NUMBERIC = 8,
238 	WASM_GRP_PARAMETRIC,
239 	WASM_GRP_VARIABLE,
240 	WASM_GRP_MEMORY,
241 	WASM_GRP_CONTROL,
242 
243 	WASM_GRP_ENDING,   ///< <-- mark the end of the list of groups
244 } wasm_insn_group;
245 
246 #ifdef __cplusplus
247 }
248 #endif
249 
250 #endif
251