Coverage Report

Created: 2025-07-01 07:03

/src/capstonenext/arch/X86/X86ATTInstPrinter.c
Line
Count
Source (jump to first uncovered line)
1
//===-- X86ATTInstPrinter.cpp - AT&T 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 AT&T-style
11
// assembly.
12
//
13
//===----------------------------------------------------------------------===//
14
15
/* Capstone Disassembly Engine */
16
/* By Nguyen Anh Quynh <aquynh@gmail.com>, 2013-2019 */
17
18
// this code is only relevant when DIET mode is disable
19
#if defined(CAPSTONE_HAS_X86) && !defined(CAPSTONE_DIET) && !defined(CAPSTONE_X86_ATT_DISABLE)
20
21
#ifdef _MSC_VER
22
#pragma warning(disable:4996)     // disable MSVC's warning on strncpy()
23
#pragma warning(disable:28719)    // disable MSVC's warning on strncpy()
24
#endif
25
26
#if !defined(CAPSTONE_HAS_OSXKERNEL)
27
#include <ctype.h>
28
#endif
29
#include <capstone/platform.h>
30
31
#if defined(CAPSTONE_HAS_OSXKERNEL)
32
#include <Availability.h>
33
#include <libkern/libkern.h>
34
#else
35
#include <stdio.h>
36
#include <stdlib.h>
37
#endif
38
39
#include <string.h>
40
41
#include "../../utils.h"
42
#include "../../MCInst.h"
43
#include "../../SStream.h"
44
#include "../../MCRegisterInfo.h"
45
#include "X86Mapping.h"
46
#include "X86BaseInfo.h"
47
#include "X86InstPrinterCommon.h"
48
49
#define GET_INSTRINFO_ENUM
50
#ifdef CAPSTONE_X86_REDUCE
51
#include "X86GenInstrInfo_reduce.inc"
52
#else
53
#include "X86GenInstrInfo.inc"
54
#endif
55
56
#define GET_REGINFO_ENUM
57
#include "X86GenRegisterInfo.inc"
58
59
static void printMemReference(MCInst *MI, unsigned Op, SStream *O);
60
static void printOperand(MCInst *MI, unsigned OpNo, SStream *O);
61
62
63
static void set_mem_access(MCInst *MI, bool status)
64
71.9k
{
65
71.9k
  if (MI->csh->detail_opt != CS_OPT_ON)
66
0
    return;
67
68
71.9k
  MI->csh->doing_mem = status;
69
71.9k
  if (!status)
70
    // done, create the next operand slot
71
35.9k
    MI->flat_insn->detail->x86.op_count++;
72
71.9k
}
73
74
static void printopaquemem(MCInst *MI, unsigned OpNo, SStream *O)
75
4.02k
{
76
4.02k
  switch(MI->csh->mode) {
77
1.44k
    case CS_MODE_16:
78
1.44k
      switch(MI->flat_insn->id) {
79
690
        default:
80
690
          MI->x86opsize = 2;
81
690
          break;
82
140
        case X86_INS_LJMP:
83
234
        case X86_INS_LCALL:
84
234
          MI->x86opsize = 4;
85
234
          break;
86
84
        case X86_INS_SGDT:
87
244
        case X86_INS_SIDT:
88
413
        case X86_INS_LGDT:
89
522
        case X86_INS_LIDT:
90
522
          MI->x86opsize = 6;
91
522
          break;
92
1.44k
      }
93
1.44k
      break;
94
1.44k
    case CS_MODE_32:
95
1.11k
      switch(MI->flat_insn->id) {
96
275
        default:
97
275
          MI->x86opsize = 4;
98
275
          break;
99
111
        case X86_INS_LJMP:
100
212
        case X86_INS_JMP:
101
278
        case X86_INS_LCALL:
102
355
        case X86_INS_SGDT:
103
463
        case X86_INS_SIDT:
104
636
        case X86_INS_LGDT:
105
837
        case X86_INS_LIDT:
106
837
          MI->x86opsize = 6;
107
837
          break;
108
1.11k
      }
109
1.11k
      break;
110
1.47k
    case CS_MODE_64:
111
1.47k
      switch(MI->flat_insn->id) {
112
534
        default:
113
534
          MI->x86opsize = 8;
114
534
          break;
115
279
        case X86_INS_LJMP:
116
458
        case X86_INS_LCALL:
117
672
        case X86_INS_SGDT:
118
707
        case X86_INS_SIDT:
119
804
        case X86_INS_LGDT:
120
936
        case X86_INS_LIDT:
121
936
          MI->x86opsize = 10;
122
936
          break;
123
1.47k
      }
124
1.47k
      break;
125
1.47k
    default:  // never reach
126
0
      break;
127
4.02k
  }
128
129
4.02k
  printMemReference(MI, OpNo, O);
130
4.02k
}
131
132
static void printi8mem(MCInst *MI, unsigned OpNo, SStream *O)
133
54.7k
{
134
54.7k
  MI->x86opsize = 1;
135
54.7k
  printMemReference(MI, OpNo, O);
136
54.7k
}
137
138
static void printi16mem(MCInst *MI, unsigned OpNo, SStream *O)
139
12.8k
{
140
12.8k
  MI->x86opsize = 2;
141
142
12.8k
  printMemReference(MI, OpNo, O);
143
12.8k
}
144
145
static void printi32mem(MCInst *MI, unsigned OpNo, SStream *O)
146
17.1k
{
147
17.1k
  MI->x86opsize = 4;
148
149
17.1k
  printMemReference(MI, OpNo, O);
150
17.1k
}
151
152
static void printi64mem(MCInst *MI, unsigned OpNo, SStream *O)
153
6.02k
{
154
6.02k
  MI->x86opsize = 8;
155
6.02k
  printMemReference(MI, OpNo, O);
156
6.02k
}
157
158
static void printi128mem(MCInst *MI, unsigned OpNo, SStream *O)
159
1.98k
{
160
1.98k
  MI->x86opsize = 16;
161
1.98k
  printMemReference(MI, OpNo, O);
162
1.98k
}
163
164
static void printi512mem(MCInst *MI, unsigned OpNo, SStream *O)
165
1.06k
{
166
1.06k
  MI->x86opsize = 64;
167
1.06k
  printMemReference(MI, OpNo, O);
168
1.06k
}
169
170
#ifndef CAPSTONE_X86_REDUCE
171
static void printi256mem(MCInst *MI, unsigned OpNo, SStream *O)
172
1.19k
{
173
1.19k
  MI->x86opsize = 32;
174
1.19k
  printMemReference(MI, OpNo, O);
175
1.19k
}
176
177
static void printf32mem(MCInst *MI, unsigned OpNo, SStream *O)
178
1.92k
{
179
1.92k
  switch(MCInst_getOpcode(MI)) {
180
1.42k
    default:
181
1.42k
      MI->x86opsize = 4;
182
1.42k
      break;
183
263
    case X86_FSTENVm:
184
495
    case X86_FLDENVm:
185
      // TODO: fix this in tablegen instead
186
495
      switch(MI->csh->mode) {
187
0
        default:    // never reach
188
0
          break;
189
92
        case CS_MODE_16:
190
92
          MI->x86opsize = 14;
191
92
          break;
192
207
        case CS_MODE_32:
193
403
        case CS_MODE_64:
194
403
          MI->x86opsize = 28;
195
403
          break;
196
495
      }
197
495
      break;
198
1.92k
  }
199
200
1.92k
  printMemReference(MI, OpNo, O);
201
1.92k
}
202
203
static void printf64mem(MCInst *MI, unsigned OpNo, SStream *O)
204
1.82k
{
205
1.82k
  MI->x86opsize = 8;
206
1.82k
  printMemReference(MI, OpNo, O);
207
1.82k
}
208
209
static void printf80mem(MCInst *MI, unsigned OpNo, SStream *O)
210
165
{
211
165
  MI->x86opsize = 10;
212
165
  printMemReference(MI, OpNo, O);
213
165
}
214
215
static void printf128mem(MCInst *MI, unsigned OpNo, SStream *O)
216
1.48k
{
217
1.48k
  MI->x86opsize = 16;
218
1.48k
  printMemReference(MI, OpNo, O);
219
1.48k
}
220
221
static void printf256mem(MCInst *MI, unsigned OpNo, SStream *O)
222
871
{
223
871
  MI->x86opsize = 32;
224
871
  printMemReference(MI, OpNo, O);
225
871
}
226
227
static void printf512mem(MCInst *MI, unsigned OpNo, SStream *O)
228
646
{
229
646
  MI->x86opsize = 64;
230
646
  printMemReference(MI, OpNo, O);
231
646
}
232
233
#endif
234
235
static void printRegName(SStream *OS, unsigned RegNo);
236
237
// local printOperand, without updating public operands
238
static void _printOperand(MCInst *MI, unsigned OpNo, SStream *O)
239
145k
{
240
145k
  MCOperand *Op  = MCInst_getOperand(MI, OpNo);
241
145k
  if (MCOperand_isReg(Op)) {
242
145k
    printRegName(O, MCOperand_getReg(Op));
243
145k
  } else if (MCOperand_isImm(Op)) {
244
0
    uint8_t encsize;
245
0
    uint8_t opsize = X86_immediate_size(MCInst_getOpcode(MI), &encsize);
246
247
    // Print X86 immediates as signed values.
248
0
    int64_t imm = MCOperand_getImm(Op);
249
0
    if (imm < 0) {
250
0
      if (MI->csh->imm_unsigned) {
251
0
        if (opsize) {
252
0
          switch(opsize) {
253
0
            default:
254
0
              break;
255
0
            case 1:
256
0
              imm &= 0xff;
257
0
              break;
258
0
            case 2:
259
0
              imm &= 0xffff;
260
0
              break;
261
0
            case 4:
262
0
              imm &= 0xffffffff;
263
0
              break;
264
0
          }
265
0
        }
266
267
0
        SStream_concat(O, "$0x%"PRIx64, imm);
268
0
      } else {
269
0
        if (imm < -HEX_THRESHOLD)
270
0
          SStream_concat(O, "$-0x%"PRIx64, -imm);
271
0
        else
272
0
          SStream_concat(O, "$-%"PRIu64, -imm);
273
0
      }
274
0
    } else {
275
0
      if (imm > HEX_THRESHOLD)
276
0
        SStream_concat(O, "$0x%"PRIx64, imm);
277
0
      else
278
0
        SStream_concat(O, "$%"PRIu64, imm);
279
0
    }
280
0
  }
281
145k
}
282
283
// convert Intel access info to AT&T access info
284
static void get_op_access(cs_struct *h, unsigned int id, uint8_t *access, uint64_t *eflags)
285
636k
{
286
636k
  uint8_t count, i;
287
636k
  const uint8_t *arr = X86_get_op_access(h, id, eflags);
288
289
  // initialize access
290
636k
  memset(access, 0, CS_X86_MAXIMUM_OPERAND_SIZE * sizeof(access[0]));
291
636k
  if (!arr) {
292
0
    return;
293
0
  }
294
295
  // find the non-zero last entry
296
1.80M
  for(count = 0; arr[count]; count++);
297
298
636k
  if (count == 0)
299
36.1k
    return;
300
301
  // copy in reverse order this access array from Intel syntax -> AT&T syntax
302
600k
  count--;
303
1.76M
  for(i = 0; i <= count && ((count - i) < CS_X86_MAXIMUM_OPERAND_SIZE) && i < CS_X86_MAXIMUM_OPERAND_SIZE; i++) {
304
1.16M
    if (arr[count - i] != CS_AC_IGNORE)
305
1.01M
      access[i] = arr[count - i];
306
155k
    else
307
155k
      access[i] = 0;
308
1.16M
  }
309
600k
}
310
311
static void printSrcIdx(MCInst *MI, unsigned Op, SStream *O)
312
17.9k
{
313
17.9k
  MCOperand *SegReg;
314
17.9k
  int reg;
315
316
17.9k
  if (MI->csh->detail_opt) {
317
17.9k
    uint8_t access[CS_X86_MAXIMUM_OPERAND_SIZE];
318
319
17.9k
    MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].type = X86_OP_MEM;
320
17.9k
    MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].size = MI->x86opsize;
321
17.9k
    MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].mem.segment = X86_REG_INVALID;
322
17.9k
    MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].mem.base = X86_REG_INVALID;
323
17.9k
    MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].mem.index = X86_REG_INVALID;
324
17.9k
    MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].mem.scale = 1;
325
17.9k
    MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].mem.disp = 0;
326
327
17.9k
    get_op_access(MI->csh, MCInst_getOpcode(MI), access, &MI->flat_insn->detail->x86.eflags);
328
17.9k
    MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].access = access[MI->flat_insn->detail->x86.op_count];
329
17.9k
  }
330
331
17.9k
  SegReg = MCInst_getOperand(MI, Op+1);
332
17.9k
  reg = MCOperand_getReg(SegReg);
333
  // If this has a segment register, print it.
334
17.9k
  if (reg) {
335
237
    _printOperand(MI, Op + 1, O);
336
237
    SStream_concat0(O, ":");
337
338
237
    if (MI->csh->detail_opt) {
339
237
      MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].mem.segment = X86_register_map(reg);
340
237
    }
341
237
  }
342
343
17.9k
  SStream_concat0(O, "(");
344
17.9k
  set_mem_access(MI, true);
345
346
17.9k
  printOperand(MI, Op, O);
347
348
17.9k
  SStream_concat0(O, ")");
349
17.9k
  set_mem_access(MI, false);
350
17.9k
}
351
352
static void printDstIdx(MCInst *MI, unsigned Op, SStream *O)
353
18.0k
{
354
18.0k
  if (MI->csh->detail_opt) {
355
18.0k
    uint8_t access[CS_X86_MAXIMUM_OPERAND_SIZE];
356
357
18.0k
    MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].type = X86_OP_MEM;
358
18.0k
    MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].size = MI->x86opsize;
359
18.0k
    MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].mem.segment = X86_REG_INVALID;
360
18.0k
    MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].mem.base = X86_REG_INVALID;
361
18.0k
    MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].mem.index = X86_REG_INVALID;
362
18.0k
    MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].mem.scale = 1;
363
18.0k
    MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].mem.disp = 0;
364
365
18.0k
    get_op_access(MI->csh, MCInst_getOpcode(MI), access, &MI->flat_insn->detail->x86.eflags);
366
18.0k
    MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].access = access[MI->flat_insn->detail->x86.op_count];
367
18.0k
  }
368
369
  // DI accesses are always ES-based on non-64bit mode
370
18.0k
  if (MI->csh->mode != CS_MODE_64) {
371
9.76k
    SStream_concat0(O, "%es:(");
372
9.76k
    if (MI->csh->detail_opt) {
373
9.76k
      MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].mem.segment = X86_REG_ES;
374
9.76k
    }
375
9.76k
  } else
376
8.28k
    SStream_concat0(O, "(");
377
378
18.0k
  set_mem_access(MI, true);
379
380
18.0k
  printOperand(MI, Op, O);
381
382
18.0k
  SStream_concat0(O, ")");
383
18.0k
  set_mem_access(MI, false);
384
18.0k
}
385
386
static void printSrcIdx8(MCInst *MI, unsigned OpNo, SStream *O)
387
5.99k
{
388
5.99k
  MI->x86opsize = 1;
389
5.99k
  printSrcIdx(MI, OpNo, O);
390
5.99k
}
391
392
static void printSrcIdx16(MCInst *MI, unsigned OpNo, SStream *O)
393
2.48k
{
394
2.48k
  MI->x86opsize = 2;
395
2.48k
  printSrcIdx(MI, OpNo, O);
396
2.48k
}
397
398
static void printSrcIdx32(MCInst *MI, unsigned OpNo, SStream *O)
399
8.30k
{
400
8.30k
  MI->x86opsize = 4;
401
8.30k
  printSrcIdx(MI, OpNo, O);
402
8.30k
}
403
404
static void printSrcIdx64(MCInst *MI, unsigned OpNo, SStream *O)
405
1.12k
{
406
1.12k
  MI->x86opsize = 8;
407
1.12k
  printSrcIdx(MI, OpNo, O);
408
1.12k
}
409
410
static void printDstIdx8(MCInst *MI, unsigned OpNo, SStream *O)
411
6.89k
{
412
6.89k
  MI->x86opsize = 1;
413
6.89k
  printDstIdx(MI, OpNo, O);
414
6.89k
}
415
416
static void printDstIdx16(MCInst *MI, unsigned OpNo, SStream *O)
417
3.77k
{
418
3.77k
  MI->x86opsize = 2;
419
3.77k
  printDstIdx(MI, OpNo, O);
420
3.77k
}
421
422
static void printDstIdx32(MCInst *MI, unsigned OpNo, SStream *O)
423
5.82k
{
424
5.82k
  MI->x86opsize = 4;
425
5.82k
  printDstIdx(MI, OpNo, O);
426
5.82k
}
427
428
static void printDstIdx64(MCInst *MI, unsigned OpNo, SStream *O)
429
1.54k
{
430
1.54k
  MI->x86opsize = 8;
431
1.54k
  printDstIdx(MI, OpNo, O);
432
1.54k
}
433
434
static void printMemOffset(MCInst *MI, unsigned Op, SStream *O)
435
3.26k
{
436
3.26k
  MCOperand *DispSpec = MCInst_getOperand(MI, Op);
437
3.26k
  MCOperand *SegReg = MCInst_getOperand(MI, Op+1);
438
3.26k
  int reg;
439
440
3.26k
  if (MI->csh->detail_opt) {
441
3.26k
    uint8_t access[CS_X86_MAXIMUM_OPERAND_SIZE];
442
443
3.26k
    MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].type = X86_OP_MEM;
444
3.26k
    MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].size = MI->x86opsize;
445
3.26k
    MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].mem.segment = X86_REG_INVALID;
446
3.26k
    MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].mem.base = X86_REG_INVALID;
447
3.26k
    MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].mem.index = X86_REG_INVALID;
448
3.26k
    MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].mem.scale = 1;
449
3.26k
    MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].mem.disp = 0;
450
451
3.26k
    get_op_access(MI->csh, MCInst_getOpcode(MI), access, &MI->flat_insn->detail->x86.eflags);
452
3.26k
    MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].access = access[MI->flat_insn->detail->x86.op_count];
453
3.26k
  }
454
455
  // If this has a segment register, print it.
456
3.26k
  reg = MCOperand_getReg(SegReg);
457
3.26k
  if (reg) {
458
535
    _printOperand(MI, Op + 1, O);
459
535
    SStream_concat0(O, ":");
460
461
535
    if (MI->csh->detail_opt) {
462
535
      MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].mem.segment = X86_register_map(reg);
463
535
    }
464
535
  }
465
466
3.26k
  if (MCOperand_isImm(DispSpec)) {
467
3.26k
    int64_t imm = MCOperand_getImm(DispSpec);
468
3.26k
    if (MI->csh->detail_opt)
469
3.26k
      MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].mem.disp = imm;
470
3.26k
    if (imm < 0) {
471
545
      SStream_concat(O, "0x%"PRIx64, arch_masks[MI->csh->mode] & imm);
472
2.71k
    } else {
473
2.71k
      if (imm > HEX_THRESHOLD)
474
2.61k
        SStream_concat(O, "0x%"PRIx64, imm);
475
99
      else
476
99
        SStream_concat(O, "%"PRIu64, imm);
477
2.71k
    }
478
3.26k
  }
479
480
3.26k
  if (MI->csh->detail_opt)
481
3.26k
    MI->flat_insn->detail->x86.op_count++;
482
3.26k
}
483
484
static void printU8Imm(MCInst *MI, unsigned Op, SStream *O)
485
11.7k
{
486
11.7k
  uint8_t val = MCOperand_getImm(MCInst_getOperand(MI, Op)) & 0xff;
487
488
11.7k
  if (val > HEX_THRESHOLD)
489
10.7k
    SStream_concat(O, "$0x%x", val);
490
1.06k
  else
491
1.06k
    SStream_concat(O, "$%u", val);
492
493
11.7k
  if (MI->csh->detail_opt) {
494
11.7k
    MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].type = X86_OP_IMM;
495
11.7k
    MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].imm = val;
496
11.7k
    MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].size = 1;
497
11.7k
    MI->flat_insn->detail->x86.op_count++;
498
11.7k
  }
499
11.7k
}
500
501
static void printMemOffs8(MCInst *MI, unsigned OpNo, SStream *O)
502
1.60k
{
503
1.60k
  MI->x86opsize = 1;
504
1.60k
  printMemOffset(MI, OpNo, O);
505
1.60k
}
506
507
static void printMemOffs16(MCInst *MI, unsigned OpNo, SStream *O)
508
899
{
509
899
  MI->x86opsize = 2;
510
899
  printMemOffset(MI, OpNo, O);
511
899
}
512
513
static void printMemOffs32(MCInst *MI, unsigned OpNo, SStream *O)
514
695
{
515
695
  MI->x86opsize = 4;
516
695
  printMemOffset(MI, OpNo, O);
517
695
}
518
519
static void printMemOffs64(MCInst *MI, unsigned OpNo, SStream *O)
520
61
{
521
61
  MI->x86opsize = 8;
522
61
  printMemOffset(MI, OpNo, O);
523
61
}
524
525
/// printPCRelImm - This is used to print an immediate value that ends up
526
/// being encoded as a pc-relative value (e.g. for jumps and calls).  These
527
/// print slightly differently than normal immediates.  For example, a $ is not
528
/// emitted.
529
static void printPCRelImm(MCInst *MI, unsigned OpNo, SStream *O)
530
21.6k
{
531
21.6k
  MCOperand *Op = MCInst_getOperand(MI, OpNo);
532
21.6k
  if (MCOperand_isImm(Op)) {
533
21.6k
    int64_t imm = MCOperand_getImm(Op) + MI->flat_insn->size + MI->address;
534
535
    // truncate imm for non-64bit
536
21.6k
    if (MI->csh->mode != CS_MODE_64) {
537
13.9k
      imm = imm & 0xffffffff;
538
13.9k
    }
539
540
21.6k
    if (imm < 0) {
541
539
      SStream_concat(O, "0x%"PRIx64, imm);
542
21.1k
    } else {
543
21.1k
      if (imm > HEX_THRESHOLD)
544
21.0k
        SStream_concat(O, "0x%"PRIx64, imm);
545
17
      else
546
17
        SStream_concat(O, "%"PRIu64, imm);
547
21.1k
    }
548
21.6k
    if (MI->csh->detail_opt) {
549
21.6k
      MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].type = X86_OP_IMM;
550
21.6k
      MI->has_imm = true;
551
21.6k
      MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].imm = imm;
552
21.6k
      MI->flat_insn->detail->x86.op_count++;
553
21.6k
    }
554
21.6k
  }
555
21.6k
}
556
557
static void printOperand(MCInst *MI, unsigned OpNo, SStream *O)
558
264k
{
559
264k
  MCOperand *Op  = MCInst_getOperand(MI, OpNo);
560
264k
  if (MCOperand_isReg(Op)) {
561
233k
    unsigned int reg = MCOperand_getReg(Op);
562
233k
    printRegName(O, reg);
563
233k
    if (MI->csh->detail_opt) {
564
233k
      if (MI->csh->doing_mem) {
565
35.9k
        MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].mem.base = X86_register_map(reg);
566
197k
      } else {
567
197k
        uint8_t access[CS_X86_MAXIMUM_OPERAND_SIZE];
568
569
197k
        MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].type = X86_OP_REG;
570
197k
        MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].reg = X86_register_map(reg);
571
197k
        MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].size = MI->csh->regsize_map[X86_register_map(reg)];
572
573
197k
        get_op_access(MI->csh, MCInst_getOpcode(MI), access, &MI->flat_insn->detail->x86.eflags);
574
197k
        MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].access = access[MI->flat_insn->detail->x86.op_count];
575
576
197k
        MI->flat_insn->detail->x86.op_count++;
577
197k
      }
578
233k
    }
579
233k
  } else if (MCOperand_isImm(Op)) {
580
    // Print X86 immediates as signed values.
581
31.5k
    uint8_t encsize;
582
31.5k
    int64_t imm = MCOperand_getImm(Op);
583
31.5k
    uint8_t opsize = X86_immediate_size(MCInst_getOpcode(MI), &encsize);
584
585
31.5k
    if (opsize == 1) {   // print 1 byte immediate in positive form
586
14.5k
      imm = imm & 0xff;
587
14.5k
    }
588
589
31.5k
    switch(MI->flat_insn->id) {
590
13.9k
      default:
591
13.9k
        if (imm >= 0) {
592
12.7k
          if (imm > HEX_THRESHOLD)
593
11.3k
            SStream_concat(O, "$0x%"PRIx64, imm);
594
1.46k
          else
595
1.46k
            SStream_concat(O, "$%"PRIu64, imm);
596
12.7k
        } else {
597
1.15k
          if (MI->csh->imm_unsigned) {
598
0
            if (opsize) {
599
0
              switch(opsize) {
600
0
                default:
601
0
                  break;
602
                // case 1 cannot occur because above imm was ANDed with 0xff,
603
                // making it effectively always positive.
604
                // So this switch is never reached.
605
0
                case 2:
606
0
                  imm &= 0xffff;
607
0
                  break;
608
0
                case 4:
609
0
                  imm &= 0xffffffff;
610
0
                  break;
611
0
              }
612
0
            }
613
614
0
            SStream_concat(O, "$0x%"PRIx64, imm);
615
1.15k
          } else {
616
1.15k
            if (imm == 0x8000000000000000LL)  // imm == -imm
617
0
              SStream_concat0(O, "$0x8000000000000000");
618
1.15k
            else if (imm < -HEX_THRESHOLD)
619
1.04k
              SStream_concat(O, "$-0x%"PRIx64, -imm);
620
106
            else
621
106
              SStream_concat(O, "$-%"PRIu64, -imm);
622
1.15k
          }
623
1.15k
        }
624
13.9k
        break;
625
626
13.9k
      case X86_INS_MOVABS:
627
6.54k
      case X86_INS_MOV:
628
        // do not print number in negative form
629
6.54k
        if (imm > HEX_THRESHOLD)
630
6.04k
          SStream_concat(O, "$0x%"PRIx64, imm);
631
501
        else
632
501
          SStream_concat(O, "$%"PRIu64, imm);
633
6.54k
        break;
634
635
0
      case X86_INS_IN:
636
0
      case X86_INS_OUT:
637
0
      case X86_INS_INT:
638
        // do not print number in negative form
639
0
        imm = imm & 0xff;
640
0
        if (imm >= 0 && imm <= HEX_THRESHOLD)
641
0
          SStream_concat(O, "$%u", imm);
642
0
        else {
643
0
          SStream_concat(O, "$0x%x", imm);
644
0
        }
645
0
        break;
646
647
884
      case X86_INS_LCALL:
648
1.90k
      case X86_INS_LJMP:
649
1.90k
      case X86_INS_JMP:
650
        // always print address in positive form
651
1.90k
        if (OpNo == 1) { // selector is ptr16
652
954
          imm = imm & 0xffff;
653
954
          opsize = 2;
654
954
        } else
655
954
          opsize = 4;
656
1.90k
        SStream_concat(O, "$0x%"PRIx64, imm);
657
1.90k
        break;
658
659
1.55k
      case X86_INS_AND:
660
3.85k
      case X86_INS_OR:
661
5.71k
      case X86_INS_XOR:
662
        // do not print number in negative form
663
5.71k
        if (imm >= 0 && imm <= HEX_THRESHOLD)
664
366
          SStream_concat(O, "$%u", imm);
665
5.34k
        else {
666
5.34k
          imm = arch_masks[opsize? opsize : MI->imm_size] & imm;
667
5.34k
          SStream_concat(O, "$0x%"PRIx64, imm);
668
5.34k
        }
669
5.71k
        break;
670
671
2.27k
      case X86_INS_RET:
672
3.39k
      case X86_INS_RETF:
673
        // RET imm16
674
3.39k
        if (imm >= 0 && imm <= HEX_THRESHOLD)
675
407
          SStream_concat(O, "$%u", imm);
676
2.98k
        else {
677
2.98k
          imm = 0xffff & imm;
678
2.98k
          SStream_concat(O, "$0x%x", imm);
679
2.98k
        }
680
3.39k
        break;
681
31.5k
    }
682
683
31.5k
    if (MI->csh->detail_opt) {
684
31.5k
      if (MI->csh->doing_mem) {
685
0
        MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].type = X86_OP_MEM;
686
0
        MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].mem.disp = imm;
687
31.5k
      } else {
688
31.5k
        MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].type = X86_OP_IMM;
689
31.5k
        MI->has_imm = true;
690
31.5k
        MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].imm = imm;
691
692
31.5k
        if (opsize > 0) {
693
26.5k
          MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].size = opsize;
694
26.5k
          MI->flat_insn->detail->x86.encoding.imm_size = encsize;
695
26.5k
        } else if (MI->op1_size > 0)
696
0
          MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].size = MI->op1_size;
697
4.91k
        else
698
4.91k
          MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].size = MI->imm_size;
699
700
31.5k
        MI->flat_insn->detail->x86.op_count++;
701
31.5k
      }
702
31.5k
    }
703
31.5k
  }
704
264k
}
705
706
static void printMemReference(MCInst *MI, unsigned Op, SStream *O)
707
109k
{
708
109k
  MCOperand *BaseReg  = MCInst_getOperand(MI, Op + X86_AddrBaseReg);
709
109k
  MCOperand *IndexReg  = MCInst_getOperand(MI, Op + X86_AddrIndexReg);
710
109k
  MCOperand *DispSpec = MCInst_getOperand(MI, Op + X86_AddrDisp);
711
109k
  MCOperand *SegReg = MCInst_getOperand(MI, Op + X86_AddrSegmentReg);
712
109k
  uint64_t ScaleVal;
713
109k
  int segreg;
714
109k
  int64_t DispVal = 1;
715
716
109k
  if (MI->csh->detail_opt) {
717
109k
    uint8_t access[CS_X86_MAXIMUM_OPERAND_SIZE];
718
719
109k
    MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].type = X86_OP_MEM;
720
109k
    MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].size = MI->x86opsize;
721
109k
    MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].mem.segment = X86_REG_INVALID;
722
109k
    MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].mem.base = X86_register_map(MCOperand_getReg(BaseReg));
723
109k
        if (MCOperand_getReg(IndexReg) != X86_EIZ) {
724
108k
            MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].mem.index = X86_register_map(MCOperand_getReg(IndexReg));
725
108k
        }
726
109k
    MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].mem.scale = 1;
727
109k
    MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].mem.disp = 0;
728
729
109k
    get_op_access(MI->csh, MCInst_getOpcode(MI), access, &MI->flat_insn->detail->x86.eflags);
730
109k
    MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].access = access[MI->flat_insn->detail->x86.op_count];
731
109k
  }
732
733
  // If this has a segment register, print it.
734
109k
  segreg = MCOperand_getReg(SegReg);
735
109k
  if (segreg) {
736
2.92k
    _printOperand(MI, Op + X86_AddrSegmentReg, O);
737
2.92k
    SStream_concat0(O, ":");
738
739
2.92k
    if (MI->csh->detail_opt) {
740
2.92k
      MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].mem.segment = X86_register_map(segreg);
741
2.92k
    }
742
2.92k
  }
743
744
109k
  if (MCOperand_isImm(DispSpec)) {
745
109k
    DispVal = MCOperand_getImm(DispSpec);
746
109k
    if (MI->csh->detail_opt)
747
109k
      MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].mem.disp = DispVal;
748
109k
    if (DispVal) {
749
34.1k
      if (MCOperand_getReg(IndexReg) || MCOperand_getReg(BaseReg)) {
750
32.1k
        printInt64(O, DispVal);
751
32.1k
      } else {
752
        // only immediate as address of memory
753
1.97k
        if (DispVal < 0) {
754
692
          SStream_concat(O, "0x%"PRIx64, arch_masks[MI->csh->mode] & DispVal);
755
1.27k
        } else {
756
1.27k
          if (DispVal > HEX_THRESHOLD)
757
1.22k
            SStream_concat(O, "0x%"PRIx64, DispVal);
758
55
          else
759
55
            SStream_concat(O, "%"PRIu64, DispVal);
760
1.27k
        }
761
1.97k
      }
762
34.1k
    }
763
109k
  }
764
765
109k
  if (MCOperand_getReg(IndexReg) || MCOperand_getReg(BaseReg)) {
766
106k
    SStream_concat0(O, "(");
767
768
106k
    if (MCOperand_getReg(BaseReg))
769
106k
      _printOperand(MI, Op + X86_AddrBaseReg, O);
770
771
106k
        if (MCOperand_getReg(IndexReg) && MCOperand_getReg(IndexReg) != X86_EIZ) {
772
34.7k
      SStream_concat0(O, ", ");
773
34.7k
      _printOperand(MI, Op + X86_AddrIndexReg, O);
774
34.7k
      ScaleVal = MCOperand_getImm(MCInst_getOperand(MI, Op + X86_AddrScaleAmt));
775
34.7k
      if (MI->csh->detail_opt)
776
34.7k
        MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count].mem.scale = (int)ScaleVal;
777
34.7k
      if (ScaleVal != 1) {
778
2.50k
        SStream_concat(O, ", %u", ScaleVal);
779
2.50k
      }
780
34.7k
    }
781
782
106k
    SStream_concat0(O, ")");
783
106k
  } else {
784
2.19k
    if (!DispVal)
785
228
      SStream_concat0(O, "0");
786
2.19k
  }
787
788
109k
  if (MI->csh->detail_opt)
789
109k
    MI->flat_insn->detail->x86.op_count++;
790
109k
}
791
792
static void printanymem(MCInst *MI, unsigned OpNo, SStream *O)
793
3.18k
{
794
3.18k
  switch(MI->Opcode) {
795
265
    default: break;
796
296
    case X86_LEA16r:
797
296
         MI->x86opsize = 2;
798
296
         break;
799
354
    case X86_LEA32r:
800
821
    case X86_LEA64_32r:
801
821
         MI->x86opsize = 4;
802
821
         break;
803
262
    case X86_LEA64r:
804
262
         MI->x86opsize = 8;
805
262
         break;
806
0
#ifndef CAPSTONE_X86_REDUCE
807
318
    case X86_BNDCL32rm:
808
626
    case X86_BNDCN32rm:
809
661
    case X86_BNDCU32rm:
810
865
    case X86_BNDSTXmr:
811
1.09k
    case X86_BNDLDXrm:
812
1.43k
    case X86_BNDCL64rm:
813
1.47k
    case X86_BNDCN64rm:
814
1.54k
    case X86_BNDCU64rm:
815
1.54k
         MI->x86opsize = 16;
816
1.54k
         break;
817
3.18k
#endif
818
3.18k
  }
819
820
3.18k
  printMemReference(MI, OpNo, O);
821
3.18k
}
822
823
#include "X86InstPrinter.h"
824
825
// Include the auto-generated portion of the assembly writer.
826
#ifdef CAPSTONE_X86_REDUCE
827
#include "X86GenAsmWriter_reduce.inc"
828
#else
829
#include "X86GenAsmWriter.inc"
830
#endif
831
832
#include "X86GenRegisterName.inc"
833
834
static void printRegName(SStream *OS, unsigned RegNo)
835
378k
{
836
378k
  SStream_concat(OS, "%%%s", getRegisterName(RegNo));
837
378k
}
838
839
void X86_ATT_printInst(MCInst *MI, SStream *OS, void *info)
840
290k
{
841
290k
  x86_reg reg, reg2;
842
290k
  enum cs_ac_type access1, access2;
843
290k
  int i;
844
845
  // perhaps this instruction does not need printer
846
290k
  if (MI->assembly[0]) {
847
0
    strncpy(OS->buffer, MI->assembly, sizeof(OS->buffer));
848
0
    return;
849
0
  }
850
851
  // Output CALLpcrel32 as "callq" in 64-bit mode.
852
  // In Intel annotation it's always emitted as "call".
853
  //
854
  // TODO: Probably this hack should be redesigned via InstAlias in
855
  // InstrInfo.td as soon as Requires clause is supported properly
856
  // for InstAlias.
857
290k
  if (MI->csh->mode == CS_MODE_64 && MCInst_getOpcode(MI) == X86_CALLpcrel32) {
858
0
    SStream_concat0(OS, "callq\t");
859
0
    MCInst_setOpcodePub(MI, X86_INS_CALL);
860
0
    printPCRelImm(MI, 0, OS);
861
0
    return;
862
0
  }
863
864
290k
  X86_lockrep(MI, OS);
865
290k
  printInstruction(MI, OS);
866
867
290k
  if (MI->has_imm) {
868
    // if op_count > 1, then this operand's size is taken from the destination op
869
51.8k
    if (MI->flat_insn->detail->x86.op_count > 1) {
870
24.7k
      if (MI->flat_insn->id != X86_INS_LCALL && MI->flat_insn->id != X86_INS_LJMP && MI->flat_insn->id != X86_INS_JMP) {
871
73.0k
        for (i = 0; i < MI->flat_insn->detail->x86.op_count; i++) {
872
49.2k
          if (MI->flat_insn->detail->x86.operands[i].type == X86_OP_IMM)
873
24.1k
            MI->flat_insn->detail->x86.operands[i].size =
874
24.1k
              MI->flat_insn->detail->x86.operands[MI->flat_insn->detail->x86.op_count - 1].size;
875
49.2k
        }
876
23.8k
      }
877
24.7k
    } else
878
27.0k
      MI->flat_insn->detail->x86.operands[0].size = MI->imm_size;
879
51.8k
  }
880
881
290k
  if (MI->csh->detail_opt) {
882
290k
    uint8_t access[CS_X86_MAXIMUM_OPERAND_SIZE] = {0};
883
884
    // some instructions need to supply immediate 1 in the first op
885
290k
    switch(MCInst_getOpcode(MI)) {
886
273k
      default:
887
273k
        break;
888
273k
      case X86_SHL8r1:
889
178
      case X86_SHL16r1:
890
306
      case X86_SHL32r1:
891
361
      case X86_SHL64r1:
892
469
      case X86_SAL8r1:
893
785
      case X86_SAL16r1:
894
1.22k
      case X86_SAL32r1:
895
1.27k
      case X86_SAL64r1:
896
1.42k
      case X86_SHR8r1:
897
1.60k
      case X86_SHR16r1:
898
1.89k
      case X86_SHR32r1:
899
2.00k
      case X86_SHR64r1:
900
2.30k
      case X86_SAR8r1:
901
2.35k
      case X86_SAR16r1:
902
2.54k
      case X86_SAR32r1:
903
2.65k
      case X86_SAR64r1:
904
4.58k
      case X86_RCL8r1:
905
5.45k
      case X86_RCL16r1:
906
7.10k
      case X86_RCL32r1:
907
7.46k
      case X86_RCL64r1:
908
7.72k
      case X86_RCR8r1:
909
7.83k
      case X86_RCR16r1:
910
7.90k
      case X86_RCR32r1:
911
8.03k
      case X86_RCR64r1:
912
8.27k
      case X86_ROL8r1:
913
8.42k
      case X86_ROL16r1:
914
8.79k
      case X86_ROL32r1:
915
9.11k
      case X86_ROL64r1:
916
9.31k
      case X86_ROR8r1:
917
9.67k
      case X86_ROR16r1:
918
9.95k
      case X86_ROR32r1:
919
10.1k
      case X86_ROR64r1:
920
10.3k
      case X86_SHL8m1:
921
10.7k
      case X86_SHL16m1:
922
11.1k
      case X86_SHL32m1:
923
11.2k
      case X86_SHL64m1:
924
11.5k
      case X86_SAL8m1:
925
11.7k
      case X86_SAL16m1:
926
11.8k
      case X86_SAL32m1:
927
11.9k
      case X86_SAL64m1:
928
12.1k
      case X86_SHR8m1:
929
12.4k
      case X86_SHR16m1:
930
12.7k
      case X86_SHR32m1:
931
12.8k
      case X86_SHR64m1:
932
12.9k
      case X86_SAR8m1:
933
13.0k
      case X86_SAR16m1:
934
13.6k
      case X86_SAR32m1:
935
13.6k
      case X86_SAR64m1:
936
14.2k
      case X86_RCL8m1:
937
14.4k
      case X86_RCL16m1:
938
14.7k
      case X86_RCL32m1:
939
14.9k
      case X86_RCL64m1:
940
15.0k
      case X86_RCR8m1:
941
15.2k
      case X86_RCR16m1:
942
15.3k
      case X86_RCR32m1:
943
15.3k
      case X86_RCR64m1:
944
15.5k
      case X86_ROL8m1:
945
15.9k
      case X86_ROL16m1:
946
16.4k
      case X86_ROL32m1:
947
16.5k
      case X86_ROL64m1:
948
16.8k
      case X86_ROR8m1:
949
16.9k
      case X86_ROR16m1:
950
17.5k
      case X86_ROR32m1:
951
17.7k
      case X86_ROR64m1:
952
        // shift all the ops right to leave 1st slot for this new register op
953
17.7k
        memmove(&(MI->flat_insn->detail->x86.operands[1]), &(MI->flat_insn->detail->x86.operands[0]),
954
17.7k
            sizeof(MI->flat_insn->detail->x86.operands[0]) * (ARR_SIZE(MI->flat_insn->detail->x86.operands) - 1));
955
17.7k
        MI->flat_insn->detail->x86.operands[0].type = X86_OP_IMM;
956
17.7k
        MI->flat_insn->detail->x86.operands[0].imm = 1;
957
17.7k
        MI->flat_insn->detail->x86.operands[0].size = 1;
958
17.7k
        MI->flat_insn->detail->x86.op_count++;
959
290k
    }
960
961
    // special instruction needs to supply register op
962
    // first op can be embedded in the asm by llvm.
963
    // so we have to add the missing register as the first operand
964
965
    //printf(">>> opcode = %u\n", MCInst_getOpcode(MI));
966
967
290k
    reg = X86_insn_reg_att(MCInst_getOpcode(MI), &access1);
968
290k
    if (reg) {
969
      // shift all the ops right to leave 1st slot for this new register op
970
17.0k
      memmove(&(MI->flat_insn->detail->x86.operands[1]), &(MI->flat_insn->detail->x86.operands[0]),
971
17.0k
          sizeof(MI->flat_insn->detail->x86.operands[0]) * (ARR_SIZE(MI->flat_insn->detail->x86.operands) - 1));
972
17.0k
      MI->flat_insn->detail->x86.operands[0].type = X86_OP_REG;
973
17.0k
      MI->flat_insn->detail->x86.operands[0].reg = reg;
974
17.0k
      MI->flat_insn->detail->x86.operands[0].size = MI->csh->regsize_map[reg];
975
17.0k
      MI->flat_insn->detail->x86.operands[0].access = access1;
976
977
17.0k
      MI->flat_insn->detail->x86.op_count++;
978
273k
    } else {
979
273k
      if (X86_insn_reg_att2(MCInst_getOpcode(MI), &reg, &access1, &reg2, &access2)) {
980
981
8.04k
        MI->flat_insn->detail->x86.operands[0].type = X86_OP_REG;
982
8.04k
        MI->flat_insn->detail->x86.operands[0].reg = reg;
983
8.04k
        MI->flat_insn->detail->x86.operands[0].size = MI->csh->regsize_map[reg];
984
8.04k
        MI->flat_insn->detail->x86.operands[0].access = access1;
985
8.04k
        MI->flat_insn->detail->x86.operands[1].type = X86_OP_REG;
986
8.04k
        MI->flat_insn->detail->x86.operands[1].reg = reg2;
987
8.04k
        MI->flat_insn->detail->x86.operands[1].size = MI->csh->regsize_map[reg2];
988
8.04k
        MI->flat_insn->detail->x86.operands[1].access = access2;
989
8.04k
        MI->flat_insn->detail->x86.op_count = 2;
990
8.04k
      }
991
273k
    }
992
993
290k
#ifndef CAPSTONE_DIET
994
290k
    get_op_access(MI->csh, MCInst_getOpcode(MI), access, &MI->flat_insn->detail->x86.eflags);
995
290k
    MI->flat_insn->detail->x86.operands[0].access = access[0];
996
290k
    MI->flat_insn->detail->x86.operands[1].access = access[1];
997
290k
#endif
998
290k
  }
999
290k
}
1000
1001
#endif