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/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" 22 using namespace llvm; 23 24 static unsigned adjustFixupValue(unsigned Kind, uint64_t Value) { 25 switch (Kind) { 26 default: 27 llvm_unreachable("Unknown fixup kind!"); 28 case FK_Data_1: 29 case FK_Data_2: 30 case FK_Data_4: 31 return Value; 32 case PPC::fixup_ppc_brcond14: 33 return Value & 0x3ffc; 34 case PPC::fixup_ppc_br24: 35 return Value & 0x3fffffc; 36 #if 0 37 case PPC::fixup_ppc_hi16: 38 return (Value >> 16) & 0xffff; 39 #endif 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; 44 } 45 } 46 47 namespace { 48 class PPCMachObjectWriter : public MCMachObjectTargetWriter { 49 public: 50 PPCMachObjectWriter(bool Is64Bit, uint32_t CPUType, 51 uint32_t CPUSubtype) 52 : MCMachObjectTargetWriter(Is64Bit, CPUType, CPUSubtype) {} 53 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) {} 58 }; 59 60 class PPCELFObjectWriter : public MCELFObjectTargetWriter { 61 public: 62 PPCELFObjectWriter(bool Is64Bit, Triple::OSType OSType, uint16_t EMachine, 63 bool HasRelocationAddend, bool isLittleEndian) 64 : MCELFObjectTargetWriter(Is64Bit, OSType, EMachine, HasRelocationAddend) {} 65 }; 66 67 class PPCAsmBackend : public MCAsmBackend { 68 const Target &TheTarget; 69 public: 70 PPCAsmBackend(const Target &T) : MCAsmBackend(), TheTarget(T) {} 71 72 unsigned getNumFixupKinds() const { return PPC::NumTargetFixupKinds; } 73 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 } 82 }; 83 84 if (Kind < FirstTargetFixupKind) 85 return MCAsmBackend::getFixupKindInfo(Kind); 86 87 assert(unsigned(Kind - FirstTargetFixupKind) < getNumFixupKinds() && 88 "Invalid kind!"); 89 return Infos[Kind - FirstTargetFixupKind]; 90 } 91 92 bool MayNeedRelaxation(const MCInst &Inst) const { 93 // FIXME. 94 return false; 95 } 96 97 bool fixupNeedsRelaxation(const MCFixup &Fixup, 98 uint64_t Value, 99 const MCInstFragment *DF, 100 const MCAsmLayout &Layout) const { 101 // FIXME. 102 assert(0 && "RelaxInstruction() unimplemented"); 103 return false; 104 } 105 106 107 void RelaxInstruction(const MCInst &Inst, MCInst &Res) const { 108 // FIXME. 109 assert(0 && "RelaxInstruction() unimplemented"); 110 } 111 112 bool WriteNopData(uint64_t Count, MCObjectWriter *OW) const { 113 // FIXME: Zero fill for now. That's not right, but at least will get the 114 // section size right. 115 for (uint64_t i = 0; i != Count; ++i) 116 OW->Write8(0); 117 return true; 118 } 119 120 unsigned getPointerSize() const { 121 StringRef Name = TheTarget.getName(); 122 if (Name == "ppc64") return 8; 123 assert(Name == "ppc32" && "Unknown target name!"); 124 return 4; 125 } 126 }; 127 } // end anonymous namespace 128 129 130 // FIXME: This should be in a separate file. 131 namespace { 132 class DarwinPPCAsmBackend : public PPCAsmBackend { 133 public: 134 DarwinPPCAsmBackend(const Target &T) : PPCAsmBackend(T) { } 135 136 void ApplyFixup(const MCFixup &Fixup, char *Data, unsigned DataSize, 137 uint64_t Value) const { 138 assert(0 && "UNIMP"); 139 } 140 141 MCObjectWriter *createObjectWriter(raw_ostream &OS) const { 142 bool is64 = getPointerSize() == 8; 143 return createMachObjectWriter(new PPCMachObjectWriter( 144 /*Is64Bit=*/is64, 145 (is64 ? object::mach::CTM_PowerPC64 : 146 object::mach::CTM_PowerPC), 147 object::mach::CSPPC_ALL), 148 OS, /*IsLittleEndian=*/false); 149 } 150 151 virtual bool doesSectionRequireSymbols(const MCSection &Section) const { 152 return false; 153 } 154 }; 155 156 class ELFPPCAsmBackend : public PPCAsmBackend { 157 Triple::OSType OSType; 158 public: 159 ELFPPCAsmBackend(const Target &T, Triple::OSType OSType) : 160 PPCAsmBackend(T), OSType(OSType) { } 161 162 void ApplyFixup(const MCFixup &Fixup, char *Data, unsigned DataSize, 163 uint64_t Value) const { 164 Value = adjustFixupValue(Fixup.getKind(), Value); 165 if (!Value) return; // Doesn't change encoding. 166 167 unsigned Offset = Fixup.getOffset(); 168 169 // For each byte of the fragment that the fixup touches, mask in the bits from 170 // the fixup value. The Value has been "split up" into the appropriate 171 // bitfields above. 172 for (unsigned i = 0; i != 4; ++i) 173 Data[Offset + i] |= uint8_t((Value >> ((4 - i - 1)*8)) & 0xff); 174 } 175 176 MCObjectWriter *createObjectWriter(raw_ostream &OS) const { 177 bool is64 = getPointerSize() == 8; 178 return createELFObjectWriter(new PPCELFObjectWriter( 179 /*Is64Bit=*/is64, 180 OSType, 181 is64 ? ELF::EM_PPC64 : ELF::EM_PPC, 182 /*addend*/ true, /*isLittleEndian*/ false), 183 OS, /*IsLittleEndian=*/false); 184 } 185 186 virtual bool doesSectionRequireSymbols(const MCSection &Section) const { 187 return false; 188 } 189 }; 190 191 } // end anonymous namespace 192 193 194 195 196 MCAsmBackend *llvm::createPPCAsmBackend(const Target &T, StringRef TT) { 197 if (Triple(TT).isOSDarwin()) 198 return new DarwinPPCAsmBackend(T); 199 200 return new ELFPPCAsmBackend(T, Triple(TT).getOS()); 201 } 202