| 1 |
/* |
| 2 |
* FCML - Free Code Manipulation Library. |
| 3 |
* Copyright (C) 2010-2024 Slawomir Wojtasiak |
| 4 |
* |
| 5 |
* This library is free software; you can redistribute it and/or |
| 6 |
* modify it under the terms of the GNU Lesser General Public |
| 7 |
* License as published by the Free Software Foundation; either |
| 8 |
* version 2.1 of the License, or (at your option) any later version. |
| 9 |
* |
| 10 |
* This library is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 |
* Lesser General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU Lesser General Public |
| 16 |
* License along with this library; if not, write to the Free Software |
| 17 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 |
*/ |
| 19 |
|
| 20 |
/** @file fcml_common.h |
| 21 |
* Definitions of common structures used by FCML components. |
| 22 |
* @copyright Copyright (C) 2010-2020 Slawomir Wojtasiak. All rights reserved. |
| 23 |
* This project is released under the GNU Lesser General Public License. |
| 24 |
*/ |
| 25 |
|
| 26 |
#ifndef FCML_INT_COMMON_H_ |
| 27 |
#define FCML_INT_COMMON_H_ |
| 28 |
|
| 29 |
#include "fcml_lib_export.h" |
| 30 |
|
| 31 |
#include "fcml_types.h" |
| 32 |
#include "fcml_instructions.h" |
| 33 |
|
| 34 |
/** Maximal number of the instruction operands. */ |
| 35 |
#define FCML_OPERANDS_COUNT 5 |
| 36 |
/** Maximal number of bytes instruction can use. */ |
| 37 |
#define FCML_INSTRUCTION_SIZE 15 |
| 38 |
/** Number of opcode bytes. */ |
| 39 |
#define FCML_OPCODES_NUM 3 |
| 40 |
|
| 41 |
/** |
| 42 |
* @defgroup PREFIX_GROUP Explicit prefixes |
| 43 |
* List of prefixes that can be explicitly defined for instruction. |
| 44 |
* @{ |
| 45 |
*/ |
| 46 |
|
| 47 |
/** LOCK prefix (0xF0)*/ |
| 48 |
#define FCML_PREFIX_LOCK 0x0001 |
| 49 |
/** REPNE prefix (0xF2) */ |
| 50 |
#define FCML_PREFIX_REPNE 0x0002 |
| 51 |
/** REPNZ prefix (0xF2) */ |
| 52 |
#define FCML_PREFIX_REPNZ FCML_PREFIX_REPNE |
| 53 |
/** REP prefix (0xF3) */ |
| 54 |
#define FCML_PREFIX_REP 0x0004 |
| 55 |
/** REPE prefix (0xF3) */ |
| 56 |
#define FCML_PREFIX_REPE FCML_PREFIX_REP |
| 57 |
/** REPZ prefix (0xF3) */ |
| 58 |
#define FCML_PREFIX_REPZ FCML_PREFIX_REP |
| 59 |
/** XACQUIRE prefix (0xF2) */ |
| 60 |
#define FCML_PREFIX_XACQUIRE 0x0008 |
| 61 |
/** XRELEASE prefix (0xF3) */ |
| 62 |
#define FCML_PREFIX_XRELEASE 0x0010 |
| 63 |
/** branch hint (0x2E) (SSE2 extension)*/ |
| 64 |
#define FCML_PREFIX_BRANCH_HINT 0x0020 |
| 65 |
/** nobranch hint (0x3E) (SSE2 extension)*/ |
| 66 |
#define FCML_PREFIX_NOBRANCH_HINT 0x0040 |
| 67 |
|
| 68 |
/** @} */ |
| 69 |
|
| 70 |
/** |
| 71 |
* Supported processor operating modes. |
| 72 |
*/ |
| 73 |
typedef enum fcml_en_operating_mode { |
| 74 |
/** Real-addressing mode, virtual 8086 mode. */ |
| 75 |
FCML_OM_16_BIT = 1, |
| 76 |
/** Protected/Compatibility mode when 'D' segment descriptor |
| 77 |
* flag is set to 1. */ |
| 78 |
FCML_OM_32_BIT, |
| 79 |
/** 64-bit mode. ('L' flag of segment descriptor set to 1.) */ |
| 80 |
FCML_OM_64_BIT, |
| 81 |
} fcml_en_operating_mode; |
| 82 |
|
| 83 |
/* Simple types. */ |
| 84 |
|
| 85 |
/** Type used for storing instruction and operand hint masks. */ |
| 86 |
typedef fcml_uint16_t fcml_hints; |
| 87 |
|
| 88 |
/** |
| 89 |
* Type for explicit instruction prefixes bit mask. |
| 90 |
*/ |
| 91 |
typedef fcml_uint16_t fcml_prefixes; |
| 92 |
|
| 93 |
/** |
| 94 |
* General instruction pointer holder. |
| 95 |
*/ |
| 96 |
typedef fcml_int64_t fcml_ip; |
| 97 |
|
| 98 |
/* Register numbers.*/ |
| 99 |
|
| 100 |
/** |
| 101 |
* @defgroup REGISTERS_GROUP Registers. |
| 102 |
* All supported x64_64 registers are defined here. |
| 103 |
* @{ |
| 104 |
*/ |
| 105 |
|
| 106 |
#define FCML_REG_AL 0 |
| 107 |
#define FCML_REG_AX 0 |
| 108 |
#define FCML_REG_EAX 0 |
| 109 |
#define FCML_REG_RAX 0 |
| 110 |
#define FCML_REG_MM0 0 |
| 111 |
#define FCML_REG_XMM0 0 |
| 112 |
#define FCML_REG_YMM0 0 |
| 113 |
#define FCML_REG_ZMM0 0 |
| 114 |
|
| 115 |
#define FCML_REG_CL 1 |
| 116 |
#define FCML_REG_CX 1 |
| 117 |
#define FCML_REG_ECX 1 |
| 118 |
#define FCML_REG_RCX 1 |
| 119 |
#define FCML_REG_MM1 1 |
| 120 |
#define FCML_REG_XMM1 1 |
| 121 |
#define FCML_REG_YMM1 1 |
| 122 |
#define FCML_REG_ZMM1 1 |
| 123 |
|
| 124 |
#define FCML_REG_DL 2 |
| 125 |
#define FCML_REG_DX 2 |
| 126 |
#define FCML_REG_EDX 2 |
| 127 |
#define FCML_REG_RDX 2 |
| 128 |
#define FCML_REG_MM2 2 |
| 129 |
#define FCML_REG_XMM2 2 |
| 130 |
#define FCML_REG_YMM2 2 |
| 131 |
#define FCML_REG_ZMM2 2 |
| 132 |
|
| 133 |
#define FCML_REG_BL 3 |
| 134 |
#define FCML_REG_BX 3 |
| 135 |
#define FCML_REG_EBX 3 |
| 136 |
#define FCML_REG_RBX 3 |
| 137 |
#define FCML_REG_MM3 3 |
| 138 |
#define FCML_REG_XMM3 3 |
| 139 |
#define FCML_REG_YMM3 3 |
| 140 |
#define FCML_REG_ZMM3 3 |
| 141 |
|
| 142 |
#define FCML_REG_AH 4 |
| 143 |
#define FCML_REG_SP 4 |
| 144 |
#define FCML_REG_SPL 4 |
| 145 |
#define FCML_REG_ESP 4 |
| 146 |
#define FCML_REG_RSP 4 |
| 147 |
#define FCML_REG_MM4 4 |
| 148 |
#define FCML_REG_XMM4 4 |
| 149 |
#define FCML_REG_YMM4 4 |
| 150 |
#define FCML_REG_ZMM4 4 |
| 151 |
|
| 152 |
#define FCML_REG_CH 5 |
| 153 |
#define FCML_REG_BP 5 |
| 154 |
#define FCML_REG_BPL 5 |
| 155 |
#define FCML_REG_EBP 5 |
| 156 |
#define FCML_REG_RBP 5 |
| 157 |
#define FCML_REG_MM5 5 |
| 158 |
#define FCML_REG_XMM5 5 |
| 159 |
#define FCML_REG_YMM5 5 |
| 160 |
#define FCML_REG_ZMM5 5 |
| 161 |
|
| 162 |
#define FCML_REG_DH 6 |
| 163 |
#define FCML_REG_SI 6 |
| 164 |
#define FCML_REG_SIL 6 |
| 165 |
#define FCML_REG_ESI 6 |
| 166 |
#define FCML_REG_RSI 6 |
| 167 |
#define FCML_REG_MM6 6 |
| 168 |
#define FCML_REG_XMM6 6 |
| 169 |
#define FCML_REG_YMM6 6 |
| 170 |
#define FCML_REG_ZMM6 6 |
| 171 |
|
| 172 |
#define FCML_REG_BH 7 |
| 173 |
#define FCML_REG_DI 7 |
| 174 |
#define FCML_REG_DIL 7 |
| 175 |
#define FCML_REG_EDI 7 |
| 176 |
#define FCML_REG_RDI 7 |
| 177 |
#define FCML_REG_MM7 7 |
| 178 |
#define FCML_REG_XMM7 7 |
| 179 |
#define FCML_REG_YMM7 7 |
| 180 |
#define FCML_REG_ZMM7 7 |
| 181 |
|
| 182 |
#define FCML_REG_R8L 8 |
| 183 |
#define FCML_REG_R8W 8 |
| 184 |
#define FCML_REG_R8D 8 |
| 185 |
#define FCML_REG_R8 8 |
| 186 |
#define FCML_REG_XMM8 8 |
| 187 |
#define FCML_REG_YMM8 8 |
| 188 |
#define FCML_REG_ZMM8 8 |
| 189 |
|
| 190 |
#define FCML_REG_R9L 9 |
| 191 |
#define FCML_REG_R9W 9 |
| 192 |
#define FCML_REG_R9D 9 |
| 193 |
#define FCML_REG_R9 9 |
| 194 |
#define FCML_REG_XMM9 9 |
| 195 |
#define FCML_REG_YMM9 9 |
| 196 |
#define FCML_REG_ZMM9 9 |
| 197 |
|
| 198 |
#define FCML_REG_R10L 10 |
| 199 |
#define FCML_REG_R10W 10 |
| 200 |
#define FCML_REG_R10D 10 |
| 201 |
#define FCML_REG_R10 10 |
| 202 |
#define FCML_REG_XMM10 10 |
| 203 |
#define FCML_REG_YMM10 10 |
| 204 |
#define FCML_REG_ZMM10 10 |
| 205 |
|
| 206 |
#define FCML_REG_R11L 11 |
| 207 |
#define FCML_REG_R11W 11 |
| 208 |
#define FCML_REG_R11D 11 |
| 209 |
#define FCML_REG_R11 11 |
| 210 |
#define FCML_REG_XMM11 11 |
| 211 |
#define FCML_REG_YMM11 11 |
| 212 |
#define FCML_REG_ZMM11 11 |
| 213 |
|
| 214 |
#define FCML_REG_R12L 12 |
| 215 |
#define FCML_REG_R12W 12 |
| 216 |
#define FCML_REG_R12D 12 |
| 217 |
#define FCML_REG_R12 12 |
| 218 |
#define FCML_REG_XMM12 12 |
| 219 |
#define FCML_REG_YMM12 12 |
| 220 |
#define FCML_REG_ZMM12 12 |
| 221 |
|
| 222 |
#define FCML_REG_R13L 13 |
| 223 |
#define FCML_REG_R13W 13 |
| 224 |
#define FCML_REG_R13D 13 |
| 225 |
#define FCML_REG_R13 13 |
| 226 |
#define FCML_REG_XMM13 13 |
| 227 |
#define FCML_REG_YMM13 13 |
| 228 |
#define FCML_REG_ZMM13 13 |
| 229 |
|
| 230 |
#define FCML_REG_R14L 14 |
| 231 |
#define FCML_REG_R14W 14 |
| 232 |
#define FCML_REG_R14D 14 |
| 233 |
#define FCML_REG_R14 14 |
| 234 |
#define FCML_REG_XMM14 14 |
| 235 |
#define FCML_REG_YMM14 14 |
| 236 |
#define FCML_REG_ZMM14 14 |
| 237 |
|
| 238 |
#define FCML_REG_R15L 15 |
| 239 |
#define FCML_REG_R15W 15 |
| 240 |
#define FCML_REG_R15D 15 |
| 241 |
#define FCML_REG_R15 15 |
| 242 |
#define FCML_REG_XMM15 15 |
| 243 |
#define FCML_REG_YMM15 15 |
| 244 |
#define FCML_REG_ZMM15 15 |
| 245 |
|
| 246 |
#define FCML_REG_XMM16 16 |
| 247 |
#define FCML_REG_YMM16 16 |
| 248 |
#define FCML_REG_ZMM16 16 |
| 249 |
|
| 250 |
#define FCML_REG_XMM17 17 |
| 251 |
#define FCML_REG_YMM17 17 |
| 252 |
#define FCML_REG_ZMM17 17 |
| 253 |
|
| 254 |
#define FCML_REG_XMM18 18 |
| 255 |
#define FCML_REG_YMM18 18 |
| 256 |
#define FCML_REG_ZMM18 18 |
| 257 |
|
| 258 |
#define FCML_REG_XMM19 19 |
| 259 |
#define FCML_REG_YMM19 19 |
| 260 |
#define FCML_REG_ZMM19 19 |
| 261 |
|
| 262 |
#define FCML_REG_XMM20 20 |
| 263 |
#define FCML_REG_YMM20 20 |
| 264 |
#define FCML_REG_ZMM20 20 |
| 265 |
|
| 266 |
#define FCML_REG_XMM21 21 |
| 267 |
#define FCML_REG_YMM21 21 |
| 268 |
#define FCML_REG_ZMM21 21 |
| 269 |
|
| 270 |
#define FCML_REG_XMM22 22 |
| 271 |
#define FCML_REG_YMM22 22 |
| 272 |
#define FCML_REG_ZMM22 22 |
| 273 |
|
| 274 |
#define FCML_REG_XMM23 23 |
| 275 |
#define FCML_REG_YMM23 23 |
| 276 |
#define FCML_REG_ZMM23 23 |
| 277 |
|
| 278 |
#define FCML_REG_XMM24 24 |
| 279 |
#define FCML_REG_YMM24 24 |
| 280 |
#define FCML_REG_ZMM24 24 |
| 281 |
|
| 282 |
#define FCML_REG_XMM25 25 |
| 283 |
#define FCML_REG_YMM25 25 |
| 284 |
#define FCML_REG_ZMM25 25 |
| 285 |
|
| 286 |
#define FCML_REG_XMM26 26 |
| 287 |
#define FCML_REG_YMM26 26 |
| 288 |
#define FCML_REG_ZMM26 26 |
| 289 |
|
| 290 |
#define FCML_REG_XMM27 27 |
| 291 |
#define FCML_REG_YMM27 27 |
| 292 |
#define FCML_REG_ZMM27 27 |
| 293 |
|
| 294 |
#define FCML_REG_XMM28 28 |
| 295 |
#define FCML_REG_YMM28 28 |
| 296 |
#define FCML_REG_ZMM28 28 |
| 297 |
|
| 298 |
#define FCML_REG_XMM29 29 |
| 299 |
#define FCML_REG_YMM29 29 |
| 300 |
#define FCML_REG_ZMM29 29 |
| 301 |
|
| 302 |
#define FCML_REG_XMM30 30 |
| 303 |
#define FCML_REG_YMM30 30 |
| 304 |
#define FCML_REG_ZMM30 30 |
| 305 |
|
| 306 |
#define FCML_REG_XMM31 31 |
| 307 |
#define FCML_REG_YMM31 31 |
| 308 |
#define FCML_REG_ZMM31 31 |
| 309 |
|
| 310 |
/* Segment registers. */ |
| 311 |
|
| 312 |
#define FCML_REG_ES 0 |
| 313 |
#define FCML_REG_CS 1 |
| 314 |
#define FCML_REG_SS 2 |
| 315 |
#define FCML_REG_DS 3 |
| 316 |
#define FCML_REG_FS 4 |
| 317 |
#define FCML_REG_GS 5 |
| 318 |
|
| 319 |
/* FPU registers */ |
| 320 |
|
| 321 |
#define FCML_REG_ST0 0 |
| 322 |
#define FCML_REG_ST1 1 |
| 323 |
#define FCML_REG_ST2 2 |
| 324 |
#define FCML_REG_ST3 3 |
| 325 |
#define FCML_REG_ST4 4 |
| 326 |
#define FCML_REG_ST5 5 |
| 327 |
#define FCML_REG_ST6 6 |
| 328 |
#define FCML_REG_ST7 7 |
| 329 |
|
| 330 |
/* Control registers. */ |
| 331 |
|
| 332 |
#define FCML_REG_CR0 0 |
| 333 |
#define FCML_REG_CR2 2 |
| 334 |
#define FCML_REG_CR3 3 |
| 335 |
#define FCML_REG_CR4 4 |
| 336 |
#define FCML_REG_CR8 8 |
| 337 |
|
| 338 |
/* Debug register. */ |
| 339 |
|
| 340 |
#define FCML_REG_DR0 0 |
| 341 |
#define FCML_REG_DR1 1 |
| 342 |
#define FCML_REG_DR2 2 |
| 343 |
#define FCML_REG_DR3 3 |
| 344 |
#define FCML_REG_DR4 4 |
| 345 |
#define FCML_REG_DR5 5 |
| 346 |
#define FCML_REG_DR6 6 |
| 347 |
#define FCML_REG_DR7 7 |
| 348 |
|
| 349 |
/* Opmask registers. */ |
| 350 |
|
| 351 |
#define FCML_REG_K0 0 |
| 352 |
#define FCML_REG_K1 1 |
| 353 |
#define FCML_REG_K2 2 |
| 354 |
#define FCML_REG_K3 3 |
| 355 |
#define FCML_REG_K4 4 |
| 356 |
#define FCML_REG_K5 5 |
| 357 |
#define FCML_REG_K6 6 |
| 358 |
#define FCML_REG_K7 7 |
| 359 |
|
| 360 |
/** @} */ |
| 361 |
|
| 362 |
/** |
| 363 |
* @defgroup DATA_SIZE_GROUP Constants used to describe data size. |
| 364 |
* @{instrunction->hints |
| 365 |
*/ |
| 366 |
|
| 367 |
#define FCML_DS_UNDEF 0 |
| 368 |
#define FCML_DS_8 8 |
| 369 |
#define FCML_DS_16 16 |
| 370 |
#define FCML_DS_32 32 |
| 371 |
#define FCML_DS_64 64 |
| 372 |
#define FCML_DS_128 128 |
| 373 |
#define FCML_DS_256 256 |
| 374 |
#define FCML_DS_512 512 |
| 375 |
|
| 376 |
/** @} */ |
| 377 |
|
| 378 |
/* Size operators. */ |
| 379 |
|
| 380 |
#define FCML_OS_UNDEFINED 0 |
| 381 |
#define FCML_OS_BYTE 8 |
| 382 |
#define FCML_OS_WORD 16 |
| 383 |
#define FCML_OS_DWORD 32 |
| 384 |
#define FCML_OS_FWORD 48 |
| 385 |
#define FCML_OS_QWORD 64 |
| 386 |
#define FCML_OS_MWORD 64 |
| 387 |
#define FCML_OS_TBYTE 80 |
| 388 |
#define FCML_OS_OWORD 128 |
| 389 |
#define FCML_OS_XWORD 128 |
| 390 |
#define FCML_OS_YWORD 256 |
| 391 |
#define FCML_OS_ZWORD 512 |
| 392 |
|
| 393 |
/* SIMD tuple types. */ |
| 394 |
|
| 395 |
/* Instructions affected by embedded broadcast */ |
| 396 |
#define FCML_TT_NONE 0 |
| 397 |
#define FCML_TT_FV 1 |
| 398 |
#define FCML_TT_HV 2 |
| 399 |
|
| 400 |
/* Instructions not affected by embedded broadcast */ |
| 401 |
#define FCML_TT_FVM 3 /* Full Mem */ |
| 402 |
#define FCML_TT_T1S 4 |
| 403 |
#define FCML_TT_T1F 5 |
| 404 |
#define FCML_TT_T2 6 |
| 405 |
#define FCML_TT_T4 7 |
| 406 |
#define FCML_TT_T8 8 |
| 407 |
#define FCML_TT_HVM 9 |
| 408 |
#define FCML_TT_QVM 10 |
| 409 |
#define FCML_TT_OVM 11 |
| 410 |
#define FCML_TT_M128 12 |
| 411 |
#define FCML_TT_DUP 13 |
| 412 |
#define FCML_TT_T1x4 14 /* Tuple1_4X */ |
| 413 |
|
| 414 |
/* Embedded rounding mode. */ |
| 415 |
|
| 416 |
#define FCML_ER_RN_SAE 0 |
| 417 |
#define FCML_ER_RD_SAE 1 |
| 418 |
#define FCML_ER_RU_SAE 2 |
| 419 |
#define FCML_ER_RZ_SAE 3 |
| 420 |
|
| 421 |
/** |
| 422 |
* Register type. |
| 423 |
* Every register is represented as an integer value and it's |
| 424 |
* register type. This enumeration provides all supported register types. |
| 425 |
*/ |
| 426 |
typedef enum fcml_en_register { |
| 427 |
/** Undefined register type. */ |
| 428 |
FCML_REG_UNDEFINED = 0, |
| 429 |
/** General purpose register. */ |
| 430 |
FCML_REG_GPR, |
| 431 |
/** SIMD (SSE, MMX) register. */ |
| 432 |
FCML_REG_SIMD, |
| 433 |
/** FPU register. */ |
| 434 |
FCML_REG_FPU, |
| 435 |
/** Segment register */ |
| 436 |
FCML_REG_SEG, |
| 437 |
/** Control register. */ |
| 438 |
FCML_REG_CR, |
| 439 |
/** Debug register */ |
| 440 |
FCML_REG_DR, |
| 441 |
/** Instruction pointer register. Used for relative RIP addressing. */ |
| 442 |
FCML_REG_IP, |
| 443 |
/** Operand mask register. */ |
| 444 |
FCML_REG_OPMASK |
| 445 |
} fcml_en_register; |
| 446 |
|
| 447 |
/** |
| 448 |
* Structure describes x86_64 register. |
| 449 |
*/ |
| 450 |
typedef struct fcml_st_register { |
| 451 |
/** Register type. */ |
| 452 |
fcml_en_register type; |
| 453 |
/** Register size in bits. */ |
| 454 |
fcml_usize size; |
| 455 |
/** Register itself as a positive integer. @see REGISTERS_GROUP */ |
| 456 |
fcml_uint8_t reg; |
| 457 |
/** In case of SPL,BPL,SIL,DIL GPR registers has to be set to true. */ |
| 458 |
fcml_bool x64_exp; |
| 459 |
} fcml_st_register; |
| 460 |
|
| 461 |
/********************************* |
| 462 |
* Conditions. |
| 463 |
*********************************/ |
| 464 |
|
| 465 |
/** Number of supported condition types. */ |
| 466 |
#define FCML_NUMBER_OF_CONDITIONS 8 |
| 467 |
|
| 468 |
/** |
| 469 |
* Condition type. |
| 470 |
* Every conditional instruction has an appropriate condition type set. |
| 471 |
* Following enumeration defines all supported types. |
| 472 |
*/ |
| 473 |
typedef enum fcml_en_condition_type { |
| 474 |
/** 0 Overflow*/ |
| 475 |
FCML_CONDITION_O = 0, |
| 476 |
/** 1 Below*/ |
| 477 |
FCML_CONDITION_B, |
| 478 |
/** 2 Equal*/ |
| 479 |
FCML_CONDITION_E, |
| 480 |
/** 3 Below or equal*/ |
| 481 |
FCML_CONDITION_BE, |
| 482 |
/** 4 Sign*/ |
| 483 |
FCML_CONDITION_S, |
| 484 |
/** 5 Parity*/ |
| 485 |
FCML_CONDITION_P, |
| 486 |
/** 6 Less than*/ |
| 487 |
FCML_CONDITION_L, |
| 488 |
/** 7 Less than or equal to*/ |
| 489 |
FCML_CONDITION_LE |
| 490 |
} fcml_en_condition_type; |
| 491 |
|
| 492 |
/** |
| 493 |
* Defines instruction's condition. |
| 494 |
*/ |
| 495 |
typedef struct fcml_st_condition { |
| 496 |
/* Condition type.*/ |
| 497 |
fcml_en_condition_type condition_type; |
| 498 |
/* True if condition should be negated.*/ |
| 499 |
fcml_bool is_negation; |
| 500 |
} fcml_st_condition; |
| 501 |
|
| 502 |
/********************************* |
| 503 |
* Size attributes flags. |
| 504 |
*********************************/ |
| 505 |
|
| 506 |
/** |
| 507 |
* @defgroup SUPPORTED_SIZE_GROUP These values can be used in order to |
| 508 |
* prepare a mask of supported sizes. Used mainly by optimizers where |
| 509 |
* instructions can be assembled using different attribute sizes. |
| 510 |
* @{ |
| 511 |
*/ |
| 512 |
|
| 513 |
/* Addressing mode is not set, so the caller does not care about it - any |
| 514 |
* addressing mode is allowed. */ |
| 515 |
#define FCML_EN_ASF_ANY 0x00 |
| 516 |
#define FCML_EN_ASF_16 0x01 |
| 517 |
#define FCML_EN_ASF_32 0x02 |
| 518 |
#define FCML_EN_ASF_64 0x04 |
| 519 |
/* All addressing modes are allowed, but in this particular case it's explicitly stated. |
| 520 |
* Effectively it's the same option as FCML_EN_ASF_ANY. */ |
| 521 |
#define FCML_EN_ASF_ALL FCML_EN_ASF_16 | FCML_EN_ASF_32 | FCML_EN_ASF_64 |
| 522 |
|
| 523 |
/** @} */ |
| 524 |
|
| 525 |
/** |
| 526 |
* Nullable wrapper for mask of size flags. |
| 527 |
*/ |
| 528 |
typedef struct fcml_st_nullable_size_flags { |
| 529 |
/** True if mask is set. */ |
| 530 |
fcml_bool is_set; |
| 531 |
/** Mask of supported size values. */ |
| 532 |
fcml_flags flags; |
| 533 |
} fcml_st_nullable_size_flags; |
| 534 |
|
| 535 |
/********************************* |
| 536 |
* Operands. |
| 537 |
*********************************/ |
| 538 |
|
| 539 |
/** |
| 540 |
* Operand access mode. |
| 541 |
*/ |
| 542 |
typedef enum fcml_en_access_mode { |
| 543 |
/** Undefined mode. */ |
| 544 |
FCML_AM_ACCESS_MODE_UNDEFINED = 0, |
| 545 |
/** Operand is read by instruction. */ |
| 546 |
FCML_AM_READ = 0x01, |
| 547 |
/** Operand is set by instruction */ |
| 548 |
FCML_AM_WRITE = 0x02, |
| 549 |
/** Operand is read but can be also set. */ |
| 550 |
FCML_AM_READ_WRITE = FCML_AM_READ | FCML_AM_WRITE |
| 551 |
} fcml_en_access_mode; |
| 552 |
|
| 553 |
/** |
| 554 |
* Representation of far pointer operand. |
| 555 |
*/ |
| 556 |
typedef struct fcml_st_far_pointer { |
| 557 |
/** 16-bit Code segment. */ |
| 558 |
fcml_uint16_t segment; |
| 559 |
/** Size of the offset. */ |
| 560 |
fcml_usize offset_size; |
| 561 |
/** 16-bit offset. */ |
| 562 |
fcml_int16_t offset16; |
| 563 |
/** 32-bit offset. */ |
| 564 |
fcml_int32_t offset32; |
| 565 |
} fcml_st_far_pointer; |
| 566 |
|
| 567 |
/********************************************/ |
| 568 |
/** Memory addressing using ModR/M field **/ |
| 569 |
/********************************************/ |
| 570 |
|
| 571 |
/** |
| 572 |
* Addressing form. |
| 573 |
* Distinguish between two types of addressing forms: effective addressing |
| 574 |
* and explicit absolute offset. |
| 575 |
*/ |
| 576 |
typedef enum fcml_en_address_form { |
| 577 |
/** Default value set if memory addressing hasn't been configured. */ |
| 578 |
FCML_AF_UNDEFINED, |
| 579 |
/** Absolute offset (address). */ |
| 580 |
FCML_AF_OFFSET, |
| 581 |
/** Effective address combined from address components like base register, |
| 582 |
* index registers, factor, displacement etc... */ |
| 583 |
FCML_AF_COMBINED |
| 584 |
} fcml_en_effective_address_form; |
| 585 |
|
| 586 |
/** |
| 587 |
* Absolute offset. |
| 588 |
*/ |
| 589 |
typedef struct fcml_st_offset { |
| 590 |
/** Offset size 16,32 or 64 bits. */ |
| 591 |
fcml_usize size; |
| 592 |
/** True if offset should be treated as signed value. */ |
| 593 |
fcml_bool is_signed; |
| 594 |
/** Place for 16-bit absolute offset. */ |
| 595 |
fcml_int16_t off16; |
| 596 |
/** Place for 32-bit absolute offset. */ |
| 597 |
fcml_int32_t off32; |
| 598 |
/** Place for 64-bit absolute offset. */ |
| 599 |
fcml_int64_t off64; |
| 600 |
} fcml_st_offset; |
| 601 |
|
| 602 |
/** |
| 603 |
* Effective address. |
| 604 |
*/ |
| 605 |
typedef struct fcml_st_effective_address { |
| 606 |
/** GPR base register. @see fcml_st_register. */ |
| 607 |
fcml_st_register base; |
| 608 |
/** GPR index register. @see fcml_st_register. */ |
| 609 |
fcml_st_register index; |
| 610 |
/** Scale factor 1,2,4 or 8. */ |
| 611 |
fcml_uint8_t scale_factor; |
| 612 |
/** Displacement value. @see fcml_st_integer. */ |
| 613 |
fcml_st_integer displacement; |
| 614 |
} fcml_st_effective_address; |
| 615 |
|
| 616 |
/** |
| 617 |
* Describes segment register. |
| 618 |
*/ |
| 619 |
typedef struct fcml_st_segment_selector { |
| 620 |
/** Used segment register. @see fcml_st_register*/ |
| 621 |
fcml_st_register segment_selector; |
| 622 |
/** Set to true if given segment register is a default one in given |
| 623 |
* context. This value is set by disassembler. |
| 624 |
*/ |
| 625 |
fcml_bool is_default_reg; |
| 626 |
} fcml_st_segment_selector; |
| 627 |
|
| 628 |
/** |
| 629 |
* Generic memory addressing operator. |
| 630 |
*/ |
| 631 |
typedef struct fcml_st_address { |
| 632 |
/** Size of data accessed in memory.*/ |
| 633 |
fcml_usize size_operator; |
| 634 |
/** Memory addressing format: absolute offset/effective address. |
| 635 |
* @see fcml_en_effective_address_form |
| 636 |
*/ |
| 637 |
fcml_en_effective_address_form address_form; |
| 638 |
/** Segment register.*/ |
| 639 |
fcml_st_segment_selector segment_selector; |
| 640 |
/** Memory address for FCML_AF_COMBINED form.*/ |
| 641 |
fcml_st_effective_address effective_address; |
| 642 |
/** Memory address for FCML_AF_OFFSET form.*/ |
| 643 |
fcml_st_offset offset; |
| 644 |
} fcml_st_address; |
| 645 |
|
| 646 |
/** |
| 647 |
* Rounding mode. |
| 648 |
*/ |
| 649 |
typedef enum fcml_en_embeeded_rounding_control { |
| 650 |
FCML_ERC_RNE, FCML_ERC_RD, FCML_ERC_RU, FCML_ERC_RZ |
| 651 |
} fcml_en_embeeded_rounding_control; |
| 652 |
|
| 653 |
/** |
| 654 |
* Container for operand decorators. |
| 655 |
* @since 1.2.0 |
| 656 |
*/ |
| 657 |
typedef struct fcml_st_operand_decorators { |
| 658 |
/** Broadcasting: 2, 4, 8, 16, 32, 64. */ |
| 659 |
fcml_nuint8_t bcast; |
| 660 |
/** Zeroing masking. */ |
| 661 |
fcml_bool z; |
| 662 |
/** The 64-bit k registers are: k0 through k7. */ |
| 663 |
fcml_st_register operand_mask_reg; |
| 664 |
/** Embedded rounding control. */ |
| 665 |
fcml_nuint8_t er; |
| 666 |
/** Indicates support for SAE (Suppress All Exceptions). */ |
| 667 |
fcml_bool sae; |
| 668 |
} fcml_st_operand_decorators; |
| 669 |
|
| 670 |
/** |
| 671 |
* Supported operand types. |
| 672 |
*/ |
| 673 |
typedef enum fcml_en_operand_type { |
| 674 |
/** Operand not used. */ |
| 675 |
FCML_OT_NONE, |
| 676 |
/** Immediate integer value. */ |
| 677 |
FCML_OT_IMMEDIATE, |
| 678 |
/** Direct far pointer. */ |
| 679 |
FCML_OT_FAR_POINTER, |
| 680 |
/** Memory address. */ |
| 681 |
FCML_OT_ADDRESS, |
| 682 |
/** Processor register. */ |
| 683 |
FCML_OT_REGISTER, |
| 684 |
/** |
| 685 |
* Not an operand in a strict sense, |
| 686 |
* only a container for attributes. |
| 687 |
*/ |
| 688 |
FCML_OT_VIRTUAL |
| 689 |
} fcml_en_operand_type; |
| 690 |
|
| 691 |
/** |
| 692 |
* Operand hints. |
| 693 |
* Hints dedicated for instruction operands. |
| 694 |
*/ |
| 695 |
typedef enum fcml_en_operand_hints { |
| 696 |
/** |
| 697 |
* Undefined. |
| 698 |
*/ |
| 699 |
FCML_OP_HINT_UNDEFIEND = 0x0000, |
| 700 |
/** |
| 701 |
* SIMD operand. All operands which uses SIMD registers (mmx, xmm, ymm) |
| 702 |
* have this flag set. It is for instance used by Intel syntax renderer |
| 703 |
* for data size operators (mmword ptr, xmmword ptr, ymmword ptr). |
| 704 |
*/ |
| 705 |
FCML_OP_HINT_MULTIMEDIA_INSTRUCTION = 0x0001, |
| 706 |
/** |
| 707 |
* Relative address. Flags set for all branches which use jumps |
| 708 |
* calculated by displacement relative to the IP of the next instructions. |
| 709 |
*/ |
| 710 |
FCML_OP_HINT_DISPLACEMENT_RELATIVE_ADDRESS = 0x0002, |
| 711 |
/** |
| 712 |
* Pseudo opcode. Hint set for last operand (Intel syntax) which contains |
| 713 |
* comparison predicate of the following instructions: CMPSD, VCMPSD, CMPSS, |
| 714 |
* VCMPSS, VPCOMB, VPCOMW, VPCOMD, VPCOMQ, VPCOMUB, VPCOMUW, |
| 715 |
* VPCOMUD, VPCOMUQ. |
| 716 |
*/ |
| 717 |
FCML_OP_HINT_PSEUDO_OPCODE = 0x0004, |
| 718 |
/** |
| 719 |
* Offset should be encoded as absolute address. |
| 720 |
*/ |
| 721 |
FCML_OP_HINT_ABSOLUTE_ADDRESSING = 0x0008, |
| 722 |
/** |
| 723 |
* Offset should be encoded as relative address. |
| 724 |
*/ |
| 725 |
FCML_OP_HINT_RELATIVE_ADDRESSING = 0x0010, |
| 726 |
/** |
| 727 |
* Encode ModR/M with optional SIB byte if possible. |
| 728 |
**/ |
| 729 |
FCML_OP_HINT_SIB_ENCODING = 0x0020 |
| 730 |
} fcml_en_operand_hints; |
| 731 |
|
| 732 |
/** Instruction operand. |
| 733 |
* Structure represents one instruction operand. |
| 734 |
*/ |
| 735 |
typedef struct fcml_st_operand { |
| 736 |
/** Operand type */ |
| 737 |
fcml_en_operand_type type; |
| 738 |
/** Optional operand level hints. */ |
| 739 |
fcml_hints hints; |
| 740 |
/** Immediate value operand. */ |
| 741 |
fcml_st_integer immediate; |
| 742 |
/** Far pointer operand. */ |
| 743 |
fcml_st_far_pointer far_pointer; |
| 744 |
/** Effective address or absolute offset. */ |
| 745 |
fcml_st_address address; |
| 746 |
/** Register operand. */ |
| 747 |
fcml_st_register reg; |
| 748 |
/** Operand decorators. */ |
| 749 |
fcml_st_operand_decorators decorators; |
| 750 |
} fcml_st_operand; |
| 751 |
|
| 752 |
/********************************* |
| 753 |
* Instruction definition. |
| 754 |
*********************************/ |
| 755 |
|
| 756 |
/** |
| 757 |
* Instruction level hints. |
| 758 |
* Set of hints which can be defined on the instruction level. |
| 759 |
*/ |
| 760 |
typedef enum fcml_en_instruction_hints { |
| 761 |
/** No hints defined. */ |
| 762 |
FCML_HINT_NO_HINTS, |
| 763 |
/** Hints an instruction to use a FAR pointer to address the memory. */ |
| 764 |
FCML_HINT_FAR_POINTER = 0x0001, |
| 765 |
/** Hints an instruction to use a NEAR pointer to address the memory. */ |
| 766 |
FCML_HINT_NEAR_POINTER = 0x0002, |
| 767 |
/** This hint is used only by the assembler in order to force it to generate |
| 768 |
* three byte VEX/XOP prefix even if prefix fields fit in two bytes. */ |
| 769 |
FCML_HINT_LONG_FORM_POINTER = 0x0004, |
| 770 |
/** Hints an instruction to use an INDIRECT pointer to address the memory. */ |
| 771 |
FCML_HINT_INDIRECT_POINTER = 0x0008, |
| 772 |
/** Hints an instruction to use a DIRECT memory addressing. */ |
| 773 |
FCML_HINT_DIRECT_POINTER = 0x0010 |
| 774 |
} fcml_en_instruction_hints; |
| 775 |
|
| 776 |
/** |
| 777 |
* Represents an instruction as a generic model. |
| 778 |
* The generic instruction model (GIM) is a common structure used to describe an |
| 779 |
* instruction in a common way used by the FCML assembler and disassembler. |
| 780 |
*/ |
| 781 |
typedef struct fcml_st_instruction { |
| 782 |
/** Describes explicit instruction prefixes. @ref PREFIX_GROUP "List |
| 783 |
* of explicit prefixes." */ |
| 784 |
fcml_prefixes prefixes; |
| 785 |
/** Holds instruction level hints. */ |
| 786 |
fcml_hints hints; |
| 787 |
/** Dialect-dependent instruction mnemonic. |
| 788 |
* @see fcml_en_instruction_hints */ |
| 789 |
fcml_char *mnemonic; |
| 790 |
/** True for conditional instructions. */ |
| 791 |
fcml_bool is_conditional; |
| 792 |
/** Describes condition used by assembled/disassembled |
| 793 |
* conditional instruction. */ |
| 794 |
fcml_st_condition condition; |
| 795 |
/** Fixed size array of instruction operands. */ |
| 796 |
fcml_st_operand operands[FCML_OPERANDS_COUNT]; |
| 797 |
/** Number of operands defined for instruction. */ |
| 798 |
fcml_int operands_count; |
| 799 |
} fcml_st_instruction; |
| 800 |
|
| 801 |
/********************************* |
| 802 |
* Instruction definition. |
| 803 |
*********************************/ |
| 804 |
|
| 805 |
/** |
| 806 |
* Details describing an assembled instruction. |
| 807 |
* It may help to choose the final instruction if there are more than one. |
| 808 |
*/ |
| 809 |
typedef struct fcml_st_assembled_instruction_details { |
| 810 |
/* The instruction group flags. */ |
| 811 |
fcml_uint64_t instruction_group; |
| 812 |
/* If the instruction overrides the default address size attribute. */ |
| 813 |
fcml_bool asa_override; |
| 814 |
/* If the instruction overrides the default operand size attribute. */ |
| 815 |
fcml_bool osa_override; |
| 816 |
} fcml_st_assembled_instruction_details; |
| 817 |
|
| 818 |
/** |
| 819 |
* An encoded instruction. |
| 820 |
*/ |
| 821 |
typedef struct fcml_st_instruction_code { |
| 822 |
/** Pointer to the instruction code. */ |
| 823 |
fcml_uint8_t *code; |
| 824 |
/** Instruction code length. */ |
| 825 |
fcml_usize code_length; |
| 826 |
/** Instruction flags, which can be helpful while choosing |
| 827 |
* the final instruction. */ |
| 828 |
fcml_st_assembled_instruction_details details; |
| 829 |
} fcml_st_instruction_code; |
| 830 |
|
| 831 |
/**************************** |
| 832 |
* Instruction entry point. |
| 833 |
****************************/ |
| 834 |
|
| 835 |
/** |
| 836 |
* Describes the execution context for the instruction being assembled. |
| 837 |
*/ |
| 838 |
typedef struct fcml_st_entry_point { |
| 839 |
/** The processor operating mode 16/32/64-bit.*/ |
| 840 |
fcml_en_operating_mode op_mode; |
| 841 |
/** The default address size attribute (See 'D' flag of segment descriptor.)*/ |
| 842 |
fcml_usize address_size_attribute; |
| 843 |
/** The default operand size attribute (See 'D' flag of segment descriptor.)*/ |
| 844 |
fcml_usize operand_size_attribute; |
| 845 |
/** The instruction pointer EIP/RIP. Take into account that even in 16-bit |
| 846 |
* mode the EIP register is used. */ |
| 847 |
fcml_ip ip; |
| 848 |
} fcml_st_entry_point; |
| 849 |
|
| 850 |
#endif /* FCML_INT_COMMON_H_ */ |