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