Coverage Report

Created: 2026-04-12 06:30

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