Chris Lattner | b46443a | 2010-11-15 08:49:58 +0000 | [diff] [blame] | 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/MCSectionMachO.h" |
| 14 | #include "llvm/MC/MCObjectFormat.h" |
| 15 | #include "llvm/MC/MCObjectWriter.h" |
Daniel Dunbar | 36d76a8 | 2010-11-27 04:38:36 +0000 | [diff] [blame] | 16 | #include "llvm/Object/MachOFormat.h" |
Chris Lattner | b46443a | 2010-11-15 08:49:58 +0000 | [diff] [blame] | 17 | #include "llvm/Target/TargetRegistry.h" |
Chris Lattner | b46443a | 2010-11-15 08:49:58 +0000 | [diff] [blame] | 18 | using namespace llvm; |
| 19 | |
| 20 | namespace { |
| 21 | class PPCAsmBackend : public TargetAsmBackend { |
Rafael Espindola | fd46797 | 2010-11-26 04:24:21 +0000 | [diff] [blame] | 22 | const Target &TheTarget; |
Chris Lattner | b46443a | 2010-11-15 08:49:58 +0000 | [diff] [blame] | 23 | public: |
Rafael Espindola | fd46797 | 2010-11-26 04:24:21 +0000 | [diff] [blame] | 24 | PPCAsmBackend(const Target &T) : TargetAsmBackend(), TheTarget(T) {} |
Daniel Dunbar | 2761fc4 | 2010-12-16 03:20:06 +0000 | [diff] [blame^] | 25 | |
| 26 | unsigned getNumFixupKinds() const { return PPC::NumTargetFixupKinds; } |
| 27 | |
| 28 | const MCFixupKindInfo &getFixupKindInfo(MCFixupKind Kind) const { |
| 29 | const static MCFixupKindInfo Infos[PPC::NumTargetFixupKinds] = { |
| 30 | // name offset bits flags |
| 31 | { "fixup_ppc_br24", 6, 24, MCFixupKindInfo::FKF_IsPCRel }, |
| 32 | { "fixup_ppc_brcond14", 16, 14, MCFixupKindInfo::FKF_IsPCRel }, |
| 33 | { "fixup_ppc_lo16", 16, 16, 0 }, |
| 34 | { "fixup_ppc_ha16", 16, 16, 0 }, |
| 35 | { "fixup_ppc_lo14", 16, 14, 0 } |
| 36 | }; |
| 37 | |
| 38 | if (Kind < FirstTargetFixupKind) |
| 39 | return TargetAsmBackend::getFixupKindInfo(Kind); |
| 40 | |
| 41 | assert(unsigned(Kind - FirstTargetFixupKind) < getNumFixupKinds() && |
| 42 | "Invalid kind!"); |
| 43 | return Infos[Kind - FirstTargetFixupKind]; |
| 44 | } |
Chris Lattner | b46443a | 2010-11-15 08:49:58 +0000 | [diff] [blame] | 45 | |
| 46 | bool MayNeedRelaxation(const MCInst &Inst) const { |
| 47 | // FIXME. |
| 48 | return false; |
| 49 | } |
| 50 | |
| 51 | void RelaxInstruction(const MCInst &Inst, MCInst &Res) const { |
| 52 | // FIXME. |
| 53 | assert(0 && "RelaxInstruction() unimplemented"); |
| 54 | } |
| 55 | |
| 56 | bool WriteNopData(uint64_t Count, MCObjectWriter *OW) const { |
| 57 | // FIXME: Zero fill for now. That's not right, but at least will get the |
| 58 | // section size right. |
| 59 | for (uint64_t i = 0; i != Count; ++i) |
| 60 | OW->Write8(0); |
| 61 | return true; |
| 62 | } |
| 63 | |
| 64 | unsigned getPointerSize() const { |
| 65 | StringRef Name = TheTarget.getName(); |
| 66 | if (Name == "ppc64") return 8; |
| 67 | assert(Name == "ppc32" && "Unknown target name!"); |
| 68 | return 4; |
| 69 | } |
| 70 | }; |
| 71 | } // end anonymous namespace |
| 72 | |
| 73 | |
| 74 | // FIXME: This should be in a separate file. |
| 75 | namespace { |
| 76 | class DarwinPPCAsmBackend : public PPCAsmBackend { |
| 77 | MCMachOObjectFormat Format; |
| 78 | public: |
| 79 | DarwinPPCAsmBackend(const Target &T) : PPCAsmBackend(T) { |
| 80 | HasScatteredSymbols = true; |
| 81 | } |
| 82 | |
| 83 | virtual const MCObjectFormat &getObjectFormat() const { |
| 84 | return Format; |
| 85 | } |
| 86 | |
Rafael Espindola | 179821a | 2010-12-06 19:08:48 +0000 | [diff] [blame] | 87 | void ApplyFixup(const MCFixup &Fixup, char *Data, unsigned DataSize, |
Chris Lattner | b46443a | 2010-11-15 08:49:58 +0000 | [diff] [blame] | 88 | uint64_t Value) const { |
| 89 | assert(0 && "UNIMP"); |
| 90 | } |
| 91 | |
Chris Lattner | b46443a | 2010-11-15 08:49:58 +0000 | [diff] [blame] | 92 | MCObjectWriter *createObjectWriter(raw_ostream &OS) const { |
| 93 | bool is64 = getPointerSize() == 8; |
| 94 | return createMachObjectWriter(OS, /*Is64Bit=*/is64, |
Daniel Dunbar | 36d76a8 | 2010-11-27 04:38:36 +0000 | [diff] [blame] | 95 | (is64 ? object::mach::CTM_PowerPC64 : |
| 96 | object::mach::CTM_PowerPC), |
| 97 | object::mach::CSPPC_ALL, |
Chris Lattner | b46443a | 2010-11-15 08:49:58 +0000 | [diff] [blame] | 98 | /*IsLittleEndian=*/false); |
| 99 | } |
| 100 | |
| 101 | virtual bool doesSectionRequireSymbols(const MCSection &Section) const { |
| 102 | return false; |
| 103 | } |
| 104 | }; |
| 105 | } // end anonymous namespace |
| 106 | |
| 107 | |
| 108 | |
| 109 | |
| 110 | TargetAsmBackend *llvm::createPPCAsmBackend(const Target &T, |
| 111 | const std::string &TT) { |
| 112 | switch (Triple(TT).getOS()) { |
| 113 | case Triple::Darwin: |
| 114 | return new DarwinPPCAsmBackend(T); |
| 115 | default: |
| 116 | return 0; |
| 117 | } |
| 118 | } |