Coverage Report

Created: 2026-09-03 07:09

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
118k
{
67
118k
  if (MI->csh->detail_opt != CS_OPT_ON)
68
0
    return;
69
70
118k
  MI->csh->doing_mem = status;
71
118k
  if (!status)
72
    // done, create the next operand slot
73
59.0k
    MI->flat_insn->detail->x86.op_count++;
74
118k
}
75
76
static void printopaquemem(MCInst *MI, unsigned OpNo, SStream *O)
77
10.5k
{
78
  // FIXME: do this with autogen
79
  // printf(">>> ID = %u\n", MI->flat_insn->id);
80
10.5k
  switch (MI->flat_insn->id) {
81
4.25k
  default:
82
4.25k
    SStream_concat0(O, "ptr ");
83
4.25k
    break;
84
599
  case X86_INS_SGDT:
85
1.36k
  case X86_INS_SIDT:
86
2.45k
  case X86_INS_LGDT:
87
3.78k
  case X86_INS_LIDT:
88
4.19k
  case X86_INS_FXRSTOR:
89
4.64k
  case X86_INS_FXSAVE:
90
5.82k
  case X86_INS_LJMP:
91
6.34k
  case X86_INS_LCALL:
92
    // do not print "ptr"
93
6.34k
    break;
94
10.5k
  }
95
96
10.5k
  switch (MI->csh->mode) {
97
3.93k
  case CS_MODE_16:
98
3.93k
    switch (MI->flat_insn->id) {
99
1.85k
    default:
100
1.85k
      MI->x86opsize = 2;
101
1.85k
      break;
102
309
    case X86_INS_LJMP:
103
618
    case X86_INS_LCALL:
104
618
      MI->x86opsize = 4;
105
618
      break;
106
319
    case X86_INS_SGDT:
107
541
    case X86_INS_SIDT:
108
1.08k
    case X86_INS_LGDT:
109
1.45k
    case X86_INS_LIDT:
110
1.45k
      MI->x86opsize = 6;
111
1.45k
      break;
112
3.93k
    }
113
3.93k
    break;
114
4.14k
  case CS_MODE_32:
115
4.14k
    switch (MI->flat_insn->id) {
116
1.47k
    default:
117
1.47k
      MI->x86opsize = 4;
118
1.47k
      break;
119
365
    case X86_INS_LJMP:
120
1.28k
    case X86_INS_JMP:
121
1.37k
    case X86_INS_LCALL:
122
1.47k
    case X86_INS_SGDT:
123
1.85k
    case X86_INS_SIDT:
124
2.07k
    case X86_INS_LGDT:
125
2.67k
    case X86_INS_LIDT:
126
2.67k
      MI->x86opsize = 6;
127
2.67k
      break;
128
4.14k
    }
129
4.14k
    break;
130
4.14k
  case CS_MODE_64:
131
2.51k
    switch (MI->flat_insn->id) {
132
875
    default:
133
875
      MI->x86opsize = 8;
134
875
      break;
135
499
    case X86_INS_LJMP:
136
619
    case X86_INS_LCALL:
137
795
    case X86_INS_SGDT:
138
960
    case X86_INS_SIDT:
139
1.29k
    case X86_INS_LGDT:
140
1.64k
    case X86_INS_LIDT:
141
1.64k
      MI->x86opsize = 10;
142
1.64k
      break;
143
2.51k
    }
144
2.51k
    break;
145
2.51k
  default: // never reach
146
0
    break;
147
10.5k
  }
148
149
10.5k
  printMemReference(MI, OpNo, O);
150
10.5k
}
151
152
static void printi8mem(MCInst *MI, unsigned OpNo, SStream *O)
153
87.0k
{
154
87.0k
  SStream_concat0(O, "byte ptr ");
155
87.0k
  MI->x86opsize = 1;
156
87.0k
  printMemReference(MI, OpNo, O);
157
87.0k
}
158
159
static void printi16mem(MCInst *MI, unsigned OpNo, SStream *O)
160
20.7k
{
161
20.7k
  MI->x86opsize = 2;
162
20.7k
  SStream_concat0(O, "word ptr ");
163
20.7k
  printMemReference(MI, OpNo, O);
164
20.7k
}
165
166
static void printi32mem(MCInst *MI, unsigned OpNo, SStream *O)
167
40.7k
{
168
40.7k
  MI->x86opsize = 4;
169
40.7k
  SStream_concat0(O, "dword ptr ");
170
40.7k
  printMemReference(MI, OpNo, O);
171
40.7k
}
172
173
static void printi64mem(MCInst *MI, unsigned OpNo, SStream *O)
174
13.0k
{
175
13.0k
  SStream_concat0(O, "qword ptr ");
176
13.0k
  MI->x86opsize = 8;
177
13.0k
  printMemReference(MI, OpNo, O);
178
13.0k
}
179
180
static void printi128mem(MCInst *MI, unsigned OpNo, SStream *O)
181
3.98k
{
182
3.98k
  SStream_concat0(O, "xmmword ptr ");
183
3.98k
  MI->x86opsize = 16;
184
3.98k
  printMemReference(MI, OpNo, O);
185
3.98k
}
186
187
static void printi512mem(MCInst *MI, unsigned OpNo, SStream *O)
188
2.10k
{
189
2.10k
  SStream_concat0(O, "zmmword ptr ");
190
2.10k
  MI->x86opsize = 64;
191
2.10k
  printMemReference(MI, OpNo, O);
192
2.10k
}
193
194
#ifndef CAPSTONE_X86_REDUCE
195
static void printi256mem(MCInst *MI, unsigned OpNo, SStream *O)
196
1.99k
{
197
1.99k
  SStream_concat0(O, "ymmword ptr ");
198
1.99k
  MI->x86opsize = 32;
199
1.99k
  printMemReference(MI, OpNo, O);
200
1.99k
}
201
202
static void printf32mem(MCInst *MI, unsigned OpNo, SStream *O)
203
3.99k
{
204
3.99k
  switch (MCInst_getOpcode(MI)) {
205
2.85k
  default:
206
2.85k
    SStream_concat0(O, "dword ptr ");
207
2.85k
    MI->x86opsize = 4;
208
2.85k
    break;
209
610
  case X86_FSTENVm:
210
1.14k
  case X86_FLDENVm:
211
    // TODO: fix this in tablegen instead
212
1.14k
    switch (MI->csh->mode) {
213
0
    default: // never reach
214
0
      break;
215
330
    case CS_MODE_16:
216
330
      MI->x86opsize = 14;
217
330
      break;
218
576
    case CS_MODE_32:
219
814
    case CS_MODE_64:
220
814
      MI->x86opsize = 28;
221
814
      break;
222
1.14k
    }
223
1.14k
    break;
224
3.99k
  }
225
226
3.99k
  printMemReference(MI, OpNo, O);
227
3.99k
}
228
229
static void printf64mem(MCInst *MI, unsigned OpNo, SStream *O)
230
4.50k
{
231
  // TODO: fix COMISD in Tablegen instead (#1456)
232
4.50k
  if (MI->op1_size == 16) {
233
    // printf("printf64mem id = %u\n", MCInst_getOpcode(MI));
234
1.95k
    switch (MCInst_getOpcode(MI)) {
235
1.95k
    default:
236
1.95k
      SStream_concat0(O, "qword ptr ");
237
1.95k
      MI->x86opsize = 8;
238
1.95k
      break;
239
0
    case X86_MOVPQI2QImr:
240
0
      SStream_concat0(O, "xmmword ptr ");
241
0
      MI->x86opsize = 16;
242
0
      break;
243
1.95k
    }
244
2.54k
  } else {
245
2.54k
    SStream_concat0(O, "qword ptr ");
246
2.54k
    MI->x86opsize = 8;
247
2.54k
  }
248
249
4.50k
  printMemReference(MI, OpNo, O);
250
4.50k
}
251
252
static void printf80mem(MCInst *MI, unsigned OpNo, SStream *O)
253
329
{
254
329
  switch (MCInst_getOpcode(MI)) {
255
112
  default:
256
112
    SStream_concat0(O, "xword ptr ");
257
112
    break;
258
138
  case X86_FBLDm:
259
217
  case X86_FBSTPm:
260
217
    break;
261
329
  }
262
263
329
  MI->x86opsize = 10;
264
329
  printMemReference(MI, OpNo, O);
265
329
}
266
267
static void printf128mem(MCInst *MI, unsigned OpNo, SStream *O)
268
2.71k
{
269
2.71k
  SStream_concat0(O, "xmmword ptr ");
270
2.71k
  MI->x86opsize = 16;
271
2.71k
  printMemReference(MI, OpNo, O);
272
2.71k
}
273
274
static void printf256mem(MCInst *MI, unsigned OpNo, SStream *O)
275
1.53k
{
276
1.53k
  SStream_concat0(O, "ymmword ptr ");
277
1.53k
  MI->x86opsize = 32;
278
1.53k
  printMemReference(MI, OpNo, O);
279
1.53k
}
280
281
static void printf512mem(MCInst *MI, unsigned OpNo, SStream *O)
282
1.95k
{
283
1.95k
  SStream_concat0(O, "zmmword ptr ");
284
1.95k
  MI->x86opsize = 64;
285
1.95k
  printMemReference(MI, OpNo, O);
286
1.95k
}
287
#endif
288
289
static const char *getRegisterName(unsigned RegNo);
290
static void printRegName(SStream *OS, unsigned RegNo)
291
666k
{
292
666k
  SStream_concat0(OS, getRegisterName(RegNo));
293
666k
}
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
191k
{
311
191k
  if (positive) {
312
    // always print this number in positive form
313
163k
    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
163k
    } else { // Intel syntax
350
163k
      if (imm < 0) {
351
1.96k
        if (MI->op1_size) {
352
326
          switch (MI->op1_size) {
353
326
          default:
354
326
            break;
355
326
          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
326
          }
365
326
        }
366
367
1.96k
        SStream_concat(O, "0x%" PRIx64, imm);
368
161k
      } else {
369
161k
        if (imm > HEX_THRESHOLD)
370
152k
          SStream_concat(O, "0x%" PRIx64, imm);
371
9.32k
        else
372
9.32k
          SStream_concat(O, "%" PRIu64, imm);
373
161k
      }
374
163k
    }
375
163k
  } else {
376
27.8k
    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
27.8k
    } else { // Intel syntax
404
27.8k
      if (imm < 0) {
405
3.13k
        if (imm == 0x8000000000000000LL) // imm == -imm
406
0
          SStream_concat0(O,
407
0
              "0x8000000000000000");
408
3.13k
        else if (imm < -HEX_THRESHOLD)
409
2.96k
          SStream_concat(O, "-0x%" PRIx64, -imm);
410
172
        else
411
172
          SStream_concat(O, "-%" PRIu64, -imm);
412
413
24.7k
      } else {
414
24.7k
        if (imm > HEX_THRESHOLD)
415
21.1k
          SStream_concat(O, "0x%" PRIx64, imm);
416
3.63k
        else
417
3.63k
          SStream_concat(O, "%" PRIu64, imm);
418
24.7k
      }
419
27.8k
    }
420
27.8k
  }
421
191k
}
422
423
// local printOperand, without updating public operands
424
static void _printOperand(MCInst *MI, unsigned OpNo, SStream *O)
425
245k
{
426
245k
  MCOperand *Op = MCInst_getOperand(MI, OpNo);
427
245k
  if (MCOperand_isReg(Op)) {
428
245k
    printRegName(O, MCOperand_getReg(Op));
429
245k
  } 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
245k
}
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.24M
{
440
1.24M
#ifndef CAPSTONE_DIET
441
1.24M
  uint8_t i;
442
1.24M
  const uint8_t *arr = X86_get_op_access(h, id, eflags);
443
444
  // initialize access
445
1.24M
  memset(access, 0, CS_X86_MAXIMUM_OPERAND_SIZE * sizeof(access[0]));
446
447
1.24M
  if (!arr) {
448
0
    access[0] = 0;
449
0
    return;
450
0
  }
451
452
  // copy to access but zero out CS_AC_IGNORE
453
3.55M
  for (i = 0; arr[i]; i++) {
454
2.30M
    if (arr[i] != CS_AC_IGNORE)
455
1.93M
      access[i] = arr[i];
456
372k
    else
457
372k
      access[i] = 0;
458
2.30M
  }
459
460
  // mark the end of array
461
1.24M
  access[i] = 0;
462
1.24M
#endif
463
1.24M
}
464
#endif
465
466
static void printSrcIdx(MCInst *MI, unsigned Op, SStream *O)
467
25.1k
{
468
25.1k
  MCOperand *SegReg;
469
25.1k
  int reg;
470
471
25.1k
  if (MI->csh->detail_opt) {
472
25.1k
#ifndef CAPSTONE_DIET
473
25.1k
    uint8_t access[CS_X86_MAXIMUM_OPERAND_SIZE];
474
25.1k
#endif
475
476
25.1k
    MI->flat_insn->detail->x86
477
25.1k
      .operands[MI->flat_insn->detail->x86.op_count]
478
25.1k
      .type = X86_OP_MEM;
479
25.1k
    MI->flat_insn->detail->x86
480
25.1k
      .operands[MI->flat_insn->detail->x86.op_count]
481
25.1k
      .size = MI->x86opsize;
482
25.1k
    MI->flat_insn->detail->x86
483
25.1k
      .operands[MI->flat_insn->detail->x86.op_count]
484
25.1k
      .mem.segment = X86_REG_INVALID;
485
25.1k
    MI->flat_insn->detail->x86
486
25.1k
      .operands[MI->flat_insn->detail->x86.op_count]
487
25.1k
      .mem.base = X86_REG_INVALID;
488
25.1k
    MI->flat_insn->detail->x86
489
25.1k
      .operands[MI->flat_insn->detail->x86.op_count]
490
25.1k
      .mem.index = X86_REG_INVALID;
491
25.1k
    MI->flat_insn->detail->x86
492
25.1k
      .operands[MI->flat_insn->detail->x86.op_count]
493
25.1k
      .mem.scale = 1;
494
25.1k
    MI->flat_insn->detail->x86
495
25.1k
      .operands[MI->flat_insn->detail->x86.op_count]
496
25.1k
      .mem.disp = 0;
497
498
25.1k
#ifndef CAPSTONE_DIET
499
25.1k
    get_op_access(MI->csh, MCInst_getOpcode(MI), access,
500
25.1k
            &MI->flat_insn->detail->x86.eflags);
501
25.1k
    MI->flat_insn->detail->x86
502
25.1k
      .operands[MI->flat_insn->detail->x86.op_count]
503
25.1k
      .access = access[MI->flat_insn->detail->x86.op_count];
504
25.1k
#endif
505
25.1k
  }
506
507
25.1k
  SegReg = MCInst_getOperand(MI, Op + 1);
508
25.1k
  reg = MCOperand_getReg(SegReg);
509
510
  // If this has a segment register, print it.
511
25.1k
  if (reg) {
512
788
    _printOperand(MI, Op + 1, O);
513
788
    if (MI->csh->detail_opt) {
514
788
      MI->flat_insn->detail->x86
515
788
        .operands[MI->flat_insn->detail->x86.op_count]
516
788
        .mem.segment = X86_register_map(reg);
517
788
    }
518
788
    SStream_concat0(O, ":");
519
788
  }
520
521
25.1k
  SStream_concat0(O, "[");
522
25.1k
  set_mem_access(MI, true);
523
25.1k
  printOperand(MI, Op, O);
524
25.1k
  SStream_concat0(O, "]");
525
25.1k
  set_mem_access(MI, false);
526
25.1k
}
527
528
static void printDstIdx(MCInst *MI, unsigned Op, SStream *O)
529
33.8k
{
530
33.8k
  if (MI->csh->detail_opt) {
531
33.8k
#ifndef CAPSTONE_DIET
532
33.8k
    uint8_t access[CS_X86_MAXIMUM_OPERAND_SIZE];
533
33.8k
#endif
534
535
33.8k
    MI->flat_insn->detail->x86
536
33.8k
      .operands[MI->flat_insn->detail->x86.op_count]
537
33.8k
      .type = X86_OP_MEM;
538
33.8k
    MI->flat_insn->detail->x86
539
33.8k
      .operands[MI->flat_insn->detail->x86.op_count]
540
33.8k
      .size = MI->x86opsize;
541
33.8k
    MI->flat_insn->detail->x86
542
33.8k
      .operands[MI->flat_insn->detail->x86.op_count]
543
33.8k
      .mem.segment = X86_REG_INVALID;
544
33.8k
    MI->flat_insn->detail->x86
545
33.8k
      .operands[MI->flat_insn->detail->x86.op_count]
546
33.8k
      .mem.base = X86_REG_INVALID;
547
33.8k
    MI->flat_insn->detail->x86
548
33.8k
      .operands[MI->flat_insn->detail->x86.op_count]
549
33.8k
      .mem.index = X86_REG_INVALID;
550
33.8k
    MI->flat_insn->detail->x86
551
33.8k
      .operands[MI->flat_insn->detail->x86.op_count]
552
33.8k
      .mem.scale = 1;
553
33.8k
    MI->flat_insn->detail->x86
554
33.8k
      .operands[MI->flat_insn->detail->x86.op_count]
555
33.8k
      .mem.disp = 0;
556
557
33.8k
#ifndef CAPSTONE_DIET
558
33.8k
    get_op_access(MI->csh, MCInst_getOpcode(MI), access,
559
33.8k
            &MI->flat_insn->detail->x86.eflags);
560
33.8k
    MI->flat_insn->detail->x86
561
33.8k
      .operands[MI->flat_insn->detail->x86.op_count]
562
33.8k
      .access = access[MI->flat_insn->detail->x86.op_count];
563
33.8k
#endif
564
33.8k
  }
565
566
  // DI accesses are always ES-based on non-64bit mode
567
33.8k
  if (MI->csh->mode != CS_MODE_64) {
568
21.1k
    SStream_concat0(O, "es:[");
569
21.1k
    if (MI->csh->detail_opt) {
570
21.1k
      MI->flat_insn->detail->x86
571
21.1k
        .operands[MI->flat_insn->detail->x86.op_count]
572
21.1k
        .mem.segment = X86_REG_ES;
573
21.1k
    }
574
21.1k
  } else
575
12.6k
    SStream_concat0(O, "[");
576
577
33.8k
  set_mem_access(MI, true);
578
33.8k
  printOperand(MI, Op, O);
579
33.8k
  SStream_concat0(O, "]");
580
33.8k
  set_mem_access(MI, false);
581
33.8k
}
582
583
static void printSrcIdx8(MCInst *MI, unsigned OpNo, SStream *O)
584
9.56k
{
585
9.56k
  SStream_concat0(O, "byte ptr ");
586
9.56k
  MI->x86opsize = 1;
587
9.56k
  printSrcIdx(MI, OpNo, O);
588
9.56k
}
589
590
static void printSrcIdx16(MCInst *MI, unsigned OpNo, SStream *O)
591
5.51k
{
592
5.51k
  SStream_concat0(O, "word ptr ");
593
5.51k
  MI->x86opsize = 2;
594
5.51k
  printSrcIdx(MI, OpNo, O);
595
5.51k
}
596
597
static void printSrcIdx32(MCInst *MI, unsigned OpNo, SStream *O)
598
7.87k
{
599
7.87k
  SStream_concat0(O, "dword ptr ");
600
7.87k
  MI->x86opsize = 4;
601
7.87k
  printSrcIdx(MI, OpNo, O);
602
7.87k
}
603
604
static void printSrcIdx64(MCInst *MI, unsigned OpNo, SStream *O)
605
2.19k
{
606
2.19k
  SStream_concat0(O, "qword ptr ");
607
2.19k
  MI->x86opsize = 8;
608
2.19k
  printSrcIdx(MI, OpNo, O);
609
2.19k
}
610
611
static void printDstIdx8(MCInst *MI, unsigned OpNo, SStream *O)
612
13.1k
{
613
13.1k
  SStream_concat0(O, "byte ptr ");
614
13.1k
  MI->x86opsize = 1;
615
13.1k
  printDstIdx(MI, OpNo, O);
616
13.1k
}
617
618
static void printDstIdx16(MCInst *MI, unsigned OpNo, SStream *O)
619
7.61k
{
620
7.61k
  SStream_concat0(O, "word ptr ");
621
7.61k
  MI->x86opsize = 2;
622
7.61k
  printDstIdx(MI, OpNo, O);
623
7.61k
}
624
625
static void printDstIdx32(MCInst *MI, unsigned OpNo, SStream *O)
626
9.70k
{
627
9.70k
  SStream_concat0(O, "dword ptr ");
628
9.70k
  MI->x86opsize = 4;
629
9.70k
  printDstIdx(MI, OpNo, O);
630
9.70k
}
631
632
static void printDstIdx64(MCInst *MI, unsigned OpNo, SStream *O)
633
3.42k
{
634
3.42k
  SStream_concat0(O, "qword ptr ");
635
3.42k
  MI->x86opsize = 8;
636
3.42k
  printDstIdx(MI, OpNo, O);
637
3.42k
}
638
639
static void printMemOffset(MCInst *MI, unsigned Op, SStream *O)
640
4.62k
{
641
4.62k
  MCOperand *DispSpec = MCInst_getOperand(MI, Op);
642
4.62k
  MCOperand *SegReg = MCInst_getOperand(MI, Op + 1);
643
4.62k
  int reg;
644
645
4.62k
  if (MI->csh->detail_opt) {
646
4.62k
#ifndef CAPSTONE_DIET
647
4.62k
    uint8_t access[CS_X86_MAXIMUM_OPERAND_SIZE];
648
4.62k
#endif
649
650
4.62k
    MI->flat_insn->detail->x86
651
4.62k
      .operands[MI->flat_insn->detail->x86.op_count]
652
4.62k
      .type = X86_OP_MEM;
653
4.62k
    MI->flat_insn->detail->x86
654
4.62k
      .operands[MI->flat_insn->detail->x86.op_count]
655
4.62k
      .size = MI->x86opsize;
656
4.62k
    MI->flat_insn->detail->x86
657
4.62k
      .operands[MI->flat_insn->detail->x86.op_count]
658
4.62k
      .mem.segment = X86_REG_INVALID;
659
4.62k
    MI->flat_insn->detail->x86
660
4.62k
      .operands[MI->flat_insn->detail->x86.op_count]
661
4.62k
      .mem.base = X86_REG_INVALID;
662
4.62k
    MI->flat_insn->detail->x86
663
4.62k
      .operands[MI->flat_insn->detail->x86.op_count]
664
4.62k
      .mem.index = X86_REG_INVALID;
665
4.62k
    MI->flat_insn->detail->x86
666
4.62k
      .operands[MI->flat_insn->detail->x86.op_count]
667
4.62k
      .mem.scale = 1;
668
4.62k
    MI->flat_insn->detail->x86
669
4.62k
      .operands[MI->flat_insn->detail->x86.op_count]
670
4.62k
      .mem.disp = 0;
671
672
4.62k
#ifndef CAPSTONE_DIET
673
4.62k
    get_op_access(MI->csh, MCInst_getOpcode(MI), access,
674
4.62k
            &MI->flat_insn->detail->x86.eflags);
675
4.62k
    MI->flat_insn->detail->x86
676
4.62k
      .operands[MI->flat_insn->detail->x86.op_count]
677
4.62k
      .access = access[MI->flat_insn->detail->x86.op_count];
678
4.62k
#endif
679
4.62k
  }
680
681
  // If this has a segment register, print it.
682
4.62k
  reg = MCOperand_getReg(SegReg);
683
4.62k
  if (reg) {
684
362
    _printOperand(MI, Op + 1, O);
685
362
    SStream_concat0(O, ":");
686
362
    if (MI->csh->detail_opt) {
687
362
      MI->flat_insn->detail->x86
688
362
        .operands[MI->flat_insn->detail->x86.op_count]
689
362
        .mem.segment = X86_register_map(reg);
690
362
    }
691
362
  }
692
693
4.62k
  SStream_concat0(O, "[");
694
695
4.62k
  if (MCOperand_isImm(DispSpec)) {
696
4.62k
    int64_t imm = MCOperand_getImm(DispSpec);
697
4.62k
    if (MI->csh->detail_opt)
698
4.62k
      MI->flat_insn->detail->x86
699
4.62k
        .operands[MI->flat_insn->detail->x86.op_count]
700
4.62k
        .mem.disp = imm;
701
702
4.62k
    if (imm < 0)
703
751
      printImm(MI, O, arch_masks[MI->csh->mode] & imm, true);
704
3.87k
    else
705
3.87k
      printImm(MI, O, imm, true);
706
4.62k
  }
707
708
4.62k
  SStream_concat0(O, "]");
709
710
4.62k
  if (MI->csh->detail_opt)
711
4.62k
    MI->flat_insn->detail->x86.op_count++;
712
713
4.62k
  if (MI->op1_size == 0)
714
4.62k
    MI->op1_size = MI->x86opsize;
715
4.62k
}
716
717
static void printU8Imm(MCInst *MI, unsigned Op, SStream *O)
718
26.2k
{
719
26.2k
  uint8_t val = MCOperand_getImm(MCInst_getOperand(MI, Op)) & 0xff;
720
721
26.2k
  printImm(MI, O, val, true);
722
723
26.2k
  if (MI->csh->detail_opt) {
724
26.2k
#ifndef CAPSTONE_DIET
725
26.2k
    uint8_t access[CS_X86_MAXIMUM_OPERAND_SIZE];
726
26.2k
#endif
727
728
26.2k
    MI->flat_insn->detail->x86
729
26.2k
      .operands[MI->flat_insn->detail->x86.op_count]
730
26.2k
      .type = X86_OP_IMM;
731
26.2k
    MI->flat_insn->detail->x86
732
26.2k
      .operands[MI->flat_insn->detail->x86.op_count]
733
26.2k
      .imm = val;
734
26.2k
    MI->flat_insn->detail->x86
735
26.2k
      .operands[MI->flat_insn->detail->x86.op_count]
736
26.2k
      .size = 1;
737
738
26.2k
#ifndef CAPSTONE_DIET
739
26.2k
    get_op_access(MI->csh, MCInst_getOpcode(MI), access,
740
26.2k
            &MI->flat_insn->detail->x86.eflags);
741
26.2k
    MI->flat_insn->detail->x86
742
26.2k
      .operands[MI->flat_insn->detail->x86.op_count]
743
26.2k
      .access = access[MI->flat_insn->detail->x86.op_count];
744
26.2k
#endif
745
746
26.2k
    MI->flat_insn->detail->x86.op_count++;
747
26.2k
  }
748
26.2k
}
749
750
static void printMemOffs8(MCInst *MI, unsigned OpNo, SStream *O)
751
2.40k
{
752
2.40k
  SStream_concat0(O, "byte ptr ");
753
2.40k
  MI->x86opsize = 1;
754
2.40k
  printMemOffset(MI, OpNo, O);
755
2.40k
}
756
757
static void printMemOffs16(MCInst *MI, unsigned OpNo, SStream *O)
758
583
{
759
583
  SStream_concat0(O, "word ptr ");
760
583
  MI->x86opsize = 2;
761
583
  printMemOffset(MI, OpNo, O);
762
583
}
763
764
static void printMemOffs32(MCInst *MI, unsigned OpNo, SStream *O)
765
1.52k
{
766
1.52k
  SStream_concat0(O, "dword ptr ");
767
1.52k
  MI->x86opsize = 4;
768
1.52k
  printMemOffset(MI, OpNo, O);
769
1.52k
}
770
771
static void printMemOffs64(MCInst *MI, unsigned OpNo, SStream *O)
772
121
{
773
121
  SStream_concat0(O, "qword ptr ");
774
121
  MI->x86opsize = 8;
775
121
  printMemOffset(MI, OpNo, O);
776
121
}
777
778
static void printInstruction(MCInst *MI, SStream *O);
779
780
void X86_Intel_printInst(MCInst *MI, SStream *O, void *Info)
781
496k
{
782
496k
  x86_reg reg = X86_REG_INVALID, reg2;
783
496k
  enum cs_ac_type access1, access2;
784
785
  // printf("opcode = %u\n", MCInst_getOpcode(MI));
786
787
  // perhaps this instruction does not need printer
788
496k
  if (MI->assembly[0]) {
789
0
    strncpy(O->buffer, MI->assembly, sizeof(O->buffer));
790
0
    return;
791
0
  }
792
793
496k
  X86_lockrep(MI, O);
794
496k
  printInstruction(MI, O);
795
796
496k
  if (MI->csh->detail_opt) {
797
496k
#ifndef CAPSTONE_DIET
798
496k
    uint8_t access[CS_X86_MAXIMUM_OPERAND_SIZE] = { 0 };
799
496k
#endif
800
801
    // first op can be embedded in the asm by llvm.
802
    // so we have to add the missing register as the first operand
803
496k
    reg = X86_insn_reg_intel_h(MI->csh, MCInst_getOpcode(MI),
804
496k
             &access1);
805
496k
    if (reg) {
806
      // shift all the ops right to leave 1st slot for this new register op
807
52.8k
      memmove(&(MI->flat_insn->detail->x86.operands[1]),
808
52.8k
        &(MI->flat_insn->detail->x86.operands[0]),
809
52.8k
        sizeof(MI->flat_insn->detail->x86.operands[0]) *
810
52.8k
          (ARR_SIZE(MI->flat_insn->detail->x86
811
52.8k
                .operands) -
812
52.8k
           1));
813
52.8k
      MI->flat_insn->detail->x86.operands[0].type =
814
52.8k
        X86_OP_REG;
815
52.8k
      MI->flat_insn->detail->x86.operands[0].reg = reg;
816
52.8k
      MI->flat_insn->detail->x86.operands[0].size =
817
52.8k
        MI->csh->regsize_map[reg];
818
52.8k
      MI->flat_insn->detail->x86.operands[0].access = access1;
819
52.8k
      MI->flat_insn->detail->x86.op_count++;
820
443k
    } else {
821
443k
      if (X86_insn_reg_intel2(MCInst_getOpcode(MI), &reg,
822
443k
            &access1, &reg2, &access2)) {
823
9.56k
        MI->flat_insn->detail->x86.operands[0].type =
824
9.56k
          X86_OP_REG;
825
9.56k
        MI->flat_insn->detail->x86.operands[0].reg =
826
9.56k
          reg;
827
9.56k
        MI->flat_insn->detail->x86.operands[0].size =
828
9.56k
          MI->csh->regsize_map[reg];
829
9.56k
        MI->flat_insn->detail->x86.operands[0].access =
830
9.56k
          access1;
831
9.56k
        MI->flat_insn->detail->x86.operands[1].type =
832
9.56k
          X86_OP_REG;
833
9.56k
        MI->flat_insn->detail->x86.operands[1].reg =
834
9.56k
          reg2;
835
9.56k
        MI->flat_insn->detail->x86.operands[1].size =
836
9.56k
          MI->csh->regsize_map[reg2];
837
9.56k
        MI->flat_insn->detail->x86.operands[1].access =
838
9.56k
          access2;
839
9.56k
        MI->flat_insn->detail->x86.op_count = 2;
840
9.56k
      }
841
443k
    }
842
843
496k
#ifndef CAPSTONE_DIET
844
496k
    get_op_access(MI->csh, MCInst_getOpcode(MI), access,
845
496k
            &MI->flat_insn->detail->x86.eflags);
846
496k
    MI->flat_insn->detail->x86.operands[0].access = access[0];
847
496k
    MI->flat_insn->detail->x86.operands[1].access = access[1];
848
496k
#endif
849
496k
  }
850
851
496k
  if (MI->op1_size == 0 && reg)
852
41.7k
    MI->op1_size = MI->csh->regsize_map[reg];
853
496k
}
854
855
/// printPCRelImm - This is used to print an immediate value that ends up
856
/// being encoded as a pc-relative value.
857
static void printPCRelImm(MCInst *MI, unsigned OpNo, SStream *O)
858
31.1k
{
859
31.1k
  MCOperand *Op = MCInst_getOperand(MI, OpNo);
860
31.1k
  if (MCOperand_isImm(Op)) {
861
31.1k
    int64_t imm = MCOperand_getImm(Op) + MI->flat_insn->size +
862
31.1k
            MI->address;
863
31.1k
    uint8_t opsize = X86_immediate_size(MI->Opcode, NULL);
864
865
    // truncate imm for non-64bit
866
31.1k
    if (MI->csh->mode != CS_MODE_64) {
867
20.9k
      imm = imm & 0xffffffff;
868
20.9k
    }
869
870
31.1k
    printImm(MI, O, imm, true);
871
872
31.1k
    if (MI->csh->detail_opt) {
873
31.1k
#ifndef CAPSTONE_DIET
874
31.1k
      uint8_t access[CS_X86_MAXIMUM_OPERAND_SIZE];
875
31.1k
#endif
876
877
31.1k
      MI->flat_insn->detail->x86
878
31.1k
        .operands[MI->flat_insn->detail->x86.op_count]
879
31.1k
        .type = X86_OP_IMM;
880
      // if op_count > 0, then this operand's size is taken from the destination op
881
31.1k
      if (MI->flat_insn->detail->x86.op_count > 0)
882
0
        MI->flat_insn->detail->x86
883
0
          .operands[MI->flat_insn->detail->x86
884
0
                .op_count]
885
0
          .size =
886
0
          MI->flat_insn->detail->x86.operands[0]
887
0
            .size;
888
31.1k
      else if (opsize > 0)
889
1.37k
        MI->flat_insn->detail->x86
890
1.37k
          .operands[MI->flat_insn->detail->x86
891
1.37k
                .op_count]
892
1.37k
          .size = opsize;
893
29.7k
      else
894
29.7k
        MI->flat_insn->detail->x86
895
29.7k
          .operands[MI->flat_insn->detail->x86
896
29.7k
                .op_count]
897
29.7k
          .size = MI->imm_size;
898
31.1k
      MI->flat_insn->detail->x86
899
31.1k
        .operands[MI->flat_insn->detail->x86.op_count]
900
31.1k
        .imm = imm;
901
902
31.1k
#ifndef CAPSTONE_DIET
903
31.1k
      get_op_access(MI->csh, MCInst_getOpcode(MI), access,
904
31.1k
              &MI->flat_insn->detail->x86.eflags);
905
31.1k
      MI->flat_insn->detail->x86
906
31.1k
        .operands[MI->flat_insn->detail->x86.op_count]
907
31.1k
        .access =
908
31.1k
        access[MI->flat_insn->detail->x86.op_count];
909
31.1k
#endif
910
911
31.1k
      MI->flat_insn->detail->x86.op_count++;
912
31.1k
    }
913
914
31.1k
    if (MI->op1_size == 0)
915
31.1k
      MI->op1_size = MI->imm_size;
916
31.1k
  }
917
31.1k
}
918
919
static void printOperand(MCInst *MI, unsigned OpNo, SStream *O)
920
486k
{
921
486k
  MCOperand *Op = MCInst_getOperand(MI, OpNo);
922
923
486k
  if (MCOperand_isReg(Op)) {
924
420k
    unsigned int reg = MCOperand_getReg(Op);
925
926
420k
    printRegName(O, reg);
927
420k
    if (MI->csh->detail_opt) {
928
420k
      if (MI->csh->doing_mem) {
929
59.0k
        MI->flat_insn->detail->x86
930
59.0k
          .operands[MI->flat_insn->detail->x86
931
59.0k
                .op_count]
932
59.0k
          .mem.base = X86_register_map(reg);
933
361k
      } else {
934
361k
#ifndef CAPSTONE_DIET
935
361k
        uint8_t access[CS_X86_MAXIMUM_OPERAND_SIZE];
936
361k
#endif
937
938
361k
        MI->flat_insn->detail->x86
939
361k
          .operands[MI->flat_insn->detail->x86
940
361k
                .op_count]
941
361k
          .type = X86_OP_REG;
942
361k
        MI->flat_insn->detail->x86
943
361k
          .operands[MI->flat_insn->detail->x86
944
361k
                .op_count]
945
361k
          .reg = X86_register_map(reg);
946
361k
        MI->flat_insn->detail->x86
947
361k
          .operands[MI->flat_insn->detail->x86
948
361k
                .op_count]
949
361k
          .size =
950
361k
          MI->csh->regsize_map[X86_register_map(
951
361k
            reg)];
952
953
361k
#ifndef CAPSTONE_DIET
954
361k
        get_op_access(
955
361k
          MI->csh, MCInst_getOpcode(MI), access,
956
361k
          &MI->flat_insn->detail->x86.eflags);
957
361k
        MI->flat_insn->detail->x86
958
361k
          .operands[MI->flat_insn->detail->x86
959
361k
                .op_count]
960
361k
          .access =
961
361k
          access[MI->flat_insn->detail->x86
962
361k
                   .op_count];
963
361k
#endif
964
965
361k
        MI->flat_insn->detail->x86.op_count++;
966
361k
      }
967
420k
    }
968
969
420k
    if (MI->op1_size == 0)
970
219k
      MI->op1_size =
971
219k
        MI->csh->regsize_map[X86_register_map(reg)];
972
420k
  } else if (MCOperand_isImm(Op)) {
973
66.0k
    uint8_t encsize;
974
66.0k
    int64_t imm = MCOperand_getImm(Op);
975
66.0k
    uint8_t opsize =
976
66.0k
      X86_immediate_size(MCInst_getOpcode(MI), &encsize);
977
978
66.0k
    if (opsize == 1) // print 1 byte immediate in positive form
979
31.2k
      imm = imm & 0xff;
980
981
    // printf(">>> id = %u\n", MI->flat_insn->id);
982
66.0k
    switch (MI->flat_insn->id) {
983
27.8k
    default:
984
27.8k
      printImm(MI, O, imm, MI->csh->imm_unsigned);
985
27.8k
      break;
986
987
378
    case X86_INS_MOVABS:
988
13.7k
    case X86_INS_MOV:
989
      // do not print number in negative form
990
13.7k
      printImm(MI, O, imm, true);
991
13.7k
      break;
992
993
0
    case X86_INS_IN:
994
0
    case X86_INS_OUT:
995
0
    case X86_INS_INT:
996
      // do not print number in negative form
997
0
      imm = imm & 0xff;
998
0
      printImm(MI, O, imm, true);
999
0
      break;
1000
1001
944
    case X86_INS_LCALL:
1002
2.01k
    case X86_INS_LJMP:
1003
2.01k
    case X86_INS_JMP:
1004
      // always print address in positive form
1005
2.01k
      if (OpNo == 1) { // ptr16 part
1006
1.00k
        imm = imm & 0xffff;
1007
1.00k
        opsize = 2;
1008
1.00k
      } else
1009
1.00k
        opsize = 4;
1010
2.01k
      printImm(MI, O, imm, true);
1011
2.01k
      break;
1012
1013
5.74k
    case X86_INS_AND:
1014
10.1k
    case X86_INS_OR:
1015
15.0k
    case X86_INS_XOR:
1016
      // do not print number in negative form
1017
15.0k
      if (imm >= 0 && imm <= HEX_THRESHOLD)
1018
1.55k
        printImm(MI, O, imm, true);
1019
13.5k
      else {
1020
13.5k
        imm = arch_masks[opsize ? opsize : MI->imm_size] &
1021
13.5k
              imm;
1022
13.5k
        printImm(MI, O, imm, true);
1023
13.5k
      }
1024
15.0k
      break;
1025
1026
5.74k
    case X86_INS_RET:
1027
7.29k
    case X86_INS_RETF:
1028
      // RET imm16
1029
7.29k
      if (imm >= 0 && imm <= HEX_THRESHOLD)
1030
276
        printImm(MI, O, imm, true);
1031
7.02k
      else {
1032
7.02k
        imm = 0xffff & imm;
1033
7.02k
        printImm(MI, O, imm, true);
1034
7.02k
      }
1035
7.29k
      break;
1036
66.0k
    }
1037
1038
66.0k
    if (MI->csh->detail_opt) {
1039
66.0k
      if (MI->csh->doing_mem) {
1040
0
        MI->flat_insn->detail->x86
1041
0
          .operands[MI->flat_insn->detail->x86
1042
0
                .op_count]
1043
0
          .mem.disp = imm;
1044
66.0k
      } else {
1045
66.0k
#ifndef CAPSTONE_DIET
1046
66.0k
        uint8_t access[CS_X86_MAXIMUM_OPERAND_SIZE];
1047
66.0k
#endif
1048
1049
66.0k
        MI->flat_insn->detail->x86
1050
66.0k
          .operands[MI->flat_insn->detail->x86
1051
66.0k
                .op_count]
1052
66.0k
          .type = X86_OP_IMM;
1053
66.0k
        if (opsize > 0) {
1054
55.4k
          MI->flat_insn->detail->x86
1055
55.4k
            .operands[MI->flat_insn->detail
1056
55.4k
                  ->x86.op_count]
1057
55.4k
            .size = opsize;
1058
55.4k
          MI->flat_insn->detail->x86.encoding
1059
55.4k
            .imm_size = encsize;
1060
55.4k
        } else if (MI->flat_insn->detail->x86.op_count >
1061
10.5k
             0) {
1062
2.09k
          if (MI->flat_insn->id !=
1063
2.09k
                X86_INS_LCALL &&
1064
2.09k
              MI->flat_insn->id != X86_INS_LJMP) {
1065
2.09k
            MI->flat_insn->detail->x86
1066
2.09k
              .operands[MI->flat_insn
1067
2.09k
                    ->detail
1068
2.09k
                    ->x86
1069
2.09k
                    .op_count]
1070
2.09k
              .size =
1071
2.09k
              MI->flat_insn->detail
1072
2.09k
                ->x86
1073
2.09k
                .operands[0]
1074
2.09k
                .size;
1075
2.09k
          } else
1076
0
            MI->flat_insn->detail->x86
1077
0
              .operands[MI->flat_insn
1078
0
                    ->detail
1079
0
                    ->x86
1080
0
                    .op_count]
1081
0
              .size = MI->imm_size;
1082
2.09k
        } else
1083
8.48k
          MI->flat_insn->detail->x86
1084
8.48k
            .operands[MI->flat_insn->detail
1085
8.48k
                  ->x86.op_count]
1086
8.48k
            .size = MI->imm_size;
1087
66.0k
        MI->flat_insn->detail->x86
1088
66.0k
          .operands[MI->flat_insn->detail->x86
1089
66.0k
                .op_count]
1090
66.0k
          .imm = imm;
1091
1092
66.0k
#ifndef CAPSTONE_DIET
1093
66.0k
        get_op_access(
1094
66.0k
          MI->csh, MCInst_getOpcode(MI), access,
1095
66.0k
          &MI->flat_insn->detail->x86.eflags);
1096
66.0k
        MI->flat_insn->detail->x86
1097
66.0k
          .operands[MI->flat_insn->detail->x86
1098
66.0k
                .op_count]
1099
66.0k
          .access =
1100
66.0k
          access[MI->flat_insn->detail->x86
1101
66.0k
                   .op_count];
1102
66.0k
#endif
1103
1104
66.0k
        MI->flat_insn->detail->x86.op_count++;
1105
66.0k
      }
1106
66.0k
    }
1107
66.0k
  }
1108
486k
}
1109
1110
static void printMemReference(MCInst *MI, unsigned Op, SStream *O)
1111
198k
{
1112
198k
  bool NeedPlus = false;
1113
198k
  MCOperand *BaseReg = MCInst_getOperand(MI, Op + X86_AddrBaseReg);
1114
198k
  uint64_t ScaleVal =
1115
198k
    MCOperand_getImm(MCInst_getOperand(MI, Op + X86_AddrScaleAmt));
1116
198k
  MCOperand *IndexReg = MCInst_getOperand(MI, Op + X86_AddrIndexReg);
1117
198k
  MCOperand *DispSpec = MCInst_getOperand(MI, Op + X86_AddrDisp);
1118
198k
  MCOperand *SegReg = MCInst_getOperand(MI, Op + X86_AddrSegmentReg);
1119
198k
  int reg;
1120
1121
198k
  if (MI->csh->detail_opt) {
1122
198k
#ifndef CAPSTONE_DIET
1123
198k
    uint8_t access[CS_X86_MAXIMUM_OPERAND_SIZE];
1124
198k
#endif
1125
1126
198k
    MI->flat_insn->detail->x86
1127
198k
      .operands[MI->flat_insn->detail->x86.op_count]
1128
198k
      .type = X86_OP_MEM;
1129
198k
    MI->flat_insn->detail->x86
1130
198k
      .operands[MI->flat_insn->detail->x86.op_count]
1131
198k
      .size = MI->x86opsize;
1132
198k
    MI->flat_insn->detail->x86
1133
198k
      .operands[MI->flat_insn->detail->x86.op_count]
1134
198k
      .mem.segment = X86_REG_INVALID;
1135
198k
    MI->flat_insn->detail->x86
1136
198k
      .operands[MI->flat_insn->detail->x86.op_count]
1137
198k
      .mem.base = X86_register_map(MCOperand_getReg(BaseReg));
1138
198k
    if (MCOperand_getReg(IndexReg) != X86_EIZ) {
1139
197k
      MI->flat_insn->detail->x86
1140
197k
        .operands[MI->flat_insn->detail->x86.op_count]
1141
197k
        .mem.index =
1142
197k
        X86_register_map(MCOperand_getReg(IndexReg));
1143
197k
    }
1144
198k
    MI->flat_insn->detail->x86
1145
198k
      .operands[MI->flat_insn->detail->x86.op_count]
1146
198k
      .mem.scale = (int)ScaleVal;
1147
198k
    MI->flat_insn->detail->x86
1148
198k
      .operands[MI->flat_insn->detail->x86.op_count]
1149
198k
      .mem.disp = 0;
1150
1151
198k
#ifndef CAPSTONE_DIET
1152
198k
    get_op_access(MI->csh, MCInst_getOpcode(MI), access,
1153
198k
            &MI->flat_insn->detail->x86.eflags);
1154
198k
    MI->flat_insn->detail->x86
1155
198k
      .operands[MI->flat_insn->detail->x86.op_count]
1156
198k
      .access = access[MI->flat_insn->detail->x86.op_count];
1157
198k
#endif
1158
198k
  }
1159
1160
  // If this has a segment register, print it.
1161
198k
  reg = MCOperand_getReg(SegReg);
1162
198k
  if (reg) {
1163
3.45k
    _printOperand(MI, Op + X86_AddrSegmentReg, O);
1164
3.45k
    if (MI->csh->detail_opt) {
1165
3.45k
      MI->flat_insn->detail->x86
1166
3.45k
        .operands[MI->flat_insn->detail->x86.op_count]
1167
3.45k
        .mem.segment = X86_register_map(reg);
1168
3.45k
    }
1169
3.45k
    SStream_concat0(O, ":");
1170
3.45k
  }
1171
1172
198k
  SStream_concat0(O, "[");
1173
1174
198k
  if (MCOperand_getReg(BaseReg)) {
1175
194k
    _printOperand(MI, Op + X86_AddrBaseReg, O);
1176
194k
    NeedPlus = true;
1177
194k
  }
1178
1179
198k
  if (MCOperand_getReg(IndexReg) &&
1180
47.8k
      MCOperand_getReg(IndexReg) != X86_EIZ) {
1181
46.7k
    if (NeedPlus)
1182
46.0k
      SStream_concat0(O, " + ");
1183
46.7k
    _printOperand(MI, Op + X86_AddrIndexReg, O);
1184
46.7k
    if (ScaleVal != 1)
1185
6.62k
      SStream_concat(O, "*%" PRIu64, ScaleVal);
1186
46.7k
    NeedPlus = true;
1187
46.7k
  }
1188
1189
198k
  if (MCOperand_isImm(DispSpec)) {
1190
198k
    int64_t DispVal = MCOperand_getImm(DispSpec);
1191
198k
    if (MI->csh->detail_opt)
1192
198k
      MI->flat_insn->detail->x86
1193
198k
        .operands[MI->flat_insn->detail->x86.op_count]
1194
198k
        .mem.disp = DispVal;
1195
198k
    if (DispVal) {
1196
63.5k
      if (NeedPlus) {
1197
60.4k
        if (DispVal < 0) {
1198
24.1k
          SStream_concat0(O, " - ");
1199
24.1k
          printImm(MI, O, -DispVal, true);
1200
36.2k
        } else {
1201
36.2k
          SStream_concat0(O, " + ");
1202
36.2k
          printImm(MI, O, DispVal, true);
1203
36.2k
        }
1204
60.4k
      } else {
1205
        // memory reference to an immediate address
1206
3.13k
        if (MI->csh->mode == CS_MODE_64)
1207
76
          MI->op1_size = 8;
1208
3.13k
        if (DispVal < 0) {
1209
1.34k
          printImm(MI, O,
1210
1.34k
             arch_masks[MI->csh->mode] &
1211
1.34k
               DispVal,
1212
1.34k
             true);
1213
1.79k
        } else {
1214
1.79k
          printImm(MI, O, DispVal, true);
1215
1.79k
        }
1216
3.13k
      }
1217
1218
134k
    } else {
1219
      // DispVal = 0
1220
134k
      if (!NeedPlus) // [0]
1221
160
        SStream_concat0(O, "0");
1222
134k
    }
1223
198k
  }
1224
1225
198k
  SStream_concat0(O, "]");
1226
1227
198k
  if (MI->csh->detail_opt)
1228
198k
    MI->flat_insn->detail->x86.op_count++;
1229
1230
198k
  if (MI->op1_size == 0)
1231
139k
    MI->op1_size = MI->x86opsize;
1232
198k
}
1233
1234
static void printanymem(MCInst *MI, unsigned OpNo, SStream *O)
1235
3.06k
{
1236
3.06k
  switch (MI->Opcode) {
1237
78
  default:
1238
78
    break;
1239
327
  case X86_LEA16r:
1240
327
    MI->x86opsize = 2;
1241
327
    break;
1242
437
  case X86_LEA32r:
1243
867
  case X86_LEA64_32r:
1244
867
    MI->x86opsize = 4;
1245
867
    break;
1246
274
  case X86_LEA64r:
1247
274
    MI->x86opsize = 8;
1248
274
    break;
1249
0
#ifndef CAPSTONE_X86_REDUCE
1250
424
  case X86_BNDCL32rm:
1251
585
  case X86_BNDCN32rm:
1252
679
  case X86_BNDCU32rm:
1253
791
  case X86_BNDSTXmr:
1254
1.01k
  case X86_BNDLDXrm:
1255
1.08k
  case X86_BNDCL64rm:
1256
1.43k
  case X86_BNDCN64rm:
1257
1.52k
  case X86_BNDCU64rm:
1258
1.52k
    MI->x86opsize = 16;
1259
1.52k
    break;
1260
3.06k
#endif
1261
3.06k
  }
1262
1263
3.06k
  printMemReference(MI, OpNo, O);
1264
3.06k
}
1265
1266
#ifdef CAPSTONE_X86_REDUCE
1267
#include "X86GenAsmWriter1_reduce.inc"
1268
#else
1269
#include "X86GenAsmWriter1.inc"
1270
#endif
1271
1272
#include "X86GenRegisterName1.inc"
1273
1274
#endif