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 bool isTypeExtractorEnabled = ((program->Extension() == ScriptExtension::TS) && options.typeExtractor);
43 CompilerContext context(program->Binder(), options.isDebug, options.isDebuggerEvaluateExpressionMode,
44 options.mergeAbc, isTypeExtractorEnabled, false, debugInfoSourceFile, pkgName,
45 program->RecordName(), patchFixHelper_);
46
47 ArenaAllocator localAllocator(SpaceType::SPACE_TYPE_COMPILER, nullptr, true);
48
49 if (isTypeExtractorEnabled) {
50 auto rootNode = context.Binder()->TopScope()->Node()->AsBlockStatement();
51 extractor_ = std::make_unique<extractor::TypeExtractor>(rootNode, program->IsDtsFile(),
52 options.typeDtsBuiltin, &localAllocator, &context);
53 extractor_->StartTypeExtractor(program);
54
55 context.SetTypeRecorder(extractor_->Recorder());
56 context.SetTypeExtractor(extractor_.get());
57 }
58
59 queue_ = new CompileFuncQueue(threadCount_, &context);
60 queue_->Schedule();
61
62 /* Main thread can also be used instead of idling */
63 queue_->Consume();
64 queue_->Wait();
65
66 // For Type Extractor
67 // Global record to show type extractor is enabled or not
68 if (!context.IsMergeAbc()) {
69 context.GetEmitter()->GenTypeInfoRecord();
70 } else {
71 context.GetEmitter()->GenRecordNameInfo();
72 }
73
74 if (program->Extension() == ScriptExtension::TS) {
75 context.GetEmitter()->FillTypeInfoRecord(&context, options.typeExtractor,
76 options.typeExtractor ? extractor_->Recorder()->GetTypeSummaryIndex() : 0,
77 std::string(program->RecordName()));
78 }
79
80 return context.GetEmitter()->Finalize(options.dumpDebugInfo, patchFixHelper_);
81 }
82
DumpAsm(const panda::pandasm::Program * prog)83 void CompilerImpl::DumpAsm(const panda::pandasm::Program *prog)
84 {
85 Emitter::DumpAsm(prog);
86 }
87
88 } // namespace panda::es2panda::compiler
89