Coverage Report

Created: 2026-12-01 07:03

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