1 //===-- PPCMCTargetDesc.cpp - PowerPC Target Descriptions -----------------===//
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 provides PowerPC specific target descriptions.
11 //===----------------------------------------------------------------------===//
13 #include "MCTargetDesc/PPCMCTargetDesc.h"
14 #include "MCTargetDesc/PPCInstPrinter.h"
15 #include "MCTargetDesc/PPCMCAsmInfo.h"
16 #include "PPCTargetStreamer.h"
17 #include "TargetInfo/PowerPCTargetInfo.h"
18 #include "llvm/ADT/SmallPtrSet.h"
19 #include "llvm/ADT/StringRef.h"
20 #include "llvm/ADT/Triple.h"
21 #include "llvm/BinaryFormat/ELF.h"
22 #include "llvm/MC/MCAssembler.h"
23 #include "llvm/MC/MCContext.h"
24 #include "llvm/MC/MCDwarf.h"
25 #include "llvm/MC/MCELFStreamer.h"
26 #include "llvm/MC/MCExpr.h"
27 #include "llvm/MC/MCInstrInfo.h"
28 #include "llvm/MC/MCRegisterInfo.h"
29 #include "llvm/MC/MCStreamer.h"
30 #include "llvm/MC/MCSubtargetInfo.h"
31 #include "llvm/MC/MCSymbol.h"
32 #include "llvm/MC/MCSymbolELF.h"
33 #include "llvm/Support/Casting.h"
34 #include "llvm/Support/CodeGen.h"
35 #include "llvm/Support/ErrorHandling.h"
36 #include "llvm/Support/FormattedStream.h"
37 #include "llvm/Support/TargetRegistry.h"
38 #include "llvm/Support/raw_ostream.h"
42 #define GET_INSTRINFO_MC_DESC
43 #include "PPCGenInstrInfo.inc"
45 #define GET_SUBTARGETINFO_MC_DESC
46 #include "PPCGenSubtargetInfo.inc"
48 #define GET_REGINFO_MC_DESC
49 #include "PPCGenRegisterInfo.inc"
51 PPCTargetStreamer::PPCTargetStreamer(MCStreamer
&S
) : MCTargetStreamer(S
) {}
53 // Pin the vtable to this file.
54 PPCTargetStreamer::~PPCTargetStreamer() = default;
56 static MCInstrInfo
*createPPCMCInstrInfo() {
57 MCInstrInfo
*X
= new MCInstrInfo();
58 InitPPCMCInstrInfo(X
);
62 static MCRegisterInfo
*createPPCMCRegisterInfo(const Triple
&TT
) {
64 (TT
.getArch() == Triple::ppc64
|| TT
.getArch() == Triple::ppc64le
);
65 unsigned Flavour
= isPPC64
? 0 : 1;
66 unsigned RA
= isPPC64
? PPC::LR8
: PPC::LR
;
68 MCRegisterInfo
*X
= new MCRegisterInfo();
69 InitPPCMCRegisterInfo(X
, RA
, Flavour
, Flavour
);
73 static MCSubtargetInfo
*createPPCMCSubtargetInfo(const Triple
&TT
,
74 StringRef CPU
, StringRef FS
) {
75 return createPPCMCSubtargetInfoImpl(TT
, CPU
, FS
);
78 static MCAsmInfo
*createPPCMCAsmInfo(const MCRegisterInfo
&MRI
,
79 const Triple
&TheTriple
) {
80 bool isPPC64
= (TheTriple
.getArch() == Triple::ppc64
||
81 TheTriple
.getArch() == Triple::ppc64le
);
84 if (TheTriple
.isOSDarwin())
85 MAI
= new PPCMCAsmInfoDarwin(isPPC64
, TheTriple
);
86 else if (TheTriple
.isOSBinFormatXCOFF())
87 MAI
= new PPCXCOFFMCAsmInfo(isPPC64
, TheTriple
);
89 MAI
= new PPCELFMCAsmInfo(isPPC64
, TheTriple
);
91 // Initial state of the frame pointer is R1.
92 unsigned Reg
= isPPC64
? PPC::X1
: PPC::R1
;
93 MCCFIInstruction Inst
=
94 MCCFIInstruction::createDefCfa(nullptr, MRI
.getDwarfRegNum(Reg
, true), 0);
95 MAI
->addInitialFrameState(Inst
);
102 class PPCTargetAsmStreamer
: public PPCTargetStreamer
{
103 formatted_raw_ostream
&OS
;
106 PPCTargetAsmStreamer(MCStreamer
&S
, formatted_raw_ostream
&OS
)
107 : PPCTargetStreamer(S
), OS(OS
) {}
109 void emitTCEntry(const MCSymbol
&S
) override
{
117 void emitMachine(StringRef CPU
) override
{
118 OS
<< "\t.machine " << CPU
<< '\n';
121 void emitAbiVersion(int AbiVersion
) override
{
122 OS
<< "\t.abiversion " << AbiVersion
<< '\n';
125 void emitLocalEntry(MCSymbolELF
*S
, const MCExpr
*LocalOffset
) override
{
126 const MCAsmInfo
*MAI
= Streamer
.getContext().getAsmInfo();
128 OS
<< "\t.localentry\t";
131 LocalOffset
->print(OS
, MAI
);
136 class PPCTargetELFStreamer
: public PPCTargetStreamer
{
138 PPCTargetELFStreamer(MCStreamer
&S
) : PPCTargetStreamer(S
) {}
140 MCELFStreamer
&getStreamer() {
141 return static_cast<MCELFStreamer
&>(Streamer
);
144 void emitTCEntry(const MCSymbol
&S
) override
{
145 // Creates a R_PPC64_TOC relocation
146 Streamer
.EmitValueToAlignment(8);
147 Streamer
.EmitSymbolValue(&S
, 8);
150 void emitMachine(StringRef CPU
) override
{
151 // FIXME: Is there anything to do in here or does this directive only
155 void emitAbiVersion(int AbiVersion
) override
{
156 MCAssembler
&MCA
= getStreamer().getAssembler();
157 unsigned Flags
= MCA
.getELFHeaderEFlags();
158 Flags
&= ~ELF::EF_PPC64_ABI
;
159 Flags
|= (AbiVersion
& ELF::EF_PPC64_ABI
);
160 MCA
.setELFHeaderEFlags(Flags
);
163 void emitLocalEntry(MCSymbolELF
*S
, const MCExpr
*LocalOffset
) override
{
164 MCAssembler
&MCA
= getStreamer().getAssembler();
167 if (!LocalOffset
->evaluateAsAbsolute(Res
, MCA
))
168 report_fatal_error(".localentry expression must be absolute.");
170 unsigned Encoded
= ELF::encodePPC64LocalEntryOffset(Res
);
171 if (Res
!= ELF::decodePPC64LocalEntryOffset(Encoded
))
172 report_fatal_error(".localentry expression cannot be encoded.");
174 unsigned Other
= S
->getOther();
175 Other
&= ~ELF::STO_PPC64_LOCAL_MASK
;
179 // For GAS compatibility, unless we already saw a .abiversion directive,
180 // set e_flags to indicate ELFv2 ABI.
181 unsigned Flags
= MCA
.getELFHeaderEFlags();
182 if ((Flags
& ELF::EF_PPC64_ABI
) == 0)
183 MCA
.setELFHeaderEFlags(Flags
| 2);
186 void emitAssignment(MCSymbol
*S
, const MCExpr
*Value
) override
{
187 auto *Symbol
= cast
<MCSymbolELF
>(S
);
189 // When encoding an assignment to set symbol A to symbol B, also copy
190 // the st_other bits encoding the local entry point offset.
191 if (copyLocalEntry(Symbol
, Value
))
192 UpdateOther
.insert(Symbol
);
194 UpdateOther
.erase(Symbol
);
197 void finish() override
{
198 for (auto *Sym
: UpdateOther
)
199 copyLocalEntry(Sym
, Sym
->getVariableValue());
203 SmallPtrSet
<MCSymbolELF
*, 32> UpdateOther
;
205 bool copyLocalEntry(MCSymbolELF
*D
, const MCExpr
*S
) {
206 auto *Ref
= dyn_cast
<const MCSymbolRefExpr
>(S
);
209 const auto &RhsSym
= cast
<MCSymbolELF
>(Ref
->getSymbol());
210 unsigned Other
= D
->getOther();
211 Other
&= ~ELF::STO_PPC64_LOCAL_MASK
;
212 Other
|= RhsSym
.getOther() & ELF::STO_PPC64_LOCAL_MASK
;
218 class PPCTargetMachOStreamer
: public PPCTargetStreamer
{
220 PPCTargetMachOStreamer(MCStreamer
&S
) : PPCTargetStreamer(S
) {}
222 void emitTCEntry(const MCSymbol
&S
) override
{
223 llvm_unreachable("Unknown pseudo-op: .tc");
226 void emitMachine(StringRef CPU
) override
{
227 // FIXME: We should update the CPUType, CPUSubType in the Object file if
228 // the new values are different from the defaults.
231 void emitAbiVersion(int AbiVersion
) override
{
232 llvm_unreachable("Unknown pseudo-op: .abiversion");
235 void emitLocalEntry(MCSymbolELF
*S
, const MCExpr
*LocalOffset
) override
{
236 llvm_unreachable("Unknown pseudo-op: .localentry");
240 class PPCTargetXCOFFStreamer
: public PPCTargetStreamer
{
242 PPCTargetXCOFFStreamer(MCStreamer
&S
) : PPCTargetStreamer(S
) {}
244 void emitTCEntry(const MCSymbol
&S
) override
{
245 report_fatal_error("TOC entries not supported yet.");
248 void emitMachine(StringRef CPU
) override
{
249 llvm_unreachable("Machine pseudo-ops are invalid for XCOFF.");
252 void emitAbiVersion(int AbiVersion
) override
{
253 llvm_unreachable("ABI-version pseudo-ops are invalid for XCOFF.");
256 void emitLocalEntry(MCSymbolELF
*S
, const MCExpr
*LocalOffset
) override
{
257 llvm_unreachable("Local-entry pseudo-ops are invalid for XCOFF.");
261 } // end anonymous namespace
263 static MCTargetStreamer
*createAsmTargetStreamer(MCStreamer
&S
,
264 formatted_raw_ostream
&OS
,
265 MCInstPrinter
*InstPrint
,
267 return new PPCTargetAsmStreamer(S
, OS
);
270 static MCTargetStreamer
*
271 createObjectTargetStreamer(MCStreamer
&S
, const MCSubtargetInfo
&STI
) {
272 const Triple
&TT
= STI
.getTargetTriple();
273 if (TT
.isOSBinFormatELF())
274 return new PPCTargetELFStreamer(S
);
275 if (TT
.isOSBinFormatXCOFF())
276 return new PPCTargetXCOFFStreamer(S
);
277 return new PPCTargetMachOStreamer(S
);
280 static MCInstPrinter
*createPPCMCInstPrinter(const Triple
&T
,
281 unsigned SyntaxVariant
,
282 const MCAsmInfo
&MAI
,
283 const MCInstrInfo
&MII
,
284 const MCRegisterInfo
&MRI
) {
285 return new PPCInstPrinter(MAI
, MII
, MRI
, T
);
288 extern "C" void LLVMInitializePowerPCTargetMC() {
290 {&getThePPC32Target(), &getThePPC64Target(), &getThePPC64LETarget()}) {
291 // Register the MC asm info.
292 RegisterMCAsmInfoFn
C(*T
, createPPCMCAsmInfo
);
294 // Register the MC instruction info.
295 TargetRegistry::RegisterMCInstrInfo(*T
, createPPCMCInstrInfo
);
297 // Register the MC register info.
298 TargetRegistry::RegisterMCRegInfo(*T
, createPPCMCRegisterInfo
);
300 // Register the MC subtarget info.
301 TargetRegistry::RegisterMCSubtargetInfo(*T
, createPPCMCSubtargetInfo
);
303 // Register the MC Code Emitter
304 TargetRegistry::RegisterMCCodeEmitter(*T
, createPPCMCCodeEmitter
);
306 // Register the asm backend.
307 TargetRegistry::RegisterMCAsmBackend(*T
, createPPCAsmBackend
);
309 // Register the object target streamer.
310 TargetRegistry::RegisterObjectTargetStreamer(*T
,
311 createObjectTargetStreamer
);
313 // Register the asm target streamer.
314 TargetRegistry::RegisterAsmTargetStreamer(*T
, createAsmTargetStreamer
);
316 // Register the MCInstPrinter.
317 TargetRegistry::RegisterMCInstPrinter(*T
, createPPCMCInstPrinter
);