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 "compilerImpl.h"
17
18 #include <binder/binder.h>
19 #include <compiler/core/compileQueue.h>
20 #include <compiler/core/compilerContext.h>
21 #include <compiler/core/emitter/emitter.h>
22 #include <es2panda.h>
23 #include <parser/program/program.h>
24 #include <typescript/checker.h>
25
26 #include <iostream>
27 #include <thread>
28
29 namespace panda::es2panda::compiler {
30
~CompilerImpl()31 CompilerImpl::~CompilerImpl()
32 {
33 if (queue_ != nullptr) {
34 delete queue_;
35 queue_ = nullptr;
36 }
37 }
38
Compile(parser::Program * program,const es2panda::CompilerOptions & options,const std::string & debugInfoSourceFile,const std::string & pkgName)39 panda::pandasm::Program *CompilerImpl::Compile(parser::Program *program, const es2panda::CompilerOptions &options,
40 const std::string &debugInfoSourceFile, const std::string &pkgName)
41 {
42 CompilerContext context(program->Binder(), options.isDebug, options.isDebuggerEvaluateExpressionMode,
43 options.mergeAbc, options.typeExtractor, false, debugInfoSourceFile, pkgName,
44 program->RecordName(), hotfixHelper_);
45
46 ArenaAllocator localAllocator(SpaceType::SPACE_TYPE_COMPILER, nullptr, true);
47
48 if (options.typeExtractor) {
49 ASSERT(program->Extension() == ScriptExtension::TS);
50
51 auto rootNode = context.Binder()->TopScope()->Node()->AsBlockStatement();
52 extractor_ = std::make_unique<extractor::TypeExtractor>(rootNode, program->IsDtsFile(),
53 options.typeDtsBuiltin, &localAllocator, &context);
54 extractor_->StartTypeExtractor(program);
55
56 context.SetTypeRecorder(extractor_->Recorder());
57 }
58
59 if (program->Extension() == ScriptExtension::TS) {
60 context.GetEmitter()->FillTypeInfoRecord(options.typeExtractor,
61 options.typeExtractor ? extractor_->Recorder()->GetTypeSummaryIndex() : 0);
62 }
63
64 queue_ = new CompileFuncQueue(threadCount_, &context);
65 queue_->Schedule();
66
67 /* Main thread can also be used instead of idling */
68 queue_->Consume();
69 queue_->Wait();
70
71 return context.GetEmitter()->Finalize(options.dumpDebugInfo, hotfixHelper_);
72 }
73
DumpAsm(const panda::pandasm::Program * prog)74 void CompilerImpl::DumpAsm(const panda::pandasm::Program *prog)
75 {
76 Emitter::DumpAsm(prog);
77 }
78
79 } // namespace panda::es2panda::compiler
80