Coverage Report

Created: 2026-07-16 06:55

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