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