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