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/ADT/Twine.h"
12 #include "llvm/MC/MCAsmBackend.h"
13 #include "llvm/MC/MCAsmLayout.h"
14 #include "llvm/MC/MCAssembler.h"
15 #include "llvm/MC/MCELFObjectWriter.h"
16 #include "llvm/MC/MCELFSymbolFlags.h"
17 #include "llvm/MC/MCExpr.h"
18 #include "llvm/MC/MCObjectWriter.h"
19 #include "llvm/MC/MCSectionELF.h"
20 #include "llvm/MC/MCSectionMachO.h"
21 #include "llvm/MC/MCValue.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: llvm_unreachable("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;
43 class MBlazeAsmBackend : public MCAsmBackend {
45 MBlazeAsmBackend(const Target &T)
49 unsigned getNumFixupKinds() const {
53 bool mayNeedRelaxation(const MCInst &Inst) const;
55 bool fixupNeedsRelaxation(const MCFixup &Fixup,
57 const MCRelaxableFragment *DF,
58 const MCAsmLayout &Layout) const;
60 void relaxInstruction(const MCInst &Inst, MCInst &Res) const;
62 bool writeNopData(uint64_t Count, MCObjectWriter *OW) const;
64 unsigned getPointerSize() const {
69 static unsigned getRelaxedOpcode(unsigned Op) {
72 case MBlaze::ADDIK: return MBlaze::ADDIK32;
73 case MBlaze::ORI: return MBlaze::ORI32;
74 case MBlaze::BRLID: return MBlaze::BRLID32;
78 bool MBlazeAsmBackend::mayNeedRelaxation(const MCInst &Inst) const {
79 if (getRelaxedOpcode(Inst.getOpcode()) == Inst.getOpcode())
82 bool hasExprOrImm = false;
83 for (unsigned i = 0; i < Inst.getNumOperands(); ++i)
84 hasExprOrImm |= Inst.getOperand(i).isExpr();
89 bool MBlazeAsmBackend::fixupNeedsRelaxation(const MCFixup &Fixup,
91 const MCRelaxableFragment *DF,
92 const MCAsmLayout &Layout) const {
93 // FIXME: Is this right? It's what the "generic" code was doing before,
94 // but is X86 specific. Is it actually true for MBlaze also, or was it
95 // just close enough to not be a big deal?
97 // Relax if the value is too big for a (signed) i8.
98 return int64_t(Value) != int64_t(int8_t(Value));
101 void MBlazeAsmBackend::relaxInstruction(const MCInst &Inst, MCInst &Res) const {
103 Res.setOpcode(getRelaxedOpcode(Inst.getOpcode()));
106 bool MBlazeAsmBackend::writeNopData(uint64_t Count, MCObjectWriter *OW) const {
107 if ((Count % 4) != 0)
110 for (uint64_t i = 0; i < Count; i += 4)
111 OW->Write32(0x00000000);
115 } // end anonymous namespace
118 class ELFMBlazeAsmBackend : public MBlazeAsmBackend {
121 ELFMBlazeAsmBackend(const Target &T, uint8_t _OSABI)
122 : MBlazeAsmBackend(T), OSABI(_OSABI) { }
124 void applyFixup(const MCFixup &Fixup, char *Data, unsigned DataSize,
125 uint64_t Value) const;
127 MCObjectWriter *createObjectWriter(raw_ostream &OS) const {
128 return createMBlazeELFObjectWriter(OS, OSABI);
132 void ELFMBlazeAsmBackend::applyFixup(const MCFixup &Fixup, char *Data,
133 unsigned DataSize, uint64_t Value) const {
134 unsigned Size = getFixupKindSize(Fixup.getKind());
136 assert(Fixup.getOffset() + Size <= DataSize &&
137 "Invalid fixup offset!");
139 char *data = Data + Fixup.getOffset();
141 default: llvm_unreachable("Cannot fixup unknown value.");
142 case 1: llvm_unreachable("Cannot fixup 1 byte value.");
143 case 8: llvm_unreachable("Cannot fixup 8 byte value.");
146 *(data+7) = uint8_t(Value);
147 *(data+6) = uint8_t(Value >> 8);
148 *(data+3) = uint8_t(Value >> 16);
149 *(data+2) = uint8_t(Value >> 24);
153 *(data+3) = uint8_t(Value >> 0);
154 *(data+2) = uint8_t(Value >> 8);
157 } // end anonymous namespace
159 MCAsmBackend *llvm::createMBlazeAsmBackend(const Target &T, StringRef TT,
161 Triple TheTriple(TT);
163 if (TheTriple.isOSDarwin())
164 assert(0 && "Mac not supported on MBlaze");
166 if (TheTriple.isOSWindows())
167 assert(0 && "Windows not supported on MBlaze");
169 uint8_t OSABI = MCELFObjectTargetWriter::getOSABI(TheTriple.getOS());
170 return new ELFMBlazeAsmBackend(T, OSABI);