• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "ecmascript/compiler/gate_accessor.h"
16 #include "ecmascript/compiler/verifier.h"
17 #include "ecmascript/compiler/ts_hcr_lowering.h"
18 #include "ecmascript/compiler/type_mcr_lowering.h"
19 #include "ecmascript/mem/native_area_allocator.h"
20 #include "ecmascript/pgo_profiler/pgo_profiler_type.h"
21 #include "ecmascript/tests/test_helper.h"
22 
23 namespace panda::test {
24 class LoweringRelateGateTests : public testing::Test {
25 };
26 using ecmascript::GlobalEnvConstants;
27 using ecmascript::ConstantIndex;
28 using ecmascript::RegionSpaceFlag;
29 using ecmascript::kungfu::Circuit;
30 using ecmascript::kungfu::GateAccessor;
31 using ecmascript::kungfu::OpCode;
32 using ecmascript::kungfu::GateType;
33 using ecmascript::kungfu::MachineType;
34 using ecmascript::kungfu::CircuitBuilder;
35 using ecmascript::kungfu::Verifier;
36 using ecmascript::kungfu::TypedBinOp;
37 using ecmascript::kungfu::Label;
38 using ecmascript::kungfu::Variable;
39 using ecmascript::kungfu::VariableType;
40 using ecmascript::kungfu::Environment;
41 using ecmascript::kungfu::TypeMCRLowering;
42 using ecmascript::kungfu::TSHCRLowering;
43 using ecmascript::kungfu::CompilationConfig;
44 
HWTEST_F_L0(LoweringRelateGateTests,TypeCheckFramework)45 HWTEST_F_L0(LoweringRelateGateTests, TypeCheckFramework)
46 {
47     // construct a circuit
48     ecmascript::NativeAreaAllocator allocator;
49     Circuit circuit(&allocator);
50     CircuitBuilder builder(&circuit);
51     GateAccessor acc(&circuit);
52     Environment env(1, &builder);
53     builder.SetEnvironment(&env);
54     auto depend = acc.GetDependRoot();
55     auto state = acc.GetStateRoot();
56     auto arg0 = builder.Arguments(0);
57     auto pcGate = circuit.GetConstantGate(MachineType::I64, 0, GateType::NJSValue());
58     auto frameArgs = circuit.NewGate(
59         circuit.FrameArgs(), {builder.Arguments(3), builder.Arguments(4), builder.Arguments(5), builder.Arguments(2)});
60     auto frameState = circuit.NewGate(circuit.FrameState(1), {pcGate, frameArgs});
61     auto stateSplit = circuit.NewGate(circuit.StateSplit(), {state, depend, frameState});
62     builder.SetDepend(stateSplit);
63     auto check = builder.TryPrimitiveTypeCheck(GateType::NumberType(), arg0);
64     builder.ReturnVoid(check, depend);
65 
66     CompilationConfig config(TARGET_X64);
67     TypeMCRLowering typeMCRLowering(&circuit, &config, nullptr, false, "TypeCheckFramework");
68     typeMCRLowering.RunTypeMCRLowering();
69     EXPECT_TRUE(Verifier::Run(&circuit));
70 }
71 
HWTEST_F_L0(LoweringRelateGateTests,TypeConvertFramework)72 HWTEST_F_L0(LoweringRelateGateTests, TypeConvertFramework)
73 {
74     // construct a circuit
75     ecmascript::NativeAreaAllocator allocator;
76     Circuit circuit(&allocator);
77     CircuitBuilder builder(&circuit);
78     GateAccessor acc(&circuit);
79     auto entry = acc.GetStateRoot();
80     auto depend = acc.GetDependRoot();
81     auto arg0 = builder.Arguments(0);
82     auto convert = builder.TypeConvert(MachineType::I64, GateType::NJSValue(), GateType::NumberType(),
83                                        {entry, depend, arg0});
84     builder.Return(convert, convert, convert);
85     EXPECT_TRUE(Verifier::Run(&circuit));
86     CompilationConfig config(TARGET_X64);
87     TypeMCRLowering typeMCRLowering(&circuit, &config, nullptr, false, "TypeConvertFramework");
88     typeMCRLowering.RunTypeMCRLowering();
89     EXPECT_TRUE(Verifier::Run(&circuit));
90 }
91 
HWTEST_F_L0(LoweringRelateGateTests,HeapAllocTest)92 HWTEST_F_L0(LoweringRelateGateTests, HeapAllocTest)
93 {
94     // construct a circuit
95     ecmascript::NativeAreaAllocator allocator;
96     Circuit circuit(&allocator);
97     CircuitBuilder builder(&circuit);
98     Environment env(0, &builder);
99     builder.SetEnvironment(&env);
100     auto glue = builder.Arguments(0);
101     auto arg0 = builder.Arguments(1);
102     auto arg1 = builder.Arguments(2);
103     auto array = builder.HeapAlloc(arg0, GateType::AnyType(), RegionSpaceFlag::IN_YOUNG_SPACE);
104 
105     auto offset = builder.Int64(JSThread::GlueData::GetGlobalConstOffset(false));
106     auto globalEnv = builder.Load(VariableType::JS_POINTER(), glue, offset);
107     auto lenthOffset = builder.IntPtr(GlobalEnvConstants::GetOffsetOfLengthString());
108     auto lengthString = builder.Load(VariableType::JS_POINTER(), globalEnv, lenthOffset);
109 
110     builder.Store(VariableType::JS_POINTER(), glue, array, builder.IntPtr(0), arg1);
111     builder.StoreElement<ecmascript::kungfu::TypedStoreOp::FLOAT32ARRAY_STORE_ELEMENT>(array, builder.IntPtr(0),
112         builder.ToTaggedInt(builder.Int64(0)), builder.Undefined());
113     builder.StoreElement<ecmascript::kungfu::TypedStoreOp::FLOAT32ARRAY_STORE_ELEMENT>(array, builder.IntPtr(1),
114         builder.ToTaggedInt(builder.Int64(1)), builder.Undefined());
115     builder.StoreProperty(array, lengthString, builder.ToTaggedInt(builder.Int64(2)));
116     auto length = builder.LoadProperty(array, lengthString, false);
117     Label less2(&builder);
118     Label notLess2(&builder);
119     auto condtion = builder.TaggedIsTrue(builder.TypedBinaryOp<TypedBinOp::TYPED_LESS>(length,
120         builder.ToTaggedInt(builder.Int64(2))), GateType::NumberType(), GateType::NumberType(),
121         GateType::AnyType(), PGOSampleType::NoneType());
122     builder.Branch(condtion, &less2, &notLess2);
123     builder.Bind(&less2);
124     auto ret = builder.LoadElement<ecmascript::kungfu::TypedLoadOp::FLOAT32ARRAY_LOAD_ELEMENT>(array, builder.IntPtr(1),
125                                                                                                builder.Undefined());
126     builder.Return(ret);
127     builder.Bind(&notLess2);
128     builder.Return(builder.Int64(-1));
129     EXPECT_TRUE(Verifier::Run(&circuit));
130 }
131 } // namespace panda::test
132