Coverage Report

Created: 2026-08-31 06:58

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