1 //===-- PPCAsmPrinter.cpp - Print machine instrs to PowerPC assembly ------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file contains a printer that converts from our internal representation 10 // of machine-dependent LLVM code to PowerPC assembly language. This printer is 11 // the output mechanism used by `llc'. 12 // 13 // Documentation at http://developer.apple.com/documentation/DeveloperTools/ 14 // Reference/Assembler/ASMIntroduction/chapter_1_section_1.html 15 // 16 //===----------------------------------------------------------------------===// 17 18 #include "MCTargetDesc/PPCInstPrinter.h" 19 #include "MCTargetDesc/PPCMCExpr.h" 20 #include "MCTargetDesc/PPCMCTargetDesc.h" 21 #include "MCTargetDesc/PPCPredicates.h" 22 #include "PPC.h" 23 #include "PPCInstrInfo.h" 24 #include "PPCMachineFunctionInfo.h" 25 #include "PPCSubtarget.h" 26 #include "PPCTargetMachine.h" 27 #include "PPCTargetStreamer.h" 28 #include "TargetInfo/PowerPCTargetInfo.h" 29 #include "llvm/ADT/MapVector.h" 30 #include "llvm/ADT/SmallPtrSet.h" 31 #include "llvm/ADT/StringRef.h" 32 #include "llvm/ADT/Triple.h" 33 #include "llvm/ADT/Twine.h" 34 #include "llvm/BinaryFormat/ELF.h" 35 #include "llvm/CodeGen/AsmPrinter.h" 36 #include "llvm/CodeGen/MachineBasicBlock.h" 37 #include "llvm/CodeGen/MachineFunction.h" 38 #include "llvm/CodeGen/MachineInstr.h" 39 #include "llvm/CodeGen/MachineModuleInfoImpls.h" 40 #include "llvm/CodeGen/MachineOperand.h" 41 #include "llvm/CodeGen/MachineRegisterInfo.h" 42 #include "llvm/CodeGen/StackMaps.h" 43 #include "llvm/CodeGen/TargetLoweringObjectFileImpl.h" 44 #include "llvm/IR/DataLayout.h" 45 #include "llvm/IR/GlobalValue.h" 46 #include "llvm/IR/GlobalVariable.h" 47 #include "llvm/IR/Module.h" 48 #include "llvm/MC/MCAsmInfo.h" 49 #include "llvm/MC/MCContext.h" 50 #include "llvm/MC/MCDirectives.h" 51 #include "llvm/MC/MCExpr.h" 52 #include "llvm/MC/MCInst.h" 53 #include "llvm/MC/MCInstBuilder.h" 54 #include "llvm/MC/MCSectionELF.h" 55 #include "llvm/MC/MCSectionXCOFF.h" 56 #include "llvm/MC/MCStreamer.h" 57 #include "llvm/MC/MCSymbol.h" 58 #include "llvm/MC/MCSymbolELF.h" 59 #include "llvm/MC/MCSymbolXCOFF.h" 60 #include "llvm/MC/SectionKind.h" 61 #include "llvm/MC/TargetRegistry.h" 62 #include "llvm/Support/Casting.h" 63 #include "llvm/Support/CodeGen.h" 64 #include "llvm/Support/Debug.h" 65 #include "llvm/Support/Error.h" 66 #include "llvm/Support/ErrorHandling.h" 67 #include "llvm/Support/Process.h" 68 #include "llvm/Support/raw_ostream.h" 69 #include "llvm/Target/TargetMachine.h" 70 #include "llvm/Transforms/Utils/ModuleUtils.h" 71 #include <algorithm> 72 #include <cassert> 73 #include <cstdint> 74 #include <memory> 75 #include <new> 76 77 using namespace llvm; 78 using namespace llvm::XCOFF; 79 80 #define DEBUG_TYPE "asmprinter" 81 82 static cl::opt<bool> EnableSSPCanaryBitInTB( 83 "aix-ssp-tb-bit", cl::init(false), 84 cl::desc("Enable Passing SSP Canary info in Trackback on AIX"), cl::Hidden); 85 86 // Specialize DenseMapInfo to allow 87 // std::pair<const MCSymbol *, MCSymbolRefExpr::VariantKind> in DenseMap. 88 // This specialization is needed here because that type is used as keys in the 89 // map representing TOC entries. 90 namespace llvm { 91 template <> 92 struct DenseMapInfo<std::pair<const MCSymbol *, MCSymbolRefExpr::VariantKind>> { 93 using TOCKey = std::pair<const MCSymbol *, MCSymbolRefExpr::VariantKind>; 94 95 static inline TOCKey getEmptyKey() { 96 return {nullptr, MCSymbolRefExpr::VariantKind::VK_None}; 97 } 98 static inline TOCKey getTombstoneKey() { 99 return {nullptr, MCSymbolRefExpr::VariantKind::VK_Invalid}; 100 } 101 static unsigned getHashValue(const TOCKey &PairVal) { 102 return detail::combineHashValue( 103 DenseMapInfo<const MCSymbol *>::getHashValue(PairVal.first), 104 DenseMapInfo<int>::getHashValue(PairVal.second)); 105 } 106 static bool isEqual(const TOCKey &A, const TOCKey &B) { return A == B; } 107 }; 108 } // end namespace llvm 109 110 namespace { 111 112 enum { 113 // GNU attribute tags for PowerPC ABI 114 Tag_GNU_Power_ABI_FP = 4, 115 Tag_GNU_Power_ABI_Vector = 8, 116 Tag_GNU_Power_ABI_Struct_Return = 12, 117 118 // GNU attribute values for PowerPC float ABI, as combination of two parts 119 Val_GNU_Power_ABI_NoFloat = 0b00, 120 Val_GNU_Power_ABI_HardFloat_DP = 0b01, 121 Val_GNU_Power_ABI_SoftFloat_DP = 0b10, 122 Val_GNU_Power_ABI_HardFloat_SP = 0b11, 123 124 Val_GNU_Power_ABI_LDBL_IBM128 = 0b0100, 125 Val_GNU_Power_ABI_LDBL_64 = 0b1000, 126 Val_GNU_Power_ABI_LDBL_IEEE128 = 0b1100, 127 }; 128 129 class PPCAsmPrinter : public AsmPrinter { 130 protected: 131 // For TLS on AIX, we need to be able to identify TOC entries of specific 132 // VariantKind so we can add the right relocations when we generate the 133 // entries. So each entry is represented by a pair of MCSymbol and 134 // VariantKind. For example, we need to be able to identify the following 135 // entry as a TLSGD entry so we can add the @m relocation: 136 // .tc .i[TC],i[TL]@m 137 // By default, VK_None is used for the VariantKind. 138 MapVector<std::pair<const MCSymbol *, MCSymbolRefExpr::VariantKind>, 139 MCSymbol *> 140 TOC; 141 const PPCSubtarget *Subtarget = nullptr; 142 StackMaps SM; 143 144 public: 145 explicit PPCAsmPrinter(TargetMachine &TM, 146 std::unique_ptr<MCStreamer> Streamer) 147 : AsmPrinter(TM, std::move(Streamer)), SM(*this) {} 148 149 StringRef getPassName() const override { return "PowerPC Assembly Printer"; } 150 151 MCSymbol *lookUpOrCreateTOCEntry(const MCSymbol *Sym, 152 MCSymbolRefExpr::VariantKind Kind = 153 MCSymbolRefExpr::VariantKind::VK_None); 154 155 bool doInitialization(Module &M) override { 156 if (!TOC.empty()) 157 TOC.clear(); 158 return AsmPrinter::doInitialization(M); 159 } 160 161 void emitInstruction(const MachineInstr *MI) override; 162 163 /// This function is for PrintAsmOperand and PrintAsmMemoryOperand, 164 /// invoked by EmitMSInlineAsmStr and EmitGCCInlineAsmStr only. 165 /// The \p MI would be INLINEASM ONLY. 166 void printOperand(const MachineInstr *MI, unsigned OpNo, raw_ostream &O); 167 168 void PrintSymbolOperand(const MachineOperand &MO, raw_ostream &O) override; 169 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo, 170 const char *ExtraCode, raw_ostream &O) override; 171 bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo, 172 const char *ExtraCode, raw_ostream &O) override; 173 174 void emitEndOfAsmFile(Module &M) override; 175 176 void LowerSTACKMAP(StackMaps &SM, const MachineInstr &MI); 177 void LowerPATCHPOINT(StackMaps &SM, const MachineInstr &MI); 178 void EmitTlsCall(const MachineInstr *MI, MCSymbolRefExpr::VariantKind VK); 179 bool runOnMachineFunction(MachineFunction &MF) override { 180 Subtarget = &MF.getSubtarget<PPCSubtarget>(); 181 bool Changed = AsmPrinter::runOnMachineFunction(MF); 182 emitXRayTable(); 183 return Changed; 184 } 185 }; 186 187 /// PPCLinuxAsmPrinter - PowerPC assembly printer, customized for Linux 188 class PPCLinuxAsmPrinter : public PPCAsmPrinter { 189 public: 190 explicit PPCLinuxAsmPrinter(TargetMachine &TM, 191 std::unique_ptr<MCStreamer> Streamer) 192 : PPCAsmPrinter(TM, std::move(Streamer)) {} 193 194 StringRef getPassName() const override { 195 return "Linux PPC Assembly Printer"; 196 } 197 198 void emitGNUAttributes(Module &M); 199 200 void emitStartOfAsmFile(Module &M) override; 201 void emitEndOfAsmFile(Module &) override; 202 203 void emitFunctionEntryLabel() override; 204 205 void emitFunctionBodyStart() override; 206 void emitFunctionBodyEnd() override; 207 void emitInstruction(const MachineInstr *MI) override; 208 }; 209 210 class PPCAIXAsmPrinter : public PPCAsmPrinter { 211 private: 212 /// Symbols lowered from ExternalSymbolSDNodes, we will need to emit extern 213 /// linkage for them in AIX. 214 SmallPtrSet<MCSymbol *, 8> ExtSymSDNodeSymbols; 215 216 /// A format indicator and unique trailing identifier to form part of the 217 /// sinit/sterm function names. 218 std::string FormatIndicatorAndUniqueModId; 219 220 // Record a list of GlobalAlias associated with a GlobalObject. 221 // This is used for AIX's extra-label-at-definition aliasing strategy. 222 DenseMap<const GlobalObject *, SmallVector<const GlobalAlias *, 1>> 223 GOAliasMap; 224 225 uint16_t getNumberOfVRSaved(); 226 void emitTracebackTable(); 227 228 SmallVector<const GlobalVariable *, 8> TOCDataGlobalVars; 229 230 void emitGlobalVariableHelper(const GlobalVariable *); 231 232 public: 233 PPCAIXAsmPrinter(TargetMachine &TM, std::unique_ptr<MCStreamer> Streamer) 234 : PPCAsmPrinter(TM, std::move(Streamer)) { 235 if (MAI->isLittleEndian()) 236 report_fatal_error( 237 "cannot create AIX PPC Assembly Printer for a little-endian target"); 238 } 239 240 StringRef getPassName() const override { return "AIX PPC Assembly Printer"; } 241 242 bool doInitialization(Module &M) override; 243 244 void emitXXStructorList(const DataLayout &DL, const Constant *List, 245 bool IsCtor) override; 246 247 void SetupMachineFunction(MachineFunction &MF) override; 248 249 void emitGlobalVariable(const GlobalVariable *GV) override; 250 251 void emitFunctionDescriptor() override; 252 253 void emitFunctionEntryLabel() override; 254 255 void emitFunctionBodyEnd() override; 256 257 void emitEndOfAsmFile(Module &) override; 258 259 void emitLinkage(const GlobalValue *GV, MCSymbol *GVSym) const override; 260 261 void emitInstruction(const MachineInstr *MI) override; 262 263 bool doFinalization(Module &M) override; 264 265 void emitTTypeReference(const GlobalValue *GV, unsigned Encoding) override; 266 }; 267 268 } // end anonymous namespace 269 270 void PPCAsmPrinter::PrintSymbolOperand(const MachineOperand &MO, 271 raw_ostream &O) { 272 // Computing the address of a global symbol, not calling it. 273 const GlobalValue *GV = MO.getGlobal(); 274 getSymbol(GV)->print(O, MAI); 275 printOffset(MO.getOffset(), O); 276 } 277 278 void PPCAsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNo, 279 raw_ostream &O) { 280 const DataLayout &DL = getDataLayout(); 281 const MachineOperand &MO = MI->getOperand(OpNo); 282 283 switch (MO.getType()) { 284 case MachineOperand::MO_Register: { 285 // The MI is INLINEASM ONLY and UseVSXReg is always false. 286 const char *RegName = PPCInstPrinter::getRegisterName(MO.getReg()); 287 288 // Linux assembler (Others?) does not take register mnemonics. 289 // FIXME - What about special registers used in mfspr/mtspr? 290 O << PPCRegisterInfo::stripRegisterPrefix(RegName); 291 return; 292 } 293 case MachineOperand::MO_Immediate: 294 O << MO.getImm(); 295 return; 296 297 case MachineOperand::MO_MachineBasicBlock: 298 MO.getMBB()->getSymbol()->print(O, MAI); 299 return; 300 case MachineOperand::MO_ConstantPoolIndex: 301 O << DL.getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << '_' 302 << MO.getIndex(); 303 return; 304 case MachineOperand::MO_BlockAddress: 305 GetBlockAddressSymbol(MO.getBlockAddress())->print(O, MAI); 306 return; 307 case MachineOperand::MO_GlobalAddress: { 308 PrintSymbolOperand(MO, O); 309 return; 310 } 311 312 default: 313 O << "<unknown operand type: " << (unsigned)MO.getType() << ">"; 314 return; 315 } 316 } 317 318 /// PrintAsmOperand - Print out an operand for an inline asm expression. 319 /// 320 bool PPCAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo, 321 const char *ExtraCode, raw_ostream &O) { 322 // Does this asm operand have a single letter operand modifier? 323 if (ExtraCode && ExtraCode[0]) { 324 if (ExtraCode[1] != 0) return true; // Unknown modifier. 325 326 switch (ExtraCode[0]) { 327 default: 328 // See if this is a generic print operand 329 return AsmPrinter::PrintAsmOperand(MI, OpNo, ExtraCode, O); 330 case 'L': // Write second word of DImode reference. 331 // Verify that this operand has two consecutive registers. 332 if (!MI->getOperand(OpNo).isReg() || 333 OpNo+1 == MI->getNumOperands() || 334 !MI->getOperand(OpNo+1).isReg()) 335 return true; 336 ++OpNo; // Return the high-part. 337 break; 338 case 'I': 339 // Write 'i' if an integer constant, otherwise nothing. Used to print 340 // addi vs add, etc. 341 if (MI->getOperand(OpNo).isImm()) 342 O << "i"; 343 return false; 344 case 'x': 345 if(!MI->getOperand(OpNo).isReg()) 346 return true; 347 // This operand uses VSX numbering. 348 // If the operand is a VMX register, convert it to a VSX register. 349 Register Reg = MI->getOperand(OpNo).getReg(); 350 if (PPCInstrInfo::isVRRegister(Reg)) 351 Reg = PPC::VSX32 + (Reg - PPC::V0); 352 else if (PPCInstrInfo::isVFRegister(Reg)) 353 Reg = PPC::VSX32 + (Reg - PPC::VF0); 354 const char *RegName; 355 RegName = PPCInstPrinter::getRegisterName(Reg); 356 RegName = PPCRegisterInfo::stripRegisterPrefix(RegName); 357 O << RegName; 358 return false; 359 } 360 } 361 362 printOperand(MI, OpNo, O); 363 return false; 364 } 365 366 // At the moment, all inline asm memory operands are a single register. 367 // In any case, the output of this routine should always be just one 368 // assembler operand. 369 bool PPCAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo, 370 const char *ExtraCode, 371 raw_ostream &O) { 372 if (ExtraCode && ExtraCode[0]) { 373 if (ExtraCode[1] != 0) return true; // Unknown modifier. 374 375 switch (ExtraCode[0]) { 376 default: return true; // Unknown modifier. 377 case 'L': // A memory reference to the upper word of a double word op. 378 O << getDataLayout().getPointerSize() << "("; 379 printOperand(MI, OpNo, O); 380 O << ")"; 381 return false; 382 case 'y': // A memory reference for an X-form instruction 383 O << "0, "; 384 printOperand(MI, OpNo, O); 385 return false; 386 case 'I': 387 // Write 'i' if an integer constant, otherwise nothing. Used to print 388 // addi vs add, etc. 389 if (MI->getOperand(OpNo).isImm()) 390 O << "i"; 391 return false; 392 case 'U': // Print 'u' for update form. 393 case 'X': // Print 'x' for indexed form. 394 // FIXME: Currently for PowerPC memory operands are always loaded 395 // into a register, so we never get an update or indexed form. 396 // This is bad even for offset forms, since even if we know we 397 // have a value in -16(r1), we will generate a load into r<n> 398 // and then load from 0(r<n>). Until that issue is fixed, 399 // tolerate 'U' and 'X' but don't output anything. 400 assert(MI->getOperand(OpNo).isReg()); 401 return false; 402 } 403 } 404 405 assert(MI->getOperand(OpNo).isReg()); 406 O << "0("; 407 printOperand(MI, OpNo, O); 408 O << ")"; 409 return false; 410 } 411 412 /// lookUpOrCreateTOCEntry -- Given a symbol, look up whether a TOC entry 413 /// exists for it. If not, create one. Then return a symbol that references 414 /// the TOC entry. 415 MCSymbol * 416 PPCAsmPrinter::lookUpOrCreateTOCEntry(const MCSymbol *Sym, 417 MCSymbolRefExpr::VariantKind Kind) { 418 MCSymbol *&TOCEntry = TOC[{Sym, Kind}]; 419 if (!TOCEntry) 420 TOCEntry = createTempSymbol("C"); 421 return TOCEntry; 422 } 423 424 void PPCAsmPrinter::emitEndOfAsmFile(Module &M) { 425 emitStackMaps(SM); 426 } 427 428 void PPCAsmPrinter::LowerSTACKMAP(StackMaps &SM, const MachineInstr &MI) { 429 unsigned NumNOPBytes = MI.getOperand(1).getImm(); 430 431 auto &Ctx = OutStreamer->getContext(); 432 MCSymbol *MILabel = Ctx.createTempSymbol(); 433 OutStreamer->emitLabel(MILabel); 434 435 SM.recordStackMap(*MILabel, MI); 436 assert(NumNOPBytes % 4 == 0 && "Invalid number of NOP bytes requested!"); 437 438 // Scan ahead to trim the shadow. 439 const MachineBasicBlock &MBB = *MI.getParent(); 440 MachineBasicBlock::const_iterator MII(MI); 441 ++MII; 442 while (NumNOPBytes > 0) { 443 if (MII == MBB.end() || MII->isCall() || 444 MII->getOpcode() == PPC::DBG_VALUE || 445 MII->getOpcode() == TargetOpcode::PATCHPOINT || 446 MII->getOpcode() == TargetOpcode::STACKMAP) 447 break; 448 ++MII; 449 NumNOPBytes -= 4; 450 } 451 452 // Emit nops. 453 for (unsigned i = 0; i < NumNOPBytes; i += 4) 454 EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::NOP)); 455 } 456 457 // Lower a patchpoint of the form: 458 // [<def>], <id>, <numBytes>, <target>, <numArgs> 459 void PPCAsmPrinter::LowerPATCHPOINT(StackMaps &SM, const MachineInstr &MI) { 460 auto &Ctx = OutStreamer->getContext(); 461 MCSymbol *MILabel = Ctx.createTempSymbol(); 462 OutStreamer->emitLabel(MILabel); 463 464 SM.recordPatchPoint(*MILabel, MI); 465 PatchPointOpers Opers(&MI); 466 467 unsigned EncodedBytes = 0; 468 const MachineOperand &CalleeMO = Opers.getCallTarget(); 469 470 if (CalleeMO.isImm()) { 471 int64_t CallTarget = CalleeMO.getImm(); 472 if (CallTarget) { 473 assert((CallTarget & 0xFFFFFFFFFFFF) == CallTarget && 474 "High 16 bits of call target should be zero."); 475 Register ScratchReg = MI.getOperand(Opers.getNextScratchIdx()).getReg(); 476 EncodedBytes = 0; 477 // Materialize the jump address: 478 EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::LI8) 479 .addReg(ScratchReg) 480 .addImm((CallTarget >> 32) & 0xFFFF)); 481 ++EncodedBytes; 482 EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::RLDIC) 483 .addReg(ScratchReg) 484 .addReg(ScratchReg) 485 .addImm(32).addImm(16)); 486 ++EncodedBytes; 487 EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::ORIS8) 488 .addReg(ScratchReg) 489 .addReg(ScratchReg) 490 .addImm((CallTarget >> 16) & 0xFFFF)); 491 ++EncodedBytes; 492 EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::ORI8) 493 .addReg(ScratchReg) 494 .addReg(ScratchReg) 495 .addImm(CallTarget & 0xFFFF)); 496 497 // Save the current TOC pointer before the remote call. 498 int TOCSaveOffset = Subtarget->getFrameLowering()->getTOCSaveOffset(); 499 EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::STD) 500 .addReg(PPC::X2) 501 .addImm(TOCSaveOffset) 502 .addReg(PPC::X1)); 503 ++EncodedBytes; 504 505 // If we're on ELFv1, then we need to load the actual function pointer 506 // from the function descriptor. 507 if (!Subtarget->isELFv2ABI()) { 508 // Load the new TOC pointer and the function address, but not r11 509 // (needing this is rare, and loading it here would prevent passing it 510 // via a 'nest' parameter. 511 EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::LD) 512 .addReg(PPC::X2) 513 .addImm(8) 514 .addReg(ScratchReg)); 515 ++EncodedBytes; 516 EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::LD) 517 .addReg(ScratchReg) 518 .addImm(0) 519 .addReg(ScratchReg)); 520 ++EncodedBytes; 521 } 522 523 EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::MTCTR8) 524 .addReg(ScratchReg)); 525 ++EncodedBytes; 526 EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::BCTRL8)); 527 ++EncodedBytes; 528 529 // Restore the TOC pointer after the call. 530 EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::LD) 531 .addReg(PPC::X2) 532 .addImm(TOCSaveOffset) 533 .addReg(PPC::X1)); 534 ++EncodedBytes; 535 } 536 } else if (CalleeMO.isGlobal()) { 537 const GlobalValue *GValue = CalleeMO.getGlobal(); 538 MCSymbol *MOSymbol = getSymbol(GValue); 539 const MCExpr *SymVar = MCSymbolRefExpr::create(MOSymbol, OutContext); 540 541 EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::BL8_NOP) 542 .addExpr(SymVar)); 543 EncodedBytes += 2; 544 } 545 546 // Each instruction is 4 bytes. 547 EncodedBytes *= 4; 548 549 // Emit padding. 550 unsigned NumBytes = Opers.getNumPatchBytes(); 551 assert(NumBytes >= EncodedBytes && 552 "Patchpoint can't request size less than the length of a call."); 553 assert((NumBytes - EncodedBytes) % 4 == 0 && 554 "Invalid number of NOP bytes requested!"); 555 for (unsigned i = EncodedBytes; i < NumBytes; i += 4) 556 EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::NOP)); 557 } 558 559 /// This helper function creates the TlsGetAddr MCSymbol for AIX. We will 560 /// create the csect and use the qual-name symbol instead of creating just the 561 /// external symbol. 562 static MCSymbol *createMCSymbolForTlsGetAddr(MCContext &Ctx) { 563 return Ctx 564 .getXCOFFSection(".__tls_get_addr", SectionKind::getText(), 565 XCOFF::CsectProperties(XCOFF::XMC_PR, XCOFF::XTY_ER)) 566 ->getQualNameSymbol(); 567 } 568 569 /// EmitTlsCall -- Given a GETtls[ld]ADDR[32] instruction, print a 570 /// call to __tls_get_addr to the current output stream. 571 void PPCAsmPrinter::EmitTlsCall(const MachineInstr *MI, 572 MCSymbolRefExpr::VariantKind VK) { 573 MCSymbolRefExpr::VariantKind Kind = MCSymbolRefExpr::VK_None; 574 unsigned Opcode = PPC::BL8_NOP_TLS; 575 576 assert(MI->getNumOperands() >= 3 && "Expecting at least 3 operands from MI"); 577 if (MI->getOperand(2).getTargetFlags() == PPCII::MO_GOT_TLSGD_PCREL_FLAG || 578 MI->getOperand(2).getTargetFlags() == PPCII::MO_GOT_TLSLD_PCREL_FLAG) { 579 Kind = MCSymbolRefExpr::VK_PPC_NOTOC; 580 Opcode = PPC::BL8_NOTOC_TLS; 581 } 582 const Module *M = MF->getFunction().getParent(); 583 584 assert(MI->getOperand(0).isReg() && 585 ((Subtarget->isPPC64() && MI->getOperand(0).getReg() == PPC::X3) || 586 (!Subtarget->isPPC64() && MI->getOperand(0).getReg() == PPC::R3)) && 587 "GETtls[ld]ADDR[32] must define GPR3"); 588 assert(MI->getOperand(1).isReg() && 589 ((Subtarget->isPPC64() && MI->getOperand(1).getReg() == PPC::X3) || 590 (!Subtarget->isPPC64() && MI->getOperand(1).getReg() == PPC::R3)) && 591 "GETtls[ld]ADDR[32] must read GPR3"); 592 593 if (Subtarget->isAIXABI()) { 594 // On AIX, the variable offset should already be in R4 and the region handle 595 // should already be in R3. 596 // For TLSGD, which currently is the only supported access model, we only 597 // need to generate an absolute branch to .__tls_get_addr. 598 Register VarOffsetReg = Subtarget->isPPC64() ? PPC::X4 : PPC::R4; 599 (void)VarOffsetReg; 600 assert(MI->getOperand(2).isReg() && 601 MI->getOperand(2).getReg() == VarOffsetReg && 602 "GETtls[ld]ADDR[32] must read GPR4"); 603 MCSymbol *TlsGetAddr = createMCSymbolForTlsGetAddr(OutContext); 604 const MCExpr *TlsRef = MCSymbolRefExpr::create( 605 TlsGetAddr, MCSymbolRefExpr::VK_None, OutContext); 606 EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::BLA).addExpr(TlsRef)); 607 return; 608 } 609 610 MCSymbol *TlsGetAddr = OutContext.getOrCreateSymbol("__tls_get_addr"); 611 612 if (Subtarget->is32BitELFABI() && isPositionIndependent()) 613 Kind = MCSymbolRefExpr::VK_PLT; 614 615 const MCExpr *TlsRef = 616 MCSymbolRefExpr::create(TlsGetAddr, Kind, OutContext); 617 618 // Add 32768 offset to the symbol so we follow up the latest GOT/PLT ABI. 619 if (Kind == MCSymbolRefExpr::VK_PLT && Subtarget->isSecurePlt() && 620 M->getPICLevel() == PICLevel::BigPIC) 621 TlsRef = MCBinaryExpr::createAdd( 622 TlsRef, MCConstantExpr::create(32768, OutContext), OutContext); 623 const MachineOperand &MO = MI->getOperand(2); 624 const GlobalValue *GValue = MO.getGlobal(); 625 MCSymbol *MOSymbol = getSymbol(GValue); 626 const MCExpr *SymVar = MCSymbolRefExpr::create(MOSymbol, VK, OutContext); 627 EmitToStreamer(*OutStreamer, 628 MCInstBuilder(Subtarget->isPPC64() ? Opcode 629 : (unsigned)PPC::BL_TLS) 630 .addExpr(TlsRef) 631 .addExpr(SymVar)); 632 } 633 634 /// Map a machine operand for a TOC pseudo-machine instruction to its 635 /// corresponding MCSymbol. 636 static MCSymbol *getMCSymbolForTOCPseudoMO(const MachineOperand &MO, 637 AsmPrinter &AP) { 638 switch (MO.getType()) { 639 case MachineOperand::MO_GlobalAddress: 640 return AP.getSymbol(MO.getGlobal()); 641 case MachineOperand::MO_ConstantPoolIndex: 642 return AP.GetCPISymbol(MO.getIndex()); 643 case MachineOperand::MO_JumpTableIndex: 644 return AP.GetJTISymbol(MO.getIndex()); 645 case MachineOperand::MO_BlockAddress: 646 return AP.GetBlockAddressSymbol(MO.getBlockAddress()); 647 default: 648 llvm_unreachable("Unexpected operand type to get symbol."); 649 } 650 } 651 652 /// EmitInstruction -- Print out a single PowerPC MI in Darwin syntax to 653 /// the current output stream. 654 /// 655 void PPCAsmPrinter::emitInstruction(const MachineInstr *MI) { 656 MCInst TmpInst; 657 const bool IsPPC64 = Subtarget->isPPC64(); 658 const bool IsAIX = Subtarget->isAIXABI(); 659 const Module *M = MF->getFunction().getParent(); 660 PICLevel::Level PL = M->getPICLevel(); 661 662 #ifndef NDEBUG 663 // Validate that SPE and FPU are mutually exclusive in codegen 664 if (!MI->isInlineAsm()) { 665 for (const MachineOperand &MO: MI->operands()) { 666 if (MO.isReg()) { 667 Register Reg = MO.getReg(); 668 if (Subtarget->hasSPE()) { 669 if (PPC::F4RCRegClass.contains(Reg) || 670 PPC::F8RCRegClass.contains(Reg) || 671 PPC::VFRCRegClass.contains(Reg) || 672 PPC::VRRCRegClass.contains(Reg) || 673 PPC::VSFRCRegClass.contains(Reg) || 674 PPC::VSSRCRegClass.contains(Reg) 675 ) 676 llvm_unreachable("SPE targets cannot have FPRegs!"); 677 } else { 678 if (PPC::SPERCRegClass.contains(Reg)) 679 llvm_unreachable("SPE register found in FPU-targeted code!"); 680 } 681 } 682 } 683 } 684 #endif 685 686 auto getTOCRelocAdjustedExprForXCOFF = [this](const MCExpr *Expr, 687 ptrdiff_t OriginalOffset) { 688 // Apply an offset to the TOC-based expression such that the adjusted 689 // notional offset from the TOC base (to be encoded into the instruction's D 690 // or DS field) is the signed 16-bit truncation of the original notional 691 // offset from the TOC base. 692 // This is consistent with the treatment used both by XL C/C++ and 693 // by AIX ld -r. 694 ptrdiff_t Adjustment = 695 OriginalOffset - llvm::SignExtend32<16>(OriginalOffset); 696 return MCBinaryExpr::createAdd( 697 Expr, MCConstantExpr::create(-Adjustment, OutContext), OutContext); 698 }; 699 700 auto getTOCEntryLoadingExprForXCOFF = 701 [IsPPC64, getTOCRelocAdjustedExprForXCOFF, 702 this](const MCSymbol *MOSymbol, const MCExpr *Expr, 703 MCSymbolRefExpr::VariantKind VK = 704 MCSymbolRefExpr::VariantKind::VK_None) -> const MCExpr * { 705 const unsigned EntryByteSize = IsPPC64 ? 8 : 4; 706 const auto TOCEntryIter = TOC.find({MOSymbol, VK}); 707 assert(TOCEntryIter != TOC.end() && 708 "Could not find the TOC entry for this symbol."); 709 const ptrdiff_t EntryDistanceFromTOCBase = 710 (TOCEntryIter - TOC.begin()) * EntryByteSize; 711 constexpr int16_t PositiveTOCRange = INT16_MAX; 712 713 if (EntryDistanceFromTOCBase > PositiveTOCRange) 714 return getTOCRelocAdjustedExprForXCOFF(Expr, EntryDistanceFromTOCBase); 715 716 return Expr; 717 }; 718 auto GetVKForMO = [&](const MachineOperand &MO) { 719 // For GD TLS access on AIX, we have two TOC entries for the symbol (one for 720 // the variable offset and the other for the region handle). They are 721 // differentiated by MO_TLSGD_FLAG and MO_TLSGDM_FLAG. 722 if (MO.getTargetFlags() & PPCII::MO_TLSGDM_FLAG) 723 return MCSymbolRefExpr::VariantKind::VK_PPC_AIX_TLSGDM; 724 if (MO.getTargetFlags() & PPCII::MO_TLSGD_FLAG) 725 return MCSymbolRefExpr::VariantKind::VK_PPC_AIX_TLSGD; 726 return MCSymbolRefExpr::VariantKind::VK_None; 727 }; 728 729 // Lower multi-instruction pseudo operations. 730 switch (MI->getOpcode()) { 731 default: break; 732 case TargetOpcode::DBG_VALUE: 733 llvm_unreachable("Should be handled target independently"); 734 case TargetOpcode::STACKMAP: 735 return LowerSTACKMAP(SM, *MI); 736 case TargetOpcode::PATCHPOINT: 737 return LowerPATCHPOINT(SM, *MI); 738 739 case PPC::MoveGOTtoLR: { 740 // Transform %lr = MoveGOTtoLR 741 // Into this: bl _GLOBAL_OFFSET_TABLE_@local-4 742 // _GLOBAL_OFFSET_TABLE_@local-4 (instruction preceding 743 // _GLOBAL_OFFSET_TABLE_) has exactly one instruction: 744 // blrl 745 // This will return the pointer to _GLOBAL_OFFSET_TABLE_@local 746 MCSymbol *GOTSymbol = 747 OutContext.getOrCreateSymbol(StringRef("_GLOBAL_OFFSET_TABLE_")); 748 const MCExpr *OffsExpr = 749 MCBinaryExpr::createSub(MCSymbolRefExpr::create(GOTSymbol, 750 MCSymbolRefExpr::VK_PPC_LOCAL, 751 OutContext), 752 MCConstantExpr::create(4, OutContext), 753 OutContext); 754 755 // Emit the 'bl'. 756 EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::BL).addExpr(OffsExpr)); 757 return; 758 } 759 case PPC::MovePCtoLR: 760 case PPC::MovePCtoLR8: { 761 // Transform %lr = MovePCtoLR 762 // Into this, where the label is the PIC base: 763 // bl L1$pb 764 // L1$pb: 765 MCSymbol *PICBase = MF->getPICBaseSymbol(); 766 767 // Emit the 'bl'. 768 EmitToStreamer(*OutStreamer, 769 MCInstBuilder(PPC::BL) 770 // FIXME: We would like an efficient form for this, so we 771 // don't have to do a lot of extra uniquing. 772 .addExpr(MCSymbolRefExpr::create(PICBase, OutContext))); 773 774 // Emit the label. 775 OutStreamer->emitLabel(PICBase); 776 return; 777 } 778 case PPC::UpdateGBR: { 779 // Transform %rd = UpdateGBR(%rt, %ri) 780 // Into: lwz %rt, .L0$poff - .L0$pb(%ri) 781 // add %rd, %rt, %ri 782 // or into (if secure plt mode is on): 783 // addis r30, r30, {.LTOC,_GLOBAL_OFFSET_TABLE} - .L0$pb@ha 784 // addi r30, r30, {.LTOC,_GLOBAL_OFFSET_TABLE} - .L0$pb@l 785 // Get the offset from the GOT Base Register to the GOT 786 LowerPPCMachineInstrToMCInst(MI, TmpInst, *this); 787 if (Subtarget->isSecurePlt() && isPositionIndependent() ) { 788 unsigned PICR = TmpInst.getOperand(0).getReg(); 789 MCSymbol *BaseSymbol = OutContext.getOrCreateSymbol( 790 M->getPICLevel() == PICLevel::SmallPIC ? "_GLOBAL_OFFSET_TABLE_" 791 : ".LTOC"); 792 const MCExpr *PB = 793 MCSymbolRefExpr::create(MF->getPICBaseSymbol(), OutContext); 794 795 const MCExpr *DeltaExpr = MCBinaryExpr::createSub( 796 MCSymbolRefExpr::create(BaseSymbol, OutContext), PB, OutContext); 797 798 const MCExpr *DeltaHi = PPCMCExpr::createHa(DeltaExpr, OutContext); 799 EmitToStreamer( 800 *OutStreamer, 801 MCInstBuilder(PPC::ADDIS).addReg(PICR).addReg(PICR).addExpr(DeltaHi)); 802 803 const MCExpr *DeltaLo = PPCMCExpr::createLo(DeltaExpr, OutContext); 804 EmitToStreamer( 805 *OutStreamer, 806 MCInstBuilder(PPC::ADDI).addReg(PICR).addReg(PICR).addExpr(DeltaLo)); 807 return; 808 } else { 809 MCSymbol *PICOffset = 810 MF->getInfo<PPCFunctionInfo>()->getPICOffsetSymbol(*MF); 811 TmpInst.setOpcode(PPC::LWZ); 812 const MCExpr *Exp = 813 MCSymbolRefExpr::create(PICOffset, MCSymbolRefExpr::VK_None, OutContext); 814 const MCExpr *PB = 815 MCSymbolRefExpr::create(MF->getPICBaseSymbol(), 816 MCSymbolRefExpr::VK_None, 817 OutContext); 818 const MCOperand TR = TmpInst.getOperand(1); 819 const MCOperand PICR = TmpInst.getOperand(0); 820 821 // Step 1: lwz %rt, .L$poff - .L$pb(%ri) 822 TmpInst.getOperand(1) = 823 MCOperand::createExpr(MCBinaryExpr::createSub(Exp, PB, OutContext)); 824 TmpInst.getOperand(0) = TR; 825 TmpInst.getOperand(2) = PICR; 826 EmitToStreamer(*OutStreamer, TmpInst); 827 828 TmpInst.setOpcode(PPC::ADD4); 829 TmpInst.getOperand(0) = PICR; 830 TmpInst.getOperand(1) = TR; 831 TmpInst.getOperand(2) = PICR; 832 EmitToStreamer(*OutStreamer, TmpInst); 833 return; 834 } 835 } 836 case PPC::LWZtoc: { 837 // Transform %rN = LWZtoc @op1, %r2 838 LowerPPCMachineInstrToMCInst(MI, TmpInst, *this); 839 840 // Change the opcode to LWZ. 841 TmpInst.setOpcode(PPC::LWZ); 842 843 const MachineOperand &MO = MI->getOperand(1); 844 assert((MO.isGlobal() || MO.isCPI() || MO.isJTI() || MO.isBlockAddress()) && 845 "Invalid operand for LWZtoc."); 846 847 // Map the operand to its corresponding MCSymbol. 848 const MCSymbol *const MOSymbol = getMCSymbolForTOCPseudoMO(MO, *this); 849 850 // Create a reference to the GOT entry for the symbol. The GOT entry will be 851 // synthesized later. 852 if (PL == PICLevel::SmallPIC && !IsAIX) { 853 const MCExpr *Exp = 854 MCSymbolRefExpr::create(MOSymbol, MCSymbolRefExpr::VK_GOT, 855 OutContext); 856 TmpInst.getOperand(1) = MCOperand::createExpr(Exp); 857 EmitToStreamer(*OutStreamer, TmpInst); 858 return; 859 } 860 861 MCSymbolRefExpr::VariantKind VK = GetVKForMO(MO); 862 863 // Otherwise, use the TOC. 'TOCEntry' is a label used to reference the 864 // storage allocated in the TOC which contains the address of 865 // 'MOSymbol'. Said TOC entry will be synthesized later. 866 MCSymbol *TOCEntry = lookUpOrCreateTOCEntry(MOSymbol, VK); 867 const MCExpr *Exp = 868 MCSymbolRefExpr::create(TOCEntry, MCSymbolRefExpr::VK_None, OutContext); 869 870 // AIX uses the label directly as the lwz displacement operand for 871 // references into the toc section. The displacement value will be generated 872 // relative to the toc-base. 873 if (IsAIX) { 874 assert( 875 TM.getCodeModel() == CodeModel::Small && 876 "This pseudo should only be selected for 32-bit small code model."); 877 Exp = getTOCEntryLoadingExprForXCOFF(MOSymbol, Exp, VK); 878 TmpInst.getOperand(1) = MCOperand::createExpr(Exp); 879 880 // Print MO for better readability 881 if (isVerbose()) 882 OutStreamer->GetCommentOS() << MO << '\n'; 883 EmitToStreamer(*OutStreamer, TmpInst); 884 return; 885 } 886 887 // Create an explicit subtract expression between the local symbol and 888 // '.LTOC' to manifest the toc-relative offset. 889 const MCExpr *PB = MCSymbolRefExpr::create( 890 OutContext.getOrCreateSymbol(Twine(".LTOC")), OutContext); 891 Exp = MCBinaryExpr::createSub(Exp, PB, OutContext); 892 TmpInst.getOperand(1) = MCOperand::createExpr(Exp); 893 EmitToStreamer(*OutStreamer, TmpInst); 894 return; 895 } 896 case PPC::ADDItoc: 897 case PPC::ADDItoc8: { 898 assert(IsAIX && TM.getCodeModel() == CodeModel::Small && 899 "PseudoOp only valid for small code model AIX"); 900 901 // Transform %rN = ADDItoc/8 @op1, %r2. 902 LowerPPCMachineInstrToMCInst(MI, TmpInst, *this); 903 904 // Change the opcode to load address. 905 TmpInst.setOpcode((!IsPPC64) ? (PPC::LA) : (PPC::LA8)); 906 907 const MachineOperand &MO = MI->getOperand(1); 908 assert(MO.isGlobal() && "Invalid operand for ADDItoc[8]."); 909 910 // Map the operand to its corresponding MCSymbol. 911 const MCSymbol *const MOSymbol = getMCSymbolForTOCPseudoMO(MO, *this); 912 913 const MCExpr *Exp = 914 MCSymbolRefExpr::create(MOSymbol, MCSymbolRefExpr::VK_None, OutContext); 915 916 TmpInst.getOperand(1) = TmpInst.getOperand(2); 917 TmpInst.getOperand(2) = MCOperand::createExpr(Exp); 918 EmitToStreamer(*OutStreamer, TmpInst); 919 return; 920 } 921 case PPC::LDtocJTI: 922 case PPC::LDtocCPT: 923 case PPC::LDtocBA: 924 case PPC::LDtoc: { 925 // Transform %x3 = LDtoc @min1, %x2 926 LowerPPCMachineInstrToMCInst(MI, TmpInst, *this); 927 928 // Change the opcode to LD. 929 TmpInst.setOpcode(PPC::LD); 930 931 const MachineOperand &MO = MI->getOperand(1); 932 assert((MO.isGlobal() || MO.isCPI() || MO.isJTI() || MO.isBlockAddress()) && 933 "Invalid operand!"); 934 935 // Map the operand to its corresponding MCSymbol. 936 const MCSymbol *const MOSymbol = getMCSymbolForTOCPseudoMO(MO, *this); 937 938 MCSymbolRefExpr::VariantKind VK = GetVKForMO(MO); 939 940 // Map the machine operand to its corresponding MCSymbol, then map the 941 // global address operand to be a reference to the TOC entry we will 942 // synthesize later. 943 MCSymbol *TOCEntry = lookUpOrCreateTOCEntry(MOSymbol, VK); 944 945 MCSymbolRefExpr::VariantKind VKExpr = 946 IsAIX ? MCSymbolRefExpr::VK_None : MCSymbolRefExpr::VK_PPC_TOC; 947 const MCExpr *Exp = MCSymbolRefExpr::create(TOCEntry, VKExpr, OutContext); 948 TmpInst.getOperand(1) = MCOperand::createExpr( 949 IsAIX ? getTOCEntryLoadingExprForXCOFF(MOSymbol, Exp, VK) : Exp); 950 951 // Print MO for better readability 952 if (isVerbose() && IsAIX) 953 OutStreamer->GetCommentOS() << MO << '\n'; 954 EmitToStreamer(*OutStreamer, TmpInst); 955 return; 956 } 957 case PPC::ADDIStocHA: { 958 assert((IsAIX && !IsPPC64 && TM.getCodeModel() == CodeModel::Large) && 959 "This pseudo should only be selected for 32-bit large code model on" 960 " AIX."); 961 962 // Transform %rd = ADDIStocHA %rA, @sym(%r2) 963 LowerPPCMachineInstrToMCInst(MI, TmpInst, *this); 964 965 // Change the opcode to ADDIS. 966 TmpInst.setOpcode(PPC::ADDIS); 967 968 const MachineOperand &MO = MI->getOperand(2); 969 assert((MO.isGlobal() || MO.isCPI() || MO.isJTI() || MO.isBlockAddress()) && 970 "Invalid operand for ADDIStocHA."); 971 972 // Map the machine operand to its corresponding MCSymbol. 973 MCSymbol *MOSymbol = getMCSymbolForTOCPseudoMO(MO, *this); 974 975 MCSymbolRefExpr::VariantKind VK = GetVKForMO(MO); 976 977 // Always use TOC on AIX. Map the global address operand to be a reference 978 // to the TOC entry we will synthesize later. 'TOCEntry' is a label used to 979 // reference the storage allocated in the TOC which contains the address of 980 // 'MOSymbol'. 981 MCSymbol *TOCEntry = lookUpOrCreateTOCEntry(MOSymbol, VK); 982 const MCExpr *Exp = MCSymbolRefExpr::create(TOCEntry, 983 MCSymbolRefExpr::VK_PPC_U, 984 OutContext); 985 TmpInst.getOperand(2) = MCOperand::createExpr(Exp); 986 EmitToStreamer(*OutStreamer, TmpInst); 987 return; 988 } 989 case PPC::LWZtocL: { 990 assert(IsAIX && !IsPPC64 && TM.getCodeModel() == CodeModel::Large && 991 "This pseudo should only be selected for 32-bit large code model on" 992 " AIX."); 993 994 // Transform %rd = LWZtocL @sym, %rs. 995 LowerPPCMachineInstrToMCInst(MI, TmpInst, *this); 996 997 // Change the opcode to lwz. 998 TmpInst.setOpcode(PPC::LWZ); 999 1000 const MachineOperand &MO = MI->getOperand(1); 1001 assert((MO.isGlobal() || MO.isCPI() || MO.isJTI() || MO.isBlockAddress()) && 1002 "Invalid operand for LWZtocL."); 1003 1004 // Map the machine operand to its corresponding MCSymbol. 1005 MCSymbol *MOSymbol = getMCSymbolForTOCPseudoMO(MO, *this); 1006 1007 MCSymbolRefExpr::VariantKind VK = GetVKForMO(MO); 1008 1009 // Always use TOC on AIX. Map the global address operand to be a reference 1010 // to the TOC entry we will synthesize later. 'TOCEntry' is a label used to 1011 // reference the storage allocated in the TOC which contains the address of 1012 // 'MOSymbol'. 1013 MCSymbol *TOCEntry = lookUpOrCreateTOCEntry(MOSymbol, VK); 1014 const MCExpr *Exp = MCSymbolRefExpr::create(TOCEntry, 1015 MCSymbolRefExpr::VK_PPC_L, 1016 OutContext); 1017 TmpInst.getOperand(1) = MCOperand::createExpr(Exp); 1018 EmitToStreamer(*OutStreamer, TmpInst); 1019 return; 1020 } 1021 case PPC::ADDIStocHA8: { 1022 // Transform %xd = ADDIStocHA8 %x2, @sym 1023 LowerPPCMachineInstrToMCInst(MI, TmpInst, *this); 1024 1025 // Change the opcode to ADDIS8. If the global address is the address of 1026 // an external symbol, is a jump table address, is a block address, or is a 1027 // constant pool index with large code model enabled, then generate a TOC 1028 // entry and reference that. Otherwise, reference the symbol directly. 1029 TmpInst.setOpcode(PPC::ADDIS8); 1030 1031 const MachineOperand &MO = MI->getOperand(2); 1032 assert((MO.isGlobal() || MO.isCPI() || MO.isJTI() || MO.isBlockAddress()) && 1033 "Invalid operand for ADDIStocHA8!"); 1034 1035 const MCSymbol *MOSymbol = getMCSymbolForTOCPseudoMO(MO, *this); 1036 1037 MCSymbolRefExpr::VariantKind VK = GetVKForMO(MO); 1038 1039 const bool GlobalToc = 1040 MO.isGlobal() && Subtarget->isGVIndirectSymbol(MO.getGlobal()); 1041 if (GlobalToc || MO.isJTI() || MO.isBlockAddress() || 1042 (MO.isCPI() && TM.getCodeModel() == CodeModel::Large)) 1043 MOSymbol = lookUpOrCreateTOCEntry(MOSymbol, VK); 1044 1045 VK = IsAIX ? MCSymbolRefExpr::VK_PPC_U : MCSymbolRefExpr::VK_PPC_TOC_HA; 1046 1047 const MCExpr *Exp = 1048 MCSymbolRefExpr::create(MOSymbol, VK, OutContext); 1049 1050 if (!MO.isJTI() && MO.getOffset()) 1051 Exp = MCBinaryExpr::createAdd(Exp, 1052 MCConstantExpr::create(MO.getOffset(), 1053 OutContext), 1054 OutContext); 1055 1056 TmpInst.getOperand(2) = MCOperand::createExpr(Exp); 1057 EmitToStreamer(*OutStreamer, TmpInst); 1058 return; 1059 } 1060 case PPC::LDtocL: { 1061 // Transform %xd = LDtocL @sym, %xs 1062 LowerPPCMachineInstrToMCInst(MI, TmpInst, *this); 1063 1064 // Change the opcode to LD. If the global address is the address of 1065 // an external symbol, is a jump table address, is a block address, or is 1066 // a constant pool index with large code model enabled, then generate a 1067 // TOC entry and reference that. Otherwise, reference the symbol directly. 1068 TmpInst.setOpcode(PPC::LD); 1069 1070 const MachineOperand &MO = MI->getOperand(1); 1071 assert((MO.isGlobal() || MO.isCPI() || MO.isJTI() || 1072 MO.isBlockAddress()) && 1073 "Invalid operand for LDtocL!"); 1074 1075 LLVM_DEBUG(assert( 1076 (!MO.isGlobal() || Subtarget->isGVIndirectSymbol(MO.getGlobal())) && 1077 "LDtocL used on symbol that could be accessed directly is " 1078 "invalid. Must match ADDIStocHA8.")); 1079 1080 const MCSymbol *MOSymbol = getMCSymbolForTOCPseudoMO(MO, *this); 1081 1082 MCSymbolRefExpr::VariantKind VK = GetVKForMO(MO); 1083 1084 if (!MO.isCPI() || TM.getCodeModel() == CodeModel::Large) 1085 MOSymbol = lookUpOrCreateTOCEntry(MOSymbol, VK); 1086 1087 VK = IsAIX ? MCSymbolRefExpr::VK_PPC_L : MCSymbolRefExpr::VK_PPC_TOC_LO; 1088 const MCExpr *Exp = 1089 MCSymbolRefExpr::create(MOSymbol, VK, OutContext); 1090 TmpInst.getOperand(1) = MCOperand::createExpr(Exp); 1091 EmitToStreamer(*OutStreamer, TmpInst); 1092 return; 1093 } 1094 case PPC::ADDItocL: { 1095 // Transform %xd = ADDItocL %xs, @sym 1096 LowerPPCMachineInstrToMCInst(MI, TmpInst, *this); 1097 1098 // Change the opcode to ADDI8. If the global address is external, then 1099 // generate a TOC entry and reference that. Otherwise, reference the 1100 // symbol directly. 1101 TmpInst.setOpcode(PPC::ADDI8); 1102 1103 const MachineOperand &MO = MI->getOperand(2); 1104 assert((MO.isGlobal() || MO.isCPI()) && "Invalid operand for ADDItocL."); 1105 1106 LLVM_DEBUG(assert( 1107 !(MO.isGlobal() && Subtarget->isGVIndirectSymbol(MO.getGlobal())) && 1108 "Interposable definitions must use indirect access.")); 1109 1110 const MCExpr *Exp = 1111 MCSymbolRefExpr::create(getMCSymbolForTOCPseudoMO(MO, *this), 1112 MCSymbolRefExpr::VK_PPC_TOC_LO, OutContext); 1113 TmpInst.getOperand(2) = MCOperand::createExpr(Exp); 1114 EmitToStreamer(*OutStreamer, TmpInst); 1115 return; 1116 } 1117 case PPC::ADDISgotTprelHA: { 1118 // Transform: %xd = ADDISgotTprelHA %x2, @sym 1119 // Into: %xd = ADDIS8 %x2, sym@got@tlsgd@ha 1120 assert(IsPPC64 && "Not supported for 32-bit PowerPC"); 1121 const MachineOperand &MO = MI->getOperand(2); 1122 const GlobalValue *GValue = MO.getGlobal(); 1123 MCSymbol *MOSymbol = getSymbol(GValue); 1124 const MCExpr *SymGotTprel = 1125 MCSymbolRefExpr::create(MOSymbol, MCSymbolRefExpr::VK_PPC_GOT_TPREL_HA, 1126 OutContext); 1127 EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::ADDIS8) 1128 .addReg(MI->getOperand(0).getReg()) 1129 .addReg(MI->getOperand(1).getReg()) 1130 .addExpr(SymGotTprel)); 1131 return; 1132 } 1133 case PPC::LDgotTprelL: 1134 case PPC::LDgotTprelL32: { 1135 // Transform %xd = LDgotTprelL @sym, %xs 1136 LowerPPCMachineInstrToMCInst(MI, TmpInst, *this); 1137 1138 // Change the opcode to LD. 1139 TmpInst.setOpcode(IsPPC64 ? PPC::LD : PPC::LWZ); 1140 const MachineOperand &MO = MI->getOperand(1); 1141 const GlobalValue *GValue = MO.getGlobal(); 1142 MCSymbol *MOSymbol = getSymbol(GValue); 1143 const MCExpr *Exp = MCSymbolRefExpr::create( 1144 MOSymbol, IsPPC64 ? MCSymbolRefExpr::VK_PPC_GOT_TPREL_LO 1145 : MCSymbolRefExpr::VK_PPC_GOT_TPREL, 1146 OutContext); 1147 TmpInst.getOperand(1) = MCOperand::createExpr(Exp); 1148 EmitToStreamer(*OutStreamer, TmpInst); 1149 return; 1150 } 1151 1152 case PPC::PPC32PICGOT: { 1153 MCSymbol *GOTSymbol = OutContext.getOrCreateSymbol(StringRef("_GLOBAL_OFFSET_TABLE_")); 1154 MCSymbol *GOTRef = OutContext.createTempSymbol(); 1155 MCSymbol *NextInstr = OutContext.createTempSymbol(); 1156 1157 EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::BL) 1158 // FIXME: We would like an efficient form for this, so we don't have to do 1159 // a lot of extra uniquing. 1160 .addExpr(MCSymbolRefExpr::create(NextInstr, OutContext))); 1161 const MCExpr *OffsExpr = 1162 MCBinaryExpr::createSub(MCSymbolRefExpr::create(GOTSymbol, OutContext), 1163 MCSymbolRefExpr::create(GOTRef, OutContext), 1164 OutContext); 1165 OutStreamer->emitLabel(GOTRef); 1166 OutStreamer->emitValue(OffsExpr, 4); 1167 OutStreamer->emitLabel(NextInstr); 1168 EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::MFLR) 1169 .addReg(MI->getOperand(0).getReg())); 1170 EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::LWZ) 1171 .addReg(MI->getOperand(1).getReg()) 1172 .addImm(0) 1173 .addReg(MI->getOperand(0).getReg())); 1174 EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::ADD4) 1175 .addReg(MI->getOperand(0).getReg()) 1176 .addReg(MI->getOperand(1).getReg()) 1177 .addReg(MI->getOperand(0).getReg())); 1178 return; 1179 } 1180 case PPC::PPC32GOT: { 1181 MCSymbol *GOTSymbol = 1182 OutContext.getOrCreateSymbol(StringRef("_GLOBAL_OFFSET_TABLE_")); 1183 const MCExpr *SymGotTlsL = MCSymbolRefExpr::create( 1184 GOTSymbol, MCSymbolRefExpr::VK_PPC_LO, OutContext); 1185 const MCExpr *SymGotTlsHA = MCSymbolRefExpr::create( 1186 GOTSymbol, MCSymbolRefExpr::VK_PPC_HA, OutContext); 1187 EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::LI) 1188 .addReg(MI->getOperand(0).getReg()) 1189 .addExpr(SymGotTlsL)); 1190 EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::ADDIS) 1191 .addReg(MI->getOperand(0).getReg()) 1192 .addReg(MI->getOperand(0).getReg()) 1193 .addExpr(SymGotTlsHA)); 1194 return; 1195 } 1196 case PPC::ADDIStlsgdHA: { 1197 // Transform: %xd = ADDIStlsgdHA %x2, @sym 1198 // Into: %xd = ADDIS8 %x2, sym@got@tlsgd@ha 1199 assert(IsPPC64 && "Not supported for 32-bit PowerPC"); 1200 const MachineOperand &MO = MI->getOperand(2); 1201 const GlobalValue *GValue = MO.getGlobal(); 1202 MCSymbol *MOSymbol = getSymbol(GValue); 1203 const MCExpr *SymGotTlsGD = 1204 MCSymbolRefExpr::create(MOSymbol, MCSymbolRefExpr::VK_PPC_GOT_TLSGD_HA, 1205 OutContext); 1206 EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::ADDIS8) 1207 .addReg(MI->getOperand(0).getReg()) 1208 .addReg(MI->getOperand(1).getReg()) 1209 .addExpr(SymGotTlsGD)); 1210 return; 1211 } 1212 case PPC::ADDItlsgdL: 1213 // Transform: %xd = ADDItlsgdL %xs, @sym 1214 // Into: %xd = ADDI8 %xs, sym@got@tlsgd@l 1215 case PPC::ADDItlsgdL32: { 1216 // Transform: %rd = ADDItlsgdL32 %rs, @sym 1217 // Into: %rd = ADDI %rs, sym@got@tlsgd 1218 const MachineOperand &MO = MI->getOperand(2); 1219 const GlobalValue *GValue = MO.getGlobal(); 1220 MCSymbol *MOSymbol = getSymbol(GValue); 1221 const MCExpr *SymGotTlsGD = MCSymbolRefExpr::create( 1222 MOSymbol, IsPPC64 ? MCSymbolRefExpr::VK_PPC_GOT_TLSGD_LO 1223 : MCSymbolRefExpr::VK_PPC_GOT_TLSGD, 1224 OutContext); 1225 EmitToStreamer(*OutStreamer, 1226 MCInstBuilder(IsPPC64 ? PPC::ADDI8 : PPC::ADDI) 1227 .addReg(MI->getOperand(0).getReg()) 1228 .addReg(MI->getOperand(1).getReg()) 1229 .addExpr(SymGotTlsGD)); 1230 return; 1231 } 1232 case PPC::GETtlsADDR: 1233 // Transform: %x3 = GETtlsADDR %x3, @sym 1234 // Into: BL8_NOP_TLS __tls_get_addr(sym at tlsgd) 1235 case PPC::GETtlsADDRPCREL: 1236 case PPC::GETtlsADDR32AIX: 1237 case PPC::GETtlsADDR64AIX: 1238 // Transform: %r3 = GETtlsADDRNNAIX %r3, %r4 (for NN == 32/64). 1239 // Into: BLA .__tls_get_addr() 1240 // Unlike on Linux, there is no symbol or relocation needed for this call. 1241 case PPC::GETtlsADDR32: { 1242 // Transform: %r3 = GETtlsADDR32 %r3, @sym 1243 // Into: BL_TLS __tls_get_addr(sym at tlsgd)@PLT 1244 EmitTlsCall(MI, MCSymbolRefExpr::VK_PPC_TLSGD); 1245 return; 1246 } 1247 case PPC::ADDIStlsldHA: { 1248 // Transform: %xd = ADDIStlsldHA %x2, @sym 1249 // Into: %xd = ADDIS8 %x2, sym@got@tlsld@ha 1250 assert(IsPPC64 && "Not supported for 32-bit PowerPC"); 1251 const MachineOperand &MO = MI->getOperand(2); 1252 const GlobalValue *GValue = MO.getGlobal(); 1253 MCSymbol *MOSymbol = getSymbol(GValue); 1254 const MCExpr *SymGotTlsLD = 1255 MCSymbolRefExpr::create(MOSymbol, MCSymbolRefExpr::VK_PPC_GOT_TLSLD_HA, 1256 OutContext); 1257 EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::ADDIS8) 1258 .addReg(MI->getOperand(0).getReg()) 1259 .addReg(MI->getOperand(1).getReg()) 1260 .addExpr(SymGotTlsLD)); 1261 return; 1262 } 1263 case PPC::ADDItlsldL: 1264 // Transform: %xd = ADDItlsldL %xs, @sym 1265 // Into: %xd = ADDI8 %xs, sym@got@tlsld@l 1266 case PPC::ADDItlsldL32: { 1267 // Transform: %rd = ADDItlsldL32 %rs, @sym 1268 // Into: %rd = ADDI %rs, sym@got@tlsld 1269 const MachineOperand &MO = MI->getOperand(2); 1270 const GlobalValue *GValue = MO.getGlobal(); 1271 MCSymbol *MOSymbol = getSymbol(GValue); 1272 const MCExpr *SymGotTlsLD = MCSymbolRefExpr::create( 1273 MOSymbol, IsPPC64 ? MCSymbolRefExpr::VK_PPC_GOT_TLSLD_LO 1274 : MCSymbolRefExpr::VK_PPC_GOT_TLSLD, 1275 OutContext); 1276 EmitToStreamer(*OutStreamer, 1277 MCInstBuilder(IsPPC64 ? PPC::ADDI8 : PPC::ADDI) 1278 .addReg(MI->getOperand(0).getReg()) 1279 .addReg(MI->getOperand(1).getReg()) 1280 .addExpr(SymGotTlsLD)); 1281 return; 1282 } 1283 case PPC::GETtlsldADDR: 1284 // Transform: %x3 = GETtlsldADDR %x3, @sym 1285 // Into: BL8_NOP_TLS __tls_get_addr(sym at tlsld) 1286 case PPC::GETtlsldADDRPCREL: 1287 case PPC::GETtlsldADDR32: { 1288 // Transform: %r3 = GETtlsldADDR32 %r3, @sym 1289 // Into: BL_TLS __tls_get_addr(sym at tlsld)@PLT 1290 EmitTlsCall(MI, MCSymbolRefExpr::VK_PPC_TLSLD); 1291 return; 1292 } 1293 case PPC::ADDISdtprelHA: 1294 // Transform: %xd = ADDISdtprelHA %xs, @sym 1295 // Into: %xd = ADDIS8 %xs, sym@dtprel@ha 1296 case PPC::ADDISdtprelHA32: { 1297 // Transform: %rd = ADDISdtprelHA32 %rs, @sym 1298 // Into: %rd = ADDIS %rs, sym@dtprel@ha 1299 const MachineOperand &MO = MI->getOperand(2); 1300 const GlobalValue *GValue = MO.getGlobal(); 1301 MCSymbol *MOSymbol = getSymbol(GValue); 1302 const MCExpr *SymDtprel = 1303 MCSymbolRefExpr::create(MOSymbol, MCSymbolRefExpr::VK_PPC_DTPREL_HA, 1304 OutContext); 1305 EmitToStreamer( 1306 *OutStreamer, 1307 MCInstBuilder(IsPPC64 ? PPC::ADDIS8 : PPC::ADDIS) 1308 .addReg(MI->getOperand(0).getReg()) 1309 .addReg(MI->getOperand(1).getReg()) 1310 .addExpr(SymDtprel)); 1311 return; 1312 } 1313 case PPC::PADDIdtprel: { 1314 // Transform: %rd = PADDIdtprel %rs, @sym 1315 // Into: %rd = PADDI8 %rs, sym@dtprel 1316 const MachineOperand &MO = MI->getOperand(2); 1317 const GlobalValue *GValue = MO.getGlobal(); 1318 MCSymbol *MOSymbol = getSymbol(GValue); 1319 const MCExpr *SymDtprel = MCSymbolRefExpr::create( 1320 MOSymbol, MCSymbolRefExpr::VK_DTPREL, OutContext); 1321 EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::PADDI8) 1322 .addReg(MI->getOperand(0).getReg()) 1323 .addReg(MI->getOperand(1).getReg()) 1324 .addExpr(SymDtprel)); 1325 return; 1326 } 1327 1328 case PPC::ADDIdtprelL: 1329 // Transform: %xd = ADDIdtprelL %xs, @sym 1330 // Into: %xd = ADDI8 %xs, sym@dtprel@l 1331 case PPC::ADDIdtprelL32: { 1332 // Transform: %rd = ADDIdtprelL32 %rs, @sym 1333 // Into: %rd = ADDI %rs, sym@dtprel@l 1334 const MachineOperand &MO = MI->getOperand(2); 1335 const GlobalValue *GValue = MO.getGlobal(); 1336 MCSymbol *MOSymbol = getSymbol(GValue); 1337 const MCExpr *SymDtprel = 1338 MCSymbolRefExpr::create(MOSymbol, MCSymbolRefExpr::VK_PPC_DTPREL_LO, 1339 OutContext); 1340 EmitToStreamer(*OutStreamer, 1341 MCInstBuilder(IsPPC64 ? PPC::ADDI8 : PPC::ADDI) 1342 .addReg(MI->getOperand(0).getReg()) 1343 .addReg(MI->getOperand(1).getReg()) 1344 .addExpr(SymDtprel)); 1345 return; 1346 } 1347 case PPC::MFOCRF: 1348 case PPC::MFOCRF8: 1349 if (!Subtarget->hasMFOCRF()) { 1350 // Transform: %r3 = MFOCRF %cr7 1351 // Into: %r3 = MFCR ;; cr7 1352 unsigned NewOpcode = 1353 MI->getOpcode() == PPC::MFOCRF ? PPC::MFCR : PPC::MFCR8; 1354 OutStreamer->AddComment(PPCInstPrinter:: 1355 getRegisterName(MI->getOperand(1).getReg())); 1356 EmitToStreamer(*OutStreamer, MCInstBuilder(NewOpcode) 1357 .addReg(MI->getOperand(0).getReg())); 1358 return; 1359 } 1360 break; 1361 case PPC::MTOCRF: 1362 case PPC::MTOCRF8: 1363 if (!Subtarget->hasMFOCRF()) { 1364 // Transform: %cr7 = MTOCRF %r3 1365 // Into: MTCRF mask, %r3 ;; cr7 1366 unsigned NewOpcode = 1367 MI->getOpcode() == PPC::MTOCRF ? PPC::MTCRF : PPC::MTCRF8; 1368 unsigned Mask = 0x80 >> OutContext.getRegisterInfo() 1369 ->getEncodingValue(MI->getOperand(0).getReg()); 1370 OutStreamer->AddComment(PPCInstPrinter:: 1371 getRegisterName(MI->getOperand(0).getReg())); 1372 EmitToStreamer(*OutStreamer, MCInstBuilder(NewOpcode) 1373 .addImm(Mask) 1374 .addReg(MI->getOperand(1).getReg())); 1375 return; 1376 } 1377 break; 1378 case PPC::LD: 1379 case PPC::STD: 1380 case PPC::LWA_32: 1381 case PPC::LWA: { 1382 // Verify alignment is legal, so we don't create relocations 1383 // that can't be supported. 1384 unsigned OpNum = (MI->getOpcode() == PPC::STD) ? 2 : 1; 1385 const MachineOperand &MO = MI->getOperand(OpNum); 1386 if (MO.isGlobal()) { 1387 const DataLayout &DL = MO.getGlobal()->getParent()->getDataLayout(); 1388 if (MO.getGlobal()->getPointerAlignment(DL) < 4) 1389 llvm_unreachable("Global must be word-aligned for LD, STD, LWA!"); 1390 } 1391 // Now process the instruction normally. 1392 break; 1393 } 1394 case PPC::PseudoEIEIO: { 1395 EmitToStreamer( 1396 *OutStreamer, 1397 MCInstBuilder(PPC::ORI).addReg(PPC::X2).addReg(PPC::X2).addImm(0)); 1398 EmitToStreamer( 1399 *OutStreamer, 1400 MCInstBuilder(PPC::ORI).addReg(PPC::X2).addReg(PPC::X2).addImm(0)); 1401 EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::EnforceIEIO)); 1402 return; 1403 } 1404 } 1405 1406 LowerPPCMachineInstrToMCInst(MI, TmpInst, *this); 1407 EmitToStreamer(*OutStreamer, TmpInst); 1408 } 1409 1410 void PPCLinuxAsmPrinter::emitGNUAttributes(Module &M) { 1411 // Emit float ABI into GNU attribute 1412 Metadata *MD = M.getModuleFlag("float-abi"); 1413 MDString *FloatABI = dyn_cast_or_null<MDString>(MD); 1414 if (!FloatABI) 1415 return; 1416 StringRef flt = FloatABI->getString(); 1417 // TODO: Support emitting soft-fp and hard double/single attributes. 1418 if (flt == "doubledouble") 1419 OutStreamer->emitGNUAttribute(Tag_GNU_Power_ABI_FP, 1420 Val_GNU_Power_ABI_HardFloat_DP | 1421 Val_GNU_Power_ABI_LDBL_IBM128); 1422 else if (flt == "ieeequad") 1423 OutStreamer->emitGNUAttribute(Tag_GNU_Power_ABI_FP, 1424 Val_GNU_Power_ABI_HardFloat_DP | 1425 Val_GNU_Power_ABI_LDBL_IEEE128); 1426 else if (flt == "ieeedouble") 1427 OutStreamer->emitGNUAttribute(Tag_GNU_Power_ABI_FP, 1428 Val_GNU_Power_ABI_HardFloat_DP | 1429 Val_GNU_Power_ABI_LDBL_64); 1430 } 1431 1432 void PPCLinuxAsmPrinter::emitInstruction(const MachineInstr *MI) { 1433 if (!Subtarget->isPPC64()) 1434 return PPCAsmPrinter::emitInstruction(MI); 1435 1436 switch (MI->getOpcode()) { 1437 default: 1438 return PPCAsmPrinter::emitInstruction(MI); 1439 case TargetOpcode::PATCHABLE_FUNCTION_ENTER: { 1440 // .begin: 1441 // b .end # lis 0, FuncId[16..32] 1442 // nop # li 0, FuncId[0..15] 1443 // std 0, -8(1) 1444 // mflr 0 1445 // bl __xray_FunctionEntry 1446 // mtlr 0 1447 // .end: 1448 // 1449 // Update compiler-rt/lib/xray/xray_powerpc64.cc accordingly when number 1450 // of instructions change. 1451 MCSymbol *BeginOfSled = OutContext.createTempSymbol(); 1452 MCSymbol *EndOfSled = OutContext.createTempSymbol(); 1453 OutStreamer->emitLabel(BeginOfSled); 1454 EmitToStreamer(*OutStreamer, 1455 MCInstBuilder(PPC::B).addExpr( 1456 MCSymbolRefExpr::create(EndOfSled, OutContext))); 1457 EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::NOP)); 1458 EmitToStreamer( 1459 *OutStreamer, 1460 MCInstBuilder(PPC::STD).addReg(PPC::X0).addImm(-8).addReg(PPC::X1)); 1461 EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::MFLR8).addReg(PPC::X0)); 1462 EmitToStreamer(*OutStreamer, 1463 MCInstBuilder(PPC::BL8_NOP) 1464 .addExpr(MCSymbolRefExpr::create( 1465 OutContext.getOrCreateSymbol("__xray_FunctionEntry"), 1466 OutContext))); 1467 EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::MTLR8).addReg(PPC::X0)); 1468 OutStreamer->emitLabel(EndOfSled); 1469 recordSled(BeginOfSled, *MI, SledKind::FUNCTION_ENTER, 2); 1470 break; 1471 } 1472 case TargetOpcode::PATCHABLE_RET: { 1473 unsigned RetOpcode = MI->getOperand(0).getImm(); 1474 MCInst RetInst; 1475 RetInst.setOpcode(RetOpcode); 1476 for (const auto &MO : llvm::drop_begin(MI->operands())) { 1477 MCOperand MCOp; 1478 if (LowerPPCMachineOperandToMCOperand(MO, MCOp, *this)) 1479 RetInst.addOperand(MCOp); 1480 } 1481 1482 bool IsConditional; 1483 if (RetOpcode == PPC::BCCLR) { 1484 IsConditional = true; 1485 } else if (RetOpcode == PPC::TCRETURNdi8 || RetOpcode == PPC::TCRETURNri8 || 1486 RetOpcode == PPC::TCRETURNai8) { 1487 break; 1488 } else if (RetOpcode == PPC::BLR8 || RetOpcode == PPC::TAILB8) { 1489 IsConditional = false; 1490 } else { 1491 EmitToStreamer(*OutStreamer, RetInst); 1492 break; 1493 } 1494 1495 MCSymbol *FallthroughLabel; 1496 if (IsConditional) { 1497 // Before: 1498 // bgtlr cr0 1499 // 1500 // After: 1501 // ble cr0, .end 1502 // .p2align 3 1503 // .begin: 1504 // blr # lis 0, FuncId[16..32] 1505 // nop # li 0, FuncId[0..15] 1506 // std 0, -8(1) 1507 // mflr 0 1508 // bl __xray_FunctionExit 1509 // mtlr 0 1510 // blr 1511 // .end: 1512 // 1513 // Update compiler-rt/lib/xray/xray_powerpc64.cc accordingly when number 1514 // of instructions change. 1515 FallthroughLabel = OutContext.createTempSymbol(); 1516 EmitToStreamer( 1517 *OutStreamer, 1518 MCInstBuilder(PPC::BCC) 1519 .addImm(PPC::InvertPredicate( 1520 static_cast<PPC::Predicate>(MI->getOperand(1).getImm()))) 1521 .addReg(MI->getOperand(2).getReg()) 1522 .addExpr(MCSymbolRefExpr::create(FallthroughLabel, OutContext))); 1523 RetInst = MCInst(); 1524 RetInst.setOpcode(PPC::BLR8); 1525 } 1526 // .p2align 3 1527 // .begin: 1528 // b(lr)? # lis 0, FuncId[16..32] 1529 // nop # li 0, FuncId[0..15] 1530 // std 0, -8(1) 1531 // mflr 0 1532 // bl __xray_FunctionExit 1533 // mtlr 0 1534 // b(lr)? 1535 // 1536 // Update compiler-rt/lib/xray/xray_powerpc64.cc accordingly when number 1537 // of instructions change. 1538 OutStreamer->emitCodeAlignment(8, &getSubtargetInfo()); 1539 MCSymbol *BeginOfSled = OutContext.createTempSymbol(); 1540 OutStreamer->emitLabel(BeginOfSled); 1541 EmitToStreamer(*OutStreamer, RetInst); 1542 EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::NOP)); 1543 EmitToStreamer( 1544 *OutStreamer, 1545 MCInstBuilder(PPC::STD).addReg(PPC::X0).addImm(-8).addReg(PPC::X1)); 1546 EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::MFLR8).addReg(PPC::X0)); 1547 EmitToStreamer(*OutStreamer, 1548 MCInstBuilder(PPC::BL8_NOP) 1549 .addExpr(MCSymbolRefExpr::create( 1550 OutContext.getOrCreateSymbol("__xray_FunctionExit"), 1551 OutContext))); 1552 EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::MTLR8).addReg(PPC::X0)); 1553 EmitToStreamer(*OutStreamer, RetInst); 1554 if (IsConditional) 1555 OutStreamer->emitLabel(FallthroughLabel); 1556 recordSled(BeginOfSled, *MI, SledKind::FUNCTION_EXIT, 2); 1557 break; 1558 } 1559 case TargetOpcode::PATCHABLE_FUNCTION_EXIT: 1560 llvm_unreachable("PATCHABLE_FUNCTION_EXIT should never be emitted"); 1561 case TargetOpcode::PATCHABLE_TAIL_CALL: 1562 // TODO: Define a trampoline `__xray_FunctionTailExit` and differentiate a 1563 // normal function exit from a tail exit. 1564 llvm_unreachable("Tail call is handled in the normal case. See comments " 1565 "around this assert."); 1566 } 1567 } 1568 1569 void PPCLinuxAsmPrinter::emitStartOfAsmFile(Module &M) { 1570 if (static_cast<const PPCTargetMachine &>(TM).isELFv2ABI()) { 1571 PPCTargetStreamer *TS = 1572 static_cast<PPCTargetStreamer *>(OutStreamer->getTargetStreamer()); 1573 1574 if (TS) 1575 TS->emitAbiVersion(2); 1576 } 1577 1578 if (static_cast<const PPCTargetMachine &>(TM).isPPC64() || 1579 !isPositionIndependent()) 1580 return AsmPrinter::emitStartOfAsmFile(M); 1581 1582 if (M.getPICLevel() == PICLevel::SmallPIC) 1583 return AsmPrinter::emitStartOfAsmFile(M); 1584 1585 OutStreamer->SwitchSection(OutContext.getELFSection( 1586 ".got2", ELF::SHT_PROGBITS, ELF::SHF_WRITE | ELF::SHF_ALLOC)); 1587 1588 MCSymbol *TOCSym = OutContext.getOrCreateSymbol(Twine(".LTOC")); 1589 MCSymbol *CurrentPos = OutContext.createTempSymbol(); 1590 1591 OutStreamer->emitLabel(CurrentPos); 1592 1593 // The GOT pointer points to the middle of the GOT, in order to reference the 1594 // entire 64kB range. 0x8000 is the midpoint. 1595 const MCExpr *tocExpr = 1596 MCBinaryExpr::createAdd(MCSymbolRefExpr::create(CurrentPos, OutContext), 1597 MCConstantExpr::create(0x8000, OutContext), 1598 OutContext); 1599 1600 OutStreamer->emitAssignment(TOCSym, tocExpr); 1601 1602 OutStreamer->SwitchSection(getObjFileLowering().getTextSection()); 1603 } 1604 1605 void PPCLinuxAsmPrinter::emitFunctionEntryLabel() { 1606 // linux/ppc32 - Normal entry label. 1607 if (!Subtarget->isPPC64() && 1608 (!isPositionIndependent() || 1609 MF->getFunction().getParent()->getPICLevel() == PICLevel::SmallPIC)) 1610 return AsmPrinter::emitFunctionEntryLabel(); 1611 1612 if (!Subtarget->isPPC64()) { 1613 const PPCFunctionInfo *PPCFI = MF->getInfo<PPCFunctionInfo>(); 1614 if (PPCFI->usesPICBase() && !Subtarget->isSecurePlt()) { 1615 MCSymbol *RelocSymbol = PPCFI->getPICOffsetSymbol(*MF); 1616 MCSymbol *PICBase = MF->getPICBaseSymbol(); 1617 OutStreamer->emitLabel(RelocSymbol); 1618 1619 const MCExpr *OffsExpr = 1620 MCBinaryExpr::createSub( 1621 MCSymbolRefExpr::create(OutContext.getOrCreateSymbol(Twine(".LTOC")), 1622 OutContext), 1623 MCSymbolRefExpr::create(PICBase, OutContext), 1624 OutContext); 1625 OutStreamer->emitValue(OffsExpr, 4); 1626 OutStreamer->emitLabel(CurrentFnSym); 1627 return; 1628 } else 1629 return AsmPrinter::emitFunctionEntryLabel(); 1630 } 1631 1632 // ELFv2 ABI - Normal entry label. 1633 if (Subtarget->isELFv2ABI()) { 1634 // In the Large code model, we allow arbitrary displacements between 1635 // the text section and its associated TOC section. We place the 1636 // full 8-byte offset to the TOC in memory immediately preceding 1637 // the function global entry point. 1638 if (TM.getCodeModel() == CodeModel::Large 1639 && !MF->getRegInfo().use_empty(PPC::X2)) { 1640 const PPCFunctionInfo *PPCFI = MF->getInfo<PPCFunctionInfo>(); 1641 1642 MCSymbol *TOCSymbol = OutContext.getOrCreateSymbol(StringRef(".TOC.")); 1643 MCSymbol *GlobalEPSymbol = PPCFI->getGlobalEPSymbol(*MF); 1644 const MCExpr *TOCDeltaExpr = 1645 MCBinaryExpr::createSub(MCSymbolRefExpr::create(TOCSymbol, OutContext), 1646 MCSymbolRefExpr::create(GlobalEPSymbol, 1647 OutContext), 1648 OutContext); 1649 1650 OutStreamer->emitLabel(PPCFI->getTOCOffsetSymbol(*MF)); 1651 OutStreamer->emitValue(TOCDeltaExpr, 8); 1652 } 1653 return AsmPrinter::emitFunctionEntryLabel(); 1654 } 1655 1656 // Emit an official procedure descriptor. 1657 MCSectionSubPair Current = OutStreamer->getCurrentSection(); 1658 MCSectionELF *Section = OutStreamer->getContext().getELFSection( 1659 ".opd", ELF::SHT_PROGBITS, ELF::SHF_WRITE | ELF::SHF_ALLOC); 1660 OutStreamer->SwitchSection(Section); 1661 OutStreamer->emitLabel(CurrentFnSym); 1662 OutStreamer->emitValueToAlignment(8); 1663 MCSymbol *Symbol1 = CurrentFnSymForSize; 1664 // Generates a R_PPC64_ADDR64 (from FK_DATA_8) relocation for the function 1665 // entry point. 1666 OutStreamer->emitValue(MCSymbolRefExpr::create(Symbol1, OutContext), 1667 8 /*size*/); 1668 MCSymbol *Symbol2 = OutContext.getOrCreateSymbol(StringRef(".TOC.")); 1669 // Generates a R_PPC64_TOC relocation for TOC base insertion. 1670 OutStreamer->emitValue( 1671 MCSymbolRefExpr::create(Symbol2, MCSymbolRefExpr::VK_PPC_TOCBASE, OutContext), 1672 8/*size*/); 1673 // Emit a null environment pointer. 1674 OutStreamer->emitIntValue(0, 8 /* size */); 1675 OutStreamer->SwitchSection(Current.first, Current.second); 1676 } 1677 1678 void PPCLinuxAsmPrinter::emitEndOfAsmFile(Module &M) { 1679 const DataLayout &DL = getDataLayout(); 1680 1681 bool isPPC64 = DL.getPointerSizeInBits() == 64; 1682 1683 PPCTargetStreamer *TS = 1684 static_cast<PPCTargetStreamer *>(OutStreamer->getTargetStreamer()); 1685 1686 emitGNUAttributes(M); 1687 1688 if (!TOC.empty()) { 1689 const char *Name = isPPC64 ? ".toc" : ".got2"; 1690 MCSectionELF *Section = OutContext.getELFSection( 1691 Name, ELF::SHT_PROGBITS, ELF::SHF_WRITE | ELF::SHF_ALLOC); 1692 OutStreamer->SwitchSection(Section); 1693 if (!isPPC64) 1694 OutStreamer->emitValueToAlignment(4); 1695 1696 for (const auto &TOCMapPair : TOC) { 1697 const MCSymbol *const TOCEntryTarget = TOCMapPair.first.first; 1698 MCSymbol *const TOCEntryLabel = TOCMapPair.second; 1699 1700 OutStreamer->emitLabel(TOCEntryLabel); 1701 if (isPPC64 && TS != nullptr) 1702 TS->emitTCEntry(*TOCEntryTarget, TOCMapPair.first.second); 1703 else 1704 OutStreamer->emitSymbolValue(TOCEntryTarget, 4); 1705 } 1706 } 1707 1708 PPCAsmPrinter::emitEndOfAsmFile(M); 1709 } 1710 1711 /// EmitFunctionBodyStart - Emit a global entry point prefix for ELFv2. 1712 void PPCLinuxAsmPrinter::emitFunctionBodyStart() { 1713 // In the ELFv2 ABI, in functions that use the TOC register, we need to 1714 // provide two entry points. The ABI guarantees that when calling the 1715 // local entry point, r2 is set up by the caller to contain the TOC base 1716 // for this function, and when calling the global entry point, r12 is set 1717 // up by the caller to hold the address of the global entry point. We 1718 // thus emit a prefix sequence along the following lines: 1719 // 1720 // func: 1721 // .Lfunc_gepNN: 1722 // # global entry point 1723 // addis r2,r12,(.TOC.-.Lfunc_gepNN)@ha 1724 // addi r2,r2,(.TOC.-.Lfunc_gepNN)@l 1725 // .Lfunc_lepNN: 1726 // .localentry func, .Lfunc_lepNN-.Lfunc_gepNN 1727 // # local entry point, followed by function body 1728 // 1729 // For the Large code model, we create 1730 // 1731 // .Lfunc_tocNN: 1732 // .quad .TOC.-.Lfunc_gepNN # done by EmitFunctionEntryLabel 1733 // func: 1734 // .Lfunc_gepNN: 1735 // # global entry point 1736 // ld r2,.Lfunc_tocNN-.Lfunc_gepNN(r12) 1737 // add r2,r2,r12 1738 // .Lfunc_lepNN: 1739 // .localentry func, .Lfunc_lepNN-.Lfunc_gepNN 1740 // # local entry point, followed by function body 1741 // 1742 // This ensures we have r2 set up correctly while executing the function 1743 // body, no matter which entry point is called. 1744 const PPCFunctionInfo *PPCFI = MF->getInfo<PPCFunctionInfo>(); 1745 const bool UsesX2OrR2 = !MF->getRegInfo().use_empty(PPC::X2) || 1746 !MF->getRegInfo().use_empty(PPC::R2); 1747 const bool PCrelGEPRequired = Subtarget->isUsingPCRelativeCalls() && 1748 UsesX2OrR2 && PPCFI->usesTOCBasePtr(); 1749 const bool NonPCrelGEPRequired = !Subtarget->isUsingPCRelativeCalls() && 1750 Subtarget->isELFv2ABI() && UsesX2OrR2; 1751 1752 // Only do all that if the function uses R2 as the TOC pointer 1753 // in the first place. We don't need the global entry point if the 1754 // function uses R2 as an allocatable register. 1755 if (NonPCrelGEPRequired || PCrelGEPRequired) { 1756 // Note: The logic here must be synchronized with the code in the 1757 // branch-selection pass which sets the offset of the first block in the 1758 // function. This matters because it affects the alignment. 1759 MCSymbol *GlobalEntryLabel = PPCFI->getGlobalEPSymbol(*MF); 1760 OutStreamer->emitLabel(GlobalEntryLabel); 1761 const MCSymbolRefExpr *GlobalEntryLabelExp = 1762 MCSymbolRefExpr::create(GlobalEntryLabel, OutContext); 1763 1764 if (TM.getCodeModel() != CodeModel::Large) { 1765 MCSymbol *TOCSymbol = OutContext.getOrCreateSymbol(StringRef(".TOC.")); 1766 const MCExpr *TOCDeltaExpr = 1767 MCBinaryExpr::createSub(MCSymbolRefExpr::create(TOCSymbol, OutContext), 1768 GlobalEntryLabelExp, OutContext); 1769 1770 const MCExpr *TOCDeltaHi = PPCMCExpr::createHa(TOCDeltaExpr, OutContext); 1771 EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::ADDIS) 1772 .addReg(PPC::X2) 1773 .addReg(PPC::X12) 1774 .addExpr(TOCDeltaHi)); 1775 1776 const MCExpr *TOCDeltaLo = PPCMCExpr::createLo(TOCDeltaExpr, OutContext); 1777 EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::ADDI) 1778 .addReg(PPC::X2) 1779 .addReg(PPC::X2) 1780 .addExpr(TOCDeltaLo)); 1781 } else { 1782 MCSymbol *TOCOffset = PPCFI->getTOCOffsetSymbol(*MF); 1783 const MCExpr *TOCOffsetDeltaExpr = 1784 MCBinaryExpr::createSub(MCSymbolRefExpr::create(TOCOffset, OutContext), 1785 GlobalEntryLabelExp, OutContext); 1786 1787 EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::LD) 1788 .addReg(PPC::X2) 1789 .addExpr(TOCOffsetDeltaExpr) 1790 .addReg(PPC::X12)); 1791 EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::ADD8) 1792 .addReg(PPC::X2) 1793 .addReg(PPC::X2) 1794 .addReg(PPC::X12)); 1795 } 1796 1797 MCSymbol *LocalEntryLabel = PPCFI->getLocalEPSymbol(*MF); 1798 OutStreamer->emitLabel(LocalEntryLabel); 1799 const MCSymbolRefExpr *LocalEntryLabelExp = 1800 MCSymbolRefExpr::create(LocalEntryLabel, OutContext); 1801 const MCExpr *LocalOffsetExp = 1802 MCBinaryExpr::createSub(LocalEntryLabelExp, 1803 GlobalEntryLabelExp, OutContext); 1804 1805 PPCTargetStreamer *TS = 1806 static_cast<PPCTargetStreamer *>(OutStreamer->getTargetStreamer()); 1807 1808 if (TS) 1809 TS->emitLocalEntry(cast<MCSymbolELF>(CurrentFnSym), LocalOffsetExp); 1810 } else if (Subtarget->isUsingPCRelativeCalls()) { 1811 // When generating the entry point for a function we have a few scenarios 1812 // based on whether or not that function uses R2 and whether or not that 1813 // function makes calls (or is a leaf function). 1814 // 1) A leaf function that does not use R2 (or treats it as callee-saved 1815 // and preserves it). In this case st_other=0 and both 1816 // the local and global entry points for the function are the same. 1817 // No special entry point code is required. 1818 // 2) A function uses the TOC pointer R2. This function may or may not have 1819 // calls. In this case st_other=[2,6] and the global and local entry 1820 // points are different. Code to correctly setup the TOC pointer in R2 1821 // is put between the global and local entry points. This case is 1822 // covered by the if statatement above. 1823 // 3) A function does not use the TOC pointer R2 but does have calls. 1824 // In this case st_other=1 since we do not know whether or not any 1825 // of the callees clobber R2. This case is dealt with in this else if 1826 // block. Tail calls are considered calls and the st_other should also 1827 // be set to 1 in that case as well. 1828 // 4) The function does not use the TOC pointer but R2 is used inside 1829 // the function. In this case st_other=1 once again. 1830 // 5) This function uses inline asm. We mark R2 as reserved if the function 1831 // has inline asm as we have to assume that it may be used. 1832 if (MF->getFrameInfo().hasCalls() || MF->getFrameInfo().hasTailCall() || 1833 MF->hasInlineAsm() || (!PPCFI->usesTOCBasePtr() && UsesX2OrR2)) { 1834 PPCTargetStreamer *TS = 1835 static_cast<PPCTargetStreamer *>(OutStreamer->getTargetStreamer()); 1836 if (TS) 1837 TS->emitLocalEntry(cast<MCSymbolELF>(CurrentFnSym), 1838 MCConstantExpr::create(1, OutContext)); 1839 } 1840 } 1841 } 1842 1843 /// EmitFunctionBodyEnd - Print the traceback table before the .size 1844 /// directive. 1845 /// 1846 void PPCLinuxAsmPrinter::emitFunctionBodyEnd() { 1847 // Only the 64-bit target requires a traceback table. For now, 1848 // we only emit the word of zeroes that GDB requires to find 1849 // the end of the function, and zeroes for the eight-byte 1850 // mandatory fields. 1851 // FIXME: We should fill in the eight-byte mandatory fields as described in 1852 // the PPC64 ELF ABI (this is a low-priority item because GDB does not 1853 // currently make use of these fields). 1854 if (Subtarget->isPPC64()) { 1855 OutStreamer->emitIntValue(0, 4/*size*/); 1856 OutStreamer->emitIntValue(0, 8/*size*/); 1857 } 1858 } 1859 1860 void PPCAIXAsmPrinter::emitLinkage(const GlobalValue *GV, 1861 MCSymbol *GVSym) const { 1862 1863 assert(MAI->hasVisibilityOnlyWithLinkage() && 1864 "AIX's linkage directives take a visibility setting."); 1865 1866 MCSymbolAttr LinkageAttr = MCSA_Invalid; 1867 switch (GV->getLinkage()) { 1868 case GlobalValue::ExternalLinkage: 1869 LinkageAttr = GV->isDeclaration() ? MCSA_Extern : MCSA_Global; 1870 break; 1871 case GlobalValue::LinkOnceAnyLinkage: 1872 case GlobalValue::LinkOnceODRLinkage: 1873 case GlobalValue::WeakAnyLinkage: 1874 case GlobalValue::WeakODRLinkage: 1875 case GlobalValue::ExternalWeakLinkage: 1876 LinkageAttr = MCSA_Weak; 1877 break; 1878 case GlobalValue::AvailableExternallyLinkage: 1879 LinkageAttr = MCSA_Extern; 1880 break; 1881 case GlobalValue::PrivateLinkage: 1882 return; 1883 case GlobalValue::InternalLinkage: 1884 assert(GV->getVisibility() == GlobalValue::DefaultVisibility && 1885 "InternalLinkage should not have other visibility setting."); 1886 LinkageAttr = MCSA_LGlobal; 1887 break; 1888 case GlobalValue::AppendingLinkage: 1889 llvm_unreachable("Should never emit this"); 1890 case GlobalValue::CommonLinkage: 1891 llvm_unreachable("CommonLinkage of XCOFF should not come to this path"); 1892 } 1893 1894 assert(LinkageAttr != MCSA_Invalid && "LinkageAttr should not MCSA_Invalid."); 1895 1896 MCSymbolAttr VisibilityAttr = MCSA_Invalid; 1897 if (!TM.getIgnoreXCOFFVisibility()) { 1898 switch (GV->getVisibility()) { 1899 1900 // TODO: "exported" and "internal" Visibility needs to go here. 1901 case GlobalValue::DefaultVisibility: 1902 break; 1903 case GlobalValue::HiddenVisibility: 1904 VisibilityAttr = MAI->getHiddenVisibilityAttr(); 1905 break; 1906 case GlobalValue::ProtectedVisibility: 1907 VisibilityAttr = MAI->getProtectedVisibilityAttr(); 1908 break; 1909 } 1910 } 1911 1912 OutStreamer->emitXCOFFSymbolLinkageWithVisibility(GVSym, LinkageAttr, 1913 VisibilityAttr); 1914 } 1915 1916 void PPCAIXAsmPrinter::SetupMachineFunction(MachineFunction &MF) { 1917 // Setup CurrentFnDescSym and its containing csect. 1918 MCSectionXCOFF *FnDescSec = 1919 cast<MCSectionXCOFF>(getObjFileLowering().getSectionForFunctionDescriptor( 1920 &MF.getFunction(), TM)); 1921 FnDescSec->setAlignment(Align(Subtarget->isPPC64() ? 8 : 4)); 1922 1923 CurrentFnDescSym = FnDescSec->getQualNameSymbol(); 1924 1925 return AsmPrinter::SetupMachineFunction(MF); 1926 } 1927 1928 uint16_t PPCAIXAsmPrinter::getNumberOfVRSaved() { 1929 // Calculate the number of VRs be saved. 1930 // Vector registers 20 through 31 are marked as reserved and cannot be used 1931 // in the default ABI. 1932 const PPCSubtarget &Subtarget = MF->getSubtarget<PPCSubtarget>(); 1933 if (Subtarget.isAIXABI() && Subtarget.hasAltivec() && 1934 TM.getAIXExtendedAltivecABI()) { 1935 const MachineRegisterInfo &MRI = MF->getRegInfo(); 1936 for (unsigned Reg = PPC::V20; Reg <= PPC::V31; ++Reg) 1937 if (MRI.isPhysRegModified(Reg)) 1938 // Number of VRs saved. 1939 return PPC::V31 - Reg + 1; 1940 } 1941 return 0; 1942 } 1943 1944 void PPCAIXAsmPrinter::emitFunctionBodyEnd() { 1945 1946 if (!TM.getXCOFFTracebackTable()) 1947 return; 1948 1949 emitTracebackTable(); 1950 1951 // If ShouldEmitEHBlock returns true, then the eh info table 1952 // will be emitted via `AIXException::endFunction`. Otherwise, we 1953 // need to emit a dumy eh info table when VRs are saved. We could not 1954 // consolidate these two places into one because there is no easy way 1955 // to access register information in `AIXException` class. 1956 if (!TargetLoweringObjectFileXCOFF::ShouldEmitEHBlock(MF) && 1957 (getNumberOfVRSaved() > 0)) { 1958 // Emit dummy EH Info Table. 1959 OutStreamer->SwitchSection(getObjFileLowering().getCompactUnwindSection()); 1960 MCSymbol *EHInfoLabel = 1961 TargetLoweringObjectFileXCOFF::getEHInfoTableSymbol(MF); 1962 OutStreamer->emitLabel(EHInfoLabel); 1963 1964 // Version number. 1965 OutStreamer->emitInt32(0); 1966 1967 const DataLayout &DL = MMI->getModule()->getDataLayout(); 1968 const unsigned PointerSize = DL.getPointerSize(); 1969 // Add necessary paddings in 64 bit mode. 1970 OutStreamer->emitValueToAlignment(PointerSize); 1971 1972 OutStreamer->emitIntValue(0, PointerSize); 1973 OutStreamer->emitIntValue(0, PointerSize); 1974 OutStreamer->SwitchSection(MF->getSection()); 1975 } 1976 } 1977 1978 void PPCAIXAsmPrinter::emitTracebackTable() { 1979 1980 // Create a symbol for the end of function. 1981 MCSymbol *FuncEnd = createTempSymbol(MF->getName()); 1982 OutStreamer->emitLabel(FuncEnd); 1983 1984 OutStreamer->AddComment("Traceback table begin"); 1985 // Begin with a fullword of zero. 1986 OutStreamer->emitIntValueInHexWithPadding(0, 4 /*size*/); 1987 1988 SmallString<128> CommentString; 1989 raw_svector_ostream CommentOS(CommentString); 1990 1991 auto EmitComment = [&]() { 1992 OutStreamer->AddComment(CommentOS.str()); 1993 CommentString.clear(); 1994 }; 1995 1996 auto EmitCommentAndValue = [&](uint64_t Value, int Size) { 1997 EmitComment(); 1998 OutStreamer->emitIntValueInHexWithPadding(Value, Size); 1999 }; 2000 2001 unsigned int Version = 0; 2002 CommentOS << "Version = " << Version; 2003 EmitCommentAndValue(Version, 1); 2004 2005 // There is a lack of information in the IR to assist with determining the 2006 // source language. AIX exception handling mechanism would only search for 2007 // personality routine and LSDA area when such language supports exception 2008 // handling. So to be conservatively correct and allow runtime to do its job, 2009 // we need to set it to C++ for now. 2010 TracebackTable::LanguageID LanguageIdentifier = 2011 TracebackTable::CPlusPlus; // C++ 2012 2013 CommentOS << "Language = " 2014 << getNameForTracebackTableLanguageId(LanguageIdentifier); 2015 EmitCommentAndValue(LanguageIdentifier, 1); 2016 2017 // This is only populated for the third and fourth bytes. 2018 uint32_t FirstHalfOfMandatoryField = 0; 2019 2020 // Emit the 3rd byte of the mandatory field. 2021 2022 // We always set traceback offset bit to true. 2023 FirstHalfOfMandatoryField |= TracebackTable::HasTraceBackTableOffsetMask; 2024 2025 const PPCFunctionInfo *FI = MF->getInfo<PPCFunctionInfo>(); 2026 const MachineRegisterInfo &MRI = MF->getRegInfo(); 2027 2028 // Check the function uses floating-point processor instructions or not 2029 for (unsigned Reg = PPC::F0; Reg <= PPC::F31; ++Reg) { 2030 if (MRI.isPhysRegUsed(Reg, /* SkipRegMaskTest */ true)) { 2031 FirstHalfOfMandatoryField |= TracebackTable::IsFloatingPointPresentMask; 2032 break; 2033 } 2034 } 2035 2036 #define GENBOOLCOMMENT(Prefix, V, Field) \ 2037 CommentOS << (Prefix) << ((V) & (TracebackTable::Field##Mask) ? "+" : "-") \ 2038 << #Field 2039 2040 #define GENVALUECOMMENT(PrefixAndName, V, Field) \ 2041 CommentOS << (PrefixAndName) << " = " \ 2042 << static_cast<unsigned>(((V) & (TracebackTable::Field##Mask)) >> \ 2043 (TracebackTable::Field##Shift)) 2044 2045 GENBOOLCOMMENT("", FirstHalfOfMandatoryField, IsGlobaLinkage); 2046 GENBOOLCOMMENT(", ", FirstHalfOfMandatoryField, IsOutOfLineEpilogOrPrologue); 2047 EmitComment(); 2048 2049 GENBOOLCOMMENT("", FirstHalfOfMandatoryField, HasTraceBackTableOffset); 2050 GENBOOLCOMMENT(", ", FirstHalfOfMandatoryField, IsInternalProcedure); 2051 EmitComment(); 2052 2053 GENBOOLCOMMENT("", FirstHalfOfMandatoryField, HasControlledStorage); 2054 GENBOOLCOMMENT(", ", FirstHalfOfMandatoryField, IsTOCless); 2055 EmitComment(); 2056 2057 GENBOOLCOMMENT("", FirstHalfOfMandatoryField, IsFloatingPointPresent); 2058 EmitComment(); 2059 GENBOOLCOMMENT("", FirstHalfOfMandatoryField, 2060 IsFloatingPointOperationLogOrAbortEnabled); 2061 EmitComment(); 2062 2063 OutStreamer->emitIntValueInHexWithPadding( 2064 (FirstHalfOfMandatoryField & 0x0000ff00) >> 8, 1); 2065 2066 // Set the 4th byte of the mandatory field. 2067 FirstHalfOfMandatoryField |= TracebackTable::IsFunctionNamePresentMask; 2068 2069 const PPCRegisterInfo *RegInfo = 2070 static_cast<const PPCRegisterInfo *>(Subtarget->getRegisterInfo()); 2071 Register FrameReg = RegInfo->getFrameRegister(*MF); 2072 if (FrameReg == (Subtarget->isPPC64() ? PPC::X31 : PPC::R31)) 2073 FirstHalfOfMandatoryField |= TracebackTable::IsAllocaUsedMask; 2074 2075 const SmallVectorImpl<Register> &MustSaveCRs = FI->getMustSaveCRs(); 2076 if (!MustSaveCRs.empty()) 2077 FirstHalfOfMandatoryField |= TracebackTable::IsCRSavedMask; 2078 2079 if (FI->mustSaveLR()) 2080 FirstHalfOfMandatoryField |= TracebackTable::IsLRSavedMask; 2081 2082 GENBOOLCOMMENT("", FirstHalfOfMandatoryField, IsInterruptHandler); 2083 GENBOOLCOMMENT(", ", FirstHalfOfMandatoryField, IsFunctionNamePresent); 2084 GENBOOLCOMMENT(", ", FirstHalfOfMandatoryField, IsAllocaUsed); 2085 EmitComment(); 2086 GENVALUECOMMENT("OnConditionDirective", FirstHalfOfMandatoryField, 2087 OnConditionDirective); 2088 GENBOOLCOMMENT(", ", FirstHalfOfMandatoryField, IsCRSaved); 2089 GENBOOLCOMMENT(", ", FirstHalfOfMandatoryField, IsLRSaved); 2090 EmitComment(); 2091 OutStreamer->emitIntValueInHexWithPadding((FirstHalfOfMandatoryField & 0xff), 2092 1); 2093 2094 // Set the 5th byte of mandatory field. 2095 uint32_t SecondHalfOfMandatoryField = 0; 2096 2097 // Always store back chain. 2098 SecondHalfOfMandatoryField |= TracebackTable::IsBackChainStoredMask; 2099 2100 uint32_t FPRSaved = 0; 2101 for (unsigned Reg = PPC::F14; Reg <= PPC::F31; ++Reg) { 2102 if (MRI.isPhysRegModified(Reg)) { 2103 FPRSaved = PPC::F31 - Reg + 1; 2104 break; 2105 } 2106 } 2107 SecondHalfOfMandatoryField |= (FPRSaved << TracebackTable::FPRSavedShift) & 2108 TracebackTable::FPRSavedMask; 2109 GENBOOLCOMMENT("", SecondHalfOfMandatoryField, IsBackChainStored); 2110 GENBOOLCOMMENT(", ", SecondHalfOfMandatoryField, IsFixup); 2111 GENVALUECOMMENT(", NumOfFPRsSaved", SecondHalfOfMandatoryField, FPRSaved); 2112 EmitComment(); 2113 OutStreamer->emitIntValueInHexWithPadding( 2114 (SecondHalfOfMandatoryField & 0xff000000) >> 24, 1); 2115 2116 // Set the 6th byte of mandatory field. 2117 2118 // Check whether has Vector Instruction,We only treat instructions uses vector 2119 // register as vector instructions. 2120 bool HasVectorInst = false; 2121 for (unsigned Reg = PPC::V0; Reg <= PPC::V31; ++Reg) 2122 if (MRI.isPhysRegUsed(Reg, /* SkipRegMaskTest */ true)) { 2123 // Has VMX instruction. 2124 HasVectorInst = true; 2125 break; 2126 } 2127 2128 if (FI->hasVectorParms() || HasVectorInst) 2129 SecondHalfOfMandatoryField |= TracebackTable::HasVectorInfoMask; 2130 2131 uint16_t NumOfVRSaved = getNumberOfVRSaved(); 2132 bool ShouldEmitEHBlock = 2133 TargetLoweringObjectFileXCOFF::ShouldEmitEHBlock(MF) || NumOfVRSaved > 0; 2134 2135 if (ShouldEmitEHBlock) 2136 SecondHalfOfMandatoryField |= TracebackTable::HasExtensionTableMask; 2137 2138 uint32_t GPRSaved = 0; 2139 2140 // X13 is reserved under 64-bit environment. 2141 unsigned GPRBegin = Subtarget->isPPC64() ? PPC::X14 : PPC::R13; 2142 unsigned GPREnd = Subtarget->isPPC64() ? PPC::X31 : PPC::R31; 2143 2144 for (unsigned Reg = GPRBegin; Reg <= GPREnd; ++Reg) { 2145 if (MRI.isPhysRegModified(Reg)) { 2146 GPRSaved = GPREnd - Reg + 1; 2147 break; 2148 } 2149 } 2150 2151 SecondHalfOfMandatoryField |= (GPRSaved << TracebackTable::GPRSavedShift) & 2152 TracebackTable::GPRSavedMask; 2153 2154 GENBOOLCOMMENT("", SecondHalfOfMandatoryField, HasExtensionTable); 2155 GENBOOLCOMMENT(", ", SecondHalfOfMandatoryField, HasVectorInfo); 2156 GENVALUECOMMENT(", NumOfGPRsSaved", SecondHalfOfMandatoryField, GPRSaved); 2157 EmitComment(); 2158 OutStreamer->emitIntValueInHexWithPadding( 2159 (SecondHalfOfMandatoryField & 0x00ff0000) >> 16, 1); 2160 2161 // Set the 7th byte of mandatory field. 2162 uint32_t NumberOfFixedParms = FI->getFixedParmsNum(); 2163 SecondHalfOfMandatoryField |= 2164 (NumberOfFixedParms << TracebackTable::NumberOfFixedParmsShift) & 2165 TracebackTable::NumberOfFixedParmsMask; 2166 GENVALUECOMMENT("NumberOfFixedParms", SecondHalfOfMandatoryField, 2167 NumberOfFixedParms); 2168 EmitComment(); 2169 OutStreamer->emitIntValueInHexWithPadding( 2170 (SecondHalfOfMandatoryField & 0x0000ff00) >> 8, 1); 2171 2172 // Set the 8th byte of mandatory field. 2173 2174 // Always set parameter on stack. 2175 SecondHalfOfMandatoryField |= TracebackTable::HasParmsOnStackMask; 2176 2177 uint32_t NumberOfFPParms = FI->getFloatingPointParmsNum(); 2178 SecondHalfOfMandatoryField |= 2179 (NumberOfFPParms << TracebackTable::NumberOfFloatingPointParmsShift) & 2180 TracebackTable::NumberOfFloatingPointParmsMask; 2181 2182 GENVALUECOMMENT("NumberOfFPParms", SecondHalfOfMandatoryField, 2183 NumberOfFloatingPointParms); 2184 GENBOOLCOMMENT(", ", SecondHalfOfMandatoryField, HasParmsOnStack); 2185 EmitComment(); 2186 OutStreamer->emitIntValueInHexWithPadding(SecondHalfOfMandatoryField & 0xff, 2187 1); 2188 2189 // Generate the optional fields of traceback table. 2190 2191 // Parameter type. 2192 if (NumberOfFixedParms || NumberOfFPParms) { 2193 uint32_t ParmsTypeValue = FI->getParmsType(); 2194 2195 Expected<SmallString<32>> ParmsType = 2196 FI->hasVectorParms() 2197 ? XCOFF::parseParmsTypeWithVecInfo( 2198 ParmsTypeValue, NumberOfFixedParms, NumberOfFPParms, 2199 FI->getVectorParmsNum()) 2200 : XCOFF::parseParmsType(ParmsTypeValue, NumberOfFixedParms, 2201 NumberOfFPParms); 2202 2203 assert(ParmsType && toString(ParmsType.takeError()).c_str()); 2204 if (ParmsType) { 2205 CommentOS << "Parameter type = " << ParmsType.get(); 2206 EmitComment(); 2207 } 2208 OutStreamer->emitIntValueInHexWithPadding(ParmsTypeValue, 2209 sizeof(ParmsTypeValue)); 2210 } 2211 // Traceback table offset. 2212 OutStreamer->AddComment("Function size"); 2213 if (FirstHalfOfMandatoryField & TracebackTable::HasTraceBackTableOffsetMask) { 2214 MCSymbol *FuncSectSym = getObjFileLowering().getFunctionEntryPointSymbol( 2215 &(MF->getFunction()), TM); 2216 OutStreamer->emitAbsoluteSymbolDiff(FuncEnd, FuncSectSym, 4); 2217 } 2218 2219 // Since we unset the Int_Handler. 2220 if (FirstHalfOfMandatoryField & TracebackTable::IsInterruptHandlerMask) 2221 report_fatal_error("Hand_Mask not implement yet"); 2222 2223 if (FirstHalfOfMandatoryField & TracebackTable::HasControlledStorageMask) 2224 report_fatal_error("Ctl_Info not implement yet"); 2225 2226 if (FirstHalfOfMandatoryField & TracebackTable::IsFunctionNamePresentMask) { 2227 StringRef Name = MF->getName().substr(0, INT16_MAX); 2228 int16_t NameLength = Name.size(); 2229 CommentOS << "Function name len = " 2230 << static_cast<unsigned int>(NameLength); 2231 EmitCommentAndValue(NameLength, 2); 2232 OutStreamer->AddComment("Function Name"); 2233 OutStreamer->emitBytes(Name); 2234 } 2235 2236 if (FirstHalfOfMandatoryField & TracebackTable::IsAllocaUsedMask) { 2237 uint8_t AllocReg = XCOFF::AllocRegNo; 2238 OutStreamer->AddComment("AllocaUsed"); 2239 OutStreamer->emitIntValueInHex(AllocReg, sizeof(AllocReg)); 2240 } 2241 2242 if (SecondHalfOfMandatoryField & TracebackTable::HasVectorInfoMask) { 2243 uint16_t VRData = 0; 2244 if (NumOfVRSaved) { 2245 // Number of VRs saved. 2246 VRData |= (NumOfVRSaved << TracebackTable::NumberOfVRSavedShift) & 2247 TracebackTable::NumberOfVRSavedMask; 2248 // This bit is supposed to set only when the special register 2249 // VRSAVE is saved on stack. 2250 // However, IBM XL compiler sets the bit when any vector registers 2251 // are saved on the stack. We will follow XL's behavior on AIX 2252 // so that we don't get surprise behavior change for C code. 2253 VRData |= TracebackTable::IsVRSavedOnStackMask; 2254 } 2255 2256 // Set has_varargs. 2257 if (FI->getVarArgsFrameIndex()) 2258 VRData |= TracebackTable::HasVarArgsMask; 2259 2260 // Vector parameters number. 2261 unsigned VectorParmsNum = FI->getVectorParmsNum(); 2262 VRData |= (VectorParmsNum << TracebackTable::NumberOfVectorParmsShift) & 2263 TracebackTable::NumberOfVectorParmsMask; 2264 2265 if (HasVectorInst) 2266 VRData |= TracebackTable::HasVMXInstructionMask; 2267 2268 GENVALUECOMMENT("NumOfVRsSaved", VRData, NumberOfVRSaved); 2269 GENBOOLCOMMENT(", ", VRData, IsVRSavedOnStack); 2270 GENBOOLCOMMENT(", ", VRData, HasVarArgs); 2271 EmitComment(); 2272 OutStreamer->emitIntValueInHexWithPadding((VRData & 0xff00) >> 8, 1); 2273 2274 GENVALUECOMMENT("NumOfVectorParams", VRData, NumberOfVectorParms); 2275 GENBOOLCOMMENT(", ", VRData, HasVMXInstruction); 2276 EmitComment(); 2277 OutStreamer->emitIntValueInHexWithPadding(VRData & 0x00ff, 1); 2278 2279 uint32_t VecParmTypeValue = FI->getVecExtParmsType(); 2280 2281 Expected<SmallString<32>> VecParmsType = 2282 XCOFF::parseVectorParmsType(VecParmTypeValue, VectorParmsNum); 2283 assert(VecParmsType && toString(VecParmsType.takeError()).c_str()); 2284 if (VecParmsType) { 2285 CommentOS << "Vector Parameter type = " << VecParmsType.get(); 2286 EmitComment(); 2287 } 2288 OutStreamer->emitIntValueInHexWithPadding(VecParmTypeValue, 2289 sizeof(VecParmTypeValue)); 2290 // Padding 2 bytes. 2291 CommentOS << "Padding"; 2292 EmitCommentAndValue(0, 2); 2293 } 2294 2295 uint8_t ExtensionTableFlag = 0; 2296 if (SecondHalfOfMandatoryField & TracebackTable::HasExtensionTableMask) { 2297 if (ShouldEmitEHBlock) 2298 ExtensionTableFlag |= ExtendedTBTableFlag::TB_EH_INFO; 2299 if (EnableSSPCanaryBitInTB && 2300 TargetLoweringObjectFileXCOFF::ShouldSetSSPCanaryBitInTB(MF)) 2301 ExtensionTableFlag |= ExtendedTBTableFlag::TB_SSP_CANARY; 2302 2303 CommentOS << "ExtensionTableFlag = " 2304 << getExtendedTBTableFlagString(ExtensionTableFlag); 2305 EmitCommentAndValue(ExtensionTableFlag, sizeof(ExtensionTableFlag)); 2306 } 2307 2308 if (ExtensionTableFlag & ExtendedTBTableFlag::TB_EH_INFO) { 2309 auto &Ctx = OutStreamer->getContext(); 2310 MCSymbol *EHInfoSym = 2311 TargetLoweringObjectFileXCOFF::getEHInfoTableSymbol(MF); 2312 MCSymbol *TOCEntry = lookUpOrCreateTOCEntry(EHInfoSym); 2313 const MCSymbol *TOCBaseSym = 2314 cast<MCSectionXCOFF>(getObjFileLowering().getTOCBaseSection()) 2315 ->getQualNameSymbol(); 2316 const MCExpr *Exp = 2317 MCBinaryExpr::createSub(MCSymbolRefExpr::create(TOCEntry, Ctx), 2318 MCSymbolRefExpr::create(TOCBaseSym, Ctx), Ctx); 2319 2320 const DataLayout &DL = getDataLayout(); 2321 OutStreamer->emitValueToAlignment(4); 2322 OutStreamer->AddComment("EHInfo Table"); 2323 OutStreamer->emitValue(Exp, DL.getPointerSize()); 2324 } 2325 #undef GENBOOLCOMMENT 2326 #undef GENVALUECOMMENT 2327 } 2328 2329 static bool isSpecialLLVMGlobalArrayToSkip(const GlobalVariable *GV) { 2330 return GV->hasAppendingLinkage() && 2331 StringSwitch<bool>(GV->getName()) 2332 // TODO: Linker could still eliminate the GV if we just skip 2333 // handling llvm.used array. Skipping them for now until we or the 2334 // AIX OS team come up with a good solution. 2335 .Case("llvm.used", true) 2336 // It's correct to just skip llvm.compiler.used array here. 2337 .Case("llvm.compiler.used", true) 2338 .Default(false); 2339 } 2340 2341 static bool isSpecialLLVMGlobalArrayForStaticInit(const GlobalVariable *GV) { 2342 return StringSwitch<bool>(GV->getName()) 2343 .Cases("llvm.global_ctors", "llvm.global_dtors", true) 2344 .Default(false); 2345 } 2346 2347 void PPCAIXAsmPrinter::emitGlobalVariable(const GlobalVariable *GV) { 2348 // Special LLVM global arrays have been handled at the initialization. 2349 if (isSpecialLLVMGlobalArrayToSkip(GV) || isSpecialLLVMGlobalArrayForStaticInit(GV)) 2350 return; 2351 2352 // If the Global Variable has the toc-data attribute, it needs to be emitted 2353 // when we emit the .toc section. 2354 if (GV->hasAttribute("toc-data")) { 2355 TOCDataGlobalVars.push_back(GV); 2356 return; 2357 } 2358 2359 emitGlobalVariableHelper(GV); 2360 } 2361 2362 void PPCAIXAsmPrinter::emitGlobalVariableHelper(const GlobalVariable *GV) { 2363 assert(!GV->getName().startswith("llvm.") && 2364 "Unhandled intrinsic global variable."); 2365 2366 if (GV->hasComdat()) 2367 report_fatal_error("COMDAT not yet supported by AIX."); 2368 2369 MCSymbolXCOFF *GVSym = cast<MCSymbolXCOFF>(getSymbol(GV)); 2370 2371 if (GV->isDeclarationForLinker()) { 2372 emitLinkage(GV, GVSym); 2373 return; 2374 } 2375 2376 SectionKind GVKind = getObjFileLowering().getKindForGlobal(GV, TM); 2377 if (!GVKind.isGlobalWriteableData() && !GVKind.isReadOnly() && 2378 !GVKind.isThreadLocal()) // Checks for both ThreadData and ThreadBSS. 2379 report_fatal_error("Encountered a global variable kind that is " 2380 "not supported yet."); 2381 2382 // Print GV in verbose mode 2383 if (isVerbose()) { 2384 if (GV->hasInitializer()) { 2385 GV->printAsOperand(OutStreamer->GetCommentOS(), 2386 /*PrintType=*/false, GV->getParent()); 2387 OutStreamer->GetCommentOS() << '\n'; 2388 } 2389 } 2390 2391 MCSectionXCOFF *Csect = cast<MCSectionXCOFF>( 2392 getObjFileLowering().SectionForGlobal(GV, GVKind, TM)); 2393 2394 // Switch to the containing csect. 2395 OutStreamer->SwitchSection(Csect); 2396 2397 const DataLayout &DL = GV->getParent()->getDataLayout(); 2398 2399 // Handle common and zero-initialized local symbols. 2400 if (GV->hasCommonLinkage() || GVKind.isBSSLocal() || 2401 GVKind.isThreadBSSLocal()) { 2402 Align Alignment = GV->getAlign().getValueOr(DL.getPreferredAlign(GV)); 2403 uint64_t Size = DL.getTypeAllocSize(GV->getValueType()); 2404 GVSym->setStorageClass( 2405 TargetLoweringObjectFileXCOFF::getStorageClassForGlobal(GV)); 2406 2407 if (GVKind.isBSSLocal() || GVKind.isThreadBSSLocal()) 2408 OutStreamer->emitXCOFFLocalCommonSymbol( 2409 OutContext.getOrCreateSymbol(GVSym->getSymbolTableName()), Size, 2410 GVSym, Alignment.value()); 2411 else 2412 OutStreamer->emitCommonSymbol(GVSym, Size, Alignment.value()); 2413 return; 2414 } 2415 2416 MCSymbol *EmittedInitSym = GVSym; 2417 emitLinkage(GV, EmittedInitSym); 2418 emitAlignment(getGVAlignment(GV, DL), GV); 2419 2420 // When -fdata-sections is enabled, every GlobalVariable will 2421 // be put into its own csect; therefore, label is not necessary here. 2422 if (!TM.getDataSections() || GV->hasSection()) { 2423 OutStreamer->emitLabel(EmittedInitSym); 2424 } 2425 2426 // Emit aliasing label for global variable. 2427 llvm::for_each(GOAliasMap[GV], [this](const GlobalAlias *Alias) { 2428 OutStreamer->emitLabel(getSymbol(Alias)); 2429 }); 2430 2431 emitGlobalConstant(GV->getParent()->getDataLayout(), GV->getInitializer()); 2432 } 2433 2434 void PPCAIXAsmPrinter::emitFunctionDescriptor() { 2435 const DataLayout &DL = getDataLayout(); 2436 const unsigned PointerSize = DL.getPointerSizeInBits() == 64 ? 8 : 4; 2437 2438 MCSectionSubPair Current = OutStreamer->getCurrentSection(); 2439 // Emit function descriptor. 2440 OutStreamer->SwitchSection( 2441 cast<MCSymbolXCOFF>(CurrentFnDescSym)->getRepresentedCsect()); 2442 2443 // Emit aliasing label for function descriptor csect. 2444 llvm::for_each(GOAliasMap[&MF->getFunction()], 2445 [this](const GlobalAlias *Alias) { 2446 OutStreamer->emitLabel(getSymbol(Alias)); 2447 }); 2448 2449 // Emit function entry point address. 2450 OutStreamer->emitValue(MCSymbolRefExpr::create(CurrentFnSym, OutContext), 2451 PointerSize); 2452 // Emit TOC base address. 2453 const MCSymbol *TOCBaseSym = 2454 cast<MCSectionXCOFF>(getObjFileLowering().getTOCBaseSection()) 2455 ->getQualNameSymbol(); 2456 OutStreamer->emitValue(MCSymbolRefExpr::create(TOCBaseSym, OutContext), 2457 PointerSize); 2458 // Emit a null environment pointer. 2459 OutStreamer->emitIntValue(0, PointerSize); 2460 2461 OutStreamer->SwitchSection(Current.first, Current.second); 2462 } 2463 2464 void PPCAIXAsmPrinter::emitFunctionEntryLabel() { 2465 // It's not necessary to emit the label when we have individual 2466 // function in its own csect. 2467 if (!TM.getFunctionSections()) 2468 PPCAsmPrinter::emitFunctionEntryLabel(); 2469 2470 // Emit aliasing label for function entry point label. 2471 llvm::for_each( 2472 GOAliasMap[&MF->getFunction()], [this](const GlobalAlias *Alias) { 2473 OutStreamer->emitLabel( 2474 getObjFileLowering().getFunctionEntryPointSymbol(Alias, TM)); 2475 }); 2476 } 2477 2478 void PPCAIXAsmPrinter::emitEndOfAsmFile(Module &M) { 2479 // If there are no functions and there are no toc-data definitions in this 2480 // module, we will never need to reference the TOC base. 2481 if (M.empty() && TOCDataGlobalVars.empty()) 2482 return; 2483 2484 // Switch to section to emit TOC base. 2485 OutStreamer->SwitchSection(getObjFileLowering().getTOCBaseSection()); 2486 2487 PPCTargetStreamer *TS = 2488 static_cast<PPCTargetStreamer *>(OutStreamer->getTargetStreamer()); 2489 2490 for (auto &I : TOC) { 2491 MCSectionXCOFF *TCEntry; 2492 // Setup the csect for the current TC entry. If the variant kind is 2493 // VK_PPC_AIX_TLSGDM the entry represents the region handle, we create a 2494 // new symbol to prefix the name with a dot. 2495 if (I.first.second == MCSymbolRefExpr::VariantKind::VK_PPC_AIX_TLSGDM) { 2496 SmallString<128> Name; 2497 StringRef Prefix = "."; 2498 Name += Prefix; 2499 Name += I.first.first->getName(); 2500 MCSymbol *S = OutContext.getOrCreateSymbol(Name); 2501 TCEntry = cast<MCSectionXCOFF>( 2502 getObjFileLowering().getSectionForTOCEntry(S, TM)); 2503 } else { 2504 TCEntry = cast<MCSectionXCOFF>( 2505 getObjFileLowering().getSectionForTOCEntry(I.first.first, TM)); 2506 } 2507 OutStreamer->SwitchSection(TCEntry); 2508 2509 OutStreamer->emitLabel(I.second); 2510 if (TS != nullptr) 2511 TS->emitTCEntry(*I.first.first, I.first.second); 2512 } 2513 2514 for (const auto *GV : TOCDataGlobalVars) 2515 emitGlobalVariableHelper(GV); 2516 } 2517 2518 bool PPCAIXAsmPrinter::doInitialization(Module &M) { 2519 const bool Result = PPCAsmPrinter::doInitialization(M); 2520 2521 auto setCsectAlignment = [this](const GlobalObject *GO) { 2522 // Declarations have 0 alignment which is set by default. 2523 if (GO->isDeclarationForLinker()) 2524 return; 2525 2526 SectionKind GOKind = getObjFileLowering().getKindForGlobal(GO, TM); 2527 MCSectionXCOFF *Csect = cast<MCSectionXCOFF>( 2528 getObjFileLowering().SectionForGlobal(GO, GOKind, TM)); 2529 2530 Align GOAlign = getGVAlignment(GO, GO->getParent()->getDataLayout()); 2531 if (GOAlign > Csect->getAlignment()) 2532 Csect->setAlignment(GOAlign); 2533 }; 2534 2535 // We need to know, up front, the alignment of csects for the assembly path, 2536 // because once a .csect directive gets emitted, we could not change the 2537 // alignment value on it. 2538 for (const auto &G : M.globals()) { 2539 if (isSpecialLLVMGlobalArrayToSkip(&G)) 2540 continue; 2541 2542 if (isSpecialLLVMGlobalArrayForStaticInit(&G)) { 2543 // Generate a format indicator and a unique module id to be a part of 2544 // the sinit and sterm function names. 2545 if (FormatIndicatorAndUniqueModId.empty()) { 2546 std::string UniqueModuleId = getUniqueModuleId(&M); 2547 if (UniqueModuleId != "") 2548 // TODO: Use source file full path to generate the unique module id 2549 // and add a format indicator as a part of function name in case we 2550 // will support more than one format. 2551 FormatIndicatorAndUniqueModId = "clang_" + UniqueModuleId.substr(1); 2552 else 2553 // Use the Pid and current time as the unique module id when we cannot 2554 // generate one based on a module's strong external symbols. 2555 // FIXME: Adjust the comment accordingly after we use source file full 2556 // path instead. 2557 FormatIndicatorAndUniqueModId = 2558 "clangPidTime_" + llvm::itostr(sys::Process::getProcessId()) + 2559 "_" + llvm::itostr(time(nullptr)); 2560 } 2561 2562 emitSpecialLLVMGlobal(&G); 2563 continue; 2564 } 2565 2566 setCsectAlignment(&G); 2567 } 2568 2569 for (const auto &F : M) 2570 setCsectAlignment(&F); 2571 2572 // Construct an aliasing list for each GlobalObject. 2573 for (const auto &Alias : M.aliases()) { 2574 const GlobalObject *Base = Alias.getAliaseeObject(); 2575 if (!Base) 2576 report_fatal_error( 2577 "alias without a base object is not yet supported on AIX"); 2578 GOAliasMap[Base].push_back(&Alias); 2579 } 2580 2581 return Result; 2582 } 2583 2584 void PPCAIXAsmPrinter::emitInstruction(const MachineInstr *MI) { 2585 switch (MI->getOpcode()) { 2586 default: 2587 break; 2588 case PPC::GETtlsADDR64AIX: 2589 case PPC::GETtlsADDR32AIX: { 2590 // The reference to .__tls_get_addr is unknown to the assembler 2591 // so we need to emit an external symbol reference. 2592 MCSymbol *TlsGetAddr = createMCSymbolForTlsGetAddr(OutContext); 2593 ExtSymSDNodeSymbols.insert(TlsGetAddr); 2594 break; 2595 } 2596 case PPC::BL8: 2597 case PPC::BL: 2598 case PPC::BL8_NOP: 2599 case PPC::BL_NOP: { 2600 const MachineOperand &MO = MI->getOperand(0); 2601 if (MO.isSymbol()) { 2602 MCSymbolXCOFF *S = 2603 cast<MCSymbolXCOFF>(OutContext.getOrCreateSymbol(MO.getSymbolName())); 2604 ExtSymSDNodeSymbols.insert(S); 2605 } 2606 } break; 2607 case PPC::BL_TLS: 2608 case PPC::BL8_TLS: 2609 case PPC::BL8_TLS_: 2610 case PPC::BL8_NOP_TLS: 2611 report_fatal_error("TLS call not yet implemented"); 2612 case PPC::TAILB: 2613 case PPC::TAILB8: 2614 case PPC::TAILBA: 2615 case PPC::TAILBA8: 2616 case PPC::TAILBCTR: 2617 case PPC::TAILBCTR8: 2618 if (MI->getOperand(0).isSymbol()) 2619 report_fatal_error("Tail call for extern symbol not yet supported."); 2620 break; 2621 case PPC::DST: 2622 case PPC::DST64: 2623 case PPC::DSTT: 2624 case PPC::DSTT64: 2625 case PPC::DSTST: 2626 case PPC::DSTST64: 2627 case PPC::DSTSTT: 2628 case PPC::DSTSTT64: 2629 EmitToStreamer( 2630 *OutStreamer, 2631 MCInstBuilder(PPC::ORI).addReg(PPC::R0).addReg(PPC::R0).addImm(0)); 2632 return; 2633 } 2634 return PPCAsmPrinter::emitInstruction(MI); 2635 } 2636 2637 bool PPCAIXAsmPrinter::doFinalization(Module &M) { 2638 // Do streamer related finalization for DWARF. 2639 if (!MAI->usesDwarfFileAndLocDirectives() && MMI->hasDebugInfo()) 2640 OutStreamer->doFinalizationAtSectionEnd( 2641 OutStreamer->getContext().getObjectFileInfo()->getTextSection()); 2642 2643 for (MCSymbol *Sym : ExtSymSDNodeSymbols) 2644 OutStreamer->emitSymbolAttribute(Sym, MCSA_Extern); 2645 return PPCAsmPrinter::doFinalization(M); 2646 } 2647 2648 static unsigned mapToSinitPriority(int P) { 2649 if (P < 0 || P > 65535) 2650 report_fatal_error("invalid init priority"); 2651 2652 if (P <= 20) 2653 return P; 2654 2655 if (P < 81) 2656 return 20 + (P - 20) * 16; 2657 2658 if (P <= 1124) 2659 return 1004 + (P - 81); 2660 2661 if (P < 64512) 2662 return 2047 + (P - 1124) * 33878; 2663 2664 return 2147482625u + (P - 64512); 2665 } 2666 2667 static std::string convertToSinitPriority(int Priority) { 2668 // This helper function converts clang init priority to values used in sinit 2669 // and sterm functions. 2670 // 2671 // The conversion strategies are: 2672 // We map the reserved clang/gnu priority range [0, 100] into the sinit/sterm 2673 // reserved priority range [0, 1023] by 2674 // - directly mapping the first 21 and the last 20 elements of the ranges 2675 // - linear interpolating the intermediate values with a step size of 16. 2676 // 2677 // We map the non reserved clang/gnu priority range of [101, 65535] into the 2678 // sinit/sterm priority range [1024, 2147483648] by: 2679 // - directly mapping the first and the last 1024 elements of the ranges 2680 // - linear interpolating the intermediate values with a step size of 33878. 2681 unsigned int P = mapToSinitPriority(Priority); 2682 2683 std::string PrioritySuffix; 2684 llvm::raw_string_ostream os(PrioritySuffix); 2685 os << llvm::format_hex_no_prefix(P, 8); 2686 os.flush(); 2687 return PrioritySuffix; 2688 } 2689 2690 void PPCAIXAsmPrinter::emitXXStructorList(const DataLayout &DL, 2691 const Constant *List, bool IsCtor) { 2692 SmallVector<Structor, 8> Structors; 2693 preprocessXXStructorList(DL, List, Structors); 2694 if (Structors.empty()) 2695 return; 2696 2697 unsigned Index = 0; 2698 for (Structor &S : Structors) { 2699 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(S.Func)) 2700 S.Func = CE->getOperand(0); 2701 2702 llvm::GlobalAlias::create( 2703 GlobalValue::ExternalLinkage, 2704 (IsCtor ? llvm::Twine("__sinit") : llvm::Twine("__sterm")) + 2705 llvm::Twine(convertToSinitPriority(S.Priority)) + 2706 llvm::Twine("_", FormatIndicatorAndUniqueModId) + 2707 llvm::Twine("_", llvm::utostr(Index++)), 2708 cast<Function>(S.Func)); 2709 } 2710 } 2711 2712 void PPCAIXAsmPrinter::emitTTypeReference(const GlobalValue *GV, 2713 unsigned Encoding) { 2714 if (GV) { 2715 MCSymbol *TypeInfoSym = TM.getSymbol(GV); 2716 MCSymbol *TOCEntry = lookUpOrCreateTOCEntry(TypeInfoSym); 2717 const MCSymbol *TOCBaseSym = 2718 cast<MCSectionXCOFF>(getObjFileLowering().getTOCBaseSection()) 2719 ->getQualNameSymbol(); 2720 auto &Ctx = OutStreamer->getContext(); 2721 const MCExpr *Exp = 2722 MCBinaryExpr::createSub(MCSymbolRefExpr::create(TOCEntry, Ctx), 2723 MCSymbolRefExpr::create(TOCBaseSym, Ctx), Ctx); 2724 OutStreamer->emitValue(Exp, GetSizeOfEncodedValue(Encoding)); 2725 } else 2726 OutStreamer->emitIntValue(0, GetSizeOfEncodedValue(Encoding)); 2727 } 2728 2729 // Return a pass that prints the PPC assembly code for a MachineFunction to the 2730 // given output stream. 2731 static AsmPrinter * 2732 createPPCAsmPrinterPass(TargetMachine &tm, 2733 std::unique_ptr<MCStreamer> &&Streamer) { 2734 if (tm.getTargetTriple().isOSAIX()) 2735 return new PPCAIXAsmPrinter(tm, std::move(Streamer)); 2736 2737 return new PPCLinuxAsmPrinter(tm, std::move(Streamer)); 2738 } 2739 2740 // Force static initialization. 2741 extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializePowerPCAsmPrinter() { 2742 TargetRegistry::RegisterAsmPrinter(getThePPC32Target(), 2743 createPPCAsmPrinterPass); 2744 TargetRegistry::RegisterAsmPrinter(getThePPC32LETarget(), 2745 createPPCAsmPrinterPass); 2746 TargetRegistry::RegisterAsmPrinter(getThePPC64Target(), 2747 createPPCAsmPrinterPass); 2748 TargetRegistry::RegisterAsmPrinter(getThePPC64LETarget(), 2749 createPPCAsmPrinterPass); 2750 } 2751