Coverage Report

Created: 2026-10-17 07:17

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/capstonenext/arch/X86/X86IntelInstPrinter.c
Line
Count
Source
1
//===-- X86IntelInstPrinter.cpp - Intel assembly instruction printing -----===//
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 includes code for rendering MCInst instances as Intel-style
11
// assembly.
12
//
13
//===----------------------------------------------------------------------===//
14
15
/* Capstone Disassembly Engine */
16
/* By Nguyen Anh Quynh <aquynh@gmail.com>, 2013-2019 */
17
18
#ifdef CAPSTONE_HAS_X86
19
20
#ifdef _MSC_VER
21
// disable MSVC's warning on strncpy()
22
#pragma warning(disable : 4996)
23
// disable MSVC's warning on strncpy()
24
#pragma warning(disable : 28719)
25
#endif
26
27
#if !defined(CAPSTONE_HAS_OSXKERNEL)
28
#include <ctype.h>
29
#endif
30
#include <capstone/platform.h>
31
32
#if defined(CAPSTONE_HAS_OSXKERNEL)
33
#include <Availability.h>
34
#include <libkern/libkern.h>
35
#else
36
#include <stdio.h>
37
#include <stdlib.h>
38
#endif
39
#include <string.h>
40
41
#include "../../utils.h"
42
#include "../../MCInst.h"
43
#include "../../SStream.h"
44
#include "../../MCRegisterInfo.h"
45
46
#include "X86InstPrinter.h"
47
#include "X86Mapping.h"
48
#include "X86InstPrinterCommon.h"
49
50
#define GET_INSTRINFO_ENUM
51
#ifdef CAPSTONE_X86_REDUCE
52
#include "X86GenInstrInfo_reduce.inc"
53
#else
54
#include "X86GenInstrInfo.inc"
55
#endif
56
57
#define GET_REGINFO_ENUM
58
#include "X86GenRegisterInfo.inc"
59
60
#include "X86BaseInfo.h"
61
62
static void printMemReference(MCInst *MI, unsigned Op, SStream *O);
63
static void printOperand(MCInst *MI, unsigned OpNo, SStream *O);
64
65
static void set_mem_access(MCInst *MI, bool status)
66
145k
{
67
145k
  if (MI->csh->detail_opt != CS_OPT_ON)
68
0
    return;
69
70
145k
  MI->csh->doing_mem = status;
71
145k
  if (!status)
72
    // done, create the next operand slot
73
72.8k
    MI->flat_insn->detail->x86.op_count++;
74
145k
}
75
76
static void printopaquemem(MCInst *MI, unsigned OpNo, SStream *O)
77
13.6k
{
78
  // FIXME: do this with autogen
79
  // printf(">>> ID = %u\n", MI->flat_insn->id);
80
13.6k
  switch (MI->flat_insn->id) {
81
3.92k
  default:
82
3.92k
    SStream_concat0(O, "ptr ");
83
3.92k
    break;
84
1.56k
  case X86_INS_SGDT:
85
2.74k
  case X86_INS_SIDT:
86
3.89k
  case X86_INS_LGDT:
87
6.03k
  case X86_INS_LIDT:
88
6.50k
  case X86_INS_FXRSTOR:
89
7.01k
  case X86_INS_FXSAVE:
90
8.58k
  case X86_INS_LJMP:
91
9.73k
  case X86_INS_LCALL:
92
    // do not print "ptr"
93
9.73k
    break;
94
13.6k
  }
95
96
13.6k
  switch (x86_get_bit_mode(MI->csh->mode)) {
97
4.21k
  case CS_MODE_16:
98
4.21k
    switch (MI->flat_insn->id) {
99
1.48k
    default:
100
1.48k
      MI->x86opsize = 2;
101
1.48k
      break;
102
489
    case X86_INS_LJMP:
103
929
    case X86_INS_LCALL:
104
929
      MI->x86opsize = 4;
105
929
      break;
106
469
    case X86_INS_SGDT:
107
784
    case X86_INS_SIDT:
108
1.24k
    case X86_INS_LGDT:
109
1.79k
    case X86_INS_LIDT:
110
1.79k
      MI->x86opsize = 6;
111
1.79k
      break;
112
4.21k
    }
113
4.21k
    break;
114
6.19k
  case CS_MODE_32:
115
6.19k
    switch (MI->flat_insn->id) {
116
2.05k
    default:
117
2.05k
      MI->x86opsize = 4;
118
2.05k
      break;
119
397
    case X86_INS_LJMP:
120
1.07k
    case X86_INS_JMP:
121
1.46k
    case X86_INS_LCALL:
122
2.02k
    case X86_INS_SGDT:
123
2.55k
    case X86_INS_SIDT:
124
2.95k
    case X86_INS_LGDT:
125
4.13k
    case X86_INS_LIDT:
126
4.13k
      MI->x86opsize = 6;
127
4.13k
      break;
128
6.19k
    }
129
6.19k
    break;
130
6.19k
  case CS_MODE_64:
131
3.25k
    switch (MI->flat_insn->id) {
132
684
    default:
133
684
      MI->x86opsize = 8;
134
684
      break;
135
681
    case X86_INS_LJMP:
136
1.00k
    case X86_INS_LCALL:
137
1.55k
    case X86_INS_SGDT:
138
1.88k
    case X86_INS_SIDT:
139
2.17k
    case X86_INS_LGDT:
140
2.57k
    case X86_INS_LIDT:
141
2.57k
      MI->x86opsize = 10;
142
2.57k
      break;
143
3.25k
    }
144
3.25k
    break;
145
3.25k
  default: // never reach
146
0
    break;
147
13.6k
  }
148
149
13.6k
  printMemReference(MI, OpNo, O);
150
13.6k
}
151
152
static void printi8mem(MCInst *MI, unsigned OpNo, SStream *O)
153
92.8k
{
154
92.8k
  SStream_concat0(O, "byte ptr ");
155
92.8k
  MI->x86opsize = 1;
156
92.8k
  printMemReference(MI, OpNo, O);
157
92.8k
}
158
159
static void printi16mem(MCInst *MI, unsigned OpNo, SStream *O)
160
26.1k
{
161
26.1k
  MI->x86opsize = 2;
162
26.1k
  SStream_concat0(O, "word ptr ");
163
26.1k
  printMemReference(MI, OpNo, O);
164
26.1k
}
165
166
static void printi32mem(MCInst *MI, unsigned OpNo, SStream *O)
167
62.7k
{
168
62.7k
  MI->x86opsize = 4;
169
62.7k
  SStream_concat0(O, "dword ptr ");
170
62.7k
  printMemReference(MI, OpNo, O);
171
62.7k
}
172
173
static void printi64mem(MCInst *MI, unsigned OpNo, SStream *O)
174
24.4k
{
175
24.4k
  SStream_concat0(O, "qword ptr ");
176
24.4k
  MI->x86opsize = 8;
177
24.4k
  printMemReference(MI, OpNo, O);
178
24.4k
}
179
180
static void printi128mem(MCInst *MI, unsigned OpNo, SStream *O)
181
7.94k
{
182
7.94k
  SStream_concat0(O, "xmmword ptr ");
183
7.94k
  MI->x86opsize = 16;
184
7.94k
  printMemReference(MI, OpNo, O);
185
7.94k
}
186
187
static void printi512mem(MCInst *MI, unsigned OpNo, SStream *O)
188
4.51k
{
189
4.51k
  SStream_concat0(O, "zmmword ptr ");
190
4.51k
  MI->x86opsize = 64;
191
4.51k
  printMemReference(MI, OpNo, O);
192
4.51k
}
193
194
#ifndef CAPSTONE_X86_REDUCE
195
static void printi256mem(MCInst *MI, unsigned OpNo, SStream *O)
196
4.72k
{
197
4.72k
  SStream_concat0(O, "ymmword ptr ");
198
4.72k
  MI->x86opsize = 32;
199
4.72k
  printMemReference(MI, OpNo, O);
200
4.72k
}
201
202
static void printf32mem(MCInst *MI, unsigned OpNo, SStream *O)
203
8.93k
{
204
8.93k
  switch (MCInst_getOpcode(MI)) {
205
7.30k
  default:
206
7.30k
    SStream_concat0(O, "dword ptr ");
207
7.30k
    MI->x86opsize = 4;
208
7.30k
    break;
209
805
  case X86_FSTENVm:
210
1.63k
  case X86_FLDENVm:
211
    // TODO: fix this in tablegen instead
212
1.63k
    switch (x86_get_bit_mode(MI->csh->mode)) {
213
0
    default: // never reach
214
0
      break;
215
639
    case CS_MODE_16:
216
639
      MI->x86opsize = 14;
217
639
      break;
218
473
    case CS_MODE_32:
219
991
    case CS_MODE_64:
220
991
      MI->x86opsize = 28;
221
991
      break;
222
1.63k
    }
223
1.63k
    break;
224
8.93k
  }
225
226
8.93k
  printMemReference(MI, OpNo, O);
227
8.93k
}
228
229
static void printf64mem(MCInst *MI, unsigned OpNo, SStream *O)
230
4.74k
{
231
  // TODO: fix COMISD in Tablegen instead (#1456)
232
4.74k
  if (MI->op1_size == 16) {
233
    // printf("printf64mem id = %u\n", MCInst_getOpcode(MI));
234
1.93k
    switch (MCInst_getOpcode(MI)) {
235
1.93k
    default:
236
1.93k
      SStream_concat0(O, "qword ptr ");
237
1.93k
      MI->x86opsize = 8;
238
1.93k
      break;
239
0
    case X86_MOVPQI2QImr:
240
0
      SStream_concat0(O, "xmmword ptr ");
241
0
      MI->x86opsize = 16;
242
0
      break;
243
1.93k
    }
244
2.80k
  } else {
245
2.80k
    SStream_concat0(O, "qword ptr ");
246
2.80k
    MI->x86opsize = 8;
247
2.80k
  }
248
249
4.74k
  printMemReference(MI, OpNo, O);
250
4.74k
}
251
252
static void printf80mem(MCInst *MI, unsigned OpNo, SStream *O)
253
884
{
254
884
  switch (MCInst_getOpcode(MI)) {
255
429
  default:
256
429
    SStream_concat0(O, "xword ptr ");
257
429
    break;
258
125
  case X86_FBLDm:
259
455
  case X86_FBSTPm:
260
455
    break;
261
884
  }
262
263
884
  MI->x86opsize = 10;
264
884
  printMemReference(MI, OpNo, O);
265
884
}
266
267
static void printf128mem(MCInst *MI, unsigned OpNo, SStream *O)
268
5.62k
{
269
5.62k
  SStream_concat0(O, "xmmword ptr ");
270
5.62k
  MI->x86opsize = 16;
271
5.62k
  printMemReference(MI, OpNo, O);
272
5.62k
}
273
274
static void printf256mem(MCInst *MI, unsigned OpNo, SStream *O)
275
5.12k
{
276
5.12k
  SStream_concat0(O, "ymmword ptr ");
277
5.12k
  MI->x86opsize = 32;
278
5.12k
  printMemReference(MI, OpNo, O);
279
5.12k
}
280
281
static void printf512mem(MCInst *MI, unsigned OpNo, SStream *O)
282
3.27k
{
283
3.27k
  SStream_concat0(O, "zmmword ptr ");
284
3.27k
  MI->x86opsize = 64;
285
3.27k
  printMemReference(MI, OpNo, O);
286
3.27k
}
287
#endif
288
289
static const char *getRegisterName(unsigned RegNo);
290
static void printRegName(SStream *OS, unsigned RegNo)
291
948k
{
292
948k
  SStream_concat0(OS, getRegisterName(RegNo));
293
948k
}
294
295
// for MASM syntax, 0x123 = 123h, 0xA123 = 0A123h
296
// this function tell us if we need to have prefix 0 in front of a number
297
static bool need_zero_prefix(uint64_t imm)
298
0
{
299
  // find the first hex letter representing imm
300
0
  while (imm >= 0x10)
301
0
    imm >>= 4;
302
303
0
  if (imm < 0xa)
304
0
    return false;
305
0
  else // this need 0 prefix
306
0
    return true;
307
0
}
308
309
static void printImm(MCInst *MI, SStream *O, int64_t imm, bool positive)
310
255k
{
311
255k
  if (positive) {
312
    // always print this number in positive form
313
221k
    if (MI->csh->syntax == CS_OPT_SYNTAX_MASM) {
314
0
      if (imm < 0) {
315
0
        if (MI->op1_size) {
316
0
          switch (MI->op1_size) {
317
0
          default:
318
0
            break;
319
0
          case 1:
320
0
            imm &= 0xff;
321
0
            break;
322
0
          case 2:
323
0
            imm &= 0xffff;
324
0
            break;
325
0
          case 4:
326
0
            imm &= 0xffffffff;
327
0
            break;
328
0
          }
329
0
        }
330
331
0
        if (imm == 0x8000000000000000LL) // imm == -imm
332
0
          SStream_concat0(O, "8000000000000000h");
333
0
        else if (need_zero_prefix(imm))
334
0
          SStream_concat(O, "0%" PRIx64 "h", imm);
335
0
        else
336
0
          SStream_concat(O, "%" PRIx64 "h", imm);
337
0
      } else {
338
0
        if (imm > HEX_THRESHOLD) {
339
0
          if (need_zero_prefix(imm))
340
0
            SStream_concat(O,
341
0
                     "0%" PRIx64 "h",
342
0
                     imm);
343
0
          else
344
0
            SStream_concat(
345
0
              O, "%" PRIx64 "h", imm);
346
0
        } else
347
0
          SStream_concat(O, "%" PRIu64, imm);
348
0
      }
349
221k
    } else { // Intel syntax
350
221k
      if (imm < 0) {
351
3.39k
        if (MI->op1_size) {
352
932
          switch (MI->op1_size) {
353
932
          default:
354
932
            break;
355
932
          case 1:
356
0
            imm &= 0xff;
357
0
            break;
358
0
          case 2:
359
0
            imm &= 0xffff;
360
0
            break;
361
0
          case 4:
362
0
            imm &= 0xffffffff;
363
0
            break;
364
932
          }
365
932
        }
366
367
3.39k
        SStream_concat(O, "0x%" PRIx64, imm);
368
217k
      } else {
369
217k
        if (imm > HEX_THRESHOLD)
370
201k
          SStream_concat(O, "0x%" PRIx64, imm);
371
15.6k
        else
372
15.6k
          SStream_concat(O, "%" PRIu64, imm);
373
217k
      }
374
221k
    }
375
221k
  } else {
376
34.3k
    if (MI->csh->syntax == CS_OPT_SYNTAX_MASM) {
377
0
      if (imm < 0) {
378
0
        if (imm == 0x8000000000000000LL) // imm == -imm
379
0
          SStream_concat0(O, "8000000000000000h");
380
0
        else if (imm < -HEX_THRESHOLD) {
381
0
          if (need_zero_prefix(imm))
382
0
            SStream_concat(O,
383
0
                     "-0%" PRIx64 "h",
384
0
                     -imm);
385
0
          else
386
0
            SStream_concat(O,
387
0
                     "-%" PRIx64 "h",
388
0
                     -imm);
389
0
        } else
390
0
          SStream_concat(O, "-%" PRIu64, -imm);
391
0
      } else {
392
0
        if (imm > HEX_THRESHOLD) {
393
0
          if (need_zero_prefix(imm))
394
0
            SStream_concat(O,
395
0
                     "0%" PRIx64 "h",
396
0
                     imm);
397
0
          else
398
0
            SStream_concat(
399
0
              O, "%" PRIx64 "h", imm);
400
0
        } else
401
0
          SStream_concat(O, "%" PRIu64, imm);
402
0
      }
403
34.3k
    } else { // Intel syntax
404
34.3k
      if (imm < 0) {
405
4.81k
        if (imm == 0x8000000000000000LL) // imm == -imm
406
0
          SStream_concat0(O,
407
0
              "0x8000000000000000");
408
4.81k
        else if (imm < -HEX_THRESHOLD)
409
4.12k
          SStream_concat(O, "-0x%" PRIx64, -imm);
410
689
        else
411
689
          SStream_concat(O, "-%" PRIu64, -imm);
412
413
29.5k
      } else {
414
29.5k
        if (imm > HEX_THRESHOLD)
415
24.3k
          SStream_concat(O, "0x%" PRIx64, imm);
416
5.22k
        else
417
5.22k
          SStream_concat(O, "%" PRIu64, imm);
418
29.5k
      }
419
34.3k
    }
420
34.3k
  }
421
255k
}
422
423
// local printOperand, without updating public operands
424
static void _printOperand(MCInst *MI, unsigned OpNo, SStream *O)
425
331k
{
426
331k
  MCOperand *Op = MCInst_getOperand(MI, OpNo);
427
331k
  if (MCOperand_isReg(Op)) {
428
331k
    printRegName(O, MCOperand_getReg(Op));
429
331k
  } else if (MCOperand_isImm(Op)) {
430
0
    int64_t imm = MCOperand_getImm(Op);
431
0
    printImm(MI, O, imm, MI->csh->imm_unsigned);
432
0
  }
433
331k
}
434
435
#ifndef CAPSTONE_DIET
436
// copy & normalize access info
437
static void get_op_access(cs_struct *h, unsigned int id, uint8_t *access,
438
        uint64_t *eflags)
439
1.70M
{
440
1.70M
#ifndef CAPSTONE_DIET
441
1.70M
  uint8_t i;
442
1.70M
  const uint8_t *arr = X86_get_op_access(h, id, eflags);
443
444
  // initialize access
445
1.70M
  memset(access, 0, CS_X86_MAXIMUM_OPERAND_SIZE * sizeof(access[0]));
446
447
1.70M
  if (!arr) {
448
0
    access[0] = 0;
449
0
    return;
450
0
  }
451
452
  // copy to access but zero out CS_AC_IGNORE
453
4.97M
  for (i = 0; arr[i]; i++) {
454
3.27M
    if (arr[i] != CS_AC_IGNORE)
455
2.76M
      access[i] = arr[i];
456
507k
    else
457
507k
      access[i] = 0;
458
3.27M
  }
459
460
  // mark the end of array
461
1.70M
  access[i] = 0;
462
1.70M
#endif
463
1.70M
}
464
#endif
465
466
static void printSrcIdx(MCInst *MI, unsigned Op, SStream *O)
467
33.7k
{
468
33.7k
  MCOperand *SegReg;
469
33.7k
  int reg;
470
471
33.7k
  if (MI->csh->detail_opt) {
472
33.7k
#ifndef CAPSTONE_DIET
473
33.7k
    uint8_t access[CS_X86_MAXIMUM_OPERAND_SIZE];
474
33.7k
#endif
475
476
33.7k
    MI->flat_insn->detail->x86
477
33.7k
      .operands[MI->flat_insn->detail->x86.op_count]
478
33.7k
      .type = X86_OP_MEM;
479
33.7k
    MI->flat_insn->detail->x86
480
33.7k
      .operands[MI->flat_insn->detail->x86.op_count]
481
33.7k
      .size = MI->x86opsize;
482
33.7k
    MI->flat_insn->detail->x86
483
33.7k
      .operands[MI->flat_insn->detail->x86.op_count]
484
33.7k
      .mem.segment = X86_REG_INVALID;
485
33.7k
    MI->flat_insn->detail->x86
486
33.7k
      .operands[MI->flat_insn->detail->x86.op_count]
487
33.7k
      .mem.base = X86_REG_INVALID;
488
33.7k
    MI->flat_insn->detail->x86
489
33.7k
      .operands[MI->flat_insn->detail->x86.op_count]
490
33.7k
      .mem.index = X86_REG_INVALID;
491
33.7k
    MI->flat_insn->detail->x86
492
33.7k
      .operands[MI->flat_insn->detail->x86.op_count]
493
33.7k
      .mem.scale = 1;
494
33.7k
    MI->flat_insn->detail->x86
495
33.7k
      .operands[MI->flat_insn->detail->x86.op_count]
496
33.7k
      .mem.disp = 0;
497
498
33.7k
#ifndef CAPSTONE_DIET
499
33.7k
    get_op_access(MI->csh, MCInst_getOpcode(MI), access,
500
33.7k
            &MI->flat_insn->detail->x86.eflags);
501
33.7k
    MI->flat_insn->detail->x86
502
33.7k
      .operands[MI->flat_insn->detail->x86.op_count]
503
33.7k
      .access = access[MI->flat_insn->detail->x86.op_count];
504
33.7k
#endif
505
33.7k
  }
506
507
33.7k
  SegReg = MCInst_getOperand(MI, Op + 1);
508
33.7k
  reg = MCOperand_getReg(SegReg);
509
510
  // If this has a segment register, print it.
511
33.7k
  if (reg) {
512
883
    _printOperand(MI, Op + 1, O);
513
883
    if (MI->csh->detail_opt) {
514
883
      MI->flat_insn->detail->x86
515
883
        .operands[MI->flat_insn->detail->x86.op_count]
516
883
        .mem.segment = X86_register_map(reg);
517
883
    }
518
883
    SStream_concat0(O, ":");
519
883
  }
520
521
33.7k
  SStream_concat0(O, "[");
522
33.7k
  set_mem_access(MI, true);
523
33.7k
  printOperand(MI, Op, O);
524
33.7k
  SStream_concat0(O, "]");
525
33.7k
  set_mem_access(MI, false);
526
33.7k
}
527
528
static void printDstIdx(MCInst *MI, unsigned Op, SStream *O)
529
21.8k
{
530
21.8k
  if (MI->csh->detail_opt) {
531
21.8k
#ifndef CAPSTONE_DIET
532
21.8k
    uint8_t access[CS_X86_MAXIMUM_OPERAND_SIZE];
533
21.8k
#endif
534
535
21.8k
    MI->flat_insn->detail->x86
536
21.8k
      .operands[MI->flat_insn->detail->x86.op_count]
537
21.8k
      .type = X86_OP_MEM;
538
21.8k
    MI->flat_insn->detail->x86
539
21.8k
      .operands[MI->flat_insn->detail->x86.op_count]
540
21.8k
      .size = MI->x86opsize;
541
21.8k
    MI->flat_insn->detail->x86
542
21.8k
      .operands[MI->flat_insn->detail->x86.op_count]
543
21.8k
      .mem.segment = X86_REG_INVALID;
544
21.8k
    MI->flat_insn->detail->x86
545
21.8k
      .operands[MI->flat_insn->detail->x86.op_count]
546
21.8k
      .mem.base = X86_REG_INVALID;
547
21.8k
    MI->flat_insn->detail->x86
548
21.8k
      .operands[MI->flat_insn->detail->x86.op_count]
549
21.8k
      .mem.index = X86_REG_INVALID;
550
21.8k
    MI->flat_insn->detail->x86
551
21.8k
      .operands[MI->flat_insn->detail->x86.op_count]
552
21.8k
      .mem.scale = 1;
553
21.8k
    MI->flat_insn->detail->x86
554
21.8k
      .operands[MI->flat_insn->detail->x86.op_count]
555
21.8k
      .mem.disp = 0;
556
557
21.8k
#ifndef CAPSTONE_DIET
558
21.8k
    get_op_access(MI->csh, MCInst_getOpcode(MI), access,
559
21.8k
            &MI->flat_insn->detail->x86.eflags);
560
21.8k
    MI->flat_insn->detail->x86
561
21.8k
      .operands[MI->flat_insn->detail->x86.op_count]
562
21.8k
      .access = access[MI->flat_insn->detail->x86.op_count];
563
21.8k
#endif
564
21.8k
  }
565
566
  // DI accesses are always ES-based on non-64bit mode
567
21.8k
  if (!(x86_has_feature(MI->csh->mode, CS_MODE_64))) {
568
12.5k
    SStream_concat0(O, "es:[");
569
12.5k
    if (MI->csh->detail_opt) {
570
12.5k
      MI->flat_insn->detail->x86
571
12.5k
        .operands[MI->flat_insn->detail->x86.op_count]
572
12.5k
        .mem.segment = X86_REG_ES;
573
12.5k
    }
574
12.5k
  } else
575
9.35k
    SStream_concat0(O, "[");
576
577
21.8k
  set_mem_access(MI, true);
578
21.8k
  printOperand(MI, Op, O);
579
21.8k
  SStream_concat0(O, "]");
580
21.8k
  set_mem_access(MI, false);
581
21.8k
}
582
583
static void printSrcIdx8(MCInst *MI, unsigned OpNo, SStream *O)
584
11.8k
{
585
11.8k
  SStream_concat0(O, "byte ptr ");
586
11.8k
  MI->x86opsize = 1;
587
11.8k
  printSrcIdx(MI, OpNo, O);
588
11.8k
}
589
590
static void printSrcIdx16(MCInst *MI, unsigned OpNo, SStream *O)
591
5.22k
{
592
5.22k
  SStream_concat0(O, "word ptr ");
593
5.22k
  MI->x86opsize = 2;
594
5.22k
  printSrcIdx(MI, OpNo, O);
595
5.22k
}
596
597
static void printSrcIdx32(MCInst *MI, unsigned OpNo, SStream *O)
598
13.4k
{
599
13.4k
  SStream_concat0(O, "dword ptr ");
600
13.4k
  MI->x86opsize = 4;
601
13.4k
  printSrcIdx(MI, OpNo, O);
602
13.4k
}
603
604
static void printSrcIdx64(MCInst *MI, unsigned OpNo, SStream *O)
605
3.13k
{
606
3.13k
  SStream_concat0(O, "qword ptr ");
607
3.13k
  MI->x86opsize = 8;
608
3.13k
  printSrcIdx(MI, OpNo, O);
609
3.13k
}
610
611
static void printDstIdx8(MCInst *MI, unsigned OpNo, SStream *O)
612
12.4k
{
613
12.4k
  SStream_concat0(O, "byte ptr ");
614
12.4k
  MI->x86opsize = 1;
615
12.4k
  printDstIdx(MI, OpNo, O);
616
12.4k
}
617
618
static void printDstIdx16(MCInst *MI, unsigned OpNo, SStream *O)
619
6.62k
{
620
6.62k
  SStream_concat0(O, "word ptr ");
621
6.62k
  MI->x86opsize = 2;
622
6.62k
  printDstIdx(MI, OpNo, O);
623
6.62k
}
624
625
static void printDstIdx32(MCInst *MI, unsigned OpNo, SStream *O)
626
15.9k
{
627
15.9k
  SStream_concat0(O, "dword ptr ");
628
15.9k
  MI->x86opsize = 4;
629
15.9k
  printDstIdx(MI, OpNo, O);
630
15.9k
}
631
632
static void printDstIdx64(MCInst *MI, unsigned OpNo, SStream *O)
633
4.00k
{
634
4.00k
  SStream_concat0(O, "qword ptr ");
635
4.00k
  MI->x86opsize = 8;
636
4.00k
  printDstIdx(MI, OpNo, O);
637
4.00k
}
638
639
static void printMemOffset(MCInst *MI, unsigned Op, SStream *O)
640
5.89k
{
641
5.89k
  MCOperand *DispSpec = MCInst_getOperand(MI, Op);
642
5.89k
  MCOperand *SegReg = MCInst_getOperand(MI, Op + 1);
643
5.89k
  int reg;
644
645
5.89k
  if (MI->csh->detail_opt) {
646
5.89k
#ifndef CAPSTONE_DIET
647
5.89k
    uint8_t access[CS_X86_MAXIMUM_OPERAND_SIZE];
648
5.89k
#endif
649
650
5.89k
    MI->flat_insn->detail->x86
651
5.89k
      .operands[MI->flat_insn->detail->x86.op_count]
652
5.89k
      .type = X86_OP_MEM;
653
5.89k
    MI->flat_insn->detail->x86
654
5.89k
      .operands[MI->flat_insn->detail->x86.op_count]
655
5.89k
      .size = MI->x86opsize;
656
5.89k
    MI->flat_insn->detail->x86
657
5.89k
      .operands[MI->flat_insn->detail->x86.op_count]
658
5.89k
      .mem.segment = X86_REG_INVALID;
659
5.89k
    MI->flat_insn->detail->x86
660
5.89k
      .operands[MI->flat_insn->detail->x86.op_count]
661
5.89k
      .mem.base = X86_REG_INVALID;
662
5.89k
    MI->flat_insn->detail->x86
663
5.89k
      .operands[MI->flat_insn->detail->x86.op_count]
664
5.89k
      .mem.index = X86_REG_INVALID;
665
5.89k
    MI->flat_insn->detail->x86
666
5.89k
      .operands[MI->flat_insn->detail->x86.op_count]
667
5.89k
      .mem.scale = 1;
668
5.89k
    MI->flat_insn->detail->x86
669
5.89k
      .operands[MI->flat_insn->detail->x86.op_count]
670
5.89k
      .mem.disp = 0;
671
672
5.89k
#ifndef CAPSTONE_DIET
673
5.89k
    get_op_access(MI->csh, MCInst_getOpcode(MI), access,
674
5.89k
            &MI->flat_insn->detail->x86.eflags);
675
5.89k
    MI->flat_insn->detail->x86
676
5.89k
      .operands[MI->flat_insn->detail->x86.op_count]
677
5.89k
      .access = access[MI->flat_insn->detail->x86.op_count];
678
5.89k
#endif
679
5.89k
  }
680
681
  // If this has a segment register, print it.
682
5.89k
  reg = MCOperand_getReg(SegReg);
683
5.89k
  if (reg) {
684
556
    _printOperand(MI, Op + 1, O);
685
556
    SStream_concat0(O, ":");
686
556
    if (MI->csh->detail_opt) {
687
556
      MI->flat_insn->detail->x86
688
556
        .operands[MI->flat_insn->detail->x86.op_count]
689
556
        .mem.segment = X86_register_map(reg);
690
556
    }
691
556
  }
692
693
5.89k
  SStream_concat0(O, "[");
694
695
5.89k
  if (MCOperand_isImm(DispSpec)) {
696
5.89k
    int64_t imm = MCOperand_getImm(DispSpec);
697
5.89k
    if (MI->csh->detail_opt)
698
5.89k
      MI->flat_insn->detail->x86
699
5.89k
        .operands[MI->flat_insn->detail->x86.op_count]
700
5.89k
        .mem.disp = imm;
701
702
5.89k
    if (imm < 0)
703
1.04k
      printImm(MI, O,
704
1.04k
         x86_get_address_mask(MI->csh->mode) & imm,
705
1.04k
         true);
706
4.85k
    else
707
4.85k
      printImm(MI, O, imm, true);
708
5.89k
  }
709
710
5.89k
  SStream_concat0(O, "]");
711
712
5.89k
  if (MI->csh->detail_opt)
713
5.89k
    MI->flat_insn->detail->x86.op_count++;
714
715
5.89k
  if (MI->op1_size == 0)
716
5.89k
    MI->op1_size = MI->x86opsize;
717
5.89k
}
718
719
static void printU8Imm(MCInst *MI, unsigned Op, SStream *O)
720
42.6k
{
721
42.6k
  uint8_t val = MCOperand_getImm(MCInst_getOperand(MI, Op)) & 0xff;
722
723
42.6k
  printImm(MI, O, val, true);
724
725
42.6k
  if (MI->csh->detail_opt) {
726
42.6k
#ifndef CAPSTONE_DIET
727
42.6k
    uint8_t access[CS_X86_MAXIMUM_OPERAND_SIZE];
728
42.6k
#endif
729
730
42.6k
    MI->flat_insn->detail->x86
731
42.6k
      .operands[MI->flat_insn->detail->x86.op_count]
732
42.6k
      .type = X86_OP_IMM;
733
42.6k
    MI->flat_insn->detail->x86
734
42.6k
      .operands[MI->flat_insn->detail->x86.op_count]
735
42.6k
      .imm = val;
736
42.6k
    MI->flat_insn->detail->x86
737
42.6k
      .operands[MI->flat_insn->detail->x86.op_count]
738
42.6k
      .size = 1;
739
740
42.6k
#ifndef CAPSTONE_DIET
741
42.6k
    get_op_access(MI->csh, MCInst_getOpcode(MI), access,
742
42.6k
            &MI->flat_insn->detail->x86.eflags);
743
42.6k
    MI->flat_insn->detail->x86
744
42.6k
      .operands[MI->flat_insn->detail->x86.op_count]
745
42.6k
      .access = access[MI->flat_insn->detail->x86.op_count];
746
42.6k
#endif
747
748
42.6k
    MI->flat_insn->detail->x86.op_count++;
749
42.6k
  }
750
42.6k
}
751
752
static void printMemOffs8(MCInst *MI, unsigned OpNo, SStream *O)
753
3.27k
{
754
3.27k
  SStream_concat0(O, "byte ptr ");
755
3.27k
  MI->x86opsize = 1;
756
3.27k
  printMemOffset(MI, OpNo, O);
757
3.27k
}
758
759
static void printMemOffs16(MCInst *MI, unsigned OpNo, SStream *O)
760
965
{
761
965
  SStream_concat0(O, "word ptr ");
762
965
  MI->x86opsize = 2;
763
965
  printMemOffset(MI, OpNo, O);
764
965
}
765
766
static void printMemOffs32(MCInst *MI, unsigned OpNo, SStream *O)
767
1.27k
{
768
1.27k
  SStream_concat0(O, "dword ptr ");
769
1.27k
  MI->x86opsize = 4;
770
1.27k
  printMemOffset(MI, OpNo, O);
771
1.27k
}
772
773
static void printMemOffs64(MCInst *MI, unsigned OpNo, SStream *O)
774
390
{
775
390
  SStream_concat0(O, "qword ptr ");
776
390
  MI->x86opsize = 8;
777
390
  printMemOffset(MI, OpNo, O);
778
390
}
779
780
static void printInstruction(MCInst *MI, SStream *O);
781
782
void X86_Intel_printInst(MCInst *MI, SStream *O, void *Info)
783
373k
{
784
373k
  x86_reg reg = X86_REG_INVALID, reg2;
785
373k
  enum cs_ac_type access1, access2;
786
787
  // printf("opcode = %u\n", MCInst_getOpcode(MI));
788
789
  // perhaps this instruction does not need printer
790
373k
  if (MI->assembly[0]) {
791
0
    strncpy(O->buffer, MI->assembly, sizeof(O->buffer));
792
0
    return;
793
0
  }
794
795
373k
  X86_lockrep(MI, O);
796
373k
  printInstruction(MI, O);
797
798
373k
  if (MI->csh->detail_opt) {
799
373k
#ifndef CAPSTONE_DIET
800
373k
    uint8_t access[CS_X86_MAXIMUM_OPERAND_SIZE] = { 0 };
801
373k
#endif
802
803
    // first op can be embedded in the asm by llvm.
804
    // so we have to add the missing register as the first operand
805
373k
    reg = X86_insn_reg_intel_h(MI->csh, MCInst_getOpcode(MI),
806
373k
             &access1);
807
373k
    if (reg) {
808
      // shift all the ops right to leave 1st slot for this new register op
809
37.5k
      memmove(&(MI->flat_insn->detail->x86.operands[1]),
810
37.5k
        &(MI->flat_insn->detail->x86.operands[0]),
811
37.5k
        sizeof(MI->flat_insn->detail->x86.operands[0]) *
812
37.5k
          (ARR_SIZE(MI->flat_insn->detail->x86
813
37.5k
                .operands) -
814
37.5k
           1));
815
37.5k
      memset(&(MI->flat_insn->detail->x86.operands[0]), 0,
816
37.5k
             sizeof(MI->flat_insn->detail->x86.operands[0]));
817
37.5k
      MI->flat_insn->detail->x86.operands[0].type =
818
37.5k
        X86_OP_REG;
819
37.5k
      MI->flat_insn->detail->x86.operands[0].reg = reg;
820
37.5k
      MI->flat_insn->detail->x86.operands[0].size =
821
37.5k
        MI->csh->regsize_map[reg];
822
37.5k
      MI->flat_insn->detail->x86.operands[0].access = access1;
823
37.5k
      MI->flat_insn->detail->x86.op_count++;
824
336k
    } else {
825
336k
      if (X86_insn_reg_intel2(MCInst_getOpcode(MI), &reg,
826
336k
            &access1, &reg2, &access2)) {
827
6.28k
        MI->flat_insn->detail->x86.operands[0].type =
828
6.28k
          X86_OP_REG;
829
6.28k
        MI->flat_insn->detail->x86.operands[0].reg =
830
6.28k
          reg;
831
6.28k
        MI->flat_insn->detail->x86.operands[0].size =
832
6.28k
          MI->csh->regsize_map[reg];
833
6.28k
        MI->flat_insn->detail->x86.operands[0].access =
834
6.28k
          access1;
835
6.28k
        MI->flat_insn->detail->x86.operands[1].type =
836
6.28k
          X86_OP_REG;
837
6.28k
        MI->flat_insn->detail->x86.operands[1].reg =
838
6.28k
          reg2;
839
6.28k
        MI->flat_insn->detail->x86.operands[1].size =
840
6.28k
          MI->csh->regsize_map[reg2];
841
6.28k
        MI->flat_insn->detail->x86.operands[1].access =
842
6.28k
          access2;
843
6.28k
        MI->flat_insn->detail->x86.op_count = 2;
844
6.28k
      }
845
336k
    }
846
847
373k
#ifndef CAPSTONE_DIET
848
373k
    get_op_access(MI->csh, MCInst_getOpcode(MI), access,
849
373k
            &MI->flat_insn->detail->x86.eflags);
850
1.04M
    for (uint8_t i = 0; i < MI->flat_insn->detail->x86.op_count &&
851
673k
            i < ARR_SIZE(access);
852
673k
         i++) {
853
673k
      MI->flat_insn->detail->x86.operands[i].access =
854
673k
        access[i];
855
673k
    }
856
373k
#endif
857
373k
  }
858
859
373k
  if (MI->op1_size == 0 && reg)
860
24.7k
    MI->op1_size = MI->csh->regsize_map[reg];
861
373k
}
862
863
/// printPCRelImm - This is used to print an immediate value that ends up
864
/// being encoded as a pc-relative value.
865
static void printPCRelImm(MCInst *MI, unsigned OpNo, SStream *O)
866
26.4k
{
867
26.4k
  MCOperand *Op = MCInst_getOperand(MI, OpNo);
868
26.4k
  if (MCOperand_isImm(Op)) {
869
26.4k
    int64_t imm = MCOperand_getImm(Op) + MI->flat_insn->size +
870
26.4k
            MI->address;
871
26.4k
    uint8_t opsize = X86_immediate_size(MI->Opcode, NULL);
872
873
    // truncate imm for non-64bit
874
26.4k
    if (!(x86_has_feature(MI->csh->mode, CS_MODE_64))) {
875
17.6k
      imm = imm & 0xffffffff;
876
17.6k
    } else if ((x86_has_feature(MI->csh->mode, CS_MODE_X86_AMD)) &&
877
0
         MI->imm_size == 2) {
878
0
      imm &= 0xffff;
879
0
    }
880
881
26.4k
    printImm(MI, O, imm, true);
882
883
26.4k
    if (MI->csh->detail_opt) {
884
26.4k
#ifndef CAPSTONE_DIET
885
26.4k
      uint8_t access[CS_X86_MAXIMUM_OPERAND_SIZE];
886
26.4k
#endif
887
888
26.4k
      MI->flat_insn->detail->x86
889
26.4k
        .operands[MI->flat_insn->detail->x86.op_count]
890
26.4k
        .type = X86_OP_IMM;
891
      // if op_count > 0, then this operand's size is taken from the destination op
892
26.4k
      if (MI->flat_insn->detail->x86.op_count > 0)
893
0
        MI->flat_insn->detail->x86
894
0
          .operands[MI->flat_insn->detail->x86
895
0
                .op_count]
896
0
          .size =
897
0
          MI->flat_insn->detail->x86.operands[0]
898
0
            .size;
899
26.4k
      else if (opsize > 0)
900
1.00k
        MI->flat_insn->detail->x86
901
1.00k
          .operands[MI->flat_insn->detail->x86
902
1.00k
                .op_count]
903
1.00k
          .size = opsize;
904
25.4k
      else
905
25.4k
        MI->flat_insn->detail->x86
906
25.4k
          .operands[MI->flat_insn->detail->x86
907
25.4k
                .op_count]
908
25.4k
          .size = MI->imm_size;
909
26.4k
      MI->flat_insn->detail->x86
910
26.4k
        .operands[MI->flat_insn->detail->x86.op_count]
911
26.4k
        .imm = imm;
912
913
26.4k
#ifndef CAPSTONE_DIET
914
26.4k
      get_op_access(MI->csh, MCInst_getOpcode(MI), access,
915
26.4k
              &MI->flat_insn->detail->x86.eflags);
916
26.4k
      MI->flat_insn->detail->x86
917
26.4k
        .operands[MI->flat_insn->detail->x86.op_count]
918
26.4k
        .access =
919
26.4k
        access[MI->flat_insn->detail->x86.op_count];
920
26.4k
#endif
921
922
26.4k
      MI->flat_insn->detail->x86.op_count++;
923
26.4k
    }
924
925
26.4k
    if (MI->op1_size == 0)
926
26.4k
      MI->op1_size = MI->imm_size;
927
26.4k
  }
928
26.4k
}
929
930
static void printOperand(MCInst *MI, unsigned OpNo, SStream *O)
931
695k
{
932
695k
  MCOperand *Op = MCInst_getOperand(MI, OpNo);
933
934
695k
  if (MCOperand_isReg(Op)) {
935
616k
    unsigned int reg = MCOperand_getReg(Op);
936
937
616k
    printRegName(O, reg);
938
616k
    if (MI->csh->detail_opt) {
939
616k
      if (MI->csh->doing_mem) {
940
72.8k
        MI->flat_insn->detail->x86
941
72.8k
          .operands[MI->flat_insn->detail->x86
942
72.8k
                .op_count]
943
72.8k
          .mem.base = X86_register_map(reg);
944
544k
      } else {
945
544k
#ifndef CAPSTONE_DIET
946
544k
        uint8_t access[CS_X86_MAXIMUM_OPERAND_SIZE];
947
544k
#endif
948
949
544k
        MI->flat_insn->detail->x86
950
544k
          .operands[MI->flat_insn->detail->x86
951
544k
                .op_count]
952
544k
          .type = X86_OP_REG;
953
544k
        MI->flat_insn->detail->x86
954
544k
          .operands[MI->flat_insn->detail->x86
955
544k
                .op_count]
956
544k
          .reg = X86_register_map(reg);
957
544k
        MI->flat_insn->detail->x86
958
544k
          .operands[MI->flat_insn->detail->x86
959
544k
                .op_count]
960
544k
          .size =
961
544k
          MI->csh->regsize_map[X86_register_map(
962
544k
            reg)];
963
964
544k
#ifndef CAPSTONE_DIET
965
544k
        get_op_access(
966
544k
          MI->csh, MCInst_getOpcode(MI), access,
967
544k
          &MI->flat_insn->detail->x86.eflags);
968
544k
        MI->flat_insn->detail->x86
969
544k
          .operands[MI->flat_insn->detail->x86
970
544k
                .op_count]
971
544k
          .access =
972
544k
          access[MI->flat_insn->detail->x86
973
544k
                   .op_count];
974
544k
#endif
975
976
544k
        MI->flat_insn->detail->x86.op_count++;
977
544k
      }
978
616k
    }
979
980
616k
    if (MI->op1_size == 0)
981
314k
      MI->op1_size =
982
314k
        MI->csh->regsize_map[X86_register_map(reg)];
983
616k
  } else if (MCOperand_isImm(Op)) {
984
78.1k
    uint8_t encsize;
985
78.1k
    int64_t imm = MCOperand_getImm(Op);
986
78.1k
    uint8_t opsize =
987
78.1k
      X86_immediate_size(MCInst_getOpcode(MI), &encsize);
988
989
78.1k
    if (opsize == 1) // print 1 byte immediate in positive form
990
30.6k
      imm = imm & 0xff;
991
992
    // printf(">>> id = %u\n", MI->flat_insn->id);
993
78.1k
    switch (MI->flat_insn->id) {
994
34.3k
    default:
995
34.3k
      printImm(MI, O, imm, MI->csh->imm_unsigned);
996
34.3k
      break;
997
998
492
    case X86_INS_MOVABS:
999
11.3k
    case X86_INS_MOV:
1000
      // do not print number in negative form
1001
11.3k
      printImm(MI, O, imm, true);
1002
11.3k
      break;
1003
1004
0
    case X86_INS_IN:
1005
0
    case X86_INS_OUT:
1006
0
    case X86_INS_INT:
1007
      // do not print number in negative form
1008
0
      imm = imm & 0xff;
1009
0
      printImm(MI, O, imm, true);
1010
0
      break;
1011
1012
1.22k
    case X86_INS_LCALL:
1013
2.71k
    case X86_INS_LJMP:
1014
2.71k
    case X86_INS_JMP:
1015
      // always print address in positive form
1016
2.71k
      if (OpNo == 1) { // ptr16 part
1017
1.35k
        imm = imm & 0xffff;
1018
1.35k
        opsize = 2;
1019
1.35k
      } else
1020
1.35k
        opsize = 4;
1021
2.71k
      printImm(MI, O, imm, true);
1022
2.71k
      break;
1023
1024
7.12k
    case X86_INS_AND:
1025
12.0k
    case X86_INS_OR:
1026
19.6k
    case X86_INS_XOR:
1027
      // do not print number in negative form
1028
19.6k
      if (imm >= 0 && imm <= HEX_THRESHOLD)
1029
2.52k
        printImm(MI, O, imm, true);
1030
17.0k
      else {
1031
17.0k
        imm = arch_masks[opsize ? opsize : MI->imm_size] &
1032
17.0k
              imm;
1033
17.0k
        printImm(MI, O, imm, true);
1034
17.0k
      }
1035
19.6k
      break;
1036
1037
8.47k
    case X86_INS_RET:
1038
10.1k
    case X86_INS_RETF:
1039
      // RET imm16
1040
10.1k
      if (imm >= 0 && imm <= HEX_THRESHOLD)
1041
459
        printImm(MI, O, imm, true);
1042
9.67k
      else {
1043
9.67k
        imm = 0xffff & imm;
1044
9.67k
        printImm(MI, O, imm, true);
1045
9.67k
      }
1046
10.1k
      break;
1047
78.1k
    }
1048
1049
78.1k
    if (MI->csh->detail_opt) {
1050
78.1k
      if (MI->csh->doing_mem) {
1051
0
        MI->flat_insn->detail->x86
1052
0
          .operands[MI->flat_insn->detail->x86
1053
0
                .op_count]
1054
0
          .mem.disp = imm;
1055
78.1k
      } else {
1056
78.1k
#ifndef CAPSTONE_DIET
1057
78.1k
        uint8_t access[CS_X86_MAXIMUM_OPERAND_SIZE];
1058
78.1k
#endif
1059
1060
78.1k
        MI->flat_insn->detail->x86
1061
78.1k
          .operands[MI->flat_insn->detail->x86
1062
78.1k
                .op_count]
1063
78.1k
          .type = X86_OP_IMM;
1064
78.1k
        if (opsize > 0) {
1065
63.8k
          MI->flat_insn->detail->x86
1066
63.8k
            .operands[MI->flat_insn->detail
1067
63.8k
                  ->x86.op_count]
1068
63.8k
            .size = opsize;
1069
63.8k
          MI->flat_insn->detail->x86.encoding
1070
63.8k
            .imm_size = encsize;
1071
63.8k
        } else if (MI->flat_insn->detail->x86.op_count >
1072
14.3k
             0) {
1073
2.76k
          if (MI->flat_insn->id !=
1074
2.76k
                X86_INS_LCALL &&
1075
2.76k
              MI->flat_insn->id != X86_INS_LJMP) {
1076
2.76k
            MI->flat_insn->detail->x86
1077
2.76k
              .operands[MI->flat_insn
1078
2.76k
                    ->detail
1079
2.76k
                    ->x86
1080
2.76k
                    .op_count]
1081
2.76k
              .size =
1082
2.76k
              MI->flat_insn->detail
1083
2.76k
                ->x86
1084
2.76k
                .operands[0]
1085
2.76k
                .size;
1086
2.76k
          } else
1087
0
            MI->flat_insn->detail->x86
1088
0
              .operands[MI->flat_insn
1089
0
                    ->detail
1090
0
                    ->x86
1091
0
                    .op_count]
1092
0
              .size = MI->imm_size;
1093
2.76k
        } else
1094
11.5k
          MI->flat_insn->detail->x86
1095
11.5k
            .operands[MI->flat_insn->detail
1096
11.5k
                  ->x86.op_count]
1097
11.5k
            .size = MI->imm_size;
1098
78.1k
        MI->flat_insn->detail->x86
1099
78.1k
          .operands[MI->flat_insn->detail->x86
1100
78.1k
                .op_count]
1101
78.1k
          .imm = imm;
1102
1103
78.1k
#ifndef CAPSTONE_DIET
1104
78.1k
        get_op_access(
1105
78.1k
          MI->csh, MCInst_getOpcode(MI), access,
1106
78.1k
          &MI->flat_insn->detail->x86.eflags);
1107
78.1k
        MI->flat_insn->detail->x86
1108
78.1k
          .operands[MI->flat_insn->detail->x86
1109
78.1k
                .op_count]
1110
78.1k
          .access =
1111
78.1k
          access[MI->flat_insn->detail->x86
1112
78.1k
                   .op_count];
1113
78.1k
#endif
1114
1115
78.1k
        MI->flat_insn->detail->x86.op_count++;
1116
78.1k
      }
1117
78.1k
    }
1118
78.1k
  }
1119
695k
}
1120
1121
static void printMemReference(MCInst *MI, unsigned Op, SStream *O)
1122
158k
{
1123
158k
  bool NeedPlus = false;
1124
158k
  MCOperand *BaseReg = MCInst_getOperand(MI, Op + X86_AddrBaseReg);
1125
158k
  uint64_t ScaleVal =
1126
158k
    MCOperand_getImm(MCInst_getOperand(MI, Op + X86_AddrScaleAmt));
1127
158k
  MCOperand *IndexReg = MCInst_getOperand(MI, Op + X86_AddrIndexReg);
1128
158k
  MCOperand *DispSpec = MCInst_getOperand(MI, Op + X86_AddrDisp);
1129
158k
  MCOperand *SegReg = MCInst_getOperand(MI, Op + X86_AddrSegmentReg);
1130
158k
  int reg;
1131
1132
158k
  if (MI->csh->detail_opt) {
1133
158k
#ifndef CAPSTONE_DIET
1134
158k
    uint8_t access[CS_X86_MAXIMUM_OPERAND_SIZE];
1135
158k
#endif
1136
1137
158k
    MI->flat_insn->detail->x86
1138
158k
      .operands[MI->flat_insn->detail->x86.op_count]
1139
158k
      .type = X86_OP_MEM;
1140
158k
    MI->flat_insn->detail->x86
1141
158k
      .operands[MI->flat_insn->detail->x86.op_count]
1142
158k
      .size = MI->x86opsize;
1143
158k
    MI->flat_insn->detail->x86
1144
158k
      .operands[MI->flat_insn->detail->x86.op_count]
1145
158k
      .mem.segment = X86_REG_INVALID;
1146
158k
    MI->flat_insn->detail->x86
1147
158k
      .operands[MI->flat_insn->detail->x86.op_count]
1148
158k
      .mem.base = X86_register_map(MCOperand_getReg(BaseReg));
1149
158k
    if (MCOperand_getReg(IndexReg) != X86_EIZ) {
1150
157k
      MI->flat_insn->detail->x86
1151
157k
        .operands[MI->flat_insn->detail->x86.op_count]
1152
157k
        .mem.index =
1153
157k
        X86_register_map(MCOperand_getReg(IndexReg));
1154
157k
    }
1155
158k
    MI->flat_insn->detail->x86
1156
158k
      .operands[MI->flat_insn->detail->x86.op_count]
1157
158k
      .mem.scale = (int)ScaleVal;
1158
158k
    MI->flat_insn->detail->x86
1159
158k
      .operands[MI->flat_insn->detail->x86.op_count]
1160
158k
      .mem.disp = 0;
1161
1162
158k
#ifndef CAPSTONE_DIET
1163
158k
    get_op_access(MI->csh, MCInst_getOpcode(MI), access,
1164
158k
            &MI->flat_insn->detail->x86.eflags);
1165
158k
    MI->flat_insn->detail->x86
1166
158k
      .operands[MI->flat_insn->detail->x86.op_count]
1167
158k
      .access = access[MI->flat_insn->detail->x86.op_count];
1168
158k
#endif
1169
158k
  }
1170
1171
  // If this has a segment register, print it.
1172
158k
  reg = MCOperand_getReg(SegReg);
1173
158k
  if (reg) {
1174
2.42k
    _printOperand(MI, Op + X86_AddrSegmentReg, O);
1175
2.42k
    if (MI->csh->detail_opt) {
1176
2.42k
      MI->flat_insn->detail->x86
1177
2.42k
        .operands[MI->flat_insn->detail->x86.op_count]
1178
2.42k
        .mem.segment = X86_register_map(reg);
1179
2.42k
    }
1180
2.42k
    SStream_concat0(O, ":");
1181
2.42k
  }
1182
1183
158k
  SStream_concat0(O, "[");
1184
1185
158k
  if (MCOperand_getReg(BaseReg)) {
1186
154k
    _printOperand(MI, Op + X86_AddrBaseReg, O);
1187
154k
    NeedPlus = true;
1188
154k
  }
1189
1190
158k
  if (MCOperand_getReg(IndexReg) &&
1191
35.1k
      MCOperand_getReg(IndexReg) != X86_EIZ) {
1192
34.3k
    if (NeedPlus)
1193
33.5k
      SStream_concat0(O, " + ");
1194
34.3k
    _printOperand(MI, Op + X86_AddrIndexReg, O);
1195
34.3k
    if (ScaleVal != 1)
1196
6.67k
      SStream_concat(O, "*%" PRIu64, ScaleVal);
1197
34.3k
    NeedPlus = true;
1198
34.3k
  }
1199
1200
158k
  if (MCOperand_isImm(DispSpec)) {
1201
158k
    int64_t DispVal = MCOperand_getImm(DispSpec);
1202
158k
    if (MI->csh->detail_opt)
1203
158k
      MI->flat_insn->detail->x86
1204
158k
        .operands[MI->flat_insn->detail->x86.op_count]
1205
158k
        .mem.disp = DispVal;
1206
158k
    if (DispVal) {
1207
47.3k
      if (NeedPlus) {
1208
44.5k
        if (DispVal < 0) {
1209
16.7k
          SStream_concat0(O, " - ");
1210
16.7k
          printImm(MI, O, -DispVal, true);
1211
27.8k
        } else {
1212
27.8k
          SStream_concat0(O, " + ");
1213
27.8k
          printImm(MI, O, DispVal, true);
1214
27.8k
        }
1215
44.5k
      } else {
1216
        // memory reference to an immediate address
1217
2.80k
        if (x86_has_feature(MI->csh->mode, CS_MODE_64))
1218
466
          MI->op1_size = 8;
1219
2.80k
        if (DispVal < 0) {
1220
1.03k
          printImm(MI, O,
1221
1.03k
             x86_get_address_mask(
1222
1.03k
               MI->csh->mode) &
1223
1.03k
               DispVal,
1224
1.03k
             true);
1225
1.77k
        } else {
1226
1.77k
          printImm(MI, O, DispVal, true);
1227
1.77k
        }
1228
2.80k
      }
1229
1230
111k
    } else {
1231
      // DispVal = 0
1232
111k
      if (!NeedPlus) // [0]
1233
227
        SStream_concat0(O, "0");
1234
111k
    }
1235
158k
  }
1236
1237
158k
  SStream_concat0(O, "]");
1238
1239
158k
  if (MI->csh->detail_opt)
1240
158k
    MI->flat_insn->detail->x86.op_count++;
1241
1242
158k
  if (MI->op1_size == 0)
1243
98.5k
    MI->op1_size = MI->x86opsize;
1244
158k
}
1245
1246
static void printanymem(MCInst *MI, unsigned OpNo, SStream *O)
1247
7.03k
{
1248
7.03k
  switch (MI->Opcode) {
1249
555
  default:
1250
555
    break;
1251
770
  case X86_LEA16r:
1252
770
    MI->x86opsize = 2;
1253
770
    break;
1254
714
  case X86_LEA32r:
1255
1.44k
  case X86_LEA64_32r:
1256
1.44k
    MI->x86opsize = 4;
1257
1.44k
    break;
1258
419
  case X86_LEA64r:
1259
419
    MI->x86opsize = 8;
1260
419
    break;
1261
0
#ifndef CAPSTONE_X86_REDUCE
1262
387
  case X86_BNDCL32rm:
1263
793
  case X86_BNDCN32rm:
1264
1.23k
  case X86_BNDCU32rm:
1265
1.87k
  case X86_BNDSTXmr:
1266
2.56k
  case X86_BNDLDXrm:
1267
2.96k
  case X86_BNDCL64rm:
1268
3.37k
  case X86_BNDCN64rm:
1269
3.84k
  case X86_BNDCU64rm:
1270
3.84k
    MI->x86opsize = 16;
1271
3.84k
    break;
1272
7.03k
#endif
1273
7.03k
  }
1274
1275
7.03k
  printMemReference(MI, OpNo, O);
1276
7.03k
}
1277
1278
#ifdef CAPSTONE_X86_REDUCE
1279
#include "X86GenAsmWriter1_reduce.inc"
1280
#else
1281
#include "X86GenAsmWriter1.inc"
1282
#endif
1283
1284
#include "X86GenRegisterName1.inc"
1285
1286
#endif