BPFMCInstLower.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. //=-- BPFMCInstLower.cpp - Convert BPF MachineInstr to an MCInst ------------=//
  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 contains code to lower BPF MachineInstrs to their corresponding
  10. // MCInst records.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "BPFMCInstLower.h"
  14. #include "llvm/CodeGen/AsmPrinter.h"
  15. #include "llvm/CodeGen/MachineBasicBlock.h"
  16. #include "llvm/CodeGen/MachineInstr.h"
  17. #include "llvm/MC/MCAsmInfo.h"
  18. #include "llvm/MC/MCContext.h"
  19. #include "llvm/MC/MCExpr.h"
  20. #include "llvm/MC/MCInst.h"
  21. #include "llvm/Support/ErrorHandling.h"
  22. #include "llvm/Support/raw_ostream.h"
  23. using namespace llvm;
  24. MCSymbol *
  25. BPFMCInstLower::GetGlobalAddressSymbol(const MachineOperand &MO) const {
  26. return Printer.getSymbol(MO.getGlobal());
  27. }
  28. MCSymbol *
  29. BPFMCInstLower::GetExternalSymbolSymbol(const MachineOperand &MO) const {
  30. return Printer.GetExternalSymbolSymbol(MO.getSymbolName());
  31. }
  32. MCOperand BPFMCInstLower::LowerSymbolOperand(const MachineOperand &MO,
  33. MCSymbol *Sym) const {
  34. const MCExpr *Expr = MCSymbolRefExpr::create(Sym, Ctx);
  35. if (!MO.isJTI() && MO.getOffset())
  36. llvm_unreachable("unknown symbol op");
  37. return MCOperand::createExpr(Expr);
  38. }
  39. void BPFMCInstLower::Lower(const MachineInstr *MI, MCInst &OutMI) const {
  40. OutMI.setOpcode(MI->getOpcode());
  41. for (const MachineOperand &MO : MI->operands()) {
  42. MCOperand MCOp;
  43. switch (MO.getType()) {
  44. default:
  45. MI->print(errs());
  46. llvm_unreachable("unknown operand type");
  47. case MachineOperand::MO_Register:
  48. // Ignore all implicit register operands.
  49. if (MO.isImplicit())
  50. continue;
  51. MCOp = MCOperand::createReg(MO.getReg());
  52. break;
  53. case MachineOperand::MO_Immediate:
  54. MCOp = MCOperand::createImm(MO.getImm());
  55. break;
  56. case MachineOperand::MO_MachineBasicBlock:
  57. MCOp = MCOperand::createExpr(
  58. MCSymbolRefExpr::create(MO.getMBB()->getSymbol(), Ctx));
  59. break;
  60. case MachineOperand::MO_RegisterMask:
  61. continue;
  62. case MachineOperand::MO_ExternalSymbol:
  63. MCOp = LowerSymbolOperand(MO, GetExternalSymbolSymbol(MO));
  64. break;
  65. case MachineOperand::MO_GlobalAddress:
  66. MCOp = LowerSymbolOperand(MO, GetGlobalAddressSymbol(MO));
  67. break;
  68. }
  69. OutMI.addOperand(MCOp);
  70. }
  71. }