fix PrintAsmOperand and PrintAsmMemoryOperand to pass down
[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, raw_ostream &O);
66     void printInlineJT(const MachineInstr *MI, int opNum, raw_ostream &O,
67                        const std::string &directive = ".jmptable");
68     void printInlineJT32(const MachineInstr *MI, int opNum, raw_ostream &O) {
69       printInlineJT(MI, opNum, O, ".jmptable32");
70     }
71     void printOperand(const MachineInstr *MI, int opNum, raw_ostream &O);
72     bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
73                          unsigned AsmVariant, const char *ExtraCode,
74                          raw_ostream &O);
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, raw_ostream &O); // autogen'd.
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                                       raw_ostream &O) {
257   printOperand(MI, opNum, O);
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, O);
265 }
266
267 void XCoreAsmPrinter::
268 printInlineJT(const MachineInstr *MI, int opNum, raw_ostream &O,
269               const std::string &directive) {
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();
281   }
282 }
283
284 void XCoreAsmPrinter::printOperand(const MachineInstr *MI, int opNum,
285                                    raw_ostream &O) {
286   const MachineOperand &MO = MI->getOperand(opNum);
287   switch (MO.getType()) {
288   case MachineOperand::MO_Register:
289     O << getRegisterName(MO.getReg());
290     break;
291   case MachineOperand::MO_Immediate:
292     O << MO.getImm();
293     break;
294   case MachineOperand::MO_MachineBasicBlock:
295     O << *MO.getMBB()->getSymbol();
296     break;
297   case MachineOperand::MO_GlobalAddress:
298     O << *Mang->getSymbol(MO.getGlobal());
299     break;
300   case MachineOperand::MO_ExternalSymbol:
301     O << MO.getSymbolName();
302     break;
303   case MachineOperand::MO_ConstantPoolIndex:
304     O << MAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber()
305       << '_' << MO.getIndex();
306     break;
307   case MachineOperand::MO_JumpTableIndex:
308     O << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
309       << '_' << MO.getIndex();
310     break;
311   case MachineOperand::MO_BlockAddress:
312     O << *GetBlockAddressSymbol(MO.getBlockAddress());
313     break;
314   default:
315     llvm_unreachable("not implemented");
316   }
317 }
318
319 /// PrintAsmOperand - Print out an operand for an inline asm expression.
320 ///
321 bool XCoreAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
322                                       unsigned AsmVariant,const char *ExtraCode,
323                                       raw_ostream &O) {
324   printOperand(MI, OpNo, O);
325   return false;
326 }
327
328 void XCoreAsmPrinter::EmitInstruction(const MachineInstr *MI) {
329   // Check for mov mnemonic
330   unsigned src, dst, srcSR, dstSR;
331   if (TM.getInstrInfo()->isMoveInstr(*MI, src, dst, srcSR, dstSR)) {
332     O << "\tmov " << getRegisterName(dst) << ", ";
333     O << getRegisterName(src);
334     OutStreamer.AddBlankLine();
335     return;
336   }
337   printInstruction(MI, O);
338   OutStreamer.AddBlankLine();
339 }
340
341 // Force static initialization.
342 extern "C" void LLVMInitializeXCoreAsmPrinter() { 
343   RegisterAsmPrinter<XCoreAsmPrinter> X(TheXCoreTarget);
344 }