From c9feb113663e065d9ecce86f1855c4c93de83ebb Mon Sep 17 00:00:00 2001 From: apio Date: Tue, 15 Nov 2022 19:36:50 +0100 Subject: [PATCH] Introduce a check() method (like assert() but always on) --- kernel/CMakeLists.txt | 2 ++ kernel/src/main.cpp | 2 ++ luna/Check.h | 22 ++++++++++++++++++++++ luna/Result.h | 17 +++++++++-------- 4 files changed, 35 insertions(+), 8 deletions(-) create mode 100644 luna/Check.h diff --git a/kernel/CMakeLists.txt b/kernel/CMakeLists.txt index 65604d76..25eca3cd 100644 --- a/kernel/CMakeLists.txt +++ b/kernel/CMakeLists.txt @@ -27,6 +27,8 @@ add_executable(moon ${SOURCES}) target_link_libraries(moon moon-asm) +target_compile_definitions(moon PRIVATE IN_MOON) + target_compile_options(moon PRIVATE -Os) target_compile_options(moon PRIVATE -pedantic -Wall -Wextra -Werror -Wvla) diff --git a/kernel/src/main.cpp b/kernel/src/main.cpp index 8747cc4f..17499474 100644 --- a/kernel/src/main.cpp +++ b/kernel/src/main.cpp @@ -84,5 +84,7 @@ extern "C" [[noreturn]] void _start() Serial::println("Successfully unmapped address"); + check(false); + CPU::efficient_halt(); } \ No newline at end of file diff --git a/luna/Check.h b/luna/Check.h new file mode 100644 index 00000000..f8c4fd13 --- /dev/null +++ b/luna/Check.h @@ -0,0 +1,22 @@ +#pragma once +#ifdef IN_MOON +#include "arch/Serial.h" + +[[noreturn]] inline bool __check_failed(const char* string) +{ + Serial::print("CHECK FAILED: "); + Serial::println(string); + for (;;) + ; +} +#else +[[noreturn]] inline bool __check_failed(const char*) +{ + __builtin_trap(); +} +#endif + +#define STRINGIZE(x) #x +#define STRINGIZE_VALUE_OF(x) STRINGIZE(x) + +#define check(expr) (expr) || __check_failed("at " __FILE__ ":" STRINGIZE_VALUE_OF(__LINE__) ": " #expr) \ No newline at end of file diff --git a/luna/Result.h b/luna/Result.h index ca09050e..c5777205 100644 --- a/luna/Result.h +++ b/luna/Result.h @@ -1,4 +1,5 @@ #pragma once +#include #include #include #include @@ -81,19 +82,19 @@ template class Result int error() { - // ensure(has_error()); + check(has_error()); return m_error; } Error release_error() { - // ensure(has_error()); + check(has_error()); return {m_error}; } T value() { - // ensure(has_value()); + check(has_value()); return m_storage.fetch_reference(); } @@ -105,7 +106,7 @@ template class Result T release_value() { - // ensure(has_value()); + check(has_value()); T item = m_storage.fetch_reference(); m_has_value = false; m_storage.destroy(); @@ -206,25 +207,25 @@ template <> class Result int error() { - // ensure(has_error()); + check(has_error()); return m_error; } Error release_error() { - // ensure(has_error()); + check(has_error()); return {m_error}; } void value() { - // ensure(has_value()); + check(has_value()); return; } void release_value() { - // ensure(has_value()); + check(has_value()); return; }