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 virtual void PrintGlobalVariable(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);
83 void getAnalysisUsage(AnalysisUsage &AU) const {
84 AsmPrinter::getAnalysisUsage(AU);
86 AU.addRequired<MachineModuleInfo>();
87 AU.addRequired<DwarfWriter>();
90 } // end of anonymous namespace
92 #include "XCoreGenAsmWriter.inc"
94 /// createXCoreCodePrinterPass - Returns a pass that prints the XCore
95 /// assembly code for a MachineFunction to the given output stream,
96 /// using the given target machine description. This should work
97 /// regardless of whether the function is in SSA form.
99 FunctionPass *llvm::createXCoreCodePrinterPass(formatted_raw_ostream &o,
102 return new XCoreAsmPrinter(o, tm, tm.getTargetAsmInfo(), verbose);
105 void XCoreAsmPrinter::
106 emitGlobalDirective(const std::string &name)
108 O << TAI->getGlobalDirective() << name;
112 void XCoreAsmPrinter::
113 emitExternDirective(const std::string &name)
115 O << "\t.extern\t" << name;
119 void XCoreAsmPrinter::
120 emitArrayBound(const std::string &name, const GlobalVariable *GV)
122 assert(((GV->hasExternalLinkage() ||
123 GV->hasWeakLinkage()) ||
124 GV->hasLinkOnceLinkage()) && "Unexpected linkage");
125 if (const ArrayType *ATy = dyn_cast<ArrayType>(
126 cast<PointerType>(GV->getType())->getElementType()))
128 O << TAI->getGlobalDirective() << name << ".globound" << "\n";
129 O << TAI->getSetDirective() << name << ".globound" << ","
130 << ATy->getNumElements() << "\n";
131 if (GV->hasWeakLinkage() || GV->hasLinkOnceLinkage()) {
132 // TODO Use COMDAT groups for LinkOnceLinkage
133 O << TAI->getWeakDefDirective() << name << ".globound" << "\n";
138 void XCoreAsmPrinter::PrintGlobalVariable(const GlobalVariable *GV) {
139 // Check to see if this is a special global used by LLVM, if so, emit it.
140 if (!GV->hasInitializer() ||
141 EmitSpecialLLVMGlobal(GV))
144 const TargetData *TD = TM.getTargetData();
146 SwitchToSection(TAI->SectionForGlobal(GV));
148 std::string name = Mang->getMangledName(GV);
149 Constant *C = GV->getInitializer();
150 unsigned Align = (unsigned)TD->getPreferredTypeAlignmentShift(C->getType());
152 // Mark the start of the global
153 O << "\t.cc_top " << name << ".data," << name << "\n";
155 switch (GV->getLinkage()) {
156 case GlobalValue::AppendingLinkage:
157 llvm_report_error("AppendingLinkage is not supported by this target!");
158 case GlobalValue::LinkOnceAnyLinkage:
159 case GlobalValue::LinkOnceODRLinkage:
160 case GlobalValue::WeakAnyLinkage:
161 case GlobalValue::WeakODRLinkage:
162 case GlobalValue::ExternalLinkage:
163 emitArrayBound(name, GV);
164 emitGlobalDirective(name);
165 // TODO Use COMDAT groups for LinkOnceLinkage
166 if (GV->hasWeakLinkage() || GV->hasLinkOnceLinkage()) {
167 O << TAI->getWeakDefDirective() << name << "\n";
170 case GlobalValue::InternalLinkage:
171 case GlobalValue::PrivateLinkage:
172 case GlobalValue::LinkerPrivateLinkage:
174 case GlobalValue::GhostLinkage:
175 llvm_unreachable("Should not have any unmaterialized functions!");
176 case GlobalValue::DLLImportLinkage:
177 llvm_unreachable("DLLImport linkage is not supported by this target!");
178 case GlobalValue::DLLExportLinkage:
179 llvm_unreachable("DLLExport linkage is not supported by this target!");
181 llvm_unreachable("Unknown linkage type!");
184 EmitAlignment(Align, GV, 2);
186 unsigned Size = TD->getTypeAllocSize(C->getType());
187 if (GV->isThreadLocal()) {
190 if (TAI->hasDotTypeDotSizeDirective()) {
191 O << "\t.type " << name << ",@object\n";
192 O << "\t.size " << name << "," << Size << "\n";
196 EmitGlobalConstant(C);
197 if (GV->isThreadLocal()) {
198 for (unsigned i = 1; i < MaxThreads; ++i) {
199 EmitGlobalConstant(C);
203 // The ABI requires that unsigned scalar types smaller than 32 bits
204 // are are padded to 32 bits.
208 // Mark the end of the global
209 O << "\t.cc_bottom " << name << ".data\n";
212 /// Emit the directives on the start of functions
213 void XCoreAsmPrinter::emitFunctionStart(MachineFunction &MF) {
214 // Print out the label for the function.
215 const Function *F = MF.getFunction();
217 SwitchToSection(TAI->SectionForGlobal(F));
219 // Mark the start of the function
220 O << "\t.cc_top " << CurrentFnName << ".function," << CurrentFnName << "\n";
222 switch (F->getLinkage()) {
223 default: llvm_unreachable("Unknown linkage type!");
224 case Function::InternalLinkage: // Symbols default to internal.
225 case Function::PrivateLinkage:
226 case Function::LinkerPrivateLinkage:
228 case Function::ExternalLinkage:
229 emitGlobalDirective(CurrentFnName);
231 case Function::LinkOnceAnyLinkage:
232 case Function::LinkOnceODRLinkage:
233 case Function::WeakAnyLinkage:
234 case Function::WeakODRLinkage:
235 // TODO Use COMDAT groups for LinkOnceLinkage
236 O << TAI->getGlobalDirective() << CurrentFnName << "\n";
237 O << TAI->getWeakDefDirective() << CurrentFnName << "\n";
240 // (1 << 1) byte aligned
241 EmitAlignment(MF.getAlignment(), F, 1);
242 if (TAI->hasDotTypeDotSizeDirective()) {
243 O << "\t.type " << CurrentFnName << ",@function\n";
245 O << CurrentFnName << ":\n";
248 /// Emit the directives on the end of functions
249 void XCoreAsmPrinter::
250 emitFunctionEnd(MachineFunction &MF)
252 // Mark the end of the function
253 O << "\t.cc_bottom " << CurrentFnName << ".function\n";
256 /// runOnMachineFunction - This uses the printMachineInstruction()
257 /// method to print assembly for each instruction.
259 bool XCoreAsmPrinter::runOnMachineFunction(MachineFunction &MF)
263 SetupMachineFunction(MF);
265 // Print out constants referenced by the function
266 EmitConstantPool(MF.getConstantPool());
268 // Print out jump tables referenced by the function
269 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
271 // Emit the function start directives
272 emitFunctionStart(MF);
274 // Emit pre-function debug information.
275 DW->BeginFunction(&MF);
277 // Print out code for the function.
278 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
281 // Print a label for the basic block.
282 if (I != MF.begin()) {
283 printBasicBlockLabel(I, true , true);
287 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
289 // Print the assembly for the instruction.
291 printMachineInstruction(II);
294 // Each Basic Block is separated by a newline
298 // Emit function end directives
301 // Emit post-function debug information.
302 DW->EndFunction(&MF);
304 // We didn't modify anything.
308 void XCoreAsmPrinter::printMemOperand(const MachineInstr *MI, int opNum)
310 printOperand(MI, opNum);
312 if (MI->getOperand(opNum+1).isImm()
313 && MI->getOperand(opNum+1).getImm() == 0)
317 printOperand(MI, opNum+1);
320 void XCoreAsmPrinter::printOperand(const MachineInstr *MI, int opNum) {
321 const MachineOperand &MO = MI->getOperand(opNum);
322 switch (MO.getType()) {
323 case MachineOperand::MO_Register:
324 if (TargetRegisterInfo::isPhysicalRegister(MO.getReg()))
325 O << TM.getRegisterInfo()->get(MO.getReg()).AsmName;
327 llvm_unreachable("not implemented");
329 case MachineOperand::MO_Immediate:
332 case MachineOperand::MO_MachineBasicBlock:
333 printBasicBlockLabel(MO.getMBB());
335 case MachineOperand::MO_GlobalAddress:
336 O << Mang->getMangledName(MO.getGlobal());
338 case MachineOperand::MO_ExternalSymbol:
339 O << MO.getSymbolName();
341 case MachineOperand::MO_ConstantPoolIndex:
342 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber()
343 << '_' << MO.getIndex();
345 case MachineOperand::MO_JumpTableIndex:
346 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
347 << '_' << MO.getIndex();
350 llvm_unreachable("not implemented");
354 /// PrintAsmOperand - Print out an operand for an inline asm expression.
356 bool XCoreAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
358 const char *ExtraCode) {
359 printOperand(MI, OpNo);
363 void XCoreAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
366 // Check for mov mnemonic
367 unsigned src, dst, srcSR, dstSR;
368 if (TM.getInstrInfo()->isMoveInstr(*MI, src, dst, srcSR, dstSR)) {
370 O << TM.getRegisterInfo()->get(dst).AsmName;
372 O << TM.getRegisterInfo()->get(src).AsmName;
376 if (printInstruction(MI)) {
379 llvm_unreachable("Unhandled instruction in asm writer!");
382 bool XCoreAsmPrinter::doInitialization(Module &M) {
383 bool Result = AsmPrinter::doInitialization(M);
384 DW = getAnalysisIfAvailable<DwarfWriter>();