1 /* 2 * Copyright (c) 2023 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_GRAPH_VISITOR_H 17 #define ECMASCRIPT_COMPILER_GRAPH_VISITOR_H 18 19 #include "ecmascript/compiler/circuit_builder.h" 20 #include "ecmascript/compiler/gate_accessor.h" 21 #include "ecmascript/mem/chunk_containers.h" 22 23 namespace panda::ecmascript::kungfu { 24 25 class GraphVisitor { 26 public: GraphVisitor(Circuit * circuit,Chunk * chunk)27 GraphVisitor(Circuit *circuit, Chunk* chunk) 28 : circuit_(circuit), acc_(circuit), 29 chunk_(chunk), workList_(chunk), changedList_(chunk), orderList_(chunk) {} 30 31 virtual ~GraphVisitor() = default; 32 33 void VisitGraph(); 34 void ReVisitGate(GateRef gate); 35 int32_t GetGateOrder(GateRef gate) const; 36 void SetGateOrder(GateRef gate, int32_t orderId); 37 38 virtual GateRef VisitGate(GateRef gate) = 0; 39 protected: 40 void ReplaceGate(GateRef gate, GateRef replacement); 41 void ReplaceGate(GateRef gate, StateDepend stateDepend, GateRef replacement); 42 void VisitTopGate(Edge& current); 43 PushGate(GateRef gate,size_t index)44 void PushGate(GateRef gate, size_t index) 45 { 46 workList_.push_back(Edge{gate, index}); 47 acc_.SetMark(gate, MarkCode::VISITED); 48 } 49 PushChangedGate(GateRef gate)50 void PushChangedGate(GateRef gate) 51 { 52 changedList_.push_back(gate); 53 acc_.SetMark(gate, MarkCode::PREVISIT); 54 } 55 PopGate(GateRef gate)56 void PopGate(GateRef gate) 57 { 58 workList_.pop_back(); 59 acc_.SetMark(gate, MarkCode::FINISHED); 60 } 61 GetChunk()62 Chunk *GetChunk() const 63 { 64 return chunk_; 65 } 66 void PrintStack(); 67 68 Circuit *circuit_ {nullptr}; 69 GateAccessor acc_; 70 Chunk* chunk_ {nullptr}; 71 ChunkDeque<Edge> workList_; 72 ChunkDeque<GateRef> changedList_; 73 ChunkVector<int32_t> orderList_; 74 uint32_t orderCount_ {0}; 75 }; 76 } // panda::ecmascript::kungfu 77 #endif // ECMASCRIPT_COMPILER_GRAPH_VISITOR_H