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