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/MCAssembler.h"
15 #include "llvm/MC/MCExpr.h"
16 #include "llvm/MC/MCObjectFormat.h"
17 #include "llvm/MC/MCObjectWriter.h"
18 #include "llvm/MC/MCSectionELF.h"
19 #include "llvm/MC/MCSectionMachO.h"
20 #include "llvm/Support/ELF.h"
21 #include "llvm/Support/ErrorHandling.h"
22 #include "llvm/Support/raw_ostream.h"
23 #include "llvm/Target/TargetRegistry.h"
24 #include "llvm/Target/TargetAsmBackend.h"
27 static unsigned getFixupKindSize(unsigned Kind) {
29 default: assert(0 && "invalid fixup kind!");
30 case FK_Data_1: return 1;
31 case MBlaze::reloc_pcrel_2byte:
32 case FK_Data_2: return 2;
33 case MBlaze::reloc_pcrel_4byte:
34 case FK_Data_4: return 4;
35 case FK_Data_8: return 8;
41 class MBlazeAsmBackend : public TargetAsmBackend {
43 MBlazeAsmBackend(const Target &T)
44 : TargetAsmBackend(T) {
47 bool MayNeedRelaxation(const MCInst &Inst) const;
49 void RelaxInstruction(const MCInst &Inst, MCInst &Res) const;
51 bool WriteNopData(uint64_t Count, MCObjectWriter *OW) const;
53 unsigned getPointerSize() const {
58 bool MBlazeAsmBackend::MayNeedRelaxation(const MCInst &Inst) const {
62 void MBlazeAsmBackend::RelaxInstruction(const MCInst &Inst, MCInst &Res) const {
63 assert(0 && "MBlazeAsmBackend::RelaxInstruction() unimplemented");
67 bool MBlazeAsmBackend::WriteNopData(uint64_t Count, MCObjectWriter *OW) const {
71 for (uint64_t i = 0; i < Count; i += 4)
72 OW->Write32(0x00000000);
76 } // end anonymous namespace
79 // FIXME: This should be in a separate file.
80 // ELF is an ELF of course...
81 class ELFMBlazeAsmBackend : public MBlazeAsmBackend {
82 MCELFObjectFormat Format;
85 Triple::OSType OSType;
86 ELFMBlazeAsmBackend(const Target &T, Triple::OSType _OSType)
87 : MBlazeAsmBackend(T), OSType(_OSType) {
88 HasScatteredSymbols = true;
91 virtual const MCObjectFormat &getObjectFormat() const {
96 void ApplyFixup(const MCFixup &Fixup, MCDataFragment &DF,
97 uint64_t Value) const;
99 MCObjectWriter *createObjectWriter(raw_ostream &OS) const {
100 return createELFObjectWriter(OS, /*Is64Bit=*/false,
101 OSType, ELF::EM_MBLAZE,
102 /*IsLittleEndian=*/false,
103 /*HasRelocationAddend=*/true);
107 void ELFMBlazeAsmBackend::ApplyFixup(const MCFixup &Fixup, MCDataFragment &DF,
108 uint64_t Value) const {
109 unsigned Size = getFixupKindSize(Fixup.getKind());
111 assert(Fixup.getOffset() + Size <= DF.getContents().size() &&
112 "Invalid fixup offset!");
114 char *data = DF.getContents().data() + Fixup.getOffset();
116 default: llvm_unreachable("Cannot fixup unknown value.");
117 case 1: llvm_unreachable("Cannot fixup 1 byte value.");
118 case 8: llvm_unreachable("Cannot fixup 8 byte value.");
121 *(data+7) = uint8_t(Value);
122 *(data+6) = uint8_t(Value >> 8);
123 *(data+3) = uint8_t(Value >> 16);
124 *(data+2) = uint8_t(Value >> 24);
128 *(data+3) = uint8_t(Value >> 0);
129 *(data+2) = uint8_t(Value >> 8);
132 } // end anonymous namespace
134 TargetAsmBackend *llvm::createMBlazeAsmBackend(const Target &T,
135 const std::string &TT) {
136 switch (Triple(TT).getOS()) {
138 assert(0 && "Mac not supported on MBlaze");
139 case Triple::MinGW32:
142 assert(0 && "Windows not supported on MBlaze");
144 return new ELFMBlazeAsmBackend(T, Triple(TT).getOS());