blob: 449445df7d27cd944bb16f95d5dd1fd1a2e7dc56 [file] [log] [blame]
Chad Rosier095e1cd2012-10-03 19:00:20 +00001//===-- X86IntelInstPrinter.cpp - Intel assembly instruction printing -----===//
Chris Lattner44790342009-09-20 07:17:49 +00002//
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//
Chad Rosier095e1cd2012-10-03 19:00:20 +000010// This file includes code for rendering MCInst instances as Intel-style
Chris Lattner44790342009-09-20 07:17:49 +000011// assembly.
12//
13//===----------------------------------------------------------------------===//
14
Chris Lattner44790342009-09-20 07:17:49 +000015#include "X86IntelInstPrinter.h"
Michael Liao425c0db2012-09-26 05:13:44 +000016#include "MCTargetDesc/X86BaseInfo.h"
Evan Cheng3ddfbd32011-07-06 22:01:53 +000017#include "MCTargetDesc/X86MCTargetDesc.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000018#include "X86InstComments.h"
Chris Lattner44790342009-09-20 07:17:49 +000019#include "llvm/MC/MCExpr.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000020#include "llvm/MC/MCInst.h"
Craig Topperdab9e352012-04-02 07:01:04 +000021#include "llvm/MC/MCInstrInfo.h"
Chris Lattner44790342009-09-20 07:17:49 +000022#include "llvm/Support/ErrorHandling.h"
23#include "llvm/Support/FormattedStream.h"
Douglas Gregor69e62062011-01-17 19:17:01 +000024#include <cctype>
Chris Lattner44790342009-09-20 07:17:49 +000025using namespace llvm;
26
Chandler Carruth84e68b22014-04-22 02:41:26 +000027#define DEBUG_TYPE "asm-printer"
28
Chris Lattner44790342009-09-20 07:17:49 +000029#include "X86GenAsmWriter1.inc"
Chris Lattner44790342009-09-20 07:17:49 +000030
Rafael Espindolad6860522011-06-02 02:34:55 +000031void X86IntelInstPrinter::printRegName(raw_ostream &OS, unsigned RegNo) const {
32 OS << getRegisterName(RegNo);
Rafael Espindola08600bc2011-05-30 20:20:15 +000033}
34
Owen Andersona0c3b972011-09-15 23:38:46 +000035void X86IntelInstPrinter::printInst(const MCInst *MI, raw_ostream &OS,
36 StringRef Annot) {
Michael Liao425c0db2012-09-26 05:13:44 +000037 const MCInstrDesc &Desc = MII.get(MI->getOpcode());
38 uint64_t TSFlags = Desc.TSFlags;
39
40 if (TSFlags & X86II::LOCK)
41 OS << "\tlock\n";
42
Chris Lattner70129162010-04-04 05:04:31 +000043 printInstruction(MI, OS);
Kevin Enderby6fbcd8d2012-02-23 18:18:17 +000044
45 // Next always print the annotation.
46 printAnnotation(OS, Annot);
47
Chris Lattner7a05e6d2010-08-28 20:42:31 +000048 // If verbose assembly is enabled, we can print some informative comments.
Kevin Enderby6fbcd8d2012-02-23 18:18:17 +000049 if (CommentStream)
Chris Lattner7a05e6d2010-08-28 20:42:31 +000050 EmitAnyX86InstComments(MI, *CommentStream, getRegisterName);
Chris Lattner76c564b2010-04-04 04:47:45 +000051}
Chris Lattner44790342009-09-20 07:17:49 +000052
Craig Topperee9eef22014-12-26 06:36:28 +000053static void printSSEAVXCC(int64_t Imm, raw_ostream &O) {
Craig Topperf1c20162012-10-09 05:26:13 +000054 switch (Imm) {
55 default: llvm_unreachable("Invalid avxcc argument!");
56 case 0: O << "eq"; break;
57 case 1: O << "lt"; break;
58 case 2: O << "le"; break;
59 case 3: O << "unord"; break;
60 case 4: O << "neq"; break;
61 case 5: O << "nlt"; break;
62 case 6: O << "nle"; break;
63 case 7: O << "ord"; break;
64 case 8: O << "eq_uq"; break;
65 case 9: O << "nge"; break;
66 case 0xa: O << "ngt"; break;
67 case 0xb: O << "false"; break;
68 case 0xc: O << "neq_oq"; break;
69 case 0xd: O << "ge"; break;
70 case 0xe: O << "gt"; break;
71 case 0xf: O << "true"; break;
Elena Demikhovsky1adc1d52012-02-08 08:37:26 +000072 case 0x10: O << "eq_os"; break;
73 case 0x11: O << "lt_oq"; break;
74 case 0x12: O << "le_oq"; break;
75 case 0x13: O << "unord_s"; break;
76 case 0x14: O << "neq_us"; break;
77 case 0x15: O << "nlt_uq"; break;
78 case 0x16: O << "nle_uq"; break;
79 case 0x17: O << "ord_s"; break;
80 case 0x18: O << "eq_us"; break;
81 case 0x19: O << "nge_uq"; break;
82 case 0x1a: O << "ngt_uq"; break;
83 case 0x1b: O << "false_os"; break;
84 case 0x1c: O << "neq_os"; break;
85 case 0x1d: O << "ge_oq"; break;
86 case 0x1e: O << "gt_oq"; break;
87 case 0x1f: O << "true_us"; break;
Chris Lattner44790342009-09-20 07:17:49 +000088 }
89}
90
Craig Topperee9eef22014-12-26 06:36:28 +000091void X86IntelInstPrinter::printSSECC(const MCInst *MI, unsigned Op,
92 raw_ostream &O) {
Craig Topper53f75b92014-12-27 18:11:00 +000093 int64_t Imm = MI->getOperand(Op).getImm();
94 assert((Imm & 0x7) == Imm); // Ensure valid immediate.
Craig Topperee9eef22014-12-26 06:36:28 +000095 printSSEAVXCC(Imm, O);
96}
97
98void X86IntelInstPrinter::printAVXCC(const MCInst *MI, unsigned Op,
99 raw_ostream &O) {
Craig Topper53f75b92014-12-27 18:11:00 +0000100 int64_t Imm = MI->getOperand(Op).getImm();
101 assert((Imm & 0x1f) == Imm); // Ensure valid immediate.
Craig Topperee9eef22014-12-26 06:36:28 +0000102 printSSEAVXCC(Imm, O);
103}
104
Elena Demikhovskyde3f7512014-01-01 15:12:34 +0000105void X86IntelInstPrinter::printRoundingControl(const MCInst *MI, unsigned Op,
106 raw_ostream &O) {
Elena Demikhovskyb19c9dc2014-01-13 12:55:03 +0000107 int64_t Imm = MI->getOperand(Op).getImm() & 0x3;
Elena Demikhovskyde3f7512014-01-01 15:12:34 +0000108 switch (Imm) {
109 case 0: O << "{rn-sae}"; break;
110 case 1: O << "{rd-sae}"; break;
111 case 2: O << "{ru-sae}"; break;
112 case 3: O << "{rz-sae}"; break;
Elena Demikhovskyde3f7512014-01-01 15:12:34 +0000113 }
114}
115
Chad Rosier38e05a92012-09-10 22:50:57 +0000116/// printPCRelImm - This is used to print an immediate value that ends up
Chris Lattner13306a12009-09-20 07:47:59 +0000117/// being encoded as a pc-relative value.
Chad Rosier38e05a92012-09-10 22:50:57 +0000118void X86IntelInstPrinter::printPCRelImm(const MCInst *MI, unsigned OpNo,
119 raw_ostream &O) {
Chris Lattner44790342009-09-20 07:17:49 +0000120 const MCOperand &Op = MI->getOperand(OpNo);
121 if (Op.isImm())
Daniel Maleaa3d42452013-08-01 21:18:16 +0000122 O << formatImm(Op.getImm());
Chris Lattner44790342009-09-20 07:17:49 +0000123 else {
124 assert(Op.isExpr() && "unknown pcrel immediate operand");
Kevin Enderby6fbcd8d2012-02-23 18:18:17 +0000125 // If a symbolic branch target was added as a constant expression then print
126 // that address in hex.
127 const MCConstantExpr *BranchTarget = dyn_cast<MCConstantExpr>(Op.getExpr());
128 int64_t Address;
129 if (BranchTarget && BranchTarget->EvaluateAsAbsolute(Address)) {
Daniel Maleaa3d42452013-08-01 21:18:16 +0000130 O << formatHex((uint64_t)Address);
Kevin Enderby6fbcd8d2012-02-23 18:18:17 +0000131 }
132 else {
133 // Otherwise, just print the expression.
134 O << *Op.getExpr();
135 }
Chris Lattner44790342009-09-20 07:17:49 +0000136 }
137}
138
Chris Lattner44790342009-09-20 07:17:49 +0000139void X86IntelInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
Chris Lattner76c564b2010-04-04 04:47:45 +0000140 raw_ostream &O) {
Chris Lattner44790342009-09-20 07:17:49 +0000141 const MCOperand &Op = MI->getOperand(OpNo);
142 if (Op.isReg()) {
Craig Topperefd67d42013-07-31 02:47:52 +0000143 printRegName(O, Op.getReg());
Chris Lattner44790342009-09-20 07:17:49 +0000144 } else if (Op.isImm()) {
Daniel Maleaa3d42452013-08-01 21:18:16 +0000145 O << formatImm((int64_t)Op.getImm());
Chris Lattner44790342009-09-20 07:17:49 +0000146 } else {
147 assert(Op.isExpr() && "unknown operand kind in printOperand");
Chris Lattnerc8f77172010-01-18 00:37:40 +0000148 O << *Op.getExpr();
Chris Lattner44790342009-09-20 07:17:49 +0000149 }
150}
151
Chris Lattnerf4693072010-07-08 23:46:44 +0000152void X86IntelInstPrinter::printMemReference(const MCInst *MI, unsigned Op,
153 raw_ostream &O) {
Manuel Jacobdcb78db2014-03-18 16:14:11 +0000154 const MCOperand &BaseReg = MI->getOperand(Op+X86::AddrBaseReg);
155 unsigned ScaleVal = MI->getOperand(Op+X86::AddrScaleAmt).getImm();
156 const MCOperand &IndexReg = MI->getOperand(Op+X86::AddrIndexReg);
157 const MCOperand &DispSpec = MI->getOperand(Op+X86::AddrDisp);
158 const MCOperand &SegReg = MI->getOperand(Op+X86::AddrSegmentReg);
Michael Liao5bf95782014-12-04 05:20:33 +0000159
Chris Lattnerf4693072010-07-08 23:46:44 +0000160 // If this has a segment register, print it.
161 if (SegReg.getReg()) {
Manuel Jacobdcb78db2014-03-18 16:14:11 +0000162 printOperand(MI, Op+X86::AddrSegmentReg, O);
Chris Lattnerf4693072010-07-08 23:46:44 +0000163 O << ':';
164 }
Michael Liao5bf95782014-12-04 05:20:33 +0000165
Chris Lattner44790342009-09-20 07:17:49 +0000166 O << '[';
Michael Liao5bf95782014-12-04 05:20:33 +0000167
Chris Lattner44790342009-09-20 07:17:49 +0000168 bool NeedPlus = false;
169 if (BaseReg.getReg()) {
Manuel Jacobdcb78db2014-03-18 16:14:11 +0000170 printOperand(MI, Op+X86::AddrBaseReg, O);
Chris Lattner44790342009-09-20 07:17:49 +0000171 NeedPlus = true;
172 }
Michael Liao5bf95782014-12-04 05:20:33 +0000173
Chris Lattner44790342009-09-20 07:17:49 +0000174 if (IndexReg.getReg()) {
175 if (NeedPlus) O << " + ";
176 if (ScaleVal != 1)
177 O << ScaleVal << '*';
Manuel Jacobdcb78db2014-03-18 16:14:11 +0000178 printOperand(MI, Op+X86::AddrIndexReg, O);
Chris Lattner44790342009-09-20 07:17:49 +0000179 NeedPlus = true;
180 }
Chad Rosier095e1cd2012-10-03 19:00:20 +0000181
Chris Lattner44790342009-09-20 07:17:49 +0000182 if (!DispSpec.isImm()) {
183 if (NeedPlus) O << " + ";
184 assert(DispSpec.isExpr() && "non-immediate displacement for LEA?");
Chris Lattnerc8f77172010-01-18 00:37:40 +0000185 O << *DispSpec.getExpr();
Chris Lattner44790342009-09-20 07:17:49 +0000186 } else {
187 int64_t DispVal = DispSpec.getImm();
188 if (DispVal || (!IndexReg.getReg() && !BaseReg.getReg())) {
189 if (NeedPlus) {
190 if (DispVal > 0)
191 O << " + ";
192 else {
193 O << " - ";
194 DispVal = -DispVal;
195 }
196 }
Daniel Maleaa3d42452013-08-01 21:18:16 +0000197 O << formatImm(DispVal);
Chris Lattner44790342009-09-20 07:17:49 +0000198 }
199 }
Michael Liao5bf95782014-12-04 05:20:33 +0000200
Chris Lattner44790342009-09-20 07:17:49 +0000201 O << ']';
202}
Craig Topper18854172013-08-25 22:23:38 +0000203
David Woodhouse2ef8d9c2014-01-22 15:08:08 +0000204void X86IntelInstPrinter::printSrcIdx(const MCInst *MI, unsigned Op,
205 raw_ostream &O) {
206 const MCOperand &SegReg = MI->getOperand(Op+1);
207
208 // If this has a segment register, print it.
209 if (SegReg.getReg()) {
210 printOperand(MI, Op+1, O);
211 O << ':';
212 }
213 O << '[';
214 printOperand(MI, Op, O);
215 O << ']';
216}
217
David Woodhouseb33c2ef2014-01-22 15:08:21 +0000218void X86IntelInstPrinter::printDstIdx(const MCInst *MI, unsigned Op,
219 raw_ostream &O) {
220 // DI accesses are always ES-based.
221 O << "es:[";
222 printOperand(MI, Op, O);
223 O << ']';
224}
225
Craig Topper18854172013-08-25 22:23:38 +0000226void X86IntelInstPrinter::printMemOffset(const MCInst *MI, unsigned Op,
227 raw_ostream &O) {
228 const MCOperand &DispSpec = MI->getOperand(Op);
Craig Topper35da3d12014-01-16 07:36:58 +0000229 const MCOperand &SegReg = MI->getOperand(Op+1);
230
231 // If this has a segment register, print it.
232 if (SegReg.getReg()) {
233 printOperand(MI, Op+1, O);
234 O << ':';
235 }
Craig Topper18854172013-08-25 22:23:38 +0000236
237 O << '[';
238
239 if (DispSpec.isImm()) {
240 O << formatImm(DispSpec.getImm());
241 } else {
242 assert(DispSpec.isExpr() && "non-immediate displacement?");
243 O << *DispSpec.getExpr();
244 }
245
246 O << ']';
247}