blob: 5f581bab3906d44c9dd78269d402b9222b2401a2 [file] [log] [blame]
Chris Lattnerb3c85472009-09-20 07:28:26 +00001//===-- X86IntelInstPrinter.cpp - AT&T assembly instruction printing ------===//
Chris Lattnere895c612009-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 Lattner6aa928d2010-08-28 20:42:31 +000017#include "X86InstComments.h"
Bill Wendling44dcfd32011-04-07 21:20:06 +000018#include "X86Subtarget.h"
Chris Lattnere895c612009-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"
24#include "X86GenInstrNames.inc"
Douglas Gregore7358822011-01-17 19:17:01 +000025#include <cctype>
Chris Lattnere895c612009-09-20 07:17:49 +000026using namespace llvm;
27
28// Include the auto-generated portion of the assembly writer.
Chris Lattner0d7b0aa2010-02-11 22:57:32 +000029#define GET_INSTRUCTION_NAME
Chris Lattnere895c612009-09-20 07:17:49 +000030#include "X86GenAsmWriter1.inc"
Chris Lattnere895c612009-09-20 07:17:49 +000031
Rafael Espindolacde4ce42011-06-02 02:34:55 +000032void X86IntelInstPrinter::printRegName(raw_ostream &OS, unsigned RegNo) const {
33 OS << getRegisterName(RegNo);
Rafael Espindola6e032942011-05-30 20:20:15 +000034}
35
Chris Lattnerd3740872010-04-04 05:04:31 +000036void X86IntelInstPrinter::printInst(const MCInst *MI, raw_ostream &OS) {
37 printInstruction(MI, OS);
Chris Lattner6aa928d2010-08-28 20:42:31 +000038
39 // If verbose assembly is enabled, we can print some informative comments.
40 if (CommentStream)
41 EmitAnyX86InstComments(MI, *CommentStream, getRegisterName);
Chris Lattner35c33bd2010-04-04 04:47:45 +000042}
Chris Lattner0d7b0aa2010-02-11 22:57:32 +000043StringRef X86IntelInstPrinter::getOpcodeName(unsigned Opcode) const {
44 return getInstructionName(Opcode);
45}
Chris Lattnere895c612009-09-20 07:17:49 +000046
Chris Lattner35c33bd2010-04-04 04:47:45 +000047void X86IntelInstPrinter::printSSECC(const MCInst *MI, unsigned Op,
48 raw_ostream &O) {
Chris Lattnere895c612009-09-20 07:17:49 +000049 switch (MI->getOperand(Op).getImm()) {
Chris Lattner35c33bd2010-04-04 04:47:45 +000050 default: assert(0 && "Invalid ssecc argument!");
Chris Lattnere895c612009-09-20 07:17:49 +000051 case 0: O << "eq"; break;
52 case 1: O << "lt"; break;
53 case 2: O << "le"; break;
54 case 3: O << "unord"; break;
55 case 4: O << "neq"; break;
56 case 5: O << "nlt"; break;
57 case 6: O << "nle"; break;
58 case 7: O << "ord"; break;
59 }
60}
61
Chris Lattnere895c612009-09-20 07:17:49 +000062/// print_pcrel_imm - This is used to print an immediate value that ends up
Chris Lattner70576412009-09-20 07:47:59 +000063/// being encoded as a pc-relative value.
Chris Lattner35c33bd2010-04-04 04:47:45 +000064void X86IntelInstPrinter::print_pcrel_imm(const MCInst *MI, unsigned OpNo,
65 raw_ostream &O) {
Chris Lattnere895c612009-09-20 07:17:49 +000066 const MCOperand &Op = MI->getOperand(OpNo);
67 if (Op.isImm())
68 O << Op.getImm();
69 else {
70 assert(Op.isExpr() && "unknown pcrel immediate operand");
Chris Lattner8cb9a3b2010-01-18 00:37:40 +000071 O << *Op.getExpr();
Chris Lattnere895c612009-09-20 07:17:49 +000072 }
73}
74
75static void PrintRegName(raw_ostream &O, StringRef RegName) {
76 for (unsigned i = 0, e = RegName.size(); i != e; ++i)
77 O << (char)toupper(RegName[i]);
78}
79
80void X86IntelInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
Chris Lattner35c33bd2010-04-04 04:47:45 +000081 raw_ostream &O) {
Chris Lattnere895c612009-09-20 07:17:49 +000082 const MCOperand &Op = MI->getOperand(OpNo);
83 if (Op.isReg()) {
84 PrintRegName(O, getRegisterName(Op.getReg()));
85 } else if (Op.isImm()) {
86 O << Op.getImm();
87 } else {
88 assert(Op.isExpr() && "unknown operand kind in printOperand");
Chris Lattner8cb9a3b2010-01-18 00:37:40 +000089 O << *Op.getExpr();
Chris Lattnere895c612009-09-20 07:17:49 +000090 }
91}
92
Chris Lattner599b5312010-07-08 23:46:44 +000093void X86IntelInstPrinter::printMemReference(const MCInst *MI, unsigned Op,
94 raw_ostream &O) {
Chris Lattnere895c612009-09-20 07:17:49 +000095 const MCOperand &BaseReg = MI->getOperand(Op);
96 unsigned ScaleVal = MI->getOperand(Op+1).getImm();
97 const MCOperand &IndexReg = MI->getOperand(Op+2);
98 const MCOperand &DispSpec = MI->getOperand(Op+3);
Chris Lattner599b5312010-07-08 23:46:44 +000099 const MCOperand &SegReg = MI->getOperand(Op+4);
100
101 // If this has a segment register, print it.
102 if (SegReg.getReg()) {
103 printOperand(MI, Op+4, O);
104 O << ':';
105 }
Chris Lattnere895c612009-09-20 07:17:49 +0000106
107 O << '[';
108
109 bool NeedPlus = false;
110 if (BaseReg.getReg()) {
Chris Lattner35c33bd2010-04-04 04:47:45 +0000111 printOperand(MI, Op, O);
Chris Lattnere895c612009-09-20 07:17:49 +0000112 NeedPlus = true;
113 }
114
115 if (IndexReg.getReg()) {
116 if (NeedPlus) O << " + ";
117 if (ScaleVal != 1)
118 O << ScaleVal << '*';
Chris Lattner35c33bd2010-04-04 04:47:45 +0000119 printOperand(MI, Op+2, O);
Chris Lattnere895c612009-09-20 07:17:49 +0000120 NeedPlus = true;
121 }
122
Chris Lattner599b5312010-07-08 23:46:44 +0000123
Chris Lattnere895c612009-09-20 07:17:49 +0000124 if (!DispSpec.isImm()) {
125 if (NeedPlus) O << " + ";
126 assert(DispSpec.isExpr() && "non-immediate displacement for LEA?");
Chris Lattner8cb9a3b2010-01-18 00:37:40 +0000127 O << *DispSpec.getExpr();
Chris Lattnere895c612009-09-20 07:17:49 +0000128 } else {
129 int64_t DispVal = DispSpec.getImm();
130 if (DispVal || (!IndexReg.getReg() && !BaseReg.getReg())) {
131 if (NeedPlus) {
132 if (DispVal > 0)
133 O << " + ";
134 else {
135 O << " - ";
136 DispVal = -DispVal;
137 }
138 }
139 O << DispVal;
140 }
141 }
142
143 O << ']';
144}