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 "circuit.h" 20 #include "stub.h" 21 22 namespace panda::ecmascript::kungfu { 23 using ControlFlowGraph = std::vector<std::vector<GateRef>>; 24 class CodeGeneratorImpl { 25 public: 26 CodeGeneratorImpl() = default; 27 virtual ~CodeGeneratorImpl() = default; 28 virtual void GenerateCodeForStub(Circuit *circuit, const ControlFlowGraph &graph, int index, 29 const CompilationConfig *cfg) = 0; 30 }; 31 32 class CodeGenerator { 33 public: CodeGenerator(std::unique_ptr<CodeGeneratorImpl> & impl)34 explicit CodeGenerator(std::unique_ptr<CodeGeneratorImpl> &impl) : impl_(std::move(impl)) {} 35 ~CodeGenerator() = default; Run(Circuit * circuit,const ControlFlowGraph & graph,int index,const CompilationConfig * cfg)36 void Run(Circuit *circuit, const ControlFlowGraph &graph, int index, const CompilationConfig *cfg) 37 { 38 impl_->GenerateCodeForStub(circuit, graph, index, cfg); 39 } 40 41 private: 42 std::unique_ptr<CodeGeneratorImpl> impl_{nullptr}; 43 }; 44 } // namespace panda::ecmascript::kungfu 45 #endif // ECMASCRIPT_COMPILER_CODE_GENERATOR_H