• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 ECMASCRIPT_COMPILER_CODE_GENERATOR_H
17 #define ECMASCRIPT_COMPILER_CODE_GENERATOR_H
18 
19 #include "ecmascript/compiler/circuit.h"
20 #include "ecmascript/jspandafile/method_literal.h"
21 
22 namespace panda::ecmascript::kungfu {
23 using ControlFlowGraph = std::vector<std::vector<GateRef>>;
24 class CompilationConfig;
25 
26 class CodeGeneratorImpl {
27 public:
28     CodeGeneratorImpl() = default;
29 
30     virtual ~CodeGeneratorImpl() = default;
31 
32     virtual void GenerateCodeForStub(Circuit *circuit, const ControlFlowGraph &graph, size_t index,
33                                      const CompilationConfig *cfg) = 0;
34 
35     virtual void GenerateCode(Circuit *circuit, const ControlFlowGraph &graph, const CompilationConfig *cfg,
36                               const MethodLiteral *methodLiteral, const JSPandaFile *jsPandaFile,
37                               const std::string &methodName) = 0;
38 };
39 
40 class CodeGenerator {
41 public:
CodeGenerator(std::unique_ptr<CodeGeneratorImpl> & impl,const std::string & methodName)42     CodeGenerator(std::unique_ptr<CodeGeneratorImpl> &impl, const std::string& methodName)
43         : impl_(std::move(impl)), methodName_(methodName)
44     {
45     }
46 
47     ~CodeGenerator() = default;
48 
RunForStub(Circuit * circuit,const ControlFlowGraph & graph,size_t index,const CompilationConfig * cfg)49     void RunForStub(Circuit *circuit, const ControlFlowGraph &graph, size_t index, const CompilationConfig *cfg)
50     {
51         impl_->GenerateCodeForStub(circuit, graph, index, cfg);
52     }
53 
GetMethodName()54     const std::string& GetMethodName() const
55     {
56         return methodName_;
57     }
58 
Run(Circuit * circuit,const ControlFlowGraph & graph,const CompilationConfig * cfg,const MethodLiteral * methodLiteral,const JSPandaFile * jsPandaFile)59     void Run(Circuit *circuit, const ControlFlowGraph &graph, const CompilationConfig *cfg,
60              const MethodLiteral *methodLiteral, const JSPandaFile *jsPandaFile)
61     {
62         impl_->GenerateCode(circuit, graph, cfg, methodLiteral, jsPandaFile, methodName_);
63     }
64 
65 private:
66     std::unique_ptr<CodeGeneratorImpl> impl_{nullptr};
67     std::string methodName_;
68 };
69 } // namespace panda::ecmascript::kungfu
70 #endif // ECMASCRIPT_COMPILER_CODE_GENERATOR_H
71