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/CodeGen/AsmPrinter.h"
24 #include "llvm/CodeGen/DwarfWriter.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/Target/TargetAsmInfo.h"
30 #include "llvm/Target/TargetData.h"
31 #include "llvm/Support/Mangler.h"
32 #include "llvm/ADT/Statistic.h"
33 #include "llvm/ADT/StringExtras.h"
34 #include "llvm/Support/CommandLine.h"
35 #include "llvm/Support/MathExtras.h"
36 #include "llvm/Support/raw_ostream.h"
41 STATISTIC(EmittedInsts, "Number of machine instrs printed");
43 static cl::opt<std::string> FileDirective("xcore-file-directive", cl::Optional,
44 cl::desc("Output a file directive into the assembly file"),
46 cl::value_desc("filename"),
49 static cl::opt<unsigned> MaxThreads("xcore-max-threads", cl::Optional,
50 cl::desc("Maximum number of threads (for emulation thread-local storage)"),
52 cl::value_desc("number"),
56 struct VISIBILITY_HIDDEN XCoreAsmPrinter : public AsmPrinter {
57 XCoreAsmPrinter(raw_ostream &O, XCoreTargetMachine &TM,
58 const TargetAsmInfo *T)
59 : AsmPrinter(O, TM, T), DW(0),
60 Subtarget(*TM.getSubtargetImpl()) { }
63 const XCoreSubtarget &Subtarget;
65 virtual const char *getPassName() const {
66 return "XCore Assembly Printer";
69 void printMemOperand(const MachineInstr *MI, int opNum);
70 void printOperand(const MachineInstr *MI, int opNum);
71 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
72 unsigned AsmVariant, const char *ExtraCode);
74 void emitFileDirective(const std::string &filename);
75 void emitGlobalDirective(const std::string &name);
76 void emitExternDirective(const std::string &name);
78 void emitArrayBound(const std::string &name, const GlobalVariable *GV);
79 void emitGlobal(const GlobalVariable *GV);
81 void emitFunctionStart(MachineFunction &MF);
82 void emitFunctionEnd(MachineFunction &MF);
84 bool printInstruction(const MachineInstr *MI); // autogenerated.
85 void printMachineInstruction(const MachineInstr *MI);
86 bool runOnMachineFunction(MachineFunction &F);
87 bool doInitialization(Module &M);
88 bool doFinalization(Module &M);
90 void getAnalysisUsage(AnalysisUsage &AU) const {
91 AsmPrinter::getAnalysisUsage(AU);
93 AU.addRequired<MachineModuleInfo>();
94 AU.addRequired<DwarfWriter>();
97 } // end of anonymous namespace
99 #include "XCoreGenAsmWriter.inc"
101 /// createXCoreCodePrinterPass - Returns a pass that prints the XCore
102 /// assembly code for a MachineFunction to the given output stream,
103 /// using the given target machine description. This should work
104 /// regardless of whether the function is in SSA form.
106 FunctionPass *llvm::createXCoreCodePrinterPass(raw_ostream &o,
107 XCoreTargetMachine &tm) {
108 return new XCoreAsmPrinter(o, tm, tm.getTargetAsmInfo());
111 // PrintEscapedString - Print each character of the specified string, escaping
112 // it if it is not printable or if it is an escape char.
113 static void PrintEscapedString(const std::string &Str, raw_ostream &Out) {
114 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
115 unsigned char C = Str[i];
116 if (isprint(C) && C != '"' && C != '\\') {
120 << (char) ((C/16 < 10) ? ( C/16 +'0') : ( C/16 -10+'A'))
121 << (char)(((C&15) < 10) ? ((C&15)+'0') : ((C&15)-10+'A'));
126 void XCoreAsmPrinter::
127 emitFileDirective(const std::string &name)
130 PrintEscapedString(name, O);
134 void XCoreAsmPrinter::
135 emitGlobalDirective(const std::string &name)
137 O << TAI->getGlobalDirective() << name;
141 void XCoreAsmPrinter::
142 emitExternDirective(const std::string &name)
144 O << "\t.extern\t" << name;
148 void XCoreAsmPrinter::
149 emitArrayBound(const std::string &name, const GlobalVariable *GV)
151 assert(((GV->hasExternalLinkage() ||
152 GV->hasWeakLinkage()) ||
153 GV->hasLinkOnceLinkage()) && "Unexpected linkage");
154 if (const ArrayType *ATy = dyn_cast<ArrayType>(
155 cast<PointerType>(GV->getType())->getElementType()))
157 O << TAI->getGlobalDirective() << name << ".globound" << "\n";
158 O << TAI->getSetDirective() << name << ".globound" << ","
159 << ATy->getNumElements() << "\n";
160 if (GV->hasWeakLinkage() || GV->hasLinkOnceLinkage()) {
161 // TODO Use COMDAT groups for LinkOnceLinkage
162 O << TAI->getWeakDefDirective() << name << ".globound" << "\n";
167 void XCoreAsmPrinter::
168 emitGlobal(const GlobalVariable *GV)
170 const TargetData *TD = TM.getTargetData();
172 if (GV->hasInitializer()) {
173 // Check to see if this is a special global used by LLVM, if so, emit it.
174 if (EmitSpecialLLVMGlobal(GV))
177 SwitchToSection(TAI->SectionForGlobal(GV));
179 std::string name = Mang->getValueName(GV);
180 Constant *C = GV->getInitializer();
181 unsigned Align = (unsigned)TD->getPreferredTypeAlignmentShift(C->getType());
183 // Mark the start of the global
184 O << "\t.cc_top " << name << ".data," << name << "\n";
186 switch (GV->getLinkage()) {
187 case GlobalValue::AppendingLinkage:
188 cerr << "AppendingLinkage is not supported by this target!\n";
190 case GlobalValue::LinkOnceLinkage:
191 case GlobalValue::WeakLinkage:
192 case GlobalValue::ExternalLinkage:
193 emitArrayBound(name, GV);
194 emitGlobalDirective(name);
195 // TODO Use COMDAT groups for LinkOnceLinkage
196 if (GV->hasWeakLinkage() || GV->hasLinkOnceLinkage()) {
197 O << TAI->getWeakDefDirective() << name << "\n";
200 case GlobalValue::InternalLinkage:
201 case GlobalValue::PrivateLinkage:
203 case GlobalValue::GhostLinkage:
204 cerr << "Should not have any unmaterialized functions!\n";
206 case GlobalValue::DLLImportLinkage:
207 cerr << "DLLImport linkage is not supported by this target!\n";
209 case GlobalValue::DLLExportLinkage:
210 cerr << "DLLExport linkage is not supported by this target!\n";
213 assert(0 && "Unknown linkage type!");
216 EmitAlignment(Align, GV, 2);
218 unsigned Size = TD->getTypePaddedSize(C->getType());
219 if (GV->isThreadLocal()) {
222 if (TAI->hasDotTypeDotSizeDirective()) {
223 O << "\t.type " << name << ",@object\n";
224 O << "\t.size " << name << "," << Size << "\n";
228 EmitGlobalConstant(C);
229 if (GV->isThreadLocal()) {
230 for (unsigned i = 1; i < MaxThreads; ++i) {
231 EmitGlobalConstant(C);
235 // The ABI requires that unsigned scalar types smaller than 32 bits
236 // are are padded to 32 bits.
240 // Mark the end of the global
241 O << "\t.cc_bottom " << name << ".data\n";
243 if (GV->hasExternalWeakLinkage())
244 ExtWeakSymbols.insert(GV);
248 /// Emit the directives on the start of functions
249 void XCoreAsmPrinter::
250 emitFunctionStart(MachineFunction &MF)
252 // Print out the label for the function.
253 const Function *F = MF.getFunction();
255 SwitchToSection(TAI->SectionForGlobal(F));
257 // Mark the start of the function
258 O << "\t.cc_top " << CurrentFnName << ".function," << CurrentFnName << "\n";
260 switch (F->getLinkage()) {
261 default: assert(0 && "Unknown linkage type!");
262 case Function::InternalLinkage: // Symbols default to internal.
263 case Function::PrivateLinkage:
265 case Function::ExternalLinkage:
266 emitGlobalDirective(CurrentFnName);
268 case Function::LinkOnceLinkage:
269 case Function::WeakLinkage:
270 // TODO Use COMDAT groups for LinkOnceLinkage
271 O << TAI->getGlobalDirective() << CurrentFnName << "\n";
272 O << TAI->getWeakDefDirective() << CurrentFnName << "\n";
275 // (1 << 1) byte aligned
276 EmitAlignment(1, F, 1);
277 if (TAI->hasDotTypeDotSizeDirective()) {
278 O << "\t.type " << CurrentFnName << ",@function\n";
280 O << CurrentFnName << ":\n";
283 /// Emit the directives on the end of functions
284 void XCoreAsmPrinter::
285 emitFunctionEnd(MachineFunction &MF)
287 // Mark the end of the function
288 O << "\t.cc_bottom " << CurrentFnName << ".function\n";
291 /// runOnMachineFunction - This uses the printMachineInstruction()
292 /// method to print assembly for each instruction.
294 bool XCoreAsmPrinter::runOnMachineFunction(MachineFunction &MF)
296 SetupMachineFunction(MF);
298 // Print out constants referenced by the function
299 EmitConstantPool(MF.getConstantPool());
301 // Print out jump tables referenced by the function
302 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
304 // Emit the function start directives
305 emitFunctionStart(MF);
307 // Emit pre-function debug information.
308 DW->BeginFunction(&MF);
310 // Print out code for the function.
311 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
314 // Print a label for the basic block.
315 if (I != MF.begin()) {
316 printBasicBlockLabel(I, true , true);
320 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
322 // Print the assembly for the instruction.
324 printMachineInstruction(II);
327 // Each Basic Block is separated by a newline
331 // Emit function end directives
334 // Emit post-function debug information.
335 DW->EndFunction(&MF);
337 // We didn't modify anything.
341 void XCoreAsmPrinter::printMemOperand(const MachineInstr *MI, int opNum)
343 printOperand(MI, opNum);
345 if (MI->getOperand(opNum+1).isImm()
346 && MI->getOperand(opNum+1).getImm() == 0)
350 printOperand(MI, opNum+1);
353 void XCoreAsmPrinter::printOperand(const MachineInstr *MI, int opNum) {
354 const MachineOperand &MO = MI->getOperand(opNum);
355 switch (MO.getType()) {
356 case MachineOperand::MO_Register:
357 if (TargetRegisterInfo::isPhysicalRegister(MO.getReg()))
358 O << TM.getRegisterInfo()->get(MO.getReg()).AsmName;
360 assert(0 && "not implemented");
362 case MachineOperand::MO_Immediate:
365 case MachineOperand::MO_MachineBasicBlock:
366 printBasicBlockLabel(MO.getMBB());
368 case MachineOperand::MO_GlobalAddress:
370 const GlobalValue *GV = MO.getGlobal();
371 O << Mang->getValueName(GV);
372 if (GV->hasExternalWeakLinkage())
373 ExtWeakSymbols.insert(GV);
376 case MachineOperand::MO_ExternalSymbol:
377 O << MO.getSymbolName();
379 case MachineOperand::MO_ConstantPoolIndex:
380 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber()
381 << '_' << MO.getIndex();
383 case MachineOperand::MO_JumpTableIndex:
384 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
385 << '_' << MO.getIndex();
388 assert(0 && "not implemented");
392 /// PrintAsmOperand - Print out an operand for an inline asm expression.
394 bool XCoreAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
396 const char *ExtraCode) {
397 printOperand(MI, OpNo);
401 void XCoreAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
404 // Check for mov mnemonic
405 unsigned src, dst, srcSR, dstSR;
406 if (TM.getInstrInfo()->isMoveInstr(*MI, src, dst, srcSR, dstSR)) {
408 O << TM.getRegisterInfo()->get(dst).AsmName;
410 O << TM.getRegisterInfo()->get(src).AsmName;
414 if (printInstruction(MI)) {
417 assert(0 && "Unhandled instruction in asm writer!");
420 bool XCoreAsmPrinter::doInitialization(Module &M) {
421 bool Result = AsmPrinter::doInitialization(M);
423 if (!FileDirective.empty()) {
424 emitFileDirective(FileDirective);
427 // Print out type strings for external functions here
428 for (Module::const_iterator I = M.begin(), E = M.end();
430 if (I->isDeclaration() && !I->isIntrinsic()) {
431 switch (I->getLinkage()) {
433 assert(0 && "Unexpected linkage");
434 case Function::ExternalWeakLinkage:
435 ExtWeakSymbols.insert(I);
437 case Function::ExternalLinkage:
443 // Emit initial debug information.
444 DW = getAnalysisIfAvailable<DwarfWriter>();
445 assert(DW && "Dwarf Writer is not available");
446 DW->BeginModule(&M, getAnalysisIfAvailable<MachineModuleInfo>(),
451 bool XCoreAsmPrinter::doFinalization(Module &M) {
453 // Print out module-level global variables.
454 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
459 // Emit final debug information.
462 return AsmPrinter::doFinalization(M);