Coverage Report

Created: 2026-09-03 07:09

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/capstonenext/arch/X86/X86DisassemblerDecoder.c
Line
Count
Source
1
/*===-- X86DisassemblerDecoder.c - Disassembler decoder ------------*- C -*-===*
2
 *
3
 *                     The LLVM Compiler Infrastructure
4
 *
5
 * This file is distributed under the University of Illinois Open Source
6
 * License. See LICENSE.TXT for details.
7
 *
8
 *===----------------------------------------------------------------------===*
9
 *
10
 * This file is part of the X86 Disassembler.
11
 * It contains the implementation of the instruction decoder.
12
 * Documentation for the disassembler can be found in X86Disassembler.h.
13
 *
14
 *===----------------------------------------------------------------------===*/
15
16
/* Capstone Disassembly Engine */
17
/* By Nguyen Anh Quynh <aquynh@gmail.com>, 2013-2019 */
18
19
#ifdef CAPSTONE_HAS_X86
20
21
#include <stdarg.h> /* for va_*()       */
22
#if defined(CAPSTONE_HAS_OSXKERNEL)
23
#include <libkern/libkern.h>
24
#else
25
#include <stdlib.h> /* for exit()       */
26
#endif
27
28
#include <string.h>
29
30
#include "../../cs_priv.h"
31
#include "../../utils.h"
32
33
#include "X86DisassemblerDecoder.h"
34
#include "X86Mapping.h"
35
36
/// Specifies whether a ModR/M byte is needed and (if so) which
37
/// instruction each possible value of the ModR/M byte corresponds to.  Once
38
/// this information is known, we have narrowed down to a single instruction.
39
struct ModRMDecision {
40
  uint8_t modrm_type;
41
  uint16_t instructionIDs;
42
};
43
44
/// Specifies which set of ModR/M->instruction tables to look at
45
/// given a particular opcode.
46
struct OpcodeDecision {
47
  struct ModRMDecision modRMDecisions[256];
48
};
49
50
/// Specifies which opcode->instruction tables to look at given
51
/// a particular context (set of attributes).  Since there are many possible
52
/// contexts, the decoder first uses CONTEXTS_SYM to determine which context
53
/// applies given a specific set of attributes.  Hence there are only IC_max
54
/// entries in this table, rather than 2^(ATTR_max).
55
struct ContextDecision {
56
  struct OpcodeDecision opcodeDecisions[IC_max];
57
};
58
59
#ifdef CAPSTONE_X86_REDUCE
60
#include "X86GenDisassemblerTables_reduce.inc"
61
#include "X86GenDisassemblerTables_reduce2.inc"
62
#include "X86Lookup16_reduce.inc"
63
#else
64
#include "X86GenDisassemblerTables.inc"
65
#include "X86GenDisassemblerTables2.inc"
66
#include "X86Lookup16.inc"
67
#endif
68
69
/*
70
 * contextForAttrs - Client for the instruction context table.  Takes a set of
71
 *   attributes and returns the appropriate decode context.
72
 *
73
 * @param attrMask  - Attributes, from the enumeration attributeBits.
74
 * @return          - The InstructionContext to use when looking up an
75
 *                    an instruction with these attributes.
76
 */
77
static InstructionContext contextForAttrs(uint16_t attrMask)
78
1.34M
{
79
1.34M
  return CONTEXTS_SYM[attrMask];
80
1.34M
}
81
82
/*
83
 * modRMRequired - Reads the appropriate instruction table to determine whether
84
 *   the ModR/M byte is required to decode a particular instruction.
85
 *
86
 * @param type        - The opcode type (i.e., how many bytes it has).
87
 * @param insnContext - The context for the instruction, as returned by
88
 *                      contextForAttrs.
89
 * @param opcode      - The last byte of the instruction's opcode, not counting
90
 *                      ModR/M extensions and escapes.
91
 * @return            - true if the ModR/M byte is required, false otherwise.
92
 */
93
static int modRMRequired(OpcodeType type, InstructionContext insnContext,
94
       uint16_t opcode)
95
1.34M
{
96
1.34M
  const struct OpcodeDecision *decision = NULL;
97
1.34M
  const uint8_t *indextable = NULL;
98
1.34M
  unsigned int index;
99
100
1.34M
  switch (type) {
101
0
  default:
102
0
    return false;
103
1.17M
  case ONEBYTE:
104
1.17M
    decision = ONEBYTE_SYM;
105
1.17M
    indextable = index_x86DisassemblerOneByteOpcodes;
106
1.17M
    break;
107
106k
  case TWOBYTE:
108
106k
    decision = TWOBYTE_SYM;
109
106k
    indextable = index_x86DisassemblerTwoByteOpcodes;
110
106k
    break;
111
23.7k
  case THREEBYTE_38:
112
23.7k
    decision = THREEBYTE38_SYM;
113
23.7k
    indextable = index_x86DisassemblerThreeByte38Opcodes;
114
23.7k
    break;
115
30.9k
  case THREEBYTE_3A:
116
30.9k
    decision = THREEBYTE3A_SYM;
117
30.9k
    indextable = index_x86DisassemblerThreeByte3AOpcodes;
118
30.9k
    break;
119
0
#ifndef CAPSTONE_X86_REDUCE
120
11.4k
  case XOP8_MAP:
121
11.4k
    decision = XOP8_MAP_SYM;
122
11.4k
    indextable = index_x86DisassemblerXOP8Opcodes;
123
11.4k
    break;
124
1.74k
  case XOP9_MAP:
125
1.74k
    decision = XOP9_MAP_SYM;
126
1.74k
    indextable = index_x86DisassemblerXOP9Opcodes;
127
1.74k
    break;
128
634
  case XOPA_MAP:
129
634
    decision = XOPA_MAP_SYM;
130
634
    indextable = index_x86DisassemblerXOPAOpcodes;
131
634
    break;
132
810
  case THREEDNOW_MAP:
133
    // 3DNow instructions always have ModRM byte
134
810
    return true;
135
1.34M
#endif
136
1.34M
  }
137
138
  // return decision->opcodeDecisions[insnContext].modRMDecisions[opcode].modrm_type != MODRM_ONEENTRY;
139
1.34M
  index = indextable[insnContext];
140
1.34M
  if (index)
141
1.34M
    return decision[index - 1].modRMDecisions[opcode].modrm_type !=
142
1.34M
           MODRM_ONEENTRY;
143
5.27k
  else
144
5.27k
    return false;
145
1.34M
}
146
147
/*
148
 * decode - Reads the appropriate instruction table to obtain the unique ID of
149
 *   an instruction.
150
 *
151
 * @param type        - See modRMRequired().
152
 * @param insnContext - See modRMRequired().
153
 * @param opcode      - See modRMRequired().
154
 * @param modRM       - The ModR/M byte if required, or any value if not.
155
 * @return            - The UID of the instruction, or 0 on failure.
156
 */
157
static InstrUID decode(OpcodeType type, InstructionContext insnContext,
158
           uint8_t opcode, uint8_t modRM)
159
1.34M
{
160
1.34M
  const struct ModRMDecision *dec = NULL;
161
1.34M
  unsigned int index;
162
1.34M
  static const struct OpcodeDecision emptyDecision = { 0 };
163
164
1.34M
  switch (type) {
165
0
  default:
166
0
    return 0;
167
1.16M
  case ONEBYTE:
168
    // dec = &ONEBYTE_SYM.opcodeDecisions[insnContext].modRMDecisions[opcode];
169
1.16M
    index = index_x86DisassemblerOneByteOpcodes[insnContext];
170
1.16M
    if (index)
171
1.16M
      dec = &ONEBYTE_SYM[index - 1].modRMDecisions[opcode];
172
335
    else
173
335
      dec = &emptyDecision.modRMDecisions[opcode];
174
1.16M
    break;
175
106k
  case TWOBYTE:
176
    //dec = &TWOBYTE_SYM.opcodeDecisions[insnContext].modRMDecisions[opcode];
177
106k
    index = index_x86DisassemblerTwoByteOpcodes[insnContext];
178
106k
    if (index)
179
104k
      dec = &TWOBYTE_SYM[index - 1].modRMDecisions[opcode];
180
2.46k
    else
181
2.46k
      dec = &emptyDecision.modRMDecisions[opcode];
182
106k
    break;
183
23.7k
  case THREEBYTE_38:
184
    // dec = &THREEBYTE38_SYM.opcodeDecisions[insnContext].modRMDecisions[opcode];
185
23.7k
    index = index_x86DisassemblerThreeByte38Opcodes[insnContext];
186
23.7k
    if (index)
187
23.6k
      dec = &THREEBYTE38_SYM[index - 1].modRMDecisions[opcode];
188
114
    else
189
114
      dec = &emptyDecision.modRMDecisions[opcode];
190
23.7k
    break;
191
30.9k
  case THREEBYTE_3A:
192
    //dec = &THREEBYTE3A_SYM.opcodeDecisions[insnContext].modRMDecisions[opcode];
193
30.9k
    index = index_x86DisassemblerThreeByte3AOpcodes[insnContext];
194
30.9k
    if (index)
195
30.7k
      dec = &THREEBYTE3A_SYM[index - 1].modRMDecisions[opcode];
196
260
    else
197
260
      dec = &emptyDecision.modRMDecisions[opcode];
198
30.9k
    break;
199
0
#ifndef CAPSTONE_X86_REDUCE
200
11.4k
  case XOP8_MAP:
201
    // dec = &XOP8_MAP_SYM.opcodeDecisions[insnContext].modRMDecisions[opcode];
202
11.4k
    index = index_x86DisassemblerXOP8Opcodes[insnContext];
203
11.4k
    if (index)
204
9.83k
      dec = &XOP8_MAP_SYM[index - 1].modRMDecisions[opcode];
205
1.56k
    else
206
1.56k
      dec = &emptyDecision.modRMDecisions[opcode];
207
11.4k
    break;
208
1.74k
  case XOP9_MAP:
209
    // dec = &XOP9_MAP_SYM.opcodeDecisions[insnContext].modRMDecisions[opcode];
210
1.74k
    index = index_x86DisassemblerXOP9Opcodes[insnContext];
211
1.74k
    if (index)
212
1.36k
      dec = &XOP9_MAP_SYM[index - 1].modRMDecisions[opcode];
213
375
    else
214
375
      dec = &emptyDecision.modRMDecisions[opcode];
215
1.74k
    break;
216
634
  case XOPA_MAP:
217
    // dec = &XOPA_MAP_SYM.opcodeDecisions[insnContext].modRMDecisions[opcode];
218
634
    index = index_x86DisassemblerXOPAOpcodes[insnContext];
219
634
    if (index)
220
471
      dec = &XOPA_MAP_SYM[index - 1].modRMDecisions[opcode];
221
163
    else
222
163
      dec = &emptyDecision.modRMDecisions[opcode];
223
634
    break;
224
810
  case THREEDNOW_MAP:
225
    // dec = &THREEDNOW_MAP_SYM.opcodeDecisions[insnContext].modRMDecisions[opcode];
226
810
    index = index_x86Disassembler3DNowOpcodes[insnContext];
227
810
    if (index)
228
494
      dec = &THREEDNOW_MAP_SYM[index - 1]
229
494
               .modRMDecisions[opcode];
230
316
    else
231
316
      dec = &emptyDecision.modRMDecisions[opcode];
232
810
    break;
233
1.34M
#endif
234
1.34M
  }
235
236
1.34M
  switch (dec->modrm_type) {
237
0
  default:
238
    // debug("Corrupt table!  Unknown modrm_type");
239
0
    return 0;
240
655k
  case MODRM_ONEENTRY:
241
655k
    return modRMTable[dec->instructionIDs];
242
518k
  case MODRM_SPLITRM:
243
518k
    if (modFromModRM(modRM) == 0x3)
244
112k
      return modRMTable[dec->instructionIDs + 1];
245
406k
    return modRMTable[dec->instructionIDs];
246
144k
  case MODRM_SPLITREG:
247
144k
    if (modFromModRM(modRM) == 0x3)
248
46.1k
      return modRMTable[dec->instructionIDs +
249
46.1k
            ((modRM & 0x38) >> 3) + 8];
250
98.2k
    return modRMTable[dec->instructionIDs + ((modRM & 0x38) >> 3)];
251
26.6k
  case MODRM_SPLITMISC:
252
26.6k
    if (modFromModRM(modRM) == 0x3)
253
9.02k
      return modRMTable[dec->instructionIDs + (modRM & 0x3f) +
254
9.02k
            8];
255
17.6k
    return modRMTable[dec->instructionIDs + ((modRM & 0x38) >> 3)];
256
0
  case MODRM_FULL:
257
0
    return modRMTable[dec->instructionIDs + modRM];
258
1.34M
  }
259
1.34M
}
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.14M
{
271
1.14M
  return &INSTRUCTIONS_SYM[uid];
272
1.14M
}
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
3.21M
{
286
3.21M
  int ret = insn->reader(insn->readerArg, byte, insn->readerCursor);
287
288
3.21M
  if (!ret)
289
3.20M
    ++(insn->readerCursor);
290
291
3.21M
  return ret;
292
3.21M
}
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
181k
{
303
181k
  return insn->reader(insn->readerArg, byte, insn->readerCursor);
304
181k
}
305
306
static void unconsumeByte(struct InternalInstruction *insn)
307
947k
{
308
947k
  insn->readerCursor--;
309
947k
}
310
311
#define CONSUME_FUNC(name, type) \
312
  static int name(struct InternalInstruction *insn, type *ptr) \
313
209k
  { \
314
209k
    type combined = 0; \
315
209k
    unsigned offset; \
316
692k
    for (offset = 0; offset < sizeof(type); ++offset) { \
317
484k
      uint8_t byte; \
318
484k
      int ret = insn->reader(insn->readerArg, &byte, \
319
484k
                 insn->readerCursor + offset); \
320
484k
      if (ret) \
321
484k
        return ret; \
322
484k
      combined = combined | \
323
483k
           ((uint64_t)byte << (offset * 8)); \
324
483k
    } \
325
209k
    *ptr = combined; \
326
208k
    insn->readerCursor += sizeof(type); \
327
208k
    return 0; \
328
209k
  }
X86DisassemblerDecoder.c:consumeInt8
Line
Count
Source
313
82.8k
  { \
314
82.8k
    type combined = 0; \
315
82.8k
    unsigned offset; \
316
165k
    for (offset = 0; offset < sizeof(type); ++offset) { \
317
82.8k
      uint8_t byte; \
318
82.8k
      int ret = insn->reader(insn->readerArg, &byte, \
319
82.8k
                 insn->readerCursor + offset); \
320
82.8k
      if (ret) \
321
82.8k
        return ret; \
322
82.8k
      combined = combined | \
323
82.6k
           ((uint64_t)byte << (offset * 8)); \
324
82.6k
    } \
325
82.8k
    *ptr = combined; \
326
82.6k
    insn->readerCursor += sizeof(type); \
327
82.6k
    return 0; \
328
82.8k
  }
X86DisassemblerDecoder.c:consumeInt16
Line
Count
Source
313
20.0k
  { \
314
20.0k
    type combined = 0; \
315
20.0k
    unsigned offset; \
316
59.8k
    for (offset = 0; offset < sizeof(type); ++offset) { \
317
39.9k
      uint8_t byte; \
318
39.9k
      int ret = insn->reader(insn->readerArg, &byte, \
319
39.9k
                 insn->readerCursor + offset); \
320
39.9k
      if (ret) \
321
39.9k
        return ret; \
322
39.9k
      combined = combined | \
323
39.8k
           ((uint64_t)byte << (offset * 8)); \
324
39.8k
    } \
325
20.0k
    *ptr = combined; \
326
19.8k
    insn->readerCursor += sizeof(type); \
327
19.8k
    return 0; \
328
20.0k
  }
X86DisassemblerDecoder.c:consumeInt32
Line
Count
Source
313
33.8k
  { \
314
33.8k
    type combined = 0; \
315
33.8k
    unsigned offset; \
316
168k
    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
33.8k
    *ptr = combined; \
326
33.4k
    insn->readerCursor += sizeof(type); \
327
33.4k
    return 0; \
328
33.8k
  }
X86DisassemblerDecoder.c:consumeUInt16
Line
Count
Source
313
39.2k
  { \
314
39.2k
    type combined = 0; \
315
39.2k
    unsigned offset; \
316
117k
    for (offset = 0; offset < sizeof(type); ++offset) { \
317
78.4k
      uint8_t byte; \
318
78.4k
      int ret = insn->reader(insn->readerArg, &byte, \
319
78.4k
                 insn->readerCursor + offset); \
320
78.4k
      if (ret) \
321
78.4k
        return ret; \
322
78.4k
      combined = combined | \
323
78.2k
           ((uint64_t)byte << (offset * 8)); \
324
78.2k
    } \
325
39.2k
    *ptr = combined; \
326
39.0k
    insn->readerCursor += sizeof(type); \
327
39.0k
    return 0; \
328
39.2k
  }
X86DisassemblerDecoder.c:consumeUInt32
Line
Count
Source
313
29.8k
  { \
314
29.8k
    type combined = 0; \
315
29.8k
    unsigned offset; \
316
148k
    for (offset = 0; offset < sizeof(type); ++offset) { \
317
118k
      uint8_t byte; \
318
118k
      int ret = insn->reader(insn->readerArg, &byte, \
319
118k
                 insn->readerCursor + offset); \
320
118k
      if (ret) \
321
118k
        return ret; \
322
118k
      combined = combined | \
323
118k
           ((uint64_t)byte << (offset * 8)); \
324
118k
    } \
325
29.8k
    *ptr = combined; \
326
29.4k
    insn->readerCursor += sizeof(type); \
327
29.4k
    return 0; \
328
29.8k
  }
X86DisassemblerDecoder.c:consumeUInt64
Line
Count
Source
313
3.73k
  { \
314
3.73k
    type combined = 0; \
315
3.73k
    unsigned offset; \
316
33.2k
    for (offset = 0; offset < sizeof(type); ++offset) { \
317
29.5k
      uint8_t byte; \
318
29.5k
      int ret = insn->reader(insn->readerArg, &byte, \
319
29.5k
                 insn->readerCursor + offset); \
320
29.5k
      if (ret) \
321
29.5k
        return ret; \
322
29.5k
      combined = combined | \
323
29.4k
           ((uint64_t)byte << (offset * 8)); \
324
29.4k
    } \
325
3.73k
    *ptr = combined; \
326
3.65k
    insn->readerCursor += sizeof(type); \
327
3.65k
    return 0; \
328
3.73k
  }
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.11M
{
349
1.11M
  if (insn->mode == MODE_64BIT)
350
421k
    return prefix >= 0x40 && prefix <= 0x4f;
351
352
692k
  return false;
353
1.11M
}
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
102k
{
363
102k
  switch (prefix) {
364
38.0k
  case 0xf0: // LOCK
365
38.0k
    insn->hasLockPrefix = true;
366
38.0k
    break;
367
368
35.6k
  case 0xf2: // REPNE/REPNZ
369
64.0k
  case 0xf3: // REP or REPE/REPZ
370
64.0k
    insn->repeatPrefix = prefix;
371
64.0k
    break;
372
102k
  }
373
102k
}
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.8k
{
385
  // In 32-bit or 16-bit mode all segment override prefixes are used.
386
32.8k
  if (insn->mode != MODE_64BIT) {
387
18.3k
    insn->segmentOverride = prefix;
388
18.3k
    insn->prefix1 = byte;
389
18.3k
    return;
390
18.3k
  }
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
14.5k
  switch (insn->prefix1) {
396
1.49k
  case 0x64: // FS
397
2.43k
  case 0x65: // GS
398
2.43k
    return;
399
14.5k
  }
400
401
  // If the proposed override is for FS or GS, mark it overridden.
402
  // All other segment prefixes are ignored.
403
12.1k
  switch (byte) {
404
2.93k
  case 0x64: // FS
405
5.78k
  case 0x65: // GS
406
5.78k
    insn->segmentOverride = prefix;
407
5.78k
    break;
408
12.1k
  }
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
12.1k
  insn->prefix1 = byte;
414
12.1k
}
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.00M
{
427
1.00M
  bool isPrefix = true;
428
1.00M
  uint8_t byte = 0;
429
1.00M
  uint8_t nextByte;
430
431
2.24M
  while (isPrefix) {
432
    /* If we fail reading prefixes, just stop here and let the opcode reader deal with it */
433
1.24M
    if (consumeByte(insn, &byte))
434
360
      return -1;
435
436
1.24M
    if (insn->readerCursor - 1 == insn->startLocation &&
437
1.00M
        (byte == 0xf2 || byte == 0xf3)) {
438
      // prefix requires next byte
439
46.1k
      if (lookAtByte(insn, &nextByte))
440
78
        return -1;
441
442
46.0k
      if (isREX(insn, nextByte)) {
443
4.32k
        uint8_t nnextByte;
444
445
        // Go to REX prefix after the current one
446
4.32k
        if (consumeByte(insn, &nnextByte))
447
0
          return -1;
448
449
        // We should be able to read next byte after REX prefix
450
4.32k
        if (lookAtByte(insn, &nnextByte))
451
9
          return -1;
452
453
4.31k
        unconsumeByte(insn);
454
4.31k
      }
455
46.0k
    }
456
457
1.24M
    switch (byte) {
458
38.0k
    case 0xf0: /* LOCK */
459
73.6k
    case 0xf2: /* REPNE/REPNZ */
460
102k
    case 0xf3: /* REP or REPE/REPZ */
461
      // only accept the last prefix
462
102k
      setGroup0Prefix(insn, byte);
463
102k
      insn->prefix0 = byte;
464
102k
      insn->rexPrefix = 0;
465
102k
      break;
466
467
5.07k
    case 0x2e: /* CS segment override -OR- Branch not taken */
468
9.34k
    case 0x36: /* SS segment override -OR- Branch taken */
469
13.5k
    case 0x3e: /* DS segment override */
470
16.4k
    case 0x26: /* ES segment override */
471
23.5k
    case 0x64: /* FS segment override */
472
32.8k
    case 0x65: /* GS segment override */
473
32.8k
      switch (byte) {
474
5.07k
      case 0x2e:
475
5.07k
        setSegmentOverride(insn, SEG_OVERRIDE_CS, byte);
476
5.07k
        break;
477
4.27k
      case 0x36:
478
4.27k
        setSegmentOverride(insn, SEG_OVERRIDE_SS, byte);
479
4.27k
        break;
480
4.16k
      case 0x3e:
481
4.16k
        setSegmentOverride(insn, SEG_OVERRIDE_DS, byte);
482
4.16k
        break;
483
2.94k
      case 0x26:
484
2.94k
        setSegmentOverride(insn, SEG_OVERRIDE_ES, byte);
485
2.94k
        break;
486
7.08k
      case 0x64:
487
7.08k
        setSegmentOverride(insn, SEG_OVERRIDE_FS, byte);
488
7.08k
        break;
489
9.34k
      case 0x65:
490
9.34k
        setSegmentOverride(insn, SEG_OVERRIDE_GS, byte);
491
9.34k
        break;
492
0
      default:
493
        // debug("Unhandled override");
494
0
        return -1;
495
32.8k
      }
496
32.8k
      insn->rexPrefix = 0;
497
32.8k
      break;
498
499
31.0k
    case 0x66: /* Operand-size override */
500
31.0k
      insn->hasOpSize = true;
501
31.0k
      insn->prefix2 = byte;
502
31.0k
      insn->rexPrefix = 0;
503
31.0k
      break;
504
505
11.1k
    case 0x67: /* Address-size override */
506
11.1k
      insn->hasAdSize = true;
507
11.1k
      insn->prefix3 = byte;
508
11.1k
      insn->rexPrefix = 0;
509
11.1k
      break;
510
1.06M
    default:
511
1.06M
      if (isREX(insn, byte)) {
512
        /* REX prefix byte */
513
66.0k
        insn->rexPrefix = byte;
514
1.00M
      } else {
515
        /* Not a prefix byte */
516
1.00M
        isPrefix = false;
517
1.00M
      }
518
1.06M
      break;
519
1.24M
    }
520
1.24M
  }
521
522
1.00M
  insn->vectorExtensionType = TYPE_NO_VEX_XOP;
523
524
1.00M
  if (byte == 0x62) {
525
50.6k
    uint8_t byte1, byte2;
526
527
50.6k
    if (consumeByte(insn, &byte1)) {
528
      // dbgprintf(insn, "Couldn't read second byte of EVEX prefix");
529
68
      return -1;
530
68
    }
531
532
50.5k
    if (lookAtByte(insn, &byte2)) {
533
      // dbgprintf(insn, "Couldn't read third byte of EVEX prefix");
534
48
      unconsumeByte(insn); /* unconsume byte1 */
535
48
      unconsumeByte(insn); /* unconsume byte  */
536
50.4k
    } else {
537
50.4k
      if ((insn->mode == MODE_64BIT ||
538
30.2k
           (byte1 & 0xc0) == 0xc0) &&
539
44.2k
          ((~byte1 & 0xc) == 0xc) && ((byte2 & 0x4) == 0x4)) {
540
44.0k
        insn->vectorExtensionType = TYPE_EVEX;
541
44.0k
      } else {
542
6.44k
        unconsumeByte(insn); /* unconsume byte1 */
543
6.44k
        unconsumeByte(insn); /* unconsume byte  */
544
6.44k
      }
545
50.4k
    }
546
547
50.5k
    if (insn->vectorExtensionType == TYPE_EVEX) {
548
44.0k
      insn->vectorExtensionPrefix[0] = byte;
549
44.0k
      insn->vectorExtensionPrefix[1] = byte1;
550
44.0k
      if (consumeByte(insn,
551
44.0k
          &insn->vectorExtensionPrefix[2])) {
552
        // dbgprintf(insn, "Couldn't read third byte of EVEX prefix");
553
0
        return -1;
554
0
      }
555
556
44.0k
      if (consumeByte(insn,
557
44.0k
          &insn->vectorExtensionPrefix[3])) {
558
        // dbgprintf(insn, "Couldn't read fourth byte of EVEX prefix");
559
24
        return -1;
560
24
      }
561
562
      /* We simulate the REX prefix for simplicity's sake */
563
44.0k
      if (insn->mode == MODE_64BIT) {
564
20.0k
        insn->rexPrefix =
565
20.0k
          0x40 |
566
20.0k
          (wFromEVEX3of4(
567
20.0k
             insn->vectorExtensionPrefix[2])
568
20.0k
           << 3) |
569
20.0k
          (rFromEVEX2of4(
570
20.0k
             insn->vectorExtensionPrefix[1])
571
20.0k
           << 2) |
572
20.0k
          (xFromEVEX2of4(
573
20.0k
             insn->vectorExtensionPrefix[1])
574
20.0k
           << 1) |
575
20.0k
          (bFromEVEX2of4(
576
20.0k
             insn->vectorExtensionPrefix[1])
577
20.0k
           << 0);
578
20.0k
      }
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
44.0k
    }
584
951k
  } else if (byte == 0xc4) {
585
5.93k
    uint8_t byte1;
586
587
5.93k
    if (lookAtByte(insn, &byte1)) {
588
      // dbgprintf(insn, "Couldn't read second byte of VEX");
589
16
      return -1;
590
16
    }
591
592
5.92k
    if (insn->mode == MODE_64BIT || (byte1 & 0xc0) == 0xc0)
593
4.37k
      insn->vectorExtensionType = TYPE_VEX_3B;
594
1.54k
    else
595
1.54k
      unconsumeByte(insn);
596
597
5.92k
    if (insn->vectorExtensionType == TYPE_VEX_3B) {
598
4.37k
      insn->vectorExtensionPrefix[0] = byte;
599
4.37k
      consumeByte(insn, &insn->vectorExtensionPrefix[1]);
600
4.37k
      consumeByte(insn, &insn->vectorExtensionPrefix[2]);
601
602
      /* We simulate the REX prefix for simplicity's sake */
603
4.37k
      if (insn->mode == MODE_64BIT)
604
1.79k
        insn->rexPrefix =
605
1.79k
          0x40 |
606
1.79k
          (wFromVEX3of3(
607
1.79k
             insn->vectorExtensionPrefix[2])
608
1.79k
           << 3) |
609
1.79k
          (rFromVEX2of3(
610
1.79k
             insn->vectorExtensionPrefix[1])
611
1.79k
           << 2) |
612
1.79k
          (xFromVEX2of3(
613
1.79k
             insn->vectorExtensionPrefix[1])
614
1.79k
           << 1) |
615
1.79k
          (bFromVEX2of3(
616
1.79k
             insn->vectorExtensionPrefix[1])
617
1.79k
           << 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
4.37k
    }
623
945k
  } else if (byte == 0xc5) {
624
11.9k
    uint8_t byte1;
625
626
11.9k
    if (lookAtByte(insn, &byte1)) {
627
      // dbgprintf(insn, "Couldn't read second byte of VEX");
628
23
      return -1;
629
23
    }
630
631
11.9k
    if (insn->mode == MODE_64BIT || (byte1 & 0xc0) == 0xc0)
632
9.89k
      insn->vectorExtensionType = TYPE_VEX_2B;
633
2.08k
    else
634
2.08k
      unconsumeByte(insn);
635
636
11.9k
    if (insn->vectorExtensionType == TYPE_VEX_2B) {
637
9.89k
      insn->vectorExtensionPrefix[0] = byte;
638
9.89k
      consumeByte(insn, &insn->vectorExtensionPrefix[1]);
639
640
9.89k
      if (insn->mode == MODE_64BIT)
641
995
        insn->rexPrefix =
642
995
          0x40 |
643
995
          (rFromVEX2of2(
644
995
             insn->vectorExtensionPrefix[1])
645
995
           << 2);
646
647
9.89k
      switch (ppFromVEX2of2(insn->vectorExtensionPrefix[1])) {
648
3.95k
      default:
649
3.95k
        break;
650
5.93k
      case VEX_PREFIX_66:
651
5.93k
        insn->hasOpSize = true;
652
5.93k
        break;
653
9.89k
      }
654
655
      // dbgprintf(insn, "Found VEX prefix 0x%hhx 0x%hhx",
656
      //    insn->vectorExtensionPrefix[0],
657
      //    insn->vectorExtensionPrefix[1]);
658
9.89k
    }
659
933k
  } else if (byte == 0x8f) {
660
8.13k
    uint8_t byte1;
661
662
8.13k
    if (lookAtByte(insn, &byte1)) {
663
      // dbgprintf(insn, "Couldn't read second byte of XOP");
664
14
      return -1;
665
14
    }
666
667
8.11k
    if ((byte1 & 0x38) !=
668
8.11k
        0x0) /* 0 in these 3 bits is a POP instruction. */
669
6.77k
      insn->vectorExtensionType = TYPE_XOP;
670
1.34k
    else
671
1.34k
      unconsumeByte(insn);
672
673
8.11k
    if (insn->vectorExtensionType == TYPE_XOP) {
674
6.77k
      insn->vectorExtensionPrefix[0] = byte;
675
6.77k
      consumeByte(insn, &insn->vectorExtensionPrefix[1]);
676
6.77k
      consumeByte(insn, &insn->vectorExtensionPrefix[2]);
677
678
      /* We simulate the REX prefix for simplicity's sake */
679
6.77k
      if (insn->mode == MODE_64BIT)
680
1.82k
        insn->rexPrefix =
681
1.82k
          0x40 |
682
1.82k
          (wFromXOP3of3(
683
1.82k
             insn->vectorExtensionPrefix[2])
684
1.82k
           << 3) |
685
1.82k
          (rFromXOP2of3(
686
1.82k
             insn->vectorExtensionPrefix[1])
687
1.82k
           << 2) |
688
1.82k
          (xFromXOP2of3(
689
1.82k
             insn->vectorExtensionPrefix[1])
690
1.82k
           << 1) |
691
1.82k
          (bFromXOP2of3(
692
1.82k
             insn->vectorExtensionPrefix[1])
693
1.82k
           << 0);
694
695
6.77k
      switch (ppFromXOP3of3(insn->vectorExtensionPrefix[2])) {
696
6.74k
      default:
697
6.74k
        break;
698
6.74k
      case VEX_PREFIX_66:
699
24
        insn->hasOpSize = true;
700
24
        break;
701
6.77k
      }
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
6.77k
    }
707
8.11k
  } else
708
924k
    unconsumeByte(insn);
709
710
1.00M
  if (insn->repeatPrefix != 0) {
711
54.7k
    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.7k
    if ((insn->hasLockPrefix || ((nextByte & 0xfe) == 0x86 ||
722
53.3k
               (nextByte & 0xf8) == 0x90)) &&
723
2.28k
        nextByte != 0x90) {
724
1.60k
      insn->xAcquireRelease = insn->repeatPrefix;
725
1.60k
    }
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.7k
    if (insn->repeatPrefix == 0xf3 &&
734
25.6k
        (nextByte == 0x88 || nextByte == 0x89 || nextByte == 0xc6 ||
735
25.3k
         nextByte == 0xc7)) {
736
342
      insn->xAcquireRelease = insn->repeatPrefix;
737
342
    }
738
54.7k
  }
739
740
1.00M
  if (insn->mode == MODE_16BIT) {
741
317k
    insn->registerSize = (insn->hasOpSize ? 4 : 2);
742
317k
    insn->addressSize = (insn->hasAdSize ? 4 : 2);
743
317k
    insn->displacementSize = (insn->hasAdSize ? 4 : 2);
744
317k
    insn->immediateSize = (insn->hasOpSize ? 4 : 2);
745
317k
    insn->immSize = (insn->hasOpSize ? 4 : 2);
746
684k
  } else if (insn->mode == MODE_32BIT) {
747
344k
    insn->registerSize = (insn->hasOpSize ? 2 : 4);
748
344k
    insn->addressSize = (insn->hasAdSize ? 2 : 4);
749
344k
    insn->displacementSize = (insn->hasAdSize ? 2 : 4);
750
344k
    insn->immediateSize = (insn->hasOpSize ? 2 : 4);
751
344k
    insn->immSize = (insn->hasOpSize ? 2 : 4);
752
344k
  } else if (insn->mode == MODE_64BIT) {
753
339k
    if (insn->rexPrefix && wFromREX(insn->rexPrefix)) {
754
50.1k
      insn->registerSize = 8;
755
50.1k
      insn->addressSize = (insn->hasAdSize ? 4 : 8);
756
50.1k
      insn->displacementSize = 4;
757
50.1k
      insn->immediateSize = 4;
758
50.1k
      insn->immSize = 4;
759
289k
    } else {
760
289k
      insn->registerSize = (insn->hasOpSize ? 2 : 4);
761
289k
      insn->addressSize = (insn->hasAdSize ? 4 : 8);
762
289k
      insn->displacementSize = (insn->hasOpSize ? 2 : 4);
763
289k
      insn->immediateSize = (insn->hasOpSize ? 2 : 4);
764
289k
      insn->immSize = (insn->hasOpSize ? 4 : 8);
765
289k
    }
766
339k
  }
767
768
1.00M
  return 0;
769
1.00M
}
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.00M
{
782
1.00M
  uint8_t current;
783
784
  // dbgprintf(insn, "readOpcode()");
785
786
1.00M
  insn->opcodeType = ONEBYTE;
787
788
1.00M
  if (insn->vectorExtensionType == TYPE_EVEX) {
789
44.0k
    switch (mmFromEVEX2of4(insn->vectorExtensionPrefix[1])) {
790
7
    default:
791
      // dbgprintf(insn, "Unhandled mm field for instruction (0x%hhx)",
792
      //    mmFromEVEX2of4(insn->vectorExtensionPrefix[1]));
793
7
      return -1;
794
15.1k
    case VEX_LOB_0F:
795
15.1k
      insn->opcodeType = TWOBYTE;
796
15.1k
      return consumeByte(insn, &insn->opcode);
797
13.1k
    case VEX_LOB_0F38:
798
13.1k
      insn->opcodeType = THREEBYTE_38;
799
13.1k
      return consumeByte(insn, &insn->opcode);
800
15.7k
    case VEX_LOB_0F3A:
801
15.7k
      insn->opcodeType = THREEBYTE_3A;
802
15.7k
      return consumeByte(insn, &insn->opcode);
803
44.0k
    }
804
957k
  } else if (insn->vectorExtensionType == TYPE_VEX_3B) {
805
4.37k
    switch (mmmmmFromVEX2of3(insn->vectorExtensionPrefix[1])) {
806
36
    default:
807
      // dbgprintf(insn, "Unhandled m-mmmm field for instruction (0x%hhx)",
808
      //    mmmmmFromVEX2of3(insn->vectorExtensionPrefix[1]));
809
36
      return -1;
810
317
    case VEX_LOB_0F:
811
      //insn->twoByteEscape = 0x0f;
812
317
      insn->opcodeType = TWOBYTE;
813
317
      return consumeByte(insn, &insn->opcode);
814
2.92k
    case VEX_LOB_0F38:
815
      //insn->twoByteEscape = 0x0f;
816
2.92k
      insn->opcodeType = THREEBYTE_38;
817
2.92k
      return consumeByte(insn, &insn->opcode);
818
1.09k
    case VEX_LOB_0F3A:
819
      //insn->twoByteEscape = 0x0f;
820
1.09k
      insn->opcodeType = THREEBYTE_3A;
821
1.09k
      return consumeByte(insn, &insn->opcode);
822
4.37k
    }
823
953k
  } else if (insn->vectorExtensionType == TYPE_VEX_2B) {
824
    //insn->twoByteEscape = 0x0f;
825
9.89k
    insn->opcodeType = TWOBYTE;
826
9.89k
    return consumeByte(insn, &insn->opcode);
827
943k
  } else if (insn->vectorExtensionType == TYPE_XOP) {
828
6.77k
    switch (mmmmmFromXOP2of3(insn->vectorExtensionPrefix[1])) {
829
52
    default:
830
      // dbgprintf(insn, "Unhandled m-mmmm field for instruction (0x%hhx)",
831
      //    mmmmmFromVEX2of3(insn->vectorExtensionPrefix[1]));
832
52
      return -1;
833
5.83k
    case XOP_MAP_SELECT_8:
834
5.83k
      insn->opcodeType = XOP8_MAP;
835
5.83k
      return consumeByte(insn, &insn->opcode);
836
715
    case XOP_MAP_SELECT_9:
837
715
      insn->opcodeType = XOP9_MAP;
838
715
      return consumeByte(insn, &insn->opcode);
839
174
    case XOP_MAP_SELECT_A:
840
174
      insn->opcodeType = XOPA_MAP;
841
174
      return consumeByte(insn, &insn->opcode);
842
6.77k
    }
843
6.77k
  }
844
845
936k
  if (consumeByte(insn, &current))
846
0
    return -1;
847
848
  // save this first byte for MOVcr, MOVdr, MOVrc, MOVrd
849
936k
  insn->firstByte = current;
850
851
936k
  if (current == 0x0f) {
852
    // dbgprintf(insn, "Found a two-byte escape prefix (0x%hhx)", current);
853
48.7k
    insn->twoByteEscape = current;
854
855
48.7k
    if (consumeByte(insn, &current))
856
98
      return -1;
857
858
48.6k
    if (current == 0x38) {
859
      // dbgprintf(insn, "Found a three-byte escape prefix (0x%hhx)", current);
860
865
      if (consumeByte(insn, &current))
861
1
        return -1;
862
863
864
      insn->opcodeType = THREEBYTE_38;
864
47.7k
    } else if (current == 0x3a) {
865
      // dbgprintf(insn, "Found a three-byte escape prefix (0x%hhx)", current);
866
241
      if (consumeByte(insn, &current))
867
1
        return -1;
868
869
240
      insn->opcodeType = THREEBYTE_3A;
870
47.5k
    } 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
458
      if (readModRM(insn))
874
13
        return -1;
875
876
445
      if (consumeByte(insn, &current))
877
3
        return -1;
878
879
442
      insn->opcodeType = THREEDNOW_MAP;
880
47.0k
    } else {
881
      // dbgprintf(insn, "Didn't find a three-byte escape prefix");
882
47.0k
      insn->opcodeType = TWOBYTE;
883
47.0k
    }
884
48.6k
  }
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
936k
  insn->opcode = current;
892
893
936k
  return 0;
894
936k
}
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
1.34M
{
920
1.34M
  bool hasModRMExtension;
921
922
1.34M
  InstructionContext instructionClass = contextForAttrs(attrMask);
923
924
1.34M
  hasModRMExtension =
925
1.34M
    modRMRequired(insn->opcodeType, instructionClass, insn->opcode);
926
927
1.34M
  if (hasModRMExtension) {
928
691k
    if (readModRM(insn))
929
1.83k
      return -1;
930
931
689k
    *instructionID = decode(insn->opcodeType, instructionClass,
932
689k
          insn->opcode, insn->modRM);
933
689k
  } else {
934
655k
    *instructionID = decode(insn->opcodeType, instructionClass,
935
655k
          insn->opcode, 0);
936
655k
  }
937
938
1.34M
  return 0;
939
1.34M
}
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
303k
{
950
303k
  size_t i;
951
303k
  uint16_t idx;
952
953
303k
  if ((idx = x86_16_bit_eq_lookup[orig]) != 0) {
954
154k
    for (i = idx - 1; i < ARR_SIZE(x86_16_bit_eq_tbl) &&
955
154k
          x86_16_bit_eq_tbl[i].first == orig;
956
151k
         i++) {
957
151k
      if (x86_16_bit_eq_tbl[i].second == equiv)
958
148k
        return true;
959
151k
    }
960
151k
  }
961
962
155k
  return false;
963
303k
}
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
16.8k
{
972
16.8k
  unsigned int i = find_insn(id);
973
16.8k
  if (i != -1) {
974
16.7k
    return insns[i].is64bit;
975
16.7k
  }
976
977
  // not found??
978
95
  return false;
979
16.8k
}
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.69k
{
1015
3.69k
  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.69k
  switch (insn->opcodeType) {
1020
  // No one-byte opcodes have mandatory prefixes.
1021
281
  case ONEBYTE:
1022
281
    resolution = DO_NOT_RESOLVE;
1023
281
    break;
1024
2.90k
  case TWOBYTE:
1025
    // Exceptions for instructions that operate on data size-overridable
1026
    // operands.
1027
2.90k
    if (
1028
      // XADD
1029
2.90k
      (insn->opcode & 0xFE) == 0xC0
1030
1031
      // BSWAP
1032
2.81k
      || (insn->opcode & 0xF8) == 0xC8
1033
1034
      // CMPXCHG, LSS, BTR, LFS, LGS, MOVZX
1035
2.52k
      || (insn->opcode & 0xF8) == 0xB0
1036
1037
      // Group 16, various NOPs
1038
2.41k
      || (insn->opcode & 0xF8) == 0x18
1039
1040
      // UD0
1041
2.14k
      || insn->opcode == 0xFF) {
1042
845
      resolution = IGNORE_REP;
1043
845
      break;
1044
845
    }
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
2.06k
    switch (insn->opcode & 0xf0) {
1066
102
    case 0x10:
1067
116
    case 0x50:
1068
295
    case 0x60:
1069
611
    case 0x70:
1070
730
    case 0xC0:
1071
813
    case 0xD0:
1072
828
    case 0xE0:
1073
861
    case 0xF0:
1074
861
      resolution = IGNORE_DATA_SIZE;
1075
861
      break;
1076
69
    case 0x00:
1077
79
    case 0x20:
1078
482
    case 0x30:
1079
498
    case 0x40:
1080
866
    case 0x80:
1081
894
    case 0x90:
1082
1.01k
    case 0xA0:
1083
1.01k
      resolution = IGNORE_REP;
1084
1.01k
      break;
1085
190
    default: // 0xB0
1086
190
      resolution = DO_NOT_RESOLVE;
1087
190
      break;
1088
2.06k
    }
1089
2.06k
    break;
1090
2.06k
  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
197
    if ((insn->opcode & 0xF0) == 0xF0) {
1095
99
      resolution = IGNORE_DATA_SIZE;
1096
99
      break;
1097
99
    }
1098
1099
    // Do not need to be resolved, all REP+DATA16 combinations are UD
1100
    // or separately specified.
1101
98
    resolution = DO_NOT_RESOLVE;
1102
98
    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
309
  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
309
    resolution = IGNORE_DATA_SIZE;
1119
309
    break;
1120
3.69k
  }
1121
1122
3.69k
  switch (resolution) {
1123
1.85k
  case IGNORE_REP:
1124
1.85k
    return attrMask & ~(ATTR_XD | ATTR_XS);
1125
1.26k
  case IGNORE_DATA_SIZE:
1126
1.26k
    return attrMask & ~ATTR_OPSIZE;
1127
0
  default:
1128
569
  case DO_NOT_RESOLVE:
1129
569
    return attrMask;
1130
3.69k
  }
1131
3.69k
}
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.00M
{
1144
1.00M
  uint16_t attrMask;
1145
1.00M
  uint16_t instructionID;
1146
1.00M
  bool rexWOverridesOpSize;
1147
1148
  /* REX.W overrides the operand-sized prefix for near RET in 64-bit mode */
1149
1.00M
  rexWOverridesOpSize = insn->mode == MODE_64BIT && insn->hasOpSize &&
1150
15.7k
            insn->opcodeType == ONEBYTE &&
1151
11.6k
            (insn->opcode == 0xC2 || insn->opcode == 0xC3) &&
1152
61
            (insn->rexPrefix & 0x08);
1153
1154
1.00M
  attrMask = ATTR_NONE;
1155
1156
1.00M
  if (insn->mode == MODE_64BIT)
1157
339k
    attrMask |= ATTR_64BIT;
1158
1159
1.00M
  if (insn->vectorExtensionType != TYPE_NO_VEX_XOP) {
1160
64.8k
    attrMask |= (insn->vectorExtensionType == TYPE_EVEX) ?
1161
43.9k
            ATTR_EVEX :
1162
64.8k
            ATTR_VEX;
1163
1164
64.8k
    if (insn->vectorExtensionType == TYPE_EVEX) {
1165
43.9k
      switch (ppFromEVEX3of4(
1166
43.9k
        insn->vectorExtensionPrefix[2])) {
1167
36.0k
      case VEX_PREFIX_66:
1168
36.0k
        attrMask |= ATTR_OPSIZE;
1169
36.0k
        break;
1170
2.37k
      case VEX_PREFIX_F3:
1171
2.37k
        attrMask |= ATTR_XS;
1172
2.37k
        break;
1173
636
      case VEX_PREFIX_F2:
1174
636
        attrMask |= ATTR_XD;
1175
636
        break;
1176
43.9k
      }
1177
1178
43.9k
      if (zFromEVEX4of4(insn->vectorExtensionPrefix[3]))
1179
4.84k
        attrMask |= ATTR_EVEXKZ;
1180
43.9k
      if (bFromEVEX4of4(insn->vectorExtensionPrefix[3]))
1181
16.9k
        attrMask |= ATTR_EVEXB;
1182
43.9k
      if (aaaFromEVEX4of4(insn->vectorExtensionPrefix[3]))
1183
28.5k
        attrMask |= ATTR_EVEXK;
1184
43.9k
      if (lFromEVEX4of4(insn->vectorExtensionPrefix[3]))
1185
21.3k
        attrMask |= ATTR_EVEXL;
1186
43.9k
      if (l2FromEVEX4of4(insn->vectorExtensionPrefix[3]))
1187
18.3k
        attrMask |= ATTR_EVEXL2;
1188
43.9k
    } else if (insn->vectorExtensionType == TYPE_VEX_3B) {
1189
4.32k
      switch (ppFromVEX3of3(insn->vectorExtensionPrefix[2])) {
1190
3.95k
      case VEX_PREFIX_66:
1191
3.95k
        attrMask |= ATTR_OPSIZE;
1192
3.95k
        break;
1193
50
      case VEX_PREFIX_F3:
1194
50
        attrMask |= ATTR_XS;
1195
50
        break;
1196
226
      case VEX_PREFIX_F2:
1197
226
        attrMask |= ATTR_XD;
1198
226
        break;
1199
4.32k
      }
1200
1201
4.32k
      if (lFromVEX3of3(insn->vectorExtensionPrefix[2]))
1202
1.98k
        attrMask |= ATTR_VEXL;
1203
16.5k
    } else if (insn->vectorExtensionType == TYPE_VEX_2B) {
1204
9.87k
      switch (ppFromVEX2of2(insn->vectorExtensionPrefix[1])) {
1205
5.92k
      case VEX_PREFIX_66:
1206
5.92k
        attrMask |= ATTR_OPSIZE;
1207
5.92k
        break;
1208
890
      case VEX_PREFIX_F3:
1209
890
        attrMask |= ATTR_XS;
1210
890
        break;
1211
1.88k
      case VEX_PREFIX_F2:
1212
1.88k
        attrMask |= ATTR_XD;
1213
1.88k
        break;
1214
9.87k
      }
1215
1216
9.87k
      if (lFromVEX2of2(insn->vectorExtensionPrefix[1]))
1217
8.12k
        attrMask |= ATTR_VEXL;
1218
9.87k
    } else if (insn->vectorExtensionType == TYPE_XOP) {
1219
6.69k
      switch (ppFromXOP3of3(insn->vectorExtensionPrefix[2])) {
1220
12
      case VEX_PREFIX_66:
1221
12
        attrMask |= ATTR_OPSIZE;
1222
12
        break;
1223
6
      case VEX_PREFIX_F3:
1224
6
        attrMask |= ATTR_XS;
1225
6
        break;
1226
13
      case VEX_PREFIX_F2:
1227
13
        attrMask |= ATTR_XD;
1228
13
        break;
1229
6.69k
      }
1230
1231
6.69k
      if (lFromXOP3of3(insn->vectorExtensionPrefix[2]))
1232
714
        attrMask |= ATTR_VEXL;
1233
6.69k
    } else {
1234
0
      return -1;
1235
0
    }
1236
936k
  } else {
1237
936k
    if (insn->hasOpSize && insn->mode != MODE_16BIT &&
1238
22.6k
        !rexWOverridesOpSize) {
1239
22.5k
      attrMask |= ATTR_OPSIZE;
1240
22.5k
    }
1241
936k
    if (insn->hasAdSize)
1242
7.25k
      attrMask |= ATTR_ADSIZE;
1243
936k
    if (insn->opcodeType == ONEBYTE) {
1244
887k
      if (insn->repeatPrefix == 0xf3 &&
1245
18.7k
          (insn->opcode == 0x90))
1246
        // Special support for PAUSE
1247
636
        attrMask |= ATTR_XS;
1248
887k
    } else {
1249
48.5k
      if (insn->repeatPrefix == 0xf2)
1250
7.20k
        attrMask |= ATTR_XD;
1251
41.3k
      else if (insn->repeatPrefix == 0xf3)
1252
5.66k
        attrMask |= ATTR_XS;
1253
48.5k
    }
1254
1255
936k
    if ((attrMask & ATTR_OPSIZE) &&
1256
22.5k
        (attrMask & (ATTR_XD | ATTR_XS))) {
1257
3.69k
      attrMask =
1258
3.69k
        resolveMandatoryPrefixConflict(insn, attrMask);
1259
3.69k
    }
1260
936k
  }
1261
1262
1.00M
  if (insn->rexPrefix & 0x08) {
1263
50.0k
    attrMask |= ATTR_REXW;
1264
50.0k
    attrMask &= ~ATTR_ADSIZE;
1265
50.0k
  }
1266
1267
  /*
1268
   * JCXZ/JECXZ need special handling for 16-bit mode because the meaning
1269
   * of the AdSize prefix is inverted w.r.t. 32-bit mode.
1270
   */
1271
1.00M
  if (insn->mode == MODE_16BIT && insn->opcodeType == ONEBYTE &&
1272
285k
      insn->opcode == 0xE3)
1273
2.32k
    attrMask ^= ATTR_ADSIZE;
1274
1275
  /*
1276
   * In 64-bit mode all f64 superscripted opcodes ignore opcode size prefix
1277
   * CALL/JMP/JCC instructions need to ignore 0x66 and consume 4 bytes
1278
   */
1279
1.00M
  if ((insn->mode == MODE_64BIT) && insn->hasOpSize) {
1280
15.7k
    switch (insn->opcode) {
1281
529
    case 0xE8:
1282
969
    case 0xE9:
1283
      // Take care of psubsb and other mmx instructions.
1284
969
      if (insn->opcodeType == ONEBYTE) {
1285
639
        attrMask ^= ATTR_OPSIZE;
1286
639
        insn->immediateSize = 4;
1287
639
        insn->displacementSize = 4;
1288
639
      }
1289
969
      break;
1290
283
    case 0x82:
1291
805
    case 0x83:
1292
960
    case 0x84:
1293
1.08k
    case 0x85:
1294
1.30k
    case 0x86:
1295
2.03k
    case 0x87:
1296
2.24k
    case 0x88:
1297
2.49k
    case 0x89:
1298
2.78k
    case 0x8A:
1299
2.94k
    case 0x8B:
1300
3.07k
    case 0x8C:
1301
3.15k
    case 0x8D:
1302
3.32k
    case 0x8E:
1303
3.93k
    case 0x8F:
1304
      // Take care of lea and three byte ops.
1305
3.93k
      if (insn->opcodeType == TWOBYTE) {
1306
487
        attrMask ^= ATTR_OPSIZE;
1307
487
        insn->immediateSize = 4;
1308
487
        insn->displacementSize = 4;
1309
487
      }
1310
3.93k
      break;
1311
15.7k
    }
1312
15.7k
  }
1313
1314
  /* The following clauses compensate for limitations of the tables. */
1315
1.00M
  if (insn->mode != MODE_64BIT &&
1316
661k
      insn->vectorExtensionType != TYPE_NO_VEX_XOP) {
1317
40.2k
    if (getIDWithAttrMask(&instructionID, insn, attrMask)) {
1318
23
      return -1;
1319
23
    }
1320
1321
    /*
1322
     * The tables can't distinguish between cases where the W-bit is used to
1323
     * select register size and cases where it's a required part of the opcode.
1324
     */
1325
40.2k
    if ((insn->vectorExtensionType == TYPE_EVEX &&
1326
23.9k
         wFromEVEX3of4(insn->vectorExtensionPrefix[2])) ||
1327
25.2k
        (insn->vectorExtensionType == TYPE_VEX_3B &&
1328
2.55k
         wFromVEX3of3(insn->vectorExtensionPrefix[2])) ||
1329
23.5k
        (insn->vectorExtensionType == TYPE_XOP &&
1330
16.8k
         wFromXOP3of3(insn->vectorExtensionPrefix[2]))) {
1331
16.8k
      uint16_t instructionIDWithREXW;
1332
1333
16.8k
      if (getIDWithAttrMask(&instructionIDWithREXW, insn,
1334
16.8k
                attrMask | ATTR_REXW)) {
1335
5
        insn->instructionID = instructionID;
1336
5
        insn->spec = specifierForUID(instructionID);
1337
5
        return 0;
1338
5
      }
1339
1340
      // If not a 64-bit instruction. Switch the opcode.
1341
16.8k
      if (!is64Bit(instructionIDWithREXW)) {
1342
15.9k
        insn->instructionID = instructionIDWithREXW;
1343
15.9k
        insn->spec =
1344
15.9k
          specifierForUID(instructionIDWithREXW);
1345
1346
15.9k
        return 0;
1347
15.9k
      }
1348
16.8k
    }
1349
40.2k
  }
1350
1351
  /*
1352
   * Absolute moves, umonitor, and movdir64b need special handling.
1353
   * -For 16-bit mode because the meaning of the AdSize and OpSize prefixes are
1354
   *  inverted w.r.t.
1355
   * -For 32-bit mode we need to ensure the ADSIZE prefix is observed in
1356
   *  any position.
1357
   */
1358
985k
  if ((insn->opcodeType == ONEBYTE && ((insn->opcode & 0xFC) == 0xA0)) ||
1359
974k
      (insn->opcodeType == TWOBYTE && (insn->opcode == 0xAE)) ||
1360
974k
      (insn->opcodeType == THREEBYTE_38 && insn->opcode == 0xF8)) {
1361
    /* Make sure we observed the prefixes in any position. */
1362
11.3k
    if (insn->hasAdSize)
1363
239
      attrMask |= ATTR_ADSIZE;
1364
1365
11.3k
    if (insn->hasOpSize)
1366
187
      attrMask |= ATTR_OPSIZE;
1367
1368
    /* In 16-bit, invert the attributes. */
1369
11.3k
    if (insn->mode == MODE_16BIT) {
1370
4.33k
      attrMask ^= ATTR_ADSIZE;
1371
1372
      /* The OpSize attribute is only valid with the absolute moves. */
1373
4.33k
      if (insn->opcodeType == ONEBYTE &&
1374
3.54k
          ((insn->opcode & 0xFC) == 0xA0))
1375
3.54k
        attrMask ^= ATTR_OPSIZE;
1376
4.33k
    }
1377
1378
11.3k
    if (getIDWithAttrMask(&instructionID, insn, attrMask)) {
1379
3
      return -1;
1380
3
    }
1381
1382
11.3k
    insn->instructionID = instructionID;
1383
11.3k
    insn->spec = specifierForUID(instructionID);
1384
1385
11.3k
    return 0;
1386
11.3k
  }
1387
973k
  if (getIDWithAttrMask(&instructionID, insn, attrMask)) {
1388
1.80k
    return -1;
1389
1.80k
  }
1390
1391
972k
  if ((insn->mode == MODE_16BIT ||
1392
664k
       (insn->hasOpSize && !rexWOverridesOpSize)) &&
1393
332k
      !(attrMask & ATTR_OPSIZE)) {
1394
    /*
1395
     * The instruction tables make no distinction between instructions that
1396
     * allow OpSize anywhere (i.e., 16-bit operations) and that need it in a
1397
     * particular spot (i.e., many MMX operations).  In general we're
1398
     * conservative, but in the specific case where OpSize is present but not
1399
     * in the right place we check if there's a 16-bit operation.
1400
     */
1401
303k
    const struct InstructionSpecifier *spec;
1402
303k
    uint16_t instructionIDWithOpsize;
1403
1404
303k
    spec = specifierForUID(instructionID);
1405
1406
303k
    if (getIDWithAttrMask(&instructionIDWithOpsize, insn,
1407
303k
              attrMask | ATTR_OPSIZE)) {
1408
      /*
1409
       * ModRM required with OpSize but not present; give up and return version
1410
       * without OpSize set
1411
       */
1412
2
      insn->instructionID = instructionID;
1413
2
      insn->spec = spec;
1414
1415
2
      return 0;
1416
2
    }
1417
1418
303k
    if (is16BitEquivalent(instructionID, instructionIDWithOpsize) &&
1419
148k
        (insn->mode == MODE_16BIT) ^ insn->hasOpSize) {
1420
146k
      insn->instructionID = instructionIDWithOpsize;
1421
146k
      insn->spec = specifierForUID(instructionIDWithOpsize);
1422
157k
    } else {
1423
157k
      insn->instructionID = instructionID;
1424
157k
      insn->spec = spec;
1425
157k
    }
1426
1427
303k
    return 0;
1428
303k
  }
1429
1430
668k
  if (insn->opcodeType == ONEBYTE && insn->opcode == 0x90 &&
1431
3.12k
      insn->rexPrefix & 0x01) {
1432
    /*
1433
     * NOOP shouldn't decode as NOOP if REX.b is set. Instead
1434
     * it should decode as XCHG %r8, %eax.
1435
     */
1436
664
    const struct InstructionSpecifier *spec;
1437
664
    uint16_t instructionIDWithNewOpcode;
1438
664
    const struct InstructionSpecifier *specWithNewOpcode;
1439
1440
664
    spec = specifierForUID(instructionID);
1441
1442
    /* Borrow opcode from one of the other XCHGar opcodes */
1443
664
    insn->opcode = 0x91;
1444
1445
664
    if (getIDWithAttrMask(&instructionIDWithNewOpcode, insn,
1446
664
              attrMask)) {
1447
0
      insn->opcode = 0x90;
1448
1449
0
      insn->instructionID = instructionID;
1450
0
      insn->spec = spec;
1451
1452
0
      return 0;
1453
0
    }
1454
1455
664
    specWithNewOpcode = specifierForUID(instructionIDWithNewOpcode);
1456
1457
    /* Change back */
1458
664
    insn->opcode = 0x90;
1459
1460
664
    insn->instructionID = instructionIDWithNewOpcode;
1461
664
    insn->spec = specWithNewOpcode;
1462
1463
664
    return 0;
1464
664
  }
1465
1466
667k
  insn->instructionID = instructionID;
1467
667k
  insn->spec = specifierForUID(insn->instructionID);
1468
1469
667k
  return 0;
1470
668k
}
1471
1472
/*
1473
 * readSIB - Consumes the SIB byte to determine addressing information for an
1474
 *   instruction.
1475
 *
1476
 * @param insn  - The instruction whose SIB byte is to be read.
1477
 * @return      - 0 if the SIB byte was successfully read; nonzero otherwise.
1478
 */
1479
static int readSIB(struct InternalInstruction *insn)
1480
24.4k
{
1481
24.4k
  SIBBase sibBaseBase = SIB_BASE_NONE;
1482
24.4k
  uint8_t index, base;
1483
1484
  // dbgprintf(insn, "readSIB()");
1485
1486
24.4k
  if (insn->consumedSIB)
1487
0
    return 0;
1488
1489
24.4k
  insn->consumedSIB = true;
1490
1491
24.4k
  switch (insn->addressSize) {
1492
0
  case 2:
1493
    // dbgprintf(insn, "SIB-based addressing doesn't work in 16-bit mode");
1494
0
    return -1;
1495
10.8k
  case 4:
1496
10.8k
    insn->sibIndexBase = SIB_INDEX_EAX;
1497
10.8k
    sibBaseBase = SIB_BASE_EAX;
1498
10.8k
    break;
1499
13.6k
  case 8:
1500
13.6k
    insn->sibIndexBase = SIB_INDEX_RAX;
1501
13.6k
    sibBaseBase = SIB_BASE_RAX;
1502
13.6k
    break;
1503
24.4k
  }
1504
1505
24.4k
  if (consumeByte(insn, &insn->sib))
1506
48
    return -1;
1507
1508
24.4k
  index = indexFromSIB(insn->sib) | (xFromREX(insn->rexPrefix) << 3);
1509
1510
24.4k
  if (index == 0x4) {
1511
4.39k
    insn->sibIndex = SIB_INDEX_NONE;
1512
20.0k
  } else {
1513
20.0k
    insn->sibIndex = (SIBIndex)(insn->sibIndexBase + index);
1514
20.0k
  }
1515
1516
24.4k
  insn->sibScale = 1 << scaleFromSIB(insn->sib);
1517
1518
24.4k
  base = baseFromSIB(insn->sib) | (bFromREX(insn->rexPrefix) << 3);
1519
1520
24.4k
  switch (base) {
1521
2.96k
  case 0x5:
1522
3.82k
  case 0xd:
1523
3.82k
    switch (modFromModRM(insn->modRM)) {
1524
1.82k
    case 0x0:
1525
1.82k
      insn->eaDisplacement = EA_DISP_32;
1526
1.82k
      insn->sibBase = SIB_BASE_NONE;
1527
1.82k
      break;
1528
1.19k
    case 0x1:
1529
1.19k
      insn->eaDisplacement = EA_DISP_8;
1530
1.19k
      insn->sibBase = (SIBBase)(sibBaseBase + base);
1531
1.19k
      break;
1532
809
    case 0x2:
1533
809
      insn->eaDisplacement = EA_DISP_32;
1534
809
      insn->sibBase = (SIBBase)(sibBaseBase + base);
1535
809
      break;
1536
0
    case 0x3:
1537
      // debug("Cannot have Mod = 0b11 and a SIB byte");
1538
0
      return -1;
1539
3.82k
    }
1540
3.82k
    break;
1541
20.6k
  default:
1542
20.6k
    insn->sibBase = (SIBBase)(sibBaseBase + base);
1543
20.6k
    break;
1544
24.4k
  }
1545
1546
24.4k
  return 0;
1547
24.4k
}
1548
1549
/*
1550
 * readDisplacement - Consumes the displacement of an instruction.
1551
 *
1552
 * @param insn  - The instruction whose displacement is to be read.
1553
 * @return      - 0 if the displacement byte was successfully read; nonzero
1554
 *                otherwise.
1555
 */
1556
static int readDisplacement(struct InternalInstruction *insn)
1557
181k
{
1558
181k
  int8_t d8;
1559
181k
  int16_t d16;
1560
181k
  int32_t d32;
1561
1562
  // dbgprintf(insn, "readDisplacement()");
1563
1564
181k
  if (insn->consumedDisplacement)
1565
0
    return 0;
1566
1567
181k
  insn->consumedDisplacement = true;
1568
181k
  insn->displacementOffset = insn->readerCursor - insn->startLocation;
1569
1570
181k
  switch (insn->eaDisplacement) {
1571
45.1k
  case EA_DISP_NONE:
1572
45.1k
    insn->consumedDisplacement = false;
1573
45.1k
    break;
1574
82.8k
  case EA_DISP_8:
1575
82.8k
    if (consumeInt8(insn, &d8))
1576
156
      return -1;
1577
82.6k
    insn->displacement = d8;
1578
82.6k
    break;
1579
20.0k
  case EA_DISP_16:
1580
20.0k
    if (consumeInt16(insn, &d16))
1581
115
      return -1;
1582
19.8k
    insn->displacement = d16;
1583
19.8k
    break;
1584
33.8k
  case EA_DISP_32:
1585
33.8k
    if (consumeInt32(insn, &d32))
1586
353
      return -1;
1587
33.4k
    insn->displacement = d32;
1588
33.4k
    break;
1589
181k
  }
1590
1591
181k
  return 0;
1592
181k
}
1593
1594
/*
1595
 * readModRM - Consumes all addressing information (ModR/M byte, SIB byte, and
1596
 *   displacement) for an instruction and interprets it.
1597
 *
1598
 * @param insn  - The instruction whose addressing information is to be read.
1599
 * @return      - 0 if the information was successfully read; nonzero otherwise.
1600
 */
1601
static int readModRM(struct InternalInstruction *insn)
1602
1.58M
{
1603
1.58M
  uint8_t mod, rm, reg, evexrm;
1604
1605
  // dbgprintf(insn, "readModRM()");
1606
1607
1.58M
  if (insn->consumedModRM)
1608
1.06M
    return 0;
1609
1610
518k
  insn->modRMOffset = (uint8_t)(insn->readerCursor - insn->startLocation);
1611
1612
518k
  if (consumeByte(insn, &insn->modRM))
1613
1.17k
    return -1;
1614
1615
517k
  insn->consumedModRM = true;
1616
1617
  // save original ModRM for later reference
1618
517k
  insn->orgModRM = insn->modRM;
1619
1620
  // handle MOVcr, MOVdr, MOVrc, MOVrd by pretending they have MRM.mod = 3
1621
517k
  if ((insn->firstByte == 0x0f && insn->opcodeType == TWOBYTE) &&
1622
40.8k
      (insn->opcode >= 0x20 && insn->opcode <= 0x23))
1623
1.49k
    insn->modRM |= 0xC0;
1624
1625
517k
  mod = modFromModRM(insn->modRM);
1626
517k
  rm = rmFromModRM(insn->modRM);
1627
517k
  reg = regFromModRM(insn->modRM);
1628
1629
  /*
1630
   * This goes by insn->registerSize to pick the correct register, which messes
1631
   * up if we're using (say) XMM or 8-bit register operands.  That gets fixed in
1632
   * fixupReg().
1633
   */
1634
517k
  switch (insn->registerSize) {
1635
169k
  case 2:
1636
169k
    insn->regBase = MODRM_REG_AX;
1637
169k
    insn->eaRegBase = EA_REG_AX;
1638
169k
    break;
1639
311k
  case 4:
1640
311k
    insn->regBase = MODRM_REG_EAX;
1641
311k
    insn->eaRegBase = EA_REG_EAX;
1642
311k
    break;
1643
36.0k
  case 8:
1644
36.0k
    insn->regBase = MODRM_REG_RAX;
1645
36.0k
    insn->eaRegBase = EA_REG_RAX;
1646
36.0k
    break;
1647
517k
  }
1648
1649
517k
  reg |= rFromREX(insn->rexPrefix) << 3;
1650
517k
  rm |= bFromREX(insn->rexPrefix) << 3;
1651
1652
517k
  evexrm = 0;
1653
517k
  if (insn->vectorExtensionType == TYPE_EVEX &&
1654
43.7k
      insn->mode == MODE_64BIT) {
1655
19.9k
    reg |= r2FromEVEX2of4(insn->vectorExtensionPrefix[1]) << 4;
1656
19.9k
    evexrm = xFromEVEX2of4(insn->vectorExtensionPrefix[1]) << 4;
1657
19.9k
  }
1658
1659
517k
  insn->reg = (Reg)(insn->regBase + reg);
1660
1661
517k
  switch (insn->addressSize) {
1662
157k
  case 2: {
1663
157k
    EABase eaBaseBase = EA_BASE_BX_SI;
1664
1665
157k
    switch (mod) {
1666
84.3k
    case 0x0:
1667
84.3k
      if (rm == 0x6) {
1668
5.05k
        insn->eaBase = EA_BASE_NONE;
1669
5.05k
        insn->eaDisplacement = EA_DISP_16;
1670
5.05k
        if (readDisplacement(insn))
1671
17
          return -1;
1672
79.3k
      } else {
1673
79.3k
        insn->eaBase = (EABase)(eaBaseBase + rm);
1674
79.3k
        insn->eaDisplacement = EA_DISP_NONE;
1675
79.3k
      }
1676
84.3k
      break;
1677
84.3k
    case 0x1:
1678
23.8k
      insn->eaBase = (EABase)(eaBaseBase + rm);
1679
23.8k
      insn->eaDisplacement = EA_DISP_8;
1680
23.8k
      insn->displacementSize = 1;
1681
23.8k
      if (readDisplacement(insn))
1682
41
        return -1;
1683
23.8k
      break;
1684
23.8k
    case 0x2:
1685
14.9k
      insn->eaBase = (EABase)(eaBaseBase + rm);
1686
14.9k
      insn->eaDisplacement = EA_DISP_16;
1687
14.9k
      if (readDisplacement(insn))
1688
98
        return -1;
1689
14.8k
      break;
1690
34.1k
    case 0x3:
1691
34.1k
      insn->eaBase = (EABase)(insn->eaRegBase + rm);
1692
34.1k
      if (readDisplacement(insn))
1693
0
        return -1;
1694
34.1k
      break;
1695
157k
    }
1696
157k
    break;
1697
157k
  }
1698
1699
180k
  case 4:
1700
359k
  case 8: {
1701
359k
    EABase eaBaseBase =
1702
359k
      (insn->addressSize == 4 ? EA_BASE_EAX : EA_BASE_RAX);
1703
1704
359k
    switch (mod) {
1705
0
    default:
1706
0
      break;
1707
178k
    case 0x0:
1708
178k
      insn->eaDisplacement =
1709
178k
        EA_DISP_NONE; /* readSIB may override this */
1710
      // In determining whether RIP-relative mode is used (rm=5),
1711
      // or whether a SIB byte is present (rm=4),
1712
      // the extension bits (REX.b and EVEX.x) are ignored.
1713
178k
      switch (rm & 7) {
1714
12.8k
      case 0x4: // SIB byte is present
1715
12.8k
        insn->eaBase = (insn->addressSize == 4 ?
1716
5.92k
              EA_BASE_sib :
1717
12.8k
              EA_BASE_sib64);
1718
12.8k
        if (readSIB(insn) || readDisplacement(insn))
1719
31
          return -1;
1720
12.8k
        break;
1721
12.8k
      case 0x5: // RIP-relative
1722
4.38k
        insn->eaBase = EA_BASE_NONE;
1723
4.38k
        insn->eaDisplacement = EA_DISP_32;
1724
4.38k
        if (readDisplacement(insn))
1725
57
          return -1;
1726
4.33k
        break;
1727
161k
      default:
1728
161k
        insn->eaBase = (EABase)(eaBaseBase + rm);
1729
161k
        break;
1730
178k
      }
1731
178k
      break;
1732
178k
    case 0x1:
1733
59.0k
      insn->displacementSize = 1;
1734
      /* FALLTHROUGH */
1735
86.6k
    case 0x2:
1736
86.6k
      insn->eaDisplacement =
1737
86.6k
        (mod == 0x1 ? EA_DISP_8 : EA_DISP_32);
1738
86.6k
      switch (rm & 7) {
1739
11.6k
      case 0x4: // SIB byte is present
1740
11.6k
        insn->eaBase = EA_BASE_sib;
1741
11.6k
        if (readSIB(insn) || readDisplacement(insn))
1742
74
          return -1;
1743
11.5k
        break;
1744
74.9k
      default:
1745
74.9k
        insn->eaBase = (EABase)(eaBaseBase + rm);
1746
74.9k
        if (readDisplacement(insn))
1747
354
          return -1;
1748
74.6k
        break;
1749
86.6k
      }
1750
86.2k
      break;
1751
94.7k
    case 0x3:
1752
94.7k
      insn->eaDisplacement = EA_DISP_NONE;
1753
94.7k
      insn->eaBase = (EABase)(insn->eaRegBase + rm + evexrm);
1754
94.7k
      break;
1755
359k
    }
1756
1757
359k
    break;
1758
359k
  }
1759
517k
  } /* switch (insn->addressSize) */
1760
1761
516k
  return 0;
1762
517k
}
1763
1764
#define GENERIC_FIXUP_FUNC(name, base, prefix, mask) \
1765
  static uint16_t name(struct InternalInstruction *insn, \
1766
           OperandType type, uint8_t index, uint8_t *valid) \
1767
554k
  { \
1768
554k
    *valid = 1; \
1769
554k
    switch (type) { \
1770
0
    default: \
1771
0
      *valid = 0; \
1772
0
      return 0; \
1773
147k
    case TYPE_Rv: \
1774
147k
      return base + index; \
1775
219k
    case TYPE_R8: \
1776
219k
      index &= mask; \
1777
219k
      if (index > 0xf) \
1778
219k
        *valid = 0; \
1779
219k
      if (insn->rexPrefix && index >= 4 && index <= 7) { \
1780
1.98k
        return prefix##_SPL + (index - 4); \
1781
217k
      } else { \
1782
217k
        return prefix##_AL + index; \
1783
217k
      } \
1784
219k
    case TYPE_R16: \
1785
3.09k
      index &= mask; \
1786
3.09k
      if (index > 0xf) \
1787
3.09k
        *valid = 0; \
1788
3.09k
      return prefix##_AX + index; \
1789
219k
    case TYPE_R32: \
1790
2.71k
      index &= mask; \
1791
2.71k
      if (index > 0xf) \
1792
2.71k
        *valid = 0; \
1793
2.71k
      return prefix##_EAX + index; \
1794
219k
    case TYPE_R64: \
1795
13.1k
      index &= mask; \
1796
13.1k
      if (index > 0xf) \
1797
13.1k
        *valid = 0; \
1798
13.1k
      return prefix##_RAX + index; \
1799
219k
    case TYPE_ZMM: \
1800
34.9k
      return prefix##_ZMM0 + index; \
1801
219k
    case TYPE_YMM: \
1802
29.6k
      return prefix##_YMM0 + index; \
1803
219k
    case TYPE_XMM: \
1804
66.4k
      return prefix##_XMM0 + index; \
1805
219k
    case TYPE_VK: \
1806
21.8k
      index &= 0xf; \
1807
21.8k
      if (index > 7) \
1808
21.8k
        *valid = 0; \
1809
21.8k
      return prefix##_K0 + index; \
1810
219k
    case TYPE_MM64: \
1811
7.21k
      return prefix##_MM0 + (index & 0x7); \
1812
219k
    case TYPE_SEGMENTREG: \
1813
1.61k
      if ((index & 7) > 5) \
1814
1.61k
        *valid = 0; \
1815
1.61k
      return prefix##_ES + (index & 7); \
1816
219k
    case TYPE_DEBUGREG: \
1817
939
      return prefix##_DR0 + index; \
1818
219k
    case TYPE_CONTROLREG: \
1819
551
      return prefix##_CR0 + index; \
1820
219k
    case TYPE_BNDR: \
1821
6.30k
      if (index > 3) \
1822
6.30k
        *valid = 0; \
1823
6.30k
      return prefix##_BND0 + index; \
1824
219k
    case TYPE_MVSIBX: \
1825
0
      return prefix##_XMM0 + index; \
1826
219k
    case TYPE_MVSIBY: \
1827
0
      return prefix##_YMM0 + index; \
1828
219k
    case TYPE_MVSIBZ: \
1829
0
      return prefix##_ZMM0 + index; \
1830
554k
    } \
1831
554k
  }
X86DisassemblerDecoder.c:fixupRegValue
Line
Count
Source
1767
434k
  { \
1768
434k
    *valid = 1; \
1769
434k
    switch (type) { \
1770
0
    default: \
1771
0
      *valid = 0; \
1772
0
      return 0; \
1773
107k
    case TYPE_Rv: \
1774
107k
      return base + index; \
1775
179k
    case TYPE_R8: \
1776
179k
      index &= mask; \
1777
179k
      if (index > 0xf) \
1778
179k
        *valid = 0; \
1779
179k
      if (insn->rexPrefix && index >= 4 && index <= 7) { \
1780
1.19k
        return prefix##_SPL + (index - 4); \
1781
178k
      } else { \
1782
178k
        return prefix##_AL + index; \
1783
178k
      } \
1784
179k
    case TYPE_R16: \
1785
2.46k
      index &= mask; \
1786
2.46k
      if (index > 0xf) \
1787
2.46k
        *valid = 0; \
1788
2.46k
      return prefix##_AX + index; \
1789
179k
    case TYPE_R32: \
1790
1.75k
      index &= mask; \
1791
1.75k
      if (index > 0xf) \
1792
1.75k
        *valid = 0; \
1793
1.75k
      return prefix##_EAX + index; \
1794
179k
    case TYPE_R64: \
1795
6.49k
      index &= mask; \
1796
6.49k
      if (index > 0xf) \
1797
6.49k
        *valid = 0; \
1798
6.49k
      return prefix##_RAX + index; \
1799
179k
    case TYPE_ZMM: \
1800
27.7k
      return prefix##_ZMM0 + index; \
1801
179k
    case TYPE_YMM: \
1802
23.4k
      return prefix##_YMM0 + index; \
1803
179k
    case TYPE_XMM: \
1804
52.3k
      return prefix##_XMM0 + index; \
1805
179k
    case TYPE_VK: \
1806
20.6k
      index &= 0xf; \
1807
20.6k
      if (index > 7) \
1808
20.6k
        *valid = 0; \
1809
20.6k
      return prefix##_K0 + index; \
1810
179k
    case TYPE_MM64: \
1811
4.36k
      return prefix##_MM0 + (index & 0x7); \
1812
179k
    case TYPE_SEGMENTREG: \
1813
1.61k
      if ((index & 7) > 5) \
1814
1.61k
        *valid = 0; \
1815
1.61k
      return prefix##_ES + (index & 7); \
1816
179k
    case TYPE_DEBUGREG: \
1817
939
      return prefix##_DR0 + index; \
1818
179k
    case TYPE_CONTROLREG: \
1819
551
      return prefix##_CR0 + index; \
1820
179k
    case TYPE_BNDR: \
1821
5.56k
      if (index > 3) \
1822
5.56k
        *valid = 0; \
1823
5.56k
      return prefix##_BND0 + index; \
1824
179k
    case TYPE_MVSIBX: \
1825
0
      return prefix##_XMM0 + index; \
1826
179k
    case TYPE_MVSIBY: \
1827
0
      return prefix##_YMM0 + index; \
1828
179k
    case TYPE_MVSIBZ: \
1829
0
      return prefix##_ZMM0 + index; \
1830
434k
    } \
1831
434k
  }
X86DisassemblerDecoder.c:fixupRMValue
Line
Count
Source
1767
120k
  { \
1768
120k
    *valid = 1; \
1769
120k
    switch (type) { \
1770
0
    default: \
1771
0
      *valid = 0; \
1772
0
      return 0; \
1773
39.9k
    case TYPE_Rv: \
1774
39.9k
      return base + index; \
1775
39.8k
    case TYPE_R8: \
1776
39.8k
      index &= mask; \
1777
39.8k
      if (index > 0xf) \
1778
39.8k
        *valid = 0; \
1779
39.8k
      if (insn->rexPrefix && index >= 4 && index <= 7) { \
1780
793
        return prefix##_SPL + (index - 4); \
1781
39.0k
      } else { \
1782
39.0k
        return prefix##_AL + index; \
1783
39.0k
      } \
1784
39.8k
    case TYPE_R16: \
1785
630
      index &= mask; \
1786
630
      if (index > 0xf) \
1787
630
        *valid = 0; \
1788
630
      return prefix##_AX + index; \
1789
39.8k
    case TYPE_R32: \
1790
966
      index &= mask; \
1791
966
      if (index > 0xf) \
1792
966
        *valid = 0; \
1793
966
      return prefix##_EAX + index; \
1794
39.8k
    case TYPE_R64: \
1795
6.69k
      index &= mask; \
1796
6.69k
      if (index > 0xf) \
1797
6.69k
        *valid = 0; \
1798
6.69k
      return prefix##_RAX + index; \
1799
39.8k
    case TYPE_ZMM: \
1800
7.26k
      return prefix##_ZMM0 + index; \
1801
39.8k
    case TYPE_YMM: \
1802
6.18k
      return prefix##_YMM0 + index; \
1803
39.8k
    case TYPE_XMM: \
1804
14.1k
      return prefix##_XMM0 + index; \
1805
39.8k
    case TYPE_VK: \
1806
1.27k
      index &= 0xf; \
1807
1.27k
      if (index > 7) \
1808
1.27k
        *valid = 0; \
1809
1.27k
      return prefix##_K0 + index; \
1810
39.8k
    case TYPE_MM64: \
1811
2.84k
      return prefix##_MM0 + (index & 0x7); \
1812
39.8k
    case TYPE_SEGMENTREG: \
1813
0
      if ((index & 7) > 5) \
1814
0
        *valid = 0; \
1815
0
      return prefix##_ES + (index & 7); \
1816
39.8k
    case TYPE_DEBUGREG: \
1817
0
      return prefix##_DR0 + index; \
1818
39.8k
    case TYPE_CONTROLREG: \
1819
0
      return prefix##_CR0 + index; \
1820
39.8k
    case TYPE_BNDR: \
1821
736
      if (index > 3) \
1822
736
        *valid = 0; \
1823
736
      return prefix##_BND0 + index; \
1824
39.8k
    case TYPE_MVSIBX: \
1825
0
      return prefix##_XMM0 + index; \
1826
39.8k
    case TYPE_MVSIBY: \
1827
0
      return prefix##_YMM0 + index; \
1828
39.8k
    case TYPE_MVSIBZ: \
1829
0
      return prefix##_ZMM0 + index; \
1830
120k
    } \
1831
120k
  }
1832
1833
/*
1834
 * fixup*Value - Consults an operand type to determine the meaning of the
1835
 *   reg or R/M field.  If the operand is an XMM operand, for example, an
1836
 *   operand would be XMM0 instead of AX, which readModRM() would otherwise
1837
 *   misinterpret it as.
1838
 *
1839
 * @param insn  - The instruction containing the operand.
1840
 * @param type  - The operand type.
1841
 * @param index - The existing value of the field as reported by readModRM().
1842
 * @param valid - The address of a uint8_t.  The target is set to 1 if the
1843
 *                field is valid for the register class; 0 if not.
1844
 * @return      - The proper value.
1845
 */
1846
GENERIC_FIXUP_FUNC(fixupRegValue, insn->regBase, MODRM_REG, 0x1f)
1847
GENERIC_FIXUP_FUNC(fixupRMValue, insn->eaRegBase, EA_REG, 0xf)
1848
1849
/*
1850
 * fixupReg - Consults an operand specifier to determine which of the
1851
 *   fixup*Value functions to use in correcting readModRM()'ss interpretation.
1852
 *
1853
 * @param insn  - See fixup*Value().
1854
 * @param op    - The operand specifier.
1855
 * @return      - 0 if fixup was successful; -1 if the register returned was
1856
 *                invalid for its class.
1857
 */
1858
static int fixupReg(struct InternalInstruction *insn,
1859
        const struct OperandSpecifier *op)
1860
937k
{
1861
937k
  uint8_t valid;
1862
1863
937k
  switch ((OperandEncoding)op->encoding) {
1864
0
  default:
1865
    // debug("Expected a REG or R/M encoding in fixupReg");
1866
0
    return -1;
1867
48.9k
  case ENCODING_VVVV:
1868
48.9k
    insn->vvvv = (Reg)fixupRegValue(insn, (OperandType)op->type,
1869
48.9k
            insn->vvvv, &valid);
1870
48.9k
    if (!valid)
1871
1
      return -1;
1872
48.9k
    break;
1873
385k
  case ENCODING_REG:
1874
385k
    insn->reg = (Reg)fixupRegValue(insn, (OperandType)op->type,
1875
385k
                 insn->reg - insn->regBase,
1876
385k
                 &valid);
1877
385k
    if (!valid)
1878
21
      return -1;
1879
385k
    break;
1880
3.37M
CASE_ENCODING_RM:
1881
3.37M
    if (insn->eaBase >= insn->eaRegBase) {
1882
120k
      insn->eaBase = (EABase)fixupRMValue(
1883
120k
        insn, (OperandType)op->type,
1884
120k
        insn->eaBase - insn->eaRegBase, &valid);
1885
120k
      if (!valid)
1886
2
        return -1;
1887
120k
    }
1888
502k
    break;
1889
937k
  }
1890
1891
937k
  return 0;
1892
937k
}
1893
1894
/*
1895
 * readOpcodeRegister - Reads an operand from the opcode field of an
1896
 *   instruction and interprets it appropriately given the operand width.
1897
 *   Handles AddRegFrm instructions.
1898
 *
1899
 * @param insn  - the instruction whose opcode field is to be read.
1900
 * @param size  - The width (in bytes) of the register being specified.
1901
 *                1 means AL and friends, 2 means AX, 4 means EAX, and 8 means
1902
 *                RAX.
1903
 * @return      - 0 on success; nonzero otherwise.
1904
 */
1905
static int readOpcodeRegister(struct InternalInstruction *insn, uint8_t size)
1906
122k
{
1907
122k
  if (size == 0)
1908
84.4k
    size = insn->registerSize;
1909
1910
122k
  switch (size) {
1911
21.0k
  case 1:
1912
21.0k
    insn->opcodeRegister =
1913
21.0k
      (Reg)(MODRM_REG_AL + ((bFromREX(insn->rexPrefix) << 3) |
1914
21.0k
                (insn->opcode & 7)));
1915
21.0k
    if (insn->rexPrefix &&
1916
908
        insn->opcodeRegister >= MODRM_REG_AL + 0x4 &&
1917
559
        insn->opcodeRegister < MODRM_REG_AL + 0x8) {
1918
132
      insn->opcodeRegister =
1919
132
        (Reg)(MODRM_REG_SPL + (insn->opcodeRegister -
1920
132
                   MODRM_REG_AL - 4));
1921
132
    }
1922
1923
21.0k
    break;
1924
37.3k
  case 2:
1925
37.3k
    insn->opcodeRegister =
1926
37.3k
      (Reg)(MODRM_REG_AX + ((bFromREX(insn->rexPrefix) << 3) |
1927
37.3k
                (insn->opcode & 7)));
1928
37.3k
    break;
1929
46.9k
  case 4:
1930
46.9k
    insn->opcodeRegister = (Reg)(MODRM_REG_EAX +
1931
46.9k
               ((bFromREX(insn->rexPrefix) << 3) |
1932
46.9k
                (insn->opcode & 7)));
1933
46.9k
    break;
1934
17.3k
  case 8:
1935
17.3k
    insn->opcodeRegister = (Reg)(MODRM_REG_RAX +
1936
17.3k
               ((bFromREX(insn->rexPrefix) << 3) |
1937
17.3k
                (insn->opcode & 7)));
1938
17.3k
    break;
1939
122k
  }
1940
1941
122k
  return 0;
1942
122k
}
1943
1944
/*
1945
 * readImmediate - Consumes an immediate operand from an instruction, given the
1946
 *   desired operand size.
1947
 *
1948
 * @param insn  - The instruction whose operand is to be read.
1949
 * @param size  - The width (in bytes) of the operand.
1950
 * @return      - 0 if the immediate was successfully consumed; nonzero
1951
 *                otherwise.
1952
 */
1953
static int readImmediate(struct InternalInstruction *insn, uint8_t size)
1954
269k
{
1955
269k
  uint8_t imm8;
1956
269k
  uint16_t imm16;
1957
269k
  uint32_t imm32;
1958
269k
  uint64_t imm64;
1959
1960
269k
  if (insn->numImmediatesConsumed == 2) {
1961
    // debug("Already consumed two immediates");
1962
0
    return -1;
1963
0
  }
1964
1965
269k
  if (size == 0)
1966
0
    size = insn->immediateSize;
1967
269k
  else
1968
269k
    insn->immediateSize = size;
1969
1970
269k
  insn->immediateOffset = insn->readerCursor - insn->startLocation;
1971
1972
269k
  switch (size) {
1973
196k
  case 1:
1974
196k
    if (consumeByte(insn, &imm8))
1975
474
      return -1;
1976
1977
196k
    insn->immediates[insn->numImmediatesConsumed] = imm8;
1978
196k
    break;
1979
39.2k
  case 2:
1980
39.2k
    if (consumeUInt16(insn, &imm16))
1981
182
      return -1;
1982
1983
39.0k
    insn->immediates[insn->numImmediatesConsumed] = imm16;
1984
39.0k
    break;
1985
29.8k
  case 4:
1986
29.8k
    if (consumeUInt32(insn, &imm32))
1987
403
      return -1;
1988
1989
29.4k
    insn->immediates[insn->numImmediatesConsumed] = imm32;
1990
29.4k
    break;
1991
3.73k
  case 8:
1992
3.73k
    if (consumeUInt64(insn, &imm64))
1993
77
      return -1;
1994
3.65k
    insn->immediates[insn->numImmediatesConsumed] = imm64;
1995
3.65k
    break;
1996
269k
  }
1997
1998
268k
  insn->numImmediatesConsumed++;
1999
2000
268k
  return 0;
2001
269k
}
2002
2003
/*
2004
 * readVVVV - Consumes vvvv from an instruction if it has a VEX prefix.
2005
 *
2006
 * @param insn  - The instruction whose operand is to be read.
2007
 * @return      - 0 if the vvvv was successfully consumed; nonzero
2008
 *                otherwise.
2009
 */
2010
static int readVVVV(struct InternalInstruction *insn)
2011
997k
{
2012
997k
  int vvvv;
2013
2014
997k
  if (insn->vectorExtensionType == TYPE_EVEX)
2015
43.7k
    vvvv = (v2FromEVEX4of4(insn->vectorExtensionPrefix[3]) << 4 |
2016
43.7k
      vvvvFromEVEX3of4(insn->vectorExtensionPrefix[2]));
2017
953k
  else if (insn->vectorExtensionType == TYPE_VEX_3B)
2018
4.29k
    vvvv = vvvvFromVEX3of3(insn->vectorExtensionPrefix[2]);
2019
949k
  else if (insn->vectorExtensionType == TYPE_VEX_2B)
2020
9.79k
    vvvv = vvvvFromVEX2of2(insn->vectorExtensionPrefix[1]);
2021
939k
  else if (insn->vectorExtensionType == TYPE_XOP)
2022
6.62k
    vvvv = vvvvFromXOP3of3(insn->vectorExtensionPrefix[2]);
2023
933k
  else
2024
933k
    return -1;
2025
2026
64.4k
  if (insn->mode != MODE_64BIT)
2027
39.9k
    vvvv &= 0xf; // Can only clear bit 4. Bit 3 must be cleared later.
2028
2029
64.4k
  insn->vvvv = (Reg)vvvv;
2030
2031
64.4k
  return 0;
2032
997k
}
2033
2034
/*
2035
 * readMaskRegister - Reads an mask register from the opcode field of an
2036
 *   instruction.
2037
 *
2038
 * @param insn    - The instruction whose opcode field is to be read.
2039
 * @return        - 0 on success; nonzero otherwise.
2040
 */
2041
static int readMaskRegister(struct InternalInstruction *insn)
2042
28.9k
{
2043
28.9k
  if (insn->vectorExtensionType != TYPE_EVEX)
2044
0
    return -1;
2045
2046
28.9k
  insn->writemask =
2047
28.9k
    (Reg)(aaaFromEVEX4of4(insn->vectorExtensionPrefix[3]));
2048
2049
28.9k
  return 0;
2050
28.9k
}
2051
2052
/*
2053
 * readOperands - Consults the specifier for an instruction and consumes all
2054
 *   operands for that instruction, interpreting them as it goes.
2055
 *
2056
 * @param insn  - The instruction whose operands are to be read and interpreted.
2057
 * @return      - 0 if all operands could be read; nonzero otherwise.
2058
 */
2059
static int readOperands(struct InternalInstruction *insn)
2060
997k
{
2061
997k
  int hasVVVV, needVVVV;
2062
997k
  int sawRegImm = 0;
2063
997k
  int i;
2064
2065
  /* If non-zero vvvv specified, need to make sure one of the operands
2066
     uses it. */
2067
997k
  hasVVVV = !readVVVV(insn);
2068
997k
  needVVVV = hasVVVV && (insn->vvvv != 0);
2069
2070
6.97M
  for (i = 0; i < X86_MAX_OPERANDS; ++i) {
2071
5.97M
    const OperandSpecifier *op =
2072
5.97M
      &x86OperandSets[insn->spec->operands][i];
2073
5.97M
    switch (op->encoding) {
2074
4.29M
    case ENCODING_NONE:
2075
4.34M
    case ENCODING_SI:
2076
4.40M
    case ENCODING_DI:
2077
4.40M
      break;
2078
2079
25.6k
CASE_ENCODING_VSIB:
2080
      // VSIB can use the V2 bit so check only the other bits.
2081
25.6k
      if (needVVVV)
2082
3.43k
        needVVVV = hasVVVV & ((insn->vvvv & 0xf) != 0);
2083
2084
25.6k
      if (readModRM(insn))
2085
0
        return -1;
2086
2087
      // Reject if SIB wasn't used.
2088
4.90k
      if (insn->eaBase != EA_BASE_sib &&
2089
2.44k
          insn->eaBase != EA_BASE_sib64)
2090
7
        return -1;
2091
2092
      // If sibIndex was set to SIB_INDEX_NONE, index offset is 4.
2093
4.90k
      if (insn->sibIndex == SIB_INDEX_NONE)
2094
449
        insn->sibIndex =
2095
449
          (SIBIndex)(insn->sibIndexBase + 4);
2096
2097
      // If EVEX.v2 is set this is one of the 16-31 registers.
2098
4.90k
      if (insn->vectorExtensionType == TYPE_EVEX &&
2099
3.37k
          insn->mode == MODE_64BIT &&
2100
2.46k
          v2FromEVEX4of4(insn->vectorExtensionPrefix[3]))
2101
1.89k
        insn->sibIndex =
2102
1.89k
          (SIBIndex)(insn->sibIndex + 16);
2103
2104
      // Adjust the index register to the correct size.
2105
4.90k
      switch (op->type) {
2106
0
      default:
2107
        // debug("Unhandled VSIB index type");
2108
0
        return -1;
2109
1.80k
      case TYPE_MVSIBX:
2110
1.80k
        insn->sibIndex =
2111
1.80k
          (SIBIndex)(SIB_INDEX_XMM0 +
2112
1.80k
               (insn->sibIndex -
2113
1.80k
                insn->sibIndexBase));
2114
1.80k
        break;
2115
1.45k
      case TYPE_MVSIBY:
2116
1.45k
        insn->sibIndex =
2117
1.45k
          (SIBIndex)(SIB_INDEX_YMM0 +
2118
1.45k
               (insn->sibIndex -
2119
1.45k
                insn->sibIndexBase));
2120
1.45k
        break;
2121
1.64k
      case TYPE_MVSIBZ:
2122
1.64k
        insn->sibIndex =
2123
1.64k
          (SIBIndex)(SIB_INDEX_ZMM0 +
2124
1.64k
               (insn->sibIndex -
2125
1.64k
                insn->sibIndexBase));
2126
1.64k
        break;
2127
4.90k
      }
2128
2129
      // Apply the AVX512 compressed displacement scaling factor.
2130
4.90k
      if (op->encoding != ENCODING_REG &&
2131
4.90k
          insn->eaDisplacement == EA_DISP_8)
2132
1.38k
        insn->displacement *=
2133
1.38k
          1 << (op->encoding - ENCODING_VSIB);
2134
4.90k
      break;
2135
2136
385k
    case ENCODING_REG:
2137
6.07M
CASE_ENCODING_RM:
2138
6.07M
      if (readModRM(insn))
2139
0
        return -1;
2140
2141
888k
      if (fixupReg(insn, op))
2142
23
        return -1;
2143
2144
      // Apply the AVX512 compressed displacement scaling factor.
2145
888k
      if (op->encoding != ENCODING_REG &&
2146
502k
          insn->eaDisplacement == EA_DISP_8)
2147
81.2k
        insn->displacement *=
2148
81.2k
          1 << (op->encoding - ENCODING_RM);
2149
888k
      break;
2150
2151
197k
    case ENCODING_IB:
2152
197k
      if (sawRegImm) {
2153
        /* Saw a register immediate so don't read again and instead split the
2154
             previous immediate.  FIXME: This is a hack. */
2155
562
        insn->immediates[insn->numImmediatesConsumed] =
2156
562
          insn->immediates
2157
562
            [insn->numImmediatesConsumed -
2158
562
             1] &
2159
562
          0xf;
2160
562
        ++insn->numImmediatesConsumed;
2161
562
        break;
2162
562
      }
2163
196k
      if (readImmediate(insn, 1))
2164
474
        return -1;
2165
196k
      if (op->type == TYPE_XMM || op->type == TYPE_YMM)
2166
993
        sawRegImm = 1;
2167
196k
      break;
2168
2169
13.7k
    case ENCODING_IW:
2170
13.7k
      if (readImmediate(insn, 2))
2171
52
        return -1;
2172
13.6k
      break;
2173
2174
13.6k
    case ENCODING_ID:
2175
5.18k
      if (readImmediate(insn, 4))
2176
67
        return -1;
2177
5.11k
      break;
2178
2179
5.11k
    case ENCODING_IO:
2180
659
      if (readImmediate(insn, 8))
2181
8
        return -1;
2182
651
      break;
2183
2184
42.9k
    case ENCODING_Iv:
2185
42.9k
      if (readImmediate(insn, insn->immediateSize))
2186
409
        return -1;
2187
42.5k
      break;
2188
2189
42.5k
    case ENCODING_Ia:
2190
10.3k
      if (readImmediate(insn, insn->addressSize))
2191
126
        return -1;
2192
      /* Direct memory-offset (moffset) immediate will get mapped
2193
           to memory operand later. We want the encoding info to
2194
           reflect that as well. */
2195
10.1k
      insn->displacementOffset = insn->immediateOffset;
2196
10.1k
      insn->consumedDisplacement = true;
2197
10.1k
      insn->displacementSize = insn->immediateSize;
2198
10.1k
      insn->displacement =
2199
10.1k
        insn->immediates[insn->numImmediatesConsumed -
2200
10.1k
             1];
2201
10.1k
      insn->immediateOffset = 0;
2202
10.1k
      insn->immediateSize = 0;
2203
10.1k
      break;
2204
2205
2.74k
    case ENCODING_IRC:
2206
2.74k
      insn->RC =
2207
2.74k
        (l2FromEVEX4of4(insn->vectorExtensionPrefix[3])
2208
2.74k
         << 1) |
2209
2.74k
        lFromEVEX4of4(insn->vectorExtensionPrefix[3]);
2210
2.74k
      break;
2211
2212
21.0k
    case ENCODING_RB:
2213
21.0k
      if (readOpcodeRegister(insn, 1))
2214
0
        return -1;
2215
21.0k
      break;
2216
2217
21.0k
    case ENCODING_RW:
2218
0
      if (readOpcodeRegister(insn, 2))
2219
0
        return -1;
2220
0
      break;
2221
2222
0
    case ENCODING_RD:
2223
0
      if (readOpcodeRegister(insn, 4))
2224
0
        return -1;
2225
0
      break;
2226
2227
17.1k
    case ENCODING_RO:
2228
17.1k
      if (readOpcodeRegister(insn, 8))
2229
0
        return -1;
2230
17.1k
      break;
2231
2232
84.4k
    case ENCODING_Rv:
2233
84.4k
      if (readOpcodeRegister(insn, 0))
2234
0
        return -1;
2235
84.4k
      break;
2236
2237
84.4k
    case ENCODING_FP:
2238
5.47k
      break;
2239
2240
48.9k
    case ENCODING_VVVV:
2241
48.9k
      if (!hasVVVV)
2242
0
        return -1;
2243
2244
48.9k
      needVVVV =
2245
48.9k
        0; /* Mark that we have found a VVVV operand. */
2246
2247
48.9k
      if (insn->mode != MODE_64BIT)
2248
30.8k
        insn->vvvv = (Reg)(insn->vvvv & 0x7);
2249
2250
48.9k
      if (fixupReg(insn, op))
2251
1
        return -1;
2252
48.9k
      break;
2253
2254
48.9k
    case ENCODING_WRITEMASK:
2255
28.9k
      if (readMaskRegister(insn))
2256
0
        return -1;
2257
28.9k
      break;
2258
2259
205k
    case ENCODING_DUP:
2260
205k
      break;
2261
2262
0
    default:
2263
      // dbgprintf(insn, "Encountered an operand with an unknown encoding.");
2264
0
      return -1;
2265
5.97M
    }
2266
5.97M
  }
2267
2268
  /* If we didn't find ENCODING_VVVV operand, but non-zero vvvv present, fail */
2269
996k
  if (needVVVV)
2270
8
    return -1;
2271
2272
996k
  return 0;
2273
996k
}
2274
2275
// return True if instruction is illegal to use with prefixes
2276
// This also check & fix the isPrefixNN when a prefix is irrelevant.
2277
static bool checkPrefix(struct InternalInstruction *insn)
2278
997k
{
2279
  // LOCK prefix
2280
997k
  if (insn->hasLockPrefix) {
2281
32.8k
    switch (insn->instructionID) {
2282
221
    default:
2283
      // invalid LOCK
2284
221
      return true;
2285
2286
    // nop dword [rax]
2287
67
    case X86_NOOPL:
2288
2289
    // DEC
2290
171
    case X86_DEC16m:
2291
549
    case X86_DEC32m:
2292
970
    case X86_DEC64m:
2293
1.22k
    case X86_DEC8m:
2294
2295
    // ADC
2296
1.46k
    case X86_ADC16mi:
2297
1.86k
    case X86_ADC16mi8:
2298
2.00k
    case X86_ADC16mr:
2299
2.21k
    case X86_ADC32mi:
2300
2.33k
    case X86_ADC32mi8:
2301
2.45k
    case X86_ADC32mr:
2302
2.59k
    case X86_ADC64mi32:
2303
2.81k
    case X86_ADC64mi8:
2304
2.93k
    case X86_ADC64mr:
2305
3.22k
    case X86_ADC8mi:
2306
3.53k
    case X86_ADC8mi8:
2307
3.94k
    case X86_ADC8mr:
2308
4.02k
    case X86_ADC8rm:
2309
4.31k
    case X86_ADC16rm:
2310
4.39k
    case X86_ADC32rm:
2311
4.49k
    case X86_ADC64rm:
2312
2313
    // ADD
2314
4.73k
    case X86_ADD16mi:
2315
4.95k
    case X86_ADD16mi8:
2316
5.15k
    case X86_ADD16mr:
2317
5.28k
    case X86_ADD32mi:
2318
5.47k
    case X86_ADD32mi8:
2319
6.27k
    case X86_ADD32mr:
2320
6.35k
    case X86_ADD64mi32:
2321
6.55k
    case X86_ADD64mi8:
2322
6.76k
    case X86_ADD64mr:
2323
6.94k
    case X86_ADD8mi:
2324
7.10k
    case X86_ADD8mi8:
2325
7.72k
    case X86_ADD8mr:
2326
7.81k
    case X86_ADD8rm:
2327
8.02k
    case X86_ADD16rm:
2328
8.14k
    case X86_ADD32rm:
2329
8.49k
    case X86_ADD64rm:
2330
2331
    // AND
2332
8.60k
    case X86_AND16mi:
2333
9.02k
    case X86_AND16mi8:
2334
9.12k
    case X86_AND16mr:
2335
9.30k
    case X86_AND32mi:
2336
9.47k
    case X86_AND32mi8:
2337
9.55k
    case X86_AND32mr:
2338
9.59k
    case X86_AND64mi32:
2339
9.69k
    case X86_AND64mi8:
2340
9.90k
    case X86_AND64mr:
2341
10.3k
    case X86_AND8mi:
2342
10.3k
    case X86_AND8mi8:
2343
10.6k
    case X86_AND8mr:
2344
10.8k
    case X86_AND8rm:
2345
10.9k
    case X86_AND16rm:
2346
11.0k
    case X86_AND32rm:
2347
11.3k
    case X86_AND64rm:
2348
2349
    // BTC
2350
11.4k
    case X86_BTC16mi8:
2351
11.8k
    case X86_BTC16mr:
2352
11.9k
    case X86_BTC32mi8:
2353
12.0k
    case X86_BTC32mr:
2354
12.0k
    case X86_BTC64mi8:
2355
12.1k
    case X86_BTC64mr:
2356
2357
    // BTR
2358
12.1k
    case X86_BTR16mi8:
2359
12.2k
    case X86_BTR16mr:
2360
12.3k
    case X86_BTR32mi8:
2361
12.4k
    case X86_BTR32mr:
2362
12.4k
    case X86_BTR64mi8:
2363
12.4k
    case X86_BTR64mr:
2364
2365
    // BTS
2366
12.7k
    case X86_BTS16mi8:
2367
12.8k
    case X86_BTS16mr:
2368
12.9k
    case X86_BTS32mi8:
2369
12.9k
    case X86_BTS32mr:
2370
13.0k
    case X86_BTS64mi8:
2371
13.1k
    case X86_BTS64mr:
2372
2373
    // CMPXCHG
2374
13.2k
    case X86_CMPXCHG16B:
2375
13.4k
    case X86_CMPXCHG16rm:
2376
13.4k
    case X86_CMPXCHG32rm:
2377
13.6k
    case X86_CMPXCHG64rm:
2378
14.4k
    case X86_CMPXCHG8rm:
2379
14.4k
    case X86_CMPXCHG8B:
2380
2381
    // INC
2382
14.5k
    case X86_INC16m:
2383
14.9k
    case X86_INC32m:
2384
15.0k
    case X86_INC64m:
2385
15.1k
    case X86_INC8m:
2386
2387
    // NEG
2388
15.2k
    case X86_NEG16m:
2389
15.3k
    case X86_NEG32m:
2390
15.3k
    case X86_NEG64m:
2391
15.4k
    case X86_NEG8m:
2392
2393
    // NOT
2394
15.5k
    case X86_NOT16m:
2395
15.7k
    case X86_NOT32m:
2396
15.8k
    case X86_NOT64m:
2397
16.1k
    case X86_NOT8m:
2398
2399
    // OR
2400
16.2k
    case X86_OR16mi:
2401
16.8k
    case X86_OR16mi8:
2402
17.0k
    case X86_OR16mr:
2403
17.1k
    case X86_OR32mi:
2404
17.5k
    case X86_OR32mi8:
2405
17.8k
    case X86_OR32mr:
2406
17.9k
    case X86_OR64mi32:
2407
18.0k
    case X86_OR64mi8:
2408
18.0k
    case X86_OR64mr:
2409
18.2k
    case X86_OR8mi8:
2410
18.3k
    case X86_OR8mi:
2411
18.4k
    case X86_OR8mr:
2412
18.5k
    case X86_OR8rm:
2413
18.9k
    case X86_OR16rm:
2414
19.2k
    case X86_OR32rm:
2415
19.3k
    case X86_OR64rm:
2416
2417
    // SBB
2418
19.5k
    case X86_SBB16mi:
2419
20.0k
    case X86_SBB16mi8:
2420
20.6k
    case X86_SBB16mr:
2421
20.7k
    case X86_SBB32mi:
2422
20.9k
    case X86_SBB32mi8:
2423
21.2k
    case X86_SBB32mr:
2424
21.7k
    case X86_SBB64mi32:
2425
21.8k
    case X86_SBB64mi8:
2426
21.9k
    case X86_SBB64mr:
2427
22.2k
    case X86_SBB8mi:
2428
22.6k
    case X86_SBB8mi8:
2429
22.9k
    case X86_SBB8mr:
2430
2431
    // SUB
2432
23.0k
    case X86_SUB16mi:
2433
23.6k
    case X86_SUB16mi8:
2434
23.9k
    case X86_SUB16mr:
2435
24.0k
    case X86_SUB32mi:
2436
24.4k
    case X86_SUB32mi8:
2437
24.6k
    case X86_SUB32mr:
2438
24.7k
    case X86_SUB64mi32:
2439
24.9k
    case X86_SUB64mi8:
2440
24.9k
    case X86_SUB64mr:
2441
25.1k
    case X86_SUB8mi8:
2442
25.2k
    case X86_SUB8mi:
2443
25.6k
    case X86_SUB8mr:
2444
25.8k
    case X86_SUB8rm:
2445
26.0k
    case X86_SUB16rm:
2446
26.2k
    case X86_SUB32rm:
2447
26.5k
    case X86_SUB64rm:
2448
2449
    // XADD
2450
26.6k
    case X86_XADD16rm:
2451
26.7k
    case X86_XADD32rm:
2452
26.9k
    case X86_XADD64rm:
2453
27.2k
    case X86_XADD8rm:
2454
2455
    // XCHG
2456
27.3k
    case X86_XCHG16rm:
2457
27.5k
    case X86_XCHG32rm:
2458
28.1k
    case X86_XCHG64rm:
2459
28.6k
    case X86_XCHG8rm:
2460
2461
    // XOR
2462
28.9k
    case X86_XOR16mi:
2463
29.3k
    case X86_XOR16mi8:
2464
29.4k
    case X86_XOR16mr:
2465
29.6k
    case X86_XOR32mi:
2466
29.9k
    case X86_XOR32mi8:
2467
30.2k
    case X86_XOR32mr:
2468
30.4k
    case X86_XOR64mi32:
2469
30.5k
    case X86_XOR64mi8:
2470
30.7k
    case X86_XOR64mr:
2471
30.8k
    case X86_XOR8mi8:
2472
31.7k
    case X86_XOR8mi:
2473
32.0k
    case X86_XOR8mr:
2474
32.2k
    case X86_XOR8rm:
2475
32.3k
    case X86_XOR16rm:
2476
32.5k
    case X86_XOR32rm:
2477
32.6k
    case X86_XOR64rm:
2478
2479
      // this instruction can be used with LOCK prefix
2480
32.6k
      return false;
2481
32.8k
    }
2482
32.8k
  }
2483
2484
#if 0
2485
  // REPNE prefix
2486
  if (insn->repeatPrefix) {
2487
    // 0xf2 can be a part of instruction encoding, but not really a prefix.
2488
    // In such a case, clear it.
2489
    if (insn->twoByteEscape == 0x0f) {
2490
      insn->prefix0 = 0;
2491
    }
2492
  }
2493
#endif
2494
2495
  // no invalid prefixes
2496
964k
  return false;
2497
997k
}
2498
2499
/*
2500
 * decodeInstruction - Reads and interprets a full instruction provided by the
2501
 *   user.
2502
 *
2503
 * @param insn      - A pointer to the instruction to be populated.  Must be
2504
 *                    pre-allocated.
2505
 * @param reader    - The function to be used to read the instruction's bytes.
2506
 * @param readerArg - A generic argument to be passed to the reader to store
2507
 *                    any internal state.
2508
 * @param startLoc  - The address (in the reader's address space) of the first
2509
 *                    byte in the instruction.
2510
 * @param mode      - The mode (real mode, IA-32e, or IA-32e in 64-bit mode) to
2511
 *                    decode the instruction in.
2512
 * @return          - 0 if instruction is valid; nonzero if not.
2513
 */
2514
int decodeInstruction(struct InternalInstruction *insn, byteReader_t reader,
2515
          const void *readerArg, uint64_t startLoc,
2516
          DisassemblerMode mode)
2517
1.00M
{
2518
1.00M
  insn->reader = reader;
2519
1.00M
  insn->readerArg = readerArg;
2520
1.00M
  insn->startLocation = startLoc;
2521
1.00M
  insn->readerCursor = startLoc;
2522
1.00M
  insn->mode = mode;
2523
1.00M
  insn->numImmediatesConsumed = 0;
2524
2525
1.00M
  if (readPrefixes(insn) || readOpcode(insn) || getID(insn) ||
2526
999k
      insn->instructionID == 0 || checkPrefix(insn) || readOperands(insn))
2527
5.80k
    return -1;
2528
2529
996k
  insn->length = (size_t)(insn->readerCursor - insn->startLocation);
2530
2531
  // instruction length must be <= 15 to be valid
2532
996k
  if (insn->length > 15)
2533
50
    return -1;
2534
2535
996k
  if (insn->operandSize == 0)
2536
996k
    insn->operandSize = insn->registerSize;
2537
2538
996k
  insn->operands = &x86OperandSets[insn->spec->operands][0];
2539
2540
996k
  return 0;
2541
996k
}
2542
2543
#endif