1 //===-- PPCAsmBackend.cpp - PPC Assembler Backend -------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "MCTargetDesc/PPCMCTargetDesc.h"
11 #include "MCTargetDesc/PPCFixupKinds.h"
12 #include "llvm/MC/MCAsmBackend.h"
13 #include "llvm/MC/MCELFObjectWriter.h"
14 #include "llvm/MC/MCFixupKindInfo.h"
15 #include "llvm/MC/MCMachObjectWriter.h"
16 #include "llvm/MC/MCObjectWriter.h"
17 #include "llvm/MC/MCSectionMachO.h"
18 #include "llvm/MC/MCValue.h"
19 #include "llvm/Support/ELF.h"
20 #include "llvm/Support/ErrorHandling.h"
21 #include "llvm/Support/MachO.h"
22 #include "llvm/Support/TargetRegistry.h"
25 static uint64_t adjustFixupValue(unsigned Kind, uint64_t Value) {
28 llvm_unreachable("Unknown fixup kind!");
33 case PPC::fixup_ppc_nofixup:
35 case PPC::fixup_ppc_brcond14:
36 case PPC::fixup_ppc_brcond14abs:
37 return Value & 0xfffc;
38 case PPC::fixup_ppc_br24:
39 case PPC::fixup_ppc_br24abs:
40 return Value & 0x3fffffc;
41 case PPC::fixup_ppc_half16:
42 return Value & 0xffff;
43 case PPC::fixup_ppc_half16ds:
44 return Value & 0xfffc;
48 static unsigned getFixupKindNumBytes(unsigned Kind) {
51 llvm_unreachable("Unknown fixup kind!");
55 case PPC::fixup_ppc_half16:
56 case PPC::fixup_ppc_half16ds:
59 case PPC::fixup_ppc_brcond14:
60 case PPC::fixup_ppc_brcond14abs:
61 case PPC::fixup_ppc_br24:
62 case PPC::fixup_ppc_br24abs:
66 case PPC::fixup_ppc_nofixup:
73 class PPCAsmBackend : public MCAsmBackend {
74 const Target &TheTarget;
76 PPCAsmBackend(const Target &T) : MCAsmBackend(), TheTarget(T) {}
78 unsigned getNumFixupKinds() const { return PPC::NumTargetFixupKinds; }
80 const MCFixupKindInfo &getFixupKindInfo(MCFixupKind Kind) const {
81 const static MCFixupKindInfo Infos[PPC::NumTargetFixupKinds] = {
82 // name offset bits flags
83 { "fixup_ppc_br24", 6, 24, MCFixupKindInfo::FKF_IsPCRel },
84 { "fixup_ppc_brcond14", 16, 14, MCFixupKindInfo::FKF_IsPCRel },
85 { "fixup_ppc_br24abs", 6, 24, 0 },
86 { "fixup_ppc_brcond14abs", 16, 14, 0 },
87 { "fixup_ppc_half16", 0, 16, 0 },
88 { "fixup_ppc_half16ds", 0, 14, 0 },
89 { "fixup_ppc_nofixup", 0, 0, 0 }
92 if (Kind < FirstTargetFixupKind)
93 return MCAsmBackend::getFixupKindInfo(Kind);
95 assert(unsigned(Kind - FirstTargetFixupKind) < getNumFixupKinds() &&
97 return Infos[Kind - FirstTargetFixupKind];
100 void applyFixup(const MCFixup &Fixup, char *Data, unsigned DataSize,
101 uint64_t Value) const {
102 Value = adjustFixupValue(Fixup.getKind(), Value);
103 if (!Value) return; // Doesn't change encoding.
105 unsigned Offset = Fixup.getOffset();
106 unsigned NumBytes = getFixupKindNumBytes(Fixup.getKind());
108 // For each byte of the fragment that the fixup touches, mask in the bits
109 // from the fixup value. The Value has been "split up" into the appropriate
111 for (unsigned i = 0; i != NumBytes; ++i)
112 Data[Offset + i] |= uint8_t((Value >> ((NumBytes - i - 1)*8)) & 0xff);
115 bool mayNeedRelaxation(const MCInst &Inst) const {
120 bool fixupNeedsRelaxation(const MCFixup &Fixup,
122 const MCRelaxableFragment *DF,
123 const MCAsmLayout &Layout) const {
125 llvm_unreachable("relaxInstruction() unimplemented");
129 void relaxInstruction(const MCInst &Inst, MCInst &Res) const {
131 llvm_unreachable("relaxInstruction() unimplemented");
134 bool writeNopData(uint64_t Count, MCObjectWriter *OW) const {
135 uint64_t NumNops = Count / 4;
136 for (uint64_t i = 0; i != NumNops; ++i)
137 OW->Write32(0x60000000);
140 default: break; // No leftover bytes to write
141 case 1: OW->Write8(0); break;
142 case 2: OW->Write16(0); break;
143 case 3: OW->Write16(0); OW->Write8(0); break;
149 unsigned getPointerSize() const {
150 StringRef Name = TheTarget.getName();
151 if (Name == "ppc64" || Name == "ppc64le") return 8;
152 assert(Name == "ppc32" && "Unknown target name!");
156 } // end anonymous namespace
159 // FIXME: This should be in a separate file.
161 class DarwinPPCAsmBackend : public PPCAsmBackend {
163 DarwinPPCAsmBackend(const Target &T) : PPCAsmBackend(T) { }
165 MCObjectWriter *createObjectWriter(raw_ostream &OS) const {
166 bool is64 = getPointerSize() == 8;
167 return createPPCMachObjectWriter(
170 (is64 ? MachO::CPU_TYPE_POWERPC64 : MachO::CPU_TYPE_POWERPC),
171 MachO::CPU_SUBTYPE_POWERPC_ALL);
175 class ELFPPCAsmBackend : public PPCAsmBackend {
178 ELFPPCAsmBackend(const Target &T, uint8_t OSABI) :
179 PPCAsmBackend(T), OSABI(OSABI) { }
182 MCObjectWriter *createObjectWriter(raw_ostream &OS) const {
183 bool is64 = getPointerSize() == 8;
184 return createPPCELFObjectWriter(OS, is64, OSABI);
188 } // end anonymous namespace
190 MCAsmBackend *llvm::createPPCAsmBackend(const Target &T,
191 const MCRegisterInfo &MRI,
192 StringRef TT, StringRef CPU) {
193 if (Triple(TT).isOSDarwin())
194 return new DarwinPPCAsmBackend(T);
196 uint8_t OSABI = MCELFObjectTargetWriter::getOSABI(Triple(TT).getOS());
197 return new ELFPPCAsmBackend(T, OSABI);