AsmPrinter.cpp revision 75bdd29b81a79ff8dd3a6d6629cb7d42b48da45d
1//===-- AsmPrinter.cpp - Common AsmPrinter code ---------------------------===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9// 10// This file implements the AsmPrinter class. 11// 12//===----------------------------------------------------------------------===// 13 14#include "llvm/CodeGen/AsmPrinter.h" 15#include "llvm/Assembly/Writer.h" 16#include "llvm/DerivedTypes.h" 17#include "llvm/Constants.h" 18#include "llvm/Module.h" 19#include "llvm/CodeGen/GCMetadataPrinter.h" 20#include "llvm/CodeGen/MachineConstantPool.h" 21#include "llvm/CodeGen/MachineJumpTableInfo.h" 22#include "llvm/CodeGen/MachineModuleInfo.h" 23#include "llvm/CodeGen/DwarfWriter.h" 24#include "llvm/Analysis/DebugInfo.h" 25#include "llvm/MC/MCContext.h" 26#include "llvm/MC/MCInst.h" 27#include "llvm/MC/MCSection.h" 28#include "llvm/MC/MCStreamer.h" 29#include "llvm/Support/CommandLine.h" 30#include "llvm/Support/ErrorHandling.h" 31#include "llvm/Support/FormattedStream.h" 32#include "llvm/Support/Mangler.h" 33#include "llvm/Target/TargetAsmInfo.h" 34#include "llvm/Target/TargetData.h" 35#include "llvm/Target/TargetLowering.h" 36#include "llvm/Target/TargetLoweringObjectFile.h" 37#include "llvm/Target/TargetOptions.h" 38#include "llvm/Target/TargetRegisterInfo.h" 39#include "llvm/ADT/SmallPtrSet.h" 40#include "llvm/ADT/SmallString.h" 41#include "llvm/ADT/StringExtras.h" 42#include <cerrno> 43using namespace llvm; 44 45static cl::opt<cl::boolOrDefault> 46AsmVerbose("asm-verbose", cl::desc("Add comments to directives."), 47 cl::init(cl::BOU_UNSET)); 48 49char AsmPrinter::ID = 0; 50AsmPrinter::AsmPrinter(formatted_raw_ostream &o, TargetMachine &tm, 51 const TargetAsmInfo *T, bool VDef) 52 : MachineFunctionPass(&ID), FunctionNumber(0), O(o), 53 TM(tm), TAI(T), TRI(tm.getRegisterInfo()), 54 55 OutContext(*new MCContext()), 56 OutStreamer(*createAsmStreamer(OutContext, O)), 57 58 IsInTextSection(false), LastMI(0), LastFn(0), Counter(~0U), 59 PrevDLT(0, ~0U, ~0U) { 60 DW = 0; MMI = 0; 61 switch (AsmVerbose) { 62 case cl::BOU_UNSET: VerboseAsm = VDef; break; 63 case cl::BOU_TRUE: VerboseAsm = true; break; 64 case cl::BOU_FALSE: VerboseAsm = false; break; 65 } 66} 67 68AsmPrinter::~AsmPrinter() { 69 for (gcp_iterator I = GCMetadataPrinters.begin(), 70 E = GCMetadataPrinters.end(); I != E; ++I) 71 delete I->second; 72 73 delete &OutStreamer; 74 delete &OutContext; 75} 76 77TargetLoweringObjectFile &AsmPrinter::getObjFileLowering() const { 78 return TM.getTargetLowering()->getObjFileLowering(); 79} 80 81 82/// SwitchToTextSection - Switch to the specified text section of the executable 83/// if we are not already in it! 84/// 85void AsmPrinter::SwitchToTextSection(const char *NewSection, 86 const GlobalValue *GV) { 87 std::string NS; 88 if (GV && GV->hasSection()) 89 NS = TAI->getSwitchToSectionDirective() + GV->getSection(); 90 else 91 NS = NewSection; 92 93 // If we're already in this section, we're done. 94 if (CurrentSection == NS) return; 95 96 // Close the current section, if applicable. 97 if (TAI->getSectionEndDirectiveSuffix() && !CurrentSection.empty()) 98 O << CurrentSection << TAI->getSectionEndDirectiveSuffix() << '\n'; 99 100 CurrentSection = NS; 101 102 if (!CurrentSection.empty()) 103 O << CurrentSection << TAI->getTextSectionStartSuffix() << '\n'; 104 105 IsInTextSection = true; 106} 107 108/// SwitchToDataSection - Switch to the specified data section of the executable 109/// if we are not already in it! 110/// 111void AsmPrinter::SwitchToDataSection(const char *NewSection, 112 const GlobalValue *GV) { 113 std::string NS; 114 if (GV && GV->hasSection()) 115 NS = TAI->getSwitchToSectionDirective() + GV->getSection(); 116 else 117 NS = NewSection; 118 119 // If we're already in this section, we're done. 120 if (CurrentSection == NS) return; 121 122 // Close the current section, if applicable. 123 if (TAI->getSectionEndDirectiveSuffix() && !CurrentSection.empty()) 124 O << CurrentSection << TAI->getSectionEndDirectiveSuffix() << '\n'; 125 126 CurrentSection = NS; 127 128 if (!CurrentSection.empty()) 129 O << CurrentSection << TAI->getDataSectionStartSuffix() << '\n'; 130 131 IsInTextSection = false; 132} 133 134/// SwitchToSection - Switch to the specified section of the executable if we 135/// are not already in it! If "NS" is null, then this causes us to exit the 136/// current section and not reenter another one. This is generally used for 137/// asmprinter hacks. 138/// 139/// FIXME: Remove support for null sections. 140/// 141void AsmPrinter::SwitchToSection(const MCSection *NS) { 142 const std::string &NewSection = NS ? NS->getName() : ""; 143 144 // If we're already in this section, we're done. 145 if (CurrentSection == NewSection) return; 146 147 // Close the current section, if applicable. 148 if (TAI->getSectionEndDirectiveSuffix() && !CurrentSection.empty()) 149 O << CurrentSection << TAI->getSectionEndDirectiveSuffix() << '\n'; 150 151 // FIXME: Make CurrentSection a Section* in the future 152 CurrentSection = NewSection; 153 CurrentSection_ = NS; 154 155 if (!CurrentSection.empty()) { 156 // If section is named we need to switch into it via special '.section' 157 // directive and also append funky flags. Otherwise - section name is just 158 // some magic assembler directive. 159 if (!NS->isDirective()) { 160 SmallString<32> FlagsStr; 161 162 getObjFileLowering().getSectionFlagsAsString(NS->getKind(), FlagsStr); 163 164 O << TAI->getSwitchToSectionDirective() 165 << CurrentSection 166 << FlagsStr.c_str(); 167 } else { 168 O << CurrentSection; 169 } 170 O << TAI->getDataSectionStartSuffix() << '\n'; 171 } 172 173 IsInTextSection = NS ? NS->getKind().isText() : false; 174} 175 176void AsmPrinter::getAnalysisUsage(AnalysisUsage &AU) const { 177 AU.setPreservesAll(); 178 MachineFunctionPass::getAnalysisUsage(AU); 179 AU.addRequired<GCModuleInfo>(); 180} 181 182bool AsmPrinter::doInitialization(Module &M) { 183 // Initialize TargetLoweringObjectFile. 184 const_cast<TargetLoweringObjectFile&>(getObjFileLowering()) 185 .Initialize(OutContext, TM); 186 187 Mang = new Mangler(M, TAI->getGlobalPrefix(), TAI->getPrivateGlobalPrefix(), 188 TAI->getLinkerPrivateGlobalPrefix()); 189 190 if (TAI->doesAllowQuotesInName()) 191 Mang->setUseQuotes(true); 192 193 GCModuleInfo *MI = getAnalysisIfAvailable<GCModuleInfo>(); 194 assert(MI && "AsmPrinter didn't require GCModuleInfo?"); 195 196 if (TAI->hasSingleParameterDotFile()) { 197 /* Very minimal debug info. It is ignored if we emit actual 198 debug info. If we don't, this at helps the user find where 199 a function came from. */ 200 O << "\t.file\t\"" << M.getModuleIdentifier() << "\"\n"; 201 } 202 203 for (GCModuleInfo::iterator I = MI->begin(), E = MI->end(); I != E; ++I) 204 if (GCMetadataPrinter *MP = GetOrCreateGCPrinter(*I)) 205 MP->beginAssembly(O, *this, *TAI); 206 207 if (!M.getModuleInlineAsm().empty()) 208 O << TAI->getCommentString() << " Start of file scope inline assembly\n" 209 << M.getModuleInlineAsm() 210 << '\n' << TAI->getCommentString() 211 << " End of file scope inline assembly\n"; 212 213 SwitchToDataSection(""); // Reset back to no section. 214 215 if (TAI->doesSupportDebugInformation() || 216 TAI->doesSupportExceptionHandling()) { 217 MMI = getAnalysisIfAvailable<MachineModuleInfo>(); 218 if (MMI) 219 MMI->AnalyzeModule(M); 220 DW = getAnalysisIfAvailable<DwarfWriter>(); 221 if (DW) 222 DW->BeginModule(&M, MMI, O, this, TAI); 223 } 224 225 return false; 226} 227 228bool AsmPrinter::doFinalization(Module &M) { 229 // Emit global variables. 230 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end(); 231 I != E; ++I) 232 PrintGlobalVariable(I); 233 234 // Emit final debug information. 235 if (TAI->doesSupportDebugInformation() || TAI->doesSupportExceptionHandling()) 236 DW->EndModule(); 237 238 // If the target wants to know about weak references, print them all. 239 if (TAI->getWeakRefDirective()) { 240 // FIXME: This is not lazy, it would be nice to only print weak references 241 // to stuff that is actually used. Note that doing so would require targets 242 // to notice uses in operands (due to constant exprs etc). This should 243 // happen with the MC stuff eventually. 244 SwitchToDataSection(""); 245 246 // Print out module-level global variables here. 247 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end(); 248 I != E; ++I) { 249 if (I->hasExternalWeakLinkage()) 250 O << TAI->getWeakRefDirective() << Mang->getMangledName(I) << '\n'; 251 } 252 253 for (Module::const_iterator I = M.begin(), E = M.end(); 254 I != E; ++I) { 255 if (I->hasExternalWeakLinkage()) 256 O << TAI->getWeakRefDirective() << Mang->getMangledName(I) << '\n'; 257 } 258 } 259 260 if (TAI->getSetDirective()) { 261 O << '\n'; 262 for (Module::const_alias_iterator I = M.alias_begin(), E = M.alias_end(); 263 I != E; ++I) { 264 std::string Name = Mang->getMangledName(I); 265 266 const GlobalValue *GV = cast<GlobalValue>(I->getAliasedGlobal()); 267 std::string Target = Mang->getMangledName(GV); 268 269 if (I->hasExternalLinkage() || !TAI->getWeakRefDirective()) 270 O << "\t.globl\t" << Name << '\n'; 271 else if (I->hasWeakLinkage()) 272 O << TAI->getWeakRefDirective() << Name << '\n'; 273 else if (!I->hasLocalLinkage()) 274 llvm_unreachable("Invalid alias linkage"); 275 276 printVisibility(Name, I->getVisibility()); 277 278 O << TAI->getSetDirective() << ' ' << Name << ", " << Target << '\n'; 279 } 280 } 281 282 GCModuleInfo *MI = getAnalysisIfAvailable<GCModuleInfo>(); 283 assert(MI && "AsmPrinter didn't require GCModuleInfo?"); 284 for (GCModuleInfo::iterator I = MI->end(), E = MI->begin(); I != E; ) 285 if (GCMetadataPrinter *MP = GetOrCreateGCPrinter(*--I)) 286 MP->finishAssembly(O, *this, *TAI); 287 288 // If we don't have any trampolines, then we don't require stack memory 289 // to be executable. Some targets have a directive to declare this. 290 Function *InitTrampolineIntrinsic = M.getFunction("llvm.init.trampoline"); 291 if (!InitTrampolineIntrinsic || InitTrampolineIntrinsic->use_empty()) 292 if (TAI->getNonexecutableStackDirective()) 293 O << TAI->getNonexecutableStackDirective() << '\n'; 294 295 delete Mang; Mang = 0; 296 DW = 0; MMI = 0; 297 298 OutStreamer.Finish(); 299 return false; 300} 301 302std::string 303AsmPrinter::getCurrentFunctionEHName(const MachineFunction *MF) const { 304 assert(MF && "No machine function?"); 305 return Mang->getMangledName(MF->getFunction(), ".eh", 306 TAI->is_EHSymbolPrivate()); 307} 308 309void AsmPrinter::SetupMachineFunction(MachineFunction &MF) { 310 // What's my mangled name? 311 CurrentFnName = Mang->getMangledName(MF.getFunction()); 312 IncrementFunctionNumber(); 313} 314 315namespace { 316 // SectionCPs - Keep track the alignment, constpool entries per Section. 317 struct SectionCPs { 318 const MCSection *S; 319 unsigned Alignment; 320 SmallVector<unsigned, 4> CPEs; 321 SectionCPs(const MCSection *s, unsigned a) : S(s), Alignment(a) {}; 322 }; 323} 324 325/// EmitConstantPool - Print to the current output stream assembly 326/// representations of the constants in the constant pool MCP. This is 327/// used to print out constants which have been "spilled to memory" by 328/// the code generator. 329/// 330void AsmPrinter::EmitConstantPool(MachineConstantPool *MCP) { 331 const std::vector<MachineConstantPoolEntry> &CP = MCP->getConstants(); 332 if (CP.empty()) return; 333 334 // Calculate sections for constant pool entries. We collect entries to go into 335 // the same section together to reduce amount of section switch statements. 336 SmallVector<SectionCPs, 4> CPSections; 337 for (unsigned i = 0, e = CP.size(); i != e; ++i) { 338 const MachineConstantPoolEntry &CPE = CP[i]; 339 unsigned Align = CPE.getAlignment(); 340 341 SectionKind Kind; 342 switch (CPE.getRelocationInfo()) { 343 default: llvm_unreachable("Unknown section kind"); 344 case 2: Kind = SectionKind::getReadOnlyWithRel(); break; 345 case 1: 346 Kind = SectionKind::getReadOnlyWithRelLocal(); 347 break; 348 case 0: 349 switch (TM.getTargetData()->getTypeAllocSize(CPE.getType())) { 350 case 4: Kind = SectionKind::getMergeableConst4(); break; 351 case 8: Kind = SectionKind::getMergeableConst8(); break; 352 case 16: Kind = SectionKind::getMergeableConst16();break; 353 default: Kind = SectionKind::getMergeableConst(); break; 354 } 355 } 356 357 const MCSection *S = getObjFileLowering().getSectionForConstant(Kind); 358 359 // The number of sections are small, just do a linear search from the 360 // last section to the first. 361 bool Found = false; 362 unsigned SecIdx = CPSections.size(); 363 while (SecIdx != 0) { 364 if (CPSections[--SecIdx].S == S) { 365 Found = true; 366 break; 367 } 368 } 369 if (!Found) { 370 SecIdx = CPSections.size(); 371 CPSections.push_back(SectionCPs(S, Align)); 372 } 373 374 if (Align > CPSections[SecIdx].Alignment) 375 CPSections[SecIdx].Alignment = Align; 376 CPSections[SecIdx].CPEs.push_back(i); 377 } 378 379 // Now print stuff into the calculated sections. 380 for (unsigned i = 0, e = CPSections.size(); i != e; ++i) { 381 SwitchToSection(CPSections[i].S); 382 EmitAlignment(Log2_32(CPSections[i].Alignment)); 383 384 unsigned Offset = 0; 385 for (unsigned j = 0, ee = CPSections[i].CPEs.size(); j != ee; ++j) { 386 unsigned CPI = CPSections[i].CPEs[j]; 387 MachineConstantPoolEntry CPE = CP[CPI]; 388 389 // Emit inter-object padding for alignment. 390 unsigned AlignMask = CPE.getAlignment() - 1; 391 unsigned NewOffset = (Offset + AlignMask) & ~AlignMask; 392 EmitZeros(NewOffset - Offset); 393 394 const Type *Ty = CPE.getType(); 395 Offset = NewOffset + TM.getTargetData()->getTypeAllocSize(Ty); 396 397 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << '_' 398 << CPI << ":\t\t\t\t\t"; 399 if (VerboseAsm) { 400 O << TAI->getCommentString() << ' '; 401 WriteTypeSymbolic(O, CPE.getType(), 0); 402 } 403 O << '\n'; 404 if (CPE.isMachineConstantPoolEntry()) 405 EmitMachineConstantPoolValue(CPE.Val.MachineCPVal); 406 else 407 EmitGlobalConstant(CPE.Val.ConstVal); 408 } 409 } 410} 411 412/// EmitJumpTableInfo - Print assembly representations of the jump tables used 413/// by the current function to the current output stream. 414/// 415void AsmPrinter::EmitJumpTableInfo(MachineJumpTableInfo *MJTI, 416 MachineFunction &MF) { 417 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables(); 418 if (JT.empty()) return; 419 420 bool IsPic = TM.getRelocationModel() == Reloc::PIC_; 421 422 // Pick the directive to use to print the jump table entries, and switch to 423 // the appropriate section. 424 TargetLowering *LoweringInfo = TM.getTargetLowering(); 425 426 const Function *F = MF.getFunction(); 427 bool JTInDiffSection = false; 428 if (F->isWeakForLinker() || 429 (IsPic && !LoweringInfo->usesGlobalOffsetTable())) { 430 // In PIC mode, we need to emit the jump table to the same section as the 431 // function body itself, otherwise the label differences won't make sense. 432 // We should also do if the section name is NULL or function is declared in 433 // discardable section. 434 SwitchToSection(getObjFileLowering().SectionForGlobal(F, Mang, TM)); 435 } else { 436 // Otherwise, drop it in the readonly section. 437 const MCSection *ReadOnlySection = 438 getObjFileLowering().getSectionForConstant(SectionKind::getReadOnly()); 439 SwitchToSection(ReadOnlySection); 440 JTInDiffSection = true; 441 } 442 443 EmitAlignment(Log2_32(MJTI->getAlignment())); 444 445 for (unsigned i = 0, e = JT.size(); i != e; ++i) { 446 const std::vector<MachineBasicBlock*> &JTBBs = JT[i].MBBs; 447 448 // If this jump table was deleted, ignore it. 449 if (JTBBs.empty()) continue; 450 451 // For PIC codegen, if possible we want to use the SetDirective to reduce 452 // the number of relocations the assembler will generate for the jump table. 453 // Set directives are all printed before the jump table itself. 454 SmallPtrSet<MachineBasicBlock*, 16> EmittedSets; 455 if (TAI->getSetDirective() && IsPic) 456 for (unsigned ii = 0, ee = JTBBs.size(); ii != ee; ++ii) 457 if (EmittedSets.insert(JTBBs[ii])) 458 printPICJumpTableSetLabel(i, JTBBs[ii]); 459 460 // On some targets (e.g. darwin) we want to emit two consequtive labels 461 // before each jump table. The first label is never referenced, but tells 462 // the assembler and linker the extents of the jump table object. The 463 // second label is actually referenced by the code. 464 if (JTInDiffSection) { 465 if (const char *JTLabelPrefix = TAI->getJumpTableSpecialLabelPrefix()) 466 O << JTLabelPrefix << "JTI" << getFunctionNumber() << '_' << i << ":\n"; 467 } 468 469 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber() 470 << '_' << i << ":\n"; 471 472 for (unsigned ii = 0, ee = JTBBs.size(); ii != ee; ++ii) { 473 printPICJumpTableEntry(MJTI, JTBBs[ii], i); 474 O << '\n'; 475 } 476 } 477} 478 479void AsmPrinter::printPICJumpTableEntry(const MachineJumpTableInfo *MJTI, 480 const MachineBasicBlock *MBB, 481 unsigned uid) const { 482 bool IsPic = TM.getRelocationModel() == Reloc::PIC_; 483 484 // Use JumpTableDirective otherwise honor the entry size from the jump table 485 // info. 486 const char *JTEntryDirective = TAI->getJumpTableDirective(); 487 bool HadJTEntryDirective = JTEntryDirective != NULL; 488 if (!HadJTEntryDirective) { 489 JTEntryDirective = MJTI->getEntrySize() == 4 ? 490 TAI->getData32bitsDirective() : TAI->getData64bitsDirective(); 491 } 492 493 O << JTEntryDirective << ' '; 494 495 // If we have emitted set directives for the jump table entries, print 496 // them rather than the entries themselves. If we're emitting PIC, then 497 // emit the table entries as differences between two text section labels. 498 // If we're emitting non-PIC code, then emit the entries as direct 499 // references to the target basic blocks. 500 if (IsPic) { 501 if (TAI->getSetDirective()) { 502 O << TAI->getPrivateGlobalPrefix() << getFunctionNumber() 503 << '_' << uid << "_set_" << MBB->getNumber(); 504 } else { 505 printBasicBlockLabel(MBB, false, false, false); 506 // If the arch uses custom Jump Table directives, don't calc relative to 507 // JT 508 if (!HadJTEntryDirective) 509 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI" 510 << getFunctionNumber() << '_' << uid; 511 } 512 } else { 513 printBasicBlockLabel(MBB, false, false, false); 514 } 515} 516 517 518/// EmitSpecialLLVMGlobal - Check to see if the specified global is a 519/// special global used by LLVM. If so, emit it and return true, otherwise 520/// do nothing and return false. 521bool AsmPrinter::EmitSpecialLLVMGlobal(const GlobalVariable *GV) { 522 if (GV->getName() == "llvm.used") { 523 if (TAI->getUsedDirective() != 0) // No need to emit this at all. 524 EmitLLVMUsedList(GV->getInitializer()); 525 return true; 526 } 527 528 // Ignore debug and non-emitted data. This handles llvm.compiler.used. 529 if (GV->getSection() == "llvm.metadata" || 530 GV->hasAvailableExternallyLinkage()) 531 return true; 532 533 if (!GV->hasAppendingLinkage()) return false; 534 535 assert(GV->hasInitializer() && "Not a special LLVM global!"); 536 537 const TargetData *TD = TM.getTargetData(); 538 unsigned Align = Log2_32(TD->getPointerPrefAlignment()); 539 if (GV->getName() == "llvm.global_ctors") { 540 SwitchToSection(getObjFileLowering().getStaticCtorSection()); 541 EmitAlignment(Align, 0); 542 EmitXXStructorList(GV->getInitializer()); 543 return true; 544 } 545 546 if (GV->getName() == "llvm.global_dtors") { 547 SwitchToSection(getObjFileLowering().getStaticDtorSection()); 548 EmitAlignment(Align, 0); 549 EmitXXStructorList(GV->getInitializer()); 550 return true; 551 } 552 553 return false; 554} 555 556/// EmitLLVMUsedList - For targets that define a TAI::UsedDirective, mark each 557/// global in the specified llvm.used list for which emitUsedDirectiveFor 558/// is true, as being used with this directive. 559void AsmPrinter::EmitLLVMUsedList(Constant *List) { 560 const char *Directive = TAI->getUsedDirective(); 561 562 // Should be an array of 'i8*'. 563 ConstantArray *InitList = dyn_cast<ConstantArray>(List); 564 if (InitList == 0) return; 565 566 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) { 567 const GlobalValue *GV = 568 dyn_cast<GlobalValue>(InitList->getOperand(i)->stripPointerCasts()); 569 if (GV && getObjFileLowering().shouldEmitUsedDirectiveFor(GV, Mang)) { 570 O << Directive; 571 EmitConstantValueOnly(InitList->getOperand(i)); 572 O << '\n'; 573 } 574 } 575} 576 577/// EmitXXStructorList - Emit the ctor or dtor list. This just prints out the 578/// function pointers, ignoring the init priority. 579void AsmPrinter::EmitXXStructorList(Constant *List) { 580 // Should be an array of '{ int, void ()* }' structs. The first value is the 581 // init priority, which we ignore. 582 if (!isa<ConstantArray>(List)) return; 583 ConstantArray *InitList = cast<ConstantArray>(List); 584 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) 585 if (ConstantStruct *CS = dyn_cast<ConstantStruct>(InitList->getOperand(i))){ 586 if (CS->getNumOperands() != 2) return; // Not array of 2-element structs. 587 588 if (CS->getOperand(1)->isNullValue()) 589 return; // Found a null terminator, exit printing. 590 // Emit the function pointer. 591 EmitGlobalConstant(CS->getOperand(1)); 592 } 593} 594 595/// getGlobalLinkName - Returns the asm/link name of of the specified 596/// global variable. Should be overridden by each target asm printer to 597/// generate the appropriate value. 598const std::string &AsmPrinter::getGlobalLinkName(const GlobalVariable *GV, 599 std::string &LinkName) const { 600 if (isa<Function>(GV)) { 601 LinkName += TAI->getFunctionAddrPrefix(); 602 LinkName += Mang->getMangledName(GV); 603 LinkName += TAI->getFunctionAddrSuffix(); 604 } else { 605 LinkName += TAI->getGlobalVarAddrPrefix(); 606 LinkName += Mang->getMangledName(GV); 607 LinkName += TAI->getGlobalVarAddrSuffix(); 608 } 609 610 return LinkName; 611} 612 613/// EmitExternalGlobal - Emit the external reference to a global variable. 614/// Should be overridden if an indirect reference should be used. 615void AsmPrinter::EmitExternalGlobal(const GlobalVariable *GV) { 616 std::string GLN; 617 O << getGlobalLinkName(GV, GLN); 618} 619 620 621 622//===----------------------------------------------------------------------===// 623/// LEB 128 number encoding. 624 625/// PrintULEB128 - Print a series of hexidecimal values (separated by commas) 626/// representing an unsigned leb128 value. 627void AsmPrinter::PrintULEB128(unsigned Value) const { 628 char Buffer[20]; 629 do { 630 unsigned char Byte = static_cast<unsigned char>(Value & 0x7f); 631 Value >>= 7; 632 if (Value) Byte |= 0x80; 633 O << "0x" << utohex_buffer(Byte, Buffer+20); 634 if (Value) O << ", "; 635 } while (Value); 636} 637 638/// PrintSLEB128 - Print a series of hexidecimal values (separated by commas) 639/// representing a signed leb128 value. 640void AsmPrinter::PrintSLEB128(int Value) const { 641 int Sign = Value >> (8 * sizeof(Value) - 1); 642 bool IsMore; 643 char Buffer[20]; 644 645 do { 646 unsigned char Byte = static_cast<unsigned char>(Value & 0x7f); 647 Value >>= 7; 648 IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0; 649 if (IsMore) Byte |= 0x80; 650 O << "0x" << utohex_buffer(Byte, Buffer+20); 651 if (IsMore) O << ", "; 652 } while (IsMore); 653} 654 655//===--------------------------------------------------------------------===// 656// Emission and print routines 657// 658 659/// PrintHex - Print a value as a hexidecimal value. 660/// 661void AsmPrinter::PrintHex(int Value) const { 662 char Buffer[20]; 663 O << "0x" << utohex_buffer(static_cast<unsigned>(Value), Buffer+20); 664} 665 666/// EOL - Print a newline character to asm stream. If a comment is present 667/// then it will be printed first. Comments should not contain '\n'. 668void AsmPrinter::EOL() const { 669 O << '\n'; 670} 671 672void AsmPrinter::EOL(const std::string &Comment) const { 673 if (VerboseAsm && !Comment.empty()) { 674 O << '\t' 675 << TAI->getCommentString() 676 << ' ' 677 << Comment; 678 } 679 O << '\n'; 680} 681 682void AsmPrinter::EOL(const char* Comment) const { 683 if (VerboseAsm && *Comment) { 684 O << '\t' 685 << TAI->getCommentString() 686 << ' ' 687 << Comment; 688 } 689 O << '\n'; 690} 691 692/// EmitULEB128Bytes - Emit an assembler byte data directive to compose an 693/// unsigned leb128 value. 694void AsmPrinter::EmitULEB128Bytes(unsigned Value) const { 695 if (TAI->hasLEB128()) { 696 O << "\t.uleb128\t" 697 << Value; 698 } else { 699 O << TAI->getData8bitsDirective(); 700 PrintULEB128(Value); 701 } 702} 703 704/// EmitSLEB128Bytes - print an assembler byte data directive to compose a 705/// signed leb128 value. 706void AsmPrinter::EmitSLEB128Bytes(int Value) const { 707 if (TAI->hasLEB128()) { 708 O << "\t.sleb128\t" 709 << Value; 710 } else { 711 O << TAI->getData8bitsDirective(); 712 PrintSLEB128(Value); 713 } 714} 715 716/// EmitInt8 - Emit a byte directive and value. 717/// 718void AsmPrinter::EmitInt8(int Value) const { 719 O << TAI->getData8bitsDirective(); 720 PrintHex(Value & 0xFF); 721} 722 723/// EmitInt16 - Emit a short directive and value. 724/// 725void AsmPrinter::EmitInt16(int Value) const { 726 O << TAI->getData16bitsDirective(); 727 PrintHex(Value & 0xFFFF); 728} 729 730/// EmitInt32 - Emit a long directive and value. 731/// 732void AsmPrinter::EmitInt32(int Value) const { 733 O << TAI->getData32bitsDirective(); 734 PrintHex(Value); 735} 736 737/// EmitInt64 - Emit a long long directive and value. 738/// 739void AsmPrinter::EmitInt64(uint64_t Value) const { 740 if (TAI->getData64bitsDirective()) { 741 O << TAI->getData64bitsDirective(); 742 PrintHex(Value); 743 } else { 744 if (TM.getTargetData()->isBigEndian()) { 745 EmitInt32(unsigned(Value >> 32)); O << '\n'; 746 EmitInt32(unsigned(Value)); 747 } else { 748 EmitInt32(unsigned(Value)); O << '\n'; 749 EmitInt32(unsigned(Value >> 32)); 750 } 751 } 752} 753 754/// toOctal - Convert the low order bits of X into an octal digit. 755/// 756static inline char toOctal(int X) { 757 return (X&7)+'0'; 758} 759 760/// printStringChar - Print a char, escaped if necessary. 761/// 762static void printStringChar(formatted_raw_ostream &O, unsigned char C) { 763 if (C == '"') { 764 O << "\\\""; 765 } else if (C == '\\') { 766 O << "\\\\"; 767 } else if (isprint((unsigned char)C)) { 768 O << C; 769 } else { 770 switch(C) { 771 case '\b': O << "\\b"; break; 772 case '\f': O << "\\f"; break; 773 case '\n': O << "\\n"; break; 774 case '\r': O << "\\r"; break; 775 case '\t': O << "\\t"; break; 776 default: 777 O << '\\'; 778 O << toOctal(C >> 6); 779 O << toOctal(C >> 3); 780 O << toOctal(C >> 0); 781 break; 782 } 783 } 784} 785 786/// EmitString - Emit a string with quotes and a null terminator. 787/// Special characters are emitted properly. 788/// \literal (Eg. '\t') \endliteral 789void AsmPrinter::EmitString(const std::string &String) const { 790 EmitString(String.c_str(), String.size()); 791} 792 793void AsmPrinter::EmitString(const char *String, unsigned Size) const { 794 const char* AscizDirective = TAI->getAscizDirective(); 795 if (AscizDirective) 796 O << AscizDirective; 797 else 798 O << TAI->getAsciiDirective(); 799 O << '\"'; 800 for (unsigned i = 0; i < Size; ++i) 801 printStringChar(O, String[i]); 802 if (AscizDirective) 803 O << '\"'; 804 else 805 O << "\\0\""; 806} 807 808 809/// EmitFile - Emit a .file directive. 810void AsmPrinter::EmitFile(unsigned Number, const std::string &Name) const { 811 O << "\t.file\t" << Number << " \""; 812 for (unsigned i = 0, N = Name.size(); i < N; ++i) 813 printStringChar(O, Name[i]); 814 O << '\"'; 815} 816 817 818//===----------------------------------------------------------------------===// 819 820// EmitAlignment - Emit an alignment directive to the specified power of 821// two boundary. For example, if you pass in 3 here, you will get an 8 822// byte alignment. If a global value is specified, and if that global has 823// an explicit alignment requested, it will unconditionally override the 824// alignment request. However, if ForcedAlignBits is specified, this value 825// has final say: the ultimate alignment will be the max of ForcedAlignBits 826// and the alignment computed with NumBits and the global. 827// 828// The algorithm is: 829// Align = NumBits; 830// if (GV && GV->hasalignment) Align = GV->getalignment(); 831// Align = std::max(Align, ForcedAlignBits); 832// 833void AsmPrinter::EmitAlignment(unsigned NumBits, const GlobalValue *GV, 834 unsigned ForcedAlignBits, 835 bool UseFillExpr) const { 836 if (GV && GV->getAlignment()) 837 NumBits = Log2_32(GV->getAlignment()); 838 NumBits = std::max(NumBits, ForcedAlignBits); 839 840 if (NumBits == 0) return; // No need to emit alignment. 841 if (TAI->getAlignmentIsInBytes()) NumBits = 1 << NumBits; 842 O << TAI->getAlignDirective() << NumBits; 843 844 unsigned FillValue = TAI->getTextAlignFillValue(); 845 UseFillExpr &= IsInTextSection && FillValue; 846 if (UseFillExpr) { 847 O << ','; 848 PrintHex(FillValue); 849 } 850 O << '\n'; 851} 852 853/// PadToColumn - This gets called every time a tab is emitted. If 854/// column padding is turned on, we replace the tab with the 855/// appropriate amount of padding. If not, we replace the tab with a 856/// space, except for the first operand so that initial operands are 857/// always lined up by tabs. 858void AsmPrinter::PadToColumn(unsigned Operand) const { 859 if (TAI->getOperandColumn(Operand) > 0) { 860 O.PadToColumn(TAI->getOperandColumn(Operand), 1); 861 } 862 else { 863 if (Operand == 1) { 864 // Emit the tab after the mnemonic. 865 O << '\t'; 866 } 867 else { 868 // Replace the tab with a space. 869 O << ' '; 870 } 871 } 872} 873 874/// EmitZeros - Emit a block of zeros. 875/// 876void AsmPrinter::EmitZeros(uint64_t NumZeros, unsigned AddrSpace) const { 877 if (NumZeros) { 878 if (TAI->getZeroDirective()) { 879 O << TAI->getZeroDirective() << NumZeros; 880 if (TAI->getZeroDirectiveSuffix()) 881 O << TAI->getZeroDirectiveSuffix(); 882 O << '\n'; 883 } else { 884 for (; NumZeros; --NumZeros) 885 O << TAI->getData8bitsDirective(AddrSpace) << "0\n"; 886 } 887 } 888} 889 890// Print out the specified constant, without a storage class. Only the 891// constants valid in constant expressions can occur here. 892void AsmPrinter::EmitConstantValueOnly(const Constant *CV) { 893 if (CV->isNullValue() || isa<UndefValue>(CV)) 894 O << '0'; 895 else if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) { 896 O << CI->getZExtValue(); 897 } else if (const GlobalValue *GV = dyn_cast<GlobalValue>(CV)) { 898 // This is a constant address for a global variable or function. Use the 899 // name of the variable or function as the address value, possibly 900 // decorating it with GlobalVarAddrPrefix/Suffix or 901 // FunctionAddrPrefix/Suffix (these all default to "" ) 902 if (isa<Function>(GV)) { 903 O << TAI->getFunctionAddrPrefix() 904 << Mang->getMangledName(GV) 905 << TAI->getFunctionAddrSuffix(); 906 } else { 907 O << TAI->getGlobalVarAddrPrefix() 908 << Mang->getMangledName(GV) 909 << TAI->getGlobalVarAddrSuffix(); 910 } 911 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) { 912 const TargetData *TD = TM.getTargetData(); 913 unsigned Opcode = CE->getOpcode(); 914 switch (Opcode) { 915 case Instruction::Trunc: 916 case Instruction::ZExt: 917 case Instruction::SExt: 918 case Instruction::FPTrunc: 919 case Instruction::FPExt: 920 case Instruction::UIToFP: 921 case Instruction::SIToFP: 922 case Instruction::FPToUI: 923 case Instruction::FPToSI: 924 llvm_unreachable("FIXME: Don't support this constant cast expr"); 925 case Instruction::GetElementPtr: { 926 // generate a symbolic expression for the byte address 927 const Constant *ptrVal = CE->getOperand(0); 928 SmallVector<Value*, 8> idxVec(CE->op_begin()+1, CE->op_end()); 929 if (int64_t Offset = TD->getIndexedOffset(ptrVal->getType(), &idxVec[0], 930 idxVec.size())) { 931 // Truncate/sext the offset to the pointer size. 932 if (TD->getPointerSizeInBits() != 64) { 933 int SExtAmount = 64-TD->getPointerSizeInBits(); 934 Offset = (Offset << SExtAmount) >> SExtAmount; 935 } 936 937 if (Offset) 938 O << '('; 939 EmitConstantValueOnly(ptrVal); 940 if (Offset > 0) 941 O << ") + " << Offset; 942 else if (Offset < 0) 943 O << ") - " << -Offset; 944 } else { 945 EmitConstantValueOnly(ptrVal); 946 } 947 break; 948 } 949 case Instruction::BitCast: 950 return EmitConstantValueOnly(CE->getOperand(0)); 951 952 case Instruction::IntToPtr: { 953 // Handle casts to pointers by changing them into casts to the appropriate 954 // integer type. This promotes constant folding and simplifies this code. 955 Constant *Op = CE->getOperand(0); 956 Op = ConstantExpr::getIntegerCast(Op, TD->getIntPtrType(), false/*ZExt*/); 957 return EmitConstantValueOnly(Op); 958 } 959 960 961 case Instruction::PtrToInt: { 962 // Support only foldable casts to/from pointers that can be eliminated by 963 // changing the pointer to the appropriately sized integer type. 964 Constant *Op = CE->getOperand(0); 965 const Type *Ty = CE->getType(); 966 967 // We can emit the pointer value into this slot if the slot is an 968 // integer slot greater or equal to the size of the pointer. 969 if (TD->getTypeAllocSize(Ty) == TD->getTypeAllocSize(Op->getType())) 970 return EmitConstantValueOnly(Op); 971 972 O << "(("; 973 EmitConstantValueOnly(Op); 974 APInt ptrMask = 975 APInt::getAllOnesValue(TD->getTypeAllocSizeInBits(Op->getType())); 976 977 SmallString<40> S; 978 ptrMask.toStringUnsigned(S); 979 O << ") & " << S.c_str() << ')'; 980 break; 981 } 982 case Instruction::Add: 983 case Instruction::Sub: 984 case Instruction::And: 985 case Instruction::Or: 986 case Instruction::Xor: 987 O << '('; 988 EmitConstantValueOnly(CE->getOperand(0)); 989 O << ')'; 990 switch (Opcode) { 991 case Instruction::Add: 992 O << " + "; 993 break; 994 case Instruction::Sub: 995 O << " - "; 996 break; 997 case Instruction::And: 998 O << " & "; 999 break; 1000 case Instruction::Or: 1001 O << " | "; 1002 break; 1003 case Instruction::Xor: 1004 O << " ^ "; 1005 break; 1006 default: 1007 break; 1008 } 1009 O << '('; 1010 EmitConstantValueOnly(CE->getOperand(1)); 1011 O << ')'; 1012 break; 1013 default: 1014 llvm_unreachable("Unsupported operator!"); 1015 } 1016 } else { 1017 llvm_unreachable("Unknown constant value!"); 1018 } 1019} 1020 1021/// printAsCString - Print the specified array as a C compatible string, only if 1022/// the predicate isString is true. 1023/// 1024static void printAsCString(formatted_raw_ostream &O, const ConstantArray *CVA, 1025 unsigned LastElt) { 1026 assert(CVA->isString() && "Array is not string compatible!"); 1027 1028 O << '\"'; 1029 for (unsigned i = 0; i != LastElt; ++i) { 1030 unsigned char C = 1031 (unsigned char)cast<ConstantInt>(CVA->getOperand(i))->getZExtValue(); 1032 printStringChar(O, C); 1033 } 1034 O << '\"'; 1035} 1036 1037/// EmitString - Emit a zero-byte-terminated string constant. 1038/// 1039void AsmPrinter::EmitString(const ConstantArray *CVA) const { 1040 unsigned NumElts = CVA->getNumOperands(); 1041 if (TAI->getAscizDirective() && NumElts && 1042 cast<ConstantInt>(CVA->getOperand(NumElts-1))->getZExtValue() == 0) { 1043 O << TAI->getAscizDirective(); 1044 printAsCString(O, CVA, NumElts-1); 1045 } else { 1046 O << TAI->getAsciiDirective(); 1047 printAsCString(O, CVA, NumElts); 1048 } 1049 O << '\n'; 1050} 1051 1052void AsmPrinter::EmitGlobalConstantArray(const ConstantArray *CVA, 1053 unsigned AddrSpace) { 1054 if (CVA->isString()) { 1055 EmitString(CVA); 1056 } else { // Not a string. Print the values in successive locations 1057 for (unsigned i = 0, e = CVA->getNumOperands(); i != e; ++i) 1058 EmitGlobalConstant(CVA->getOperand(i), AddrSpace); 1059 } 1060} 1061 1062void AsmPrinter::EmitGlobalConstantVector(const ConstantVector *CP) { 1063 const VectorType *PTy = CP->getType(); 1064 1065 for (unsigned I = 0, E = PTy->getNumElements(); I < E; ++I) 1066 EmitGlobalConstant(CP->getOperand(I)); 1067} 1068 1069void AsmPrinter::EmitGlobalConstantStruct(const ConstantStruct *CVS, 1070 unsigned AddrSpace) { 1071 // Print the fields in successive locations. Pad to align if needed! 1072 const TargetData *TD = TM.getTargetData(); 1073 unsigned Size = TD->getTypeAllocSize(CVS->getType()); 1074 const StructLayout *cvsLayout = TD->getStructLayout(CVS->getType()); 1075 uint64_t sizeSoFar = 0; 1076 for (unsigned i = 0, e = CVS->getNumOperands(); i != e; ++i) { 1077 const Constant* field = CVS->getOperand(i); 1078 1079 // Check if padding is needed and insert one or more 0s. 1080 uint64_t fieldSize = TD->getTypeAllocSize(field->getType()); 1081 uint64_t padSize = ((i == e-1 ? Size : cvsLayout->getElementOffset(i+1)) 1082 - cvsLayout->getElementOffset(i)) - fieldSize; 1083 sizeSoFar += fieldSize + padSize; 1084 1085 // Now print the actual field value. 1086 EmitGlobalConstant(field, AddrSpace); 1087 1088 // Insert padding - this may include padding to increase the size of the 1089 // current field up to the ABI size (if the struct is not packed) as well 1090 // as padding to ensure that the next field starts at the right offset. 1091 EmitZeros(padSize, AddrSpace); 1092 } 1093 assert(sizeSoFar == cvsLayout->getSizeInBytes() && 1094 "Layout of constant struct may be incorrect!"); 1095} 1096 1097void AsmPrinter::EmitGlobalConstantFP(const ConstantFP *CFP, 1098 unsigned AddrSpace) { 1099 // FP Constants are printed as integer constants to avoid losing 1100 // precision... 1101 const TargetData *TD = TM.getTargetData(); 1102 if (CFP->getType() == Type::DoubleTy) { 1103 double Val = CFP->getValueAPF().convertToDouble(); // for comment only 1104 uint64_t i = CFP->getValueAPF().bitcastToAPInt().getZExtValue(); 1105 if (TAI->getData64bitsDirective(AddrSpace)) { 1106 O << TAI->getData64bitsDirective(AddrSpace) << i; 1107 if (VerboseAsm) 1108 O << '\t' << TAI->getCommentString() << " double value: " << Val; 1109 O << '\n'; 1110 } else if (TD->isBigEndian()) { 1111 O << TAI->getData32bitsDirective(AddrSpace) << unsigned(i >> 32); 1112 if (VerboseAsm) 1113 O << '\t' << TAI->getCommentString() 1114 << " double most significant word " << Val; 1115 O << '\n'; 1116 O << TAI->getData32bitsDirective(AddrSpace) << unsigned(i); 1117 if (VerboseAsm) 1118 O << '\t' << TAI->getCommentString() 1119 << " double least significant word " << Val; 1120 O << '\n'; 1121 } else { 1122 O << TAI->getData32bitsDirective(AddrSpace) << unsigned(i); 1123 if (VerboseAsm) 1124 O << '\t' << TAI->getCommentString() 1125 << " double least significant word " << Val; 1126 O << '\n'; 1127 O << TAI->getData32bitsDirective(AddrSpace) << unsigned(i >> 32); 1128 if (VerboseAsm) 1129 O << '\t' << TAI->getCommentString() 1130 << " double most significant word " << Val; 1131 O << '\n'; 1132 } 1133 return; 1134 } else if (CFP->getType() == Type::FloatTy) { 1135 float Val = CFP->getValueAPF().convertToFloat(); // for comment only 1136 O << TAI->getData32bitsDirective(AddrSpace) 1137 << CFP->getValueAPF().bitcastToAPInt().getZExtValue(); 1138 if (VerboseAsm) 1139 O << '\t' << TAI->getCommentString() << " float " << Val; 1140 O << '\n'; 1141 return; 1142 } else if (CFP->getType() == Type::X86_FP80Ty) { 1143 // all long double variants are printed as hex 1144 // api needed to prevent premature destruction 1145 APInt api = CFP->getValueAPF().bitcastToAPInt(); 1146 const uint64_t *p = api.getRawData(); 1147 // Convert to double so we can print the approximate val as a comment. 1148 APFloat DoubleVal = CFP->getValueAPF(); 1149 bool ignored; 1150 DoubleVal.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven, 1151 &ignored); 1152 if (TD->isBigEndian()) { 1153 O << TAI->getData16bitsDirective(AddrSpace) << uint16_t(p[1]); 1154 if (VerboseAsm) 1155 O << '\t' << TAI->getCommentString() 1156 << " long double most significant halfword of ~" 1157 << DoubleVal.convertToDouble(); 1158 O << '\n'; 1159 O << TAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 48); 1160 if (VerboseAsm) 1161 O << '\t' << TAI->getCommentString() << " long double next halfword"; 1162 O << '\n'; 1163 O << TAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 32); 1164 if (VerboseAsm) 1165 O << '\t' << TAI->getCommentString() << " long double next halfword"; 1166 O << '\n'; 1167 O << TAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 16); 1168 if (VerboseAsm) 1169 O << '\t' << TAI->getCommentString() << " long double next halfword"; 1170 O << '\n'; 1171 O << TAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0]); 1172 if (VerboseAsm) 1173 O << '\t' << TAI->getCommentString() 1174 << " long double least significant halfword"; 1175 O << '\n'; 1176 } else { 1177 O << TAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0]); 1178 if (VerboseAsm) 1179 O << '\t' << TAI->getCommentString() 1180 << " long double least significant halfword of ~" 1181 << DoubleVal.convertToDouble(); 1182 O << '\n'; 1183 O << TAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 16); 1184 if (VerboseAsm) 1185 O << '\t' << TAI->getCommentString() 1186 << " long double next halfword"; 1187 O << '\n'; 1188 O << TAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 32); 1189 if (VerboseAsm) 1190 O << '\t' << TAI->getCommentString() 1191 << " long double next halfword"; 1192 O << '\n'; 1193 O << TAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 48); 1194 if (VerboseAsm) 1195 O << '\t' << TAI->getCommentString() 1196 << " long double next halfword"; 1197 O << '\n'; 1198 O << TAI->getData16bitsDirective(AddrSpace) << uint16_t(p[1]); 1199 if (VerboseAsm) 1200 O << '\t' << TAI->getCommentString() 1201 << " long double most significant halfword"; 1202 O << '\n'; 1203 } 1204 EmitZeros(TD->getTypeAllocSize(Type::X86_FP80Ty) - 1205 TD->getTypeStoreSize(Type::X86_FP80Ty), AddrSpace); 1206 return; 1207 } else if (CFP->getType() == Type::PPC_FP128Ty) { 1208 // all long double variants are printed as hex 1209 // api needed to prevent premature destruction 1210 APInt api = CFP->getValueAPF().bitcastToAPInt(); 1211 const uint64_t *p = api.getRawData(); 1212 if (TD->isBigEndian()) { 1213 O << TAI->getData32bitsDirective(AddrSpace) << uint32_t(p[0] >> 32); 1214 if (VerboseAsm) 1215 O << '\t' << TAI->getCommentString() 1216 << " long double most significant word"; 1217 O << '\n'; 1218 O << TAI->getData32bitsDirective(AddrSpace) << uint32_t(p[0]); 1219 if (VerboseAsm) 1220 O << '\t' << TAI->getCommentString() 1221 << " long double next word"; 1222 O << '\n'; 1223 O << TAI->getData32bitsDirective(AddrSpace) << uint32_t(p[1] >> 32); 1224 if (VerboseAsm) 1225 O << '\t' << TAI->getCommentString() 1226 << " long double next word"; 1227 O << '\n'; 1228 O << TAI->getData32bitsDirective(AddrSpace) << uint32_t(p[1]); 1229 if (VerboseAsm) 1230 O << '\t' << TAI->getCommentString() 1231 << " long double least significant word"; 1232 O << '\n'; 1233 } else { 1234 O << TAI->getData32bitsDirective(AddrSpace) << uint32_t(p[1]); 1235 if (VerboseAsm) 1236 O << '\t' << TAI->getCommentString() 1237 << " long double least significant word"; 1238 O << '\n'; 1239 O << TAI->getData32bitsDirective(AddrSpace) << uint32_t(p[1] >> 32); 1240 if (VerboseAsm) 1241 O << '\t' << TAI->getCommentString() 1242 << " long double next word"; 1243 O << '\n'; 1244 O << TAI->getData32bitsDirective(AddrSpace) << uint32_t(p[0]); 1245 if (VerboseAsm) 1246 O << '\t' << TAI->getCommentString() 1247 << " long double next word"; 1248 O << '\n'; 1249 O << TAI->getData32bitsDirective(AddrSpace) << uint32_t(p[0] >> 32); 1250 if (VerboseAsm) 1251 O << '\t' << TAI->getCommentString() 1252 << " long double most significant word"; 1253 O << '\n'; 1254 } 1255 return; 1256 } else llvm_unreachable("Floating point constant type not handled"); 1257} 1258 1259void AsmPrinter::EmitGlobalConstantLargeInt(const ConstantInt *CI, 1260 unsigned AddrSpace) { 1261 const TargetData *TD = TM.getTargetData(); 1262 unsigned BitWidth = CI->getBitWidth(); 1263 assert(isPowerOf2_32(BitWidth) && 1264 "Non-power-of-2-sized integers not handled!"); 1265 1266 // We don't expect assemblers to support integer data directives 1267 // for more than 64 bits, so we emit the data in at most 64-bit 1268 // quantities at a time. 1269 const uint64_t *RawData = CI->getValue().getRawData(); 1270 for (unsigned i = 0, e = BitWidth / 64; i != e; ++i) { 1271 uint64_t Val; 1272 if (TD->isBigEndian()) 1273 Val = RawData[e - i - 1]; 1274 else 1275 Val = RawData[i]; 1276 1277 if (TAI->getData64bitsDirective(AddrSpace)) 1278 O << TAI->getData64bitsDirective(AddrSpace) << Val << '\n'; 1279 else if (TD->isBigEndian()) { 1280 O << TAI->getData32bitsDirective(AddrSpace) << unsigned(Val >> 32); 1281 if (VerboseAsm) 1282 O << '\t' << TAI->getCommentString() 1283 << " Double-word most significant word " << Val; 1284 O << '\n'; 1285 O << TAI->getData32bitsDirective(AddrSpace) << unsigned(Val); 1286 if (VerboseAsm) 1287 O << '\t' << TAI->getCommentString() 1288 << " Double-word least significant word " << Val; 1289 O << '\n'; 1290 } else { 1291 O << TAI->getData32bitsDirective(AddrSpace) << unsigned(Val); 1292 if (VerboseAsm) 1293 O << '\t' << TAI->getCommentString() 1294 << " Double-word least significant word " << Val; 1295 O << '\n'; 1296 O << TAI->getData32bitsDirective(AddrSpace) << unsigned(Val >> 32); 1297 if (VerboseAsm) 1298 O << '\t' << TAI->getCommentString() 1299 << " Double-word most significant word " << Val; 1300 O << '\n'; 1301 } 1302 } 1303} 1304 1305/// EmitGlobalConstant - Print a general LLVM constant to the .s file. 1306void AsmPrinter::EmitGlobalConstant(const Constant *CV, unsigned AddrSpace) { 1307 const TargetData *TD = TM.getTargetData(); 1308 const Type *type = CV->getType(); 1309 unsigned Size = TD->getTypeAllocSize(type); 1310 1311 if (CV->isNullValue() || isa<UndefValue>(CV)) { 1312 EmitZeros(Size, AddrSpace); 1313 return; 1314 } else if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) { 1315 EmitGlobalConstantArray(CVA , AddrSpace); 1316 return; 1317 } else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV)) { 1318 EmitGlobalConstantStruct(CVS, AddrSpace); 1319 return; 1320 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) { 1321 EmitGlobalConstantFP(CFP, AddrSpace); 1322 return; 1323 } else if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) { 1324 // Small integers are handled below; large integers are handled here. 1325 if (Size > 4) { 1326 EmitGlobalConstantLargeInt(CI, AddrSpace); 1327 return; 1328 } 1329 } else if (const ConstantVector *CP = dyn_cast<ConstantVector>(CV)) { 1330 EmitGlobalConstantVector(CP); 1331 return; 1332 } 1333 1334 printDataDirective(type, AddrSpace); 1335 EmitConstantValueOnly(CV); 1336 if (VerboseAsm) { 1337 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) { 1338 SmallString<40> S; 1339 CI->getValue().toStringUnsigned(S, 16); 1340 O << "\t\t\t" << TAI->getCommentString() << " 0x" << S.c_str(); 1341 } 1342 } 1343 O << '\n'; 1344} 1345 1346void AsmPrinter::EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV) { 1347 // Target doesn't support this yet! 1348 llvm_unreachable("Target does not support EmitMachineConstantPoolValue"); 1349} 1350 1351/// PrintSpecial - Print information related to the specified machine instr 1352/// that is independent of the operand, and may be independent of the instr 1353/// itself. This can be useful for portably encoding the comment character 1354/// or other bits of target-specific knowledge into the asmstrings. The 1355/// syntax used is ${:comment}. Targets can override this to add support 1356/// for their own strange codes. 1357void AsmPrinter::PrintSpecial(const MachineInstr *MI, const char *Code) const { 1358 if (!strcmp(Code, "private")) { 1359 O << TAI->getPrivateGlobalPrefix(); 1360 } else if (!strcmp(Code, "comment")) { 1361 if (VerboseAsm) 1362 O << TAI->getCommentString(); 1363 } else if (!strcmp(Code, "uid")) { 1364 // Comparing the address of MI isn't sufficient, because machineinstrs may 1365 // be allocated to the same address across functions. 1366 const Function *ThisF = MI->getParent()->getParent()->getFunction(); 1367 1368 // If this is a new LastFn instruction, bump the counter. 1369 if (LastMI != MI || LastFn != ThisF) { 1370 ++Counter; 1371 LastMI = MI; 1372 LastFn = ThisF; 1373 } 1374 O << Counter; 1375 } else { 1376 std::string msg; 1377 raw_string_ostream Msg(msg); 1378 Msg << "Unknown special formatter '" << Code 1379 << "' for machine instr: " << *MI; 1380 llvm_report_error(Msg.str()); 1381 } 1382} 1383 1384/// processDebugLoc - Processes the debug information of each machine 1385/// instruction's DebugLoc. 1386void AsmPrinter::processDebugLoc(DebugLoc DL) { 1387 if (TAI->doesSupportDebugInformation() && DW->ShouldEmitDwarfDebug()) { 1388 if (!DL.isUnknown()) { 1389 DebugLocTuple CurDLT = MF->getDebugLocTuple(DL); 1390 1391 if (CurDLT.CompileUnit != 0 && PrevDLT != CurDLT) 1392 printLabel(DW->RecordSourceLine(CurDLT.Line, CurDLT.Col, 1393 DICompileUnit(CurDLT.CompileUnit))); 1394 1395 PrevDLT = CurDLT; 1396 } 1397 } 1398} 1399 1400/// printInlineAsm - This method formats and prints the specified machine 1401/// instruction that is an inline asm. 1402void AsmPrinter::printInlineAsm(const MachineInstr *MI) const { 1403 unsigned NumOperands = MI->getNumOperands(); 1404 1405 // Count the number of register definitions. 1406 unsigned NumDefs = 0; 1407 for (; MI->getOperand(NumDefs).isReg() && MI->getOperand(NumDefs).isDef(); 1408 ++NumDefs) 1409 assert(NumDefs != NumOperands-1 && "No asm string?"); 1410 1411 assert(MI->getOperand(NumDefs).isSymbol() && "No asm string?"); 1412 1413 // Disassemble the AsmStr, printing out the literal pieces, the operands, etc. 1414 const char *AsmStr = MI->getOperand(NumDefs).getSymbolName(); 1415 1416 // If this asmstr is empty, just print the #APP/#NOAPP markers. 1417 // These are useful to see where empty asm's wound up. 1418 if (AsmStr[0] == 0) { 1419 O << TAI->getInlineAsmStart() << "\n\t" << TAI->getInlineAsmEnd() << '\n'; 1420 return; 1421 } 1422 1423 O << TAI->getInlineAsmStart() << "\n\t"; 1424 1425 // The variant of the current asmprinter. 1426 int AsmPrinterVariant = TAI->getAssemblerDialect(); 1427 1428 int CurVariant = -1; // The number of the {.|.|.} region we are in. 1429 const char *LastEmitted = AsmStr; // One past the last character emitted. 1430 1431 while (*LastEmitted) { 1432 switch (*LastEmitted) { 1433 default: { 1434 // Not a special case, emit the string section literally. 1435 const char *LiteralEnd = LastEmitted+1; 1436 while (*LiteralEnd && *LiteralEnd != '{' && *LiteralEnd != '|' && 1437 *LiteralEnd != '}' && *LiteralEnd != '$' && *LiteralEnd != '\n') 1438 ++LiteralEnd; 1439 if (CurVariant == -1 || CurVariant == AsmPrinterVariant) 1440 O.write(LastEmitted, LiteralEnd-LastEmitted); 1441 LastEmitted = LiteralEnd; 1442 break; 1443 } 1444 case '\n': 1445 ++LastEmitted; // Consume newline character. 1446 O << '\n'; // Indent code with newline. 1447 break; 1448 case '$': { 1449 ++LastEmitted; // Consume '$' character. 1450 bool Done = true; 1451 1452 // Handle escapes. 1453 switch (*LastEmitted) { 1454 default: Done = false; break; 1455 case '$': // $$ -> $ 1456 if (CurVariant == -1 || CurVariant == AsmPrinterVariant) 1457 O << '$'; 1458 ++LastEmitted; // Consume second '$' character. 1459 break; 1460 case '(': // $( -> same as GCC's { character. 1461 ++LastEmitted; // Consume '(' character. 1462 if (CurVariant != -1) { 1463 llvm_report_error("Nested variants found in inline asm string: '" 1464 + std::string(AsmStr) + "'"); 1465 } 1466 CurVariant = 0; // We're in the first variant now. 1467 break; 1468 case '|': 1469 ++LastEmitted; // consume '|' character. 1470 if (CurVariant == -1) 1471 O << '|'; // this is gcc's behavior for | outside a variant 1472 else 1473 ++CurVariant; // We're in the next variant. 1474 break; 1475 case ')': // $) -> same as GCC's } char. 1476 ++LastEmitted; // consume ')' character. 1477 if (CurVariant == -1) 1478 O << '}'; // this is gcc's behavior for } outside a variant 1479 else 1480 CurVariant = -1; 1481 break; 1482 } 1483 if (Done) break; 1484 1485 bool HasCurlyBraces = false; 1486 if (*LastEmitted == '{') { // ${variable} 1487 ++LastEmitted; // Consume '{' character. 1488 HasCurlyBraces = true; 1489 } 1490 1491 // If we have ${:foo}, then this is not a real operand reference, it is a 1492 // "magic" string reference, just like in .td files. Arrange to call 1493 // PrintSpecial. 1494 if (HasCurlyBraces && *LastEmitted == ':') { 1495 ++LastEmitted; 1496 const char *StrStart = LastEmitted; 1497 const char *StrEnd = strchr(StrStart, '}'); 1498 if (StrEnd == 0) { 1499 llvm_report_error("Unterminated ${:foo} operand in inline asm string: '" 1500 + std::string(AsmStr) + "'"); 1501 } 1502 1503 std::string Val(StrStart, StrEnd); 1504 PrintSpecial(MI, Val.c_str()); 1505 LastEmitted = StrEnd+1; 1506 break; 1507 } 1508 1509 const char *IDStart = LastEmitted; 1510 char *IDEnd; 1511 errno = 0; 1512 long Val = strtol(IDStart, &IDEnd, 10); // We only accept numbers for IDs. 1513 if (!isdigit(*IDStart) || (Val == 0 && errno == EINVAL)) { 1514 llvm_report_error("Bad $ operand number in inline asm string: '" 1515 + std::string(AsmStr) + "'"); 1516 } 1517 LastEmitted = IDEnd; 1518 1519 char Modifier[2] = { 0, 0 }; 1520 1521 if (HasCurlyBraces) { 1522 // If we have curly braces, check for a modifier character. This 1523 // supports syntax like ${0:u}, which correspond to "%u0" in GCC asm. 1524 if (*LastEmitted == ':') { 1525 ++LastEmitted; // Consume ':' character. 1526 if (*LastEmitted == 0) { 1527 llvm_report_error("Bad ${:} expression in inline asm string: '" 1528 + std::string(AsmStr) + "'"); 1529 } 1530 1531 Modifier[0] = *LastEmitted; 1532 ++LastEmitted; // Consume modifier character. 1533 } 1534 1535 if (*LastEmitted != '}') { 1536 llvm_report_error("Bad ${} expression in inline asm string: '" 1537 + std::string(AsmStr) + "'"); 1538 } 1539 ++LastEmitted; // Consume '}' character. 1540 } 1541 1542 if ((unsigned)Val >= NumOperands-1) { 1543 llvm_report_error("Invalid $ operand number in inline asm string: '" 1544 + std::string(AsmStr) + "'"); 1545 } 1546 1547 // Okay, we finally have a value number. Ask the target to print this 1548 // operand! 1549 if (CurVariant == -1 || CurVariant == AsmPrinterVariant) { 1550 unsigned OpNo = 1; 1551 1552 bool Error = false; 1553 1554 // Scan to find the machine operand number for the operand. 1555 for (; Val; --Val) { 1556 if (OpNo >= MI->getNumOperands()) break; 1557 unsigned OpFlags = MI->getOperand(OpNo).getImm(); 1558 OpNo += InlineAsm::getNumOperandRegisters(OpFlags) + 1; 1559 } 1560 1561 if (OpNo >= MI->getNumOperands()) { 1562 Error = true; 1563 } else { 1564 unsigned OpFlags = MI->getOperand(OpNo).getImm(); 1565 ++OpNo; // Skip over the ID number. 1566 1567 if (Modifier[0]=='l') // labels are target independent 1568 printBasicBlockLabel(MI->getOperand(OpNo).getMBB(), 1569 false, false, false); 1570 else { 1571 AsmPrinter *AP = const_cast<AsmPrinter*>(this); 1572 if ((OpFlags & 7) == 4) { 1573 Error = AP->PrintAsmMemoryOperand(MI, OpNo, AsmPrinterVariant, 1574 Modifier[0] ? Modifier : 0); 1575 } else { 1576 Error = AP->PrintAsmOperand(MI, OpNo, AsmPrinterVariant, 1577 Modifier[0] ? Modifier : 0); 1578 } 1579 } 1580 } 1581 if (Error) { 1582 std::string msg; 1583 raw_string_ostream Msg(msg); 1584 Msg << "Invalid operand found in inline asm: '" 1585 << AsmStr << "'\n"; 1586 MI->print(Msg); 1587 llvm_report_error(Msg.str()); 1588 } 1589 } 1590 break; 1591 } 1592 } 1593 } 1594 O << "\n\t" << TAI->getInlineAsmEnd() << '\n'; 1595} 1596 1597/// printImplicitDef - This method prints the specified machine instruction 1598/// that is an implicit def. 1599void AsmPrinter::printImplicitDef(const MachineInstr *MI) const { 1600 if (VerboseAsm) 1601 O << '\t' << TAI->getCommentString() << " implicit-def: " 1602 << TRI->getAsmName(MI->getOperand(0).getReg()) << '\n'; 1603} 1604 1605/// printLabel - This method prints a local label used by debug and 1606/// exception handling tables. 1607void AsmPrinter::printLabel(const MachineInstr *MI) const { 1608 printLabel(MI->getOperand(0).getImm()); 1609} 1610 1611void AsmPrinter::printLabel(unsigned Id) const { 1612 O << TAI->getPrivateGlobalPrefix() << "label" << Id << ":\n"; 1613} 1614 1615/// printDeclare - This method prints a local variable declaration used by 1616/// debug tables. 1617/// FIXME: It doesn't really print anything rather it inserts a DebugVariable 1618/// entry into dwarf table. 1619void AsmPrinter::printDeclare(const MachineInstr *MI) const { 1620 unsigned FI = MI->getOperand(0).getIndex(); 1621 GlobalValue *GV = MI->getOperand(1).getGlobal(); 1622 DW->RecordVariable(cast<GlobalVariable>(GV), FI, MI); 1623} 1624 1625/// PrintAsmOperand - Print the specified operand of MI, an INLINEASM 1626/// instruction, using the specified assembler variant. Targets should 1627/// overried this to format as appropriate. 1628bool AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo, 1629 unsigned AsmVariant, const char *ExtraCode) { 1630 // Target doesn't support this yet! 1631 return true; 1632} 1633 1634bool AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo, 1635 unsigned AsmVariant, 1636 const char *ExtraCode) { 1637 // Target doesn't support this yet! 1638 return true; 1639} 1640 1641/// printBasicBlockLabel - This method prints the label for the specified 1642/// MachineBasicBlock 1643void AsmPrinter::printBasicBlockLabel(const MachineBasicBlock *MBB, 1644 bool printAlign, 1645 bool printColon, 1646 bool printComment) const { 1647 if (printAlign) { 1648 unsigned Align = MBB->getAlignment(); 1649 if (Align) 1650 EmitAlignment(Log2_32(Align)); 1651 } 1652 1653 O << TAI->getPrivateGlobalPrefix() << "BB" << getFunctionNumber() << '_' 1654 << MBB->getNumber(); 1655 if (printColon) 1656 O << ':'; 1657 if (printComment && MBB->getBasicBlock()) 1658 O << '\t' << TAI->getCommentString() << ' ' 1659 << MBB->getBasicBlock()->getNameStr(); 1660} 1661 1662/// printPICJumpTableSetLabel - This method prints a set label for the 1663/// specified MachineBasicBlock for a jumptable entry. 1664void AsmPrinter::printPICJumpTableSetLabel(unsigned uid, 1665 const MachineBasicBlock *MBB) const { 1666 if (!TAI->getSetDirective()) 1667 return; 1668 1669 O << TAI->getSetDirective() << ' ' << TAI->getPrivateGlobalPrefix() 1670 << getFunctionNumber() << '_' << uid << "_set_" << MBB->getNumber() << ','; 1671 printBasicBlockLabel(MBB, false, false, false); 1672 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber() 1673 << '_' << uid << '\n'; 1674} 1675 1676void AsmPrinter::printPICJumpTableSetLabel(unsigned uid, unsigned uid2, 1677 const MachineBasicBlock *MBB) const { 1678 if (!TAI->getSetDirective()) 1679 return; 1680 1681 O << TAI->getSetDirective() << ' ' << TAI->getPrivateGlobalPrefix() 1682 << getFunctionNumber() << '_' << uid << '_' << uid2 1683 << "_set_" << MBB->getNumber() << ','; 1684 printBasicBlockLabel(MBB, false, false, false); 1685 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber() 1686 << '_' << uid << '_' << uid2 << '\n'; 1687} 1688 1689/// printDataDirective - This method prints the asm directive for the 1690/// specified type. 1691void AsmPrinter::printDataDirective(const Type *type, unsigned AddrSpace) { 1692 const TargetData *TD = TM.getTargetData(); 1693 switch (type->getTypeID()) { 1694 case Type::FloatTyID: case Type::DoubleTyID: 1695 case Type::X86_FP80TyID: case Type::FP128TyID: case Type::PPC_FP128TyID: 1696 assert(0 && "Should have already output floating point constant."); 1697 default: 1698 assert(0 && "Can't handle printing this type of thing"); 1699 case Type::IntegerTyID: { 1700 unsigned BitWidth = cast<IntegerType>(type)->getBitWidth(); 1701 if (BitWidth <= 8) 1702 O << TAI->getData8bitsDirective(AddrSpace); 1703 else if (BitWidth <= 16) 1704 O << TAI->getData16bitsDirective(AddrSpace); 1705 else if (BitWidth <= 32) 1706 O << TAI->getData32bitsDirective(AddrSpace); 1707 else if (BitWidth <= 64) { 1708 assert(TAI->getData64bitsDirective(AddrSpace) && 1709 "Target cannot handle 64-bit constant exprs!"); 1710 O << TAI->getData64bitsDirective(AddrSpace); 1711 } else { 1712 llvm_unreachable("Target cannot handle given data directive width!"); 1713 } 1714 break; 1715 } 1716 case Type::PointerTyID: 1717 if (TD->getPointerSize() == 8) { 1718 assert(TAI->getData64bitsDirective(AddrSpace) && 1719 "Target cannot handle 64-bit pointer exprs!"); 1720 O << TAI->getData64bitsDirective(AddrSpace); 1721 } else if (TD->getPointerSize() == 2) { 1722 O << TAI->getData16bitsDirective(AddrSpace); 1723 } else if (TD->getPointerSize() == 1) { 1724 O << TAI->getData8bitsDirective(AddrSpace); 1725 } else { 1726 O << TAI->getData32bitsDirective(AddrSpace); 1727 } 1728 break; 1729 } 1730} 1731 1732void AsmPrinter::printVisibility(const std::string& Name, 1733 unsigned Visibility) const { 1734 if (Visibility == GlobalValue::HiddenVisibility) { 1735 if (const char *Directive = TAI->getHiddenDirective()) 1736 O << Directive << Name << '\n'; 1737 } else if (Visibility == GlobalValue::ProtectedVisibility) { 1738 if (const char *Directive = TAI->getProtectedDirective()) 1739 O << Directive << Name << '\n'; 1740 } 1741} 1742 1743void AsmPrinter::printOffset(int64_t Offset) const { 1744 if (Offset > 0) 1745 O << '+' << Offset; 1746 else if (Offset < 0) 1747 O << Offset; 1748} 1749 1750GCMetadataPrinter *AsmPrinter::GetOrCreateGCPrinter(GCStrategy *S) { 1751 if (!S->usesMetadata()) 1752 return 0; 1753 1754 gcp_iterator GCPI = GCMetadataPrinters.find(S); 1755 if (GCPI != GCMetadataPrinters.end()) 1756 return GCPI->second; 1757 1758 const char *Name = S->getName().c_str(); 1759 1760 for (GCMetadataPrinterRegistry::iterator 1761 I = GCMetadataPrinterRegistry::begin(), 1762 E = GCMetadataPrinterRegistry::end(); I != E; ++I) 1763 if (strcmp(Name, I->getName()) == 0) { 1764 GCMetadataPrinter *GMP = I->instantiate(); 1765 GMP->S = S; 1766 GCMetadataPrinters.insert(std::make_pair(S, GMP)); 1767 return GMP; 1768 } 1769 1770 cerr << "no GCMetadataPrinter registered for GC: " << Name << "\n"; 1771 llvm_unreachable(0); 1772} 1773 1774/// EmitComments - Pretty-print comments for instructions 1775void AsmPrinter::EmitComments(const MachineInstr &MI) const 1776{ 1777 if (VerboseAsm) { 1778 if (!MI.getDebugLoc().isUnknown()) { 1779 DebugLocTuple DLT = MF->getDebugLocTuple(MI.getDebugLoc()); 1780 1781 // Print source line info 1782 O.PadToColumn(TAI->getCommentColumn(), 1); 1783 O << TAI->getCommentString() << " SrcLine "; 1784 if (DLT.CompileUnit->hasInitializer()) { 1785 Constant *Name = DLT.CompileUnit->getInitializer(); 1786 if (ConstantArray *NameString = dyn_cast<ConstantArray>(Name)) 1787 if (NameString->isString()) { 1788 O << NameString->getAsString() << " "; 1789 } 1790 } 1791 O << DLT.Line; 1792 if (DLT.Col != 0) 1793 O << ":" << DLT.Col; 1794 } 1795 } 1796} 1797 1798/// EmitComments - Pretty-print comments for instructions 1799void AsmPrinter::EmitComments(const MCInst &MI) const 1800{ 1801 if (VerboseAsm) { 1802 if (!MI.getDebugLoc().isUnknown()) { 1803 DebugLocTuple DLT = MF->getDebugLocTuple(MI.getDebugLoc()); 1804 1805 // Print source line info 1806 O.PadToColumn(TAI->getCommentColumn(), 1); 1807 O << TAI->getCommentString() << " SrcLine "; 1808 if (DLT.CompileUnit->hasInitializer()) { 1809 Constant *Name = DLT.CompileUnit->getInitializer(); 1810 if (ConstantArray *NameString = dyn_cast<ConstantArray>(Name)) 1811 if (NameString->isString()) { 1812 O << NameString->getAsString() << " "; 1813 } 1814 } 1815 O << DLT.Line; 1816 if (DLT.Col != 0) 1817 O << ":" << DLT.Col; 1818 } 1819 } 1820} 1821