• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef LIBLLVMBACKEND_MIR_COMPILER_H
17 #define LIBLLVMBACKEND_MIR_COMPILER_H
18 
19 #include <unordered_map>
20 
21 #include <llvm/IR/LegacyPassManager.h>
22 #include <llvm/Pass.h>
23 #include <llvm/Target/TargetMachine.h>
24 
25 #include "object_code/created_object_file.h"
26 
27 namespace panda::llvmbackend {
28 
29 class InsertingPassManager : public llvm::legacy::PassManager {
30 public:
31     void add(llvm::Pass *p) override;
32 
InsertBefore(llvm::AnalysisID before,llvm::Pass * pass)33     void InsertBefore(llvm::AnalysisID before, llvm::Pass *pass)
34     {
35         befores_[before] = pass;
36     }
37 
38 private:
39     std::unordered_map<llvm::AnalysisID, llvm::Pass *> befores_;
40 };
41 
42 class MIRCompiler {
43 public:
44     using PassInserterFunction = std::function<void(InsertingPassManager *manager)>;
45 
46     explicit MIRCompiler(
47         std::shared_ptr<llvm::TargetMachine> targetMachine, PassInserterFunction insertPasses,
48         CreatedObjectFile::ObjectFilePostProcessor objectFilePostProcessor = [](llvm::object::ObjectFile *) {})
targetMachine_(std::move (targetMachine))49         : targetMachine_(std::move(targetMachine)),
50           insertPasses_(std::move(insertPasses)),
51           objectFilePostProcessor_(std::move(objectFilePostProcessor))
52     {
53     }
54 
55     llvm::Expected<std::unique_ptr<CreatedObjectFile>> CompileModule(llvm::Module &module);
56 
GetTargetMachine()57     std::shared_ptr<llvm::TargetMachine> GetTargetMachine()
58     {
59         return targetMachine_;
60     }
61 
62 private:
63     std::shared_ptr<llvm::TargetMachine> targetMachine_;
64     PassInserterFunction insertPasses_;
65     CreatedObjectFile::ObjectFilePostProcessor objectFilePostProcessor_;
66 };
67 
68 }  // namespace panda::llvmbackend
69 
70 #endif  //  LIBLLVMBACKEND_MIR_COMPILER_H
71