LLVM  8.0.1
LanaiAsmBackend.cpp
Go to the documentation of this file.
1 //===-- LanaiAsmBackend.cpp - Lanai 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 "LanaiFixupKinds.h"
12 #include "llvm/MC/MCAsmBackend.h"
13 #include "llvm/MC/MCAssembler.h"
14 #include "llvm/MC/MCDirectives.h"
17 #include "llvm/MC/MCObjectWriter.h"
21 
22 using namespace llvm;
23 
24 // Prepare value for the target space
25 static unsigned adjustFixupValue(unsigned Kind, uint64_t Value) {
26  switch (Kind) {
27  case FK_Data_1:
28  case FK_Data_2:
29  case FK_Data_4:
30  case FK_Data_8:
31  return Value;
38  return Value;
39  default:
40  llvm_unreachable("Unknown fixup kind!");
41  }
42 }
43 
44 namespace {
45 class LanaiAsmBackend : public MCAsmBackend {
46  Triple::OSType OSType;
47 
48 public:
49  LanaiAsmBackend(const Target &T, Triple::OSType OST)
50  : MCAsmBackend(support::big), OSType(OST) {}
51 
52  void applyFixup(const MCAssembler &Asm, const MCFixup &Fixup,
54  uint64_t Value, bool IsResolved,
55  const MCSubtargetInfo *STI) const override;
56 
57  std::unique_ptr<MCObjectTargetWriter>
58  createObjectTargetWriter() const override;
59 
60  // No instruction requires relaxation
61  bool fixupNeedsRelaxation(const MCFixup & /*Fixup*/, uint64_t /*Value*/,
62  const MCRelaxableFragment * /*DF*/,
63  const MCAsmLayout & /*Layout*/) const override {
64  return false;
65  }
66 
67  const MCFixupKindInfo &getFixupKindInfo(MCFixupKind Kind) const override;
68 
69  unsigned getNumFixupKinds() const override {
71  }
72 
73  bool mayNeedRelaxation(const MCInst & /*Inst*/,
74  const MCSubtargetInfo &STI) const override {
75  return false;
76  }
77 
78  void relaxInstruction(const MCInst & /*Inst*/,
79  const MCSubtargetInfo & /*STI*/,
80  MCInst & /*Res*/) const override {}
81 
82  bool writeNopData(raw_ostream &OS, uint64_t Count) const override;
83 };
84 
85 bool LanaiAsmBackend::writeNopData(raw_ostream &OS, uint64_t Count) const {
86  if ((Count % 4) != 0)
87  return false;
88 
89  for (uint64_t i = 0; i < Count; i += 4)
90  OS.write("\x15\0\0\0", 4);
91 
92  return true;
93 }
94 
95 void LanaiAsmBackend::applyFixup(const MCAssembler &Asm, const MCFixup &Fixup,
96  const MCValue &Target,
98  bool /*IsResolved*/,
99  const MCSubtargetInfo * /*STI*/) const {
100  MCFixupKind Kind = Fixup.getKind();
101  Value = adjustFixupValue(static_cast<unsigned>(Kind), Value);
102 
103  if (!Value)
104  return; // This value doesn't change the encoding
105 
106  // Where in the object and where the number of bytes that need
107  // fixing up
108  unsigned Offset = Fixup.getOffset();
109  unsigned NumBytes = (getFixupKindInfo(Kind).TargetSize + 7) / 8;
110  unsigned FullSize = 4;
111 
112  // Grab current value, if any, from bits.
113  uint64_t CurVal = 0;
114 
115  // Load instruction and apply value
116  for (unsigned i = 0; i != NumBytes; ++i) {
117  unsigned Idx = (FullSize - 1 - i);
118  CurVal |= static_cast<uint64_t>(static_cast<uint8_t>(Data[Offset + Idx]))
119  << (i * 8);
120  }
121 
122  uint64_t Mask =
123  (static_cast<uint64_t>(-1) >> (64 - getFixupKindInfo(Kind).TargetSize));
124  CurVal |= Value & Mask;
125 
126  // Write out the fixed up bytes back to the code/data bits.
127  for (unsigned i = 0; i != NumBytes; ++i) {
128  unsigned Idx = (FullSize - 1 - i);
129  Data[Offset + Idx] = static_cast<uint8_t>((CurVal >> (i * 8)) & 0xff);
130  }
131 }
132 
133 std::unique_ptr<MCObjectTargetWriter>
134 LanaiAsmBackend::createObjectTargetWriter() const {
136 }
137 
138 const MCFixupKindInfo &
139 LanaiAsmBackend::getFixupKindInfo(MCFixupKind Kind) const {
140  static const MCFixupKindInfo Infos[Lanai::NumTargetFixupKinds] = {
141  // This table *must* be in same the order of fixup_* kinds in
142  // LanaiFixupKinds.h.
143  // Note: The number of bits indicated here are assumed to be contiguous.
144  // This does not hold true for LANAI_21 and LANAI_21_F which are applied
145  // to bits 0x7cffff and 0x7cfffc, respectively. Since the 'bits' counts
146  // here are used only for cosmetic purposes, we set the size to 16 bits
147  // for these 21-bit relocation as llvm/lib/MC/MCAsmStreamer.cpp checks
148  // no bits are set in the fixup range.
149  //
150  // name offset bits flags
151  {"FIXUP_LANAI_NONE", 0, 32, 0},
152  {"FIXUP_LANAI_21", 16, 16 /*21*/, 0},
153  {"FIXUP_LANAI_21_F", 16, 16 /*21*/, 0},
154  {"FIXUP_LANAI_25", 7, 25, 0},
155  {"FIXUP_LANAI_32", 0, 32, 0},
156  {"FIXUP_LANAI_HI16", 16, 16, 0},
157  {"FIXUP_LANAI_LO16", 16, 16, 0}};
158 
159  if (Kind < FirstTargetFixupKind)
160  return MCAsmBackend::getFixupKindInfo(Kind);
161 
162  assert(unsigned(Kind - FirstTargetFixupKind) < getNumFixupKinds() &&
163  "Invalid kind!");
164  return Infos[Kind - FirstTargetFixupKind];
165 }
166 
167 } // namespace
168 
170  const MCSubtargetInfo &STI,
171  const MCRegisterInfo & /*MRI*/,
172  const MCTargetOptions & /*Options*/) {
173  const Triple &TT = STI.getTargetTriple();
174  if (!TT.isOSBinFormatELF())
175  llvm_unreachable("OS not supported");
176 
177  return new LanaiAsmBackend(T, TT.getOS());
178 }
This class represents lattice values for constants.
Definition: AllocatorList.h:24
This represents an "assembler immediate".
Definition: MCValue.h:40
bool isOSBinFormatELF() const
Tests whether the OS uses the ELF binary format.
Definition: Triple.h:604
OSType getOS() const
getOS - Get the parsed operating system type of this triple.
Definition: Triple.h:299
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:74
const Triple & getTargetTriple() const
Encapsulates the layout of an assembly file at a particular point in time.
Definition: MCAsmLayout.h:29
A four-byte fixup.
Definition: MCFixup.h:26
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:161
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:271
MutableArrayRef - Represent a mutable reference to an array (0 or more elements consecutively in memo...
Definition: ArrayRef.h:291
MCFixupKind
Extensible enumeration to represent the type of a fixup.
Definition: MCFixup.h:23
std::unique_ptr< MCObjectTargetWriter > createLanaiELFObjectWriter(uint8_t OSABI)
uint32_t getOffset() const
Definition: MCFixup.h:125
#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:44
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.
const unsigned Kind
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
LLVM Value Representation.
Definition: Value.h:73
Generic interface to target specific assembler backends.
Definition: MCAsmBackend.h:42
std::underlying_type< E >::type Mask()
Get a bitmask with 1s in all places up to the high-order bit of E&#39;s largest value.
Definition: BitmaskEnum.h:81
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:123
MCAsmBackend * createLanaiAsmBackend(const Target &T, const MCSubtargetInfo &STI, const MCRegisterInfo &MRI, const MCTargetOptions &Options)