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