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