14514f5e3Sopenharmony_ci/*
24514f5e3Sopenharmony_ci * Copyright (c) 2022-2024 Huawei Device Co., Ltd.
34514f5e3Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
44514f5e3Sopenharmony_ci * you may not use this file except in compliance with the License.
54514f5e3Sopenharmony_ci * You may obtain a copy of the License at
64514f5e3Sopenharmony_ci *
74514f5e3Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
84514f5e3Sopenharmony_ci *
94514f5e3Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
104514f5e3Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
114514f5e3Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
124514f5e3Sopenharmony_ci * See the License for the specific language governing permissions and
134514f5e3Sopenharmony_ci * limitations under the License.
144514f5e3Sopenharmony_ci */
154514f5e3Sopenharmony_ci
164514f5e3Sopenharmony_ci#ifndef ECMASCRIPT_COMPILER_BUILTINS_STRING_STUB_BUILDER_H
174514f5e3Sopenharmony_ci#define ECMASCRIPT_COMPILER_BUILTINS_STRING_STUB_BUILDER_H
184514f5e3Sopenharmony_ci#include "ecmascript/compiler/stub_builder-inl.h"
194514f5e3Sopenharmony_ci#include "ecmascript/compiler/builtins/builtins_stubs.h"
204514f5e3Sopenharmony_ci
214514f5e3Sopenharmony_cinamespace panda::ecmascript::kungfu {
224514f5e3Sopenharmony_ciclass FlatStringStubBuilder;
234514f5e3Sopenharmony_cistruct StringInfoGateRef;
244514f5e3Sopenharmony_ci
254514f5e3Sopenharmony_ciclass BuiltinsStringStubBuilder : public BuiltinsStubBuilder {
264514f5e3Sopenharmony_cipublic:
274514f5e3Sopenharmony_ci    explicit BuiltinsStringStubBuilder(StubBuilder *parent)
284514f5e3Sopenharmony_ci        : BuiltinsStubBuilder(parent) {}
294514f5e3Sopenharmony_ci    BuiltinsStringStubBuilder(CallSignature *callSignature, Environment *env)
304514f5e3Sopenharmony_ci        : BuiltinsStubBuilder(callSignature, env) {}
314514f5e3Sopenharmony_ci    explicit BuiltinsStringStubBuilder(Environment* env): BuiltinsStubBuilder(env) {}
324514f5e3Sopenharmony_ci    ~BuiltinsStringStubBuilder() override = default;
334514f5e3Sopenharmony_ci    NO_MOVE_SEMANTIC(BuiltinsStringStubBuilder);
344514f5e3Sopenharmony_ci    NO_COPY_SEMANTIC(BuiltinsStringStubBuilder);
354514f5e3Sopenharmony_ci    void GenerateCircuit() override {}
364514f5e3Sopenharmony_ci
374514f5e3Sopenharmony_ci#define DECLARE_BUILTINS_SRRING_STUB_BUILDER(method, ...)           \
384514f5e3Sopenharmony_ci    void method(GateRef glue, GateRef thisValue, GateRef numArgs, Variable* res, Label *exit, Label *slowPath);
394514f5e3Sopenharmony_ciBUILTINS_WITH_STRING_STUB_BUILDER(DECLARE_BUILTINS_SRRING_STUB_BUILDER)
404514f5e3Sopenharmony_ci#undef DECLARE_BUILTINS_SRRING_STUB_BUILDER
414514f5e3Sopenharmony_ci
424514f5e3Sopenharmony_ci    void LocaleCompare(GateRef glue, GateRef thisValue, GateRef numArgs, Variable *res, Label *exit, Label *slowPath);
434514f5e3Sopenharmony_ci    void StringIteratorNext(GateRef glue, GateRef thisValue, GateRef numArgs,
444514f5e3Sopenharmony_ci                            Variable *res, Label *exit, Label *slowPath);
454514f5e3Sopenharmony_ci
464514f5e3Sopenharmony_ci    GateRef ConvertAndClampRelativeIndex(GateRef index, GateRef length);
474514f5e3Sopenharmony_ci    GateRef StringAt(const StringInfoGateRef &stringInfoGate, GateRef index);
484514f5e3Sopenharmony_ci    GateRef FastSubString(GateRef glue, GateRef thisValue, GateRef from, GateRef len,
494514f5e3Sopenharmony_ci        const StringInfoGateRef &stringInfoGate);
504514f5e3Sopenharmony_ci    GateRef FastSubUtf8String(GateRef glue, GateRef from, GateRef len, const StringInfoGateRef &stringInfoGate);
514514f5e3Sopenharmony_ci    GateRef FastSubUtf16String(GateRef glue, GateRef from, GateRef len, const StringInfoGateRef &stringInfoGate);
524514f5e3Sopenharmony_ci    GateRef FastStringCharCodeAt(GateRef glue, GateRef thisValue, GateRef pos);
534514f5e3Sopenharmony_ci    GateRef GetSubstitution(GateRef glue, GateRef searchString, GateRef thisString,
544514f5e3Sopenharmony_ci        GateRef pos, GateRef replaceString);
554514f5e3Sopenharmony_ci    void CopyChars(GateRef glue, GateRef dst, GateRef source, GateRef sourceLength, GateRef size, VariableType type);
564514f5e3Sopenharmony_ci    void CopyUtf16AsUtf8(GateRef glue, GateRef dst, GateRef src, GateRef sourceLength);
574514f5e3Sopenharmony_ci    void CopyUtf8AsUtf16(GateRef glue, GateRef dst, GateRef src, GateRef sourceLength);
584514f5e3Sopenharmony_ci    GateRef StringIndexOf(GateRef lhsData, bool lhsIsUtf8, GateRef rhsData, bool rhsIsUtf8,
594514f5e3Sopenharmony_ci                          GateRef pos, GateRef max, GateRef rhsCount);
604514f5e3Sopenharmony_ci    GateRef StringIndexOf(const StringInfoGateRef &lStringInfoGate,
614514f5e3Sopenharmony_ci        const StringInfoGateRef &rStringInfoGate, GateRef pos);
624514f5e3Sopenharmony_ci    GateRef GetSingleCharCodeByIndex(GateRef str, GateRef index);
634514f5e3Sopenharmony_ci    GateRef CreateStringBySingleCharCode(GateRef glue, GateRef charCode);
644514f5e3Sopenharmony_ci    GateRef CreateFromEcmaString(GateRef glue, GateRef index, const StringInfoGateRef &stringInfoGate);
654514f5e3Sopenharmony_ci    GateRef StringConcat(GateRef glue, GateRef leftString, GateRef rightString);
664514f5e3Sopenharmony_ci    GateRef EcmaStringTrim(GateRef glue, GateRef srcString, GateRef trimMode);
674514f5e3Sopenharmony_ci    GateRef EcmaStringTrimBody(GateRef glue, GateRef thisValue, StringInfoGateRef srcStringInfoGate,
684514f5e3Sopenharmony_ci        GateRef trimMode, GateRef isUtf8);
694514f5e3Sopenharmony_ci    void StoreParent(GateRef glue, GateRef object, GateRef parent);
704514f5e3Sopenharmony_ci    void StoreStartIndex(GateRef glue, GateRef object, GateRef startIndex);
714514f5e3Sopenharmony_ci    void StoreHasBackingStore(GateRef glue, GateRef object, GateRef hasBackingStore);
724514f5e3Sopenharmony_ci    GateRef IsSubStringAt(GateRef lhsData, bool lhsIsUtf8, GateRef rhsData, bool rhsIsUtf8,
734514f5e3Sopenharmony_ci        GateRef pos, GateRef rhsCount);
744514f5e3Sopenharmony_ci    GateRef IsSubStringAt(const StringInfoGateRef &lStringInfoGate,
754514f5e3Sopenharmony_ci        const StringInfoGateRef &rStringInfoGate, GateRef pos);
764514f5e3Sopenharmony_ci    GateRef GetSubString(GateRef glue, GateRef thisValue, GateRef from, GateRef len);
774514f5e3Sopenharmony_ci    GateRef GetFastSubString(GateRef glue, GateRef thisValue, GateRef start, GateRef len);
784514f5e3Sopenharmony_ciprivate:
794514f5e3Sopenharmony_ci    GateRef ChangeStringTaggedPointerToInt64(GateRef x)
804514f5e3Sopenharmony_ci    {
814514f5e3Sopenharmony_ci        return GetEnvironment()->GetBuilder()->ChangeTaggedPointerToInt64(x);
824514f5e3Sopenharmony_ci    }
834514f5e3Sopenharmony_ci    GateRef GetStringDataFromLineOrConstantString(GateRef str);
844514f5e3Sopenharmony_ci    GateRef CanBeCompressed(GateRef utf16Data, GateRef utf16Len, bool isUtf16);
854514f5e3Sopenharmony_ci    GateRef GetUtf16Data(GateRef stringData, GateRef index);
864514f5e3Sopenharmony_ci    GateRef IsASCIICharacter(GateRef data);
874514f5e3Sopenharmony_ci    GateRef GetUtf8Data(GateRef stringData, GateRef index);
884514f5e3Sopenharmony_ci    GateRef GetSingleCharCodeFromConstantString(GateRef str, GateRef index);
894514f5e3Sopenharmony_ci    GateRef GetSingleCharCodeFromLineString(GateRef str, GateRef index);
904514f5e3Sopenharmony_ci    GateRef GetSingleCharCodeFromSlicedString(GateRef str, GateRef index);
914514f5e3Sopenharmony_ci    void CheckParamsAndGetPosition(GateRef glue, GateRef thisValue, GateRef numArgs,
924514f5e3Sopenharmony_ci        Variable* pos, Label *exit, Label *slowPath, Label *posIsValid);
934514f5e3Sopenharmony_ci};
944514f5e3Sopenharmony_ci
954514f5e3Sopenharmony_ciclass FlatStringStubBuilder : public StubBuilder {
964514f5e3Sopenharmony_cipublic:
974514f5e3Sopenharmony_ci    explicit FlatStringStubBuilder(StubBuilder *parent)
984514f5e3Sopenharmony_ci        : StubBuilder(parent) {}
994514f5e3Sopenharmony_ci    ~FlatStringStubBuilder() override = default;
1004514f5e3Sopenharmony_ci    NO_MOVE_SEMANTIC(FlatStringStubBuilder);
1014514f5e3Sopenharmony_ci    NO_COPY_SEMANTIC(FlatStringStubBuilder);
1024514f5e3Sopenharmony_ci    void GenerateCircuit() override {}
1034514f5e3Sopenharmony_ci
1044514f5e3Sopenharmony_ci    void FlattenString(GateRef glue, GateRef str, Label *fastPath);
1054514f5e3Sopenharmony_ci    void FlattenStringWithIndex(GateRef glue, GateRef str, Variable *index, Label *fastPath);
1064514f5e3Sopenharmony_ci    GateRef GetParentFromSlicedString(GateRef string)
1074514f5e3Sopenharmony_ci    {
1084514f5e3Sopenharmony_ci        GateRef offset = IntPtr(SlicedString::PARENT_OFFSET);
1094514f5e3Sopenharmony_ci        return Load(VariableType::JS_POINTER(), string, offset);
1104514f5e3Sopenharmony_ci    }
1114514f5e3Sopenharmony_ci    GateRef GetStartIndexFromSlicedString(GateRef string)
1124514f5e3Sopenharmony_ci    {
1134514f5e3Sopenharmony_ci        GateRef offset = IntPtr(SlicedString::STARTINDEX_OFFSET);
1144514f5e3Sopenharmony_ci        return Load(VariableType::INT32(), string, offset);
1154514f5e3Sopenharmony_ci    }
1164514f5e3Sopenharmony_ci    GateRef GetHasBackingStoreFromSlicedString(GateRef string)
1174514f5e3Sopenharmony_ci    {
1184514f5e3Sopenharmony_ci        GateRef offset = IntPtr(SlicedString::BACKING_STORE_FLAG);
1194514f5e3Sopenharmony_ci        return Load(VariableType::INT32(), string, offset);
1204514f5e3Sopenharmony_ci    }
1214514f5e3Sopenharmony_ci
1224514f5e3Sopenharmony_ci    GateRef GetFlatString()
1234514f5e3Sopenharmony_ci    {
1244514f5e3Sopenharmony_ci        return flatString_.ReadVariable();
1254514f5e3Sopenharmony_ci    }
1264514f5e3Sopenharmony_ci
1274514f5e3Sopenharmony_ci    GateRef GetStartIndex()
1284514f5e3Sopenharmony_ci    {
1294514f5e3Sopenharmony_ci        return startIndex_.ReadVariable();
1304514f5e3Sopenharmony_ci    }
1314514f5e3Sopenharmony_ci
1324514f5e3Sopenharmony_ci    GateRef GetLength()
1334514f5e3Sopenharmony_ci    {
1344514f5e3Sopenharmony_ci        return length_;
1354514f5e3Sopenharmony_ci    }
1364514f5e3Sopenharmony_ci
1374514f5e3Sopenharmony_ciprivate:
1384514f5e3Sopenharmony_ci    Variable flatString_ { GetEnvironment(), VariableType::JS_POINTER(), NextVariableId(), Undefined() };
1394514f5e3Sopenharmony_ci    Variable startIndex_ { GetEnvironment(), VariableType::INT32(), NextVariableId(), Int32(0) };
1404514f5e3Sopenharmony_ci    GateRef length_ { Circuit::NullGate() };
1414514f5e3Sopenharmony_ci};
1424514f5e3Sopenharmony_ci
1434514f5e3Sopenharmony_cistruct StringInfoGateRef {
1444514f5e3Sopenharmony_ci    GateRef string_ { Circuit::NullGate() };
1454514f5e3Sopenharmony_ci    GateRef startIndex_ { Circuit::NullGate() };
1464514f5e3Sopenharmony_ci    GateRef length_ { Circuit::NullGate() };
1474514f5e3Sopenharmony_ci    StringInfoGateRef(FlatStringStubBuilder *flatString) : string_(flatString->GetFlatString()),
1484514f5e3Sopenharmony_ci                                                           startIndex_(flatString->GetStartIndex()),
1494514f5e3Sopenharmony_ci                                                           length_(flatString->GetLength()) {}
1504514f5e3Sopenharmony_ci    GateRef GetString() const
1514514f5e3Sopenharmony_ci    {
1524514f5e3Sopenharmony_ci        return string_;
1534514f5e3Sopenharmony_ci    }
1544514f5e3Sopenharmony_ci
1554514f5e3Sopenharmony_ci    GateRef GetStartIndex() const
1564514f5e3Sopenharmony_ci    {
1574514f5e3Sopenharmony_ci        return startIndex_;
1584514f5e3Sopenharmony_ci    }
1594514f5e3Sopenharmony_ci
1604514f5e3Sopenharmony_ci    GateRef GetLength() const
1614514f5e3Sopenharmony_ci    {
1624514f5e3Sopenharmony_ci        return length_;
1634514f5e3Sopenharmony_ci    }
1644514f5e3Sopenharmony_ci};
1654514f5e3Sopenharmony_ci}  // namespace panda::ecmascript::kungfu
1664514f5e3Sopenharmony_ci#endif  // ECMASCRIPT_COMPILER_BUILTINS_STRING_STUB_BUILDER_H