Coverage Report

Created: 2026-09-01 07:20

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