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