From 9045c9ed0c91563bf1dc806e3da8ed0d123f4c12 Mon Sep 17 00:00:00 2001 From: olesya-wo <29059032+olesya-wo@users.noreply.github.com> Date: Tue, 12 Mar 2024 13:53:31 +0300 Subject: [PATCH 01/53] Improved statistics for core/mem/Tracking_Allocator --- core/mem/tracking_allocator.odin | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/core/mem/tracking_allocator.odin b/core/mem/tracking_allocator.odin index d6d189731..bdf6aa5e2 100644 --- a/core/mem/tracking_allocator.odin +++ b/core/mem/tracking_allocator.odin @@ -22,6 +22,12 @@ Tracking_Allocator :: struct { bad_free_array: [dynamic]Tracking_Allocator_Bad_Free_Entry, mutex: sync.Mutex, clear_on_free_all: bool, + allocated: int, + alloc_count: int, + freed: int, + free_count: int, + peak: int, + current: int, } tracking_allocator_init :: proc(t: ^Tracking_Allocator, backing_allocator: Allocator, internals_allocator := context.allocator) { @@ -44,6 +50,7 @@ tracking_allocator_clear :: proc(t: ^Tracking_Allocator) { sync.mutex_lock(&t.mutex) clear(&t.allocation_map) clear(&t.bad_free_array) + t.current = 0 sync.mutex_unlock(&t.mutex) } @@ -56,6 +63,19 @@ tracking_allocator :: proc(data: ^Tracking_Allocator) -> Allocator { } } +track_alloc :: proc(data: ^Tracking_Allocator, entry: ^Tracking_Allocator_Entry) { + data.allocated += entry.size + data.alloc_count += 1 + data.current += entry.size + if data.current > data.peak do data.peak = data.current +} + +track_free :: proc(data: ^Tracking_Allocator, entry: ^Tracking_Allocator_Entry) { + data.freed += entry.size + data.free_count += 1 + data.current -= entry.size +} + tracking_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode, size, alignment: int, old_memory: rawptr, old_size: int, loc := #caller_location) -> (result: []byte, err: Allocator_Error) { @@ -100,13 +120,21 @@ tracking_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode, err = err, location = loc, } + track_alloc(data, &data.allocation_map[result_ptr]) case .Free: + if old_memory != nil && old_memory in data.allocation_map { + track_free(data, &data.allocation_map[old_memory]) + } delete_key(&data.allocation_map, old_memory) case .Free_All: if data.clear_on_free_all { clear_map(&data.allocation_map) + data.current = 0 } case .Resize, .Resize_Non_Zeroed: + if old_memory != nil && old_memory in data.allocation_map { + track_free(data, &data.allocation_map[old_memory]) + } if old_memory != result_ptr { delete_key(&data.allocation_map, old_memory) } @@ -118,6 +146,7 @@ tracking_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode, err = err, location = loc, } + track_alloc(data, &data.allocation_map[result_ptr]) case .Query_Features: set := (^Allocator_Mode_Set)(old_memory) From d979129a50ae4b22ffb1196686d2cef760ce5141 Mon Sep 17 00:00:00 2001 From: olesya-wo <29059032+olesya-wo@users.noreply.github.com> Date: Tue, 12 Mar 2024 16:32:17 +0300 Subject: [PATCH 02/53] Naming and type changes --- core/mem/tracking_allocator.odin | 45 +++++++++++++++++--------------- 1 file changed, 24 insertions(+), 21 deletions(-) diff --git a/core/mem/tracking_allocator.odin b/core/mem/tracking_allocator.odin index bdf6aa5e2..9ed49a41b 100644 --- a/core/mem/tracking_allocator.odin +++ b/core/mem/tracking_allocator.odin @@ -22,12 +22,13 @@ Tracking_Allocator :: struct { bad_free_array: [dynamic]Tracking_Allocator_Bad_Free_Entry, mutex: sync.Mutex, clear_on_free_all: bool, - allocated: int, - alloc_count: int, - freed: int, - free_count: int, - peak: int, - current: int, + + total_memory_allocated: i64, + total_allocation_count: i64, + total_memory_freed: i64, + total_free_count: i64, + peak_memory_allocated: i64, + current_memory_allocated: i64, } tracking_allocator_init :: proc(t: ^Tracking_Allocator, backing_allocator: Allocator, internals_allocator := context.allocator) { @@ -50,7 +51,7 @@ tracking_allocator_clear :: proc(t: ^Tracking_Allocator) { sync.mutex_lock(&t.mutex) clear(&t.allocation_map) clear(&t.bad_free_array) - t.current = 0 + t.current_memory_allocated = 0 sync.mutex_unlock(&t.mutex) } @@ -63,22 +64,24 @@ tracking_allocator :: proc(data: ^Tracking_Allocator) -> Allocator { } } -track_alloc :: proc(data: ^Tracking_Allocator, entry: ^Tracking_Allocator_Entry) { - data.allocated += entry.size - data.alloc_count += 1 - data.current += entry.size - if data.current > data.peak do data.peak = data.current -} - -track_free :: proc(data: ^Tracking_Allocator, entry: ^Tracking_Allocator_Entry) { - data.freed += entry.size - data.free_count += 1 - data.current -= entry.size -} - tracking_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode, size, alignment: int, old_memory: rawptr, old_size: int, loc := #caller_location) -> (result: []byte, err: Allocator_Error) { + track_alloc :: proc(data: ^Tracking_Allocator, entry: ^Tracking_Allocator_Entry) { + data.total_memory_allocated += entry.size + data.total_allocation_count += 1 + data.current_memory_allocated += entry.size + if data.current_memory_allocated > data.peak_memory_allocated { + data.peak_memory_allocated = data.current_memory_allocated + } + } + + track_free :: proc(data: ^Tracking_Allocator, entry: ^Tracking_Allocator_Entry) { + data.total_memory_freed += entry.size + data.total_free_count += 1 + data.current_memory_allocated -= entry.size + } + data := (^Tracking_Allocator)(allocator_data) sync.mutex_guard(&data.mutex) @@ -129,7 +132,7 @@ tracking_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode, case .Free_All: if data.clear_on_free_all { clear_map(&data.allocation_map) - data.current = 0 + data.current_memory_allocated = 0 } case .Resize, .Resize_Non_Zeroed: if old_memory != nil && old_memory in data.allocation_map { From 51a4d97f03980838139b4883b1d62debe55105e8 Mon Sep 17 00:00:00 2001 From: olesya-wo <29059032+olesya-wo@users.noreply.github.com> Date: Tue, 12 Mar 2024 18:25:54 +0300 Subject: [PATCH 03/53] type conversion fix --- core/mem/tracking_allocator.odin | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/core/mem/tracking_allocator.odin b/core/mem/tracking_allocator.odin index 9ed49a41b..bc624617d 100644 --- a/core/mem/tracking_allocator.odin +++ b/core/mem/tracking_allocator.odin @@ -68,18 +68,18 @@ tracking_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode, size, alignment: int, old_memory: rawptr, old_size: int, loc := #caller_location) -> (result: []byte, err: Allocator_Error) { track_alloc :: proc(data: ^Tracking_Allocator, entry: ^Tracking_Allocator_Entry) { - data.total_memory_allocated += entry.size + data.total_memory_allocated += i64(entry.size) data.total_allocation_count += 1 - data.current_memory_allocated += entry.size + data.current_memory_allocated += i64(entry.size) if data.current_memory_allocated > data.peak_memory_allocated { data.peak_memory_allocated = data.current_memory_allocated } } track_free :: proc(data: ^Tracking_Allocator, entry: ^Tracking_Allocator_Entry) { - data.total_memory_freed += entry.size + data.total_memory_freed += i64(entry.size) data.total_free_count += 1 - data.current_memory_allocated -= entry.size + data.current_memory_allocated -= i64(entry.size) } data := (^Tracking_Allocator)(allocator_data) From 902e877467bb4bb1705652e36e5f43a2776adeb7 Mon Sep 17 00:00:00 2001 From: Yawning Angel Date: Sun, 21 Apr 2024 21:21:46 +0900 Subject: [PATCH 04/53] repo: Add more test binaries to .gitignore --- .gitignore | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.gitignore b/.gitignore index 228f006a3..f6c3927a2 100644 --- a/.gitignore +++ b/.gitignore @@ -27,6 +27,8 @@ tests/documentation/all.odin-doc tests/internal/test_map tests/internal/test_pow tests/internal/test_rtti +tests/core/test_base64 +tests/core/test_cbor tests/core/test_core_compress tests/core/test_core_container tests/core/test_core_filepath @@ -40,8 +42,10 @@ tests/core/test_core_net tests/core/test_core_os_exit tests/core/test_core_reflect tests/core/test_core_strings +tests/core/test_core_time tests/core/test_crypto tests/core/test_hash +tests/core/test_hex tests/core/test_hxa tests/core/test_json tests/core/test_linalg_glsl_math From d1a1e8f646c5b959be638aa955856f686b11a4f3 Mon Sep 17 00:00:00 2001 From: Laytan Date: Mon, 22 Apr 2024 20:33:19 +0200 Subject: [PATCH 05/53] fix linking with clang-18 Because we currently just use the clang from the user's path linking suddenly breaks when the user updates their system clang to 18 with an error about an unknown option -arch. I had already fixed it for my LLVM 18 PR but it seems like a good idea to get this in already to avoid that breakage (had a few people come to the Discord with it and an issue). This fixes #3461 --- src/build_settings.cpp | 102 ++++++++++++----------------------------- 1 file changed, 29 insertions(+), 73 deletions(-) diff --git a/src/build_settings.cpp b/src/build_settings.cpp index 03a95a19b..3bd362996 100644 --- a/src/build_settings.cpp +++ b/src/build_settings.cpp @@ -1465,26 +1465,6 @@ gb_internal void init_build_context(TargetMetrics *cross_target, Subtarget subta } bc->metrics = *metrics; - if (metrics->os == TargetOs_darwin) { - if (!bc->minimum_os_version_string_given) { - bc->minimum_os_version_string = str_lit("11.0.0"); - } - - switch (subtarget) { - case Subtarget_Default: - bc->metrics.target_triplet = concatenate_strings(permanent_allocator(), bc->metrics.target_triplet, bc->minimum_os_version_string); - break; - case Subtarget_iOS: - if (metrics->arch == TargetArch_arm64) { - bc->metrics.target_triplet = str_lit("arm64-apple-ios"); - } else if (metrics->arch == TargetArch_amd64) { - bc->metrics.target_triplet = str_lit("x86_64-apple-ios"); - } else { - GB_PANIC("Unknown architecture for darwin"); - } - break; - } - } bc->ODIN_OS = target_os_names[metrics->os]; bc->ODIN_ARCH = target_arch_names[metrics->arch]; @@ -1520,62 +1500,26 @@ gb_internal void init_build_context(TargetMetrics *cross_target, Subtarget subta bc->ODIN_WINDOWS_SUBSYSTEM = windows_subsystem_names[Windows_Subsystem_CONSOLE]; } - // NOTE(zangent): The linker flags to set the build architecture are different - // across OSs. It doesn't make sense to allocate extra data on the heap - // here, so I just #defined the linker flags to keep things concise. - if (bc->metrics.arch == TargetArch_amd64) { - switch (bc->metrics.os) { - case TargetOs_windows: - bc->link_flags = str_lit("/machine:x64 "); + if (metrics->os == TargetOs_darwin && subtarget == Subtarget_iOS) { + switch (metrics->arch) { + case TargetArch_arm64: + bc->metrics.target_triplet = str_lit("arm64-apple-ios"); break; - case TargetOs_darwin: - bc->link_flags = str_lit("-arch x86_64 "); - break; - case TargetOs_linux: - bc->link_flags = str_lit("-arch x86-64 "); - break; - case TargetOs_freebsd: - bc->link_flags = str_lit("-arch x86-64 "); - break; - case TargetOs_openbsd: - bc->link_flags = str_lit("-arch x86-64 "); - break; - case TargetOs_haiku: - bc->link_flags = str_lit("-arch x86-64 "); - break; - } - } else if (bc->metrics.arch == TargetArch_i386) { - switch (bc->metrics.os) { - case TargetOs_windows: - bc->link_flags = str_lit("/machine:x86 "); - break; - case TargetOs_darwin: - gb_printf_err("Unsupported architecture\n"); - gb_exit(1); - break; - case TargetOs_linux: - bc->link_flags = str_lit("-arch x86 "); - break; - case TargetOs_freebsd: - bc->link_flags = str_lit("-arch x86 "); - break; - } - } else if (bc->metrics.arch == TargetArch_arm32) { - switch (bc->metrics.os) { - case TargetOs_linux: - bc->link_flags = str_lit("-arch arm "); + case TargetArch_amd64: + bc->metrics.target_triplet = str_lit("x86_64-apple-ios"); break; default: - gb_printf_err("Compiler Error: Unsupported architecture\n"); - gb_exit(1); + GB_PANIC("Unknown architecture for darwin"); } - } else if (bc->metrics.arch == TargetArch_arm64) { - switch (bc->metrics.os) { - case TargetOs_darwin: - bc->link_flags = str_lit("-arch arm64 "); + } + + if (bc->metrics.os == TargetOs_windows) { + switch (bc->metrics.arch) { + case TargetArch_amd64: + bc->link_flags = str_lit("/machine:x64 "); break; - case TargetOs_linux: - bc->link_flags = str_lit("-arch aarch64 "); + case TargetArch_i386: + bc->link_flags = str_lit("/machine:x86 "); break; } } else if (is_arch_wasm()) { @@ -1595,8 +1539,20 @@ gb_internal void init_build_context(TargetMetrics *cross_target, Subtarget subta // Disallow on wasm bc->use_separate_modules = false; } else { - gb_printf_err("Compiler Error: Unsupported architecture\n"); - gb_exit(1); + bc->link_flags = concatenate3_strings(permanent_allocator(), + str_lit("-target "), bc->metrics.target_triplet, str_lit(" ")); + } + + // NOTE: needs to be done after adding the -target flag to the linker flags so the linker + // does not annoy the user with version warnings. + if (metrics->os == TargetOs_darwin) { + if (!bc->minimum_os_version_string_given) { + bc->minimum_os_version_string = str_lit("11.0.0"); + } + + if (subtarget == Subtarget_Default) { + bc->metrics.target_triplet = concatenate_strings(permanent_allocator(), bc->metrics.target_triplet, bc->minimum_os_version_string); + } } if (bc->ODIN_DEBUG && !bc->custom_optimization_level) { From a6eb64df6cd136639d1234e5a157ad280a1a32a8 Mon Sep 17 00:00:00 2001 From: Yawning Angel Date: Sun, 21 Apr 2024 21:06:21 +0900 Subject: [PATCH 06/53] core/crypto: Add a `HAS_RAND_BYTES` constant --- core/crypto/crypto.odin | 9 +++------ core/crypto/rand_bsd.odin | 7 +++---- core/crypto/rand_darwin.odin | 7 +++---- core/crypto/rand_generic.odin | 7 +++---- core/crypto/rand_js.odin | 8 ++++---- core/crypto/rand_linux.odin | 8 ++++---- core/crypto/rand_windows.odin | 7 +++---- tests/core/crypto/test_core_crypto.odin | 2 +- 8 files changed, 24 insertions(+), 31 deletions(-) diff --git a/core/crypto/crypto.odin b/core/crypto/crypto.odin index 05f25111a..f0874cc6d 100644 --- a/core/crypto/crypto.odin +++ b/core/crypto/crypto.odin @@ -49,15 +49,12 @@ compare_byte_ptrs_constant_time :: proc "contextless" (a, b: ^byte, n: int) -> i // the system entropy source. This routine will block if the system entropy // source is not ready yet. All system entropy source failures are treated // as catastrophic, resulting in a panic. +// +// Support for the system entropy source can be checked with the +// `HAS_RAND_BYTES` boolean constant. rand_bytes :: proc (dst: []byte) { // zero-fill the buffer first mem.zero_explicit(raw_data(dst), len(dst)) _rand_bytes(dst) } - -// has_rand_bytes returns true iff the target has support for accessing the -// system entropty source. -has_rand_bytes :: proc () -> bool { - return _has_rand_bytes() -} diff --git a/core/crypto/rand_bsd.odin b/core/crypto/rand_bsd.odin index 7a0c42683..a31e4f2b2 100644 --- a/core/crypto/rand_bsd.odin +++ b/core/crypto/rand_bsd.odin @@ -3,14 +3,13 @@ package crypto foreign import libc "system:c" +HAS_RAND_BYTES :: true + foreign libc { arc4random_buf :: proc(buf: [^]byte, nbytes: uint) --- } +@(private) _rand_bytes :: proc(dst: []byte) { arc4random_buf(raw_data(dst), len(dst)) } - -_has_rand_bytes :: proc() -> bool { - return true -} diff --git a/core/crypto/rand_darwin.odin b/core/crypto/rand_darwin.odin index c1a3d1dbc..5355f31c5 100644 --- a/core/crypto/rand_darwin.odin +++ b/core/crypto/rand_darwin.odin @@ -5,6 +5,9 @@ import "core:fmt" import CF "core:sys/darwin/CoreFoundation" import Sec "core:sys/darwin/Security" +HAS_RAND_BYTES :: true + +@(private) _rand_bytes :: proc(dst: []byte) { err := Sec.RandomCopyBytes(count=len(dst), bytes=raw_data(dst)) if err != .Success { @@ -12,7 +15,3 @@ _rand_bytes :: proc(dst: []byte) { panic(fmt.tprintf("crypto/rand_bytes: SecRandomCopyBytes returned non-zero result: %v %s", err, msg)) } } - -_has_rand_bytes :: proc() -> bool { - return true -} diff --git a/core/crypto/rand_generic.odin b/core/crypto/rand_generic.odin index cba49f700..4ea61ec91 100644 --- a/core/crypto/rand_generic.odin +++ b/core/crypto/rand_generic.odin @@ -6,10 +6,9 @@ //+build !js package crypto +HAS_RAND_BYTES :: false + +@(private) _rand_bytes :: proc(dst: []byte) { unimplemented("crypto: rand_bytes not supported on this OS") } - -_has_rand_bytes :: proc() -> bool { - return false -} diff --git a/core/crypto/rand_js.odin b/core/crypto/rand_js.odin index 90f60b99b..72093810e 100644 --- a/core/crypto/rand_js.odin +++ b/core/crypto/rand_js.odin @@ -6,8 +6,12 @@ foreign odin_env { env_rand_bytes :: proc "contextless" (buf: []byte) --- } +HAS_RAND_BYTES :: true + +@(private) _MAX_PER_CALL_BYTES :: 65536 // 64kiB +@(private) _rand_bytes :: proc(dst: []byte) { dst := dst @@ -18,7 +22,3 @@ _rand_bytes :: proc(dst: []byte) { dst = dst[to_read:] } } - -_has_rand_bytes :: proc() -> bool { - return true -} diff --git a/core/crypto/rand_linux.odin b/core/crypto/rand_linux.odin index a9dc37415..43b3b3075 100644 --- a/core/crypto/rand_linux.odin +++ b/core/crypto/rand_linux.odin @@ -4,8 +4,12 @@ import "core:fmt" import "core:sys/linux" +HAS_RAND_BYTES :: true + +@(private) _MAX_PER_CALL_BYTES :: 33554431 // 2^25 - 1 +@(private) _rand_bytes :: proc (dst: []byte) { dst := dst l := len(dst) @@ -34,7 +38,3 @@ _rand_bytes :: proc (dst: []byte) { dst = dst[n_read:] } } - -_has_rand_bytes :: proc() -> bool { - return true -} diff --git a/core/crypto/rand_windows.odin b/core/crypto/rand_windows.odin index 5cafe7fb5..a92d376cb 100644 --- a/core/crypto/rand_windows.odin +++ b/core/crypto/rand_windows.odin @@ -4,6 +4,9 @@ import win32 "core:sys/windows" import "core:os" import "core:fmt" +HAS_RAND_BYTES :: true + +@(private) _rand_bytes :: proc(dst: []byte) { ret := (os.Errno)(win32.BCryptGenRandom(nil, raw_data(dst), u32(len(dst)), win32.BCRYPT_USE_SYSTEM_PREFERRED_RNG)) if ret != os.ERROR_NONE { @@ -21,7 +24,3 @@ _rand_bytes :: proc(dst: []byte) { } } } - -_has_rand_bytes :: proc() -> bool { - return true -} diff --git a/tests/core/crypto/test_core_crypto.odin b/tests/core/crypto/test_core_crypto.odin index 72d8e7c78..95db3f292 100644 --- a/tests/core/crypto/test_core_crypto.odin +++ b/tests/core/crypto/test_core_crypto.odin @@ -277,7 +277,7 @@ test_chacha20poly1305 :: proc(t: ^testing.T) { test_rand_bytes :: proc(t: ^testing.T) { tc.log(t, "Testing rand_bytes") - if !crypto.has_rand_bytes() { + if !crypto.HAS_RAND_BYTES { tc.log(t, "rand_bytes not supported - skipping") return } From e2fa9be7e2c02ad950e4f3205f5e67c9ebd3a70c Mon Sep 17 00:00:00 2001 From: Yawning Angel Date: Sun, 21 Apr 2024 21:16:50 +0900 Subject: [PATCH 07/53] core/math/rand: Use `core:crypto` for the system RNG This removes some code duplication and expands support for the system RNG to all targets that `core:crypto` supports. --- core/math/rand/rand.odin | 39 ++++++++++++++++-------------- core/math/rand/system_darwin.odin | 22 ----------------- core/math/rand/system_js.odin | 14 ----------- core/math/rand/system_linux.odin | 29 ---------------------- core/math/rand/system_windows.odin | 13 ---------- 5 files changed, 21 insertions(+), 96 deletions(-) delete mode 100644 core/math/rand/system_darwin.odin delete mode 100644 core/math/rand/system_js.odin delete mode 100644 core/math/rand/system_linux.odin delete mode 100644 core/math/rand/system_windows.odin diff --git a/core/math/rand/rand.odin b/core/math/rand/rand.odin index d6a20bd1e..664d6abc9 100644 --- a/core/math/rand/rand.odin +++ b/core/math/rand/rand.odin @@ -5,6 +5,7 @@ Package core:math/rand implements various random number generators package rand import "base:intrinsics" +import "core:crypto" import "core:math" import "core:mem" @@ -104,27 +105,30 @@ init :: proc(r: ^Rand, seed: u64) { } /* -Initialises a random number generator to use the system random number generator. -The system random number generator is platform specific. -On `linux` refer to the `getrandom` syscall. -On `darwin` refer to `getentropy`. -On `windows` refer to `BCryptGenRandom`. - -All other platforms are not supported +Initialises a random number generator to use the system random number generator. +The system random number generator is platform specific, and not supported +on all targets. Inputs: - r: The random number generator to use the system random number generator -WARNING: Panics if the system is not either `windows`, `darwin` or `linux` +WARNING: Panics if the system random number generator is not supported. +Support can be determined via the `core:crypto.HAS_RAND_BYTES` constant. Example: + import "core:crypto" import "core:math/rand" import "core:fmt" init_as_system_example :: proc() { my_rand: rand.Rand - rand.init_as_system(&my_rand) - fmt.println(rand.uint64(&my_rand)) + switch crypto.HAS_RAND_BYTES { + case true: + rand.init_as_system(&my_rand) + fmt.println(rand.uint64(&my_rand)) + case false: + fmt.println("system random not supported!") + } } Possible Output: @@ -133,7 +137,7 @@ Possible Output: */ init_as_system :: proc(r: ^Rand) { - if !#defined(_system_random) { + if !crypto.HAS_RAND_BYTES { panic(#procedure + " is not supported on this platform yet") } r.state = 0 @@ -144,15 +148,14 @@ init_as_system :: proc(r: ^Rand) { @(private) _random_u64 :: proc(r: ^Rand) -> u64 { r := r - if r == nil { + switch { + case r == nil: r = &global_rand + case r.is_system: + value: u64 + crypto.rand_bytes((cast([^]u8)&value)[:size_of(u64)]) + return value } - when #defined(_system_random) { - if r.is_system { - return _system_random() - } - } - old_state := r.state r.state = old_state * 6364136223846793005 + (r.inc|1) diff --git a/core/math/rand/system_darwin.odin b/core/math/rand/system_darwin.odin deleted file mode 100644 index 756f7fcae..000000000 --- a/core/math/rand/system_darwin.odin +++ /dev/null @@ -1,22 +0,0 @@ -package rand - -import "core:sys/darwin" - -@(require_results) -_system_random :: proc() -> u64 { - for { - value: u64 - ret := darwin.syscall_getentropy(([^]u8)(&value), size_of(value)) - if ret < 0 { - switch ret { - case -4: // EINTR - continue - case -78: // ENOSYS - panic("getentropy not available in kernel") - case: - panic("getentropy failed") - } - } - return value - } -} \ No newline at end of file diff --git a/core/math/rand/system_js.odin b/core/math/rand/system_js.odin deleted file mode 100644 index b9b71c4a6..000000000 --- a/core/math/rand/system_js.odin +++ /dev/null @@ -1,14 +0,0 @@ -package rand - -foreign import "odin_env" -foreign odin_env { - @(link_name = "rand_bytes") - env_rand_bytes :: proc "contextless" (buf: []byte) --- -} - -@(require_results) -_system_random :: proc() -> u64 { - buf: [8]u8 - env_rand_bytes(buf[:]) - return transmute(u64)buf -} diff --git a/core/math/rand/system_linux.odin b/core/math/rand/system_linux.odin deleted file mode 100644 index 42c9f86fa..000000000 --- a/core/math/rand/system_linux.odin +++ /dev/null @@ -1,29 +0,0 @@ -package rand - -import "core:sys/linux" - -@(require_results) -_system_random :: proc() -> u64 { - for { - value: u64 - value_buf := (cast([^]u8)&value)[:size_of(u64)] - _, errno := linux.getrandom(value_buf, {}) - #partial switch errno { - case .NONE: - // Do nothing - case .EINTR: - // Call interupted by a signal handler, just retry the request. - continue - case .ENOSYS: - // The kernel is apparently prehistoric (< 3.17 circa 2014) - // and does not support getrandom. - panic("getrandom not available in kernel") - case: - // All other failures are things that should NEVER happen - // unless the kernel interface changes (ie: the Linux - // developers break userland). - panic("getrandom failed") - } - return value - } -} \ No newline at end of file diff --git a/core/math/rand/system_windows.odin b/core/math/rand/system_windows.odin deleted file mode 100644 index c6d68816d..000000000 --- a/core/math/rand/system_windows.odin +++ /dev/null @@ -1,13 +0,0 @@ -package rand - -import win32 "core:sys/windows" - -@(require_results) -_system_random :: proc() -> u64 { - value: u64 - status := win32.BCryptGenRandom(nil, ([^]u8)(&value), size_of(value), win32.BCRYPT_USE_SYSTEM_PREFERRED_RNG) - if status < 0 { - panic("BCryptGenRandom failed") - } - return value -} \ No newline at end of file From e8c5bb46296f8eb65fe8f343407381e88ba9bcca Mon Sep 17 00:00:00 2001 From: Laytan Laats Date: Wed, 24 Apr 2024 18:38:46 +0200 Subject: [PATCH 08/53] compiler: support returning 0 sized types in arm64 abi --- src/llvm_abi.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/llvm_abi.cpp b/src/llvm_abi.cpp index 88bb58c55..85a16d321 100644 --- a/src/llvm_abi.cpp +++ b/src/llvm_abi.cpp @@ -1131,8 +1131,9 @@ namespace lbAbiArm64 { if (size <= 16) { LLVMTypeRef cast_type = nullptr; - GB_ASSERT(size > 0); - if (size <= 8) { + if (size == 0) { + cast_type = LLVMStructTypeInContext(c, nullptr, 0, false); + } else if (size <= 8) { cast_type = LLVMIntTypeInContext(c, cast(unsigned)(size*8)); } else { unsigned count = cast(unsigned)((size+7)/8); From a4cec2e8b823ee922d73c15ba5e2fbf8461b89a4 Mon Sep 17 00:00:00 2001 From: Laytan Laats Date: Wed, 24 Apr 2024 18:37:03 +0200 Subject: [PATCH 09/53] sys/darwin/foundation: fix Application->sendEvent signature --- core/sys/darwin/Foundation/NSApplication.odin | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/sys/darwin/Foundation/NSApplication.odin b/core/sys/darwin/Foundation/NSApplication.odin index d332345f9..34221aed6 100644 --- a/core/sys/darwin/Foundation/NSApplication.odin +++ b/core/sys/darwin/Foundation/NSApplication.odin @@ -132,7 +132,7 @@ Application_nextEventMatchingMask :: proc "c" (self: ^Application, mask: EventMa @(objc_type=Application, objc_name="sendEvent") Application_sendEvent :: proc "c" (self: ^Application, event: ^Event) { - msgSend(Event, self, "sendEvent:", event) + msgSend(nil, self, "sendEvent:", event) } @(objc_type=Application, objc_name="updateWindows") Application_updateWindows :: proc "c" (self: ^Application) { From d3bd1c211038d4cbc6eacfba642154282c48d845 Mon Sep 17 00:00:00 2001 From: Laytan Laats Date: Thu, 25 Apr 2024 19:08:48 +0200 Subject: [PATCH 10/53] improve some Negative_Read/Negative_Write logic Returns the actual error if one is set, instead of swallowing it for the less descriptive negative error. Also fixes a out-of-bounds slice error in `bufio.writer_write` because it wasn't checking the returned `m`. --- core/bufio/reader.odin | 8 ++++---- core/bufio/writer.odin | 4 ++++ core/bytes/buffer.odin | 2 +- 3 files changed, 9 insertions(+), 5 deletions(-) diff --git a/core/bufio/reader.odin b/core/bufio/reader.odin index e5307f105..8ec736a66 100644 --- a/core/bufio/reader.odin +++ b/core/bufio/reader.odin @@ -81,7 +81,7 @@ _reader_read_new_chunk :: proc(b: ^Reader) -> io.Error { for i := b.max_consecutive_empty_reads; i > 0; i -= 1 { n, err := io.read(b.rd, b.buf[b.w:]) if n < 0 { - return .Negative_Read + return err if err != nil else .Negative_Read } b.w += n if err != nil { @@ -189,7 +189,7 @@ reader_read :: proc(b: ^Reader, p: []byte) -> (n: int, err: io.Error) { if len(p) >= len(b.buf) { n, b.err = io.read(b.rd, p) if n < 0 { - return 0, .Negative_Read + return 0, b.err if b.err != nil else .Negative_Read } if n > 0 { @@ -202,7 +202,7 @@ reader_read :: proc(b: ^Reader, p: []byte) -> (n: int, err: io.Error) { b.r, b.w = 0, 0 n, b.err = io.read(b.rd, b.buf) if n < 0 { - return 0, .Negative_Read + return 0, b.err if b.err != nil else .Negative_Read } if n == 0 { return 0, _reader_consume_err(b) @@ -290,7 +290,7 @@ reader_write_to :: proc(b: ^Reader, w: io.Writer) -> (n: i64, err: io.Error) { write_buf :: proc(b: ^Reader, w: io.Writer) -> (i64, io.Error) { n, err := io.write(w, b.buf[b.r:b.w]) if n < 0 { - return 0, .Negative_Write + return 0, err if err != nil else .Negative_Write } b.r += n return i64(n), err diff --git a/core/bufio/writer.odin b/core/bufio/writer.odin index 3c7fd30c5..5edd3dd6b 100644 --- a/core/bufio/writer.odin +++ b/core/bufio/writer.odin @@ -95,6 +95,10 @@ writer_write :: proc(b: ^Writer, p: []byte) -> (n: int, err: io.Error) { m: int if writer_buffered(b) == 0 { m, b.err = io.write(b.wr, p) + if m < 0 && b.err == nil { + b.err = .Negative_Write + break + } } else { m = copy(b.buf[b.n:], p) b.n += m diff --git a/core/bytes/buffer.odin b/core/bytes/buffer.odin index abfee6f2f..cb2ef9c62 100644 --- a/core/bytes/buffer.odin +++ b/core/bytes/buffer.odin @@ -359,7 +359,7 @@ buffer_read_from :: proc(b: ^Buffer, r: io.Reader) -> (n: i64, err: io.Error) #n resize(&b.buf, i) m, e := io.read(r, b.buf[i:cap(b.buf)]) if m < 0 { - err = .Negative_Read + err = e if e != nil else .Negative_Read return } From f95bb77f722ac076963f072432c508a32d338340 Mon Sep 17 00:00:00 2001 From: Feoramund <161657516+Feoramund@users.noreply.github.com> Date: Fri, 26 Apr 2024 05:19:52 -0400 Subject: [PATCH 11/53] Fix memory leak in `sync/chan` --- core/sync/chan/chan.odin | 2 ++ 1 file changed, 2 insertions(+) diff --git a/core/sync/chan/chan.odin b/core/sync/chan/chan.odin index cbcfdf3bf..f4774e4f8 100644 --- a/core/sync/chan/chan.odin +++ b/core/sync/chan/chan.odin @@ -75,6 +75,7 @@ create_raw_unbuffered :: proc(#any_int msg_size, msg_alignment: int, allocator: ptr := mem.alloc(size, align, allocator) or_return c = (^Raw_Chan)(ptr) + c.allocator = allocator c.allocation_size = size c.unbuffered_data = ([^]byte)(ptr)[offset:] c.msg_size = u16(msg_size) @@ -99,6 +100,7 @@ create_raw_buffered :: proc(#any_int msg_size, msg_alignment: int, #any_int cap: ptr := mem.alloc(size, align, allocator) or_return c = (^Raw_Chan)(ptr) + c.allocator = allocator c.allocation_size = size bptr := ([^]byte)(ptr) From 94e0707456bd2dc697cff055810e36d68e0f5a47 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Fri, 26 Apr 2024 13:12:15 +0100 Subject: [PATCH 12/53] Fix minor bug --- src/linker.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/linker.cpp b/src/linker.cpp index 498a96c5f..e694fd999 100644 --- a/src/linker.cpp +++ b/src/linker.cpp @@ -167,7 +167,7 @@ gb_internal i32 linker_stage(LinkerData *gen) { if (has_asm_extension(lib)) { if (!string_set_update(&asm_files, lib)) { - String asm_file = asm_files.entries[i].value; + String asm_file = lib; String obj_file = concatenate_strings(permanent_allocator(), asm_file, str_lit(".obj")); String obj_format = str_lit("win64"); #if defined(GB_ARCH_32_BIT) From 7305478261700fc95f6748ba3091978a3fe7b1f3 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Fri, 26 Apr 2024 13:12:23 +0100 Subject: [PATCH 13/53] Minor changes --- src/checker.cpp | 2 ++ src/docs_writer.cpp | 11 +++++------ 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/src/checker.cpp b/src/checker.cpp index b7fe2b903..116f275bc 100644 --- a/src/checker.cpp +++ b/src/checker.cpp @@ -4486,6 +4486,8 @@ gb_internal void check_all_global_entities(Checker *c) { (void)type_align_of(e->type); } } + + gb_printf_err("Global Entity Count: %td\n", c->info.entities.count); } diff --git a/src/docs_writer.cpp b/src/docs_writer.cpp index 9ced78d33..ba71eae4d 100644 --- a/src/docs_writer.cpp +++ b/src/docs_writer.cpp @@ -987,9 +987,8 @@ gb_internal void odin_doc_update_entities(OdinDocWriter *w) { auto entities = array_make(heap_allocator(), 0, w->entity_cache.count); defer (array_free(&entities)); - for (u32 i = 0; i < w->entity_cache.count; i++) { - Entity *e = w->entity_cache.entries[i].key; - array_add(&entities, e); + for (auto const &entry : w->entity_cache) { + array_add(&entities, entry.key); } for (Entity *e : entities) { GB_ASSERT(e != nullptr); @@ -998,9 +997,9 @@ gb_internal void odin_doc_update_entities(OdinDocWriter *w) { } } - for (u32 i = 0; i < w->entity_cache.count; i++) { - Entity *e = w->entity_cache.entries[i].key; - OdinDocEntityIndex entity_index = w->entity_cache.entries[i].value; + for (auto const &entry : w->entity_cache) { + Entity *e = entry.key; + OdinDocEntityIndex entity_index = entry.value; OdinDocTypeIndex type_index = odin_doc_type(w, e->type); OdinDocEntityIndex foreign_library = 0; From a3e77dcc3bc4cf7d0548afa73d38914354761aa0 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Fri, 26 Apr 2024 13:25:08 +0100 Subject: [PATCH 14/53] Minor clean up --- src/checker.cpp | 16 ++++++++++------ src/string_map.cpp | 7 ++++--- src/string_set.cpp | 4 +++- 3 files changed, 17 insertions(+), 10 deletions(-) diff --git a/src/checker.cpp b/src/checker.cpp index 116f275bc..f3e14eeba 100644 --- a/src/checker.cpp +++ b/src/checker.cpp @@ -4309,17 +4309,21 @@ gb_internal bool correct_single_type_alias(CheckerContext *c, Entity *e) { gb_internal bool correct_type_alias_in_scope_backwards(CheckerContext *c, Scope *s) { bool correction = false; - u32 n = s->elements.count; - for (u32 i = n-1; i < n; i--) { - correction |= correct_single_type_alias(c, s->elements.entries[i].value); + for (u32 n = s->elements.count, i = n-1; i < n; i--) { + Entity *e = s->elements.entries[i].value; + if (e != nullptr) { + correction |= correct_single_type_alias(c, e); + } } return correction; } gb_internal bool correct_type_alias_in_scope_forwards(CheckerContext *c, Scope *s) { bool correction = false; - u32 n = s->elements.count; - for (isize i = 0; i < n; i++) { - correction |= correct_single_type_alias(c, s->elements.entries[i].value); + for (auto const &entry : s->elements) { + Entity *e = entry.value; + if (e != nullptr) { + correction |= correct_single_type_alias(c, entry.value); + } } return correction; } diff --git a/src/string_map.cpp b/src/string_map.cpp index f8b86a950..894579a03 100644 --- a/src/string_map.cpp +++ b/src/string_map.cpp @@ -2,8 +2,8 @@ GB_STATIC_ASSERT(sizeof(MapIndex) == sizeof(u32)); struct StringHashKey { - u32 hash; String string; + u32 hash; operator String() const noexcept { return this->string; @@ -329,11 +329,12 @@ gb_internal StringMapEntry const *begin(StringMap const &m) noexcept { template -gb_internal StringMapEntry *end(StringMap &m) { +gb_internal StringMapEntry *end(StringMap &m) noexcept { return m.entries + m.count; } template gb_internal StringMapEntry const *end(StringMap const &m) noexcept { return m.entries + m.count; -} \ No newline at end of file +} + diff --git a/src/string_set.cpp b/src/string_set.cpp index fb4640c20..a37d8ba80 100644 --- a/src/string_set.cpp +++ b/src/string_set.cpp @@ -208,7 +208,9 @@ gb_internal void string_set__erase(StringSet *s, MapFindResult fr) { } auto *entry = &s->entries[fr.entry_index]; *entry = s->entries[s->entries.count-1]; - StringHashKey key = {entry->hash, entry->value}; + StringHashKey key; + key.hash = entry->hash; + key.string = entry->value; last = string_set__find(s, key); if (last.entry_prev != MAP_SENTINEL) { s->entries[last.entry_prev].next = fr.entry_index; From c685b404ea9cc9807b6d8cfc6f2a119924a79b4f Mon Sep 17 00:00:00 2001 From: gingerBill Date: Fri, 26 Apr 2024 14:15:22 +0100 Subject: [PATCH 15/53] Implement dumb `StringMap` --- src/checker.cpp | 7 +- src/string_map.cpp | 305 ++++++++++++++++++++------------------------- 2 files changed, 139 insertions(+), 173 deletions(-) diff --git a/src/checker.cpp b/src/checker.cpp index f3e14eeba..3b51cc6e0 100644 --- a/src/checker.cpp +++ b/src/checker.cpp @@ -4310,8 +4310,9 @@ gb_internal bool correct_single_type_alias(CheckerContext *c, Entity *e) { gb_internal bool correct_type_alias_in_scope_backwards(CheckerContext *c, Scope *s) { bool correction = false; for (u32 n = s->elements.count, i = n-1; i < n; i--) { - Entity *e = s->elements.entries[i].value; - if (e != nullptr) { + auto const &entry = s->elements.entries[i]; + Entity *e = entry.value; + if (entry.hash && e != nullptr) { correction |= correct_single_type_alias(c, e); } } @@ -4490,8 +4491,6 @@ gb_internal void check_all_global_entities(Checker *c) { (void)type_align_of(e->type); } } - - gb_printf_err("Global Entity Count: %td\n", c->info.entities.count); } diff --git a/src/string_map.cpp b/src/string_map.cpp index 894579a03..4de88bbf9 100644 --- a/src/string_map.cpp +++ b/src/string_map.cpp @@ -13,7 +13,8 @@ struct StringHashKey { } }; gb_internal gb_inline u32 string_hash(String const &s) { - return fnv32a(s.text, s.len) & 0x7fffffff; + u32 res = fnv32a(s.text, s.len) & 0x7fffffff; + return res | (res == 0); } gb_internal gb_inline StringHashKey string_hash_string(String const &s) { @@ -25,19 +26,16 @@ gb_internal gb_inline StringHashKey string_hash_string(String const &s) { template struct StringMapEntry { - String key; - u32 hash; - MapIndex next; - T value; + String key; + u32 hash; + T value; }; template struct StringMap { - MapIndex * hashes; - usize hashes_count; StringMapEntry *entries; u32 count; - u32 entries_capacity; + u32 capacity; }; @@ -73,127 +71,91 @@ gb_internal gb_inline void string_map_init(StringMap *h, usize capacity) { template gb_internal gb_inline void string_map_destroy(StringMap *h) { - gb_free(string_map_allocator(), h->hashes); gb_free(string_map_allocator(), h->entries); } template -gb_internal void string_map__resize_hashes(StringMap *h, usize count) { - h->hashes_count = cast(u32)resize_array_raw(&h->hashes, string_map_allocator(), h->hashes_count, count, MAP_CACHE_LINE_SIZE); -} - - -template -gb_internal void string_map__reserve_entries(StringMap *h, usize capacity) { - h->entries_capacity = cast(u32)resize_array_raw(&h->entries, string_map_allocator(), h->entries_capacity, capacity, MAP_CACHE_LINE_SIZE); -} - - -template -gb_internal MapIndex string_map__add_entry(StringMap *h, u32 hash, String const &key) { - StringMapEntry e = {}; - e.key = key; - e.hash = hash; - e.next = MAP_SENTINEL; - if (h->count+1 >= h->entries_capacity) { - string_map__reserve_entries(h, gb_max(h->entries_capacity*2, 4)); +gb_internal void string_map__insert(StringMap *h, u32 hash, String const &key, T const &value) { + if (h->count+1 >= h->capacity) { + string_map_grow(h); } - h->entries[h->count++] = e; - return cast(MapIndex)(h->count-1); -} + GB_ASSERT(h->count+1 < h->capacity); -template -gb_internal MapFindResult string_map__find(StringMap *h, u32 hash, String const &key) { - MapFindResult fr = {MAP_SENTINEL, MAP_SENTINEL, MAP_SENTINEL}; - if (h->hashes_count != 0) { - fr.hash_index = cast(MapIndex)(hash & (h->hashes_count-1)); - fr.entry_index = h->hashes[fr.hash_index]; - while (fr.entry_index != MAP_SENTINEL) { - auto *entry = &h->entries[fr.entry_index]; - if (entry->hash == hash && entry->key == key) { - return fr; - } - fr.entry_prev = fr.entry_index; - fr.entry_index = entry->next; + u32 mask = h->capacity-1; + MapIndex index = hash & mask; + MapIndex original_index = index; + do { + auto *entry = h->entries+index; + if (entry->hash == 0) { + entry->key = key; + entry->hash = hash; + entry->value = value; + + h->count += 1; + return; } - } - return fr; -} + index = (index+1)&mask; + } while (index != original_index); -template -gb_internal MapFindResult string_map__find_from_entry(StringMap *h, StringMapEntry *e) { - MapFindResult fr = {MAP_SENTINEL, MAP_SENTINEL, MAP_SENTINEL}; - if (h->hashes_count != 0) { - fr.hash_index = cast(MapIndex)(e->hash & (h->hashes_count-1)); - fr.entry_index = h->hashes[fr.hash_index]; - while (fr.entry_index != MAP_SENTINEL) { - auto *entry = &h->entries[fr.entry_index]; - if (entry == e) { - return fr; - } - fr.entry_prev = fr.entry_index; - fr.entry_index = entry->next; - } - } - return fr; + GB_PANIC("Full map"); } template gb_internal b32 string_map__full(StringMap *h) { - return 0.75f * h->hashes_count <= h->count; + return 0.75f * h->count <= h->capacity; } template gb_inline void string_map_grow(StringMap *h) { - isize new_count = gb_max(h->hashes_count<<1, 16); - string_map_reserve(h, new_count); + isize new_capacity = gb_max(h->capacity<<1, 16); + string_map_reserve(h, new_capacity); } -template -gb_internal void string_map_reset_entries(StringMap *h) { - for (u32 i = 0; i < h->hashes_count; i++) { - h->hashes[i] = MAP_SENTINEL; - } - for (isize i = 0; i < h->count; i++) { - MapFindResult fr; - StringMapEntry *e = &h->entries[i]; - e->next = MAP_SENTINEL; - fr = string_map__find_from_entry(h, e); - if (fr.entry_prev == MAP_SENTINEL) { - h->hashes[fr.hash_index] = cast(MapIndex)i; - } else { - h->entries[fr.entry_prev].next = cast(MapIndex)i; - } - } -} - template gb_internal void string_map_reserve(StringMap *h, usize cap) { - if (h->count*2 < h->hashes_count) { + if (cap < h->capacity) { return; } - string_map__reserve_entries(h, cap); - string_map__resize_hashes(h, cap*2); - string_map_reset_entries(h); + cap = next_pow2_isize(cap); + + StringMap new_h = {}; + new_h.count = 0; + new_h.capacity = cast(u32)cap; + new_h.entries = gb_alloc_array(string_map_allocator(), StringMapEntry, new_h.capacity); + + if (h->count) { + for (u32 i = 0; i < h->capacity; i++) { + auto *entry = h->entries+i; + if (entry->hash) { + string_map__insert(&new_h, entry->hash, entry->key, entry->value); + } + } + } + string_map_destroy(h); + *h = new_h; } template gb_internal T *string_map_get(StringMap *h, u32 hash, String const &key) { - MapFindResult fr = {MAP_SENTINEL, MAP_SENTINEL, MAP_SENTINEL}; - if (h->hashes_count != 0) { - fr.hash_index = cast(MapIndex)(hash & (h->hashes_count-1)); - fr.entry_index = h->hashes[fr.hash_index]; - while (fr.entry_index != MAP_SENTINEL) { - auto *entry = &h->entries[fr.entry_index]; - if (entry->hash == hash && entry->key == key) { - return &entry->value; - } - fr.entry_prev = fr.entry_index; - fr.entry_index = entry->next; - } + if (h->count == 0) { + return nullptr; } + u32 mask = (h->capacity-1); + u32 index = hash & mask; + u32 original_index = index; + do { + auto *entry = h->entries+index; + u32 curr_hash = entry->hash; + if (curr_hash == 0) { + // NOTE(bill): no found, but there isn't any key removal for this hash map + return nullptr; + } else if (curr_hash == hash && entry->key == key) { + return &entry->value; + } + index = (index+1) & mask; + } while (original_index != index); return nullptr; } @@ -216,9 +178,9 @@ gb_internal gb_inline T *string_map_get(StringMap *h, char const *key) { template gb_internal T &string_map_must_get(StringMap *h, u32 hash, String const &key) { - isize index = string_map__find(h, hash, key).entry_index; - GB_ASSERT(index != MAP_SENTINEL); - return h->entries[index].value; + T *found = string_map_get(h, hash, key); + GB_ASSERT(found != nullptr); + return *found; } template @@ -239,27 +201,15 @@ gb_internal gb_inline T &string_map_must_get(StringMap *h, char const *key) { template gb_internal void string_map_set(StringMap *h, u32 hash, String const &key, T const &value) { - MapIndex index; - MapFindResult fr; - if (h->hashes_count == 0) { + if (h->count == 0) { string_map_grow(h); } - fr = string_map__find(h, hash, key); - if (fr.entry_index != MAP_SENTINEL) { - index = fr.entry_index; - } else { - index = string_map__add_entry(h, hash, key); - if (fr.entry_prev != MAP_SENTINEL) { - h->entries[fr.entry_prev].next = index; - } else { - h->hashes[fr.hash_index] = index; - } - } - h->entries[index].value = value; - - if (string_map__full(h)) { - string_map_grow(h); + auto *found = string_map_get(h, hash, key); + if (found) { + *found = value; + return; } + string_map__insert(h, hash, key, value); } template @@ -278,63 +228,80 @@ gb_internal gb_inline void string_map_set(StringMap *h, StringHashKey const & } - -// template -// gb_internal void string_map__erase(StringMap *h, MapFindResult const &fr) { -// MapFindResult last; -// if (fr.entry_prev == MAP_SENTINEL) { -// h->hashes[fr.hash_index] = h->entries[fr.entry_index].next; -// } else { -// h->entries[fr.entry_prev].next = h->entries[fr.entry_index].next; -// } -// if (fr.entry_index == h->count-1) { -// array_pop(&h->entries); -// return; -// } -// h->entries[fr.entry_index] = h->entries[h->count-1]; -// last = string_map__find(h, h->entries[fr.entry_index].key); -// if (last.entry_prev != MAP_SENTINEL) { -// h->entries[last.entry_prev].next = fr.entry_index; -// } else { -// h->hashes[last.hash_index] = fr.entry_index; -// } -// } - -// template -// gb_internal void string_map_remove(StringMap *h, StringHashKey const &key) { -// MapFindResult fr = string_map__find(h, key); -// if (fr.entry_index != MAP_SENTINEL) { -// string_map__erase(h, fr); -// } -// } - template gb_internal gb_inline void string_map_clear(StringMap *h) { h->count = 0; - for (u32 i = 0; i < h->hashes_count; i++) { - h->hashes[i] = MAP_SENTINEL; + gb_zero_array(h->entries, h->capacity); +} + + +template +struct StringMapIterator { + StringMap *map; + MapIndex index; + + StringMapIterator &operator++() noexcept { + for (;;) { + ++index; + if (map->capacity == index) { + return *this; + } + StringMapEntry *entry = map->entries+index; + if (entry->hash != 0) { + return *this; + } + } } + + bool operator==(StringMapIterator const &other) const noexcept { + return this->map == other->map && this->index == other->index; + } + + operator StringMapEntry *() const { + return map->entries+index; + } +}; + + +template +gb_internal StringMapIterator end(StringMap &m) noexcept { + return StringMapIterator{&m, m.capacity}; +} + +template +gb_internal StringMapIterator const end(StringMap const &m) noexcept { + return StringMapIterator{&m, m.capacity}; } template -gb_internal StringMapEntry *begin(StringMap &m) noexcept { - return m.entries; +gb_internal StringMapIterator begin(StringMap &m) noexcept { + if (m.count == 0) { + return end(m); + } + + MapIndex index = 0; + while (index < m.capacity) { + if (m.entries[index].hash) { + break; + } + index++; + } + return StringMapIterator{&m, index}; } template -gb_internal StringMapEntry const *begin(StringMap const &m) noexcept { - return m.entries; +gb_internal StringMapIterator const begin(StringMap const &m) noexcept { + if (m.count == 0) { + return end(m); + } + + MapIndex index = 0; + while (index < m.capacity) { + if (m.entries[index].hash) { + break; + } + index++; + } + return StringMapIterator{&m, index}; } - - -template -gb_internal StringMapEntry *end(StringMap &m) noexcept { - return m.entries + m.count; -} - -template -gb_internal StringMapEntry const *end(StringMap const &m) noexcept { - return m.entries + m.count; -} - From 2b26384b89ed2b29f9d7db13f73c52029a2a2341 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Fri, 26 Apr 2024 15:04:46 +0100 Subject: [PATCH 16/53] Implement dumb `PtrMap` --- src/check_expr.cpp | 10 +- src/checker.cpp | 5 +- src/exact_value.cpp | 37 ++-- src/ptr_map.cpp | 455 +++++++++++++++++++++----------------------- 4 files changed, 245 insertions(+), 262 deletions(-) diff --git a/src/check_expr.cpp b/src/check_expr.cpp index b893b3a00..06d0a8b12 100644 --- a/src/check_expr.cpp +++ b/src/check_expr.cpp @@ -8079,11 +8079,10 @@ gb_internal void add_constant_switch_case(CheckerContext *ctx, SeenMap *seen, Op } uintptr key = hash_exact_value(operand.value); - TypeAndToken *found = map_get(seen, key); - if (found != nullptr) { + GB_ASSERT(key != 0); + isize count = multi_map_count(seen, key); + if (count) { TEMPORARY_ALLOCATOR_GUARD(); - - isize count = multi_map_count(seen, key); TypeAndToken *taps = gb_alloc_array(temporary_allocator(), TypeAndToken, count); multi_map_get_all(seen, key, taps); @@ -9406,7 +9405,8 @@ gb_internal ExprKind check_compound_literal(CheckerContext *c, Operand *o, Ast * continue; } ExactValue v = f->Constant.value; - auto found = map_get(&seen, hash_exact_value(v)); + uintptr hash = hash_exact_value(v); + auto found = map_get(&seen, hash); if (!found) { array_add(&unhandled, f); } diff --git a/src/checker.cpp b/src/checker.cpp index 3b51cc6e0..64fca0312 100644 --- a/src/checker.cpp +++ b/src/checker.cpp @@ -1897,8 +1897,7 @@ gb_internal void add_type_info_type_internal(CheckerContext *c, Type *t) { add_type_info_dependency(c->info, c->decl, t); MUTEX_GUARD_BLOCK(&c->info->type_info_mutex) { - MapFindResult fr; - auto found = map_try_get(&c->info->type_info_map, t, &fr); + auto found = map_get(&c->info->type_info_map, t); if (found != nullptr) { // Types have already been added return; @@ -1922,7 +1921,7 @@ gb_internal void add_type_info_type_internal(CheckerContext *c, Type *t) { ti_index = c->info->type_info_types.count; array_add(&c->info->type_info_types, t); } - map_set_internal_from_try_get(&c->checker->info.type_info_map, t, ti_index, fr); + map_set(&c->checker->info.type_info_map, t, ti_index); if (prev) { // NOTE(bill): If a previous one exists already, no need to continue diff --git a/src/exact_value.cpp b/src/exact_value.cpp index b744d2db0..83af82f55 100644 --- a/src/exact_value.cpp +++ b/src/exact_value.cpp @@ -54,37 +54,50 @@ gb_global ExactValue const empty_exact_value = {}; gb_internal uintptr hash_exact_value(ExactValue v) { mutex_lock(&hash_exact_value_mutex); defer (mutex_unlock(&hash_exact_value_mutex)); + + uintptr res = 0; switch (v.kind) { case ExactValue_Invalid: return 0; case ExactValue_Bool: - return gb_fnv32a(&v.value_bool, gb_size_of(v.value_bool)); + res = gb_fnv32a(&v.value_bool, gb_size_of(v.value_bool)); + break; case ExactValue_String: - return gb_fnv32a(v.value_string.text, v.value_string.len); + res = gb_fnv32a(v.value_string.text, v.value_string.len); + break; case ExactValue_Integer: { u32 key = gb_fnv32a(v.value_integer.dp, gb_size_of(*v.value_integer.dp) * v.value_integer.used); u8 last = (u8)v.value_integer.sign; - return (key ^ last) * 0x01000193; + res = (key ^ last) * 0x01000193; + break; } case ExactValue_Float: - return gb_fnv32a(&v.value_float, gb_size_of(v.value_float)); + res = gb_fnv32a(&v.value_float, gb_size_of(v.value_float)); + break; case ExactValue_Pointer: - return ptr_map_hash_key(v.value_pointer); + res = ptr_map_hash_key(v.value_pointer); + break; case ExactValue_Complex: - return gb_fnv32a(v.value_complex, gb_size_of(Complex128)); + res = gb_fnv32a(v.value_complex, gb_size_of(Complex128)); + break; case ExactValue_Quaternion: - return gb_fnv32a(v.value_quaternion, gb_size_of(Quaternion256)); + res = gb_fnv32a(v.value_quaternion, gb_size_of(Quaternion256)); + break; case ExactValue_Compound: - return ptr_map_hash_key(v.value_compound); + res = ptr_map_hash_key(v.value_compound); + break; case ExactValue_Procedure: - return ptr_map_hash_key(v.value_procedure); + res = ptr_map_hash_key(v.value_procedure); + break; case ExactValue_Typeid: - return ptr_map_hash_key(v.value_typeid); + res = ptr_map_hash_key(v.value_typeid); + break; + default: + res = gb_fnv32a(&v, gb_size_of(ExactValue)); } - return gb_fnv32a(&v, gb_size_of(ExactValue)); - + return res & 0x7fffffff; } diff --git a/src/ptr_map.cpp b/src/ptr_map.cpp index 23278014f..8fd627768 100644 --- a/src/ptr_map.cpp +++ b/src/ptr_map.cpp @@ -16,23 +16,21 @@ struct MapFindResult { }; enum : MapIndex { MAP_SENTINEL = ~(MapIndex)0 }; +static void *const MAP_TOMBSTONE = (void *)~(uintptr)0; template struct PtrMapEntry { static_assert(sizeof(K) == sizeof(void *), "Key size must be pointer size"); - K key; - V value; - MapIndex next; + K key; + V value; }; template struct PtrMap { - MapIndex * hashes; - usize hashes_count; PtrMapEntry *entries; u32 count; - u32 entries_capacity; + u32 capacity; }; @@ -69,7 +67,6 @@ template gb_internal void map_grow (PtrMap< template gb_internal void map_rehash (PtrMap *h, isize new_count); template gb_internal void map_reserve (PtrMap *h, isize cap); -#if PTR_MAP_ENABLE_MULTI_MAP // Mutlivalued map procedure template gb_internal PtrMapEntry * multi_map_find_first(PtrMap *h, K key); template gb_internal PtrMapEntry * multi_map_find_next (PtrMap *h, PtrMapEntry *e); @@ -79,7 +76,6 @@ template gb_internal void multi_map_get_all (PtrMap< template gb_internal void multi_map_insert (PtrMap *h, K key, V const &value); template gb_internal void multi_map_remove (PtrMap *h, K key, PtrMapEntry *e); template gb_internal void multi_map_remove_all(PtrMap *h, K key); -#endif gb_internal gbAllocator map_allocator(void) { return heap_allocator(); @@ -94,170 +90,141 @@ gb_internal gb_inline void map_init(PtrMap *h, isize capacity) { template gb_internal gb_inline void map_destroy(PtrMap *h) { gbAllocator a = map_allocator(); - gb_free(a, h->hashes); gb_free(a, h->entries); } -template -gb_internal void map__resize_hashes(PtrMap *h, usize count) { - h->hashes_count = cast(u32)resize_array_raw(&h->hashes, map_allocator(), h->hashes_count, count, MAP_CACHE_LINE_SIZE); -} template -gb_internal void map__reserve_entries(PtrMap *h, usize capacity) { - h->entries_capacity = cast(u32)resize_array_raw(&h->entries, map_allocator(), h->entries_capacity, capacity, MAP_CACHE_LINE_SIZE); -} - - -template -gb_internal MapIndex map__add_entry(PtrMap *h, K key) { - PtrMapEntry e = {}; - e.key = key; - e.next = MAP_SENTINEL; - if (h->count+1 >= h->entries_capacity) { - map__reserve_entries(h, gb_max(h->entries_capacity*2, 4)); - } - h->entries[h->count++] = e; - return cast(MapIndex)(h->count-1); -} - -template -gb_internal MapFindResult map__find(PtrMap *h, K key) { - MapFindResult fr = {MAP_SENTINEL, MAP_SENTINEL, MAP_SENTINEL}; - if (h->hashes_count == 0) { - return fr; +gb_internal void map__insert(PtrMap *h, K key, V const &value) { + if (h->count+1 >= h->capacity) { + map_grow(h); } u32 hash = ptr_map_hash_key(key); - fr.hash_index = cast(MapIndex)(hash & (h->hashes_count-1)); - fr.entry_index = h->hashes[fr.hash_index]; - while (fr.entry_index != MAP_SENTINEL) { - auto *entry = &h->entries[fr.entry_index]; - if (entry->key == key) { - return fr; + u32 mask = h->capacity-1; + MapIndex index = hash & mask; + MapIndex original_index = index; + do { + auto *entry = h->entries+index; + if (!entry->key || entry->key == cast(K)MAP_TOMBSTONE) { + entry->key = key; + entry->value = value; + h->count += 1; + return; } - fr.entry_prev = fr.entry_index; - fr.entry_index = entry->next; - } - return fr; -} + index = (index+1)&mask; + } while (index != original_index); -template -gb_internal MapFindResult map__find_from_entry(PtrMap *h, PtrMapEntry *e) { - MapFindResult fr = {MAP_SENTINEL, MAP_SENTINEL, MAP_SENTINEL}; - if (h->hashes_count == 0) { - return fr; - } - u32 hash = ptr_map_hash_key(e->key); - fr.hash_index = cast(MapIndex)(hash & (h->hashes_count-1)); - fr.entry_index = h->hashes[fr.hash_index]; - while (fr.entry_index != MAP_SENTINEL) { - if (&h->entries[fr.entry_index] == e) { - return fr; - } - fr.entry_prev = fr.entry_index; - fr.entry_index = h->entries[fr.entry_index].next; - } - return fr; + GB_PANIC("FAILED TO INSERT"); } template gb_internal b32 map__full(PtrMap *h) { - return 0.75f * h->hashes_count <= h->count; + return 0.75f * h->capacity <= h->count; } template gb_internal gb_inline void map_grow(PtrMap *h) { - isize new_count = gb_max(h->hashes_count<<1, 16); - map_rehash(h, new_count); + isize new_capacity = gb_max(h->capacity<<1, 16); + map_reserve(h, new_capacity); } template -gb_internal void map_reset_entries(PtrMap *h) { - for (usize i = 0; i < h->hashes_count; i++) { - h->hashes[i] = MAP_SENTINEL; - } - for (usize i = 0; i < h->count; i++) { - MapFindResult fr; - PtrMapEntry *e = &h->entries[i]; - e->next = MAP_SENTINEL; - fr = map__find_from_entry(h, e); - if (fr.entry_prev == MAP_SENTINEL) { - h->hashes[fr.hash_index] = cast(MapIndex)i; - } else { - h->entries[fr.entry_prev].next = cast(MapIndex)i; - } +gb_internal void try_map_grow(PtrMap *h) { + if (h->capacity == 0 || map__full(h)) { + map_grow(h); } } + template gb_internal void map_reserve(PtrMap *h, isize cap) { - if (h->count*2 < h->hashes_count) { + if (cap < h->capacity) { return; } - map__reserve_entries(h, cap); - map__resize_hashes(h, cap*2); - map_reset_entries(h); -} + cap = next_pow2_isize(cap); + typedef PtrMapEntry EntryType; + PtrMap new_h = {}; + new_h.count = 0; + new_h.capacity = cast(u32)cap; + new_h.entries = gb_alloc_array(string_map_allocator(), EntryType, new_h.capacity); -template -gb_internal void map_rehash(PtrMap *h, isize new_count) { - map_reserve(h, new_count); + if (h->count) { + for (u32 i = 0; i < h->capacity; i++) { + auto *entry = h->entries+i; + if (entry->key && + entry->key != cast(K)MAP_TOMBSTONE) { + map__insert(&new_h, entry->key, entry->value); + } + } + } + map_destroy(h); + *h = new_h; } template gb_internal V *map_get(PtrMap *h, K key) { - MapIndex hash_index = MAP_SENTINEL; - MapIndex entry_prev = MAP_SENTINEL; - MapIndex entry_index = MAP_SENTINEL; - if (h->hashes_count != 0) { - u32 hash = ptr_map_hash_key(key); - hash_index = cast(MapIndex)(hash & (h->hashes_count-1)); - entry_index = h->hashes[hash_index]; - while (entry_index != MAP_SENTINEL) { - auto *entry = &h->entries[entry_index]; - if (entry->key == key) { - return &entry->value; - } - entry_prev = entry_index; - entry_index = entry->next; - } + if (h->count == 0) { + return nullptr; } + if (key == 0) { + GB_PANIC("0 key"); + } + + u32 hash = ptr_map_hash_key(key); + u32 mask = (h->capacity-1); + u32 index = hash & mask; + u32 original_index = index; + do { + auto *entry = h->entries+index; + if (!entry->key) { + // NOTE(bill): no found, but there isn't any key removal for this hash map + return nullptr; + } else if (entry->key == key) { + return &entry->value; + } + index = (index+1) & mask; + } while (original_index != index); return nullptr; } template -gb_internal V *map_try_get(PtrMap *h, K key, MapFindResult *fr_) { - MapFindResult fr = {MAP_SENTINEL, MAP_SENTINEL, MAP_SENTINEL}; - if (h->hashes_count != 0) { - u32 hash = ptr_map_hash_key(key); - fr.hash_index = cast(MapIndex)(hash & (h->hashes_count-1)); - fr.entry_index = h->hashes[fr.hash_index]; - while (fr.entry_index != MAP_SENTINEL) { - auto *entry = &h->entries[fr.entry_index]; - if (entry->key == key) { - return &entry->value; - } - fr.entry_prev = fr.entry_index; - fr.entry_index = entry->next; +gb_internal V *map_try_get(PtrMap *h, K key, MapIndex *found_index_) { + if (found_index_) *found_index_ = ~(MapIndex)0; + + if (h->count == 0) { + return nullptr; + } + if (key == 0) { + GB_PANIC("0 key"); + } + + u32 hash = ptr_map_hash_key(key); + u32 mask = (h->capacity-1); + u32 index = hash & mask; + u32 original_index = index; + do { + auto *entry = h->entries+index; + if (!entry->key) { + // NOTE(bill): no found, but there isn't any key removal for this hash map + return nullptr; + } else if (entry->key == key) { + if (found_index_) *found_index_ = index; + return &entry->value; } - } - if (h->hashes_count == 0 || map__full(h)) { - map_grow(h); - } - if (fr_) *fr_ = fr; + index = (index+1) & mask; + } while (original_index != index); return nullptr; } template -gb_internal void map_set_internal_from_try_get(PtrMap *h, K key, V const &value, MapFindResult const &fr) { - MapIndex index = map__add_entry(h, key); - if (fr.entry_prev != MAP_SENTINEL) { - h->entries[fr.entry_prev].next = index; +gb_internal void map_set_internal_from_try_get(PtrMap *h, K key, V const &value, MapIndex found_index) { + if (found_index != MAP_SENTINEL) { + GB_ASSERT(h->entries[found_index].key == key); + h->entries[found_index].value = value; } else { - h->hashes[fr.hash_index] = index; + map_set(h, key, value); } - h->entries[index].value = value; } template @@ -269,116 +236,83 @@ gb_internal V &map_must_get(PtrMap *h, K key) { template gb_internal void map_set(PtrMap *h, K key, V const &value) { - MapIndex index; - MapFindResult fr; - if (h->hashes_count == 0) { - map_grow(h); - } - fr = map__find(h, key); - if (fr.entry_index != MAP_SENTINEL) { - index = fr.entry_index; - } else { - index = map__add_entry(h, key); - if (fr.entry_prev != MAP_SENTINEL) { - h->entries[fr.entry_prev].next = index; - } else { - h->hashes[fr.hash_index] = index; - } - } - h->entries[index].value = value; - - if (map__full(h)) { - map_grow(h); + GB_ASSERT(key != 0); + try_map_grow(h); + auto *found = map_get(h, key); + if (found) { + *found = value; + return; } + map__insert(h, key, value); } // returns true if it previously existed template gb_internal bool map_set_if_not_previously_exists(PtrMap *h, K key, V const &value) { - MapIndex index; - MapFindResult fr; - if (h->hashes_count == 0) { - map_grow(h); - } - fr = map__find(h, key); - if (fr.entry_index != MAP_SENTINEL) { + try_map_grow(h); + auto *found = map_get(h, key); + if (found) { return true; - } else { - index = map__add_entry(h, key); - if (fr.entry_prev != MAP_SENTINEL) { - h->entries[fr.entry_prev].next = index; - } else { - h->hashes[fr.hash_index] = index; - } - } - h->entries[index].value = value; - - if (map__full(h)) { - map_grow(h); } + map__insert(h, key, value); return false; } -template -gb_internal void map__erase(PtrMap *h, MapFindResult const &fr) { - MapFindResult last; - if (fr.entry_prev == MAP_SENTINEL) { - h->hashes[fr.hash_index] = h->entries[fr.entry_index].next; - } else { - h->entries[fr.entry_prev].next = h->entries[fr.entry_index].next; - } - if (fr.entry_index == h->count-1) { - h->count--; - return; - } - h->entries[fr.entry_index] = h->entries[h->count-1]; - h->count--; - - last = map__find(h, h->entries[fr.entry_index].key); - if (last.entry_prev != MAP_SENTINEL) { - h->entries[last.entry_prev].next = fr.entry_index; - } else { - h->hashes[last.hash_index] = fr.entry_index; - } -} - template gb_internal void map_remove(PtrMap *h, K key) { - MapFindResult fr = map__find(h, key); - if (fr.entry_index != MAP_SENTINEL) { - map__erase(h, fr); + MapIndex found_index = 0; + if (map_try_get(h, key, &found_index)) { + h->entries[found_index].key = cast(K)MAP_TOMBSTONE; + h->count -= 1; } } template gb_internal gb_inline void map_clear(PtrMap *h) { h->count = 0; - for (usize i = 0; i < h->hashes_count; i++) { - h->hashes[i] = MAP_SENTINEL; - } + gb_zero_array(h->entries, h->capacity); } #if PTR_MAP_ENABLE_MULTI_MAP template gb_internal PtrMapEntry *multi_map_find_first(PtrMap *h, K key) { - MapIndex i = map__find(h, key).entry_index; - if (i == MAP_SENTINEL) { + if (h->count == 0) { return nullptr; } - return &h->entries[i]; + u32 hash = ptr_map_hash_key(key); + u32 mask = (h->capacity-1); + u32 index = hash & mask; + u32 original_index = index; + do { + auto *entry = h->entries+index; + if (!entry->key) { + // NOTE(bill): no found, but there isn't any key removal for this hash map + return nullptr; + } else if (entry->key == key) { + return entry; + } + index = (index+1) & mask; + } while (original_index != index); + return nullptr; } template gb_internal PtrMapEntry *multi_map_find_next(PtrMap *h, PtrMapEntry *e) { - MapIndex i = e->next; - while (i != MAP_SENTINEL) { - if (h->entries[i].key == e->key) { - return &h->entries[i]; + u32 mask = h->capacity-1; + MapIndex index = cast(MapIndex)(e - h->entries); + MapIndex original_index = index; + do { + index = (index+1)&mask; + auto *entry = h->entries+index; + if (!entry->key) { + return nullptr; } - i = h->entries[i].next; - } + if (entry->key == e->key) { + return entry; + } + } while (original_index != index); return nullptr; } @@ -405,34 +339,16 @@ gb_internal void multi_map_get_all(PtrMap *h, K key, V *items) { template gb_internal void multi_map_insert(PtrMap *h, K key, V const &value) { - MapFindResult fr; - MapIndex i; - if (h->hashes_count == 0) { - map_grow(h); - } - // Make - fr = map__find(h, key); - i = map__add_entry(h, key); - if (fr.entry_prev == MAP_SENTINEL) { - h->hashes[fr.hash_index] = i; - } else { - h->entries[fr.entry_prev].next = i; - } - h->entries[i].next = fr.entry_index; - h->entries[i].value = value; - // Grow if needed - if (map__full(h)) { - map_grow(h); - } + try_map_grow(h); + map__insert(h, key, value); } -template -gb_internal void multi_map_remove(PtrMap *h, K key, PtrMapEntry *e) { - MapFindResult fr = map__find_from_entry(h, e); - if (fr.entry_index != MAP_SENTINEL) { - map__erase(h, fr); - } -} +// template +// gb_internal void multi_map_remove(PtrMap *h, K key, PtrMapEntry *e) { +// if (fr.entry_index != MAP_SENTINEL) { +// map__erase(h, fr); +// } +// } template gb_internal void multi_map_remove_all(PtrMap *h, K key) { @@ -443,22 +359,77 @@ gb_internal void multi_map_remove_all(PtrMap *h, K key) { #endif -template -gb_internal PtrMapEntry *begin(PtrMap &m) { - return m.entries; -} -template -gb_internal PtrMapEntry const *begin(PtrMap const &m) { - return m.entries; -} template -gb_internal PtrMapEntry *end(PtrMap &m) { - return m.entries + m.count; +struct PtrMapIterator { + PtrMap *map; + MapIndex index; + + PtrMapIterator &operator++() noexcept { + for (;;) { + ++index; + if (map->capacity == index) { + return *this; + } + PtrMapEntry *entry = map->entries+index; + if (entry->key && entry->key != cast(K)MAP_TOMBSTONE) { + return *this; + } + } + } + + bool operator==(PtrMapIterator const &other) const noexcept { + return this->map == other->map && this->index == other->index; + } + + operator PtrMapEntry *() const { + return map->entries+index; + } +}; + + +template +gb_internal PtrMapIterator end(PtrMap &m) noexcept { + return PtrMapIterator{&m, m.capacity}; } template -gb_internal PtrMapEntry const *end(PtrMap const &m) { - return m.entries + m.count; +gb_internal PtrMapIterator const end(PtrMap const &m) noexcept { + return PtrMapIterator{&m, m.capacity}; +} + + + +template +gb_internal PtrMapIterator begin(PtrMap &m) noexcept { + if (m.count == 0) { + return end(m); + } + + MapIndex index = 0; + while (index < m.capacity) { + auto key = m.entries[index].key; + if (key && key != cast(K)MAP_TOMBSTONE) { + break; + } + index++; + } + return PtrMapIterator{&m, index}; +} +template +gb_internal PtrMapIterator const begin(PtrMap const &m) noexcept { + if (m.count == 0) { + return end(m); + } + + MapIndex index = 0; + while (index < m.capacity) { + auto key = m.entries[index].key; + if (key && key != cast(K)MAP_TOMBSTONE) { + break; + } + index++; + } + return PtrMapIterator{&m, index}; } From 4bea5dbac1aea38d33b21e2c13297b0fc96c1be9 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Fri, 26 Apr 2024 15:09:08 +0100 Subject: [PATCH 17/53] Correct map usage --- src/checker.cpp | 2 +- src/llvm_backend_general.cpp | 2 +- src/llvm_backend_type.cpp | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/checker.cpp b/src/checker.cpp index 64fca0312..ee0ee5713 100644 --- a/src/checker.cpp +++ b/src/checker.cpp @@ -2192,7 +2192,7 @@ gb_internal void add_min_dep_type_info(Checker *c, Type *t) { // IMPORTANT NOTE(bill): this must be copied as `map_set` takes a const ref // and effectively assigns the `+1` of the value isize const count = set->count; - if (map_set_if_not_previously_exists(set, ti_index, count)) { + if (map_set_if_not_previously_exists(set, ti_index+1, count)) { // Type already exists; return; } diff --git a/src/llvm_backend_general.cpp b/src/llvm_backend_general.cpp index 02afa628c..15cbb7c71 100644 --- a/src/llvm_backend_general.cpp +++ b/src/llvm_backend_general.cpp @@ -140,7 +140,7 @@ gb_internal bool lb_init_generator(lbGenerator *gen, Checker *c) { } gen->default_module.gen = gen; - map_set(&gen->modules, cast(void *)nullptr, &gen->default_module); + map_set(&gen->modules, cast(void *)1, &gen->default_module); lb_init_module(&gen->default_module, c); diff --git a/src/llvm_backend_type.cpp b/src/llvm_backend_type.cpp index e202a59ba..2c4abbb4d 100644 --- a/src/llvm_backend_type.cpp +++ b/src/llvm_backend_type.cpp @@ -2,7 +2,7 @@ gb_internal isize lb_type_info_index(CheckerInfo *info, Type *type, bool err_on_ auto *set = &info->minimum_dependency_type_info_set; isize index = type_info_index(info, type, err_on_not_found); if (index >= 0) { - auto *found = map_get(set, index); + auto *found = map_get(set, index+1); if (found) { GB_ASSERT(*found >= 0); return *found + 1; From 92402a75f63fd75118fd364ab6e59e9ab438e75f Mon Sep 17 00:00:00 2001 From: Feoramund <161657516+Feoramund@users.noreply.github.com> Date: Fri, 26 Apr 2024 18:40:59 -0400 Subject: [PATCH 18/53] Fix wrong llvm-config in build script for FreeBSD --- build_odin.sh | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/build_odin.sh b/build_odin.sh index c53766290..aca98a36d 100755 --- a/build_odin.sh +++ b/build_odin.sh @@ -32,11 +32,11 @@ if [ -z "$LLVM_CONFIG" ]; then elif [ -n "$(command -v llvm-config-12)" ]; then LLVM_CONFIG="llvm-config-12" elif [ -n "$(command -v llvm-config-11)" ]; then LLVM_CONFIG="llvm-config-11" # freebsd - elif [ -n "$(command -v llvm-config17)" ]; then LLVM_CONFIG="llvm-config-17" - elif [ -n "$(command -v llvm-config14)" ]; then LLVM_CONFIG="llvm-config-14" - elif [ -n "$(command -v llvm-config13)" ]; then LLVM_CONFIG="llvm-config-13" - elif [ -n "$(command -v llvm-config12)" ]; then LLVM_CONFIG="llvm-config-12" - elif [ -n "$(command -v llvm-config11)" ]; then LLVM_CONFIG="llvm-config-11" + elif [ -n "$(command -v llvm-config17)" ]; then LLVM_CONFIG="llvm-config17" + elif [ -n "$(command -v llvm-config14)" ]; then LLVM_CONFIG="llvm-config14" + elif [ -n "$(command -v llvm-config13)" ]; then LLVM_CONFIG="llvm-config13" + elif [ -n "$(command -v llvm-config12)" ]; then LLVM_CONFIG="llvm-config12" + elif [ -n "$(command -v llvm-config11)" ]; then LLVM_CONFIG="llvm-config11" # fallback elif [ -n "$(command -v llvm-config)" ]; then LLVM_CONFIG="llvm-config" else From 652079476489b2539c77824ec2719847f28c352d Mon Sep 17 00:00:00 2001 From: gingerBill Date: Sat, 27 Apr 2024 08:50:05 +0100 Subject: [PATCH 19/53] Fix wrong allocator usage --- src/ptr_map.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ptr_map.cpp b/src/ptr_map.cpp index 8fd627768..362e412ba 100644 --- a/src/ptr_map.cpp +++ b/src/ptr_map.cpp @@ -147,7 +147,7 @@ gb_internal void map_reserve(PtrMap *h, isize cap) { PtrMap new_h = {}; new_h.count = 0; new_h.capacity = cast(u32)cap; - new_h.entries = gb_alloc_array(string_map_allocator(), EntryType, new_h.capacity); + new_h.entries = gb_alloc_array(map_allocator(), EntryType, new_h.capacity); if (h->count) { for (u32 i = 0; i < h->capacity; i++) { From c752d0b541f5cc92fe44f510a4db1e7e02469fd6 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Sat, 27 Apr 2024 09:16:18 +0100 Subject: [PATCH 20/53] Fix printing of big endian integers in a `bit_field` --- core/fmt/fmt.odin | 5 +++- core/reflect/types.odin | 61 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+), 1 deletion(-) diff --git a/core/fmt/fmt.odin b/core/fmt/fmt.odin index ba749d102..018c66bd3 100644 --- a/core/fmt/fmt.odin +++ b/core/fmt/fmt.odin @@ -2526,8 +2526,11 @@ fmt_bit_field :: proc(fi: ^Info, v: any, verb: rune, info: runtime.Type_Info_Bit bit_offset := info.bit_offsets[i] bit_size := info.bit_sizes[i] - value := read_bits(([^]byte)(v.data), bit_offset, bit_size) type := info.types[i] + value := read_bits(([^]byte)(v.data), bit_offset, bit_size) + if reflect.is_endian_big(type) { + value <<= u64(8*type.size) - u64(bit_size) + } if !reflect.is_unsigned(runtime.type_info_core(type)) { // Sign Extension diff --git a/core/reflect/types.odin b/core/reflect/types.odin index 9cff46a00..f242dfd5c 100644 --- a/core/reflect/types.odin +++ b/core/reflect/types.odin @@ -408,7 +408,68 @@ is_relative_multi_pointer :: proc(info: ^Type_Info) -> bool { } +@(require_results) +is_endian_platform :: proc(info: ^Type_Info) -> bool { + if info == nil { return false} + info := info + info = type_info_core(info) + #partial switch v in info.variant { + case Type_Info_Integer: + return v.endianness == .Platform + case Type_Info_Bit_Set: + if v.underlying != nil { + return is_endian_platform(v.underlying) + } + return true + case Type_Info_Pointer: + return true + } + return false +} +@(require_results) +is_endian_little :: proc(info: ^Type_Info) -> bool { + if info == nil { return false} + info := info + info = type_info_core(info) + #partial switch v in info.variant { + case Type_Info_Integer: + if v.endianness == .Platform { + return ODIN_ENDIAN == .Little + } + return v.endianness == .Little + case Type_Info_Bit_Set: + if v.underlying != nil { + return is_endian_platform(v.underlying) + } + return ODIN_ENDIAN == .Little + case Type_Info_Pointer: + return ODIN_ENDIAN == .Little + } + return ODIN_ENDIAN == .Little +} + +@(require_results) +is_endian_big :: proc(info: ^Type_Info) -> bool { + if info == nil { return false} + info := info + info = type_info_core(info) + #partial switch v in info.variant { + case Type_Info_Integer: + if v.endianness == .Platform { + return ODIN_ENDIAN == .Big + } + return v.endianness == .Big + case Type_Info_Bit_Set: + if v.underlying != nil { + return is_endian_platform(v.underlying) + } + return ODIN_ENDIAN == .Big + case Type_Info_Pointer: + return ODIN_ENDIAN == .Big + } + return ODIN_ENDIAN == .Big +} From 44548492521b3f03d39f46ccadec78160c014b2b Mon Sep 17 00:00:00 2001 From: gingerBill Date: Sat, 27 Apr 2024 09:16:36 +0100 Subject: [PATCH 21/53] Add attributes to procedures in text/scanner --- core/text/scanner/scanner.odin | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/core/text/scanner/scanner.odin b/core/text/scanner/scanner.odin index 7c17a0ec0..6d2465461 100644 --- a/core/text/scanner/scanner.odin +++ b/core/text/scanner/scanner.odin @@ -8,6 +8,7 @@ // A Scanner may be customized to recognize only a subset of those literals and to recognize different identifiers and white space characters. package text_scanner +import "base:runtime" import "core:fmt" import "core:strings" import "core:unicode" @@ -23,10 +24,12 @@ Position :: struct { } // position_is_valid reports where the position is valid +@(require_results) position_is_valid :: proc(pos: Position) -> bool { return pos.line > 0 } +@(require_results) position_to_string :: proc(pos: Position, allocator := context.temp_allocator) -> string { s := pos.filename if s == "" { @@ -140,7 +143,7 @@ init :: proc(s: ^Scanner, src: string, filename := "") -> ^Scanner { } -@(private) +@(private, require_results) advance :: proc(s: ^Scanner) -> rune { if s.src_pos >= len(s.src) { s.prev_char_len = 0 @@ -190,6 +193,7 @@ next :: proc(s: ^Scanner) -> rune { // peek returns the next Unicode character in the source without advancing the scanner // It returns EOF if the scanner's position is at least the last character of the source // if n > 0, it call next n times and return the nth Unicode character and then restore the Scanner's state +@(require_results) peek :: proc(s: ^Scanner, n := 0) -> (ch: rune) { if s.ch == -2 { s.ch = advance(s) @@ -211,6 +215,7 @@ peek :: proc(s: ^Scanner, n := 0) -> (ch: rune) { // peek returns the next token in the source // It returns EOF if the scanner's position is at least the last character of the source // if n > 0, it call next n times and return the nth token and then restore the Scanner's state +@(require_results) peek_token :: proc(s: ^Scanner, n := 0) -> (tok: rune) { assert(n >= 0) prev_s := s^ @@ -249,7 +254,7 @@ errorf :: proc(s: ^Scanner, format: string, args: ..any) { error(s, fmt.tprintf(format, ..args)) } -@(private) +@(private, require_results) is_ident_rune :: proc(s: ^Scanner, ch: rune, i: int) -> bool { if s.is_ident_rune != nil { return s.is_ident_rune(ch, i) @@ -257,7 +262,7 @@ is_ident_rune :: proc(s: ^Scanner, ch: rune, i: int) -> bool { return ch == '_' || unicode.is_letter(ch) || unicode.is_digit(ch) && i > 0 } -@(private) +@(private, require_results) scan_identifier :: proc(s: ^Scanner) -> rune { ch := advance(s) for i := 1; is_ident_rune(s, ch, i); i += 1 { @@ -266,13 +271,13 @@ scan_identifier :: proc(s: ^Scanner) -> rune { return ch } -@(private) lower :: proc(ch: rune) -> rune { return ('a' - 'A') | ch } -@(private) is_decimal :: proc(ch: rune) -> bool { return '0' <= ch && ch <= '9' } -@(private) is_hex :: proc(ch: rune) -> bool { return '0' <= ch && ch <= '9' || 'a' <= lower(ch) && lower(ch) <= 'f' } +@(private, require_results) lower :: proc(ch: rune) -> rune { return ('a' - 'A') | ch } +@(private, require_results) is_decimal :: proc(ch: rune) -> bool { return '0' <= ch && ch <= '9' } +@(private, require_results) is_hex :: proc(ch: rune) -> bool { return '0' <= ch && ch <= '9' || 'a' <= lower(ch) && lower(ch) <= 'f' } -@(private) +@(private, require_results) scan_number :: proc(s: ^Scanner, ch: rune, seen_dot: bool) -> (rune, rune) { lit_name :: proc(prefix: rune) -> string { switch prefix { @@ -417,7 +422,7 @@ scan_number :: proc(s: ^Scanner, ch: rune, seen_dot: bool) -> (rune, rune) { return tok, ch } -@(private) +@(private, require_results) scan_string :: proc(s: ^Scanner, quote: rune) -> (n: int) { digit_val :: proc(ch: rune) -> int { switch v := lower(ch); v { @@ -484,7 +489,7 @@ scan_char :: proc(s: ^Scanner) { } } -@(private) +@(private, require_results) scan_comment :: proc(s: ^Scanner, ch: rune) -> rune { ch := ch if ch == '/' { // line comment @@ -611,6 +616,7 @@ scan :: proc(s: ^Scanner) -> (tok: rune) { // position returns the position of the character immediately after the character or token returns by the previous call to next or scan // Use the Scanner's position field for the most recently scanned token position +@(require_results) position :: proc(s: ^Scanner) -> Position { pos: Position pos.filename = s.pos.filename @@ -630,6 +636,7 @@ position :: proc(s: ^Scanner) -> Position { } // token_text returns the string of the most recently scanned token +@(require_results) token_text :: proc(s: ^Scanner) -> string { if s.tok_pos < 0 { return "" @@ -639,7 +646,8 @@ token_text :: proc(s: ^Scanner) -> string { // token_string returns a printable string for a token or Unicode character // By default, it uses the context.temp_allocator to produce the string -token_string :: proc(tok: rune, allocator := context.temp_allocator) -> string { +@(require_results) +token_string :: proc(tok: rune, allocator: runtime.Allocator) -> string { context.allocator = allocator switch tok { case EOF: return strings.clone("EOF") From efae99971bd93875ed50aeb1854701959b8814bb Mon Sep 17 00:00:00 2001 From: gingerBill Date: Sat, 27 Apr 2024 09:19:50 +0100 Subject: [PATCH 22/53] Fix missing `_ =` --- core/text/scanner/scanner.odin | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/core/text/scanner/scanner.odin b/core/text/scanner/scanner.odin index 6d2465461..d27c66f24 100644 --- a/core/text/scanner/scanner.odin +++ b/core/text/scanner/scanner.odin @@ -568,13 +568,13 @@ scan :: proc(s: ^Scanner) -> (tok: rune) { break case '"': if .Scan_Strings in s.flags { - scan_string(s, '"') + _ = scan_string(s, '"') tok = String } ch = advance(s) case '\'': if .Scan_Chars in s.flags { - scan_string(s, '\'') + _ = scan_string(s, '\'') tok = Char } ch = advance(s) From 393e4a9db6d649387dd5c831134e75d98b780156 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Sat, 27 Apr 2024 09:53:02 +0100 Subject: [PATCH 23/53] Generalize Odin call-based "iterators" to work with more than 2-values: `for x, y, z, w in iterate(&it)` It has an artificial limitation of 100 values because if you need for than that, you're doing something wrong. --- src/check_stmt.cpp | 35 +++++++++++++++----------- src/llvm_backend_stmt.cpp | 53 ++++++++++++++++++++++++++++++++------- 2 files changed, 65 insertions(+), 23 deletions(-) diff --git a/src/check_stmt.cpp b/src/check_stmt.cpp index 971841165..c31056b33 100644 --- a/src/check_stmt.cpp +++ b/src/check_stmt.cpp @@ -1669,12 +1669,20 @@ gb_internal void check_range_stmt(CheckerContext *ctx, Ast *node, u32 mod_flags) case Type_Tuple: { isize count = t->Tuple.variables.count; - if (count < 1 || count > 3) { + if (count < 1) { ERROR_BLOCK(); check_not_tuple(ctx, &operand); - error_line("\tMultiple return valued parameters in a range statement are limited to a maximum of 2 usable values with a trailing boolean for the conditional\n"); + error_line("\tMultiple return valued parameters in a range statement are limited to a minimum of 1 usable values with a trailing boolean for the conditional, got %td\n", count); break; } + enum : isize {MAXIMUM_COUNT = 100}; + if (count > MAXIMUM_COUNT) { + ERROR_BLOCK(); + check_not_tuple(ctx, &operand); + error_line("\tMultiple return valued parameters in a range statement are limited to a maximum of %td usable values with a trailing boolean for the conditional, got %td\n", MAXIMUM_COUNT, count); + break; + } + Type *cond_type = t->Tuple.variables[count-1]->type; if (!is_type_boolean(cond_type)) { gbString s = type_to_string(cond_type); @@ -1683,24 +1691,23 @@ gb_internal void check_range_stmt(CheckerContext *ctx, Ast *node, u32 mod_flags) break; } + max_val_count = count; + for (Entity *e : t->Tuple.variables) { array_add(&vals, e->type); } is_possibly_addressable = false; - if (rs->vals.count > 1 && rs->vals[1] != nullptr && count < 3) { - gbString s = type_to_string(t); - error(operand.expr, "Expected a 3-valued expression on the rhs, got (%s)", s); - gb_string_free(s); - break; - } - - if (rs->vals.count > 0 && rs->vals[0] != nullptr && count < 2) { - gbString s = type_to_string(t); - error(operand.expr, "Expected at least a 2-valued expression on the rhs, got (%s)", s); - gb_string_free(s); - break; + bool do_break = false; + for (isize i = rs->vals.count-1; i >= 0; i--) { + if (rs->vals[i] != nullptr && count < i+2) { + gbString s = type_to_string(t); + error(operand.expr, "Expected a %td-valued expression on the rhs, got (%s)", i+2, s); + gb_string_free(s); + do_break = true; + break; + } } if (is_reverse) { diff --git a/src/llvm_backend_stmt.cpp b/src/llvm_backend_stmt.cpp index 24dd321f6..851433415 100644 --- a/src/llvm_backend_stmt.cpp +++ b/src/llvm_backend_stmt.cpp @@ -802,8 +802,19 @@ gb_internal void lb_build_range_enum(lbProcedure *p, Type *enum_type, Type *val_ if (done_) *done_ = done; } -gb_internal void lb_build_range_tuple(lbProcedure *p, Ast *expr, Type *val0_type, Type *val1_type, - lbValue *val0_, lbValue *val1_, lbBlock **loop_, lbBlock **done_) { +gb_internal void lb_build_range_tuple(lbProcedure *p, AstRangeStmt *rs, Scope *scope) { + Ast *expr = unparen_expr(rs->expr); + + Type *expr_type = type_of_expr(expr); + Type *et = base_type(type_deref(expr_type)); + GB_ASSERT(et->kind == Type_Tuple); + + i32 value_count = cast(i32)et->Tuple.variables.count; + + lbValue *values = gb_alloc_array(permanent_allocator(), lbValue, value_count); + + lb_open_scope(p, scope); + lbBlock *loop = lb_create_block(p, "for.tuple.loop"); lb_emit_jump(p, loop); lb_start_block(p, loop); @@ -821,11 +832,26 @@ gb_internal void lb_build_range_tuple(lbProcedure *p, Ast *expr, Type *val0_type lb_emit_if(p, cond, body, done); lb_start_block(p, body); + for (i32 i = 0; i < value_count; i++) { + values[i] = lb_emit_tuple_ev(p, tuple_value, i); + } - if (val0_) *val0_ = lb_emit_tuple_ev(p, tuple_value, 0); - if (val1_) *val1_ = lb_emit_tuple_ev(p, tuple_value, 1); - if (loop_) *loop_ = loop; - if (done_) *done_ = done; + GB_ASSERT(rs->vals.count <= value_count); + for (isize i = 0; i < rs->vals.count; i++) { + Ast *val = rs->vals[i]; + if (val != nullptr) { + lb_store_range_stmt_val(p, val, values[i]); + } + } + + lb_push_target_list(p, rs->label, done, loop, nullptr); + + lb_build_stmt(p, rs->body); + + lb_close_scope(p, lbDeferExit_Default, nullptr); + lb_pop_target_list(p); + lb_emit_jump(p, loop); + lb_start_block(p, done); } gb_internal void lb_build_range_stmt_struct_soa(lbProcedure *p, AstRangeStmt *rs, Scope *scope) { @@ -968,6 +994,17 @@ gb_internal void lb_build_range_stmt(lbProcedure *p, AstRangeStmt *rs, Scope *sc } } + TypeAndValue tav = type_and_value_of_expr(expr); + if (tav.mode != Addressing_Type) { + Type *expr_type = type_of_expr(expr); + Type *et = base_type(type_deref(expr_type)); + if (et->kind == Type_Tuple) { + lb_build_range_tuple(p, rs, scope); + return; + } + } + + lb_open_scope(p, scope); Ast *val0 = rs->vals.count > 0 ? lb_strip_and_prefix(rs->vals[0]) : nullptr; @@ -986,7 +1023,6 @@ gb_internal void lb_build_range_stmt(lbProcedure *p, AstRangeStmt *rs, Scope *sc lbBlock *loop = nullptr; lbBlock *done = nullptr; bool is_map = false; - TypeAndValue tav = type_and_value_of_expr(expr); if (tav.mode == Addressing_Type) { lb_build_range_enum(p, type_deref(tav.type), val0_type, &val, &key, &loop, &done); @@ -1062,8 +1098,7 @@ gb_internal void lb_build_range_stmt(lbProcedure *p, AstRangeStmt *rs, Scope *sc break; } case Type_Tuple: - lb_build_range_tuple(p, expr, val0_type, val1_type, &val, &key, &loop, &done); - break; + GB_PANIC("Should be handled already"); case Type_BitSet: { lbModule *m = p->module; From 309a770cbf49ef950a9c9e02ccfaced57f59e73a Mon Sep 17 00:00:00 2001 From: Feoramund <161657516+Feoramund@users.noreply.github.com> Date: Sat, 27 Apr 2024 05:24:59 -0400 Subject: [PATCH 24/53] Fix `omitempty` in `json.marshal` --- core/encoding/json/marshal.odin | 2 +- core/encoding/json/unmarshal.odin | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/core/encoding/json/marshal.odin b/core/encoding/json/marshal.odin index 04ef6d434..f45cdb1f1 100644 --- a/core/encoding/json/marshal.odin +++ b/core/encoding/json/marshal.odin @@ -420,7 +420,7 @@ marshal_to_writer :: proc(w: io.Writer, v: any, opt: ^Marshal_Options) -> (err: data := rawptr(uintptr(v.data) + info.offsets[i]) the_value := any{data, id} - if is_omitempty(the_value) { + if omitempty && is_omitempty(the_value) { continue } diff --git a/core/encoding/json/unmarshal.odin b/core/encoding/json/unmarshal.odin index 691303521..eb59e7838 100644 --- a/core/encoding/json/unmarshal.odin +++ b/core/encoding/json/unmarshal.odin @@ -348,7 +348,7 @@ json_name_from_tag_value :: proc(value: string) -> (json_name, extra: string) { json_name = value if comma_index := strings.index_byte(json_name, ','); comma_index >= 0 { json_name = json_name[:comma_index] - extra = json_name[comma_index:] + extra = value[1 + comma_index:] } return } From 5e1b376e22854ee4dd1d455d9db372d24f445ae4 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Sat, 27 Apr 2024 10:34:17 +0100 Subject: [PATCH 25/53] Disallow `for x in bitset_or_map` if `x` is a variable that matches the "key" --- src/check_stmt.cpp | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/check_stmt.cpp b/src/check_stmt.cpp index c31056b33..cccbab4f6 100644 --- a/src/check_stmt.cpp +++ b/src/check_stmt.cpp @@ -1629,6 +1629,17 @@ gb_internal void check_range_stmt(CheckerContext *ctx, Ast *node, u32 mod_flags) if (build_context.no_rtti && is_type_enum(t->BitSet.elem)) { error(node, "Iteration over a bit_set of an enum is not allowed runtime type information (RTTI) has been disallowed"); } + if (rs->vals.count == 1 && rs->vals[0] && rs->vals[0]->kind == Ast_Ident) { + String name = rs->vals[0]->Ident.token.string; + Entity *found = scope_lookup(ctx->scope, name); + if (found && are_types_identical(found->type, t->BitSet.elem)) { + ERROR_BLOCK(); + gbString s = expr_to_string(expr); + error(rs->vals[0], "'%.*s' shadows a previous declaration which might be ambiguous with 'for (%.*s in %s)'", LIT(name), LIT(name), s); + error_line("\tSuggestion: Use a different identifier if iteration is wanted, or surround in parentheses if a normal for loop is wanted\n"); + gb_string_free(s); + } + } break; case Type_EnumeratedArray: @@ -1664,6 +1675,17 @@ gb_internal void check_range_stmt(CheckerContext *ctx, Ast *node, u32 mod_flags) if (is_reverse) { error(node, "#reverse for is not supported for map types, as maps are unordered"); } + if (rs->vals.count == 1 && rs->vals[0] && rs->vals[0]->kind == Ast_Ident) { + String name = rs->vals[0]->Ident.token.string; + Entity *found = scope_lookup(ctx->scope, name); + if (found && are_types_identical(found->type, t->Map.key)) { + ERROR_BLOCK(); + gbString s = expr_to_string(expr); + error(rs->vals[0], "'%.*s' shadows a previous declaration which might be ambiguous with 'for (%.*s in %s)'", LIT(name), LIT(name), s); + error_line("\tSuggestion: Use a different identifier if iteration is wanted, or surround in parentheses if a normal for loop is wanted\n"); + gb_string_free(s); + } + } break; case Type_Tuple: From 1deb53cddb20b6383878975ffb23579bbdddb1b9 Mon Sep 17 00:00:00 2001 From: Yunky <126269099+DreepyYunky@users.noreply.github.com> Date: Sat, 27 Apr 2024 18:58:46 +0100 Subject: [PATCH 26/53] Add SetMenu --- core/sys/windows/user32.odin | 1 + 1 file changed, 1 insertion(+) diff --git a/core/sys/windows/user32.odin b/core/sys/windows/user32.odin index a589c3ec9..cb4091058 100644 --- a/core/sys/windows/user32.odin +++ b/core/sys/windows/user32.odin @@ -126,6 +126,7 @@ foreign user32 { CreatePopupMenu :: proc() -> HMENU --- DestroyMenu :: proc(hMenu: HMENU) -> BOOL --- AppendMenuW :: proc(hMenu: HMENU, uFlags: UINT, uIDNewItem: UINT_PTR, lpNewItem: LPCWSTR) -> BOOL --- + SetMenu :: proc(hWnd: HWND, hMenu: HMENU) -> BOOL --- TrackPopupMenu :: proc(hMenu: HMENU, uFlags: UINT, x: int, y: int, nReserved: int, hWnd: HWND, prcRect: ^RECT) -> i32 --- RegisterWindowMessageW :: proc(lpString: LPCWSTR) -> UINT --- From 00b1a41540306a0dc292e179ad780130c02f9f44 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Sun, 28 Apr 2024 11:31:50 +0100 Subject: [PATCH 27/53] Add stack frame procedures for `core:sys/windows` --- core/sys/windows/dbghelp.odin | 38 ++++++++++++++++++++++++++++++++++ core/sys/windows/kernel32.odin | 2 ++ core/sys/windows/types.odin | 4 ++++ 3 files changed, 44 insertions(+) diff --git a/core/sys/windows/dbghelp.odin b/core/sys/windows/dbghelp.odin index c2e506748..cb5458248 100644 --- a/core/sys/windows/dbghelp.odin +++ b/core/sys/windows/dbghelp.odin @@ -228,6 +228,38 @@ MINIDUMP_TYPE :: enum u32 { ValidTypeFlags = 0x01ffffff, } + +SYMBOL_INFOW :: struct { + SizeOfStruct: ULONG, + TypeIndex: ULONG, + Reserved: [2]ULONG64, + Index: ULONG, + Size: ULONG, + ModBase: ULONG64, + Flags: ULONG, + Value: ULONG64, + Address: ULONG64, + Register: ULONG, + Scope: ULONG, + Tag: ULONG, + NameLen: ULONG, + MaxNameLen: ULONG, + Name: [1]WCHAR, +} + +IMAGEHLP_LINE64 :: struct { + SizeOfStruct: DWORD, + Key: PVOID, + LineNumber: DWORD, + FileName: PWSTR, + Address: DWORD64, +} + +PSYMBOL_INFOW :: ^SYMBOL_INFOW +PIMAGEHLP_LINEW64 :: ^IMAGEHLP_LINE64 + +SYMOPT_LOAD_LINES :: 0x00000010 + @(default_calling_convention = "system") foreign Dbghelp { MiniDumpWriteDump :: proc( @@ -247,4 +279,10 @@ foreign Dbghelp { StreamPointer: ^PVOID, StreamSize: ^ULONG, ) -> BOOL --- + + SymInitialize :: proc(hProcess: HANDLE, UserSearchPath: PCSTR, fInvadeProcess: BOOL) -> BOOL --- + SymCleanup :: proc(hProcess: HANDLE) -> BOOL --- + SymSetOptions :: proc(SymOptions: DWORD) -> DWORD --- + SymFromAddrW :: proc(hProcess: HANDLE, Address: DWORD64, Displacement: PDWORD64, Symbol: PSYMBOL_INFOW) -> BOOL --- + SymGetLineFromAddrW64 :: proc(hProcess: HANDLE, dwAddr: DWORD64, pdwDisplacement: PDWORD, Line: PIMAGEHLP_LINEW64) -> BOOL --- } diff --git a/core/sys/windows/kernel32.odin b/core/sys/windows/kernel32.odin index 10cc80041..5409c58f5 100644 --- a/core/sys/windows/kernel32.odin +++ b/core/sys/windows/kernel32.odin @@ -460,6 +460,8 @@ foreign kernel32 { PostQueuedCompletionStatus :: proc(CompletionPort: HANDLE, dwNumberOfBytesTransferred: DWORD, dwCompletionKey: c_ulong, lpOverlapped: ^OVERLAPPED) -> BOOL --- // [MS-Docs](https://learn.microsoft.com/en-us/windows/win32/api/handleapi/nf-handleapi-gethandleinformation) GetHandleInformation :: proc(hObject: HANDLE, lpdwFlags: ^DWORD) -> BOOL --- + + RtlCaptureStackBackTrace :: proc(FramesToSkip: ULONG, FramesToCapture: ULONG, BackTrace: [^]PVOID, BackTraceHash: PULONG) -> USHORT ---; } DEBUG_PROCESS :: 0x00000001 diff --git a/core/sys/windows/types.odin b/core/sys/windows/types.odin index 4b54f0ed1..11d2774d6 100644 --- a/core/sys/windows/types.odin +++ b/core/sys/windows/types.odin @@ -93,10 +93,14 @@ LONG32 :: i32 ULONG64 :: u64 LONG64 :: i64 +DWORD64 :: u64 +PDWORD64 :: ^DWORD64 + PDWORD_PTR :: ^DWORD_PTR ATOM :: distinct WORD wstring :: [^]WCHAR +PWSTR :: [^]WCHAR PBYTE :: ^BYTE LPBYTE :: ^BYTE From e71cf96bbc809c1c1fb7172cd9e3c7c259520625 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Sun, 28 Apr 2024 11:35:51 +0100 Subject: [PATCH 28/53] Keep -vet happy --- core/sys/windows/kernel32.odin | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/sys/windows/kernel32.odin b/core/sys/windows/kernel32.odin index 5409c58f5..f998f33da 100644 --- a/core/sys/windows/kernel32.odin +++ b/core/sys/windows/kernel32.odin @@ -461,7 +461,7 @@ foreign kernel32 { // [MS-Docs](https://learn.microsoft.com/en-us/windows/win32/api/handleapi/nf-handleapi-gethandleinformation) GetHandleInformation :: proc(hObject: HANDLE, lpdwFlags: ^DWORD) -> BOOL --- - RtlCaptureStackBackTrace :: proc(FramesToSkip: ULONG, FramesToCapture: ULONG, BackTrace: [^]PVOID, BackTraceHash: PULONG) -> USHORT ---; + RtlCaptureStackBackTrace :: proc(FramesToSkip: ULONG, FramesToCapture: ULONG, BackTrace: [^]PVOID, BackTraceHash: PULONG) -> USHORT --- } DEBUG_PROCESS :: 0x00000001 From ebfbe4d26031bde4e91ba489102d60fe6959173f Mon Sep 17 00:00:00 2001 From: Feoramund <161657516+Feoramund@users.noreply.github.com> Date: Sun, 28 Apr 2024 06:38:32 -0400 Subject: [PATCH 29/53] Clear unused `global_error_collector.curr_error` This should cleanly prevent acknowledging duplicate errors on the same position as seems to be the intent based on the prior `else if` condition. --- src/error.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/error.cpp b/src/error.cpp index 7fb62c966..bbbb98053 100644 --- a/src/error.cpp +++ b/src/error.cpp @@ -403,6 +403,8 @@ gb_internal void error_va(TokenPos const &pos, TokenPos end, char const *fmt, va error_out("\n"); show_error_on_line(pos, end); } else { + global_error_collector.curr_error_value = {}; + global_error_collector.curr_error_value_set.store(false); global_error_collector.count.fetch_sub(1); } try_pop_error_value(); From 362aa82f59557743c81d9eb2853e972541ba7690 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Sun, 28 Apr 2024 11:58:16 +0100 Subject: [PATCH 30/53] Begin work on `core:debug/trace` --- core/debug/trace/trace.odin | 44 ++++++++++++++++++ core/debug/trace/trace_windows.odin | 70 +++++++++++++++++++++++++++++ 2 files changed, 114 insertions(+) create mode 100644 core/debug/trace/trace.odin create mode 100644 core/debug/trace/trace_windows.odin diff --git a/core/debug/trace/trace.odin b/core/debug/trace/trace.odin new file mode 100644 index 000000000..6ca9ed2c3 --- /dev/null +++ b/core/debug/trace/trace.odin @@ -0,0 +1,44 @@ +package debug_trace + +import "base:intrinsics" +import "base:runtime" + +Frame :: distinct uintptr +MAX_FRAMES :: 64 + +Frame_Location :: runtime.Source_Code_Location + +delete_frame_location :: proc(loc: Frame_Location, allocator: runtime.Allocator) -> runtime.Allocator_Error { + delete(loc.procedure, allocator) or_return + delete(loc.file_path, allocator) or_return + return nil +} + +Context :: struct { + in_resolve: bool, // atomic + impl: _Context, +} + +init :: proc(ctx: ^Context) -> bool { + return _init(ctx) +} + +destroy :: proc(ctx: ^Context) -> bool { + return _destroy(ctx) +} + +@(require_results) +frames :: proc(ctx: ^Context, skip: uint, allocator: runtime.Allocator) -> []Frame { + return _frames(ctx, skip, allocator) +} + +@(require_results) +resolve :: proc(ctx: ^Context, frame: Frame, allocator: runtime.Allocator) -> (result: Frame_Location) { + return _resolve(ctx, frame, allocator) +} + + +@(require_results) +in_resolve :: proc "contextless" (ctx: ^Context) -> bool { + return intrinsics.atomic_load(&ctx.in_resolve) +} \ No newline at end of file diff --git a/core/debug/trace/trace_windows.odin b/core/debug/trace/trace_windows.odin new file mode 100644 index 000000000..3ad5d7ed0 --- /dev/null +++ b/core/debug/trace/trace_windows.odin @@ -0,0 +1,70 @@ +//+private +//+build windows +package debug_trace + +import "base:intrinsics" +import "base:runtime" + +import win32 "core:sys/windows" +import "core:fmt" + +_Context :: struct { + hProcess: win32.HANDLE, + lock: win32.SRWLOCK, +} + +_init :: proc "contextless" (ctx: ^Context) -> (ok: bool) { + defer if !ok { _destroy(ctx) } + ctx.impl.hProcess = win32.GetCurrentProcess() + win32.SymInitialize(ctx.impl.hProcess, nil, true) or_return + win32.SymSetOptions(win32.SYMOPT_LOAD_LINES) + return true +} + +_destroy :: proc "contextless" (ctx: ^Context) -> bool { + if ctx != nil { + win32.SymCleanup(ctx.impl.hProcess) + } + return true +} + +_frames :: proc(ctx: ^Context, skip: uint, allocator: runtime.Allocator) -> []Frame { + buffer: [MAX_FRAMES]rawptr + frame_count := win32.RtlCaptureStackBackTrace(u32(skip) + 2, len(buffer), &buffer[0], nil) + frames := make([]Frame, frame_count, allocator) + for &f, i in frames { + // NOTE: Return address is one after the call instruction so subtract a byte to + // end up back inside the call instruction which is needed for SymFromAddr. + f = Frame(buffer[i]) - 1 + } + return frames +} + + +_resolve :: proc(ctx: ^Context, frame: Frame, allocator: runtime.Allocator) -> (result: runtime.Source_Code_Location) { + intrinsics.atomic_store(&ctx.in_resolve, true) + defer intrinsics.atomic_store(&ctx.in_resolve, false) + + // NOTE(bill): Dbghelp is not thread-safe + win32.AcquireSRWLockExclusive(&ctx.impl.lock) + defer win32.ReleaseSRWLockExclusive(&ctx.impl.lock) + + data: [size_of(win32.SYMBOL_INFOW) + size_of([256]win32.WCHAR)]byte + symbol := (^win32.SYMBOL_INFOW)(&data[0]) + symbol.SizeOfStruct = size_of(symbol) + symbol.MaxNameLen = 255 + if win32.SymFromAddrW(ctx.impl.hProcess, win32.DWORD64(frame), &{}, symbol) { + result.procedure, _ = win32.wstring_to_utf8(&symbol.Name[0], -1, allocator) + } else { + result.procedure = fmt.aprintf("(procedure: 0x%x)", frame, allocator=allocator) + } + + line: win32.IMAGEHLP_LINE64 + line.SizeOfStruct = size_of(line) + if win32.SymGetLineFromAddrW64(ctx.impl.hProcess, win32.DWORD64(frame), &{}, &line) { + result.file_path, _ = win32.wstring_to_utf8(line.FileName, -1, allocator) + result.line = i32(line.LineNumber) + } + + return result +} \ No newline at end of file From 29987c20c058fe269ea0961205d12f51fc3e8326 Mon Sep 17 00:00:00 2001 From: Feoramund <161657516+Feoramund@users.noreply.github.com> Date: Sun, 28 Apr 2024 06:59:55 -0400 Subject: [PATCH 31/53] Fix invalid rune literal reported twice The tokenizer and the parser were reporting it in different positions. This way, they'll report in the same spot. --- src/tokenizer.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/tokenizer.cpp b/src/tokenizer.cpp index fdff9224a..f7751d840 100644 --- a/src/tokenizer.cpp +++ b/src/tokenizer.cpp @@ -767,9 +767,8 @@ gb_internal void tokenizer_get_token(Tokenizer *t, Token *token, int repeat=0) { } } - // TODO(bill): Better Error Handling if (valid && n != 1) { - tokenizer_err(t, "Invalid rune literal"); + tokenizer_err(t, token->pos, "Invalid rune literal"); } token->string.len = t->curr - token->string.text; goto semicolon_check; From 44c9b988bbc54203307d50418c79188aee53c08c Mon Sep 17 00:00:00 2001 From: gingerBill Date: Sun, 28 Apr 2024 12:05:15 +0100 Subject: [PATCH 32/53] Add default debug/trace to do nothing --- core/debug/trace/trace_nil.odin | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 core/debug/trace/trace_nil.odin diff --git a/core/debug/trace/trace_nil.odin b/core/debug/trace/trace_nil.odin new file mode 100644 index 000000000..ab08aee04 --- /dev/null +++ b/core/debug/trace/trace_nil.odin @@ -0,0 +1,18 @@ +//+build !windows +package debug_trace + +_Context :: struct { +} + +_init :: proc(ctx: ^Context) -> (ok: bool) { + return true +} +_destroy :: proc(ctx: ^Context) -> bool { + return true +} +_frames :: proc(ctx: ^Context, skip: uint, allocator: runtime.Allocator) -> []Frame { + return nil +} +_resolve :: proc(ctx: ^Context, frame: Frame, allocator: runtime.Allocator) -> (result: runtime.Source_Code_Location) { + return +} From 6c185a5dca739b4aab034449f0627925e0137173 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Sun, 28 Apr 2024 12:43:27 +0100 Subject: [PATCH 33/53] Add `core:debug/trace` for Linux --- core/debug/trace/trace_linux.odin | 191 ++++++++++++++++++++++++++++++ core/debug/trace/trace_nil.odin | 2 +- 2 files changed, 192 insertions(+), 1 deletion(-) create mode 100644 core/debug/trace/trace_linux.odin diff --git a/core/debug/trace/trace_linux.odin b/core/debug/trace/trace_linux.odin new file mode 100644 index 000000000..529a83470 --- /dev/null +++ b/core/debug/trace/trace_linux.odin @@ -0,0 +1,191 @@ +//+private file +//+build linux +package debug_trace + +import "base:runtime" +import "core:strings" +import "core:fmt" +import "core:c" + +// NOTE: Relies on C++23 which adds and becomes ABI and that can be used +foreign import stdcpplibbacktrace "system:stdc++_libbacktrace" + +foreign import libdl "system:dl" + +backtrace_state :: struct {} +backtrace_error_callback :: proc "c" (data: rawptr, msg: cstring, errnum: c.int) +backtrace_simple_callback :: proc "c" (data: rawptr, pc: uintptr) -> c.int +backtrace_full_callback :: proc "c" (data: rawptr, pc: uintptr, filename: cstring, lineno: c.int, function: cstring) -> c.int +backtrace_syminfo_callback :: proc "c" (data: rawptr, pc: uintptr, symname: cstring, symval: uintptr, symsize: uintptr) + +@(default_calling_convention="c", link_prefix="__glibcxx_") +foreign stdcpplibbacktrace { + backtrace_create_state :: proc( + filename: cstring, + threaded: c.int, + error_callback: backtrace_error_callback, + data: rawptr, + ) -> ^backtrace_state --- + backtrace_simple :: proc( + state: ^backtrace_state, + skip: c.int, + callback: backtrace_simple_callback, + error_callback: backtrace_error_callback, + data: rawptr, + ) -> c.int --- + backtrace_pcinfo :: proc( + state: ^backtrace_state, + pc: uintptr, + callback: backtrace_full_callback, + error_callback: backtrace_error_callback, + data: rawptr, + ) -> c.int --- + backtrace_syminfo :: proc( + state: ^backtrace_state, + addr: uintptr, + callback: backtrace_syminfo_callback, + error_callback: backtrace_error_callback, + data: rawptr, + ) -> c.int --- + + // NOTE(bill): this is technically an internal procedure, but it is exposed + backtrace_free :: proc( + state: ^backtrace_state, + p: rawptr, + size: c.size_t, // unused + error_callback: backtrace_error_callback, // unused + data: rawptr, // unused + ) --- +} + +Dl_info :: struct { + dli_fname: cstring, + dli_fbase: rawptr, + dli_sname: cstring, + dli_saddr: rawptr, +} + +@(default_calling_convention="c") +foreign libdl { + dladdr :: proc(addr: rawptr, info: ^Dl_info) -> c.int --- +} + +@(private="package") +_Context :: struct { + state: ^backtrace_state, +} + +@(private="package") +_init :: proc(ctx: ^Context) -> (ok: bool) { + defer if !ok do destroy(ctx) + + ctx.impl.state = backtrace_create_state("odin-debug-trace", 1, nil, ctx) + return ctx.impl.state != nil +} + +@(private="package") +_destroy :: proc(ctx: ^Context) -> bool { + if ctx != nil { + backtrace_free(ctx.impl.state, nil, 0, nil, nil) + } + return true +} + +@(private="package") +_frames :: proc(ctx: ^Context, skip: uint, allocator: runtime.Allocator) -> (frames: []Frame) { + Backtrace_Context :: struct { + ctx: ^Context, + rt_ctx: runtime.Context, + frames: [MAX_FRAMES]Frame, + frame_count: int, + } + + btc := &Backtrace_Context{ + ctx = ctx, + rt_ctx = context, + } + backtrace_simple( + ctx.impl.state, + c.int(skip + 2), + proc "c" (user: rawptr, address: uintptr) -> c.int { + btc := (^Backtrace_Context)(user) + address := Frame(address) + if address == 0 { + return 1 + } + btc.frames[btc.frame_count] = address + btc.frame_count += 1 + return 0 + }, + nil, + btc, + ) + + res := btc.frames[:btc.frame_count] + if len(res) > 0 { + frames = make([]Frame, btc.frame_count, allocator) + copy(frames, res) + } + return +} + +@(private="package") +_resolve :: proc(ctx: ^Context, frame: Frame, allocator: runtime.Allocator) -> Frame_Location { + Backtrace_Context :: struct { + rt_ctx: runtime.Context, + allocator: runtime.Allocator, + frame: Frame_Location, + } + + btc := &Backtrace_Context{ + rt_ctx = context, + allocator = allocator, + } + done := backtrace_pcinfo( + ctx.impl.state, + uintptr(frame), + proc "c" (data: rawptr, address: uintptr, file: cstring, line: c.int, symbol: cstring) -> c.int { + btc := (^Backtrace_Context)(data) + context = btc.rt_ctx + + frame := &btc.frame + + if file != nil { + frame.file_path = strings.clone_from_cstring(file, btc.allocator) + } else if info: Dl_info; dladdr(rawptr(address), &info) != 0 && info.dli_fname != "" { + frame.file_path = strings.clone_from_cstring(info.dli_fname, btc.allocator) + } + if symbol != nil { + frame.procedure = strings.clone_from_cstring(symbol, btc.allocator) + } else if info: Dl_info; dladdr(rawptr(address), &info) != 0 && info.dli_sname != "" { + frame.procedure = strings.clone_from_cstring(info.dli_sname, btc.allocator) + } else { + frame.procedure = fmt.aprintf("(procedure: 0x%x)", allocator=btc.allocator) + } + frame.line = i32(line) + return 0 + }, + nil, + btc, + ) + if done != 0 { + return btc.frame + } + + // NOTE(bill): pcinfo cannot resolve, but it might be possible to get the procedure name at least + backtrace_syminfo( + ctx.impl.state, + uintptr(frame), + proc "c" (data: rawptr, address: uintptr, symbol: cstring, _ignore0, _ignore1: uintptr) { + if symbol != nil { + btc := (^Backtrace_Context)(data) + context = btc.rt_ctx + btc.frame.procedure = strings.clone_from_cstring(symbol, btc.allocator) + } + }, + nil, + btc, + ) + + return btc.frame +} \ No newline at end of file diff --git a/core/debug/trace/trace_nil.odin b/core/debug/trace/trace_nil.odin index ab08aee04..926bd18cc 100644 --- a/core/debug/trace/trace_nil.odin +++ b/core/debug/trace/trace_nil.odin @@ -1,4 +1,4 @@ -//+build !windows +//+build !windows !linux package debug_trace _Context :: struct { From 0fa269811a6001fb21c8c9f074005f7c82584d82 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Sun, 28 Apr 2024 12:49:17 +0100 Subject: [PATCH 34/53] Change layout of `Frame_Location` --- core/debug/trace/trace.odin | 12 ++++++++---- core/debug/trace/trace_windows.odin | 12 ++++++------ 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/core/debug/trace/trace.odin b/core/debug/trace/trace.odin index 6ca9ed2c3..eef27499d 100644 --- a/core/debug/trace/trace.odin +++ b/core/debug/trace/trace.odin @@ -6,11 +6,15 @@ import "base:runtime" Frame :: distinct uintptr MAX_FRAMES :: 64 -Frame_Location :: runtime.Source_Code_Location +Frame_Location :: struct { + using loc: runtime.Source_Code_Location, + allocator: runtime.Allocator, +} -delete_frame_location :: proc(loc: Frame_Location, allocator: runtime.Allocator) -> runtime.Allocator_Error { - delete(loc.procedure, allocator) or_return - delete(loc.file_path, allocator) or_return +delete_frame_location :: proc(fl: Frame_Location) -> runtime.Allocator_Error { + allocator := fl.allocator + delete(fl.loc.procedure, allocator) or_return + delete(fl.loc.file_path, allocator) or_return return nil } diff --git a/core/debug/trace/trace_windows.odin b/core/debug/trace/trace_windows.odin index 3ad5d7ed0..c7b4eeaa1 100644 --- a/core/debug/trace/trace_windows.odin +++ b/core/debug/trace/trace_windows.odin @@ -41,7 +41,7 @@ _frames :: proc(ctx: ^Context, skip: uint, allocator: runtime.Allocator) -> []Fr } -_resolve :: proc(ctx: ^Context, frame: Frame, allocator: runtime.Allocator) -> (result: runtime.Source_Code_Location) { +_resolve :: proc(ctx: ^Context, frame: Frame, allocator: runtime.Allocator) -> (fl: Frame_Location) { intrinsics.atomic_store(&ctx.in_resolve, true) defer intrinsics.atomic_store(&ctx.in_resolve, false) @@ -54,17 +54,17 @@ _resolve :: proc(ctx: ^Context, frame: Frame, allocator: runtime.Allocator) -> ( symbol.SizeOfStruct = size_of(symbol) symbol.MaxNameLen = 255 if win32.SymFromAddrW(ctx.impl.hProcess, win32.DWORD64(frame), &{}, symbol) { - result.procedure, _ = win32.wstring_to_utf8(&symbol.Name[0], -1, allocator) + fl.procedure, _ = win32.wstring_to_utf8(&symbol.Name[0], -1, allocator) } else { - result.procedure = fmt.aprintf("(procedure: 0x%x)", frame, allocator=allocator) + fl.procedure = fmt.aprintf("(procedure: 0x%x)", frame, allocator=allocator) } line: win32.IMAGEHLP_LINE64 line.SizeOfStruct = size_of(line) if win32.SymGetLineFromAddrW64(ctx.impl.hProcess, win32.DWORD64(frame), &{}, &line) { - result.file_path, _ = win32.wstring_to_utf8(line.FileName, -1, allocator) - result.line = i32(line.LineNumber) + fl.file_path, _ = win32.wstring_to_utf8(line.FileName, -1, allocator) + fl.line = i32(line.LineNumber) } - return result + return } \ No newline at end of file From 2eea06fc73ea7abc1432a2ad368d0fe032f5ba6f Mon Sep 17 00:00:00 2001 From: gingerBill Date: Sun, 28 Apr 2024 12:51:10 +0100 Subject: [PATCH 35/53] Set `in_resolve` for linux --- core/debug/trace/trace_linux.odin | 3 +++ 1 file changed, 3 insertions(+) diff --git a/core/debug/trace/trace_linux.odin b/core/debug/trace/trace_linux.odin index 529a83470..c65a165f9 100644 --- a/core/debug/trace/trace_linux.odin +++ b/core/debug/trace/trace_linux.odin @@ -131,6 +131,9 @@ _frames :: proc(ctx: ^Context, skip: uint, allocator: runtime.Allocator) -> (fra @(private="package") _resolve :: proc(ctx: ^Context, frame: Frame, allocator: runtime.Allocator) -> Frame_Location { + intrinsics.atomic_store(&ctx.in_resolve, true) + defer intrinsics.atomic_store(&ctx.in_resolve, false) + Backtrace_Context :: struct { rt_ctx: runtime.Context, allocator: runtime.Allocator, From 5ac8e8f9fd6d2e749edd7bf37c28d7439ed4e840 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Sun, 28 Apr 2024 12:52:02 +0100 Subject: [PATCH 36/53] Add doc.odin --- core/debug/trace/doc.odin | 50 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 core/debug/trace/doc.odin diff --git a/core/debug/trace/doc.odin b/core/debug/trace/doc.odin new file mode 100644 index 000000000..8c0778558 --- /dev/null +++ b/core/debug/trace/doc.odin @@ -0,0 +1,50 @@ +/* +A debug stack trace library. Only works when debug symbols are enabled `-debug`. + +Example: + import "base:runtime" + import "core:debug/trace" + + import "core:fmt" + + global_trace_ctx: trace.Context + + debug_trace_assertion_failure_proc :: proc(prefix, message: string, loc := #caller_location) -> ! { + runtime.print_caller_location(loc) + runtime.print_string(" ") + runtime.print_string(prefix) + if len(message) > 0 { + runtime.print_string(": ") + runtime.print_string(message) + } + runtime.print_byte('\n') + + ctx := &global_trace_ctx + if !trace.in_resolve(ctx) { + runtime.print_string("Debug Trace:\n") + frames := trace.frames(ctx, skip=1, allocator=context.temp_allocator) + for f, i in frames { + fl := trace.resolve(ctx, f, context.temp_allocator) + if fl.loc.file_path == "" && fl.loc.line == 0 { + continue + } + runtime.print_caller_location(fl.loc) + runtime.print_string(" - frame ") + runtime.print_int(i) + runtime.print_byte('\n') + } + } + runtime.trap() + } + + main :: proc() { + trace.init(&global_trace_ctx) + defer trace.destroy(&global_trace_ctx) + + context.assertion_failure_proc = debug_trace_assertion_failure_proc + + ... + } + +*/ +package debug_trace \ No newline at end of file From be09584ea5a61ac83c8ddc96eba1c7bccbe51472 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Sun, 28 Apr 2024 12:56:53 +0100 Subject: [PATCH 37/53] Increase `MAX_FRAMES` --- core/debug/trace/trace.odin | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/debug/trace/trace.odin b/core/debug/trace/trace.odin index eef27499d..243232ddc 100644 --- a/core/debug/trace/trace.odin +++ b/core/debug/trace/trace.odin @@ -4,7 +4,7 @@ import "base:intrinsics" import "base:runtime" Frame :: distinct uintptr -MAX_FRAMES :: 64 +MAX_FRAMES :: 512 Frame_Location :: struct { using loc: runtime.Source_Code_Location, From c0b7dd7da60c4b7557e494049937af630bc25c33 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Sun, 28 Apr 2024 13:05:19 +0100 Subject: [PATCH 38/53] Remove need for allocator and MAX_FRAMES in `trace.frames` --- core/debug/trace/trace.odin | 5 ++--- core/debug/trace/trace_linux.odin | 17 +++++++++-------- core/debug/trace/trace_windows.odin | 12 +++++------- 3 files changed, 16 insertions(+), 18 deletions(-) diff --git a/core/debug/trace/trace.odin b/core/debug/trace/trace.odin index 243232ddc..134609b05 100644 --- a/core/debug/trace/trace.odin +++ b/core/debug/trace/trace.odin @@ -4,7 +4,6 @@ import "base:intrinsics" import "base:runtime" Frame :: distinct uintptr -MAX_FRAMES :: 512 Frame_Location :: struct { using loc: runtime.Source_Code_Location, @@ -32,8 +31,8 @@ destroy :: proc(ctx: ^Context) -> bool { } @(require_results) -frames :: proc(ctx: ^Context, skip: uint, allocator: runtime.Allocator) -> []Frame { - return _frames(ctx, skip, allocator) +frames :: proc(ctx: ^Context, skip: uint, frames_buffer: []Frame) -> []Frame { + return _frames(ctx, skip, frames_buffer) } @(require_results) diff --git a/core/debug/trace/trace_linux.odin b/core/debug/trace/trace_linux.odin index c65a165f9..211c379ca 100644 --- a/core/debug/trace/trace_linux.odin +++ b/core/debug/trace/trace_linux.odin @@ -2,6 +2,7 @@ //+build linux package debug_trace +import "base:intrinsics" import "base:runtime" import "core:strings" import "core:fmt" @@ -92,17 +93,16 @@ _destroy :: proc(ctx: ^Context) -> bool { } @(private="package") -_frames :: proc(ctx: ^Context, skip: uint, allocator: runtime.Allocator) -> (frames: []Frame) { +_frames :: proc "contextless" (ctx: ^Context, skip: uint, frames_buffer: []Frame) -> (frames: []Frame) { Backtrace_Context :: struct { ctx: ^Context, - rt_ctx: runtime.Context, - frames: [MAX_FRAMES]Frame, + frames: []Frame, frame_count: int, } btc := &Backtrace_Context{ ctx = ctx, - rt_ctx = context, + frames = frames_buffer, } backtrace_simple( ctx.impl.state, @@ -113,6 +113,9 @@ _frames :: proc(ctx: ^Context, skip: uint, allocator: runtime.Allocator) -> (fra if address == 0 { return 1 } + if btc.frame_count == len(btc.frames) { + return 1 + } btc.frames[btc.frame_count] = address btc.frame_count += 1 return 0 @@ -121,10 +124,8 @@ _frames :: proc(ctx: ^Context, skip: uint, allocator: runtime.Allocator) -> (fra btc, ) - res := btc.frames[:btc.frame_count] - if len(res) > 0 { - frames = make([]Frame, btc.frame_count, allocator) - copy(frames, res) + if btc.frame_count > 0 { + frames = btc.frames[:btc.frame_count] } return } diff --git a/core/debug/trace/trace_windows.odin b/core/debug/trace/trace_windows.odin index c7b4eeaa1..5535c09f5 100644 --- a/core/debug/trace/trace_windows.odin +++ b/core/debug/trace/trace_windows.odin @@ -28,16 +28,14 @@ _destroy :: proc "contextless" (ctx: ^Context) -> bool { return true } -_frames :: proc(ctx: ^Context, skip: uint, allocator: runtime.Allocator) -> []Frame { - buffer: [MAX_FRAMES]rawptr - frame_count := win32.RtlCaptureStackBackTrace(u32(skip) + 2, len(buffer), &buffer[0], nil) - frames := make([]Frame, frame_count, allocator) - for &f, i in frames { +_frames :: proc "contextless" (ctx: ^Context, skip: uint, frames_buffer: []Frame) -> []Frame { + frame_count := win32.RtlCaptureStackBackTrace(u32(skip) + 2, len(frames_buffer), &frames_buffer[0], nil) + for i in 0.. Date: Sun, 28 Apr 2024 13:51:10 +0100 Subject: [PATCH 39/53] Update doc.odin --- core/debug/trace/doc.odin | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/core/debug/trace/doc.odin b/core/debug/trace/doc.odin index 8c0778558..e65548769 100644 --- a/core/debug/trace/doc.odin +++ b/core/debug/trace/doc.odin @@ -19,10 +19,11 @@ Example: } runtime.print_byte('\n') - ctx := &global_trace_ctx + ctx := &trace_ctx if !trace.in_resolve(ctx) { + buf: [64]trace.Frame runtime.print_string("Debug Trace:\n") - frames := trace.frames(ctx, skip=1, allocator=context.temp_allocator) + frames := trace.frames(ctx, 1, buf[:]) for f, i in frames { fl := trace.resolve(ctx, f, context.temp_allocator) if fl.loc.file_path == "" && fl.loc.line == 0 { From 74d75fb7fb9092e77eabb36cb71771c2c60ebb88 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Sun, 28 Apr 2024 13:51:46 +0100 Subject: [PATCH 40/53] Correct types on windows --- core/debug/trace/trace_windows.odin | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/debug/trace/trace_windows.odin b/core/debug/trace/trace_windows.odin index 5535c09f5..de1461e96 100644 --- a/core/debug/trace/trace_windows.odin +++ b/core/debug/trace/trace_windows.odin @@ -29,7 +29,7 @@ _destroy :: proc "contextless" (ctx: ^Context) -> bool { } _frames :: proc "contextless" (ctx: ^Context, skip: uint, frames_buffer: []Frame) -> []Frame { - frame_count := win32.RtlCaptureStackBackTrace(u32(skip) + 2, len(frames_buffer), &frames_buffer[0], nil) + frame_count := win32.RtlCaptureStackBackTrace(u32(skip) + 2, u32(len(frames_buffer)), ([^]rawptr)(&frames_buffer[0]), nil) for i in 0.. Date: Sun, 28 Apr 2024 13:52:52 +0100 Subject: [PATCH 41/53] Rename `trace_linux.odin` to `trace_cpp.odin` --- core/debug/trace/{trace_linux.odin => trace_cpp.odin} | 2 +- core/debug/trace/trace_nil.odin | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) rename core/debug/trace/{trace_linux.odin => trace_cpp.odin} (99%) diff --git a/core/debug/trace/trace_linux.odin b/core/debug/trace/trace_cpp.odin similarity index 99% rename from core/debug/trace/trace_linux.odin rename to core/debug/trace/trace_cpp.odin index 211c379ca..894046c45 100644 --- a/core/debug/trace/trace_linux.odin +++ b/core/debug/trace/trace_cpp.odin @@ -1,5 +1,5 @@ //+private file -//+build linux +//+build linux, darwin package debug_trace import "base:intrinsics" diff --git a/core/debug/trace/trace_nil.odin b/core/debug/trace/trace_nil.odin index 926bd18cc..40478898b 100644 --- a/core/debug/trace/trace_nil.odin +++ b/core/debug/trace/trace_nil.odin @@ -1,4 +1,4 @@ -//+build !windows !linux +//+build !windows !linux !darwin package debug_trace _Context :: struct { From 30cfdd73b0cf6df7bf04f0f17a91fd6450ba7f73 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Sun, 28 Apr 2024 14:45:59 +0100 Subject: [PATCH 42/53] Add extra asserts --- src/checker.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/checker.cpp b/src/checker.cpp index ee0ee5713..7e2d88982 100644 --- a/src/checker.cpp +++ b/src/checker.cpp @@ -2925,6 +2925,8 @@ gb_internal void init_core_type_info(Checker *c) { return; } Entity *type_info_entity = find_core_entity(c, str_lit("Type_Info")); + GB_ASSERT(type_info_entity != nullptr); + GB_ASSERT(type_info_entity->type != nullptr); t_type_info = type_info_entity->type; t_type_info_ptr = alloc_type_pointer(t_type_info); From 4fea5720a52b726671a1e6f8e9acfe0ee2569972 Mon Sep 17 00:00:00 2001 From: Laytan Laats Date: Sun, 28 Apr 2024 16:05:41 +0200 Subject: [PATCH 43/53] wasm: allow `-default-to-nil-allocator` --- src/build_settings.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/build_settings.cpp b/src/build_settings.cpp index 3bd362996..8509394ff 100644 --- a/src/build_settings.cpp +++ b/src/build_settings.cpp @@ -1583,8 +1583,10 @@ gb_internal void init_build_context(TargetMetrics *cross_target, Subtarget subta if (bc->metrics.os == TargetOs_js || bc->metrics.os == TargetOs_wasi) { // TODO(bill): Should these even have a default "heap-like" allocator? } - bc->ODIN_DEFAULT_TO_PANIC_ALLOCATOR = true; - bc->ODIN_DEFAULT_TO_NIL_ALLOCATOR = !bc->ODIN_DEFAULT_TO_PANIC_ALLOCATOR; + + if (!bc->ODIN_DEFAULT_TO_NIL_ALLOCATOR && !bc->ODIN_DEFAULT_TO_PANIC_ALLOCATOR) { + bc->ODIN_DEFAULT_TO_PANIC_ALLOCATOR = true; + } } } From 0530f86a48a191381e343e1b51a88b0ef2f0e1c9 Mon Sep 17 00:00:00 2001 From: Laytan Laats Date: Sun, 28 Apr 2024 16:09:03 +0200 Subject: [PATCH 44/53] fix: buddy allocator wrong query info pointer --- core/mem/allocators.odin | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/mem/allocators.odin b/core/mem/allocators.odin index eba79eacf..1d79e09c1 100644 --- a/core/mem/allocators.odin +++ b/core/mem/allocators.odin @@ -1124,7 +1124,7 @@ buddy_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode, case .Query_Info: info := (^Allocator_Query_Info)(old_memory) if info != nil && info.pointer != nil { - ptr := old_memory + ptr := info.pointer if !(b.head <= ptr && ptr <= b.tail) { return nil, .Invalid_Pointer } From cc5faecceda60ee6ccb1fd574c4fde8890245be0 Mon Sep 17 00:00:00 2001 From: Laytan Laats Date: Sun, 28 Apr 2024 16:10:04 +0200 Subject: [PATCH 45/53] wasm: add the `fprint` procedures to `fmt` This makes the `log` package work on wasm --- core/fmt/fmt_js.odin | 51 ++++++++++++++++++++++++++++++++++++++++++++ core/os/os_js.odin | 9 ++------ 2 files changed, 53 insertions(+), 7 deletions(-) diff --git a/core/fmt/fmt_js.odin b/core/fmt/fmt_js.odin index a0a890a9a..acf218eb5 100644 --- a/core/fmt/fmt_js.odin +++ b/core/fmt/fmt_js.odin @@ -1,7 +1,9 @@ //+build js package fmt +import "core:bufio" import "core:io" +import "core:os" foreign import "odin_env" @@ -31,6 +33,55 @@ stderr := io.Writer{ data = rawptr(uintptr(2)), } +@(private="file") +fd_to_writer :: proc(fd: os.Handle, loc := #caller_location) -> io.Writer { + switch fd { + case 1: return stdout + case 2: return stderr + case: panic("`fmt.fprint` variant called with invalid file descriptor for JS, only 1 (stdout) and 2 (stderr) are supported", loc) + } +} + +// fprint formats using the default print settings and writes to fd +fprint :: proc(fd: os.Handle, args: ..any, sep := " ", flush := true, loc := #caller_location) -> int { + buf: [1024]byte + b: bufio.Writer + defer bufio.writer_flush(&b) + + bufio.writer_init_with_buf(&b, fd_to_writer(fd, loc), buf[:]) + w := bufio.writer_to_writer(&b) + return wprint(w, ..args, sep=sep, flush=flush) +} + +// fprintln formats using the default print settings and writes to fd +fprintln :: proc(fd: os.Handle, args: ..any, sep := " ", flush := true, loc := #caller_location) -> int { + buf: [1024]byte + b: bufio.Writer + defer bufio.writer_flush(&b) + + bufio.writer_init_with_buf(&b, fd_to_writer(fd, loc), buf[:]) + + w := bufio.writer_to_writer(&b) + return wprintln(w, ..args, sep=sep, flush=flush) +} + +// fprintf formats according to the specified format string and writes to fd +fprintf :: proc(fd: os.Handle, fmt: string, args: ..any, flush := true, newline := false, loc := #caller_location) -> int { + buf: [1024]byte + b: bufio.Writer + defer bufio.writer_flush(&b) + + bufio.writer_init_with_buf(&b, fd_to_writer(fd, loc), buf[:]) + + w := bufio.writer_to_writer(&b) + return wprintf(w, fmt, ..args, flush=flush, newline=newline) +} + +// fprintfln formats according to the specified format string and writes to fd, followed by a newline. +fprintfln :: proc(fd: os.Handle, fmt: string, args: ..any, flush := true, loc := #caller_location) -> int { + return fprintf(fd, fmt, ..args, flush=flush, newline=true, loc=loc) +} + // print formats using the default print settings and writes to stdout print :: proc(args: ..any, sep := " ", flush := true) -> int { return wprint(w=stdout, args=args, sep=sep, flush=flush) } // println formats using the default print settings and writes to stdout diff --git a/core/os/os_js.odin b/core/os/os_js.odin index 910cb8155..56b830e36 100644 --- a/core/os/os_js.odin +++ b/core/os/os_js.odin @@ -64,13 +64,8 @@ write_at :: proc(fd: Handle, data: []byte, offset: i64) -> (n: int, err: Errno) unimplemented("core:os procedure not supported on JS target") } - - -// NOTE(bill): Uses startup to initialize it -//stdin := get_std_handle(uint(win32.STD_INPUT_HANDLE)) -//stdout := get_std_handle(uint(win32.STD_OUTPUT_HANDLE)) -//stderr := get_std_handle(uint(win32.STD_ERROR_HANDLE)) - +stdout: Handle = 1 +stderr: Handle = 2 get_std_handle :: proc "contextless" (h: uint) -> Handle { context = runtime.default_context() From f1c13d6bd8ad7390ba29d5d6de79596b9b53bf24 Mon Sep 17 00:00:00 2001 From: Feoramund <161657516+Feoramund@users.noreply.github.com> Date: Sun, 28 Apr 2024 14:03:11 -0400 Subject: [PATCH 46/53] Fix race condition in `error_va` If the error count exceeded `MAX_ERROR_COLLECTOR_COUNT`, multiple threads could print and exit simultaneously, causing a segfault. This change moves the mutex lock back before the conditional. --- src/error.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/error.cpp b/src/error.cpp index bbbb98053..1b091f88e 100644 --- a/src/error.cpp +++ b/src/error.cpp @@ -376,11 +376,11 @@ gb_internal void error_out_coloured(char const *str, TerminalStyle style, Termin gb_internal void error_va(TokenPos const &pos, TokenPos end, char const *fmt, va_list va) { global_error_collector.count.fetch_add(1); + mutex_lock(&global_error_collector.mutex); if (global_error_collector.count > MAX_ERROR_COLLECTOR_COUNT()) { print_all_errors(); gb_exit(1); } - mutex_lock(&global_error_collector.mutex); push_error_value(pos, ErrorValue_Error); // NOTE(bill): Duplicate error, skip it From a573161abde810bcad561f60079f69d938497ce3 Mon Sep 17 00:00:00 2001 From: Feoramund <161657516+Feoramund@users.noreply.github.com> Date: Sun, 28 Apr 2024 14:42:04 -0400 Subject: [PATCH 47/53] Allow `@(init)` procs to be `@(disabled)` --- src/checker.cpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/checker.cpp b/src/checker.cpp index 7e2d88982..70ca4fc47 100644 --- a/src/checker.cpp +++ b/src/checker.cpp @@ -2535,6 +2535,11 @@ gb_internal void generate_minimum_dependency_set_internal(Checker *c, Entity *st is_init = false; } + if ((e->flags & EntityFlag_Disabled) != 0) { + warning(e->token, "This @(init) procedure is disabled; you must call it manually"); + is_init = false; + } + if (is_init) { add_dependency_to_set(c, e); array_add(&c->info.init_procedures, e); From 1f5f41711609ade4df5383ae18cdb6723639e54f Mon Sep 17 00:00:00 2001 From: IllusionMan1212 Date: Sun, 28 Apr 2024 21:44:34 +0200 Subject: [PATCH 48/53] fix(linalg/glsl): incorrect quat by vector3 multiplication --- core/math/linalg/glsl/linalg_glsl.odin | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/core/math/linalg/glsl/linalg_glsl.odin b/core/math/linalg/glsl/linalg_glsl.odin index bda1f1723..363a95887 100644 --- a/core/math/linalg/glsl/linalg_glsl.odin +++ b/core/math/linalg/glsl/linalg_glsl.odin @@ -1724,7 +1724,7 @@ quatFromMat4 :: proc "c" (m: mat4) -> (q: quat) { @(require_results) quatMulVec3 :: proc "c" (q: quat, v: vec3) -> vec3 { xyz := vec3{q.x, q.y, q.z} - t := cross(xyz, v) + t := cross(2.0 * xyz, v) return v + q.w*t + cross(xyz, t) } @@ -1832,7 +1832,7 @@ dquatFromDmat4 :: proc "c" (m: dmat4) -> (q: dquat) { @(require_results) dquatMulDvec3 :: proc "c" (q: dquat, v: dvec3) -> dvec3 { xyz := dvec3{q.x, q.y, q.z} - t := cross(xyz, v) + t := cross(2.0 * xyz, v) return v + q.w*t + cross(xyz, t) } From c712de0cd07a6e7c5d34570228a2612326cc2acf Mon Sep 17 00:00:00 2001 From: Feoramund <161657516+Feoramund@users.noreply.github.com> Date: Sun, 28 Apr 2024 17:17:01 -0400 Subject: [PATCH 49/53] Require results for non-buffered `print` procs --- core/fmt/fmt.odin | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/core/fmt/fmt.odin b/core/fmt/fmt.odin index 018c66bd3..2209af6e2 100644 --- a/core/fmt/fmt.odin +++ b/core/fmt/fmt.odin @@ -120,6 +120,7 @@ register_user_formatter :: proc(id: typeid, formatter: User_Formatter) -> Regist // // Returns: A formatted string. // +@(require_results) aprint :: proc(args: ..any, sep := " ", allocator := context.allocator) -> string { str: strings.Builder strings.builder_init(&str, allocator) @@ -136,6 +137,7 @@ aprint :: proc(args: ..any, sep := " ", allocator := context.allocator) -> strin // // Returns: A formatted string with a newline character at the end. // +@(require_results) aprintln :: proc(args: ..any, sep := " ", allocator := context.allocator) -> string { str: strings.Builder strings.builder_init(&str, allocator) @@ -153,6 +155,7 @@ aprintln :: proc(args: ..any, sep := " ", allocator := context.allocator) -> str // // Returns: A formatted string. The returned string must be freed accordingly. // +@(require_results) aprintf :: proc(fmt: string, args: ..any, allocator := context.allocator, newline := false) -> string { str: strings.Builder strings.builder_init(&str, allocator) @@ -169,6 +172,7 @@ aprintf :: proc(fmt: string, args: ..any, allocator := context.allocator, newlin // // Returns: A formatted string. The returned string must be freed accordingly. // +@(require_results) aprintfln :: proc(fmt: string, args: ..any, allocator := context.allocator) -> string { return aprintf(fmt, ..args, allocator=allocator, newline=true) } @@ -182,6 +186,7 @@ aprintfln :: proc(fmt: string, args: ..any, allocator := context.allocator) -> s // // Returns: A formatted string. // +@(require_results) tprint :: proc(args: ..any, sep := " ") -> string { str: strings.Builder strings.builder_init(&str, context.temp_allocator) @@ -198,6 +203,7 @@ tprint :: proc(args: ..any, sep := " ") -> string { // // Returns: A formatted string with a newline character at the end. // +@(require_results) tprintln :: proc(args: ..any, sep := " ") -> string { str: strings.Builder strings.builder_init(&str, context.temp_allocator) @@ -215,6 +221,7 @@ tprintln :: proc(args: ..any, sep := " ") -> string { // // Returns: A formatted string. // +@(require_results) tprintf :: proc(fmt: string, args: ..any, newline := false) -> string { str: strings.Builder strings.builder_init(&str, context.temp_allocator) @@ -231,6 +238,7 @@ tprintf :: proc(fmt: string, args: ..any, newline := false) -> string { // // Returns: A formatted string. // +@(require_results) tprintfln :: proc(fmt: string, args: ..any) -> string { return tprintf(fmt, ..args, newline=true) } @@ -339,6 +347,7 @@ panicf :: proc(fmt: string, args: ..any, loc := #caller_location) -> ! { // // Returns: A formatted C string // +@(require_results) caprintf :: proc(format: string, args: ..any, newline := false) -> cstring { str: strings.Builder strings.builder_init(&str) @@ -357,6 +366,7 @@ caprintf :: proc(format: string, args: ..any, newline := false) -> cstring { // // Returns: A formatted C string // +@(require_results) caprintfln :: proc(format: string, args: ..any) -> cstring { return caprintf(format, ..args, newline=true) } @@ -371,6 +381,7 @@ caprintfln :: proc(format: string, args: ..any) -> cstring { // // Returns: A formatted C string // +@(require_results) ctprintf :: proc(format: string, args: ..any, newline := false) -> cstring { str: strings.Builder strings.builder_init(&str, context.temp_allocator) @@ -389,6 +400,7 @@ ctprintf :: proc(format: string, args: ..any, newline := false) -> cstring { // // Returns: A formatted C string // +@(require_results) ctprintfln :: proc(format: string, args: ..any) -> cstring { return ctprintf(format, ..args, newline=true) } From 700f9c94bd44dfcf77d7bef3bf7d0222c39e798e Mon Sep 17 00:00:00 2001 From: Feoramund <161657516+Feoramund@users.noreply.github.com> Date: Sun, 28 Apr 2024 17:18:46 -0400 Subject: [PATCH 50/53] Combine adjacent `sbprint*`/`to_string` calls The `sbprint*` procs already return a string conversion. --- core/fmt/fmt.odin | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/core/fmt/fmt.odin b/core/fmt/fmt.odin index 2209af6e2..867257491 100644 --- a/core/fmt/fmt.odin +++ b/core/fmt/fmt.odin @@ -124,8 +124,7 @@ register_user_formatter :: proc(id: typeid, formatter: User_Formatter) -> Regist aprint :: proc(args: ..any, sep := " ", allocator := context.allocator) -> string { str: strings.Builder strings.builder_init(&str, allocator) - sbprint(&str, ..args, sep=sep) - return strings.to_string(str) + return sbprint(&str, ..args, sep=sep) } // Creates a formatted string with a newline character at the end // @@ -141,8 +140,7 @@ aprint :: proc(args: ..any, sep := " ", allocator := context.allocator) -> strin aprintln :: proc(args: ..any, sep := " ", allocator := context.allocator) -> string { str: strings.Builder strings.builder_init(&str, allocator) - sbprintln(&str, ..args, sep=sep) - return strings.to_string(str) + return sbprintln(&str, ..args, sep=sep) } // Creates a formatted string using a format string and arguments // @@ -159,8 +157,7 @@ aprintln :: proc(args: ..any, sep := " ", allocator := context.allocator) -> str aprintf :: proc(fmt: string, args: ..any, allocator := context.allocator, newline := false) -> string { str: strings.Builder strings.builder_init(&str, allocator) - sbprintf(&str, fmt, ..args, newline=newline) - return strings.to_string(str) + return sbprintf(&str, fmt, ..args, newline=newline) } // Creates a formatted string using a format string and arguments, followed by a newline. // @@ -190,8 +187,7 @@ aprintfln :: proc(fmt: string, args: ..any, allocator := context.allocator) -> s tprint :: proc(args: ..any, sep := " ") -> string { str: strings.Builder strings.builder_init(&str, context.temp_allocator) - sbprint(&str, ..args, sep=sep) - return strings.to_string(str) + return sbprint(&str, ..args, sep=sep) } // Creates a formatted string with a newline character at the end // @@ -207,8 +203,7 @@ tprint :: proc(args: ..any, sep := " ") -> string { tprintln :: proc(args: ..any, sep := " ") -> string { str: strings.Builder strings.builder_init(&str, context.temp_allocator) - sbprintln(&str, ..args, sep=sep) - return strings.to_string(str) + return sbprintln(&str, ..args, sep=sep) } // Creates a formatted string using a format string and arguments // @@ -225,8 +220,7 @@ tprintln :: proc(args: ..any, sep := " ") -> string { tprintf :: proc(fmt: string, args: ..any, newline := false) -> string { str: strings.Builder strings.builder_init(&str, context.temp_allocator) - sbprintf(&str, fmt, ..args, newline=newline) - return strings.to_string(str) + return sbprintf(&str, fmt, ..args, newline=newline) } // Creates a formatted string using a format string and arguments, followed by a newline. // From bbebb4ad60996cea544507cf551f7b2a4fa86520 Mon Sep 17 00:00:00 2001 From: Feoramund <161657516+Feoramund@users.noreply.github.com> Date: Sun, 28 Apr 2024 17:20:52 -0400 Subject: [PATCH 51/53] Fix unseen `print` call in demo --- examples/demo/demo.odin | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/demo/demo.odin b/examples/demo/demo.odin index 1f6d337e8..a62c11310 100644 --- a/examples/demo/demo.odin +++ b/examples/demo/demo.odin @@ -2385,7 +2385,7 @@ matrix_type :: proc() { c := a * b #assert(type_of(c) == matrix[2, 2]f32) - fmt.tprintln("c = a * b", c) + fmt.println("c = a * b", c) } { // Matrices support multiplication between matrices and arrays From ae322739b58cdd48224b7a332753ab393cc40405 Mon Sep 17 00:00:00 2001 From: Damian Tarnawski Date: Mon, 29 Apr 2024 16:59:52 +0200 Subject: [PATCH 52/53] Remove instrinsics and utf16 imports from os/os_js --- core/os/os_js.odin | 2 -- 1 file changed, 2 deletions(-) diff --git a/core/os/os_js.odin b/core/os/os_js.odin index 56b830e36..8b61cb7ed 100644 --- a/core/os/os_js.odin +++ b/core/os/os_js.odin @@ -1,9 +1,7 @@ //+build js package os -import "base:intrinsics" import "base:runtime" -import "core:unicode/utf16" is_path_separator :: proc(c: byte) -> bool { return c == '/' || c == '\\' From 5c1201fa422639f0c70bb772b29f3cfb4c0e3d04 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Tue, 30 Apr 2024 09:10:00 +0100 Subject: [PATCH 53/53] Fix #3459 --- base/runtime/internal.odin | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/base/runtime/internal.odin b/base/runtime/internal.odin index 6f0445787..3e9c524bd 100644 --- a/base/runtime/internal.odin +++ b/base/runtime/internal.odin @@ -1043,8 +1043,8 @@ __write_bits :: proc "contextless" (dst, src: [^]byte, offset: uintptr, size: ui for i in 0..>3]) & (1<<(i&7)) != 0) - b := the_bit<<(j&7) - dst[j>>3] = (dst[j>>3] &~ b) | b + dst[j>>3] &~= 1<<(j&7) + dst[j>>3] |= the_bit<<(j&7) } } @@ -1052,7 +1052,7 @@ __read_bits :: proc "contextless" (dst, src: [^]byte, offset: uintptr, size: uin for j in 0..>3]) & (1<<(i&7)) != 0) - b := the_bit<<(j&7) - dst[j>>3] = (dst[j>>3] &~ b) | b + dst[j>>3] &~= 1<<(j&7) + dst[j>>3] |= the_bit<<(j&7) } } \ No newline at end of file