/src/zydis/src/DecoderData.c
Line | Count | Source |
1 | | /*************************************************************************************************** |
2 | | |
3 | | Zyan Disassembler Library (Zydis) |
4 | | |
5 | | Original Author : Florian Bernd |
6 | | |
7 | | * Permission is hereby granted, free of charge, to any person obtaining a copy |
8 | | * of this software and associated documentation files (the "Software"), to deal |
9 | | * in the Software without restriction, including without limitation the rights |
10 | | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
11 | | * copies of the Software, and to permit persons to whom the Software is |
12 | | * furnished to do so, subject to the following conditions: |
13 | | * |
14 | | * The above copyright notice and this permission notice shall be included in all |
15 | | * copies or substantial portions of the Software. |
16 | | * |
17 | | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
18 | | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
19 | | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
20 | | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
21 | | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
22 | | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
23 | | * SOFTWARE. |
24 | | |
25 | | ***************************************************************************************************/ |
26 | | |
27 | | #include <Zydis/SharedTypes.h> |
28 | | #include <Zydis/Internal/DecoderData.h> |
29 | | |
30 | | /* ============================================================================================== */ |
31 | | /* Data tables */ |
32 | | /* ============================================================================================== */ |
33 | | |
34 | | /* ---------------------------------------------------------------------------------------------- */ |
35 | | /* Physical instruction encodings */ |
36 | | /* ---------------------------------------------------------------------------------------------- */ |
37 | | |
38 | | #include <Generated/InstructionEncodings.inc> |
39 | | |
40 | | /* ---------------------------------------------------------------------------------------------- */ |
41 | | /* Decoder tree */ |
42 | | /* ---------------------------------------------------------------------------------------------- */ |
43 | | |
44 | | ZYAN_STATIC_ASSERT(ZYDIS_NODETYPE_REQUIRED_BITS <= 8); |
45 | | |
46 | | /** |
47 | | * Defines a decoder tree node header. |
48 | | * |
49 | | * @param type The node type, see `ZydisDecoderTreeNodeTypes`. |
50 | | * @param arg0 The first argument (if applicable). |
51 | | * |
52 | | * Encoding: [15..8] = ARG0, [7..0] = TYPE. |
53 | | */ |
54 | | #define ZYDIS_DT_HEADER(type, arg0) \ |
55 | | (0x0000 | (((ZyanU8)(arg0)) << 8) | ((ZyanU8)(type))) |
56 | | |
57 | | /** |
58 | | * Defines an arbitrary value decoder tree node entry. |
59 | | * |
60 | | * @param value The value. |
61 | | */ |
62 | | #define ZYDIS_DT_VALUE(value) \ |
63 | | ((ZyanU16)(value)) |
64 | | |
65 | | /** |
66 | | * Defines an invalid (unused) decoder tree node entry. |
67 | | */ |
68 | | #define ZYDIS_DT_INVALID \ |
69 | | 0x0000 |
70 | | |
71 | | /** |
72 | | * Defines the offset to the next node in the decoder tree, relative to the start of the current |
73 | | * node. |
74 | | * |
75 | | * @param offset The offset to the next node. |
76 | | */ |
77 | | #define ZYDIS_DT_OFFSET(offset) \ |
78 | | ((ZyanU16)(offset)) |
79 | | |
80 | | /** |
81 | | * Defines an opcode table switch decoder tree node header that instructs the decoder to switch |
82 | | * to the opcode table with the given `opcode_table_id`. |
83 | | * |
84 | | * @param opcode_table_id The id of the opcode table to switch to. |
85 | | */ |
86 | | #define ZYDIS_DT_SWITCH_TABLE_HEADER(opcode_table_id) \ |
87 | | ZYDIS_DT_HEADER(ZYDIS_NODETYPE_SWITCH_TABLE, (opcode_table_id)) |
88 | | |
89 | | /** |
90 | | * Defines a definition node decoder tree node header. |
91 | | * |
92 | | * @param encoding_id The id of the physical instruction encoding entry for the definition. |
93 | | * See `INSTR_ENCODINGS`. |
94 | | */ |
95 | | #define ZYDIS_DT_DEFINITION_HEADER(encoding_id) \ |
96 | | ZYDIS_DT_HEADER(ZYDIS_NODETYPE_DEFINITION, (encoding_id)) |
97 | | |
98 | | /** |
99 | | * Represents an instruction definition reference. Only valid in combination with the corresponding |
100 | | * instruction encoding. |
101 | | * |
102 | | * @param id The instruction definition. |
103 | | */ |
104 | | #define ZYDIS_DT_DEFINITION(id) \ |
105 | | ((ZyanU16)(id)) |
106 | | |
107 | | #include <Generated/DecoderTables.inc> |
108 | | |
109 | | #undef ZYDIS_DT_HEADER |
110 | | #undef ZYDIS_DT_VALUE |
111 | | #undef ZYDIS_DT_INVALID |
112 | | #undef ZYDIS_DT_OFFSET |
113 | | #undef ZYDIS_DT_SWITCH_TABLE_HEADER |
114 | | #undef ZYDIS_DT_DEFINITION_HEADER |
115 | | #undef ZYDIS_DT_DEFINITION |
116 | | |
117 | | /* ---------------------------------------------------------------------------------------------- */ |
118 | | |
119 | | /* ============================================================================================== */ |
120 | | /* API */ |
121 | | /* ============================================================================================== */ |
122 | | |
123 | | /* ---------------------------------------------------------------------------------------------- */ |
124 | | /* Decoder tree */ |
125 | | /* ---------------------------------------------------------------------------------------------- */ |
126 | | |
127 | | const ZydisDecoderTreeNode* ZydisGetOpcodeTableRootNode(ZyanU8 opcode_table_id) |
128 | 5.65k | { |
129 | 5.65k | ZYAN_ASSERT((ZyanUSize)opcode_table_id < ZYAN_ARRAY_LENGTH(OPCODE_TABLE_TREES)); |
130 | | |
131 | 5.65k | return OPCODE_TABLE_TREES[opcode_table_id]; |
132 | 5.65k | } |
133 | | |
134 | | void ZydisGetInstructionEncodingInfo(const ZydisDecoderTreeNode* node, |
135 | | const ZydisInstructionEncodingInfo** info) |
136 | 4.75k | { |
137 | 4.75k | ZYAN_ASSERT(ZYDIS_DT_GET_TYPE(node) == ZYDIS_NODETYPE_DEFINITION); |
138 | 4.75k | const ZyanU8 encoding_id = ZYDIS_DT_GET_ARG0(node); |
139 | | |
140 | 4.75k | ZYAN_ASSERT((ZyanUSize)encoding_id < ZYAN_ARRAY_LENGTH(INSTR_ENCODINGS)); |
141 | 4.75k | *info = &INSTR_ENCODINGS[encoding_id]; |
142 | 4.75k | } |
143 | | |
144 | | /* ---------------------------------------------------------------------------------------------- */ |
145 | | |
146 | | /* ============================================================================================== */ |