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