cmake_minimum_required(VERSION 3.10)
project(R8Forge)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

include_directories(include)

# The width of a cell.  11 is the project default; the tests and the VMs follow
# it, so `-DR8FORGE_CELL_SIZE=16` builds an entirely 16 bit toolchain.
# 32 is the ceiling: the VMs (and ROT-8-viz) store a cell in uint32_t, and the
# assembler's immediate type is narrower than that anyway.  Refuse anything else
# here so a wrong -DR8FORGE_CELL_SIZE fails at configure time instead of as a
# C++ error in whichever file happens to shift by the width first.
set(R8FORGE_CELL_SIZE 11 CACHE STRING "bits per cell (1..32)")
if(R8FORGE_CELL_SIZE LESS 1 OR R8FORGE_CELL_SIZE GREATER 32)
  message(FATAL_ERROR
    "R8FORGE_CELL_SIZE=${R8FORGE_CELL_SIZE} is out of range: a cell is stored in "
    "uint32_t in every VM, so the supported range is 1..32")
endif()
add_compile_definitions(R8FORGE_CELL_SIZE=${R8FORGE_CELL_SIZE})

# How many cells the tape holds.  259 is the historical default (a prime ring, so
# a walked-off-end bug cannot alias onto a power of two); it is a *hardware*
# knob as much as a software one.  Every program we ship peaks well below 64
# cells (calc 51, hello 49, signed_demo 34, term 27 -- geometry.hh lists the
# measurements), so `-DR8FORGE_MEMORY_CELLS=64` runs all of them; only the
# asm/test.r8asm self test, which lays every macro's cell block out side by side,
# needs the default.  The ring is a modulo counter either way, so any size is
# legal; 1..65536 keeps the VMs' bookkeeping sane.
set(R8FORGE_MEMORY_CELLS 259 CACHE STRING "cells on the tape (1..65536)")
if(R8FORGE_MEMORY_CELLS LESS 1 OR R8FORGE_MEMORY_CELLS GREATER 65536)
  message(FATAL_ERROR
    "R8FORGE_MEMORY_CELLS=${R8FORGE_MEMORY_CELLS} is out of range: the ring is "
    "addressed by a 16 bit counter in the physical machine, so the supported "
    "range is 1..65536")
endif()
add_compile_definitions(R8FORGE_MEMORY_CELLS=${R8FORGE_MEMORY_CELLS})

find_package(Threads REQUIRED)

add_library(r8forge_core src/r8asm.cc src/synth.cc src/tape_opt.cc src/tape.cc)
target_link_libraries(r8forge_core PUBLIC Threads::Threads)

# 别再 set(CMAKE_CXX_FLAGS ...) 了：那会把默认 flag 整个换掉，MSVC 下丢 /EHsc、
# 还会把 -O3 塞进 Debug 和 /RTC1 打架。Release 配置本来就带 -O3 / /O2 和 NDEBUG。
if(NOT MSVC)
  add_compile_options($<$<CONFIG:Release>:-march=native>)
endif()

add_executable(r8forge tests/asm_test.cc)
target_link_libraries(r8forge r8forge_core)

add_executable(r8forge_synth_test tests/synth_test.cc)
target_link_libraries(r8forge_synth_test r8forge_core)

add_executable(r8forge_derive src/derive.cc)
target_link_libraries(r8forge_derive r8forge_core)

# A second, independent ROT-8 interpreter: runs a .rot8 file with no assembler
# and no macros involved, so its verdict can be checked against r8forge_derive.
add_executable(r8forge_tape_run tests/tape_run.cc)
target_link_libraries(r8forge_tape_run r8forge_core)
add_executable(r8forge_tape_pack tests/tape_pack.cc)
target_link_libraries(r8forge_tape_pack r8forge_core)

# The two things software cannot check by reading the source: the 3 bit packing
# the hardware decodes, and the bracket strategy it uses.
add_executable(r8forge_tape_pack_test tests/tape_pack_test.cc)
target_link_libraries(r8forge_tape_pack_test r8forge_core)

# Runs the byte level library macros (raw_add / raw_and / raw_or / raw_xor /
# raw_inc) over every input combination.
add_executable(r8forge_byte_op_test tests/byte_op_test.cc)
target_link_libraries(r8forge_byte_op_test r8forge_core)

# Checks that every %macro in asm/ carries a doc comment in the house format.
add_executable(r8forge_macro_docs_test tests/macro_docs_test.cc)

# Runs every examples/*.r8asm as a stand alone program and compares its OUT
# stream with the `;   out ...` line in the file header.
add_executable(r8forge_examples_test tests/examples_test.cc)
target_link_libraries(r8forge_examples_test r8forge_core)

# The tape level peephole pass: three run-local rules, no bracket bookkeeping.
add_executable(r8forge_tape_opt tests/tape_opt.cc)
target_link_libraries(r8forge_tape_opt r8forge_core)

add_executable(r8forge_cost_par tests/cost_par.cc)
target_link_libraries(r8forge_cost_par r8forge_core)

add_executable(r8forge_cost_search tests/cost_search.cc)
target_link_libraries(r8forge_cost_search r8forge_core)
# ---- tests ------------------------------------------------------------------
# `ctest --test-dir build -j` runs the whole baseline.  Every entry is a program
# under tests/ that exits non zero on failure; the working directory is the repo
# root because the drivers use relative paths.
#
# Labels: `widthfree` means "true at any cell width" (bit level slices, the
# synthesizer, the looping demo, the macro comment format).  Everything else
# assumes the default 11 bit geometry -- examples/ fixes 2047 style values and
# byte_op_test drives the 8/11 bit word macros -- so a CI matrix over
# -DR8FORGE_CELL_SIZE runs only the widthfree set.
enable_testing()
set(R8FORGE_TEST_DIR ${CMAKE_CURRENT_SOURCE_DIR})

add_test(NAME flagship        COMMAND r8forge asm/test.r8asm)
add_test(NAME byte_ops        COMMAND r8forge_byte_op_test)
add_test(NAME examples        COMMAND r8forge_examples_test examples)
add_test(NAME synth_full_add  COMMAND r8forge_synth_test examples/full_add.r8asm)
add_test(NAME bit_add_slice   COMMAND r8forge verify_clb_bit_add_nc.r8asm)
add_test(NAME bit_inc_slice   COMMAND r8forge verify_clb_bit_inc_nc.r8asm)
add_test(NAME loop_derive     COMMAND r8forge_derive verify_loop.r8asm -1 0=5)
add_test(NAME macro_docs      COMMAND r8forge_macro_docs_test)
# The packer's CLI and the runner's loader, end to end: assemble, pack, then run
# the text tape and the packed file and require the same output under every
# bracket strategy.
add_test(NAME pack_roundtrip
         COMMAND ${CMAKE_COMMAND}
                 -DR8FORGE=$<TARGET_FILE:r8forge>
                 -DPACK=$<TARGET_FILE:r8forge_tape_pack>
                 -DRUN=$<TARGET_FILE:r8forge_tape_run>
                 -DSOURCE=${R8FORGE_TEST_DIR}/examples/hello.r8asm
                 -DWORK=${CMAKE_CURRENT_BINARY_DIR}
                 -P ${R8FORGE_TEST_DIR}/tests/pack_roundtrip.cmake)
add_test(NAME tape_pack COMMAND r8forge_tape_pack_test)
# The RTL against the interpreter: compiles hardware/rot8.v with iverilog, runs
# a tape on it, and requires the same OUT stream and step count as
# `tape_run --loop-stack --retest`.  The RTL is the 8 bit / 64 cell machine, so
# the script reports SKIP (and passes) in any other geometry.
find_program(IVERILOG_EXE iverilog)
add_test(NAME rtl_sim
         COMMAND ${CMAKE_COMMAND}
                 -DGEOMETRY=${R8FORGE_CELL_SIZE}/${R8FORGE_MEMORY_CELLS}
                 -DIVERILOG=${IVERILOG_EXE}
                 -DPACK=$<TARGET_FILE:r8forge_tape_pack>
                 -DRUN=$<TARGET_FILE:r8forge_tape_run>
                 -DR8FORGE=$<TARGET_FILE:r8forge>
                 -DSOURCE=${R8FORGE_TEST_DIR}/examples/hello.r8asm
                 -DHARDWARE=${R8FORGE_TEST_DIR}/hardware
                 -DWORK=${CMAKE_CURRENT_BINARY_DIR}
                 -P ${R8FORGE_TEST_DIR}/tests/rtl_sim.cmake)

set_tests_properties(flagship byte_ops examples PROPERTIES LABELS "default-width")
set_tests_properties(synth_full_add bit_add_slice bit_inc_slice loop_derive pack_roundtrip tape_pack rtl_sim
                     macro_docs PROPERTIES LABELS "widthfree")
set_tests_properties(flagship byte_ops examples synth_full_add bit_add_slice
                     bit_inc_slice loop_derive macro_docs tape_pack pack_roundtrip rtl_sim
                     PROPERTIES WORKING_DIRECTORY ${R8FORGE_TEST_DIR})