Made the IncludePreprocessor a bit more robust (added -I option to dasm)

This commit is contained in:
OmniaX-Dev 2026-03-20 01:41:22 +01:00
parent 2486e5e4c0
commit 16b706175d
6 changed files with 120 additions and 83 deletions

View file

@ -3,7 +3,7 @@
.entry _kernel0_main .entry _kernel0_main
.header KERNEL0_BOOT .header KERNEL0_BOOT
@include <../../sdk/bios_api.dss> @include <sdk/bios_api.dss>
@include <../drivers/display_driver.dss> @include <../drivers/display_driver.dss>
@include <../drivers/keyboard_driver.dss> @include <../drivers/keyboard_driver.dss>
@include <memory.dss> @include <memory.dss>

View file

@ -6,19 +6,19 @@ green='\033[0;32m'
clear='\033[0m' clear='\033[0m'
printf "${green}Flashing BIOS...\n${clear}" 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}Compiling Drake...\n${clear}"
printf "\n${green}1) loader.dss\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 ./dtools load-binary dragon/disk1.dr dragon/drake_loader.bin 0x00000000
printf "\n${green}2) bootsector.dss\n${clear}" 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 ./dtools load-binary dragon/disk1.dr dragon/drake_bootsector.bin 0x00000400
printf "\n${green}Compiling DragonOS...\n${clear}" printf "\n${green}Compiling DragonOS...\n${clear}"
printf "\n${green}1) kernel0.dss\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 ./dtools load-binary dragon/disk1.dr dragon/kernel0.bin 0x00018000
printf "\n" printf "\n"

View file

@ -3,6 +3,7 @@
#include <ostd/utils/Utils.hpp> #include <ostd/utils/Utils.hpp>
#include <ostd/io/IOHandlers.hpp> #include <ostd/io/IOHandlers.hpp>
#include <unordered_map> #include <unordered_map>
#include <vector>
namespace dragon namespace dragon
{ {
@ -96,6 +97,7 @@ namespace dragon
bool save_exports { true }; bool save_exports { true };
ostd::String disassembly_file_path { "" }; ostd::String disassembly_file_path { "" };
std::vector<ostd::String> cpu_extensions; std::vector<ostd::String> cpu_extensions;
std::vector<ostd::String> include_directories;
}; };
public: public:
@ -108,6 +110,7 @@ namespace dragon
inline static const int32_t RETURN_VAL_CLOSE_PROGRAM = 512; 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_TOO_FEW_ARGUMENTS = 1;
inline static const int32_t RETURN_VAL_MISSING_PARAM = 2; inline static const int32_t RETURN_VAL_MISSING_PARAM = 2;
inline static const int32_t RETURN_VAL_INVALID_PARAM = 3;
}; };
public: public:

View file

@ -1,5 +1,6 @@
#include "Assembler.hpp" #include "Assembler.hpp"
#include <ostd/math/Random.hpp> #include <ostd/math/Random.hpp>
#include <ostd/io/FileSystem.hpp>
namespace dragon namespace dragon
{ {
@ -35,6 +36,19 @@ namespace dragon
i++; i++;
args.dest_file_path = argv[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") else if (edit == "--save-disassembly")
{ {
if (i == argc - 1) if (i == argc - 1)
@ -111,6 +125,9 @@ namespace dragon
tmpCommand = "--debug, -D"; tmpCommand = "--debug, -D";
tmpCommand.addRightPadding(commandLength); tmpCommand.addRightPadding(commandLength);
out.fg(ostd::ConsoleColors::Blue).p(tmpCommand).fg(ostd::ConsoleColors::Green).p("Used to enable debug mode.").reset().nl(); out.fg(ostd::ConsoleColors::Blue).p(tmpCommand).fg(ostd::ConsoleColors::Green).p("Used to enable debug mode.").reset().nl();
tmpCommand = "--include-path, -I <include-directory-path>";
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 <dasm> executable location).").reset().nl();
tmpCommand = "--help"; tmpCommand = "--help";
tmpCommand.addRightPadding(commandLength); tmpCommand.addRightPadding(commandLength);
out.fg(ostd::ConsoleColors::Blue).p(tmpCommand).fg(ostd::ConsoleColors::Green).p("Displays this help message.").reset().nl(); out.fg(ostd::ConsoleColors::Blue).p(tmpCommand).fg(ostd::ConsoleColors::Green).p("Displays this help message.").reset().nl();

View file

@ -1,24 +1,25 @@
#include "Assembler.hpp" #include "Assembler.hpp"
#include <ostd/io/File.hpp> #include <ostd/io/File.hpp>
#include <ostd/io/FileSystem.hpp>
namespace dragon namespace dragon
{ {
namespace code namespace code
{ {
std::vector<ostd::String> IncludePreprocessor::loadEntryFile(const ostd::String& filePath) std::vector<ostd::String> IncludePreprocessor::loadEntryFile(const ostd::String& filePath)
{ {
m_guards.clear(); m_guards.clear();
m_lines.clear(); m_lines.clear();
m_directory = ""; m_directory = "";
m_lines = __load_file(filePath); m_lines = __load_file(filePath);
if (m_lines.size() == 0) return { }; //TODO: Error if (m_lines.size() == 0) return { }; //TODO: Error
if (filePath.contains("/")) if (filePath.contains("/"))
m_directory = filePath.new_substr(0, filePath.lastIndexOf("/") + 1); m_directory = filePath.new_substr(0, filePath.lastIndexOf("/") + 1);
if (!__can_file_be_included(m_lines)) if (!__can_file_be_included(m_lines))
return { }; //TODO: Error return { }; //TODO: Error
if (!__include_loop()) return { }; //TODO: Error if (!__include_loop()) return { }; //TODO: Error
std::vector<ostd::String> newLines; std::vector<ostd::String> newLines;
for (auto& line : m_lines) for (auto& line : m_lines)
{ {
@ -28,75 +29,90 @@ namespace dragon
} }
m_lines.clear(); m_lines.clear();
m_lines = newLines; m_lines = newLines;
return m_lines; return m_lines;
} }
bool IncludePreprocessor::__can_file_be_included(std::vector<ostd::String>& lines) bool IncludePreprocessor::__can_file_be_included(std::vector<ostd::String>& lines)
{ {
ostd::String guard_name = ""; ostd::String guard_name = "";
for (auto& line : lines) for (auto& line : lines)
{ {
ostd::String lineEdit = line.new_trim(); ostd::String lineEdit = line.new_trim();
if (lineEdit.new_toLower().startsWith("@guard ")) if (lineEdit.new_toLower().startsWith("@guard "))
{ {
guard_name = lineEdit.new_substr(7).trim(); guard_name = lineEdit.new_substr(7).trim();
line = ""; line = "";
break; break;
} }
} }
if (guard_name == "") return true; if (guard_name == "") return true;
for (auto& guard : m_guards) for (auto& guard : m_guards)
{ {
if (guard == guard_name) if (guard == guard_name)
return false; return false;
} }
m_guards.push_back(guard_name); m_guards.push_back(guard_name);
return true; return true;
} }
bool IncludePreprocessor::__include_loop(void) bool IncludePreprocessor::__include_loop(void)
{ {
std::vector<ostd::String> lines = m_lines; std::vector<ostd::String> lines = m_lines;
bool included = false; bool included = false;
do do
{ {
included = false; included = false;
uint32_t i = 0; uint32_t i = 0;
for ( ; i < lines.size(); i++) for ( ; i < lines.size(); i++)
{ {
ostd::String line = lines[i]; ostd::String line = lines[i];
line.trim(); line.trim();
if (line.new_toLower().startsWith("@include") && line.len() > 8) if (line.new_toLower().startsWith("@include") && line.len() > 8)
{ {
line.substr(8).trim(); line.substr(8).trim();
if (!line.startsWith("<") || !line.endsWith(">")) if (!line.startsWith("<") || !line.endsWith(">"))
return false; return false;
line.substr(1, line.len() - 1).trim(); line.substr(1, line.len() - 1).trim();
auto file_lines = __load_file(m_directory + line); auto file_lines = __load_file(line);
if (file_lines.size() == 0) if (file_lines.size() == 0)
return false; return false;
lines.erase(lines.begin() + i); lines.erase(lines.begin() + i);
if (__can_file_be_included(file_lines)) if (__can_file_be_included(file_lines))
{ {
lines.insert(lines.begin() + i, file_lines.begin(), file_lines.end()); lines.insert(lines.begin() + i, file_lines.begin(), file_lines.end());
included = true; included = true;
} }
break; break;
} }
} }
} while(included); } while(included);
m_lines = lines; m_lines = lines;
return true; return true;
} }
std::vector<ostd::String> IncludePreprocessor::__load_file(const ostd::String& filePath) std::vector<ostd::String> IncludePreprocessor::__load_file(const ostd::String& filePath)
{ {
ostd::TextFileBuffer file(filePath); const auto& include_dirs = Assembler::Application::args.include_directories;
if (!file.exists())
return { }; //TODO: Error if (ostd::FileSystem::fileExists(m_directory + filePath))
ostd::String source = file.rawContent(); {
return ostd::String(source.replaceAll("\t", " ")).tokenize("\n", false).getRawData(); 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
}
} }
} }

View file

@ -1,6 +1,7 @@
#include "DisassemblyLoader.hpp" #include "DisassemblyLoader.hpp"
#include <ostd/io/File.hpp> #include <ostd/io/File.hpp>
#include <ostd/io/FileSystem.hpp>
#include <ostd/io/Serial.hpp> #include <ostd/io/Serial.hpp>
#include <algorithm> #include <algorithm>
@ -92,7 +93,7 @@ namespace dragon
void DisassemblyLoader::loadDirectory(const ostd::String& directoryPath) void DisassemblyLoader::loadDirectory(const ostd::String& directoryPath)
{ {
auto list = ostd::Utils::listFilesInDirectory(directoryPath); auto list = ostd::FileSystem::listFilesInDirectory(directoryPath);
for (auto& path : list) for (auto& path : list)
loadFile(path.string()); loadFile(path.string());
} }