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/MathExtras.h"
45 STATISTIC(EmittedInsts, "Number of machine instrs printed");
47 static cl::opt<unsigned> MaxThreads("xcore-max-threads", cl::Optional,
48 cl::desc("Maximum number of threads (for emulation thread-local storage)"),
50 cl::value_desc("number"),
54 class XCoreAsmPrinter : public AsmPrinter {
55 const XCoreSubtarget &Subtarget;
57 explicit XCoreAsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
58 const MCAsmInfo *T, bool V)
59 : AsmPrinter(O, TM, T, V),
60 Subtarget(TM.getSubtarget<XCoreSubtarget>()) {}
62 virtual const char *getPassName() const {
63 return "XCore Assembly Printer";
66 void printMemOperand(const MachineInstr *MI, int opNum);
67 void printOperand(const MachineInstr *MI, int opNum);
68 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
69 unsigned AsmVariant, const char *ExtraCode);
71 void emitGlobalDirective(const MCSymbol *Sym);
73 void emitArrayBound(const MCSymbol *Sym, const GlobalVariable *GV);
74 virtual void EmitGlobalVariable(const GlobalVariable *GV);
76 void emitFunctionStart(MachineFunction &MF);
77 void emitFunctionEnd(MachineFunction &MF);
79 void printInstruction(const MachineInstr *MI); // autogenerated.
80 static const char *getRegisterName(unsigned RegNo);
82 void printMachineInstruction(const MachineInstr *MI);
83 bool runOnMachineFunction(MachineFunction &F);
85 void getAnalysisUsage(AnalysisUsage &AU) const {
86 AsmPrinter::getAnalysisUsage(AU);
88 AU.addRequired<MachineModuleInfo>();
89 AU.addRequired<DwarfWriter>();
92 } // end of anonymous namespace
94 #include "XCoreGenAsmWriter.inc"
96 void XCoreAsmPrinter::emitGlobalDirective(const MCSymbol *Sym) {
97 O << MAI->getGlobalDirective() << *Sym << "\n";
100 void XCoreAsmPrinter::emitArrayBound(const MCSymbol *Sym,
101 const GlobalVariable *GV) {
102 assert(((GV->hasExternalLinkage() ||
103 GV->hasWeakLinkage()) ||
104 GV->hasLinkOnceLinkage()) && "Unexpected linkage");
105 if (const ArrayType *ATy = dyn_cast<ArrayType>(
106 cast<PointerType>(GV->getType())->getElementType())) {
107 O << MAI->getGlobalDirective() << *Sym;
108 O << ".globound" << "\n";
109 O << MAI->getSetDirective() << *Sym;
110 O << ".globound" << "," << ATy->getNumElements() << "\n";
111 if (GV->hasWeakLinkage() || GV->hasLinkOnceLinkage()) {
112 // TODO Use COMDAT groups for LinkOnceLinkage
113 O << MAI->getWeakDefDirective() << *Sym << ".globound" << "\n";
118 void XCoreAsmPrinter::EmitGlobalVariable(const GlobalVariable *GV) {
119 // Check to see if this is a special global used by LLVM, if so, emit it.
120 if (!GV->hasInitializer() ||
121 EmitSpecialLLVMGlobal(GV))
124 const TargetData *TD = TM.getTargetData();
125 OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(GV, Mang,TM));
128 MCSymbol *GVSym = GetGlobalValueSymbol(GV);
129 Constant *C = GV->getInitializer();
130 unsigned Align = (unsigned)TD->getPreferredTypeAlignmentShift(C->getType());
132 // Mark the start of the global
133 O << "\t.cc_top " << *GVSym << ".data," << *GVSym << "\n";
135 switch (GV->getLinkage()) {
136 case GlobalValue::AppendingLinkage:
137 llvm_report_error("AppendingLinkage is not supported by this target!");
138 case GlobalValue::LinkOnceAnyLinkage:
139 case GlobalValue::LinkOnceODRLinkage:
140 case GlobalValue::WeakAnyLinkage:
141 case GlobalValue::WeakODRLinkage:
142 case GlobalValue::ExternalLinkage:
143 emitArrayBound(GVSym, GV);
144 emitGlobalDirective(GVSym);
145 // TODO Use COMDAT groups for LinkOnceLinkage
146 if (GV->hasWeakLinkage() || GV->hasLinkOnceLinkage())
147 O << MAI->getWeakDefDirective() << *GVSym << "\n";
149 case GlobalValue::InternalLinkage:
150 case GlobalValue::PrivateLinkage:
151 case GlobalValue::LinkerPrivateLinkage:
153 case GlobalValue::GhostLinkage:
154 llvm_unreachable("Should not have any unmaterialized functions!");
155 case GlobalValue::DLLImportLinkage:
156 llvm_unreachable("DLLImport linkage is not supported by this target!");
157 case GlobalValue::DLLExportLinkage:
158 llvm_unreachable("DLLExport linkage is not supported by this target!");
160 llvm_unreachable("Unknown linkage type!");
163 EmitAlignment(Align, GV, 2);
165 unsigned Size = TD->getTypeAllocSize(C->getType());
166 if (GV->isThreadLocal()) {
169 if (MAI->hasDotTypeDotSizeDirective()) {
170 O << "\t.type " << *GVSym << ",@object\n";
171 O << "\t.size " << *GVSym << "," << Size << "\n";
173 O << *GVSym << ":\n";
175 EmitGlobalConstant(C);
176 if (GV->isThreadLocal()) {
177 for (unsigned i = 1; i < MaxThreads; ++i) {
178 EmitGlobalConstant(C);
182 // The ABI requires that unsigned scalar types smaller than 32 bits
183 // are are padded to 32 bits.
187 // Mark the end of the global
188 O << "\t.cc_bottom " << *GVSym << ".data\n";
191 /// Emit the directives on the start of functions
192 void XCoreAsmPrinter::emitFunctionStart(MachineFunction &MF) {
193 // Print out the label for the function.
194 const Function *F = MF.getFunction();
196 OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(F, Mang, TM));
198 // Mark the start of the function
199 O << "\t.cc_top " << *CurrentFnSym << ".function," << *CurrentFnSym << "\n";
201 switch (F->getLinkage()) {
202 default: llvm_unreachable("Unknown linkage type!");
203 case Function::InternalLinkage: // Symbols default to internal.
204 case Function::PrivateLinkage:
205 case Function::LinkerPrivateLinkage:
207 case Function::ExternalLinkage:
208 emitGlobalDirective(CurrentFnSym);
210 case Function::LinkOnceAnyLinkage:
211 case Function::LinkOnceODRLinkage:
212 case Function::WeakAnyLinkage:
213 case Function::WeakODRLinkage:
214 // TODO Use COMDAT groups for LinkOnceLinkage
215 O << MAI->getGlobalDirective() << *CurrentFnSym << "\n";
216 O << MAI->getWeakDefDirective() << *CurrentFnSym << "\n";
219 // (1 << 1) byte aligned
220 EmitAlignment(MF.getAlignment(), F, 1);
221 if (MAI->hasDotTypeDotSizeDirective())
222 O << "\t.type " << *CurrentFnSym << ",@function\n";
224 O << *CurrentFnSym << ":\n";
227 /// Emit the directives on the end of functions
228 void XCoreAsmPrinter::emitFunctionEnd(MachineFunction &MF) {
229 // Mark the end of the function
230 O << "\t.cc_bottom " << *CurrentFnSym << ".function\n";
233 /// runOnMachineFunction - This uses the printMachineInstruction()
234 /// method to print assembly for each instruction.
236 bool XCoreAsmPrinter::runOnMachineFunction(MachineFunction &MF)
240 SetupMachineFunction(MF);
242 // Print out constants referenced by the function
243 EmitConstantPool(MF.getConstantPool());
245 // Print out jump tables referenced by the function
246 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
248 // Emit the function start directives
249 emitFunctionStart(MF);
251 // Emit pre-function debug information.
252 DW->BeginFunction(&MF);
254 // Print out code for the function.
255 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
258 // Print a label for the basic block.
259 if (I != MF.begin()) {
260 EmitBasicBlockStart(I);
263 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
265 // Print the assembly for the instruction.
266 printMachineInstruction(II);
269 // Each Basic Block is separated by a newline
273 // Emit function end directives
276 // Emit post-function debug information.
277 DW->EndFunction(&MF);
279 // We didn't modify anything.
283 void XCoreAsmPrinter::printMemOperand(const MachineInstr *MI, int opNum)
285 printOperand(MI, opNum);
287 if (MI->getOperand(opNum+1).isImm()
288 && MI->getOperand(opNum+1).getImm() == 0)
292 printOperand(MI, opNum+1);
295 void XCoreAsmPrinter::printOperand(const MachineInstr *MI, int opNum) {
296 const MachineOperand &MO = MI->getOperand(opNum);
297 switch (MO.getType()) {
298 case MachineOperand::MO_Register:
299 O << getRegisterName(MO.getReg());
301 case MachineOperand::MO_Immediate:
304 case MachineOperand::MO_MachineBasicBlock:
305 O << *GetMBBSymbol(MO.getMBB()->getNumber());
307 case MachineOperand::MO_GlobalAddress:
308 O << *GetGlobalValueSymbol(MO.getGlobal());
310 case MachineOperand::MO_ExternalSymbol:
311 O << MO.getSymbolName();
313 case MachineOperand::MO_ConstantPoolIndex:
314 O << MAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber()
315 << '_' << MO.getIndex();
317 case MachineOperand::MO_JumpTableIndex:
318 O << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
319 << '_' << MO.getIndex();
320 case MachineOperand::MO_BlockAddress:
321 O << *GetBlockAddressSymbol(MO.getBlockAddress());
324 llvm_unreachable("not implemented");
328 /// PrintAsmOperand - Print out an operand for an inline asm expression.
330 bool XCoreAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
332 const char *ExtraCode) {
333 printOperand(MI, OpNo);
337 void XCoreAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
340 processDebugLoc(MI, true);
342 // Check for mov mnemonic
343 unsigned src, dst, srcSR, dstSR;
344 if (TM.getInstrInfo()->isMoveInstr(*MI, src, dst, srcSR, dstSR)) {
345 O << "\tmov " << getRegisterName(dst) << ", ";
346 O << getRegisterName(src) << '\n';
349 printInstruction(MI);
354 processDebugLoc(MI, false);
357 // Force static initialization.
358 extern "C" void LLVMInitializeXCoreAsmPrinter() {
359 RegisterAsmPrinter<XCoreAsmPrinter> X(TheXCoreTarget);