1 /*
2 * Copyright (c) 2021-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 "compileQueue.h"
17
18 #include <binder/binder.h>
19 #include <binder/scope.h>
20 #include <compiler/core/compilerContext.h>
21 #include <compiler/core/emitter/emitter.h>
22 #include <compiler/core/function.h>
23 #include <compiler/core/pandagen.h>
24 #include <es2panda.h>
25 #include <mem/arena_allocator.h>
26 #include <mem/pool_manager.h>
27 #include <protobufSnapshotGenerator.h>
28 #include <util/dumper.h>
29 #include <util/helpers.h>
30
31 namespace panda::es2panda::compiler {
32
33 std::mutex CompileFileJob::global_m_;
34
Run()35 void CompileFunctionJob::Run()
36 {
37 std::unique_lock<std::mutex> lock(m_);
38 cond_.wait(lock, [this] { return dependencies_ == 0; });
39
40 ArenaAllocator allocator(SpaceType::SPACE_TYPE_COMPILER, nullptr, true);
41 PandaGen pg(&allocator, context_, scope_);
42
43 Function::Compile(&pg);
44
45 FunctionEmitter funcEmitter(&allocator, &pg);
46 funcEmitter.Generate(context_->HotfixHelper());
47
48 context_->GetEmitter()->AddFunction(&funcEmitter, context_);
49
50 if (dependant_) {
51 dependant_->Signal();
52 }
53 }
54
Run()55 void CompileModuleRecordJob::Run()
56 {
57 std::unique_lock<std::mutex> lock(m_);
58 cond_.wait(lock, [this] { return dependencies_ == 0; });
59
60 ModuleRecordEmitter moduleEmitter(context_->Binder()->Program()->ModuleRecord(), context_->NewLiteralIndex());
61 moduleEmitter.Generate();
62
63 context_->GetEmitter()->AddSourceTextModuleRecord(&moduleEmitter, context_);
64
65 if (dependant_) {
66 dependant_->Signal();
67 }
68 }
69
Run()70 void CompileFileJob::Run()
71 {
72 std::stringstream ss;
73 std::string buffer;
74 if (!src_->fileName.empty()) {
75 if (!util::Helpers::ReadFileToBuffer(src_->fileName, ss)) {
76 return;
77 }
78 buffer = ss.str();
79 src_->source = buffer;
80
81 auto cacheFileIter = options_->cacheFiles.find(src_->fileName);
82 if (cacheFileIter != options_->cacheFiles.end()) {
83 src_->hash = GetHash32String(reinterpret_cast<const uint8_t *>(buffer.c_str()));
84
85 ArenaAllocator allocator(SpaceType::SPACE_TYPE_COMPILER, nullptr, true);
86 auto *cacheProgramInfo = proto::ProtobufSnapshotGenerator::GetCacheContext(cacheFileIter->second,
87 &allocator);
88
89 if (cacheProgramInfo != nullptr && cacheProgramInfo->hashCode == src_->hash) {
90 std::unique_lock<std::mutex> lock(global_m_);
91 auto *cache = allocator_->New<util::ProgramCache>(src_->hash, std::move(cacheProgramInfo->program));
92 progsInfo_.insert({src_->fileName, cache});
93 return;
94 }
95 }
96 }
97
98 es2panda::Compiler compiler(options_->extension, options_->functionThreadCount);
99 auto *prog = compiler.CompileFile(*options_, src_, symbolTable_);
100 if (prog == nullptr) {
101 return;
102 }
103
104 if (src_->scriptKind != parser::ScriptKind::COMMONJS && options_->optLevel != 0) {
105 // common-js files has hidden parameters which cause optimizer abort, skip optimizing them.
106 util::Helpers::OptimizeProgram(prog, src_->fileName);
107 }
108
109 {
110 std::unique_lock<std::mutex> lock(global_m_);
111 auto *cache = allocator_->New<util::ProgramCache>(src_->hash, std::move(*prog), true);
112 progsInfo_.insert({src_->fileName, cache});
113 }
114 }
115
Schedule()116 void CompileFuncQueue::Schedule()
117 {
118 ASSERT(jobsCount_ == 0);
119 std::unique_lock<std::mutex> lock(m_);
120 const auto &functions = context_->Binder()->Functions();
121
122 for (auto *function : functions) {
123 auto *funcJob = new CompileFunctionJob(context_);
124 funcJob->SetFunctionScope(function);
125 jobs_.push_back(funcJob);
126 jobsCount_++;
127 }
128
129 if (context_->Binder()->Program()->Kind() == parser::ScriptKind::MODULE) {
130 auto *moduleRecordJob = new CompileModuleRecordJob(context_);
131 jobs_.push_back(moduleRecordJob);
132 jobsCount_++;
133 }
134
135 lock.unlock();
136 jobsAvailable_.notify_all();
137 }
138
Schedule()139 void CompileFileQueue::Schedule()
140 {
141 ASSERT(jobsCount_ == 0);
142 std::unique_lock<std::mutex> lock(m_);
143
144 for (auto &input: options_->sourceFiles) {
145 auto *fileJob = new CompileFileJob(&input, options_, progsInfo_, symbolTable_, allocator_);
146 jobs_.push_back(fileJob);
147 jobsCount_++;
148 }
149
150 lock.unlock();
151 jobsAvailable_.notify_all();
152 }
153
154 } // namespace panda::es2panda::compiler
155