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