Wesley Peck | 4e9141f | 2010-10-21 03:57:26 +0000 | [diff] [blame] | 1 | //===-- MBlazeAsmBackend.cpp - MBlaze 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 "MBlaze.h" |
| 12 | #include "MBlazeFixupKinds.h" |
| 13 | #include "llvm/ADT/Twine.h" |
| 14 | #include "llvm/MC/ELFObjectWriter.h" |
| 15 | #include "llvm/MC/MCAssembler.h" |
| 16 | #include "llvm/MC/MCExpr.h" |
| 17 | #include "llvm/MC/MCObjectFormat.h" |
| 18 | #include "llvm/MC/MCObjectWriter.h" |
| 19 | #include "llvm/MC/MCSectionELF.h" |
| 20 | #include "llvm/MC/MCSectionMachO.h" |
| 21 | #include "llvm/MC/MachObjectWriter.h" |
Wesley Peck | eecb858 | 2010-10-22 15:52:49 +0000 | [diff] [blame] | 22 | #include "llvm/Support/ELF.h" |
Wesley Peck | 4e9141f | 2010-10-21 03:57:26 +0000 | [diff] [blame] | 23 | #include "llvm/Support/ErrorHandling.h" |
| 24 | #include "llvm/Support/raw_ostream.h" |
| 25 | #include "llvm/Target/TargetRegistry.h" |
| 26 | #include "llvm/Target/TargetAsmBackend.h" |
| 27 | using namespace llvm; |
| 28 | |
| 29 | static unsigned getFixupKindSize(unsigned Kind) { |
| 30 | switch (Kind) { |
| 31 | default: assert(0 && "invalid fixup kind!"); |
| 32 | case FK_Data_1: return 1; |
| 33 | case MBlaze::reloc_pcrel_2byte: |
| 34 | case FK_Data_2: return 2; |
| 35 | case MBlaze::reloc_pcrel_4byte: |
| 36 | case FK_Data_4: return 4; |
| 37 | case FK_Data_8: return 8; |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | |
| 42 | namespace { |
| 43 | class MBlazeAsmBackend : public TargetAsmBackend { |
| 44 | public: |
| 45 | MBlazeAsmBackend(const Target &T) |
| 46 | : TargetAsmBackend(T) { |
| 47 | } |
| 48 | |
| 49 | bool MayNeedRelaxation(const MCInst &Inst) const; |
| 50 | |
| 51 | void RelaxInstruction(const MCInst &Inst, MCInst &Res) const; |
| 52 | |
| 53 | bool WriteNopData(uint64_t Count, MCObjectWriter *OW) const; |
| 54 | |
| 55 | unsigned getPointerSize() const { |
| 56 | return 4; |
| 57 | } |
| 58 | }; |
| 59 | |
| 60 | bool MBlazeAsmBackend::MayNeedRelaxation(const MCInst &Inst) const { |
| 61 | return false; |
| 62 | } |
| 63 | |
| 64 | void MBlazeAsmBackend::RelaxInstruction(const MCInst &Inst, MCInst &Res) const { |
| 65 | assert(0 && "MBlazeAsmBackend::RelaxInstruction() unimplemented"); |
| 66 | return; |
| 67 | } |
| 68 | |
| 69 | bool MBlazeAsmBackend::WriteNopData(uint64_t Count, MCObjectWriter *OW) const { |
| 70 | if ((Count % 4) != 0) |
| 71 | return false; |
| 72 | |
| 73 | for (uint64_t i = 0; i < Count; i += 4 ) |
| 74 | OW->Write32( 0x00000000 ); |
| 75 | |
| 76 | return true; |
| 77 | } |
| 78 | } // end anonymous namespace |
| 79 | |
| 80 | namespace { |
| 81 | // FIXME: This should be in a separate file. |
| 82 | // ELF is an ELF of course... |
| 83 | class ELFMBlazeAsmBackend : public MBlazeAsmBackend { |
| 84 | MCELFObjectFormat Format; |
| 85 | |
| 86 | public: |
| 87 | Triple::OSType OSType; |
| 88 | ELFMBlazeAsmBackend(const Target &T, Triple::OSType _OSType) |
| 89 | : MBlazeAsmBackend(T), OSType(_OSType) { |
| 90 | HasScatteredSymbols = true; |
| 91 | } |
| 92 | |
| 93 | virtual const MCObjectFormat &getObjectFormat() const { |
| 94 | return Format; |
| 95 | } |
| 96 | |
| 97 | |
| 98 | void ApplyFixup(const MCFixup &Fixup, MCDataFragment &DF, |
| 99 | uint64_t Value) const; |
| 100 | |
| 101 | bool isVirtualSection(const MCSection &Section) const { |
| 102 | const MCSectionELF &SE = static_cast<const MCSectionELF&>(Section); |
| 103 | return SE.getType() == MCSectionELF::SHT_NOBITS; |
| 104 | } |
| 105 | |
| 106 | MCObjectWriter *createObjectWriter(raw_ostream &OS) const { |
| 107 | return new ELFObjectWriter(OS, /*Is64Bit=*/false, |
Wesley Peck | eecb858 | 2010-10-22 15:52:49 +0000 | [diff] [blame] | 108 | OSType, ELF::EM_MBLAZE, |
Wesley Peck | 4e9141f | 2010-10-21 03:57:26 +0000 | [diff] [blame] | 109 | /*IsLittleEndian=*/false, |
| 110 | /*HasRelocationAddend=*/true); |
| 111 | } |
| 112 | }; |
| 113 | |
| 114 | void ELFMBlazeAsmBackend::ApplyFixup(const MCFixup &Fixup, MCDataFragment &DF, |
| 115 | uint64_t Value) const { |
| 116 | unsigned Size = getFixupKindSize(Fixup.getKind()); |
| 117 | |
| 118 | assert(Fixup.getOffset() + Size <= DF.getContents().size() && |
| 119 | "Invalid fixup offset!"); |
| 120 | |
| 121 | char *data = DF.getContents().data() + Fixup.getOffset(); |
| 122 | switch (Size) { |
| 123 | default: llvm_unreachable( "Cannot fixup unknown value." ); |
| 124 | case 1: llvm_unreachable( "Cannot fixup 1 byte value." ); |
| 125 | case 8: llvm_unreachable( "Cannot fixup 8 byte value." ); |
| 126 | |
| 127 | case 4: |
| 128 | *(data+7) = uint8_t(Value); |
| 129 | *(data+6) = uint8_t(Value >> 8); |
| 130 | *(data+3) = uint8_t(Value >> 16); |
| 131 | *(data+2) = uint8_t(Value >> 24); |
| 132 | break; |
| 133 | |
| 134 | case 2: |
| 135 | *(data+3) = uint8_t(Value >> 0); |
| 136 | *(data+2) = uint8_t(Value >> 8); |
| 137 | } |
| 138 | } |
| 139 | } // end anonymous namespace |
| 140 | |
| 141 | TargetAsmBackend *llvm::createMBlazeAsmBackend(const Target &T, |
| 142 | const std::string &TT) { |
| 143 | switch (Triple(TT).getOS()) { |
| 144 | case Triple::Darwin: |
| 145 | assert(0 && "Mac not supported on MBlaze"); |
| 146 | case Triple::MinGW32: |
| 147 | case Triple::Cygwin: |
| 148 | case Triple::Win32: |
| 149 | assert(0 && "Windows not supported on MBlaze"); |
| 150 | default: |
| 151 | return new ELFMBlazeAsmBackend(T, Triple(TT).getOS()); |
| 152 | } |
| 153 | } |