• 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/hotfix.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::Hotfix *hotfixHelper);
78     const ArenaSet<util::StringView> &Strings() const;
79 
80 private:
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 
95     const PandaGen *pg_;
96     panda::pandasm::Function *func_ {};
97     ArenaVector<std::pair<int32_t, std::vector<Literal>>> literalBuffers_;
98     size_t offset_ {0};
99 };
100 
101 class Emitter {
102 public:
103     explicit Emitter(const CompilerContext *context);
104     ~Emitter();
105     NO_COPY_SEMANTIC(Emitter);
106     NO_MOVE_SEMANTIC(Emitter);
107 
108     void AddFunction(FunctionEmitter *func, CompilerContext *context);
109     void AddSourceTextModuleRecord(ModuleRecordEmitter *module, CompilerContext *context);
110     void FillTypeInfoRecord(bool typeFlag, int64_t typeSummaryIndex) const;
111     void FillTypeLiteralBuffers(const extractor::TypeRecorder *recorder) const;
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::Hotfix *hotfixHelper);
116     void GenJsonContentRecord(const CompilerContext *context);
117 
118 private:
119     void SetCommonjsField(bool isCommonjs);
120     void SetPkgNameField(std::string pkgName);
121     void GenCommonjsRecord() const;
122     void GenTypeInfoRecord() const;
123     void GenESTypeAnnotationRecord() const;
124 
125     std::mutex m_;
126     panda::pandasm::Program *prog_;
127     panda::pandasm::Record *rec_;
128 };
129 }  // namespace panda::es2panda::compiler
130 
131 #endif
132