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