1 /* 2 * Copyright (c) 2024 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_BUILTINS_COLLECTION_ITERATOR_STUB_BUILDER_H 17 #define ECMASCRIPT_COMPILER_BUILTINS_COLLECTION_ITERATOR_STUB_BUILDER_H 18 #include "ecmascript/compiler/builtins/linked_hashtable_stub_builder.h" 19 #include "ecmascript/compiler/builtins/builtins_stubs.h" 20 #include "ecmascript/js_map.h" 21 #include "ecmascript/js_map_iterator.h" 22 #include "ecmascript/js_set.h" 23 #include "ecmascript/js_set_iterator.h" 24 #include "ecmascript/linked_hash_table.h" 25 26 namespace panda::ecmascript::kungfu { 27 template <typename IteratorType> 28 class BuiltinsCollectionIteratorStubBuilder : public BuiltinsStubBuilder { 29 public: BuiltinsCollectionIteratorStubBuilder(StubBuilder * parent,GateRef glue,GateRef thisValue,GateRef numArgs)30 explicit BuiltinsCollectionIteratorStubBuilder(StubBuilder *parent, GateRef glue, GateRef thisValue, 31 GateRef numArgs) : BuiltinsStubBuilder(parent), glue_(glue), thisValue_(thisValue), numArgs_(numArgs) {} 32 ~BuiltinsCollectionIteratorStubBuilder() override = default; 33 NO_MOVE_SEMANTIC(BuiltinsCollectionIteratorStubBuilder); 34 NO_COPY_SEMANTIC(BuiltinsCollectionIteratorStubBuilder); GenerateCircuit()35 void GenerateCircuit() override {} 36 37 void Next(Variable *result, Label *exit); 38 39 private: 40 GateRef CreateIterValueForEntry(GateRef key, GateRef value); 41 // check target obj 42 void CheckCollectionIteratorObj(Label *updateIter, Label *throwException); 43 void UpdateIter(Label *iterUpdated); 44 GetIteratedLinkedObjOffset()45 GateRef GetIteratedLinkedObjOffset() 46 { 47 int32_t linkedObjOffset = 0; 48 if constexpr (std::is_same_v<IteratorType, JSMapIterator>) { 49 linkedObjOffset = JSMapIterator::ITERATED_MAP_OFFSET; 50 } else { 51 linkedObjOffset = JSSetIterator::ITERATED_SET_OFFSET; 52 } 53 return IntPtr(linkedObjOffset); 54 } 55 GetIteratedLinkedObj()56 GateRef GetIteratedLinkedObj() 57 { 58 GateRef iteratedLinkedObjOffset = GetIteratedLinkedObjOffset(); 59 return Load(VariableType::JS_ANY(), thisValue_, iteratedLinkedObjOffset); 60 } 61 SetIteratedLinkedObj(GateRef newLinkedObj)62 void SetIteratedLinkedObj(GateRef newLinkedObj) 63 { 64 GateRef iteratedLinkedObjOffset = GetIteratedLinkedObjOffset(); 65 Store(VariableType::JS_ANY(), glue_, thisValue_, iteratedLinkedObjOffset, newLinkedObj); 66 } 67 GetNextIndexOffset()68 GateRef GetNextIndexOffset() 69 { 70 return IntPtr(IteratorType::NEXT_INDEX_OFFSET); 71 } 72 GetNextIndex()73 GateRef GetNextIndex() 74 { 75 GateRef nextIndexOffset = GetNextIndexOffset(); 76 return Load(VariableType::INT32(), thisValue_, nextIndexOffset); 77 } 78 SetNextIndex(GateRef newNextIndex)79 void SetNextIndex(GateRef newNextIndex) 80 { 81 GateRef nextIndexOffset = GetNextIndexOffset(); 82 Store(VariableType::INT32(), glue_, thisValue_, nextIndexOffset, newNextIndex); 83 } 84 GetIterationKind()85 GateRef GetIterationKind() 86 { 87 GateRef bitFieldOffset = IntPtr(IteratorType::BIT_FIELD_OFFSET); 88 GateRef bitField = Load(VariableType::INT32(), thisValue_, bitFieldOffset); 89 // decode 90 GateRef mask = Int32((1LLU << IteratorType::ITERATION_KIND_BITS) - 1); 91 return Int32And(bitField, mask); 92 } 93 GetNextTable(GateRef iteratedLinkedObj)94 GateRef GetNextTable(GateRef iteratedLinkedObj) 95 { 96 if constexpr (std::is_same_v<IteratorType, JSMapIterator>) { 97 LinkedHashTableStubBuilder<LinkedHashMap, LinkedHashMapObject> linkedHashTableStubBuilder(this, glue_); 98 return linkedHashTableStubBuilder.GetNextTable(iteratedLinkedObj); 99 } else { 100 LinkedHashTableStubBuilder<LinkedHashSet, LinkedHashSetObject> linkedHashTableStubBuilder(this, glue_); 101 return linkedHashTableStubBuilder.GetNextTable(iteratedLinkedObj); 102 } 103 } 104 105 GateRef glue_; 106 GateRef thisValue_; 107 GateRef numArgs_; 108 }; 109 } // namespace panda::ecmascript::kungfu 110 #endif // ECMASCRIPT_COMPILER_BUILTINS_COLLECTION_ITERATOR_STUB_BUILDER_H