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/ErrorHandling.h"
36 #include "llvm/Support/FormattedStream.h"
37 #include "llvm/Support/MathExtras.h"
42 STATISTIC(EmittedInsts, "Number of machine instrs printed");
44 static cl::opt<std::string> FileDirective("xcore-file-directive", cl::Optional,
45 cl::desc("Output a file directive into the assembly file"),
47 cl::value_desc("filename"),
50 static cl::opt<unsigned> MaxThreads("xcore-max-threads", cl::Optional,
51 cl::desc("Maximum number of threads (for emulation thread-local storage)"),
53 cl::value_desc("number"),
57 class VISIBILITY_HIDDEN XCoreAsmPrinter : public AsmPrinter {
59 const XCoreSubtarget &Subtarget;
61 explicit XCoreAsmPrinter(formatted_raw_ostream &O, XCoreTargetMachine &TM,
62 const TargetAsmInfo *T, bool V)
63 : AsmPrinter(O, TM, T, V), DW(0),
64 Subtarget(*TM.getSubtargetImpl()) {}
66 virtual const char *getPassName() const {
67 return "XCore Assembly Printer";
70 void printMemOperand(const MachineInstr *MI, int opNum);
71 void printOperand(const MachineInstr *MI, int opNum);
72 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
73 unsigned AsmVariant, const char *ExtraCode);
75 void emitFileDirective(const std::string &filename);
76 void emitGlobalDirective(const std::string &name);
77 void emitExternDirective(const std::string &name);
79 void emitArrayBound(const std::string &name, const GlobalVariable *GV);
80 void emitGlobal(const GlobalVariable *GV);
82 void emitFunctionStart(MachineFunction &MF);
83 void emitFunctionEnd(MachineFunction &MF);
85 bool printInstruction(const MachineInstr *MI); // autogenerated.
86 void printMachineInstruction(const MachineInstr *MI);
87 bool runOnMachineFunction(MachineFunction &F);
88 bool doInitialization(Module &M);
89 bool doFinalization(Module &M);
91 void getAnalysisUsage(AnalysisUsage &AU) const {
92 AsmPrinter::getAnalysisUsage(AU);
94 AU.addRequired<MachineModuleInfo>();
95 AU.addRequired<DwarfWriter>();
98 } // end of anonymous namespace
100 #include "XCoreGenAsmWriter.inc"
102 /// createXCoreCodePrinterPass - Returns a pass that prints the XCore
103 /// assembly code for a MachineFunction to the given output stream,
104 /// using the given target machine description. This should work
105 /// regardless of whether the function is in SSA form.
107 FunctionPass *llvm::createXCoreCodePrinterPass(formatted_raw_ostream &o,
108 XCoreTargetMachine &tm,
110 return new XCoreAsmPrinter(o, tm, tm.getTargetAsmInfo(), 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,
116 formatted_raw_ostream &Out) {
117 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
118 unsigned char C = Str[i];
119 if (isprint(C) && C != '"' && C != '\\') {
123 << (char) ((C/16 < 10) ? ( C/16 +'0') : ( C/16 -10+'A'))
124 << (char)(((C&15) < 10) ? ((C&15)+'0') : ((C&15)-10+'A'));
129 void XCoreAsmPrinter::
130 emitFileDirective(const std::string &name)
133 PrintEscapedString(name, O);
137 void XCoreAsmPrinter::
138 emitGlobalDirective(const std::string &name)
140 O << TAI->getGlobalDirective() << name;
144 void XCoreAsmPrinter::
145 emitExternDirective(const std::string &name)
147 O << "\t.extern\t" << name;
151 void XCoreAsmPrinter::
152 emitArrayBound(const std::string &name, const GlobalVariable *GV)
154 assert(((GV->hasExternalLinkage() ||
155 GV->hasWeakLinkage()) ||
156 GV->hasLinkOnceLinkage()) && "Unexpected linkage");
157 if (const ArrayType *ATy = dyn_cast<ArrayType>(
158 cast<PointerType>(GV->getType())->getElementType()))
160 O << TAI->getGlobalDirective() << name << ".globound" << "\n";
161 O << TAI->getSetDirective() << name << ".globound" << ","
162 << ATy->getNumElements() << "\n";
163 if (GV->hasWeakLinkage() || GV->hasLinkOnceLinkage()) {
164 // TODO Use COMDAT groups for LinkOnceLinkage
165 O << TAI->getWeakDefDirective() << name << ".globound" << "\n";
170 void XCoreAsmPrinter::
171 emitGlobal(const GlobalVariable *GV)
173 const TargetData *TD = TM.getTargetData();
175 if (GV->hasInitializer()) {
176 // Check to see if this is a special global used by LLVM, if so, emit it.
177 if (EmitSpecialLLVMGlobal(GV))
180 SwitchToSection(TAI->SectionForGlobal(GV));
182 std::string name = Mang->getMangledName(GV);
183 Constant *C = GV->getInitializer();
184 unsigned Align = (unsigned)TD->getPreferredTypeAlignmentShift(C->getType());
186 // Mark the start of the global
187 O << "\t.cc_top " << name << ".data," << name << "\n";
189 switch (GV->getLinkage()) {
190 case GlobalValue::AppendingLinkage:
191 llvm_report_error("AppendingLinkage is not supported by this target!");
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 llvm_unreachable("Should not have any unmaterialized functions!");
209 case GlobalValue::DLLImportLinkage:
210 llvm_unreachable("DLLImport linkage is not supported by this target!");
211 case GlobalValue::DLLExportLinkage:
212 llvm_unreachable("DLLExport linkage is not supported by this target!");
214 llvm_unreachable("Unknown linkage type!");
217 EmitAlignment(Align, GV, 2);
219 unsigned Size = TD->getTypeAllocSize(C->getType());
220 if (GV->isThreadLocal()) {
223 if (TAI->hasDotTypeDotSizeDirective()) {
224 O << "\t.type " << name << ",@object\n";
225 O << "\t.size " << name << "," << Size << "\n";
229 EmitGlobalConstant(C);
230 if (GV->isThreadLocal()) {
231 for (unsigned i = 1; i < MaxThreads; ++i) {
232 EmitGlobalConstant(C);
236 // The ABI requires that unsigned scalar types smaller than 32 bits
237 // are are padded to 32 bits.
241 // Mark the end of the global
242 O << "\t.cc_bottom " << name << ".data\n";
246 /// Emit the directives on the start of functions
247 void XCoreAsmPrinter::
248 emitFunctionStart(MachineFunction &MF)
250 // Print out the label for the function.
251 const Function *F = MF.getFunction();
253 SwitchToSection(TAI->SectionForGlobal(F));
255 // Mark the start of the function
256 O << "\t.cc_top " << CurrentFnName << ".function," << CurrentFnName << "\n";
258 switch (F->getLinkage()) {
259 default: llvm_unreachable("Unknown linkage type!");
260 case Function::InternalLinkage: // Symbols default to internal.
261 case Function::PrivateLinkage:
263 case Function::ExternalLinkage:
264 emitGlobalDirective(CurrentFnName);
266 case Function::LinkOnceAnyLinkage:
267 case Function::LinkOnceODRLinkage:
268 case Function::WeakAnyLinkage:
269 case Function::WeakODRLinkage:
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(MF.getAlignment(), 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)
298 SetupMachineFunction(MF);
300 // Print out constants referenced by the function
301 EmitConstantPool(MF.getConstantPool());
303 // Print out jump tables referenced by the function
304 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
306 // Emit the function start directives
307 emitFunctionStart(MF);
309 // Emit pre-function debug information.
310 DW->BeginFunction(&MF);
312 // Print out code for the function.
313 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
316 // Print a label for the basic block.
317 if (I != MF.begin()) {
318 printBasicBlockLabel(I, true , true);
322 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
324 // Print the assembly for the instruction.
326 printMachineInstruction(II);
329 // Each Basic Block is separated by a newline
333 // Emit function end directives
336 // Emit post-function debug information.
337 DW->EndFunction(&MF);
339 // We didn't modify anything.
343 void XCoreAsmPrinter::printMemOperand(const MachineInstr *MI, int opNum)
345 printOperand(MI, opNum);
347 if (MI->getOperand(opNum+1).isImm()
348 && MI->getOperand(opNum+1).getImm() == 0)
352 printOperand(MI, opNum+1);
355 void XCoreAsmPrinter::printOperand(const MachineInstr *MI, int opNum) {
356 const MachineOperand &MO = MI->getOperand(opNum);
357 switch (MO.getType()) {
358 case MachineOperand::MO_Register:
359 if (TargetRegisterInfo::isPhysicalRegister(MO.getReg()))
360 O << TM.getRegisterInfo()->get(MO.getReg()).AsmName;
362 llvm_unreachable("not implemented");
364 case MachineOperand::MO_Immediate:
367 case MachineOperand::MO_MachineBasicBlock:
368 printBasicBlockLabel(MO.getMBB());
370 case MachineOperand::MO_GlobalAddress:
371 O << Mang->getMangledName(MO.getGlobal());
373 case MachineOperand::MO_ExternalSymbol:
374 O << MO.getSymbolName();
376 case MachineOperand::MO_ConstantPoolIndex:
377 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber()
378 << '_' << MO.getIndex();
380 case MachineOperand::MO_JumpTableIndex:
381 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
382 << '_' << MO.getIndex();
385 llvm_unreachable("not implemented");
389 /// PrintAsmOperand - Print out an operand for an inline asm expression.
391 bool XCoreAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
393 const char *ExtraCode) {
394 printOperand(MI, OpNo);
398 void XCoreAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
401 // Check for mov mnemonic
402 unsigned src, dst, srcSR, dstSR;
403 if (TM.getInstrInfo()->isMoveInstr(*MI, src, dst, srcSR, dstSR)) {
405 O << TM.getRegisterInfo()->get(dst).AsmName;
407 O << TM.getRegisterInfo()->get(src).AsmName;
411 if (printInstruction(MI)) {
414 llvm_unreachable("Unhandled instruction in asm writer!");
417 bool XCoreAsmPrinter::doInitialization(Module &M) {
418 bool Result = AsmPrinter::doInitialization(M);
419 DW = getAnalysisIfAvailable<DwarfWriter>();
421 if (!FileDirective.empty())
422 emitFileDirective(FileDirective);
427 bool XCoreAsmPrinter::doFinalization(Module &M) {
429 // Print out module-level global variables.
430 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
435 return AsmPrinter::doFinalization(M);