1//===-- BPFMCCodeEmitter.cpp - Convert BPF code to machine code -----------===//
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//
9// This file implements the BPFMCCodeEmitter class.
10//
11//===----------------------------------------------------------------------===//
12
13#include "MCTargetDesc/BPFMCFixups.h"
14#include "MCTargetDesc/BPFMCTargetDesc.h"
15#include "llvm/ADT/SmallVector.h"
16#include "llvm/MC/MCCodeEmitter.h"
17#include "llvm/MC/MCExpr.h"
18#include "llvm/MC/MCFixup.h"
19#include "llvm/MC/MCInst.h"
20#include "llvm/MC/MCInstrInfo.h"
21#include "llvm/MC/MCRegisterInfo.h"
22#include "llvm/MC/MCSubtargetInfo.h"
23#include "llvm/Support/Endian.h"
24#include "llvm/Support/EndianStream.h"
25#include <cassert>
26#include <cstdint>
27
28using namespace llvm;
29
30#define DEBUG_TYPE "mccodeemitter"
31
32namespace {
33
34class BPFMCCodeEmitter : public MCCodeEmitter {
35 const MCRegisterInfo &MRI;
36 bool IsLittleEndian;
37
38public:
39 BPFMCCodeEmitter(const MCInstrInfo &, const MCRegisterInfo &mri,
40 bool IsLittleEndian)
41 : MRI(mri), IsLittleEndian(IsLittleEndian) { }
42 BPFMCCodeEmitter(const BPFMCCodeEmitter &) = delete;
43 void operator=(const BPFMCCodeEmitter &) = delete;
44 ~BPFMCCodeEmitter() override = default;
45
46 // getBinaryCodeForInstr - TableGen'erated function for getting the
47 // binary encoding for an instruction.
48 uint64_t getBinaryCodeForInstr(const MCInst &MI,
49 SmallVectorImpl<MCFixup> &Fixups,
50 const MCSubtargetInfo &STI) const;
51
52 // getMachineOpValue - Return binary encoding of operand. If the machin
53 // operand requires relocation, record the relocation and return zero.
54 unsigned getMachineOpValue(const MCInst &MI, const MCOperand &MO,
55 SmallVectorImpl<MCFixup> &Fixups,
56 const MCSubtargetInfo &STI) const;
57
58 uint64_t getMemoryOpValue(const MCInst &MI, unsigned Op,
59 SmallVectorImpl<MCFixup> &Fixups,
60 const MCSubtargetInfo &STI) const;
61
62 void encodeInstruction(const MCInst &MI, SmallVectorImpl<char> &CB,
63 SmallVectorImpl<MCFixup> &Fixups,
64 const MCSubtargetInfo &STI) const override;
65};
66
67} // end anonymous namespace
68
69MCCodeEmitter *llvm::createBPFMCCodeEmitter(const MCInstrInfo &MCII,
70 MCContext &Ctx) {
71 return new BPFMCCodeEmitter(MCII, *Ctx.getRegisterInfo(), true);
72}
73
74MCCodeEmitter *llvm::createBPFbeMCCodeEmitter(const MCInstrInfo &MCII,
75 MCContext &Ctx) {
76 return new BPFMCCodeEmitter(MCII, *Ctx.getRegisterInfo(), false);
77}
78
79unsigned BPFMCCodeEmitter::getMachineOpValue(const MCInst &MI,
80 const MCOperand &MO,
81 SmallVectorImpl<MCFixup> &Fixups,
82 const MCSubtargetInfo &STI) const {
83 if (MO.isReg())
84 return MRI.getEncodingValue(RegNo: MO.getReg());
85 if (MO.isImm())
86 return static_cast<unsigned>(MO.getImm());
87
88 assert(MO.isExpr());
89
90 const MCExpr *Expr = MO.getExpr();
91
92 assert(Expr->getKind() == MCExpr::SymbolRef);
93
94 if (MI.getOpcode() == BPF::JAL)
95 // func call name
96 Fixups.push_back(Elt: MCFixup::create(Offset: 0, Value: Expr, Kind: FK_PCRel_4));
97 else if (MI.getOpcode() == BPF::LD_imm64)
98 Fixups.push_back(Elt: MCFixup::create(Offset: 0, Value: Expr, Kind: FK_SecRel_8));
99 else if (MI.getOpcode() == BPF::JMPL)
100 Fixups.push_back(Elt: MCFixup::create(Offset: 0, Value: Expr, Kind: (MCFixupKind)BPF::FK_BPF_PCRel_4));
101 else
102 // bb label
103 Fixups.push_back(Elt: MCFixup::create(Offset: 0, Value: Expr, Kind: FK_PCRel_2));
104
105 return 0;
106}
107
108static uint8_t SwapBits(uint8_t Val)
109{
110 return (Val & 0x0F) << 4 | (Val & 0xF0) >> 4;
111}
112
113void BPFMCCodeEmitter::encodeInstruction(const MCInst &MI,
114 SmallVectorImpl<char> &CB,
115 SmallVectorImpl<MCFixup> &Fixups,
116 const MCSubtargetInfo &STI) const {
117 unsigned Opcode = MI.getOpcode();
118 raw_svector_ostream OS(CB);
119 support::endian::Writer OSE(OS, IsLittleEndian ? llvm::endianness::little
120 : llvm::endianness::big);
121
122 if (Opcode == BPF::LD_imm64 || Opcode == BPF::LD_pseudo) {
123 uint64_t Value = getBinaryCodeForInstr(MI, Fixups, STI);
124 CB.push_back(Elt: Value >> 56);
125 if (IsLittleEndian)
126 CB.push_back(Elt: (Value >> 48) & 0xff);
127 else
128 CB.push_back(Elt: SwapBits(Val: (Value >> 48) & 0xff));
129 OSE.write<uint16_t>(Val: 0);
130 OSE.write<uint32_t>(Val: Value & 0xffffFFFF);
131
132 const MCOperand &MO = MI.getOperand(i: 1);
133 uint64_t Imm = MO.isImm() ? MO.getImm() : 0;
134 OSE.write<uint8_t>(Val: 0);
135 OSE.write<uint8_t>(Val: 0);
136 OSE.write<uint16_t>(Val: 0);
137 OSE.write<uint32_t>(Val: Imm >> 32);
138 } else {
139 // Get instruction encoding and emit it
140 uint64_t Value = getBinaryCodeForInstr(MI, Fixups, STI);
141 CB.push_back(Elt: Value >> 56);
142 if (IsLittleEndian)
143 CB.push_back(Elt: char((Value >> 48) & 0xff));
144 else
145 CB.push_back(Elt: SwapBits(Val: (Value >> 48) & 0xff));
146 OSE.write<uint16_t>(Val: (Value >> 32) & 0xffff);
147 OSE.write<uint32_t>(Val: Value & 0xffffFFFF);
148 }
149}
150
151// Encode BPF Memory Operand
152uint64_t BPFMCCodeEmitter::getMemoryOpValue(const MCInst &MI, unsigned Op,
153 SmallVectorImpl<MCFixup> &Fixups,
154 const MCSubtargetInfo &STI) const {
155 // For CMPXCHG instructions, output is implicitly in R0/W0,
156 // so memory operand starts from operand 0.
157 int MemOpStartIndex = 1, Opcode = MI.getOpcode();
158 if (Opcode == BPF::CMPXCHGW32 || Opcode == BPF::CMPXCHGD)
159 MemOpStartIndex = 0;
160
161 uint64_t Encoding;
162 const MCOperand Op1 = MI.getOperand(i: MemOpStartIndex);
163 assert(Op1.isReg() && "First operand is not register.");
164 Encoding = MRI.getEncodingValue(RegNo: Op1.getReg());
165 Encoding <<= 16;
166 MCOperand Op2 = MI.getOperand(i: MemOpStartIndex + 1);
167 assert(Op2.isImm() && "Second operand is not immediate.");
168 Encoding |= Op2.getImm() & 0xffff;
169 return Encoding;
170}
171
172#include "BPFGenMCCodeEmitter.inc"
173