xref: /freebsd/contrib/llvm-project/llvm/lib/Target/BPF/MCTargetDesc/BPFMCTargetDesc.cpp (revision 5ca8e32633c4ffbbcd6762e5888b6a4ba0708c6c)
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(X);
39   return X;
40 }
41 
42 static MCRegisterInfo *createBPFMCRegisterInfo(const Triple &TT) {
43   MCRegisterInfo *X = new MCRegisterInfo();
44   InitBPFMCRegisterInfo(X, 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 *createBPFMCStreamer(const Triple &T, MCContext &Ctx,
54                                        std::unique_ptr<MCAsmBackend> &&MAB,
55                                        std::unique_ptr<MCObjectWriter> &&OW,
56                                        std::unique_ptr<MCCodeEmitter> &&Emitter,
57                                        bool RelaxAll) {
58   return createELFStreamer(Ctx, std::move(MAB), std::move(OW), std::move(Emitter),
59                            RelaxAll);
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     int16_t Imm;
83     if (isConditionalBranch(Inst)) {
84       Imm = Inst.getOperand(2).getImm();
85     } else if (isUnconditionalBranch(Inst))
86       Imm = Inst.getOperand(0).getImm();
87     else
88       return false;
89 
90     Target = Addr + Size + Imm * Size;
91     return true;
92   }
93 };
94 
95 } // end anonymous namespace
96 
97 static MCInstrAnalysis *createBPFInstrAnalysis(const MCInstrInfo *Info) {
98   return new BPFMCInstrAnalysis(Info);
99 }
100 
101 extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializeBPFTargetMC() {
102   for (Target *T :
103        {&getTheBPFleTarget(), &getTheBPFbeTarget(), &getTheBPFTarget()}) {
104     // Register the MC asm info.
105     RegisterMCAsmInfo<BPFMCAsmInfo> X(*T);
106 
107     // Register the MC instruction info.
108     TargetRegistry::RegisterMCInstrInfo(*T, createBPFMCInstrInfo);
109 
110     // Register the MC register info.
111     TargetRegistry::RegisterMCRegInfo(*T, createBPFMCRegisterInfo);
112 
113     // Register the MC subtarget info.
114     TargetRegistry::RegisterMCSubtargetInfo(*T,
115                                             createBPFMCSubtargetInfo);
116 
117     // Register the object streamer
118     TargetRegistry::RegisterELFStreamer(*T, createBPFMCStreamer);
119 
120     // Register the MCInstPrinter.
121     TargetRegistry::RegisterMCInstPrinter(*T, createBPFMCInstPrinter);
122 
123     // Register the MC instruction analyzer.
124     TargetRegistry::RegisterMCInstrAnalysis(*T, createBPFInstrAnalysis);
125   }
126 
127   // Register the MC code emitter
128   TargetRegistry::RegisterMCCodeEmitter(getTheBPFleTarget(),
129                                         createBPFMCCodeEmitter);
130   TargetRegistry::RegisterMCCodeEmitter(getTheBPFbeTarget(),
131                                         createBPFbeMCCodeEmitter);
132 
133   // Register the ASM Backend
134   TargetRegistry::RegisterMCAsmBackend(getTheBPFleTarget(),
135                                        createBPFAsmBackend);
136   TargetRegistry::RegisterMCAsmBackend(getTheBPFbeTarget(),
137                                        createBPFbeAsmBackend);
138 
139   if (sys::IsLittleEndianHost) {
140     TargetRegistry::RegisterMCCodeEmitter(getTheBPFTarget(),
141                                           createBPFMCCodeEmitter);
142     TargetRegistry::RegisterMCAsmBackend(getTheBPFTarget(),
143                                          createBPFAsmBackend);
144   } else {
145     TargetRegistry::RegisterMCCodeEmitter(getTheBPFTarget(),
146                                           createBPFbeMCCodeEmitter);
147     TargetRegistry::RegisterMCAsmBackend(getTheBPFTarget(),
148                                          createBPFbeAsmBackend);
149   }
150 
151 }
152