blob: 320ac5addbbb431be07a6e757f2e46f943b7fbed [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
15#define DEBUG_TYPE "asm-printer"
16#include "X86IntelInstPrinter.h"
Michael Liao425c0db2012-09-26 05:13:44 +000017#include "MCTargetDesc/X86BaseInfo.h"
Evan Cheng3ddfbd32011-07-06 22:01:53 +000018#include "MCTargetDesc/X86MCTargetDesc.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000019#include "X86InstComments.h"
Chris Lattner44790342009-09-20 07:17:49 +000020#include "llvm/MC/MCExpr.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000021#include "llvm/MC/MCInst.h"
Craig Topperdab9e352012-04-02 07:01:04 +000022#include "llvm/MC/MCInstrInfo.h"
Chris Lattner44790342009-09-20 07:17:49 +000023#include "llvm/Support/ErrorHandling.h"
24#include "llvm/Support/FormattedStream.h"
Douglas Gregor69e62062011-01-17 19:17:01 +000025#include <cctype>
Chris Lattner44790342009-09-20 07:17:49 +000026using namespace llvm;
27
Chris Lattner44790342009-09-20 07:17:49 +000028#include "X86GenAsmWriter1.inc"
Chris Lattner44790342009-09-20 07:17:49 +000029
Rafael Espindolad6860522011-06-02 02:34:55 +000030void X86IntelInstPrinter::printRegName(raw_ostream &OS, unsigned RegNo) const {
31 OS << getRegisterName(RegNo);
Rafael Espindola08600bc2011-05-30 20:20:15 +000032}
33
Owen Andersona0c3b972011-09-15 23:38:46 +000034void X86IntelInstPrinter::printInst(const MCInst *MI, raw_ostream &OS,
35 StringRef Annot) {
Michael Liao425c0db2012-09-26 05:13:44 +000036 const MCInstrDesc &Desc = MII.get(MI->getOpcode());
37 uint64_t TSFlags = Desc.TSFlags;
38
39 if (TSFlags & X86II::LOCK)
40 OS << "\tlock\n";
41
Chris Lattner70129162010-04-04 05:04:31 +000042 printInstruction(MI, OS);
Kevin Enderby6fbcd8d2012-02-23 18:18:17 +000043
44 // Next always print the annotation.
45 printAnnotation(OS, Annot);
46
Chris Lattner7a05e6d2010-08-28 20:42:31 +000047 // If verbose assembly is enabled, we can print some informative comments.
Kevin Enderby6fbcd8d2012-02-23 18:18:17 +000048 if (CommentStream)
Chris Lattner7a05e6d2010-08-28 20:42:31 +000049 EmitAnyX86InstComments(MI, *CommentStream, getRegisterName);
Chris Lattner76c564b2010-04-04 04:47:45 +000050}
Chris Lattner44790342009-09-20 07:17:49 +000051
Chris Lattner76c564b2010-04-04 04:47:45 +000052void X86IntelInstPrinter::printSSECC(const MCInst *MI, unsigned Op,
53 raw_ostream &O) {
Craig Topperf1c20162012-10-09 05:26:13 +000054 int64_t Imm = MI->getOperand(Op).getImm() & 0xf;
55 switch (Imm) {
Craig Topper4ed72782012-02-05 05:38:58 +000056 default: llvm_unreachable("Invalid ssecc argument!");
Elena Demikhovsky1adc1d52012-02-08 08:37:26 +000057 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;
Craig Topperf1c20162012-10-09 05:26:13 +000073 }
74}
75
76void 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;
Elena Demikhovsky1adc1d52012-02-08 08:37:26 +000097 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;
Chris Lattner44790342009-09-20 07:17:49 +0000113 }
114}
115
Elena Demikhovskyde3f7512014-01-01 15:12:34 +0000116void X86IntelInstPrinter::printRoundingControl(const MCInst *MI, unsigned Op,
117 raw_ostream &O) {
118 int64_t Imm = MI->getOperand(Op).getImm() & 0x1f;
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 default: llvm_unreachable("Invalid AVX-512 rounding control argument!");
126 }
127}
128
Chad Rosier38e05a92012-09-10 22:50:57 +0000129/// printPCRelImm - This is used to print an immediate value that ends up
Chris Lattner13306a12009-09-20 07:47:59 +0000130/// being encoded as a pc-relative value.
Chad Rosier38e05a92012-09-10 22:50:57 +0000131void X86IntelInstPrinter::printPCRelImm(const MCInst *MI, unsigned OpNo,
132 raw_ostream &O) {
Chris Lattner44790342009-09-20 07:17:49 +0000133 const MCOperand &Op = MI->getOperand(OpNo);
134 if (Op.isImm())
Daniel Maleaa3d42452013-08-01 21:18:16 +0000135 O << formatImm(Op.getImm());
Chris Lattner44790342009-09-20 07:17:49 +0000136 else {
137 assert(Op.isExpr() && "unknown pcrel immediate operand");
Kevin Enderby6fbcd8d2012-02-23 18:18:17 +0000138 // If a symbolic branch target was added as a constant expression then print
139 // that address in hex.
140 const MCConstantExpr *BranchTarget = dyn_cast<MCConstantExpr>(Op.getExpr());
141 int64_t Address;
142 if (BranchTarget && BranchTarget->EvaluateAsAbsolute(Address)) {
Daniel Maleaa3d42452013-08-01 21:18:16 +0000143 O << formatHex((uint64_t)Address);
Kevin Enderby6fbcd8d2012-02-23 18:18:17 +0000144 }
145 else {
146 // Otherwise, just print the expression.
147 O << *Op.getExpr();
148 }
Chris Lattner44790342009-09-20 07:17:49 +0000149 }
150}
151
Chris Lattner44790342009-09-20 07:17:49 +0000152void X86IntelInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
Chris Lattner76c564b2010-04-04 04:47:45 +0000153 raw_ostream &O) {
Chris Lattner44790342009-09-20 07:17:49 +0000154 const MCOperand &Op = MI->getOperand(OpNo);
155 if (Op.isReg()) {
Craig Topperefd67d42013-07-31 02:47:52 +0000156 printRegName(O, Op.getReg());
Chris Lattner44790342009-09-20 07:17:49 +0000157 } else if (Op.isImm()) {
Daniel Maleaa3d42452013-08-01 21:18:16 +0000158 O << formatImm((int64_t)Op.getImm());
Chris Lattner44790342009-09-20 07:17:49 +0000159 } else {
160 assert(Op.isExpr() && "unknown operand kind in printOperand");
Chris Lattnerc8f77172010-01-18 00:37:40 +0000161 O << *Op.getExpr();
Chris Lattner44790342009-09-20 07:17:49 +0000162 }
163}
164
Chris Lattnerf4693072010-07-08 23:46:44 +0000165void X86IntelInstPrinter::printMemReference(const MCInst *MI, unsigned Op,
166 raw_ostream &O) {
Chris Lattner44790342009-09-20 07:17:49 +0000167 const MCOperand &BaseReg = MI->getOperand(Op);
168 unsigned ScaleVal = MI->getOperand(Op+1).getImm();
169 const MCOperand &IndexReg = MI->getOperand(Op+2);
170 const MCOperand &DispSpec = MI->getOperand(Op+3);
Chris Lattnerf4693072010-07-08 23:46:44 +0000171 const MCOperand &SegReg = MI->getOperand(Op+4);
172
173 // If this has a segment register, print it.
174 if (SegReg.getReg()) {
175 printOperand(MI, Op+4, O);
176 O << ':';
177 }
Chris Lattner44790342009-09-20 07:17:49 +0000178
179 O << '[';
180
181 bool NeedPlus = false;
182 if (BaseReg.getReg()) {
Chris Lattner76c564b2010-04-04 04:47:45 +0000183 printOperand(MI, Op, O);
Chris Lattner44790342009-09-20 07:17:49 +0000184 NeedPlus = true;
185 }
186
187 if (IndexReg.getReg()) {
188 if (NeedPlus) O << " + ";
189 if (ScaleVal != 1)
190 O << ScaleVal << '*';
Chris Lattner76c564b2010-04-04 04:47:45 +0000191 printOperand(MI, Op+2, O);
Chris Lattner44790342009-09-20 07:17:49 +0000192 NeedPlus = true;
193 }
Chad Rosier095e1cd2012-10-03 19:00:20 +0000194
Chris Lattner44790342009-09-20 07:17:49 +0000195 if (!DispSpec.isImm()) {
196 if (NeedPlus) O << " + ";
197 assert(DispSpec.isExpr() && "non-immediate displacement for LEA?");
Chris Lattnerc8f77172010-01-18 00:37:40 +0000198 O << *DispSpec.getExpr();
Chris Lattner44790342009-09-20 07:17:49 +0000199 } else {
200 int64_t DispVal = DispSpec.getImm();
201 if (DispVal || (!IndexReg.getReg() && !BaseReg.getReg())) {
202 if (NeedPlus) {
203 if (DispVal > 0)
204 O << " + ";
205 else {
206 O << " - ";
207 DispVal = -DispVal;
208 }
209 }
Daniel Maleaa3d42452013-08-01 21:18:16 +0000210 O << formatImm(DispVal);
Chris Lattner44790342009-09-20 07:17:49 +0000211 }
212 }
213
214 O << ']';
215}
Craig Topper18854172013-08-25 22:23:38 +0000216
217void X86IntelInstPrinter::printMemOffset(const MCInst *MI, unsigned Op,
218 raw_ostream &O) {
219 const MCOperand &DispSpec = MI->getOperand(Op);
220
221 O << '[';
222
223 if (DispSpec.isImm()) {
224 O << formatImm(DispSpec.getImm());
225 } else {
226 assert(DispSpec.isExpr() && "non-immediate displacement?");
227 O << *DispSpec.getExpr();
228 }
229
230 O << ']';
231}