14514f5e3Sopenharmony_ci/* 24514f5e3Sopenharmony_ci * Copyright (c) 2023 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#include "ecmascript/compiler/bytecodes.h" 164514f5e3Sopenharmony_ci#include "ecmascript/compiler/circuit_builder.h" 174514f5e3Sopenharmony_ci#include "ecmascript/compiler/constant_folding.h" 184514f5e3Sopenharmony_ci#include "ecmascript/compiler/gate_accessor.h" 194514f5e3Sopenharmony_ci#include "ecmascript/compiler/graph_editor.h" 204514f5e3Sopenharmony_ci#include "ecmascript/compiler/pass.h" 214514f5e3Sopenharmony_ci#include "ecmascript/compiler/share_opcodes.h" 224514f5e3Sopenharmony_ci#include "ecmascript/compiler/stub_builder.h" 234514f5e3Sopenharmony_ci#include "ecmascript/compiler/type.h" 244514f5e3Sopenharmony_ci#include "ecmascript/compiler/variable_type.h" 254514f5e3Sopenharmony_ci#include "ecmascript/compiler/verifier.h" 264514f5e3Sopenharmony_ci#include "ecmascript/compiler/typed_bytecode_lowering.h" 274514f5e3Sopenharmony_ci#include "ecmascript/compiler/typed_hcr_lowering.h" 284514f5e3Sopenharmony_ci#include "ecmascript/mem/chunk.h" 294514f5e3Sopenharmony_ci#include "ecmascript/mem/native_area_allocator.h" 304514f5e3Sopenharmony_ci#include "ecmascript/tests/test_helper.h" 314514f5e3Sopenharmony_ci#include "gtest/gtest-death-test.h" 324514f5e3Sopenharmony_ci#include "gtest/gtest.h" 334514f5e3Sopenharmony_ci 344514f5e3Sopenharmony_cinamespace panda::test { 354514f5e3Sopenharmony_ciclass ConstantFoldingTest : public testing::Test { 364514f5e3Sopenharmony_ci}; 374514f5e3Sopenharmony_ciusing ecmascript::kungfu::Circuit; 384514f5e3Sopenharmony_ciusing ecmascript::kungfu::GateAccessor; 394514f5e3Sopenharmony_ciusing ecmascript::kungfu::GateType; 404514f5e3Sopenharmony_ciusing ecmascript::kungfu::MachineType; 414514f5e3Sopenharmony_ciusing ecmascript::kungfu::CircuitBuilder; 424514f5e3Sopenharmony_ciusing ecmascript::kungfu::Label; 434514f5e3Sopenharmony_ciusing ecmascript::kungfu::OpCode; 444514f5e3Sopenharmony_ciusing ecmascript::kungfu::GateRef; 454514f5e3Sopenharmony_ciusing ecmascript::kungfu::Variable; 464514f5e3Sopenharmony_ciusing ecmascript::kungfu::VariableType; 474514f5e3Sopenharmony_ciusing ecmascript::kungfu::Verifier; 484514f5e3Sopenharmony_ciusing ecmascript::kungfu::Environment; 494514f5e3Sopenharmony_ciusing ecmascript::kungfu::LoopPeeling; 504514f5e3Sopenharmony_ciusing ecmascript::kungfu::CombinedPassVisitor; 514514f5e3Sopenharmony_ciusing ecmascript::kungfu::TypedBinOp; 524514f5e3Sopenharmony_ciusing ecmascript::kungfu::PGOSampleType; 534514f5e3Sopenharmony_ciusing ecmascript::kungfu::GraphLinearizer; 544514f5e3Sopenharmony_ciusing ecmascript::kungfu::ConstantFolding; 554514f5e3Sopenharmony_ciusing ecmascript::kungfu::CompilationConfig; 564514f5e3Sopenharmony_ci 574514f5e3Sopenharmony_ciHWTEST_F_L0(ConstantFoldingTest, ConstantFoldingTypedBinOpTest) 584514f5e3Sopenharmony_ci{ 594514f5e3Sopenharmony_ci // construct a circuit 604514f5e3Sopenharmony_ci ecmascript::NativeAreaAllocator allocator; 614514f5e3Sopenharmony_ci Circuit circuit(&allocator); 624514f5e3Sopenharmony_ci ecmascript::Chunk chunk(&allocator); 634514f5e3Sopenharmony_ci GateAccessor acc(&circuit); 644514f5e3Sopenharmony_ci CircuitBuilder builder(&circuit); 654514f5e3Sopenharmony_ci Environment env(0, &builder); 664514f5e3Sopenharmony_ci 674514f5e3Sopenharmony_ci builder.SetEnvironment(&env); 684514f5e3Sopenharmony_ci 694514f5e3Sopenharmony_ci DEFVALUE(number1, (&builder), VariableType::INT32(), builder.Int32(14)); 704514f5e3Sopenharmony_ci DEFVALUE(number2, (&builder), VariableType::INT32(), builder.Int32(7)); 714514f5e3Sopenharmony_ci 724514f5e3Sopenharmony_ci auto sum = builder.Int32Add(*number1, *number2); 734514f5e3Sopenharmony_ci auto convert = builder.ConvertInt32ToTaggedInt(sum); 744514f5e3Sopenharmony_ci builder.Return(convert); 754514f5e3Sopenharmony_ci CombinedPassVisitor constantFoldingVisitor(&circuit, false, "ConstantFoldingTypedBinOpTest", &chunk); 764514f5e3Sopenharmony_ci CompilationConfig cmpCfg = CompilationConfig("x86_64-unknown-linux-gnu"); 774514f5e3Sopenharmony_ci ConstantFolding constantFolding(&circuit, &constantFoldingVisitor, &cmpCfg, false, 784514f5e3Sopenharmony_ci "ConstantFoldingTypedBinOpTest", &chunk); 794514f5e3Sopenharmony_ci constantFoldingVisitor.AddPass(&constantFolding); 804514f5e3Sopenharmony_ci constantFoldingVisitor.VisitGraph(); 814514f5e3Sopenharmony_ci 824514f5e3Sopenharmony_ci EXPECT_EQ(acc.GetOpCode(acc.GetValueIn(convert, 0)), OpCode::CONSTANT); 834514f5e3Sopenharmony_ci EXPECT_TRUE(acc.GetInt32FromConstant(acc.GetValueIn(convert, 0)) == 21); 844514f5e3Sopenharmony_ci} 854514f5e3Sopenharmony_ci} // namespace panda::test