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_EMITTER_H
17 #define ES2PANDA_COMPILER_IR_EMITTER_H
18 
19 #include <assembly-literals.h>
20 #include <compiler/core/emitter/moduleRecordEmitter.h>
21 #include <ir/astNode.h>
22 #include <lexer/token/sourceLocation.h>
23 #include <macros.h>
24 #include <util/patchFix.h>
25 #include <util/ustring.h>
26 
27 #include <list>
28 #include <mutex>
29 #include <string>
30 #include <unordered_map>
31 #include <unordered_set>
32 #include <vector>
33 
34 namespace panda::pandasm {
35 struct Program;
36 struct Function;
37 struct Ins;
38 struct Record;
39 }  // namespace panda::pandasm
40 
41 namespace panda::es2panda::ir {
42 class Statement;
43 }  // namespace panda::es2panda::ir
44 
45 namespace panda::es2panda::binder {
46 class Scope;
47 }  // namespace panda::es2panda::binder
48 
49 namespace panda::es2panda::compiler {
50 class PandaGen;
51 class LiteralBuffer;
52 class DebugInfo;
53 class Label;
54 class IRNode;
55 class CompilerContext;
56 
57 using Literal = panda::pandasm::LiteralArray::Literal;
58 
59 class FunctionEmitter {
60 public:
61     explicit FunctionEmitter(ArenaAllocator *allocator, const PandaGen *pg);
62     ~FunctionEmitter() = default;
63     NO_COPY_SEMANTIC(FunctionEmitter);
64     NO_MOVE_SEMANTIC(FunctionEmitter);
65 
Function()66     panda::pandasm::Function *Function()
67     {
68         return func_;
69     }
70 
LiteralBuffers()71     auto &LiteralBuffers()
72     {
73         return literalBuffers_;
74     }
75 
LiteralArrays()76     auto &LiteralArrays()
77     {
78         return literalArrays_;
79     }
80 
ExternalAnnotationRecords()81     auto &ExternalAnnotationRecords()
82     {
83         return externalAnnotationRecords_;
84     }
85 
86     void Generate(util::PatchFix *patchFixHelper);
87     const ArenaSet<util::StringView> &Strings() const;
88 
89 private:
90     uint32_t UpdateForReturnIns(const ir::AstNode *astNode, panda::pandasm::Ins *pandaIns);
91     void GenInstructionDebugInfo(const IRNode *ins, panda::pandasm::Ins *pandaIns);
92     void GenFunctionInstructions();
93     void GenFunctionCatchTables();
94     void GenScopeVariableInfo(const binder::Scope *scope);
95     void GenSourceFileDebugInfo();
96     void GenVariablesDebugInfo();
97     void GenFunctionKind();
98     void GenIcSize();
99     pandasm::AnnotationElement CreateAnnotationElement(const std::string &propName, const ir::Expression *initValue);
100     pandasm::AnnotationData CreateAnnotation(const ir::Annotation *anno);
101     void GenAnnotations();
102     util::StringView SourceCode() const;
103     lexer::LineIndex &GetLineIndex() const;
104 
105     void GenLiteralBuffers();
106     void GenBufferLiterals(const LiteralBuffer *buff);
107     void GenConcurrentFunctionModuleRequests();
108 
109     const PandaGen *pg_;
110     panda::pandasm::Function *func_ {};
111     ArenaVector<std::pair<int32_t, std::vector<Literal>>> literalBuffers_;
112     size_t offset_ {0};
113 
114     ArenaVector<std::pair<std::string, std::vector<Literal>>> literalArrays_;
115     ArenaVector<panda::pandasm::Record> externalAnnotationRecords_;
116 };
117 
118 class Emitter {
119 public:
120     explicit Emitter(CompilerContext *context);
121     ~Emitter();
122     NO_COPY_SEMANTIC(Emitter);
123     NO_MOVE_SEMANTIC(Emitter);
124 
125     void AddAnnotationRecord(const std::string &annoName, const ir::ClassDeclaration *classDecl);
126     void AddFunction(FunctionEmitter *func, CompilerContext *context);
127     void AddSourceTextModuleRecord(ModuleRecordEmitter *module, CompilerContext *context);
128     void AddScopeNamesRecord(CompilerContext *context);
129     static void GenBufferLiterals(ArenaVector<std::pair<int32_t, std::vector<Literal>>> &literalBuffers,
130                                   const LiteralBuffer *buff);
131     static void DumpAsm(const panda::pandasm::Program *prog);
132     panda::pandasm::Program *Finalize(bool dumpDebugInfo, util::PatchFix *patchFixHelper);
133     panda::pandasm::Program *GetProgram() const;
134     void GenJsonContentRecord(const CompilerContext *context);
135     void GenRecordNameInfo() const;
GetEmitterLock()136     std::mutex &GetEmitterLock()
137     {
138         return m_;
139     };
140 
GetConstantLocalExportSlots()141     const std::unordered_set<uint32_t> &GetConstantLocalExportSlots()
142     {
143         return constant_local_export_slots_;
144     }
145 
146     static std::vector<std::pair<std::string, std::vector<Literal>>> CreateLiteralArray(const ir::Expression *array,
147                                                                                         const std::string &baseName);
148 
149 private:
150     void CreateStringClass();
151     panda::pandasm::Type DeduceArrayEnumType(const ir::Expression *value, uint8_t rank, bool &needToCreateArrayValue);
152 
153     void SetCommonjsField(bool isCommonjs);
154     void SetPkgNameField(const std::string &pkgName);
155     void GenCommonjsRecord() const;
156     void AddHasTopLevelAwaitRecord(bool hasTLA, const CompilerContext *context);
157     void AddSharedModuleRecord(const CompilerContext *context);
158     void AddModuleRequestPhaseRecord(ModuleRecordEmitter *module, CompilerContext *context);
159 
160     void CreateEnumProp(const ir::ClassProperty *prop, const std::string &annoName, panda::pandasm::Field &annoProp);
161     void CreateLiteralArrayProp(const ir::ClassProperty *prop, const std::string &annoName,
162                                 panda::pandasm::Field &annoProp);
163     panda::pandasm::Field CreateAnnotationProp(const ir::ClassProperty *prop, const std::string &annoName);
164 
165     std::mutex m_;
166     panda::pandasm::Program *prog_;
167     panda::pandasm::Record *rec_;
168     // Constant local export module variable slots.
169     // Used by the branch elimination optimization to identify initial constants.
170     // Should be replaced by analyzing the stconstmodulevar instruction in the
171     // bytecode file after it is introduced
172     std::unordered_set<uint32_t> constant_local_export_slots_;
173 };
174 }  // namespace panda::es2panda::compiler
175 
176 #endif
177