Coverage Report

Created: 2026-03-13 06:50

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