LLVM 17.0.0git
BPFAsmBackend.cpp
Go to the documentation of this file.
1//===-- BPFAsmBackend.cpp - BPF 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
10#include "llvm/ADT/StringRef.h"
12#include "llvm/MC/MCAssembler.h"
13#include "llvm/MC/MCContext.h"
14#include "llvm/MC/MCFixup.h"
17#include <cassert>
18#include <cstdint>
19
20using namespace llvm;
21
22namespace {
23
24class BPFAsmBackend : public MCAsmBackend {
25public:
26 BPFAsmBackend(support::endianness Endian) : MCAsmBackend(Endian) {}
27 ~BPFAsmBackend() override = default;
28
29 void applyFixup(const MCAssembler &Asm, const MCFixup &Fixup,
31 uint64_t Value, bool IsResolved,
32 const MCSubtargetInfo *STI) const override;
33
34 std::unique_ptr<MCObjectTargetWriter>
35 createObjectTargetWriter() const override;
36
37 // No instruction requires relaxation
40 const MCAsmLayout &Layout) const override {
41 return false;
42 }
43
44 unsigned getNumFixupKinds() const override { return 1; }
45
47 const MCSubtargetInfo *STI) const override;
48};
49
50} // end anonymous namespace
51
52bool BPFAsmBackend::writeNopData(raw_ostream &OS, uint64_t Count,
53 const MCSubtargetInfo *STI) const {
54 if ((Count % 8) != 0)
55 return false;
56
57 for (uint64_t i = 0; i < Count; i += 8)
58 support::endian::write<uint64_t>(OS, 0x15000000, Endian);
59
60 return true;
61}
62
63void BPFAsmBackend::applyFixup(const MCAssembler &Asm, const MCFixup &Fixup,
64 const MCValue &Target,
66 bool IsResolved,
67 const MCSubtargetInfo *STI) const {
68 if (Fixup.getKind() == FK_SecRel_8) {
69 // The Value is 0 for global variables, and the in-section offset
70 // for static variables. Write to the immediate field of the inst.
71 assert(Value <= UINT32_MAX);
72 support::endian::write<uint32_t>(&Data[Fixup.getOffset() + 4],
73 static_cast<uint32_t>(Value),
74 Endian);
75 } else if (Fixup.getKind() == FK_Data_4) {
76 support::endian::write<uint32_t>(&Data[Fixup.getOffset()], Value, Endian);
77 } else if (Fixup.getKind() == FK_Data_8) {
78 support::endian::write<uint64_t>(&Data[Fixup.getOffset()], Value, Endian);
79 } else if (Fixup.getKind() == FK_PCRel_4) {
80 Value = (uint32_t)((Value - 8) / 8);
81 if (Endian == support::little) {
82 Data[Fixup.getOffset() + 1] = 0x10;
83 support::endian::write32le(&Data[Fixup.getOffset() + 4], Value);
84 } else {
85 Data[Fixup.getOffset() + 1] = 0x1;
86 support::endian::write32be(&Data[Fixup.getOffset() + 4], Value);
87 }
88 } else {
89 assert(Fixup.getKind() == FK_PCRel_2);
90
91 int64_t ByteOff = (int64_t)Value - 8;
92 if (ByteOff > INT16_MAX * 8 || ByteOff < INT16_MIN * 8)
93 report_fatal_error("Branch target out of insn range");
94
95 Value = (uint16_t)((Value - 8) / 8);
96 support::endian::write<uint16_t>(&Data[Fixup.getOffset() + 2], Value,
97 Endian);
98 }
99}
100
101std::unique_ptr<MCObjectTargetWriter>
102BPFAsmBackend::createObjectTargetWriter() const {
103 return createBPFELFObjectWriter(0);
104}
105
107 const MCSubtargetInfo &STI,
108 const MCRegisterInfo &MRI,
109 const MCTargetOptions &) {
110 return new BPFAsmBackend(support::little);
111}
112
114 const MCSubtargetInfo &STI,
115 const MCRegisterInfo &MRI,
116 const MCTargetOptions &) {
117 return new BPFAsmBackend(support::big);
118}
unsigned const MachineRegisterInfo * MRI
static RegisterPass< DebugifyFunctionPass > DF("debugify-function", "Attach debug info to a function")
PowerPC TLS Dynamic Call Fixup
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
endianness Endian
raw_pwrite_stream & OS
Generic interface to target specific assembler backends.
Definition: MCAsmBackend.h:41
virtual bool writeNopData(raw_ostream &OS, uint64_t Count, const MCSubtargetInfo *STI) const =0
Write an (optimal) nop sequence of Count bytes to the given output.
virtual bool fixupNeedsRelaxation(const MCFixup &Fixup, uint64_t Value, const MCRelaxableFragment *DF, const MCAsmLayout &Layout) const =0
Simple predicate for targets where !Resolved implies requiring relaxation.
virtual std::unique_ptr< MCObjectTargetWriter > createObjectTargetWriter() const =0
virtual unsigned getNumFixupKinds() const =0
Get the number of target specific fixup kinds.
virtual void applyFixup(const MCAssembler &Asm, const MCFixup &Fixup, const MCValue &Target, MutableArrayRef< char > Data, uint64_t Value, bool IsResolved, const MCSubtargetInfo *STI) const =0
Apply the Value for given Fixup into the provided data fragment, at the offset specified by the fixup...
Encapsulates the layout of an assembly file at a particular point in time.
Definition: MCAsmLayout.h:28
Encode information on a single operation to perform on a byte sequence (e.g., an encoded instruction)...
Definition: MCFixup.h:71
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:270
Generic base class for all target subtargets.
This represents an "assembler immediate".
Definition: MCValue.h:36
MutableArrayRef - Represent a mutable reference to an array (0 or more elements consecutively in memo...
Definition: ArrayRef.h:305
Target - Wrapper for Target specific information.
LLVM Value Representation.
Definition: Value.h:74
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
void write32le(void *P, uint32_t V)
Definition: Endian.h:416
void write32be(void *P, uint32_t V)
Definition: Endian.h:419
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
MCAsmBackend * createBPFAsmBackend(const Target &T, const MCSubtargetInfo &STI, const MCRegisterInfo &MRI, const MCTargetOptions &Options)
MCAsmBackend * createBPFbeAsmBackend(const Target &T, const MCSubtargetInfo &STI, const MCRegisterInfo &MRI, const MCTargetOptions &Options)
void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:145
@ FK_PCRel_4
A four-byte pc relative fixup.
Definition: MCFixup.h:30
@ FK_PCRel_2
A two-byte pc relative fixup.
Definition: MCFixup.h:29
@ FK_Data_8
A eight-byte fixup.
Definition: MCFixup.h:26
@ FK_Data_4
A four-byte fixup.
Definition: MCFixup.h:25
@ FK_SecRel_8
A eight-byte section relative fixup.
Definition: MCFixup.h:43
std::unique_ptr< MCObjectTargetWriter > createBPFELFObjectWriter(uint8_t OSABI)