1 //===-- BPFAsmBackend.cpp - BPF Assembler Backend -------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "MCTargetDesc/BPFMCTargetDesc.h"
11 #include "llvm/MC/MCAsmBackend.h"
12 #include "llvm/MC/MCAssembler.h"
13 #include "llvm/MC/MCDirectives.h"
14 #include "llvm/MC/MCELFObjectWriter.h"
15 #include "llvm/MC/MCFixupKindInfo.h"
16 #include "llvm/MC/MCObjectWriter.h"
17 #include "llvm/MC/MCSubtargetInfo.h"
18 #include "llvm/MC/MCExpr.h"
19 #include "llvm/MC/MCSymbol.h"
20 #include "llvm/Support/ErrorHandling.h"
21 #include "llvm/Support/raw_ostream.h"
26 class BPFAsmBackend : public MCAsmBackend {
30 BPFAsmBackend(bool IsLittleEndian)
31 : MCAsmBackend(), IsLittleEndian(IsLittleEndian) {}
32 ~BPFAsmBackend() override {}
34 void applyFixup(const MCFixup &Fixup, char *Data, unsigned DataSize,
35 uint64_t Value, bool IsPCRel) const override;
37 MCObjectWriter *createObjectWriter(raw_pwrite_stream &OS) const override;
39 // No instruction requires relaxation
40 bool fixupNeedsRelaxation(const MCFixup &Fixup, uint64_t Value,
41 const MCRelaxableFragment *DF,
42 const MCAsmLayout &Layout) const override {
46 unsigned getNumFixupKinds() const override { return 1; }
48 bool mayNeedRelaxation(const MCInst &Inst) const override { return false; }
50 void relaxInstruction(const MCInst &Inst, MCInst &Res) const override {}
52 bool writeNopData(uint64_t Count, MCObjectWriter *OW) const override;
55 bool BPFAsmBackend::writeNopData(uint64_t Count, MCObjectWriter *OW) const {
59 for (uint64_t i = 0; i < Count; i += 8)
60 OW->write64(0x15000000);
65 void BPFAsmBackend::applyFixup(const MCFixup &Fixup, char *Data,
66 unsigned DataSize, uint64_t Value,
69 if (Fixup.getKind() == FK_SecRel_4 || Fixup.getKind() == FK_SecRel_8) {
73 assert(Fixup.getKind() == FK_PCRel_2);
74 Value = (uint16_t)((Value - 8) / 8);
76 Data[Fixup.getOffset() + 2] = Value & 0xFF;
77 Data[Fixup.getOffset() + 3] = Value >> 8;
79 Data[Fixup.getOffset() + 2] = Value >> 8;
80 Data[Fixup.getOffset() + 3] = Value & 0xFF;
84 MCObjectWriter *BPFAsmBackend::createObjectWriter(raw_pwrite_stream &OS) const {
85 return createBPFELFObjectWriter(OS, 0, IsLittleEndian);
89 MCAsmBackend *llvm::createBPFAsmBackend(const Target &T,
90 const MCRegisterInfo &MRI,
91 const Triple &TT, StringRef CPU) {
92 return new BPFAsmBackend(/*IsLittleEndian=*/true);
95 MCAsmBackend *llvm::createBPFbeAsmBackend(const Target &T,
96 const MCRegisterInfo &MRI,
97 const Triple &TT, StringRef CPU) {
98 return new BPFAsmBackend(/*IsLittleEndian=*/false);