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(TheBPFTarget);
27 }
28
29 // DataLayout --> Little-endian, 64-bit pointer/ABI/alignment
30 // The stack is always 8 byte aligned
31 // On function prologue, the stack is created by decrementing
32 // its pointer. Once decremented, all references are done with positive
33 // offset from the stack/frame pointer.
BPFTargetMachine(const Target & T,StringRef TT,StringRef CPU,StringRef FS,const TargetOptions & Options,Reloc::Model RM,CodeModel::Model CM,CodeGenOpt::Level OL)34 BPFTargetMachine::BPFTargetMachine(const Target &T, StringRef TT, StringRef CPU,
35 StringRef FS, const TargetOptions &Options,
36 Reloc::Model RM, CodeModel::Model CM,
37 CodeGenOpt::Level OL)
38 : LLVMTargetMachine(T, "e-m:e-p:64:64-i64:64-n32:64-S128", TT, CPU, FS,
39 Options, RM, CM, OL),
40 TLOF(make_unique<TargetLoweringObjectFileELF>()),
41 Subtarget(TT, CPU, FS, *this) {
42 initAsmInfo();
43 }
44 namespace {
45 // BPF Code Generator Pass Configuration Options.
46 class BPFPassConfig : public TargetPassConfig {
47 public:
BPFPassConfig(BPFTargetMachine * TM,PassManagerBase & PM)48 BPFPassConfig(BPFTargetMachine *TM, PassManagerBase &PM)
49 : TargetPassConfig(TM, PM) {}
50
getBPFTargetMachine() const51 BPFTargetMachine &getBPFTargetMachine() const {
52 return getTM<BPFTargetMachine>();
53 }
54
55 bool addInstSelector() override;
56 };
57 }
58
createPassConfig(PassManagerBase & PM)59 TargetPassConfig *BPFTargetMachine::createPassConfig(PassManagerBase &PM) {
60 return new BPFPassConfig(this, PM);
61 }
62
63 // Install an instruction selector pass using
64 // the ISelDag to gen BPF code.
addInstSelector()65 bool BPFPassConfig::addInstSelector() {
66 addPass(createBPFISelDag(getBPFTargetMachine()));
67
68 return false;
69 }
70