blob: 048452985089d10ffcd41d1a5d352b555e94cc32 [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"
Chris Lattnere895c612009-09-20 07:17:49 +000018#include "llvm/MC/MCInst.h"
19#include "llvm/MC/MCAsmInfo.h"
20#include "llvm/MC/MCExpr.h"
21#include "llvm/Support/ErrorHandling.h"
22#include "llvm/Support/FormattedStream.h"
23#include "X86GenInstrNames.inc"
Douglas Gregore7358822011-01-17 19:17:01 +000024#include <cctype>
Chris Lattnere895c612009-09-20 07:17:49 +000025using namespace llvm;
26
27// Include the auto-generated portion of the assembly writer.
Chris Lattner0d7b0aa2010-02-11 22:57:32 +000028#define GET_INSTRUCTION_NAME
Chris Lattnere895c612009-09-20 07:17:49 +000029#include "X86GenAsmWriter1.inc"
Chris Lattnere895c612009-09-20 07:17:49 +000030
Chris Lattnerd3740872010-04-04 05:04:31 +000031void X86IntelInstPrinter::printInst(const MCInst *MI, raw_ostream &OS) {
32 printInstruction(MI, OS);
Chris Lattner6aa928d2010-08-28 20:42:31 +000033
34 // If verbose assembly is enabled, we can print some informative comments.
35 if (CommentStream)
36 EmitAnyX86InstComments(MI, *CommentStream, getRegisterName);
Chris Lattner35c33bd2010-04-04 04:47:45 +000037}
Chris Lattner0d7b0aa2010-02-11 22:57:32 +000038StringRef X86IntelInstPrinter::getOpcodeName(unsigned Opcode) const {
39 return getInstructionName(Opcode);
40}
Chris Lattnere895c612009-09-20 07:17:49 +000041
Chris Lattner35c33bd2010-04-04 04:47:45 +000042void X86IntelInstPrinter::printSSECC(const MCInst *MI, unsigned Op,
43 raw_ostream &O) {
Chris Lattnere895c612009-09-20 07:17:49 +000044 switch (MI->getOperand(Op).getImm()) {
Chris Lattner35c33bd2010-04-04 04:47:45 +000045 default: assert(0 && "Invalid ssecc argument!");
Chris Lattnere895c612009-09-20 07:17:49 +000046 case 0: O << "eq"; break;
47 case 1: O << "lt"; break;
48 case 2: O << "le"; break;
49 case 3: O << "unord"; break;
50 case 4: O << "neq"; break;
51 case 5: O << "nlt"; break;
52 case 6: O << "nle"; break;
53 case 7: O << "ord"; break;
54 }
55}
56
Chris Lattnere895c612009-09-20 07:17:49 +000057/// print_pcrel_imm - This is used to print an immediate value that ends up
Chris Lattner70576412009-09-20 07:47:59 +000058/// being encoded as a pc-relative value.
Chris Lattner35c33bd2010-04-04 04:47:45 +000059void X86IntelInstPrinter::print_pcrel_imm(const MCInst *MI, unsigned OpNo,
60 raw_ostream &O) {
Chris Lattnere895c612009-09-20 07:17:49 +000061 const MCOperand &Op = MI->getOperand(OpNo);
62 if (Op.isImm())
63 O << Op.getImm();
64 else {
65 assert(Op.isExpr() && "unknown pcrel immediate operand");
Chris Lattner8cb9a3b2010-01-18 00:37:40 +000066 O << *Op.getExpr();
Chris Lattnere895c612009-09-20 07:17:49 +000067 }
68}
69
70static void PrintRegName(raw_ostream &O, StringRef RegName) {
71 for (unsigned i = 0, e = RegName.size(); i != e; ++i)
72 O << (char)toupper(RegName[i]);
73}
74
75void X86IntelInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
Chris Lattner35c33bd2010-04-04 04:47:45 +000076 raw_ostream &O) {
Chris Lattnere895c612009-09-20 07:17:49 +000077 const MCOperand &Op = MI->getOperand(OpNo);
78 if (Op.isReg()) {
79 PrintRegName(O, getRegisterName(Op.getReg()));
80 } else if (Op.isImm()) {
81 O << Op.getImm();
82 } else {
83 assert(Op.isExpr() && "unknown operand kind in printOperand");
Chris Lattner8cb9a3b2010-01-18 00:37:40 +000084 O << *Op.getExpr();
Chris Lattnere895c612009-09-20 07:17:49 +000085 }
86}
87
Chris Lattner599b5312010-07-08 23:46:44 +000088void X86IntelInstPrinter::printMemReference(const MCInst *MI, unsigned Op,
89 raw_ostream &O) {
Chris Lattnere895c612009-09-20 07:17:49 +000090 const MCOperand &BaseReg = MI->getOperand(Op);
91 unsigned ScaleVal = MI->getOperand(Op+1).getImm();
92 const MCOperand &IndexReg = MI->getOperand(Op+2);
93 const MCOperand &DispSpec = MI->getOperand(Op+3);
Chris Lattner599b5312010-07-08 23:46:44 +000094 const MCOperand &SegReg = MI->getOperand(Op+4);
95
96 // If this has a segment register, print it.
97 if (SegReg.getReg()) {
98 printOperand(MI, Op+4, O);
99 O << ':';
100 }
Chris Lattnere895c612009-09-20 07:17:49 +0000101
102 O << '[';
103
104 bool NeedPlus = false;
105 if (BaseReg.getReg()) {
Chris Lattner35c33bd2010-04-04 04:47:45 +0000106 printOperand(MI, Op, O);
Chris Lattnere895c612009-09-20 07:17:49 +0000107 NeedPlus = true;
108 }
109
110 if (IndexReg.getReg()) {
111 if (NeedPlus) O << " + ";
112 if (ScaleVal != 1)
113 O << ScaleVal << '*';
Chris Lattner35c33bd2010-04-04 04:47:45 +0000114 printOperand(MI, Op+2, O);
Chris Lattnere895c612009-09-20 07:17:49 +0000115 NeedPlus = true;
116 }
117
Chris Lattner599b5312010-07-08 23:46:44 +0000118
Chris Lattnere895c612009-09-20 07:17:49 +0000119 if (!DispSpec.isImm()) {
120 if (NeedPlus) O << " + ";
121 assert(DispSpec.isExpr() && "non-immediate displacement for LEA?");
Chris Lattner8cb9a3b2010-01-18 00:37:40 +0000122 O << *DispSpec.getExpr();
Chris Lattnere895c612009-09-20 07:17:49 +0000123 } else {
124 int64_t DispVal = DispSpec.getImm();
125 if (DispVal || (!IndexReg.getReg() && !BaseReg.getReg())) {
126 if (NeedPlus) {
127 if (DispVal > 0)
128 O << " + ";
129 else {
130 O << " - ";
131 DispVal = -DispVal;
132 }
133 }
134 O << DispVal;
135 }
136 }
137
138 O << ']';
139}