LLVM  8.0.1
BPFAsmBackend.cpp
Go to the documentation of this file.
1 //===-- BPFAsmBackend.cpp - BPF 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 
11 #include "llvm/ADT/StringRef.h"
12 #include "llvm/MC/MCAsmBackend.h"
13 #include "llvm/MC/MCAssembler.h"
14 #include "llvm/MC/MCContext.h"
15 #include "llvm/MC/MCFixup.h"
16 #include "llvm/MC/MCObjectWriter.h"
18 #include <cassert>
19 #include <cstdint>
20 
21 using namespace llvm;
22 
23 namespace {
24 
25 class BPFAsmBackend : public MCAsmBackend {
26 public:
27  BPFAsmBackend(support::endianness Endian) : MCAsmBackend(Endian) {}
28  ~BPFAsmBackend() override = default;
29 
30  void applyFixup(const MCAssembler &Asm, const MCFixup &Fixup,
32  uint64_t Value, bool IsResolved,
33  const MCSubtargetInfo *STI) const override;
34 
35  std::unique_ptr<MCObjectTargetWriter>
36  createObjectTargetWriter() const override;
37 
38  // No instruction requires relaxation
39  bool fixupNeedsRelaxation(const MCFixup &Fixup, uint64_t Value,
40  const MCRelaxableFragment *DF,
41  const MCAsmLayout &Layout) const override {
42  return false;
43  }
44 
45  unsigned getNumFixupKinds() const override { return 1; }
46 
47  bool mayNeedRelaxation(const MCInst &Inst,
48  const MCSubtargetInfo &STI) const override {
49  return false;
50  }
51 
52  void relaxInstruction(const MCInst &Inst, const MCSubtargetInfo &STI,
53  MCInst &Res) const override {}
54 
55  bool writeNopData(raw_ostream &OS, uint64_t Count) const override;
56 };
57 
58 } // end anonymous namespace
59 
60 bool BPFAsmBackend::writeNopData(raw_ostream &OS, uint64_t Count) const {
61  if ((Count % 8) != 0)
62  return false;
63 
64  for (uint64_t i = 0; i < Count; i += 8)
65  support::endian::write<uint64_t>(OS, 0x15000000, Endian);
66 
67  return true;
68 }
69 
70 void BPFAsmBackend::applyFixup(const MCAssembler &Asm, const MCFixup &Fixup,
71  const MCValue &Target,
73  bool IsResolved,
74  const MCSubtargetInfo *STI) const {
75  if (Fixup.getKind() == FK_SecRel_4 || Fixup.getKind() == FK_SecRel_8) {
76  if (Value) {
77  MCContext &Ctx = Asm.getContext();
78  Ctx.reportError(Fixup.getLoc(),
79  "Unsupported relocation: try to compile with -O2 or above, "
80  "or check your static variable usage");
81  }
82  } else if (Fixup.getKind() == FK_Data_4) {
83  support::endian::write<uint32_t>(&Data[Fixup.getOffset()], Value, Endian);
84  } else if (Fixup.getKind() == FK_Data_8) {
85  support::endian::write<uint64_t>(&Data[Fixup.getOffset()], Value, Endian);
86  } else if (Fixup.getKind() == FK_PCRel_4) {
87  Value = (uint32_t)((Value - 8) / 8);
88  if (Endian == support::little) {
89  Data[Fixup.getOffset() + 1] = 0x10;
90  support::endian::write32le(&Data[Fixup.getOffset() + 4], Value);
91  } else {
92  Data[Fixup.getOffset() + 1] = 0x1;
93  support::endian::write32be(&Data[Fixup.getOffset() + 4], Value);
94  }
95  } else {
96  assert(Fixup.getKind() == FK_PCRel_2);
97  Value = (uint16_t)((Value - 8) / 8);
98  support::endian::write<uint16_t>(&Data[Fixup.getOffset() + 2], Value,
99  Endian);
100  }
101 }
102 
103 std::unique_ptr<MCObjectTargetWriter>
104 BPFAsmBackend::createObjectTargetWriter() const {
105  return createBPFELFObjectWriter(0);
106 }
107 
109  const MCSubtargetInfo &STI,
110  const MCRegisterInfo &MRI,
111  const MCTargetOptions &) {
112  return new BPFAsmBackend(support::little);
113 }
114 
116  const MCSubtargetInfo &STI,
117  const MCRegisterInfo &MRI,
118  const MCTargetOptions &) {
119  return new BPFAsmBackend(support::big);
120 }
void write32be(void *P, uint32_t V)
Definition: Endian.h:407
This class represents lattice values for constants.
Definition: AllocatorList.h:24
std::unique_ptr< MCObjectTargetWriter > createBPFELFObjectWriter(uint8_t OSABI)
This represents an "assembler immediate".
Definition: MCValue.h:40
void write32le(void *P, uint32_t V)
Definition: Endian.h:404
Encode information on a single operation to perform on a byte sequence (e.g., an encoded instruction)...
Definition: MCFixup.h:74
MCContext & getContext() const
Definition: MCAssembler.h:285
Encapsulates the layout of an assembly file at a particular point in time.
Definition: MCAsmLayout.h:29
A four-byte section relative fixup.
Definition: MCFixup.h:42
A four-byte fixup.
Definition: MCFixup.h:26
Context object for machine code objects.
Definition: MCContext.h:63
MCAsmBackend * createBPFAsmBackend(const Target &T, const MCSubtargetInfo &STI, const MCRegisterInfo &MRI, const MCTargetOptions &Options)
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
unsigned const MachineRegisterInfo * MRI
MutableArrayRef - Represent a mutable reference to an array (0 or more elements consecutively in memo...
Definition: ArrayRef.h:291
void reportError(SMLoc L, const Twine &Msg)
Definition: MCContext.cpp:612
uint32_t getOffset() const
Definition: MCFixup.h:125
SMLoc getLoc() const
Definition: MCFixup.h:166
A two-byte pc relative fixup.
Definition: MCFixup.h:29
A four-byte pc relative fixup.
Definition: MCFixup.h:30
MCAsmBackend * createBPFbeAsmBackend(const Target &T, const MCSubtargetInfo &STI, const MCRegisterInfo &MRI, const MCTargetOptions &Options)
Target - Wrapper for Target specific information.
A eight-byte section relative fixup.
Definition: MCFixup.h:43
Generic base class for all target subtargets.
A eight-byte fixup.
Definition: MCFixup.h:27
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
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:46
MCFixupKind getKind() const
Definition: MCFixup.h:123