• 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 <typescript/extractor/typeRecorder.h>
25 #include <util/patchFix.h>
26 #include <util/ustring.h>
27 
28 #include <list>
29 #include <mutex>
30 #include <string>
31 #include <unordered_map>
32 #include <unordered_set>
33 #include <vector>
34 
35 namespace panda::pandasm {
36 struct Program;
37 struct Function;
38 struct Ins;
39 struct Record;
40 }  // namespace panda::pandasm
41 
42 namespace panda::es2panda::ir {
43 class Statement;
44 }  // namespace panda::es2panda::ir
45 
46 namespace panda::es2panda::binder {
47 class Scope;
48 }  // namespace panda::es2panda::binder
49 
50 namespace panda::es2panda::compiler {
51 class PandaGen;
52 class LiteralBuffer;
53 class DebugInfo;
54 class Label;
55 class IRNode;
56 class CompilerContext;
57 
58 using Literal = panda::pandasm::LiteralArray::Literal;
59 
60 class FunctionEmitter {
61 public:
62     explicit FunctionEmitter(ArenaAllocator *allocator, const PandaGen *pg);
63     ~FunctionEmitter() = default;
64     NO_COPY_SEMANTIC(FunctionEmitter);
65     NO_MOVE_SEMANTIC(FunctionEmitter);
66 
Function()67     panda::pandasm::Function *Function()
68     {
69         return func_;
70     }
71 
LiteralBuffers()72     auto &LiteralBuffers()
73     {
74         return literalBuffers_;
75     }
76 
77     void Generate(util::PatchFix *patchFixHelper);
78     const ArenaSet<util::StringView> &Strings() const;
79 
80 private:
81     uint32_t UpdateForReturnIns(const ir::AstNode *astNode, panda::pandasm::Ins *pandaIns);
82     void GenInstructionDebugInfo(const IRNode *ins, panda::pandasm::Ins *pandaIns);
83     void GenFunctionInstructions();
84     void GenFunctionCatchTables();
85     void GenScopeVariableInfo(const binder::Scope *scope);
86     void GenSourceFileDebugInfo();
87     void GenVariablesDebugInfo();
88     void GenFunctionKind();
89     void GenIcSize();
90     util::StringView SourceCode() const;
91     lexer::LineIndex &GetLineIndex() const;
92 
93     void GenLiteralBuffers();
94     void GenBufferLiterals(const LiteralBuffer *buff);
95     void GenFunctionSource();
96     void GenConcurrentFunctionModuleRequests();
97 
98     const PandaGen *pg_;
99     panda::pandasm::Function *func_ {};
100     ArenaVector<std::pair<int32_t, std::vector<Literal>>> literalBuffers_;
101     size_t offset_ {0};
102 };
103 
104 class Emitter {
105 public:
106     explicit Emitter(const CompilerContext *context);
107     ~Emitter();
108     NO_COPY_SEMANTIC(Emitter);
109     NO_MOVE_SEMANTIC(Emitter);
110 
111     void AddFunction(FunctionEmitter *func, CompilerContext *context);
112     void AddSourceTextModuleRecord(ModuleRecordEmitter *module, CompilerContext *context);
113     void FillTypeInfoRecord(CompilerContext *context, bool typeFlag, int64_t typeSummaryIndex,
114         const std::string &recordName) const;
115     void FillTypeLiteralBuffers(const extractor::TypeRecorder *recorder) const;
116     static void GenBufferLiterals(ArenaVector<std::pair<int32_t, std::vector<Literal>>> &literalBuffers,
117                                   const LiteralBuffer *buff);
118     static void DumpAsm(const panda::pandasm::Program *prog);
119     panda::pandasm::Program *Finalize(bool dumpDebugInfo, util::PatchFix *patchFixHelper);
120     panda::pandasm::Program *GetProgram() const;
121     void GenJsonContentRecord(const CompilerContext *context);
122     void GenRecordNameInfo() const;
123     void GenTypeInfoRecord() const;
GetEmitterLock()124     std::mutex &GetEmitterLock()
125     {
126         return m_;
127     };
128 
129 private:
130     void SetCommonjsField(bool isCommonjs);
131     void SetPkgNameField(std::string pkgName);
132     void GenCommonjsRecord() const;
133     void GenESTypeAnnotationRecord() const;
134     void AddHasTopLevelAwaitRecord(bool hasTLA, const CompilerContext *context);
135 
136     std::mutex m_;
137     panda::pandasm::Program *prog_;
138     panda::pandasm::Record *rec_;
139 };
140 }  // namespace panda::es2panda::compiler
141 
142 #endif
143