10b57cec5SDimitry Andric //===-- PPCAsmPrinter.cpp - Print machine instrs to PowerPC assembly ------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // This file contains a printer that converts from our internal representation
100b57cec5SDimitry Andric // of machine-dependent LLVM code to PowerPC assembly language. This printer is
110b57cec5SDimitry Andric // the output mechanism used by `llc'.
120b57cec5SDimitry Andric //
130b57cec5SDimitry Andric // Documentation at http://developer.apple.com/documentation/DeveloperTools/
140b57cec5SDimitry Andric // Reference/Assembler/ASMIntroduction/chapter_1_section_1.html
150b57cec5SDimitry Andric //
160b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
170b57cec5SDimitry Andric
180b57cec5SDimitry Andric #include "MCTargetDesc/PPCInstPrinter.h"
190b57cec5SDimitry Andric #include "MCTargetDesc/PPCMCExpr.h"
200b57cec5SDimitry Andric #include "MCTargetDesc/PPCMCTargetDesc.h"
210b57cec5SDimitry Andric #include "MCTargetDesc/PPCPredicates.h"
220b57cec5SDimitry Andric #include "PPC.h"
230b57cec5SDimitry Andric #include "PPCInstrInfo.h"
240b57cec5SDimitry Andric #include "PPCMachineFunctionInfo.h"
250b57cec5SDimitry Andric #include "PPCSubtarget.h"
260b57cec5SDimitry Andric #include "PPCTargetMachine.h"
270b57cec5SDimitry Andric #include "PPCTargetStreamer.h"
280b57cec5SDimitry Andric #include "TargetInfo/PowerPCTargetInfo.h"
290b57cec5SDimitry Andric #include "llvm/ADT/MapVector.h"
30*0fca6ea1SDimitry Andric #include "llvm/ADT/SetVector.h"
3106c3fb27SDimitry Andric #include "llvm/ADT/Statistic.h"
3206c3fb27SDimitry Andric #include "llvm/ADT/StringExtras.h"
330b57cec5SDimitry Andric #include "llvm/ADT/StringRef.h"
340b57cec5SDimitry Andric #include "llvm/ADT/Twine.h"
350b57cec5SDimitry Andric #include "llvm/BinaryFormat/ELF.h"
360b57cec5SDimitry Andric #include "llvm/CodeGen/AsmPrinter.h"
370b57cec5SDimitry Andric #include "llvm/CodeGen/MachineBasicBlock.h"
3881ad6265SDimitry Andric #include "llvm/CodeGen/MachineFrameInfo.h"
390b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFunction.h"
400b57cec5SDimitry Andric #include "llvm/CodeGen/MachineInstr.h"
410b57cec5SDimitry Andric #include "llvm/CodeGen/MachineModuleInfoImpls.h"
420b57cec5SDimitry Andric #include "llvm/CodeGen/MachineOperand.h"
430b57cec5SDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h"
440b57cec5SDimitry Andric #include "llvm/CodeGen/StackMaps.h"
450b57cec5SDimitry Andric #include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
460b57cec5SDimitry Andric #include "llvm/IR/DataLayout.h"
470b57cec5SDimitry Andric #include "llvm/IR/GlobalValue.h"
48480093f4SDimitry Andric #include "llvm/IR/GlobalVariable.h"
490b57cec5SDimitry Andric #include "llvm/IR/Module.h"
500b57cec5SDimitry Andric #include "llvm/MC/MCAsmInfo.h"
510b57cec5SDimitry Andric #include "llvm/MC/MCContext.h"
52e8d8bef9SDimitry Andric #include "llvm/MC/MCDirectives.h"
530b57cec5SDimitry Andric #include "llvm/MC/MCExpr.h"
540b57cec5SDimitry Andric #include "llvm/MC/MCInst.h"
550b57cec5SDimitry Andric #include "llvm/MC/MCInstBuilder.h"
560b57cec5SDimitry Andric #include "llvm/MC/MCSectionELF.h"
578bcb0991SDimitry Andric #include "llvm/MC/MCSectionXCOFF.h"
580b57cec5SDimitry Andric #include "llvm/MC/MCStreamer.h"
590b57cec5SDimitry Andric #include "llvm/MC/MCSymbol.h"
600b57cec5SDimitry Andric #include "llvm/MC/MCSymbolELF.h"
618bcb0991SDimitry Andric #include "llvm/MC/MCSymbolXCOFF.h"
620b57cec5SDimitry Andric #include "llvm/MC/SectionKind.h"
63349cc55cSDimitry Andric #include "llvm/MC/TargetRegistry.h"
640b57cec5SDimitry Andric #include "llvm/Support/Casting.h"
650b57cec5SDimitry Andric #include "llvm/Support/CodeGen.h"
660b57cec5SDimitry Andric #include "llvm/Support/Debug.h"
67fe6060f1SDimitry Andric #include "llvm/Support/Error.h"
680b57cec5SDimitry Andric #include "llvm/Support/ErrorHandling.h"
69*0fca6ea1SDimitry Andric #include "llvm/Support/MathExtras.h"
70e8d8bef9SDimitry Andric #include "llvm/Support/Process.h"
715f757f3fSDimitry Andric #include "llvm/Support/Threading.h"
72*0fca6ea1SDimitry Andric #include "llvm/Support/raw_ostream.h"
730b57cec5SDimitry Andric #include "llvm/Target/TargetMachine.h"
7406c3fb27SDimitry Andric #include "llvm/TargetParser/Triple.h"
75e8d8bef9SDimitry Andric #include "llvm/Transforms/Utils/ModuleUtils.h"
760b57cec5SDimitry Andric #include <algorithm>
770b57cec5SDimitry Andric #include <cassert>
780b57cec5SDimitry Andric #include <cstdint>
790b57cec5SDimitry Andric #include <memory>
800b57cec5SDimitry Andric #include <new>
810b57cec5SDimitry Andric
820b57cec5SDimitry Andric using namespace llvm;
83e8d8bef9SDimitry Andric using namespace llvm::XCOFF;
840b57cec5SDimitry Andric
850b57cec5SDimitry Andric #define DEBUG_TYPE "asmprinter"
860b57cec5SDimitry Andric
8706c3fb27SDimitry Andric STATISTIC(NumTOCEntries, "Number of Total TOC Entries Emitted.");
8806c3fb27SDimitry Andric STATISTIC(NumTOCConstPool, "Number of Constant Pool TOC Entries.");
8906c3fb27SDimitry Andric STATISTIC(NumTOCGlobalInternal,
9006c3fb27SDimitry Andric "Number of Internal Linkage Global TOC Entries.");
9106c3fb27SDimitry Andric STATISTIC(NumTOCGlobalExternal,
9206c3fb27SDimitry Andric "Number of External Linkage Global TOC Entries.");
9306c3fb27SDimitry Andric STATISTIC(NumTOCJumpTable, "Number of Jump Table TOC Entries.");
9406c3fb27SDimitry Andric STATISTIC(NumTOCThreadLocal, "Number of Thread Local TOC Entries.");
9506c3fb27SDimitry Andric STATISTIC(NumTOCBlockAddress, "Number of Block Address TOC Entries.");
9606c3fb27SDimitry Andric STATISTIC(NumTOCEHBlock, "Number of EH Block TOC Entries.");
9706c3fb27SDimitry Andric
98fe6060f1SDimitry Andric static cl::opt<bool> EnableSSPCanaryBitInTB(
99fe6060f1SDimitry Andric "aix-ssp-tb-bit", cl::init(false),
100fe6060f1SDimitry Andric cl::desc("Enable Passing SSP Canary info in Trackback on AIX"), cl::Hidden);
101fe6060f1SDimitry Andric
102fe6060f1SDimitry Andric // Specialize DenseMapInfo to allow
103fe6060f1SDimitry Andric // std::pair<const MCSymbol *, MCSymbolRefExpr::VariantKind> in DenseMap.
104fe6060f1SDimitry Andric // This specialization is needed here because that type is used as keys in the
105fe6060f1SDimitry Andric // map representing TOC entries.
106fe6060f1SDimitry Andric namespace llvm {
107fe6060f1SDimitry Andric template <>
108fe6060f1SDimitry Andric struct DenseMapInfo<std::pair<const MCSymbol *, MCSymbolRefExpr::VariantKind>> {
109fe6060f1SDimitry Andric using TOCKey = std::pair<const MCSymbol *, MCSymbolRefExpr::VariantKind>;
110fe6060f1SDimitry Andric
getEmptyKeyllvm::DenseMapInfo111fe6060f1SDimitry Andric static inline TOCKey getEmptyKey() {
112fe6060f1SDimitry Andric return {nullptr, MCSymbolRefExpr::VariantKind::VK_None};
113fe6060f1SDimitry Andric }
getTombstoneKeyllvm::DenseMapInfo114fe6060f1SDimitry Andric static inline TOCKey getTombstoneKey() {
115fe6060f1SDimitry Andric return {nullptr, MCSymbolRefExpr::VariantKind::VK_Invalid};
116fe6060f1SDimitry Andric }
getHashValuellvm::DenseMapInfo117fe6060f1SDimitry Andric static unsigned getHashValue(const TOCKey &PairVal) {
118fe6060f1SDimitry Andric return detail::combineHashValue(
119fe6060f1SDimitry Andric DenseMapInfo<const MCSymbol *>::getHashValue(PairVal.first),
120fe6060f1SDimitry Andric DenseMapInfo<int>::getHashValue(PairVal.second));
121fe6060f1SDimitry Andric }
isEqualllvm::DenseMapInfo122fe6060f1SDimitry Andric static bool isEqual(const TOCKey &A, const TOCKey &B) { return A == B; }
123fe6060f1SDimitry Andric };
124fe6060f1SDimitry Andric } // end namespace llvm
125fe6060f1SDimitry Andric
1260b57cec5SDimitry Andric namespace {
1270b57cec5SDimitry Andric
12804eeddc0SDimitry Andric enum {
12904eeddc0SDimitry Andric // GNU attribute tags for PowerPC ABI
13004eeddc0SDimitry Andric Tag_GNU_Power_ABI_FP = 4,
13104eeddc0SDimitry Andric Tag_GNU_Power_ABI_Vector = 8,
13204eeddc0SDimitry Andric Tag_GNU_Power_ABI_Struct_Return = 12,
13304eeddc0SDimitry Andric
13404eeddc0SDimitry Andric // GNU attribute values for PowerPC float ABI, as combination of two parts
13504eeddc0SDimitry Andric Val_GNU_Power_ABI_NoFloat = 0b00,
13604eeddc0SDimitry Andric Val_GNU_Power_ABI_HardFloat_DP = 0b01,
13704eeddc0SDimitry Andric Val_GNU_Power_ABI_SoftFloat_DP = 0b10,
13804eeddc0SDimitry Andric Val_GNU_Power_ABI_HardFloat_SP = 0b11,
13904eeddc0SDimitry Andric
14004eeddc0SDimitry Andric Val_GNU_Power_ABI_LDBL_IBM128 = 0b0100,
14104eeddc0SDimitry Andric Val_GNU_Power_ABI_LDBL_64 = 0b1000,
14204eeddc0SDimitry Andric Val_GNU_Power_ABI_LDBL_IEEE128 = 0b1100,
14304eeddc0SDimitry Andric };
14404eeddc0SDimitry Andric
1450b57cec5SDimitry Andric class PPCAsmPrinter : public AsmPrinter {
1460b57cec5SDimitry Andric protected:
147fe6060f1SDimitry Andric // For TLS on AIX, we need to be able to identify TOC entries of specific
148fe6060f1SDimitry Andric // VariantKind so we can add the right relocations when we generate the
149fe6060f1SDimitry Andric // entries. So each entry is represented by a pair of MCSymbol and
150fe6060f1SDimitry Andric // VariantKind. For example, we need to be able to identify the following
151fe6060f1SDimitry Andric // entry as a TLSGD entry so we can add the @m relocation:
152fe6060f1SDimitry Andric // .tc .i[TC],i[TL]@m
153fe6060f1SDimitry Andric // By default, VK_None is used for the VariantKind.
154fe6060f1SDimitry Andric MapVector<std::pair<const MCSymbol *, MCSymbolRefExpr::VariantKind>,
155fe6060f1SDimitry Andric MCSymbol *>
156fe6060f1SDimitry Andric TOC;
157480093f4SDimitry Andric const PPCSubtarget *Subtarget = nullptr;
1580b57cec5SDimitry Andric
159*0fca6ea1SDimitry Andric // Keep track of the number of TLS variables and their corresponding
160*0fca6ea1SDimitry Andric // addresses, which is then used for the assembly printing of
161*0fca6ea1SDimitry Andric // non-TOC-based local-exec variables.
162*0fca6ea1SDimitry Andric MapVector<const GlobalValue *, uint64_t> TLSVarsToAddressMapping;
163*0fca6ea1SDimitry Andric
1640b57cec5SDimitry Andric public:
PPCAsmPrinter(TargetMachine & TM,std::unique_ptr<MCStreamer> Streamer)1650b57cec5SDimitry Andric explicit PPCAsmPrinter(TargetMachine &TM,
1660b57cec5SDimitry Andric std::unique_ptr<MCStreamer> Streamer)
167bdd1243dSDimitry Andric : AsmPrinter(TM, std::move(Streamer)) {}
1680b57cec5SDimitry Andric
getPassName() const1690b57cec5SDimitry Andric StringRef getPassName() const override { return "PowerPC Assembly Printer"; }
1700b57cec5SDimitry Andric
17106c3fb27SDimitry Andric enum TOCEntryType {
17206c3fb27SDimitry Andric TOCType_ConstantPool,
17306c3fb27SDimitry Andric TOCType_GlobalExternal,
17406c3fb27SDimitry Andric TOCType_GlobalInternal,
17506c3fb27SDimitry Andric TOCType_JumpTable,
17606c3fb27SDimitry Andric TOCType_ThreadLocal,
17706c3fb27SDimitry Andric TOCType_BlockAddress,
17806c3fb27SDimitry Andric TOCType_EHBlock
17906c3fb27SDimitry Andric };
18006c3fb27SDimitry Andric
18106c3fb27SDimitry Andric MCSymbol *lookUpOrCreateTOCEntry(const MCSymbol *Sym, TOCEntryType Type,
182fe6060f1SDimitry Andric MCSymbolRefExpr::VariantKind Kind =
183fe6060f1SDimitry Andric MCSymbolRefExpr::VariantKind::VK_None);
1840b57cec5SDimitry Andric
doInitialization(Module & M)1850b57cec5SDimitry Andric bool doInitialization(Module &M) override {
1860b57cec5SDimitry Andric if (!TOC.empty())
1870b57cec5SDimitry Andric TOC.clear();
1880b57cec5SDimitry Andric return AsmPrinter::doInitialization(M);
1890b57cec5SDimitry Andric }
1900b57cec5SDimitry Andric
1915ffd83dbSDimitry Andric void emitInstruction(const MachineInstr *MI) override;
1920b57cec5SDimitry Andric
1930b57cec5SDimitry Andric /// This function is for PrintAsmOperand and PrintAsmMemoryOperand,
1940b57cec5SDimitry Andric /// invoked by EmitMSInlineAsmStr and EmitGCCInlineAsmStr only.
1950b57cec5SDimitry Andric /// The \p MI would be INLINEASM ONLY.
1960b57cec5SDimitry Andric void printOperand(const MachineInstr *MI, unsigned OpNo, raw_ostream &O);
1970b57cec5SDimitry Andric
1980b57cec5SDimitry Andric void PrintSymbolOperand(const MachineOperand &MO, raw_ostream &O) override;
1990b57cec5SDimitry Andric bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
2000b57cec5SDimitry Andric const char *ExtraCode, raw_ostream &O) override;
2010b57cec5SDimitry Andric bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
2020b57cec5SDimitry Andric const char *ExtraCode, raw_ostream &O) override;
2030b57cec5SDimitry Andric
2040b57cec5SDimitry Andric void LowerSTACKMAP(StackMaps &SM, const MachineInstr &MI);
2050b57cec5SDimitry Andric void LowerPATCHPOINT(StackMaps &SM, const MachineInstr &MI);
2060b57cec5SDimitry Andric void EmitTlsCall(const MachineInstr *MI, MCSymbolRefExpr::VariantKind VK);
20706c3fb27SDimitry Andric void EmitAIXTlsCallHelper(const MachineInstr *MI);
208*0fca6ea1SDimitry Andric const MCExpr *getAdjustedFasterLocalExpr(const MachineOperand &MO,
209*0fca6ea1SDimitry Andric int64_t Offset);
runOnMachineFunction(MachineFunction & MF)2100b57cec5SDimitry Andric bool runOnMachineFunction(MachineFunction &MF) override {
2110b57cec5SDimitry Andric Subtarget = &MF.getSubtarget<PPCSubtarget>();
2120b57cec5SDimitry Andric bool Changed = AsmPrinter::runOnMachineFunction(MF);
2130b57cec5SDimitry Andric emitXRayTable();
2140b57cec5SDimitry Andric return Changed;
2150b57cec5SDimitry Andric }
2160b57cec5SDimitry Andric };
2170b57cec5SDimitry Andric
2180b57cec5SDimitry Andric /// PPCLinuxAsmPrinter - PowerPC assembly printer, customized for Linux
2190b57cec5SDimitry Andric class PPCLinuxAsmPrinter : public PPCAsmPrinter {
2200b57cec5SDimitry Andric public:
PPCLinuxAsmPrinter(TargetMachine & TM,std::unique_ptr<MCStreamer> Streamer)2210b57cec5SDimitry Andric explicit PPCLinuxAsmPrinter(TargetMachine &TM,
2220b57cec5SDimitry Andric std::unique_ptr<MCStreamer> Streamer)
2230b57cec5SDimitry Andric : PPCAsmPrinter(TM, std::move(Streamer)) {}
2240b57cec5SDimitry Andric
getPassName() const2250b57cec5SDimitry Andric StringRef getPassName() const override {
2260b57cec5SDimitry Andric return "Linux PPC Assembly Printer";
2270b57cec5SDimitry Andric }
2280b57cec5SDimitry Andric
22904eeddc0SDimitry Andric void emitGNUAttributes(Module &M);
23004eeddc0SDimitry Andric
2315ffd83dbSDimitry Andric void emitStartOfAsmFile(Module &M) override;
2325ffd83dbSDimitry Andric void emitEndOfAsmFile(Module &) override;
2330b57cec5SDimitry Andric
2345ffd83dbSDimitry Andric void emitFunctionEntryLabel() override;
2350b57cec5SDimitry Andric
2365ffd83dbSDimitry Andric void emitFunctionBodyStart() override;
2375ffd83dbSDimitry Andric void emitFunctionBodyEnd() override;
2385ffd83dbSDimitry Andric void emitInstruction(const MachineInstr *MI) override;
2390b57cec5SDimitry Andric };
2400b57cec5SDimitry Andric
2410b57cec5SDimitry Andric class PPCAIXAsmPrinter : public PPCAsmPrinter {
242480093f4SDimitry Andric private:
243e8d8bef9SDimitry Andric /// Symbols lowered from ExternalSymbolSDNodes, we will need to emit extern
244e8d8bef9SDimitry Andric /// linkage for them in AIX.
245*0fca6ea1SDimitry Andric SmallSetVector<MCSymbol *, 8> ExtSymSDNodeSymbols;
246e8d8bef9SDimitry Andric
247e8d8bef9SDimitry Andric /// A format indicator and unique trailing identifier to form part of the
248e8d8bef9SDimitry Andric /// sinit/sterm function names.
249e8d8bef9SDimitry Andric std::string FormatIndicatorAndUniqueModId;
250e8d8bef9SDimitry Andric
251e8d8bef9SDimitry Andric // Record a list of GlobalAlias associated with a GlobalObject.
252e8d8bef9SDimitry Andric // This is used for AIX's extra-label-at-definition aliasing strategy.
253e8d8bef9SDimitry Andric DenseMap<const GlobalObject *, SmallVector<const GlobalAlias *, 1>>
254e8d8bef9SDimitry Andric GOAliasMap;
255e8d8bef9SDimitry Andric
256fe6060f1SDimitry Andric uint16_t getNumberOfVRSaved();
257e8d8bef9SDimitry Andric void emitTracebackTable();
258480093f4SDimitry Andric
259fe6060f1SDimitry Andric SmallVector<const GlobalVariable *, 8> TOCDataGlobalVars;
260fe6060f1SDimitry Andric
261fe6060f1SDimitry Andric void emitGlobalVariableHelper(const GlobalVariable *);
262fe6060f1SDimitry Andric
263753f127fSDimitry Andric // Get the offset of an alias based on its AliaseeObject.
264753f127fSDimitry Andric uint64_t getAliasOffset(const Constant *C);
265753f127fSDimitry Andric
2660b57cec5SDimitry Andric public:
PPCAIXAsmPrinter(TargetMachine & TM,std::unique_ptr<MCStreamer> Streamer)2670b57cec5SDimitry Andric PPCAIXAsmPrinter(TargetMachine &TM, std::unique_ptr<MCStreamer> Streamer)
2685ffd83dbSDimitry Andric : PPCAsmPrinter(TM, std::move(Streamer)) {
2695ffd83dbSDimitry Andric if (MAI->isLittleEndian())
2705ffd83dbSDimitry Andric report_fatal_error(
2715ffd83dbSDimitry Andric "cannot create AIX PPC Assembly Printer for a little-endian target");
2725ffd83dbSDimitry Andric }
2730b57cec5SDimitry Andric
getPassName() const2740b57cec5SDimitry Andric StringRef getPassName() const override { return "AIX PPC Assembly Printer"; }
2758bcb0991SDimitry Andric
2765ffd83dbSDimitry Andric bool doInitialization(Module &M) override;
2775ffd83dbSDimitry Andric
278e8d8bef9SDimitry Andric void emitXXStructorList(const DataLayout &DL, const Constant *List,
279e8d8bef9SDimitry Andric bool IsCtor) override;
280e8d8bef9SDimitry Andric
2818bcb0991SDimitry Andric void SetupMachineFunction(MachineFunction &MF) override;
2828bcb0991SDimitry Andric
2835ffd83dbSDimitry Andric void emitGlobalVariable(const GlobalVariable *GV) override;
284480093f4SDimitry Andric
2855ffd83dbSDimitry Andric void emitFunctionDescriptor() override;
2868bcb0991SDimitry Andric
287e8d8bef9SDimitry Andric void emitFunctionEntryLabel() override;
288e8d8bef9SDimitry Andric
289e8d8bef9SDimitry Andric void emitFunctionBodyEnd() override;
290e8d8bef9SDimitry Andric
2914542f901SDimitry Andric void emitPGORefs(Module &M);
29281ad6265SDimitry Andric
2935ffd83dbSDimitry Andric void emitEndOfAsmFile(Module &) override;
2948bcb0991SDimitry Andric
2955ffd83dbSDimitry Andric void emitLinkage(const GlobalValue *GV, MCSymbol *GVSym) const override;
296e8d8bef9SDimitry Andric
297e8d8bef9SDimitry Andric void emitInstruction(const MachineInstr *MI) override;
298e8d8bef9SDimitry Andric
299e8d8bef9SDimitry Andric bool doFinalization(Module &M) override;
300e8d8bef9SDimitry Andric
301e8d8bef9SDimitry Andric void emitTTypeReference(const GlobalValue *GV, unsigned Encoding) override;
30206c3fb27SDimitry Andric
30306c3fb27SDimitry Andric void emitModuleCommandLines(Module &M) override;
3040b57cec5SDimitry Andric };
3050b57cec5SDimitry Andric
3060b57cec5SDimitry Andric } // end anonymous namespace
3070b57cec5SDimitry Andric
PrintSymbolOperand(const MachineOperand & MO,raw_ostream & O)3080b57cec5SDimitry Andric void PPCAsmPrinter::PrintSymbolOperand(const MachineOperand &MO,
3090b57cec5SDimitry Andric raw_ostream &O) {
3100b57cec5SDimitry Andric // Computing the address of a global symbol, not calling it.
3110b57cec5SDimitry Andric const GlobalValue *GV = MO.getGlobal();
3125ffd83dbSDimitry Andric getSymbol(GV)->print(O, MAI);
3130b57cec5SDimitry Andric printOffset(MO.getOffset(), O);
3140b57cec5SDimitry Andric }
3150b57cec5SDimitry Andric
printOperand(const MachineInstr * MI,unsigned OpNo,raw_ostream & O)3160b57cec5SDimitry Andric void PPCAsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNo,
3170b57cec5SDimitry Andric raw_ostream &O) {
3180b57cec5SDimitry Andric const DataLayout &DL = getDataLayout();
3190b57cec5SDimitry Andric const MachineOperand &MO = MI->getOperand(OpNo);
3200b57cec5SDimitry Andric
3210b57cec5SDimitry Andric switch (MO.getType()) {
3220b57cec5SDimitry Andric case MachineOperand::MO_Register: {
3230b57cec5SDimitry Andric // The MI is INLINEASM ONLY and UseVSXReg is always false.
3240b57cec5SDimitry Andric const char *RegName = PPCInstPrinter::getRegisterName(MO.getReg());
3250b57cec5SDimitry Andric
3260b57cec5SDimitry Andric // Linux assembler (Others?) does not take register mnemonics.
3270b57cec5SDimitry Andric // FIXME - What about special registers used in mfspr/mtspr?
3285f757f3fSDimitry Andric O << PPC::stripRegisterPrefix(RegName);
3290b57cec5SDimitry Andric return;
3300b57cec5SDimitry Andric }
3310b57cec5SDimitry Andric case MachineOperand::MO_Immediate:
3320b57cec5SDimitry Andric O << MO.getImm();
3330b57cec5SDimitry Andric return;
3340b57cec5SDimitry Andric
3350b57cec5SDimitry Andric case MachineOperand::MO_MachineBasicBlock:
3360b57cec5SDimitry Andric MO.getMBB()->getSymbol()->print(O, MAI);
3370b57cec5SDimitry Andric return;
3380b57cec5SDimitry Andric case MachineOperand::MO_ConstantPoolIndex:
3390b57cec5SDimitry Andric O << DL.getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << '_'
3400b57cec5SDimitry Andric << MO.getIndex();
3410b57cec5SDimitry Andric return;
3420b57cec5SDimitry Andric case MachineOperand::MO_BlockAddress:
3430b57cec5SDimitry Andric GetBlockAddressSymbol(MO.getBlockAddress())->print(O, MAI);
3440b57cec5SDimitry Andric return;
3450b57cec5SDimitry Andric case MachineOperand::MO_GlobalAddress: {
3460b57cec5SDimitry Andric PrintSymbolOperand(MO, O);
3470b57cec5SDimitry Andric return;
3480b57cec5SDimitry Andric }
3490b57cec5SDimitry Andric
3500b57cec5SDimitry Andric default:
3510b57cec5SDimitry Andric O << "<unknown operand type: " << (unsigned)MO.getType() << ">";
3520b57cec5SDimitry Andric return;
3530b57cec5SDimitry Andric }
3540b57cec5SDimitry Andric }
3550b57cec5SDimitry Andric
3560b57cec5SDimitry Andric /// PrintAsmOperand - Print out an operand for an inline asm expression.
3570b57cec5SDimitry Andric ///
PrintAsmOperand(const MachineInstr * MI,unsigned OpNo,const char * ExtraCode,raw_ostream & O)3580b57cec5SDimitry Andric bool PPCAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
3590b57cec5SDimitry Andric const char *ExtraCode, raw_ostream &O) {
3600b57cec5SDimitry Andric // Does this asm operand have a single letter operand modifier?
3610b57cec5SDimitry Andric if (ExtraCode && ExtraCode[0]) {
3620b57cec5SDimitry Andric if (ExtraCode[1] != 0) return true; // Unknown modifier.
3630b57cec5SDimitry Andric
3640b57cec5SDimitry Andric switch (ExtraCode[0]) {
3650b57cec5SDimitry Andric default:
3660b57cec5SDimitry Andric // See if this is a generic print operand
3670b57cec5SDimitry Andric return AsmPrinter::PrintAsmOperand(MI, OpNo, ExtraCode, O);
3680b57cec5SDimitry Andric case 'L': // Write second word of DImode reference.
3690b57cec5SDimitry Andric // Verify that this operand has two consecutive registers.
3700b57cec5SDimitry Andric if (!MI->getOperand(OpNo).isReg() ||
3710b57cec5SDimitry Andric OpNo+1 == MI->getNumOperands() ||
3720b57cec5SDimitry Andric !MI->getOperand(OpNo+1).isReg())
3730b57cec5SDimitry Andric return true;
3740b57cec5SDimitry Andric ++OpNo; // Return the high-part.
3750b57cec5SDimitry Andric break;
3760b57cec5SDimitry Andric case 'I':
3770b57cec5SDimitry Andric // Write 'i' if an integer constant, otherwise nothing. Used to print
3780b57cec5SDimitry Andric // addi vs add, etc.
3790b57cec5SDimitry Andric if (MI->getOperand(OpNo).isImm())
3800b57cec5SDimitry Andric O << "i";
3810b57cec5SDimitry Andric return false;
3820b57cec5SDimitry Andric case 'x':
3830b57cec5SDimitry Andric if(!MI->getOperand(OpNo).isReg())
3840b57cec5SDimitry Andric return true;
3850b57cec5SDimitry Andric // This operand uses VSX numbering.
3860b57cec5SDimitry Andric // If the operand is a VMX register, convert it to a VSX register.
3878bcb0991SDimitry Andric Register Reg = MI->getOperand(OpNo).getReg();
3885f757f3fSDimitry Andric if (PPC::isVRRegister(Reg))
3890b57cec5SDimitry Andric Reg = PPC::VSX32 + (Reg - PPC::V0);
3905f757f3fSDimitry Andric else if (PPC::isVFRegister(Reg))
3910b57cec5SDimitry Andric Reg = PPC::VSX32 + (Reg - PPC::VF0);
3920b57cec5SDimitry Andric const char *RegName;
3930b57cec5SDimitry Andric RegName = PPCInstPrinter::getRegisterName(Reg);
3945f757f3fSDimitry Andric RegName = PPC::stripRegisterPrefix(RegName);
3950b57cec5SDimitry Andric O << RegName;
3960b57cec5SDimitry Andric return false;
3970b57cec5SDimitry Andric }
3980b57cec5SDimitry Andric }
3990b57cec5SDimitry Andric
4000b57cec5SDimitry Andric printOperand(MI, OpNo, O);
4010b57cec5SDimitry Andric return false;
4020b57cec5SDimitry Andric }
4030b57cec5SDimitry Andric
4040b57cec5SDimitry Andric // At the moment, all inline asm memory operands are a single register.
4050b57cec5SDimitry Andric // In any case, the output of this routine should always be just one
4060b57cec5SDimitry Andric // assembler operand.
PrintAsmMemoryOperand(const MachineInstr * MI,unsigned OpNo,const char * ExtraCode,raw_ostream & O)4070b57cec5SDimitry Andric bool PPCAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
4080b57cec5SDimitry Andric const char *ExtraCode,
4090b57cec5SDimitry Andric raw_ostream &O) {
4100b57cec5SDimitry Andric if (ExtraCode && ExtraCode[0]) {
4110b57cec5SDimitry Andric if (ExtraCode[1] != 0) return true; // Unknown modifier.
4120b57cec5SDimitry Andric
4130b57cec5SDimitry Andric switch (ExtraCode[0]) {
4140b57cec5SDimitry Andric default: return true; // Unknown modifier.
41562cfcf62SDimitry Andric case 'L': // A memory reference to the upper word of a double word op.
41662cfcf62SDimitry Andric O << getDataLayout().getPointerSize() << "(";
41762cfcf62SDimitry Andric printOperand(MI, OpNo, O);
41862cfcf62SDimitry Andric O << ")";
41962cfcf62SDimitry Andric return false;
4200b57cec5SDimitry Andric case 'y': // A memory reference for an X-form instruction
4215ffd83dbSDimitry Andric O << "0, ";
4220b57cec5SDimitry Andric printOperand(MI, OpNo, O);
4230b57cec5SDimitry Andric return false;
42423408297SDimitry Andric case 'I':
42523408297SDimitry Andric // Write 'i' if an integer constant, otherwise nothing. Used to print
42623408297SDimitry Andric // addi vs add, etc.
42723408297SDimitry Andric if (MI->getOperand(OpNo).isImm())
42823408297SDimitry Andric O << "i";
42923408297SDimitry Andric return false;
4300b57cec5SDimitry Andric case 'U': // Print 'u' for update form.
4310b57cec5SDimitry Andric case 'X': // Print 'x' for indexed form.
4320b57cec5SDimitry Andric // FIXME: Currently for PowerPC memory operands are always loaded
4330b57cec5SDimitry Andric // into a register, so we never get an update or indexed form.
4340b57cec5SDimitry Andric // This is bad even for offset forms, since even if we know we
4350b57cec5SDimitry Andric // have a value in -16(r1), we will generate a load into r<n>
4360b57cec5SDimitry Andric // and then load from 0(r<n>). Until that issue is fixed,
4370b57cec5SDimitry Andric // tolerate 'U' and 'X' but don't output anything.
4380b57cec5SDimitry Andric assert(MI->getOperand(OpNo).isReg());
4390b57cec5SDimitry Andric return false;
4400b57cec5SDimitry Andric }
4410b57cec5SDimitry Andric }
4420b57cec5SDimitry Andric
4430b57cec5SDimitry Andric assert(MI->getOperand(OpNo).isReg());
4440b57cec5SDimitry Andric O << "0(";
4450b57cec5SDimitry Andric printOperand(MI, OpNo, O);
4460b57cec5SDimitry Andric O << ")";
4470b57cec5SDimitry Andric return false;
4480b57cec5SDimitry Andric }
4490b57cec5SDimitry Andric
collectTOCStats(PPCAsmPrinter::TOCEntryType Type)45006c3fb27SDimitry Andric static void collectTOCStats(PPCAsmPrinter::TOCEntryType Type) {
45106c3fb27SDimitry Andric ++NumTOCEntries;
45206c3fb27SDimitry Andric switch (Type) {
45306c3fb27SDimitry Andric case PPCAsmPrinter::TOCType_ConstantPool:
45406c3fb27SDimitry Andric ++NumTOCConstPool;
45506c3fb27SDimitry Andric break;
45606c3fb27SDimitry Andric case PPCAsmPrinter::TOCType_GlobalInternal:
45706c3fb27SDimitry Andric ++NumTOCGlobalInternal;
45806c3fb27SDimitry Andric break;
45906c3fb27SDimitry Andric case PPCAsmPrinter::TOCType_GlobalExternal:
46006c3fb27SDimitry Andric ++NumTOCGlobalExternal;
46106c3fb27SDimitry Andric break;
46206c3fb27SDimitry Andric case PPCAsmPrinter::TOCType_JumpTable:
46306c3fb27SDimitry Andric ++NumTOCJumpTable;
46406c3fb27SDimitry Andric break;
46506c3fb27SDimitry Andric case PPCAsmPrinter::TOCType_ThreadLocal:
46606c3fb27SDimitry Andric ++NumTOCThreadLocal;
46706c3fb27SDimitry Andric break;
46806c3fb27SDimitry Andric case PPCAsmPrinter::TOCType_BlockAddress:
46906c3fb27SDimitry Andric ++NumTOCBlockAddress;
47006c3fb27SDimitry Andric break;
47106c3fb27SDimitry Andric case PPCAsmPrinter::TOCType_EHBlock:
47206c3fb27SDimitry Andric ++NumTOCEHBlock;
47306c3fb27SDimitry Andric break;
47406c3fb27SDimitry Andric }
47506c3fb27SDimitry Andric }
47606c3fb27SDimitry Andric
getCodeModel(const PPCSubtarget & S,const TargetMachine & TM,const MachineOperand & MO)477*0fca6ea1SDimitry Andric static CodeModel::Model getCodeModel(const PPCSubtarget &S,
478*0fca6ea1SDimitry Andric const TargetMachine &TM,
479*0fca6ea1SDimitry Andric const MachineOperand &MO) {
480*0fca6ea1SDimitry Andric CodeModel::Model ModuleModel = TM.getCodeModel();
481*0fca6ea1SDimitry Andric
482*0fca6ea1SDimitry Andric // If the operand is not a global address then there is no
483*0fca6ea1SDimitry Andric // global variable to carry an attribute.
484*0fca6ea1SDimitry Andric if (!(MO.getType() == MachineOperand::MO_GlobalAddress))
485*0fca6ea1SDimitry Andric return ModuleModel;
486*0fca6ea1SDimitry Andric
487*0fca6ea1SDimitry Andric const GlobalValue *GV = MO.getGlobal();
488*0fca6ea1SDimitry Andric assert(GV && "expected global for MO_GlobalAddress");
489*0fca6ea1SDimitry Andric
490*0fca6ea1SDimitry Andric return S.getCodeModel(TM, GV);
491*0fca6ea1SDimitry Andric }
492*0fca6ea1SDimitry Andric
setOptionalCodeModel(MCSymbolXCOFF * XSym,CodeModel::Model CM)493*0fca6ea1SDimitry Andric static void setOptionalCodeModel(MCSymbolXCOFF *XSym, CodeModel::Model CM) {
494*0fca6ea1SDimitry Andric switch (CM) {
495*0fca6ea1SDimitry Andric case CodeModel::Large:
496*0fca6ea1SDimitry Andric XSym->setPerSymbolCodeModel(MCSymbolXCOFF::CM_Large);
497*0fca6ea1SDimitry Andric return;
498*0fca6ea1SDimitry Andric case CodeModel::Small:
499*0fca6ea1SDimitry Andric XSym->setPerSymbolCodeModel(MCSymbolXCOFF::CM_Small);
500*0fca6ea1SDimitry Andric return;
501*0fca6ea1SDimitry Andric default:
502*0fca6ea1SDimitry Andric report_fatal_error("Invalid code model for AIX");
503*0fca6ea1SDimitry Andric }
504*0fca6ea1SDimitry Andric }
505*0fca6ea1SDimitry Andric
5060b57cec5SDimitry Andric /// lookUpOrCreateTOCEntry -- Given a symbol, look up whether a TOC entry
5070b57cec5SDimitry Andric /// exists for it. If not, create one. Then return a symbol that references
5080b57cec5SDimitry Andric /// the TOC entry.
509fe6060f1SDimitry Andric MCSymbol *
lookUpOrCreateTOCEntry(const MCSymbol * Sym,TOCEntryType Type,MCSymbolRefExpr::VariantKind Kind)51006c3fb27SDimitry Andric PPCAsmPrinter::lookUpOrCreateTOCEntry(const MCSymbol *Sym, TOCEntryType Type,
511fe6060f1SDimitry Andric MCSymbolRefExpr::VariantKind Kind) {
51206c3fb27SDimitry Andric // If this is a new TOC entry add statistics about it.
51306c3fb27SDimitry Andric if (!TOC.contains({Sym, Kind}))
51406c3fb27SDimitry Andric collectTOCStats(Type);
51506c3fb27SDimitry Andric
516fe6060f1SDimitry Andric MCSymbol *&TOCEntry = TOC[{Sym, Kind}];
5170b57cec5SDimitry Andric if (!TOCEntry)
5180b57cec5SDimitry Andric TOCEntry = createTempSymbol("C");
5190b57cec5SDimitry Andric return TOCEntry;
5200b57cec5SDimitry Andric }
5210b57cec5SDimitry Andric
LowerSTACKMAP(StackMaps & SM,const MachineInstr & MI)5220b57cec5SDimitry Andric void PPCAsmPrinter::LowerSTACKMAP(StackMaps &SM, const MachineInstr &MI) {
5230b57cec5SDimitry Andric unsigned NumNOPBytes = MI.getOperand(1).getImm();
5240b57cec5SDimitry Andric
525480093f4SDimitry Andric auto &Ctx = OutStreamer->getContext();
526480093f4SDimitry Andric MCSymbol *MILabel = Ctx.createTempSymbol();
5275ffd83dbSDimitry Andric OutStreamer->emitLabel(MILabel);
528480093f4SDimitry Andric
529480093f4SDimitry Andric SM.recordStackMap(*MILabel, MI);
5300b57cec5SDimitry Andric assert(NumNOPBytes % 4 == 0 && "Invalid number of NOP bytes requested!");
5310b57cec5SDimitry Andric
5320b57cec5SDimitry Andric // Scan ahead to trim the shadow.
5330b57cec5SDimitry Andric const MachineBasicBlock &MBB = *MI.getParent();
5340b57cec5SDimitry Andric MachineBasicBlock::const_iterator MII(MI);
5350b57cec5SDimitry Andric ++MII;
5360b57cec5SDimitry Andric while (NumNOPBytes > 0) {
5370b57cec5SDimitry Andric if (MII == MBB.end() || MII->isCall() ||
5380b57cec5SDimitry Andric MII->getOpcode() == PPC::DBG_VALUE ||
5390b57cec5SDimitry Andric MII->getOpcode() == TargetOpcode::PATCHPOINT ||
5400b57cec5SDimitry Andric MII->getOpcode() == TargetOpcode::STACKMAP)
5410b57cec5SDimitry Andric break;
5420b57cec5SDimitry Andric ++MII;
5430b57cec5SDimitry Andric NumNOPBytes -= 4;
5440b57cec5SDimitry Andric }
5450b57cec5SDimitry Andric
5460b57cec5SDimitry Andric // Emit nops.
5470b57cec5SDimitry Andric for (unsigned i = 0; i < NumNOPBytes; i += 4)
5480b57cec5SDimitry Andric EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::NOP));
5490b57cec5SDimitry Andric }
5500b57cec5SDimitry Andric
5510b57cec5SDimitry Andric // Lower a patchpoint of the form:
5520b57cec5SDimitry Andric // [<def>], <id>, <numBytes>, <target>, <numArgs>
LowerPATCHPOINT(StackMaps & SM,const MachineInstr & MI)5530b57cec5SDimitry Andric void PPCAsmPrinter::LowerPATCHPOINT(StackMaps &SM, const MachineInstr &MI) {
554480093f4SDimitry Andric auto &Ctx = OutStreamer->getContext();
555480093f4SDimitry Andric MCSymbol *MILabel = Ctx.createTempSymbol();
5565ffd83dbSDimitry Andric OutStreamer->emitLabel(MILabel);
557480093f4SDimitry Andric
558480093f4SDimitry Andric SM.recordPatchPoint(*MILabel, MI);
5590b57cec5SDimitry Andric PatchPointOpers Opers(&MI);
5600b57cec5SDimitry Andric
5610b57cec5SDimitry Andric unsigned EncodedBytes = 0;
5620b57cec5SDimitry Andric const MachineOperand &CalleeMO = Opers.getCallTarget();
5630b57cec5SDimitry Andric
5640b57cec5SDimitry Andric if (CalleeMO.isImm()) {
5650b57cec5SDimitry Andric int64_t CallTarget = CalleeMO.getImm();
5660b57cec5SDimitry Andric if (CallTarget) {
5670b57cec5SDimitry Andric assert((CallTarget & 0xFFFFFFFFFFFF) == CallTarget &&
5680b57cec5SDimitry Andric "High 16 bits of call target should be zero.");
5698bcb0991SDimitry Andric Register ScratchReg = MI.getOperand(Opers.getNextScratchIdx()).getReg();
5700b57cec5SDimitry Andric EncodedBytes = 0;
5710b57cec5SDimitry Andric // Materialize the jump address:
5720b57cec5SDimitry Andric EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::LI8)
5730b57cec5SDimitry Andric .addReg(ScratchReg)
5740b57cec5SDimitry Andric .addImm((CallTarget >> 32) & 0xFFFF));
5750b57cec5SDimitry Andric ++EncodedBytes;
5760b57cec5SDimitry Andric EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::RLDIC)
5770b57cec5SDimitry Andric .addReg(ScratchReg)
5780b57cec5SDimitry Andric .addReg(ScratchReg)
5790b57cec5SDimitry Andric .addImm(32).addImm(16));
5800b57cec5SDimitry Andric ++EncodedBytes;
5810b57cec5SDimitry Andric EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::ORIS8)
5820b57cec5SDimitry Andric .addReg(ScratchReg)
5830b57cec5SDimitry Andric .addReg(ScratchReg)
5840b57cec5SDimitry Andric .addImm((CallTarget >> 16) & 0xFFFF));
5850b57cec5SDimitry Andric ++EncodedBytes;
5860b57cec5SDimitry Andric EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::ORI8)
5870b57cec5SDimitry Andric .addReg(ScratchReg)
5880b57cec5SDimitry Andric .addReg(ScratchReg)
5890b57cec5SDimitry Andric .addImm(CallTarget & 0xFFFF));
5900b57cec5SDimitry Andric
5910b57cec5SDimitry Andric // Save the current TOC pointer before the remote call.
5920b57cec5SDimitry Andric int TOCSaveOffset = Subtarget->getFrameLowering()->getTOCSaveOffset();
5930b57cec5SDimitry Andric EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::STD)
5940b57cec5SDimitry Andric .addReg(PPC::X2)
5950b57cec5SDimitry Andric .addImm(TOCSaveOffset)
5960b57cec5SDimitry Andric .addReg(PPC::X1));
5970b57cec5SDimitry Andric ++EncodedBytes;
5980b57cec5SDimitry Andric
5990b57cec5SDimitry Andric // If we're on ELFv1, then we need to load the actual function pointer
6000b57cec5SDimitry Andric // from the function descriptor.
6010b57cec5SDimitry Andric if (!Subtarget->isELFv2ABI()) {
6020b57cec5SDimitry Andric // Load the new TOC pointer and the function address, but not r11
6030b57cec5SDimitry Andric // (needing this is rare, and loading it here would prevent passing it
6040b57cec5SDimitry Andric // via a 'nest' parameter.
6050b57cec5SDimitry Andric EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::LD)
6060b57cec5SDimitry Andric .addReg(PPC::X2)
6070b57cec5SDimitry Andric .addImm(8)
6080b57cec5SDimitry Andric .addReg(ScratchReg));
6090b57cec5SDimitry Andric ++EncodedBytes;
6100b57cec5SDimitry Andric EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::LD)
6110b57cec5SDimitry Andric .addReg(ScratchReg)
6120b57cec5SDimitry Andric .addImm(0)
6130b57cec5SDimitry Andric .addReg(ScratchReg));
6140b57cec5SDimitry Andric ++EncodedBytes;
6150b57cec5SDimitry Andric }
6160b57cec5SDimitry Andric
6170b57cec5SDimitry Andric EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::MTCTR8)
6180b57cec5SDimitry Andric .addReg(ScratchReg));
6190b57cec5SDimitry Andric ++EncodedBytes;
6200b57cec5SDimitry Andric EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::BCTRL8));
6210b57cec5SDimitry Andric ++EncodedBytes;
6220b57cec5SDimitry Andric
6230b57cec5SDimitry Andric // Restore the TOC pointer after the call.
6240b57cec5SDimitry Andric EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::LD)
6250b57cec5SDimitry Andric .addReg(PPC::X2)
6260b57cec5SDimitry Andric .addImm(TOCSaveOffset)
6270b57cec5SDimitry Andric .addReg(PPC::X1));
6280b57cec5SDimitry Andric ++EncodedBytes;
6290b57cec5SDimitry Andric }
6300b57cec5SDimitry Andric } else if (CalleeMO.isGlobal()) {
6310b57cec5SDimitry Andric const GlobalValue *GValue = CalleeMO.getGlobal();
6320b57cec5SDimitry Andric MCSymbol *MOSymbol = getSymbol(GValue);
6330b57cec5SDimitry Andric const MCExpr *SymVar = MCSymbolRefExpr::create(MOSymbol, OutContext);
6340b57cec5SDimitry Andric
6350b57cec5SDimitry Andric EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::BL8_NOP)
6360b57cec5SDimitry Andric .addExpr(SymVar));
6370b57cec5SDimitry Andric EncodedBytes += 2;
6380b57cec5SDimitry Andric }
6390b57cec5SDimitry Andric
6400b57cec5SDimitry Andric // Each instruction is 4 bytes.
6410b57cec5SDimitry Andric EncodedBytes *= 4;
6420b57cec5SDimitry Andric
6430b57cec5SDimitry Andric // Emit padding.
6440b57cec5SDimitry Andric unsigned NumBytes = Opers.getNumPatchBytes();
6450b57cec5SDimitry Andric assert(NumBytes >= EncodedBytes &&
6460b57cec5SDimitry Andric "Patchpoint can't request size less than the length of a call.");
6470b57cec5SDimitry Andric assert((NumBytes - EncodedBytes) % 4 == 0 &&
6480b57cec5SDimitry Andric "Invalid number of NOP bytes requested!");
6490b57cec5SDimitry Andric for (unsigned i = EncodedBytes; i < NumBytes; i += 4)
6500b57cec5SDimitry Andric EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::NOP));
6510b57cec5SDimitry Andric }
6520b57cec5SDimitry Andric
653*0fca6ea1SDimitry Andric /// This helper function creates the TlsGetAddr/TlsGetMod MCSymbol for AIX. We
654*0fca6ea1SDimitry Andric /// will create the csect and use the qual-name symbol instead of creating just
655*0fca6ea1SDimitry Andric /// the external symbol.
createMCSymbolForTlsGetAddr(MCContext & Ctx,unsigned MIOpc)65606c3fb27SDimitry Andric static MCSymbol *createMCSymbolForTlsGetAddr(MCContext &Ctx, unsigned MIOpc) {
657*0fca6ea1SDimitry Andric StringRef SymName;
658*0fca6ea1SDimitry Andric switch (MIOpc) {
659*0fca6ea1SDimitry Andric default:
660*0fca6ea1SDimitry Andric SymName = ".__tls_get_addr";
661*0fca6ea1SDimitry Andric break;
662*0fca6ea1SDimitry Andric case PPC::GETtlsTpointer32AIX:
663*0fca6ea1SDimitry Andric SymName = ".__get_tpointer";
664*0fca6ea1SDimitry Andric break;
665*0fca6ea1SDimitry Andric case PPC::GETtlsMOD32AIX:
666*0fca6ea1SDimitry Andric case PPC::GETtlsMOD64AIX:
667*0fca6ea1SDimitry Andric SymName = ".__tls_get_mod";
668*0fca6ea1SDimitry Andric break;
669*0fca6ea1SDimitry Andric }
670fe6060f1SDimitry Andric return Ctx
67106c3fb27SDimitry Andric .getXCOFFSection(SymName, SectionKind::getText(),
672fe6060f1SDimitry Andric XCOFF::CsectProperties(XCOFF::XMC_PR, XCOFF::XTY_ER))
673fe6060f1SDimitry Andric ->getQualNameSymbol();
674fe6060f1SDimitry Andric }
675fe6060f1SDimitry Andric
EmitAIXTlsCallHelper(const MachineInstr * MI)67606c3fb27SDimitry Andric void PPCAsmPrinter::EmitAIXTlsCallHelper(const MachineInstr *MI) {
67706c3fb27SDimitry Andric assert(Subtarget->isAIXABI() &&
67806c3fb27SDimitry Andric "Only expecting to emit calls to get the thread pointer on AIX!");
67906c3fb27SDimitry Andric
68006c3fb27SDimitry Andric MCSymbol *TlsCall = createMCSymbolForTlsGetAddr(OutContext, MI->getOpcode());
68106c3fb27SDimitry Andric const MCExpr *TlsRef =
68206c3fb27SDimitry Andric MCSymbolRefExpr::create(TlsCall, MCSymbolRefExpr::VK_None, OutContext);
68306c3fb27SDimitry Andric EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::BLA).addExpr(TlsRef));
68406c3fb27SDimitry Andric }
68506c3fb27SDimitry Andric
6860b57cec5SDimitry Andric /// EmitTlsCall -- Given a GETtls[ld]ADDR[32] instruction, print a
6870b57cec5SDimitry Andric /// call to __tls_get_addr to the current output stream.
EmitTlsCall(const MachineInstr * MI,MCSymbolRefExpr::VariantKind VK)6880b57cec5SDimitry Andric void PPCAsmPrinter::EmitTlsCall(const MachineInstr *MI,
6890b57cec5SDimitry Andric MCSymbolRefExpr::VariantKind VK) {
6900b57cec5SDimitry Andric MCSymbolRefExpr::VariantKind Kind = MCSymbolRefExpr::VK_None;
691e8d8bef9SDimitry Andric unsigned Opcode = PPC::BL8_NOP_TLS;
692e8d8bef9SDimitry Andric
693e8d8bef9SDimitry Andric assert(MI->getNumOperands() >= 3 && "Expecting at least 3 operands from MI");
694e8d8bef9SDimitry Andric if (MI->getOperand(2).getTargetFlags() == PPCII::MO_GOT_TLSGD_PCREL_FLAG ||
695e8d8bef9SDimitry Andric MI->getOperand(2).getTargetFlags() == PPCII::MO_GOT_TLSLD_PCREL_FLAG) {
696e8d8bef9SDimitry Andric Kind = MCSymbolRefExpr::VK_PPC_NOTOC;
697e8d8bef9SDimitry Andric Opcode = PPC::BL8_NOTOC_TLS;
698e8d8bef9SDimitry Andric }
6990b57cec5SDimitry Andric const Module *M = MF->getFunction().getParent();
7000b57cec5SDimitry Andric
7010b57cec5SDimitry Andric assert(MI->getOperand(0).isReg() &&
7020b57cec5SDimitry Andric ((Subtarget->isPPC64() && MI->getOperand(0).getReg() == PPC::X3) ||
7030b57cec5SDimitry Andric (!Subtarget->isPPC64() && MI->getOperand(0).getReg() == PPC::R3)) &&
7040b57cec5SDimitry Andric "GETtls[ld]ADDR[32] must define GPR3");
7050b57cec5SDimitry Andric assert(MI->getOperand(1).isReg() &&
7060b57cec5SDimitry Andric ((Subtarget->isPPC64() && MI->getOperand(1).getReg() == PPC::X3) ||
7070b57cec5SDimitry Andric (!Subtarget->isPPC64() && MI->getOperand(1).getReg() == PPC::R3)) &&
7080b57cec5SDimitry Andric "GETtls[ld]ADDR[32] must read GPR3");
7090b57cec5SDimitry Andric
710fe6060f1SDimitry Andric if (Subtarget->isAIXABI()) {
711*0fca6ea1SDimitry Andric // For TLSGD, the variable offset should already be in R4 and the region
712*0fca6ea1SDimitry Andric // handle should already be in R3. We generate an absolute branch to
713*0fca6ea1SDimitry Andric // .__tls_get_addr. For TLSLD, the module handle should already be in R3.
714*0fca6ea1SDimitry Andric // We generate an absolute branch to .__tls_get_mod.
715fe6060f1SDimitry Andric Register VarOffsetReg = Subtarget->isPPC64() ? PPC::X4 : PPC::R4;
716fe6060f1SDimitry Andric (void)VarOffsetReg;
717*0fca6ea1SDimitry Andric assert((MI->getOpcode() == PPC::GETtlsMOD32AIX ||
718*0fca6ea1SDimitry Andric MI->getOpcode() == PPC::GETtlsMOD64AIX ||
719*0fca6ea1SDimitry Andric (MI->getOperand(2).isReg() &&
720*0fca6ea1SDimitry Andric MI->getOperand(2).getReg() == VarOffsetReg)) &&
721fe6060f1SDimitry Andric "GETtls[ld]ADDR[32] must read GPR4");
72206c3fb27SDimitry Andric EmitAIXTlsCallHelper(MI);
723fe6060f1SDimitry Andric return;
724fe6060f1SDimitry Andric }
725fe6060f1SDimitry Andric
726fe6060f1SDimitry Andric MCSymbol *TlsGetAddr = OutContext.getOrCreateSymbol("__tls_get_addr");
727fe6060f1SDimitry Andric
728480093f4SDimitry Andric if (Subtarget->is32BitELFABI() && isPositionIndependent())
7290b57cec5SDimitry Andric Kind = MCSymbolRefExpr::VK_PLT;
730480093f4SDimitry Andric
7310b57cec5SDimitry Andric const MCExpr *TlsRef =
7320b57cec5SDimitry Andric MCSymbolRefExpr::create(TlsGetAddr, Kind, OutContext);
7330b57cec5SDimitry Andric
7340b57cec5SDimitry Andric // Add 32768 offset to the symbol so we follow up the latest GOT/PLT ABI.
7350b57cec5SDimitry Andric if (Kind == MCSymbolRefExpr::VK_PLT && Subtarget->isSecurePlt() &&
7360b57cec5SDimitry Andric M->getPICLevel() == PICLevel::BigPIC)
7370b57cec5SDimitry Andric TlsRef = MCBinaryExpr::createAdd(
7380b57cec5SDimitry Andric TlsRef, MCConstantExpr::create(32768, OutContext), OutContext);
7390b57cec5SDimitry Andric const MachineOperand &MO = MI->getOperand(2);
7400b57cec5SDimitry Andric const GlobalValue *GValue = MO.getGlobal();
7410b57cec5SDimitry Andric MCSymbol *MOSymbol = getSymbol(GValue);
7420b57cec5SDimitry Andric const MCExpr *SymVar = MCSymbolRefExpr::create(MOSymbol, VK, OutContext);
7430b57cec5SDimitry Andric EmitToStreamer(*OutStreamer,
744e8d8bef9SDimitry Andric MCInstBuilder(Subtarget->isPPC64() ? Opcode
745e8d8bef9SDimitry Andric : (unsigned)PPC::BL_TLS)
7460b57cec5SDimitry Andric .addExpr(TlsRef)
7470b57cec5SDimitry Andric .addExpr(SymVar));
7480b57cec5SDimitry Andric }
7490b57cec5SDimitry Andric
7508bcb0991SDimitry Andric /// Map a machine operand for a TOC pseudo-machine instruction to its
7518bcb0991SDimitry Andric /// corresponding MCSymbol.
getMCSymbolForTOCPseudoMO(const MachineOperand & MO,AsmPrinter & AP)7525ffd83dbSDimitry Andric static MCSymbol *getMCSymbolForTOCPseudoMO(const MachineOperand &MO,
7535ffd83dbSDimitry Andric AsmPrinter &AP) {
7548bcb0991SDimitry Andric switch (MO.getType()) {
7558bcb0991SDimitry Andric case MachineOperand::MO_GlobalAddress:
7565ffd83dbSDimitry Andric return AP.getSymbol(MO.getGlobal());
7578bcb0991SDimitry Andric case MachineOperand::MO_ConstantPoolIndex:
7585ffd83dbSDimitry Andric return AP.GetCPISymbol(MO.getIndex());
7598bcb0991SDimitry Andric case MachineOperand::MO_JumpTableIndex:
7605ffd83dbSDimitry Andric return AP.GetJTISymbol(MO.getIndex());
7618bcb0991SDimitry Andric case MachineOperand::MO_BlockAddress:
7625ffd83dbSDimitry Andric return AP.GetBlockAddressSymbol(MO.getBlockAddress());
7638bcb0991SDimitry Andric default:
7648bcb0991SDimitry Andric llvm_unreachable("Unexpected operand type to get symbol.");
7658bcb0991SDimitry Andric }
7668bcb0991SDimitry Andric }
7678bcb0991SDimitry Andric
76806c3fb27SDimitry Andric static PPCAsmPrinter::TOCEntryType
getTOCEntryTypeForMO(const MachineOperand & MO)76906c3fb27SDimitry Andric getTOCEntryTypeForMO(const MachineOperand &MO) {
77006c3fb27SDimitry Andric // Use the target flags to determine if this MO is Thread Local.
77106c3fb27SDimitry Andric // If we don't do this it comes out as Global.
7725f757f3fSDimitry Andric if (PPCInstrInfo::hasTLSFlag(MO.getTargetFlags()))
77306c3fb27SDimitry Andric return PPCAsmPrinter::TOCType_ThreadLocal;
77406c3fb27SDimitry Andric
77506c3fb27SDimitry Andric switch (MO.getType()) {
77606c3fb27SDimitry Andric case MachineOperand::MO_GlobalAddress: {
77706c3fb27SDimitry Andric const GlobalValue *GlobalV = MO.getGlobal();
77806c3fb27SDimitry Andric GlobalValue::LinkageTypes Linkage = GlobalV->getLinkage();
77906c3fb27SDimitry Andric if (Linkage == GlobalValue::ExternalLinkage ||
78006c3fb27SDimitry Andric Linkage == GlobalValue::AvailableExternallyLinkage ||
78106c3fb27SDimitry Andric Linkage == GlobalValue::ExternalWeakLinkage)
78206c3fb27SDimitry Andric return PPCAsmPrinter::TOCType_GlobalExternal;
78306c3fb27SDimitry Andric
78406c3fb27SDimitry Andric return PPCAsmPrinter::TOCType_GlobalInternal;
78506c3fb27SDimitry Andric }
78606c3fb27SDimitry Andric case MachineOperand::MO_ConstantPoolIndex:
78706c3fb27SDimitry Andric return PPCAsmPrinter::TOCType_ConstantPool;
78806c3fb27SDimitry Andric case MachineOperand::MO_JumpTableIndex:
78906c3fb27SDimitry Andric return PPCAsmPrinter::TOCType_JumpTable;
79006c3fb27SDimitry Andric case MachineOperand::MO_BlockAddress:
79106c3fb27SDimitry Andric return PPCAsmPrinter::TOCType_BlockAddress;
79206c3fb27SDimitry Andric default:
79306c3fb27SDimitry Andric llvm_unreachable("Unexpected operand type to get TOC type.");
79406c3fb27SDimitry Andric }
79506c3fb27SDimitry Andric }
7960b57cec5SDimitry Andric /// EmitInstruction -- Print out a single PowerPC MI in Darwin syntax to
7970b57cec5SDimitry Andric /// the current output stream.
7980b57cec5SDimitry Andric ///
emitInstruction(const MachineInstr * MI)7995ffd83dbSDimitry Andric void PPCAsmPrinter::emitInstruction(const MachineInstr *MI) {
800753f127fSDimitry Andric PPC_MC::verifyInstructionPredicates(MI->getOpcode(),
801753f127fSDimitry Andric getSubtargetInfo().getFeatureBits());
802753f127fSDimitry Andric
8030b57cec5SDimitry Andric MCInst TmpInst;
8048bcb0991SDimitry Andric const bool IsPPC64 = Subtarget->isPPC64();
8058bcb0991SDimitry Andric const bool IsAIX = Subtarget->isAIXABI();
806*0fca6ea1SDimitry Andric const bool HasAIXSmallLocalTLS = Subtarget->hasAIXSmallLocalExecTLS() ||
807*0fca6ea1SDimitry Andric Subtarget->hasAIXSmallLocalDynamicTLS();
8080b57cec5SDimitry Andric const Module *M = MF->getFunction().getParent();
8090b57cec5SDimitry Andric PICLevel::Level PL = M->getPICLevel();
8100b57cec5SDimitry Andric
8110b57cec5SDimitry Andric #ifndef NDEBUG
8120b57cec5SDimitry Andric // Validate that SPE and FPU are mutually exclusive in codegen
8130b57cec5SDimitry Andric if (!MI->isInlineAsm()) {
8140b57cec5SDimitry Andric for (const MachineOperand &MO: MI->operands()) {
8150b57cec5SDimitry Andric if (MO.isReg()) {
8168bcb0991SDimitry Andric Register Reg = MO.getReg();
8170b57cec5SDimitry Andric if (Subtarget->hasSPE()) {
8180b57cec5SDimitry Andric if (PPC::F4RCRegClass.contains(Reg) ||
8190b57cec5SDimitry Andric PPC::F8RCRegClass.contains(Reg) ||
8200b57cec5SDimitry Andric PPC::VFRCRegClass.contains(Reg) ||
8210b57cec5SDimitry Andric PPC::VRRCRegClass.contains(Reg) ||
8220b57cec5SDimitry Andric PPC::VSFRCRegClass.contains(Reg) ||
8230b57cec5SDimitry Andric PPC::VSSRCRegClass.contains(Reg)
8240b57cec5SDimitry Andric )
8250b57cec5SDimitry Andric llvm_unreachable("SPE targets cannot have FPRegs!");
8260b57cec5SDimitry Andric } else {
8270b57cec5SDimitry Andric if (PPC::SPERCRegClass.contains(Reg))
8280b57cec5SDimitry Andric llvm_unreachable("SPE register found in FPU-targeted code!");
8290b57cec5SDimitry Andric }
8300b57cec5SDimitry Andric }
8310b57cec5SDimitry Andric }
8320b57cec5SDimitry Andric }
8330b57cec5SDimitry Andric #endif
834e8d8bef9SDimitry Andric
835e8d8bef9SDimitry Andric auto getTOCRelocAdjustedExprForXCOFF = [this](const MCExpr *Expr,
836e8d8bef9SDimitry Andric ptrdiff_t OriginalOffset) {
837e8d8bef9SDimitry Andric // Apply an offset to the TOC-based expression such that the adjusted
838e8d8bef9SDimitry Andric // notional offset from the TOC base (to be encoded into the instruction's D
839e8d8bef9SDimitry Andric // or DS field) is the signed 16-bit truncation of the original notional
840e8d8bef9SDimitry Andric // offset from the TOC base.
841e8d8bef9SDimitry Andric // This is consistent with the treatment used both by XL C/C++ and
842e8d8bef9SDimitry Andric // by AIX ld -r.
843e8d8bef9SDimitry Andric ptrdiff_t Adjustment =
844e8d8bef9SDimitry Andric OriginalOffset - llvm::SignExtend32<16>(OriginalOffset);
845e8d8bef9SDimitry Andric return MCBinaryExpr::createAdd(
846e8d8bef9SDimitry Andric Expr, MCConstantExpr::create(-Adjustment, OutContext), OutContext);
847e8d8bef9SDimitry Andric };
848e8d8bef9SDimitry Andric
849e8d8bef9SDimitry Andric auto getTOCEntryLoadingExprForXCOFF =
850e8d8bef9SDimitry Andric [IsPPC64, getTOCRelocAdjustedExprForXCOFF,
851fe6060f1SDimitry Andric this](const MCSymbol *MOSymbol, const MCExpr *Expr,
852fe6060f1SDimitry Andric MCSymbolRefExpr::VariantKind VK =
853fe6060f1SDimitry Andric MCSymbolRefExpr::VariantKind::VK_None) -> const MCExpr * {
854e8d8bef9SDimitry Andric const unsigned EntryByteSize = IsPPC64 ? 8 : 4;
855fe6060f1SDimitry Andric const auto TOCEntryIter = TOC.find({MOSymbol, VK});
856e8d8bef9SDimitry Andric assert(TOCEntryIter != TOC.end() &&
857e8d8bef9SDimitry Andric "Could not find the TOC entry for this symbol.");
858e8d8bef9SDimitry Andric const ptrdiff_t EntryDistanceFromTOCBase =
859e8d8bef9SDimitry Andric (TOCEntryIter - TOC.begin()) * EntryByteSize;
860e8d8bef9SDimitry Andric constexpr int16_t PositiveTOCRange = INT16_MAX;
861e8d8bef9SDimitry Andric
862e8d8bef9SDimitry Andric if (EntryDistanceFromTOCBase > PositiveTOCRange)
863e8d8bef9SDimitry Andric return getTOCRelocAdjustedExprForXCOFF(Expr, EntryDistanceFromTOCBase);
864e8d8bef9SDimitry Andric
865e8d8bef9SDimitry Andric return Expr;
866e8d8bef9SDimitry Andric };
867fe6060f1SDimitry Andric auto GetVKForMO = [&](const MachineOperand &MO) {
8685f757f3fSDimitry Andric // For TLS initial-exec and local-exec accesses on AIX, we have one TOC
8695f757f3fSDimitry Andric // entry for the symbol (with the variable offset), which is differentiated
8705f757f3fSDimitry Andric // by MO_TPREL_FLAG.
8715f757f3fSDimitry Andric unsigned Flag = MO.getTargetFlags();
8725f757f3fSDimitry Andric if (Flag == PPCII::MO_TPREL_FLAG ||
8735f757f3fSDimitry Andric Flag == PPCII::MO_GOT_TPREL_PCREL_FLAG ||
8745f757f3fSDimitry Andric Flag == PPCII::MO_TPREL_PCREL_FLAG) {
87506c3fb27SDimitry Andric assert(MO.isGlobal() && "Only expecting a global MachineOperand here!\n");
87606c3fb27SDimitry Andric TLSModel::Model Model = TM.getTLSModel(MO.getGlobal());
87706c3fb27SDimitry Andric if (Model == TLSModel::LocalExec)
87806c3fb27SDimitry Andric return MCSymbolRefExpr::VariantKind::VK_PPC_AIX_TLSLE;
8795f757f3fSDimitry Andric if (Model == TLSModel::InitialExec)
8805f757f3fSDimitry Andric return MCSymbolRefExpr::VariantKind::VK_PPC_AIX_TLSIE;
881*0fca6ea1SDimitry Andric // On AIX, TLS model opt may have turned local-dynamic accesses into
882*0fca6ea1SDimitry Andric // initial-exec accesses.
883*0fca6ea1SDimitry Andric PPCFunctionInfo *FuncInfo = MF->getInfo<PPCFunctionInfo>();
884*0fca6ea1SDimitry Andric if (Model == TLSModel::LocalDynamic &&
885*0fca6ea1SDimitry Andric FuncInfo->isAIXFuncUseTLSIEForLD()) {
886*0fca6ea1SDimitry Andric LLVM_DEBUG(
887*0fca6ea1SDimitry Andric dbgs() << "Current function uses IE access for default LD vars.\n");
888*0fca6ea1SDimitry Andric return MCSymbolRefExpr::VariantKind::VK_PPC_AIX_TLSIE;
889*0fca6ea1SDimitry Andric }
8905f757f3fSDimitry Andric llvm_unreachable("Only expecting local-exec or initial-exec accesses!");
89106c3fb27SDimitry Andric }
892fe6060f1SDimitry Andric // For GD TLS access on AIX, we have two TOC entries for the symbol (one for
893fe6060f1SDimitry Andric // the variable offset and the other for the region handle). They are
894fe6060f1SDimitry Andric // differentiated by MO_TLSGD_FLAG and MO_TLSGDM_FLAG.
8955f757f3fSDimitry Andric if (Flag == PPCII::MO_TLSGDM_FLAG)
896fe6060f1SDimitry Andric return MCSymbolRefExpr::VariantKind::VK_PPC_AIX_TLSGDM;
8975f757f3fSDimitry Andric if (Flag == PPCII::MO_TLSGD_FLAG || Flag == PPCII::MO_GOT_TLSGD_PCREL_FLAG)
898fe6060f1SDimitry Andric return MCSymbolRefExpr::VariantKind::VK_PPC_AIX_TLSGD;
899*0fca6ea1SDimitry Andric // For local-dynamic TLS access on AIX, we have one TOC entry for the symbol
900*0fca6ea1SDimitry Andric // (the variable offset) and one shared TOC entry for the module handle.
901*0fca6ea1SDimitry Andric // They are differentiated by MO_TLSLD_FLAG and MO_TLSLDM_FLAG.
902*0fca6ea1SDimitry Andric if (Flag == PPCII::MO_TLSLD_FLAG && IsAIX)
903*0fca6ea1SDimitry Andric return MCSymbolRefExpr::VariantKind::VK_PPC_AIX_TLSLD;
904*0fca6ea1SDimitry Andric if (Flag == PPCII::MO_TLSLDM_FLAG && IsAIX)
905*0fca6ea1SDimitry Andric return MCSymbolRefExpr::VariantKind::VK_PPC_AIX_TLSML;
906fe6060f1SDimitry Andric return MCSymbolRefExpr::VariantKind::VK_None;
907fe6060f1SDimitry Andric };
908e8d8bef9SDimitry Andric
9090b57cec5SDimitry Andric // Lower multi-instruction pseudo operations.
9100b57cec5SDimitry Andric switch (MI->getOpcode()) {
9110b57cec5SDimitry Andric default: break;
912*0fca6ea1SDimitry Andric case TargetOpcode::PATCHABLE_FUNCTION_ENTER: {
913*0fca6ea1SDimitry Andric assert(!Subtarget->isAIXABI() &&
914*0fca6ea1SDimitry Andric "AIX does not support patchable function entry!");
915*0fca6ea1SDimitry Andric // PATCHABLE_FUNCTION_ENTER on little endian is for XRAY support which is
916*0fca6ea1SDimitry Andric // handled in PPCLinuxAsmPrinter.
917*0fca6ea1SDimitry Andric if (MAI->isLittleEndian())
918*0fca6ea1SDimitry Andric return;
919*0fca6ea1SDimitry Andric const Function &F = MF->getFunction();
920*0fca6ea1SDimitry Andric unsigned Num = 0;
921*0fca6ea1SDimitry Andric (void)F.getFnAttribute("patchable-function-entry")
922*0fca6ea1SDimitry Andric .getValueAsString()
923*0fca6ea1SDimitry Andric .getAsInteger(10, Num);
924*0fca6ea1SDimitry Andric if (!Num)
925*0fca6ea1SDimitry Andric return;
926*0fca6ea1SDimitry Andric emitNops(Num);
927*0fca6ea1SDimitry Andric return;
928*0fca6ea1SDimitry Andric }
9290b57cec5SDimitry Andric case TargetOpcode::DBG_VALUE:
9300b57cec5SDimitry Andric llvm_unreachable("Should be handled target independently");
9310b57cec5SDimitry Andric case TargetOpcode::STACKMAP:
9320b57cec5SDimitry Andric return LowerSTACKMAP(SM, *MI);
9330b57cec5SDimitry Andric case TargetOpcode::PATCHPOINT:
9340b57cec5SDimitry Andric return LowerPATCHPOINT(SM, *MI);
9350b57cec5SDimitry Andric
9360b57cec5SDimitry Andric case PPC::MoveGOTtoLR: {
9370b57cec5SDimitry Andric // Transform %lr = MoveGOTtoLR
9380b57cec5SDimitry Andric // Into this: bl _GLOBAL_OFFSET_TABLE_@local-4
9390b57cec5SDimitry Andric // _GLOBAL_OFFSET_TABLE_@local-4 (instruction preceding
9400b57cec5SDimitry Andric // _GLOBAL_OFFSET_TABLE_) has exactly one instruction:
9410b57cec5SDimitry Andric // blrl
9420b57cec5SDimitry Andric // This will return the pointer to _GLOBAL_OFFSET_TABLE_@local
9430b57cec5SDimitry Andric MCSymbol *GOTSymbol =
9440b57cec5SDimitry Andric OutContext.getOrCreateSymbol(StringRef("_GLOBAL_OFFSET_TABLE_"));
9450b57cec5SDimitry Andric const MCExpr *OffsExpr =
9460b57cec5SDimitry Andric MCBinaryExpr::createSub(MCSymbolRefExpr::create(GOTSymbol,
9470b57cec5SDimitry Andric MCSymbolRefExpr::VK_PPC_LOCAL,
9480b57cec5SDimitry Andric OutContext),
9490b57cec5SDimitry Andric MCConstantExpr::create(4, OutContext),
9500b57cec5SDimitry Andric OutContext);
9510b57cec5SDimitry Andric
9520b57cec5SDimitry Andric // Emit the 'bl'.
9530b57cec5SDimitry Andric EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::BL).addExpr(OffsExpr));
9540b57cec5SDimitry Andric return;
9550b57cec5SDimitry Andric }
9560b57cec5SDimitry Andric case PPC::MovePCtoLR:
9570b57cec5SDimitry Andric case PPC::MovePCtoLR8: {
9580b57cec5SDimitry Andric // Transform %lr = MovePCtoLR
9590b57cec5SDimitry Andric // Into this, where the label is the PIC base:
9600b57cec5SDimitry Andric // bl L1$pb
9610b57cec5SDimitry Andric // L1$pb:
9620b57cec5SDimitry Andric MCSymbol *PICBase = MF->getPICBaseSymbol();
9630b57cec5SDimitry Andric
9640b57cec5SDimitry Andric // Emit the 'bl'.
9650b57cec5SDimitry Andric EmitToStreamer(*OutStreamer,
9660b57cec5SDimitry Andric MCInstBuilder(PPC::BL)
9670b57cec5SDimitry Andric // FIXME: We would like an efficient form for this, so we
9680b57cec5SDimitry Andric // don't have to do a lot of extra uniquing.
9690b57cec5SDimitry Andric .addExpr(MCSymbolRefExpr::create(PICBase, OutContext)));
9700b57cec5SDimitry Andric
9710b57cec5SDimitry Andric // Emit the label.
9725ffd83dbSDimitry Andric OutStreamer->emitLabel(PICBase);
9730b57cec5SDimitry Andric return;
9740b57cec5SDimitry Andric }
9750b57cec5SDimitry Andric case PPC::UpdateGBR: {
9760b57cec5SDimitry Andric // Transform %rd = UpdateGBR(%rt, %ri)
9770b57cec5SDimitry Andric // Into: lwz %rt, .L0$poff - .L0$pb(%ri)
9780b57cec5SDimitry Andric // add %rd, %rt, %ri
9790b57cec5SDimitry Andric // or into (if secure plt mode is on):
9800b57cec5SDimitry Andric // addis r30, r30, {.LTOC,_GLOBAL_OFFSET_TABLE} - .L0$pb@ha
9810b57cec5SDimitry Andric // addi r30, r30, {.LTOC,_GLOBAL_OFFSET_TABLE} - .L0$pb@l
9820b57cec5SDimitry Andric // Get the offset from the GOT Base Register to the GOT
9835ffd83dbSDimitry Andric LowerPPCMachineInstrToMCInst(MI, TmpInst, *this);
9840b57cec5SDimitry Andric if (Subtarget->isSecurePlt() && isPositionIndependent() ) {
9850b57cec5SDimitry Andric unsigned PICR = TmpInst.getOperand(0).getReg();
9860b57cec5SDimitry Andric MCSymbol *BaseSymbol = OutContext.getOrCreateSymbol(
9870b57cec5SDimitry Andric M->getPICLevel() == PICLevel::SmallPIC ? "_GLOBAL_OFFSET_TABLE_"
9880b57cec5SDimitry Andric : ".LTOC");
9890b57cec5SDimitry Andric const MCExpr *PB =
9900b57cec5SDimitry Andric MCSymbolRefExpr::create(MF->getPICBaseSymbol(), OutContext);
9910b57cec5SDimitry Andric
9920b57cec5SDimitry Andric const MCExpr *DeltaExpr = MCBinaryExpr::createSub(
9930b57cec5SDimitry Andric MCSymbolRefExpr::create(BaseSymbol, OutContext), PB, OutContext);
9940b57cec5SDimitry Andric
9955ffd83dbSDimitry Andric const MCExpr *DeltaHi = PPCMCExpr::createHa(DeltaExpr, OutContext);
9960b57cec5SDimitry Andric EmitToStreamer(
9970b57cec5SDimitry Andric *OutStreamer,
9980b57cec5SDimitry Andric MCInstBuilder(PPC::ADDIS).addReg(PICR).addReg(PICR).addExpr(DeltaHi));
9990b57cec5SDimitry Andric
10005ffd83dbSDimitry Andric const MCExpr *DeltaLo = PPCMCExpr::createLo(DeltaExpr, OutContext);
10010b57cec5SDimitry Andric EmitToStreamer(
10020b57cec5SDimitry Andric *OutStreamer,
10030b57cec5SDimitry Andric MCInstBuilder(PPC::ADDI).addReg(PICR).addReg(PICR).addExpr(DeltaLo));
10040b57cec5SDimitry Andric return;
10050b57cec5SDimitry Andric } else {
10060b57cec5SDimitry Andric MCSymbol *PICOffset =
10075ffd83dbSDimitry Andric MF->getInfo<PPCFunctionInfo>()->getPICOffsetSymbol(*MF);
10080b57cec5SDimitry Andric TmpInst.setOpcode(PPC::LWZ);
10090b57cec5SDimitry Andric const MCExpr *Exp =
10100b57cec5SDimitry Andric MCSymbolRefExpr::create(PICOffset, MCSymbolRefExpr::VK_None, OutContext);
10110b57cec5SDimitry Andric const MCExpr *PB =
10120b57cec5SDimitry Andric MCSymbolRefExpr::create(MF->getPICBaseSymbol(),
10130b57cec5SDimitry Andric MCSymbolRefExpr::VK_None,
10140b57cec5SDimitry Andric OutContext);
10150b57cec5SDimitry Andric const MCOperand TR = TmpInst.getOperand(1);
10160b57cec5SDimitry Andric const MCOperand PICR = TmpInst.getOperand(0);
10170b57cec5SDimitry Andric
10180b57cec5SDimitry Andric // Step 1: lwz %rt, .L$poff - .L$pb(%ri)
10190b57cec5SDimitry Andric TmpInst.getOperand(1) =
10200b57cec5SDimitry Andric MCOperand::createExpr(MCBinaryExpr::createSub(Exp, PB, OutContext));
10210b57cec5SDimitry Andric TmpInst.getOperand(0) = TR;
10220b57cec5SDimitry Andric TmpInst.getOperand(2) = PICR;
10230b57cec5SDimitry Andric EmitToStreamer(*OutStreamer, TmpInst);
10240b57cec5SDimitry Andric
10250b57cec5SDimitry Andric TmpInst.setOpcode(PPC::ADD4);
10260b57cec5SDimitry Andric TmpInst.getOperand(0) = PICR;
10270b57cec5SDimitry Andric TmpInst.getOperand(1) = TR;
10280b57cec5SDimitry Andric TmpInst.getOperand(2) = PICR;
10290b57cec5SDimitry Andric EmitToStreamer(*OutStreamer, TmpInst);
10300b57cec5SDimitry Andric return;
10310b57cec5SDimitry Andric }
10320b57cec5SDimitry Andric }
10330b57cec5SDimitry Andric case PPC::LWZtoc: {
10348bcb0991SDimitry Andric // Transform %rN = LWZtoc @op1, %r2
10355ffd83dbSDimitry Andric LowerPPCMachineInstrToMCInst(MI, TmpInst, *this);
10368bcb0991SDimitry Andric
10378bcb0991SDimitry Andric // Change the opcode to LWZ.
10380b57cec5SDimitry Andric TmpInst.setOpcode(PPC::LWZ);
10398bcb0991SDimitry Andric
10400b57cec5SDimitry Andric const MachineOperand &MO = MI->getOperand(1);
10418bcb0991SDimitry Andric assert((MO.isGlobal() || MO.isCPI() || MO.isJTI() || MO.isBlockAddress()) &&
10428bcb0991SDimitry Andric "Invalid operand for LWZtoc.");
10430b57cec5SDimitry Andric
10448bcb0991SDimitry Andric // Map the operand to its corresponding MCSymbol.
10455ffd83dbSDimitry Andric const MCSymbol *const MOSymbol = getMCSymbolForTOCPseudoMO(MO, *this);
10460b57cec5SDimitry Andric
10478bcb0991SDimitry Andric // Create a reference to the GOT entry for the symbol. The GOT entry will be
10488bcb0991SDimitry Andric // synthesized later.
10498bcb0991SDimitry Andric if (PL == PICLevel::SmallPIC && !IsAIX) {
10500b57cec5SDimitry Andric const MCExpr *Exp =
10510b57cec5SDimitry Andric MCSymbolRefExpr::create(MOSymbol, MCSymbolRefExpr::VK_GOT,
10520b57cec5SDimitry Andric OutContext);
10530b57cec5SDimitry Andric TmpInst.getOperand(1) = MCOperand::createExpr(Exp);
10548bcb0991SDimitry Andric EmitToStreamer(*OutStreamer, TmpInst);
10558bcb0991SDimitry Andric return;
10568bcb0991SDimitry Andric }
10570b57cec5SDimitry Andric
1058fe6060f1SDimitry Andric MCSymbolRefExpr::VariantKind VK = GetVKForMO(MO);
1059fe6060f1SDimitry Andric
10608bcb0991SDimitry Andric // Otherwise, use the TOC. 'TOCEntry' is a label used to reference the
10618bcb0991SDimitry Andric // storage allocated in the TOC which contains the address of
10628bcb0991SDimitry Andric // 'MOSymbol'. Said TOC entry will be synthesized later.
106306c3fb27SDimitry Andric MCSymbol *TOCEntry =
106406c3fb27SDimitry Andric lookUpOrCreateTOCEntry(MOSymbol, getTOCEntryTypeForMO(MO), VK);
10650b57cec5SDimitry Andric const MCExpr *Exp =
10668bcb0991SDimitry Andric MCSymbolRefExpr::create(TOCEntry, MCSymbolRefExpr::VK_None, OutContext);
10678bcb0991SDimitry Andric
10688bcb0991SDimitry Andric // AIX uses the label directly as the lwz displacement operand for
10698bcb0991SDimitry Andric // references into the toc section. The displacement value will be generated
10708bcb0991SDimitry Andric // relative to the toc-base.
10718bcb0991SDimitry Andric if (IsAIX) {
10728bcb0991SDimitry Andric assert(
1073*0fca6ea1SDimitry Andric getCodeModel(*Subtarget, TM, MO) == CodeModel::Small &&
10748bcb0991SDimitry Andric "This pseudo should only be selected for 32-bit small code model.");
1075fe6060f1SDimitry Andric Exp = getTOCEntryLoadingExprForXCOFF(MOSymbol, Exp, VK);
10768bcb0991SDimitry Andric TmpInst.getOperand(1) = MCOperand::createExpr(Exp);
1077fe6060f1SDimitry Andric
1078fe6060f1SDimitry Andric // Print MO for better readability
1079fe6060f1SDimitry Andric if (isVerbose())
108081ad6265SDimitry Andric OutStreamer->getCommentOS() << MO << '\n';
10818bcb0991SDimitry Andric EmitToStreamer(*OutStreamer, TmpInst);
10828bcb0991SDimitry Andric return;
10838bcb0991SDimitry Andric }
10848bcb0991SDimitry Andric
10858bcb0991SDimitry Andric // Create an explicit subtract expression between the local symbol and
10868bcb0991SDimitry Andric // '.LTOC' to manifest the toc-relative offset.
10878bcb0991SDimitry Andric const MCExpr *PB = MCSymbolRefExpr::create(
10888bcb0991SDimitry Andric OutContext.getOrCreateSymbol(Twine(".LTOC")), OutContext);
10890b57cec5SDimitry Andric Exp = MCBinaryExpr::createSub(Exp, PB, OutContext);
10900b57cec5SDimitry Andric TmpInst.getOperand(1) = MCOperand::createExpr(Exp);
10910b57cec5SDimitry Andric EmitToStreamer(*OutStreamer, TmpInst);
10920b57cec5SDimitry Andric return;
10930b57cec5SDimitry Andric }
10944824e7fdSDimitry Andric case PPC::ADDItoc:
10954824e7fdSDimitry Andric case PPC::ADDItoc8: {
1096fe6060f1SDimitry Andric assert(IsAIX && TM.getCodeModel() == CodeModel::Small &&
10974824e7fdSDimitry Andric "PseudoOp only valid for small code model AIX");
1098fe6060f1SDimitry Andric
1099*0fca6ea1SDimitry Andric // Transform %rN = ADDItoc/8 %r2, @op1.
1100fe6060f1SDimitry Andric LowerPPCMachineInstrToMCInst(MI, TmpInst, *this);
1101fe6060f1SDimitry Andric
1102fe6060f1SDimitry Andric // Change the opcode to load address.
11034824e7fdSDimitry Andric TmpInst.setOpcode((!IsPPC64) ? (PPC::LA) : (PPC::LA8));
1104fe6060f1SDimitry Andric
1105*0fca6ea1SDimitry Andric const MachineOperand &MO = MI->getOperand(2);
11064824e7fdSDimitry Andric assert(MO.isGlobal() && "Invalid operand for ADDItoc[8].");
1107fe6060f1SDimitry Andric
1108fe6060f1SDimitry Andric // Map the operand to its corresponding MCSymbol.
1109fe6060f1SDimitry Andric const MCSymbol *const MOSymbol = getMCSymbolForTOCPseudoMO(MO, *this);
1110fe6060f1SDimitry Andric
1111fe6060f1SDimitry Andric const MCExpr *Exp =
1112fe6060f1SDimitry Andric MCSymbolRefExpr::create(MOSymbol, MCSymbolRefExpr::VK_None, OutContext);
1113fe6060f1SDimitry Andric
1114fe6060f1SDimitry Andric TmpInst.getOperand(2) = MCOperand::createExpr(Exp);
1115fe6060f1SDimitry Andric EmitToStreamer(*OutStreamer, TmpInst);
1116fe6060f1SDimitry Andric return;
1117fe6060f1SDimitry Andric }
11180b57cec5SDimitry Andric case PPC::LDtocJTI:
11190b57cec5SDimitry Andric case PPC::LDtocCPT:
11200b57cec5SDimitry Andric case PPC::LDtocBA:
11210b57cec5SDimitry Andric case PPC::LDtoc: {
11220b57cec5SDimitry Andric // Transform %x3 = LDtoc @min1, %x2
11235ffd83dbSDimitry Andric LowerPPCMachineInstrToMCInst(MI, TmpInst, *this);
11240b57cec5SDimitry Andric
11258bcb0991SDimitry Andric // Change the opcode to LD.
11260b57cec5SDimitry Andric TmpInst.setOpcode(PPC::LD);
11278bcb0991SDimitry Andric
11280b57cec5SDimitry Andric const MachineOperand &MO = MI->getOperand(1);
11298bcb0991SDimitry Andric assert((MO.isGlobal() || MO.isCPI() || MO.isJTI() || MO.isBlockAddress()) &&
11308bcb0991SDimitry Andric "Invalid operand!");
11310b57cec5SDimitry Andric
1132e8d8bef9SDimitry Andric // Map the operand to its corresponding MCSymbol.
1133e8d8bef9SDimitry Andric const MCSymbol *const MOSymbol = getMCSymbolForTOCPseudoMO(MO, *this);
1134e8d8bef9SDimitry Andric
1135fe6060f1SDimitry Andric MCSymbolRefExpr::VariantKind VK = GetVKForMO(MO);
1136fe6060f1SDimitry Andric
11378bcb0991SDimitry Andric // Map the machine operand to its corresponding MCSymbol, then map the
11388bcb0991SDimitry Andric // global address operand to be a reference to the TOC entry we will
11398bcb0991SDimitry Andric // synthesize later.
114006c3fb27SDimitry Andric MCSymbol *TOCEntry =
114106c3fb27SDimitry Andric lookUpOrCreateTOCEntry(MOSymbol, getTOCEntryTypeForMO(MO), VK);
11420b57cec5SDimitry Andric
1143fe6060f1SDimitry Andric MCSymbolRefExpr::VariantKind VKExpr =
11448bcb0991SDimitry Andric IsAIX ? MCSymbolRefExpr::VK_None : MCSymbolRefExpr::VK_PPC_TOC;
1145fe6060f1SDimitry Andric const MCExpr *Exp = MCSymbolRefExpr::create(TOCEntry, VKExpr, OutContext);
1146e8d8bef9SDimitry Andric TmpInst.getOperand(1) = MCOperand::createExpr(
1147fe6060f1SDimitry Andric IsAIX ? getTOCEntryLoadingExprForXCOFF(MOSymbol, Exp, VK) : Exp);
1148fe6060f1SDimitry Andric
1149fe6060f1SDimitry Andric // Print MO for better readability
1150fe6060f1SDimitry Andric if (isVerbose() && IsAIX)
115181ad6265SDimitry Andric OutStreamer->getCommentOS() << MO << '\n';
11528bcb0991SDimitry Andric EmitToStreamer(*OutStreamer, TmpInst);
11538bcb0991SDimitry Andric return;
11548bcb0991SDimitry Andric }
11558bcb0991SDimitry Andric case PPC::ADDIStocHA: {
1156*0fca6ea1SDimitry Andric const MachineOperand &MO = MI->getOperand(2);
1157*0fca6ea1SDimitry Andric
1158*0fca6ea1SDimitry Andric assert((MO.isGlobal() || MO.isCPI() || MO.isJTI() || MO.isBlockAddress()) &&
1159*0fca6ea1SDimitry Andric "Invalid operand for ADDIStocHA.");
1160*0fca6ea1SDimitry Andric assert((IsAIX && !IsPPC64 &&
1161*0fca6ea1SDimitry Andric getCodeModel(*Subtarget, TM, MO) == CodeModel::Large) &&
11628bcb0991SDimitry Andric "This pseudo should only be selected for 32-bit large code model on"
11638bcb0991SDimitry Andric " AIX.");
11648bcb0991SDimitry Andric
11658bcb0991SDimitry Andric // Transform %rd = ADDIStocHA %rA, @sym(%r2)
11665ffd83dbSDimitry Andric LowerPPCMachineInstrToMCInst(MI, TmpInst, *this);
11678bcb0991SDimitry Andric
11688bcb0991SDimitry Andric // Change the opcode to ADDIS.
11698bcb0991SDimitry Andric TmpInst.setOpcode(PPC::ADDIS);
11708bcb0991SDimitry Andric
11718bcb0991SDimitry Andric // Map the machine operand to its corresponding MCSymbol.
11725ffd83dbSDimitry Andric MCSymbol *MOSymbol = getMCSymbolForTOCPseudoMO(MO, *this);
11738bcb0991SDimitry Andric
1174fe6060f1SDimitry Andric MCSymbolRefExpr::VariantKind VK = GetVKForMO(MO);
1175fe6060f1SDimitry Andric
1176*0fca6ea1SDimitry Andric // Map the global address operand to be a reference to the TOC entry we
1177*0fca6ea1SDimitry Andric // will synthesize later. 'TOCEntry' is a label used to reference the
1178*0fca6ea1SDimitry Andric // storage allocated in the TOC which contains the address of 'MOSymbol'.
1179*0fca6ea1SDimitry Andric // If the symbol does not have the toc-data attribute, then we create the
1180*0fca6ea1SDimitry Andric // TOC entry on AIX. If the toc-data attribute is used, the TOC entry
1181*0fca6ea1SDimitry Andric // contains the data rather than the address of the MOSymbol.
1182*0fca6ea1SDimitry Andric if ( {
1183*0fca6ea1SDimitry Andric if (!MO.isGlobal())
1184*0fca6ea1SDimitry Andric return false;
1185*0fca6ea1SDimitry Andric
1186*0fca6ea1SDimitry Andric const GlobalVariable *GV = dyn_cast<GlobalVariable>(MO.getGlobal());
1187*0fca6ea1SDimitry Andric if (!GV)
1188*0fca6ea1SDimitry Andric return false;
1189*0fca6ea1SDimitry Andric return GV->hasAttribute("toc-data");
1190*0fca6ea1SDimitry Andric }(MO)) {
1191*0fca6ea1SDimitry Andric MOSymbol = lookUpOrCreateTOCEntry(MOSymbol, getTOCEntryTypeForMO(MO), VK);
1192*0fca6ea1SDimitry Andric }
1193*0fca6ea1SDimitry Andric
1194*0fca6ea1SDimitry Andric const MCExpr *Exp = MCSymbolRefExpr::create(
1195*0fca6ea1SDimitry Andric MOSymbol, MCSymbolRefExpr::VK_PPC_U, OutContext);
11968bcb0991SDimitry Andric TmpInst.getOperand(2) = MCOperand::createExpr(Exp);
11978bcb0991SDimitry Andric EmitToStreamer(*OutStreamer, TmpInst);
11988bcb0991SDimitry Andric return;
11998bcb0991SDimitry Andric }
12008bcb0991SDimitry Andric case PPC::LWZtocL: {
1201*0fca6ea1SDimitry Andric const MachineOperand &MO = MI->getOperand(1);
1202*0fca6ea1SDimitry Andric
1203*0fca6ea1SDimitry Andric assert((MO.isGlobal() || MO.isCPI() || MO.isJTI() || MO.isBlockAddress()) &&
1204*0fca6ea1SDimitry Andric "Invalid operand for LWZtocL.");
1205*0fca6ea1SDimitry Andric assert(IsAIX && !IsPPC64 &&
1206*0fca6ea1SDimitry Andric getCodeModel(*Subtarget, TM, MO) == CodeModel::Large &&
12078bcb0991SDimitry Andric "This pseudo should only be selected for 32-bit large code model on"
12088bcb0991SDimitry Andric " AIX.");
12098bcb0991SDimitry Andric
12108bcb0991SDimitry Andric // Transform %rd = LWZtocL @sym, %rs.
12115ffd83dbSDimitry Andric LowerPPCMachineInstrToMCInst(MI, TmpInst, *this);
12128bcb0991SDimitry Andric
12138bcb0991SDimitry Andric // Change the opcode to lwz.
12148bcb0991SDimitry Andric TmpInst.setOpcode(PPC::LWZ);
12158bcb0991SDimitry Andric
12168bcb0991SDimitry Andric // Map the machine operand to its corresponding MCSymbol.
12175ffd83dbSDimitry Andric MCSymbol *MOSymbol = getMCSymbolForTOCPseudoMO(MO, *this);
12188bcb0991SDimitry Andric
1219fe6060f1SDimitry Andric MCSymbolRefExpr::VariantKind VK = GetVKForMO(MO);
1220fe6060f1SDimitry Andric
12218bcb0991SDimitry Andric // Always use TOC on AIX. Map the global address operand to be a reference
12228bcb0991SDimitry Andric // to the TOC entry we will synthesize later. 'TOCEntry' is a label used to
12238bcb0991SDimitry Andric // reference the storage allocated in the TOC which contains the address of
12248bcb0991SDimitry Andric // 'MOSymbol'.
122506c3fb27SDimitry Andric MCSymbol *TOCEntry =
122606c3fb27SDimitry Andric lookUpOrCreateTOCEntry(MOSymbol, getTOCEntryTypeForMO(MO), VK);
12278bcb0991SDimitry Andric const MCExpr *Exp = MCSymbolRefExpr::create(TOCEntry,
12288bcb0991SDimitry Andric MCSymbolRefExpr::VK_PPC_L,
12290b57cec5SDimitry Andric OutContext);
12300b57cec5SDimitry Andric TmpInst.getOperand(1) = MCOperand::createExpr(Exp);
12310b57cec5SDimitry Andric EmitToStreamer(*OutStreamer, TmpInst);
12320b57cec5SDimitry Andric return;
12330b57cec5SDimitry Andric }
12348bcb0991SDimitry Andric case PPC::ADDIStocHA8: {
12358bcb0991SDimitry Andric // Transform %xd = ADDIStocHA8 %x2, @sym
12365ffd83dbSDimitry Andric LowerPPCMachineInstrToMCInst(MI, TmpInst, *this);
12370b57cec5SDimitry Andric
12388bcb0991SDimitry Andric // Change the opcode to ADDIS8. If the global address is the address of
12398bcb0991SDimitry Andric // an external symbol, is a jump table address, is a block address, or is a
12408bcb0991SDimitry Andric // constant pool index with large code model enabled, then generate a TOC
12418bcb0991SDimitry Andric // entry and reference that. Otherwise, reference the symbol directly.
12420b57cec5SDimitry Andric TmpInst.setOpcode(PPC::ADDIS8);
12438bcb0991SDimitry Andric
12440b57cec5SDimitry Andric const MachineOperand &MO = MI->getOperand(2);
12458bcb0991SDimitry Andric assert((MO.isGlobal() || MO.isCPI() || MO.isJTI() || MO.isBlockAddress()) &&
12468bcb0991SDimitry Andric "Invalid operand for ADDIStocHA8!");
12470b57cec5SDimitry Andric
12485ffd83dbSDimitry Andric const MCSymbol *MOSymbol = getMCSymbolForTOCPseudoMO(MO, *this);
12490b57cec5SDimitry Andric
1250fe6060f1SDimitry Andric MCSymbolRefExpr::VariantKind VK = GetVKForMO(MO);
1251fe6060f1SDimitry Andric
12528bcb0991SDimitry Andric const bool GlobalToc =
12538bcb0991SDimitry Andric MO.isGlobal() && Subtarget->isGVIndirectSymbol(MO.getGlobal());
1254*0fca6ea1SDimitry Andric
1255*0fca6ea1SDimitry Andric const CodeModel::Model CM =
1256*0fca6ea1SDimitry Andric IsAIX ? getCodeModel(*Subtarget, TM, MO) : TM.getCodeModel();
1257*0fca6ea1SDimitry Andric
12580b57cec5SDimitry Andric if (GlobalToc || MO.isJTI() || MO.isBlockAddress() ||
1259*0fca6ea1SDimitry Andric (MO.isCPI() && CM == CodeModel::Large))
126006c3fb27SDimitry Andric MOSymbol = lookUpOrCreateTOCEntry(MOSymbol, getTOCEntryTypeForMO(MO), VK);
12610b57cec5SDimitry Andric
1262fe6060f1SDimitry Andric VK = IsAIX ? MCSymbolRefExpr::VK_PPC_U : MCSymbolRefExpr::VK_PPC_TOC_HA;
12638bcb0991SDimitry Andric
12640b57cec5SDimitry Andric const MCExpr *Exp =
12658bcb0991SDimitry Andric MCSymbolRefExpr::create(MOSymbol, VK, OutContext);
12660b57cec5SDimitry Andric
12670b57cec5SDimitry Andric if (!MO.isJTI() && MO.getOffset())
12680b57cec5SDimitry Andric Exp = MCBinaryExpr::createAdd(Exp,
12690b57cec5SDimitry Andric MCConstantExpr::create(MO.getOffset(),
12700b57cec5SDimitry Andric OutContext),
12710b57cec5SDimitry Andric OutContext);
12720b57cec5SDimitry Andric
12730b57cec5SDimitry Andric TmpInst.getOperand(2) = MCOperand::createExpr(Exp);
12740b57cec5SDimitry Andric EmitToStreamer(*OutStreamer, TmpInst);
12750b57cec5SDimitry Andric return;
12760b57cec5SDimitry Andric }
12770b57cec5SDimitry Andric case PPC::LDtocL: {
12788bcb0991SDimitry Andric // Transform %xd = LDtocL @sym, %xs
12795ffd83dbSDimitry Andric LowerPPCMachineInstrToMCInst(MI, TmpInst, *this);
12808bcb0991SDimitry Andric
12818bcb0991SDimitry Andric // Change the opcode to LD. If the global address is the address of
12828bcb0991SDimitry Andric // an external symbol, is a jump table address, is a block address, or is
12838bcb0991SDimitry Andric // a constant pool index with large code model enabled, then generate a
12848bcb0991SDimitry Andric // TOC entry and reference that. Otherwise, reference the symbol directly.
12850b57cec5SDimitry Andric TmpInst.setOpcode(PPC::LD);
12868bcb0991SDimitry Andric
12870b57cec5SDimitry Andric const MachineOperand &MO = MI->getOperand(1);
12880b57cec5SDimitry Andric assert((MO.isGlobal() || MO.isCPI() || MO.isJTI() ||
12890b57cec5SDimitry Andric MO.isBlockAddress()) &&
12900b57cec5SDimitry Andric "Invalid operand for LDtocL!");
12910b57cec5SDimitry Andric
12928bcb0991SDimitry Andric LLVM_DEBUG(assert(
12938bcb0991SDimitry Andric (!MO.isGlobal() || Subtarget->isGVIndirectSymbol(MO.getGlobal())) &&
12940b57cec5SDimitry Andric "LDtocL used on symbol that could be accessed directly is "
12958bcb0991SDimitry Andric "invalid. Must match ADDIStocHA8."));
12960b57cec5SDimitry Andric
12975ffd83dbSDimitry Andric const MCSymbol *MOSymbol = getMCSymbolForTOCPseudoMO(MO, *this);
12988bcb0991SDimitry Andric
1299fe6060f1SDimitry Andric MCSymbolRefExpr::VariantKind VK = GetVKForMO(MO);
1300*0fca6ea1SDimitry Andric CodeModel::Model CM =
1301*0fca6ea1SDimitry Andric IsAIX ? getCodeModel(*Subtarget, TM, MO) : TM.getCodeModel();
1302*0fca6ea1SDimitry Andric if (!MO.isCPI() || CM == CodeModel::Large)
130306c3fb27SDimitry Andric MOSymbol = lookUpOrCreateTOCEntry(MOSymbol, getTOCEntryTypeForMO(MO), VK);
1304fe6060f1SDimitry Andric
1305fe6060f1SDimitry Andric VK = IsAIX ? MCSymbolRefExpr::VK_PPC_L : MCSymbolRefExpr::VK_PPC_TOC_LO;
13060b57cec5SDimitry Andric const MCExpr *Exp =
13078bcb0991SDimitry Andric MCSymbolRefExpr::create(MOSymbol, VK, OutContext);
13080b57cec5SDimitry Andric TmpInst.getOperand(1) = MCOperand::createExpr(Exp);
13090b57cec5SDimitry Andric EmitToStreamer(*OutStreamer, TmpInst);
13100b57cec5SDimitry Andric return;
13110b57cec5SDimitry Andric }
1312*0fca6ea1SDimitry Andric case PPC::ADDItocL:
1313*0fca6ea1SDimitry Andric case PPC::ADDItocL8: {
13140b57cec5SDimitry Andric // Transform %xd = ADDItocL %xs, @sym
13155ffd83dbSDimitry Andric LowerPPCMachineInstrToMCInst(MI, TmpInst, *this);
13160b57cec5SDimitry Andric
1317*0fca6ea1SDimitry Andric unsigned Op = MI->getOpcode();
1318*0fca6ea1SDimitry Andric
1319*0fca6ea1SDimitry Andric // Change the opcode to load address for toc-data.
1320*0fca6ea1SDimitry Andric // ADDItocL is only used for 32-bit toc-data on AIX and will always use LA.
1321*0fca6ea1SDimitry Andric TmpInst.setOpcode(Op == PPC::ADDItocL8 ? (IsAIX ? PPC::LA8 : PPC::ADDI8)
1322*0fca6ea1SDimitry Andric : PPC::LA);
13230b57cec5SDimitry Andric
13248bcb0991SDimitry Andric const MachineOperand &MO = MI->getOperand(2);
1325*0fca6ea1SDimitry Andric assert((Op == PPC::ADDItocL8)
1326*0fca6ea1SDimitry Andric ? (MO.isGlobal() || MO.isCPI())
1327*0fca6ea1SDimitry Andric : MO.isGlobal() && "Invalid operand for ADDItocL8.");
1328*0fca6ea1SDimitry Andric assert(!(MO.isGlobal() && Subtarget->isGVIndirectSymbol(MO.getGlobal())) &&
1329*0fca6ea1SDimitry Andric "Interposable definitions must use indirect accesses.");
13308bcb0991SDimitry Andric
1331*0fca6ea1SDimitry Andric // Map the operand to its corresponding MCSymbol.
1332*0fca6ea1SDimitry Andric const MCSymbol *const MOSymbol = getMCSymbolForTOCPseudoMO(MO, *this);
13330b57cec5SDimitry Andric
1334*0fca6ea1SDimitry Andric const MCExpr *Exp = MCSymbolRefExpr::create(
1335*0fca6ea1SDimitry Andric MOSymbol,
1336*0fca6ea1SDimitry Andric IsAIX ? MCSymbolRefExpr::VK_PPC_L : MCSymbolRefExpr::VK_PPC_TOC_LO,
1337*0fca6ea1SDimitry Andric OutContext);
1338*0fca6ea1SDimitry Andric
13390b57cec5SDimitry Andric TmpInst.getOperand(2) = MCOperand::createExpr(Exp);
13400b57cec5SDimitry Andric EmitToStreamer(*OutStreamer, TmpInst);
13410b57cec5SDimitry Andric return;
13420b57cec5SDimitry Andric }
13430b57cec5SDimitry Andric case PPC::ADDISgotTprelHA: {
13440b57cec5SDimitry Andric // Transform: %xd = ADDISgotTprelHA %x2, @sym
13450b57cec5SDimitry Andric // Into: %xd = ADDIS8 %x2, sym@got@tlsgd@ha
13468bcb0991SDimitry Andric assert(IsPPC64 && "Not supported for 32-bit PowerPC");
13470b57cec5SDimitry Andric const MachineOperand &MO = MI->getOperand(2);
13480b57cec5SDimitry Andric const GlobalValue *GValue = MO.getGlobal();
13490b57cec5SDimitry Andric MCSymbol *MOSymbol = getSymbol(GValue);
13500b57cec5SDimitry Andric const MCExpr *SymGotTprel =
13510b57cec5SDimitry Andric MCSymbolRefExpr::create(MOSymbol, MCSymbolRefExpr::VK_PPC_GOT_TPREL_HA,
13520b57cec5SDimitry Andric OutContext);
13530b57cec5SDimitry Andric EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::ADDIS8)
13540b57cec5SDimitry Andric .addReg(MI->getOperand(0).getReg())
13550b57cec5SDimitry Andric .addReg(MI->getOperand(1).getReg())
13560b57cec5SDimitry Andric .addExpr(SymGotTprel));
13570b57cec5SDimitry Andric return;
13580b57cec5SDimitry Andric }
13590b57cec5SDimitry Andric case PPC::LDgotTprelL:
13600b57cec5SDimitry Andric case PPC::LDgotTprelL32: {
13610b57cec5SDimitry Andric // Transform %xd = LDgotTprelL @sym, %xs
13625ffd83dbSDimitry Andric LowerPPCMachineInstrToMCInst(MI, TmpInst, *this);
13630b57cec5SDimitry Andric
13640b57cec5SDimitry Andric // Change the opcode to LD.
13658bcb0991SDimitry Andric TmpInst.setOpcode(IsPPC64 ? PPC::LD : PPC::LWZ);
13660b57cec5SDimitry Andric const MachineOperand &MO = MI->getOperand(1);
13670b57cec5SDimitry Andric const GlobalValue *GValue = MO.getGlobal();
13680b57cec5SDimitry Andric MCSymbol *MOSymbol = getSymbol(GValue);
13698bcb0991SDimitry Andric const MCExpr *Exp = MCSymbolRefExpr::create(
13708bcb0991SDimitry Andric MOSymbol, IsPPC64 ? MCSymbolRefExpr::VK_PPC_GOT_TPREL_LO
13710b57cec5SDimitry Andric : MCSymbolRefExpr::VK_PPC_GOT_TPREL,
13720b57cec5SDimitry Andric OutContext);
13730b57cec5SDimitry Andric TmpInst.getOperand(1) = MCOperand::createExpr(Exp);
13740b57cec5SDimitry Andric EmitToStreamer(*OutStreamer, TmpInst);
13750b57cec5SDimitry Andric return;
13760b57cec5SDimitry Andric }
13770b57cec5SDimitry Andric
13780b57cec5SDimitry Andric case PPC::PPC32PICGOT: {
13790b57cec5SDimitry Andric MCSymbol *GOTSymbol = OutContext.getOrCreateSymbol(StringRef("_GLOBAL_OFFSET_TABLE_"));
13800b57cec5SDimitry Andric MCSymbol *GOTRef = OutContext.createTempSymbol();
13810b57cec5SDimitry Andric MCSymbol *NextInstr = OutContext.createTempSymbol();
13820b57cec5SDimitry Andric
13830b57cec5SDimitry Andric EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::BL)
13840b57cec5SDimitry Andric // FIXME: We would like an efficient form for this, so we don't have to do
13850b57cec5SDimitry Andric // a lot of extra uniquing.
13860b57cec5SDimitry Andric .addExpr(MCSymbolRefExpr::create(NextInstr, OutContext)));
13870b57cec5SDimitry Andric const MCExpr *OffsExpr =
13880b57cec5SDimitry Andric MCBinaryExpr::createSub(MCSymbolRefExpr::create(GOTSymbol, OutContext),
13890b57cec5SDimitry Andric MCSymbolRefExpr::create(GOTRef, OutContext),
13900b57cec5SDimitry Andric OutContext);
13915ffd83dbSDimitry Andric OutStreamer->emitLabel(GOTRef);
13925ffd83dbSDimitry Andric OutStreamer->emitValue(OffsExpr, 4);
13935ffd83dbSDimitry Andric OutStreamer->emitLabel(NextInstr);
13940b57cec5SDimitry Andric EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::MFLR)
13950b57cec5SDimitry Andric .addReg(MI->getOperand(0).getReg()));
13960b57cec5SDimitry Andric EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::LWZ)
13970b57cec5SDimitry Andric .addReg(MI->getOperand(1).getReg())
13980b57cec5SDimitry Andric .addImm(0)
13990b57cec5SDimitry Andric .addReg(MI->getOperand(0).getReg()));
14000b57cec5SDimitry Andric EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::ADD4)
14010b57cec5SDimitry Andric .addReg(MI->getOperand(0).getReg())
14020b57cec5SDimitry Andric .addReg(MI->getOperand(1).getReg())
14030b57cec5SDimitry Andric .addReg(MI->getOperand(0).getReg()));
14040b57cec5SDimitry Andric return;
14050b57cec5SDimitry Andric }
14060b57cec5SDimitry Andric case PPC::PPC32GOT: {
14070b57cec5SDimitry Andric MCSymbol *GOTSymbol =
14080b57cec5SDimitry Andric OutContext.getOrCreateSymbol(StringRef("_GLOBAL_OFFSET_TABLE_"));
14090b57cec5SDimitry Andric const MCExpr *SymGotTlsL = MCSymbolRefExpr::create(
14100b57cec5SDimitry Andric GOTSymbol, MCSymbolRefExpr::VK_PPC_LO, OutContext);
14110b57cec5SDimitry Andric const MCExpr *SymGotTlsHA = MCSymbolRefExpr::create(
14120b57cec5SDimitry Andric GOTSymbol, MCSymbolRefExpr::VK_PPC_HA, OutContext);
14130b57cec5SDimitry Andric EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::LI)
14140b57cec5SDimitry Andric .addReg(MI->getOperand(0).getReg())
14150b57cec5SDimitry Andric .addExpr(SymGotTlsL));
14160b57cec5SDimitry Andric EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::ADDIS)
14170b57cec5SDimitry Andric .addReg(MI->getOperand(0).getReg())
14180b57cec5SDimitry Andric .addReg(MI->getOperand(0).getReg())
14190b57cec5SDimitry Andric .addExpr(SymGotTlsHA));
14200b57cec5SDimitry Andric return;
14210b57cec5SDimitry Andric }
14220b57cec5SDimitry Andric case PPC::ADDIStlsgdHA: {
14230b57cec5SDimitry Andric // Transform: %xd = ADDIStlsgdHA %x2, @sym
14240b57cec5SDimitry Andric // Into: %xd = ADDIS8 %x2, sym@got@tlsgd@ha
14258bcb0991SDimitry Andric assert(IsPPC64 && "Not supported for 32-bit PowerPC");
14260b57cec5SDimitry Andric const MachineOperand &MO = MI->getOperand(2);
14270b57cec5SDimitry Andric const GlobalValue *GValue = MO.getGlobal();
14280b57cec5SDimitry Andric MCSymbol *MOSymbol = getSymbol(GValue);
14290b57cec5SDimitry Andric const MCExpr *SymGotTlsGD =
14300b57cec5SDimitry Andric MCSymbolRefExpr::create(MOSymbol, MCSymbolRefExpr::VK_PPC_GOT_TLSGD_HA,
14310b57cec5SDimitry Andric OutContext);
14320b57cec5SDimitry Andric EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::ADDIS8)
14330b57cec5SDimitry Andric .addReg(MI->getOperand(0).getReg())
14340b57cec5SDimitry Andric .addReg(MI->getOperand(1).getReg())
14350b57cec5SDimitry Andric .addExpr(SymGotTlsGD));
14360b57cec5SDimitry Andric return;
14370b57cec5SDimitry Andric }
14380b57cec5SDimitry Andric case PPC::ADDItlsgdL:
14390b57cec5SDimitry Andric // Transform: %xd = ADDItlsgdL %xs, @sym
14400b57cec5SDimitry Andric // Into: %xd = ADDI8 %xs, sym@got@tlsgd@l
14410b57cec5SDimitry Andric case PPC::ADDItlsgdL32: {
14420b57cec5SDimitry Andric // Transform: %rd = ADDItlsgdL32 %rs, @sym
14430b57cec5SDimitry Andric // Into: %rd = ADDI %rs, sym@got@tlsgd
14440b57cec5SDimitry Andric const MachineOperand &MO = MI->getOperand(2);
14450b57cec5SDimitry Andric const GlobalValue *GValue = MO.getGlobal();
14460b57cec5SDimitry Andric MCSymbol *MOSymbol = getSymbol(GValue);
14470b57cec5SDimitry Andric const MCExpr *SymGotTlsGD = MCSymbolRefExpr::create(
14488bcb0991SDimitry Andric MOSymbol, IsPPC64 ? MCSymbolRefExpr::VK_PPC_GOT_TLSGD_LO
14490b57cec5SDimitry Andric : MCSymbolRefExpr::VK_PPC_GOT_TLSGD,
14500b57cec5SDimitry Andric OutContext);
14510b57cec5SDimitry Andric EmitToStreamer(*OutStreamer,
14528bcb0991SDimitry Andric MCInstBuilder(IsPPC64 ? PPC::ADDI8 : PPC::ADDI)
14530b57cec5SDimitry Andric .addReg(MI->getOperand(0).getReg())
14540b57cec5SDimitry Andric .addReg(MI->getOperand(1).getReg())
14550b57cec5SDimitry Andric .addExpr(SymGotTlsGD));
14560b57cec5SDimitry Andric return;
14570b57cec5SDimitry Andric }
1458*0fca6ea1SDimitry Andric case PPC::GETtlsMOD32AIX:
1459*0fca6ea1SDimitry Andric case PPC::GETtlsMOD64AIX:
1460*0fca6ea1SDimitry Andric // Transform: %r3 = GETtlsMODNNAIX %r3 (for NN == 32/64).
1461*0fca6ea1SDimitry Andric // Into: BLA .__tls_get_mod()
1462*0fca6ea1SDimitry Andric // Input parameter is a module handle (_$TLSML[TC]@ml) for all variables.
14630b57cec5SDimitry Andric case PPC::GETtlsADDR:
14640b57cec5SDimitry Andric // Transform: %x3 = GETtlsADDR %x3, @sym
14650b57cec5SDimitry Andric // Into: BL8_NOP_TLS __tls_get_addr(sym at tlsgd)
1466e8d8bef9SDimitry Andric case PPC::GETtlsADDRPCREL:
1467fe6060f1SDimitry Andric case PPC::GETtlsADDR32AIX:
1468fe6060f1SDimitry Andric case PPC::GETtlsADDR64AIX:
1469fe6060f1SDimitry Andric // Transform: %r3 = GETtlsADDRNNAIX %r3, %r4 (for NN == 32/64).
1470fe6060f1SDimitry Andric // Into: BLA .__tls_get_addr()
1471fe6060f1SDimitry Andric // Unlike on Linux, there is no symbol or relocation needed for this call.
14720b57cec5SDimitry Andric case PPC::GETtlsADDR32: {
14730b57cec5SDimitry Andric // Transform: %r3 = GETtlsADDR32 %r3, @sym
14740b57cec5SDimitry Andric // Into: BL_TLS __tls_get_addr(sym at tlsgd)@PLT
14750b57cec5SDimitry Andric EmitTlsCall(MI, MCSymbolRefExpr::VK_PPC_TLSGD);
14760b57cec5SDimitry Andric return;
14770b57cec5SDimitry Andric }
147806c3fb27SDimitry Andric case PPC::GETtlsTpointer32AIX: {
147906c3fb27SDimitry Andric // Transform: %r3 = GETtlsTpointer32AIX
148006c3fb27SDimitry Andric // Into: BLA .__get_tpointer()
148106c3fb27SDimitry Andric EmitAIXTlsCallHelper(MI);
148206c3fb27SDimitry Andric return;
148306c3fb27SDimitry Andric }
14840b57cec5SDimitry Andric case PPC::ADDIStlsldHA: {
14850b57cec5SDimitry Andric // Transform: %xd = ADDIStlsldHA %x2, @sym
14860b57cec5SDimitry Andric // Into: %xd = ADDIS8 %x2, sym@got@tlsld@ha
14878bcb0991SDimitry Andric assert(IsPPC64 && "Not supported for 32-bit PowerPC");
14880b57cec5SDimitry Andric const MachineOperand &MO = MI->getOperand(2);
14890b57cec5SDimitry Andric const GlobalValue *GValue = MO.getGlobal();
14900b57cec5SDimitry Andric MCSymbol *MOSymbol = getSymbol(GValue);
14910b57cec5SDimitry Andric const MCExpr *SymGotTlsLD =
14920b57cec5SDimitry Andric MCSymbolRefExpr::create(MOSymbol, MCSymbolRefExpr::VK_PPC_GOT_TLSLD_HA,
14930b57cec5SDimitry Andric OutContext);
14940b57cec5SDimitry Andric EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::ADDIS8)
14950b57cec5SDimitry Andric .addReg(MI->getOperand(0).getReg())
14960b57cec5SDimitry Andric .addReg(MI->getOperand(1).getReg())
14970b57cec5SDimitry Andric .addExpr(SymGotTlsLD));
14980b57cec5SDimitry Andric return;
14990b57cec5SDimitry Andric }
15000b57cec5SDimitry Andric case PPC::ADDItlsldL:
15010b57cec5SDimitry Andric // Transform: %xd = ADDItlsldL %xs, @sym
15020b57cec5SDimitry Andric // Into: %xd = ADDI8 %xs, sym@got@tlsld@l
15030b57cec5SDimitry Andric case PPC::ADDItlsldL32: {
15040b57cec5SDimitry Andric // Transform: %rd = ADDItlsldL32 %rs, @sym
15050b57cec5SDimitry Andric // Into: %rd = ADDI %rs, sym@got@tlsld
15060b57cec5SDimitry Andric const MachineOperand &MO = MI->getOperand(2);
15070b57cec5SDimitry Andric const GlobalValue *GValue = MO.getGlobal();
15080b57cec5SDimitry Andric MCSymbol *MOSymbol = getSymbol(GValue);
15090b57cec5SDimitry Andric const MCExpr *SymGotTlsLD = MCSymbolRefExpr::create(
15108bcb0991SDimitry Andric MOSymbol, IsPPC64 ? MCSymbolRefExpr::VK_PPC_GOT_TLSLD_LO
15110b57cec5SDimitry Andric : MCSymbolRefExpr::VK_PPC_GOT_TLSLD,
15120b57cec5SDimitry Andric OutContext);
15130b57cec5SDimitry Andric EmitToStreamer(*OutStreamer,
15148bcb0991SDimitry Andric MCInstBuilder(IsPPC64 ? PPC::ADDI8 : PPC::ADDI)
15150b57cec5SDimitry Andric .addReg(MI->getOperand(0).getReg())
15160b57cec5SDimitry Andric .addReg(MI->getOperand(1).getReg())
15170b57cec5SDimitry Andric .addExpr(SymGotTlsLD));
15180b57cec5SDimitry Andric return;
15190b57cec5SDimitry Andric }
15200b57cec5SDimitry Andric case PPC::GETtlsldADDR:
15210b57cec5SDimitry Andric // Transform: %x3 = GETtlsldADDR %x3, @sym
15220b57cec5SDimitry Andric // Into: BL8_NOP_TLS __tls_get_addr(sym at tlsld)
1523e8d8bef9SDimitry Andric case PPC::GETtlsldADDRPCREL:
15240b57cec5SDimitry Andric case PPC::GETtlsldADDR32: {
15250b57cec5SDimitry Andric // Transform: %r3 = GETtlsldADDR32 %r3, @sym
15260b57cec5SDimitry Andric // Into: BL_TLS __tls_get_addr(sym at tlsld)@PLT
15270b57cec5SDimitry Andric EmitTlsCall(MI, MCSymbolRefExpr::VK_PPC_TLSLD);
15280b57cec5SDimitry Andric return;
15290b57cec5SDimitry Andric }
15300b57cec5SDimitry Andric case PPC::ADDISdtprelHA:
15310b57cec5SDimitry Andric // Transform: %xd = ADDISdtprelHA %xs, @sym
15320b57cec5SDimitry Andric // Into: %xd = ADDIS8 %xs, sym@dtprel@ha
15330b57cec5SDimitry Andric case PPC::ADDISdtprelHA32: {
15340b57cec5SDimitry Andric // Transform: %rd = ADDISdtprelHA32 %rs, @sym
15350b57cec5SDimitry Andric // Into: %rd = ADDIS %rs, sym@dtprel@ha
15360b57cec5SDimitry Andric const MachineOperand &MO = MI->getOperand(2);
15370b57cec5SDimitry Andric const GlobalValue *GValue = MO.getGlobal();
15380b57cec5SDimitry Andric MCSymbol *MOSymbol = getSymbol(GValue);
15390b57cec5SDimitry Andric const MCExpr *SymDtprel =
15400b57cec5SDimitry Andric MCSymbolRefExpr::create(MOSymbol, MCSymbolRefExpr::VK_PPC_DTPREL_HA,
15410b57cec5SDimitry Andric OutContext);
15420b57cec5SDimitry Andric EmitToStreamer(
15430b57cec5SDimitry Andric *OutStreamer,
15448bcb0991SDimitry Andric MCInstBuilder(IsPPC64 ? PPC::ADDIS8 : PPC::ADDIS)
15450b57cec5SDimitry Andric .addReg(MI->getOperand(0).getReg())
15460b57cec5SDimitry Andric .addReg(MI->getOperand(1).getReg())
15470b57cec5SDimitry Andric .addExpr(SymDtprel));
15480b57cec5SDimitry Andric return;
15490b57cec5SDimitry Andric }
1550e8d8bef9SDimitry Andric case PPC::PADDIdtprel: {
1551e8d8bef9SDimitry Andric // Transform: %rd = PADDIdtprel %rs, @sym
1552e8d8bef9SDimitry Andric // Into: %rd = PADDI8 %rs, sym@dtprel
1553e8d8bef9SDimitry Andric const MachineOperand &MO = MI->getOperand(2);
1554e8d8bef9SDimitry Andric const GlobalValue *GValue = MO.getGlobal();
1555e8d8bef9SDimitry Andric MCSymbol *MOSymbol = getSymbol(GValue);
1556e8d8bef9SDimitry Andric const MCExpr *SymDtprel = MCSymbolRefExpr::create(
1557e8d8bef9SDimitry Andric MOSymbol, MCSymbolRefExpr::VK_DTPREL, OutContext);
1558e8d8bef9SDimitry Andric EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::PADDI8)
1559e8d8bef9SDimitry Andric .addReg(MI->getOperand(0).getReg())
1560e8d8bef9SDimitry Andric .addReg(MI->getOperand(1).getReg())
1561e8d8bef9SDimitry Andric .addExpr(SymDtprel));
1562e8d8bef9SDimitry Andric return;
1563e8d8bef9SDimitry Andric }
1564e8d8bef9SDimitry Andric
15650b57cec5SDimitry Andric case PPC::ADDIdtprelL:
15660b57cec5SDimitry Andric // Transform: %xd = ADDIdtprelL %xs, @sym
15670b57cec5SDimitry Andric // Into: %xd = ADDI8 %xs, sym@dtprel@l
15680b57cec5SDimitry Andric case PPC::ADDIdtprelL32: {
15690b57cec5SDimitry Andric // Transform: %rd = ADDIdtprelL32 %rs, @sym
15700b57cec5SDimitry Andric // Into: %rd = ADDI %rs, sym@dtprel@l
15710b57cec5SDimitry Andric const MachineOperand &MO = MI->getOperand(2);
15720b57cec5SDimitry Andric const GlobalValue *GValue = MO.getGlobal();
15730b57cec5SDimitry Andric MCSymbol *MOSymbol = getSymbol(GValue);
15740b57cec5SDimitry Andric const MCExpr *SymDtprel =
15750b57cec5SDimitry Andric MCSymbolRefExpr::create(MOSymbol, MCSymbolRefExpr::VK_PPC_DTPREL_LO,
15760b57cec5SDimitry Andric OutContext);
15770b57cec5SDimitry Andric EmitToStreamer(*OutStreamer,
15788bcb0991SDimitry Andric MCInstBuilder(IsPPC64 ? PPC::ADDI8 : PPC::ADDI)
15790b57cec5SDimitry Andric .addReg(MI->getOperand(0).getReg())
15800b57cec5SDimitry Andric .addReg(MI->getOperand(1).getReg())
15810b57cec5SDimitry Andric .addExpr(SymDtprel));
15820b57cec5SDimitry Andric return;
15830b57cec5SDimitry Andric }
15840b57cec5SDimitry Andric case PPC::MFOCRF:
15850b57cec5SDimitry Andric case PPC::MFOCRF8:
15860b57cec5SDimitry Andric if (!Subtarget->hasMFOCRF()) {
15870b57cec5SDimitry Andric // Transform: %r3 = MFOCRF %cr7
15880b57cec5SDimitry Andric // Into: %r3 = MFCR ;; cr7
15890b57cec5SDimitry Andric unsigned NewOpcode =
15900b57cec5SDimitry Andric MI->getOpcode() == PPC::MFOCRF ? PPC::MFCR : PPC::MFCR8;
15910b57cec5SDimitry Andric OutStreamer->AddComment(PPCInstPrinter::
15920b57cec5SDimitry Andric getRegisterName(MI->getOperand(1).getReg()));
15930b57cec5SDimitry Andric EmitToStreamer(*OutStreamer, MCInstBuilder(NewOpcode)
15940b57cec5SDimitry Andric .addReg(MI->getOperand(0).getReg()));
15950b57cec5SDimitry Andric return;
15960b57cec5SDimitry Andric }
15970b57cec5SDimitry Andric break;
15980b57cec5SDimitry Andric case PPC::MTOCRF:
15990b57cec5SDimitry Andric case PPC::MTOCRF8:
16000b57cec5SDimitry Andric if (!Subtarget->hasMFOCRF()) {
16010b57cec5SDimitry Andric // Transform: %cr7 = MTOCRF %r3
16020b57cec5SDimitry Andric // Into: MTCRF mask, %r3 ;; cr7
16030b57cec5SDimitry Andric unsigned NewOpcode =
16040b57cec5SDimitry Andric MI->getOpcode() == PPC::MTOCRF ? PPC::MTCRF : PPC::MTCRF8;
16050b57cec5SDimitry Andric unsigned Mask = 0x80 >> OutContext.getRegisterInfo()
16060b57cec5SDimitry Andric ->getEncodingValue(MI->getOperand(0).getReg());
16070b57cec5SDimitry Andric OutStreamer->AddComment(PPCInstPrinter::
16080b57cec5SDimitry Andric getRegisterName(MI->getOperand(0).getReg()));
16090b57cec5SDimitry Andric EmitToStreamer(*OutStreamer, MCInstBuilder(NewOpcode)
16100b57cec5SDimitry Andric .addImm(Mask)
16110b57cec5SDimitry Andric .addReg(MI->getOperand(1).getReg()));
16120b57cec5SDimitry Andric return;
16130b57cec5SDimitry Andric }
16140b57cec5SDimitry Andric break;
16150b57cec5SDimitry Andric case PPC::LD:
16160b57cec5SDimitry Andric case PPC::STD:
16170b57cec5SDimitry Andric case PPC::LWA_32:
16180b57cec5SDimitry Andric case PPC::LWA: {
16190b57cec5SDimitry Andric // Verify alignment is legal, so we don't create relocations
16200b57cec5SDimitry Andric // that can't be supported.
16210b57cec5SDimitry Andric unsigned OpNum = (MI->getOpcode() == PPC::STD) ? 2 : 1;
1622*0fca6ea1SDimitry Andric // For non-TOC-based local-exec TLS accesses with non-zero offsets, the
1623*0fca6ea1SDimitry Andric // machine operand (which is a TargetGlobalTLSAddress) is expected to be
1624*0fca6ea1SDimitry Andric // the same operand for both loads and stores.
1625*0fca6ea1SDimitry Andric for (const MachineOperand &TempMO : MI->operands()) {
1626*0fca6ea1SDimitry Andric if (((TempMO.getTargetFlags() == PPCII::MO_TPREL_FLAG ||
1627*0fca6ea1SDimitry Andric TempMO.getTargetFlags() == PPCII::MO_TLSLD_FLAG)) &&
1628*0fca6ea1SDimitry Andric TempMO.getOperandNo() == 1)
1629*0fca6ea1SDimitry Andric OpNum = 1;
1630*0fca6ea1SDimitry Andric }
16310b57cec5SDimitry Andric const MachineOperand &MO = MI->getOperand(OpNum);
16325ffd83dbSDimitry Andric if (MO.isGlobal()) {
1633*0fca6ea1SDimitry Andric const DataLayout &DL = MO.getGlobal()->getDataLayout();
16345ffd83dbSDimitry Andric if (MO.getGlobal()->getPointerAlignment(DL) < 4)
16350b57cec5SDimitry Andric llvm_unreachable("Global must be word-aligned for LD, STD, LWA!");
16360b57cec5SDimitry Andric }
1637*0fca6ea1SDimitry Andric // As these load/stores share common code with the following load/stores,
1638*0fca6ea1SDimitry Andric // fall through to the subsequent cases in order to either process the
1639*0fca6ea1SDimitry Andric // non-TOC-based local-exec sequence or to process the instruction normally.
1640*0fca6ea1SDimitry Andric [[fallthrough]];
1641*0fca6ea1SDimitry Andric }
1642*0fca6ea1SDimitry Andric case PPC::LBZ:
1643*0fca6ea1SDimitry Andric case PPC::LBZ8:
1644*0fca6ea1SDimitry Andric case PPC::LHA:
1645*0fca6ea1SDimitry Andric case PPC::LHA8:
1646*0fca6ea1SDimitry Andric case PPC::LHZ:
1647*0fca6ea1SDimitry Andric case PPC::LHZ8:
1648*0fca6ea1SDimitry Andric case PPC::LWZ:
1649*0fca6ea1SDimitry Andric case PPC::LWZ8:
1650*0fca6ea1SDimitry Andric case PPC::STB:
1651*0fca6ea1SDimitry Andric case PPC::STB8:
1652*0fca6ea1SDimitry Andric case PPC::STH:
1653*0fca6ea1SDimitry Andric case PPC::STH8:
1654*0fca6ea1SDimitry Andric case PPC::STW:
1655*0fca6ea1SDimitry Andric case PPC::STW8:
1656*0fca6ea1SDimitry Andric case PPC::LFS:
1657*0fca6ea1SDimitry Andric case PPC::STFS:
1658*0fca6ea1SDimitry Andric case PPC::LFD:
1659*0fca6ea1SDimitry Andric case PPC::STFD:
1660*0fca6ea1SDimitry Andric case PPC::ADDI8: {
1661*0fca6ea1SDimitry Andric // A faster non-TOC-based local-[exec|dynamic] sequence is represented by
1662*0fca6ea1SDimitry Andric // `addi` or a load/store instruction (that directly loads or stores off of
1663*0fca6ea1SDimitry Andric // the thread pointer) with an immediate operand having the
1664*0fca6ea1SDimitry Andric // [MO_TPREL_FLAG|MO_TLSLD_FLAG]. Such instructions do not otherwise arise.
1665*0fca6ea1SDimitry Andric if (!HasAIXSmallLocalTLS)
1666*0fca6ea1SDimitry Andric break;
1667*0fca6ea1SDimitry Andric bool IsMIADDI8 = MI->getOpcode() == PPC::ADDI8;
1668*0fca6ea1SDimitry Andric unsigned OpNum = IsMIADDI8 ? 2 : 1;
1669*0fca6ea1SDimitry Andric const MachineOperand &MO = MI->getOperand(OpNum);
1670*0fca6ea1SDimitry Andric unsigned Flag = MO.getTargetFlags();
1671*0fca6ea1SDimitry Andric if (Flag == PPCII::MO_TPREL_FLAG ||
1672*0fca6ea1SDimitry Andric Flag == PPCII::MO_GOT_TPREL_PCREL_FLAG ||
1673*0fca6ea1SDimitry Andric Flag == PPCII::MO_TPREL_PCREL_FLAG || Flag == PPCII::MO_TLSLD_FLAG) {
1674*0fca6ea1SDimitry Andric LowerPPCMachineInstrToMCInst(MI, TmpInst, *this);
1675*0fca6ea1SDimitry Andric
1676*0fca6ea1SDimitry Andric const MCExpr *Expr = getAdjustedFasterLocalExpr(MO, MO.getOffset());
1677*0fca6ea1SDimitry Andric if (Expr)
1678*0fca6ea1SDimitry Andric TmpInst.getOperand(OpNum) = MCOperand::createExpr(Expr);
1679*0fca6ea1SDimitry Andric
1680*0fca6ea1SDimitry Andric // Change the opcode to load address if the original opcode is an `addi`.
1681*0fca6ea1SDimitry Andric if (IsMIADDI8)
1682*0fca6ea1SDimitry Andric TmpInst.setOpcode(PPC::LA8);
1683*0fca6ea1SDimitry Andric
1684*0fca6ea1SDimitry Andric EmitToStreamer(*OutStreamer, TmpInst);
1685*0fca6ea1SDimitry Andric return;
1686*0fca6ea1SDimitry Andric }
16870b57cec5SDimitry Andric // Now process the instruction normally.
16880b57cec5SDimitry Andric break;
16890b57cec5SDimitry Andric }
1690fe6060f1SDimitry Andric case PPC::PseudoEIEIO: {
1691fe6060f1SDimitry Andric EmitToStreamer(
1692fe6060f1SDimitry Andric *OutStreamer,
1693fe6060f1SDimitry Andric MCInstBuilder(PPC::ORI).addReg(PPC::X2).addReg(PPC::X2).addImm(0));
1694fe6060f1SDimitry Andric EmitToStreamer(
1695fe6060f1SDimitry Andric *OutStreamer,
1696fe6060f1SDimitry Andric MCInstBuilder(PPC::ORI).addReg(PPC::X2).addReg(PPC::X2).addImm(0));
1697fe6060f1SDimitry Andric EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::EnforceIEIO));
1698fe6060f1SDimitry Andric return;
1699fe6060f1SDimitry Andric }
17000b57cec5SDimitry Andric }
17010b57cec5SDimitry Andric
17025ffd83dbSDimitry Andric LowerPPCMachineInstrToMCInst(MI, TmpInst, *this);
17030b57cec5SDimitry Andric EmitToStreamer(*OutStreamer, TmpInst);
17040b57cec5SDimitry Andric }
17050b57cec5SDimitry Andric
1706*0fca6ea1SDimitry Andric // For non-TOC-based local-[exec|dynamic] variables that have a non-zero offset,
1707*0fca6ea1SDimitry Andric // we need to create a new MCExpr that adds the non-zero offset to the address
1708*0fca6ea1SDimitry Andric // of the local-[exec|dynamic] variable that will be used in either an addi,
1709*0fca6ea1SDimitry Andric // load or store. However, the final displacement for these instructions must be
1710*0fca6ea1SDimitry Andric // between [-32768, 32768), so if the TLS address + its non-zero offset is
1711*0fca6ea1SDimitry Andric // greater than 32KB, a new MCExpr is produced to accommodate this situation.
1712*0fca6ea1SDimitry Andric const MCExpr *
getAdjustedFasterLocalExpr(const MachineOperand & MO,int64_t Offset)1713*0fca6ea1SDimitry Andric PPCAsmPrinter::getAdjustedFasterLocalExpr(const MachineOperand &MO,
1714*0fca6ea1SDimitry Andric int64_t Offset) {
1715*0fca6ea1SDimitry Andric // Non-zero offsets (for loads, stores or `addi`) require additional handling.
1716*0fca6ea1SDimitry Andric // When the offset is zero, there is no need to create an adjusted MCExpr.
1717*0fca6ea1SDimitry Andric if (!Offset)
1718*0fca6ea1SDimitry Andric return nullptr;
1719*0fca6ea1SDimitry Andric
1720*0fca6ea1SDimitry Andric assert(MO.isGlobal() && "Only expecting a global MachineOperand here!");
1721*0fca6ea1SDimitry Andric const GlobalValue *GValue = MO.getGlobal();
1722*0fca6ea1SDimitry Andric TLSModel::Model Model = TM.getTLSModel(GValue);
1723*0fca6ea1SDimitry Andric assert((Model == TLSModel::LocalExec || Model == TLSModel::LocalDynamic) &&
1724*0fca6ea1SDimitry Andric "Only local-[exec|dynamic] accesses are handled!");
1725*0fca6ea1SDimitry Andric
1726*0fca6ea1SDimitry Andric bool IsGlobalADeclaration = GValue->isDeclarationForLinker();
1727*0fca6ea1SDimitry Andric // Find the GlobalVariable that corresponds to the particular TLS variable
1728*0fca6ea1SDimitry Andric // in the TLS variable-to-address mapping. All TLS variables should exist
1729*0fca6ea1SDimitry Andric // within this map, with the exception of TLS variables marked as extern.
1730*0fca6ea1SDimitry Andric const auto TLSVarsMapEntryIter = TLSVarsToAddressMapping.find(GValue);
1731*0fca6ea1SDimitry Andric if (TLSVarsMapEntryIter == TLSVarsToAddressMapping.end())
1732*0fca6ea1SDimitry Andric assert(IsGlobalADeclaration &&
1733*0fca6ea1SDimitry Andric "Only expecting to find extern TLS variables not present in the TLS "
1734*0fca6ea1SDimitry Andric "variable-to-address map!");
1735*0fca6ea1SDimitry Andric
1736*0fca6ea1SDimitry Andric unsigned TLSVarAddress =
1737*0fca6ea1SDimitry Andric IsGlobalADeclaration ? 0 : TLSVarsMapEntryIter->second;
1738*0fca6ea1SDimitry Andric ptrdiff_t FinalAddress = (TLSVarAddress + Offset);
1739*0fca6ea1SDimitry Andric // If the address of the TLS variable + the offset is less than 32KB,
1740*0fca6ea1SDimitry Andric // or if the TLS variable is extern, we simply produce an MCExpr to add the
1741*0fca6ea1SDimitry Andric // non-zero offset to the TLS variable address.
1742*0fca6ea1SDimitry Andric // For when TLS variables are extern, this is safe to do because we can
1743*0fca6ea1SDimitry Andric // assume that the address of extern TLS variables are zero.
1744*0fca6ea1SDimitry Andric const MCExpr *Expr = MCSymbolRefExpr::create(
1745*0fca6ea1SDimitry Andric getSymbol(GValue),
1746*0fca6ea1SDimitry Andric Model == TLSModel::LocalExec ? MCSymbolRefExpr::VK_PPC_AIX_TLSLE
1747*0fca6ea1SDimitry Andric : MCSymbolRefExpr::VK_PPC_AIX_TLSLD,
1748*0fca6ea1SDimitry Andric OutContext);
1749*0fca6ea1SDimitry Andric Expr = MCBinaryExpr::createAdd(
1750*0fca6ea1SDimitry Andric Expr, MCConstantExpr::create(Offset, OutContext), OutContext);
1751*0fca6ea1SDimitry Andric if (FinalAddress >= 32768) {
1752*0fca6ea1SDimitry Andric // Handle the written offset for cases where:
1753*0fca6ea1SDimitry Andric // TLS variable address + Offset > 32KB.
1754*0fca6ea1SDimitry Andric
1755*0fca6ea1SDimitry Andric // The assembly that is printed will look like:
1756*0fca6ea1SDimitry Andric // TLSVar@le + Offset - Delta
1757*0fca6ea1SDimitry Andric // where Delta is a multiple of 64KB: ((FinalAddress + 32768) & ~0xFFFF).
1758*0fca6ea1SDimitry Andric ptrdiff_t Delta = ((FinalAddress + 32768) & ~0xFFFF);
1759*0fca6ea1SDimitry Andric // Check that the total instruction displacement fits within [-32768,32768).
1760*0fca6ea1SDimitry Andric [[maybe_unused]] ptrdiff_t InstDisp = TLSVarAddress + Offset - Delta;
1761*0fca6ea1SDimitry Andric assert(
1762*0fca6ea1SDimitry Andric ((InstDisp < 32768) && (InstDisp >= -32768)) &&
1763*0fca6ea1SDimitry Andric "Expecting the instruction displacement for local-[exec|dynamic] TLS "
1764*0fca6ea1SDimitry Andric "variables to be between [-32768, 32768)!");
1765*0fca6ea1SDimitry Andric Expr = MCBinaryExpr::createAdd(
1766*0fca6ea1SDimitry Andric Expr, MCConstantExpr::create(-Delta, OutContext), OutContext);
1767*0fca6ea1SDimitry Andric }
1768*0fca6ea1SDimitry Andric
1769*0fca6ea1SDimitry Andric return Expr;
1770*0fca6ea1SDimitry Andric }
1771*0fca6ea1SDimitry Andric
emitGNUAttributes(Module & M)177204eeddc0SDimitry Andric void PPCLinuxAsmPrinter::emitGNUAttributes(Module &M) {
177304eeddc0SDimitry Andric // Emit float ABI into GNU attribute
177404eeddc0SDimitry Andric Metadata *MD = M.getModuleFlag("float-abi");
177504eeddc0SDimitry Andric MDString *FloatABI = dyn_cast_or_null<MDString>(MD);
177604eeddc0SDimitry Andric if (!FloatABI)
177704eeddc0SDimitry Andric return;
177804eeddc0SDimitry Andric StringRef flt = FloatABI->getString();
177904eeddc0SDimitry Andric // TODO: Support emitting soft-fp and hard double/single attributes.
178004eeddc0SDimitry Andric if (flt == "doubledouble")
178104eeddc0SDimitry Andric OutStreamer->emitGNUAttribute(Tag_GNU_Power_ABI_FP,
178204eeddc0SDimitry Andric Val_GNU_Power_ABI_HardFloat_DP |
178304eeddc0SDimitry Andric Val_GNU_Power_ABI_LDBL_IBM128);
178404eeddc0SDimitry Andric else if (flt == "ieeequad")
178504eeddc0SDimitry Andric OutStreamer->emitGNUAttribute(Tag_GNU_Power_ABI_FP,
178604eeddc0SDimitry Andric Val_GNU_Power_ABI_HardFloat_DP |
178704eeddc0SDimitry Andric Val_GNU_Power_ABI_LDBL_IEEE128);
178804eeddc0SDimitry Andric else if (flt == "ieeedouble")
178904eeddc0SDimitry Andric OutStreamer->emitGNUAttribute(Tag_GNU_Power_ABI_FP,
179004eeddc0SDimitry Andric Val_GNU_Power_ABI_HardFloat_DP |
179104eeddc0SDimitry Andric Val_GNU_Power_ABI_LDBL_64);
179204eeddc0SDimitry Andric }
179304eeddc0SDimitry Andric
emitInstruction(const MachineInstr * MI)17945ffd83dbSDimitry Andric void PPCLinuxAsmPrinter::emitInstruction(const MachineInstr *MI) {
17950b57cec5SDimitry Andric if (!Subtarget->isPPC64())
17965ffd83dbSDimitry Andric return PPCAsmPrinter::emitInstruction(MI);
17970b57cec5SDimitry Andric
17980b57cec5SDimitry Andric switch (MI->getOpcode()) {
17990b57cec5SDimitry Andric default:
1800*0fca6ea1SDimitry Andric break;
18010b57cec5SDimitry Andric case TargetOpcode::PATCHABLE_FUNCTION_ENTER: {
18020b57cec5SDimitry Andric // .begin:
18030b57cec5SDimitry Andric // b .end # lis 0, FuncId[16..32]
18040b57cec5SDimitry Andric // nop # li 0, FuncId[0..15]
18050b57cec5SDimitry Andric // std 0, -8(1)
18060b57cec5SDimitry Andric // mflr 0
18070b57cec5SDimitry Andric // bl __xray_FunctionEntry
18080b57cec5SDimitry Andric // mtlr 0
18090b57cec5SDimitry Andric // .end:
18100b57cec5SDimitry Andric //
18110b57cec5SDimitry Andric // Update compiler-rt/lib/xray/xray_powerpc64.cc accordingly when number
18120b57cec5SDimitry Andric // of instructions change.
1813*0fca6ea1SDimitry Andric // XRAY is only supported on PPC Linux little endian.
1814*0fca6ea1SDimitry Andric if (!MAI->isLittleEndian())
1815*0fca6ea1SDimitry Andric break;
18160b57cec5SDimitry Andric MCSymbol *BeginOfSled = OutContext.createTempSymbol();
18170b57cec5SDimitry Andric MCSymbol *EndOfSled = OutContext.createTempSymbol();
18185ffd83dbSDimitry Andric OutStreamer->emitLabel(BeginOfSled);
18190b57cec5SDimitry Andric EmitToStreamer(*OutStreamer,
18200b57cec5SDimitry Andric MCInstBuilder(PPC::B).addExpr(
18210b57cec5SDimitry Andric MCSymbolRefExpr::create(EndOfSled, OutContext)));
18220b57cec5SDimitry Andric EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::NOP));
18230b57cec5SDimitry Andric EmitToStreamer(
18240b57cec5SDimitry Andric *OutStreamer,
18250b57cec5SDimitry Andric MCInstBuilder(PPC::STD).addReg(PPC::X0).addImm(-8).addReg(PPC::X1));
18260b57cec5SDimitry Andric EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::MFLR8).addReg(PPC::X0));
18270b57cec5SDimitry Andric EmitToStreamer(*OutStreamer,
18280b57cec5SDimitry Andric MCInstBuilder(PPC::BL8_NOP)
18290b57cec5SDimitry Andric .addExpr(MCSymbolRefExpr::create(
18300b57cec5SDimitry Andric OutContext.getOrCreateSymbol("__xray_FunctionEntry"),
18310b57cec5SDimitry Andric OutContext)));
18320b57cec5SDimitry Andric EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::MTLR8).addReg(PPC::X0));
18335ffd83dbSDimitry Andric OutStreamer->emitLabel(EndOfSled);
18345ffd83dbSDimitry Andric recordSled(BeginOfSled, *MI, SledKind::FUNCTION_ENTER, 2);
18350b57cec5SDimitry Andric break;
18360b57cec5SDimitry Andric }
18370b57cec5SDimitry Andric case TargetOpcode::PATCHABLE_RET: {
18380b57cec5SDimitry Andric unsigned RetOpcode = MI->getOperand(0).getImm();
18390b57cec5SDimitry Andric MCInst RetInst;
18400b57cec5SDimitry Andric RetInst.setOpcode(RetOpcode);
1841fe6060f1SDimitry Andric for (const auto &MO : llvm::drop_begin(MI->operands())) {
18420b57cec5SDimitry Andric MCOperand MCOp;
18435ffd83dbSDimitry Andric if (LowerPPCMachineOperandToMCOperand(MO, MCOp, *this))
18440b57cec5SDimitry Andric RetInst.addOperand(MCOp);
18450b57cec5SDimitry Andric }
18460b57cec5SDimitry Andric
18470b57cec5SDimitry Andric bool IsConditional;
18480b57cec5SDimitry Andric if (RetOpcode == PPC::BCCLR) {
18490b57cec5SDimitry Andric IsConditional = true;
18500b57cec5SDimitry Andric } else if (RetOpcode == PPC::TCRETURNdi8 || RetOpcode == PPC::TCRETURNri8 ||
18510b57cec5SDimitry Andric RetOpcode == PPC::TCRETURNai8) {
18520b57cec5SDimitry Andric break;
18530b57cec5SDimitry Andric } else if (RetOpcode == PPC::BLR8 || RetOpcode == PPC::TAILB8) {
18540b57cec5SDimitry Andric IsConditional = false;
18550b57cec5SDimitry Andric } else {
18560b57cec5SDimitry Andric EmitToStreamer(*OutStreamer, RetInst);
1857*0fca6ea1SDimitry Andric return;
18580b57cec5SDimitry Andric }
18590b57cec5SDimitry Andric
18600b57cec5SDimitry Andric MCSymbol *FallthroughLabel;
18610b57cec5SDimitry Andric if (IsConditional) {
18620b57cec5SDimitry Andric // Before:
18630b57cec5SDimitry Andric // bgtlr cr0
18640b57cec5SDimitry Andric //
18650b57cec5SDimitry Andric // After:
18660b57cec5SDimitry Andric // ble cr0, .end
18670b57cec5SDimitry Andric // .p2align 3
18680b57cec5SDimitry Andric // .begin:
18690b57cec5SDimitry Andric // blr # lis 0, FuncId[16..32]
18700b57cec5SDimitry Andric // nop # li 0, FuncId[0..15]
18710b57cec5SDimitry Andric // std 0, -8(1)
18720b57cec5SDimitry Andric // mflr 0
18730b57cec5SDimitry Andric // bl __xray_FunctionExit
18740b57cec5SDimitry Andric // mtlr 0
18750b57cec5SDimitry Andric // blr
18760b57cec5SDimitry Andric // .end:
18770b57cec5SDimitry Andric //
18780b57cec5SDimitry Andric // Update compiler-rt/lib/xray/xray_powerpc64.cc accordingly when number
18790b57cec5SDimitry Andric // of instructions change.
18800b57cec5SDimitry Andric FallthroughLabel = OutContext.createTempSymbol();
18810b57cec5SDimitry Andric EmitToStreamer(
18820b57cec5SDimitry Andric *OutStreamer,
18830b57cec5SDimitry Andric MCInstBuilder(PPC::BCC)
18840b57cec5SDimitry Andric .addImm(PPC::InvertPredicate(
18850b57cec5SDimitry Andric static_cast<PPC::Predicate>(MI->getOperand(1).getImm())))
18860b57cec5SDimitry Andric .addReg(MI->getOperand(2).getReg())
18870b57cec5SDimitry Andric .addExpr(MCSymbolRefExpr::create(FallthroughLabel, OutContext)));
18880b57cec5SDimitry Andric RetInst = MCInst();
18890b57cec5SDimitry Andric RetInst.setOpcode(PPC::BLR8);
18900b57cec5SDimitry Andric }
18910b57cec5SDimitry Andric // .p2align 3
18920b57cec5SDimitry Andric // .begin:
18930b57cec5SDimitry Andric // b(lr)? # lis 0, FuncId[16..32]
18940b57cec5SDimitry Andric // nop # li 0, FuncId[0..15]
18950b57cec5SDimitry Andric // std 0, -8(1)
18960b57cec5SDimitry Andric // mflr 0
18970b57cec5SDimitry Andric // bl __xray_FunctionExit
18980b57cec5SDimitry Andric // mtlr 0
18990b57cec5SDimitry Andric // b(lr)?
19000b57cec5SDimitry Andric //
19010b57cec5SDimitry Andric // Update compiler-rt/lib/xray/xray_powerpc64.cc accordingly when number
19020b57cec5SDimitry Andric // of instructions change.
1903bdd1243dSDimitry Andric OutStreamer->emitCodeAlignment(Align(8), &getSubtargetInfo());
19040b57cec5SDimitry Andric MCSymbol *BeginOfSled = OutContext.createTempSymbol();
19055ffd83dbSDimitry Andric OutStreamer->emitLabel(BeginOfSled);
19060b57cec5SDimitry Andric EmitToStreamer(*OutStreamer, RetInst);
19070b57cec5SDimitry Andric EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::NOP));
19080b57cec5SDimitry Andric EmitToStreamer(
19090b57cec5SDimitry Andric *OutStreamer,
19100b57cec5SDimitry Andric MCInstBuilder(PPC::STD).addReg(PPC::X0).addImm(-8).addReg(PPC::X1));
19110b57cec5SDimitry Andric EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::MFLR8).addReg(PPC::X0));
19120b57cec5SDimitry Andric EmitToStreamer(*OutStreamer,
19130b57cec5SDimitry Andric MCInstBuilder(PPC::BL8_NOP)
19140b57cec5SDimitry Andric .addExpr(MCSymbolRefExpr::create(
19150b57cec5SDimitry Andric OutContext.getOrCreateSymbol("__xray_FunctionExit"),
19160b57cec5SDimitry Andric OutContext)));
19170b57cec5SDimitry Andric EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::MTLR8).addReg(PPC::X0));
19180b57cec5SDimitry Andric EmitToStreamer(*OutStreamer, RetInst);
19190b57cec5SDimitry Andric if (IsConditional)
19205ffd83dbSDimitry Andric OutStreamer->emitLabel(FallthroughLabel);
19215ffd83dbSDimitry Andric recordSled(BeginOfSled, *MI, SledKind::FUNCTION_EXIT, 2);
1922*0fca6ea1SDimitry Andric return;
19230b57cec5SDimitry Andric }
19240b57cec5SDimitry Andric case TargetOpcode::PATCHABLE_FUNCTION_EXIT:
19250b57cec5SDimitry Andric llvm_unreachable("PATCHABLE_FUNCTION_EXIT should never be emitted");
19260b57cec5SDimitry Andric case TargetOpcode::PATCHABLE_TAIL_CALL:
19270b57cec5SDimitry Andric // TODO: Define a trampoline `__xray_FunctionTailExit` and differentiate a
19280b57cec5SDimitry Andric // normal function exit from a tail exit.
19290b57cec5SDimitry Andric llvm_unreachable("Tail call is handled in the normal case. See comments "
19300b57cec5SDimitry Andric "around this assert.");
19310b57cec5SDimitry Andric }
1932*0fca6ea1SDimitry Andric return PPCAsmPrinter::emitInstruction(MI);
19330b57cec5SDimitry Andric }
19340b57cec5SDimitry Andric
emitStartOfAsmFile(Module & M)19355ffd83dbSDimitry Andric void PPCLinuxAsmPrinter::emitStartOfAsmFile(Module &M) {
19360b57cec5SDimitry Andric if (static_cast<const PPCTargetMachine &>(TM).isELFv2ABI()) {
19370b57cec5SDimitry Andric PPCTargetStreamer *TS =
19380b57cec5SDimitry Andric static_cast<PPCTargetStreamer *>(OutStreamer->getTargetStreamer());
19390b57cec5SDimitry Andric TS->emitAbiVersion(2);
19400b57cec5SDimitry Andric }
19410b57cec5SDimitry Andric
19420b57cec5SDimitry Andric if (static_cast<const PPCTargetMachine &>(TM).isPPC64() ||
19430b57cec5SDimitry Andric !isPositionIndependent())
19445ffd83dbSDimitry Andric return AsmPrinter::emitStartOfAsmFile(M);
19450b57cec5SDimitry Andric
19460b57cec5SDimitry Andric if (M.getPICLevel() == PICLevel::SmallPIC)
19475ffd83dbSDimitry Andric return AsmPrinter::emitStartOfAsmFile(M);
19480b57cec5SDimitry Andric
194981ad6265SDimitry Andric OutStreamer->switchSection(OutContext.getELFSection(
19500b57cec5SDimitry Andric ".got2", ELF::SHT_PROGBITS, ELF::SHF_WRITE | ELF::SHF_ALLOC));
19510b57cec5SDimitry Andric
19520b57cec5SDimitry Andric MCSymbol *TOCSym = OutContext.getOrCreateSymbol(Twine(".LTOC"));
19530b57cec5SDimitry Andric MCSymbol *CurrentPos = OutContext.createTempSymbol();
19540b57cec5SDimitry Andric
19555ffd83dbSDimitry Andric OutStreamer->emitLabel(CurrentPos);
19560b57cec5SDimitry Andric
19570b57cec5SDimitry Andric // The GOT pointer points to the middle of the GOT, in order to reference the
19580b57cec5SDimitry Andric // entire 64kB range. 0x8000 is the midpoint.
19590b57cec5SDimitry Andric const MCExpr *tocExpr =
19600b57cec5SDimitry Andric MCBinaryExpr::createAdd(MCSymbolRefExpr::create(CurrentPos, OutContext),
19610b57cec5SDimitry Andric MCConstantExpr::create(0x8000, OutContext),
19620b57cec5SDimitry Andric OutContext);
19630b57cec5SDimitry Andric
19645ffd83dbSDimitry Andric OutStreamer->emitAssignment(TOCSym, tocExpr);
19650b57cec5SDimitry Andric
196681ad6265SDimitry Andric OutStreamer->switchSection(getObjFileLowering().getTextSection());
19670b57cec5SDimitry Andric }
19680b57cec5SDimitry Andric
emitFunctionEntryLabel()19695ffd83dbSDimitry Andric void PPCLinuxAsmPrinter::emitFunctionEntryLabel() {
19700b57cec5SDimitry Andric // linux/ppc32 - Normal entry label.
19710b57cec5SDimitry Andric if (!Subtarget->isPPC64() &&
19720b57cec5SDimitry Andric (!isPositionIndependent() ||
19730b57cec5SDimitry Andric MF->getFunction().getParent()->getPICLevel() == PICLevel::SmallPIC))
19745ffd83dbSDimitry Andric return AsmPrinter::emitFunctionEntryLabel();
19750b57cec5SDimitry Andric
19760b57cec5SDimitry Andric if (!Subtarget->isPPC64()) {
19770b57cec5SDimitry Andric const PPCFunctionInfo *PPCFI = MF->getInfo<PPCFunctionInfo>();
19780b57cec5SDimitry Andric if (PPCFI->usesPICBase() && !Subtarget->isSecurePlt()) {
19795ffd83dbSDimitry Andric MCSymbol *RelocSymbol = PPCFI->getPICOffsetSymbol(*MF);
19800b57cec5SDimitry Andric MCSymbol *PICBase = MF->getPICBaseSymbol();
19815ffd83dbSDimitry Andric OutStreamer->emitLabel(RelocSymbol);
19820b57cec5SDimitry Andric
19830b57cec5SDimitry Andric const MCExpr *OffsExpr =
19840b57cec5SDimitry Andric MCBinaryExpr::createSub(
19850b57cec5SDimitry Andric MCSymbolRefExpr::create(OutContext.getOrCreateSymbol(Twine(".LTOC")),
19860b57cec5SDimitry Andric OutContext),
19870b57cec5SDimitry Andric MCSymbolRefExpr::create(PICBase, OutContext),
19880b57cec5SDimitry Andric OutContext);
19895ffd83dbSDimitry Andric OutStreamer->emitValue(OffsExpr, 4);
19905ffd83dbSDimitry Andric OutStreamer->emitLabel(CurrentFnSym);
19910b57cec5SDimitry Andric return;
19920b57cec5SDimitry Andric } else
19935ffd83dbSDimitry Andric return AsmPrinter::emitFunctionEntryLabel();
19940b57cec5SDimitry Andric }
19950b57cec5SDimitry Andric
19960b57cec5SDimitry Andric // ELFv2 ABI - Normal entry label.
19970b57cec5SDimitry Andric if (Subtarget->isELFv2ABI()) {
19980b57cec5SDimitry Andric // In the Large code model, we allow arbitrary displacements between
19990b57cec5SDimitry Andric // the text section and its associated TOC section. We place the
20000b57cec5SDimitry Andric // full 8-byte offset to the TOC in memory immediately preceding
20010b57cec5SDimitry Andric // the function global entry point.
20020b57cec5SDimitry Andric if (TM.getCodeModel() == CodeModel::Large
20030b57cec5SDimitry Andric && !MF->getRegInfo().use_empty(PPC::X2)) {
20040b57cec5SDimitry Andric const PPCFunctionInfo *PPCFI = MF->getInfo<PPCFunctionInfo>();
20050b57cec5SDimitry Andric
20060b57cec5SDimitry Andric MCSymbol *TOCSymbol = OutContext.getOrCreateSymbol(StringRef(".TOC."));
20075ffd83dbSDimitry Andric MCSymbol *GlobalEPSymbol = PPCFI->getGlobalEPSymbol(*MF);
20080b57cec5SDimitry Andric const MCExpr *TOCDeltaExpr =
20090b57cec5SDimitry Andric MCBinaryExpr::createSub(MCSymbolRefExpr::create(TOCSymbol, OutContext),
20100b57cec5SDimitry Andric MCSymbolRefExpr::create(GlobalEPSymbol,
20110b57cec5SDimitry Andric OutContext),
20120b57cec5SDimitry Andric OutContext);
20130b57cec5SDimitry Andric
20145ffd83dbSDimitry Andric OutStreamer->emitLabel(PPCFI->getTOCOffsetSymbol(*MF));
20155ffd83dbSDimitry Andric OutStreamer->emitValue(TOCDeltaExpr, 8);
20160b57cec5SDimitry Andric }
20175ffd83dbSDimitry Andric return AsmPrinter::emitFunctionEntryLabel();
20180b57cec5SDimitry Andric }
20190b57cec5SDimitry Andric
20200b57cec5SDimitry Andric // Emit an official procedure descriptor.
20210b57cec5SDimitry Andric MCSectionSubPair Current = OutStreamer->getCurrentSection();
20220b57cec5SDimitry Andric MCSectionELF *Section = OutStreamer->getContext().getELFSection(
20230b57cec5SDimitry Andric ".opd", ELF::SHT_PROGBITS, ELF::SHF_WRITE | ELF::SHF_ALLOC);
202481ad6265SDimitry Andric OutStreamer->switchSection(Section);
20255ffd83dbSDimitry Andric OutStreamer->emitLabel(CurrentFnSym);
2026bdd1243dSDimitry Andric OutStreamer->emitValueToAlignment(Align(8));
20270b57cec5SDimitry Andric MCSymbol *Symbol1 = CurrentFnSymForSize;
20280b57cec5SDimitry Andric // Generates a R_PPC64_ADDR64 (from FK_DATA_8) relocation for the function
20290b57cec5SDimitry Andric // entry point.
20305ffd83dbSDimitry Andric OutStreamer->emitValue(MCSymbolRefExpr::create(Symbol1, OutContext),
20310b57cec5SDimitry Andric 8 /*size*/);
20320b57cec5SDimitry Andric MCSymbol *Symbol2 = OutContext.getOrCreateSymbol(StringRef(".TOC."));
20330b57cec5SDimitry Andric // Generates a R_PPC64_TOC relocation for TOC base insertion.
20345ffd83dbSDimitry Andric OutStreamer->emitValue(
20350b57cec5SDimitry Andric MCSymbolRefExpr::create(Symbol2, MCSymbolRefExpr::VK_PPC_TOCBASE, OutContext),
20360b57cec5SDimitry Andric 8/*size*/);
20370b57cec5SDimitry Andric // Emit a null environment pointer.
20385ffd83dbSDimitry Andric OutStreamer->emitIntValue(0, 8 /* size */);
203981ad6265SDimitry Andric OutStreamer->switchSection(Current.first, Current.second);
20400b57cec5SDimitry Andric }
20410b57cec5SDimitry Andric
emitEndOfAsmFile(Module & M)20425ffd83dbSDimitry Andric void PPCLinuxAsmPrinter::emitEndOfAsmFile(Module &M) {
20430b57cec5SDimitry Andric const DataLayout &DL = getDataLayout();
20440b57cec5SDimitry Andric
20450b57cec5SDimitry Andric bool isPPC64 = DL.getPointerSizeInBits() == 64;
20460b57cec5SDimitry Andric
20475ffd83dbSDimitry Andric PPCTargetStreamer *TS =
20485ffd83dbSDimitry Andric static_cast<PPCTargetStreamer *>(OutStreamer->getTargetStreamer());
20490b57cec5SDimitry Andric
2050*0fca6ea1SDimitry Andric // If we are using any values provided by Glibc at fixed addresses,
2051*0fca6ea1SDimitry Andric // we need to ensure that the Glibc used at link time actually provides
2052*0fca6ea1SDimitry Andric // those values. All versions of Glibc that do will define the symbol
2053*0fca6ea1SDimitry Andric // named "__parse_hwcap_and_convert_at_platform".
2054*0fca6ea1SDimitry Andric if (static_cast<const PPCTargetMachine &>(TM).hasGlibcHWCAPAccess())
2055*0fca6ea1SDimitry Andric OutStreamer->emitSymbolValue(
2056*0fca6ea1SDimitry Andric GetExternalSymbolSymbol("__parse_hwcap_and_convert_at_platform"),
2057*0fca6ea1SDimitry Andric MAI->getCodePointerSize());
205804eeddc0SDimitry Andric emitGNUAttributes(M);
205904eeddc0SDimitry Andric
20600b57cec5SDimitry Andric if (!TOC.empty()) {
20615ffd83dbSDimitry Andric const char *Name = isPPC64 ? ".toc" : ".got2";
20625ffd83dbSDimitry Andric MCSectionELF *Section = OutContext.getELFSection(
20635ffd83dbSDimitry Andric Name, ELF::SHT_PROGBITS, ELF::SHF_WRITE | ELF::SHF_ALLOC);
206481ad6265SDimitry Andric OutStreamer->switchSection(Section);
20655ffd83dbSDimitry Andric if (!isPPC64)
2066bdd1243dSDimitry Andric OutStreamer->emitValueToAlignment(Align(4));
20670b57cec5SDimitry Andric
20688bcb0991SDimitry Andric for (const auto &TOCMapPair : TOC) {
2069fe6060f1SDimitry Andric const MCSymbol *const TOCEntryTarget = TOCMapPair.first.first;
20708bcb0991SDimitry Andric MCSymbol *const TOCEntryLabel = TOCMapPair.second;
20718bcb0991SDimitry Andric
20725ffd83dbSDimitry Andric OutStreamer->emitLabel(TOCEntryLabel);
2073bdd1243dSDimitry Andric if (isPPC64)
2074fe6060f1SDimitry Andric TS->emitTCEntry(*TOCEntryTarget, TOCMapPair.first.second);
20755ffd83dbSDimitry Andric else
20765ffd83dbSDimitry Andric OutStreamer->emitSymbolValue(TOCEntryTarget, 4);
20770b57cec5SDimitry Andric }
20780b57cec5SDimitry Andric }
20790b57cec5SDimitry Andric
20805ffd83dbSDimitry Andric PPCAsmPrinter::emitEndOfAsmFile(M);
20810b57cec5SDimitry Andric }
20820b57cec5SDimitry Andric
20830b57cec5SDimitry Andric /// EmitFunctionBodyStart - Emit a global entry point prefix for ELFv2.
emitFunctionBodyStart()20845ffd83dbSDimitry Andric void PPCLinuxAsmPrinter::emitFunctionBodyStart() {
20850b57cec5SDimitry Andric // In the ELFv2 ABI, in functions that use the TOC register, we need to
20860b57cec5SDimitry Andric // provide two entry points. The ABI guarantees that when calling the
20870b57cec5SDimitry Andric // local entry point, r2 is set up by the caller to contain the TOC base
20880b57cec5SDimitry Andric // for this function, and when calling the global entry point, r12 is set
20890b57cec5SDimitry Andric // up by the caller to hold the address of the global entry point. We
20900b57cec5SDimitry Andric // thus emit a prefix sequence along the following lines:
20910b57cec5SDimitry Andric //
20920b57cec5SDimitry Andric // func:
20930b57cec5SDimitry Andric // .Lfunc_gepNN:
20940b57cec5SDimitry Andric // # global entry point
20950b57cec5SDimitry Andric // addis r2,r12,(.TOC.-.Lfunc_gepNN)@ha
20960b57cec5SDimitry Andric // addi r2,r2,(.TOC.-.Lfunc_gepNN)@l
20970b57cec5SDimitry Andric // .Lfunc_lepNN:
20980b57cec5SDimitry Andric // .localentry func, .Lfunc_lepNN-.Lfunc_gepNN
20990b57cec5SDimitry Andric // # local entry point, followed by function body
21000b57cec5SDimitry Andric //
21010b57cec5SDimitry Andric // For the Large code model, we create
21020b57cec5SDimitry Andric //
21030b57cec5SDimitry Andric // .Lfunc_tocNN:
21040b57cec5SDimitry Andric // .quad .TOC.-.Lfunc_gepNN # done by EmitFunctionEntryLabel
21050b57cec5SDimitry Andric // func:
21060b57cec5SDimitry Andric // .Lfunc_gepNN:
21070b57cec5SDimitry Andric // # global entry point
21080b57cec5SDimitry Andric // ld r2,.Lfunc_tocNN-.Lfunc_gepNN(r12)
21090b57cec5SDimitry Andric // add r2,r2,r12
21100b57cec5SDimitry Andric // .Lfunc_lepNN:
21110b57cec5SDimitry Andric // .localentry func, .Lfunc_lepNN-.Lfunc_gepNN
21120b57cec5SDimitry Andric // # local entry point, followed by function body
21130b57cec5SDimitry Andric //
21140b57cec5SDimitry Andric // This ensures we have r2 set up correctly while executing the function
21150b57cec5SDimitry Andric // body, no matter which entry point is called.
21165ffd83dbSDimitry Andric const PPCFunctionInfo *PPCFI = MF->getInfo<PPCFunctionInfo>();
21175ffd83dbSDimitry Andric const bool UsesX2OrR2 = !MF->getRegInfo().use_empty(PPC::X2) ||
21185ffd83dbSDimitry Andric !MF->getRegInfo().use_empty(PPC::R2);
21195ffd83dbSDimitry Andric const bool PCrelGEPRequired = Subtarget->isUsingPCRelativeCalls() &&
21205ffd83dbSDimitry Andric UsesX2OrR2 && PPCFI->usesTOCBasePtr();
21215ffd83dbSDimitry Andric const bool NonPCrelGEPRequired = !Subtarget->isUsingPCRelativeCalls() &&
21225ffd83dbSDimitry Andric Subtarget->isELFv2ABI() && UsesX2OrR2;
21235ffd83dbSDimitry Andric
21245ffd83dbSDimitry Andric // Only do all that if the function uses R2 as the TOC pointer
21255ffd83dbSDimitry Andric // in the first place. We don't need the global entry point if the
21265ffd83dbSDimitry Andric // function uses R2 as an allocatable register.
21275ffd83dbSDimitry Andric if (NonPCrelGEPRequired || PCrelGEPRequired) {
21280b57cec5SDimitry Andric // Note: The logic here must be synchronized with the code in the
21290b57cec5SDimitry Andric // branch-selection pass which sets the offset of the first block in the
21300b57cec5SDimitry Andric // function. This matters because it affects the alignment.
21315ffd83dbSDimitry Andric MCSymbol *GlobalEntryLabel = PPCFI->getGlobalEPSymbol(*MF);
21325ffd83dbSDimitry Andric OutStreamer->emitLabel(GlobalEntryLabel);
21330b57cec5SDimitry Andric const MCSymbolRefExpr *GlobalEntryLabelExp =
21340b57cec5SDimitry Andric MCSymbolRefExpr::create(GlobalEntryLabel, OutContext);
21350b57cec5SDimitry Andric
21360b57cec5SDimitry Andric if (TM.getCodeModel() != CodeModel::Large) {
21370b57cec5SDimitry Andric MCSymbol *TOCSymbol = OutContext.getOrCreateSymbol(StringRef(".TOC."));
21380b57cec5SDimitry Andric const MCExpr *TOCDeltaExpr =
21390b57cec5SDimitry Andric MCBinaryExpr::createSub(MCSymbolRefExpr::create(TOCSymbol, OutContext),
21400b57cec5SDimitry Andric GlobalEntryLabelExp, OutContext);
21410b57cec5SDimitry Andric
21425ffd83dbSDimitry Andric const MCExpr *TOCDeltaHi = PPCMCExpr::createHa(TOCDeltaExpr, OutContext);
21430b57cec5SDimitry Andric EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::ADDIS)
21440b57cec5SDimitry Andric .addReg(PPC::X2)
21450b57cec5SDimitry Andric .addReg(PPC::X12)
21460b57cec5SDimitry Andric .addExpr(TOCDeltaHi));
21470b57cec5SDimitry Andric
21485ffd83dbSDimitry Andric const MCExpr *TOCDeltaLo = PPCMCExpr::createLo(TOCDeltaExpr, OutContext);
21490b57cec5SDimitry Andric EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::ADDI)
21500b57cec5SDimitry Andric .addReg(PPC::X2)
21510b57cec5SDimitry Andric .addReg(PPC::X2)
21520b57cec5SDimitry Andric .addExpr(TOCDeltaLo));
21530b57cec5SDimitry Andric } else {
21545ffd83dbSDimitry Andric MCSymbol *TOCOffset = PPCFI->getTOCOffsetSymbol(*MF);
21550b57cec5SDimitry Andric const MCExpr *TOCOffsetDeltaExpr =
21560b57cec5SDimitry Andric MCBinaryExpr::createSub(MCSymbolRefExpr::create(TOCOffset, OutContext),
21570b57cec5SDimitry Andric GlobalEntryLabelExp, OutContext);
21580b57cec5SDimitry Andric
21590b57cec5SDimitry Andric EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::LD)
21600b57cec5SDimitry Andric .addReg(PPC::X2)
21610b57cec5SDimitry Andric .addExpr(TOCOffsetDeltaExpr)
21620b57cec5SDimitry Andric .addReg(PPC::X12));
21630b57cec5SDimitry Andric EmitToStreamer(*OutStreamer, MCInstBuilder(PPC::ADD8)
21640b57cec5SDimitry Andric .addReg(PPC::X2)
21650b57cec5SDimitry Andric .addReg(PPC::X2)
21660b57cec5SDimitry Andric .addReg(PPC::X12));
21670b57cec5SDimitry Andric }
21680b57cec5SDimitry Andric
21695ffd83dbSDimitry Andric MCSymbol *LocalEntryLabel = PPCFI->getLocalEPSymbol(*MF);
21705ffd83dbSDimitry Andric OutStreamer->emitLabel(LocalEntryLabel);
21710b57cec5SDimitry Andric const MCSymbolRefExpr *LocalEntryLabelExp =
21720b57cec5SDimitry Andric MCSymbolRefExpr::create(LocalEntryLabel, OutContext);
21730b57cec5SDimitry Andric const MCExpr *LocalOffsetExp =
21740b57cec5SDimitry Andric MCBinaryExpr::createSub(LocalEntryLabelExp,
21750b57cec5SDimitry Andric GlobalEntryLabelExp, OutContext);
21760b57cec5SDimitry Andric
21770b57cec5SDimitry Andric PPCTargetStreamer *TS =
21780b57cec5SDimitry Andric static_cast<PPCTargetStreamer *>(OutStreamer->getTargetStreamer());
21790b57cec5SDimitry Andric TS->emitLocalEntry(cast<MCSymbolELF>(CurrentFnSym), LocalOffsetExp);
21805ffd83dbSDimitry Andric } else if (Subtarget->isUsingPCRelativeCalls()) {
21815ffd83dbSDimitry Andric // When generating the entry point for a function we have a few scenarios
21825ffd83dbSDimitry Andric // based on whether or not that function uses R2 and whether or not that
21835ffd83dbSDimitry Andric // function makes calls (or is a leaf function).
21845ffd83dbSDimitry Andric // 1) A leaf function that does not use R2 (or treats it as callee-saved
21855ffd83dbSDimitry Andric // and preserves it). In this case st_other=0 and both
21865ffd83dbSDimitry Andric // the local and global entry points for the function are the same.
21875ffd83dbSDimitry Andric // No special entry point code is required.
21885ffd83dbSDimitry Andric // 2) A function uses the TOC pointer R2. This function may or may not have
21895ffd83dbSDimitry Andric // calls. In this case st_other=[2,6] and the global and local entry
21905ffd83dbSDimitry Andric // points are different. Code to correctly setup the TOC pointer in R2
21915ffd83dbSDimitry Andric // is put between the global and local entry points. This case is
21925ffd83dbSDimitry Andric // covered by the if statatement above.
21935ffd83dbSDimitry Andric // 3) A function does not use the TOC pointer R2 but does have calls.
21945ffd83dbSDimitry Andric // In this case st_other=1 since we do not know whether or not any
21955ffd83dbSDimitry Andric // of the callees clobber R2. This case is dealt with in this else if
21965ffd83dbSDimitry Andric // block. Tail calls are considered calls and the st_other should also
21975ffd83dbSDimitry Andric // be set to 1 in that case as well.
21985ffd83dbSDimitry Andric // 4) The function does not use the TOC pointer but R2 is used inside
21995ffd83dbSDimitry Andric // the function. In this case st_other=1 once again.
22005ffd83dbSDimitry Andric // 5) This function uses inline asm. We mark R2 as reserved if the function
22015ffd83dbSDimitry Andric // has inline asm as we have to assume that it may be used.
22025ffd83dbSDimitry Andric if (MF->getFrameInfo().hasCalls() || MF->getFrameInfo().hasTailCall() ||
22035ffd83dbSDimitry Andric MF->hasInlineAsm() || (!PPCFI->usesTOCBasePtr() && UsesX2OrR2)) {
22045ffd83dbSDimitry Andric PPCTargetStreamer *TS =
22055ffd83dbSDimitry Andric static_cast<PPCTargetStreamer *>(OutStreamer->getTargetStreamer());
22065ffd83dbSDimitry Andric TS->emitLocalEntry(cast<MCSymbolELF>(CurrentFnSym),
22075ffd83dbSDimitry Andric MCConstantExpr::create(1, OutContext));
22085ffd83dbSDimitry Andric }
22090b57cec5SDimitry Andric }
22100b57cec5SDimitry Andric }
22110b57cec5SDimitry Andric
22120b57cec5SDimitry Andric /// EmitFunctionBodyEnd - Print the traceback table before the .size
22130b57cec5SDimitry Andric /// directive.
22140b57cec5SDimitry Andric ///
emitFunctionBodyEnd()22155ffd83dbSDimitry Andric void PPCLinuxAsmPrinter::emitFunctionBodyEnd() {
22160b57cec5SDimitry Andric // Only the 64-bit target requires a traceback table. For now,
22170b57cec5SDimitry Andric // we only emit the word of zeroes that GDB requires to find
22180b57cec5SDimitry Andric // the end of the function, and zeroes for the eight-byte
22190b57cec5SDimitry Andric // mandatory fields.
22200b57cec5SDimitry Andric // FIXME: We should fill in the eight-byte mandatory fields as described in
22210b57cec5SDimitry Andric // the PPC64 ELF ABI (this is a low-priority item because GDB does not
22220b57cec5SDimitry Andric // currently make use of these fields).
22230b57cec5SDimitry Andric if (Subtarget->isPPC64()) {
22245ffd83dbSDimitry Andric OutStreamer->emitIntValue(0, 4/*size*/);
22255ffd83dbSDimitry Andric OutStreamer->emitIntValue(0, 8/*size*/);
22260b57cec5SDimitry Andric }
22270b57cec5SDimitry Andric }
22280b57cec5SDimitry Andric
emitLinkage(const GlobalValue * GV,MCSymbol * GVSym) const22295ffd83dbSDimitry Andric void PPCAIXAsmPrinter::emitLinkage(const GlobalValue *GV,
22305ffd83dbSDimitry Andric MCSymbol *GVSym) const {
22315ffd83dbSDimitry Andric
22325ffd83dbSDimitry Andric assert(MAI->hasVisibilityOnlyWithLinkage() &&
22335ffd83dbSDimitry Andric "AIX's linkage directives take a visibility setting.");
22345ffd83dbSDimitry Andric
22355ffd83dbSDimitry Andric MCSymbolAttr LinkageAttr = MCSA_Invalid;
22365ffd83dbSDimitry Andric switch (GV->getLinkage()) {
22375ffd83dbSDimitry Andric case GlobalValue::ExternalLinkage:
22385ffd83dbSDimitry Andric LinkageAttr = GV->isDeclaration() ? MCSA_Extern : MCSA_Global;
22395ffd83dbSDimitry Andric break;
22405ffd83dbSDimitry Andric case GlobalValue::LinkOnceAnyLinkage:
22415ffd83dbSDimitry Andric case GlobalValue::LinkOnceODRLinkage:
22425ffd83dbSDimitry Andric case GlobalValue::WeakAnyLinkage:
22435ffd83dbSDimitry Andric case GlobalValue::WeakODRLinkage:
22445ffd83dbSDimitry Andric case GlobalValue::ExternalWeakLinkage:
22455ffd83dbSDimitry Andric LinkageAttr = MCSA_Weak;
22465ffd83dbSDimitry Andric break;
22475ffd83dbSDimitry Andric case GlobalValue::AvailableExternallyLinkage:
22485ffd83dbSDimitry Andric LinkageAttr = MCSA_Extern;
22495ffd83dbSDimitry Andric break;
22505ffd83dbSDimitry Andric case GlobalValue::PrivateLinkage:
22515ffd83dbSDimitry Andric return;
22525ffd83dbSDimitry Andric case GlobalValue::InternalLinkage:
22535ffd83dbSDimitry Andric assert(GV->getVisibility() == GlobalValue::DefaultVisibility &&
22545ffd83dbSDimitry Andric "InternalLinkage should not have other visibility setting.");
22555ffd83dbSDimitry Andric LinkageAttr = MCSA_LGlobal;
22565ffd83dbSDimitry Andric break;
22575ffd83dbSDimitry Andric case GlobalValue::AppendingLinkage:
22585ffd83dbSDimitry Andric llvm_unreachable("Should never emit this");
22595ffd83dbSDimitry Andric case GlobalValue::CommonLinkage:
22605ffd83dbSDimitry Andric llvm_unreachable("CommonLinkage of XCOFF should not come to this path");
22615ffd83dbSDimitry Andric }
22625ffd83dbSDimitry Andric
22635ffd83dbSDimitry Andric assert(LinkageAttr != MCSA_Invalid && "LinkageAttr should not MCSA_Invalid.");
22645ffd83dbSDimitry Andric
22655ffd83dbSDimitry Andric MCSymbolAttr VisibilityAttr = MCSA_Invalid;
2266e8d8bef9SDimitry Andric if (!TM.getIgnoreXCOFFVisibility()) {
226781ad6265SDimitry Andric if (GV->hasDLLExportStorageClass() && !GV->hasDefaultVisibility())
226881ad6265SDimitry Andric report_fatal_error(
226981ad6265SDimitry Andric "Cannot not be both dllexport and non-default visibility");
22705ffd83dbSDimitry Andric switch (GV->getVisibility()) {
22715ffd83dbSDimitry Andric
227281ad6265SDimitry Andric // TODO: "internal" Visibility needs to go here.
22735ffd83dbSDimitry Andric case GlobalValue::DefaultVisibility:
227481ad6265SDimitry Andric if (GV->hasDLLExportStorageClass())
227581ad6265SDimitry Andric VisibilityAttr = MAI->getExportedVisibilityAttr();
22765ffd83dbSDimitry Andric break;
22775ffd83dbSDimitry Andric case GlobalValue::HiddenVisibility:
22785ffd83dbSDimitry Andric VisibilityAttr = MAI->getHiddenVisibilityAttr();
22795ffd83dbSDimitry Andric break;
22805ffd83dbSDimitry Andric case GlobalValue::ProtectedVisibility:
22815ffd83dbSDimitry Andric VisibilityAttr = MAI->getProtectedVisibilityAttr();
22825ffd83dbSDimitry Andric break;
22835ffd83dbSDimitry Andric }
2284e8d8bef9SDimitry Andric }
22855ffd83dbSDimitry Andric
2286*0fca6ea1SDimitry Andric // Do not emit the _$TLSML symbol.
2287*0fca6ea1SDimitry Andric if (GV->getThreadLocalMode() == GlobalVariable::LocalDynamicTLSModel &&
2288*0fca6ea1SDimitry Andric GV->hasName() && GV->getName() == "_$TLSML")
2289*0fca6ea1SDimitry Andric return;
2290*0fca6ea1SDimitry Andric
22915ffd83dbSDimitry Andric OutStreamer->emitXCOFFSymbolLinkageWithVisibility(GVSym, LinkageAttr,
22925ffd83dbSDimitry Andric VisibilityAttr);
22935ffd83dbSDimitry Andric }
22945ffd83dbSDimitry Andric
SetupMachineFunction(MachineFunction & MF)22958bcb0991SDimitry Andric void PPCAIXAsmPrinter::SetupMachineFunction(MachineFunction &MF) {
22965ffd83dbSDimitry Andric // Setup CurrentFnDescSym and its containing csect.
22975ffd83dbSDimitry Andric MCSectionXCOFF *FnDescSec =
22985ffd83dbSDimitry Andric cast<MCSectionXCOFF>(getObjFileLowering().getSectionForFunctionDescriptor(
22995ffd83dbSDimitry Andric &MF.getFunction(), TM));
23005ffd83dbSDimitry Andric FnDescSec->setAlignment(Align(Subtarget->isPPC64() ? 8 : 4));
23015ffd83dbSDimitry Andric
23025ffd83dbSDimitry Andric CurrentFnDescSym = FnDescSec->getQualNameSymbol();
23038bcb0991SDimitry Andric
23048bcb0991SDimitry Andric return AsmPrinter::SetupMachineFunction(MF);
23058bcb0991SDimitry Andric }
23068bcb0991SDimitry Andric
getNumberOfVRSaved()2307fe6060f1SDimitry Andric uint16_t PPCAIXAsmPrinter::getNumberOfVRSaved() {
2308fe6060f1SDimitry Andric // Calculate the number of VRs be saved.
2309fe6060f1SDimitry Andric // Vector registers 20 through 31 are marked as reserved and cannot be used
2310fe6060f1SDimitry Andric // in the default ABI.
2311fe6060f1SDimitry Andric const PPCSubtarget &Subtarget = MF->getSubtarget<PPCSubtarget>();
2312fe6060f1SDimitry Andric if (Subtarget.isAIXABI() && Subtarget.hasAltivec() &&
2313fe6060f1SDimitry Andric TM.getAIXExtendedAltivecABI()) {
2314fe6060f1SDimitry Andric const MachineRegisterInfo &MRI = MF->getRegInfo();
2315fe6060f1SDimitry Andric for (unsigned Reg = PPC::V20; Reg <= PPC::V31; ++Reg)
2316fe6060f1SDimitry Andric if (MRI.isPhysRegModified(Reg))
2317fe6060f1SDimitry Andric // Number of VRs saved.
2318fe6060f1SDimitry Andric return PPC::V31 - Reg + 1;
2319fe6060f1SDimitry Andric }
2320fe6060f1SDimitry Andric return 0;
2321fe6060f1SDimitry Andric }
2322fe6060f1SDimitry Andric
emitFunctionBodyEnd()2323e8d8bef9SDimitry Andric void PPCAIXAsmPrinter::emitFunctionBodyEnd() {
2324e8d8bef9SDimitry Andric
2325e8d8bef9SDimitry Andric if (!TM.getXCOFFTracebackTable())
2326e8d8bef9SDimitry Andric return;
2327e8d8bef9SDimitry Andric
2328e8d8bef9SDimitry Andric emitTracebackTable();
2329fe6060f1SDimitry Andric
2330fe6060f1SDimitry Andric // If ShouldEmitEHBlock returns true, then the eh info table
2331fe6060f1SDimitry Andric // will be emitted via `AIXException::endFunction`. Otherwise, we
2332fe6060f1SDimitry Andric // need to emit a dumy eh info table when VRs are saved. We could not
2333fe6060f1SDimitry Andric // consolidate these two places into one because there is no easy way
2334fe6060f1SDimitry Andric // to access register information in `AIXException` class.
2335fe6060f1SDimitry Andric if (!TargetLoweringObjectFileXCOFF::ShouldEmitEHBlock(MF) &&
2336fe6060f1SDimitry Andric (getNumberOfVRSaved() > 0)) {
2337fe6060f1SDimitry Andric // Emit dummy EH Info Table.
233881ad6265SDimitry Andric OutStreamer->switchSection(getObjFileLowering().getCompactUnwindSection());
2339fe6060f1SDimitry Andric MCSymbol *EHInfoLabel =
2340fe6060f1SDimitry Andric TargetLoweringObjectFileXCOFF::getEHInfoTableSymbol(MF);
2341fe6060f1SDimitry Andric OutStreamer->emitLabel(EHInfoLabel);
2342fe6060f1SDimitry Andric
2343fe6060f1SDimitry Andric // Version number.
2344fe6060f1SDimitry Andric OutStreamer->emitInt32(0);
2345fe6060f1SDimitry Andric
2346fe6060f1SDimitry Andric const DataLayout &DL = MMI->getModule()->getDataLayout();
2347fe6060f1SDimitry Andric const unsigned PointerSize = DL.getPointerSize();
2348fe6060f1SDimitry Andric // Add necessary paddings in 64 bit mode.
2349bdd1243dSDimitry Andric OutStreamer->emitValueToAlignment(Align(PointerSize));
2350fe6060f1SDimitry Andric
2351fe6060f1SDimitry Andric OutStreamer->emitIntValue(0, PointerSize);
2352fe6060f1SDimitry Andric OutStreamer->emitIntValue(0, PointerSize);
235381ad6265SDimitry Andric OutStreamer->switchSection(MF->getSection());
2354fe6060f1SDimitry Andric }
2355e8d8bef9SDimitry Andric }
2356e8d8bef9SDimitry Andric
emitTracebackTable()2357e8d8bef9SDimitry Andric void PPCAIXAsmPrinter::emitTracebackTable() {
2358e8d8bef9SDimitry Andric
2359e8d8bef9SDimitry Andric // Create a symbol for the end of function.
2360e8d8bef9SDimitry Andric MCSymbol *FuncEnd = createTempSymbol(MF->getName());
2361e8d8bef9SDimitry Andric OutStreamer->emitLabel(FuncEnd);
2362e8d8bef9SDimitry Andric
2363e8d8bef9SDimitry Andric OutStreamer->AddComment("Traceback table begin");
2364e8d8bef9SDimitry Andric // Begin with a fullword of zero.
2365e8d8bef9SDimitry Andric OutStreamer->emitIntValueInHexWithPadding(0, 4 /*size*/);
2366e8d8bef9SDimitry Andric
2367e8d8bef9SDimitry Andric SmallString<128> CommentString;
2368e8d8bef9SDimitry Andric raw_svector_ostream CommentOS(CommentString);
2369e8d8bef9SDimitry Andric
2370e8d8bef9SDimitry Andric auto EmitComment = [&]() {
2371e8d8bef9SDimitry Andric OutStreamer->AddComment(CommentOS.str());
2372e8d8bef9SDimitry Andric CommentString.clear();
2373e8d8bef9SDimitry Andric };
2374e8d8bef9SDimitry Andric
2375e8d8bef9SDimitry Andric auto EmitCommentAndValue = [&](uint64_t Value, int Size) {
2376e8d8bef9SDimitry Andric EmitComment();
2377e8d8bef9SDimitry Andric OutStreamer->emitIntValueInHexWithPadding(Value, Size);
2378e8d8bef9SDimitry Andric };
2379e8d8bef9SDimitry Andric
2380e8d8bef9SDimitry Andric unsigned int Version = 0;
2381e8d8bef9SDimitry Andric CommentOS << "Version = " << Version;
2382e8d8bef9SDimitry Andric EmitCommentAndValue(Version, 1);
2383e8d8bef9SDimitry Andric
2384e8d8bef9SDimitry Andric // There is a lack of information in the IR to assist with determining the
2385e8d8bef9SDimitry Andric // source language. AIX exception handling mechanism would only search for
2386e8d8bef9SDimitry Andric // personality routine and LSDA area when such language supports exception
2387e8d8bef9SDimitry Andric // handling. So to be conservatively correct and allow runtime to do its job,
2388e8d8bef9SDimitry Andric // we need to set it to C++ for now.
2389e8d8bef9SDimitry Andric TracebackTable::LanguageID LanguageIdentifier =
2390e8d8bef9SDimitry Andric TracebackTable::CPlusPlus; // C++
2391e8d8bef9SDimitry Andric
2392e8d8bef9SDimitry Andric CommentOS << "Language = "
2393e8d8bef9SDimitry Andric << getNameForTracebackTableLanguageId(LanguageIdentifier);
2394e8d8bef9SDimitry Andric EmitCommentAndValue(LanguageIdentifier, 1);
2395e8d8bef9SDimitry Andric
2396e8d8bef9SDimitry Andric // This is only populated for the third and fourth bytes.
2397e8d8bef9SDimitry Andric uint32_t FirstHalfOfMandatoryField = 0;
2398e8d8bef9SDimitry Andric
2399e8d8bef9SDimitry Andric // Emit the 3rd byte of the mandatory field.
2400e8d8bef9SDimitry Andric
2401e8d8bef9SDimitry Andric // We always set traceback offset bit to true.
2402e8d8bef9SDimitry Andric FirstHalfOfMandatoryField |= TracebackTable::HasTraceBackTableOffsetMask;
2403e8d8bef9SDimitry Andric
2404e8d8bef9SDimitry Andric const PPCFunctionInfo *FI = MF->getInfo<PPCFunctionInfo>();
2405e8d8bef9SDimitry Andric const MachineRegisterInfo &MRI = MF->getRegInfo();
2406e8d8bef9SDimitry Andric
2407e8d8bef9SDimitry Andric // Check the function uses floating-point processor instructions or not
2408e8d8bef9SDimitry Andric for (unsigned Reg = PPC::F0; Reg <= PPC::F31; ++Reg) {
2409fe6060f1SDimitry Andric if (MRI.isPhysRegUsed(Reg, /* SkipRegMaskTest */ true)) {
2410e8d8bef9SDimitry Andric FirstHalfOfMandatoryField |= TracebackTable::IsFloatingPointPresentMask;
2411e8d8bef9SDimitry Andric break;
2412e8d8bef9SDimitry Andric }
2413e8d8bef9SDimitry Andric }
2414e8d8bef9SDimitry Andric
2415e8d8bef9SDimitry Andric #define GENBOOLCOMMENT(Prefix, V, Field) \
2416e8d8bef9SDimitry Andric CommentOS << (Prefix) << ((V) & (TracebackTable::Field##Mask) ? "+" : "-") \
2417e8d8bef9SDimitry Andric << #Field
2418e8d8bef9SDimitry Andric
2419e8d8bef9SDimitry Andric #define GENVALUECOMMENT(PrefixAndName, V, Field) \
2420e8d8bef9SDimitry Andric CommentOS << (PrefixAndName) << " = " \
2421e8d8bef9SDimitry Andric << static_cast<unsigned>(((V) & (TracebackTable::Field##Mask)) >> \
2422e8d8bef9SDimitry Andric (TracebackTable::Field##Shift))
2423e8d8bef9SDimitry Andric
2424e8d8bef9SDimitry Andric GENBOOLCOMMENT("", FirstHalfOfMandatoryField, IsGlobaLinkage);
2425e8d8bef9SDimitry Andric GENBOOLCOMMENT(", ", FirstHalfOfMandatoryField, IsOutOfLineEpilogOrPrologue);
2426e8d8bef9SDimitry Andric EmitComment();
2427e8d8bef9SDimitry Andric
2428e8d8bef9SDimitry Andric GENBOOLCOMMENT("", FirstHalfOfMandatoryField, HasTraceBackTableOffset);
2429e8d8bef9SDimitry Andric GENBOOLCOMMENT(", ", FirstHalfOfMandatoryField, IsInternalProcedure);
2430e8d8bef9SDimitry Andric EmitComment();
2431e8d8bef9SDimitry Andric
2432e8d8bef9SDimitry Andric GENBOOLCOMMENT("", FirstHalfOfMandatoryField, HasControlledStorage);
2433e8d8bef9SDimitry Andric GENBOOLCOMMENT(", ", FirstHalfOfMandatoryField, IsTOCless);
2434e8d8bef9SDimitry Andric EmitComment();
2435e8d8bef9SDimitry Andric
2436e8d8bef9SDimitry Andric GENBOOLCOMMENT("", FirstHalfOfMandatoryField, IsFloatingPointPresent);
2437e8d8bef9SDimitry Andric EmitComment();
2438e8d8bef9SDimitry Andric GENBOOLCOMMENT("", FirstHalfOfMandatoryField,
2439e8d8bef9SDimitry Andric IsFloatingPointOperationLogOrAbortEnabled);
2440e8d8bef9SDimitry Andric EmitComment();
2441e8d8bef9SDimitry Andric
2442e8d8bef9SDimitry Andric OutStreamer->emitIntValueInHexWithPadding(
2443e8d8bef9SDimitry Andric (FirstHalfOfMandatoryField & 0x0000ff00) >> 8, 1);
2444e8d8bef9SDimitry Andric
2445e8d8bef9SDimitry Andric // Set the 4th byte of the mandatory field.
2446e8d8bef9SDimitry Andric FirstHalfOfMandatoryField |= TracebackTable::IsFunctionNamePresentMask;
2447e8d8bef9SDimitry Andric
2448349cc55cSDimitry Andric const PPCRegisterInfo *RegInfo =
2449349cc55cSDimitry Andric static_cast<const PPCRegisterInfo *>(Subtarget->getRegisterInfo());
2450349cc55cSDimitry Andric Register FrameReg = RegInfo->getFrameRegister(*MF);
2451349cc55cSDimitry Andric if (FrameReg == (Subtarget->isPPC64() ? PPC::X31 : PPC::R31))
2452e8d8bef9SDimitry Andric FirstHalfOfMandatoryField |= TracebackTable::IsAllocaUsedMask;
2453e8d8bef9SDimitry Andric
2454e8d8bef9SDimitry Andric const SmallVectorImpl<Register> &MustSaveCRs = FI->getMustSaveCRs();
2455e8d8bef9SDimitry Andric if (!MustSaveCRs.empty())
2456e8d8bef9SDimitry Andric FirstHalfOfMandatoryField |= TracebackTable::IsCRSavedMask;
2457e8d8bef9SDimitry Andric
2458e8d8bef9SDimitry Andric if (FI->mustSaveLR())
2459e8d8bef9SDimitry Andric FirstHalfOfMandatoryField |= TracebackTable::IsLRSavedMask;
2460e8d8bef9SDimitry Andric
2461e8d8bef9SDimitry Andric GENBOOLCOMMENT("", FirstHalfOfMandatoryField, IsInterruptHandler);
2462e8d8bef9SDimitry Andric GENBOOLCOMMENT(", ", FirstHalfOfMandatoryField, IsFunctionNamePresent);
2463e8d8bef9SDimitry Andric GENBOOLCOMMENT(", ", FirstHalfOfMandatoryField, IsAllocaUsed);
2464e8d8bef9SDimitry Andric EmitComment();
2465e8d8bef9SDimitry Andric GENVALUECOMMENT("OnConditionDirective", FirstHalfOfMandatoryField,
2466e8d8bef9SDimitry Andric OnConditionDirective);
2467e8d8bef9SDimitry Andric GENBOOLCOMMENT(", ", FirstHalfOfMandatoryField, IsCRSaved);
2468e8d8bef9SDimitry Andric GENBOOLCOMMENT(", ", FirstHalfOfMandatoryField, IsLRSaved);
2469e8d8bef9SDimitry Andric EmitComment();
2470e8d8bef9SDimitry Andric OutStreamer->emitIntValueInHexWithPadding((FirstHalfOfMandatoryField & 0xff),
2471e8d8bef9SDimitry Andric 1);
2472e8d8bef9SDimitry Andric
2473e8d8bef9SDimitry Andric // Set the 5th byte of mandatory field.
2474e8d8bef9SDimitry Andric uint32_t SecondHalfOfMandatoryField = 0;
2475e8d8bef9SDimitry Andric
247606c3fb27SDimitry Andric SecondHalfOfMandatoryField |= MF->getFrameInfo().getStackSize()
247706c3fb27SDimitry Andric ? TracebackTable::IsBackChainStoredMask
247806c3fb27SDimitry Andric : 0;
2479e8d8bef9SDimitry Andric
2480e8d8bef9SDimitry Andric uint32_t FPRSaved = 0;
2481e8d8bef9SDimitry Andric for (unsigned Reg = PPC::F14; Reg <= PPC::F31; ++Reg) {
2482e8d8bef9SDimitry Andric if (MRI.isPhysRegModified(Reg)) {
2483e8d8bef9SDimitry Andric FPRSaved = PPC::F31 - Reg + 1;
2484e8d8bef9SDimitry Andric break;
2485e8d8bef9SDimitry Andric }
2486e8d8bef9SDimitry Andric }
2487e8d8bef9SDimitry Andric SecondHalfOfMandatoryField |= (FPRSaved << TracebackTable::FPRSavedShift) &
2488e8d8bef9SDimitry Andric TracebackTable::FPRSavedMask;
2489e8d8bef9SDimitry Andric GENBOOLCOMMENT("", SecondHalfOfMandatoryField, IsBackChainStored);
2490e8d8bef9SDimitry Andric GENBOOLCOMMENT(", ", SecondHalfOfMandatoryField, IsFixup);
2491e8d8bef9SDimitry Andric GENVALUECOMMENT(", NumOfFPRsSaved", SecondHalfOfMandatoryField, FPRSaved);
2492e8d8bef9SDimitry Andric EmitComment();
2493e8d8bef9SDimitry Andric OutStreamer->emitIntValueInHexWithPadding(
2494e8d8bef9SDimitry Andric (SecondHalfOfMandatoryField & 0xff000000) >> 24, 1);
2495e8d8bef9SDimitry Andric
2496e8d8bef9SDimitry Andric // Set the 6th byte of mandatory field.
2497fe6060f1SDimitry Andric
2498fe6060f1SDimitry Andric // Check whether has Vector Instruction,We only treat instructions uses vector
2499fe6060f1SDimitry Andric // register as vector instructions.
2500fe6060f1SDimitry Andric bool HasVectorInst = false;
2501fe6060f1SDimitry Andric for (unsigned Reg = PPC::V0; Reg <= PPC::V31; ++Reg)
2502fe6060f1SDimitry Andric if (MRI.isPhysRegUsed(Reg, /* SkipRegMaskTest */ true)) {
2503fe6060f1SDimitry Andric // Has VMX instruction.
2504fe6060f1SDimitry Andric HasVectorInst = true;
2505fe6060f1SDimitry Andric break;
2506fe6060f1SDimitry Andric }
2507fe6060f1SDimitry Andric
2508fe6060f1SDimitry Andric if (FI->hasVectorParms() || HasVectorInst)
2509fe6060f1SDimitry Andric SecondHalfOfMandatoryField |= TracebackTable::HasVectorInfoMask;
2510fe6060f1SDimitry Andric
2511fe6060f1SDimitry Andric uint16_t NumOfVRSaved = getNumberOfVRSaved();
2512fe6060f1SDimitry Andric bool ShouldEmitEHBlock =
2513fe6060f1SDimitry Andric TargetLoweringObjectFileXCOFF::ShouldEmitEHBlock(MF) || NumOfVRSaved > 0;
2514fe6060f1SDimitry Andric
2515e8d8bef9SDimitry Andric if (ShouldEmitEHBlock)
2516e8d8bef9SDimitry Andric SecondHalfOfMandatoryField |= TracebackTable::HasExtensionTableMask;
2517e8d8bef9SDimitry Andric
2518e8d8bef9SDimitry Andric uint32_t GPRSaved = 0;
2519e8d8bef9SDimitry Andric
2520e8d8bef9SDimitry Andric // X13 is reserved under 64-bit environment.
2521e8d8bef9SDimitry Andric unsigned GPRBegin = Subtarget->isPPC64() ? PPC::X14 : PPC::R13;
2522e8d8bef9SDimitry Andric unsigned GPREnd = Subtarget->isPPC64() ? PPC::X31 : PPC::R31;
2523e8d8bef9SDimitry Andric
2524e8d8bef9SDimitry Andric for (unsigned Reg = GPRBegin; Reg <= GPREnd; ++Reg) {
2525e8d8bef9SDimitry Andric if (MRI.isPhysRegModified(Reg)) {
2526e8d8bef9SDimitry Andric GPRSaved = GPREnd - Reg + 1;
2527e8d8bef9SDimitry Andric break;
2528e8d8bef9SDimitry Andric }
2529e8d8bef9SDimitry Andric }
2530e8d8bef9SDimitry Andric
2531e8d8bef9SDimitry Andric SecondHalfOfMandatoryField |= (GPRSaved << TracebackTable::GPRSavedShift) &
2532e8d8bef9SDimitry Andric TracebackTable::GPRSavedMask;
2533e8d8bef9SDimitry Andric
2534fe6060f1SDimitry Andric GENBOOLCOMMENT("", SecondHalfOfMandatoryField, HasExtensionTable);
2535fe6060f1SDimitry Andric GENBOOLCOMMENT(", ", SecondHalfOfMandatoryField, HasVectorInfo);
2536e8d8bef9SDimitry Andric GENVALUECOMMENT(", NumOfGPRsSaved", SecondHalfOfMandatoryField, GPRSaved);
2537e8d8bef9SDimitry Andric EmitComment();
2538e8d8bef9SDimitry Andric OutStreamer->emitIntValueInHexWithPadding(
2539e8d8bef9SDimitry Andric (SecondHalfOfMandatoryField & 0x00ff0000) >> 16, 1);
2540e8d8bef9SDimitry Andric
2541e8d8bef9SDimitry Andric // Set the 7th byte of mandatory field.
2542fe6060f1SDimitry Andric uint32_t NumberOfFixedParms = FI->getFixedParmsNum();
2543e8d8bef9SDimitry Andric SecondHalfOfMandatoryField |=
2544fe6060f1SDimitry Andric (NumberOfFixedParms << TracebackTable::NumberOfFixedParmsShift) &
2545e8d8bef9SDimitry Andric TracebackTable::NumberOfFixedParmsMask;
2546e8d8bef9SDimitry Andric GENVALUECOMMENT("NumberOfFixedParms", SecondHalfOfMandatoryField,
2547e8d8bef9SDimitry Andric NumberOfFixedParms);
2548e8d8bef9SDimitry Andric EmitComment();
2549e8d8bef9SDimitry Andric OutStreamer->emitIntValueInHexWithPadding(
2550e8d8bef9SDimitry Andric (SecondHalfOfMandatoryField & 0x0000ff00) >> 8, 1);
2551e8d8bef9SDimitry Andric
2552e8d8bef9SDimitry Andric // Set the 8th byte of mandatory field.
2553e8d8bef9SDimitry Andric
2554e8d8bef9SDimitry Andric // Always set parameter on stack.
2555e8d8bef9SDimitry Andric SecondHalfOfMandatoryField |= TracebackTable::HasParmsOnStackMask;
2556e8d8bef9SDimitry Andric
2557fe6060f1SDimitry Andric uint32_t NumberOfFPParms = FI->getFloatingPointParmsNum();
2558e8d8bef9SDimitry Andric SecondHalfOfMandatoryField |=
2559fe6060f1SDimitry Andric (NumberOfFPParms << TracebackTable::NumberOfFloatingPointParmsShift) &
2560e8d8bef9SDimitry Andric TracebackTable::NumberOfFloatingPointParmsMask;
2561e8d8bef9SDimitry Andric
2562e8d8bef9SDimitry Andric GENVALUECOMMENT("NumberOfFPParms", SecondHalfOfMandatoryField,
2563e8d8bef9SDimitry Andric NumberOfFloatingPointParms);
2564e8d8bef9SDimitry Andric GENBOOLCOMMENT(", ", SecondHalfOfMandatoryField, HasParmsOnStack);
2565e8d8bef9SDimitry Andric EmitComment();
2566e8d8bef9SDimitry Andric OutStreamer->emitIntValueInHexWithPadding(SecondHalfOfMandatoryField & 0xff,
2567e8d8bef9SDimitry Andric 1);
2568e8d8bef9SDimitry Andric
2569e8d8bef9SDimitry Andric // Generate the optional fields of traceback table.
2570e8d8bef9SDimitry Andric
2571e8d8bef9SDimitry Andric // Parameter type.
2572fe6060f1SDimitry Andric if (NumberOfFixedParms || NumberOfFPParms) {
2573fe6060f1SDimitry Andric uint32_t ParmsTypeValue = FI->getParmsType();
2574e8d8bef9SDimitry Andric
2575fe6060f1SDimitry Andric Expected<SmallString<32>> ParmsType =
2576fe6060f1SDimitry Andric FI->hasVectorParms()
2577fe6060f1SDimitry Andric ? XCOFF::parseParmsTypeWithVecInfo(
2578fe6060f1SDimitry Andric ParmsTypeValue, NumberOfFixedParms, NumberOfFPParms,
2579fe6060f1SDimitry Andric FI->getVectorParmsNum())
2580fe6060f1SDimitry Andric : XCOFF::parseParmsType(ParmsTypeValue, NumberOfFixedParms,
2581fe6060f1SDimitry Andric NumberOfFPParms);
2582fe6060f1SDimitry Andric
2583fe6060f1SDimitry Andric assert(ParmsType && toString(ParmsType.takeError()).c_str());
2584fe6060f1SDimitry Andric if (ParmsType) {
2585fe6060f1SDimitry Andric CommentOS << "Parameter type = " << ParmsType.get();
2586fe6060f1SDimitry Andric EmitComment();
2587fe6060f1SDimitry Andric }
2588fe6060f1SDimitry Andric OutStreamer->emitIntValueInHexWithPadding(ParmsTypeValue,
2589fe6060f1SDimitry Andric sizeof(ParmsTypeValue));
2590fe6060f1SDimitry Andric }
2591e8d8bef9SDimitry Andric // Traceback table offset.
2592e8d8bef9SDimitry Andric OutStreamer->AddComment("Function size");
2593e8d8bef9SDimitry Andric if (FirstHalfOfMandatoryField & TracebackTable::HasTraceBackTableOffsetMask) {
2594e8d8bef9SDimitry Andric MCSymbol *FuncSectSym = getObjFileLowering().getFunctionEntryPointSymbol(
2595e8d8bef9SDimitry Andric &(MF->getFunction()), TM);
2596e8d8bef9SDimitry Andric OutStreamer->emitAbsoluteSymbolDiff(FuncEnd, FuncSectSym, 4);
2597e8d8bef9SDimitry Andric }
2598e8d8bef9SDimitry Andric
2599e8d8bef9SDimitry Andric // Since we unset the Int_Handler.
2600e8d8bef9SDimitry Andric if (FirstHalfOfMandatoryField & TracebackTable::IsInterruptHandlerMask)
2601e8d8bef9SDimitry Andric report_fatal_error("Hand_Mask not implement yet");
2602e8d8bef9SDimitry Andric
2603e8d8bef9SDimitry Andric if (FirstHalfOfMandatoryField & TracebackTable::HasControlledStorageMask)
2604e8d8bef9SDimitry Andric report_fatal_error("Ctl_Info not implement yet");
2605e8d8bef9SDimitry Andric
2606e8d8bef9SDimitry Andric if (FirstHalfOfMandatoryField & TracebackTable::IsFunctionNamePresentMask) {
2607e8d8bef9SDimitry Andric StringRef Name = MF->getName().substr(0, INT16_MAX);
2608e8d8bef9SDimitry Andric int16_t NameLength = Name.size();
2609e8d8bef9SDimitry Andric CommentOS << "Function name len = "
2610e8d8bef9SDimitry Andric << static_cast<unsigned int>(NameLength);
2611e8d8bef9SDimitry Andric EmitCommentAndValue(NameLength, 2);
2612e8d8bef9SDimitry Andric OutStreamer->AddComment("Function Name");
2613e8d8bef9SDimitry Andric OutStreamer->emitBytes(Name);
2614e8d8bef9SDimitry Andric }
2615e8d8bef9SDimitry Andric
2616e8d8bef9SDimitry Andric if (FirstHalfOfMandatoryField & TracebackTable::IsAllocaUsedMask) {
2617e8d8bef9SDimitry Andric uint8_t AllocReg = XCOFF::AllocRegNo;
2618e8d8bef9SDimitry Andric OutStreamer->AddComment("AllocaUsed");
2619e8d8bef9SDimitry Andric OutStreamer->emitIntValueInHex(AllocReg, sizeof(AllocReg));
2620e8d8bef9SDimitry Andric }
2621e8d8bef9SDimitry Andric
2622fe6060f1SDimitry Andric if (SecondHalfOfMandatoryField & TracebackTable::HasVectorInfoMask) {
2623fe6060f1SDimitry Andric uint16_t VRData = 0;
2624fe6060f1SDimitry Andric if (NumOfVRSaved) {
2625fe6060f1SDimitry Andric // Number of VRs saved.
2626fe6060f1SDimitry Andric VRData |= (NumOfVRSaved << TracebackTable::NumberOfVRSavedShift) &
2627fe6060f1SDimitry Andric TracebackTable::NumberOfVRSavedMask;
2628fe6060f1SDimitry Andric // This bit is supposed to set only when the special register
2629fe6060f1SDimitry Andric // VRSAVE is saved on stack.
2630fe6060f1SDimitry Andric // However, IBM XL compiler sets the bit when any vector registers
2631fe6060f1SDimitry Andric // are saved on the stack. We will follow XL's behavior on AIX
2632fe6060f1SDimitry Andric // so that we don't get surprise behavior change for C code.
2633fe6060f1SDimitry Andric VRData |= TracebackTable::IsVRSavedOnStackMask;
2634fe6060f1SDimitry Andric }
2635fe6060f1SDimitry Andric
2636fe6060f1SDimitry Andric // Set has_varargs.
2637fe6060f1SDimitry Andric if (FI->getVarArgsFrameIndex())
2638fe6060f1SDimitry Andric VRData |= TracebackTable::HasVarArgsMask;
2639fe6060f1SDimitry Andric
2640fe6060f1SDimitry Andric // Vector parameters number.
2641fe6060f1SDimitry Andric unsigned VectorParmsNum = FI->getVectorParmsNum();
2642fe6060f1SDimitry Andric VRData |= (VectorParmsNum << TracebackTable::NumberOfVectorParmsShift) &
2643fe6060f1SDimitry Andric TracebackTable::NumberOfVectorParmsMask;
2644fe6060f1SDimitry Andric
2645fe6060f1SDimitry Andric if (HasVectorInst)
2646fe6060f1SDimitry Andric VRData |= TracebackTable::HasVMXInstructionMask;
2647fe6060f1SDimitry Andric
2648fe6060f1SDimitry Andric GENVALUECOMMENT("NumOfVRsSaved", VRData, NumberOfVRSaved);
2649fe6060f1SDimitry Andric GENBOOLCOMMENT(", ", VRData, IsVRSavedOnStack);
2650fe6060f1SDimitry Andric GENBOOLCOMMENT(", ", VRData, HasVarArgs);
2651fe6060f1SDimitry Andric EmitComment();
2652fe6060f1SDimitry Andric OutStreamer->emitIntValueInHexWithPadding((VRData & 0xff00) >> 8, 1);
2653fe6060f1SDimitry Andric
2654fe6060f1SDimitry Andric GENVALUECOMMENT("NumOfVectorParams", VRData, NumberOfVectorParms);
2655fe6060f1SDimitry Andric GENBOOLCOMMENT(", ", VRData, HasVMXInstruction);
2656fe6060f1SDimitry Andric EmitComment();
2657fe6060f1SDimitry Andric OutStreamer->emitIntValueInHexWithPadding(VRData & 0x00ff, 1);
2658fe6060f1SDimitry Andric
2659fe6060f1SDimitry Andric uint32_t VecParmTypeValue = FI->getVecExtParmsType();
2660fe6060f1SDimitry Andric
2661fe6060f1SDimitry Andric Expected<SmallString<32>> VecParmsType =
2662fe6060f1SDimitry Andric XCOFF::parseVectorParmsType(VecParmTypeValue, VectorParmsNum);
2663fe6060f1SDimitry Andric assert(VecParmsType && toString(VecParmsType.takeError()).c_str());
2664fe6060f1SDimitry Andric if (VecParmsType) {
2665fe6060f1SDimitry Andric CommentOS << "Vector Parameter type = " << VecParmsType.get();
2666fe6060f1SDimitry Andric EmitComment();
2667fe6060f1SDimitry Andric }
2668fe6060f1SDimitry Andric OutStreamer->emitIntValueInHexWithPadding(VecParmTypeValue,
2669fe6060f1SDimitry Andric sizeof(VecParmTypeValue));
2670fe6060f1SDimitry Andric // Padding 2 bytes.
2671fe6060f1SDimitry Andric CommentOS << "Padding";
2672fe6060f1SDimitry Andric EmitCommentAndValue(0, 2);
2673fe6060f1SDimitry Andric }
2674fe6060f1SDimitry Andric
2675e8d8bef9SDimitry Andric uint8_t ExtensionTableFlag = 0;
2676e8d8bef9SDimitry Andric if (SecondHalfOfMandatoryField & TracebackTable::HasExtensionTableMask) {
2677e8d8bef9SDimitry Andric if (ShouldEmitEHBlock)
2678e8d8bef9SDimitry Andric ExtensionTableFlag |= ExtendedTBTableFlag::TB_EH_INFO;
2679fe6060f1SDimitry Andric if (EnableSSPCanaryBitInTB &&
2680fe6060f1SDimitry Andric TargetLoweringObjectFileXCOFF::ShouldSetSSPCanaryBitInTB(MF))
2681fe6060f1SDimitry Andric ExtensionTableFlag |= ExtendedTBTableFlag::TB_SSP_CANARY;
2682e8d8bef9SDimitry Andric
2683e8d8bef9SDimitry Andric CommentOS << "ExtensionTableFlag = "
2684e8d8bef9SDimitry Andric << getExtendedTBTableFlagString(ExtensionTableFlag);
2685e8d8bef9SDimitry Andric EmitCommentAndValue(ExtensionTableFlag, sizeof(ExtensionTableFlag));
2686e8d8bef9SDimitry Andric }
2687e8d8bef9SDimitry Andric
2688e8d8bef9SDimitry Andric if (ExtensionTableFlag & ExtendedTBTableFlag::TB_EH_INFO) {
2689e8d8bef9SDimitry Andric auto &Ctx = OutStreamer->getContext();
2690e8d8bef9SDimitry Andric MCSymbol *EHInfoSym =
2691e8d8bef9SDimitry Andric TargetLoweringObjectFileXCOFF::getEHInfoTableSymbol(MF);
269206c3fb27SDimitry Andric MCSymbol *TOCEntry = lookUpOrCreateTOCEntry(EHInfoSym, TOCType_EHBlock);
2693e8d8bef9SDimitry Andric const MCSymbol *TOCBaseSym =
2694e8d8bef9SDimitry Andric cast<MCSectionXCOFF>(getObjFileLowering().getTOCBaseSection())
2695e8d8bef9SDimitry Andric ->getQualNameSymbol();
2696e8d8bef9SDimitry Andric const MCExpr *Exp =
2697e8d8bef9SDimitry Andric MCBinaryExpr::createSub(MCSymbolRefExpr::create(TOCEntry, Ctx),
2698e8d8bef9SDimitry Andric MCSymbolRefExpr::create(TOCBaseSym, Ctx), Ctx);
2699e8d8bef9SDimitry Andric
2700e8d8bef9SDimitry Andric const DataLayout &DL = getDataLayout();
2701bdd1243dSDimitry Andric OutStreamer->emitValueToAlignment(Align(4));
2702e8d8bef9SDimitry Andric OutStreamer->AddComment("EHInfo Table");
2703e8d8bef9SDimitry Andric OutStreamer->emitValue(Exp, DL.getPointerSize());
2704e8d8bef9SDimitry Andric }
2705e8d8bef9SDimitry Andric #undef GENBOOLCOMMENT
2706e8d8bef9SDimitry Andric #undef GENVALUECOMMENT
2707e8d8bef9SDimitry Andric }
2708e8d8bef9SDimitry Andric
isSpecialLLVMGlobalArrayToSkip(const GlobalVariable * GV)2709e8d8bef9SDimitry Andric static bool isSpecialLLVMGlobalArrayToSkip(const GlobalVariable *GV) {
2710e8d8bef9SDimitry Andric return GV->hasAppendingLinkage() &&
2711e8d8bef9SDimitry Andric StringSwitch<bool>(GV->getName())
2712e8d8bef9SDimitry Andric // TODO: Linker could still eliminate the GV if we just skip
2713e8d8bef9SDimitry Andric // handling llvm.used array. Skipping them for now until we or the
2714e8d8bef9SDimitry Andric // AIX OS team come up with a good solution.
2715e8d8bef9SDimitry Andric .Case("llvm.used", true)
2716e8d8bef9SDimitry Andric // It's correct to just skip llvm.compiler.used array here.
2717e8d8bef9SDimitry Andric .Case("llvm.compiler.used", true)
2718e8d8bef9SDimitry Andric .Default(false);
2719e8d8bef9SDimitry Andric }
2720e8d8bef9SDimitry Andric
isSpecialLLVMGlobalArrayForStaticInit(const GlobalVariable * GV)27215ffd83dbSDimitry Andric static bool isSpecialLLVMGlobalArrayForStaticInit(const GlobalVariable *GV) {
27225ffd83dbSDimitry Andric return StringSwitch<bool>(GV->getName())
27235ffd83dbSDimitry Andric .Cases("llvm.global_ctors", "llvm.global_dtors", true)
27245ffd83dbSDimitry Andric .Default(false);
2725480093f4SDimitry Andric }
2726480093f4SDimitry Andric
getAliasOffset(const Constant * C)2727753f127fSDimitry Andric uint64_t PPCAIXAsmPrinter::getAliasOffset(const Constant *C) {
2728753f127fSDimitry Andric if (auto *GA = dyn_cast<GlobalAlias>(C))
2729753f127fSDimitry Andric return getAliasOffset(GA->getAliasee());
2730753f127fSDimitry Andric if (auto *CE = dyn_cast<ConstantExpr>(C)) {
2731753f127fSDimitry Andric const MCExpr *LowC = lowerConstant(CE);
2732753f127fSDimitry Andric const MCBinaryExpr *CBE = dyn_cast<MCBinaryExpr>(LowC);
2733753f127fSDimitry Andric if (!CBE)
2734753f127fSDimitry Andric return 0;
2735753f127fSDimitry Andric if (CBE->getOpcode() != MCBinaryExpr::Add)
2736753f127fSDimitry Andric report_fatal_error("Only adding an offset is supported now.");
2737753f127fSDimitry Andric auto *RHS = dyn_cast<MCConstantExpr>(CBE->getRHS());
2738753f127fSDimitry Andric if (!RHS)
2739753f127fSDimitry Andric report_fatal_error("Unable to get the offset of alias.");
2740753f127fSDimitry Andric return RHS->getValue();
2741753f127fSDimitry Andric }
2742753f127fSDimitry Andric return 0;
2743753f127fSDimitry Andric }
2744753f127fSDimitry Andric
tocDataChecks(unsigned PointerSize,const GlobalVariable * GV)2745*0fca6ea1SDimitry Andric static void tocDataChecks(unsigned PointerSize, const GlobalVariable *GV) {
2746*0fca6ea1SDimitry Andric // TODO: These asserts should be updated as more support for the toc data
2747*0fca6ea1SDimitry Andric // transformation is added (struct support, etc.).
2748*0fca6ea1SDimitry Andric assert(
2749*0fca6ea1SDimitry Andric PointerSize >= GV->getAlign().valueOrOne().value() &&
2750*0fca6ea1SDimitry Andric "GlobalVariables with an alignment requirement stricter than TOC entry "
2751*0fca6ea1SDimitry Andric "size not supported by the toc data transformation.");
2752*0fca6ea1SDimitry Andric
2753*0fca6ea1SDimitry Andric Type *GVType = GV->getValueType();
2754*0fca6ea1SDimitry Andric assert(GVType->isSized() && "A GlobalVariable's size must be known to be "
2755*0fca6ea1SDimitry Andric "supported by the toc data transformation.");
2756*0fca6ea1SDimitry Andric if (GV->getDataLayout().getTypeSizeInBits(GVType) >
2757*0fca6ea1SDimitry Andric PointerSize * 8)
2758*0fca6ea1SDimitry Andric report_fatal_error(
2759*0fca6ea1SDimitry Andric "A GlobalVariable with size larger than a TOC entry is not currently "
2760*0fca6ea1SDimitry Andric "supported by the toc data transformation.");
2761*0fca6ea1SDimitry Andric if (GV->hasPrivateLinkage())
2762*0fca6ea1SDimitry Andric report_fatal_error("A GlobalVariable with private linkage is not "
2763*0fca6ea1SDimitry Andric "currently supported by the toc data transformation.");
2764*0fca6ea1SDimitry Andric }
2765*0fca6ea1SDimitry Andric
emitGlobalVariable(const GlobalVariable * GV)27665ffd83dbSDimitry Andric void PPCAIXAsmPrinter::emitGlobalVariable(const GlobalVariable *GV) {
2767e8d8bef9SDimitry Andric // Special LLVM global arrays have been handled at the initialization.
2768e8d8bef9SDimitry Andric if (isSpecialLLVMGlobalArrayToSkip(GV) || isSpecialLLVMGlobalArrayForStaticInit(GV))
2769480093f4SDimitry Andric return;
2770480093f4SDimitry Andric
2771fe6060f1SDimitry Andric // If the Global Variable has the toc-data attribute, it needs to be emitted
2772fe6060f1SDimitry Andric // when we emit the .toc section.
2773fe6060f1SDimitry Andric if (GV->hasAttribute("toc-data")) {
2774*0fca6ea1SDimitry Andric unsigned PointerSize = GV->getDataLayout().getPointerSize();
2775*0fca6ea1SDimitry Andric tocDataChecks(PointerSize, GV);
2776fe6060f1SDimitry Andric TOCDataGlobalVars.push_back(GV);
2777fe6060f1SDimitry Andric return;
2778fe6060f1SDimitry Andric }
2779fe6060f1SDimitry Andric
2780fe6060f1SDimitry Andric emitGlobalVariableHelper(GV);
2781fe6060f1SDimitry Andric }
2782fe6060f1SDimitry Andric
emitGlobalVariableHelper(const GlobalVariable * GV)2783fe6060f1SDimitry Andric void PPCAIXAsmPrinter::emitGlobalVariableHelper(const GlobalVariable *GV) {
27845f757f3fSDimitry Andric assert(!GV->getName().starts_with("llvm.") &&
2785e8d8bef9SDimitry Andric "Unhandled intrinsic global variable.");
2786fe6060f1SDimitry Andric
2787fe6060f1SDimitry Andric if (GV->hasComdat())
2788fe6060f1SDimitry Andric report_fatal_error("COMDAT not yet supported by AIX.");
2789e8d8bef9SDimitry Andric
2790480093f4SDimitry Andric MCSymbolXCOFF *GVSym = cast<MCSymbolXCOFF>(getSymbol(GV));
27918bcb0991SDimitry Andric
27925ffd83dbSDimitry Andric if (GV->isDeclarationForLinker()) {
27935ffd83dbSDimitry Andric emitLinkage(GV, GVSym);
27945ffd83dbSDimitry Andric return;
27955ffd83dbSDimitry Andric }
27965ffd83dbSDimitry Andric
27978bcb0991SDimitry Andric SectionKind GVKind = getObjFileLowering().getKindForGlobal(GV, TM);
2798fe6060f1SDimitry Andric if (!GVKind.isGlobalWriteableData() && !GVKind.isReadOnly() &&
2799fe6060f1SDimitry Andric !GVKind.isThreadLocal()) // Checks for both ThreadData and ThreadBSS.
28008bcb0991SDimitry Andric report_fatal_error("Encountered a global variable kind that is "
28018bcb0991SDimitry Andric "not supported yet.");
28028bcb0991SDimitry Andric
2803fe6060f1SDimitry Andric // Print GV in verbose mode
2804fe6060f1SDimitry Andric if (isVerbose()) {
2805fe6060f1SDimitry Andric if (GV->hasInitializer()) {
280681ad6265SDimitry Andric GV->printAsOperand(OutStreamer->getCommentOS(),
2807fe6060f1SDimitry Andric /*PrintType=*/false, GV->getParent());
280881ad6265SDimitry Andric OutStreamer->getCommentOS() << '\n';
2809fe6060f1SDimitry Andric }
2810fe6060f1SDimitry Andric }
2811fe6060f1SDimitry Andric
2812480093f4SDimitry Andric MCSectionXCOFF *Csect = cast<MCSectionXCOFF>(
28138bcb0991SDimitry Andric getObjFileLowering().SectionForGlobal(GV, GVKind, TM));
28145ffd83dbSDimitry Andric
28155ffd83dbSDimitry Andric // Switch to the containing csect.
281681ad6265SDimitry Andric OutStreamer->switchSection(Csect);
28178bcb0991SDimitry Andric
2818*0fca6ea1SDimitry Andric const DataLayout &DL = GV->getDataLayout();
28198bcb0991SDimitry Andric
2820fe6060f1SDimitry Andric // Handle common and zero-initialized local symbols.
2821fe6060f1SDimitry Andric if (GV->hasCommonLinkage() || GVKind.isBSSLocal() ||
2822fe6060f1SDimitry Andric GVKind.isThreadBSSLocal()) {
282381ad6265SDimitry Andric Align Alignment = GV->getAlign().value_or(DL.getPreferredAlign(GV));
2824fe6060f1SDimitry Andric uint64_t Size = DL.getTypeAllocSize(GV->getValueType());
2825e8d8bef9SDimitry Andric GVSym->setStorageClass(
2826e8d8bef9SDimitry Andric TargetLoweringObjectFileXCOFF::getStorageClassForGlobal(GV));
28278bcb0991SDimitry Andric
2828*0fca6ea1SDimitry Andric if (GVKind.isBSSLocal() && Csect->getMappingClass() == XCOFF::XMC_TD) {
2829*0fca6ea1SDimitry Andric OutStreamer->emitZeros(Size);
2830*0fca6ea1SDimitry Andric } else if (GVKind.isBSSLocal() || GVKind.isThreadBSSLocal()) {
2831*0fca6ea1SDimitry Andric assert(Csect->getMappingClass() != XCOFF::XMC_TD &&
2832*0fca6ea1SDimitry Andric "BSS local toc-data already handled and TLS variables "
2833*0fca6ea1SDimitry Andric "incompatible with XMC_TD");
28345ffd83dbSDimitry Andric OutStreamer->emitXCOFFLocalCommonSymbol(
2835e8d8bef9SDimitry Andric OutContext.getOrCreateSymbol(GVSym->getSymbolTableName()), Size,
2836bdd1243dSDimitry Andric GVSym, Alignment);
2837*0fca6ea1SDimitry Andric } else {
2838bdd1243dSDimitry Andric OutStreamer->emitCommonSymbol(GVSym, Size, Alignment);
2839*0fca6ea1SDimitry Andric }
28408bcb0991SDimitry Andric return;
28418bcb0991SDimitry Andric }
28428bcb0991SDimitry Andric
28438bcb0991SDimitry Andric MCSymbol *EmittedInitSym = GVSym;
2844753f127fSDimitry Andric
2845753f127fSDimitry Andric // Emit linkage for the global variable and its aliases.
28465ffd83dbSDimitry Andric emitLinkage(GV, EmittedInitSym);
2847753f127fSDimitry Andric for (const GlobalAlias *GA : GOAliasMap[GV])
2848753f127fSDimitry Andric emitLinkage(GA, getSymbol(GA));
2849753f127fSDimitry Andric
28505ffd83dbSDimitry Andric emitAlignment(getGVAlignment(GV, DL), GV);
2851e8d8bef9SDimitry Andric
2852e8d8bef9SDimitry Andric // When -fdata-sections is enabled, every GlobalVariable will
2853e8d8bef9SDimitry Andric // be put into its own csect; therefore, label is not necessary here.
2854*0fca6ea1SDimitry Andric if (!TM.getDataSections() || GV->hasSection()) {
2855*0fca6ea1SDimitry Andric if (Csect->getMappingClass() != XCOFF::XMC_TD)
28565ffd83dbSDimitry Andric OutStreamer->emitLabel(EmittedInitSym);
2857*0fca6ea1SDimitry Andric }
2858753f127fSDimitry Andric
2859753f127fSDimitry Andric // No alias to emit.
2860753f127fSDimitry Andric if (!GOAliasMap[GV].size()) {
2861*0fca6ea1SDimitry Andric emitGlobalConstant(GV->getDataLayout(), GV->getInitializer());
2862753f127fSDimitry Andric return;
2863e8d8bef9SDimitry Andric }
2864e8d8bef9SDimitry Andric
2865753f127fSDimitry Andric // Aliases with the same offset should be aligned. Record the list of aliases
2866753f127fSDimitry Andric // associated with the offset.
2867753f127fSDimitry Andric AliasMapTy AliasList;
2868753f127fSDimitry Andric for (const GlobalAlias *GA : GOAliasMap[GV])
2869753f127fSDimitry Andric AliasList[getAliasOffset(GA->getAliasee())].push_back(GA);
2870e8d8bef9SDimitry Andric
2871753f127fSDimitry Andric // Emit alias label and element value for global variable.
2872*0fca6ea1SDimitry Andric emitGlobalConstant(GV->getDataLayout(), GV->getInitializer(),
2873753f127fSDimitry Andric &AliasList);
28748bcb0991SDimitry Andric }
28758bcb0991SDimitry Andric
emitFunctionDescriptor()28765ffd83dbSDimitry Andric void PPCAIXAsmPrinter::emitFunctionDescriptor() {
28778bcb0991SDimitry Andric const DataLayout &DL = getDataLayout();
28788bcb0991SDimitry Andric const unsigned PointerSize = DL.getPointerSizeInBits() == 64 ? 8 : 4;
28798bcb0991SDimitry Andric
28808bcb0991SDimitry Andric MCSectionSubPair Current = OutStreamer->getCurrentSection();
28818bcb0991SDimitry Andric // Emit function descriptor.
288281ad6265SDimitry Andric OutStreamer->switchSection(
28835ffd83dbSDimitry Andric cast<MCSymbolXCOFF>(CurrentFnDescSym)->getRepresentedCsect());
2884e8d8bef9SDimitry Andric
2885e8d8bef9SDimitry Andric // Emit aliasing label for function descriptor csect.
288681ad6265SDimitry Andric for (const GlobalAlias *Alias : GOAliasMap[&MF->getFunction()])
2887e8d8bef9SDimitry Andric OutStreamer->emitLabel(getSymbol(Alias));
2888e8d8bef9SDimitry Andric
28898bcb0991SDimitry Andric // Emit function entry point address.
28905ffd83dbSDimitry Andric OutStreamer->emitValue(MCSymbolRefExpr::create(CurrentFnSym, OutContext),
28918bcb0991SDimitry Andric PointerSize);
28928bcb0991SDimitry Andric // Emit TOC base address.
28935ffd83dbSDimitry Andric const MCSymbol *TOCBaseSym =
28945ffd83dbSDimitry Andric cast<MCSectionXCOFF>(getObjFileLowering().getTOCBaseSection())
28955ffd83dbSDimitry Andric ->getQualNameSymbol();
28965ffd83dbSDimitry Andric OutStreamer->emitValue(MCSymbolRefExpr::create(TOCBaseSym, OutContext),
28978bcb0991SDimitry Andric PointerSize);
28988bcb0991SDimitry Andric // Emit a null environment pointer.
28995ffd83dbSDimitry Andric OutStreamer->emitIntValue(0, PointerSize);
29008bcb0991SDimitry Andric
290181ad6265SDimitry Andric OutStreamer->switchSection(Current.first, Current.second);
29028bcb0991SDimitry Andric }
29038bcb0991SDimitry Andric
emitFunctionEntryLabel()2904e8d8bef9SDimitry Andric void PPCAIXAsmPrinter::emitFunctionEntryLabel() {
2905*0fca6ea1SDimitry Andric // For functions without user defined section, it's not necessary to emit the
2906*0fca6ea1SDimitry Andric // label when we have individual function in its own csect.
2907*0fca6ea1SDimitry Andric if (!TM.getFunctionSections() || MF->getFunction().hasSection())
2908e8d8bef9SDimitry Andric PPCAsmPrinter::emitFunctionEntryLabel();
2909e8d8bef9SDimitry Andric
2910e8d8bef9SDimitry Andric // Emit aliasing label for function entry point label.
291181ad6265SDimitry Andric for (const GlobalAlias *Alias : GOAliasMap[&MF->getFunction()])
2912e8d8bef9SDimitry Andric OutStreamer->emitLabel(
2913e8d8bef9SDimitry Andric getObjFileLowering().getFunctionEntryPointSymbol(Alias, TM));
291481ad6265SDimitry Andric }
291581ad6265SDimitry Andric
emitPGORefs(Module & M)29164542f901SDimitry Andric void PPCAIXAsmPrinter::emitPGORefs(Module &M) {
29174542f901SDimitry Andric if (!OutContext.hasXCOFFSection(
291881ad6265SDimitry Andric "__llvm_prf_cnts",
29194542f901SDimitry Andric XCOFF::CsectProperties(XCOFF::XMC_RW, XCOFF::XTY_SD)))
29204542f901SDimitry Andric return;
29214542f901SDimitry Andric
29224542f901SDimitry Andric // When inside a csect `foo`, a .ref directive referring to a csect `bar`
29234542f901SDimitry Andric // translates into a relocation entry from `foo` to` bar`. The referring
29244542f901SDimitry Andric // csect, `foo`, is identified by its address. If multiple csects have the
29254542f901SDimitry Andric // same address (because one or more of them are zero-length), the referring
29264542f901SDimitry Andric // csect cannot be determined. Hence, we don't generate the .ref directives
29274542f901SDimitry Andric // if `__llvm_prf_cnts` is an empty section.
29284542f901SDimitry Andric bool HasNonZeroLengthPrfCntsSection = false;
29294542f901SDimitry Andric const DataLayout &DL = M.getDataLayout();
29304542f901SDimitry Andric for (GlobalVariable &GV : M.globals())
2931*0fca6ea1SDimitry Andric if (GV.hasSection() && GV.getSection() == "__llvm_prf_cnts" &&
29324542f901SDimitry Andric DL.getTypeAllocSize(GV.getValueType()) > 0) {
29334542f901SDimitry Andric HasNonZeroLengthPrfCntsSection = true;
29344542f901SDimitry Andric break;
29354542f901SDimitry Andric }
29364542f901SDimitry Andric
29374542f901SDimitry Andric if (HasNonZeroLengthPrfCntsSection) {
293881ad6265SDimitry Andric MCSection *CntsSection = OutContext.getXCOFFSection(
293981ad6265SDimitry Andric "__llvm_prf_cnts", SectionKind::getData(),
294081ad6265SDimitry Andric XCOFF::CsectProperties(XCOFF::XMC_RW, XCOFF::XTY_SD),
294181ad6265SDimitry Andric /*MultiSymbolsAllowed*/ true);
294281ad6265SDimitry Andric
294381ad6265SDimitry Andric OutStreamer->switchSection(CntsSection);
294481ad6265SDimitry Andric if (OutContext.hasXCOFFSection(
294581ad6265SDimitry Andric "__llvm_prf_data",
294606c3fb27SDimitry Andric XCOFF::CsectProperties(XCOFF::XMC_RW, XCOFF::XTY_SD))) {
294706c3fb27SDimitry Andric MCSymbol *S = OutContext.getOrCreateSymbol("__llvm_prf_data[RW]");
294806c3fb27SDimitry Andric OutStreamer->emitXCOFFRefDirective(S);
294906c3fb27SDimitry Andric }
295081ad6265SDimitry Andric if (OutContext.hasXCOFFSection(
295181ad6265SDimitry Andric "__llvm_prf_names",
295206c3fb27SDimitry Andric XCOFF::CsectProperties(XCOFF::XMC_RO, XCOFF::XTY_SD))) {
295306c3fb27SDimitry Andric MCSymbol *S = OutContext.getOrCreateSymbol("__llvm_prf_names[RO]");
295406c3fb27SDimitry Andric OutStreamer->emitXCOFFRefDirective(S);
295506c3fb27SDimitry Andric }
295681ad6265SDimitry Andric if (OutContext.hasXCOFFSection(
295781ad6265SDimitry Andric "__llvm_prf_vnds",
295806c3fb27SDimitry Andric XCOFF::CsectProperties(XCOFF::XMC_RW, XCOFF::XTY_SD))) {
295906c3fb27SDimitry Andric MCSymbol *S = OutContext.getOrCreateSymbol("__llvm_prf_vnds[RW]");
296006c3fb27SDimitry Andric OutStreamer->emitXCOFFRefDirective(S);
296106c3fb27SDimitry Andric }
296281ad6265SDimitry Andric }
2963e8d8bef9SDimitry Andric }
2964e8d8bef9SDimitry Andric
emitEndOfAsmFile(Module & M)29655ffd83dbSDimitry Andric void PPCAIXAsmPrinter::emitEndOfAsmFile(Module &M) {
2966fe6060f1SDimitry Andric // If there are no functions and there are no toc-data definitions in this
2967fe6060f1SDimitry Andric // module, we will never need to reference the TOC base.
2968fe6060f1SDimitry Andric if (M.empty() && TOCDataGlobalVars.empty())
29698bcb0991SDimitry Andric return;
29708bcb0991SDimitry Andric
29714542f901SDimitry Andric emitPGORefs(M);
297281ad6265SDimitry Andric
29738bcb0991SDimitry Andric // Switch to section to emit TOC base.
297481ad6265SDimitry Andric OutStreamer->switchSection(getObjFileLowering().getTOCBaseSection());
2975480093f4SDimitry Andric
29765ffd83dbSDimitry Andric PPCTargetStreamer *TS =
29775ffd83dbSDimitry Andric static_cast<PPCTargetStreamer *>(OutStreamer->getTargetStreamer());
29785ffd83dbSDimitry Andric
2979480093f4SDimitry Andric for (auto &I : TOC) {
2980fe6060f1SDimitry Andric MCSectionXCOFF *TCEntry;
2981fe6060f1SDimitry Andric // Setup the csect for the current TC entry. If the variant kind is
2982fe6060f1SDimitry Andric // VK_PPC_AIX_TLSGDM the entry represents the region handle, we create a
2983fe6060f1SDimitry Andric // new symbol to prefix the name with a dot.
2984*0fca6ea1SDimitry Andric // If TLS model opt is turned on, create a new symbol to prefix the name
2985*0fca6ea1SDimitry Andric // with a dot.
2986*0fca6ea1SDimitry Andric if (I.first.second == MCSymbolRefExpr::VariantKind::VK_PPC_AIX_TLSGDM ||
2987*0fca6ea1SDimitry Andric (Subtarget->hasAIXShLibTLSModelOpt() &&
2988*0fca6ea1SDimitry Andric I.first.second == MCSymbolRefExpr::VariantKind::VK_PPC_AIX_TLSLD)) {
2989fe6060f1SDimitry Andric SmallString<128> Name;
2990fe6060f1SDimitry Andric StringRef Prefix = ".";
2991fe6060f1SDimitry Andric Name += Prefix;
2992bdd1243dSDimitry Andric Name += cast<MCSymbolXCOFF>(I.first.first)->getSymbolTableName();
2993fe6060f1SDimitry Andric MCSymbol *S = OutContext.getOrCreateSymbol(Name);
2994fe6060f1SDimitry Andric TCEntry = cast<MCSectionXCOFF>(
2995fe6060f1SDimitry Andric getObjFileLowering().getSectionForTOCEntry(S, TM));
2996fe6060f1SDimitry Andric } else {
2997fe6060f1SDimitry Andric TCEntry = cast<MCSectionXCOFF>(
2998fe6060f1SDimitry Andric getObjFileLowering().getSectionForTOCEntry(I.first.first, TM));
2999fe6060f1SDimitry Andric }
300081ad6265SDimitry Andric OutStreamer->switchSection(TCEntry);
3001480093f4SDimitry Andric
30025ffd83dbSDimitry Andric OutStreamer->emitLabel(I.second);
3003fe6060f1SDimitry Andric TS->emitTCEntry(*I.first.first, I.first.second);
3004480093f4SDimitry Andric }
3005fe6060f1SDimitry Andric
3006*0fca6ea1SDimitry Andric // Traverse the list of global variables twice, emitting all of the
3007*0fca6ea1SDimitry Andric // non-common global variables before the common ones, as emitting a
3008*0fca6ea1SDimitry Andric // .comm directive changes the scope from .toc to the common symbol.
3009*0fca6ea1SDimitry Andric for (const auto *GV : TOCDataGlobalVars) {
3010*0fca6ea1SDimitry Andric if (!GV->hasCommonLinkage())
3011fe6060f1SDimitry Andric emitGlobalVariableHelper(GV);
30128bcb0991SDimitry Andric }
3013*0fca6ea1SDimitry Andric for (const auto *GV : TOCDataGlobalVars) {
3014*0fca6ea1SDimitry Andric if (GV->hasCommonLinkage())
3015*0fca6ea1SDimitry Andric emitGlobalVariableHelper(GV);
3016*0fca6ea1SDimitry Andric }
3017*0fca6ea1SDimitry Andric }
30188bcb0991SDimitry Andric
doInitialization(Module & M)30195ffd83dbSDimitry Andric bool PPCAIXAsmPrinter::doInitialization(Module &M) {
30205ffd83dbSDimitry Andric const bool Result = PPCAsmPrinter::doInitialization(M);
3021480093f4SDimitry Andric
30225ffd83dbSDimitry Andric auto setCsectAlignment = [this](const GlobalObject *GO) {
30235ffd83dbSDimitry Andric // Declarations have 0 alignment which is set by default.
30245ffd83dbSDimitry Andric if (GO->isDeclarationForLinker())
30255ffd83dbSDimitry Andric return;
3026480093f4SDimitry Andric
3027480093f4SDimitry Andric SectionKind GOKind = getObjFileLowering().getKindForGlobal(GO, TM);
30285ffd83dbSDimitry Andric MCSectionXCOFF *Csect = cast<MCSectionXCOFF>(
30295ffd83dbSDimitry Andric getObjFileLowering().SectionForGlobal(GO, GOKind, TM));
3030480093f4SDimitry Andric
3031*0fca6ea1SDimitry Andric Align GOAlign = getGVAlignment(GO, GO->getDataLayout());
3032bdd1243dSDimitry Andric Csect->ensureMinAlignment(GOAlign);
30335ffd83dbSDimitry Andric };
3034480093f4SDimitry Andric
3035*0fca6ea1SDimitry Andric // For all TLS variables, calculate their corresponding addresses and store
3036*0fca6ea1SDimitry Andric // them into TLSVarsToAddressMapping, which will be used to determine whether
3037*0fca6ea1SDimitry Andric // or not local-exec TLS variables require special assembly printing.
3038*0fca6ea1SDimitry Andric uint64_t TLSVarAddress = 0;
3039*0fca6ea1SDimitry Andric auto DL = M.getDataLayout();
3040*0fca6ea1SDimitry Andric for (const auto &G : M.globals()) {
3041*0fca6ea1SDimitry Andric if (G.isThreadLocal() && !G.isDeclaration()) {
3042*0fca6ea1SDimitry Andric TLSVarAddress = alignTo(TLSVarAddress, getGVAlignment(&G, DL));
3043*0fca6ea1SDimitry Andric TLSVarsToAddressMapping[&G] = TLSVarAddress;
3044*0fca6ea1SDimitry Andric TLSVarAddress += DL.getTypeAllocSize(G.getValueType());
3045*0fca6ea1SDimitry Andric }
3046*0fca6ea1SDimitry Andric }
3047*0fca6ea1SDimitry Andric
30485ffd83dbSDimitry Andric // We need to know, up front, the alignment of csects for the assembly path,
30495ffd83dbSDimitry Andric // because once a .csect directive gets emitted, we could not change the
30505ffd83dbSDimitry Andric // alignment value on it.
3051e8d8bef9SDimitry Andric for (const auto &G : M.globals()) {
3052e8d8bef9SDimitry Andric if (isSpecialLLVMGlobalArrayToSkip(&G))
3053e8d8bef9SDimitry Andric continue;
3054e8d8bef9SDimitry Andric
3055e8d8bef9SDimitry Andric if (isSpecialLLVMGlobalArrayForStaticInit(&G)) {
3056e8d8bef9SDimitry Andric // Generate a format indicator and a unique module id to be a part of
3057e8d8bef9SDimitry Andric // the sinit and sterm function names.
3058e8d8bef9SDimitry Andric if (FormatIndicatorAndUniqueModId.empty()) {
3059e8d8bef9SDimitry Andric std::string UniqueModuleId = getUniqueModuleId(&M);
3060e8d8bef9SDimitry Andric if (UniqueModuleId != "")
3061e8d8bef9SDimitry Andric // TODO: Use source file full path to generate the unique module id
3062e8d8bef9SDimitry Andric // and add a format indicator as a part of function name in case we
3063e8d8bef9SDimitry Andric // will support more than one format.
3064e8d8bef9SDimitry Andric FormatIndicatorAndUniqueModId = "clang_" + UniqueModuleId.substr(1);
30655f757f3fSDimitry Andric else {
30665f757f3fSDimitry Andric // Use threadId, Pid, and current time as the unique module id when we
30675f757f3fSDimitry Andric // cannot generate one based on a module's strong external symbols.
30685f757f3fSDimitry Andric auto CurTime =
30695f757f3fSDimitry Andric std::chrono::duration_cast<std::chrono::nanoseconds>(
30705f757f3fSDimitry Andric std::chrono::steady_clock::now().time_since_epoch())
30715f757f3fSDimitry Andric .count();
3072e8d8bef9SDimitry Andric FormatIndicatorAndUniqueModId =
30735f757f3fSDimitry Andric "clangPidTidTime_" + llvm::itostr(sys::Process::getProcessId()) +
30745f757f3fSDimitry Andric "_" + llvm::itostr(llvm::get_threadid()) + "_" +
30755f757f3fSDimitry Andric llvm::itostr(CurTime);
30765f757f3fSDimitry Andric }
3077e8d8bef9SDimitry Andric }
3078e8d8bef9SDimitry Andric
3079e8d8bef9SDimitry Andric emitSpecialLLVMGlobal(&G);
3080e8d8bef9SDimitry Andric continue;
3081e8d8bef9SDimitry Andric }
3082e8d8bef9SDimitry Andric
30835ffd83dbSDimitry Andric setCsectAlignment(&G);
3084*0fca6ea1SDimitry Andric std::optional<CodeModel::Model> OptionalCodeModel = G.getCodeModel();
3085*0fca6ea1SDimitry Andric if (OptionalCodeModel)
3086*0fca6ea1SDimitry Andric setOptionalCodeModel(cast<MCSymbolXCOFF>(getSymbol(&G)),
3087*0fca6ea1SDimitry Andric *OptionalCodeModel);
3088e8d8bef9SDimitry Andric }
30895ffd83dbSDimitry Andric
30905ffd83dbSDimitry Andric for (const auto &F : M)
30915ffd83dbSDimitry Andric setCsectAlignment(&F);
30925ffd83dbSDimitry Andric
3093e8d8bef9SDimitry Andric // Construct an aliasing list for each GlobalObject.
3094e8d8bef9SDimitry Andric for (const auto &Alias : M.aliases()) {
30955f757f3fSDimitry Andric const GlobalObject *Aliasee = Alias.getAliaseeObject();
30965f757f3fSDimitry Andric if (!Aliasee)
3097e8d8bef9SDimitry Andric report_fatal_error(
3098e8d8bef9SDimitry Andric "alias without a base object is not yet supported on AIX");
30995f757f3fSDimitry Andric
31005f757f3fSDimitry Andric if (Aliasee->hasCommonLinkage()) {
31015f757f3fSDimitry Andric report_fatal_error("Aliases to common variables are not allowed on AIX:"
31025f757f3fSDimitry Andric "\n\tAlias attribute for " +
31035f757f3fSDimitry Andric Alias.getGlobalIdentifier() +
31045f757f3fSDimitry Andric " is invalid because " + Aliasee->getName() +
31055f757f3fSDimitry Andric " is common.",
31065f757f3fSDimitry Andric false);
31075f757f3fSDimitry Andric }
31085f757f3fSDimitry Andric
3109*0fca6ea1SDimitry Andric const GlobalVariable *GVar =
3110*0fca6ea1SDimitry Andric dyn_cast_or_null<GlobalVariable>(Alias.getAliaseeObject());
3111*0fca6ea1SDimitry Andric if (GVar) {
3112*0fca6ea1SDimitry Andric std::optional<CodeModel::Model> OptionalCodeModel = GVar->getCodeModel();
3113*0fca6ea1SDimitry Andric if (OptionalCodeModel)
3114*0fca6ea1SDimitry Andric setOptionalCodeModel(cast<MCSymbolXCOFF>(getSymbol(&Alias)),
3115*0fca6ea1SDimitry Andric *OptionalCodeModel);
3116*0fca6ea1SDimitry Andric }
3117*0fca6ea1SDimitry Andric
31185f757f3fSDimitry Andric GOAliasMap[Aliasee].push_back(&Alias);
3119e8d8bef9SDimitry Andric }
3120e8d8bef9SDimitry Andric
31215ffd83dbSDimitry Andric return Result;
3122480093f4SDimitry Andric }
31238bcb0991SDimitry Andric
emitInstruction(const MachineInstr * MI)3124e8d8bef9SDimitry Andric void PPCAIXAsmPrinter::emitInstruction(const MachineInstr *MI) {
3125e8d8bef9SDimitry Andric switch (MI->getOpcode()) {
3126e8d8bef9SDimitry Andric default:
3127e8d8bef9SDimitry Andric break;
3128bdd1243dSDimitry Andric case PPC::TW:
3129bdd1243dSDimitry Andric case PPC::TWI:
3130bdd1243dSDimitry Andric case PPC::TD:
3131bdd1243dSDimitry Andric case PPC::TDI: {
3132bdd1243dSDimitry Andric if (MI->getNumOperands() < 5)
3133bdd1243dSDimitry Andric break;
3134bdd1243dSDimitry Andric const MachineOperand &LangMO = MI->getOperand(3);
3135bdd1243dSDimitry Andric const MachineOperand &ReasonMO = MI->getOperand(4);
3136bdd1243dSDimitry Andric if (!LangMO.isImm() || !ReasonMO.isImm())
3137bdd1243dSDimitry Andric break;
3138bdd1243dSDimitry Andric MCSymbol *TempSym = OutContext.createNamedTempSymbol();
3139bdd1243dSDimitry Andric OutStreamer->emitLabel(TempSym);
3140bdd1243dSDimitry Andric OutStreamer->emitXCOFFExceptDirective(CurrentFnSym, TempSym,
3141bdd1243dSDimitry Andric LangMO.getImm(), ReasonMO.getImm(),
3142bdd1243dSDimitry Andric Subtarget->isPPC64() ? MI->getMF()->getInstructionCount() * 8 :
3143bdd1243dSDimitry Andric MI->getMF()->getInstructionCount() * 4,
3144bdd1243dSDimitry Andric MMI->hasDebugInfo());
3145bdd1243dSDimitry Andric break;
3146bdd1243dSDimitry Andric }
3147*0fca6ea1SDimitry Andric case PPC::GETtlsMOD32AIX:
3148*0fca6ea1SDimitry Andric case PPC::GETtlsMOD64AIX:
314906c3fb27SDimitry Andric case PPC::GETtlsTpointer32AIX:
3150fe6060f1SDimitry Andric case PPC::GETtlsADDR64AIX:
3151fe6060f1SDimitry Andric case PPC::GETtlsADDR32AIX: {
3152*0fca6ea1SDimitry Andric // A reference to .__tls_get_mod/.__tls_get_addr/.__get_tpointer is unknown
3153*0fca6ea1SDimitry Andric // to the assembler so we need to emit an external symbol reference.
315406c3fb27SDimitry Andric MCSymbol *TlsGetAddr =
315506c3fb27SDimitry Andric createMCSymbolForTlsGetAddr(OutContext, MI->getOpcode());
3156fe6060f1SDimitry Andric ExtSymSDNodeSymbols.insert(TlsGetAddr);
3157fe6060f1SDimitry Andric break;
3158fe6060f1SDimitry Andric }
3159e8d8bef9SDimitry Andric case PPC::BL8:
3160e8d8bef9SDimitry Andric case PPC::BL:
3161e8d8bef9SDimitry Andric case PPC::BL8_NOP:
3162e8d8bef9SDimitry Andric case PPC::BL_NOP: {
3163e8d8bef9SDimitry Andric const MachineOperand &MO = MI->getOperand(0);
3164e8d8bef9SDimitry Andric if (MO.isSymbol()) {
3165e8d8bef9SDimitry Andric MCSymbolXCOFF *S =
3166e8d8bef9SDimitry Andric cast<MCSymbolXCOFF>(OutContext.getOrCreateSymbol(MO.getSymbolName()));
3167e8d8bef9SDimitry Andric ExtSymSDNodeSymbols.insert(S);
3168e8d8bef9SDimitry Andric }
3169e8d8bef9SDimitry Andric } break;
3170e8d8bef9SDimitry Andric case PPC::BL_TLS:
3171e8d8bef9SDimitry Andric case PPC::BL8_TLS:
3172e8d8bef9SDimitry Andric case PPC::BL8_TLS_:
3173e8d8bef9SDimitry Andric case PPC::BL8_NOP_TLS:
3174e8d8bef9SDimitry Andric report_fatal_error("TLS call not yet implemented");
3175e8d8bef9SDimitry Andric case PPC::TAILB:
3176e8d8bef9SDimitry Andric case PPC::TAILB8:
3177e8d8bef9SDimitry Andric case PPC::TAILBA:
3178e8d8bef9SDimitry Andric case PPC::TAILBA8:
3179e8d8bef9SDimitry Andric case PPC::TAILBCTR:
3180e8d8bef9SDimitry Andric case PPC::TAILBCTR8:
3181e8d8bef9SDimitry Andric if (MI->getOperand(0).isSymbol())
3182e8d8bef9SDimitry Andric report_fatal_error("Tail call for extern symbol not yet supported.");
3183e8d8bef9SDimitry Andric break;
3184fe6060f1SDimitry Andric case PPC::DST:
3185fe6060f1SDimitry Andric case PPC::DST64:
3186fe6060f1SDimitry Andric case PPC::DSTT:
3187fe6060f1SDimitry Andric case PPC::DSTT64:
3188fe6060f1SDimitry Andric case PPC::DSTST:
3189fe6060f1SDimitry Andric case PPC::DSTST64:
3190fe6060f1SDimitry Andric case PPC::DSTSTT:
3191fe6060f1SDimitry Andric case PPC::DSTSTT64:
3192fe6060f1SDimitry Andric EmitToStreamer(
3193fe6060f1SDimitry Andric *OutStreamer,
3194fe6060f1SDimitry Andric MCInstBuilder(PPC::ORI).addReg(PPC::R0).addReg(PPC::R0).addImm(0));
3195fe6060f1SDimitry Andric return;
3196e8d8bef9SDimitry Andric }
3197e8d8bef9SDimitry Andric return PPCAsmPrinter::emitInstruction(MI);
3198e8d8bef9SDimitry Andric }
3199e8d8bef9SDimitry Andric
doFinalization(Module & M)3200e8d8bef9SDimitry Andric bool PPCAIXAsmPrinter::doFinalization(Module &M) {
3201fe6060f1SDimitry Andric // Do streamer related finalization for DWARF.
3202fe6060f1SDimitry Andric if (!MAI->usesDwarfFileAndLocDirectives() && MMI->hasDebugInfo())
3203fe6060f1SDimitry Andric OutStreamer->doFinalizationAtSectionEnd(
3204fe6060f1SDimitry Andric OutStreamer->getContext().getObjectFileInfo()->getTextSection());
3205fe6060f1SDimitry Andric
3206e8d8bef9SDimitry Andric for (MCSymbol *Sym : ExtSymSDNodeSymbols)
3207e8d8bef9SDimitry Andric OutStreamer->emitSymbolAttribute(Sym, MCSA_Extern);
3208e8d8bef9SDimitry Andric return PPCAsmPrinter::doFinalization(M);
3209e8d8bef9SDimitry Andric }
3210e8d8bef9SDimitry Andric
mapToSinitPriority(int P)3211e8d8bef9SDimitry Andric static unsigned mapToSinitPriority(int P) {
3212e8d8bef9SDimitry Andric if (P < 0 || P > 65535)
3213e8d8bef9SDimitry Andric report_fatal_error("invalid init priority");
3214e8d8bef9SDimitry Andric
3215e8d8bef9SDimitry Andric if (P <= 20)
3216e8d8bef9SDimitry Andric return P;
3217e8d8bef9SDimitry Andric
3218e8d8bef9SDimitry Andric if (P < 81)
3219e8d8bef9SDimitry Andric return 20 + (P - 20) * 16;
3220e8d8bef9SDimitry Andric
3221e8d8bef9SDimitry Andric if (P <= 1124)
3222e8d8bef9SDimitry Andric return 1004 + (P - 81);
3223e8d8bef9SDimitry Andric
3224e8d8bef9SDimitry Andric if (P < 64512)
3225e8d8bef9SDimitry Andric return 2047 + (P - 1124) * 33878;
3226e8d8bef9SDimitry Andric
3227e8d8bef9SDimitry Andric return 2147482625u + (P - 64512);
3228e8d8bef9SDimitry Andric }
3229e8d8bef9SDimitry Andric
convertToSinitPriority(int Priority)3230e8d8bef9SDimitry Andric static std::string convertToSinitPriority(int Priority) {
3231e8d8bef9SDimitry Andric // This helper function converts clang init priority to values used in sinit
3232e8d8bef9SDimitry Andric // and sterm functions.
3233e8d8bef9SDimitry Andric //
3234e8d8bef9SDimitry Andric // The conversion strategies are:
3235e8d8bef9SDimitry Andric // We map the reserved clang/gnu priority range [0, 100] into the sinit/sterm
3236e8d8bef9SDimitry Andric // reserved priority range [0, 1023] by
3237e8d8bef9SDimitry Andric // - directly mapping the first 21 and the last 20 elements of the ranges
3238e8d8bef9SDimitry Andric // - linear interpolating the intermediate values with a step size of 16.
3239e8d8bef9SDimitry Andric //
3240e8d8bef9SDimitry Andric // We map the non reserved clang/gnu priority range of [101, 65535] into the
3241e8d8bef9SDimitry Andric // sinit/sterm priority range [1024, 2147483648] by:
3242e8d8bef9SDimitry Andric // - directly mapping the first and the last 1024 elements of the ranges
3243e8d8bef9SDimitry Andric // - linear interpolating the intermediate values with a step size of 33878.
3244e8d8bef9SDimitry Andric unsigned int P = mapToSinitPriority(Priority);
3245e8d8bef9SDimitry Andric
3246e8d8bef9SDimitry Andric std::string PrioritySuffix;
3247e8d8bef9SDimitry Andric llvm::raw_string_ostream os(PrioritySuffix);
3248e8d8bef9SDimitry Andric os << llvm::format_hex_no_prefix(P, 8);
3249e8d8bef9SDimitry Andric os.flush();
3250e8d8bef9SDimitry Andric return PrioritySuffix;
3251e8d8bef9SDimitry Andric }
3252e8d8bef9SDimitry Andric
emitXXStructorList(const DataLayout & DL,const Constant * List,bool IsCtor)3253e8d8bef9SDimitry Andric void PPCAIXAsmPrinter::emitXXStructorList(const DataLayout &DL,
3254e8d8bef9SDimitry Andric const Constant *List, bool IsCtor) {
3255e8d8bef9SDimitry Andric SmallVector<Structor, 8> Structors;
3256e8d8bef9SDimitry Andric preprocessXXStructorList(DL, List, Structors);
3257e8d8bef9SDimitry Andric if (Structors.empty())
3258e8d8bef9SDimitry Andric return;
3259e8d8bef9SDimitry Andric
3260e8d8bef9SDimitry Andric unsigned Index = 0;
3261e8d8bef9SDimitry Andric for (Structor &S : Structors) {
3262e8d8bef9SDimitry Andric if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(S.Func))
3263e8d8bef9SDimitry Andric S.Func = CE->getOperand(0);
3264e8d8bef9SDimitry Andric
3265e8d8bef9SDimitry Andric llvm::GlobalAlias::create(
3266e8d8bef9SDimitry Andric GlobalValue::ExternalLinkage,
3267e8d8bef9SDimitry Andric (IsCtor ? llvm::Twine("__sinit") : llvm::Twine("__sterm")) +
3268e8d8bef9SDimitry Andric llvm::Twine(convertToSinitPriority(S.Priority)) +
3269e8d8bef9SDimitry Andric llvm::Twine("_", FormatIndicatorAndUniqueModId) +
3270e8d8bef9SDimitry Andric llvm::Twine("_", llvm::utostr(Index++)),
3271e8d8bef9SDimitry Andric cast<Function>(S.Func));
3272e8d8bef9SDimitry Andric }
3273e8d8bef9SDimitry Andric }
3274e8d8bef9SDimitry Andric
emitTTypeReference(const GlobalValue * GV,unsigned Encoding)3275e8d8bef9SDimitry Andric void PPCAIXAsmPrinter::emitTTypeReference(const GlobalValue *GV,
3276e8d8bef9SDimitry Andric unsigned Encoding) {
3277e8d8bef9SDimitry Andric if (GV) {
327806c3fb27SDimitry Andric TOCEntryType GlobalType = TOCType_GlobalInternal;
327906c3fb27SDimitry Andric GlobalValue::LinkageTypes Linkage = GV->getLinkage();
328006c3fb27SDimitry Andric if (Linkage == GlobalValue::ExternalLinkage ||
328106c3fb27SDimitry Andric Linkage == GlobalValue::AvailableExternallyLinkage ||
328206c3fb27SDimitry Andric Linkage == GlobalValue::ExternalWeakLinkage)
328306c3fb27SDimitry Andric GlobalType = TOCType_GlobalExternal;
3284e8d8bef9SDimitry Andric MCSymbol *TypeInfoSym = TM.getSymbol(GV);
328506c3fb27SDimitry Andric MCSymbol *TOCEntry = lookUpOrCreateTOCEntry(TypeInfoSym, GlobalType);
3286e8d8bef9SDimitry Andric const MCSymbol *TOCBaseSym =
3287e8d8bef9SDimitry Andric cast<MCSectionXCOFF>(getObjFileLowering().getTOCBaseSection())
3288e8d8bef9SDimitry Andric ->getQualNameSymbol();
3289e8d8bef9SDimitry Andric auto &Ctx = OutStreamer->getContext();
3290e8d8bef9SDimitry Andric const MCExpr *Exp =
3291e8d8bef9SDimitry Andric MCBinaryExpr::createSub(MCSymbolRefExpr::create(TOCEntry, Ctx),
3292e8d8bef9SDimitry Andric MCSymbolRefExpr::create(TOCBaseSym, Ctx), Ctx);
3293e8d8bef9SDimitry Andric OutStreamer->emitValue(Exp, GetSizeOfEncodedValue(Encoding));
3294e8d8bef9SDimitry Andric } else
3295e8d8bef9SDimitry Andric OutStreamer->emitIntValue(0, GetSizeOfEncodedValue(Encoding));
3296e8d8bef9SDimitry Andric }
3297e8d8bef9SDimitry Andric
3298e8d8bef9SDimitry Andric // Return a pass that prints the PPC assembly code for a MachineFunction to the
3299e8d8bef9SDimitry Andric // given output stream.
33000b57cec5SDimitry Andric static AsmPrinter *
createPPCAsmPrinterPass(TargetMachine & tm,std::unique_ptr<MCStreamer> && Streamer)33010b57cec5SDimitry Andric createPPCAsmPrinterPass(TargetMachine &tm,
33020b57cec5SDimitry Andric std::unique_ptr<MCStreamer> &&Streamer) {
33030b57cec5SDimitry Andric if (tm.getTargetTriple().isOSAIX())
33040b57cec5SDimitry Andric return new PPCAIXAsmPrinter(tm, std::move(Streamer));
33050b57cec5SDimitry Andric
33060b57cec5SDimitry Andric return new PPCLinuxAsmPrinter(tm, std::move(Streamer));
33070b57cec5SDimitry Andric }
33080b57cec5SDimitry Andric
emitModuleCommandLines(Module & M)330906c3fb27SDimitry Andric void PPCAIXAsmPrinter::emitModuleCommandLines(Module &M) {
331006c3fb27SDimitry Andric const NamedMDNode *NMD = M.getNamedMetadata("llvm.commandline");
331106c3fb27SDimitry Andric if (!NMD || !NMD->getNumOperands())
331206c3fb27SDimitry Andric return;
331306c3fb27SDimitry Andric
331406c3fb27SDimitry Andric std::string S;
331506c3fb27SDimitry Andric raw_string_ostream RSOS(S);
331606c3fb27SDimitry Andric for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
331706c3fb27SDimitry Andric const MDNode *N = NMD->getOperand(i);
331806c3fb27SDimitry Andric assert(N->getNumOperands() == 1 &&
331906c3fb27SDimitry Andric "llvm.commandline metadata entry can have only one operand");
332006c3fb27SDimitry Andric const MDString *MDS = cast<MDString>(N->getOperand(0));
332106c3fb27SDimitry Andric // Add "@(#)" to support retrieving the command line information with the
332206c3fb27SDimitry Andric // AIX "what" command
332306c3fb27SDimitry Andric RSOS << "@(#)opt " << MDS->getString() << "\n";
332406c3fb27SDimitry Andric RSOS.write('\0');
332506c3fb27SDimitry Andric }
332606c3fb27SDimitry Andric OutStreamer->emitXCOFFCInfoSym(".GCC.command.line", RSOS.str());
332706c3fb27SDimitry Andric }
332806c3fb27SDimitry Andric
33290b57cec5SDimitry Andric // Force static initialization.
LLVMInitializePowerPCAsmPrinter()3330480093f4SDimitry Andric extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializePowerPCAsmPrinter() {
33310b57cec5SDimitry Andric TargetRegistry::RegisterAsmPrinter(getThePPC32Target(),
33320b57cec5SDimitry Andric createPPCAsmPrinterPass);
3333e8d8bef9SDimitry Andric TargetRegistry::RegisterAsmPrinter(getThePPC32LETarget(),
3334e8d8bef9SDimitry Andric createPPCAsmPrinterPass);
33350b57cec5SDimitry Andric TargetRegistry::RegisterAsmPrinter(getThePPC64Target(),
33360b57cec5SDimitry Andric createPPCAsmPrinterPass);
33370b57cec5SDimitry Andric TargetRegistry::RegisterAsmPrinter(getThePPC64LETarget(),
33380b57cec5SDimitry Andric createPPCAsmPrinterPass);
33390b57cec5SDimitry Andric }
3340