LLVM  11.0.0git
LanaiAsmBackend.cpp
Go to the documentation of this file.
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"
11 #include "llvm/MC/MCAsmBackend.h"
12 #include "llvm/MC/MCAssembler.h"
13 #include "llvm/MC/MCDirectives.h"
16 #include "llvm/MC/MCObjectWriter.h"
20 
21 using namespace llvm;
22 
23 // Prepare value for the target space
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;
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:
48  LanaiAsmBackend(const Target &T, Triple::OSType OST)
49  : MCAsmBackend(support::big), OSType(OST) {}
50 
51  void applyFixup(const MCAssembler &Asm, const MCFixup &Fixup,
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
60  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 
68  unsigned getNumFixupKinds() const override {
70  }
71 
72  bool mayNeedRelaxation(const MCInst & /*Inst*/,
73  const MCSubtargetInfo &STI) const override {
74  return false;
75  }
76 
77  bool writeNopData(raw_ostream &OS, uint64_t Count) const override;
78 };
79 
80 bool LanaiAsmBackend::writeNopData(raw_ostream &OS, uint64_t Count) const {
81  if ((Count % 4) != 0)
82  return false;
83 
84  for (uint64_t i = 0; i < Count; i += 4)
85  OS.write("\x15\0\0\0", 4);
86 
87  return true;
88 }
89 
90 void LanaiAsmBackend::applyFixup(const MCAssembler &Asm, const MCFixup &Fixup,
91  const MCValue &Target,
93  bool /*IsResolved*/,
94  const MCSubtargetInfo * /*STI*/) const {
95  MCFixupKind Kind = Fixup.getKind();
96  Value = adjustFixupValue(static_cast<unsigned>(Kind), Value);
97 
98  if (!Value)
99  return; // This value doesn't change the encoding
100 
101  // Where in the object and where the number of bytes that need
102  // fixing up
103  unsigned Offset = Fixup.getOffset();
104  unsigned NumBytes = (getFixupKindInfo(Kind).TargetSize + 7) / 8;
105  unsigned FullSize = 4;
106 
107  // Grab current value, if any, from bits.
108  uint64_t CurVal = 0;
109 
110  // Load instruction and apply value
111  for (unsigned i = 0; i != NumBytes; ++i) {
112  unsigned Idx = (FullSize - 1 - i);
113  CurVal |= static_cast<uint64_t>(static_cast<uint8_t>(Data[Offset + Idx]))
114  << (i * 8);
115  }
116 
117  uint64_t Mask =
118  (static_cast<uint64_t>(-1) >> (64 - getFixupKindInfo(Kind).TargetSize));
119  CurVal |= Value & Mask;
120 
121  // Write out the fixed up bytes back to the code/data bits.
122  for (unsigned i = 0; i != NumBytes; ++i) {
123  unsigned Idx = (FullSize - 1 - i);
124  Data[Offset + Idx] = static_cast<uint8_t>((CurVal >> (i * 8)) & 0xff);
125  }
126 }
127 
128 std::unique_ptr<MCObjectTargetWriter>
129 LanaiAsmBackend::createObjectTargetWriter() const {
131 }
132 
133 const MCFixupKindInfo &
134 LanaiAsmBackend::getFixupKindInfo(MCFixupKind Kind) const {
135  static const MCFixupKindInfo Infos[Lanai::NumTargetFixupKinds] = {
136  // This table *must* be in same the order of fixup_* kinds in
137  // LanaiFixupKinds.h.
138  // Note: The number of bits indicated here are assumed to be contiguous.
139  // This does not hold true for LANAI_21 and LANAI_21_F which are applied
140  // to bits 0x7cffff and 0x7cfffc, respectively. Since the 'bits' counts
141  // here are used only for cosmetic purposes, we set the size to 16 bits
142  // for these 21-bit relocation as llvm/lib/MC/MCAsmStreamer.cpp checks
143  // no bits are set in the fixup range.
144  //
145  // name offset bits flags
146  {"FIXUP_LANAI_NONE", 0, 32, 0},
147  {"FIXUP_LANAI_21", 16, 16 /*21*/, 0},
148  {"FIXUP_LANAI_21_F", 16, 16 /*21*/, 0},
149  {"FIXUP_LANAI_25", 7, 25, 0},
150  {"FIXUP_LANAI_32", 0, 32, 0},
151  {"FIXUP_LANAI_HI16", 16, 16, 0},
152  {"FIXUP_LANAI_LO16", 16, 16, 0}};
153 
154  if (Kind < FirstTargetFixupKind)
155  return MCAsmBackend::getFixupKindInfo(Kind);
156 
157  assert(unsigned(Kind - FirstTargetFixupKind) < getNumFixupKinds() &&
158  "Invalid kind!");
159  return Infos[Kind - FirstTargetFixupKind];
160 }
161 
162 } // namespace
163 
165  const MCSubtargetInfo &STI,
166  const MCRegisterInfo & /*MRI*/,
167  const MCTargetOptions & /*Options*/) {
168  const Triple &TT = STI.getTargetTriple();
169  if (!TT.isOSBinFormatELF())
170  llvm_unreachable("OS not supported");
171 
172  return new LanaiAsmBackend(T, TT.getOS());
173 }
This class represents lattice values for constants.
Definition: AllocatorList.h:23
This represents an "assembler immediate".
Definition: MCValue.h:37
bool isOSBinFormatELF() const
Tests whether the OS uses the ELF binary format.
Definition: Triple.h:617
OSType getOS() const
getOS - Get the parsed operating system type of this triple.
Definition: Triple.h:309
virtual const MCFixupKindInfo & getFixupKindInfo(MCFixupKind Kind) const
Get information on a fixup kind.
Encode information on a single operation to perform on a byte sequence (e.g., an encoded instruction)...
Definition: MCFixup.h:82
const Triple & getTargetTriple() const
Encapsulates the layout of an assembly file at a particular point in time.
Definition: MCAsmLayout.h:28
std::underlying_type_t< E > Mask()
Get a bitmask with 1s in all places up to the high-order bit of E&#39;s largest value.
Definition: BitmaskEnum.h:80
A four-byte fixup.
Definition: MCFixup.h:26
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:158
MCRegisterInfo base class - We assume that the target defines a static array of MCRegisterDesc object...
A relaxable fragment holds on to its MCInst, since it may need to be relaxed during the assembler lay...
Definition: MCFragment.h:261
MutableArrayRef - Represent a mutable reference to an array (0 or more elements consecutively in memo...
Definition: ArrayRef.h:298
MCFixupKind
Extensible enumeration to represent the type of a fixup.
Definition: MCFixup.h:22
std::unique_ptr< MCObjectTargetWriter > createLanaiELFObjectWriter(uint8_t OSABI)
uint32_t getOffset() const
Definition: MCFixup.h:135
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
A one-byte fixup.
Definition: MCFixup.h:24
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:45
raw_ostream & write(unsigned char C)
PowerPC TLS Dynamic Call Fixup
static unsigned adjustFixupValue(unsigned Kind, uint64_t Value)
Target - Wrapper for Target specific information.
Generic base class for all target subtargets.
A eight-byte fixup.
Definition: MCFixup.h:27
Target independent information on a fixup kind.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
LLVM Value Representation.
Definition: Value.h:74
Generic interface to target specific assembler backends.
Definition: MCAsmBackend.h:36
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:46
A two-byte fixup.
Definition: MCFixup.h:25
MCFixupKind getKind() const
Definition: MCFixup.h:131
MCAsmBackend * createLanaiAsmBackend(const Target &T, const MCSubtargetInfo &STI, const MCRegisterInfo &MRI, const MCTargetOptions &Options)