[Sparc] Add support for parsing fcmp with %fcc registers.
[oota-llvm.git] / lib / Target / Sparc / InstPrinter / SparcInstPrinter.cpp
1 //===-- SparcInstPrinter.cpp - Convert Sparc MCInst to assembly syntax -----==//
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 class prints an Sparc MCInst to a .s file.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #define DEBUG_TYPE "asm-printer"
15 #include "SparcInstPrinter.h"
16 #include "Sparc.h"
17 #include "llvm/MC/MCExpr.h"
18 #include "llvm/MC/MCInst.h"
19 #include "llvm/MC/MCSymbol.h"
20 #include "llvm/Support/raw_ostream.h"
21 using namespace llvm;
22
23 // The generated AsmMatcher SparcGenAsmWriter uses "Sparc" as the target
24 // namespace. But SPARC backend uses "SP" as its namespace.
25 namespace llvm {
26 namespace Sparc {
27   using namespace SP;
28 }
29 }
30
31 #define GET_INSTRUCTION_NAME
32 #define PRINT_ALIAS_INSTR
33 #include "SparcGenAsmWriter.inc"
34
35 bool SparcInstPrinter::isV9() const {
36   return (STI.getFeatureBits() & Sparc::FeatureV9) != 0;
37 }
38
39 void SparcInstPrinter::printRegName(raw_ostream &OS, unsigned RegNo) const
40 {
41   OS << '%' << StringRef(getRegisterName(RegNo)).lower();
42 }
43
44 void SparcInstPrinter::printInst(const MCInst *MI, raw_ostream &O,
45                                StringRef Annot)
46 {
47   if (!printAliasInstr(MI, O) && !printSparcAliasInstr(MI, O))
48     printInstruction(MI, O);
49   printAnnotation(O, Annot);
50 }
51
52 bool SparcInstPrinter::printSparcAliasInstr(const MCInst *MI, raw_ostream &O)
53 {
54   switch (MI->getOpcode()) {
55   default: return false;
56   case SP::JMPLrr:
57   case SP::JMPLri: {
58     if (MI->getNumOperands() != 3)
59       return false;
60     if (!MI->getOperand(0).isReg())
61       return false;
62     switch (MI->getOperand(0).getReg()) {
63     default: return false;
64     case SP::G0: // jmp $addr
65       O << "\tjmp "; printMemOperand(MI, 1, O);
66       return true;
67     case SP::O7: // call $addr
68       O << "\tcall "; printMemOperand(MI, 1, O);
69       return true;
70     }
71   }
72   case SP::V9FCMPS:
73   case SP::V9FCMPD:
74   case SP::V9FCMPQ: {
75     if (isV9()
76         || (MI->getNumOperands() != 3)
77         || (!MI->getOperand(0).isReg())
78         || (MI->getOperand(0).getReg() != SP::FCC0))
79       return false;
80     // if V8, skip printing %fcc0.
81     switch(MI->getOpcode()) {
82     default:
83     case SP::V9FCMPS: O << "\tfcmps "; break;
84     case SP::V9FCMPD: O << "\tfcmpd "; break;
85     case SP::V9FCMPQ: O << "\tfcmpq "; break;
86     }
87     printOperand(MI, 1, O);
88     O << ", ";
89     printOperand(MI, 2, O);
90     return true;
91   }
92   }
93 }
94
95 void SparcInstPrinter::printOperand(const MCInst *MI, int opNum,
96                                     raw_ostream &O)
97 {
98   const MCOperand &MO = MI->getOperand (opNum);
99
100   if (MO.isReg()) {
101     printRegName(O, MO.getReg());
102     return ;
103   }
104
105   if (MO.isImm()) {
106     O << (int)MO.getImm();
107     return;
108   }
109
110   assert(MO.isExpr() && "Unknown operand kind in printOperand");
111   MO.getExpr()->print(O);
112 }
113
114 void SparcInstPrinter::printMemOperand(const MCInst *MI, int opNum,
115                                       raw_ostream &O, const char *Modifier)
116 {
117   printOperand(MI, opNum, O);
118
119   // If this is an ADD operand, emit it like normal operands.
120   if (Modifier && !strcmp(Modifier, "arith")) {
121     O << ", ";
122     printOperand(MI, opNum+1, O);
123     return;
124   }
125   const MCOperand &MO = MI->getOperand(opNum+1);
126
127   if (MO.isReg() && MO.getReg() == SP::G0)
128     return;   // don't print "+%g0"
129   if (MO.isImm() && MO.getImm() == 0)
130     return;   // don't print "+0"
131
132   O << "+";
133
134   printOperand(MI, opNum+1, O);
135 }
136
137 void SparcInstPrinter::printCCOperand(const MCInst *MI, int opNum,
138                                      raw_ostream &O)
139 {
140   int CC = (int)MI->getOperand(opNum).getImm();
141   switch (MI->getOpcode()) {
142   default: break;
143   case SP::FBCOND:
144   case SP::FBCONDA:
145   case SP::BPFCC:
146   case SP::BPFCCA:
147   case SP::BPFCCNT:
148   case SP::BPFCCANT:
149   case SP::MOVFCCrr:
150   case SP::MOVFCCri:
151   case SP::FMOVS_FCC:
152   case SP::FMOVD_FCC:
153   case SP::FMOVQ_FCC:  // Make sure CC is a fp conditional flag.
154     CC = (CC < 16) ? (CC + 16) : CC;
155     break;
156   }
157   O << SPARCCondCodeToString((SPCC::CondCodes)CC);
158 }
159
160 bool SparcInstPrinter::printGetPCX(const MCInst *MI, unsigned opNum,
161                                   raw_ostream &O)
162 {
163   assert(0 && "FIXME: Implement SparcInstPrinter::printGetPCX.");
164   return true;
165 }