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/CodeGen/MachineJumpTableInfo.h"
31 #include "llvm/MC/MCStreamer.h"
32 #include "llvm/MC/MCSymbol.h"
33 #include "llvm/Target/TargetData.h"
34 #include "llvm/Target/TargetLoweringObjectFile.h"
35 #include "llvm/Target/TargetRegistry.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 static cl::opt<unsigned> MaxThreads("xcore-max-threads", cl::Optional,
46 cl::desc("Maximum number of threads (for emulation thread-local storage)"),
48 cl::value_desc("number"),
52 class XCoreAsmPrinter : public AsmPrinter {
53 const XCoreSubtarget &Subtarget;
55 explicit XCoreAsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
56 MCContext &Ctx, MCStreamer &Streamer,
58 : AsmPrinter(O, TM, Ctx, Streamer, T),
59 Subtarget(TM.getSubtarget<XCoreSubtarget>()) {}
61 virtual const char *getPassName() const {
62 return "XCore Assembly Printer";
65 void printMemOperand(const MachineInstr *MI, int opNum);
66 void printInlineJT(const MachineInstr *MI, int opNum,
67 const std::string &directive = ".jmptable");
68 void printInlineJT32(const MachineInstr *MI, int opNum) {
69 printInlineJT(MI, opNum, ".jmptable32");
71 void printOperand(const MachineInstr *MI, int opNum);
72 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
73 unsigned AsmVariant, const char *ExtraCode);
75 void emitGlobalDirective(const MCSymbol *Sym);
77 void emitArrayBound(const MCSymbol *Sym, const GlobalVariable *GV);
78 virtual void EmitGlobalVariable(const GlobalVariable *GV);
80 void emitFunctionStart(MachineFunction &MF);
82 void printInstruction(const MachineInstr *MI); // autogenerated.
83 static const char *getRegisterName(unsigned RegNo);
85 bool runOnMachineFunction(MachineFunction &MF);
86 void EmitInstruction(const MachineInstr *MI);
87 void EmitFunctionBodyEnd();
89 void getAnalysisUsage(AnalysisUsage &AU) const {
90 AsmPrinter::getAnalysisUsage(AU);
92 AU.addRequired<MachineModuleInfo>();
93 AU.addRequired<DwarfWriter>();
96 } // end of anonymous namespace
98 #include "XCoreGenAsmWriter.inc"
100 void XCoreAsmPrinter::emitGlobalDirective(const MCSymbol *Sym) {
101 O << MAI->getGlobalDirective() << *Sym << "\n";
104 void XCoreAsmPrinter::emitArrayBound(const MCSymbol *Sym,
105 const GlobalVariable *GV) {
106 assert(((GV->hasExternalLinkage() ||
107 GV->hasWeakLinkage()) ||
108 GV->hasLinkOnceLinkage()) && "Unexpected linkage");
109 if (const ArrayType *ATy = dyn_cast<ArrayType>(
110 cast<PointerType>(GV->getType())->getElementType())) {
111 O << MAI->getGlobalDirective() << *Sym;
112 O << ".globound" << "\n";
113 O << "\t.set\t" << *Sym;
114 O << ".globound" << "," << ATy->getNumElements() << "\n";
115 if (GV->hasWeakLinkage() || GV->hasLinkOnceLinkage()) {
116 // TODO Use COMDAT groups for LinkOnceLinkage
117 O << MAI->getWeakDefDirective() << *Sym << ".globound" << "\n";
122 void XCoreAsmPrinter::EmitGlobalVariable(const GlobalVariable *GV) {
123 // Check to see if this is a special global used by LLVM, if so, emit it.
124 if (!GV->hasInitializer() ||
125 EmitSpecialLLVMGlobal(GV))
128 const TargetData *TD = TM.getTargetData();
129 OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(GV, Mang,TM));
132 MCSymbol *GVSym = GetGlobalValueSymbol(GV);
133 Constant *C = GV->getInitializer();
134 unsigned Align = (unsigned)TD->getPreferredTypeAlignmentShift(C->getType());
136 // Mark the start of the global
137 O << "\t.cc_top " << *GVSym << ".data," << *GVSym << "\n";
139 switch (GV->getLinkage()) {
140 case GlobalValue::AppendingLinkage:
141 llvm_report_error("AppendingLinkage is not supported by this target!");
142 case GlobalValue::LinkOnceAnyLinkage:
143 case GlobalValue::LinkOnceODRLinkage:
144 case GlobalValue::WeakAnyLinkage:
145 case GlobalValue::WeakODRLinkage:
146 case GlobalValue::ExternalLinkage:
147 emitArrayBound(GVSym, GV);
148 emitGlobalDirective(GVSym);
149 // TODO Use COMDAT groups for LinkOnceLinkage
150 if (GV->hasWeakLinkage() || GV->hasLinkOnceLinkage())
151 O << MAI->getWeakDefDirective() << *GVSym << "\n";
153 case GlobalValue::InternalLinkage:
154 case GlobalValue::PrivateLinkage:
155 case GlobalValue::LinkerPrivateLinkage:
157 case GlobalValue::DLLImportLinkage:
158 llvm_unreachable("DLLImport linkage is not supported by this target!");
159 case GlobalValue::DLLExportLinkage:
160 llvm_unreachable("DLLExport linkage is not supported by this target!");
162 llvm_unreachable("Unknown linkage type!");
165 EmitAlignment(Align, GV, 2);
167 unsigned Size = TD->getTypeAllocSize(C->getType());
168 if (GV->isThreadLocal()) {
171 if (MAI->hasDotTypeDotSizeDirective()) {
172 O << "\t.type " << *GVSym << ",@object\n";
173 O << "\t.size " << *GVSym << "," << Size << "\n";
175 O << *GVSym << ":\n";
177 EmitGlobalConstant(C);
178 if (GV->isThreadLocal()) {
179 for (unsigned i = 1; i < MaxThreads; ++i)
180 EmitGlobalConstant(C);
182 // The ABI requires that unsigned scalar types smaller than 32 bits
183 // are padded to 32 bits.
185 OutStreamer.EmitZeros(4 - Size, 0);
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";
228 /// EmitFunctionBodyEnd - Targets can override this to emit stuff after
229 /// the last basic block in the function.
230 void XCoreAsmPrinter::EmitFunctionBodyEnd() {
231 // Emit function end directives
232 O << "\t.cc_bottom " << *CurrentFnSym << ".function\n";
235 /// runOnMachineFunction - This uses the printMachineInstruction()
236 /// method to print assembly for each instruction.
238 bool XCoreAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
239 SetupMachineFunction(MF);
241 // Print out constants referenced by the function
244 // Emit the function start directives
245 emitFunctionStart(MF);
247 // Emit pre-function debug information.
248 DW->BeginFunction(&MF);
254 void XCoreAsmPrinter::printMemOperand(const MachineInstr *MI, int opNum)
256 printOperand(MI, opNum);
258 if (MI->getOperand(opNum+1).isImm()
259 && MI->getOperand(opNum+1).getImm() == 0)
263 printOperand(MI, opNum+1);
266 void XCoreAsmPrinter::
267 printInlineJT(const MachineInstr *MI, int opNum, const std::string &directive)
269 unsigned JTI = MI->getOperand(opNum).getIndex();
270 const MachineFunction *MF = MI->getParent()->getParent();
271 const MachineJumpTableInfo *MJTI = MF->getJumpTableInfo();
272 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
273 const std::vector<MachineBasicBlock*> &JTBBs = JT[JTI].MBBs;
274 O << "\t" << directive << " ";
275 for (unsigned i = 0, e = JTBBs.size(); i != e; ++i) {
276 MachineBasicBlock *MBB = JTBBs[i];
279 O << *MBB->getSymbol(OutContext);
283 void XCoreAsmPrinter::printOperand(const MachineInstr *MI, int opNum) {
284 const MachineOperand &MO = MI->getOperand(opNum);
285 switch (MO.getType()) {
286 case MachineOperand::MO_Register:
287 O << getRegisterName(MO.getReg());
289 case MachineOperand::MO_Immediate:
292 case MachineOperand::MO_MachineBasicBlock:
293 O << *MO.getMBB()->getSymbol(OutContext);
295 case MachineOperand::MO_GlobalAddress:
296 O << *GetGlobalValueSymbol(MO.getGlobal());
298 case MachineOperand::MO_ExternalSymbol:
299 O << MO.getSymbolName();
301 case MachineOperand::MO_ConstantPoolIndex:
302 O << MAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber()
303 << '_' << MO.getIndex();
305 case MachineOperand::MO_JumpTableIndex:
306 O << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
307 << '_' << MO.getIndex();
309 case MachineOperand::MO_BlockAddress:
310 O << *GetBlockAddressSymbol(MO.getBlockAddress());
313 llvm_unreachable("not implemented");
317 /// PrintAsmOperand - Print out an operand for an inline asm expression.
319 bool XCoreAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
321 const char *ExtraCode) {
322 printOperand(MI, OpNo);
326 void XCoreAsmPrinter::EmitInstruction(const MachineInstr *MI) {
327 // Check for mov mnemonic
328 unsigned src, dst, srcSR, dstSR;
329 if (TM.getInstrInfo()->isMoveInstr(*MI, src, dst, srcSR, dstSR)) {
330 O << "\tmov " << getRegisterName(dst) << ", ";
331 O << getRegisterName(src);
332 OutStreamer.AddBlankLine();
335 printInstruction(MI);
336 OutStreamer.AddBlankLine();
339 // Force static initialization.
340 extern "C" void LLVMInitializeXCoreAsmPrinter() {
341 RegisterAsmPrinter<XCoreAsmPrinter> X(TheXCoreTarget);