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
LLVMInitializeBPFTarget()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
computeDataLayout(const Triple & TT)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
getEffectiveRelocModel(Optional<Reloc::Model> RM)40 static Reloc::Model getEffectiveRelocModel(Optional<Reloc::Model> RM) {
41 if (!RM.hasValue())
42 return Reloc::PIC_;
43 return *RM;
44 }
45
BPFTargetMachine(const Target & T,const Triple & TT,StringRef CPU,StringRef FS,const TargetOptions & Options,Optional<Reloc::Model> RM,CodeModel::Model CM,CodeGenOpt::Level OL)46 BPFTargetMachine::BPFTargetMachine(const Target &T, const Triple &TT,
47 StringRef CPU, StringRef FS,
48 const TargetOptions &Options,
49 Optional<Reloc::Model> RM,
50 CodeModel::Model CM, CodeGenOpt::Level OL)
51 : LLVMTargetMachine(T, computeDataLayout(TT), TT, CPU, FS, Options,
52 getEffectiveRelocModel(RM), CM, OL),
53 TLOF(make_unique<TargetLoweringObjectFileELF>()),
54 Subtarget(TT, CPU, FS, *this) {
55 initAsmInfo();
56 }
57 namespace {
58 // BPF Code Generator Pass Configuration Options.
59 class BPFPassConfig : public TargetPassConfig {
60 public:
BPFPassConfig(BPFTargetMachine * TM,PassManagerBase & PM)61 BPFPassConfig(BPFTargetMachine *TM, PassManagerBase &PM)
62 : TargetPassConfig(TM, PM) {}
63
getBPFTargetMachine() const64 BPFTargetMachine &getBPFTargetMachine() const {
65 return getTM<BPFTargetMachine>();
66 }
67
68 bool addInstSelector() override;
69 };
70 }
71
createPassConfig(PassManagerBase & PM)72 TargetPassConfig *BPFTargetMachine::createPassConfig(PassManagerBase &PM) {
73 return new BPFPassConfig(this, PM);
74 }
75
76 // Install an instruction selector pass using
77 // the ISelDag to gen BPF code.
addInstSelector()78 bool BPFPassConfig::addInstSelector() {
79 addPass(createBPFISelDag(getBPFTargetMachine()));
80
81 return false;
82 }
83