Add support for stored annotations to MCInst, and provide facilities for MC-based...
[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 "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/FormattedStream.h"
24 #include <cctype>
25 using namespace llvm;
26
27 // Include the auto-generated portion of the assembly writer.
28 #define GET_INSTRUCTION_NAME
29 #include "X86GenAsmWriter1.inc"
30
31 void X86IntelInstPrinter::printRegName(raw_ostream &OS, unsigned RegNo) const {
32   OS << getRegisterName(RegNo);
33 }
34
35 void X86IntelInstPrinter::printInst(const MCInst *MI, raw_ostream &OS) {
36   printInstruction(MI, OS);
37   
38   // If verbose assembly is enabled, we can print some informative comments.
39   if (CommentStream) {
40     printAnnotations(MI, *CommentStream);
41     EmitAnyX86InstComments(MI, *CommentStream, getRegisterName);
42   }
43 }
44 StringRef X86IntelInstPrinter::getOpcodeName(unsigned Opcode) const {
45   return getInstructionName(Opcode);
46 }
47
48 void X86IntelInstPrinter::printSSECC(const MCInst *MI, unsigned Op,
49                                      raw_ostream &O) {
50   switch (MI->getOperand(Op).getImm()) {
51   default: assert(0 && "Invalid ssecc argument!");
52   case 0: O << "eq"; break;
53   case 1: O << "lt"; break;
54   case 2: O << "le"; break;
55   case 3: O << "unord"; break;
56   case 4: O << "neq"; break;
57   case 5: O << "nlt"; break;
58   case 6: O << "nle"; break;
59   case 7: O << "ord"; break;
60   }
61 }
62
63 /// print_pcrel_imm - This is used to print an immediate value that ends up
64 /// being encoded as a pc-relative value.
65 void X86IntelInstPrinter::print_pcrel_imm(const MCInst *MI, unsigned OpNo,
66                                           raw_ostream &O) {
67   const MCOperand &Op = MI->getOperand(OpNo);
68   if (Op.isImm())
69     O << Op.getImm();
70   else {
71     assert(Op.isExpr() && "unknown pcrel immediate operand");
72     O << *Op.getExpr();
73   }
74 }
75
76 static void PrintRegName(raw_ostream &O, StringRef RegName) {
77   for (unsigned i = 0, e = RegName.size(); i != e; ++i)
78     O << (char)toupper(RegName[i]);
79 }
80
81 void X86IntelInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
82                                        raw_ostream &O) {
83   const MCOperand &Op = MI->getOperand(OpNo);
84   if (Op.isReg()) {
85     PrintRegName(O, getRegisterName(Op.getReg()));
86   } else if (Op.isImm()) {
87     O << Op.getImm();
88   } else {
89     assert(Op.isExpr() && "unknown operand kind in printOperand");
90     O << *Op.getExpr();
91   }
92 }
93
94 void X86IntelInstPrinter::printMemReference(const MCInst *MI, unsigned Op,
95                                             raw_ostream &O) {
96   const MCOperand &BaseReg  = MI->getOperand(Op);
97   unsigned ScaleVal         = MI->getOperand(Op+1).getImm();
98   const MCOperand &IndexReg = MI->getOperand(Op+2);
99   const MCOperand &DispSpec = MI->getOperand(Op+3);
100   const MCOperand &SegReg   = MI->getOperand(Op+4);
101   
102   // If this has a segment register, print it.
103   if (SegReg.getReg()) {
104     printOperand(MI, Op+4, O);
105     O << ':';
106   }
107   
108   O << '[';
109   
110   bool NeedPlus = false;
111   if (BaseReg.getReg()) {
112     printOperand(MI, Op, O);
113     NeedPlus = true;
114   }
115   
116   if (IndexReg.getReg()) {
117     if (NeedPlus) O << " + ";
118     if (ScaleVal != 1)
119       O << ScaleVal << '*';
120     printOperand(MI, Op+2, O);
121     NeedPlus = true;
122   }
123   
124   
125   if (!DispSpec.isImm()) {
126     if (NeedPlus) O << " + ";
127     assert(DispSpec.isExpr() && "non-immediate displacement for LEA?");
128     O << *DispSpec.getExpr();
129   } else {
130     int64_t DispVal = DispSpec.getImm();
131     if (DispVal || (!IndexReg.getReg() && !BaseReg.getReg())) {
132       if (NeedPlus) {
133         if (DispVal > 0)
134           O << " + ";
135         else {
136           O << " - ";
137           DispVal = -DispVal;
138         }
139       }
140       O << DispVal;
141     }
142   }
143   
144   O << ']';
145 }