10b57cec5SDimitry Andric //===-- BPFMCCodeEmitter.cpp - Convert BPF code to machine code -----------===// 20b57cec5SDimitry Andric // 30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60b57cec5SDimitry Andric // 70b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 80b57cec5SDimitry Andric // 90b57cec5SDimitry Andric // This file implements the BPFMCCodeEmitter class. 100b57cec5SDimitry Andric // 110b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 120b57cec5SDimitry Andric 13*5f757f3fSDimitry Andric #include "MCTargetDesc/BPFMCFixups.h" 140b57cec5SDimitry Andric #include "MCTargetDesc/BPFMCTargetDesc.h" 150b57cec5SDimitry Andric #include "llvm/ADT/SmallVector.h" 160b57cec5SDimitry Andric #include "llvm/MC/MCCodeEmitter.h" 17e8d8bef9SDimitry Andric #include "llvm/MC/MCExpr.h" 180b57cec5SDimitry Andric #include "llvm/MC/MCFixup.h" 190b57cec5SDimitry Andric #include "llvm/MC/MCInst.h" 200b57cec5SDimitry Andric #include "llvm/MC/MCInstrInfo.h" 210b57cec5SDimitry Andric #include "llvm/MC/MCRegisterInfo.h" 220b57cec5SDimitry Andric #include "llvm/MC/MCSubtargetInfo.h" 230b57cec5SDimitry Andric #include "llvm/Support/Endian.h" 240b57cec5SDimitry Andric #include "llvm/Support/EndianStream.h" 250b57cec5SDimitry Andric #include <cassert> 260b57cec5SDimitry Andric #include <cstdint> 270b57cec5SDimitry Andric 280b57cec5SDimitry Andric using namespace llvm; 290b57cec5SDimitry Andric 300b57cec5SDimitry Andric #define DEBUG_TYPE "mccodeemitter" 310b57cec5SDimitry Andric 320b57cec5SDimitry Andric namespace { 330b57cec5SDimitry Andric 340b57cec5SDimitry Andric class BPFMCCodeEmitter : public MCCodeEmitter { 350b57cec5SDimitry Andric const MCRegisterInfo &MRI; 360b57cec5SDimitry Andric bool IsLittleEndian; 370b57cec5SDimitry Andric 380b57cec5SDimitry Andric public: 39753f127fSDimitry Andric BPFMCCodeEmitter(const MCInstrInfo &, const MCRegisterInfo &mri, 400b57cec5SDimitry Andric bool IsLittleEndian) 41753f127fSDimitry Andric : MRI(mri), IsLittleEndian(IsLittleEndian) { } 420b57cec5SDimitry Andric BPFMCCodeEmitter(const BPFMCCodeEmitter &) = delete; 430b57cec5SDimitry Andric void operator=(const BPFMCCodeEmitter &) = delete; 440b57cec5SDimitry Andric ~BPFMCCodeEmitter() override = default; 450b57cec5SDimitry Andric 460b57cec5SDimitry Andric // getBinaryCodeForInstr - TableGen'erated function for getting the 470b57cec5SDimitry Andric // binary encoding for an instruction. 480b57cec5SDimitry Andric uint64_t getBinaryCodeForInstr(const MCInst &MI, 490b57cec5SDimitry Andric SmallVectorImpl<MCFixup> &Fixups, 500b57cec5SDimitry Andric const MCSubtargetInfo &STI) const; 510b57cec5SDimitry Andric 520b57cec5SDimitry Andric // getMachineOpValue - Return binary encoding of operand. If the machin 530b57cec5SDimitry Andric // operand requires relocation, record the relocation and return zero. 540b57cec5SDimitry Andric unsigned getMachineOpValue(const MCInst &MI, const MCOperand &MO, 550b57cec5SDimitry Andric SmallVectorImpl<MCFixup> &Fixups, 560b57cec5SDimitry Andric const MCSubtargetInfo &STI) const; 570b57cec5SDimitry Andric 580b57cec5SDimitry Andric uint64_t getMemoryOpValue(const MCInst &MI, unsigned Op, 590b57cec5SDimitry Andric SmallVectorImpl<MCFixup> &Fixups, 600b57cec5SDimitry Andric const MCSubtargetInfo &STI) const; 610b57cec5SDimitry Andric 6206c3fb27SDimitry Andric void encodeInstruction(const MCInst &MI, SmallVectorImpl<char> &CB, 630b57cec5SDimitry Andric SmallVectorImpl<MCFixup> &Fixups, 640b57cec5SDimitry Andric const MCSubtargetInfo &STI) const override; 650b57cec5SDimitry Andric }; 660b57cec5SDimitry Andric 670b57cec5SDimitry Andric } // end anonymous namespace 680b57cec5SDimitry Andric 690b57cec5SDimitry Andric MCCodeEmitter *llvm::createBPFMCCodeEmitter(const MCInstrInfo &MCII, 700b57cec5SDimitry Andric MCContext &Ctx) { 7181ad6265SDimitry Andric return new BPFMCCodeEmitter(MCII, *Ctx.getRegisterInfo(), true); 720b57cec5SDimitry Andric } 730b57cec5SDimitry Andric 740b57cec5SDimitry Andric MCCodeEmitter *llvm::createBPFbeMCCodeEmitter(const MCInstrInfo &MCII, 750b57cec5SDimitry Andric MCContext &Ctx) { 7681ad6265SDimitry Andric return new BPFMCCodeEmitter(MCII, *Ctx.getRegisterInfo(), false); 770b57cec5SDimitry Andric } 780b57cec5SDimitry Andric 790b57cec5SDimitry Andric unsigned BPFMCCodeEmitter::getMachineOpValue(const MCInst &MI, 800b57cec5SDimitry Andric const MCOperand &MO, 810b57cec5SDimitry Andric SmallVectorImpl<MCFixup> &Fixups, 820b57cec5SDimitry Andric const MCSubtargetInfo &STI) const { 830b57cec5SDimitry Andric if (MO.isReg()) 840b57cec5SDimitry Andric return MRI.getEncodingValue(MO.getReg()); 850b57cec5SDimitry Andric if (MO.isImm()) 860b57cec5SDimitry Andric return static_cast<unsigned>(MO.getImm()); 870b57cec5SDimitry Andric 880b57cec5SDimitry Andric assert(MO.isExpr()); 890b57cec5SDimitry Andric 900b57cec5SDimitry Andric const MCExpr *Expr = MO.getExpr(); 910b57cec5SDimitry Andric 920b57cec5SDimitry Andric assert(Expr->getKind() == MCExpr::SymbolRef); 930b57cec5SDimitry Andric 940b57cec5SDimitry Andric if (MI.getOpcode() == BPF::JAL) 950b57cec5SDimitry Andric // func call name 960b57cec5SDimitry Andric Fixups.push_back(MCFixup::create(0, Expr, FK_PCRel_4)); 970b57cec5SDimitry Andric else if (MI.getOpcode() == BPF::LD_imm64) 980b57cec5SDimitry Andric Fixups.push_back(MCFixup::create(0, Expr, FK_SecRel_8)); 99*5f757f3fSDimitry Andric else if (MI.getOpcode() == BPF::JMPL) 100*5f757f3fSDimitry Andric Fixups.push_back(MCFixup::create(0, Expr, (MCFixupKind)BPF::FK_BPF_PCRel_4)); 1010b57cec5SDimitry Andric else 1020b57cec5SDimitry Andric // bb label 1030b57cec5SDimitry Andric Fixups.push_back(MCFixup::create(0, Expr, FK_PCRel_2)); 1040b57cec5SDimitry Andric 1050b57cec5SDimitry Andric return 0; 1060b57cec5SDimitry Andric } 1070b57cec5SDimitry Andric 1080b57cec5SDimitry Andric static uint8_t SwapBits(uint8_t Val) 1090b57cec5SDimitry Andric { 1100b57cec5SDimitry Andric return (Val & 0x0F) << 4 | (Val & 0xF0) >> 4; 1110b57cec5SDimitry Andric } 1120b57cec5SDimitry Andric 11306c3fb27SDimitry Andric void BPFMCCodeEmitter::encodeInstruction(const MCInst &MI, 11406c3fb27SDimitry Andric SmallVectorImpl<char> &CB, 1150b57cec5SDimitry Andric SmallVectorImpl<MCFixup> &Fixups, 1160b57cec5SDimitry Andric const MCSubtargetInfo &STI) const { 1170b57cec5SDimitry Andric unsigned Opcode = MI.getOpcode(); 11806c3fb27SDimitry Andric raw_svector_ostream OS(CB); 119*5f757f3fSDimitry Andric support::endian::Writer OSE(OS, IsLittleEndian ? llvm::endianness::little 120*5f757f3fSDimitry Andric : llvm::endianness::big); 1210b57cec5SDimitry Andric 1220b57cec5SDimitry Andric if (Opcode == BPF::LD_imm64 || Opcode == BPF::LD_pseudo) { 1230b57cec5SDimitry Andric uint64_t Value = getBinaryCodeForInstr(MI, Fixups, STI); 12406c3fb27SDimitry Andric CB.push_back(Value >> 56); 1250b57cec5SDimitry Andric if (IsLittleEndian) 12606c3fb27SDimitry Andric CB.push_back((Value >> 48) & 0xff); 1270b57cec5SDimitry Andric else 12806c3fb27SDimitry Andric CB.push_back(SwapBits((Value >> 48) & 0xff)); 1290b57cec5SDimitry Andric OSE.write<uint16_t>(0); 1300b57cec5SDimitry Andric OSE.write<uint32_t>(Value & 0xffffFFFF); 1310b57cec5SDimitry Andric 1320b57cec5SDimitry Andric const MCOperand &MO = MI.getOperand(1); 1330b57cec5SDimitry Andric uint64_t Imm = MO.isImm() ? MO.getImm() : 0; 1340b57cec5SDimitry Andric OSE.write<uint8_t>(0); 1350b57cec5SDimitry Andric OSE.write<uint8_t>(0); 1360b57cec5SDimitry Andric OSE.write<uint16_t>(0); 1370b57cec5SDimitry Andric OSE.write<uint32_t>(Imm >> 32); 1380b57cec5SDimitry Andric } else { 1390b57cec5SDimitry Andric // Get instruction encoding and emit it 1400b57cec5SDimitry Andric uint64_t Value = getBinaryCodeForInstr(MI, Fixups, STI); 14106c3fb27SDimitry Andric CB.push_back(Value >> 56); 1420b57cec5SDimitry Andric if (IsLittleEndian) 14306c3fb27SDimitry Andric CB.push_back(char((Value >> 48) & 0xff)); 1440b57cec5SDimitry Andric else 14506c3fb27SDimitry Andric CB.push_back(SwapBits((Value >> 48) & 0xff)); 1460b57cec5SDimitry Andric OSE.write<uint16_t>((Value >> 32) & 0xffff); 1470b57cec5SDimitry Andric OSE.write<uint32_t>(Value & 0xffffFFFF); 1480b57cec5SDimitry Andric } 1490b57cec5SDimitry Andric } 1500b57cec5SDimitry Andric 1510b57cec5SDimitry Andric // Encode BPF Memory Operand 1520b57cec5SDimitry Andric uint64_t BPFMCCodeEmitter::getMemoryOpValue(const MCInst &MI, unsigned Op, 1530b57cec5SDimitry Andric SmallVectorImpl<MCFixup> &Fixups, 1540b57cec5SDimitry Andric const MCSubtargetInfo &STI) const { 155e8d8bef9SDimitry Andric // For CMPXCHG instructions, output is implicitly in R0/W0, 156e8d8bef9SDimitry Andric // so memory operand starts from operand 0. 157e8d8bef9SDimitry Andric int MemOpStartIndex = 1, Opcode = MI.getOpcode(); 158e8d8bef9SDimitry Andric if (Opcode == BPF::CMPXCHGW32 || Opcode == BPF::CMPXCHGD) 159e8d8bef9SDimitry Andric MemOpStartIndex = 0; 160e8d8bef9SDimitry Andric 1610b57cec5SDimitry Andric uint64_t Encoding; 162e8d8bef9SDimitry Andric const MCOperand Op1 = MI.getOperand(MemOpStartIndex); 1630b57cec5SDimitry Andric assert(Op1.isReg() && "First operand is not register."); 1640b57cec5SDimitry Andric Encoding = MRI.getEncodingValue(Op1.getReg()); 1650b57cec5SDimitry Andric Encoding <<= 16; 166e8d8bef9SDimitry Andric MCOperand Op2 = MI.getOperand(MemOpStartIndex + 1); 1670b57cec5SDimitry Andric assert(Op2.isImm() && "Second operand is not immediate."); 1680b57cec5SDimitry Andric Encoding |= Op2.getImm() & 0xffff; 1690b57cec5SDimitry Andric return Encoding; 1700b57cec5SDimitry Andric } 1710b57cec5SDimitry Andric 1720b57cec5SDimitry Andric #include "BPFGenMCCodeEmitter.inc" 173