• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_COMBINED_PASS_VISITOR_H
17 #define ECMASCRIPT_COMPILER_COMBINED_PASS_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 RPOVisitor {
26 public:
27     virtual ~RPOVisitor() = default;
28     virtual int32_t GetGateOrder(GateRef gate) = 0;
29     virtual void SetGateOrder(GateRef gate, int32_t orderId) = 0;
30     virtual void Resize(int32_t size, int32_t num) = 0;
31     virtual void ReVisitGate(GateRef gate) = 0;
32     virtual void ReplaceGate(GateRef gate, GateRef replacement) = 0;
33     virtual void ReplaceGate(GateRef gate, StateDepend stateDepend, GateRef replacement) = 0;
34 };
35 
36 class PassVisitor {
37 public:
PassVisitor(Circuit * circuit,Chunk * chunk,RPOVisitor * visitor)38     PassVisitor(Circuit* circuit, Chunk* chunk, RPOVisitor* visitor)
39         : circuit_(circuit), acc_(circuit), chunk_(chunk), visitor_(visitor) {}
40     virtual ~PassVisitor() = default;
41 
42     virtual GateRef VisitGate(GateRef gate) = 0;
Initialize()43     virtual void Initialize() {}
Finalize()44     virtual void Finalize() {}
45 protected:
ReplaceGate(GateRef gate,GateRef replacement)46     void ReplaceGate(GateRef gate, GateRef replacement)
47     {
48         visitor_->ReplaceGate(gate, replacement);
49     }
ReplaceGate(GateRef gate,StateDepend stateDepend,GateRef replacement)50     void ReplaceGate(GateRef gate, StateDepend stateDepend, GateRef replacement)
51     {
52         visitor_->ReplaceGate(gate, stateDepend, replacement);
53     }
54     Circuit* circuit_ {nullptr};
55     GateAccessor acc_;
56     Chunk* chunk_ {nullptr};
57     RPOVisitor* visitor_;
58 };
59 
60 class CombinedPassVisitor : public RPOVisitor {
61 public:
CombinedPassVisitor(Circuit * circuit,bool enableLog,const std::string & name,Chunk * chunk)62     CombinedPassVisitor(Circuit* circuit, bool enableLog, const std::string& name, Chunk* chunk)
63         : enableLog_(enableLog), methodName_(name), circuit_(circuit), acc_(circuit),
64         chunk_(chunk), workList_(chunk), changedList_(chunk), orderList_(chunk), passList_(chunk) {}
65     virtual ~CombinedPassVisitor() = default;
66     void AddPass(PassVisitor* pass);
67 
68     int32_t GetGateOrder(GateRef gate);
69     void SetGateOrder(GateRef gate, int32_t orderId);
70     void Resize(int32_t size, int32_t num);
71 
72     void VisitGraph();
73     GateRef VisitGate(GateRef gate);
74     void ReVisitGate(GateRef gate);
75     void ReplaceGate(GateRef gate, GateRef replacement);
76     void ReplaceGate(GateRef gate, StateDepend stateDepend, GateRef replacement);
77     void PrintLog(const std::string& phaseName);
78 
79 protected:
80 
81     void VisitTopGate(Edge& current);
82 
PushGate(GateRef gate,size_t index)83     void PushGate(GateRef gate, size_t index)
84     {
85         workList_.push_back(Edge{gate, index});
86         acc_.SetMark(gate, MarkCode::VISITED);
87     }
88 
PushChangedGate(GateRef gate)89     void PushChangedGate(GateRef gate)
90     {
91         changedList_.push_back(gate);
92         acc_.SetMark(gate, MarkCode::PREVISIT);
93     }
94 
PopGate(GateRef gate)95     void PopGate(GateRef gate)
96     {
97         workList_.pop_back();
98         acc_.SetMark(gate, MarkCode::FINISHED);
99     }
100 
GetChunk()101     Chunk *GetChunk() const
102     {
103         return chunk_;
104     }
105     void PrintStack();
106 
107 private:
108     bool enableLog_ {false};
109     std::string methodName_;
110     Circuit* circuit_ {nullptr};
111     GateAccessor acc_;
112     Chunk* chunk_ {nullptr};
113     ChunkDeque<Edge> workList_;
114     ChunkDeque<GateRef> changedList_;
115     ChunkVector<int32_t> orderList_;
116     ChunkVector<PassVisitor*> passList_;
117     uint32_t orderCount_ {0};
118 };
119 }  // panda::ecmascript::kungfu
120 #endif  // ECMASCRIPT_COMPILER_COMBINED_PASS_VISITOR_H
121