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/StringExtras.h"
36 #include "llvm/Support/CommandLine.h"
37 #include "llvm/Support/ErrorHandling.h"
38 #include "llvm/Support/FormattedStream.h"
39 #include "llvm/Support/MathExtras.h"
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 XCoreAsmPrinter : public AsmPrinter {
52 const XCoreSubtarget &Subtarget;
54 explicit XCoreAsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
55 MCContext &Ctx, MCStreamer &Streamer,
57 : AsmPrinter(O, TM, Ctx, Streamer, T),
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 MCSymbol *Sym);
71 void emitArrayBound(const MCSymbol *Sym, const GlobalVariable *GV);
72 virtual void EmitGlobalVariable(const GlobalVariable *GV);
74 void emitFunctionStart(MachineFunction &MF);
76 void printInstruction(const MachineInstr *MI); // autogenerated.
77 static const char *getRegisterName(unsigned RegNo);
79 bool runOnMachineFunction(MachineFunction &MF);
80 void EmitInstruction(const MachineInstr *MI);
81 void EmitFunctionBodyEnd();
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 void XCoreAsmPrinter::emitGlobalDirective(const MCSymbol *Sym) {
95 O << MAI->getGlobalDirective() << *Sym << "\n";
98 void XCoreAsmPrinter::emitArrayBound(const MCSymbol *Sym,
99 const GlobalVariable *GV) {
100 assert(((GV->hasExternalLinkage() ||
101 GV->hasWeakLinkage()) ||
102 GV->hasLinkOnceLinkage()) && "Unexpected linkage");
103 if (const ArrayType *ATy = dyn_cast<ArrayType>(
104 cast<PointerType>(GV->getType())->getElementType())) {
105 O << MAI->getGlobalDirective() << *Sym;
106 O << ".globound" << "\n";
107 O << "\t.set\t" << *Sym;
108 O << ".globound" << "," << ATy->getNumElements() << "\n";
109 if (GV->hasWeakLinkage() || GV->hasLinkOnceLinkage()) {
110 // TODO Use COMDAT groups for LinkOnceLinkage
111 O << MAI->getWeakDefDirective() << *Sym << ".globound" << "\n";
116 void XCoreAsmPrinter::EmitGlobalVariable(const GlobalVariable *GV) {
117 // Check to see if this is a special global used by LLVM, if so, emit it.
118 if (!GV->hasInitializer() ||
119 EmitSpecialLLVMGlobal(GV))
122 const TargetData *TD = TM.getTargetData();
123 OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(GV, Mang,TM));
126 MCSymbol *GVSym = GetGlobalValueSymbol(GV);
127 Constant *C = GV->getInitializer();
128 unsigned Align = (unsigned)TD->getPreferredTypeAlignmentShift(C->getType());
130 // Mark the start of the global
131 O << "\t.cc_top " << *GVSym << ".data," << *GVSym << "\n";
133 switch (GV->getLinkage()) {
134 case GlobalValue::AppendingLinkage:
135 llvm_report_error("AppendingLinkage is not supported by this target!");
136 case GlobalValue::LinkOnceAnyLinkage:
137 case GlobalValue::LinkOnceODRLinkage:
138 case GlobalValue::WeakAnyLinkage:
139 case GlobalValue::WeakODRLinkage:
140 case GlobalValue::ExternalLinkage:
141 emitArrayBound(GVSym, GV);
142 emitGlobalDirective(GVSym);
143 // TODO Use COMDAT groups for LinkOnceLinkage
144 if (GV->hasWeakLinkage() || GV->hasLinkOnceLinkage())
145 O << MAI->getWeakDefDirective() << *GVSym << "\n";
147 case GlobalValue::InternalLinkage:
148 case GlobalValue::PrivateLinkage:
149 case GlobalValue::LinkerPrivateLinkage:
151 case GlobalValue::DLLImportLinkage:
152 llvm_unreachable("DLLImport linkage is not supported by this target!");
153 case GlobalValue::DLLExportLinkage:
154 llvm_unreachable("DLLExport linkage is not supported by this target!");
156 llvm_unreachable("Unknown linkage type!");
159 EmitAlignment(Align, GV, 2);
161 unsigned Size = TD->getTypeAllocSize(C->getType());
162 if (GV->isThreadLocal()) {
165 if (MAI->hasDotTypeDotSizeDirective()) {
166 O << "\t.type " << *GVSym << ",@object\n";
167 O << "\t.size " << *GVSym << "," << Size << "\n";
169 O << *GVSym << ":\n";
171 EmitGlobalConstant(C);
172 if (GV->isThreadLocal()) {
173 for (unsigned i = 1; i < MaxThreads; ++i)
174 EmitGlobalConstant(C);
176 // The ABI requires that unsigned scalar types smaller than 32 bits
177 // are padded to 32 bits.
179 OutStreamer.EmitZeros(4 - Size, 0);
181 // Mark the end of the global
182 O << "\t.cc_bottom " << *GVSym << ".data\n";
185 /// Emit the directives on the start of functions
186 void XCoreAsmPrinter::emitFunctionStart(MachineFunction &MF) {
187 // Print out the label for the function.
188 const Function *F = MF.getFunction();
190 OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(F, Mang, TM));
192 // Mark the start of the function
193 O << "\t.cc_top " << *CurrentFnSym << ".function," << *CurrentFnSym << "\n";
195 switch (F->getLinkage()) {
196 default: llvm_unreachable("Unknown linkage type!");
197 case Function::InternalLinkage: // Symbols default to internal.
198 case Function::PrivateLinkage:
199 case Function::LinkerPrivateLinkage:
201 case Function::ExternalLinkage:
202 emitGlobalDirective(CurrentFnSym);
204 case Function::LinkOnceAnyLinkage:
205 case Function::LinkOnceODRLinkage:
206 case Function::WeakAnyLinkage:
207 case Function::WeakODRLinkage:
208 // TODO Use COMDAT groups for LinkOnceLinkage
209 O << MAI->getGlobalDirective() << *CurrentFnSym << "\n";
210 O << MAI->getWeakDefDirective() << *CurrentFnSym << "\n";
213 // (1 << 1) byte aligned
214 EmitAlignment(MF.getAlignment(), F, 1);
215 if (MAI->hasDotTypeDotSizeDirective())
216 O << "\t.type " << *CurrentFnSym << ",@function\n";
218 O << *CurrentFnSym << ":\n";
222 /// EmitFunctionBodyEnd - Targets can override this to emit stuff after
223 /// the last basic block in the function.
224 void XCoreAsmPrinter::EmitFunctionBodyEnd() {
225 // Emit function end directives
226 O << "\t.cc_bottom " << *CurrentFnSym << ".function\n";
229 /// runOnMachineFunction - This uses the printMachineInstruction()
230 /// method to print assembly for each instruction.
232 bool XCoreAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
233 SetupMachineFunction(MF);
235 // Print out constants referenced by the function
238 // Emit the function start directives
239 emitFunctionStart(MF);
241 // Emit pre-function debug information.
242 DW->BeginFunction(&MF);
248 void XCoreAsmPrinter::printMemOperand(const MachineInstr *MI, int opNum)
250 printOperand(MI, opNum);
252 if (MI->getOperand(opNum+1).isImm()
253 && MI->getOperand(opNum+1).getImm() == 0)
257 printOperand(MI, opNum+1);
260 void XCoreAsmPrinter::printOperand(const MachineInstr *MI, int opNum) {
261 const MachineOperand &MO = MI->getOperand(opNum);
262 switch (MO.getType()) {
263 case MachineOperand::MO_Register:
264 O << getRegisterName(MO.getReg());
266 case MachineOperand::MO_Immediate:
269 case MachineOperand::MO_MachineBasicBlock:
270 O << *MO.getMBB()->getSymbol(OutContext);
272 case MachineOperand::MO_GlobalAddress:
273 O << *GetGlobalValueSymbol(MO.getGlobal());
275 case MachineOperand::MO_ExternalSymbol:
276 O << MO.getSymbolName();
278 case MachineOperand::MO_ConstantPoolIndex:
279 O << MAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber()
280 << '_' << MO.getIndex();
282 case MachineOperand::MO_JumpTableIndex:
283 O << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
284 << '_' << MO.getIndex();
286 case MachineOperand::MO_BlockAddress:
287 O << *GetBlockAddressSymbol(MO.getBlockAddress());
290 llvm_unreachable("not implemented");
294 /// PrintAsmOperand - Print out an operand for an inline asm expression.
296 bool XCoreAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
298 const char *ExtraCode) {
299 printOperand(MI, OpNo);
303 void XCoreAsmPrinter::EmitInstruction(const MachineInstr *MI) {
304 // Check for mov mnemonic
305 unsigned src, dst, srcSR, dstSR;
306 if (TM.getInstrInfo()->isMoveInstr(*MI, src, dst, srcSR, dstSR)) {
307 O << "\tmov " << getRegisterName(dst) << ", ";
308 O << getRegisterName(src);
309 OutStreamer.AddBlankLine();
312 printInstruction(MI);
313 OutStreamer.AddBlankLine();
316 // Force static initialization.
317 extern "C" void LLVMInitializeXCoreAsmPrinter() {
318 RegisterAsmPrinter<XCoreAsmPrinter> X(TheXCoreTarget);