LLVM API Documentation
00001 //===-- AsmPrinter.cpp - Common AsmPrinter code ---------------------------===// 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 implements the AsmPrinter class. 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 #include "llvm/CodeGen/AsmPrinter.h" 00015 #include "llvm/Assembly/Writer.h" 00016 #include "llvm/DerivedTypes.h" 00017 #include "llvm/Constants.h" 00018 #include "llvm/Module.h" 00019 #include "llvm/CodeGen/GCMetadataPrinter.h" 00020 #include "llvm/CodeGen/MachineConstantPool.h" 00021 #include "llvm/CodeGen/MachineFunction.h" 00022 #include "llvm/CodeGen/MachineJumpTableInfo.h" 00023 #include "llvm/CodeGen/MachineLoopInfo.h" 00024 #include "llvm/CodeGen/MachineModuleInfo.h" 00025 #include "llvm/CodeGen/DwarfWriter.h" 00026 #include "llvm/Analysis/DebugInfo.h" 00027 #include "llvm/MC/MCContext.h" 00028 #include "llvm/MC/MCInst.h" 00029 #include "llvm/MC/MCSection.h" 00030 #include "llvm/MC/MCStreamer.h" 00031 #include "llvm/Support/CommandLine.h" 00032 #include "llvm/Support/ErrorHandling.h" 00033 #include "llvm/Support/FormattedStream.h" 00034 #include "llvm/Support/Mangler.h" 00035 #include "llvm/Target/TargetAsmInfo.h" 00036 #include "llvm/Target/TargetData.h" 00037 #include "llvm/Target/TargetLowering.h" 00038 #include "llvm/Target/TargetLoweringObjectFile.h" 00039 #include "llvm/Target/TargetOptions.h" 00040 #include "llvm/Target/TargetRegisterInfo.h" 00041 #include "llvm/ADT/SmallPtrSet.h" 00042 #include "llvm/ADT/SmallString.h" 00043 #include "llvm/ADT/StringExtras.h" 00044 #include <cerrno> 00045 using namespace llvm; 00046 00047 static cl::opt<cl::boolOrDefault> 00048 AsmVerbose("asm-verbose", cl::desc("Add comments to directives."), 00049 cl::init(cl::BOU_UNSET)); 00050 00051 char AsmPrinter::ID = 0; 00052 AsmPrinter::AsmPrinter(formatted_raw_ostream &o, TargetMachine &tm, 00053 const TargetAsmInfo *T, bool VDef) 00054 : MachineFunctionPass(&ID), FunctionNumber(0), O(o), 00055 TM(tm), TAI(T), TRI(tm.getRegisterInfo()), 00056 00057 OutContext(*new MCContext()), 00058 OutStreamer(*createAsmStreamer(OutContext, O, *T, this)), 00059 00060 LastMI(0), LastFn(0), Counter(~0U), 00061 PrevDLT(0, ~0U, ~0U) { 00062 DW = 0; MMI = 0; 00063 switch (AsmVerbose) { 00064 case cl::BOU_UNSET: VerboseAsm = VDef; break; 00065 case cl::BOU_TRUE: VerboseAsm = true; break; 00066 case cl::BOU_FALSE: VerboseAsm = false; break; 00067 } 00068 } 00069 00070 AsmPrinter::~AsmPrinter() { 00071 for (gcp_iterator I = GCMetadataPrinters.begin(), 00072 E = GCMetadataPrinters.end(); I != E; ++I) 00073 delete I->second; 00074 00075 delete &OutStreamer; 00076 delete &OutContext; 00077 } 00078 00079 TargetLoweringObjectFile &AsmPrinter::getObjFileLowering() const { 00080 return TM.getTargetLowering()->getObjFileLowering(); 00081 } 00082 00083 /// getCurrentSection() - Return the current section we are emitting to. 00084 const MCSection *AsmPrinter::getCurrentSection() const { 00085 return OutStreamer.getCurrentSection(); 00086 } 00087 00088 00089 void AsmPrinter::getAnalysisUsage(AnalysisUsage &AU) const { 00090 AU.setPreservesAll(); 00091 MachineFunctionPass::getAnalysisUsage(AU); 00092 AU.addRequired<GCModuleInfo>(); 00093 if (VerboseAsm) 00094 AU.addRequired<MachineLoopInfo>(); 00095 } 00096 00097 bool AsmPrinter::doInitialization(Module &M) { 00098 // Initialize TargetLoweringObjectFile. 00099 const_cast<TargetLoweringObjectFile&>(getObjFileLowering()) 00100 .Initialize(OutContext, TM); 00101 00102 Mang = new Mangler(M, TAI->getGlobalPrefix(), TAI->getPrivateGlobalPrefix(), 00103 TAI->getLinkerPrivateGlobalPrefix()); 00104 00105 if (TAI->doesAllowQuotesInName()) 00106 Mang->setUseQuotes(true); 00107 00108 GCModuleInfo *MI = getAnalysisIfAvailable<GCModuleInfo>(); 00109 assert(MI && "AsmPrinter didn't require GCModuleInfo?"); 00110 00111 if (TAI->hasSingleParameterDotFile()) { 00112 /* Very minimal debug info. It is ignored if we emit actual 00113 debug info. If we don't, this at helps the user find where 00114 a function came from. */ 00115 O << "\t.file\t\"" << M.getModuleIdentifier() << "\"\n"; 00116 } 00117 00118 for (GCModuleInfo::iterator I = MI->begin(), E = MI->end(); I != E; ++I) 00119 if (GCMetadataPrinter *MP = GetOrCreateGCPrinter(*I)) 00120 MP->beginAssembly(O, *this, *TAI); 00121 00122 if (!M.getModuleInlineAsm().empty()) 00123 O << TAI->getCommentString() << " Start of file scope inline assembly\n" 00124 << M.getModuleInlineAsm() 00125 << '\n' << TAI->getCommentString() 00126 << " End of file scope inline assembly\n"; 00127 00128 if (TAI->doesSupportDebugInformation() || 00129 TAI->doesSupportExceptionHandling()) { 00130 MMI = getAnalysisIfAvailable<MachineModuleInfo>(); 00131 if (MMI) 00132 MMI->AnalyzeModule(M); 00133 DW = getAnalysisIfAvailable<DwarfWriter>(); 00134 if (DW) 00135 DW->BeginModule(&M, MMI, O, this, TAI); 00136 } 00137 00138 return false; 00139 } 00140 00141 bool AsmPrinter::doFinalization(Module &M) { 00142 // Emit global variables. 00143 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end(); 00144 I != E; ++I) 00145 PrintGlobalVariable(I); 00146 00147 // Emit final debug information. 00148 if (TAI->doesSupportDebugInformation() || TAI->doesSupportExceptionHandling()) 00149 DW->EndModule(); 00150 00151 // If the target wants to know about weak references, print them all. 00152 if (TAI->getWeakRefDirective()) { 00153 // FIXME: This is not lazy, it would be nice to only print weak references 00154 // to stuff that is actually used. Note that doing so would require targets 00155 // to notice uses in operands (due to constant exprs etc). This should 00156 // happen with the MC stuff eventually. 00157 00158 // Print out module-level global variables here. 00159 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end(); 00160 I != E; ++I) { 00161 if (I->hasExternalWeakLinkage()) 00162 O << TAI->getWeakRefDirective() << Mang->getMangledName(I) << '\n'; 00163 } 00164 00165 for (Module::const_iterator I = M.begin(), E = M.end(); I != E; ++I) { 00166 if (I->hasExternalWeakLinkage()) 00167 O << TAI->getWeakRefDirective() << Mang->getMangledName(I) << '\n'; 00168 } 00169 } 00170 00171 if (TAI->getSetDirective()) { 00172 O << '\n'; 00173 for (Module::const_alias_iterator I = M.alias_begin(), E = M.alias_end(); 00174 I != E; ++I) { 00175 std::string Name = Mang->getMangledName(I); 00176 00177 const GlobalValue *GV = cast<GlobalValue>(I->getAliasedGlobal()); 00178 std::string Target = Mang->getMangledName(GV); 00179 00180 if (I->hasExternalLinkage() || !TAI->getWeakRefDirective()) 00181 O << "\t.globl\t" << Name << '\n'; 00182 else if (I->hasWeakLinkage()) 00183 O << TAI->getWeakRefDirective() << Name << '\n'; 00184 else if (!I->hasLocalLinkage()) 00185 llvm_unreachable("Invalid alias linkage"); 00186 00187 printVisibility(Name, I->getVisibility()); 00188 00189 O << TAI->getSetDirective() << ' ' << Name << ", " << Target << '\n'; 00190 } 00191 } 00192 00193 GCModuleInfo *MI = getAnalysisIfAvailable<GCModuleInfo>(); 00194 assert(MI && "AsmPrinter didn't require GCModuleInfo?"); 00195 for (GCModuleInfo::iterator I = MI->end(), E = MI->begin(); I != E; ) 00196 if (GCMetadataPrinter *MP = GetOrCreateGCPrinter(*--I)) 00197 MP->finishAssembly(O, *this, *TAI); 00198 00199 // If we don't have any trampolines, then we don't require stack memory 00200 // to be executable. Some targets have a directive to declare this. 00201 Function *InitTrampolineIntrinsic = M.getFunction("llvm.init.trampoline"); 00202 if (!InitTrampolineIntrinsic || InitTrampolineIntrinsic->use_empty()) 00203 if (TAI->getNonexecutableStackDirective()) 00204 O << TAI->getNonexecutableStackDirective() << '\n'; 00205 00206 delete Mang; Mang = 0; 00207 DW = 0; MMI = 0; 00208 00209 OutStreamer.Finish(); 00210 return false; 00211 } 00212 00213 std::string 00214 AsmPrinter::getCurrentFunctionEHName(const MachineFunction *MF) const { 00215 assert(MF && "No machine function?"); 00216 return Mang->getMangledName(MF->getFunction(), ".eh", 00217 TAI->is_EHSymbolPrivate()); 00218 } 00219 00220 void AsmPrinter::SetupMachineFunction(MachineFunction &MF) { 00221 // What's my mangled name? 00222 CurrentFnName = Mang->getMangledName(MF.getFunction()); 00223 IncrementFunctionNumber(); 00224 00225 if (VerboseAsm) { 00226 LI = &getAnalysis<MachineLoopInfo>(); 00227 } 00228 } 00229 00230 namespace { 00231 // SectionCPs - Keep track the alignment, constpool entries per Section. 00232 struct SectionCPs { 00233 const MCSection *S; 00234 unsigned Alignment; 00235 SmallVector<unsigned, 4> CPEs; 00236 SectionCPs(const MCSection *s, unsigned a) : S(s), Alignment(a) {}; 00237 }; 00238 } 00239 00240 /// EmitConstantPool - Print to the current output stream assembly 00241 /// representations of the constants in the constant pool MCP. This is 00242 /// used to print out constants which have been "spilled to memory" by 00243 /// the code generator. 00244 /// 00245 void AsmPrinter::EmitConstantPool(MachineConstantPool *MCP) { 00246 const std::vector<MachineConstantPoolEntry> &CP = MCP->getConstants(); 00247 if (CP.empty()) return; 00248 00249 // Calculate sections for constant pool entries. We collect entries to go into 00250 // the same section together to reduce amount of section switch statements. 00251 SmallVector<SectionCPs, 4> CPSections; 00252 for (unsigned i = 0, e = CP.size(); i != e; ++i) { 00253 const MachineConstantPoolEntry &CPE = CP[i]; 00254 unsigned Align = CPE.getAlignment(); 00255 00256 SectionKind Kind; 00257 switch (CPE.getRelocationInfo()) { 00258 default: llvm_unreachable("Unknown section kind"); 00259 case 2: Kind = SectionKind::getReadOnlyWithRel(); break; 00260 case 1: 00261 Kind = SectionKind::getReadOnlyWithRelLocal(); 00262 break; 00263 case 0: 00264 switch (TM.getTargetData()->getTypeAllocSize(CPE.getType())) { 00265 case 4: Kind = SectionKind::getMergeableConst4(); break; 00266 case 8: Kind = SectionKind::getMergeableConst8(); break; 00267 case 16: Kind = SectionKind::getMergeableConst16();break; 00268 default: Kind = SectionKind::getMergeableConst(); break; 00269 } 00270 } 00271 00272 const MCSection *S = getObjFileLowering().getSectionForConstant(Kind); 00273 00274 // The number of sections are small, just do a linear search from the 00275 // last section to the first. 00276 bool Found = false; 00277 unsigned SecIdx = CPSections.size(); 00278 while (SecIdx != 0) { 00279 if (CPSections[--SecIdx].S == S) { 00280 Found = true; 00281 break; 00282 } 00283 } 00284 if (!Found) { 00285 SecIdx = CPSections.size(); 00286 CPSections.push_back(SectionCPs(S, Align)); 00287 } 00288 00289 if (Align > CPSections[SecIdx].Alignment) 00290 CPSections[SecIdx].Alignment = Align; 00291 CPSections[SecIdx].CPEs.push_back(i); 00292 } 00293 00294 // Now print stuff into the calculated sections. 00295 for (unsigned i = 0, e = CPSections.size(); i != e; ++i) { 00296 OutStreamer.SwitchSection(CPSections[i].S); 00297 EmitAlignment(Log2_32(CPSections[i].Alignment)); 00298 00299 unsigned Offset = 0; 00300 for (unsigned j = 0, ee = CPSections[i].CPEs.size(); j != ee; ++j) { 00301 unsigned CPI = CPSections[i].CPEs[j]; 00302 MachineConstantPoolEntry CPE = CP[CPI]; 00303 00304 // Emit inter-object padding for alignment. 00305 unsigned AlignMask = CPE.getAlignment() - 1; 00306 unsigned NewOffset = (Offset + AlignMask) & ~AlignMask; 00307 EmitZeros(NewOffset - Offset); 00308 00309 const Type *Ty = CPE.getType(); 00310 Offset = NewOffset + TM.getTargetData()->getTypeAllocSize(Ty); 00311 00312 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << '_' 00313 << CPI << ':'; 00314 if (VerboseAsm) { 00315 O.PadToColumn(TAI->getCommentColumn()); 00316 O << TAI->getCommentString() << " constant "; 00317 WriteTypeSymbolic(O, CPE.getType(), MF->getFunction()->getParent()); 00318 } 00319 O << '\n'; 00320 if (CPE.isMachineConstantPoolEntry()) 00321 EmitMachineConstantPoolValue(CPE.Val.MachineCPVal); 00322 else 00323 EmitGlobalConstant(CPE.Val.ConstVal); 00324 } 00325 } 00326 } 00327 00328 /// EmitJumpTableInfo - Print assembly representations of the jump tables used 00329 /// by the current function to the current output stream. 00330 /// 00331 void AsmPrinter::EmitJumpTableInfo(MachineJumpTableInfo *MJTI, 00332 MachineFunction &MF) { 00333 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables(); 00334 if (JT.empty()) return; 00335 00336 bool IsPic = TM.getRelocationModel() == Reloc::PIC_; 00337 00338 // Pick the directive to use to print the jump table entries, and switch to 00339 // the appropriate section. 00340 TargetLowering *LoweringInfo = TM.getTargetLowering(); 00341 00342 const Function *F = MF.getFunction(); 00343 bool JTInDiffSection = false; 00344 if (F->isWeakForLinker() || 00345 (IsPic && !LoweringInfo->usesGlobalOffsetTable())) { 00346 // In PIC mode, we need to emit the jump table to the same section as the 00347 // function body itself, otherwise the label differences won't make sense. 00348 // We should also do if the section name is NULL or function is declared in 00349 // discardable section. 00350 OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(F, Mang, 00351 TM)); 00352 } else { 00353 // Otherwise, drop it in the readonly section. 00354 const MCSection *ReadOnlySection = 00355 getObjFileLowering().getSectionForConstant(SectionKind::getReadOnly()); 00356 OutStreamer.SwitchSection(ReadOnlySection); 00357 JTInDiffSection = true; 00358 } 00359 00360 EmitAlignment(Log2_32(MJTI->getAlignment())); 00361 00362 for (unsigned i = 0, e = JT.size(); i != e; ++i) { 00363 const std::vector<MachineBasicBlock*> &JTBBs = JT[i].MBBs; 00364 00365 // If this jump table was deleted, ignore it. 00366 if (JTBBs.empty()) continue; 00367 00368 // For PIC codegen, if possible we want to use the SetDirective to reduce 00369 // the number of relocations the assembler will generate for the jump table. 00370 // Set directives are all printed before the jump table itself. 00371 SmallPtrSet<MachineBasicBlock*, 16> EmittedSets; 00372 if (TAI->getSetDirective() && IsPic) 00373 for (unsigned ii = 0, ee = JTBBs.size(); ii != ee; ++ii) 00374 if (EmittedSets.insert(JTBBs[ii])) 00375 printPICJumpTableSetLabel(i, JTBBs[ii]); 00376 00377 // On some targets (e.g. darwin) we want to emit two consequtive labels 00378 // before each jump table. The first label is never referenced, but tells 00379 // the assembler and linker the extents of the jump table object. The 00380 // second label is actually referenced by the code. 00381 if (JTInDiffSection) { 00382 if (const char *JTLabelPrefix = TAI->getJumpTableSpecialLabelPrefix()) 00383 O << JTLabelPrefix << "JTI" << getFunctionNumber() << '_' << i << ":\n"; 00384 } 00385 00386 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber() 00387 << '_' << i << ":\n"; 00388 00389 for (unsigned ii = 0, ee = JTBBs.size(); ii != ee; ++ii) { 00390 printPICJumpTableEntry(MJTI, JTBBs[ii], i); 00391 O << '\n'; 00392 } 00393 } 00394 } 00395 00396 void AsmPrinter::printPICJumpTableEntry(const MachineJumpTableInfo *MJTI, 00397 const MachineBasicBlock *MBB, 00398 unsigned uid) const { 00399 bool isPIC = TM.getRelocationModel() == Reloc::PIC_; 00400 00401 // Use JumpTableDirective otherwise honor the entry size from the jump table 00402 // info. 00403 const char *JTEntryDirective = TAI->getJumpTableDirective(isPIC); 00404 bool HadJTEntryDirective = JTEntryDirective != NULL; 00405 if (!HadJTEntryDirective) { 00406 JTEntryDirective = MJTI->getEntrySize() == 4 ? 00407 TAI->getData32bitsDirective() : TAI->getData64bitsDirective(); 00408 } 00409 00410 O << JTEntryDirective << ' '; 00411 00412 // If we have emitted set directives for the jump table entries, print 00413 // them rather than the entries themselves. If we're emitting PIC, then 00414 // emit the table entries as differences between two text section labels. 00415 // If we're emitting non-PIC code, then emit the entries as direct 00416 // references to the target basic blocks. 00417 if (!isPIC) { 00418 printBasicBlockLabel(MBB, false, false, false); 00419 } else if (TAI->getSetDirective()) { 00420 O << TAI->getPrivateGlobalPrefix() << getFunctionNumber() 00421 << '_' << uid << "_set_" << MBB->getNumber(); 00422 } else { 00423 printBasicBlockLabel(MBB, false, false, false); 00424 // If the arch uses custom Jump Table directives, don't calc relative to 00425 // JT 00426 if (!HadJTEntryDirective) 00427 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI" 00428 << getFunctionNumber() << '_' << uid; 00429 } 00430 } 00431 00432 00433 /// EmitSpecialLLVMGlobal - Check to see if the specified global is a 00434 /// special global used by LLVM. If so, emit it and return true, otherwise 00435 /// do nothing and return false. 00436 bool AsmPrinter::EmitSpecialLLVMGlobal(const GlobalVariable *GV) { 00437 if (GV->getName() == "llvm.used") { 00438 if (TAI->getUsedDirective() != 0) // No need to emit this at all. 00439 EmitLLVMUsedList(GV->getInitializer()); 00440 return true; 00441 } 00442 00443 // Ignore debug and non-emitted data. This handles llvm.compiler.used. 00444 if (GV->getSection() == "llvm.metadata" || 00445 GV->hasAvailableExternallyLinkage()) 00446 return true; 00447 00448 if (!GV->hasAppendingLinkage()) return false; 00449 00450 assert(GV->hasInitializer() && "Not a special LLVM global!"); 00451 00452 const TargetData *TD = TM.getTargetData(); 00453 unsigned Align = Log2_32(TD->getPointerPrefAlignment()); 00454 if (GV->getName() == "llvm.global_ctors") { 00455 OutStreamer.SwitchSection(getObjFileLowering().getStaticCtorSection()); 00456 EmitAlignment(Align, 0); 00457 EmitXXStructorList(GV->getInitializer()); 00458 return true; 00459 } 00460 00461 if (GV->getName() == "llvm.global_dtors") { 00462 OutStreamer.SwitchSection(getObjFileLowering().getStaticDtorSection()); 00463 EmitAlignment(Align, 0); 00464 EmitXXStructorList(GV->getInitializer()); 00465 return true; 00466 } 00467 00468 return false; 00469 } 00470 00471 /// EmitLLVMUsedList - For targets that define a TAI::UsedDirective, mark each 00472 /// global in the specified llvm.used list for which emitUsedDirectiveFor 00473 /// is true, as being used with this directive. 00474 void AsmPrinter::EmitLLVMUsedList(Constant *List) { 00475 const char *Directive = TAI->getUsedDirective(); 00476 00477 // Should be an array of 'i8*'. 00478 ConstantArray *InitList = dyn_cast<ConstantArray>(List); 00479 if (InitList == 0) return; 00480 00481 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) { 00482 const GlobalValue *GV = 00483 dyn_cast<GlobalValue>(InitList->getOperand(i)->stripPointerCasts()); 00484 if (GV && getObjFileLowering().shouldEmitUsedDirectiveFor(GV, Mang)) { 00485 O << Directive; 00486 EmitConstantValueOnly(InitList->getOperand(i)); 00487 O << '\n'; 00488 } 00489 } 00490 } 00491 00492 /// EmitXXStructorList - Emit the ctor or dtor list. This just prints out the 00493 /// function pointers, ignoring the init priority. 00494 void AsmPrinter::EmitXXStructorList(Constant *List) { 00495 // Should be an array of '{ int, void ()* }' structs. The first value is the 00496 // init priority, which we ignore. 00497 if (!isa<ConstantArray>(List)) return; 00498 ConstantArray *InitList = cast<ConstantArray>(List); 00499 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) 00500 if (ConstantStruct *CS = dyn_cast<ConstantStruct>(InitList->getOperand(i))){ 00501 if (CS->getNumOperands() != 2) return; // Not array of 2-element structs. 00502 00503 if (CS->getOperand(1)->isNullValue()) 00504 return; // Found a null terminator, exit printing. 00505 // Emit the function pointer. 00506 EmitGlobalConstant(CS->getOperand(1)); 00507 } 00508 } 00509 00510 /// getGlobalLinkName - Returns the asm/link name of of the specified 00511 /// global variable. Should be overridden by each target asm printer to 00512 /// generate the appropriate value. 00513 const std::string &AsmPrinter::getGlobalLinkName(const GlobalVariable *GV, 00514 std::string &LinkName) const { 00515 if (isa<Function>(GV)) { 00516 LinkName += TAI->getFunctionAddrPrefix(); 00517 LinkName += Mang->getMangledName(GV); 00518 LinkName += TAI->getFunctionAddrSuffix(); 00519 } else { 00520 LinkName += TAI->getGlobalVarAddrPrefix(); 00521 LinkName += Mang->getMangledName(GV); 00522 LinkName += TAI->getGlobalVarAddrSuffix(); 00523 } 00524 00525 return LinkName; 00526 } 00527 00528 /// EmitExternalGlobal - Emit the external reference to a global variable. 00529 /// Should be overridden if an indirect reference should be used. 00530 void AsmPrinter::EmitExternalGlobal(const GlobalVariable *GV) { 00531 std::string GLN; 00532 O << getGlobalLinkName(GV, GLN); 00533 } 00534 00535 00536 00537 //===----------------------------------------------------------------------===// 00538 /// LEB 128 number encoding. 00539 00540 /// PrintULEB128 - Print a series of hexidecimal values (separated by commas) 00541 /// representing an unsigned leb128 value. 00542 void AsmPrinter::PrintULEB128(unsigned Value) const { 00543 char Buffer[20]; 00544 do { 00545 unsigned char Byte = static_cast<unsigned char>(Value & 0x7f); 00546 Value >>= 7; 00547 if (Value) Byte |= 0x80; 00548 O << "0x" << utohex_buffer(Byte, Buffer+20); 00549 if (Value) O << ", "; 00550 } while (Value); 00551 } 00552 00553 /// PrintSLEB128 - Print a series of hexidecimal values (separated by commas) 00554 /// representing a signed leb128 value. 00555 void AsmPrinter::PrintSLEB128(int Value) const { 00556 int Sign = Value >> (8 * sizeof(Value) - 1); 00557 bool IsMore; 00558 char Buffer[20]; 00559 00560 do { 00561 unsigned char Byte = static_cast<unsigned char>(Value & 0x7f); 00562 Value >>= 7; 00563 IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0; 00564 if (IsMore) Byte |= 0x80; 00565 O << "0x" << utohex_buffer(Byte, Buffer+20); 00566 if (IsMore) O << ", "; 00567 } while (IsMore); 00568 } 00569 00570 //===--------------------------------------------------------------------===// 00571 // Emission and print routines 00572 // 00573 00574 /// PrintHex - Print a value as a hexidecimal value. 00575 /// 00576 void AsmPrinter::PrintHex(int Value) const { 00577 char Buffer[20]; 00578 O << "0x" << utohex_buffer(static_cast<unsigned>(Value), Buffer+20); 00579 } 00580 00581 /// EOL - Print a newline character to asm stream. If a comment is present 00582 /// then it will be printed first. Comments should not contain '\n'. 00583 void AsmPrinter::EOL() const { 00584 O << '\n'; 00585 } 00586 00587 void AsmPrinter::EOL(const std::string &Comment) const { 00588 if (VerboseAsm && !Comment.empty()) { 00589 O.PadToColumn(TAI->getCommentColumn()); 00590 O << TAI->getCommentString() 00591 << ' ' 00592 << Comment; 00593 } 00594 O << '\n'; 00595 } 00596 00597 void AsmPrinter::EOL(const char* Comment) const { 00598 if (VerboseAsm && *Comment) { 00599 O.PadToColumn(TAI->getCommentColumn()); 00600 O << TAI->getCommentString() 00601 << ' ' 00602 << Comment; 00603 } 00604 O << '\n'; 00605 } 00606 00607 /// EmitULEB128Bytes - Emit an assembler byte data directive to compose an 00608 /// unsigned leb128 value. 00609 void AsmPrinter::EmitULEB128Bytes(unsigned Value) const { 00610 if (TAI->hasLEB128()) { 00611 O << "\t.uleb128\t" 00612 << Value; 00613 } else { 00614 O << TAI->getData8bitsDirective(); 00615 PrintULEB128(Value); 00616 } 00617 } 00618 00619 /// EmitSLEB128Bytes - print an assembler byte data directive to compose a 00620 /// signed leb128 value. 00621 void AsmPrinter::EmitSLEB128Bytes(int Value) const { 00622 if (TAI->hasLEB128()) { 00623 O << "\t.sleb128\t" 00624 << Value; 00625 } else { 00626 O << TAI->getData8bitsDirective(); 00627 PrintSLEB128(Value); 00628 } 00629 } 00630 00631 /// EmitInt8 - Emit a byte directive and value. 00632 /// 00633 void AsmPrinter::EmitInt8(int Value) const { 00634 O << TAI->getData8bitsDirective(); 00635 PrintHex(Value & 0xFF); 00636 } 00637 00638 /// EmitInt16 - Emit a short directive and value. 00639 /// 00640 void AsmPrinter::EmitInt16(int Value) const { 00641 O << TAI->getData16bitsDirective(); 00642 PrintHex(Value & 0xFFFF); 00643 } 00644 00645 /// EmitInt32 - Emit a long directive and value. 00646 /// 00647 void AsmPrinter::EmitInt32(int Value) const { 00648 O << TAI->getData32bitsDirective(); 00649 PrintHex(Value); 00650 } 00651 00652 /// EmitInt64 - Emit a long long directive and value. 00653 /// 00654 void AsmPrinter::EmitInt64(uint64_t Value) const { 00655 if (TAI->getData64bitsDirective()) { 00656 O << TAI->getData64bitsDirective(); 00657 PrintHex(Value); 00658 } else { 00659 if (TM.getTargetData()->isBigEndian()) { 00660 EmitInt32(unsigned(Value >> 32)); O << '\n'; 00661 EmitInt32(unsigned(Value)); 00662 } else { 00663 EmitInt32(unsigned(Value)); O << '\n'; 00664 EmitInt32(unsigned(Value >> 32)); 00665 } 00666 } 00667 } 00668 00669 /// toOctal - Convert the low order bits of X into an octal digit. 00670 /// 00671 static inline char toOctal(int X) { 00672 return (X&7)+'0'; 00673 } 00674 00675 /// printStringChar - Print a char, escaped if necessary. 00676 /// 00677 static void printStringChar(formatted_raw_ostream &O, unsigned char C) { 00678 if (C == '"') { 00679 O << "\\\""; 00680 } else if (C == '\\') { 00681 O << "\\\\"; 00682 } else if (isprint((unsigned char)C)) { 00683 O << C; 00684 } else { 00685 switch(C) { 00686 case '\b': O << "\\b"; break; 00687 case '\f': O << "\\f"; break; 00688 case '\n': O << "\\n"; break; 00689 case '\r': O << "\\r"; break; 00690 case '\t': O << "\\t"; break; 00691 default: 00692 O << '\\'; 00693 O << toOctal(C >> 6); 00694 O << toOctal(C >> 3); 00695 O << toOctal(C >> 0); 00696 break; 00697 } 00698 } 00699 } 00700 00701 /// EmitString - Emit a string with quotes and a null terminator. 00702 /// Special characters are emitted properly. 00703 /// \literal (Eg. '\t') \endliteral 00704 void AsmPrinter::EmitString(const std::string &String) const { 00705 EmitString(String.c_str(), String.size()); 00706 } 00707 00708 void AsmPrinter::EmitString(const char *String, unsigned Size) const { 00709 const char* AscizDirective = TAI->getAscizDirective(); 00710 if (AscizDirective) 00711 O << AscizDirective; 00712 else 00713 O << TAI->getAsciiDirective(); 00714 O << '\"'; 00715 for (unsigned i = 0; i < Size; ++i) 00716 printStringChar(O, String[i]); 00717 if (AscizDirective) 00718 O << '\"'; 00719 else 00720 O << "\\0\""; 00721 } 00722 00723 00724 /// EmitFile - Emit a .file directive. 00725 void AsmPrinter::EmitFile(unsigned Number, const std::string &Name) const { 00726 O << "\t.file\t" << Number << " \""; 00727 for (unsigned i = 0, N = Name.size(); i < N; ++i) 00728 printStringChar(O, Name[i]); 00729 O << '\"'; 00730 } 00731 00732 00733 //===----------------------------------------------------------------------===// 00734 00735 // EmitAlignment - Emit an alignment directive to the specified power of 00736 // two boundary. For example, if you pass in 3 here, you will get an 8 00737 // byte alignment. If a global value is specified, and if that global has 00738 // an explicit alignment requested, it will unconditionally override the 00739 // alignment request. However, if ForcedAlignBits is specified, this value 00740 // has final say: the ultimate alignment will be the max of ForcedAlignBits 00741 // and the alignment computed with NumBits and the global. 00742 // 00743 // The algorithm is: 00744 // Align = NumBits; 00745 // if (GV && GV->hasalignment) Align = GV->getalignment(); 00746 // Align = std::max(Align, ForcedAlignBits); 00747 // 00748 void AsmPrinter::EmitAlignment(unsigned NumBits, const GlobalValue *GV, 00749 unsigned ForcedAlignBits, 00750 bool UseFillExpr) const { 00751 if (GV && GV->getAlignment()) 00752 NumBits = Log2_32(GV->getAlignment()); 00753 NumBits = std::max(NumBits, ForcedAlignBits); 00754 00755 if (NumBits == 0) return; // No need to emit alignment. 00756 00757 unsigned FillValue = 0; 00758 if (getCurrentSection()->getKind().isText()) 00759 FillValue = TAI->getTextAlignFillValue(); 00760 00761 OutStreamer.EmitValueToAlignment(1 << NumBits, FillValue, 1, 0); 00762 } 00763 00764 /// EmitZeros - Emit a block of zeros. 00765 /// 00766 void AsmPrinter::EmitZeros(uint64_t NumZeros, unsigned AddrSpace) const { 00767 if (NumZeros) { 00768 if (TAI->getZeroDirective()) { 00769 O << TAI->getZeroDirective() << NumZeros; 00770 if (TAI->getZeroDirectiveSuffix()) 00771 O << TAI->getZeroDirectiveSuffix(); 00772 O << '\n'; 00773 } else { 00774 for (; NumZeros; --NumZeros) 00775 O << TAI->getData8bitsDirective(AddrSpace) << "0\n"; 00776 } 00777 } 00778 } 00779 00780 // Print out the specified constant, without a storage class. Only the 00781 // constants valid in constant expressions can occur here. 00782 void AsmPrinter::EmitConstantValueOnly(const Constant *CV) { 00783 if (CV->isNullValue() || isa<UndefValue>(CV)) 00784 O << '0'; 00785 else if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) { 00786 O << CI->getZExtValue(); 00787 } else if (const GlobalValue *GV = dyn_cast<GlobalValue>(CV)) { 00788 // This is a constant address for a global variable or function. Use the 00789 // name of the variable or function as the address value, possibly 00790 // decorating it with GlobalVarAddrPrefix/Suffix or 00791 // FunctionAddrPrefix/Suffix (these all default to "" ) 00792 if (isa<Function>(GV)) { 00793 O << TAI->getFunctionAddrPrefix() 00794 << Mang->getMangledName(GV) 00795 << TAI->getFunctionAddrSuffix(); 00796 } else { 00797 O << TAI->getGlobalVarAddrPrefix() 00798 << Mang->getMangledName(GV) 00799 << TAI->getGlobalVarAddrSuffix(); 00800 } 00801 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) { 00802 const TargetData *TD = TM.getTargetData(); 00803 unsigned Opcode = CE->getOpcode(); 00804 switch (Opcode) { 00805 case Instruction::Trunc: 00806 case Instruction::ZExt: 00807 case Instruction::SExt: 00808 case Instruction::FPTrunc: 00809 case Instruction::FPExt: 00810 case Instruction::UIToFP: 00811 case Instruction::SIToFP: 00812 case Instruction::FPToUI: 00813 case Instruction::FPToSI: 00814 llvm_unreachable("FIXME: Don't support this constant cast expr"); 00815 case Instruction::GetElementPtr: { 00816 // generate a symbolic expression for the byte address 00817 const Constant *ptrVal = CE->getOperand(0); 00818 SmallVector<Value*, 8> idxVec(CE->op_begin()+1, CE->op_end()); 00819 if (int64_t Offset = TD->getIndexedOffset(ptrVal->getType(), &idxVec[0], 00820 idxVec.size())) { 00821 // Truncate/sext the offset to the pointer size. 00822 if (TD->getPointerSizeInBits() != 64) { 00823 int SExtAmount = 64-TD->getPointerSizeInBits(); 00824 Offset = (Offset << SExtAmount) >> SExtAmount; 00825 } 00826 00827 if (Offset) 00828 O << '('; 00829 EmitConstantValueOnly(ptrVal); 00830 if (Offset > 0) 00831 O << ") + " << Offset; 00832 else if (Offset < 0) 00833 O << ") - " << -Offset; 00834 } else { 00835 EmitConstantValueOnly(ptrVal); 00836 } 00837 break; 00838 } 00839 case Instruction::BitCast: 00840 return EmitConstantValueOnly(CE->getOperand(0)); 00841 00842 case Instruction::IntToPtr: { 00843 // Handle casts to pointers by changing them into casts to the appropriate 00844 // integer type. This promotes constant folding and simplifies this code. 00845 Constant *Op = CE->getOperand(0); 00846 Op = ConstantExpr::getIntegerCast(Op, TD->getIntPtrType(CV->getContext()), 00847 false/*ZExt*/); 00848 return EmitConstantValueOnly(Op); 00849 } 00850 00851 00852 case Instruction::PtrToInt: { 00853 // Support only foldable casts to/from pointers that can be eliminated by 00854 // changing the pointer to the appropriately sized integer type. 00855 Constant *Op = CE->getOperand(0); 00856 const Type *Ty = CE->getType(); 00857 00858 // We can emit the pointer value into this slot if the slot is an 00859 // integer slot greater or equal to the size of the pointer. 00860 if (TD->getTypeAllocSize(Ty) == TD->getTypeAllocSize(Op->getType())) 00861 return EmitConstantValueOnly(Op); 00862 00863 O << "(("; 00864 EmitConstantValueOnly(Op); 00865 APInt ptrMask = 00866 APInt::getAllOnesValue(TD->getTypeAllocSizeInBits(Op->getType())); 00867 00868 SmallString<40> S; 00869 ptrMask.toStringUnsigned(S); 00870 O << ") & " << S.str() << ')'; 00871 break; 00872 } 00873 case Instruction::Add: 00874 case Instruction::Sub: 00875 case Instruction::And: 00876 case Instruction::Or: 00877 case Instruction::Xor: 00878 O << '('; 00879 EmitConstantValueOnly(CE->getOperand(0)); 00880 O << ')'; 00881 switch (Opcode) { 00882 case Instruction::Add: 00883 O << " + "; 00884 break; 00885 case Instruction::Sub: 00886 O << " - "; 00887 break; 00888 case Instruction::And: 00889 O << " & "; 00890 break; 00891 case Instruction::Or: 00892 O << " | "; 00893 break; 00894 case Instruction::Xor: 00895 O << " ^ "; 00896 break; 00897 default: 00898 break; 00899 } 00900 O << '('; 00901 EmitConstantValueOnly(CE->getOperand(1)); 00902 O << ')'; 00903 break; 00904 default: 00905 llvm_unreachable("Unsupported operator!"); 00906 } 00907 } else { 00908 llvm_unreachable("Unknown constant value!"); 00909 } 00910 } 00911 00912 /// printAsCString - Print the specified array as a C compatible string, only if 00913 /// the predicate isString is true. 00914 /// 00915 static void printAsCString(formatted_raw_ostream &O, const ConstantArray *CVA, 00916 unsigned LastElt) { 00917 assert(CVA->isString() && "Array is not string compatible!"); 00918 00919 O << '\"'; 00920 for (unsigned i = 0; i != LastElt; ++i) { 00921 unsigned char C = 00922 (unsigned char)cast<ConstantInt>(CVA->getOperand(i))->getZExtValue(); 00923 printStringChar(O, C); 00924 } 00925 O << '\"'; 00926 } 00927 00928 /// EmitString - Emit a zero-byte-terminated string constant. 00929 /// 00930 void AsmPrinter::EmitString(const ConstantArray *CVA) const { 00931 unsigned NumElts = CVA->getNumOperands(); 00932 if (TAI->getAscizDirective() && NumElts && 00933 cast<ConstantInt>(CVA->getOperand(NumElts-1))->getZExtValue() == 0) { 00934 O << TAI->getAscizDirective(); 00935 printAsCString(O, CVA, NumElts-1); 00936 } else { 00937 O << TAI->getAsciiDirective(); 00938 printAsCString(O, CVA, NumElts); 00939 } 00940 O << '\n'; 00941 } 00942 00943 void AsmPrinter::EmitGlobalConstantArray(const ConstantArray *CVA, 00944 unsigned AddrSpace) { 00945 if (CVA->isString()) { 00946 EmitString(CVA); 00947 } else { // Not a string. Print the values in successive locations 00948 for (unsigned i = 0, e = CVA->getNumOperands(); i != e; ++i) 00949 EmitGlobalConstant(CVA->getOperand(i), AddrSpace); 00950 } 00951 } 00952 00953 void AsmPrinter::EmitGlobalConstantVector(const ConstantVector *CP) { 00954 const VectorType *PTy = CP->getType(); 00955 00956 for (unsigned I = 0, E = PTy->getNumElements(); I < E; ++I) 00957 EmitGlobalConstant(CP->getOperand(I)); 00958 } 00959 00960 void AsmPrinter::EmitGlobalConstantStruct(const ConstantStruct *CVS, 00961 unsigned AddrSpace) { 00962 // Print the fields in successive locations. Pad to align if needed! 00963 const TargetData *TD = TM.getTargetData(); 00964 unsigned Size = TD->getTypeAllocSize(CVS->getType()); 00965 const StructLayout *cvsLayout = TD->getStructLayout(CVS->getType()); 00966 uint64_t sizeSoFar = 0; 00967 for (unsigned i = 0, e = CVS->getNumOperands(); i != e; ++i) { 00968 const Constant* field = CVS->getOperand(i); 00969 00970 // Check if padding is needed and insert one or more 0s. 00971 uint64_t fieldSize = TD->getTypeAllocSize(field->getType()); 00972 uint64_t padSize = ((i == e-1 ? Size : cvsLayout->getElementOffset(i+1)) 00973 - cvsLayout->getElementOffset(i)) - fieldSize; 00974 sizeSoFar += fieldSize + padSize; 00975 00976 // Now print the actual field value. 00977 EmitGlobalConstant(field, AddrSpace); 00978 00979 // Insert padding - this may include padding to increase the size of the 00980 // current field up to the ABI size (if the struct is not packed) as well 00981 // as padding to ensure that the next field starts at the right offset. 00982 EmitZeros(padSize, AddrSpace); 00983 } 00984 assert(sizeSoFar == cvsLayout->getSizeInBytes() && 00985 "Layout of constant struct may be incorrect!"); 00986 } 00987 00988 void AsmPrinter::EmitGlobalConstantFP(const ConstantFP *CFP, 00989 unsigned AddrSpace) { 00990 // FP Constants are printed as integer constants to avoid losing 00991 // precision... 00992 LLVMContext &Context = CFP->getContext(); 00993 const TargetData *TD = TM.getTargetData(); 00994 if (CFP->getType() == Type::getDoubleTy(Context)) { 00995 double Val = CFP->getValueAPF().convertToDouble(); // for comment only 00996 uint64_t i = CFP->getValueAPF().bitcastToAPInt().getZExtValue(); 00997 if (TAI->getData64bitsDirective(AddrSpace)) { 00998 O << TAI->getData64bitsDirective(AddrSpace) << i; 00999 if (VerboseAsm) { 01000 O.PadToColumn(TAI->getCommentColumn()); 01001 O << TAI->getCommentString() << " double " << Val; 01002 } 01003 O << '\n'; 01004 } else if (TD->isBigEndian()) { 01005 O << TAI->getData32bitsDirective(AddrSpace) << unsigned(i >> 32); 01006 if (VerboseAsm) { 01007 O.PadToColumn(TAI->getCommentColumn()); 01008 O << TAI->getCommentString() 01009 << " most significant word of double " << Val; 01010 } 01011 O << '\n'; 01012 O << TAI->getData32bitsDirective(AddrSpace) << unsigned(i); 01013 if (VerboseAsm) { 01014 O.PadToColumn(TAI->getCommentColumn()); 01015 O << TAI->getCommentString() 01016 << " least significant word of double " << Val; 01017 } 01018 O << '\n'; 01019 } else { 01020 O << TAI->getData32bitsDirective(AddrSpace) << unsigned(i); 01021 if (VerboseAsm) { 01022 O.PadToColumn(TAI->getCommentColumn()); 01023 O << TAI->getCommentString() 01024 << " least significant word of double " << Val; 01025 } 01026 O << '\n'; 01027 O << TAI->getData32bitsDirective(AddrSpace) << unsigned(i >> 32); 01028 if (VerboseAsm) { 01029 O.PadToColumn(TAI->getCommentColumn()); 01030 O << TAI->getCommentString() 01031 << " most significant word of double " << Val; 01032 } 01033 O << '\n'; 01034 } 01035 return; 01036 } else if (CFP->getType() == Type::getFloatTy(Context)) { 01037 float Val = CFP->getValueAPF().convertToFloat(); // for comment only 01038 O << TAI->getData32bitsDirective(AddrSpace) 01039 << CFP->getValueAPF().bitcastToAPInt().getZExtValue(); 01040 if (VerboseAsm) { 01041 O.PadToColumn(TAI->getCommentColumn()); 01042 O << TAI->getCommentString() << " float " << Val; 01043 } 01044 O << '\n'; 01045 return; 01046 } else if (CFP->getType() == Type::getX86_FP80Ty(Context)) { 01047 // all long double variants are printed as hex 01048 // api needed to prevent premature destruction 01049 APInt api = CFP->getValueAPF().bitcastToAPInt(); 01050 const uint64_t *p = api.getRawData(); 01051 // Convert to double so we can print the approximate val as a comment. 01052 APFloat DoubleVal = CFP->getValueAPF(); 01053 bool ignored; 01054 DoubleVal.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven, 01055 &ignored); 01056 if (TD->isBigEndian()) { 01057 O << TAI->getData16bitsDirective(AddrSpace) << uint16_t(p[1]); 01058 if (VerboseAsm) { 01059 O.PadToColumn(TAI->getCommentColumn()); 01060 O << TAI->getCommentString() 01061 << " most significant halfword of x86_fp80 ~" 01062 << DoubleVal.convertToDouble(); 01063 } 01064 O << '\n'; 01065 O << TAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 48); 01066 if (VerboseAsm) { 01067 O.PadToColumn(TAI->getCommentColumn()); 01068 O << TAI->getCommentString() << " next halfword"; 01069 } 01070 O << '\n'; 01071 O << TAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 32); 01072 if (VerboseAsm) { 01073 O.PadToColumn(TAI->getCommentColumn()); 01074 O << TAI->getCommentString() << " next halfword"; 01075 } 01076 O << '\n'; 01077 O << TAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 16); 01078 if (VerboseAsm) { 01079 O.PadToColumn(TAI->getCommentColumn()); 01080 O << TAI->getCommentString() << " next halfword"; 01081 } 01082 O << '\n'; 01083 O << TAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0]); 01084 if (VerboseAsm) { 01085 O.PadToColumn(TAI->getCommentColumn()); 01086 O << TAI->getCommentString() 01087 << " least significant halfword"; 01088 } 01089 O << '\n'; 01090 } else { 01091 O << TAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0]); 01092 if (VerboseAsm) { 01093 O.PadToColumn(TAI->getCommentColumn()); 01094 O << TAI->getCommentString() 01095 << " least significant halfword of x86_fp80 ~" 01096 << DoubleVal.convertToDouble(); 01097 } 01098 O << '\n'; 01099 O << TAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 16); 01100 if (VerboseAsm) { 01101 O.PadToColumn(TAI->getCommentColumn()); 01102 O << TAI->getCommentString() 01103 << " next halfword"; 01104 } 01105 O << '\n'; 01106 O << TAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 32); 01107 if (VerboseAsm) { 01108 O.PadToColumn(TAI->getCommentColumn()); 01109 O << TAI->getCommentString() 01110 << " next halfword"; 01111 } 01112 O << '\n'; 01113 O << TAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 48); 01114 if (VerboseAsm) { 01115 O.PadToColumn(TAI->getCommentColumn()); 01116 O << TAI->getCommentString() 01117 << " next halfword"; 01118 } 01119 O << '\n'; 01120 O << TAI->getData16bitsDirective(AddrSpace) << uint16_t(p[1]); 01121 if (VerboseAsm) { 01122 O.PadToColumn(TAI->getCommentColumn()); 01123 O << TAI->getCommentString() 01124 << " most significant halfword"; 01125 } 01126 O << '\n'; 01127 } 01128 EmitZeros(TD->getTypeAllocSize(Type::getX86_FP80Ty(Context)) - 01129 TD->getTypeStoreSize(Type::getX86_FP80Ty(Context)), AddrSpace); 01130 return; 01131 } else if (CFP->getType() == Type::getPPC_FP128Ty(Context)) { 01132 // all long double variants are printed as hex 01133 // api needed to prevent premature destruction 01134 APInt api = CFP->getValueAPF().bitcastToAPInt(); 01135 const uint64_t *p = api.getRawData(); 01136 if (TD->isBigEndian()) { 01137 O << TAI->getData32bitsDirective(AddrSpace) << uint32_t(p[0] >> 32); 01138 if (VerboseAsm) { 01139 O.PadToColumn(TAI->getCommentColumn()); 01140 O << TAI->getCommentString() 01141 << " most significant word of ppc_fp128"; 01142 } 01143 O << '\n'; 01144 O << TAI->getData32bitsDirective(AddrSpace) << uint32_t(p[0]); 01145 if (VerboseAsm) { 01146 O.PadToColumn(TAI->getCommentColumn()); 01147 O << TAI->getCommentString() 01148 << " next word"; 01149 } 01150 O << '\n'; 01151 O << TAI->getData32bitsDirective(AddrSpace) << uint32_t(p[1] >> 32); 01152 if (VerboseAsm) { 01153 O.PadToColumn(TAI->getCommentColumn()); 01154 O << TAI->getCommentString() 01155 << " next word"; 01156 } 01157 O << '\n'; 01158 O << TAI->getData32bitsDirective(AddrSpace) << uint32_t(p[1]); 01159 if (VerboseAsm) { 01160 O.PadToColumn(TAI->getCommentColumn()); 01161 O << TAI->getCommentString() 01162 << " least significant word"; 01163 } 01164 O << '\n'; 01165 } else { 01166 O << TAI->getData32bitsDirective(AddrSpace) << uint32_t(p[1]); 01167 if (VerboseAsm) { 01168 O.PadToColumn(TAI->getCommentColumn()); 01169 O << TAI->getCommentString() 01170 << " least significant word of ppc_fp128"; 01171 } 01172 O << '\n'; 01173 O << TAI->getData32bitsDirective(AddrSpace) << uint32_t(p[1] >> 32); 01174 if (VerboseAsm) { 01175 O.PadToColumn(TAI->getCommentColumn()); 01176 O << TAI->getCommentString() 01177 << " next word"; 01178 } 01179 O << '\n'; 01180 O << TAI->getData32bitsDirective(AddrSpace) << uint32_t(p[0]); 01181 if (VerboseAsm) { 01182 O.PadToColumn(TAI->getCommentColumn()); 01183 O << TAI->getCommentString() 01184 << " next word"; 01185 } 01186 O << '\n'; 01187 O << TAI->getData32bitsDirective(AddrSpace) << uint32_t(p[0] >> 32); 01188 if (VerboseAsm) { 01189 O.PadToColumn(TAI->getCommentColumn()); 01190 O << TAI->getCommentString() 01191 << " most significant word"; 01192 } 01193 O << '\n'; 01194 } 01195 return; 01196 } else llvm_unreachable("Floating point constant type not handled"); 01197 } 01198 01199 void AsmPrinter::EmitGlobalConstantLargeInt(const ConstantInt *CI, 01200 unsigned AddrSpace) { 01201 const TargetData *TD = TM.getTargetData(); 01202 unsigned BitWidth = CI->getBitWidth(); 01203 assert(isPowerOf2_32(BitWidth) && 01204 "Non-power-of-2-sized integers not handled!"); 01205 01206 // We don't expect assemblers to support integer data directives 01207 // for more than 64 bits, so we emit the data in at most 64-bit 01208 // quantities at a time. 01209 const uint64_t *RawData = CI->getValue().getRawData(); 01210 for (unsigned i = 0, e = BitWidth / 64; i != e; ++i) { 01211 uint64_t Val; 01212 if (TD->isBigEndian()) 01213 Val = RawData[e - i - 1]; 01214 else 01215 Val = RawData[i]; 01216 01217 if (TAI->getData64bitsDirective(AddrSpace)) 01218 O << TAI->getData64bitsDirective(AddrSpace) << Val << '\n'; 01219 else if (TD->isBigEndian()) { 01220 O << TAI->getData32bitsDirective(AddrSpace) << unsigned(Val >> 32); 01221 if (VerboseAsm) { 01222 O.PadToColumn(TAI->getCommentColumn()); 01223 O << TAI->getCommentString() 01224 << " most significant half of i64 " << Val; 01225 } 01226 O << '\n'; 01227 O << TAI->getData32bitsDirective(AddrSpace) << unsigned(Val); 01228 if (VerboseAsm) { 01229 O.PadToColumn(TAI->getCommentColumn()); 01230 O << TAI->getCommentString() 01231 << " least significant half of i64 " << Val; 01232 } 01233 O << '\n'; 01234 } else { 01235 O << TAI->getData32bitsDirective(AddrSpace) << unsigned(Val); 01236 if (VerboseAsm) { 01237 O.PadToColumn(TAI->getCommentColumn()); 01238 O << TAI->getCommentString() 01239 << " least significant half of i64 " << Val; 01240 } 01241 O << '\n'; 01242 O << TAI->getData32bitsDirective(AddrSpace) << unsigned(Val >> 32); 01243 if (VerboseAsm) { 01244 O.PadToColumn(TAI->getCommentColumn()); 01245 O << TAI->getCommentString() 01246 << " most significant half of i64 " << Val; 01247 } 01248 O << '\n'; 01249 } 01250 } 01251 } 01252 01253 /// EmitGlobalConstant - Print a general LLVM constant to the .s file. 01254 void AsmPrinter::EmitGlobalConstant(const Constant *CV, unsigned AddrSpace) { 01255 const TargetData *TD = TM.getTargetData(); 01256 const Type *type = CV->getType(); 01257 unsigned Size = TD->getTypeAllocSize(type); 01258 01259 if (CV->isNullValue() || isa<UndefValue>(CV)) { 01260 EmitZeros(Size, AddrSpace); 01261 return; 01262 } else if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) { 01263 EmitGlobalConstantArray(CVA , AddrSpace); 01264 return; 01265 } else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV)) { 01266 EmitGlobalConstantStruct(CVS, AddrSpace); 01267 return; 01268 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) { 01269 EmitGlobalConstantFP(CFP, AddrSpace); 01270 return; 01271 } else if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) { 01272 // Small integers are handled below; large integers are handled here. 01273 if (Size > 4) { 01274 EmitGlobalConstantLargeInt(CI, AddrSpace); 01275 return; 01276 } 01277 } else if (const ConstantVector *CP = dyn_cast<ConstantVector>(CV)) { 01278 EmitGlobalConstantVector(CP); 01279 return; 01280 } 01281 01282 printDataDirective(type, AddrSpace); 01283 EmitConstantValueOnly(CV); 01284 if (VerboseAsm) { 01285 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) { 01286 SmallString<40> S; 01287 CI->getValue().toStringUnsigned(S, 16); 01288 O.PadToColumn(TAI->getCommentColumn()); 01289 O << TAI->getCommentString() << " 0x" << S.str(); 01290 } 01291 } 01292 O << '\n'; 01293 } 01294 01295 void AsmPrinter::EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV) { 01296 // Target doesn't support this yet! 01297 llvm_unreachable("Target does not support EmitMachineConstantPoolValue"); 01298 } 01299 01300 /// PrintSpecial - Print information related to the specified machine instr 01301 /// that is independent of the operand, and may be independent of the instr 01302 /// itself. This can be useful for portably encoding the comment character 01303 /// or other bits of target-specific knowledge into the asmstrings. The 01304 /// syntax used is ${:comment}. Targets can override this to add support 01305 /// for their own strange codes. 01306 void AsmPrinter::PrintSpecial(const MachineInstr *MI, const char *Code) const { 01307 if (!strcmp(Code, "private")) { 01308 O << TAI->getPrivateGlobalPrefix(); 01309 } else if (!strcmp(Code, "comment")) { 01310 if (VerboseAsm) 01311 O << TAI->getCommentString(); 01312 } else if (!strcmp(Code, "uid")) { 01313 // Comparing the address of MI isn't sufficient, because machineinstrs may 01314 // be allocated to the same address across functions. 01315 const Function *ThisF = MI->getParent()->getParent()->getFunction(); 01316 01317 // If this is a new LastFn instruction, bump the counter. 01318 if (LastMI != MI || LastFn != ThisF) { 01319 ++Counter; 01320 LastMI = MI; 01321 LastFn = ThisF; 01322 } 01323 O << Counter; 01324 } else { 01325 std::string msg; 01326 raw_string_ostream Msg(msg); 01327 Msg << "Unknown special formatter '" << Code 01328 << "' for machine instr: " << *MI; 01329 llvm_report_error(Msg.str()); 01330 } 01331 } 01332 01333 /// processDebugLoc - Processes the debug information of each machine 01334 /// instruction's DebugLoc. 01335 void AsmPrinter::processDebugLoc(DebugLoc DL) { 01336 if (!TAI || !DW) 01337 return; 01338 01339 if (TAI->doesSupportDebugInformation() && DW->ShouldEmitDwarfDebug()) { 01340 if (!DL.isUnknown()) { 01341 DebugLocTuple CurDLT = MF->getDebugLocTuple(DL); 01342 01343 if (CurDLT.CompileUnit != 0 && PrevDLT != CurDLT) 01344 printLabel(DW->RecordSourceLine(CurDLT.Line, CurDLT.Col, 01345 DICompileUnit(CurDLT.CompileUnit))); 01346 01347 PrevDLT = CurDLT; 01348 } 01349 } 01350 } 01351 01352 /// printInlineAsm - This method formats and prints the specified machine 01353 /// instruction that is an inline asm. 01354 void AsmPrinter::printInlineAsm(const MachineInstr *MI) const { 01355 unsigned NumOperands = MI->getNumOperands(); 01356 01357 // Count the number of register definitions. 01358 unsigned NumDefs = 0; 01359 for (; MI->getOperand(NumDefs).isReg() && MI->getOperand(NumDefs).isDef(); 01360 ++NumDefs) 01361 assert(NumDefs != NumOperands-1 && "No asm string?"); 01362 01363 assert(MI->getOperand(NumDefs).isSymbol() && "No asm string?"); 01364 01365 // Disassemble the AsmStr, printing out the literal pieces, the operands, etc. 01366 const char *AsmStr = MI->getOperand(NumDefs).getSymbolName(); 01367 01368 // If this asmstr is empty, just print the #APP/#NOAPP markers. 01369 // These are useful to see where empty asm's wound up. 01370 if (AsmStr[0] == 0) { 01371 O << TAI->getCommentString() << TAI->getInlineAsmStart() << "\n\t"; 01372 O << TAI->getCommentString() << TAI->getInlineAsmEnd() << '\n'; 01373 return; 01374 } 01375 01376 O << TAI->getCommentString() << TAI->getInlineAsmStart() << "\n\t"; 01377 01378 // The variant of the current asmprinter. 01379 int AsmPrinterVariant = TAI->getAssemblerDialect(); 01380 01381 int CurVariant = -1; // The number of the {.|.|.} region we are in. 01382 const char *LastEmitted = AsmStr; // One past the last character emitted. 01383 01384 while (*LastEmitted) { 01385 switch (*LastEmitted) { 01386 default: { 01387 // Not a special case, emit the string section literally. 01388 const char *LiteralEnd = LastEmitted+1; 01389 while (*LiteralEnd && *LiteralEnd != '{' && *LiteralEnd != '|' && 01390 *LiteralEnd != '}' && *LiteralEnd != '$' && *LiteralEnd != '\n') 01391 ++LiteralEnd; 01392 if (CurVariant == -1 || CurVariant == AsmPrinterVariant) 01393 O.write(LastEmitted, LiteralEnd-LastEmitted); 01394 LastEmitted = LiteralEnd; 01395 break; 01396 } 01397 case '\n': 01398 ++LastEmitted; // Consume newline character. 01399 O << '\n'; // Indent code with newline. 01400 break; 01401 case '$': { 01402 ++LastEmitted; // Consume '$' character. 01403 bool Done = true; 01404 01405 // Handle escapes. 01406 switch (*LastEmitted) { 01407 default: Done = false; break; 01408 case '$': // $$ -> $ 01409 if (CurVariant == -1 || CurVariant == AsmPrinterVariant) 01410 O << '$'; 01411 ++LastEmitted; // Consume second '$' character. 01412 break; 01413 case '(': // $( -> same as GCC's { character. 01414 ++LastEmitted; // Consume '(' character. 01415 if (CurVariant != -1) { 01416 llvm_report_error("Nested variants found in inline asm string: '" 01417 + std::string(AsmStr) + "'"); 01418 } 01419 CurVariant = 0; // We're in the first variant now. 01420 break; 01421 case '|': 01422 ++LastEmitted; // consume '|' character. 01423 if (CurVariant == -1) 01424 O << '|'; // this is gcc's behavior for | outside a variant 01425 else 01426 ++CurVariant; // We're in the next variant. 01427 break; 01428 case ')': // $) -> same as GCC's } char. 01429 ++LastEmitted; // consume ')' character. 01430 if (CurVariant == -1) 01431 O << '}'; // this is gcc's behavior for } outside a variant 01432 else 01433 CurVariant = -1; 01434 break; 01435 } 01436 if (Done) break; 01437 01438 bool HasCurlyBraces = false; 01439 if (*LastEmitted == '{') { // ${variable} 01440 ++LastEmitted; // Consume '{' character. 01441 HasCurlyBraces = true; 01442 } 01443 01444 // If we have ${:foo}, then this is not a real operand reference, it is a 01445 // "magic" string reference, just like in .td files. Arrange to call 01446 // PrintSpecial. 01447 if (HasCurlyBraces && *LastEmitted == ':') { 01448 ++LastEmitted; 01449 const char *StrStart = LastEmitted; 01450 const char *StrEnd = strchr(StrStart, '}'); 01451 if (StrEnd == 0) { 01452 llvm_report_error("Unterminated ${:foo} operand in inline asm string: '" 01453 + std::string(AsmStr) + "'"); 01454 } 01455 01456 std::string Val(StrStart, StrEnd); 01457 PrintSpecial(MI, Val.c_str()); 01458 LastEmitted = StrEnd+1; 01459 break; 01460 } 01461 01462 const char *IDStart = LastEmitted; 01463 char *IDEnd; 01464 errno = 0; 01465 long Val = strtol(IDStart, &IDEnd, 10); // We only accept numbers for IDs. 01466 if (!isdigit(*IDStart) || (Val == 0 && errno == EINVAL)) { 01467 llvm_report_error("Bad $ operand number in inline asm string: '" 01468 + std::string(AsmStr) + "'"); 01469 } 01470 LastEmitted = IDEnd; 01471 01472 char Modifier[2] = { 0, 0 }; 01473 01474 if (HasCurlyBraces) { 01475 // If we have curly braces, check for a modifier character. This 01476 // supports syntax like ${0:u}, which correspond to "%u0" in GCC asm. 01477 if (*LastEmitted == ':') { 01478 ++LastEmitted; // Consume ':' character. 01479 if (*LastEmitted == 0) { 01480 llvm_report_error("Bad ${:} expression in inline asm string: '" 01481 + std::string(AsmStr) + "'"); 01482 } 01483 01484 Modifier[0] = *LastEmitted; 01485 ++LastEmitted; // Consume modifier character. 01486 } 01487 01488 if (*LastEmitted != '}') { 01489 llvm_report_error("Bad ${} expression in inline asm string: '" 01490 + std::string(AsmStr) + "'"); 01491 } 01492 ++LastEmitted; // Consume '}' character. 01493 } 01494 01495 if ((unsigned)Val >= NumOperands-1) { 01496 llvm_report_error("Invalid $ operand number in inline asm string: '" 01497 + std::string(AsmStr) + "'"); 01498 } 01499 01500 // Okay, we finally have a value number. Ask the target to print this 01501 // operand! 01502 if (CurVariant == -1 || CurVariant == AsmPrinterVariant) { 01503 unsigned OpNo = 1; 01504 01505 bool Error = false; 01506 01507 // Scan to find the machine operand number for the operand. 01508 for (; Val; --Val) { 01509 if (OpNo >= MI->getNumOperands()) break; 01510 unsigned OpFlags = MI->getOperand(OpNo).getImm(); 01511 OpNo += InlineAsm::getNumOperandRegisters(OpFlags) + 1; 01512 } 01513 01514 if (OpNo >= MI->getNumOperands()) { 01515 Error = true; 01516 } else { 01517 unsigned OpFlags = MI->getOperand(OpNo).getImm(); 01518 ++OpNo; // Skip over the ID number. 01519 01520 if (Modifier[0]=='l') // labels are target independent 01521 printBasicBlockLabel(MI->getOperand(OpNo).getMBB(), 01522 false, false, false); 01523 else { 01524 AsmPrinter *AP = const_cast<AsmPrinter*>(this); 01525 if ((OpFlags & 7) == 4) { 01526 Error = AP->PrintAsmMemoryOperand(MI, OpNo, AsmPrinterVariant, 01527 Modifier[0] ? Modifier : 0); 01528 } else { 01529 Error = AP->PrintAsmOperand(MI, OpNo, AsmPrinterVariant, 01530 Modifier[0] ? Modifier : 0); 01531 } 01532 } 01533 } 01534 if (Error) { 01535 std::string msg; 01536 raw_string_ostream Msg(msg); 01537 Msg << "Invalid operand found in inline asm: '" 01538 << AsmStr << "'\n"; 01539 MI->print(Msg); 01540 llvm_report_error(Msg.str()); 01541 } 01542 } 01543 break; 01544 } 01545 } 01546 } 01547 O << "\n\t" << TAI->getCommentString() << TAI->getInlineAsmEnd() << '\n'; 01548 } 01549 01550 /// printImplicitDef - This method prints the specified machine instruction 01551 /// that is an implicit def. 01552 void AsmPrinter::printImplicitDef(const MachineInstr *MI) const { 01553 if (VerboseAsm) { 01554 O.PadToColumn(TAI->getCommentColumn()); 01555 O << TAI->getCommentString() << " implicit-def: " 01556 << TRI->getAsmName(MI->getOperand(0).getReg()) << '\n'; 01557 } 01558 } 01559 01560 /// printLabel - This method prints a local label used by debug and 01561 /// exception handling tables. 01562 void AsmPrinter::printLabel(const MachineInstr *MI) const { 01563 printLabel(MI->getOperand(0).getImm()); 01564 } 01565 01566 void AsmPrinter::printLabel(unsigned Id) const { 01567 O << TAI->getPrivateGlobalPrefix() << "label" << Id << ":\n"; 01568 } 01569 01570 /// printDeclare - This method prints a local variable declaration used by 01571 /// debug tables. 01572 /// FIXME: It doesn't really print anything rather it inserts a DebugVariable 01573 /// entry into dwarf table. 01574 void AsmPrinter::printDeclare(const MachineInstr *MI) const { 01575 unsigned FI = MI->getOperand(0).getIndex(); 01576 GlobalValue *GV = MI->getOperand(1).getGlobal(); 01577 DW->RecordVariable(cast<GlobalVariable>(GV), FI, MI); 01578 } 01579 01580 /// PrintAsmOperand - Print the specified operand of MI, an INLINEASM 01581 /// instruction, using the specified assembler variant. Targets should 01582 /// overried this to format as appropriate. 01583 bool AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo, 01584 unsigned AsmVariant, const char *ExtraCode) { 01585 // Target doesn't support this yet! 01586 return true; 01587 } 01588 01589 bool AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo, 01590 unsigned AsmVariant, 01591 const char *ExtraCode) { 01592 // Target doesn't support this yet! 01593 return true; 01594 } 01595 01596 /// printBasicBlockLabel - This method prints the label for the specified 01597 /// MachineBasicBlock 01598 void AsmPrinter::printBasicBlockLabel(const MachineBasicBlock *MBB, 01599 bool printAlign, 01600 bool printColon, 01601 bool printComment) const { 01602 if (printAlign) { 01603 unsigned Align = MBB->getAlignment(); 01604 if (Align) 01605 EmitAlignment(Log2_32(Align)); 01606 } 01607 01608 O << TAI->getPrivateGlobalPrefix() << "BB" << getFunctionNumber() << '_' 01609 << MBB->getNumber(); 01610 if (printColon) 01611 O << ':'; 01612 if (printComment) { 01613 if (const BasicBlock *BB = MBB->getBasicBlock()) 01614 if (BB->hasName()) { 01615 O.PadToColumn(TAI->getCommentColumn()); 01616 O << TAI->getCommentString() << ' '; 01617 WriteAsOperand(O, BB, /*PrintType=*/false); 01618 } 01619 01620 if (printColon) 01621 EmitComments(*MBB); 01622 } 01623 } 01624 01625 /// printPICJumpTableSetLabel - This method prints a set label for the 01626 /// specified MachineBasicBlock for a jumptable entry. 01627 void AsmPrinter::printPICJumpTableSetLabel(unsigned uid, 01628 const MachineBasicBlock *MBB) const { 01629 if (!TAI->getSetDirective()) 01630 return; 01631 01632 O << TAI->getSetDirective() << ' ' << TAI->getPrivateGlobalPrefix() 01633 << getFunctionNumber() << '_' << uid << "_set_" << MBB->getNumber() << ','; 01634 printBasicBlockLabel(MBB, false, false, false); 01635 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber() 01636 << '_' << uid << '\n'; 01637 } 01638 01639 void AsmPrinter::printPICJumpTableSetLabel(unsigned uid, unsigned uid2, 01640 const MachineBasicBlock *MBB) const { 01641 if (!TAI->getSetDirective()) 01642 return; 01643 01644 O << TAI->getSetDirective() << ' ' << TAI->getPrivateGlobalPrefix() 01645 << getFunctionNumber() << '_' << uid << '_' << uid2 01646 << "_set_" << MBB->getNumber() << ','; 01647 printBasicBlockLabel(MBB, false, false, false); 01648 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber() 01649 << '_' << uid << '_' << uid2 << '\n'; 01650 } 01651 01652 /// printDataDirective - This method prints the asm directive for the 01653 /// specified type. 01654 void AsmPrinter::printDataDirective(const Type *type, unsigned AddrSpace) { 01655 const TargetData *TD = TM.getTargetData(); 01656 switch (type->getTypeID()) { 01657 case Type::FloatTyID: case Type::DoubleTyID: 01658 case Type::X86_FP80TyID: case Type::FP128TyID: case Type::PPC_FP128TyID: 01659 assert(0 && "Should have already output floating point constant."); 01660 default: 01661 assert(0 && "Can't handle printing this type of thing"); 01662 case Type::IntegerTyID: { 01663 unsigned BitWidth = cast<IntegerType>(type)->getBitWidth(); 01664 if (BitWidth <= 8) 01665 O << TAI->getData8bitsDirective(AddrSpace); 01666 else if (BitWidth <= 16) 01667 O << TAI->getData16bitsDirective(AddrSpace); 01668 else if (BitWidth <= 32) 01669 O << TAI->getData32bitsDirective(AddrSpace); 01670 else if (BitWidth <= 64) { 01671 assert(TAI->getData64bitsDirective(AddrSpace) && 01672 "Target cannot handle 64-bit constant exprs!"); 01673 O << TAI->getData64bitsDirective(AddrSpace); 01674 } else { 01675 llvm_unreachable("Target cannot handle given data directive width!"); 01676 } 01677 break; 01678 } 01679 case Type::PointerTyID: 01680 if (TD->getPointerSize() == 8) { 01681 assert(TAI->getData64bitsDirective(AddrSpace) && 01682 "Target cannot handle 64-bit pointer exprs!"); 01683 O << TAI->getData64bitsDirective(AddrSpace); 01684 } else if (TD->getPointerSize() == 2) { 01685 O << TAI->getData16bitsDirective(AddrSpace); 01686 } else if (TD->getPointerSize() == 1) { 01687 O << TAI->getData8bitsDirective(AddrSpace); 01688 } else { 01689 O << TAI->getData32bitsDirective(AddrSpace); 01690 } 01691 break; 01692 } 01693 } 01694 01695 void AsmPrinter::printVisibility(const std::string& Name, 01696 unsigned Visibility) const { 01697 if (Visibility == GlobalValue::HiddenVisibility) { 01698 if (const char *Directive = TAI->getHiddenDirective()) 01699 O << Directive << Name << '\n'; 01700 } else if (Visibility == GlobalValue::ProtectedVisibility) { 01701 if (const char *Directive = TAI->getProtectedDirective()) 01702 O << Directive << Name << '\n'; 01703 } 01704 } 01705 01706 void AsmPrinter::printOffset(int64_t Offset) const { 01707 if (Offset > 0) 01708 O << '+' << Offset; 01709 else if (Offset < 0) 01710 O << Offset; 01711 } 01712 01713 void AsmPrinter::printMCInst(const MCInst *MI) { 01714 llvm_unreachable("MCInst printing unavailable on this target!"); 01715 } 01716 01717 GCMetadataPrinter *AsmPrinter::GetOrCreateGCPrinter(GCStrategy *S) { 01718 if (!S->usesMetadata()) 01719 return 0; 01720 01721 gcp_iterator GCPI = GCMetadataPrinters.find(S); 01722 if (GCPI != GCMetadataPrinters.end()) 01723 return GCPI->second; 01724 01725 const char *Name = S->getName().c_str(); 01726 01727 for (GCMetadataPrinterRegistry::iterator 01728 I = GCMetadataPrinterRegistry::begin(), 01729 E = GCMetadataPrinterRegistry::end(); I != E; ++I) 01730 if (strcmp(Name, I->getName()) == 0) { 01731 GCMetadataPrinter *GMP = I->instantiate(); 01732 GMP->S = S; 01733 GCMetadataPrinters.insert(std::make_pair(S, GMP)); 01734 return GMP; 01735 } 01736 01737 cerr << "no GCMetadataPrinter registered for GC: " << Name << "\n"; 01738 llvm_unreachable(0); 01739 } 01740 01741 /// EmitComments - Pretty-print comments for instructions 01742 void AsmPrinter::EmitComments(const MachineInstr &MI) const { 01743 if (!VerboseAsm || 01744 MI.getDebugLoc().isUnknown()) 01745 return; 01746 01747 DebugLocTuple DLT = MF->getDebugLocTuple(MI.getDebugLoc()); 01748 01749 // Print source line info. 01750 O.PadToColumn(TAI->getCommentColumn()); 01751 O << TAI->getCommentString() << " SrcLine "; 01752 if (DLT.CompileUnit->hasInitializer()) { 01753 Constant *Name = DLT.CompileUnit->getInitializer(); 01754 if (ConstantArray *NameString = dyn_cast<ConstantArray>(Name)) 01755 if (NameString->isString()) 01756 O << NameString->getAsString() << " "; 01757 } 01758 O << DLT.Line; 01759 if (DLT.Col != 0) 01760 O << ":" << DLT.Col; 01761 } 01762 01763 /// EmitComments - Pretty-print comments for instructions 01764 void AsmPrinter::EmitComments(const MCInst &MI) const 01765 { 01766 if (VerboseAsm) { 01767 if (!MI.getDebugLoc().isUnknown()) { 01768 DebugLocTuple DLT = MF->getDebugLocTuple(MI.getDebugLoc()); 01769 01770 // Print source line info 01771 O.PadToColumn(TAI->getCommentColumn()); 01772 O << TAI->getCommentString() << " SrcLine "; 01773 if (DLT.CompileUnit->hasInitializer()) { 01774 Constant *Name = DLT.CompileUnit->getInitializer(); 01775 if (ConstantArray *NameString = dyn_cast<ConstantArray>(Name)) 01776 if (NameString->isString()) { 01777 O << NameString->getAsString() << " "; 01778 } 01779 } 01780 O << DLT.Line; 01781 if (DLT.Col != 0) 01782 O << ":" << DLT.Col; 01783 } 01784 } 01785 } 01786 01787 /// Indent - Insert spaces into the character output stream. The 01788 /// "level" is multiplied by the "scale" to calculate the number of 01789 /// spaces to insert. "level" can represent something like loop 01790 /// nesting level, for example. 01791 /// 01792 static formatted_raw_ostream & 01793 Indent(formatted_raw_ostream &out, int level, int scale = 2) { 01794 for(int i = 0; i < level*scale; ++i) { 01795 out << " "; 01796 } 01797 return out; 01798 } 01799 01800 /// PrintChildLoopComment - Print comments about child loops within 01801 /// the loop for this basic block, with nesting. 01802 /// 01803 static void PrintChildLoopComment(formatted_raw_ostream &O, 01804 const MachineLoop *loop, 01805 const TargetAsmInfo *TAI, 01806 int FunctionNumber) { 01807 // Add child loop information 01808 for(MachineLoop::iterator cl = loop->begin(), 01809 clend = loop->end(); 01810 cl != clend; 01811 ++cl) { 01812 MachineBasicBlock *Header = (*cl)->getHeader(); 01813 assert(Header && "No header for loop"); 01814 01815 O << '\n'; 01816 O.PadToColumn(TAI->getCommentColumn()); 01817 01818 O << TAI->getCommentString(); 01819 Indent(O, (*cl)->getLoopDepth()-1) 01820 << " Child Loop BB" << FunctionNumber << "_" 01821 << Header->getNumber() << " Depth " << (*cl)->getLoopDepth(); 01822 01823 PrintChildLoopComment(O, *cl, TAI, FunctionNumber); 01824 } 01825 } 01826 01827 /// EmitComments - Pretty-print comments for basic blocks 01828 void AsmPrinter::EmitComments(const MachineBasicBlock &MBB) const 01829 { 01830 if (VerboseAsm) { 01831 // Add loop depth information 01832 const MachineLoop *loop = LI->getLoopFor(&MBB); 01833 01834 if (loop) { 01835 // Print a newline after bb# annotation. 01836 O << "\n"; 01837 O.PadToColumn(TAI->getCommentColumn()); 01838 O << TAI->getCommentString() << " Loop Depth " << loop->getLoopDepth() 01839 << '\n'; 01840 01841 O.PadToColumn(TAI->getCommentColumn()); 01842 01843 MachineBasicBlock *Header = loop->getHeader(); 01844 assert(Header && "No header for loop"); 01845 01846 if (Header == &MBB) { 01847 O << TAI->getCommentString() << " Loop Header"; 01848 PrintChildLoopComment(O, loop, TAI, getFunctionNumber()); 01849 } 01850 else { 01851 O << TAI->getCommentString() << " Loop Header is BB" 01852 << getFunctionNumber() << "_" << loop->getHeader()->getNumber(); 01853 } 01854 01855 if (loop->empty()) { 01856 O << '\n'; 01857 O.PadToColumn(TAI->getCommentColumn()); 01858 O << TAI->getCommentString() << " Inner Loop"; 01859 } 01860 01861 // Add parent loop information 01862 for (const MachineLoop *CurLoop = loop->getParentLoop(); 01863 CurLoop; 01864 CurLoop = CurLoop->getParentLoop()) { 01865 MachineBasicBlock *Header = CurLoop->getHeader(); 01866 assert(Header && "No header for loop"); 01867 01868 O << '\n'; 01869 O.PadToColumn(TAI->getCommentColumn()); 01870 O << TAI->getCommentString(); 01871 Indent(O, CurLoop->getLoopDepth()-1) 01872 << " Inside Loop BB" << getFunctionNumber() << "_" 01873 << Header->getNumber() << " Depth " << CurLoop->getLoopDepth(); 01874 } 01875 } 01876 } 01877 }