Kill ModuleProvider and ghost linkage by inverting the relationship between
[oota-llvm.git] / lib / Target / XCore / AsmPrinter / XCoreAsmPrinter.cpp
1 //===-- XCoreAsmPrinter.cpp - XCore 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 the XAS-format XCore assembly language.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #define DEBUG_TYPE "asm-printer"
16 #include "XCore.h"
17 #include "XCoreInstrInfo.h"
18 #include "XCoreSubtarget.h"
19 #include "XCoreMCAsmInfo.h"
20 #include "XCoreTargetMachine.h"
21 #include "llvm/Constants.h"
22 #include "llvm/DerivedTypes.h"
23 #include "llvm/Module.h"
24 #include "llvm/CodeGen/AsmPrinter.h"
25 #include "llvm/CodeGen/DwarfWriter.h"
26 #include "llvm/CodeGen/MachineModuleInfo.h"
27 #include "llvm/CodeGen/MachineFunctionPass.h"
28 #include "llvm/CodeGen/MachineConstantPool.h"
29 #include "llvm/CodeGen/MachineInstr.h"
30 #include "llvm/MC/MCStreamer.h"
31 #include "llvm/MC/MCSymbol.h"
32 #include "llvm/Target/TargetData.h"
33 #include "llvm/Target/TargetLoweringObjectFile.h"
34 #include "llvm/Target/TargetRegistry.h"
35 #include "llvm/ADT/Statistic.h"
36 #include "llvm/ADT/StringExtras.h"
37 #include "llvm/Support/CommandLine.h"
38 #include "llvm/Support/ErrorHandling.h"
39 #include "llvm/Support/FormattedStream.h"
40 #include "llvm/Support/MathExtras.h"
41 #include <algorithm>
42 #include <cctype>
43 using namespace llvm;
44
45 STATISTIC(EmittedInsts, "Number of machine instrs printed");
46
47 static cl::opt<unsigned> MaxThreads("xcore-max-threads", cl::Optional,
48   cl::desc("Maximum number of threads (for emulation thread-local storage)"),
49   cl::Hidden,
50   cl::value_desc("number"),
51   cl::init(8));
52
53 namespace {
54   class XCoreAsmPrinter : public AsmPrinter {
55     const XCoreSubtarget &Subtarget;
56   public:
57     explicit XCoreAsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
58                              const MCAsmInfo *T, bool V)
59       : AsmPrinter(O, TM, T, V),
60       Subtarget(TM.getSubtarget<XCoreSubtarget>()) {}
61
62     virtual const char *getPassName() const {
63       return "XCore Assembly Printer";
64     }
65
66     void printMemOperand(const MachineInstr *MI, int opNum);
67     void printOperand(const MachineInstr *MI, int opNum);
68     bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
69                         unsigned AsmVariant, const char *ExtraCode);
70
71     void emitGlobalDirective(const MCSymbol *Sym);
72     
73     void emitArrayBound(const MCSymbol *Sym, const GlobalVariable *GV);
74     virtual void EmitGlobalVariable(const GlobalVariable *GV);
75
76     void emitFunctionStart(MachineFunction &MF);
77     void emitFunctionEnd(MachineFunction &MF);
78
79     void printInstruction(const MachineInstr *MI);  // autogenerated.
80     static const char *getRegisterName(unsigned RegNo);
81
82     void printMachineInstruction(const MachineInstr *MI);
83     bool runOnMachineFunction(MachineFunction &F);
84     
85     void getAnalysisUsage(AnalysisUsage &AU) const {
86       AsmPrinter::getAnalysisUsage(AU);
87       AU.setPreservesAll();
88       AU.addRequired<MachineModuleInfo>();
89       AU.addRequired<DwarfWriter>();
90     }
91   };
92 } // end of anonymous namespace
93
94 #include "XCoreGenAsmWriter.inc"
95
96 void XCoreAsmPrinter::emitGlobalDirective(const MCSymbol *Sym) {
97   O << MAI->getGlobalDirective() << *Sym << "\n";
98 }
99
100 void XCoreAsmPrinter::emitArrayBound(const MCSymbol *Sym,
101                                      const GlobalVariable *GV) {
102   assert(((GV->hasExternalLinkage() ||
103     GV->hasWeakLinkage()) ||
104     GV->hasLinkOnceLinkage()) && "Unexpected linkage");
105   if (const ArrayType *ATy = dyn_cast<ArrayType>(
106     cast<PointerType>(GV->getType())->getElementType())) {
107     O << MAI->getGlobalDirective() << *Sym;
108     O << ".globound" << "\n";
109     O << "\t.set\t" << *Sym;
110     O << ".globound" << "," << ATy->getNumElements() << "\n";
111     if (GV->hasWeakLinkage() || GV->hasLinkOnceLinkage()) {
112       // TODO Use COMDAT groups for LinkOnceLinkage
113       O << MAI->getWeakDefDirective() << *Sym << ".globound" << "\n";
114     }
115   }
116 }
117
118 void XCoreAsmPrinter::EmitGlobalVariable(const GlobalVariable *GV) {
119   // Check to see if this is a special global used by LLVM, if so, emit it.
120   if (!GV->hasInitializer() ||
121       EmitSpecialLLVMGlobal(GV))
122     return;
123
124   const TargetData *TD = TM.getTargetData();
125   OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(GV, Mang,TM));
126
127   
128   MCSymbol *GVSym = GetGlobalValueSymbol(GV);
129   Constant *C = GV->getInitializer();
130   unsigned Align = (unsigned)TD->getPreferredTypeAlignmentShift(C->getType());
131   
132   // Mark the start of the global
133   O << "\t.cc_top " << *GVSym << ".data," << *GVSym << "\n";
134
135   switch (GV->getLinkage()) {
136   case GlobalValue::AppendingLinkage:
137     llvm_report_error("AppendingLinkage is not supported by this target!");
138   case GlobalValue::LinkOnceAnyLinkage:
139   case GlobalValue::LinkOnceODRLinkage:
140   case GlobalValue::WeakAnyLinkage:
141   case GlobalValue::WeakODRLinkage:
142   case GlobalValue::ExternalLinkage:
143     emitArrayBound(GVSym, GV);
144     emitGlobalDirective(GVSym);
145     // TODO Use COMDAT groups for LinkOnceLinkage
146     if (GV->hasWeakLinkage() || GV->hasLinkOnceLinkage())
147       O << MAI->getWeakDefDirective() << *GVSym << "\n";
148     // FALL THROUGH
149   case GlobalValue::InternalLinkage:
150   case GlobalValue::PrivateLinkage:
151   case GlobalValue::LinkerPrivateLinkage:
152     break;
153   case GlobalValue::DLLImportLinkage:
154     llvm_unreachable("DLLImport linkage is not supported by this target!");
155   case GlobalValue::DLLExportLinkage:
156     llvm_unreachable("DLLExport linkage is not supported by this target!");
157   default:
158     llvm_unreachable("Unknown linkage type!");
159   }
160
161   EmitAlignment(Align, GV, 2);
162   
163   unsigned Size = TD->getTypeAllocSize(C->getType());
164   if (GV->isThreadLocal()) {
165     Size *= MaxThreads;
166   }
167   if (MAI->hasDotTypeDotSizeDirective()) {
168     O << "\t.type " << *GVSym << ",@object\n";
169     O << "\t.size " << *GVSym << "," << Size << "\n";
170   }
171   O << *GVSym << ":\n";
172   
173   EmitGlobalConstant(C);
174   if (GV->isThreadLocal()) {
175     for (unsigned i = 1; i < MaxThreads; ++i)
176       EmitGlobalConstant(C);
177   }
178   // The ABI requires that unsigned scalar types smaller than 32 bits
179   // are padded to 32 bits.
180   if (Size < 4)
181     OutStreamer.EmitZeros(4 - Size, 0);
182   
183   // Mark the end of the global
184   O << "\t.cc_bottom " << *GVSym << ".data\n";
185 }
186
187 /// Emit the directives on the start of functions
188 void XCoreAsmPrinter::emitFunctionStart(MachineFunction &MF) {
189   // Print out the label for the function.
190   const Function *F = MF.getFunction();
191
192   OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(F, Mang, TM));
193   
194   // Mark the start of the function
195   O << "\t.cc_top " << *CurrentFnSym << ".function," << *CurrentFnSym << "\n";
196
197   switch (F->getLinkage()) {
198   default: llvm_unreachable("Unknown linkage type!");
199   case Function::InternalLinkage:  // Symbols default to internal.
200   case Function::PrivateLinkage:
201   case Function::LinkerPrivateLinkage:
202     break;
203   case Function::ExternalLinkage:
204     emitGlobalDirective(CurrentFnSym);
205     break;
206   case Function::LinkOnceAnyLinkage:
207   case Function::LinkOnceODRLinkage:
208   case Function::WeakAnyLinkage:
209   case Function::WeakODRLinkage:
210     // TODO Use COMDAT groups for LinkOnceLinkage
211     O << MAI->getGlobalDirective() << *CurrentFnSym << "\n";
212     O << MAI->getWeakDefDirective() << *CurrentFnSym << "\n";
213     break;
214   }
215   // (1 << 1) byte aligned
216   EmitAlignment(MF.getAlignment(), F, 1);
217   if (MAI->hasDotTypeDotSizeDirective())
218     O << "\t.type " << *CurrentFnSym << ",@function\n";
219
220   O << *CurrentFnSym << ":\n";
221 }
222
223 /// Emit the directives on the end of functions
224 void XCoreAsmPrinter::emitFunctionEnd(MachineFunction &MF) {
225   // Mark the end of the function
226   O << "\t.cc_bottom " << *CurrentFnSym << ".function\n";
227 }
228
229 /// runOnMachineFunction - This uses the printMachineInstruction()
230 /// method to print assembly for each instruction.
231 ///
232 bool XCoreAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
233   SetupMachineFunction(MF);
234
235   // Print out constants referenced by the function
236   EmitConstantPool(MF.getConstantPool());
237
238   // Emit the function start directives
239   emitFunctionStart(MF);
240   
241   // Emit pre-function debug information.
242   DW->BeginFunction(&MF);
243
244   // Print out code for the function.
245   for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
246        I != E; ++I) {
247
248     // Print a label for the basic block.
249     if (I != MF.begin()) {
250       EmitBasicBlockStart(I);
251     }
252
253     for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
254          II != E; ++II) {
255       // Print the assembly for the instruction.
256       printMachineInstruction(II);
257     }
258
259     // Each Basic Block is separated by a newline
260     O << '\n';
261   }
262
263   // Emit function end directives
264   emitFunctionEnd(MF);
265   
266   // Print out jump tables referenced by the function
267   EmitJumpTableInfo(MF);
268
269   // Emit post-function debug information.
270   DW->EndFunction(&MF);
271
272   // We didn't modify anything.
273   return false;
274 }
275
276 void XCoreAsmPrinter::printMemOperand(const MachineInstr *MI, int opNum)
277 {
278   printOperand(MI, opNum);
279   
280   if (MI->getOperand(opNum+1).isImm()
281     && MI->getOperand(opNum+1).getImm() == 0)
282     return;
283   
284   O << "+";
285   printOperand(MI, opNum+1);
286 }
287
288 void XCoreAsmPrinter::printOperand(const MachineInstr *MI, int opNum) {
289   const MachineOperand &MO = MI->getOperand(opNum);
290   switch (MO.getType()) {
291   case MachineOperand::MO_Register:
292     O << getRegisterName(MO.getReg());
293     break;
294   case MachineOperand::MO_Immediate:
295     O << MO.getImm();
296     break;
297   case MachineOperand::MO_MachineBasicBlock:
298     O << *MO.getMBB()->getSymbol(OutContext);
299     break;
300   case MachineOperand::MO_GlobalAddress:
301     O << *GetGlobalValueSymbol(MO.getGlobal());
302     break;
303   case MachineOperand::MO_ExternalSymbol:
304     O << MO.getSymbolName();
305     break;
306   case MachineOperand::MO_ConstantPoolIndex:
307     O << MAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber()
308       << '_' << MO.getIndex();
309     break;
310   case MachineOperand::MO_JumpTableIndex:
311     O << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
312       << '_' << MO.getIndex();
313     break;
314   case MachineOperand::MO_BlockAddress:
315     O << *GetBlockAddressSymbol(MO.getBlockAddress());
316     break;
317   default:
318     llvm_unreachable("not implemented");
319   }
320 }
321
322 /// PrintAsmOperand - Print out an operand for an inline asm expression.
323 ///
324 bool XCoreAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
325                                       unsigned AsmVariant, 
326                                       const char *ExtraCode) {
327   printOperand(MI, OpNo);
328   return false;
329 }
330
331 void XCoreAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
332   ++EmittedInsts;
333
334   processDebugLoc(MI, true);
335
336   // Check for mov mnemonic
337   unsigned src, dst, srcSR, dstSR;
338   if (TM.getInstrInfo()->isMoveInstr(*MI, src, dst, srcSR, dstSR)) {
339     O << "\tmov " << getRegisterName(dst) << ", ";
340     O << getRegisterName(src) << '\n';
341     return;
342   }
343   printInstruction(MI);
344   if (VerboseAsm)
345     EmitComments(*MI);
346   O << '\n';
347
348   processDebugLoc(MI, false);
349 }
350
351 // Force static initialization.
352 extern "C" void LLVMInitializeXCoreAsmPrinter() { 
353   RegisterAsmPrinter<XCoreAsmPrinter> X(TheXCoreTarget);
354 }