BPFMCCodeEmitter.cpp 6.4 KB

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