Now that we have everything nicely factored (e.g. asmprinter is not
[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/Constants.h"
20 #include "llvm/DerivedTypes.h"
21 #include "llvm/Module.h"
22 #include "llvm/CodeGen/AsmPrinter.h"
23 #include "llvm/CodeGen/DwarfWriter.h"
24 #include "llvm/CodeGen/MachineFunctionPass.h"
25 #include "llvm/CodeGen/MachineConstantPool.h"
26 #include "llvm/CodeGen/MachineInstr.h"
27 #include "llvm/MC/MCAsmInfo.h"
28 #include "llvm/MC/MCStreamer.h"
29 #include "llvm/MC/MCSymbol.h"
30 #include "llvm/Target/TargetLoweringObjectFile.h"
31 #include "llvm/Target/TargetRegistry.h"
32 #include "llvm/ADT/Statistic.h"
33 #include "llvm/ADT/StringExtras.h"
34 #include "llvm/Support/CommandLine.h"
35 #include "llvm/Support/ErrorHandling.h"
36 #include "llvm/Support/FormattedStream.h"
37 #include "llvm/Support/MathExtras.h"
38 #include <cctype>
39 #include <cstring>
40 #include <map>
41 using namespace llvm;
42
43 STATISTIC(EmittedInsts, "Number of machine instrs printed");
44
45 namespace {
46   class SparcAsmPrinter : public AsmPrinter {
47     /// We name each basic block in a Function with a unique number, so
48     /// that we can consistently refer to them later. This is cleared
49     /// at the beginning of each call to runOnMachineFunction().
50     ///
51     typedef std::map<const Value *, unsigned> ValueMapTy;
52     ValueMapTy NumberForBB;
53     unsigned BBNumber;
54   public:
55     explicit SparcAsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
56                              const MCAsmInfo *T, bool V)
57       : AsmPrinter(O, TM, T, V), BBNumber(0) {}
58
59     virtual const char *getPassName() const {
60       return "Sparc Assembly Printer";
61     }
62
63     void printOperand(const MachineInstr *MI, int opNum);
64     void printMemOperand(const MachineInstr *MI, int opNum,
65                          const char *Modifier = 0);
66     void printCCOperand(const MachineInstr *MI, int opNum);
67
68     void printInstruction(const MachineInstr *MI);  // autogenerated.
69     static const char *getRegisterName(unsigned RegNo);
70
71     bool runOnMachineFunction(MachineFunction &F);
72     bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
73                        unsigned AsmVariant, const char *ExtraCode);
74     bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
75                              unsigned AsmVariant, const char *ExtraCode);
76
77     void emitFunctionHeader(const MachineFunction &MF);
78     bool printGetPCX(const MachineInstr *MI, unsigned OpNo);
79   };
80 } // end of anonymous namespace
81
82 #include "SparcGenAsmWriter.inc"
83
84 /// runOnMachineFunction - This uses the printInstruction()
85 /// method to print assembly for each instruction.
86 ///
87 bool SparcAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
88   this->MF = &MF;
89
90   SetupMachineFunction(MF);
91
92   // Print out constants referenced by the function
93   EmitConstantPool(MF.getConstantPool());
94
95   // BBNumber is used here so that a given Printer will never give two
96   // BBs the same name. (If you have a better way, please let me know!)
97
98   O << "\n\n";
99   emitFunctionHeader(MF);
100   
101   
102   // Emit pre-function debug information.
103   DW->BeginFunction(&MF);
104
105   // Number each basic block so that we can consistently refer to them
106   // in PC-relative references.
107   // FIXME: Why not use the MBB numbers?
108   NumberForBB.clear();
109   for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
110        I != E; ++I) {
111     NumberForBB[I->getBasicBlock()] = BBNumber++;
112   }
113
114   // Print out code for the function.
115   for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
116        I != E; ++I) {
117     // Print a label for the basic block.
118     if (I != MF.begin()) {
119       EmitBasicBlockStart(I);
120     }
121     for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
122          II != E; ++II) {
123       // Print the assembly for the instruction.
124       processDebugLoc(II, true);
125       printInstruction(II);
126       
127       if (VerboseAsm)
128         EmitComments(*II);
129       O << '\n';
130       processDebugLoc(II, false);
131       ++EmittedInsts;
132     }
133   }
134
135   // Emit post-function debug information.
136   DW->EndFunction(&MF);
137
138   // We didn't modify anything.
139   O << "\t.size\t" << *CurrentFnSym << ", .-" << *CurrentFnSym << '\n';
140   return false;
141 }
142
143 void SparcAsmPrinter::emitFunctionHeader(const MachineFunction &MF) {
144   const Function *F = MF.getFunction();
145   OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(F, Mang, TM));
146   EmitAlignment(MF.getAlignment(), F);
147   
148   switch (F->getLinkage()) {
149   default: llvm_unreachable("Unknown linkage type");
150   case Function::PrivateLinkage:
151   case Function::InternalLinkage:
152     // Function is internal.
153     break;
154   case Function::DLLExportLinkage:
155   case Function::ExternalLinkage:
156     // Function is externally visible
157     O << "\t.global\t" << *CurrentFnSym << '\n';
158     break;
159   case Function::LinkerPrivateLinkage:
160   case Function::LinkOnceAnyLinkage:
161   case Function::LinkOnceODRLinkage:
162   case Function::WeakAnyLinkage:
163   case Function::WeakODRLinkage:
164     // Function is weak
165     O << "\t.weak\t" << *CurrentFnSym << '\n';
166     break;
167   }
168   
169   printVisibility(CurrentFnSym, F->getVisibility());
170   
171   O << "\t.type\t" << *CurrentFnSym << ", #function\n";
172   O << *CurrentFnSym << ":\n";
173 }
174
175
176 void SparcAsmPrinter::printOperand(const MachineInstr *MI, int opNum) {
177   const MachineOperand &MO = MI->getOperand (opNum);
178   bool CloseParen = false;
179   if (MI->getOpcode() == SP::SETHIi && !MO.isReg() && !MO.isImm()) {
180     O << "%hi(";
181     CloseParen = true;
182   } else if ((MI->getOpcode() == SP::ORri || MI->getOpcode() == SP::ADDri) &&
183              !MO.isReg() && !MO.isImm()) {
184     O << "%lo(";
185     CloseParen = true;
186   }
187   switch (MO.getType()) {
188   case MachineOperand::MO_Register:
189     O << "%" << LowercaseString(getRegisterName(MO.getReg()));
190     break;
191
192   case MachineOperand::MO_Immediate:
193     O << (int)MO.getImm();
194     break;
195   case MachineOperand::MO_MachineBasicBlock:
196     O << *GetMBBSymbol(MO.getMBB()->getNumber());
197     return;
198   case MachineOperand::MO_GlobalAddress:
199     O << *GetGlobalValueSymbol(MO.getGlobal());
200     break;
201   case MachineOperand::MO_ExternalSymbol:
202     O << MO.getSymbolName();
203     break;
204   case MachineOperand::MO_ConstantPoolIndex:
205     O << MAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << "_"
206       << MO.getIndex();
207     break;
208   default:
209     llvm_unreachable("<unknown operand type>");
210   }
211   if (CloseParen) O << ")";
212 }
213
214 void SparcAsmPrinter::printMemOperand(const MachineInstr *MI, int opNum,
215                                       const char *Modifier) {
216   printOperand(MI, opNum);
217
218   // If this is an ADD operand, emit it like normal operands.
219   if (Modifier && !strcmp(Modifier, "arith")) {
220     O << ", ";
221     printOperand(MI, opNum+1);
222     return;
223   }
224
225   if (MI->getOperand(opNum+1).isReg() &&
226       MI->getOperand(opNum+1).getReg() == SP::G0)
227     return;   // don't print "+%g0"
228   if (MI->getOperand(opNum+1).isImm() &&
229       MI->getOperand(opNum+1).getImm() == 0)
230     return;   // don't print "+0"
231
232   O << "+";
233   if (MI->getOperand(opNum+1).isGlobal() ||
234       MI->getOperand(opNum+1).isCPI()) {
235     O << "%lo(";
236     printOperand(MI, opNum+1);
237     O << ")";
238   } else {
239     printOperand(MI, opNum+1);
240   }
241 }
242
243 bool SparcAsmPrinter::printGetPCX(const MachineInstr *MI, unsigned opNum) {
244   std::string operand = "";
245   const MachineOperand &MO = MI->getOperand(opNum);
246   switch (MO.getType()) {
247   default: assert(0 && "Operand is not a register ");
248   case MachineOperand::MO_Register:
249     assert(TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
250            "Operand is not a physical register ");
251     operand = "%" + LowercaseString(getRegisterName(MO.getReg()));
252     break;
253   }
254
255   unsigned bbNum = NumberForBB[MI->getParent()->getBasicBlock()];
256
257   O << '\n' << ".LLGETPCH" << bbNum << ":\n";
258   O << "\tcall\t.LLGETPC" << bbNum << '\n' ;
259
260   O << "\t  sethi\t"
261     << "%hi(_GLOBAL_OFFSET_TABLE_+(.-.LLGETPCH" << bbNum << ")), "  
262     << operand << '\n' ;
263
264   O << ".LLGETPC" << bbNum << ":\n" ;
265   O << "\tor\t" << operand  
266     << ", %lo(_GLOBAL_OFFSET_TABLE_+(.-.LLGETPCH" << bbNum << ")), "
267     << operand << '\n';
268   O << "\tadd\t" << operand << ", %o7, " << operand << '\n'; 
269   
270   return true;
271 }
272
273 void SparcAsmPrinter::printCCOperand(const MachineInstr *MI, int opNum) {
274   int CC = (int)MI->getOperand(opNum).getImm();
275   O << SPARCCondCodeToString((SPCC::CondCodes)CC);
276 }
277
278 /// PrintAsmOperand - Print out an operand for an inline asm expression.
279 ///
280 bool SparcAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
281                                       unsigned AsmVariant,
282                                       const char *ExtraCode) {
283   if (ExtraCode && ExtraCode[0]) {
284     if (ExtraCode[1] != 0) return true; // Unknown modifier.
285
286     switch (ExtraCode[0]) {
287     default: return true;  // Unknown modifier.
288     case 'r':
289      break;
290     }
291   }
292
293   printOperand(MI, OpNo);
294
295   return false;
296 }
297
298 bool SparcAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
299                                             unsigned OpNo,
300                                             unsigned AsmVariant,
301                                             const char *ExtraCode) {
302   if (ExtraCode && ExtraCode[0])
303     return true;  // Unknown modifier
304
305   O << '[';
306   printMemOperand(MI, OpNo);
307   O << ']';
308
309   return false;
310 }
311
312 // Force static initialization.
313 extern "C" void LLVMInitializeSparcAsmPrinter() { 
314   RegisterAsmPrinter<SparcAsmPrinter> X(TheSparcTarget);
315 }