Coverage Report

Created: 2026-01-10 06:34

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