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