blob: 7f1f8a3c4cb7642193e52bcfcc97ee30b9cb2ef8 [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()) {
Craig Topper4ed72782012-02-05 05:38:58 +000052 default: llvm_unreachable("Invalid ssecc argument!");
Elena Demikhovsky1adc1d52012-02-08 08:37:26 +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 case 8: O << "eq_uq"; break;
62 case 9: O << "nge"; break;
63 case 0xa: O << "ngt"; break;
64 case 0xb: O << "false"; break;
65 case 0xc: O << "neq_oq"; break;
66 case 0xd: O << "ge"; break;
67 case 0xe: O << "gt"; break;
68 case 0xf: O << "true"; break;
69 case 0x10: O << "eq_os"; break;
70 case 0x11: O << "lt_oq"; break;
71 case 0x12: O << "le_oq"; break;
72 case 0x13: O << "unord_s"; break;
73 case 0x14: O << "neq_us"; break;
74 case 0x15: O << "nlt_uq"; break;
75 case 0x16: O << "nle_uq"; break;
76 case 0x17: O << "ord_s"; break;
77 case 0x18: O << "eq_us"; break;
78 case 0x19: O << "nge_uq"; break;
79 case 0x1a: O << "ngt_uq"; break;
80 case 0x1b: O << "false_os"; break;
81 case 0x1c: O << "neq_os"; break;
82 case 0x1d: O << "ge_oq"; break;
83 case 0x1e: O << "gt_oq"; break;
84 case 0x1f: O << "true_us"; break;
85
Chris Lattner44790342009-09-20 07:17:49 +000086 }
87}
88
Chris Lattner44790342009-09-20 07:17:49 +000089/// print_pcrel_imm - This is used to print an immediate value that ends up
Chris Lattner13306a12009-09-20 07:47:59 +000090/// being encoded as a pc-relative value.
Chris Lattner76c564b2010-04-04 04:47:45 +000091void X86IntelInstPrinter::print_pcrel_imm(const MCInst *MI, unsigned OpNo,
92 raw_ostream &O) {
Chris Lattner44790342009-09-20 07:17:49 +000093 const MCOperand &Op = MI->getOperand(OpNo);
94 if (Op.isImm())
95 O << Op.getImm();
96 else {
97 assert(Op.isExpr() && "unknown pcrel immediate operand");
Chris Lattnerc8f77172010-01-18 00:37:40 +000098 O << *Op.getExpr();
Chris Lattner44790342009-09-20 07:17:49 +000099 }
100}
101
102static void PrintRegName(raw_ostream &O, StringRef RegName) {
103 for (unsigned i = 0, e = RegName.size(); i != e; ++i)
104 O << (char)toupper(RegName[i]);
105}
106
107void X86IntelInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
Chris Lattner76c564b2010-04-04 04:47:45 +0000108 raw_ostream &O) {
Chris Lattner44790342009-09-20 07:17:49 +0000109 const MCOperand &Op = MI->getOperand(OpNo);
110 if (Op.isReg()) {
111 PrintRegName(O, getRegisterName(Op.getReg()));
112 } else if (Op.isImm()) {
113 O << Op.getImm();
114 } else {
115 assert(Op.isExpr() && "unknown operand kind in printOperand");
Chris Lattnerc8f77172010-01-18 00:37:40 +0000116 O << *Op.getExpr();
Chris Lattner44790342009-09-20 07:17:49 +0000117 }
118}
119
Chris Lattnerf4693072010-07-08 23:46:44 +0000120void X86IntelInstPrinter::printMemReference(const MCInst *MI, unsigned Op,
121 raw_ostream &O) {
Chris Lattner44790342009-09-20 07:17:49 +0000122 const MCOperand &BaseReg = MI->getOperand(Op);
123 unsigned ScaleVal = MI->getOperand(Op+1).getImm();
124 const MCOperand &IndexReg = MI->getOperand(Op+2);
125 const MCOperand &DispSpec = MI->getOperand(Op+3);
Chris Lattnerf4693072010-07-08 23:46:44 +0000126 const MCOperand &SegReg = MI->getOperand(Op+4);
127
128 // If this has a segment register, print it.
129 if (SegReg.getReg()) {
130 printOperand(MI, Op+4, O);
131 O << ':';
132 }
Chris Lattner44790342009-09-20 07:17:49 +0000133
134 O << '[';
135
136 bool NeedPlus = false;
137 if (BaseReg.getReg()) {
Chris Lattner76c564b2010-04-04 04:47:45 +0000138 printOperand(MI, Op, O);
Chris Lattner44790342009-09-20 07:17:49 +0000139 NeedPlus = true;
140 }
141
142 if (IndexReg.getReg()) {
143 if (NeedPlus) O << " + ";
144 if (ScaleVal != 1)
145 O << ScaleVal << '*';
Chris Lattner76c564b2010-04-04 04:47:45 +0000146 printOperand(MI, Op+2, O);
Chris Lattner44790342009-09-20 07:17:49 +0000147 NeedPlus = true;
148 }
149
Chris Lattnerf4693072010-07-08 23:46:44 +0000150
Chris Lattner44790342009-09-20 07:17:49 +0000151 if (!DispSpec.isImm()) {
152 if (NeedPlus) O << " + ";
153 assert(DispSpec.isExpr() && "non-immediate displacement for LEA?");
Chris Lattnerc8f77172010-01-18 00:37:40 +0000154 O << *DispSpec.getExpr();
Chris Lattner44790342009-09-20 07:17:49 +0000155 } else {
156 int64_t DispVal = DispSpec.getImm();
157 if (DispVal || (!IndexReg.getReg() && !BaseReg.getReg())) {
158 if (NeedPlus) {
159 if (DispVal > 0)
160 O << " + ";
161 else {
162 O << " - ";
163 DispVal = -DispVal;
164 }
165 }
166 O << DispVal;
167 }
168 }
169
170 O << ']';
171}