1 /* 2 * Copyright (c) 2022 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_STRING_STUB_BUILDER_H 17 #define ECMASCRIPT_COMPILER_BUILTINS_STRING_STUB_BUILDER_H 18 #include "ecmascript/compiler/stub_builder-inl.h" 19 20 namespace panda::ecmascript::kungfu { 21 class FlatStringStubBuilder; 22 struct StringInfoGateRef; 23 24 class BuiltinsStringStubBuilder : public BuiltinsStubBuilder { 25 public: BuiltinsStringStubBuilder(StubBuilder * parent)26 explicit BuiltinsStringStubBuilder(StubBuilder *parent) 27 : BuiltinsStubBuilder(parent) {} BuiltinsStringStubBuilder(CallSignature * callSignature,Environment * env)28 BuiltinsStringStubBuilder(CallSignature *callSignature, Environment *env) 29 : BuiltinsStubBuilder(callSignature, env) {} 30 ~BuiltinsStringStubBuilder() override = default; 31 NO_MOVE_SEMANTIC(BuiltinsStringStubBuilder); 32 NO_COPY_SEMANTIC(BuiltinsStringStubBuilder); GenerateCircuit()33 void GenerateCircuit() override {} 34 35 void FromCharCode(GateRef glue, GateRef thisValue, GateRef numArgs, Variable* res, Label *exit, Label *slowPath); 36 void CharAt(GateRef glue, GateRef thisValue, GateRef numArgs, Variable* res, Label *exit, Label *slowPath); 37 void CharCodeAt(GateRef glue, GateRef thisValue, GateRef numArgs, Variable* res, Label *exit, Label *slowPath); 38 void IndexOf(GateRef glue, GateRef thisValue, GateRef numArgs, Variable* res, Label *exit, Label *slowPath); 39 void Substring(GateRef glue, GateRef thisValue, GateRef numArgs, Variable* res, Label *exit, Label *slowPath); 40 void Replace(GateRef glue, GateRef thisValue, GateRef numArgs, Variable *res, Label *exit, Label *slowPath); 41 void Trim(GateRef glue, GateRef thisValue, GateRef numArgs, Variable *res, Label *exit, Label *slowPath); 42 void Slice(GateRef glue, GateRef thisValue, GateRef numArgs, Variable *res, Label *exit, Label *slowPath); 43 void LocaleCompare(GateRef glue, GateRef thisValue, GateRef numArgs, Variable *res, Label *exit, Label *slowPath); 44 45 GateRef ConvertAndClampRelativeIndex(GateRef index, GateRef length); 46 GateRef StringAt(const StringInfoGateRef &stringInfoGate, GateRef index); 47 GateRef FastSubString(GateRef glue, GateRef thisValue, GateRef from, GateRef len, 48 const StringInfoGateRef &stringInfoGate); 49 GateRef FastSubUtf8String(GateRef glue, GateRef from, GateRef len, const StringInfoGateRef &stringInfoGate); 50 GateRef FastSubUtf16String(GateRef glue, GateRef from, GateRef len, const StringInfoGateRef &stringInfoGate); 51 GateRef GetSubstitution(GateRef glue, GateRef searchString, GateRef thisString, 52 GateRef pos, GateRef replaceString); 53 void CopyChars(GateRef glue, GateRef dst, GateRef source, GateRef sourceLength, GateRef size, VariableType type); 54 void CopyUtf16AsUtf8(GateRef glue, GateRef dst, GateRef src, GateRef sourceLength); 55 void CopyUtf8AsUtf16(GateRef glue, GateRef dst, GateRef src, GateRef sourceLength); 56 GateRef StringIndexOf(GateRef lhsData, bool lhsIsUtf8, GateRef rhsData, bool rhsIsUtf8, 57 GateRef pos, GateRef max, GateRef rhsCount); 58 GateRef StringIndexOf(const StringInfoGateRef &lStringInfoGate, 59 const StringInfoGateRef &rStringInfoGate, GateRef pos); 60 GateRef GetSingleCharCodeByIndex(GateRef str, GateRef index); 61 GateRef CreateStringBySingleCharCode(GateRef glue, GateRef charCode); 62 GateRef CreateFromEcmaString(GateRef glue, GateRef index, const StringInfoGateRef &stringInfoGate); 63 GateRef StringConcat(GateRef glue, GateRef leftString, GateRef rightString); 64 GateRef EcmaStringTrim(GateRef glue, GateRef srcString, GateRef trimMode); 65 GateRef EcmaStringTrimBody(GateRef glue, GateRef thisValue, StringInfoGateRef srcStringInfoGate, 66 GateRef trimMode, GateRef isUtf8); 67 void StoreParent(GateRef glue, GateRef object, GateRef parent); 68 void StoreStartIndex(GateRef glue, GateRef object, GateRef startIndex); 69 void StoreHasBackingStore(GateRef glue, GateRef object, GateRef hasBackingStore); 70 private: ChangeStringTaggedPointerToInt64(GateRef x)71 GateRef ChangeStringTaggedPointerToInt64(GateRef x) 72 { 73 return GetEnvironment()->GetBuilder()->ChangeTaggedPointerToInt64(x); 74 } 75 GateRef GetStringDataFromLineOrConstantString(GateRef str); 76 GateRef CanBeCompressed(GateRef utf16Data, GateRef utf16Len, bool isUtf16); 77 GateRef GetUtf16Data(GateRef stringData, GateRef index); 78 GateRef IsASCIICharacter(GateRef data); 79 GateRef GetUtf8Data(GateRef stringData, GateRef index); 80 GateRef GetSingleCharCodeFromConstantString(GateRef str, GateRef index); 81 GateRef GetSingleCharCodeFromLineString(GateRef str, GateRef index); 82 GateRef GetSingleCharCodeFromSlicedString(GateRef str, GateRef index); 83 GateRef GetSubString(GateRef glue, GateRef thisValue, GateRef from, GateRef len); 84 }; 85 86 class FlatStringStubBuilder : public StubBuilder { 87 public: FlatStringStubBuilder(StubBuilder * parent)88 explicit FlatStringStubBuilder(StubBuilder *parent) 89 : StubBuilder(parent) {} 90 ~FlatStringStubBuilder() override = default; 91 NO_MOVE_SEMANTIC(FlatStringStubBuilder); 92 NO_COPY_SEMANTIC(FlatStringStubBuilder); GenerateCircuit()93 void GenerateCircuit() override {} 94 95 void FlattenString(GateRef glue, GateRef str, Label *fastPath); GetParentFromSlicedString(GateRef string)96 GateRef GetParentFromSlicedString(GateRef string) 97 { 98 GateRef offset = IntPtr(SlicedString::PARENT_OFFSET); 99 return Load(VariableType::JS_POINTER(), string, offset); 100 } GetStartIndexFromSlicedString(GateRef string)101 GateRef GetStartIndexFromSlicedString(GateRef string) 102 { 103 GateRef offset = IntPtr(SlicedString::STARTINDEX_OFFSET); 104 return Load(VariableType::INT32(), string, offset); 105 } GetHasBackingStoreFromSlicedString(GateRef string)106 GateRef GetHasBackingStoreFromSlicedString(GateRef string) 107 { 108 GateRef offset = IntPtr(SlicedString::BACKING_STORE_FLAG); 109 return Load(VariableType::INT32(), string, offset); 110 } 111 GetFlatString()112 GateRef GetFlatString() 113 { 114 return flatString_.ReadVariable(); 115 } 116 GetStartIndex()117 GateRef GetStartIndex() 118 { 119 return startIndex_.ReadVariable(); 120 } 121 GetLength()122 GateRef GetLength() 123 { 124 return length_; 125 } 126 127 private: 128 Variable flatString_ { GetEnvironment(), VariableType::JS_POINTER(), NextVariableId(), Undefined() }; 129 Variable startIndex_ { GetEnvironment(), VariableType::INT32(), NextVariableId(), Int32(0) }; 130 GateRef length_ { Circuit::NullGate() }; 131 }; 132 133 struct StringInfoGateRef { 134 GateRef string_ { Circuit::NullGate() }; 135 GateRef startIndex_ { Circuit::NullGate() }; 136 GateRef length_ { Circuit::NullGate() }; StringInfoGateRefStringInfoGateRef137 StringInfoGateRef(FlatStringStubBuilder *flatString) : string_(flatString->GetFlatString()), 138 startIndex_(flatString->GetStartIndex()), 139 length_(flatString->GetLength()) {} GetStringStringInfoGateRef140 GateRef GetString() const 141 { 142 return string_; 143 } 144 GetStartIndexStringInfoGateRef145 GateRef GetStartIndex() const 146 { 147 return startIndex_; 148 } 149 GetLengthStringInfoGateRef150 GateRef GetLength() const 151 { 152 return length_; 153 } 154 }; 155 } // namespace panda::ecmascript::kungfu 156 #endif // ECMASCRIPT_COMPILER_BUILTINS_STRING_STUB_BUILDER_H