OSDN Git Service

[X86] Added support for nocf_check attribute for indirect Branch Tracking
[android-x86/external-llvm.git] / lib / Target / X86 / InstPrinter / X86IntelInstPrinter.cpp
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 #include "X86IntelInstPrinter.h"
16 #include "MCTargetDesc/X86BaseInfo.h"
17 #include "X86InstComments.h"
18 #include "llvm/MC/MCExpr.h"
19 #include "llvm/MC/MCInst.h"
20 #include "llvm/MC/MCInstrDesc.h"
21 #include "llvm/MC/MCInstrInfo.h"
22 #include "llvm/Support/Casting.h"
23 #include "llvm/Support/ErrorHandling.h"
24 #include <cassert>
25 #include <cstdint>
26
27 using namespace llvm;
28
29 #define DEBUG_TYPE "asm-printer"
30
31 #include "X86GenAsmWriter1.inc"
32
33 void X86IntelInstPrinter::printRegName(raw_ostream &OS, unsigned RegNo) const {
34   OS << getRegisterName(RegNo);
35 }
36
37 void X86IntelInstPrinter::printInst(const MCInst *MI, raw_ostream &OS,
38                                     StringRef Annot,
39                                     const MCSubtargetInfo &STI) {
40   const MCInstrDesc &Desc = MII.get(MI->getOpcode());
41   uint64_t TSFlags = Desc.TSFlags;
42   unsigned Flags = MI->getFlags();
43
44   if ((TSFlags & X86II::LOCK) || (Flags & X86::IP_HAS_LOCK))
45     OS << "\tlock\t";
46
47   if (Flags & X86::IP_HAS_REPEAT_NE)
48     OS << "\trepne\t";
49   else if (Flags & X86::IP_HAS_REPEAT)
50     OS << "\trep\t";
51
52   if ((TSFlags & X86II::NOTRACK) || (Flags & X86::IP_HAS_NOTRACK))
53     OS << "\tnotrack\t";
54
55   printInstruction(MI, OS);
56
57   // Next always print the annotation.
58   printAnnotation(OS, Annot);
59
60   // If verbose assembly is enabled, we can print some informative comments.
61   if (CommentStream)
62     EmitAnyX86InstComments(MI, *CommentStream, MII, getRegisterName);
63 }
64
65 void X86IntelInstPrinter::printSSEAVXCC(const MCInst *MI, unsigned Op,
66                                         raw_ostream &O) {
67   int64_t Imm = MI->getOperand(Op).getImm();
68   switch (Imm) {
69   default: llvm_unreachable("Invalid avxcc argument!");
70   case    0: O << "eq"; break;
71   case    1: O << "lt"; break;
72   case    2: O << "le"; break;
73   case    3: O << "unord"; break;
74   case    4: O << "neq"; break;
75   case    5: O << "nlt"; break;
76   case    6: O << "nle"; break;
77   case    7: O << "ord"; break;
78   case    8: O << "eq_uq"; break;
79   case    9: O << "nge"; break;
80   case  0xa: O << "ngt"; break;
81   case  0xb: O << "false"; break;
82   case  0xc: O << "neq_oq"; break;
83   case  0xd: O << "ge"; break;
84   case  0xe: O << "gt"; break;
85   case  0xf: O << "true"; break;
86   case 0x10: O << "eq_os"; break;
87   case 0x11: O << "lt_oq"; break;
88   case 0x12: O << "le_oq"; break;
89   case 0x13: O << "unord_s"; break;
90   case 0x14: O << "neq_us"; break;
91   case 0x15: O << "nlt_uq"; break;
92   case 0x16: O << "nle_uq"; break;
93   case 0x17: O << "ord_s"; break;
94   case 0x18: O << "eq_us"; break;
95   case 0x19: O << "nge_uq"; break;
96   case 0x1a: O << "ngt_uq"; break;
97   case 0x1b: O << "false_os"; break;
98   case 0x1c: O << "neq_os"; break;
99   case 0x1d: O << "ge_oq"; break;
100   case 0x1e: O << "gt_oq"; break;
101   case 0x1f: O << "true_us"; break;
102   }
103 }
104
105 void X86IntelInstPrinter::printXOPCC(const MCInst *MI, unsigned Op,
106                                      raw_ostream &O) {
107   int64_t Imm = MI->getOperand(Op).getImm();
108   switch (Imm) {
109   default: llvm_unreachable("Invalid xopcc argument!");
110   case 0: O << "lt"; break;
111   case 1: O << "le"; break;
112   case 2: O << "gt"; break;
113   case 3: O << "ge"; break;
114   case 4: O << "eq"; break;
115   case 5: O << "neq"; break;
116   case 6: O << "false"; break;
117   case 7: O << "true"; break;
118   }
119 }
120
121 void X86IntelInstPrinter::printRoundingControl(const MCInst *MI, unsigned Op,
122                                                raw_ostream &O) {
123   int64_t Imm = MI->getOperand(Op).getImm() & 0x3;
124   switch (Imm) {
125   case 0: O << "{rn-sae}"; break;
126   case 1: O << "{rd-sae}"; break;
127   case 2: O << "{ru-sae}"; break;
128   case 3: O << "{rz-sae}"; break;
129   }
130 }
131
132 /// printPCRelImm - This is used to print an immediate value that ends up
133 /// being encoded as a pc-relative value.
134 void X86IntelInstPrinter::printPCRelImm(const MCInst *MI, unsigned OpNo,
135                                         raw_ostream &O) {
136   const MCOperand &Op = MI->getOperand(OpNo);
137   if (Op.isImm())
138     O << formatImm(Op.getImm());
139   else {
140     assert(Op.isExpr() && "unknown pcrel immediate operand");
141     // If a symbolic branch target was added as a constant expression then print
142     // that address in hex.
143     const MCConstantExpr *BranchTarget = dyn_cast<MCConstantExpr>(Op.getExpr());
144     int64_t Address;
145     if (BranchTarget && BranchTarget->evaluateAsAbsolute(Address)) {
146       O << formatHex((uint64_t)Address);
147     }
148     else {
149       // Otherwise, just print the expression.
150       Op.getExpr()->print(O, &MAI);
151     }
152   }
153 }
154
155 void X86IntelInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
156                                        raw_ostream &O) {
157   const MCOperand &Op = MI->getOperand(OpNo);
158   if (Op.isReg()) {
159     printRegName(O, Op.getReg());
160   } else if (Op.isImm()) {
161     O << formatImm((int64_t)Op.getImm());
162   } else {
163     assert(Op.isExpr() && "unknown operand kind in printOperand");
164     O << "offset ";
165     Op.getExpr()->print(O, &MAI);
166   }
167 }
168
169 void X86IntelInstPrinter::printMemReference(const MCInst *MI, unsigned Op,
170                                             raw_ostream &O) {
171   const MCOperand &BaseReg  = MI->getOperand(Op+X86::AddrBaseReg);
172   unsigned ScaleVal         = MI->getOperand(Op+X86::AddrScaleAmt).getImm();
173   const MCOperand &IndexReg = MI->getOperand(Op+X86::AddrIndexReg);
174   const MCOperand &DispSpec = MI->getOperand(Op+X86::AddrDisp);
175   const MCOperand &SegReg   = MI->getOperand(Op+X86::AddrSegmentReg);
176
177   // If this has a segment register, print it.
178   if (SegReg.getReg()) {
179     printOperand(MI, Op+X86::AddrSegmentReg, O);
180     O << ':';
181   }
182
183   O << '[';
184
185   bool NeedPlus = false;
186   if (BaseReg.getReg()) {
187     printOperand(MI, Op+X86::AddrBaseReg, O);
188     NeedPlus = true;
189   }
190
191   if (IndexReg.getReg()) {
192     if (NeedPlus) O << " + ";
193     if (ScaleVal != 1)
194       O << ScaleVal << '*';
195     printOperand(MI, Op+X86::AddrIndexReg, O);
196     NeedPlus = true;
197   }
198
199   if (!DispSpec.isImm()) {
200     if (NeedPlus) O << " + ";
201     assert(DispSpec.isExpr() && "non-immediate displacement for LEA?");
202     DispSpec.getExpr()->print(O, &MAI);
203   } else {
204     int64_t DispVal = DispSpec.getImm();
205     if (DispVal || (!IndexReg.getReg() && !BaseReg.getReg())) {
206       if (NeedPlus) {
207         if (DispVal > 0)
208           O << " + ";
209         else {
210           O << " - ";
211           DispVal = -DispVal;
212         }
213       }
214       O << formatImm(DispVal);
215     }
216   }
217
218   O << ']';
219 }
220
221 void X86IntelInstPrinter::printSrcIdx(const MCInst *MI, unsigned Op,
222                                       raw_ostream &O) {
223   const MCOperand &SegReg   = MI->getOperand(Op+1);
224
225   // If this has a segment register, print it.
226   if (SegReg.getReg()) {
227     printOperand(MI, Op+1, O);
228     O << ':';
229   }
230   O << '[';
231   printOperand(MI, Op, O);
232   O << ']';
233 }
234
235 void X86IntelInstPrinter::printDstIdx(const MCInst *MI, unsigned Op,
236                                       raw_ostream &O) {
237   // DI accesses are always ES-based.
238   O << "es:[";
239   printOperand(MI, Op, O);
240   O << ']';
241 }
242
243 void X86IntelInstPrinter::printMemOffset(const MCInst *MI, unsigned Op,
244                                          raw_ostream &O) {
245   const MCOperand &DispSpec = MI->getOperand(Op);
246   const MCOperand &SegReg   = MI->getOperand(Op+1);
247
248   // If this has a segment register, print it.
249   if (SegReg.getReg()) {
250     printOperand(MI, Op+1, O);
251     O << ':';
252   }
253
254   O << '[';
255
256   if (DispSpec.isImm()) {
257     O << formatImm(DispSpec.getImm());
258   } else {
259     assert(DispSpec.isExpr() && "non-immediate displacement?");
260     DispSpec.getExpr()->print(O, &MAI);
261   }
262
263   O << ']';
264 }
265
266 void X86IntelInstPrinter::printU8Imm(const MCInst *MI, unsigned Op,
267                                      raw_ostream &O) {
268   if (MI->getOperand(Op).isExpr())
269     return MI->getOperand(Op).getExpr()->print(O, &MAI);
270
271   O << formatImm(MI->getOperand(Op).getImm() & 0xff);
272 }