From 26d2eaa8b3c37519a9d22bc5648e6ced538548dd Mon Sep 17 00:00:00 2001 From: ProgramSnail Date: Sun, 1 Jun 2025 17:47:35 +0300 Subject: [PATCH] basic compiler integration (with errors, without check) --- byterun/dune | 4 +++- byterun/include/compiler.hpp | 6 ++++++ byterun/src/cli.cpp | 10 +++++++++- byterun/src/compiler.cpp | 13 +++++++++++++ byterun/xmake.lua | 1 - 5 files changed, 31 insertions(+), 3 deletions(-) create mode 100644 byterun/include/compiler.hpp diff --git a/byterun/dune b/byterun/dune index 79f9552b0..8e5707a29 100644 --- a/byterun/dune +++ b/byterun/dune @@ -6,12 +6,14 @@ (:parser src/parser.cpp) (:analyzer src/analyzer.cpp) (:module_manager src/module_manager.cpp) + (:sm_parser src/sm_parser.cpp) + (:compiler src/compiler.cpp) (:obj types.o interpreter.o) (:runtime ../runtime/runtime.a)) (mode (promote (until-clean))) (action - (run g++ -Wall -Wextra -O3 -std=c++20 -DWITH_CHECK -Iinclude/ %{main} %{parser} %{analyzer} %{module_manager} %{runtime} %{obj} -o %{target}))) + (run g++ -Wall -Wextra -O3 -std=c++20 -DWITH_CHECK -Iinclude/ %{main} %{parser} %{analyzer} %{module_manager} %{sm_parser} %{compiler} %{runtime} %{obj} -o %{target}))) (rule (target types.o) diff --git a/byterun/include/compiler.hpp b/byterun/include/compiler.hpp new file mode 100644 index 000000000..f7b0d17af --- /dev/null +++ b/byterun/include/compiler.hpp @@ -0,0 +1,6 @@ +#pragma once + +#include "sm_parser.hpp" +#include + +std::vector compile_to_code(const std::vector &code); diff --git a/byterun/src/cli.cpp b/byterun/src/cli.cpp index 52e0a576b..ab2875363 100644 --- a/byterun/src/cli.cpp +++ b/byterun/src/cli.cpp @@ -2,6 +2,7 @@ #include #include "analyzer.hpp" +#include "compiler.hpp" #include "parser.hpp" #include "sm_parser.hpp" @@ -24,7 +25,7 @@ int main(int argc, char **argv) { bool do_print = false; if (strcmp(argv[1], "-i") == 0) { do_interpretation = true; - } else if (strcmp(argv[1], "-sm") == 0) { // TODO: TMP, FOR CHECKS + } else if (strcmp(argv[1], "-ds") == 0) { // TODO: TMP, FOR CHECKS std::ifstream file(argv[2]); std::cout << "-- parse\n"; auto instrs = parse_sm(file); @@ -33,6 +34,13 @@ int main(int argc, char **argv) { std::cout << print_sm(instr) << "\n"; } return 0; + } else if (strcmp(argv[1], "-s") == 0) { + std::ifstream file(argv[2]); + auto instrs = compile_to_code(parse_sm(file)); + for (auto &instr : instrs) { + std::cout << instr << "\n"; + } + return 0; } #ifdef WITH_CHECK else if (strcmp(argv[1], "-vi") == 0) { diff --git a/byterun/src/compiler.cpp b/byterun/src/compiler.cpp index f6cd4ae50..c86b696b4 100644 --- a/byterun/src/compiler.cpp +++ b/byterun/src/compiler.cpp @@ -2,6 +2,7 @@ #include "../../runtime/runtime.h" +#include "compiler.hpp" #include "sm_parser.hpp" #include @@ -2115,3 +2116,15 @@ std::vector compile(const Options &cmd, Env &env, } return result; } + +std::vector compile_to_code(const std::vector &code) { + Options cmd{.topname = "byterun", .filename = "byterun"}; // TODO TMP + Env env(Mode{.is_debug = true, .target_os = OS::LINUX}); + + auto asm_code = compile(cmd, env, {/*imports (TODO TMP)*/}, code); + std::vector res; + std::transform(asm_code.begin(), asm_code.end(), std::back_inserter(res), + [&env](const auto &instr) { return to_code(env, instr); }); + + return res; +} diff --git a/byterun/xmake.lua b/byterun/xmake.lua index 69000980f..b9e86f1df 100644 --- a/byterun/xmake.lua +++ b/byterun/xmake.lua @@ -8,7 +8,6 @@ target("byterun") add_includedirs("include") 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")