From 6c19722d9e2f3f3ece44e2bd6c53694b6b01a206 Mon Sep 17 00:00:00 2001 From: ProgramSnail Date: Sat, 24 May 2025 17:24:19 +0300 Subject: [PATCH] xmake initial build, uint -> uint32_t (stdlib test 03 fails with xmake, possible due to newer c and c++ versions) --- byterun/dep_check.sh | 5 ++++- byterun/include/module_manager.h | 2 +- byterun/include/sm_parser.hpp | 2 +- byterun/include/utils.h | 26 +++++++++++++------------- byterun/regression_check.sh | 5 ++++- byterun/src/analyzer.cpp | 22 +++++++++++----------- byterun/src/cli.cpp | 4 ++++ byterun/src/interpreter.c | 26 +++++++++++++------------- byterun/stdlib_regression_check.sh | 8 ++++++-- byterun/tutorial_check.sh | 5 ++++- byterun/xmake.lua | 5 +++-- 11 files changed, 64 insertions(+), 46 deletions(-) diff --git a/byterun/dep_check.sh b/byterun/dep_check.sh index 64c0fcbe6..85dacb2dd 100755 --- a/byterun/dep_check.sh +++ b/byterun/dep_check.sh @@ -1,6 +1,9 @@ #!/usr/bin/env bash -dune build > /dev/null +xmake build +cp "build/linux/x86_64/release/byterun" byterun.exe + +# dune build > /dev/null prefix="../regression/" suffix=".lama" diff --git a/byterun/include/module_manager.h b/byterun/include/module_manager.h index e31022eea..0f994f729 100644 --- a/byterun/include/module_manager.h +++ b/byterun/include/module_manager.h @@ -11,7 +11,7 @@ Bytefile *run_with_imports(Bytefile *root, int argc, char **argv, // --- -enum BUILTIN : uint { +enum BUILTIN : uint32_t { BUILTIN_Luppercase, // 0 BUILTIN_Llowercase, // 1 BUILTIN_Lassert, // 2 diff --git a/byterun/include/sm_parser.hpp b/byterun/include/sm_parser.hpp index 56fd9581e..05eb560b0 100644 --- a/byterun/include/sm_parser.hpp +++ b/byterun/include/sm_parser.hpp @@ -239,4 +239,4 @@ struct SMInstr { std::vector parse_sm(std::istream &in); -std::optional parse_sm(std::string &line); +std::optional parse_sm(const std::string &line); diff --git a/byterun/include/utils.h b/byterun/include/utils.h index e0c886a9d..540d76a8a 100644 --- a/byterun/include/utils.h +++ b/byterun/include/utils.h @@ -9,19 +9,19 @@ /* The unpacked representation of bytecode file */ typedef struct { - uint main_offset; /* offset of the function 'main' */ - char *string_ptr; /* A pointer to the beginning of the string table */ - int *imports_ptr; /* A pointer to the beginning of imports table */ - int *public_ptr; /* A pointer to the beginning of publics table */ - char *code_ptr; /* A pointer to the bytecode itself */ - void **global_ptr; /* A pointer to the global area */ - char *substs_ptr; /* A pointer to the substs area */ - int code_size; /* The size (in bytes) of code */ - uint stringtab_size; /* The size (in bytes) of the string table */ - uint global_area_size; /* The size (in words) of global area */ - uint substs_area_size; /* number of required address substitutions */ - uint imports_number; /* The number of imports */ - uint public_symbols_number; /* The number of public symbols */ + uint32_t main_offset; /* offset of the function 'main' */ + char *string_ptr; /* A pointer to the beginning of the string table */ + int32_t *imports_ptr; /* A pointer to the beginning of imports table */ + int32_t *public_ptr; /* A pointer to the beginning of publics table */ + char *code_ptr; /* A pointer to the bytecode itself */ + void **global_ptr; /* A pointer to the global area */ + char *substs_ptr; /* A pointer to the substs area */ + int32_t code_size; /* The size (in bytes) of code */ + uint32_t stringtab_size; /* The size (in bytes) of the string table */ + uint32_t global_area_size; /* The size (in words) of global area */ + uint32_t substs_area_size; /* number of required address substitutions */ + uint32_t imports_number; /* The number of imports */ + uint32_t public_symbols_number; /* The number of public symbols */ char buffer[0]; } Bytefile; diff --git a/byterun/regression_check.sh b/byterun/regression_check.sh index d17a6294a..7ef986b38 100755 --- a/byterun/regression_check.sh +++ b/byterun/regression_check.sh @@ -1,6 +1,9 @@ #!/usr/bin/env bash -dune build > /dev/null +xmake build +cp "build/linux/x86_64/release/byterun" byterun.exe + +# dune build > /dev/null prefix="../regression/" suffix=".lama" diff --git a/byterun/src/analyzer.cpp b/byterun/src/analyzer.cpp index ec742f4d0..c99c7de4f 100644 --- a/byterun/src/analyzer.cpp +++ b/byterun/src/analyzer.cpp @@ -19,9 +19,9 @@ void analyze(Bytefile *bf, std::vector &&add_publics) { uint16_t mock_builtin_begin_counter = 0; int current_stack_depth = 0; - const uint globals_count = bf->global_area_size; - uint current_locals_count = 0; - uint current_args_count = 0; + const uint32_t globals_count = bf->global_area_size; + uint32_t current_locals_count = 0; + uint32_t current_args_count = 0; bool is_in_closure = false; uint16_t *current_begin_counter = nullptr; int func_end_found = 0; @@ -55,7 +55,7 @@ void analyze(Bytefile *bf, std::vector &&add_publics) { auto const check_correct_var = [&saved_current_ip, bf, &globals_count, ¤t_locals_count, ¤t_args_count, - &is_in_closure](uint8_t l, uint id) { + &is_in_closure](uint8_t l, uint32_t id) { if (l > 3) { ip_failure(saved_current_ip, bf, "unexpected variable category"); } @@ -264,7 +264,7 @@ void analyze(Bytefile *bf, std::vector &&add_publics) { is_in_closure = (cmd == Cmd::CBEGIN); break; case Cmd::CLOSURE: { - /*uint closure_offset = */ ip_read_int_unsafe( + /*uint32_t closure_offset = */ ip_read_int_unsafe( ¤t_ip); // closure offset size_t args_count = ip_read_int_unsafe(¤t_ip); // args count extra_stack_during_opr = args_count; @@ -286,7 +286,7 @@ void analyze(Bytefile *bf, std::vector &&add_publics) { // } } break; case Cmd::CALLC: { - uint args_count = ip_read_int_unsafe(¤t_ip); + uint32_t args_count = ip_read_int_unsafe(¤t_ip); current_stack_depth -= args_count + 1; // + closure itself if (current_stack_depth < 0) { ip_failure(saved_current_ip, bf, "not enough elements in stack"); @@ -295,8 +295,8 @@ void analyze(Bytefile *bf, std::vector &&add_publics) { // NOTE: can't check args == cbegin args } break; case Cmd::CALL: { - uint call_offset = ip_read_int_unsafe(¤t_ip); // call offset - uint args_count = ip_read_int_unsafe(¤t_ip); + uint32_t call_offset = ip_read_int_unsafe(¤t_ip); // call offset + uint32_t args_count = ip_read_int_unsafe(¤t_ip); current_stack_depth -= args_count; if (current_stack_depth < 0) { ip_failure(saved_current_ip, bf, "not enough elements in stack"); @@ -309,11 +309,11 @@ void analyze(Bytefile *bf, std::vector &&add_publics) { if (is_command_name(bf->code_ptr + call_offset, bf, Cmd::BUILTIN)) { if (args_count != - *(uint *)(bf->code_ptr + call_offset + 1 + sizeof(uint32_t))) { + *(uint32_t *)(bf->code_ptr + call_offset + 1 + sizeof(uint32_t))) { ip_failure(saved_current_ip, bf, "wrong builtin call argument count"); } } else if (is_command_name(bf->code_ptr + call_offset, bf, Cmd::BEGIN)) { - if (args_count != *(uint *)(bf->code_ptr + call_offset + 1)) { + if (args_count != *(uint32_t *)(bf->code_ptr + call_offset + 1)) { ip_failure(saved_current_ip, bf, "wrong call argument count"); } } else { @@ -352,7 +352,7 @@ void analyze(Bytefile *bf, std::vector &&add_publics) { // add end to behave like end ++func_end_found; - /*uint args_count = */ ip_read_int_unsafe(¤t_ip); + /*uint32_t args_count = */ ip_read_int_unsafe(¤t_ip); // NOTE: args checks done in corresponding CALL/CALLC } break; diff --git a/byterun/src/cli.cpp b/byterun/src/cli.cpp index 75f48b8f6..70261f034 100644 --- a/byterun/src/cli.cpp +++ b/byterun/src/cli.cpp @@ -34,7 +34,11 @@ int main(int argc, char **argv) { else if (strcmp(argv[1], "-p") == 0) { do_print = true; } else { +#ifdef WITH_CHECK + failure("wrong execution option (acceptable options - '-i')"); +#else failure("wrong execution option (acceptable options - '-i', '-v', '-vi')"); +#endif } if (argc < 3) { diff --git a/byterun/src/interpreter.c b/byterun/src/interpreter.c index 4a091ffbd..2378c75c1 100644 --- a/byterun/src/interpreter.c +++ b/byterun/src/interpreter.c @@ -96,7 +96,7 @@ static inline void call_Barray(size_t elem_count) { s_push(array); } -void call_builtin(uint builtin_id, uint args_count) { +void call_builtin(uint32_t builtin_id, uint32_t args_count) { #ifndef WITH_CHECK if (builtin_id >= BUILTIN_NONE) { s_failure(&s, "invalid builtin"); @@ -211,7 +211,7 @@ void run_main(Bytefile* bf, int argc, char **argv) { } case CMD_BASIC_JMP: { // JMP 0x%.8x - uint jmp_p = ip_read_int(&s.ip); + uint32_t jmp_p = ip_read_int(&s.ip); #ifndef WITH_CHECK if (jmp_p >= s.bf->code_size) { @@ -284,7 +284,7 @@ void run_main(Bytefile* bf, int argc, char **argv) { case CMD_CTRL: switch (l) { case CMD_CTRL_CJMPz: { // CJMPz 0x%.8x - uint jmp_p = ip_read_int(&s.ip); + uint32_t jmp_p = ip_read_int(&s.ip); #ifndef WITH_CHECK if (jmp_p >= s.bf->code_size) { @@ -298,7 +298,7 @@ void run_main(Bytefile* bf, int argc, char **argv) { } case CMD_CTRL_CJMPnz: { // CJMPnz 0x%.8x - uint jmp_p = ip_read_int(&s.ip); + uint32_t jmp_p = ip_read_int(&s.ip); #ifndef WITH_CHECK if (jmp_p >= s.bf->code_size) { @@ -312,12 +312,12 @@ void run_main(Bytefile* bf, int argc, char **argv) { } case CMD_CTRL_BEGIN: { // BEGIN %d %d // function begin - uint args_sz = ip_read_int(&s.ip); + uint32_t args_sz = ip_read_int(&s.ip); // #ifdef WITH_CHECK - uint locals_sz = ip_read_half_int(&s.ip); - uint max_additional_stack_sz = ip_read_half_int(&s.ip); + uint32_t locals_sz = ip_read_half_int(&s.ip); + uint32_t max_additional_stack_sz = ip_read_half_int(&s.ip); // #else -// uint locals_sz = ip_read_int(&s.ip); +// uint32_t locals_sz = ip_read_int(&s.ip); // #endif #ifndef WITH_CHECK if (s.fp != NULL && s.call_ip == NULL) { @@ -336,12 +336,12 @@ void run_main(Bytefile* bf, int argc, char **argv) { case CMD_CTRL_CBEGIN: { // CBEGIN %d %d // NOTE: example not found, no checks done - uint args_sz = ip_read_int(&s.ip); + uint32_t args_sz = ip_read_int(&s.ip); // #ifdef WITH_CHECK - uint locals_sz = ip_read_half_int(&s.ip); - uint max_additional_stack_sz = ip_read_half_int(&s.ip); + uint32_t locals_sz = ip_read_half_int(&s.ip); + uint32_t max_additional_stack_sz = ip_read_half_int(&s.ip); // #else - // uint locals_sz = ip_read_int(&s.ip); + // uint32_t locals_sz = ip_read_int(&s.ip); // #endif #ifndef WITH_CHECK if (s.fp != NULL && s.call_ip == NULL) { @@ -403,7 +403,7 @@ void run_main(Bytefile* bf, int argc, char **argv) { } case CMD_CTRL_CALL: { // CALL 0x%.8x %d // call function - uint call_p = ip_read_int(&s.ip); + uint32_t call_p = ip_read_int(&s.ip); ip_read_int(&s.ip); // args count call_happened = true; diff --git a/byterun/stdlib_regression_check.sh b/byterun/stdlib_regression_check.sh index 673f5965f..92382f1cb 100755 --- a/byterun/stdlib_regression_check.sh +++ b/byterun/stdlib_regression_check.sh @@ -1,6 +1,10 @@ #!/usr/bin/env bash -dune build +# TODO: FIXME: error on test 3, possible UB or standard change +xmake build +cp "build/linux/x86_64/release/byterun" byterun.exe + +dune build > /dev/null prefix="../stdlib/regression/" suffix=".lama" @@ -21,7 +25,7 @@ for mod in ../stdlib/*.lama; do done echo "Run tests:" -for test in ../stdlib/regression/*.lama; do +for test in ../stdlib/regression/*03.lama; do echo $test $compiler -b $test -I ../stdlib/ > /dev/null test_path="${test%.*}" diff --git a/byterun/tutorial_check.sh b/byterun/tutorial_check.sh index 3fbcb90bc..e52e755f1 100755 --- a/byterun/tutorial_check.sh +++ b/byterun/tutorial_check.sh @@ -1,6 +1,9 @@ #!/usr/bin/env bash -dune build > /dev/null +xmake build +cp "build/linux/x86_64/release/byterun" byterun.exe + +# dune build > /dev/null prefix="../regression/" suffix=".lama" diff --git a/byterun/xmake.lua b/byterun/xmake.lua index 437b8814c..69000980f 100644 --- a/byterun/xmake.lua +++ b/byterun/xmake.lua @@ -1,14 +1,15 @@ -- add_rules("mode.debug", "mode.release") -- add_rules("c++.unity_build") -set_languages("c++20", "c20") +set_languages("c++23", "c23") target("byterun") set_kind("binary") add_includedirs("include") - add_files("../runtime/runtime.a") + add_files("../runtime/**.c", "../runtime/**.S") add_files("src/**.cpp", "src/**.c") remove_files("src/compiler.cpp") set_warnings("allextra") set_rundir("$(projectdir)") + add_defines("WITH_CHECK")