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 "MCTargetDesc/MBlazeMCTargetDesc.h"
11 #include "llvm/MC/MCAsmBackend.h"
12 #include "llvm/MC/MCAssembler.h"
13 #include "llvm/MC/MCAsmLayout.h"
14 #include "llvm/MC/MCELFObjectWriter.h"
15 #include "llvm/MC/MCELFSymbolFlags.h"
16 #include "llvm/MC/MCExpr.h"
17 #include "llvm/MC/MCObjectWriter.h"
18 #include "llvm/MC/MCSectionELF.h"
19 #include "llvm/MC/MCSectionMachO.h"
20 #include "llvm/MC/MCValue.h"
21 #include "llvm/ADT/Twine.h"
22 #include "llvm/Support/ELF.h"
23 #include "llvm/Support/ErrorHandling.h"
24 #include "llvm/Support/TargetRegistry.h"
25 #include "llvm/Support/raw_ostream.h"
28 static unsigned getFixupKindSize(unsigned Kind) {
30 default: assert(0 && "invalid fixup kind!");
31 case FK_Data_1: return 1;
33 case FK_Data_2: return 2;
35 case FK_Data_4: return 4;
36 case FK_Data_8: return 8;
42 class MBlazeELFObjectWriter : public MCELFObjectTargetWriter {
44 MBlazeELFObjectWriter(Triple::OSType OSType)
45 : MCELFObjectTargetWriter(/*is64Bit*/ false, OSType, ELF::EM_MBLAZE,
46 /*HasRelocationAddend*/ true) {}
49 class MBlazeAsmBackend : public MCAsmBackend {
51 MBlazeAsmBackend(const Target &T)
55 unsigned getNumFixupKinds() const {
59 bool MayNeedRelaxation(const MCInst &Inst) const;
61 void RelaxInstruction(const MCInst &Inst, MCInst &Res) const;
63 bool WriteNopData(uint64_t Count, MCObjectWriter *OW) const;
65 unsigned getPointerSize() const {
70 static unsigned getRelaxedOpcode(unsigned Op) {
73 case MBlaze::ADDIK: return MBlaze::ADDIK32;
74 case MBlaze::ORI: return MBlaze::ORI32;
75 case MBlaze::BRLID: return MBlaze::BRLID32;
79 bool MBlazeAsmBackend::MayNeedRelaxation(const MCInst &Inst) const {
80 if (getRelaxedOpcode(Inst.getOpcode()) == Inst.getOpcode())
83 bool hasExprOrImm = false;
84 for (unsigned i = 0; i < Inst.getNumOperands(); ++i)
85 hasExprOrImm |= Inst.getOperand(i).isExpr();
90 void MBlazeAsmBackend::RelaxInstruction(const MCInst &Inst, MCInst &Res) const {
92 Res.setOpcode(getRelaxedOpcode(Inst.getOpcode()));
95 bool MBlazeAsmBackend::WriteNopData(uint64_t Count, MCObjectWriter *OW) const {
99 for (uint64_t i = 0; i < Count; i += 4)
100 OW->Write32(0x00000000);
104 } // end anonymous namespace
107 class ELFMBlazeAsmBackend : public MBlazeAsmBackend {
109 Triple::OSType OSType;
110 ELFMBlazeAsmBackend(const Target &T, Triple::OSType _OSType)
111 : MBlazeAsmBackend(T), OSType(_OSType) { }
113 void ApplyFixup(const MCFixup &Fixup, char *Data, unsigned DataSize,
114 uint64_t Value) const;
116 MCObjectWriter *createObjectWriter(raw_ostream &OS) const {
117 return createELFObjectWriter(new MBlazeELFObjectWriter(OSType), OS,
118 /*IsLittleEndian*/ false);
122 void ELFMBlazeAsmBackend::ApplyFixup(const MCFixup &Fixup, char *Data,
123 unsigned DataSize, uint64_t Value) const {
124 unsigned Size = getFixupKindSize(Fixup.getKind());
126 assert(Fixup.getOffset() + Size <= DataSize &&
127 "Invalid fixup offset!");
129 char *data = Data + Fixup.getOffset();
131 default: llvm_unreachable("Cannot fixup unknown value.");
132 case 1: llvm_unreachable("Cannot fixup 1 byte value.");
133 case 8: llvm_unreachable("Cannot fixup 8 byte value.");
136 *(data+7) = uint8_t(Value);
137 *(data+6) = uint8_t(Value >> 8);
138 *(data+3) = uint8_t(Value >> 16);
139 *(data+2) = uint8_t(Value >> 24);
143 *(data+3) = uint8_t(Value >> 0);
144 *(data+2) = uint8_t(Value >> 8);
147 } // end anonymous namespace
149 MCAsmBackend *llvm::createMBlazeAsmBackend(const Target &T, StringRef TT) {
150 Triple TheTriple(TT);
152 if (TheTriple.isOSDarwin())
153 assert(0 && "Mac not supported on MBlaze");
155 if (TheTriple.isOSWindows())
156 assert(0 && "Windows not supported on MBlaze");
158 return new ELFMBlazeAsmBackend(T, TheTriple.getOS());