|
| 1 | +find_package(Python3 REQUIRED COMPONENTS Interpreter) |
| 2 | + |
| 3 | +set(DECODE_PYGEN_DIR "${CMAKE_CURRENT_LIST_DIR}") |
| 4 | +set(DECODE_DIR "${DECODE_PYGEN_DIR}/..") # decode/ |
| 5 | +get_filename_component(DECODE_DIR "${DECODE_DIR}" ABSOLUTE) # normilize path |
| 6 | + |
| 7 | +set(GEN_SCRIPT "${DECODE_PYGEN_DIR}/gen_decoder.py") |
| 8 | +set(GEN_DEPS |
| 9 | + "${DECODE_PYGEN_DIR}/gen_decoder.py" |
| 10 | + "${DECODE_PYGEN_DIR}/insn_mnem.py" |
| 11 | + "${DECODE_PYGEN_DIR}/opcode_dtree.py" |
| 12 | + "${DECODE_PYGEN_DIR}/mnem_parse.py" |
| 13 | + "${DECODE_PYGEN_DIR}/emit_cpp.py" |
| 14 | +) |
| 15 | + |
| 16 | +set(LIN_CPP "${DECODE_DIR}/source/lin_decode.cpp") |
| 17 | + |
| 18 | +set(ISA_EXT_INC "${CMAKE_SOURCE_DIR}/src/isa/include/isa/isa_ext.inc") |
| 19 | + |
| 20 | +set(GEN_DIR "${CMAKE_CURRENT_BINARY_DIR}/generated") |
| 21 | +set(DECODE_CPP_OUT "${GEN_DIR}/decode.cpp") |
| 22 | +set(DOT_OUT "${GEN_DIR}/dtree.dot") |
| 23 | +set(COMPILE_DOT_SH "compile_dot.sh") |
| 24 | + |
| 25 | +# options |
| 26 | +set(DECODE_BACKEND "generated" CACHE STRING "Decode backend: linear|generated") |
| 27 | +set_property(CACHE DECODE_BACKEND PROPERTY STRINGS linear generated) |
| 28 | + |
| 29 | +option(DECODE_EMIT_DOT "Emit decode decision tree .dot during generation" OFF) |
| 30 | +set(DECODE_GEN_EXTRA_ARGS "" CACHE STRING "Extra args for gen_decoder.py (CMake list separated by ';')") |
| 31 | + |
| 32 | +# Build command depending on backend |
| 33 | +if (DECODE_BACKEND STREQUAL "generated") |
| 34 | + if (DECODE_EMIT_DOT) |
| 35 | + set(DOT_ARGS --dot "${DOT_OUT}") |
| 36 | + else() |
| 37 | + set(DOT_ARGS) |
| 38 | + endif() |
| 39 | + |
| 40 | + add_custom_command( |
| 41 | + OUTPUT "${DECODE_CPP_OUT}" |
| 42 | + COMMAND "${CMAKE_COMMAND}" -E make_directory "${GEN_DIR}" |
| 43 | + COMMAND "${Python3_EXECUTABLE}" "${GEN_SCRIPT}" |
| 44 | + --isa "${ISA_EXT_INC}" |
| 45 | + --output "${DECODE_CPP_OUT}" |
| 46 | + ${DOT_ARGS} |
| 47 | + ${DECODE_GEN_EXTRA_ARGS} |
| 48 | + COMMAND "${CMAKE_COMMAND}" -E copy_if_different "${DECODE_PYGEN_DIR}/${COMPILE_DOT_SH}" "${GEN_DIR}/${COMPILE_DOT_SH}" |
| 49 | + DEPENDS |
| 50 | + "${ISA_EXT_INC}" |
| 51 | + "${DECODE_PYGEN_DIR}/${COMPILE_DOT_SH}" |
| 52 | + ${GEN_DEPS} |
| 53 | + WORKING_DIRECTORY "${DECODE_PYGEN_DIR}" |
| 54 | + COMMENT "Generating decoder.cpp (backend=generated)" |
| 55 | + VERBATIM |
| 56 | + ) |
| 57 | +elseif (DECODE_BACKEND STREQUAL "linear") |
| 58 | + add_custom_command( |
| 59 | + OUTPUT "${DECODE_CPP_OUT}" |
| 60 | + COMMAND "${CMAKE_COMMAND}" -E make_directory "${GEN_DIR}" |
| 61 | + COMMAND "${CMAKE_COMMAND}" -E copy_if_different "${LIN_CPP}" "${DECODE_CPP_OUT}" |
| 62 | + DEPENDS "${LIN_CPP}" |
| 63 | + COMMENT "Preparing decoder.cpp (backend=linear)" |
| 64 | + VERBATIM |
| 65 | + ) |
| 66 | +else() |
| 67 | + message(FATAL_ERROR "Unknown DECODE_BACKEND='${DECODE_BACKEND}'. Use linear|generated.") |
| 68 | +endif() |
| 69 | + |
| 70 | +add_custom_target(decode_codegen DEPENDS "${DECODE_CPP_OUT}") |
| 71 | + |
| 72 | +# Export outputs to decode |
| 73 | +set(DECODE_CPP_OUT "${DECODE_CPP_OUT}" PARENT_SCOPE) |
| 74 | +set(DECODE_CODEGEN_TARGET decode_codegen PARENT_SCOPE) |
0 commit comments