Use the dwarf->llvm mapping to print register names in the cfi
[oota-llvm.git] / lib / Target / X86 / InstPrinter / X86IntelInstPrinter.cpp
1 //===-- X86IntelInstPrinter.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 "X86IntelInstPrinter.h"
17 #include "X86InstComments.h"
18 #include "X86Subtarget.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/FormattedStream.h"
24 #include "X86GenInstrNames.inc"
25 #include <cctype>
26 using namespace llvm;
27
28 // Include the auto-generated portion of the assembly writer.
29 #define GET_INSTRUCTION_NAME
30 #include "X86GenAsmWriter1.inc"
31
32 StringRef X86IntelInstPrinter::getRegName(unsigned RegNo) const {
33   return getRegisterName(RegNo);
34 }
35
36 void X86IntelInstPrinter::printInst(const MCInst *MI, raw_ostream &OS) {
37   printInstruction(MI, OS);
38   
39   // If verbose assembly is enabled, we can print some informative comments.
40   if (CommentStream)
41     EmitAnyX86InstComments(MI, *CommentStream, getRegisterName);
42 }
43 StringRef X86IntelInstPrinter::getOpcodeName(unsigned Opcode) const {
44   return getInstructionName(Opcode);
45 }
46
47 void X86IntelInstPrinter::printSSECC(const MCInst *MI, unsigned Op,
48                                      raw_ostream &O) {
49   switch (MI->getOperand(Op).getImm()) {
50   default: assert(0 && "Invalid ssecc argument!");
51   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
62 /// print_pcrel_imm - This is used to print an immediate value that ends up
63 /// being encoded as a pc-relative value.
64 void X86IntelInstPrinter::print_pcrel_imm(const MCInst *MI, unsigned OpNo,
65                                           raw_ostream &O) {
66   const MCOperand &Op = MI->getOperand(OpNo);
67   if (Op.isImm())
68     O << Op.getImm();
69   else {
70     assert(Op.isExpr() && "unknown pcrel immediate operand");
71     O << *Op.getExpr();
72   }
73 }
74
75 static 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
80 void X86IntelInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
81                                        raw_ostream &O) {
82   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");
89     O << *Op.getExpr();
90   }
91 }
92
93 void X86IntelInstPrinter::printMemReference(const MCInst *MI, unsigned Op,
94                                             raw_ostream &O) {
95   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);
99   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   }
106   
107   O << '[';
108   
109   bool NeedPlus = false;
110   if (BaseReg.getReg()) {
111     printOperand(MI, Op, O);
112     NeedPlus = true;
113   }
114   
115   if (IndexReg.getReg()) {
116     if (NeedPlus) O << " + ";
117     if (ScaleVal != 1)
118       O << ScaleVal << '*';
119     printOperand(MI, Op+2, O);
120     NeedPlus = true;
121   }
122   
123   
124   if (!DispSpec.isImm()) {
125     if (NeedPlus) O << " + ";
126     assert(DispSpec.isExpr() && "non-immediate displacement for LEA?");
127     O << *DispSpec.getExpr();
128   } 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 }