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/MCDirectives.h"
14 #include "llvm/MC/MCELFObjectWriter.h"
15 #include "llvm/MC/MCFixupKindInfo.h"
16 #include "llvm/MC/MCObjectWriter.h"
17 #include "llvm/MC/MCSubtargetInfo.h"
18 #include "llvm/Support/ErrorHandling.h"
19 #include "llvm/Support/raw_ostream.h"
20 
21 using namespace llvm;
22 
23 // Prepare value for the target space
adjustFixupValue(unsigned Kind,uint64_t Value)24 static 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 
43 namespace {
44 class LanaiAsmBackend : public MCAsmBackend {
45   Triple::OSType OSType;
46 
47 public:
LanaiAsmBackend(const Target & T,Triple::OSType OST)48   LanaiAsmBackend(const Target &T, Triple::OSType OST)
49       : MCAsmBackend(llvm::endianness::big), OSType(OST) {}
50 
51   void applyFixup(const MCAssembler &Asm, const MCFixup &Fixup,
52                   const MCValue &Target, MutableArrayRef<char> Data,
53                   uint64_t Value, bool IsResolved,
54                   const MCSubtargetInfo *STI) const override;
55 
56   std::unique_ptr<MCObjectTargetWriter>
57   createObjectTargetWriter() const override;
58 
59   // No instruction requires relaxation
fixupNeedsRelaxation(const MCFixup &,uint64_t,const MCRelaxableFragment *,const MCAsmLayout &) const60   bool fixupNeedsRelaxation(const MCFixup & /*Fixup*/, uint64_t /*Value*/,
61                             const MCRelaxableFragment * /*DF*/,
62                             const MCAsmLayout & /*Layout*/) const override {
63     return false;
64   }
65 
66   const MCFixupKindInfo &getFixupKindInfo(MCFixupKind Kind) const override;
67 
getNumFixupKinds() const68   unsigned getNumFixupKinds() const override {
69     return Lanai::NumTargetFixupKinds;
70   }
71 
72   bool writeNopData(raw_ostream &OS, uint64_t Count,
73                     const MCSubtargetInfo *STI) const override;
74 };
75 
writeNopData(raw_ostream & OS,uint64_t Count,const MCSubtargetInfo * STI) const76 bool LanaiAsmBackend::writeNopData(raw_ostream &OS, uint64_t Count,
77                                    const MCSubtargetInfo *STI) const {
78   if ((Count % 4) != 0)
79     return false;
80 
81   for (uint64_t i = 0; i < Count; i += 4)
82     OS.write("\x15\0\0\0", 4);
83 
84   return true;
85 }
86 
applyFixup(const MCAssembler & Asm,const MCFixup & Fixup,const MCValue & Target,MutableArrayRef<char> Data,uint64_t Value,bool,const MCSubtargetInfo *) const87 void LanaiAsmBackend::applyFixup(const MCAssembler &Asm, const MCFixup &Fixup,
88                                  const MCValue &Target,
89                                  MutableArrayRef<char> Data, uint64_t Value,
90                                  bool /*IsResolved*/,
91                                  const MCSubtargetInfo * /*STI*/) const {
92   MCFixupKind Kind = Fixup.getKind();
93   Value = adjustFixupValue(static_cast<unsigned>(Kind), Value);
94 
95   if (!Value)
96     return; // This value doesn't change the encoding
97 
98   // Where in the object and where the number of bytes that need
99   // fixing up
100   unsigned Offset = Fixup.getOffset();
101   unsigned NumBytes = (getFixupKindInfo(Kind).TargetSize + 7) / 8;
102   unsigned FullSize = 4;
103 
104   // Grab current value, if any, from bits.
105   uint64_t CurVal = 0;
106 
107   // Load instruction and apply value
108   for (unsigned i = 0; i != NumBytes; ++i) {
109     unsigned Idx = (FullSize - 1 - i);
110     CurVal |= static_cast<uint64_t>(static_cast<uint8_t>(Data[Offset + Idx]))
111               << (i * 8);
112   }
113 
114   uint64_t Mask =
115       (static_cast<uint64_t>(-1) >> (64 - getFixupKindInfo(Kind).TargetSize));
116   CurVal |= Value & Mask;
117 
118   // Write out the fixed up bytes back to the code/data bits.
119   for (unsigned i = 0; i != NumBytes; ++i) {
120     unsigned Idx = (FullSize - 1 - i);
121     Data[Offset + Idx] = static_cast<uint8_t>((CurVal >> (i * 8)) & 0xff);
122   }
123 }
124 
125 std::unique_ptr<MCObjectTargetWriter>
createObjectTargetWriter() const126 LanaiAsmBackend::createObjectTargetWriter() const {
127   return createLanaiELFObjectWriter(MCELFObjectTargetWriter::getOSABI(OSType));
128 }
129 
130 const MCFixupKindInfo &
getFixupKindInfo(MCFixupKind Kind) const131 LanaiAsmBackend::getFixupKindInfo(MCFixupKind Kind) const {
132   static const MCFixupKindInfo Infos[Lanai::NumTargetFixupKinds] = {
133       // This table *must* be in same the order of fixup_* kinds in
134       // LanaiFixupKinds.h.
135       // Note: The number of bits indicated here are assumed to be contiguous.
136       //   This does not hold true for LANAI_21 and LANAI_21_F which are applied
137       //   to bits 0x7cffff and 0x7cfffc, respectively. Since the 'bits' counts
138       //   here are used only for cosmetic purposes, we set the size to 16 bits
139       //   for these 21-bit relocation as llvm/lib/MC/MCAsmStreamer.cpp checks
140       //   no bits are set in the fixup range.
141       //
142       // name          offset bits flags
143       {"FIXUP_LANAI_NONE", 0, 32, 0},
144       {"FIXUP_LANAI_21", 16, 16 /*21*/, 0},
145       {"FIXUP_LANAI_21_F", 16, 16 /*21*/, 0},
146       {"FIXUP_LANAI_25", 7, 25, 0},
147       {"FIXUP_LANAI_32", 0, 32, 0},
148       {"FIXUP_LANAI_HI16", 16, 16, 0},
149       {"FIXUP_LANAI_LO16", 16, 16, 0}};
150 
151   if (Kind < FirstTargetFixupKind)
152     return MCAsmBackend::getFixupKindInfo(Kind);
153 
154   assert(unsigned(Kind - FirstTargetFixupKind) < getNumFixupKinds() &&
155          "Invalid kind!");
156   return Infos[Kind - FirstTargetFixupKind];
157 }
158 
159 } // namespace
160 
createLanaiAsmBackend(const Target & T,const MCSubtargetInfo & STI,const MCRegisterInfo &,const MCTargetOptions &)161 MCAsmBackend *llvm::createLanaiAsmBackend(const Target &T,
162                                           const MCSubtargetInfo &STI,
163                                           const MCRegisterInfo & /*MRI*/,
164                                           const MCTargetOptions & /*Options*/) {
165   const Triple &TT = STI.getTargetTriple();
166   if (!TT.isOSBinFormatELF())
167     llvm_unreachable("OS not supported");
168 
169   return new LanaiAsmBackend(T, TT.getOS());
170 }
171