| 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 | |
| 13 | #include "MCTargetDesc/BPFMCTargetDesc.h" |
| 14 | #include "MCTargetDesc/BPFInstPrinter.h" |
| 15 | #include "MCTargetDesc/BPFMCAsmInfo.h" |
| 16 | #include "TargetInfo/BPFTargetInfo.h" |
| 17 | #include "llvm/MC/MCInstrAnalysis.h" |
| 18 | #include "llvm/MC/MCInstrInfo.h" |
| 19 | #include "llvm/MC/MCRegisterInfo.h" |
| 20 | #include "llvm/MC/MCStreamer.h" |
| 21 | #include "llvm/MC/MCSubtargetInfo.h" |
| 22 | #include "llvm/MC/TargetRegistry.h" |
| 23 | #include "llvm/Support/Compiler.h" |
| 24 | #include "llvm/TargetParser/Host.h" |
| 25 | |
| 26 | #define GET_INSTRINFO_MC_DESC |
| 27 | #define ENABLE_INSTR_PREDICATE_VERIFIER |
| 28 | #include "BPFGenInstrInfo.inc" |
| 29 | |
| 30 | #define GET_SUBTARGETINFO_MC_DESC |
| 31 | #include "BPFGenSubtargetInfo.inc" |
| 32 | |
| 33 | #define GET_REGINFO_MC_DESC |
| 34 | #include "BPFGenRegisterInfo.inc" |
| 35 | |
| 36 | using namespace llvm; |
| 37 | |
| 38 | static MCInstrInfo *createBPFMCInstrInfo() { |
| 39 | MCInstrInfo *X = new MCInstrInfo(); |
| 40 | InitBPFMCInstrInfo(II: X); |
| 41 | return X; |
| 42 | } |
| 43 | |
| 44 | static MCRegisterInfo *createBPFMCRegisterInfo(const Triple &TT) { |
| 45 | MCRegisterInfo *X = new MCRegisterInfo(); |
| 46 | InitBPFMCRegisterInfo(RI: X, RA: BPF::R11 /* RAReg doesn't exist */); |
| 47 | return X; |
| 48 | } |
| 49 | |
| 50 | static MCSubtargetInfo *createBPFMCSubtargetInfo(const Triple &TT, |
| 51 | StringRef CPU, StringRef FS) { |
| 52 | return createBPFMCSubtargetInfoImpl(TT, CPU, /*TuneCPU*/ CPU, FS); |
| 53 | } |
| 54 | |
| 55 | static MCStreamer * |
| 56 | createBPFMCStreamer(const Triple &T, MCContext &Ctx, |
| 57 | std::unique_ptr<MCAsmBackend> &&MAB, |
| 58 | std::unique_ptr<MCObjectWriter> &&OW, |
| 59 | std::unique_ptr<MCCodeEmitter> &&Emitter) { |
| 60 | return createELFStreamer(Ctx, TAB: std::move(MAB), OW: std::move(OW), |
| 61 | CE: std::move(Emitter)); |
| 62 | } |
| 63 | |
| 64 | static MCInstPrinter *createBPFMCInstPrinter(const Triple &T, |
| 65 | unsigned SyntaxVariant, |
| 66 | const MCAsmInfo &MAI, |
| 67 | const MCInstrInfo &MII, |
| 68 | const MCRegisterInfo &MRI) { |
| 69 | if (SyntaxVariant == 0) |
| 70 | return new BPFInstPrinter(MAI, MII, MRI); |
| 71 | return nullptr; |
| 72 | } |
| 73 | |
| 74 | namespace { |
| 75 | |
| 76 | class BPFMCInstrAnalysis : public MCInstrAnalysis { |
| 77 | public: |
| 78 | explicit BPFMCInstrAnalysis(const MCInstrInfo *Info) |
| 79 | : MCInstrAnalysis(Info) {} |
| 80 | |
| 81 | bool evaluateBranch(const MCInst &Inst, uint64_t Addr, uint64_t Size, |
| 82 | uint64_t &Target) const override { |
| 83 | // The target is the 3rd operand of cond inst and the 1st of uncond inst. |
| 84 | int32_t Imm; |
| 85 | if (isConditionalBranch(Inst)) { |
| 86 | if (Inst.getOpcode() == BPF::JCOND || |
| 87 | Inst.getOpcode() == BPF::UNWIND_GOTO) |
| 88 | Imm = (short)Inst.getOperand(i: 0).getImm(); |
| 89 | else |
| 90 | Imm = (short)Inst.getOperand(i: 2).getImm(); |
| 91 | } else if (isUnconditionalBranch(Inst)) { |
| 92 | if (Inst.getOpcode() == BPF::JMP) |
| 93 | Imm = (short)Inst.getOperand(i: 0).getImm(); |
| 94 | else |
| 95 | Imm = (int)Inst.getOperand(i: 0).getImm(); |
| 96 | } else |
| 97 | return false; |
| 98 | |
| 99 | Target = Addr + Size + Imm * Size; |
| 100 | return true; |
| 101 | } |
| 102 | }; |
| 103 | |
| 104 | } // end anonymous namespace |
| 105 | |
| 106 | static MCTargetStreamer *createNullTargetStreamer(MCStreamer &S) { |
| 107 | return new MCTargetStreamer(S); |
| 108 | } |
| 109 | |
| 110 | static MCInstrAnalysis *createBPFInstrAnalysis(const MCInstrInfo *Info) { |
| 111 | return new BPFMCInstrAnalysis(Info); |
| 112 | } |
| 113 | |
| 114 | extern "C" LLVM_ABI LLVM_EXTERNAL_VISIBILITY void LLVMInitializeBPFTargetMC() { |
| 115 | for (Target *T : |
| 116 | {&getTheBPFleTarget(), &getTheBPFbeTarget(), &getTheBPFTarget()}) { |
| 117 | // Register the MC asm info. |
| 118 | RegisterMCAsmInfo<BPFMCAsmInfo> X(*T); |
| 119 | |
| 120 | // Register the MC instruction info. |
| 121 | TargetRegistry::RegisterMCInstrInfo(T&: *T, Fn: createBPFMCInstrInfo); |
| 122 | |
| 123 | // Register the MC register info. |
| 124 | TargetRegistry::RegisterMCRegInfo(T&: *T, Fn: createBPFMCRegisterInfo); |
| 125 | |
| 126 | // Register the MC subtarget info. |
| 127 | TargetRegistry::RegisterMCSubtargetInfo(T&: *T, |
| 128 | Fn: createBPFMCSubtargetInfo); |
| 129 | |
| 130 | // Register the object streamer |
| 131 | TargetRegistry::RegisterELFStreamer(T&: *T, Fn: createBPFMCStreamer); |
| 132 | |
| 133 | TargetRegistry::RegisterNullTargetStreamer(T&: *T, Fn: createNullTargetStreamer); |
| 134 | |
| 135 | // Register the MCInstPrinter. |
| 136 | TargetRegistry::RegisterMCInstPrinter(T&: *T, Fn: createBPFMCInstPrinter); |
| 137 | |
| 138 | // Register the MC instruction analyzer. |
| 139 | TargetRegistry::RegisterMCInstrAnalysis(T&: *T, Fn: createBPFInstrAnalysis); |
| 140 | } |
| 141 | |
| 142 | // Register the MC code emitter |
| 143 | TargetRegistry::RegisterMCCodeEmitter(T&: getTheBPFleTarget(), |
| 144 | Fn: createBPFMCCodeEmitter); |
| 145 | TargetRegistry::RegisterMCCodeEmitter(T&: getTheBPFbeTarget(), |
| 146 | Fn: createBPFbeMCCodeEmitter); |
| 147 | |
| 148 | // Register the ASM Backend |
| 149 | TargetRegistry::RegisterMCAsmBackend(T&: getTheBPFleTarget(), |
| 150 | Fn: createBPFAsmBackend); |
| 151 | TargetRegistry::RegisterMCAsmBackend(T&: getTheBPFbeTarget(), |
| 152 | Fn: createBPFbeAsmBackend); |
| 153 | |
| 154 | if (sys::IsLittleEndianHost) { |
| 155 | TargetRegistry::RegisterMCCodeEmitter(T&: getTheBPFTarget(), |
| 156 | Fn: createBPFMCCodeEmitter); |
| 157 | TargetRegistry::RegisterMCAsmBackend(T&: getTheBPFTarget(), |
| 158 | Fn: createBPFAsmBackend); |
| 159 | } else { |
| 160 | TargetRegistry::RegisterMCCodeEmitter(T&: getTheBPFTarget(), |
| 161 | Fn: createBPFbeMCCodeEmitter); |
| 162 | TargetRegistry::RegisterMCAsmBackend(T&: getTheBPFTarget(), |
| 163 | Fn: createBPFbeAsmBackend); |
| 164 | } |
| 165 | } |
| 166 | |