1 //===-- X86ATTInstPrinter.cpp - AT&T assembly instruction printing --------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file includes code for rendering MCInst instances as AT&T-style
13 //===----------------------------------------------------------------------===//
15 #define DEBUG_TYPE "asm-printer"
16 #include "X86ATTInstPrinter.h"
17 #include "X86InstComments.h"
18 #include "X86Subtarget.h"
19 #include "llvm/MC/MCInst.h"
20 #include "llvm/MC/MCAsmInfo.h"
21 #include "llvm/MC/MCExpr.h"
22 #include "llvm/Support/ErrorHandling.h"
23 #include "llvm/Support/Format.h"
24 #include "llvm/Support/FormattedStream.h"
25 #include "X86GenInstrNames.inc"
29 // Include the auto-generated portion of the assembly writer.
30 #define GET_INSTRUCTION_NAME
31 #define PRINT_ALIAS_INSTR
32 #include "X86GenRegisterNames.inc"
33 #include "X86GenAsmWriter.inc"
34 #undef PRINT_ALIAS_INSTR
35 #undef GET_INSTRUCTION_NAME
37 X86ATTInstPrinter::X86ATTInstPrinter(TargetMachine
&TM
, const MCAsmInfo
&MAI
)
38 : MCInstPrinter(MAI
) {
39 // Initialize the set of available features.
40 setAvailableFeatures(ComputeAvailableFeatures(
41 &TM
.getSubtarget
<X86Subtarget
>()));
44 void X86ATTInstPrinter::printInst(const MCInst
*MI
, raw_ostream
&OS
) {
45 printInstruction(MI
, OS
);
47 // If verbose assembly is enabled, we can print some informative comments.
49 EmitAnyX86InstComments(MI
, *CommentStream
, getRegisterName
);
52 StringRef
X86ATTInstPrinter::getOpcodeName(unsigned Opcode
) const {
53 return getInstructionName(Opcode
);
56 void X86ATTInstPrinter::printSSECC(const MCInst
*MI
, unsigned Op
,
58 switch (MI
->getOperand(Op
).getImm()) {
59 default: assert(0 && "Invalid ssecc argument!");
60 case 0: O
<< "eq"; break;
61 case 1: O
<< "lt"; break;
62 case 2: O
<< "le"; break;
63 case 3: O
<< "unord"; break;
64 case 4: O
<< "neq"; break;
65 case 5: O
<< "nlt"; break;
66 case 6: O
<< "nle"; break;
67 case 7: O
<< "ord"; break;
71 /// print_pcrel_imm - This is used to print an immediate value that ends up
72 /// being encoded as a pc-relative value (e.g. for jumps and calls). These
73 /// print slightly differently than normal immediates. For example, a $ is not
75 void X86ATTInstPrinter::print_pcrel_imm(const MCInst
*MI
, unsigned OpNo
,
77 const MCOperand
&Op
= MI
->getOperand(OpNo
);
79 // Print this as a signed 32-bit value.
80 O
<< (int)Op
.getImm();
82 assert(Op
.isExpr() && "unknown pcrel immediate operand");
87 void X86ATTInstPrinter::printOperand(const MCInst
*MI
, unsigned OpNo
,
89 const MCOperand
&Op
= MI
->getOperand(OpNo
);
91 O
<< '%' << getRegisterName(Op
.getReg());
92 } else if (Op
.isImm()) {
93 O
<< '$' << Op
.getImm();
95 if (CommentStream
&& (Op
.getImm() > 255 || Op
.getImm() < -256))
96 *CommentStream
<< format("imm = 0x%llX\n", (long long)Op
.getImm());
99 assert(Op
.isExpr() && "unknown operand kind in printOperand");
100 O
<< '$' << *Op
.getExpr();
104 void X86ATTInstPrinter::printMemReference(const MCInst
*MI
, unsigned Op
,
106 const MCOperand
&BaseReg
= MI
->getOperand(Op
);
107 const MCOperand
&IndexReg
= MI
->getOperand(Op
+2);
108 const MCOperand
&DispSpec
= MI
->getOperand(Op
+3);
109 const MCOperand
&SegReg
= MI
->getOperand(Op
+4);
111 // If this has a segment register, print it.
112 if (SegReg
.getReg()) {
113 printOperand(MI
, Op
+4, O
);
117 if (DispSpec
.isImm()) {
118 int64_t DispVal
= DispSpec
.getImm();
119 if (DispVal
|| (!IndexReg
.getReg() && !BaseReg
.getReg()))
122 assert(DispSpec
.isExpr() && "non-immediate displacement for LEA?");
123 O
<< *DispSpec
.getExpr();
126 if (IndexReg
.getReg() || BaseReg
.getReg()) {
128 if (BaseReg
.getReg())
129 printOperand(MI
, Op
, O
);
131 if (IndexReg
.getReg()) {
133 printOperand(MI
, Op
+2, O
);
134 unsigned ScaleVal
= MI
->getOperand(Op
+1).getImm();
136 O
<< ',' << ScaleVal
;