LLVM  3.7.0
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/MC/MCAsmBackend.h"
12 #include "llvm/MC/MCAssembler.h"
13 #include "llvm/MC/MCDirectives.h"
16 #include "llvm/MC/MCObjectWriter.h"
18 #include "llvm/MC/MCExpr.h"
19 #include "llvm/MC/MCSymbol.h"
22 
23 using namespace llvm;
24 
25 namespace {
26 class BPFAsmBackend : public MCAsmBackend {
27 public:
28  bool IsLittleEndian;
29 
30  BPFAsmBackend(bool IsLittleEndian)
31  : MCAsmBackend(), IsLittleEndian(IsLittleEndian) {}
32  ~BPFAsmBackend() override {}
33 
34  void applyFixup(const MCFixup &Fixup, char *Data, unsigned DataSize,
35  uint64_t Value, bool IsPCRel) const override;
36 
37  MCObjectWriter *createObjectWriter(raw_pwrite_stream &OS) const override;
38 
39  // No instruction requires relaxation
40  bool fixupNeedsRelaxation(const MCFixup &Fixup, uint64_t Value,
41  const MCRelaxableFragment *DF,
42  const MCAsmLayout &Layout) const override {
43  return false;
44  }
45 
46  unsigned getNumFixupKinds() const override { return 1; }
47 
48  bool mayNeedRelaxation(const MCInst &Inst) const override { return false; }
49 
50  void relaxInstruction(const MCInst &Inst, MCInst &Res) const override {}
51 
52  bool writeNopData(uint64_t Count, MCObjectWriter *OW) const override;
53 };
54 
55 bool BPFAsmBackend::writeNopData(uint64_t Count, MCObjectWriter *OW) const {
56  if ((Count % 8) != 0)
57  return false;
58 
59  for (uint64_t i = 0; i < Count; i += 8)
60  OW->write64(0x15000000);
61 
62  return true;
63 }
64 
65 void BPFAsmBackend::applyFixup(const MCFixup &Fixup, char *Data,
66  unsigned DataSize, uint64_t Value,
67  bool IsPCRel) const {
68 
69  if (Fixup.getKind() == FK_SecRel_4 || Fixup.getKind() == FK_SecRel_8) {
70  assert(Value == 0);
71  return;
72  }
73  assert(Fixup.getKind() == FK_PCRel_2);
74  Value = (uint16_t)((Value - 8) / 8);
75  if (IsLittleEndian) {
76  Data[Fixup.getOffset() + 2] = Value & 0xFF;
77  Data[Fixup.getOffset() + 3] = Value >> 8;
78  } else {
79  Data[Fixup.getOffset() + 2] = Value >> 8;
80  Data[Fixup.getOffset() + 3] = Value & 0xFF;
81  }
82 }
83 
84 MCObjectWriter *BPFAsmBackend::createObjectWriter(raw_pwrite_stream &OS) const {
85  return createBPFELFObjectWriter(OS, 0, IsLittleEndian);
86 }
87 }
88 
90  const MCRegisterInfo &MRI,
91  const Triple &TT, StringRef CPU) {
92  return new BPFAsmBackend(/*IsLittleEndian=*/true);
93 }
94 
96  const MCRegisterInfo &MRI,
97  const Triple &TT, StringRef CPU) {
98  return new BPFAsmBackend(/*IsLittleEndian=*/false);
99 }
void write64(uint64_t Value)
MCObjectWriter * createBPFELFObjectWriter(raw_pwrite_stream &OS, uint8_t OSABI, bool IsLittleEndian)
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:62
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:38
uint32_t getOffset() const
Definition: MCFixup.h:91
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:150
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: MCAssembler.h:259
MCFixupKind getKind() const
Definition: MCFixup.h:89
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:44
MCAsmBackend * createBPFAsmBackend(const Target &T, const MCRegisterInfo &MRI, const Triple &TT, StringRef CPU)
PowerPC TLS Dynamic Call Fixup
A two-byte pc relative fixup.
Definition: MCFixup.h:29
Target - Wrapper for Target specific information.
MCAsmBackend * createBPFbeAsmBackend(const Target &T, const MCRegisterInfo &MRI, const Triple &TT, StringRef CPU)
A eight-byte section relative fixup.
Definition: MCFixup.h:39
An abstract base class for streams implementations that also support a pwrite operation.
Definition: raw_ostream.h:321
LLVM Value Representation.
Definition: Value.h:69
Generic interface to target specific assembler backends.
Definition: MCAsmBackend.h:34
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:40