1 //===-- BPFMCTargetDesc.cpp - BPF Target Descriptions ---------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // This file provides BPF specific target descriptions.
11 //===----------------------------------------------------------------------===//
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/Support/Host.h"
22 #include "llvm/Support/TargetRegistry.h"
24 #define GET_INSTRINFO_MC_DESC
25 #include "BPFGenInstrInfo.inc"
27 #define GET_SUBTARGETINFO_MC_DESC
28 #include "BPFGenSubtargetInfo.inc"
30 #define GET_REGINFO_MC_DESC
31 #include "BPFGenRegisterInfo.inc"
35 static MCInstrInfo
*createBPFMCInstrInfo() {
36 MCInstrInfo
*X
= new MCInstrInfo();
37 InitBPFMCInstrInfo(X
);
41 static MCRegisterInfo
*createBPFMCRegisterInfo(const Triple
&TT
) {
42 MCRegisterInfo
*X
= new MCRegisterInfo();
43 InitBPFMCRegisterInfo(X
, BPF::R11
/* RAReg doesn't exist */);
47 static MCSubtargetInfo
*createBPFMCSubtargetInfo(const Triple
&TT
,
48 StringRef CPU
, StringRef FS
) {
49 return createBPFMCSubtargetInfoImpl(TT
, CPU
, FS
);
52 static MCStreamer
*createBPFMCStreamer(const Triple
&T
, MCContext
&Ctx
,
53 std::unique_ptr
<MCAsmBackend
> &&MAB
,
54 std::unique_ptr
<MCObjectWriter
> &&OW
,
55 std::unique_ptr
<MCCodeEmitter
> &&Emitter
,
57 return createELFStreamer(Ctx
, std::move(MAB
), std::move(OW
), std::move(Emitter
),
61 static MCInstPrinter
*createBPFMCInstPrinter(const Triple
&T
,
62 unsigned SyntaxVariant
,
64 const MCInstrInfo
&MII
,
65 const MCRegisterInfo
&MRI
) {
66 if (SyntaxVariant
== 0)
67 return new BPFInstPrinter(MAI
, MII
, MRI
);
73 class BPFMCInstrAnalysis
: public MCInstrAnalysis
{
75 explicit BPFMCInstrAnalysis(const MCInstrInfo
*Info
)
76 : MCInstrAnalysis(Info
) {}
78 bool evaluateBranch(const MCInst
&Inst
, uint64_t Addr
, uint64_t Size
,
79 uint64_t &Target
) const override
{
80 // The target is the 3rd operand of cond inst and the 1st of uncond inst.
82 if (isConditionalBranch(Inst
)) {
83 Imm
= Inst
.getOperand(2).getImm();
84 } else if (isUnconditionalBranch(Inst
))
85 Imm
= Inst
.getOperand(0).getImm();
89 Target
= Addr
+ Size
+ Imm
* Size
;
94 } // end anonymous namespace
96 static MCInstrAnalysis
*createBPFInstrAnalysis(const MCInstrInfo
*Info
) {
97 return new BPFMCInstrAnalysis(Info
);
100 extern "C" void LLVMInitializeBPFTargetMC() {
102 {&getTheBPFleTarget(), &getTheBPFbeTarget(), &getTheBPFTarget()}) {
103 // Register the MC asm info.
104 RegisterMCAsmInfo
<BPFMCAsmInfo
> X(*T
);
106 // Register the MC instruction info.
107 TargetRegistry::RegisterMCInstrInfo(*T
, createBPFMCInstrInfo
);
109 // Register the MC register info.
110 TargetRegistry::RegisterMCRegInfo(*T
, createBPFMCRegisterInfo
);
112 // Register the MC subtarget info.
113 TargetRegistry::RegisterMCSubtargetInfo(*T
,
114 createBPFMCSubtargetInfo
);
116 // Register the object streamer
117 TargetRegistry::RegisterELFStreamer(*T
, createBPFMCStreamer
);
119 // Register the MCInstPrinter.
120 TargetRegistry::RegisterMCInstPrinter(*T
, createBPFMCInstPrinter
);
122 // Register the MC instruction analyzer.
123 TargetRegistry::RegisterMCInstrAnalysis(*T
, createBPFInstrAnalysis
);
126 // Register the MC code emitter
127 TargetRegistry::RegisterMCCodeEmitter(getTheBPFleTarget(),
128 createBPFMCCodeEmitter
);
129 TargetRegistry::RegisterMCCodeEmitter(getTheBPFbeTarget(),
130 createBPFbeMCCodeEmitter
);
132 // Register the ASM Backend
133 TargetRegistry::RegisterMCAsmBackend(getTheBPFleTarget(),
134 createBPFAsmBackend
);
135 TargetRegistry::RegisterMCAsmBackend(getTheBPFbeTarget(),
136 createBPFbeAsmBackend
);
138 if (sys::IsLittleEndianHost
) {
139 TargetRegistry::RegisterMCCodeEmitter(getTheBPFTarget(),
140 createBPFMCCodeEmitter
);
141 TargetRegistry::RegisterMCAsmBackend(getTheBPFTarget(),
142 createBPFAsmBackend
);
144 TargetRegistry::RegisterMCCodeEmitter(getTheBPFTarget(),
145 createBPFbeMCCodeEmitter
);
146 TargetRegistry::RegisterMCAsmBackend(getTheBPFTarget(),
147 createBPFbeAsmBackend
);