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