Coverage Report

Created: 2026-01-12 07:13

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