LLVM API Documentation
00001 //===-- XCoreAsmPrinter.cpp - XCore LLVM assembly writer ------------------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file contains a printer that converts from our internal representation 00011 // of machine-dependent LLVM code to the XAS-format XCore assembly language. 00012 // 00013 //===----------------------------------------------------------------------===// 00014 00015 #define DEBUG_TYPE "asm-printer" 00016 #include "XCore.h" 00017 #include "XCoreInstrInfo.h" 00018 #include "XCoreSubtarget.h" 00019 #include "XCoreTargetAsmInfo.h" 00020 #include "XCoreTargetMachine.h" 00021 #include "llvm/Constants.h" 00022 #include "llvm/DerivedTypes.h" 00023 #include "llvm/Module.h" 00024 #include "llvm/CodeGen/AsmPrinter.h" 00025 #include "llvm/CodeGen/DwarfWriter.h" 00026 #include "llvm/CodeGen/MachineModuleInfo.h" 00027 #include "llvm/CodeGen/MachineFunctionPass.h" 00028 #include "llvm/CodeGen/MachineConstantPool.h" 00029 #include "llvm/CodeGen/MachineInstr.h" 00030 #include "llvm/MC/MCStreamer.h" 00031 #include "llvm/Target/TargetData.h" 00032 #include "llvm/Target/TargetLoweringObjectFile.h" 00033 #include "llvm/Target/TargetRegistry.h" 00034 #include "llvm/ADT/Statistic.h" 00035 #include "llvm/ADT/StringExtras.h" 00036 #include "llvm/Support/CommandLine.h" 00037 #include "llvm/Support/ErrorHandling.h" 00038 #include "llvm/Support/FormattedStream.h" 00039 #include "llvm/Support/Mangler.h" 00040 #include "llvm/Support/MathExtras.h" 00041 #include <algorithm> 00042 #include <cctype> 00043 using namespace llvm; 00044 00045 STATISTIC(EmittedInsts, "Number of machine instrs printed"); 00046 00047 static cl::opt<unsigned> MaxThreads("xcore-max-threads", cl::Optional, 00048 cl::desc("Maximum number of threads (for emulation thread-local storage)"), 00049 cl::Hidden, 00050 cl::value_desc("number"), 00051 cl::init(8)); 00052 00053 namespace { 00054 class VISIBILITY_HIDDEN XCoreAsmPrinter : public AsmPrinter { 00055 DwarfWriter *DW; 00056 const XCoreSubtarget &Subtarget; 00057 public: 00058 explicit XCoreAsmPrinter(formatted_raw_ostream &O, TargetMachine &TM, 00059 const TargetAsmInfo *T, bool V) 00060 : AsmPrinter(O, TM, T, V), DW(0), 00061 Subtarget(TM.getSubtarget<XCoreSubtarget>()) {} 00062 00063 virtual const char *getPassName() const { 00064 return "XCore Assembly Printer"; 00065 } 00066 00067 void printMemOperand(const MachineInstr *MI, int opNum); 00068 void printOperand(const MachineInstr *MI, int opNum); 00069 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo, 00070 unsigned AsmVariant, const char *ExtraCode); 00071 00072 void emitGlobalDirective(const std::string &name); 00073 void emitExternDirective(const std::string &name); 00074 00075 void emitArrayBound(const std::string &name, const GlobalVariable *GV); 00076 virtual void PrintGlobalVariable(const GlobalVariable *GV); 00077 00078 void emitFunctionStart(MachineFunction &MF); 00079 void emitFunctionEnd(MachineFunction &MF); 00080 00081 void printInstruction(const MachineInstr *MI); // autogenerated. 00082 void printMachineInstruction(const MachineInstr *MI); 00083 bool runOnMachineFunction(MachineFunction &F); 00084 bool doInitialization(Module &M); 00085 00086 void getAnalysisUsage(AnalysisUsage &AU) const { 00087 AsmPrinter::getAnalysisUsage(AU); 00088 AU.setPreservesAll(); 00089 AU.addRequired<MachineModuleInfo>(); 00090 AU.addRequired<DwarfWriter>(); 00091 } 00092 }; 00093 } // end of anonymous namespace 00094 00095 #include "XCoreGenAsmWriter.inc" 00096 00097 void XCoreAsmPrinter:: 00098 emitGlobalDirective(const std::string &name) 00099 { 00100 O << TAI->getGlobalDirective() << name; 00101 O << "\n"; 00102 } 00103 00104 void XCoreAsmPrinter:: 00105 emitExternDirective(const std::string &name) 00106 { 00107 O << "\t.extern\t" << name; 00108 O << '\n'; 00109 } 00110 00111 void XCoreAsmPrinter:: 00112 emitArrayBound(const std::string &name, const GlobalVariable *GV) 00113 { 00114 assert(((GV->hasExternalLinkage() || 00115 GV->hasWeakLinkage()) || 00116 GV->hasLinkOnceLinkage()) && "Unexpected linkage"); 00117 if (const ArrayType *ATy = dyn_cast<ArrayType>( 00118 cast<PointerType>(GV->getType())->getElementType())) 00119 { 00120 O << TAI->getGlobalDirective() << name << ".globound" << "\n"; 00121 O << TAI->getSetDirective() << name << ".globound" << "," 00122 << ATy->getNumElements() << "\n"; 00123 if (GV->hasWeakLinkage() || GV->hasLinkOnceLinkage()) { 00124 // TODO Use COMDAT groups for LinkOnceLinkage 00125 O << TAI->getWeakDefDirective() << name << ".globound" << "\n"; 00126 } 00127 } 00128 } 00129 00130 void XCoreAsmPrinter::PrintGlobalVariable(const GlobalVariable *GV) { 00131 // Check to see if this is a special global used by LLVM, if so, emit it. 00132 if (!GV->hasInitializer() || 00133 EmitSpecialLLVMGlobal(GV)) 00134 return; 00135 00136 const TargetData *TD = TM.getTargetData(); 00137 00138 OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(GV, Mang,TM)); 00139 00140 std::string name = Mang->getMangledName(GV); 00141 Constant *C = GV->getInitializer(); 00142 unsigned Align = (unsigned)TD->getPreferredTypeAlignmentShift(C->getType()); 00143 00144 // Mark the start of the global 00145 O << "\t.cc_top " << name << ".data," << name << "\n"; 00146 00147 switch (GV->getLinkage()) { 00148 case GlobalValue::AppendingLinkage: 00149 llvm_report_error("AppendingLinkage is not supported by this target!"); 00150 case GlobalValue::LinkOnceAnyLinkage: 00151 case GlobalValue::LinkOnceODRLinkage: 00152 case GlobalValue::WeakAnyLinkage: 00153 case GlobalValue::WeakODRLinkage: 00154 case GlobalValue::ExternalLinkage: 00155 emitArrayBound(name, GV); 00156 emitGlobalDirective(name); 00157 // TODO Use COMDAT groups for LinkOnceLinkage 00158 if (GV->hasWeakLinkage() || GV->hasLinkOnceLinkage()) { 00159 O << TAI->getWeakDefDirective() << name << "\n"; 00160 } 00161 // FALL THROUGH 00162 case GlobalValue::InternalLinkage: 00163 case GlobalValue::PrivateLinkage: 00164 case GlobalValue::LinkerPrivateLinkage: 00165 break; 00166 case GlobalValue::GhostLinkage: 00167 llvm_unreachable("Should not have any unmaterialized functions!"); 00168 case GlobalValue::DLLImportLinkage: 00169 llvm_unreachable("DLLImport linkage is not supported by this target!"); 00170 case GlobalValue::DLLExportLinkage: 00171 llvm_unreachable("DLLExport linkage is not supported by this target!"); 00172 default: 00173 llvm_unreachable("Unknown linkage type!"); 00174 } 00175 00176 EmitAlignment(Align, GV, 2); 00177 00178 unsigned Size = TD->getTypeAllocSize(C->getType()); 00179 if (GV->isThreadLocal()) { 00180 Size *= MaxThreads; 00181 } 00182 if (TAI->hasDotTypeDotSizeDirective()) { 00183 O << "\t.type " << name << ",@object\n"; 00184 O << "\t.size " << name << "," << Size << "\n"; 00185 } 00186 O << name << ":\n"; 00187 00188 EmitGlobalConstant(C); 00189 if (GV->isThreadLocal()) { 00190 for (unsigned i = 1; i < MaxThreads; ++i) { 00191 EmitGlobalConstant(C); 00192 } 00193 } 00194 if (Size < 4) { 00195 // The ABI requires that unsigned scalar types smaller than 32 bits 00196 // are are padded to 32 bits. 00197 EmitZeros(4 - Size); 00198 } 00199 00200 // Mark the end of the global 00201 O << "\t.cc_bottom " << name << ".data\n"; 00202 } 00203 00204 /// Emit the directives on the start of functions 00205 void XCoreAsmPrinter::emitFunctionStart(MachineFunction &MF) { 00206 // Print out the label for the function. 00207 const Function *F = MF.getFunction(); 00208 00209 OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(F, Mang, TM)); 00210 00211 // Mark the start of the function 00212 O << "\t.cc_top " << CurrentFnName << ".function," << CurrentFnName << "\n"; 00213 00214 switch (F->getLinkage()) { 00215 default: llvm_unreachable("Unknown linkage type!"); 00216 case Function::InternalLinkage: // Symbols default to internal. 00217 case Function::PrivateLinkage: 00218 case Function::LinkerPrivateLinkage: 00219 break; 00220 case Function::ExternalLinkage: 00221 emitGlobalDirective(CurrentFnName); 00222 break; 00223 case Function::LinkOnceAnyLinkage: 00224 case Function::LinkOnceODRLinkage: 00225 case Function::WeakAnyLinkage: 00226 case Function::WeakODRLinkage: 00227 // TODO Use COMDAT groups for LinkOnceLinkage 00228 O << TAI->getGlobalDirective() << CurrentFnName << "\n"; 00229 O << TAI->getWeakDefDirective() << CurrentFnName << "\n"; 00230 break; 00231 } 00232 // (1 << 1) byte aligned 00233 EmitAlignment(MF.getAlignment(), F, 1); 00234 if (TAI->hasDotTypeDotSizeDirective()) { 00235 O << "\t.type " << CurrentFnName << ",@function\n"; 00236 } 00237 O << CurrentFnName << ":\n"; 00238 } 00239 00240 /// Emit the directives on the end of functions 00241 void XCoreAsmPrinter:: 00242 emitFunctionEnd(MachineFunction &MF) 00243 { 00244 // Mark the end of the function 00245 O << "\t.cc_bottom " << CurrentFnName << ".function\n"; 00246 } 00247 00248 /// runOnMachineFunction - This uses the printMachineInstruction() 00249 /// method to print assembly for each instruction. 00250 /// 00251 bool XCoreAsmPrinter::runOnMachineFunction(MachineFunction &MF) 00252 { 00253 this->MF = &MF; 00254 00255 SetupMachineFunction(MF); 00256 00257 // Print out constants referenced by the function 00258 EmitConstantPool(MF.getConstantPool()); 00259 00260 // Print out jump tables referenced by the function 00261 EmitJumpTableInfo(MF.getJumpTableInfo(), MF); 00262 00263 // Emit the function start directives 00264 emitFunctionStart(MF); 00265 00266 // Emit pre-function debug information. 00267 DW->BeginFunction(&MF); 00268 00269 // Print out code for the function. 00270 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end(); 00271 I != E; ++I) { 00272 00273 // Print a label for the basic block. 00274 if (I != MF.begin()) { 00275 printBasicBlockLabel(I, true , true); 00276 O << '\n'; 00277 } 00278 00279 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end(); 00280 II != E; ++II) { 00281 // Print the assembly for the instruction. 00282 O << "\t"; 00283 printMachineInstruction(II); 00284 } 00285 00286 // Each Basic Block is separated by a newline 00287 O << '\n'; 00288 } 00289 00290 // Emit function end directives 00291 emitFunctionEnd(MF); 00292 00293 // Emit post-function debug information. 00294 DW->EndFunction(&MF); 00295 00296 // We didn't modify anything. 00297 return false; 00298 } 00299 00300 void XCoreAsmPrinter::printMemOperand(const MachineInstr *MI, int opNum) 00301 { 00302 printOperand(MI, opNum); 00303 00304 if (MI->getOperand(opNum+1).isImm() 00305 && MI->getOperand(opNum+1).getImm() == 0) 00306 return; 00307 00308 O << "+"; 00309 printOperand(MI, opNum+1); 00310 } 00311 00312 void XCoreAsmPrinter::printOperand(const MachineInstr *MI, int opNum) { 00313 const MachineOperand &MO = MI->getOperand(opNum); 00314 switch (MO.getType()) { 00315 case MachineOperand::MO_Register: 00316 if (TargetRegisterInfo::isPhysicalRegister(MO.getReg())) 00317 O << TM.getRegisterInfo()->get(MO.getReg()).AsmName; 00318 else 00319 llvm_unreachable("not implemented"); 00320 break; 00321 case MachineOperand::MO_Immediate: 00322 O << MO.getImm(); 00323 break; 00324 case MachineOperand::MO_MachineBasicBlock: 00325 printBasicBlockLabel(MO.getMBB()); 00326 break; 00327 case MachineOperand::MO_GlobalAddress: 00328 O << Mang->getMangledName(MO.getGlobal()); 00329 break; 00330 case MachineOperand::MO_ExternalSymbol: 00331 O << MO.getSymbolName(); 00332 break; 00333 case MachineOperand::MO_ConstantPoolIndex: 00334 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() 00335 << '_' << MO.getIndex(); 00336 break; 00337 case MachineOperand::MO_JumpTableIndex: 00338 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber() 00339 << '_' << MO.getIndex(); 00340 break; 00341 default: 00342 llvm_unreachable("not implemented"); 00343 } 00344 } 00345 00346 /// PrintAsmOperand - Print out an operand for an inline asm expression. 00347 /// 00348 bool XCoreAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo, 00349 unsigned AsmVariant, 00350 const char *ExtraCode) { 00351 printOperand(MI, OpNo); 00352 return false; 00353 } 00354 00355 void XCoreAsmPrinter::printMachineInstruction(const MachineInstr *MI) { 00356 ++EmittedInsts; 00357 00358 // Check for mov mnemonic 00359 unsigned src, dst, srcSR, dstSR; 00360 if (TM.getInstrInfo()->isMoveInstr(*MI, src, dst, srcSR, dstSR)) { 00361 O << "\tmov "; 00362 O << TM.getRegisterInfo()->get(dst).AsmName; 00363 O << ", "; 00364 O << TM.getRegisterInfo()->get(src).AsmName; 00365 O << "\n"; 00366 return; 00367 } 00368 printInstruction(MI); 00369 } 00370 00371 bool XCoreAsmPrinter::doInitialization(Module &M) { 00372 bool Result = AsmPrinter::doInitialization(M); 00373 DW = getAnalysisIfAvailable<DwarfWriter>(); 00374 00375 return Result; 00376 } 00377 00378 // Force static initialization. 00379 extern "C" void LLVMInitializeXCoreAsmPrinter() { 00380 RegisterAsmPrinter<XCoreAsmPrinter> X(TheXCoreTarget); 00381 }