1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
|
//===-- M68kAsmBackend.cpp - M68k Assembler Backend -------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
///
/// \file
/// This file contains definitions for M68k assembler backend.
///
//===----------------------------------------------------------------------===//
#include "MCTargetDesc/M68kBaseInfo.h"
#include "MCTargetDesc/M68kFixupKinds.h"
#include "llvm/ADT/StringSwitch.h"
#include "llvm/BinaryFormat/ELF.h"
#include "llvm/BinaryFormat/MachO.h"
#include "llvm/MC/MCAsmBackend.h"
#include "llvm/MC/MCAssembler.h"
#include "llvm/MC/MCELFObjectWriter.h"
#include "llvm/MC/MCExpr.h"
#include "llvm/MC/MCInst.h"
#include "llvm/MC/MCMachObjectWriter.h"
#include "llvm/MC/MCObjectWriter.h"
#include "llvm/MC/MCRegisterInfo.h"
#include "llvm/MC/MCSectionCOFF.h"
#include "llvm/MC/MCSectionELF.h"
#include "llvm/MC/MCSectionMachO.h"
#include "llvm/MC/MCSubtargetInfo.h"
#include "llvm/MC/MCValue.h"
#include "llvm/MC/TargetRegistry.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/MathExtras.h"
#include "llvm/Support/raw_ostream.h"
using namespace llvm;
#define DEBUG_TYPE "M68k-asm-backend"
namespace {
class M68kAsmBackend : public MCAsmBackend {
bool Allows32BitBranch;
public:
M68kAsmBackend(const Target &T, const MCSubtargetInfo &STI)
: MCAsmBackend(llvm::endianness::big),
Allows32BitBranch(llvm::StringSwitch<bool>(STI.getCPU())
.CasesLower("m68020", "m68030", "m68040", true)
.Default(false)) {}
void applyFixup(const MCFragment &, const MCFixup &, const MCValue &,
uint8_t *Data, uint64_t Value, bool IsResolved) override;
bool mayNeedRelaxation(unsigned Opcode, ArrayRef<MCOperand> Operands,
const MCSubtargetInfo &STI) const override;
bool fixupNeedsRelaxation(const MCFixup &Fixup,
uint64_t Value) const override;
void relaxInstruction(MCInst &Inst,
const MCSubtargetInfo &STI) const override;
/// Returns the minimum size of a nop in bytes on this target. The assembler
/// will use this to emit excess padding in situations where the padding
/// required for simple alignment would be less than the minimum nop size.
unsigned getMinimumNopSize() const override { return 2; }
/// Write a sequence of optimal nops to the output, covering \p Count bytes.
/// \return - true on success, false on failure
bool writeNopData(raw_ostream &OS, uint64_t Count,
const MCSubtargetInfo *STI) const override;
};
} // end anonymous namespace
void M68kAsmBackend::applyFixup(const MCFragment &F, const MCFixup &Fixup,
const MCValue &Target, uint8_t *Data,
uint64_t Value, bool IsResolved) {
if (!IsResolved)
Asm->getWriter().recordRelocation(F, Fixup, Target, Value);
unsigned Size = 1 << getFixupKindLog2Size(Fixup.getKind());
assert(Fixup.getOffset() + Size <= F.getSize() && "Invalid fixup offset!");
// Check that uppper bits are either all zeros or all ones.
// Specifically ignore overflow/underflow as long as the leakage is
// limited to the lower bits. This is to remain compatible with
// other assemblers.
assert(isIntN(Size * 8 + 1, static_cast<int64_t>(Value)) &&
"Value does not fit in the Fixup field");
// Write in Big Endian
for (unsigned i = 0; i != Size; ++i)
Data[i] = uint8_t(static_cast<int64_t>(Value) >> ((Size - i - 1) * 8));
}
/// cc—Carry clear GE—Greater than or equal
/// LS—Lower or same PL—Plus
/// CS—Carry set GT—Greater than
/// LT—Less than
/// EQ—Equal HI—Higher
/// MI—Minus VC—Overflow clear
/// LE—Less than or equal
/// NE—Not equal VS—Overflow set
static unsigned getRelaxedOpcodeBranch(unsigned Op) {
switch (Op) {
default:
return Op;
// 8 -> 16
case M68k::BRA8:
return M68k::BRA16;
case M68k::Bcc8:
return M68k::Bcc16;
case M68k::Bls8:
return M68k::Bls16;
case M68k::Blt8:
return M68k::Blt16;
case M68k::Beq8:
return M68k::Beq16;
case M68k::Bmi8:
return M68k::Bmi16;
case M68k::Bne8:
return M68k::Bne16;
case M68k::Bge8:
return M68k::Bge16;
case M68k::Bcs8:
return M68k::Bcs16;
case M68k::Bpl8:
return M68k::Bpl16;
case M68k::Bgt8:
return M68k::Bgt16;
case M68k::Bhi8:
return M68k::Bhi16;
case M68k::Bvc8:
return M68k::Bvc16;
case M68k::Ble8:
return M68k::Ble16;
case M68k::Bvs8:
return M68k::Bvs16;
// 16 -> 32
case M68k::BRA16:
return M68k::BRA32;
case M68k::Bcc16:
return M68k::Bcc32;
case M68k::Bls16:
return M68k::Bls32;
case M68k::Blt16:
return M68k::Blt32;
case M68k::Beq16:
return M68k::Beq32;
case M68k::Bmi16:
return M68k::Bmi32;
case M68k::Bne16:
return M68k::Bne32;
case M68k::Bge16:
return M68k::Bge32;
case M68k::Bcs16:
return M68k::Bcs32;
case M68k::Bpl16:
return M68k::Bpl32;
case M68k::Bgt16:
return M68k::Bgt32;
case M68k::Bhi16:
return M68k::Bhi32;
case M68k::Bvc16:
return M68k::Bvc32;
case M68k::Ble16:
return M68k::Ble32;
case M68k::Bvs16:
return M68k::Bvs32;
}
}
static unsigned getRelaxedOpcode(unsigned Opcode) {
// NOTE there will be some relaxations for PCD and ARD mem for x20
return getRelaxedOpcodeBranch(Opcode);
}
bool M68kAsmBackend::mayNeedRelaxation(unsigned Opcode, ArrayRef<MCOperand>,
const MCSubtargetInfo &STI) const {
// Branches can always be relaxed in either mode.
return getRelaxedOpcode(Opcode) != Opcode;
// NOTE will change for x20 mem
}
bool M68kAsmBackend::fixupNeedsRelaxation(const MCFixup &Fixup,
uint64_t UnsignedValue) const {
int64_t Value = static_cast<int64_t>(UnsignedValue);
if (!isInt<32>(Value) || (!Allows32BitBranch && !isInt<16>(Value)))
llvm_unreachable("Cannot relax the instruction, value does not fit");
// Relax if the value is too big for a (signed) i8
// (or signed i16 if 32 bit branches can be used). This means
// that byte-wide instructions have to matched by default
unsigned KindLog2Size = getFixupKindLog2Size(Fixup.getKind());
bool FixupFieldTooSmall = false;
if (!isInt<8>(Value) && KindLog2Size == 0)
FixupFieldTooSmall = true;
else if (!isInt<16>(Value) && KindLog2Size <= 1)
FixupFieldTooSmall = true;
// NOTE
// A branch to the immediately following instruction automatically
// uses the 16-bit displacement format because the 8-bit
// displacement field contains $00 (zero offset).
bool ZeroDisplacementNeedsFixup = Value == 0 && KindLog2Size == 0;
return ZeroDisplacementNeedsFixup || FixupFieldTooSmall;
}
// NOTE Can tblgen help at all here to verify there aren't other instructions
// we can relax?
void M68kAsmBackend::relaxInstruction(MCInst &Inst,
const MCSubtargetInfo &STI) const {
unsigned RelaxedOp = getRelaxedOpcode(Inst.getOpcode());
assert(RelaxedOp != Inst.getOpcode());
Inst.setOpcode(RelaxedOp);
}
bool M68kAsmBackend::writeNopData(raw_ostream &OS, uint64_t Count,
const MCSubtargetInfo *STI) const {
// Cannot emit NOP with size being not multiple of 16 bits.
if (Count % 2 != 0)
return false;
uint64_t NumNops = Count / 2;
for (uint64_t i = 0; i != NumNops; ++i) {
OS << "\x4E\x71";
}
return true;
}
namespace {
class M68kELFAsmBackend : public M68kAsmBackend {
public:
uint8_t OSABI;
M68kELFAsmBackend(const Target &T, const MCSubtargetInfo &STI, uint8_t OSABI)
: M68kAsmBackend(T, STI), OSABI(OSABI) {}
std::unique_ptr<MCObjectTargetWriter>
createObjectTargetWriter() const override {
return createM68kELFObjectWriter(OSABI);
}
};
} // end anonymous namespace
MCAsmBackend *llvm::createM68kAsmBackend(const Target &T,
const MCSubtargetInfo &STI,
const MCRegisterInfo &MRI,
const MCTargetOptions &Options) {
const Triple &TheTriple = STI.getTargetTriple();
uint8_t OSABI = MCELFObjectTargetWriter::getOSABI(TheTriple.getOS());
return new M68kELFAsmBackend(T, STI, OSABI);
}
|