diff --git a/src-self-hosted/stage1.zig b/src-self-hosted/stage1.zig index bf400113e4..f87c8a0e70 100644 --- a/src-self-hosted/stage1.zig +++ b/src-self-hosted/stage1.zig @@ -25,3 +25,7 @@ export fn stage2_zen(ptr: *[*]const u8, len: *usize) void { ptr.* = &info_zen; len.* = info_zen.len; } + +export fn stage2_panic(ptr: [*]const u8, len: usize) void { + @panic(ptr[0..len]); +} diff --git a/src/buffer.hpp b/src/buffer.hpp index 789abea3e9..082d584e2c 100644 --- a/src/buffer.hpp +++ b/src/buffer.hpp @@ -10,7 +10,6 @@ #include "list.hpp" -#include #include #include #include diff --git a/src/error.hpp b/src/error.hpp index d943703268..75ee801112 100644 --- a/src/error.hpp +++ b/src/error.hpp @@ -8,8 +8,6 @@ #ifndef ERROR_HPP #define ERROR_HPP -#include - enum Error { ErrorNone, ErrorNoMem, @@ -56,8 +54,6 @@ enum Error { const char *err_str(Error err); -static inline void assertNoError(Error err) { - assert(err == ErrorNone); -} +#define assertNoError(err) assert((err) == ErrorNone); #endif diff --git a/src/list.hpp b/src/list.hpp index b69a369f5a..f838e44a5b 100644 --- a/src/list.hpp +++ b/src/list.hpp @@ -10,8 +10,6 @@ #include "util.hpp" -#include - template struct ZigList { void deinit() { diff --git a/src/userland.cpp b/src/userland.cpp index 25b1492290..6c56bceaa0 100644 --- a/src/userland.cpp +++ b/src/userland.cpp @@ -2,9 +2,23 @@ // src-self-hosted/stage1.zig #include "userland.h" +#include +#include +#include -void stage2_translate_c(void) {} -void stage2_zen(const char **ptr, size_t *len) { - *ptr = nullptr; - *len = 0; +void stage2_translate_c(void) { + const char *msg = "stage0 called stage2_translate_c"; + stage2_panic(msg, strlen(msg)); +} + +void stage2_zen(const char **ptr, size_t *len) { + const char *msg = "stage0 called stage2_zen"; + stage2_panic(msg, strlen(msg)); +} + +void stage2_panic(const char *ptr, size_t len) { + fwrite(ptr, 1, len, stderr); + fprintf(stderr, "\n"); + fflush(stderr); + abort(); } diff --git a/src/userland.h b/src/userland.h index 92557ef994..a01bcc62c3 100644 --- a/src/userland.h +++ b/src/userland.h @@ -20,4 +20,6 @@ ZIG_USERLAND_EXTERN_C void stage2_translate_c(void); ZIG_USERLAND_EXTERN_C void stage2_zen(const char **ptr, size_t *len); +ZIG_USERLAND_EXTERN_C void stage2_panic(const char *ptr, size_t len); + #endif diff --git a/src/util.cpp b/src/util.cpp index 192d74e766..9a6a382993 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -10,17 +10,25 @@ #include #include "util.hpp" +#include "userland.h" void zig_panic(const char *format, ...) { va_list ap; va_start(ap, format); vfprintf(stderr, format, ap); - fprintf(stderr, "\n"); fflush(stderr); va_end(ap); + stage2_panic(nullptr, 0); abort(); } +void assert(bool ok) { + if (!ok) { + const char *msg = "Assertion failed. This is a bug in the Zig compiler."; + stage2_panic(msg, strlen(msg)); + } +} + uint32_t int_hash(int i) { return (uint32_t)(i % UINT32_MAX); } diff --git a/src/util.hpp b/src/util.hpp index 64c85033e3..f1942dd480 100644 --- a/src/util.hpp +++ b/src/util.hpp @@ -48,6 +48,10 @@ void zig_panic(const char *format, ...); #define zig_unreachable() zig_panic("unreachable: %s:%s:%d", __FILE__, __func__, __LINE__) +// Assertions in stage1 are always on, and they call zig @panic. +#undef assert +void assert(bool ok); + #if defined(_MSC_VER) static inline int clzll(unsigned long long mask) { unsigned long lz;