Commit deb3606

kravantokh <kravantokh@noreply.codeberg.org>
2026-07-05 14:08:30
Add some syscalls. Add unreachability macro.
1 parent 55f6d3d
platform/common/compiler.h
@@ -136,4 +136,16 @@ typedef struct _##name { \
 #define ASSERT( expression )                                                   \
 	typedef char UNIQUE_NAME(compile_time_assert)[(expression)? 1 : -1]
 
+
+#if COMPILER == COMPILER_GCC || COMPILER == COMPILER_CLANG
+	#define UNREACHABLE __builtin_unreachable()
+#elif COMPILER == MSVC
+	#define UNREACHABLE __assume(0)
+#endif
+
+#ifndef UNREACHABLE
+#define UNREACHABLE
+#warning UNREACHABLE is not defined for the current compiler. Compiler may not perform all optimizations it could based on unreachability in some special cases.
+#endif
+
 #endif
platform/linux/corefunc.h
@@ -1,5 +1,4 @@
-#include "../common/arch.h"
-#include "../common/compiler.h"
+#include "../common/build.h"
 
 static inline void breakpoint(void) {
 	__asm__ volatile (
@@ -7,8 +6,16 @@ static inline void breakpoint(void) {
 	);
 }
 
-static inline void terminationpoint(void) {
+static inline void terminate_forced(void) {
 	__asm__ volatile (
 		"ud2\n"
 	);
 }
+
+
+#include "./syscalls.h"
+
+static inline void terminate(u32 code) {
+	linux_exit(*(i32*)&code);
+	UNREACHABLE;
+}
platform/linux/linux_types.h
@@ -0,0 +1,3 @@
+#include "./coretypes.h"
+#include "../common/compiler.h"
+
platform/linux/syscalls.h
@@ -18,4 +18,28 @@ static inline usize linux_syscall6(usize num, usize arg1, usize arg2, usize arg3
 	#include "./syscall_wrappers_generic.h"
 #endif
 
+
+
+static inline usize linux_read( u32 fd, u8* buf, usize count ) {
+	return linux_syscall3( 0, fd, (usize)buf, count );
+}
+
+static inline usize linux_write( u32 fd, const u8* buf, usize count ) {
+	return linux_syscall3( 1, fd, (usize)buf, count );
+}
+
+static inline usize linux_open( u8* path, i32 flags, i32 mode ) {
+	return linux_syscall3( 2, (usize)path, *(u32*)&flags, *(u32*)&mode);
+}
+
+static inline usize linux_close( u32 fd ) {
+	return linux_syscall1( 3, fd );
+}
+
+static inline void linux_exit( i32 exitcode ) {
+	(void)linux_syscall1( 60, exitcode );
+	UNREACHABLE;
+}
+
+
 #endif
platform/platform.h
@@ -25,8 +25,11 @@ ASSERT( sizeof( f64 ) == 8 );
 
 /* Core platform functions */
 
+static inline void terminate(u32 code); /* aka exit */
+static inline void terminate_forced(void); /* force exit, fault, anything that terminates forcefully */
+
 static inline void breakpoint(void); /* raise a debug breakpoint, if possible on the target */
-static inline void terminationpoint(void); /* force exit, fault, anything that terminates forcefully */
+
 
 #define HEADER_PLATFORM_COREFUNC   \
 	LINUX("linux/corefunc.h")     \