xref: /haiku/src/libs/zydis/Zydis/DecoderData.c (revision 909af08f4328301fbdef1ffb41f566c3b5bec0c7)
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/Internal/DecoderData.h>
28 
29 /* ============================================================================================== */
30 /* Data tables                                                                                    */
31 /* ============================================================================================== */
32 
33 /* ---------------------------------------------------------------------------------------------- */
34 /* Physical instruction encodings                                                                 */
35 /* ---------------------------------------------------------------------------------------------- */
36 
37 #include <Generated/InstructionEncodings.inc>
38 
39 /* ---------------------------------------------------------------------------------------------- */
40 /* Decoder tree                                                                                   */
41 /* ---------------------------------------------------------------------------------------------- */
42 
43 #define ZYDIS_INVALID \
44     { ZYDIS_NODETYPE_INVALID, 0x00000000 }
45 #define ZYDIS_FILTER(type, id) \
46     { type, id }
47 #define ZYDIS_DEFINITION(encoding_id, id) \
48     { ZYDIS_NODETYPE_DEFINITION_MASK | encoding_id, id }
49 
50 #include <Generated/DecoderTables.inc>
51 
52 #undef ZYDIS_INVALID
53 #undef ZYDIS_FILTER
54 #undef ZYDIS_DEFINITION
55 
56 /* ---------------------------------------------------------------------------------------------- */
57 
58 /* ============================================================================================== */
59 /* Functions                                                                                      */
60 /* ============================================================================================== */
61 
62 /* ---------------------------------------------------------------------------------------------- */
63 /* Decoder tree                                                                                   */
64 /* ---------------------------------------------------------------------------------------------- */
65 
66 const ZydisDecoderTreeNode zydis_decoder_tree_root = { ZYDIS_NODETYPE_FILTER_OPCODE, 0x0000 };
67 
68 const ZydisDecoderTreeNode* ZydisDecoderTreeGetChildNode(const ZydisDecoderTreeNode* parent,
69     ZyanU16 index)
70 {
71     switch (parent->type)
72     {
73     case ZYDIS_NODETYPE_FILTER_XOP:
74         ZYAN_ASSERT(index <  13);
75         return &FILTERS_XOP[parent->value][index];
76     case ZYDIS_NODETYPE_FILTER_VEX:
77         ZYAN_ASSERT(index <  17);
78         return &FILTERS_VEX[parent->value][index];
79     case ZYDIS_NODETYPE_FILTER_EMVEX:
80         ZYAN_ASSERT(index <  49);
81         return &FILTERS_EMVEX[parent->value][index];
82     case ZYDIS_NODETYPE_FILTER_OPCODE:
83         ZYAN_ASSERT(index < 256);
84         return &FILTERS_OPCODE[parent->value][index];
85     case ZYDIS_NODETYPE_FILTER_MODE:
86         ZYAN_ASSERT(index <   4);
87         return &FILTERS_MODE[parent->value][index];
88     case ZYDIS_NODETYPE_FILTER_MODE_COMPACT:
89         ZYAN_ASSERT(index <   3);
90         return &FILTERS_MODE_COMPACT[parent->value][index];
91     case ZYDIS_NODETYPE_FILTER_MODRM_MOD:
92         ZYAN_ASSERT(index <   4);
93         return &FILTERS_MODRM_MOD[parent->value][index];
94     case ZYDIS_NODETYPE_FILTER_MODRM_MOD_COMPACT:
95         ZYAN_ASSERT(index <   2);
96         return &FILTERS_MODRM_MOD_COMPACT[parent->value][index];
97     case ZYDIS_NODETYPE_FILTER_MODRM_REG:
98         ZYAN_ASSERT(index <   8);
99         return &FILTERS_MODRM_REG[parent->value][index];
100     case ZYDIS_NODETYPE_FILTER_MODRM_RM:
101         ZYAN_ASSERT(index <   8);
102         return &FILTERS_MODRM_RM[parent->value][index];
103     case ZYDIS_NODETYPE_FILTER_PREFIX_GROUP1:
104         ZYAN_ASSERT(index < 2);
105         return &FILTERS_PREFIX_GROUP1[parent->value][index];
106     case ZYDIS_NODETYPE_FILTER_MANDATORY_PREFIX:
107         ZYAN_ASSERT(index <   5);
108         return &FILTERS_MANDATORY_PREFIX[parent->value][index];
109     case ZYDIS_NODETYPE_FILTER_OPERAND_SIZE:
110         ZYAN_ASSERT(index <   3);
111         return &FILTERS_OPERAND_SIZE[parent->value][index];
112     case ZYDIS_NODETYPE_FILTER_ADDRESS_SIZE:
113         ZYAN_ASSERT(index <   3);
114         return &FILTERS_ADDRESS_SIZE[parent->value][index];
115     case ZYDIS_NODETYPE_FILTER_VECTOR_LENGTH:
116         ZYAN_ASSERT(index <   3);
117         return &FILTERS_VECTOR_LENGTH[parent->value][index];
118     case ZYDIS_NODETYPE_FILTER_REX_W:
119         ZYAN_ASSERT(index <   2);
120         return &FILTERS_REX_W[parent->value][index];
121     case ZYDIS_NODETYPE_FILTER_REX_B:
122         ZYAN_ASSERT(index <   2);
123         return &FILTERS_REX_B[parent->value][index];
124 #ifndef ZYDIS_DISABLE_AVX512
125     case ZYDIS_NODETYPE_FILTER_EVEX_B:
126         ZYAN_ASSERT(index <   2);
127         return &FILTERS_EVEX_B[parent->value][index];
128 #endif
129 #ifndef ZYDIS_DISABLE_KNC
130     case ZYDIS_NODETYPE_FILTER_MVEX_E:
131         ZYAN_ASSERT(index <   2);
132         return &FILTERS_MVEX_E[parent->value][index];
133 #endif
134     case ZYDIS_NODETYPE_FILTER_MODE_AMD:
135         ZYAN_ASSERT(index <   2);
136         return &FILTERS_MODE_AMD[parent->value][index];
137     case ZYDIS_NODETYPE_FILTER_MODE_KNC:
138         ZYAN_ASSERT(index <   2);
139         return &FILTERS_MODE_KNC[parent->value][index];
140     case ZYDIS_NODETYPE_FILTER_MODE_MPX:
141         ZYAN_ASSERT(index <   2);
142         return &FILTERS_MODE_MPX[parent->value][index];
143     case ZYDIS_NODETYPE_FILTER_MODE_CET:
144         ZYAN_ASSERT(index <   2);
145         return &FILTERS_MODE_CET[parent->value][index];
146     case ZYDIS_NODETYPE_FILTER_MODE_LZCNT:
147         ZYAN_ASSERT(index <   2);
148         return &FILTERS_MODE_LZCNT[parent->value][index];
149     case ZYDIS_NODETYPE_FILTER_MODE_TZCNT:
150         ZYAN_ASSERT(index <   2);
151         return &FILTERS_MODE_TZCNT[parent->value][index];
152     case ZYDIS_NODETYPE_FILTER_MODE_WBNOINVD:
153         ZYAN_ASSERT(index <   2);
154         return &FILTERS_MODE_WBNOINVD[parent->value][index];
155     case ZYDIS_NODETYPE_FILTER_MODE_CLDEMOTE:
156         ZYAN_ASSERT(index <   2);
157         return &FILTERS_MODE_CLDEMOTE[parent->value][index];
158     case ZYDIS_NODETYPE_FILTER_MODE_IPREFETCH:
159         ZYAN_ASSERT(index <   2);
160         return &FILTERS_MODE_IPREFETCH[parent->value][index];
161     case ZYDIS_NODETYPE_FILTER_MODE_UD0_COMPAT:
162         ZYAN_ASSERT(index <   2);
163         return &FILTERS_MODE_UD0_COMPAT[parent->value][index];
164     default:
165         ZYAN_UNREACHABLE;
166     }
167 }
168 
169 void ZydisGetInstructionEncodingInfo(const ZydisDecoderTreeNode* node,
170     const ZydisInstructionEncodingInfo** info)
171 {
172     ZYAN_ASSERT(node->type & ZYDIS_NODETYPE_DEFINITION_MASK);
173     const ZyanU8 class = (node->type) & 0x7F;
174     ZYAN_ASSERT(class < ZYAN_ARRAY_LENGTH(INSTR_ENCODINGS));
175     *info = &INSTR_ENCODINGS[class];
176 }
177 
178 /* ---------------------------------------------------------------------------------------------- */
179 
180 /* ============================================================================================== */
181