add a new MachineBasicBlock::getSymbol method, replacing
[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   SetupMachineFunction(MF);
89
90   // Print out constants referenced by the function
91   EmitConstantPool(MF.getConstantPool());
92
93   // BBNumber is used here so that a given Printer will never give two
94   // BBs the same name. (If you have a better way, please let me know!)
95
96   O << "\n\n";
97   emitFunctionHeader(MF);
98   
99   
100   // Emit pre-function debug information.
101   DW->BeginFunction(&MF);
102
103   // Number each basic block so that we can consistently refer to them
104   // in PC-relative references.
105   // FIXME: Why not use the MBB numbers?
106   NumberForBB.clear();
107   for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
108        I != E; ++I) {
109     NumberForBB[I->getBasicBlock()] = BBNumber++;
110   }
111
112   // Print out code for the function.
113   for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
114        I != E; ++I) {
115     // Print a label for the basic block.
116     if (I != MF.begin()) {
117       EmitBasicBlockStart(I);
118     }
119     for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
120          II != E; ++II) {
121       // Print the assembly for the instruction.
122       processDebugLoc(II, true);
123       printInstruction(II);
124       
125       if (VerboseAsm)
126         EmitComments(*II);
127       O << '\n';
128       processDebugLoc(II, false);
129       ++EmittedInsts;
130     }
131   }
132
133   // Emit post-function debug information.
134   DW->EndFunction(&MF);
135
136   // We didn't modify anything.
137   O << "\t.size\t" << *CurrentFnSym << ", .-" << *CurrentFnSym << '\n';
138   return false;
139 }
140
141 void SparcAsmPrinter::emitFunctionHeader(const MachineFunction &MF) {
142   const Function *F = MF.getFunction();
143   OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(F, Mang, TM));
144   EmitAlignment(MF.getAlignment(), F);
145   
146   switch (F->getLinkage()) {
147   default: llvm_unreachable("Unknown linkage type");
148   case Function::PrivateLinkage:
149   case Function::InternalLinkage:
150     // Function is internal.
151     break;
152   case Function::DLLExportLinkage:
153   case Function::ExternalLinkage:
154     // Function is externally visible
155     O << "\t.global\t" << *CurrentFnSym << '\n';
156     break;
157   case Function::LinkerPrivateLinkage:
158   case Function::LinkOnceAnyLinkage:
159   case Function::LinkOnceODRLinkage:
160   case Function::WeakAnyLinkage:
161   case Function::WeakODRLinkage:
162     // Function is weak
163     O << "\t.weak\t" << *CurrentFnSym << '\n';
164     break;
165   }
166   
167   printVisibility(CurrentFnSym, F->getVisibility());
168   
169   O << "\t.type\t" << *CurrentFnSym << ", #function\n";
170   O << *CurrentFnSym << ":\n";
171 }
172
173
174 void SparcAsmPrinter::printOperand(const MachineInstr *MI, int opNum) {
175   const MachineOperand &MO = MI->getOperand (opNum);
176   bool CloseParen = false;
177   if (MI->getOpcode() == SP::SETHIi && !MO.isReg() && !MO.isImm()) {
178     O << "%hi(";
179     CloseParen = true;
180   } else if ((MI->getOpcode() == SP::ORri || MI->getOpcode() == SP::ADDri) &&
181              !MO.isReg() && !MO.isImm()) {
182     O << "%lo(";
183     CloseParen = true;
184   }
185   switch (MO.getType()) {
186   case MachineOperand::MO_Register:
187     O << "%" << LowercaseString(getRegisterName(MO.getReg()));
188     break;
189
190   case MachineOperand::MO_Immediate:
191     O << (int)MO.getImm();
192     break;
193   case MachineOperand::MO_MachineBasicBlock:
194     O << *MO.getMBB()->getSymbol(OutContext);
195     return;
196   case MachineOperand::MO_GlobalAddress:
197     O << *GetGlobalValueSymbol(MO.getGlobal());
198     break;
199   case MachineOperand::MO_ExternalSymbol:
200     O << MO.getSymbolName();
201     break;
202   case MachineOperand::MO_ConstantPoolIndex:
203     O << MAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << "_"
204       << MO.getIndex();
205     break;
206   default:
207     llvm_unreachable("<unknown operand type>");
208   }
209   if (CloseParen) O << ")";
210 }
211
212 void SparcAsmPrinter::printMemOperand(const MachineInstr *MI, int opNum,
213                                       const char *Modifier) {
214   printOperand(MI, opNum);
215
216   // If this is an ADD operand, emit it like normal operands.
217   if (Modifier && !strcmp(Modifier, "arith")) {
218     O << ", ";
219     printOperand(MI, opNum+1);
220     return;
221   }
222
223   if (MI->getOperand(opNum+1).isReg() &&
224       MI->getOperand(opNum+1).getReg() == SP::G0)
225     return;   // don't print "+%g0"
226   if (MI->getOperand(opNum+1).isImm() &&
227       MI->getOperand(opNum+1).getImm() == 0)
228     return;   // don't print "+0"
229
230   O << "+";
231   if (MI->getOperand(opNum+1).isGlobal() ||
232       MI->getOperand(opNum+1).isCPI()) {
233     O << "%lo(";
234     printOperand(MI, opNum+1);
235     O << ")";
236   } else {
237     printOperand(MI, opNum+1);
238   }
239 }
240
241 bool SparcAsmPrinter::printGetPCX(const MachineInstr *MI, unsigned opNum) {
242   std::string operand = "";
243   const MachineOperand &MO = MI->getOperand(opNum);
244   switch (MO.getType()) {
245   default: assert(0 && "Operand is not a register ");
246   case MachineOperand::MO_Register:
247     assert(TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
248            "Operand is not a physical register ");
249     operand = "%" + LowercaseString(getRegisterName(MO.getReg()));
250     break;
251   }
252
253   unsigned bbNum = NumberForBB[MI->getParent()->getBasicBlock()];
254
255   O << '\n' << ".LLGETPCH" << bbNum << ":\n";
256   O << "\tcall\t.LLGETPC" << bbNum << '\n' ;
257
258   O << "\t  sethi\t"
259     << "%hi(_GLOBAL_OFFSET_TABLE_+(.-.LLGETPCH" << bbNum << ")), "  
260     << operand << '\n' ;
261
262   O << ".LLGETPC" << bbNum << ":\n" ;
263   O << "\tor\t" << operand  
264     << ", %lo(_GLOBAL_OFFSET_TABLE_+(.-.LLGETPCH" << bbNum << ")), "
265     << operand << '\n';
266   O << "\tadd\t" << operand << ", %o7, " << operand << '\n'; 
267   
268   return true;
269 }
270
271 void SparcAsmPrinter::printCCOperand(const MachineInstr *MI, int opNum) {
272   int CC = (int)MI->getOperand(opNum).getImm();
273   O << SPARCCondCodeToString((SPCC::CondCodes)CC);
274 }
275
276 /// PrintAsmOperand - Print out an operand for an inline asm expression.
277 ///
278 bool SparcAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
279                                       unsigned AsmVariant,
280                                       const char *ExtraCode) {
281   if (ExtraCode && ExtraCode[0]) {
282     if (ExtraCode[1] != 0) return true; // Unknown modifier.
283
284     switch (ExtraCode[0]) {
285     default: return true;  // Unknown modifier.
286     case 'r':
287      break;
288     }
289   }
290
291   printOperand(MI, OpNo);
292
293   return false;
294 }
295
296 bool SparcAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
297                                             unsigned OpNo,
298                                             unsigned AsmVariant,
299                                             const char *ExtraCode) {
300   if (ExtraCode && ExtraCode[0])
301     return true;  // Unknown modifier
302
303   O << '[';
304   printMemOperand(MI, OpNo);
305   O << ']';
306
307   return false;
308 }
309
310 // Force static initialization.
311 extern "C" void LLVMInitializeSparcAsmPrinter() { 
312   RegisterAsmPrinter<SparcAsmPrinter> X(TheSparcTarget);
313 }