1 /*
2 * Copyright (c) 2022 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 #include "ecmascript/compiler/compilation_driver.h"
17
18 #include "ecmascript/compiler/file_generators.h"
19 #include "ecmascript/compiler/pgo_type/pgo_type_manager.h"
20
21 namespace panda::ecmascript::kungfu {
CompilationDriver(PGOProfilerDecoder & profilerDecoder,BytecodeInfoCollector * collector,AOTFileGenerator * fileGenerator,const std::string & fileName,const std::string & triple,LOptions * lOptions,CompilerLog * log,bool outputAsm,size_t maxMethodsInModule)22 CompilationDriver::CompilationDriver(PGOProfilerDecoder &profilerDecoder,
23 BytecodeInfoCollector *collector,
24 AOTFileGenerator *fileGenerator,
25 const std::string &fileName,
26 const std::string &triple,
27 LOptions *lOptions,
28 CompilerLog *log,
29 bool outputAsm,
30 size_t maxMethodsInModule)
31 : compilationEnv_(collector->GetCompilationEnv()),
32 jsPandaFile_(collector->GetJSPandaFile()),
33 pfDecoder_(profilerDecoder),
34 collector_(collector),
35 bytecodeInfo_(collector->GetBytecodeInfo()),
36 fileGenerator_(fileGenerator),
37 fileName_(fileName),
38 triple_(triple),
39 lOptions_(lOptions),
40 log_(log),
41 outputAsm_(outputAsm),
42 maxMethodsInModule_(maxMethodsInModule)
43 {
44 }
45
GetModule()46 Module *CompilationDriver::GetModule()
47 {
48 return IsCurModuleFull() ? fileGenerator_->AddModule(fileName_, triple_, *lOptions_, outputAsm_)
49 : fileGenerator_->GetLatestModule();
50 }
51
IncCompiledMethod()52 void CompilationDriver::IncCompiledMethod()
53 {
54 compiledMethodCnt_++;
55 }
56
IsCurModuleFull() const57 bool CompilationDriver::IsCurModuleFull() const
58 {
59 return (compiledMethodCnt_ % maxMethodsInModule_ == 0);
60 }
61
CompileModuleThenDestroyIfNeeded(bool isJitAndCodeSign)62 void CompilationDriver::CompileModuleThenDestroyIfNeeded(bool isJitAndCodeSign)
63 {
64 if (IsCurModuleFull()) {
65 fileGenerator_->CompileLatestModuleThenDestroy(isJitAndCodeSign);
66 }
67 }
68
CompileLastModuleThenDestroyIfNeeded()69 void CompilationDriver::CompileLastModuleThenDestroyIfNeeded()
70 {
71 if (!IsCurModuleFull()) {
72 fileGenerator_->CompileLatestModuleThenDestroy();
73 }
74 }
75
SplitString(const std::string & str,const char ch) const76 std::vector<std::string> CompilationDriver::SplitString(const std::string &str, const char ch) const
77 {
78 std::vector<std::string> vec {};
79 std::istringstream sstr(str.c_str());
80 std::string split;
81 while (getline(sstr, split, ch)) {
82 vec.emplace_back(split);
83 }
84 return vec;
85 }
86
SetCurrentCompilationFile() const87 void CompilationDriver::SetCurrentCompilationFile() const
88 {
89 PGOTypeManager *ptManager = compilationEnv_->GetPTManager();
90 ptManager->SetCurCompilationFile(jsPandaFile_);
91 }
92
StoreConstantPoolInfo() const93 void CompilationDriver::StoreConstantPoolInfo() const
94 {
95 auto &methodList = bytecodeInfo_.GetMethodList();
96 for (auto &x : methodList) {
97 if (!bytecodeInfo_.FindMethodOffsetToRecordName(x.first)) {
98 bytecodeInfo_.AddSkippedMethod(x.first);
99 }
100 }
101 PGOTypeManager *ptManager = compilationEnv_->GetPTManager();
102 ptManager->GetAOTSnapshot().StoreConstantPoolInfo(collector_);
103 }
104
RunCg()105 bool JitCompilationDriver::RunCg()
106 {
107 IncCompiledMethod();
108 bool enableCodeSign = !compilationEnv_->GetJSOptions().GetDisableCodeSign();
109 CompileModuleThenDestroyIfNeeded(enableCodeSign); // isJit AND !DisabelCodeSign
110 return true;
111 }
112
GetModule()113 Module *JitCompilationDriver::GetModule()
114 {
115 return IsCurModuleFull() ? fileGenerator_->AddModule(fileName_, triple_, *lOptions_, outputAsm_, true) :
116 fileGenerator_->GetLatestModule();
117 }
118 } // namespace panda::ecmascript::kungfu
119