Coverage Report

Created: 2025-10-14 06:42

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/capstonenext/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
#ifdef _MSC_VER
82
// disable MSVC's warning on strncpy()
83
#pragma warning(disable : 4996)
84
// disable MSVC's warning on strncpy()
85
#pragma warning(disable : 28719)
86
#endif
87
88
#include <capstone/platform.h>
89
90
#if defined(CAPSTONE_HAS_OSXKERNEL)
91
#include <Availability.h>
92
#endif
93
94
#include <string.h>
95
96
#include "../../cs_priv.h"
97
98
#include "X86BaseInfo.h"
99
#include "X86Disassembler.h"
100
#include "X86DisassemblerDecoderCommon.h"
101
#include "X86DisassemblerDecoder.h"
102
#include "../../MCInst.h"
103
#include "../../utils.h"
104
#include "X86Mapping.h"
105
106
#define GET_REGINFO_ENUM
107
#define GET_REGINFO_MC_DESC
108
#include "X86GenRegisterInfo.inc"
109
110
#define GET_INSTRINFO_ENUM
111
#ifdef CAPSTONE_X86_REDUCE
112
#include "X86GenInstrInfo_reduce.inc"
113
#else
114
#include "X86GenInstrInfo.inc"
115
#endif
116
117
// Fill-ins to make the compiler happy.  These constants are never actually
118
//   assigned; they are just filler to make an automatically-generated switch
119
//   statement work.
120
enum {
121
  X86_BX_SI = 500,
122
  X86_BX_DI = 501,
123
  X86_BP_SI = 502,
124
  X86_BP_DI = 503,
125
  X86_sib = 504,
126
  X86_sib64 = 505
127
};
128
129
//
130
// Private code that translates from struct InternalInstructions to MCInsts.
131
//
132
133
/// translateRegister - Translates an internal register to the appropriate LLVM
134
///   register, and appends it as an operand to an MCInst.
135
///
136
/// @param mcInst     - The MCInst to append to.
137
/// @param reg        - The Reg to append.
138
static void translateRegister(MCInst *mcInst, Reg reg)
139
579k
{
140
129M
#define ENTRY(x) X86_##x,
141
579k
  static const uint16_t llvmRegnums[] = { ALL_REGS 0 };
142
579k
#undef ENTRY
143
144
579k
  uint16_t llvmRegnum = llvmRegnums[reg];
145
579k
  MCOperand_CreateReg0(mcInst, llvmRegnum);
146
579k
}
147
148
static const uint8_t segmentRegnums[SEG_OVERRIDE_max] = {
149
  0, // SEG_OVERRIDE_NONE
150
  X86_CS, X86_SS, X86_DS, X86_ES, X86_FS, X86_GS
151
};
152
153
/// translateSrcIndex   - Appends a source index operand to an MCInst.
154
///
155
/// @param mcInst       - The MCInst to append to.
156
/// @param insn         - The internal instruction.
157
static bool translateSrcIndex(MCInst *mcInst, InternalInstruction *insn)
158
35.4k
{
159
35.4k
  unsigned baseRegNo;
160
161
35.4k
  if (insn->mode == MODE_64BIT)
162
12.9k
    baseRegNo = insn->hasAdSize ? X86_ESI : X86_RSI;
163
22.4k
  else if (insn->mode == MODE_32BIT)
164
10.2k
    baseRegNo = insn->hasAdSize ? X86_SI : X86_ESI;
165
12.1k
  else {
166
    // assert(insn->mode == MODE_16BIT);
167
12.1k
    baseRegNo = insn->hasAdSize ? X86_ESI : X86_SI;
168
12.1k
  }
169
170
35.4k
  MCOperand_CreateReg0(mcInst, baseRegNo);
171
172
35.4k
  MCOperand_CreateReg0(mcInst, segmentRegnums[insn->segmentOverride]);
173
174
35.4k
  return false;
175
35.4k
}
176
177
/// translateDstIndex   - Appends a destination index operand to an MCInst.
178
///
179
/// @param mcInst       - The MCInst to append to.
180
/// @param insn         - The internal instruction.
181
static bool translateDstIndex(MCInst *mcInst, InternalInstruction *insn)
182
41.8k
{
183
41.8k
  unsigned baseRegNo;
184
185
41.8k
  if (insn->mode == MODE_64BIT)
186
14.3k
    baseRegNo = insn->hasAdSize ? X86_EDI : X86_RDI;
187
27.4k
  else if (insn->mode == MODE_32BIT)
188
14.0k
    baseRegNo = insn->hasAdSize ? X86_DI : X86_EDI;
189
13.3k
  else {
190
    // assert(insn->mode == MODE_16BIT);
191
13.3k
    baseRegNo = insn->hasAdSize ? X86_EDI : X86_DI;
192
13.3k
  }
193
194
41.8k
  MCOperand_CreateReg0(mcInst, baseRegNo);
195
196
41.8k
  return false;
197
41.8k
}
198
199
/// translateImmediate  - Appends an immediate operand to an MCInst.
200
///
201
/// @param mcInst       - The MCInst to append to.
202
/// @param immediate    - The immediate value to append.
203
/// @param operand      - The operand, as stored in the descriptor table.
204
/// @param insn         - The internal instruction.
205
static void translateImmediate(MCInst *mcInst, uint64_t immediate,
206
             const OperandSpecifier *operand,
207
             InternalInstruction *insn)
208
229k
{
209
229k
  OperandType type;
210
211
229k
  type = (OperandType)operand->type;
212
229k
  if (type == TYPE_REL) {
213
    //isBranch = true;
214
    //pcrel = insn->startLocation + insn->immediateOffset + insn->immediateSize;
215
54.7k
    switch (operand->encoding) {
216
0
    default:
217
0
      break;
218
3.76k
    case ENCODING_Iv:
219
3.76k
      switch (insn->displacementSize) {
220
0
      default:
221
0
        break;
222
0
      case 1:
223
0
        if (immediate & 0x80)
224
0
          immediate |= ~(0xffull);
225
0
        break;
226
2.28k
      case 2:
227
2.28k
        if (immediate & 0x8000)
228
1.75k
          immediate |= ~(0xffffull);
229
2.28k
        break;
230
1.48k
      case 4:
231
1.48k
        if (immediate & 0x80000000)
232
930
          immediate |= ~(0xffffffffull);
233
1.48k
        break;
234
0
      case 8:
235
0
        break;
236
3.76k
      }
237
3.76k
      break;
238
48.3k
    case ENCODING_IB:
239
48.3k
      if (immediate & 0x80)
240
16.4k
        immediate |= ~(0xffull);
241
48.3k
      break;
242
1.24k
    case ENCODING_IW:
243
1.24k
      if (immediate & 0x8000)
244
588
        immediate |= ~(0xffffull);
245
1.24k
      break;
246
1.38k
    case ENCODING_ID:
247
1.38k
      if (immediate & 0x80000000)
248
726
        immediate |= ~(0xffffffffull);
249
1.38k
      break;
250
54.7k
    }
251
54.7k
  } // By default sign-extend all X86 immediates based on their encoding.
252
175k
  else if (type == TYPE_IMM) {
253
100k
    switch (operand->encoding) {
254
29.6k
    default:
255
29.6k
      break;
256
58.7k
    case ENCODING_IB:
257
58.7k
      if (immediate & 0x80)
258
21.0k
        immediate |= ~(0xffull);
259
58.7k
      break;
260
8.78k
    case ENCODING_IW:
261
8.78k
      if (immediate & 0x8000)
262
4.48k
        immediate |= ~(0xffffull);
263
8.78k
      break;
264
2.63k
    case ENCODING_ID:
265
2.63k
      if (immediate & 0x80000000)
266
1.21k
        immediate |= ~(0xffffffffull);
267
2.63k
      break;
268
543
    case ENCODING_IO:
269
543
      break;
270
100k
    }
271
100k
  } else if (type == TYPE_IMM3) {
272
11.2k
#ifndef CAPSTONE_X86_REDUCE
273
    // Check for immediates that printSSECC can't handle.
274
11.2k
    if (immediate >= 8) {
275
5.87k
      unsigned NewOpc = 0;
276
277
5.87k
      switch (MCInst_getOpcode(mcInst)) {
278
0
      default:
279
0
        break; // never reach
280
78
      case X86_CMPPDrmi:
281
78
        NewOpc = X86_CMPPDrmi_alt;
282
78
        break;
283
85
      case X86_CMPPDrri:
284
85
        NewOpc = X86_CMPPDrri_alt;
285
85
        break;
286
853
      case X86_CMPPSrmi:
287
853
        NewOpc = X86_CMPPSrmi_alt;
288
853
        break;
289
569
      case X86_CMPPSrri:
290
569
        NewOpc = X86_CMPPSrri_alt;
291
569
        break;
292
533
      case X86_CMPSDrm:
293
533
        NewOpc = X86_CMPSDrm_alt;
294
533
        break;
295
411
      case X86_CMPSDrr:
296
411
        NewOpc = X86_CMPSDrr_alt;
297
411
        break;
298
544
      case X86_CMPSSrm:
299
544
        NewOpc = X86_CMPSSrm_alt;
300
544
        break;
301
462
      case X86_CMPSSrr:
302
462
        NewOpc = X86_CMPSSrr_alt;
303
462
        break;
304
109
      case X86_VPCOMBri:
305
109
        NewOpc = X86_VPCOMBri_alt;
306
109
        break;
307
73
      case X86_VPCOMBmi:
308
73
        NewOpc = X86_VPCOMBmi_alt;
309
73
        break;
310
46
      case X86_VPCOMWri:
311
46
        NewOpc = X86_VPCOMWri_alt;
312
46
        break;
313
73
      case X86_VPCOMWmi:
314
73
        NewOpc = X86_VPCOMWmi_alt;
315
73
        break;
316
340
      case X86_VPCOMDri:
317
340
        NewOpc = X86_VPCOMDri_alt;
318
340
        break;
319
92
      case X86_VPCOMDmi:
320
92
        NewOpc = X86_VPCOMDmi_alt;
321
92
        break;
322
70
      case X86_VPCOMQri:
323
70
        NewOpc = X86_VPCOMQri_alt;
324
70
        break;
325
212
      case X86_VPCOMQmi:
326
212
        NewOpc = X86_VPCOMQmi_alt;
327
212
        break;
328
103
      case X86_VPCOMUBri:
329
103
        NewOpc = X86_VPCOMUBri_alt;
330
103
        break;
331
76
      case X86_VPCOMUBmi:
332
76
        NewOpc = X86_VPCOMUBmi_alt;
333
76
        break;
334
69
      case X86_VPCOMUWri:
335
69
        NewOpc = X86_VPCOMUWri_alt;
336
69
        break;
337
152
      case X86_VPCOMUWmi:
338
152
        NewOpc = X86_VPCOMUWmi_alt;
339
152
        break;
340
655
      case X86_VPCOMUDri:
341
655
        NewOpc = X86_VPCOMUDri_alt;
342
655
        break;
343
127
      case X86_VPCOMUDmi:
344
127
        NewOpc = X86_VPCOMUDmi_alt;
345
127
        break;
346
68
      case X86_VPCOMUQri:
347
68
        NewOpc = X86_VPCOMUQri_alt;
348
68
        break;
349
73
      case X86_VPCOMUQmi:
350
73
        NewOpc = X86_VPCOMUQmi_alt;
351
73
        break;
352
5.87k
      }
353
354
      // Switch opcode to the one that doesn't get special printing.
355
5.87k
      if (NewOpc != 0) {
356
5.87k
        MCInst_setOpcode(mcInst, NewOpc);
357
5.87k
      }
358
5.87k
    }
359
11.2k
#endif
360
63.5k
  } else if (type == TYPE_IMM5) {
361
12.2k
#ifndef CAPSTONE_X86_REDUCE
362
    // Check for immediates that printAVXCC can't handle.
363
12.2k
    if (immediate >= 32) {
364
9.53k
      unsigned NewOpc = 0;
365
366
9.53k
      switch (MCInst_getOpcode(mcInst)) {
367
3.24k
      default:
368
3.24k
        break; // unexpected opcode
369
3.24k
      case X86_VCMPPDrmi:
370
75
        NewOpc = X86_VCMPPDrmi_alt;
371
75
        break;
372
205
      case X86_VCMPPDrri:
373
205
        NewOpc = X86_VCMPPDrri_alt;
374
205
        break;
375
197
      case X86_VCMPPSrmi:
376
197
        NewOpc = X86_VCMPPSrmi_alt;
377
197
        break;
378
141
      case X86_VCMPPSrri:
379
141
        NewOpc = X86_VCMPPSrri_alt;
380
141
        break;
381
297
      case X86_VCMPSDrm:
382
297
        NewOpc = X86_VCMPSDrm_alt;
383
297
        break;
384
91
      case X86_VCMPSDrr:
385
91
        NewOpc = X86_VCMPSDrr_alt;
386
91
        break;
387
266
      case X86_VCMPSSrm:
388
266
        NewOpc = X86_VCMPSSrm_alt;
389
266
        break;
390
304
      case X86_VCMPSSrr:
391
304
        NewOpc = X86_VCMPSSrr_alt;
392
304
        break;
393
66
      case X86_VCMPPDYrmi:
394
66
        NewOpc = X86_VCMPPDYrmi_alt;
395
66
        break;
396
68
      case X86_VCMPPDYrri:
397
68
        NewOpc = X86_VCMPPDYrri_alt;
398
68
        break;
399
112
      case X86_VCMPPSYrmi:
400
112
        NewOpc = X86_VCMPPSYrmi_alt;
401
112
        break;
402
316
      case X86_VCMPPSYrri:
403
316
        NewOpc = X86_VCMPPSYrri_alt;
404
316
        break;
405
165
      case X86_VCMPPDZrmi:
406
165
        NewOpc = X86_VCMPPDZrmi_alt;
407
165
        break;
408
100
      case X86_VCMPPDZrri:
409
100
        NewOpc = X86_VCMPPDZrri_alt;
410
100
        break;
411
73
      case X86_VCMPPDZrrib:
412
73
        NewOpc = X86_VCMPPDZrrib_alt;
413
73
        break;
414
98
      case X86_VCMPPSZrmi:
415
98
        NewOpc = X86_VCMPPSZrmi_alt;
416
98
        break;
417
120
      case X86_VCMPPSZrri:
418
120
        NewOpc = X86_VCMPPSZrri_alt;
419
120
        break;
420
398
      case X86_VCMPPSZrrib:
421
398
        NewOpc = X86_VCMPPSZrrib_alt;
422
398
        break;
423
76
      case X86_VCMPPDZ128rmi:
424
76
        NewOpc = X86_VCMPPDZ128rmi_alt;
425
76
        break;
426
101
      case X86_VCMPPDZ128rri:
427
101
        NewOpc = X86_VCMPPDZ128rri_alt;
428
101
        break;
429
68
      case X86_VCMPPSZ128rmi:
430
68
        NewOpc = X86_VCMPPSZ128rmi_alt;
431
68
        break;
432
111
      case X86_VCMPPSZ128rri:
433
111
        NewOpc = X86_VCMPPSZ128rri_alt;
434
111
        break;
435
92
      case X86_VCMPPDZ256rmi:
436
92
        NewOpc = X86_VCMPPDZ256rmi_alt;
437
92
        break;
438
223
      case X86_VCMPPDZ256rri:
439
223
        NewOpc = X86_VCMPPDZ256rri_alt;
440
223
        break;
441
334
      case X86_VCMPPSZ256rmi:
442
334
        NewOpc = X86_VCMPPSZ256rmi_alt;
443
334
        break;
444
474
      case X86_VCMPPSZ256rri:
445
474
        NewOpc = X86_VCMPPSZ256rri_alt;
446
474
        break;
447
216
      case X86_VCMPSDZrm_Int:
448
216
        NewOpc = X86_VCMPSDZrmi_alt;
449
216
        break;
450
75
      case X86_VCMPSDZrr_Int:
451
75
        NewOpc = X86_VCMPSDZrri_alt;
452
75
        break;
453
840
      case X86_VCMPSDZrrb_Int:
454
840
        NewOpc = X86_VCMPSDZrrb_alt;
455
840
        break;
456
50
      case X86_VCMPSSZrm_Int:
457
50
        NewOpc = X86_VCMPSSZrmi_alt;
458
50
        break;
459
247
      case X86_VCMPSSZrr_Int:
460
247
        NewOpc = X86_VCMPSSZrri_alt;
461
247
        break;
462
289
      case X86_VCMPSSZrrb_Int:
463
289
        NewOpc = X86_VCMPSSZrrb_alt;
464
289
        break;
465
9.53k
      }
466
467
      // Switch opcode to the one that doesn't get special printing.
468
9.53k
      if (NewOpc != 0) {
469
6.28k
        MCInst_setOpcode(mcInst, NewOpc);
470
6.28k
      }
471
9.53k
    }
472
12.2k
#endif
473
51.3k
  } else if (type == TYPE_AVX512ICC) {
474
11.2k
#ifndef CAPSTONE_X86_REDUCE
475
11.2k
    if (immediate >= 8 || ((immediate & 0x3) == 3)) {
476
6.96k
      unsigned NewOpc = 0;
477
6.96k
      switch (MCInst_getOpcode(mcInst)) {
478
0
      default: // llvm_unreachable("unexpected opcode");
479
323
      case X86_VPCMPBZ128rmi:
480
323
        NewOpc = X86_VPCMPBZ128rmi_alt;
481
323
        break;
482
4
      case X86_VPCMPBZ128rmik:
483
4
        NewOpc = X86_VPCMPBZ128rmik_alt;
484
4
        break;
485
24
      case X86_VPCMPBZ128rri:
486
24
        NewOpc = X86_VPCMPBZ128rri_alt;
487
24
        break;
488
3
      case X86_VPCMPBZ128rrik:
489
3
        NewOpc = X86_VPCMPBZ128rrik_alt;
490
3
        break;
491
63
      case X86_VPCMPBZ256rmi:
492
63
        NewOpc = X86_VPCMPBZ256rmi_alt;
493
63
        break;
494
13
      case X86_VPCMPBZ256rmik:
495
13
        NewOpc = X86_VPCMPBZ256rmik_alt;
496
13
        break;
497
12
      case X86_VPCMPBZ256rri:
498
12
        NewOpc = X86_VPCMPBZ256rri_alt;
499
12
        break;
500
10
      case X86_VPCMPBZ256rrik:
501
10
        NewOpc = X86_VPCMPBZ256rrik_alt;
502
10
        break;
503
237
      case X86_VPCMPBZrmi:
504
237
        NewOpc = X86_VPCMPBZrmi_alt;
505
237
        break;
506
8
      case X86_VPCMPBZrmik:
507
8
        NewOpc = X86_VPCMPBZrmik_alt;
508
8
        break;
509
40
      case X86_VPCMPBZrri:
510
40
        NewOpc = X86_VPCMPBZrri_alt;
511
40
        break;
512
20
      case X86_VPCMPBZrrik:
513
20
        NewOpc = X86_VPCMPBZrrik_alt;
514
20
        break;
515
5
      case X86_VPCMPDZ128rmi:
516
5
        NewOpc = X86_VPCMPDZ128rmi_alt;
517
5
        break;
518
21
      case X86_VPCMPDZ128rmib:
519
21
        NewOpc = X86_VPCMPDZ128rmib_alt;
520
21
        break;
521
215
      case X86_VPCMPDZ128rmibk:
522
215
        NewOpc = X86_VPCMPDZ128rmibk_alt;
523
215
        break;
524
48
      case X86_VPCMPDZ128rmik:
525
48
        NewOpc = X86_VPCMPDZ128rmik_alt;
526
48
        break;
527
90
      case X86_VPCMPDZ128rri:
528
90
        NewOpc = X86_VPCMPDZ128rri_alt;
529
90
        break;
530
53
      case X86_VPCMPDZ128rrik:
531
53
        NewOpc = X86_VPCMPDZ128rrik_alt;
532
53
        break;
533
29
      case X86_VPCMPDZ256rmi:
534
29
        NewOpc = X86_VPCMPDZ256rmi_alt;
535
29
        break;
536
14
      case X86_VPCMPDZ256rmib:
537
14
        NewOpc = X86_VPCMPDZ256rmib_alt;
538
14
        break;
539
27
      case X86_VPCMPDZ256rmibk:
540
27
        NewOpc = X86_VPCMPDZ256rmibk_alt;
541
27
        break;
542
19
      case X86_VPCMPDZ256rmik:
543
19
        NewOpc = X86_VPCMPDZ256rmik_alt;
544
19
        break;
545
48
      case X86_VPCMPDZ256rri:
546
48
        NewOpc = X86_VPCMPDZ256rri_alt;
547
48
        break;
548
38
      case X86_VPCMPDZ256rrik:
549
38
        NewOpc = X86_VPCMPDZ256rrik_alt;
550
38
        break;
551
128
      case X86_VPCMPDZrmi:
552
128
        NewOpc = X86_VPCMPDZrmi_alt;
553
128
        break;
554
36
      case X86_VPCMPDZrmib:
555
36
        NewOpc = X86_VPCMPDZrmib_alt;
556
36
        break;
557
11
      case X86_VPCMPDZrmibk:
558
11
        NewOpc = X86_VPCMPDZrmibk_alt;
559
11
        break;
560
339
      case X86_VPCMPDZrmik:
561
339
        NewOpc = X86_VPCMPDZrmik_alt;
562
339
        break;
563
19
      case X86_VPCMPDZrri:
564
19
        NewOpc = X86_VPCMPDZrri_alt;
565
19
        break;
566
91
      case X86_VPCMPDZrrik:
567
91
        NewOpc = X86_VPCMPDZrrik_alt;
568
91
        break;
569
10
      case X86_VPCMPQZ128rmi:
570
10
        NewOpc = X86_VPCMPQZ128rmi_alt;
571
10
        break;
572
79
      case X86_VPCMPQZ128rmib:
573
79
        NewOpc = X86_VPCMPQZ128rmib_alt;
574
79
        break;
575
18
      case X86_VPCMPQZ128rmibk:
576
18
        NewOpc = X86_VPCMPQZ128rmibk_alt;
577
18
        break;
578
8
      case X86_VPCMPQZ128rmik:
579
8
        NewOpc = X86_VPCMPQZ128rmik_alt;
580
8
        break;
581
173
      case X86_VPCMPQZ128rri:
582
173
        NewOpc = X86_VPCMPQZ128rri_alt;
583
173
        break;
584
0
      case X86_VPCMPQZ128rrik:
585
0
        NewOpc = X86_VPCMPQZ128rrik_alt;
586
0
        break;
587
74
      case X86_VPCMPQZ256rmi:
588
74
        NewOpc = X86_VPCMPQZ256rmi_alt;
589
74
        break;
590
76
      case X86_VPCMPQZ256rmib:
591
76
        NewOpc = X86_VPCMPQZ256rmib_alt;
592
76
        break;
593
292
      case X86_VPCMPQZ256rmibk:
594
292
        NewOpc = X86_VPCMPQZ256rmibk_alt;
595
292
        break;
596
40
      case X86_VPCMPQZ256rmik:
597
40
        NewOpc = X86_VPCMPQZ256rmik_alt;
598
40
        break;
599
247
      case X86_VPCMPQZ256rri:
600
247
        NewOpc = X86_VPCMPQZ256rri_alt;
601
247
        break;
602
78
      case X86_VPCMPQZ256rrik:
603
78
        NewOpc = X86_VPCMPQZ256rrik_alt;
604
78
        break;
605
46
      case X86_VPCMPQZrmi:
606
46
        NewOpc = X86_VPCMPQZrmi_alt;
607
46
        break;
608
6
      case X86_VPCMPQZrmib:
609
6
        NewOpc = X86_VPCMPQZrmib_alt;
610
6
        break;
611
173
      case X86_VPCMPQZrmibk:
612
173
        NewOpc = X86_VPCMPQZrmibk_alt;
613
173
        break;
614
19
      case X86_VPCMPQZrmik:
615
19
        NewOpc = X86_VPCMPQZrmik_alt;
616
19
        break;
617
15
      case X86_VPCMPQZrri:
618
15
        NewOpc = X86_VPCMPQZrri_alt;
619
15
        break;
620
1
      case X86_VPCMPQZrrik:
621
1
        NewOpc = X86_VPCMPQZrrik_alt;
622
1
        break;
623
4
      case X86_VPCMPUBZ128rmi:
624
4
        NewOpc = X86_VPCMPUBZ128rmi_alt;
625
4
        break;
626
36
      case X86_VPCMPUBZ128rmik:
627
36
        NewOpc = X86_VPCMPUBZ128rmik_alt;
628
36
        break;
629
4
      case X86_VPCMPUBZ128rri:
630
4
        NewOpc = X86_VPCMPUBZ128rri_alt;
631
4
        break;
632
11
      case X86_VPCMPUBZ128rrik:
633
11
        NewOpc = X86_VPCMPUBZ128rrik_alt;
634
11
        break;
635
128
      case X86_VPCMPUBZ256rmi:
636
128
        NewOpc = X86_VPCMPUBZ256rmi_alt;
637
128
        break;
638
7
      case X86_VPCMPUBZ256rmik:
639
7
        NewOpc = X86_VPCMPUBZ256rmik_alt;
640
7
        break;
641
310
      case X86_VPCMPUBZ256rri:
642
310
        NewOpc = X86_VPCMPUBZ256rri_alt;
643
310
        break;
644
75
      case X86_VPCMPUBZ256rrik:
645
75
        NewOpc = X86_VPCMPUBZ256rrik_alt;
646
75
        break;
647
47
      case X86_VPCMPUBZrmi:
648
47
        NewOpc = X86_VPCMPUBZrmi_alt;
649
47
        break;
650
154
      case X86_VPCMPUBZrmik:
651
154
        NewOpc = X86_VPCMPUBZrmik_alt;
652
154
        break;
653
18
      case X86_VPCMPUBZrri:
654
18
        NewOpc = X86_VPCMPUBZrri_alt;
655
18
        break;
656
88
      case X86_VPCMPUBZrrik:
657
88
        NewOpc = X86_VPCMPUBZrrik_alt;
658
88
        break;
659
7
      case X86_VPCMPUDZ128rmi:
660
7
        NewOpc = X86_VPCMPUDZ128rmi_alt;
661
7
        break;
662
7
      case X86_VPCMPUDZ128rmib:
663
7
        NewOpc = X86_VPCMPUDZ128rmib_alt;
664
7
        break;
665
20
      case X86_VPCMPUDZ128rmibk:
666
20
        NewOpc = X86_VPCMPUDZ128rmibk_alt;
667
20
        break;
668
118
      case X86_VPCMPUDZ128rmik:
669
118
        NewOpc = X86_VPCMPUDZ128rmik_alt;
670
118
        break;
671
9
      case X86_VPCMPUDZ128rri:
672
9
        NewOpc = X86_VPCMPUDZ128rri_alt;
673
9
        break;
674
3
      case X86_VPCMPUDZ128rrik:
675
3
        NewOpc = X86_VPCMPUDZ128rrik_alt;
676
3
        break;
677
8
      case X86_VPCMPUDZ256rmi:
678
8
        NewOpc = X86_VPCMPUDZ256rmi_alt;
679
8
        break;
680
70
      case X86_VPCMPUDZ256rmib:
681
70
        NewOpc = X86_VPCMPUDZ256rmib_alt;
682
70
        break;
683
11
      case X86_VPCMPUDZ256rmibk:
684
11
        NewOpc = X86_VPCMPUDZ256rmibk_alt;
685
11
        break;
686
35
      case X86_VPCMPUDZ256rmik:
687
35
        NewOpc = X86_VPCMPUDZ256rmik_alt;
688
35
        break;
689
1
      case X86_VPCMPUDZ256rri:
690
1
        NewOpc = X86_VPCMPUDZ256rri_alt;
691
1
        break;
692
16
      case X86_VPCMPUDZ256rrik:
693
16
        NewOpc = X86_VPCMPUDZ256rrik_alt;
694
16
        break;
695
2
      case X86_VPCMPUDZrmi:
696
2
        NewOpc = X86_VPCMPUDZrmi_alt;
697
2
        break;
698
149
      case X86_VPCMPUDZrmib:
699
149
        NewOpc = X86_VPCMPUDZrmib_alt;
700
149
        break;
701
68
      case X86_VPCMPUDZrmibk:
702
68
        NewOpc = X86_VPCMPUDZrmibk_alt;
703
68
        break;
704
33
      case X86_VPCMPUDZrmik:
705
33
        NewOpc = X86_VPCMPUDZrmik_alt;
706
33
        break;
707
5
      case X86_VPCMPUDZrri:
708
5
        NewOpc = X86_VPCMPUDZrri_alt;
709
5
        break;
710
90
      case X86_VPCMPUDZrrik:
711
90
        NewOpc = X86_VPCMPUDZrrik_alt;
712
90
        break;
713
6
      case X86_VPCMPUQZ128rmi:
714
6
        NewOpc = X86_VPCMPUQZ128rmi_alt;
715
6
        break;
716
73
      case X86_VPCMPUQZ128rmib:
717
73
        NewOpc = X86_VPCMPUQZ128rmib_alt;
718
73
        break;
719
29
      case X86_VPCMPUQZ128rmibk:
720
29
        NewOpc = X86_VPCMPUQZ128rmibk_alt;
721
29
        break;
722
32
      case X86_VPCMPUQZ128rmik:
723
32
        NewOpc = X86_VPCMPUQZ128rmik_alt;
724
32
        break;
725
42
      case X86_VPCMPUQZ128rri:
726
42
        NewOpc = X86_VPCMPUQZ128rri_alt;
727
42
        break;
728
4
      case X86_VPCMPUQZ128rrik:
729
4
        NewOpc = X86_VPCMPUQZ128rrik_alt;
730
4
        break;
731
61
      case X86_VPCMPUQZ256rmi:
732
61
        NewOpc = X86_VPCMPUQZ256rmi_alt;
733
61
        break;
734
79
      case X86_VPCMPUQZ256rmib:
735
79
        NewOpc = X86_VPCMPUQZ256rmib_alt;
736
79
        break;
737
99
      case X86_VPCMPUQZ256rmibk:
738
99
        NewOpc = X86_VPCMPUQZ256rmibk_alt;
739
99
        break;
740
41
      case X86_VPCMPUQZ256rmik:
741
41
        NewOpc = X86_VPCMPUQZ256rmik_alt;
742
41
        break;
743
9
      case X86_VPCMPUQZ256rri:
744
9
        NewOpc = X86_VPCMPUQZ256rri_alt;
745
9
        break;
746
73
      case X86_VPCMPUQZ256rrik:
747
73
        NewOpc = X86_VPCMPUQZ256rrik_alt;
748
73
        break;
749
32
      case X86_VPCMPUQZrmi:
750
32
        NewOpc = X86_VPCMPUQZrmi_alt;
751
32
        break;
752
36
      case X86_VPCMPUQZrmib:
753
36
        NewOpc = X86_VPCMPUQZrmib_alt;
754
36
        break;
755
110
      case X86_VPCMPUQZrmibk:
756
110
        NewOpc = X86_VPCMPUQZrmibk_alt;
757
110
        break;
758
13
      case X86_VPCMPUQZrmik:
759
13
        NewOpc = X86_VPCMPUQZrmik_alt;
760
13
        break;
761
47
      case X86_VPCMPUQZrri:
762
47
        NewOpc = X86_VPCMPUQZrri_alt;
763
47
        break;
764
68
      case X86_VPCMPUQZrrik:
765
68
        NewOpc = X86_VPCMPUQZrrik_alt;
766
68
        break;
767
135
      case X86_VPCMPUWZ128rmi:
768
135
        NewOpc = X86_VPCMPUWZ128rmi_alt;
769
135
        break;
770
84
      case X86_VPCMPUWZ128rmik:
771
84
        NewOpc = X86_VPCMPUWZ128rmik_alt;
772
84
        break;
773
0
      case X86_VPCMPUWZ128rri:
774
0
        NewOpc = X86_VPCMPUWZ128rri_alt;
775
0
        break;
776
10
      case X86_VPCMPUWZ128rrik:
777
10
        NewOpc = X86_VPCMPUWZ128rrik_alt;
778
10
        break;
779
99
      case X86_VPCMPUWZ256rmi:
780
99
        NewOpc = X86_VPCMPUWZ256rmi_alt;
781
99
        break;
782
28
      case X86_VPCMPUWZ256rmik:
783
28
        NewOpc = X86_VPCMPUWZ256rmik_alt;
784
28
        break;
785
23
      case X86_VPCMPUWZ256rri:
786
23
        NewOpc = X86_VPCMPUWZ256rri_alt;
787
23
        break;
788
26
      case X86_VPCMPUWZ256rrik:
789
26
        NewOpc = X86_VPCMPUWZ256rrik_alt;
790
26
        break;
791
167
      case X86_VPCMPUWZrmi:
792
167
        NewOpc = X86_VPCMPUWZrmi_alt;
793
167
        break;
794
111
      case X86_VPCMPUWZrmik:
795
111
        NewOpc = X86_VPCMPUWZrmik_alt;
796
111
        break;
797
41
      case X86_VPCMPUWZrri:
798
41
        NewOpc = X86_VPCMPUWZrri_alt;
799
41
        break;
800
122
      case X86_VPCMPUWZrrik:
801
122
        NewOpc = X86_VPCMPUWZrrik_alt;
802
122
        break;
803
33
      case X86_VPCMPWZ128rmi:
804
33
        NewOpc = X86_VPCMPWZ128rmi_alt;
805
33
        break;
806
5
      case X86_VPCMPWZ128rmik:
807
5
        NewOpc = X86_VPCMPWZ128rmik_alt;
808
5
        break;
809
51
      case X86_VPCMPWZ128rri:
810
51
        NewOpc = X86_VPCMPWZ128rri_alt;
811
51
        break;
812
18
      case X86_VPCMPWZ128rrik:
813
18
        NewOpc = X86_VPCMPWZ128rrik_alt;
814
18
        break;
815
15
      case X86_VPCMPWZ256rmi:
816
15
        NewOpc = X86_VPCMPWZ256rmi_alt;
817
15
        break;
818
71
      case X86_VPCMPWZ256rmik:
819
71
        NewOpc = X86_VPCMPWZ256rmik_alt;
820
71
        break;
821
145
      case X86_VPCMPWZ256rri:
822
145
        NewOpc = X86_VPCMPWZ256rri_alt;
823
145
        break;
824
3
      case X86_VPCMPWZ256rrik:
825
3
        NewOpc = X86_VPCMPWZ256rrik_alt;
826
3
        break;
827
25
      case X86_VPCMPWZrmi:
828
25
        NewOpc = X86_VPCMPWZrmi_alt;
829
25
        break;
830
14
      case X86_VPCMPWZrmik:
831
14
        NewOpc = X86_VPCMPWZrmik_alt;
832
14
        break;
833
6
      case X86_VPCMPWZrri:
834
6
        NewOpc = X86_VPCMPWZrri_alt;
835
6
        break;
836
6
      case X86_VPCMPWZrrik:
837
6
        NewOpc = X86_VPCMPWZrrik_alt;
838
6
        break;
839
6.96k
      }
840
841
      // Switch opcode to the one that doesn't get special printing.
842
6.96k
      if (NewOpc != 0) {
843
6.96k
        MCInst_setOpcode(mcInst, NewOpc);
844
6.96k
      }
845
6.96k
    }
846
11.2k
#endif
847
11.2k
  }
848
849
229k
  switch (type) {
850
645
  case TYPE_XMM:
851
645
    MCOperand_CreateReg0(mcInst,
852
645
             X86_XMM0 + ((uint32_t)immediate >> 4));
853
645
    return;
854
1.25k
  case TYPE_YMM:
855
1.25k
    MCOperand_CreateReg0(mcInst,
856
1.25k
             X86_YMM0 + ((uint32_t)immediate >> 4));
857
1.25k
    return;
858
0
  case TYPE_ZMM:
859
0
    MCOperand_CreateReg0(mcInst,
860
0
             X86_ZMM0 + ((uint32_t)immediate >> 4));
861
0
    return;
862
227k
  default:
863
    // operand is 64 bits wide.  Do nothing.
864
227k
    break;
865
229k
  }
866
867
227k
  MCOperand_CreateImm0(mcInst, immediate);
868
869
227k
  if (type == TYPE_MOFFS) {
870
9.62k
    MCOperand_CreateReg0(mcInst,
871
9.62k
             segmentRegnums[insn->segmentOverride]);
872
9.62k
  }
873
227k
}
874
875
/// translateRMRegister - Translates a register stored in the R/M field of the
876
///   ModR/M byte to its LLVM equivalent and appends it to an MCInst.
877
/// @param mcInst       - The MCInst to append to.
878
/// @param insn         - The internal instruction to extract the R/M field
879
///                       from.
880
/// @return             - 0 on success; -1 otherwise
881
static bool translateRMRegister(MCInst *mcInst, InternalInstruction *insn)
882
165k
{
883
165k
  if (insn->eaBase == EA_BASE_sib || insn->eaBase == EA_BASE_sib64) {
884
    //debug("A R/M register operand may not have a SIB byte");
885
0
    return true;
886
0
  }
887
888
165k
  switch (insn->eaBase) {
889
0
  case EA_BASE_NONE:
890
    //debug("EA_BASE_NONE for ModR/M base");
891
0
    return true;
892
0
#define ENTRY(x) case EA_BASE_##x:
893
0
    ALL_EA_BASES
894
0
#undef ENTRY
895
    //debug("A R/M register operand may not have a base; "
896
    //      "the operand must be a register.");
897
0
    return true;
898
0
#define ENTRY(x) \
899
165k
  case EA_REG_##x: \
900
165k
    MCOperand_CreateReg0(mcInst, X86_##x); \
901
165k
    break;
902
0
    ALL_REGS
903
0
#undef ENTRY
904
0
  default:
905
    //debug("Unexpected EA base register");
906
0
    return true;
907
165k
  }
908
909
165k
  return false;
910
165k
}
911
912
/// translateRMMemory - Translates a memory operand stored in the Mod and R/M
913
///   fields of an internal instruction (and possibly its SIB byte) to a memory
914
///   operand in LLVM's format, and appends it to an MCInst.
915
///
916
/// @param mcInst       - The MCInst to append to.
917
/// @param insn         - The instruction to extract Mod, R/M, and SIB fields
918
///                       from.
919
/// @return             - 0 on success; nonzero otherwise
920
static bool translateRMMemory(MCInst *mcInst, InternalInstruction *insn)
921
314k
{
922
  // Addresses in an MCInst are represented as five operands:
923
  //   1. basereg       (register)  The R/M base, or (if there is a SIB) the
924
  //                                SIB base
925
  //   2. scaleamount   (immediate) 1, or (if there is a SIB) the specified
926
  //                                scale amount
927
  //   3. indexreg      (register)  x86_registerNONE, or (if there is a SIB)
928
  //                                the index (which is multiplied by the
929
  //                                scale amount)
930
  //   4. displacement  (immediate) 0, or the displacement if there is one
931
  //   5. segmentreg    (register)  x86_registerNONE for now, but could be set
932
  //                                if we have segment overrides
933
314k
  int scaleAmount, indexReg;
934
935
314k
  if (insn->eaBase == EA_BASE_sib || insn->eaBase == EA_BASE_sib64) {
936
23.9k
    if (insn->sibBase != SIB_BASE_NONE) {
937
22.5k
      switch (insn->sibBase) {
938
0
#define ENTRY(x) \
939
22.5k
  case SIB_BASE_##x: \
940
22.5k
    MCOperand_CreateReg0(mcInst, X86_##x); \
941
22.5k
    break;
942
0
        ALL_SIB_BASES
943
0
#undef ENTRY
944
0
      default:
945
        //debug("Unexpected sibBase");
946
0
        return true;
947
22.5k
      }
948
22.5k
    } else {
949
1.39k
      MCOperand_CreateReg0(mcInst, 0);
950
1.39k
    }
951
952
23.9k
    if (insn->sibIndex != SIB_INDEX_NONE) {
953
19.6k
      switch (insn->sibIndex) {
954
0
      default:
955
        //debug("Unexpected sibIndex");
956
0
        return true;
957
0
#define ENTRY(x) \
958
19.6k
  case SIB_INDEX_##x: \
959
19.6k
    indexReg = X86_##x; \
960
19.6k
    break;
961
0
        EA_BASES_32BIT
962
176
        EA_BASES_64BIT
963
236
        REGS_XMM
964
22
        REGS_YMM
965
19.6k
        REGS_ZMM
966
19.6k
#undef ENTRY
967
19.6k
      }
968
19.6k
    } else {
969
      // Use EIZ/RIZ for a few ambiguous cases where the SIB byte is present,
970
      // but no index is used and modrm alone should have been enough.
971
      // -No base register in 32-bit mode. In 64-bit mode this is used to
972
      //  avoid rip-relative addressing.
973
      // -Any base register used other than ESP/RSP/R12D/R12. Using these as a
974
      //  base always requires a SIB byte.
975
      // -A scale other than 1 is used.
976
4.32k
      if (insn->sibScale != 1 ||
977
2.49k
          (insn->sibBase == SIB_BASE_NONE &&
978
511
           insn->mode != MODE_64BIT) ||
979
2.24k
          (insn->sibBase != SIB_BASE_NONE &&
980
1.98k
           insn->sibBase != SIB_BASE_ESP &&
981
1.53k
           insn->sibBase != SIB_BASE_RSP &&
982
1.17k
           insn->sibBase != SIB_BASE_R12D &&
983
2.66k
           insn->sibBase != SIB_BASE_R12)) {
984
2.66k
        indexReg = insn->addressSize == 4 ? X86_EIZ :
985
2.66k
                    X86_RIZ;
986
2.66k
      } else
987
1.65k
        indexReg = 0;
988
4.32k
    }
989
990
23.9k
    scaleAmount = insn->sibScale;
991
290k
  } else {
992
290k
    switch (insn->eaBase) {
993
6.31k
    case EA_BASE_NONE:
994
6.31k
      if (insn->eaDisplacement == EA_DISP_NONE) {
995
        //debug("EA_BASE_NONE and EA_DISP_NONE for ModR/M base");
996
0
        return true;
997
0
      }
998
6.31k
      if (insn->mode == MODE_64BIT) {
999
1.28k
        if (insn->prefix3 ==
1000
1.28k
            0x67) // address-size prefix overrides RIP relative addressing
1001
203
          MCOperand_CreateReg0(mcInst, X86_EIP);
1002
1.08k
        else
1003
          // Section 2.2.1.6
1004
1.08k
          MCOperand_CreateReg0(
1005
1.08k
            mcInst, insn->addressSize == 4 ?
1006
0
                X86_EIP :
1007
1.08k
                X86_RIP);
1008
5.02k
      } else {
1009
5.02k
        MCOperand_CreateReg0(mcInst, 0);
1010
5.02k
      }
1011
1012
6.31k
      indexReg = 0;
1013
6.31k
      break;
1014
37.0k
    case EA_BASE_BX_SI:
1015
37.0k
      MCOperand_CreateReg0(mcInst, X86_BX);
1016
37.0k
      indexReg = X86_SI;
1017
37.0k
      break;
1018
16.3k
    case EA_BASE_BX_DI:
1019
16.3k
      MCOperand_CreateReg0(mcInst, X86_BX);
1020
16.3k
      indexReg = X86_DI;
1021
16.3k
      break;
1022
9.00k
    case EA_BASE_BP_SI:
1023
9.00k
      MCOperand_CreateReg0(mcInst, X86_BP);
1024
9.00k
      indexReg = X86_SI;
1025
9.00k
      break;
1026
9.47k
    case EA_BASE_BP_DI:
1027
9.47k
      MCOperand_CreateReg0(mcInst, X86_BP);
1028
9.47k
      indexReg = X86_DI;
1029
9.47k
      break;
1030
212k
    default:
1031
212k
      indexReg = 0;
1032
212k
      switch (insn->eaBase) {
1033
0
      default:
1034
        //debug("Unexpected eaBase");
1035
0
        return true;
1036
        // Here, we will use the fill-ins defined above.  However,
1037
        //   BX_SI, BX_DI, BP_SI, and BP_DI are all handled above and
1038
        //   sib and sib64 were handled in the top-level if, so they're only
1039
        //   placeholders to keep the compiler happy.
1040
0
#define ENTRY(x) \
1041
212k
  case EA_BASE_##x: \
1042
212k
    MCOperand_CreateReg0(mcInst, X86_##x); \
1043
212k
    break;
1044
0
        ALL_EA_BASES
1045
0
#undef ENTRY
1046
3.74k
#define ENTRY(x) case EA_REG_##x:
1047
1.24k
        ALL_REGS
1048
0
#undef ENTRY
1049
        //debug("A R/M memory operand may not be a register; "
1050
        //      "the base field must be a base.");
1051
0
        return true;
1052
212k
      }
1053
290k
    }
1054
1055
290k
    scaleAmount = 1;
1056
290k
  }
1057
1058
314k
  MCOperand_CreateImm0(mcInst, scaleAmount);
1059
314k
  MCOperand_CreateReg0(mcInst, indexReg);
1060
314k
  MCOperand_CreateImm0(mcInst, insn->displacement);
1061
1062
314k
  MCOperand_CreateReg0(mcInst, segmentRegnums[insn->segmentOverride]);
1063
1064
314k
  return false;
1065
314k
}
1066
1067
/// translateRM - Translates an operand stored in the R/M (and possibly SIB)
1068
///   byte of an instruction to LLVM form, and appends it to an MCInst.
1069
///
1070
/// @param mcInst       - The MCInst to append to.
1071
/// @param operand      - The operand, as stored in the descriptor table.
1072
/// @param insn         - The instruction to extract Mod, R/M, and SIB fields
1073
///                       from.
1074
/// @return             - 0 on success; nonzero otherwise
1075
static bool translateRM(MCInst *mcInst, const OperandSpecifier *operand,
1076
      InternalInstruction *insn)
1077
479k
{
1078
479k
  switch (operand->type) {
1079
0
  default:
1080
    //debug("Unexpected type for a R/M operand");
1081
0
    return true;
1082
59.6k
  case TYPE_R8:
1083
60.0k
  case TYPE_R16:
1084
60.9k
  case TYPE_R32:
1085
71.7k
  case TYPE_R64:
1086
125k
  case TYPE_Rv:
1087
127k
  case TYPE_MM64:
1088
144k
  case TYPE_XMM:
1089
152k
  case TYPE_YMM:
1090
162k
  case TYPE_ZMM:
1091
165k
  case TYPE_VK:
1092
165k
  case TYPE_DEBUGREG:
1093
165k
  case TYPE_CONTROLREG:
1094
165k
  case TYPE_BNDR:
1095
165k
    return translateRMRegister(mcInst, insn);
1096
308k
  case TYPE_M:
1097
310k
  case TYPE_MVSIBX:
1098
312k
  case TYPE_MVSIBY:
1099
314k
  case TYPE_MVSIBZ:
1100
314k
    return translateRMMemory(mcInst, insn);
1101
479k
  }
1102
479k
}
1103
1104
/// translateFPRegister - Translates a stack position on the FPU stack to its
1105
///   LLVM form, and appends it to an MCInst.
1106
///
1107
/// @param mcInst       - The MCInst to append to.
1108
/// @param stackPos     - The stack position to translate.
1109
static void translateFPRegister(MCInst *mcInst, uint8_t stackPos)
1110
5.16k
{
1111
5.16k
  MCOperand_CreateReg0(mcInst, X86_ST0 + stackPos);
1112
5.16k
}
1113
1114
/// translateMaskRegister - Translates a 3-bit mask register number to
1115
///   LLVM form, and appends it to an MCInst.
1116
///
1117
/// @param mcInst       - The MCInst to append to.
1118
/// @param maskRegNum   - Number of mask register from 0 to 7.
1119
/// @return             - false on success; true otherwise.
1120
static bool translateMaskRegister(MCInst *mcInst, uint8_t maskRegNum)
1121
41.3k
{
1122
41.3k
  if (maskRegNum >= 8) {
1123
    // debug("Invalid mask register number");
1124
0
    return true;
1125
0
  }
1126
1127
41.3k
  MCOperand_CreateReg0(mcInst, X86_K0 + maskRegNum);
1128
1129
41.3k
  return false;
1130
41.3k
}
1131
1132
/// translateOperand - Translates an operand stored in an internal instruction
1133
///   to LLVM's format and appends it to an MCInst.
1134
///
1135
/// @param mcInst       - The MCInst to append to.
1136
/// @param operand      - The operand, as stored in the descriptor table.
1137
/// @param insn         - The internal instruction.
1138
/// @return             - false on success; true otherwise.
1139
static bool translateOperand(MCInst *mcInst, const OperandSpecifier *operand,
1140
           InternalInstruction *insn)
1141
1.60M
{
1142
1.60M
  switch (operand->encoding) {
1143
395k
  case ENCODING_REG:
1144
395k
    translateRegister(mcInst, insn->reg);
1145
395k
    return false;
1146
41.3k
  case ENCODING_WRITEMASK:
1147
41.3k
    return translateMaskRegister(mcInst, insn->writemask);
1148
3.11M
CASE_ENCODING_RM:
1149
3.11M
CASE_ENCODING_VSIB:
1150
479k
    return translateRM(mcInst, operand, insn);
1151
172k
  case ENCODING_IB:
1152
182k
  case ENCODING_IW:
1153
186k
  case ENCODING_ID:
1154
186k
  case ENCODING_IO:
1155
220k
  case ENCODING_Iv:
1156
229k
  case ENCODING_Ia:
1157
229k
    translateImmediate(
1158
229k
      mcInst,
1159
229k
      insn->immediates[insn->numImmediatesTranslated++],
1160
229k
      operand, insn);
1161
229k
    return false;
1162
2.03k
  case ENCODING_IRC:
1163
2.03k
    MCOperand_CreateImm0(mcInst, insn->RC);
1164
2.03k
    return false;
1165
35.4k
  case ENCODING_SI:
1166
35.4k
    return translateSrcIndex(mcInst, insn);
1167
41.8k
  case ENCODING_DI:
1168
41.8k
    return translateDstIndex(mcInst, insn);
1169
10.0k
  case ENCODING_RB:
1170
10.0k
  case ENCODING_RW:
1171
10.0k
  case ENCODING_RD:
1172
20.7k
  case ENCODING_RO:
1173
133k
  case ENCODING_Rv:
1174
133k
    translateRegister(mcInst, insn->opcodeRegister);
1175
133k
    return false;
1176
5.16k
  case ENCODING_FP:
1177
5.16k
    translateFPRegister(mcInst, insn->modRM & 7);
1178
5.16k
    return false;
1179
50.8k
  case ENCODING_VVVV:
1180
50.8k
    translateRegister(mcInst, insn->vvvv);
1181
50.8k
    return false;
1182
185k
  case ENCODING_DUP:
1183
185k
    return translateOperand(
1184
185k
      mcInst, &insn->operands[operand->type - TYPE_DUP0],
1185
185k
      insn);
1186
0
  default:
1187
    //debug("Unhandled operand encoding during translation");
1188
0
    return true;
1189
1.60M
  }
1190
1.60M
}
1191
1192
static bool translateInstruction(MCInst *mcInst, InternalInstruction *insn)
1193
803k
{
1194
803k
  int index;
1195
1196
803k
  if (!insn->spec) {
1197
    //debug("Instruction has no specification");
1198
0
    return true;
1199
0
  }
1200
1201
803k
  MCInst_clear(mcInst);
1202
803k
  MCInst_setOpcode(mcInst, insn->instructionID);
1203
1204
  // If when reading the prefix bytes we determined the overlapping 0xf2 or 0xf3
1205
  // prefix bytes should be disassembled as xrelease and xacquire then set the
1206
  // opcode to those instead of the rep and repne opcodes.
1207
803k
#ifndef CAPSTONE_X86_REDUCE
1208
803k
  if (insn->xAcquireRelease) {
1209
3.46k
    if (MCInst_getOpcode(mcInst) == X86_REP_PREFIX)
1210
0
      MCInst_setOpcode(mcInst, X86_XRELEASE_PREFIX);
1211
3.46k
    else if (MCInst_getOpcode(mcInst) == X86_REPNE_PREFIX)
1212
0
      MCInst_setOpcode(mcInst, X86_XACQUIRE_PREFIX);
1213
3.46k
  }
1214
803k
#endif
1215
1216
803k
  insn->numImmediatesTranslated = 0;
1217
1218
5.62M
  for (index = 0; index < X86_MAX_OPERANDS; ++index) {
1219
4.81M
    if (insn->operands[index].encoding != ENCODING_NONE) {
1220
1.41M
      if (translateOperand(mcInst, &insn->operands[index],
1221
1.41M
               insn)) {
1222
0
        return true;
1223
0
      }
1224
1.41M
    }
1225
4.81M
  }
1226
1227
803k
  return false;
1228
803k
}
1229
1230
static int reader(const struct reader_info *info, uint8_t *byte,
1231
      uint64_t address)
1232
3.66M
{
1233
3.66M
  if (address - info->offset >= info->size)
1234
    // out of buffer range
1235
3.67k
    return -1;
1236
1237
3.66M
  *byte = info->code[address - info->offset];
1238
1239
3.66M
  return 0;
1240
3.66M
}
1241
1242
// copy x86 detail information from internal structure to public structure
1243
static void update_pub_insn(cs_insn *pub, InternalInstruction *inter)
1244
803k
{
1245
803k
  if (inter->vectorExtensionType != 0) {
1246
69.7k
    memcpy(pub->detail->x86.opcode, inter->vectorExtensionPrefix,
1247
69.7k
           sizeof(pub->detail->x86.opcode));
1248
733k
  } else {
1249
733k
    if (inter->twoByteEscape) {
1250
41.6k
      if (inter->threeByteEscape) {
1251
0
        pub->detail->x86.opcode[0] =
1252
0
          inter->twoByteEscape;
1253
0
        pub->detail->x86.opcode[1] =
1254
0
          inter->threeByteEscape;
1255
0
        pub->detail->x86.opcode[2] = inter->opcode;
1256
41.6k
      } else {
1257
41.6k
        pub->detail->x86.opcode[0] =
1258
41.6k
          inter->twoByteEscape;
1259
41.6k
        pub->detail->x86.opcode[1] = inter->opcode;
1260
41.6k
      }
1261
691k
    } else {
1262
691k
      pub->detail->x86.opcode[0] = inter->opcode;
1263
691k
    }
1264
733k
  }
1265
1266
803k
  pub->detail->x86.rex = inter->rexPrefix;
1267
1268
803k
  pub->detail->x86.addr_size = inter->addressSize;
1269
1270
803k
  pub->detail->x86.modrm = inter->orgModRM;
1271
803k
  pub->detail->x86.encoding.modrm_offset = inter->modRMOffset;
1272
1273
803k
  pub->detail->x86.sib = inter->sib;
1274
803k
  pub->detail->x86.sib_index = x86_map_sib_index(inter->sibIndex);
1275
803k
  pub->detail->x86.sib_scale = inter->sibScale;
1276
803k
  pub->detail->x86.sib_base = x86_map_sib_base(inter->sibBase);
1277
1278
803k
  pub->detail->x86.disp = inter->displacement;
1279
803k
  if (inter->consumedDisplacement) {
1280
112k
    pub->detail->x86.encoding.disp_offset =
1281
112k
      inter->displacementOffset;
1282
112k
    pub->detail->x86.encoding.disp_size = inter->displacementSize;
1283
112k
  }
1284
1285
803k
  pub->detail->x86.encoding.imm_offset = inter->immediateOffset;
1286
803k
  if (pub->detail->x86.encoding.imm_size == 0 &&
1287
803k
      inter->immediateOffset != 0)
1288
214k
    pub->detail->x86.encoding.imm_size = inter->immediateSize;
1289
803k
}
1290
1291
void X86_init(MCRegisterInfo *MRI)
1292
9.73k
{
1293
  // InitMCRegisterInfo(), X86GenRegisterInfo.inc
1294
  // RI->InitMCRegisterInfo(X86RegDesc, 277,
1295
  //                        RA, PC,
1296
  //                        X86MCRegisterClasses, 86,
1297
  //                        X86RegUnitRoots, 162, X86RegDiffLists, X86LaneMaskLists, X86RegStrings,
1298
  //                        X86RegClassStrings,
1299
  //                        X86SubRegIdxLists, 9,
1300
  //                        X86SubRegIdxRanges, X86RegEncodingTable);
1301
  /*
1302
     InitMCRegisterInfo(X86RegDesc, 234,
1303
     RA, PC,
1304
     X86MCRegisterClasses, 79,
1305
     X86RegUnitRoots, 119, X86RegDiffLists, X86RegStrings,
1306
     X86SubRegIdxLists, 7,
1307
     X86SubRegIdxRanges, X86RegEncodingTable);
1308
  */
1309
1310
9.73k
  MCRegisterInfo_InitMCRegisterInfo(MRI, X86RegDesc, 277, 0, 0,
1311
9.73k
            X86MCRegisterClasses, 86, 0, 0,
1312
9.73k
            X86RegDiffLists, 0, X86SubRegIdxLists,
1313
9.73k
            9, 0);
1314
9.73k
}
1315
1316
// Public interface for the disassembler
1317
bool X86_getInstruction(csh ud, const uint8_t *code, size_t code_len,
1318
      MCInst *instr, uint16_t *size, uint64_t address,
1319
      void *_info)
1320
808k
{
1321
808k
  cs_struct *handle = (cs_struct *)(uintptr_t)ud;
1322
808k
  InternalInstruction insn = { 0 };
1323
808k
  struct reader_info info;
1324
808k
  int ret;
1325
808k
  bool result;
1326
1327
808k
  info.code = code;
1328
808k
  info.size = code_len;
1329
808k
  info.offset = address;
1330
1331
808k
  if (instr->flat_insn->detail) {
1332
    // instr->flat_insn->detail initialization: 3 alternatives
1333
1334
    // 1. The whole structure, this is how it's done in other arch disassemblers
1335
    // Probably overkill since cs_detail is huge because of the 36 operands of ARM
1336
1337
    //memset(instr->flat_insn->detail, 0, sizeof(cs_detail));
1338
1339
    // 2. Only the part relevant to x86
1340
808k
    memset(instr->flat_insn->detail, 0,
1341
808k
           offsetof(cs_detail, x86) + sizeof(cs_x86));
1342
1343
    // 3. The relevant part except for x86.operands
1344
    // sizeof(cs_x86) is 0x1c0, sizeof(x86.operands) is 0x180
1345
    // marginally faster, should be okay since x86.op_count is set to 0
1346
1347
    //memset(instr->flat_insn->detail, 0, offsetof(cs_detail, x86)+offsetof(cs_x86, operands));
1348
808k
  }
1349
1350
808k
  if (handle->mode & CS_MODE_16)
1351
267k
    ret = decodeInstruction(&insn, reader, &info, address,
1352
267k
          MODE_16BIT);
1353
540k
  else if (handle->mode & CS_MODE_32)
1354
267k
    ret = decodeInstruction(&insn, reader, &info, address,
1355
267k
          MODE_32BIT);
1356
273k
  else
1357
273k
    ret = decodeInstruction(&insn, reader, &info, address,
1358
273k
          MODE_64BIT);
1359
1360
808k
  if (ret) {
1361
    // *size = (uint16_t)(insn.readerCursor - address);
1362
5.39k
    return false;
1363
803k
  } else {
1364
803k
    *size = (uint16_t)insn.length;
1365
1366
803k
    result = (!translateInstruction(instr, &insn)) ? true : false;
1367
803k
    if (result) {
1368
803k
      unsigned Flags = X86_IP_NO_PREFIX;
1369
803k
      instr->imm_size = insn.immSize;
1370
1371
      // copy all prefixes
1372
803k
      instr->x86_prefix[0] = insn.prefix0;
1373
803k
      instr->x86_prefix[1] = insn.prefix1;
1374
803k
      instr->x86_prefix[2] = insn.prefix2;
1375
803k
      instr->x86_prefix[3] = insn.prefix3;
1376
803k
      instr->xAcquireRelease = insn.xAcquireRelease;
1377
1378
803k
      if (handle->detail_opt) {
1379
803k
        update_pub_insn(instr->flat_insn, &insn);
1380
803k
      }
1381
1382
803k
      if (insn.hasAdSize)
1383
6.02k
        Flags |= X86_IP_HAS_AD_SIZE;
1384
1385
803k
      if (!insn.mandatoryPrefix) {
1386
789k
        if (insn.hasOpSize)
1387
14.0k
          Flags |= X86_IP_HAS_OP_SIZE;
1388
1389
789k
        if (insn.repeatPrefix == 0xf2)
1390
21.0k
          Flags |= X86_IP_HAS_REPEAT_NE;
1391
768k
        else if (insn.repeatPrefix == 0xf3 &&
1392
           // It should not be 'pause' f3 90
1393
21.2k
           insn.opcode != 0x90)
1394
21.0k
          Flags |= X86_IP_HAS_REPEAT;
1395
789k
        if (insn.hasLockPrefix)
1396
29.9k
          Flags |= X86_IP_HAS_LOCK;
1397
789k
      }
1398
1399
803k
      instr->flags = Flags;
1400
803k
    }
1401
1402
803k
    return result;
1403
803k
  }
1404
808k
}
1405
1406
#endif