• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_->PatchFixHelper());
47 
48     context_->GetEmitter()->AddFunction(&funcEmitter, context_);
49 
50     for (auto *dependant : dependants_) {
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     for (auto *dependant : dependants_) {
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(src_->scriptExtension, options_->functionThreadCount);
99     auto *prog = compiler.CompileFile(*options_, src_, symbolTable_);
100     if (prog == nullptr) {
101         return;
102     }
103 
104     if (options_->optLevel != 0) {
105         util::Helpers::OptimizeProgram(prog, src_->fileName);
106     }
107 
108     {
109         std::unique_lock<std::mutex> lock(global_m_);
110         auto *cache = allocator_->New<util::ProgramCache>(src_->hash, std::move(*prog), true);
111         progsInfo_.insert({src_->fileName, cache});
112     }
113 }
114 
Schedule()115 void CompileFuncQueue::Schedule()
116 {
117     ASSERT(jobsCount_ == 0);
118     std::unique_lock<std::mutex> lock(m_);
119     const auto &functions = context_->Binder()->Functions();
120 
121     for (auto *function : functions) {
122         auto *funcJob = new CompileFunctionJob(context_);
123         funcJob->SetFunctionScope(function);
124         jobs_.push_back(funcJob);
125         jobsCount_++;
126     }
127 
128     if (context_->Binder()->Program()->Kind() == parser::ScriptKind::MODULE) {
129         auto *moduleRecordJob = new CompileModuleRecordJob(context_);
130         jobs_.push_back(moduleRecordJob);
131         jobsCount_++;
132     }
133 
134     lock.unlock();
135     jobsAvailable_.notify_all();
136 }
137 
Schedule()138 void CompileFileQueue::Schedule()
139 {
140     ASSERT(jobsCount_ == 0);
141     std::unique_lock<std::mutex> lock(m_);
142 
143     for (auto &input: options_->sourceFiles) {
144         auto *fileJob = new CompileFileJob(&input, options_, progsInfo_, symbolTable_, allocator_);
145         jobs_.push_back(fileJob);
146         jobsCount_++;
147     }
148 
149     lock.unlock();
150     jobsAvailable_.notify_all();
151 }
152 
153 }  // namespace panda::es2panda::compiler
154