diff --git a/src-self-hosted/stage1.zig b/src-self-hosted/stage1.zig index bf400113e4fd8f3e1bbf30d6f78d0b5b17a83eaf..f87c8a0e70fa3bc15235386668ca702c8fa950e5 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 789abea3e9ee0a441886bd410d64f184287f2751..082d584e2cf58f96eec83ac060d53225042168aa 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 d943703268a64afebb0b8b02dc0a8f31ae32f5db..75ee801112fee680ffef13f3df8756cf7e0d5505 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 b69a369f5aba88bb453ec66186ccf6523ac00e35..f838e44a5b8e34c8a1db4e47a2b1d0c008635e41 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 25b1492290b7ab34ed3313494b7eca472c8801cb..6c56bceaa0f4f2a422fcfb120136f11aea02c1d2 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) { + const char *msg = "stage0 called stage2_translate_c"; + stage2_panic(msg, strlen(msg)); +} -void stage2_translate_c(void) {} void stage2_zen(const char **ptr, size_t *len) { - *ptr = nullptr; - *len = 0; + 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 92557ef9945322cd271def59924c0627313b080b..a01bcc62c3dccc5775a94acaf1950a892a8960cc 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 192d74e7668a9b5a64445750c8bd1b7c2a6188b8..9a6a3829934d5b18b3e4692adf679e7aba64f316 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 64c85033e3752c770eea81613fecf6ce52d80b34..f1942dd48034c8b84280a6998754540b7db67f77 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;