b94b71800b6283fcda30716fc5e8c62f8967fda7
[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/MCStreamer.h"
28 #include "llvm/MC/MCAsmInfo.h"
29 #include "llvm/Target/TargetLoweringObjectFile.h"
30 #include "llvm/Target/TargetRegistry.h"
31 #include "llvm/ADT/Statistic.h"
32 #include "llvm/ADT/StringExtras.h"
33 #include "llvm/Support/CommandLine.h"
34 #include "llvm/Support/ErrorHandling.h"
35 #include "llvm/Support/FormattedStream.h"
36 #include "llvm/Support/Mangler.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 VISIBILITY_HIDDEN 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 PrintGlobalVariable(const GlobalVariable *GVar);
64     void printOperand(const MachineInstr *MI, int opNum);
65     void printMemOperand(const MachineInstr *MI, int opNum,
66                          const char *Modifier = 0);
67     void printCCOperand(const MachineInstr *MI, int opNum);
68
69     void printInstruction(const MachineInstr *MI);  // autogenerated.
70     bool runOnMachineFunction(MachineFunction &F);
71     bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
72                        unsigned AsmVariant, const char *ExtraCode);
73     bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
74                              unsigned AsmVariant, const char *ExtraCode);
75   };
76 } // end of anonymous namespace
77
78 #include "SparcGenAsmWriter.inc"
79
80
81 /// runOnMachineFunction - This uses the printInstruction()
82 /// method to print assembly for each instruction.
83 ///
84 bool SparcAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
85   this->MF = &MF;
86
87   SetupMachineFunction(MF);
88
89   // Print out constants referenced by the function
90   EmitConstantPool(MF.getConstantPool());
91
92   // BBNumber is used here so that a given Printer will never give two
93   // BBs the same name. (If you have a better way, please let me know!)
94
95   O << "\n\n";
96
97   // Print out the label for the function.
98   const Function *F = MF.getFunction();
99   OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(F, Mang, TM));
100   EmitAlignment(MF.getAlignment(), F);
101   O << "\t.globl\t" << CurrentFnName << '\n';
102
103   printVisibility(CurrentFnName, F->getVisibility());
104
105   O << "\t.type\t" << CurrentFnName << ", #function\n";
106   O << CurrentFnName << ":\n";
107
108   // Number each basic block so that we can consistently refer to them
109   // in PC-relative references.
110   // FIXME: Why not use the MBB numbers?
111   NumberForBB.clear();
112   for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
113        I != E; ++I) {
114     NumberForBB[I->getBasicBlock()] = BBNumber++;
115   }
116
117   // Print out code for the function.
118   for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
119        I != E; ++I) {
120     // Print a label for the basic block.
121     if (I != MF.begin()) {
122       printBasicBlockLabel(I, true, true);
123       O << '\n';
124     }
125     for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
126          II != E; ++II) {
127       // Print the assembly for the instruction.
128       printInstruction(II);
129       ++EmittedInsts;
130     }
131   }
132
133   // We didn't modify anything.
134   return false;
135 }
136
137 void SparcAsmPrinter::printOperand(const MachineInstr *MI, int opNum) {
138   const MachineOperand &MO = MI->getOperand (opNum);
139   const TargetRegisterInfo &RI = *TM.getRegisterInfo();
140   bool CloseParen = false;
141   if (MI->getOpcode() == SP::SETHIi && !MO.isReg() && !MO.isImm()) {
142     O << "%hi(";
143     CloseParen = true;
144   } else if ((MI->getOpcode() == SP::ORri || MI->getOpcode() == SP::ADDri) &&
145              !MO.isReg() && !MO.isImm()) {
146     O << "%lo(";
147     CloseParen = true;
148   }
149   switch (MO.getType()) {
150   case MachineOperand::MO_Register:
151     if (TargetRegisterInfo::isPhysicalRegister(MO.getReg()))
152       O << "%" << LowercaseString (RI.get(MO.getReg()).AsmName);
153     else
154       O << "%reg" << MO.getReg();
155     break;
156
157   case MachineOperand::MO_Immediate:
158     O << (int)MO.getImm();
159     break;
160   case MachineOperand::MO_MachineBasicBlock:
161     printBasicBlockLabel(MO.getMBB());
162     return;
163   case MachineOperand::MO_GlobalAddress:
164     O << Mang->getMangledName(MO.getGlobal());
165     break;
166   case MachineOperand::MO_ExternalSymbol:
167     O << MO.getSymbolName();
168     break;
169   case MachineOperand::MO_ConstantPoolIndex:
170     O << MAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << "_"
171       << MO.getIndex();
172     break;
173   default:
174     llvm_unreachable("<unknown operand type>");
175   }
176   if (CloseParen) O << ")";
177 }
178
179 void SparcAsmPrinter::printMemOperand(const MachineInstr *MI, int opNum,
180                                       const char *Modifier) {
181   printOperand(MI, opNum);
182
183   // If this is an ADD operand, emit it like normal operands.
184   if (Modifier && !strcmp(Modifier, "arith")) {
185     O << ", ";
186     printOperand(MI, opNum+1);
187     return;
188   }
189
190   if (MI->getOperand(opNum+1).isReg() &&
191       MI->getOperand(opNum+1).getReg() == SP::G0)
192     return;   // don't print "+%g0"
193   if (MI->getOperand(opNum+1).isImm() &&
194       MI->getOperand(opNum+1).getImm() == 0)
195     return;   // don't print "+0"
196
197   O << "+";
198   if (MI->getOperand(opNum+1).isGlobal() ||
199       MI->getOperand(opNum+1).isCPI()) {
200     O << "%lo(";
201     printOperand(MI, opNum+1);
202     O << ")";
203   } else {
204     printOperand(MI, opNum+1);
205   }
206 }
207
208 void SparcAsmPrinter::printCCOperand(const MachineInstr *MI, int opNum) {
209   int CC = (int)MI->getOperand(opNum).getImm();
210   O << SPARCCondCodeToString((SPCC::CondCodes)CC);
211 }
212
213 void SparcAsmPrinter::PrintGlobalVariable(const GlobalVariable* GVar) {
214   const TargetData *TD = TM.getTargetData();
215
216   if (!GVar->hasInitializer())
217     return;  // External global require no code
218
219   // Check to see if this is a special global used by LLVM, if so, emit it.
220   if (EmitSpecialLLVMGlobal(GVar))
221     return;
222
223   O << "\n\n";
224   std::string name = Mang->getMangledName(GVar);
225   Constant *C = GVar->getInitializer();
226   unsigned Size = TD->getTypeAllocSize(C->getType());
227   unsigned Align = TD->getPreferredAlignment(GVar);
228
229   printVisibility(name, GVar->getVisibility());
230
231   OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(GVar, Mang,
232                                                                   TM));
233
234   if (C->isNullValue() && !GVar->hasSection()) {
235     if (!GVar->isThreadLocal() &&
236         (GVar->hasLocalLinkage() || GVar->isWeakForLinker())) {
237       if (Size == 0) Size = 1;   // .comm Foo, 0 is undefined, avoid it.
238
239       if (GVar->hasLocalLinkage())
240         O << "\t.local " << name << '\n';
241
242       O << MAI->getCOMMDirective() << name << ',' << Size;
243       if (MAI->getCOMMDirectiveTakesAlignment())
244         O << ',' << (1 << Align);
245
246       O << '\n';
247       return;
248     }
249   }
250
251   switch (GVar->getLinkage()) {
252    case GlobalValue::CommonLinkage:
253    case GlobalValue::LinkOnceAnyLinkage:
254    case GlobalValue::LinkOnceODRLinkage:
255    case GlobalValue::WeakAnyLinkage: // FIXME: Verify correct for weak.
256    case GlobalValue::WeakODRLinkage: // FIXME: Verify correct for weak.
257     // Nonnull linkonce -> weak
258     O << "\t.weak " << name << '\n';
259     break;
260    case GlobalValue::AppendingLinkage:
261     // FIXME: appending linkage variables should go into a section of
262     // their name or something.  For now, just emit them as external.
263    case GlobalValue::ExternalLinkage:
264     // If external or appending, declare as a global symbol
265     O << MAI->getGlobalDirective() << name << '\n';
266     // FALL THROUGH
267    case GlobalValue::PrivateLinkage:
268    case GlobalValue::LinkerPrivateLinkage:
269    case GlobalValue::InternalLinkage:
270     break;
271    case GlobalValue::GhostLinkage:
272     llvm_unreachable("Should not have any unmaterialized functions!");
273    case GlobalValue::DLLImportLinkage:
274     llvm_unreachable("DLLImport linkage is not supported by this target!");
275    case GlobalValue::DLLExportLinkage:
276     llvm_unreachable("DLLExport linkage is not supported by this target!");
277    default:
278     llvm_unreachable("Unknown linkage type!");
279   }
280
281   EmitAlignment(Align, GVar);
282
283   if (MAI->hasDotTypeDotSizeDirective()) {
284     O << "\t.type " << name << ",#object\n";
285     O << "\t.size " << name << ',' << Size << '\n';
286   }
287
288   O << name << ":\n";
289   EmitGlobalConstant(C);
290 }
291
292 /// PrintAsmOperand - Print out an operand for an inline asm expression.
293 ///
294 bool SparcAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
295                                       unsigned AsmVariant,
296                                       const char *ExtraCode) {
297   if (ExtraCode && ExtraCode[0]) {
298     if (ExtraCode[1] != 0) return true; // Unknown modifier.
299
300     switch (ExtraCode[0]) {
301     default: return true;  // Unknown modifier.
302     case 'r':
303      break;
304     }
305   }
306
307   printOperand(MI, OpNo);
308
309   return false;
310 }
311
312 bool SparcAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
313                                             unsigned OpNo,
314                                             unsigned AsmVariant,
315                                             const char *ExtraCode) {
316   if (ExtraCode && ExtraCode[0])
317     return true;  // Unknown modifier
318
319   O << '[';
320   printMemOperand(MI, OpNo);
321   O << ']';
322
323   return false;
324 }
325
326 // Force static initialization.
327 extern "C" void LLVMInitializeSparcAsmPrinter() { 
328   RegisterAsmPrinter<SparcAsmPrinter> X(TheSparcTarget);
329 }