blob: f9ab5aeee122cdec0ea209b2802fccb8a12f400e [file] [log] [blame]
Chris Lattnera93dcf12009-09-20 07:28:26 +00001//===-- X86IntelInstPrinter.cpp - AT&T 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//
10// This file includes code for rendering MCInst instances as AT&T-style
11// assembly.
12//
13//===----------------------------------------------------------------------===//
14
15#define DEBUG_TYPE "asm-printer"
16#include "X86IntelInstPrinter.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 Lattner44790342009-09-20 07:17:49 +000019#include "llvm/MC/MCInst.h"
20#include "llvm/MC/MCAsmInfo.h"
21#include "llvm/MC/MCExpr.h"
22#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
27// Include the auto-generated portion of the assembly writer.
Chris Lattnerb1913c42010-02-11 22:57:32 +000028#define GET_INSTRUCTION_NAME
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) {
Chris Lattner70129162010-04-04 05:04:31 +000037 printInstruction(MI, OS);
Chris Lattner7a05e6d2010-08-28 20:42:31 +000038
39 // If verbose assembly is enabled, we can print some informative comments.
Owen Andersond1814792011-09-15 18:36:29 +000040 if (CommentStream) {
Owen Anderson69fa8ff2011-09-21 00:25:23 +000041 printAnnotation(OS, Annot);
Chris Lattner7a05e6d2010-08-28 20:42:31 +000042 EmitAnyX86InstComments(MI, *CommentStream, getRegisterName);
Owen Andersond1814792011-09-15 18:36:29 +000043 }
Chris Lattner76c564b2010-04-04 04:47:45 +000044}
Chris Lattnerb1913c42010-02-11 22:57:32 +000045StringRef X86IntelInstPrinter::getOpcodeName(unsigned Opcode) const {
46 return getInstructionName(Opcode);
47}
Chris Lattner44790342009-09-20 07:17:49 +000048
Chris Lattner76c564b2010-04-04 04:47:45 +000049void X86IntelInstPrinter::printSSECC(const MCInst *MI, unsigned Op,
50 raw_ostream &O) {
Chris Lattner44790342009-09-20 07:17:49 +000051 switch (MI->getOperand(Op).getImm()) {
Chris Lattner76c564b2010-04-04 04:47:45 +000052 default: assert(0 && "Invalid ssecc argument!");
Chris Lattner44790342009-09-20 07:17:49 +000053 case 0: O << "eq"; break;
54 case 1: O << "lt"; break;
55 case 2: O << "le"; break;
56 case 3: O << "unord"; break;
57 case 4: O << "neq"; break;
58 case 5: O << "nlt"; break;
59 case 6: O << "nle"; break;
60 case 7: O << "ord"; break;
61 }
62}
63
Chris Lattner44790342009-09-20 07:17:49 +000064/// print_pcrel_imm - This is used to print an immediate value that ends up
Chris Lattner13306a12009-09-20 07:47:59 +000065/// being encoded as a pc-relative value.
Chris Lattner76c564b2010-04-04 04:47:45 +000066void X86IntelInstPrinter::print_pcrel_imm(const MCInst *MI, unsigned OpNo,
67 raw_ostream &O) {
Chris Lattner44790342009-09-20 07:17:49 +000068 const MCOperand &Op = MI->getOperand(OpNo);
69 if (Op.isImm())
70 O << Op.getImm();
71 else {
72 assert(Op.isExpr() && "unknown pcrel immediate operand");
Chris Lattnerc8f77172010-01-18 00:37:40 +000073 O << *Op.getExpr();
Chris Lattner44790342009-09-20 07:17:49 +000074 }
75}
76
77static void PrintRegName(raw_ostream &O, StringRef RegName) {
78 for (unsigned i = 0, e = RegName.size(); i != e; ++i)
79 O << (char)toupper(RegName[i]);
80}
81
82void X86IntelInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
Chris Lattner76c564b2010-04-04 04:47:45 +000083 raw_ostream &O) {
Chris Lattner44790342009-09-20 07:17:49 +000084 const MCOperand &Op = MI->getOperand(OpNo);
85 if (Op.isReg()) {
86 PrintRegName(O, getRegisterName(Op.getReg()));
87 } else if (Op.isImm()) {
88 O << Op.getImm();
89 } else {
90 assert(Op.isExpr() && "unknown operand kind in printOperand");
Chris Lattnerc8f77172010-01-18 00:37:40 +000091 O << *Op.getExpr();
Chris Lattner44790342009-09-20 07:17:49 +000092 }
93}
94
Chris Lattnerf4693072010-07-08 23:46:44 +000095void X86IntelInstPrinter::printMemReference(const MCInst *MI, unsigned Op,
96 raw_ostream &O) {
Chris Lattner44790342009-09-20 07:17:49 +000097 const MCOperand &BaseReg = MI->getOperand(Op);
98 unsigned ScaleVal = MI->getOperand(Op+1).getImm();
99 const MCOperand &IndexReg = MI->getOperand(Op+2);
100 const MCOperand &DispSpec = MI->getOperand(Op+3);
Chris Lattnerf4693072010-07-08 23:46:44 +0000101 const MCOperand &SegReg = MI->getOperand(Op+4);
102
103 // If this has a segment register, print it.
104 if (SegReg.getReg()) {
105 printOperand(MI, Op+4, O);
106 O << ':';
107 }
Chris Lattner44790342009-09-20 07:17:49 +0000108
109 O << '[';
110
111 bool NeedPlus = false;
112 if (BaseReg.getReg()) {
Chris Lattner76c564b2010-04-04 04:47:45 +0000113 printOperand(MI, Op, O);
Chris Lattner44790342009-09-20 07:17:49 +0000114 NeedPlus = true;
115 }
116
117 if (IndexReg.getReg()) {
118 if (NeedPlus) O << " + ";
119 if (ScaleVal != 1)
120 O << ScaleVal << '*';
Chris Lattner76c564b2010-04-04 04:47:45 +0000121 printOperand(MI, Op+2, O);
Chris Lattner44790342009-09-20 07:17:49 +0000122 NeedPlus = true;
123 }
124
Chris Lattnerf4693072010-07-08 23:46:44 +0000125
Chris Lattner44790342009-09-20 07:17:49 +0000126 if (!DispSpec.isImm()) {
127 if (NeedPlus) O << " + ";
128 assert(DispSpec.isExpr() && "non-immediate displacement for LEA?");
Chris Lattnerc8f77172010-01-18 00:37:40 +0000129 O << *DispSpec.getExpr();
Chris Lattner44790342009-09-20 07:17:49 +0000130 } else {
131 int64_t DispVal = DispSpec.getImm();
132 if (DispVal || (!IndexReg.getReg() && !BaseReg.getReg())) {
133 if (NeedPlus) {
134 if (DispVal > 0)
135 O << " + ";
136 else {
137 O << " - ";
138 DispVal = -DispVal;
139 }
140 }
141 O << DispVal;
142 }
143 }
144
145 O << ']';
146}