1 //===-- PPCAsmPrinter.cpp - Print machine instrs to PowerPC assembly ------===// 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 contains a printer that converts from our internal representation 11 // of machine-dependent LLVM code to PowerPC assembly language. This printer is 12 // the output mechanism used by `llc'. 13 // 14 // Documentation at http://developer.apple.com/documentation/DeveloperTools/ 15 // Reference/Assembler/ASMIntroduction/chapter_1_section_1.html 16 // 17 //===----------------------------------------------------------------------===// 18 19 #include "PPC.h" 20 #include "InstPrinter/PPCInstPrinter.h" 21 #include "MCTargetDesc/PPCMCExpr.h" 22 #include "MCTargetDesc/PPCPredicates.h" 23 #include "PPCMachineFunctionInfo.h" 24 #include "PPCSubtarget.h" 25 #include "PPCTargetMachine.h" 26 #include "PPCTargetStreamer.h" 27 #include "llvm/ADT/MapVector.h" 28 #include "llvm/ADT/SmallString.h" 29 #include "llvm/ADT/StringExtras.h" 30 #include "llvm/CodeGen/AsmPrinter.h" 31 #include "llvm/CodeGen/MachineConstantPool.h" 32 #include "llvm/CodeGen/MachineFunctionPass.h" 33 #include "llvm/CodeGen/MachineInstr.h" 34 #include "llvm/CodeGen/MachineInstrBuilder.h" 35 #include "llvm/CodeGen/MachineModuleInfoImpls.h" 36 #include "llvm/CodeGen/MachineRegisterInfo.h" 37 #include "llvm/CodeGen/StackMaps.h" 38 #include "llvm/CodeGen/TargetLoweringObjectFileImpl.h" 39 #include "llvm/IR/Constants.h" 40 #include "llvm/IR/DebugInfo.h" 41 #include "llvm/IR/DerivedTypes.h" 42 #include "llvm/IR/Mangler.h" 43 #include "llvm/IR/Module.h" 44 #include "llvm/MC/MCAsmInfo.h" 45 #include "llvm/MC/MCContext.h" 46 #include "llvm/MC/MCExpr.h" 47 #include "llvm/MC/MCInst.h" 48 #include "llvm/MC/MCInstBuilder.h" 49 #include "llvm/MC/MCSectionELF.h" 50 #include "llvm/MC/MCSectionMachO.h" 51 #include "llvm/MC/MCStreamer.h" 52 #include "llvm/MC/MCSymbolELF.h" 53 #include "llvm/Support/CommandLine.h" 54 #include "llvm/Support/Debug.h" 55 #include "llvm/Support/ELF.h" 56 #include "llvm/Support/ErrorHandling.h" 57 #include "llvm/Support/MathExtras.h" 58 #include "llvm/Support/TargetRegistry.h" 59 #include "llvm/Support/raw_ostream.h" 60 #include "llvm/Target/TargetInstrInfo.h" 61 #include "llvm/Target/TargetOptions.h" 62 #include "llvm/Target/TargetRegisterInfo.h" 63 using namespace llvm; 64 65 #define DEBUG_TYPE "asmprinter" 66 67 namespace { 68 class PPCAsmPrinter : public AsmPrinter { 69 protected: 70 MapVector<MCSymbol*, MCSymbol*> TOC; 71 const PPCSubtarget *Subtarget; 72 StackMaps SM; 73 public: 74 explicit PPCAsmPrinter(TargetMachine &TM, 75 std::unique_ptr<MCStreamer> Streamer) 76 : AsmPrinter(TM, std::move(Streamer)), SM(*this) {} 77 78 const char *getPassName() const override { 79 return "PowerPC Assembly Printer"; 80 } 81 82 MCSymbol *lookUpOrCreateTOCEntry(MCSymbol *Sym); 83 84 void EmitInstruction(const MachineInstr *MI) override; 85 86 void printOperand(const MachineInstr *MI, unsigned OpNo, raw_ostream &O); 87 88 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo, 89 unsigned AsmVariant, const char *ExtraCode, 90 raw_ostream &O) override; 91 bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo, 92 unsigned AsmVariant, const char *ExtraCode, 93 raw_ostream &O) override; 94 95 void EmitEndOfAsmFile(Module &M) override; 96 97 void LowerSTACKMAP(MCStreamer &OutStreamer, StackMaps &SM, 98 const MachineInstr &MI); 99 void LowerPATCHPOINT(MCStreamer &OutStreamer, StackMaps &SM, 100 const MachineInstr &MI); 101 void EmitTlsCall(const MachineInstr *MI, MCSymbolRefExpr::VariantKind VK); 102 bool runOnMachineFunction(MachineFunction &MF) override { 103 Subtarget = &MF.getSubtarget<PPCSubtarget>(); 104 return AsmPrinter::runOnMachineFunction(MF); 105 } 106 }; 107 108 /// PPCLinuxAsmPrinter - PowerPC assembly printer, customized for Linux 109 class PPCLinuxAsmPrinter : public PPCAsmPrinter { 110 public: 111 explicit PPCLinuxAsmPrinter(TargetMachine &TM, 112 std::unique_ptr<MCStreamer> Streamer) 113 : PPCAsmPrinter(TM, std::move(Streamer)) {} 114 115 const char *getPassName() const override { 116 return "Linux PPC Assembly Printer"; 117 } 118 119 bool doFinalization(Module &M) override; 120 void EmitStartOfAsmFile(Module &M) override; 121 122 void EmitFunctionEntryLabel() override; 123 124 void EmitFunctionBodyStart() override; 125 void EmitFunctionBodyEnd() override; 126 }; 127 128 /// PPCDarwinAsmPrinter - PowerPC assembly printer, customized for Darwin/Mac 129 /// OS X 130 class PPCDarwinAsmPrinter : public PPCAsmPrinter { 131 public: 132 explicit PPCDarwinAsmPrinter(TargetMachine &TM, 133 std::unique_ptr<MCStreamer> Streamer) 134 : PPCAsmPrinter(TM, std::move(Streamer)) {} 135 136 const char *getPassName() const override { 137 return "Darwin PPC Assembly Printer"; 138 } 139 140 bool doFinalization(Module &M) override; 141 void EmitStartOfAsmFile(Module &M) override; 142 143 void EmitFunctionStubs(const MachineModuleInfoMachO::SymbolListTy &Stubs); 144 }; 145 } // end of anonymous namespace 146 147 /// stripRegisterPrefix - This method strips the character prefix from a 148 /// register name so that only the number is left. Used by for linux asm. 149 static const char *stripRegisterPrefix(const char *RegName) { 150 switch (RegName[0]) { 151 case 'r': 152 case 'f': 153 case 'q': // for QPX 154 case 'v': 155 if (RegName[1] == 's') 156 return RegName + 2; 157 return RegName + 1; 158 case 'c': if (RegName[1] == 'r') return RegName + 2; 159 } 160 161 return RegName; 162 } 163 164 void PPCAsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNo, 165 raw_ostream &O) { 166 const DataLayout *DL = TM.getDataLayout(); 167 const MachineOperand &MO = MI->getOperand(OpNo); 168 169 switch (MO.getType()) { 170 case MachineOperand::MO_Register: { 171 const char *RegName = PPCInstPrinter::getRegisterName(MO.getReg()); 172 // Linux assembler (Others?) does not take register mnemonics. 173 // FIXME - What about special registers used in mfspr/mtspr? 174 if (!Subtarget->isDarwin()) 175 RegName = stripRegisterPrefix(RegName); 176 O << RegName; 177 return; 178 } 179 case MachineOperand::MO_Immediate: 180 O << MO.getImm(); 181 return; 182 183 case MachineOperand::MO_MachineBasicBlock: 184 MO.getMBB()->getSymbol()->print(O, MAI); 185 return; 186 case MachineOperand::MO_ConstantPoolIndex: 187 O << DL->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() 188 << '_' << MO.getIndex(); 189 return; 190 case MachineOperand::MO_BlockAddress: 191 GetBlockAddressSymbol(MO.getBlockAddress())->print(O, MAI); 192 return; 193 case MachineOperand::MO_GlobalAddress: { 194 // Computing the address of a global symbol, not calling it. 195 const GlobalValue *GV = MO.getGlobal(); 196 MCSymbol *SymToPrint; 197 198 // External or weakly linked global variables need non-lazily-resolved stubs 199 if (TM.getRelocationModel() != Reloc::Static && 200 !GV->isStrongDefinitionForLinker()) { 201 if (!GV->hasHiddenVisibility()) { 202 SymToPrint = getSymbolWithGlobalValueBase(GV, "$non_lazy_ptr"); 203 MachineModuleInfoImpl::StubValueTy &StubSym = 204 MMI->getObjFileInfo<MachineModuleInfoMachO>() 205 .getGVStubEntry(SymToPrint); 206 if (!StubSym.getPointer()) 207 StubSym = MachineModuleInfoImpl:: 208 StubValueTy(getSymbol(GV), !GV->hasInternalLinkage()); 209 } else if (GV->isDeclaration() || GV->hasCommonLinkage() || 210 GV->hasAvailableExternallyLinkage()) { 211 SymToPrint = getSymbolWithGlobalValueBase(GV, "$non_lazy_ptr"); 212 213 MachineModuleInfoImpl::StubValueTy &StubSym = 214 MMI->getObjFileInfo<MachineModuleInfoMachO>(). 215 getHiddenGVStubEntry(SymToPrint); 216 if (!StubSym.getPointer()) 217 StubSym = MachineModuleInfoImpl:: 218 StubValueTy(getSymbol(GV), !GV->hasInternalLinkage()); 219 } else { 220 SymToPrint = getSymbol(GV); 221 } 222 } else { 223 SymToPrint = getSymbol(GV); 224 } 225 226 SymToPrint->print(O, MAI); 227 228 printOffset(MO.getOffset(), O); 229 return; 230 } 231 232 default: 233 O << "<unknown operand type: " << (unsigned)MO.getType() << ">"; 234 return; 235 } 236 } 237 238 /// PrintAsmOperand - Print out an operand for an inline asm expression. 239 /// 240 bool PPCAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo, 241 unsigned AsmVariant, 242 const char *ExtraCode, raw_ostream &O) { 243 // Does this asm operand have a single letter operand modifier? 244 if (ExtraCode && ExtraCode[0]) { 245 if (ExtraCode[1] != 0) return true; // Unknown modifier. 246 247 switch (ExtraCode[0]) { 248 default: 249 // See if this is a generic print operand 250 return AsmPrinter::PrintAsmOperand(MI, OpNo, AsmVariant, ExtraCode, O); 251 case 'c': // Don't print "$" before a global var name or constant. 252 break; // PPC never has a prefix. 253 case 'L': // Write second word of DImode reference. 254 // Verify that this operand has two consecutive registers. 255 if (!MI->getOperand(OpNo).isReg() || 256 OpNo+1 == MI->getNumOperands() || 257 !MI->getOperand(OpNo+1).isReg()) 258 return true; 259 ++OpNo; // Return the high-part. 260 break; 261 case 'I': 262 // Write 'i' if an integer constant, otherwise nothing. Used to print 263 // addi vs add, etc. 264 if (MI->getOperand(OpNo).isImm()) 265 O << "i"; 266 return false; 267 } 268 } 269 270 printOperand(MI, OpNo, O); 271 return false; 272 } 273 274 // At the moment, all inline asm memory operands are a single register. 275 // In any case, the output of this routine should always be just one 276 // assembler operand. 277 278 bool PPCAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo, 279 unsigned AsmVariant, 280 const char *ExtraCode, 281 raw_ostream &O) { 282 if (ExtraCode && ExtraCode[0]) { 283 if (ExtraCode[1] != 0) return true; // Unknown modifier. 284 285 switch (ExtraCode[0]) { 286 default: return true; // Unknown modifier. 287 case 'y': // A memory reference for an X-form instruction 288 { 289 const char *RegName = "r0"; 290 if (!Subtarget->isDarwin()) 291 RegName = stripRegisterPrefix(RegName); 292 O << RegName << ", "; 293 printOperand(MI, OpNo, O); 294 return false; 295 } 296 case 'U': // Print 'u' for update form. 297 case 'X': // Print 'x' for indexed form. 298 { 299 // FIXME: Currently for PowerPC memory operands are always loaded 300 // into a register, so we never get an update or indexed form. 301 // This is bad even for offset forms, since even if we know we 302 // have a value in -16(r1), we will generate a load into r<n> 303 // and then load from 0(r<n>). Until that issue is fixed, 304 // tolerate 'U' and 'X' but don't output anything. 305 assert(MI->getOperand(OpNo).isReg()); 306 return false; 307 } 308 } 309 } 310 311 assert(MI->getOperand(OpNo).isReg()); 312 O << "0("; 313 printOperand(MI, OpNo, O); 314 O << ")"; 315 return false; 316 } 317 318 319 /// lookUpOrCreateTOCEntry -- Given a symbol, look up whether a TOC entry 320 /// exists for it. If not, create one. Then return a symbol that references 321 /// the TOC entry. 322 MCSymbol *PPCAsmPrinter::lookUpOrCreateTOCEntry(MCSymbol *Sym) { 323 MCSymbol *&TOCEntry = TOC[Sym]; 324 if (!TOCEntry) 325 TOCEntry = createTempSymbol("C"); 326 return TOCEntry; 327 } 328 329 void PPCAsmPrinter::EmitEndOfAsmFile(Module &M) { 330 SM.serializeToStackMapSection(); 331 } 332 333 void PPCAsmPrinter::LowerSTACKMAP(MCStreamer &OutStreamer, StackMaps &SM, 334 const MachineInstr &MI) { 335 unsigned NumNOPBytes = MI.getOperand(1).getImm(); 336 337 SM.recordStackMap(MI); 338 assert(NumNOPBytes % 4 == 0 && "Invalid number of NOP bytes requested!"); 339 340 // Scan ahead to trim the shadow. 341 const MachineBasicBlock &MBB = *MI.getParent(); 342 MachineBasicBlock::const_iterator MII(MI); 343 ++MII; 344 while (NumNOPBytes > 0) { 345 if (MII == MBB.end() || MII->isCall() || 346 MII->getOpcode() == PPC::DBG_VALUE || 347 MII->getOpcode() == TargetOpcode::PATCHPOINT || 348 MII->getOpcode() == TargetOpcode::STACKMAP) 349 break; 350 ++MII; 351 NumNOPBytes -= 4; 352 } 353 354 // Emit nops. 355 for (unsigned i = 0; i < NumNOPBytes; i += 4) 356 EmitToStreamer(OutStreamer, MCInstBuilder(PPC::NOP)); 357 } 358 359 // Lower a patchpoint of the form: 360 // [<def>], <id>, <numBytes>, <target>, <numArgs> 361 void PPCAsmPrinter::LowerPATCHPOINT(MCStreamer &OutStreamer, StackMaps &SM, 362 const MachineInstr &MI) { 363 SM.recordPatchPoint(MI); 364 PatchPointOpers Opers(&MI); 365 366 int64_t CallTarget = Opers.getMetaOper(PatchPointOpers::TargetPos).getImm(); 367 unsigned EncodedBytes = 0; 368 if (CallTarget) { 369 assert((CallTarget & 0xFFFFFFFFFFFF) == CallTarget && 370 "High 16 bits of call target should be zero."); 371 unsigned ScratchReg = MI.getOperand(Opers.getNextScratchIdx()).getReg(); 372 EncodedBytes = 0; 373 // Materialize the jump address: 374 EmitToStreamer(OutStreamer, MCInstBuilder(PPC::LI8) 375 .addReg(ScratchReg) 376 .addImm((CallTarget >> 32) & 0xFFFF)); 377 ++EncodedBytes; 378 EmitToStreamer(OutStreamer, MCInstBuilder(PPC::RLDIC) 379 .addReg(ScratchReg) 380 .addReg(ScratchReg) 381 .addImm(32).addImm(16)); 382 ++EncodedBytes; 383 EmitToStreamer(OutStreamer, MCInstBuilder(PPC::ORIS8) 384 .addReg(ScratchReg) 385 .addReg(ScratchReg) 386 .addImm((CallTarget >> 16) & 0xFFFF)); 387 ++EncodedBytes; 388 EmitToStreamer(OutStreamer, MCInstBuilder(PPC::ORI8) 389 .addReg(ScratchReg) 390 .addReg(ScratchReg) 391 .addImm(CallTarget & 0xFFFF)); 392 393 // Save the current TOC pointer before the remote call. 394 int TOCSaveOffset = Subtarget->isELFv2ABI() ? 24 : 40; 395 EmitToStreamer(OutStreamer, MCInstBuilder(PPC::STD) 396 .addReg(PPC::X2) 397 .addImm(TOCSaveOffset) 398 .addReg(PPC::X1)); 399 ++EncodedBytes; 400 401 402 // If we're on ELFv1, then we need to load the actual function pointer from 403 // the function descriptor. 404 if (!Subtarget->isELFv2ABI()) { 405 // Load the new TOC pointer and the function address, but not r11 406 // (needing this is rare, and loading it here would prevent passing it 407 // via a 'nest' parameter. 408 EmitToStreamer(OutStreamer, MCInstBuilder(PPC::LD) 409 .addReg(PPC::X2) 410 .addImm(8) 411 .addReg(ScratchReg)); 412 ++EncodedBytes; 413 EmitToStreamer(OutStreamer, MCInstBuilder(PPC::LD) 414 .addReg(ScratchReg) 415 .addImm(0) 416 .addReg(ScratchReg)); 417 ++EncodedBytes; 418 } 419 420 EmitToStreamer(OutStreamer, MCInstBuilder(PPC::MTCTR8).addReg(ScratchReg)); 421 ++EncodedBytes; 422 EmitToStreamer(OutStreamer, MCInstBuilder(PPC::BCTRL8)); 423 ++EncodedBytes; 424 425 // Restore the TOC pointer after the call. 426 EmitToStreamer(OutStreamer, MCInstBuilder(PPC::LD) 427 .addReg(PPC::X2) 428 .addImm(TOCSaveOffset) 429 .addReg(PPC::X1)); 430 ++EncodedBytes; 431 } 432 433 // Each instruction is 4 bytes. 434 EncodedBytes *= 4; 435 436 // Emit padding. 437 unsigned NumBytes = Opers.getMetaOper(PatchPointOpers::NBytesPos).getImm(); 438 assert(NumBytes >= EncodedBytes && 439 "Patchpoint can't request size less than the length of a call."); 440 assert((NumBytes - EncodedBytes) % 4 == 0 && 441 "Invalid number of NOP bytes requested!"); 442 for (unsigned i = EncodedBytes; i < NumBytes; i += 4) 443 EmitToStreamer(OutStreamer, MCInstBuilder(PPC::NOP)); 444 } 445 446 /// EmitTlsCall -- Given a GETtls[ld]ADDR[32] instruction, print a 447 /// call to __tls_get_addr to the current output stream. 448 void PPCAsmPrinter::EmitTlsCall(const MachineInstr *MI, 449 MCSymbolRefExpr::VariantKind VK) { 450 StringRef Name = "__tls_get_addr"; 451 MCSymbol *TlsGetAddr = OutContext.getOrCreateSymbol(Name); 452 MCSymbolRefExpr::VariantKind Kind = MCSymbolRefExpr::VK_None; 453 454 assert(MI->getOperand(0).isReg() && 455 ((Subtarget->isPPC64() && MI->getOperand(0).getReg() == PPC::X3) || 456 (!Subtarget->isPPC64() && MI->getOperand(0).getReg() == PPC::R3)) && 457 "GETtls[ld]ADDR[32] must define GPR3"); 458 assert(MI->getOperand(1).isReg() && 459 ((Subtarget->isPPC64() && MI->getOperand(1).getReg() == PPC::X3) || 460 (!Subtarget->isPPC64() && MI->getOperand(1).getReg() == PPC::R3)) && 461 "GETtls[ld]ADDR[32] must read GPR3"); 462 463 if (!Subtarget->isPPC64() && !Subtarget->isDarwin() && 464 TM.getRelocationModel() == Reloc::PIC_) 465 Kind = MCSymbolRefExpr::VK_PLT; 466 const MCSymbolRefExpr *TlsRef = 467 MCSymbolRefExpr::create(TlsGetAddr, Kind, OutContext); 468 const MachineOperand &MO = MI->getOperand(2); 469 const GlobalValue *GValue = MO.getGlobal(); 470 MCSymbol *MOSymbol = getSymbol(GValue); 471 const MCExpr *SymVar = MCSymbolRefExpr::create(MOSymbol, VK, OutContext); 472 EmitToStreamer(*OutStreamer, 473 MCInstBuilder(Subtarget->isPPC64() ? 474 PPC::BL8_NOP_TLS : PPC::BL_TLS) 475 .addExpr(TlsRef) 476 .addExpr(SymVar)); 477 } 478 479 /// EmitInstruction -- Print out a single PowerPC MI in Darwin syntax to 480 /// the current output stream. 481 /// 482 void PPCAsmPrinter::EmitInstruction(const MachineInstr *MI) { 483 MCInst TmpInst; 484 bool isPPC64 = Subtarget->isPPC64(); 485 bool isDarwin = TM.getTargetTriple().isOSDarwin(); 486 const Module *M = MF->getFunction()->getParent(); 487 PICLevel::Level PL = M->getPICLevel(); 488 489 // Lower multi-instruction pseudo operations. 490 switch (MI->getOpcode()) { 491 default: break; 492 case TargetOpcode::DBG_VALUE: 493 llvm_unreachable("Should be handled target independently"); 494 case TargetOpcode::STACKMAP: 495 return LowerSTACKMAP(*OutStreamer, SM, *MI); 496 case TargetOpcode::PATCHPOINT: 497 return LowerPATCHPOINT(*OutStreamer, SM, *MI); 498 499 case PPC::MoveGOTtoLR: { 500 // Transform %LR = MoveGOTtoLR 501 // Into this: bl _GLOBAL_OFFSET_TABLE_@local-4 502 // _GLOBAL_OFFSET_TABLE_@local-4 (instruction preceding 503 // _GLOBAL_OFFSET_TABLE_) has exactly one instruction: 504 // blrl 505 // This will return the pointer to _GLOBAL_OFFSET_TABLE_@local 506 MCSymbol *GOTSymbol = 507 OutContext.getOrCreateSymbol(StringRef("_GLOBAL_OFFSET_TABLE_")); 508 const MCExpr *OffsExpr = 509 MCBinaryExpr::createSub(MCSymbolRefExpr::create(GOTSymbol, 510 MCSymbolRefExpr::VK_PPC_LOCAL, 511 OutContext), 512 MCConstantExpr::create(4, OutContext), 513 OutContext); 514 515 // Emit the 'bl'. 516 EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::BL).addExpr(OffsExpr)); 517 return; 518 } 519 case PPC::MovePCtoLR: 520 case PPC::MovePCtoLR8: { 521 // Transform %LR = MovePCtoLR 522 // Into this, where the label is the PIC base: 523 // bl L1$pb 524 // L1$pb: 525 MCSymbol *PICBase = MF->getPICBaseSymbol(); 526 527 // Emit the 'bl'. 528 EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::BL) 529 // FIXME: We would like an efficient form for this, so we don't have to do 530 // a lot of extra uniquing. 531 .addExpr(MCSymbolRefExpr::create(PICBase, OutContext))); 532 533 // Emit the label. 534 OutStreamer->EmitLabel(PICBase); 535 return; 536 } 537 case PPC::UpdateGBR: { 538 // Transform %Rd = UpdateGBR(%Rt, %Ri) 539 // Into: lwz %Rt, .L0$poff - .L0$pb(%Ri) 540 // add %Rd, %Rt, %Ri 541 // Get the offset from the GOT Base Register to the GOT 542 LowerPPCMachineInstrToMCInst(MI, TmpInst, *this, isDarwin); 543 MCSymbol *PICOffset = 544 MF->getInfo<PPCFunctionInfo>()->getPICOffsetSymbol(); 545 TmpInst.setOpcode(PPC::LWZ); 546 const MCExpr *Exp = 547 MCSymbolRefExpr::create(PICOffset, MCSymbolRefExpr::VK_None, OutContext); 548 const MCExpr *PB = 549 MCSymbolRefExpr::create(MF->getPICBaseSymbol(), 550 MCSymbolRefExpr::VK_None, 551 OutContext); 552 const MCOperand TR = TmpInst.getOperand(1); 553 const MCOperand PICR = TmpInst.getOperand(0); 554 555 // Step 1: lwz %Rt, .L$poff - .L$pb(%Ri) 556 TmpInst.getOperand(1) = 557 MCOperand::createExpr(MCBinaryExpr::createSub(Exp, PB, OutContext)); 558 TmpInst.getOperand(0) = TR; 559 TmpInst.getOperand(2) = PICR; 560 EmitToStreamer(*OutStreamer, TmpInst); 561 562 TmpInst.setOpcode(PPC::ADD4); 563 TmpInst.getOperand(0) = PICR; 564 TmpInst.getOperand(1) = TR; 565 TmpInst.getOperand(2) = PICR; 566 EmitToStreamer(*OutStreamer, TmpInst); 567 return; 568 } 569 case PPC::LWZtoc: { 570 // Transform %R3 = LWZtoc <ga:@min1>, %R2 571 LowerPPCMachineInstrToMCInst(MI, TmpInst, *this, isDarwin); 572 573 // Change the opcode to LWZ, and the global address operand to be a 574 // reference to the GOT entry we will synthesize later. 575 TmpInst.setOpcode(PPC::LWZ); 576 const MachineOperand &MO = MI->getOperand(1); 577 578 // Map symbol -> label of TOC entry 579 assert(MO.isGlobal() || MO.isCPI() || MO.isJTI() || MO.isBlockAddress()); 580 MCSymbol *MOSymbol = nullptr; 581 if (MO.isGlobal()) 582 MOSymbol = getSymbol(MO.getGlobal()); 583 else if (MO.isCPI()) 584 MOSymbol = GetCPISymbol(MO.getIndex()); 585 else if (MO.isJTI()) 586 MOSymbol = GetJTISymbol(MO.getIndex()); 587 else if (MO.isBlockAddress()) 588 MOSymbol = GetBlockAddressSymbol(MO.getBlockAddress()); 589 590 if (PL == PICLevel::Small) { 591 const MCExpr *Exp = 592 MCSymbolRefExpr::create(MOSymbol, MCSymbolRefExpr::VK_GOT, 593 OutContext); 594 TmpInst.getOperand(1) = MCOperand::createExpr(Exp); 595 } else { 596 MCSymbol *TOCEntry = lookUpOrCreateTOCEntry(MOSymbol); 597 598 const MCExpr *Exp = 599 MCSymbolRefExpr::create(TOCEntry, MCSymbolRefExpr::VK_None, 600 OutContext); 601 const MCExpr *PB = 602 MCSymbolRefExpr::create(OutContext.getOrCreateSymbol(Twine(".LTOC")), 603 OutContext); 604 Exp = MCBinaryExpr::createSub(Exp, PB, OutContext); 605 TmpInst.getOperand(1) = MCOperand::createExpr(Exp); 606 } 607 EmitToStreamer(*OutStreamer, TmpInst); 608 return; 609 } 610 case PPC::LDtocJTI: 611 case PPC::LDtocCPT: 612 case PPC::LDtocBA: 613 case PPC::LDtoc: { 614 // Transform %X3 = LDtoc <ga:@min1>, %X2 615 LowerPPCMachineInstrToMCInst(MI, TmpInst, *this, isDarwin); 616 617 // Change the opcode to LD, and the global address operand to be a 618 // reference to the TOC entry we will synthesize later. 619 TmpInst.setOpcode(PPC::LD); 620 const MachineOperand &MO = MI->getOperand(1); 621 622 // Map symbol -> label of TOC entry 623 assert(MO.isGlobal() || MO.isCPI() || MO.isJTI() || MO.isBlockAddress()); 624 MCSymbol *MOSymbol = nullptr; 625 if (MO.isGlobal()) 626 MOSymbol = getSymbol(MO.getGlobal()); 627 else if (MO.isCPI()) 628 MOSymbol = GetCPISymbol(MO.getIndex()); 629 else if (MO.isJTI()) 630 MOSymbol = GetJTISymbol(MO.getIndex()); 631 else if (MO.isBlockAddress()) 632 MOSymbol = GetBlockAddressSymbol(MO.getBlockAddress()); 633 634 MCSymbol *TOCEntry = lookUpOrCreateTOCEntry(MOSymbol); 635 636 const MCExpr *Exp = 637 MCSymbolRefExpr::create(TOCEntry, MCSymbolRefExpr::VK_PPC_TOC, 638 OutContext); 639 TmpInst.getOperand(1) = MCOperand::createExpr(Exp); 640 EmitToStreamer(*OutStreamer, TmpInst); 641 return; 642 } 643 644 case PPC::ADDIStocHA: { 645 // Transform %Xd = ADDIStocHA %X2, <ga:@sym> 646 LowerPPCMachineInstrToMCInst(MI, TmpInst, *this, isDarwin); 647 648 // Change the opcode to ADDIS8. If the global address is external, has 649 // common linkage, is a non-local function address, or is a jump table 650 // address, then generate a TOC entry and reference that. Otherwise 651 // reference the symbol directly. 652 TmpInst.setOpcode(PPC::ADDIS8); 653 const MachineOperand &MO = MI->getOperand(2); 654 assert((MO.isGlobal() || MO.isCPI() || MO.isJTI() || 655 MO.isBlockAddress()) && 656 "Invalid operand for ADDIStocHA!"); 657 MCSymbol *MOSymbol = nullptr; 658 bool IsExternal = false; 659 bool IsNonLocalFunction = false; 660 bool IsCommon = false; 661 bool IsAvailExt = false; 662 663 if (MO.isGlobal()) { 664 const GlobalValue *GV = MO.getGlobal(); 665 MOSymbol = getSymbol(GV); 666 IsExternal = GV->isDeclaration(); 667 IsCommon = GV->hasCommonLinkage(); 668 IsNonLocalFunction = GV->getType()->getElementType()->isFunctionTy() && 669 !GV->isStrongDefinitionForLinker(); 670 IsAvailExt = GV->hasAvailableExternallyLinkage(); 671 } else if (MO.isCPI()) 672 MOSymbol = GetCPISymbol(MO.getIndex()); 673 else if (MO.isJTI()) 674 MOSymbol = GetJTISymbol(MO.getIndex()); 675 else if (MO.isBlockAddress()) 676 MOSymbol = GetBlockAddressSymbol(MO.getBlockAddress()); 677 678 if (IsExternal || IsNonLocalFunction || IsCommon || IsAvailExt || 679 MO.isJTI() || MO.isBlockAddress() || 680 TM.getCodeModel() == CodeModel::Large) 681 MOSymbol = lookUpOrCreateTOCEntry(MOSymbol); 682 683 const MCExpr *Exp = 684 MCSymbolRefExpr::create(MOSymbol, MCSymbolRefExpr::VK_PPC_TOC_HA, 685 OutContext); 686 TmpInst.getOperand(2) = MCOperand::createExpr(Exp); 687 EmitToStreamer(*OutStreamer, TmpInst); 688 return; 689 } 690 case PPC::LDtocL: { 691 // Transform %Xd = LDtocL <ga:@sym>, %Xs 692 LowerPPCMachineInstrToMCInst(MI, TmpInst, *this, isDarwin); 693 694 // Change the opcode to LD. If the global address is external, has 695 // common linkage, or is a jump table address, then reference the 696 // associated TOC entry. Otherwise reference the symbol directly. 697 TmpInst.setOpcode(PPC::LD); 698 const MachineOperand &MO = MI->getOperand(1); 699 assert((MO.isGlobal() || MO.isCPI() || MO.isJTI() || 700 MO.isBlockAddress()) && 701 "Invalid operand for LDtocL!"); 702 MCSymbol *MOSymbol = nullptr; 703 704 if (MO.isJTI()) 705 MOSymbol = lookUpOrCreateTOCEntry(GetJTISymbol(MO.getIndex())); 706 else if (MO.isBlockAddress()) { 707 MOSymbol = GetBlockAddressSymbol(MO.getBlockAddress()); 708 MOSymbol = lookUpOrCreateTOCEntry(MOSymbol); 709 } 710 else if (MO.isCPI()) { 711 MOSymbol = GetCPISymbol(MO.getIndex()); 712 if (TM.getCodeModel() == CodeModel::Large) 713 MOSymbol = lookUpOrCreateTOCEntry(MOSymbol); 714 } 715 else if (MO.isGlobal()) { 716 const GlobalValue *GValue = MO.getGlobal(); 717 MOSymbol = getSymbol(GValue); 718 if (GValue->getType()->getElementType()->isFunctionTy() || 719 GValue->isDeclaration() || GValue->hasCommonLinkage() || 720 GValue->hasAvailableExternallyLinkage() || 721 TM.getCodeModel() == CodeModel::Large) 722 MOSymbol = lookUpOrCreateTOCEntry(MOSymbol); 723 } 724 725 const MCExpr *Exp = 726 MCSymbolRefExpr::create(MOSymbol, MCSymbolRefExpr::VK_PPC_TOC_LO, 727 OutContext); 728 TmpInst.getOperand(1) = MCOperand::createExpr(Exp); 729 EmitToStreamer(*OutStreamer, TmpInst); 730 return; 731 } 732 case PPC::ADDItocL: { 733 // Transform %Xd = ADDItocL %Xs, <ga:@sym> 734 LowerPPCMachineInstrToMCInst(MI, TmpInst, *this, isDarwin); 735 736 // Change the opcode to ADDI8. If the global address is external, then 737 // generate a TOC entry and reference that. Otherwise reference the 738 // symbol directly. 739 TmpInst.setOpcode(PPC::ADDI8); 740 const MachineOperand &MO = MI->getOperand(2); 741 assert((MO.isGlobal() || MO.isCPI()) && "Invalid operand for ADDItocL"); 742 MCSymbol *MOSymbol = nullptr; 743 bool IsExternal = false; 744 bool IsNonLocalFunction = false; 745 746 if (MO.isGlobal()) { 747 const GlobalValue *GV = MO.getGlobal(); 748 MOSymbol = getSymbol(GV); 749 IsExternal = GV->isDeclaration(); 750 IsNonLocalFunction = GV->getType()->getElementType()->isFunctionTy() && 751 !GV->isStrongDefinitionForLinker(); 752 } else if (MO.isCPI()) 753 MOSymbol = GetCPISymbol(MO.getIndex()); 754 755 if (IsNonLocalFunction || IsExternal || 756 TM.getCodeModel() == CodeModel::Large) 757 MOSymbol = lookUpOrCreateTOCEntry(MOSymbol); 758 759 const MCExpr *Exp = 760 MCSymbolRefExpr::create(MOSymbol, MCSymbolRefExpr::VK_PPC_TOC_LO, 761 OutContext); 762 TmpInst.getOperand(2) = MCOperand::createExpr(Exp); 763 EmitToStreamer(*OutStreamer, TmpInst); 764 return; 765 } 766 case PPC::ADDISgotTprelHA: { 767 // Transform: %Xd = ADDISgotTprelHA %X2, <ga:@sym> 768 // Into: %Xd = ADDIS8 %X2, sym@got@tlsgd@ha 769 assert(Subtarget->isPPC64() && "Not supported for 32-bit PowerPC"); 770 const MachineOperand &MO = MI->getOperand(2); 771 const GlobalValue *GValue = MO.getGlobal(); 772 MCSymbol *MOSymbol = getSymbol(GValue); 773 const MCExpr *SymGotTprel = 774 MCSymbolRefExpr::create(MOSymbol, MCSymbolRefExpr::VK_PPC_GOT_TPREL_HA, 775 OutContext); 776 EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::ADDIS8) 777 .addReg(MI->getOperand(0).getReg()) 778 .addReg(MI->getOperand(1).getReg()) 779 .addExpr(SymGotTprel)); 780 return; 781 } 782 case PPC::LDgotTprelL: 783 case PPC::LDgotTprelL32: { 784 // Transform %Xd = LDgotTprelL <ga:@sym>, %Xs 785 LowerPPCMachineInstrToMCInst(MI, TmpInst, *this, isDarwin); 786 787 // Change the opcode to LD. 788 TmpInst.setOpcode(isPPC64 ? PPC::LD : PPC::LWZ); 789 const MachineOperand &MO = MI->getOperand(1); 790 const GlobalValue *GValue = MO.getGlobal(); 791 MCSymbol *MOSymbol = getSymbol(GValue); 792 const MCExpr *Exp = 793 MCSymbolRefExpr::create(MOSymbol, MCSymbolRefExpr::VK_PPC_GOT_TPREL_LO, 794 OutContext); 795 TmpInst.getOperand(1) = MCOperand::createExpr(Exp); 796 EmitToStreamer(*OutStreamer, TmpInst); 797 return; 798 } 799 800 case PPC::PPC32PICGOT: { 801 MCSymbol *GOTSymbol = OutContext.getOrCreateSymbol(StringRef("_GLOBAL_OFFSET_TABLE_")); 802 MCSymbol *GOTRef = OutContext.createTempSymbol(); 803 MCSymbol *NextInstr = OutContext.createTempSymbol(); 804 805 EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::BL) 806 // FIXME: We would like an efficient form for this, so we don't have to do 807 // a lot of extra uniquing. 808 .addExpr(MCSymbolRefExpr::create(NextInstr, OutContext))); 809 const MCExpr *OffsExpr = 810 MCBinaryExpr::createSub(MCSymbolRefExpr::create(GOTSymbol, OutContext), 811 MCSymbolRefExpr::create(GOTRef, OutContext), 812 OutContext); 813 OutStreamer->EmitLabel(GOTRef); 814 OutStreamer->EmitValue(OffsExpr, 4); 815 OutStreamer->EmitLabel(NextInstr); 816 EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::MFLR) 817 .addReg(MI->getOperand(0).getReg())); 818 EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::LWZ) 819 .addReg(MI->getOperand(1).getReg()) 820 .addImm(0) 821 .addReg(MI->getOperand(0).getReg())); 822 EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::ADD4) 823 .addReg(MI->getOperand(0).getReg()) 824 .addReg(MI->getOperand(1).getReg()) 825 .addReg(MI->getOperand(0).getReg())); 826 return; 827 } 828 case PPC::PPC32GOT: { 829 MCSymbol *GOTSymbol = OutContext.getOrCreateSymbol(StringRef("_GLOBAL_OFFSET_TABLE_")); 830 const MCExpr *SymGotTlsL = 831 MCSymbolRefExpr::create(GOTSymbol, MCSymbolRefExpr::VK_PPC_LO, 832 OutContext); 833 const MCExpr *SymGotTlsHA = 834 MCSymbolRefExpr::create(GOTSymbol, MCSymbolRefExpr::VK_PPC_HA, 835 OutContext); 836 EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::LI) 837 .addReg(MI->getOperand(0).getReg()) 838 .addExpr(SymGotTlsL)); 839 EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::ADDIS) 840 .addReg(MI->getOperand(0).getReg()) 841 .addReg(MI->getOperand(0).getReg()) 842 .addExpr(SymGotTlsHA)); 843 return; 844 } 845 case PPC::ADDIStlsgdHA: { 846 // Transform: %Xd = ADDIStlsgdHA %X2, <ga:@sym> 847 // Into: %Xd = ADDIS8 %X2, sym@got@tlsgd@ha 848 assert(Subtarget->isPPC64() && "Not supported for 32-bit PowerPC"); 849 const MachineOperand &MO = MI->getOperand(2); 850 const GlobalValue *GValue = MO.getGlobal(); 851 MCSymbol *MOSymbol = getSymbol(GValue); 852 const MCExpr *SymGotTlsGD = 853 MCSymbolRefExpr::create(MOSymbol, MCSymbolRefExpr::VK_PPC_GOT_TLSGD_HA, 854 OutContext); 855 EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::ADDIS8) 856 .addReg(MI->getOperand(0).getReg()) 857 .addReg(MI->getOperand(1).getReg()) 858 .addExpr(SymGotTlsGD)); 859 return; 860 } 861 case PPC::ADDItlsgdL: 862 // Transform: %Xd = ADDItlsgdL %Xs, <ga:@sym> 863 // Into: %Xd = ADDI8 %Xs, sym@got@tlsgd@l 864 case PPC::ADDItlsgdL32: { 865 // Transform: %Rd = ADDItlsgdL32 %Rs, <ga:@sym> 866 // Into: %Rd = ADDI %Rs, sym@got@tlsgd 867 const MachineOperand &MO = MI->getOperand(2); 868 const GlobalValue *GValue = MO.getGlobal(); 869 MCSymbol *MOSymbol = getSymbol(GValue); 870 const MCExpr *SymGotTlsGD = MCSymbolRefExpr::create( 871 MOSymbol, Subtarget->isPPC64() ? MCSymbolRefExpr::VK_PPC_GOT_TLSGD_LO 872 : MCSymbolRefExpr::VK_PPC_GOT_TLSGD, 873 OutContext); 874 EmitToStreamer(*OutStreamer, 875 MCInstBuilder(Subtarget->isPPC64() ? PPC::ADDI8 : PPC::ADDI) 876 .addReg(MI->getOperand(0).getReg()) 877 .addReg(MI->getOperand(1).getReg()) 878 .addExpr(SymGotTlsGD)); 879 return; 880 } 881 case PPC::GETtlsADDR: 882 // Transform: %X3 = GETtlsADDR %X3, <ga:@sym> 883 // Into: BL8_NOP_TLS __tls_get_addr(sym at tlsgd) 884 case PPC::GETtlsADDR32: { 885 // Transform: %R3 = GETtlsADDR32 %R3, <ga:@sym> 886 // Into: BL_TLS __tls_get_addr(sym at tlsgd)@PLT 887 EmitTlsCall(MI, MCSymbolRefExpr::VK_PPC_TLSGD); 888 return; 889 } 890 case PPC::ADDIStlsldHA: { 891 // Transform: %Xd = ADDIStlsldHA %X2, <ga:@sym> 892 // Into: %Xd = ADDIS8 %X2, sym@got@tlsld@ha 893 assert(Subtarget->isPPC64() && "Not supported for 32-bit PowerPC"); 894 const MachineOperand &MO = MI->getOperand(2); 895 const GlobalValue *GValue = MO.getGlobal(); 896 MCSymbol *MOSymbol = getSymbol(GValue); 897 const MCExpr *SymGotTlsLD = 898 MCSymbolRefExpr::create(MOSymbol, MCSymbolRefExpr::VK_PPC_GOT_TLSLD_HA, 899 OutContext); 900 EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::ADDIS8) 901 .addReg(MI->getOperand(0).getReg()) 902 .addReg(MI->getOperand(1).getReg()) 903 .addExpr(SymGotTlsLD)); 904 return; 905 } 906 case PPC::ADDItlsldL: 907 // Transform: %Xd = ADDItlsldL %Xs, <ga:@sym> 908 // Into: %Xd = ADDI8 %Xs, sym@got@tlsld@l 909 case PPC::ADDItlsldL32: { 910 // Transform: %Rd = ADDItlsldL32 %Rs, <ga:@sym> 911 // Into: %Rd = ADDI %Rs, sym@got@tlsld 912 const MachineOperand &MO = MI->getOperand(2); 913 const GlobalValue *GValue = MO.getGlobal(); 914 MCSymbol *MOSymbol = getSymbol(GValue); 915 const MCExpr *SymGotTlsLD = MCSymbolRefExpr::create( 916 MOSymbol, Subtarget->isPPC64() ? MCSymbolRefExpr::VK_PPC_GOT_TLSLD_LO 917 : MCSymbolRefExpr::VK_PPC_GOT_TLSLD, 918 OutContext); 919 EmitToStreamer(*OutStreamer, 920 MCInstBuilder(Subtarget->isPPC64() ? PPC::ADDI8 : PPC::ADDI) 921 .addReg(MI->getOperand(0).getReg()) 922 .addReg(MI->getOperand(1).getReg()) 923 .addExpr(SymGotTlsLD)); 924 return; 925 } 926 case PPC::GETtlsldADDR: 927 // Transform: %X3 = GETtlsldADDR %X3, <ga:@sym> 928 // Into: BL8_NOP_TLS __tls_get_addr(sym at tlsld) 929 case PPC::GETtlsldADDR32: { 930 // Transform: %R3 = GETtlsldADDR32 %R3, <ga:@sym> 931 // Into: BL_TLS __tls_get_addr(sym at tlsld)@PLT 932 EmitTlsCall(MI, MCSymbolRefExpr::VK_PPC_TLSLD); 933 return; 934 } 935 case PPC::ADDISdtprelHA: 936 // Transform: %Xd = ADDISdtprelHA %X3, <ga:@sym> 937 // Into: %Xd = ADDIS8 %X3, sym@dtprel@ha 938 case PPC::ADDISdtprelHA32: { 939 // Transform: %Rd = ADDISdtprelHA32 %R3, <ga:@sym> 940 // Into: %Rd = ADDIS %R3, sym@dtprel@ha 941 const MachineOperand &MO = MI->getOperand(2); 942 const GlobalValue *GValue = MO.getGlobal(); 943 MCSymbol *MOSymbol = getSymbol(GValue); 944 const MCExpr *SymDtprel = 945 MCSymbolRefExpr::create(MOSymbol, MCSymbolRefExpr::VK_PPC_DTPREL_HA, 946 OutContext); 947 EmitToStreamer( 948 *OutStreamer, 949 MCInstBuilder(Subtarget->isPPC64() ? PPC::ADDIS8 : PPC::ADDIS) 950 .addReg(MI->getOperand(0).getReg()) 951 .addReg(Subtarget->isPPC64() ? PPC::X3 : PPC::R3) 952 .addExpr(SymDtprel)); 953 return; 954 } 955 case PPC::ADDIdtprelL: 956 // Transform: %Xd = ADDIdtprelL %Xs, <ga:@sym> 957 // Into: %Xd = ADDI8 %Xs, sym@dtprel@l 958 case PPC::ADDIdtprelL32: { 959 // Transform: %Rd = ADDIdtprelL32 %Rs, <ga:@sym> 960 // Into: %Rd = ADDI %Rs, sym@dtprel@l 961 const MachineOperand &MO = MI->getOperand(2); 962 const GlobalValue *GValue = MO.getGlobal(); 963 MCSymbol *MOSymbol = getSymbol(GValue); 964 const MCExpr *SymDtprel = 965 MCSymbolRefExpr::create(MOSymbol, MCSymbolRefExpr::VK_PPC_DTPREL_LO, 966 OutContext); 967 EmitToStreamer(*OutStreamer, 968 MCInstBuilder(Subtarget->isPPC64() ? PPC::ADDI8 : PPC::ADDI) 969 .addReg(MI->getOperand(0).getReg()) 970 .addReg(MI->getOperand(1).getReg()) 971 .addExpr(SymDtprel)); 972 return; 973 } 974 case PPC::MFOCRF: 975 case PPC::MFOCRF8: 976 if (!Subtarget->hasMFOCRF()) { 977 // Transform: %R3 = MFOCRF %CR7 978 // Into: %R3 = MFCR ;; cr7 979 unsigned NewOpcode = 980 MI->getOpcode() == PPC::MFOCRF ? PPC::MFCR : PPC::MFCR8; 981 OutStreamer->AddComment(PPCInstPrinter:: 982 getRegisterName(MI->getOperand(1).getReg())); 983 EmitToStreamer(*OutStreamer, MCInstBuilder(NewOpcode) 984 .addReg(MI->getOperand(0).getReg())); 985 return; 986 } 987 break; 988 case PPC::MTOCRF: 989 case PPC::MTOCRF8: 990 if (!Subtarget->hasMFOCRF()) { 991 // Transform: %CR7 = MTOCRF %R3 992 // Into: MTCRF mask, %R3 ;; cr7 993 unsigned NewOpcode = 994 MI->getOpcode() == PPC::MTOCRF ? PPC::MTCRF : PPC::MTCRF8; 995 unsigned Mask = 0x80 >> OutContext.getRegisterInfo() 996 ->getEncodingValue(MI->getOperand(0).getReg()); 997 OutStreamer->AddComment(PPCInstPrinter:: 998 getRegisterName(MI->getOperand(0).getReg())); 999 EmitToStreamer(*OutStreamer, MCInstBuilder(NewOpcode) 1000 .addImm(Mask) 1001 .addReg(MI->getOperand(1).getReg())); 1002 return; 1003 } 1004 break; 1005 case PPC::LD: 1006 case PPC::STD: 1007 case PPC::LWA_32: 1008 case PPC::LWA: { 1009 // Verify alignment is legal, so we don't create relocations 1010 // that can't be supported. 1011 // FIXME: This test is currently disabled for Darwin. The test 1012 // suite shows a handful of test cases that fail this check for 1013 // Darwin. Those need to be investigated before this sanity test 1014 // can be enabled for those subtargets. 1015 if (!Subtarget->isDarwin()) { 1016 unsigned OpNum = (MI->getOpcode() == PPC::STD) ? 2 : 1; 1017 const MachineOperand &MO = MI->getOperand(OpNum); 1018 if (MO.isGlobal() && MO.getGlobal()->getAlignment() < 4) 1019 llvm_unreachable("Global must be word-aligned for LD, STD, LWA!"); 1020 } 1021 // Now process the instruction normally. 1022 break; 1023 } 1024 } 1025 1026 LowerPPCMachineInstrToMCInst(MI, TmpInst, *this, isDarwin); 1027 EmitToStreamer(*OutStreamer, TmpInst); 1028 } 1029 1030 void PPCLinuxAsmPrinter::EmitStartOfAsmFile(Module &M) { 1031 if (static_cast<const PPCTargetMachine &>(TM).isELFv2ABI()) { 1032 PPCTargetStreamer *TS = 1033 static_cast<PPCTargetStreamer *>(OutStreamer->getTargetStreamer()); 1034 1035 if (TS) 1036 TS->emitAbiVersion(2); 1037 } 1038 1039 if (static_cast<const PPCTargetMachine &>(TM).isPPC64() || 1040 TM.getRelocationModel() != Reloc::PIC_) 1041 return AsmPrinter::EmitStartOfAsmFile(M); 1042 1043 if (M.getPICLevel() == PICLevel::Small) 1044 return AsmPrinter::EmitStartOfAsmFile(M); 1045 1046 OutStreamer->SwitchSection(OutContext.getELFSection( 1047 ".got2", ELF::SHT_PROGBITS, ELF::SHF_WRITE | ELF::SHF_ALLOC)); 1048 1049 MCSymbol *TOCSym = OutContext.getOrCreateSymbol(Twine(".LTOC")); 1050 MCSymbol *CurrentPos = OutContext.createTempSymbol(); 1051 1052 OutStreamer->EmitLabel(CurrentPos); 1053 1054 // The GOT pointer points to the middle of the GOT, in order to reference the 1055 // entire 64kB range. 0x8000 is the midpoint. 1056 const MCExpr *tocExpr = 1057 MCBinaryExpr::createAdd(MCSymbolRefExpr::create(CurrentPos, OutContext), 1058 MCConstantExpr::create(0x8000, OutContext), 1059 OutContext); 1060 1061 OutStreamer->EmitAssignment(TOCSym, tocExpr); 1062 1063 OutStreamer->SwitchSection(getObjFileLowering().getTextSection()); 1064 } 1065 1066 void PPCLinuxAsmPrinter::EmitFunctionEntryLabel() { 1067 // linux/ppc32 - Normal entry label. 1068 if (!Subtarget->isPPC64() && 1069 (TM.getRelocationModel() != Reloc::PIC_ || 1070 MF->getFunction()->getParent()->getPICLevel() == PICLevel::Small)) 1071 return AsmPrinter::EmitFunctionEntryLabel(); 1072 1073 if (!Subtarget->isPPC64()) { 1074 const PPCFunctionInfo *PPCFI = MF->getInfo<PPCFunctionInfo>(); 1075 if (PPCFI->usesPICBase()) { 1076 MCSymbol *RelocSymbol = PPCFI->getPICOffsetSymbol(); 1077 MCSymbol *PICBase = MF->getPICBaseSymbol(); 1078 OutStreamer->EmitLabel(RelocSymbol); 1079 1080 const MCExpr *OffsExpr = 1081 MCBinaryExpr::createSub( 1082 MCSymbolRefExpr::create(OutContext.getOrCreateSymbol(Twine(".LTOC")), 1083 OutContext), 1084 MCSymbolRefExpr::create(PICBase, OutContext), 1085 OutContext); 1086 OutStreamer->EmitValue(OffsExpr, 4); 1087 OutStreamer->EmitLabel(CurrentFnSym); 1088 return; 1089 } else 1090 return AsmPrinter::EmitFunctionEntryLabel(); 1091 } 1092 1093 // ELFv2 ABI - Normal entry label. 1094 if (Subtarget->isELFv2ABI()) 1095 return AsmPrinter::EmitFunctionEntryLabel(); 1096 1097 // Emit an official procedure descriptor. 1098 MCSectionSubPair Current = OutStreamer->getCurrentSection(); 1099 MCSectionELF *Section = OutStreamer->getContext().getELFSection( 1100 ".opd", ELF::SHT_PROGBITS, ELF::SHF_WRITE | ELF::SHF_ALLOC); 1101 OutStreamer->SwitchSection(Section); 1102 OutStreamer->EmitLabel(CurrentFnSym); 1103 OutStreamer->EmitValueToAlignment(8); 1104 MCSymbol *Symbol1 = CurrentFnSymForSize; 1105 // Generates a R_PPC64_ADDR64 (from FK_DATA_8) relocation for the function 1106 // entry point. 1107 OutStreamer->EmitValue(MCSymbolRefExpr::create(Symbol1, OutContext), 1108 8 /*size*/); 1109 MCSymbol *Symbol2 = OutContext.getOrCreateSymbol(StringRef(".TOC.")); 1110 // Generates a R_PPC64_TOC relocation for TOC base insertion. 1111 OutStreamer->EmitValue( 1112 MCSymbolRefExpr::create(Symbol2, MCSymbolRefExpr::VK_PPC_TOCBASE, OutContext), 1113 8/*size*/); 1114 // Emit a null environment pointer. 1115 OutStreamer->EmitIntValue(0, 8 /* size */); 1116 OutStreamer->SwitchSection(Current.first, Current.second); 1117 } 1118 1119 1120 bool PPCLinuxAsmPrinter::doFinalization(Module &M) { 1121 const DataLayout *TD = TM.getDataLayout(); 1122 1123 bool isPPC64 = TD->getPointerSizeInBits() == 64; 1124 1125 PPCTargetStreamer &TS = 1126 static_cast<PPCTargetStreamer &>(*OutStreamer->getTargetStreamer()); 1127 1128 if (!TOC.empty()) { 1129 MCSectionELF *Section; 1130 1131 if (isPPC64) 1132 Section = OutStreamer->getContext().getELFSection( 1133 ".toc", ELF::SHT_PROGBITS, ELF::SHF_WRITE | ELF::SHF_ALLOC); 1134 else 1135 Section = OutStreamer->getContext().getELFSection( 1136 ".got2", ELF::SHT_PROGBITS, ELF::SHF_WRITE | ELF::SHF_ALLOC); 1137 OutStreamer->SwitchSection(Section); 1138 1139 for (MapVector<MCSymbol*, MCSymbol*>::iterator I = TOC.begin(), 1140 E = TOC.end(); I != E; ++I) { 1141 OutStreamer->EmitLabel(I->second); 1142 MCSymbol *S = I->first; 1143 if (isPPC64) 1144 TS.emitTCEntry(*S); 1145 else 1146 OutStreamer->EmitSymbolValue(S, 4); 1147 } 1148 } 1149 1150 return AsmPrinter::doFinalization(M); 1151 } 1152 1153 /// EmitFunctionBodyStart - Emit a global entry point prefix for ELFv2. 1154 void PPCLinuxAsmPrinter::EmitFunctionBodyStart() { 1155 // In the ELFv2 ABI, in functions that use the TOC register, we need to 1156 // provide two entry points. The ABI guarantees that when calling the 1157 // local entry point, r2 is set up by the caller to contain the TOC base 1158 // for this function, and when calling the global entry point, r12 is set 1159 // up by the caller to hold the address of the global entry point. We 1160 // thus emit a prefix sequence along the following lines: 1161 // 1162 // func: 1163 // # global entry point 1164 // addis r2,r12,(.TOC.-func)@ha 1165 // addi r2,r2,(.TOC.-func)@l 1166 // .localentry func, .-func 1167 // # local entry point, followed by function body 1168 // 1169 // This ensures we have r2 set up correctly while executing the function 1170 // body, no matter which entry point is called. 1171 if (Subtarget->isELFv2ABI() 1172 // Only do all that if the function uses r2 in the first place. 1173 && !MF->getRegInfo().use_empty(PPC::X2)) { 1174 1175 MCSymbol *GlobalEntryLabel = OutContext.createTempSymbol(); 1176 OutStreamer->EmitLabel(GlobalEntryLabel); 1177 const MCSymbolRefExpr *GlobalEntryLabelExp = 1178 MCSymbolRefExpr::create(GlobalEntryLabel, OutContext); 1179 1180 MCSymbol *TOCSymbol = OutContext.getOrCreateSymbol(StringRef(".TOC.")); 1181 const MCExpr *TOCDeltaExpr = 1182 MCBinaryExpr::createSub(MCSymbolRefExpr::create(TOCSymbol, OutContext), 1183 GlobalEntryLabelExp, OutContext); 1184 1185 const MCExpr *TOCDeltaHi = 1186 PPCMCExpr::createHa(TOCDeltaExpr, false, OutContext); 1187 EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::ADDIS) 1188 .addReg(PPC::X2) 1189 .addReg(PPC::X12) 1190 .addExpr(TOCDeltaHi)); 1191 1192 const MCExpr *TOCDeltaLo = 1193 PPCMCExpr::createLo(TOCDeltaExpr, false, OutContext); 1194 EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::ADDI) 1195 .addReg(PPC::X2) 1196 .addReg(PPC::X2) 1197 .addExpr(TOCDeltaLo)); 1198 1199 MCSymbol *LocalEntryLabel = OutContext.createTempSymbol(); 1200 OutStreamer->EmitLabel(LocalEntryLabel); 1201 const MCSymbolRefExpr *LocalEntryLabelExp = 1202 MCSymbolRefExpr::create(LocalEntryLabel, OutContext); 1203 const MCExpr *LocalOffsetExp = 1204 MCBinaryExpr::createSub(LocalEntryLabelExp, 1205 GlobalEntryLabelExp, OutContext); 1206 1207 PPCTargetStreamer *TS = 1208 static_cast<PPCTargetStreamer *>(OutStreamer->getTargetStreamer()); 1209 1210 if (TS) 1211 TS->emitLocalEntry(cast<MCSymbolELF>(CurrentFnSym), LocalOffsetExp); 1212 } 1213 } 1214 1215 /// EmitFunctionBodyEnd - Print the traceback table before the .size 1216 /// directive. 1217 /// 1218 void PPCLinuxAsmPrinter::EmitFunctionBodyEnd() { 1219 // Only the 64-bit target requires a traceback table. For now, 1220 // we only emit the word of zeroes that GDB requires to find 1221 // the end of the function, and zeroes for the eight-byte 1222 // mandatory fields. 1223 // FIXME: We should fill in the eight-byte mandatory fields as described in 1224 // the PPC64 ELF ABI (this is a low-priority item because GDB does not 1225 // currently make use of these fields). 1226 if (Subtarget->isPPC64()) { 1227 OutStreamer->EmitIntValue(0, 4/*size*/); 1228 OutStreamer->EmitIntValue(0, 8/*size*/); 1229 } 1230 } 1231 1232 void PPCDarwinAsmPrinter::EmitStartOfAsmFile(Module &M) { 1233 static const char *const CPUDirectives[] = { 1234 "", 1235 "ppc", 1236 "ppc440", 1237 "ppc601", 1238 "ppc602", 1239 "ppc603", 1240 "ppc7400", 1241 "ppc750", 1242 "ppc970", 1243 "ppcA2", 1244 "ppce500mc", 1245 "ppce5500", 1246 "power3", 1247 "power4", 1248 "power5", 1249 "power5x", 1250 "power6", 1251 "power6x", 1252 "power7", 1253 "ppc64", 1254 "ppc64le" 1255 }; 1256 1257 // Get the numerically largest directive. 1258 // FIXME: How should we merge darwin directives? 1259 unsigned Directive = PPC::DIR_NONE; 1260 for (const Function &F : M) { 1261 const PPCSubtarget &STI = TM.getSubtarget<PPCSubtarget>(F); 1262 unsigned FDir = STI.getDarwinDirective(); 1263 Directive = Directive > FDir ? FDir : STI.getDarwinDirective(); 1264 if (STI.hasMFOCRF() && Directive < PPC::DIR_970) 1265 Directive = PPC::DIR_970; 1266 if (STI.hasAltivec() && Directive < PPC::DIR_7400) 1267 Directive = PPC::DIR_7400; 1268 if (STI.isPPC64() && Directive < PPC::DIR_64) 1269 Directive = PPC::DIR_64; 1270 } 1271 1272 assert(Directive <= PPC::DIR_64 && "Directive out of range."); 1273 1274 assert(Directive < array_lengthof(CPUDirectives) && 1275 "CPUDirectives[] might not be up-to-date!"); 1276 PPCTargetStreamer &TStreamer = 1277 *static_cast<PPCTargetStreamer *>(OutStreamer->getTargetStreamer()); 1278 TStreamer.emitMachine(CPUDirectives[Directive]); 1279 1280 // Prime text sections so they are adjacent. This reduces the likelihood a 1281 // large data or debug section causes a branch to exceed 16M limit. 1282 const TargetLoweringObjectFileMachO &TLOFMacho = 1283 static_cast<const TargetLoweringObjectFileMachO &>(getObjFileLowering()); 1284 OutStreamer->SwitchSection(TLOFMacho.getTextCoalSection()); 1285 if (TM.getRelocationModel() == Reloc::PIC_) { 1286 OutStreamer->SwitchSection( 1287 OutContext.getMachOSection("__TEXT", "__picsymbolstub1", 1288 MachO::S_SYMBOL_STUBS | 1289 MachO::S_ATTR_PURE_INSTRUCTIONS, 1290 32, SectionKind::getText())); 1291 } else if (TM.getRelocationModel() == Reloc::DynamicNoPIC) { 1292 OutStreamer->SwitchSection( 1293 OutContext.getMachOSection("__TEXT","__symbol_stub1", 1294 MachO::S_SYMBOL_STUBS | 1295 MachO::S_ATTR_PURE_INSTRUCTIONS, 1296 16, SectionKind::getText())); 1297 } 1298 OutStreamer->SwitchSection(getObjFileLowering().getTextSection()); 1299 } 1300 1301 static MCSymbol *GetLazyPtr(MCSymbol *Sym, MCContext &Ctx) { 1302 // Remove $stub suffix, add $lazy_ptr. 1303 StringRef NoStub = Sym->getName().substr(0, Sym->getName().size()-5); 1304 return Ctx.getOrCreateSymbol(NoStub + "$lazy_ptr"); 1305 } 1306 1307 static MCSymbol *GetAnonSym(MCSymbol *Sym, MCContext &Ctx) { 1308 // Add $tmp suffix to $stub, yielding $stub$tmp. 1309 return Ctx.getOrCreateSymbol(Sym->getName() + "$tmp"); 1310 } 1311 1312 void PPCDarwinAsmPrinter:: 1313 EmitFunctionStubs(const MachineModuleInfoMachO::SymbolListTy &Stubs) { 1314 bool isPPC64 = TM.getDataLayout()->getPointerSizeInBits() == 64; 1315 1316 // Construct a local MCSubtargetInfo and shadow EmitToStreamer here. 1317 // This is because the MachineFunction won't exist (but have not yet been 1318 // freed) and since we're at the global level we can use the default 1319 // constructed subtarget. 1320 std::unique_ptr<MCSubtargetInfo> STI(TM.getTarget().createMCSubtargetInfo( 1321 TM.getTargetTriple().str(), TM.getTargetCPU(), 1322 TM.getTargetFeatureString())); 1323 auto EmitToStreamer = [&STI] (MCStreamer &S, const MCInst &Inst) { 1324 S.EmitInstruction(Inst, *STI); 1325 }; 1326 1327 const TargetLoweringObjectFileMachO &TLOFMacho = 1328 static_cast<const TargetLoweringObjectFileMachO &>(getObjFileLowering()); 1329 1330 // .lazy_symbol_pointer 1331 MCSection *LSPSection = TLOFMacho.getLazySymbolPointerSection(); 1332 1333 // Output stubs for dynamically-linked functions 1334 if (TM.getRelocationModel() == Reloc::PIC_) { 1335 MCSection *StubSection = OutContext.getMachOSection( 1336 "__TEXT", "__picsymbolstub1", 1337 MachO::S_SYMBOL_STUBS | MachO::S_ATTR_PURE_INSTRUCTIONS, 32, 1338 SectionKind::getText()); 1339 for (unsigned i = 0, e = Stubs.size(); i != e; ++i) { 1340 OutStreamer->SwitchSection(StubSection); 1341 EmitAlignment(4); 1342 1343 MCSymbol *Stub = Stubs[i].first; 1344 MCSymbol *RawSym = Stubs[i].second.getPointer(); 1345 MCSymbol *LazyPtr = GetLazyPtr(Stub, OutContext); 1346 MCSymbol *AnonSymbol = GetAnonSym(Stub, OutContext); 1347 1348 OutStreamer->EmitLabel(Stub); 1349 OutStreamer->EmitSymbolAttribute(RawSym, MCSA_IndirectSymbol); 1350 1351 const MCExpr *Anon = MCSymbolRefExpr::create(AnonSymbol, OutContext); 1352 const MCExpr *LazyPtrExpr = MCSymbolRefExpr::create(LazyPtr, OutContext); 1353 const MCExpr *Sub = 1354 MCBinaryExpr::createSub(LazyPtrExpr, Anon, OutContext); 1355 1356 // mflr r0 1357 EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::MFLR).addReg(PPC::R0)); 1358 // bcl 20, 31, AnonSymbol 1359 EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::BCLalways).addExpr(Anon)); 1360 OutStreamer->EmitLabel(AnonSymbol); 1361 // mflr r11 1362 EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::MFLR).addReg(PPC::R11)); 1363 // addis r11, r11, ha16(LazyPtr - AnonSymbol) 1364 const MCExpr *SubHa16 = PPCMCExpr::createHa(Sub, true, OutContext); 1365 EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::ADDIS) 1366 .addReg(PPC::R11) 1367 .addReg(PPC::R11) 1368 .addExpr(SubHa16)); 1369 // mtlr r0 1370 EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::MTLR).addReg(PPC::R0)); 1371 1372 // ldu r12, lo16(LazyPtr - AnonSymbol)(r11) 1373 // lwzu r12, lo16(LazyPtr - AnonSymbol)(r11) 1374 const MCExpr *SubLo16 = PPCMCExpr::createLo(Sub, true, OutContext); 1375 EmitToStreamer(*OutStreamer, MCInstBuilder(isPPC64 ? PPC::LDU : PPC::LWZU) 1376 .addReg(PPC::R12) 1377 .addExpr(SubLo16).addExpr(SubLo16) 1378 .addReg(PPC::R11)); 1379 // mtctr r12 1380 EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::MTCTR).addReg(PPC::R12)); 1381 // bctr 1382 EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::BCTR)); 1383 1384 OutStreamer->SwitchSection(LSPSection); 1385 OutStreamer->EmitLabel(LazyPtr); 1386 OutStreamer->EmitSymbolAttribute(RawSym, MCSA_IndirectSymbol); 1387 1388 MCSymbol *DyldStubBindingHelper = 1389 OutContext.getOrCreateSymbol(StringRef("dyld_stub_binding_helper")); 1390 if (isPPC64) { 1391 // .quad dyld_stub_binding_helper 1392 OutStreamer->EmitSymbolValue(DyldStubBindingHelper, 8); 1393 } else { 1394 // .long dyld_stub_binding_helper 1395 OutStreamer->EmitSymbolValue(DyldStubBindingHelper, 4); 1396 } 1397 } 1398 OutStreamer->AddBlankLine(); 1399 return; 1400 } 1401 1402 MCSection *StubSection = OutContext.getMachOSection( 1403 "__TEXT", "__symbol_stub1", 1404 MachO::S_SYMBOL_STUBS | MachO::S_ATTR_PURE_INSTRUCTIONS, 16, 1405 SectionKind::getText()); 1406 for (unsigned i = 0, e = Stubs.size(); i != e; ++i) { 1407 MCSymbol *Stub = Stubs[i].first; 1408 MCSymbol *RawSym = Stubs[i].second.getPointer(); 1409 MCSymbol *LazyPtr = GetLazyPtr(Stub, OutContext); 1410 const MCExpr *LazyPtrExpr = MCSymbolRefExpr::create(LazyPtr, OutContext); 1411 1412 OutStreamer->SwitchSection(StubSection); 1413 EmitAlignment(4); 1414 OutStreamer->EmitLabel(Stub); 1415 OutStreamer->EmitSymbolAttribute(RawSym, MCSA_IndirectSymbol); 1416 1417 // lis r11, ha16(LazyPtr) 1418 const MCExpr *LazyPtrHa16 = 1419 PPCMCExpr::createHa(LazyPtrExpr, true, OutContext); 1420 EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::LIS) 1421 .addReg(PPC::R11) 1422 .addExpr(LazyPtrHa16)); 1423 1424 // ldu r12, lo16(LazyPtr)(r11) 1425 // lwzu r12, lo16(LazyPtr)(r11) 1426 const MCExpr *LazyPtrLo16 = 1427 PPCMCExpr::createLo(LazyPtrExpr, true, OutContext); 1428 EmitToStreamer(*OutStreamer, MCInstBuilder(isPPC64 ? PPC::LDU : PPC::LWZU) 1429 .addReg(PPC::R12) 1430 .addExpr(LazyPtrLo16).addExpr(LazyPtrLo16) 1431 .addReg(PPC::R11)); 1432 1433 // mtctr r12 1434 EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::MTCTR).addReg(PPC::R12)); 1435 // bctr 1436 EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::BCTR)); 1437 1438 OutStreamer->SwitchSection(LSPSection); 1439 OutStreamer->EmitLabel(LazyPtr); 1440 OutStreamer->EmitSymbolAttribute(RawSym, MCSA_IndirectSymbol); 1441 1442 MCSymbol *DyldStubBindingHelper = 1443 OutContext.getOrCreateSymbol(StringRef("dyld_stub_binding_helper")); 1444 if (isPPC64) { 1445 // .quad dyld_stub_binding_helper 1446 OutStreamer->EmitSymbolValue(DyldStubBindingHelper, 8); 1447 } else { 1448 // .long dyld_stub_binding_helper 1449 OutStreamer->EmitSymbolValue(DyldStubBindingHelper, 4); 1450 } 1451 } 1452 1453 OutStreamer->AddBlankLine(); 1454 } 1455 1456 1457 bool PPCDarwinAsmPrinter::doFinalization(Module &M) { 1458 bool isPPC64 = TM.getDataLayout()->getPointerSizeInBits() == 64; 1459 1460 // Darwin/PPC always uses mach-o. 1461 const TargetLoweringObjectFileMachO &TLOFMacho = 1462 static_cast<const TargetLoweringObjectFileMachO &>(getObjFileLowering()); 1463 MachineModuleInfoMachO &MMIMacho = 1464 MMI->getObjFileInfo<MachineModuleInfoMachO>(); 1465 1466 MachineModuleInfoMachO::SymbolListTy Stubs = MMIMacho.GetFnStubList(); 1467 if (!Stubs.empty()) 1468 EmitFunctionStubs(Stubs); 1469 1470 if (MAI->doesSupportExceptionHandling() && MMI) { 1471 // Add the (possibly multiple) personalities to the set of global values. 1472 // Only referenced functions get into the Personalities list. 1473 const std::vector<const Function*> &Personalities = MMI->getPersonalities(); 1474 for (std::vector<const Function*>::const_iterator I = Personalities.begin(), 1475 E = Personalities.end(); I != E; ++I) { 1476 if (*I) { 1477 MCSymbol *NLPSym = getSymbolWithGlobalValueBase(*I, "$non_lazy_ptr"); 1478 MachineModuleInfoImpl::StubValueTy &StubSym = 1479 MMIMacho.getGVStubEntry(NLPSym); 1480 StubSym = MachineModuleInfoImpl::StubValueTy(getSymbol(*I), true); 1481 } 1482 } 1483 } 1484 1485 // Output stubs for dynamically-linked functions. 1486 Stubs = MMIMacho.GetGVStubList(); 1487 1488 // Output macho stubs for external and common global variables. 1489 if (!Stubs.empty()) { 1490 // Switch with ".non_lazy_symbol_pointer" directive. 1491 OutStreamer->SwitchSection(TLOFMacho.getNonLazySymbolPointerSection()); 1492 EmitAlignment(isPPC64 ? 3 : 2); 1493 1494 for (unsigned i = 0, e = Stubs.size(); i != e; ++i) { 1495 // L_foo$stub: 1496 OutStreamer->EmitLabel(Stubs[i].first); 1497 // .indirect_symbol _foo 1498 MachineModuleInfoImpl::StubValueTy &MCSym = Stubs[i].second; 1499 OutStreamer->EmitSymbolAttribute(MCSym.getPointer(), MCSA_IndirectSymbol); 1500 1501 if (MCSym.getInt()) 1502 // External to current translation unit. 1503 OutStreamer->EmitIntValue(0, isPPC64 ? 8 : 4/*size*/); 1504 else 1505 // Internal to current translation unit. 1506 // 1507 // When we place the LSDA into the TEXT section, the type info pointers 1508 // need to be indirect and pc-rel. We accomplish this by using NLPs. 1509 // However, sometimes the types are local to the file. So we need to 1510 // fill in the value for the NLP in those cases. 1511 OutStreamer->EmitValue(MCSymbolRefExpr::create(MCSym.getPointer(), 1512 OutContext), 1513 isPPC64 ? 8 : 4/*size*/); 1514 } 1515 1516 Stubs.clear(); 1517 OutStreamer->AddBlankLine(); 1518 } 1519 1520 Stubs = MMIMacho.GetHiddenGVStubList(); 1521 if (!Stubs.empty()) { 1522 OutStreamer->SwitchSection(getObjFileLowering().getDataSection()); 1523 EmitAlignment(isPPC64 ? 3 : 2); 1524 1525 for (unsigned i = 0, e = Stubs.size(); i != e; ++i) { 1526 // L_foo$stub: 1527 OutStreamer->EmitLabel(Stubs[i].first); 1528 // .long _foo 1529 OutStreamer->EmitValue(MCSymbolRefExpr:: 1530 create(Stubs[i].second.getPointer(), 1531 OutContext), 1532 isPPC64 ? 8 : 4/*size*/); 1533 } 1534 1535 Stubs.clear(); 1536 OutStreamer->AddBlankLine(); 1537 } 1538 1539 // Funny Darwin hack: This flag tells the linker that no global symbols 1540 // contain code that falls through to other global symbols (e.g. the obvious 1541 // implementation of multiple entry points). If this doesn't occur, the 1542 // linker can safely perform dead code stripping. Since LLVM never generates 1543 // code that does this, it is always safe to set. 1544 OutStreamer->EmitAssemblerFlag(MCAF_SubsectionsViaSymbols); 1545 1546 return AsmPrinter::doFinalization(M); 1547 } 1548 1549 /// createPPCAsmPrinterPass - Returns a pass that prints the PPC assembly code 1550 /// for a MachineFunction to the given output stream, in a format that the 1551 /// Darwin assembler can deal with. 1552 /// 1553 static AsmPrinter * 1554 createPPCAsmPrinterPass(TargetMachine &tm, 1555 std::unique_ptr<MCStreamer> &&Streamer) { 1556 if (tm.getTargetTriple().isMacOSX()) 1557 return new PPCDarwinAsmPrinter(tm, std::move(Streamer)); 1558 return new PPCLinuxAsmPrinter(tm, std::move(Streamer)); 1559 } 1560 1561 // Force static initialization. 1562 extern "C" void LLVMInitializePowerPCAsmPrinter() { 1563 TargetRegistry::RegisterAsmPrinter(ThePPC32Target, createPPCAsmPrinterPass); 1564 TargetRegistry::RegisterAsmPrinter(ThePPC64Target, createPPCAsmPrinterPass); 1565 TargetRegistry::RegisterAsmPrinter(ThePPC64LETarget, createPPCAsmPrinterPass); 1566 } 1567