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