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
29namespace panda::es2panda::compiler {
30
31CompilerImpl::~CompilerImpl()
32{
33    if (queue_ != nullptr) {
34        delete queue_;
35        queue_ = nullptr;
36    }
37}
38
39panda::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, false, options.recordDebugSource,
44                            debugInfoSourceFile, pkgName, program->RecordName(), patchFixHelper_);
45
46    ArenaAllocator localAllocator(SpaceType::SPACE_TYPE_COMPILER, nullptr, true);
47
48    queue_ = new CompileFuncQueue(threadCount_, &context);
49    queue_->Schedule();
50
51    /* Main thread can also be used instead of idling */
52    queue_->Consume();
53    queue_->Wait();
54
55    if (context.IsMergeAbc()) {
56        context.GetEmitter()->GenRecordNameInfo();
57    }
58
59    // For global optimization
60    // Constant local export module variable slot info is recorded in the emitter for now,
61    // emit it to the bytecode analysis result here.
62    if (options.requireGlobalOptimization) {
63        util::Helpers::SetConstantLocalExportSlots(std::string(program->RecordName()),
64            context.GetEmitter()->GetConstantLocalExportSlots());
65    }
66
67    return context.GetEmitter()->Finalize(options.dumpDebugInfo, patchFixHelper_);
68}
69
70void CompilerImpl::DumpAsm(const panda::pandasm::Program *prog)
71{
72    Emitter::DumpAsm(prog);
73}
74
75}  // namespace panda::es2panda::compiler
76