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/Mangler.h"
34 #include "llvm/Target/TargetData.h"
35 #include "llvm/Target/TargetLoweringObjectFile.h"
36 #include "llvm/Target/TargetRegistry.h"
37 #include "llvm/ADT/StringExtras.h"
38 #include "llvm/Support/CommandLine.h"
39 #include "llvm/Support/ErrorHandling.h"
40 #include "llvm/Support/FormattedStream.h"
41 #include "llvm/Support/MathExtras.h"
46 static cl::opt<unsigned> MaxThreads("xcore-max-threads", cl::Optional,
47 cl::desc("Maximum number of threads (for emulation thread-local storage)"),
49 cl::value_desc("number"),
53 class XCoreAsmPrinter : public AsmPrinter {
54 const XCoreSubtarget &Subtarget;
56 explicit XCoreAsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
57 MCContext &Ctx, MCStreamer &Streamer,
59 : AsmPrinter(O, TM, Ctx, Streamer, T),
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 printInlineJT(const MachineInstr *MI, int opNum,
68 const std::string &directive = ".jmptable");
69 void printInlineJT32(const MachineInstr *MI, int opNum) {
70 printInlineJT(MI, opNum, ".jmptable32");
72 void printOperand(const MachineInstr *MI, int opNum);
73 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
74 unsigned AsmVariant, const char *ExtraCode);
76 void emitGlobalDirective(const MCSymbol *Sym);
78 void emitArrayBound(const MCSymbol *Sym, const GlobalVariable *GV);
79 virtual void EmitGlobalVariable(const GlobalVariable *GV);
81 void emitFunctionStart(MachineFunction &MF);
83 void printInstruction(const MachineInstr *MI); // autogenerated.
84 static const char *getRegisterName(unsigned RegNo);
86 bool runOnMachineFunction(MachineFunction &MF);
87 void EmitInstruction(const MachineInstr *MI);
88 void EmitFunctionBodyEnd();
90 void getAnalysisUsage(AnalysisUsage &AU) const {
91 AsmPrinter::getAnalysisUsage(AU);
93 AU.addRequired<MachineModuleInfo>();
94 AU.addRequired<DwarfWriter>();
97 } // end of anonymous namespace
99 #include "XCoreGenAsmWriter.inc"
101 void XCoreAsmPrinter::emitGlobalDirective(const MCSymbol *Sym) {
102 O << MAI->getGlobalDirective() << *Sym << "\n";
105 void XCoreAsmPrinter::emitArrayBound(const MCSymbol *Sym,
106 const GlobalVariable *GV) {
107 assert(((GV->hasExternalLinkage() ||
108 GV->hasWeakLinkage()) ||
109 GV->hasLinkOnceLinkage()) && "Unexpected linkage");
110 if (const ArrayType *ATy = dyn_cast<ArrayType>(
111 cast<PointerType>(GV->getType())->getElementType())) {
112 O << MAI->getGlobalDirective() << *Sym;
113 O << ".globound" << "\n";
114 O << "\t.set\t" << *Sym;
115 O << ".globound" << "," << ATy->getNumElements() << "\n";
116 if (GV->hasWeakLinkage() || GV->hasLinkOnceLinkage()) {
117 // TODO Use COMDAT groups for LinkOnceLinkage
118 O << MAI->getWeakDefDirective() << *Sym << ".globound" << "\n";
123 void XCoreAsmPrinter::EmitGlobalVariable(const GlobalVariable *GV) {
124 // Check to see if this is a special global used by LLVM, if so, emit it.
125 if (!GV->hasInitializer() ||
126 EmitSpecialLLVMGlobal(GV))
129 const TargetData *TD = TM.getTargetData();
130 OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(GV, Mang,TM));
133 MCSymbol *GVSym = Mang->getSymbol(GV);
134 Constant *C = GV->getInitializer();
135 unsigned Align = (unsigned)TD->getPreferredTypeAlignmentShift(C->getType());
137 // Mark the start of the global
138 O << "\t.cc_top " << *GVSym << ".data," << *GVSym << "\n";
140 switch (GV->getLinkage()) {
141 case GlobalValue::AppendingLinkage:
142 llvm_report_error("AppendingLinkage is not supported by this target!");
143 case GlobalValue::LinkOnceAnyLinkage:
144 case GlobalValue::LinkOnceODRLinkage:
145 case GlobalValue::WeakAnyLinkage:
146 case GlobalValue::WeakODRLinkage:
147 case GlobalValue::ExternalLinkage:
148 emitArrayBound(GVSym, GV);
149 emitGlobalDirective(GVSym);
150 // TODO Use COMDAT groups for LinkOnceLinkage
151 if (GV->hasWeakLinkage() || GV->hasLinkOnceLinkage())
152 O << MAI->getWeakDefDirective() << *GVSym << "\n";
154 case GlobalValue::InternalLinkage:
155 case GlobalValue::PrivateLinkage:
156 case GlobalValue::LinkerPrivateLinkage:
158 case GlobalValue::DLLImportLinkage:
159 llvm_unreachable("DLLImport linkage is not supported by this target!");
160 case GlobalValue::DLLExportLinkage:
161 llvm_unreachable("DLLExport linkage is not supported by this target!");
163 llvm_unreachable("Unknown linkage type!");
166 EmitAlignment(Align, GV, 2);
168 unsigned Size = TD->getTypeAllocSize(C->getType());
169 if (GV->isThreadLocal()) {
172 if (MAI->hasDotTypeDotSizeDirective()) {
173 O << "\t.type " << *GVSym << ",@object\n";
174 O << "\t.size " << *GVSym << "," << Size << "\n";
176 O << *GVSym << ":\n";
178 EmitGlobalConstant(C);
179 if (GV->isThreadLocal()) {
180 for (unsigned i = 1; i < MaxThreads; ++i)
181 EmitGlobalConstant(C);
183 // The ABI requires that unsigned scalar types smaller than 32 bits
184 // are padded to 32 bits.
186 OutStreamer.EmitZeros(4 - Size, 0);
188 // Mark the end of the global
189 O << "\t.cc_bottom " << *GVSym << ".data\n";
192 /// Emit the directives on the start of functions
193 void XCoreAsmPrinter::emitFunctionStart(MachineFunction &MF) {
194 // Print out the label for the function.
195 const Function *F = MF.getFunction();
197 OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(F, Mang, TM));
199 // Mark the start of the function
200 O << "\t.cc_top " << *CurrentFnSym << ".function," << *CurrentFnSym << "\n";
202 switch (F->getLinkage()) {
203 default: llvm_unreachable("Unknown linkage type!");
204 case Function::InternalLinkage: // Symbols default to internal.
205 case Function::PrivateLinkage:
206 case Function::LinkerPrivateLinkage:
208 case Function::ExternalLinkage:
209 emitGlobalDirective(CurrentFnSym);
211 case Function::LinkOnceAnyLinkage:
212 case Function::LinkOnceODRLinkage:
213 case Function::WeakAnyLinkage:
214 case Function::WeakODRLinkage:
215 // TODO Use COMDAT groups for LinkOnceLinkage
216 O << MAI->getGlobalDirective() << *CurrentFnSym << "\n";
217 O << MAI->getWeakDefDirective() << *CurrentFnSym << "\n";
220 // (1 << 1) byte aligned
221 EmitAlignment(MF.getAlignment(), F, 1);
222 if (MAI->hasDotTypeDotSizeDirective())
223 O << "\t.type " << *CurrentFnSym << ",@function\n";
225 O << *CurrentFnSym << ":\n";
229 /// EmitFunctionBodyEnd - Targets can override this to emit stuff after
230 /// the last basic block in the function.
231 void XCoreAsmPrinter::EmitFunctionBodyEnd() {
232 // Emit function end directives
233 O << "\t.cc_bottom " << *CurrentFnSym << ".function\n";
236 /// runOnMachineFunction - This uses the printMachineInstruction()
237 /// method to print assembly for each instruction.
239 bool XCoreAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
240 SetupMachineFunction(MF);
242 // Print out constants referenced by the function
245 // Emit the function start directives
246 emitFunctionStart(MF);
248 // Emit pre-function debug information.
249 DW->BeginFunction(&MF);
255 void XCoreAsmPrinter::printMemOperand(const MachineInstr *MI, int opNum)
257 printOperand(MI, opNum);
259 if (MI->getOperand(opNum+1).isImm()
260 && MI->getOperand(opNum+1).getImm() == 0)
264 printOperand(MI, opNum+1);
267 void XCoreAsmPrinter::
268 printInlineJT(const MachineInstr *MI, int opNum, const std::string &directive)
270 unsigned JTI = MI->getOperand(opNum).getIndex();
271 const MachineFunction *MF = MI->getParent()->getParent();
272 const MachineJumpTableInfo *MJTI = MF->getJumpTableInfo();
273 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
274 const std::vector<MachineBasicBlock*> &JTBBs = JT[JTI].MBBs;
275 O << "\t" << directive << " ";
276 for (unsigned i = 0, e = JTBBs.size(); i != e; ++i) {
277 MachineBasicBlock *MBB = JTBBs[i];
280 O << *MBB->getSymbol(OutContext);
284 void XCoreAsmPrinter::printOperand(const MachineInstr *MI, int opNum) {
285 const MachineOperand &MO = MI->getOperand(opNum);
286 switch (MO.getType()) {
287 case MachineOperand::MO_Register:
288 O << getRegisterName(MO.getReg());
290 case MachineOperand::MO_Immediate:
293 case MachineOperand::MO_MachineBasicBlock:
294 O << *MO.getMBB()->getSymbol(OutContext);
296 case MachineOperand::MO_GlobalAddress:
297 O << *Mang->getSymbol(MO.getGlobal());
299 case MachineOperand::MO_ExternalSymbol:
300 O << MO.getSymbolName();
302 case MachineOperand::MO_ConstantPoolIndex:
303 O << MAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber()
304 << '_' << MO.getIndex();
306 case MachineOperand::MO_JumpTableIndex:
307 O << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
308 << '_' << MO.getIndex();
310 case MachineOperand::MO_BlockAddress:
311 O << *GetBlockAddressSymbol(MO.getBlockAddress());
314 llvm_unreachable("not implemented");
318 /// PrintAsmOperand - Print out an operand for an inline asm expression.
320 bool XCoreAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
322 const char *ExtraCode) {
323 printOperand(MI, OpNo);
327 void XCoreAsmPrinter::EmitInstruction(const MachineInstr *MI) {
328 // Check for mov mnemonic
329 unsigned src, dst, srcSR, dstSR;
330 if (TM.getInstrInfo()->isMoveInstr(*MI, src, dst, srcSR, dstSR)) {
331 O << "\tmov " << getRegisterName(dst) << ", ";
332 O << getRegisterName(src);
333 OutStreamer.AddBlankLine();
336 printInstruction(MI);
337 OutStreamer.AddBlankLine();
340 // Force static initialization.
341 extern "C" void LLVMInitializeXCoreAsmPrinter() {
342 RegisterAsmPrinter<XCoreAsmPrinter> X(TheXCoreTarget);