• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
14project(irtoc)
15
16if (TARGET arkcompiler)
17    message(FATAL_ERROR "IRtoC core and front-end must be configured before defining target arkcompiler")
18endif()
19
20# These variables will be used in the IRtoC frontend and backend.
21# Since it will be invoked from arbitrary folder,
22# we need to use these variables to determine irtoc folders.
23
24#
25# IRtoC sources:
26#
27
28set(IRTOC_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
29set(IRTOC_SOURCE_DIR ${IRTOC_SOURCE_DIR} PARENT_SCOPE)
30
31#
32# IRtoC build directory:
33#
34
35set(IRTOC_BUILD_DIR ${CMAKE_CURRENT_BINARY_DIR})
36set(IRTOC_BUILD_DIR ${IRTOC_BUILD_DIR} PARENT_SCOPE)
37
38#
39# IRtoC target:
40#
41
42if (NOT IRTOC_TARGET)
43    if (PANDA_TARGET_AMD64)
44        set(IRTOC_TARGET x86_64)
45    elseif(PANDA_TARGET_ARM64)
46        set(IRTOC_TARGET arm64)
47    elseif(PANDA_TARGET_ARM32)
48        set(IRTOC_TARGET arm32)
49    else()
50        message(FATAL_ERROR "Unsupported IRtoC target")
51    endif()
52endif()
53set(IRTOC_TARGET ${IRTOC_TARGET} PARENT_SCOPE)
54
55#
56# IRtoC front-end:
57#
58
59set(IRTOC_LANG_SOURCES
60    ${IRTOC_SOURCE_DIR}/lang/basic_block.rb
61    ${IRTOC_SOURCE_DIR}/lang/cpp_function.rb
62    ${IRTOC_SOURCE_DIR}/lang/function.rb
63    ${IRTOC_SOURCE_DIR}/lang/ir_generator.rb
64    ${IRTOC_SOURCE_DIR}/lang/instruction.rb
65    ${IRTOC_SOURCE_DIR}/lang/instructions_data.rb
66    ${IRTOC_SOURCE_DIR}/lang/irtoc.rb
67    ${IRTOC_SOURCE_DIR}/lang/isa.rb
68    ${IRTOC_SOURCE_DIR}/lang/options.rb
69    ${IRTOC_SOURCE_DIR}/lang/output.rb
70    ${IRTOC_SOURCE_DIR}/lang/regmap.rb
71    ${IRTOC_SOURCE_DIR}/lang/regmask.rb
72    ${IRTOC_SOURCE_DIR}/lang/validation.rb
73)
74set(IRTOC_LANG_SOURCES ${IRTOC_LANG_SOURCES} PARENT_SCOPE) # To be available in irtoc_generate
75
76# Unit tests for IRtoC frontend. Available only in x86 mode, since there is no need to test in different targets.
77if (PANDA_WITH_TESTS AND PANDA_TARGET_AMD64)
78    add_custom_target(irtoc_unit_tests COMMENT "Irtoc frontend unit tests"
79        COMMAND ruby ${IRTOC_SOURCE_DIR}/lang/tests/regmask_test.rb
80        DEPENDS ${IRTOC_LANG_SOURCES}
81                ${IRTOC_SOURCE_DIR}/lang/tests/regmask_test.rb)
82    add_dependencies(tests irtoc_unit_tests)
83endif()
84
85# Run IRtoC front-end to generate C++ code from IRtoC sources.
86# Arguments:
87#   [in] TARGET            - name of the target that will generate C++ code. Thereby user can make a dependency on it.
88#   [in] INPUT_FILES       - input IRtoC scripts, usually files from `irtoc/scripts` folder.
89#   [in] OUTPUT_FILE       - name of the output C++ file.
90#   [in] IR_API            - IR API to be emitted during C++ code generation. See IRtoC sources for possible supported values.
91#   [in] WORKING_DIRECTORY - working directory for the front-end.
92function(irtoc_generate)
93    set(noValues)
94    set(singleValueArgs TARGET OUTPUT_FILE IR_API WORKING_DIRECTORY)
95    set(multiValueArgs INPUT_FILES)
96    cmake_parse_arguments(ARG "${noValues}" "${singleValueArgs}" "${multiValueArgs}" ${ARGN})
97
98    get_directory_property(irtoc_definitions_ DIRECTORY ${IRTOC_SOURCE_DIR} COMPILE_DEFINITIONS)
99
100    if (CMAKE_BUILD_TYPE STREQUAL "Release")
101        list(APPEND irtoc_definitions_ NDEBUG)
102    endif()
103    string(REGEX REPLACE ";" ":" IRTOC_DEFINITIONS "${irtoc_definitions_}")
104
105    string(REPLACE ";" ":" IRTOC_INPUT "${ARG_INPUT_FILES}")
106
107    set(ISAPI "${PANDA_ROOT}/isa/isapi.rb")
108    set(ISA "${PANDA_BINARY_ROOT}/isa/isa.yaml")
109
110    if (NOT DEFINED ARG_WORKING_DIRECTORY)
111        set(ARG_WORKING_DIRECTORY ${IRTOC_BUILD_DIR})
112    else()
113        file(MAKE_DIRECTORY ${ARG_WORKING_DIRECTORY})
114    endif()
115
116    set(IRTOC_PLUGINS_FILE ${IRTOC_BUILD_DIR}/generated/plugins.txt)
117    set(IRTOC_PLUGIN_FILES)
118    if(EXISTS ${IRTOC_PLUGINS_FILE})
119        file(STRINGS ${IRTOC_BUILD_DIR}/generated/plugins.txt PLUGINS_LIST)
120        foreach(PLUGIN_FILE ${PLUGINS_LIST})
121            list(APPEND IRTOC_PLUGIN_FILES "${PANDA_ROOT}/${PLUGIN_FILE}")
122        endforeach()
123    endif()
124
125    add_custom_command(OUTPUT ${ARG_OUTPUT_FILE}
126        COMMENT             "Run IRtoC generator for ${ARG_TARGET}"
127        COMMAND             ruby ${IRTOC_SOURCE_DIR}/lang/irtoc.rb
128                                --input ${IRTOC_INPUT}
129                                --output ${ARG_OUTPUT_FILE}
130                                --ir-api ${ARG_IR_API}
131                                --ark_source_dir ${PANDA_ROOT}
132                                --isa ${ISA}
133                                --definitions ${IRTOC_DEFINITIONS}
134                                --arch ${IRTOC_TARGET}
135                                --plugins ${IRTOC_PLUGINS_FILE}
136        WORKING_DIRECTORY   ${ARG_WORKING_DIRECTORY}
137        DEPENDS             ${IRTOC_LANG_SOURCES} ${ARG_INPUT_FILES} ${ISA} ${ISAPI} ${IRTOC_SOURCE_DIR}/scripts/common.irt ${IRTOC_PLUGIN_FILES}
138    )
139
140    add_custom_target(${ARG_TARGET} DEPENDS ${ARG_OUTPUT_FILE} irtoc_plugins_txt)
141    add_dependencies(${ARG_TARGET} isa_combine)
142endfunction()
143