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