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_GATE_ACCESSOR_H 17 #define ECMASCRIPT_COMPILER_GATE_ACCESSOR_H 18 19 #include "circuit.h" 20 #include "gate.h" 21 22 namespace panda::ecmascript::kungfu { 23 class UseIterator { 24 public: 25 explicit UseIterator(Circuit *circuit, GateRef gate); 26 ~UseIterator() = default; 27 GetUse()28 GateRef GetUse() const 29 { 30 return current_; 31 } 32 GetIdx()33 size_t GetIdx() const 34 { 35 return currentIdx_; 36 } 37 IsEnd()38 bool IsEnd() const 39 { 40 Gate *curGatePtr = circuit_->LoadGatePtr(current_); 41 Out *use = curGatePtr->GetOut(currentIdx_); 42 return use->IsNextOutNull(); 43 } 44 45 UseIterator &operator++() 46 { 47 ASSERT(!IsEnd()); 48 Gate *curGatePtr = circuit_->LoadGatePtr(current_); 49 Out *use = curGatePtr->GetOut(currentIdx_); 50 Out *nextUse = use->GetNextOut(); 51 current_ = circuit_->SaveGatePtr(nextUse->GetGate()); 52 currentIdx_ = nextUse->GetIndex(); 53 return *this; 54 } 55 56 private: 57 Circuit *circuit_; 58 GateRef current_; 59 size_t currentIdx_; 60 }; 61 62 class GateAccessor { 63 public: GateAccessor(Circuit * circuit)64 explicit GateAccessor(Circuit *circuit) : circuit_(circuit) {} 65 ~GateAccessor() = default; 66 67 [[nodiscard]] bool HasUse(GateRef gate); 68 [[nodiscard]] size_t GetNumIns(GateRef gate); 69 [[nodiscard]] OpCode GetOpCode(GateRef gate); 70 void SetOpCode(GateRef gate, OpCode::Op opcode); 71 [[nodiscard]] GateId GetId(GateRef gate); 72 [[nodiscard]] GateRef GetValueIn(GateRef gate, size_t idx); 73 [[nodiscard]] size_t GetNumValueIn(GateRef gate); 74 [[nodiscard]] GateRef GetIn(GateRef gate, size_t idx); 75 [[nodiscard]] GateRef GetState(GateRef gate, size_t idx = 0); 76 [[nodiscard]] GateRef GetDep(GateRef gate, size_t idx = 0); 77 void SetDep(GateRef gate, GateRef depGate, size_t idx = 0); 78 [[nodiscard]] size_t GetFirstUseIdx(GateRef gate); 79 void ReplaceIn(UseIterator &it, GateRef replaceGate); 80 81 private: 82 Circuit *circuit_; 83 }; 84 } 85 #endif // ECMASCRIPT_COMPILER_GATE_ACCESSOR_H 86