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