1# Copyright (c) 2021-2022 Huawei Device Co., Ltd.
2# Licensed under the Apache License, Version 2.0 (the "License");
3# you may not use this file except in compliance with the License.
4# You may obtain a copy of the License at
5#
6# http://www.apache.org/licenses/LICENSE-2.0
7#
8# Unless required by applicable law or agreed to in writing, software
9# distributed under the License is distributed on an "AS IS" BASIS,
10# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11# See the License for the specific language governing permissions and
12# limitations under the License.
13
14cmake_minimum_required(VERSION 3.5.2 FATAL_ERROR)
15
16project(bytecode_optimizer)
17
18include(cmake/coverage.cmake)
19
20set(BYTECODE_OPT_SOURCES
21    canonicalization.cpp
22    codegen.cpp
23    bytecodeopt_peepholes.cpp
24    optimize_bytecode.cpp
25    reg_acc_alloc.cpp
26    reg_encoder.cpp
27    check_resolver.cpp
28    common.cpp
29    const_array_resolver.cpp
30)
31
32set(SOURCES ${BYTECODE_OPT_SOURCES})
33set(GENERATED_DIR ${CMAKE_CURRENT_BINARY_DIR}/generated)
34file(MAKE_DIRECTORY "${GENERATED_DIR}")
35
36panda_isa_gen(
37    TEMPLATES
38        "insn_selection.h.erb"
39        "insn_selection.cpp.erb"
40        "check_width.cpp.erb"
41        "check_width.h.erb"
42    REQUIRES
43        "${CMAKE_CURRENT_SOURCE_DIR}/bytecode_optimizer_isapi.rb"
44        "${PANDA_ROOT}/assembler/asm_isapi.rb"
45    DESTINATION
46        "${GENERATED_DIR}"
47)
48
49add_library(arkbytecodeopt ${PANDA_DEFAULT_LIB_TYPE} ${SOURCES})
50add_dependencies(arkbytecodeopt isa_gen_bytecode_optimizer)
51
52panda_gen_options(TARGET arkbytecodeopt YAML_FILE options.yaml GENERATED_HEADER bytecodeopt_options_gen.h)
53
54target_include_directories(arkbytecodeopt
55    PUBLIC ${PANDA_ROOT}
56    PUBLIC ${CMAKE_CURRENT_BINARY_DIR}
57    PUBLIC ${GENERATED_DIR}
58    PUBLIC ${GENERATED_DIR}/../compiler/generated
59)
60
61target_link_libraries(arkbytecodeopt arkcompiler arkassembler arkfile)
62
63set(PANDA_BYTECODE_OPT_TESTS_LIBRARIES arkbytecodeopt arkfile arkbase)
64if (NOT (PANDA_TARGET_MOBILE OR PANDA_TARGET_OHOS OR PANDA_ENABLE_FUZZBENCH))
65    list(APPEND PANDA_BYTECODE_OPT_TESTS_LIBRARIES stdc++fs)
66endif()
67
68set(BYTECODE_OPT_TEST_SOURCES
69    tests/bitops_bitwise_and_test.cpp
70    tests/bc_lowering_test.cpp
71    tests/codegen_test.cpp
72    tests/reg_acc_alloc_test.cpp
73    tests/reg_encoder_test.cpp
74    tests/runtime_adapter_test.cpp
75    tests/const_array_resolver_test.cpp
76    tests/bytecodeopt_peepholes_test.cpp
77    tests/check_resolver_test.cpp
78    tests/canonicalization_test.cpp
79)
80
81panda_add_gtest(
82    NAME bytecodeopt_unit_tests
83    SOURCES
84        ${BYTECODE_OPT_TEST_SOURCES}
85    INCLUDE_DIRS
86        ${CMAKE_CURRENT_SOURCE_DIR}
87    LIBRARIES
88        ${PANDA_BYTECODE_OPT_TESTS_LIBRARIES}
89    SANITIZERS
90        ${PANDA_SANITIZERS_LIST}
91)
92
93panda_add_gtest(
94    NO_CORES
95    NAME bytecodeopt_peepholes_runtime_tests
96    SOURCES
97        tests/bytecodeopt_peepholes_runtime_test.cpp
98    LIBRARIES
99        arkassembler
100        arkbytecodeopt
101        arkruntime
102    SANITIZERS
103        ${PANDA_SANITIZERS_LIST}
104)
105
106panda_add_to_clang_tidy(TARGET arkbytecodeopt CHECKS
107    # TODO(mbolshov): elaborate
108    "-hicpp-use-equals-default"
109    "-modernize-use-equals-default"
110    "-cppcoreguidelines-pro-type-member-init"
111    "-misc-unused-parameters"
112    # From assembler
113    "-misc-non-private-member-variables-in-classes"
114    "-cert-dcl21-cpp"
115    "-cppcoreguidelines-macro-usage"
116    "-google-runtime-references"
117    "-readability-identifier-naming" # to use ins_create_api.h
118)
119
120panda_add_sanitizers(TARGET arkbytecodeopt SANITIZERS ${PANDA_SANITIZERS_LIST})
121
122add_check_style(".")
123