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 74 public: 75 explicit PPCAsmPrinter(TargetMachine &TM, 76 std::unique_ptr<MCStreamer> Streamer) 77 : AsmPrinter(TM, std::move(Streamer)), SM(*this) {} 78 79 const char *getPassName() const override { 80 return "PowerPC Assembly Printer"; 81 } 82 83 MCSymbol *lookUpOrCreateTOCEntry(MCSymbol *Sym); 84 85 void EmitInstruction(const MachineInstr *MI) override; 86 87 void printOperand(const MachineInstr *MI, unsigned OpNo, raw_ostream &O); 88 89 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo, 90 unsigned AsmVariant, const char *ExtraCode, 91 raw_ostream &O) override; 92 bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo, 93 unsigned AsmVariant, const char *ExtraCode, 94 raw_ostream &O) override; 95 96 void EmitEndOfAsmFile(Module &M) override; 97 98 void LowerSTACKMAP(StackMaps &SM, const MachineInstr &MI); 99 void LowerPATCHPOINT(StackMaps &SM, const MachineInstr &MI); 100 void EmitTlsCall(const MachineInstr *MI, MCSymbolRefExpr::VariantKind VK); 101 bool runOnMachineFunction(MachineFunction &MF) override { 102 Subtarget = &MF.getSubtarget<PPCSubtarget>(); 103 return AsmPrinter::runOnMachineFunction(MF); 104 } 105 }; 106 107 /// PPCLinuxAsmPrinter - PowerPC assembly printer, customized for Linux 108 class PPCLinuxAsmPrinter : public PPCAsmPrinter { 109 public: 110 explicit PPCLinuxAsmPrinter(TargetMachine &TM, 111 std::unique_ptr<MCStreamer> Streamer) 112 : PPCAsmPrinter(TM, std::move(Streamer)) {} 113 114 const char *getPassName() const override { 115 return "Linux PPC Assembly Printer"; 116 } 117 118 bool doFinalization(Module &M) override; 119 void EmitStartOfAsmFile(Module &M) override; 120 121 void EmitFunctionEntryLabel() override; 122 123 void EmitFunctionBodyStart() override; 124 void EmitFunctionBodyEnd() override; 125 }; 126 127 /// PPCDarwinAsmPrinter - PowerPC assembly printer, customized for Darwin/Mac 128 /// OS X 129 class PPCDarwinAsmPrinter : public PPCAsmPrinter { 130 public: 131 explicit PPCDarwinAsmPrinter(TargetMachine &TM, 132 std::unique_ptr<MCStreamer> Streamer) 133 : PPCAsmPrinter(TM, std::move(Streamer)) {} 134 135 const char *getPassName() const override { 136 return "Darwin PPC Assembly Printer"; 137 } 138 139 bool doFinalization(Module &M) override; 140 void EmitStartOfAsmFile(Module &M) override; 141 142 void EmitFunctionStubs(const MachineModuleInfoMachO::SymbolListTy &Stubs); 143 }; 144 } // end of anonymous namespace 145 146 /// stripRegisterPrefix - This method strips the character prefix from a 147 /// register name so that only the number is left. Used by for linux asm. 148 static const char *stripRegisterPrefix(const char *RegName) { 149 switch (RegName[0]) { 150 case 'r': 151 case 'f': 152 case 'q': // for QPX 153 case 'v': 154 if (RegName[1] == 's') 155 return RegName + 2; 156 return RegName + 1; 157 case 'c': if (RegName[1] == 'r') return RegName + 2; 158 } 159 160 return RegName; 161 } 162 163 void PPCAsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNo, 164 raw_ostream &O) { 165 const DataLayout &DL = getDataLayout(); 166 const MachineOperand &MO = MI->getOperand(OpNo); 167 168 switch (MO.getType()) { 169 case MachineOperand::MO_Register: { 170 const char *RegName = PPCInstPrinter::getRegisterName(MO.getReg()); 171 // Linux assembler (Others?) does not take register mnemonics. 172 // FIXME - What about special registers used in mfspr/mtspr? 173 if (!Subtarget->isDarwin()) 174 RegName = stripRegisterPrefix(RegName); 175 O << RegName; 176 return; 177 } 178 case MachineOperand::MO_Immediate: 179 O << MO.getImm(); 180 return; 181 182 case MachineOperand::MO_MachineBasicBlock: 183 MO.getMBB()->getSymbol()->print(O, MAI); 184 return; 185 case MachineOperand::MO_ConstantPoolIndex: 186 O << DL.getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << '_' 187 << MO.getIndex(); 188 return; 189 case MachineOperand::MO_BlockAddress: 190 GetBlockAddressSymbol(MO.getBlockAddress())->print(O, MAI); 191 return; 192 case MachineOperand::MO_GlobalAddress: { 193 // Computing the address of a global symbol, not calling it. 194 const GlobalValue *GV = MO.getGlobal(); 195 MCSymbol *SymToPrint; 196 197 // External or weakly linked global variables need non-lazily-resolved stubs 198 if (TM.getRelocationModel() != Reloc::Static && 199 !GV->isStrongDefinitionForLinker()) { 200 if (!GV->hasHiddenVisibility()) { 201 SymToPrint = getSymbolWithGlobalValueBase(GV, "$non_lazy_ptr"); 202 MachineModuleInfoImpl::StubValueTy &StubSym = 203 MMI->getObjFileInfo<MachineModuleInfoMachO>().getGVStubEntry( 204 SymToPrint); 205 if (!StubSym.getPointer()) 206 StubSym = MachineModuleInfoImpl:: 207 StubValueTy(getSymbol(GV), !GV->hasInternalLinkage()); 208 } else if (GV->isDeclaration() || GV->hasCommonLinkage() || 209 GV->hasAvailableExternallyLinkage()) { 210 SymToPrint = getSymbolWithGlobalValueBase(GV, "$non_lazy_ptr"); 211 212 MachineModuleInfoImpl::StubValueTy &StubSym = 213 MMI->getObjFileInfo<MachineModuleInfoMachO>().getHiddenGVStubEntry( 214 SymToPrint); 215 if (!StubSym.getPointer()) 216 StubSym = MachineModuleInfoImpl:: 217 StubValueTy(getSymbol(GV), !GV->hasInternalLinkage()); 218 } else { 219 SymToPrint = getSymbol(GV); 220 } 221 } else { 222 SymToPrint = getSymbol(GV); 223 } 224 225 SymToPrint->print(O, MAI); 226 227 printOffset(MO.getOffset(), O); 228 return; 229 } 230 231 default: 232 O << "<unknown operand type: " << (unsigned)MO.getType() << ">"; 233 return; 234 } 235 } 236 237 /// PrintAsmOperand - Print out an operand for an inline asm expression. 238 /// 239 bool PPCAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo, 240 unsigned AsmVariant, 241 const char *ExtraCode, raw_ostream &O) { 242 // Does this asm operand have a single letter operand modifier? 243 if (ExtraCode && ExtraCode[0]) { 244 if (ExtraCode[1] != 0) return true; // Unknown modifier. 245 246 switch (ExtraCode[0]) { 247 default: 248 // See if this is a generic print operand 249 return AsmPrinter::PrintAsmOperand(MI, OpNo, AsmVariant, ExtraCode, O); 250 case 'c': // Don't print "$" before a global var name or constant. 251 break; // PPC never has a prefix. 252 case 'L': // Write second word of DImode reference. 253 // Verify that this operand has two consecutive registers. 254 if (!MI->getOperand(OpNo).isReg() || 255 OpNo+1 == MI->getNumOperands() || 256 !MI->getOperand(OpNo+1).isReg()) 257 return true; 258 ++OpNo; // Return the high-part. 259 break; 260 case 'I': 261 // Write 'i' if an integer constant, otherwise nothing. Used to print 262 // addi vs add, etc. 263 if (MI->getOperand(OpNo).isImm()) 264 O << "i"; 265 return false; 266 } 267 } 268 269 printOperand(MI, OpNo, O); 270 return false; 271 } 272 273 // At the moment, all inline asm memory operands are a single register. 274 // In any case, the output of this routine should always be just one 275 // assembler operand. 276 277 bool PPCAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo, 278 unsigned AsmVariant, 279 const char *ExtraCode, 280 raw_ostream &O) { 281 if (ExtraCode && ExtraCode[0]) { 282 if (ExtraCode[1] != 0) return true; // Unknown modifier. 283 284 switch (ExtraCode[0]) { 285 default: return true; // Unknown modifier. 286 case 'y': // A memory reference for an X-form instruction 287 { 288 const char *RegName = "r0"; 289 if (!Subtarget->isDarwin()) 290 RegName = stripRegisterPrefix(RegName); 291 O << RegName << ", "; 292 printOperand(MI, OpNo, O); 293 return false; 294 } 295 case 'U': // Print 'u' for update form. 296 case 'X': // Print 'x' for indexed form. 297 { 298 // FIXME: Currently for PowerPC memory operands are always loaded 299 // into a register, so we never get an update or indexed form. 300 // This is bad even for offset forms, since even if we know we 301 // have a value in -16(r1), we will generate a load into r<n> 302 // and then load from 0(r<n>). Until that issue is fixed, 303 // tolerate 'U' and 'X' but don't output anything. 304 assert(MI->getOperand(OpNo).isReg()); 305 return false; 306 } 307 } 308 } 309 310 assert(MI->getOperand(OpNo).isReg()); 311 O << "0("; 312 printOperand(MI, OpNo, O); 313 O << ")"; 314 return false; 315 } 316 317 /// lookUpOrCreateTOCEntry -- Given a symbol, look up whether a TOC entry 318 /// exists for it. If not, create one. Then return a symbol that references 319 /// the TOC entry. 320 MCSymbol *PPCAsmPrinter::lookUpOrCreateTOCEntry(MCSymbol *Sym) { 321 MCSymbol *&TOCEntry = TOC[Sym]; 322 if (!TOCEntry) 323 TOCEntry = createTempSymbol("C"); 324 return TOCEntry; 325 } 326 327 void PPCAsmPrinter::EmitEndOfAsmFile(Module &M) { 328 SM.serializeToStackMapSection(); 329 } 330 331 void PPCAsmPrinter::LowerSTACKMAP(StackMaps &SM, const MachineInstr &MI) { 332 unsigned NumNOPBytes = MI.getOperand(1).getImm(); 333 334 SM.recordStackMap(MI); 335 assert(NumNOPBytes % 4 == 0 && "Invalid number of NOP bytes requested!"); 336 337 // Scan ahead to trim the shadow. 338 const MachineBasicBlock &MBB = *MI.getParent(); 339 MachineBasicBlock::const_iterator MII(MI); 340 ++MII; 341 while (NumNOPBytes > 0) { 342 if (MII == MBB.end() || MII->isCall() || 343 MII->getOpcode() == PPC::DBG_VALUE || 344 MII->getOpcode() == TargetOpcode::PATCHPOINT || 345 MII->getOpcode() == TargetOpcode::STACKMAP) 346 break; 347 ++MII; 348 NumNOPBytes -= 4; 349 } 350 351 // Emit nops. 352 for (unsigned i = 0; i < NumNOPBytes; i += 4) 353 EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::NOP)); 354 } 355 356 // Lower a patchpoint of the form: 357 // [<def>], <id>, <numBytes>, <target>, <numArgs> 358 void PPCAsmPrinter::LowerPATCHPOINT(StackMaps &SM, const MachineInstr &MI) { 359 SM.recordPatchPoint(MI); 360 PatchPointOpers Opers(&MI); 361 362 unsigned EncodedBytes = 0; 363 const MachineOperand &CalleeMO = 364 Opers.getMetaOper(PatchPointOpers::TargetPos); 365 366 if (CalleeMO.isImm()) { 367 int64_t CallTarget = Opers.getMetaOper(PatchPointOpers::TargetPos).getImm(); 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 // If we're on ELFv1, then we need to load the actual function pointer 402 // from the function descriptor. 403 if (!Subtarget->isELFv2ABI()) { 404 // Load the new TOC pointer and the function address, but not r11 405 // (needing this is rare, and loading it here would prevent passing it 406 // via a 'nest' parameter. 407 EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::LD) 408 .addReg(PPC::X2) 409 .addImm(8) 410 .addReg(ScratchReg)); 411 ++EncodedBytes; 412 EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::LD) 413 .addReg(ScratchReg) 414 .addImm(0) 415 .addReg(ScratchReg)); 416 ++EncodedBytes; 417 } 418 419 EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::MTCTR8) 420 .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 } else if (CalleeMO.isGlobal()) { 433 const GlobalValue *GValue = CalleeMO.getGlobal(); 434 MCSymbol *MOSymbol = getSymbol(GValue); 435 const MCExpr *SymVar = MCSymbolRefExpr::create(MOSymbol, OutContext); 436 437 EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::BL8_NOP) 438 .addExpr(SymVar)); 439 EncodedBytes += 2; 440 } 441 442 // Each instruction is 4 bytes. 443 EncodedBytes *= 4; 444 445 // Emit padding. 446 unsigned NumBytes = Opers.getMetaOper(PatchPointOpers::NBytesPos).getImm(); 447 assert(NumBytes >= EncodedBytes && 448 "Patchpoint can't request size less than the length of a call."); 449 assert((NumBytes - EncodedBytes) % 4 == 0 && 450 "Invalid number of NOP bytes requested!"); 451 for (unsigned i = EncodedBytes; i < NumBytes; i += 4) 452 EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::NOP)); 453 } 454 455 /// EmitTlsCall -- Given a GETtls[ld]ADDR[32] instruction, print a 456 /// call to __tls_get_addr to the current output stream. 457 void PPCAsmPrinter::EmitTlsCall(const MachineInstr *MI, 458 MCSymbolRefExpr::VariantKind VK) { 459 StringRef Name = "__tls_get_addr"; 460 MCSymbol *TlsGetAddr = OutContext.getOrCreateSymbol(Name); 461 MCSymbolRefExpr::VariantKind Kind = MCSymbolRefExpr::VK_None; 462 463 assert(MI->getOperand(0).isReg() && 464 ((Subtarget->isPPC64() && MI->getOperand(0).getReg() == PPC::X3) || 465 (!Subtarget->isPPC64() && MI->getOperand(0).getReg() == PPC::R3)) && 466 "GETtls[ld]ADDR[32] must define GPR3"); 467 assert(MI->getOperand(1).isReg() && 468 ((Subtarget->isPPC64() && MI->getOperand(1).getReg() == PPC::X3) || 469 (!Subtarget->isPPC64() && MI->getOperand(1).getReg() == PPC::R3)) && 470 "GETtls[ld]ADDR[32] must read GPR3"); 471 472 if (!Subtarget->isPPC64() && !Subtarget->isDarwin() && 473 TM.getRelocationModel() == Reloc::PIC_) 474 Kind = MCSymbolRefExpr::VK_PLT; 475 const MCSymbolRefExpr *TlsRef = 476 MCSymbolRefExpr::create(TlsGetAddr, Kind, OutContext); 477 const MachineOperand &MO = MI->getOperand(2); 478 const GlobalValue *GValue = MO.getGlobal(); 479 MCSymbol *MOSymbol = getSymbol(GValue); 480 const MCExpr *SymVar = MCSymbolRefExpr::create(MOSymbol, VK, OutContext); 481 EmitToStreamer(*OutStreamer, 482 MCInstBuilder(Subtarget->isPPC64() ? 483 PPC::BL8_NOP_TLS : PPC::BL_TLS) 484 .addExpr(TlsRef) 485 .addExpr(SymVar)); 486 } 487 488 /// EmitInstruction -- Print out a single PowerPC MI in Darwin syntax to 489 /// the current output stream. 490 /// 491 void PPCAsmPrinter::EmitInstruction(const MachineInstr *MI) { 492 MCInst TmpInst; 493 bool isPPC64 = Subtarget->isPPC64(); 494 bool isDarwin = TM.getTargetTriple().isOSDarwin(); 495 const Module *M = MF->getFunction()->getParent(); 496 PICLevel::Level PL = M->getPICLevel(); 497 498 // Lower multi-instruction pseudo operations. 499 switch (MI->getOpcode()) { 500 default: break; 501 case TargetOpcode::DBG_VALUE: 502 llvm_unreachable("Should be handled target independently"); 503 case TargetOpcode::STACKMAP: 504 return LowerSTACKMAP(SM, *MI); 505 case TargetOpcode::PATCHPOINT: 506 return LowerPATCHPOINT(SM, *MI); 507 508 case PPC::MoveGOTtoLR: { 509 // Transform %LR = MoveGOTtoLR 510 // Into this: bl _GLOBAL_OFFSET_TABLE_@local-4 511 // _GLOBAL_OFFSET_TABLE_@local-4 (instruction preceding 512 // _GLOBAL_OFFSET_TABLE_) has exactly one instruction: 513 // blrl 514 // This will return the pointer to _GLOBAL_OFFSET_TABLE_@local 515 MCSymbol *GOTSymbol = 516 OutContext.getOrCreateSymbol(StringRef("_GLOBAL_OFFSET_TABLE_")); 517 const MCExpr *OffsExpr = 518 MCBinaryExpr::createSub(MCSymbolRefExpr::create(GOTSymbol, 519 MCSymbolRefExpr::VK_PPC_LOCAL, 520 OutContext), 521 MCConstantExpr::create(4, OutContext), 522 OutContext); 523 524 // Emit the 'bl'. 525 EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::BL).addExpr(OffsExpr)); 526 return; 527 } 528 case PPC::MovePCtoLR: 529 case PPC::MovePCtoLR8: { 530 // Transform %LR = MovePCtoLR 531 // Into this, where the label is the PIC base: 532 // bl L1$pb 533 // L1$pb: 534 MCSymbol *PICBase = MF->getPICBaseSymbol(); 535 536 // Emit the 'bl'. 537 EmitToStreamer(*OutStreamer, 538 MCInstBuilder(PPC::BL) 539 // FIXME: We would like an efficient form for this, so we 540 // don't have to do a lot of extra uniquing. 541 .addExpr(MCSymbolRefExpr::create(PICBase, OutContext))); 542 543 // Emit the label. 544 OutStreamer->EmitLabel(PICBase); 545 return; 546 } 547 case PPC::UpdateGBR: { 548 // Transform %Rd = UpdateGBR(%Rt, %Ri) 549 // Into: lwz %Rt, .L0$poff - .L0$pb(%Ri) 550 // add %Rd, %Rt, %Ri 551 // Get the offset from the GOT Base Register to the GOT 552 LowerPPCMachineInstrToMCInst(MI, TmpInst, *this, isDarwin); 553 MCSymbol *PICOffset = 554 MF->getInfo<PPCFunctionInfo>()->getPICOffsetSymbol(); 555 TmpInst.setOpcode(PPC::LWZ); 556 const MCExpr *Exp = 557 MCSymbolRefExpr::create(PICOffset, MCSymbolRefExpr::VK_None, OutContext); 558 const MCExpr *PB = 559 MCSymbolRefExpr::create(MF->getPICBaseSymbol(), 560 MCSymbolRefExpr::VK_None, 561 OutContext); 562 const MCOperand TR = TmpInst.getOperand(1); 563 const MCOperand PICR = TmpInst.getOperand(0); 564 565 // Step 1: lwz %Rt, .L$poff - .L$pb(%Ri) 566 TmpInst.getOperand(1) = 567 MCOperand::createExpr(MCBinaryExpr::createSub(Exp, PB, OutContext)); 568 TmpInst.getOperand(0) = TR; 569 TmpInst.getOperand(2) = PICR; 570 EmitToStreamer(*OutStreamer, TmpInst); 571 572 TmpInst.setOpcode(PPC::ADD4); 573 TmpInst.getOperand(0) = PICR; 574 TmpInst.getOperand(1) = TR; 575 TmpInst.getOperand(2) = PICR; 576 EmitToStreamer(*OutStreamer, TmpInst); 577 return; 578 } 579 case PPC::LWZtoc: { 580 // Transform %R3 = LWZtoc <ga:@min1>, %R2 581 LowerPPCMachineInstrToMCInst(MI, TmpInst, *this, isDarwin); 582 583 // Change the opcode to LWZ, and the global address operand to be a 584 // reference to the GOT entry we will synthesize later. 585 TmpInst.setOpcode(PPC::LWZ); 586 const MachineOperand &MO = MI->getOperand(1); 587 588 // Map symbol -> label of TOC entry 589 assert(MO.isGlobal() || MO.isCPI() || MO.isJTI() || MO.isBlockAddress()); 590 MCSymbol *MOSymbol = nullptr; 591 if (MO.isGlobal()) 592 MOSymbol = getSymbol(MO.getGlobal()); 593 else if (MO.isCPI()) 594 MOSymbol = GetCPISymbol(MO.getIndex()); 595 else if (MO.isJTI()) 596 MOSymbol = GetJTISymbol(MO.getIndex()); 597 else if (MO.isBlockAddress()) 598 MOSymbol = GetBlockAddressSymbol(MO.getBlockAddress()); 599 600 if (PL == PICLevel::Small) { 601 const MCExpr *Exp = 602 MCSymbolRefExpr::create(MOSymbol, MCSymbolRefExpr::VK_GOT, 603 OutContext); 604 TmpInst.getOperand(1) = MCOperand::createExpr(Exp); 605 } else { 606 MCSymbol *TOCEntry = lookUpOrCreateTOCEntry(MOSymbol); 607 608 const MCExpr *Exp = 609 MCSymbolRefExpr::create(TOCEntry, MCSymbolRefExpr::VK_None, 610 OutContext); 611 const MCExpr *PB = 612 MCSymbolRefExpr::create(OutContext.getOrCreateSymbol(Twine(".LTOC")), 613 OutContext); 614 Exp = MCBinaryExpr::createSub(Exp, PB, OutContext); 615 TmpInst.getOperand(1) = MCOperand::createExpr(Exp); 616 } 617 EmitToStreamer(*OutStreamer, TmpInst); 618 return; 619 } 620 case PPC::LDtocJTI: 621 case PPC::LDtocCPT: 622 case PPC::LDtocBA: 623 case PPC::LDtoc: { 624 // Transform %X3 = LDtoc <ga:@min1>, %X2 625 LowerPPCMachineInstrToMCInst(MI, TmpInst, *this, isDarwin); 626 627 // Change the opcode to LD, and the global address operand to be a 628 // reference to the TOC entry we will synthesize later. 629 TmpInst.setOpcode(PPC::LD); 630 const MachineOperand &MO = MI->getOperand(1); 631 632 // Map symbol -> label of TOC entry 633 assert(MO.isGlobal() || MO.isCPI() || MO.isJTI() || MO.isBlockAddress()); 634 MCSymbol *MOSymbol = nullptr; 635 if (MO.isGlobal()) 636 MOSymbol = getSymbol(MO.getGlobal()); 637 else if (MO.isCPI()) 638 MOSymbol = GetCPISymbol(MO.getIndex()); 639 else if (MO.isJTI()) 640 MOSymbol = GetJTISymbol(MO.getIndex()); 641 else if (MO.isBlockAddress()) 642 MOSymbol = GetBlockAddressSymbol(MO.getBlockAddress()); 643 644 MCSymbol *TOCEntry = lookUpOrCreateTOCEntry(MOSymbol); 645 646 const MCExpr *Exp = 647 MCSymbolRefExpr::create(TOCEntry, MCSymbolRefExpr::VK_PPC_TOC, 648 OutContext); 649 TmpInst.getOperand(1) = MCOperand::createExpr(Exp); 650 EmitToStreamer(*OutStreamer, TmpInst); 651 return; 652 } 653 654 case PPC::ADDIStocHA: { 655 // Transform %Xd = ADDIStocHA %X2, <ga:@sym> 656 LowerPPCMachineInstrToMCInst(MI, TmpInst, *this, isDarwin); 657 658 // Change the opcode to ADDIS8. If the global address is external, has 659 // common linkage, is a non-local function address, or is a jump table 660 // address, then generate a TOC entry and reference that. Otherwise 661 // reference the symbol directly. 662 TmpInst.setOpcode(PPC::ADDIS8); 663 const MachineOperand &MO = MI->getOperand(2); 664 assert((MO.isGlobal() || MO.isCPI() || MO.isJTI() || 665 MO.isBlockAddress()) && 666 "Invalid operand for ADDIStocHA!"); 667 MCSymbol *MOSymbol = nullptr; 668 bool GlobalToc = false; 669 670 if (MO.isGlobal()) { 671 const GlobalValue *GV = MO.getGlobal(); 672 MOSymbol = getSymbol(GV); 673 unsigned char GVFlags = Subtarget->classifyGlobalReference(GV); 674 GlobalToc = (GVFlags & PPCII::MO_NLP_FLAG); 675 } else if (MO.isCPI()) { 676 MOSymbol = GetCPISymbol(MO.getIndex()); 677 } else if (MO.isJTI()) { 678 MOSymbol = GetJTISymbol(MO.getIndex()); 679 } else if (MO.isBlockAddress()) { 680 MOSymbol = GetBlockAddressSymbol(MO.getBlockAddress()); 681 } 682 683 if (GlobalToc || MO.isJTI() || MO.isBlockAddress() || 684 TM.getCodeModel() == CodeModel::Large) 685 MOSymbol = lookUpOrCreateTOCEntry(MOSymbol); 686 687 const MCExpr *Exp = 688 MCSymbolRefExpr::create(MOSymbol, MCSymbolRefExpr::VK_PPC_TOC_HA, 689 OutContext); 690 TmpInst.getOperand(2) = MCOperand::createExpr(Exp); 691 EmitToStreamer(*OutStreamer, TmpInst); 692 return; 693 } 694 case PPC::LDtocL: { 695 // Transform %Xd = LDtocL <ga:@sym>, %Xs 696 LowerPPCMachineInstrToMCInst(MI, TmpInst, *this, isDarwin); 697 698 // Change the opcode to LD. If the global address is external, has 699 // common linkage, or is a jump table address, then reference the 700 // associated TOC entry. Otherwise reference the symbol directly. 701 TmpInst.setOpcode(PPC::LD); 702 const MachineOperand &MO = MI->getOperand(1); 703 assert((MO.isGlobal() || MO.isCPI() || MO.isJTI() || 704 MO.isBlockAddress()) && 705 "Invalid operand for LDtocL!"); 706 MCSymbol *MOSymbol = nullptr; 707 708 if (MO.isJTI()) 709 MOSymbol = lookUpOrCreateTOCEntry(GetJTISymbol(MO.getIndex())); 710 else if (MO.isBlockAddress()) { 711 MOSymbol = GetBlockAddressSymbol(MO.getBlockAddress()); 712 MOSymbol = lookUpOrCreateTOCEntry(MOSymbol); 713 } 714 else if (MO.isCPI()) { 715 MOSymbol = GetCPISymbol(MO.getIndex()); 716 if (TM.getCodeModel() == CodeModel::Large) 717 MOSymbol = lookUpOrCreateTOCEntry(MOSymbol); 718 } 719 else if (MO.isGlobal()) { 720 const GlobalValue *GV = MO.getGlobal(); 721 MOSymbol = getSymbol(GV); 722 DEBUG( 723 unsigned char GVFlags = Subtarget->classifyGlobalReference(GV); 724 assert((GVFlags & PPCII::MO_NLP_FLAG) && 725 "LDtocL used on symbol that could be accessed directly is " 726 "invalid. Must match ADDIStocHA.")); 727 MOSymbol = lookUpOrCreateTOCEntry(MOSymbol); 728 } 729 730 const MCExpr *Exp = 731 MCSymbolRefExpr::create(MOSymbol, MCSymbolRefExpr::VK_PPC_TOC_LO, 732 OutContext); 733 TmpInst.getOperand(1) = MCOperand::createExpr(Exp); 734 EmitToStreamer(*OutStreamer, TmpInst); 735 return; 736 } 737 case PPC::ADDItocL: { 738 // Transform %Xd = ADDItocL %Xs, <ga:@sym> 739 LowerPPCMachineInstrToMCInst(MI, TmpInst, *this, isDarwin); 740 741 // Change the opcode to ADDI8. If the global address is external, then 742 // generate a TOC entry and reference that. Otherwise reference the 743 // symbol directly. 744 TmpInst.setOpcode(PPC::ADDI8); 745 const MachineOperand &MO = MI->getOperand(2); 746 assert((MO.isGlobal() || MO.isCPI()) && "Invalid operand for ADDItocL"); 747 MCSymbol *MOSymbol = nullptr; 748 749 if (MO.isGlobal()) { 750 const GlobalValue *GV = MO.getGlobal(); 751 DEBUG( 752 unsigned char GVFlags = Subtarget->classifyGlobalReference(GV); 753 assert ( 754 !(GVFlags & PPCII::MO_NLP_FLAG) && 755 "Interposable definitions must use indirect access.")); 756 MOSymbol = getSymbol(GV); 757 } else if (MO.isCPI()) { 758 MOSymbol = GetCPISymbol(MO.getIndex()); 759 } 760 761 const MCExpr *Exp = 762 MCSymbolRefExpr::create(MOSymbol, MCSymbolRefExpr::VK_PPC_TOC_LO, 763 OutContext); 764 TmpInst.getOperand(2) = MCOperand::createExpr(Exp); 765 EmitToStreamer(*OutStreamer, TmpInst); 766 return; 767 } 768 case PPC::ADDISgotTprelHA: { 769 // Transform: %Xd = ADDISgotTprelHA %X2, <ga:@sym> 770 // Into: %Xd = ADDIS8 %X2, sym@got@tlsgd@ha 771 assert(Subtarget->isPPC64() && "Not supported for 32-bit PowerPC"); 772 const MachineOperand &MO = MI->getOperand(2); 773 const GlobalValue *GValue = MO.getGlobal(); 774 MCSymbol *MOSymbol = getSymbol(GValue); 775 const MCExpr *SymGotTprel = 776 MCSymbolRefExpr::create(MOSymbol, MCSymbolRefExpr::VK_PPC_GOT_TPREL_HA, 777 OutContext); 778 EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::ADDIS8) 779 .addReg(MI->getOperand(0).getReg()) 780 .addReg(MI->getOperand(1).getReg()) 781 .addExpr(SymGotTprel)); 782 return; 783 } 784 case PPC::LDgotTprelL: 785 case PPC::LDgotTprelL32: { 786 // Transform %Xd = LDgotTprelL <ga:@sym>, %Xs 787 LowerPPCMachineInstrToMCInst(MI, TmpInst, *this, isDarwin); 788 789 // Change the opcode to LD. 790 TmpInst.setOpcode(isPPC64 ? PPC::LD : PPC::LWZ); 791 const MachineOperand &MO = MI->getOperand(1); 792 const GlobalValue *GValue = MO.getGlobal(); 793 MCSymbol *MOSymbol = getSymbol(GValue); 794 const MCExpr *Exp = 795 MCSymbolRefExpr::create(MOSymbol, MCSymbolRefExpr::VK_PPC_GOT_TPREL_LO, 796 OutContext); 797 TmpInst.getOperand(1) = MCOperand::createExpr(Exp); 798 EmitToStreamer(*OutStreamer, TmpInst); 799 return; 800 } 801 802 case PPC::PPC32PICGOT: { 803 MCSymbol *GOTSymbol = OutContext.getOrCreateSymbol(StringRef("_GLOBAL_OFFSET_TABLE_")); 804 MCSymbol *GOTRef = OutContext.createTempSymbol(); 805 MCSymbol *NextInstr = OutContext.createTempSymbol(); 806 807 EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::BL) 808 // FIXME: We would like an efficient form for this, so we don't have to do 809 // a lot of extra uniquing. 810 .addExpr(MCSymbolRefExpr::create(NextInstr, OutContext))); 811 const MCExpr *OffsExpr = 812 MCBinaryExpr::createSub(MCSymbolRefExpr::create(GOTSymbol, OutContext), 813 MCSymbolRefExpr::create(GOTRef, OutContext), 814 OutContext); 815 OutStreamer->EmitLabel(GOTRef); 816 OutStreamer->EmitValue(OffsExpr, 4); 817 OutStreamer->EmitLabel(NextInstr); 818 EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::MFLR) 819 .addReg(MI->getOperand(0).getReg())); 820 EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::LWZ) 821 .addReg(MI->getOperand(1).getReg()) 822 .addImm(0) 823 .addReg(MI->getOperand(0).getReg())); 824 EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::ADD4) 825 .addReg(MI->getOperand(0).getReg()) 826 .addReg(MI->getOperand(1).getReg()) 827 .addReg(MI->getOperand(0).getReg())); 828 return; 829 } 830 case PPC::PPC32GOT: { 831 MCSymbol *GOTSymbol = 832 OutContext.getOrCreateSymbol(StringRef("_GLOBAL_OFFSET_TABLE_")); 833 const MCExpr *SymGotTlsL = MCSymbolRefExpr::create( 834 GOTSymbol, MCSymbolRefExpr::VK_PPC_LO, OutContext); 835 const MCExpr *SymGotTlsHA = MCSymbolRefExpr::create( 836 GOTSymbol, MCSymbolRefExpr::VK_PPC_HA, OutContext); 837 EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::LI) 838 .addReg(MI->getOperand(0).getReg()) 839 .addExpr(SymGotTlsL)); 840 EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::ADDIS) 841 .addReg(MI->getOperand(0).getReg()) 842 .addReg(MI->getOperand(0).getReg()) 843 .addExpr(SymGotTlsHA)); 844 return; 845 } 846 case PPC::ADDIStlsgdHA: { 847 // Transform: %Xd = ADDIStlsgdHA %X2, <ga:@sym> 848 // Into: %Xd = ADDIS8 %X2, sym@got@tlsgd@ha 849 assert(Subtarget->isPPC64() && "Not supported for 32-bit PowerPC"); 850 const MachineOperand &MO = MI->getOperand(2); 851 const GlobalValue *GValue = MO.getGlobal(); 852 MCSymbol *MOSymbol = getSymbol(GValue); 853 const MCExpr *SymGotTlsGD = 854 MCSymbolRefExpr::create(MOSymbol, MCSymbolRefExpr::VK_PPC_GOT_TLSGD_HA, 855 OutContext); 856 EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::ADDIS8) 857 .addReg(MI->getOperand(0).getReg()) 858 .addReg(MI->getOperand(1).getReg()) 859 .addExpr(SymGotTlsGD)); 860 return; 861 } 862 case PPC::ADDItlsgdL: 863 // Transform: %Xd = ADDItlsgdL %Xs, <ga:@sym> 864 // Into: %Xd = ADDI8 %Xs, sym@got@tlsgd@l 865 case PPC::ADDItlsgdL32: { 866 // Transform: %Rd = ADDItlsgdL32 %Rs, <ga:@sym> 867 // Into: %Rd = ADDI %Rs, sym@got@tlsgd 868 const MachineOperand &MO = MI->getOperand(2); 869 const GlobalValue *GValue = MO.getGlobal(); 870 MCSymbol *MOSymbol = getSymbol(GValue); 871 const MCExpr *SymGotTlsGD = MCSymbolRefExpr::create( 872 MOSymbol, Subtarget->isPPC64() ? MCSymbolRefExpr::VK_PPC_GOT_TLSGD_LO 873 : MCSymbolRefExpr::VK_PPC_GOT_TLSGD, 874 OutContext); 875 EmitToStreamer(*OutStreamer, 876 MCInstBuilder(Subtarget->isPPC64() ? PPC::ADDI8 : PPC::ADDI) 877 .addReg(MI->getOperand(0).getReg()) 878 .addReg(MI->getOperand(1).getReg()) 879 .addExpr(SymGotTlsGD)); 880 return; 881 } 882 case PPC::GETtlsADDR: 883 // Transform: %X3 = GETtlsADDR %X3, <ga:@sym> 884 // Into: BL8_NOP_TLS __tls_get_addr(sym at tlsgd) 885 case PPC::GETtlsADDR32: { 886 // Transform: %R3 = GETtlsADDR32 %R3, <ga:@sym> 887 // Into: BL_TLS __tls_get_addr(sym at tlsgd)@PLT 888 EmitTlsCall(MI, MCSymbolRefExpr::VK_PPC_TLSGD); 889 return; 890 } 891 case PPC::ADDIStlsldHA: { 892 // Transform: %Xd = ADDIStlsldHA %X2, <ga:@sym> 893 // Into: %Xd = ADDIS8 %X2, sym@got@tlsld@ha 894 assert(Subtarget->isPPC64() && "Not supported for 32-bit PowerPC"); 895 const MachineOperand &MO = MI->getOperand(2); 896 const GlobalValue *GValue = MO.getGlobal(); 897 MCSymbol *MOSymbol = getSymbol(GValue); 898 const MCExpr *SymGotTlsLD = 899 MCSymbolRefExpr::create(MOSymbol, MCSymbolRefExpr::VK_PPC_GOT_TLSLD_HA, 900 OutContext); 901 EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::ADDIS8) 902 .addReg(MI->getOperand(0).getReg()) 903 .addReg(MI->getOperand(1).getReg()) 904 .addExpr(SymGotTlsLD)); 905 return; 906 } 907 case PPC::ADDItlsldL: 908 // Transform: %Xd = ADDItlsldL %Xs, <ga:@sym> 909 // Into: %Xd = ADDI8 %Xs, sym@got@tlsld@l 910 case PPC::ADDItlsldL32: { 911 // Transform: %Rd = ADDItlsldL32 %Rs, <ga:@sym> 912 // Into: %Rd = ADDI %Rs, sym@got@tlsld 913 const MachineOperand &MO = MI->getOperand(2); 914 const GlobalValue *GValue = MO.getGlobal(); 915 MCSymbol *MOSymbol = getSymbol(GValue); 916 const MCExpr *SymGotTlsLD = MCSymbolRefExpr::create( 917 MOSymbol, Subtarget->isPPC64() ? MCSymbolRefExpr::VK_PPC_GOT_TLSLD_LO 918 : MCSymbolRefExpr::VK_PPC_GOT_TLSLD, 919 OutContext); 920 EmitToStreamer(*OutStreamer, 921 MCInstBuilder(Subtarget->isPPC64() ? PPC::ADDI8 : PPC::ADDI) 922 .addReg(MI->getOperand(0).getReg()) 923 .addReg(MI->getOperand(1).getReg()) 924 .addExpr(SymGotTlsLD)); 925 return; 926 } 927 case PPC::GETtlsldADDR: 928 // Transform: %X3 = GETtlsldADDR %X3, <ga:@sym> 929 // Into: BL8_NOP_TLS __tls_get_addr(sym at tlsld) 930 case PPC::GETtlsldADDR32: { 931 // Transform: %R3 = GETtlsldADDR32 %R3, <ga:@sym> 932 // Into: BL_TLS __tls_get_addr(sym at tlsld)@PLT 933 EmitTlsCall(MI, MCSymbolRefExpr::VK_PPC_TLSLD); 934 return; 935 } 936 case PPC::ADDISdtprelHA: 937 // Transform: %Xd = ADDISdtprelHA %Xs, <ga:@sym> 938 // Into: %Xd = ADDIS8 %Xs, sym@dtprel@ha 939 case PPC::ADDISdtprelHA32: { 940 // Transform: %Rd = ADDISdtprelHA32 %Rs, <ga:@sym> 941 // Into: %Rd = ADDIS %Rs, sym@dtprel@ha 942 const MachineOperand &MO = MI->getOperand(2); 943 const GlobalValue *GValue = MO.getGlobal(); 944 MCSymbol *MOSymbol = getSymbol(GValue); 945 const MCExpr *SymDtprel = 946 MCSymbolRefExpr::create(MOSymbol, MCSymbolRefExpr::VK_PPC_DTPREL_HA, 947 OutContext); 948 EmitToStreamer( 949 *OutStreamer, 950 MCInstBuilder(Subtarget->isPPC64() ? PPC::ADDIS8 : PPC::ADDIS) 951 .addReg(MI->getOperand(0).getReg()) 952 .addReg(MI->getOperand(1).getReg()) 953 .addExpr(SymDtprel)); 954 return; 955 } 956 case PPC::ADDIdtprelL: 957 // Transform: %Xd = ADDIdtprelL %Xs, <ga:@sym> 958 // Into: %Xd = ADDI8 %Xs, sym@dtprel@l 959 case PPC::ADDIdtprelL32: { 960 // Transform: %Rd = ADDIdtprelL32 %Rs, <ga:@sym> 961 // Into: %Rd = ADDI %Rs, sym@dtprel@l 962 const MachineOperand &MO = MI->getOperand(2); 963 const GlobalValue *GValue = MO.getGlobal(); 964 MCSymbol *MOSymbol = getSymbol(GValue); 965 const MCExpr *SymDtprel = 966 MCSymbolRefExpr::create(MOSymbol, MCSymbolRefExpr::VK_PPC_DTPREL_LO, 967 OutContext); 968 EmitToStreamer(*OutStreamer, 969 MCInstBuilder(Subtarget->isPPC64() ? PPC::ADDI8 : PPC::ADDI) 970 .addReg(MI->getOperand(0).getReg()) 971 .addReg(MI->getOperand(1).getReg()) 972 .addExpr(SymDtprel)); 973 return; 974 } 975 case PPC::MFOCRF: 976 case PPC::MFOCRF8: 977 if (!Subtarget->hasMFOCRF()) { 978 // Transform: %R3 = MFOCRF %CR7 979 // Into: %R3 = MFCR ;; cr7 980 unsigned NewOpcode = 981 MI->getOpcode() == PPC::MFOCRF ? PPC::MFCR : PPC::MFCR8; 982 OutStreamer->AddComment(PPCInstPrinter:: 983 getRegisterName(MI->getOperand(1).getReg())); 984 EmitToStreamer(*OutStreamer, MCInstBuilder(NewOpcode) 985 .addReg(MI->getOperand(0).getReg())); 986 return; 987 } 988 break; 989 case PPC::MTOCRF: 990 case PPC::MTOCRF8: 991 if (!Subtarget->hasMFOCRF()) { 992 // Transform: %CR7 = MTOCRF %R3 993 // Into: MTCRF mask, %R3 ;; cr7 994 unsigned NewOpcode = 995 MI->getOpcode() == PPC::MTOCRF ? PPC::MTCRF : PPC::MTCRF8; 996 unsigned Mask = 0x80 >> OutContext.getRegisterInfo() 997 ->getEncodingValue(MI->getOperand(0).getReg()); 998 OutStreamer->AddComment(PPCInstPrinter:: 999 getRegisterName(MI->getOperand(0).getReg())); 1000 EmitToStreamer(*OutStreamer, MCInstBuilder(NewOpcode) 1001 .addImm(Mask) 1002 .addReg(MI->getOperand(1).getReg())); 1003 return; 1004 } 1005 break; 1006 case PPC::LD: 1007 case PPC::STD: 1008 case PPC::LWA_32: 1009 case PPC::LWA: { 1010 // Verify alignment is legal, so we don't create relocations 1011 // that can't be supported. 1012 // FIXME: This test is currently disabled for Darwin. The test 1013 // suite shows a handful of test cases that fail this check for 1014 // Darwin. Those need to be investigated before this sanity test 1015 // can be enabled for those subtargets. 1016 if (!Subtarget->isDarwin()) { 1017 unsigned OpNum = (MI->getOpcode() == PPC::STD) ? 2 : 1; 1018 const MachineOperand &MO = MI->getOperand(OpNum); 1019 if (MO.isGlobal() && MO.getGlobal()->getAlignment() < 4) 1020 llvm_unreachable("Global must be word-aligned for LD, STD, LWA!"); 1021 } 1022 // Now process the instruction normally. 1023 break; 1024 } 1025 } 1026 1027 LowerPPCMachineInstrToMCInst(MI, TmpInst, *this, isDarwin); 1028 EmitToStreamer(*OutStreamer, TmpInst); 1029 } 1030 1031 void PPCLinuxAsmPrinter::EmitStartOfAsmFile(Module &M) { 1032 if (static_cast<const PPCTargetMachine &>(TM).isELFv2ABI()) { 1033 PPCTargetStreamer *TS = 1034 static_cast<PPCTargetStreamer *>(OutStreamer->getTargetStreamer()); 1035 1036 if (TS) 1037 TS->emitAbiVersion(2); 1038 } 1039 1040 if (static_cast<const PPCTargetMachine &>(TM).isPPC64() || 1041 TM.getRelocationModel() != Reloc::PIC_) 1042 return AsmPrinter::EmitStartOfAsmFile(M); 1043 1044 if (M.getPICLevel() == PICLevel::Small) 1045 return AsmPrinter::EmitStartOfAsmFile(M); 1046 1047 OutStreamer->SwitchSection(OutContext.getELFSection( 1048 ".got2", ELF::SHT_PROGBITS, ELF::SHF_WRITE | ELF::SHF_ALLOC)); 1049 1050 MCSymbol *TOCSym = OutContext.getOrCreateSymbol(Twine(".LTOC")); 1051 MCSymbol *CurrentPos = OutContext.createTempSymbol(); 1052 1053 OutStreamer->EmitLabel(CurrentPos); 1054 1055 // The GOT pointer points to the middle of the GOT, in order to reference the 1056 // entire 64kB range. 0x8000 is the midpoint. 1057 const MCExpr *tocExpr = 1058 MCBinaryExpr::createAdd(MCSymbolRefExpr::create(CurrentPos, OutContext), 1059 MCConstantExpr::create(0x8000, OutContext), 1060 OutContext); 1061 1062 OutStreamer->EmitAssignment(TOCSym, tocExpr); 1063 1064 OutStreamer->SwitchSection(getObjFileLowering().getTextSection()); 1065 } 1066 1067 void PPCLinuxAsmPrinter::EmitFunctionEntryLabel() { 1068 // linux/ppc32 - Normal entry label. 1069 if (!Subtarget->isPPC64() && 1070 (TM.getRelocationModel() != Reloc::PIC_ || 1071 MF->getFunction()->getParent()->getPICLevel() == PICLevel::Small)) 1072 return AsmPrinter::EmitFunctionEntryLabel(); 1073 1074 if (!Subtarget->isPPC64()) { 1075 const PPCFunctionInfo *PPCFI = MF->getInfo<PPCFunctionInfo>(); 1076 if (PPCFI->usesPICBase()) { 1077 MCSymbol *RelocSymbol = PPCFI->getPICOffsetSymbol(); 1078 MCSymbol *PICBase = MF->getPICBaseSymbol(); 1079 OutStreamer->EmitLabel(RelocSymbol); 1080 1081 const MCExpr *OffsExpr = 1082 MCBinaryExpr::createSub( 1083 MCSymbolRefExpr::create(OutContext.getOrCreateSymbol(Twine(".LTOC")), 1084 OutContext), 1085 MCSymbolRefExpr::create(PICBase, OutContext), 1086 OutContext); 1087 OutStreamer->EmitValue(OffsExpr, 4); 1088 OutStreamer->EmitLabel(CurrentFnSym); 1089 return; 1090 } else 1091 return AsmPrinter::EmitFunctionEntryLabel(); 1092 } 1093 1094 // ELFv2 ABI - Normal entry label. 1095 if (Subtarget->isELFv2ABI()) 1096 return AsmPrinter::EmitFunctionEntryLabel(); 1097 1098 // Emit an official procedure descriptor. 1099 MCSectionSubPair Current = OutStreamer->getCurrentSection(); 1100 MCSectionELF *Section = OutStreamer->getContext().getELFSection( 1101 ".opd", ELF::SHT_PROGBITS, ELF::SHF_WRITE | ELF::SHF_ALLOC); 1102 OutStreamer->SwitchSection(Section); 1103 OutStreamer->EmitLabel(CurrentFnSym); 1104 OutStreamer->EmitValueToAlignment(8); 1105 MCSymbol *Symbol1 = CurrentFnSymForSize; 1106 // Generates a R_PPC64_ADDR64 (from FK_DATA_8) relocation for the function 1107 // entry point. 1108 OutStreamer->EmitValue(MCSymbolRefExpr::create(Symbol1, OutContext), 1109 8 /*size*/); 1110 MCSymbol *Symbol2 = OutContext.getOrCreateSymbol(StringRef(".TOC.")); 1111 // Generates a R_PPC64_TOC relocation for TOC base insertion. 1112 OutStreamer->EmitValue( 1113 MCSymbolRefExpr::create(Symbol2, MCSymbolRefExpr::VK_PPC_TOCBASE, OutContext), 1114 8/*size*/); 1115 // Emit a null environment pointer. 1116 OutStreamer->EmitIntValue(0, 8 /* size */); 1117 OutStreamer->SwitchSection(Current.first, Current.second); 1118 } 1119 1120 bool PPCLinuxAsmPrinter::doFinalization(Module &M) { 1121 const DataLayout &DL = getDataLayout(); 1122 1123 bool isPPC64 = DL.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 = 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 bool PPCDarwinAsmPrinter::doFinalization(Module &M) { 1457 bool isPPC64 = getDataLayout().getPointerSizeInBits() == 64; 1458 1459 // Darwin/PPC always uses mach-o. 1460 const TargetLoweringObjectFileMachO &TLOFMacho = 1461 static_cast<const TargetLoweringObjectFileMachO &>(getObjFileLowering()); 1462 MachineModuleInfoMachO &MMIMacho = 1463 MMI->getObjFileInfo<MachineModuleInfoMachO>(); 1464 1465 MachineModuleInfoMachO::SymbolListTy Stubs = MMIMacho.GetFnStubList(); 1466 if (!Stubs.empty()) 1467 EmitFunctionStubs(Stubs); 1468 1469 if (MAI->doesSupportExceptionHandling() && MMI) { 1470 // Add the (possibly multiple) personalities to the set of global values. 1471 // Only referenced functions get into the Personalities list. 1472 for (const Function *Personality : MMI->getPersonalities()) { 1473 if (Personality) { 1474 MCSymbol *NLPSym = 1475 getSymbolWithGlobalValueBase(Personality, "$non_lazy_ptr"); 1476 MachineModuleInfoImpl::StubValueTy &StubSym = 1477 MMIMacho.getGVStubEntry(NLPSym); 1478 StubSym = 1479 MachineModuleInfoImpl::StubValueTy(getSymbol(Personality), true); 1480 } 1481 } 1482 } 1483 1484 // Output stubs for dynamically-linked functions. 1485 Stubs = MMIMacho.GetGVStubList(); 1486 1487 // Output macho stubs for external and common global variables. 1488 if (!Stubs.empty()) { 1489 // Switch with ".non_lazy_symbol_pointer" directive. 1490 OutStreamer->SwitchSection(TLOFMacho.getNonLazySymbolPointerSection()); 1491 EmitAlignment(isPPC64 ? 3 : 2); 1492 1493 for (unsigned i = 0, e = Stubs.size(); i != e; ++i) { 1494 // L_foo$stub: 1495 OutStreamer->EmitLabel(Stubs[i].first); 1496 // .indirect_symbol _foo 1497 MachineModuleInfoImpl::StubValueTy &MCSym = Stubs[i].second; 1498 OutStreamer->EmitSymbolAttribute(MCSym.getPointer(), MCSA_IndirectSymbol); 1499 1500 if (MCSym.getInt()) 1501 // External to current translation unit. 1502 OutStreamer->EmitIntValue(0, isPPC64 ? 8 : 4/*size*/); 1503 else 1504 // Internal to current translation unit. 1505 // 1506 // When we place the LSDA into the TEXT section, the type info pointers 1507 // need to be indirect and pc-rel. We accomplish this by using NLPs. 1508 // However, sometimes the types are local to the file. So we need to 1509 // fill in the value for the NLP in those cases. 1510 OutStreamer->EmitValue(MCSymbolRefExpr::create(MCSym.getPointer(), 1511 OutContext), 1512 isPPC64 ? 8 : 4/*size*/); 1513 } 1514 1515 Stubs.clear(); 1516 OutStreamer->AddBlankLine(); 1517 } 1518 1519 Stubs = MMIMacho.GetHiddenGVStubList(); 1520 if (!Stubs.empty()) { 1521 OutStreamer->SwitchSection(getObjFileLowering().getDataSection()); 1522 EmitAlignment(isPPC64 ? 3 : 2); 1523 1524 for (unsigned i = 0, e = Stubs.size(); i != e; ++i) { 1525 // L_foo$stub: 1526 OutStreamer->EmitLabel(Stubs[i].first); 1527 // .long _foo 1528 OutStreamer->EmitValue(MCSymbolRefExpr:: 1529 create(Stubs[i].second.getPointer(), 1530 OutContext), 1531 isPPC64 ? 8 : 4/*size*/); 1532 } 1533 1534 Stubs.clear(); 1535 OutStreamer->AddBlankLine(); 1536 } 1537 1538 // Funny Darwin hack: This flag tells the linker that no global symbols 1539 // contain code that falls through to other global symbols (e.g. the obvious 1540 // implementation of multiple entry points). If this doesn't occur, the 1541 // linker can safely perform dead code stripping. Since LLVM never generates 1542 // code that does this, it is always safe to set. 1543 OutStreamer->EmitAssemblerFlag(MCAF_SubsectionsViaSymbols); 1544 1545 return AsmPrinter::doFinalization(M); 1546 } 1547 1548 /// createPPCAsmPrinterPass - Returns a pass that prints the PPC assembly code 1549 /// for a MachineFunction to the given output stream, in a format that the 1550 /// Darwin assembler can deal with. 1551 /// 1552 static AsmPrinter * 1553 createPPCAsmPrinterPass(TargetMachine &tm, 1554 std::unique_ptr<MCStreamer> &&Streamer) { 1555 if (tm.getTargetTriple().isMacOSX()) 1556 return new PPCDarwinAsmPrinter(tm, std::move(Streamer)); 1557 return new PPCLinuxAsmPrinter(tm, std::move(Streamer)); 1558 } 1559 1560 // Force static initialization. 1561 extern "C" void LLVMInitializePowerPCAsmPrinter() { 1562 TargetRegistry::RegisterAsmPrinter(ThePPC32Target, createPPCAsmPrinterPass); 1563 TargetRegistry::RegisterAsmPrinter(ThePPC64Target, createPPCAsmPrinterPass); 1564 TargetRegistry::RegisterAsmPrinter(ThePPC64LETarget, createPPCAsmPrinterPass); 1565 } 1566