Coverage Report

Created: 2026-06-15 06:41

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