blob: 2edabf84b6f5f18b4059a4c286fde1648cc1ed95 [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"
Chris Lattner7a05e6d2010-08-28 20:42:31 +000017#include "X86InstComments.h"
Evan Cheng3ddfbd32011-07-06 22:01:53 +000018#include "MCTargetDesc/X86MCTargetDesc.h"
Chris Lattnera08186a2009-06-19 00:47:59 +000019#include "llvm/MC/MCInst.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"
Torok Edwin6dd27302009-07-08 18:01:40 +000022#include "llvm/Support/ErrorHandling.h"
Chris Lattner482bf692010-02-10 00:10:18 +000023#include "llvm/Support/Format.h"
David Greenea31f96c2009-07-14 20:18:05 +000024#include "llvm/Support/FormattedStream.h"
Bill Wendlingbc3f7902011-04-07 21:20:06 +000025#include <map>
Chris Lattnera08186a2009-06-19 00:47:59 +000026using namespace llvm;
27
Chris Lattner8d284c72009-06-19 23:59:57 +000028// Include the auto-generated portion of the assembly writer.
Chris Lattnerb1913c42010-02-11 22:57:32 +000029#define GET_INSTRUCTION_NAME
Bill Wendlingbc3f7902011-04-07 21:20:06 +000030#define PRINT_ALIAS_INSTR
Chris Lattner8d284c72009-06-19 23:59:57 +000031#include "X86GenAsmWriter.inc"
Bill Wendlingbc3f7902011-04-07 21:20:06 +000032
Evan Chengab37af92011-07-06 19:45:42 +000033X86ATTInstPrinter::X86ATTInstPrinter(const MCAsmInfo &MAI)
Bill Wendlingbc3f7902011-04-07 21:20:06 +000034 : MCInstPrinter(MAI) {
Bill Wendlingbc3f7902011-04-07 21:20:06 +000035}
Chris Lattner8d284c72009-06-19 23:59:57 +000036
Rafael Espindolad6860522011-06-02 02:34:55 +000037void X86ATTInstPrinter::printRegName(raw_ostream &OS,
38 unsigned RegNo) const {
39 OS << '%' << getRegisterName(RegNo);
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) {
Eric Christopher2e3fbaa2011-04-18 21:28:11 +000044 // Try to print any aliases first.
45 if (!printAliasInstr(MI, OS))
Bill Wendling7e07d6f2011-04-14 01:11:51 +000046 printInstruction(MI, OS);
Chris Lattner7a05e6d2010-08-28 20:42:31 +000047
Kevin Enderby6fbcd8d2012-02-23 18:18:17 +000048 // Next always print the annotation.
49 printAnnotation(OS, Annot);
50
Chris Lattner7a05e6d2010-08-28 20:42:31 +000051 // If verbose assembly is enabled, we can print some informative comments.
Kevin Enderby6fbcd8d2012-02-23 18:18:17 +000052 if (CommentStream)
Chris Lattner7a05e6d2010-08-28 20:42:31 +000053 EmitAnyX86InstComments(MI, *CommentStream, getRegisterName);
Chris Lattner76c564b2010-04-04 04:47:45 +000054}
Bill Wendlingbc3f7902011-04-07 21:20:06 +000055
Chris Lattnerb1913c42010-02-11 22:57:32 +000056StringRef X86ATTInstPrinter::getOpcodeName(unsigned Opcode) const {
57 return getInstructionName(Opcode);
58}
59
Chris Lattner76c564b2010-04-04 04:47:45 +000060void X86ATTInstPrinter::printSSECC(const MCInst *MI, unsigned Op,
61 raw_ostream &O) {
Chris Lattnera0022a72009-06-20 07:03:18 +000062 switch (MI->getOperand(Op).getImm()) {
Craig Topper4ed72782012-02-05 05:38:58 +000063 default: llvm_unreachable("Invalid ssecc argument!");
Elena Demikhovsky1adc1d52012-02-08 08:37:26 +000064 case 0: O << "eq"; break;
65 case 1: O << "lt"; break;
66 case 2: O << "le"; break;
67 case 3: O << "unord"; break;
68 case 4: O << "neq"; break;
69 case 5: O << "nlt"; break;
70 case 6: O << "nle"; break;
71 case 7: O << "ord"; break;
72 case 8: O << "eq_uq"; break;
73 case 9: O << "nge"; break;
74 case 0xa: O << "ngt"; break;
75 case 0xb: O << "false"; break;
76 case 0xc: O << "neq_oq"; break;
77 case 0xd: O << "ge"; break;
78 case 0xe: O << "gt"; break;
79 case 0xf: O << "true"; break;
80 case 0x10: O << "eq_os"; break;
81 case 0x11: O << "lt_oq"; break;
82 case 0x12: O << "le_oq"; break;
83 case 0x13: O << "unord_s"; break;
84 case 0x14: O << "neq_us"; break;
85 case 0x15: O << "nlt_uq"; break;
86 case 0x16: O << "nle_uq"; break;
87 case 0x17: O << "ord_s"; break;
88 case 0x18: O << "eq_us"; break;
89 case 0x19: O << "nge_uq"; break;
90 case 0x1a: O << "ngt_uq"; break;
91 case 0x1b: O << "false_os"; break;
92 case 0x1c: O << "neq_os"; break;
93 case 0x1d: O << "ge_oq"; break;
94 case 0x1e: O << "gt_oq"; break;
95 case 0x1f: O << "true_us"; break;
Chris Lattner8d284c72009-06-19 23:59:57 +000096 }
97}
98
Chris Lattner9c211962009-06-20 19:34:09 +000099/// print_pcrel_imm - This is used to print an immediate value that ends up
Chris Lattner6211d7b2009-12-22 00:44:05 +0000100/// being encoded as a pc-relative value (e.g. for jumps and calls). These
101/// print slightly differently than normal immediates. For example, a $ is not
102/// emitted.
Chris Lattner76c564b2010-04-04 04:47:45 +0000103void X86ATTInstPrinter::print_pcrel_imm(const MCInst *MI, unsigned OpNo,
104 raw_ostream &O) {
Chris Lattner9c211962009-06-20 19:34:09 +0000105 const MCOperand &Op = MI->getOperand(OpNo);
Chris Lattner9c211962009-06-20 19:34:09 +0000106 if (Op.isImm())
Kevin Enderby6fbcd8d2012-02-23 18:18:17 +0000107 O << Op.getImm();
Chris Lattneraa398f52009-09-14 01:34:40 +0000108 else {
109 assert(Op.isExpr() && "unknown pcrel immediate operand");
Kevin Enderby6fbcd8d2012-02-23 18:18:17 +0000110 // If a symbolic branch target was added as a constant expression then print
111 // that address in hex.
112 const MCConstantExpr *BranchTarget = dyn_cast<MCConstantExpr>(Op.getExpr());
113 int64_t Address;
114 if (BranchTarget && BranchTarget->EvaluateAsAbsolute(Address)) {
115 O << "0x";
116 O.write_hex(Address);
117 }
118 else {
119 // Otherwise, just print the expression.
120 O << *Op.getExpr();
121 }
Chris Lattneraa398f52009-09-14 01:34:40 +0000122 }
Chris Lattner9c211962009-06-20 19:34:09 +0000123}
124
Chris Lattner76c564b2010-04-04 04:47:45 +0000125void X86ATTInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
126 raw_ostream &O) {
Chris Lattner46820152009-06-20 00:49:26 +0000127 const MCOperand &Op = MI->getOperand(OpNo);
128 if (Op.isReg()) {
Chris Lattner56950c62009-09-13 20:15:16 +0000129 O << '%' << getRegisterName(Op.getReg());
Chris Lattner46820152009-06-20 00:49:26 +0000130 } else if (Op.isImm()) {
Kevin Enderby5b03f722011-09-02 20:01:23 +0000131 // Print X86 immediates as signed values.
132 O << '$' << (int64_t)Op.getImm();
Chris Lattner482bf692010-02-10 00:10:18 +0000133
134 if (CommentStream && (Op.getImm() > 255 || Op.getImm() < -256))
Benjamin Kramerf3da5292011-11-05 08:57:40 +0000135 *CommentStream << format("imm = 0x%" PRIX64 "\n", (uint64_t)Op.getImm());
Chris Lattner482bf692010-02-10 00:10:18 +0000136
Chris Lattneraa398f52009-09-14 01:34:40 +0000137 } else {
138 assert(Op.isExpr() && "unknown operand kind in printOperand");
Chris Lattnerc8f77172010-01-18 00:37:40 +0000139 O << '$' << *Op.getExpr();
Chris Lattner46820152009-06-20 00:49:26 +0000140 }
Chris Lattner8d284c72009-06-19 23:59:57 +0000141}
142
Chris Lattnerf4693072010-07-08 23:46:44 +0000143void X86ATTInstPrinter::printMemReference(const MCInst *MI, unsigned Op,
144 raw_ostream &O) {
Chris Lattner46820152009-06-20 00:49:26 +0000145 const MCOperand &BaseReg = MI->getOperand(Op);
146 const MCOperand &IndexReg = MI->getOperand(Op+2);
147 const MCOperand &DispSpec = MI->getOperand(Op+3);
Chris Lattnerf4693072010-07-08 23:46:44 +0000148 const MCOperand &SegReg = MI->getOperand(Op+4);
149
150 // If this has a segment register, print it.
151 if (SegReg.getReg()) {
152 printOperand(MI, Op+4, O);
153 O << ':';
154 }
Chris Lattner46820152009-06-20 00:49:26 +0000155
Chris Lattner46820152009-06-20 00:49:26 +0000156 if (DispSpec.isImm()) {
157 int64_t DispVal = DispSpec.getImm();
158 if (DispVal || (!IndexReg.getReg() && !BaseReg.getReg()))
159 O << DispVal;
160 } else {
Chris Lattner24083062009-09-09 00:40:31 +0000161 assert(DispSpec.isExpr() && "non-immediate displacement for LEA?");
Chris Lattnerc8f77172010-01-18 00:37:40 +0000162 O << *DispSpec.getExpr();
Chris Lattner46820152009-06-20 00:49:26 +0000163 }
164
165 if (IndexReg.getReg() || BaseReg.getReg()) {
Chris Lattner46820152009-06-20 00:49:26 +0000166 O << '(';
167 if (BaseReg.getReg())
Chris Lattner76c564b2010-04-04 04:47:45 +0000168 printOperand(MI, Op, O);
Chris Lattner46820152009-06-20 00:49:26 +0000169
170 if (IndexReg.getReg()) {
171 O << ',';
Chris Lattner76c564b2010-04-04 04:47:45 +0000172 printOperand(MI, Op+2, O);
Chris Lattnerb4b5c102009-06-20 08:13:12 +0000173 unsigned ScaleVal = MI->getOperand(Op+1).getImm();
174 if (ScaleVal != 1)
Chris Lattner46820152009-06-20 00:49:26 +0000175 O << ',' << ScaleVal;
176 }
177 O << ')';
178 }
Chris Lattner8d284c72009-06-19 23:59:57 +0000179}