LLVM  5.0.0
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(), OSType(OST) {}
51 
52  void applyFixup(const MCAssembler &Asm, const MCFixup &Fixup,
54  uint64_t Value, bool IsResolved) const override;
55 
56  MCObjectWriter *createObjectWriter(raw_pwrite_stream &OS) const override;
57 
58  // No instruction requires relaxation
59  bool fixupNeedsRelaxation(const MCFixup & /*Fixup*/, uint64_t /*Value*/,
60  const MCRelaxableFragment * /*DF*/,
61  const MCAsmLayout & /*Layout*/) const override {
62  return false;
63  }
64 
65  const MCFixupKindInfo &getFixupKindInfo(MCFixupKind Kind) const override;
66 
67  unsigned getNumFixupKinds() const override {
69  }
70 
71  bool mayNeedRelaxation(const MCInst & /*Inst*/) const override {
72  return false;
73  }
74 
75  void relaxInstruction(const MCInst & /*Inst*/,
76  const MCSubtargetInfo & /*STI*/,
77  MCInst & /*Res*/) const override {}
78 
79  bool writeNopData(uint64_t Count, MCObjectWriter *OW) const override;
80 };
81 
82 bool LanaiAsmBackend::writeNopData(uint64_t Count, MCObjectWriter *OW) const {
83  if ((Count % 4) != 0)
84  return false;
85 
86  for (uint64_t i = 0; i < Count; i += 4)
87  OW->write32(0x15000000);
88 
89  return true;
90 }
91 
92 void LanaiAsmBackend::applyFixup(const MCAssembler &Asm, const MCFixup &Fixup,
93  const MCValue &Target,
95  bool /*IsResolved*/) const {
96  MCFixupKind Kind = Fixup.getKind();
97  Value = adjustFixupValue(static_cast<unsigned>(Kind), Value);
98 
99  if (!Value)
100  return; // This value doesn't change the encoding
101 
102  // Where in the object and where the number of bytes that need
103  // fixing up
104  unsigned Offset = Fixup.getOffset();
105  unsigned NumBytes = (getFixupKindInfo(Kind).TargetSize + 7) / 8;
106  unsigned FullSize = 4;
107 
108  // Grab current value, if any, from bits.
109  uint64_t CurVal = 0;
110 
111  // Load instruction and apply value
112  for (unsigned i = 0; i != NumBytes; ++i) {
113  unsigned Idx = (FullSize - 1 - i);
114  CurVal |= static_cast<uint64_t>(static_cast<uint8_t>(Data[Offset + Idx]))
115  << (i * 8);
116  }
117 
118  uint64_t Mask =
119  (static_cast<uint64_t>(-1) >> (64 - getFixupKindInfo(Kind).TargetSize));
120  CurVal |= Value & Mask;
121 
122  // Write out the fixed up bytes back to the code/data bits.
123  for (unsigned i = 0; i != NumBytes; ++i) {
124  unsigned Idx = (FullSize - 1 - i);
125  Data[Offset + Idx] = static_cast<uint8_t>((CurVal >> (i * 8)) & 0xff);
126  }
127 }
128 
130 LanaiAsmBackend::createObjectWriter(raw_pwrite_stream &OS) const {
131  return createLanaiELFObjectWriter(OS,
133 }
134 
135 const MCFixupKindInfo &
136 LanaiAsmBackend::getFixupKindInfo(MCFixupKind Kind) const {
137  static const MCFixupKindInfo Infos[Lanai::NumTargetFixupKinds] = {
138  // This table *must* be in same the order of fixup_* kinds in
139  // LanaiFixupKinds.h.
140  // Note: The number of bits indicated here are assumed to be contiguous.
141  // This does not hold true for LANAI_21 and LANAI_21_F which are applied
142  // to bits 0x7cffff and 0x7cfffc, respectively. Since the 'bits' counts
143  // here are used only for cosmetic purposes, we set the size to 16 bits
144  // for these 21-bit relocation as llvm/lib/MC/MCAsmStreamer.cpp checks
145  // no bits are set in the fixup range.
146  //
147  // name offset bits flags
148  {"FIXUP_LANAI_NONE", 0, 32, 0},
149  {"FIXUP_LANAI_21", 16, 16 /*21*/, 0},
150  {"FIXUP_LANAI_21_F", 16, 16 /*21*/, 0},
151  {"FIXUP_LANAI_25", 7, 25, 0},
152  {"FIXUP_LANAI_32", 0, 32, 0},
153  {"FIXUP_LANAI_HI16", 16, 16, 0},
154  {"FIXUP_LANAI_LO16", 16, 16, 0}};
155 
156  if (Kind < FirstTargetFixupKind)
157  return MCAsmBackend::getFixupKindInfo(Kind);
158 
159  assert(unsigned(Kind - FirstTargetFixupKind) < getNumFixupKinds() &&
160  "Invalid kind!");
161  return Infos[Kind - FirstTargetFixupKind];
162 }
163 
164 } // namespace
165 
167  const MCRegisterInfo & /*MRI*/,
168  const Triple &TT, StringRef /*CPU*/,
169  const MCTargetOptions & /*Options*/) {
170  if (!TT.isOSBinFormatELF())
171  llvm_unreachable("OS not supported");
172 
173  return new LanaiAsmBackend(T, TT.getOS());
174 }
OSType getOS() const
getOS - Get the parsed operating system type of this triple.
Definition: Triple.h:290
This represents an "assembler immediate".
Definition: MCValue.h:40
Defines the object file and target independent interfaces used by the assembler backend to write nati...
Encode information on a single operation to perform on a byte sequence (e.g., an encoded instruction)...
Definition: MCFixup.h:66
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
MCObjectWriter * createLanaiELFObjectWriter(raw_pwrite_stream &OS, uint8_t OSABI)
uint32_t getOffset() const
Definition: MCFixup.h:95
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:159
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:255
void write32(uint32_t Value)
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
MCAsmBackend * createLanaiAsmBackend(const Target &T, const MCRegisterInfo &MRI, const Triple &TheTriple, StringRef CPU, const MCTargetOptions &Options)
MCFixupKind getKind() const
Definition: MCFixup.h:93
A one-byte fixup.
Definition: MCFixup.h:24
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:44
static unsigned adjustFixupValue(unsigned Kind, uint64_t Value)
Target - Wrapper for Target specific information.
llvm_unreachable("Unknown RegBank!")
bool isOSBinFormatELF() const
Tests whether the OS uses the ELF binary format.
Definition: Triple.h:577
MCSubtargetInfo - Generic base class for all target subtargets.
A eight-byte fixup.
Definition: MCFixup.h:27
Target independent information on a fixup kind.
An abstract base class for streams implementations that also support a pwrite operation.
Definition: raw_ostream.h:333
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:35
std::underlying_type< E >::type Mask()
Get a bitmask with 1s in all places up to the high-order bit of E's largest value.
Definition: BitmaskEnum.h:81
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
virtual const MCFixupKindInfo & getFixupKindInfo(MCFixupKind Kind) const
Get information on a fixup kind.
A two-byte fixup.
Definition: MCFixup.h:25