[Alignment][NFC] Use Align with TargetLowering::setMinFunctionAlignment
[llvm-core.git] / lib / Target / PowerPC / PPCAsmPrinter.cpp
blobfc59360ebe2da80e66669f66609accff0c77a557
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'.
13 // Documentation at http://developer.apple.com/documentation/DeveloperTools/
14 // Reference/Assembler/ASMIntroduction/chapter_1_section_1.html
16 //===----------------------------------------------------------------------===//
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/StringRef.h"
31 #include "llvm/ADT/Triple.h"
32 #include "llvm/ADT/Twine.h"
33 #include "llvm/BinaryFormat/ELF.h"
34 #include "llvm/BinaryFormat/MachO.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/Module.h"
47 #include "llvm/MC/MCAsmInfo.h"
48 #include "llvm/MC/MCContext.h"
49 #include "llvm/MC/MCExpr.h"
50 #include "llvm/MC/MCInst.h"
51 #include "llvm/MC/MCInstBuilder.h"
52 #include "llvm/MC/MCSectionELF.h"
53 #include "llvm/MC/MCSectionMachO.h"
54 #include "llvm/MC/MCSectionXCOFF.h"
55 #include "llvm/MC/MCStreamer.h"
56 #include "llvm/MC/MCSymbol.h"
57 #include "llvm/MC/MCSymbolELF.h"
58 #include "llvm/MC/MCSymbolXCOFF.h"
59 #include "llvm/MC/SectionKind.h"
60 #include "llvm/Support/Casting.h"
61 #include "llvm/Support/CodeGen.h"
62 #include "llvm/Support/Debug.h"
63 #include "llvm/Support/ErrorHandling.h"
64 #include "llvm/Support/TargetRegistry.h"
65 #include "llvm/Support/raw_ostream.h"
66 #include "llvm/Target/TargetMachine.h"
67 #include <algorithm>
68 #include <cassert>
69 #include <cstdint>
70 #include <memory>
71 #include <new>
73 using namespace llvm;
75 #define DEBUG_TYPE "asmprinter"
77 namespace {
79 class PPCAsmPrinter : public AsmPrinter {
80 protected:
81 MapVector<MCSymbol *, MCSymbol *> TOC;
82 const PPCSubtarget *Subtarget;
83 StackMaps SM;
85 public:
86 explicit PPCAsmPrinter(TargetMachine &TM,
87 std::unique_ptr<MCStreamer> Streamer)
88 : AsmPrinter(TM, std::move(Streamer)), SM(*this) {}
90 StringRef getPassName() const override { return "PowerPC Assembly Printer"; }
92 MCSymbol *lookUpOrCreateTOCEntry(MCSymbol *Sym);
94 bool doInitialization(Module &M) override {
95 if (!TOC.empty())
96 TOC.clear();
97 return AsmPrinter::doInitialization(M);
100 void EmitInstruction(const MachineInstr *MI) override;
102 /// This function is for PrintAsmOperand and PrintAsmMemoryOperand,
103 /// invoked by EmitMSInlineAsmStr and EmitGCCInlineAsmStr only.
104 /// The \p MI would be INLINEASM ONLY.
105 void printOperand(const MachineInstr *MI, unsigned OpNo, raw_ostream &O);
107 void PrintSymbolOperand(const MachineOperand &MO, raw_ostream &O) override;
108 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
109 const char *ExtraCode, raw_ostream &O) override;
110 bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
111 const char *ExtraCode, raw_ostream &O) override;
113 void EmitEndOfAsmFile(Module &M) override;
115 void LowerSTACKMAP(StackMaps &SM, const MachineInstr &MI);
116 void LowerPATCHPOINT(StackMaps &SM, const MachineInstr &MI);
117 void EmitTlsCall(const MachineInstr *MI, MCSymbolRefExpr::VariantKind VK);
118 bool runOnMachineFunction(MachineFunction &MF) override {
119 Subtarget = &MF.getSubtarget<PPCSubtarget>();
120 bool Changed = AsmPrinter::runOnMachineFunction(MF);
121 emitXRayTable();
122 return Changed;
126 /// PPCLinuxAsmPrinter - PowerPC assembly printer, customized for Linux
127 class PPCLinuxAsmPrinter : public PPCAsmPrinter {
128 public:
129 explicit PPCLinuxAsmPrinter(TargetMachine &TM,
130 std::unique_ptr<MCStreamer> Streamer)
131 : PPCAsmPrinter(TM, std::move(Streamer)) {}
133 StringRef getPassName() const override {
134 return "Linux PPC Assembly Printer";
137 bool doFinalization(Module &M) override;
138 void EmitStartOfAsmFile(Module &M) override;
140 void EmitFunctionEntryLabel() override;
142 void EmitFunctionBodyStart() override;
143 void EmitFunctionBodyEnd() override;
144 void EmitInstruction(const MachineInstr *MI) override;
147 /// PPCDarwinAsmPrinter - PowerPC assembly printer, customized for Darwin/Mac
148 /// OS X
149 class PPCDarwinAsmPrinter : public PPCAsmPrinter {
150 public:
151 explicit PPCDarwinAsmPrinter(TargetMachine &TM,
152 std::unique_ptr<MCStreamer> Streamer)
153 : PPCAsmPrinter(TM, std::move(Streamer)) {}
155 StringRef getPassName() const override {
156 return "Darwin PPC Assembly Printer";
159 bool doFinalization(Module &M) override;
160 void EmitStartOfAsmFile(Module &M) override;
163 class PPCAIXAsmPrinter : public PPCAsmPrinter {
164 public:
165 PPCAIXAsmPrinter(TargetMachine &TM, std::unique_ptr<MCStreamer> Streamer)
166 : PPCAsmPrinter(TM, std::move(Streamer)) {}
168 StringRef getPassName() const override { return "AIX PPC Assembly Printer"; }
170 void EmitGlobalVariable(const GlobalVariable *GV) override;
173 } // end anonymous namespace
175 void PPCAsmPrinter::PrintSymbolOperand(const MachineOperand &MO,
176 raw_ostream &O) {
177 // Computing the address of a global symbol, not calling it.
178 const GlobalValue *GV = MO.getGlobal();
179 MCSymbol *SymToPrint;
181 // External or weakly linked global variables need non-lazily-resolved stubs
182 if (Subtarget->hasLazyResolverStub(GV)) {
183 SymToPrint = getSymbolWithGlobalValueBase(GV, "$non_lazy_ptr");
184 MachineModuleInfoImpl::StubValueTy &StubSym =
185 MMI->getObjFileInfo<MachineModuleInfoMachO>().getGVStubEntry(
186 SymToPrint);
187 if (!StubSym.getPointer())
188 StubSym = MachineModuleInfoImpl::StubValueTy(getSymbol(GV),
189 !GV->hasInternalLinkage());
190 } else {
191 SymToPrint = getSymbol(GV);
194 SymToPrint->print(O, MAI);
196 printOffset(MO.getOffset(), O);
199 void PPCAsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNo,
200 raw_ostream &O) {
201 const DataLayout &DL = getDataLayout();
202 const MachineOperand &MO = MI->getOperand(OpNo);
204 switch (MO.getType()) {
205 case MachineOperand::MO_Register: {
206 // The MI is INLINEASM ONLY and UseVSXReg is always false.
207 const char *RegName = PPCInstPrinter::getRegisterName(MO.getReg());
209 // Linux assembler (Others?) does not take register mnemonics.
210 // FIXME - What about special registers used in mfspr/mtspr?
211 if (!Subtarget->isDarwin())
212 RegName = PPCRegisterInfo::stripRegisterPrefix(RegName);
213 O << RegName;
214 return;
216 case MachineOperand::MO_Immediate:
217 O << MO.getImm();
218 return;
220 case MachineOperand::MO_MachineBasicBlock:
221 MO.getMBB()->getSymbol()->print(O, MAI);
222 return;
223 case MachineOperand::MO_ConstantPoolIndex:
224 O << DL.getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << '_'
225 << MO.getIndex();
226 return;
227 case MachineOperand::MO_BlockAddress:
228 GetBlockAddressSymbol(MO.getBlockAddress())->print(O, MAI);
229 return;
230 case MachineOperand::MO_GlobalAddress: {
231 PrintSymbolOperand(MO, O);
232 return;
235 default:
236 O << "<unknown operand type: " << (unsigned)MO.getType() << ">";
237 return;
241 /// PrintAsmOperand - Print out an operand for an inline asm expression.
243 bool PPCAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
244 const char *ExtraCode, raw_ostream &O) {
245 // Does this asm operand have a single letter operand modifier?
246 if (ExtraCode && ExtraCode[0]) {
247 if (ExtraCode[1] != 0) return true; // Unknown modifier.
249 switch (ExtraCode[0]) {
250 default:
251 // See if this is a generic print operand
252 return AsmPrinter::PrintAsmOperand(MI, OpNo, ExtraCode, O);
253 case 'L': // Write second word of DImode reference.
254 // Verify that this operand has two consecutive registers.
255 if (!MI->getOperand(OpNo).isReg() ||
256 OpNo+1 == MI->getNumOperands() ||
257 !MI->getOperand(OpNo+1).isReg())
258 return true;
259 ++OpNo; // Return the high-part.
260 break;
261 case 'I':
262 // Write 'i' if an integer constant, otherwise nothing. Used to print
263 // addi vs add, etc.
264 if (MI->getOperand(OpNo).isImm())
265 O << "i";
266 return false;
267 case 'x':
268 if(!MI->getOperand(OpNo).isReg())
269 return true;
270 // This operand uses VSX numbering.
271 // If the operand is a VMX register, convert it to a VSX register.
272 Register Reg = MI->getOperand(OpNo).getReg();
273 if (PPCInstrInfo::isVRRegister(Reg))
274 Reg = PPC::VSX32 + (Reg - PPC::V0);
275 else if (PPCInstrInfo::isVFRegister(Reg))
276 Reg = PPC::VSX32 + (Reg - PPC::VF0);
277 const char *RegName;
278 RegName = PPCInstPrinter::getRegisterName(Reg);
279 RegName = PPCRegisterInfo::stripRegisterPrefix(RegName);
280 O << RegName;
281 return false;
285 printOperand(MI, OpNo, O);
286 return false;
289 // At the moment, all inline asm memory operands are a single register.
290 // In any case, the output of this routine should always be just one
291 // assembler operand.
293 bool PPCAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
294 const char *ExtraCode,
295 raw_ostream &O) {
296 if (ExtraCode && ExtraCode[0]) {
297 if (ExtraCode[1] != 0) return true; // Unknown modifier.
299 switch (ExtraCode[0]) {
300 default: return true; // Unknown modifier.
301 case 'y': // A memory reference for an X-form instruction
303 const char *RegName = "r0";
304 if (!Subtarget->isDarwin())
305 RegName = PPCRegisterInfo::stripRegisterPrefix(RegName);
306 O << RegName << ", ";
307 printOperand(MI, OpNo, O);
308 return false;
310 case 'U': // Print 'u' for update form.
311 case 'X': // Print 'x' for indexed form.
313 // FIXME: Currently for PowerPC memory operands are always loaded
314 // into a register, so we never get an update or indexed form.
315 // This is bad even for offset forms, since even if we know we
316 // have a value in -16(r1), we will generate a load into r<n>
317 // and then load from 0(r<n>). Until that issue is fixed,
318 // tolerate 'U' and 'X' but don't output anything.
319 assert(MI->getOperand(OpNo).isReg());
320 return false;
325 assert(MI->getOperand(OpNo).isReg());
326 O << "0(";
327 printOperand(MI, OpNo, O);
328 O << ")";
329 return false;
332 /// lookUpOrCreateTOCEntry -- Given a symbol, look up whether a TOC entry
333 /// exists for it. If not, create one. Then return a symbol that references
334 /// the TOC entry.
335 MCSymbol *PPCAsmPrinter::lookUpOrCreateTOCEntry(MCSymbol *Sym) {
336 MCSymbol *&TOCEntry = TOC[Sym];
337 if (!TOCEntry)
338 TOCEntry = createTempSymbol("C");
339 return TOCEntry;
342 void PPCAsmPrinter::EmitEndOfAsmFile(Module &M) {
343 emitStackMaps(SM);
346 void PPCAsmPrinter::LowerSTACKMAP(StackMaps &SM, const MachineInstr &MI) {
347 unsigned NumNOPBytes = MI.getOperand(1).getImm();
349 SM.recordStackMap(MI);
350 assert(NumNOPBytes % 4 == 0 && "Invalid number of NOP bytes requested!");
352 // Scan ahead to trim the shadow.
353 const MachineBasicBlock &MBB = *MI.getParent();
354 MachineBasicBlock::const_iterator MII(MI);
355 ++MII;
356 while (NumNOPBytes > 0) {
357 if (MII == MBB.end() || MII->isCall() ||
358 MII->getOpcode() == PPC::DBG_VALUE ||
359 MII->getOpcode() == TargetOpcode::PATCHPOINT ||
360 MII->getOpcode() == TargetOpcode::STACKMAP)
361 break;
362 ++MII;
363 NumNOPBytes -= 4;
366 // Emit nops.
367 for (unsigned i = 0; i < NumNOPBytes; i += 4)
368 EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::NOP));
371 // Lower a patchpoint of the form:
372 // [<def>], <id>, <numBytes>, <target>, <numArgs>
373 void PPCAsmPrinter::LowerPATCHPOINT(StackMaps &SM, const MachineInstr &MI) {
374 SM.recordPatchPoint(MI);
375 PatchPointOpers Opers(&MI);
377 unsigned EncodedBytes = 0;
378 const MachineOperand &CalleeMO = Opers.getCallTarget();
380 if (CalleeMO.isImm()) {
381 int64_t CallTarget = CalleeMO.getImm();
382 if (CallTarget) {
383 assert((CallTarget & 0xFFFFFFFFFFFF) == CallTarget &&
384 "High 16 bits of call target should be zero.");
385 Register ScratchReg = MI.getOperand(Opers.getNextScratchIdx()).getReg();
386 EncodedBytes = 0;
387 // Materialize the jump address:
388 EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::LI8)
389 .addReg(ScratchReg)
390 .addImm((CallTarget >> 32) & 0xFFFF));
391 ++EncodedBytes;
392 EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::RLDIC)
393 .addReg(ScratchReg)
394 .addReg(ScratchReg)
395 .addImm(32).addImm(16));
396 ++EncodedBytes;
397 EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::ORIS8)
398 .addReg(ScratchReg)
399 .addReg(ScratchReg)
400 .addImm((CallTarget >> 16) & 0xFFFF));
401 ++EncodedBytes;
402 EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::ORI8)
403 .addReg(ScratchReg)
404 .addReg(ScratchReg)
405 .addImm(CallTarget & 0xFFFF));
407 // Save the current TOC pointer before the remote call.
408 int TOCSaveOffset = Subtarget->getFrameLowering()->getTOCSaveOffset();
409 EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::STD)
410 .addReg(PPC::X2)
411 .addImm(TOCSaveOffset)
412 .addReg(PPC::X1));
413 ++EncodedBytes;
415 // If we're on ELFv1, then we need to load the actual function pointer
416 // from the function descriptor.
417 if (!Subtarget->isELFv2ABI()) {
418 // Load the new TOC pointer and the function address, but not r11
419 // (needing this is rare, and loading it here would prevent passing it
420 // via a 'nest' parameter.
421 EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::LD)
422 .addReg(PPC::X2)
423 .addImm(8)
424 .addReg(ScratchReg));
425 ++EncodedBytes;
426 EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::LD)
427 .addReg(ScratchReg)
428 .addImm(0)
429 .addReg(ScratchReg));
430 ++EncodedBytes;
433 EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::MTCTR8)
434 .addReg(ScratchReg));
435 ++EncodedBytes;
436 EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::BCTRL8));
437 ++EncodedBytes;
439 // Restore the TOC pointer after the call.
440 EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::LD)
441 .addReg(PPC::X2)
442 .addImm(TOCSaveOffset)
443 .addReg(PPC::X1));
444 ++EncodedBytes;
446 } else if (CalleeMO.isGlobal()) {
447 const GlobalValue *GValue = CalleeMO.getGlobal();
448 MCSymbol *MOSymbol = getSymbol(GValue);
449 const MCExpr *SymVar = MCSymbolRefExpr::create(MOSymbol, OutContext);
451 EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::BL8_NOP)
452 .addExpr(SymVar));
453 EncodedBytes += 2;
456 // Each instruction is 4 bytes.
457 EncodedBytes *= 4;
459 // Emit padding.
460 unsigned NumBytes = Opers.getNumPatchBytes();
461 assert(NumBytes >= EncodedBytes &&
462 "Patchpoint can't request size less than the length of a call.");
463 assert((NumBytes - EncodedBytes) % 4 == 0 &&
464 "Invalid number of NOP bytes requested!");
465 for (unsigned i = EncodedBytes; i < NumBytes; i += 4)
466 EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::NOP));
469 /// EmitTlsCall -- Given a GETtls[ld]ADDR[32] instruction, print a
470 /// call to __tls_get_addr to the current output stream.
471 void PPCAsmPrinter::EmitTlsCall(const MachineInstr *MI,
472 MCSymbolRefExpr::VariantKind VK) {
473 StringRef Name = "__tls_get_addr";
474 MCSymbol *TlsGetAddr = OutContext.getOrCreateSymbol(Name);
475 MCSymbolRefExpr::VariantKind Kind = MCSymbolRefExpr::VK_None;
476 const Module *M = MF->getFunction().getParent();
478 assert(MI->getOperand(0).isReg() &&
479 ((Subtarget->isPPC64() && MI->getOperand(0).getReg() == PPC::X3) ||
480 (!Subtarget->isPPC64() && MI->getOperand(0).getReg() == PPC::R3)) &&
481 "GETtls[ld]ADDR[32] must define GPR3");
482 assert(MI->getOperand(1).isReg() &&
483 ((Subtarget->isPPC64() && MI->getOperand(1).getReg() == PPC::X3) ||
484 (!Subtarget->isPPC64() && MI->getOperand(1).getReg() == PPC::R3)) &&
485 "GETtls[ld]ADDR[32] must read GPR3");
487 if (!Subtarget->isPPC64() && !Subtarget->isDarwin() &&
488 isPositionIndependent())
489 Kind = MCSymbolRefExpr::VK_PLT;
490 const MCExpr *TlsRef =
491 MCSymbolRefExpr::create(TlsGetAddr, Kind, OutContext);
493 // Add 32768 offset to the symbol so we follow up the latest GOT/PLT ABI.
494 if (Kind == MCSymbolRefExpr::VK_PLT && Subtarget->isSecurePlt() &&
495 M->getPICLevel() == PICLevel::BigPIC)
496 TlsRef = MCBinaryExpr::createAdd(
497 TlsRef, MCConstantExpr::create(32768, OutContext), OutContext);
498 const MachineOperand &MO = MI->getOperand(2);
499 const GlobalValue *GValue = MO.getGlobal();
500 MCSymbol *MOSymbol = getSymbol(GValue);
501 const MCExpr *SymVar = MCSymbolRefExpr::create(MOSymbol, VK, OutContext);
502 EmitToStreamer(*OutStreamer,
503 MCInstBuilder(Subtarget->isPPC64() ?
504 PPC::BL8_NOP_TLS : PPC::BL_TLS)
505 .addExpr(TlsRef)
506 .addExpr(SymVar));
509 /// EmitInstruction -- Print out a single PowerPC MI in Darwin syntax to
510 /// the current output stream.
512 void PPCAsmPrinter::EmitInstruction(const MachineInstr *MI) {
513 MCInst TmpInst;
514 bool isDarwin = TM.getTargetTriple().isOSDarwin();
515 const Module *M = MF->getFunction().getParent();
516 PICLevel::Level PL = M->getPICLevel();
518 #ifndef NDEBUG
519 // Validate that SPE and FPU are mutually exclusive in codegen
520 if (!MI->isInlineAsm()) {
521 for (const MachineOperand &MO: MI->operands()) {
522 if (MO.isReg()) {
523 Register Reg = MO.getReg();
524 if (Subtarget->hasSPE()) {
525 if (PPC::F4RCRegClass.contains(Reg) ||
526 PPC::F8RCRegClass.contains(Reg) ||
527 PPC::QBRCRegClass.contains(Reg) ||
528 PPC::QFRCRegClass.contains(Reg) ||
529 PPC::QSRCRegClass.contains(Reg) ||
530 PPC::VFRCRegClass.contains(Reg) ||
531 PPC::VRRCRegClass.contains(Reg) ||
532 PPC::VSFRCRegClass.contains(Reg) ||
533 PPC::VSSRCRegClass.contains(Reg)
535 llvm_unreachable("SPE targets cannot have FPRegs!");
536 } else {
537 if (PPC::SPERCRegClass.contains(Reg))
538 llvm_unreachable("SPE register found in FPU-targeted code!");
543 #endif
544 // Lower multi-instruction pseudo operations.
545 switch (MI->getOpcode()) {
546 default: break;
547 case TargetOpcode::DBG_VALUE:
548 llvm_unreachable("Should be handled target independently");
549 case TargetOpcode::STACKMAP:
550 return LowerSTACKMAP(SM, *MI);
551 case TargetOpcode::PATCHPOINT:
552 return LowerPATCHPOINT(SM, *MI);
554 case PPC::MoveGOTtoLR: {
555 // Transform %lr = MoveGOTtoLR
556 // Into this: bl _GLOBAL_OFFSET_TABLE_@local-4
557 // _GLOBAL_OFFSET_TABLE_@local-4 (instruction preceding
558 // _GLOBAL_OFFSET_TABLE_) has exactly one instruction:
559 // blrl
560 // This will return the pointer to _GLOBAL_OFFSET_TABLE_@local
561 MCSymbol *GOTSymbol =
562 OutContext.getOrCreateSymbol(StringRef("_GLOBAL_OFFSET_TABLE_"));
563 const MCExpr *OffsExpr =
564 MCBinaryExpr::createSub(MCSymbolRefExpr::create(GOTSymbol,
565 MCSymbolRefExpr::VK_PPC_LOCAL,
566 OutContext),
567 MCConstantExpr::create(4, OutContext),
568 OutContext);
570 // Emit the 'bl'.
571 EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::BL).addExpr(OffsExpr));
572 return;
574 case PPC::MovePCtoLR:
575 case PPC::MovePCtoLR8: {
576 // Transform %lr = MovePCtoLR
577 // Into this, where the label is the PIC base:
578 // bl L1$pb
579 // L1$pb:
580 MCSymbol *PICBase = MF->getPICBaseSymbol();
582 // Emit the 'bl'.
583 EmitToStreamer(*OutStreamer,
584 MCInstBuilder(PPC::BL)
585 // FIXME: We would like an efficient form for this, so we
586 // don't have to do a lot of extra uniquing.
587 .addExpr(MCSymbolRefExpr::create(PICBase, OutContext)));
589 // Emit the label.
590 OutStreamer->EmitLabel(PICBase);
591 return;
593 case PPC::UpdateGBR: {
594 // Transform %rd = UpdateGBR(%rt, %ri)
595 // Into: lwz %rt, .L0$poff - .L0$pb(%ri)
596 // add %rd, %rt, %ri
597 // or into (if secure plt mode is on):
598 // addis r30, r30, {.LTOC,_GLOBAL_OFFSET_TABLE} - .L0$pb@ha
599 // addi r30, r30, {.LTOC,_GLOBAL_OFFSET_TABLE} - .L0$pb@l
600 // Get the offset from the GOT Base Register to the GOT
601 LowerPPCMachineInstrToMCInst(MI, TmpInst, *this, isDarwin);
602 if (Subtarget->isSecurePlt() && isPositionIndependent() ) {
603 unsigned PICR = TmpInst.getOperand(0).getReg();
604 MCSymbol *BaseSymbol = OutContext.getOrCreateSymbol(
605 M->getPICLevel() == PICLevel::SmallPIC ? "_GLOBAL_OFFSET_TABLE_"
606 : ".LTOC");
607 const MCExpr *PB =
608 MCSymbolRefExpr::create(MF->getPICBaseSymbol(), OutContext);
610 const MCExpr *DeltaExpr = MCBinaryExpr::createSub(
611 MCSymbolRefExpr::create(BaseSymbol, OutContext), PB, OutContext);
613 const MCExpr *DeltaHi = PPCMCExpr::createHa(DeltaExpr, false, OutContext);
614 EmitToStreamer(
615 *OutStreamer,
616 MCInstBuilder(PPC::ADDIS).addReg(PICR).addReg(PICR).addExpr(DeltaHi));
618 const MCExpr *DeltaLo = PPCMCExpr::createLo(DeltaExpr, false, OutContext);
619 EmitToStreamer(
620 *OutStreamer,
621 MCInstBuilder(PPC::ADDI).addReg(PICR).addReg(PICR).addExpr(DeltaLo));
622 return;
623 } else {
624 MCSymbol *PICOffset =
625 MF->getInfo<PPCFunctionInfo>()->getPICOffsetSymbol();
626 TmpInst.setOpcode(PPC::LWZ);
627 const MCExpr *Exp =
628 MCSymbolRefExpr::create(PICOffset, MCSymbolRefExpr::VK_None, OutContext);
629 const MCExpr *PB =
630 MCSymbolRefExpr::create(MF->getPICBaseSymbol(),
631 MCSymbolRefExpr::VK_None,
632 OutContext);
633 const MCOperand TR = TmpInst.getOperand(1);
634 const MCOperand PICR = TmpInst.getOperand(0);
636 // Step 1: lwz %rt, .L$poff - .L$pb(%ri)
637 TmpInst.getOperand(1) =
638 MCOperand::createExpr(MCBinaryExpr::createSub(Exp, PB, OutContext));
639 TmpInst.getOperand(0) = TR;
640 TmpInst.getOperand(2) = PICR;
641 EmitToStreamer(*OutStreamer, TmpInst);
643 TmpInst.setOpcode(PPC::ADD4);
644 TmpInst.getOperand(0) = PICR;
645 TmpInst.getOperand(1) = TR;
646 TmpInst.getOperand(2) = PICR;
647 EmitToStreamer(*OutStreamer, TmpInst);
648 return;
651 case PPC::LWZtoc: {
652 // Transform %r3 = LWZtoc @min1, %r2
653 LowerPPCMachineInstrToMCInst(MI, TmpInst, *this, isDarwin);
655 // Change the opcode to LWZ, and the global address operand to be a
656 // reference to the GOT entry we will synthesize later.
657 TmpInst.setOpcode(PPC::LWZ);
658 const MachineOperand &MO = MI->getOperand(1);
660 // Map symbol -> label of TOC entry
661 assert(MO.isGlobal() || MO.isCPI() || MO.isJTI() || MO.isBlockAddress());
662 MCSymbol *MOSymbol = nullptr;
663 if (MO.isGlobal())
664 MOSymbol = getSymbol(MO.getGlobal());
665 else if (MO.isCPI())
666 MOSymbol = GetCPISymbol(MO.getIndex());
667 else if (MO.isJTI())
668 MOSymbol = GetJTISymbol(MO.getIndex());
669 else if (MO.isBlockAddress())
670 MOSymbol = GetBlockAddressSymbol(MO.getBlockAddress());
672 if (PL == PICLevel::SmallPIC) {
673 const MCExpr *Exp =
674 MCSymbolRefExpr::create(MOSymbol, MCSymbolRefExpr::VK_GOT,
675 OutContext);
676 TmpInst.getOperand(1) = MCOperand::createExpr(Exp);
677 } else {
678 MCSymbol *TOCEntry = lookUpOrCreateTOCEntry(MOSymbol);
680 const MCExpr *Exp =
681 MCSymbolRefExpr::create(TOCEntry, MCSymbolRefExpr::VK_None,
682 OutContext);
683 const MCExpr *PB =
684 MCSymbolRefExpr::create(OutContext.getOrCreateSymbol(Twine(".LTOC")),
685 OutContext);
686 Exp = MCBinaryExpr::createSub(Exp, PB, OutContext);
687 TmpInst.getOperand(1) = MCOperand::createExpr(Exp);
689 EmitToStreamer(*OutStreamer, TmpInst);
690 return;
692 case PPC::LDtocJTI:
693 case PPC::LDtocCPT:
694 case PPC::LDtocBA:
695 case PPC::LDtoc: {
696 // Transform %x3 = LDtoc @min1, %x2
697 LowerPPCMachineInstrToMCInst(MI, TmpInst, *this, isDarwin);
699 // Change the opcode to LD, and the global address operand to be a
700 // reference to the TOC entry we will synthesize later.
701 TmpInst.setOpcode(PPC::LD);
702 const MachineOperand &MO = MI->getOperand(1);
704 // Map symbol -> label of TOC entry
705 assert(MO.isGlobal() || MO.isCPI() || MO.isJTI() || MO.isBlockAddress());
706 MCSymbol *MOSymbol = nullptr;
707 if (MO.isGlobal())
708 MOSymbol = getSymbol(MO.getGlobal());
709 else if (MO.isCPI())
710 MOSymbol = GetCPISymbol(MO.getIndex());
711 else if (MO.isJTI())
712 MOSymbol = GetJTISymbol(MO.getIndex());
713 else if (MO.isBlockAddress())
714 MOSymbol = GetBlockAddressSymbol(MO.getBlockAddress());
716 MCSymbol *TOCEntry = lookUpOrCreateTOCEntry(MOSymbol);
718 const MCExpr *Exp =
719 MCSymbolRefExpr::create(TOCEntry, MCSymbolRefExpr::VK_PPC_TOC,
720 OutContext);
721 TmpInst.getOperand(1) = MCOperand::createExpr(Exp);
722 EmitToStreamer(*OutStreamer, TmpInst);
723 return;
726 case PPC::ADDIStocHA8: {
727 // Transform %xd = ADDIStocHA8 %x2, @sym
728 LowerPPCMachineInstrToMCInst(MI, TmpInst, *this, isDarwin);
730 // Change the opcode to ADDIS8. If the global address is external, has
731 // common linkage, is a non-local function address, or is a jump table
732 // address, then generate a TOC entry and reference that. Otherwise
733 // reference the symbol directly.
734 TmpInst.setOpcode(PPC::ADDIS8);
735 const MachineOperand &MO = MI->getOperand(2);
736 assert((MO.isGlobal() || MO.isCPI() || MO.isJTI() ||
737 MO.isBlockAddress()) &&
738 "Invalid operand for ADDIStocHA8!");
739 MCSymbol *MOSymbol = nullptr;
740 bool GlobalToc = false;
742 if (MO.isGlobal()) {
743 const GlobalValue *GV = MO.getGlobal();
744 MOSymbol = getSymbol(GV);
745 unsigned char GVFlags = Subtarget->classifyGlobalReference(GV);
746 GlobalToc = (GVFlags & PPCII::MO_NLP_FLAG);
747 } else if (MO.isCPI()) {
748 MOSymbol = GetCPISymbol(MO.getIndex());
749 } else if (MO.isJTI()) {
750 MOSymbol = GetJTISymbol(MO.getIndex());
751 } else if (MO.isBlockAddress()) {
752 MOSymbol = GetBlockAddressSymbol(MO.getBlockAddress());
755 if (GlobalToc || MO.isJTI() || MO.isBlockAddress() ||
756 TM.getCodeModel() == CodeModel::Large)
757 MOSymbol = lookUpOrCreateTOCEntry(MOSymbol);
759 const MCExpr *Exp =
760 MCSymbolRefExpr::create(MOSymbol, MCSymbolRefExpr::VK_PPC_TOC_HA,
761 OutContext);
763 if (!MO.isJTI() && MO.getOffset())
764 Exp = MCBinaryExpr::createAdd(Exp,
765 MCConstantExpr::create(MO.getOffset(),
766 OutContext),
767 OutContext);
769 TmpInst.getOperand(2) = MCOperand::createExpr(Exp);
770 EmitToStreamer(*OutStreamer, TmpInst);
771 return;
773 case PPC::LDtocL: {
774 // Transform %xd = LDtocL @sym, %xs
775 LowerPPCMachineInstrToMCInst(MI, TmpInst, *this, isDarwin);
777 // Change the opcode to LD. If the global address is external, has
778 // common linkage, or is a jump table address, then reference the
779 // associated TOC entry. Otherwise reference the symbol directly.
780 TmpInst.setOpcode(PPC::LD);
781 const MachineOperand &MO = MI->getOperand(1);
782 assert((MO.isGlobal() || MO.isCPI() || MO.isJTI() ||
783 MO.isBlockAddress()) &&
784 "Invalid operand for LDtocL!");
785 MCSymbol *MOSymbol = nullptr;
787 if (MO.isJTI())
788 MOSymbol = lookUpOrCreateTOCEntry(GetJTISymbol(MO.getIndex()));
789 else if (MO.isBlockAddress()) {
790 MOSymbol = GetBlockAddressSymbol(MO.getBlockAddress());
791 MOSymbol = lookUpOrCreateTOCEntry(MOSymbol);
793 else if (MO.isCPI()) {
794 MOSymbol = GetCPISymbol(MO.getIndex());
795 if (TM.getCodeModel() == CodeModel::Large)
796 MOSymbol = lookUpOrCreateTOCEntry(MOSymbol);
798 else if (MO.isGlobal()) {
799 const GlobalValue *GV = MO.getGlobal();
800 MOSymbol = getSymbol(GV);
801 LLVM_DEBUG(
802 unsigned char GVFlags = Subtarget->classifyGlobalReference(GV);
803 assert((GVFlags & PPCII::MO_NLP_FLAG) &&
804 "LDtocL used on symbol that could be accessed directly is "
805 "invalid. Must match ADDIStocHA8."));
806 MOSymbol = lookUpOrCreateTOCEntry(MOSymbol);
809 const MCExpr *Exp =
810 MCSymbolRefExpr::create(MOSymbol, MCSymbolRefExpr::VK_PPC_TOC_LO,
811 OutContext);
812 TmpInst.getOperand(1) = MCOperand::createExpr(Exp);
813 EmitToStreamer(*OutStreamer, TmpInst);
814 return;
816 case PPC::ADDItocL: {
817 // Transform %xd = ADDItocL %xs, @sym
818 LowerPPCMachineInstrToMCInst(MI, TmpInst, *this, isDarwin);
820 // Change the opcode to ADDI8. If the global address is external, then
821 // generate a TOC entry and reference that. Otherwise reference the
822 // symbol directly.
823 TmpInst.setOpcode(PPC::ADDI8);
824 const MachineOperand &MO = MI->getOperand(2);
825 assert((MO.isGlobal() || MO.isCPI()) && "Invalid operand for ADDItocL");
826 MCSymbol *MOSymbol = nullptr;
828 if (MO.isGlobal()) {
829 const GlobalValue *GV = MO.getGlobal();
830 LLVM_DEBUG(unsigned char GVFlags = Subtarget->classifyGlobalReference(GV);
831 assert(!(GVFlags & PPCII::MO_NLP_FLAG) &&
832 "Interposable definitions must use indirect access."));
833 MOSymbol = getSymbol(GV);
834 } else if (MO.isCPI()) {
835 MOSymbol = GetCPISymbol(MO.getIndex());
838 const MCExpr *Exp =
839 MCSymbolRefExpr::create(MOSymbol, MCSymbolRefExpr::VK_PPC_TOC_LO,
840 OutContext);
841 TmpInst.getOperand(2) = MCOperand::createExpr(Exp);
842 EmitToStreamer(*OutStreamer, TmpInst);
843 return;
845 case PPC::ADDISgotTprelHA: {
846 // Transform: %xd = ADDISgotTprelHA %x2, @sym
847 // Into: %xd = ADDIS8 %x2, sym@got@tlsgd@ha
848 assert(Subtarget->isPPC64() && "Not supported for 32-bit PowerPC");
849 const MachineOperand &MO = MI->getOperand(2);
850 const GlobalValue *GValue = MO.getGlobal();
851 MCSymbol *MOSymbol = getSymbol(GValue);
852 const MCExpr *SymGotTprel =
853 MCSymbolRefExpr::create(MOSymbol, MCSymbolRefExpr::VK_PPC_GOT_TPREL_HA,
854 OutContext);
855 EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::ADDIS8)
856 .addReg(MI->getOperand(0).getReg())
857 .addReg(MI->getOperand(1).getReg())
858 .addExpr(SymGotTprel));
859 return;
861 case PPC::LDgotTprelL:
862 case PPC::LDgotTprelL32: {
863 // Transform %xd = LDgotTprelL @sym, %xs
864 LowerPPCMachineInstrToMCInst(MI, TmpInst, *this, isDarwin);
866 // Change the opcode to LD.
867 TmpInst.setOpcode(Subtarget->isPPC64() ? PPC::LD : PPC::LWZ);
868 const MachineOperand &MO = MI->getOperand(1);
869 const GlobalValue *GValue = MO.getGlobal();
870 MCSymbol *MOSymbol = getSymbol(GValue);
871 const MCExpr *Exp = MCSymbolRefExpr::create(
872 MOSymbol,
873 Subtarget->isPPC64() ? MCSymbolRefExpr::VK_PPC_GOT_TPREL_LO
874 : MCSymbolRefExpr::VK_PPC_GOT_TPREL,
875 OutContext);
876 TmpInst.getOperand(1) = MCOperand::createExpr(Exp);
877 EmitToStreamer(*OutStreamer, TmpInst);
878 return;
881 case PPC::PPC32PICGOT: {
882 MCSymbol *GOTSymbol = OutContext.getOrCreateSymbol(StringRef("_GLOBAL_OFFSET_TABLE_"));
883 MCSymbol *GOTRef = OutContext.createTempSymbol();
884 MCSymbol *NextInstr = OutContext.createTempSymbol();
886 EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::BL)
887 // FIXME: We would like an efficient form for this, so we don't have to do
888 // a lot of extra uniquing.
889 .addExpr(MCSymbolRefExpr::create(NextInstr, OutContext)));
890 const MCExpr *OffsExpr =
891 MCBinaryExpr::createSub(MCSymbolRefExpr::create(GOTSymbol, OutContext),
892 MCSymbolRefExpr::create(GOTRef, OutContext),
893 OutContext);
894 OutStreamer->EmitLabel(GOTRef);
895 OutStreamer->EmitValue(OffsExpr, 4);
896 OutStreamer->EmitLabel(NextInstr);
897 EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::MFLR)
898 .addReg(MI->getOperand(0).getReg()));
899 EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::LWZ)
900 .addReg(MI->getOperand(1).getReg())
901 .addImm(0)
902 .addReg(MI->getOperand(0).getReg()));
903 EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::ADD4)
904 .addReg(MI->getOperand(0).getReg())
905 .addReg(MI->getOperand(1).getReg())
906 .addReg(MI->getOperand(0).getReg()));
907 return;
909 case PPC::PPC32GOT: {
910 MCSymbol *GOTSymbol =
911 OutContext.getOrCreateSymbol(StringRef("_GLOBAL_OFFSET_TABLE_"));
912 const MCExpr *SymGotTlsL = MCSymbolRefExpr::create(
913 GOTSymbol, MCSymbolRefExpr::VK_PPC_LO, OutContext);
914 const MCExpr *SymGotTlsHA = MCSymbolRefExpr::create(
915 GOTSymbol, MCSymbolRefExpr::VK_PPC_HA, OutContext);
916 EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::LI)
917 .addReg(MI->getOperand(0).getReg())
918 .addExpr(SymGotTlsL));
919 EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::ADDIS)
920 .addReg(MI->getOperand(0).getReg())
921 .addReg(MI->getOperand(0).getReg())
922 .addExpr(SymGotTlsHA));
923 return;
925 case PPC::ADDIStlsgdHA: {
926 // Transform: %xd = ADDIStlsgdHA %x2, @sym
927 // Into: %xd = ADDIS8 %x2, sym@got@tlsgd@ha
928 assert(Subtarget->isPPC64() && "Not supported for 32-bit PowerPC");
929 const MachineOperand &MO = MI->getOperand(2);
930 const GlobalValue *GValue = MO.getGlobal();
931 MCSymbol *MOSymbol = getSymbol(GValue);
932 const MCExpr *SymGotTlsGD =
933 MCSymbolRefExpr::create(MOSymbol, MCSymbolRefExpr::VK_PPC_GOT_TLSGD_HA,
934 OutContext);
935 EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::ADDIS8)
936 .addReg(MI->getOperand(0).getReg())
937 .addReg(MI->getOperand(1).getReg())
938 .addExpr(SymGotTlsGD));
939 return;
941 case PPC::ADDItlsgdL:
942 // Transform: %xd = ADDItlsgdL %xs, @sym
943 // Into: %xd = ADDI8 %xs, sym@got@tlsgd@l
944 case PPC::ADDItlsgdL32: {
945 // Transform: %rd = ADDItlsgdL32 %rs, @sym
946 // Into: %rd = ADDI %rs, sym@got@tlsgd
947 const MachineOperand &MO = MI->getOperand(2);
948 const GlobalValue *GValue = MO.getGlobal();
949 MCSymbol *MOSymbol = getSymbol(GValue);
950 const MCExpr *SymGotTlsGD = MCSymbolRefExpr::create(
951 MOSymbol, Subtarget->isPPC64() ? MCSymbolRefExpr::VK_PPC_GOT_TLSGD_LO
952 : MCSymbolRefExpr::VK_PPC_GOT_TLSGD,
953 OutContext);
954 EmitToStreamer(*OutStreamer,
955 MCInstBuilder(Subtarget->isPPC64() ? PPC::ADDI8 : PPC::ADDI)
956 .addReg(MI->getOperand(0).getReg())
957 .addReg(MI->getOperand(1).getReg())
958 .addExpr(SymGotTlsGD));
959 return;
961 case PPC::GETtlsADDR:
962 // Transform: %x3 = GETtlsADDR %x3, @sym
963 // Into: BL8_NOP_TLS __tls_get_addr(sym at tlsgd)
964 case PPC::GETtlsADDR32: {
965 // Transform: %r3 = GETtlsADDR32 %r3, @sym
966 // Into: BL_TLS __tls_get_addr(sym at tlsgd)@PLT
967 EmitTlsCall(MI, MCSymbolRefExpr::VK_PPC_TLSGD);
968 return;
970 case PPC::ADDIStlsldHA: {
971 // Transform: %xd = ADDIStlsldHA %x2, @sym
972 // Into: %xd = ADDIS8 %x2, sym@got@tlsld@ha
973 assert(Subtarget->isPPC64() && "Not supported for 32-bit PowerPC");
974 const MachineOperand &MO = MI->getOperand(2);
975 const GlobalValue *GValue = MO.getGlobal();
976 MCSymbol *MOSymbol = getSymbol(GValue);
977 const MCExpr *SymGotTlsLD =
978 MCSymbolRefExpr::create(MOSymbol, MCSymbolRefExpr::VK_PPC_GOT_TLSLD_HA,
979 OutContext);
980 EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::ADDIS8)
981 .addReg(MI->getOperand(0).getReg())
982 .addReg(MI->getOperand(1).getReg())
983 .addExpr(SymGotTlsLD));
984 return;
986 case PPC::ADDItlsldL:
987 // Transform: %xd = ADDItlsldL %xs, @sym
988 // Into: %xd = ADDI8 %xs, sym@got@tlsld@l
989 case PPC::ADDItlsldL32: {
990 // Transform: %rd = ADDItlsldL32 %rs, @sym
991 // Into: %rd = ADDI %rs, sym@got@tlsld
992 const MachineOperand &MO = MI->getOperand(2);
993 const GlobalValue *GValue = MO.getGlobal();
994 MCSymbol *MOSymbol = getSymbol(GValue);
995 const MCExpr *SymGotTlsLD = MCSymbolRefExpr::create(
996 MOSymbol, Subtarget->isPPC64() ? MCSymbolRefExpr::VK_PPC_GOT_TLSLD_LO
997 : MCSymbolRefExpr::VK_PPC_GOT_TLSLD,
998 OutContext);
999 EmitToStreamer(*OutStreamer,
1000 MCInstBuilder(Subtarget->isPPC64() ? PPC::ADDI8 : PPC::ADDI)
1001 .addReg(MI->getOperand(0).getReg())
1002 .addReg(MI->getOperand(1).getReg())
1003 .addExpr(SymGotTlsLD));
1004 return;
1006 case PPC::GETtlsldADDR:
1007 // Transform: %x3 = GETtlsldADDR %x3, @sym
1008 // Into: BL8_NOP_TLS __tls_get_addr(sym at tlsld)
1009 case PPC::GETtlsldADDR32: {
1010 // Transform: %r3 = GETtlsldADDR32 %r3, @sym
1011 // Into: BL_TLS __tls_get_addr(sym at tlsld)@PLT
1012 EmitTlsCall(MI, MCSymbolRefExpr::VK_PPC_TLSLD);
1013 return;
1015 case PPC::ADDISdtprelHA:
1016 // Transform: %xd = ADDISdtprelHA %xs, @sym
1017 // Into: %xd = ADDIS8 %xs, sym@dtprel@ha
1018 case PPC::ADDISdtprelHA32: {
1019 // Transform: %rd = ADDISdtprelHA32 %rs, @sym
1020 // Into: %rd = ADDIS %rs, sym@dtprel@ha
1021 const MachineOperand &MO = MI->getOperand(2);
1022 const GlobalValue *GValue = MO.getGlobal();
1023 MCSymbol *MOSymbol = getSymbol(GValue);
1024 const MCExpr *SymDtprel =
1025 MCSymbolRefExpr::create(MOSymbol, MCSymbolRefExpr::VK_PPC_DTPREL_HA,
1026 OutContext);
1027 EmitToStreamer(
1028 *OutStreamer,
1029 MCInstBuilder(Subtarget->isPPC64() ? PPC::ADDIS8 : PPC::ADDIS)
1030 .addReg(MI->getOperand(0).getReg())
1031 .addReg(MI->getOperand(1).getReg())
1032 .addExpr(SymDtprel));
1033 return;
1035 case PPC::ADDIdtprelL:
1036 // Transform: %xd = ADDIdtprelL %xs, @sym
1037 // Into: %xd = ADDI8 %xs, sym@dtprel@l
1038 case PPC::ADDIdtprelL32: {
1039 // Transform: %rd = ADDIdtprelL32 %rs, @sym
1040 // Into: %rd = ADDI %rs, sym@dtprel@l
1041 const MachineOperand &MO = MI->getOperand(2);
1042 const GlobalValue *GValue = MO.getGlobal();
1043 MCSymbol *MOSymbol = getSymbol(GValue);
1044 const MCExpr *SymDtprel =
1045 MCSymbolRefExpr::create(MOSymbol, MCSymbolRefExpr::VK_PPC_DTPREL_LO,
1046 OutContext);
1047 EmitToStreamer(*OutStreamer,
1048 MCInstBuilder(Subtarget->isPPC64() ? PPC::ADDI8 : PPC::ADDI)
1049 .addReg(MI->getOperand(0).getReg())
1050 .addReg(MI->getOperand(1).getReg())
1051 .addExpr(SymDtprel));
1052 return;
1054 case PPC::MFOCRF:
1055 case PPC::MFOCRF8:
1056 if (!Subtarget->hasMFOCRF()) {
1057 // Transform: %r3 = MFOCRF %cr7
1058 // Into: %r3 = MFCR ;; cr7
1059 unsigned NewOpcode =
1060 MI->getOpcode() == PPC::MFOCRF ? PPC::MFCR : PPC::MFCR8;
1061 OutStreamer->AddComment(PPCInstPrinter::
1062 getRegisterName(MI->getOperand(1).getReg()));
1063 EmitToStreamer(*OutStreamer, MCInstBuilder(NewOpcode)
1064 .addReg(MI->getOperand(0).getReg()));
1065 return;
1067 break;
1068 case PPC::MTOCRF:
1069 case PPC::MTOCRF8:
1070 if (!Subtarget->hasMFOCRF()) {
1071 // Transform: %cr7 = MTOCRF %r3
1072 // Into: MTCRF mask, %r3 ;; cr7
1073 unsigned NewOpcode =
1074 MI->getOpcode() == PPC::MTOCRF ? PPC::MTCRF : PPC::MTCRF8;
1075 unsigned Mask = 0x80 >> OutContext.getRegisterInfo()
1076 ->getEncodingValue(MI->getOperand(0).getReg());
1077 OutStreamer->AddComment(PPCInstPrinter::
1078 getRegisterName(MI->getOperand(0).getReg()));
1079 EmitToStreamer(*OutStreamer, MCInstBuilder(NewOpcode)
1080 .addImm(Mask)
1081 .addReg(MI->getOperand(1).getReg()));
1082 return;
1084 break;
1085 case PPC::LD:
1086 case PPC::STD:
1087 case PPC::LWA_32:
1088 case PPC::LWA: {
1089 // Verify alignment is legal, so we don't create relocations
1090 // that can't be supported.
1091 // FIXME: This test is currently disabled for Darwin. The test
1092 // suite shows a handful of test cases that fail this check for
1093 // Darwin. Those need to be investigated before this sanity test
1094 // can be enabled for those subtargets.
1095 if (!Subtarget->isDarwin()) {
1096 unsigned OpNum = (MI->getOpcode() == PPC::STD) ? 2 : 1;
1097 const MachineOperand &MO = MI->getOperand(OpNum);
1098 if (MO.isGlobal() && MO.getGlobal()->getAlignment() < 4)
1099 llvm_unreachable("Global must be word-aligned for LD, STD, LWA!");
1101 // Now process the instruction normally.
1102 break;
1106 LowerPPCMachineInstrToMCInst(MI, TmpInst, *this, isDarwin);
1107 EmitToStreamer(*OutStreamer, TmpInst);
1110 void PPCLinuxAsmPrinter::EmitInstruction(const MachineInstr *MI) {
1111 if (!Subtarget->isPPC64())
1112 return PPCAsmPrinter::EmitInstruction(MI);
1114 switch (MI->getOpcode()) {
1115 default:
1116 return PPCAsmPrinter::EmitInstruction(MI);
1117 case TargetOpcode::PATCHABLE_FUNCTION_ENTER: {
1118 // .begin:
1119 // b .end # lis 0, FuncId[16..32]
1120 // nop # li 0, FuncId[0..15]
1121 // std 0, -8(1)
1122 // mflr 0
1123 // bl __xray_FunctionEntry
1124 // mtlr 0
1125 // .end:
1127 // Update compiler-rt/lib/xray/xray_powerpc64.cc accordingly when number
1128 // of instructions change.
1129 MCSymbol *BeginOfSled = OutContext.createTempSymbol();
1130 MCSymbol *EndOfSled = OutContext.createTempSymbol();
1131 OutStreamer->EmitLabel(BeginOfSled);
1132 EmitToStreamer(*OutStreamer,
1133 MCInstBuilder(PPC::B).addExpr(
1134 MCSymbolRefExpr::create(EndOfSled, OutContext)));
1135 EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::NOP));
1136 EmitToStreamer(
1137 *OutStreamer,
1138 MCInstBuilder(PPC::STD).addReg(PPC::X0).addImm(-8).addReg(PPC::X1));
1139 EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::MFLR8).addReg(PPC::X0));
1140 EmitToStreamer(*OutStreamer,
1141 MCInstBuilder(PPC::BL8_NOP)
1142 .addExpr(MCSymbolRefExpr::create(
1143 OutContext.getOrCreateSymbol("__xray_FunctionEntry"),
1144 OutContext)));
1145 EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::MTLR8).addReg(PPC::X0));
1146 OutStreamer->EmitLabel(EndOfSled);
1147 recordSled(BeginOfSled, *MI, SledKind::FUNCTION_ENTER);
1148 break;
1150 case TargetOpcode::PATCHABLE_RET: {
1151 unsigned RetOpcode = MI->getOperand(0).getImm();
1152 MCInst RetInst;
1153 RetInst.setOpcode(RetOpcode);
1154 for (const auto &MO :
1155 make_range(std::next(MI->operands_begin()), MI->operands_end())) {
1156 MCOperand MCOp;
1157 if (LowerPPCMachineOperandToMCOperand(MO, MCOp, *this, false))
1158 RetInst.addOperand(MCOp);
1161 bool IsConditional;
1162 if (RetOpcode == PPC::BCCLR) {
1163 IsConditional = true;
1164 } else if (RetOpcode == PPC::TCRETURNdi8 || RetOpcode == PPC::TCRETURNri8 ||
1165 RetOpcode == PPC::TCRETURNai8) {
1166 break;
1167 } else if (RetOpcode == PPC::BLR8 || RetOpcode == PPC::TAILB8) {
1168 IsConditional = false;
1169 } else {
1170 EmitToStreamer(*OutStreamer, RetInst);
1171 break;
1174 MCSymbol *FallthroughLabel;
1175 if (IsConditional) {
1176 // Before:
1177 // bgtlr cr0
1179 // After:
1180 // ble cr0, .end
1181 // .p2align 3
1182 // .begin:
1183 // blr # lis 0, FuncId[16..32]
1184 // nop # li 0, FuncId[0..15]
1185 // std 0, -8(1)
1186 // mflr 0
1187 // bl __xray_FunctionExit
1188 // mtlr 0
1189 // blr
1190 // .end:
1192 // Update compiler-rt/lib/xray/xray_powerpc64.cc accordingly when number
1193 // of instructions change.
1194 FallthroughLabel = OutContext.createTempSymbol();
1195 EmitToStreamer(
1196 *OutStreamer,
1197 MCInstBuilder(PPC::BCC)
1198 .addImm(PPC::InvertPredicate(
1199 static_cast<PPC::Predicate>(MI->getOperand(1).getImm())))
1200 .addReg(MI->getOperand(2).getReg())
1201 .addExpr(MCSymbolRefExpr::create(FallthroughLabel, OutContext)));
1202 RetInst = MCInst();
1203 RetInst.setOpcode(PPC::BLR8);
1205 // .p2align 3
1206 // .begin:
1207 // b(lr)? # lis 0, FuncId[16..32]
1208 // nop # li 0, FuncId[0..15]
1209 // std 0, -8(1)
1210 // mflr 0
1211 // bl __xray_FunctionExit
1212 // mtlr 0
1213 // b(lr)?
1215 // Update compiler-rt/lib/xray/xray_powerpc64.cc accordingly when number
1216 // of instructions change.
1217 OutStreamer->EmitCodeAlignment(8);
1218 MCSymbol *BeginOfSled = OutContext.createTempSymbol();
1219 OutStreamer->EmitLabel(BeginOfSled);
1220 EmitToStreamer(*OutStreamer, RetInst);
1221 EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::NOP));
1222 EmitToStreamer(
1223 *OutStreamer,
1224 MCInstBuilder(PPC::STD).addReg(PPC::X0).addImm(-8).addReg(PPC::X1));
1225 EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::MFLR8).addReg(PPC::X0));
1226 EmitToStreamer(*OutStreamer,
1227 MCInstBuilder(PPC::BL8_NOP)
1228 .addExpr(MCSymbolRefExpr::create(
1229 OutContext.getOrCreateSymbol("__xray_FunctionExit"),
1230 OutContext)));
1231 EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::MTLR8).addReg(PPC::X0));
1232 EmitToStreamer(*OutStreamer, RetInst);
1233 if (IsConditional)
1234 OutStreamer->EmitLabel(FallthroughLabel);
1235 recordSled(BeginOfSled, *MI, SledKind::FUNCTION_EXIT);
1236 break;
1238 case TargetOpcode::PATCHABLE_FUNCTION_EXIT:
1239 llvm_unreachable("PATCHABLE_FUNCTION_EXIT should never be emitted");
1240 case TargetOpcode::PATCHABLE_TAIL_CALL:
1241 // TODO: Define a trampoline `__xray_FunctionTailExit` and differentiate a
1242 // normal function exit from a tail exit.
1243 llvm_unreachable("Tail call is handled in the normal case. See comments "
1244 "around this assert.");
1248 void PPCLinuxAsmPrinter::EmitStartOfAsmFile(Module &M) {
1249 if (static_cast<const PPCTargetMachine &>(TM).isELFv2ABI()) {
1250 PPCTargetStreamer *TS =
1251 static_cast<PPCTargetStreamer *>(OutStreamer->getTargetStreamer());
1253 if (TS)
1254 TS->emitAbiVersion(2);
1257 if (static_cast<const PPCTargetMachine &>(TM).isPPC64() ||
1258 !isPositionIndependent())
1259 return AsmPrinter::EmitStartOfAsmFile(M);
1261 if (M.getPICLevel() == PICLevel::SmallPIC)
1262 return AsmPrinter::EmitStartOfAsmFile(M);
1264 OutStreamer->SwitchSection(OutContext.getELFSection(
1265 ".got2", ELF::SHT_PROGBITS, ELF::SHF_WRITE | ELF::SHF_ALLOC));
1267 MCSymbol *TOCSym = OutContext.getOrCreateSymbol(Twine(".LTOC"));
1268 MCSymbol *CurrentPos = OutContext.createTempSymbol();
1270 OutStreamer->EmitLabel(CurrentPos);
1272 // The GOT pointer points to the middle of the GOT, in order to reference the
1273 // entire 64kB range. 0x8000 is the midpoint.
1274 const MCExpr *tocExpr =
1275 MCBinaryExpr::createAdd(MCSymbolRefExpr::create(CurrentPos, OutContext),
1276 MCConstantExpr::create(0x8000, OutContext),
1277 OutContext);
1279 OutStreamer->EmitAssignment(TOCSym, tocExpr);
1281 OutStreamer->SwitchSection(getObjFileLowering().getTextSection());
1284 void PPCLinuxAsmPrinter::EmitFunctionEntryLabel() {
1285 // linux/ppc32 - Normal entry label.
1286 if (!Subtarget->isPPC64() &&
1287 (!isPositionIndependent() ||
1288 MF->getFunction().getParent()->getPICLevel() == PICLevel::SmallPIC))
1289 return AsmPrinter::EmitFunctionEntryLabel();
1291 if (!Subtarget->isPPC64()) {
1292 const PPCFunctionInfo *PPCFI = MF->getInfo<PPCFunctionInfo>();
1293 if (PPCFI->usesPICBase() && !Subtarget->isSecurePlt()) {
1294 MCSymbol *RelocSymbol = PPCFI->getPICOffsetSymbol();
1295 MCSymbol *PICBase = MF->getPICBaseSymbol();
1296 OutStreamer->EmitLabel(RelocSymbol);
1298 const MCExpr *OffsExpr =
1299 MCBinaryExpr::createSub(
1300 MCSymbolRefExpr::create(OutContext.getOrCreateSymbol(Twine(".LTOC")),
1301 OutContext),
1302 MCSymbolRefExpr::create(PICBase, OutContext),
1303 OutContext);
1304 OutStreamer->EmitValue(OffsExpr, 4);
1305 OutStreamer->EmitLabel(CurrentFnSym);
1306 return;
1307 } else
1308 return AsmPrinter::EmitFunctionEntryLabel();
1311 // ELFv2 ABI - Normal entry label.
1312 if (Subtarget->isELFv2ABI()) {
1313 // In the Large code model, we allow arbitrary displacements between
1314 // the text section and its associated TOC section. We place the
1315 // full 8-byte offset to the TOC in memory immediately preceding
1316 // the function global entry point.
1317 if (TM.getCodeModel() == CodeModel::Large
1318 && !MF->getRegInfo().use_empty(PPC::X2)) {
1319 const PPCFunctionInfo *PPCFI = MF->getInfo<PPCFunctionInfo>();
1321 MCSymbol *TOCSymbol = OutContext.getOrCreateSymbol(StringRef(".TOC."));
1322 MCSymbol *GlobalEPSymbol = PPCFI->getGlobalEPSymbol();
1323 const MCExpr *TOCDeltaExpr =
1324 MCBinaryExpr::createSub(MCSymbolRefExpr::create(TOCSymbol, OutContext),
1325 MCSymbolRefExpr::create(GlobalEPSymbol,
1326 OutContext),
1327 OutContext);
1329 OutStreamer->EmitLabel(PPCFI->getTOCOffsetSymbol());
1330 OutStreamer->EmitValue(TOCDeltaExpr, 8);
1332 return AsmPrinter::EmitFunctionEntryLabel();
1335 // Emit an official procedure descriptor.
1336 MCSectionSubPair Current = OutStreamer->getCurrentSection();
1337 MCSectionELF *Section = OutStreamer->getContext().getELFSection(
1338 ".opd", ELF::SHT_PROGBITS, ELF::SHF_WRITE | ELF::SHF_ALLOC);
1339 OutStreamer->SwitchSection(Section);
1340 OutStreamer->EmitLabel(CurrentFnSym);
1341 OutStreamer->EmitValueToAlignment(8);
1342 MCSymbol *Symbol1 = CurrentFnSymForSize;
1343 // Generates a R_PPC64_ADDR64 (from FK_DATA_8) relocation for the function
1344 // entry point.
1345 OutStreamer->EmitValue(MCSymbolRefExpr::create(Symbol1, OutContext),
1346 8 /*size*/);
1347 MCSymbol *Symbol2 = OutContext.getOrCreateSymbol(StringRef(".TOC."));
1348 // Generates a R_PPC64_TOC relocation for TOC base insertion.
1349 OutStreamer->EmitValue(
1350 MCSymbolRefExpr::create(Symbol2, MCSymbolRefExpr::VK_PPC_TOCBASE, OutContext),
1351 8/*size*/);
1352 // Emit a null environment pointer.
1353 OutStreamer->EmitIntValue(0, 8 /* size */);
1354 OutStreamer->SwitchSection(Current.first, Current.second);
1357 bool PPCLinuxAsmPrinter::doFinalization(Module &M) {
1358 const DataLayout &DL = getDataLayout();
1360 bool isPPC64 = DL.getPointerSizeInBits() == 64;
1362 PPCTargetStreamer &TS =
1363 static_cast<PPCTargetStreamer &>(*OutStreamer->getTargetStreamer());
1365 if (!TOC.empty()) {
1366 MCSectionELF *Section;
1368 if (isPPC64)
1369 Section = OutStreamer->getContext().getELFSection(
1370 ".toc", ELF::SHT_PROGBITS, ELF::SHF_WRITE | ELF::SHF_ALLOC);
1371 else
1372 Section = OutStreamer->getContext().getELFSection(
1373 ".got2", ELF::SHT_PROGBITS, ELF::SHF_WRITE | ELF::SHF_ALLOC);
1374 OutStreamer->SwitchSection(Section);
1376 for (MapVector<MCSymbol*, MCSymbol*>::iterator I = TOC.begin(),
1377 E = TOC.end(); I != E; ++I) {
1378 OutStreamer->EmitLabel(I->second);
1379 MCSymbol *S = I->first;
1380 if (isPPC64) {
1381 TS.emitTCEntry(*S);
1382 } else {
1383 OutStreamer->EmitValueToAlignment(4);
1384 OutStreamer->EmitSymbolValue(S, 4);
1389 return AsmPrinter::doFinalization(M);
1392 /// EmitFunctionBodyStart - Emit a global entry point prefix for ELFv2.
1393 void PPCLinuxAsmPrinter::EmitFunctionBodyStart() {
1394 // In the ELFv2 ABI, in functions that use the TOC register, we need to
1395 // provide two entry points. The ABI guarantees that when calling the
1396 // local entry point, r2 is set up by the caller to contain the TOC base
1397 // for this function, and when calling the global entry point, r12 is set
1398 // up by the caller to hold the address of the global entry point. We
1399 // thus emit a prefix sequence along the following lines:
1401 // func:
1402 // .Lfunc_gepNN:
1403 // # global entry point
1404 // addis r2,r12,(.TOC.-.Lfunc_gepNN)@ha
1405 // addi r2,r2,(.TOC.-.Lfunc_gepNN)@l
1406 // .Lfunc_lepNN:
1407 // .localentry func, .Lfunc_lepNN-.Lfunc_gepNN
1408 // # local entry point, followed by function body
1410 // For the Large code model, we create
1412 // .Lfunc_tocNN:
1413 // .quad .TOC.-.Lfunc_gepNN # done by EmitFunctionEntryLabel
1414 // func:
1415 // .Lfunc_gepNN:
1416 // # global entry point
1417 // ld r2,.Lfunc_tocNN-.Lfunc_gepNN(r12)
1418 // add r2,r2,r12
1419 // .Lfunc_lepNN:
1420 // .localentry func, .Lfunc_lepNN-.Lfunc_gepNN
1421 // # local entry point, followed by function body
1423 // This ensures we have r2 set up correctly while executing the function
1424 // body, no matter which entry point is called.
1425 if (Subtarget->isELFv2ABI()
1426 // Only do all that if the function uses r2 in the first place.
1427 && !MF->getRegInfo().use_empty(PPC::X2)) {
1428 // Note: The logic here must be synchronized with the code in the
1429 // branch-selection pass which sets the offset of the first block in the
1430 // function. This matters because it affects the alignment.
1431 const PPCFunctionInfo *PPCFI = MF->getInfo<PPCFunctionInfo>();
1433 MCSymbol *GlobalEntryLabel = PPCFI->getGlobalEPSymbol();
1434 OutStreamer->EmitLabel(GlobalEntryLabel);
1435 const MCSymbolRefExpr *GlobalEntryLabelExp =
1436 MCSymbolRefExpr::create(GlobalEntryLabel, OutContext);
1438 if (TM.getCodeModel() != CodeModel::Large) {
1439 MCSymbol *TOCSymbol = OutContext.getOrCreateSymbol(StringRef(".TOC."));
1440 const MCExpr *TOCDeltaExpr =
1441 MCBinaryExpr::createSub(MCSymbolRefExpr::create(TOCSymbol, OutContext),
1442 GlobalEntryLabelExp, OutContext);
1444 const MCExpr *TOCDeltaHi =
1445 PPCMCExpr::createHa(TOCDeltaExpr, false, OutContext);
1446 EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::ADDIS)
1447 .addReg(PPC::X2)
1448 .addReg(PPC::X12)
1449 .addExpr(TOCDeltaHi));
1451 const MCExpr *TOCDeltaLo =
1452 PPCMCExpr::createLo(TOCDeltaExpr, false, OutContext);
1453 EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::ADDI)
1454 .addReg(PPC::X2)
1455 .addReg(PPC::X2)
1456 .addExpr(TOCDeltaLo));
1457 } else {
1458 MCSymbol *TOCOffset = PPCFI->getTOCOffsetSymbol();
1459 const MCExpr *TOCOffsetDeltaExpr =
1460 MCBinaryExpr::createSub(MCSymbolRefExpr::create(TOCOffset, OutContext),
1461 GlobalEntryLabelExp, OutContext);
1463 EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::LD)
1464 .addReg(PPC::X2)
1465 .addExpr(TOCOffsetDeltaExpr)
1466 .addReg(PPC::X12));
1467 EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::ADD8)
1468 .addReg(PPC::X2)
1469 .addReg(PPC::X2)
1470 .addReg(PPC::X12));
1473 MCSymbol *LocalEntryLabel = PPCFI->getLocalEPSymbol();
1474 OutStreamer->EmitLabel(LocalEntryLabel);
1475 const MCSymbolRefExpr *LocalEntryLabelExp =
1476 MCSymbolRefExpr::create(LocalEntryLabel, OutContext);
1477 const MCExpr *LocalOffsetExp =
1478 MCBinaryExpr::createSub(LocalEntryLabelExp,
1479 GlobalEntryLabelExp, OutContext);
1481 PPCTargetStreamer *TS =
1482 static_cast<PPCTargetStreamer *>(OutStreamer->getTargetStreamer());
1484 if (TS)
1485 TS->emitLocalEntry(cast<MCSymbolELF>(CurrentFnSym), LocalOffsetExp);
1489 /// EmitFunctionBodyEnd - Print the traceback table before the .size
1490 /// directive.
1492 void PPCLinuxAsmPrinter::EmitFunctionBodyEnd() {
1493 // Only the 64-bit target requires a traceback table. For now,
1494 // we only emit the word of zeroes that GDB requires to find
1495 // the end of the function, and zeroes for the eight-byte
1496 // mandatory fields.
1497 // FIXME: We should fill in the eight-byte mandatory fields as described in
1498 // the PPC64 ELF ABI (this is a low-priority item because GDB does not
1499 // currently make use of these fields).
1500 if (Subtarget->isPPC64()) {
1501 OutStreamer->EmitIntValue(0, 4/*size*/);
1502 OutStreamer->EmitIntValue(0, 8/*size*/);
1506 void PPCDarwinAsmPrinter::EmitStartOfAsmFile(Module &M) {
1507 static const char *const CPUDirectives[] = {
1509 "ppc",
1510 "ppc440",
1511 "ppc601",
1512 "ppc602",
1513 "ppc603",
1514 "ppc7400",
1515 "ppc750",
1516 "ppc970",
1517 "ppcA2",
1518 "ppce500",
1519 "ppce500mc",
1520 "ppce5500",
1521 "power3",
1522 "power4",
1523 "power5",
1524 "power5x",
1525 "power6",
1526 "power6x",
1527 "power7",
1528 // FIXME: why is power8 missing here?
1529 "ppc64",
1530 "ppc64le",
1531 "power9"
1534 // Get the numerically largest directive.
1535 // FIXME: How should we merge darwin directives?
1536 unsigned Directive = PPC::DIR_NONE;
1537 for (const Function &F : M) {
1538 const PPCSubtarget &STI = TM.getSubtarget<PPCSubtarget>(F);
1539 unsigned FDir = STI.getDarwinDirective();
1540 Directive = Directive > FDir ? FDir : STI.getDarwinDirective();
1541 if (STI.hasMFOCRF() && Directive < PPC::DIR_970)
1542 Directive = PPC::DIR_970;
1543 if (STI.hasAltivec() && Directive < PPC::DIR_7400)
1544 Directive = PPC::DIR_7400;
1545 if (STI.isPPC64() && Directive < PPC::DIR_64)
1546 Directive = PPC::DIR_64;
1549 assert(Directive <= PPC::DIR_64 && "Directive out of range.");
1551 assert(Directive < array_lengthof(CPUDirectives) &&
1552 "CPUDirectives[] might not be up-to-date!");
1553 PPCTargetStreamer &TStreamer =
1554 *static_cast<PPCTargetStreamer *>(OutStreamer->getTargetStreamer());
1555 TStreamer.emitMachine(CPUDirectives[Directive]);
1557 // Prime text sections so they are adjacent. This reduces the likelihood a
1558 // large data or debug section causes a branch to exceed 16M limit.
1559 const TargetLoweringObjectFileMachO &TLOFMacho =
1560 static_cast<const TargetLoweringObjectFileMachO &>(getObjFileLowering());
1561 OutStreamer->SwitchSection(TLOFMacho.getTextCoalSection());
1562 if (TM.getRelocationModel() == Reloc::PIC_) {
1563 OutStreamer->SwitchSection(
1564 OutContext.getMachOSection("__TEXT", "__picsymbolstub1",
1565 MachO::S_SYMBOL_STUBS |
1566 MachO::S_ATTR_PURE_INSTRUCTIONS,
1567 32, SectionKind::getText()));
1568 } else if (TM.getRelocationModel() == Reloc::DynamicNoPIC) {
1569 OutStreamer->SwitchSection(
1570 OutContext.getMachOSection("__TEXT","__symbol_stub1",
1571 MachO::S_SYMBOL_STUBS |
1572 MachO::S_ATTR_PURE_INSTRUCTIONS,
1573 16, SectionKind::getText()));
1575 OutStreamer->SwitchSection(getObjFileLowering().getTextSection());
1578 bool PPCDarwinAsmPrinter::doFinalization(Module &M) {
1579 bool isPPC64 = getDataLayout().getPointerSizeInBits() == 64;
1581 // Darwin/PPC always uses mach-o.
1582 const TargetLoweringObjectFileMachO &TLOFMacho =
1583 static_cast<const TargetLoweringObjectFileMachO &>(getObjFileLowering());
1584 if (MMI) {
1585 MachineModuleInfoMachO &MMIMacho =
1586 MMI->getObjFileInfo<MachineModuleInfoMachO>();
1588 if (MAI->doesSupportExceptionHandling()) {
1589 // Add the (possibly multiple) personalities to the set of global values.
1590 // Only referenced functions get into the Personalities list.
1591 for (const Function *Personality : MMI->getPersonalities()) {
1592 if (Personality) {
1593 MCSymbol *NLPSym =
1594 getSymbolWithGlobalValueBase(Personality, "$non_lazy_ptr");
1595 MachineModuleInfoImpl::StubValueTy &StubSym =
1596 MMIMacho.getGVStubEntry(NLPSym);
1597 StubSym =
1598 MachineModuleInfoImpl::StubValueTy(getSymbol(Personality), true);
1603 // Output stubs for dynamically-linked functions.
1604 MachineModuleInfoMachO::SymbolListTy Stubs = MMIMacho.GetGVStubList();
1606 // Output macho stubs for external and common global variables.
1607 if (!Stubs.empty()) {
1608 // Switch with ".non_lazy_symbol_pointer" directive.
1609 OutStreamer->SwitchSection(TLOFMacho.getNonLazySymbolPointerSection());
1610 EmitAlignment(isPPC64 ? 3 : 2);
1612 for (unsigned i = 0, e = Stubs.size(); i != e; ++i) {
1613 // L_foo$stub:
1614 OutStreamer->EmitLabel(Stubs[i].first);
1615 // .indirect_symbol _foo
1616 MachineModuleInfoImpl::StubValueTy &MCSym = Stubs[i].second;
1617 OutStreamer->EmitSymbolAttribute(MCSym.getPointer(),
1618 MCSA_IndirectSymbol);
1620 if (MCSym.getInt())
1621 // External to current translation unit.
1622 OutStreamer->EmitIntValue(0, isPPC64 ? 8 : 4 /*size*/);
1623 else
1624 // Internal to current translation unit.
1626 // When we place the LSDA into the TEXT section, the type info
1627 // pointers
1628 // need to be indirect and pc-rel. We accomplish this by using NLPs.
1629 // However, sometimes the types are local to the file. So we need to
1630 // fill in the value for the NLP in those cases.
1631 OutStreamer->EmitValue(
1632 MCSymbolRefExpr::create(MCSym.getPointer(), OutContext),
1633 isPPC64 ? 8 : 4 /*size*/);
1636 Stubs.clear();
1637 OutStreamer->AddBlankLine();
1641 // Funny Darwin hack: This flag tells the linker that no global symbols
1642 // contain code that falls through to other global symbols (e.g. the obvious
1643 // implementation of multiple entry points). If this doesn't occur, the
1644 // linker can safely perform dead code stripping. Since LLVM never generates
1645 // code that does this, it is always safe to set.
1646 OutStreamer->EmitAssemblerFlag(MCAF_SubsectionsViaSymbols);
1648 return AsmPrinter::doFinalization(M);
1651 void PPCAIXAsmPrinter::EmitGlobalVariable(const GlobalVariable *GV) {
1652 // Early error checking limiting what is supported.
1653 if (GV->isThreadLocal())
1654 report_fatal_error("Thread local not yet supported on AIX.");
1656 if (GV->hasSection())
1657 report_fatal_error("Custom section for Data not yet supported.");
1659 if (GV->hasComdat())
1660 report_fatal_error("COMDAT not yet supported by AIX.");
1662 SectionKind GVKind = getObjFileLowering().getKindForGlobal(GV, TM);
1663 if (!GVKind.isCommon() && !GVKind.isBSSLocal() && !GVKind.isData())
1664 report_fatal_error("Encountered a global variable kind that is "
1665 "not supported yet.");
1667 // Create the containing csect and switch to it.
1668 MCSectionXCOFF *CSect = cast<MCSectionXCOFF>(
1669 getObjFileLowering().SectionForGlobal(GV, GVKind, TM));
1670 OutStreamer->SwitchSection(CSect);
1672 // Create the symbol, set its storage class, and emit it.
1673 MCSymbolXCOFF *GVSym = cast<MCSymbolXCOFF>(getSymbol(GV));
1674 GVSym->setStorageClass(
1675 TargetLoweringObjectFileXCOFF::getStorageClassForGlobal(GV));
1676 GVSym->setContainingCsect(CSect);
1678 const DataLayout &DL = GV->getParent()->getDataLayout();
1680 // Handle common symbols.
1681 if (GVKind.isCommon() || GVKind.isBSSLocal()) {
1682 unsigned Align =
1683 GV->getAlignment() ? GV->getAlignment() : DL.getPreferredAlignment(GV);
1684 uint64_t Size = DL.getTypeAllocSize(GV->getType()->getElementType());
1686 if (GVKind.isBSSLocal())
1687 OutStreamer->EmitXCOFFLocalCommonSymbol(GVSym, Size, Align);
1688 else
1689 OutStreamer->EmitCommonSymbol(GVSym, Size, Align);
1690 return;
1693 // Get the alignment in the log2 form.
1694 const unsigned AlignLog = getGVAlignmentLog2(GV, DL);
1696 MCSymbol *EmittedInitSym = GVSym;
1697 EmitLinkage(GV, EmittedInitSym);
1698 EmitAlignment(AlignLog, GV);
1699 OutStreamer->EmitLabel(EmittedInitSym);
1700 EmitGlobalConstant(GV->getParent()->getDataLayout(), GV->getInitializer());
1703 /// createPPCAsmPrinterPass - Returns a pass that prints the PPC assembly code
1704 /// for a MachineFunction to the given output stream, in a format that the
1705 /// Darwin assembler can deal with.
1707 static AsmPrinter *
1708 createPPCAsmPrinterPass(TargetMachine &tm,
1709 std::unique_ptr<MCStreamer> &&Streamer) {
1710 if (tm.getTargetTriple().isMacOSX())
1711 return new PPCDarwinAsmPrinter(tm, std::move(Streamer));
1712 if (tm.getTargetTriple().isOSAIX())
1713 return new PPCAIXAsmPrinter(tm, std::move(Streamer));
1715 return new PPCLinuxAsmPrinter(tm, std::move(Streamer));
1718 // Force static initialization.
1719 extern "C" void LLVMInitializePowerPCAsmPrinter() {
1720 TargetRegistry::RegisterAsmPrinter(getThePPC32Target(),
1721 createPPCAsmPrinterPass);
1722 TargetRegistry::RegisterAsmPrinter(getThePPC64Target(),
1723 createPPCAsmPrinterPass);
1724 TargetRegistry::RegisterAsmPrinter(getThePPC64LETarget(),
1725 createPPCAsmPrinterPass);