1 //===-- MBlazeAsmBackend.cpp - MBlaze Assembler Backend -------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/Target/TargetAsmBackend.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"
22 #include "llvm/Support/ELF.h"
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"
29 static unsigned getFixupKindSize(unsigned 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;
43 class MBlazeAsmBackend : public TargetAsmBackend {
45 MBlazeAsmBackend(const Target &T)
46 : TargetAsmBackend(T) {
49 bool MayNeedRelaxation(const MCInst &Inst) const;
51 void RelaxInstruction(const MCInst &Inst, MCInst &Res) const;
53 bool WriteNopData(uint64_t Count, MCObjectWriter *OW) const;
55 unsigned getPointerSize() const {
60 bool MBlazeAsmBackend::MayNeedRelaxation(const MCInst &Inst) const {
64 void MBlazeAsmBackend::RelaxInstruction(const MCInst &Inst, MCInst &Res) const {
65 assert(0 && "MBlazeAsmBackend::RelaxInstruction() unimplemented");
69 bool MBlazeAsmBackend::WriteNopData(uint64_t Count, MCObjectWriter *OW) const {
73 for (uint64_t i = 0; i < Count; i += 4)
74 OW->Write32(0x00000000);
78 } // end anonymous 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;
87 Triple::OSType OSType;
88 ELFMBlazeAsmBackend(const Target &T, Triple::OSType _OSType)
89 : MBlazeAsmBackend(T), OSType(_OSType) {
90 HasScatteredSymbols = true;
93 virtual const MCObjectFormat &getObjectFormat() const {
98 void ApplyFixup(const MCFixup &Fixup, MCDataFragment &DF,
99 uint64_t Value) const;
101 bool isVirtualSection(const MCSection &Section) const {
102 const MCSectionELF &SE = static_cast<const MCSectionELF&>(Section);
103 return SE.getType() == MCSectionELF::SHT_NOBITS;
106 MCObjectWriter *createObjectWriter(raw_ostream &OS) const {
107 return new ELFObjectWriter(OS, /*Is64Bit=*/false,
108 OSType, ELF::EM_MBLAZE,
109 /*IsLittleEndian=*/false,
110 /*HasRelocationAddend=*/true);
114 void ELFMBlazeAsmBackend::ApplyFixup(const MCFixup &Fixup, MCDataFragment &DF,
115 uint64_t Value) const {
116 unsigned Size = getFixupKindSize(Fixup.getKind());
118 assert(Fixup.getOffset() + Size <= DF.getContents().size() &&
119 "Invalid fixup offset!");
121 char *data = DF.getContents().data() + Fixup.getOffset();
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.");
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);
135 *(data+3) = uint8_t(Value >> 0);
136 *(data+2) = uint8_t(Value >> 8);
139 } // end anonymous namespace
141 TargetAsmBackend *llvm::createMBlazeAsmBackend(const Target &T,
142 const std::string &TT) {
143 switch (Triple(TT).getOS()) {
145 assert(0 && "Mac not supported on MBlaze");
146 case Triple::MinGW32:
149 assert(0 && "Windows not supported on MBlaze");
151 return new ELFMBlazeAsmBackend(T, Triple(TT).getOS());