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