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 "XCoreMCAsmInfo.h"
20 #include "XCoreTargetMachine.h"
21 #include "llvm/Constants.h"
22 #include "llvm/DerivedTypes.h"
23 #include "llvm/Module.h"
24 #include "llvm/CodeGen/AsmPrinter.h"
25 #include "llvm/CodeGen/DwarfWriter.h"
26 #include "llvm/CodeGen/MachineModuleInfo.h"
27 #include "llvm/CodeGen/MachineFunctionPass.h"
28 #include "llvm/CodeGen/MachineConstantPool.h"
29 #include "llvm/CodeGen/MachineInstr.h"
30 #include "llvm/MC/MCStreamer.h"
31 #include "llvm/MC/MCSymbol.h"
32 #include "llvm/Target/TargetData.h"
33 #include "llvm/Target/TargetLoweringObjectFile.h"
34 #include "llvm/Target/TargetRegistry.h"
35 #include "llvm/ADT/Statistic.h"
36 #include "llvm/ADT/StringExtras.h"
37 #include "llvm/Support/CommandLine.h"
38 #include "llvm/Support/ErrorHandling.h"
39 #include "llvm/Support/FormattedStream.h"
40 #include "llvm/Support/Mangler.h"
41 #include "llvm/Support/MathExtras.h"
46 STATISTIC(EmittedInsts, "Number of machine instrs printed");
48 static cl::opt<unsigned> MaxThreads("xcore-max-threads", cl::Optional,
49 cl::desc("Maximum number of threads (for emulation thread-local storage)"),
51 cl::value_desc("number"),
55 class XCoreAsmPrinter : public AsmPrinter {
56 const XCoreSubtarget &Subtarget;
58 explicit XCoreAsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
59 const MCAsmInfo *T, bool V)
60 : AsmPrinter(O, TM, T, V),
61 Subtarget(TM.getSubtarget<XCoreSubtarget>()) {}
63 virtual const char *getPassName() const {
64 return "XCore Assembly Printer";
67 void printMemOperand(const MachineInstr *MI, int opNum);
68 void printOperand(const MachineInstr *MI, int opNum);
69 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
70 unsigned AsmVariant, const char *ExtraCode);
72 void emitGlobalDirective(const std::string &name);
73 void emitExternDirective(const std::string &name);
75 void emitArrayBound(const std::string &name, const GlobalVariable *GV);
76 virtual void PrintGlobalVariable(const GlobalVariable *GV);
78 void emitFunctionStart(MachineFunction &MF);
79 void emitFunctionEnd(MachineFunction &MF);
81 void printInstruction(const MachineInstr *MI); // autogenerated.
82 static const char *getRegisterName(unsigned RegNo);
84 void printMachineInstruction(const MachineInstr *MI);
85 bool runOnMachineFunction(MachineFunction &F);
87 void getAnalysisUsage(AnalysisUsage &AU) const {
88 AsmPrinter::getAnalysisUsage(AU);
90 AU.addRequired<MachineModuleInfo>();
91 AU.addRequired<DwarfWriter>();
94 } // end of anonymous namespace
96 #include "XCoreGenAsmWriter.inc"
98 void XCoreAsmPrinter::
99 emitGlobalDirective(const std::string &name)
101 O << MAI->getGlobalDirective() << name;
105 void XCoreAsmPrinter::
106 emitExternDirective(const std::string &name)
108 O << "\t.extern\t" << name;
112 void XCoreAsmPrinter::
113 emitArrayBound(const std::string &name, const GlobalVariable *GV)
115 assert(((GV->hasExternalLinkage() ||
116 GV->hasWeakLinkage()) ||
117 GV->hasLinkOnceLinkage()) && "Unexpected linkage");
118 if (const ArrayType *ATy = dyn_cast<ArrayType>(
119 cast<PointerType>(GV->getType())->getElementType()))
121 O << MAI->getGlobalDirective() << name << ".globound" << "\n";
122 O << MAI->getSetDirective() << name << ".globound" << ","
123 << ATy->getNumElements() << "\n";
124 if (GV->hasWeakLinkage() || GV->hasLinkOnceLinkage()) {
125 // TODO Use COMDAT groups for LinkOnceLinkage
126 O << MAI->getWeakDefDirective() << name << ".globound" << "\n";
131 void XCoreAsmPrinter::PrintGlobalVariable(const GlobalVariable *GV) {
132 // Check to see if this is a special global used by LLVM, if so, emit it.
133 if (!GV->hasInitializer() ||
134 EmitSpecialLLVMGlobal(GV))
137 const TargetData *TD = TM.getTargetData();
139 OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(GV, Mang,TM));
141 std::string name = Mang->getMangledName(GV);
142 Constant *C = GV->getInitializer();
143 unsigned Align = (unsigned)TD->getPreferredTypeAlignmentShift(C->getType());
145 // Mark the start of the global
146 O << "\t.cc_top " << name << ".data," << name << "\n";
148 switch (GV->getLinkage()) {
149 case GlobalValue::AppendingLinkage:
150 llvm_report_error("AppendingLinkage is not supported by this target!");
151 case GlobalValue::LinkOnceAnyLinkage:
152 case GlobalValue::LinkOnceODRLinkage:
153 case GlobalValue::WeakAnyLinkage:
154 case GlobalValue::WeakODRLinkage:
155 case GlobalValue::ExternalLinkage:
156 emitArrayBound(name, GV);
157 emitGlobalDirective(name);
158 // TODO Use COMDAT groups for LinkOnceLinkage
159 if (GV->hasWeakLinkage() || GV->hasLinkOnceLinkage()) {
160 O << MAI->getWeakDefDirective() << name << "\n";
163 case GlobalValue::InternalLinkage:
164 case GlobalValue::PrivateLinkage:
165 case GlobalValue::LinkerPrivateLinkage:
167 case GlobalValue::GhostLinkage:
168 llvm_unreachable("Should not have any unmaterialized functions!");
169 case GlobalValue::DLLImportLinkage:
170 llvm_unreachable("DLLImport linkage is not supported by this target!");
171 case GlobalValue::DLLExportLinkage:
172 llvm_unreachable("DLLExport linkage is not supported by this target!");
174 llvm_unreachable("Unknown linkage type!");
177 EmitAlignment(Align, GV, 2);
179 unsigned Size = TD->getTypeAllocSize(C->getType());
180 if (GV->isThreadLocal()) {
183 if (MAI->hasDotTypeDotSizeDirective()) {
184 O << "\t.type " << name << ",@object\n";
185 O << "\t.size " << name << "," << Size << "\n";
189 EmitGlobalConstant(C);
190 if (GV->isThreadLocal()) {
191 for (unsigned i = 1; i < MaxThreads; ++i) {
192 EmitGlobalConstant(C);
196 // The ABI requires that unsigned scalar types smaller than 32 bits
197 // are are padded to 32 bits.
201 // Mark the end of the global
202 O << "\t.cc_bottom " << name << ".data\n";
205 /// Emit the directives on the start of functions
206 void XCoreAsmPrinter::emitFunctionStart(MachineFunction &MF) {
207 // Print out the label for the function.
208 const Function *F = MF.getFunction();
210 OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(F, Mang, TM));
212 // Mark the start of the function
213 O << "\t.cc_top " << CurrentFnName << ".function," << CurrentFnName << "\n";
215 switch (F->getLinkage()) {
216 default: llvm_unreachable("Unknown linkage type!");
217 case Function::InternalLinkage: // Symbols default to internal.
218 case Function::PrivateLinkage:
219 case Function::LinkerPrivateLinkage:
221 case Function::ExternalLinkage:
222 emitGlobalDirective(CurrentFnName);
224 case Function::LinkOnceAnyLinkage:
225 case Function::LinkOnceODRLinkage:
226 case Function::WeakAnyLinkage:
227 case Function::WeakODRLinkage:
228 // TODO Use COMDAT groups for LinkOnceLinkage
229 O << MAI->getGlobalDirective() << CurrentFnName << "\n";
230 O << MAI->getWeakDefDirective() << CurrentFnName << "\n";
233 // (1 << 1) byte aligned
234 EmitAlignment(MF.getAlignment(), F, 1);
235 if (MAI->hasDotTypeDotSizeDirective()) {
236 O << "\t.type " << CurrentFnName << ",@function\n";
238 O << CurrentFnName << ":\n";
241 /// Emit the directives on the end of functions
242 void XCoreAsmPrinter::
243 emitFunctionEnd(MachineFunction &MF)
245 // Mark the end of the function
246 O << "\t.cc_bottom " << CurrentFnName << ".function\n";
249 /// runOnMachineFunction - This uses the printMachineInstruction()
250 /// method to print assembly for each instruction.
252 bool XCoreAsmPrinter::runOnMachineFunction(MachineFunction &MF)
256 SetupMachineFunction(MF);
258 // Print out constants referenced by the function
259 EmitConstantPool(MF.getConstantPool());
261 // Print out jump tables referenced by the function
262 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
264 // Emit the function start directives
265 emitFunctionStart(MF);
267 // Emit pre-function debug information.
268 DW->BeginFunction(&MF);
270 // Print out code for the function.
271 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
274 // Print a label for the basic block.
275 if (I != MF.begin()) {
276 EmitBasicBlockStart(I);
279 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
281 // Print the assembly for the instruction.
282 printMachineInstruction(II);
285 // Each Basic Block is separated by a newline
289 // Emit function end directives
292 // Emit post-function debug information.
293 DW->EndFunction(&MF);
295 // We didn't modify anything.
299 void XCoreAsmPrinter::printMemOperand(const MachineInstr *MI, int opNum)
301 printOperand(MI, opNum);
303 if (MI->getOperand(opNum+1).isImm()
304 && MI->getOperand(opNum+1).getImm() == 0)
308 printOperand(MI, opNum+1);
311 void XCoreAsmPrinter::printOperand(const MachineInstr *MI, int opNum) {
312 const MachineOperand &MO = MI->getOperand(opNum);
313 switch (MO.getType()) {
314 case MachineOperand::MO_Register:
315 O << getRegisterName(MO.getReg());
317 case MachineOperand::MO_Immediate:
320 case MachineOperand::MO_MachineBasicBlock:
321 GetMBBSymbol(MO.getMBB()->getNumber())->print(O, MAI);
323 case MachineOperand::MO_GlobalAddress:
324 O << Mang->getMangledName(MO.getGlobal());
326 case MachineOperand::MO_ExternalSymbol:
327 O << MO.getSymbolName();
329 case MachineOperand::MO_ConstantPoolIndex:
330 O << MAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber()
331 << '_' << MO.getIndex();
333 case MachineOperand::MO_JumpTableIndex:
334 O << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
335 << '_' << MO.getIndex();
338 llvm_unreachable("not implemented");
342 /// PrintAsmOperand - Print out an operand for an inline asm expression.
344 bool XCoreAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
346 const char *ExtraCode) {
347 printOperand(MI, OpNo);
351 void XCoreAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
354 processDebugLoc(MI, true);
356 // Check for mov mnemonic
357 unsigned src, dst, srcSR, dstSR;
358 if (TM.getInstrInfo()->isMoveInstr(*MI, src, dst, srcSR, dstSR)) {
359 O << "\tmov " << getRegisterName(dst) << ", ";
360 O << getRegisterName(src) << '\n';
363 printInstruction(MI);
364 if (VerboseAsm && !MI->getDebugLoc().isUnknown())
368 processDebugLoc(MI, false);
371 // Force static initialization.
372 extern "C" void LLVMInitializeXCoreAsmPrinter() {
373 RegisterAsmPrinter<XCoreAsmPrinter> X(TheXCoreTarget);