Coverage Report

Created: 2025-12-05 06:11

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