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 "llvm/MC/MCAsmBackend.h"
11 #include "MCTargetDesc/PPCMCTargetDesc.h"
12 #include "MCTargetDesc/PPCFixupKinds.h"
13 #include "llvm/MC/MCELFObjectWriter.h"
14 #include "llvm/MC/MCMachObjectWriter.h"
15 #include "llvm/MC/MCSectionMachO.h"
16 #include "llvm/MC/MCObjectWriter.h"
17 #include "llvm/MC/MCValue.h"
18 #include "llvm/Object/MachOFormat.h"
19 #include "llvm/Support/ELF.h"
20 #include "llvm/Support/ErrorHandling.h"
21 #include "llvm/Support/TargetRegistry.h"
24 static unsigned adjustFixupValue(unsigned Kind, uint64_t Value) {
27 llvm_unreachable("Unknown fixup kind!");
32 case PPC::fixup_ppc_brcond14:
33 return Value & 0x3ffc;
34 case PPC::fixup_ppc_br24:
35 return Value & 0x3fffffc;
37 case PPC::fixup_ppc_hi16:
38 return (Value >> 16) & 0xffff;
40 case PPC::fixup_ppc_ha16:
41 return ((Value >> 16) + ((Value & 0x8000) ? 1 : 0)) & 0xffff;
42 case PPC::fixup_ppc_lo16:
43 return Value & 0xffff;
48 class PPCMachObjectWriter : public MCMachObjectTargetWriter {
50 PPCMachObjectWriter(bool Is64Bit, uint32_t CPUType,
52 : MCMachObjectTargetWriter(Is64Bit, CPUType, CPUSubtype) {}
54 void RecordRelocation(MachObjectWriter *Writer,
55 const MCAssembler &Asm, const MCAsmLayout &Layout,
56 const MCFragment *Fragment, const MCFixup &Fixup,
57 MCValue Target, uint64_t &FixedValue) {}
60 class PPCELFObjectWriter : public MCELFObjectTargetWriter {
62 PPCELFObjectWriter(bool Is64Bit, Triple::OSType OSType, uint16_t EMachine,
63 bool HasRelocationAddend, bool isLittleEndian)
64 : MCELFObjectTargetWriter(Is64Bit, OSType, EMachine, HasRelocationAddend) {}
67 class PPCAsmBackend : public MCAsmBackend {
68 const Target &TheTarget;
70 PPCAsmBackend(const Target &T) : MCAsmBackend(), TheTarget(T) {}
72 unsigned getNumFixupKinds() const { return PPC::NumTargetFixupKinds; }
74 const MCFixupKindInfo &getFixupKindInfo(MCFixupKind Kind) const {
75 const static MCFixupKindInfo Infos[PPC::NumTargetFixupKinds] = {
76 // name offset bits flags
77 { "fixup_ppc_br24", 6, 24, MCFixupKindInfo::FKF_IsPCRel },
78 { "fixup_ppc_brcond14", 16, 14, MCFixupKindInfo::FKF_IsPCRel },
79 { "fixup_ppc_lo16", 16, 16, 0 },
80 { "fixup_ppc_ha16", 16, 16, 0 },
81 { "fixup_ppc_lo14", 16, 14, 0 }
84 if (Kind < FirstTargetFixupKind)
85 return MCAsmBackend::getFixupKindInfo(Kind);
87 assert(unsigned(Kind - FirstTargetFixupKind) < getNumFixupKinds() &&
89 return Infos[Kind - FirstTargetFixupKind];
92 bool MayNeedRelaxation(const MCInst &Inst) const {
97 bool fixupNeedsRelaxation(const MCFixup &Fixup,
99 const MCInstFragment *DF,
100 const MCAsmLayout &Layout) const {
102 assert(0 && "RelaxInstruction() unimplemented");
106 void RelaxInstruction(const MCInst &Inst, MCInst &Res) const {
108 assert(0 && "RelaxInstruction() unimplemented");
111 bool WriteNopData(uint64_t Count, MCObjectWriter *OW) const {
112 // FIXME: Zero fill for now. That's not right, but at least will get the
113 // section size right.
114 for (uint64_t i = 0; i != Count; ++i)
119 unsigned getPointerSize() const {
120 StringRef Name = TheTarget.getName();
121 if (Name == "ppc64") return 8;
122 assert(Name == "ppc32" && "Unknown target name!");
126 } // end anonymous namespace
129 // FIXME: This should be in a separate file.
131 class DarwinPPCAsmBackend : public PPCAsmBackend {
133 DarwinPPCAsmBackend(const Target &T) : PPCAsmBackend(T) { }
135 void ApplyFixup(const MCFixup &Fixup, char *Data, unsigned DataSize,
136 uint64_t Value) const {
137 assert(0 && "UNIMP");
140 MCObjectWriter *createObjectWriter(raw_ostream &OS) const {
141 bool is64 = getPointerSize() == 8;
142 return createMachObjectWriter(new PPCMachObjectWriter(
144 (is64 ? object::mach::CTM_PowerPC64 :
145 object::mach::CTM_PowerPC),
146 object::mach::CSPPC_ALL),
147 OS, /*IsLittleEndian=*/false);
150 virtual bool doesSectionRequireSymbols(const MCSection &Section) const {
155 class ELFPPCAsmBackend : public PPCAsmBackend {
156 Triple::OSType OSType;
158 ELFPPCAsmBackend(const Target &T, Triple::OSType OSType) :
159 PPCAsmBackend(T), OSType(OSType) { }
161 void ApplyFixup(const MCFixup &Fixup, char *Data, unsigned DataSize,
162 uint64_t Value) const {
163 Value = adjustFixupValue(Fixup.getKind(), Value);
164 if (!Value) return; // Doesn't change encoding.
166 unsigned Offset = Fixup.getOffset();
168 // For each byte of the fragment that the fixup touches, mask in the bits from
169 // the fixup value. The Value has been "split up" into the appropriate
171 for (unsigned i = 0; i != 4; ++i)
172 Data[Offset + i] |= uint8_t((Value >> ((4 - i - 1)*8)) & 0xff);
175 MCObjectWriter *createObjectWriter(raw_ostream &OS) const {
176 bool is64 = getPointerSize() == 8;
177 return createELFObjectWriter(new PPCELFObjectWriter(
180 is64 ? ELF::EM_PPC64 : ELF::EM_PPC,
181 /*addend*/ true, /*isLittleEndian*/ false),
182 OS, /*IsLittleEndian=*/false);
185 virtual bool doesSectionRequireSymbols(const MCSection &Section) const {
190 } // end anonymous namespace
195 MCAsmBackend *llvm::createPPCAsmBackend(const Target &T, StringRef TT) {
196 if (Triple(TT).isOSDarwin())
197 return new DarwinPPCAsmBackend(T);
199 return new ELFPPCAsmBackend(T, Triple(TT).getOS());