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