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/compiler/stub_builder.h" 21 #include "ecmascript/jspandafile/method_literal.h" 22 23 namespace panda::ecmascript::kungfu { 24 using ControlFlowGraph = std::vector<std::vector<GateRef>>; 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) = 0; 37 }; 38 39 class CodeGenerator { 40 public: CodeGenerator(std::unique_ptr<CodeGeneratorImpl> & impl,const std::string & methodName)41 explicit CodeGenerator(std::unique_ptr<CodeGeneratorImpl> &impl, const std::string& methodName) 42 : impl_(std::move(impl)), methodName_(methodName) 43 { 44 } 45 46 ~CodeGenerator() = default; 47 RunForStub(Circuit * circuit,const ControlFlowGraph & graph,size_t index,const CompilationConfig * cfg)48 void RunForStub(Circuit *circuit, const ControlFlowGraph &graph, size_t index, const CompilationConfig *cfg) 49 { 50 impl_->GenerateCodeForStub(circuit, graph, index, cfg); 51 } 52 GetMethodName()53 const std::string& GetMethodName() const 54 { 55 return methodName_; 56 } 57 Run(Circuit * circuit,const ControlFlowGraph & graph,const CompilationConfig * cfg,const MethodLiteral * methodLiteral,const JSPandaFile * jsPandaFile)58 void Run(Circuit *circuit, const ControlFlowGraph &graph, const CompilationConfig *cfg, 59 const MethodLiteral *methodLiteral, const JSPandaFile *jsPandaFile) 60 { 61 impl_->GenerateCode(circuit, graph, cfg, methodLiteral, jsPandaFile); 62 } 63 64 private: 65 std::unique_ptr<CodeGeneratorImpl> impl_{nullptr}; 66 std::string methodName_; 67 }; 68 } // namespace panda::ecmascript::kungfu 69 #endif // ECMASCRIPT_COMPILER_CODE_GENERATOR_H 70