Commit a932036

kravantokh <kravantokh@noreply.codeberg.org>
2026-07-04 17:03:20
Implement platform info and start linux platform implementation.
platform/common/arch.h
@@ -0,0 +1,33 @@
+#ifndef NOTLIBC_HEADER_PLATFORM_ARCH_INCLUDED
+#define NOTLIBC_HEADER_PLATFORM_ARCH_INCLUDED
+
+#define ARCH_I386 1
+#define ARCH_X86_64 2
+
+
+#ifndef ARCH
+	#if defined(__x86_64__) || defined(_M_X64)
+		#define ARCH ARCH_X86_64
+		#define ARCH_NAME "x86_64"
+		#define X86_64(...) __VA_ARGS__
+	#elif defined(__i386__) || defined(_M_IX86)
+		#define ARCH ARCH_I386
+		#define ARCH_NAME "i386"
+		#define I386(...) __VA_ARGS__
+	#endif
+#endif
+
+#if !defined(ARCH) || !defined(ARCH_NAME)
+	#error Failed to identify architecture. Please specify it via -DARCH=number (choose the most compatible option from above) and -DARCH_NAME=name to override the automatic detection.
+#endif
+
+#ifndef X86_64
+#define X86_64(...)
+#endif
+
+#ifndef I386
+#define I386(...)
+#endif
+
+
+#endif
platform/common/build.h
@@ -0,0 +1,35 @@
+#ifndef NOTLIBC_HEADER_BUILD_INCLUDED
+#define NOTLIBC_HEADER_BUILD_INCLUDED
+
+#define BUILD_DEBUG 1
+#define BUILD_RELEASE 2
+
+#ifndef BUILD_TYPE
+#define BUILD_TYPE 0
+#endif
+
+#if BUILD_TYPE == 1
+	#define BUILD BUILD_DEBUG
+	#define DEBUG(...) __VA_ARGS__
+#elif BUILD_TYPE == 2
+	#define BUILD BUILD_RELEASE
+	#define RELEASE(...) __VA_ARGS__
+#endif
+
+#ifndef BUILD
+	#error No build type was specified. There is no defaulting to debug builds.
+#endif
+
+#ifndef DEBUG
+#define DEBUG(...) 
+#endif
+
+#ifndef RELEASE
+#define RELEASE(...) 
+#endif
+
+#ifdef NOTLIBC_IMPLEMENT
+#define IMPL(...) __VA_ARGS__
+#endif
+
+#endif
platform/common/compiler.h
@@ -0,0 +1,139 @@
+#ifndef NOTLIBC_HEADER_PLATFORM_COMPILER_INCLUDED
+#define NOTLIBC_HEADER_PLATFORM_COMPILER_INCLUDED
+
+/* Compiler information detection */
+
+#define COMPILER_GCC 1
+#define COMPILER_CLANG 2
+#define COMPILER_TCC 3
+#define COMPILER_MSVC 4
+
+#ifndef COMPILER /* skip if -DCOMPILER=<something> was used */
+	#if defined(__clang__) || defined(__llvm__)
+		#define COMPILER COMPILER_CLANG
+		#define COMPILER_NAME "clang"
+
+		#define CLANG(...) __VA_ARGS__
+		#define GCCLIKE(...) __VA_ARGS__
+
+	#elif defined(__GNUC__) && !defined(__clang__) && !defined(__llvm__) && !defined(__INTEL_COMPILER)
+		#define COMPILER COMPILER_GCC
+		#define COMPILER_NAME "gcc"
+
+		#define GCC(...) __VA_ARGS__
+		#define GCCLIKE(...) __VA_ARGS__
+
+	#elif defined(__TINYC__)
+		#define COMPILER COMPILER_TCC
+		#define COMPILER_NAME "tcc"
+
+		#define TCC(...) __VA_ARGS__
+
+	#elif defined( _MSC_VER )
+		#define COMPILER_MSVC
+		#define COMPILER_NAME "msvc"
+
+		#define MSVC(...) __VA_ARGS__
+	#endif
+#endif
+
+#if !defined(COMPILER) || !defined(COMPILER_NAME)
+	#error Failed to identify compiler. Please specify it via -DCOMPILER=number (choose the most compatible option from above) and -DCOMPILER_NAME=name to override the automatic detection.
+#endif
+
+/* Code compiler-predicators (aka conditional compilation based on compiler) */
+
+#ifndef CLANG
+#define CLANG(...)
+#endif
+
+#ifndef GCC
+#define GCC(...)
+#endif
+
+#ifndef GCCLIKE
+#define GCCLIKE(...)
+#endif
+
+#ifndef TCC
+#define TCC(...)
+#endif
+
+#ifndef MSVC
+#define MSVC(...)
+#endif
+
+/* Macro trickery */
+#define EMPTY()
+
+#define DEFER( macro ) macro EMPTY()
+#define OBSTRUCT( ... ) __VA_ARGS__ DEFER( EMPTY )()
+#define EXPAND( ... ) __VA_ARGS__
+
+#define STRING_JOIN( str1, str2 ) str1##str2
+
+#define UNIQUE_NAME( name ) EXPAND(DEFER(STRING_JOIN)(name, __LINE__))
+
+/* Warnings and errors */
+
+#if COMPILER == COMPILER_GCC || COMPILER == COMPILER_CLANG
+	#define PRAGMA(pragma) _Pragma(#pragma)
+#elif COMPILER == COMPILER_MSVC
+	#define PRAGMA(pragma) __pragma(pragma)
+#endif
+
+#if COMPILER == COMPILER_GCC || COMPILER == COMPILER_CLANG
+	#define WARNING(...) PRAGMA(GCC warning __VA_ARGS__)
+	#define ERROR(...) PRAGMA(GCC error __VA_ARGS__)
+#endif
+
+#if COMPILER == COMPILER_TCC
+	#define WARN(...)
+	#define ERROR(...) (*(void*)0 = 0)
+	#warning Compiling with tcc. Pragmas are not supported, so warnings will just not be printed and errors will give an 'assignment to void' error, but the line number should still point to the right location.
+#endif
+
+#ifndef WARNING
+#define WARNING(...)
+#warning No warning directive is implemented for this compiler. No warnings will be emitted from the code.
+#endif
+
+#ifndef ERROR
+#define ERROR(...)
+#error No error directive is implemented for this compiler, thus errors can not be emitted.
+#endif
+
+/* Struct shorthand declaration */
+#define STRUCT( name, ...) \
+typedef struct _##name { \
+	__VA_ARGS__ \
+} name;
+
+/* Cross-compiler packed struct declarator */
+#if COMPILER == COMPILER_GCC || COMPILER == COMPILER_CLANG || COMPILER == COMPILER_TCC
+
+	#define PACKED_STRUCT( name, ...) \
+		typedef struct __attribute__((packed)) _##name { \
+			__VA_ARGS__ \
+		} name;
+
+#elif COMPILER == COMPILER_MSVC
+
+	#define PACKED_STRUCT( name, ...) \
+		PRAGMA(pack(push, 1)) \
+		typedef struct _##name { \
+			__VA_ARGS__ \
+		} name; \
+		PRAGMA(pack(pop))
+
+#endif
+
+#ifndef PACKED_STRUCT
+#error PACKED_STRUCT macro undefined.
+#endif
+
+/* Compile-time assertion */
+#define ASSERT( expression )                                                   \
+	typedef char UNIQUE_NAME(compile_time_assert)[(expression)? 1 : -1]
+
+#endif
platform/common/os.h
@@ -0,0 +1,31 @@
+#ifndef NOTLIBC_HEADER_PLATFORM_OS_INCLUDED
+#define NOTLIBC_HEADER_PLATFORM_OS_INCLUDED
+
+#define OS_LINUX 1
+#define OS_WINDOWS 2
+
+#ifndef OS
+	#if defined(__linux__)
+		#define OS OS_LINUX
+		#define OS_NAME "linux"
+		#define LINUX(...) __VA_ARGS__
+	#elif defined(_WIN32) || defined(_WIN64) 
+		#define OS OS_WINDOWS
+		#define OS_NAME "windows"
+		#define WINDOWS(...) __VA_ARGS__
+	#endif
+#endif
+
+#if !defined(OS) || !defined(OS_NAME)
+	#error Failed to identify operating system. Please specify it via -DOS=number (choose the most compatible option from above) and -DOS_NAME=name to override the automatic detection.
+#endif
+
+#ifndef WINDOWS
+#define WINDOWS(...)
+#endif
+
+#ifndef LINUX
+#define LINUX(...)
+#endif
+
+#endif
platform/linux/corefunc.h
@@ -0,0 +1,14 @@
+#include "../common/arch.h"
+#include "../common/compiler.h"
+
+static inline void breakpoint(void) {
+	__asm__ volatile (
+		"int $3\n"
+	);
+}
+
+static inline void terminationpoint(void) {
+	__asm__ volatile (
+		"ud2\n"
+	);
+}
platform/linux/coretypes.h
@@ -0,0 +1,28 @@
+#ifndef NOTLIBC_HEADER_PLATFORM_LINUX_TYPES_INCLUDED
+#define NOTLIBC_HEADER_PLATFORM_LINUX_TYPES_INCLUDED
+
+#include "../common/arch.h"
+
+typedef unsigned char      u8;
+typedef unsigned short     u16;
+typedef unsigned int       u32;
+typedef unsigned long long u64;
+
+typedef signed char      i8;
+typedef signed short     i16;
+typedef signed int       i32;
+typedef signed long long i64;
+
+typedef float  f32;
+typedef double f64;
+
+X86_64(
+	typedef u64 syscall_uint;
+)
+
+
+I386(
+	typedef u32 syscall_uint;
+)
+
+#endif
platform/platform.h
@@ -0,0 +1,35 @@
+#include "./common/build.h"
+#include "./common/compiler.h"
+#include "./common/arch.h"
+#include "./common/os.h"
+
+#define HEADER_PLATFORM_CORETYPES \
+	LINUX("linux/coretypes.h") \
+	WINDOWS("windows/coretypes.h")
+
+/* Basic types - with sanity checks */
+#include HEADER_PLATFORM_CORETYPES
+
+ASSERT( sizeof( u8 ) == 1 );
+ASSERT( sizeof( u16 ) == 2 );
+ASSERT( sizeof( u32 ) == 4 );
+ASSERT( sizeof( u64 ) == 8 );
+
+ASSERT( sizeof( i8 ) == 1 );
+ASSERT( sizeof( i16 ) == 2 );
+ASSERT( sizeof( i32 ) == 4 );
+ASSERT( sizeof( i64 ) == 8 );
+
+ASSERT( sizeof( f32 ) == 4 );
+ASSERT( sizeof( f64 ) == 8 );
+
+/* Core platform functions */
+
+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")     \
+	WINDOWS("windows/corefunc.h")
+
+#include HEADER_PLATFORM_COREFUNC
.gitignore
@@ -0,0 +1,4 @@
+*.out
+Session.vim
+main.c
+compile_flags.txt
README.md
@@ -0,0 +1,3 @@
+# notlibc
+
+This is not a libc. It's just a C library.