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