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