fcml  1.1.3
fcml_common.h
Go to the documentation of this file.
1 /*
2  * FCML - Free Code Manipulation Library.
3  * Copyright (C) 2010-2015 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 
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 
35 #define FCML_OPERANDS_COUNT 5
36 
37 #define FCML_INSTRUCTION_SIZE 15
38 
39 #define FCML_OPCODES_NUM 3
40 
48 #define FCML_PREFIX_LOCK 0x0001
49 
50 #define FCML_PREFIX_REPNE 0x0002
51 
52 #define FCML_PREFIX_REPNZ FCML_PREFIX_REPNE
53 
54 #define FCML_PREFIX_REP 0x0004
55 
56 #define FCML_PREFIX_REPE FCML_PREFIX_REP
57 
58 #define FCML_PREFIX_REPZ FCML_PREFIX_REP
59 
60 #define FCML_PREFIX_XACQUIRE 0x0008
61 
62 #define FCML_PREFIX_XRELEASE 0x0010
63 
64 #define FCML_PREFIX_BRANCH_HINT 0x0020
65 
66 #define FCML_PREFIX_NOBRANCH_HINT 0x0040
67 
73 typedef enum fcml_en_operating_mode {
81 
82 /* Simple types. */
83 
85 typedef fcml_uint16_t fcml_hints;
86 
90 typedef fcml_uint16_t fcml_prefixes;
91 
95 typedef fcml_int64_t fcml_ip;
96 
97 /* Register numbers.*/
98 
105 #define FCML_REG_AL 0
106 #define FCML_REG_AX 0
107 #define FCML_REG_EAX 0
108 #define FCML_REG_RAX 0
109 #define FCML_REG_MM0 0
110 #define FCML_REG_XMM0 0
111 #define FCML_REG_YMM0 0
112 
113 #define FCML_REG_CL 1
114 #define FCML_REG_CX 1
115 #define FCML_REG_ECX 1
116 #define FCML_REG_RCX 1
117 #define FCML_REG_MM1 1
118 #define FCML_REG_XMM1 1
119 #define FCML_REG_YMM1 1
120 
121 #define FCML_REG_DL 2
122 #define FCML_REG_DX 2
123 #define FCML_REG_EDX 2
124 #define FCML_REG_RDX 2
125 #define FCML_REG_MM2 2
126 #define FCML_REG_XMM2 2
127 #define FCML_REG_YMM2 2
128 
129 #define FCML_REG_BL 3
130 #define FCML_REG_BX 3
131 #define FCML_REG_EBX 3
132 #define FCML_REG_RBX 3
133 #define FCML_REG_MM3 3
134 #define FCML_REG_XMM3 3
135 #define FCML_REG_YMM3 3
136 
137 #define FCML_REG_AH 4
138 #define FCML_REG_SP 4
139 #define FCML_REG_SPL 4
140 #define FCML_REG_ESP 4
141 #define FCML_REG_RSP 4
142 #define FCML_REG_MM4 4
143 #define FCML_REG_XMM4 4
144 #define FCML_REG_YMM4 4
145 
146 #define FCML_REG_CH 5
147 #define FCML_REG_BP 5
148 #define FCML_REG_BPL 5
149 #define FCML_REG_EBP 5
150 #define FCML_REG_RBP 5
151 #define FCML_REG_MM5 5
152 #define FCML_REG_XMM5 5
153 #define FCML_REG_YMM5 5
154 
155 #define FCML_REG_DH 6
156 #define FCML_REG_SI 6
157 #define FCML_REG_SIL 6
158 #define FCML_REG_ESI 6
159 #define FCML_REG_RSI 6
160 #define FCML_REG_MM6 6
161 #define FCML_REG_XMM6 6
162 #define FCML_REG_YMM6 6
163 
164 #define FCML_REG_BH 7
165 #define FCML_REG_DI 7
166 #define FCML_REG_DIL 7
167 #define FCML_REG_EDI 7
168 #define FCML_REG_RDI 7
169 #define FCML_REG_MM7 7
170 #define FCML_REG_XMM7 7
171 #define FCML_REG_YMM7 7
172 
173 #define FCML_REG_R8L 8
174 #define FCML_REG_R8W 8
175 #define FCML_REG_R8D 8
176 #define FCML_REG_R8 8
177 #define FCML_REG_XMM8 8
178 #define FCML_REG_YMM8 8
179 
180 #define FCML_REG_R9L 9
181 #define FCML_REG_R9W 9
182 #define FCML_REG_R9D 9
183 #define FCML_REG_R9 9
184 #define FCML_REG_XMM9 9
185 #define FCML_REG_YMM9 9
186 
187 #define FCML_REG_R10L 10
188 #define FCML_REG_R10W 10
189 #define FCML_REG_R10D 10
190 #define FCML_REG_R10 10
191 #define FCML_REG_XMM10 10
192 #define FCML_REG_YMM10 10
193 
194 #define FCML_REG_R11L 11
195 #define FCML_REG_R11W 11
196 #define FCML_REG_R11D 11
197 #define FCML_REG_R11 11
198 #define FCML_REG_XMM11 11
199 #define FCML_REG_YMM11 11
200 
201 #define FCML_REG_R12L 12
202 #define FCML_REG_R12W 12
203 #define FCML_REG_R12D 12
204 #define FCML_REG_R12 12
205 #define FCML_REG_XMM12 12
206 #define FCML_REG_YMM12 12
207 
208 #define FCML_REG_R13L 13
209 #define FCML_REG_R13W 13
210 #define FCML_REG_R13D 13
211 #define FCML_REG_R13 13
212 #define FCML_REG_XMM13 13
213 #define FCML_REG_YMM13 13
214 
215 #define FCML_REG_R14L 14
216 #define FCML_REG_R14W 14
217 #define FCML_REG_R14D 14
218 #define FCML_REG_R14 14
219 #define FCML_REG_XMM14 14
220 #define FCML_REG_YMM14 14
221 
222 #define FCML_REG_R15L 15
223 #define FCML_REG_R15W 15
224 #define FCML_REG_R15D 15
225 #define FCML_REG_R15 15
226 #define FCML_REG_XMM15 15
227 #define FCML_REG_YMM15 15
228 
229 /* Segment registers. */
230 
231 #define FCML_REG_ES 0
232 #define FCML_REG_CS 1
233 #define FCML_REG_SS 2
234 #define FCML_REG_DS 3
235 #define FCML_REG_FS 4
236 #define FCML_REG_GS 5
237 
238 /* FPU registers */
239 
240 #define FCML_REG_ST0 0
241 #define FCML_REG_ST1 1
242 #define FCML_REG_ST2 2
243 #define FCML_REG_ST3 3
244 #define FCML_REG_ST4 4
245 #define FCML_REG_ST5 5
246 #define FCML_REG_ST6 6
247 #define FCML_REG_ST7 7
248 
249 /* Control registers. */
250 
251 #define FCML_REG_CR0 0
252 #define FCML_REG_CR2 2
253 #define FCML_REG_CR3 3
254 #define FCML_REG_CR4 4
255 #define FCML_REG_CR8 8
256 
257 /* Debug register. */
258 
259 #define FCML_REG_DR0 0
260 #define FCML_REG_DR1 1
261 #define FCML_REG_DR2 2
262 #define FCML_REG_DR3 3
263 #define FCML_REG_DR4 4
264 #define FCML_REG_DR5 5
265 #define FCML_REG_DR6 6
266 #define FCML_REG_DR7 7
267 
275 #define FCML_DS_UNDEF 0
276 #define FCML_DS_8 8
277 #define FCML_DS_16 16
278 #define FCML_DS_32 32
279 #define FCML_DS_64 64
280 #define FCML_DS_128 128
281 #define FCML_DS_256 256
282 
285 /* Size operators. */
286 
287 #define FCML_OS_UNDEFINED 0
288 #define FCML_OS_BYTE 8
289 #define FCML_OS_WORD 16
290 #define FCML_OS_DWORD 32
291 #define FCML_OS_FWORD 48
292 #define FCML_OS_QWORD 64
293 #define FCML_OS_MWORD 64
294 #define FCML_OS_TBYTE 80
295 #define FCML_OS_OWORD 128
296 #define FCML_OS_XWORD 128
297 #define FCML_OS_YWORD 256
298 
303 typedef enum fcml_en_register {
321 
325 typedef struct fcml_st_register {
329  fcml_usize size;
331  fcml_uint8_t reg;
333  fcml_bool x64_exp;
335 
336 /*********************************
337  * Conditions.
338  *********************************/
339 
341 #define FCML_NUMBER_OF_CONDITIONS 8
342 
366 
370 typedef struct fcml_st_condition {
371  /* Condition type.*/
372  fcml_en_condition_type condition_type;
373  /* True if condition should be negated.*/
374  fcml_bool is_negation;
376 
377 /*********************************
378  * Size attributes flags.
379  *********************************/
380 
387 #define FCML_EN_ASF_ANY 0x00
388 #define FCML_EN_ASF_16 0x01
389 #define FCML_EN_ASF_32 0x02
390 #define FCML_EN_ASF_64 0x04
391 #define FCML_EN_ASF_ALL FCML_EN_ASF_16 | FCML_EN_ASF_32 | FCML_EN_ASF_64
392 
400  fcml_bool is_set;
402  fcml_flags flags;
404 
405 /*********************************
406  * Operands.
407  *********************************/
408 
412 typedef enum fcml_en_access_mode {
416  FCML_AM_READ = 0x01,
422 
426 typedef struct fcml_st_far_pointer {
428  fcml_uint16_t segment;
430  fcml_usize offset_size;
432  fcml_int16_t offset16;
434  fcml_int32_t offset32;
436 
437 /* *******************************************/
438 /* * Memory addressing using Mod/RM field **/
439 /* *******************************************/
440 
445 typedef enum fcml_en_address_form {
453 
457 typedef struct fcml_st_offset {
459  fcml_usize size;
461  fcml_bool is_signed;
463  fcml_int16_t off16;
465  fcml_int32_t off32;
467  fcml_int64_t off64;
469 
479  fcml_uint8_t scale_factor;
483 
487 typedef struct fcml_st_segment_selector {
491  fcml_bool is_default_reg;
493 
497 typedef struct fcml_st_address {
499  fcml_usize size_operator;
509 
513 typedef enum fcml_en_operand_type {
525 
530 typedef enum fcml_en_operand_hints {
563 
567 typedef struct fcml_st_operand {
581 
582 /*********************************
583  * Instruction definition.
584  *********************************/
585 
606 
611 typedef struct fcml_st_instruction {
617  fcml_char *mnemonic;
619  fcml_bool is_conditional;
625  fcml_int operands_count;
627 
628 /*********************************
629  * Instruction definition.
630  *********************************/
631 
635 typedef struct fcml_st_instruction_code {
637  fcml_uint8_t *code;
639  fcml_usize code_length;
641 
642 /****************************
643  * Instruction entry point.
644  ****************************/
645 
649 typedef struct fcml_st_entry_point {
659 
660 #endif /* FCML_INT_COMMON_H_ */
fcml_char * mnemonic
Dialect-dependent instruction mnemonic.
Definition: fcml_common.h:617
fcml_ip ip
Instruction pointer EIP/RIP.
Definition: fcml_common.h:657
Operand is set by instruction.
Definition: fcml_common.h:418
Describes segment register.
Definition: fcml_common.h:487
fcml_bool is_set
True if mask is set.
Definition: fcml_common.h:400
Operand is read but can be also set.
Definition: fcml_common.h:420
fcml_usize operand_size_attribute
Default operand size attribute (See 'D' flag of segment descriptor.)
Definition: fcml_common.h:655
Defines instruction's condition.
Definition: fcml_common.h:370
Effective address combined from address components like base register, index registers, factor, displacement etc...
Definition: fcml_common.h:451
fcml_uint16_t segment
16-bit Code segment.
Definition: fcml_common.h:428
fcml_int16_t offset16
16-bit offset.
Definition: fcml_common.h:432
fcml_en_register
Register type.
Definition: fcml_common.h:303
struct fcml_st_entry_point fcml_st_entry_point
Describes address of an instruction code.
fcml_st_integer immediate
Immediate value operand.
Definition: fcml_common.h:573
6 Less than
Definition: fcml_common.h:362
fcml_bool x64_exp
In case of SPL,BPL,SIL,DIL GPR registers has to be set to true.
Definition: fcml_common.h:333
Control register.
Definition: fcml_common.h:315
Segment register.
Definition: fcml_common.h:313
fcml_st_segment_selector segment_selector
Segment register.
Definition: fcml_common.h:503
fcml_en_operand_type
Supported operand types.
Definition: fcml_common.h:513
Relative address.
Definition: fcml_common.h:544
fcml_uint16_t fcml_hints
Type used for storing instruction and operand hint masks.
Definition: fcml_common.h:85
fcml_usize size
Offset size 16,32 or 64 bits.
Definition: fcml_common.h:459
General purpose register.
Definition: fcml_common.h:307
5 Parity
Definition: fcml_common.h:360
fcml_en_address_form
Addressing form.
Definition: fcml_common.h:445
fcml_en_operating_mode
Supported processor operating modes.
Definition: fcml_common.h:73
struct fcml_st_nullable_size_flags fcml_st_nullable_size_flags
Nullable wrapper for mask of size flags.
fcml_en_access_mode
Operand access mode.
Definition: fcml_common.h:412
Hints instruction to use FAR pointer to address the memory.
Definition: fcml_common.h:596
Instruction pointer register.
Definition: fcml_common.h:319
fcml_uint8_t * code
Pointer to the instruction code.
Definition: fcml_common.h:637
fcml_uint8_t reg
Register itself as a positive integer.
Definition: fcml_common.h:331
enum fcml_en_address_form fcml_en_effective_address_form
Addressing form.
fcml_st_integer displacement
Displacement value.
Definition: fcml_common.h:481
0 Overflow
Definition: fcml_common.h:350
Encode ModR/M with optional SIB byte if possible.
Definition: fcml_common.h:561
Generic instruction model.
Definition: fcml_common.h:611
fcml_int32_t offset32
32-bit offset.
Definition: fcml_common.h:434
fcml_bool is_default_reg
Set to true if given segment register is a default one in given context.
Definition: fcml_common.h:491
Handles Win32 DLL symbols importing/exporting.
Offset should be encoded as relative address.
Definition: fcml_common.h:557
#define FCML_OPERANDS_COUNT
Maximal number of the instruction operands.
Definition: fcml_common.h:35
This hint is used only by assembler in order to force it to generate three byte VEX/XOP prefix even i...
Definition: fcml_common.h:600
Offset should be encoded as absolute address.
Definition: fcml_common.h:553
Structure describes x86_64 register.
Definition: fcml_common.h:325
Hints instruction to use DIRECT memory addressing.
Definition: fcml_common.h:604
struct fcml_st_register fcml_st_register
Structure describes x86_64 register.
3 Below or equal
Definition: fcml_common.h:356
fcml_en_condition_type
Condition type.
Definition: fcml_common.h:348
fcml_st_register reg
Register operand.
Definition: fcml_common.h:579
fcml_en_operating_mode op_mode
Processor operating mode 16/32/64-bit.
Definition: fcml_common.h:651
Operand not used.
Definition: fcml_common.h:515
FPU register.
Definition: fcml_common.h:311
struct fcml_st_offset fcml_st_offset
Absolute offset.
Processor register.
Definition: fcml_common.h:523
fcml_prefixes prefixes
Describes explicit instruction prefixes.
Definition: fcml_common.h:613
Nullable wrapper for mask of size flags.
Definition: fcml_common.h:398
fcml_st_register base
GPR base register.
Definition: fcml_common.h:475
struct fcml_st_instruction fcml_st_instruction
Generic instruction model.
fcml_usize address_size_attribute
Default address size attribute (See 'D' flag of segment descriptor.)
Definition: fcml_common.h:653
fcml_int64_t fcml_ip
General instruction pointer holder.
Definition: fcml_common.h:95
Debug register.
Definition: fcml_common.h:317
Protected/Compatibility mode when 'D' segment descriptor flag is set to 1.
Definition: fcml_common.h:77
Immediate integer value.
Definition: fcml_common.h:517
fcml_hints hints
Holds instruction level hints.
Definition: fcml_common.h:615
Absolute offset (address).
Definition: fcml_common.h:449
fcml_hints hints
Optional operand level hints.
Definition: fcml_common.h:571
SIMD operand.
Definition: fcml_common.h:539
fcml_int operands_count
Number of operands defined for instruction.
Definition: fcml_common.h:625
fcml_st_address address
Effective address or absolute offset.
Definition: fcml_common.h:577
Operand is read by instruction.
Definition: fcml_common.h:416
fcml_flags flags
Mask of supported size values.
Definition: fcml_common.h:402
Undefined.
Definition: fcml_common.h:534
Undefined register type.
Definition: fcml_common.h:305
fcml_st_effective_address effective_address
Memory address for FCML_AF_COMBINED form.
Definition: fcml_common.h:505
fcml_uint8_t scale_factor
Scale factor 1,2,4 or 8.
Definition: fcml_common.h:479
fcml_st_far_pointer far_pointer
Far pointer operand.
Definition: fcml_common.h:575
Default value set if memory addressing hasn't been configured.
Definition: fcml_common.h:447
fcml_int16_t off16
Place for 16-bit absolute offset.
Definition: fcml_common.h:463
Hints instruction to use INDIRECT pointer to address the memory.
Definition: fcml_common.h:602
struct fcml_st_operand fcml_st_operand
Instruction operand.
7 Less than or equal to
Definition: fcml_common.h:364
Undefined mode.
Definition: fcml_common.h:414
SIMD (SSE, MMX) register.
Definition: fcml_common.h:309
fcml_en_instruction_hints
Instruction level hints.
Definition: fcml_common.h:592
Definition: fcml_types.h:257
Direct far pointer.
Definition: fcml_common.h:519
Real-addressing mode, virtual 8086 mode.
Definition: fcml_common.h:75
Hints instruction to use NEAR pointer to address the memory.
Definition: fcml_common.h:598
struct fcml_st_address fcml_st_address
Generic memory addressing operator.
Instruction operand.
Definition: fcml_common.h:567
fcml_usize offset_size
Size of the offset.
Definition: fcml_common.h:430
Instruction codes and addressing modes/instruction forms.
fcml_st_register segment_selector
Used segment register.
Definition: fcml_common.h:489
fcml_bool is_signed
True if offset should be treated as signed value.
Definition: fcml_common.h:461
fcml_usize size
Register size in bits.
Definition: fcml_common.h:329
fcml_bool is_conditional
True for conditional instructions.
Definition: fcml_common.h:619
No hints defined.
Definition: fcml_common.h:594
Representation of far pointer operand.
Definition: fcml_common.h:426
Effective address.
Definition: fcml_common.h:473
fcml_st_register index
GPR index register.
Definition: fcml_common.h:477
fcml_usize code_length
Instruction code length.
Definition: fcml_common.h:639
Describes address of an instruction code.
Definition: fcml_common.h:649
4 Sign
Definition: fcml_common.h:358
fcml_uint16_t fcml_prefixes
Type for explicit instruction prefixes bit mask.
Definition: fcml_common.h:90
Memory address.
Definition: fcml_common.h:521
Generic memory addressing operator.
Definition: fcml_common.h:497
struct fcml_st_segment_selector fcml_st_segment_selector
Describes segment register.
struct fcml_st_instruction_code fcml_st_instruction_code
An encoded instruction.
fcml_usize size_operator
Size of data accessed in memory.
Definition: fcml_common.h:499
Absolute offset.
Definition: fcml_common.h:457
An encoded instruction.
Definition: fcml_common.h:635
fcml_en_register type
Register type.
Definition: fcml_common.h:327
fcml_st_condition condition
Describes condition used by assembled/disassembled conditional instruction.
Definition: fcml_common.h:621
fcml_en_operand_type type
Operand type.
Definition: fcml_common.h:569
struct fcml_st_condition fcml_st_condition
Defines instruction's condition.
fcml_int32_t off32
Place for 32-bit absolute offset.
Definition: fcml_common.h:465
Types declarations.
fcml_en_operand_hints
Operand hints.
Definition: fcml_common.h:530
2 Equal
Definition: fcml_common.h:354
struct fcml_st_effective_address fcml_st_effective_address
Effective address.
fcml_en_effective_address_form address_form
Memory addressing format: absolute offset/effective address.
Definition: fcml_common.h:501
1 Below
Definition: fcml_common.h:352
struct fcml_st_far_pointer fcml_st_far_pointer
Representation of far pointer operand.
fcml_st_offset offset
Memory address for FCML_AF_OFFSET form.
Definition: fcml_common.h:507
Pseudo opcode.
Definition: fcml_common.h:549
64-bit mode.
Definition: fcml_common.h:79
fcml_int64_t off64
Place for 64-bit absolute offset.
Definition: fcml_common.h:467