Coverage Report

Created: 2025-08-29 06:29

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