[X86] Clean up whitespace as well as minor coding style
[oota-llvm.git] / lib / Target / X86 / InstPrinter / X86ATTInstPrinter.cpp
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 #include "X86ATTInstPrinter.h"
16 #include "MCTargetDesc/X86BaseInfo.h"
17 #include "MCTargetDesc/X86MCTargetDesc.h"
18 #include "X86InstComments.h"
19 #include "llvm/MC/MCAsmInfo.h"
20 #include "llvm/MC/MCExpr.h"
21 #include "llvm/MC/MCInst.h"
22 #include "llvm/MC/MCInstrInfo.h"
23 #include "llvm/MC/MCRegisterInfo.h"
24 #include "llvm/Support/ErrorHandling.h"
25 #include "llvm/Support/Format.h"
26 #include "llvm/Support/FormattedStream.h"
27 #include <map>
28 using namespace llvm;
29
30 #define DEBUG_TYPE "asm-printer"
31
32 // Include the auto-generated portion of the assembly writer.
33 #define PRINT_ALIAS_INSTR
34 #include "X86GenAsmWriter.inc"
35
36 void X86ATTInstPrinter::printRegName(raw_ostream &OS,
37                                      unsigned RegNo) const {
38   OS << markup("<reg:")
39      << '%' << getRegisterName(RegNo)
40      << markup(">");
41 }
42
43 void X86ATTInstPrinter::printInst(const MCInst *MI, raw_ostream &OS,
44                                   StringRef Annot) {
45   const MCInstrDesc &Desc = MII.get(MI->getOpcode());
46   uint64_t TSFlags = Desc.TSFlags;
47
48   // If verbose assembly is enabled, we can print some informative comments.
49   if (CommentStream)
50     HasCustomInstComment =
51         EmitAnyX86InstComments(MI, *CommentStream, getRegisterName);
52
53   if (TSFlags & X86II::LOCK)
54     OS << "\tlock\n";
55
56   // Output CALLpcrel32 as "callq" in 64-bit mode.
57   // In Intel annotation it's always emitted as "call".
58   //
59   // TODO: Probably this hack should be redesigned via InstAlias in
60   // InstrInfo.td as soon as Requires clause is supported properly
61   // for InstAlias.
62   if (MI->getOpcode() == X86::CALLpcrel32 &&
63       (getAvailableFeatures() & X86::Mode64Bit) != 0) {
64     OS << "\tcallq\t";
65     printPCRelImm(MI, 0, OS);
66   }
67   // Try to print any aliases first.
68   else if (!printAliasInstr(MI, OS))
69     printInstruction(MI, OS);
70
71   // Next always print the annotation.
72   printAnnotation(OS, Annot);
73 }
74
75 void X86ATTInstPrinter::printSSECC(const MCInst *MI, unsigned Op,
76                                    raw_ostream &O) {
77   int64_t Imm = MI->getOperand(Op).getImm() & 0xf;
78   switch (Imm) {
79   default: llvm_unreachable("Invalid ssecc argument!");
80   case    0: O << "eq"; break;
81   case    1: O << "lt"; break;
82   case    2: O << "le"; break;
83   case    3: O << "unord"; break;
84   case    4: O << "neq"; break;
85   case    5: O << "nlt"; break;
86   case    6: O << "nle"; break;
87   case    7: O << "ord"; break;
88   case    8: O << "eq_uq"; break;
89   case    9: O << "nge"; break;
90   case  0xa: O << "ngt"; break;
91   case  0xb: O << "false"; break;
92   case  0xc: O << "neq_oq"; break;
93   case  0xd: O << "ge"; break;
94   case  0xe: O << "gt"; break;
95   case  0xf: O << "true"; break;
96   }
97 }
98
99 void X86ATTInstPrinter::printAVXCC(const MCInst *MI, unsigned Op,
100                                    raw_ostream &O) {
101   int64_t Imm = MI->getOperand(Op).getImm() & 0x1f;
102   switch (Imm) {
103   default: llvm_unreachable("Invalid avxcc argument!");
104   case    0: O << "eq"; break;
105   case    1: O << "lt"; break;
106   case    2: O << "le"; break;
107   case    3: O << "unord"; break;
108   case    4: O << "neq"; break;
109   case    5: O << "nlt"; break;
110   case    6: O << "nle"; break;
111   case    7: O << "ord"; break;
112   case    8: O << "eq_uq"; break;
113   case    9: O << "nge"; break;
114   case  0xa: O << "ngt"; break;
115   case  0xb: O << "false"; break;
116   case  0xc: O << "neq_oq"; break;
117   case  0xd: O << "ge"; break;
118   case  0xe: O << "gt"; break;
119   case  0xf: O << "true"; break;
120   case 0x10: O << "eq_os"; break;
121   case 0x11: O << "lt_oq"; break;
122   case 0x12: O << "le_oq"; break;
123   case 0x13: O << "unord_s"; break;
124   case 0x14: O << "neq_us"; break;
125   case 0x15: O << "nlt_uq"; break;
126   case 0x16: O << "nle_uq"; break;
127   case 0x17: O << "ord_s"; break;
128   case 0x18: O << "eq_us"; break;
129   case 0x19: O << "nge_uq"; break;
130   case 0x1a: O << "ngt_uq"; break;
131   case 0x1b: O << "false_os"; break;
132   case 0x1c: O << "neq_os"; break;
133   case 0x1d: O << "ge_oq"; break;
134   case 0x1e: O << "gt_oq"; break;
135   case 0x1f: O << "true_us"; break;
136   }
137 }
138
139 void X86ATTInstPrinter::printRoundingControl(const MCInst *MI, unsigned Op,
140                                    raw_ostream &O) {
141   int64_t Imm = MI->getOperand(Op).getImm() & 0x3;
142   switch (Imm) {
143   case 0: O << "{rn-sae}"; break;
144   case 1: O << "{rd-sae}"; break;
145   case 2: O << "{ru-sae}"; break;
146   case 3: O << "{rz-sae}"; break;
147   }
148 }
149 /// printPCRelImm - This is used to print an immediate value that ends up
150 /// being encoded as a pc-relative value (e.g. for jumps and calls).  These
151 /// print slightly differently than normal immediates.  For example, a $ is not
152 /// emitted.
153 void X86ATTInstPrinter::printPCRelImm(const MCInst *MI, unsigned OpNo,
154                                       raw_ostream &O) {
155   const MCOperand &Op = MI->getOperand(OpNo);
156   if (Op.isImm())
157     O << formatImm(Op.getImm());
158   else {
159     assert(Op.isExpr() && "unknown pcrel immediate operand");
160     // If a symbolic branch target was added as a constant expression then print
161     // that address in hex.
162     const MCConstantExpr *BranchTarget = dyn_cast<MCConstantExpr>(Op.getExpr());
163     int64_t Address;
164     if (BranchTarget && BranchTarget->EvaluateAsAbsolute(Address)) {
165       O << formatHex((uint64_t)Address);
166     } else {
167       // Otherwise, just print the expression.
168       O << *Op.getExpr();
169     }
170   }
171 }
172
173 void X86ATTInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
174                                      raw_ostream &O) {
175   const MCOperand &Op = MI->getOperand(OpNo);
176   if (Op.isReg()) {
177     printRegName(O, Op.getReg());
178   } else if (Op.isImm()) {
179     // Print X86 immediates as signed values.
180     O << markup("<imm:")
181       << '$' << formatImm((int64_t)Op.getImm())
182       << markup(">");
183
184     // If there are no instruction-specific comments, add a comment clarifying
185     // the hex value of the immediate operand when it isn't in the range
186     // [-256,255].
187     if (CommentStream && !HasCustomInstComment &&
188         (Op.getImm() > 255 || Op.getImm() < -256))
189       *CommentStream << format("imm = 0x%" PRIX64 "\n", (uint64_t)Op.getImm());
190
191   } else {
192     assert(Op.isExpr() && "unknown operand kind in printOperand");
193     O << markup("<imm:")
194       << '$' << *Op.getExpr()
195       << markup(">");
196   }
197 }
198
199 void X86ATTInstPrinter::printMemReference(const MCInst *MI, unsigned Op,
200                                           raw_ostream &O) {
201   const MCOperand &BaseReg  = MI->getOperand(Op+X86::AddrBaseReg);
202   const MCOperand &IndexReg = MI->getOperand(Op+X86::AddrIndexReg);
203   const MCOperand &DispSpec = MI->getOperand(Op+X86::AddrDisp);
204   const MCOperand &SegReg = MI->getOperand(Op+X86::AddrSegmentReg);
205
206   O << markup("<mem:");
207
208   // If this has a segment register, print it.
209   if (SegReg.getReg()) {
210     printOperand(MI, Op+X86::AddrSegmentReg, O);
211     O << ':';
212   }
213
214   if (DispSpec.isImm()) {
215     int64_t DispVal = DispSpec.getImm();
216     if (DispVal || (!IndexReg.getReg() && !BaseReg.getReg()))
217       O << formatImm(DispVal);
218   } else {
219     assert(DispSpec.isExpr() && "non-immediate displacement for LEA?");
220     O << *DispSpec.getExpr();
221   }
222
223   if (IndexReg.getReg() || BaseReg.getReg()) {
224     O << '(';
225     if (BaseReg.getReg())
226       printOperand(MI, Op+X86::AddrBaseReg, O);
227
228     if (IndexReg.getReg()) {
229       O << ',';
230       printOperand(MI, Op+X86::AddrIndexReg, O);
231       unsigned ScaleVal = MI->getOperand(Op+X86::AddrScaleAmt).getImm();
232       if (ScaleVal != 1) {
233         O << ','
234           << markup("<imm:")
235           << ScaleVal // never printed in hex.
236           << markup(">");
237       }
238     }
239     O << ')';
240   }
241
242   O << markup(">");
243 }
244
245 void X86ATTInstPrinter::printSrcIdx(const MCInst *MI, unsigned Op,
246                                     raw_ostream &O) {
247   const MCOperand &SegReg = MI->getOperand(Op+1);
248
249   O << markup("<mem:");
250
251   // If this has a segment register, print it.
252   if (SegReg.getReg()) {
253     printOperand(MI, Op+1, O);
254     O << ':';
255   }
256
257   O << "(";
258   printOperand(MI, Op, O);
259   O << ")";
260
261   O << markup(">");
262 }
263
264 void X86ATTInstPrinter::printDstIdx(const MCInst *MI, unsigned Op,
265                                     raw_ostream &O) {
266   O << markup("<mem:");
267
268   O << "%es:(";
269   printOperand(MI, Op, O);
270   O << ")";
271
272   O << markup(">");
273 }
274
275 void X86ATTInstPrinter::printMemOffset(const MCInst *MI, unsigned Op,
276                                        raw_ostream &O) {
277   const MCOperand &DispSpec = MI->getOperand(Op);
278   const MCOperand &SegReg = MI->getOperand(Op+1);
279
280   O << markup("<mem:");
281
282   // If this has a segment register, print it.
283   if (SegReg.getReg()) {
284     printOperand(MI, Op+1, O);
285     O << ':';
286   }
287
288   if (DispSpec.isImm()) {
289     O << formatImm(DispSpec.getImm());
290   } else {
291     assert(DispSpec.isExpr() && "non-immediate displacement?");
292     O << *DispSpec.getExpr();
293   }
294
295   O << markup(">");
296 }