• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2021-2024 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_CORE_EMITTER_H
17 #define ES2PANDA_COMPILER_CORE_EMITTER_H
18 
19 #include "compiler/base/literals.h"
20 
21 #include "util/ustring.h"
22 
23 #include <list>
24 #include <mutex>
25 #include <string>
26 #include <unordered_map>
27 #include <unordered_set>
28 #include <vector>
29 
30 namespace ark::pandasm {
31 struct Program;
32 struct Function;
33 struct Ins;
34 namespace debuginfo {
35 struct LocalVariable;
36 }  // namespace debuginfo
37 }  // namespace ark::pandasm
38 
39 namespace ark::es2panda::varbinder {
40 class Scope;
41 class LocalVariable;
42 }  // namespace ark::es2panda::varbinder
43 
44 namespace ark::es2panda::public_lib {
45 struct Context;
46 }  // namespace ark::es2panda::public_lib
47 
48 namespace ark::es2panda::compiler {
49 class CodeGen;
50 class DebugInfo;
51 class Label;
52 class IRNode;
53 class ProgramElement;
54 class RegSpiller;
55 
56 class FunctionEmitter {
57 public:
FunctionEmitter(const CodeGen * cg,ProgramElement * programElement)58     explicit FunctionEmitter(const CodeGen *cg, ProgramElement *programElement)
59         : cg_(cg), programElement_(programElement)
60     {
61     }
62 
63     virtual ~FunctionEmitter() = default;
64     NO_COPY_SEMANTIC(FunctionEmitter);
65     NO_MOVE_SEMANTIC(FunctionEmitter);
66 
67     void Generate();
68 
69 protected:
70     virtual pandasm::Function *GenFunctionSignature() = 0;
71     virtual void GenFunctionAnnotations(pandasm::Function *func) = 0;
72     virtual void GenVariableSignature(pandasm::debuginfo::LocalVariable &variableDebug,
73                                       varbinder::LocalVariable *variable) const = 0;
74 
75     void GenInstructionDebugInfo(const IRNode *ins, ark::pandasm::Ins *pandaIns);
76     void GenFunctionInstructions(pandasm::Function *func);
77     void GenScopeVariableInfo(pandasm::Function *func, const varbinder::Scope *scope) const;
78     void GenScopeVariableInfoEnd(pandasm::Function *func, const varbinder::Scope *scope, uint32_t count,
79                                  uint32_t start) const;
80     void GenSourceFileDebugInfo(pandasm::Function *func);
81     void GenFunctionCatchTables(ark::pandasm::Function *func);
82     void GenVariablesDebugInfo(pandasm::Function *func);
83     util::StringView SourceCode() const;
84 
Cg()85     const CodeGen *Cg() const
86     {
87         return cg_;
88     }
89 
GetProgramElement()90     ProgramElement *GetProgramElement() const
91     {
92         return programElement_;
93     }
94 
95 private:
96     const CodeGen *cg_;
97     ProgramElement *programElement_;
98     size_t offset_ {0};
99 };
100 
101 class Emitter {
102 public:
103     virtual ~Emitter();
104     NO_COPY_SEMANTIC(Emitter);
105     NO_MOVE_SEMANTIC(Emitter);
106 
107     void AddLiteralBuffer(const LiteralBuffer &literals, uint32_t index);
108     void AddProgramElement(ProgramElement *programElement);
109     static void DumpAsm(const pandasm::Program *prog);
110     pandasm::Program *Finalize(bool dumpDebugInfo, std::string_view globalClass = "");
111 
LiteralBufferIndex()112     uint32_t &LiteralBufferIndex()
113     {
114         return literalBufferIndex_;
115     }
116 
117     virtual void GenAnnotation() = 0;
118 
119 protected:
120     explicit Emitter(const public_lib::Context *context);
121 
Program()122     pandasm::Program *Program() const
123     {
124         return prog_;
125     }
126 
Context()127     const public_lib::Context *Context() const
128     {
129         return context_;
130     }
131 
132 private:
133     pandasm::Program *prog_;
134     const public_lib::Context *context_;
135     uint32_t literalBufferIndex_ {};
136 };
137 }  // namespace ark::es2panda::compiler
138 
139 #endif
140