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