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<unsigned> MaxThreads("xcore-max-threads", cl::Optional,
45 cl::desc("Maximum number of threads (for emulation thread-local storage)"),
47 cl::value_desc("number"),
51 class VISIBILITY_HIDDEN XCoreAsmPrinter : public AsmPrinter {
53 const XCoreSubtarget &Subtarget;
55 explicit XCoreAsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
56 const TargetAsmInfo *T, bool V)
57 : AsmPrinter(O, TM, T, V), DW(0),
58 Subtarget(TM.getSubtarget<XCoreSubtarget>()) {}
60 virtual const char *getPassName() const {
61 return "XCore Assembly Printer";
64 void printMemOperand(const MachineInstr *MI, int opNum);
65 void printOperand(const MachineInstr *MI, int opNum);
66 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
67 unsigned AsmVariant, const char *ExtraCode);
69 void emitGlobalDirective(const std::string &name);
70 void emitExternDirective(const std::string &name);
72 void emitArrayBound(const std::string &name, const GlobalVariable *GV);
73 void emitGlobal(const GlobalVariable *GV);
75 void emitFunctionStart(MachineFunction &MF);
76 void emitFunctionEnd(MachineFunction &MF);
78 bool printInstruction(const MachineInstr *MI); // autogenerated.
79 void printMachineInstruction(const MachineInstr *MI);
80 bool runOnMachineFunction(MachineFunction &F);
81 bool doInitialization(Module &M);
82 bool doFinalization(Module &M);
84 void getAnalysisUsage(AnalysisUsage &AU) const {
85 AsmPrinter::getAnalysisUsage(AU);
87 AU.addRequired<MachineModuleInfo>();
88 AU.addRequired<DwarfWriter>();
91 } // end of anonymous namespace
93 #include "XCoreGenAsmWriter.inc"
95 /// createXCoreCodePrinterPass - Returns a pass that prints the XCore
96 /// assembly code for a MachineFunction to the given output stream,
97 /// using the given target machine description. This should work
98 /// regardless of whether the function is in SSA form.
100 FunctionPass *llvm::createXCoreCodePrinterPass(formatted_raw_ostream &o,
103 return new XCoreAsmPrinter(o, tm, tm.getTargetAsmInfo(), verbose);
106 void XCoreAsmPrinter::
107 emitGlobalDirective(const std::string &name)
109 O << TAI->getGlobalDirective() << name;
113 void XCoreAsmPrinter::
114 emitExternDirective(const std::string &name)
116 O << "\t.extern\t" << name;
120 void XCoreAsmPrinter::
121 emitArrayBound(const std::string &name, const GlobalVariable *GV)
123 assert(((GV->hasExternalLinkage() ||
124 GV->hasWeakLinkage()) ||
125 GV->hasLinkOnceLinkage()) && "Unexpected linkage");
126 if (const ArrayType *ATy = dyn_cast<ArrayType>(
127 cast<PointerType>(GV->getType())->getElementType()))
129 O << TAI->getGlobalDirective() << name << ".globound" << "\n";
130 O << TAI->getSetDirective() << name << ".globound" << ","
131 << ATy->getNumElements() << "\n";
132 if (GV->hasWeakLinkage() || GV->hasLinkOnceLinkage()) {
133 // TODO Use COMDAT groups for LinkOnceLinkage
134 O << TAI->getWeakDefDirective() << name << ".globound" << "\n";
139 void XCoreAsmPrinter::
140 emitGlobal(const GlobalVariable *GV)
142 const TargetData *TD = TM.getTargetData();
144 if (GV->hasInitializer()) {
145 // Check to see if this is a special global used by LLVM, if so, emit it.
146 if (EmitSpecialLLVMGlobal(GV))
149 SwitchToSection(TAI->SectionForGlobal(GV));
151 std::string name = Mang->getMangledName(GV);
152 Constant *C = GV->getInitializer();
153 unsigned Align = (unsigned)TD->getPreferredTypeAlignmentShift(C->getType());
155 // Mark the start of the global
156 O << "\t.cc_top " << name << ".data," << name << "\n";
158 switch (GV->getLinkage()) {
159 case GlobalValue::AppendingLinkage:
160 llvm_report_error("AppendingLinkage is not supported by this target!");
161 case GlobalValue::LinkOnceAnyLinkage:
162 case GlobalValue::LinkOnceODRLinkage:
163 case GlobalValue::WeakAnyLinkage:
164 case GlobalValue::WeakODRLinkage:
165 case GlobalValue::ExternalLinkage:
166 emitArrayBound(name, GV);
167 emitGlobalDirective(name);
168 // TODO Use COMDAT groups for LinkOnceLinkage
169 if (GV->hasWeakLinkage() || GV->hasLinkOnceLinkage()) {
170 O << TAI->getWeakDefDirective() << name << "\n";
173 case GlobalValue::InternalLinkage:
174 case GlobalValue::PrivateLinkage:
176 case GlobalValue::GhostLinkage:
177 llvm_unreachable("Should not have any unmaterialized functions!");
178 case GlobalValue::DLLImportLinkage:
179 llvm_unreachable("DLLImport linkage is not supported by this target!");
180 case GlobalValue::DLLExportLinkage:
181 llvm_unreachable("DLLExport linkage is not supported by this target!");
183 llvm_unreachable("Unknown linkage type!");
186 EmitAlignment(Align, GV, 2);
188 unsigned Size = TD->getTypeAllocSize(C->getType());
189 if (GV->isThreadLocal()) {
192 if (TAI->hasDotTypeDotSizeDirective()) {
193 O << "\t.type " << name << ",@object\n";
194 O << "\t.size " << name << "," << Size << "\n";
198 EmitGlobalConstant(C);
199 if (GV->isThreadLocal()) {
200 for (unsigned i = 1; i < MaxThreads; ++i) {
201 EmitGlobalConstant(C);
205 // The ABI requires that unsigned scalar types smaller than 32 bits
206 // are are padded to 32 bits.
210 // Mark the end of the global
211 O << "\t.cc_bottom " << name << ".data\n";
215 /// Emit the directives on the start of functions
216 void XCoreAsmPrinter::
217 emitFunctionStart(MachineFunction &MF)
219 // Print out the label for the function.
220 const Function *F = MF.getFunction();
222 SwitchToSection(TAI->SectionForGlobal(F));
224 // Mark the start of the function
225 O << "\t.cc_top " << CurrentFnName << ".function," << CurrentFnName << "\n";
227 switch (F->getLinkage()) {
228 default: llvm_unreachable("Unknown linkage type!");
229 case Function::InternalLinkage: // Symbols default to internal.
230 case Function::PrivateLinkage:
232 case Function::ExternalLinkage:
233 emitGlobalDirective(CurrentFnName);
235 case Function::LinkOnceAnyLinkage:
236 case Function::LinkOnceODRLinkage:
237 case Function::WeakAnyLinkage:
238 case Function::WeakODRLinkage:
239 // TODO Use COMDAT groups for LinkOnceLinkage
240 O << TAI->getGlobalDirective() << CurrentFnName << "\n";
241 O << TAI->getWeakDefDirective() << CurrentFnName << "\n";
244 // (1 << 1) byte aligned
245 EmitAlignment(MF.getAlignment(), F, 1);
246 if (TAI->hasDotTypeDotSizeDirective()) {
247 O << "\t.type " << CurrentFnName << ",@function\n";
249 O << CurrentFnName << ":\n";
252 /// Emit the directives on the end of functions
253 void XCoreAsmPrinter::
254 emitFunctionEnd(MachineFunction &MF)
256 // Mark the end of the function
257 O << "\t.cc_bottom " << CurrentFnName << ".function\n";
260 /// runOnMachineFunction - This uses the printMachineInstruction()
261 /// method to print assembly for each instruction.
263 bool XCoreAsmPrinter::runOnMachineFunction(MachineFunction &MF)
267 SetupMachineFunction(MF);
269 // Print out constants referenced by the function
270 EmitConstantPool(MF.getConstantPool());
272 // Print out jump tables referenced by the function
273 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
275 // Emit the function start directives
276 emitFunctionStart(MF);
278 // Emit pre-function debug information.
279 DW->BeginFunction(&MF);
281 // Print out code for the function.
282 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
285 // Print a label for the basic block.
286 if (I != MF.begin()) {
287 printBasicBlockLabel(I, true , true);
291 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
293 // Print the assembly for the instruction.
295 printMachineInstruction(II);
298 // Each Basic Block is separated by a newline
302 // Emit function end directives
305 // Emit post-function debug information.
306 DW->EndFunction(&MF);
308 // We didn't modify anything.
312 void XCoreAsmPrinter::printMemOperand(const MachineInstr *MI, int opNum)
314 printOperand(MI, opNum);
316 if (MI->getOperand(opNum+1).isImm()
317 && MI->getOperand(opNum+1).getImm() == 0)
321 printOperand(MI, opNum+1);
324 void XCoreAsmPrinter::printOperand(const MachineInstr *MI, int opNum) {
325 const MachineOperand &MO = MI->getOperand(opNum);
326 switch (MO.getType()) {
327 case MachineOperand::MO_Register:
328 if (TargetRegisterInfo::isPhysicalRegister(MO.getReg()))
329 O << TM.getRegisterInfo()->get(MO.getReg()).AsmName;
331 llvm_unreachable("not implemented");
333 case MachineOperand::MO_Immediate:
336 case MachineOperand::MO_MachineBasicBlock:
337 printBasicBlockLabel(MO.getMBB());
339 case MachineOperand::MO_GlobalAddress:
340 O << Mang->getMangledName(MO.getGlobal());
342 case MachineOperand::MO_ExternalSymbol:
343 O << MO.getSymbolName();
345 case MachineOperand::MO_ConstantPoolIndex:
346 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber()
347 << '_' << MO.getIndex();
349 case MachineOperand::MO_JumpTableIndex:
350 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
351 << '_' << MO.getIndex();
354 llvm_unreachable("not implemented");
358 /// PrintAsmOperand - Print out an operand for an inline asm expression.
360 bool XCoreAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
362 const char *ExtraCode) {
363 printOperand(MI, OpNo);
367 void XCoreAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
370 // Check for mov mnemonic
371 unsigned src, dst, srcSR, dstSR;
372 if (TM.getInstrInfo()->isMoveInstr(*MI, src, dst, srcSR, dstSR)) {
374 O << TM.getRegisterInfo()->get(dst).AsmName;
376 O << TM.getRegisterInfo()->get(src).AsmName;
380 if (printInstruction(MI)) {
383 llvm_unreachable("Unhandled instruction in asm writer!");
386 bool XCoreAsmPrinter::doInitialization(Module &M) {
387 bool Result = AsmPrinter::doInitialization(M);
388 DW = getAnalysisIfAvailable<DwarfWriter>();
393 bool XCoreAsmPrinter::doFinalization(Module &M) {
395 // Print out module-level global variables.
396 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
401 return AsmPrinter::doFinalization(M);