76a1da49595f6638fbc8f7d8f3fb5067b3ee3fad
[oota-llvm.git] / lib / Target / X86 / InstPrinter / X86ATTInstPrinter.cpp
1 //===-- X86ATTInstPrinter.cpp - AT&T assembly instruction printing --------===//
2 //
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 "X86ATTInstPrinter.h"
17 #include "X86InstComments.h"
18 #include "MCTargetDesc/X86MCTargetDesc.h"
19 #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/Format.h"
24 #include "llvm/Support/FormattedStream.h"
25 #include <map>
26 using namespace llvm;
27
28 // Include the auto-generated portion of the assembly writer.
29 #define GET_INSTRUCTION_NAME
30 #define PRINT_ALIAS_INSTR
31 #include "X86GenAsmWriter.inc"
32
33 X86ATTInstPrinter::X86ATTInstPrinter(const MCAsmInfo &MAI)
34   : MCInstPrinter(MAI) {
35 }
36
37 void X86ATTInstPrinter::printRegName(raw_ostream &OS,
38                                      unsigned RegNo) const {
39   OS << '%' << getRegisterName(RegNo);
40 }
41
42 void X86ATTInstPrinter::printInst(const MCInst *MI, raw_ostream &OS) {
43   // Try to print any aliases first.
44   if (!printAliasInstr(MI, OS))
45     printInstruction(MI, OS);
46   
47   // If verbose assembly is enabled, we can print some informative comments.
48   if (CommentStream) {
49     printAnnotations(MI, *CommentStream);
50     EmitAnyX86InstComments(MI, *CommentStream, getRegisterName);
51   }
52 }
53
54 StringRef X86ATTInstPrinter::getOpcodeName(unsigned Opcode) const {
55   return getInstructionName(Opcode);
56 }
57
58 void X86ATTInstPrinter::printSSECC(const MCInst *MI, unsigned Op,
59                                    raw_ostream &O) {
60   switch (MI->getOperand(Op).getImm()) {
61   default: assert(0 && "Invalid ssecc argument!");
62   case 0: O << "eq"; break;
63   case 1: O << "lt"; break;
64   case 2: O << "le"; break;
65   case 3: O << "unord"; break;
66   case 4: O << "neq"; break;
67   case 5: O << "nlt"; break;
68   case 6: O << "nle"; break;
69   case 7: O << "ord"; break;
70   }
71 }
72
73 /// print_pcrel_imm - This is used to print an immediate value that ends up
74 /// being encoded as a pc-relative value (e.g. for jumps and calls).  These
75 /// print slightly differently than normal immediates.  For example, a $ is not
76 /// emitted.
77 void X86ATTInstPrinter::print_pcrel_imm(const MCInst *MI, unsigned OpNo,
78                                         raw_ostream &O) {
79   const MCOperand &Op = MI->getOperand(OpNo);
80   if (Op.isImm())
81     // Print this as a signed 32-bit value.
82     O << (int)Op.getImm();
83   else {
84     assert(Op.isExpr() && "unknown pcrel immediate operand");
85     O << *Op.getExpr();
86   }
87 }
88
89 void X86ATTInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
90                                      raw_ostream &O) {
91   const MCOperand &Op = MI->getOperand(OpNo);
92   if (Op.isReg()) {
93     O << '%' << getRegisterName(Op.getReg());
94   } else if (Op.isImm()) {
95     // Print X86 immediates as signed values.
96     O << '$' << (int64_t)Op.getImm();
97     
98     if (CommentStream && (Op.getImm() > 255 || Op.getImm() < -256))
99       *CommentStream << format("imm = 0x%llX\n", (long long)Op.getImm());
100     
101   } else {
102     assert(Op.isExpr() && "unknown operand kind in printOperand");
103     O << '$' << *Op.getExpr();
104   }
105 }
106
107 void X86ATTInstPrinter::printMemReference(const MCInst *MI, unsigned Op,
108                                           raw_ostream &O) {
109   const MCOperand &BaseReg  = MI->getOperand(Op);
110   const MCOperand &IndexReg = MI->getOperand(Op+2);
111   const MCOperand &DispSpec = MI->getOperand(Op+3);
112   const MCOperand &SegReg = MI->getOperand(Op+4);
113   
114   // If this has a segment register, print it.
115   if (SegReg.getReg()) {
116     printOperand(MI, Op+4, O);
117     O << ':';
118   }
119   
120   if (DispSpec.isImm()) {
121     int64_t DispVal = DispSpec.getImm();
122     if (DispVal || (!IndexReg.getReg() && !BaseReg.getReg()))
123       O << DispVal;
124   } else {
125     assert(DispSpec.isExpr() && "non-immediate displacement for LEA?");
126     O << *DispSpec.getExpr();
127   }
128   
129   if (IndexReg.getReg() || BaseReg.getReg()) {
130     O << '(';
131     if (BaseReg.getReg())
132       printOperand(MI, Op, O);
133     
134     if (IndexReg.getReg()) {
135       O << ',';
136       printOperand(MI, Op+2, O);
137       unsigned ScaleVal = MI->getOperand(Op+1).getImm();
138       if (ScaleVal != 1)
139         O << ',' << ScaleVal;
140     }
141     O << ')';
142   }
143 }