AsmPrinter.cpp revision 65a7bd8b8e111c51a51278ab46f1a1c91744441b
1//===-- AsmPrinter.cpp - Common AsmPrinter code ---------------------------===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9// 10// This file implements the AsmPrinter class. 11// 12//===----------------------------------------------------------------------===// 13 14#include "llvm/CodeGen/AsmPrinter.h" 15#include "llvm/Assembly/Writer.h" 16#include "llvm/DerivedTypes.h" 17#include "llvm/Constants.h" 18#include "llvm/Module.h" 19#include "llvm/CodeGen/GCMetadataPrinter.h" 20#include "llvm/CodeGen/MachineConstantPool.h" 21#include "llvm/CodeGen/MachineJumpTableInfo.h" 22#include "llvm/CodeGen/MachineModuleInfo.h" 23#include "llvm/Support/Mangler.h" 24#include "llvm/Support/MathExtras.h" 25#include "llvm/Support/Streams.h" 26#include "llvm/Target/TargetAsmInfo.h" 27#include "llvm/Target/TargetData.h" 28#include "llvm/Target/TargetLowering.h" 29#include "llvm/Target/TargetMachine.h" 30#include "llvm/Target/TargetOptions.h" 31#include "llvm/Target/TargetRegisterInfo.h" 32#include "llvm/ADT/SmallPtrSet.h" 33#include "llvm/ADT/SmallString.h" 34#include <cerrno> 35using namespace llvm; 36 37char AsmPrinter::ID = 0; 38AsmPrinter::AsmPrinter(std::ostream &o, TargetMachine &tm, 39 const TargetAsmInfo *T) 40 : MachineFunctionPass((intptr_t)&ID), FunctionNumber(0), O(o), 41 TM(tm), TAI(T), TRI(tm.getRegisterInfo()), 42 IsInTextSection(false) 43{} 44 45AsmPrinter::~AsmPrinter() { 46 for (gcp_iterator I = GCMetadataPrinters.begin(), 47 E = GCMetadataPrinters.end(); I != E; ++I) 48 delete I->second; 49} 50 51std::string AsmPrinter::getSectionForFunction(const Function &F) const { 52 return TAI->getTextSection(); 53} 54 55 56/// SwitchToTextSection - Switch to the specified text section of the executable 57/// if we are not already in it! 58/// 59void AsmPrinter::SwitchToTextSection(const char *NewSection, 60 const GlobalValue *GV) { 61 std::string NS; 62 if (GV && GV->hasSection()) 63 NS = TAI->getSwitchToSectionDirective() + GV->getSection(); 64 else 65 NS = NewSection; 66 67 // If we're already in this section, we're done. 68 if (CurrentSection == NS) return; 69 70 // Close the current section, if applicable. 71 if (TAI->getSectionEndDirectiveSuffix() && !CurrentSection.empty()) 72 O << CurrentSection << TAI->getSectionEndDirectiveSuffix() << '\n'; 73 74 CurrentSection = NS; 75 76 if (!CurrentSection.empty()) 77 O << CurrentSection << TAI->getTextSectionStartSuffix() << '\n'; 78 79 IsInTextSection = true; 80} 81 82/// SwitchToDataSection - Switch to the specified data section of the executable 83/// if we are not already in it! 84/// 85void AsmPrinter::SwitchToDataSection(const char *NewSection, 86 const GlobalValue *GV) { 87 std::string NS; 88 if (GV && GV->hasSection()) 89 NS = TAI->getSwitchToSectionDirective() + GV->getSection(); 90 else 91 NS = NewSection; 92 93 // If we're already in this section, we're done. 94 if (CurrentSection == NS) return; 95 96 // Close the current section, if applicable. 97 if (TAI->getSectionEndDirectiveSuffix() && !CurrentSection.empty()) 98 O << CurrentSection << TAI->getSectionEndDirectiveSuffix() << '\n'; 99 100 CurrentSection = NS; 101 102 if (!CurrentSection.empty()) 103 O << CurrentSection << TAI->getDataSectionStartSuffix() << '\n'; 104 105 IsInTextSection = false; 106} 107 108 109void AsmPrinter::getAnalysisUsage(AnalysisUsage &AU) const { 110 MachineFunctionPass::getAnalysisUsage(AU); 111 AU.addRequired<GCModuleInfo>(); 112} 113 114bool AsmPrinter::doInitialization(Module &M) { 115 Mang = new Mangler(M, TAI->getGlobalPrefix()); 116 117 GCModuleInfo *MI = getAnalysisToUpdate<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, *TAI); 122 123 if (!M.getModuleInlineAsm().empty()) 124 O << TAI->getCommentString() << " Start of file scope inline assembly\n" 125 << M.getModuleInlineAsm() 126 << '\n' << TAI->getCommentString() 127 << " End of file scope inline assembly\n"; 128 129 SwitchToDataSection(""); // Reset back to no section. 130 131 MMI = getAnalysisToUpdate<MachineModuleInfo>(); 132 if (MMI) MMI->AnalyzeModule(M); 133 134 return false; 135} 136 137bool AsmPrinter::doFinalization(Module &M) { 138 if (TAI->getWeakRefDirective()) { 139 if (!ExtWeakSymbols.empty()) 140 SwitchToDataSection(""); 141 142 for (std::set<const GlobalValue*>::iterator i = ExtWeakSymbols.begin(), 143 e = ExtWeakSymbols.end(); i != e; ++i) { 144 const GlobalValue *GV = *i; 145 std::string Name = Mang->getValueName(GV); 146 O << TAI->getWeakRefDirective() << Name << '\n'; 147 } 148 } 149 150 if (TAI->getSetDirective()) { 151 if (!M.alias_empty()) 152 SwitchToTextSection(TAI->getTextSection()); 153 154 O << '\n'; 155 for (Module::const_alias_iterator I = M.alias_begin(), E = M.alias_end(); 156 I!=E; ++I) { 157 std::string Name = Mang->getValueName(I); 158 std::string Target; 159 160 const GlobalValue *GV = cast<GlobalValue>(I->getAliasedGlobal()); 161 Target = Mang->getValueName(GV); 162 163 if (I->hasExternalLinkage() || !TAI->getWeakRefDirective()) 164 O << "\t.globl\t" << Name << '\n'; 165 else if (I->hasWeakLinkage()) 166 O << TAI->getWeakRefDirective() << Name << '\n'; 167 else if (!I->hasInternalLinkage()) 168 assert(0 && "Invalid alias linkage"); 169 170 if (I->hasHiddenVisibility()) { 171 if (const char *Directive = TAI->getHiddenDirective()) 172 O << Directive << Name << '\n'; 173 } else if (I->hasProtectedVisibility()) { 174 if (const char *Directive = TAI->getProtectedDirective()) 175 O << Directive << Name << '\n'; 176 } 177 178 O << TAI->getSetDirective() << ' ' << Name << ", " << Target << '\n'; 179 180 // If the aliasee has external weak linkage it can be referenced only by 181 // alias itself. In this case it can be not in ExtWeakSymbols list. Emit 182 // weak reference in such case. 183 if (GV->hasExternalWeakLinkage()) { 184 if (TAI->getWeakRefDirective()) 185 O << TAI->getWeakRefDirective() << Target << '\n'; 186 else 187 O << "\t.globl\t" << Target << '\n'; 188 } 189 } 190 } 191 192 GCModuleInfo *MI = getAnalysisToUpdate<GCModuleInfo>(); 193 assert(MI && "AsmPrinter didn't require GCModuleInfo?"); 194 for (GCModuleInfo::iterator I = MI->end(), E = MI->begin(); I != E; ) 195 if (GCMetadataPrinter *MP = GetOrCreateGCPrinter(*--I)) 196 MP->finishAssembly(O, *this, *TAI); 197 198 // If we don't have any trampolines, then we don't require stack memory 199 // to be executable. Some targets have a directive to declare this. 200 Function* InitTrampolineIntrinsic = M.getFunction("llvm.init.trampoline"); 201 if (!InitTrampolineIntrinsic || InitTrampolineIntrinsic->use_empty()) 202 if (TAI->getNonexecutableStackDirective()) 203 O << TAI->getNonexecutableStackDirective() << '\n'; 204 205 delete Mang; Mang = 0; 206 return false; 207} 208 209std::string AsmPrinter::getCurrentFunctionEHName(const MachineFunction *MF) { 210 assert(MF && "No machine function?"); 211 std::string Name = MF->getFunction()->getName(); 212 if (Name.empty()) 213 Name = Mang->getValueName(MF->getFunction()); 214 return Mang->makeNameProper(Name + ".eh", TAI->getGlobalPrefix()); 215} 216 217void AsmPrinter::SetupMachineFunction(MachineFunction &MF) { 218 // What's my mangled name? 219 CurrentFnName = Mang->getValueName(MF.getFunction()); 220 IncrementFunctionNumber(); 221} 222 223/// EmitConstantPool - Print to the current output stream assembly 224/// representations of the constants in the constant pool MCP. This is 225/// used to print out constants which have been "spilled to memory" by 226/// the code generator. 227/// 228void AsmPrinter::EmitConstantPool(MachineConstantPool *MCP) { 229 const std::vector<MachineConstantPoolEntry> &CP = MCP->getConstants(); 230 if (CP.empty()) return; 231 232 // Some targets require 4-, 8-, and 16- byte constant literals to be placed 233 // in special sections. 234 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > FourByteCPs; 235 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > EightByteCPs; 236 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > SixteenByteCPs; 237 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > OtherCPs; 238 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > TargetCPs; 239 for (unsigned i = 0, e = CP.size(); i != e; ++i) { 240 MachineConstantPoolEntry CPE = CP[i]; 241 const Type *Ty = CPE.getType(); 242 if (TAI->getFourByteConstantSection() && 243 TM.getTargetData()->getABITypeSize(Ty) == 4) 244 FourByteCPs.push_back(std::make_pair(CPE, i)); 245 else if (TAI->getEightByteConstantSection() && 246 TM.getTargetData()->getABITypeSize(Ty) == 8) 247 EightByteCPs.push_back(std::make_pair(CPE, i)); 248 else if (TAI->getSixteenByteConstantSection() && 249 TM.getTargetData()->getABITypeSize(Ty) == 16) 250 SixteenByteCPs.push_back(std::make_pair(CPE, i)); 251 else 252 OtherCPs.push_back(std::make_pair(CPE, i)); 253 } 254 255 unsigned Alignment = MCP->getConstantPoolAlignment(); 256 EmitConstantPool(Alignment, TAI->getFourByteConstantSection(), FourByteCPs); 257 EmitConstantPool(Alignment, TAI->getEightByteConstantSection(), EightByteCPs); 258 EmitConstantPool(Alignment, TAI->getSixteenByteConstantSection(), 259 SixteenByteCPs); 260 EmitConstantPool(Alignment, TAI->getConstantPoolSection(), OtherCPs); 261} 262 263void AsmPrinter::EmitConstantPool(unsigned Alignment, const char *Section, 264 std::vector<std::pair<MachineConstantPoolEntry,unsigned> > &CP) { 265 if (CP.empty()) return; 266 267 SwitchToDataSection(Section); 268 EmitAlignment(Alignment); 269 for (unsigned i = 0, e = CP.size(); i != e; ++i) { 270 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << '_' 271 << CP[i].second << ":\t\t\t\t\t" << TAI->getCommentString() << ' '; 272 WriteTypeSymbolic(O, CP[i].first.getType(), 0); 273 O << '\n'; 274 if (CP[i].first.isMachineConstantPoolEntry()) 275 EmitMachineConstantPoolValue(CP[i].first.Val.MachineCPVal); 276 else 277 EmitGlobalConstant(CP[i].first.Val.ConstVal); 278 if (i != e-1) { 279 const Type *Ty = CP[i].first.getType(); 280 unsigned EntSize = 281 TM.getTargetData()->getABITypeSize(Ty); 282 unsigned ValEnd = CP[i].first.getOffset() + EntSize; 283 // Emit inter-object padding for alignment. 284 EmitZeros(CP[i+1].first.getOffset()-ValEnd); 285 } 286 } 287} 288 289/// EmitJumpTableInfo - Print assembly representations of the jump tables used 290/// by the current function to the current output stream. 291/// 292void AsmPrinter::EmitJumpTableInfo(MachineJumpTableInfo *MJTI, 293 MachineFunction &MF) { 294 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables(); 295 if (JT.empty()) return; 296 297 bool IsPic = TM.getRelocationModel() == Reloc::PIC_; 298 299 // Pick the directive to use to print the jump table entries, and switch to 300 // the appropriate section. 301 TargetLowering *LoweringInfo = TM.getTargetLowering(); 302 303 const char* JumpTableDataSection = TAI->getJumpTableDataSection(); 304 const Function *F = MF.getFunction(); 305 unsigned SectionFlags = TAI->SectionFlagsForGlobal(F); 306 if ((IsPic && !(LoweringInfo && LoweringInfo->usesGlobalOffsetTable())) || 307 !JumpTableDataSection || 308 SectionFlags & SectionFlags::Linkonce) { 309 // In PIC mode, we need to emit the jump table to the same section as the 310 // function body itself, otherwise the label differences won't make sense. 311 // We should also do if the section name is NULL or function is declared in 312 // discardable section. 313 SwitchToTextSection(getSectionForFunction(*F).c_str(), F); 314 } else { 315 SwitchToDataSection(JumpTableDataSection); 316 } 317 318 EmitAlignment(Log2_32(MJTI->getAlignment())); 319 320 for (unsigned i = 0, e = JT.size(); i != e; ++i) { 321 const std::vector<MachineBasicBlock*> &JTBBs = JT[i].MBBs; 322 323 // If this jump table was deleted, ignore it. 324 if (JTBBs.empty()) continue; 325 326 // For PIC codegen, if possible we want to use the SetDirective to reduce 327 // the number of relocations the assembler will generate for the jump table. 328 // Set directives are all printed before the jump table itself. 329 SmallPtrSet<MachineBasicBlock*, 16> EmittedSets; 330 if (TAI->getSetDirective() && IsPic) 331 for (unsigned ii = 0, ee = JTBBs.size(); ii != ee; ++ii) 332 if (EmittedSets.insert(JTBBs[ii])) 333 printPICJumpTableSetLabel(i, JTBBs[ii]); 334 335 // On some targets (e.g. darwin) we want to emit two consequtive labels 336 // before each jump table. The first label is never referenced, but tells 337 // the assembler and linker the extents of the jump table object. The 338 // second label is actually referenced by the code. 339 if (const char *JTLabelPrefix = TAI->getJumpTableSpecialLabelPrefix()) 340 O << JTLabelPrefix << "JTI" << getFunctionNumber() << '_' << i << ":\n"; 341 342 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber() 343 << '_' << i << ":\n"; 344 345 for (unsigned ii = 0, ee = JTBBs.size(); ii != ee; ++ii) { 346 printPICJumpTableEntry(MJTI, JTBBs[ii], i); 347 O << '\n'; 348 } 349 } 350} 351 352void AsmPrinter::printPICJumpTableEntry(const MachineJumpTableInfo *MJTI, 353 const MachineBasicBlock *MBB, 354 unsigned uid) const { 355 bool IsPic = TM.getRelocationModel() == Reloc::PIC_; 356 357 // Use JumpTableDirective otherwise honor the entry size from the jump table 358 // info. 359 const char *JTEntryDirective = TAI->getJumpTableDirective(); 360 bool HadJTEntryDirective = JTEntryDirective != NULL; 361 if (!HadJTEntryDirective) { 362 JTEntryDirective = MJTI->getEntrySize() == 4 ? 363 TAI->getData32bitsDirective() : TAI->getData64bitsDirective(); 364 } 365 366 O << JTEntryDirective << ' '; 367 368 // If we have emitted set directives for the jump table entries, print 369 // them rather than the entries themselves. If we're emitting PIC, then 370 // emit the table entries as differences between two text section labels. 371 // If we're emitting non-PIC code, then emit the entries as direct 372 // references to the target basic blocks. 373 if (IsPic) { 374 if (TAI->getSetDirective()) { 375 O << TAI->getPrivateGlobalPrefix() << getFunctionNumber() 376 << '_' << uid << "_set_" << MBB->getNumber(); 377 } else { 378 printBasicBlockLabel(MBB, false, false, false); 379 // If the arch uses custom Jump Table directives, don't calc relative to 380 // JT 381 if (!HadJTEntryDirective) 382 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI" 383 << getFunctionNumber() << '_' << uid; 384 } 385 } else { 386 printBasicBlockLabel(MBB, false, false, false); 387 } 388} 389 390 391/// EmitSpecialLLVMGlobal - Check to see if the specified global is a 392/// special global used by LLVM. If so, emit it and return true, otherwise 393/// do nothing and return false. 394bool AsmPrinter::EmitSpecialLLVMGlobal(const GlobalVariable *GV) { 395 if (GV->getName() == "llvm.used") { 396 if (TAI->getUsedDirective() != 0) // No need to emit this at all. 397 EmitLLVMUsedList(GV->getInitializer()); 398 return true; 399 } 400 401 // Ignore debug and non-emitted data. 402 if (GV->getSection() == "llvm.metadata") return true; 403 404 if (!GV->hasAppendingLinkage()) return false; 405 406 assert(GV->hasInitializer() && "Not a special LLVM global!"); 407 408 const TargetData *TD = TM.getTargetData(); 409 unsigned Align = Log2_32(TD->getPointerPrefAlignment()); 410 if (GV->getName() == "llvm.global_ctors" && GV->use_empty()) { 411 SwitchToDataSection(TAI->getStaticCtorsSection()); 412 EmitAlignment(Align, 0); 413 EmitXXStructorList(GV->getInitializer()); 414 return true; 415 } 416 417 if (GV->getName() == "llvm.global_dtors" && GV->use_empty()) { 418 SwitchToDataSection(TAI->getStaticDtorsSection()); 419 EmitAlignment(Align, 0); 420 EmitXXStructorList(GV->getInitializer()); 421 return true; 422 } 423 424 return false; 425} 426 427/// EmitLLVMUsedList - For targets that define a TAI::UsedDirective, mark each 428/// global in the specified llvm.used list as being used with this directive. 429void AsmPrinter::EmitLLVMUsedList(Constant *List) { 430 const char *Directive = TAI->getUsedDirective(); 431 432 // Should be an array of 'sbyte*'. 433 ConstantArray *InitList = dyn_cast<ConstantArray>(List); 434 if (InitList == 0) return; 435 436 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) { 437 O << Directive; 438 EmitConstantValueOnly(InitList->getOperand(i)); 439 O << '\n'; 440 } 441} 442 443/// EmitXXStructorList - Emit the ctor or dtor list. This just prints out the 444/// function pointers, ignoring the init priority. 445void AsmPrinter::EmitXXStructorList(Constant *List) { 446 // Should be an array of '{ int, void ()* }' structs. The first value is the 447 // init priority, which we ignore. 448 if (!isa<ConstantArray>(List)) return; 449 ConstantArray *InitList = cast<ConstantArray>(List); 450 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) 451 if (ConstantStruct *CS = dyn_cast<ConstantStruct>(InitList->getOperand(i))){ 452 if (CS->getNumOperands() != 2) return; // Not array of 2-element structs. 453 454 if (CS->getOperand(1)->isNullValue()) 455 return; // Found a null terminator, exit printing. 456 // Emit the function pointer. 457 EmitGlobalConstant(CS->getOperand(1)); 458 } 459} 460 461/// getGlobalLinkName - Returns the asm/link name of of the specified 462/// global variable. Should be overridden by each target asm printer to 463/// generate the appropriate value. 464const std::string AsmPrinter::getGlobalLinkName(const GlobalVariable *GV) const{ 465 std::string LinkName; 466 467 if (isa<Function>(GV)) { 468 LinkName += TAI->getFunctionAddrPrefix(); 469 LinkName += Mang->getValueName(GV); 470 LinkName += TAI->getFunctionAddrSuffix(); 471 } else { 472 LinkName += TAI->getGlobalVarAddrPrefix(); 473 LinkName += Mang->getValueName(GV); 474 LinkName += TAI->getGlobalVarAddrSuffix(); 475 } 476 477 return LinkName; 478} 479 480/// EmitExternalGlobal - Emit the external reference to a global variable. 481/// Should be overridden if an indirect reference should be used. 482void AsmPrinter::EmitExternalGlobal(const GlobalVariable *GV) { 483 O << getGlobalLinkName(GV); 484} 485 486 487 488//===----------------------------------------------------------------------===// 489/// LEB 128 number encoding. 490 491/// PrintULEB128 - Print a series of hexidecimal values (separated by commas) 492/// representing an unsigned leb128 value. 493void AsmPrinter::PrintULEB128(unsigned Value) const { 494 do { 495 unsigned Byte = Value & 0x7f; 496 Value >>= 7; 497 if (Value) Byte |= 0x80; 498 O << "0x" << std::hex << Byte << std::dec; 499 if (Value) O << ", "; 500 } while (Value); 501} 502 503/// PrintSLEB128 - Print a series of hexidecimal values (separated by commas) 504/// representing a signed leb128 value. 505void AsmPrinter::PrintSLEB128(int Value) const { 506 int Sign = Value >> (8 * sizeof(Value) - 1); 507 bool IsMore; 508 509 do { 510 unsigned Byte = Value & 0x7f; 511 Value >>= 7; 512 IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0; 513 if (IsMore) Byte |= 0x80; 514 O << "0x" << std::hex << Byte << std::dec; 515 if (IsMore) O << ", "; 516 } while (IsMore); 517} 518 519//===--------------------------------------------------------------------===// 520// Emission and print routines 521// 522 523/// PrintHex - Print a value as a hexidecimal value. 524/// 525void AsmPrinter::PrintHex(int Value) const { 526 O << "0x" << std::hex << Value << std::dec; 527} 528 529/// EOL - Print a newline character to asm stream. If a comment is present 530/// then it will be printed first. Comments should not contain '\n'. 531void AsmPrinter::EOL() const { 532 O << '\n'; 533} 534 535void AsmPrinter::EOL(const std::string &Comment) const { 536 if (VerboseAsm && !Comment.empty()) { 537 O << '\t' 538 << TAI->getCommentString() 539 << ' ' 540 << Comment; 541 } 542 O << '\n'; 543} 544 545void AsmPrinter::EOL(const char* Comment) const { 546 if (VerboseAsm && *Comment) { 547 O << '\t' 548 << TAI->getCommentString() 549 << ' ' 550 << Comment; 551 } 552 O << '\n'; 553} 554 555/// EmitULEB128Bytes - Emit an assembler byte data directive to compose an 556/// unsigned leb128 value. 557void AsmPrinter::EmitULEB128Bytes(unsigned Value) const { 558 if (TAI->hasLEB128()) { 559 O << "\t.uleb128\t" 560 << Value; 561 } else { 562 O << TAI->getData8bitsDirective(); 563 PrintULEB128(Value); 564 } 565} 566 567/// EmitSLEB128Bytes - print an assembler byte data directive to compose a 568/// signed leb128 value. 569void AsmPrinter::EmitSLEB128Bytes(int Value) const { 570 if (TAI->hasLEB128()) { 571 O << "\t.sleb128\t" 572 << Value; 573 } else { 574 O << TAI->getData8bitsDirective(); 575 PrintSLEB128(Value); 576 } 577} 578 579/// EmitInt8 - Emit a byte directive and value. 580/// 581void AsmPrinter::EmitInt8(int Value) const { 582 O << TAI->getData8bitsDirective(); 583 PrintHex(Value & 0xFF); 584} 585 586/// EmitInt16 - Emit a short directive and value. 587/// 588void AsmPrinter::EmitInt16(int Value) const { 589 O << TAI->getData16bitsDirective(); 590 PrintHex(Value & 0xFFFF); 591} 592 593/// EmitInt32 - Emit a long directive and value. 594/// 595void AsmPrinter::EmitInt32(int Value) const { 596 O << TAI->getData32bitsDirective(); 597 PrintHex(Value); 598} 599 600/// EmitInt64 - Emit a long long directive and value. 601/// 602void AsmPrinter::EmitInt64(uint64_t Value) const { 603 if (TAI->getData64bitsDirective()) { 604 O << TAI->getData64bitsDirective(); 605 PrintHex(Value); 606 } else { 607 if (TM.getTargetData()->isBigEndian()) { 608 EmitInt32(unsigned(Value >> 32)); O << '\n'; 609 EmitInt32(unsigned(Value)); 610 } else { 611 EmitInt32(unsigned(Value)); O << '\n'; 612 EmitInt32(unsigned(Value >> 32)); 613 } 614 } 615} 616 617/// toOctal - Convert the low order bits of X into an octal digit. 618/// 619static inline char toOctal(int X) { 620 return (X&7)+'0'; 621} 622 623/// printStringChar - Print a char, escaped if necessary. 624/// 625static void printStringChar(std::ostream &O, unsigned char C) { 626 if (C == '"') { 627 O << "\\\""; 628 } else if (C == '\\') { 629 O << "\\\\"; 630 } else if (isprint(C)) { 631 O << C; 632 } else { 633 switch(C) { 634 case '\b': O << "\\b"; break; 635 case '\f': O << "\\f"; break; 636 case '\n': O << "\\n"; break; 637 case '\r': O << "\\r"; break; 638 case '\t': O << "\\t"; break; 639 default: 640 O << '\\'; 641 O << toOctal(C >> 6); 642 O << toOctal(C >> 3); 643 O << toOctal(C >> 0); 644 break; 645 } 646 } 647} 648 649/// EmitString - Emit a string with quotes and a null terminator. 650/// Special characters are emitted properly. 651/// \literal (Eg. '\t') \endliteral 652void AsmPrinter::EmitString(const std::string &String) const { 653 const char* AscizDirective = TAI->getAscizDirective(); 654 if (AscizDirective) 655 O << AscizDirective; 656 else 657 O << TAI->getAsciiDirective(); 658 O << '\"'; 659 for (unsigned i = 0, N = String.size(); i < N; ++i) { 660 unsigned char C = String[i]; 661 printStringChar(O, C); 662 } 663 if (AscizDirective) 664 O << '\"'; 665 else 666 O << "\\0\""; 667} 668 669 670/// EmitFile - Emit a .file directive. 671void AsmPrinter::EmitFile(unsigned Number, const std::string &Name) const { 672 O << "\t.file\t" << Number << " \""; 673 for (unsigned i = 0, N = Name.size(); i < N; ++i) { 674 unsigned char C = Name[i]; 675 printStringChar(O, C); 676 } 677 O << '\"'; 678} 679 680 681//===----------------------------------------------------------------------===// 682 683// EmitAlignment - Emit an alignment directive to the specified power of 684// two boundary. For example, if you pass in 3 here, you will get an 8 685// byte alignment. If a global value is specified, and if that global has 686// an explicit alignment requested, it will unconditionally override the 687// alignment request. However, if ForcedAlignBits is specified, this value 688// has final say: the ultimate alignment will be the max of ForcedAlignBits 689// and the alignment computed with NumBits and the global. 690// 691// The algorithm is: 692// Align = NumBits; 693// if (GV && GV->hasalignment) Align = GV->getalignment(); 694// Align = std::max(Align, ForcedAlignBits); 695// 696void AsmPrinter::EmitAlignment(unsigned NumBits, const GlobalValue *GV, 697 unsigned ForcedAlignBits, 698 bool UseFillExpr) const { 699 if (GV && GV->getAlignment()) 700 NumBits = Log2_32(GV->getAlignment()); 701 NumBits = std::max(NumBits, ForcedAlignBits); 702 703 if (NumBits == 0) return; // No need to emit alignment. 704 if (TAI->getAlignmentIsInBytes()) NumBits = 1 << NumBits; 705 O << TAI->getAlignDirective() << NumBits; 706 707 unsigned FillValue = TAI->getTextAlignFillValue(); 708 UseFillExpr &= IsInTextSection && FillValue; 709 if (UseFillExpr) O << ",0x" << std::hex << FillValue << std::dec; 710 O << '\n'; 711} 712 713 714/// EmitZeros - Emit a block of zeros. 715/// 716void AsmPrinter::EmitZeros(uint64_t NumZeros) const { 717 if (NumZeros) { 718 if (TAI->getZeroDirective()) { 719 O << TAI->getZeroDirective() << NumZeros; 720 if (TAI->getZeroDirectiveSuffix()) 721 O << TAI->getZeroDirectiveSuffix(); 722 O << '\n'; 723 } else { 724 for (; NumZeros; --NumZeros) 725 O << TAI->getData8bitsDirective() << "0\n"; 726 } 727 } 728} 729 730// Print out the specified constant, without a storage class. Only the 731// constants valid in constant expressions can occur here. 732void AsmPrinter::EmitConstantValueOnly(const Constant *CV) { 733 if (CV->isNullValue() || isa<UndefValue>(CV)) 734 O << '0'; 735 else if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) { 736 O << CI->getZExtValue(); 737 } else if (const GlobalValue *GV = dyn_cast<GlobalValue>(CV)) { 738 // This is a constant address for a global variable or function. Use the 739 // name of the variable or function as the address value, possibly 740 // decorating it with GlobalVarAddrPrefix/Suffix or 741 // FunctionAddrPrefix/Suffix (these all default to "" ) 742 if (isa<Function>(GV)) { 743 O << TAI->getFunctionAddrPrefix() 744 << Mang->getValueName(GV) 745 << TAI->getFunctionAddrSuffix(); 746 } else { 747 O << TAI->getGlobalVarAddrPrefix() 748 << Mang->getValueName(GV) 749 << TAI->getGlobalVarAddrSuffix(); 750 } 751 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) { 752 const TargetData *TD = TM.getTargetData(); 753 unsigned Opcode = CE->getOpcode(); 754 switch (Opcode) { 755 case Instruction::GetElementPtr: { 756 // generate a symbolic expression for the byte address 757 const Constant *ptrVal = CE->getOperand(0); 758 SmallVector<Value*, 8> idxVec(CE->op_begin()+1, CE->op_end()); 759 if (int64_t Offset = TD->getIndexedOffset(ptrVal->getType(), &idxVec[0], 760 idxVec.size())) { 761 if (Offset) 762 O << '('; 763 EmitConstantValueOnly(ptrVal); 764 if (Offset > 0) 765 O << ") + " << Offset; 766 else if (Offset < 0) 767 O << ") - " << -Offset; 768 } else { 769 EmitConstantValueOnly(ptrVal); 770 } 771 break; 772 } 773 case Instruction::Trunc: 774 case Instruction::ZExt: 775 case Instruction::SExt: 776 case Instruction::FPTrunc: 777 case Instruction::FPExt: 778 case Instruction::UIToFP: 779 case Instruction::SIToFP: 780 case Instruction::FPToUI: 781 case Instruction::FPToSI: 782 assert(0 && "FIXME: Don't yet support this kind of constant cast expr"); 783 break; 784 case Instruction::BitCast: 785 return EmitConstantValueOnly(CE->getOperand(0)); 786 787 case Instruction::IntToPtr: { 788 // Handle casts to pointers by changing them into casts to the appropriate 789 // integer type. This promotes constant folding and simplifies this code. 790 Constant *Op = CE->getOperand(0); 791 Op = ConstantExpr::getIntegerCast(Op, TD->getIntPtrType(), false/*ZExt*/); 792 return EmitConstantValueOnly(Op); 793 } 794 795 796 case Instruction::PtrToInt: { 797 // Support only foldable casts to/from pointers that can be eliminated by 798 // changing the pointer to the appropriately sized integer type. 799 Constant *Op = CE->getOperand(0); 800 const Type *Ty = CE->getType(); 801 802 // We can emit the pointer value into this slot if the slot is an 803 // integer slot greater or equal to the size of the pointer. 804 if (TD->getABITypeSize(Ty) >= TD->getABITypeSize(Op->getType())) 805 return EmitConstantValueOnly(Op); 806 807 O << "(("; 808 EmitConstantValueOnly(Op); 809 APInt ptrMask = APInt::getAllOnesValue(TD->getABITypeSizeInBits(Ty)); 810 811 SmallString<40> S; 812 ptrMask.toStringUnsigned(S); 813 O << ") & " << S.c_str() << ')'; 814 break; 815 } 816 case Instruction::Add: 817 case Instruction::Sub: 818 case Instruction::And: 819 case Instruction::Or: 820 case Instruction::Xor: 821 O << '('; 822 EmitConstantValueOnly(CE->getOperand(0)); 823 O << ')'; 824 switch (Opcode) { 825 case Instruction::Add: 826 O << " + "; 827 break; 828 case Instruction::Sub: 829 O << " - "; 830 break; 831 case Instruction::And: 832 O << " & "; 833 break; 834 case Instruction::Or: 835 O << " | "; 836 break; 837 case Instruction::Xor: 838 O << " ^ "; 839 break; 840 default: 841 break; 842 } 843 O << '('; 844 EmitConstantValueOnly(CE->getOperand(1)); 845 O << ')'; 846 break; 847 default: 848 assert(0 && "Unsupported operator!"); 849 } 850 } else { 851 assert(0 && "Unknown constant value!"); 852 } 853} 854 855/// printAsCString - Print the specified array as a C compatible string, only if 856/// the predicate isString is true. 857/// 858static void printAsCString(std::ostream &O, const ConstantArray *CVA, 859 unsigned LastElt) { 860 assert(CVA->isString() && "Array is not string compatible!"); 861 862 O << '\"'; 863 for (unsigned i = 0; i != LastElt; ++i) { 864 unsigned char C = 865 (unsigned char)cast<ConstantInt>(CVA->getOperand(i))->getZExtValue(); 866 printStringChar(O, C); 867 } 868 O << '\"'; 869} 870 871/// EmitString - Emit a zero-byte-terminated string constant. 872/// 873void AsmPrinter::EmitString(const ConstantArray *CVA) const { 874 unsigned NumElts = CVA->getNumOperands(); 875 if (TAI->getAscizDirective() && NumElts && 876 cast<ConstantInt>(CVA->getOperand(NumElts-1))->getZExtValue() == 0) { 877 O << TAI->getAscizDirective(); 878 printAsCString(O, CVA, NumElts-1); 879 } else { 880 O << TAI->getAsciiDirective(); 881 printAsCString(O, CVA, NumElts); 882 } 883 O << '\n'; 884} 885 886/// EmitGlobalConstant - Print a general LLVM constant to the .s file. 887void AsmPrinter::EmitGlobalConstant(const Constant *CV) { 888 const TargetData *TD = TM.getTargetData(); 889 unsigned Size = TD->getABITypeSize(CV->getType()); 890 891 if (CV->isNullValue() || isa<UndefValue>(CV)) { 892 EmitZeros(Size); 893 return; 894 } else if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) { 895 if (CVA->isString()) { 896 EmitString(CVA); 897 } else { // Not a string. Print the values in successive locations 898 for (unsigned i = 0, e = CVA->getNumOperands(); i != e; ++i) 899 EmitGlobalConstant(CVA->getOperand(i)); 900 } 901 return; 902 } else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV)) { 903 // Print the fields in successive locations. Pad to align if needed! 904 const StructLayout *cvsLayout = TD->getStructLayout(CVS->getType()); 905 uint64_t sizeSoFar = 0; 906 for (unsigned i = 0, e = CVS->getNumOperands(); i != e; ++i) { 907 const Constant* field = CVS->getOperand(i); 908 909 // Check if padding is needed and insert one or more 0s. 910 uint64_t fieldSize = TD->getABITypeSize(field->getType()); 911 uint64_t padSize = ((i == e-1 ? Size : cvsLayout->getElementOffset(i+1)) 912 - cvsLayout->getElementOffset(i)) - fieldSize; 913 sizeSoFar += fieldSize + padSize; 914 915 // Now print the actual field value. 916 EmitGlobalConstant(field); 917 918 // Insert padding - this may include padding to increase the size of the 919 // current field up to the ABI size (if the struct is not packed) as well 920 // as padding to ensure that the next field starts at the right offset. 921 EmitZeros(padSize); 922 } 923 assert(sizeSoFar == cvsLayout->getSizeInBytes() && 924 "Layout of constant struct may be incorrect!"); 925 return; 926 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) { 927 // FP Constants are printed as integer constants to avoid losing 928 // precision... 929 if (CFP->getType() == Type::DoubleTy) { 930 double Val = CFP->getValueAPF().convertToDouble(); // for comment only 931 uint64_t i = CFP->getValueAPF().convertToAPInt().getZExtValue(); 932 if (TAI->getData64bitsDirective()) 933 O << TAI->getData64bitsDirective() << i << '\t' 934 << TAI->getCommentString() << " double value: " << Val << '\n'; 935 else if (TD->isBigEndian()) { 936 O << TAI->getData32bitsDirective() << unsigned(i >> 32) 937 << '\t' << TAI->getCommentString() 938 << " double most significant word " << Val << '\n'; 939 O << TAI->getData32bitsDirective() << unsigned(i) 940 << '\t' << TAI->getCommentString() 941 << " double least significant word " << Val << '\n'; 942 } else { 943 O << TAI->getData32bitsDirective() << unsigned(i) 944 << '\t' << TAI->getCommentString() 945 << " double least significant word " << Val << '\n'; 946 O << TAI->getData32bitsDirective() << unsigned(i >> 32) 947 << '\t' << TAI->getCommentString() 948 << " double most significant word " << Val << '\n'; 949 } 950 return; 951 } else if (CFP->getType() == Type::FloatTy) { 952 float Val = CFP->getValueAPF().convertToFloat(); // for comment only 953 O << TAI->getData32bitsDirective() 954 << CFP->getValueAPF().convertToAPInt().getZExtValue() 955 << '\t' << TAI->getCommentString() << " float " << Val << '\n'; 956 return; 957 } else if (CFP->getType() == Type::X86_FP80Ty) { 958 // all long double variants are printed as hex 959 // api needed to prevent premature destruction 960 APInt api = CFP->getValueAPF().convertToAPInt(); 961 const uint64_t *p = api.getRawData(); 962 APFloat DoubleVal = CFP->getValueAPF(); 963 DoubleVal.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven); 964 if (TD->isBigEndian()) { 965 O << TAI->getData16bitsDirective() << uint16_t(p[0] >> 48) 966 << '\t' << TAI->getCommentString() 967 << " long double most significant halfword of ~" 968 << DoubleVal.convertToDouble() << '\n'; 969 O << TAI->getData16bitsDirective() << uint16_t(p[0] >> 32) 970 << '\t' << TAI->getCommentString() 971 << " long double next halfword\n"; 972 O << TAI->getData16bitsDirective() << uint16_t(p[0] >> 16) 973 << '\t' << TAI->getCommentString() 974 << " long double next halfword\n"; 975 O << TAI->getData16bitsDirective() << uint16_t(p[0]) 976 << '\t' << TAI->getCommentString() 977 << " long double next halfword\n"; 978 O << TAI->getData16bitsDirective() << uint16_t(p[1]) 979 << '\t' << TAI->getCommentString() 980 << " long double least significant halfword\n"; 981 } else { 982 O << TAI->getData16bitsDirective() << uint16_t(p[1]) 983 << '\t' << TAI->getCommentString() 984 << " long double least significant halfword of ~" 985 << DoubleVal.convertToDouble() << '\n'; 986 O << TAI->getData16bitsDirective() << uint16_t(p[0]) 987 << '\t' << TAI->getCommentString() 988 << " long double next halfword\n"; 989 O << TAI->getData16bitsDirective() << uint16_t(p[0] >> 16) 990 << '\t' << TAI->getCommentString() 991 << " long double next halfword\n"; 992 O << TAI->getData16bitsDirective() << uint16_t(p[0] >> 32) 993 << '\t' << TAI->getCommentString() 994 << " long double next halfword\n"; 995 O << TAI->getData16bitsDirective() << uint16_t(p[0] >> 48) 996 << '\t' << TAI->getCommentString() 997 << " long double most significant halfword\n"; 998 } 999 EmitZeros(Size - TD->getTypeStoreSize(Type::X86_FP80Ty)); 1000 return; 1001 } else if (CFP->getType() == Type::PPC_FP128Ty) { 1002 // all long double variants are printed as hex 1003 // api needed to prevent premature destruction 1004 APInt api = CFP->getValueAPF().convertToAPInt(); 1005 const uint64_t *p = api.getRawData(); 1006 if (TD->isBigEndian()) { 1007 O << TAI->getData32bitsDirective() << uint32_t(p[0] >> 32) 1008 << '\t' << TAI->getCommentString() 1009 << " long double most significant word\n"; 1010 O << TAI->getData32bitsDirective() << uint32_t(p[0]) 1011 << '\t' << TAI->getCommentString() 1012 << " long double next word\n"; 1013 O << TAI->getData32bitsDirective() << uint32_t(p[1] >> 32) 1014 << '\t' << TAI->getCommentString() 1015 << " long double next word\n"; 1016 O << TAI->getData32bitsDirective() << uint32_t(p[1]) 1017 << '\t' << TAI->getCommentString() 1018 << " long double least significant word\n"; 1019 } else { 1020 O << TAI->getData32bitsDirective() << uint32_t(p[1]) 1021 << '\t' << TAI->getCommentString() 1022 << " long double least significant word\n"; 1023 O << TAI->getData32bitsDirective() << uint32_t(p[1] >> 32) 1024 << '\t' << TAI->getCommentString() 1025 << " long double next word\n"; 1026 O << TAI->getData32bitsDirective() << uint32_t(p[0]) 1027 << '\t' << TAI->getCommentString() 1028 << " long double next word\n"; 1029 O << TAI->getData32bitsDirective() << uint32_t(p[0] >> 32) 1030 << '\t' << TAI->getCommentString() 1031 << " long double most significant word\n"; 1032 } 1033 return; 1034 } else assert(0 && "Floating point constant type not handled"); 1035 } else if (CV->getType() == Type::Int64Ty) { 1036 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) { 1037 uint64_t Val = CI->getZExtValue(); 1038 1039 if (TAI->getData64bitsDirective()) 1040 O << TAI->getData64bitsDirective() << Val << '\n'; 1041 else if (TD->isBigEndian()) { 1042 O << TAI->getData32bitsDirective() << unsigned(Val >> 32) 1043 << '\t' << TAI->getCommentString() 1044 << " Double-word most significant word " << Val << '\n'; 1045 O << TAI->getData32bitsDirective() << unsigned(Val) 1046 << '\t' << TAI->getCommentString() 1047 << " Double-word least significant word " << Val << '\n'; 1048 } else { 1049 O << TAI->getData32bitsDirective() << unsigned(Val) 1050 << '\t' << TAI->getCommentString() 1051 << " Double-word least significant word " << Val << '\n'; 1052 O << TAI->getData32bitsDirective() << unsigned(Val >> 32) 1053 << '\t' << TAI->getCommentString() 1054 << " Double-word most significant word " << Val << '\n'; 1055 } 1056 return; 1057 } 1058 } else if (const ConstantVector *CP = dyn_cast<ConstantVector>(CV)) { 1059 const VectorType *PTy = CP->getType(); 1060 1061 for (unsigned I = 0, E = PTy->getNumElements(); I < E; ++I) 1062 EmitGlobalConstant(CP->getOperand(I)); 1063 1064 return; 1065 } 1066 1067 const Type *type = CV->getType(); 1068 printDataDirective(type); 1069 EmitConstantValueOnly(CV); 1070 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) { 1071 SmallString<40> S; 1072 CI->getValue().toStringUnsigned(S, 16); 1073 O << "\t\t\t" << TAI->getCommentString() << " 0x" << S.c_str(); 1074 } 1075 O << '\n'; 1076} 1077 1078void AsmPrinter::EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV) { 1079 // Target doesn't support this yet! 1080 abort(); 1081} 1082 1083/// PrintSpecial - Print information related to the specified machine instr 1084/// that is independent of the operand, and may be independent of the instr 1085/// itself. This can be useful for portably encoding the comment character 1086/// or other bits of target-specific knowledge into the asmstrings. The 1087/// syntax used is ${:comment}. Targets can override this to add support 1088/// for their own strange codes. 1089void AsmPrinter::PrintSpecial(const MachineInstr *MI, const char *Code) { 1090 if (!strcmp(Code, "private")) { 1091 O << TAI->getPrivateGlobalPrefix(); 1092 } else if (!strcmp(Code, "comment")) { 1093 O << TAI->getCommentString(); 1094 } else if (!strcmp(Code, "uid")) { 1095 // Assign a unique ID to this machine instruction. 1096 static const MachineInstr *LastMI = 0; 1097 static const Function *F = 0; 1098 static unsigned Counter = 0U-1; 1099 1100 // Comparing the address of MI isn't sufficient, because machineinstrs may 1101 // be allocated to the same address across functions. 1102 const Function *ThisF = MI->getParent()->getParent()->getFunction(); 1103 1104 // If this is a new machine instruction, bump the counter. 1105 if (LastMI != MI || F != ThisF) { 1106 ++Counter; 1107 LastMI = MI; 1108 F = ThisF; 1109 } 1110 O << Counter; 1111 } else { 1112 cerr << "Unknown special formatter '" << Code 1113 << "' for machine instr: " << *MI; 1114 exit(1); 1115 } 1116} 1117 1118 1119/// printInlineAsm - This method formats and prints the specified machine 1120/// instruction that is an inline asm. 1121void AsmPrinter::printInlineAsm(const MachineInstr *MI) const { 1122 unsigned NumOperands = MI->getNumOperands(); 1123 1124 // Count the number of register definitions. 1125 unsigned NumDefs = 0; 1126 for (; MI->getOperand(NumDefs).isRegister() && MI->getOperand(NumDefs).isDef(); 1127 ++NumDefs) 1128 assert(NumDefs != NumOperands-1 && "No asm string?"); 1129 1130 assert(MI->getOperand(NumDefs).isExternalSymbol() && "No asm string?"); 1131 1132 // Disassemble the AsmStr, printing out the literal pieces, the operands, etc. 1133 const char *AsmStr = MI->getOperand(NumDefs).getSymbolName(); 1134 1135 // If this asmstr is empty, just print the #APP/#NOAPP markers. 1136 // These are useful to see where empty asm's wound up. 1137 if (AsmStr[0] == 0) { 1138 O << TAI->getInlineAsmStart() << "\n\t" << TAI->getInlineAsmEnd() << '\n'; 1139 return; 1140 } 1141 1142 O << TAI->getInlineAsmStart() << "\n\t"; 1143 1144 // The variant of the current asmprinter. 1145 int AsmPrinterVariant = TAI->getAssemblerDialect(); 1146 1147 int CurVariant = -1; // The number of the {.|.|.} region we are in. 1148 const char *LastEmitted = AsmStr; // One past the last character emitted. 1149 1150 while (*LastEmitted) { 1151 switch (*LastEmitted) { 1152 default: { 1153 // Not a special case, emit the string section literally. 1154 const char *LiteralEnd = LastEmitted+1; 1155 while (*LiteralEnd && *LiteralEnd != '{' && *LiteralEnd != '|' && 1156 *LiteralEnd != '}' && *LiteralEnd != '$' && *LiteralEnd != '\n') 1157 ++LiteralEnd; 1158 if (CurVariant == -1 || CurVariant == AsmPrinterVariant) 1159 O.write(LastEmitted, LiteralEnd-LastEmitted); 1160 LastEmitted = LiteralEnd; 1161 break; 1162 } 1163 case '\n': 1164 ++LastEmitted; // Consume newline character. 1165 O << '\n'; // Indent code with newline. 1166 break; 1167 case '$': { 1168 ++LastEmitted; // Consume '$' character. 1169 bool Done = true; 1170 1171 // Handle escapes. 1172 switch (*LastEmitted) { 1173 default: Done = false; break; 1174 case '$': // $$ -> $ 1175 if (CurVariant == -1 || CurVariant == AsmPrinterVariant) 1176 O << '$'; 1177 ++LastEmitted; // Consume second '$' character. 1178 break; 1179 case '(': // $( -> same as GCC's { character. 1180 ++LastEmitted; // Consume '(' character. 1181 if (CurVariant != -1) { 1182 cerr << "Nested variants found in inline asm string: '" 1183 << AsmStr << "'\n"; 1184 exit(1); 1185 } 1186 CurVariant = 0; // We're in the first variant now. 1187 break; 1188 case '|': 1189 ++LastEmitted; // consume '|' character. 1190 if (CurVariant == -1) { 1191 cerr << "Found '|' character outside of variant in inline asm " 1192 << "string: '" << AsmStr << "'\n"; 1193 exit(1); 1194 } 1195 ++CurVariant; // We're in the next variant. 1196 break; 1197 case ')': // $) -> same as GCC's } char. 1198 ++LastEmitted; // consume ')' character. 1199 if (CurVariant == -1) { 1200 cerr << "Found '}' character outside of variant in inline asm " 1201 << "string: '" << AsmStr << "'\n"; 1202 exit(1); 1203 } 1204 CurVariant = -1; 1205 break; 1206 } 1207 if (Done) break; 1208 1209 bool HasCurlyBraces = false; 1210 if (*LastEmitted == '{') { // ${variable} 1211 ++LastEmitted; // Consume '{' character. 1212 HasCurlyBraces = true; 1213 } 1214 1215 const char *IDStart = LastEmitted; 1216 char *IDEnd; 1217 errno = 0; 1218 long Val = strtol(IDStart, &IDEnd, 10); // We only accept numbers for IDs. 1219 if (!isdigit(*IDStart) || (Val == 0 && errno == EINVAL)) { 1220 cerr << "Bad $ operand number in inline asm string: '" 1221 << AsmStr << "'\n"; 1222 exit(1); 1223 } 1224 LastEmitted = IDEnd; 1225 1226 char Modifier[2] = { 0, 0 }; 1227 1228 if (HasCurlyBraces) { 1229 // If we have curly braces, check for a modifier character. This 1230 // supports syntax like ${0:u}, which correspond to "%u0" in GCC asm. 1231 if (*LastEmitted == ':') { 1232 ++LastEmitted; // Consume ':' character. 1233 if (*LastEmitted == 0) { 1234 cerr << "Bad ${:} expression in inline asm string: '" 1235 << AsmStr << "'\n"; 1236 exit(1); 1237 } 1238 1239 Modifier[0] = *LastEmitted; 1240 ++LastEmitted; // Consume modifier character. 1241 } 1242 1243 if (*LastEmitted != '}') { 1244 cerr << "Bad ${} expression in inline asm string: '" 1245 << AsmStr << "'\n"; 1246 exit(1); 1247 } 1248 ++LastEmitted; // Consume '}' character. 1249 } 1250 1251 if ((unsigned)Val >= NumOperands-1) { 1252 cerr << "Invalid $ operand number in inline asm string: '" 1253 << AsmStr << "'\n"; 1254 exit(1); 1255 } 1256 1257 // Okay, we finally have a value number. Ask the target to print this 1258 // operand! 1259 if (CurVariant == -1 || CurVariant == AsmPrinterVariant) { 1260 unsigned OpNo = 1; 1261 1262 bool Error = false; 1263 1264 // Scan to find the machine operand number for the operand. 1265 for (; Val; --Val) { 1266 if (OpNo >= MI->getNumOperands()) break; 1267 unsigned OpFlags = MI->getOperand(OpNo).getImm(); 1268 OpNo += (OpFlags >> 3) + 1; 1269 } 1270 1271 if (OpNo >= MI->getNumOperands()) { 1272 Error = true; 1273 } else { 1274 unsigned OpFlags = MI->getOperand(OpNo).getImm(); 1275 ++OpNo; // Skip over the ID number. 1276 1277 if (Modifier[0]=='l') // labels are target independent 1278 printBasicBlockLabel(MI->getOperand(OpNo).getMBB(), 1279 false, false, false); 1280 else { 1281 AsmPrinter *AP = const_cast<AsmPrinter*>(this); 1282 if ((OpFlags & 7) == 4 /*ADDR MODE*/) { 1283 Error = AP->PrintAsmMemoryOperand(MI, OpNo, AsmPrinterVariant, 1284 Modifier[0] ? Modifier : 0); 1285 } else { 1286 Error = AP->PrintAsmOperand(MI, OpNo, AsmPrinterVariant, 1287 Modifier[0] ? Modifier : 0); 1288 } 1289 } 1290 } 1291 if (Error) { 1292 cerr << "Invalid operand found in inline asm: '" 1293 << AsmStr << "'\n"; 1294 MI->dump(); 1295 exit(1); 1296 } 1297 } 1298 break; 1299 } 1300 } 1301 } 1302 O << "\n\t" << TAI->getInlineAsmEnd() << '\n'; 1303} 1304 1305/// printImplicitDef - This method prints the specified machine instruction 1306/// that is an implicit def. 1307void AsmPrinter::printImplicitDef(const MachineInstr *MI) const { 1308 O << '\t' << TAI->getCommentString() << " implicit-def: " 1309 << TRI->getAsmName(MI->getOperand(0).getReg()) << '\n'; 1310} 1311 1312/// printLabel - This method prints a local label used by debug and 1313/// exception handling tables. 1314void AsmPrinter::printLabel(const MachineInstr *MI) const { 1315 printLabel(MI->getOperand(0).getImm()); 1316} 1317 1318void AsmPrinter::printLabel(unsigned Id) const { 1319 O << TAI->getPrivateGlobalPrefix() << "label" << Id << ":\n"; 1320} 1321 1322/// printDeclare - This method prints a local variable declaration used by 1323/// debug tables. 1324/// FIXME: It doesn't really print anything rather it inserts a DebugVariable 1325/// entry into dwarf table. 1326void AsmPrinter::printDeclare(const MachineInstr *MI) const { 1327 int FI = MI->getOperand(0).getIndex(); 1328 GlobalValue *GV = MI->getOperand(1).getGlobal(); 1329 MMI->RecordVariable(GV, FI); 1330} 1331 1332/// PrintAsmOperand - Print the specified operand of MI, an INLINEASM 1333/// instruction, using the specified assembler variant. Targets should 1334/// overried this to format as appropriate. 1335bool AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo, 1336 unsigned AsmVariant, const char *ExtraCode) { 1337 // Target doesn't support this yet! 1338 return true; 1339} 1340 1341bool AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo, 1342 unsigned AsmVariant, 1343 const char *ExtraCode) { 1344 // Target doesn't support this yet! 1345 return true; 1346} 1347 1348/// printBasicBlockLabel - This method prints the label for the specified 1349/// MachineBasicBlock 1350void AsmPrinter::printBasicBlockLabel(const MachineBasicBlock *MBB, 1351 bool printAlign, 1352 bool printColon, 1353 bool printComment) const { 1354 if (printAlign) { 1355 unsigned Align = MBB->getAlignment(); 1356 if (Align) 1357 EmitAlignment(Log2_32(Align)); 1358 } 1359 1360 O << TAI->getPrivateGlobalPrefix() << "BB" << getFunctionNumber() << '_' 1361 << MBB->getNumber(); 1362 if (printColon) 1363 O << ':'; 1364 if (printComment && MBB->getBasicBlock()) 1365 O << '\t' << TAI->getCommentString() << ' ' 1366 << MBB->getBasicBlock()->getNameStart(); 1367} 1368 1369/// printPICJumpTableSetLabel - This method prints a set label for the 1370/// specified MachineBasicBlock for a jumptable entry. 1371void AsmPrinter::printPICJumpTableSetLabel(unsigned uid, 1372 const MachineBasicBlock *MBB) const { 1373 if (!TAI->getSetDirective()) 1374 return; 1375 1376 O << TAI->getSetDirective() << ' ' << TAI->getPrivateGlobalPrefix() 1377 << getFunctionNumber() << '_' << uid << "_set_" << MBB->getNumber() << ','; 1378 printBasicBlockLabel(MBB, false, false, false); 1379 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber() 1380 << '_' << uid << '\n'; 1381} 1382 1383void AsmPrinter::printPICJumpTableSetLabel(unsigned uid, unsigned uid2, 1384 const MachineBasicBlock *MBB) const { 1385 if (!TAI->getSetDirective()) 1386 return; 1387 1388 O << TAI->getSetDirective() << ' ' << TAI->getPrivateGlobalPrefix() 1389 << getFunctionNumber() << '_' << uid << '_' << uid2 1390 << "_set_" << MBB->getNumber() << ','; 1391 printBasicBlockLabel(MBB, false, false, false); 1392 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber() 1393 << '_' << uid << '_' << uid2 << '\n'; 1394} 1395 1396/// printDataDirective - This method prints the asm directive for the 1397/// specified type. 1398void AsmPrinter::printDataDirective(const Type *type) { 1399 const TargetData *TD = TM.getTargetData(); 1400 switch (type->getTypeID()) { 1401 case Type::IntegerTyID: { 1402 unsigned BitWidth = cast<IntegerType>(type)->getBitWidth(); 1403 if (BitWidth <= 8) 1404 O << TAI->getData8bitsDirective(); 1405 else if (BitWidth <= 16) 1406 O << TAI->getData16bitsDirective(); 1407 else if (BitWidth <= 32) 1408 O << TAI->getData32bitsDirective(); 1409 else if (BitWidth <= 64) { 1410 assert(TAI->getData64bitsDirective() && 1411 "Target cannot handle 64-bit constant exprs!"); 1412 O << TAI->getData64bitsDirective(); 1413 } 1414 break; 1415 } 1416 case Type::PointerTyID: 1417 if (TD->getPointerSize() == 8) { 1418 assert(TAI->getData64bitsDirective() && 1419 "Target cannot handle 64-bit pointer exprs!"); 1420 O << TAI->getData64bitsDirective(); 1421 } else { 1422 O << TAI->getData32bitsDirective(); 1423 } 1424 break; 1425 case Type::FloatTyID: case Type::DoubleTyID: 1426 case Type::X86_FP80TyID: case Type::FP128TyID: case Type::PPC_FP128TyID: 1427 assert (0 && "Should have already output floating point constant."); 1428 default: 1429 assert (0 && "Can't handle printing this type of thing"); 1430 break; 1431 } 1432} 1433 1434void AsmPrinter::printSuffixedName(const char *Name, const char *Suffix, 1435 const char *Prefix) { 1436 if (Name[0]=='\"') 1437 O << '\"'; 1438 O << TAI->getPrivateGlobalPrefix(); 1439 if (Prefix) O << Prefix; 1440 if (Name[0]=='\"') 1441 O << '\"'; 1442 if (Name[0]=='\"') 1443 O << Name[1]; 1444 else 1445 O << Name; 1446 O << Suffix; 1447 if (Name[0]=='\"') 1448 O << '\"'; 1449} 1450 1451void AsmPrinter::printSuffixedName(const std::string &Name, const char* Suffix) { 1452 printSuffixedName(Name.c_str(), Suffix); 1453} 1454 1455void AsmPrinter::printVisibility(const std::string& Name, 1456 unsigned Visibility) const { 1457 if (Visibility == GlobalValue::HiddenVisibility) { 1458 if (const char *Directive = TAI->getHiddenDirective()) 1459 O << Directive << Name << '\n'; 1460 } else if (Visibility == GlobalValue::ProtectedVisibility) { 1461 if (const char *Directive = TAI->getProtectedDirective()) 1462 O << Directive << Name << '\n'; 1463 } 1464} 1465 1466GCMetadataPrinter *AsmPrinter::GetOrCreateGCPrinter(GCStrategy *S) { 1467 if (!S->usesMetadata()) 1468 return 0; 1469 1470 gcp_iterator GCPI = GCMetadataPrinters.find(S); 1471 if (GCPI != GCMetadataPrinters.end()) 1472 return GCPI->second; 1473 1474 const char *Name = S->getName().c_str(); 1475 1476 for (GCMetadataPrinterRegistry::iterator 1477 I = GCMetadataPrinterRegistry::begin(), 1478 E = GCMetadataPrinterRegistry::end(); I != E; ++I) 1479 if (strcmp(Name, I->getName()) == 0) { 1480 GCMetadataPrinter *GMP = I->instantiate(); 1481 GMP->S = S; 1482 GCMetadataPrinters.insert(std::make_pair(S, GMP)); 1483 return GMP; 1484 } 1485 1486 cerr << "no GCMetadataPrinter registered for GC: " << Name << "\n"; 1487 abort(); 1488} 1489