Revert r107205 and r107207.
[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/MachineModuleInfo.h"
26 #include "llvm/CodeGen/MachineFunctionPass.h"
27 #include "llvm/CodeGen/MachineConstantPool.h"
28 #include "llvm/CodeGen/MachineInstr.h"
29 #include "llvm/CodeGen/MachineJumpTableInfo.h"
30 #include "llvm/MC/MCStreamer.h"
31 #include "llvm/MC/MCSymbol.h"
32 #include "llvm/Target/Mangler.h"
33 #include "llvm/Target/TargetData.h"
34 #include "llvm/Target/TargetLoweringObjectFile.h"
35 #include "llvm/Target/TargetRegistry.h"
36 #include "llvm/ADT/SmallString.h"
37 #include "llvm/ADT/StringExtras.h"
38 #include "llvm/Support/CommandLine.h"
39 #include "llvm/Support/ErrorHandling.h"
40 #include "llvm/Support/raw_ostream.h"
41 #include <algorithm>
42 #include <cctype>
43 using namespace llvm;
44
45 static cl::opt<unsigned> MaxThreads("xcore-max-threads", cl::Optional,
46   cl::desc("Maximum number of threads (for emulation thread-local storage)"),
47   cl::Hidden,
48   cl::value_desc("number"),
49   cl::init(8));
50
51 namespace {
52   class XCoreAsmPrinter : public AsmPrinter {
53     const XCoreSubtarget &Subtarget;
54   public:
55     explicit XCoreAsmPrinter(TargetMachine &TM, MCStreamer &Streamer)
56       : AsmPrinter(TM, Streamer), Subtarget(TM.getSubtarget<XCoreSubtarget>()){}
57
58     virtual const char *getPassName() const {
59       return "XCore Assembly Printer";
60     }
61
62     void printMemOperand(const MachineInstr *MI, int opNum, raw_ostream &O);
63     void printInlineJT(const MachineInstr *MI, int opNum, raw_ostream &O,
64                        const std::string &directive = ".jmptable");
65     void printInlineJT32(const MachineInstr *MI, int opNum, raw_ostream &O) {
66       printInlineJT(MI, opNum, O, ".jmptable32");
67     }
68     void printOperand(const MachineInstr *MI, int opNum, raw_ostream &O);
69     bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
70                          unsigned AsmVariant, const char *ExtraCode,
71                          raw_ostream &O);
72
73     void emitArrayBound(MCSymbol *Sym, const GlobalVariable *GV);
74     virtual void EmitGlobalVariable(const GlobalVariable *GV);
75
76     void printInstruction(const MachineInstr *MI, raw_ostream &O); // autogen'd.
77     static const char *getRegisterName(unsigned RegNo);
78
79     void EmitFunctionEntryLabel();
80     void EmitInstruction(const MachineInstr *MI);
81     void EmitFunctionBodyEnd();
82   };
83 } // end of anonymous namespace
84
85 #include "XCoreGenAsmWriter.inc"
86
87 void XCoreAsmPrinter::emitArrayBound(MCSymbol *Sym, const GlobalVariable *GV) {
88   assert(((GV->hasExternalLinkage() ||
89     GV->hasWeakLinkage()) ||
90     GV->hasLinkOnceLinkage()) && "Unexpected linkage");
91   if (const ArrayType *ATy = dyn_cast<ArrayType>(
92     cast<PointerType>(GV->getType())->getElementType())) {
93     OutStreamer.EmitSymbolAttribute(Sym, MCSA_Global);
94     // FIXME: MCStreamerize.
95     OutStreamer.EmitRawText(StringRef(".globound"));
96     OutStreamer.EmitRawText("\t.set\t" + Twine(Sym->getName()));
97     OutStreamer.EmitRawText(".globound," + Twine(ATy->getNumElements()));
98     if (GV->hasWeakLinkage() || GV->hasLinkOnceLinkage()) {
99       // TODO Use COMDAT groups for LinkOnceLinkage
100       OutStreamer.EmitRawText(MAI->getWeakDefDirective() +Twine(Sym->getName())+
101                               ".globound");
102     }
103   }
104 }
105
106 void XCoreAsmPrinter::EmitGlobalVariable(const GlobalVariable *GV) {
107   // Check to see if this is a special global used by LLVM, if so, emit it.
108   if (!GV->hasInitializer() ||
109       EmitSpecialLLVMGlobal(GV))
110     return;
111
112   const TargetData *TD = TM.getTargetData();
113   OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(GV, Mang,TM));
114
115   
116   MCSymbol *GVSym = Mang->getSymbol(GV);
117   Constant *C = GV->getInitializer();
118   unsigned Align = (unsigned)TD->getPreferredTypeAlignmentShift(C->getType());
119   
120   // Mark the start of the global
121   OutStreamer.EmitRawText("\t.cc_top " + Twine(GVSym->getName()) + ".data," +
122                           GVSym->getName());
123
124   switch (GV->getLinkage()) {
125   case GlobalValue::AppendingLinkage:
126     report_fatal_error("AppendingLinkage is not supported by this target!");
127   case GlobalValue::LinkOnceAnyLinkage:
128   case GlobalValue::LinkOnceODRLinkage:
129   case GlobalValue::WeakAnyLinkage:
130   case GlobalValue::WeakODRLinkage:
131   case GlobalValue::ExternalLinkage:
132     emitArrayBound(GVSym, GV);
133     OutStreamer.EmitSymbolAttribute(GVSym, MCSA_Global);
134
135     // TODO Use COMDAT groups for LinkOnceLinkage
136     if (GV->hasWeakLinkage() || GV->hasLinkOnceLinkage())
137       OutStreamer.EmitSymbolAttribute(GVSym, MCSA_Weak);
138     // FALL THROUGH
139   case GlobalValue::InternalLinkage:
140   case GlobalValue::PrivateLinkage:
141   case GlobalValue::LinkerPrivateLinkage:
142     break;
143   case GlobalValue::DLLImportLinkage:
144     llvm_unreachable("DLLImport linkage is not supported by this target!");
145   case GlobalValue::DLLExportLinkage:
146     llvm_unreachable("DLLExport linkage is not supported by this target!");
147   default:
148     llvm_unreachable("Unknown linkage type!");
149   }
150
151   EmitAlignment(Align > 2 ? Align : 2, GV);
152   
153   unsigned Size = TD->getTypeAllocSize(C->getType());
154   if (GV->isThreadLocal()) {
155     Size *= MaxThreads;
156   }
157   if (MAI->hasDotTypeDotSizeDirective()) {
158     OutStreamer.EmitSymbolAttribute(GVSym, MCSA_ELF_TypeObject);
159     OutStreamer.EmitRawText("\t.size " + Twine(GVSym->getName()) + "," +
160                             Twine(Size));
161   }
162   OutStreamer.EmitLabel(GVSym);
163   
164   EmitGlobalConstant(C);
165   if (GV->isThreadLocal()) {
166     for (unsigned i = 1; i < MaxThreads; ++i)
167       EmitGlobalConstant(C);
168   }
169   // The ABI requires that unsigned scalar types smaller than 32 bits
170   // are padded to 32 bits.
171   if (Size < 4)
172     OutStreamer.EmitZeros(4 - Size, 0);
173   
174   // Mark the end of the global
175   OutStreamer.EmitRawText("\t.cc_bottom " + Twine(GVSym->getName()) + ".data");
176 }
177
178 /// EmitFunctionBodyEnd - Targets can override this to emit stuff after
179 /// the last basic block in the function.
180 void XCoreAsmPrinter::EmitFunctionBodyEnd() {
181   // Emit function end directives
182   OutStreamer.EmitRawText("\t.cc_bottom " + Twine(CurrentFnSym->getName()) +
183                           ".function");
184 }
185
186 void XCoreAsmPrinter::EmitFunctionEntryLabel() {
187   // Mark the start of the function
188   OutStreamer.EmitRawText("\t.cc_top " + Twine(CurrentFnSym->getName()) +
189                           ".function," + CurrentFnSym->getName());
190   OutStreamer.EmitLabel(CurrentFnSym);
191 }
192
193 void XCoreAsmPrinter::printMemOperand(const MachineInstr *MI, int opNum,
194                                       raw_ostream &O) {
195   printOperand(MI, opNum, O);
196   
197   if (MI->getOperand(opNum+1).isImm() && MI->getOperand(opNum+1).getImm() == 0)
198     return;
199   
200   O << "+";
201   printOperand(MI, opNum+1, O);
202 }
203
204 void XCoreAsmPrinter::
205 printInlineJT(const MachineInstr *MI, int opNum, raw_ostream &O,
206               const std::string &directive) {
207   unsigned JTI = MI->getOperand(opNum).getIndex();
208   const MachineFunction *MF = MI->getParent()->getParent();
209   const MachineJumpTableInfo *MJTI = MF->getJumpTableInfo();
210   const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
211   const std::vector<MachineBasicBlock*> &JTBBs = JT[JTI].MBBs;
212   O << "\t" << directive << " ";
213   for (unsigned i = 0, e = JTBBs.size(); i != e; ++i) {
214     MachineBasicBlock *MBB = JTBBs[i];
215     if (i > 0)
216       O << ",";
217     O << *MBB->getSymbol();
218   }
219 }
220
221 void XCoreAsmPrinter::printOperand(const MachineInstr *MI, int opNum,
222                                    raw_ostream &O) {
223   const MachineOperand &MO = MI->getOperand(opNum);
224   switch (MO.getType()) {
225   case MachineOperand::MO_Register:
226     O << getRegisterName(MO.getReg());
227     break;
228   case MachineOperand::MO_Immediate:
229     O << MO.getImm();
230     break;
231   case MachineOperand::MO_MachineBasicBlock:
232     O << *MO.getMBB()->getSymbol();
233     break;
234   case MachineOperand::MO_GlobalAddress:
235     O << *Mang->getSymbol(MO.getGlobal());
236     break;
237   case MachineOperand::MO_ExternalSymbol:
238     O << MO.getSymbolName();
239     break;
240   case MachineOperand::MO_ConstantPoolIndex:
241     O << MAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber()
242       << '_' << MO.getIndex();
243     break;
244   case MachineOperand::MO_JumpTableIndex:
245     O << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
246       << '_' << MO.getIndex();
247     break;
248   case MachineOperand::MO_BlockAddress:
249     O << *GetBlockAddressSymbol(MO.getBlockAddress());
250     break;
251   default:
252     llvm_unreachable("not implemented");
253   }
254 }
255
256 /// PrintAsmOperand - Print out an operand for an inline asm expression.
257 ///
258 bool XCoreAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
259                                       unsigned AsmVariant,const char *ExtraCode,
260                                       raw_ostream &O) {
261   printOperand(MI, OpNo, O);
262   return false;
263 }
264
265 void XCoreAsmPrinter::EmitInstruction(const MachineInstr *MI) {
266   SmallString<128> Str;
267   raw_svector_ostream O(Str);
268   
269   // Check for mov mnemonic
270   unsigned src, dst, srcSR, dstSR;
271   if (TM.getInstrInfo()->isMoveInstr(*MI, src, dst, srcSR, dstSR)) {
272     O << "\tmov " << getRegisterName(dst) << ", ";
273     O << getRegisterName(src);
274   } else {
275     printInstruction(MI, O);
276   }
277   OutStreamer.EmitRawText(O.str());
278 }
279
280 // Force static initialization.
281 extern "C" void LLVMInitializeXCoreAsmPrinter() { 
282   RegisterAsmPrinter<XCoreAsmPrinter> X(TheXCoreTarget);
283 }