1 //===-- PPCAsmBackend.cpp - PPC Assembler Backend -------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "llvm/Target/TargetAsmBackend.h"
11 #include "PPC.h"
12 #include "PPCFixupKinds.h"
13 #include "llvm/MC/MCMachObjectWriter.h"
14 #include "llvm/MC/MCSectionMachO.h"
15 #include "llvm/MC/MCObjectWriter.h"
16 #include "llvm/MC/MCValue.h"
17 #include "llvm/Object/MachOFormat.h"
18 #include "llvm/Target/TargetRegistry.h"
19 using namespace llvm;
20
21 namespace {
22 class PPCMachObjectWriter : public MCMachObjectTargetWriter {
23 public:
PPCMachObjectWriter(bool Is64Bit,uint32_t CPUType,uint32_t CPUSubtype)24 PPCMachObjectWriter(bool Is64Bit, uint32_t CPUType,
25 uint32_t CPUSubtype)
26 : MCMachObjectTargetWriter(Is64Bit, CPUType, CPUSubtype) {}
27
RecordRelocation(MachObjectWriter * Writer,const MCAssembler & Asm,const MCAsmLayout & Layout,const MCFragment * Fragment,const MCFixup & Fixup,MCValue Target,uint64_t & FixedValue)28 void RecordRelocation(MachObjectWriter *Writer,
29 const MCAssembler &Asm, const MCAsmLayout &Layout,
30 const MCFragment *Fragment, const MCFixup &Fixup,
31 MCValue Target, uint64_t &FixedValue) {}
32 };
33
34 class PPCAsmBackend : public TargetAsmBackend {
35 const Target &TheTarget;
36 public:
PPCAsmBackend(const Target & T)37 PPCAsmBackend(const Target &T) : TargetAsmBackend(), TheTarget(T) {}
38
getNumFixupKinds() const39 unsigned getNumFixupKinds() const { return PPC::NumTargetFixupKinds; }
40
getFixupKindInfo(MCFixupKind Kind) const41 const MCFixupKindInfo &getFixupKindInfo(MCFixupKind Kind) const {
42 const static MCFixupKindInfo Infos[PPC::NumTargetFixupKinds] = {
43 // name offset bits flags
44 { "fixup_ppc_br24", 6, 24, MCFixupKindInfo::FKF_IsPCRel },
45 { "fixup_ppc_brcond14", 16, 14, MCFixupKindInfo::FKF_IsPCRel },
46 { "fixup_ppc_lo16", 16, 16, 0 },
47 { "fixup_ppc_ha16", 16, 16, 0 },
48 { "fixup_ppc_lo14", 16, 14, 0 }
49 };
50
51 if (Kind < FirstTargetFixupKind)
52 return TargetAsmBackend::getFixupKindInfo(Kind);
53
54 assert(unsigned(Kind - FirstTargetFixupKind) < getNumFixupKinds() &&
55 "Invalid kind!");
56 return Infos[Kind - FirstTargetFixupKind];
57 }
58
MayNeedRelaxation(const MCInst & Inst) const59 bool MayNeedRelaxation(const MCInst &Inst) const {
60 // FIXME.
61 return false;
62 }
63
RelaxInstruction(const MCInst & Inst,MCInst & Res) const64 void RelaxInstruction(const MCInst &Inst, MCInst &Res) const {
65 // FIXME.
66 assert(0 && "RelaxInstruction() unimplemented");
67 }
68
WriteNopData(uint64_t Count,MCObjectWriter * OW) const69 bool WriteNopData(uint64_t Count, MCObjectWriter *OW) const {
70 // FIXME: Zero fill for now. That's not right, but at least will get the
71 // section size right.
72 for (uint64_t i = 0; i != Count; ++i)
73 OW->Write8(0);
74 return true;
75 }
76
getPointerSize() const77 unsigned getPointerSize() const {
78 StringRef Name = TheTarget.getName();
79 if (Name == "ppc64") return 8;
80 assert(Name == "ppc32" && "Unknown target name!");
81 return 4;
82 }
83 };
84 } // end anonymous namespace
85
86
87 // FIXME: This should be in a separate file.
88 namespace {
89 class DarwinPPCAsmBackend : public PPCAsmBackend {
90 public:
DarwinPPCAsmBackend(const Target & T)91 DarwinPPCAsmBackend(const Target &T) : PPCAsmBackend(T) { }
92
ApplyFixup(const MCFixup & Fixup,char * Data,unsigned DataSize,uint64_t Value) const93 void ApplyFixup(const MCFixup &Fixup, char *Data, unsigned DataSize,
94 uint64_t Value) const {
95 assert(0 && "UNIMP");
96 }
97
createObjectWriter(raw_ostream & OS) const98 MCObjectWriter *createObjectWriter(raw_ostream &OS) const {
99 bool is64 = getPointerSize() == 8;
100 return createMachObjectWriter(new PPCMachObjectWriter(
101 /*Is64Bit=*/is64,
102 (is64 ? object::mach::CTM_PowerPC64 :
103 object::mach::CTM_PowerPC),
104 object::mach::CSPPC_ALL),
105 OS, /*IsLittleEndian=*/false);
106 }
107
doesSectionRequireSymbols(const MCSection & Section) const108 virtual bool doesSectionRequireSymbols(const MCSection &Section) const {
109 return false;
110 }
111 };
112 } // end anonymous namespace
113
114
115
116
createPPCAsmBackend(const Target & T,const std::string & TT)117 TargetAsmBackend *llvm::createPPCAsmBackend(const Target &T,
118 const std::string &TT) {
119 if (Triple(TT).isOSDarwin())
120 return new DarwinPPCAsmBackend(T);
121
122 return 0;
123 }
124