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 #define PRINT_ALIAS_INSTR
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   if (!printAliasInstr(MI, O) && !printSparcAliasInstr(MI, O))
38     printInstruction(MI, O);
39   printAnnotation(O, Annot);
40 }
41 
42 bool SparcInstPrinter::printSparcAliasInstr(const MCInst *MI, raw_ostream &O)
43 {
44   switch (MI->getOpcode()) {
45   default: return false;
46   case SP::JMPLrr:
47   case SP::JMPLri: {
48     if (MI->getNumOperands() != 3)
49       return false;
50     if (!MI->getOperand(0).isReg())
51       return false;
52     switch (MI->getOperand(0).getReg()) {
53     default: return false;
54     case SP::G0: // jmp $addr
55       O << "\tjmp "; printMemOperand(MI, 1, O);
56       return true;
57     case SP::O7: // call $addr
58       O << "\tcall "; printMemOperand(MI, 1, O);
59       return true;
60     }
61   }
62   }
63 }
64 
65 void SparcInstPrinter::printOperand(const MCInst *MI, int opNum,
66                                     raw_ostream &O)
67 {
68   const MCOperand &MO = MI->getOperand (opNum);
69 
70   if (MO.isReg()) {
71     printRegName(O, MO.getReg());
72     return ;
73   }
74 
75   if (MO.isImm()) {
76     O << (int)MO.getImm();
77     return;
78   }
79 
80   assert(MO.isExpr() && "Unknown operand kind in printOperand");
81   MO.getExpr()->print(O);
82 }
83 
84 void SparcInstPrinter::printMemOperand(const MCInst *MI, int opNum,
85                                       raw_ostream &O, const char *Modifier)
86 {
87   printOperand(MI, opNum, O);
88 
89   // If this is an ADD operand, emit it like normal operands.
90   if (Modifier && !strcmp(Modifier, "arith")) {
91     O << ", ";
92     printOperand(MI, opNum+1, O);
93     return;
94   }
95   const MCOperand &MO = MI->getOperand(opNum+1);
96 
97   if (MO.isReg() && MO.getReg() == SP::G0)
98     return;   // don't print "+%g0"
99   if (MO.isImm() && MO.getImm() == 0)
100     return;   // don't print "+0"
101 
102   O << "+";
103 
104   printOperand(MI, opNum+1, O);
105 }
106 
107 void SparcInstPrinter::printCCOperand(const MCInst *MI, int opNum,
108                                      raw_ostream &O)
109 {
110   int CC = (int)MI->getOperand(opNum).getImm();
111   switch (MI->getOpcode()) {
112   default: break;
113   case SP::FBCOND:
114   case SP::MOVFCCrr:
115   case SP::MOVFCCri:
116   case SP::FMOVS_FCC:
117   case SP::FMOVD_FCC:
118   case SP::FMOVQ_FCC:  // Make sure CC is a fp conditional flag.
119     CC = (CC < 16) ? (CC + 16) : CC;
120     break;
121   }
122   O << SPARCCondCodeToString((SPCC::CondCodes)CC);
123 }
124 
125 bool SparcInstPrinter::printGetPCX(const MCInst *MI, unsigned opNum,
126                                   raw_ostream &O)
127 {
128   assert(0 && "FIXME: Implement SparcInstPrinter::printGetPCX.");
129   return true;
130 }
131