From f93f2dfd5cf56383e33f3eb7d2773c4646b37e2f Mon Sep 17 00:00:00 2001 From: Colin Davidson Date: Wed, 24 Jan 2024 13:24:25 -0800 Subject: [PATCH 01/40] Add support for basic EGL on Linux --- vendor/egl/egl.odin | 61 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 vendor/egl/egl.odin diff --git a/vendor/egl/egl.odin b/vendor/egl/egl.odin new file mode 100644 index 000000000..cf6a02b7a --- /dev/null +++ b/vendor/egl/egl.odin @@ -0,0 +1,61 @@ +//+build linux +package egl + +NativeDisplayType :: rawptr +NativeWindowType :: rawptr +Display :: rawptr +Surface :: rawptr +Config :: rawptr +Context :: rawptr + +NO_DISPLAY :: Display(uintptr(0)) +NO_CONTEXT :: Context(uintptr(0)) +NO_SURFACE :: Surface(uintptr(0)) + +CONTEXT_OPENGL_CORE_PROFILE_BIT :: 0x00000001 +WINDOW_BIT :: 0x0004 +OPENGL_BIT :: 0x0008 + +BLUE_SIZE :: 0x3022 +GREEN_SIZE :: 0x3023 +RED_SIZE :: 0x3024 +DEPTH_SIZE :: 0x3025 +STENCIL_SIZE :: 0x3026 + +SURFACE_TYPE :: 0x3033 +NONE :: 0x3038 +COLOR_BUFFER_TYPE :: 0x303F +RENDERABLE_TYPE :: 0x3040 +CONFORMANT :: 0x3042 + +BACK_BUFFER :: 0x3084 +RENDER_BUFFER :: 0x3086 +GL_COLORSPACE_SRGB :: 0x3089 +GL_COLORSPACE_LINEAR :: 0x308A +RGB_BUFFER :: 0x308E +GL_COLORSPACE :: 0x309D + +CONTEXT_MAJOR_VERSION :: 0x3098 +CONTEXT_MINOR_VERSION :: 0x30FB +CONTEXT_OPENGL_PROFILE_MASK :: 0x30FD + +OPENGL_API :: 0x30A2 + +foreign import egl "system:EGL" +@(default_calling_convention="c", link_prefix="egl") +foreign egl { + GetDisplay :: proc(display: NativeDisplayType) -> Display --- + Initialize :: proc(display: Display, major: ^i32, minor: ^i32) -> i32 --- + BindAPI :: proc(api: u32) -> i32 --- + ChooseConfig :: proc(display: Display, attrib_list: ^i32, configs: ^Context, config_size: i32, num_config: ^i32) -> i32 --- + CreateWindowSurface :: proc(display: Display, config: Config, native_window: NativeWindowType, attrib_list: ^i32) -> Surface --- + CreateContext :: proc(display: Display, config: Config, share_context: Context, attrib_list: ^i32) -> Context --- + MakeCurrent :: proc(display: Display, draw: Surface, read: Surface, ctx: Context) -> i32 --- + SwapInterval :: proc(display: Display, interval: i32) -> i32 --- + SwapBuffers :: proc(display: Display, surface: Surface) -> i32 --- + GetProcAddress :: proc(name: cstring) -> rawptr --- +} + +gl_set_proc_address :: proc(p: rawptr, name: cstring) { + (^rawptr)(p)^ = GetProcAddress(name) +} From 3bc172c70bc48c88110476b3a8f076f36671ea51 Mon Sep 17 00:00:00 2001 From: Laytan Laats Date: Sat, 2 Dec 2023 21:37:55 +0100 Subject: [PATCH 02/40] add crypto.rand_bytes for Darwin and FreeBSD --- core/crypto/rand_darwin_and_bsd.odin | 12 ++++++++++++ core/crypto/rand_generic.odin | 7 +++---- core/crypto/rand_openbsd.odin | 12 ------------ 3 files changed, 15 insertions(+), 16 deletions(-) create mode 100644 core/crypto/rand_darwin_and_bsd.odin delete mode 100644 core/crypto/rand_openbsd.odin diff --git a/core/crypto/rand_darwin_and_bsd.odin b/core/crypto/rand_darwin_and_bsd.odin new file mode 100644 index 000000000..aea7e2953 --- /dev/null +++ b/core/crypto/rand_darwin_and_bsd.odin @@ -0,0 +1,12 @@ +//+build freebsd, openbsd, darwin +package crypto + +foreign import libc "system:c" + +foreign libc { + arc4random_buf :: proc(buf: [^]byte, nbytes: uint) --- +} + +_rand_bytes :: proc(dst: []byte) { + arc4random_buf(raw_data(dst), len(dst)) +} diff --git a/core/crypto/rand_generic.odin b/core/crypto/rand_generic.odin index fde91f85a..b8bf900cd 100644 --- a/core/crypto/rand_generic.odin +++ b/core/crypto/rand_generic.odin @@ -1,7 +1,6 @@ +//+build !linux !windows !openbsd !freebsd !darwin !js package crypto -when ODIN_OS != .Linux && ODIN_OS != .OpenBSD && ODIN_OS != .Windows && ODIN_OS != .JS { - _rand_bytes :: proc(dst: []byte) { - unimplemented("crypto: rand_bytes not supported on this OS") - } +_rand_bytes :: proc(dst: []byte) { + unimplemented("crypto: rand_bytes not supported on this OS") } diff --git a/core/crypto/rand_openbsd.odin b/core/crypto/rand_openbsd.odin deleted file mode 100644 index bae97e8f0..000000000 --- a/core/crypto/rand_openbsd.odin +++ /dev/null @@ -1,12 +0,0 @@ -package crypto - -import "core:c" - -foreign import libc "system:c" -foreign libc { - arc4random_buf :: proc "c" (buf: rawptr, nbytes: c.size_t) --- -} - -_rand_bytes :: proc (dst: []byte) { - arc4random_buf(raw_data(dst), len(dst)) -} From 91cf0826c18531213bc91a432ac67e21b26f74ab Mon Sep 17 00:00:00 2001 From: Laytan Laats Date: Sun, 3 Dec 2023 20:43:46 +0100 Subject: [PATCH 03/40] use `Security.framework` with `SecRandomCopyBytes` for rand_bytes on darwin --- core/crypto/rand_darwin.odin | 11 +++++++++++ core/crypto/rand_darwin_and_bsd.odin | 2 +- core/sys/darwin/security.odin | 24 ++++++++++++++++++++++++ 3 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 core/crypto/rand_darwin.odin create mode 100644 core/sys/darwin/security.odin diff --git a/core/crypto/rand_darwin.odin b/core/crypto/rand_darwin.odin new file mode 100644 index 000000000..f7978c3fa --- /dev/null +++ b/core/crypto/rand_darwin.odin @@ -0,0 +1,11 @@ +package crypto + +import "core:fmt" +import "core:sys/darwin" + +_rand_bytes :: proc(dst: []byte) { + res := darwin.SecRandomCopyBytes(count=len(dst), bytes=raw_data(dst)) + if res != .Success { + panic(fmt.tprintf("crypto/rand_bytes: SecRandomCopyBytes returned non-zero result: %v", res)) + } +} diff --git a/core/crypto/rand_darwin_and_bsd.odin b/core/crypto/rand_darwin_and_bsd.odin index aea7e2953..8e2be1d95 100644 --- a/core/crypto/rand_darwin_and_bsd.odin +++ b/core/crypto/rand_darwin_and_bsd.odin @@ -1,4 +1,4 @@ -//+build freebsd, openbsd, darwin +//+build freebsd, openbsd package crypto foreign import libc "system:c" diff --git a/core/sys/darwin/security.odin b/core/sys/darwin/security.odin new file mode 100644 index 000000000..4f9789326 --- /dev/null +++ b/core/sys/darwin/security.odin @@ -0,0 +1,24 @@ +//+build darwin +package darwin + +foreign import security "system:Security.framework" + +// A reference to a random number generator. +SecRandomRef :: distinct rawptr + +OSStatus :: distinct i32 + +errSec :: enum OSStatus { + Success = 0, // No error. + Unimplemented = -4, // Function or operation not implemented. + + // Many more... +} + +foreign security { + // Synonym for nil, uses a cryptographically secure random number generator. + kSecRandomDefault: SecRandomRef + + // Generates an array of cryptographically secure random bytes. + SecRandomCopyBytes :: proc(rnd: SecRandomRef = kSecRandomDefault, count: uint, bytes: [^]byte) -> errSec --- +} From 32a4a5e6017df4eec3d6380beff5c7dca5077cb4 Mon Sep 17 00:00:00 2001 From: Laytan Laats Date: Sun, 3 Dec 2023 21:01:06 +0100 Subject: [PATCH 04/40] remove darwin from bsd filename --- core/crypto/{rand_darwin_and_bsd.odin => rand_bsd.odin} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename core/crypto/{rand_darwin_and_bsd.odin => rand_bsd.odin} (100%) diff --git a/core/crypto/rand_darwin_and_bsd.odin b/core/crypto/rand_bsd.odin similarity index 100% rename from core/crypto/rand_darwin_and_bsd.odin rename to core/crypto/rand_bsd.odin From 0d413b81364d57c569bcc35c2f9ac6e0c215cab8 Mon Sep 17 00:00:00 2001 From: Laytan Laats Date: Sun, 3 Dec 2023 22:32:24 +0100 Subject: [PATCH 05/40] implement part of core foundation framework bindings for err message --- core/crypto/rand_darwin.odin | 3 +- core/sys/darwin/core_foundation.odin | 98 ++++++++++++++++++++++++++++ core/sys/darwin/darwin.odin | 4 ++ core/sys/darwin/security.odin | 2 + 4 files changed, 106 insertions(+), 1 deletion(-) create mode 100644 core/sys/darwin/core_foundation.odin create mode 100644 core/sys/darwin/darwin.odin diff --git a/core/crypto/rand_darwin.odin b/core/crypto/rand_darwin.odin index f7978c3fa..ec44c1491 100644 --- a/core/crypto/rand_darwin.odin +++ b/core/crypto/rand_darwin.odin @@ -6,6 +6,7 @@ import "core:sys/darwin" _rand_bytes :: proc(dst: []byte) { res := darwin.SecRandomCopyBytes(count=len(dst), bytes=raw_data(dst)) if res != .Success { - panic(fmt.tprintf("crypto/rand_bytes: SecRandomCopyBytes returned non-zero result: %v", res)) + msg := darwin.CFStringCopyToOdinString(darwin.SecCopyErrorMessageString(res)) + panic(fmt.tprintf("crypto/rand_bytes: SecRandomCopyBytes returned non-zero result: %v %s", res, msg)) } } diff --git a/core/sys/darwin/core_foundation.odin b/core/sys/darwin/core_foundation.odin new file mode 100644 index 000000000..eafe1a1f3 --- /dev/null +++ b/core/sys/darwin/core_foundation.odin @@ -0,0 +1,98 @@ +//+build darwin +package darwin + +import "core:runtime" + +foreign import core_foundation "system:CoreFoundation.framework" + +CFTypeRef :: distinct rawptr + +CFStringRef :: distinct CFTypeRef + +CFIndex :: int + +CFRange :: struct { + location: CFIndex, + length: CFIndex, +} + +CFStringEncoding :: enum u32 { + ASCII = 1, + NEXTSTEP = 2, + JapaneseEUC = 3, + UTF8 = 4, + ISOLatin1 = 5, + Symbol = 6, + NonLossyASCII = 7, + ShiftJIS = 8, + ISOLatin2 = 9, + Unicode = 10, + WindowsCP1251 = 11, + WindowsCP1252 = 12, + WindowsCP1253 = 13, + WindowsCP1254 = 14, + WindowsCP1250 = 15, + ISO2022JP = 21, + MacOSRoman = 30, + + UTF16 = Unicode, + + UTF16BigEndian = 0x90000100, + UTF16LittleEndian = 0x94000100, + + UTF32 = 0x8c000100, + UTF32BigEndian = 0x98000100, + UTF32LittleEndian = 0x9c000100, +} + +foreign core_foundation { + // Copies the character contents of a string to a local C string buffer after converting the characters to a given encoding. + CFStringGetCString :: proc(theString: CFStringRef, buffer: [^]byte, bufferSize: CFIndex, encoding: CFStringEncoding) -> Bool --- + + // Returns the number (in terms of UTF-16 code pairs) of Unicode characters in a string. + CFStringGetLength :: proc(theString: CFStringRef) -> CFIndex --- + + // Returns the maximum number of bytes a string of a specified length (in Unicode characters) will take up if encoded in a specified encoding. + CFStringGetMaximumSizeForEncoding :: proc(length: CFIndex, encoding: CFStringEncoding) -> CFIndex --- + + // Fetches a range of the characters from a string into a byte buffer after converting the characters to a specified encoding. + CFStringGetBytes :: proc( + thestring: CFStringRef, + range: CFRange, + encoding: CFStringEncoding, + lossByte: u8, + isExternalRepresentation: Bool, + buffer: [^]byte, + maxBufLen: CFIndex, + usedBufLen: ^CFIndex, + ) -> CFIndex --- + + // Releases a Core Foundation object. + @(link_name="CFRelease") + _CFRelease :: proc(cf: CFTypeRef) --- +} + +// Releases a Core Foundation object. +CFRelease :: proc { + CFReleaseString, +} + +// Releases a Core Foundation string. +CFReleaseString :: #force_inline proc(theString: CFStringRef) { + _CFRelease(CFTypeRef(theString)) +} + +CFStringCopyToOdinString :: proc(theString: CFStringRef, allocator := context.allocator) -> (str: string, ok: bool) #optional_ok { + length := CFStringGetLength(theString) + max := CFStringGetMaximumSizeForEncoding(length, .UTF8) + + buf, err := make([]byte, max, allocator) + if err != nil { return } + + raw_str := runtime.Raw_String{ + data = raw_data(buf), + } + CFStringGetBytes(theString, {0, length}, .UTF8, 0, false, raw_data(buf), max, &raw_str.len) + + return transmute(string)raw_str, true +} diff --git a/core/sys/darwin/darwin.odin b/core/sys/darwin/darwin.odin new file mode 100644 index 000000000..0b3efb1f5 --- /dev/null +++ b/core/sys/darwin/darwin.odin @@ -0,0 +1,4 @@ +//+build darwin +package darwin + +Bool :: b8 diff --git a/core/sys/darwin/security.odin b/core/sys/darwin/security.odin index 4f9789326..0c58260e7 100644 --- a/core/sys/darwin/security.odin +++ b/core/sys/darwin/security.odin @@ -21,4 +21,6 @@ foreign security { // Generates an array of cryptographically secure random bytes. SecRandomCopyBytes :: proc(rnd: SecRandomRef = kSecRandomDefault, count: uint, bytes: [^]byte) -> errSec --- + + SecCopyErrorMessageString :: proc(status: errSec, reserved: rawptr = nil) -> CFStringRef --- } From 88e6980b132cbe14426fc1e91e19385bc4748a02 Mon Sep 17 00:00:00 2001 From: Laytan Laats Date: Mon, 18 Dec 2023 13:42:34 +0100 Subject: [PATCH 06/40] fix build tags --- core/crypto/rand_generic.odin | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/core/crypto/rand_generic.odin b/core/crypto/rand_generic.odin index b8bf900cd..bf7abbbe2 100644 --- a/core/crypto/rand_generic.odin +++ b/core/crypto/rand_generic.odin @@ -1,4 +1,9 @@ -//+build !linux !windows !openbsd !freebsd !darwin !js +//+build !linux +//+build !windows +//+build !openbsd +//+build !freebsd +//+build !darwin +//+build !js package crypto _rand_bytes :: proc(dst: []byte) { From 7a592cbb31f351627e7d508de9fafacfa65404cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Ignacio=20D=C3=ADaz?= Date: Fri, 16 Feb 2024 13:11:49 -0300 Subject: [PATCH 07/40] port math.round from Golang --- core/math/math.odin | 103 +++++++++++----- tests/core/math/test_core_math.odin | 181 ++++++++++++++++++++++++++++ 2 files changed, 254 insertions(+), 30 deletions(-) diff --git a/core/math/math.odin b/core/math/math.odin index 7fdbcba04..982f4b984 100644 --- a/core/math/math.odin +++ b/core/math/math.odin @@ -644,42 +644,85 @@ trunc :: proc{ } @(require_results) -round_f16 :: proc "contextless" (x: f16) -> f16 { - return ceil(x - 0.5) if x < 0 else floor(x + 0.5) -} -@(require_results) -round_f16le :: proc "contextless" (x: f16le) -> f16le { - return ceil(x - 0.5) if x < 0 else floor(x + 0.5) -} -@(require_results) -round_f16be :: proc "contextless" (x: f16be) -> f16be { - return ceil(x - 0.5) if x < 0 else floor(x + 0.5) +round_f16 :: proc "contextless" (x: f16) -> f16 { + mask :: F16_MASK + shift :: F16_SHIFT + bias :: F16_BIAS + + bits := transmute(u16)x + e := (bits >> shift) & mask + + if e < bias { + bits &= 0x8000 + if e == bias - 1 { + bits |= transmute(u16)f16(1) + } + } else if e < bias + shift { + half :: 1 << (shift - 1) + mantissa :: (1 << shift) - 1 + e -= bias + bits += half >> e + bits &~= mantissa >> e + } + + return transmute(f16)bits } +@(require_results) round_f16le :: proc "contextless" (x: f16le) -> f16le { return #force_inline f16le(round_f16(f16(x))) } +@(require_results) round_f16be :: proc "contextless" (x: f16be) -> f16be { return #force_inline f16be(round_f16(f16(x))) } @(require_results) -round_f32 :: proc "contextless" (x: f32) -> f32 { - return ceil(x - 0.5) if x < 0 else floor(x + 0.5) +round_f32 :: proc "contextless" (x: f32) -> f32 { + mask :: F32_MASK + shift :: F32_SHIFT + bias :: F32_BIAS + + bits := transmute(u32)x + e := (bits >> shift) & mask + + if e < bias { + bits &= 0x8000_0000 + if e == bias - 1 { + bits |= transmute(u32)f32(1) + } + } else if e < bias + shift { + half :: 1 << (shift - 1) + mantissa :: (1 << shift) - 1 + e -= bias + bits += half >> e + bits &~= mantissa >> e + } + + return transmute(f32)bits } +@(require_results) round_f32le :: proc "contextless" (x: f32le) -> f32le { return #force_inline f32le(round_f32(f32(x))) } +@(require_results) round_f32be :: proc "contextless" (x: f32be) -> f32be { return #force_inline f32be(round_f32(f32(x))) } + @(require_results) -round_f32le :: proc "contextless" (x: f32le) -> f32le { - return ceil(x - 0.5) if x < 0 else floor(x + 0.5) -} -@(require_results) -round_f32be :: proc "contextless" (x: f32be) -> f32be { - return ceil(x - 0.5) if x < 0 else floor(x + 0.5) -} -@(require_results) -round_f64 :: proc "contextless" (x: f64) -> f64 { - return ceil(x - 0.5) if x < 0 else floor(x + 0.5) -} -@(require_results) -round_f64le :: proc "contextless" (x: f64le) -> f64le { - return ceil(x - 0.5) if x < 0 else floor(x + 0.5) -} -@(require_results) -round_f64be :: proc "contextless" (x: f64be) -> f64be { - return ceil(x - 0.5) if x < 0 else floor(x + 0.5) +round_f64 :: proc "contextless" (x: f64) -> f64 { + mask :: F64_MASK + shift :: F64_SHIFT + bias :: F64_BIAS + + bits := transmute(u64)x + e := (bits >> shift) & mask + + if e < bias { + bits &= 0x8000_0000_0000_0000 + if e == bias - 1 { + bits |= transmute(u64)f64(1) + } + } else if e < bias + shift { + half :: 1 << (shift - 1) + mantissa :: (1 << shift) - 1 + e -= bias + bits += half >> e + bits &~= mantissa >> e + } + + return transmute(f64)bits } +@(require_results) round_f64le :: proc "contextless" (x: f64le) -> f64le { return #force_inline f64le(round_f64(f64(x))) } +@(require_results) round_f64be :: proc "contextless" (x: f64be) -> f64be { return #force_inline f64be(round_f64(f64(x))) } round :: proc{ round_f16, round_f16le, round_f16be, round_f32, round_f32le, round_f32be, diff --git a/tests/core/math/test_core_math.odin b/tests/core/math/test_core_math.odin index 30e1875c0..df989bff6 100644 --- a/tests/core/math/test_core_math.odin +++ b/tests/core/math/test_core_math.odin @@ -19,6 +19,10 @@ main :: proc() { test_trunc_f32(&t) test_trunc_f64(&t) + test_round_f16(&t) + test_round_f32(&t) + test_round_f64(&t) + test_nan(&t) test_acos(&t) test_acosh(&t) @@ -307,6 +311,183 @@ test_trunc_f64 :: proc(t: ^testing.T) { tc.expect(t, math.is_nan_f64(r), fmt.tprintf("%s(%f) -> %f != NaN", #procedure, v, r)) } +@test +test_round_f16 :: proc(t: ^testing.T) { + r, v: f16 + + Datum :: struct { + i: int, + v: f16, + e: f16, + } + @static data := []Datum{ + { 0, 10.5, 11 }, + { 1, -10.5, -11 }, + + { 2, math.F16_MAX, math.F16_MAX }, + { 3, -math.F16_MAX, -math.F16_MAX }, + { 4, math.F16_MIN, 0.0 }, + { 5, -math.F16_MIN, -0.0 }, + { 6, 0.0, 0.0 }, + { 7, -0.0, -0.0 }, + { 8, 1, 1 }, + { 9, -1, -1 }, + { 10, math.INF_F16, math.INF_F16 }, + { 11, math.NEG_INF_F16, math.NEG_INF_F16 }, + + /* From https://en.wikipedia.org/wiki/Half-precision_floating-point_format */ + { 12, 0h3C01, 1 }, // 0x1.004p+0 (smallest > 1) + { 13, -0h3C01, -1 }, + { 14, 0h3BFF, 1 }, // 0x1.ffcp-1 (largest < 1) + { 15, -0h3BFF, -1 }, + { 16, 0h0001, 0.0 }, // 0x0.004p-14 (smallest subnormal) + { 17, -0h0001, -0.0 }, + { 18, 0h03FF, 0.0 }, // 0x0.ffcp-14 (largest subnormal) + { 19, -0h03FF, -0.0 }, + + { 20, 0hC809, -8 }, // -0x1.024p+3 + { 21, 0h4458, 4 }, // 0x1.16p+2 + } + + for d, i in data { + assert(i == d.i) + r = math.round_f16(d.v) + tc.expect(t, r == d.e, fmt.tprintf("i:%d %s(%h) -> %h != %h", i, #procedure, d.v, r, d.e)) + } + + v = math.SNAN_F16 + r = math.round_f16(v) + tc.expect(t, math.is_nan_f16(r), fmt.tprintf("%s(%f) -> %f != NaN", #procedure, v, r)) + + v = math.QNAN_F16 + r = math.round_f16(v) + tc.expect(t, math.is_nan_f16(r), fmt.tprintf("%s(%f) -> %f != NaN", #procedure, v, r)) +} + +@test +test_round_f32 :: proc(t: ^testing.T) { + r, v: f32 + + Datum :: struct { + i: int, + v: f32, + e: f32, + } + @static data := []Datum{ + { 0, 10.5, 11 }, + { 1, -10.5, -11 }, + + { 2, math.F32_MAX, math.F32_MAX }, + { 3, -math.F32_MAX, -math.F32_MAX }, + { 4, math.F32_MIN, 0.0 }, + { 5, -math.F32_MIN, -0.0 }, + { 6, 0.0, 0.0 }, + { 7, -0.0, -0.0 }, + { 8, 1, 1 }, + { 9, -1, -1 }, + { 10, math.INF_F32, math.INF_F32 }, + { 11, math.NEG_INF_F32, math.NEG_INF_F32 }, + + /* From https://en.wikipedia.org/wiki/Single-precision_floating-point_format */ + { 12, 0h3F80_0001, 1 }, // 0x1.000002p+0 (smallest > 1) + { 13, -0h3F80_0001, -1 }, + { 14, 0h3F7F_FFFF, 1 }, // 0x1.fffffep-1 (largest < 1) + { 15, -0h3F7F_FFFF, -1 }, + { 16, 0h0000_0001, 0.0 }, // 0x0.000002p-126 (smallest subnormal) + { 17, -0h0000_0001, -0.0 }, + { 18, 0h007F_FFFF, 0.0 }, // 0x0.fffffep-126 (largest subnormal) + { 19, -0h007F_FFFF, -0.0 }, + + /* From libc-test src/math/sanity/roundf.h */ + { 20, 0hC101_11D0, -8 }, // -0x1.0223ap+3 + { 21, 0h408B_0C34, 4 }, // 0x1.161868p+2 + { 22, 0hC106_1A5A, -8 }, // -0x1.0c34b4p+3 + { 23, 0hC0D1_0378, -7 }, // -0x1.a206fp+2 + { 24, 0h4114_45DE, 9 }, // 0x1.288bbcp+3 + { 25, 0h3F29_77E8, 1.0 }, // 0x1.52efdp-1 + { 26, 0hBED0_2E64, -0.0 }, // -0x1.a05cc8p-2 + { 27, 0h3F0F_CF7D, 1.0 }, // 0x1.1f9efap-1 + { 28, 0h3F46_2ED8, 1.0 }, // 0x1.8c5dbp-1 + { 29, 0hBF2D_C375, -1.0 }, // -0x1.5b86eap-1 + } + + for d, i in data { + assert(i == d.i) + r = math.round_f32(d.v) + tc.expect(t, r == d.e, fmt.tprintf("i:%d %s(%h) -> %h != %h", i, #procedure, d.v, r, d.e)) + } + + v = math.SNAN_F32 + r = math.round_f32(v) + tc.expect(t, math.is_nan_f32(r), fmt.tprintf("%s(%f) -> %f != NaN", #procedure, v, r)) + + v = math.QNAN_F32 + r = math.round_f32(v) + tc.expect(t, math.is_nan_f32(r), fmt.tprintf("%s(%f) -> %f != NaN", #procedure, v, r)) +} + +@test +test_round_f64 :: proc(t: ^testing.T) { + r, v: f64 + + Datum :: struct { + i: int, + v: f64, + e: f64, + } + data := []Datum{ + { 0, 10.5, 11 }, // Issue #1574 fract in linalg/glm is broken + { 1, -10.5, -11 }, + + { 2, math.F64_MAX, math.F64_MAX }, + { 3, -math.F64_MAX, -math.F64_MAX }, + { 4, math.F64_MIN, 0.0 }, + { 5, -math.F64_MIN, -0.0 }, + { 6, 0.0, 0.0 }, + { 7, -0.0, -0.0 }, + { 8, 1, 1 }, + { 9, -1, -1 }, + { 10, math.INF_F64, math.INF_F64 }, + { 11, math.NEG_INF_F64, math.NEG_INF_F64 }, + + /* From https://en.wikipedia.org/wiki/Double-precision_floating-point_format */ + { 12, 0h3FF0_0000_0000_0001, 1 }, // 0x1.0000000000001p+0 (smallest > 1) + { 13, -0h3FF0_0000_0000_0001, -1 }, + { 14, 0h3FEF_FFFF_FFFF_FFFF, 1 }, // 0x1.fffffffffffffp-1 (largest < 1) + { 15, -0h3FEF_FFFF_FFFF_FFFF, -1 }, + { 16, 0h0000_0000_0000_0001, 0.0 }, // 0x0.0000000000001p-1022 (smallest subnormal) + { 17, -0h0000_0000_0000_0001, -0.0 }, + { 18, 0h000F_FFFF_FFFF_FFFF, 0.0 }, // 0x0.fffffffffffffp-1022 (largest subnormal) + { 19, -0h000F_FFFF_FFFF_FFFF, -0.0 }, + + /* From libc-test src/math/sanity/round.h */ + { 20, 0hC020_2239_F3C6_A8F1, -8 }, // -0x1.02239f3c6a8f1p+3 + { 21, 0h4011_6186_8E18_BC67, 4 }, // 0x1.161868e18bc67p+2 + { 22, 0hC020_C34B_3E01_E6E7, -8 }, // -0x1.0c34b3e01e6e7p+3 + { 23, 0hC01A_206F_0A19_DCC4, -7 }, // -0x1.a206f0a19dcc4p+2 + { 24, 0h4022_88BB_B0D6_A1E6, 9 }, // 0x1.288bbb0d6a1e6p+3 + { 25, 0h3FE5_2EFD_0CD8_0497, 1.0 }, // 0x1.52efd0cd80497p-1 + { 26, 0hBFDA_05CC_7544_81D1, -0.0 }, // -0x1.a05cc754481d1p-2 + { 27, 0h3FE1_F9EF_9347_45CB, 1.0 }, // 0x1.1f9ef934745cbp-1 + { 28, 0h3FE8_C5DB_097F_7442, 1.0 }, // 0x1.8c5db097f7442p-1 + { 29, 0hBFE5_B86E_A811_8A0E, -1.0 }, // -0x1.5b86ea8118a0ep-1 + } + + for d, i in data { + assert(i == d.i) + r = math.round_f64(d.v) + tc.expect(t, r == d.e, fmt.tprintf("i:%d %s(%h) -> %h != %h", i, #procedure, d.v, r, d.e)) + } + + v = math.SNAN_F64 + r = math.round_f64(v) + tc.expect(t, math.is_nan_f64(r), fmt.tprintf("%s(%f) -> %f != NaN", #procedure, v, r)) + + v = math.QNAN_F64 + r = math.round_f64(v) + tc.expect(t, math.is_nan_f64(r), fmt.tprintf("%s(%f) -> %f != NaN", #procedure, v, r)) +} + vf := []f64{ 4.9790119248836735e+00, From cd61251d398bda34075c3b9c7d98e5e985dd6ab6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Ignacio=20D=C3=ADaz?= Date: Sat, 17 Feb 2024 07:25:33 -0300 Subject: [PATCH 08/40] add Go license --- core/math/math.odin | 92 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 91 insertions(+), 1 deletion(-) diff --git a/core/math/math.odin b/core/math/math.odin index 982f4b984..570c2d255 100644 --- a/core/math/math.odin +++ b/core/math/math.odin @@ -645,6 +645,36 @@ trunc :: proc{ @(require_results) round_f16 :: proc "contextless" (x: f16) -> f16 { + // origin: Go /src/math/floor.go + // + // Copyright (c) 2009 The Go Authors. All rights reserved. + // + // Redistribution and use in source and binary forms, with or without + // modification, are permitted provided that the following conditions are + // met: + // + // * Redistributions of source code must retain the above copyright + // notice, this list of conditions and the following disclaimer. + // * Redistributions in binary form must reproduce the above + // copyright notice, this list of conditions and the following disclaimer + // in the documentation and/or other materials provided with the + // distribution. + // * Neither the name of Google Inc. nor the names of its + // contributors may be used to endorse or promote products derived from + // this software without specific prior written permission. + // + // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + mask :: F16_MASK shift :: F16_SHIFT bias :: F16_BIAS @@ -672,6 +702,36 @@ round_f16 :: proc "contextless" (x: f16) -> f16 { @(require_results) round_f32 :: proc "contextless" (x: f32) -> f32 { + // origin: Go /src/math/floor.go + // + // Copyright (c) 2009 The Go Authors. All rights reserved. + // + // Redistribution and use in source and binary forms, with or without + // modification, are permitted provided that the following conditions are + // met: + // + // * Redistributions of source code must retain the above copyright + // notice, this list of conditions and the following disclaimer. + // * Redistributions in binary form must reproduce the above + // copyright notice, this list of conditions and the following disclaimer + // in the documentation and/or other materials provided with the + // distribution. + // * Neither the name of Google Inc. nor the names of its + // contributors may be used to endorse or promote products derived from + // this software without specific prior written permission. + // + // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + mask :: F32_MASK shift :: F32_SHIFT bias :: F32_BIAS @@ -699,6 +759,36 @@ round_f32 :: proc "contextless" (x: f32) -> f32 { @(require_results) round_f64 :: proc "contextless" (x: f64) -> f64 { + // origin: Go /src/math/floor.go + // + // Copyright (c) 2009 The Go Authors. All rights reserved. + // + // Redistribution and use in source and binary forms, with or without + // modification, are permitted provided that the following conditions are + // met: + // + // * Redistributions of source code must retain the above copyright + // notice, this list of conditions and the following disclaimer. + // * Redistributions in binary form must reproduce the above + // copyright notice, this list of conditions and the following disclaimer + // in the documentation and/or other materials provided with the + // distribution. + // * Neither the name of Google Inc. nor the names of its + // contributors may be used to endorse or promote products derived from + // this software without specific prior written permission. + // + // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + mask :: F64_MASK shift :: F64_SHIFT bias :: F64_BIAS @@ -2396,4 +2486,4 @@ INF_F64 :: f64(0h7FF0_0000_0000_0000) NEG_INF_F64 :: f64(0hFFF0_0000_0000_0000) SNAN_F64 :: f64(0h7FF0_0000_0000_0001) -QNAN_F64 :: f64(0h7FF8_0000_0000_0001) \ No newline at end of file +QNAN_F64 :: f64(0h7FF8_0000_0000_0001) From b1e608bfba86fafb65825a8ac6028d72f0b9d134 Mon Sep 17 00:00:00 2001 From: Tetralux Date: Sun, 18 Feb 2024 14:46:21 +0000 Subject: [PATCH 09/40] [fmt] Add *printfln() Adds the following procedures, which just call through to the `printf` versions, with `newline = true`; a new parameter also added in this commit. In all cases, `wprintf` is the one that ultimately writes the newline, if requested. - printfln - fprintfln - eprintfln - aprintfln - tprintfln - bprintfln - caprintfln - ctprintfln - sbprintfln - wprintfln --- core/fmt/fmt.odin | 145 ++++++++++++++++++++++++++++++++++++------- core/fmt/fmt_os.odin | 24 ++++--- 2 files changed, 137 insertions(+), 32 deletions(-) diff --git a/core/fmt/fmt.odin b/core/fmt/fmt.odin index a0d6d66d1..c840135ed 100644 --- a/core/fmt/fmt.odin +++ b/core/fmt/fmt.odin @@ -147,16 +147,30 @@ aprintln :: proc(args: ..any, sep := " ", allocator := context.allocator) -> str // *Allocates Using Context's Allocator* // // Inputs: +// - fmt: A format string with placeholders for the provided arguments. +// - args: A variadic list of arguments to be formatted. +// - newline: Whether the string should end with a newline. (See `aprintfln`.) +// +// Returns: A formatted string. The returned string must be freed accordingly. +// +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) +} +// Creates a formatted string using a format string and arguments, followed by a newline. +// +// *Allocates Using Context's Allocator* +// +// Inputs: // - fmt: A format string with placeholders for the provided arguments. // - args: A variadic list of arguments to be formatted. // // Returns: A formatted string. The returned string must be freed accordingly. // -aprintf :: proc(fmt: string, args: ..any, allocator := context.allocator) -> string { - str: strings.Builder - strings.builder_init(&str, allocator) - sbprintf(&str, fmt, ..args) - return strings.to_string(str) +aprintfln :: proc(fmt: string, args: ..any, allocator := context.allocator) -> string { + return aprintf(fmt, ..args, allocator=allocator, newline=true) } // Creates a formatted string // @@ -195,16 +209,30 @@ tprintln :: proc(args: ..any, sep := " ") -> string { // *Allocates Using Context's Temporary Allocator* // // Inputs: +// - fmt: A format string with placeholders for the provided arguments. +// - args: A variadic list of arguments to be formatted. +// - newline: Whether the string should end with a newline. (See `tprintfln`.) +// +// Returns: A formatted 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) +} +// Creates a formatted string using a format string and arguments, followed by a newline. +// +// *Allocates Using Context's Temporary Allocator* +// +// Inputs: // - fmt: A format string with placeholders for the provided arguments. // - args: A variadic list of arguments to be formatted. // // Returns: A formatted string. // -tprintf :: proc(fmt: string, args: ..any) -> string { - str: strings.Builder - strings.builder_init(&str, context.temp_allocator) - sbprintf(&str, fmt, ..args) - return strings.to_string(str) +tprintfln :: proc(fmt: string, args: ..any) -> string { + return tprintf(fmt, ..args, newline=true) } // Creates a formatted string using a supplied buffer as the backing array. Writes into the buffer. // @@ -238,12 +266,25 @@ bprintln :: proc(buf: []byte, args: ..any, sep := " ") -> string { // - buf: The backing buffer // - fmt: A format string with placeholders for the provided arguments // - args: A variadic list of arguments to be formatted +// - newline: Whether the string should end with a newline. (See `bprintfln`.) // // Returns: A formatted string // -bprintf :: proc(buf: []byte, fmt: string, args: ..any) -> string { +bprintf :: proc(buf: []byte, fmt: string, args: ..any, newline := false) -> string { sb := strings.builder_from_bytes(buf) - return sbprintf(&sb, fmt, ..args) + return sbprintf(&sb, fmt, ..args, newline=newline) +} +// Creates a formatted string using a supplied buffer as the backing array, followed by a newline. Writes into the buffer. +// +// Inputs: +// - buf: The backing buffer +// - fmt: A format string with placeholders for the provided arguments +// - args: A variadic list of arguments to be formatted +// +// Returns: A formatted string +// +bprintfln :: proc(buf: []byte, fmt: string, args: ..any) -> string { + return bprintf(buf, fmt, ..args, newline=true) } // Runtime assertion with a formatted message // @@ -294,17 +335,31 @@ panicf :: proc(fmt: string, args: ..any, loc := #caller_location) -> ! { // Inputs: // - format: A format string with placeholders for the provided arguments // - args: A variadic list of arguments to be formatted +// - newline: Whether the string should end with a newline. (See `caprintfln`.) // // Returns: A formatted C string // -caprintf :: proc(format: string, args: ..any) -> cstring { +caprintf :: proc(format: string, args: ..any, newline := false) -> cstring { str: strings.Builder strings.builder_init(&str) - sbprintf(&str, format, ..args) + sbprintf(&str, format, ..args, newline=newline) strings.write_byte(&str, 0) s := strings.to_string(str) return cstring(raw_data(s)) } +// Creates a formatted C string, followed by a newline. +// +// *Allocates Using Context's Allocator* +// +// Inputs: +// - format: A format string with placeholders for the provided arguments +// - args: A variadic list of arguments to be formatted +// +// Returns: A formatted C string +// +caprintfln :: proc(format: string, args: ..any) -> cstring { + return caprintf(format, ..args, newline=true) +} // Creates a formatted C string // // *Allocates Using Context's Temporary Allocator* @@ -312,16 +367,30 @@ caprintf :: proc(format: string, args: ..any) -> cstring { // Inputs: // - format: A format string with placeholders for the provided arguments // - args: A variadic list of arguments to be formatted +// - newline: Whether the string should end with a newline. (See `ctprintfln`.) +// +// Returns: A formatted C string +// +ctprintf :: proc(format: string, args: ..any, newline := false) -> cstring { + str: strings.Builder + strings.builder_init(&str, context.temp_allocator) + sbprintf(&str, format, ..args, newline=newline) + strings.write_byte(&str, 0) + s := strings.to_string(str) + return cstring(raw_data(s)) +} +// Creates a formatted C string, followed by a newline. +// +// *Allocates Using Context's Temporary Allocator* +// +// Inputs: +// - format: A format string with placeholders for the provided arguments +// - args: A variadic list of arguments to be formatted // // Returns: A formatted C string // -ctprintf :: proc(format: string, args: ..any) -> cstring { - str: strings.Builder - strings.builder_init(&str, context.temp_allocator) - sbprintf(&str, format, ..args) - strings.write_byte(&str, 0) - s := strings.to_string(str) - return cstring(raw_data(s)) +ctprintfln :: proc(format: string, args: ..any) -> cstring { + return ctprintf(format, ..args, newline=true) } // Formats using the default print settings and writes to the given strings.Builder // @@ -355,13 +424,25 @@ sbprintln :: proc(buf: ^strings.Builder, args: ..any, sep := " ") -> string { // - buf: A pointer to a strings.Builder buffer // - fmt: The format string // - args: A variadic list of arguments to be formatted +// - newline: Whether a trailing newline should be written. (See `sbprintfln`.) // // Returns: The resulting formatted string // -sbprintf :: proc(buf: ^strings.Builder, fmt: string, args: ..any) -> string { - wprintf(strings.to_writer(buf), fmt, ..args, flush=true) +sbprintf :: proc(buf: ^strings.Builder, fmt: string, args: ..any, newline := false) -> string { + wprintf(strings.to_writer(buf), fmt, ..args, flush=true, newline=newline) return strings.to_string(buf^) } +// Formats and writes to a strings.Builder buffer according to the specified format string, followed by a newline. +// +// Inputs: +// - buf: A pointer to a strings.Builder to store the formatted string +// - args: A variadic list of arguments to be formatted +// +// Returns: A formatted string +// +sbprintfln :: proc(buf: ^strings.Builder, format: string, args: ..any) -> string { + return sbprintf(buf, format, ..args, newline=true) +} // Formats and writes to an io.Writer using the default print settings // // Inputs: @@ -435,10 +516,11 @@ wprintln :: proc(w: io.Writer, args: ..any, sep := " ", flush := true) -> int { // - w: An io.Writer to write to // - fmt: The format string // - args: A variadic list of arguments to be formatted +// - newline: Whether a trailing newline should be written. (See `wprintfln`.) // // Returns: The number of bytes written // -wprintf :: proc(w: io.Writer, fmt: string, args: ..any, flush := true) -> int { +wprintf :: proc(w: io.Writer, fmt: string, args: ..any, flush := true, newline := false) -> int { fi: Info arg_index: int = 0 end := len(fmt) @@ -708,12 +790,27 @@ wprintf :: proc(w: io.Writer, fmt: string, args: ..any, flush := true) -> int { } io.write_string(fi.writer, ")", &fi.n) } + + if newline { + io.write_byte(w, '\n', &fi.n) + } if flush { io.flush(w) } return fi.n } +// Formats and writes to an io.Writer according to the specified format string, followed by a newline. +// +// Inputs: +// - w: The io.Writer to write to. +// - args: A variadic list of arguments to be formatted. +// +// Returns: The number of bytes written. +// +wprintfln :: proc(w: io.Writer, format: string, args: ..any, flush := true) -> int { + return wprintf(w, format, ..args, flush=flush, newline=true) +} // Writes a ^runtime.Type_Info value to an io.Writer // // Inputs: diff --git a/core/fmt/fmt_os.odin b/core/fmt/fmt_os.odin index afc28ffff..a403dcd65 100644 --- a/core/fmt/fmt_os.odin +++ b/core/fmt/fmt_os.odin @@ -30,7 +30,7 @@ fprintln :: proc(fd: os.Handle, args: ..any, sep := " ", flush := true) -> int { 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) -> int { +fprintf :: proc(fd: os.Handle, fmt: string, args: ..any, flush := true, newline := false) -> int { buf: [1024]byte b: bufio.Writer defer bufio.writer_flush(&b) @@ -38,7 +38,11 @@ fprintf :: proc(fd: os.Handle, fmt: string, args: ..any, flush := true) -> int { bufio.writer_init_with_buf(&b, os.stream_from_handle(fd), buf[:]) w := bufio.writer_to_writer(&b) - return wprintf(w, fmt, ..args, flush=flush) + 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) -> int { + return fprintf(fd, fmt, ..args, flush=flush, newline=true) } fprint_type :: proc(fd: os.Handle, info: ^runtime.Type_Info, flush := true) -> (n: int, err: io.Error) { buf: [1024]byte @@ -62,15 +66,19 @@ fprint_typeid :: proc(fd: os.Handle, id: typeid, flush := true) -> (n: int, err: } // print formats using the default print settings and writes to os.stdout -print :: proc(args: ..any, sep := " ", flush := true) -> int { return fprint(os.stdout, ..args, sep=sep, flush=flush) } +print :: proc(args: ..any, sep := " ", flush := true) -> int { return fprint(os.stdout, ..args, sep=sep, flush=flush) } // println formats using the default print settings and writes to os.stdout -println :: proc(args: ..any, sep := " ", flush := true) -> int { return fprintln(os.stdout, ..args, sep=sep, flush=flush) } +println :: proc(args: ..any, sep := " ", flush := true) -> int { return fprintln(os.stdout, ..args, sep=sep, flush=flush) } // printf formats according to the specified format string and writes to os.stdout -printf :: proc(fmt: string, args: ..any, flush := true) -> int { return fprintf(os.stdout, fmt, ..args, flush=flush) } +printf :: proc(fmt: string, args: ..any, flush := true) -> int { return fprintf(os.stdout, fmt, ..args, flush=flush) } +// printfln formats according to the specified format string and writes to os.stdout, followed by a newline. +printfln :: proc(fmt: string, args: ..any, flush := true) -> int { return fprintf(os.stdout, fmt, ..args, flush=flush, newline=true) } // eprint formats using the default print settings and writes to os.stderr -eprint :: proc(args: ..any, sep := " ", flush := true) -> int { return fprint(os.stderr, ..args, sep=sep, flush=flush) } +eprint :: proc(args: ..any, sep := " ", flush := true) -> int { return fprint(os.stderr, ..args, sep=sep, flush=flush) } // eprintln formats using the default print settings and writes to os.stderr -eprintln :: proc(args: ..any, sep := " ", flush := true) -> int { return fprintln(os.stderr, ..args, sep=sep, flush=flush) } +eprintln :: proc(args: ..any, sep := " ", flush := true) -> int { return fprintln(os.stderr, ..args, sep=sep, flush=flush) } // eprintf formats according to the specified format string and writes to os.stderr -eprintf :: proc(fmt: string, args: ..any, flush := true) -> int { return fprintf(os.stderr, fmt, ..args, flush=flush) } +eprintf :: proc(fmt: string, args: ..any, flush := true) -> int { return fprintf(os.stderr, fmt, ..args, flush=flush) } +// eprintfln formats according to the specified format string and writes to os.stderr, followed by a newline. +eprintfln :: proc(fmt: string, args: ..any, flush := true) -> int { return fprintf(os.stderr, fmt, ..args, flush=flush, newline=true) } From f6f3a760bcdbad183a4141738b19779e88ed7dfc Mon Sep 17 00:00:00 2001 From: Laytan Laats Date: Wed, 21 Feb 2024 22:05:11 +0100 Subject: [PATCH 10/40] Promote types in `#c_varargs` according to C rules --- src/llvm_backend_proc.cpp | 4 ++-- src/types.cpp | 46 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 2 deletions(-) diff --git a/src/llvm_backend_proc.cpp b/src/llvm_backend_proc.cpp index 9419f9a3c..4a981277d 100644 --- a/src/llvm_backend_proc.cpp +++ b/src/llvm_backend_proc.cpp @@ -3361,9 +3361,9 @@ gb_internal lbValue lb_build_call_expr_internal(lbProcedure *p, Ast *expr) { for (Ast *var_arg : variadic) { lbValue arg = lb_build_expr(p, var_arg); if (is_type_any(elem_type)) { - array_add(&args, lb_emit_conv(p, arg, default_type(arg.type))); + array_add(&args, lb_emit_conv(p, arg, c_vararg_promote_type(default_type(arg.type)))); } else { - array_add(&args, lb_emit_conv(p, arg, elem_type)); + array_add(&args, lb_emit_conv(p, arg, c_vararg_promote_type(elem_type))); } } break; diff --git a/src/types.cpp b/src/types.cpp index 2f1994574..c31b6e1bd 100644 --- a/src/types.cpp +++ b/src/types.cpp @@ -548,6 +548,14 @@ gb_global Type *t_f16 = &basic_types[Basic_f16]; gb_global Type *t_f32 = &basic_types[Basic_f32]; gb_global Type *t_f64 = &basic_types[Basic_f64]; +gb_global Type *t_f16be = &basic_types[Basic_f16be]; +gb_global Type *t_f32be = &basic_types[Basic_f32be]; +gb_global Type *t_f64be = &basic_types[Basic_f64be]; + +gb_global Type *t_f16le = &basic_types[Basic_f16le]; +gb_global Type *t_f32le = &basic_types[Basic_f32le]; +gb_global Type *t_f64le = &basic_types[Basic_f64le]; + gb_global Type *t_complex32 = &basic_types[Basic_complex32]; gb_global Type *t_complex64 = &basic_types[Basic_complex64]; gb_global Type *t_complex128 = &basic_types[Basic_complex128]; @@ -2795,6 +2803,44 @@ gb_internal Type *default_type(Type *type) { return type; } +// See https://en.cppreference.com/w/c/language/conversion#Default_argument_promotions +gb_internal Type *c_vararg_promote_type(Type *type) { + GB_ASSERT(type != nullptr); + + Type *core = core_type(type); + if (core->kind == Type_Basic) { + switch (core->Basic.kind) { + case Basic_f32: + case Basic_UntypedFloat: + return t_f64; + case Basic_f32le: + return t_f64le; + case Basic_f32be: + return t_f64be; + + case Basic_UntypedBool: + case Basic_bool: + case Basic_b8: + case Basic_b16: + case Basic_i8: + case Basic_i16: + case Basic_u8: + case Basic_u16: + return t_i32; + + case Basic_i16le: + case Basic_u16le: + return t_i32le; + + case Basic_i16be: + case Basic_u16be: + return t_i32be; + } + } + + return type; +} + gb_internal bool union_variant_index_types_equal(Type *v, Type *vt) { if (are_types_identical(v, vt)) { return true; From bb23648c7133a1827373bfbb8922504f20e0478c Mon Sep 17 00:00:00 2001 From: Laytan Laats Date: Thu, 22 Feb 2024 18:17:52 +0100 Subject: [PATCH 11/40] Also convert and promote bit_set types for #c_varargs --- src/types.cpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/types.cpp b/src/types.cpp index c31b6e1bd..9f52ed17d 100644 --- a/src/types.cpp +++ b/src/types.cpp @@ -2808,6 +2808,11 @@ gb_internal Type *c_vararg_promote_type(Type *type) { GB_ASSERT(type != nullptr); Type *core = core_type(type); + + if (core->kind == Type_BitSet) { + core = core_type(bit_set_to_int(core)); + } + if (core->kind == Type_Basic) { switch (core->Basic.kind) { case Basic_f32: From b894df2125df66516851faaa4ba1984b99f9059c Mon Sep 17 00:00:00 2001 From: Laytan Laats Date: Thu, 22 Feb 2024 20:17:11 +0100 Subject: [PATCH 12/40] Make MacOS releases self contained / bundle LLVM --- .github/workflows/nightly.yml | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/.github/workflows/nightly.yml b/.github/workflows/nightly.yml index 481434a7a..27d3ceceb 100644 --- a/.github/workflows/nightly.yml +++ b/.github/workflows/nightly.yml @@ -76,15 +76,13 @@ jobs: - uses: actions/checkout@v1 - name: Download LLVM and setup PATH run: | - brew install llvm@13 + brew install llvm@13 dylibbundler echo "/usr/local/opt/llvm@13/bin" >> $GITHUB_PATH TMP_PATH=$(xcrun --show-sdk-path)/user/include echo "CPATH=$TMP_PATH" >> $GITHUB_ENV - name: build odin run: make nightly - - name: Odin run - run: ./odin run examples/demo - - name: Copy artifacts + - name: Bundle run: | mkdir dist cp odin dist @@ -94,6 +92,9 @@ jobs: cp -r core dist cp -r vendor dist cp -r examples dist + dylibbundler -b -x dist/odin -d dist/libs -od -p @executable_path/libs + - name: Odin run + run: ./dist/odin run examples/demo - name: Upload artifact uses: actions/upload-artifact@v1 with: @@ -107,15 +108,13 @@ jobs: - uses: actions/checkout@v1 - name: Download LLVM and setup PATH run: | - brew install llvm@13 + brew install llvm@13 dylibbundler echo "/opt/homebrew/opt/llvm@13/bin" >> $GITHUB_PATH TMP_PATH=$(xcrun --show-sdk-path)/user/include echo "CPATH=$TMP_PATH" >> $GITHUB_ENV - name: build odin run: make nightly - - name: Odin run - run: ./odin run examples/demo - - name: Copy artifacts + - name: Bundle run: | mkdir dist cp odin dist @@ -125,6 +124,9 @@ jobs: cp -r core dist cp -r vendor dist cp -r examples dist + dylibbundler -b -x dist/odin -d dist/libs -od -p @executable_path/libs + - name: Odin run + run: ./dist/odin run examples/demo - name: Upload artifact uses: actions/upload-artifact@v1 with: From ee543a304a12716495397211a91e3fe1aebee713 Mon Sep 17 00:00:00 2001 From: Laytan Laats Date: Thu, 22 Feb 2024 20:53:56 +0100 Subject: [PATCH 13/40] Zip so compiler keeps executable permissions --- .github/workflows/nightly.yml | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/.github/workflows/nightly.yml b/.github/workflows/nightly.yml index 27d3ceceb..4da7d42f7 100644 --- a/.github/workflows/nightly.yml +++ b/.github/workflows/nightly.yml @@ -63,11 +63,13 @@ jobs: cp -r core dist cp -r vendor dist cp -r examples dist + # Zipping so executable permissions are retained, see https://github.com/actions/upload-artifact/issues/38 + zip -r dist.zip dist - name: Upload artifact uses: actions/upload-artifact@v1 with: name: ubuntu_artifacts - path: dist + path: dist.zip build_macos: name: MacOS Build if: github.repository == 'odin-lang/Odin' @@ -93,13 +95,15 @@ jobs: cp -r vendor dist cp -r examples dist dylibbundler -b -x dist/odin -d dist/libs -od -p @executable_path/libs + # Zipping so executable permissions are retained, see https://github.com/actions/upload-artifact/issues/38 + zip -r dist.zip dist - name: Odin run run: ./dist/odin run examples/demo - name: Upload artifact uses: actions/upload-artifact@v1 with: name: macos_artifacts - path: dist + path: dist.zip build_macos_arm: name: MacOS ARM Build if: github.repository == 'odin-lang/Odin' @@ -125,13 +129,15 @@ jobs: cp -r vendor dist cp -r examples dist dylibbundler -b -x dist/odin -d dist/libs -od -p @executable_path/libs + # Zipping so executable permissions are retained, see https://github.com/actions/upload-artifact/issues/38 + zip -r dist.zip dist - name: Odin run run: ./dist/odin run examples/demo - name: Upload artifact uses: actions/upload-artifact@v1 with: name: macos_arm_artifacts - path: dist + path: dist.zip upload_b2: runs-on: [ubuntu-latest] needs: [build_windows, build_macos, build_macos_arm, build_ubuntu] From a642ea0b28f8b1edad247b484ae000f20218347d Mon Sep 17 00:00:00 2001 From: gingerBill Date: Fri, 23 Feb 2024 11:38:23 +0000 Subject: [PATCH 14/40] Add `intrinsics.type_bit_set_backing_type` --- src/check_builtin.cpp | 20 ++++++++++++++++++++ src/checker_builtin_procs.hpp | 4 ++++ 2 files changed, 24 insertions(+) diff --git a/src/check_builtin.cpp b/src/check_builtin.cpp index d39be37a9..c85fb28d6 100644 --- a/src/check_builtin.cpp +++ b/src/check_builtin.cpp @@ -5820,6 +5820,26 @@ gb_internal bool check_builtin_procedure(CheckerContext *c, Operand *operand, As } break; + case BuiltinProc_type_bit_set_backing_type: + { + Operand op = {}; + Type *type = check_type(c, ce->args[0]); + Type *bt = base_type(type); + if (bt == nullptr || bt == t_invalid) { + error(ce->args[0], "Expected a type for '%.*s'", LIT(builtin_name)); + return false; + } + if (bt->kind != Type_BitSet) { + gbString s = type_to_string(type); + error(ce->args[0], "Expected a bit_set type for '%.*s', got %s", LIT(builtin_name), s); + return false; + } + + operand->mode = Addressing_Type; + operand->type = bit_set_to_int(bt); + break; + } + case BuiltinProc_type_equal_proc: { Operand op = {}; diff --git a/src/checker_builtin_procs.hpp b/src/checker_builtin_procs.hpp index 42ffa6938..c15ec7137 100644 --- a/src/checker_builtin_procs.hpp +++ b/src/checker_builtin_procs.hpp @@ -282,6 +282,8 @@ BuiltinProc__type_simple_boolean_end, BuiltinProc_type_field_index_of, + BuiltinProc_type_bit_set_backing_type, + BuiltinProc_type_equal_proc, BuiltinProc_type_hasher_proc, BuiltinProc_type_map_info, @@ -586,6 +588,8 @@ gb_global BuiltinProc builtin_procs[BuiltinProc_COUNT] = { {STR_LIT("type_field_index_of"), 2, false, Expr_Expr, BuiltinProcPkg_intrinsics}, + {STR_LIT("type_bit_set_backing_type"), 1, false, Expr_Expr, BuiltinProcPkg_intrinsics}, + {STR_LIT("type_equal_proc"), 1, false, Expr_Expr, BuiltinProcPkg_intrinsics}, {STR_LIT("type_hasher_proc"), 1, false, Expr_Expr, BuiltinProcPkg_intrinsics}, {STR_LIT("type_map_info"), 1, false, Expr_Expr, BuiltinProcPkg_intrinsics}, From 9251e06143af437d919c5b9f639b746905708303 Mon Sep 17 00:00:00 2001 From: Yawning Angel Date: Fri, 16 Feb 2024 18:55:26 +0900 Subject: [PATCH 15/40] tests/core: Bring the Makefile more in-sync with build.bat --- .gitignore | 1 + tests/core/Makefile | 50 +++++++++++-------- tests/core/c/libc/test_core_libc.odin | 1 - .../core/text/match/test_core_text_match.odin | 7 ++- 4 files changed, 34 insertions(+), 25 deletions(-) diff --git a/.gitignore b/.gitignore index a5ddfe670..228f006a3 100644 --- a/.gitignore +++ b/.gitignore @@ -28,6 +28,7 @@ tests/internal/test_map tests/internal/test_pow tests/internal/test_rtti tests/core/test_core_compress +tests/core/test_core_container tests/core/test_core_filepath tests/core/test_core_fmt tests/core/test_core_i18n diff --git a/tests/core/Makefile b/tests/core/Makefile index 35321696f..1207eeec5 100644 --- a/tests/core/Makefile +++ b/tests/core/Makefile @@ -1,8 +1,11 @@ ODIN=../../odin PYTHON=$(shell which python3) +COMMON=-vet -strict-style +COLLECTION=-collection:tests=.. all: c_libc_test \ compress_test \ + container_test \ crypto_test \ download_test_assets \ encoding_test \ @@ -27,64 +30,67 @@ download_test_assets: $(PYTHON) download_assets.py image_test: - $(ODIN) run image/test_core_image.odin -file -out:test_core_image + $(ODIN) run image $(COMMON) -out:test_core_image compress_test: - $(ODIN) run compress/test_core_compress.odin -file -out:test_core_compress + $(ODIN) run compress $(COMMON) -out:test_core_compress + +container_test: + $(ODIN) run container $(COMMON) $(COLLECTION) -out:test_core_container strings_test: - $(ODIN) run strings/test_core_strings.odin -file -out:test_core_strings + $(ODIN) run strings $(COMMON) -out:test_core_strings hash_test: - $(ODIN) run hash -o:speed -no-bounds-check -out:test_hash + $(ODIN) run hash $(COMMON) -o:speed -no-bounds-check -out:test_hash crypto_test: - $(ODIN) run crypto -o:speed -no-bounds-check -out:test_crypto + $(ODIN) run crypto $(COMMON) -o:speed -no-bounds-check -out:test_crypto noise_test: - $(ODIN) run math/noise -out:test_noise + $(ODIN) run math/noise $(COMMON) -out:test_noise encoding_test: - $(ODIN) run encoding/hxa -out:test_hxa -collection:tests=.. - $(ODIN) run encoding/json -out:test_json - $(ODIN) run encoding/varint -out:test_varint - $(ODIN) run encoding/xml -out:test_xml + $(ODIN) run encoding/hxa $(COMMON) $(COLLECTION) -out:test_hxa + $(ODIN) run encoding/json $(COMMON) -out:test_json + $(ODIN) run encoding/varint $(COMMON) -out:test_varint + $(ODIN) run encoding/xml $(COMMON) -out:test_xml math_test: - $(ODIN) run math/test_core_math.odin -file -collection:tests=.. -out:test_core_math + $(ODIN) run math $(COMMON) $(COLLECTION) -out:test_core_math linalg_glsl_math_test: - $(ODIN) run math/linalg/glsl/test_linalg_glsl_math.odin -file -collection:tests=.. -out:test_linalg_glsl_math + $(ODIN) run math/linalg/glsl $(COMMON) $(COLLECTION) -out:test_linalg_glsl_math filepath_test: - $(ODIN) run path/filepath/test_core_filepath.odin -file -collection:tests=.. -out:test_core_filepath + $(ODIN) run path/filepath $(COMMON) $(COLLECTION) -out:test_core_filepath reflect_test: - $(ODIN) run reflect/test_core_reflect.odin -file -collection:tests=.. -out:test_core_reflect + $(ODIN) run reflect $(COMMON) $(COLLECTION) -out:test_core_reflect slice_test: - $(ODIN) run slice/test_core_slice.odin -file -out:test_core_slice + $(ODIN) run slice $(COMMON) -out:test_core_slice os_exit_test: $(ODIN) run os/test_core_os_exit.odin -file -out:test_core_os_exit && exit 1 || exit 0 i18n_test: - $(ODIN) run text/i18n -out:test_core_i18n + $(ODIN) run text/i18n $(COMMON) -out:test_core_i18n match_test: - $(ODIN) run text/match -out:test_core_match + $(ODIN) run text/match $(COMMON) -out:test_core_match c_libc_test: - $(ODIN) run c/libc -out:test_core_libc + $(ODIN) run c/libc $(COMMON) -out:test_core_libc net_test: - $(ODIN) run net -out:test_core_net + $(ODIN) run net $(COMMON) -out:test_core_net fmt_test: - $(ODIN) run fmt -out:test_core_fmt + $(ODIN) run fmt $(COMMON) -out:test_core_fmt thread_test: - $(ODIN) run thread -out:test_core_thread + $(ODIN) run thread $(COMMON) -out:test_core_thread runtime_test: - $(ODIN) run runtime -out:test_core_runtime + $(ODIN) run runtime $(COMMON) -out:test_core_runtime diff --git a/tests/core/c/libc/test_core_libc.odin b/tests/core/c/libc/test_core_libc.odin index 6ad37ac6d..9b5014dee 100644 --- a/tests/core/c/libc/test_core_libc.odin +++ b/tests/core/c/libc/test_core_libc.odin @@ -2,7 +2,6 @@ package test_core_libc import "core:fmt" import "core:os" -import "core:strings" import "core:testing" TEST_count := 0 diff --git a/tests/core/text/match/test_core_text_match.odin b/tests/core/text/match/test_core_text_match.odin index 79defb849..b72190f78 100644 --- a/tests/core/text/match/test_core_text_match.odin +++ b/tests/core/text/match/test_core_text_match.odin @@ -202,8 +202,11 @@ test_captures :: proc(t: ^testing.T) { // match all captures compare_captures :: proc(t: ^testing.T, test: ^Temp, haystack: string, comp: []string, loc := #caller_location) { length, err := match.find_aux(haystack, test.pattern, 0, false, &test.captures) - if failed(t, len(comp) == length) { - logf(t, "Captures Compare Failed -> Lengths %d != %d\n", len(comp), length) + result := len(comp) == length && err == .OK + if failed(t, result == true) { + logf(t, "Captures Compare Failed!\n") + logf(t, "\tErr: %v\n", err) + logf(t, "\tLengths: %v != %v\n", len(comp), length) } for i in 0.. Date: Fri, 16 Feb 2024 18:58:02 +0900 Subject: [PATCH 16/40] test/core/container: Refactor for multiple container types --- tests/core/container/test_core_container.odin | 25 ++++++++++++++++ .../core/container/test_core_small_array.odin | 30 +++++++------------ 2 files changed, 35 insertions(+), 20 deletions(-) create mode 100644 tests/core/container/test_core_container.odin diff --git a/tests/core/container/test_core_container.odin b/tests/core/container/test_core_container.odin new file mode 100644 index 000000000..9065bed2c --- /dev/null +++ b/tests/core/container/test_core_container.odin @@ -0,0 +1,25 @@ +package test_core_container + +import "core:fmt" +import "core:testing" + +import tc "tests:common" + +expect_equal :: proc(t: ^testing.T, the_slice, expected: []int, loc := #caller_location) { + _eq :: proc(a, b: []int) -> bool { + if len(a) != len(b) do return false + for a, i in a { + if b[i] != a do return false + } + return true + } + tc.expect(t, _eq(the_slice, expected), fmt.tprintf("Expected %v, got %v\n", the_slice, expected), loc) +} + +main :: proc() { + t := testing.T{} + + test_small_array(&t) + + tc.report(&t) +} diff --git a/tests/core/container/test_core_small_array.odin b/tests/core/container/test_core_small_array.odin index 88bc8e532..78998de16 100644 --- a/tests/core/container/test_core_small_array.odin +++ b/tests/core/container/test_core_small_array.odin @@ -1,29 +1,19 @@ -package test_core_compress +package test_core_container -import "core:fmt" import "core:testing" import "core:container/small_array" + import tc "tests:common" -main :: proc() { - t := testing.T{} - test_small_array_removes(&t) - test_small_array_inject_at(&t) - tc.report(&t) +@(test) +test_small_array :: proc(t: ^testing.T) { + tc.log(t, "Testing small_array") + + test_small_array_removes(t) + test_small_array_inject_at(t) } -expect_equal :: proc(t: ^testing.T, the_slice, expected: []int, loc := #caller_location) { - _eq :: proc(a, b: []int) -> bool { - if len(a) != len(b) do return false - for a, i in a { - if b[i] != a do return false - } - return true - } - tc.expect(t, _eq(the_slice, expected), fmt.tprintf("Expected %v, got %v\n", the_slice, expected), loc) -} - -@test +@(test) test_small_array_removes :: proc(t: ^testing.T) { array: small_array.Small_Array(10, int) small_array.append(&array, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9) @@ -42,7 +32,7 @@ test_small_array_removes :: proc(t: ^testing.T) { expect_equal(t, small_array.slice(&array), []int { 9, 2, 7, 4 }) } -@test +@(test) test_small_array_inject_at :: proc(t: ^testing.T) { array: small_array.Small_Array(13, int) small_array.append(&array, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9) From 874d6ccb6078078e7554bc40acebbb86e6e8ee7c Mon Sep 17 00:00:00 2001 From: Yawning Angel Date: Fri, 16 Feb 2024 18:58:41 +0900 Subject: [PATCH 17/40] core/container/avl: Initial import --- core/container/avl/avl.odin | 678 ++++++++++++++++++ examples/all/all_main.odin | 2 + tests/core/container/test_core_avl.odin | 161 +++++ tests/core/container/test_core_container.odin | 1 + 4 files changed, 842 insertions(+) create mode 100644 core/container/avl/avl.odin create mode 100644 tests/core/container/test_core_avl.odin diff --git a/core/container/avl/avl.odin b/core/container/avl/avl.odin new file mode 100644 index 000000000..eecc1b756 --- /dev/null +++ b/core/container/avl/avl.odin @@ -0,0 +1,678 @@ +/* +package avl implements an AVL tree. + +The implementation is non-intrusive, and non-recursive. +*/ +package container_avl + +import "base:intrinsics" +import "base:runtime" +import "core:slice" + +_ :: intrinsics +_ :: runtime + +// Originally based on the CC0 implementation by Eric Biggers +// See: https://github.com/ebiggers/avl_tree/ + +// Direction specifies the traversal direction for a tree iterator. +Direction :: enum i8 { + // Backward is the in-order backwards direction. + Backward = -1, + // Forward is the in-order forwards direction. + Forward = 1, +} + +// Ordering specifies order when inserting/finding values into the tree. +Ordering :: slice.Ordering + +// Tree is an AVL tree. +Tree :: struct($Value: typeid) { + // user_data is a parameter that will be passed to the on_remove + // callback. + user_data: rawptr, + // on_remove is an optional callback that can be called immediately + // after a node is removed from the tree. + on_remove: proc(value: Value, user_data: rawptr), + + _root: ^Node(Value), + _node_allocator: runtime.Allocator, + _cmp_fn: proc(a, b: Value) -> Ordering, + _size: int, +} + +// Node is an AVL tree node. +// +// WARNING: It is unsafe to mutate value if the node is part of a tree +// if doing so will alter the Node's sort position relative to other +// elements in the tree. +Node :: struct($Value: typeid) { + value: Value, + + _parent: ^Node(Value), + _left: ^Node(Value), + _right: ^Node(Value), + _balance: i8, +} + +// Iterator is a tree iterator. +// +// WARNING: It is unsafe to modify the tree while iterating, except via +// the iterator_remove method. +Iterator :: struct($Value: typeid) { + _tree: ^Tree(Value), + _cur: ^Node(Value), + _next: ^Node(Value), + _direction: Direction, + _called_next: bool, +} + +// init initializes a tree. +init :: proc { + init_ordered, + init_cmp, +} + +// init_cmp initializes a tree. +init_cmp :: proc( + t: ^$T/Tree($Value), + cmp_fn: proc(a, b: Value) -> Ordering, + node_allocator := context.allocator, +) { + t._root = nil + t._node_allocator = node_allocator + t._cmp_fn = cmp_fn + t._size = 0 +} + +// init_ordered initializes a tree containing ordered items, with +// a comparison function that results in an ascending order sort. +init_ordered :: proc( + t: ^$T/Tree($Value), + node_allocator := context.allocator, +) where intrinsics.type_is_ordered_numeric(Value) { + init_cmp(t, slice.cmp_proc(Value), node_allocator) +} + +// destroy de-initializes a tree. +destroy :: proc(t: ^$T/Tree($Value), call_on_remove: bool = true) { + iter := iterator(t, Direction.Forward) + for _ in iterator_next(&iter) { + iterator_remove(&iter, call_on_remove) + } +} + +// len returns the number of elements in the tree. +len :: proc "contextless" (t: ^$T/Tree($Value)) -> int { + return t._size +} + +// first returns the first node in the tree (in-order) or nil iff +// the tree is empty. +first :: proc "contextless" (t: ^$T/Tree($Value)) -> ^Node(Value) { + return tree_first_or_last_in_order(t, Direction.Backward) +} + +// last returns the last element in the tree (in-order) or nil iff +// the tree is empty. +last :: proc "contextless" (t: ^$T/Tree($Value)) -> ^Node(Value) { + return tree_first_or_last_in_order(t, Direction.Forward) +} + +// find finds the value in the tree, and returns the corresponding +// node or nil iff the value is not present. +find :: proc(t: ^$T/Tree($Value), value: Value) -> ^Node(Value) { + cur := t._root + descend_loop: for cur != nil { + switch t._cmp_fn(value, cur.value) { + case .Less: + cur = cur._left + case .Greater: + cur = cur._right + case .Equal: + break descend_loop + } + } + + return cur +} + +// find_or_insert attempts to insert the value into the tree, and returns +// the node, a boolean indicating if the value was inserted, and the +// node allocator error if relevant. If the value is already +// present, the existing node is returned un-altered. +find_or_insert :: proc( + t: ^$T/Tree($Value), + value: Value, +) -> ( + n: ^Node(Value), + inserted: bool, + err: runtime.Allocator_Error, +) { + n_ptr := &t._root + for n_ptr^ != nil { + n = n_ptr^ + switch t._cmp_fn(value, n.value) { + case .Less: + n_ptr = &n._left + case .Greater: + n_ptr = &n._right + case .Equal: + return + } + } + + parent := n + n = new(Node(Value), t._node_allocator) or_return + n.value = value + n._parent = parent + n_ptr^ = n + tree_rebalance_after_insert(t, n) + + t._size += 1 + inserted = true + + return +} + +// remove removes a node or value from the tree, and returns true iff the +// removal was successful. While the node's value will be left intact, +// the node itself will be freed via the tree's node allocator. +remove :: proc { + remove_value, + remove_node, +} + +// remove_value removes a value from the tree, and returns true iff the +// removal was successful. While the node's value will be left intact, +// the node itself will be freed via the tree's node allocator. +remove_value :: proc(t: ^$T/Tree($Value), value: Value, call_on_remove: bool = true) -> bool { + n := find(t, value) + if n == nil { + return false + } + return remove_node(t, n, call_on_remove) +} + +// remove_node removes a node from the tree, and returns true iff the +// removal was successful. While the node's value will be left intact, +// the node itself will be freed via the tree's node allocator. +remove_node :: proc(t: ^$T/Tree($Value), node: ^Node(Value), call_on_remove: bool = true) -> bool { + if node._parent == node || (node._parent == nil && t._root != node) { + return false + } + defer { + if call_on_remove && t.on_remove != nil { + t.on_remove(node.value, t.user_data) + } + free(node, t._node_allocator) + } + + parent: ^Node(Value) + left_deleted: bool + + t._size -= 1 + if node._left != nil && node._right != nil { + parent, left_deleted = tree_swap_with_successor(t, node) + } else { + child := node._left + if child == nil { + child = node._right + } + parent = node._parent + if parent != nil { + if node == parent._left { + parent._left = child + left_deleted = true + } else { + parent._right = child + left_deleted = false + } + if child != nil { + child._parent = parent + } + } else { + if child != nil { + child._parent = parent + } + t._root = child + node_reset(node) + return true + } + } + + for { + if left_deleted { + parent = tree_handle_subtree_shrink(t, parent, +1, &left_deleted) + } else { + parent = tree_handle_subtree_shrink(t, parent, -1, &left_deleted) + } + if parent == nil { + break + } + } + node_reset(node) + + return true +} + +// iterator returns a tree iterator in the specified direction. +iterator :: proc "contextless" (t: ^$T/Tree($Value), direction: Direction) -> Iterator(Value) { + it: Iterator(Value) + it._tree = transmute(^Tree(Value))t + it._direction = direction + + iterator_first(&it) + + return it +} + +// iterator_from_pos returns a tree iterator in the specified direction, +// spanning the range [pos, last] (inclusive). +iterator_from_pos :: proc "contextless" ( + t: ^$T/Tree($Value), + pos: ^Node(Value), + direction: Direction, +) -> Iterator(Value) { + it: Iterator(Value) + it._tree = transmute(^Tree(Value))t + it._direction = direction + it._next = nil + it._called_next = false + + if it._cur = pos; pos != nil { + it._next = node_next_or_prev_in_order(it._cur, it._direction) + } + + return it +} + +// iterator_get returns the node currently pointed to by the iterator, +// or nil iff the node has been removed, the tree is empty, or the end +// of the tree has been reached. +iterator_get :: proc "contextless" (it: ^$I/Iterator($Value)) -> ^Node(Value) { + return it._cur +} + +// iterator_remove removes the node currently pointed to by the iterator, +// and returns true iff the removal was successful. Semantics are the +// same as the Tree remove. +iterator_remove :: proc(it: ^$I/Iterator($Value), call_on_remove: bool = true) -> bool { + if it._cur == nil { + return false + } + + ok := remove_node(it._tree, it._cur, call_on_remove) + if ok { + it._cur = nil + } + + return ok +} + +// iterator_next advances the iterator and returns the (node, true) or +// or (nil, false) iff the end of the tree has been reached. +// +// Note: The first call to iterator_next will return the first node instead +// of advancing the iterator. +iterator_next :: proc "contextless" (it: ^$I/Iterator($Value)) -> (^Node(Value), bool) { + // This check is needed so that the first element gets returned from + // a brand-new iterator, and so that the somewhat contrived case where + // iterator_remove is called before the first call to iterator_next + // returns the correct value. + if !it._called_next { + it._called_next = true + + // There can be the contrived case where iterator_remove is + // called before ever calling iterator_next, which needs to be + // handled as an actual call to next. + // + // If this happens it._cur will be nil, so only return the + // first value, if it._cur is valid. + if it._cur != nil { + return it._cur, true + } + } + + if it._next == nil { + return nil, false + } + + it._cur = it._next + it._next = node_next_or_prev_in_order(it._cur, it._direction) + + return it._cur, true +} + +@(private) +tree_first_or_last_in_order :: proc "contextless" ( + t: ^$T/Tree($Value), + direction: Direction, +) -> ^Node(Value) { + first, sign := t._root, i8(direction) + if first != nil { + for { + tmp := node_get_child(first, +sign) + if tmp == nil { + break + } + first = tmp + } + } + + return first +} + +@(private) +tree_replace_child :: proc "contextless" ( + t: ^$T/Tree($Value), + parent, old_child, new_child: ^Node(Value), +) { + if parent != nil { + if old_child == parent._left { + parent._left = new_child + } else { + parent._right = new_child + } + } else { + t._root = new_child + } +} + +@(private) +tree_rotate :: proc "contextless" (t: ^$T/Tree($Value), a: ^Node(Value), sign: i8) { + b := node_get_child(a, -sign) + e := node_get_child(b, +sign) + p := a._parent + + node_set_child(a, -sign, e) + a._parent = b + + node_set_child(b, +sign, a) + b._parent = p + + if e != nil { + e._parent = a + } + + tree_replace_child(t, p, a, b) +} + +@(private) +tree_double_rotate :: proc "contextless" ( + t: ^$T/Tree($Value), + b, a: ^Node(Value), + sign: i8, +) -> ^Node(Value) { + e := node_get_child(b, +sign) + f := node_get_child(e, -sign) + g := node_get_child(e, +sign) + p := a._parent + e_bal := e._balance + + node_set_child(a, -sign, g) + a_bal := -e_bal + if sign * e_bal >= 0 { + a_bal = 0 + } + node_set_parent_balance(a, e, a_bal) + + node_set_child(b, +sign, f) + b_bal := -e_bal + if sign * e_bal <= 0 { + b_bal = 0 + } + node_set_parent_balance(b, e, b_bal) + + node_set_child(e, +sign, a) + node_set_child(e, -sign, b) + node_set_parent_balance(e, p, 0) + + if g != nil { + g._parent = a + } + + if f != nil { + f._parent = b + } + + tree_replace_child(t, p, a, e) + + return e +} + +@(private) +tree_handle_subtree_growth :: proc "contextless" ( + t: ^$T/Tree($Value), + node, parent: ^Node(Value), + sign: i8, +) -> bool { + old_balance_factor := parent._balance + if old_balance_factor == 0 { + node_adjust_balance_factor(parent, sign) + return false + } + + new_balance_factor := old_balance_factor + sign + if new_balance_factor == 0 { + node_adjust_balance_factor(parent, sign) + return true + } + + if sign * node._balance > 0 { + tree_rotate(t, parent, -sign) + node_adjust_balance_factor(parent, -sign) + node_adjust_balance_factor(node, -sign) + } else { + tree_double_rotate(t, node, parent, -sign) + } + + return true +} + +@(private) +tree_rebalance_after_insert :: proc "contextless" (t: ^$T/Tree($Value), inserted: ^Node(Value)) { + node, parent := inserted, inserted._parent + switch { + case parent == nil: + return + case node == parent._left: + node_adjust_balance_factor(parent, -1) + case: + node_adjust_balance_factor(parent, +1) + } + + if parent._balance == 0 { + return + } + + for done := false; !done; { + node = parent + if parent = node._parent; parent == nil { + return + } + + if node == parent._left { + done = tree_handle_subtree_growth(t, node, parent, -1) + } else { + done = tree_handle_subtree_growth(t, node, parent, +1) + } + } +} + +@(private) +tree_swap_with_successor :: proc "contextless" ( + t: ^$T/Tree($Value), + x: ^Node(Value), +) -> ( + ^Node(Value), + bool, +) { + ret: ^Node(Value) + left_deleted: bool + + y := x._right + if y._left == nil { + ret = y + } else { + q: ^Node(Value) + + for { + q = y + if y = y._left; y._left == nil { + break + } + } + + if q._left = y._right; q._left != nil { + q._left._parent = q + } + y._right = x._right + x._right._parent = y + ret = q + left_deleted = true + } + + y._left = x._left + x._left._parent = y + + y._parent = x._parent + y._balance = x._balance + + tree_replace_child(t, x._parent, x, y) + + return ret, left_deleted +} + +@(private) +tree_handle_subtree_shrink :: proc "contextless" ( + t: ^$T/Tree($Value), + parent: ^Node(Value), + sign: i8, + left_deleted: ^bool, +) -> ^Node(Value) { + old_balance_factor := parent._balance + if old_balance_factor == 0 { + node_adjust_balance_factor(parent, sign) + return nil + } + + node: ^Node(Value) + new_balance_factor := old_balance_factor + sign + if new_balance_factor == 0 { + node_adjust_balance_factor(parent, sign) + node = parent + } else { + node = node_get_child(parent, sign) + if sign * node._balance >= 0 { + tree_rotate(t, parent, -sign) + if node._balance == 0 { + node_adjust_balance_factor(node, -sign) + return nil + } + node_adjust_balance_factor(parent, -sign) + node_adjust_balance_factor(node, -sign) + } else { + node = tree_double_rotate(t, node, parent, -sign) + } + } + + parent := parent + if parent = node._parent; parent != nil { + left_deleted^ = node == parent._left + } + return parent +} + +@(private) +node_reset :: proc "contextless" (n: ^Node($Value)) { + // Mostly pointless as n will be deleted after this is called, but + // attempt to be able to catch cases of n not being in the tree. + n._parent = n + n._left = nil + n._right = nil + n._balance = 0 +} + +@(private) +node_set_parent_balance :: #force_inline proc "contextless" ( + n, parent: ^Node($Value), + balance: i8, +) { + n._parent = parent + n._balance = balance +} + +@(private) +node_get_child :: #force_inline proc "contextless" (n: ^Node($Value), sign: i8) -> ^Node(Value) { + if sign < 0 { + return n._left + } + return n._right +} + +@(private) +node_next_or_prev_in_order :: proc "contextless" ( + n: ^Node($Value), + direction: Direction, +) -> ^Node(Value) { + next, tmp: ^Node(Value) + sign := i8(direction) + + if next = node_get_child(n, +sign); next != nil { + for { + tmp = node_get_child(next, -sign) + if tmp == nil { + break + } + next = tmp + } + } else { + tmp, next = n, n._parent + for next != nil && tmp == node_get_child(next, +sign) { + tmp, next = next, next._parent + } + } + return next +} + +@(private) +node_set_child :: #force_inline proc "contextless" ( + n: ^Node($Value), + sign: i8, + child: ^Node(Value), +) { + if sign < 0 { + n._left = child + } else { + n._right = child + } +} + +@(private) +node_adjust_balance_factor :: #force_inline proc "contextless" (n: ^Node($Value), amount: i8) { + n._balance += amount +} + +@(private) +iterator_first :: proc "contextless" (it: ^Iterator($Value)) { + // This is private because behavior when the user manually calls + // iterator_first followed by iterator_next is unintuitive, since + // the first call to iterator_next MUST return the first node + // instead of advancing so that `for node in iterator_next(&next)` + // works as expected. + + switch it._direction { + case .Forward: + it._cur = tree_first_or_last_in_order(it._tree, .Backward) + case .Backward: + it._cur = tree_first_or_last_in_order(it._tree, .Forward) + } + + it._next = nil + it._called_next = false + + if it._cur != nil { + it._next = node_next_or_prev_in_order(it._cur, it._direction) + } +} diff --git a/examples/all/all_main.odin b/examples/all/all_main.odin index 8f2eebc8f..fff344b22 100644 --- a/examples/all/all_main.odin +++ b/examples/all/all_main.odin @@ -14,6 +14,7 @@ import shoco "core:compress/shoco" import gzip "core:compress/gzip" import zlib "core:compress/zlib" +import avl "core:container/avl" import bit_array "core:container/bit_array" import priority_queue "core:container/priority_queue" import queue "core:container/queue" @@ -131,6 +132,7 @@ _ :: compress _ :: shoco _ :: gzip _ :: zlib +_ :: avl _ :: bit_array _ :: priority_queue _ :: queue diff --git a/tests/core/container/test_core_avl.odin b/tests/core/container/test_core_avl.odin new file mode 100644 index 000000000..f6343c5ea --- /dev/null +++ b/tests/core/container/test_core_avl.odin @@ -0,0 +1,161 @@ +package test_core_container + +import "core:container/avl" +import "core:math/rand" +import "core:slice" +import "core:testing" + +import tc "tests:common" + +@(test) +test_avl :: proc(t: ^testing.T) { + tc.log(t, "Testing avl") + + // Initialization. + tree: avl.Tree(int) + avl.init(&tree, slice.cmp_proc(int)) + tc.expect(t, avl.len(&tree) == 0, "empty: len should be 0") + tc.expect(t, avl.first(&tree) == nil, "empty: first should be nil") + tc.expect(t, avl.last(&tree) == nil, "empty: last should be nil") + + iter := avl.iterator(&tree, avl.Direction.Forward) + tc.expect(t, avl.iterator_get(&iter) == nil, "empty/iterator: first node should be nil") + + // Test insertion. + NR_INSERTS :: 32 + 1 // Ensure at least 1 collision. + inserted_map := make(map[int]^avl.Node(int)) + for i := 0; i < NR_INSERTS; i += 1 { + v := int(rand.uint32() & 0x1f) + existing_node, in_map := inserted_map[v] + + n, ok, _ := avl.find_or_insert(&tree, v) + tc.expect(t, in_map != ok, "insert: ok should match inverse of map lookup") + if ok { + inserted_map[v] = n + } else { + tc.expect(t, existing_node == n, "insert: expecting existing node") + } + } + nrEntries := len(inserted_map) + tc.expect(t, avl.len(&tree) == nrEntries, "insert: len after") + tree_validate(t, &tree) + + // Ensure that all entries can be found. + for k, v in inserted_map { + tc.expect(t, v == avl.find(&tree, k), "Find(): Node") + tc.expect(t, k == v.value, "Find(): Node value") + } + + // Test the forward/backward iterators. + inserted_values: [dynamic]int + for k in inserted_map { + append(&inserted_values, k) + } + slice.sort(inserted_values[:]) + + iter = avl.iterator(&tree, avl.Direction.Forward) + visited: int + for node in avl.iterator_next(&iter) { + v, idx := node.value, visited + tc.expect(t, inserted_values[idx] == v, "iterator/forward: value") + tc.expect(t, node == avl.iterator_get(&iter), "iterator/forward: get") + visited += 1 + } + tc.expect(t, visited == nrEntries, "iterator/forward: visited") + + slice.reverse(inserted_values[:]) + iter = avl.iterator(&tree, avl.Direction.Backward) + visited = 0 + for node in avl.iterator_next(&iter) { + v, idx := node.value, visited + tc.expect(t, inserted_values[idx] == v, "iterator/backward: value") + visited += 1 + } + tc.expect(t, visited == nrEntries, "iterator/backward: visited") + + // Test removal. + rand.shuffle(inserted_values[:]) + for v, i in inserted_values { + node := avl.find(&tree, v) + tc.expect(t, node != nil, "remove: find (pre)") + + ok := avl.remove(&tree, v) + tc.expect(t, ok, "remove: succeeds") + tc.expect(t, nrEntries - (i + 1) == avl.len(&tree), "remove: len (post)") + tree_validate(t, &tree) + + tc.expect(t, nil == avl.find(&tree, v), "remove: find (post") + } + tc.expect(t, avl.len(&tree) == 0, "remove: len should be 0") + tc.expect(t, avl.first(&tree) == nil, "remove: first should be nil") + tc.expect(t, avl.last(&tree) == nil, "remove: last should be nil") + + // Refill the tree. + for v in inserted_values { + avl.find_or_insert(&tree, v) + } + + // Test that removing the node doesn't break the iterator. + iter = avl.iterator(&tree, avl.Direction.Forward) + if node := avl.iterator_get(&iter); node != nil { + v := node.value + + ok := avl.iterator_remove(&iter) + tc.expect(t, ok, "iterator/remove: success") + + ok = avl.iterator_remove(&iter) + tc.expect(t, !ok, "iterator/remove: redundant removes should fail") + + tc.expect(t, avl.find(&tree, v) == nil, "iterator/remove: node should be gone") + tc.expect(t, avl.iterator_get(&iter) == nil, "iterator/remove: get should return nil") + + // Ensure that iterator_next still works. + node, ok = avl.iterator_next(&iter) + tc.expect(t, ok == (avl.len(&tree) > 0), "iterator/remove: next should return false") + tc.expect(t, node == avl.first(&tree), "iterator/remove: next should return first") + + tree_validate(t, &tree) + } + tc.expect(t, avl.len(&tree) == nrEntries - 1, "iterator/remove: len should drop by 1") + + avl.destroy(&tree) + tc.expect(t, avl.len(&tree) == 0, "destroy: len should be 0") +} + +@(private) +tree_validate :: proc(t: ^testing.T, tree: ^avl.Tree($Value)) { + tree_check_invariants(t, tree, tree._root, nil) +} + +@(private) +tree_check_invariants :: proc( + t: ^testing.T, + tree: ^avl.Tree($Value), + node, parent: ^avl.Node(Value), +) -> int { + if node == nil { + return 0 + } + + // Validate the parent pointer. + tc.expect(t, parent == node._parent, "invalid parent pointer") + + // Validate that the balance factor is -1, 0, 1. + tc.expect( + t, + node._balance == -1 || node._balance == 0 || node._balance == 1, + "invalid balance factor", + ) + + // Recursively derive the height of the left and right sub-trees. + l_height := tree_check_invariants(t, tree, node._left, node) + r_height := tree_check_invariants(t, tree, node._right, node) + + // Validate the AVL invariant and the balance factor. + tc.expect(t, int(node._balance) == r_height - l_height, "AVL balance factor invariant violated") + if l_height > r_height { + return l_height + 1 + } + + return r_height + 1 +} diff --git a/tests/core/container/test_core_container.odin b/tests/core/container/test_core_container.odin index 9065bed2c..f816a6bcb 100644 --- a/tests/core/container/test_core_container.odin +++ b/tests/core/container/test_core_container.odin @@ -19,6 +19,7 @@ expect_equal :: proc(t: ^testing.T, the_slice, expected: []int, loc := #caller_l main :: proc() { t := testing.T{} + test_avl(&t) test_small_array(&t) tc.report(&t) From 8a78b0d241df4c5533343ffdf543791754afadde Mon Sep 17 00:00:00 2001 From: Eddie Woodley Date: Sat, 24 Feb 2024 23:02:12 +0000 Subject: [PATCH 18/40] vendor/glfw Add RawMouseMotionSupported to wrapper --- vendor/glfw/wrapper.odin | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/vendor/glfw/wrapper.odin b/vendor/glfw/wrapper.odin index db0e8364e..6ef46c183 100644 --- a/vendor/glfw/wrapper.odin +++ b/vendor/glfw/wrapper.odin @@ -149,8 +149,9 @@ WaitEvents :: glfw.WaitEvents WaitEventsTimeout :: glfw.WaitEventsTimeout PostEmptyEvent :: glfw.PostEmptyEvent -GetInputMode :: glfw.GetInputMode -SetInputMode :: glfw.SetInputMode +RawMouseMotionSupported :: glfw.RawMouseMotionSupported +GetInputMode :: glfw.GetInputMode +SetInputMode :: glfw.SetInputMode GetMouseButton :: glfw.GetMouseButton GetCursorPos :: proc "c" (window: WindowHandle) -> (xpos, ypos: f64) { From ae67f37fc191cddb0c9cbfa7d94ae8d2b8342616 Mon Sep 17 00:00:00 2001 From: Karl Zylinski Date: Mon, 26 Feb 2024 13:32:16 +0100 Subject: [PATCH 19/40] Fix ModelAnimation in raylib.odin missing a field. --- vendor/raylib/raylib.odin | 1 + 1 file changed, 1 insertion(+) diff --git a/vendor/raylib/raylib.odin b/vendor/raylib/raylib.odin index 7a350f6a7..c21b9f3ab 100644 --- a/vendor/raylib/raylib.odin +++ b/vendor/raylib/raylib.odin @@ -425,6 +425,7 @@ ModelAnimation :: struct { frameCount: c.int, // Number of animation frames bones: [^]BoneInfo, // Bones information (skeleton) framePoses: [^][^]Transform, // Poses array by frame + name: [32]u8, // Animation name } // Ray type (useful for raycast) From cdda8f0eb959737660117c61794bb67e52ae4df4 Mon Sep 17 00:00:00 2001 From: Karl Zylinski Date: Mon, 26 Feb 2024 17:16:45 +0100 Subject: [PATCH 20/40] Additional fixes for errors in Raylib bindings since Raylib 4 -> 5 upgrade. --- vendor/raylib/raylib.odin | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/vendor/raylib/raylib.odin b/vendor/raylib/raylib.odin index c21b9f3ab..3db4d4bbd 100644 --- a/vendor/raylib/raylib.odin +++ b/vendor/raylib/raylib.odin @@ -318,11 +318,11 @@ GlyphInfo :: struct { // Font type, includes texture and charSet array data Font :: struct { baseSize: c.int, // Base size (default chars height) - charsCount: c.int, // Number of characters - charsPadding: c.int, // Padding around the chars + glyphCount: c.int, // Number of characters + glyphPadding: c.int, // Padding around the chars texture: Texture2D, // Characters texture atlas recs: [^]Rectangle, // Characters rectangles in texture - chars: [^]GlyphInfo, // Characters info data + glyphs: [^]GlyphInfo, // Characters info data } // Camera type, defines a camera position/orientation in 3d space @@ -425,7 +425,7 @@ ModelAnimation :: struct { frameCount: c.int, // Number of animation frames bones: [^]BoneInfo, // Bones information (skeleton) framePoses: [^][^]Transform, // Poses array by frame - name: [32]u8, // Animation name + name: [32]byte, // Animation name } // Ray type (useful for raycast) @@ -491,7 +491,6 @@ VrDeviceInfo :: struct { vResolution: c.int, // Vertical resolution in pixels hScreenSize: f32, // Horizontal size in meters vScreenSize: f32, // Vertical size in meters - vScreenCenter: f32, // Screen center in meters eyeToScreenDistance: f32, // Distance between eye and display in meters lensSeparationDistance: f32, // Lens separation distance in meters interpupillaryDistance: f32, // IPD (distance between pupils) in meters From 51edf011629a424481a64f00e6aab9ca3726e389 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Tue, 27 Feb 2024 15:07:55 +0000 Subject: [PATCH 21/40] Change type info table to be initializable constantly []Type_Info -> []^Type_Info --- base/runtime/core.odin | 4 +- src/llvm_backend.cpp | 19 +- src/llvm_backend_type.cpp | 988 ++++---------------------------------- src/types.cpp | 3 + 4 files changed, 95 insertions(+), 919 deletions(-) diff --git a/base/runtime/core.odin b/base/runtime/core.odin index 2f63a7ac2..8f27ca674 100644 --- a/base/runtime/core.odin +++ b/base/runtime/core.odin @@ -280,7 +280,7 @@ Typeid_Kind :: enum u8 { // NOTE(bill): only the ones that are needed (not all types) // This will be set by the compiler -type_table: []Type_Info +type_table: []^Type_Info args__: []cstring @@ -609,7 +609,7 @@ __type_info_of :: proc "contextless" (id: typeid) -> ^Type_Info #no_bounds_check if n < 0 || n >= len(type_table) { n = 0 } - return &type_table[n] + return type_table[n] } when !ODIN_NO_RTTI { diff --git a/src/llvm_backend.cpp b/src/llvm_backend.cpp index 45d903b43..d3a2f25d2 100644 --- a/src/llvm_backend.cpp +++ b/src/llvm_backend.cpp @@ -1069,10 +1069,6 @@ gb_internal lbProcedure *lb_create_startup_type_info(lbModule *m) { // lb_add_attribute_to_proc(p->module, p->value, "norecurse"); // lb_add_attribute_to_proc(p->module, p->value, "nosync"); // lb_add_attribute_to_proc(p->module, p->value, "willreturn"); - if (!LB_USE_GIANT_PACKED_STRUCT) { - lb_add_attribute_to_proc(m, p->value, "optnone"); - lb_add_attribute_to_proc(m, p->value, "noinline"); - } lb_begin_procedure_body(p); @@ -2691,17 +2687,19 @@ gb_internal bool lb_generate_code(lbGenerator *gen) { { // Add type info data isize max_type_info_count = info->minimum_dependency_type_info_set.count+1; - Type *t = alloc_type_array(t_type_info, max_type_info_count); + Type *t = alloc_type_array(t_type_info_ptr, max_type_info_count); // IMPORTANT NOTE(bill): As LLVM does not have a union type, an array of unions cannot be initialized // at compile time without cheating in some way. This means to emulate an array of unions is to use // a giant packed struct of "corrected" data types. - LLVMTypeRef internal_llvm_type = lb_setup_type_info_data_internal_type(m, max_type_info_count); + LLVMTypeRef internal_llvm_type = lb_type(m, t); LLVMValueRef g = LLVMAddGlobal(m->mod, internal_llvm_type, LB_TYPE_INFO_DATA_NAME); LLVMSetInitializer(g, LLVMConstNull(internal_llvm_type)); LLVMSetLinkage(g, USE_SEPARATE_MODULES ? LLVMExternalLinkage : LLVMInternalLinkage); + LLVMSetUnnamedAddress(g, LLVMGlobalUnnamedAddr); + LLVMSetGlobalConstant(g, /*true*/false); lbValue value = {}; value.value = g; @@ -2710,11 +2708,6 @@ gb_internal bool lb_generate_code(lbGenerator *gen) { lb_global_type_info_data_entity = alloc_entity_variable(nullptr, make_token_ident(LB_TYPE_INFO_DATA_NAME), t, EntityState_Resolved); lb_add_entity(m, lb_global_type_info_data_entity, value); - if (LB_USE_GIANT_PACKED_STRUCT) { - LLVMSetLinkage(g, LLVMPrivateLinkage); - LLVMSetUnnamedAddress(g, LLVMGlobalUnnamedAddr); - LLVMSetGlobalConstant(g, /*true*/false); - } } { // Type info member buffer // NOTE(bill): Removes need for heap allocation by making it global memory @@ -2750,9 +2743,7 @@ gb_internal bool lb_generate_code(lbGenerator *gen) { LLVMValueRef g = LLVMAddGlobal(m->mod, lb_type(m, t), name); LLVMSetInitializer(g, LLVMConstNull(lb_type(m, t))); LLVMSetLinkage(g, LLVMInternalLinkage); - if (LB_USE_GIANT_PACKED_STRUCT) { - lb_make_global_private_const(g); - } + lb_make_global_private_const(g); return lb_addr({g, alloc_type_pointer(t)}); }; diff --git a/src/llvm_backend_type.cpp b/src/llvm_backend_type.cpp index 4952d75de..09a758827 100644 --- a/src/llvm_backend_type.cpp +++ b/src/llvm_backend_type.cpp @@ -180,16 +180,7 @@ gb_internal lbValue lb_type_info_member_tags_offset(lbModule *m, isize count, i6 return offset; } -// enum {LB_USE_GIANT_PACKED_STRUCT = LB_USE_NEW_PASS_SYSTEM}; -enum {LB_USE_GIANT_PACKED_STRUCT = 0}; - -gb_internal LLVMTypeRef lb_setup_type_info_data_internal_type(lbModule *m, isize max_type_info_count) { - if (!LB_USE_GIANT_PACKED_STRUCT) { - Type *t = alloc_type_array(t_type_info, max_type_info_count); - return lb_type(m, t); - } - CheckerInfo *info = m->gen->info; - +gb_internal LLVMTypeRef *lb_setup_modified_types_for_type_info(lbModule *m, isize max_type_info_count) { LLVMTypeRef *element_types = gb_alloc_array(heap_allocator(), LLVMTypeRef, max_type_info_count); defer (gb_free(heap_allocator(), element_types)); @@ -219,8 +210,8 @@ gb_internal LLVMTypeRef lb_setup_type_info_data_internal_type(lbModule *m, isize variant_index = 4; } - LLVMTypeRef modified_types[32] = {}; - GB_ASSERT(gb_count_of(modified_types) >= ut->Union.variants.count); + LLVMTypeRef *modified_types = gb_alloc_array(heap_allocator(), LLVMTypeRef, Typeid__COUNT); + GB_ASSERT(Typeid__COUNT == ut->Union.variants.count); modified_types[0] = element_types[0]; i64 tag_offset = ut->Union.variant_block_size; @@ -242,40 +233,24 @@ gb_internal LLVMTypeRef lb_setup_type_info_data_internal_type(lbModule *m, isize modified_types[i] = modified_type; } - for_array(type_info_type_index, info->type_info_types) { - Type *t = info->type_info_types[type_info_type_index]; - if (t == nullptr || t == t_invalid) { - continue; - } - isize entry_index = lb_type_info_index(info, t, false); - if (entry_index <= 0) { - continue; - } - - if (entries_handled[entry_index]) { - continue; - } - entries_handled[entry_index] = true; - - - if (t->kind == Type_Named) { - element_types[entry_index] = modified_types[0]; - } else { - i64 variant_index = lb_typeid_kind(m, t); - element_types[entry_index] = modified_types[variant_index]; - } - - GB_ASSERT(element_types[entry_index] != nullptr); + for (isize i = 0; i < Typeid__COUNT; i++) { + GB_ASSERT_MSG(modified_types[i] != nullptr, "%td", ut->Union.variants.count); } - for_array(i, entries_handled) { - GB_ASSERT(entries_handled[i]); - } - - return LLVMStructType(element_types, cast(unsigned)max_type_info_count, true); + return modified_types; } -gb_internal void lb_setup_type_info_data_giant_packed_struct(lbModule *m, i64 global_type_info_data_entity_count, lbProcedure *p) { // NOTE(bill): Setup type_info data +gb_internal void lb_setup_type_info_data_giant_array(lbModule *m, i64 global_type_info_data_entity_count, lbProcedure *p) { // NOTE(bill): Setup type_info data + auto const &ADD_GLOBAL_TYPE_INFO_ENTRY = [](lbModule *m, LLVMTypeRef type, isize index) -> LLVMValueRef { + char name[64] = {}; + gb_snprintf(name, 63, "__$ti-%lld", cast(long long)index); + LLVMValueRef g = LLVMAddGlobal(m->mod, type, name); + LLVMSetLinkage(g, LLVMInternalLinkage); + LLVMSetUnnamedAddress(g, LLVMGlobalUnnamedAddr); + LLVMSetGlobalConstant(g, true); + return g; + }; + CheckerInfo *info = m->info; // Useful types @@ -292,19 +267,47 @@ gb_internal void lb_setup_type_info_data_giant_packed_struct(lbModule *m, i64 gl defer (gb_free(heap_allocator(), entries_handled.data)); entries_handled[0] = true; - LLVMValueRef giant_struct = lb_global_type_info_data_ptr(m).value; - LLVMTypeRef giant_struct_type = LLVMGlobalGetValueType(giant_struct); - GB_ASSERT(LLVMGetTypeKind(giant_struct_type) == LLVMStructTypeKind); - LLVMValueRef *giant_const_values = gb_alloc_array(heap_allocator(), LLVMValueRef, global_type_info_data_entity_count); defer (gb_free(heap_allocator(), giant_const_values)); - giant_const_values[0] = LLVMConstNull(LLVMStructGetTypeAtIndex(giant_struct_type, 0)); + // zero value is just zero data + giant_const_values[0] = ADD_GLOBAL_TYPE_INFO_ENTRY(m, lb_type(m, t_type_info), 0); + LLVMSetInitializer(giant_const_values[0], LLVMConstNull(lb_type(m, t_type_info))); + + + LLVMTypeRef *modified_types = lb_setup_modified_types_for_type_info(m, global_type_info_data_entity_count); + defer (gb_free(heap_allocator(), modified_types)); + for_array(type_info_type_index, info->type_info_types) { + Type *t = info->type_info_types[type_info_type_index]; + if (t == nullptr || t == t_invalid) { + continue; + } + + isize entry_index = lb_type_info_index(info, t, false); + if (entry_index <= 0) { + continue; + } + + if (entries_handled[entry_index]) { + continue; + } + entries_handled[entry_index] = true; + + + LLVMTypeRef stype = modified_types[0]; + if (t->kind != Type_Named) { + stype = modified_types[lb_typeid_kind(m, t)]; + } + giant_const_values[entry_index] = ADD_GLOBAL_TYPE_INFO_ENTRY(m, stype, entry_index); + } + for (isize i = 1; i < global_type_info_data_entity_count; i++) { + entries_handled[i] = false; + } + LLVMValueRef *small_const_values = gb_alloc_array(heap_allocator(), LLVMValueRef, 6); defer (gb_free(heap_allocator(), small_const_values)); - #define type_info_allocate_values(name) \ LLVMValueRef *name##_values = gb_alloc_array(heap_allocator(), LLVMValueRef, type_deref(name.addr.type)->Array.count); \ defer (gb_free(heap_allocator(), name##_values)); \ @@ -316,7 +319,7 @@ gb_internal void lb_setup_type_info_data_giant_packed_struct(lbModule *m, i64 gl (name##_values)[i] = LLVMConstNull(elem); \ } \ } \ - LLVMSetInitializer(name.addr.value, llvm_const_array(elem, name##_values, at->Array.count)); \ + LLVMSetInitializer(name.addr.value, llvm_const_array(elem, name##_values, at->Array.count)); \ }) type_info_allocate_values(lb_global_type_info_member_types); @@ -326,27 +329,13 @@ gb_internal void lb_setup_type_info_data_giant_packed_struct(lbModule *m, i64 gl type_info_allocate_values(lb_global_type_info_member_tags); - i64 const type_info_struct_size = type_size_of(t_type_info); - LLVMTypeRef llvm_u8 = lb_type(m, t_u8); - LLVMTypeRef llvm_int = lb_type(m, t_int); - // LLVMTypeRef llvm_type_info_ptr = lb_type(m, t_type_info_ptr); - auto const get_type_info_ptr = [&](lbModule *m, Type *type) -> LLVMValueRef { type = default_type(type); isize index = lb_type_info_index(m->info, type); GB_ASSERT(index >= 0); - u64 offset = cast(u64)(index * type_info_struct_size); - - LLVMValueRef indices[1] = { - LLVMConstInt(llvm_int, offset, false) - }; - - // LLVMValueRef ptr = LLVMConstInBoundsGEP2(llvm_u8, giant_struct, indices, gb_count_of(indices)); - LLVMValueRef ptr = LLVMConstGEP2(llvm_u8, giant_struct, indices, gb_count_of(indices)); - return ptr; - // return LLVMConstPointerCast(ptr, llvm_type_info_ptr); + return giant_const_values[index]; }; for_array(type_info_type_index, info->type_info_types) { @@ -366,7 +355,10 @@ gb_internal void lb_setup_type_info_data_giant_packed_struct(lbModule *m, i64 gl entries_handled[entry_index] = true; - LLVMTypeRef stype = LLVMStructGetTypeAtIndex(giant_struct_type, cast(unsigned)entry_index); + LLVMTypeRef stype = modified_types[0]; + if (t->kind != Type_Named) { + stype = modified_types[lb_typeid_kind(m, t)]; + } i64 size = type_size_of(t); i64 align = type_align_of(t); @@ -376,6 +368,10 @@ gb_internal void lb_setup_type_info_data_giant_packed_struct(lbModule *m, i64 gl lbValue type_info_flags = lb_const_int(m, t_type_info_flags, flags); + for (isize i = 0; i < 6; i++) { + small_const_values[i] = nullptr; + } + small_const_values[0] = LLVMConstInt(lb_type(m, t_int), size, true); small_const_values[1] = LLVMConstInt(lb_type(m, t_int), align, true); small_const_values[2] = type_info_flags.value; @@ -994,6 +990,7 @@ gb_internal void lb_setup_type_info_data_giant_packed_struct(lbModule *m, i64 gl if (tag_type != nullptr) { tag_index = union_variant_index(ut, tag_type); } + GB_ASSERT(tag_index <= Typeid__COUNT); LLVMValueRef full_variant_values[3] = {}; @@ -1024,11 +1021,16 @@ gb_internal void lb_setup_type_info_data_giant_packed_struct(lbModule *m, i64 gl small_const_values[variant_index] = full_variant_value; - giant_const_values[entry_index] = LLVMConstNamedStruct(stype, small_const_values, variant_index+1); + LLVMSetInitializer(giant_const_values[entry_index], LLVMConstNamedStruct(stype, small_const_values, variant_index+1)); + } + for (isize i = 0; i < global_type_info_data_entity_count; i++) { + giant_const_values[i] = LLVMConstPointerCast(giant_const_values[i], lb_type(m, t_type_info_ptr)); } - LLVMValueRef giant_const = LLVMConstNamedStruct(giant_struct_type, giant_const_values, cast(unsigned)global_type_info_data_entity_count); - LLVMSetInitializer(giant_struct, giant_const); + + LLVMValueRef giant_const = LLVMConstArray(lb_type(m, t_type_info_ptr), giant_const_values, cast(unsigned)global_type_info_data_entity_count); + LLVMValueRef giant_array = lb_global_type_info_data_ptr(m).value; + LLVMSetInitializer(giant_array, giant_const); } @@ -1038,845 +1040,25 @@ gb_internal void lb_setup_type_info_data(lbProcedure *p) { // NOTE(bill): Setup } lbModule *m = p->module; - CheckerInfo *info = m->info; i64 global_type_info_data_entity_count = 0; - { - // NOTE(bill): Set the type_table slice with the global backing array - lbValue global_type_table = lb_find_runtime_value(m, str_lit("type_table")); - Type *type = base_type(lb_global_type_info_data_entity->type); - GB_ASSERT(type->kind == Type_Array); - global_type_info_data_entity_count = type->Array.count; - LLVMValueRef data = lb_global_type_info_data_ptr(m).value; - data = LLVMConstPointerCast(data, lb_type(m, alloc_type_pointer(type->Array.elem))); - LLVMValueRef len = LLVMConstInt(lb_type(m, t_int), type->Array.count, true); - Type *t = type_deref(global_type_table.type); - GB_ASSERT(is_type_slice(t)); - LLVMValueRef slice = llvm_const_slice_internal(m, data, len); + // NOTE(bill): Set the type_table slice with the global backing array + lbValue global_type_table = lb_find_runtime_value(m, str_lit("type_table")); + Type *type = base_type(lb_global_type_info_data_entity->type); + GB_ASSERT(type->kind == Type_Array); + global_type_info_data_entity_count = type->Array.count; - LLVMSetInitializer(global_type_table.value, slice); + if (true) { + lb_setup_type_info_data_giant_array(m, global_type_info_data_entity_count, p); } - if (LB_USE_GIANT_PACKED_STRUCT) { - lb_setup_type_info_data_giant_packed_struct(m, global_type_info_data_entity_count, p); - return; - } - - // Useful types - Entity *type_info_flags_entity = find_core_entity(info->checker, str_lit("Type_Info_Flags")); - Type *t_type_info_flags = type_info_flags_entity->type; - - - auto entries_handled = slice_make(heap_allocator(), cast(isize)global_type_info_data_entity_count); - defer (gb_free(heap_allocator(), entries_handled.data)); - entries_handled[0] = true; - - for_array(type_info_type_index, info->type_info_types) { - Type *t = info->type_info_types[type_info_type_index]; - if (t == nullptr || t == t_invalid) { - continue; - } - - isize entry_index = lb_type_info_index(info, t, false); - if (entry_index <= 0) { - continue; - } - - if (entries_handled[entry_index]) { - continue; - } - entries_handled[entry_index] = true; - - lbValue global_data_ptr = lb_global_type_info_data_ptr(m); - lbValue tag = {}; - lbValue ti_ptr = lb_emit_array_epi(p, global_data_ptr, cast(i32)entry_index); - - i64 size = type_size_of(t); - i64 align = type_align_of(t); - u32 flags = type_info_flags_of_type(t); - lbValue id = lb_typeid(m, t); - GB_ASSERT_MSG(align != 0, "%lld %s", align, type_to_string(t)); - - lbValue type_info_flags = lb_const_int(p->module, t_type_info_flags, flags); - - lbValue size_ptr = lb_emit_struct_ep(p, ti_ptr, 0); - lbValue align_ptr = lb_emit_struct_ep(p, ti_ptr, 1); - lbValue flags_ptr = lb_emit_struct_ep(p, ti_ptr, 2); - lbValue id_ptr = lb_emit_struct_ep(p, ti_ptr, 3); - - lb_emit_store(p, size_ptr, lb_const_int(m, t_int, size)); - lb_emit_store(p, align_ptr, lb_const_int(m, t_int, align)); - lb_emit_store(p, flags_ptr, type_info_flags); - lb_emit_store(p, id_ptr, id); - - lbValue variant_ptr = lb_emit_struct_ep(p, ti_ptr, 4); - - switch (t->kind) { - case Type_Named: { - tag = lb_const_ptr_cast(m, variant_ptr, t_type_info_named_ptr); - - LLVMValueRef pkg_name = nullptr; - if (t->Named.type_name->pkg) { - pkg_name = lb_const_string(m, t->Named.type_name->pkg->name).value; - } else { - pkg_name = LLVMConstNull(lb_type(m, t_string)); - } - - String proc_name = {}; - if (t->Named.type_name->parent_proc_decl) { - DeclInfo *decl = t->Named.type_name->parent_proc_decl; - if (decl->entity && decl->entity->kind == Entity_Procedure) { - proc_name = decl->entity->token.string; - } - } - TokenPos pos = t->Named.type_name->token.pos; - - lbValue loc = lb_emit_source_code_location_const(p, proc_name, pos); - - LLVMValueRef vals[4] = { - lb_const_string(p->module, t->Named.type_name->token.string).value, - lb_type_info(m, t->Named.base).value, - pkg_name, - loc.value - }; - - lbValue res = {}; - res.type = type_deref(tag.type); - res.value = llvm_const_named_struct(m, res.type, vals, gb_count_of(vals)); - lb_emit_store(p, tag, res); - break; - } - - case Type_Basic: - switch (t->Basic.kind) { - case Basic_bool: - case Basic_b8: - case Basic_b16: - case Basic_b32: - case Basic_b64: - tag = lb_const_ptr_cast(m, variant_ptr, t_type_info_boolean_ptr); - break; - - case Basic_i8: - case Basic_u8: - case Basic_i16: - case Basic_u16: - case Basic_i32: - case Basic_u32: - case Basic_i64: - case Basic_u64: - case Basic_i128: - case Basic_u128: - - case Basic_i16le: - case Basic_u16le: - case Basic_i32le: - case Basic_u32le: - case Basic_i64le: - case Basic_u64le: - case Basic_i128le: - case Basic_u128le: - case Basic_i16be: - case Basic_u16be: - case Basic_i32be: - case Basic_u32be: - case Basic_i64be: - case Basic_u64be: - case Basic_i128be: - case Basic_u128be: - - case Basic_int: - case Basic_uint: - case Basic_uintptr: { - tag = lb_const_ptr_cast(m, variant_ptr, t_type_info_integer_ptr); - - lbValue is_signed = lb_const_bool(m, t_bool, (t->Basic.flags & BasicFlag_Unsigned) == 0); - // NOTE(bill): This is matches the runtime layout - u8 endianness_value = 0; - if (t->Basic.flags & BasicFlag_EndianLittle) { - endianness_value = 1; - } else if (t->Basic.flags & BasicFlag_EndianBig) { - endianness_value = 2; - } - lbValue endianness = lb_const_int(m, t_u8, endianness_value); - - LLVMValueRef vals[2] = { - is_signed.value, - endianness.value, - }; - - lbValue res = {}; - res.type = type_deref(tag.type); - res.value = llvm_const_named_struct(m, res.type, vals, gb_count_of(vals)); - lb_emit_store(p, tag, res); - break; - } - - case Basic_rune: - tag = lb_const_ptr_cast(m, variant_ptr, t_type_info_rune_ptr); - break; - - case Basic_f16: - case Basic_f32: - case Basic_f64: - case Basic_f16le: - case Basic_f32le: - case Basic_f64le: - case Basic_f16be: - case Basic_f32be: - case Basic_f64be: - { - tag = lb_const_ptr_cast(m, variant_ptr, t_type_info_float_ptr); - - // NOTE(bill): This is matches the runtime layout - u8 endianness_value = 0; - if (t->Basic.flags & BasicFlag_EndianLittle) { - endianness_value = 1; - } else if (t->Basic.flags & BasicFlag_EndianBig) { - endianness_value = 2; - } - lbValue endianness = lb_const_int(m, t_u8, endianness_value); - - LLVMValueRef vals[1] = { - endianness.value, - }; - - lbValue res = {}; - res.type = type_deref(tag.type); - res.value = llvm_const_named_struct(m, res.type, vals, gb_count_of(vals)); - lb_emit_store(p, tag, res); - } - break; - - case Basic_complex32: - case Basic_complex64: - case Basic_complex128: - tag = lb_const_ptr_cast(m, variant_ptr, t_type_info_complex_ptr); - break; - - case Basic_quaternion64: - case Basic_quaternion128: - case Basic_quaternion256: - tag = lb_const_ptr_cast(m, variant_ptr, t_type_info_quaternion_ptr); - break; - - case Basic_rawptr: - tag = lb_const_ptr_cast(m, variant_ptr, t_type_info_pointer_ptr); - break; - - case Basic_string: - tag = lb_const_ptr_cast(m, variant_ptr, t_type_info_string_ptr); - break; - - case Basic_cstring: - { - tag = lb_const_ptr_cast(m, variant_ptr, t_type_info_string_ptr); - LLVMValueRef vals[1] = { - lb_const_bool(m, t_bool, true).value, - }; - - lbValue res = {}; - res.type = type_deref(tag.type); - res.value = llvm_const_named_struct(m, res.type, vals, gb_count_of(vals)); - lb_emit_store(p, tag, res); - } - break; - - case Basic_any: - tag = lb_const_ptr_cast(m, variant_ptr, t_type_info_any_ptr); - break; - - case Basic_typeid: - tag = lb_const_ptr_cast(m, variant_ptr, t_type_info_typeid_ptr); - break; - } - break; - - case Type_Pointer: { - tag = lb_const_ptr_cast(m, variant_ptr, t_type_info_pointer_ptr); - lbValue gep = lb_type_info(m, t->Pointer.elem); - - LLVMValueRef vals[1] = { - gep.value, - }; - - lbValue res = {}; - res.type = type_deref(tag.type); - res.value = llvm_const_named_struct(m, res.type, vals, gb_count_of(vals)); - lb_emit_store(p, tag, res); - break; - } - case Type_MultiPointer: { - tag = lb_const_ptr_cast(m, variant_ptr, t_type_info_multi_pointer_ptr); - lbValue gep = lb_type_info(m, t->MultiPointer.elem); - - LLVMValueRef vals[1] = { - gep.value, - }; - - lbValue res = {}; - res.type = type_deref(tag.type); - res.value = llvm_const_named_struct(m, res.type, vals, gb_count_of(vals)); - lb_emit_store(p, tag, res); - break; - } - case Type_SoaPointer: { - tag = lb_const_ptr_cast(m, variant_ptr, t_type_info_soa_pointer_ptr); - lbValue gep = lb_type_info(m, t->SoaPointer.elem); - - LLVMValueRef vals[1] = { - gep.value, - }; - - lbValue res = {}; - res.type = type_deref(tag.type); - res.value = llvm_const_named_struct(m, res.type, vals, gb_count_of(vals)); - lb_emit_store(p, tag, res); - break; - } - case Type_Array: { - tag = lb_const_ptr_cast(m, variant_ptr, t_type_info_array_ptr); - i64 ez = type_size_of(t->Array.elem); - - LLVMValueRef vals[3] = { - lb_type_info(m, t->Array.elem).value, - lb_const_int(m, t_int, ez).value, - lb_const_int(m, t_int, t->Array.count).value, - }; - - lbValue res = {}; - res.type = type_deref(tag.type); - res.value = llvm_const_named_struct(m, res.type, vals, gb_count_of(vals)); - lb_emit_store(p, tag, res); - break; - } - case Type_EnumeratedArray: { - tag = lb_const_ptr_cast(m, variant_ptr, t_type_info_enumerated_array_ptr); - - LLVMValueRef vals[7] = { - lb_type_info(m, t->EnumeratedArray.elem).value, - lb_type_info(m, t->EnumeratedArray.index).value, - lb_const_int(m, t_int, type_size_of(t->EnumeratedArray.elem)).value, - lb_const_int(m, t_int, t->EnumeratedArray.count).value, - - // Unions - LLVMConstNull(lb_type(m, t_type_info_enum_value)), - LLVMConstNull(lb_type(m, t_type_info_enum_value)), - - lb_const_bool(m, t_bool, t->EnumeratedArray.is_sparse).value, - }; - - lbValue res = {}; - res.type = type_deref(tag.type); - res.value = llvm_const_named_struct(m, res.type, vals, gb_count_of(vals)); - lb_emit_store(p, tag, res); - - // NOTE(bill): Union assignment - lbValue min_value = lb_emit_struct_ep(p, tag, 4); - lbValue max_value = lb_emit_struct_ep(p, tag, 5); - - lbValue min_v = lb_const_value(m, t_i64, *t->EnumeratedArray.min_value); - lbValue max_v = lb_const_value(m, t_i64, *t->EnumeratedArray.max_value); - - lb_emit_store(p, min_value, min_v); - lb_emit_store(p, max_value, max_v); - break; - } - case Type_DynamicArray: { - tag = lb_const_ptr_cast(m, variant_ptr, t_type_info_dynamic_array_ptr); - - LLVMValueRef vals[2] = { - lb_type_info(m, t->DynamicArray.elem).value, - lb_const_int(m, t_int, type_size_of(t->DynamicArray.elem)).value, - }; - - lbValue res = {}; - res.type = type_deref(tag.type); - res.value = llvm_const_named_struct(m, res.type, vals, gb_count_of(vals)); - lb_emit_store(p, tag, res); - break; - } - case Type_Slice: { - tag = lb_const_ptr_cast(m, variant_ptr, t_type_info_slice_ptr); - - LLVMValueRef vals[2] = { - lb_type_info(m, t->Slice.elem).value, - lb_const_int(m, t_int, type_size_of(t->Slice.elem)).value, - }; - - lbValue res = {}; - res.type = type_deref(tag.type); - res.value = llvm_const_named_struct(m, res.type, vals, gb_count_of(vals)); - lb_emit_store(p, tag, res); - break; - } - case Type_Proc: { - tag = lb_const_ptr_cast(m, variant_ptr, t_type_info_procedure_ptr); - - LLVMValueRef params = LLVMConstNull(lb_type(m, t_type_info_ptr)); - LLVMValueRef results = LLVMConstNull(lb_type(m, t_type_info_ptr)); - if (t->Proc.params != nullptr) { - params = lb_type_info(m, t->Proc.params).value; - } - if (t->Proc.results != nullptr) { - results = lb_type_info(m, t->Proc.results).value; - } - - LLVMValueRef vals[4] = { - params, - results, - lb_const_bool(m, t_bool, t->Proc.variadic).value, - lb_const_int(m, t_u8, t->Proc.calling_convention).value, - }; - - lbValue res = {}; - res.type = type_deref(tag.type); - res.value = llvm_const_named_struct(m, res.type, vals, gb_count_of(vals)); - lb_emit_store(p, tag, res); - break; - } - case Type_Tuple: { - tag = lb_const_ptr_cast(m, variant_ptr, t_type_info_parameters_ptr); - - lbValue memory_types = lb_type_info_member_types_offset(m, t->Tuple.variables.count); - lbValue memory_names = lb_type_info_member_names_offset(m, t->Tuple.variables.count); - - - for_array(i, t->Tuple.variables) { - // NOTE(bill): offset is not used for tuples - Entity *f = t->Tuple.variables[i]; - - lbValue index = lb_const_int(m, t_int, i); - lbValue type_info = lb_emit_ptr_offset(p, memory_types, index); - - // TODO(bill): Make this constant if possible, 'lb_const_store' does not work - lb_emit_store(p, type_info, lb_type_info(m, f->type)); - if (f->token.string.len > 0) { - lbValue name = lb_emit_ptr_offset(p, memory_names, index); - lb_emit_store(p, name, lb_const_string(m, f->token.string)); - } - } - - lbValue count = lb_const_int(m, t_int, t->Tuple.variables.count); - - LLVMValueRef types_slice = llvm_const_slice(m, memory_types, count); - LLVMValueRef names_slice = llvm_const_slice(m, memory_names, count); - - LLVMValueRef vals[2] = { - types_slice, - names_slice, - }; - - lbValue res = {}; - res.type = type_deref(tag.type); - res.value = llvm_const_named_struct(m, res.type, vals, gb_count_of(vals)); - lb_emit_store(p, tag, res); - - break; - } - - case Type_Enum: - tag = lb_const_ptr_cast(m, variant_ptr, t_type_info_enum_ptr); - - { - GB_ASSERT(t->Enum.base_type != nullptr); - // GB_ASSERT_MSG(type_size_of(t_type_info_enum_value) == 16, "%lld == 16", cast(long long)type_size_of(t_type_info_enum_value)); - - - LLVMValueRef vals[3] = {}; - vals[0] = lb_type_info(m, t->Enum.base_type).value; - if (t->Enum.fields.count > 0) { - auto fields = t->Enum.fields; - lbValue name_array = lb_generate_global_array(m, t_string, fields.count, - str_lit("$enum_names"), cast(i64)entry_index); - lbValue value_array = lb_generate_global_array(m, t_type_info_enum_value, fields.count, - str_lit("$enum_values"), cast(i64)entry_index); - - - LLVMValueRef *name_values = gb_alloc_array(temporary_allocator(), LLVMValueRef, fields.count); - LLVMValueRef *value_values = gb_alloc_array(temporary_allocator(), LLVMValueRef, fields.count); - - GB_ASSERT(is_type_integer(t->Enum.base_type)); - - for_array(i, fields) { - name_values[i] = lb_const_string(m, fields[i]->token.string).value; - value_values[i] = lb_const_value(m, t_i64, fields[i]->Constant.value).value; - } - - LLVMValueRef name_init = llvm_const_array(lb_type(m, t_string), name_values, cast(unsigned)fields.count); - LLVMValueRef value_init = llvm_const_array(lb_type(m, t_type_info_enum_value), value_values, cast(unsigned)fields.count); - LLVMSetInitializer(name_array.value, name_init); - LLVMSetInitializer(value_array.value, value_init); - LLVMSetGlobalConstant(name_array.value, true); - LLVMSetGlobalConstant(value_array.value, true); - - lbValue v_count = lb_const_int(m, t_int, fields.count); - - vals[1] = llvm_const_slice(m, lb_array_elem(p, name_array), v_count); - vals[2] = llvm_const_slice(m, lb_array_elem(p, value_array), v_count); - } else { - vals[1] = LLVMConstNull(lb_type(m, base_type(t_type_info_enum)->Struct.fields[1]->type)); - vals[2] = LLVMConstNull(lb_type(m, base_type(t_type_info_enum)->Struct.fields[2]->type)); - } - - - lbValue res = {}; - res.type = type_deref(tag.type); - res.value = llvm_const_named_struct(m, res.type, vals, gb_count_of(vals)); - lb_emit_store(p, tag, res); - } - break; - - case Type_Union: { - tag = lb_const_ptr_cast(m, variant_ptr, t_type_info_union_ptr); - - { - LLVMValueRef vals[7] = {}; - - isize variant_count = gb_max(0, t->Union.variants.count); - lbValue memory_types = lb_type_info_member_types_offset(m, variant_count); - - // NOTE(bill): Zeroth is nil so ignore it - for (isize variant_index = 0; variant_index < variant_count; variant_index++) { - Type *vt = t->Union.variants[variant_index]; - lbValue tip = lb_type_info(m, vt); - - lbValue index = lb_const_int(m, t_int, variant_index); - lbValue type_info = lb_emit_ptr_offset(p, memory_types, index); - lb_emit_store(p, type_info, lb_type_info(m, vt)); - } - - lbValue count = lb_const_int(m, t_int, variant_count); - vals[0] = llvm_const_slice(m, memory_types, count); - - i64 tag_size = union_tag_size(t); - if (tag_size > 0) { - i64 tag_offset = align_formula(t->Union.variant_block_size, tag_size); - vals[1] = lb_const_int(m, t_uintptr, tag_offset).value; - vals[2] = lb_type_info(m, union_tag_type(t)).value; - } else { - vals[1] = lb_const_int(m, t_uintptr, 0).value; - vals[2] = LLVMConstNull(lb_type(m, t_type_info_ptr)); - } - - if (is_type_comparable(t) && !is_type_simple_compare(t)) { - vals[3] = lb_equal_proc_for_type(m, t).value; - } - - vals[4] = lb_const_bool(m, t_bool, t->Union.custom_align != 0).value; - vals[5] = lb_const_bool(m, t_bool, t->Union.kind == UnionType_no_nil).value; - vals[6] = lb_const_bool(m, t_bool, t->Union.kind == UnionType_shared_nil).value; - - for (isize i = 0; i < gb_count_of(vals); i++) { - if (vals[i] == nullptr) { - vals[i] = LLVMConstNull(lb_type(m, get_struct_field_type(tag.type, i))); - } - } - - lbValue res = {}; - res.type = type_deref(tag.type); - res.value = llvm_const_named_struct(m, res.type, vals, gb_count_of(vals)); - lb_emit_store(p, tag, res); - } - - break; - } - - case Type_Struct: { - tag = lb_const_ptr_cast(m, variant_ptr, t_type_info_struct_ptr); - - LLVMValueRef vals[13] = {}; - - - { - lbValue is_packed = lb_const_bool(m, t_bool, t->Struct.is_packed); - lbValue is_raw_union = lb_const_bool(m, t_bool, t->Struct.is_raw_union); - lbValue is_no_copy = lb_const_bool(m, t_bool, t->Struct.is_no_copy); - lbValue is_custom_align = lb_const_bool(m, t_bool, t->Struct.custom_align != 0); - vals[5] = is_packed.value; - vals[6] = is_raw_union.value; - vals[7] = is_no_copy.value; - vals[8] = is_custom_align.value; - if (is_type_comparable(t) && !is_type_simple_compare(t)) { - vals[9] = lb_equal_proc_for_type(m, t).value; - } - - - if (t->Struct.soa_kind != StructSoa_None) { - lbValue kind = lb_emit_struct_ep(p, tag, 10); - Type *kind_type = type_deref(kind.type); - - lbValue soa_kind = lb_const_value(m, kind_type, exact_value_i64(t->Struct.soa_kind)); - lbValue soa_type = lb_type_info(m, t->Struct.soa_elem); - lbValue soa_len = lb_const_int(m, t_int, t->Struct.soa_count); - - vals[10] = soa_kind.value; - vals[11] = soa_type.value; - vals[12] = soa_len.value; - } - } - - isize count = t->Struct.fields.count; - if (count > 0) { - lbValue memory_types = lb_type_info_member_types_offset (m, count); - lbValue memory_names = lb_type_info_member_names_offset (m, count); - lbValue memory_offsets = lb_type_info_member_offsets_offset(m, count); - lbValue memory_usings = lb_type_info_member_usings_offset (m, count); - lbValue memory_tags = lb_type_info_member_tags_offset (m, count); - - type_set_offsets(t); // NOTE(bill): Just incase the offsets have not been set yet - for (isize source_index = 0; source_index < count; source_index++) { - Entity *f = t->Struct.fields[source_index]; - lbValue tip = lb_type_info(m, f->type); - i64 foffset = 0; - if (!t->Struct.is_raw_union) { - GB_ASSERT(t->Struct.offsets != nullptr); - GB_ASSERT(0 <= f->Variable.field_index && f->Variable.field_index < count); - foffset = t->Struct.offsets[source_index]; - } - GB_ASSERT(f->kind == Entity_Variable && f->flags & EntityFlag_Field); - - lbValue index = lb_const_int(m, t_int, source_index); - lbValue type_info = lb_emit_ptr_offset(p, memory_types, index); - lbValue offset = lb_emit_ptr_offset(p, memory_offsets, index); - lbValue is_using = lb_emit_ptr_offset(p, memory_usings, index); - - lb_emit_store(p, type_info, lb_type_info(m, f->type)); - if (f->token.string.len > 0) { - lbValue name = lb_emit_ptr_offset(p, memory_names, index); - lb_emit_store(p, name, lb_const_string(m, f->token.string)); - } - lb_emit_store(p, offset, lb_const_int(m, t_uintptr, foffset)); - lb_emit_store(p, is_using, lb_const_bool(m, t_bool, (f->flags&EntityFlag_Using) != 0)); - - if (t->Struct.tags != nullptr) { - String tag_string = t->Struct.tags[source_index]; - if (tag_string.len > 0) { - lbValue tag_ptr = lb_emit_ptr_offset(p, memory_tags, index); - lb_emit_store(p, tag_ptr, lb_const_string(m, tag_string)); - } - } - - } - - lbValue cv = lb_const_int(m, t_int, count); - vals[0] = llvm_const_slice(m, memory_types, cv); - vals[1] = llvm_const_slice(m, memory_names, cv); - vals[2] = llvm_const_slice(m, memory_offsets, cv); - vals[3] = llvm_const_slice(m, memory_usings, cv); - vals[4] = llvm_const_slice(m, memory_tags, cv); - } - for (isize i = 0; i < gb_count_of(vals); i++) { - if (vals[i] == nullptr) { - vals[i] = LLVMConstNull(lb_type(m, get_struct_field_type(tag.type, i))); - } - } - - lbValue res = {}; - res.type = type_deref(tag.type); - res.value = llvm_const_named_struct(m, res.type, vals, gb_count_of(vals)); - lb_emit_store(p, tag, res); - - break; - } - - case Type_Map: { - tag = lb_const_ptr_cast(m, variant_ptr, t_type_info_map_ptr); - init_map_internal_types(t); - - LLVMValueRef vals[3] = { - lb_type_info(m, t->Map.key).value, - lb_type_info(m, t->Map.value).value, - lb_gen_map_info_ptr(p->module, t).value - }; - - lbValue res = {}; - res.type = type_deref(tag.type); - res.value = llvm_const_named_struct(m, res.type, vals, gb_count_of(vals)); - lb_emit_store(p, tag, res); - break; - } - - case Type_BitSet: - { - tag = lb_const_ptr_cast(m, variant_ptr, t_type_info_bit_set_ptr); - - GB_ASSERT(is_type_typed(t->BitSet.elem)); - - - LLVMValueRef vals[4] = { - lb_type_info(m, t->BitSet.elem).value, - LLVMConstNull(lb_type(m, t_type_info_ptr)), - lb_const_int(m, t_i64, t->BitSet.lower).value, - lb_const_int(m, t_i64, t->BitSet.upper).value, - }; - if (t->BitSet.underlying != nullptr) { - vals[1] =lb_type_info(m, t->BitSet.underlying).value; - } - - lbValue res = {}; - res.type = type_deref(tag.type); - res.value = llvm_const_named_struct(m, res.type, vals, gb_count_of(vals)); - lb_emit_store(p, tag, res); - } - break; - - case Type_SimdVector: - { - tag = lb_const_ptr_cast(m, variant_ptr, t_type_info_simd_vector_ptr); - - LLVMValueRef vals[3] = {}; - - vals[0] = lb_type_info(m, t->SimdVector.elem).value; - vals[1] = lb_const_int(m, t_int, type_size_of(t->SimdVector.elem)).value; - vals[2] = lb_const_int(m, t_int, t->SimdVector.count).value; - - lbValue res = {}; - res.type = type_deref(tag.type); - res.value = llvm_const_named_struct(m, res.type, vals, gb_count_of(vals)); - lb_emit_store(p, tag, res); - } - break; - - case Type_RelativePointer: - { - tag = lb_const_ptr_cast(m, variant_ptr, t_type_info_relative_pointer_ptr); - LLVMValueRef vals[2] = { - lb_type_info(m, t->RelativePointer.pointer_type).value, - lb_type_info(m, t->RelativePointer.base_integer).value, - }; - - lbValue res = {}; - res.type = type_deref(tag.type); - res.value = llvm_const_named_struct(m, res.type, vals, gb_count_of(vals)); - lb_emit_store(p, tag, res); - } - break; - - case Type_RelativeMultiPointer: - { - tag = lb_const_ptr_cast(m, variant_ptr, t_type_info_relative_multi_pointer_ptr); - LLVMValueRef vals[2] = { - lb_type_info(m, t->RelativeMultiPointer.pointer_type).value, - lb_type_info(m, t->RelativeMultiPointer.base_integer).value, - }; - - lbValue res = {}; - res.type = type_deref(tag.type); - res.value = llvm_const_named_struct(m, res.type, vals, gb_count_of(vals)); - lb_emit_store(p, tag, res); - } - break; - - case Type_Matrix: - { - tag = lb_const_ptr_cast(m, variant_ptr, t_type_info_matrix_ptr); - i64 ez = type_size_of(t->Matrix.elem); - - LLVMValueRef vals[5] = { - lb_type_info(m, t->Matrix.elem).value, - lb_const_int(m, t_int, ez).value, - lb_const_int(m, t_int, matrix_type_stride_in_elems(t)).value, - lb_const_int(m, t_int, t->Matrix.row_count).value, - lb_const_int(m, t_int, t->Matrix.column_count).value, - }; - - lbValue res = {}; - res.type = type_deref(tag.type); - res.value = llvm_const_named_struct(m, res.type, vals, gb_count_of(vals)); - lb_emit_store(p, tag, res); - } - break; - - case Type_BitField: - { - tag = lb_const_ptr_cast(m, variant_ptr, t_type_info_bit_field_ptr); - LLVMValueRef vals[6] = {}; - - vals[0] = lb_type_info(m, t->BitField.backing_type).value; - isize count = t->BitField.fields.count; - if (count > 0) { - i64 names_offset = 0; - i64 types_offset = 0; - i64 bit_sizes_offset = 0; - i64 bit_offsets_offset = 0; - i64 tags_offset = 0; - lbValue memory_names = lb_type_info_member_names_offset (m, count, &names_offset); - lbValue memory_types = lb_type_info_member_types_offset (m, count, &types_offset); - lbValue memory_bit_sizes = lb_type_info_member_offsets_offset(m, count, &bit_sizes_offset); - lbValue memory_bit_offsets = lb_type_info_member_offsets_offset(m, count, &bit_offsets_offset); - lbValue memory_tags = lb_type_info_member_tags_offset (m, count, &tags_offset); - - u64 bit_offset = 0; - for (isize source_index = 0; source_index < count; source_index++) { - Entity *f = t->BitField.fields[source_index]; - u64 bit_size = cast(u64)t->BitField.bit_sizes[source_index]; - - lbValue index = lb_const_int(m, t_int, source_index); - if (f->token.string.len > 0) { - lbValue name_ptr = lb_emit_ptr_offset(p, memory_names, index); - lb_emit_store(p, name_ptr, lb_const_string(m, f->token.string)); - } - lbValue type_ptr = lb_emit_ptr_offset(p, memory_types, index); - lbValue bit_size_ptr = lb_emit_ptr_offset(p, memory_bit_sizes, index); - lbValue bit_offset_ptr = lb_emit_ptr_offset(p, memory_bit_offsets, index); - - lb_emit_store(p, type_ptr, lb_type_info(m, f->type)); - lb_emit_store(p, bit_size_ptr, lb_const_int(m, t_uintptr, bit_size)); - lb_emit_store(p, bit_offset_ptr, lb_const_int(m, t_uintptr, bit_offset)); - - if (t->BitField.tags) { - String tag = t->BitField.tags[source_index]; - if (tag.len > 0) { - lbValue tag_ptr = lb_emit_ptr_offset(p, memory_tags, index); - lb_emit_store(p, tag_ptr, lb_const_string(m, tag)); - } - } - - bit_offset += bit_size; - } - - lbValue cv = lb_const_int(m, t_int, count); - vals[1] = llvm_const_slice(m, memory_names, cv); - vals[2] = llvm_const_slice(m, memory_types, cv); - vals[3] = llvm_const_slice(m, memory_bit_sizes, cv); - vals[4] = llvm_const_slice(m, memory_bit_offsets, cv); - vals[5] = llvm_const_slice(m, memory_tags, cv); - } - - for (isize i = 0; i < gb_count_of(vals); i++) { - if (vals[i] == nullptr) { - vals[i] = LLVMConstNull(lb_type(m, get_struct_field_type(tag.type, i))); - } - } - - lbValue res = {}; - res.type = type_deref(tag.type); - res.value = llvm_const_named_struct(m, res.type, vals, gb_count_of(vals)); - lb_emit_store(p, tag, res); - - break; - } - - } - - - if (tag.value != nullptr) { - Type *tag_type = type_deref(tag.type); - GB_ASSERT(is_type_named(tag_type)); - // lb_emit_store_union_variant(p, variant_ptr, lb_emit_load(p, tag), tag_type); - lb_emit_store_union_variant_tag(p, variant_ptr, tag_type); - } else { - if (t != t_llvm_bool) { - GB_PANIC("Unhandled Type_Info variant: %s", type_to_string(t)); - } - } - } - - for_array(i, entries_handled) { - if (!entries_handled[i]) { - GB_PANIC("UNHANDLED ENTRY %td (%td)", i, entries_handled.count); - } - } + LLVMValueRef data = lb_global_type_info_data_ptr(m).value; + data = LLVMConstPointerCast(data, lb_type(m, alloc_type_pointer(type->Array.elem))); + LLVMValueRef len = LLVMConstInt(lb_type(m, t_int), type->Array.count, true); + Type *t = type_deref(global_type_table.type); + GB_ASSERT(is_type_slice(t)); + LLVMValueRef slice = llvm_const_slice_internal(m, data, len); + + LLVMSetInitializer(global_type_table.value, slice); } diff --git a/src/types.cpp b/src/types.cpp index 90cb130b6..efe7d3f6f 100644 --- a/src/types.cpp +++ b/src/types.cpp @@ -365,6 +365,9 @@ enum Typeid_Kind : u8 { Typeid_Matrix, Typeid_SoaPointer, Typeid_Bit_Field, + + Typeid__COUNT + }; // IMPORTANT NOTE(bill): This must match the same as the in core.odin From 826cf1508bcdbf610f0cf1fa385b90c0eaea4173 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Tue, 27 Feb 2024 15:09:33 +0000 Subject: [PATCH 22/40] Remove `__$startup_type_info` procedure --- src/llvm_backend.cpp | 43 +++---------------------------------------- src/llvm_backend.hpp | 2 -- 2 files changed, 3 insertions(+), 42 deletions(-) diff --git a/src/llvm_backend.cpp b/src/llvm_backend.cpp index d3a2f25d2..17583e883 100644 --- a/src/llvm_backend.cpp +++ b/src/llvm_backend.cpp @@ -1053,37 +1053,6 @@ struct lbGlobalVariable { bool is_initialized; }; -gb_internal lbProcedure *lb_create_startup_type_info(lbModule *m) { - if (build_context.no_rtti) { - return nullptr; - } - Type *proc_type = alloc_type_proc(nullptr, nullptr, 0, nullptr, 0, false, ProcCC_CDecl); - - lbProcedure *p = lb_create_dummy_procedure(m, str_lit(LB_STARTUP_TYPE_INFO_PROC_NAME), proc_type); - p->is_startup = true; - LLVMSetLinkage(p->value, LLVMInternalLinkage); - - lb_add_attribute_to_proc(m, p->value, "nounwind"); - // lb_add_attribute_to_proc(p->module, p->value, "mustprogress"); - // lb_add_attribute_to_proc(p->module, p->value, "nofree"); - // lb_add_attribute_to_proc(p->module, p->value, "norecurse"); - // lb_add_attribute_to_proc(p->module, p->value, "nosync"); - // lb_add_attribute_to_proc(p->module, p->value, "willreturn"); - - lb_begin_procedure_body(p); - - lb_setup_type_info_data(p); - - lb_end_procedure_body(p); - - if (!m->debug_builder && LLVMVerifyFunction(p->value, LLVMReturnStatusAction)) { - gb_printf_err("LLVM CODE GEN FAILED FOR PROCEDURE: %s\n", "main"); - LLVMDumpValue(p->value); - gb_printf_err("\n\n\n\n"); - LLVMVerifyFunction(p->value, LLVMAbortProcessAction); - } - return p; -} gb_internal lbProcedure *lb_create_objc_names(lbModule *main_module) { if (build_context.metrics.os != TargetOs_darwin) { @@ -1125,7 +1094,7 @@ gb_internal void lb_finalize_objc_names(lbProcedure *p) { lb_end_procedure_body(p); } -gb_internal lbProcedure *lb_create_startup_runtime(lbModule *main_module, lbProcedure *startup_type_info, lbProcedure *objc_names, Array &global_variables) { // Startup Runtime +gb_internal lbProcedure *lb_create_startup_runtime(lbModule *main_module, lbProcedure *objc_names, Array &global_variables) { // Startup Runtime Type *proc_type = alloc_type_proc(nullptr, nullptr, 0, nullptr, 0, false, ProcCC_Odin); lbProcedure *p = lb_create_dummy_procedure(main_module, str_lit(LB_STARTUP_RUNTIME_PROC_NAME), proc_type); @@ -1135,10 +1104,6 @@ gb_internal lbProcedure *lb_create_startup_runtime(lbModule *main_module, lbProc lb_begin_procedure_body(p); - if (startup_type_info) { - LLVMBuildCall2(p->builder, lb_type_internal_for_procedures_raw(main_module, startup_type_info->type), startup_type_info->value, nullptr, 0, ""); - } - if (objc_names) { LLVMBuildCall2(p->builder, lb_type_internal_for_procedures_raw(main_module, objc_names->type), objc_names->value, nullptr, 0, ""); } @@ -1422,7 +1387,6 @@ gb_internal WORKER_TASK_PROC(lb_llvm_function_pass_per_module) { } if (m == &m->gen->default_module) { - lb_llvm_function_pass_per_function_internal(m, m->gen->startup_type_info); lb_llvm_function_pass_per_function_internal(m, m->gen->startup_runtime); lb_llvm_function_pass_per_function_internal(m, m->gen->cleanup_runtime); lb_llvm_function_pass_per_function_internal(m, m->gen->objc_names); @@ -2912,12 +2876,11 @@ gb_internal bool lb_generate_code(lbGenerator *gen) { } } - TIME_SECTION("LLVM Runtime Type Information Creation"); - gen->startup_type_info = lb_create_startup_type_info(default_module); + TIME_SECTION("LLVM Runtime Objective-C Names Creation"); gen->objc_names = lb_create_objc_names(default_module); TIME_SECTION("LLVM Runtime Startup Creation (Global Variables & @(init))"); - gen->startup_runtime = lb_create_startup_runtime(default_module, gen->startup_type_info, gen->objc_names, global_variables); + gen->startup_runtime = lb_create_startup_runtime(default_module, gen->objc_names, global_variables); TIME_SECTION("LLVM Runtime Cleanup Creation & @(fini)"); gen->cleanup_runtime = lb_create_cleanup_runtime(default_module); diff --git a/src/llvm_backend.hpp b/src/llvm_backend.hpp index 00d1b7a21..48e1c87c6 100644 --- a/src/llvm_backend.hpp +++ b/src/llvm_backend.hpp @@ -225,7 +225,6 @@ struct lbGenerator : LinkerData { std::atomic global_array_index; std::atomic global_generated_index; - lbProcedure *startup_type_info; lbProcedure *startup_runtime; lbProcedure *cleanup_runtime; lbProcedure *objc_names; @@ -587,7 +586,6 @@ gb_internal LLVMTypeRef llvm_array_type(LLVMTypeRef ElementType, uint64_t Elemen #define LB_STARTUP_RUNTIME_PROC_NAME "__$startup_runtime" #define LB_CLEANUP_RUNTIME_PROC_NAME "__$cleanup_runtime" -#define LB_STARTUP_TYPE_INFO_PROC_NAME "__$startup_type_info" #define LB_TYPE_INFO_DATA_NAME "__$type_info_data" #define LB_TYPE_INFO_TYPES_NAME "__$type_info_types_data" #define LB_TYPE_INFO_NAMES_NAME "__$type_info_names_data" From c6ee025063b0b64c8e14cdda702477436b921415 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Tue, 27 Feb 2024 15:27:51 +0000 Subject: [PATCH 23/40] Add type info generation for `bit_field` --- src/llvm_backend.cpp | 2 + src/llvm_backend_type.cpp | 83 +++++++++++++++++++++++++++++++++++---- 2 files changed, 77 insertions(+), 8 deletions(-) diff --git a/src/llvm_backend.cpp b/src/llvm_backend.cpp index 17583e883..a8d2df181 100644 --- a/src/llvm_backend.cpp +++ b/src/llvm_backend.cpp @@ -1104,6 +1104,8 @@ gb_internal lbProcedure *lb_create_startup_runtime(lbModule *main_module, lbProc lb_begin_procedure_body(p); + lb_setup_type_info_data(main_module); + if (objc_names) { LLVMBuildCall2(p->builder, lb_type_internal_for_procedures_raw(main_module, objc_names->type), objc_names->value, nullptr, 0, ""); } diff --git a/src/llvm_backend_type.cpp b/src/llvm_backend_type.cpp index 09a758827..336678ce9 100644 --- a/src/llvm_backend_type.cpp +++ b/src/llvm_backend_type.cpp @@ -62,6 +62,7 @@ gb_internal u64 lb_typeid_kind(lbModule *m, Type *type, u64 id=0) { case Type_RelativePointer: kind = Typeid_Relative_Pointer; break; case Type_RelativeMultiPointer: kind = Typeid_Relative_Multi_Pointer; break; case Type_SoaPointer: kind = Typeid_SoaPointer; break; + case Type_BitField: kind = Typeid_Bit_Field; break; } return kind; @@ -240,7 +241,7 @@ gb_internal LLVMTypeRef *lb_setup_modified_types_for_type_info(lbModule *m, isiz return modified_types; } -gb_internal void lb_setup_type_info_data_giant_array(lbModule *m, i64 global_type_info_data_entity_count, lbProcedure *p) { // NOTE(bill): Setup type_info data +gb_internal void lb_setup_type_info_data_giant_array(lbModule *m, i64 global_type_info_data_entity_count) { // NOTE(bill): Setup type_info data auto const &ADD_GLOBAL_TYPE_INFO_ENTRY = [](lbModule *m, LLVMTypeRef type, isize index) -> LLVMValueRef { char name[64] = {}; gb_snprintf(name, 63, "__$ti-%lld", cast(long long)index); @@ -294,8 +295,10 @@ gb_internal void lb_setup_type_info_data_giant_array(lbModule *m, i64 global_typ entries_handled[entry_index] = true; - LLVMTypeRef stype = modified_types[0]; - if (t->kind != Type_Named) { + LLVMTypeRef stype = nullptr; + if (t->kind == Type_Named) { + stype = modified_types[0]; + } else { stype = modified_types[lb_typeid_kind(m, t)]; } giant_const_values[entry_index] = ADD_GLOBAL_TYPE_INFO_ENTRY(m, stype, entry_index); @@ -355,8 +358,10 @@ gb_internal void lb_setup_type_info_data_giant_array(lbModule *m, i64 global_typ entries_handled[entry_index] = true; - LLVMTypeRef stype = modified_types[0]; - if (t->kind != Type_Named) { + LLVMTypeRef stype = nullptr; + if (t->kind == Type_Named) { + stype = modified_types[0]; + } else { stype = modified_types[lb_typeid_kind(m, t)]; } @@ -982,6 +987,69 @@ gb_internal void lb_setup_type_info_data_giant_array(lbModule *m, i64 global_typ variant_value = llvm_const_named_struct(m, tag_type, vals, gb_count_of(vals)); } break; + + case Type_BitField: + { + tag_type = t_type_info_bit_field; + + LLVMValueRef vals[6] = {}; + vals[0] = get_type_info_ptr(m, t->BitField.backing_type); + isize count = t->BitField.fields.count; + if (count > 0) { + i64 names_offset = 0; + i64 types_offset = 0; + i64 bit_sizes_offset = 0; + i64 bit_offsets_offset = 0; + i64 tags_offset = 0; + lbValue memory_names = lb_type_info_member_names_offset (m, count, &names_offset); + lbValue memory_types = lb_type_info_member_types_offset (m, count, &types_offset); + lbValue memory_bit_sizes = lb_type_info_member_offsets_offset(m, count, &bit_sizes_offset); + lbValue memory_bit_offsets = lb_type_info_member_offsets_offset(m, count, &bit_offsets_offset); + lbValue memory_tags = lb_type_info_member_tags_offset (m, count, &tags_offset); + + u64 bit_offset = 0; + for (isize source_index = 0; source_index < count; source_index++) { + Entity *f = t->BitField.fields[source_index]; + u64 bit_size = cast(u64)t->BitField.bit_sizes[source_index]; + + lbValue index = lb_const_int(m, t_int, source_index); + if (f->token.string.len > 0) { + lb_global_type_info_member_names_values[names_offset+source_index] = lb_const_string(m, f->token.string).value; + } + + lb_global_type_info_member_types_values[types_offset+source_index] = get_type_info_ptr(m, f->type); + + lb_global_type_info_member_offsets_values[bit_sizes_offset+source_index] = lb_const_int(m, t_uintptr, bit_size).value; + lb_global_type_info_member_offsets_values[bit_offsets_offset+source_index] = lb_const_int(m, t_uintptr, bit_offset).value; + + if (t->BitField.tags) { + String tag = t->BitField.tags[source_index]; + if (tag.len > 0) { + lb_global_type_info_member_tags_values[tags_offset+source_index] = lb_const_string(m, tag).value; + } + } + + bit_offset += bit_size; + } + + lbValue cv = lb_const_int(m, t_int, count); + vals[1] = llvm_const_slice(m, memory_names, cv); + vals[2] = llvm_const_slice(m, memory_types, cv); + vals[3] = llvm_const_slice(m, memory_bit_sizes, cv); + vals[4] = llvm_const_slice(m, memory_bit_offsets, cv); + vals[5] = llvm_const_slice(m, memory_tags, cv); + } + + + for (isize i = 0; i < gb_count_of(vals); i++) { + if (vals[i] == nullptr) { + vals[i] = LLVMConstNull(lb_type(m, get_struct_field_type(tag_type, i))); + } + } + + variant_value = llvm_const_named_struct(m, tag_type, vals, gb_count_of(vals)); + break; + } } @@ -1034,12 +1102,11 @@ gb_internal void lb_setup_type_info_data_giant_array(lbModule *m, i64 global_typ } -gb_internal void lb_setup_type_info_data(lbProcedure *p) { // NOTE(bill): Setup type_info data +gb_internal void lb_setup_type_info_data(lbModule *m) { // NOTE(bill): Setup type_info data if (build_context.no_rtti) { return; } - lbModule *m = p->module; i64 global_type_info_data_entity_count = 0; @@ -1050,7 +1117,7 @@ gb_internal void lb_setup_type_info_data(lbProcedure *p) { // NOTE(bill): Setup global_type_info_data_entity_count = type->Array.count; if (true) { - lb_setup_type_info_data_giant_array(m, global_type_info_data_entity_count, p); + lb_setup_type_info_data_giant_array(m, global_type_info_data_entity_count); } LLVMValueRef data = lb_global_type_info_data_ptr(m).value; From 5137d12d36e8631201bbf4fcb8ebacd24e7cc4c5 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Tue, 27 Feb 2024 15:40:45 +0000 Subject: [PATCH 24/40] Fix `lb_type_info` for the new layout --- src/llvm_backend_type.cpp | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/llvm_backend_type.cpp b/src/llvm_backend_type.cpp index 336678ce9..881ac3119 100644 --- a/src/llvm_backend_type.cpp +++ b/src/llvm_backend_type.cpp @@ -119,8 +119,14 @@ gb_internal lbValue lb_type_info(lbModule *m, Type *type) { isize index = lb_type_info_index(m->info, type); GB_ASSERT(index >= 0); - lbValue data = lb_global_type_info_data_ptr(m); - return lb_emit_array_epi(m, data, index); + LLVMValueRef global = lb_global_type_info_data_ptr(m).value; + + LLVMValueRef global_array = LLVMGetInitializer(global); + LLVMValueRef index_value = LLVMConstInt(lb_type(m, t_int), index, false); + lbValue res = {}; + res.value = LLVMConstPointerCast(LLVMConstExtractElement(global_array, index_value), lb_type(m, t_type_info_ptr)); + res.type = t_type_info_ptr; + return res; } gb_internal LLVMTypeRef lb_get_procedure_raw_type(lbModule *m, Type *type) { From 5107bdc06b7c1c8d02caef3e270e904218d82911 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Tue, 27 Feb 2024 15:45:53 +0000 Subject: [PATCH 25/40] Make `lb_type_info` use a procedure to load the global value --- src/llvm_backend.cpp | 2 +- src/llvm_backend.hpp | 2 +- src/llvm_backend_proc.cpp | 2 +- src/llvm_backend_stmt.cpp | 2 +- src/llvm_backend_type.cpp | 13 +++++-------- 5 files changed, 9 insertions(+), 12 deletions(-) diff --git a/src/llvm_backend.cpp b/src/llvm_backend.cpp index a8d2df181..efba19f23 100644 --- a/src/llvm_backend.cpp +++ b/src/llvm_backend.cpp @@ -1164,7 +1164,7 @@ gb_internal lbProcedure *lb_create_startup_runtime(lbModule *main_module, lbProc lbValue data = lb_emit_struct_ep(p, var.var, 0); lbValue ti = lb_emit_struct_ep(p, var.var, 1); lb_emit_store(p, data, lb_emit_conv(p, gp, t_rawptr)); - lb_emit_store(p, ti, lb_type_info(main_module, var_type)); + lb_emit_store(p, ti, lb_type_info(p, var_type)); } else { LLVMTypeRef vt = llvm_addr_type(p->module, var.var); lbValue src0 = lb_emit_conv(p, var.init, t); diff --git a/src/llvm_backend.hpp b/src/llvm_backend.hpp index 48e1c87c6..741557efd 100644 --- a/src/llvm_backend.hpp +++ b/src/llvm_backend.hpp @@ -485,7 +485,7 @@ gb_internal lbValue lb_emit_mul_add(lbProcedure *p, lbValue a, lbValue b, lbValu gb_internal void lb_fill_slice(lbProcedure *p, lbAddr const &slice, lbValue base_elem, lbValue len); -gb_internal lbValue lb_type_info(lbModule *m, Type *type); +gb_internal lbValue lb_type_info(lbProcedure *p, Type *type); gb_internal lbValue lb_find_or_add_entity_string(lbModule *m, String const &str); gb_internal lbValue lb_generate_anonymous_proc_lit(lbModule *m, String const &prefix_name, Ast *expr, lbProcedure *parent = nullptr); diff --git a/src/llvm_backend_proc.cpp b/src/llvm_backend_proc.cpp index 9419f9a3c..13b0171e4 100644 --- a/src/llvm_backend_proc.cpp +++ b/src/llvm_backend_proc.cpp @@ -1755,7 +1755,7 @@ gb_internal lbValue lb_build_builtin_proc(lbProcedure *p, Ast *expr, TypeAndValu TypeAndValue tav = type_and_value_of_expr(arg); if (tav.mode == Addressing_Type) { Type *t = default_type(type_of_expr(arg)); - return lb_type_info(p->module, t); + return lb_type_info(p, t); } GB_ASSERT(is_type_typeid(tav.type)); diff --git a/src/llvm_backend_stmt.cpp b/src/llvm_backend_stmt.cpp index 388b512b2..0de9c0bf9 100644 --- a/src/llvm_backend_stmt.cpp +++ b/src/llvm_backend_stmt.cpp @@ -748,7 +748,7 @@ gb_internal void lb_build_range_enum(lbProcedure *p, Type *enum_type, Type *val_ i64 enum_count = t->Enum.fields.count; lbValue max_count = lb_const_int(m, t_int, enum_count); - lbValue ti = lb_type_info(m, t); + lbValue ti = lb_type_info(p, t); lbValue variant = lb_emit_struct_ep(p, ti, 4); lbValue eti_ptr = lb_emit_conv(p, variant, t_type_info_enum_ptr); lbValue values = lb_emit_load(p, lb_emit_struct_ep(p, eti_ptr, 2)); diff --git a/src/llvm_backend_type.cpp b/src/llvm_backend_type.cpp index 881ac3119..aec1fb201 100644 --- a/src/llvm_backend_type.cpp +++ b/src/llvm_backend_type.cpp @@ -111,22 +111,19 @@ gb_internal lbValue lb_typeid(lbModule *m, Type *type) { return res; } -gb_internal lbValue lb_type_info(lbModule *m, Type *type) { +gb_internal lbValue lb_type_info(lbProcedure *p, Type *type) { GB_ASSERT(!build_context.no_rtti); type = default_type(type); + lbModule *m = p->module; isize index = lb_type_info_index(m->info, type); GB_ASSERT(index >= 0); - LLVMValueRef global = lb_global_type_info_data_ptr(m).value; + lbValue global = lb_global_type_info_data_ptr(m); - LLVMValueRef global_array = LLVMGetInitializer(global); - LLVMValueRef index_value = LLVMConstInt(lb_type(m, t_int), index, false); - lbValue res = {}; - res.value = LLVMConstPointerCast(LLVMConstExtractElement(global_array, index_value), lb_type(m, t_type_info_ptr)); - res.type = t_type_info_ptr; - return res; + lbValue ptr = lb_emit_array_epi(p, global, index); + return lb_emit_load(p, ptr); } gb_internal LLVMTypeRef lb_get_procedure_raw_type(lbModule *m, Type *type) { From d1174f66bc76e690c95e7b4cadc5b9607d1344a2 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Wed, 28 Feb 2024 13:41:54 +0000 Subject: [PATCH 26/40] Fix #3233 --- vendor/raylib/raylib.odin | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/vendor/raylib/raylib.odin b/vendor/raylib/raylib.odin index 3db4d4bbd..576be29e7 100644 --- a/vendor/raylib/raylib.odin +++ b/vendor/raylib/raylib.odin @@ -404,7 +404,7 @@ BoneInfo :: struct { } // Model type -Model :: struct { +Model :: struct #align(align_of(uintptr)) { transform: Matrix, // Local transform matrix meshCount: c.int, // Number of meshes @@ -499,7 +499,7 @@ VrDeviceInfo :: struct { } // VR Stereo rendering configuration for simulator -VrStereoConfig :: struct { +VrStereoConfig :: struct #align(4) { projection: [2]Matrix, // VR projection matrices (per eye) viewOffset: [2]Matrix, // VR view offset matrices (per eye) leftLensCenter: [2]f32, // VR left lens center From dce176fa39b34acca49c965809cad97060bf5ff3 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Wed, 28 Feb 2024 18:24:59 +0000 Subject: [PATCH 27/40] Remove unnecessary use of `transmute` --- core/os/os.odin | 6 ++---- core/os/os2/file_util.odin | 6 ++---- 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/core/os/os.odin b/core/os/os.odin index c74712d4e..6d0e22a04 100644 --- a/core/os/os.odin +++ b/core/os/os.odin @@ -160,13 +160,11 @@ write_entire_file :: proc(name: string, data: []byte, truncate := true) -> (succ } write_ptr :: proc(fd: Handle, data: rawptr, len: int) -> (int, Errno) { - s := transmute([]byte)mem.Raw_Slice{data, len} - return write(fd, s) + return write(fd, ([^]byte)(data)[:len]) } read_ptr :: proc(fd: Handle, data: rawptr, len: int) -> (int, Errno) { - s := transmute([]byte)mem.Raw_Slice{data, len} - return read(fd, s) + return read(fd, ([^]byte)(data)[:len]) } heap_allocator_proc :: runtime.heap_allocator_proc diff --git a/core/os/os2/file_util.odin b/core/os/os2/file_util.odin index e52d53f08..11d1f688d 100644 --- a/core/os/os2/file_util.odin +++ b/core/os/os2/file_util.odin @@ -64,13 +64,11 @@ write_encoded_rune :: proc(f: ^File, r: rune) -> (n: int, err: Error) { write_ptr :: proc(f: ^File, data: rawptr, len: int) -> (n: int, err: Error) { - s := transmute([]byte)mem.Raw_Slice{data, len} - return write(f, s) + return write(f, ([^]byte)(data)[:len]) } read_ptr :: proc(f: ^File, data: rawptr, len: int) -> (n: int, err: Error) { - s := transmute([]byte)mem.Raw_Slice{data, len} - return read(f, s) + return read(f, ([^]byte)(data)[:len]) } From e423a6d692613cd15aad8d9db4c61b452c61753f Mon Sep 17 00:00:00 2001 From: gingerBill Date: Wed, 28 Feb 2024 18:25:29 +0000 Subject: [PATCH 28/40] Make types `distinct` --- vendor/egl/egl.odin | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/vendor/egl/egl.odin b/vendor/egl/egl.odin index cf6a02b7a..3174fa60b 100644 --- a/vendor/egl/egl.odin +++ b/vendor/egl/egl.odin @@ -1,12 +1,12 @@ //+build linux package egl -NativeDisplayType :: rawptr -NativeWindowType :: rawptr -Display :: rawptr -Surface :: rawptr -Config :: rawptr -Context :: rawptr +NativeDisplayType :: distinct rawptr +NativeWindowType :: distinct rawptr +Display :: distinct rawptr +Surface :: distinct rawptr +Config :: distinct rawptr +Context :: distinct rawptr NO_DISPLAY :: Display(uintptr(0)) NO_CONTEXT :: Context(uintptr(0)) From 7df7fec6f736a52712961a2a54de3747eac4ec84 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Wed, 28 Feb 2024 18:27:49 +0000 Subject: [PATCH 29/40] Keep -vet happy --- core/os/os.odin | 1 - core/os/os2/file_util.odin | 1 - 2 files changed, 2 deletions(-) diff --git a/core/os/os.odin b/core/os/os.odin index 6d0e22a04..aa460fe01 100644 --- a/core/os/os.odin +++ b/core/os/os.odin @@ -1,7 +1,6 @@ package os import "base:runtime" -import "core:mem" import "core:strconv" import "core:unicode/utf8" diff --git a/core/os/os2/file_util.odin b/core/os/os2/file_util.odin index 11d1f688d..459544fc0 100644 --- a/core/os/os2/file_util.odin +++ b/core/os/os2/file_util.odin @@ -1,6 +1,5 @@ package os2 -import "core:mem" import "base:runtime" import "core:strconv" import "core:unicode/utf8" From 17b1c8d338ac9f6b6bdef2cf32fd472212b515e9 Mon Sep 17 00:00:00 2001 From: Laytan Laats Date: Thu, 29 Feb 2024 16:15:15 +0100 Subject: [PATCH 30/40] fix releases being a zip of a zip --- .github/workflows/nightly.yml | 8 ++++---- ci/upload_create_nightly.sh | 16 ++++++++++++++-- 2 files changed, 18 insertions(+), 6 deletions(-) mode change 100644 => 100755 ci/upload_create_nightly.sh diff --git a/.github/workflows/nightly.yml b/.github/workflows/nightly.yml index 4da7d42f7..0a344ebf1 100644 --- a/.github/workflows/nightly.yml +++ b/.github/workflows/nightly.yml @@ -107,7 +107,7 @@ jobs: build_macos_arm: name: MacOS ARM Build if: github.repository == 'odin-lang/Odin' - runs-on: macos-14 + runs-on: macos-14 # ARM machine steps: - uses: actions/checkout@v1 - name: Download LLVM and setup PATH @@ -190,9 +190,9 @@ jobs: echo Uploading artifcates to B2 chmod +x ./ci/upload_create_nightly.sh ./ci/upload_create_nightly.sh "$BUCKET" windows-amd64 windows_artifacts/ - ./ci/upload_create_nightly.sh "$BUCKET" ubuntu-amd64 ubuntu_artifacts/ - ./ci/upload_create_nightly.sh "$BUCKET" macos-amd64 macos_artifacts/ - ./ci/upload_create_nightly.sh "$BUCKET" macos-arm64 macos_arm_artifacts/ + ./ci/upload_create_nightly.sh "$BUCKET" ubuntu-amd64 ubuntu_artifacts/dist.zip + ./ci/upload_create_nightly.sh "$BUCKET" macos-amd64 macos_artifacts/dist.zip + ./ci/upload_create_nightly.sh "$BUCKET" macos-arm64 macos_arm_artifacts/dist.zip echo Deleting old artifacts in B2 python3 ci/delete_old_binaries.py "$BUCKET" "$DAYS_TO_KEEP" diff --git a/ci/upload_create_nightly.sh b/ci/upload_create_nightly.sh old mode 100644 new mode 100755 index 754b9b87c..065cb13bf --- a/ci/upload_create_nightly.sh +++ b/ci/upload_create_nightly.sh @@ -1,5 +1,7 @@ #!/bin/bash +set -e + bucket=$1 platform=$2 artifact=$3 @@ -9,5 +11,15 @@ filename="odin-$platform-nightly+$now.zip" echo "Creating archive $filename from $artifact and uploading to $bucket" -7z a -bd "output/$filename" -r "$artifact" -b2 upload-file --noProgress "$bucket" "output/$filename" "nightly/$filename" \ No newline at end of file +# If this is already zipped up (done before artifact upload to keep permissions in tact), just move it. +if [ "${artifact: -4}" == ".zip" ] +then + echo "Artifact already a zip" + mkdir -p "output" + mv "$artifact" "output/$filename" +else + echo "Artifact needs to be zipped" + 7z a -bd "output/$filename" -r "$artifact" +fi + +b2 upload-file --noProgress "$bucket" "output/$filename" "nightly/$filename" From 6805b85f898a361b2b03f1ff6e9872e13442b747 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Thu, 29 Feb 2024 20:20:37 +0000 Subject: [PATCH 31/40] Fix leap year bug --- core/time/time.odin | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/core/time/time.odin b/core/time/time.odin index 7911457de..72a09ad94 100644 --- a/core/time/time.odin +++ b/core/time/time.odin @@ -369,6 +369,10 @@ datetime_to_time :: proc "contextless" (year, month, day, hour, minute, second: mod = year % divisor return } + _is_leap_year :: proc "contextless" (year: int) -> bool { + return year%4 == 0 && (year%100 != 0 || year%400 == 0) + } + ok = true @@ -395,6 +399,10 @@ datetime_to_time :: proc "contextless" (year, month, day, hour, minute, second: days += int(days_before[_m]) + _d + if _is_leap_year(year) && _m >= 2 { + days += 1 + } + s += i64(days) * SECONDS_PER_DAY s += i64(hour) * SECONDS_PER_HOUR s += i64(minute) * SECONDS_PER_MINUTE From 23cd64ec357d4ba66476878dbe6cc63f71dac0a9 Mon Sep 17 00:00:00 2001 From: Laytan Laats Date: Thu, 29 Feb 2024 23:39:18 +0100 Subject: [PATCH 32/40] reduce dynamic library dependencies for macos release --- .github/workflows/nightly.yml | 8 ++++++-- build_odin.sh | 3 +-- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/.github/workflows/nightly.yml b/.github/workflows/nightly.yml index 0a344ebf1..709f968a7 100644 --- a/.github/workflows/nightly.yml +++ b/.github/workflows/nightly.yml @@ -83,7 +83,9 @@ jobs: TMP_PATH=$(xcrun --show-sdk-path)/user/include echo "CPATH=$TMP_PATH" >> $GITHUB_ENV - name: build odin - run: make nightly + # These -L makes the linker prioritize system libraries over LLVM libraries, this is mainly to + # not link with libunwind bundled with LLVM but link with libunwind on the system. + run: CXXFLAGS="-L/usr/lib/system -L/usr/lib" make nightly - name: Bundle run: | mkdir dist @@ -117,7 +119,9 @@ jobs: TMP_PATH=$(xcrun --show-sdk-path)/user/include echo "CPATH=$TMP_PATH" >> $GITHUB_ENV - name: build odin - run: make nightly + # These -L makes the linker prioritize system libraries over LLVM libraries, this is mainly to + # not link with libunwind bundled with LLVM but link with libunwind on the system. + run: CXXFLAGS="-L/usr/lib/system -L/usr/lib" make nightly - name: Bundle run: | mkdir dist diff --git a/build_odin.sh b/build_odin.sh index 589aeb550..fab6c5fd1 100755 --- a/build_odin.sh +++ b/build_odin.sh @@ -63,8 +63,7 @@ Darwin) fi CXXFLAGS="$CXXFLAGS $($LLVM_CONFIG --cxxflags --ldflags)" - LDFLAGS="$LDFLAGS -liconv -ldl -framework System" - LDFLAGS="$LDFLAGS -lLLVM-C" + LDFLAGS="$LDFLAGS -liconv -ldl -framework System -lLLVM" ;; FreeBSD) CXXFLAGS="$CXXFLAGS $($LLVM_CONFIG --cxxflags --ldflags)" From 7f1069cb0bb5a1f66fbd7836f642527938957851 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Fri, 1 Mar 2024 11:31:13 +0000 Subject: [PATCH 33/40] Add Recursive_Mutex procedures to procedure groups --- core/sync/sync_util.odin | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/core/sync/sync_util.odin b/core/sync/sync_util.odin index 0c948eb9e..07b970a82 100644 --- a/core/sync/sync_util.odin +++ b/core/sync/sync_util.odin @@ -9,6 +9,7 @@ Example: guard :: proc{ mutex_guard, rw_mutex_guard, + recursive_mutex_guard, ticket_mutex_guard, benaphore_guard, recursive_benaphore_guard, @@ -31,6 +32,7 @@ shared_guard :: proc{ lock :: proc{ mutex_lock, rw_mutex_lock, + recursive_mutex_lock, ticket_mutex_lock, benaphore_lock, recursive_benaphore_lock, @@ -43,6 +45,7 @@ lock :: proc{ unlock :: proc{ mutex_unlock, rw_mutex_unlock, + recursive_mutex_unlock, ticket_mutex_unlock, benaphore_unlock, recursive_benaphore_unlock, @@ -55,6 +58,7 @@ unlock :: proc{ try_lock :: proc{ mutex_try_lock, rw_mutex_try_lock, + recursive_mutex_try_lock, benaphore_try_lock, recursive_benaphore_try_lock, atomic_mutex_try_lock, From 5c20676c76ed888b230c80596f7ed20ee9b19183 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Fri, 1 Mar 2024 13:36:06 +0000 Subject: [PATCH 34/40] Fix type for SDL LoadFile(_RW) --- vendor/sdl2/sdl_rwops.odin | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/vendor/sdl2/sdl_rwops.odin b/vendor/sdl2/sdl_rwops.odin index 86fb23c75..28d09511b 100644 --- a/vendor/sdl2/sdl_rwops.odin +++ b/vendor/sdl2/sdl_rwops.odin @@ -88,8 +88,8 @@ foreign lib { RWwrite :: proc(ctx: ^RWops, size: c.size_t, num: c.size_t) -> c.size_t --- RWclose :: proc(ctx: ^RWops) -> c.int --- - LoadFile_RW :: proc(src: ^RWops, datasize: c.size_t, freesrc: bool) -> rawptr --- - LoadFile :: proc(file: rawptr, datasize: c.size_t) -> rawptr --- + LoadFile_RW :: proc(src: ^RWops, datasize: ^c.size_t, freesrc: bool) -> rawptr --- + LoadFile :: proc(file: rawptr, datasize: ^c.size_t) -> rawptr --- ReadU8 :: proc(src: ^RWops) -> u8 --- ReadLE16 :: proc(src: ^RWops) -> u16 --- From ff24cfe314b0dc121a5ab6d600ae06c31a29b3d3 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Fri, 1 Mar 2024 14:00:14 +0000 Subject: [PATCH 35/40] Fix debug issue with `map`s --- src/llvm_backend_debug.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/llvm_backend_debug.cpp b/src/llvm_backend_debug.cpp index 7d3692a53..c06026568 100644 --- a/src/llvm_backend_debug.cpp +++ b/src/llvm_backend_debug.cpp @@ -746,8 +746,8 @@ gb_internal void lb_debug_complete_types(lbModule *m) { case Type_Map: GB_ASSERT(t_raw_map != nullptr); - bt = base_type(bt->Map.debug_metadata_type); - // bt = base_type(t_raw_map); + // bt = base_type(bt->Map.debug_metadata_type); + bt = base_type(t_raw_map); GB_ASSERT(bt->kind == Type_Struct); /*fallthrough*/ case Type_Struct: From 674bd94f72b0faa50f64aaa0f0ad9ebee59f198b Mon Sep 17 00:00:00 2001 From: gingerBill Date: Fri, 1 Mar 2024 18:30:23 +0000 Subject: [PATCH 36/40] Add check to see if raylib is imported with `ShowCursor` on sys/windows --- core/sys/windows/user32.odin | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/core/sys/windows/user32.odin b/core/sys/windows/user32.odin index 64e39df39..8c0fa246d 100644 --- a/core/sys/windows/user32.odin +++ b/core/sys/windows/user32.odin @@ -1,6 +1,7 @@ // +build windows package sys_windows +import "base:intrinsics" foreign import user32 "system:User32.lib" @(default_calling_convention="system") @@ -155,6 +156,9 @@ foreign user32 { GetCursorPos :: proc(lpPoint: LPPOINT) -> BOOL --- SetCursorPos :: proc(X: c_int, Y: c_int) -> BOOL --- SetCursor :: proc(hCursor: HCURSOR) -> HCURSOR --- + when !intrinsics.is_package_imported("raylib") { + ShowCursor :: proc(bShow: BOOL) -> INT --- + } EnumDisplaySettingsW :: proc(lpszDeviceName: LPCWSTR, iModeNum: DWORD, lpDevMode: ^DEVMODEW) -> BOOL --- From 4c35633e0147b481dd7b2352d6bdb603f78c6dc7 Mon Sep 17 00:00:00 2001 From: Jeroen van Rijn Date: Sun, 3 Mar 2024 15:09:38 +0100 Subject: [PATCH 37/40] math.big constants were no longer initialized Fixes #3243 --- core/math/big/helpers.odin | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/core/math/big/helpers.odin b/core/math/big/helpers.odin index 8ab19e3e7..35be4f1fd 100644 --- a/core/math/big/helpers.odin +++ b/core/math/big/helpers.odin @@ -777,6 +777,11 @@ int_from_bytes_little_python :: proc(a: ^Int, buf: []u8, signed := false, alloca */ INT_ONE, INT_ZERO, INT_MINUS_ONE, INT_INF, INT_MINUS_INF, INT_NAN := &Int{}, &Int{}, &Int{}, &Int{}, &Int{}, &Int{} +@(init, private) +_init_constants :: proc() { + initialize_constants() +} + initialize_constants :: proc() -> (res: int) { internal_set( INT_ZERO, 0); INT_ZERO.flags = {.Immutable} internal_set( INT_ONE, 1); INT_ONE.flags = {.Immutable} @@ -788,7 +793,7 @@ initialize_constants :: proc() -> (res: int) { */ internal_set( INT_NAN, 1); INT_NAN.flags = {.Immutable, .NaN} internal_set( INT_INF, 1); INT_INF.flags = {.Immutable, .Inf} - internal_set( INT_INF, -1); INT_MINUS_INF.flags = {.Immutable, .Inf} + internal_set(INT_MINUS_INF, -1); INT_MINUS_INF.flags = {.Immutable, .Inf} return _DEFAULT_MUL_KARATSUBA_CUTOFF } From de41c2256d98e8b2e2742e6fd7266bdc2a5e970d Mon Sep 17 00:00:00 2001 From: Ed Yu Date: Mon, 4 Mar 2024 10:14:51 -0800 Subject: [PATCH 38/40] For invmod, b has to be > 1, fix a logic typo --- core/math/big/internal.odin | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/core/math/big/internal.odin b/core/math/big/internal.odin index 829cbf0e2..35c95f465 100644 --- a/core/math/big/internal.odin +++ b/core/math/big/internal.odin @@ -2046,9 +2046,9 @@ internal_int_inverse_modulo :: proc(dest, a, b: ^Int, allocator := context.alloc if internal_is_positive(a) && internal_eq(b, 1) { return internal_zero(dest) } /* - `b` cannot be negative and has to be > 1 + `b` cannot be negative and b has to be > 1 */ - if internal_is_negative(b) || internal_gt(b, 1) { return .Invalid_Argument } + if internal_is_negative(b) || !internal_gt(b, 1) { return .Invalid_Argument } /* If the modulus is odd we can use a faster routine instead. @@ -2954,4 +2954,4 @@ internal_zero_unused :: proc { internal_int_zero_unused, } /* ========================== End of low-level routines ========================== -*/ \ No newline at end of file +*/ From 3e295734cb5bc6e4e6e446d3f53d8138947f225a Mon Sep 17 00:00:00 2001 From: gingerBill Date: Mon, 4 Mar 2024 20:10:34 +0000 Subject: [PATCH 39/40] Correct `is_type_comparable` for `bit_field` --- src/types.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/types.cpp b/src/types.cpp index 57ae4e81d..e4db31246 100644 --- a/src/types.cpp +++ b/src/types.cpp @@ -2400,6 +2400,9 @@ gb_internal bool is_type_comparable(Type *t) { case Type_SimdVector: return true; + + case Type_BitField: + return is_type_comparable(t->BitField.backing_type); } return false; } From 7ae22b7ce507dca47c3da7aa6d750a8fb557e1ad Mon Sep 17 00:00:00 2001 From: gingerBill Date: Mon, 4 Mar 2024 20:22:49 +0000 Subject: [PATCH 40/40] Update `are_types_identical` for `bit_field` --- src/types.cpp | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/types.cpp b/src/types.cpp index e4db31246..b442acd53 100644 --- a/src/types.cpp +++ b/src/types.cpp @@ -2810,6 +2810,29 @@ gb_internal bool are_types_identical_internal(Type *x, Type *y, bool check_tuple return are_types_identical(x->SimdVector.elem, y->SimdVector.elem); } break; + + case Type_BitField: + if (are_types_identical(x->BitField.backing_type, y->BitField.backing_type) && + x->BitField.fields.count == y->BitField.fields.count) { + for_array(i, x->BitField.fields) { + Entity *a = x->BitField.fields[i]; + Entity *b = y->BitField.fields[i]; + if (!are_types_identical(a->type, b->type)) { + return false; + } + if (a->token.string != b->token.string) { + return false; + } + if (x->BitField.bit_sizes[i] != y->BitField.bit_sizes[i]) { + return false; + } + if (x->BitField.bit_offsets[i] != y->BitField.bit_offsets[i]) { + return false; + } + } + return true; + } + break; } return false;