From bebe6dfda0894b0323167a58dd192fea3e45de71 Mon Sep 17 00:00:00 2001 From: hanez Date: Fri, 10 Apr 2026 22:04:27 +0200 Subject: [PATCH] Made MAX_FRAMES, MAX_FRAME_LOCALS, MAX_GLOBALS, OUTPUT_SIZE and STACK_SIZE configurable during build configuration using cmake defines (-D). No code changes. (0.40.5) --- cmake/Options.cmake | 7 +++++++ cmake/Targets.cmake | 7 +++++++ docs/build.md | 17 +++++++++++++++++ src/vm.h | 14 ++++++++++++++ 4 files changed, 45 insertions(+) diff --git a/cmake/Options.cmake b/cmake/Options.cmake index 92a5fb8..8ad1fa5 100644 --- a/cmake/Options.cmake +++ b/cmake/Options.cmake @@ -33,3 +33,10 @@ endif() # Debug option to enable verbose parser/VM logging option(FUN_DEBUG "Enable extra debug logging in Fun" OFF) + +# VM settings (configurable via -D...) +set(MAX_FRAMES 128 CACHE STRING "Maximum depth of the call stack (frames)") +set(MAX_FRAME_LOCALS 64 CACHE STRING "Maximum number of local variables per frame") +set(MAX_GLOBALS 128 CACHE STRING "Maximum number of global variables") +set(OUTPUT_SIZE 1024 CACHE STRING "Size of the VM output buffer (number of values)") +set(STACK_SIZE 1024 CACHE STRING "Size of the VM evaluation stack") diff --git a/cmake/Targets.cmake b/cmake/Targets.cmake index 0e98eab..9deaeb2 100644 --- a/cmake/Targets.cmake +++ b/cmake/Targets.cmake @@ -25,6 +25,13 @@ endif() target_compile_definitions(fun_core PUBLIC FUN_VERSION="${PROJECT_VERSION}") target_compile_definitions(fun_core PUBLIC DEFAULT_LIB_DIR="${DEFAULT_LIB_DIR}") +# VM configuration +target_compile_definitions(fun_core PUBLIC MAX_FRAMES=${MAX_FRAMES}) +target_compile_definitions(fun_core PUBLIC MAX_FRAME_LOCALS=${MAX_FRAME_LOCALS}) +target_compile_definitions(fun_core PUBLIC MAX_GLOBALS=${MAX_GLOBALS}) +target_compile_definitions(fun_core PUBLIC OUTPUT_SIZE=${OUTPUT_SIZE}) +target_compile_definitions(fun_core PUBLIC STACK_SIZE=${STACK_SIZE}) + # Apply extension include/link variables discovered in cmake/Extensions foreach(var_pair PCSC diff --git a/docs/build.md b/docs/build.md index 9fb92d6..e7b815b 100644 --- a/docs/build.md +++ b/docs/build.md @@ -24,6 +24,17 @@ Fun exposes several options you can toggle at configure time: - `FUN_WITH_RUST` (ON/OFF) - Build and link Rust staticlib from `src/rust/` - `FUN_WITH_OPENSSL` (ON/OFF) - Enable OpenSSL-backed helpers (MD5/SHA-256/SHA-512/RIPEMD-160) +### VM configuration constants +You can override internal VM limits at compile time by passing `-D=` to CMake: + +- `MAX_FRAMES` (default: 128) - Maximum depth of the call stack (frames) +- `MAX_FRAME_LOCALS` (default: 64) - Maximum number of local variables per frame +- `MAX_GLOBALS` (default: 128) - Maximum number of global variables +- `OUTPUT_SIZE` (default: 1024) - Size of the VM output buffer (number of values) +- `STACK_SIZE` (default: 1024) - Size of the VM evaluation stack (number of `Value` slots) + +These are defined as `CACHE` variables, so they will persist in your `CMakeCache.txt`. + When configuring, the build prints a summary like: ``` @@ -59,6 +70,12 @@ cmake -S . -B build_release -DCMAKE_BUILD_TYPE=Release \ cmake --build build_release --target build ``` +### Customizing VM limits +``` +cmake -S . -B build_custom -DSTACK_SIZE=4096 -DMAX_GLOBALS=512 +cmake --build build_custom --target fun +``` + If `FUN_WITH_RUST` is enabled, ensure `cargo` is available in PATH; the build will invoke it and link the produced static library. If `FUN_WITH_OPENSSL` is enabled, CMake must detect your system OpenSSL (libcrypto). diff --git a/src/vm.h b/src/vm.h index 8b47615..ec652b1 100644 --- a/src/vm.h +++ b/src/vm.h @@ -13,11 +13,25 @@ #include "bytecode.h" #include +#ifndef MAX_FRAMES #define MAX_FRAMES 128 +#endif + +#ifndef MAX_FRAME_LOCALS #define MAX_FRAME_LOCALS 64 +#endif + +#ifndef MAX_GLOBALS #define MAX_GLOBALS 128 +#endif + +#ifndef OUTPUT_SIZE #define OUTPUT_SIZE 1024 +#endif + +#ifndef STACK_SIZE #define STACK_SIZE 1024 +#endif static const char *opcode_names[] = { "NOP", "LOAD_CONST", "LOAD_LOCAL", "STORE_LOCAL",