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_RANGE_GUARD_H 17 #define ECMASCRIPT_COMPILER_RANGE_GUARD_H 18 19 #include "ecmascript/compiler/circuit_builder.h" 20 #include "ecmascript/compiler/gate_accessor.h" 21 #include "ecmascript/compiler/graph_visitor.h" 22 #include "ecmascript/compiler/pass_manager.h" 23 #include "ecmascript/compiler/base/depend_chain_helper.h" 24 #include "ecmascript/mem/chunk_containers.h" 25 #include "ecmascript/compiler/number_gate_info.h" 26 27 namespace panda::ecmascript::kungfu { 28 class DependChains; 29 class RangeGuard : public GraphVisitor { 30 public: RangeGuard(Circuit * circuit,Chunk * chunk)31 RangeGuard(Circuit *circuit, Chunk* chunk) 32 : GraphVisitor(circuit, chunk), circuit_(circuit), 33 builder_(circuit), dependChains_(chunk) {} 34 35 ~RangeGuard() = default; 36 37 void Run(); 38 39 GateRef VisitGate(GateRef gate) override; 40 bool CheckInputSource(GateRef lhs, GateRef rhs); 41 uint32_t CheckIndexCheckLengthInput(GateRef lhs, GateRef rhs); 42 uint32_t CheckIndexCheckIndexInput(GateRef lhs, GateRef rhs); 43 private: 44 GetDependChain(GateRef dependIn)45 DependChains* GetDependChain(GateRef dependIn) 46 { 47 size_t idx = acc_.GetId(dependIn); 48 ASSERT(idx <= circuit_->GetMaxGateId()); 49 return dependChains_[idx]; 50 } 51 52 GateRef VisitDependEntry(GateRef gate); 53 GateRef UpdateDependChain(GateRef gate, DependChains* dependInfo); 54 GateRef TryApplyRangeGuardForLength(DependChains* dependInfo, GateRef gate, GateRef input); 55 GateRef TryApplyRangeGuardForIndex(DependChains* dependInfo, GateRef gate, GateRef input); 56 GateRef TryApplyRangeGuardGate(GateRef gate); 57 GateRef TraverseOthers(GateRef gate); 58 GateRef TraverseDependSelector(GateRef gate); 59 60 Circuit* circuit_; 61 CircuitBuilder builder_; 62 ChunkVector<DependChains*> dependChains_; 63 64 friend class RangeInfo; 65 }; 66 } // panda::ecmascript::kungfu 67 #endif // ECMASCRIPT_COMPILER_RANGE_GUARD_H