diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 41f548119..53600b258 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -6,7 +6,7 @@ jobs: name: NetBSD Build, Check, and Test runs-on: ubuntu-latest env: - PKGSRC_BRANCH: 2024Q2 + PKGSRC_BRANCH: 2024Q3 steps: - uses: actions/checkout@v4 - name: Build, Check, and Test diff --git a/base/runtime/core.odin b/base/runtime/core.odin index a5a3a4d8c..e47f3ecbc 100644 --- a/base/runtime/core.odin +++ b/base/runtime/core.odin @@ -171,14 +171,6 @@ Type_Info_Simd_Vector :: struct { elem_size: int, count: int, } -Type_Info_Relative_Pointer :: struct { - pointer: ^Type_Info, // ^T - base_integer: ^Type_Info, -} -Type_Info_Relative_Multi_Pointer :: struct { - pointer: ^Type_Info, // [^]T - base_integer: ^Type_Info, -} Type_Info_Matrix :: struct { elem: ^Type_Info, elem_size: int, @@ -241,8 +233,6 @@ Type_Info :: struct { Type_Info_Map, Type_Info_Bit_Set, Type_Info_Simd_Vector, - Type_Info_Relative_Pointer, - Type_Info_Relative_Multi_Pointer, Type_Info_Matrix, Type_Info_Soa_Pointer, Type_Info_Bit_Field, @@ -275,8 +265,6 @@ Typeid_Kind :: enum u8 { Map, Bit_Set, Simd_Vector, - Relative_Pointer, - Relative_Multi_Pointer, Matrix, Soa_Pointer, Bit_Field, diff --git a/base/runtime/core_builtin.odin b/base/runtime/core_builtin.odin index 8b7a5004a..d28dadd02 100644 --- a/base/runtime/core_builtin.odin +++ b/base/runtime/core_builtin.odin @@ -388,7 +388,7 @@ _make_dynamic_array_len_cap :: proc(array: ^Raw_Dynamic_Array, size_of_elem, ali // // Note: Prefer using the procedure group `make`. @(builtin, require_results) -make_map :: proc($T: typeid/map[$K]$E, allocator := context.allocator) -> (m: T) { +make_map :: proc($T: typeid/map[$K]$E, allocator := context.allocator, loc := #caller_location) -> (m: T) { m.allocator = allocator return m } @@ -399,7 +399,7 @@ make_map :: proc($T: typeid/map[$K]$E, allocator := context.allocator) -> (m: T) // // Note: Prefer using the procedure group `make`. @(builtin, require_results) -make_map_cap :: proc($T: typeid/map[$K]$E, #any_int capacity: int = 1< (m: T, err: Allocator_Error) #optional_allocator_error { +make_map_cap :: proc($T: typeid/map[$K]$E, #any_int capacity: int, allocator := context.allocator, loc := #caller_location) -> (m: T, err: Allocator_Error) #optional_allocator_error { make_map_expr_error_loc(loc, capacity) context.allocator = allocator diff --git a/base/runtime/core_builtin_soa.odin b/base/runtime/core_builtin_soa.odin index 2980b7a9e..ff27a4559 100644 --- a/base/runtime/core_builtin_soa.odin +++ b/base/runtime/core_builtin_soa.odin @@ -142,6 +142,7 @@ make_soa_slice :: proc($T: typeid/#soa[]$E, #any_int length: int, allocator := c @(builtin, require_results) make_soa_dynamic_array :: proc($T: typeid/#soa[dynamic]$E, allocator := context.allocator, loc := #caller_location) -> (array: T, err: Allocator_Error) #optional_allocator_error { context.allocator = allocator + array.allocator = allocator reserve_soa(&array, 0, loc) or_return return array, nil } @@ -149,6 +150,7 @@ make_soa_dynamic_array :: proc($T: typeid/#soa[dynamic]$E, allocator := context. @(builtin, require_results) make_soa_dynamic_array_len :: proc($T: typeid/#soa[dynamic]$E, #any_int length: int, allocator := context.allocator, loc := #caller_location) -> (array: T, err: Allocator_Error) #optional_allocator_error { context.allocator = allocator + array.allocator = allocator resize_soa(&array, length, loc) or_return return array, nil } diff --git a/base/runtime/print.odin b/base/runtime/print.odin index 45f6f01ef..c28fd593d 100644 --- a/base/runtime/print.odin +++ b/base/runtime/print.odin @@ -486,18 +486,6 @@ print_type :: #force_no_inline proc "contextless" (ti: ^Type_Info) { print_u64(u64(info.count)) print_byte(']') print_type(info.elem) - - case Type_Info_Relative_Pointer: - print_string("#relative(") - print_type(info.base_integer) - print_string(") ") - print_type(info.pointer) - - case Type_Info_Relative_Multi_Pointer: - print_string("#relative(") - print_type(info.base_integer) - print_string(") ") - print_type(info.pointer) case Type_Info_Matrix: print_string("matrix[") diff --git a/bin/RAD-LICENSE b/bin/RAD-LICENSE new file mode 100644 index 000000000..1a1ccbcd2 --- /dev/null +++ b/bin/RAD-LICENSE @@ -0,0 +1,19 @@ +Copyright (c) 2024 Epic Games Tools + +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the “Software”), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies +of the Software, and to permit persons to whom the Software is furnished to do +so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. \ No newline at end of file diff --git a/bin/radlink.exe b/bin/radlink.exe new file mode 100644 index 000000000..bb2fd206e Binary files /dev/null and b/bin/radlink.exe differ diff --git a/build.bat b/build.bat index 55c71ca9f..a788a8c04 100644 --- a/build.bat +++ b/build.bat @@ -19,7 +19,11 @@ if "%VSCMD_ARG_TGT_ARCH%" neq "x64" ( ) ) -for /f %%i in ('powershell get-date -format "{yyyyMMdd}"') do ( +pushd misc +cl /nologo get-date.c +popd + +for /f %%i in ('misc\get-date') do ( set CURR_DATE_TIME=%%i ) set curr_year=%CURR_DATE_TIME:~0,4% @@ -58,7 +62,6 @@ set V4=0 set odin_version_full="%V1%.%V2%.%V3%.%V4%" set odin_version_raw="dev-%V1%-%V2%" - set compiler_flags= -nologo -Oi -TP -fp:precise -Gm- -MP -FC -EHsc- -GR- -GF rem Parse source code as utf-8 even on shift-jis and other codepages rem See https://learn.microsoft.com/en-us/cpp/build/reference/utf-8-set-source-and-executable-character-sets-to-utf-8?view=msvc-170 diff --git a/build_odin.sh b/build_odin.sh index c06004ea8..3547689d5 100755 --- a/build_odin.sh +++ b/build_odin.sh @@ -25,7 +25,8 @@ error() { # Brew advises people not to add llvm to their $PATH, so try and use brew to find it. if [ -z "$LLVM_CONFIG" ] && [ -n "$(command -v brew)" ]; then - if [ -n "$(command -v $(brew --prefix llvm@18)/bin/llvm-config)" ]; then LLVM_CONFIG="$(brew --prefix llvm@18)/bin/llvm-config" + if [ -n "$(command -v $(brew --prefix llvm@19)/bin/llvm-config)" ]; then LLVM_CONFIG="$(brew --prefix llvm@19)/bin/llvm-config" + elif [ -n "$(command -v $(brew --prefix llvm@18)/bin/llvm-config)" ]; then LLVM_CONFIG="$(brew --prefix llvm@18)/bin/llvm-config" elif [ -n "$(command -v $(brew --prefix llvm@17)/bin/llvm-config)" ]; then LLVM_CONFIG="$(brew --prefix llvm@17)/bin/llvm-config" elif [ -n "$(command -v $(brew --prefix llvm@14)/bin/llvm-config)" ]; then LLVM_CONFIG="$(brew --prefix llvm@14)/bin/llvm-config" fi @@ -33,13 +34,15 @@ fi if [ -z "$LLVM_CONFIG" ]; then # darwin, linux, openbsd - if [ -n "$(command -v llvm-config-18)" ]; then LLVM_CONFIG="llvm-config-18" + if [ -n "$(command -v llvm-config-19)" ]; then LLVM_CONFIG="llvm-config-19" + elif [ -n "$(command -v llvm-config-18)" ]; then LLVM_CONFIG="llvm-config-18" elif [ -n "$(command -v llvm-config-17)" ]; then LLVM_CONFIG="llvm-config-17" elif [ -n "$(command -v llvm-config-14)" ]; then LLVM_CONFIG="llvm-config-14" elif [ -n "$(command -v llvm-config-13)" ]; then LLVM_CONFIG="llvm-config-13" elif [ -n "$(command -v llvm-config-12)" ]; then LLVM_CONFIG="llvm-config-12" elif [ -n "$(command -v llvm-config-11)" ]; then LLVM_CONFIG="llvm-config-11" # freebsd + elif [ -n "$(command -v llvm-config19)" ]; then LLVM_CONFIG="llvm-config19" elif [ -n "$(command -v llvm-config18)" ]; then LLVM_CONFIG="llvm-config18" elif [ -n "$(command -v llvm-config17)" ]; then LLVM_CONFIG="llvm-config17" elif [ -n "$(command -v llvm-config14)" ]; then LLVM_CONFIG="llvm-config14" @@ -66,15 +69,15 @@ LLVM_VERSION_MAJOR="$(echo $LLVM_VERSION | awk -F. '{print $1}')" LLVM_VERSION_MINOR="$(echo $LLVM_VERSION | awk -F. '{print $2}')" LLVM_VERSION_PATCH="$(echo $LLVM_VERSION | awk -F. '{print $3}')" -if [ $LLVM_VERSION_MAJOR -lt 11 ] || ([ $LLVM_VERSION_MAJOR -gt 14 ] && [ $LLVM_VERSION_MAJOR -lt 17 ]) || [ $LLVM_VERSION_MAJOR -gt 18 ]; then - error "Invalid LLVM version $LLVM_VERSION: must be 11, 12, 13, 14, 17 or 18" +if [ $LLVM_VERSION_MAJOR -lt 11 ] || ([ $LLVM_VERSION_MAJOR -gt 14 ] && [ $LLVM_VERSION_MAJOR -lt 17 ]) || [ $LLVM_VERSION_MAJOR -gt 19 ]; then + error "Invalid LLVM version $LLVM_VERSION: must be 11, 12, 13, 14, 17, 18 or 19" fi case "$OS_NAME" in Darwin) if [ "$OS_ARCH" = "arm64" ]; then if [ $LLVM_VERSION_MAJOR -lt 13 ]; then - error "Invalid LLVM version $LLVM_VERSION: Darwin Arm64 requires LLVM 13, 14, 17 or 18" + error "Invalid LLVM version $LLVM_VERSION: Darwin Arm64 requires LLVM 13, 14, 17, 18 or 19" fi fi @@ -152,9 +155,7 @@ build_odin() { } run_demo() { - if [ $# -eq 0 ] || [ "$1" = "debug" ]; then - ./odin run examples/demo -vet -strict-style -- Hellope World - fi + ./odin run examples/demo -vet -strict-style -- Hellope World } if [ $# -eq 0 ]; then @@ -166,14 +167,20 @@ if [ $# -eq 0 ]; then elif [ $# -eq 1 ]; then case $1 in report) - [ ! -f "./odin" ] && build_odin debug + if [ ! -f "./odin" ]; then + build_odin debug + run_demo + fi ./odin report ;; + debug) + build_odin debug + run_demo + ;; *) build_odin $1 ;; esac - run_demo else error "Too many arguments!" fi diff --git a/core/c/frontend/preprocessor/const_expr.odin b/core/c/frontend/preprocessor/const_expr.odin deleted file mode 100644 index ff13f6432..000000000 --- a/core/c/frontend/preprocessor/const_expr.odin +++ /dev/null @@ -1,25 +0,0 @@ -package c_frontend_preprocess - -import "core:c/frontend/tokenizer" - -const_expr :: proc(rest: ^^Token, tok: ^Token) -> i64 { - // TODO(bill): Handle const_expr correctly - // This is effectively a mini-parser - - assert(rest != nil) - assert(tok != nil) - rest^ = tokenizer.new_eof(tok) - switch v in tok.val { - case i64: - return v - case f64: - return i64(v) - case string: - return 0 - case []u16: - // TODO - case []u32: - // TODO - } - return 0 -} diff --git a/core/c/frontend/preprocessor/preprocess.odin b/core/c/frontend/preprocessor/preprocess.odin deleted file mode 100644 index b5eab0bb3..000000000 --- a/core/c/frontend/preprocessor/preprocess.odin +++ /dev/null @@ -1,1510 +0,0 @@ -package c_frontend_preprocess - -import "../tokenizer" - -import "core:strings" -import "core:strconv" -import "core:path/filepath" -import "core:unicode/utf8" -import "core:unicode/utf16" -import "core:os" -import "core:io" - -@(private) -Tokenizer :: tokenizer.Tokenizer -@(private) -Token :: tokenizer.Token - -Error_Handler :: tokenizer.Error_Handler - -Macro_Param :: struct { - next: ^Macro_Param, - name: string, -} - -Macro_Arg :: struct { - next: ^Macro_Arg, - name: string, - tok: ^Token, - is_va_args: bool, -} - -Macro_Kind :: enum u8 { - Function_Like, - Value_Like, -} - -Macro_Handler :: #type proc(^Preprocessor, ^Token) -> ^Token - -Macro :: struct { - name: string, - kind: Macro_Kind, - params: ^Macro_Param, - va_args_name: string, - body: ^Token, - handler: Macro_Handler, -} - -Cond_Incl_State :: enum u8 { - In_Then, - In_Elif, - In_Else, -} - -Cond_Incl :: struct { - next: ^Cond_Incl, - tok: ^Token, - state: Cond_Incl_State, - included: bool, -} - -Pragma_Handler :: #type proc(^Preprocessor, ^Token) - -Preprocessor :: struct { - // Lookup tables - macros: map[string]^Macro, - pragma_once: map[string]bool, - include_guards: map[string]string, - filepath_cache: map[string]string, - - // Include path data - include_paths: []string, - - // Counter for __COUNTER__ macro - counter: i64, - - // Include information - cond_incl: ^Cond_Incl, - include_level: int, - include_next_index: int, - - wide_char_size: int, - - // Mutable data - err: Error_Handler, - warn: Error_Handler, - pragma_handler: Pragma_Handler, - error_count: int, - warning_count: int, -} - -MAX_INCLUDE_LEVEL :: 1024 - -error :: proc(cpp: ^Preprocessor, tok: ^Token, msg: string, args: ..any) { - if cpp.err != nil { - cpp.err(tok.pos, msg, ..args) - } - cpp.error_count += 1 -} - -warn :: proc(cpp: ^Preprocessor, tok: ^Token, msg: string, args: ..any) { - if cpp.warn != nil { - cpp.warn(tok.pos, msg, ..args) - } - cpp.warning_count += 1 -} - -is_hash :: proc(tok: ^Token) -> bool { - return tok.at_bol && tok.lit == "#" -} - -skip_line :: proc(cpp: ^Preprocessor, tok: ^Token) -> ^Token { - tok := tok - if tok.at_bol { - return tok - } - warn(cpp, tok, "extra token") - for tok.at_bol { - tok = tok.next - } - return tok -} - - -append_token :: proc(a, b: ^Token) -> ^Token { - if a.kind == .EOF { - return b - } - - head: Token - curr := &head - - for tok := a; tok.kind != .EOF; tok = tok.next { - curr.next = tokenizer.copy_token(tok) - curr = curr.next - } - curr.next = b - return head.next -} - - -is_hex_digit :: proc(x: byte) -> bool { - switch x { - case '0'..='9', 'a'..='f', 'A'..='F': - return true - } - return false -} -from_hex :: proc(x: byte) -> i32 { - switch x { - case '0'..='9': - return i32(x) - '0' - case 'a'..='f': - return i32(x) - 'a' + 10 - case 'A'..='F': - return i32(x) - 'A' + 10 - } - return 16 -} - - -convert_pp_number :: proc(tok: ^Token) { - convert_pp_int :: proc(tok: ^Token) -> bool { - p := tok.lit - base := 10 - if len(p) > 2 { - if strings.equal_fold(p[:2], "0x") && is_hex_digit(p[2]) { - p = p[2:] - base = 16 - } else if strings.equal_fold(p[:2], "0b") && p[2] == '0' || p[2] == '1' { - p = p[2:] - base = 2 - } - } - if base == 10 && p[0] == '0' { - base = 8 - } - - - tok.val, _ = strconv.parse_i64_of_base(p, base) - - l, u: int - - suf: [3]byte - suf_n := 0 - i := len(p)-1 - for /**/; i >= 0 && suf_n < len(suf); i -= 1 { - switch p[i] { - case 'l', 'L': - suf[suf_n] = 'l' - l += 1 - suf_n += 1 - case 'u', 'U': - suf[suf_n] = 'u' - u += 1 - suf_n += 1 - } - } - if i < len(p) { - if !is_hex_digit(p[i]) && p[i] != '.' { - return false - } - } - if u > 1 { - return false - } - - if l > 2 { - return false - } - - if u == 1 { - switch l { - case 0: tok.type_hint = .Unsigned_Int - case 1: tok.type_hint = .Unsigned_Long - case 2: tok.type_hint = .Unsigned_Long_Long - } - } else { - switch l { - case 0: tok.type_hint = .Int - case 1: tok.type_hint = .Long - case 2: tok.type_hint = .Long_Long - } - } - return true - } - - if convert_pp_int(tok) { - return - } - - fval, _ := strconv.parse_f64(tok.lit) - tok.val = fval - - end := tok.lit[len(tok.lit)-1] - switch end { - case 'f', 'F': - tok.type_hint = .Float - case 'l', 'L': - tok.type_hint = .Long_Double - case: - tok.type_hint = .Double - } - -} - -convert_pp_char :: proc(tok: ^Token) { - assert(len(tok.lit) >= 2) - r, _, _, _ := unquote_char(tok.lit, tok.lit[0]) - tok.val = i64(r) - - tok.type_hint = .Int - switch tok.prefix { - case "u": tok.type_hint = .UTF_16 - case "U": tok.type_hint = .UTF_32 - case "L": tok.type_hint = .UTF_Wide - } -} - -wide_char_size :: proc(cpp: ^Preprocessor) -> int { - char_size := 4 - if cpp.wide_char_size > 0 { - char_size = clamp(cpp.wide_char_size, 1, 4) - assert(char_size & (char_size-1) == 0) - } - return char_size -} - -convert_pp_string :: proc(cpp: ^Preprocessor, tok: ^Token) { - assert(len(tok.lit) >= 2) - str, _, _ := unquote_string(tok.lit) - tok.val = str - - char_size := 1 - - switch tok.prefix { - case "u8": - tok.type_hint = .UTF_8 - char_size = 1 - case "u": - tok.type_hint = .UTF_16 - char_size = 2 - case "U": - tok.type_hint = .UTF_32 - char_size = 4 - case "L": - tok.type_hint = .UTF_Wide - char_size = wide_char_size(cpp) - } - - switch char_size { - case 2: - n: int - buf := make([]u16, len(str)) - for c in str { - ch := c - if ch < 0x10000 { - buf[n] = u16(ch) - n += 1 - } else { - ch -= 0x10000 - buf[n+0] = 0xd800 + u16((ch >> 10) & 0x3ff) - buf[n+1] = 0xdc00 + u16(ch & 0x3ff) - n += 2 - } - } - tok.val = buf[:n] - case 4: - n: int - buf := make([]u32, len(str)) - for ch in str { - buf[n] = u32(ch) - n += 1 - } - tok.val = buf[:n] - } - -} - -convert_pp_token :: proc(cpp: ^Preprocessor, t: ^Token, is_keyword: tokenizer.Is_Keyword_Proc) { - switch { - case t.kind == .Char: - convert_pp_char(t) - case t.kind == .String: - convert_pp_string(cpp, t) - case is_keyword != nil && is_keyword(t): - t.kind = .Keyword - case t.kind == .PP_Number: - convert_pp_number(t) - } -} -convert_pp_tokens :: proc(cpp: ^Preprocessor, tok: ^Token, is_keyword: tokenizer.Is_Keyword_Proc) { - for t := tok; t != nil && t.kind != .EOF; t = t.next { - convert_pp_token(cpp, tok, is_keyword) - } -} - -join_adjacent_string_literals :: proc(cpp: ^Preprocessor, initial_tok: ^Token) { - for tok1 := initial_tok; tok1.kind != .EOF; /**/ { - if tok1.kind != .String || tok1.next.kind != .String { - tok1 = tok1.next - continue - } - - type_hint := tokenizer.Token_Type_Hint.None - char_size := 1 - - start := tok1 - for t := tok1; t != nil && t.kind == .String; t = t.next { - if t.val == nil { - convert_pp_string(cpp, t) - } - tok1 = t.next - if type_hint != t.type_hint { - if t.type_hint != .None && type_hint != .None { - error(cpp, t, "unsupported non-standard concatenation of string literals of different types") - } - prev_char_size := char_size - - #partial switch type_hint { - case .UTF_8: char_size = max(char_size, 1) - case .UTF_16: char_size = max(char_size, 2) - case .UTF_32: char_size = max(char_size, 4) - case .UTF_Wide: char_size = max(char_size, wide_char_size(cpp)) - } - - if type_hint == .None || prev_char_size < char_size { - type_hint = t.type_hint - } - } - } - - // NOTE(bill): Verbose logic in order to correctly concantenate strings, even if they different in type - max_len := 0 - switch char_size { - case 1: - for t := start; t != nil && t.kind == .String; t = t.next { - #partial switch v in t.val { - case string: max_len += len(v) - case []u16: max_len += 2*len(v) - case []u32: max_len += 4*len(v) - } - } - n := 0 - buf := make([]byte, max_len) - for t := start; t != nil && t.kind == .String; t = t.next { - #partial switch v in t.val { - case string: - n += copy(buf[n:], v) - case []u16: - for i := 0; i < len(v); /**/ { - c1 := v[i] - r: rune - if !utf16.is_surrogate(rune(c1)) { - r = rune(c1) - i += 1 - } else if i+1 == len(v) { - r = utf16.REPLACEMENT_CHAR - i += 1 - } else { - c2 := v[i+1] - i += 2 - r = utf16.decode_surrogate_pair(rune(c1), rune(c2)) - } - - b, w := utf8.encode_rune(r) - n += copy(buf[n:], b[:w]) - } - case []u32: - for r in v { - b, w := utf8.encode_rune(rune(r)) - n += copy(buf[n:], b[:w]) - } - } - } - - new_tok := tokenizer.copy_token(start) - new_tok.lit = "" - new_tok.val = string(buf[:n]) - new_tok.next = tok1 - new_tok.type_hint = type_hint - start^ = new_tok^ - case 2: - for t := start; t != nil && t.kind == .String; t = t.next { - #partial switch v in t.val { - case string: max_len += len(v) - case []u16: max_len += len(v) - case []u32: max_len += 2*len(v) - } - } - n := 0 - buf := make([]u16, max_len) - for t := start; t != nil && t.kind == .String; t = t.next { - #partial switch v in t.val { - case string: - for r in v { - if r >= 0x10000 { - c1, c2 := utf16.encode_surrogate_pair(r) - buf[n+0] = u16(c1) - buf[n+1] = u16(c2) - n += 2 - } else { - buf[n] = u16(r) - n += 1 - } - } - case []u16: - n += copy(buf[n:], v) - case []u32: - for r in v { - if r >= 0x10000 { - c1, c2 := utf16.encode_surrogate_pair(rune(r)) - buf[n+0] = u16(c1) - buf[n+1] = u16(c2) - n += 2 - } else { - buf[n] = u16(r) - n += 1 - } - } - } - } - - new_tok := tokenizer.copy_token(start) - new_tok.lit = "" - new_tok.val = buf[:n] - new_tok.next = tok1 - new_tok.type_hint = type_hint - start^ = new_tok^ - case 4: - for t := start; t != nil && t.kind == .String; t = t.next { - #partial switch v in t.val { - case string: max_len += len(v) - case []u16: max_len += len(v) - case []u32: max_len += len(v) - } - } - n := 0 - buf := make([]u32, max_len) - for t := start; t != nil && t.kind == .String; t = t.next { - #partial switch v in t.val { - case string: - for r in v { - buf[n] = u32(r) - n += 1 - } - case []u16: - for i := 0; i < len(v); /**/ { - c1 := v[i] - if !utf16.is_surrogate(rune(c1)) { - buf[n] = u32(c1) - n += 1 - i += 1 - } else if i+1 == len(v) { - buf[n] = utf16.REPLACEMENT_CHAR - n += 1 - i += 1 - } else { - c2 := v[i+1] - i += 2 - r := utf16.decode_surrogate_pair(rune(c1), rune(c2)) - buf[n] = u32(r) - n += 1 - } - } - case []u32: - n += copy(buf[n:], v) - } - } - - new_tok := tokenizer.copy_token(start) - new_tok.lit = "" - new_tok.val = buf[:n] - new_tok.next = tok1 - new_tok.type_hint = type_hint - start^ = new_tok^ - } - } -} - - -quote_string :: proc(s: string) -> []byte { - b := strings.builder_make(0, len(s)+2) - io.write_quoted_string(strings.to_writer(&b), s, '"') - return b.buf[:] -} - - -_init_tokenizer_from_preprocessor :: proc(t: ^Tokenizer, cpp: ^Preprocessor) -> ^Tokenizer { - t.warn = cpp.warn - t.err = cpp.err - return t -} - -new_string_token :: proc(cpp: ^Preprocessor, str: string, tok: ^Token) -> ^Token { - assert(tok != nil) - assert(str != "") - t := _init_tokenizer_from_preprocessor(&Tokenizer{}, cpp) - src := quote_string(str) - return tokenizer.inline_tokenize(t, tok, src) -} - -stringize :: proc(cpp: ^Preprocessor, hash, arg: ^Token) -> ^Token { - s := join_tokens(arg, nil) - return new_string_token(cpp, s, hash) -} - - -new_number_token :: proc(cpp: ^Preprocessor, i: i64, tok: ^Token) -> ^Token { - t := _init_tokenizer_from_preprocessor(&Tokenizer{}, cpp) - buf: [32]byte - n := len(strconv.append_int(buf[:], i, 10)) - src := make([]byte, n) - copy(src, buf[:n]) - return tokenizer.inline_tokenize(t, tok, src) -} - - -find_macro :: proc(cpp: ^Preprocessor, tok: ^Token) -> ^Macro { - if tok.kind != .Ident { - return nil - } - return cpp.macros[tok.lit] -} - -add_macro :: proc(cpp: ^Preprocessor, name: string, kind: Macro_Kind, body: ^Token) -> ^Macro { - m := new(Macro) - m.name = name - m.kind = kind - m.body = body - cpp.macros[name] = m - return m -} - - -undef_macro :: proc(cpp: ^Preprocessor, name: string) { - delete_key(&cpp.macros, name) -} - -add_builtin :: proc(cpp: ^Preprocessor, name: string, handler: Macro_Handler) -> ^Macro { - m := add_macro(cpp, name, .Value_Like, nil) - m.handler = handler - return m -} - - -skip :: proc(cpp: ^Preprocessor, tok: ^Token, op: string) -> ^Token { - if tok.lit != op { - error(cpp, tok, "expected '%q'", op) - } - return tok.next -} - -consume :: proc(rest: ^^Token, tok: ^Token, lit: string) -> bool { - if tok.lit == lit { - rest^ = tok.next - return true - } - rest^ = tok - return false -} - -read_macro_params :: proc(cpp: ^Preprocessor, rest: ^^Token, tok: ^Token) -> (param: ^Macro_Param, va_args_name: string) { - head: Macro_Param - curr := &head - - tok := tok - for tok.lit != ")" && tok.kind != .EOF { - if curr != &head { - tok = skip(cpp, tok, ",") - } - - if tok.lit == "..." { - va_args_name = "__VA_ARGS__" - rest^ = skip(cpp, tok.next, ")") - param = head.next - return - } - - if tok.kind != .Ident { - error(cpp, tok, "expected an identifier") - } - - if tok.next.lit == "..." { - va_args_name = tok.lit - rest^ = skip(cpp, tok.next.next, ")") - param = head.next - return - } - - m := new(Macro_Param) - m.name = tok.lit - curr.next = m - curr = curr.next - tok = tok.next - } - - - rest^ = tok.next - param = head.next - return -} - -copy_line :: proc(rest: ^^Token, tok: ^Token) -> ^Token { - head: Token - curr := &head - - tok := tok - for ; !tok.at_bol; tok = tok.next { - curr.next = tokenizer.copy_token(tok) - curr = curr.next - } - curr.next = tokenizer.new_eof(tok) - rest^ = tok - return head.next -} - -read_macro_definition :: proc(cpp: ^Preprocessor, rest: ^^Token, tok: ^Token) { - tok := tok - if tok.kind != .Ident { - error(cpp, tok, "macro name must be an identifier") - } - name := tok.lit - tok = tok.next - - if !tok.has_space && tok.lit == "(" { - params, va_args_name := read_macro_params(cpp, &tok, tok.next) - - m := add_macro(cpp, name, .Function_Like, copy_line(rest, tok)) - m.params = params - m.va_args_name = va_args_name - } else { - add_macro(cpp, name, .Value_Like, copy_line(rest, tok)) - } -} - - -join_tokens :: proc(tok, end: ^Token) -> string { - n := 1 - for t := tok; t != end && t.kind != .EOF; t = t.next { - if t != tok && t.has_space { - n += 1 - } - n += len(t.lit) - } - - buf := make([]byte, n) - - pos := 0 - for t := tok; t != end && t.kind != .EOF; t = t.next { - if t != tok && t.has_space { - buf[pos] = ' ' - pos += 1 - } - copy(buf[pos:], t.lit) - pos += len(t.lit) - } - - return string(buf[:pos]) -} - -read_include_filename :: proc(cpp: ^Preprocessor, rest: ^^Token, tok: ^Token) -> (filename: string, is_quote: bool) { - tok := tok - - if tok.kind == .String { - rest^ = skip_line(cpp, tok.next) - filename = tok.lit[1:len(tok.lit)-1] - is_quote = true - return - } - - if tok.lit == "<" { - start := tok - for ; tok.kind != .EOF; tok = tok.next { - if tok.at_bol || tok.kind == .EOF { - error(cpp, tok, "expected '>'") - } - is_quote = false - if tok.lit == ">" { - break - } - } - rest^ = skip_line(cpp, tok.next) - filename = join_tokens(start.next, tok) - return - } - - if tok.kind == .Ident { - tok2 := preprocess_internal(cpp, copy_line(rest, tok)) - return read_include_filename(cpp, &tok2, tok2) - } - - error(cpp, tok, "expected a filename") - return -} - -skip_cond_incl :: proc(tok: ^Token) -> ^Token { - next_skip :: proc(tok: ^Token) -> ^Token { - tok := tok - for tok.kind != .EOF { - if is_hash(tok) { - switch tok.next.lit { - case "if", "ifdef", "ifndef": - tok = next_skip(tok.next.next) - continue - - case "endif": - return tok.next.next - } - } - tok = tok.next - } - return tok - } - - tok := tok - - loop: for tok.kind != .EOF { - if is_hash(tok) { - switch tok.next.lit { - case "if", "ifdef", "ifndef": - tok = next_skip(tok.next.next) - continue loop - - case "elif", "else", "endif": - break loop - } - } - - tok = tok.next - } - return tok -} - -check_for_include_guard :: proc(tok: ^Token) -> (guard: string, ok: bool) { - if !is_hash(tok) || tok.next.lit != "ifndef" { - return - } - tok := tok - tok = tok.next.next - - if tok.kind != .Ident { - return - } - - m := tok.lit - tok = tok.next - - if !is_hash(tok) || tok.next.lit != "define" || tok.next.lit != "macro" { - return - } - - for tok.kind != .EOF { - if !is_hash(tok) { - tok = tok.next - continue - } - - if tok.next.lit == "endif" && tok.next.next.kind == .EOF { - return m, true - } - - switch tok.lit { - case "if", "ifdef", "ifndef": - tok = skip_cond_incl(tok.next) - case: - tok = tok.next - } - } - return -} - -include_file :: proc(cpp: ^Preprocessor, tok: ^Token, path: string, filename_tok: ^Token) -> ^Token { - if cpp.pragma_once[path] { - return tok - } - - guard_name, guard_name_found := cpp.include_guards[path] - if guard_name_found && cpp.macros[guard_name] != nil { - return tok - } - - if !os.exists(path) { - error(cpp, filename_tok, "%s: cannot open file", path) - return tok - } - - cpp.include_level += 1 - if cpp.include_level > MAX_INCLUDE_LEVEL { - error(cpp, tok, "exceeded maximum nest amount: %d", MAX_INCLUDE_LEVEL) - return tok - } - - t := _init_tokenizer_from_preprocessor(&Tokenizer{}, cpp) - tok2 := tokenizer.tokenize_file(t, path, /*file.id*/1) - if tok2 == nil { - error(cpp, filename_tok, "%s: cannot open file", path) - } - cpp.include_level -= 1 - - guard_name, guard_name_found = check_for_include_guard(tok2) - if guard_name_found { - cpp.include_guards[path] = guard_name - } - - return append_token(tok2, tok) -} - -find_arg :: proc(args: ^Macro_Arg, tok: ^Token) -> ^Macro_Arg { - for ap := args; ap != nil; ap = ap.next { - if tok.lit == ap.name { - return ap - } - } - return nil -} - -paste :: proc(cpp: ^Preprocessor, lhs, rhs: ^Token) -> ^Token { - buf := strings.concatenate({lhs.lit, rhs.lit}) - t := _init_tokenizer_from_preprocessor(&Tokenizer{}, cpp) - tok := tokenizer.inline_tokenize(t, lhs, transmute([]byte)buf) - if tok.next.kind != .EOF { - error(cpp, lhs, "pasting forms '%s', an invalid token", buf) - } - return tok -} - -has_varargs :: proc(args: ^Macro_Arg) -> bool { - for ap := args; ap != nil; ap = ap.next { - if ap.name == "__VA_ARGS__" { - return ap.tok.kind != .EOF - } - } - return false -} - -substitute_token :: proc(cpp: ^Preprocessor, tok: ^Token, args: ^Macro_Arg) -> ^Token { - head: Token - curr := &head - tok := tok - for tok.kind != .EOF { - if tok.lit == "#" { - arg := find_arg(args, tok.next) - if arg == nil { - error(cpp, tok.next, "'#' is not followed by a macro parameter") - } - arg_tok := arg.tok if arg != nil else tok.next - curr.next = stringize(cpp, tok, arg_tok) - curr = curr.next - tok = tok.next.next - continue - } - - if tok.lit == "," && tok.next.lit == "##" { - if arg := find_arg(args, tok.next.next); arg != nil && arg.is_va_args { - if arg.tok.kind == .EOF { - tok = tok.next.next.next - } else { - curr.next = tokenizer.copy_token(tok) - curr = curr.next - tok = tok.next.next - } - continue - } - } - - if tok.lit == "##" { - if curr == &head { - error(cpp, tok, "'##' cannot appear at start of macro expansion") - } - if tok.next.kind == .EOF { - error(cpp, tok, "'##' cannot appear at end of macro expansion") - } - - if arg := find_arg(args, tok.next); arg != nil { - if arg.tok.kind != .EOF { - curr^ = paste(cpp, curr, arg.tok)^ - for t := arg.tok.next; t.kind != .EOF; t = t.next { - curr.next = tokenizer.copy_token(t) - curr = curr.next - } - } - tok = tok.next.next - continue - } - - curr^ = paste(cpp, curr, tok.next)^ - tok = tok.next.next - continue - } - - arg := find_arg(args, tok) - - if arg != nil && tok.next.lit == "##" { - rhs := tok.next.next - - if arg.tok.kind == .EOF { - args2 := find_arg(args, rhs) - if args2 != nil { - for t := args.tok; t.kind != .EOF; t = t.next { - curr.next = tokenizer.copy_token(t) - curr = curr.next - } - } else { - curr.next = tokenizer.copy_token(rhs) - curr = curr.next - } - tok = rhs.next - continue - } - - for t := arg.tok; t.kind != .EOF; t = t.next { - curr.next = tokenizer.copy_token(t) - curr = curr.next - } - tok = tok.next - continue - } - - if tok.lit == "__VA_OPT__" && tok.next.lit == "(" { - opt_arg := read_macro_arg_one(cpp, &tok, tok.next.next, true) - if has_varargs(args) { - for t := opt_arg.tok; t.kind != .EOF; t = t.next { - curr.next = t - curr = curr.next - } - } - tok = skip(cpp, tok, ")") - continue - } - - if arg != nil { - t := preprocess_internal(cpp, arg.tok) - t.at_bol = tok.at_bol - t.has_space = tok.has_space - for ; t.kind != .EOF; t = t.next { - curr.next = tokenizer.copy_token(t) - curr = curr.next - } - tok = tok.next - continue - } - - curr.next = tokenizer.copy_token(tok) - curr = curr.next - tok = tok.next - continue - } - - curr.next = tok - return head.next -} - -read_macro_arg_one :: proc(cpp: ^Preprocessor, rest: ^^Token, tok: ^Token, read_rest: bool) -> ^Macro_Arg { - tok := tok - head: Token - curr := &head - level := 0 - for { - if level == 0 && tok.lit == ")" { - break - } - if level == 0 && !read_rest && tok.lit == "," { - break - } - - if tok.kind == .EOF { - error(cpp, tok, "premature end of input") - } - - switch tok.lit { - case "(": level += 1 - case ")": level -= 1 - } - - curr.next = tokenizer.copy_token(tok) - curr = curr.next - tok = tok.next - } - curr.next = tokenizer.new_eof(tok) - - arg := new(Macro_Arg) - arg.tok = head.next - rest^ = tok - return arg -} - -read_macro_args :: proc(cpp: ^Preprocessor, rest: ^^Token, tok: ^Token, params: ^Macro_Param, va_args_name: string) -> ^Macro_Arg { - tok := tok - start := tok - tok = tok.next.next - - head: Macro_Arg - curr := &head - - pp := params - for ; pp != nil; pp = pp.next { - if curr != &head { - tok = skip(cpp, tok, ",") - } - curr.next = read_macro_arg_one(cpp, &tok, tok, false) - curr = curr.next - curr.name = pp.name - } - - if va_args_name != "" { - arg: ^Macro_Arg - if tok.lit == ")" { - arg = new(Macro_Arg) - arg.tok = tokenizer.new_eof(tok) - } else { - if pp != params { - tok = skip(cpp, tok, ",") - } - arg = read_macro_arg_one(cpp, &tok, tok, true) - } - arg.name = va_args_name - arg.is_va_args = true - curr.next = arg - curr = curr.next - } else if pp != nil { - error(cpp, start, "too many arguments") - } - - skip(cpp, tok, ")") - rest^ = tok - return head.next -} - -expand_macro :: proc(cpp: ^Preprocessor, rest: ^^Token, tok: ^Token) -> bool { - if tokenizer.hide_set_contains(tok.hide_set, tok.lit) { - return false - } - tok := tok - m := find_macro(cpp, tok) - if m == nil { - return false - } - - if m.handler != nil { - rest^ = m.handler(cpp, tok) - rest^.next = tok.next - return true - } - - if m.kind == .Value_Like { - hs := tokenizer.hide_set_union(tok.hide_set, tokenizer.new_hide_set(m.name)) - body := tokenizer.add_hide_set(m.body, hs) - for t := body; t.kind != .EOF; t = t.next { - t.origin = tok - } - rest^ = append_token(body, tok.next) - rest^.at_bol = tok.at_bol - rest^.has_space = tok.has_space - return true - } - - if tok.next.lit != "(" { - return false - } - - macro_token := tok - args := read_macro_args(cpp, &tok, tok, m.params, m.va_args_name) - close_paren := tok - - hs := tokenizer.hide_set_intersection(macro_token.hide_set, close_paren.hide_set) - hs = tokenizer.hide_set_union(hs, tokenizer.new_hide_set(m.name)) - - body := substitute_token(cpp, m.body, args) - body = tokenizer.add_hide_set(body, hs) - for t := body; t.kind != .EOF; t = t.next { - t.origin = macro_token - } - rest^ = append_token(body, tok.next) - rest^.at_bol = macro_token.at_bol - rest^.has_space = macro_token.has_space - return true -} - -search_include_next :: proc(cpp: ^Preprocessor, filename: string) -> (path: string, ok: bool) { - for ; cpp.include_next_index < len(cpp.include_paths); cpp.include_next_index += 1 { - tpath := filepath.join({cpp.include_paths[cpp.include_next_index], filename}, allocator=context.temp_allocator) - if os.exists(tpath) { - return strings.clone(tpath), true - } - } - return -} - -search_include_paths :: proc(cpp: ^Preprocessor, filename: string) -> (path: string, ok: bool) { - if filepath.is_abs(filename) { - return filename, true - } - - if path, ok = cpp.filepath_cache[filename]; ok { - return - } - - for include_path in cpp.include_paths { - tpath := filepath.join({include_path, filename}, allocator=context.temp_allocator) - if os.exists(tpath) { - path, ok = strings.clone(tpath), true - cpp.filepath_cache[filename] = path - return - } - } - - return -} - -read_const_expr :: proc(cpp: ^Preprocessor, rest: ^^Token, tok: ^Token) -> ^Token { - tok := tok - tok = copy_line(rest, tok) - head: Token - curr := &head - for tok.kind != .EOF { - if tok.lit == "defined" { - start := tok - has_paren := consume(&tok, tok.next, "(") - if tok.kind != .Ident { - error(cpp, start, "macro name must be an identifier") - } - m := find_macro(cpp, tok) - tok = tok.next - - if has_paren { - tok = skip(cpp, tok, ")") - } - - curr.next = new_number_token(cpp, 1 if m != nil else 0, start) - curr = curr.next - continue - } - - curr.next = tok - curr = curr.next - tok = tok.next - } - - curr.next = tok - return head.next -} - -eval_const_expr :: proc(cpp: ^Preprocessor, rest: ^^Token, tok: ^Token) -> (val: i64) { - tok := tok - start := tok - expr := read_const_expr(cpp, rest, tok.next) - expr = preprocess_internal(cpp, expr) - - if expr.kind == .EOF { - error(cpp, start, "no expression") - } - - for t := expr; t.kind != .EOF; t = t.next { - if t.kind == .Ident { - next := t.next - t^ = new_number_token(cpp, 0, t)^ - t.next = next - } - } - - val = 1 - convert_pp_tokens(cpp, expr, tokenizer.default_is_keyword) - - rest2: ^Token - val = const_expr(&rest2, expr) - if rest2 != nil && rest2.kind != .EOF { - error(cpp, rest2, "extra token") - } - return -} - -push_cond_incl :: proc(cpp: ^Preprocessor, tok: ^Token, included: bool) -> ^Cond_Incl { - ci := new(Cond_Incl) - ci.next = cpp.cond_incl - ci.state = .In_Then - ci.tok = tok - ci.included = included - cpp.cond_incl = ci - return ci -} - -read_line_marker:: proc(cpp: ^Preprocessor, rest: ^^Token, tok: ^Token) { - tok := tok - start := tok - tok = preprocess(cpp, copy_line(rest, tok)) - if tok.kind != .Number { - error(cpp, tok, "invalid line marker") - } - ival, _ := tok.val.(i64) - start.file.line_delta = int(ival - i64(start.pos.line)) - tok = tok.next - if tok.kind == .EOF { - return - } - - if tok.kind != .String { - error(cpp, tok, "filename expected") - } - start.file.display_name = tok.lit -} - -preprocess_internal :: proc(cpp: ^Preprocessor, tok: ^Token) -> ^Token { - head: Token - curr := &head - - tok := tok - for tok != nil && tok.kind != .EOF { - if expand_macro(cpp, &tok, tok) { - continue - } - - if !is_hash(tok) { - if tok.file != nil { - tok.line_delta = tok.file.line_delta - } - curr.next = tok - curr = curr.next - tok = tok.next - continue - } - - start := tok - tok = tok.next - - switch tok.lit { - case "include": - filename, is_quote := read_include_filename(cpp, &tok, tok.next) - is_absolute := filepath.is_abs(filename) - if is_absolute { - tok = include_file(cpp, tok, filename, start.next.next) - continue - } - - if is_quote { - dir := "" - if start.file != nil { - dir = filepath.dir(start.file.name) - } - path := filepath.join({dir, filename}) - if os.exists(path) { - tok = include_file(cpp, tok, path, start.next.next) - continue - } - } - - path, ok := search_include_paths(cpp, filename) - if !ok { - path = filename - } - tok = include_file(cpp, tok, path, start.next.next) - continue - - case "include_next": - filename, _ := read_include_filename(cpp, &tok, tok.next) - path, ok := search_include_next(cpp, filename) - if !ok { - path = filename - } - tok = include_file(cpp, tok, path, start.next.next) - continue - - case "define": - read_macro_definition(cpp, &tok, tok.next) - continue - - case "undef": - tok = tok.next - if tok.kind != .Ident { - error(cpp, tok, "macro name must be an identifier") - } - undef_macro(cpp, tok.lit) - tok = skip_line(cpp, tok.next) - continue - - case "if": - val := eval_const_expr(cpp, &tok, tok) - push_cond_incl(cpp, start, val != 0) - if val == 0 { - tok = skip_cond_incl(tok) - } - continue - - case "ifdef": - defined := find_macro(cpp, tok.next) - push_cond_incl(cpp, tok, defined != nil) - tok = skip_line(cpp, tok.next.next) - if defined == nil { - tok = skip_cond_incl(tok) - } - continue - - case "ifndef": - defined := find_macro(cpp, tok.next) - push_cond_incl(cpp, tok, defined != nil) - tok = skip_line(cpp, tok.next.next) - if !(defined == nil) { - tok = skip_cond_incl(tok) - } - continue - - case "elif": - if cpp.cond_incl == nil || cpp.cond_incl.state == .In_Else { - error(cpp, start, "stray #elif") - } - if cpp.cond_incl != nil { - cpp.cond_incl.state = .In_Elif - } - - if (cpp.cond_incl != nil && !cpp.cond_incl.included) && eval_const_expr(cpp, &tok, tok) != 0 { - cpp.cond_incl.included = true - } else { - tok = skip_cond_incl(tok) - } - continue - - case "else": - if cpp.cond_incl == nil || cpp.cond_incl.state == .In_Else { - error(cpp, start, "stray #else") - } - if cpp.cond_incl != nil { - cpp.cond_incl.state = .In_Else - } - tok = skip_line(cpp, tok.next) - - if cpp.cond_incl != nil { - tok = skip_cond_incl(tok) - } - continue - - case "endif": - if cpp.cond_incl == nil { - error(cpp, start, "stray #endif") - } else { - cpp.cond_incl = cpp.cond_incl.next - } - tok = skip_line(cpp, tok.next) - continue - - case "line": - read_line_marker(cpp, &tok, tok.next) - continue - - case "pragma": - if tok.next.lit == "once" { - cpp.pragma_once[tok.pos.file] = true - tok = skip_line(cpp, tok.next.next) - continue - } - - pragma_tok, pragma_end := tok, tok - - for tok != nil && tok.kind != .EOF { - pragma_end = tok - tok = tok.next - if tok.at_bol { - break - } - } - pragma_end.next = tokenizer.new_eof(tok) - if cpp.pragma_handler != nil { - cpp.pragma_handler(cpp, pragma_tok.next) - continue - } - - continue - - case "error": - error(cpp, tok, "error") - } - - if tok.kind == .PP_Number { - read_line_marker(cpp, &tok, tok) - continue - } - - if !tok.at_bol { - error(cpp, tok, "invalid preprocessor directive") - } - } - - curr.next = tok - return head.next -} - - -preprocess :: proc(cpp: ^Preprocessor, tok: ^Token) -> ^Token { - tok := tok - tok = preprocess_internal(cpp, tok) - if cpp.cond_incl != nil { - error(cpp, tok, "unterminated conditional directive") - } - convert_pp_tokens(cpp, tok, tokenizer.default_is_keyword) - join_adjacent_string_literals(cpp, tok) - for t := tok; t != nil; t = t.next { - t.pos.line += t.line_delta - } - return tok -} - - -define_macro :: proc(cpp: ^Preprocessor, name, def: string) { - src := transmute([]byte)def - - file := new(tokenizer.File) - file.id = -1 - file.src = src - file.name = "" - file.display_name = file.name - - - t := _init_tokenizer_from_preprocessor(&Tokenizer{}, cpp) - tok := tokenizer.tokenize(t, file) - add_macro(cpp, name, .Value_Like, tok) -} - - -file_macro :: proc(cpp: ^Preprocessor, tok: ^Token) -> ^Token { - tok := tok - for tok.origin != nil { - tok = tok.origin - } - i := i64(tok.pos.line + tok.file.line_delta) - return new_number_token(cpp, i, tok) -} -line_macro :: proc(cpp: ^Preprocessor, tok: ^Token) -> ^Token { - tok := tok - for tok.origin != nil { - tok = tok.origin - } - return new_string_token(cpp, tok.file.display_name, tok) -} -counter_macro :: proc(cpp: ^Preprocessor, tok: ^Token) -> ^Token { - i := cpp.counter - cpp.counter += 1 - return new_number_token(cpp, i, tok) -} - -init_default_macros :: proc(cpp: ^Preprocessor) { - define_macro(cpp, "__C99_MACRO_WITH_VA_ARGS", "1") - define_macro(cpp, "__alignof__", "_Alignof") - define_macro(cpp, "__const__", "const") - define_macro(cpp, "__inline__", "inline") - define_macro(cpp, "__signed__", "signed") - define_macro(cpp, "__typeof__", "typeof") - define_macro(cpp, "__volatile__", "volatile") - - add_builtin(cpp, "__FILE__", file_macro) - add_builtin(cpp, "__LINE__", line_macro) - add_builtin(cpp, "__COUNTER__", counter_macro) -} - -init_lookup_tables :: proc(cpp: ^Preprocessor, allocator := context.allocator) { - context.allocator = allocator - reserve(&cpp.macros, max(16, cap(cpp.macros))) - reserve(&cpp.pragma_once, max(16, cap(cpp.pragma_once))) - reserve(&cpp.include_guards, max(16, cap(cpp.include_guards))) - reserve(&cpp.filepath_cache, max(16, cap(cpp.filepath_cache))) -} - - -init_defaults :: proc(cpp: ^Preprocessor, lookup_tables_allocator := context.allocator) { - if cpp.warn == nil { - cpp.warn = tokenizer.default_warn_handler - } - if cpp.err == nil { - cpp.err = tokenizer.default_error_handler - } - init_lookup_tables(cpp, lookup_tables_allocator) - init_default_macros(cpp) -} diff --git a/core/c/frontend/preprocessor/unquote.odin b/core/c/frontend/preprocessor/unquote.odin deleted file mode 100644 index 5869fa7ef..000000000 --- a/core/c/frontend/preprocessor/unquote.odin +++ /dev/null @@ -1,154 +0,0 @@ -package c_frontend_preprocess - -import "core:unicode/utf8" - -unquote_char :: proc(str: string, quote: byte) -> (r: rune, multiple_bytes: bool, tail_string: string, success: bool) { - hex_to_int :: proc(c: byte) -> int { - switch c { - case '0'..='9': return int(c-'0') - case 'a'..='f': return int(c-'a')+10 - case 'A'..='F': return int(c-'A')+10 - } - return -1 - } - w: int - - if str[0] == quote && quote == '"' { - return - } else if str[0] >= 0x80 { - r, w = utf8.decode_rune_in_string(str) - return r, true, str[w:], true - } else if str[0] != '\\' { - return rune(str[0]), false, str[1:], true - } - - if len(str) <= 1 { - return - } - s := str - c := s[1] - s = s[2:] - - switch c { - case: r = rune(c) - - case 'a': r = '\a' - case 'b': r = '\b' - case 'e': r = '\e' - case 'f': r = '\f' - case 'n': r = '\n' - case 'r': r = '\r' - case 't': r = '\t' - case 'v': r = '\v' - case '\\': r = '\\' - - case '"': r = '"' - case '\'': r = '\'' - - case '0'..='7': - v := int(c-'0') - if len(s) < 2 { - return - } - for i in 0.. 7 { - return - } - v = (v<<3) | d - } - s = s[2:] - if v > 0xff { - return - } - r = rune(v) - - case 'x', 'u', 'U': - count: int - switch c { - case 'x': count = 2 - case 'u': count = 4 - case 'U': count = 8 - } - - if len(s) < count { - return - } - - for i in 0.. utf8.MAX_RUNE { - return - } - multiple_bytes = true - } - - success = true - tail_string = s - return -} - -unquote_string :: proc(lit: string, allocator := context.allocator) -> (res: string, allocated, success: bool) { - contains_rune :: proc(s: string, r: rune) -> int { - for c, offset in s { - if c == r { - return offset - } - } - return -1 - } - - assert(len(lit) >= 2) - - s := lit - quote := '"' - - if s == `""` { - return "", false, true - } - - if contains_rune(s, '\n') >= 0 { - return s, false, false - } - - if contains_rune(s, '\\') < 0 && contains_rune(s, quote) < 0 { - if quote == '"' { - return s, false, true - } - } - s = s[1:len(s)-1] - - - buf_len := 3*len(s) / 2 - buf := make([]byte, buf_len, allocator) - offset := 0 - for len(s) > 0 { - r, multiple_bytes, tail_string, ok := unquote_char(s, byte(quote)) - if !ok { - delete(buf) - return s, false, false - } - s = tail_string - if r < 0x80 || !multiple_bytes { - buf[offset] = byte(r) - offset += 1 - } else { - b, w := utf8.encode_rune(r) - copy(buf[offset:], b[:w]) - offset += w - } - } - - new_string := string(buf[:offset]) - - return new_string, true, true -} diff --git a/core/c/frontend/tokenizer/doc.odin b/core/c/frontend/tokenizer/doc.odin deleted file mode 100644 index 43747dfe8..000000000 --- a/core/c/frontend/tokenizer/doc.odin +++ /dev/null @@ -1,31 +0,0 @@ -/* -Example: - package demo - - import tokenizer "core:c/frontend/tokenizer" - import preprocessor "core:c/frontend/preprocessor" - import "core:fmt" - - main :: proc() { - t := &tokenizer.Tokenizer{}; - tokenizer.init_defaults(t); - - cpp := &preprocessor.Preprocessor{}; - cpp.warn, cpp.err = t.warn, t.err; - preprocessor.init_lookup_tables(cpp); - preprocessor.init_default_macros(cpp); - cpp.include_paths = {"my/path/to/include"}; - - tok := tokenizer.tokenize_file(t, "the/source/file.c", 1); - - tok = preprocessor.preprocess(cpp, tok); - if tok != nil { - for t := tok; t.kind != .EOF; t = t.next { - fmt.println(t.lit); - } - } - - fmt.println("[Done]"); - } -*/ -package c_frontend_tokenizer diff --git a/core/c/frontend/tokenizer/hide_set.odin b/core/c/frontend/tokenizer/hide_set.odin deleted file mode 100644 index ec8b77e6e..000000000 --- a/core/c/frontend/tokenizer/hide_set.odin +++ /dev/null @@ -1,68 +0,0 @@ -package c_frontend_tokenizer - -// NOTE(bill): This is a really dumb approach for a hide set, -// but it's really simple and probably fast enough in practice - - -Hide_Set :: struct { - next: ^Hide_Set, - name: string, -} - - -new_hide_set :: proc(name: string) -> ^Hide_Set { - hs := new(Hide_Set) - hs.name = name - return hs -} - -hide_set_contains :: proc(hs: ^Hide_Set, name: string) -> bool { - for h := hs; h != nil; h = h.next { - if h.name == name { - return true - } - } - return false -} - - -hide_set_union :: proc(a, b: ^Hide_Set) -> ^Hide_Set { - head: Hide_Set - curr := &head - - for h := a; h != nil; h = h.next { - curr.next = new_hide_set(h.name) - curr = curr.next - } - curr.next = b - return head.next -} - - -hide_set_intersection :: proc(a, b: ^Hide_Set) -> ^Hide_Set { - head: Hide_Set - curr := &head - - for h := a; h != nil; h = h.next { - if hide_set_contains(b, h.name) { - curr.next = new_hide_set(h.name) - curr = curr.next - } - } - return head.next -} - - -add_hide_set :: proc(tok: ^Token, hs: ^Hide_Set) -> ^Token { - head: Token - curr := &head - - tok := tok - for ; tok != nil; tok = tok.next { - t := copy_token(tok) - t.hide_set = hide_set_union(t.hide_set, hs) - curr.next = t - curr = curr.next - } - return head.next -} diff --git a/core/c/frontend/tokenizer/token.odin b/core/c/frontend/tokenizer/token.odin deleted file mode 100644 index 1376a651f..000000000 --- a/core/c/frontend/tokenizer/token.odin +++ /dev/null @@ -1,169 +0,0 @@ -package c_frontend_tokenizer - - -Pos :: struct { - file: string, - line: int, - column: int, - offset: int, -} - -Token_Kind :: enum { - Invalid, - Ident, - Punct, - Keyword, - Char, - String, - Number, - PP_Number, - Comment, - EOF, -} - -File :: struct { - name: string, - id: int, - src: []byte, - - display_name: string, - line_delta: int, -} - - -Token_Type_Hint :: enum u8 { - None, - - Int, - Long, - Long_Long, - - Unsigned_Int, - Unsigned_Long, - Unsigned_Long_Long, - - Float, - Double, - Long_Double, - - UTF_8, - UTF_16, - UTF_32, - UTF_Wide, -} - -Token_Value :: union { - i64, - f64, - string, - []u16, - []u32, -} - -Token :: struct { - kind: Token_Kind, - next: ^Token, - lit: string, - - pos: Pos, - file: ^File, - line_delta: int, - at_bol: bool, - has_space: bool, - - type_hint: Token_Type_Hint, - val: Token_Value, - prefix: string, - - // Preprocessor values - hide_set: ^Hide_Set, - origin: ^Token, -} - -Is_Keyword_Proc :: #type proc(tok: ^Token) -> bool - -copy_token :: proc(tok: ^Token) -> ^Token { - t, _ := new_clone(tok^) - t.next = nil - return t -} - -new_eof :: proc(tok: ^Token) -> ^Token { - t, _ := new_clone(tok^) - t.kind = .EOF - t.lit = "" - return t -} - -default_is_keyword :: proc(tok: ^Token) -> bool { - if tok.kind == .Keyword { - return true - } - if len(tok.lit) > 0 { - return default_keyword_set[tok.lit] - } - return false -} - - -token_name := [Token_Kind]string { - .Invalid = "invalid", - .Ident = "ident", - .Punct = "punct", - .Keyword = "keyword", - .Char = "char", - .String = "string", - .Number = "number", - .PP_Number = "preprocessor number", - .Comment = "comment", - .EOF = "eof", -} - -default_keyword_set := map[string]bool{ - "auto" = true, - "break" = true, - "case" = true, - "char" = true, - "const" = true, - "continue" = true, - "default" = true, - "do" = true, - "double" = true, - "else" = true, - "enum" = true, - "extern" = true, - "float" = true, - "for" = true, - "goto" = true, - "if" = true, - "int" = true, - "long" = true, - "register" = true, - "restrict" = true, - "return" = true, - "short" = true, - "signed" = true, - "sizeof" = true, - "static" = true, - "struct" = true, - "switch" = true, - "typedef" = true, - "union" = true, - "unsigned" = true, - "void" = true, - "volatile" = true, - "while" = true, - "_Alignas" = true, - "_Alignof" = true, - "_Atomic" = true, - "_Bool" = true, - "_Generic" = true, - "_Noreturn" = true, - "_Thread_local" = true, - "__restrict" = true, - "typeof" = true, - "asm" = true, - "__restrict__" = true, - "__thread" = true, - "__attribute__" = true, -} diff --git a/core/c/frontend/tokenizer/tokenizer.odin b/core/c/frontend/tokenizer/tokenizer.odin deleted file mode 100644 index 558077717..000000000 --- a/core/c/frontend/tokenizer/tokenizer.odin +++ /dev/null @@ -1,667 +0,0 @@ -package c_frontend_tokenizer - -import "core:fmt" -import "core:os" -import "core:strings" -import "core:unicode/utf8" - - -Error_Handler :: #type proc(pos: Pos, fmt: string, args: ..any) - - -Tokenizer :: struct { - // Immutable data - path: string, - src: []byte, - - - // Tokenizing state - ch: rune, - offset: int, - read_offset: int, - line_offset: int, - line_count: int, - - // Extra information for tokens - at_bol: bool, - has_space: bool, - - // Mutable data - err: Error_Handler, - warn: Error_Handler, - error_count: int, - warning_count: int, -} - -init_defaults :: proc(t: ^Tokenizer, err: Error_Handler = default_error_handler, warn: Error_Handler = default_warn_handler) { - t.err = err - t.warn = warn -} - - -@(private) -offset_to_pos :: proc(t: ^Tokenizer, offset: int) -> (pos: Pos) { - pos.file = t.path - pos.offset = offset - pos.line = t.line_count - pos.column = offset - t.line_offset + 1 - return -} - -default_error_handler :: proc(pos: Pos, msg: string, args: ..any) { - fmt.eprintf("%s(%d:%d) ", pos.file, pos.line, pos.column) - fmt.eprintf(msg, ..args) - fmt.eprintf("\n") -} - -default_warn_handler :: proc(pos: Pos, msg: string, args: ..any) { - fmt.eprintf("%s(%d:%d) warning: ", pos.file, pos.line, pos.column) - fmt.eprintf(msg, ..args) - fmt.eprintf("\n") -} - -error_offset :: proc(t: ^Tokenizer, offset: int, msg: string, args: ..any) { - pos := offset_to_pos(t, offset) - if t.err != nil { - t.err(pos, msg, ..args) - } - t.error_count += 1 -} - -warn_offset :: proc(t: ^Tokenizer, offset: int, msg: string, args: ..any) { - pos := offset_to_pos(t, offset) - if t.warn != nil { - t.warn(pos, msg, ..args) - } - t.warning_count += 1 -} - -error :: proc(t: ^Tokenizer, tok: ^Token, msg: string, args: ..any) { - pos := tok.pos - if t.err != nil { - t.err(pos, msg, ..args) - } - t.error_count += 1 -} - -warn :: proc(t: ^Tokenizer, tok: ^Token, msg: string, args: ..any) { - pos := tok.pos - if t.warn != nil { - t.warn(pos, msg, ..args) - } - t.warning_count += 1 -} - - -advance_rune :: proc(t: ^Tokenizer) { - if t.read_offset < len(t.src) { - t.offset = t.read_offset - if t.ch == '\n' { - t.at_bol = true - t.line_offset = t.offset - t.line_count += 1 - } - r, w := rune(t.src[t.read_offset]), 1 - switch { - case r == 0: - error_offset(t, t.offset, "illegal character NUL") - case r >= utf8.RUNE_SELF: - r, w = utf8.decode_rune(t.src[t.read_offset:]) - if r == utf8.RUNE_ERROR && w == 1 { - error_offset(t, t.offset, "illegal UTF-8 encoding") - } else if r == utf8.RUNE_BOM && t.offset > 0 { - error_offset(t, t.offset, "illegal byte order mark") - } - } - t.read_offset += w - t.ch = r - } else { - t.offset = len(t.src) - if t.ch == '\n' { - t.at_bol = true - t.line_offset = t.offset - t.line_count += 1 - } - t.ch = -1 - } -} - -advance_rune_n :: proc(t: ^Tokenizer, n: int) { - for _ in 0.. bool { - return '0' <= r && r <= '9' -} - -skip_whitespace :: proc(t: ^Tokenizer) { - for { - switch t.ch { - case ' ', '\t', '\r', '\v', '\f', '\n': - t.has_space = true - advance_rune(t) - case: - return - } - } -} - -scan_comment :: proc(t: ^Tokenizer) -> string { - offset := t.offset-1 - next := -1 - general: { - if t.ch == '/'{ // line comments - advance_rune(t) - for t.ch != '\n' && t.ch >= 0 { - advance_rune(t) - } - - next = t.offset - if t.ch == '\n' { - next += 1 - } - break general - } - - /* style comment */ - advance_rune(t) - for t.ch >= 0 { - ch := t.ch - advance_rune(t) - if ch == '*' && t.ch == '/' { - advance_rune(t) - next = t.offset - break general - } - } - - error_offset(t, offset, "comment not terminated") - } - - lit := t.src[offset : t.offset] - - // NOTE(bill): Strip CR for line comments - for len(lit) > 2 && lit[1] == '/' && lit[len(lit)-1] == '\r' { - lit = lit[:len(lit)-1] - } - - - return string(lit) -} - -scan_identifier :: proc(t: ^Tokenizer) -> string { - offset := t.offset - - for is_ident1(t.ch) { - advance_rune(t) - } - - return string(t.src[offset : t.offset]) -} - -scan_string :: proc(t: ^Tokenizer) -> string { - offset := t.offset-1 - - for { - ch := t.ch - if ch == '\n' || ch < 0 { - error_offset(t, offset, "string literal was not terminated") - break - } - advance_rune(t) - if ch == '"' { - break - } - if ch == '\\' { - scan_escape(t) - } - } - - return string(t.src[offset : t.offset]) -} - -digit_val :: proc(r: rune) -> int { - switch r { - case '0'..='9': - return int(r-'0') - case 'A'..='F': - return int(r-'A' + 10) - case 'a'..='f': - return int(r-'a' + 10) - } - return 16 -} - -scan_escape :: proc(t: ^Tokenizer) -> bool { - offset := t.offset - - esc := t.ch - n: int - base, max: u32 - switch esc { - case 'a', 'b', 'e', 'f', 'n', 't', 'v', 'r', '\\', '\'', '"': - advance_rune(t) - return true - - case '0'..='7': - for digit_val(t.ch) < 8 { - advance_rune(t) - } - return true - case 'x': - advance_rune(t) - for digit_val(t.ch) < 16 { - advance_rune(t) - } - return true - case 'u': - advance_rune(t) - n, base, max = 4, 16, utf8.MAX_RUNE - case 'U': - advance_rune(t) - n, base, max = 8, 16, utf8.MAX_RUNE - case: - if t.ch < 0 { - error_offset(t, offset, "escape sequence was not terminated") - } else { - break - } - return false - } - - x: u32 - main_loop: for n > 0 { - d := u32(digit_val(t.ch)) - if d >= base { - if t.ch == '"' || t.ch == '\'' { - break main_loop - } - if t.ch < 0 { - error_offset(t, t.offset, "escape sequence was not terminated") - } else { - error_offset(t, t.offset, "illegal character '%r' : %d in escape sequence", t.ch, t.ch) - } - return false - } - - x = x*base + d - advance_rune(t) - n -= 1 - } - - if x > max || 0xd800 <= x && x <= 0xdfff { - error_offset(t, offset, "escape sequence is an invalid Unicode code point") - return false - } - return true -} - -scan_rune :: proc(t: ^Tokenizer) -> string { - offset := t.offset-1 - valid := true - n := 0 - for { - ch := t.ch - if ch == '\n' || ch < 0 { - if valid { - error_offset(t, offset, "rune literal not terminated") - valid = false - } - break - } - advance_rune(t) - if ch == '\'' { - break - } - n += 1 - if ch == '\\' { - if !scan_escape(t) { - valid = false - } - } - } - - if valid && n != 1 { - error_offset(t, offset, "illegal rune literal") - } - - return string(t.src[offset : t.offset]) -} - -scan_number :: proc(t: ^Tokenizer, seen_decimal_point: bool) -> (Token_Kind, string) { - scan_mantissa :: proc(t: ^Tokenizer, base: int) { - for digit_val(t.ch) < base { - advance_rune(t) - } - } - scan_exponent :: proc(t: ^Tokenizer) { - if t.ch == 'e' || t.ch == 'E' || t.ch == 'p' || t.ch == 'P' { - advance_rune(t) - if t.ch == '-' || t.ch == '+' { - advance_rune(t) - } - if digit_val(t.ch) < 10 { - scan_mantissa(t, 10) - } else { - error_offset(t, t.offset, "illegal floating-point exponent") - } - } - } - scan_fraction :: proc(t: ^Tokenizer) -> (early_exit: bool) { - if t.ch == '.' && peek(t) == '.' { - return true - } - if t.ch == '.' { - advance_rune(t) - scan_mantissa(t, 10) - } - return false - } - - check_end := true - - - offset := t.offset - seen_point := seen_decimal_point - - if seen_point { - offset -= 1 - scan_mantissa(t, 10) - scan_exponent(t) - } else { - if t.ch == '0' { - int_base :: proc(t: ^Tokenizer, base: int, msg: string) { - prev := t.offset - advance_rune(t) - scan_mantissa(t, base) - if t.offset - prev <= 1 { - error_offset(t, t.offset, msg) - } - } - - advance_rune(t) - switch t.ch { - case 'b', 'B': - int_base(t, 2, "illegal binary integer") - case 'x', 'X': - int_base(t, 16, "illegal hexadecimal integer") - case: - seen_point = false - scan_mantissa(t, 10) - if t.ch == '.' { - seen_point = true - if scan_fraction(t) { - check_end = false - } - } - if check_end { - scan_exponent(t) - check_end = false - } - } - } - } - - if check_end { - scan_mantissa(t, 10) - - if !scan_fraction(t) { - scan_exponent(t) - } - } - - return .Number, string(t.src[offset : t.offset]) -} - -scan_punct :: proc(t: ^Tokenizer, ch: rune) -> (kind: Token_Kind) { - kind = .Punct - switch ch { - case: - kind = .Invalid - - case '<', '>': - if t.ch == ch { - advance_rune(t) - } - if t.ch == '=' { - advance_rune(t) - } - case '!', '+', '-', '*', '/', '%', '^', '=': - if t.ch == '=' { - advance_rune(t) - } - case '#': - if t.ch == '#' { - advance_rune(t) - } - case '&': - if t.ch == '=' || t.ch == '&' { - advance_rune(t) - } - case '|': - if t.ch == '=' || t.ch == '|' { - advance_rune(t) - } - case '(', ')', '[', ']', '{', '}': - // okay - case '~', ',', ':', ';', '?': - // okay - case '`': - // okay - case '.': - if t.ch == '.' && peek(t) == '.' { - advance_rune(t) - advance_rune(t) // consume last '.' - } - } - return -} - -peek :: proc(t: ^Tokenizer) -> byte { - if t.read_offset < len(t.src) { - return t.src[t.read_offset] - } - return 0 -} -peek_str :: proc(t: ^Tokenizer, str: string) -> bool { - if t.read_offset < len(t.src) { - return strings.has_prefix(string(t.src[t.offset:]), str) - } - return false -} - -scan_literal_prefix :: proc(t: ^Tokenizer, str: string, prefix: ^string) -> bool { - if peek_str(t, str) { - offset := t.offset - for _ in str { - advance_rune(t) - } - prefix^ = string(t.src[offset:][:len(str)-1]) - return true - } - return false -} - - -allow_next_to_be_newline :: proc(t: ^Tokenizer) -> bool { - if t.ch == '\n' { - advance_rune(t) - return true - } else if t.ch == '\r' && peek(t) == '\n' { // allow for MS-DOS style line endings - advance_rune(t) // \r - advance_rune(t) // \n - return true - } - return false -} - -scan :: proc(t: ^Tokenizer, f: ^File) -> ^Token { - skip_whitespace(t) - - offset := t.offset - - kind: Token_Kind - lit: string - prefix: string - - switch ch := t.ch; { - case scan_literal_prefix(t, `u8"`, &prefix): - kind = .String - lit = scan_string(t) - case scan_literal_prefix(t, `u"`, &prefix): - kind = .String - lit = scan_string(t) - case scan_literal_prefix(t, `L"`, &prefix): - kind = .String - lit = scan_string(t) - case scan_literal_prefix(t, `U"`, &prefix): - kind = .String - lit = scan_string(t) - case scan_literal_prefix(t, `u'`, &prefix): - kind = .Char - lit = scan_rune(t) - case scan_literal_prefix(t, `L'`, &prefix): - kind = .Char - lit = scan_rune(t) - case scan_literal_prefix(t, `U'`, &prefix): - kind = .Char - lit = scan_rune(t) - - case is_ident0(ch): - lit = scan_identifier(t) - kind = .Ident - case '0' <= ch && ch <= '9': - kind, lit = scan_number(t, false) - case: - advance_rune(t) - switch ch { - case -1: - kind = .EOF - case '\\': - kind = .Punct - if allow_next_to_be_newline(t) { - t.at_bol = true - t.has_space = false - return scan(t, f) - } - - case '.': - if is_digit(t.ch) { - kind, lit = scan_number(t, true) - } else { - kind = scan_punct(t, ch) - } - case '"': - kind = .String - lit = scan_string(t) - case '\'': - kind = .Char - lit = scan_rune(t) - case '/': - if t.ch == '/' || t.ch == '*' { - kind = .Comment - lit = scan_comment(t) - t.has_space = true - break - } - fallthrough - case: - kind = scan_punct(t, ch) - if kind == .Invalid && ch != utf8.RUNE_BOM { - error_offset(t, t.offset, "illegal character '%r': %d", ch, ch) - } - } - } - - if lit == "" { - lit = string(t.src[offset : t.offset]) - } - - if kind == .Comment { - return scan(t, f) - } - - tok := new(Token) - tok.kind = kind - tok.lit = lit - tok.pos = offset_to_pos(t, offset) - tok.file = f - tok.prefix = prefix - tok.at_bol = t.at_bol - tok.has_space = t.has_space - - t.at_bol, t.has_space = false, false - - return tok -} - -tokenize :: proc(t: ^Tokenizer, f: ^File) -> ^Token { - setup_tokenizer: { - t.src = f.src - t.ch = ' ' - t.offset = 0 - t.read_offset = 0 - t.line_offset = 0 - t.line_count = len(t.src) > 0 ? 1 : 0 - t.error_count = 0 - t.path = f.name - - - advance_rune(t) - if t.ch == utf8.RUNE_BOM { - advance_rune(t) - } - } - - - t.at_bol = true - t.has_space = false - - head: Token - curr := &head - for { - tok := scan(t, f) - if tok == nil { - break - } - curr.next = tok - curr = curr.next - if tok.kind == .EOF { - break - } - } - - return head.next -} - -add_new_file :: proc(t: ^Tokenizer, name: string, src: []byte, id: int) -> ^File { - file := new(File) - file.id = id - file.src = src - file.name = name - file.display_name = name - return file -} - -tokenize_file :: proc(t: ^Tokenizer, path: string, id: int, loc := #caller_location) -> ^Token { - src, ok := os.read_entire_file(path) - if !ok { - return nil - } - return tokenize(t, add_new_file(t, path, src, id)) -} - - -inline_tokenize :: proc(t: ^Tokenizer, tok: ^Token, src: []byte) -> ^Token { - file := new(File) - file.src = src - if tok.file != nil { - file.id = tok.file.id - file.name = tok.file.name - file.display_name = tok.file.name - } - - return tokenize(t, file) -} diff --git a/core/c/frontend/tokenizer/unicode.odin b/core/c/frontend/tokenizer/unicode.odin deleted file mode 100644 index 317ee160e..000000000 --- a/core/c/frontend/tokenizer/unicode.odin +++ /dev/null @@ -1,116 +0,0 @@ -package c_frontend_tokenizer - - -in_range :: proc(range: []rune, c: rune) -> bool #no_bounds_check { - for i := 0; range[i] != -1; i += 2 { - if range[i] <= c && c <= range[i+1] { - return true - } - } - return false -} - - -// [https://www.sigbus.info/n1570#D] C11 allows ASCII and some multibyte characters in certan Unicode ranges to be used in an identifier. -// -// is_ident0 returns true if a given character is acceptable as the first character of an identifier. -is_ident0 :: proc(c: rune) -> bool { - return in_range(_range_ident0, c) -} -// is_ident0 returns true if a given character is acceptable as a non-first character of an identifier. -is_ident1 :: proc(c: rune) -> bool { - return is_ident0(c) || in_range(_range_ident1, c) -} - -// Returns the number of columns needed to display a given character in a fixed-width font. -// Based on https://www.cl.cam.ac.uk/~mgk25/ucs/wcwidth.c -char_width :: proc(c: rune) -> int { - switch { - case in_range(_range_width0, c): - return 0 - case in_range(_range_width2, c): - return 2 - } - return 1 -} - -display_width :: proc(str: string) -> (w: int) { - for c in str { - w += char_width(c) - } - return -} - - - -_range_ident0 := []rune{ - '_', '_', 'a', 'z', 'A', 'Z', '$', '$', - 0x00A8, 0x00A8, 0x00AA, 0x00AA, 0x00AD, 0x00AD, 0x00AF, 0x00AF, - 0x00B2, 0x00B5, 0x00B7, 0x00BA, 0x00BC, 0x00BE, 0x00C0, 0x00D6, - 0x00D8, 0x00F6, 0x00F8, 0x00FF, 0x0100, 0x02FF, 0x0370, 0x167F, - 0x1681, 0x180D, 0x180F, 0x1DBF, 0x1E00, 0x1FFF, 0x200B, 0x200D, - 0x202A, 0x202E, 0x203F, 0x2040, 0x2054, 0x2054, 0x2060, 0x206F, - 0x2070, 0x20CF, 0x2100, 0x218F, 0x2460, 0x24FF, 0x2776, 0x2793, - 0x2C00, 0x2DFF, 0x2E80, 0x2FFF, 0x3004, 0x3007, 0x3021, 0x302F, - 0x3031, 0x303F, 0x3040, 0xD7FF, 0xF900, 0xFD3D, 0xFD40, 0xFDCF, - 0xFDF0, 0xFE1F, 0xFE30, 0xFE44, 0xFE47, 0xFFFD, - 0x10000, 0x1FFFD, 0x20000, 0x2FFFD, 0x30000, 0x3FFFD, 0x40000, 0x4FFFD, - 0x50000, 0x5FFFD, 0x60000, 0x6FFFD, 0x70000, 0x7FFFD, 0x80000, 0x8FFFD, - 0x90000, 0x9FFFD, 0xA0000, 0xAFFFD, 0xB0000, 0xBFFFD, 0xC0000, 0xCFFFD, - 0xD0000, 0xDFFFD, 0xE0000, 0xEFFFD, - -1, -} - -_range_ident1 := []rune{ - '0', '9', '$', '$', 0x0300, 0x036F, 0x1DC0, 0x1DFF, 0x20D0, 0x20FF, 0xFE20, 0xFE2F, - -1, -} - - -_range_width0 := []rune{ - 0x0000, 0x001F, 0x007f, 0x00a0, 0x0300, 0x036F, 0x0483, 0x0486, - 0x0488, 0x0489, 0x0591, 0x05BD, 0x05BF, 0x05BF, 0x05C1, 0x05C2, - 0x05C4, 0x05C5, 0x05C7, 0x05C7, 0x0600, 0x0603, 0x0610, 0x0615, - 0x064B, 0x065E, 0x0670, 0x0670, 0x06D6, 0x06E4, 0x06E7, 0x06E8, - 0x06EA, 0x06ED, 0x070F, 0x070F, 0x0711, 0x0711, 0x0730, 0x074A, - 0x07A6, 0x07B0, 0x07EB, 0x07F3, 0x0901, 0x0902, 0x093C, 0x093C, - 0x0941, 0x0948, 0x094D, 0x094D, 0x0951, 0x0954, 0x0962, 0x0963, - 0x0981, 0x0981, 0x09BC, 0x09BC, 0x09C1, 0x09C4, 0x09CD, 0x09CD, - 0x09E2, 0x09E3, 0x0A01, 0x0A02, 0x0A3C, 0x0A3C, 0x0A41, 0x0A42, - 0x0A47, 0x0A48, 0x0A4B, 0x0A4D, 0x0A70, 0x0A71, 0x0A81, 0x0A82, - 0x0ABC, 0x0ABC, 0x0AC1, 0x0AC5, 0x0AC7, 0x0AC8, 0x0ACD, 0x0ACD, - 0x0AE2, 0x0AE3, 0x0B01, 0x0B01, 0x0B3C, 0x0B3C, 0x0B3F, 0x0B3F, - 0x0B41, 0x0B43, 0x0B4D, 0x0B4D, 0x0B56, 0x0B56, 0x0B82, 0x0B82, - 0x0BC0, 0x0BC0, 0x0BCD, 0x0BCD, 0x0C3E, 0x0C40, 0x0C46, 0x0C48, - 0x0C4A, 0x0C4D, 0x0C55, 0x0C56, 0x0CBC, 0x0CBC, 0x0CBF, 0x0CBF, - 0x0CC6, 0x0CC6, 0x0CCC, 0x0CCD, 0x0CE2, 0x0CE3, 0x0D41, 0x0D43, - 0x0D4D, 0x0D4D, 0x0DCA, 0x0DCA, 0x0DD2, 0x0DD4, 0x0DD6, 0x0DD6, - 0x0E31, 0x0E31, 0x0E34, 0x0E3A, 0x0E47, 0x0E4E, 0x0EB1, 0x0EB1, - 0x0EB4, 0x0EB9, 0x0EBB, 0x0EBC, 0x0EC8, 0x0ECD, 0x0F18, 0x0F19, - 0x0F35, 0x0F35, 0x0F37, 0x0F37, 0x0F39, 0x0F39, 0x0F71, 0x0F7E, - 0x0F80, 0x0F84, 0x0F86, 0x0F87, 0x0F90, 0x0F97, 0x0F99, 0x0FBC, - 0x0FC6, 0x0FC6, 0x102D, 0x1030, 0x1032, 0x1032, 0x1036, 0x1037, - 0x1039, 0x1039, 0x1058, 0x1059, 0x1160, 0x11FF, 0x135F, 0x135F, - 0x1712, 0x1714, 0x1732, 0x1734, 0x1752, 0x1753, 0x1772, 0x1773, - 0x17B4, 0x17B5, 0x17B7, 0x17BD, 0x17C6, 0x17C6, 0x17C9, 0x17D3, - 0x17DD, 0x17DD, 0x180B, 0x180D, 0x18A9, 0x18A9, 0x1920, 0x1922, - 0x1927, 0x1928, 0x1932, 0x1932, 0x1939, 0x193B, 0x1A17, 0x1A18, - 0x1B00, 0x1B03, 0x1B34, 0x1B34, 0x1B36, 0x1B3A, 0x1B3C, 0x1B3C, - 0x1B42, 0x1B42, 0x1B6B, 0x1B73, 0x1DC0, 0x1DCA, 0x1DFE, 0x1DFF, - 0x200B, 0x200F, 0x202A, 0x202E, 0x2060, 0x2063, 0x206A, 0x206F, - 0x20D0, 0x20EF, 0x302A, 0x302F, 0x3099, 0x309A, 0xA806, 0xA806, - 0xA80B, 0xA80B, 0xA825, 0xA826, 0xFB1E, 0xFB1E, 0xFE00, 0xFE0F, - 0xFE20, 0xFE23, 0xFEFF, 0xFEFF, 0xFFF9, 0xFFFB, 0x10A01, 0x10A03, - 0x10A05, 0x10A06, 0x10A0C, 0x10A0F, 0x10A38, 0x10A3A, 0x10A3F, 0x10A3F, - 0x1D167, 0x1D169, 0x1D173, 0x1D182, 0x1D185, 0x1D18B, 0x1D1AA, 0x1D1AD, - 0x1D242, 0x1D244, 0xE0001, 0xE0001, 0xE0020, 0xE007F, 0xE0100, 0xE01EF, - -1, -} - -_range_width2 := []rune{ - 0x1100, 0x115F, 0x2329, 0x2329, 0x232A, 0x232A, 0x2E80, 0x303E, - 0x3040, 0xA4CF, 0xAC00, 0xD7A3, 0xF900, 0xFAFF, 0xFE10, 0xFE19, - 0xFE30, 0xFE6F, 0xFF00, 0xFF60, 0xFFE0, 0xFFE6, 0x1F000, 0x1F644, - 0x20000, 0x2FFFD, 0x30000, 0x3FFFD, - -1, -} diff --git a/core/compress/zlib/doc.odin b/core/compress/zlib/doc.odin index 0a5b4eb40..6ae537a87 100644 --- a/core/compress/zlib/doc.odin +++ b/core/compress/zlib/doc.odin @@ -13,6 +13,7 @@ Example: package main import "core:bytes" + import "core:compress/zlib" import "core:fmt" main :: proc() { @@ -36,7 +37,7 @@ Example: buf: bytes.Buffer // We can pass ", true" to inflate a raw DEFLATE stream instead of a ZLIB wrapped one. - err := inflate(input=ODIN_DEMO, buf=&buf, expected_output_size=OUTPUT_SIZE) + err := zlib.inflate(input=ODIN_DEMO, buf=&buf, expected_output_size=OUTPUT_SIZE) defer bytes.buffer_destroy(&buf) if err != nil { diff --git a/core/dynlib/example/example.odin b/core/dynlib/example/example.odin index f12233b0a..78fb5a98c 100644 --- a/core/dynlib/example/example.odin +++ b/core/dynlib/example/example.odin @@ -21,12 +21,14 @@ Symbols :: struct { main :: proc() { sym: Symbols + LIB_PATH :: "lib." + dynlib.LIBRARY_FILE_EXTENSION + // Load symbols from `lib.dll` into Symbols struct. // Each struct field is prefixed with `foo_` before lookup in the DLL's symbol table. // The library's Handle (to unload) will be stored in `sym._my_lib_handle`. This way you can load multiple DLLs in one struct. - count, ok := dynlib.initialize_symbols(&sym, "lib.dll", "foo_", "_my_lib_handle") + count, ok := dynlib.initialize_symbols(&sym, LIB_PATH, "foo_", "_my_lib_handle") defer dynlib.unload_library(sym._my_lib_handle) - fmt.printf("(Initial DLL Load) ok: %v. %v symbols loaded from lib.dll (%p).\n", ok, count, sym._my_lib_handle) + fmt.printf("(Initial DLL Load) ok: %v. %v symbols loaded from " + LIB_PATH + " (%p).\n", ok, count, sym._my_lib_handle) if count > 0 { fmt.println("42 + 42 =", sym.add(42, 42)) @@ -34,12 +36,12 @@ main :: proc() { fmt.println("hellope =", sym.hellope^) } - count, ok = dynlib.initialize_symbols(&sym, "lib.dll", "foo_", "_my_lib_handle") - fmt.printf("(DLL Reload) ok: %v. %v symbols loaded from lib.dll (%p).\n", ok, count, sym._my_lib_handle) + count, ok = dynlib.initialize_symbols(&sym, LIB_PATH, "foo_", "_my_lib_handle") + fmt.printf("(DLL Reload) ok: %v. %v symbols loaded from " + LIB_PATH + " (%p).\n", ok, count, sym._my_lib_handle) if count > 0 { fmt.println("42 + 42 =", sym.add(42, 42)) fmt.println("84 - 13 =", sym.sub(84, 13)) fmt.println("hellope =", sym.hellope^) } -} \ No newline at end of file +} diff --git a/core/dynlib/lib.odin b/core/dynlib/lib.odin index 84675a560..cd9eed5f8 100644 --- a/core/dynlib/lib.odin +++ b/core/dynlib/lib.odin @@ -12,6 +12,11 @@ A handle to a dynamically loaded library. */ Library :: distinct rawptr +/* +The file extension for dynamic libraries on the target OS. +*/ +LIBRARY_FILE_EXTENSION :: _LIBRARY_FILE_EXTENSION + /* Loads a dynamic library from the filesystem. The paramater `global_symbols` makes the symbols in the loaded library available to resolve references in subsequently loaded libraries. @@ -123,31 +128,36 @@ initialize_symbols :: proc( ) -> (count: int = -1, ok: bool = false) where intrinsics.type_is_struct(T) { assert(symbol_table != nil) - handle := load_library(library_path) or_return + // First, (re)load the library. + handle: Library + for field in reflect.struct_fields_zipped(T) { + if field.name == handle_field_name { + field_ptr := rawptr(uintptr(symbol_table) + field.offset) + + // We appear to be hot reloading. Unload previous incarnation of the library. + if old_handle := (^Library)(field_ptr)^; old_handle != nil { + unload_library(old_handle) or_return + } + + handle = load_library(library_path) or_return + (^Library)(field_ptr)^ = handle + break + } + } // Buffer to concatenate the prefix + symbol name. prefixed_symbol_buf: [2048]u8 = --- count = 0 for field in reflect.struct_fields_zipped(T) { + // If we're not the library handle, the field needs to be a pointer type, be it a procedure pointer or an exported global. + if field.name == handle_field_name || !(reflect.is_procedure(field.type) || reflect.is_pointer(field.type)) { + continue + } + // Calculate address of struct member field_ptr := rawptr(uintptr(symbol_table) + field.offset) - // If we've come across the struct member for the handle, store it and continue scanning for other symbols. - if field.name == handle_field_name { - // We appear to be hot reloading. Unload previous incarnation of the library. - if old_handle := (^Library)(field_ptr)^; old_handle != nil { - unload_library(old_handle) or_return - } - (^Library)(field_ptr)^ = handle - continue - } - - // We're not the library handle, so the field needs to be a pointer type, be it a procedure pointer or an exported global. - if !(reflect.is_procedure(field.type) || reflect.is_pointer(field.type)) { - continue - } - // Let's look up or construct the symbol name to find in the library prefixed_name: string diff --git a/core/dynlib/lib_js.odin b/core/dynlib/lib_js.odin index b99143ba0..99f486dd0 100644 --- a/core/dynlib/lib_js.odin +++ b/core/dynlib/lib_js.odin @@ -4,6 +4,8 @@ package dynlib import "base:runtime" +_LIBRARY_FILE_EXTENSION :: "" + _load_library :: proc(path: string, global_symbols: bool, allocator: runtime.Allocator) -> (Library, bool) { return nil, false } diff --git a/core/dynlib/lib_unix.odin b/core/dynlib/lib_unix.odin index 337bf496d..50ab1acc8 100644 --- a/core/dynlib/lib_unix.odin +++ b/core/dynlib/lib_unix.odin @@ -7,6 +7,8 @@ import "base:runtime" import "core:strings" import "core:sys/posix" +_LIBRARY_FILE_EXTENSION :: "dylib" when ODIN_OS == .Darwin else "so" + _load_library :: proc(path: string, global_symbols: bool, allocator: runtime.Allocator) -> (Library, bool) { flags := posix.RTLD_Flags{.NOW} if global_symbols { diff --git a/core/dynlib/lib_windows.odin b/core/dynlib/lib_windows.odin index 928a1510d..05cd2cb3c 100644 --- a/core/dynlib/lib_windows.odin +++ b/core/dynlib/lib_windows.odin @@ -8,6 +8,8 @@ import win32 "core:sys/windows" import "core:strings" import "core:reflect" +_LIBRARY_FILE_EXTENSION :: "dll" + _load_library :: proc(path: string, global_symbols: bool, allocator: runtime.Allocator) -> (Library, bool) { // NOTE(bill): 'global_symbols' is here only for consistency with POSIX which has RTLD_GLOBAL wide_path := win32.utf8_to_wstring(path, allocator) diff --git a/core/encoding/hxa/read.odin b/core/encoding/hxa/read.odin index 5c8503229..a679946f8 100644 --- a/core/encoding/hxa/read.odin +++ b/core/encoding/hxa/read.odin @@ -117,7 +117,7 @@ read :: proc(data: []byte, filename := "", print_error := false, allocato layer.name = read_name(r) or_return layer.components = read_value(r, u8) or_return type := read_value(r, Layer_Data_Type) or_return - if type > max(type) { + if type > max(Layer_Data_Type) { if r.print_error { fmt.eprintf("HxA Error: file '%s' has layer data type %d. Maximum value is %d\n", r.filename, u8(type), u8(max(Layer_Data_Type))) diff --git a/core/encoding/json/marshal.odin b/core/encoding/json/marshal.odin index 009bf7ade..f0f0927a1 100644 --- a/core/encoding/json/marshal.odin +++ b/core/encoding/json/marshal.odin @@ -192,12 +192,6 @@ marshal_to_writer :: proc(w: io.Writer, v: any, opt: ^Marshal_Options) -> (err: case runtime.Type_Info_Simd_Vector: return .Unsupported_Type - - case runtime.Type_Info_Relative_Pointer: - return .Unsupported_Type - - case runtime.Type_Info_Relative_Multi_Pointer: - return .Unsupported_Type case runtime.Type_Info_Matrix: return .Unsupported_Type diff --git a/core/encoding/json/tokenizer.odin b/core/encoding/json/tokenizer.odin index 5c20a2cc3..e46d879a7 100644 --- a/core/encoding/json/tokenizer.odin +++ b/core/encoding/json/tokenizer.odin @@ -259,6 +259,7 @@ get_token :: proc(t: ^Tokenizer) -> (token: Token, err: Error) { skip_digits(t) } if t.r == 'e' || t.r == 'E' { + token.kind = .Float switch r := next_rune(t); r { case '+', '-': next_rune(t) @@ -485,7 +486,7 @@ is_valid_string_literal :: proc(str: string, spec: Specification) -> bool { case '"': // okay case '\'': - if spec != .JSON { + if spec == .JSON { return false } // okay diff --git a/core/encoding/json/unmarshal.odin b/core/encoding/json/unmarshal.odin index 8a50989f4..33fd104b7 100644 --- a/core/encoding/json/unmarshal.odin +++ b/core/encoding/json/unmarshal.odin @@ -172,20 +172,33 @@ assign_float :: proc(val: any, f: $T) -> bool { @(private) -unmarshal_string_token :: proc(p: ^Parser, val: any, str: string, ti: ^reflect.Type_Info) -> bool { +unmarshal_string_token :: proc(p: ^Parser, val: any, str: string, ti: ^reflect.Type_Info) -> (ok: bool, err: Error) { val := val switch &dst in val { case string: dst = str - return true + return true, nil case cstring: if str == "" { - dst = strings.clone_to_cstring("", p.allocator) + a_err: runtime.Allocator_Error + dst, a_err = strings.clone_to_cstring("", p.allocator) + #partial switch a_err { + case nil: + // okay + case .Out_Of_Memory: + err = .Out_Of_Memory + case: + err = .Invalid_Allocator + } + if err != nil { + return + } } else { // NOTE: This is valid because 'clone_string' appends a NUL terminator dst = cstring(raw_data(str)) } - return true + ok = true + return } #partial switch variant in ti.variant { @@ -193,31 +206,37 @@ unmarshal_string_token :: proc(p: ^Parser, val: any, str: string, ti: ^reflect.T for name, i in variant.names { if name == str { assign_int(val, variant.values[i]) - return true + return true, nil } } // TODO(bill): should this be an error or not? - return true + return true, nil case reflect.Type_Info_Integer: - i := strconv.parse_i128(str) or_return + i, pok := strconv.parse_i128(str) + if !pok { + return false, nil + } if assign_int(val, i) { - return true + return true, nil } if assign_float(val, i) { - return true + return true, nil } case reflect.Type_Info_Float: - f := strconv.parse_f64(str) or_return + f, pok := strconv.parse_f64(str) + if !pok { + return false, nil + } if assign_int(val, f) { - return true + return true, nil } if assign_float(val, f) { - return true + return true, nil } } - return false + return false, nil } @@ -304,7 +323,7 @@ unmarshal_value :: proc(p: ^Parser, v: any) -> (err: Unmarshal_Error) { case .Ident: advance_token(p) if p.spec == .MJSON { - if unmarshal_string_token(p, any{v.data, ti.id}, token.text, ti) { + if unmarshal_string_token(p, any{v.data, ti.id}, token.text, ti) or_return { return nil } } @@ -314,7 +333,7 @@ unmarshal_value :: proc(p: ^Parser, v: any) -> (err: Unmarshal_Error) { advance_token(p) str := unquote_string(token, p.spec, p.allocator) or_return dest := any{v.data, ti.id} - if !unmarshal_string_token(p, dest, str, ti) { + if !(unmarshal_string_token(p, dest, str, ti) or_return) { delete(str, p.allocator) return UNSUPPORTED_TYPE } @@ -398,15 +417,15 @@ unmarshal_object :: proc(p: ^Parser, v: any, end_token: Token_Kind) -> (err: Unm if .raw_union in t.flags { return UNSUPPORTED_TYPE } - + + fields := reflect.struct_fields_zipped(ti.id) + struct_loop: for p.curr_token.kind != end_token { - key, _ := parse_object_key(p, p.allocator) + key := parse_object_key(p, p.allocator) or_return defer delete(key, p.allocator) unmarshal_expect_token(p, .Colon) - fields := reflect.struct_fields_zipped(ti.id) - field_test :: #force_inline proc "contextless" (field_used: [^]byte, offset: uintptr) -> bool { prev_set := field_used[offset/8] & byte(offset&7) != 0 field_used[offset/8] |= byte(offset&7) diff --git a/core/fmt/fmt.odin b/core/fmt/fmt.odin index dd5c2c6a2..49e9f2e6d 100644 --- a/core/fmt/fmt.odin +++ b/core/fmt/fmt.odin @@ -3002,18 +3002,6 @@ fmt_value :: proc(fi: ^Info, v: any, verb: rune) { case runtime.Type_Info_Bit_Set: fmt_bit_set(fi, v, verb = verb) - case runtime.Type_Info_Relative_Pointer: - ptr := reflect.relative_pointer_to_absolute_raw(v.data, info.base_integer.id) - absolute_ptr := any{ptr, info.pointer.id} - - fmt_value(fi, absolute_ptr, verb) - - case runtime.Type_Info_Relative_Multi_Pointer: - ptr := reflect.relative_pointer_to_absolute_raw(v.data, info.base_integer.id) - absolute_ptr := any{ptr, info.pointer.id} - - fmt_value(fi, absolute_ptr, verb) - case runtime.Type_Info_Matrix: fmt_matrix(fi, v, verb, info) diff --git a/core/log/file_console_logger.odin b/core/log/file_console_logger.odin index e45f99523..6d93fb879 100644 --- a/core/log/file_console_logger.odin +++ b/core/log/file_console_logger.odin @@ -37,30 +37,30 @@ File_Console_Logger_Data :: struct { ident: string, } -create_file_logger :: proc(h: os.Handle, lowest := Level.Debug, opt := Default_File_Logger_Opts, ident := "") -> Logger { - data := new(File_Console_Logger_Data) +create_file_logger :: proc(h: os.Handle, lowest := Level.Debug, opt := Default_File_Logger_Opts, ident := "", allocator := context.allocator) -> Logger { + data := new(File_Console_Logger_Data, allocator) data.file_handle = h data.ident = ident return Logger{file_console_logger_proc, data, lowest, opt} } -destroy_file_logger :: proc(log: Logger) { +destroy_file_logger :: proc(log: Logger, allocator := context.allocator) { data := cast(^File_Console_Logger_Data)log.data if data.file_handle != os.INVALID_HANDLE { os.close(data.file_handle) } - free(data) + free(data, allocator) } -create_console_logger :: proc(lowest := Level.Debug, opt := Default_Console_Logger_Opts, ident := "") -> Logger { - data := new(File_Console_Logger_Data) +create_console_logger :: proc(lowest := Level.Debug, opt := Default_Console_Logger_Opts, ident := "", allocator := context.allocator) -> Logger { + data := new(File_Console_Logger_Data, allocator) data.file_handle = os.INVALID_HANDLE data.ident = ident return Logger{file_console_logger_proc, data, lowest, opt} } -destroy_console_logger :: proc(log: Logger) { - free(log.data) +destroy_console_logger :: proc(log: Logger, allocator := context.allocator) { + free(log.data, allocator) } file_console_logger_proc :: proc(logger_data: rawptr, level: Level, text: string, options: Options, location := #caller_location) { diff --git a/core/log/multi_logger.odin b/core/log/multi_logger.odin index 96d0f3dbd..6122554bb 100644 --- a/core/log/multi_logger.odin +++ b/core/log/multi_logger.odin @@ -5,17 +5,17 @@ Multi_Logger_Data :: struct { loggers: []Logger, } -create_multi_logger :: proc(logs: ..Logger) -> Logger { - data := new(Multi_Logger_Data) - data.loggers = make([]Logger, len(logs)) +create_multi_logger :: proc(logs: ..Logger, allocator := context.allocator) -> Logger { + data := new(Multi_Logger_Data, allocator) + data.loggers = make([]Logger, len(logs), allocator) copy(data.loggers, logs) return Logger{multi_logger_proc, data, Level.Debug, nil} } -destroy_multi_logger :: proc(log: Logger) { +destroy_multi_logger :: proc(log: Logger, allocator := context.allocator) { data := (^Multi_Logger_Data)(log.data) - delete(data.loggers) - free(data) + delete(data.loggers, allocator) + free(data, allocator) } multi_logger_proc :: proc(logger_data: rawptr, level: Level, text: string, diff --git a/core/math/linalg/general.odin b/core/math/linalg/general.odin index 7587ad63f..8c4f2954a 100644 --- a/core/math/linalg/general.odin +++ b/core/math/linalg/general.odin @@ -53,15 +53,15 @@ vector_dot :: proc "contextless" (a, b: $T/[$N]$E) -> (c: E) where IS_NUMERIC(E) } @(require_results) quaternion64_dot :: proc "contextless" (a, b: $T/quaternion64) -> (c: f16) { - return a.w*a.w + a.x*b.x + a.y*b.y + a.z*b.z + return a.w*b.w + a.x*b.x + a.y*b.y + a.z*b.z } @(require_results) quaternion128_dot :: proc "contextless" (a, b: $T/quaternion128) -> (c: f32) { - return a.w*a.w + a.x*b.x + a.y*b.y + a.z*b.z + return a.w*b.w + a.x*b.x + a.y*b.y + a.z*b.z } @(require_results) quaternion256_dot :: proc "contextless" (a, b: $T/quaternion256) -> (c: f64) { - return a.w*a.w + a.x*b.x + a.y*b.y + a.z*b.z + return a.w*b.w + a.x*b.x + a.y*b.y + a.z*b.z } dot :: proc{scalar_dot, vector_dot, quaternion64_dot, quaternion128_dot, quaternion256_dot} @@ -167,6 +167,18 @@ vector_triple_product :: proc "contextless" (a, b, c: $T/[$N]$E) -> T where IS_N length :: proc{vector_length, quaternion_length} length2 :: proc{vector_length2, quaternion_length2} + +@(require_results) +clamp_length :: proc "contextless" (v: $T/[$N]$E, a: E) -> T where IS_FLOAT(E) { + if a <= 0 { + return 0 + } + + m2 := length2(v) + return v if (m2 <= a*a) else (v / sqrt(m2) * a) // returns original when m2 is 0 +} + + @(require_results) projection :: proc "contextless" (x, normal: $T/[$N]$E) -> T where IS_NUMERIC(E) { return dot(x, normal) / dot(normal, normal) * normal diff --git a/core/math/math.odin b/core/math/math.odin index 0e21afa67..934842318 100644 --- a/core/math/math.odin +++ b/core/math/math.odin @@ -1271,7 +1271,7 @@ binomial :: proc "contextless" (n, k: int) -> int { } b := n - for i in 2.. (sin, cos: f64) #no_bounds_check { // sin coefficients @(private="file") _sin := [?]f64{ - 0h3de5d8fd1fd19ccd, // 1.58962301576546568060e-10 - 0hbe5ae5e5a9291f5d, // -2.50507477628578072866e-8 - 0h3ec71de3567d48a1, // 2.75573136213857245213e-6 - 0hbf2a01a019bfdf03, // -1.98412698295895385996e-4 - 0h3f8111111110f7d0, // 8.33333333332211858878e-3 - 0hbfc5555555555548, // -1.66666666666666307295e-1 + 0h3de5d8fd1fd19ccd, // 1.58962301576546568060e-10 + 0hbe5ae5e5a9291f5d, // -2.50507477628578072866e-8 + 0h3ec71de3567d48a1, // 2.75573136213857245213e-6 + 0hbf2a01a019bfdf03, // -1.98412698295895385996e-4 + 0h3f8111111110f7d0, // 8.33333333332211858878e-3 + 0hbfc5555555555548, // -1.66666666666666307295e-1 } // cos coefficients diff --git a/core/mem/virtual/virtual_posix.odin b/core/mem/virtual/virtual_posix.odin index 105849774..c3d6a9095 100644 --- a/core/mem/virtual/virtual_posix.odin +++ b/core/mem/virtual/virtual_posix.odin @@ -6,17 +6,13 @@ import "core:sys/posix" // Define non-posix needed flags: when ODIN_OS == .Darwin || ODIN_OS == .FreeBSD { - MAP_ANONYMOUS :: 0x1000 /* allocated from memory, swap space */ - - MADV_FREE :: 5 /* pages unneeded, discard contents */ + MADV_FREE :: 5 /* pages unneeded, discard contents */ } else when ODIN_OS == .OpenBSD || ODIN_OS == .NetBSD { - MAP_ANONYMOUS :: 0x1000 - - MADV_FREE :: 6 + MADV_FREE :: 6 } _reserve :: proc "contextless" (size: uint) -> (data: []byte, err: Allocator_Error) { - flags := posix.Map_Flags{ .PRIVATE } + transmute(posix.Map_Flags)i32(MAP_ANONYMOUS) + flags := posix.Map_Flags{ .ANONYMOUS, .PRIVATE } result := posix.mmap(nil, size, {}, flags) if result == posix.MAP_FAILED { return nil, .Out_Of_Memory diff --git a/core/odin/parser/file_tags.odin b/core/odin/parser/file_tags.odin index 84b172148..c5c6637c3 100644 --- a/core/odin/parser/file_tags.odin +++ b/core/odin/parser/file_tags.odin @@ -17,6 +17,9 @@ Build_Kind :: struct { arch: runtime.Odin_Arch_Types, } +// empty build kind acts as a marker for separating multiple lines with build tags +BUILD_KIND_NEWLINE_MARKER :: Build_Kind{} + File_Tags :: struct { build_project_name: [][]string, build: []Build_Kind, @@ -147,6 +150,11 @@ parse_file_tags :: proc(file: ast.File, allocator := context.allocator) -> (tags append(build_project_names, build_project_name_strings[index_start:]) } case "build": + + if len(build_kinds) > 0 { + append(build_kinds, BUILD_KIND_NEWLINE_MARKER) + } + kinds_loop: for { os_positive: runtime.Odin_OS_Types os_negative: runtime.Odin_OS_Types @@ -248,10 +256,20 @@ match_build_tags :: proc(file_tags: File_Tags, target: Build_Target) -> bool { project_name_correct ||= group_correct } - os_and_arch_correct := len(file_tags.build) == 0 + os_and_arch_correct := true - for kind in file_tags.build { - os_and_arch_correct ||= target.os in kind.os && target.arch in kind.arch + if len(file_tags.build) > 0 { + os_and_arch_correct_line := false + + for kind in file_tags.build { + if kind == BUILD_KIND_NEWLINE_MARKER { + os_and_arch_correct &&= os_and_arch_correct_line + os_and_arch_correct_line = false + } else { + os_and_arch_correct_line ||= target.os in kind.os && target.arch in kind.arch + } + } + os_and_arch_correct &&= os_and_arch_correct_line } return !file_tags.ignore && project_name_correct && os_and_arch_correct diff --git a/core/os/file_windows.odin b/core/os/file_windows.odin deleted file mode 100644 index 375da6aff..000000000 --- a/core/os/file_windows.odin +++ /dev/null @@ -1,584 +0,0 @@ -package os - -import win32 "core:sys/windows" -import "base:intrinsics" -import "base:runtime" -import "core:unicode/utf16" - -@(require_results) -is_path_separator :: proc(c: byte) -> bool { - return c == '/' || c == '\\' -} - -@(require_results) -open :: proc(path: string, mode: int = O_RDONLY, perm: int = 0) -> (Handle, Error) { - if len(path) == 0 { - return INVALID_HANDLE, General_Error.Not_Exist - } - - access: u32 - switch mode & (O_RDONLY|O_WRONLY|O_RDWR) { - case O_RDONLY: access = win32.FILE_GENERIC_READ - case O_WRONLY: access = win32.FILE_GENERIC_WRITE - case O_RDWR: access = win32.FILE_GENERIC_READ | win32.FILE_GENERIC_WRITE - } - - if mode&O_CREATE != 0 { - access |= win32.FILE_GENERIC_WRITE - } - if mode&O_APPEND != 0 { - access &~= win32.FILE_GENERIC_WRITE - access |= win32.FILE_APPEND_DATA - } - - share_mode := win32.FILE_SHARE_READ|win32.FILE_SHARE_WRITE - sa: ^win32.SECURITY_ATTRIBUTES = nil - sa_inherit := win32.SECURITY_ATTRIBUTES{nLength = size_of(win32.SECURITY_ATTRIBUTES), bInheritHandle = true} - if mode&O_CLOEXEC == 0 { - sa = &sa_inherit - } - - create_mode: u32 - switch { - case mode&(O_CREATE|O_EXCL) == (O_CREATE | O_EXCL): - create_mode = win32.CREATE_NEW - case mode&(O_CREATE|O_TRUNC) == (O_CREATE | O_TRUNC): - create_mode = win32.CREATE_ALWAYS - case mode&O_CREATE == O_CREATE: - create_mode = win32.OPEN_ALWAYS - case mode&O_TRUNC == O_TRUNC: - create_mode = win32.TRUNCATE_EXISTING - case: - create_mode = win32.OPEN_EXISTING - } - wide_path := win32.utf8_to_wstring(path) - handle := Handle(win32.CreateFileW(wide_path, access, share_mode, sa, create_mode, win32.FILE_ATTRIBUTE_NORMAL|win32.FILE_FLAG_BACKUP_SEMANTICS, nil)) - if handle != INVALID_HANDLE { - return handle, nil - } - - return INVALID_HANDLE, get_last_error() -} - -close :: proc(fd: Handle) -> Error { - if !win32.CloseHandle(win32.HANDLE(fd)) { - return get_last_error() - } - return nil -} - -flush :: proc(fd: Handle) -> (err: Error) { - if !win32.FlushFileBuffers(win32.HANDLE(fd)) { - err = get_last_error() - } - return -} - - - -write :: proc(fd: Handle, data: []byte) -> (int, Error) { - if len(data) == 0 { - return 0, nil - } - - single_write_length: win32.DWORD - total_write: i64 - length := i64(len(data)) - - for total_write < length { - remaining := length - total_write - to_write := win32.DWORD(min(i32(remaining), MAX_RW)) - - e := win32.WriteFile(win32.HANDLE(fd), &data[total_write], to_write, &single_write_length, nil) - if single_write_length <= 0 || !e { - return int(total_write), get_last_error() - } - total_write += i64(single_write_length) - } - return int(total_write), nil -} - -@(private="file", require_results) -read_console :: proc(handle: win32.HANDLE, b: []byte) -> (n: int, err: Error) { - if len(b) == 0 { - return 0, nil - } - - BUF_SIZE :: 386 - buf16: [BUF_SIZE]u16 - buf8: [4*BUF_SIZE]u8 - - for n < len(b) && err == nil { - min_read := max(len(b)/4, 1 if len(b) > 0 else 0) - max_read := u32(min(BUF_SIZE, min_read)) - if max_read == 0 { - break - } - - single_read_length: u32 - ok := win32.ReadConsoleW(handle, &buf16[0], max_read, &single_read_length, nil) - if !ok { - err = get_last_error() - } - - buf8_len := utf16.decode_to_utf8(buf8[:], buf16[:single_read_length]) - src := buf8[:buf8_len] - - ctrl_z := false - for i := 0; i < len(src) && n < len(b); i += 1 { - x := src[i] - if x == 0x1a { // ctrl-z - ctrl_z = true - break - } - b[n] = x - n += 1 - } - if ctrl_z || single_read_length < max_read { - break - } - - // NOTE(bill): if the last two values were a newline, then it is expected that - // this is the end of the input - if n >= 2 && single_read_length == max_read && string(b[n-2:n]) == "\r\n" { - break - } - - } - - return -} - -read :: proc(fd: Handle, data: []byte) -> (total_read: int, err: Error) { - if len(data) == 0 { - return 0, nil - } - - handle := win32.HANDLE(fd) - - m: u32 - is_console := win32.GetConsoleMode(handle, &m) - length := len(data) - - // NOTE(Jeroen): `length` can't be casted to win32.DWORD here because it'll overflow if > 4 GiB and return 0 if exactly that. - to_read := min(i64(length), MAX_RW) - - if is_console { - total_read, err = read_console(handle, data[total_read:][:to_read]) - if err != nil { - return total_read, err - } - } else { - // NOTE(Jeroen): So we cast it here *after* we've ensured that `to_read` is at most MAX_RW (1 GiB) - bytes_read: win32.DWORD - if e := win32.ReadFile(handle, &data[total_read], win32.DWORD(to_read), &bytes_read, nil); e { - // Successful read can mean two things, including EOF, see: - // https://learn.microsoft.com/en-us/windows/win32/fileio/testing-for-the-end-of-a-file - if bytes_read == 0 { - return 0, .EOF - } else { - return int(bytes_read), nil - } - } else { - return 0, get_last_error() - } - } - return total_read, nil -} - -seek :: proc(fd: Handle, offset: i64, whence: int) -> (i64, Error) { - w: u32 - switch whence { - case 0: w = win32.FILE_BEGIN - case 1: w = win32.FILE_CURRENT - case 2: w = win32.FILE_END - case: - return 0, .Invalid_Whence - } - hi := i32(offset>>32) - lo := i32(offset) - ft := win32.GetFileType(win32.HANDLE(fd)) - if ft == win32.FILE_TYPE_PIPE { - return 0, .File_Is_Pipe - } - - dw_ptr := win32.SetFilePointer(win32.HANDLE(fd), lo, &hi, w) - if dw_ptr == win32.INVALID_SET_FILE_POINTER { - err := get_last_error() - return 0, err - } - return i64(hi)<<32 + i64(dw_ptr), nil -} - -@(require_results) -file_size :: proc(fd: Handle) -> (i64, Error) { - length: win32.LARGE_INTEGER - err: Error - if !win32.GetFileSizeEx(win32.HANDLE(fd), &length) { - err = get_last_error() - } - return i64(length), err -} - - -@(private) -MAX_RW :: 1<<30 - -@(private) -pread :: proc(fd: Handle, data: []byte, offset: i64) -> (n: int, err: Error) { - curr_off := seek(fd, 0, 1) or_return - defer seek(fd, curr_off, 0) - - buf := data - if len(buf) > MAX_RW { - buf = buf[:MAX_RW] - } - - o := win32.OVERLAPPED{ - OffsetHigh = u32(offset>>32), - Offset = u32(offset), - } - - // TODO(bill): Determine the correct behaviour for consoles - - h := win32.HANDLE(fd) - done: win32.DWORD - e: Error - if !win32.ReadFile(h, raw_data(buf), u32(len(buf)), &done, &o) { - e = get_last_error() - done = 0 - } - return int(done), e -} -@(private) -pwrite :: proc(fd: Handle, data: []byte, offset: i64) -> (n: int, err: Error) { - curr_off := seek(fd, 0, 1) or_return - defer seek(fd, curr_off, 0) - - buf := data - if len(buf) > MAX_RW { - buf = buf[:MAX_RW] - } - - o := win32.OVERLAPPED{ - OffsetHigh = u32(offset>>32), - Offset = u32(offset), - } - - h := win32.HANDLE(fd) - done: win32.DWORD - e: Error - if !win32.WriteFile(h, raw_data(buf), u32(len(buf)), &done, &o) { - e = get_last_error() - done = 0 - } - return int(done), e -} - -/* -read_at returns n: 0, err: 0 on EOF -*/ -read_at :: proc(fd: Handle, data: []byte, offset: i64) -> (n: int, err: Error) { - if offset < 0 { - return 0, .Invalid_Offset - } - - b, offset := data, offset - for len(b) > 0 { - m, e := pread(fd, b, offset) - if e == ERROR_EOF { - err = nil - break - } - if e != nil { - err = e - break - } - n += m - b = b[m:] - offset += i64(m) - } - return -} - -write_at :: proc(fd: Handle, data: []byte, offset: i64) -> (n: int, err: Error) { - if offset < 0 { - return 0, .Invalid_Offset - } - - b, offset := data, offset - for len(b) > 0 { - m := pwrite(fd, b, offset) or_return - n += m - b = b[m:] - offset += i64(m) - } - return -} - - - -// NOTE(bill): Uses startup to initialize it -stdin := get_std_handle(uint(win32.STD_INPUT_HANDLE)) -stdout := get_std_handle(uint(win32.STD_OUTPUT_HANDLE)) -stderr := get_std_handle(uint(win32.STD_ERROR_HANDLE)) - - -@(require_results) -get_std_handle :: proc "contextless" (h: uint) -> Handle { - fd := win32.GetStdHandle(win32.DWORD(h)) - return Handle(fd) -} - - -exists :: proc(path: string) -> bool { - runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD() - wpath := win32.utf8_to_wstring(path, context.temp_allocator) - attribs := win32.GetFileAttributesW(wpath) - - return attribs != win32.INVALID_FILE_ATTRIBUTES -} - -@(require_results) -is_file :: proc(path: string) -> bool { - runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD() - wpath := win32.utf8_to_wstring(path, context.temp_allocator) - attribs := win32.GetFileAttributesW(wpath) - - if attribs != win32.INVALID_FILE_ATTRIBUTES { - return attribs & win32.FILE_ATTRIBUTE_DIRECTORY == 0 - } - return false -} - -@(require_results) -is_dir :: proc(path: string) -> bool { - runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD() - wpath := win32.utf8_to_wstring(path, context.temp_allocator) - attribs := win32.GetFileAttributesW(wpath) - - if attribs != win32.INVALID_FILE_ATTRIBUTES { - return attribs & win32.FILE_ATTRIBUTE_DIRECTORY != 0 - } - return false -} - -// NOTE(tetra): GetCurrentDirectory is not thread safe with SetCurrentDirectory and GetFullPathName -@private cwd_lock := win32.SRWLOCK{} // zero is initialized - -@(require_results) -get_current_directory :: proc(allocator := context.allocator) -> string { - win32.AcquireSRWLockExclusive(&cwd_lock) - - runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD(ignore = context.temp_allocator == allocator) - - sz_utf16 := win32.GetCurrentDirectoryW(0, nil) - dir_buf_wstr, _ := make([]u16, sz_utf16, context.temp_allocator) // the first time, it _includes_ the NUL. - - sz_utf16 = win32.GetCurrentDirectoryW(win32.DWORD(len(dir_buf_wstr)), raw_data(dir_buf_wstr)) - assert(int(sz_utf16)+1 == len(dir_buf_wstr)) // the second time, it _excludes_ the NUL. - - win32.ReleaseSRWLockExclusive(&cwd_lock) - - return win32.utf16_to_utf8(dir_buf_wstr, allocator) or_else "" -} - -set_current_directory :: proc(path: string) -> (err: Error) { - runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD() - wstr := win32.utf8_to_wstring(path, context.temp_allocator) - - win32.AcquireSRWLockExclusive(&cwd_lock) - - if !win32.SetCurrentDirectoryW(wstr) { - err = get_last_error() - } - - win32.ReleaseSRWLockExclusive(&cwd_lock) - - return -} -change_directory :: set_current_directory - -make_directory :: proc(path: string, mode: u32 = 0) -> (err: Error) { - runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD() - // Mode is unused on Windows, but is needed on *nix - wpath := win32.utf8_to_wstring(path, context.temp_allocator) - - if !win32.CreateDirectoryW(wpath, nil) { - err = get_last_error() - } - return -} - - -remove_directory :: proc(path: string) -> (err: Error) { - runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD() - wpath := win32.utf8_to_wstring(path, context.temp_allocator) - - if !win32.RemoveDirectoryW(wpath) { - err = get_last_error() - } - return -} - - - -@(private, require_results) -is_abs :: proc(path: string) -> bool { - if len(path) > 0 && path[0] == '/' { - return true - } - when ODIN_OS == .Windows { - if len(path) > 2 { - switch path[0] { - case 'A'..='Z', 'a'..='z': - return path[1] == ':' && is_path_separator(path[2]) - } - } - } - return false -} - -@(private, require_results) -fix_long_path :: proc(path: string) -> string { - if len(path) < 248 { - return path - } - - if len(path) >= 2 && path[:2] == `\\` { - return path - } - if !is_abs(path) { - return path - } - - prefix :: `\\?` - - path_buf, _ := make([]byte, len(prefix)+len(path)+len(`\`), context.temp_allocator) - copy(path_buf, prefix) - n := len(path) - r, w := 0, len(prefix) - for r < n { - switch { - case is_path_separator(path[r]): - r += 1 - case path[r] == '.' && (r+1 == n || is_path_separator(path[r+1])): - r += 1 - case r+1 < n && path[r] == '.' && path[r+1] == '.' && (r+2 == n || is_path_separator(path[r+2])): - return path - case: - path_buf[w] = '\\' - w += 1 - for ; r < n && !is_path_separator(path[r]); r += 1 { - path_buf[w] = path[r] - w += 1 - } - } - } - - if w == len(`\\?\c:`) { - path_buf[w] = '\\' - w += 1 - } - return string(path_buf[:w]) -} - - -link :: proc(old_name, new_name: string) -> (err: Error) { - runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD() - n := win32.utf8_to_wstring(fix_long_path(new_name)) - o := win32.utf8_to_wstring(fix_long_path(old_name)) - return Platform_Error(win32.CreateHardLinkW(n, o, nil)) -} - -unlink :: proc(path: string) -> (err: Error) { - runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD() - wpath := win32.utf8_to_wstring(path, context.temp_allocator) - - if !win32.DeleteFileW(wpath) { - err = get_last_error() - } - return -} - - - -rename :: proc(old_path, new_path: string) -> (err: Error) { - runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD() - from := win32.utf8_to_wstring(old_path, context.temp_allocator) - to := win32.utf8_to_wstring(new_path, context.temp_allocator) - - if !win32.MoveFileExW(from, to, win32.MOVEFILE_REPLACE_EXISTING) { - err = get_last_error() - } - return -} - - -ftruncate :: proc(fd: Handle, length: i64) -> (err: Error) { - curr_off := seek(fd, 0, 1) or_return - defer seek(fd, curr_off, 0) - _= seek(fd, length, 0) or_return - ok := win32.SetEndOfFile(win32.HANDLE(fd)) - if !ok { - return get_last_error() - } - return nil -} - -truncate :: proc(path: string, length: i64) -> (err: Error) { - fd := open(path, O_WRONLY|O_CREATE, 0o666) or_return - defer close(fd) - return ftruncate(fd, length) -} - - -remove :: proc(name: string) -> Error { - p := win32.utf8_to_wstring(fix_long_path(name)) - err, err1: win32.DWORD - if !win32.DeleteFileW(p) { - err = win32.GetLastError() - } - if err == 0 { - return nil - } - if !win32.RemoveDirectoryW(p) { - err1 = win32.GetLastError() - } - if err1 == 0 { - return nil - } - - if err != err1 { - a := win32.GetFileAttributesW(p) - if a == ~u32(0) { - err = win32.GetLastError() - } else { - if a & win32.FILE_ATTRIBUTE_DIRECTORY != 0 { - err = err1 - } else if a & win32.FILE_ATTRIBUTE_READONLY != 0 { - if win32.SetFileAttributesW(p, a &~ win32.FILE_ATTRIBUTE_READONLY) { - err = 0 - if !win32.DeleteFileW(p) { - err = win32.GetLastError() - } - } - } - } - } - - return Platform_Error(err) -} - - -@(require_results) -pipe :: proc() -> (r, w: Handle, err: Error) { - sa: win32.SECURITY_ATTRIBUTES - sa.nLength = size_of(win32.SECURITY_ATTRIBUTES) - sa.bInheritHandle = true - if !win32.CreatePipe((^win32.HANDLE)(&r), (^win32.HANDLE)(&w), &sa, 0) { - err = get_last_error() - } - return -} - diff --git a/core/os/os2/dir_linux.odin b/core/os/os2/dir_linux.odin index 6a097e192..f26b4fc79 100644 --- a/core/os/os2/dir_linux.odin +++ b/core/os/os2/dir_linux.odin @@ -13,7 +13,7 @@ _read_directory_iterator :: proc(it: ^Read_Directory_Iterator) -> (fi: File_Info @(require_results) _read_directory_iterator_create :: proc(f: ^File) -> (Read_Directory_Iterator, Error) { - return {}, nil + return {}, .Unsupported } _read_directory_iterator_destroy :: proc(it: ^Read_Directory_Iterator) { diff --git a/core/os/os2/dir_windows.odin b/core/os/os2/dir_windows.odin index 09990aeec..f71e7e763 100644 --- a/core/os/os2/dir_windows.odin +++ b/core/os/os2/dir_windows.odin @@ -16,28 +16,25 @@ find_data_to_file_info :: proc(base_path: string, d: ^win32.WIN32_FIND_DATAW, al } path := concatenate({base_path, `\`, win32_utf16_to_utf8(d.cFileName[:], temp_allocator()) or_else ""}, allocator) or_return + handle := win32.HANDLE(_open_internal(path, {.Read}, 0o666) or_else 0) + defer win32.CloseHandle(handle) fi.fullpath = path fi.name = basename(path) fi.size = i64(d.nFileSizeHigh)<<32 + i64(d.nFileSizeLow) - fi.type, fi.mode = _file_type_mode_from_file_attributes(d.dwFileAttributes, nil, d.dwReserved0) + fi.type, fi.mode = _file_type_mode_from_file_attributes(d.dwFileAttributes, handle, d.dwReserved0) fi.creation_time = time.unix(0, win32.FILETIME_as_unix_nanoseconds(d.ftCreationTime)) fi.modification_time = time.unix(0, win32.FILETIME_as_unix_nanoseconds(d.ftLastWriteTime)) fi.access_time = time.unix(0, win32.FILETIME_as_unix_nanoseconds(d.ftLastAccessTime)) - - handle := win32.HANDLE(_open_internal(path, {.Read}, 0o666) or_else 0) - defer win32.CloseHandle(handle) - if file_id_info: win32.FILE_ID_INFO; handle != nil && win32.GetFileInformationByHandleEx(handle, .FileIdInfo, &file_id_info, size_of(file_id_info)) { #assert(size_of(fi.inode) == size_of(file_id_info.FileId)) #assert(size_of(fi.inode) == 16) runtime.mem_copy_non_overlapping(&fi.inode, &file_id_info.FileId, 16) } - return } @@ -137,5 +134,6 @@ _read_directory_iterator_destroy :: proc(it: ^Read_Directory_Iterator) { return } file_info_delete(it.impl.prev_fi, file_allocator()) + delete(it.impl.path, file_allocator()) win32.FindClose(it.impl.find_handle) } diff --git a/core/os/os2/stat_windows.odin b/core/os/os2/stat_windows.odin index 8ed2a6fed..0a019e9da 100644 --- a/core/os/os2/stat_windows.odin +++ b/core/os/os2/stat_windows.odin @@ -200,22 +200,21 @@ _file_type_mode_from_file_attributes :: proc(file_attributes: win32.DWORD, h: wi } else { mode |= 0o666 } + is_sym := false if file_attributes & win32.FILE_ATTRIBUTE_REPARSE_POINT == 0 { is_sym = false } else { is_sym = ReparseTag == win32.IO_REPARSE_TAG_SYMLINK || ReparseTag == win32.IO_REPARSE_TAG_MOUNT_POINT } + if is_sym { type = .Symlink - } else { - if file_attributes & win32.FILE_ATTRIBUTE_DIRECTORY != 0 { - type = .Directory - mode |= 0o111 - } - if h != nil { - type = file_type(h) - } + } else if file_attributes & win32.FILE_ATTRIBUTE_DIRECTORY != 0 { + type = .Directory + mode |= 0o111 + } else if h != nil { + type = file_type(h) } return } diff --git a/core/os/os_darwin.odin b/core/os/os_darwin.odin index 616e167a1..d4435ec63 100644 --- a/core/os/os_darwin.odin +++ b/core/os/os_darwin.odin @@ -679,7 +679,7 @@ get_last_error_string :: proc() -> string { @(require_results) -open :: proc(path: string, flags: int = O_RDWR, mode: int = 0) -> (handle: Handle, err: Error) { +open :: proc(path: string, flags: int = O_RDONLY, mode: int = 0) -> (handle: Handle, err: Error) { isDir := is_dir_path(path) flags := flags if isDir { diff --git a/core/os/os_windows.odin b/core/os/os_windows.odin index b0e166b82..58e5d1e6e 100644 --- a/core/os/os_windows.odin +++ b/core/os/os_windows.odin @@ -4,15 +4,13 @@ package os import win32 "core:sys/windows" import "base:runtime" import "base:intrinsics" +import "core:unicode/utf16" Handle :: distinct uintptr File_Time :: distinct u64 - INVALID_HANDLE :: ~Handle(0) - - O_RDONLY :: 0x00000 O_WRONLY :: 0x00001 O_RDWR :: 0x00002 @@ -278,3 +276,580 @@ is_windows_11 :: proc "contextless" () -> bool { osvi := get_windows_version_w() return (osvi.dwMajorVersion == 10 && osvi.dwMinorVersion == 0 && osvi.dwBuildNumber >= WINDOWS_11_BUILD_CUTOFF) } + +@(require_results) +is_path_separator :: proc(c: byte) -> bool { + return c == '/' || c == '\\' +} + +@(require_results) +open :: proc(path: string, mode: int = O_RDONLY, perm: int = 0) -> (Handle, Error) { + if len(path) == 0 { + return INVALID_HANDLE, General_Error.Not_Exist + } + + access: u32 + switch mode & (O_RDONLY|O_WRONLY|O_RDWR) { + case O_RDONLY: access = win32.FILE_GENERIC_READ + case O_WRONLY: access = win32.FILE_GENERIC_WRITE + case O_RDWR: access = win32.FILE_GENERIC_READ | win32.FILE_GENERIC_WRITE + } + + if mode&O_CREATE != 0 { + access |= win32.FILE_GENERIC_WRITE + } + if mode&O_APPEND != 0 { + access &~= win32.FILE_GENERIC_WRITE + access |= win32.FILE_APPEND_DATA + } + + share_mode := win32.FILE_SHARE_READ|win32.FILE_SHARE_WRITE + sa: ^win32.SECURITY_ATTRIBUTES = nil + sa_inherit := win32.SECURITY_ATTRIBUTES{nLength = size_of(win32.SECURITY_ATTRIBUTES), bInheritHandle = true} + if mode&O_CLOEXEC == 0 { + sa = &sa_inherit + } + + create_mode: u32 + switch { + case mode&(O_CREATE|O_EXCL) == (O_CREATE | O_EXCL): + create_mode = win32.CREATE_NEW + case mode&(O_CREATE|O_TRUNC) == (O_CREATE | O_TRUNC): + create_mode = win32.CREATE_ALWAYS + case mode&O_CREATE == O_CREATE: + create_mode = win32.OPEN_ALWAYS + case mode&O_TRUNC == O_TRUNC: + create_mode = win32.TRUNCATE_EXISTING + case: + create_mode = win32.OPEN_EXISTING + } + wide_path := win32.utf8_to_wstring(path) + handle := Handle(win32.CreateFileW(wide_path, access, share_mode, sa, create_mode, win32.FILE_ATTRIBUTE_NORMAL|win32.FILE_FLAG_BACKUP_SEMANTICS, nil)) + if handle != INVALID_HANDLE { + return handle, nil + } + + return INVALID_HANDLE, get_last_error() +} + +close :: proc(fd: Handle) -> Error { + if !win32.CloseHandle(win32.HANDLE(fd)) { + return get_last_error() + } + return nil +} + +flush :: proc(fd: Handle) -> (err: Error) { + if !win32.FlushFileBuffers(win32.HANDLE(fd)) { + err = get_last_error() + } + return +} + + + +write :: proc(fd: Handle, data: []byte) -> (int, Error) { + if len(data) == 0 { + return 0, nil + } + + single_write_length: win32.DWORD + total_write: i64 + length := i64(len(data)) + + for total_write < length { + remaining := length - total_write + to_write := win32.DWORD(min(i32(remaining), MAX_RW)) + + e := win32.WriteFile(win32.HANDLE(fd), &data[total_write], to_write, &single_write_length, nil) + if single_write_length <= 0 || !e { + return int(total_write), get_last_error() + } + total_write += i64(single_write_length) + } + return int(total_write), nil +} + +@(private="file", require_results) +read_console :: proc(handle: win32.HANDLE, b: []byte) -> (n: int, err: Error) { + if len(b) == 0 { + return 0, nil + } + + BUF_SIZE :: 386 + buf16: [BUF_SIZE]u16 + buf8: [4*BUF_SIZE]u8 + + for n < len(b) && err == nil { + min_read := max(len(b)/4, 1 if len(b) > 0 else 0) + max_read := u32(min(BUF_SIZE, min_read)) + if max_read == 0 { + break + } + + single_read_length: u32 + ok := win32.ReadConsoleW(handle, &buf16[0], max_read, &single_read_length, nil) + if !ok { + err = get_last_error() + } + + buf8_len := utf16.decode_to_utf8(buf8[:], buf16[:single_read_length]) + src := buf8[:buf8_len] + + ctrl_z := false + for i := 0; i < len(src) && n < len(b); i += 1 { + x := src[i] + if x == 0x1a { // ctrl-z + ctrl_z = true + break + } + b[n] = x + n += 1 + } + if ctrl_z || single_read_length < max_read { + break + } + + // NOTE(bill): if the last two values were a newline, then it is expected that + // this is the end of the input + if n >= 2 && single_read_length == max_read && string(b[n-2:n]) == "\r\n" { + break + } + + } + + return +} + +read :: proc(fd: Handle, data: []byte) -> (total_read: int, err: Error) { + if len(data) == 0 { + return 0, nil + } + + handle := win32.HANDLE(fd) + + m: u32 + is_console := win32.GetConsoleMode(handle, &m) + length := len(data) + + // NOTE(Jeroen): `length` can't be casted to win32.DWORD here because it'll overflow if > 4 GiB and return 0 if exactly that. + to_read := min(i64(length), MAX_RW) + + if is_console { + total_read, err = read_console(handle, data[total_read:][:to_read]) + if err != nil { + return total_read, err + } + } else { + // NOTE(Jeroen): So we cast it here *after* we've ensured that `to_read` is at most MAX_RW (1 GiB) + bytes_read: win32.DWORD + if e := win32.ReadFile(handle, &data[total_read], win32.DWORD(to_read), &bytes_read, nil); e { + // Successful read can mean two things, including EOF, see: + // https://learn.microsoft.com/en-us/windows/win32/fileio/testing-for-the-end-of-a-file + if bytes_read == 0 { + return 0, .EOF + } else { + return int(bytes_read), nil + } + } else { + return 0, get_last_error() + } + } + return total_read, nil +} + +seek :: proc(fd: Handle, offset: i64, whence: int) -> (i64, Error) { + w: u32 + switch whence { + case 0: w = win32.FILE_BEGIN + case 1: w = win32.FILE_CURRENT + case 2: w = win32.FILE_END + case: + return 0, .Invalid_Whence + } + hi := i32(offset>>32) + lo := i32(offset) + ft := win32.GetFileType(win32.HANDLE(fd)) + if ft == win32.FILE_TYPE_PIPE { + return 0, .File_Is_Pipe + } + + dw_ptr := win32.SetFilePointer(win32.HANDLE(fd), lo, &hi, w) + if dw_ptr == win32.INVALID_SET_FILE_POINTER { + err := get_last_error() + return 0, err + } + return i64(hi)<<32 + i64(dw_ptr), nil +} + +@(require_results) +file_size :: proc(fd: Handle) -> (i64, Error) { + length: win32.LARGE_INTEGER + err: Error + if !win32.GetFileSizeEx(win32.HANDLE(fd), &length) { + err = get_last_error() + } + return i64(length), err +} + + +@(private) +MAX_RW :: 1<<30 + +@(private) +pread :: proc(fd: Handle, data: []byte, offset: i64) -> (n: int, err: Error) { + curr_off := seek(fd, 0, 1) or_return + defer seek(fd, curr_off, 0) + + buf := data + if len(buf) > MAX_RW { + buf = buf[:MAX_RW] + } + + o := win32.OVERLAPPED{ + OffsetHigh = u32(offset>>32), + Offset = u32(offset), + } + + // TODO(bill): Determine the correct behaviour for consoles + + h := win32.HANDLE(fd) + done: win32.DWORD + e: Error + if !win32.ReadFile(h, raw_data(buf), u32(len(buf)), &done, &o) { + e = get_last_error() + done = 0 + } + return int(done), e +} +@(private) +pwrite :: proc(fd: Handle, data: []byte, offset: i64) -> (n: int, err: Error) { + curr_off := seek(fd, 0, 1) or_return + defer seek(fd, curr_off, 0) + + buf := data + if len(buf) > MAX_RW { + buf = buf[:MAX_RW] + } + + o := win32.OVERLAPPED{ + OffsetHigh = u32(offset>>32), + Offset = u32(offset), + } + + h := win32.HANDLE(fd) + done: win32.DWORD + e: Error + if !win32.WriteFile(h, raw_data(buf), u32(len(buf)), &done, &o) { + e = get_last_error() + done = 0 + } + return int(done), e +} + +/* +read_at returns n: 0, err: 0 on EOF +*/ +read_at :: proc(fd: Handle, data: []byte, offset: i64) -> (n: int, err: Error) { + if offset < 0 { + return 0, .Invalid_Offset + } + + b, offset := data, offset + for len(b) > 0 { + m, e := pread(fd, b, offset) + if e == ERROR_EOF { + err = nil + break + } + if e != nil { + err = e + break + } + n += m + b = b[m:] + offset += i64(m) + } + return +} + +write_at :: proc(fd: Handle, data: []byte, offset: i64) -> (n: int, err: Error) { + if offset < 0 { + return 0, .Invalid_Offset + } + + b, offset := data, offset + for len(b) > 0 { + m := pwrite(fd, b, offset) or_return + n += m + b = b[m:] + offset += i64(m) + } + return +} + + + +// NOTE(bill): Uses startup to initialize it +stdin := get_std_handle(uint(win32.STD_INPUT_HANDLE)) +stdout := get_std_handle(uint(win32.STD_OUTPUT_HANDLE)) +stderr := get_std_handle(uint(win32.STD_ERROR_HANDLE)) + + +@(require_results) +get_std_handle :: proc "contextless" (h: uint) -> Handle { + fd := win32.GetStdHandle(win32.DWORD(h)) + return Handle(fd) +} + + +exists :: proc(path: string) -> bool { + runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD() + wpath := win32.utf8_to_wstring(path, context.temp_allocator) + attribs := win32.GetFileAttributesW(wpath) + + return attribs != win32.INVALID_FILE_ATTRIBUTES +} + +@(require_results) +is_file :: proc(path: string) -> bool { + runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD() + wpath := win32.utf8_to_wstring(path, context.temp_allocator) + attribs := win32.GetFileAttributesW(wpath) + + if attribs != win32.INVALID_FILE_ATTRIBUTES { + return attribs & win32.FILE_ATTRIBUTE_DIRECTORY == 0 + } + return false +} + +@(require_results) +is_dir :: proc(path: string) -> bool { + runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD() + wpath := win32.utf8_to_wstring(path, context.temp_allocator) + attribs := win32.GetFileAttributesW(wpath) + + if attribs != win32.INVALID_FILE_ATTRIBUTES { + return attribs & win32.FILE_ATTRIBUTE_DIRECTORY != 0 + } + return false +} + +// NOTE(tetra): GetCurrentDirectory is not thread safe with SetCurrentDirectory and GetFullPathName +@private cwd_lock := win32.SRWLOCK{} // zero is initialized + +@(require_results) +get_current_directory :: proc(allocator := context.allocator) -> string { + win32.AcquireSRWLockExclusive(&cwd_lock) + + runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD(ignore = context.temp_allocator == allocator) + + sz_utf16 := win32.GetCurrentDirectoryW(0, nil) + dir_buf_wstr, _ := make([]u16, sz_utf16, context.temp_allocator) // the first time, it _includes_ the NUL. + + sz_utf16 = win32.GetCurrentDirectoryW(win32.DWORD(len(dir_buf_wstr)), raw_data(dir_buf_wstr)) + assert(int(sz_utf16)+1 == len(dir_buf_wstr)) // the second time, it _excludes_ the NUL. + + win32.ReleaseSRWLockExclusive(&cwd_lock) + + return win32.utf16_to_utf8(dir_buf_wstr, allocator) or_else "" +} + +set_current_directory :: proc(path: string) -> (err: Error) { + runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD() + wstr := win32.utf8_to_wstring(path, context.temp_allocator) + + win32.AcquireSRWLockExclusive(&cwd_lock) + + if !win32.SetCurrentDirectoryW(wstr) { + err = get_last_error() + } + + win32.ReleaseSRWLockExclusive(&cwd_lock) + + return +} +change_directory :: set_current_directory + +make_directory :: proc(path: string, mode: u32 = 0) -> (err: Error) { + runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD() + // Mode is unused on Windows, but is needed on *nix + wpath := win32.utf8_to_wstring(path, context.temp_allocator) + + if !win32.CreateDirectoryW(wpath, nil) { + err = get_last_error() + } + return +} + + +remove_directory :: proc(path: string) -> (err: Error) { + runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD() + wpath := win32.utf8_to_wstring(path, context.temp_allocator) + + if !win32.RemoveDirectoryW(wpath) { + err = get_last_error() + } + return +} + + + +@(private, require_results) +is_abs :: proc(path: string) -> bool { + if len(path) > 0 && path[0] == '/' { + return true + } + when ODIN_OS == .Windows { + if len(path) > 2 { + switch path[0] { + case 'A'..='Z', 'a'..='z': + return path[1] == ':' && is_path_separator(path[2]) + } + } + } + return false +} + +@(private, require_results) +fix_long_path :: proc(path: string) -> string { + if len(path) < 248 { + return path + } + + if len(path) >= 2 && path[:2] == `\\` { + return path + } + if !is_abs(path) { + return path + } + + prefix :: `\\?` + + path_buf, _ := make([]byte, len(prefix)+len(path)+len(`\`), context.temp_allocator) + copy(path_buf, prefix) + n := len(path) + r, w := 0, len(prefix) + for r < n { + switch { + case is_path_separator(path[r]): + r += 1 + case path[r] == '.' && (r+1 == n || is_path_separator(path[r+1])): + r += 1 + case r+1 < n && path[r] == '.' && path[r+1] == '.' && (r+2 == n || is_path_separator(path[r+2])): + return path + case: + path_buf[w] = '\\' + w += 1 + for ; r < n && !is_path_separator(path[r]); r += 1 { + path_buf[w] = path[r] + w += 1 + } + } + } + + if w == len(`\\?\c:`) { + path_buf[w] = '\\' + w += 1 + } + return string(path_buf[:w]) +} + + +link :: proc(old_name, new_name: string) -> (err: Error) { + runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD() + n := win32.utf8_to_wstring(fix_long_path(new_name)) + o := win32.utf8_to_wstring(fix_long_path(old_name)) + return Platform_Error(win32.CreateHardLinkW(n, o, nil)) +} + +unlink :: proc(path: string) -> (err: Error) { + runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD() + wpath := win32.utf8_to_wstring(path, context.temp_allocator) + + if !win32.DeleteFileW(wpath) { + err = get_last_error() + } + return +} + + + +rename :: proc(old_path, new_path: string) -> (err: Error) { + runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD() + from := win32.utf8_to_wstring(old_path, context.temp_allocator) + to := win32.utf8_to_wstring(new_path, context.temp_allocator) + + if !win32.MoveFileExW(from, to, win32.MOVEFILE_REPLACE_EXISTING) { + err = get_last_error() + } + return +} + + +ftruncate :: proc(fd: Handle, length: i64) -> (err: Error) { + curr_off := seek(fd, 0, 1) or_return + defer seek(fd, curr_off, 0) + _= seek(fd, length, 0) or_return + ok := win32.SetEndOfFile(win32.HANDLE(fd)) + if !ok { + return get_last_error() + } + return nil +} + +truncate :: proc(path: string, length: i64) -> (err: Error) { + fd := open(path, O_WRONLY|O_CREATE, 0o666) or_return + defer close(fd) + return ftruncate(fd, length) +} + + +remove :: proc(name: string) -> Error { + p := win32.utf8_to_wstring(fix_long_path(name)) + err, err1: win32.DWORD + if !win32.DeleteFileW(p) { + err = win32.GetLastError() + } + if err == 0 { + return nil + } + if !win32.RemoveDirectoryW(p) { + err1 = win32.GetLastError() + } + if err1 == 0 { + return nil + } + + if err != err1 { + a := win32.GetFileAttributesW(p) + if a == ~u32(0) { + err = win32.GetLastError() + } else { + if a & win32.FILE_ATTRIBUTE_DIRECTORY != 0 { + err = err1 + } else if a & win32.FILE_ATTRIBUTE_READONLY != 0 { + if win32.SetFileAttributesW(p, a &~ win32.FILE_ATTRIBUTE_READONLY) { + err = 0 + if !win32.DeleteFileW(p) { + err = win32.GetLastError() + } + } + } + } + } + + return Platform_Error(err) +} + + +@(require_results) +pipe :: proc() -> (r, w: Handle, err: Error) { + sa: win32.SECURITY_ATTRIBUTES + sa.nLength = size_of(win32.SECURITY_ATTRIBUTES) + sa.bInheritHandle = true + if !win32.CreatePipe((^win32.HANDLE)(&r), (^win32.HANDLE)(&w), &sa, 0) { + err = get_last_error() + } + return +} \ No newline at end of file diff --git a/core/reflect/reflect.odin b/core/reflect/reflect.odin index c04afb380..7f79acb77 100644 --- a/core/reflect/reflect.odin +++ b/core/reflect/reflect.odin @@ -31,8 +31,6 @@ Type_Info_Enum :: runtime.Type_Info_Enum Type_Info_Map :: runtime.Type_Info_Map Type_Info_Bit_Set :: runtime.Type_Info_Bit_Set Type_Info_Simd_Vector :: runtime.Type_Info_Simd_Vector -Type_Info_Relative_Pointer :: runtime.Type_Info_Relative_Pointer -Type_Info_Relative_Multi_Pointer :: runtime.Type_Info_Relative_Multi_Pointer Type_Info_Matrix :: runtime.Type_Info_Matrix Type_Info_Soa_Pointer :: runtime.Type_Info_Soa_Pointer Type_Info_Bit_Field :: runtime.Type_Info_Bit_Field @@ -67,8 +65,6 @@ Type_Kind :: enum { Map, Bit_Set, Simd_Vector, - Relative_Pointer, - Relative_Multi_Pointer, Matrix, Soa_Pointer, Bit_Field, @@ -80,35 +76,33 @@ type_kind :: proc(T: typeid) -> Type_Kind { ti := type_info_of(T) if ti != nil { switch _ in ti.variant { - case Type_Info_Named: return .Named - case Type_Info_Integer: return .Integer - case Type_Info_Rune: return .Rune - case Type_Info_Float: return .Float - case Type_Info_Complex: return .Complex - case Type_Info_Quaternion: return .Quaternion - case Type_Info_String: return .String - case Type_Info_Boolean: return .Boolean - case Type_Info_Any: return .Any - case Type_Info_Type_Id: return .Type_Id - case Type_Info_Pointer: return .Pointer - case Type_Info_Multi_Pointer: return .Multi_Pointer - case Type_Info_Procedure: return .Procedure - case Type_Info_Array: return .Array - case Type_Info_Enumerated_Array: return .Enumerated_Array - case Type_Info_Dynamic_Array: return .Dynamic_Array - case Type_Info_Slice: return .Slice - case Type_Info_Parameters: return .Tuple - case Type_Info_Struct: return .Struct - case Type_Info_Union: return .Union - case Type_Info_Enum: return .Enum - case Type_Info_Map: return .Map - case Type_Info_Bit_Set: return .Bit_Set - case Type_Info_Simd_Vector: return .Simd_Vector - case Type_Info_Relative_Pointer: return .Relative_Pointer - case Type_Info_Relative_Multi_Pointer: return .Relative_Multi_Pointer - case Type_Info_Matrix: return .Matrix - case Type_Info_Soa_Pointer: return .Soa_Pointer - case Type_Info_Bit_Field: return .Bit_Field + case Type_Info_Named: return .Named + case Type_Info_Integer: return .Integer + case Type_Info_Rune: return .Rune + case Type_Info_Float: return .Float + case Type_Info_Complex: return .Complex + case Type_Info_Quaternion: return .Quaternion + case Type_Info_String: return .String + case Type_Info_Boolean: return .Boolean + case Type_Info_Any: return .Any + case Type_Info_Type_Id: return .Type_Id + case Type_Info_Pointer: return .Pointer + case Type_Info_Multi_Pointer: return .Multi_Pointer + case Type_Info_Procedure: return .Procedure + case Type_Info_Array: return .Array + case Type_Info_Enumerated_Array: return .Enumerated_Array + case Type_Info_Dynamic_Array: return .Dynamic_Array + case Type_Info_Slice: return .Slice + case Type_Info_Parameters: return .Tuple + case Type_Info_Struct: return .Struct + case Type_Info_Union: return .Union + case Type_Info_Enum: return .Enum + case Type_Info_Map: return .Map + case Type_Info_Bit_Set: return .Bit_Set + case Type_Info_Simd_Vector: return .Simd_Vector + case Type_Info_Matrix: return .Matrix + case Type_Info_Soa_Pointer: return .Soa_Pointer + case Type_Info_Bit_Field: return .Bit_Field } } @@ -723,6 +717,27 @@ enum_name_from_value_any :: proc(value: any) -> (name: string, ok: bool) { return } +/* +Returns whether the value given has a defined name in the enum type. +*/ +@(require_results) +enum_value_has_name :: proc(value: $T) -> bool where intrinsics.type_is_enum(T) { + when len(T) == cap(T) { + return value >= min(T) && value <= max(T) + } else { + if value < min(T) || value > max(T) { + return false + } + + for valid_value in T { + if valid_value == value { + return true + } + } + + return false + } +} @@ -1467,21 +1482,6 @@ as_string :: proc(a: any) -> (value: string, valid: bool) { return } -@(require_results) -relative_pointer_to_absolute :: proc(a: any) -> rawptr { - if a == nil { return nil } - a := a - ti := runtime.type_info_core(type_info_of(a.id)) - a.id = ti.id - - #partial switch info in ti.variant { - case Type_Info_Relative_Pointer: - return relative_pointer_to_absolute_raw(a.data, info.base_integer.id) - } - return nil -} - - @(require_results) relative_pointer_to_absolute_raw :: proc(data: rawptr, base_integer_id: typeid) -> rawptr { _handle :: proc(ptr: ^$T) -> rawptr where intrinsics.type_is_integer(T) { @@ -1543,10 +1543,6 @@ as_pointer :: proc(a: any) -> (value: rawptr, valid: bool) { case cstring: value = rawptr(v) case: valid = false } - - case Type_Info_Relative_Pointer: - valid = true - value = relative_pointer_to_absolute_raw(a.data, info.base_integer.id) } return @@ -1656,8 +1652,6 @@ equal :: proc(a, b: any, including_indirect_array_recursion := false, recursion_ Type_Info_Bit_Set, Type_Info_Enum, Type_Info_Simd_Vector, - Type_Info_Relative_Pointer, - Type_Info_Relative_Multi_Pointer, Type_Info_Soa_Pointer, Type_Info_Matrix: return runtime.memory_compare(a.data, b.data, t.size) == 0 diff --git a/core/reflect/types.odin b/core/reflect/types.odin index 4f0674dc8..cb31a27e2 100644 --- a/core/reflect/types.odin +++ b/core/reflect/types.odin @@ -158,14 +158,6 @@ are_types_identical :: proc(a, b: ^Type_Info) -> bool { case Type_Info_Simd_Vector: y := b.variant.(Type_Info_Simd_Vector) or_return return x.count == y.count && x.elem == y.elem - - case Type_Info_Relative_Pointer: - y := b.variant.(Type_Info_Relative_Pointer) or_return - return x.base_integer == y.base_integer && x.pointer == y.pointer - - case Type_Info_Relative_Multi_Pointer: - y := b.variant.(Type_Info_Relative_Multi_Pointer) or_return - return x.base_integer == y.base_integer && x.pointer == y.pointer case Type_Info_Matrix: y := b.variant.(Type_Info_Matrix) or_return @@ -392,18 +384,6 @@ is_simd_vector :: proc(info: ^Type_Info) -> bool { _, ok := type_info_base(info).variant.(Type_Info_Simd_Vector) return ok } -@(require_results) -is_relative_pointer :: proc(info: ^Type_Info) -> bool { - if info == nil { return false } - _, ok := type_info_base(info).variant.(Type_Info_Relative_Pointer) - return ok -} -@(require_results) -is_relative_multi_pointer :: proc(info: ^Type_Info) -> bool { - if info == nil { return false } - _, ok := type_info_base(info).variant.(Type_Info_Relative_Multi_Pointer) - return ok -} @(require_results) @@ -736,18 +716,6 @@ write_type_writer :: #force_no_inline proc(w: io.Writer, ti: ^Type_Info, n_writt io.write_i64(w, i64(info.count), 10, &n) or_return io.write_byte(w, ']', &n) or_return write_type(w, info.elem, &n) or_return - - case Type_Info_Relative_Pointer: - io.write_string(w, "#relative(", &n) or_return - write_type(w, info.base_integer, &n) or_return - io.write_string(w, ") ", &n) or_return - write_type(w, info.pointer, &n) or_return - - case Type_Info_Relative_Multi_Pointer: - io.write_string(w, "#relative(", &n) or_return - write_type(w, info.base_integer, &n) or_return - io.write_string(w, ") ", &n) or_return - write_type(w, info.pointer, &n) or_return case Type_Info_Matrix: if info.layout == .Row_Major { diff --git a/core/slice/slice.odin b/core/slice/slice.odin index cd12a8c70..c31edf281 100644 --- a/core/slice/slice.odin +++ b/core/slice/slice.odin @@ -471,6 +471,12 @@ is_empty :: proc(a: $T/[]$E) -> bool { return len(a) == 0 } +// Gets the byte size of the backing data +@(require_results) +size :: proc "contextless" (a: $T/[]$E) -> int { + return len(a) * size_of(E) +} + @(require_results) diff --git a/core/strings/strings.odin b/core/strings/strings.odin index af93ff33c..c014d2b2b 100644 --- a/core/strings/strings.odin +++ b/core/strings/strings.odin @@ -1872,7 +1872,8 @@ index_multi :: proc(s: string, substrs: []string) -> (idx: int, width: int) { lowest_index := len(s) found := false for substr in substrs { - if i := index(s, substr); i >= 0 { + haystack := s[:min(len(s), lowest_index + len(substr))] + if i := index(haystack, substr); i >= 0 { if i < lowest_index { lowest_index = i width = len(substr) diff --git a/core/sync/futex_wasm.odin b/core/sync/futex_wasm.odin index 0f9659a02..16e69ca74 100644 --- a/core/sync/futex_wasm.odin +++ b/core/sync/futex_wasm.odin @@ -12,8 +12,8 @@ _futex_wait :: proc "contextless" (f: ^Futex, expected: u32) -> bool { when !intrinsics.has_target_feature("atomics") { panic_contextless("usage of `core:sync` requires the `-target-feature:\"atomics\"` or a `-microarch` that supports it") } else { - s := intrinsics.wasm_memory_atomic_wait32((^u32)(f), expected, -1) - return s != 0 + _ = intrinsics.wasm_memory_atomic_wait32((^u32)(f), expected, -1) + return true } } @@ -22,7 +22,7 @@ _futex_wait_with_timeout :: proc "contextless" (f: ^Futex, expected: u32, durati panic_contextless("usage of `core:sync` requires the `-target-feature:\"atomics\"` or a `-microarch` that supports it") } else { s := intrinsics.wasm_memory_atomic_wait32((^u32)(f), expected, i64(duration)) - return s != 0 + return s != 2 } } @@ -30,12 +30,7 @@ _futex_signal :: proc "contextless" (f: ^Futex) { when !intrinsics.has_target_feature("atomics") { panic_contextless("usage of `core:sync` requires the `-target-feature:\"atomics\"` or a `-microarch` that supports it") } else { - loop: for { - s := intrinsics.wasm_memory_atomic_notify32((^u32)(f), 1) - if s >= 1 { - return - } - } + _ = intrinsics.wasm_memory_atomic_notify32((^u32)(f), 1) } } @@ -43,12 +38,7 @@ _futex_broadcast :: proc "contextless" (f: ^Futex) { when !intrinsics.has_target_feature("atomics") { panic_contextless("usage of `core:sync` requires the `-target-feature:\"atomics\"` or a `-microarch` that supports it") } else { - loop: for { - s := intrinsics.wasm_memory_atomic_notify32((^u32)(f), ~u32(0)) - if s >= 0 { - return - } - } + _ = intrinsics.wasm_memory_atomic_notify32((^u32)(f), max(u32)) } } diff --git a/core/sys/darwin/Foundation/NSApplication.odin b/core/sys/darwin/Foundation/NSApplication.odin index 7191f6d07..254da75ad 100644 --- a/core/sys/darwin/Foundation/NSApplication.odin +++ b/core/sys/darwin/Foundation/NSApplication.odin @@ -108,6 +108,16 @@ Application_setMainMenu :: proc "c" (self: ^Application, menu: ^Menu) { msgSend(nil, self, "setMainMenu:", menu) } +@(objc_type=Application, objc_name="mainWindow") +Application_mainWindow :: proc "c" (self: ^Application) -> ^Window { + return msgSend(^Window, self, "mainWindow") +} + +@(objc_type=Application, objc_name="keyWindow") +Application_keyWindow :: proc "c" (self: ^Application) -> ^Window { + return msgSend(^Window, self, "keyWindow") +} + @(objc_type=Application, objc_name="windows") Application_windows :: proc "c" (self: ^Application) -> ^Array { return msgSend(^Array, self, "windows") diff --git a/core/sys/darwin/Foundation/NSObjectProtocol.odin b/core/sys/darwin/Foundation/NSObjectProtocol.odin new file mode 100644 index 000000000..99d942579 --- /dev/null +++ b/core/sys/darwin/Foundation/NSObjectProtocol.odin @@ -0,0 +1,5 @@ +package objc_Foundation + +@(objc_class="NSObjectProtocol") +ObjectProtocol :: struct {using _: Object} +// TODO: implement NSObjectProtocol diff --git a/core/sys/darwin/Foundation/NSProcessInfo.odin b/core/sys/darwin/Foundation/NSProcessInfo.odin new file mode 100644 index 000000000..e070bf8e2 --- /dev/null +++ b/core/sys/darwin/Foundation/NSProcessInfo.odin @@ -0,0 +1,203 @@ +package objc_Foundation + +import "base:intrinsics" + +import "core:c" + +@(objc_class="NSProcessInfo") +ProcessInfo :: struct {using _: Object} + +// Getting the Process Information Agent + +@(objc_type=ProcessInfo, objc_name="processInfo", objc_is_class_method=true) +ProcessInfo_processInfo :: proc "c" () -> ^ProcessInfo { + return msgSend(^ProcessInfo, ProcessInfo, "processInfo") +} + +// Accessing Process Information + +@(objc_type=ProcessInfo, objc_name="arguments") +ProcessInfo_arguments :: proc "c" (self: ^ProcessInfo) -> ^Array { + return msgSend(^Array, self, "arguments") +} + +@(objc_type=ProcessInfo, objc_name="environment") +ProcessInfo_environment :: proc "c" (self: ^ProcessInfo) -> ^Dictionary { + return msgSend(^Dictionary, self, "environment") +} + +@(objc_type=ProcessInfo, objc_name="globallyUniqueString") +ProcessInfo_globallyUniqueString :: proc "c" (self: ^ProcessInfo) -> ^String { + return msgSend(^String, self, "globallyUniqueString") +} + +@(objc_type=ProcessInfo, objc_name="isMacCatalystApp") +ProcessInfo_isMacCatalystApp :: proc "c" (self: ^ProcessInfo) -> bool { + return msgSend(bool, self, "isMacCatalystApp") +} + +@(objc_type=ProcessInfo, objc_name="isiOSAppOnMac") +ProcessInfo_isiOSAppOnMac :: proc "c" (self: ^ProcessInfo) -> bool { + return msgSend(bool, self, "isiOSAppOnMac") +} + +@(objc_type=ProcessInfo, objc_name="processIdentifier") +ProcessInfo_processIdentifier :: proc "c" (self: ^ProcessInfo) -> c.int { + return msgSend(c.int, self, "processIdentifier") +} + +@(objc_type=ProcessInfo, objc_name="processName") +ProcessInfo_processName :: proc "c" (self: ^ProcessInfo) -> ^String { + return msgSend(^String, self, "processName") +} + +// Accessing User Information + +@(objc_type=ProcessInfo, objc_name="userName") +ProcessInfo_userName :: proc "c" (self: ^ProcessInfo) -> ^String { + return msgSend(^String, self, "userName") +} + +@(objc_type=ProcessInfo, objc_name="fullUserName") +ProcessInfo_fullUserName :: proc "c" (self: ^ProcessInfo) -> ^String { + return msgSend(^String, self, "fullUserName") +} + +// Sudden Application Termination + +@(objc_type=ProcessInfo, objc_name="disableSuddenTermination") +ProcessInfo_disableSuddenTermination :: proc "c" (self: ^ProcessInfo) { + msgSend(nil, self, "disableSuddenTermination") +} + +@(objc_type=ProcessInfo, objc_name="enableSuddenTermination") +ProcessInfo_enableSuddenTermination :: proc "c" (self: ^ProcessInfo) { + msgSend(nil, self, "enableSuddenTermination") +} + +// Controlling Automatic Termination + +@(objc_type=ProcessInfo, objc_name="disableAutomaticTermination") +ProcessInfo_disableAutomaticTermination :: proc "c" (self: ^ProcessInfo, reason: ^String) { + msgSend(nil, self, "disableAutomaticTermination:", reason) +} + +@(objc_type=ProcessInfo, objc_name="enableAutomaticTermination") +ProcessInfo_enableAutomaticTermination :: proc "c" (self: ^ProcessInfo, reason: ^String) { + msgSend(nil, self, "enableAutomaticTermination:", reason) +} + +@(objc_type=ProcessInfo, objc_name="automaticTerminationSupportEnabled") +ProcessInfo_automaticTerminationSupportEnabled :: proc "c" (self: ^ProcessInfo) -> bool { + return msgSend(bool, self, "automaticTerminationSupportEnabled") +} + +@(objc_type=ProcessInfo, objc_name="setAutomaticTerminationSupportEnabled") +ProcessInfo_setAutomaticTerminationSupportEnabled :: proc "c" (self: ^ProcessInfo, automaticTerminationSupportEnabled: bool) { + msgSend(nil, self, "setAutomaticTerminationSupportEnabled:", automaticTerminationSupportEnabled) +} + +// Getting Host Information + +@(objc_type=ProcessInfo, objc_name="hostName") +ProcessInfo_hostName :: proc "c" (self: ^ProcessInfo) -> ^String { + return msgSend(^String, self, "hostName") +} + +@(objc_type=ProcessInfo, objc_name="operatingSystemVersionString") +ProcessInfo_operatingSystemVersionString :: proc "c" (self: ^ProcessInfo) -> ^String { + return msgSend(^String, self, "operatingSystemVersionString") +} + +@(objc_type=ProcessInfo, objc_name="operatingSystemVersion") +ProcessInfo_operatingSystemVersion :: proc "c" (self: ^ProcessInfo) -> OperatingSystemVersion { + return msgSend(OperatingSystemVersion, self, "operatingSystemVersion") +} + +@(objc_type=ProcessInfo, objc_name="isOperatingSystemAtLeastVersion") +ProcessInfo_isOperatingSystemAtLeastVersion :: proc "c" (self: ^ProcessInfo, version: OperatingSystemVersion) -> bool { + return msgSend(bool, self, "isOperatingSystemAtLeastVersion:", version) +} + +// Getting Computer Information + +@(objc_type=ProcessInfo, objc_name="processorCount") +ProcessInfo_processorCount :: proc "c" (self: ^ProcessInfo) -> UInteger { + return msgSend(UInteger, self, "processorCount") +} + +@(objc_type=ProcessInfo, objc_name="activeProcessorCount") +ProcessInfo_activeProcessorCount :: proc "c" (self: ^ProcessInfo) -> UInteger { + return msgSend(UInteger, self, "activeProcessorCount") +} + +@(objc_type=ProcessInfo, objc_name="physicalMemory") +ProcessInfo_physicalMemory :: proc "c" (self: ^ProcessInfo) -> c.ulonglong { + return msgSend(c.ulonglong, self, "physicalMemory") +} + +@(objc_type=ProcessInfo, objc_name="systemUptime") +ProcessInfo_systemUptime :: proc "c" (self: ^ProcessInfo) -> TimeInterval { + return msgSend(TimeInterval, self, "systemUptime") +} + +// Managing Activities + +@(private) +log2 :: intrinsics.constant_log2 + +ActivityOptionsBits :: enum u64 { + IdleDisplaySleepDisabled = log2(1099511627776), // Require the screen to stay powered on. + IdleSystemSleepDisabled = log2(1048576), // Prevent idle sleep. + SuddenTerminationDisabled = log2(16384), // Prevent sudden termination. + AutomaticTerminationDisabled = log2(32768), // Prevent automatic termination. + AnimationTrackingEnabled = log2(35184372088832), // Track activity with an animation signpost interval. + TrackingEnabled = log2(70368744177664), // Track activity with a signpost interval. + UserInitiated = log2(16777215), // Performing a user-requested action. + UserInitiatedAllowingIdleSystemSleep = log2(15728639), // Performing a user-requested action, but the system can sleep on idle. + Background = log2(255), // Initiated some kind of work, but not as the direct result of a user request. + LatencyCritical = log2(1095216660480), // Requires the highest amount of timer and I/O precision available. + UserInteractive = log2(1095233437695), // Responding to user interaction. +} +ActivityOptions :: bit_set[ActivityOptionsBits; u64] + +@(objc_type=ProcessInfo, objc_name="beginActivityWithOptions") +ProcessInfo_beginActivityWithOptions :: proc "c" (self: ^ProcessInfo, options: ActivityOptions, reason: ^String) -> ^ObjectProtocol { + return msgSend(^ObjectProtocol, self, "beginActivityWithOptions:reason:", options, reason) +} + +@(objc_type=ProcessInfo, objc_name="endActivity") +ProcessInfo_endActivity :: proc "c" (self: ^ProcessInfo, activity: ^ObjectProtocol) { + msgSend(nil, self, "endActivity:", activity) +} + +@(objc_type=ProcessInfo, objc_name="performActivityWithOptions") +ProcessInfo_performActivityWithOptions :: proc "c" (self: ^ProcessInfo, options: ActivityOptions, reason: ^String, block: proc "c" ()) { + msgSend(nil, self, "performActivityWithOptions:reason:usingBlock:", options, reason, block) +} + +@(objc_type=ProcessInfo, objc_name="performExpiringActivityWithReason") +ProcessInfo_performExpiringActivityWithReason :: proc "c" (self: ^ProcessInfo, reason: ^String, block: proc "c" (expired: bool)) { + msgSend(nil, self, "performExpiringActivityWithReason:usingBlock:", reason, block) +} + +// Getting the Thermal State + +ProcessInfoThermalState :: enum c.long { + Nominal, + Fair, + Serious, + Critical, +} + +@(objc_type=ProcessInfo, objc_name="thermalState") +ProcessInfo_thermalState :: proc "c" (self: ^ProcessInfo) -> ProcessInfoThermalState { + return msgSend(ProcessInfoThermalState, self, "thermalState") +} + +// Determining Whether Low Power Mode is Enabled + +@(objc_type=ProcessInfo, objc_name="isLowPowerModeEnabled") +ProcessInfo_isLowPowerModeEnabled :: proc "c" (self: ^ProcessInfo) -> bool { + return msgSend(bool, self, "isLowPowerModeEnabled") +} diff --git a/core/sys/darwin/Foundation/NSTypes.odin b/core/sys/darwin/Foundation/NSTypes.odin index fbd883a8f..822a07ab1 100644 --- a/core/sys/darwin/Foundation/NSTypes.odin +++ b/core/sys/darwin/Foundation/NSTypes.odin @@ -20,7 +20,7 @@ BOOL :: bool // TODO(bill): should this be `distinct`? YES :: true NO :: false -OperatingSystemVersion :: struct #packed { +OperatingSystemVersion :: struct #align(8) { majorVersion: Integer, minorVersion: Integer, patchVersion: Integer, @@ -58,4 +58,4 @@ when size_of(Float) == 8 { } else { _POINT_ENCODING :: "{NSPoint=ff}" _SIZE_ENCODING :: "{NSSize=ff}" -} \ No newline at end of file +} diff --git a/core/sys/info/doc.odin b/core/sys/info/doc.odin index b5cd62d81..2fd34b864 100644 --- a/core/sys/info/doc.odin +++ b/core/sys/info/doc.odin @@ -4,7 +4,7 @@ Made available under Odin's BSD-3 license. List of contributors: Jeroen van Rijn: Initial implementation. - Laytan: ARM and RISC-V CPU feature detection. + Laytan: ARM and RISC-V CPU feature detection, iOS/macOS platform overhaul. */ /* diff --git a/core/sys/info/platform_darwin.odin b/core/sys/info/platform_darwin.odin index 56cd883d3..7dc49bcd1 100644 --- a/core/sys/info/platform_darwin.odin +++ b/core/sys/info/platform_darwin.odin @@ -1,581 +1,101 @@ package sysinfo -import sys "core:sys/unix" -import "core:strconv" -import "core:strings" -import "base:runtime" +import "core:strconv" +import "core:strings" +import "core:sys/unix" +import NS "core:sys/darwin/Foundation" @(private) version_string_buf: [1024]u8 @(init, private) -init_os_version :: proc () { - os_version.platform = .MacOS +init_platform :: proc() { + ws :: strings.write_string + wi :: strings.write_int - // Start building display version b := strings.builder_from_bytes(version_string_buf[:]) - mib := []i32{sys.CTL_KERN, sys.KERN_OSVERSION} - build_buf: [12]u8 + version: NS.OperatingSystemVersion + { + NS.scoped_autoreleasepool() - ok := sys.sysctl(mib, &build_buf) - if !ok { - strings.write_string(&b, "macOS Unknown") - os_version.as_string = strings.to_string(b) - return + info := NS.ProcessInfo.processInfo() + version = info->operatingSystemVersion() + mem := info->physicalMemory() + + ram.total_ram = int(mem) } - build := string(cstring(&build_buf[0])) + macos_version = {int(version.majorVersion), int(version.minorVersion), int(version.patchVersion)} - // Do we have an exact match? - match: Darwin_Match - rel, exact := macos_release_map[build] - - if exact { - match = .Exact + when ODIN_PLATFORM_SUBTARGET == .iOS { + os_version.platform = .iOS + ws(&b, "iOS") } else { + os_version.platform = .MacOS + switch version.majorVersion { + case 15: ws(&b, "macOS Sequoia") + case 14: ws(&b, "macOS Sonoma") + case 13: ws(&b, "macOS Ventura") + case 12: ws(&b, "macOS Monterey") + case 11: ws(&b, "macOS Big Sur") + case 10: + switch version.minorVersion { + case 15: ws(&b, "macOS Catalina") + case 14: ws(&b, "macOS Mojave") + case 13: ws(&b, "macOS High Sierra") + case 12: ws(&b, "macOS Sierra") + case 11: ws(&b, "OS X El Capitan") + case 10: ws(&b, "OS X Yosemite") + case: + // `ProcessInfo.operatingSystemVersion` is 10.10 and up. + unreachable() + } + case: + // New version not yet added here. + assert(version.majorVersion > 15) + ws(&b, "macOS Unknown") + } + } + + ws(&b, " ") + wi(&b, int(version.majorVersion)) + ws(&b, ".") + wi(&b, int(version.minorVersion)) + ws(&b, ".") + wi(&b, int(version.patchVersion)) + + { + build_buf: [12]u8 + mib := []i32{unix.CTL_KERN, unix.KERN_OSVERSION} + ok := unix.sysctl(mib, &build_buf) + build := string(cstring(raw_data(build_buf[:]))) if ok else "Unknown" + + ws(&b, " (build ") + + build_start := len(b.buf) + ws(&b, build) + os_version.version = string(b.buf[build_start:][:len(build)]) + } + + { // Match on XNU kernel version - mib = []i32{sys.CTL_KERN, sys.KERN_OSRELEASE} version_bits: [12]u8 // enough for 999.999.999\x00 - have_kernel_version := sys.sysctl(mib, &version_bits) + mib := []i32{unix.CTL_KERN, unix.KERN_OSRELEASE} + ok := unix.sysctl(mib, &version_bits) + kernel := string(cstring(raw_data(version_bits[:]))) if ok else "Unknown" - major_ok, minor_ok, patch_ok: bool + major, _, tail := strings.partition(kernel, ".") + minor, _, patch := strings.partition(tail, ".") - tmp := runtime.default_temp_allocator_temp_begin() + os_version.major, _ = strconv.parse_int(major, 10) + os_version.minor, _ = strconv.parse_int(minor, 10) + os_version.patch, _ = strconv.parse_int(patch, 10) - triplet := strings.split(string(cstring(&version_bits[0])), ".", context.temp_allocator) - if len(triplet) != 3 { - have_kernel_version = false - } else { - rel.darwin.x, major_ok = strconv.parse_int(triplet[0]) - rel.darwin.y, minor_ok = strconv.parse_int(triplet[1]) - rel.darwin.z, patch_ok = strconv.parse_int(triplet[2]) - - if !(major_ok && minor_ok && patch_ok) { - have_kernel_version = false - } - } - - runtime.default_temp_allocator_temp_end(tmp) - - if !have_kernel_version { - // We don't know the kernel version, but we do know the build - strings.write_string(&b, "macOS Unknown (build ") - l := strings.builder_len(b) - strings.write_string(&b, build) - os_version.version = strings.to_string(b)[l:] - strings.write_rune(&b, ')') - os_version.as_string = strings.to_string(b) - return - } - rel, match = map_darwin_kernel_version_to_macos_release(build, rel.darwin) + ws(&b, ", kernel ") + ws(&b, kernel) + ws(&b, ")") } - os_version.major = rel.darwin.x - os_version.minor = rel.darwin.y - os_version.patch = rel.darwin.z - - macos_version = transmute(Version)rel.release.version - - strings.write_string(&b, rel.os_name) - if match == .Exact || match == .Nearest { - strings.write_rune(&b, ' ') - strings.write_string(&b, rel.release.name) - strings.write_rune(&b, ' ') - strings.write_int(&b, rel.release.version.x) - if rel.release.version.y > 0 || rel.release.version.z > 0 { - strings.write_rune(&b, '.') - strings.write_int(&b, rel.release.version.y) - } - if rel.release.version.z > 0 { - strings.write_rune(&b, '.') - strings.write_int(&b, rel.release.version.z) - } - if match == .Nearest { - strings.write_rune(&b, '?') - } - } else { - strings.write_string(&b, " Unknown") - } - - strings.write_string(&b, " (build ") - l := strings.builder_len(b) - strings.write_string(&b, build) - os_version.version = strings.to_string(b)[l:] - - strings.write_string(&b, ", kernel ") - strings.write_int(&b, rel.darwin.x) - strings.write_rune(&b, '.') - strings.write_int(&b, rel.darwin.y) - strings.write_rune(&b, '.') - strings.write_int(&b, rel.darwin.z) - strings.write_rune(&b, ')') - - os_version.as_string = strings.to_string(b) -} - -@(init, private) -init_ram :: proc() { - // Retrieve RAM info using `sysctl` - - mib := []i32{sys.CTL_HW, sys.HW_MEMSIZE} - mem_size: u64 - if sys.sysctl(mib, &mem_size) { - ram.total_ram = int(mem_size) - } -} - -@(private) -Darwin_To_Release :: struct { - darwin: [3]int, // Darwin kernel triplet - os_name: string, // OS X, MacOS - release: struct { - name: string, // Monterey, Mojave, etc. - version: [3]int, // 12.4, etc. - }, -} - -// Important: Order from lowest to highest kernel version -@(private) -macos_release_map: map[string]Darwin_To_Release = { - // MacOS Tiger - "8A428" = {{8, 0, 0}, "macOS", {"Tiger", {10, 4, 0}}}, - "8A432" = {{8, 0, 0}, "macOS", {"Tiger", {10, 4, 0}}}, - "8B15" = {{8, 1, 0}, "macOS", {"Tiger", {10, 4, 1}}}, - "8B17" = {{8, 1, 0}, "macOS", {"Tiger", {10, 4, 1}}}, - "8C46" = {{8, 2, 0}, "macOS", {"Tiger", {10, 4, 2}}}, - "8C47" = {{8, 2, 0}, "macOS", {"Tiger", {10, 4, 2}}}, - "8E102" = {{8, 2, 0}, "macOS", {"Tiger", {10, 4, 2}}}, - "8E45" = {{8, 2, 0}, "macOS", {"Tiger", {10, 4, 2}}}, - "8E90" = {{8, 2, 0}, "macOS", {"Tiger", {10, 4, 2}}}, - "8F46" = {{8, 3, 0}, "macOS", {"Tiger", {10, 4, 3}}}, - "8G32" = {{8, 4, 0}, "macOS", {"Tiger", {10, 4, 4}}}, - "8G1165" = {{8, 4, 0}, "macOS", {"Tiger", {10, 4, 4}}}, - "8H14" = {{8, 5, 0}, "macOS", {"Tiger", {10, 4, 5}}}, - "8G1454" = {{8, 5, 0}, "macOS", {"Tiger", {10, 4, 5}}}, - "8I127" = {{8, 6, 0}, "macOS", {"Tiger", {10, 4, 6}}}, - "8I1119" = {{8, 6, 0}, "macOS", {"Tiger", {10, 4, 6}}}, - "8J135" = {{8, 7, 0}, "macOS", {"Tiger", {10, 4, 7}}}, - "8J2135a" = {{8, 7, 0}, "macOS", {"Tiger", {10, 4, 7}}}, - "8K1079" = {{8, 7, 0}, "macOS", {"Tiger", {10, 4, 7}}}, - "8N5107" = {{8, 7, 0}, "macOS", {"Tiger", {10, 4, 7}}}, - "8L127" = {{8, 8, 0}, "macOS", {"Tiger", {10, 4, 8}}}, - "8L2127" = {{8, 8, 0}, "macOS", {"Tiger", {10, 4, 8}}}, - "8P135" = {{8, 9, 0}, "macOS", {"Tiger", {10, 4, 9}}}, - "8P2137" = {{8, 9, 0}, "macOS", {"Tiger", {10, 4, 9}}}, - "8R218" = {{8, 10, 0}, "macOS", {"Tiger", {10, 4, 10}}}, - "8R2218" = {{8, 10, 0}, "macOS", {"Tiger", {10, 4, 10}}}, - "8R2232" = {{8, 10, 0}, "macOS", {"Tiger", {10, 4, 10}}}, - "8S165" = {{8, 11, 0}, "macOS", {"Tiger", {10, 4, 11}}}, - "8S2167" = {{8, 11, 0}, "macOS", {"Tiger", {10, 4, 11}}}, - - // MacOS Leopard - "9A581" = {{9, 0, 0}, "macOS", {"Leopard", {10, 5, 0}}}, - "9B18" = {{9, 1, 0}, "macOS", {"Leopard", {10, 5, 1}}}, - "9B2117" = {{9, 1, 1}, "macOS", {"Leopard", {10, 5, 1}}}, - "9C31" = {{9, 2, 0}, "macOS", {"Leopard", {10, 5, 2}}}, - "9C7010" = {{9, 2, 0}, "macOS", {"Leopard", {10, 5, 2}}}, - "9D34" = {{9, 3, 0}, "macOS", {"Leopard", {10, 5, 3}}}, - "9E17" = {{9, 4, 0}, "macOS", {"Leopard", {10, 5, 4}}}, - "9F33" = {{9, 5, 0}, "macOS", {"Leopard", {10, 5, 5}}}, - "9G55" = {{9, 6, 0}, "macOS", {"Leopard", {10, 5, 6}}}, - "9G66" = {{9, 6, 0}, "macOS", {"Leopard", {10, 5, 6}}}, - "9G71" = {{9, 6, 0}, "macOS", {"Leopard", {10, 5, 6}}}, - "9J61" = {{9, 7, 0}, "macOS", {"Leopard", {10, 5, 7}}}, - "9L30" = {{9, 8, 0}, "macOS", {"Leopard", {10, 5, 8}}}, - "9L34" = {{9, 8, 0}, "macOS", {"Leopard", {10, 5, 8}}}, - - // MacOS Snow Leopard - "10A432" = {{10, 0, 0}, "macOS", {"Snow Leopard", {10, 6, 0}}}, - "10A433" = {{10, 0, 0}, "macOS", {"Snow Leopard", {10, 6, 0}}}, - "10B504" = {{10, 1, 0}, "macOS", {"Snow Leopard", {10, 6, 1}}}, - "10C540" = {{10, 2, 0}, "macOS", {"Snow Leopard", {10, 6, 2}}}, - "10D573" = {{10, 3, 0}, "macOS", {"Snow Leopard", {10, 6, 3}}}, - "10D575" = {{10, 3, 0}, "macOS", {"Snow Leopard", {10, 6, 3}}}, - "10D578" = {{10, 3, 0}, "macOS", {"Snow Leopard", {10, 6, 3}}}, - "10F569" = {{10, 4, 0}, "macOS", {"Snow Leopard", {10, 6, 4}}}, - "10H574" = {{10, 5, 0}, "macOS", {"Snow Leopard", {10, 6, 5}}}, - "10J567" = {{10, 6, 0}, "macOS", {"Snow Leopard", {10, 6, 6}}}, - "10J869" = {{10, 7, 0}, "macOS", {"Snow Leopard", {10, 6, 7}}}, - "10J3250" = {{10, 7, 0}, "macOS", {"Snow Leopard", {10, 6, 7}}}, - "10J4138" = {{10, 7, 0}, "macOS", {"Snow Leopard", {10, 6, 7}}}, - "10K540" = {{10, 8, 0}, "macOS", {"Snow Leopard", {10, 6, 8}}}, - "10K549" = {{10, 8, 0}, "macOS", {"Snow Leopard", {10, 6, 8}}}, - - // MacOS Lion - "11A511" = {{11, 0, 0}, "macOS", {"Lion", {10, 7, 0}}}, - "11A511s" = {{11, 0, 0}, "macOS", {"Lion", {10, 7, 0}}}, - "11A2061" = {{11, 0, 2}, "macOS", {"Lion", {10, 7, 0}}}, - "11A2063" = {{11, 0, 2}, "macOS", {"Lion", {10, 7, 0}}}, - "11B26" = {{11, 1, 0}, "macOS", {"Lion", {10, 7, 1}}}, - "11B2118" = {{11, 1, 0}, "macOS", {"Lion", {10, 7, 1}}}, - "11C74" = {{11, 2, 0}, "macOS", {"Lion", {10, 7, 2}}}, - "11D50" = {{11, 3, 0}, "macOS", {"Lion", {10, 7, 3}}}, - "11E53" = {{11, 4, 0}, "macOS", {"Lion", {10, 7, 4}}}, - "11G56" = {{11, 4, 2}, "macOS", {"Lion", {10, 7, 5}}}, - "11G63" = {{11, 4, 2}, "macOS", {"Lion", {10, 7, 5}}}, - - // MacOS Mountain Lion - "12A269" = {{12, 0, 0}, "macOS", {"Mountain Lion", {10, 8, 0}}}, - "12B19" = {{12, 1, 0}, "macOS", {"Mountain Lion", {10, 8, 1}}}, - "12C54" = {{12, 2, 0}, "macOS", {"Mountain Lion", {10, 8, 2}}}, - "12C60" = {{12, 2, 0}, "macOS", {"Mountain Lion", {10, 8, 2}}}, - "12C2034" = {{12, 2, 0}, "macOS", {"Mountain Lion", {10, 8, 2}}}, - "12C3104" = {{12, 2, 0}, "macOS", {"Mountain Lion", {10, 8, 2}}}, - "12D78" = {{12, 3, 0}, "macOS", {"Mountain Lion", {10, 8, 3}}}, - "12E55" = {{12, 4, 0}, "macOS", {"Mountain Lion", {10, 8, 4}}}, - "12E3067" = {{12, 4, 0}, "macOS", {"Mountain Lion", {10, 8, 4}}}, - "12E4022" = {{12, 4, 0}, "macOS", {"Mountain Lion", {10, 8, 4}}}, - "12F37" = {{12, 5, 0}, "macOS", {"Mountain Lion", {10, 8, 5}}}, - "12F45" = {{12, 5, 0}, "macOS", {"Mountain Lion", {10, 8, 5}}}, - "12F2501" = {{12, 5, 0}, "macOS", {"Mountain Lion", {10, 8, 5}}}, - "12F2518" = {{12, 5, 0}, "macOS", {"Mountain Lion", {10, 8, 5}}}, - "12F2542" = {{12, 5, 0}, "macOS", {"Mountain Lion", {10, 8, 5}}}, - "12F2560" = {{12, 5, 0}, "macOS", {"Mountain Lion", {10, 8, 5}}}, - - // MacOS Mavericks - "13A603" = {{13, 0, 0}, "macOS", {"Mavericks", {10, 9, 0}}}, - "13B42" = {{13, 0, 0}, "macOS", {"Mavericks", {10, 9, 1}}}, - "13C64" = {{13, 1, 0}, "macOS", {"Mavericks", {10, 9, 2}}}, - "13C1021" = {{13, 1, 0}, "macOS", {"Mavericks", {10, 9, 2}}}, - "13D65" = {{13, 2, 0}, "macOS", {"Mavericks", {10, 9, 3}}}, - "13E28" = {{13, 3, 0}, "macOS", {"Mavericks", {10, 9, 4}}}, - "13F34" = {{13, 4, 0}, "macOS", {"Mavericks", {10, 9, 5}}}, - "13F1066" = {{13, 4, 0}, "macOS", {"Mavericks", {10, 9, 5}}}, - "13F1077" = {{13, 4, 0}, "macOS", {"Mavericks", {10, 9, 5}}}, - "13F1096" = {{13, 4, 0}, "macOS", {"Mavericks", {10, 9, 5}}}, - "13F1112" = {{13, 4, 0}, "macOS", {"Mavericks", {10, 9, 5}}}, - "13F1134" = {{13, 4, 0}, "macOS", {"Mavericks", {10, 9, 5}}}, - "13F1507" = {{13, 4, 0}, "macOS", {"Mavericks", {10, 9, 5}}}, - "13F1603" = {{13, 4, 0}, "macOS", {"Mavericks", {10, 9, 5}}}, - "13F1712" = {{13, 4, 0}, "macOS", {"Mavericks", {10, 9, 5}}}, - "13F1808" = {{13, 4, 0}, "macOS", {"Mavericks", {10, 9, 5}}}, - "13F1911" = {{13, 4, 0}, "macOS", {"Mavericks", {10, 9, 5}}}, - - // MacOS Yosemite - "14A389" = {{14, 0, 0}, "macOS", {"Yosemite", {10, 10, 0}}}, - "14B25" = {{14, 0, 0}, "macOS", {"Yosemite", {10, 10, 1}}}, - "14C109" = {{14, 1, 0}, "macOS", {"Yosemite", {10, 10, 2}}}, - "14C1510" = {{14, 1, 0}, "macOS", {"Yosemite", {10, 10, 2}}}, - "14C2043" = {{14, 1, 0}, "macOS", {"Yosemite", {10, 10, 2}}}, - "14C1514" = {{14, 1, 0}, "macOS", {"Yosemite", {10, 10, 2}}}, - "14C2513" = {{14, 1, 0}, "macOS", {"Yosemite", {10, 10, 2}}}, - "14D131" = {{14, 3, 0}, "macOS", {"Yosemite", {10, 10, 3}}}, - "14D136" = {{14, 3, 0}, "macOS", {"Yosemite", {10, 10, 3}}}, - "14E46" = {{14, 4, 0}, "macOS", {"Yosemite", {10, 10, 4}}}, - "14F27" = {{14, 5, 0}, "macOS", {"Yosemite", {10, 10, 5}}}, - "14F1021" = {{14, 5, 0}, "macOS", {"Yosemite", {10, 10, 5}}}, - "14F1505" = {{14, 5, 0}, "macOS", {"Yosemite", {10, 10, 5}}}, - "14F1509" = {{14, 5, 0}, "macOS", {"Yosemite", {10, 10, 5}}}, - "14F1605" = {{14, 5, 0}, "macOS", {"Yosemite", {10, 10, 5}}}, - "14F1713" = {{14, 5, 0}, "macOS", {"Yosemite", {10, 10, 5}}}, - "14F1808" = {{14, 5, 0}, "macOS", {"Yosemite", {10, 10, 5}}}, - "14F1909" = {{14, 5, 0}, "macOS", {"Yosemite", {10, 10, 5}}}, - "14F1912" = {{14, 5, 0}, "macOS", {"Yosemite", {10, 10, 5}}}, - "14F2009" = {{14, 5, 0}, "macOS", {"Yosemite", {10, 10, 5}}}, - "14F2109" = {{14, 5, 0}, "macOS", {"Yosemite", {10, 10, 5}}}, - "14F2315" = {{14, 5, 0}, "macOS", {"Yosemite", {10, 10, 5}}}, - "14F2411" = {{14, 5, 0}, "macOS", {"Yosemite", {10, 10, 5}}}, - "14F2511" = {{14, 5, 0}, "macOS", {"Yosemite", {10, 10, 5}}}, - - // MacOS El Capitan - "15A284" = {{15, 0, 0}, "macOS", {"El Capitan", {10, 11, 0}}}, - "15B42" = {{15, 0, 0}, "macOS", {"El Capitan", {10, 11, 1}}}, - "15C50" = {{15, 2, 0}, "macOS", {"El Capitan", {10, 11, 2}}}, - "15D21" = {{15, 3, 0}, "macOS", {"El Capitan", {10, 11, 3}}}, - "15E65" = {{15, 4, 0}, "macOS", {"El Capitan", {10, 11, 4}}}, - "15F34" = {{15, 5, 0}, "macOS", {"El Capitan", {10, 11, 5}}}, - "15G31" = {{15, 6, 0}, "macOS", {"El Capitan", {10, 11, 6}}}, - "15G1004" = {{15, 6, 0}, "macOS", {"El Capitan", {10, 11, 6}}}, - "15G1011" = {{15, 6, 0}, "macOS", {"El Capitan", {10, 11, 6}}}, - "15G1108" = {{15, 6, 0}, "macOS", {"El Capitan", {10, 11, 6}}}, - "15G1212" = {{15, 6, 0}, "macOS", {"El Capitan", {10, 11, 6}}}, - "15G1217" = {{15, 6, 0}, "macOS", {"El Capitan", {10, 11, 6}}}, - "15G1421" = {{15, 6, 0}, "macOS", {"El Capitan", {10, 11, 6}}}, - "15G1510" = {{15, 6, 0}, "macOS", {"El Capitan", {10, 11, 6}}}, - "15G1611" = {{15, 6, 0}, "macOS", {"El Capitan", {10, 11, 6}}}, - "15G17023" = {{15, 6, 0}, "macOS", {"El Capitan", {10, 11, 6}}}, - "15G18013" = {{15, 6, 0}, "macOS", {"El Capitan", {10, 11, 6}}}, - "15G19009" = {{15, 6, 0}, "macOS", {"El Capitan", {10, 11, 6}}}, - "15G20015" = {{15, 6, 0}, "macOS", {"El Capitan", {10, 11, 6}}}, - "15G21013" = {{15, 6, 0}, "macOS", {"El Capitan", {10, 11, 6}}}, - "15G22010" = {{15, 6, 0}, "macOS", {"El Capitan", {10, 11, 6}}}, - - // MacOS Sierra - "16A323" = {{16, 0, 0}, "macOS", {"Sierra", {10, 12, 0}}}, - "16B2555" = {{16, 1, 0}, "macOS", {"Sierra", {10, 12, 1}}}, - "16B2657" = {{16, 1, 0}, "macOS", {"Sierra", {10, 12, 1}}}, - "16C67" = {{16, 3, 0}, "macOS", {"Sierra", {10, 12, 2}}}, - "16C68" = {{16, 3, 0}, "macOS", {"Sierra", {10, 12, 2}}}, - "16D32" = {{16, 4, 0}, "macOS", {"Sierra", {10, 12, 3}}}, - "16E195" = {{16, 5, 0}, "macOS", {"Sierra", {10, 12, 4}}}, - "16F73" = {{16, 6, 0}, "macOS", {"Sierra", {10, 12, 5}}}, - "16F2073" = {{16, 6, 0}, "macOS", {"Sierra", {10, 12, 5}}}, - "16G29" = {{16, 7, 0}, "macOS", {"Sierra", {10, 12, 6}}}, - "16G1036" = {{16, 7, 0}, "macOS", {"Sierra", {10, 12, 6}}}, - "16G1114" = {{16, 7, 0}, "macOS", {"Sierra", {10, 12, 6}}}, - "16G1212" = {{16, 7, 0}, "macOS", {"Sierra", {10, 12, 6}}}, - "16G1314" = {{16, 7, 0}, "macOS", {"Sierra", {10, 12, 6}}}, - "16G1408" = {{16, 7, 0}, "macOS", {"Sierra", {10, 12, 6}}}, - "16G1510" = {{16, 7, 0}, "macOS", {"Sierra", {10, 12, 6}}}, - "16G1618" = {{16, 7, 0}, "macOS", {"Sierra", {10, 12, 6}}}, - "16G1710" = {{16, 7, 0}, "macOS", {"Sierra", {10, 12, 6}}}, - "16G1815" = {{16, 7, 0}, "macOS", {"Sierra", {10, 12, 6}}}, - "16G1917" = {{16, 7, 0}, "macOS", {"Sierra", {10, 12, 6}}}, - "16G1918" = {{16, 7, 0}, "macOS", {"Sierra", {10, 12, 6}}}, - "16G2016" = {{16, 7, 0}, "macOS", {"Sierra", {10, 12, 6}}}, - "16G2127" = {{16, 7, 0}, "macOS", {"Sierra", {10, 12, 6}}}, - "16G2128" = {{16, 7, 0}, "macOS", {"Sierra", {10, 12, 6}}}, - "16G2136" = {{16, 7, 0}, "macOS", {"Sierra", {10, 12, 6}}}, - - // MacOS High Sierra - "17A365" = {{17, 0, 0}, "macOS", {"High Sierra", {10, 13, 0}}}, - "17A405" = {{17, 0, 0}, "macOS", {"High Sierra", {10, 13, 0}}}, - "17B48" = {{17, 2, 0}, "macOS", {"High Sierra", {10, 13, 1}}}, - "17B1002" = {{17, 2, 0}, "macOS", {"High Sierra", {10, 13, 1}}}, - "17B1003" = {{17, 2, 0}, "macOS", {"High Sierra", {10, 13, 1}}}, - "17C88" = {{17, 3, 0}, "macOS", {"High Sierra", {10, 13, 2}}}, - "17C89" = {{17, 3, 0}, "macOS", {"High Sierra", {10, 13, 2}}}, - "17C205" = {{17, 3, 0}, "macOS", {"High Sierra", {10, 13, 2}}}, - "17C2205" = {{17, 3, 0}, "macOS", {"High Sierra", {10, 13, 2}}}, - "17D47" = {{17, 4, 0}, "macOS", {"High Sierra", {10, 13, 3}}}, - "17D2047" = {{17, 4, 0}, "macOS", {"High Sierra", {10, 13, 3}}}, - "17D102" = {{17, 4, 0}, "macOS", {"High Sierra", {10, 13, 3}}}, - "17D2102" = {{17, 4, 0}, "macOS", {"High Sierra", {10, 13, 3}}}, - "17E199" = {{17, 5, 0}, "macOS", {"High Sierra", {10, 13, 4}}}, - "17E202" = {{17, 5, 0}, "macOS", {"High Sierra", {10, 13, 4}}}, - "17F77" = {{17, 6, 0}, "macOS", {"High Sierra", {10, 13, 5}}}, - "17G65" = {{17, 7, 0}, "macOS", {"High Sierra", {10, 13, 6}}}, - "17G2208" = {{17, 7, 0}, "macOS", {"High Sierra", {10, 13, 6}}}, - "17G2307" = {{17, 7, 0}, "macOS", {"High Sierra", {10, 13, 6}}}, - "17G3025" = {{17, 7, 0}, "macOS", {"High Sierra", {10, 13, 6}}}, - "17G4015" = {{17, 7, 0}, "macOS", {"High Sierra", {10, 13, 6}}}, - "17G5019" = {{17, 7, 0}, "macOS", {"High Sierra", {10, 13, 6}}}, - "17G6029" = {{17, 7, 0}, "macOS", {"High Sierra", {10, 13, 6}}}, - "17G6030" = {{17, 7, 0}, "macOS", {"High Sierra", {10, 13, 6}}}, - "17G7024" = {{17, 7, 0}, "macOS", {"High Sierra", {10, 13, 6}}}, - "17G8029" = {{17, 7, 0}, "macOS", {"High Sierra", {10, 13, 6}}}, - "17G8030" = {{17, 7, 0}, "macOS", {"High Sierra", {10, 13, 6}}}, - "17G8037" = {{17, 7, 0}, "macOS", {"High Sierra", {10, 13, 6}}}, - "17G9016" = {{17, 7, 0}, "macOS", {"High Sierra", {10, 13, 6}}}, - "17G10021" = {{17, 7, 0}, "macOS", {"High Sierra", {10, 13, 6}}}, - "17G11023" = {{17, 7, 0}, "macOS", {"High Sierra", {10, 13, 6}}}, - "17G12034" = {{17, 7, 0}, "macOS", {"High Sierra", {10, 13, 6}}}, - "17G13033" = {{17, 7, 0}, "macOS", {"High Sierra", {10, 13, 6}}}, - "17G13035" = {{17, 7, 0}, "macOS", {"High Sierra", {10, 13, 6}}}, - "17G14019" = {{17, 7, 0}, "macOS", {"High Sierra", {10, 13, 6}}}, - "17G14033" = {{17, 7, 0}, "macOS", {"High Sierra", {10, 13, 6}}}, - "17G14042" = {{17, 7, 0}, "macOS", {"High Sierra", {10, 13, 6}}}, - - // MacOS Mojave - "18A391" = {{18, 0, 0}, "macOS", {"Mojave", {10, 14, 0}}}, - "18B75" = {{18, 2, 0}, "macOS", {"Mojave", {10, 14, 1}}}, - "18B2107" = {{18, 2, 0}, "macOS", {"Mojave", {10, 14, 1}}}, - "18B3094" = {{18, 2, 0}, "macOS", {"Mojave", {10, 14, 1}}}, - "18C54" = {{18, 2, 0}, "macOS", {"Mojave", {10, 14, 2}}}, - "18D42" = {{18, 2, 0}, "macOS", {"Mojave", {10, 14, 3}}}, - "18D43" = {{18, 2, 0}, "macOS", {"Mojave", {10, 14, 3}}}, - "18D109" = {{18, 2, 0}, "macOS", {"Mojave", {10, 14, 3}}}, - "18E226" = {{18, 5, 0}, "macOS", {"Mojave", {10, 14, 4}}}, - "18E227" = {{18, 5, 0}, "macOS", {"Mojave", {10, 14, 4}}}, - "18F132" = {{18, 6, 0}, "macOS", {"Mojave", {10, 14, 5}}}, - "18G84" = {{18, 7, 0}, "macOS", {"Mojave", {10, 14, 6}}}, - "18G87" = {{18, 7, 0}, "macOS", {"Mojave", {10, 14, 6}}}, - "18G95" = {{18, 7, 0}, "macOS", {"Mojave", {10, 14, 6}}}, - "18G103" = {{18, 7, 0}, "macOS", {"Mojave", {10, 14, 6}}}, - "18G1012" = {{18, 7, 0}, "macOS", {"Mojave", {10, 14, 6}}}, - "18G2022" = {{18, 7, 0}, "macOS", {"Mojave", {10, 14, 6}}}, - "18G3020" = {{18, 7, 0}, "macOS", {"Mojave", {10, 14, 6}}}, - "18G4032" = {{18, 7, 0}, "macOS", {"Mojave", {10, 14, 6}}}, - "18G5033" = {{18, 7, 0}, "macOS", {"Mojave", {10, 14, 6}}}, - "18G6020" = {{18, 7, 0}, "macOS", {"Mojave", {10, 14, 6}}}, - "18G6032" = {{18, 7, 0}, "macOS", {"Mojave", {10, 14, 6}}}, - "18G6042" = {{18, 7, 0}, "macOS", {"Mojave", {10, 14, 6}}}, - "18G7016" = {{18, 7, 0}, "macOS", {"Mojave", {10, 14, 6}}}, - "18G8012" = {{18, 7, 0}, "macOS", {"Mojave", {10, 14, 6}}}, - "18G8022" = {{18, 7, 0}, "macOS", {"Mojave", {10, 14, 6}}}, - "18G9028" = {{18, 7, 0}, "macOS", {"Mojave", {10, 14, 6}}}, - "18G9216" = {{18, 7, 0}, "macOS", {"Mojave", {10, 14, 6}}}, - "18G9323" = {{18, 7, 0}, "macOS", {"Mojave", {10, 14, 6}}}, - - // MacOS Catalina - "19A583" = {{19, 0, 0}, "macOS", {"Catalina", {10, 15, 0}}}, - "19A602" = {{19, 0, 0}, "macOS", {"Catalina", {10, 15, 0}}}, - "19A603" = {{19, 0, 0}, "macOS", {"Catalina", {10, 15, 0}}}, - "19B88" = {{19, 0, 0}, "macOS", {"Catalina", {10, 15, 1}}}, - "19C57" = {{19, 2, 0}, "macOS", {"Catalina", {10, 15, 2}}}, - "19C58" = {{19, 2, 0}, "macOS", {"Catalina", {10, 15, 2}}}, - "19D76" = {{19, 3, 0}, "macOS", {"Catalina", {10, 15, 3}}}, - "19E266" = {{19, 4, 0}, "macOS", {"Catalina", {10, 15, 4}}}, - "19E287" = {{19, 4, 0}, "macOS", {"Catalina", {10, 15, 4}}}, - "19F96" = {{19, 5, 0}, "macOS", {"Catalina", {10, 15, 5}}}, - "19F101" = {{19, 5, 0}, "macOS", {"Catalina", {10, 15, 5}}}, - "19G73" = {{19, 6, 0}, "macOS", {"Catalina", {10, 15, 6}}}, - "19G2021" = {{19, 6, 0}, "macOS", {"Catalina", {10, 15, 6}}}, - "19H2" = {{19, 6, 0}, "macOS", {"Catalina", {10, 15, 7}}}, - "19H4" = {{19, 6, 0}, "macOS", {"Catalina", {10, 15, 7}}}, - "19H15" = {{19, 6, 0}, "macOS", {"Catalina", {10, 15, 7}}}, - "19H114" = {{19, 6, 0}, "macOS", {"Catalina", {10, 15, 7}}}, - "19H512" = {{19, 6, 0}, "macOS", {"Catalina", {10, 15, 7}}}, - "19H524" = {{19, 6, 0}, "macOS", {"Catalina", {10, 15, 7}}}, - "19H1030" = {{19, 6, 0}, "macOS", {"Catalina", {10, 15, 7}}}, - "19H1217" = {{19, 6, 0}, "macOS", {"Catalina", {10, 15, 7}}}, - "19H1323" = {{19, 6, 0}, "macOS", {"Catalina", {10, 15, 7}}}, - "19H1417" = {{19, 6, 0}, "macOS", {"Catalina", {10, 15, 7}}}, - "19H1419" = {{19, 6, 0}, "macOS", {"Catalina", {10, 15, 7}}}, - "19H1519" = {{19, 6, 0}, "macOS", {"Catalina", {10, 15, 7}}}, - "19H1615" = {{19, 6, 0}, "macOS", {"Catalina", {10, 15, 7}}}, - "19H1713" = {{19, 6, 0}, "macOS", {"Catalina", {10, 15, 7}}}, - "19H1715" = {{19, 6, 0}, "macOS", {"Catalina", {10, 15, 7}}}, - "19H1824" = {{19, 6, 0}, "macOS", {"Catalina", {10, 15, 7}}}, - "19H1922" = {{19, 6, 0}, "macOS", {"Catalina", {10, 15, 7}}}, - "19H2026" = {{19, 6, 0}, "macOS", {"Catalina", {10, 15, 7}}}, - - // MacOS Big Sur - "20A2411" = {{20, 1, 0}, "macOS", {"Big Sur", {11, 0, 0}}}, - "20B29" = {{20, 1, 0}, "macOS", {"Big Sur", {11, 0, 1}}}, - "20B50" = {{20, 1, 0}, "macOS", {"Big Sur", {11, 0, 1}}}, - "20C69" = {{20, 2, 0}, "macOS", {"Big Sur", {11, 1, 0}}}, - "20D64" = {{20, 3, 0}, "macOS", {"Big Sur", {11, 2, 0}}}, - "20D74" = {{20, 3, 0}, "macOS", {"Big Sur", {11, 2, 1}}}, - "20D75" = {{20, 3, 0}, "macOS", {"Big Sur", {11, 2, 1}}}, - "20D80" = {{20, 3, 0}, "macOS", {"Big Sur", {11, 2, 2}}}, - "20D91" = {{20, 3, 0}, "macOS", {"Big Sur", {11, 2, 3}}}, - "20E232" = {{20, 4, 0}, "macOS", {"Big Sur", {11, 3, 0}}}, - "20E241" = {{20, 4, 0}, "macOS", {"Big Sur", {11, 3, 1}}}, - "20F71" = {{20, 5, 0}, "macOS", {"Big Sur", {11, 4, 0}}}, - "20G71" = {{20, 6, 0}, "macOS", {"Big Sur", {11, 5, 0}}}, - "20G80" = {{20, 6, 0}, "macOS", {"Big Sur", {11, 5, 1}}}, - "20G95" = {{20, 6, 0}, "macOS", {"Big Sur", {11, 5, 2}}}, - "20G165" = {{20, 6, 0}, "macOS", {"Big Sur", {11, 6, 0}}}, - "20G224" = {{20, 6, 0}, "macOS", {"Big Sur", {11, 6, 1}}}, - "20G314" = {{20, 6, 0}, "macOS", {"Big Sur", {11, 6, 2}}}, - "20G415" = {{20, 6, 0}, "macOS", {"Big Sur", {11, 6, 3}}}, - "20G417" = {{20, 6, 0}, "macOS", {"Big Sur", {11, 6, 4}}}, - "20G527" = {{20, 6, 0}, "macOS", {"Big Sur", {11, 6, 5}}}, - "20G624" = {{20, 6, 0}, "macOS", {"Big Sur", {11, 6, 6}}}, - "20G630" = {{20, 6, 0}, "macOS", {"Big Sur", {11, 6, 7}}}, - "20G730" = {{20, 6, 0}, "macOS", {"Big Sur", {11, 6, 8}}}, - "20G817" = {{20, 6, 0}, "macOS", {"Big Sur", {11, 7, 0}}}, - "20G918" = {{20, 6, 0}, "macOS", {"Big Sur", {11, 7, 1}}}, - "20G1020" = {{20, 6, 0}, "macOS", {"Big Sur", {11, 7, 2}}}, - "20G1116" = {{20, 6, 0}, "macOS", {"Big Sur", {11, 7, 3}}}, - "20G1120" = {{20, 6, 0}, "macOS", {"Big Sur", {11, 7, 4}}}, - "20G1225" = {{20, 6, 0}, "macOS", {"Big Sur", {11, 7, 5}}}, - "20G1231" = {{20, 6, 0}, "macOS", {"Big Sur", {11, 7, 6}}}, - "20G1345" = {{20, 6, 0}, "macOS", {"Big Sur", {11, 7, 7}}}, - "20G1351" = {{20, 6, 0}, "macOS", {"Big Sur", {11, 7, 8}}}, - "20G1426" = {{20, 6, 0}, "macOS", {"Big Sur", {11, 7, 9}}}, - "20G1427" = {{20, 6, 0}, "macOS", {"Big Sur", {11, 7, 10}}}, - - // MacOS Monterey - "21A344" = {{21, 0, 1}, "macOS", {"Monterey", {12, 0, 0}}}, - "21A559" = {{21, 1, 0}, "macOS", {"Monterey", {12, 0, 1}}}, - "21C52" = {{21, 2, 0}, "macOS", {"Monterey", {12, 1, 0}}}, - "21D49" = {{21, 3, 0}, "macOS", {"Monterey", {12, 2, 0}}}, - "21D62" = {{21, 3, 0}, "macOS", {"Monterey", {12, 2, 1}}}, - "21E230" = {{21, 4, 0}, "macOS", {"Monterey", {12, 3, 0}}}, - "21E258" = {{21, 4, 0}, "macOS", {"Monterey", {12, 3, 1}}}, - "21F79" = {{21, 5, 0}, "macOS", {"Monterey", {12, 4, 0}}}, - "21F2081" = {{21, 5, 0}, "macOS", {"Monterey", {12, 4, 0}}}, - "21F2092" = {{21, 5, 0}, "macOS", {"Monterey", {12, 4, 0}}}, - "21G72" = {{21, 6, 0}, "macOS", {"Monterey", {12, 5, 0}}}, - "21G83" = {{21, 6, 0}, "macOS", {"Monterey", {12, 5, 1}}}, - "21G115" = {{21, 6, 0}, "macOS", {"Monterey", {12, 6, 0}}}, - "21G217" = {{21, 6, 0}, "macOS", {"Monterey", {12, 6, 1}}}, - "21G320" = {{21, 6, 0}, "macOS", {"Monterey", {12, 6, 2}}}, - "21G419" = {{21, 6, 0}, "macOS", {"Monterey", {12, 6, 3}}}, - "21G526" = {{21, 6, 0}, "macOS", {"Monterey", {12, 6, 4}}}, - "21G531" = {{21, 6, 0}, "macOS", {"Monterey", {12, 6, 5}}}, - "21G646" = {{21, 6, 0}, "macOS", {"Monterey", {12, 6, 6}}}, - "21G651" = {{21, 6, 0}, "macOS", {"Monterey", {12, 6, 7}}}, - "21G725" = {{21, 6, 0}, "macOS", {"Monterey", {12, 6, 8}}}, - "21G726" = {{21, 6, 0}, "macOS", {"Monterey", {12, 6, 9}}}, - "21G816" = {{21, 6, 0}, "macOS", {"Monterey", {12, 7, 0}}}, - "21G920" = {{21, 6, 0}, "macOS", {"Monterey", {12, 7, 1}}}, - "21G1974" = {{21, 6, 0}, "macOS", {"Monterey", {12, 7, 2}}}, - - // MacOS Ventura - "22A380" = {{22, 1, 0}, "macOS", {"Ventura", {13, 0, 0}}}, - "22A400" = {{22, 1, 0}, "macOS", {"Ventura", {13, 0, 1}}}, - "22C65" = {{22, 2, 0}, "macOS", {"Ventura", {13, 1, 0}}}, - "22D49" = {{22, 3, 0}, "macOS", {"Ventura", {13, 2, 0}}}, - "22D68" = {{22, 3, 0}, "macOS", {"Ventura", {13, 2, 1}}}, - "22E252" = {{22, 4, 0}, "macOS", {"Ventura", {13, 3, 0}}}, - "22E261" = {{22, 4, 0}, "macOS", {"Ventura", {13, 3, 1}}}, - "22F66" = {{22, 5, 0}, "macOS", {"Ventura", {13, 4, 0}}}, - "22F82" = {{22, 5, 0}, "macOS", {"Ventura", {13, 4, 1}}}, - "22E772610a" = {{22, 5, 0}, "macOS", {"Ventura", {13, 4, 1}}}, - "22F770820d" = {{22, 5, 0}, "macOS", {"Ventura", {13, 4, 1}}}, - "22G74" = {{22, 6, 0}, "macOS", {"Ventura", {13, 5, 0}}}, - "22G90" = {{22, 6, 0}, "macOS", {"Ventura", {13, 5, 1}}}, - "22G91" = {{22, 6, 0}, "macOS", {"Ventura", {13, 5, 2}}}, - "22G120" = {{22, 6, 0}, "macOS", {"Ventura", {13, 6, 0}}}, - "22G313" = {{22, 6, 0}, "macOS", {"Ventura", {13, 6, 1}}}, - "22G320" = {{22, 6, 0}, "macOS", {"Ventura", {13, 6, 2}}}, - - // MacOS Sonoma - "23A344" = {{23, 0, 0}, "macOS", {"Sonoma", {14, 0, 0}}}, - "23B74" = {{23, 1, 0}, "macOS", {"Sonoma", {14, 1, 0}}}, - "23B81" = {{23, 1, 0}, "macOS", {"Sonoma", {14, 1, 1}}}, - "23B2082" = {{23, 1, 0}, "macOS", {"Sonoma", {14, 1, 1}}}, - "23B92" = {{23, 1, 0}, "macOS", {"Sonoma", {14, 1, 2}}}, - "23B2091" = {{23, 1, 0}, "macOS", {"Sonoma", {14, 1, 2}}}, - "23C64" = {{23, 2, 0}, "macOS", {"Sonoma", {14, 2, 0}}}, - "23C71" = {{23, 2, 0}, "macOS", {"Sonoma", {14, 2, 1}}}, - "23D56" = {{23, 3, 0}, "macOS", {"Sonoma", {14, 3, 0}}}, - "23D60" = {{23, 3, 0}, "macOS", {"Sonoma", {14, 3, 1}}}, - "23E214" = {{23, 4, 0}, "macOS", {"Sonoma", {14, 4, 0}}}, - "23E224" = {{23, 4, 0}, "macOS", {"Sonoma", {14, 4, 1}}}, - "23F79" = {{23, 5, 0}, "macOS", {"Sonoma", {14, 5, 0}}}, - "23G80" = {{23, 6, 0}, "macOS", {"Sonoma", {14, 6, 0}}}, - "23G93" = {{23, 6, 0}, "macOS", {"Sonoma", {14, 6, 1}}}, - "23H124" = {{23, 6, 0}, "macOS", {"Sonoma", {14, 7, 0}}}, - - // MacOS Sequoia - "24A335" = {{24, 0, 0}, "macOS", {"Sequoia", {15, 0, 0}}}, - "24A348" = {{24, 0, 0}, "macOS", {"Sequoia", {15, 0, 1}}}, -} - -@(private) -Darwin_Match :: enum { - Unknown, - Exact, - Nearest, -} - -@(private) -map_darwin_kernel_version_to_macos_release :: proc(build: string, darwin: [3]int) -> (res: Darwin_To_Release, match: Darwin_Match) { - // Find exact release match if possible. - if v, v_ok := macos_release_map[build]; v_ok { - return v, .Exact - } - - nearest: Darwin_To_Release - for _, v in macos_release_map { - // Try an exact match on XNU version first. - if darwin == v.darwin { - return v, .Exact - } - - // Major kernel version needs to match exactly, - // otherwise the release is considered .Unknown - if darwin.x == v.darwin.x { - if nearest == {} { - nearest = v - } - if darwin.y >= v.darwin.y && v.darwin != nearest.darwin { - nearest = v - if darwin.z >= v.darwin.z && v.darwin != nearest.darwin { - nearest = v - } - } - } - } - - if nearest == {} { - return {darwin, "macOS", {"Unknown", {}}}, .Unknown - } else { - return nearest, .Nearest - } + os_version.as_string = string(b.buf[:]) } diff --git a/core/sys/linux/bits.odin b/core/sys/linux/bits.odin index 9f2c7a5d8..9ce2e206e 100644 --- a/core/sys/linux/bits.odin +++ b/core/sys/linux/bits.odin @@ -519,6 +519,36 @@ Fd_Poll_Events_Bits :: enum { RDHUP = 13, } +Inotify_Init_Bits :: enum { + NONBLOCK = 11, + CLOEXEC = 19, +} + +Inotify_Event_Bits :: enum u32 { + ACCESS = 0, + MODIFY = 1, + ATTRIB = 2, + CLOSE_WRITE = 3, + CLOSE_NOWRITE = 4, + OPEN = 5, + MOVED_FROM = 6, + MOVED_TO = 7, + CREATE = 8, + DELETE = 9, + DELETE_SELF = 10, + MOVE_SELF = 11, + UNMOUNT = 13, + Q_OVERFLOW = 14, + IGNORED = 15, + ONLYDIR = 24, + DONT_FOLLOW = 25, + EXCL_UNLINK = 26, + MASK_CREATE = 28, + MASK_ADD = 29, + ISDIR = 30, + ONESHOT = 31, +} + /* Bits for Mem_Protection bitfield */ diff --git a/core/sys/linux/constants.odin b/core/sys/linux/constants.odin index 129444d0f..b3bbcafb3 100644 --- a/core/sys/linux/constants.odin +++ b/core/sys/linux/constants.odin @@ -135,6 +135,31 @@ STATX_BASIC_STATS :: Statx_Mask { .BLOCKS, } +IN_ALL_EVENTS :: Inotify_Event_Mask { + .ACCESS, + .MODIFY, + .ATTRIB, + .CLOSE_WRITE, + .CLOSE_NOWRITE, + .OPEN, + .MOVED_FROM, + .MOVED_TO, + .CREATE, + .DELETE, + .DELETE_SELF, + .MOVE_SELF, +} + +IN_CLOSE :: Inotify_Event_Mask { + .CLOSE_WRITE, + .CLOSE_NOWRITE, +} + +IN_MOVE :: Inotify_Event_Mask { + .MOVED_FROM, + .MOVED_TO, +} + /* Tell `shmget` to create a new key */ diff --git a/core/sys/linux/sys.odin b/core/sys/linux/sys.odin index c5894d78b..690902f07 100644 --- a/core/sys/linux/sys.odin +++ b/core/sys/linux/sys.odin @@ -2536,11 +2536,30 @@ waitid :: proc "contextless" (id_type: Id_Type, id: Id, sig_info: ^Sig_Info, opt // TODO(flysand): ioprio_get -// TODO(flysand): inotify_init +inotify_init :: proc "contextless" () -> (Fd, Errno) { + when ODIN_ARCH == .arm64 || ODIN_ARCH == .riscv64 { + ret := syscall(SYS_inotify_init1, 0) + return errno_unwrap(ret, Fd) + } else { + ret := syscall(SYS_inotify_init) + return errno_unwrap(ret, Fd) + } +} -// TODO(flysand): inotify_add_watch +inotify_init1 :: proc "contextless" (flags: Inotify_Init_Flags) -> (Fd, Errno) { + ret := syscall(SYS_inotify_init1, transmute(i32)flags) + return errno_unwrap(ret, Fd) +} -// TODO(flysand): inotify_rm_watch +inotify_add_watch :: proc "contextless" (fd: Fd, pathname: cstring, mask: Inotify_Event_Mask) -> (Wd, Errno) { + ret := syscall(SYS_inotify_add_watch, fd, transmute(uintptr) pathname, transmute(u32) mask) + return errno_unwrap(ret, Wd) +} + +inotify_rm_watch :: proc "contextless" (fd: Fd, wd: Wd) -> (Errno) { + ret := syscall(SYS_inotify_rm_watch, fd, wd) + return Errno(-ret) +} // TODO(flysand): migrate_pages diff --git a/core/sys/linux/types.odin b/core/sys/linux/types.odin index 07c654749..42d5cc988 100644 --- a/core/sys/linux/types.odin +++ b/core/sys/linux/types.odin @@ -30,6 +30,11 @@ Id :: distinct uint */ Fd :: distinct i32 +/* + Represents a watch descriptor. +*/ +Wd :: distinct i32 + /* Type for PID file descriptors. */ @@ -343,6 +348,18 @@ Poll_Fd :: struct { revents: Fd_Poll_Events, } +Inotify_Init_Flags :: bit_set[Inotify_Init_Bits; i32] + +Inotify_Event :: struct { + wd: Wd, + mask: Inotify_Event_Mask, + cookie: u32, + len: u32, + name: [0]u8, +} + +Inotify_Event_Mask :: bit_set[Inotify_Event_Bits; u32] + /* Specifies protection for memory pages. */ diff --git a/core/sys/posix/dirent.odin b/core/sys/posix/dirent.odin index 73351b29d..bf32be8cf 100644 --- a/core/sys/posix/dirent.odin +++ b/core/sys/posix/dirent.odin @@ -54,15 +54,6 @@ foreign lib { */ closedir :: proc(dirp: DIR) -> result --- - /* - Return a file descriptor referring to the same directory as the dirp argument. - - // TODO: this is a macro on NetBSD? - - [[ More; https://pubs.opengroup.org/onlinepubs/9699919799/functions/dirfd.html ]] - */ - dirfd :: proc(dirp: DIR) -> FD --- - /* Equivalent to the opendir() function except that the directory is specified by a file descriptor rather than by a name. @@ -161,11 +152,36 @@ when ODIN_OS == .NetBSD { @(private) LSCANDIR :: "__scandir30" @(private) LOPENDIR :: "__opendir30" @(private) LREADDIR :: "__readdir30" + + /* + Return a file descriptor referring to the same directory as the dirp argument. + + [[ More; https://pubs.opengroup.org/onlinepubs/9699919799/functions/dirfd.html ]] + */ + dirfd :: proc "c" (dirp: DIR) -> FD { + _dirdesc :: struct { + dd_fd: FD, + + // more stuff... + } + + return (^_dirdesc)(dirp).dd_fd + } + } else { @(private) LALPHASORT :: "alphasort" + INODE_SUFFIX @(private) LSCANDIR :: "scandir" + INODE_SUFFIX @(private) LOPENDIR :: "opendir" + INODE_SUFFIX @(private) LREADDIR :: "readdir" + INODE_SUFFIX + + foreign lib { + /* + Return a file descriptor referring to the same directory as the dirp argument. + + [[ More; https://pubs.opengroup.org/onlinepubs/9699919799/functions/dirfd.html ]] + */ + dirfd :: proc(dirp: DIR) -> FD --- + } } when ODIN_OS == .Darwin { diff --git a/core/sys/posix/sys_mman.odin b/core/sys/posix/sys_mman.odin index 9e2939a05..0594672ae 100644 --- a/core/sys/posix/sys_mman.odin +++ b/core/sys/posix/sys_mman.odin @@ -116,12 +116,14 @@ Prot_Flag_Bits :: enum c.int { Prot_Flags :: bit_set[Prot_Flag_Bits; c.int] Map_Flag_Bits :: enum c.int { + // Map anonymous memory. + ANONYMOUS = log2(MAP_ANONYMOUS), // Interpret addr exactly. - FIXED = log2(MAP_FIXED), + FIXED = log2(MAP_FIXED), // Changes are private. - PRIVATE = log2(MAP_PRIVATE), + PRIVATE = log2(MAP_PRIVATE), // Changes are shared. - SHARED = log2(MAP_SHARED), + SHARED = log2(MAP_SHARED), } Map_Flags :: bit_set[Map_Flag_Bits; c.int] @@ -171,9 +173,10 @@ when ODIN_OS == .Darwin || ODIN_OS == .NetBSD || ODIN_OS == .OpenBSD || ODIN_OS PROT_READ :: 0x01 PROT_WRITE :: 0x02 - MAP_FIXED :: 0x0010 - MAP_PRIVATE :: 0x0002 - MAP_SHARED :: 0x0001 + MAP_FIXED :: 0x0010 + MAP_PRIVATE :: 0x0002 + MAP_SHARED :: 0x0001 + MAP_ANONYMOUS :: 0x0020 when ODIN_OS == .Linux else 0x1000 when ODIN_OS == .Darwin || ODIN_OS == .Linux { MS_INVALIDATE :: 0x0002 @@ -207,9 +210,10 @@ when ODIN_OS == .Darwin || ODIN_OS == .NetBSD || ODIN_OS == .OpenBSD || ODIN_OS PROT_READ :: 0x01 PROT_WRITE :: 0x02 - MAP_FIXED :: 0x0010 - MAP_PRIVATE :: 0x0002 - MAP_SHARED :: 0x0001 + MAP_FIXED :: 0x0010 + MAP_PRIVATE :: 0x0002 + MAP_SHARED :: 0x0001 + MAP_ANONYMOUS :: 0x1000 MS_ASYNC :: 0x0001 MS_INVALIDATE :: 0x0002 diff --git a/core/sys/posix/sys_socket.odin b/core/sys/posix/sys_socket.odin index 3c9db5bfa..4dd6074a3 100644 --- a/core/sys/posix/sys_socket.odin +++ b/core/sys/posix/sys_socket.odin @@ -48,6 +48,12 @@ foreign libc { addr.sun_family = .UNIX copy(addr.sun_path[:], "/somepath\x00") + /* + unlink the socket before binding in case + of previous runs not cleaning up the socket + */ + posix.unlink("/somepath") + if posix.bind(sfd, (^posix.sockaddr)(&addr), size_of(addr)) != .OK { /* Handle error */ } diff --git a/core/sys/wasm/js/events.odin b/core/sys/wasm/js/events.odin index 905b3eba9..ffa3a1202 100644 --- a/core/sys/wasm/js/events.odin +++ b/core/sys/wasm/js/events.odin @@ -186,8 +186,8 @@ Key_Location :: enum u8 { Numpad = 3, } -KEYBOARD_MAX_KEY_SIZE :: 16 -KEYBOARD_MAX_CODE_SIZE :: 16 +KEYBOARD_MAX_KEY_SIZE :: 32 +KEYBOARD_MAX_CODE_SIZE :: 32 GAMEPAD_MAX_ID_SIZE :: 64 GAMEPAD_MAX_MAPPING_SIZE :: 64 diff --git a/core/sys/wasm/js/odin.js b/core/sys/wasm/js/odin.js index 4d93bab23..07a77952c 100644 --- a/core/sys/wasm/js/odin.js +++ b/core/sys/wasm/js/odin.js @@ -1548,8 +1548,8 @@ function odinSetupDefaultImports(wasmMemoryInterface, consoleElement, memory) { wmi.storeInt(off(W, W), e.key.length) wmi.storeInt(off(W, W), e.code.length) - wmi.storeString(off(16, 1), e.key); - wmi.storeString(off(16, 1), e.code); + wmi.storeString(off(32, 1), e.key); + wmi.storeString(off(32, 1), e.code); } else if (e.type === 'scroll') { wmi.storeF64(off(8, 8), window.scrollX); wmi.storeF64(off(8, 8), window.scrollY); diff --git a/core/sys/windows/types.odin b/core/sys/windows/types.odin index e1ace4133..5dcf09eab 100644 --- a/core/sys/windows/types.odin +++ b/core/sys/windows/types.odin @@ -4519,7 +4519,7 @@ DNS_RECORD :: struct { // aka DNS_RECORDA Flags: DWORD, dwTtl: DWORD, _: DWORD, - Data: struct #raw_union { + Data: struct #raw_union #align(4) { CNAME: DNS_PTR_DATAA, A: u32be, // Ipv4 Address AAAA: u128be, // Ipv6 Address diff --git a/core/sys/windows/user32.odin b/core/sys/windows/user32.odin index 514592e7b..4ae33cd32 100644 --- a/core/sys/windows/user32.odin +++ b/core/sys/windows/user32.odin @@ -781,3 +781,64 @@ CF_GDIOBJLAST :: 0x03FF CF_OWNERDISPLAY :: 0x0080 CF_PRIVATEFIRST :: 0x0200 CF_PRIVATELAST :: 0x02FF + +STICKYKEYS :: struct { + cbSize: UINT, + dwFlags: DWORD, +} +LPSTICKYKEYS :: ^STICKYKEYS + +SKF_STICKYKEYSON :: 0x1 +SKF_AVAILABLE :: 0x2 +SKF_HOTKEYACTIVE :: 0x4 +SKF_CONFIRMHOTKEY :: 0x8 +SKF_HOTKEYSOUND :: 0x10 +SKF_INDICATOR :: 0x20 +SKF_AUDIBLEFEEDBACK :: 0x40 +SKF_TRISTATE :: 0x80 +SKF_TWOKEYSOFF :: 0x100 +SKF_LSHIFTLOCKED :: 0x10000 +SKF_RSHIFTLOCKED :: 0x20000 +SKF_LCTLLOCKED :: 0x40000 +SKF_RCTLLOCKED :: 0x80000 +SKF_LALTLOCKED :: 0x100000 +SKF_RALTLOCKED :: 0x200000 +SKF_LWINLOCKED :: 0x400000 +SKF_RWINLOCKED :: 0x800000 +SKF_LSHIFTLATCHED :: 0x1000000 +SKF_RSHIFTLATCHED :: 0x2000000 +SKF_LCTLLATCHED :: 0x4000000 +SKF_RCTLLATCHED :: 0x8000000 +SKF_LALTLATCHED :: 0x10000000 +SKF_RALTLATCHED :: 0x20000000 + +TOGGLEKEYS :: struct { + cbSize: UINT, + dwFlags: DWORD, +} +LPTOGGLEKEYS :: ^TOGGLEKEYS + +TKF_TOGGLEKEYSON :: 0x1 +TKF_AVAILABLE :: 0x2 +TKF_HOTKEYACTIVE :: 0x4 +TKF_CONFIRMHOTKEY :: 0x8 +TKF_HOTKEYSOUND :: 0x10 +TKF_INDICATOR :: 0x20 + +FILTERKEYS :: struct { + cbSize: UINT, + dwFlags: DWORD, + iWaitMSec: DWORD, + iDelayMSec: DWORD, + iRepeatMSec: DWORD, + iBounceMSec: DWORD, +} +LPFILTERKEYS :: ^FILTERKEYS + +FKF_FILTERKEYSON :: 0x1 +FKF_AVAILABLE :: 0x2 +FKF_HOTKEYACTIVE :: 0x4 +FKF_CONFIRMHOTKEY :: 0x8 +FKF_HOTKEYSOUND :: 0x10 +FKF_INDICATOR :: 0x20 +FKF_CLICKON :: 0x40 diff --git a/core/sys/windows/ux_theme.odin b/core/sys/windows/ux_theme.odin index 527abd62f..392cf1e18 100644 --- a/core/sys/windows/ux_theme.odin +++ b/core/sys/windows/ux_theme.odin @@ -3,7 +3,7 @@ package sys_windows foreign import uxtheme "system:UxTheme.lib" -MARGINS :: distinct [4]int +MARGINS :: distinct [4]i32 PMARGINS :: ^MARGINS @(default_calling_convention="system") diff --git a/core/text/regex/regex.odin b/core/text/regex/regex.odin index 3dc26b5c6..8f8efe252 100644 --- a/core/text/regex/regex.odin +++ b/core/text/regex/regex.odin @@ -381,6 +381,7 @@ match_with_preallocated_capture :: proc( capture.pos[n] = {a, b} n += 1 } + num_groups = n } return diff --git a/core/text/scanner/scanner.odin b/core/text/scanner/scanner.odin index d27c66f24..24dbcc8a4 100644 --- a/core/text/scanner/scanner.odin +++ b/core/text/scanner/scanner.odin @@ -2,7 +2,7 @@ // It takes a string providing the source, which then can be tokenized through // repeated calls to the scan procedure. // For compatibility with existing tooling and languages, the NUL character is not allowed. -// If an UTF-8 encoded byte order mark (BOM) is the first character in the first character in the source, it will be discarded. +// If an UTF-8 encoded byte order mark (BOM) is the first character in the source, it will be discarded. // // By default, a Scanner skips white space and Odin comments and recognizes all literals defined by the Odin programming language specification. // A Scanner may be customized to recognize only a subset of those literals and to recognize different identifiers and white space characters. diff --git a/core/thread/thread_pool.odin b/core/thread/thread_pool.odin index d9166b450..59bf90620 100644 --- a/core/thread/thread_pool.odin +++ b/core/thread/thread_pool.odin @@ -9,6 +9,7 @@ package thread import "base:intrinsics" import "core:sync" import "core:mem" +import "core:container/queue" Task_Proc :: #type proc(task: Task) @@ -40,7 +41,7 @@ Pool :: struct { threads: []^Thread, - tasks: [dynamic]Task, + tasks: queue.Queue(Task), tasks_done: [dynamic]Task, } @@ -69,13 +70,13 @@ pool_thread_runner :: proc(t: ^Thread) { } // Once initialized, the pool's memory address is not allowed to change until -// it is destroyed. +// it is destroyed. // // The thread pool requires an allocator which it either owns, or which is thread safe. pool_init :: proc(pool: ^Pool, allocator: mem.Allocator, thread_count: int) { context.allocator = allocator pool.allocator = allocator - pool.tasks = make([dynamic]Task) + queue.init(&pool.tasks) pool.tasks_done = make([dynamic]Task) pool.threads = make([]^Thread, max(thread_count, 1)) @@ -92,7 +93,7 @@ pool_init :: proc(pool: ^Pool, allocator: mem.Allocator, thread_count: int) { } pool_destroy :: proc(pool: ^Pool) { - delete(pool.tasks) + queue.destroy(&pool.tasks) delete(pool.tasks_done) for &t in pool.threads { @@ -140,11 +141,11 @@ pool_join :: proc(pool: ^Pool) { // the thread pool. You can even add tasks from inside other tasks. // // Each task also needs an allocator which it either owns, or which is thread -// safe. +// safe. pool_add_task :: proc(pool: ^Pool, allocator: mem.Allocator, procedure: Task_Proc, data: rawptr, user_index: int = 0) { sync.guard(&pool.mutex) - append(&pool.tasks, Task{ + queue.push_back(&pool.tasks, Task{ procedure = procedure, data = data, user_index = user_index, @@ -288,10 +289,10 @@ pool_is_empty :: #force_inline proc(pool: ^Pool) -> bool { pool_pop_waiting :: proc(pool: ^Pool) -> (task: Task, got_task: bool) { sync.guard(&pool.mutex) - if len(pool.tasks) != 0 { + if queue.len(pool.tasks) != 0 { intrinsics.atomic_sub(&pool.num_waiting, 1) intrinsics.atomic_add(&pool.num_in_processing, 1) - task = pop_front(&pool.tasks) + task = queue.pop_front(&pool.tasks) got_task = true } diff --git a/core/time/timezone/tz_windows.odin b/core/time/timezone/tz_windows.odin index f1604b939..238c4c933 100644 --- a/core/time/timezone/tz_windows.odin +++ b/core/time/timezone/tz_windows.odin @@ -203,6 +203,22 @@ generate_rrule_from_tzi :: proc(tzi: ^REG_TZI_FORMAT, abbrevs: TZ_Abbrev, alloca if err != nil { return } defer if err != nil { delete(std_name, allocator) } + if (tzi.std_date.month == 0) { + return datetime.TZ_RRule{ + has_dst = false, + + std_name = std_name, + std_offset = -(i64(tzi.bias) + i64(tzi.std_bias)) * 60, + dst_date = datetime.TZ_Transition_Date{ + type = .Month_Week_Day, + month = u8(tzi.std_date.month), + week = u8(tzi.std_date.day), + day = tzi.std_date.day_of_week, + time = (i64(tzi.std_date.hour) * 60 * 60) + (i64(tzi.std_date.minute) * 60) + i64(tzi.std_date.second), + }, + }, true + } + dst_name: string dst_name, err = strings.clone(abbrevs.dst, allocator) if err != nil { return } diff --git a/core/time/timezone/tzdate.odin b/core/time/timezone/tzdate.odin index 8f83d1bf4..96df44299 100644 --- a/core/time/timezone/tzdate.odin +++ b/core/time/timezone/tzdate.odin @@ -93,6 +93,8 @@ trans_date_to_seconds :: proc(year: i64, td: datetime.TZ_Transition_Date) -> (se switch td.type { case .Month_Week_Day: + if td.month < 1 { return } + t += month_to_seconds(int(td.month) - 1, is_leap) weekday := ((t + (4 * DAY_SEC)) %% (7 * DAY_SEC)) / DAY_SEC diff --git a/core/time/timezone/tzif.odin b/core/time/timezone/tzif.odin index 609cbda73..3fec7be53 100644 --- a/core/time/timezone/tzif.odin +++ b/core/time/timezone/tzif.odin @@ -536,24 +536,21 @@ parse_tzif :: proc(_buffer: []u8, region_name: string, allocator := context.allo buffer = buffer[(int(real_hdr.leapcnt) * size_of(Leapsecond_Record)):] standard_wall_tags := buffer[:int(real_hdr.isstdcnt)] - buffer = buffer[int(real_hdr.isstdcnt):] - - ut_tags := buffer[:int(real_hdr.isutcnt)] - - for stdwall_tag, idx in standard_wall_tags { - ut_tag := ut_tags[idx] - + for stdwall_tag, _ in standard_wall_tags { if (stdwall_tag != 0 && stdwall_tag != 1) { return } + } + + buffer = buffer[int(real_hdr.isstdcnt):] + + ut_tags := buffer[:int(real_hdr.isutcnt)] + for ut_tag, _ in ut_tags { if (ut_tag != 0 && ut_tag != 1) { return } - - if ut_tag == 1 && stdwall_tag != 1 { - return - } } + buffer = buffer[int(real_hdr.isutcnt):] // Start of footer diff --git a/examples/README.md b/examples/README.md index 27072a480..90076df23 100644 --- a/examples/README.md +++ b/examples/README.md @@ -2,8 +2,8 @@ The `example` directory contains two packages: -A [demo](examples/demo) illustrating the basics of Odin. +A [demo](demo) illustrating the basics of Odin. -It further contains [all](examples/all), which imports all [core](core) and [vendor](vendor) packages so we can conveniently run `odin check` on everything at once. +It further contains [all](all), which imports all [core](/core) and [vendor](/vendor) packages so we can conveniently run `odin check` on everything at once. For additional example code, see the [examples](https://github.com/odin-lang/examples) repository. diff --git a/examples/all/all_experimental.odin b/examples/all/all_experimental.odin index 1d4eb4bb9..f00098abd 100644 --- a/examples/all/all_experimental.odin +++ b/examples/all/all_experimental.odin @@ -1,8 +1,2 @@ #+build windows package all - -import c_tokenizer "core:c/frontend/tokenizer" -import c_preprocessor "core:c/frontend/preprocessor" - -_ :: c_tokenizer -_ :: c_preprocessor diff --git a/examples/demo/demo.odin b/examples/demo/demo.odin index 38b0f1739..36d1359ca 100644 --- a/examples/demo/demo.odin +++ b/examples/demo/demo.odin @@ -2052,22 +2052,6 @@ explicit_context_definition :: proc "c" () { dummy_procedure() } -relative_data_types :: proc() { - fmt.println("\n#relative data types") - - x: int = 123 - ptr: #relative(i16) ^int - ptr = &x - fmt.println(ptr^) - - arr := [3]int{1, 2, 3} - multi_ptr: #relative(i16) [^]int - multi_ptr = &arr[0] - fmt.println(multi_ptr) - fmt.println(multi_ptr[:3]) - fmt.println(multi_ptr[1]) -} - or_else_operator :: proc() { fmt.println("\n#'or_else'") { @@ -2151,7 +2135,7 @@ or_return_operator :: proc() { return .None } foo_2 :: proc() -> (n: int, err: Error) { - // It is more common that your procedure turns multiple values + // It is more common that your procedure returns multiple values // If 'or_return' is used within a procedure multiple parameters (2+), // then all the parameters must be named so that the remaining parameters // so that a bare 'return' statement can be used @@ -2634,7 +2618,6 @@ main :: proc() { constant_literal_expressions() union_maybe() explicit_context_definition() - relative_data_types() or_else_operator() or_return_operator() or_break_and_or_continue_operators() diff --git a/misc/get-date.c b/misc/get-date.c new file mode 100644 index 000000000..bf5b32738 --- /dev/null +++ b/misc/get-date.c @@ -0,0 +1,13 @@ +/* + Prints the current date as YYYYMMDD + + e.g. 2024-12-25 +*/ +#include +#include + +int main(int arg_count, char const **arg_ptr) { + time_t t = time(NULL); + struct tm* now = localtime(&t); + printf("%04d%02d%02d", now->tm_year + 1900, now->tm_mon + 1, now->tm_mday); +} \ No newline at end of file diff --git a/misc/odin.rc b/misc/odin.rc index 9e605f6dc..70d395030 100644 --- a/misc/odin.rc +++ b/misc/odin.rc @@ -55,12 +55,12 @@ BEGIN BLOCK "0409FDE9" BEGIN VALUE "CompanyName", "https://odin-lang.org/" - VALUE "FileDescription", "Odin general-purpose programming language." // note this is shown in the task manager + VALUE "FileDescription", "Odin" // note this is shown in the task manager VALUE "FileVersion", QUOTE(VF) VALUE "InternalName", "odin.exe" VALUE "LegalCopyright", "Copyright (c) 2016-2024 Ginger Bill. All rights reserved." VALUE "OriginalFilename", "odin.exe" - VALUE "ProductName", "The Odin Programming Language" + VALUE "ProductName", "Odin Programming Language" VALUE "ProductVersion", QUOTE(VP) VALUE "Comments", QUOTE(git-sha: GIT_SHA) // custom values diff --git a/shell.nix b/shell.nix index 21301b9d7..040c7696e 100644 --- a/shell.nix +++ b/shell.nix @@ -3,6 +3,7 @@ pkgs.mkShell { name = "odin"; nativeBuildInputs = with pkgs; [ git + which clang_17 llvmPackages_17.llvm llvmPackages_17.bintools diff --git a/src/bug_report.cpp b/src/bug_report.cpp index c5a8adea3..ca5d0a395 100644 --- a/src/bug_report.cpp +++ b/src/bug_report.cpp @@ -2,12 +2,6 @@ Gather and print platform and version info to help with reporting Odin bugs. */ -#if !defined(GB_COMPILER_MSVC) - #if defined(GB_CPU_X86) - #include - #endif -#endif - #if defined(GB_SYSTEM_LINUX) #include #include @@ -154,21 +148,6 @@ gb_internal void report_windows_product_type(DWORD ProductType) { } #endif -gb_internal void odin_cpuid(int leaf, int result[]) { - #if defined(GB_CPU_ARM) || defined(GB_CPU_RISCV) - return; - - #elif defined(GB_CPU_X86) - - #if defined(GB_COMPILER_MSVC) - __cpuid(result, leaf); - #else - __get_cpuid(leaf, (unsigned int*)&result[0], (unsigned int*)&result[1], (unsigned int*)&result[2], (unsigned int*)&result[3]); - #endif - - #endif -} - gb_internal void report_cpu_info() { gb_printf("\tCPU: "); @@ -547,387 +526,46 @@ gb_internal void report_os_info() { gb_printf(", %s %s\n", info->sysname, info->release); #elif defined(GB_SYSTEM_OSX) - struct Darwin_To_Release { - const char* build; // 21G83 - int darwin[3]; // Darwin kernel triplet - const char* os_name; // OS X, MacOS - struct { - const char* name; // Monterey, Mojave, etc. - int version[3]; // 12.4, etc. - } release; - }; - - Darwin_To_Release macos_release_map[] = { - {"8A428", { 8, 0, 0}, "macOS", {"Tiger", {10, 4, 0}}}, - {"8A432", { 8, 0, 0}, "macOS", {"Tiger", {10, 4, 0}}}, - {"8B15", { 8, 1, 0}, "macOS", {"Tiger", {10, 4, 1}}}, - {"8B17", { 8, 1, 0}, "macOS", {"Tiger", {10, 4, 1}}}, - {"8C46", { 8, 2, 0}, "macOS", {"Tiger", {10, 4, 2}}}, - {"8C47", { 8, 2, 0}, "macOS", {"Tiger", {10, 4, 2}}}, - {"8E102", { 8, 2, 0}, "macOS", {"Tiger", {10, 4, 2}}}, - {"8E45", { 8, 2, 0}, "macOS", {"Tiger", {10, 4, 2}}}, - {"8E90", { 8, 2, 0}, "macOS", {"Tiger", {10, 4, 2}}}, - {"8F46", { 8, 3, 0}, "macOS", {"Tiger", {10, 4, 3}}}, - {"8G32", { 8, 4, 0}, "macOS", {"Tiger", {10, 4, 4}}}, - {"8G1165", { 8, 4, 0}, "macOS", {"Tiger", {10, 4, 4}}}, - {"8H14", { 8, 5, 0}, "macOS", {"Tiger", {10, 4, 5}}}, - {"8G1454", { 8, 5, 0}, "macOS", {"Tiger", {10, 4, 5}}}, - {"8I127", { 8, 6, 0}, "macOS", {"Tiger", {10, 4, 6}}}, - {"8I1119", { 8, 6, 0}, "macOS", {"Tiger", {10, 4, 6}}}, - {"8J135", { 8, 7, 0}, "macOS", {"Tiger", {10, 4, 7}}}, - {"8J2135a", { 8, 7, 0}, "macOS", {"Tiger", {10, 4, 7}}}, - {"8K1079", { 8, 7, 0}, "macOS", {"Tiger", {10, 4, 7}}}, - {"8N5107", { 8, 7, 0}, "macOS", {"Tiger", {10, 4, 7}}}, - {"8L127", { 8, 8, 0}, "macOS", {"Tiger", {10, 4, 8}}}, - {"8L2127", { 8, 8, 0}, "macOS", {"Tiger", {10, 4, 8}}}, - {"8P135", { 8, 9, 0}, "macOS", {"Tiger", {10, 4, 9}}}, - {"8P2137", { 8, 9, 0}, "macOS", {"Tiger", {10, 4, 9}}}, - {"8R218", { 8, 10, 0}, "macOS", {"Tiger", {10, 4, 10}}}, - {"8R2218", { 8, 10, 0}, "macOS", {"Tiger", {10, 4, 10}}}, - {"8R2232", { 8, 10, 0}, "macOS", {"Tiger", {10, 4, 10}}}, - {"8S165", { 8, 11, 0}, "macOS", {"Tiger", {10, 4, 11}}}, - {"8S2167", { 8, 11, 0}, "macOS", {"Tiger", {10, 4, 11}}}, - {"9A581", { 9, 0, 0}, "macOS", {"Leopard", {10, 5, 0}}}, - {"9B18", { 9, 1, 0}, "macOS", {"Leopard", {10, 5, 1}}}, - {"9B2117", { 9, 1, 1}, "macOS", {"Leopard", {10, 5, 1}}}, - {"9C31", { 9, 2, 0}, "macOS", {"Leopard", {10, 5, 2}}}, - {"9C7010", { 9, 2, 0}, "macOS", {"Leopard", {10, 5, 2}}}, - {"9D34", { 9, 3, 0}, "macOS", {"Leopard", {10, 5, 3}}}, - {"9E17", { 9, 4, 0}, "macOS", {"Leopard", {10, 5, 4}}}, - {"9F33", { 9, 5, 0}, "macOS", {"Leopard", {10, 5, 5}}}, - {"9G55", { 9, 6, 0}, "macOS", {"Leopard", {10, 5, 6}}}, - {"9G66", { 9, 6, 0}, "macOS", {"Leopard", {10, 5, 6}}}, - {"9G71", { 9, 6, 0}, "macOS", {"Leopard", {10, 5, 6}}}, - {"9J61", { 9, 7, 0}, "macOS", {"Leopard", {10, 5, 7}}}, - {"9L30", { 9, 8, 0}, "macOS", {"Leopard", {10, 5, 8}}}, - {"9L34", { 9, 8, 0}, "macOS", {"Leopard", {10, 5, 8}}}, - {"10A432", {10, 0, 0}, "macOS", {"Snow Leopard", {10, 6, 0}}}, - {"10A433", {10, 0, 0}, "macOS", {"Snow Leopard", {10, 6, 0}}}, - {"10B504", {10, 1, 0}, "macOS", {"Snow Leopard", {10, 6, 1}}}, - {"10C540", {10, 2, 0}, "macOS", {"Snow Leopard", {10, 6, 2}}}, - {"10D573", {10, 3, 0}, "macOS", {"Snow Leopard", {10, 6, 3}}}, - {"10D575", {10, 3, 0}, "macOS", {"Snow Leopard", {10, 6, 3}}}, - {"10D578", {10, 3, 0}, "macOS", {"Snow Leopard", {10, 6, 3}}}, - {"10F569", {10, 4, 0}, "macOS", {"Snow Leopard", {10, 6, 4}}}, - {"10H574", {10, 5, 0}, "macOS", {"Snow Leopard", {10, 6, 5}}}, - {"10J567", {10, 6, 0}, "macOS", {"Snow Leopard", {10, 6, 6}}}, - {"10J869", {10, 7, 0}, "macOS", {"Snow Leopard", {10, 6, 7}}}, - {"10J3250", {10, 7, 0}, "macOS", {"Snow Leopard", {10, 6, 7}}}, - {"10J4138", {10, 7, 0}, "macOS", {"Snow Leopard", {10, 6, 7}}}, - {"10K540", {10, 8, 0}, "macOS", {"Snow Leopard", {10, 6, 8}}}, - {"10K549", {10, 8, 0}, "macOS", {"Snow Leopard", {10, 6, 8}}}, - {"11A511", {11, 0, 0}, "macOS", {"Lion", {10, 7, 0}}}, - {"11A511s", {11, 0, 0}, "macOS", {"Lion", {10, 7, 0}}}, - {"11A2061", {11, 0, 2}, "macOS", {"Lion", {10, 7, 0}}}, - {"11A2063", {11, 0, 2}, "macOS", {"Lion", {10, 7, 0}}}, - {"11B26", {11, 1, 0}, "macOS", {"Lion", {10, 7, 1}}}, - {"11B2118", {11, 1, 0}, "macOS", {"Lion", {10, 7, 1}}}, - {"11C74", {11, 2, 0}, "macOS", {"Lion", {10, 7, 2}}}, - {"11D50", {11, 3, 0}, "macOS", {"Lion", {10, 7, 3}}}, - {"11E53", {11, 4, 0}, "macOS", {"Lion", {10, 7, 4}}}, - {"11G56", {11, 4, 2}, "macOS", {"Lion", {10, 7, 5}}}, - {"11G63", {11, 4, 2}, "macOS", {"Lion", {10, 7, 5}}}, - {"12A269", {12, 0, 0}, "macOS", {"Mountain Lion", {10, 8, 0}}}, - {"12B19", {12, 1, 0}, "macOS", {"Mountain Lion", {10, 8, 1}}}, - {"12C54", {12, 2, 0}, "macOS", {"Mountain Lion", {10, 8, 2}}}, - {"12C60", {12, 2, 0}, "macOS", {"Mountain Lion", {10, 8, 2}}}, - {"12C2034", {12, 2, 0}, "macOS", {"Mountain Lion", {10, 8, 2}}}, - {"12C3104", {12, 2, 0}, "macOS", {"Mountain Lion", {10, 8, 2}}}, - {"12D78", {12, 3, 0}, "macOS", {"Mountain Lion", {10, 8, 3}}}, - {"12E55", {12, 4, 0}, "macOS", {"Mountain Lion", {10, 8, 4}}}, - {"12E3067", {12, 4, 0}, "macOS", {"Mountain Lion", {10, 8, 4}}}, - {"12E4022", {12, 4, 0}, "macOS", {"Mountain Lion", {10, 8, 4}}}, - {"12F37", {12, 5, 0}, "macOS", {"Mountain Lion", {10, 8, 5}}}, - {"12F45", {12, 5, 0}, "macOS", {"Mountain Lion", {10, 8, 5}}}, - {"12F2501", {12, 5, 0}, "macOS", {"Mountain Lion", {10, 8, 5}}}, - {"12F2518", {12, 5, 0}, "macOS", {"Mountain Lion", {10, 8, 5}}}, - {"12F2542", {12, 5, 0}, "macOS", {"Mountain Lion", {10, 8, 5}}}, - {"12F2560", {12, 5, 0}, "macOS", {"Mountain Lion", {10, 8, 5}}}, - {"13A603", {13, 0, 0}, "macOS", {"Mavericks", {10, 9, 0}}}, - {"13B42", {13, 0, 0}, "macOS", {"Mavericks", {10, 9, 1}}}, - {"13C64", {13, 1, 0}, "macOS", {"Mavericks", {10, 9, 2}}}, - {"13C1021", {13, 1, 0}, "macOS", {"Mavericks", {10, 9, 2}}}, - {"13D65", {13, 2, 0}, "macOS", {"Mavericks", {10, 9, 3}}}, - {"13E28", {13, 3, 0}, "macOS", {"Mavericks", {10, 9, 4}}}, - {"13F34", {13, 4, 0}, "macOS", {"Mavericks", {10, 9, 5}}}, - {"13F1066", {13, 4, 0}, "macOS", {"Mavericks", {10, 9, 5}}}, - {"13F1077", {13, 4, 0}, "macOS", {"Mavericks", {10, 9, 5}}}, - {"13F1096", {13, 4, 0}, "macOS", {"Mavericks", {10, 9, 5}}}, - {"13F1112", {13, 4, 0}, "macOS", {"Mavericks", {10, 9, 5}}}, - {"13F1134", {13, 4, 0}, "macOS", {"Mavericks", {10, 9, 5}}}, - {"13F1507", {13, 4, 0}, "macOS", {"Mavericks", {10, 9, 5}}}, - {"13F1603", {13, 4, 0}, "macOS", {"Mavericks", {10, 9, 5}}}, - {"13F1712", {13, 4, 0}, "macOS", {"Mavericks", {10, 9, 5}}}, - {"13F1808", {13, 4, 0}, "macOS", {"Mavericks", {10, 9, 5}}}, - {"13F1911", {13, 4, 0}, "macOS", {"Mavericks", {10, 9, 5}}}, - {"14A389", {14, 0, 0}, "macOS", {"Yosemite", {10, 10, 0}}}, - {"14B25", {14, 0, 0}, "macOS", {"Yosemite", {10, 10, 1}}}, - {"14C109", {14, 1, 0}, "macOS", {"Yosemite", {10, 10, 2}}}, - {"14C1510", {14, 1, 0}, "macOS", {"Yosemite", {10, 10, 2}}}, - {"14C2043", {14, 1, 0}, "macOS", {"Yosemite", {10, 10, 2}}}, - {"14C1514", {14, 1, 0}, "macOS", {"Yosemite", {10, 10, 2}}}, - {"14C2513", {14, 1, 0}, "macOS", {"Yosemite", {10, 10, 2}}}, - {"14D131", {14, 3, 0}, "macOS", {"Yosemite", {10, 10, 3}}}, - {"14D136", {14, 3, 0}, "macOS", {"Yosemite", {10, 10, 3}}}, - {"14E46", {14, 4, 0}, "macOS", {"Yosemite", {10, 10, 4}}}, - {"14F27", {14, 5, 0}, "macOS", {"Yosemite", {10, 10, 5}}}, - {"14F1021", {14, 5, 0}, "macOS", {"Yosemite", {10, 10, 5}}}, - {"14F1505", {14, 5, 0}, "macOS", {"Yosemite", {10, 10, 5}}}, - {"14F1509", {14, 5, 0}, "macOS", {"Yosemite", {10, 10, 5}}}, - {"14F1605", {14, 5, 0}, "macOS", {"Yosemite", {10, 10, 5}}}, - {"14F1713", {14, 5, 0}, "macOS", {"Yosemite", {10, 10, 5}}}, - {"14F1808", {14, 5, 0}, "macOS", {"Yosemite", {10, 10, 5}}}, - {"14F1909", {14, 5, 0}, "macOS", {"Yosemite", {10, 10, 5}}}, - {"14F1912", {14, 5, 0}, "macOS", {"Yosemite", {10, 10, 5}}}, - {"14F2009", {14, 5, 0}, "macOS", {"Yosemite", {10, 10, 5}}}, - {"14F2109", {14, 5, 0}, "macOS", {"Yosemite", {10, 10, 5}}}, - {"14F2315", {14, 5, 0}, "macOS", {"Yosemite", {10, 10, 5}}}, - {"14F2411", {14, 5, 0}, "macOS", {"Yosemite", {10, 10, 5}}}, - {"14F2511", {14, 5, 0}, "macOS", {"Yosemite", {10, 10, 5}}}, - {"15A284", {15, 0, 0}, "macOS", {"El Capitan", {10, 11, 0}}}, - {"15B42", {15, 0, 0}, "macOS", {"El Capitan", {10, 11, 1}}}, - {"15C50", {15, 2, 0}, "macOS", {"El Capitan", {10, 11, 2}}}, - {"15D21", {15, 3, 0}, "macOS", {"El Capitan", {10, 11, 3}}}, - {"15E65", {15, 4, 0}, "macOS", {"El Capitan", {10, 11, 4}}}, - {"15F34", {15, 5, 0}, "macOS", {"El Capitan", {10, 11, 5}}}, - {"15G31", {15, 6, 0}, "macOS", {"El Capitan", {10, 11, 6}}}, - {"15G1004", {15, 6, 0}, "macOS", {"El Capitan", {10, 11, 6}}}, - {"15G1011", {15, 6, 0}, "macOS", {"El Capitan", {10, 11, 6}}}, - {"15G1108", {15, 6, 0}, "macOS", {"El Capitan", {10, 11, 6}}}, - {"15G1212", {15, 6, 0}, "macOS", {"El Capitan", {10, 11, 6}}}, - {"15G1217", {15, 6, 0}, "macOS", {"El Capitan", {10, 11, 6}}}, - {"15G1421", {15, 6, 0}, "macOS", {"El Capitan", {10, 11, 6}}}, - {"15G1510", {15, 6, 0}, "macOS", {"El Capitan", {10, 11, 6}}}, - {"15G1611", {15, 6, 0}, "macOS", {"El Capitan", {10, 11, 6}}}, - {"15G17023", {15, 6, 0}, "macOS", {"El Capitan", {10, 11, 6}}}, - {"15G18013", {15, 6, 0}, "macOS", {"El Capitan", {10, 11, 6}}}, - {"15G19009", {15, 6, 0}, "macOS", {"El Capitan", {10, 11, 6}}}, - {"15G20015", {15, 6, 0}, "macOS", {"El Capitan", {10, 11, 6}}}, - {"15G21013", {15, 6, 0}, "macOS", {"El Capitan", {10, 11, 6}}}, - {"15G22010", {15, 6, 0}, "macOS", {"El Capitan", {10, 11, 6}}}, - {"16A323", {16, 0, 0}, "macOS", {"Sierra", {10, 12, 0}}}, - {"16B2555", {16, 1, 0}, "macOS", {"Sierra", {10, 12, 1}}}, - {"16B2657", {16, 1, 0}, "macOS", {"Sierra", {10, 12, 1}}}, - {"16C67", {16, 3, 0}, "macOS", {"Sierra", {10, 12, 2}}}, - {"16C68", {16, 3, 0}, "macOS", {"Sierra", {10, 12, 2}}}, - {"16D32", {16, 4, 0}, "macOS", {"Sierra", {10, 12, 3}}}, - {"16E195", {16, 5, 0}, "macOS", {"Sierra", {10, 12, 4}}}, - {"16F73", {16, 6, 0}, "macOS", {"Sierra", {10, 12, 5}}}, - {"16F2073", {16, 6, 0}, "macOS", {"Sierra", {10, 12, 5}}}, - {"16G29", {16, 7, 0}, "macOS", {"Sierra", {10, 12, 6}}}, - {"16G1036", {16, 7, 0}, "macOS", {"Sierra", {10, 12, 6}}}, - {"16G1114", {16, 7, 0}, "macOS", {"Sierra", {10, 12, 6}}}, - {"16G1212", {16, 7, 0}, "macOS", {"Sierra", {10, 12, 6}}}, - {"16G1314", {16, 7, 0}, "macOS", {"Sierra", {10, 12, 6}}}, - {"16G1408", {16, 7, 0}, "macOS", {"Sierra", {10, 12, 6}}}, - {"16G1510", {16, 7, 0}, "macOS", {"Sierra", {10, 12, 6}}}, - {"16G1618", {16, 7, 0}, "macOS", {"Sierra", {10, 12, 6}}}, - {"16G1710", {16, 7, 0}, "macOS", {"Sierra", {10, 12, 6}}}, - {"16G1815", {16, 7, 0}, "macOS", {"Sierra", {10, 12, 6}}}, - {"16G1917", {16, 7, 0}, "macOS", {"Sierra", {10, 12, 6}}}, - {"16G1918", {16, 7, 0}, "macOS", {"Sierra", {10, 12, 6}}}, - {"16G2016", {16, 7, 0}, "macOS", {"Sierra", {10, 12, 6}}}, - {"16G2127", {16, 7, 0}, "macOS", {"Sierra", {10, 12, 6}}}, - {"16G2128", {16, 7, 0}, "macOS", {"Sierra", {10, 12, 6}}}, - {"16G2136", {16, 7, 0}, "macOS", {"Sierra", {10, 12, 6}}}, - {"17A365", {17, 0, 0}, "macOS", {"High Sierra", {10, 13, 0}}}, - {"17A405", {17, 0, 0}, "macOS", {"High Sierra", {10, 13, 0}}}, - {"17B48", {17, 2, 0}, "macOS", {"High Sierra", {10, 13, 1}}}, - {"17B1002", {17, 2, 0}, "macOS", {"High Sierra", {10, 13, 1}}}, - {"17B1003", {17, 2, 0}, "macOS", {"High Sierra", {10, 13, 1}}}, - {"17C88", {17, 3, 0}, "macOS", {"High Sierra", {10, 13, 2}}}, - {"17C89", {17, 3, 0}, "macOS", {"High Sierra", {10, 13, 2}}}, - {"17C205", {17, 3, 0}, "macOS", {"High Sierra", {10, 13, 2}}}, - {"17C2205", {17, 3, 0}, "macOS", {"High Sierra", {10, 13, 2}}}, - {"17D47", {17, 4, 0}, "macOS", {"High Sierra", {10, 13, 3}}}, - {"17D2047", {17, 4, 0}, "macOS", {"High Sierra", {10, 13, 3}}}, - {"17D102", {17, 4, 0}, "macOS", {"High Sierra", {10, 13, 3}}}, - {"17D2102", {17, 4, 0}, "macOS", {"High Sierra", {10, 13, 3}}}, - {"17E199", {17, 5, 0}, "macOS", {"High Sierra", {10, 13, 4}}}, - {"17E202", {17, 5, 0}, "macOS", {"High Sierra", {10, 13, 4}}}, - {"17F77", {17, 6, 0}, "macOS", {"High Sierra", {10, 13, 5}}}, - {"17G65", {17, 7, 0}, "macOS", {"High Sierra", {10, 13, 6}}}, - {"17G2208", {17, 7, 0}, "macOS", {"High Sierra", {10, 13, 6}}}, - {"17G2307", {17, 7, 0}, "macOS", {"High Sierra", {10, 13, 6}}}, - {"17G3025", {17, 7, 0}, "macOS", {"High Sierra", {10, 13, 6}}}, - {"17G4015", {17, 7, 0}, "macOS", {"High Sierra", {10, 13, 6}}}, - {"17G5019", {17, 7, 0}, "macOS", {"High Sierra", {10, 13, 6}}}, - {"17G6029", {17, 7, 0}, "macOS", {"High Sierra", {10, 13, 6}}}, - {"17G6030", {17, 7, 0}, "macOS", {"High Sierra", {10, 13, 6}}}, - {"17G7024", {17, 7, 0}, "macOS", {"High Sierra", {10, 13, 6}}}, - {"17G8029", {17, 7, 0}, "macOS", {"High Sierra", {10, 13, 6}}}, - {"17G8030", {17, 7, 0}, "macOS", {"High Sierra", {10, 13, 6}}}, - {"17G8037", {17, 7, 0}, "macOS", {"High Sierra", {10, 13, 6}}}, - {"17G9016", {17, 7, 0}, "macOS", {"High Sierra", {10, 13, 6}}}, - {"17G10021", {17, 7, 0}, "macOS", {"High Sierra", {10, 13, 6}}}, - {"17G11023", {17, 7, 0}, "macOS", {"High Sierra", {10, 13, 6}}}, - {"17G12034", {17, 7, 0}, "macOS", {"High Sierra", {10, 13, 6}}}, - {"17G13033", {17, 7, 0}, "macOS", {"High Sierra", {10, 13, 6}}}, - {"17G13035", {17, 7, 0}, "macOS", {"High Sierra", {10, 13, 6}}}, - {"17G14019", {17, 7, 0}, "macOS", {"High Sierra", {10, 13, 6}}}, - {"17G14033", {17, 7, 0}, "macOS", {"High Sierra", {10, 13, 6}}}, - {"17G14042", {17, 7, 0}, "macOS", {"High Sierra", {10, 13, 6}}}, - {"18A391", {18, 0, 0}, "macOS", {"Mojave", {10, 14, 0}}}, - {"18B75", {18, 2, 0}, "macOS", {"Mojave", {10, 14, 1}}}, - {"18B2107", {18, 2, 0}, "macOS", {"Mojave", {10, 14, 1}}}, - {"18B3094", {18, 2, 0}, "macOS", {"Mojave", {10, 14, 1}}}, - {"18C54", {18, 2, 0}, "macOS", {"Mojave", {10, 14, 2}}}, - {"18D42", {18, 2, 0}, "macOS", {"Mojave", {10, 14, 3}}}, - {"18D43", {18, 2, 0}, "macOS", {"Mojave", {10, 14, 3}}}, - {"18D109", {18, 2, 0}, "macOS", {"Mojave", {10, 14, 3}}}, - {"18E226", {18, 5, 0}, "macOS", {"Mojave", {10, 14, 4}}}, - {"18E227", {18, 5, 0}, "macOS", {"Mojave", {10, 14, 4}}}, - {"18F132", {18, 6, 0}, "macOS", {"Mojave", {10, 14, 5}}}, - {"18G84", {18, 7, 0}, "macOS", {"Mojave", {10, 14, 6}}}, - {"18G87", {18, 7, 0}, "macOS", {"Mojave", {10, 14, 6}}}, - {"18G95", {18, 7, 0}, "macOS", {"Mojave", {10, 14, 6}}}, - {"18G103", {18, 7, 0}, "macOS", {"Mojave", {10, 14, 6}}}, - {"18G1012", {18, 7, 0}, "macOS", {"Mojave", {10, 14, 6}}}, - {"18G2022", {18, 7, 0}, "macOS", {"Mojave", {10, 14, 6}}}, - {"18G3020", {18, 7, 0}, "macOS", {"Mojave", {10, 14, 6}}}, - {"18G4032", {18, 7, 0}, "macOS", {"Mojave", {10, 14, 6}}}, - {"18G5033", {18, 7, 0}, "macOS", {"Mojave", {10, 14, 6}}}, - {"18G6020", {18, 7, 0}, "macOS", {"Mojave", {10, 14, 6}}}, - {"18G6032", {18, 7, 0}, "macOS", {"Mojave", {10, 14, 6}}}, - {"18G6042", {18, 7, 0}, "macOS", {"Mojave", {10, 14, 6}}}, - {"18G7016", {18, 7, 0}, "macOS", {"Mojave", {10, 14, 6}}}, - {"18G8012", {18, 7, 0}, "macOS", {"Mojave", {10, 14, 6}}}, - {"18G8022", {18, 7, 0}, "macOS", {"Mojave", {10, 14, 6}}}, - {"18G9028", {18, 7, 0}, "macOS", {"Mojave", {10, 14, 6}}}, - {"18G9216", {18, 7, 0}, "macOS", {"Mojave", {10, 14, 6}}}, - {"18G9323", {18, 7, 0}, "macOS", {"Mojave", {10, 14, 6}}}, - {"19A583", {19, 0, 0}, "macOS", {"Catalina", {10, 15, 0}}}, - {"19A602", {19, 0, 0}, "macOS", {"Catalina", {10, 15, 0}}}, - {"19A603", {19, 0, 0}, "macOS", {"Catalina", {10, 15, 0}}}, - {"19B88", {19, 0, 0}, "macOS", {"Catalina", {10, 15, 1}}}, - {"19C57", {19, 2, 0}, "macOS", {"Catalina", {10, 15, 2}}}, - {"19C58", {19, 2, 0}, "macOS", {"Catalina", {10, 15, 2}}}, - {"19D76", {19, 3, 0}, "macOS", {"Catalina", {10, 15, 3}}}, - {"19E266", {19, 4, 0}, "macOS", {"Catalina", {10, 15, 4}}}, - {"19E287", {19, 4, 0}, "macOS", {"Catalina", {10, 15, 4}}}, - {"19F96", {19, 5, 0}, "macOS", {"Catalina", {10, 15, 5}}}, - {"19F101", {19, 5, 0}, "macOS", {"Catalina", {10, 15, 5}}}, - {"19G73", {19, 6, 0}, "macOS", {"Catalina", {10, 15, 6}}}, - {"19G2021", {19, 6, 0}, "macOS", {"Catalina", {10, 15, 6}}}, - {"19H2", {19, 6, 0}, "macOS", {"Catalina", {10, 15, 7}}}, - {"19H4", {19, 6, 0}, "macOS", {"Catalina", {10, 15, 7}}}, - {"19H15", {19, 6, 0}, "macOS", {"Catalina", {10, 15, 7}}}, - {"19H114", {19, 6, 0}, "macOS", {"Catalina", {10, 15, 7}}}, - {"19H512", {19, 6, 0}, "macOS", {"Catalina", {10, 15, 7}}}, - {"19H524", {19, 6, 0}, "macOS", {"Catalina", {10, 15, 7}}}, - {"19H1030", {19, 6, 0}, "macOS", {"Catalina", {10, 15, 7}}}, - {"19H1217", {19, 6, 0}, "macOS", {"Catalina", {10, 15, 7}}}, - {"19H1323", {19, 6, 0}, "macOS", {"Catalina", {10, 15, 7}}}, - {"19H1417", {19, 6, 0}, "macOS", {"Catalina", {10, 15, 7}}}, - {"19H1419", {19, 6, 0}, "macOS", {"Catalina", {10, 15, 7}}}, - {"19H1519", {19, 6, 0}, "macOS", {"Catalina", {10, 15, 7}}}, - {"19H1615", {19, 6, 0}, "macOS", {"Catalina", {10, 15, 7}}}, - {"19H1713", {19, 6, 0}, "macOS", {"Catalina", {10, 15, 7}}}, - {"19H1715", {19, 6, 0}, "macOS", {"Catalina", {10, 15, 7}}}, - {"19H1824", {19, 6, 0}, "macOS", {"Catalina", {10, 15, 7}}}, - {"19H1922", {19, 6, 0}, "macOS", {"Catalina", {10, 15, 7}}}, - {"19H2026", {19, 6, 0}, "macOS", {"Catalina", {10, 15, 7}}}, - {"20A2411", {20, 1, 0}, "macOS", {"Big Sur", {11, 0, 0}}}, - {"20B29", {20, 1, 0}, "macOS", {"Big Sur", {11, 0, 1}}}, - {"20B50", {20, 1, 0}, "macOS", {"Big Sur", {11, 0, 1}}}, - {"20C69", {20, 2, 0}, "macOS", {"Big Sur", {11, 1, 0}}}, - {"20D64", {20, 3, 0}, "macOS", {"Big Sur", {11, 2, 0}}}, - {"20D74", {20, 3, 0}, "macOS", {"Big Sur", {11, 2, 1}}}, - {"20D75", {20, 3, 0}, "macOS", {"Big Sur", {11, 2, 1}}}, - {"20D80", {20, 3, 0}, "macOS", {"Big Sur", {11, 2, 2}}}, - {"20D91", {20, 3, 0}, "macOS", {"Big Sur", {11, 2, 3}}}, - {"20E232", {20, 4, 0}, "macOS", {"Big Sur", {11, 3, 0}}}, - {"20E241", {20, 4, 0}, "macOS", {"Big Sur", {11, 3, 1}}}, - {"20F71", {20, 5, 0}, "macOS", {"Big Sur", {11, 4, 0}}}, - {"20G71", {20, 6, 0}, "macOS", {"Big Sur", {11, 5, 0}}}, - {"20G80", {20, 6, 0}, "macOS", {"Big Sur", {11, 5, 1}}}, - {"20G95", {20, 6, 0}, "macOS", {"Big Sur", {11, 5, 2}}}, - {"20G165", {20, 6, 0}, "macOS", {"Big Sur", {11, 6, 0}}}, - {"20G224", {20, 6, 0}, "macOS", {"Big Sur", {11, 6, 1}}}, - {"20G314", {20, 6, 0}, "macOS", {"Big Sur", {11, 6, 2}}}, - {"20G415", {20, 6, 0}, "macOS", {"Big Sur", {11, 6, 3}}}, - {"20G417", {20, 6, 0}, "macOS", {"Big Sur", {11, 6, 4}}}, - {"20G527", {20, 6, 0}, "macOS", {"Big Sur", {11, 6, 5}}}, - {"20G624", {20, 6, 0}, "macOS", {"Big Sur", {11, 6, 6}}}, - {"20G630", {20, 6, 0}, "macOS", {"Big Sur", {11, 6, 7}}}, - {"20G730", {20, 6, 0}, "macOS", {"Big Sur", {11, 6, 8}}}, - {"20G817", {20, 6, 0}, "macOS", {"Big Sur", {11, 7, 0}}}, - {"20G918", {20, 6, 0}, "macOS", {"Big Sur", {11, 7, 1}}}, - {"20G1020", {20, 6, 0}, "macOS", {"Big Sur", {11, 7, 2}}}, - {"20G1116", {20, 6, 0}, "macOS", {"Big Sur", {11, 7, 3}}}, - {"20G1120", {20, 6, 0}, "macOS", {"Big Sur", {11, 7, 4}}}, - {"20G1225", {20, 6, 0}, "macOS", {"Big Sur", {11, 7, 5}}}, - {"20G1231", {20, 6, 0}, "macOS", {"Big Sur", {11, 7, 6}}}, - {"20G1345", {20, 6, 0}, "macOS", {"Big Sur", {11, 7, 7}}}, - {"20G1351", {20, 6, 0}, "macOS", {"Big Sur", {11, 7, 8}}}, - {"20G1426", {20, 6, 0}, "macOS", {"Big Sur", {11, 7, 9}}}, - {"20G1427", {20, 6, 0}, "macOS", {"Big Sur", {11, 7, 10}}}, - {"21A344", {21, 0, 1}, "macOS", {"Monterey", {12, 0, 0}}}, - {"21A559", {21, 1, 0}, "macOS", {"Monterey", {12, 0, 1}}}, - {"21C52", {21, 2, 0}, "macOS", {"Monterey", {12, 1, 0}}}, - {"21D49", {21, 3, 0}, "macOS", {"Monterey", {12, 2, 0}}}, - {"21D62", {21, 3, 0}, "macOS", {"Monterey", {12, 2, 1}}}, - {"21E230", {21, 4, 0}, "macOS", {"Monterey", {12, 3, 0}}}, - {"21E258", {21, 4, 0}, "macOS", {"Monterey", {12, 3, 1}}}, - {"21F79", {21, 5, 0}, "macOS", {"Monterey", {12, 4, 0}}}, - {"21F2081", {21, 5, 0}, "macOS", {"Monterey", {12, 4, 0}}}, - {"21F2092", {21, 5, 0}, "macOS", {"Monterey", {12, 4, 0}}}, - {"21G72", {21, 6, 0}, "macOS", {"Monterey", {12, 5, 0}}}, - {"21G83", {21, 6, 0}, "macOS", {"Monterey", {12, 5, 1}}}, - {"21G115", {21, 6, 0}, "macOS", {"Monterey", {12, 6, 0}}}, - {"21G217", {21, 6, 0}, "macOS", {"Monterey", {12, 6, 1}}}, - {"21G320", {21, 6, 0}, "macOS", {"Monterey", {12, 6, 2}}}, - {"21G419", {21, 6, 0}, "macOS", {"Monterey", {12, 6, 3}}}, - {"21G526", {21, 6, 0}, "macOS", {"Monterey", {12, 6, 4}}}, - {"21G531", {21, 6, 0}, "macOS", {"Monterey", {12, 6, 5}}}, - {"21G646", {21, 6, 0}, "macOS", {"Monterey", {12, 6, 6}}}, - {"21G651", {21, 6, 0}, "macOS", {"Monterey", {12, 6, 7}}}, - {"21G725", {21, 6, 0}, "macOS", {"Monterey", {12, 6, 8}}}, - {"21G726", {21, 6, 0}, "macOS", {"Monterey", {12, 6, 9}}}, - {"21G816", {21, 6, 0}, "macOS", {"Monterey", {12, 7, 0}}}, - {"21G920", {21, 6, 0}, "macOS", {"Monterey", {12, 7, 1}}}, - {"21G1974", {21, 6, 0}, "macOS", {"Monterey", {12, 7, 2}}}, - {"22A380", {13, 0, 0}, "macOS", {"Ventura", {22, 1, 0}}}, - {"22A400", {13, 0, 1}, "macOS", {"Ventura", {22, 1, 0}}}, - {"22C65", {13, 1, 0}, "macOS", {"Ventura", {22, 2, 0}}}, - {"22D49", {13, 2, 0}, "macOS", {"Ventura", {22, 3, 0}}}, - {"22D68", {13, 2, 1}, "macOS", {"Ventura", {22, 3, 0}}}, - {"22E252", {13, 3, 0}, "macOS", {"Ventura", {22, 4, 0}}}, - {"22E261", {13, 3, 1}, "macOS", {"Ventura", {22, 4, 0}}}, - {"22F66", {13, 4, 0}, "macOS", {"Ventura", {22, 5, 0}}}, - {"22F82", {13, 4, 1}, "macOS", {"Ventura", {22, 5, 0}}}, - {"22E772610a", {13, 4, 1}, "macOS", {"Ventura", {22, 5, 0}}}, - {"22F770820d", {13, 4, 1}, "macOS", {"Ventura", {22, 5, 0}}}, - {"22G74", {13, 5, 0}, "macOS", {"Ventura", {22, 6, 0}}}, - {"22G90", {13, 5, 1}, "macOS", {"Ventura", {22, 6, 0}}}, - {"22G91", {13, 5, 2}, "macOS", {"Ventura", {22, 6, 0}}}, - {"22G120", {13, 6, 0}, "macOS", {"Ventura", {22, 6, 0}}}, - {"22G313", {13, 6, 1}, "macOS", {"Ventura", {22, 6, 0}}}, - {"22G320", {13, 6, 2}, "macOS", {"Ventura", {22, 6, 0}}}, - {"23A344", {23, 0, 0}, "macOS", {"Sonoma", {14, 0, 0}}}, - {"23B74", {23, 1, 0}, "macOS", {"Sonoma", {14, 1, 0}}}, - {"23B81", {23, 1, 0}, "macOS", {"Sonoma", {14, 1, 1}}}, - {"23B2082", {23, 1, 0}, "macOS", {"Sonoma", {14, 1, 1}}}, - {"23B92", {23, 1, 0}, "macOS", {"Sonoma", {14, 1, 2}}}, - {"23B2091", {23, 1, 0}, "macOS", {"Sonoma", {14, 1, 2}}}, - {"23C64", {23, 2, 0}, "macOS", {"Sonoma", {14, 2, 0}}}, - {"23C71", {23, 2, 0}, "macOS", {"Sonoma", {14, 2, 1}}}, - {"23D56", {23, 3, 0}, "macOS", {"Sonoma", {14, 3, 0}}}, - {"23D60", {23, 3, 0}, "macOS", {"Sonoma", {14, 3, 1}}}, - {"23E214", {23, 4, 0}, "macOS", {"Sonoma", {14, 4, 0}}}, - {"23E224", {23, 4, 0}, "macOS", {"Sonoma", {14, 4, 1}}}, - {"23F79", {23, 5, 0}, "macOS", {"Sonoma", {14, 5, 0}}}, - {"23G80", {23, 6, 0}, "macOS", {"Sonoma", {14, 6, 0}}}, - {"23G93", {23, 6, 0}, "macOS", {"Sonoma", {14, 6, 1}}}, - {"23H124", {23, 6, 0}, "macOS", {"Sonoma", {14, 7, 0}}}, - {"24A335", {24, 0, 0}, "macOS", {"Sequoia", {15, 0, 0}}}, - {"24A348", {24, 0, 0}, "macOS", {"Sequoia", {15, 0, 1}}}, + gbString sw_vers = gb_string_make(heap_allocator(), ""); + if (!system_exec_command_line_app_output("sw_vers --productVersion", &sw_vers)) { + gb_printf("macOS Unknown\n"); + return; + } + + uint32_t major, minor, patch; + + if (sscanf(cast(const char *)sw_vers, "%u.%u.%u", &major, &minor, &patch) != 3) { + gb_printf("macOS Unknown\n"); + return; + } + + switch (major) { + case 15: gb_printf("macOS Sequoia"); break; + case 14: gb_printf("macOS Sonoma"); break; + case 13: gb_printf("macOS Ventura"); break; + case 12: gb_printf("macOS Monterey"); break; + case 11: gb_printf("macOS Big Sur"); break; + case 10: + { + switch (minor) { + case 15: gb_printf("macOS Catalina"); break; + case 14: gb_printf("macOS Mojave"); break; + case 13: gb_printf("macOS High Sierra"); break; + case 12: gb_printf("macOS Sierra"); break; + case 11: gb_printf("OS X El Capitan"); break; + case 10: gb_printf("OS X Yosemite"); break; + default: gb_printf("macOS Unknown"); + }; + break; + } + default: + gb_printf("macOS Unknown"); }; + gb_printf(" %d.%d.%d (build ", major, minor, patch); b32 build_found = 1; b32 darwin_found = 1; - uint32_t major, minor, patch; #define MACOS_VERSION_BUFFER_SIZE 100 char build_buffer[MACOS_VERSION_BUFFER_SIZE]; @@ -943,81 +581,25 @@ gb_internal void report_os_info() { int darwin_mibs[] = { CTL_KERN, KERN_OSRELEASE }; if (sysctl(darwin_mibs, 2, darwin_buffer, &darwin_buffer_size, NULL, 0) == -1) { - gb_printf("macOS Unknown\n"); - return; + darwin_found = 0; } else { if (sscanf(darwin_buffer, "%u.%u.%u", &major, &minor, &patch) != 3) { darwin_found = 0; } } - // Scan table for match on BUILD - int macos_release_count = sizeof(macos_release_map) / sizeof(macos_release_map[0]); - Darwin_To_Release build_match = {}; - Darwin_To_Release kernel_match = {}; - - for (int build = 0; build < macos_release_count; build++) { - Darwin_To_Release rel = macos_release_map[build]; - - // Do we have an exact match on the BUILD? - if (gb_strcmp(rel.build, (const char *)build_buffer) == 0) { - build_match = rel; - break; - } - - // Do we have an exact Darwin match? - if (rel.darwin[0] == major && rel.darwin[1] == minor && rel.darwin[2] == patch) { - kernel_match = rel; - } - - // Major kernel version needs to match exactly, - if (rel.darwin[0] == major) { - // No major version match yet. - if (!kernel_match.os_name) { - kernel_match = rel; - } - if (minor >= rel.darwin[1]) { - kernel_match = rel; - if (patch >= rel.darwin[2]) { - kernel_match = rel; - } - } - } - } - - Darwin_To_Release match = {}; - if(!build_match.build) { - match = kernel_match; + if (build_found) { + gb_printf("%s, kernel ", build_buffer); } else { - match = build_match; + gb_printf("Unknown, kernel "); } - if (match.os_name) { - gb_printf("%s %s %d", match.os_name, match.release.name, match.release.version[0]); - if (match.release.version[1] > 0 || match.release.version[2] > 0) { - gb_printf(".%d", match.release.version[1]); - } - if (match.release.version[2] > 0) { - gb_printf(".%d", match.release.version[2]); - } - if (build_found) { - gb_printf(" (build: %s, kernel: %d.%d.%d)\n", build_buffer, match.darwin[0], match.darwin[1], match.darwin[2]); - } else { - gb_printf(" (build: %s?, kernel: %d.%d.%d)\n", match.build, match.darwin[0], match.darwin[1], match.darwin[2]); - } - return; + if (darwin_found) { + gb_printf("%s)\n", darwin_buffer); + } else { + gb_printf("Unknown)\n"); } - if (build_found && darwin_found) { - gb_printf("macOS Unknown (build: %s, kernel: %d.%d.%d)\n", build_buffer, major, minor, patch); - return; - } else if (build_found) { - gb_printf("macOS Unknown (build: %s)\n", build_buffer); - return; - } else if (darwin_found) { - gb_printf("macOS Unknown (kernel: %d.%d.%d)\n", major, minor, patch); - return; - } #elif defined(GB_SYSTEM_OPENBSD) || defined(GB_SYSTEM_NETBSD) struct utsname un; diff --git a/src/build_cpuid.cpp b/src/build_cpuid.cpp new file mode 100644 index 000000000..b7ba5dcdf --- /dev/null +++ b/src/build_cpuid.cpp @@ -0,0 +1,35 @@ +#if !defined(GB_COMPILER_MSVC) + #if defined(GB_CPU_X86) + #include + #endif +#endif + +gb_internal void odin_cpuid(int leaf, int result[]) { + #if defined(GB_CPU_ARM) || defined(GB_CPU_RISCV) + return; + + #elif defined(GB_CPU_X86) + + #if defined(GB_COMPILER_MSVC) + __cpuid(result, leaf); + #else + __get_cpuid(leaf, (unsigned int*)&result[0], (unsigned int*)&result[1], (unsigned int*)&result[2], (unsigned int*)&result[3]); + #endif + + #endif +} + +gb_internal bool should_use_march_native() { + #if !defined(GB_CPU_X86) + return false; + + #else + + int cpu[4]; + odin_cpuid(0x1, &cpu[0]); // Get feature information in ECX + EDX + + bool have_popcnt = cpu[2] & (1 << 23); // bit 23 in ECX = popcnt + return !have_popcnt; + + #endif +} \ No newline at end of file diff --git a/src/build_settings.cpp b/src/build_settings.cpp index e4413b1fe..15cc4f71d 100644 --- a/src/build_settings.cpp +++ b/src/build_settings.cpp @@ -2,6 +2,7 @@ #include #include #endif +#include "build_cpuid.cpp" // #if defined(GB_SYSTEM_WINDOWS) // #define DEFAULT_TO_THREADED_CHECKER @@ -343,6 +344,22 @@ struct BuildCacheData { bool copy_already_done; }; + +enum LinkerChoice : i32 { + Linker_Invalid = -1, + Linker_Default = 0, + Linker_lld, + Linker_radlink, + + Linker_COUNT, +}; + +String linker_choices[Linker_COUNT] = { + str_lit("default"), + str_lit("lld"), + str_lit("radlink"), +}; + // This stores the information for the specify architecture of this build struct BuildContext { // Constants @@ -418,12 +435,13 @@ struct BuildContext { bool no_rpath; bool no_entry_point; bool no_thread_local; - bool use_lld; bool cross_compiling; bool different_os; bool keep_object_files; bool disallow_do; + LinkerChoice linker_choice; + StringSet custom_attributes; bool strict_style; @@ -449,6 +467,7 @@ struct BuildContext { BuildCacheData build_cache_data; bool internal_no_inline; + bool internal_by_value; bool no_threaded_checker; @@ -1870,7 +1889,7 @@ gb_internal bool init_build_paths(String init_filename) { return false; } - if (!build_context.use_lld && find_result.vs_exe_path.len == 0) { + if (build_context.linker_choice == Linker_Default && find_result.vs_exe_path.len == 0) { gb_printf_err("link.exe not found.\n"); return false; } diff --git a/src/check_builtin.cpp b/src/check_builtin.cpp index 85e532940..2a4734cde 100644 --- a/src/check_builtin.cpp +++ b/src/check_builtin.cpp @@ -2565,6 +2565,10 @@ gb_internal bool check_builtin_procedure(CheckerContext *c, Operand *operand, As case BuiltinProc_swizzle: { // swizzle :: proc(v: [N]T, ..int) -> [M]T + if (!operand->type) { + return false; + } + Type *original_type = operand->type; Type *type = base_type(original_type); i64 max_count = 0; @@ -2922,6 +2926,10 @@ gb_internal bool check_builtin_procedure(CheckerContext *c, Operand *operand, As // imag :: proc(x: type) -> float_type Operand *x = operand; + if (!x->type) { + return false; + } + if (is_type_untyped(x->type)) { if (x->mode == Addressing_Constant) { if (is_type_numeric(x->type)) { @@ -2982,6 +2990,10 @@ gb_internal bool check_builtin_procedure(CheckerContext *c, Operand *operand, As // kmag :: proc(x: type) -> float_type Operand *x = operand; + if (!x->type) { + return false; + } + if (is_type_untyped(x->type)) { if (x->mode == Addressing_Constant) { if (is_type_numeric(x->type)) { @@ -3031,6 +3043,10 @@ gb_internal bool check_builtin_procedure(CheckerContext *c, Operand *operand, As case BuiltinProc_conj: { // conj :: proc(x: type) -> type Operand *x = operand; + if (!x->type) { + return false; + } + Type *t = x->type; Type *elem = core_array_type(t); @@ -3071,10 +3087,14 @@ gb_internal bool check_builtin_procedure(CheckerContext *c, Operand *operand, As } case BuiltinProc_expand_values: { + if (!operand->type) { + return false; + } + Type *type = base_type(operand->type); if (!is_type_struct(type) && !is_type_array(type)) { gbString type_str = type_to_string(operand->type); - error(call, "Expected a struct or array type, got '%s'", type_str); + error(call, "Expected a struct or array type to 'expand_values', got '%s'", type_str); gb_string_free(type_str); return false; } @@ -3110,8 +3130,13 @@ gb_internal bool check_builtin_procedure(CheckerContext *c, Operand *operand, As check_multi_expr_or_type(c, operand, ce->args[0]); + if (!operand->type) { + return false; + } + Type *original_type = operand->type; Type *type = base_type(operand->type); + if (operand->mode == Addressing_Type && is_type_enumerated_array(type)) { // Okay } else if (!is_type_ordered(type) || !(is_type_numeric(type) || is_type_string(type))) { @@ -3184,6 +3209,10 @@ gb_internal bool check_builtin_procedure(CheckerContext *c, Operand *operand, As return false; } + if (ce->args.count <= 1) { + error(call, "Too few arguments for 'min', two or more are required"); + return false; + } bool all_constant = operand->mode == Addressing_Constant; @@ -3278,6 +3307,10 @@ gb_internal bool check_builtin_procedure(CheckerContext *c, Operand *operand, As check_multi_expr_or_type(c, operand, ce->args[0]); + if (!operand->type) { + return false; + } + Type *original_type = operand->type; Type *type = base_type(operand->type); @@ -3357,6 +3390,11 @@ gb_internal bool check_builtin_procedure(CheckerContext *c, Operand *operand, As gb_string_free(type_str); return false; } + + if (ce->args.count <= 1) { + error(call, "Too few arguments for 'max', two or more are required"); + return false; + } bool all_constant = operand->mode == Addressing_Constant; @@ -3448,6 +3486,10 @@ gb_internal bool check_builtin_procedure(CheckerContext *c, Operand *operand, As case BuiltinProc_abs: { // abs :: proc(n: numeric) -> numeric + if (!operand->type) { + return false; + } + if (!(is_type_numeric(operand->type) && !is_type_array(operand->type))) { gbString type_str = type_to_string(operand->type); error(call, "Expected a numeric type to 'abs', got '%s'", type_str); @@ -3503,6 +3545,10 @@ gb_internal bool check_builtin_procedure(CheckerContext *c, Operand *operand, As case BuiltinProc_clamp: { // clamp :: proc(a, min, max: ordered) -> ordered + if (!operand->type) { + return false; + } + Type *type = operand->type; if (!is_type_ordered(type) || !(is_type_numeric(type) || is_type_string(type))) { gbString type_str = type_to_string(operand->type); diff --git a/src/check_expr.cpp b/src/check_expr.cpp index 71cd29817..648010f2d 100644 --- a/src/check_expr.cpp +++ b/src/check_expr.cpp @@ -897,20 +897,6 @@ gb_internal i64 check_distance_between_types(CheckerContext *c, Operand *operand } } - if (is_type_relative_pointer(dst)) { - i64 score = check_distance_between_types(c, operand, dst->RelativePointer.pointer_type, allow_array_programming); - if (score >= 0) { - return score+2; - } - } - - if (is_type_relative_multi_pointer(dst)) { - i64 score = check_distance_between_types(c, operand, dst->RelativeMultiPointer.pointer_type, allow_array_programming); - if (score >= 0) { - return score+2; - } - } - if (is_type_proc(dst)) { if (are_types_identical(src, dst)) { return 3; @@ -1052,12 +1038,6 @@ gb_internal AstPackage *get_package_of_type(Type *type) { case Type_DynamicArray: type = type->DynamicArray.elem; continue; - case Type_RelativePointer: - type = type->RelativePointer.pointer_type; - continue; - case Type_RelativeMultiPointer: - type = type->RelativeMultiPointer.pointer_type; - continue; } return nullptr; } @@ -3561,6 +3541,8 @@ gb_internal bool check_transmute(CheckerContext *c, Ast *node, Operand *o, Type return false; } + Operand src = *o; + Type *src_t = o->type; Type *dst_t = t; Type *src_bt = base_type(src_t); @@ -3649,7 +3631,8 @@ gb_internal bool check_transmute(CheckerContext *c, Ast *node, Operand *o, Type // identical casts that cannot be foreseen or otherwise // forbidden, so just skip them. if (forbid_identical && check_vet_flags(c) & VetFlag_Cast && - (c->curr_proc_sig == nullptr || !is_type_polymorphic(c->curr_proc_sig))) { + (c->curr_proc_sig == nullptr || !is_type_polymorphic(c->curr_proc_sig)) && + check_is_castable_to(c, &src, dst_t)) { if (are_types_identical(src_t, dst_t)) { gbString oper_str = expr_to_string(o->expr); gbString to_type = type_to_string(dst_t); @@ -4471,8 +4454,8 @@ gb_internal ExactValue convert_exact_value_for_type(ExactValue v, Type *type) { } gb_internal void convert_to_typed(CheckerContext *c, Operand *operand, Type *target_type) { - GB_ASSERT_NOT_NULL(target_type); - if (operand->mode == Addressing_Invalid || + // GB_ASSERT_NOT_NULL(target_type); + if (target_type == nullptr || operand->mode == Addressing_Invalid || operand->mode == Addressing_Type || is_type_typed(operand->type) || target_type == t_invalid) { @@ -4997,8 +4980,12 @@ gb_internal ExactValue get_constant_field_single(CheckerContext *c, ExactValue v if (success_) *success_ = true; if (finish_) *finish_ = false; return tav.value; + } else if (is_type_proc(tav.type)) { + if (success_) *success_ = true; + if (finish_) *finish_ = false; + return tav.value; } else { - GB_ASSERT(is_type_untyped_nil(tav.type)); + GB_ASSERT_MSG(is_type_untyped_nil(tav.type), "%s", type_to_string(tav.type)); if (success_) *success_ = true; if (finish_) *finish_ = false; return tav.value; @@ -8232,17 +8219,6 @@ gb_internal bool check_set_index_data(Operand *o, Type *t, bool indirection, i64 } return true; - case Type_RelativeMultiPointer: - { - Type *pointer_type = base_type(t->RelativeMultiPointer.pointer_type); - GB_ASSERT(pointer_type->kind == Type_MultiPointer); - o->type = pointer_type->MultiPointer.elem; - if (o->mode != Addressing_Constant) { - o->mode = Addressing_Variable; - } - } - return true; - case Type_DynamicArray: o->type = t->DynamicArray.elem; if (o->mode != Addressing_Constant) { @@ -8481,6 +8457,15 @@ gb_internal ExprKind check_implicit_selector_expr(CheckerContext *c, Operand *o, error(node, "Undeclared name '%.*s' for type '%s'", LIT(name), typ); check_did_you_mean_type(name, bt->Enum.fields); + } else if (is_type_bit_set(th) && is_type_enum(th->BitSet.elem)) { + ERROR_BLOCK(); + + gbString typ = type_to_string(th); + gbString str = expr_to_string(node); + error(node, "Cannot convert enum value to '%s'", typ); + error_line("\tSuggestion: Did you mean '{ %s }'?\n", str); + gb_string_free(typ); + gb_string_free(str); } else { gbString typ = type_to_string(th); gbString str = expr_to_string(node); @@ -10616,8 +10601,6 @@ gb_internal ExprKind check_index_expr(CheckerContext *c, Operand *o, Ast *node, // Okay } else if (is_type_string(t)) { // Okay - } else if (is_type_relative_multi_pointer(t)) { - // Okay } else if (is_type_matrix(t)) { // Okay } else { @@ -10772,11 +10755,6 @@ gb_internal ExprKind check_slice_expr(CheckerContext *c, Operand *o, Ast *node, } break; - case Type_RelativeMultiPointer: - valid = true; - o->type = type_deref(o->type); - break; - case Type_EnumeratedArray: { gbString str = expr_to_string(o->expr); @@ -10853,16 +10831,6 @@ gb_internal ExprKind check_slice_expr(CheckerContext *c, Operand *o, Ast *node, x[i:n] -> []T */ o->type = alloc_type_slice(t->MultiPointer.elem); - } else if (t->kind == Type_RelativeMultiPointer && se->high != nullptr) { - /* - x[:] -> [^]T - x[i:] -> [^]T - x[:n] -> []T - x[i:n] -> []T - */ - Type *pointer_type = base_type(t->RelativeMultiPointer.pointer_type); - GB_ASSERT(pointer_type->kind == Type_MultiPointer); - o->type = alloc_type_slice(pointer_type->MultiPointer.elem); } @@ -11223,22 +11191,6 @@ gb_internal ExprKind check_expr_base_internal(CheckerContext *c, Operand *o, Ast } else if (t->kind == Type_SoaPointer) { o->mode = Addressing_SoaVariable; o->type = type_deref(t); - } else if (t->kind == Type_RelativePointer) { - if (o->mode != Addressing_Variable) { - gbString str = expr_to_string(o->expr); - gbString typ = type_to_string(o->type); - error(o->expr, "Cannot dereference relative pointer '%s' of type '%s' as it does not have a variable addressing mode", str, typ); - gb_string_free(typ); - gb_string_free(str); - } - - // NOTE(bill): This is required because when dereferencing, the original type has been lost - add_type_info_type(c, o->type); - - Type *ptr_type = base_type(t->RelativePointer.pointer_type); - GB_ASSERT(ptr_type->kind == Type_Pointer); - o->mode = Addressing_Variable; - o->type = ptr_type->Pointer.elem; } else { gbString str = expr_to_string(o->expr); gbString typ = type_to_string(o->type); diff --git a/src/check_stmt.cpp b/src/check_stmt.cpp index 2418fcc5c..02ad72388 100644 --- a/src/check_stmt.cpp +++ b/src/check_stmt.cpp @@ -2605,6 +2605,7 @@ gb_internal void check_for_stmt(CheckerContext *ctx, Ast *node, u32 mod_flags) { if (cond && cond->kind == Ast_BinaryExpr && cond->BinaryExpr.left && cond->BinaryExpr.right && cond->BinaryExpr.op.kind == Token_GtEq && + type_of_expr(cond->BinaryExpr.left) != nullptr && is_type_unsigned(type_of_expr(cond->BinaryExpr.left)) && cond->BinaryExpr.right->tav.value.kind == ExactValue_Integer && is_exact_value_zero(cond->BinaryExpr.right->tav.value)) { @@ -2612,6 +2613,7 @@ gb_internal void check_for_stmt(CheckerContext *ctx, Ast *node, u32 mod_flags) { } else if (cond && cond->kind == Ast_BinaryExpr && cond->BinaryExpr.left && cond->BinaryExpr.right && cond->BinaryExpr.op.kind == Token_LtEq && + type_of_expr(cond->BinaryExpr.right) != nullptr && is_type_unsigned(type_of_expr(cond->BinaryExpr.right)) && cond->BinaryExpr.left->tav.value.kind == ExactValue_Integer && is_exact_value_zero(cond->BinaryExpr.left->tav.value)) { diff --git a/src/check_type.cpp b/src/check_type.cpp index bbeff9ca7..13a6125ca 100644 --- a/src/check_type.cpp +++ b/src/check_type.cpp @@ -2366,8 +2366,7 @@ gb_internal Type *check_get_results(CheckerContext *ctx, Scope *scope, Ast *_res } gb_internal void check_procedure_param_polymorphic_type(CheckerContext *ctx, Type *type, Ast *type_expr) { - GB_ASSERT_NOT_NULL(type_expr); - if (type == nullptr || ctx->in_polymorphic_specialization) { return; } + if (type == nullptr || type_expr == nullptr || ctx->in_polymorphic_specialization) { return; } if (!is_type_polymorphic_record_unspecialized(type)) { return; } bool invalid_polymorpic_type_use = false; @@ -3517,41 +3516,8 @@ gb_internal bool check_type_internal(CheckerContext *ctx, Ast *e, Type **type, T case_end; case_ast_node(rt, RelativeType, e); - GB_ASSERT(rt->tag->kind == Ast_CallExpr); - ast_node(ce, CallExpr, rt->tag); - - Type *base_integer = nullptr; - - if (ce->args.count != 1) { - error(rt->type, "#relative expected 1 type argument, got %td", ce->args.count); - } else { - base_integer = check_type(ctx, ce->args[0]); - if (!is_type_integer(base_integer)) { - error(rt->type, "#relative base types must be an integer"); - base_integer = nullptr; - } else if (type_size_of(base_integer) > 64) { - error(rt->type, "#relative base integer types be less than or equal to 64-bits"); - base_integer = nullptr; - } - } - - Type *relative_type = nullptr; - Type *base_type = check_type(ctx, rt->type); - if (!is_type_pointer(base_type) && !is_type_multi_pointer(base_type)) { - error(rt->type, "#relative types can only be a pointer or multi-pointer"); - relative_type = base_type; - } else if (base_integer == nullptr) { - relative_type = base_type; - } else { - if (is_type_pointer(base_type)) { - relative_type = alloc_type_relative_pointer(base_type, base_integer); - } else if (is_type_multi_pointer(base_type)) { - relative_type = alloc_type_relative_multi_pointer(base_type, base_integer); - } - } - GB_ASSERT(relative_type != nullptr); - - *type = relative_type; + error(e, "#relative types have been removed from the compiler. Prefer \"core:relative\"."); + *type = t_invalid; set_base_type(named_type, *type); return true; case_end; diff --git a/src/checker.cpp b/src/checker.cpp index 76f996648..b7cf343f8 100644 --- a/src/checker.cpp +++ b/src/checker.cpp @@ -2186,16 +2186,6 @@ gb_internal void add_type_info_type_internal(CheckerContext *c, Type *t) { add_type_info_type_internal(c, bt->SimdVector.elem); break; - case Type_RelativePointer: - add_type_info_type_internal(c, bt->RelativePointer.pointer_type); - add_type_info_type_internal(c, bt->RelativePointer.base_integer); - break; - - case Type_RelativeMultiPointer: - add_type_info_type_internal(c, bt->RelativeMultiPointer.pointer_type); - add_type_info_type_internal(c, bt->RelativeMultiPointer.base_integer); - break; - case Type_Matrix: add_type_info_type_internal(c, bt->Matrix.elem); break; @@ -2441,16 +2431,6 @@ gb_internal void add_min_dep_type_info(Checker *c, Type *t) { add_min_dep_type_info(c, bt->SimdVector.elem); break; - case Type_RelativePointer: - add_min_dep_type_info(c, bt->RelativePointer.pointer_type); - add_min_dep_type_info(c, bt->RelativePointer.base_integer); - break; - - case Type_RelativeMultiPointer: - add_min_dep_type_info(c, bt->RelativeMultiPointer.pointer_type); - add_min_dep_type_info(c, bt->RelativeMultiPointer.base_integer); - break; - case Type_Matrix: add_min_dep_type_info(c, bt->Matrix.elem); break; @@ -3075,8 +3055,6 @@ gb_internal void init_core_type_info(Checker *c) { t_type_info_map = find_core_type(c, str_lit("Type_Info_Map")); t_type_info_bit_set = find_core_type(c, str_lit("Type_Info_Bit_Set")); t_type_info_simd_vector = find_core_type(c, str_lit("Type_Info_Simd_Vector")); - t_type_info_relative_pointer = find_core_type(c, str_lit("Type_Info_Relative_Pointer")); - t_type_info_relative_multi_pointer = find_core_type(c, str_lit("Type_Info_Relative_Multi_Pointer")); t_type_info_matrix = find_core_type(c, str_lit("Type_Info_Matrix")); t_type_info_soa_pointer = find_core_type(c, str_lit("Type_Info_Soa_Pointer")); t_type_info_bit_field = find_core_type(c, str_lit("Type_Info_Bit_Field")); @@ -3105,8 +3083,6 @@ gb_internal void init_core_type_info(Checker *c) { t_type_info_map_ptr = alloc_type_pointer(t_type_info_map); t_type_info_bit_set_ptr = alloc_type_pointer(t_type_info_bit_set); t_type_info_simd_vector_ptr = alloc_type_pointer(t_type_info_simd_vector); - t_type_info_relative_pointer_ptr = alloc_type_pointer(t_type_info_relative_pointer); - t_type_info_relative_multi_pointer_ptr = alloc_type_pointer(t_type_info_relative_multi_pointer); t_type_info_matrix_ptr = alloc_type_pointer(t_type_info_matrix); t_type_info_soa_pointer_ptr = alloc_type_pointer(t_type_info_soa_pointer); t_type_info_bit_field_ptr = alloc_type_pointer(t_type_info_bit_field); diff --git a/src/docs_format.cpp b/src/docs_format.cpp index ca6ecb5c2..6378971d0 100644 --- a/src/docs_format.cpp +++ b/src/docs_format.cpp @@ -79,8 +79,7 @@ enum OdinDocTypeKind : u32 { OdinDocType_SOAStructFixed = 17, OdinDocType_SOAStructSlice = 18, OdinDocType_SOAStructDynamic = 19, - OdinDocType_RelativePointer = 20, - OdinDocType_RelativeMultiPointer = 21, + OdinDocType_MultiPointer = 22, OdinDocType_Matrix = 23, OdinDocType_SoaPointer = 24, diff --git a/src/docs_writer.cpp b/src/docs_writer.cpp index 835dfdff1..341b3fa6b 100644 --- a/src/docs_writer.cpp +++ b/src/docs_writer.cpp @@ -776,24 +776,6 @@ gb_internal OdinDocTypeIndex odin_doc_type(OdinDocWriter *w, Type *type) { doc_type.types = odin_doc_type_as_slice(w, type->SimdVector.elem); // TODO(bill): break; - case Type_RelativePointer: - doc_type.kind = OdinDocType_RelativePointer; - { - OdinDocTypeIndex types[2] = {}; - types[0] = odin_doc_type(w, type->RelativePointer.pointer_type); - types[1] = odin_doc_type(w, type->RelativePointer.base_integer); - doc_type.types = odin_write_slice(w, types, gb_count_of(types)); - } - break; - case Type_RelativeMultiPointer: - doc_type.kind = OdinDocType_RelativeMultiPointer; - { - OdinDocTypeIndex types[2] = {}; - types[0] = odin_doc_type(w, type->RelativeMultiPointer.pointer_type); - types[1] = odin_doc_type(w, type->RelativeMultiPointer.base_integer); - doc_type.types = odin_write_slice(w, types, gb_count_of(types)); - } - break; case Type_Matrix: doc_type.kind = OdinDocType_Matrix; diff --git a/src/exact_value.cpp b/src/exact_value.cpp index 5d6016ecc..ceaed84c1 100644 --- a/src/exact_value.cpp +++ b/src/exact_value.cpp @@ -370,7 +370,11 @@ gb_internal ExactValue exact_value_from_basic_literal(TokenKind kind, String con } case Token_Rune: { Rune r = GB_RUNE_INVALID; - utf8_decode(string.text, string.len, &r); + if (string.len == 1) { + r = cast(Rune)string.text[0]; + } else { + utf8_decode(string.text, string.len, &r); + } return exact_value_i64(r); } } diff --git a/src/linker.cpp b/src/linker.cpp index 1ffec3bf7..261d6e7a4 100644 --- a/src/linker.cpp +++ b/src/linker.cpp @@ -167,8 +167,10 @@ gb_internal i32 linker_stage(LinkerData *gen) { if (is_windows) { String section_name = str_lit("msvc-link"); - if (build_context.use_lld) { - section_name = str_lit("lld-link"); + switch (build_context.linker_choice) { + case Linker_Default: break; + case Linker_lld: section_name = str_lit("lld-link"); break; + case Linker_radlink: section_name = str_lit("rad-link"); break; } timings_start_section(timings, section_name); @@ -304,7 +306,48 @@ gb_internal i32 linker_stage(LinkerData *gen) { String windows_sdk_bin_path = path_to_string(heap_allocator(), build_context.build_paths[BuildPath_Win_SDK_Bin_Path]); defer (gb_free(heap_allocator(), windows_sdk_bin_path.text)); - if (!build_context.use_lld) { // msvc + switch (build_context.linker_choice) { + case Linker_lld: + result = system_exec_command_line_app("msvc-lld-link", + "\"%.*s\\bin\\lld-link\" %s -OUT:\"%.*s\" %s " + "/nologo /incremental:no /opt:ref /subsystem:%.*s " + "%.*s " + "%.*s " + "%s " + "", + LIT(build_context.ODIN_ROOT), object_files, LIT(output_filename), + link_settings, + LIT(build_context.ODIN_WINDOWS_SUBSYSTEM), + LIT(build_context.link_flags), + LIT(build_context.extra_linker_flags), + lib_str + ); + + if (result) { + return result; + } + break; + case Linker_radlink: + result = system_exec_command_line_app("msvc-rad-link", + "\"%.*s\\bin\\radlink\" %s -OUT:\"%.*s\" %s " + "/nologo /incremental:no /opt:ref /subsystem:%.*s " + "%.*s " + "%.*s " + "%s " + "", + LIT(build_context.ODIN_ROOT), object_files, LIT(output_filename), + link_settings, + LIT(build_context.ODIN_WINDOWS_SUBSYSTEM), + LIT(build_context.link_flags), + LIT(build_context.extra_linker_flags), + lib_str + ); + + if (result) { + return result; + } + break; + default: { // msvc String res_path = quote_path(heap_allocator(), build_context.build_paths[BuildPath_RES]); String rc_path = quote_path(heap_allocator(), build_context.build_paths[BuildPath_RC]); defer (gb_free(heap_allocator(), res_path.text)); @@ -365,25 +408,8 @@ gb_internal i32 linker_stage(LinkerData *gen) { if (result) { return result; } - } else { // lld - result = system_exec_command_line_app("msvc-lld-link", - "\"%.*s\\bin\\lld-link\" %s -OUT:\"%.*s\" %s " - "/nologo /incremental:no /opt:ref /subsystem:%.*s " - "%.*s " - "%.*s " - "%s " - "", - LIT(build_context.ODIN_ROOT), object_files, LIT(output_filename), - link_settings, - LIT(build_context.ODIN_WINDOWS_SUBSYSTEM), - LIT(build_context.link_flags), - LIT(build_context.extra_linker_flags), - lib_str - ); - - if (result) { - return result; - } + break; + } } } else { timings_start_section(timings, str_lit("ld-link")); @@ -679,7 +705,7 @@ gb_internal i32 linker_stage(LinkerData *gen) { link_command_line = gb_string_append_fmt(link_command_line, " %.*s ", LIT(build_context.extra_linker_flags)); link_command_line = gb_string_append_fmt(link_command_line, " %s ", link_settings); - if (build_context.use_lld) { + if (build_context.linker_choice == Linker_lld) { link_command_line = gb_string_append_fmt(link_command_line, " -fuse-ld=lld"); result = system_exec_command_line_app("lld-link", link_command_line); } else { diff --git a/src/llvm_backend.hpp b/src/llvm_backend.hpp index 68f95cb03..e84ffd1cd 100644 --- a/src/llvm_backend.hpp +++ b/src/llvm_backend.hpp @@ -57,6 +57,10 @@ #define LB_USE_NEW_PASS_SYSTEM 0 #endif +#if LLVM_VERSION_MAJOR >= 19 +#define LLVMDIBuilderInsertDeclareAtEnd(...) LLVMDIBuilderInsertDeclareRecordAtEnd(__VA_ARGS__) +#endif + gb_internal bool lb_use_new_pass_system(void) { return LB_USE_NEW_PASS_SYSTEM; } @@ -75,7 +79,6 @@ enum lbAddrKind { lbAddr_Context, lbAddr_SoaVariable, - lbAddr_RelativePointer, lbAddr_Swizzle, lbAddr_SwizzleLarge, @@ -103,9 +106,6 @@ struct lbAddr { lbValue index; Ast *node; } index_set; - struct { - bool deref; - } relative; struct { Type *type; u8 count; // 2, 3, or 4 components @@ -741,4 +741,4 @@ gb_global char const *llvm_linkage_strings[] = { "linker private weak linkage" }; -#define ODIN_METADATA_IS_PACKED str_lit("odin-is-packed") \ No newline at end of file +#define ODIN_METADATA_IS_PACKED str_lit("odin-is-packed") diff --git a/src/llvm_backend_debug.cpp b/src/llvm_backend_debug.cpp index 5cc79dcc8..464f7065c 100644 --- a/src/llvm_backend_debug.cpp +++ b/src/llvm_backend_debug.cpp @@ -920,17 +920,6 @@ gb_internal LLVMMetadataRef lb_debug_type_internal(lbModule *m, Type *type) { elem, subscripts, gb_count_of(subscripts)); } - case Type_RelativePointer: { - LLVMMetadataRef base_integer = lb_debug_type(m, type->RelativePointer.base_integer); - gbString name = type_to_string(type, temporary_allocator()); - return LLVMDIBuilderCreateTypedef(m->debug_builder, base_integer, name, gb_string_length(name), nullptr, 0, nullptr, cast(u32)(8*type_align_of(type))); - } - case Type_RelativeMultiPointer: { - LLVMMetadataRef base_integer = lb_debug_type(m, type->RelativeMultiPointer.base_integer); - gbString name = type_to_string(type, temporary_allocator()); - return LLVMDIBuilderCreateTypedef(m->debug_builder, base_integer, name, gb_string_length(name), nullptr, 0, nullptr, cast(u32)(8*type_align_of(type))); - } - case Type_Matrix: { LLVMMetadataRef subscripts[1] = {}; subscripts[0] = LLVMDIBuilderGetOrCreateSubrange(m->debug_builder, diff --git a/src/llvm_backend_expr.cpp b/src/llvm_backend_expr.cpp index 8ad44035d..1247eed76 100644 --- a/src/llvm_backend_expr.cpp +++ b/src/llvm_backend_expr.cpp @@ -4200,30 +4200,6 @@ gb_internal lbAddr lb_build_addr_index_expr(lbProcedure *p, Ast *expr) { return lb_addr(v); } - case Type_RelativeMultiPointer: { - lbAddr rel_ptr_addr = {}; - if (deref) { - lbValue rel_ptr_ptr = lb_build_expr(p, ie->expr); - rel_ptr_addr = lb_addr(rel_ptr_ptr); - } else { - rel_ptr_addr = lb_build_addr(p, ie->expr); - } - lbValue rel_ptr = lb_relative_pointer_to_pointer(p, rel_ptr_addr); - - lbValue index = lb_build_expr(p, ie->index); - index = lb_emit_conv(p, index, t_int); - lbValue v = {}; - - Type *pointer_type = base_type(t->RelativeMultiPointer.pointer_type); - GB_ASSERT(pointer_type->kind == Type_MultiPointer); - Type *elem = pointer_type->MultiPointer.elem; - - LLVMValueRef indices[1] = {index.value}; - v.value = LLVMBuildGEP2(p->builder, lb_type(p->module, elem), rel_ptr.value, indices, 1, ""); - v.type = alloc_type_pointer(elem); - return lb_addr(v); - } - case Type_DynamicArray: { lbValue dynamic_array = {}; dynamic_array = lb_build_expr(p, ie->expr); @@ -4333,13 +4309,6 @@ gb_internal lbAddr lb_build_addr_slice_expr(lbProcedure *p, Ast *expr) { return slice; } - case Type_RelativePointer: - GB_PANIC("TODO(bill): Type_RelativePointer should be handled above already on the lb_addr_load"); - break; - case Type_RelativeMultiPointer: - GB_PANIC("TODO(bill): Type_RelativeMultiPointer should be handled above already on the lb_addr_load"); - break; - case Type_DynamicArray: { Type *elem_type = type->DynamicArray.elem; Type *slice_type = alloc_type_slice(elem_type); @@ -4605,7 +4574,11 @@ gb_internal lbAddr lb_build_addr_compound_lit(lbProcedure *p, Ast *expr) { auto const &f = fields[i]; LLVMValueRef mask = LLVMConstInt(lit, 1, false); - mask = LLVMConstShl(mask, LLVMConstInt(lit, f.bit_size, false)); + #if LLVM_VERSION_MAJOR >= 19 + mask = LLVMBuildShl(p->builder, mask, LLVMConstInt(lit, f.bit_size, false), ""); + #else + mask = LLVMConstShl(mask, LLVMConstInt(lit, f.bit_size, false)); + #endif mask = LLVMConstSub(mask, LLVMConstInt(lit, 1, false)); LLVMValueRef elem = values[i].value; @@ -4653,7 +4626,11 @@ gb_internal lbAddr lb_build_addr_compound_lit(lbProcedure *p, Ast *expr) { bits_to_set -= mask_width; LLVMValueRef mask = LLVMConstInt(vt, 1, false); - mask = LLVMConstShl(mask, LLVMConstInt(vt, mask_width, false)); + #if LLVM_VERSION_MAJOR >= 19 + mask = LLVMBuildShl(p->builder, mask, LLVMConstInt(vt, mask_width, false), ""); + #else + mask = LLVMConstShl(mask, LLVMConstInt(vt, mask_width, false)); + #endif mask = LLVMConstSub(mask, LLVMConstInt(vt, 1, false)); LLVMValueRef to_set = LLVMBuildAnd(p->builder, val, mask, ""); @@ -5343,11 +5320,7 @@ gb_internal lbAddr lb_build_addr_internal(lbProcedure *p, Ast *expr) { case_ast_node(de, DerefExpr, expr); Type *t = type_of_expr(de->expr); - if (is_type_relative_pointer(t)) { - lbAddr addr = lb_build_addr(p, de->expr); - addr.relative.deref = true; - return addr; - } else if (is_type_soa_pointer(t)) { + if (is_type_soa_pointer(t)) { lbValue value = lb_build_expr(p, de->expr); lbValue ptr = lb_emit_struct_ev(p, value, 0); lbValue idx = lb_emit_struct_ev(p, value, 1); diff --git a/src/llvm_backend_general.cpp b/src/llvm_backend_general.cpp index 686724f6a..bab330da7 100644 --- a/src/llvm_backend_general.cpp +++ b/src/llvm_backend_general.cpp @@ -409,14 +409,6 @@ gb_internal lbModule *lb_module_of_entity(lbGenerator *gen, Entity *e) { gb_internal lbAddr lb_addr(lbValue addr) { lbAddr v = {lbAddr_Default, addr}; - if (addr.type != nullptr && is_type_relative_pointer(type_deref(addr.type))) { - GB_ASSERT(is_type_pointer(addr.type)); - v.kind = lbAddr_RelativePointer; - } else if (addr.type != nullptr && is_type_relative_multi_pointer(type_deref(addr.type))) { - GB_ASSERT(is_type_pointer(addr.type) || - is_type_multi_pointer(addr.type)); - v.kind = lbAddr_RelativePointer; - } return v; } @@ -501,42 +493,6 @@ gb_internal Type *lb_addr_type(lbAddr const &addr) { return type_deref(addr.addr.type); } - -gb_internal lbValue lb_relative_pointer_to_pointer(lbProcedure *p, lbAddr const &addr) { - GB_ASSERT(addr.kind == lbAddr_RelativePointer); - - Type *t = base_type(lb_addr_type(addr)); - GB_ASSERT(is_type_relative_pointer(t) || is_type_relative_multi_pointer(t)); - - Type *pointer_type = nullptr; - Type *base_integer = nullptr; - if (t->kind == Type_RelativePointer) { - pointer_type = t->RelativePointer.pointer_type; - base_integer = t->RelativePointer.base_integer; - } else if (t->kind == Type_RelativeMultiPointer) { - pointer_type = t->RelativeMultiPointer.pointer_type; - base_integer = t->RelativeMultiPointer.base_integer; - } - - lbValue ptr = lb_emit_conv(p, addr.addr, t_uintptr); - lbValue offset = lb_emit_conv(p, ptr, alloc_type_pointer(base_integer)); - offset = lb_emit_load(p, offset); - - if (!is_type_unsigned(base_integer)) { - offset = lb_emit_conv(p, offset, t_i64); - } - offset = lb_emit_conv(p, offset, t_uintptr); - lbValue absolute_ptr = lb_emit_arith(p, Token_Add, ptr, offset, t_uintptr); - absolute_ptr = lb_emit_conv(p, absolute_ptr, pointer_type); - - lbValue cond = lb_emit_comp(p, Token_CmpEq, offset, lb_const_nil(p->module, base_integer)); - - // NOTE(bill): nil check - lbValue nil_ptr = lb_const_nil(p->module, pointer_type); - lbValue final_ptr = lb_emit_select(p, cond, nil_ptr, absolute_ptr); - return final_ptr; -} - gb_internal lbValue lb_make_soa_pointer(lbProcedure *p, Type *type, lbValue const &addr, lbValue const &index) { lbAddr v = lb_add_local_generated(p, type, false); lbValue ptr = lb_emit_struct_ep(p, v.addr, 0); @@ -557,9 +513,6 @@ gb_internal lbValue lb_addr_get_ptr(lbProcedure *p, lbAddr const &addr) { case lbAddr_Map: return lb_internal_dynamic_map_get_ptr(p, addr.addr, addr.map.key); - case lbAddr_RelativePointer: - return lb_relative_pointer_to_pointer(p, addr); - case lbAddr_SoaVariable: { Type *soa_ptr_type = alloc_type_soa_pointer(lb_addr_type(addr)); @@ -584,9 +537,6 @@ gb_internal lbValue lb_addr_get_ptr(lbProcedure *p, lbAddr const &addr) { gb_internal lbValue lb_build_addr_ptr(lbProcedure *p, Ast *expr) { lbAddr addr = lb_build_addr(p, expr); - if (addr.kind == lbAddr_RelativePointer) { - return addr.addr; - } return lb_addr_get_ptr(p, addr); } @@ -819,10 +769,6 @@ gb_internal void lb_addr_store(lbProcedure *p, lbAddr addr, lbValue value) { value.value = LLVMConstNull(lb_type(p->module, t)); } - if (addr.kind == lbAddr_RelativePointer && addr.relative.deref) { - addr = lb_addr(lb_address_from_load(p, lb_addr_load(p, addr))); - } - if (addr.kind == lbAddr_BitField) { lbValue dst = addr.addr; if (is_type_endian_big(addr.bitfield.type)) { @@ -860,44 +806,6 @@ gb_internal void lb_addr_store(lbProcedure *p, lbAddr addr, lbValue value) { lb_emit_runtime_call(p, "__write_bits", args); } return; - } else if (addr.kind == lbAddr_RelativePointer) { - Type *rel_ptr = base_type(lb_addr_type(addr)); - GB_ASSERT(rel_ptr->kind == Type_RelativePointer || - rel_ptr->kind == Type_RelativeMultiPointer); - Type *pointer_type = nullptr; - Type *base_integer = nullptr; - - if (rel_ptr->kind == Type_RelativePointer) { - pointer_type = rel_ptr->RelativePointer.pointer_type; - base_integer = rel_ptr->RelativePointer.base_integer; - } else if (rel_ptr->kind == Type_RelativeMultiPointer) { - pointer_type = rel_ptr->RelativeMultiPointer.pointer_type; - base_integer = rel_ptr->RelativeMultiPointer.base_integer; - } - - value = lb_emit_conv(p, value, pointer_type); - - GB_ASSERT(is_type_pointer(addr.addr.type)); - lbValue ptr = lb_emit_conv(p, addr.addr, t_uintptr); - lbValue val_ptr = lb_emit_conv(p, value, t_uintptr); - lbValue offset = {}; - offset.value = LLVMBuildSub(p->builder, val_ptr.value, ptr.value, ""); - offset.type = t_uintptr; - - if (!is_type_unsigned(base_integer)) { - offset = lb_emit_conv(p, offset, t_i64); - } - offset = lb_emit_conv(p, offset, base_integer); - - lbValue offset_ptr = lb_emit_conv(p, addr.addr, alloc_type_pointer(base_integer)); - offset = lb_emit_select(p, - lb_emit_comp(p, Token_CmpEq, val_ptr, lb_const_nil(p->module, t_uintptr)), - lb_const_nil(p->module, base_integer), - offset - ); - LLVMBuildStore(p->builder, offset.value, offset_ptr.value); - return; - } else if (addr.kind == lbAddr_Map) { lb_internal_dynamic_map_set(p, addr.addr, addr.map.type, addr.map.key, value, p->curr_stmt); return; @@ -1246,46 +1154,6 @@ gb_internal lbValue lb_addr_load(lbProcedure *p, lbAddr const &addr) { } return r; - } else if (addr.kind == lbAddr_RelativePointer) { - Type *rel_ptr = base_type(lb_addr_type(addr)); - Type *base_integer = nullptr; - Type *pointer_type = nullptr; - GB_ASSERT(rel_ptr->kind == Type_RelativePointer || - rel_ptr->kind == Type_RelativeMultiPointer); - - if (rel_ptr->kind == Type_RelativePointer) { - base_integer = rel_ptr->RelativePointer.base_integer; - pointer_type = rel_ptr->RelativePointer.pointer_type; - } else if (rel_ptr->kind == Type_RelativeMultiPointer) { - base_integer = rel_ptr->RelativeMultiPointer.base_integer; - pointer_type = rel_ptr->RelativeMultiPointer.pointer_type; - } - - lbValue ptr = lb_emit_conv(p, addr.addr, t_uintptr); - lbValue offset = lb_emit_conv(p, ptr, alloc_type_pointer(base_integer)); - offset = lb_emit_load(p, offset); - - - if (!is_type_unsigned(base_integer)) { - offset = lb_emit_conv(p, offset, t_i64); - } - offset = lb_emit_conv(p, offset, t_uintptr); - lbValue absolute_ptr = lb_emit_arith(p, Token_Add, ptr, offset, t_uintptr); - absolute_ptr = lb_emit_conv(p, absolute_ptr, pointer_type); - - lbValue cond = lb_emit_comp(p, Token_CmpEq, offset, lb_const_nil(p->module, base_integer)); - - // NOTE(bill): nil check - lbValue nil_ptr = lb_const_nil(p->module, pointer_type); - lbValue final_ptr = {}; - final_ptr.type = absolute_ptr.type; - final_ptr.value = LLVMBuildSelect(p->builder, cond.value, nil_ptr.value, absolute_ptr.value, ""); - - if (rel_ptr->kind == Type_RelativeMultiPointer) { - return final_ptr; - } - return lb_emit_load(p, final_ptr); - } else if (addr.kind == lbAddr_Map) { Type *map_type = base_type(type_deref(addr.addr.type)); GB_ASSERT(map_type->kind == Type_Map); @@ -2378,13 +2246,6 @@ gb_internal LLVMTypeRef lb_type_internal(lbModule *m, Type *type) { case Type_SimdVector: return LLVMVectorType(lb_type(m, type->SimdVector.elem), cast(unsigned)type->SimdVector.count); - - case Type_RelativePointer: - return lb_type_internal(m, type->RelativePointer.base_integer); - case Type_RelativeMultiPointer: - return lb_type_internal(m, type->RelativeMultiPointer.base_integer); - - case Type_Matrix: { @@ -2740,7 +2601,7 @@ general_end:; GB_ASSERT(p->decl_block != p->curr_block); i64 max_align = gb_max(lb_alignof(src_type), lb_alignof(dst_type)); - max_align = gb_max(max_align, 4); + max_align = gb_max(max_align, 16); LLVMValueRef ptr = llvm_alloca(p, dst_type, max_align); diff --git a/src/llvm_backend_proc.cpp b/src/llvm_backend_proc.cpp index 5aee5b639..fee825a2f 100644 --- a/src/llvm_backend_proc.cpp +++ b/src/llvm_backend_proc.cpp @@ -579,6 +579,8 @@ gb_internal void lb_begin_procedure_body(lbProcedure *p) { p->raw_input_parameters = array_make(permanent_allocator(), raw_input_parameters_count); LLVMGetParams(p->value, p->raw_input_parameters.data); + bool is_odin_cc = is_calling_convention_odin(ft->calling_convention); + unsigned param_index = 0; for_array(i, params->variables) { Entity *e = params->variables[i]; @@ -613,9 +615,26 @@ gb_internal void lb_begin_procedure_body(lbProcedure *p) { } } else if (arg_type->kind == lbArg_Indirect) { if (e->token.string.len != 0 && !is_blank_ident(e->token.string)) { + i64 sz = type_size_of(e->type); + bool do_callee_copy = false; + + if (is_odin_cc) { + do_callee_copy = sz <= 16; + if (build_context.internal_by_value) { + do_callee_copy = true; + } + } + lbValue ptr = {}; ptr.value = LLVMGetParam(p->value, param_offset+param_index); ptr.type = alloc_type_pointer(e->type); + + if (do_callee_copy) { + lbValue new_ptr = lb_add_local_generated(p, e->type, false).addr; + lb_mem_copy_non_overlapping(p, new_ptr, ptr, lb_const_int(p->module, t_uint, sz)); + ptr = new_ptr; + } + lb_add_entity(p->module, e, ptr); lb_add_debug_param_variable(p, ptr.value, e->type, e->token, param_index+1, p->decl_block); } diff --git a/src/llvm_backend_stmt.cpp b/src/llvm_backend_stmt.cpp index 06d66ac80..9a5f25712 100644 --- a/src/llvm_backend_stmt.cpp +++ b/src/llvm_backend_stmt.cpp @@ -201,6 +201,8 @@ gb_internal void lb_open_scope(lbProcedure *p, Scope *s) { } } + GB_ASSERT(s != nullptr); + p->curr_scope = s; p->scope_index += 1; array_add(&p->scope_stack, s); @@ -221,6 +223,10 @@ gb_internal void lb_close_scope(lbProcedure *p, lbDeferExitKind kind, lbBlock *b } + if (p->curr_scope) { + p->curr_scope = p->curr_scope->parent; + } + p->scope_index -= 1; array_pop(&p->scope_stack); } diff --git a/src/llvm_backend_type.cpp b/src/llvm_backend_type.cpp index f3fcc8de4..6c12b37be 100644 --- a/src/llvm_backend_type.cpp +++ b/src/llvm_backend_type.cpp @@ -61,8 +61,6 @@ gb_internal u64 lb_typeid_kind(lbModule *m, Type *type, u64 id=0) { case Type_Proc: kind = Typeid_Procedure; break; case Type_BitSet: kind = Typeid_Bit_Set; break; case Type_SimdVector: kind = Typeid_Simd_Vector; break; - 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; } @@ -950,30 +948,6 @@ gb_internal void lb_setup_type_info_data_giant_array(lbModule *m, i64 global_typ } break; - case Type_RelativePointer: - { - tag_type = t_type_info_relative_pointer; - LLVMValueRef vals[2] = { - get_type_info_ptr(m, t->RelativePointer.pointer_type), - get_type_info_ptr(m, t->RelativePointer.base_integer), - }; - - variant_value = llvm_const_named_struct(m, tag_type, vals, gb_count_of(vals)); - } - break; - - case Type_RelativeMultiPointer: - { - tag_type = t_type_info_relative_multi_pointer; - LLVMValueRef vals[2] = { - get_type_info_ptr(m, t->RelativeMultiPointer.pointer_type), - get_type_info_ptr(m, t->RelativeMultiPointer.base_integer), - }; - - variant_value = llvm_const_named_struct(m, tag_type, vals, gb_count_of(vals)); - } - break; - case Type_Matrix: { tag_type = t_type_info_matrix; diff --git a/src/llvm_backend_utility.cpp b/src/llvm_backend_utility.cpp index 6367d0118..a2a0ba4cc 100644 --- a/src/llvm_backend_utility.cpp +++ b/src/llvm_backend_utility.cpp @@ -124,8 +124,16 @@ gb_internal void lb_mem_zero_ptr(lbProcedure *p, LLVMValueRef ptr, Type *type, u switch (kind) { case LLVMStructTypeKind: case LLVMArrayTypeKind: - // NOTE(bill): Enforce zeroing through memset to make sure padding is zeroed too - lb_mem_zero_ptr_internal(p, ptr, lb_const_int(p->module, t_int, sz).value, alignment, false); + if (is_type_tuple(type)) { + // NOTE(bill): even though this should be safe, to keep ASAN happy, do not zero the implicit padding at the end + GB_ASSERT(type->kind == Type_Tuple); + i64 n = type->Tuple.variables.count-1; + i64 end_offset = type->Tuple.offsets[n] + type_size_of(type->Tuple.variables[n]->type); + lb_mem_zero_ptr_internal(p, ptr, lb_const_int(p->module, t_int, end_offset).value, alignment, false); + } else { + // NOTE(bill): Enforce zeroing through memset to make sure padding is zeroed too + lb_mem_zero_ptr_internal(p, ptr, lb_const_int(p->module, t_int, sz).value, alignment, false); + } break; default: LLVMBuildStore(p->builder, LLVMConstNull(lb_type(p->module, type)), ptr); @@ -1123,10 +1131,6 @@ gb_internal lbValue lb_emit_struct_ep(lbProcedure *p, lbValue s, i32 index) { Type *t = base_type(type_deref(s.type)); Type *result_type = nullptr; - if (is_type_relative_pointer(t)) { - s = lb_addr_get_ptr(p, lb_addr(s)); - } - if (is_type_struct(t)) { result_type = get_struct_field_type(t, index); } else if (is_type_union(t)) { @@ -1432,8 +1436,6 @@ gb_internal lbValue lb_emit_deep_field_gep(lbProcedure *p, lbValue e, Selection e = lb_emit_array_epi(p, e, index); } else if (type->kind == Type_Map) { e = lb_emit_struct_ep(p, e, index); - } else if (type->kind == Type_RelativePointer) { - e = lb_emit_struct_ep(p, e, index); } else { GB_PANIC("un-gep-able type %s", type_to_string(type)); } diff --git a/src/main.cpp b/src/main.cpp index b04ee90f1..4d85a9e72 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -89,8 +89,8 @@ gb_global Timings global_timings = {0}; #if defined(GB_SYSTEM_OSX) #include - #if LLVM_VERSION_MAJOR < 11 || (LLVM_VERSION_MAJOR > 14 && LLVM_VERSION_MAJOR < 17) || LLVM_VERSION_MAJOR > 18 - #error LLVM Version 11..=14 or =18 is required => "brew install llvm@14" + #if LLVM_VERSION_MAJOR < 11 || (LLVM_VERSION_MAJOR > 14 && LLVM_VERSION_MAJOR < 17) || LLVM_VERSION_MAJOR > 19 + #error LLVM Version 11..=14 or 17..=19 is required => "brew install llvm@14" #endif #endif @@ -328,6 +328,8 @@ enum BuildFlagKind { BuildFlag_NoRPath, BuildFlag_NoEntryPoint, BuildFlag_UseLLD, + BuildFlag_UseRADLink, + BuildFlag_Linker, BuildFlag_UseSeparateModules, BuildFlag_NoThreadedChecker, BuildFlag_ShowDebugMessages, @@ -399,6 +401,7 @@ enum BuildFlagKind { BuildFlag_InternalModulePerFile, BuildFlag_InternalCached, BuildFlag_InternalNoInline, + BuildFlag_InternalByValue, BuildFlag_Tilde, @@ -539,6 +542,8 @@ gb_internal bool parse_build_flags(Array args) { add_flag(&build_flags, BuildFlag_NoRPath, str_lit("no-rpath"), BuildFlagParam_None, Command__does_build); add_flag(&build_flags, BuildFlag_NoEntryPoint, str_lit("no-entry-point"), BuildFlagParam_None, Command__does_check &~ Command_test); add_flag(&build_flags, BuildFlag_UseLLD, str_lit("lld"), BuildFlagParam_None, Command__does_build); + add_flag(&build_flags, BuildFlag_UseRADLink, str_lit("radlink"), BuildFlagParam_None, Command__does_build); + add_flag(&build_flags, BuildFlag_Linker, str_lit("linker"), BuildFlagParam_String, Command__does_build); add_flag(&build_flags, BuildFlag_UseSeparateModules, str_lit("use-separate-modules"), BuildFlagParam_None, Command__does_build); add_flag(&build_flags, BuildFlag_NoThreadedChecker, str_lit("no-threaded-checker"), BuildFlagParam_None, Command__does_check); add_flag(&build_flags, BuildFlag_ShowDebugMessages, str_lit("show-debug-messages"), BuildFlagParam_None, Command_all); @@ -608,6 +613,7 @@ gb_internal bool parse_build_flags(Array args) { add_flag(&build_flags, BuildFlag_InternalModulePerFile, str_lit("internal-module-per-file"), BuildFlagParam_None, Command_all); add_flag(&build_flags, BuildFlag_InternalCached, str_lit("internal-cached"), BuildFlagParam_None, Command_all); add_flag(&build_flags, BuildFlag_InternalNoInline, str_lit("internal-no-inline"), BuildFlagParam_None, Command_all); + add_flag(&build_flags, BuildFlag_InternalByValue, str_lit("internal-by-value"), BuildFlagParam_None, Command_all); #if ALLOW_TILDE add_flag(&build_flags, BuildFlag_Tilde, str_lit("tilde"), BuildFlagParam_None, Command__does_build); @@ -1060,27 +1066,29 @@ gb_internal bool parse_build_flags(Array args) { } if (!found) { - struct DistanceAndTargetIndex { - isize distance; - isize target_index; - }; + if (str != "?") { + struct DistanceAndTargetIndex { + isize distance; + isize target_index; + }; - DistanceAndTargetIndex distances[gb_count_of(named_targets)] = {}; - for (isize i = 0; i < gb_count_of(named_targets); i++) { - distances[i].target_index = i; - distances[i].distance = levenstein_distance_case_insensitive(str, named_targets[i].name); - } - gb_sort_array(distances, gb_count_of(distances), gb_isize_cmp(gb_offset_of(DistanceAndTargetIndex, distance))); - - gb_printf_err("Unknown target '%.*s'\n", LIT(str)); - - if (distances[0].distance <= MAX_SMALLEST_DID_YOU_MEAN_DISTANCE) { - gb_printf_err("Did you mean:\n"); + DistanceAndTargetIndex distances[gb_count_of(named_targets)] = {}; for (isize i = 0; i < gb_count_of(named_targets); i++) { - if (distances[i].distance > MAX_SMALLEST_DID_YOU_MEAN_DISTANCE) { - break; + distances[i].target_index = i; + distances[i].distance = levenstein_distance_case_insensitive(str, named_targets[i].name); + } + gb_sort_array(distances, gb_count_of(distances), gb_isize_cmp(gb_offset_of(DistanceAndTargetIndex, distance))); + + gb_printf_err("Unknown target '%.*s'\n", LIT(str)); + + if (distances[0].distance <= MAX_SMALLEST_DID_YOU_MEAN_DISTANCE) { + gb_printf_err("Did you mean:\n"); + for (isize i = 0; i < gb_count_of(named_targets); i++) { + if (distances[i].distance > MAX_SMALLEST_DID_YOU_MEAN_DISTANCE) { + break; + } + gb_printf_err("\t%.*s\n", LIT(named_targets[distances[i].target_index].name)); } - gb_printf_err("\t%.*s\n", LIT(named_targets[distances[i].target_index].name)); } } gb_printf_err("All supported targets:\n"); @@ -1199,8 +1207,38 @@ gb_internal bool parse_build_flags(Array args) { build_context.no_thread_local = true; break; case BuildFlag_UseLLD: - build_context.use_lld = true; + gb_printf_err("Warning: Use of -lld has been deprecated in favour of -linker:lld\n"); + build_context.linker_choice = Linker_lld; break; + case BuildFlag_UseRADLink: + gb_printf_err("Warning: Use of -lld has been deprecated in favour of -linker:radlink\n"); + build_context.linker_choice = Linker_radlink; + break; + case BuildFlag_Linker: + { + GB_ASSERT(value.kind == ExactValue_String); + LinkerChoice linker_choice = Linker_Invalid; + + for (i32 i = 0; i < Linker_COUNT; i++) { + if (linker_choices[i] == value.value_string) { + linker_choice = cast(LinkerChoice)i; + break; + } + } + + if (linker_choice == Linker_Invalid) { + gb_printf_err("Invalid option for -linker:. Expected one of the following\n"); + for (i32 i = 0; i < Linker_COUNT; i++) { + gb_printf_err("\t%.*s\n", LIT(linker_choices[i])); + } + bad_flags = true; + } else { + build_context.linker_choice = linker_choice; + } + } + break; + + case BuildFlag_UseSeparateModules: build_context.use_separate_modules = true; break; @@ -1472,6 +1510,9 @@ gb_internal bool parse_build_flags(Array args) { case BuildFlag_InternalNoInline: build_context.internal_no_inline = true; break; + case BuildFlag_InternalByValue: + build_context.internal_by_value = true; + break; case BuildFlag_Tilde: build_context.tilde_backend = true; @@ -1747,7 +1788,7 @@ gb_internal void check_defines(BuildContext *bc, Checker *c) { String name = make_string_c(entry.key); ExactValue value = entry.value; GB_ASSERT(value.kind != ExactValue_Invalid); - + bool found = false; for_array(i, c->info.defineables) { Defineable *def = &c->info.defineables[i]; @@ -1776,9 +1817,9 @@ gb_internal void temp_alloc_defineable_strings(Checker *c) { gb_internal GB_COMPARE_PROC(defineables_cmp) { Defineable *x = (Defineable *)a; Defineable *y = (Defineable *)b; - + int cmp = 0; - + String x_file = get_file_path_string(x->pos.file_id); String y_file = get_file_path_string(y->pos.file_id); cmp = string_compare(x_file, y_file); @@ -1789,8 +1830,22 @@ gb_internal GB_COMPARE_PROC(defineables_cmp) { return i32_cmp(x->pos.offset, y->pos.offset); } -gb_internal void sort_defineables(Checker *c) { +gb_internal void sort_defineables_and_remove_duplicates(Checker *c) { + if (c->info.defineables.count == 0) { + return; + } gb_sort_array(c->info.defineables.data, c->info.defineables.count, defineables_cmp); + + Defineable prev = c->info.defineables[0]; + for (isize i = 1; i < c->info.defineables.count; ) { + Defineable curr = c->info.defineables[i]; + if (prev.pos == curr.pos) { + array_ordered_remove(&c->info.defineables, i); + continue; + } + prev = curr; + i++; + } } gb_internal void export_defineables(Checker *c, String path) { @@ -2125,11 +2180,18 @@ gb_internal void remove_temp_files(lbGenerator *gen) { } -gb_internal void print_show_help(String const arg0, String const &command) { +gb_internal void print_show_help(String const arg0, String command, String optional_flag = {}) { + if (command == "help" && optional_flag.len != 0 && optional_flag[0] != '-') { + command = optional_flag; + optional_flag = {}; + } + print_usage_line(0, "%.*s is a tool for managing Odin source code.", LIT(arg0)); print_usage_line(0, "Usage:"); print_usage_line(1, "%.*s %.*s [arguments]", LIT(arg0), LIT(command)); print_usage_line(0, ""); + defer (print_usage_line(0, "")); + if (command == "build") { print_usage_line(1, "build Compiles directory of .odin files as an executable."); @@ -2175,520 +2237,560 @@ gb_internal void print_show_help(String const arg0, String const &command) { bool check_only = command == "check" || strip_semicolon; bool check = run_or_build || check_only; + if (command == "help") { + doc = true; + build = true; + run_or_build = true; + test_only = true; + strip_semicolon = true; + check_only = true; + check = true; + } + print_usage_line(0, ""); print_usage_line(1, "Flags"); print_usage_line(0, ""); - if (doc) { - print_usage_line(1, "-all-packages"); - print_usage_line(2, "Generates documentation for all packages used in the current project."); + + auto const print_flag = [&optional_flag](char const *flag) -> bool { + if (optional_flag.len != 0) { + String f = make_string_c(flag); + isize i = string_index_byte(f, ':'); + if (i >= 0) { + f.len = i; + } + if (optional_flag != f) { + return false; + } + } print_usage_line(0, ""); + print_usage_line(1, flag); + return true; + }; + + + if (doc) { + if (print_flag("-all-packages")) { + print_usage_line(2, "Generates documentation for all packages used in the current project."); + } } if (test_only) { - print_usage_line(1, "-all-packages"); - print_usage_line(2, "Tests all packages imported into the given initial package."); - print_usage_line(0, ""); + if (print_flag("-all-packages")) { + print_usage_line(2, "Tests all packages imported into the given initial package."); + } } if (build) { - print_usage_line(1, "-build-mode:"); - print_usage_line(2, "Sets the build mode."); - print_usage_line(2, "Available options:"); - print_usage_line(3, "-build-mode:exe Builds as an executable."); - print_usage_line(3, "-build-mode:test Builds as an executable that executes tests."); - print_usage_line(3, "-build-mode:dll Builds as a dynamically linked library."); - print_usage_line(3, "-build-mode:shared Builds as a dynamically linked library."); - print_usage_line(3, "-build-mode:dynamic Builds as a dynamically linked library."); - print_usage_line(3, "-build-mode:lib Builds as a statically linked library."); - print_usage_line(3, "-build-mode:static Builds as a statically linked library."); - print_usage_line(3, "-build-mode:obj Builds as an object file."); - print_usage_line(3, "-build-mode:object Builds as an object file."); - print_usage_line(3, "-build-mode:assembly Builds as an assembly file."); - print_usage_line(3, "-build-mode:assembler Builds as an assembly file."); - print_usage_line(3, "-build-mode:asm Builds as an assembly file."); - print_usage_line(3, "-build-mode:llvm-ir Builds as an LLVM IR file."); - print_usage_line(3, "-build-mode:llvm Builds as an LLVM IR file."); - print_usage_line(0, ""); + if (print_flag("-build-mode:")) { + print_usage_line(2, "Sets the build mode."); + print_usage_line(2, "Available options:"); + print_usage_line(3, "-build-mode:exe Builds as an executable."); + print_usage_line(3, "-build-mode:test Builds as an executable that executes tests."); + print_usage_line(3, "-build-mode:dll Builds as a dynamically linked library."); + print_usage_line(3, "-build-mode:shared Builds as a dynamically linked library."); + print_usage_line(3, "-build-mode:dynamic Builds as a dynamically linked library."); + print_usage_line(3, "-build-mode:lib Builds as a statically linked library."); + print_usage_line(3, "-build-mode:static Builds as a statically linked library."); + print_usage_line(3, "-build-mode:obj Builds as an object file."); + print_usage_line(3, "-build-mode:object Builds as an object file."); + print_usage_line(3, "-build-mode:assembly Builds as an assembly file."); + print_usage_line(3, "-build-mode:assembler Builds as an assembly file."); + print_usage_line(3, "-build-mode:asm Builds as an assembly file."); + print_usage_line(3, "-build-mode:llvm-ir Builds as an LLVM IR file."); + print_usage_line(3, "-build-mode:llvm Builds as an LLVM IR file."); + } } if (check) { - print_usage_line(1, "-collection:="); - print_usage_line(2, "Defines a library collection used for imports."); - print_usage_line(2, "Example: -collection:shared=dir/to/shared"); - print_usage_line(2, "Usage in Code:"); - print_usage_line(3, "import \"shared:foo\""); - print_usage_line(0, ""); + if (print_flag("-collection:=")) { + print_usage_line(2, "Defines a library collection used for imports."); + print_usage_line(2, "Example: -collection:shared=dir/to/shared"); + print_usage_line(2, "Usage in Code:"); + print_usage_line(3, "import \"shared:foo\""); + } - print_usage_line(1, "-custom-attribute:"); - print_usage_line(2, "Add a custom attribute which will be ignored if it is unknown."); - print_usage_line(2, "This can be used with metaprogramming tools."); - print_usage_line(2, "Examples:"); - print_usage_line(3, "-custom-attribute:my_tag"); - print_usage_line(3, "-custom-attribute:my_tag,the_other_thing"); - print_usage_line(3, "-custom-attribute:my_tag -custom-attribute:the_other_thing"); - print_usage_line(0, ""); + if (print_flag("-custom-attribute:")) { + print_usage_line(2, "Add a custom attribute which will be ignored if it is unknown."); + print_usage_line(2, "This can be used with metaprogramming tools."); + print_usage_line(2, "Examples:"); + print_usage_line(3, "-custom-attribute:my_tag"); + print_usage_line(3, "-custom-attribute:my_tag,the_other_thing"); + print_usage_line(3, "-custom-attribute:my_tag -custom-attribute:the_other_thing"); + } } if (run_or_build) { - print_usage_line(1, "-debug"); - print_usage_line(2, "Enables debug information, and defines the global constant ODIN_DEBUG to be 'true'."); - print_usage_line(0, ""); + if (print_flag("-debug")) { + print_usage_line(2, "Enables debug information, and defines the global constant ODIN_DEBUG to be 'true'."); + } } if (check) { - print_usage_line(1, "-default-to-nil-allocator"); - print_usage_line(2, "Sets the default allocator to be the nil_allocator, an allocator which does nothing."); - print_usage_line(0, ""); + if (print_flag("-default-to-nil-allocator")) { + print_usage_line(2, "Sets the default allocator to be the nil_allocator, an allocator which does nothing."); + } - print_usage_line(1, "-define:="); - print_usage_line(2, "Defines a scalar boolean, integer or string as global constant."); - print_usage_line(2, "Example: -define:SPAM=123"); - print_usage_line(2, "Usage in code:"); - print_usage_line(3, "#config(SPAM, default_value)"); - print_usage_line(0, ""); + if (print_flag("-define:=")) { + print_usage_line(2, "Defines a scalar boolean, integer or string as global constant."); + print_usage_line(2, "Example: -define:SPAM=123"); + print_usage_line(2, "Usage in code:"); + print_usage_line(3, "#config(SPAM, default_value)"); + } } if (run_or_build) { - print_usage_line(1, "-disable-assert"); - print_usage_line(2, "Disables the code generation of the built-in run-time 'assert' procedure, and defines the global constant ODIN_DISABLE_ASSERT to be 'true'."); - print_usage_line(0, ""); + if (print_flag("-disable-assert")) { + print_usage_line(2, "Disables the code generation of the built-in run-time 'assert' procedure, and defines the global constant ODIN_DISABLE_ASSERT to be 'true'."); + } - print_usage_line(1, "-disable-red-zone"); - print_usage_line(2, "Disables red zone on a supported freestanding target."); - print_usage_line(0, ""); + if (print_flag("-disable-red-zone")) { + print_usage_line(2, "Disables red zone on a supported freestanding target."); + } } if (check) { - print_usage_line(1, "-disallow-do"); - print_usage_line(2, "Disallows the 'do' keyword in the project."); - print_usage_line(0, ""); + if (print_flag("-disallow-do")) { + print_usage_line(2, "Disallows the 'do' keyword in the project."); + } } if (doc) { - print_usage_line(1, "-doc-format"); - print_usage_line(2, "Generates documentation as the .odin-doc format (useful for external tooling)."); - print_usage_line(0, ""); + if (print_flag("-doc-format")) { + print_usage_line(2, "Generates documentation as the .odin-doc format (useful for external tooling)."); + } } if (run_or_build) { - print_usage_line(1, "-dynamic-map-calls"); - print_usage_line(2, "Uses dynamic map calls to minimize code generation at the cost of runtime execution."); - print_usage_line(0, ""); + if (print_flag("-dynamic-map-calls")) { + print_usage_line(2, "Uses dynamic map calls to minimize code generation at the cost of runtime execution."); + } } if (check) { - print_usage_line(1, "-error-pos-style:"); - print_usage_line(2, "Available options:"); - print_usage_line(3, "-error-pos-style:unix file/path:45:3:"); - print_usage_line(3, "-error-pos-style:odin file/path(45:3)"); - print_usage_line(3, "-error-pos-style:default (Defaults to 'odin'.)"); - print_usage_line(0, ""); + if (print_flag("-error-pos-style:")) { + print_usage_line(2, "Available options:"); + print_usage_line(3, "-error-pos-style:unix file/path:45:3:"); + print_usage_line(3, "-error-pos-style:odin file/path(45:3)"); + print_usage_line(3, "-error-pos-style:default (Defaults to 'odin'.)"); + } - print_usage_line(1, "-export-defineables:"); - print_usage_line(2, "Exports an overview of all the #config/#defined usages in CSV format to the given file path."); - print_usage_line(2, "Example: -export-defineables:defineables.csv"); - print_usage_line(0, ""); + if (print_flag("-export-defineables:")) { + print_usage_line(2, "Exports an overview of all the #config/#defined usages in CSV format to the given file path."); + print_usage_line(2, "Example: -export-defineables:defineables.csv"); + } - print_usage_line(1, "-export-dependencies:"); - print_usage_line(2, "Exports dependencies to one of a few formats. Requires `-export-dependencies-file`."); - print_usage_line(2, "Available options:"); - print_usage_line(3, "-export-dependencies:make Exports in Makefile format"); - print_usage_line(3, "-export-dependencies:json Exports in JSON format"); - print_usage_line(0, ""); + if (print_flag("-export-dependencies:")) { + print_usage_line(2, "Exports dependencies to one of a few formats. Requires `-export-dependencies-file`."); + print_usage_line(2, "Available options:"); + print_usage_line(3, "-export-dependencies:make Exports in Makefile format"); + print_usage_line(3, "-export-dependencies:json Exports in JSON format"); + } - print_usage_line(1, "-export-dependencies-file:"); - print_usage_line(2, "Specifies the filename for `-export-dependencies`."); - print_usage_line(2, "Example: -export-dependencies-file:dependencies.d"); - print_usage_line(0, ""); + if (print_flag("-export-dependencies-file:")) { + print_usage_line(2, "Specifies the filename for `-export-dependencies`."); + print_usage_line(2, "Example: -export-dependencies-file:dependencies.d"); + } - print_usage_line(1, "-export-timings:"); - print_usage_line(2, "Exports timings to one of a few formats. Requires `-show-timings` or `-show-more-timings`."); - print_usage_line(2, "Available options:"); - print_usage_line(3, "-export-timings:json Exports compile time stats to JSON."); - print_usage_line(3, "-export-timings:csv Exports compile time stats to CSV."); - print_usage_line(0, ""); + if (print_flag("-export-timings:")) { + print_usage_line(2, "Exports timings to one of a few formats. Requires `-show-timings` or `-show-more-timings`."); + print_usage_line(2, "Available options:"); + print_usage_line(3, "-export-timings:json Exports compile time stats to JSON."); + print_usage_line(3, "-export-timings:csv Exports compile time stats to CSV."); + } - print_usage_line(1, "-export-timings-file:"); - print_usage_line(2, "Specifies the filename for `-export-timings`."); - print_usage_line(2, "Example: -export-timings-file:timings.json"); - print_usage_line(0, ""); + if (print_flag("-export-timings-file:")) { + print_usage_line(2, "Specifies the filename for `-export-timings`."); + print_usage_line(2, "Example: -export-timings-file:timings.json"); + } } if (run_or_build) { - print_usage_line(1, "-extra-assembler-flags:"); + if (print_flag("-extra-assembler-flags:")) { print_usage_line(2, "Adds extra assembler specific flags in a string."); - print_usage_line(0, ""); + } - print_usage_line(1, "-extra-linker-flags:"); + if (print_flag("-extra-linker-flags:")) { print_usage_line(2, "Adds extra linker specific flags in a string."); - print_usage_line(0, ""); + } } if (check) { - print_usage_line(1, "-file"); - print_usage_line(2, "Tells `%.*s %.*s` to treat the given file as a self-contained package.", LIT(arg0), LIT(command)); - print_usage_line(2, "This means that `/a.odin` won't have access to `/b.odin`'s contents."); - print_usage_line(0, ""); + if (print_flag("-file")) { + print_usage_line(2, "Tells `%.*s %.*s` to treat the given file as a self-contained package.", LIT(arg0), LIT(command)); + print_usage_line(2, "This means that `/a.odin` won't have access to `/b.odin`'s contents."); + } - print_usage_line(1, "-foreign-error-procedures"); - print_usage_line(2, "States that the error procedures used in the runtime are defined in a separate translation unit."); - print_usage_line(0, ""); + if (print_flag("-foreign-error-procedures")) { + print_usage_line(2, "States that the error procedures used in the runtime are defined in a separate translation unit."); + } - print_usage_line(1, "-ignore-unknown-attributes"); - print_usage_line(2, "Ignores unknown attributes."); - print_usage_line(2, "This can be used with metaprogramming tools."); - print_usage_line(0, ""); + if (print_flag("-ignore-unknown-attributes")) { + print_usage_line(2, "Ignores unknown attributes."); + print_usage_line(2, "This can be used with metaprogramming tools."); + } } if (run_or_build) { #if defined(GB_SYSTEM_WINDOWS) - print_usage_line(1, "-ignore-vs-search"); - print_usage_line(2, "[Windows only]"); - print_usage_line(2, "Ignores the Visual Studio search for library paths."); - print_usage_line(0, ""); + if (print_flag("-ignore-vs-search")) { + print_usage_line(2, "[Windows only]"); + print_usage_line(2, "Ignores the Visual Studio search for library paths."); + } #endif } if (check) { - print_usage_line(1, "-ignore-warnings"); - print_usage_line(2, "Ignores warning messages."); - print_usage_line(0, ""); + if (print_flag("-ignore-warnings")) { + print_usage_line(2, "Ignores warning messages."); + } - print_usage_line(1, "-json-errors"); - print_usage_line(2, "Prints the error messages as json to stderr."); - print_usage_line(0, ""); + if (print_flag("-json-errors")) { + print_usage_line(2, "Prints the error messages as json to stderr."); + } } if (run_or_build) { - print_usage_line(1, "-keep-temp-files"); - print_usage_line(2, "Keeps the temporary files generated during compilation."); - print_usage_line(0, ""); + if (print_flag("-keep-temp-files")) { + print_usage_line(2, "Keeps the temporary files generated during compilation."); + } } else if (strip_semicolon) { - print_usage_line(1, "-keep-temp-files"); - print_usage_line(2, "Keeps the temporary files generated during stripping the unneeded semicolons from files."); - print_usage_line(0, ""); + if (print_flag("-keep-temp-files")) { + print_usage_line(2, "Keeps the temporary files generated during stripping the unneeded semicolons from files."); + } } if (run_or_build) { - print_usage_line(1, "-lld"); - print_usage_line(2, "Uses the LLD linker rather than the default."); - print_usage_line(0, ""); + if (print_flag("-linker:")) { + print_usage_line(2, "Specify the linker to use."); + print_usage_line(2, "Choices:"); + for (i32 i = 0; i < Linker_COUNT; i++) { + print_usage_line(3, "%.*s", LIT(linker_choices[i])); + } + } + + if (print_flag("-lld")) { + print_usage_line(2, "Uses the LLD linker rather than the default."); + } } if (check) { - print_usage_line(1, "-max-error-count:"); - print_usage_line(2, "Sets the maximum number of errors that can be displayed before the compiler terminates."); - print_usage_line(2, "Must be an integer >0."); - print_usage_line(2, "If not set, the default max error count is %d.", DEFAULT_MAX_ERROR_COLLECTOR_COUNT); - print_usage_line(0, ""); + if (print_flag("-max-error-count:")) { + print_usage_line(2, "Sets the maximum number of errors that can be displayed before the compiler terminates."); + print_usage_line(2, "Must be an integer >0."); + print_usage_line(2, "If not set, the default max error count is %d.", DEFAULT_MAX_ERROR_COLLECTOR_COUNT); + } } if (run_or_build) { - print_usage_line(1, "-microarch:"); - print_usage_line(2, "Specifies the specific micro-architecture for the build in a string."); - print_usage_line(2, "Examples:"); - print_usage_line(3, "-microarch:sandybridge"); - print_usage_line(3, "-microarch:native"); - print_usage_line(3, "-microarch:\"?\" for a list"); - print_usage_line(0, ""); + if (print_flag("-microarch:")) { + print_usage_line(2, "Specifies the specific micro-architecture for the build in a string."); + print_usage_line(2, "Examples:"); + print_usage_line(3, "-microarch:sandybridge"); + print_usage_line(3, "-microarch:native"); + print_usage_line(3, "-microarch:\"?\" for a list"); + } } if (check) { - print_usage_line(1, "-min-link-libs"); - print_usage_line(2, "If set, the number of linked libraries will be minimized to prevent duplications."); - print_usage_line(2, "This is useful for so called \"dumb\" linkers compared to \"smart\" linkers."); - print_usage_line(0, ""); + if (print_flag("-min-link-libs")) { + print_usage_line(2, "If set, the number of linked libraries will be minimized to prevent duplications."); + print_usage_line(2, "This is useful for so called \"dumb\" linkers compared to \"smart\" linkers."); + } } if (run_or_build) { - print_usage_line(1, "-minimum-os-version:"); - print_usage_line(2, "Sets the minimum OS version targeted by the application."); - print_usage_line(2, "Default: -minimum-os-version:11.0.0"); - print_usage_line(2, "Only used when target is Darwin, if given, linking mismatched versions will emit a warning."); - print_usage_line(0, ""); + if (print_flag("-minimum-os-version:")) { + print_usage_line(2, "Sets the minimum OS version targeted by the application."); + print_usage_line(2, "Default: -minimum-os-version:11.0.0"); + print_usage_line(2, "Only used when target is Darwin, if given, linking mismatched versions will emit a warning."); + } - print_usage_line(1, "-no-bounds-check"); - print_usage_line(2, "Disables bounds checking program wide."); - print_usage_line(0, ""); + if (print_flag("-no-bounds-check")) { + print_usage_line(2, "Disables bounds checking program wide."); + } - print_usage_line(1, "-no-crt"); - print_usage_line(2, "Disables automatic linking with the C Run Time."); - print_usage_line(0, ""); + if (print_flag("-no-crt")) { + print_usage_line(2, "Disables automatic linking with the C Run Time."); + } } if (check && command != "test") { - print_usage_line(1, "-no-entry-point"); - print_usage_line(2, "Removes default requirement of an entry point (e.g. main procedure)."); - print_usage_line(0, ""); - } - - if (run_or_build) { - print_usage_line(1, "-no-rpath"); - print_usage_line(2, "Disables automatic addition of an rpath linked to the executable directory."); - print_usage_line(0, ""); - - print_usage_line(1, "-no-thread-local"); - print_usage_line(2, "Ignores @thread_local attribute, effectively treating the program as if it is single-threaded."); - print_usage_line(0, ""); - - print_usage_line(1, "-no-threaded-checker"); - print_usage_line(2, "Disables multithreading in the semantic checker stage."); - print_usage_line(0, ""); - - print_usage_line(1, "-no-type-assert"); - print_usage_line(2, "Disables type assertion checking program wide."); - print_usage_line(0, ""); - } - - if (run_or_build) { - print_usage_line(1, "-o:"); - print_usage_line(2, "Sets the optimization mode for compilation."); - print_usage_line(2, "Available options:"); - print_usage_line(3, "-o:none"); - print_usage_line(3, "-o:minimal"); - print_usage_line(3, "-o:size"); - print_usage_line(3, "-o:speed"); - if (LB_USE_NEW_PASS_SYSTEM) { - print_usage_line(3, "-o:aggressive (use this with caution)"); + if (print_flag("-no-entry-point")) { + print_usage_line(2, "Removes default requirement of an entry point (e.g. main procedure)."); + } + } + + if (run_or_build) { + if (print_flag("-no-rpath")) { + print_usage_line(2, "Disables automatic addition of an rpath linked to the executable directory."); + } + + if (print_flag("-no-thread-local")) { + print_usage_line(2, "Ignores @thread_local attribute, effectively treating the program as if it is single-threaded."); + } + + if (print_flag("-no-threaded-checker")) { + print_usage_line(2, "Disables multithreading in the semantic checker stage."); + } + + if (print_flag("-no-type-assert")) { + print_usage_line(2, "Disables type assertion checking program wide."); + } + } + + if (run_or_build) { + if (print_flag("-o:")) { + print_usage_line(2, "Sets the optimization mode for compilation."); + print_usage_line(2, "Available options:"); + print_usage_line(3, "-o:none"); + print_usage_line(3, "-o:minimal"); + print_usage_line(3, "-o:size"); + print_usage_line(3, "-o:speed"); + if (LB_USE_NEW_PASS_SYSTEM) { + print_usage_line(3, "-o:aggressive (use this with caution)"); + } + print_usage_line(2, "The default is -o:minimal."); } - print_usage_line(2, "The default is -o:minimal."); - print_usage_line(0, ""); - print_usage_line(1, "-obfuscate-source-code-locations"); - print_usage_line(2, "Obfuscate the file and procedure strings, and line and column numbers, stored with a 'runtime.Source_Code_Location' value."); - print_usage_line(0, ""); + if (print_flag("-obfuscate-source-code-locations")) { + print_usage_line(2, "Obfuscate the file and procedure strings, and line and column numbers, stored with a 'runtime.Source_Code_Location' value."); + } - print_usage_line(1, "-out:"); - print_usage_line(2, "Sets the file name of the outputted executable."); - print_usage_line(2, "Example: -out:foo.exe"); - print_usage_line(0, ""); + if (print_flag("-out:")) { + print_usage_line(2, "Sets the file name of the outputted executable."); + print_usage_line(2, "Example: -out:foo.exe"); + } } if (doc) { - print_usage_line(1, "-out:"); - print_usage_line(2, "Sets the base name of the resultig .odin-doc file."); - print_usage_line(2, "The extension can be optionally included; the resulting file will always have an extension of '.odin-doc'."); - print_usage_line(2, "Example: -out:foo"); - print_usage_line(0, ""); + if (print_flag("-out:")) { + print_usage_line(2, "Sets the base name of the resultig .odin-doc file."); + print_usage_line(2, "The extension can be optionally included; the resulting file will always have an extension of '.odin-doc'."); + print_usage_line(2, "Example: -out:foo"); + } } if (run_or_build) { #if defined(GB_SYSTEM_WINDOWS) - print_usage_line(1, "-pdb-name:"); - print_usage_line(2, "[Windows only]"); - print_usage_line(2, "Defines the generated PDB name when -debug is enabled."); - print_usage_line(2, "Example: -pdb-name:different.pdb"); - print_usage_line(0, ""); + if (print_flag("-pdb-name:")) { + print_usage_line(2, "[Windows only]"); + print_usage_line(2, "Defines the generated PDB name when -debug is enabled."); + print_usage_line(2, "Example: -pdb-name:different.pdb"); + } #endif } if (build) { - print_usage_line(1, "-print-linker-flags"); - print_usage_line(2, "Prints the all of the flags/arguments that will be passed to the linker."); - print_usage_line(0, ""); + if (print_flag("-print-linker-flags")) { + print_usage_line(2, "Prints the all of the flags/arguments that will be passed to the linker."); + } } if (run_or_build) { - print_usage_line(1, "-reloc-mode:"); - print_usage_line(2, "Specifies the reloc mode."); - print_usage_line(2, "Available options:"); - print_usage_line(3, "-reloc-mode:default"); - print_usage_line(3, "-reloc-mode:static"); - print_usage_line(3, "-reloc-mode:pic"); - print_usage_line(3, "-reloc-mode:dynamic-no-pic"); - print_usage_line(0, ""); + if (print_flag("-radlink")) { + print_usage_line(2, "Uses the RAD linker rather than the default."); + } + + if (print_flag("-reloc-mode:")) { + print_usage_line(2, "Specifies the reloc mode."); + print_usage_line(2, "Available options:"); + print_usage_line(3, "-reloc-mode:default"); + print_usage_line(3, "-reloc-mode:static"); + print_usage_line(3, "-reloc-mode:pic"); + print_usage_line(3, "-reloc-mode:dynamic-no-pic"); + } #if defined(GB_SYSTEM_WINDOWS) - print_usage_line(1, "-resource:"); - print_usage_line(2, "[Windows only]"); - print_usage_line(2, "Defines the resource file for the executable."); - print_usage_line(2, "Example: -resource:path/to/file.rc"); - print_usage_line(2, "or: -resource:path/to/file.res for a precompiled one."); - print_usage_line(0, ""); + if (print_flag("-resource:")) { + print_usage_line(2, "[Windows only]"); + print_usage_line(2, "Defines the resource file for the executable."); + print_usage_line(2, "Example: -resource:path/to/file.rc"); + print_usage_line(2, "or: -resource:path/to/file.res for a precompiled one."); + } #endif - print_usage_line(1, "-sanitize:"); - print_usage_line(2, "Enables sanitization analysis."); - print_usage_line(2, "Available options:"); - print_usage_line(3, "-sanitize:address"); - print_usage_line(3, "-sanitize:memory"); - print_usage_line(3, "-sanitize:thread"); - print_usage_line(2, "NOTE: This flag can be used multiple times."); - print_usage_line(0, ""); + if (print_flag("-sanitize:")) { + print_usage_line(2, "Enables sanitization analysis."); + print_usage_line(2, "Available options:"); + print_usage_line(3, "-sanitize:address"); + print_usage_line(3, "-sanitize:memory"); + print_usage_line(3, "-sanitize:thread"); + print_usage_line(2, "NOTE: This flag can be used multiple times."); + } } if (doc) { - print_usage_line(1, "-short"); - print_usage_line(2, "Shows shortened documentation for the packages."); - print_usage_line(0, ""); + if (print_flag("-short")) { + print_usage_line(2, "Shows shortened documentation for the packages."); + } } if (check) { - print_usage_line(1, "-show-defineables"); - print_usage_line(2, "Shows an overview of all the #config/#defined usages in the project."); - print_usage_line(0, ""); + if (print_flag("-show-defineables")) { + print_usage_line(2, "Shows an overview of all the #config/#defined usages in the project."); + } - print_usage_line(1, "-show-system-calls"); - print_usage_line(2, "Prints the whole command and arguments for calls to external tools like linker and assembler."); - print_usage_line(0, ""); + if (print_flag("-show-system-calls")) { + print_usage_line(2, "Prints the whole command and arguments for calls to external tools like linker and assembler."); + } - print_usage_line(1, "-show-timings"); - print_usage_line(2, "Shows basic overview of the timings of different stages within the compiler in milliseconds."); - print_usage_line(0, ""); + if (print_flag("-show-timings")) { + print_usage_line(2, "Shows basic overview of the timings of different stages within the compiler in milliseconds."); + } - print_usage_line(1, "-show-more-timings"); - print_usage_line(2, "Shows an advanced overview of the timings of different stages within the compiler in milliseconds."); - print_usage_line(0, ""); + if (print_flag("-show-more-timings")) { + print_usage_line(2, "Shows an advanced overview of the timings of different stages within the compiler in milliseconds."); + } } if (check_only) { - print_usage_line(1, "-show-unused"); - print_usage_line(2, "Shows unused package declarations within the current project."); - print_usage_line(0, ""); - print_usage_line(1, "-show-unused-with-location"); - print_usage_line(2, "Shows unused package declarations within the current project with the declarations source location."); - print_usage_line(0, ""); + if (print_flag("-show-unused")) { + print_usage_line(2, "Shows unused package declarations within the current project."); + } + if (print_flag("-show-unused-with-location")) { + print_usage_line(2, "Shows unused package declarations within the current project with the declarations source location."); + } } if (check) { - print_usage_line(1, "-strict-style"); - print_usage_line(2, "This enforces parts of same style as the Odin compiler, prefer '-vet-style -vet-semicolon' if you do not want to match it exactly."); - print_usage_line(2, ""); - print_usage_line(2, "Errs on unneeded tokens, such as unneeded semicolons."); - print_usage_line(2, "Errs on missing trailing commas followed by a newline."); - print_usage_line(2, "Errs on deprecated syntax."); - print_usage_line(2, "Errs when the attached-brace style in not adhered to (also known as 1TBS)."); - print_usage_line(2, "Errs when 'case' labels are not in the same column as the associated 'switch' token."); - print_usage_line(0, ""); + if (print_flag("-strict-style")) { + print_usage_line(2, "This enforces parts of same style as the Odin compiler, prefer '-vet-style -vet-semicolon' if you do not want to match it exactly."); + print_usage_line(2, ""); + print_usage_line(2, "Errs on unneeded tokens, such as unneeded semicolons."); + print_usage_line(2, "Errs on missing trailing commas followed by a newline."); + print_usage_line(2, "Errs on deprecated syntax."); + print_usage_line(2, "Errs when the attached-brace style in not adhered to (also known as 1TBS)."); + print_usage_line(2, "Errs when 'case' labels are not in the same column as the associated 'switch' token."); + } } if (run_or_build) { - print_usage_line(1, "-strict-target-features"); - print_usage_line(2, "Makes @(enable_target_features=\"...\") behave the same way as @(require_target_features=\"...\")."); - print_usage_line(2, "This enforces that all generated code uses features supported by the combination of -target, -microarch, and -target-features."); - print_usage_line(0, ""); + if (print_flag("-strict-target-features")) { + print_usage_line(2, "Makes @(enable_target_features=\"...\") behave the same way as @(require_target_features=\"...\")."); + print_usage_line(2, "This enforces that all generated code uses features supported by the combination of -target, -microarch, and -target-features."); + } #if defined(GB_SYSTEM_WINDOWS) - print_usage_line(1, "-subsystem: