1 //===-- BPFTargetMachine.cpp - Define TargetMachine for BPF ---------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // Implements the info about BPF target spec.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "BPF.h"
15 #include "BPFTargetMachine.h"
16 #include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
17 #include "llvm/IR/LegacyPassManager.h"
18 #include "llvm/CodeGen/Passes.h"
19 #include "llvm/Support/FormattedStream.h"
20 #include "llvm/Support/TargetRegistry.h"
21 #include "llvm/Target/TargetOptions.h"
22 using namespace llvm;
23 
LLVMInitializeBPFTarget()24 extern "C" void LLVMInitializeBPFTarget() {
25   // Register the target.
26   RegisterTargetMachine<BPFTargetMachine> X(TheBPFleTarget);
27   RegisterTargetMachine<BPFTargetMachine> Y(TheBPFbeTarget);
28   RegisterTargetMachine<BPFTargetMachine> Z(TheBPFTarget);
29 }
30 
31 // DataLayout: little or big endian
computeDataLayout(const Triple & TT)32 static std::string computeDataLayout(const Triple &TT) {
33   if (TT.getArch() == Triple::bpfeb)
34     return "E-m:e-p:64:64-i64:64-n32:64-S128";
35   else
36     return "e-m:e-p:64:64-i64:64-n32:64-S128";
37 }
38 
BPFTargetMachine(const Target & T,const Triple & TT,StringRef CPU,StringRef FS,const TargetOptions & Options,Reloc::Model RM,CodeModel::Model CM,CodeGenOpt::Level OL)39 BPFTargetMachine::BPFTargetMachine(const Target &T, const Triple &TT,
40                                    StringRef CPU, StringRef FS,
41                                    const TargetOptions &Options,
42                                    Reloc::Model RM, CodeModel::Model CM,
43                                    CodeGenOpt::Level OL)
44     : LLVMTargetMachine(T, computeDataLayout(TT), TT, CPU, FS, Options, RM, CM,
45                         OL),
46       TLOF(make_unique<TargetLoweringObjectFileELF>()),
47       Subtarget(TT, CPU, FS, *this) {
48   initAsmInfo();
49 }
50 namespace {
51 // BPF Code Generator Pass Configuration Options.
52 class BPFPassConfig : public TargetPassConfig {
53 public:
BPFPassConfig(BPFTargetMachine * TM,PassManagerBase & PM)54   BPFPassConfig(BPFTargetMachine *TM, PassManagerBase &PM)
55       : TargetPassConfig(TM, PM) {}
56 
getBPFTargetMachine() const57   BPFTargetMachine &getBPFTargetMachine() const {
58     return getTM<BPFTargetMachine>();
59   }
60 
61   bool addInstSelector() override;
62 };
63 }
64 
createPassConfig(PassManagerBase & PM)65 TargetPassConfig *BPFTargetMachine::createPassConfig(PassManagerBase &PM) {
66   return new BPFPassConfig(this, PM);
67 }
68 
69 // Install an instruction selector pass using
70 // the ISelDag to gen BPF code.
addInstSelector()71 bool BPFPassConfig::addInstSelector() {
72   addPass(createBPFISelDag(getBPFTargetMachine()));
73 
74   return false;
75 }
76