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 #ifndef ES2PANDA_COMPILER_IR_MODULERECORD_EMITTER_H
17 #define ES2PANDA_COMPILER_IR_MODULERECORD_EMITTER_H
18 
19 #include <assembly-literals.h>
20 #include <parser/module/sourceTextModuleRecord.h>
21 
22 #include <vector>
23 
24 namespace panda::es2panda::compiler {
25 class ModuleRecordEmitter {
26 public:
ModuleRecordEmitter(parser::SourceTextModuleRecord *moduleRecord, int32_t bufferIdx, int32_t phaseBufferIdx)27     explicit ModuleRecordEmitter(parser::SourceTextModuleRecord *moduleRecord, int32_t bufferIdx,
28                                  int32_t phaseBufferIdx)
29         : moduleRecord_(moduleRecord), bufferIdx_(bufferIdx), phaseBufferIdx_(phaseBufferIdx) {}
30     ~ModuleRecordEmitter() = default;
31     NO_COPY_SEMANTIC(ModuleRecordEmitter);
32     NO_MOVE_SEMANTIC(ModuleRecordEmitter);
33 
Index() const34     int32_t Index() const
35     {
36         return bufferIdx_;
37     }
38 
PhaseIndex() const39     int32_t PhaseIndex() const
40     {
41         return phaseBufferIdx_;
42     }
43 
Buffer()44     auto &Buffer()
45     {
46         return buffer_;
47     }
48 
PhaseBuffer()49     auto &PhaseBuffer()
50     {
51         return phaseBuffer_;
52     }
53 
GetConstantLocalExportSlots()54     std::unordered_set<uint32_t> &GetConstantLocalExportSlots()
55     {
56         return constant_local_export_slots_;
57     }
58 
NeedEmitPhaseRecord() const59     bool NeedEmitPhaseRecord() const
60     {
61         return phaseBufferIdx_ != -1;
62     }
63 
64     void Generate();
65 
66 private:
67     void GenModuleRequests();
68     void GenRegularImportEntries();
69     void GenNamespaceImportEntries();
70     void GenLocalExportEntries();
71     void GenIndirectExportEntries();
72     void GenStarExportEntries();
73 
74     parser::SourceTextModuleRecord *moduleRecord_;
75     int32_t bufferIdx_ {};
76     int32_t phaseBufferIdx_ {};
77     std::vector<panda::pandasm::LiteralArray::Literal> buffer_;
78     std::vector<panda::pandasm::LiteralArray::Literal> phaseBuffer_;
79     std::unordered_set<uint32_t> constant_local_export_slots_;
80 };
81 }  // namespace panda::es2panda::compiler
82 
83 #endif
84