diff --git a/build_file.c b/build_file.c index 0f53b5e..27bc9ea 100644 --- a/build_file.c +++ b/build_file.c @@ -1,5 +1,5 @@ -#include "src/core/core.h" -#include "src/core/core.c" +#include "src/core/core_inc.h" +#include "src/core/core_inc.c" #define BUILD_TOOL_LIB #define S8_String s8_t @@ -16,9 +16,9 @@ int main(int argc, char **argv) { meta_app(arena); bool run_server = false; - bool core_test_target = false; + bool core_test_target = true; bool wasm_target = true; - bool win32_target = true; + bool win32_target = false; if (run_server) { os_systemf("start /D ..\\package ..\\package\\run_server.bat"); @@ -46,6 +46,8 @@ int main(int argc, char **argv) { " /link /incremental:no" ); if (ok != 0) return ok; + + os_systemf("core_test.exe"); } diff --git a/src/app/app_wasm.c b/src/app/app_wasm.c index c24b951..f05d167 100644 --- a/src/app/app_wasm.c +++ b/src/app/app_wasm.c @@ -1,11 +1,3 @@ -#define glb_wasm_export __attribute__((visibility("default"))) -#define fn_wasm_export __attribute__((visibility("default"))) -#define fn_wasm_import - -fn_wasm_import void wasm_alert(isize ptr, i32 len); -fn_wasm_import f64 wasm_parse_float(isize str, i32 len); -fn_wasm_import void wasm_write_to_console(isize str, i32 len); - fn_wasm_import void wasm_clear(); fn_wasm_import void wasm_draw_text(isize str, i32 len, f64 x, f64 y, isize font_str, i32 font_len, i32 font_size, f32 r, f32 g, f32 b, f32 a); fn_wasm_import void wasm_draw_rect(f64 x, f64 y, f64 w, f64 h, f32 r, f32 g, f32 b, f32 a); @@ -18,8 +10,6 @@ glb_wasm_export i32 wasm_temp_buff1_len = 127; glb_wasm_export char wasm_temp_buff2[128] = {[127] = 0x13}; glb_wasm_export i32 wasm_temp_buff2_len = 127; -extern char __heap_base; -glb ma_arena_t wasm_perm_arena; glb char *font_face = "fira"; glb i32 font_face_len = 4; glb f64 wasm_dpr; @@ -28,20 +18,7 @@ glb STACK(app_event_t, 64) wasm_events; glb b32 wasm_event_failed_to_queue; glb f64 wasm_last_time = 0; -fn void app_update(ma_arena_t *perm_arena, app_event_t *events, i32 event_count); - -fn void alert(s8_t string) { - wasm_alert((isize)string.str, (i32)string.len); -} - -fn void puts(const char *string) { - wasm_write_to_console((isize)string, (i32)str_len((char *)string)); -} - -fn double strtod(const char *str, char **end_unused) { - assert(end_unused == NULL); - return wasm_parse_float((isize)str, str_len((char *)str)); -} +fn void app_update(app_event_t *events, i32 event_count); fn void set_clip(r2f64_t rect) { wasm_set_clip(wasm_dpr * rect.min.x, wasm_dpr * rect.min.y, wasm_dpr * (rect.max.x - rect.min.x), wasm_dpr * (rect.max.y - rect.min.y)); @@ -200,7 +177,7 @@ fn_wasm_export void wasm_update(f64 time, f64 width, f64 height, f64 dpr) { wasm_clear(); draw_rect(r2f64(0, 0, window_size.x, window_size.y), white_color_global); - app_update(&wasm_perm_arena, wasm_events.data, wasm_events.len); + app_update(wasm_events.data, wasm_events.len); wasm_events.len = 0; wasm_last_time = time; @@ -208,24 +185,6 @@ fn_wasm_export void wasm_update(f64 time, f64 width, f64 height, f64 dpr) { } fn_wasm_export void wasm_init(void) { - { - isize page_size = kib(64); - isize page_count = __builtin_wasm_memory_size(0); - u8 *memory = (u8 *)&__heap_base; - usize memory_size = page_count * (page_size) - (isize)memory; - wasm_perm_arena.data = memory; - wasm_perm_arena.commit = wasm_perm_arena.reserve = memory_size; - - // init core - { - ma_push_arena_ex(&wasm_perm_arena, &core_desc.scratch[0], mib(1)); - ma_push_arena_ex(&wasm_perm_arena, &core_desc.scratch[1], kib(256)); - ma_push_arena_ex(&wasm_perm_arena, &core_desc.scratch[2], kib(64)); - } - - debugf("on_init, __builtin_wasm_memory_size(0) = %d(%d)", page_count, memory_size); - } - - wasm_input_text_arena = ma_push_arena(&wasm_perm_arena, kib(1)); - + core_init(); + wasm_input_text_arena = ma_push_arena(&tcx.perm, kib(1)); } \ No newline at end of file diff --git a/src/core/core.h b/src/core/core.h index e5de7ce..a4cb214 100644 --- a/src/core/core.h +++ b/src/core/core.h @@ -1,13 +1,234 @@ -#include "core_platform_defines.h" -#include -#include -#include -#include "core_defines.h" -#include "core_unicode.h" -#include "core_arena.h" -#include "core_string.h" -#include "core_lexer.h" -#include "core_math.h" -#include "core_log.h" -#include "core_type_info.h" -#include "core_intrin.h" \ No newline at end of file +typedef uintptr_t usize; +typedef uint64_t u64; +typedef uint32_t u32; +typedef uint16_t u16; +typedef uint8_t u8; + +typedef intptr_t isize; +typedef int64_t i64; +typedef int32_t i32; +typedef int16_t i16; +typedef int8_t i8; + +typedef int64_t b64; +typedef int32_t b32; +typedef int16_t b16; +typedef int8_t b8; + +typedef float f32; +typedef double f64; + +#ifndef true +#define true 1 +#endif +#ifndef false +#define false 0 +#endif + +#define fn +#define glb +#define locl + +#if PLATFORM_WASM +#define glb_wasm_export __attribute__((visibility("default"))) +#define fn_wasm_export __attribute__((visibility("default"))) +#define fn_wasm_import +#endif + +#define U64_TO_F64(x) (((union { f64 f; u64 i; }) { .i = (x) }).f) +#define U32_TO_F32(x) (((union { f32 f; u32 i; }) { .i = (x) }).f) +#define F64_TO_U64(x) (((union { f64 f; u64 i; }) { .f = (x) }).i) +#define F32_TO_U32(x) (((union { f32 f; u32 i; }) { .f = (x) }).i) + +#define MIN(x,y) ((x) > (y) ? (y) : (x)) +#define MAX(x,y) ((x) > (y) ? (x) : (y)) + +#define CLAMP_TOP(A,X) MIN(A,X) +#define CLAMP_BOT(X,B) MAX(X,B) +#define CLAMP(x,a,b) (((x)<(a))?(a):((x)>(b))?(b):(x)) + +#define set_bit(x) (1ULL << (x)) +#define lengthof(x) (sizeof((x))/sizeof((x)[0])) +#ifndef offsetof +#define offsetof(st, m) ((usize)&(((st *)0)->m)) +#endif +#define expect(x) if (!(x)) + +#define kib(x) (1024ULL * (x##ULL)) +#define mib(x) (1024ULL * kib(x)) +#define gib(x) (1024ULL * mib(x)) + +#define DEFER_LOOP(begin, end) for (i32 PASTE(_i_, __LINE__) = (begin, 0); !PASTE(_i_, __LINE__); PASTE(_i_, __LINE__) += (end, 1)) +#define STACK(type, size) struct { type data[size]; i32 len; } +#define STACK_PUSH(stack, ...) (assert((stack).len < lengthof((stack).data)), (stack).data[(stack).len++] = __VA_ARGS__) +#define STACK_POP(stack) (assert((stack).len > 0), (stack).data[--(stack).len]) +#define STACK_LAST(stack) (assert((stack).len > 0), (stack).data + ((stack).len-1)) + +#define STRINGIFY_(S) #S +#define STRINGIFY(S) STRINGIFY_(S) +#define PASTE_(a, b) a##b +#define PASTE(a, b) PASTE_(a, b) +#define SWAP(t, a, b) do { t PASTE(temp__, __LINE__) = a; a = b; b = PASTE(temp__, __LINE__); } while(0) +#define CODE(...) #__VA_ARGS__ + +#ifndef FILE_AND_LINE_GCC_FORMAT +#define FILE_AND_LINE __FILE__"("STRINGIFY(__LINE__)")" +#else +#define FILE_AND_LINE __FILE__":"STRINGIFY(__LINE__) +#endif + +#if PLATFORM_CL + #define debug__break() __debugbreak() +#else + #define debug__break() __builtin_trap() +#endif +#define debug_break() (debug__break(), 0) + +#if PLATFORM_WASM + #define THREAD_LOCAL +#elif PLATFORM_GCC | PLATFORM_CLANG + #define THREAD_LOCAL __thread +#elif PLATFORM_CL + #define THREAD_LOCAL __declspec(thread) +#else + #define THREAD_LOCAL _Thread_local +#endif + +#if PLATFORM_CL + #pragma warning(disable: 4116) +#endif + +// Single linked list Queue +#define SLLQ_APPEND_MOD(f, l, n, next) \ + do { \ + assert((n)->next == NULL); \ + if ((f) == 0) { \ + (f) = (l) = (n); \ + } else { \ + (l) = (l)->next = (n); \ + } \ + } while (0) +#define SLLQ_APPEND(f, l, n) SLLQ_APPEND_MOD(f, l, n, next) + +#define SLLQ_PREPEND_MOD(f, l, n, next) \ + do { \ + assert((n)->next == NULL); \ + if ((f) == 0) { \ + (f) = (l) = (n); \ + } else { \ + (n)->next = (f); \ + (f) = (n); \ + } \ + } while (0) +#define SLLQ_PREPEND(f, l, n) SLLQ_PREPEND_MOD(f, l, n, next) + +#define SLLQ_REMOVE_FIRST_MOD(f, l, next) \ + do { \ + if ((f) == (l)) { \ + (f) = (l) = 0; \ + } else { \ + (f) = (f)->next; \ + } \ + } while (0) +#define SLLQ_REMOVE_FIRST(f, l) SLLQ_REMOVE_FIRST_MOD(f, l, next) + +// Singly linked list stack +#define SLLS_PUSH_MOD(stack_base, new_stack_base, next) \ + do { \ + (new_stack_base)->next = (stack_base); \ + (stack_base) = (new_stack_base); \ + } while (0) +#define SLLS_PUSH(stack_base, new_stack_base) \ + SLLS_PUSH_MOD(stack_base, new_stack_base, next) + +#define SLLS_POP_AND_STORE(stack_base, out_node) \ + do { \ + if (stack_base) { \ + (out_node) = (stack_base); \ + (stack_base) = (stack_base)->next; \ + (out_node)->next = 0; \ + } \ + } while (0) + +// Doubly linked list Queue +#define DLLQ_APPEND_MOD(f, l, node, next, prev) \ + do { \ + assert((node)->next == NULL); \ + assert((node)->prev == NULL); \ + if ((f) == 0) { \ + (f) = (l) = (node); \ + } else { \ + (l)->next = (node); \ + (node)->prev = (l); \ + (l) = (node); \ + } \ + } while (0) +#define DLLQ_APPEND(f, l, node) DLLQ_APPEND_MOD(f, l, node, next, prev) + +#define DLLQ_PREPEND_MOD(f, l, node, next, prev) \ + do { \ + assert((node)->next == NULL); \ + assert((node)->prev == NULL); \ + if ((f) == 0) { \ + (f) = (l) = (node); \ + } else { \ + (node)->next = (f); \ + (f)->prev = (node); \ + (f) = (node); \ + } \ + } while (0) +#define DLLQ_PREPEND(f, l, node) DLLQ_PREPEND_MOD(f, l, node, next, prev) + +#define DLLQ_CONTAINS(f, l, n, next, prev) for ( + +#define DLLQ_REMOVE_MOD(first, last, node, next, prev) \ + do { \ + if ((first) == (last)) { \ + assert((node) == (first)); \ + (first) = (last) = 0; \ + } else if ((last) == (node)) { \ + (last) = (last)->prev; \ + (last)->next = 0; \ + } else if ((first) == (node)) { \ + (first) = (first)->next; \ + (first)->prev = 0; \ + } else { \ + (node)->prev->next = (node)->next; \ + (node)->next->prev = (node)->prev; \ + } \ + if (node) { \ + (node)->prev = 0; \ + (node)->next = 0; \ + } \ + } while (0) +#define DLLQ_REMOVE(first, last, node) DLLQ_REMOVE_MOD(first, last, node, next, prev) + +// Doubly linked list Stack +#define DLLS_ADD_MOD(first, node, next, prev) \ + do { \ + assert((node)->next == NULL); \ + assert((node)->prev == NULL); \ + (node)->next = (first); \ + if ((first)) \ + (first)->prev = (node); \ + (first) = (node); \ + } while (0) +#define DLLS_ADD(first, node) DLLS_ADD_MOD(first, node, next, prev) +#define DLLS_REMOVE_MOD(first, node, next, prev) \ + do { \ + if ((node) == (first)) { \ + (first) = (first)->next; \ + if ((first)) \ + (first)->prev = 0; \ + } else { \ + (node)->prev->next = (node)->next; \ + if ((node)->next) \ + (node)->next->prev = (node)->prev; \ + } \ + if (node) { \ + (node)->prev = 0; \ + (node)->next = 0; \ + } \ + } while (0) +#define DLLS_REMOVE(first, node) DLLS_REMOVE_MOD(first, node, next, prev) + diff --git a/src/core/core_arena.c b/src/core/core_arena.c index a230244..114b94b 100644 --- a/src/core/core_arena.c +++ b/src/core/core_arena.c @@ -140,8 +140,8 @@ fn void ma_set0(ma_arena_t *arena) { ma_set_len(arena, 0); } // Scratch arena fn ma_temp_t ma_begin_scratch_ex(ma_arena_t **conflicts, int conflict_count) { ma_arena_t *unoccupied = 0; - for (int i = 0; i < lengthof(core_desc.scratch); i += 1) { - ma_arena_t *from_pool = core_desc.scratch + i; + for (int i = 0; i < lengthof(tcx.scratch); i += 1) { + ma_arena_t *from_pool = tcx.scratch + i; unoccupied = from_pool; for (int conflict_i = 0; conflict_i < conflict_count; conflict_i += 1) { ma_arena_t *from_conflict = conflicts[conflict_i]; @@ -162,7 +162,7 @@ fn ma_temp_t ma_begin_scratch_ex(ma_arena_t **conflicts, int conflict_count) { } fn ma_temp_t ma_begin_scratch(void) { - ma_temp_t result = ma_begin_temp(core_desc.scratch + 0); + ma_temp_t result = ma_begin_temp(tcx.scratch + 0); return result; } diff --git a/src/core/core_ctx.h b/src/core/core_ctx.h new file mode 100644 index 0000000..266562c --- /dev/null +++ b/src/core/core_ctx.h @@ -0,0 +1,14 @@ +typedef struct thread_ctx_t thread_ctx_t; +struct thread_ctx_t { + ma_arena_t scratch[3]; + ma_arena_t perm; + logger_t log; +}; + +THREAD_LOCAL thread_ctx_t tcx = { + .log = { + .break_on_fatal = true, + .log_proc = default_log_proc, + .flags = log_flag_file_path, + } +}; diff --git a/src/core/core_defines.h b/src/core/core_defines.h deleted file mode 100644 index 82ef22f..0000000 --- a/src/core/core_defines.h +++ /dev/null @@ -1,228 +0,0 @@ -typedef uintptr_t usize; -typedef uint64_t u64; -typedef uint32_t u32; -typedef uint16_t u16; -typedef uint8_t u8; - -typedef intptr_t isize; -typedef int64_t i64; -typedef int32_t i32; -typedef int16_t i16; -typedef int8_t i8; - -typedef int64_t b64; -typedef int32_t b32; -typedef int16_t b16; -typedef int8_t b8; - -typedef float f32; -typedef double f64; - -#ifndef true -#define true 1 -#endif -#ifndef false -#define false 0 -#endif - -#define fn -#define glb -#define locl - -#define U64_TO_F64(x) (((union { f64 f; u64 i; }) { .i = (x) }).f) -#define U32_TO_F32(x) (((union { f32 f; u32 i; }) { .i = (x) }).f) -#define F64_TO_U64(x) (((union { f64 f; u64 i; }) { .f = (x) }).i) -#define F32_TO_U32(x) (((union { f32 f; u32 i; }) { .f = (x) }).i) - -#define MIN(x,y) ((x) > (y) ? (y) : (x)) -#define MAX(x,y) ((x) > (y) ? (x) : (y)) - -#define CLAMP_TOP(A,X) MIN(A,X) -#define CLAMP_BOT(X,B) MAX(X,B) -#define CLAMP(x,a,b) (((x)<(a))?(a):((x)>(b))?(b):(x)) - -#define set_bit(x) (1ULL << (x)) -#define lengthof(x) (sizeof((x))/sizeof((x)[0])) -#ifndef offsetof -#define offsetof(st, m) ((usize)&(((st *)0)->m)) -#endif -#define expect(x) if (!(x)) - -#define kib(x) (1024ULL * (x##ULL)) -#define mib(x) (1024ULL * kib(x)) -#define gib(x) (1024ULL * mib(x)) - -#define DEFER_LOOP(begin, end) for (i32 PASTE(_i_, __LINE__) = (begin, 0); !PASTE(_i_, __LINE__); PASTE(_i_, __LINE__) += (end, 1)) -#define STACK(type, size) struct { type data[size]; i32 len; } -#define STACK_PUSH(stack, ...) (assert((stack).len < lengthof((stack).data)), (stack).data[(stack).len++] = __VA_ARGS__) -#define STACK_POP(stack) (assert((stack).len > 0), (stack).data[--(stack).len]) -#define STACK_LAST(stack) (assert((stack).len > 0), (stack).data + ((stack).len-1)) - -#define STRINGIFY_(S) #S -#define STRINGIFY(S) STRINGIFY_(S) -#define PASTE_(a, b) a##b -#define PASTE(a, b) PASTE_(a, b) -#define SWAP(t, a, b) do { t PASTE(temp__, __LINE__) = a; a = b; b = PASTE(temp__, __LINE__); } while(0) -#define CODE(...) #__VA_ARGS__ - -#ifndef FILE_AND_LINE_GCC_FORMAT -#define FILE_AND_LINE __FILE__"("STRINGIFY(__LINE__)")" -#else -#define FILE_AND_LINE __FILE__":"STRINGIFY(__LINE__) -#endif - -#if PLATFORM_WASM - #define THREAD_LOCAL -#elif PLATFORM_GCC | PLATFORM_CLANG - #define THREAD_LOCAL __thread -#elif PLATFORM_CL - #define THREAD_LOCAL __declspec(thread) -#else - #define THREAD_LOCAL _Thread_local -#endif - -typedef struct date_t date_t; -struct date_t { - u16 ms; - u16 sec; - u16 min; - u16 hour; - u16 day; - u16 month; - u16 year; -}; - -// Single linked list Queue -#define SLLQ_APPEND_MOD(f, l, n, next) \ - do { \ - assert((n)->next == NULL); \ - if ((f) == 0) { \ - (f) = (l) = (n); \ - } else { \ - (l) = (l)->next = (n); \ - } \ - } while (0) -#define SLLQ_APPEND(f, l, n) SLLQ_APPEND_MOD(f, l, n, next) - -#define SLLQ_PREPEND_MOD(f, l, n, next) \ - do { \ - assert((n)->next == NULL); \ - if ((f) == 0) { \ - (f) = (l) = (n); \ - } else { \ - (n)->next = (f); \ - (f) = (n); \ - } \ - } while (0) -#define SLLQ_PREPEND(f, l, n) SLLQ_PREPEND_MOD(f, l, n, next) - -#define SLLQ_REMOVE_FIRST_MOD(f, l, next) \ - do { \ - if ((f) == (l)) { \ - (f) = (l) = 0; \ - } else { \ - (f) = (f)->next; \ - } \ - } while (0) -#define SLLQ_REMOVE_FIRST(f, l) SLLQ_REMOVE_FIRST_MOD(f, l, next) - -// Singly linked list stack -#define SLLS_PUSH_MOD(stack_base, new_stack_base, next) \ - do { \ - (new_stack_base)->next = (stack_base); \ - (stack_base) = (new_stack_base); \ - } while (0) -#define SLLS_PUSH(stack_base, new_stack_base) \ - SLLS_PUSH_MOD(stack_base, new_stack_base, next) - -#define SLLS_POP_AND_STORE(stack_base, out_node) \ - do { \ - if (stack_base) { \ - (out_node) = (stack_base); \ - (stack_base) = (stack_base)->next; \ - (out_node)->next = 0; \ - } \ - } while (0) - -// Doubly linked list Queue -#define DLLQ_APPEND_MOD(f, l, node, next, prev) \ - do { \ - assert((node)->next == NULL); \ - assert((node)->prev == NULL); \ - if ((f) == 0) { \ - (f) = (l) = (node); \ - } else { \ - (l)->next = (node); \ - (node)->prev = (l); \ - (l) = (node); \ - } \ - } while (0) -#define DLLQ_APPEND(f, l, node) DLLQ_APPEND_MOD(f, l, node, next, prev) - -#define DLLQ_PREPEND_MOD(f, l, node, next, prev) \ - do { \ - assert((node)->next == NULL); \ - assert((node)->prev == NULL); \ - if ((f) == 0) { \ - (f) = (l) = (node); \ - } else { \ - (node)->next = (f); \ - (f)->prev = (node); \ - (f) = (node); \ - } \ - } while (0) -#define DLLQ_PREPEND(f, l, node) DLLQ_PREPEND_MOD(f, l, node, next, prev) - -#define DLLQ_CONTAINS(f, l, n, next, prev) for ( - -#define DLLQ_REMOVE_MOD(first, last, node, next, prev) \ - do { \ - if ((first) == (last)) { \ - assert((node) == (first)); \ - (first) = (last) = 0; \ - } else if ((last) == (node)) { \ - (last) = (last)->prev; \ - (last)->next = 0; \ - } else if ((first) == (node)) { \ - (first) = (first)->next; \ - (first)->prev = 0; \ - } else { \ - (node)->prev->next = (node)->next; \ - (node)->next->prev = (node)->prev; \ - } \ - if (node) { \ - (node)->prev = 0; \ - (node)->next = 0; \ - } \ - } while (0) -#define DLLQ_REMOVE(first, last, node) DLLQ_REMOVE_MOD(first, last, node, next, prev) - -// Doubly linked list Stack -#define DLLS_ADD_MOD(first, node, next, prev) \ - do { \ - assert((node)->next == NULL); \ - assert((node)->prev == NULL); \ - (node)->next = (first); \ - if ((first)) \ - (first)->prev = (node); \ - (first) = (node); \ - } while (0) -#define DLLS_ADD(first, node) DLLS_ADD_MOD(first, node, next, prev) -#define DLLS_REMOVE_MOD(first, node, next, prev) \ - do { \ - if ((node) == (first)) { \ - (first) = (first)->next; \ - if ((first)) \ - (first)->prev = 0; \ - } else { \ - (node)->prev->next = (node)->next; \ - if ((node)->next) \ - (node)->next->prev = (node)->prev; \ - } \ - if (node) { \ - (node)->prev = 0; \ - (node)->next = 0; \ - } \ - } while (0) -#define DLLS_REMOVE(first, node) DLLS_REMOVE_MOD(first, node, next, prev) - diff --git a/src/core/core.c b/src/core/core_inc.c similarity index 61% rename from src/core/core.c rename to src/core/core_inc.c index c742fec..5f92bcc 100644 --- a/src/core/core.c +++ b/src/core/core_inc.c @@ -1,10 +1,25 @@ -#if PLATFORM_WINDOWS -#define NOMINMAX -#define WIN32_LEAN_AND_MEAN -#include +#if PLATFORM_WASM + #include "core_platform_wasm.c" +#elif PLATFORM_WINDOWS + #pragma comment(lib, "user32.lib") + #define NOMINMAX + #define WIN32_LEAN_AND_MEAN + #include + #include + #include + #include + #include + #include "core_platform_win32.c" +#else + #include + #include + #include + #include + #include + #include + #include "core_platform_unix.c" #endif - #if PLATFORM_ADDRESS_SANITIZER #include #endif @@ -17,20 +32,6 @@ #define MA_ASAN_UNPOISON_MEMORY_REGION(addr, size) ASAN_UNPOISON_MEMORY_REGION(addr, size) #endif -#if PLATFORM_WASM -fn double strtod(const char *str, char **end_unused); -fn void puts(const char *str); -fn void alert(s8_t string); -#endif - -#if PLATFORM_CL -#include -#include -#include -#include -#include -#endif - #include "core_intrin.c" #include "core_unicode.c" #include "core_math.c" diff --git a/src/core/core_inc.h b/src/core/core_inc.h new file mode 100644 index 0000000..eabc8b8 --- /dev/null +++ b/src/core/core_inc.h @@ -0,0 +1,15 @@ +#include "core_platform_defines.h" +#include +#include +#include +#include "core.h" +#include "core_unicode.h" +#include "core_arena.h" +#include "core_string.h" +#include "core_lexer.h" +#include "core_math.h" +#include "core_log.h" +#include "core_type_info.h" +#include "core_intrin.h" +#include "core_platform.h" +#include "core_ctx.h" \ No newline at end of file diff --git a/src/core/core_intrin.c b/src/core/core_intrin.c index c59cbdf..e9fde08 100644 --- a/src/core/core_intrin.c +++ b/src/core/core_intrin.c @@ -55,60 +55,4 @@ fn f64 f64_mod(f64 a, f64 b) { return fmod(a, b); } fn f32 f32_mod(f32 a, f32 b) { return fmodf(a, b); } #endif -#if PLATFORM_WINDOWS -fn void *vmem_reserve(usize size) { - void *result = (uint8_t *)VirtualAlloc(0, size, MEM_RESERVE, PAGE_READWRITE); - return result; -} -fn b32 vmem_commit(void *p, usize size) { - void *result = VirtualAlloc(p, size, MEM_COMMIT, PAGE_READWRITE); - return result ? true : false; -} -fn b32 vmem_release(void *p) { - BOOL result = VirtualFree(p, 0, MEM_RELEASE); - return result ? true : false; -} -fn b32 vmem_decommit(void *p, usize size) { - BOOL result = VirtualFree(p, size, MEM_DECOMMIT); - return result ? true : false; -} -#else -void *vmem_reserve(usize size) { return NULL; } -b32 vmem_commit(void *p, usize size) { return false; } -b32 vmem_release(void *p) { return true; } -b32 vmem_decommit(void *p, usize size) { return true; } -#endif -fn f64 f64_from_s8(s8_t string) { - ma_temp_t scratch = ma_begin_scratch(); - s8_t num_string = s8_copy(scratch.arena, string); - f64 result = strtod(num_string.str, NULL); - ma_end_scratch(scratch); - return result; -} - -fn date_t date_now(void) { - date_t result = {0}; -#if PLATFORM_WASM -#elif PLATFORM_WINDOWS - SYSTEMTIME lt; - GetLocalTime(<); - result.ms = lt.wMilliseconds; - result.sec = lt.wSecond; - result.min = lt.wMinute; - result.hour = lt.wHour; - result.day = lt.wDay; - result.month = lt.wMonth; - result.year = lt.wYear; -#else - time_t time = time(NULL); - struct tm *lt = localtime(&time); - result.sec = lt->tm_sec; - result.min = lt->tm_min; - result.hour = lt->tm_hour; - result.day = lt->tm_mday; - result.month = lt->tm_month; - result.year = lt->tm_year; -#endif - return result; -} diff --git a/src/core/core_intrin.h b/src/core/core_intrin.h index f6be258..12483d2 100644 --- a/src/core/core_intrin.h +++ b/src/core/core_intrin.h @@ -1,21 +1,8 @@ -typedef struct core_desc_t core_desc_t; -struct core_desc_t { - ma_arena_t scratch[3]; - - logger_t log; -}; - fn void memory_copy(void *dst, void *src, usize n); fn void memory_move(void *dest, const void *src, usize n); fn void memory_set(void *dst, i32 c, usize size); fn void memory_zero(void *dst, usize size); -fn void *vmem_reserve(usize size); -fn b32 vmem_commit(void *p, usize size); -fn b32 vmem_release(void *p); -fn b32 vmem_decommit(void *p, usize size); - -fn f64 f64_from_s8(s8_t string); fn f32 f32_sqrt(f32 x); fn f64 f64_sqrt(f64 x); fn f32 f32_ceil(f32 x); @@ -27,14 +14,4 @@ fn f64 f64_abs(f64 x); fn f32 f32_round(f32 x); fn f64 f64_round(f64 x); fn f64 f64_mod(f64 a, f64 b); -fn f32 f32_mod(f32 a, f32 b); - -fn void default_log_proc(log_event_t ev); - -THREAD_LOCAL core_desc_t core_desc = { - .log = { - .break_on_fatal = true, - .log_proc = default_log_proc, - .flags = 0, - } -}; +fn f32 f32_mod(f32 a, f32 b); \ No newline at end of file diff --git a/src/core/core_log.c b/src/core/core_log.c index 9590f99..79ae358 100644 --- a/src/core/core_log.c +++ b/src/core/core_log.c @@ -10,7 +10,7 @@ fn char *log_level_str(log_level_t level) { } fn void log_base(i32 module, log_level_t level, s8_t file_and_line, s8_t string) { - core_desc.log.log_proc((log_event_t){module, level, file_and_line, string}); + tcx.log.log_proc((log_event_t){module, level, file_and_line, string}); } fn void log_basef(i32 module, log_level_t level, s8_t file_and_line, const char *str, ...) { @@ -20,30 +20,19 @@ fn void log_basef(i32 module, log_level_t level, s8_t file_and_line, const char ma_end_scratch(scratch); } -fn void core_error_message(char *str) { -#if PLATFORM_WINDOWS - MessageBoxA(NULL, str, "fatal error", MB_OK); - fprintf(stderr, "%s", str); -#elif PLATFORM_WASM - alert(s8_from_char(str)); -#else - fprintf(stderr, "%s", str); -#endif -} - fn void default_log_proc(log_event_t ev) { ma_temp_t scratch = ma_begin_scratch(); sb8_t *sb = sb8_serial_begin(scratch.arena); - if (core_desc.log.flags & log_flag_module_id) { + if (tcx.log.flags & log_flag_module_id) { sb8_printf(sb, "[%d] ", ev.module); } - if (core_desc.log.flags & log_flag_level) { + if (tcx.log.flags & log_flag_level) { sb8_printf(sb, "%-5s ", log_level_str(ev.level)); } date_t date = {0}; { - b32 bdate = (core_desc.log.flags & log_flag_date); - b32 btime = (core_desc.log.flags & log_flag_time); + b32 bdate = (tcx.log.flags & log_flag_date); + b32 btime = (tcx.log.flags & log_flag_time); if (bdate || btime) { date = date_now(); } @@ -54,22 +43,21 @@ fn void default_log_proc(log_event_t ev) { sb8_printf(sb, "%02u:%02u:%02u ", date.hour, date.min, date.sec); } } - if (core_desc.log.flags & log_flag_file_path) { - sb8_printf(sb, "%-40S ", ev.file_and_line); + if (tcx.log.flags & log_flag_file_path) { + sb8_printf(sb, "%S: ", ev.file_and_line); } - sb8_printf(sb, " %S\n", ev.string); + sb8_printf(sb, "%S\n", ev.string); s8_t result = sb8_serial_end(sb); if (ev.level != log_level_fatal) { - IF_PLATFORM_WINDOWS(OutputDebugStringA(result.str);) - puts(result.str); + print_console_message(result.str); } else { - core_error_message(result.str); + print_error_message(result.str); #if PLATFORM_WASM debug_break(); #else - if (core_desc.log.break_on_fatal) { + if (tcx.log.break_on_fatal) { debug_break(); } else { exit(1); diff --git a/src/core/core_log.h b/src/core/core_log.h index a73e224..886a8dd 100644 --- a/src/core/core_log.h +++ b/src/core/core_log.h @@ -58,10 +58,12 @@ struct logger_t { #define errorf(MODULE, ...) log_basef(MODULE, log_level_error, S8_FILE_AND_LINE, __VA_ARGS__) #define fatalf(...) (log_basef(module_null, log_level_fatal, S8_FILE_AND_LINE, __VA_ARGS__), 0) -fn void core_error_message(char *str); +fn void default_log_proc(log_event_t ev); + + #define program_version "---" #if PLATFORM_DEBUG_ASSERT -#define assert(x) (!(x) && (core_error_message(FILE_AND_LINE ": internal program error! assertion failed, program version: " program_version), debug_break())) +#define assert(x) (!(x) && (print_error_message(FILE_AND_LINE ": internal program error! assertion failed, program version: " program_version "\n"), debug_break())) #else #define assert(x) (void)(x) #endif diff --git a/src/core/core_platform.h b/src/core/core_platform.h new file mode 100644 index 0000000..66a6ea4 --- /dev/null +++ b/src/core/core_platform.h @@ -0,0 +1,21 @@ +typedef struct date_t date_t; +struct date_t { + u16 ms; + u16 sec; + u16 min; + u16 hour; + u16 day; + u16 month; + u16 year; +}; + +fn void print_error_message(char *str); +fn void print_console_message(char *str); +fn date_t date_now(void); + +fn void *vmem_reserve(usize size); +fn b32 vmem_commit(void *p, usize size); +fn b32 vmem_release(void *p); +fn b32 vmem_decommit(void *p, usize size); + +fn void core_init(void); // required in wasm \ No newline at end of file diff --git a/src/core/core_platform_defines.h b/src/core/core_platform_defines.h index fa41dca..8236579 100644 --- a/src/core/core_platform_defines.h +++ b/src/core/core_platform_defines.h @@ -76,16 +76,6 @@ #define SWITCH_PLATFORM_WINDOWS_WASM_ELSE(WINDOWS, WASM, ELSE) ELSE #endif -#if PLATFORM_CL -#pragma warning(disable: 4116) -#endif - -#if PLATFORM_CL -#define debug__break() __debugbreak() -#else -#define debug__break() __builtin_trap() -#endif -#define debug_break() (debug__break(), 0) #if PLATFORM_WINDOWS #define IF_PLATFORM_WINDOWS(x) x diff --git a/src/core/core_platform_unix.c b/src/core/core_platform_unix.c new file mode 100644 index 0000000..df1f51f --- /dev/null +++ b/src/core/core_platform_unix.c @@ -0,0 +1,44 @@ +fn void *vmem_reserve(usize size) { + void *result = mmap(0, size, PROT_NONE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0); + return result == MAP_FAILED ? NULL : result; +} + +fn b32 vmem_commit(void *p, usize size) { + mprotect(p, size, PROT_READ|PROT_WRITE); + return true; +} + +fn b32 vmem_release(void *p) { + void *result = munmap(ptr, size); + return result == MAP_FAILED ? false : true; +} + +fn b32 vmem_decommit(void *p, usize size) { + madvise(ptr, size, MADV_DONTNEED); + mprotect(ptr, size, PROT_NONE); + return true; +} + +fn date_t date_now(void) { + date_t result = {0}; + time_t time = time(NULL); + struct tm *lt = localtime(&time); + result.sec = lt->tm_sec; + result.min = lt->tm_min; + result.hour = lt->tm_hour; + result.day = lt->tm_mday; + result.month = lt->tm_month; + result.year = lt->tm_year; + return result; +} + +fn void print_error_message(char *str) { + fprintf(stderr, "%s", str); +} + +fn void print_console_message(char *str) { + puts(str); +} + +fn void core_init(void) { +} \ No newline at end of file diff --git a/src/core/core_platform_wasm.c b/src/core/core_platform_wasm.c new file mode 100644 index 0000000..a60e89a --- /dev/null +++ b/src/core/core_platform_wasm.c @@ -0,0 +1,52 @@ +fn_wasm_import void wasm_alert(isize ptr, i32 len); +fn_wasm_import f64 wasm_parse_float(isize str, i32 len); +fn_wasm_import void wasm_write_to_console(isize str, i32 len); + +fn void *vmem_reserve(usize size) { + return NULL; +} + +fn b32 vmem_commit(void *p, usize size) { + return false; +} + +fn b32 vmem_release(void *p) { + return true; +} + +fn b32 vmem_decommit(void *p, usize size) { + return true; +} + +fn date_t date_now(void) { + date_t result = {0}; // don't need timed logs in browser + return result; +} + +fn void print_error_message(char *str) { + wasm_alert((isize)str, str_len(str)); +} + +fn void print_console_message(char *str) { + wasm_write_to_console((isize)str, str_len(str)); +} + +fn double strtod(const char *str, char **end_unused) { + assert(end_unused == NULL); + return wasm_parse_float((isize)str, str_len((char *)str)); +} + +extern char __heap_base; + +fn void core_init(void) { + isize page_size = kib(64); + isize page_count = __builtin_wasm_memory_size(0); + u8 *memory = (u8 *)&__heap_base; + usize memory_size = page_count * (page_size) - (isize)memory; + tcx.perm.data = memory; + tcx.perm.commit = tcx.perm.reserve = memory_size; + + ma_push_arena_ex(&tcx.perm, &tcx.scratch[0], mib(1)); + ma_push_arena_ex(&tcx.perm, &tcx.scratch[1], kib(256)); + ma_push_arena_ex(&tcx.perm, &tcx.scratch[2], kib(64)); +} \ No newline at end of file diff --git a/src/core/core_platform_win32.c b/src/core/core_platform_win32.c new file mode 100644 index 0000000..de765c5 --- /dev/null +++ b/src/core/core_platform_win32.c @@ -0,0 +1,46 @@ +fn void *vmem_reserve(usize size) { + void *result = (uint8_t *)VirtualAlloc(0, size, MEM_RESERVE, PAGE_READWRITE); + return result; +} + +fn b32 vmem_commit(void *p, usize size) { + void *result = VirtualAlloc(p, size, MEM_COMMIT, PAGE_READWRITE); + return result ? true : false; +} + +fn b32 vmem_release(void *p) { + BOOL result = VirtualFree(p, 0, MEM_RELEASE); + return result ? true : false; +} + +fn b32 vmem_decommit(void *p, usize size) { + BOOL result = VirtualFree(p, size, MEM_DECOMMIT); + return result ? true : false; +} + +fn date_t date_now(void) { + date_t result = {0}; + SYSTEMTIME lt; + GetLocalTime(<); + result.ms = lt.wMilliseconds; + result.sec = lt.wSecond; + result.min = lt.wMinute; + result.hour = lt.wHour; + result.day = lt.wDay; + result.month = lt.wMonth; + result.year = lt.wYear; + return result; +} + +fn void print_error_message(char *str) { + MessageBoxA(NULL, str, "fatal error", MB_OK); + fprintf(stderr, "%s", str); +} + +fn void print_console_message(char *str) { + OutputDebugStringA(str); + puts(str); +} + +fn void core_init(void) { +} \ No newline at end of file diff --git a/src/core/core_string.c b/src/core/core_string.c index fd3ca64..19bf75c 100644 --- a/src/core/core_string.c +++ b/src/core/core_string.c @@ -479,3 +479,11 @@ fn u64 u64_from_s8(s8_t s, u64 base) { } return acc; } + +fn f64 f64_from_s8(s8_t string) { + ma_temp_t scratch = ma_begin_scratch(); + s8_t num_string = s8_copy(scratch.arena, string); + f64 result = strtod(num_string.str, NULL); + ma_end_scratch(scratch); + return result; +} \ No newline at end of file diff --git a/src/core/core_string.h b/src/core/core_string.h index 274cf15..a549fbe 100644 --- a/src/core/core_string.h +++ b/src/core/core_string.h @@ -63,6 +63,10 @@ fn b32 char_is_ident(char a); fn b32 char_is_digit(char a); fn b32 char_is_alphanumeric(char a); +fn u64 u64_from_hexchar(char c); +fn u64 u64_from_s8(s8_t s, u64 base); +fn f64 f64_from_s8(s8_t string); + // // string8 constructors #define s8(str,len) (s8_t){str, len} @@ -131,9 +135,6 @@ fn int64_t sb8_char_size(sb8_t *sb); // // other #define s8_fmtspec(string) (int)(string).len, (string).str -fn u64 u64_from_hexchar(char c); -fn u64 u64_from_s8(s8_t s, u64 base); - #define S8_CODE(...) s8_lit(#__VA_ARGS__) #define S8_FILE s8_lit(__FILE__) #define S8_FILE_AND_LINE s8_lit(FILE_AND_LINE) diff --git a/src/core_test/core_test_entry.c b/src/core_test/core_test_entry.c index 6c52132..52a17a2 100644 --- a/src/core_test/core_test_entry.c +++ b/src/core_test/core_test_entry.c @@ -1,5 +1,5 @@ -#include "core/core.h" -#include "core/core.c" +#include "core/core_inc.h" +#include "core/core_inc.c" void test_s8(void) { ma_arena_t *arena = ma_create(ma_default_reserve_size); diff --git a/src/wasm_app/main.c b/src/wasm_app/main.c index b639875..12d9844 100644 --- a/src/wasm_app/main.c +++ b/src/wasm_app/main.c @@ -1,12 +1,12 @@ -#include "core/core.h" -#include "core/core.c" +#include "core/core_inc.h" +#include "core/core_inc.c" #include "app/app.h" #include "app/app.c" // #include "debug.c" // #include "ui2.c" f64 delta_time; -void app_update(ma_arena_t *perm_arena, app_event_t *events, i32 event_count) { +void app_update(app_event_t *events, i32 event_count) { if (event_count) delta_time = events[0].delta_time; draw_textf(v2f64(0,0), "delta time: %f", delta_time);