1 //===-- XCoreAsmPrinter.cpp - XCore LLVM assembly writer ------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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.
13 //===----------------------------------------------------------------------===//
15 #define DEBUG_TYPE "asm-printer"
17 #include "XCoreInstrInfo.h"
18 #include "XCoreSubtarget.h"
19 #include "XCoreTargetMachine.h"
20 #include "llvm/Constants.h"
21 #include "llvm/DerivedTypes.h"
22 #include "llvm/Module.h"
23 #include "llvm/Analysis/DebugInfo.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/MCAsmInfo.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/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/TargetRegistry.h"
41 #include "llvm/Support/raw_ostream.h"
46 static cl::opt<unsigned> MaxThreads("xcore-max-threads", cl::Optional,
47 cl::desc("Maximum number of threads (for emulation thread-local storage)"),
49 cl::value_desc("number"),
53 class XCoreAsmPrinter : public AsmPrinter {
54 const XCoreSubtarget &Subtarget;
55 void PrintDebugValueComment(const MachineInstr *MI, raw_ostream &OS);
57 explicit XCoreAsmPrinter(TargetMachine &TM, MCStreamer &Streamer)
58 : AsmPrinter(TM, Streamer), Subtarget(TM.getSubtarget<XCoreSubtarget>()){}
60 virtual const char *getPassName() const {
61 return "XCore Assembly Printer";
64 void printMemOperand(const MachineInstr *MI, int opNum, raw_ostream &O);
65 void printInlineJT(const MachineInstr *MI, int opNum, raw_ostream &O,
66 const std::string &directive = ".jmptable");
67 void printInlineJT32(const MachineInstr *MI, int opNum, raw_ostream &O) {
68 printInlineJT(MI, opNum, O, ".jmptable32");
70 void printOperand(const MachineInstr *MI, int opNum, raw_ostream &O);
71 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
72 unsigned AsmVariant, const char *ExtraCode,
75 void emitArrayBound(MCSymbol *Sym, const GlobalVariable *GV);
76 virtual void EmitGlobalVariable(const GlobalVariable *GV);
78 void printInstruction(const MachineInstr *MI, raw_ostream &O); // autogen'd.
79 static const char *getRegisterName(unsigned RegNo);
81 void EmitFunctionEntryLabel();
82 void EmitInstruction(const MachineInstr *MI);
83 void EmitFunctionBodyEnd();
84 virtual MachineLocation getDebugValueLocation(const MachineInstr *MI) const;
86 } // end of anonymous namespace
88 #include "XCoreGenAsmWriter.inc"
90 void XCoreAsmPrinter::emitArrayBound(MCSymbol *Sym, const GlobalVariable *GV) {
91 assert(((GV->hasExternalLinkage() ||
92 GV->hasWeakLinkage()) ||
93 GV->hasLinkOnceLinkage()) && "Unexpected linkage");
94 if (ArrayType *ATy = dyn_cast<ArrayType>(
95 cast<PointerType>(GV->getType())->getElementType())) {
96 OutStreamer.EmitSymbolAttribute(Sym, MCSA_Global);
97 // FIXME: MCStreamerize.
98 OutStreamer.EmitRawText(StringRef(".globound"));
99 OutStreamer.EmitRawText("\t.set\t" + Twine(Sym->getName()));
100 OutStreamer.EmitRawText(".globound," + Twine(ATy->getNumElements()));
101 if (GV->hasWeakLinkage() || GV->hasLinkOnceLinkage()) {
102 // TODO Use COMDAT groups for LinkOnceLinkage
103 OutStreamer.EmitRawText(MAI->getWeakDefDirective() +Twine(Sym->getName())+
109 void XCoreAsmPrinter::EmitGlobalVariable(const GlobalVariable *GV) {
110 // Check to see if this is a special global used by LLVM, if so, emit it.
111 if (!GV->hasInitializer() ||
112 EmitSpecialLLVMGlobal(GV))
115 const TargetData *TD = TM.getTargetData();
116 OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(GV, Mang,TM));
119 MCSymbol *GVSym = Mang->getSymbol(GV);
120 const Constant *C = GV->getInitializer();
121 unsigned Align = (unsigned)TD->getPreferredTypeAlignmentShift(C->getType());
123 // Mark the start of the global
124 OutStreamer.EmitRawText("\t.cc_top " + Twine(GVSym->getName()) + ".data," +
127 switch (GV->getLinkage()) {
128 case GlobalValue::AppendingLinkage:
129 report_fatal_error("AppendingLinkage is not supported by this target!");
130 case GlobalValue::LinkOnceAnyLinkage:
131 case GlobalValue::LinkOnceODRLinkage:
132 case GlobalValue::WeakAnyLinkage:
133 case GlobalValue::WeakODRLinkage:
134 case GlobalValue::ExternalLinkage:
135 emitArrayBound(GVSym, GV);
136 OutStreamer.EmitSymbolAttribute(GVSym, MCSA_Global);
138 // TODO Use COMDAT groups for LinkOnceLinkage
139 if (GV->hasWeakLinkage() || GV->hasLinkOnceLinkage())
140 OutStreamer.EmitSymbolAttribute(GVSym, MCSA_Weak);
142 case GlobalValue::InternalLinkage:
143 case GlobalValue::PrivateLinkage:
145 case GlobalValue::DLLImportLinkage:
146 llvm_unreachable("DLLImport linkage is not supported by this target!");
147 case GlobalValue::DLLExportLinkage:
148 llvm_unreachable("DLLExport linkage is not supported by this target!");
150 llvm_unreachable("Unknown linkage type!");
153 EmitAlignment(Align > 2 ? Align : 2, GV);
155 unsigned Size = TD->getTypeAllocSize(C->getType());
156 if (GV->isThreadLocal()) {
159 if (MAI->hasDotTypeDotSizeDirective()) {
160 OutStreamer.EmitSymbolAttribute(GVSym, MCSA_ELF_TypeObject);
161 OutStreamer.EmitRawText("\t.size " + Twine(GVSym->getName()) + "," +
164 OutStreamer.EmitLabel(GVSym);
166 EmitGlobalConstant(C);
167 if (GV->isThreadLocal()) {
168 for (unsigned i = 1; i < MaxThreads; ++i)
169 EmitGlobalConstant(C);
171 // The ABI requires that unsigned scalar types smaller than 32 bits
172 // are padded to 32 bits.
174 OutStreamer.EmitZeros(4 - Size, 0);
176 // Mark the end of the global
177 OutStreamer.EmitRawText("\t.cc_bottom " + Twine(GVSym->getName()) + ".data");
180 /// EmitFunctionBodyEnd - Targets can override this to emit stuff after
181 /// the last basic block in the function.
182 void XCoreAsmPrinter::EmitFunctionBodyEnd() {
183 // Emit function end directives
184 OutStreamer.EmitRawText("\t.cc_bottom " + Twine(CurrentFnSym->getName()) +
188 void XCoreAsmPrinter::EmitFunctionEntryLabel() {
189 // Mark the start of the function
190 OutStreamer.EmitRawText("\t.cc_top " + Twine(CurrentFnSym->getName()) +
191 ".function," + CurrentFnSym->getName());
192 OutStreamer.EmitLabel(CurrentFnSym);
195 void XCoreAsmPrinter::printMemOperand(const MachineInstr *MI, int opNum,
197 printOperand(MI, opNum, O);
199 if (MI->getOperand(opNum+1).isImm() && MI->getOperand(opNum+1).getImm() == 0)
203 printOperand(MI, opNum+1, O);
206 void XCoreAsmPrinter::
207 printInlineJT(const MachineInstr *MI, int opNum, raw_ostream &O,
208 const std::string &directive) {
209 unsigned JTI = MI->getOperand(opNum).getIndex();
210 const MachineFunction *MF = MI->getParent()->getParent();
211 const MachineJumpTableInfo *MJTI = MF->getJumpTableInfo();
212 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
213 const std::vector<MachineBasicBlock*> &JTBBs = JT[JTI].MBBs;
214 O << "\t" << directive << " ";
215 for (unsigned i = 0, e = JTBBs.size(); i != e; ++i) {
216 MachineBasicBlock *MBB = JTBBs[i];
219 O << *MBB->getSymbol();
223 void XCoreAsmPrinter::printOperand(const MachineInstr *MI, int opNum,
225 const MachineOperand &MO = MI->getOperand(opNum);
226 switch (MO.getType()) {
227 case MachineOperand::MO_Register:
228 O << getRegisterName(MO.getReg());
230 case MachineOperand::MO_Immediate:
233 case MachineOperand::MO_MachineBasicBlock:
234 O << *MO.getMBB()->getSymbol();
236 case MachineOperand::MO_GlobalAddress:
237 O << *Mang->getSymbol(MO.getGlobal());
239 case MachineOperand::MO_ExternalSymbol:
240 O << MO.getSymbolName();
242 case MachineOperand::MO_ConstantPoolIndex:
243 O << MAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber()
244 << '_' << MO.getIndex();
246 case MachineOperand::MO_JumpTableIndex:
247 O << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
248 << '_' << MO.getIndex();
250 case MachineOperand::MO_BlockAddress:
251 O << *GetBlockAddressSymbol(MO.getBlockAddress());
254 llvm_unreachable("not implemented");
258 /// PrintAsmOperand - Print out an operand for an inline asm expression.
260 bool XCoreAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
261 unsigned AsmVariant,const char *ExtraCode,
263 printOperand(MI, OpNo, O);
267 void XCoreAsmPrinter::PrintDebugValueComment(const MachineInstr *MI,
269 unsigned NOps = MI->getNumOperands();
271 OS << '\t' << MAI->getCommentString() << "DEBUG_VALUE: ";
272 // cast away const; DIetc do not take const operands for some reason.
273 DIVariable V(const_cast<MDNode *>(MI->getOperand(NOps-1).getMetadata()));
276 // Frame address. Currently handles register +- offset only.
277 assert(MI->getOperand(0).isReg() && MI->getOperand(1).isImm());
278 OS << '['; printOperand(MI, 0, OS); OS << '+'; printOperand(MI, 1, OS);
281 printOperand(MI, NOps-2, OS);
284 MachineLocation XCoreAsmPrinter::
285 getDebugValueLocation(const MachineInstr *MI) const {
286 // Handles frame addresses emitted in XCoreInstrInfo::emitFrameIndexDebugValue.
287 assert(MI->getNumOperands() == 4 && "Invalid no. of machine operands!");
288 assert(MI->getOperand(0).isReg() && MI->getOperand(1).isImm() &&
289 "Unexpected MachineOperand types");
290 return MachineLocation(MI->getOperand(0).getReg(),
291 MI->getOperand(1).getImm());
294 void XCoreAsmPrinter::EmitInstruction(const MachineInstr *MI) {
295 SmallString<128> Str;
296 raw_svector_ostream O(Str);
298 switch (MI->getOpcode()) {
299 case XCore::DBG_VALUE: {
300 if (isVerbose() && OutStreamer.hasRawTextSupport()) {
301 SmallString<128> TmpStr;
302 raw_svector_ostream OS(TmpStr);
303 PrintDebugValueComment(MI, OS);
304 OutStreamer.EmitRawText(StringRef(OS.str()));
308 case XCore::ADD_2rus:
309 if (MI->getOperand(2).getImm() == 0) {
310 O << "\tmov " << getRegisterName(MI->getOperand(0).getReg()) << ", "
311 << getRegisterName(MI->getOperand(1).getReg());
312 OutStreamer.EmitRawText(O.str());
317 printInstruction(MI, O);
318 OutStreamer.EmitRawText(O.str());
321 // Force static initialization.
322 extern "C" void LLVMInitializeXCoreAsmPrinter() {
323 RegisterAsmPrinter<XCoreAsmPrinter> X(TheXCoreTarget);