1//===-- LanaiAsmBackend.cpp - Lanai Assembler Backend ---------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "LanaiFixupKinds.h"
10#include "MCTargetDesc/LanaiMCTargetDesc.h"
11#include "llvm/MC/MCAsmBackend.h"
12#include "llvm/MC/MCAssembler.h"
13#include "llvm/MC/MCELFObjectWriter.h"
14#include "llvm/MC/MCFixupKindInfo.h"
15#include "llvm/MC/MCObjectWriter.h"
16#include "llvm/MC/MCSubtargetInfo.h"
17#include "llvm/MC/MCValue.h"
18#include "llvm/Support/ErrorHandling.h"
19#include "llvm/Support/raw_ostream.h"
20
21using namespace llvm;
22
23// Prepare value for the target space
24static unsigned adjustFixupValue(unsigned Kind, uint64_t Value) {
25 switch (Kind) {
26 case FK_Data_1:
27 case FK_Data_2:
28 case FK_Data_4:
29 case FK_Data_8:
30 return Value;
31 case Lanai::FIXUP_LANAI_21:
32 case Lanai::FIXUP_LANAI_21_F:
33 case Lanai::FIXUP_LANAI_25:
34 case Lanai::FIXUP_LANAI_32:
35 case Lanai::FIXUP_LANAI_HI16:
36 case Lanai::FIXUP_LANAI_LO16:
37 return Value;
38 default:
39 llvm_unreachable("Unknown fixup kind!");
40 }
41}
42
43namespace {
44class LanaiAsmBackend : public MCAsmBackend {
45 Triple::OSType OSType;
46
47public:
48 LanaiAsmBackend(const Target &T, Triple::OSType OST)
49 : MCAsmBackend(llvm::endianness::big), OSType(OST) {}
50
51 void applyFixup(const MCFragment &, const MCFixup &, const MCValue &Target,
52 MutableArrayRef<char> Data, uint64_t Value,
53 bool IsResolved) override;
54
55 std::unique_ptr<MCObjectTargetWriter>
56 createObjectTargetWriter() const override;
57
58 MCFixupKindInfo getFixupKindInfo(MCFixupKind Kind) const override;
59
60 bool writeNopData(raw_ostream &OS, uint64_t Count,
61 const MCSubtargetInfo *STI) const override;
62};
63
64bool LanaiAsmBackend::writeNopData(raw_ostream &OS, uint64_t Count,
65 const MCSubtargetInfo *STI) const {
66 if ((Count % 4) != 0)
67 return false;
68
69 for (uint64_t i = 0; i < Count; i += 4)
70 OS.write(Ptr: "\x15\0\0\0", Size: 4);
71
72 return true;
73}
74
75void LanaiAsmBackend::applyFixup(const MCFragment &F, const MCFixup &Fixup,
76 const MCValue &Target,
77 MutableArrayRef<char> Data, uint64_t Value,
78 bool IsResolved) {
79 if (!IsResolved)
80 Asm->getWriter().recordRelocation(F, Fixup, Target, FixedValue&: Value);
81
82 MCFixupKind Kind = Fixup.getKind();
83 Value = adjustFixupValue(Kind: static_cast<unsigned>(Kind), Value);
84 if (!Value)
85 return; // This value doesn't change the encoding
86
87 // Where in the object and where the number of bytes that need
88 // fixing up
89 unsigned Offset = Fixup.getOffset();
90 unsigned NumBytes = (getFixupKindInfo(Kind).TargetSize + 7) / 8;
91 unsigned FullSize = 4;
92
93 // Grab current value, if any, from bits.
94 uint64_t CurVal = 0;
95
96 // Load instruction and apply value
97 for (unsigned i = 0; i != NumBytes; ++i) {
98 unsigned Idx = (FullSize - 1 - i);
99 CurVal |= static_cast<uint64_t>(static_cast<uint8_t>(Data[Offset + Idx]))
100 << (i * 8);
101 }
102
103 uint64_t Mask =
104 (static_cast<uint64_t>(-1) >> (64 - getFixupKindInfo(Kind).TargetSize));
105 CurVal |= Value & Mask;
106
107 // Write out the fixed up bytes back to the code/data bits.
108 for (unsigned i = 0; i != NumBytes; ++i) {
109 unsigned Idx = (FullSize - 1 - i);
110 Data[Offset + Idx] = static_cast<uint8_t>((CurVal >> (i * 8)) & 0xff);
111 }
112}
113
114std::unique_ptr<MCObjectTargetWriter>
115LanaiAsmBackend::createObjectTargetWriter() const {
116 return createLanaiELFObjectWriter(OSABI: MCELFObjectTargetWriter::getOSABI(OSType));
117}
118
119MCFixupKindInfo LanaiAsmBackend::getFixupKindInfo(MCFixupKind Kind) const {
120 static const MCFixupKindInfo Infos[Lanai::NumTargetFixupKinds] = {
121 // This table *must* be in same the order of fixup_* kinds in
122 // LanaiFixupKinds.h.
123 // Note: The number of bits indicated here are assumed to be contiguous.
124 // This does not hold true for LANAI_21 and LANAI_21_F which are applied
125 // to bits 0x7cffff and 0x7cfffc, respectively. Since the 'bits' counts
126 // here are used only for cosmetic purposes, we set the size to 16 bits
127 // for these 21-bit relocation as llvm/lib/MC/MCAsmStreamer.cpp checks
128 // no bits are set in the fixup range.
129 //
130 // name offset bits flags
131 {.Name: "FIXUP_LANAI_NONE", .TargetOffset: 0, .TargetSize: 32, .Flags: 0},
132 {.Name: "FIXUP_LANAI_21", .TargetOffset: 16, .TargetSize: 16 /*21*/, .Flags: 0},
133 {.Name: "FIXUP_LANAI_21_F", .TargetOffset: 16, .TargetSize: 16 /*21*/, .Flags: 0},
134 {.Name: "FIXUP_LANAI_25", .TargetOffset: 7, .TargetSize: 25, .Flags: 0},
135 {.Name: "FIXUP_LANAI_32", .TargetOffset: 0, .TargetSize: 32, .Flags: 0},
136 {.Name: "FIXUP_LANAI_HI16", .TargetOffset: 16, .TargetSize: 16, .Flags: 0},
137 {.Name: "FIXUP_LANAI_LO16", .TargetOffset: 16, .TargetSize: 16, .Flags: 0}};
138
139 if (Kind < FirstTargetFixupKind)
140 return MCAsmBackend::getFixupKindInfo(Kind);
141
142 assert(unsigned(Kind - FirstTargetFixupKind) < Lanai::NumTargetFixupKinds &&
143 "Invalid kind!");
144 return Infos[Kind - FirstTargetFixupKind];
145}
146
147} // namespace
148
149MCAsmBackend *llvm::createLanaiAsmBackend(const Target &T,
150 const MCSubtargetInfo &STI,
151 const MCRegisterInfo & /*MRI*/,
152 const MCTargetOptions & /*Options*/) {
153 const Triple &TT = STI.getTargetTriple();
154 if (!TT.isOSBinFormatELF())
155 llvm_unreachable("OS not supported");
156
157 return new LanaiAsmBackend(T, TT.getOS());
158}
159