]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/lib/Target/BPF/MCTargetDesc/BPFAsmBackend.cpp
Merge llvm, clang, lld, lldb, compiler-rt and libc++ release_60 r321788,
[FreeBSD/FreeBSD.git] / contrib / llvm / lib / Target / BPF / MCTargetDesc / BPFAsmBackend.cpp
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
10 #include "MCTargetDesc/BPFMCTargetDesc.h"
11 #include "llvm/ADT/StringRef.h"
12 #include "llvm/MC/MCAsmBackend.h"
13 #include "llvm/MC/MCFixup.h"
14 #include "llvm/MC/MCObjectWriter.h"
15 #include <cassert>
16 #include <cstdint>
17
18 using namespace llvm;
19
20 namespace {
21
22 class BPFAsmBackend : public MCAsmBackend {
23 public:
24   bool IsLittleEndian;
25
26   BPFAsmBackend(bool IsLittleEndian)
27     : MCAsmBackend(), IsLittleEndian(IsLittleEndian) {}
28   ~BPFAsmBackend() override = default;
29
30   void applyFixup(const MCAssembler &Asm, const MCFixup &Fixup,
31                   const MCValue &Target, MutableArrayRef<char> Data,
32                   uint64_t Value, bool IsResolved) const override;
33
34   std::unique_ptr<MCObjectWriter>
35   createObjectWriter(raw_pwrite_stream &OS) const override;
36
37   // No instruction requires relaxation
38   bool fixupNeedsRelaxation(const MCFixup &Fixup, uint64_t Value,
39                             const MCRelaxableFragment *DF,
40                             const MCAsmLayout &Layout) const override {
41     return false;
42   }
43
44   unsigned getNumFixupKinds() const override { return 1; }
45
46   bool mayNeedRelaxation(const MCInst &Inst) const override { return false; }
47
48   void relaxInstruction(const MCInst &Inst, const MCSubtargetInfo &STI,
49                         MCInst &Res) const override {}
50
51   bool writeNopData(uint64_t Count, MCObjectWriter *OW) const override;
52 };
53
54 } // end anonymous namespace
55
56 bool BPFAsmBackend::writeNopData(uint64_t Count, MCObjectWriter *OW) const {
57   if ((Count % 8) != 0)
58     return false;
59
60   for (uint64_t i = 0; i < Count; i += 8)
61     OW->write64(0x15000000);
62
63   return true;
64 }
65
66 void BPFAsmBackend::applyFixup(const MCAssembler &Asm, const MCFixup &Fixup,
67                                const MCValue &Target,
68                                MutableArrayRef<char> Data, uint64_t Value,
69                                bool IsResolved) const {
70   if (Fixup.getKind() == FK_SecRel_4 || Fixup.getKind() == FK_SecRel_8) {
71     assert(Value == 0);
72   } else if (Fixup.getKind() == FK_Data_4 || Fixup.getKind() == FK_Data_8) {
73     unsigned Size = Fixup.getKind() == FK_Data_4 ? 4 : 8;
74
75     for (unsigned i = 0; i != Size; ++i) {
76       unsigned Idx = IsLittleEndian ? i : Size - i - 1;
77       Data[Fixup.getOffset() + Idx] = uint8_t(Value >> (i * 8));
78     }
79   } else if (Fixup.getKind() == FK_PCRel_4) {
80     Value = (uint32_t)((Value - 8) / 8);
81     if (IsLittleEndian) {
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     Value = (uint16_t)((Value - 8) / 8);
91     if (IsLittleEndian) {
92       Data[Fixup.getOffset() + 2] = Value & 0xFF;
93       Data[Fixup.getOffset() + 3] = Value >> 8;
94     } else {
95       Data[Fixup.getOffset() + 2] = Value >> 8;
96       Data[Fixup.getOffset() + 3] = Value & 0xFF;
97     }
98   }
99 }
100
101 std::unique_ptr<MCObjectWriter>
102 BPFAsmBackend::createObjectWriter(raw_pwrite_stream &OS) const {
103   return createBPFELFObjectWriter(OS, 0, IsLittleEndian);
104 }
105
106 MCAsmBackend *llvm::createBPFAsmBackend(const Target &T,
107                                         const MCSubtargetInfo &STI,
108                                         const MCRegisterInfo &MRI,
109                                         const MCTargetOptions &) {
110   return new BPFAsmBackend(/*IsLittleEndian=*/true);
111 }
112
113 MCAsmBackend *llvm::createBPFbeAsmBackend(const Target &T,
114                                           const MCSubtargetInfo &STI,
115                                           const MCRegisterInfo &MRI,
116                                           const MCTargetOptions &) {
117   return new BPFAsmBackend(/*IsLittleEndian=*/false);
118 }