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::emitGlobal(const GlobalVariable *GV) {
140 // Check to see if this is a special global used by LLVM, if so, emit it.
141 if (!GV->hasInitializer() ||
142 EmitSpecialLLVMGlobal(GV))
145 const TargetData *TD = TM.getTargetData();
147 SwitchToSection(TAI->SectionForGlobal(GV));
149 std::string name = Mang->getMangledName(GV);
150 Constant *C = GV->getInitializer();
151 unsigned Align = (unsigned)TD->getPreferredTypeAlignmentShift(C->getType());
153 // Mark the start of the global
154 O << "\t.cc_top " << name << ".data," << name << "\n";
156 switch (GV->getLinkage()) {
157 case GlobalValue::AppendingLinkage:
158 llvm_report_error("AppendingLinkage is not supported by this target!");
159 case GlobalValue::LinkOnceAnyLinkage:
160 case GlobalValue::LinkOnceODRLinkage:
161 case GlobalValue::WeakAnyLinkage:
162 case GlobalValue::WeakODRLinkage:
163 case GlobalValue::ExternalLinkage:
164 emitArrayBound(name, GV);
165 emitGlobalDirective(name);
166 // TODO Use COMDAT groups for LinkOnceLinkage
167 if (GV->hasWeakLinkage() || GV->hasLinkOnceLinkage()) {
168 O << TAI->getWeakDefDirective() << name << "\n";
171 case GlobalValue::InternalLinkage:
172 case GlobalValue::PrivateLinkage:
173 case GlobalValue::LinkerPrivateLinkage:
175 case GlobalValue::GhostLinkage:
176 llvm_unreachable("Should not have any unmaterialized functions!");
177 case GlobalValue::DLLImportLinkage:
178 llvm_unreachable("DLLImport linkage is not supported by this target!");
179 case GlobalValue::DLLExportLinkage:
180 llvm_unreachable("DLLExport linkage is not supported by this target!");
182 llvm_unreachable("Unknown linkage type!");
185 EmitAlignment(Align, GV, 2);
187 unsigned Size = TD->getTypeAllocSize(C->getType());
188 if (GV->isThreadLocal()) {
191 if (TAI->hasDotTypeDotSizeDirective()) {
192 O << "\t.type " << name << ",@object\n";
193 O << "\t.size " << name << "," << Size << "\n";
197 EmitGlobalConstant(C);
198 if (GV->isThreadLocal()) {
199 for (unsigned i = 1; i < MaxThreads; ++i) {
200 EmitGlobalConstant(C);
204 // The ABI requires that unsigned scalar types smaller than 32 bits
205 // are are padded to 32 bits.
209 // Mark the end of the global
210 O << "\t.cc_bottom " << name << ".data\n";
213 /// Emit the directives on the start of functions
214 void XCoreAsmPrinter::emitFunctionStart(MachineFunction &MF) {
215 // Print out the label for the function.
216 const Function *F = MF.getFunction();
218 SwitchToSection(TAI->SectionForGlobal(F));
220 // Mark the start of the function
221 O << "\t.cc_top " << CurrentFnName << ".function," << CurrentFnName << "\n";
223 switch (F->getLinkage()) {
224 default: llvm_unreachable("Unknown linkage type!");
225 case Function::InternalLinkage: // Symbols default to internal.
226 case Function::PrivateLinkage:
227 case Function::LinkerPrivateLinkage:
229 case Function::ExternalLinkage:
230 emitGlobalDirective(CurrentFnName);
232 case Function::LinkOnceAnyLinkage:
233 case Function::LinkOnceODRLinkage:
234 case Function::WeakAnyLinkage:
235 case Function::WeakODRLinkage:
236 // TODO Use COMDAT groups for LinkOnceLinkage
237 O << TAI->getGlobalDirective() << CurrentFnName << "\n";
238 O << TAI->getWeakDefDirective() << CurrentFnName << "\n";
241 // (1 << 1) byte aligned
242 EmitAlignment(MF.getAlignment(), F, 1);
243 if (TAI->hasDotTypeDotSizeDirective()) {
244 O << "\t.type " << CurrentFnName << ",@function\n";
246 O << CurrentFnName << ":\n";
249 /// Emit the directives on the end of functions
250 void XCoreAsmPrinter::
251 emitFunctionEnd(MachineFunction &MF)
253 // Mark the end of the function
254 O << "\t.cc_bottom " << CurrentFnName << ".function\n";
257 /// runOnMachineFunction - This uses the printMachineInstruction()
258 /// method to print assembly for each instruction.
260 bool XCoreAsmPrinter::runOnMachineFunction(MachineFunction &MF)
264 SetupMachineFunction(MF);
266 // Print out constants referenced by the function
267 EmitConstantPool(MF.getConstantPool());
269 // Print out jump tables referenced by the function
270 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
272 // Emit the function start directives
273 emitFunctionStart(MF);
275 // Emit pre-function debug information.
276 DW->BeginFunction(&MF);
278 // Print out code for the function.
279 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
282 // Print a label for the basic block.
283 if (I != MF.begin()) {
284 printBasicBlockLabel(I, true , true);
288 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
290 // Print the assembly for the instruction.
292 printMachineInstruction(II);
295 // Each Basic Block is separated by a newline
299 // Emit function end directives
302 // Emit post-function debug information.
303 DW->EndFunction(&MF);
305 // We didn't modify anything.
309 void XCoreAsmPrinter::printMemOperand(const MachineInstr *MI, int opNum)
311 printOperand(MI, opNum);
313 if (MI->getOperand(opNum+1).isImm()
314 && MI->getOperand(opNum+1).getImm() == 0)
318 printOperand(MI, opNum+1);
321 void XCoreAsmPrinter::printOperand(const MachineInstr *MI, int opNum) {
322 const MachineOperand &MO = MI->getOperand(opNum);
323 switch (MO.getType()) {
324 case MachineOperand::MO_Register:
325 if (TargetRegisterInfo::isPhysicalRegister(MO.getReg()))
326 O << TM.getRegisterInfo()->get(MO.getReg()).AsmName;
328 llvm_unreachable("not implemented");
330 case MachineOperand::MO_Immediate:
333 case MachineOperand::MO_MachineBasicBlock:
334 printBasicBlockLabel(MO.getMBB());
336 case MachineOperand::MO_GlobalAddress:
337 O << Mang->getMangledName(MO.getGlobal());
339 case MachineOperand::MO_ExternalSymbol:
340 O << MO.getSymbolName();
342 case MachineOperand::MO_ConstantPoolIndex:
343 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber()
344 << '_' << MO.getIndex();
346 case MachineOperand::MO_JumpTableIndex:
347 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
348 << '_' << MO.getIndex();
351 llvm_unreachable("not implemented");
355 /// PrintAsmOperand - Print out an operand for an inline asm expression.
357 bool XCoreAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
359 const char *ExtraCode) {
360 printOperand(MI, OpNo);
364 void XCoreAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
367 // Check for mov mnemonic
368 unsigned src, dst, srcSR, dstSR;
369 if (TM.getInstrInfo()->isMoveInstr(*MI, src, dst, srcSR, dstSR)) {
371 O << TM.getRegisterInfo()->get(dst).AsmName;
373 O << TM.getRegisterInfo()->get(src).AsmName;
377 if (printInstruction(MI)) {
380 llvm_unreachable("Unhandled instruction in asm writer!");
383 bool XCoreAsmPrinter::doInitialization(Module &M) {
384 bool Result = AsmPrinter::doInitialization(M);
385 DW = getAnalysisIfAvailable<DwarfWriter>();
390 bool XCoreAsmPrinter::doFinalization(Module &M) {
392 // Print out module-level global variables.
393 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
398 return AsmPrinter::doFinalization(M);