Coverage Report

Created: 2026-08-31 06:58

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