blob: 46e72f9f6028aa42bb231f28c6625ae8becbb0b2 [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"
Craig Topperdab9e352012-04-02 07:01:04 +000022#include "llvm/MC/MCInstrInfo.h"
Benjamin Kramer682de392012-03-30 23:13:40 +000023#include "llvm/MC/MCRegisterInfo.h"
Torok Edwin6dd27302009-07-08 18:01:40 +000024#include "llvm/Support/ErrorHandling.h"
Chris Lattner482bf692010-02-10 00:10:18 +000025#include "llvm/Support/Format.h"
David Greenea31f96c2009-07-14 20:18:05 +000026#include "llvm/Support/FormattedStream.h"
Bill Wendlingbc3f7902011-04-07 21:20:06 +000027#include <map>
Chris Lattnera08186a2009-06-19 00:47:59 +000028using namespace llvm;
29
Chris Lattner8d284c72009-06-19 23:59:57 +000030// Include the auto-generated portion of the assembly writer.
Bill Wendlingbc3f7902011-04-07 21:20:06 +000031#define PRINT_ALIAS_INSTR
Chris Lattner8d284c72009-06-19 23:59:57 +000032#include "X86GenAsmWriter.inc"
Bill Wendlingbc3f7902011-04-07 21:20:06 +000033
Rafael Espindolad6860522011-06-02 02:34:55 +000034void X86ATTInstPrinter::printRegName(raw_ostream &OS,
35 unsigned RegNo) const {
36 OS << '%' << getRegisterName(RegNo);
Rafael Espindola08600bc2011-05-30 20:20:15 +000037}
38
Owen Andersona0c3b972011-09-15 23:38:46 +000039void X86ATTInstPrinter::printInst(const MCInst *MI, raw_ostream &OS,
40 StringRef Annot) {
Eric Christopher2e3fbaa2011-04-18 21:28:11 +000041 // Try to print any aliases first.
42 if (!printAliasInstr(MI, OS))
Bill Wendling7e07d6f2011-04-14 01:11:51 +000043 printInstruction(MI, OS);
Chris Lattner7a05e6d2010-08-28 20:42:31 +000044
Kevin Enderby6fbcd8d2012-02-23 18:18:17 +000045 // 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}
Bill Wendlingbc3f7902011-04-07 21:20:06 +000052
Chris Lattner76c564b2010-04-04 04:47:45 +000053void X86ATTInstPrinter::printSSECC(const MCInst *MI, unsigned Op,
54 raw_ostream &O) {
Chris Lattnera0022a72009-06-20 07:03:18 +000055 switch (MI->getOperand(Op).getImm()) {
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;
73 case 0x10: O << "eq_os"; break;
74 case 0x11: O << "lt_oq"; break;
75 case 0x12: O << "le_oq"; break;
76 case 0x13: O << "unord_s"; break;
77 case 0x14: O << "neq_us"; break;
78 case 0x15: O << "nlt_uq"; break;
79 case 0x16: O << "nle_uq"; break;
80 case 0x17: O << "ord_s"; break;
81 case 0x18: O << "eq_us"; break;
82 case 0x19: O << "nge_uq"; break;
83 case 0x1a: O << "ngt_uq"; break;
84 case 0x1b: O << "false_os"; break;
85 case 0x1c: O << "neq_os"; break;
86 case 0x1d: O << "ge_oq"; break;
87 case 0x1e: O << "gt_oq"; break;
88 case 0x1f: O << "true_us"; break;
Chris Lattner8d284c72009-06-19 23:59:57 +000089 }
90}
91
Chad Rosier38e05a92012-09-10 22:50:57 +000092/// printPCRelImm - This is used to print an immediate value that ends up
Chris Lattner6211d7b2009-12-22 00:44:05 +000093/// being encoded as a pc-relative value (e.g. for jumps and calls). These
94/// print slightly differently than normal immediates. For example, a $ is not
95/// emitted.
Chad Rosier38e05a92012-09-10 22:50:57 +000096void X86ATTInstPrinter::printPCRelImm(const MCInst *MI, unsigned OpNo,
97 raw_ostream &O) {
Chris Lattner9c211962009-06-20 19:34:09 +000098 const MCOperand &Op = MI->getOperand(OpNo);
Chris Lattner9c211962009-06-20 19:34:09 +000099 if (Op.isImm())
Kevin Enderby6fbcd8d2012-02-23 18:18:17 +0000100 O << Op.getImm();
Chris Lattneraa398f52009-09-14 01:34:40 +0000101 else {
102 assert(Op.isExpr() && "unknown pcrel immediate operand");
Kevin Enderby6fbcd8d2012-02-23 18:18:17 +0000103 // If a symbolic branch target was added as a constant expression then print
104 // that address in hex.
105 const MCConstantExpr *BranchTarget = dyn_cast<MCConstantExpr>(Op.getExpr());
106 int64_t Address;
107 if (BranchTarget && BranchTarget->EvaluateAsAbsolute(Address)) {
108 O << "0x";
109 O.write_hex(Address);
110 }
111 else {
112 // Otherwise, just print the expression.
113 O << *Op.getExpr();
114 }
Chris Lattneraa398f52009-09-14 01:34:40 +0000115 }
Chris Lattner9c211962009-06-20 19:34:09 +0000116}
117
Chris Lattner76c564b2010-04-04 04:47:45 +0000118void X86ATTInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
119 raw_ostream &O) {
Chris Lattner46820152009-06-20 00:49:26 +0000120 const MCOperand &Op = MI->getOperand(OpNo);
121 if (Op.isReg()) {
Chris Lattner56950c62009-09-13 20:15:16 +0000122 O << '%' << getRegisterName(Op.getReg());
Chris Lattner46820152009-06-20 00:49:26 +0000123 } else if (Op.isImm()) {
Kevin Enderby5b03f722011-09-02 20:01:23 +0000124 // Print X86 immediates as signed values.
125 O << '$' << (int64_t)Op.getImm();
Chris Lattner482bf692010-02-10 00:10:18 +0000126
127 if (CommentStream && (Op.getImm() > 255 || Op.getImm() < -256))
Benjamin Kramerf3da5292011-11-05 08:57:40 +0000128 *CommentStream << format("imm = 0x%" PRIX64 "\n", (uint64_t)Op.getImm());
Chris Lattner482bf692010-02-10 00:10:18 +0000129
Chris Lattneraa398f52009-09-14 01:34:40 +0000130 } else {
131 assert(Op.isExpr() && "unknown operand kind in printOperand");
Chris Lattnerc8f77172010-01-18 00:37:40 +0000132 O << '$' << *Op.getExpr();
Chris Lattner46820152009-06-20 00:49:26 +0000133 }
Chris Lattner8d284c72009-06-19 23:59:57 +0000134}
135
Chris Lattnerf4693072010-07-08 23:46:44 +0000136void X86ATTInstPrinter::printMemReference(const MCInst *MI, unsigned Op,
137 raw_ostream &O) {
Chris Lattner46820152009-06-20 00:49:26 +0000138 const MCOperand &BaseReg = MI->getOperand(Op);
139 const MCOperand &IndexReg = MI->getOperand(Op+2);
140 const MCOperand &DispSpec = MI->getOperand(Op+3);
Chris Lattnerf4693072010-07-08 23:46:44 +0000141 const MCOperand &SegReg = MI->getOperand(Op+4);
142
143 // If this has a segment register, print it.
144 if (SegReg.getReg()) {
145 printOperand(MI, Op+4, O);
146 O << ':';
147 }
Chris Lattner46820152009-06-20 00:49:26 +0000148
Chris Lattner46820152009-06-20 00:49:26 +0000149 if (DispSpec.isImm()) {
150 int64_t DispVal = DispSpec.getImm();
151 if (DispVal || (!IndexReg.getReg() && !BaseReg.getReg()))
152 O << DispVal;
153 } else {
Chris Lattner24083062009-09-09 00:40:31 +0000154 assert(DispSpec.isExpr() && "non-immediate displacement for LEA?");
Chris Lattnerc8f77172010-01-18 00:37:40 +0000155 O << *DispSpec.getExpr();
Chris Lattner46820152009-06-20 00:49:26 +0000156 }
157
158 if (IndexReg.getReg() || BaseReg.getReg()) {
Chris Lattner46820152009-06-20 00:49:26 +0000159 O << '(';
160 if (BaseReg.getReg())
Chris Lattner76c564b2010-04-04 04:47:45 +0000161 printOperand(MI, Op, O);
Chris Lattner46820152009-06-20 00:49:26 +0000162
163 if (IndexReg.getReg()) {
164 O << ',';
Chris Lattner76c564b2010-04-04 04:47:45 +0000165 printOperand(MI, Op+2, O);
Chris Lattnerb4b5c102009-06-20 08:13:12 +0000166 unsigned ScaleVal = MI->getOperand(Op+1).getImm();
167 if (ScaleVal != 1)
Chris Lattner46820152009-06-20 00:49:26 +0000168 O << ',' << ScaleVal;
169 }
170 O << ')';
171 }
Chris Lattner8d284c72009-06-19 23:59:57 +0000172}