Coverage Report

Created: 2026-04-12 06:30

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