BPFMCCodeEmitter.cpp 5.9 KB

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