Coverage Report

Created: 2025-11-24 06:12

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