• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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/stub_compiler.h"
17 
18 #include "ecmascript/base/config.h"
19 #include "ecmascript/base/string_helper.h"
20 #include "ecmascript/compiler/common_stubs.h"
21 #include "ecmascript/compiler/file_generators.h"
22 #include "ecmascript/compiler/interpreter_stub-inl.h"
23 #include "ecmascript/compiler/llvm_codegen.h"
24 #include "ecmascript/compiler/pass.h"
25 #include "ecmascript/compiler/scheduler.h"
26 #include "ecmascript/compiler/stub.h"
27 #include "ecmascript/compiler/stub_builder-inl.h"
28 #include "ecmascript/compiler/verifier.h"
29 #include "ecmascript/js_runtime_options.h"
30 #include "ecmascript/log.h"
31 #include "ecmascript/napi/include/jsnapi.h"
32 
33 namespace panda::ecmascript::kungfu {
34 class StubPassData : public PassData {
35 public:
StubPassData(Stub * stub,LLVMModule * module,CompilerLog * log)36     StubPassData(Stub *stub, LLVMModule *module, CompilerLog *log)
37         : PassData(nullptr, nullptr, nullptr, log, "stubs"),
38           cfg_(module->GetTripleStr()),
39           module_(module),
40           stub_(stub) {}
41     ~StubPassData() = default;
42 
GetCompilationConfig() const43     const CompilationConfig *GetCompilationConfig() const
44     {
45         return &cfg_;
46     }
47 
GetCircuit() const48     Circuit *GetCircuit() const
49     {
50         return stub_->GetCircuit();
51     }
52 
GetStubModule() const53     LLVMModule *GetStubModule() const
54     {
55         return module_;
56     }
57 
GetStub() const58     Stub *GetStub() const
59     {
60         return stub_;
61     }
62 
63 private:
64     CompilationConfig cfg_;
65     LLVMModule *module_;
66     Stub *stub_;
67 };
68 
69 class StubBuildCircuitPass {
70 public:
Run(StubPassData * data)71     bool Run(StubPassData *data)
72     {
73         auto stub = data->GetStub();
74         LOG_COMPILER(INFO) << "Stub Name: " << stub->GetMethodName();
75         stub->GenerateCircuit(data->GetCompilationConfig());
76         return true;
77     }
78 };
79 
80 class StubLLVMIRGenPass {
81 public:
CreateCodeGen(LLVMModule * module,bool enableLog)82     void CreateCodeGen(LLVMModule *module, bool enableLog)
83     {
84         llvmImpl_ = std::make_unique<LLVMIRGeneratorImpl>(module, enableLog);
85     }
86 
Run(StubPassData * data,size_t index)87     bool Run(StubPassData *data, size_t index)
88     {
89         bool enableLog = data->GetLog()->EnableMethodCIRLog() || data->GetLog()->OutputASM();
90         auto stubModule = data->GetStubModule();
91         CreateCodeGen(stubModule, enableLog);
92         CodeGenerator codegen(llvmImpl_, "stubs");
93         codegen.RunForStub(data->GetCircuit(), data->GetConstScheduleResult(), index, data->GetCompilationConfig());
94         return true;
95     }
96 private:
97     std::unique_ptr<CodeGeneratorImpl> llvmImpl_ {nullptr};
98 };
99 
RunPipeline(LLVMModule * module,NativeAreaAllocator * allocator) const100 void StubCompiler::RunPipeline(LLVMModule *module, NativeAreaAllocator *allocator) const
101 {
102     auto callSigns = module->GetCSigns();
103     CompilerLog *log = GetLog();
104     for (size_t i = 0; i < callSigns.size(); i++) {
105         auto &cs = callSigns[i];
106         Circuit circuit(allocator, module->GetDebugInfo(), cs->GetName().c_str(), module->Is64Bit());
107         Stub stub(cs, &circuit);
108         log->SetStubLog(stub.GetMethodName(), GetLogList());
109 
110         StubPassData data(&stub, module, log);
111         PassRunner<StubPassData> pipeline(&data);
112         pipeline.RunPass<StubBuildCircuitPass>();
113         pipeline.RunPass<VerifierPass>();
114         pipeline.RunPass<SchedulingPass>();
115         pipeline.RunPass<StubLLVMIRGenPass>(i);
116     }
117 }
118 
InitializeCS() const119 void StubCompiler::InitializeCS() const
120 {
121     BytecodeStubCSigns::Initialize();
122     CommonStubCSigns::Initialize();
123     BuiltinsStubCSigns::Initialize();
124     RuntimeStubCSigns::Initialize();
125 }
126 
BuildStubModuleAndSave() const127 bool StubCompiler::BuildStubModuleAndSave() const
128 {
129     if (filePath_.empty()) {
130         return false;
131     }
132 
133     InitializeCS();
134     CompilerLog *log = GetLog();
135     MethodLogList *logList = GetLogList();
136 
137     NativeAreaAllocator allocator;
138     StubFileGenerator generator(log, logList, triple_);
139 
140     LOG_COMPILER(INFO) << "=============== compiling bytecode handler stubs ===============";
141     LOptions stubOp(optLevel_, FPFlag::ELIM_FP, relocMode_);
142     Module* stubM = generator.AddModule(&allocator, "bc_stub", triple_, stubOp, log->OutputASM(), StubFileKind::BC);
143     RunPipeline(stubM->GetModule(), &allocator);
144 
145     LOG_COMPILER(INFO) << "=============== compiling common stubs ===============";
146     LOptions comOp(optLevel_, FPFlag::RESERVE_FP, relocMode_);
147     Module* comM = generator.AddModule(&allocator, "com_stub", triple_, comOp, log->OutputASM(), StubFileKind::COM);
148     RunPipeline(comM->GetModule(), &allocator);
149 
150     LOG_COMPILER(INFO) << "=============== compiling builtins stubs ===============";
151     LOptions builtinOp(optLevel_, FPFlag::RESERVE_FP, relocMode_);
152     Module* builtinM = generator.AddModule(&allocator, "builtin_stub", triple_, builtinOp, log->OutputASM(),
153                                            StubFileKind::BUILTIN);
154     RunPipeline(builtinM->GetModule(), &allocator);
155 
156     generator.SaveStubFile(filePath_);
157     return true;
158 }
159 
GetHelper()160 std::string GetHelper()
161 {
162     std::string str;
163     str.append(STUB_HELP_HEAD_MSG);
164     str.append(HELP_OPTION_MSG);
165     return str;
166 }
167 }  // namespace panda::ecmascript::kungfu
168 
main(const int argc,const char ** argv)169 int main(const int argc, const char **argv)
170 {
171     panda::ecmascript::JSRuntimeOptions runtimeOptions;
172     bool ret = runtimeOptions.ParseCommand(argc, argv);
173     if (!ret) {
174         std::cerr << panda::ecmascript::kungfu::GetHelper();
175         return 1;
176     }
177 
178     panda::ecmascript::Log::Initialize(runtimeOptions);
179     std::string triple = runtimeOptions.GetTargetTriple();
180     std::string stubFile = runtimeOptions.GetStubFile();
181     size_t optLevel = runtimeOptions.GetOptLevel();
182     size_t relocMode = runtimeOptions.GetRelocMode();
183     std::string logOption = runtimeOptions.GetCompilerLogOption();
184     std::string methodsList = runtimeOptions.GetMethodsListForLog();
185 
186     panda::ecmascript::kungfu::CompilerLog logOpt(logOption);
187     panda::ecmascript::kungfu::MethodLogList logList(methodsList);
188     panda::ecmascript::kungfu::StubCompiler compiler(triple, stubFile, optLevel, relocMode, &logOpt, &logList);
189 
190     bool res = compiler.BuildStubModuleAndSave();
191     LOG_COMPILER(INFO) << "stub compiler run finish, result condition(T/F):" << std::boolalpha << res;
192     return res ? 0 : -1;
193 }
194