diff --git a/BUILD.gn b/BUILD.gn index e8c847b3ff..2c6e2f5d74 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -138,9 +138,10 @@ group("ark_unittest") { deps = [] # js unittest - deps += [ + deps += [ # Register the executable block name of the unit test script here "ecmascript/base/tests:host_unittest", "ecmascript/builtins/tests:host_unittest", + "ecmascript/compiler/codegen/maple/test:host_unittest", "ecmascript/containers/tests:host_unittest", "ecmascript/dfx/cpu_profiler/tests:host_unittest", "ecmascript/dfx/hprof/tests:host_unittest", diff --git a/ecmascript/compiler/codegen/maple/test/BUILD.gn b/ecmascript/compiler/codegen/maple/test/BUILD.gn index 7a46e17c4b..98056e16db 100644 --- a/ecmascript/compiler/codegen/maple/test/BUILD.gn +++ b/ecmascript/compiler/codegen/maple/test/BUILD.gn @@ -11,40 +11,57 @@ # See the License for the specific language governing permissions and # limitations under the License. -configs = [ "${MAPLEALL_ROOT}:mapleallcompilecfg" ] +import("//arkcompiler/ets_runtime/js_runtime_config.gni") +import("//arkcompiler/ets_runtime/test/test_helper.gni") -cflags_cc -= [ "-Werror" ] +module_output_path = "arkcompiler/ets_runtime" -include_directories = [ - "${MAPLE_ROOT}/tools/gtest_lib/include", - "${MAPLEALL_ROOT}/maple_util/include", - "${MAPLEALL_ROOT}/maple_driver/include", -] -src_mapleallUT = [ - "cl_ut_test.cpp", - "triple_ut_test.cpp", -] - -executable("mapleallUT") { +host_unittest_action("mapleallUT") { + rtti_compile_flag = false + configs = [ "${MAPLEALL_ROOT}:mapleallcompilecfg" ] + module_out_path = module_output_path + include_directories = [ + "${MAPLEALL_ROOT}/maple_be/include/cg", + "${MAPLEALL_ROOT}/maple_be/include/litecg", + "${MAPLEALL_ROOT}/maple_be/include/ad", + "${MAPLEALL_ROOT}/maple_be/include/be", + "${MAPLEALL_ROOT}/maple_ir/include/", + "${MAPLEALL_ROOT}/maple_util/include/", + "${MAPLEALL_ROOT}/mempool/include/", + "${MAPLEALL_THIRD_PARTY_ROOT}/bounds_checking_function/include/", + "${MAPLEALL_ROOT}/maple_driver/include/", + "${MAPLEALL_ROOT}/maple_me/include/", + "${MAPLEALL_ROOT}/maple_phase/include/", + "${MAPLEALL_ROOT}/mpl2mpl/include/", + "${MAPLEALL_THIRD_PARTY_ROOT}/", + ] + src_mapleallUT = [ "cgbb_test.cpp" ] sources = src_mapleallUT include_dirs = include_directories - output_dir = "${root_out_dir}/lib/${HOST_ARCH}" - - deps = [ - "${MAPLEALL_ROOT}/maple_driver:libmaple_driver", - "${MAPLEALL_ROOT}/maple_ir:libmplir", - "${MAPLEALL_ROOT}/maple_util:libcommandline", - "${MAPLEALL_ROOT}/maple_util:libmplutil", + deps_libcg = [ # Add Link Library "${MAPLEALL_ROOT}/mempool:libmempool", + "${MAPLEALL_ROOT}/maple_phase:libmplphase", "${MAPLEALL_ROOT}/mpl2mpl:libmpl2mpl", - "${MAPLEALL_THIRD_PARTY_ROOT}/bounds_checking_function:libsec_static", + "${MAPLEALL_ROOT}/maple_ir:libmplir", + "${MAPLEALL_ROOT}/maple_util:libmplutil", + "${MAPLEALL_ROOT}/maple_me:libmplme", + "${MAPLEALL_ROOT}/maple_be:libcg", ] - libs = [ - "${MAPLE_ROOT}/tools/gtest_lib/lib/libgmock.a", - "${MAPLE_ROOT}/tools/gtest_lib/lib/libgmock_main.a", - "${MAPLE_ROOT}/tools/gtest_lib/lib/libgtest.a", - "${MAPLE_ROOT}/tools/gtest_lib/lib/libgtest_main.a", - "pthread", - ] + deps = [] + + # If dynamic cast is used in the source code, set COMPILE_RTTI_ to true + rtti_compile_flag = true + deps += deps_libcg + libs = [ "pthread" ] +} +group("host_unittest") { + testonly = true + + # deps file + deps = [ ":mapleallUTAction" ] + + if (is_mac) { + deps -= [ ":mapleallUTAction" ] + } } diff --git a/ecmascript/compiler/codegen/maple/test/cgbb_test.cpp b/ecmascript/compiler/codegen/maple/test/cgbb_test.cpp new file mode 100644 index 0000000000..1d8cd2070d --- /dev/null +++ b/ecmascript/compiler/codegen/maple/test/cgbb_test.cpp @@ -0,0 +1,110 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "gtest/gtest.h" +#include +#include +#include + +#include "cgbb.h" +#include "mempool.h" +#include "cgfunc.h" + +using namespace maplebe; + +using namespace std; + +maplebe::Insn CreateInsnObj(std::string poolName, maple::uint32 opc) +{ + MemPoolCtrler memPoolCtrler; + MemPool memPool(memPoolCtrler, poolName); + maplebe::Insn Insn_obj(memPool, opc); + return Insn_obj; +} +maplebe::BB CreateBBObj(uint32 bbID, std::string poolName) +{ + // BBID bbID, MapleAllocator &mallocator + MemPoolCtrler memPoolCtrler; + MemPool memPool(memPoolCtrler, poolName); + MapleAllocator mallocator(&memPool); + maplebe::BB BB_obj(bbID, mallocator); + return BB_obj; +} + +TEST(InsertInsnBefore_FUNC, t01) +{ + maplebe::BB test_bb = CreateBBObj(10, "BB_pool"); + maplebe::Insn existing = CreateInsnObj("existing", 32); + maplebe::Insn newInsn = CreateInsnObj("newInsn", 3); + + /*Test Function: Insn *BB::InsertInsnBefore(Insn &existing, Insn &newInsn)*/ + test_bb.SetFirstInsn(&existing); + EXPECT_EQ(existing.GetPrev(), nullptr); + maplebe::Insn *ans = test_bb.InsertInsnBefore(existing, newInsn); + EXPECT_EQ(ans, &newInsn); + + maplebe::Insn newInsn1 = CreateInsnObj("newInsn1", 3); + EXPECT_NE(existing.GetPrev(), nullptr); + ans = test_bb.InsertInsnBefore(existing, newInsn1); + EXPECT_EQ(ans, &newInsn1); + EXPECT_EQ(newInsn.GetNext(), &newInsn1); +} + +TEST(InsertInsnAfter_FUNC, t02) +{ + /*Test Function: Insn *BB::InsertInsnAfter(Insn &existing, Insn &newInsn)*/ + maplebe::BB test_bb = CreateBBObj(2, "BB_pool"); + maplebe::Insn existing = CreateInsnObj("existing", 32); + maplebe::Insn newInsn1 = CreateInsnObj("newInsn1", 1), newInsn2 = CreateInsnObj("newInsn2", 2); + + maplebe::Insn *ans; + test_bb.SetFirstInsn(&existing); + ans = test_bb.InsertInsnAfter(existing, newInsn1); + EXPECT_EQ(ans, &newInsn1); + EXPECT_EQ(newInsn1.GetNext(), nullptr); + + ans = test_bb.InsertInsnAfter(existing, newInsn2); + EXPECT_EQ(ans, &newInsn2); + EXPECT_EQ(newInsn2.GetNext(), &newInsn1); +} + +TEST(ReplaceInsn_FUNC, t03) +{ + /*Test Function: void BB::ReplaceInsn(Insn &insn, Insn &newInsn)*/ + maplebe::BB test_bb = CreateBBObj(3, "BB_pool"); + maplebe::Insn insn = CreateInsnObj("insn", 3); + maplebe::Insn newInsn = CreateInsnObj("newInsn", 3); + std::cout << "insn.IsAccessRefField() = " << insn.IsAccessRefField() << std::endl; +} + +TEST(RemoveInsn_FUNC, t04) +{ + /*Test Function: void BB::RemoveInsn(Insn &insn)*/ + maplebe::BB test_bb = CreateBBObj(10, "BB_pool"); + maplebe::Insn insn = CreateInsnObj("insn", 0); + maplebe::Insn insn1 = CreateInsnObj("insn1", 1); + maplebe::Insn insn2 = CreateInsnObj("insn2", 2); + + test_bb.SetFirstInsn(&insn); + test_bb.RemoveInsn(insn); + EXPECT_EQ(test_bb.GetFirstInsn(), nullptr); + + test_bb.SetFirstInsn(&insn); + test_bb.InsertInsnAfter(insn, insn1); + test_bb.InsertInsnAfter(insn1, insn2); + test_bb.RemoveInsn(insn); + EXPECT_EQ(test_bb.GetFirstInsn(), &insn1); + EXPECT_EQ((*(insn.GetNext())).GetPrev(), nullptr); +} \ No newline at end of file