950daa921676ce42762c8e5475d436cce861535e
[oota-llvm.git] / lib / Target / Sparc / AsmPrinter / SparcAsmPrinter.cpp
1 //===-- SparcAsmPrinter.cpp - Sparc LLVM assembly writer ------------------===//
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 contains a printer that converts from our internal representation
11 // of machine-dependent LLVM code to GAS-format SPARC assembly language.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #define DEBUG_TYPE "asm-printer"
16 #include "Sparc.h"
17 #include "SparcInstrInfo.h"
18 #include "SparcTargetMachine.h"
19 #include "llvm/CodeGen/AsmPrinter.h"
20 #include "llvm/CodeGen/MachineInstr.h"
21 #include "llvm/MC/MCAsmInfo.h"
22 #include "llvm/MC/MCSymbol.h"
23 #include "llvm/Target/TargetRegistry.h"
24 #include "llvm/ADT/StringExtras.h"
25 #include "llvm/Support/FormattedStream.h"
26 using namespace llvm;
27
28 namespace {
29   class SparcAsmPrinter : public AsmPrinter {
30   public:
31     explicit SparcAsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
32                              MCContext &Ctx, MCStreamer &Streamer,
33                              const MCAsmInfo *T)
34       : AsmPrinter(O, TM, Ctx, Streamer, T) {}
35
36     virtual const char *getPassName() const {
37       return "Sparc Assembly Printer";
38     }
39
40     void printOperand(const MachineInstr *MI, int opNum);
41     void printMemOperand(const MachineInstr *MI, int opNum,
42                          const char *Modifier = 0);
43     void printCCOperand(const MachineInstr *MI, int opNum);
44
45     virtual void EmitInstruction(const MachineInstr *MI) {
46       printInstruction(MI);
47     }
48     void printInstruction(const MachineInstr *MI);  // autogenerated.
49     static const char *getRegisterName(unsigned RegNo);
50
51     bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
52                        unsigned AsmVariant, const char *ExtraCode);
53     bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
54                              unsigned AsmVariant, const char *ExtraCode);
55
56     bool printGetPCX(const MachineInstr *MI, unsigned OpNo);
57   };
58 } // end of anonymous namespace
59
60 #include "SparcGenAsmWriter.inc"
61
62 void SparcAsmPrinter::printOperand(const MachineInstr *MI, int opNum) {
63   const MachineOperand &MO = MI->getOperand (opNum);
64   bool CloseParen = false;
65   if (MI->getOpcode() == SP::SETHIi && !MO.isReg() && !MO.isImm()) {
66     O << "%hi(";
67     CloseParen = true;
68   } else if ((MI->getOpcode() == SP::ORri || MI->getOpcode() == SP::ADDri) &&
69              !MO.isReg() && !MO.isImm()) {
70     O << "%lo(";
71     CloseParen = true;
72   }
73   switch (MO.getType()) {
74   case MachineOperand::MO_Register:
75     O << "%" << LowercaseString(getRegisterName(MO.getReg()));
76     break;
77
78   case MachineOperand::MO_Immediate:
79     O << (int)MO.getImm();
80     break;
81   case MachineOperand::MO_MachineBasicBlock:
82     O << *MO.getMBB()->getSymbol(OutContext);
83     return;
84   case MachineOperand::MO_GlobalAddress:
85     O << *GetGlobalValueSymbol(MO.getGlobal());
86     break;
87   case MachineOperand::MO_ExternalSymbol:
88     O << MO.getSymbolName();
89     break;
90   case MachineOperand::MO_ConstantPoolIndex:
91     O << MAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << "_"
92       << MO.getIndex();
93     break;
94   default:
95     llvm_unreachable("<unknown operand type>");
96   }
97   if (CloseParen) O << ")";
98 }
99
100 void SparcAsmPrinter::printMemOperand(const MachineInstr *MI, int opNum,
101                                       const char *Modifier) {
102   printOperand(MI, opNum);
103
104   // If this is an ADD operand, emit it like normal operands.
105   if (Modifier && !strcmp(Modifier, "arith")) {
106     O << ", ";
107     printOperand(MI, opNum+1);
108     return;
109   }
110
111   if (MI->getOperand(opNum+1).isReg() &&
112       MI->getOperand(opNum+1).getReg() == SP::G0)
113     return;   // don't print "+%g0"
114   if (MI->getOperand(opNum+1).isImm() &&
115       MI->getOperand(opNum+1).getImm() == 0)
116     return;   // don't print "+0"
117
118   O << "+";
119   if (MI->getOperand(opNum+1).isGlobal() ||
120       MI->getOperand(opNum+1).isCPI()) {
121     O << "%lo(";
122     printOperand(MI, opNum+1);
123     O << ")";
124   } else {
125     printOperand(MI, opNum+1);
126   }
127 }
128
129 bool SparcAsmPrinter::printGetPCX(const MachineInstr *MI, unsigned opNum) {
130   std::string operand = "";
131   const MachineOperand &MO = MI->getOperand(opNum);
132   switch (MO.getType()) {
133   default: assert(0 && "Operand is not a register ");
134   case MachineOperand::MO_Register:
135     assert(TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
136            "Operand is not a physical register ");
137     operand = "%" + LowercaseString(getRegisterName(MO.getReg()));
138     break;
139   }
140
141   unsigned bbNum = MI->getParent()->getNumber();
142
143   O << '\n' << ".LLGETPCH" << bbNum << ":\n";
144   O << "\tcall\t.LLGETPC" << bbNum << '\n' ;
145
146   O << "\t  sethi\t"
147     << "%hi(_GLOBAL_OFFSET_TABLE_+(.-.LLGETPCH" << bbNum << ")), "  
148     << operand << '\n' ;
149
150   O << ".LLGETPC" << bbNum << ":\n" ;
151   O << "\tor\t" << operand  
152     << ", %lo(_GLOBAL_OFFSET_TABLE_+(.-.LLGETPCH" << bbNum << ")), "
153     << operand << '\n';
154   O << "\tadd\t" << operand << ", %o7, " << operand << '\n'; 
155   
156   return true;
157 }
158
159 void SparcAsmPrinter::printCCOperand(const MachineInstr *MI, int opNum) {
160   int CC = (int)MI->getOperand(opNum).getImm();
161   O << SPARCCondCodeToString((SPCC::CondCodes)CC);
162 }
163
164 /// PrintAsmOperand - Print out an operand for an inline asm expression.
165 ///
166 bool SparcAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
167                                       unsigned AsmVariant,
168                                       const char *ExtraCode) {
169   if (ExtraCode && ExtraCode[0]) {
170     if (ExtraCode[1] != 0) return true; // Unknown modifier.
171
172     switch (ExtraCode[0]) {
173     default: return true;  // Unknown modifier.
174     case 'r':
175      break;
176     }
177   }
178
179   printOperand(MI, OpNo);
180
181   return false;
182 }
183
184 bool SparcAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
185                                             unsigned OpNo,
186                                             unsigned AsmVariant,
187                                             const char *ExtraCode) {
188   if (ExtraCode && ExtraCode[0])
189     return true;  // Unknown modifier
190
191   O << '[';
192   printMemOperand(MI, OpNo);
193   O << ']';
194
195   return false;
196 }
197
198 // Force static initialization.
199 extern "C" void LLVMInitializeSparcAsmPrinter() { 
200   RegisterAsmPrinter<SparcAsmPrinter> X(TheSparcTarget);
201 }