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 "MBlazeELFWriterInfo.h"
13 #include "MBlazeFixupKinds.h"
14 #include "llvm/ADT/Twine.h"
15 #include "llvm/MC/MCAssembler.h"
16 #include "llvm/MC/MCAsmLayout.h"
17 #include "llvm/MC/MCELFSymbolFlags.h"
18 #include "llvm/MC/MCExpr.h"
19 #include "llvm/MC/MCObjectFormat.h"
20 #include "llvm/MC/MCObjectWriter.h"
21 #include "llvm/MC/MCSectionELF.h"
22 #include "llvm/MC/MCSectionMachO.h"
23 #include "llvm/MC/MCValue.h"
24 #include "llvm/Support/ELF.h"
25 #include "llvm/Support/ErrorHandling.h"
26 #include "llvm/Support/raw_ostream.h"
27 #include "llvm/Target/TargetRegistry.h"
28 #include "llvm/Target/TargetAsmBackend.h"
31 static unsigned getFixupKindSize(unsigned Kind) {
33 default: assert(0 && "invalid fixup kind!");
34 case FK_Data_1: return 1;
35 case MBlaze::reloc_pcrel_2byte:
36 case FK_Data_2: return 2;
37 case MBlaze::reloc_pcrel_4byte:
38 case FK_Data_4: return 4;
39 case FK_Data_8: return 8;
45 class MBlazeAsmBackend : public TargetAsmBackend {
47 MBlazeAsmBackend(const Target &T)
48 : TargetAsmBackend(T) {
51 bool MayNeedRelaxation(const MCInst &Inst) const;
53 void RelaxInstruction(const MCInst &Inst, MCInst &Res) const;
55 bool WriteNopData(uint64_t Count, MCObjectWriter *OW) const;
57 unsigned getPointerSize() const {
62 static unsigned getRelaxedOpcode(unsigned Op) {
65 case MBlaze::ADDI: return MBlaze::ADDI32;
66 case MBlaze::ORI: return MBlaze::ORI32;
67 case MBlaze::BRLID: return MBlaze::BRLID32;
71 bool MBlazeAsmBackend::MayNeedRelaxation(const MCInst &Inst) const {
72 if (getRelaxedOpcode(Inst.getOpcode()) == Inst.getOpcode())
75 bool hasExprOrImm = false;
76 for (unsigned i = 0; i < Inst.getNumOperands(); ++i)
77 hasExprOrImm |= Inst.getOperand(i).isExpr();
82 void MBlazeAsmBackend::RelaxInstruction(const MCInst &Inst, MCInst &Res) const {
84 Res.setOpcode(getRelaxedOpcode(Inst.getOpcode()));
87 bool MBlazeAsmBackend::WriteNopData(uint64_t Count, MCObjectWriter *OW) const {
91 for (uint64_t i = 0; i < Count; i += 4)
92 OW->Write32(0x00000000);
96 } // end anonymous namespace
99 class ELFMBlazeAsmBackend : public MBlazeAsmBackend {
100 MCELFObjectFormat Format;
103 Triple::OSType OSType;
104 ELFMBlazeAsmBackend(const Target &T, Triple::OSType _OSType)
105 : MBlazeAsmBackend(T), OSType(_OSType) {
106 HasScatteredSymbols = true;
109 virtual const MCObjectFormat &getObjectFormat() const {
114 void ApplyFixup(const MCFixup &Fixup, MCDataFragment &DF,
115 uint64_t Value) const;
117 MCObjectWriter *createObjectWriter(raw_ostream &OS) const {
118 return createELFObjectWriter(OS,/*Is64Bit=*/false,
119 OSType, ELF::EM_MBLAZE,
120 /*IsLittleEndian=*/false,
121 /*HasRelocationAddend=*/true);
125 void ELFMBlazeAsmBackend::ApplyFixup(const MCFixup &Fixup, MCDataFragment &DF,
126 uint64_t Value) const {
127 unsigned Size = getFixupKindSize(Fixup.getKind());
129 assert(Fixup.getOffset() + Size <= DF.getContents().size() &&
130 "Invalid fixup offset!");
132 char *data = DF.getContents().data() + Fixup.getOffset();
134 default: llvm_unreachable("Cannot fixup unknown value.");
135 case 1: llvm_unreachable("Cannot fixup 1 byte value.");
136 case 8: llvm_unreachable("Cannot fixup 8 byte value.");
139 *(data+7) = uint8_t(Value);
140 *(data+6) = uint8_t(Value >> 8);
141 *(data+3) = uint8_t(Value >> 16);
142 *(data+2) = uint8_t(Value >> 24);
146 *(data+3) = uint8_t(Value >> 0);
147 *(data+2) = uint8_t(Value >> 8);
150 } // end anonymous namespace
152 TargetAsmBackend *llvm::createMBlazeAsmBackend(const Target &T,
153 const std::string &TT) {
154 switch (Triple(TT).getOS()) {
156 assert(0 && "Mac not supported on MBlaze");
157 case Triple::MinGW32:
160 assert(0 && "Windows not supported on MBlaze");
162 return new ELFMBlazeAsmBackend(T, Triple(TT).getOS());