blob: 7e11f7371e581869583d91b9e5dd37a94a3dfff8 [file] [log] [blame]
Wesley Peck4e9141f2010-10-21 03:57:26 +00001//===-- MBlazeAsmBackend.cpp - MBlaze Assembler Backend -------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "llvm/Target/TargetAsmBackend.h"
11#include "MBlaze.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/ErrorHandling.h"
23#include "llvm/Support/raw_ostream.h"
24#include "llvm/Target/TargetRegistry.h"
25#include "llvm/Target/TargetAsmBackend.h"
26using namespace llvm;
27
28static unsigned getFixupKindSize(unsigned Kind) {
29 switch (Kind) {
30 default: assert(0 && "invalid fixup kind!");
31 case FK_Data_1: return 1;
32 case MBlaze::reloc_pcrel_2byte:
33 case FK_Data_2: return 2;
34 case MBlaze::reloc_pcrel_4byte:
35 case FK_Data_4: return 4;
36 case FK_Data_8: return 8;
37 }
38}
39
40
41namespace {
42class MBlazeAsmBackend : public TargetAsmBackend {
43public:
44 MBlazeAsmBackend(const Target &T)
45 : TargetAsmBackend(T) {
46 }
47
48 bool MayNeedRelaxation(const MCInst &Inst) const;
49
50 void RelaxInstruction(const MCInst &Inst, MCInst &Res) const;
51
52 bool WriteNopData(uint64_t Count, MCObjectWriter *OW) const;
53
54 unsigned getPointerSize() const {
55 return 4;
56 }
57};
58
59bool MBlazeAsmBackend::MayNeedRelaxation(const MCInst &Inst) const {
60 return false;
61}
62
63void MBlazeAsmBackend::RelaxInstruction(const MCInst &Inst, MCInst &Res) const {
64 assert(0 && "MBlazeAsmBackend::RelaxInstruction() unimplemented");
65 return;
66}
67
68bool MBlazeAsmBackend::WriteNopData(uint64_t Count, MCObjectWriter *OW) const {
69 if ((Count % 4) != 0)
70 return false;
71
72 for (uint64_t i = 0; i < Count; i += 4 )
73 OW->Write32( 0x00000000 );
74
75 return true;
76}
77} // end anonymous namespace
78
79namespace {
80// FIXME: This should be in a separate file.
81// ELF is an ELF of course...
82class ELFMBlazeAsmBackend : public MBlazeAsmBackend {
83 MCELFObjectFormat Format;
84
85public:
86 Triple::OSType OSType;
87 ELFMBlazeAsmBackend(const Target &T, Triple::OSType _OSType)
88 : MBlazeAsmBackend(T), OSType(_OSType) {
89 HasScatteredSymbols = true;
90 }
91
92 virtual const MCObjectFormat &getObjectFormat() const {
93 return Format;
94 }
95
96
97 void ApplyFixup(const MCFixup &Fixup, MCDataFragment &DF,
98 uint64_t Value) const;
99
100 bool isVirtualSection(const MCSection &Section) const {
101 const MCSectionELF &SE = static_cast<const MCSectionELF&>(Section);
102 return SE.getType() == MCSectionELF::SHT_NOBITS;
103 }
104
105 MCObjectWriter *createObjectWriter(raw_ostream &OS) const {
106 return new ELFObjectWriter(OS, /*Is64Bit=*/false,
107 OSType,
108 /*IsLittleEndian=*/false,
109 /*HasRelocationAddend=*/true);
110 }
111};
112
113void ELFMBlazeAsmBackend::ApplyFixup(const MCFixup &Fixup, MCDataFragment &DF,
114 uint64_t Value) const {
115 unsigned Size = getFixupKindSize(Fixup.getKind());
116
117 assert(Fixup.getOffset() + Size <= DF.getContents().size() &&
118 "Invalid fixup offset!");
119
120 char *data = DF.getContents().data() + Fixup.getOffset();
121 switch (Size) {
122 default: llvm_unreachable( "Cannot fixup unknown value." );
123 case 1: llvm_unreachable( "Cannot fixup 1 byte value." );
124 case 8: llvm_unreachable( "Cannot fixup 8 byte value." );
125
126 case 4:
127 *(data+7) = uint8_t(Value);
128 *(data+6) = uint8_t(Value >> 8);
129 *(data+3) = uint8_t(Value >> 16);
130 *(data+2) = uint8_t(Value >> 24);
131 break;
132
133 case 2:
134 *(data+3) = uint8_t(Value >> 0);
135 *(data+2) = uint8_t(Value >> 8);
136 }
137}
138} // end anonymous namespace
139
140TargetAsmBackend *llvm::createMBlazeAsmBackend(const Target &T,
141 const std::string &TT) {
142 switch (Triple(TT).getOS()) {
143 case Triple::Darwin:
144 assert(0 && "Mac not supported on MBlaze");
145 case Triple::MinGW32:
146 case Triple::Cygwin:
147 case Triple::Win32:
148 assert(0 && "Windows not supported on MBlaze");
149 default:
150 return new ELFMBlazeAsmBackend(T, Triple(TT).getOS());
151 }
152}