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 PPCAsmBackend : public MCAsmBackend {
61 const Target &TheTarget;
63 PPCAsmBackend(const Target &T) : MCAsmBackend(), TheTarget(T) {}
65 unsigned getNumFixupKinds() const { return PPC::NumTargetFixupKinds; }
67 const MCFixupKindInfo &getFixupKindInfo(MCFixupKind Kind) const {
68 const static MCFixupKindInfo Infos[PPC::NumTargetFixupKinds] = {
69 // name offset bits flags
70 { "fixup_ppc_br24", 6, 24, MCFixupKindInfo::FKF_IsPCRel },
71 { "fixup_ppc_brcond14", 16, 14, MCFixupKindInfo::FKF_IsPCRel },
72 { "fixup_ppc_lo16", 16, 16, 0 },
73 { "fixup_ppc_ha16", 16, 16, 0 },
74 { "fixup_ppc_lo14", 16, 14, 0 }
77 if (Kind < FirstTargetFixupKind)
78 return MCAsmBackend::getFixupKindInfo(Kind);
80 assert(unsigned(Kind - FirstTargetFixupKind) < getNumFixupKinds() &&
82 return Infos[Kind - FirstTargetFixupKind];
85 bool MayNeedRelaxation(const MCInst &Inst) const {
90 bool fixupNeedsRelaxation(const MCFixup &Fixup,
92 const MCInstFragment *DF,
93 const MCAsmLayout &Layout) const {
95 assert(0 && "RelaxInstruction() unimplemented");
100 void RelaxInstruction(const MCInst &Inst, MCInst &Res) const {
102 assert(0 && "RelaxInstruction() unimplemented");
105 bool WriteNopData(uint64_t Count, MCObjectWriter *OW) const {
106 // FIXME: Zero fill for now. That's not right, but at least will get the
107 // section size right.
108 for (uint64_t i = 0; i != Count; ++i)
113 unsigned getPointerSize() const {
114 StringRef Name = TheTarget.getName();
115 if (Name == "ppc64") return 8;
116 assert(Name == "ppc32" && "Unknown target name!");
120 } // end anonymous namespace
123 // FIXME: This should be in a separate file.
125 class DarwinPPCAsmBackend : public PPCAsmBackend {
127 DarwinPPCAsmBackend(const Target &T) : PPCAsmBackend(T) { }
129 void ApplyFixup(const MCFixup &Fixup, char *Data, unsigned DataSize,
130 uint64_t Value) const {
131 assert(0 && "UNIMP");
134 MCObjectWriter *createObjectWriter(raw_ostream &OS) const {
135 bool is64 = getPointerSize() == 8;
136 return createMachObjectWriter(new PPCMachObjectWriter(
138 (is64 ? object::mach::CTM_PowerPC64 :
139 object::mach::CTM_PowerPC),
140 object::mach::CSPPC_ALL),
141 OS, /*IsLittleEndian=*/false);
144 virtual bool doesSectionRequireSymbols(const MCSection &Section) const {
149 class ELFPPCAsmBackend : public PPCAsmBackend {
152 ELFPPCAsmBackend(const Target &T, uint8_t OSABI) :
153 PPCAsmBackend(T), OSABI(OSABI) { }
155 void ApplyFixup(const MCFixup &Fixup, char *Data, unsigned DataSize,
156 uint64_t Value) const {
157 Value = adjustFixupValue(Fixup.getKind(), Value);
158 if (!Value) return; // Doesn't change encoding.
160 unsigned Offset = Fixup.getOffset();
162 // For each byte of the fragment that the fixup touches, mask in the bits from
163 // the fixup value. The Value has been "split up" into the appropriate
165 for (unsigned i = 0; i != 4; ++i)
166 Data[Offset + i] |= uint8_t((Value >> ((4 - i - 1)*8)) & 0xff);
169 MCObjectWriter *createObjectWriter(raw_ostream &OS) const {
170 bool is64 = getPointerSize() == 8;
171 return createPPCELFObjectWriter(OS, is64, OSABI);
174 virtual bool doesSectionRequireSymbols(const MCSection &Section) const {
179 } // end anonymous namespace
184 MCAsmBackend *llvm::createPPCAsmBackend(const Target &T, StringRef TT) {
185 if (Triple(TT).isOSDarwin())
186 return new DarwinPPCAsmBackend(T);
188 uint8_t OSABI = MCELFObjectTargetWriter::getOSABI(Triple(TT).getOS());
189 return new ELFPPCAsmBackend(T, OSABI);