From 16b706175dc14cea1c8b5d41e67c9cb57f09f2f2 Mon Sep 17 00:00:00 2001 From: OmniaX-Dev Date: Fri, 20 Mar 2026 01:41:22 +0100 Subject: [PATCH] Made the IncludePreprocessor a bit more robust (added -I option to dasm) --- extra/dss/DragonOS/kernel0/kernel0.dss | 2 +- extra/reload_env.sh | 8 +- src/assembler/Assembler.hpp | 3 + src/assembler/DASMApp.cpp | 17 +++ src/assembler/IncludePreprocessor.cpp | 170 ++++++++++++++----------- src/debugger/DisassemblyLoader.cpp | 3 +- 6 files changed, 120 insertions(+), 83 deletions(-) diff --git a/extra/dss/DragonOS/kernel0/kernel0.dss b/extra/dss/DragonOS/kernel0/kernel0.dss index 3841000..63145fb 100644 --- a/extra/dss/DragonOS/kernel0/kernel0.dss +++ b/extra/dss/DragonOS/kernel0/kernel0.dss @@ -3,7 +3,7 @@ .entry _kernel0_main .header KERNEL0_BOOT -@include <../../sdk/bios_api.dss> +@include @include <../drivers/display_driver.dss> @include <../drivers/keyboard_driver.dss> @include diff --git a/extra/reload_env.sh b/extra/reload_env.sh index f993d9d..e6c6e06 100755 --- a/extra/reload_env.sh +++ b/extra/reload_env.sh @@ -6,19 +6,19 @@ green='\033[0;32m' clear='\033[0m' printf "${green}Flashing BIOS...\n${clear}" -./dasm dss/bios/entry.dss -o dragon/bios.bin $1 +./dasm dss/bios/entry.dss -o dragon/bios.bin -I dss $1 printf "\n${green}Compiling Drake...\n${clear}" printf "\n${green}1) loader.dss\n${clear}" -./dasm dss/drake/loader.dss -o dragon/drake_loader.bin $1 +./dasm dss/drake/loader.dss -o dragon/drake_loader.bin -I dss $1 ./dtools load-binary dragon/disk1.dr dragon/drake_loader.bin 0x00000000 printf "\n${green}2) bootsector.dss\n${clear}" -./dasm dss/drake/bootsector.dss -o dragon/drake_bootsector.bin $1 +./dasm dss/drake/bootsector.dss -o dragon/drake_bootsector.bin -I dss $1 ./dtools load-binary dragon/disk1.dr dragon/drake_bootsector.bin 0x00000400 printf "\n${green}Compiling DragonOS...\n${clear}" printf "\n${green}1) kernel0.dss\n${clear}" -./dasm dss/DragonOS/kernel0/kernel0.dss -o dragon/kernel0.bin $1 +./dasm dss/DragonOS/kernel0/kernel0.dss -o dragon/kernel0.bin -I dss $1 ./dtools load-binary dragon/disk1.dr dragon/kernel0.bin 0x00018000 printf "\n" diff --git a/src/assembler/Assembler.hpp b/src/assembler/Assembler.hpp index 385d6af..02b2381 100644 --- a/src/assembler/Assembler.hpp +++ b/src/assembler/Assembler.hpp @@ -3,6 +3,7 @@ #include #include #include +#include namespace dragon { @@ -96,6 +97,7 @@ namespace dragon bool save_exports { true }; ostd::String disassembly_file_path { "" }; std::vector cpu_extensions; + std::vector include_directories; }; public: @@ -108,6 +110,7 @@ namespace dragon inline static const int32_t RETURN_VAL_CLOSE_PROGRAM = 512; inline static const int32_t RETURN_VAL_TOO_FEW_ARGUMENTS = 1; inline static const int32_t RETURN_VAL_MISSING_PARAM = 2; + inline static const int32_t RETURN_VAL_INVALID_PARAM = 3; }; public: diff --git a/src/assembler/DASMApp.cpp b/src/assembler/DASMApp.cpp index 72b90e8..85aa5d1 100644 --- a/src/assembler/DASMApp.cpp +++ b/src/assembler/DASMApp.cpp @@ -1,5 +1,6 @@ #include "Assembler.hpp" #include +#include namespace dragon { @@ -35,6 +36,19 @@ namespace dragon i++; args.dest_file_path = argv[i]; } + else if (edit == "-I" || edit == "--include-path") + { + if (i == argc - 1) + return RETURN_VAL_MISSING_PARAM; + i++; + ostd::String _include = argv[i]; + _include.trim(); + if (!_include.endsWith("/")) + _include.add("/"); + if (!ostd::FileSystem::directoryExists(_include)) + return RETURN_VAL_INVALID_PARAM; + args.include_directories.push_back(_include); + } else if (edit == "--save-disassembly") { if (i == argc - 1) @@ -111,6 +125,9 @@ namespace dragon tmpCommand = "--debug, -D"; tmpCommand.addRightPadding(commandLength); out.fg(ostd::ConsoleColors::Blue).p(tmpCommand).fg(ostd::ConsoleColors::Green).p("Used to enable debug mode.").reset().nl(); + tmpCommand = "--include-path, -I "; + tmpCommand.addRightPadding(commandLength); + out.fg(ostd::ConsoleColors::Blue).p(tmpCommand).fg(ostd::ConsoleColors::Green).p("Used specify an include directory path (absolute or relative to the executable location).").reset().nl(); tmpCommand = "--help"; tmpCommand.addRightPadding(commandLength); out.fg(ostd::ConsoleColors::Blue).p(tmpCommand).fg(ostd::ConsoleColors::Green).p("Displays this help message.").reset().nl(); diff --git a/src/assembler/IncludePreprocessor.cpp b/src/assembler/IncludePreprocessor.cpp index a5271c4..7e833ba 100644 --- a/src/assembler/IncludePreprocessor.cpp +++ b/src/assembler/IncludePreprocessor.cpp @@ -1,24 +1,25 @@ #include "Assembler.hpp" #include +#include namespace dragon { namespace code { std::vector IncludePreprocessor::loadEntryFile(const ostd::String& filePath) - { - m_guards.clear(); - m_lines.clear(); - m_directory = ""; - m_lines = __load_file(filePath); + { + m_guards.clear(); + m_lines.clear(); + m_directory = ""; + m_lines = __load_file(filePath); - if (m_lines.size() == 0) return { }; //TODO: Error - if (filePath.contains("/")) - m_directory = filePath.new_substr(0, filePath.lastIndexOf("/") + 1); - if (!__can_file_be_included(m_lines)) - return { }; //TODO: Error - if (!__include_loop()) return { }; //TODO: Error + if (m_lines.size() == 0) return { }; //TODO: Error + if (filePath.contains("/")) + m_directory = filePath.new_substr(0, filePath.lastIndexOf("/") + 1); + if (!__can_file_be_included(m_lines)) + return { }; //TODO: Error + if (!__include_loop()) return { }; //TODO: Error std::vector newLines; for (auto& line : m_lines) { @@ -28,75 +29,90 @@ namespace dragon } m_lines.clear(); m_lines = newLines; - return m_lines; - } + return m_lines; + } - bool IncludePreprocessor::__can_file_be_included(std::vector& lines) - { - ostd::String guard_name = ""; - for (auto& line : lines) - { - ostd::String lineEdit = line.new_trim(); - if (lineEdit.new_toLower().startsWith("@guard ")) - { - guard_name = lineEdit.new_substr(7).trim(); - line = ""; - break; - } - } - if (guard_name == "") return true; - for (auto& guard : m_guards) - { - if (guard == guard_name) - return false; - } - m_guards.push_back(guard_name); - return true; - } + bool IncludePreprocessor::__can_file_be_included(std::vector& lines) + { + ostd::String guard_name = ""; + for (auto& line : lines) + { + ostd::String lineEdit = line.new_trim(); + if (lineEdit.new_toLower().startsWith("@guard ")) + { + guard_name = lineEdit.new_substr(7).trim(); + line = ""; + break; + } + } + if (guard_name == "") return true; + for (auto& guard : m_guards) + { + if (guard == guard_name) + return false; + } + m_guards.push_back(guard_name); + return true; + } - bool IncludePreprocessor::__include_loop(void) - { - std::vector lines = m_lines; - bool included = false; - do - { - included = false; - uint32_t i = 0; - for ( ; i < lines.size(); i++) - { - ostd::String line = lines[i]; - line.trim(); - if (line.new_toLower().startsWith("@include") && line.len() > 8) - { - line.substr(8).trim(); - if (!line.startsWith("<") || !line.endsWith(">")) - return false; - line.substr(1, line.len() - 1).trim(); - auto file_lines = __load_file(m_directory + line); - if (file_lines.size() == 0) - return false; - lines.erase(lines.begin() + i); - if (__can_file_be_included(file_lines)) - { - lines.insert(lines.begin() + i, file_lines.begin(), file_lines.end()); - included = true; - } - break; - } - } - } while(included); - m_lines = lines; - return true; - } + bool IncludePreprocessor::__include_loop(void) + { + std::vector lines = m_lines; + bool included = false; + do + { + included = false; + uint32_t i = 0; + for ( ; i < lines.size(); i++) + { + ostd::String line = lines[i]; + line.trim(); + if (line.new_toLower().startsWith("@include") && line.len() > 8) + { + line.substr(8).trim(); + if (!line.startsWith("<") || !line.endsWith(">")) + return false; + line.substr(1, line.len() - 1).trim(); + auto file_lines = __load_file(line); + if (file_lines.size() == 0) + return false; + lines.erase(lines.begin() + i); + if (__can_file_be_included(file_lines)) + { + lines.insert(lines.begin() + i, file_lines.begin(), file_lines.end()); + included = true; + } + break; + } + } + } while(included); + m_lines = lines; + return true; + } - std::vector IncludePreprocessor::__load_file(const ostd::String& filePath) - { - ostd::TextFileBuffer file(filePath); - if (!file.exists()) - return { }; //TODO: Error - ostd::String source = file.rawContent(); - return ostd::String(source.replaceAll("\t", " ")).tokenize("\n", false).getRawData(); - } + std::vector IncludePreprocessor::__load_file(const ostd::String& filePath) + { + const auto& include_dirs = Assembler::Application::args.include_directories; + + if (ostd::FileSystem::fileExists(m_directory + filePath)) + { + ostd::TextFileBuffer file(m_directory + filePath); + ostd::String source = file.rawContent(); + return ostd::String(source.replaceAll("\t", " ")).tokenize("\n", false).getRawData(); + } + + for (const auto& dir : include_dirs) + { + if (ostd::FileSystem::fileExists(dir + filePath)) + { + ostd::TextFileBuffer file(dir + filePath); + ostd::String source = file.rawContent(); + return ostd::String(source.replaceAll("\t", " ")).tokenize("\n", false).getRawData(); + } + } + + return { }; //TODO: Error + } } } diff --git a/src/debugger/DisassemblyLoader.cpp b/src/debugger/DisassemblyLoader.cpp index 88343d2..5c9bf15 100644 --- a/src/debugger/DisassemblyLoader.cpp +++ b/src/debugger/DisassemblyLoader.cpp @@ -1,6 +1,7 @@ #include "DisassemblyLoader.hpp" #include +#include #include #include @@ -92,7 +93,7 @@ namespace dragon void DisassemblyLoader::loadDirectory(const ostd::String& directoryPath) { - auto list = ostd::Utils::listFilesInDirectory(directoryPath); + auto list = ostd::FileSystem::listFilesInDirectory(directoryPath); for (auto& path : list) loadFile(path.string()); }