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