• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
76     void Generate(util::PatchFix *patchFixHelper);
77     const ArenaSet<util::StringView> &Strings() const;
78 
79 private:
80     uint32_t UpdateForReturnIns(const ir::AstNode *astNode, panda::pandasm::Ins *pandaIns);
81     void GenInstructionDebugInfo(const IRNode *ins, panda::pandasm::Ins *pandaIns);
82     void GenFunctionInstructions();
83     void GenFunctionCatchTables();
84     void GenScopeVariableInfo(const binder::Scope *scope);
85     void GenSourceFileDebugInfo();
86     void GenVariablesDebugInfo();
87     void GenFunctionKind();
88     void GenIcSize();
89     util::StringView SourceCode() const;
90     lexer::LineIndex &GetLineIndex() const;
91 
92     void GenLiteralBuffers();
93     void GenBufferLiterals(const LiteralBuffer *buff);
94     void GenConcurrentFunctionModuleRequests();
95 
96     const PandaGen *pg_;
97     panda::pandasm::Function *func_ {};
98     ArenaVector<std::pair<int32_t, std::vector<Literal>>> literalBuffers_;
99     size_t offset_ {0};
100 };
101 
102 class Emitter {
103 public:
104     explicit Emitter(CompilerContext *context);
105     ~Emitter();
106     NO_COPY_SEMANTIC(Emitter);
107     NO_MOVE_SEMANTIC(Emitter);
108 
109     void AddFunction(FunctionEmitter *func, CompilerContext *context);
110     void AddSourceTextModuleRecord(ModuleRecordEmitter *module, CompilerContext *context);
111     void AddScopeNamesRecord(CompilerContext *context);
112     static void GenBufferLiterals(ArenaVector<std::pair<int32_t, std::vector<Literal>>> &literalBuffers,
113                                   const LiteralBuffer *buff);
114     static void DumpAsm(const panda::pandasm::Program *prog);
115     panda::pandasm::Program *Finalize(bool dumpDebugInfo, util::PatchFix *patchFixHelper);
116     panda::pandasm::Program *GetProgram() const;
117     void GenJsonContentRecord(const CompilerContext *context);
118     void GenRecordNameInfo() const;
GetEmitterLock()119     std::mutex &GetEmitterLock()
120     {
121         return m_;
122     };
123 
GetConstantLocalExportSlots()124     const std::unordered_set<uint32_t> &GetConstantLocalExportSlots()
125     {
126         return constant_local_export_slots_;
127     }
128 
129 private:
130     void SetCommonjsField(bool isCommonjs);
131     void SetPkgNameField(const std::string &pkgName);
132     void GenCommonjsRecord() const;
133     void AddHasTopLevelAwaitRecord(bool hasTLA, const CompilerContext *context);
134     void AddSharedModuleRecord(const CompilerContext *context);
135     void AddModuleRequestPhaseRecord(ModuleRecordEmitter *module, CompilerContext *context);
136 
137     std::mutex m_;
138     panda::pandasm::Program *prog_;
139     panda::pandasm::Record *rec_;
140     // Constant local export module variable slots.
141     // Used by the branch elimination optimization to identify initial constants.
142     // Should be replaced by analyzing the stconstmodulevar instruction in the
143     // bytecode file after it is introduced
144     std::unordered_set<uint32_t> constant_local_export_slots_;
145 };
146 }  // namespace panda::es2panda::compiler
147 
148 #endif
149