1 //===-- BPFMCCodeEmitter.cpp - Convert BPF code to machine code -----------===// 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 // 10 // This file implements the BPFMCCodeEmitter class. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "MCTargetDesc/BPFMCTargetDesc.h" 15 #include "llvm/MC/MCCodeEmitter.h" 16 #include "llvm/MC/MCFixup.h" 17 #include "llvm/MC/MCInst.h" 18 #include "llvm/MC/MCInstrInfo.h" 19 #include "llvm/MC/MCRegisterInfo.h" 20 #include "llvm/MC/MCSubtargetInfo.h" 21 #include "llvm/MC/MCSymbol.h" 22 #include "llvm/ADT/Statistic.h" 23 #include "llvm/Support/Compiler.h" 24 #include "llvm/Support/EndianStream.h" 25 #include "llvm/Support/raw_ostream.h" 26 using namespace llvm; 27 28 #define DEBUG_TYPE "mccodeemitter" 29 30 namespace { 31 class BPFMCCodeEmitter : public MCCodeEmitter { 32 BPFMCCodeEmitter(const BPFMCCodeEmitter &) = delete; 33 void operator=(const BPFMCCodeEmitter &) = delete; 34 const MCInstrInfo &MCII LLVM_ATTRIBUTE_UNUSED; // Unused in NDEBUG builds. 35 const MCRegisterInfo &MRI; 36 bool IsLittleEndian; 37 38 public: 39 BPFMCCodeEmitter(const MCInstrInfo &mcii, const MCRegisterInfo &mri, 40 bool IsLittleEndian) 41 : MCII(mcii), MRI(mri), IsLittleEndian(IsLittleEndian) {} 42 43 ~BPFMCCodeEmitter() {} 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, raw_ostream &OS, 62 SmallVectorImpl<MCFixup> &Fixups, 63 const MCSubtargetInfo &STI) const override; 64 65 private: 66 uint64_t computeAvailableFeatures(const FeatureBitset &FB) const; 67 void verifyInstructionPredicates(const MCInst &MI, 68 uint64_t AvailableFeatures) const; 69 }; 70 } 71 72 MCCodeEmitter *llvm::createBPFMCCodeEmitter(const MCInstrInfo &MCII, 73 const MCRegisterInfo &MRI, 74 MCContext &Ctx) { 75 return new BPFMCCodeEmitter(MCII, MRI, true); 76 } 77 78 MCCodeEmitter *llvm::createBPFbeMCCodeEmitter(const MCInstrInfo &MCII, 79 const MCRegisterInfo &MRI, 80 MCContext &Ctx) { 81 return new BPFMCCodeEmitter(MCII, MRI, false); 82 } 83 84 unsigned BPFMCCodeEmitter::getMachineOpValue(const MCInst &MI, 85 const MCOperand &MO, 86 SmallVectorImpl<MCFixup> &Fixups, 87 const MCSubtargetInfo &STI) const { 88 if (MO.isReg()) 89 return MRI.getEncodingValue(MO.getReg()); 90 if (MO.isImm()) 91 return static_cast<unsigned>(MO.getImm()); 92 93 assert(MO.isExpr()); 94 95 const MCExpr *Expr = MO.getExpr(); 96 97 assert(Expr->getKind() == MCExpr::SymbolRef); 98 99 if (MI.getOpcode() == BPF::JAL) 100 // func call name 101 Fixups.push_back(MCFixup::create(0, Expr, FK_SecRel_4)); 102 else if (MI.getOpcode() == BPF::LD_imm64) 103 Fixups.push_back(MCFixup::create(0, Expr, FK_SecRel_8)); 104 else 105 // bb label 106 Fixups.push_back(MCFixup::create(0, Expr, FK_PCRel_2)); 107 108 return 0; 109 } 110 111 static uint8_t SwapBits(uint8_t Val) 112 { 113 return (Val & 0x0F) << 4 | (Val & 0xF0) >> 4; 114 } 115 116 void BPFMCCodeEmitter::encodeInstruction(const MCInst &MI, raw_ostream &OS, 117 SmallVectorImpl<MCFixup> &Fixups, 118 const MCSubtargetInfo &STI) const { 119 verifyInstructionPredicates(MI, 120 computeAvailableFeatures(STI.getFeatureBits())); 121 122 unsigned Opcode = MI.getOpcode(); 123 support::endian::Writer<support::little> LE(OS); 124 support::endian::Writer<support::big> BE(OS); 125 126 if (Opcode == BPF::LD_imm64 || Opcode == BPF::LD_pseudo) { 127 uint64_t Value = getBinaryCodeForInstr(MI, Fixups, STI); 128 LE.write<uint8_t>(Value >> 56); 129 if (IsLittleEndian) 130 LE.write<uint8_t>((Value >> 48) & 0xff); 131 else 132 LE.write<uint8_t>(SwapBits((Value >> 48) & 0xff)); 133 LE.write<uint16_t>(0); 134 if (IsLittleEndian) 135 LE.write<uint32_t>(Value & 0xffffFFFF); 136 else 137 BE.write<uint32_t>(Value & 0xffffFFFF); 138 139 const MCOperand &MO = MI.getOperand(1); 140 uint64_t Imm = MO.isImm() ? MO.getImm() : 0; 141 LE.write<uint8_t>(0); 142 LE.write<uint8_t>(0); 143 LE.write<uint16_t>(0); 144 if (IsLittleEndian) 145 LE.write<uint32_t>(Imm >> 32); 146 else 147 BE.write<uint32_t>(Imm >> 32); 148 } else { 149 // Get instruction encoding and emit it 150 uint64_t Value = getBinaryCodeForInstr(MI, Fixups, STI); 151 LE.write<uint8_t>(Value >> 56); 152 if (IsLittleEndian) { 153 LE.write<uint8_t>((Value >> 48) & 0xff); 154 LE.write<uint16_t>((Value >> 32) & 0xffff); 155 LE.write<uint32_t>(Value & 0xffffFFFF); 156 } else { 157 LE.write<uint8_t>(SwapBits((Value >> 48) & 0xff)); 158 BE.write<uint16_t>((Value >> 32) & 0xffff); 159 BE.write<uint32_t>(Value & 0xffffFFFF); 160 } 161 } 162 } 163 164 // Encode BPF Memory Operand 165 uint64_t BPFMCCodeEmitter::getMemoryOpValue(const MCInst &MI, unsigned Op, 166 SmallVectorImpl<MCFixup> &Fixups, 167 const MCSubtargetInfo &STI) const { 168 uint64_t Encoding; 169 const MCOperand Op1 = MI.getOperand(1); 170 assert(Op1.isReg() && "First operand is not register."); 171 Encoding = MRI.getEncodingValue(Op1.getReg()); 172 Encoding <<= 16; 173 MCOperand Op2 = MI.getOperand(2); 174 assert(Op2.isImm() && "Second operand is not immediate."); 175 Encoding |= Op2.getImm() & 0xffff; 176 return Encoding; 177 } 178 179 #define ENABLE_INSTR_PREDICATE_VERIFIER 180 #include "BPFGenMCCodeEmitter.inc" 181