Coverage Report

Created: 2025-11-24 06:12

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/capstonev5/arch/X86/X86Disassembler.c
Line
Count
Source
1
//===-- X86Disassembler.cpp - Disassembler for x86 and x86_64 -------------===//
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 code to translate the data produced by the decoder into
12
//  MCInsts.
13
//
14
// The X86 disassembler is a table-driven disassembler for the 16-, 32-, and
15
// 64-bit X86 instruction sets.  The main decode sequence for an assembly
16
// instruction in this disassembler is:
17
//
18
// 1. Read the prefix bytes and determine the attributes of the instruction.
19
//    These attributes, recorded in enum attributeBits
20
//    (X86DisassemblerDecoderCommon.h), form a bitmask.  The table CONTEXTS_SYM
21
//    provides a mapping from bitmasks to contexts, which are represented by
22
//    enum InstructionContext (ibid.).
23
//
24
// 2. Read the opcode, and determine what kind of opcode it is.  The
25
//    disassembler distinguishes four kinds of opcodes, which are enumerated in
26
//    OpcodeType (X86DisassemblerDecoderCommon.h): one-byte (0xnn), two-byte
27
//    (0x0f 0xnn), three-byte-38 (0x0f 0x38 0xnn), or three-byte-3a
28
//    (0x0f 0x3a 0xnn).  Mandatory prefixes are treated as part of the context.
29
//
30
// 3. Depending on the opcode type, look in one of four ClassDecision structures
31
//    (X86DisassemblerDecoderCommon.h).  Use the opcode class to determine which
32
//    OpcodeDecision (ibid.) to look the opcode in.  Look up the opcode, to get
33
//    a ModRMDecision (ibid.).
34
//
35
// 4. Some instructions, such as escape opcodes or extended opcodes, or even
36
//    instructions that have ModRM*Reg / ModRM*Mem forms in LLVM, need the
37
//    ModR/M byte to complete decode.  The ModRMDecision's type is an entry from
38
//    ModRMDecisionType (X86DisassemblerDecoderCommon.h) that indicates if the
39
//    ModR/M byte is required and how to interpret it.
40
//
41
// 5. After resolving the ModRMDecision, the disassembler has a unique ID
42
//    of type InstrUID (X86DisassemblerDecoderCommon.h).  Looking this ID up in
43
//    INSTRUCTIONS_SYM yields the name of the instruction and the encodings and
44
//    meanings of its operands.
45
//
46
// 6. For each operand, its encoding is an entry from OperandEncoding
47
//    (X86DisassemblerDecoderCommon.h) and its type is an entry from
48
//    OperandType (ibid.).  The encoding indicates how to read it from the
49
//    instruction; the type indicates how to interpret the value once it has
50
//    been read.  For example, a register operand could be stored in the R/M
51
//    field of the ModR/M byte, the REG field of the ModR/M byte, or added to
52
//    the main opcode.  This is orthogonal from its meaning (an GPR or an XMM
53
//    register, for instance).  Given this information, the operands can be
54
//    extracted and interpreted.
55
//
56
// 7. As the last step, the disassembler translates the instruction information
57
//    and operands into a format understandable by the client - in this case, an
58
//    MCInst for use by the MC infrastructure.
59
//
60
// The disassembler is broken broadly into two parts: the table emitter that
61
// emits the instruction decode tables discussed above during compilation, and
62
// the disassembler itself.  The table emitter is documented in more detail in
63
// utils/TableGen/X86DisassemblerEmitter.h.
64
//
65
// X86Disassembler.cpp contains the code responsible for step 7, and for
66
//   invoking the decoder to execute steps 1-6.
67
// X86DisassemblerDecoderCommon.h contains the definitions needed by both the
68
//   table emitter and the disassembler.
69
// X86DisassemblerDecoder.h contains the public interface of the decoder,
70
//   factored out into C for possible use by other projects.
71
// X86DisassemblerDecoder.c contains the source code of the decoder, which is
72
//   responsible for steps 1-6.
73
//
74
//===----------------------------------------------------------------------===//
75
76
/* Capstone Disassembly Engine */
77
/* By Nguyen Anh Quynh <aquynh@gmail.com>, 2013-2019 */
78
79
#ifdef CAPSTONE_HAS_X86
80
81
#if defined (WIN32) || defined (WIN64) || defined (_WIN32) || defined (_WIN64)
82
#pragma warning(disable:4996)     // disable MSVC's warning on strncpy()
83
#pragma warning(disable:28719)    // disable MSVC's warning on strncpy()
84
#endif
85
86
#include <capstone/platform.h>
87
88
#if defined(CAPSTONE_HAS_OSXKERNEL)
89
#include <Availability.h>
90
#endif
91
92
#include <string.h>
93
94
#include "../../cs_priv.h"
95
96
#include "X86BaseInfo.h"
97
#include "X86Disassembler.h"
98
#include "X86DisassemblerDecoderCommon.h"
99
#include "X86DisassemblerDecoder.h"
100
#include "../../MCInst.h"
101
#include "../../utils.h"
102
#include "X86Mapping.h"
103
104
#define GET_REGINFO_ENUM
105
#define GET_REGINFO_MC_DESC
106
#include "X86GenRegisterInfo.inc"
107
108
#define GET_INSTRINFO_ENUM
109
#ifdef CAPSTONE_X86_REDUCE
110
#include "X86GenInstrInfo_reduce.inc"
111
#else
112
#include "X86GenInstrInfo.inc"
113
#endif
114
115
// Fill-ins to make the compiler happy.  These constants are never actually
116
//   assigned; they are just filler to make an automatically-generated switch
117
//   statement work.
118
enum {
119
  X86_BX_SI = 500,
120
  X86_BX_DI = 501,
121
  X86_BP_SI = 502,
122
  X86_BP_DI = 503,
123
  X86_sib   = 504,
124
  X86_sib64 = 505
125
};
126
127
//
128
// Private code that translates from struct InternalInstructions to MCInsts.
129
//
130
131
/// translateRegister - Translates an internal register to the appropriate LLVM
132
///   register, and appends it as an operand to an MCInst.
133
///
134
/// @param mcInst     - The MCInst to append to.
135
/// @param reg        - The Reg to append.
136
static void translateRegister(MCInst *mcInst, Reg reg)
137
92.5k
{
138
20.6M
#define ENTRY(x) X86_##x,
139
92.5k
  static const uint16_t llvmRegnums[] = {
140
92.5k
    ALL_REGS
141
92.5k
    0
142
92.5k
  };
143
92.5k
#undef ENTRY
144
145
92.5k
  uint16_t llvmRegnum = llvmRegnums[reg];
146
92.5k
  MCOperand_CreateReg0(mcInst, llvmRegnum);
147
92.5k
}
148
149
static const uint8_t segmentRegnums[SEG_OVERRIDE_max] = {
150
  0,        // SEG_OVERRIDE_NONE
151
  X86_CS,
152
  X86_SS,
153
  X86_DS,
154
  X86_ES,
155
  X86_FS,
156
  X86_GS
157
};
158
159
/// translateSrcIndex   - Appends a source index operand to an MCInst.
160
///
161
/// @param mcInst       - The MCInst to append to.
162
/// @param insn         - The internal instruction.
163
static bool translateSrcIndex(MCInst *mcInst, InternalInstruction *insn)
164
2.63k
{
165
2.63k
  unsigned baseRegNo;
166
167
2.63k
  if (insn->mode == MODE_64BIT)
168
1.00k
    baseRegNo = insn->hasAdSize ? X86_ESI : X86_RSI;
169
1.63k
  else if (insn->mode == MODE_32BIT)
170
731
    baseRegNo = insn->hasAdSize ? X86_SI : X86_ESI;
171
899
  else {
172
    // assert(insn->mode == MODE_16BIT);
173
899
    baseRegNo = insn->hasAdSize ? X86_ESI : X86_SI;
174
899
  }
175
176
2.63k
  MCOperand_CreateReg0(mcInst, baseRegNo);
177
178
2.63k
  MCOperand_CreateReg0(mcInst, segmentRegnums[insn->segmentOverride]);
179
180
2.63k
  return false;
181
2.63k
}
182
183
/// translateDstIndex   - Appends a destination index operand to an MCInst.
184
///
185
/// @param mcInst       - The MCInst to append to.
186
/// @param insn         - The internal instruction.
187
static bool translateDstIndex(MCInst *mcInst, InternalInstruction *insn)
188
4.88k
{
189
4.88k
  unsigned baseRegNo;
190
191
4.88k
  if (insn->mode == MODE_64BIT)
192
2.57k
    baseRegNo = insn->hasAdSize ? X86_EDI : X86_RDI;
193
2.31k
  else if (insn->mode == MODE_32BIT)
194
1.06k
    baseRegNo = insn->hasAdSize ? X86_DI : X86_EDI;
195
1.25k
  else {
196
    // assert(insn->mode == MODE_16BIT);
197
1.25k
    baseRegNo = insn->hasAdSize ? X86_EDI : X86_DI;
198
1.25k
  }
199
200
4.88k
  MCOperand_CreateReg0(mcInst, baseRegNo);
201
202
4.88k
  return false;
203
4.88k
}
204
205
/// translateImmediate  - Appends an immediate operand to an MCInst.
206
///
207
/// @param mcInst       - The MCInst to append to.
208
/// @param immediate    - The immediate value to append.
209
/// @param operand      - The operand, as stored in the descriptor table.
210
/// @param insn         - The internal instruction.
211
static void translateImmediate(MCInst *mcInst, uint64_t immediate,
212
    const OperandSpecifier *operand, InternalInstruction *insn)
213
33.0k
{
214
33.0k
  OperandType type;
215
216
33.0k
  type = (OperandType)operand->type;
217
33.0k
  if (type == TYPE_REL) {
218
    //isBranch = true;
219
    //pcrel = insn->startLocation + insn->immediateOffset + insn->immediateSize;
220
5.82k
    switch (operand->encoding) {
221
0
      default:
222
0
        break;
223
278
      case ENCODING_Iv:
224
278
        switch (insn->displacementSize) {
225
0
          default:
226
0
            break;
227
0
          case 1:
228
0
            if(immediate & 0x80)
229
0
              immediate |= ~(0xffull);
230
0
            break;
231
24
          case 2:
232
24
            if(immediate & 0x8000)
233
10
              immediate |= ~(0xffffull);
234
24
            break;
235
254
          case 4:
236
254
            if(immediate & 0x80000000)
237
119
              immediate |= ~(0xffffffffull);
238
254
            break;
239
0
          case 8:
240
0
            break;
241
278
        }
242
278
        break;
243
5.03k
      case ENCODING_IB:
244
5.03k
        if (immediate & 0x80)
245
1.41k
          immediate |= ~(0xffull);
246
5.03k
        break;
247
310
      case ENCODING_IW:
248
310
        if (immediate & 0x8000)
249
160
          immediate |= ~(0xffffull);
250
310
        break;
251
204
      case ENCODING_ID:
252
204
        if (immediate & 0x80000000)
253
137
          immediate |= ~(0xffffffffull);
254
204
        break;
255
5.82k
    }
256
5.82k
  } // By default sign-extend all X86 immediates based on their encoding.
257
27.2k
  else if (type == TYPE_IMM) {
258
11.1k
    switch (operand->encoding) {
259
3.82k
      default:
260
3.82k
        break;
261
6.09k
      case ENCODING_IB:
262
6.09k
        if(immediate & 0x80)
263
2.44k
          immediate |= ~(0xffull);
264
6.09k
        break;
265
905
      case ENCODING_IW:
266
905
        if(immediate & 0x8000)
267
520
          immediate |= ~(0xffffull);
268
905
        break;
269
348
      case ENCODING_ID:
270
348
        if(immediate & 0x80000000)
271
269
          immediate |= ~(0xffffffffull);
272
348
        break;
273
9
      case ENCODING_IO:
274
9
        break;
275
11.1k
    }
276
16.0k
  } else if (type == TYPE_IMM3) {
277
2.47k
#ifndef CAPSTONE_X86_REDUCE
278
    // Check for immediates that printSSECC can't handle.
279
2.47k
    if (immediate >= 8) {
280
1.32k
      unsigned NewOpc = 0;
281
282
1.32k
      switch (MCInst_getOpcode(mcInst)) {
283
0
        default: break; // never reach
284
258
        case X86_CMPPDrmi:  NewOpc = X86_CMPPDrmi_alt;  break;
285
9
        case X86_CMPPDrri:  NewOpc = X86_CMPPDrri_alt;  break;
286
211
        case X86_CMPPSrmi:  NewOpc = X86_CMPPSrmi_alt;  break;
287
80
        case X86_CMPPSrri:  NewOpc = X86_CMPPSrri_alt;  break;
288
90
        case X86_CMPSDrm:   NewOpc = X86_CMPSDrm_alt;   break;
289
3
        case X86_CMPSDrr:   NewOpc = X86_CMPSDrr_alt;   break;
290
6
        case X86_CMPSSrm:   NewOpc = X86_CMPSSrm_alt;   break;
291
3
        case X86_CMPSSrr:   NewOpc = X86_CMPSSrr_alt;   break;
292
2
        case X86_VPCOMBri:  NewOpc = X86_VPCOMBri_alt;  break;
293
33
        case X86_VPCOMBmi:  NewOpc = X86_VPCOMBmi_alt;  break;
294
241
        case X86_VPCOMWri:  NewOpc = X86_VPCOMWri_alt;  break;
295
92
        case X86_VPCOMWmi:  NewOpc = X86_VPCOMWmi_alt;  break;
296
4
        case X86_VPCOMDri:  NewOpc = X86_VPCOMDri_alt;  break;
297
17
        case X86_VPCOMDmi:  NewOpc = X86_VPCOMDmi_alt;  break;
298
33
        case X86_VPCOMQri:  NewOpc = X86_VPCOMQri_alt;  break;
299
6
        case X86_VPCOMQmi:  NewOpc = X86_VPCOMQmi_alt;  break;
300
1
        case X86_VPCOMUBri: NewOpc = X86_VPCOMUBri_alt; break;
301
16
        case X86_VPCOMUBmi: NewOpc = X86_VPCOMUBmi_alt; break;
302
138
        case X86_VPCOMUWri: NewOpc = X86_VPCOMUWri_alt; break;
303
18
        case X86_VPCOMUWmi: NewOpc = X86_VPCOMUWmi_alt; break;
304
41
        case X86_VPCOMUDri: NewOpc = X86_VPCOMUDri_alt; break;
305
1
        case X86_VPCOMUDmi: NewOpc = X86_VPCOMUDmi_alt; break;
306
13
        case X86_VPCOMUQri: NewOpc = X86_VPCOMUQri_alt; break;
307
6
        case X86_VPCOMUQmi: NewOpc = X86_VPCOMUQmi_alt; break;
308
1.32k
      }
309
310
      // Switch opcode to the one that doesn't get special printing.
311
1.32k
      if (NewOpc != 0) {
312
1.32k
        MCInst_setOpcode(mcInst, NewOpc);
313
1.32k
      }
314
1.32k
    }
315
2.47k
#endif
316
13.6k
  } else if (type == TYPE_IMM5) {
317
2.57k
#ifndef CAPSTONE_X86_REDUCE
318
    // Check for immediates that printAVXCC can't handle.
319
2.57k
    if (immediate >= 32) {
320
1.81k
      unsigned NewOpc = 0;
321
322
1.81k
      switch (MCInst_getOpcode(mcInst)) {
323
779
        default: break; // unexpected opcode
324
779
        case X86_VCMPPDrmi:   NewOpc = X86_VCMPPDrmi_alt;   break;
325
10
        case X86_VCMPPDrri:   NewOpc = X86_VCMPPDrri_alt;   break;
326
0
        case X86_VCMPPSrmi:   NewOpc = X86_VCMPPSrmi_alt;   break;
327
33
        case X86_VCMPPSrri:   NewOpc = X86_VCMPPSrri_alt;   break;
328
53
        case X86_VCMPSDrm:    NewOpc = X86_VCMPSDrm_alt;    break;
329
49
        case X86_VCMPSDrr:    NewOpc = X86_VCMPSDrr_alt;    break;
330
26
        case X86_VCMPSSrm:    NewOpc = X86_VCMPSSrm_alt;    break;
331
10
        case X86_VCMPSSrr:    NewOpc = X86_VCMPSSrr_alt;    break;
332
20
        case X86_VCMPPDYrmi:  NewOpc = X86_VCMPPDYrmi_alt;  break;
333
117
        case X86_VCMPPDYrri:  NewOpc = X86_VCMPPDYrri_alt;  break;
334
36
        case X86_VCMPPSYrmi:  NewOpc = X86_VCMPPSYrmi_alt;  break;
335
4
        case X86_VCMPPSYrri:  NewOpc = X86_VCMPPSYrri_alt;  break;
336
3
        case X86_VCMPPDZrmi:  NewOpc = X86_VCMPPDZrmi_alt;  break;
337
139
        case X86_VCMPPDZrri:  NewOpc = X86_VCMPPDZrri_alt;  break;
338
131
        case X86_VCMPPDZrrib: NewOpc = X86_VCMPPDZrrib_alt; break;
339
8
        case X86_VCMPPSZrmi:  NewOpc = X86_VCMPPSZrmi_alt;  break;
340
4
        case X86_VCMPPSZrri:  NewOpc = X86_VCMPPSZrri_alt;  break;
341
129
        case X86_VCMPPSZrrib: NewOpc = X86_VCMPPSZrrib_alt; break;
342
0
        case X86_VCMPPDZ128rmi:  NewOpc = X86_VCMPPDZ128rmi_alt;  break;
343
0
        case X86_VCMPPDZ128rri:  NewOpc = X86_VCMPPDZ128rri_alt;  break;
344
16
        case X86_VCMPPSZ128rmi:  NewOpc = X86_VCMPPSZ128rmi_alt;  break;
345
0
        case X86_VCMPPSZ128rri:  NewOpc = X86_VCMPPSZ128rri_alt;  break;
346
0
        case X86_VCMPPDZ256rmi:  NewOpc = X86_VCMPPDZ256rmi_alt;  break;
347
4
        case X86_VCMPPDZ256rri:  NewOpc = X86_VCMPPDZ256rri_alt;  break;
348
16
        case X86_VCMPPSZ256rmi:  NewOpc = X86_VCMPPSZ256rmi_alt;  break;
349
0
        case X86_VCMPPSZ256rri:  NewOpc = X86_VCMPPSZ256rri_alt;  break;
350
4
        case X86_VCMPSDZrm_Int:  NewOpc = X86_VCMPSDZrmi_alt;  break;
351
39
        case X86_VCMPSDZrr_Int:  NewOpc = X86_VCMPSDZrri_alt;  break;
352
32
        case X86_VCMPSDZrrb_Int: NewOpc = X86_VCMPSDZrrb_alt;  break;
353
12
        case X86_VCMPSSZrm_Int:  NewOpc = X86_VCMPSSZrmi_alt;  break;
354
128
        case X86_VCMPSSZrr_Int:  NewOpc = X86_VCMPSSZrri_alt;  break;
355
13
        case X86_VCMPSSZrrb_Int: NewOpc = X86_VCMPSSZrrb_alt;  break;
356
1.81k
      }
357
358
      // Switch opcode to the one that doesn't get special printing.
359
1.81k
      if (NewOpc != 0) {
360
1.03k
        MCInst_setOpcode(mcInst, NewOpc);
361
1.03k
      }
362
1.81k
    }
363
2.57k
#endif
364
11.0k
  } else if (type == TYPE_AVX512ICC) {
365
6.03k
#ifndef CAPSTONE_X86_REDUCE
366
6.03k
    if (immediate >= 8 || ((immediate & 0x3) == 3)) {
367
4.89k
      unsigned NewOpc = 0;
368
4.89k
      switch (MCInst_getOpcode(mcInst)) {
369
0
        default: // llvm_unreachable("unexpected opcode");
370
108
        case X86_VPCMPBZ128rmi:    NewOpc = X86_VPCMPBZ128rmi_alt;    break;
371
2
        case X86_VPCMPBZ128rmik:   NewOpc = X86_VPCMPBZ128rmik_alt;   break;
372
27
        case X86_VPCMPBZ128rri:    NewOpc = X86_VPCMPBZ128rri_alt;    break;
373
25
        case X86_VPCMPBZ128rrik:   NewOpc = X86_VPCMPBZ128rrik_alt;   break;
374
2
        case X86_VPCMPBZ256rmi:    NewOpc = X86_VPCMPBZ256rmi_alt;    break;
375
7
        case X86_VPCMPBZ256rmik:   NewOpc = X86_VPCMPBZ256rmik_alt;   break;
376
9
        case X86_VPCMPBZ256rri:    NewOpc = X86_VPCMPBZ256rri_alt;    break;
377
6
        case X86_VPCMPBZ256rrik:   NewOpc = X86_VPCMPBZ256rrik_alt;   break;
378
16
        case X86_VPCMPBZrmi:       NewOpc = X86_VPCMPBZrmi_alt;       break;
379
8
        case X86_VPCMPBZrmik:      NewOpc = X86_VPCMPBZrmik_alt;      break;
380
18
        case X86_VPCMPBZrri:       NewOpc = X86_VPCMPBZrri_alt;       break;
381
58
        case X86_VPCMPBZrrik:      NewOpc = X86_VPCMPBZrrik_alt;      break;
382
1
        case X86_VPCMPDZ128rmi:    NewOpc = X86_VPCMPDZ128rmi_alt;    break;
383
65
        case X86_VPCMPDZ128rmib:   NewOpc = X86_VPCMPDZ128rmib_alt;   break;
384
135
        case X86_VPCMPDZ128rmibk:  NewOpc = X86_VPCMPDZ128rmibk_alt;  break;
385
11
        case X86_VPCMPDZ128rmik:   NewOpc = X86_VPCMPDZ128rmik_alt;   break;
386
7
        case X86_VPCMPDZ128rri:    NewOpc = X86_VPCMPDZ128rri_alt;    break;
387
9
        case X86_VPCMPDZ128rrik:   NewOpc = X86_VPCMPDZ128rrik_alt;   break;
388
0
        case X86_VPCMPDZ256rmi:    NewOpc = X86_VPCMPDZ256rmi_alt;    break;
389
42
        case X86_VPCMPDZ256rmib:   NewOpc = X86_VPCMPDZ256rmib_alt;   break;
390
7
        case X86_VPCMPDZ256rmibk:  NewOpc = X86_VPCMPDZ256rmibk_alt;  break;
391
5
        case X86_VPCMPDZ256rmik:   NewOpc = X86_VPCMPDZ256rmik_alt;   break;
392
6
        case X86_VPCMPDZ256rri:    NewOpc = X86_VPCMPDZ256rri_alt;    break;
393
20
        case X86_VPCMPDZ256rrik:   NewOpc = X86_VPCMPDZ256rrik_alt;   break;
394
12
        case X86_VPCMPDZrmi:       NewOpc = X86_VPCMPDZrmi_alt;       break;
395
3
        case X86_VPCMPDZrmib:      NewOpc = X86_VPCMPDZrmib_alt;      break;
396
10
        case X86_VPCMPDZrmibk:     NewOpc = X86_VPCMPDZrmibk_alt;     break;
397
4
        case X86_VPCMPDZrmik:      NewOpc = X86_VPCMPDZrmik_alt;      break;
398
46
        case X86_VPCMPDZrri:       NewOpc = X86_VPCMPDZrri_alt;       break;
399
22
        case X86_VPCMPDZrrik:      NewOpc = X86_VPCMPDZrrik_alt;      break;
400
4
        case X86_VPCMPQZ128rmi:    NewOpc = X86_VPCMPQZ128rmi_alt;    break;
401
199
        case X86_VPCMPQZ128rmib:   NewOpc = X86_VPCMPQZ128rmib_alt;   break;
402
96
        case X86_VPCMPQZ128rmibk:  NewOpc = X86_VPCMPQZ128rmibk_alt;  break;
403
0
        case X86_VPCMPQZ128rmik:   NewOpc = X86_VPCMPQZ128rmik_alt;   break;
404
8
        case X86_VPCMPQZ128rri:    NewOpc = X86_VPCMPQZ128rri_alt;    break;
405
25
        case X86_VPCMPQZ128rrik:   NewOpc = X86_VPCMPQZ128rrik_alt;   break;
406
41
        case X86_VPCMPQZ256rmi:    NewOpc = X86_VPCMPQZ256rmi_alt;    break;
407
24
        case X86_VPCMPQZ256rmib:   NewOpc = X86_VPCMPQZ256rmib_alt;   break;
408
39
        case X86_VPCMPQZ256rmibk:  NewOpc = X86_VPCMPQZ256rmibk_alt;  break;
409
21
        case X86_VPCMPQZ256rmik:   NewOpc = X86_VPCMPQZ256rmik_alt;   break;
410
5
        case X86_VPCMPQZ256rri:    NewOpc = X86_VPCMPQZ256rri_alt;    break;
411
16
        case X86_VPCMPQZ256rrik:   NewOpc = X86_VPCMPQZ256rrik_alt;   break;
412
22
        case X86_VPCMPQZrmi:       NewOpc = X86_VPCMPQZrmi_alt;       break;
413
9
        case X86_VPCMPQZrmib:      NewOpc = X86_VPCMPQZrmib_alt;      break;
414
135
        case X86_VPCMPQZrmibk:     NewOpc = X86_VPCMPQZrmibk_alt;     break;
415
34
        case X86_VPCMPQZrmik:      NewOpc = X86_VPCMPQZrmik_alt;      break;
416
13
        case X86_VPCMPQZrri:       NewOpc = X86_VPCMPQZrri_alt;       break;
417
3
        case X86_VPCMPQZrrik:      NewOpc = X86_VPCMPQZrrik_alt;      break;
418
6
        case X86_VPCMPUBZ128rmi:   NewOpc = X86_VPCMPUBZ128rmi_alt;   break;
419
7
        case X86_VPCMPUBZ128rmik:  NewOpc = X86_VPCMPUBZ128rmik_alt;  break;
420
19
        case X86_VPCMPUBZ128rri:   NewOpc = X86_VPCMPUBZ128rri_alt;   break;
421
77
        case X86_VPCMPUBZ128rrik:  NewOpc = X86_VPCMPUBZ128rrik_alt;  break;
422
5
        case X86_VPCMPUBZ256rmi:   NewOpc = X86_VPCMPUBZ256rmi_alt;   break;
423
125
        case X86_VPCMPUBZ256rmik:  NewOpc = X86_VPCMPUBZ256rmik_alt;  break;
424
148
        case X86_VPCMPUBZ256rri:   NewOpc = X86_VPCMPUBZ256rri_alt;   break;
425
359
        case X86_VPCMPUBZ256rrik:  NewOpc = X86_VPCMPUBZ256rrik_alt;  break;
426
19
        case X86_VPCMPUBZrmi:      NewOpc = X86_VPCMPUBZrmi_alt;      break;
427
10
        case X86_VPCMPUBZrmik:     NewOpc = X86_VPCMPUBZrmik_alt;     break;
428
240
        case X86_VPCMPUBZrri:      NewOpc = X86_VPCMPUBZrri_alt;      break;
429
83
        case X86_VPCMPUBZrrik:     NewOpc = X86_VPCMPUBZrrik_alt;     break;
430
8
        case X86_VPCMPUDZ128rmi:   NewOpc = X86_VPCMPUDZ128rmi_alt;   break;
431
16
        case X86_VPCMPUDZ128rmib:  NewOpc = X86_VPCMPUDZ128rmib_alt;  break;
432
33
        case X86_VPCMPUDZ128rmibk: NewOpc = X86_VPCMPUDZ128rmibk_alt; break;
433
24
        case X86_VPCMPUDZ128rmik:  NewOpc = X86_VPCMPUDZ128rmik_alt;  break;
434
38
        case X86_VPCMPUDZ128rri:   NewOpc = X86_VPCMPUDZ128rri_alt;   break;
435
61
        case X86_VPCMPUDZ128rrik:  NewOpc = X86_VPCMPUDZ128rrik_alt;  break;
436
44
        case X86_VPCMPUDZ256rmi:   NewOpc = X86_VPCMPUDZ256rmi_alt;   break;
437
76
        case X86_VPCMPUDZ256rmib:  NewOpc = X86_VPCMPUDZ256rmib_alt;  break;
438
6
        case X86_VPCMPUDZ256rmibk: NewOpc = X86_VPCMPUDZ256rmibk_alt; break;
439
6
        case X86_VPCMPUDZ256rmik:  NewOpc = X86_VPCMPUDZ256rmik_alt;  break;
440
211
        case X86_VPCMPUDZ256rri:   NewOpc = X86_VPCMPUDZ256rri_alt;   break;
441
88
        case X86_VPCMPUDZ256rrik:  NewOpc = X86_VPCMPUDZ256rrik_alt;  break;
442
30
        case X86_VPCMPUDZrmi:      NewOpc = X86_VPCMPUDZrmi_alt;      break;
443
2
        case X86_VPCMPUDZrmib:     NewOpc = X86_VPCMPUDZrmib_alt;     break;
444
13
        case X86_VPCMPUDZrmibk:    NewOpc = X86_VPCMPUDZrmibk_alt;    break;
445
6
        case X86_VPCMPUDZrmik:     NewOpc = X86_VPCMPUDZrmik_alt;     break;
446
43
        case X86_VPCMPUDZrri:      NewOpc = X86_VPCMPUDZrri_alt;      break;
447
13
        case X86_VPCMPUDZrrik:     NewOpc = X86_VPCMPUDZrrik_alt;     break;
448
6
        case X86_VPCMPUQZ128rmi:   NewOpc = X86_VPCMPUQZ128rmi_alt;   break;
449
107
        case X86_VPCMPUQZ128rmib:  NewOpc = X86_VPCMPUQZ128rmib_alt;  break;
450
96
        case X86_VPCMPUQZ128rmibk: NewOpc = X86_VPCMPUQZ128rmibk_alt; break;
451
16
        case X86_VPCMPUQZ128rmik:  NewOpc = X86_VPCMPUQZ128rmik_alt;  break;
452
13
        case X86_VPCMPUQZ128rri:   NewOpc = X86_VPCMPUQZ128rri_alt;   break;
453
35
        case X86_VPCMPUQZ128rrik:  NewOpc = X86_VPCMPUQZ128rrik_alt;  break;
454
23
        case X86_VPCMPUQZ256rmi:   NewOpc = X86_VPCMPUQZ256rmi_alt;   break;
455
131
        case X86_VPCMPUQZ256rmib:  NewOpc = X86_VPCMPUQZ256rmib_alt;  break;
456
113
        case X86_VPCMPUQZ256rmibk: NewOpc = X86_VPCMPUQZ256rmibk_alt; break;
457
5
        case X86_VPCMPUQZ256rmik:  NewOpc = X86_VPCMPUQZ256rmik_alt;  break;
458
2
        case X86_VPCMPUQZ256rri:   NewOpc = X86_VPCMPUQZ256rri_alt;   break;
459
18
        case X86_VPCMPUQZ256rrik:  NewOpc = X86_VPCMPUQZ256rrik_alt;  break;
460
37
        case X86_VPCMPUQZrmi:      NewOpc = X86_VPCMPUQZrmi_alt;      break;
461
9
        case X86_VPCMPUQZrmib:     NewOpc = X86_VPCMPUQZrmib_alt;     break;
462
255
        case X86_VPCMPUQZrmibk:    NewOpc = X86_VPCMPUQZrmibk_alt;    break;
463
45
        case X86_VPCMPUQZrmik:     NewOpc = X86_VPCMPUQZrmik_alt;     break;
464
6
        case X86_VPCMPUQZrri:      NewOpc = X86_VPCMPUQZrri_alt;      break;
465
32
        case X86_VPCMPUQZrrik:     NewOpc = X86_VPCMPUQZrrik_alt;     break;
466
30
        case X86_VPCMPUWZ128rmi:   NewOpc = X86_VPCMPUWZ128rmi_alt;   break;
467
34
        case X86_VPCMPUWZ128rmik:  NewOpc = X86_VPCMPUWZ128rmik_alt;  break;
468
33
        case X86_VPCMPUWZ128rri:   NewOpc = X86_VPCMPUWZ128rri_alt;   break;
469
176
        case X86_VPCMPUWZ128rrik:  NewOpc = X86_VPCMPUWZ128rrik_alt;  break;
470
4
        case X86_VPCMPUWZ256rmi:   NewOpc = X86_VPCMPUWZ256rmi_alt;   break;
471
47
        case X86_VPCMPUWZ256rmik:  NewOpc = X86_VPCMPUWZ256rmik_alt;  break;
472
3
        case X86_VPCMPUWZ256rri:   NewOpc = X86_VPCMPUWZ256rri_alt;   break;
473
56
        case X86_VPCMPUWZ256rrik:  NewOpc = X86_VPCMPUWZ256rrik_alt;  break;
474
104
        case X86_VPCMPUWZrmi:      NewOpc = X86_VPCMPUWZrmi_alt;      break;
475
14
        case X86_VPCMPUWZrmik:     NewOpc = X86_VPCMPUWZrmik_alt;     break;
476
10
        case X86_VPCMPUWZrri:      NewOpc = X86_VPCMPUWZrri_alt;      break;
477
26
        case X86_VPCMPUWZrrik:     NewOpc = X86_VPCMPUWZrrik_alt;     break;
478
42
        case X86_VPCMPWZ128rmi:    NewOpc = X86_VPCMPWZ128rmi_alt;    break;
479
20
        case X86_VPCMPWZ128rmik:   NewOpc = X86_VPCMPWZ128rmik_alt;   break;
480
19
        case X86_VPCMPWZ128rri:    NewOpc = X86_VPCMPWZ128rri_alt;    break;
481
22
        case X86_VPCMPWZ128rrik:   NewOpc = X86_VPCMPWZ128rrik_alt;   break;
482
18
        case X86_VPCMPWZ256rmi:    NewOpc = X86_VPCMPWZ256rmi_alt;    break;
483
42
        case X86_VPCMPWZ256rmik:   NewOpc = X86_VPCMPWZ256rmik_alt;   break;
484
12
        case X86_VPCMPWZ256rri:    NewOpc = X86_VPCMPWZ256rri_alt;    break;
485
24
        case X86_VPCMPWZ256rrik:   NewOpc = X86_VPCMPWZ256rrik_alt;   break;
486
1
        case X86_VPCMPWZrmi:       NewOpc = X86_VPCMPWZrmi_alt;       break;
487
10
        case X86_VPCMPWZrmik:      NewOpc = X86_VPCMPWZrmik_alt;      break;
488
2
        case X86_VPCMPWZrri:       NewOpc = X86_VPCMPWZrri_alt;       break;
489
0
        case X86_VPCMPWZrrik:      NewOpc = X86_VPCMPWZrrik_alt;      break;
490
4.89k
      }
491
492
      // Switch opcode to the one that doesn't get special printing.
493
4.89k
      if (NewOpc != 0) {
494
4.89k
        MCInst_setOpcode(mcInst, NewOpc);
495
4.89k
      }
496
4.89k
    }
497
6.03k
#endif
498
6.03k
  }
499
500
33.0k
  switch (type) {
501
291
    case TYPE_XMM:
502
291
      MCOperand_CreateReg0(mcInst, X86_XMM0 + ((uint32_t)immediate >> 4));
503
291
      return;
504
30
    case TYPE_YMM:
505
30
      MCOperand_CreateReg0(mcInst, X86_YMM0 + ((uint32_t)immediate >> 4));
506
30
      return;
507
0
    case TYPE_ZMM:
508
0
      MCOperand_CreateReg0(mcInst, X86_ZMM0 + ((uint32_t)immediate >> 4));
509
0
      return;
510
32.7k
    default:
511
      // operand is 64 bits wide.  Do nothing.
512
32.7k
      break;
513
33.0k
  }
514
515
32.7k
  MCOperand_CreateImm0(mcInst, immediate);
516
517
32.7k
  if (type == TYPE_MOFFS) {
518
1.74k
    MCOperand_CreateReg0(mcInst, segmentRegnums[insn->segmentOverride]);
519
1.74k
  }
520
32.7k
}
521
522
/// translateRMRegister - Translates a register stored in the R/M field of the
523
///   ModR/M byte to its LLVM equivalent and appends it to an MCInst.
524
/// @param mcInst       - The MCInst to append to.
525
/// @param insn         - The internal instruction to extract the R/M field
526
///                       from.
527
/// @return             - 0 on success; -1 otherwise
528
static bool translateRMRegister(MCInst *mcInst, InternalInstruction *insn)
529
21.2k
{
530
21.2k
  if (insn->eaBase == EA_BASE_sib || insn->eaBase == EA_BASE_sib64) {
531
    //debug("A R/M register operand may not have a SIB byte");
532
0
    return true;
533
0
  }
534
535
21.2k
  switch (insn->eaBase) {
536
0
    case EA_BASE_NONE:
537
      //debug("EA_BASE_NONE for ModR/M base");
538
0
      return true;
539
0
#define ENTRY(x) case EA_BASE_##x:
540
0
      ALL_EA_BASES
541
0
#undef ENTRY
542
        //debug("A R/M register operand may not have a base; "
543
        //      "the operand must be a register.");
544
0
        return true;
545
0
#define ENTRY(x)                                                      \
546
21.2k
    case EA_REG_##x:                                                    \
547
21.2k
      MCOperand_CreateReg0(mcInst, X86_##x); break;
548
0
      ALL_REGS
549
0
#undef ENTRY
550
0
    default:
551
        //debug("Unexpected EA base register");
552
0
        return true;
553
21.2k
  }
554
555
21.2k
  return false;
556
21.2k
}
557
558
/// translateRMMemory - Translates a memory operand stored in the Mod and R/M
559
///   fields of an internal instruction (and possibly its SIB byte) to a memory
560
///   operand in LLVM's format, and appends it to an MCInst.
561
///
562
/// @param mcInst       - The MCInst to append to.
563
/// @param insn         - The instruction to extract Mod, R/M, and SIB fields
564
///                       from.
565
/// @return             - 0 on success; nonzero otherwise
566
static bool translateRMMemory(MCInst *mcInst, InternalInstruction *insn)
567
44.9k
{
568
  // Addresses in an MCInst are represented as five operands:
569
  //   1. basereg       (register)  The R/M base, or (if there is a SIB) the
570
  //                                SIB base
571
  //   2. scaleamount   (immediate) 1, or (if there is a SIB) the specified
572
  //                                scale amount
573
  //   3. indexreg      (register)  x86_registerNONE, or (if there is a SIB)
574
  //                                the index (which is multiplied by the
575
  //                                scale amount)
576
  //   4. displacement  (immediate) 0, or the displacement if there is one
577
  //   5. segmentreg    (register)  x86_registerNONE for now, but could be set
578
  //                                if we have segment overrides
579
44.9k
  int scaleAmount, indexReg;
580
581
44.9k
  if (insn->eaBase == EA_BASE_sib || insn->eaBase == EA_BASE_sib64) {
582
9.21k
    if (insn->sibBase != SIB_BASE_NONE) {
583
9.06k
      switch (insn->sibBase) {
584
0
#define ENTRY(x)                                          \
585
9.06k
        case SIB_BASE_##x:                                  \
586
9.06k
        MCOperand_CreateReg0(mcInst, X86_##x); break;
587
0
        ALL_SIB_BASES
588
0
#undef ENTRY
589
0
        default:
590
          //debug("Unexpected sibBase");
591
0
          return true;
592
9.06k
      }
593
9.06k
    } else {
594
150
      MCOperand_CreateReg0(mcInst, 0);
595
150
    }
596
597
9.21k
    if (insn->sibIndex != SIB_INDEX_NONE) {
598
8.71k
      switch (insn->sibIndex) {
599
0
        default:
600
          //debug("Unexpected sibIndex");
601
0
          return true;
602
0
#define ENTRY(x)                                          \
603
8.71k
        case SIB_INDEX_##x:                                 \
604
8.71k
          indexReg = X86_##x; break;
605
0
          EA_BASES_32BIT
606
7
            EA_BASES_64BIT
607
33
            REGS_XMM
608
31
            REGS_YMM
609
8.71k
            REGS_ZMM
610
8.71k
#undef ENTRY
611
8.71k
      }
612
8.71k
    } else {
613
      // Use EIZ/RIZ for a few ambiguous cases where the SIB byte is present,
614
      // but no index is used and modrm alone should have been enough.
615
      // -No base register in 32-bit mode. In 64-bit mode this is used to
616
      //  avoid rip-relative addressing.
617
      // -Any base register used other than ESP/RSP/R12D/R12. Using these as a
618
      //  base always requires a SIB byte.
619
      // -A scale other than 1 is used.
620
505
      if (insn->sibScale != 1 ||
621
291
          (insn->sibBase == SIB_BASE_NONE && insn->mode != MODE_64BIT) ||
622
291
          (insn->sibBase != SIB_BASE_NONE &&
623
267
           insn->sibBase != SIB_BASE_ESP && insn->sibBase != SIB_BASE_RSP &&
624
443
           insn->sibBase != SIB_BASE_R12D && insn->sibBase != SIB_BASE_R12)) {
625
443
        indexReg = insn->addressSize == 4? X86_EIZ : X86_RIZ;
626
443
      } else
627
62
        indexReg = 0;
628
505
    }
629
630
9.21k
    scaleAmount = insn->sibScale;
631
35.7k
  } else {
632
35.7k
    switch (insn->eaBase) {
633
736
      case EA_BASE_NONE:
634
736
        if (insn->eaDisplacement == EA_DISP_NONE) {
635
          //debug("EA_BASE_NONE and EA_DISP_NONE for ModR/M base");
636
0
          return true;
637
0
        }
638
736
        if (insn->mode == MODE_64BIT) {
639
77
          if (insn->prefix3 == 0x67)  // address-size prefix overrides RIP relative addressing
640
0
            MCOperand_CreateReg0(mcInst, X86_EIP);
641
77
          else
642
            // Section 2.2.1.6
643
77
            MCOperand_CreateReg0(mcInst, insn->addressSize == 4 ? X86_EIP : X86_RIP);
644
659
        } else {
645
659
          MCOperand_CreateReg0(mcInst, 0);
646
659
        }
647
648
736
        indexReg = 0;
649
736
        break;
650
4.84k
      case EA_BASE_BX_SI:
651
4.84k
        MCOperand_CreateReg0(mcInst, X86_BX);
652
4.84k
        indexReg = X86_SI;
653
4.84k
        break;
654
1.87k
      case EA_BASE_BX_DI:
655
1.87k
        MCOperand_CreateReg0(mcInst, X86_BX);
656
1.87k
        indexReg = X86_DI;
657
1.87k
        break;
658
1.52k
      case EA_BASE_BP_SI:
659
1.52k
        MCOperand_CreateReg0(mcInst, X86_BP);
660
1.52k
        indexReg = X86_SI;
661
1.52k
        break;
662
582
      case EA_BASE_BP_DI:
663
582
        MCOperand_CreateReg0(mcInst, X86_BP);
664
582
        indexReg = X86_DI;
665
582
        break;
666
26.2k
      default:
667
26.2k
        indexReg = 0;
668
26.2k
        switch (insn->eaBase) {
669
0
          default:
670
            //debug("Unexpected eaBase");
671
0
            return true;
672
            // Here, we will use the fill-ins defined above.  However,
673
            //   BX_SI, BX_DI, BP_SI, and BP_DI are all handled above and
674
            //   sib and sib64 were handled in the top-level if, so they're only
675
            //   placeholders to keep the compiler happy.
676
0
#define ENTRY(x)                                        \
677
26.2k
          case EA_BASE_##x:                                 \
678
26.2k
              MCOperand_CreateReg0(mcInst, X86_##x); break;
679
0
            ALL_EA_BASES
680
0
#undef ENTRY
681
1.42k
#define ENTRY(x) case EA_REG_##x:
682
474
              ALL_REGS
683
0
#undef ENTRY
684
              //debug("A R/M memory operand may not be a register; "
685
              //      "the base field must be a base.");
686
0
              return true;
687
26.2k
        }
688
35.7k
    }
689
690
35.7k
    scaleAmount = 1;
691
35.7k
  }
692
693
44.9k
  MCOperand_CreateImm0(mcInst, scaleAmount);
694
44.9k
  MCOperand_CreateReg0(mcInst, indexReg);
695
44.9k
  MCOperand_CreateImm0(mcInst, insn->displacement);
696
697
44.9k
  MCOperand_CreateReg0(mcInst, segmentRegnums[insn->segmentOverride]);
698
699
44.9k
  return false;
700
44.9k
}
701
702
/// translateRM - Translates an operand stored in the R/M (and possibly SIB)
703
///   byte of an instruction to LLVM form, and appends it to an MCInst.
704
///
705
/// @param mcInst       - The MCInst to append to.
706
/// @param operand      - The operand, as stored in the descriptor table.
707
/// @param insn         - The instruction to extract Mod, R/M, and SIB fields
708
///                       from.
709
/// @return             - 0 on success; nonzero otherwise
710
static bool translateRM(MCInst *mcInst, const OperandSpecifier *operand,
711
    InternalInstruction *insn)
712
66.2k
{
713
66.2k
  switch (operand->type) {
714
0
    default:
715
      //debug("Unexpected type for a R/M operand");
716
0
      return true;
717
5.91k
    case TYPE_R8:
718
6.04k
    case TYPE_R16:
719
6.11k
    case TYPE_R32:
720
7.50k
    case TYPE_R64:
721
12.5k
    case TYPE_Rv:
722
12.7k
    case TYPE_MM64:
723
16.8k
    case TYPE_XMM:
724
18.6k
    case TYPE_YMM:
725
20.7k
    case TYPE_ZMM:
726
21.2k
    case TYPE_VK:
727
21.2k
    case TYPE_DEBUGREG:
728
21.2k
    case TYPE_CONTROLREG:
729
21.2k
    case TYPE_BNDR:
730
21.2k
      return translateRMRegister(mcInst, insn);
731
39.3k
    case TYPE_M:
732
40.8k
    case TYPE_MVSIBX:
733
43.2k
    case TYPE_MVSIBY:
734
44.9k
    case TYPE_MVSIBZ:
735
44.9k
      return translateRMMemory(mcInst, insn);
736
66.2k
  }
737
66.2k
}
738
739
/// translateFPRegister - Translates a stack position on the FPU stack to its
740
///   LLVM form, and appends it to an MCInst.
741
///
742
/// @param mcInst       - The MCInst to append to.
743
/// @param stackPos     - The stack position to translate.
744
static void translateFPRegister(MCInst *mcInst, uint8_t stackPos)
745
651
{
746
651
  MCOperand_CreateReg0(mcInst, X86_ST0 + stackPos);
747
651
}
748
749
/// translateMaskRegister - Translates a 3-bit mask register number to
750
///   LLVM form, and appends it to an MCInst.
751
///
752
/// @param mcInst       - The MCInst to append to.
753
/// @param maskRegNum   - Number of mask register from 0 to 7.
754
/// @return             - false on success; true otherwise.
755
static bool translateMaskRegister(MCInst *mcInst, uint8_t maskRegNum)
756
15.9k
{
757
15.9k
  if (maskRegNum >= 8) {
758
    // debug("Invalid mask register number");
759
0
    return true;
760
0
  }
761
762
15.9k
  MCOperand_CreateReg0(mcInst, X86_K0 + maskRegNum);
763
764
15.9k
  return false;
765
15.9k
}
766
767
/// translateOperand - Translates an operand stored in an internal instruction
768
///   to LLVM's format and appends it to an MCInst.
769
///
770
/// @param mcInst       - The MCInst to append to.
771
/// @param operand      - The operand, as stored in the descriptor table.
772
/// @param insn         - The internal instruction.
773
/// @return             - false on success; true otherwise.
774
static bool translateOperand(MCInst *mcInst, const OperandSpecifier *operand, InternalInstruction *insn)
775
246k
{
776
246k
  switch (operand->encoding) {
777
61.6k
    case ENCODING_REG:
778
61.6k
      translateRegister(mcInst, insn->reg);
779
61.6k
      return false;
780
15.9k
    case ENCODING_WRITEMASK:
781
15.9k
      return translateMaskRegister(mcInst, insn->writemask);
782
373k
    CASE_ENCODING_RM:
783
373k
    CASE_ENCODING_VSIB:
784
66.2k
      return translateRM(mcInst, operand, insn);
785
25.4k
    case ENCODING_IB:
786
26.6k
    case ENCODING_IW:
787
27.2k
    case ENCODING_ID:
788
27.2k
    case ENCODING_IO:
789
31.3k
    case ENCODING_Iv:
790
33.0k
    case ENCODING_Ia:
791
33.0k
      translateImmediate(mcInst, insn->immediates[insn->numImmediatesTranslated++], operand, insn);
792
33.0k
      return false;
793
126
    case ENCODING_IRC:
794
126
      MCOperand_CreateImm0(mcInst, insn->RC);
795
126
      return false;
796
2.63k
    case ENCODING_SI:
797
2.63k
      return translateSrcIndex(mcInst, insn);
798
4.88k
    case ENCODING_DI:
799
4.88k
      return translateDstIndex(mcInst, insn);
800
474
    case ENCODING_RB:
801
474
    case ENCODING_RW:
802
474
    case ENCODING_RD:
803
1.44k
    case ENCODING_RO:
804
13.9k
    case ENCODING_Rv:
805
13.9k
      translateRegister(mcInst, insn->opcodeRegister);
806
13.9k
      return false;
807
651
    case ENCODING_FP:
808
651
      translateFPRegister(mcInst, insn->modRM & 7);
809
651
      return false;
810
17.0k
    case ENCODING_VVVV:
811
17.0k
      translateRegister(mcInst, insn->vvvv);
812
17.0k
      return false;
813
30.2k
    case ENCODING_DUP:
814
30.2k
      return translateOperand(mcInst, &insn->operands[operand->type - TYPE_DUP0], insn);
815
0
    default:
816
      //debug("Unhandled operand encoding during translation");
817
0
      return true;
818
246k
  }
819
246k
}
820
821
static bool translateInstruction(MCInst *mcInst, InternalInstruction *insn)
822
103k
{
823
103k
  int index;
824
825
103k
  if (!insn->spec) {
826
    //debug("Instruction has no specification");
827
0
    return true;
828
0
  }
829
830
103k
  MCInst_clear(mcInst);
831
103k
  MCInst_setOpcode(mcInst, insn->instructionID);
832
833
  // If when reading the prefix bytes we determined the overlapping 0xf2 or 0xf3
834
  // prefix bytes should be disassembled as xrelease and xacquire then set the
835
  // opcode to those instead of the rep and repne opcodes.
836
103k
#ifndef CAPSTONE_X86_REDUCE
837
103k
  if (insn->xAcquireRelease) {
838
992
    if (MCInst_getOpcode(mcInst) == X86_REP_PREFIX)
839
0
      MCInst_setOpcode(mcInst, X86_XRELEASE_PREFIX);
840
992
    else if (MCInst_getOpcode(mcInst) == X86_REPNE_PREFIX)
841
0
      MCInst_setOpcode(mcInst, X86_XACQUIRE_PREFIX);
842
992
  }
843
103k
#endif
844
845
103k
  insn->numImmediatesTranslated = 0;
846
847
721k
  for (index = 0; index < X86_MAX_OPERANDS; ++index) {
848
618k
    if (insn->operands[index].encoding != ENCODING_NONE) {
849
216k
      if (translateOperand(mcInst, &insn->operands[index], insn)) {
850
0
        return true;
851
0
      }
852
216k
    }
853
618k
  }
854
855
103k
  return false;
856
103k
}
857
858
static int reader(const struct reader_info *info, uint8_t *byte, uint64_t address)
859
549k
{
860
549k
  if (address - info->offset >= info->size)
861
    // out of buffer range
862
381
    return -1;
863
864
549k
  *byte = info->code[address - info->offset];
865
866
549k
  return 0;
867
549k
}
868
869
// copy x86 detail information from internal structure to public structure
870
static void update_pub_insn(cs_insn *pub, InternalInstruction *inter)
871
103k
{
872
103k
  if (inter->vectorExtensionType != 0) {
873
22.2k
    memcpy(pub->detail->x86.opcode, inter->vectorExtensionPrefix, sizeof(pub->detail->x86.opcode));
874
80.8k
  } else {
875
80.8k
    if (inter->twoByteEscape) {
876
6.15k
      if (inter->threeByteEscape) {
877
0
        pub->detail->x86.opcode[0] = inter->twoByteEscape;
878
0
        pub->detail->x86.opcode[1] = inter->threeByteEscape;
879
0
        pub->detail->x86.opcode[2] = inter->opcode;
880
6.15k
      } else {
881
6.15k
        pub->detail->x86.opcode[0] = inter->twoByteEscape;
882
6.15k
        pub->detail->x86.opcode[1] = inter->opcode;
883
6.15k
      }
884
74.7k
    } else {
885
74.7k
        pub->detail->x86.opcode[0] = inter->opcode;
886
74.7k
    }
887
80.8k
  }
888
889
103k
  pub->detail->x86.rex = inter->rexPrefix;
890
891
103k
  pub->detail->x86.addr_size = inter->addressSize;
892
893
103k
  pub->detail->x86.modrm = inter->orgModRM;
894
103k
  pub->detail->x86.encoding.modrm_offset = inter->modRMOffset;
895
896
103k
  pub->detail->x86.sib = inter->sib;
897
103k
  pub->detail->x86.sib_index = x86_map_sib_index(inter->sibIndex);
898
103k
  pub->detail->x86.sib_scale = inter->sibScale;
899
103k
  pub->detail->x86.sib_base = x86_map_sib_base(inter->sibBase);
900
901
103k
  pub->detail->x86.disp = inter->displacement;
902
103k
  if (inter->consumedDisplacement) {
903
13.2k
    pub->detail->x86.encoding.disp_offset = inter->displacementOffset;
904
13.2k
    pub->detail->x86.encoding.disp_size = inter->displacementSize;
905
13.2k
  }
906
907
103k
  pub->detail->x86.encoding.imm_offset = inter->immediateOffset;
908
103k
  if (pub->detail->x86.encoding.imm_size == 0 && inter->immediateOffset != 0)
909
30.7k
    pub->detail->x86.encoding.imm_size = inter->immediateSize;
910
103k
}
911
912
void X86_init(MCRegisterInfo *MRI)
913
1.62k
{
914
  // InitMCRegisterInfo(), X86GenRegisterInfo.inc
915
  // RI->InitMCRegisterInfo(X86RegDesc, 277,
916
  //                        RA, PC,
917
  //                        X86MCRegisterClasses, 86,
918
  //                        X86RegUnitRoots, 162, X86RegDiffLists, X86LaneMaskLists, X86RegStrings,
919
  //                        X86RegClassStrings,
920
  //                        X86SubRegIdxLists, 9,
921
  //                        X86SubRegIdxRanges, X86RegEncodingTable);
922
  /*
923
     InitMCRegisterInfo(X86RegDesc, 234,
924
     RA, PC,
925
     X86MCRegisterClasses, 79,
926
     X86RegUnitRoots, 119, X86RegDiffLists, X86RegStrings,
927
     X86SubRegIdxLists, 7,
928
     X86SubRegIdxRanges, X86RegEncodingTable);
929
  */
930
931
1.62k
  MCRegisterInfo_InitMCRegisterInfo(MRI, X86RegDesc, 277,
932
1.62k
      0, 0,
933
1.62k
      X86MCRegisterClasses, 86,
934
1.62k
      0, 0, X86RegDiffLists, 0,
935
1.62k
      X86SubRegIdxLists, 9,
936
1.62k
      0);
937
1.62k
}
938
939
// Public interface for the disassembler
940
bool X86_getInstruction(csh ud, const uint8_t *code, size_t code_len,
941
    MCInst *instr, uint16_t *size, uint64_t address, void *_info)
942
103k
{
943
103k
  cs_struct *handle = (cs_struct *)(uintptr_t)ud;
944
103k
  InternalInstruction insn = { 0 };
945
103k
  struct reader_info info;
946
103k
  int ret;
947
103k
  bool result;
948
949
103k
  info.code = code;
950
103k
  info.size = code_len;
951
103k
  info.offset = address;
952
953
103k
  if (instr->flat_insn->detail) {
954
    // instr->flat_insn->detail initialization: 3 alternatives
955
956
    // 1. The whole structure, this is how it's done in other arch disassemblers
957
    // Probably overkill since cs_detail is huge because of the 36 operands of ARM
958
    
959
    //memset(instr->flat_insn->detail, 0, sizeof(cs_detail));
960
961
    // 2. Only the part relevant to x86
962
103k
    memset(instr->flat_insn->detail, 0, offsetof(cs_detail, x86) + sizeof(cs_x86));
963
964
    // 3. The relevant part except for x86.operands
965
    // sizeof(cs_x86) is 0x1c0, sizeof(x86.operands) is 0x180
966
    // marginally faster, should be okay since x86.op_count is set to 0
967
968
    //memset(instr->flat_insn->detail, 0, offsetof(cs_detail, x86)+offsetof(cs_x86, operands));
969
103k
  }
970
971
103k
  if (handle->mode & CS_MODE_16)
972
31.0k
    ret = decodeInstruction(&insn,
973
31.0k
        reader, &info,
974
31.0k
        address,
975
31.0k
        MODE_16BIT);
976
72.6k
  else if (handle->mode & CS_MODE_32)
977
32.0k
    ret = decodeInstruction(&insn,
978
32.0k
        reader, &info,
979
32.0k
        address,
980
32.0k
        MODE_32BIT);
981
40.5k
  else
982
40.5k
    ret = decodeInstruction(&insn,
983
40.5k
        reader, &info,
984
40.5k
        address,
985
40.5k
        MODE_64BIT);
986
987
103k
  if (ret) {
988
    // *size = (uint16_t)(insn.readerCursor - address);
989
632
    return false;
990
103k
  } else {
991
103k
    *size = (uint16_t)insn.length;
992
993
103k
    result = (!translateInstruction(instr, &insn)) ?  true : false;
994
103k
    if (result) {
995
103k
      unsigned Flags = X86_IP_NO_PREFIX;
996
103k
      instr->imm_size = insn.immSize;
997
998
      // copy all prefixes
999
103k
      instr->x86_prefix[0] = insn.prefix0;
1000
103k
      instr->x86_prefix[1] = insn.prefix1;
1001
103k
      instr->x86_prefix[2] = insn.prefix2;
1002
103k
      instr->x86_prefix[3] = insn.prefix3;
1003
103k
      instr->xAcquireRelease = insn.xAcquireRelease;
1004
1005
103k
      if (handle->detail) {
1006
103k
        update_pub_insn(instr->flat_insn, &insn);
1007
103k
      }
1008
1009
103k
      if (insn.hasAdSize)
1010
1.16k
        Flags |= X86_IP_HAS_AD_SIZE;
1011
1012
103k
      if (!insn.mandatoryPrefix) {
1013
100k
        if (insn.hasOpSize)
1014
3.01k
          Flags |= X86_IP_HAS_OP_SIZE;
1015
1016
100k
        if (insn.repeatPrefix == 0xf2)
1017
3.27k
          Flags |= X86_IP_HAS_REPEAT_NE;
1018
97.6k
        else if (insn.repeatPrefix == 0xf3 &&
1019
            // It should not be 'pause' f3 90
1020
3.52k
            insn.opcode != 0x90)
1021
3.39k
          Flags |= X86_IP_HAS_REPEAT;
1022
100k
        if (insn.hasLockPrefix)
1023
4.87k
          Flags |= X86_IP_HAS_LOCK;
1024
100k
      }
1025
1026
103k
      instr->flags = Flags;
1027
103k
    }
1028
1029
103k
    return result;
1030
103k
  }
1031
103k
}
1032
1033
#endif