Coverage Report

Created: 2026-03-03 06:15

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/capstonenext/arch/X86/X86DisassemblerDecoder.c
Line
Count
Source
1
/*===-- X86DisassemblerDecoder.c - Disassembler decoder ------------*- C -*-===*
2
 *
3
 *                     The LLVM Compiler Infrastructure
4
 *
5
 * This file is distributed under the University of Illinois Open Source
6
 * License. See LICENSE.TXT for details.
7
 *
8
 *===----------------------------------------------------------------------===*
9
 *
10
 * This file is part of the X86 Disassembler.
11
 * It contains the implementation of the instruction decoder.
12
 * Documentation for the disassembler can be found in X86Disassembler.h.
13
 *
14
 *===----------------------------------------------------------------------===*/
15
16
/* Capstone Disassembly Engine */
17
/* By Nguyen Anh Quynh <aquynh@gmail.com>, 2013-2019 */
18
19
#ifdef CAPSTONE_HAS_X86
20
21
#include <stdarg.h> /* for va_*()       */
22
#if defined(CAPSTONE_HAS_OSXKERNEL)
23
#include <libkern/libkern.h>
24
#else
25
#include <stdlib.h> /* for exit()       */
26
#endif
27
28
#include <string.h>
29
30
#include "../../cs_priv.h"
31
#include "../../utils.h"
32
33
#include "X86DisassemblerDecoder.h"
34
#include "X86Mapping.h"
35
36
/// Specifies whether a ModR/M byte is needed and (if so) which
37
/// instruction each possible value of the ModR/M byte corresponds to.  Once
38
/// this information is known, we have narrowed down to a single instruction.
39
struct ModRMDecision {
40
  uint8_t modrm_type;
41
  uint16_t instructionIDs;
42
};
43
44
/// Specifies which set of ModR/M->instruction tables to look at
45
/// given a particular opcode.
46
struct OpcodeDecision {
47
  struct ModRMDecision modRMDecisions[256];
48
};
49
50
/// Specifies which opcode->instruction tables to look at given
51
/// a particular context (set of attributes).  Since there are many possible
52
/// contexts, the decoder first uses CONTEXTS_SYM to determine which context
53
/// applies given a specific set of attributes.  Hence there are only IC_max
54
/// entries in this table, rather than 2^(ATTR_max).
55
struct ContextDecision {
56
  struct OpcodeDecision opcodeDecisions[IC_max];
57
};
58
59
#ifdef CAPSTONE_X86_REDUCE
60
#include "X86GenDisassemblerTables_reduce.inc"
61
#include "X86GenDisassemblerTables_reduce2.inc"
62
#include "X86Lookup16_reduce.inc"
63
#else
64
#include "X86GenDisassemblerTables.inc"
65
#include "X86GenDisassemblerTables2.inc"
66
#include "X86Lookup16.inc"
67
#endif
68
69
/*
70
 * contextForAttrs - Client for the instruction context table.  Takes a set of
71
 *   attributes and returns the appropriate decode context.
72
 *
73
 * @param attrMask  - Attributes, from the enumeration attributeBits.
74
 * @return          - The InstructionContext to use when looking up an
75
 *                    an instruction with these attributes.
76
 */
77
static InstructionContext contextForAttrs(uint16_t attrMask)
78
1.57M
{
79
1.57M
  return CONTEXTS_SYM[attrMask];
80
1.57M
}
81
82
/*
83
 * modRMRequired - Reads the appropriate instruction table to determine whether
84
 *   the ModR/M byte is required to decode a particular instruction.
85
 *
86
 * @param type        - The opcode type (i.e., how many bytes it has).
87
 * @param insnContext - The context for the instruction, as returned by
88
 *                      contextForAttrs.
89
 * @param opcode      - The last byte of the instruction's opcode, not counting
90
 *                      ModR/M extensions and escapes.
91
 * @return            - true if the ModR/M byte is required, false otherwise.
92
 */
93
static int modRMRequired(OpcodeType type, InstructionContext insnContext,
94
       uint16_t opcode)
95
1.57M
{
96
1.57M
  const struct OpcodeDecision *decision = NULL;
97
1.57M
  const uint8_t *indextable = NULL;
98
1.57M
  unsigned int index;
99
100
1.57M
  switch (type) {
101
0
  default:
102
0
    break;
103
1.31M
  case ONEBYTE:
104
1.31M
    decision = ONEBYTE_SYM;
105
1.31M
    indextable = index_x86DisassemblerOneByteOpcodes;
106
1.31M
    break;
107
131k
  case TWOBYTE:
108
131k
    decision = TWOBYTE_SYM;
109
131k
    indextable = index_x86DisassemblerTwoByteOpcodes;
110
131k
    break;
111
45.7k
  case THREEBYTE_38:
112
45.7k
    decision = THREEBYTE38_SYM;
113
45.7k
    indextable = index_x86DisassemblerThreeByte38Opcodes;
114
45.7k
    break;
115
62.2k
  case THREEBYTE_3A:
116
62.2k
    decision = THREEBYTE3A_SYM;
117
62.2k
    indextable = index_x86DisassemblerThreeByte3AOpcodes;
118
62.2k
    break;
119
0
#ifndef CAPSTONE_X86_REDUCE
120
19.6k
  case XOP8_MAP:
121
19.6k
    decision = XOP8_MAP_SYM;
122
19.6k
    indextable = index_x86DisassemblerXOP8Opcodes;
123
19.6k
    break;
124
1.30k
  case XOP9_MAP:
125
1.30k
    decision = XOP9_MAP_SYM;
126
1.30k
    indextable = index_x86DisassemblerXOP9Opcodes;
127
1.30k
    break;
128
961
  case XOPA_MAP:
129
961
    decision = XOPA_MAP_SYM;
130
961
    indextable = index_x86DisassemblerXOPAOpcodes;
131
961
    break;
132
1.06k
  case THREEDNOW_MAP:
133
    // 3DNow instructions always have ModRM byte
134
1.06k
    return true;
135
1.57M
#endif
136
1.57M
  }
137
138
  // return decision->opcodeDecisions[insnContext].modRMDecisions[opcode].modrm_type != MODRM_ONEENTRY;
139
1.57M
  index = indextable[insnContext];
140
1.57M
  if (index)
141
1.56M
    return decision[index - 1].modRMDecisions[opcode].modrm_type !=
142
1.56M
           MODRM_ONEENTRY;
143
8.22k
  else
144
8.22k
    return false;
145
1.57M
}
146
147
/*
148
 * decode - Reads the appropriate instruction table to obtain the unique ID of
149
 *   an instruction.
150
 *
151
 * @param type        - See modRMRequired().
152
 * @param insnContext - See modRMRequired().
153
 * @param opcode      - See modRMRequired().
154
 * @param modRM       - The ModR/M byte if required, or any value if not.
155
 * @return            - The UID of the instruction, or 0 on failure.
156
 */
157
static InstrUID decode(OpcodeType type, InstructionContext insnContext,
158
           uint8_t opcode, uint8_t modRM)
159
1.57M
{
160
1.57M
  const struct ModRMDecision *dec = NULL;
161
1.57M
  unsigned int index;
162
1.57M
  static const struct OpcodeDecision emptyDecision = { 0 };
163
164
1.57M
  switch (type) {
165
0
  default:
166
0
    break; // never reach
167
1.30M
  case ONEBYTE:
168
    // dec = &ONEBYTE_SYM.opcodeDecisions[insnContext].modRMDecisions[opcode];
169
1.30M
    index = index_x86DisassemblerOneByteOpcodes[insnContext];
170
1.30M
    if (index)
171
1.30M
      dec = &ONEBYTE_SYM[index - 1].modRMDecisions[opcode];
172
388
    else
173
388
      dec = &emptyDecision.modRMDecisions[opcode];
174
1.30M
    break;
175
131k
  case TWOBYTE:
176
    //dec = &TWOBYTE_SYM.opcodeDecisions[insnContext].modRMDecisions[opcode];
177
131k
    index = index_x86DisassemblerTwoByteOpcodes[insnContext];
178
131k
    if (index)
179
128k
      dec = &TWOBYTE_SYM[index - 1].modRMDecisions[opcode];
180
2.48k
    else
181
2.48k
      dec = &emptyDecision.modRMDecisions[opcode];
182
131k
    break;
183
45.7k
  case THREEBYTE_38:
184
    // dec = &THREEBYTE38_SYM.opcodeDecisions[insnContext].modRMDecisions[opcode];
185
45.7k
    index = index_x86DisassemblerThreeByte38Opcodes[insnContext];
186
45.7k
    if (index)
187
45.1k
      dec = &THREEBYTE38_SYM[index - 1].modRMDecisions[opcode];
188
595
    else
189
595
      dec = &emptyDecision.modRMDecisions[opcode];
190
45.7k
    break;
191
62.1k
  case THREEBYTE_3A:
192
    //dec = &THREEBYTE3A_SYM.opcodeDecisions[insnContext].modRMDecisions[opcode];
193
62.1k
    index = index_x86DisassemblerThreeByte3AOpcodes[insnContext];
194
62.1k
    if (index)
195
61.9k
      dec = &THREEBYTE3A_SYM[index - 1].modRMDecisions[opcode];
196
227
    else
197
227
      dec = &emptyDecision.modRMDecisions[opcode];
198
62.1k
    break;
199
0
#ifndef CAPSTONE_X86_REDUCE
200
19.6k
  case XOP8_MAP:
201
    // dec = &XOP8_MAP_SYM.opcodeDecisions[insnContext].modRMDecisions[opcode];
202
19.6k
    index = index_x86DisassemblerXOP8Opcodes[insnContext];
203
19.6k
    if (index)
204
15.7k
      dec = &XOP8_MAP_SYM[index - 1].modRMDecisions[opcode];
205
3.93k
    else
206
3.93k
      dec = &emptyDecision.modRMDecisions[opcode];
207
19.6k
    break;
208
1.30k
  case XOP9_MAP:
209
    // dec = &XOP9_MAP_SYM.opcodeDecisions[insnContext].modRMDecisions[opcode];
210
1.30k
    index = index_x86DisassemblerXOP9Opcodes[insnContext];
211
1.30k
    if (index)
212
958
      dec = &XOP9_MAP_SYM[index - 1].modRMDecisions[opcode];
213
347
    else
214
347
      dec = &emptyDecision.modRMDecisions[opcode];
215
1.30k
    break;
216
961
  case XOPA_MAP:
217
    // dec = &XOPA_MAP_SYM.opcodeDecisions[insnContext].modRMDecisions[opcode];
218
961
    index = index_x86DisassemblerXOPAOpcodes[insnContext];
219
961
    if (index)
220
715
      dec = &XOPA_MAP_SYM[index - 1].modRMDecisions[opcode];
221
246
    else
222
246
      dec = &emptyDecision.modRMDecisions[opcode];
223
961
    break;
224
1.06k
  case THREEDNOW_MAP:
225
    // dec = &THREEDNOW_MAP_SYM.opcodeDecisions[insnContext].modRMDecisions[opcode];
226
1.06k
    index = index_x86Disassembler3DNowOpcodes[insnContext];
227
1.06k
    if (index)
228
623
      dec = &THREEDNOW_MAP_SYM[index - 1]
229
623
               .modRMDecisions[opcode];
230
440
    else
231
440
      dec = &emptyDecision.modRMDecisions[opcode];
232
1.06k
    break;
233
1.57M
#endif
234
1.57M
  }
235
236
1.57M
  switch (dec->modrm_type) {
237
0
  default:
238
    // debug("Corrupt table!  Unknown modrm_type");
239
0
    return 0;
240
731k
  case MODRM_ONEENTRY:
241
731k
    return modRMTable[dec->instructionIDs];
242
635k
  case MODRM_SPLITRM:
243
635k
    if (modFromModRM(modRM) == 0x3)
244
135k
      return modRMTable[dec->instructionIDs + 1];
245
499k
    return modRMTable[dec->instructionIDs];
246
169k
  case MODRM_SPLITREG:
247
169k
    if (modFromModRM(modRM) == 0x3)
248
54.7k
      return modRMTable[dec->instructionIDs +
249
54.7k
            ((modRM & 0x38) >> 3) + 8];
250
114k
    return modRMTable[dec->instructionIDs + ((modRM & 0x38) >> 3)];
251
35.0k
  case MODRM_SPLITMISC:
252
35.0k
    if (modFromModRM(modRM) == 0x3)
253
10.3k
      return modRMTable[dec->instructionIDs + (modRM & 0x3f) +
254
10.3k
            8];
255
24.7k
    return modRMTable[dec->instructionIDs + ((modRM & 0x38) >> 3)];
256
0
  case MODRM_FULL:
257
0
    return modRMTable[dec->instructionIDs + modRM];
258
1.57M
  }
259
1.57M
}
260
261
/*
262
 * specifierForUID - Given a UID, returns the name and operand specification for
263
 *   that instruction.
264
 *
265
 * @param uid - The unique ID for the instruction.  This should be returned by
266
 *              decode(); specifierForUID will not check bounds.
267
 * @return    - A pointer to the specification for that instruction.
268
 */
269
static const struct InstructionSpecifier *specifierForUID(InstrUID uid)
270
1.30M
{
271
1.30M
  return &INSTRUCTIONS_SYM[uid];
272
1.30M
}
273
274
/*
275
 * consumeByte - Uses the reader function provided by the user to consume one
276
 *   byte from the instruction's memory and advance the cursor.
277
 *
278
 * @param insn  - The instruction with the reader function to use.  The cursor
279
 *                for this instruction is advanced.
280
 * @param byte  - A pointer to a pre-allocated memory buffer to be populated
281
 *                with the data read.
282
 * @return      - 0 if the read was successful; nonzero otherwise.
283
 */
284
static int consumeByte(struct InternalInstruction *insn, uint8_t *byte)
285
4.29M
{
286
4.29M
  int ret = insn->reader(insn->readerArg, byte, insn->readerCursor);
287
288
4.29M
  if (!ret)
289
4.28M
    ++(insn->readerCursor);
290
291
4.29M
  return ret;
292
4.29M
}
293
294
/*
295
 * lookAtByte - Like consumeByte, but does not advance the cursor.
296
 *
297
 * @param insn  - See consumeByte().
298
 * @param byte  - See consumeByte().
299
 * @return      - See consumeByte().
300
 */
301
static int lookAtByte(struct InternalInstruction *insn, uint8_t *byte)
302
473k
{
303
473k
  return insn->reader(insn->readerArg, byte, insn->readerCursor);
304
473k
}
305
306
static void unconsumeByte(struct InternalInstruction *insn)
307
1.46M
{
308
1.46M
  insn->readerCursor--;
309
1.46M
}
310
311
#define CONSUME_FUNC(name, type) \
312
  static int name(struct InternalInstruction *insn, type *ptr) \
313
224k
  { \
314
224k
    type combined = 0; \
315
224k
    unsigned offset; \
316
736k
    for (offset = 0; offset < sizeof(type); ++offset) { \
317
513k
      uint8_t byte; \
318
513k
      int ret = insn->reader(insn->readerArg, &byte, \
319
513k
                 insn->readerCursor + offset); \
320
513k
      if (ret) \
321
513k
        return ret; \
322
513k
      combined = combined | \
323
512k
           ((uint64_t)byte << (offset * 8)); \
324
512k
    } \
325
224k
    *ptr = combined; \
326
223k
    insn->readerCursor += sizeof(type); \
327
223k
    return 0; \
328
224k
  }
X86DisassemblerDecoder.c:consumeInt8
Line
Count
Source
313
88.0k
  { \
314
88.0k
    type combined = 0; \
315
88.0k
    unsigned offset; \
316
175k
    for (offset = 0; offset < sizeof(type); ++offset) { \
317
88.0k
      uint8_t byte; \
318
88.0k
      int ret = insn->reader(insn->readerArg, &byte, \
319
88.0k
                 insn->readerCursor + offset); \
320
88.0k
      if (ret) \
321
88.0k
        return ret; \
322
88.0k
      combined = combined | \
323
87.8k
           ((uint64_t)byte << (offset * 8)); \
324
87.8k
    } \
325
88.0k
    *ptr = combined; \
326
87.8k
    insn->readerCursor += sizeof(type); \
327
87.8k
    return 0; \
328
88.0k
  }
X86DisassemblerDecoder.c:consumeInt16
Line
Count
Source
313
23.8k
  { \
314
23.8k
    type combined = 0; \
315
23.8k
    unsigned offset; \
316
71.5k
    for (offset = 0; offset < sizeof(type); ++offset) { \
317
47.7k
      uint8_t byte; \
318
47.7k
      int ret = insn->reader(insn->readerArg, &byte, \
319
47.7k
                 insn->readerCursor + offset); \
320
47.7k
      if (ret) \
321
47.7k
        return ret; \
322
47.7k
      combined = combined | \
323
47.6k
           ((uint64_t)byte << (offset * 8)); \
324
47.6k
    } \
325
23.8k
    *ptr = combined; \
326
23.7k
    insn->readerCursor += sizeof(type); \
327
23.7k
    return 0; \
328
23.8k
  }
X86DisassemblerDecoder.c:consumeInt32
Line
Count
Source
313
31.3k
  { \
314
31.3k
    type combined = 0; \
315
31.3k
    unsigned offset; \
316
155k
    for (offset = 0; offset < sizeof(type); ++offset) { \
317
124k
      uint8_t byte; \
318
124k
      int ret = insn->reader(insn->readerArg, &byte, \
319
124k
                 insn->readerCursor + offset); \
320
124k
      if (ret) \
321
124k
        return ret; \
322
124k
      combined = combined | \
323
124k
           ((uint64_t)byte << (offset * 8)); \
324
124k
    } \
325
31.3k
    *ptr = combined; \
326
30.9k
    insn->readerCursor += sizeof(type); \
327
30.9k
    return 0; \
328
31.3k
  }
X86DisassemblerDecoder.c:consumeUInt16
Line
Count
Source
313
44.2k
  { \
314
44.2k
    type combined = 0; \
315
44.2k
    unsigned offset; \
316
132k
    for (offset = 0; offset < sizeof(type); ++offset) { \
317
88.4k
      uint8_t byte; \
318
88.4k
      int ret = insn->reader(insn->readerArg, &byte, \
319
88.4k
                 insn->readerCursor + offset); \
320
88.4k
      if (ret) \
321
88.4k
        return ret; \
322
88.4k
      combined = combined | \
323
88.1k
           ((uint64_t)byte << (offset * 8)); \
324
88.1k
    } \
325
44.2k
    *ptr = combined; \
326
44.0k
    insn->readerCursor += sizeof(type); \
327
44.0k
    return 0; \
328
44.2k
  }
X86DisassemblerDecoder.c:consumeUInt32
Line
Count
Source
313
32.4k
  { \
314
32.4k
    type combined = 0; \
315
32.4k
    unsigned offset; \
316
160k
    for (offset = 0; offset < sizeof(type); ++offset) { \
317
128k
      uint8_t byte; \
318
128k
      int ret = insn->reader(insn->readerArg, &byte, \
319
128k
                 insn->readerCursor + offset); \
320
128k
      if (ret) \
321
128k
        return ret; \
322
128k
      combined = combined | \
323
128k
           ((uint64_t)byte << (offset * 8)); \
324
128k
    } \
325
32.4k
    *ptr = combined; \
326
31.9k
    insn->readerCursor += sizeof(type); \
327
31.9k
    return 0; \
328
32.4k
  }
X86DisassemblerDecoder.c:consumeUInt64
Line
Count
Source
313
4.54k
  { \
314
4.54k
    type combined = 0; \
315
4.54k
    unsigned offset; \
316
40.3k
    for (offset = 0; offset < sizeof(type); ++offset) { \
317
35.9k
      uint8_t byte; \
318
35.9k
      int ret = insn->reader(insn->readerArg, &byte, \
319
35.9k
                 insn->readerCursor + offset); \
320
35.9k
      if (ret) \
321
35.9k
        return ret; \
322
35.9k
      combined = combined | \
323
35.8k
           ((uint64_t)byte << (offset * 8)); \
324
35.8k
    } \
325
4.54k
    *ptr = combined; \
326
4.44k
    insn->readerCursor += sizeof(type); \
327
4.44k
    return 0; \
328
4.54k
  }
329
330
/*
331
 * consume* - Use the reader function provided by the user to consume data
332
 *   values of various sizes from the instruction's memory and advance the
333
 *   cursor appropriately.  These readers perform endian conversion.
334
 *
335
 * @param insn    - See consumeByte().
336
 * @param ptr     - A pointer to a pre-allocated memory of appropriate size to
337
 *                  be populated with the data read.
338
 * @return        - See consumeByte().
339
 */
340
CONSUME_FUNC(consumeInt8, int8_t)
341
CONSUME_FUNC(consumeInt16, int16_t)
342
CONSUME_FUNC(consumeInt32, int32_t)
343
CONSUME_FUNC(consumeUInt16, uint16_t)
344
CONSUME_FUNC(consumeUInt32, uint32_t)
345
CONSUME_FUNC(consumeUInt64, uint64_t)
346
347
static bool isREX(struct InternalInstruction *insn, uint8_t prefix)
348
1.19M
{
349
1.19M
  if (insn->mode == MODE_64BIT)
350
419k
    return prefix >= 0x40 && prefix <= 0x4f;
351
352
773k
  return false;
353
1.19M
}
354
355
/*
356
 * setGroup0Prefix - Updates the decoded instruction according to the group 0-prefix.
357
 *
358
 * @param insn      - The instruction to be updated.
359
 * @param prefix    - The group 0 prefix that is present.
360
 */
361
static void setGroup0Prefix(struct InternalInstruction *insn, uint8_t prefix)
362
35.7k
{
363
35.7k
  switch (prefix) {
364
13.6k
  case 0xf0: // LOCK
365
13.6k
    insn->hasLockPrefix = true;
366
13.6k
    insn->repeatPrefix = 0;
367
13.6k
    break;
368
369
11.8k
  case 0xf2: // REPNE/REPNZ
370
22.1k
  case 0xf3: // REP or REPE/REPZ
371
22.1k
    insn->repeatPrefix = prefix;
372
22.1k
    insn->hasLockPrefix = false;
373
22.1k
    break;
374
35.7k
  }
375
35.7k
}
376
377
/*
378
 * setSegmentOverride - Overrides an instruction's prefix1 based on CPU mode.
379
 *
380
 * @param insn      - The instruction to be overridden.
381
 * @param prefix    - The segment override to use.
382
 * @param byte      - The current decoded prefix byte. Must be a segment override.
383
 */
384
static void setSegmentOverride(struct InternalInstruction *insn,
385
             SegmentOverride prefix, uint8_t byte)
386
11.0k
{
387
  // In 32-bit or 16-bit mode all segment override prefixes are used.
388
11.0k
  if (insn->mode != MODE_64BIT) {
389
6.32k
    insn->segmentOverride = prefix;
390
6.32k
    insn->prefix1 = byte;
391
6.32k
    return;
392
6.32k
  }
393
394
  // In 64-bit mode, the ES/CS/SS/DS segment overrides should be ignored.
395
  // In the case there are multiple segment overrides, do not override
396
  // an existing FS or GS segment prefix.
397
4.67k
  switch (insn->prefix1) {
398
350
  case 0x64: // FS
399
902
  case 0x65: // GS
400
902
    return;
401
4.67k
  }
402
403
  // If the proposed override is for FS or GS, mark it overridden.
404
  // All other segment prefixes are ignored.
405
3.77k
  switch (byte) {
406
393
  case 0x64: // FS
407
1.84k
  case 0x65: // GS
408
1.84k
    insn->segmentOverride = prefix;
409
1.84k
    break;
410
3.77k
  }
411
412
  // `prefix1` may later be used to decode the `notrack` prefix.
413
  // The `notrack` prefix reuses the DS segment override, so we
414
  // need to store the prefix even if it is ignored for the segment overrides.
415
3.77k
  insn->prefix1 = byte;
416
3.77k
}
417
418
/*
419
 * readPrefixes - Consumes all of an instruction's prefix bytes, and marks the
420
 *   instruction as having them.  Also sets the instruction's default operand,
421
 *   address, and other relevant data sizes to report operands correctly.
422
 *
423
 * @param insn  - The instruction whose prefixes are to be read.
424
 * @return      - 0 if the instruction could be read until the end of the prefix
425
 *                bytes, and no prefixes conflicted; nonzero otherwise.
426
 */
427
static int readPrefixes(struct InternalInstruction *insn)
428
1.14M
{
429
1.14M
  bool isPrefix = true;
430
1.14M
  uint8_t byte = 0;
431
1.14M
  uint8_t nextByte;
432
433
2.53M
  while (isPrefix) {
434
1.38M
    if (insn->mode == MODE_64BIT) {
435
      // eliminate consecutive redundant REX bytes in front
436
493k
      if (consumeByte(insn, &byte))
437
165
        return -1;
438
439
493k
      if ((byte & 0xf0) == 0x40) {
440
93.8k
        while (true) {
441
93.8k
          if (lookAtByte(
442
93.8k
                insn,
443
93.8k
                &byte)) // out of input code
444
179
            return -1;
445
93.6k
          if ((byte & 0xf0) == 0x40) {
446
            // another REX prefix, but we only remember the last one
447
11.0k
            if (consumeByte(insn, &byte))
448
0
              return -1;
449
11.0k
          } else
450
82.6k
            break;
451
93.6k
        }
452
453
        // recover the last REX byte if next byte is not a legacy prefix
454
82.6k
        switch (byte) {
455
2.30k
        case 0xf2: /* REPNE/REPNZ */
456
3.95k
        case 0xf3: /* REP or REPE/REPZ */
457
6.33k
        case 0xf0: /* LOCK */
458
7.04k
        case 0x2e: /* CS segment override -OR- Branch not taken */
459
7.84k
        case 0x36: /* SS segment override -OR- Branch taken */
460
8.00k
        case 0x3e: /* DS segment override */
461
8.30k
        case 0x26: /* ES segment override */
462
8.57k
        case 0x64: /* FS segment override */
463
8.89k
        case 0x65: /* GS segment override */
464
10.0k
        case 0x66: /* Operand-size override */
465
10.6k
        case 0x67: /* Address-size override */
466
10.6k
          break;
467
71.9k
        default: /* Not a prefix byte */
468
71.9k
          unconsumeByte(insn);
469
71.9k
          break;
470
82.6k
        }
471
410k
      } else {
472
410k
        unconsumeByte(insn);
473
410k
      }
474
493k
    }
475
476
    /* If we fail reading prefixes, just stop here and let the opcode reader deal with it */
477
1.38M
    if (consumeByte(insn, &byte))
478
212
      return -1;
479
480
1.38M
    if (insn->readerCursor - 1 == insn->startLocation &&
481
1.13M
        (byte == 0xf2 || byte == 0xf3)) {
482
      // prefix requires next byte
483
78.5k
      if (lookAtByte(insn, &nextByte))
484
137
        return -1;
485
486
      /*
487
       * If the byte is 0xf2 or 0xf3, and any of the following conditions are
488
       * met:
489
       * - it is followed by a LOCK (0xf0) prefix
490
       * - it is followed by an xchg instruction
491
       * then it should be disassembled as a xacquire/xrelease not repne/rep.
492
       */
493
78.4k
      if (((nextByte == 0xf0) ||
494
76.1k
           ((nextByte & 0xfe) == 0x86 ||
495
74.8k
            (nextByte & 0xf8) == 0x90))) {
496
4.44k
        insn->xAcquireRelease = byte;
497
4.44k
      }
498
499
      /*
500
       * Also if the byte is 0xf3, and the following condition is met:
501
       * - it is followed by a "mov mem, reg" (opcode 0x88/0x89) or
502
       *                       "mov mem, imm" (opcode 0xc6/0xc7) instructions.
503
       * then it should be disassembled as an xrelease not rep.
504
       */
505
78.4k
      if (byte == 0xf3 &&
506
36.8k
          (nextByte == 0x88 || nextByte == 0x89 ||
507
35.9k
           nextByte == 0xc6 || nextByte == 0xc7)) {
508
1.33k
        insn->xAcquireRelease = byte;
509
1.33k
      }
510
511
78.4k
      if (isREX(insn, nextByte)) {
512
10.3k
        uint8_t nnextByte;
513
514
        // Go to REX prefix after the current one
515
10.3k
        if (consumeByte(insn, &nnextByte))
516
0
          return -1;
517
518
        // We should be able to read next byte after REX prefix
519
10.3k
        if (lookAtByte(insn, &nnextByte))
520
18
          return -1;
521
522
10.3k
        unconsumeByte(insn);
523
10.3k
      }
524
78.4k
    }
525
526
1.38M
    switch (byte) {
527
64.6k
    case 0xf0: /* LOCK */
528
116k
    case 0xf2: /* REPNE/REPNZ */
529
161k
    case 0xf3: /* REP or REPE/REPZ */
530
      // only accept the last prefix
531
161k
      setGroup0Prefix(insn, byte);
532
161k
      insn->prefix0 = byte;
533
161k
      break;
534
535
9.48k
    case 0x2e: /* CS segment override -OR- Branch not taken */
536
13.2k
    case 0x36: /* SS segment override -OR- Branch taken */
537
19.1k
    case 0x3e: /* DS segment override */
538
22.8k
    case 0x26: /* ES segment override */
539
29.1k
    case 0x64: /* FS segment override */
540
36.1k
    case 0x65: /* GS segment override */
541
36.1k
      switch (byte) {
542
9.48k
      case 0x2e:
543
9.48k
        setSegmentOverride(insn, SEG_OVERRIDE_CS, byte);
544
9.48k
        break;
545
3.76k
      case 0x36:
546
3.76k
        setSegmentOverride(insn, SEG_OVERRIDE_SS, byte);
547
3.76k
        break;
548
5.89k
      case 0x3e:
549
5.89k
        setSegmentOverride(insn, SEG_OVERRIDE_DS, byte);
550
5.89k
        break;
551
3.73k
      case 0x26:
552
3.73k
        setSegmentOverride(insn, SEG_OVERRIDE_ES, byte);
553
3.73k
        break;
554
6.31k
      case 0x64:
555
6.31k
        setSegmentOverride(insn, SEG_OVERRIDE_FS, byte);
556
6.31k
        break;
557
6.98k
      case 0x65:
558
6.98k
        setSegmentOverride(insn, SEG_OVERRIDE_GS, byte);
559
6.98k
        break;
560
0
      default:
561
        // debug("Unhandled override");
562
0
        return -1;
563
36.1k
      }
564
36.1k
      break;
565
566
36.1k
    case 0x66: /* Operand-size override */
567
30.4k
      insn->hasOpSize = true;
568
30.4k
      insn->prefix2 = byte;
569
30.4k
      break;
570
571
11.7k
    case 0x67: /* Address-size override */
572
11.7k
      insn->hasAdSize = true;
573
11.7k
      insn->prefix3 = byte;
574
11.7k
      break;
575
1.14M
    default: /* Not a prefix byte */
576
1.14M
      isPrefix = false;
577
1.14M
      break;
578
1.38M
    }
579
1.38M
  }
580
581
1.14M
  insn->vectorExtensionType = TYPE_NO_VEX_XOP;
582
583
1.14M
  if (byte == 0x62) {
584
89.1k
    uint8_t byte1, byte2;
585
586
89.1k
    if (consumeByte(insn, &byte1)) {
587
      // dbgprintf(insn, "Couldn't read second byte of EVEX prefix");
588
87
      return -1;
589
87
    }
590
591
89.0k
    if (lookAtByte(insn, &byte2)) {
592
      // dbgprintf(insn, "Couldn't read third byte of EVEX prefix");
593
103
      unconsumeByte(insn); /* unconsume byte1 */
594
103
      unconsumeByte(insn); /* unconsume byte  */
595
88.9k
    } else {
596
88.9k
      if ((insn->mode == MODE_64BIT ||
597
55.5k
           (byte1 & 0xc0) == 0xc0) &&
598
79.5k
          ((~byte1 & 0xc) == 0xc) && ((byte2 & 0x4) == 0x4)) {
599
79.2k
        insn->vectorExtensionType = TYPE_EVEX;
600
79.2k
      } else {
601
9.72k
        unconsumeByte(insn); /* unconsume byte1 */
602
9.72k
        unconsumeByte(insn); /* unconsume byte  */
603
9.72k
      }
604
88.9k
    }
605
606
89.0k
    if (insn->vectorExtensionType == TYPE_EVEX) {
607
79.2k
      insn->vectorExtensionPrefix[0] = byte;
608
79.2k
      insn->vectorExtensionPrefix[1] = byte1;
609
79.2k
      if (consumeByte(insn,
610
79.2k
          &insn->vectorExtensionPrefix[2])) {
611
        // dbgprintf(insn, "Couldn't read third byte of EVEX prefix");
612
0
        return -1;
613
0
      }
614
615
79.2k
      if (consumeByte(insn,
616
79.2k
          &insn->vectorExtensionPrefix[3])) {
617
        // dbgprintf(insn, "Couldn't read fourth byte of EVEX prefix");
618
71
        return -1;
619
71
      }
620
621
      /* We simulate the REX prefix for simplicity's sake */
622
79.1k
      if (insn->mode == MODE_64BIT) {
623
33.2k
        insn->rexPrefix =
624
33.2k
          0x40 |
625
33.2k
          (wFromEVEX3of4(
626
33.2k
             insn->vectorExtensionPrefix[2])
627
33.2k
           << 3) |
628
33.2k
          (rFromEVEX2of4(
629
33.2k
             insn->vectorExtensionPrefix[1])
630
33.2k
           << 2) |
631
33.2k
          (xFromEVEX2of4(
632
33.2k
             insn->vectorExtensionPrefix[1])
633
33.2k
           << 1) |
634
33.2k
          (bFromEVEX2of4(
635
33.2k
             insn->vectorExtensionPrefix[1])
636
33.2k
           << 0);
637
33.2k
      }
638
639
      // dbgprintf(insn, "Found EVEX prefix 0x%hhx 0x%hhx 0x%hhx 0x%hhx",
640
      //    insn->vectorExtensionPrefix[0], insn->vectorExtensionPrefix[1],
641
      //    insn->vectorExtensionPrefix[2], insn->vectorExtensionPrefix[3]);
642
79.1k
    }
643
1.05M
  } else if (byte == 0xc4) {
644
8.55k
    uint8_t byte1;
645
646
8.55k
    if (lookAtByte(insn, &byte1)) {
647
      // dbgprintf(insn, "Couldn't read second byte of VEX");
648
13
      return -1;
649
13
    }
650
651
8.54k
    if (insn->mode == MODE_64BIT || (byte1 & 0xc0) == 0xc0)
652
6.85k
      insn->vectorExtensionType = TYPE_VEX_3B;
653
1.69k
    else
654
1.69k
      unconsumeByte(insn);
655
656
8.54k
    if (insn->vectorExtensionType == TYPE_VEX_3B) {
657
6.85k
      insn->vectorExtensionPrefix[0] = byte;
658
6.85k
      consumeByte(insn, &insn->vectorExtensionPrefix[1]);
659
6.85k
      consumeByte(insn, &insn->vectorExtensionPrefix[2]);
660
661
      /* We simulate the REX prefix for simplicity's sake */
662
6.85k
      if (insn->mode == MODE_64BIT)
663
3.21k
        insn->rexPrefix =
664
3.21k
          0x40 |
665
3.21k
          (wFromVEX3of3(
666
3.21k
             insn->vectorExtensionPrefix[2])
667
3.21k
           << 3) |
668
3.21k
          (rFromVEX2of3(
669
3.21k
             insn->vectorExtensionPrefix[1])
670
3.21k
           << 2) |
671
3.21k
          (xFromVEX2of3(
672
3.21k
             insn->vectorExtensionPrefix[1])
673
3.21k
           << 1) |
674
3.21k
          (bFromVEX2of3(
675
3.21k
             insn->vectorExtensionPrefix[1])
676
3.21k
           << 0);
677
678
      // dbgprintf(insn, "Found VEX prefix 0x%hhx 0x%hhx 0x%hhx",
679
      //    insn->vectorExtensionPrefix[0], insn->vectorExtensionPrefix[1],
680
      //    insn->vectorExtensionPrefix[2]);
681
6.85k
    }
682
1.04M
  } else if (byte == 0xc5) {
683
13.3k
    uint8_t byte1;
684
685
13.3k
    if (lookAtByte(insn, &byte1)) {
686
      // dbgprintf(insn, "Couldn't read second byte of VEX");
687
20
      return -1;
688
20
    }
689
690
13.3k
    if (insn->mode == MODE_64BIT || (byte1 & 0xc0) == 0xc0)
691
11.6k
      insn->vectorExtensionType = TYPE_VEX_2B;
692
1.70k
    else
693
1.70k
      unconsumeByte(insn);
694
695
13.3k
    if (insn->vectorExtensionType == TYPE_VEX_2B) {
696
11.6k
      insn->vectorExtensionPrefix[0] = byte;
697
11.6k
      consumeByte(insn, &insn->vectorExtensionPrefix[1]);
698
699
11.6k
      if (insn->mode == MODE_64BIT)
700
2.25k
        insn->rexPrefix =
701
2.25k
          0x40 |
702
2.25k
          (rFromVEX2of2(
703
2.25k
             insn->vectorExtensionPrefix[1])
704
2.25k
           << 2);
705
706
11.6k
      switch (ppFromVEX2of2(insn->vectorExtensionPrefix[1])) {
707
5.30k
      default:
708
5.30k
        break;
709
6.30k
      case VEX_PREFIX_66:
710
6.30k
        insn->hasOpSize = true;
711
6.30k
        break;
712
11.6k
      }
713
714
      // dbgprintf(insn, "Found VEX prefix 0x%hhx 0x%hhx",
715
      //    insn->vectorExtensionPrefix[0],
716
      //    insn->vectorExtensionPrefix[1]);
717
11.6k
    }
718
1.03M
  } else if (byte == 0x8f) {
719
10.3k
    uint8_t byte1;
720
721
10.3k
    if (lookAtByte(insn, &byte1)) {
722
      // dbgprintf(insn, "Couldn't read second byte of XOP");
723
10
      return -1;
724
10
    }
725
726
10.3k
    if ((byte1 & 0x38) !=
727
10.3k
        0x0) /* 0 in these 3 bits is a POP instruction. */
728
9.30k
      insn->vectorExtensionType = TYPE_XOP;
729
1.03k
    else
730
1.03k
      unconsumeByte(insn);
731
732
10.3k
    if (insn->vectorExtensionType == TYPE_XOP) {
733
9.30k
      insn->vectorExtensionPrefix[0] = byte;
734
9.30k
      consumeByte(insn, &insn->vectorExtensionPrefix[1]);
735
9.30k
      consumeByte(insn, &insn->vectorExtensionPrefix[2]);
736
737
      /* We simulate the REX prefix for simplicity's sake */
738
9.30k
      if (insn->mode == MODE_64BIT)
739
1.21k
        insn->rexPrefix =
740
1.21k
          0x40 |
741
1.21k
          (wFromXOP3of3(
742
1.21k
             insn->vectorExtensionPrefix[2])
743
1.21k
           << 3) |
744
1.21k
          (rFromXOP2of3(
745
1.21k
             insn->vectorExtensionPrefix[1])
746
1.21k
           << 2) |
747
1.21k
          (xFromXOP2of3(
748
1.21k
             insn->vectorExtensionPrefix[1])
749
1.21k
           << 1) |
750
1.21k
          (bFromXOP2of3(
751
1.21k
             insn->vectorExtensionPrefix[1])
752
1.21k
           << 0);
753
754
9.30k
      switch (ppFromXOP3of3(insn->vectorExtensionPrefix[2])) {
755
9.28k
      default:
756
9.28k
        break;
757
9.28k
      case VEX_PREFIX_66:
758
19
        insn->hasOpSize = true;
759
19
        break;
760
9.30k
      }
761
762
      // dbgprintf(insn, "Found XOP prefix 0x%hhx 0x%hhx 0x%hhx",
763
      //    insn->vectorExtensionPrefix[0], insn->vectorExtensionPrefix[1],
764
      //    insn->vectorExtensionPrefix[2]);
765
9.30k
    }
766
1.02M
  } else if (isREX(insn, byte)) {
767
71.9k
    if (lookAtByte(insn, &nextByte))
768
0
      return -1;
769
770
71.9k
    insn->rexPrefix = byte;
771
    // dbgprintf(insn, "Found REX prefix 0x%hhx", byte);
772
71.9k
  } else
773
951k
    unconsumeByte(insn);
774
775
1.14M
  if (insn->mode == MODE_16BIT) {
776
389k
    insn->registerSize = (insn->hasOpSize ? 4 : 2);
777
389k
    insn->addressSize = (insn->hasAdSize ? 4 : 2);
778
389k
    insn->displacementSize = (insn->hasAdSize ? 4 : 2);
779
389k
    insn->immediateSize = (insn->hasOpSize ? 4 : 2);
780
389k
    insn->immSize = (insn->hasOpSize ? 4 : 2);
781
755k
  } else if (insn->mode == MODE_32BIT) {
782
366k
    insn->registerSize = (insn->hasOpSize ? 2 : 4);
783
366k
    insn->addressSize = (insn->hasAdSize ? 2 : 4);
784
366k
    insn->displacementSize = (insn->hasAdSize ? 2 : 4);
785
366k
    insn->immediateSize = (insn->hasOpSize ? 2 : 4);
786
366k
    insn->immSize = (insn->hasOpSize ? 2 : 4);
787
388k
  } else if (insn->mode == MODE_64BIT) {
788
388k
    if (insn->rexPrefix && wFromREX(insn->rexPrefix)) {
789
72.2k
      insn->registerSize = 8;
790
72.2k
      insn->addressSize = (insn->hasAdSize ? 4 : 8);
791
72.2k
      insn->displacementSize = 4;
792
72.2k
      insn->immediateSize = 4;
793
72.2k
      insn->immSize = 4;
794
316k
    } else {
795
316k
      insn->registerSize = (insn->hasOpSize ? 2 : 4);
796
316k
      insn->addressSize = (insn->hasAdSize ? 4 : 8);
797
316k
      insn->displacementSize = (insn->hasOpSize ? 2 : 4);
798
316k
      insn->immediateSize = (insn->hasOpSize ? 2 : 4);
799
316k
      insn->immSize = (insn->hasOpSize ? 4 : 8);
800
316k
    }
801
388k
  }
802
803
1.14M
  return 0;
804
1.14M
}
805
806
static int readModRM(struct InternalInstruction *insn);
807
808
/*
809
 * readOpcode - Reads the opcode (excepting the ModR/M byte in the case of
810
 *   extended or escape opcodes).
811
 *
812
 * @param insn  - The instruction whose opcode is to be read.
813
 * @return      - 0 if the opcode could be read successfully; nonzero otherwise.
814
 */
815
static int readOpcode(struct InternalInstruction *insn)
816
324k
{
817
324k
  uint8_t current;
818
819
  // dbgprintf(insn, "readOpcode()");
820
821
324k
  insn->opcodeType = ONEBYTE;
822
823
324k
  if (insn->vectorExtensionType == TYPE_EVEX) {
824
15.7k
    switch (mmFromEVEX2of4(insn->vectorExtensionPrefix[1])) {
825
3
    default:
826
      // dbgprintf(insn, "Unhandled mm field for instruction (0x%hhx)",
827
      //    mmFromEVEX2of4(insn->vectorExtensionPrefix[1]));
828
3
      return -1;
829
4.66k
    case VEX_LOB_0F:
830
4.66k
      insn->opcodeType = TWOBYTE;
831
4.66k
      return consumeByte(insn, &insn->opcode);
832
5.60k
    case VEX_LOB_0F38:
833
5.60k
      insn->opcodeType = THREEBYTE_38;
834
5.60k
      return consumeByte(insn, &insn->opcode);
835
5.49k
    case VEX_LOB_0F3A:
836
5.49k
      insn->opcodeType = THREEBYTE_3A;
837
5.49k
      return consumeByte(insn, &insn->opcode);
838
15.7k
    }
839
308k
  } else if (insn->vectorExtensionType == TYPE_VEX_3B) {
840
1.37k
    switch (mmmmmFromVEX2of3(insn->vectorExtensionPrefix[1])) {
841
7
    default:
842
      // dbgprintf(insn, "Unhandled m-mmmm field for instruction (0x%hhx)",
843
      //    mmmmmFromVEX2of3(insn->vectorExtensionPrefix[1]));
844
7
      return -1;
845
171
    case VEX_LOB_0F:
846
      //insn->twoByteEscape = 0x0f;
847
171
      insn->opcodeType = TWOBYTE;
848
171
      return consumeByte(insn, &insn->opcode);
849
780
    case VEX_LOB_0F38:
850
      //insn->twoByteEscape = 0x0f;
851
780
      insn->opcodeType = THREEBYTE_38;
852
780
      return consumeByte(insn, &insn->opcode);
853
415
    case VEX_LOB_0F3A:
854
      //insn->twoByteEscape = 0x0f;
855
415
      insn->opcodeType = THREEBYTE_3A;
856
415
      return consumeByte(insn, &insn->opcode);
857
1.37k
    }
858
307k
  } else if (insn->vectorExtensionType == TYPE_VEX_2B) {
859
    //insn->twoByteEscape = 0x0f;
860
3.29k
    insn->opcodeType = TWOBYTE;
861
3.29k
    return consumeByte(insn, &insn->opcode);
862
303k
  } else if (insn->vectorExtensionType == TYPE_XOP) {
863
2.23k
    switch (mmmmmFromXOP2of3(insn->vectorExtensionPrefix[1])) {
864
18
    default:
865
      // dbgprintf(insn, "Unhandled m-mmmm field for instruction (0x%hhx)",
866
      //    mmmmmFromVEX2of3(insn->vectorExtensionPrefix[1]));
867
18
      return -1;
868
2.02k
    case XOP_MAP_SELECT_8:
869
2.02k
      insn->opcodeType = XOP8_MAP;
870
2.02k
      return consumeByte(insn, &insn->opcode);
871
176
    case XOP_MAP_SELECT_9:
872
176
      insn->opcodeType = XOP9_MAP;
873
176
      return consumeByte(insn, &insn->opcode);
874
14
    case XOP_MAP_SELECT_A:
875
14
      insn->opcodeType = XOPA_MAP;
876
14
      return consumeByte(insn, &insn->opcode);
877
2.23k
    }
878
2.23k
  }
879
880
301k
  if (consumeByte(insn, &current))
881
0
    return -1;
882
883
  // save this first byte for MOVcr, MOVdr, MOVrc, MOVrd
884
301k
  insn->firstByte = current;
885
886
301k
  if (current == 0x0f) {
887
    // dbgprintf(insn, "Found a two-byte escape prefix (0x%hhx)", current);
888
14.5k
    insn->twoByteEscape = current;
889
890
14.5k
    if (consumeByte(insn, &current))
891
29
      return -1;
892
893
14.5k
    if (current == 0x38) {
894
      // dbgprintf(insn, "Found a three-byte escape prefix (0x%hhx)", current);
895
173
      if (consumeByte(insn, &current))
896
1
        return -1;
897
898
172
      insn->opcodeType = THREEBYTE_38;
899
14.3k
    } else if (current == 0x3a) {
900
      // dbgprintf(insn, "Found a three-byte escape prefix (0x%hhx)", current);
901
229
      if (consumeByte(insn, &current))
902
0
        return -1;
903
904
229
      insn->opcodeType = THREEBYTE_3A;
905
14.1k
    } else if (current == 0x0f) {
906
      // dbgprintf(insn, "Found a 3dnow escape prefix (0x%hhx)", current);
907
      // Consume operands before the opcode to comply with the 3DNow encoding
908
90
      if (readModRM(insn))
909
1
        return -1;
910
911
89
      if (consumeByte(insn, &current))
912
0
        return -1;
913
914
89
      insn->opcodeType = THREEDNOW_MAP;
915
14.0k
    } else {
916
      // dbgprintf(insn, "Didn't find a three-byte escape prefix");
917
14.0k
      insn->opcodeType = TWOBYTE;
918
14.0k
    }
919
14.5k
  }
920
921
  /*
922
   * At this point we have consumed the full opcode.
923
   * Anything we consume from here on must be unconsumed.
924
   */
925
926
301k
  insn->opcode = current;
927
928
301k
  return 0;
929
301k
}
930
931
// Hacky for FEMMS
932
#define GET_INSTRINFO_ENUM
933
#ifndef CAPSTONE_X86_REDUCE
934
#include "X86GenInstrInfo.inc"
935
#else
936
#include "X86GenInstrInfo_reduce.inc"
937
#endif
938
939
/*
940
 * getIDWithAttrMask - Determines the ID of an instruction, consuming
941
 *   the ModR/M byte as appropriate for extended and escape opcodes,
942
 *   and using a supplied attribute mask.
943
 *
944
 * @param instructionID - A pointer whose target is filled in with the ID of the
945
 *                        instruction.
946
 * @param insn          - The instruction whose ID is to be determined.
947
 * @param attrMask      - The attribute mask to search.
948
 * @return              - 0 if the ModR/M could be read when needed or was not
949
 *                        needed; nonzero otherwise.
950
 */
951
static int getIDWithAttrMask(uint16_t *instructionID,
952
           struct InternalInstruction *insn,
953
           uint16_t attrMask)
954
1.57M
{
955
1.57M
  bool hasModRMExtension;
956
957
1.57M
  InstructionContext instructionClass = contextForAttrs(attrMask);
958
959
1.57M
  hasModRMExtension =
960
1.57M
    modRMRequired(insn->opcodeType, instructionClass, insn->opcode);
961
962
1.57M
  if (hasModRMExtension) {
963
842k
    if (readModRM(insn))
964
2.43k
      return -1;
965
966
840k
    *instructionID = decode(insn->opcodeType, instructionClass,
967
840k
          insn->opcode, insn->modRM);
968
840k
  } else {
969
731k
    *instructionID = decode(insn->opcodeType, instructionClass,
970
731k
          insn->opcode, 0);
971
731k
  }
972
973
1.57M
  return 0;
974
1.57M
}
975
976
/*
977
 * is16BitEquivalent - Determines whether two instruction names refer to
978
 * equivalent instructions but one is 16-bit whereas the other is not.
979
 *
980
 * @param orig  - The instruction ID that is not 16-bit
981
 * @param equiv - The instruction ID that is 16-bit
982
 */
983
static bool is16BitEquivalent(unsigned orig, unsigned equiv)
984
361k
{
985
361k
  size_t i;
986
361k
  uint16_t idx;
987
988
361k
  if ((idx = x86_16_bit_eq_lookup[orig]) != 0) {
989
176k
    for (i = idx - 1; i < ARR_SIZE(x86_16_bit_eq_tbl) &&
990
176k
          x86_16_bit_eq_tbl[i].first == orig;
991
172k
         i++) {
992
172k
      if (x86_16_bit_eq_tbl[i].second == equiv)
993
167k
        return true;
994
172k
    }
995
172k
  }
996
997
193k
  return false;
998
361k
}
999
1000
/*
1001
 * is64Bit - Determines whether this instruction is a 64-bit instruction.
1002
 *
1003
 * @param name - The instruction that is not 16-bit
1004
 */
1005
static bool is64Bit(uint16_t id)
1006
25.5k
{
1007
25.5k
  unsigned int i = find_insn(id);
1008
25.5k
  if (i != -1) {
1009
25.3k
    return insns[i].is64bit;
1010
25.3k
  }
1011
1012
  // not found??
1013
126
  return false;
1014
25.5k
}
1015
1016
typedef enum {
1017
  DO_NOT_RESOLVE = 0,
1018
  IGNORE_REP = 1,
1019
  IGNORE_DATA_SIZE = 2,
1020
} MandatoryPrefixResolution;
1021
1022
/*
1023
 * shouldResolveMandatoryPrefixConflict - Resolves conflicts between the 
1024
 * data size override prefix and the REP/REPNZ prefixes in the attribute 
1025
 * mask when needed.
1026
 *
1027
 * We need to resolve these conflicts, because the TableGen lookups we 
1028
 * perform distinguish between instructions with and without REP.
1029
 * For example, there may be an entry for SHLD with a DATA16 data size 
1030
 * override prefix, but no entry for REP + DATA16.
1031
 * These entries are split, because in some cases the REP and DATA16
1032
 * prefixes are used as mandatory prefixes.
1033
 * When both are mandatory prefixes, the effect of prefixing both 
1034
 * to an instruction at the same time is not specified by
1035
 * reference manuals.
1036
 *
1037
 * Conflicts are resolved by one of these three resolutions:
1038
 *   - If conflicts should not be resolved, take no action.
1039
 *   - If conflicts should be resolved and the instruction has no 
1040
 *     mandatory prefixes, resolves in favor of data size override.
1041
 *   - If conflicts should be resolved and the instruction has mandatory 
1042
 *     prefixes, resolves in favor of REP/REPNZ.
1043
 * 
1044
 * @param insn - The instruction
1045
 * @param attrMask - The current attribute mask.
1046
 */
1047
static uint16_t resolveMandatoryPrefixConflict(struct InternalInstruction *insn,
1048
                 uint16_t attrMask)
1049
618
{
1050
618
  MandatoryPrefixResolution resolution = DO_NOT_RESOLVE;
1051
1052
  // We inspect the opcode map and opcode to determine how we need to resolve
1053
  // a mandatory prefix conflict.
1054
618
  switch (insn->opcodeType) {
1055
  // No one-byte opcodes have mandatory prefixes.
1056
0
  case ONEBYTE:
1057
0
    resolution = DO_NOT_RESOLVE;
1058
0
    break;
1059
523
  case TWOBYTE:
1060
    // Exceptions for instructions that operate on data size-overridable
1061
    // operands.
1062
523
    if (
1063
      // XADD
1064
523
      (insn->opcode & 0xFE) == 0xC0
1065
1066
      // BSWAP
1067
426
      || (insn->opcode & 0xF8) == 0xC8
1068
1069
      // CMPXCHG, LSS, BTR, LFS, LGS, MOVZX
1070
262
      || (insn->opcode & 0xF8) == 0xB0
1071
1072
      // Group 16, various NOPs
1073
261
      || (insn->opcode & 0xF8) == 0x18
1074
1075
      // UD0
1076
302
      || insn->opcode == 0xFF) {
1077
302
      resolution = IGNORE_REP;
1078
302
      break;
1079
302
    }
1080
1081
    // We inspect the instruction to determine if it operates on xmm
1082
    // registers or general-purpose registers.
1083
    //
1084
    // If it operates on general purpose registers, the data size override
1085
    // prefix is not a mandatory prefix and should not be ignored.
1086
    // In most cases, this also means that the REP prefix is not a mandatory
1087
    // prefix and should be ignored.
1088
    //
1089
    // If the instruction operates on xmm registers, the data size override
1090
    // is used to select the operation type (SS, SD, PS, or PD).
1091
    // In this case, the REP prefixes take priority over the data size [1]
1092
    // override prefixes, and when both are present the data size override
1093
    // prefix should be ignored.
1094
    //
1095
    // The exception is 0xB0, where the REP prefixes are mandatory prefixes
1096
    // but the data size override prefix should still be respected.
1097
    // For this case we return DO_NOT_RESOLVE, which returns attrMask as-is.
1098
    //
1099
    // [1]: https://stackoverflow.com/a/7197365
1100
221
    switch (insn->opcode & 0xf0) {
1101
0
    case 0x10:
1102
2
    case 0x50:
1103
2
    case 0x60:
1104
3
    case 0x70:
1105
35
    case 0xC0:
1106
39
    case 0xD0:
1107
43
    case 0xE0:
1108
54
    case 0xF0:
1109
54
      resolution = IGNORE_DATA_SIZE;
1110
54
      break;
1111
34
    case 0x00:
1112
56
    case 0x20:
1113
105
    case 0x30:
1114
116
    case 0x40:
1115
149
    case 0x80:
1116
159
    case 0x90:
1117
167
    case 0xA0:
1118
167
      resolution = IGNORE_REP;
1119
167
      break;
1120
0
    default: // 0xB0
1121
0
      resolution = DO_NOT_RESOLVE;
1122
0
      break;
1123
221
    }
1124
221
    break;
1125
221
  case THREEBYTE_38:
1126
    // Exception: the ADOX and CRC32 instructions.
1127
    // These ignore the data size override prefix even though they
1128
    // operate on general-purpose registers.
1129
26
    if ((insn->opcode & 0xF0) == 0xF0) {
1130
26
      resolution = IGNORE_DATA_SIZE;
1131
26
      break;
1132
26
    }
1133
1134
    // Do not need to be resolved, all REP+DATA16 combinations are UD
1135
    // or separately specified.
1136
0
    resolution = DO_NOT_RESOLVE;
1137
0
    break;
1138
1
  case THREEBYTE_3A:
1139
    // Do not need to be resolved, all REP+DATA16 combinations are UD
1140
    // or separately specified.
1141
1
    resolution = DO_NOT_RESOLVE;
1142
1
    break;
1143
0
  case XOP8_MAP:
1144
0
  case XOP9_MAP:
1145
0
  case XOPA_MAP:
1146
    // These instructions do not appear to operate on XMM/SSE registers,
1147
    // so the REP prefixes can be safely ignored.
1148
0
    resolution = IGNORE_REP;
1149
0
    break;
1150
68
  case THREEDNOW_MAP:
1151
    // AMD Reference Manual Volume 3, Section 1.2.1, states that all
1152
    // 3DNow! instructions ignore the data size override prefix.
1153
68
    resolution = IGNORE_DATA_SIZE;
1154
68
    break;
1155
618
  }
1156
1157
618
  switch (resolution) {
1158
469
  case IGNORE_REP:
1159
469
    return attrMask & ~(ATTR_XD | ATTR_XS);
1160
148
  case IGNORE_DATA_SIZE:
1161
148
    return attrMask & ~ATTR_OPSIZE;
1162
0
  default:
1163
1
  case DO_NOT_RESOLVE:
1164
1
    return attrMask;
1165
618
  }
1166
618
}
1167
1168
/*
1169
 * getID - Determines the ID of an instruction, consuming the ModR/M byte as
1170
 *   appropriate for extended and escape opcodes.  Determines the attributes and
1171
 *   context for the instruction before doing so.
1172
 *
1173
 * @param insn  - The instruction whose ID is to be determined.
1174
 * @return      - 0 if the ModR/M could be read when needed or was not needed;
1175
 *                nonzero otherwise.
1176
 */
1177
static int getID(struct InternalInstruction *insn)
1178
324k
{
1179
324k
  uint16_t attrMask;
1180
324k
  uint16_t instructionID;
1181
1182
324k
  attrMask = ATTR_NONE;
1183
1184
324k
  if (insn->mode == MODE_64BIT)
1185
101k
    attrMask |= ATTR_64BIT;
1186
1187
324k
  if (insn->vectorExtensionType != TYPE_NO_VEX_XOP) {
1188
22.6k
    attrMask |= (insn->vectorExtensionType == TYPE_EVEX) ?
1189
15.7k
            ATTR_EVEX :
1190
22.6k
            ATTR_VEX;
1191
1192
22.6k
    if (insn->vectorExtensionType == TYPE_EVEX) {
1193
15.7k
      switch (ppFromEVEX3of4(
1194
15.7k
        insn->vectorExtensionPrefix[2])) {
1195
12.9k
      case VEX_PREFIX_66:
1196
12.9k
        attrMask |= ATTR_OPSIZE;
1197
12.9k
        break;
1198
567
      case VEX_PREFIX_F3:
1199
567
        attrMask |= ATTR_XS;
1200
567
        break;
1201
79
      case VEX_PREFIX_F2:
1202
79
        attrMask |= ATTR_XD;
1203
79
        break;
1204
15.7k
      }
1205
1206
15.7k
      if (zFromEVEX4of4(insn->vectorExtensionPrefix[3]))
1207
1.77k
        attrMask |= ATTR_EVEXKZ;
1208
15.7k
      if (bFromEVEX4of4(insn->vectorExtensionPrefix[3]))
1209
6.37k
        attrMask |= ATTR_EVEXB;
1210
15.7k
      if (aaaFromEVEX4of4(insn->vectorExtensionPrefix[3]))
1211
10.7k
        attrMask |= ATTR_EVEXK;
1212
15.7k
      if (lFromEVEX4of4(insn->vectorExtensionPrefix[3]))
1213
8.28k
        attrMask |= ATTR_EVEXL;
1214
15.7k
      if (l2FromEVEX4of4(insn->vectorExtensionPrefix[3]))
1215
6.64k
        attrMask |= ATTR_EVEXL2;
1216
15.7k
    } else if (insn->vectorExtensionType == TYPE_VEX_3B) {
1217
1.36k
      switch (ppFromVEX3of3(insn->vectorExtensionPrefix[2])) {
1218
1.18k
      case VEX_PREFIX_66:
1219
1.18k
        attrMask |= ATTR_OPSIZE;
1220
1.18k
        break;
1221
153
      case VEX_PREFIX_F3:
1222
153
        attrMask |= ATTR_XS;
1223
153
        break;
1224
13
      case VEX_PREFIX_F2:
1225
13
        attrMask |= ATTR_XD;
1226
13
        break;
1227
1.36k
      }
1228
1229
1.36k
      if (lFromVEX3of3(insn->vectorExtensionPrefix[2]))
1230
888
        attrMask |= ATTR_VEXL;
1231
5.50k
    } else if (insn->vectorExtensionType == TYPE_VEX_2B) {
1232
3.28k
      switch (ppFromVEX2of2(insn->vectorExtensionPrefix[1])) {
1233
1.10k
      case VEX_PREFIX_66:
1234
1.10k
        attrMask |= ATTR_OPSIZE;
1235
1.10k
        break;
1236
190
      case VEX_PREFIX_F3:
1237
190
        attrMask |= ATTR_XS;
1238
190
        break;
1239
1.01k
      case VEX_PREFIX_F2:
1240
1.01k
        attrMask |= ATTR_XD;
1241
1.01k
        break;
1242
3.28k
      }
1243
1244
3.28k
      if (lFromVEX2of2(insn->vectorExtensionPrefix[1]))
1245
2.60k
        attrMask |= ATTR_VEXL;
1246
3.28k
    } else if (insn->vectorExtensionType == TYPE_XOP) {
1247
2.21k
      switch (ppFromXOP3of3(insn->vectorExtensionPrefix[2])) {
1248
7
      case VEX_PREFIX_66:
1249
7
        attrMask |= ATTR_OPSIZE;
1250
7
        break;
1251
2
      case VEX_PREFIX_F3:
1252
2
        attrMask |= ATTR_XS;
1253
2
        break;
1254
5
      case VEX_PREFIX_F2:
1255
5
        attrMask |= ATTR_XD;
1256
5
        break;
1257
2.21k
      }
1258
1259
2.21k
      if (lFromXOP3of3(insn->vectorExtensionPrefix[2]))
1260
169
        attrMask |= ATTR_VEXL;
1261
2.21k
    } else {
1262
0
      return -1;
1263
0
    }
1264
301k
  } else {
1265
301k
    if (insn->hasOpSize && insn->mode != MODE_16BIT) {
1266
5.48k
      attrMask |= ATTR_OPSIZE;
1267
5.48k
    }
1268
301k
    if (insn->hasAdSize)
1269
1.84k
      attrMask |= ATTR_ADSIZE;
1270
301k
    if (insn->opcodeType == ONEBYTE) {
1271
286k
      if (insn->repeatPrefix == 0xf3 &&
1272
7.00k
          (insn->opcode == 0x90))
1273
        // Special support for PAUSE
1274
30
        attrMask |= ATTR_XS;
1275
286k
    } else {
1276
14.5k
      if (insn->repeatPrefix == 0xf2)
1277
2.38k
        attrMask |= ATTR_XD;
1278
12.1k
      else if (insn->repeatPrefix == 0xf3)
1279
1.86k
        attrMask |= ATTR_XS;
1280
14.5k
    }
1281
1282
301k
    if ((attrMask & ATTR_OPSIZE) &&
1283
5.48k
        (attrMask & (ATTR_XD | ATTR_XS))) {
1284
618
      attrMask =
1285
618
        resolveMandatoryPrefixConflict(insn, attrMask);
1286
618
    }
1287
301k
  }
1288
1289
324k
  if (insn->rexPrefix & 0x08) {
1290
14.8k
    attrMask |= ATTR_REXW;
1291
14.8k
    attrMask &= ~ATTR_ADSIZE;
1292
14.8k
  }
1293
1294
  /*
1295
   * JCXZ/JECXZ need special handling for 16-bit mode because the meaning
1296
   * of the AdSize prefix is inverted w.r.t. 32-bit mode.
1297
   */
1298
324k
  if (insn->mode == MODE_16BIT && insn->opcodeType == ONEBYTE &&
1299
71.9k
      insn->opcode == 0xE3)
1300
187
    attrMask ^= ATTR_ADSIZE;
1301
1302
  /*
1303
   * In 64-bit mode all f64 superscripted opcodes ignore opcode size prefix
1304
   * CALL/JMP/JCC instructions need to ignore 0x66 and consume 4 bytes
1305
   */
1306
324k
  if ((insn->mode == MODE_64BIT) && insn->hasOpSize) {
1307
3.93k
    switch (insn->opcode) {
1308
128
    case 0xE8:
1309
138
    case 0xE9:
1310
      // Take care of psubsb and other mmx instructions.
1311
138
      if (insn->opcodeType == ONEBYTE) {
1312
2
        attrMask ^= ATTR_OPSIZE;
1313
2
        insn->immediateSize = 4;
1314
2
        insn->displacementSize = 4;
1315
2
      }
1316
138
      break;
1317
8
    case 0x82:
1318
36
    case 0x83:
1319
74
    case 0x84:
1320
242
    case 0x85:
1321
301
    case 0x86:
1322
707
    case 0x87:
1323
1.20k
    case 0x88:
1324
1.28k
    case 0x89:
1325
1.29k
    case 0x8A:
1326
1.30k
    case 0x8B:
1327
1.37k
    case 0x8C:
1328
1.45k
    case 0x8D:
1329
1.46k
    case 0x8E:
1330
1.52k
    case 0x8F:
1331
      // Take care of lea and three byte ops.
1332
1.52k
      if (insn->opcodeType == TWOBYTE) {
1333
30
        attrMask ^= ATTR_OPSIZE;
1334
30
        insn->immediateSize = 4;
1335
30
        insn->displacementSize = 4;
1336
30
      }
1337
1.52k
      break;
1338
3.93k
    }
1339
3.93k
  }
1340
1341
  /* The following clauses compensate for limitations of the tables. */
1342
324k
  if (insn->mode != MODE_64BIT &&
1343
222k
      insn->vectorExtensionType != TYPE_NO_VEX_XOP) {
1344
14.6k
    if (getIDWithAttrMask(&instructionID, insn, attrMask)) {
1345
9
      return -1;
1346
9
    }
1347
1348
    /*
1349
     * The tables can't distinguish between cases where the W-bit is used to
1350
     * select register size and cases where it's a required part of the opcode.
1351
     */
1352
14.6k
    if ((insn->vectorExtensionType == TYPE_EVEX &&
1353
8.74k
         wFromEVEX3of4(insn->vectorExtensionPrefix[2])) ||
1354
10.0k
        (insn->vectorExtensionType == TYPE_VEX_3B &&
1355
1.06k
         wFromVEX3of3(insn->vectorExtensionPrefix[2])) ||
1356
9.49k
        (insn->vectorExtensionType == TYPE_XOP &&
1357
5.20k
         wFromXOP3of3(insn->vectorExtensionPrefix[2]))) {
1358
5.20k
      uint16_t instructionIDWithREXW;
1359
1360
5.20k
      if (getIDWithAttrMask(&instructionIDWithREXW, insn,
1361
5.20k
                attrMask | ATTR_REXW)) {
1362
3
        insn->instructionID = instructionID;
1363
3
        insn->spec = specifierForUID(instructionID);
1364
3
        return 0;
1365
3
      }
1366
1367
      // If not a 64-bit instruction. Switch the opcode.
1368
5.20k
      if (!is64Bit(instructionIDWithREXW)) {
1369
4.85k
        insn->instructionID = instructionIDWithREXW;
1370
4.85k
        insn->spec =
1371
4.85k
          specifierForUID(instructionIDWithREXW);
1372
1373
4.85k
        return 0;
1374
4.85k
      }
1375
5.20k
    }
1376
14.6k
  }
1377
1378
  /*
1379
   * Absolute moves, umonitor, and movdir64b need special handling.
1380
   * -For 16-bit mode because the meaning of the AdSize and OpSize prefixes are
1381
   *  inverted w.r.t.
1382
   * -For 32-bit mode we need to ensure the ADSIZE prefix is observed in
1383
   *  any position.
1384
   */
1385
319k
  if ((insn->opcodeType == ONEBYTE && ((insn->opcode & 0xFC) == 0xA0)) ||
1386
315k
      (insn->opcodeType == TWOBYTE && (insn->opcode == 0xAE)) ||
1387
315k
      (insn->opcodeType == THREEBYTE_38 && insn->opcode == 0xF8)) {
1388
    /* Make sure we observed the prefixes in any position. */
1389
3.99k
    if (insn->hasAdSize)
1390
119
      attrMask |= ATTR_ADSIZE;
1391
1392
3.99k
    if (insn->hasOpSize)
1393
88
      attrMask |= ATTR_OPSIZE;
1394
1395
    /* In 16-bit, invert the attributes. */
1396
3.99k
    if (insn->mode == MODE_16BIT) {
1397
1.08k
      attrMask ^= ATTR_ADSIZE;
1398
1399
      /* The OpSize attribute is only valid with the absolute moves. */
1400
1.08k
      if (insn->opcodeType == ONEBYTE &&
1401
1.08k
          ((insn->opcode & 0xFC) == 0xA0))
1402
1.08k
        attrMask ^= ATTR_OPSIZE;
1403
1.08k
    }
1404
1405
3.99k
    if (getIDWithAttrMask(&instructionID, insn, attrMask)) {
1406
1
      return -1;
1407
1
    }
1408
1409
3.99k
    insn->instructionID = instructionID;
1410
3.99k
    insn->spec = specifierForUID(instructionID);
1411
1412
3.99k
    return 0;
1413
3.99k
  }
1414
315k
  if (getIDWithAttrMask(&instructionID, insn, attrMask)) {
1415
666
    return -1;
1416
666
  }
1417
1418
314k
  if ((insn->mode == MODE_16BIT || insn->hasOpSize) &&
1419
81.0k
      !(attrMask & ATTR_OPSIZE)) {
1420
    /*
1421
     * The instruction tables make no distinction between instructions that
1422
     * allow OpSize anywhere (i.e., 16-bit operations) and that need it in a
1423
     * particular spot (i.e., many MMX operations).  In general we're
1424
     * conservative, but in the specific case where OpSize is present but not
1425
     * in the right place we check if there's a 16-bit operation.
1426
     */
1427
74.0k
    const struct InstructionSpecifier *spec;
1428
74.0k
    uint16_t instructionIDWithOpsize;
1429
1430
74.0k
    spec = specifierForUID(instructionID);
1431
1432
74.0k
    if (getIDWithAttrMask(&instructionIDWithOpsize, insn,
1433
74.0k
              attrMask | ATTR_OPSIZE)) {
1434
      /*
1435
       * ModRM required with OpSize but not present; give up and return version
1436
       * without OpSize set
1437
       */
1438
0
      insn->instructionID = instructionID;
1439
0
      insn->spec = spec;
1440
1441
0
      return 0;
1442
0
    }
1443
1444
74.0k
    if (is16BitEquivalent(instructionID, instructionIDWithOpsize) &&
1445
36.7k
        (insn->mode == MODE_16BIT) ^ insn->hasOpSize) {
1446
36.3k
      insn->instructionID = instructionIDWithOpsize;
1447
36.3k
      insn->spec = specifierForUID(instructionIDWithOpsize);
1448
37.6k
    } else {
1449
37.6k
      insn->instructionID = instructionID;
1450
37.6k
      insn->spec = spec;
1451
37.6k
    }
1452
1453
74.0k
    return 0;
1454
74.0k
  }
1455
1456
240k
  if (insn->opcodeType == ONEBYTE && insn->opcode == 0x90 &&
1457
866
      insn->rexPrefix & 0x01) {
1458
    /*
1459
     * NOOP shouldn't decode as NOOP if REX.b is set. Instead
1460
     * it should decode as XCHG %r8, %eax.
1461
     */
1462
37
    const struct InstructionSpecifier *spec;
1463
37
    uint16_t instructionIDWithNewOpcode;
1464
37
    const struct InstructionSpecifier *specWithNewOpcode;
1465
1466
37
    spec = specifierForUID(instructionID);
1467
1468
    /* Borrow opcode from one of the other XCHGar opcodes */
1469
37
    insn->opcode = 0x91;
1470
1471
37
    if (getIDWithAttrMask(&instructionIDWithNewOpcode, insn,
1472
37
              attrMask)) {
1473
0
      insn->opcode = 0x90;
1474
1475
0
      insn->instructionID = instructionID;
1476
0
      insn->spec = spec;
1477
1478
0
      return 0;
1479
0
    }
1480
1481
37
    specWithNewOpcode = specifierForUID(instructionIDWithNewOpcode);
1482
1483
    /* Change back */
1484
37
    insn->opcode = 0x90;
1485
1486
37
    insn->instructionID = instructionIDWithNewOpcode;
1487
37
    insn->spec = specWithNewOpcode;
1488
1489
37
    return 0;
1490
37
  }
1491
1492
240k
  insn->instructionID = instructionID;
1493
240k
  insn->spec = specifierForUID(insn->instructionID);
1494
1495
240k
  return 0;
1496
240k
}
1497
1498
/*
1499
 * readSIB - Consumes the SIB byte to determine addressing information for an
1500
 *   instruction.
1501
 *
1502
 * @param insn  - The instruction whose SIB byte is to be read.
1503
 * @return      - 0 if the SIB byte was successfully read; nonzero otherwise.
1504
 */
1505
static int readSIB(struct InternalInstruction *insn)
1506
32.4k
{
1507
32.4k
  SIBBase sibBaseBase = SIB_BASE_NONE;
1508
32.4k
  uint8_t index, base;
1509
1510
  // dbgprintf(insn, "readSIB()");
1511
1512
32.4k
  if (insn->consumedSIB)
1513
0
    return 0;
1514
1515
32.4k
  insn->consumedSIB = true;
1516
1517
32.4k
  switch (insn->addressSize) {
1518
0
  case 2:
1519
    // dbgprintf(insn, "SIB-based addressing doesn't work in 16-bit mode");
1520
0
    return -1;
1521
13.8k
  case 4:
1522
13.8k
    insn->sibIndexBase = SIB_INDEX_EAX;
1523
13.8k
    sibBaseBase = SIB_BASE_EAX;
1524
13.8k
    break;
1525
18.5k
  case 8:
1526
18.5k
    insn->sibIndexBase = SIB_INDEX_RAX;
1527
18.5k
    sibBaseBase = SIB_BASE_RAX;
1528
18.5k
    break;
1529
32.4k
  }
1530
1531
32.4k
  if (consumeByte(insn, &insn->sib))
1532
68
    return -1;
1533
1534
32.3k
  index = indexFromSIB(insn->sib) | (xFromREX(insn->rexPrefix) << 3);
1535
1536
32.3k
  if (index == 0x4) {
1537
6.18k
    insn->sibIndex = SIB_INDEX_NONE;
1538
26.1k
  } else {
1539
26.1k
    insn->sibIndex = (SIBIndex)(insn->sibIndexBase + index);
1540
26.1k
  }
1541
1542
32.3k
  insn->sibScale = 1 << scaleFromSIB(insn->sib);
1543
1544
32.3k
  base = baseFromSIB(insn->sib) | (bFromREX(insn->rexPrefix) << 3);
1545
1546
32.3k
  switch (base) {
1547
2.92k
  case 0x5:
1548
3.72k
  case 0xd:
1549
3.72k
    switch (modFromModRM(insn->modRM)) {
1550
1.80k
    case 0x0:
1551
1.80k
      insn->eaDisplacement = EA_DISP_32;
1552
1.80k
      insn->sibBase = SIB_BASE_NONE;
1553
1.80k
      break;
1554
1.53k
    case 0x1:
1555
1.53k
      insn->eaDisplacement = EA_DISP_8;
1556
1.53k
      insn->sibBase = (SIBBase)(sibBaseBase + base);
1557
1.53k
      break;
1558
377
    case 0x2:
1559
377
      insn->eaDisplacement = EA_DISP_32;
1560
377
      insn->sibBase = (SIBBase)(sibBaseBase + base);
1561
377
      break;
1562
0
    case 0x3:
1563
      // debug("Cannot have Mod = 0b11 and a SIB byte");
1564
0
      return -1;
1565
3.72k
    }
1566
3.72k
    break;
1567
28.6k
  default:
1568
28.6k
    insn->sibBase = (SIBBase)(sibBaseBase + base);
1569
28.6k
    break;
1570
32.3k
  }
1571
1572
32.3k
  return 0;
1573
32.3k
}
1574
1575
/*
1576
 * readDisplacement - Consumes the displacement of an instruction.
1577
 *
1578
 * @param insn  - The instruction whose displacement is to be read.
1579
 * @return      - 0 if the displacement byte was successfully read; nonzero
1580
 *                otherwise.
1581
 */
1582
static int readDisplacement(struct InternalInstruction *insn)
1583
203k
{
1584
203k
  int8_t d8;
1585
203k
  int16_t d16;
1586
203k
  int32_t d32;
1587
1588
  // dbgprintf(insn, "readDisplacement()");
1589
1590
203k
  if (insn->consumedDisplacement)
1591
0
    return 0;
1592
1593
203k
  insn->consumedDisplacement = true;
1594
203k
  insn->displacementOffset = insn->readerCursor - insn->startLocation;
1595
1596
203k
  switch (insn->eaDisplacement) {
1597
59.8k
  case EA_DISP_NONE:
1598
59.8k
    insn->consumedDisplacement = false;
1599
59.8k
    break;
1600
88.0k
  case EA_DISP_8:
1601
88.0k
    if (consumeInt8(insn, &d8))
1602
239
      return -1;
1603
87.8k
    insn->displacement = d8;
1604
87.8k
    break;
1605
23.8k
  case EA_DISP_16:
1606
23.8k
    if (consumeInt16(insn, &d16))
1607
110
      return -1;
1608
23.7k
    insn->displacement = d16;
1609
23.7k
    break;
1610
31.3k
  case EA_DISP_32:
1611
31.3k
    if (consumeInt32(insn, &d32))
1612
417
      return -1;
1613
30.9k
    insn->displacement = d32;
1614
30.9k
    break;
1615
203k
  }
1616
1617
202k
  return 0;
1618
203k
}
1619
1620
/*
1621
 * readModRM - Consumes all addressing information (ModR/M byte, SIB byte, and
1622
 *   displacement) for an instruction and interprets it.
1623
 *
1624
 * @param insn  - The instruction whose addressing information is to be read.
1625
 * @return      - 0 if the information was successfully read; nonzero otherwise.
1626
 */
1627
static int readModRM(struct InternalInstruction *insn)
1628
1.91M
{
1629
1.91M
  uint8_t mod, rm, reg, evexrm;
1630
1631
  // dbgprintf(insn, "readModRM()");
1632
1633
1.91M
  if (insn->consumedModRM)
1634
1.29M
    return 0;
1635
1636
620k
  insn->modRMOffset = (uint8_t)(insn->readerCursor - insn->startLocation);
1637
1638
620k
  if (consumeByte(insn, &insn->modRM))
1639
1.60k
    return -1;
1640
1641
619k
  insn->consumedModRM = true;
1642
1643
  // save original ModRM for later reference
1644
619k
  insn->orgModRM = insn->modRM;
1645
1646
  // handle MOVcr, MOVdr, MOVrc, MOVrd by pretending they have MRM.mod = 3
1647
619k
  if ((insn->firstByte == 0x0f && insn->opcodeType == TWOBYTE) &&
1648
54.0k
      (insn->opcode >= 0x20 && insn->opcode <= 0x23))
1649
1.23k
    insn->modRM |= 0xC0;
1650
1651
619k
  mod = modFromModRM(insn->modRM);
1652
619k
  rm = rmFromModRM(insn->modRM);
1653
619k
  reg = regFromModRM(insn->modRM);
1654
1655
  /*
1656
   * This goes by insn->registerSize to pick the correct register, which messes
1657
   * up if we're using (say) XMM or 8-bit register operands.  That gets fixed in
1658
   * fixupReg().
1659
   */
1660
619k
  switch (insn->registerSize) {
1661
211k
  case 2:
1662
211k
    insn->regBase = MODRM_REG_AX;
1663
211k
    insn->eaRegBase = EA_REG_AX;
1664
211k
    break;
1665
353k
  case 4:
1666
353k
    insn->regBase = MODRM_REG_EAX;
1667
353k
    insn->eaRegBase = EA_REG_EAX;
1668
353k
    break;
1669
55.0k
  case 8:
1670
55.0k
    insn->regBase = MODRM_REG_RAX;
1671
55.0k
    insn->eaRegBase = EA_REG_RAX;
1672
55.0k
    break;
1673
619k
  }
1674
1675
619k
  reg |= rFromREX(insn->rexPrefix) << 3;
1676
619k
  rm |= bFromREX(insn->rexPrefix) << 3;
1677
1678
619k
  evexrm = 0;
1679
619k
  if (insn->vectorExtensionType == TYPE_EVEX &&
1680
78.7k
      insn->mode == MODE_64BIT) {
1681
33.0k
    reg |= r2FromEVEX2of4(insn->vectorExtensionPrefix[1]) << 4;
1682
33.0k
    evexrm = xFromEVEX2of4(insn->vectorExtensionPrefix[1]) << 4;
1683
33.0k
  }
1684
1685
619k
  insn->reg = (Reg)(insn->regBase + reg);
1686
1687
619k
  switch (insn->addressSize) {
1688
197k
  case 2: {
1689
197k
    EABase eaBaseBase = EA_BASE_BX_SI;
1690
1691
197k
    switch (mod) {
1692
112k
    case 0x0:
1693
112k
      if (rm == 0x6) {
1694
6.24k
        insn->eaBase = EA_BASE_NONE;
1695
6.24k
        insn->eaDisplacement = EA_DISP_16;
1696
6.24k
        if (readDisplacement(insn))
1697
17
          return -1;
1698
106k
      } else {
1699
106k
        insn->eaBase = (EABase)(eaBaseBase + rm);
1700
106k
        insn->eaDisplacement = EA_DISP_NONE;
1701
106k
      }
1702
112k
      break;
1703
112k
    case 0x1:
1704
26.9k
      insn->eaBase = (EABase)(eaBaseBase + rm);
1705
26.9k
      insn->eaDisplacement = EA_DISP_8;
1706
26.9k
      insn->displacementSize = 1;
1707
26.9k
      if (readDisplacement(insn))
1708
80
        return -1;
1709
26.8k
      break;
1710
26.8k
    case 0x2:
1711
17.6k
      insn->eaBase = (EABase)(eaBaseBase + rm);
1712
17.6k
      insn->eaDisplacement = EA_DISP_16;
1713
17.6k
      if (readDisplacement(insn))
1714
93
        return -1;
1715
17.5k
      break;
1716
40.4k
    case 0x3:
1717
40.4k
      insn->eaBase = (EABase)(insn->eaRegBase + rm);
1718
40.4k
      if (readDisplacement(insn))
1719
0
        return -1;
1720
40.4k
      break;
1721
197k
    }
1722
197k
    break;
1723
197k
  }
1724
1725
201k
  case 4:
1726
421k
  case 8: {
1727
421k
    EABase eaBaseBase =
1728
421k
      (insn->addressSize == 4 ? EA_BASE_EAX : EA_BASE_RAX);
1729
1730
421k
    switch (mod) {
1731
0
    default:
1732
0
      break;
1733
223k
    case 0x0:
1734
223k
      insn->eaDisplacement =
1735
223k
        EA_DISP_NONE; /* readSIB may override this */
1736
      // In determining whether RIP-relative mode is used (rm=5),
1737
      // or whether a SIB byte is present (rm=4),
1738
      // the extension bits (REX.b and EVEX.x) are ignored.
1739
223k
      switch (rm & 7) {
1740
21.2k
      case 0x4: // SIB byte is present
1741
21.2k
        insn->eaBase = (insn->addressSize == 4 ?
1742
8.87k
              EA_BASE_sib :
1743
21.2k
              EA_BASE_sib64);
1744
21.2k
        if (readSIB(insn) || readDisplacement(insn))
1745
52
          return -1;
1746
21.2k
        break;
1747
21.2k
      case 0x5: // RIP-relative
1748
5.73k
        insn->eaBase = EA_BASE_NONE;
1749
5.73k
        insn->eaDisplacement = EA_DISP_32;
1750
5.73k
        if (readDisplacement(insn))
1751
79
          return -1;
1752
5.65k
        break;
1753
196k
      default:
1754
196k
        insn->eaBase = (EABase)(eaBaseBase + rm);
1755
196k
        break;
1756
223k
      }
1757
223k
      break;
1758
223k
    case 0x1:
1759
61.1k
      insn->displacementSize = 1;
1760
      /* FALLTHROUGH */
1761
84.9k
    case 0x2:
1762
84.9k
      insn->eaDisplacement =
1763
84.9k
        (mod == 0x1 ? EA_DISP_8 : EA_DISP_32);
1764
84.9k
      switch (rm & 7) {
1765
11.1k
      case 0x4: // SIB byte is present
1766
11.1k
        insn->eaBase = EA_BASE_sib;
1767
11.1k
        if (readSIB(insn) || readDisplacement(insn))
1768
87
          return -1;
1769
11.0k
        break;
1770
73.8k
      default:
1771
73.8k
        insn->eaBase = (EABase)(eaBaseBase + rm);
1772
73.8k
        if (readDisplacement(insn))
1773
426
          return -1;
1774
73.3k
        break;
1775
84.9k
      }
1776
84.4k
      break;
1777
113k
    case 0x3:
1778
113k
      insn->eaDisplacement = EA_DISP_NONE;
1779
113k
      insn->eaBase = (EABase)(insn->eaRegBase + rm + evexrm);
1780
113k
      break;
1781
421k
    }
1782
1783
421k
    break;
1784
421k
  }
1785
619k
  } /* switch (insn->addressSize) */
1786
1787
618k
  return 0;
1788
619k
}
1789
1790
#define GENERIC_FIXUP_FUNC(name, base, prefix, mask) \
1791
  static uint16_t name(struct InternalInstruction *insn, \
1792
           OperandType type, uint8_t index, uint8_t *valid) \
1793
680k
  { \
1794
680k
    *valid = 1; \
1795
680k
    switch (type) { \
1796
0
    default: \
1797
0
      *valid = 0; \
1798
0
      return 0; \
1799
156k
    case TYPE_Rv: \
1800
156k
      return base + index; \
1801
230k
    case TYPE_R8: \
1802
230k
      index &= mask; \
1803
230k
      if (index > 0xf) \
1804
230k
        *valid = 0; \
1805
230k
      if (insn->rexPrefix && index >= 4 && index <= 7) { \
1806
2.84k
        return prefix##_SPL + (index - 4); \
1807
228k
      } else { \
1808
228k
        return prefix##_AL + index; \
1809
228k
      } \
1810
230k
    case TYPE_R16: \
1811
7.03k
      index &= mask; \
1812
7.03k
      if (index > 0xf) \
1813
7.03k
        *valid = 0; \
1814
7.03k
      return prefix##_AX + index; \
1815
230k
    case TYPE_R32: \
1816
2.83k
      index &= mask; \
1817
2.83k
      if (index > 0xf) \
1818
2.83k
        *valid = 0; \
1819
2.83k
      return prefix##_EAX + index; \
1820
230k
    case TYPE_R64: \
1821
20.9k
      index &= mask; \
1822
20.9k
      if (index > 0xf) \
1823
20.9k
        *valid = 0; \
1824
20.9k
      return prefix##_RAX + index; \
1825
230k
    case TYPE_ZMM: \
1826
58.6k
      return prefix##_ZMM0 + index; \
1827
230k
    case TYPE_YMM: \
1828
49.4k
      return prefix##_YMM0 + index; \
1829
230k
    case TYPE_XMM: \
1830
98.6k
      return prefix##_XMM0 + index; \
1831
230k
    case TYPE_VK: \
1832
37.4k
      index &= 0xf; \
1833
37.4k
      if (index > 7) \
1834
37.4k
        *valid = 0; \
1835
37.4k
      return prefix##_K0 + index; \
1836
230k
    case TYPE_MM64: \
1837
6.12k
      return prefix##_MM0 + (index & 0x7); \
1838
230k
    case TYPE_SEGMENTREG: \
1839
1.91k
      if ((index & 7) > 5) \
1840
1.91k
        *valid = 0; \
1841
1.91k
      return prefix##_ES + (index & 7); \
1842
230k
    case TYPE_DEBUGREG: \
1843
804
      return prefix##_DR0 + index; \
1844
230k
    case TYPE_CONTROLREG: \
1845
432
      return prefix##_CR0 + index; \
1846
230k
    case TYPE_BNDR: \
1847
8.83k
      if (index > 3) \
1848
8.83k
        *valid = 0; \
1849
8.83k
      return prefix##_BND0 + index; \
1850
230k
    case TYPE_MVSIBX: \
1851
0
      return prefix##_XMM0 + index; \
1852
230k
    case TYPE_MVSIBY: \
1853
0
      return prefix##_YMM0 + index; \
1854
230k
    case TYPE_MVSIBZ: \
1855
0
      return prefix##_ZMM0 + index; \
1856
680k
    } \
1857
680k
  }
X86DisassemblerDecoder.c:fixupRegValue
Line
Count
Source
1793
536k
  { \
1794
536k
    *valid = 1; \
1795
536k
    switch (type) { \
1796
0
    default: \
1797
0
      *valid = 0; \
1798
0
      return 0; \
1799
116k
    case TYPE_Rv: \
1800
116k
      return base + index; \
1801
188k
    case TYPE_R8: \
1802
188k
      index &= mask; \
1803
188k
      if (index > 0xf) \
1804
188k
        *valid = 0; \
1805
188k
      if (insn->rexPrefix && index >= 4 && index <= 7) { \
1806
1.70k
        return prefix##_SPL + (index - 4); \
1807
187k
      } else { \
1808
187k
        return prefix##_AL + index; \
1809
187k
      } \
1810
188k
    case TYPE_R16: \
1811
5.11k
      index &= mask; \
1812
5.11k
      if (index > 0xf) \
1813
5.11k
        *valid = 0; \
1814
5.11k
      return prefix##_AX + index; \
1815
188k
    case TYPE_R32: \
1816
1.00k
      index &= mask; \
1817
1.00k
      if (index > 0xf) \
1818
1.00k
        *valid = 0; \
1819
1.00k
      return prefix##_EAX + index; \
1820
188k
    case TYPE_R64: \
1821
11.0k
      index &= mask; \
1822
11.0k
      if (index > 0xf) \
1823
11.0k
        *valid = 0; \
1824
11.0k
      return prefix##_RAX + index; \
1825
188k
    case TYPE_ZMM: \
1826
45.3k
      return prefix##_ZMM0 + index; \
1827
188k
    case TYPE_YMM: \
1828
38.8k
      return prefix##_YMM0 + index; \
1829
188k
    case TYPE_XMM: \
1830
79.3k
      return prefix##_XMM0 + index; \
1831
188k
    case TYPE_VK: \
1832
35.3k
      index &= 0xf; \
1833
35.3k
      if (index > 7) \
1834
35.3k
        *valid = 0; \
1835
35.3k
      return prefix##_K0 + index; \
1836
188k
    case TYPE_MM64: \
1837
3.66k
      return prefix##_MM0 + (index & 0x7); \
1838
188k
    case TYPE_SEGMENTREG: \
1839
1.91k
      if ((index & 7) > 5) \
1840
1.91k
        *valid = 0; \
1841
1.91k
      return prefix##_ES + (index & 7); \
1842
188k
    case TYPE_DEBUGREG: \
1843
804
      return prefix##_DR0 + index; \
1844
188k
    case TYPE_CONTROLREG: \
1845
432
      return prefix##_CR0 + index; \
1846
188k
    case TYPE_BNDR: \
1847
7.77k
      if (index > 3) \
1848
7.77k
        *valid = 0; \
1849
7.77k
      return prefix##_BND0 + index; \
1850
188k
    case TYPE_MVSIBX: \
1851
0
      return prefix##_XMM0 + index; \
1852
188k
    case TYPE_MVSIBY: \
1853
0
      return prefix##_YMM0 + index; \
1854
188k
    case TYPE_MVSIBZ: \
1855
0
      return prefix##_ZMM0 + index; \
1856
536k
    } \
1857
536k
  }
X86DisassemblerDecoder.c:fixupRMValue
Line
Count
Source
1793
144k
  { \
1794
144k
    *valid = 1; \
1795
144k
    switch (type) { \
1796
0
    default: \
1797
0
      *valid = 0; \
1798
0
      return 0; \
1799
40.0k
    case TYPE_Rv: \
1800
40.0k
      return base + index; \
1801
41.8k
    case TYPE_R8: \
1802
41.8k
      index &= mask; \
1803
41.8k
      if (index > 0xf) \
1804
41.8k
        *valid = 0; \
1805
41.8k
      if (insn->rexPrefix && index >= 4 && index <= 7) { \
1806
1.13k
        return prefix##_SPL + (index - 4); \
1807
40.7k
      } else { \
1808
40.7k
        return prefix##_AL + index; \
1809
40.7k
      } \
1810
41.8k
    case TYPE_R16: \
1811
1.92k
      index &= mask; \
1812
1.92k
      if (index > 0xf) \
1813
1.92k
        *valid = 0; \
1814
1.92k
      return prefix##_AX + index; \
1815
41.8k
    case TYPE_R32: \
1816
1.82k
      index &= mask; \
1817
1.82k
      if (index > 0xf) \
1818
1.82k
        *valid = 0; \
1819
1.82k
      return prefix##_EAX + index; \
1820
41.8k
    case TYPE_R64: \
1821
9.85k
      index &= mask; \
1822
9.85k
      if (index > 0xf) \
1823
9.85k
        *valid = 0; \
1824
9.85k
      return prefix##_RAX + index; \
1825
41.8k
    case TYPE_ZMM: \
1826
13.3k
      return prefix##_ZMM0 + index; \
1827
41.8k
    case TYPE_YMM: \
1828
10.5k
      return prefix##_YMM0 + index; \
1829
41.8k
    case TYPE_XMM: \
1830
19.2k
      return prefix##_XMM0 + index; \
1831
41.8k
    case TYPE_VK: \
1832
2.10k
      index &= 0xf; \
1833
2.10k
      if (index > 7) \
1834
2.10k
        *valid = 0; \
1835
2.10k
      return prefix##_K0 + index; \
1836
41.8k
    case TYPE_MM64: \
1837
2.46k
      return prefix##_MM0 + (index & 0x7); \
1838
41.8k
    case TYPE_SEGMENTREG: \
1839
0
      if ((index & 7) > 5) \
1840
0
        *valid = 0; \
1841
0
      return prefix##_ES + (index & 7); \
1842
41.8k
    case TYPE_DEBUGREG: \
1843
0
      return prefix##_DR0 + index; \
1844
41.8k
    case TYPE_CONTROLREG: \
1845
0
      return prefix##_CR0 + index; \
1846
41.8k
    case TYPE_BNDR: \
1847
1.05k
      if (index > 3) \
1848
1.05k
        *valid = 0; \
1849
1.05k
      return prefix##_BND0 + index; \
1850
41.8k
    case TYPE_MVSIBX: \
1851
0
      return prefix##_XMM0 + index; \
1852
41.8k
    case TYPE_MVSIBY: \
1853
0
      return prefix##_YMM0 + index; \
1854
41.8k
    case TYPE_MVSIBZ: \
1855
0
      return prefix##_ZMM0 + index; \
1856
144k
    } \
1857
144k
  }
1858
1859
/*
1860
 * fixup*Value - Consults an operand type to determine the meaning of the
1861
 *   reg or R/M field.  If the operand is an XMM operand, for example, an
1862
 *   operand would be XMM0 instead of AX, which readModRM() would otherwise
1863
 *   misinterpret it as.
1864
 *
1865
 * @param insn  - The instruction containing the operand.
1866
 * @param type  - The operand type.
1867
 * @param index - The existing value of the field as reported by readModRM().
1868
 * @param valid - The address of a uint8_t.  The target is set to 1 if the
1869
 *                field is valid for the register class; 0 if not.
1870
 * @return      - The proper value.
1871
 */
1872
GENERIC_FIXUP_FUNC(fixupRegValue, insn->regBase, MODRM_REG, 0x1f)
1873
GENERIC_FIXUP_FUNC(fixupRMValue, insn->eaRegBase, EA_REG, 0xf)
1874
1875
/*
1876
 * fixupReg - Consults an operand specifier to determine which of the
1877
 *   fixup*Value functions to use in correcting readModRM()'ss interpretation.
1878
 *
1879
 * @param insn  - See fixup*Value().
1880
 * @param op    - The operand specifier.
1881
 * @return      - 0 if fixup was successful; -1 if the register returned was
1882
 *                invalid for its class.
1883
 */
1884
static int fixupReg(struct InternalInstruction *insn,
1885
        const struct OperandSpecifier *op)
1886
1.13M
{
1887
1.13M
  uint8_t valid;
1888
1889
1.13M
  switch ((OperandEncoding)op->encoding) {
1890
0
  default:
1891
    // debug("Expected a REG or R/M encoding in fixupReg");
1892
0
    return -1;
1893
75.6k
  case ENCODING_VVVV:
1894
75.6k
    insn->vvvv = (Reg)fixupRegValue(insn, (OperandType)op->type,
1895
75.6k
            insn->vvvv, &valid);
1896
75.6k
    if (!valid)
1897
3
      return -1;
1898
75.6k
    break;
1899
460k
  case ENCODING_REG:
1900
460k
    insn->reg = (Reg)fixupRegValue(insn, (OperandType)op->type,
1901
460k
                 insn->reg - insn->regBase,
1902
460k
                 &valid);
1903
460k
    if (!valid)
1904
45
      return -1;
1905
460k
    break;
1906
3.93M
CASE_ENCODING_RM:
1907
3.93M
    if (insn->eaBase >= insn->eaRegBase) {
1908
144k
      insn->eaBase = (EABase)fixupRMValue(
1909
144k
        insn, (OperandType)op->type,
1910
144k
        insn->eaBase - insn->eaRegBase, &valid);
1911
144k
      if (!valid)
1912
5
        return -1;
1913
144k
    }
1914
601k
    break;
1915
1.13M
  }
1916
1917
1.13M
  return 0;
1918
1.13M
}
1919
1920
/*
1921
 * readOpcodeRegister - Reads an operand from the opcode field of an
1922
 *   instruction and interprets it appropriately given the operand width.
1923
 *   Handles AddRegFrm instructions.
1924
 *
1925
 * @param insn  - the instruction whose opcode field is to be read.
1926
 * @param size  - The width (in bytes) of the register being specified.
1927
 *                1 means AL and friends, 2 means AX, 4 means EAX, and 8 means
1928
 *                RAX.
1929
 * @return      - 0 on success; nonzero otherwise.
1930
 */
1931
static int readOpcodeRegister(struct InternalInstruction *insn, uint8_t size)
1932
120k
{
1933
120k
  if (size == 0)
1934
89.6k
    size = insn->registerSize;
1935
1936
120k
  switch (size) {
1937
14.8k
  case 1:
1938
14.8k
    insn->opcodeRegister =
1939
14.8k
      (Reg)(MODRM_REG_AL + ((bFromREX(insn->rexPrefix) << 3) |
1940
14.8k
                (insn->opcode & 7)));
1941
14.8k
    if (insn->rexPrefix &&
1942
1.40k
        insn->opcodeRegister >= MODRM_REG_AL + 0x4 &&
1943
874
        insn->opcodeRegister < MODRM_REG_AL + 0x8) {
1944
371
      insn->opcodeRegister =
1945
371
        (Reg)(MODRM_REG_SPL + (insn->opcodeRegister -
1946
371
                   MODRM_REG_AL - 4));
1947
371
    }
1948
1949
14.8k
    break;
1950
43.0k
  case 2:
1951
43.0k
    insn->opcodeRegister =
1952
43.0k
      (Reg)(MODRM_REG_AX + ((bFromREX(insn->rexPrefix) << 3) |
1953
43.0k
                (insn->opcode & 7)));
1954
43.0k
    break;
1955
45.8k
  case 4:
1956
45.8k
    insn->opcodeRegister = (Reg)(MODRM_REG_EAX +
1957
45.8k
               ((bFromREX(insn->rexPrefix) << 3) |
1958
45.8k
                (insn->opcode & 7)));
1959
45.8k
    break;
1960
16.8k
  case 8:
1961
16.8k
    insn->opcodeRegister = (Reg)(MODRM_REG_RAX +
1962
16.8k
               ((bFromREX(insn->rexPrefix) << 3) |
1963
16.8k
                (insn->opcode & 7)));
1964
16.8k
    break;
1965
120k
  }
1966
1967
120k
  return 0;
1968
120k
}
1969
1970
/*
1971
 * readImmediate - Consumes an immediate operand from an instruction, given the
1972
 *   desired operand size.
1973
 *
1974
 * @param insn  - The instruction whose operand is to be read.
1975
 * @param size  - The width (in bytes) of the operand.
1976
 * @return      - 0 if the immediate was successfully consumed; nonzero
1977
 *                otherwise.
1978
 */
1979
static int readImmediate(struct InternalInstruction *insn, uint8_t size)
1980
319k
{
1981
319k
  uint8_t imm8;
1982
319k
  uint16_t imm16;
1983
319k
  uint32_t imm32;
1984
319k
  uint64_t imm64;
1985
1986
319k
  if (insn->numImmediatesConsumed == 2) {
1987
    // debug("Already consumed two immediates");
1988
0
    return -1;
1989
0
  }
1990
1991
319k
  if (size == 0)
1992
0
    size = insn->immediateSize;
1993
319k
  else
1994
319k
    insn->immediateSize = size;
1995
1996
319k
  insn->immediateOffset = insn->readerCursor - insn->startLocation;
1997
1998
319k
  switch (size) {
1999
238k
  case 1:
2000
238k
    if (consumeByte(insn, &imm8))
2001
622
      return -1;
2002
2003
237k
    insn->immediates[insn->numImmediatesConsumed] = imm8;
2004
237k
    break;
2005
44.2k
  case 2:
2006
44.2k
    if (consumeUInt16(insn, &imm16))
2007
268
      return -1;
2008
2009
44.0k
    insn->immediates[insn->numImmediatesConsumed] = imm16;
2010
44.0k
    break;
2011
32.4k
  case 4:
2012
32.4k
    if (consumeUInt32(insn, &imm32))
2013
482
      return -1;
2014
2015
31.9k
    insn->immediates[insn->numImmediatesConsumed] = imm32;
2016
31.9k
    break;
2017
4.54k
  case 8:
2018
4.54k
    if (consumeUInt64(insn, &imm64))
2019
101
      return -1;
2020
4.44k
    insn->immediates[insn->numImmediatesConsumed] = imm64;
2021
4.44k
    break;
2022
319k
  }
2023
2024
318k
  insn->numImmediatesConsumed++;
2025
2026
318k
  return 0;
2027
319k
}
2028
2029
/*
2030
 * readVVVV - Consumes vvvv from an instruction if it has a VEX prefix.
2031
 *
2032
 * @param insn  - The instruction whose operand is to be read.
2033
 * @return      - 0 if the vvvv was successfully consumed; nonzero
2034
 *                otherwise.
2035
 */
2036
static int readVVVV(struct InternalInstruction *insn)
2037
1.13M
{
2038
1.13M
  int vvvv;
2039
2040
1.13M
  if (insn->vectorExtensionType == TYPE_EVEX)
2041
78.6k
    vvvv = (v2FromEVEX4of4(insn->vectorExtensionPrefix[3]) << 4 |
2042
78.6k
      vvvvFromEVEX3of4(insn->vectorExtensionPrefix[2]));
2043
1.06M
  else if (insn->vectorExtensionType == TYPE_VEX_3B)
2044
6.72k
    vvvv = vvvvFromVEX3of3(insn->vectorExtensionPrefix[2]);
2045
1.05M
  else if (insn->vectorExtensionType == TYPE_VEX_2B)
2046
11.5k
    vvvv = vvvvFromVEX2of2(insn->vectorExtensionPrefix[1]);
2047
1.04M
  else if (insn->vectorExtensionType == TYPE_XOP)
2048
9.13k
    vvvv = vvvvFromXOP3of3(insn->vectorExtensionPrefix[2]);
2049
1.03M
  else
2050
1.03M
    return -1;
2051
2052
106k
  if (insn->mode != MODE_64BIT)
2053
66.5k
    vvvv &= 0xf; // Can only clear bit 4. Bit 3 must be cleared later.
2054
2055
106k
  insn->vvvv = (Reg)vvvv;
2056
2057
106k
  return 0;
2058
1.13M
}
2059
2060
/*
2061
 * readMaskRegister - Reads an mask register from the opcode field of an
2062
 *   instruction.
2063
 *
2064
 * @param insn    - The instruction whose opcode field is to be read.
2065
 * @return        - 0 on success; nonzero otherwise.
2066
 */
2067
static int readMaskRegister(struct InternalInstruction *insn)
2068
55.1k
{
2069
55.1k
  if (insn->vectorExtensionType != TYPE_EVEX)
2070
0
    return -1;
2071
2072
55.1k
  insn->writemask =
2073
55.1k
    (Reg)(aaaFromEVEX4of4(insn->vectorExtensionPrefix[3]));
2074
2075
55.1k
  return 0;
2076
55.1k
}
2077
2078
/*
2079
 * readOperands - Consults the specifier for an instruction and consumes all
2080
 *   operands for that instruction, interpreting them as it goes.
2081
 *
2082
 * @param insn  - The instruction whose operands are to be read and interpreted.
2083
 * @return      - 0 if all operands could be read; nonzero otherwise.
2084
 */
2085
static int readOperands(struct InternalInstruction *insn)
2086
1.13M
{
2087
1.13M
  int hasVVVV, needVVVV;
2088
1.13M
  int sawRegImm = 0;
2089
1.13M
  int i;
2090
2091
  /* If non-zero vvvv specified, need to make sure one of the operands
2092
     uses it. */
2093
1.13M
  hasVVVV = !readVVVV(insn);
2094
1.13M
  needVVVV = hasVVVV && (insn->vvvv != 0);
2095
2096
7.96M
  for (i = 0; i < X86_MAX_OPERANDS; ++i) {
2097
6.82M
    const OperandSpecifier *op =
2098
6.82M
      &x86OperandSets[insn->spec->operands][i];
2099
6.82M
    switch (op->encoding) {
2100
4.81M
    case ENCODING_NONE:
2101
4.86M
    case ENCODING_SI:
2102
4.93M
    case ENCODING_DI:
2103
4.93M
      break;
2104
2105
40.4k
CASE_ENCODING_VSIB:
2106
      // VSIB can use the V2 bit so check only the other bits.
2107
40.4k
      if (needVVVV)
2108
4.76k
        needVVVV = hasVVVV & ((insn->vvvv & 0xf) != 0);
2109
2110
40.4k
      if (readModRM(insn))
2111
0
        return -1;
2112
2113
      // Reject if SIB wasn't used.
2114
7.77k
      if (insn->eaBase != EA_BASE_sib &&
2115
5.30k
          insn->eaBase != EA_BASE_sib64)
2116
18
        return -1;
2117
2118
      // If sibIndex was set to SIB_INDEX_NONE, index offset is 4.
2119
7.75k
      if (insn->sibIndex == SIB_INDEX_NONE)
2120
788
        insn->sibIndex =
2121
788
          (SIBIndex)(insn->sibIndexBase + 4);
2122
2123
      // If EVEX.v2 is set this is one of the 16-31 registers.
2124
7.75k
      if (insn->vectorExtensionType == TYPE_EVEX &&
2125
6.14k
          insn->mode == MODE_64BIT &&
2126
4.31k
          v2FromEVEX4of4(insn->vectorExtensionPrefix[3]))
2127
3.16k
        insn->sibIndex =
2128
3.16k
          (SIBIndex)(insn->sibIndex + 16);
2129
2130
      // Adjust the index register to the correct size.
2131
7.75k
      switch (op->type) {
2132
0
      default:
2133
        // debug("Unhandled VSIB index type");
2134
0
        return -1;
2135
2.46k
      case TYPE_MVSIBX:
2136
2.46k
        insn->sibIndex =
2137
2.46k
          (SIBIndex)(SIB_INDEX_XMM0 +
2138
2.46k
               (insn->sibIndex -
2139
2.46k
                insn->sibIndexBase));
2140
2.46k
        break;
2141
2.95k
      case TYPE_MVSIBY:
2142
2.95k
        insn->sibIndex =
2143
2.95k
          (SIBIndex)(SIB_INDEX_YMM0 +
2144
2.95k
               (insn->sibIndex -
2145
2.95k
                insn->sibIndexBase));
2146
2.95k
        break;
2147
2.33k
      case TYPE_MVSIBZ:
2148
2.33k
        insn->sibIndex =
2149
2.33k
          (SIBIndex)(SIB_INDEX_ZMM0 +
2150
2.33k
               (insn->sibIndex -
2151
2.33k
                insn->sibIndexBase));
2152
2.33k
        break;
2153
7.75k
      }
2154
2155
      // Apply the AVX512 compressed displacement scaling factor.
2156
7.75k
      if (op->encoding != ENCODING_REG &&
2157
7.75k
          insn->eaDisplacement == EA_DISP_8)
2158
493
        insn->displacement *=
2159
493
          1 << (op->encoding - ENCODING_VSIB);
2160
7.75k
      break;
2161
2162
460k
    case ENCODING_REG:
2163
7.16M
CASE_ENCODING_RM:
2164
7.16M
      if (readModRM(insn))
2165
0
        return -1;
2166
2167
1.06M
      if (fixupReg(insn, op))
2168
50
        return -1;
2169
2170
      // Apply the AVX512 compressed displacement scaling factor.
2171
1.06M
      if (op->encoding != ENCODING_REG &&
2172
601k
          insn->eaDisplacement == EA_DISP_8)
2173
87.2k
        insn->displacement *=
2174
87.2k
          1 << (op->encoding - ENCODING_RM);
2175
1.06M
      break;
2176
2177
239k
    case ENCODING_IB:
2178
239k
      if (sawRegImm) {
2179
        /* Saw a register immediate so don't read again and instead split the
2180
             previous immediate.  FIXME: This is a hack. */
2181
1.37k
        insn->immediates[insn->numImmediatesConsumed] =
2182
1.37k
          insn->immediates
2183
1.37k
            [insn->numImmediatesConsumed -
2184
1.37k
             1] &
2185
1.37k
          0xf;
2186
1.37k
        ++insn->numImmediatesConsumed;
2187
1.37k
        break;
2188
1.37k
      }
2189
238k
      if (readImmediate(insn, 1))
2190
622
        return -1;
2191
237k
      if (op->type == TYPE_XMM || op->type == TYPE_YMM)
2192
2.71k
        sawRegImm = 1;
2193
237k
      break;
2194
2195
15.7k
    case ENCODING_IW:
2196
15.7k
      if (readImmediate(insn, 2))
2197
74
        return -1;
2198
15.7k
      break;
2199
2200
15.7k
    case ENCODING_ID:
2201
6.48k
      if (readImmediate(insn, 4))
2202
83
        return -1;
2203
6.40k
      break;
2204
2205
6.40k
    case ENCODING_IO:
2206
864
      if (readImmediate(insn, 8))
2207
12
        return -1;
2208
852
      break;
2209
2210
45.6k
    case ENCODING_Iv:
2211
45.6k
      if (readImmediate(insn, insn->immediateSize))
2212
526
        return -1;
2213
45.1k
      break;
2214
2215
45.1k
    case ENCODING_Ia:
2216
12.5k
      if (readImmediate(insn, insn->addressSize))
2217
156
        return -1;
2218
      /* Direct memory-offset (moffset) immediate will get mapped
2219
           to memory operand later. We want the encoding info to
2220
           reflect that as well. */
2221
12.3k
      insn->displacementOffset = insn->immediateOffset;
2222
12.3k
      insn->consumedDisplacement = true;
2223
12.3k
      insn->displacementSize = insn->immediateSize;
2224
12.3k
      insn->displacement =
2225
12.3k
        insn->immediates[insn->numImmediatesConsumed -
2226
12.3k
             1];
2227
12.3k
      insn->immediateOffset = 0;
2228
12.3k
      insn->immediateSize = 0;
2229
12.3k
      break;
2230
2231
4.32k
    case ENCODING_IRC:
2232
4.32k
      insn->RC =
2233
4.32k
        (l2FromEVEX4of4(insn->vectorExtensionPrefix[3])
2234
4.32k
         << 1) |
2235
4.32k
        lFromEVEX4of4(insn->vectorExtensionPrefix[3]);
2236
4.32k
      break;
2237
2238
14.8k
    case ENCODING_RB:
2239
14.8k
      if (readOpcodeRegister(insn, 1))
2240
0
        return -1;
2241
14.8k
      break;
2242
2243
14.8k
    case ENCODING_RW:
2244
0
      if (readOpcodeRegister(insn, 2))
2245
0
        return -1;
2246
0
      break;
2247
2248
0
    case ENCODING_RD:
2249
0
      if (readOpcodeRegister(insn, 4))
2250
0
        return -1;
2251
0
      break;
2252
2253
16.0k
    case ENCODING_RO:
2254
16.0k
      if (readOpcodeRegister(insn, 8))
2255
0
        return -1;
2256
16.0k
      break;
2257
2258
89.6k
    case ENCODING_Rv:
2259
89.6k
      if (readOpcodeRegister(insn, 0))
2260
0
        return -1;
2261
89.6k
      break;
2262
2263
89.6k
    case ENCODING_FP:
2264
7.34k
      break;
2265
2266
75.6k
    case ENCODING_VVVV:
2267
75.6k
      if (!hasVVVV)
2268
0
        return -1;
2269
2270
75.6k
      needVVVV =
2271
75.6k
        0; /* Mark that we have found a VVVV operand. */
2272
2273
75.6k
      if (insn->mode != MODE_64BIT)
2274
47.4k
        insn->vvvv = (Reg)(insn->vvvv & 0x7);
2275
2276
75.6k
      if (fixupReg(insn, op))
2277
3
        return -1;
2278
75.6k
      break;
2279
2280
75.6k
    case ENCODING_WRITEMASK:
2281
55.1k
      if (readMaskRegister(insn))
2282
0
        return -1;
2283
55.1k
      break;
2284
2285
244k
    case ENCODING_DUP:
2286
244k
      break;
2287
2288
0
    default:
2289
      // dbgprintf(insn, "Encountered an operand with an unknown encoding.");
2290
0
      return -1;
2291
6.82M
    }
2292
6.82M
  }
2293
2294
  /* If we didn't find ENCODING_VVVV operand, but non-zero vvvv present, fail */
2295
1.13M
  if (needVVVV)
2296
27
    return -1;
2297
2298
1.13M
  return 0;
2299
1.13M
}
2300
2301
// return True if instruction is illegal to use with prefixes
2302
// This also check & fix the isPrefixNN when a prefix is irrelevant.
2303
static bool checkPrefix(struct InternalInstruction *insn)
2304
1.13M
{
2305
  // LOCK prefix
2306
1.13M
  if (insn->hasLockPrefix) {
2307
54.2k
    switch (insn->instructionID) {
2308
340
    default:
2309
      // invalid LOCK
2310
340
      return true;
2311
2312
    // nop dword [rax]
2313
59
    case X86_NOOPL:
2314
2315
    // DEC
2316
201
    case X86_DEC16m:
2317
450
    case X86_DEC32m:
2318
769
    case X86_DEC64m:
2319
1.00k
    case X86_DEC8m:
2320
2321
    // ADC
2322
1.23k
    case X86_ADC16mi:
2323
1.50k
    case X86_ADC16mi8:
2324
1.87k
    case X86_ADC16mr:
2325
1.99k
    case X86_ADC32mi:
2326
2.57k
    case X86_ADC32mi8:
2327
2.81k
    case X86_ADC32mr:
2328
3.07k
    case X86_ADC64mi32:
2329
3.32k
    case X86_ADC64mi8:
2330
3.39k
    case X86_ADC64mr:
2331
3.66k
    case X86_ADC8mi:
2332
4.05k
    case X86_ADC8mi8:
2333
4.68k
    case X86_ADC8mr:
2334
5.05k
    case X86_ADC8rm:
2335
5.29k
    case X86_ADC16rm:
2336
5.44k
    case X86_ADC32rm:
2337
5.78k
    case X86_ADC64rm:
2338
2339
    // ADD
2340
5.99k
    case X86_ADD16mi:
2341
6.28k
    case X86_ADD16mi8:
2342
6.84k
    case X86_ADD16mr:
2343
7.33k
    case X86_ADD32mi:
2344
7.79k
    case X86_ADD32mi8:
2345
8.13k
    case X86_ADD32mr:
2346
8.28k
    case X86_ADD64mi32:
2347
8.72k
    case X86_ADD64mi8:
2348
9.06k
    case X86_ADD64mr:
2349
9.31k
    case X86_ADD8mi:
2350
9.74k
    case X86_ADD8mi8:
2351
10.8k
    case X86_ADD8mr:
2352
11.3k
    case X86_ADD8rm:
2353
12.0k
    case X86_ADD16rm:
2354
12.3k
    case X86_ADD32rm:
2355
12.6k
    case X86_ADD64rm:
2356
2357
    // AND
2358
12.9k
    case X86_AND16mi:
2359
13.3k
    case X86_AND16mi8:
2360
13.7k
    case X86_AND16mr:
2361
14.0k
    case X86_AND32mi:
2362
14.4k
    case X86_AND32mi8:
2363
14.8k
    case X86_AND32mr:
2364
15.4k
    case X86_AND64mi32:
2365
15.9k
    case X86_AND64mi8:
2366
16.5k
    case X86_AND64mr:
2367
16.8k
    case X86_AND8mi:
2368
17.2k
    case X86_AND8mi8:
2369
17.4k
    case X86_AND8mr:
2370
17.8k
    case X86_AND8rm:
2371
18.1k
    case X86_AND16rm:
2372
18.4k
    case X86_AND32rm:
2373
18.6k
    case X86_AND64rm:
2374
2375
    // BTC
2376
18.8k
    case X86_BTC16mi8:
2377
19.0k
    case X86_BTC16mr:
2378
19.3k
    case X86_BTC32mi8:
2379
19.6k
    case X86_BTC32mr:
2380
19.7k
    case X86_BTC64mi8:
2381
20.1k
    case X86_BTC64mr:
2382
2383
    // BTR
2384
20.3k
    case X86_BTR16mi8:
2385
20.6k
    case X86_BTR16mr:
2386
21.0k
    case X86_BTR32mi8:
2387
21.2k
    case X86_BTR32mr:
2388
21.7k
    case X86_BTR64mi8:
2389
22.0k
    case X86_BTR64mr:
2390
2391
    // BTS
2392
22.2k
    case X86_BTS16mi8:
2393
22.5k
    case X86_BTS16mr:
2394
22.8k
    case X86_BTS32mi8:
2395
23.1k
    case X86_BTS32mr:
2396
23.4k
    case X86_BTS64mi8:
2397
23.8k
    case X86_BTS64mr:
2398
2399
    // CMPXCHG
2400
24.4k
    case X86_CMPXCHG16B:
2401
24.6k
    case X86_CMPXCHG16rm:
2402
24.9k
    case X86_CMPXCHG32rm:
2403
25.1k
    case X86_CMPXCHG64rm:
2404
25.5k
    case X86_CMPXCHG8rm:
2405
25.7k
    case X86_CMPXCHG8B:
2406
2407
    // INC
2408
26.0k
    case X86_INC16m:
2409
26.2k
    case X86_INC32m:
2410
26.4k
    case X86_INC64m:
2411
26.6k
    case X86_INC8m:
2412
2413
    // NEG
2414
26.8k
    case X86_NEG16m:
2415
27.1k
    case X86_NEG32m:
2416
27.4k
    case X86_NEG64m:
2417
27.8k
    case X86_NEG8m:
2418
2419
    // NOT
2420
29.0k
    case X86_NOT16m:
2421
29.7k
    case X86_NOT32m:
2422
29.8k
    case X86_NOT64m:
2423
30.0k
    case X86_NOT8m:
2424
2425
    // OR
2426
30.3k
    case X86_OR16mi:
2427
30.9k
    case X86_OR16mi8:
2428
31.3k
    case X86_OR16mr:
2429
32.0k
    case X86_OR32mi:
2430
32.3k
    case X86_OR32mi8:
2431
32.8k
    case X86_OR32mr:
2432
32.9k
    case X86_OR64mi32:
2433
33.3k
    case X86_OR64mi8:
2434
33.5k
    case X86_OR64mr:
2435
33.7k
    case X86_OR8mi8:
2436
34.3k
    case X86_OR8mi:
2437
34.6k
    case X86_OR8mr:
2438
34.9k
    case X86_OR8rm:
2439
35.2k
    case X86_OR16rm:
2440
35.7k
    case X86_OR32rm:
2441
36.1k
    case X86_OR64rm:
2442
2443
    // SBB
2444
36.4k
    case X86_SBB16mi:
2445
36.6k
    case X86_SBB16mi8:
2446
36.9k
    case X86_SBB16mr:
2447
37.1k
    case X86_SBB32mi:
2448
37.5k
    case X86_SBB32mi8:
2449
37.8k
    case X86_SBB32mr:
2450
37.9k
    case X86_SBB64mi32:
2451
38.5k
    case X86_SBB64mi8:
2452
38.9k
    case X86_SBB64mr:
2453
39.5k
    case X86_SBB8mi:
2454
39.7k
    case X86_SBB8mi8:
2455
40.1k
    case X86_SBB8mr:
2456
2457
    // SUB
2458
40.3k
    case X86_SUB16mi:
2459
40.6k
    case X86_SUB16mi8:
2460
40.9k
    case X86_SUB16mr:
2461
41.3k
    case X86_SUB32mi:
2462
41.6k
    case X86_SUB32mi8:
2463
41.9k
    case X86_SUB32mr:
2464
42.6k
    case X86_SUB64mi32:
2465
43.1k
    case X86_SUB64mi8:
2466
43.6k
    case X86_SUB64mr:
2467
43.8k
    case X86_SUB8mi8:
2468
44.0k
    case X86_SUB8mi:
2469
44.5k
    case X86_SUB8mr:
2470
45.0k
    case X86_SUB8rm:
2471
45.2k
    case X86_SUB16rm:
2472
45.4k
    case X86_SUB32rm:
2473
45.6k
    case X86_SUB64rm:
2474
2475
    // XADD
2476
45.8k
    case X86_XADD16rm:
2477
46.1k
    case X86_XADD32rm:
2478
46.3k
    case X86_XADD64rm:
2479
46.7k
    case X86_XADD8rm:
2480
2481
    // XCHG
2482
47.1k
    case X86_XCHG16rm:
2483
47.6k
    case X86_XCHG32rm:
2484
47.8k
    case X86_XCHG64rm:
2485
47.9k
    case X86_XCHG8rm:
2486
2487
    // XOR
2488
48.2k
    case X86_XOR16mi:
2489
48.5k
    case X86_XOR16mi8:
2490
49.1k
    case X86_XOR16mr:
2491
49.4k
    case X86_XOR32mi:
2492
49.7k
    case X86_XOR32mi8:
2493
50.2k
    case X86_XOR32mr:
2494
50.5k
    case X86_XOR64mi32:
2495
51.1k
    case X86_XOR64mi8:
2496
51.2k
    case X86_XOR64mr:
2497
51.5k
    case X86_XOR8mi8:
2498
52.1k
    case X86_XOR8mi:
2499
52.4k
    case X86_XOR8mr:
2500
52.7k
    case X86_XOR8rm:
2501
53.2k
    case X86_XOR16rm:
2502
53.7k
    case X86_XOR32rm:
2503
53.9k
    case X86_XOR64rm:
2504
2505
      // this instruction can be used with LOCK prefix
2506
53.9k
      return false;
2507
54.2k
    }
2508
54.2k
  }
2509
2510
#if 0
2511
  // REPNE prefix
2512
  if (insn->repeatPrefix) {
2513
    // 0xf2 can be a part of instruction encoding, but not really a prefix.
2514
    // In such a case, clear it.
2515
    if (insn->twoByteEscape == 0x0f) {
2516
      insn->prefix0 = 0;
2517
    }
2518
  }
2519
#endif
2520
2521
  // no invalid prefixes
2522
1.08M
  return false;
2523
1.13M
}
2524
2525
/*
2526
 * decodeInstruction - Reads and interprets a full instruction provided by the
2527
 *   user.
2528
 *
2529
 * @param insn      - A pointer to the instruction to be populated.  Must be
2530
 *                    pre-allocated.
2531
 * @param reader    - The function to be used to read the instruction's bytes.
2532
 * @param readerArg - A generic argument to be passed to the reader to store
2533
 *                    any internal state.
2534
 * @param startLoc  - The address (in the reader's address space) of the first
2535
 *                    byte in the instruction.
2536
 * @param mode      - The mode (real mode, IA-32e, or IA-32e in 64-bit mode) to
2537
 *                    decode the instruction in.
2538
 * @return          - 0 if instruction is valid; nonzero if not.
2539
 */
2540
int decodeInstruction(struct InternalInstruction *insn, byteReader_t reader,
2541
          const void *readerArg, uint64_t startLoc,
2542
          DisassemblerMode mode)
2543
1.14M
{
2544
1.14M
  insn->reader = reader;
2545
1.14M
  insn->readerArg = readerArg;
2546
1.14M
  insn->startLocation = startLoc;
2547
1.14M
  insn->readerCursor = startLoc;
2548
1.14M
  insn->mode = mode;
2549
1.14M
  insn->numImmediatesConsumed = 0;
2550
2551
1.14M
  if (readPrefixes(insn) || readOpcode(insn) || getID(insn) ||
2552
1.14M
      insn->instructionID == 0 || checkPrefix(insn) || readOperands(insn))
2553
7.93k
    return -1;
2554
2555
1.13M
  insn->length = (size_t)(insn->readerCursor - insn->startLocation);
2556
2557
  // instruction length must be <= 15 to be valid
2558
1.13M
  if (insn->length > 15)
2559
55
    return -1;
2560
2561
1.13M
  if (insn->operandSize == 0)
2562
1.13M
    insn->operandSize = insn->registerSize;
2563
2564
1.13M
  insn->operands = &x86OperandSets[insn->spec->operands][0];
2565
2566
1.13M
  return 0;
2567
1.13M
}
2568
2569
#endif