From de96b0588ab680e1002c12df7ea7900d7eb71cf8 Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Fri, 19 Dec 2025 11:46:46 +0100 Subject: kstd: move println to kstd --- kernel/CMakeLists.txt | 5 +- kernel/src/kapi/cio.cpp | 183 +-------------------------------------------- kernel/src/kapi/system.cpp | 7 +- kernel/src/kstd.cpp | 16 ---- kernel/src/kstd/os.cpp | 16 ++++ kernel/src/kstd/print.cpp | 145 +++++++++++++++++++++++++++++++++++ kernel/src/main.cpp | 6 +- 7 files changed, 177 insertions(+), 201 deletions(-) delete mode 100644 kernel/src/kstd.cpp create mode 100644 kernel/src/kstd/os.cpp create mode 100644 kernel/src/kstd/print.cpp (limited to 'kernel') diff --git a/kernel/CMakeLists.txt b/kernel/CMakeLists.txt index 6bddf7c..7733c1b 100644 --- a/kernel/CMakeLists.txt +++ b/kernel/CMakeLists.txt @@ -1,7 +1,10 @@ add_executable("kernel" - "src/kstd.cpp" "src/main.cpp" + # KSTD OS Implementation + "src/kstd/os.cpp" + "src/kstd/print.cpp" + # Platform Independent KAPI implementation "src/kapi/cio.cpp" "src/kapi/memory.cpp" diff --git a/kernel/src/kapi/cio.cpp b/kernel/src/kapi/cio.cpp index 500d61e..fd5ad0d 100644 --- a/kernel/src/kapi/cio.cpp +++ b/kernel/src/kapi/cio.cpp @@ -1,10 +1,5 @@ #include "kapi/cio.hpp" -#include - -#include -#include -#include #include #include #include @@ -16,72 +11,10 @@ namespace teachos::cio struct null_device final : public output_device { null_device static instance; - - auto write(std::string_view) -> void override {} - auto writeln(std::string_view) -> void override {} - - auto write_error(std::string_view) -> void override {} - auto writeln_error(std::string_view) -> void override {} + auto write(output_stream, std::string_view) -> void override {} }; constinit null_device null_device::instance; - - struct write_buffer - { - constexpr auto static size = 128uz; - - write_buffer(write_buffer const &) = delete; - write_buffer(write_buffer &&) = delete; - auto operator=(write_buffer const &) -> write_buffer & = delete; - auto operator=(write_buffer &&) -> write_buffer & = delete; - - explicit write_buffer(output_device * device, bool write_to_error) - : m_device{device} - , m_write_to_error{write_to_error} - {} - - ~write_buffer() noexcept - { - flush(); - } - - auto flush() noexcept -> void - { - if (m_position > 0) - { - std::string_view chunk{m_buffer.data(), m_position}; - if (m_write_to_error) - { - m_device->write_error(chunk); - } - else - { - m_device->write(chunk); - } - m_position = 0; - } - } - - auto static callback(void * object, std::string_view text) -> void - { - auto * self = static_cast(object); - for (char const character : text) - { - if (self->m_position >= size) - { - self->flush(); - } - self->m_buffer.at(self->m_position++) = character; - } - } - - private: - output_device * m_device; - bool m_write_to_error; - std::array m_buffer{}; - std::size_t m_position{}; - }; - } // namespace // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables) @@ -96,117 +29,9 @@ namespace teachos::cio return std::exchange(active_device, &device); } - auto print(std::string_view text) -> void - { - active_device->write(text); - } - - auto println(std::string_view text) -> void - { - active_device->writeln(text); - } - - auto print_error(std::string_view text) -> void - { - active_device->write_error(text); - } - - auto println_error(std::string_view text) -> void - { - active_device->writeln_error(text); - } - - namespace - { - auto static vprint_impl(std::string_view fmt, kstd::format_args args, bool write_to_error) -> void - { - if (!active_device) - return; - - auto writer = write_buffer{active_device, write_to_error}; - auto context = kstd::format_context{.writer = write_buffer::callback, .user_data = &writer}; - - auto current = fmt.begin(); - auto end = fmt.end(); - auto next_automatic_index = 0uz; - - while (current != end) - { - if (*current != '{') - { - auto start = current; - while (current != end && *current != '{') - { - std::advance(current, 1); - } - context.push(std::string_view(start, current - start)); - continue; - } - - if (std::next(current) != end && *(std::next(current)) == '{') - { - context.push('{'); - std::advance(current, 2); - continue; - } - - std::advance(current, 1); - - auto index = 0uz; - if (current != end && *current >= '0' && *current <= '9') - { - while (current != end && *current >= '0' && *current <= '9') - { - // NOLINTNEXTLINE(cppcoreguidelines-avoid-magic-numbers) - index = index * 10 + static_cast(*current - '0'); - std::advance(current, 1); - } - } - else - { - index = next_automatic_index++; - } - - auto remaining_fmt = std::string_view{current, static_cast(std::distance(current, end))}; - - auto const arg = args.get(index); - if (arg.format) - { - auto const after_specs = arg.format(arg.value, remaining_fmt, context); - auto const consumed = remaining_fmt.size() - after_specs.size(); - std::advance(current, consumed); - } - else - { - context.push("{?}"); - while (current != end && *current != '}') - std::advance(current, 1); - } - - if (current != end && *current == '}') - { - std::advance(current, 1); - } - else - { - context.push("{fmt-err}"); - while (current != end && *current != '}') - std::advance(current, 1); - if (current != end) - std::advance(current, 1); - } - } - } - } // namespace - - auto vprint(std::string_view fmt, kstd::format_args args) -> void - { - vprint_impl(fmt, args, false); - } - - auto vprint_error(std::string_view fmt, kstd::format_args args) -> void + auto write(output_stream stream, std::string_view text) -> void { - vprint_impl(fmt, args, true); + active_device->write(stream, text); } -} // namespace teachos::cio \ No newline at end of file +} // namespace teachos::cio diff --git a/kernel/src/kapi/system.cpp b/kernel/src/kapi/system.cpp index 1cca82e..cdde049 100644 --- a/kernel/src/kapi/system.cpp +++ b/kernel/src/kapi/system.cpp @@ -1,8 +1,9 @@ #include "kapi/system.hpp" -#include "kapi/cio.hpp" #include "kapi/cpu.hpp" +#include + #include #include @@ -12,8 +13,8 @@ namespace teachos::system [[gnu::weak]] auto panic(std::string_view message, std::source_location location) -> void { - cio::println_error("[PANIC] in {} : {} @ {}:{}", location.function_name(), message, location.file_name(), - location.line()); + kstd::println(kstd::print_sink::stderr, "[PANIC] in {} : {} @ {}:{}", location.function_name(), message, + location.file_name(), location.line()); cpu::halt(); } diff --git a/kernel/src/kstd.cpp b/kernel/src/kstd.cpp deleted file mode 100644 index 41c4d60..0000000 --- a/kernel/src/kstd.cpp +++ /dev/null @@ -1,16 +0,0 @@ -#include "kapi/system.hpp" - -#include - -#include -#include - -namespace kstd::os -{ - - auto panic(std::string_view message, std::source_location location) -> void - { - teachos::system::panic(message, location); - } - -} // namespace kstd::os \ No newline at end of file diff --git a/kernel/src/kstd/os.cpp b/kernel/src/kstd/os.cpp new file mode 100644 index 0000000..41c4d60 --- /dev/null +++ b/kernel/src/kstd/os.cpp @@ -0,0 +1,16 @@ +#include "kapi/system.hpp" + +#include + +#include +#include + +namespace kstd::os +{ + + auto panic(std::string_view message, std::source_location location) -> void + { + teachos::system::panic(message, location); + } + +} // namespace kstd::os \ No newline at end of file diff --git a/kernel/src/kstd/print.cpp b/kernel/src/kstd/print.cpp new file mode 100644 index 0000000..d2fdafa --- /dev/null +++ b/kernel/src/kstd/print.cpp @@ -0,0 +1,145 @@ +#include "kapi/cio.hpp" + +#include +#include + +#include +#include +#include +#include + +namespace kstd::os +{ + + namespace + { + struct write_buffer + { + using output_stream = teachos::cio::output_stream; + + constexpr auto static size = 128uz; + + write_buffer(write_buffer const &) = delete; + write_buffer(write_buffer &&) = delete; + auto operator=(write_buffer const &) -> write_buffer & = delete; + auto operator=(write_buffer &&) -> write_buffer & = delete; + + explicit write_buffer(output_stream stream) + : m_stream{stream} + {} + + ~write_buffer() noexcept + { + flush(); + } + + auto flush() noexcept -> void + { + if (m_position > 0) + { + std::string_view chunk{m_buffer.data(), m_position}; + teachos::cio::write(m_stream, chunk); + m_position = 0; + } + } + + auto static callback(void * object, std::string_view text) -> void + { + auto * self = static_cast(object); + for (char const character : text) + { + if (self->m_position >= size) + { + self->flush(); + } + self->m_buffer.at(self->m_position++) = character; + } + } + + private: + output_stream m_stream; + std::array m_buffer{}; + std::size_t m_position{}; + }; + + } // namespace + + auto vprint(print_sink sink, std::string_view format, kstd::format_args args) -> void + { + auto writer = write_buffer{(sink == print_sink::stderr) ? teachos::cio::output_stream::stderr + : teachos::cio::output_stream::stdout}; + auto context = kstd::format_context{.writer = write_buffer::callback, .user_data = &writer}; + + auto current = format.begin(); + auto end = format.end(); + auto next_automatic_index = 0uz; + + while (current != end) + { + if (*current != '{') + { + auto start = current; + while (current != end && *current != '{') + { + std::advance(current, 1); + } + context.push(std::string_view(start, current - start)); + continue; + } + + if (std::next(current) != end && *(std::next(current)) == '{') + { + context.push('{'); + std::advance(current, 2); + continue; + } + + std::advance(current, 1); + + auto index = 0uz; + if (current != end && *current >= '0' && *current <= '9') + { + while (current != end && *current >= '0' && *current <= '9') + { + // NOLINTNEXTLINE(cppcoreguidelines-avoid-magic-numbers) + index = index * 10 + static_cast(*current - '0'); + std::advance(current, 1); + } + } + else + { + index = next_automatic_index++; + } + + auto remaining_fmt = std::string_view{current, static_cast(std::distance(current, end))}; + + auto const arg = args.get(index); + if (arg.format) + { + auto const after_specs = arg.format(arg.value, remaining_fmt, context); + auto const consumed = remaining_fmt.size() - after_specs.size(); + std::advance(current, consumed); + } + else + { + context.push("{?}"); + while (current != end && *current != '}') + std::advance(current, 1); + } + + if (current != end && *current == '}') + { + std::advance(current, 1); + } + else + { + context.push("{fmt-err}"); + while (current != end && *current != '}') + std::advance(current, 1); + if (current != end) + std::advance(current, 1); + } + } + } + +} // namespace kstd::os diff --git a/kernel/src/main.cpp b/kernel/src/main.cpp index 3394275..f1e5dd0 100644 --- a/kernel/src/main.cpp +++ b/kernel/src/main.cpp @@ -2,13 +2,15 @@ #include "kapi/memory.hpp" #include "kapi/system.hpp" +#include + auto main() -> int { teachos::cio::init(); - teachos::cio::println("[OS] IO subsystem initialized."); + kstd::println("[OS] IO subsystem initialized."); teachos::memory::init(); - teachos::cio::println("[OS] Memory subsystem initialized."); + kstd::println("[OS] Memory subsystem initialized."); teachos::system::panic("Returning from kernel main!"); } -- cgit v1.2.3