Coverage Report

Created: 2026-09-28 06:34

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