1/*
2 * Copyright (c) 2023 - 2024 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#ifndef ES2PANDA_AOT_EMITFILES_H
17#define ES2PANDA_AOT_EMITFILES_H
18
19#include <aot/options.h>
20#include <util/helpers.h>
21#include <util/workerQueue.h>
22
23#include <mutex>
24
25namespace panda::es2panda::aot {
26class EmitSingleAbcJob : public util::WorkerJob {
27public:
28    explicit EmitSingleAbcJob(const std::string &outputFileName, panda::pandasm::Program *prog,
29                              std::map<std::string, size_t> *statp, uint8_t targetApi, std::string targetSubApi)
30        : outputFileName_(outputFileName), prog_(prog), statp_(statp),
31          targetApiVersion_(targetApi), targetApiSubVersion_(targetSubApi) {};
32    NO_COPY_SEMANTIC(EmitSingleAbcJob);
33    NO_MOVE_SEMANTIC(EmitSingleAbcJob);
34    ~EmitSingleAbcJob() override = default;
35
36    void Run() override;
37private:
38    std::string outputFileName_;
39    panda::pandasm::Program *prog_;
40    std::map<std::string, size_t> *statp_;
41    uint8_t targetApiVersion_ = 0;
42    std::string targetApiSubVersion_ { util::Helpers::DEFAULT_SUB_API_VERSION };
43};
44
45class EmitMergedAbcJob : public util::WorkerJob {
46public:
47    explicit EmitMergedAbcJob(const std::string &outputFileName, const std::string &transformLib,
48                              const std::map<std::string, panda::es2panda::util::ProgramCache*> &progsInfo,
49                              uint8_t targetApi, std::string targetSubApi)
50        : outputFileName_(outputFileName), transformLib_(transformLib),
51        progsInfo_(progsInfo), targetApiVersion_(targetApi), targetApiSubVersion_(targetSubApi) {};
52    NO_COPY_SEMANTIC(EmitMergedAbcJob);
53    NO_MOVE_SEMANTIC(EmitMergedAbcJob);
54    ~EmitMergedAbcJob() override = default;
55
56    void Run() override;
57private:
58    std::string outputFileName_;
59    std::string transformLib_;
60    const std::map<std::string, panda::es2panda::util::ProgramCache*> &progsInfo_;
61    uint8_t targetApiVersion_ = 0;
62    std::string targetApiSubVersion_ { util::Helpers::DEFAULT_SUB_API_VERSION };
63};
64
65class EmitCacheJob : public util::WorkerJob {
66public:
67    explicit EmitCacheJob(const std::string &outputProtoName, panda::es2panda::util::ProgramCache *progCache)
68        : outputProtoName_(outputProtoName), progCache_(progCache) {};
69    NO_COPY_SEMANTIC(EmitCacheJob);
70    NO_MOVE_SEMANTIC(EmitCacheJob);
71    ~EmitCacheJob() override = default;
72
73    void Run() override;
74private:
75    std::string outputProtoName_;
76    panda::es2panda::util::ProgramCache *progCache_;
77};
78
79class EmitFileQueue : public util::WorkerQueue {
80public:
81    explicit EmitFileQueue(const std::unique_ptr<panda::es2panda::aot::Options> &options,
82                           std::map<std::string, size_t> *statp,
83                           const std::map<std::string, panda::es2panda::util::ProgramCache*> &progsInfo)
84        : WorkerQueue(options->CompilerOptions().fileThreadCount), options_(options), statp_(statp),
85        progsInfo_(progsInfo) {
86            mergeAbc_ = options_->CompilerOptions().mergeAbc;
87        }
88
89    NO_COPY_SEMANTIC(EmitFileQueue);
90    NO_MOVE_SEMANTIC(EmitFileQueue);
91    ~EmitFileQueue() override = default;
92
93    void Schedule() override;
94
95private:
96    void ScheduleEmitCacheJobs(EmitMergedAbcJob *emitMergedAbcJob);
97    const std::unique_ptr<panda::es2panda::aot::Options> &options_;
98    std::map<std::string, size_t> *statp_;
99    const std::map<std::string, panda::es2panda::util::ProgramCache*> &progsInfo_;
100    bool mergeAbc_ { false };
101};
102}  // namespace panda::es2panda::aot
103
104#endif
105