Make MCRegisterInfo available to the the MCInstPrinter.
[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 void X86ATTInstPrinter::printRegName(raw_ostream &OS,
34                                      unsigned RegNo) const {
35   OS << '%' << getRegisterName(RegNo);
36 }
37
38 void X86ATTInstPrinter::printInst(const MCInst *MI, raw_ostream &OS,
39                                   StringRef Annot) {
40   // Try to print any aliases first.
41   if (!printAliasInstr(MI, OS))
42     printInstruction(MI, OS);
43   
44   // Next always print the annotation.
45   printAnnotation(OS, Annot);
46
47   // If verbose assembly is enabled, we can print some informative comments.
48   if (CommentStream)
49     EmitAnyX86InstComments(MI, *CommentStream, getRegisterName);
50 }
51
52 StringRef X86ATTInstPrinter::getOpcodeName(unsigned Opcode) const {
53   return getInstructionName(Opcode);
54 }
55
56 void X86ATTInstPrinter::printSSECC(const MCInst *MI, unsigned Op,
57                                    raw_ostream &O) {
58   switch (MI->getOperand(Op).getImm()) {
59   default: llvm_unreachable("Invalid ssecc argument!");
60   case    0: O << "eq"; break;
61   case    1: O << "lt"; break;
62   case    2: O << "le"; break;
63   case    3: O << "unord"; break;
64   case    4: O << "neq"; break;
65   case    5: O << "nlt"; break;
66   case    6: O << "nle"; break;
67   case    7: O << "ord"; break;
68   case    8: O << "eq_uq"; break;
69   case    9: O << "nge"; break;
70   case  0xa: O << "ngt"; break;
71   case  0xb: O << "false"; break;
72   case  0xc: O << "neq_oq"; break;
73   case  0xd: O << "ge"; break;
74   case  0xe: O << "gt"; break;
75   case  0xf: O << "true"; break;
76   case 0x10: O << "eq_os"; break;
77   case 0x11: O << "lt_oq"; break;
78   case 0x12: O << "le_oq"; break;
79   case 0x13: O << "unord_s"; break;
80   case 0x14: O << "neq_us"; break;
81   case 0x15: O << "nlt_uq"; break;
82   case 0x16: O << "nle_uq"; break;
83   case 0x17: O << "ord_s"; break;
84   case 0x18: O << "eq_us"; break;
85   case 0x19: O << "nge_uq"; break;
86   case 0x1a: O << "ngt_uq"; break;
87   case 0x1b: O << "false_os"; break;
88   case 0x1c: O << "neq_os"; break;
89   case 0x1d: O << "ge_oq"; break;
90   case 0x1e: O << "gt_oq"; break;
91   case 0x1f: O << "true_us"; break;
92   }
93 }
94
95 /// print_pcrel_imm - This is used to print an immediate value that ends up
96 /// being encoded as a pc-relative value (e.g. for jumps and calls).  These
97 /// print slightly differently than normal immediates.  For example, a $ is not
98 /// emitted.
99 void X86ATTInstPrinter::print_pcrel_imm(const MCInst *MI, unsigned OpNo,
100                                         raw_ostream &O) {
101   const MCOperand &Op = MI->getOperand(OpNo);
102   if (Op.isImm())
103     O << Op.getImm();
104   else {
105     assert(Op.isExpr() && "unknown pcrel immediate operand");
106     // If a symbolic branch target was added as a constant expression then print
107     // that address in hex.
108     const MCConstantExpr *BranchTarget = dyn_cast<MCConstantExpr>(Op.getExpr());
109     int64_t Address;
110     if (BranchTarget && BranchTarget->EvaluateAsAbsolute(Address)) {
111       O << "0x";
112       O.write_hex(Address);
113     }
114     else {
115       // Otherwise, just print the expression.
116       O << *Op.getExpr();
117     }
118   }
119 }
120
121 void X86ATTInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
122                                      raw_ostream &O) {
123   const MCOperand &Op = MI->getOperand(OpNo);
124   if (Op.isReg()) {
125     O << '%' << getRegisterName(Op.getReg());
126   } else if (Op.isImm()) {
127     // Print X86 immediates as signed values.
128     O << '$' << (int64_t)Op.getImm();
129     
130     if (CommentStream && (Op.getImm() > 255 || Op.getImm() < -256))
131       *CommentStream << format("imm = 0x%" PRIX64 "\n", (uint64_t)Op.getImm());
132     
133   } else {
134     assert(Op.isExpr() && "unknown operand kind in printOperand");
135     O << '$' << *Op.getExpr();
136   }
137 }
138
139 void X86ATTInstPrinter::printMemReference(const MCInst *MI, unsigned Op,
140                                           raw_ostream &O) {
141   const MCOperand &BaseReg  = MI->getOperand(Op);
142   const MCOperand &IndexReg = MI->getOperand(Op+2);
143   const MCOperand &DispSpec = MI->getOperand(Op+3);
144   const MCOperand &SegReg = MI->getOperand(Op+4);
145   
146   // If this has a segment register, print it.
147   if (SegReg.getReg()) {
148     printOperand(MI, Op+4, O);
149     O << ':';
150   }
151   
152   if (DispSpec.isImm()) {
153     int64_t DispVal = DispSpec.getImm();
154     if (DispVal || (!IndexReg.getReg() && !BaseReg.getReg()))
155       O << DispVal;
156   } else {
157     assert(DispSpec.isExpr() && "non-immediate displacement for LEA?");
158     O << *DispSpec.getExpr();
159   }
160   
161   if (IndexReg.getReg() || BaseReg.getReg()) {
162     O << '(';
163     if (BaseReg.getReg())
164       printOperand(MI, Op, O);
165     
166     if (IndexReg.getReg()) {
167       O << ',';
168       printOperand(MI, Op+2, O);
169       unsigned ScaleVal = MI->getOperand(Op+1).getImm();
170       if (ScaleVal != 1)
171         O << ',' << ScaleVal;
172     }
173     O << ')';
174   }
175 }