BPFMCTargetDesc.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. //===-- BPFMCTargetDesc.cpp - BPF Target Descriptions ---------------------===//
  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 provides BPF specific target descriptions.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "MCTargetDesc/BPFMCTargetDesc.h"
  13. #include "MCTargetDesc/BPFInstPrinter.h"
  14. #include "MCTargetDesc/BPFMCAsmInfo.h"
  15. #include "TargetInfo/BPFTargetInfo.h"
  16. #include "llvm/MC/MCInstrAnalysis.h"
  17. #include "llvm/MC/MCInstrInfo.h"
  18. #include "llvm/MC/MCRegisterInfo.h"
  19. #include "llvm/MC/MCSubtargetInfo.h"
  20. #include "llvm/MC/TargetRegistry.h"
  21. #include "llvm/Support/Host.h"
  22. #define GET_INSTRINFO_MC_DESC
  23. #include "BPFGenInstrInfo.inc"
  24. #define GET_SUBTARGETINFO_MC_DESC
  25. #include "BPFGenSubtargetInfo.inc"
  26. #define GET_REGINFO_MC_DESC
  27. #include "BPFGenRegisterInfo.inc"
  28. using namespace llvm;
  29. static MCInstrInfo *createBPFMCInstrInfo() {
  30. MCInstrInfo *X = new MCInstrInfo();
  31. InitBPFMCInstrInfo(X);
  32. return X;
  33. }
  34. static MCRegisterInfo *createBPFMCRegisterInfo(const Triple &TT) {
  35. MCRegisterInfo *X = new MCRegisterInfo();
  36. InitBPFMCRegisterInfo(X, BPF::R11 /* RAReg doesn't exist */);
  37. return X;
  38. }
  39. static MCSubtargetInfo *createBPFMCSubtargetInfo(const Triple &TT,
  40. StringRef CPU, StringRef FS) {
  41. return createBPFMCSubtargetInfoImpl(TT, CPU, /*TuneCPU*/ CPU, FS);
  42. }
  43. static MCStreamer *createBPFMCStreamer(const Triple &T, MCContext &Ctx,
  44. std::unique_ptr<MCAsmBackend> &&MAB,
  45. std::unique_ptr<MCObjectWriter> &&OW,
  46. std::unique_ptr<MCCodeEmitter> &&Emitter,
  47. bool RelaxAll) {
  48. return createELFStreamer(Ctx, std::move(MAB), std::move(OW), std::move(Emitter),
  49. RelaxAll);
  50. }
  51. static MCInstPrinter *createBPFMCInstPrinter(const Triple &T,
  52. unsigned SyntaxVariant,
  53. const MCAsmInfo &MAI,
  54. const MCInstrInfo &MII,
  55. const MCRegisterInfo &MRI) {
  56. if (SyntaxVariant == 0)
  57. return new BPFInstPrinter(MAI, MII, MRI);
  58. return nullptr;
  59. }
  60. namespace {
  61. class BPFMCInstrAnalysis : public MCInstrAnalysis {
  62. public:
  63. explicit BPFMCInstrAnalysis(const MCInstrInfo *Info)
  64. : MCInstrAnalysis(Info) {}
  65. bool evaluateBranch(const MCInst &Inst, uint64_t Addr, uint64_t Size,
  66. uint64_t &Target) const override {
  67. // The target is the 3rd operand of cond inst and the 1st of uncond inst.
  68. int16_t Imm;
  69. if (isConditionalBranch(Inst)) {
  70. Imm = Inst.getOperand(2).getImm();
  71. } else if (isUnconditionalBranch(Inst))
  72. Imm = Inst.getOperand(0).getImm();
  73. else
  74. return false;
  75. Target = Addr + Size + Imm * Size;
  76. return true;
  77. }
  78. };
  79. } // end anonymous namespace
  80. static MCInstrAnalysis *createBPFInstrAnalysis(const MCInstrInfo *Info) {
  81. return new BPFMCInstrAnalysis(Info);
  82. }
  83. extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializeBPFTargetMC() {
  84. for (Target *T :
  85. {&getTheBPFleTarget(), &getTheBPFbeTarget(), &getTheBPFTarget()}) {
  86. // Register the MC asm info.
  87. RegisterMCAsmInfo<BPFMCAsmInfo> X(*T);
  88. // Register the MC instruction info.
  89. TargetRegistry::RegisterMCInstrInfo(*T, createBPFMCInstrInfo);
  90. // Register the MC register info.
  91. TargetRegistry::RegisterMCRegInfo(*T, createBPFMCRegisterInfo);
  92. // Register the MC subtarget info.
  93. TargetRegistry::RegisterMCSubtargetInfo(*T,
  94. createBPFMCSubtargetInfo);
  95. // Register the object streamer
  96. TargetRegistry::RegisterELFStreamer(*T, createBPFMCStreamer);
  97. // Register the MCInstPrinter.
  98. TargetRegistry::RegisterMCInstPrinter(*T, createBPFMCInstPrinter);
  99. // Register the MC instruction analyzer.
  100. TargetRegistry::RegisterMCInstrAnalysis(*T, createBPFInstrAnalysis);
  101. }
  102. // Register the MC code emitter
  103. TargetRegistry::RegisterMCCodeEmitter(getTheBPFleTarget(),
  104. createBPFMCCodeEmitter);
  105. TargetRegistry::RegisterMCCodeEmitter(getTheBPFbeTarget(),
  106. createBPFbeMCCodeEmitter);
  107. // Register the ASM Backend
  108. TargetRegistry::RegisterMCAsmBackend(getTheBPFleTarget(),
  109. createBPFAsmBackend);
  110. TargetRegistry::RegisterMCAsmBackend(getTheBPFbeTarget(),
  111. createBPFbeAsmBackend);
  112. if (sys::IsLittleEndianHost) {
  113. TargetRegistry::RegisterMCCodeEmitter(getTheBPFTarget(),
  114. createBPFMCCodeEmitter);
  115. TargetRegistry::RegisterMCAsmBackend(getTheBPFTarget(),
  116. createBPFAsmBackend);
  117. } else {
  118. TargetRegistry::RegisterMCCodeEmitter(getTheBPFTarget(),
  119. createBPFbeMCCodeEmitter);
  120. TargetRegistry::RegisterMCAsmBackend(getTheBPFTarget(),
  121. createBPFbeAsmBackend);
  122. }
  123. }