blob: 5625b0ea618f81446544f877be1d7c2c1db67bdd [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"
24using namespace llvm;
25
26// Include the auto-generated portion of the assembly writer.
27#define MachineInstr MCInst
Chris Lattner0d7b0aa2010-02-11 22:57:32 +000028#define GET_INSTRUCTION_NAME
Chris Lattnere895c612009-09-20 07:17:49 +000029#include "X86GenAsmWriter1.inc"
30#undef MachineInstr
31
Chris Lattnerd3740872010-04-04 05:04:31 +000032void X86IntelInstPrinter::printInst(const MCInst *MI, raw_ostream &OS) {
33 printInstruction(MI, OS);
Chris Lattner6aa928d2010-08-28 20:42:31 +000034
35 // If verbose assembly is enabled, we can print some informative comments.
36 if (CommentStream)
37 EmitAnyX86InstComments(MI, *CommentStream, getRegisterName);
Chris Lattner35c33bd2010-04-04 04:47:45 +000038}
Chris Lattner0d7b0aa2010-02-11 22:57:32 +000039StringRef X86IntelInstPrinter::getOpcodeName(unsigned Opcode) const {
40 return getInstructionName(Opcode);
41}
Chris Lattnere895c612009-09-20 07:17:49 +000042
Chris Lattner35c33bd2010-04-04 04:47:45 +000043void X86IntelInstPrinter::printSSECC(const MCInst *MI, unsigned Op,
44 raw_ostream &O) {
Chris Lattnere895c612009-09-20 07:17:49 +000045 switch (MI->getOperand(Op).getImm()) {
Chris Lattner35c33bd2010-04-04 04:47:45 +000046 default: assert(0 && "Invalid ssecc argument!");
Chris Lattnere895c612009-09-20 07:17:49 +000047 case 0: O << "eq"; break;
48 case 1: O << "lt"; break;
49 case 2: O << "le"; break;
50 case 3: O << "unord"; break;
51 case 4: O << "neq"; break;
52 case 5: O << "nlt"; break;
53 case 6: O << "nle"; break;
54 case 7: O << "ord"; break;
55 }
56}
57
Chris Lattnere895c612009-09-20 07:17:49 +000058/// print_pcrel_imm - This is used to print an immediate value that ends up
Chris Lattner70576412009-09-20 07:47:59 +000059/// being encoded as a pc-relative value.
Chris Lattner35c33bd2010-04-04 04:47:45 +000060void X86IntelInstPrinter::print_pcrel_imm(const MCInst *MI, unsigned OpNo,
61 raw_ostream &O) {
Chris Lattnere895c612009-09-20 07:17:49 +000062 const MCOperand &Op = MI->getOperand(OpNo);
63 if (Op.isImm())
64 O << Op.getImm();
65 else {
66 assert(Op.isExpr() && "unknown pcrel immediate operand");
Chris Lattner8cb9a3b2010-01-18 00:37:40 +000067 O << *Op.getExpr();
Chris Lattnere895c612009-09-20 07:17:49 +000068 }
69}
70
71static void PrintRegName(raw_ostream &O, StringRef RegName) {
72 for (unsigned i = 0, e = RegName.size(); i != e; ++i)
73 O << (char)toupper(RegName[i]);
74}
75
76void X86IntelInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
Chris Lattner35c33bd2010-04-04 04:47:45 +000077 raw_ostream &O) {
Chris Lattnere895c612009-09-20 07:17:49 +000078 const MCOperand &Op = MI->getOperand(OpNo);
79 if (Op.isReg()) {
80 PrintRegName(O, getRegisterName(Op.getReg()));
81 } else if (Op.isImm()) {
82 O << Op.getImm();
83 } else {
84 assert(Op.isExpr() && "unknown operand kind in printOperand");
Chris Lattner8cb9a3b2010-01-18 00:37:40 +000085 O << *Op.getExpr();
Chris Lattnere895c612009-09-20 07:17:49 +000086 }
87}
88
Chris Lattner599b5312010-07-08 23:46:44 +000089void X86IntelInstPrinter::printMemReference(const MCInst *MI, unsigned Op,
90 raw_ostream &O) {
Chris Lattnere895c612009-09-20 07:17:49 +000091 const MCOperand &BaseReg = MI->getOperand(Op);
92 unsigned ScaleVal = MI->getOperand(Op+1).getImm();
93 const MCOperand &IndexReg = MI->getOperand(Op+2);
94 const MCOperand &DispSpec = MI->getOperand(Op+3);
Chris Lattner599b5312010-07-08 23:46:44 +000095 const MCOperand &SegReg = MI->getOperand(Op+4);
96
97 // If this has a segment register, print it.
98 if (SegReg.getReg()) {
99 printOperand(MI, Op+4, O);
100 O << ':';
101 }
Chris Lattnere895c612009-09-20 07:17:49 +0000102
103 O << '[';
104
105 bool NeedPlus = false;
106 if (BaseReg.getReg()) {
Chris Lattner35c33bd2010-04-04 04:47:45 +0000107 printOperand(MI, Op, O);
Chris Lattnere895c612009-09-20 07:17:49 +0000108 NeedPlus = true;
109 }
110
111 if (IndexReg.getReg()) {
112 if (NeedPlus) O << " + ";
113 if (ScaleVal != 1)
114 O << ScaleVal << '*';
Chris Lattner35c33bd2010-04-04 04:47:45 +0000115 printOperand(MI, Op+2, O);
Chris Lattnere895c612009-09-20 07:17:49 +0000116 NeedPlus = true;
117 }
118
Chris Lattner599b5312010-07-08 23:46:44 +0000119
Chris Lattnere895c612009-09-20 07:17:49 +0000120 if (!DispSpec.isImm()) {
121 if (NeedPlus) O << " + ";
122 assert(DispSpec.isExpr() && "non-immediate displacement for LEA?");
Chris Lattner8cb9a3b2010-01-18 00:37:40 +0000123 O << *DispSpec.getExpr();
Chris Lattnere895c612009-09-20 07:17:49 +0000124 } else {
125 int64_t DispVal = DispSpec.getImm();
126 if (DispVal || (!IndexReg.getReg() && !BaseReg.getReg())) {
127 if (NeedPlus) {
128 if (DispVal > 0)
129 O << " + ";
130 else {
131 O << " - ";
132 DispVal = -DispVal;
133 }
134 }
135 O << DispVal;
136 }
137 }
138
139 O << ']';
140}