blob: e357710b20eb86bb941c620c2a306a5a97afe25a [file] [log] [blame]
Chris Lattnera08186a2009-06-19 00:47:59 +00001//===-- 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"
Chris Lattner1cbd3de2009-09-13 19:30:11 +000016#include "X86ATTInstPrinter.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 Lattner7b26fce2009-08-22 20:48:53 +000020#include "llvm/MC/MCAsmInfo.h"
Daniel Dunbar73da11e2009-08-31 08:08:38 +000021#include "llvm/MC/MCExpr.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000022#include "llvm/MC/MCInst.h"
Craig Topperdab9e352012-04-02 07:01:04 +000023#include "llvm/MC/MCInstrInfo.h"
Benjamin Kramer682de392012-03-30 23:13:40 +000024#include "llvm/MC/MCRegisterInfo.h"
Torok Edwin6dd27302009-07-08 18:01:40 +000025#include "llvm/Support/ErrorHandling.h"
Chris Lattner482bf692010-02-10 00:10:18 +000026#include "llvm/Support/Format.h"
David Greenea31f96c2009-07-14 20:18:05 +000027#include "llvm/Support/FormattedStream.h"
Bill Wendlingbc3f7902011-04-07 21:20:06 +000028#include <map>
Chris Lattnera08186a2009-06-19 00:47:59 +000029using namespace llvm;
30
Chris Lattner8d284c72009-06-19 23:59:57 +000031// Include the auto-generated portion of the assembly writer.
Bill Wendlingbc3f7902011-04-07 21:20:06 +000032#define PRINT_ALIAS_INSTR
Chris Lattner8d284c72009-06-19 23:59:57 +000033#include "X86GenAsmWriter.inc"
Bill Wendlingbc3f7902011-04-07 21:20:06 +000034
Rafael Espindolad6860522011-06-02 02:34:55 +000035void X86ATTInstPrinter::printRegName(raw_ostream &OS,
36 unsigned RegNo) const {
Kevin Enderbydccdac62012-10-23 22:52:52 +000037 OS << markup("<reg:")
38 << '%' << getRegisterName(RegNo)
39 << markup(">");
Rafael Espindola08600bc2011-05-30 20:20:15 +000040}
41
Owen Andersona0c3b972011-09-15 23:38:46 +000042void X86ATTInstPrinter::printInst(const MCInst *MI, raw_ostream &OS,
43 StringRef Annot) {
Michael Liao425c0db2012-09-26 05:13:44 +000044 const MCInstrDesc &Desc = MII.get(MI->getOpcode());
45 uint64_t TSFlags = Desc.TSFlags;
46
47 if (TSFlags & X86II::LOCK)
48 OS << "\tlock\n";
49
Eric Christopher2e3fbaa2011-04-18 21:28:11 +000050 // Try to print any aliases first.
51 if (!printAliasInstr(MI, OS))
Bill Wendling7e07d6f2011-04-14 01:11:51 +000052 printInstruction(MI, OS);
Chris Lattner7a05e6d2010-08-28 20:42:31 +000053
Kevin Enderby6fbcd8d2012-02-23 18:18:17 +000054 // Next always print the annotation.
55 printAnnotation(OS, Annot);
56
Chris Lattner7a05e6d2010-08-28 20:42:31 +000057 // If verbose assembly is enabled, we can print some informative comments.
Kevin Enderby6fbcd8d2012-02-23 18:18:17 +000058 if (CommentStream)
Chris Lattner7a05e6d2010-08-28 20:42:31 +000059 EmitAnyX86InstComments(MI, *CommentStream, getRegisterName);
Chris Lattner76c564b2010-04-04 04:47:45 +000060}
Bill Wendlingbc3f7902011-04-07 21:20:06 +000061
Chris Lattner76c564b2010-04-04 04:47:45 +000062void X86ATTInstPrinter::printSSECC(const MCInst *MI, unsigned Op,
63 raw_ostream &O) {
Craig Topperf1c20162012-10-09 05:26:13 +000064 int64_t Imm = MI->getOperand(Op).getImm() & 0xf;
65 switch (Imm) {
Craig Topper4ed72782012-02-05 05:38:58 +000066 default: llvm_unreachable("Invalid ssecc argument!");
Elena Demikhovsky1adc1d52012-02-08 08:37:26 +000067 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;
Craig Topperf1c20162012-10-09 05:26:13 +000083 }
84}
85
86void 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;
Elena Demikhovsky1adc1d52012-02-08 08:37:26 +0000107 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;
Chris Lattner8d284c72009-06-19 23:59:57 +0000123 }
124}
125
Chad Rosier38e05a92012-09-10 22:50:57 +0000126/// printPCRelImm - This is used to print an immediate value that ends up
Chris Lattner6211d7b2009-12-22 00:44:05 +0000127/// being encoded as a pc-relative value (e.g. for jumps and calls). These
128/// print slightly differently than normal immediates. For example, a $ is not
129/// emitted.
Chad Rosier38e05a92012-09-10 22:50:57 +0000130void X86ATTInstPrinter::printPCRelImm(const MCInst *MI, unsigned OpNo,
131 raw_ostream &O) {
Chris Lattner9c211962009-06-20 19:34:09 +0000132 const MCOperand &Op = MI->getOperand(OpNo);
Chris Lattner9c211962009-06-20 19:34:09 +0000133 if (Op.isImm())
Kevin Enderby168ffb32012-12-05 18:13:19 +0000134 O << formatImm(Op.getImm());
Chris Lattneraa398f52009-09-14 01:34:40 +0000135 else {
136 assert(Op.isExpr() && "unknown pcrel immediate operand");
Kevin Enderby6fbcd8d2012-02-23 18:18:17 +0000137 // If a symbolic branch target was added as a constant expression then print
138 // that address in hex.
139 const MCConstantExpr *BranchTarget = dyn_cast<MCConstantExpr>(Op.getExpr());
140 int64_t Address;
141 if (BranchTarget && BranchTarget->EvaluateAsAbsolute(Address)) {
142 O << "0x";
143 O.write_hex(Address);
144 }
145 else {
146 // Otherwise, just print the expression.
147 O << *Op.getExpr();
148 }
Chris Lattneraa398f52009-09-14 01:34:40 +0000149 }
Chris Lattner9c211962009-06-20 19:34:09 +0000150}
151
Chris Lattner76c564b2010-04-04 04:47:45 +0000152void X86ATTInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
153 raw_ostream &O) {
Chris Lattner46820152009-06-20 00:49:26 +0000154 const MCOperand &Op = MI->getOperand(OpNo);
155 if (Op.isReg()) {
Kevin Enderbydccdac62012-10-23 22:52:52 +0000156 printRegName(O, Op.getReg());
Chris Lattner46820152009-06-20 00:49:26 +0000157 } else if (Op.isImm()) {
Kevin Enderby5b03f722011-09-02 20:01:23 +0000158 // Print X86 immediates as signed values.
Kevin Enderbydccdac62012-10-23 22:52:52 +0000159 O << markup("<imm:")
Kevin Enderby168ffb32012-12-05 18:13:19 +0000160 << '$' << formatImm((int64_t)Op.getImm())
Kevin Enderbydccdac62012-10-23 22:52:52 +0000161 << markup(">");
Chris Lattner482bf692010-02-10 00:10:18 +0000162
163 if (CommentStream && (Op.getImm() > 255 || Op.getImm() < -256))
Benjamin Kramerf3da5292011-11-05 08:57:40 +0000164 *CommentStream << format("imm = 0x%" PRIX64 "\n", (uint64_t)Op.getImm());
Chris Lattner482bf692010-02-10 00:10:18 +0000165
Chris Lattneraa398f52009-09-14 01:34:40 +0000166 } else {
167 assert(Op.isExpr() && "unknown operand kind in printOperand");
Kevin Enderbydccdac62012-10-23 22:52:52 +0000168 O << markup("<imm:")
169 << '$' << *Op.getExpr()
170 << markup(">");
Chris Lattner46820152009-06-20 00:49:26 +0000171 }
Chris Lattner8d284c72009-06-19 23:59:57 +0000172}
173
Chris Lattnerf4693072010-07-08 23:46:44 +0000174void X86ATTInstPrinter::printMemReference(const MCInst *MI, unsigned Op,
175 raw_ostream &O) {
Chris Lattner46820152009-06-20 00:49:26 +0000176 const MCOperand &BaseReg = MI->getOperand(Op);
177 const MCOperand &IndexReg = MI->getOperand(Op+2);
178 const MCOperand &DispSpec = MI->getOperand(Op+3);
Chris Lattnerf4693072010-07-08 23:46:44 +0000179 const MCOperand &SegReg = MI->getOperand(Op+4);
180
Kevin Enderbydccdac62012-10-23 22:52:52 +0000181 O << markup("<mem:");
Kevin Enderby62183c42012-10-22 22:31:46 +0000182
Chris Lattnerf4693072010-07-08 23:46:44 +0000183 // If this has a segment register, print it.
184 if (SegReg.getReg()) {
185 printOperand(MI, Op+4, O);
186 O << ':';
187 }
Chris Lattner46820152009-06-20 00:49:26 +0000188
Chris Lattner46820152009-06-20 00:49:26 +0000189 if (DispSpec.isImm()) {
190 int64_t DispVal = DispSpec.getImm();
191 if (DispVal || (!IndexReg.getReg() && !BaseReg.getReg()))
Kevin Enderby168ffb32012-12-05 18:13:19 +0000192 O << formatImm(DispVal);
Chris Lattner46820152009-06-20 00:49:26 +0000193 } else {
Chris Lattner24083062009-09-09 00:40:31 +0000194 assert(DispSpec.isExpr() && "non-immediate displacement for LEA?");
Chris Lattnerc8f77172010-01-18 00:37:40 +0000195 O << *DispSpec.getExpr();
Chris Lattner46820152009-06-20 00:49:26 +0000196 }
197
198 if (IndexReg.getReg() || BaseReg.getReg()) {
Chris Lattner46820152009-06-20 00:49:26 +0000199 O << '(';
200 if (BaseReg.getReg())
Chris Lattner76c564b2010-04-04 04:47:45 +0000201 printOperand(MI, Op, O);
Chris Lattner46820152009-06-20 00:49:26 +0000202
203 if (IndexReg.getReg()) {
204 O << ',';
Chris Lattner76c564b2010-04-04 04:47:45 +0000205 printOperand(MI, Op+2, O);
Chris Lattnerb4b5c102009-06-20 08:13:12 +0000206 unsigned ScaleVal = MI->getOperand(Op+1).getImm();
Kevin Enderby62183c42012-10-22 22:31:46 +0000207 if (ScaleVal != 1) {
Kevin Enderbydccdac62012-10-23 22:52:52 +0000208 O << ','
209 << markup("<imm:")
Kevin Enderby168ffb32012-12-05 18:13:19 +0000210 << ScaleVal // never printed in hex.
Kevin Enderbydccdac62012-10-23 22:52:52 +0000211 << markup(">");
Kevin Enderby62183c42012-10-22 22:31:46 +0000212 }
Chris Lattner46820152009-06-20 00:49:26 +0000213 }
214 O << ')';
215 }
Kevin Enderby62183c42012-10-22 22:31:46 +0000216
Kevin Enderbydccdac62012-10-23 22:52:52 +0000217 O << markup(">");
Chris Lattner8d284c72009-06-19 23:59:57 +0000218}