[Sparc] Add MCInstPrinter implementation for SPARC.
[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
17 #include "Sparc.h"
18 #include "MCTargetDesc/SparcBaseInfo.h"
19 #include "llvm/MC/MCExpr.h"
20 #include "llvm/MC/MCInst.h"
21 #include "llvm/MC/MCSymbol.h"
22 #include "llvm/Support/raw_ostream.h"
23 using namespace llvm;
24
25 #define GET_INSTRUCTION_NAME
26 // Uncomment the following line once we are ready to use MCAsmWriter.
27 //#include "SparcGenAsmWriter.inc"
28
29 void SparcInstPrinter::printRegName(raw_ostream &OS, unsigned RegNo) const
30 {
31   OS << '%' << StringRef(getRegisterName(RegNo)).lower();
32 }
33
34 void SparcInstPrinter::printInst(const MCInst *MI, raw_ostream &O,
35                                StringRef Annot)
36 {
37   printInstruction(MI, O);
38   printAnnotation(O, Annot);
39 }
40
41 void SparcInstPrinter::printOperand(const MCInst *MI, int opNum,
42                                     raw_ostream &O)
43 {
44   const MCOperand &MO = MI->getOperand (opNum);
45
46   if (MO.isReg()) {
47     printRegName(O, MO.getReg());
48     return ;
49   }
50
51   if (MO.isImm()) {
52     O << (int)MO.getImm();
53     return;
54   }
55
56   assert(MO.isExpr() && "Unknown operand kind in printOperand");
57   MO.getExpr()->print(O);
58 }
59
60 void SparcInstPrinter::printMemOperand(const MCInst *MI, int opNum,
61                                       raw_ostream &O, const char *Modifier)
62 {
63   printOperand(MI, opNum, O);
64
65   // If this is an ADD operand, emit it like normal operands.
66   if (Modifier && !strcmp(Modifier, "arith")) {
67     O << ", ";
68     printOperand(MI, opNum+1, O);
69     return;
70   }
71   const MCOperand &MO = MI->getOperand(opNum+1);
72
73   if (MO.isReg() && MO.getReg() == SP::G0)
74     return;   // don't print "+%g0"
75   if (MO.isImm() && MO.getImm() == 0)
76     return;   // don't print "+0"
77
78   O << "+";
79
80   printOperand(MI, opNum+1, O);
81 }
82
83 void SparcInstPrinter::printCCOperand(const MCInst *MI, int opNum,
84                                      raw_ostream &O)
85 {
86   int CC = (int)MI->getOperand(opNum).getImm();
87   O << SPARCCondCodeToString((SPCC::CondCodes)CC);
88 }
89
90 bool SparcInstPrinter::printGetPCX(const MCInst *MI, unsigned opNum,
91                                   raw_ostream &O)
92 {
93   assert(0 && "FIXME: Implement SparcInstPrinter::printGetPCX.");
94   return true;
95 }