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/CodeGen/TargetPassConfig.h"
20 #include "llvm/Support/FormattedStream.h"
21 #include "llvm/Support/TargetRegistry.h"
22 #include "llvm/Target/TargetOptions.h"
23 using namespace llvm;
24 
25 extern "C" void LLVMInitializeBPFTarget() {
26   // Register the target.
27   RegisterTargetMachine<BPFTargetMachine> X(TheBPFleTarget);
28   RegisterTargetMachine<BPFTargetMachine> Y(TheBPFbeTarget);
29   RegisterTargetMachine<BPFTargetMachine> Z(TheBPFTarget);
30 }
31 
32 // DataLayout: little or big endian
33 static std::string computeDataLayout(const Triple &TT) {
34   if (TT.getArch() == Triple::bpfeb)
35     return "E-m:e-p:64:64-i64:64-n32:64-S128";
36   else
37     return "e-m:e-p:64:64-i64:64-n32:64-S128";
38 }
39 
40 BPFTargetMachine::BPFTargetMachine(const Target &T, const Triple &TT,
41                                    StringRef CPU, StringRef FS,
42                                    const TargetOptions &Options,
43                                    Reloc::Model RM, CodeModel::Model CM,
44                                    CodeGenOpt::Level OL)
45     : LLVMTargetMachine(T, computeDataLayout(TT), TT, CPU, FS, Options, RM, CM,
46                         OL),
47       TLOF(make_unique<TargetLoweringObjectFileELF>()),
48       Subtarget(TT, CPU, FS, *this) {
49   initAsmInfo();
50 }
51 namespace {
52 // BPF Code Generator Pass Configuration Options.
53 class BPFPassConfig : public TargetPassConfig {
54 public:
55   BPFPassConfig(BPFTargetMachine *TM, PassManagerBase &PM)
56       : TargetPassConfig(TM, PM) {}
57 
58   BPFTargetMachine &getBPFTargetMachine() const {
59     return getTM<BPFTargetMachine>();
60   }
61 
62   bool addInstSelector() override;
63 };
64 }
65 
66 TargetPassConfig *BPFTargetMachine::createPassConfig(PassManagerBase &PM) {
67   return new BPFPassConfig(this, PM);
68 }
69 
70 // Install an instruction selector pass using
71 // the ISelDag to gen BPF code.
72 bool BPFPassConfig::addInstSelector() {
73   addPass(createBPFISelDag(getBPFTargetMachine()));
74 
75   return false;
76 }
77