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 class VISIBILITY_HIDDEN XCoreAsmPrinter : public AsmPrinter {
58 const XCoreSubtarget &Subtarget;
60 XCoreAsmPrinter(raw_ostream &O, XCoreTargetMachine &TM,
61 const TargetAsmInfo *T, unsigned OL, bool V)
62 : AsmPrinter(O, TM, T, OL, V), DW(0),
63 Subtarget(*TM.getSubtargetImpl()) {}
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,
110 return new XCoreAsmPrinter(o, tm, tm.getTargetAsmInfo(), OptLevel, verbose);
113 // PrintEscapedString - Print each character of the specified string, escaping
114 // it if it is not printable or if it is an escape char.
115 static void PrintEscapedString(const std::string &Str, raw_ostream &Out) {
116 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
117 unsigned char C = Str[i];
118 if (isprint(C) && C != '"' && C != '\\') {
122 << (char) ((C/16 < 10) ? ( C/16 +'0') : ( C/16 -10+'A'))
123 << (char)(((C&15) < 10) ? ((C&15)+'0') : ((C&15)-10+'A'));
128 void XCoreAsmPrinter::
129 emitFileDirective(const std::string &name)
132 PrintEscapedString(name, O);
136 void XCoreAsmPrinter::
137 emitGlobalDirective(const std::string &name)
139 O << TAI->getGlobalDirective() << name;
143 void XCoreAsmPrinter::
144 emitExternDirective(const std::string &name)
146 O << "\t.extern\t" << name;
150 void XCoreAsmPrinter::
151 emitArrayBound(const std::string &name, const GlobalVariable *GV)
153 assert(((GV->hasExternalLinkage() ||
154 GV->hasWeakLinkage()) ||
155 GV->hasLinkOnceLinkage()) && "Unexpected linkage");
156 if (const ArrayType *ATy = dyn_cast<ArrayType>(
157 cast<PointerType>(GV->getType())->getElementType()))
159 O << TAI->getGlobalDirective() << name << ".globound" << "\n";
160 O << TAI->getSetDirective() << name << ".globound" << ","
161 << ATy->getNumElements() << "\n";
162 if (GV->hasWeakLinkage() || GV->hasLinkOnceLinkage()) {
163 // TODO Use COMDAT groups for LinkOnceLinkage
164 O << TAI->getWeakDefDirective() << name << ".globound" << "\n";
169 void XCoreAsmPrinter::
170 emitGlobal(const GlobalVariable *GV)
172 const TargetData *TD = TM.getTargetData();
174 if (GV->hasInitializer()) {
175 // Check to see if this is a special global used by LLVM, if so, emit it.
176 if (EmitSpecialLLVMGlobal(GV))
179 SwitchToSection(TAI->SectionForGlobal(GV));
181 std::string name = Mang->getValueName(GV);
182 Constant *C = GV->getInitializer();
183 unsigned Align = (unsigned)TD->getPreferredTypeAlignmentShift(C->getType());
185 // Mark the start of the global
186 O << "\t.cc_top " << name << ".data," << name << "\n";
188 switch (GV->getLinkage()) {
189 case GlobalValue::AppendingLinkage:
190 cerr << "AppendingLinkage is not supported by this target!\n";
192 case GlobalValue::LinkOnceAnyLinkage:
193 case GlobalValue::LinkOnceODRLinkage:
194 case GlobalValue::WeakAnyLinkage:
195 case GlobalValue::WeakODRLinkage:
196 case GlobalValue::ExternalLinkage:
197 emitArrayBound(name, GV);
198 emitGlobalDirective(name);
199 // TODO Use COMDAT groups for LinkOnceLinkage
200 if (GV->hasWeakLinkage() || GV->hasLinkOnceLinkage()) {
201 O << TAI->getWeakDefDirective() << name << "\n";
204 case GlobalValue::InternalLinkage:
205 case GlobalValue::PrivateLinkage:
207 case GlobalValue::GhostLinkage:
208 cerr << "Should not have any unmaterialized functions!\n";
210 case GlobalValue::DLLImportLinkage:
211 cerr << "DLLImport linkage is not supported by this target!\n";
213 case GlobalValue::DLLExportLinkage:
214 cerr << "DLLExport linkage is not supported by this target!\n";
217 assert(0 && "Unknown linkage type!");
220 EmitAlignment(Align, GV, 2);
222 unsigned Size = TD->getTypePaddedSize(C->getType());
223 if (GV->isThreadLocal()) {
226 if (TAI->hasDotTypeDotSizeDirective()) {
227 O << "\t.type " << name << ",@object\n";
228 O << "\t.size " << name << "," << Size << "\n";
232 EmitGlobalConstant(C);
233 if (GV->isThreadLocal()) {
234 for (unsigned i = 1; i < MaxThreads; ++i) {
235 EmitGlobalConstant(C);
239 // The ABI requires that unsigned scalar types smaller than 32 bits
240 // are are padded to 32 bits.
244 // Mark the end of the global
245 O << "\t.cc_bottom " << name << ".data\n";
247 if (GV->hasExternalWeakLinkage())
248 ExtWeakSymbols.insert(GV);
252 /// Emit the directives on the start of functions
253 void XCoreAsmPrinter::
254 emitFunctionStart(MachineFunction &MF)
256 // Print out the label for the function.
257 const Function *F = MF.getFunction();
259 SwitchToSection(TAI->SectionForGlobal(F));
261 // Mark the start of the function
262 O << "\t.cc_top " << CurrentFnName << ".function," << CurrentFnName << "\n";
264 switch (F->getLinkage()) {
265 default: assert(0 && "Unknown linkage type!");
266 case Function::InternalLinkage: // Symbols default to internal.
267 case Function::PrivateLinkage:
269 case Function::ExternalLinkage:
270 emitGlobalDirective(CurrentFnName);
272 case Function::LinkOnceAnyLinkage:
273 case Function::LinkOnceODRLinkage:
274 case Function::WeakAnyLinkage:
275 case Function::WeakODRLinkage:
276 // TODO Use COMDAT groups for LinkOnceLinkage
277 O << TAI->getGlobalDirective() << CurrentFnName << "\n";
278 O << TAI->getWeakDefDirective() << CurrentFnName << "\n";
281 // (1 << 1) byte aligned
282 EmitAlignment(1, F, 1);
283 if (TAI->hasDotTypeDotSizeDirective()) {
284 O << "\t.type " << CurrentFnName << ",@function\n";
286 O << CurrentFnName << ":\n";
289 /// Emit the directives on the end of functions
290 void XCoreAsmPrinter::
291 emitFunctionEnd(MachineFunction &MF)
293 // Mark the end of the function
294 O << "\t.cc_bottom " << CurrentFnName << ".function\n";
297 /// runOnMachineFunction - This uses the printMachineInstruction()
298 /// method to print assembly for each instruction.
300 bool XCoreAsmPrinter::runOnMachineFunction(MachineFunction &MF)
304 SetupMachineFunction(MF);
306 // Print out constants referenced by the function
307 EmitConstantPool(MF.getConstantPool());
309 // Print out jump tables referenced by the function
310 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
312 // Emit the function start directives
313 emitFunctionStart(MF);
315 // Emit pre-function debug information.
316 DW->BeginFunction(&MF);
318 // Print out code for the function.
319 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
322 // Print a label for the basic block.
323 if (I != MF.begin()) {
324 printBasicBlockLabel(I, true , true);
328 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
330 // Print the assembly for the instruction.
332 printMachineInstruction(II);
335 // Each Basic Block is separated by a newline
339 // Emit function end directives
342 // Emit post-function debug information.
343 DW->EndFunction(&MF);
345 // We didn't modify anything.
349 void XCoreAsmPrinter::printMemOperand(const MachineInstr *MI, int opNum)
351 printOperand(MI, opNum);
353 if (MI->getOperand(opNum+1).isImm()
354 && MI->getOperand(opNum+1).getImm() == 0)
358 printOperand(MI, opNum+1);
361 void XCoreAsmPrinter::printOperand(const MachineInstr *MI, int opNum) {
362 const MachineOperand &MO = MI->getOperand(opNum);
363 switch (MO.getType()) {
364 case MachineOperand::MO_Register:
365 if (TargetRegisterInfo::isPhysicalRegister(MO.getReg()))
366 O << TM.getRegisterInfo()->get(MO.getReg()).AsmName;
368 assert(0 && "not implemented");
370 case MachineOperand::MO_Immediate:
373 case MachineOperand::MO_MachineBasicBlock:
374 printBasicBlockLabel(MO.getMBB());
376 case MachineOperand::MO_GlobalAddress:
378 const GlobalValue *GV = MO.getGlobal();
379 O << Mang->getValueName(GV);
380 if (GV->hasExternalWeakLinkage())
381 ExtWeakSymbols.insert(GV);
384 case MachineOperand::MO_ExternalSymbol:
385 O << MO.getSymbolName();
387 case MachineOperand::MO_ConstantPoolIndex:
388 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber()
389 << '_' << MO.getIndex();
391 case MachineOperand::MO_JumpTableIndex:
392 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
393 << '_' << MO.getIndex();
396 assert(0 && "not implemented");
400 /// PrintAsmOperand - Print out an operand for an inline asm expression.
402 bool XCoreAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
404 const char *ExtraCode) {
405 printOperand(MI, OpNo);
409 void XCoreAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
412 // Check for mov mnemonic
413 unsigned src, dst, srcSR, dstSR;
414 if (TM.getInstrInfo()->isMoveInstr(*MI, src, dst, srcSR, dstSR)) {
416 O << TM.getRegisterInfo()->get(dst).AsmName;
418 O << TM.getRegisterInfo()->get(src).AsmName;
422 if (printInstruction(MI)) {
425 assert(0 && "Unhandled instruction in asm writer!");
428 bool XCoreAsmPrinter::doInitialization(Module &M) {
429 bool Result = AsmPrinter::doInitialization(M);
431 if (!FileDirective.empty()) {
432 emitFileDirective(FileDirective);
435 // Print out type strings for external functions here
436 for (Module::const_iterator I = M.begin(), E = M.end();
438 if (I->isDeclaration() && !I->isIntrinsic()) {
439 switch (I->getLinkage()) {
441 assert(0 && "Unexpected linkage");
442 case Function::ExternalWeakLinkage:
443 ExtWeakSymbols.insert(I);
445 case Function::ExternalLinkage:
451 // Emit initial debug information.
452 DW = getAnalysisIfAvailable<DwarfWriter>();
453 assert(DW && "Dwarf Writer is not available");
454 DW->BeginModule(&M, getAnalysisIfAvailable<MachineModuleInfo>(),
459 bool XCoreAsmPrinter::doFinalization(Module &M) {
461 // Print out module-level global variables.
462 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
467 // Emit final debug information.
470 return AsmPrinter::doFinalization(M);