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