Coverage Report

Created: 2025-10-14 06:42

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