diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index d72775636..e6341671a 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -1,5 +1,5 @@ name: CI -on: [push, pull_request] +on: [push, pull_request, workflow_dispatch] jobs: build_linux: diff --git a/.github/workflows/stale.yml b/.github/workflows/stale.yml new file mode 100644 index 000000000..341a09409 --- /dev/null +++ b/.github/workflows/stale.yml @@ -0,0 +1,43 @@ +name: "Close Stale Issues & PRs" +on: + workflow_dispatch: + schedule: + - cron: "0 21 * * *" + +permissions: + issues: write + pull-requests: write + +jobs: + stale: + runs-on: ubuntu-latest + steps: + - name: Close Stale Issues + uses: actions/stale@v4.1.0 + with: +# stale-issue-message: | +# Hello! +# +# I am marking this issue as stale as it has not received any engagement from the community or maintainers 120 days. That does not imply that the issue has no merit! If you feel strongly about this issue +# - open a PR referencing and resolving the issue; +# - leave a comment on it and discuss ideas how you could contribute towards resolving it; +# - leave a comment and describe in detail why this issue is critical for your use case; +# - open a new issue with updated details and a plan on resolving the issue. +# +# The motivation for this automation is to help prioritize issues in the backlog and not ignore, reject, or belittle anyone.. +# +# stale-pr-message: | +# Hello! +# +# I am marking this PR as stale as it has not received any engagement from the community or maintainers 120 days. That does not imply that the issue has no merit! If you feel strongly about this issue +# - leave a comment on it and discuss ideas how you could contribute towards resolving it; +# - leave a comment and describe in detail why this issue is critical for your use case; +# +# The motivation for this automation is to help prioritize issues in the backlog and not ignore, reject, or belittle anyone.. + + days-before-stale: 120 + days-before-close: 30 + exempt-draft-pr: true + ascending: true + operations-per-run: 1000 + exempt-issue-labels: "ignore" diff --git a/build.bat b/build.bat index f663c185c..06bc6c823 100644 --- a/build.bat +++ b/build.bat @@ -58,7 +58,7 @@ set libs= ^ set linker_flags= -incremental:no -opt:ref -subsystem:console if %release_mode% EQU 0 ( rem Debug - set linker_flags=%linker_flags% -debug + set linker_flags=%linker_flags% -debug /NATVIS:src\odin_compiler.natvis ) else ( rem Release set linker_flags=%linker_flags% -debug ) diff --git a/build_odin.sh b/build_odin.sh index aef3f2836..b8cd09c54 100755 --- a/build_odin.sh +++ b/build_odin.sh @@ -1,12 +1,20 @@ -#!/bin/bash +#!/usr/bin/env bash set -eu -GIT_SHA=$(git rev-parse --short HEAD) +: ${CXX=clang++} +: ${CPPFLAGS=} +: ${CXXFLAGS=} +: ${LDFLAGS=} +: ${ODIN_VERSION=dev-$(date +"%Y-%m")} + +CPPFLAGS="$CPPFLAGS -DODIN_VERSION_RAW=\"$ODIN_VERSION\"" +CXXFLAGS="$CXXFLAGS -std=c++14" +LDFLAGS="$LDFLAGS -pthread -lm -lstdc++" + +GIT_SHA=$(git rev-parse --short HEAD || :) +if [ "$GIT_SHA" ]; then CPPFLAGS="$CPPFLAGS -DGIT_SHA=\"$GIT_SHA\""; fi + DISABLED_WARNINGS="-Wno-switch -Wno-macro-redefined -Wno-unused-value" -LDFLAGS="-pthread -lm -lstdc++" -CFLAGS="-std=c++14 -DGIT_SHA=\"$GIT_SHA\"" -CFLAGS="$CFLAGS -DODIN_VERSION_RAW=\"dev-$(date +"%Y-%m")\"" -CC=clang OS=$(uname) panic() { @@ -18,13 +26,13 @@ version() { echo "$@" | awk -F. '{ printf("%d%03d%03d%03d\n", $1,$2,$3,$4); }'; config_darwin() { ARCH=$(uname -m) - LLVM_CONFIG=llvm-config + : ${LLVM_CONFIG=llvm-config} # allow for arm only llvm's with version 13 if [ ARCH == arm64 ]; then MIN_LLVM_VERSION=("13.0.0") else - # allow for x86 / amd64 all llvm versions begining from 11 + # allow for x86 / amd64 all llvm versions beginning from 11 MIN_LLVM_VERSION=("11.1.0") fi @@ -37,34 +45,38 @@ config_darwin() { fi LDFLAGS="$LDFLAGS -liconv -ldl" - CFLAGS="$CFLAGS $($LLVM_CONFIG --cxxflags --ldflags)" + CXXFLAGS="$CXXFLAGS $($LLVM_CONFIG --cxxflags --ldflags)" LDFLAGS="$LDFLAGS -lLLVM-C" } config_freebsd() { - LLVM_CONFIG=/usr/local/bin/llvm-config11 + : ${LLVM_CONFIG=/usr/local/bin/llvm-config11} - CFLAGS="$CFLAGS $($LLVM_CONFIG --cxxflags --ldflags)" + CXXFLAGS="$CXXFLAGS $($LLVM_CONFIG --cxxflags --ldflags)" LDFLAGS="$LDFLAGS $($LLVM_CONFIG --libs core native --system-libs)" } config_openbsd() { - LLVM_CONFIG=/usr/local/bin/llvm-config + : ${LLVM_CONFIG=/usr/local/bin/llvm-config} LDFLAGS="$LDFLAGS -liconv" - CFLAGS="$CFLAGS $($LLVM_CONFIG --cxxflags --ldflags)" + CXXFLAGS="$CXXFLAGS $($LLVM_CONFIG --cxxflags --ldflags)" LDFLAGS="$LDFLAGS $($LLVM_CONFIG --libs core native --system-libs)" } config_linux() { - if which llvm-config > /dev/null 2>&1; then - LLVM_CONFIG=llvm-config - elif which llvm-config-11 > /dev/null 2>&1; then - LLVM_CONFIG=llvm-config-11 - elif which llvm-config-11-64 > /dev/null 2>&1; then - LLVM_CONFIG=llvm-config-11-64 - else - panic "Unable to find LLVM-config" + : ${LLVM_CONFIG=} + + if [ ! "$LLVM_CONFIG" ]; then + if which llvm-config > /dev/null 2>&1; then + LLVM_CONFIG=llvm-config + elif which llvm-config-11 > /dev/null 2>&1; then + LLVM_CONFIG=llvm-config-11 + elif which llvm-config-11-64 > /dev/null 2>&1; then + LLVM_CONFIG=llvm-config-11-64 + else + panic "Unable to find LLVM-config" + fi fi MIN_LLVM_VERSION=("11.0.0") @@ -74,7 +86,7 @@ config_linux() { fi LDFLAGS="$LDFLAGS -ldl" - CFLAGS="$CFLAGS $($LLVM_CONFIG --cxxflags --ldflags)" + CXXFLAGS="$CXXFLAGS $($LLVM_CONFIG --cxxflags --ldflags)" LDFLAGS="$LDFLAGS $($LLVM_CONFIG --libs core native --system-libs)" } @@ -97,7 +109,7 @@ build_odin() { esac set -x - $CC src/main.cpp src/libtommath.cpp $DISABLED_WARNINGS $CFLAGS $EXTRAFLAGS $LDFLAGS -o odin + $CXX src/main.cpp src/libtommath.cpp $DISABLED_WARNINGS $CPPFLAGS $CXXFLAGS $EXTRAFLAGS $LDFLAGS -o odin set +x } diff --git a/core/bufio/scanner.odin b/core/bufio/scanner.odin index 86e6d8eb0..b9e620250 100644 --- a/core/bufio/scanner.odin +++ b/core/bufio/scanner.odin @@ -66,7 +66,7 @@ scanner_destroy :: proc(s: ^Scanner) { } -// Returns the first non-EOF error that was encounted by the scanner +// Returns the first non-EOF error that was encountered by the scanner scanner_error :: proc(s: ^Scanner) -> Scanner_Error { switch s._err { case .EOF, nil: diff --git a/core/bytes/buffer.odin b/core/bytes/buffer.odin index 7d8ae596d..96f057a9b 100644 --- a/core/bytes/buffer.odin +++ b/core/bytes/buffer.odin @@ -161,6 +161,10 @@ buffer_write :: proc(b: ^Buffer, p: []byte) -> (n: int, err: io.Error) { return copy(b.buf[m:], p), nil } +buffer_write_ptr :: proc(b: ^Buffer, ptr: rawptr, size: int) -> (n: int, err: io.Error) { + return buffer_write(b, ([^]byte)(ptr)[:size]) +} + buffer_write_string :: proc(b: ^Buffer, s: string) -> (n: int, err: io.Error) { b.last_read = .Invalid m, ok := _buffer_try_grow(b, len(s)) @@ -229,6 +233,10 @@ buffer_read :: proc(b: ^Buffer, p: []byte) -> (n: int, err: io.Error) { return } +buffer_read_ptr :: proc(b: ^Buffer, ptr: rawptr, size: int) -> (n: int, err: io.Error) { + return buffer_read(b, ([^]byte)(ptr)[:size]) +} + buffer_read_at :: proc(b: ^Buffer, p: []byte, offset: int) -> (n: int, err: io.Error) { b.last_read = .Invalid diff --git a/core/c/frontend/preprocessor/preprocess.odin b/core/c/frontend/preprocessor/preprocess.odin index 1db3fafa3..4cfad2c1c 100644 --- a/core/c/frontend/preprocessor/preprocess.odin +++ b/core/c/frontend/preprocessor/preprocess.odin @@ -519,7 +519,7 @@ join_adjacent_string_literals :: proc(cpp: ^Preprocessor, initial_tok: ^Token) { quote_string :: proc(s: string) -> []byte { - b := strings.make_builder(0, len(s)+2) + b := strings.builder_make(0, len(s)+2) io.write_quoted_string(strings.to_writer(&b), s, '"') return b.buf[:] } diff --git a/core/c/libc/math.odin b/core/c/libc/math.odin index 6a7b81850..0a6ecc0c3 100644 --- a/core/c/libc/math.odin +++ b/core/c/libc/math.odin @@ -331,7 +331,7 @@ fmin :: proc{libc_fmin, libc_fminf} fma :: proc{libc_fma, libc_fmaf} // But retain the 'f' suffix-variant functions as well so they can be used, -// a trick is used here where we use explicit procedrual overloading of one +// a trick is used here where we use explicit procedural overloading of one // procedure. This is done because the foreign block is marked @(private) and // aliasing functions does not remove privateness from the entity. acosf :: proc{libc_acosf} diff --git a/core/c/libc/stdio.odin b/core/c/libc/stdio.odin index 01d0cd427..9b1089d85 100644 --- a/core/c/libc/stdio.odin +++ b/core/c/libc/stdio.odin @@ -196,7 +196,7 @@ foreign libc { getc :: proc(stream: ^FILE) -> int --- getchar :: proc() -> int --- putc :: proc(c: int, stream: ^FILE) -> int --- - putchar :: proc() -> int --- + putchar :: proc(c: int) -> int --- puts :: proc(s: cstring) -> int --- ungetc :: proc(c: int, stream: ^FILE) -> int --- fread :: proc(ptr: rawptr, size: size_t, nmemb: size_t, stream: ^FILE) -> size_t --- diff --git a/core/crypto/README.md b/core/crypto/README.md index ddcb12d81..dd594d5c2 100644 --- a/core/crypto/README.md +++ b/core/crypto/README.md @@ -81,7 +81,7 @@ The crypto package is not thread-safe at the moment. This may change in the futu ### Disclaimer The algorithms were ported out of curiosity and due to interest in the field. We have not had any of the code verified by a third party or tested/fuzzed by any automatic means. -Whereever we were able to find official test vectors, those were used to verify the implementation. +Wherever we were able to find official test vectors, those were used to verify the implementation. We do not recommend using them in a production environment, without any additional testing and/or verification. ### ToDo diff --git a/core/crypto/_fiat/README.md b/core/crypto/_fiat/README.md index cd510d442..4011a06c0 100644 --- a/core/crypto/_fiat/README.md +++ b/core/crypto/_fiat/README.md @@ -30,6 +30,6 @@ equivalence. For the most part, alterations to the base fiat-crypto generated code was kept to a minimum, to aid auditability. This results in a somewhat -ideosyncratic style, and in some cases minor performance penalties. +idiosyncratic style, and in some cases minor performance penalties. [1]: https://github.com/mit-plv/fiat-crypto diff --git a/core/crypto/siphash/siphash.odin b/core/crypto/siphash/siphash.odin index a6c75f315..2d03829c2 100644 --- a/core/crypto/siphash/siphash.odin +++ b/core/crypto/siphash/siphash.odin @@ -233,7 +233,7 @@ init :: proc(ctx: ^Context, key: []byte, c_rounds, d_rounds: int) { } update :: proc(ctx: ^Context, data: []byte) { - assert(ctx.is_initialized, "crypto/siphash: Context is not initalized") + assert(ctx.is_initialized, "crypto/siphash: Context is not initialized") ctx.last_block = len(data) / 8 * 8 ctx.buf = data i := 0 diff --git a/core/encoding/entity/entity.odin b/core/encoding/entity/entity.odin index e5831a75f..694fcdffc 100644 --- a/core/encoding/entity/entity.odin +++ b/core/encoding/entity/entity.odin @@ -25,8 +25,8 @@ import "core:strings" MAX_RUNE_CODEPOINT :: int(unicode.MAX_RUNE) -write_rune :: strings.write_rune_builder -write_string :: strings.write_string_builder +write_rune :: strings.write_rune +write_string :: strings.write_string Error :: enum u8 { None = 0, @@ -94,8 +94,8 @@ decode_xml :: proc(input: string, options := XML_Decode_Options{}, allocator := l := len(input) if l == 0 { return "", .None } - builder := strings.make_builder() - defer strings.destroy_builder(&builder) + builder := strings.builder_make() + defer strings.builder_destroy(&builder) t := Tokenizer{src=input} in_data := false diff --git a/core/encoding/entity/example/entity_example.odin b/core/encoding/entity/example/entity_example.odin index 6fc377f9d..6301eb263 100644 --- a/core/encoding/entity/example/entity_example.odin +++ b/core/encoding/entity/example/entity_example.odin @@ -8,7 +8,7 @@ import "core:time" doc_print :: proc(doc: ^xml.Document) { buf: strings.Builder - defer strings.destroy_builder(&buf) + defer strings.builder_destroy(&buf) w := strings.to_writer(&buf) xml.print(w, doc) diff --git a/core/encoding/hxa/hxa.odin b/core/encoding/hxa/hxa.odin index f47661bad..9b24ede9c 100644 --- a/core/encoding/hxa/hxa.odin +++ b/core/encoding/hxa/hxa.odin @@ -107,7 +107,7 @@ Node :: struct { /* Conventions */ /* ------------ Much of HxA's use is based on convention. HxA lets users store arbitrary data in its structure that can be parsed but whose semantic meaning does not need to be understood. -A few conventions are hard, and some are soft. Hard convention that a user HAS to follow in order to produce a valid file. Hard conventions simplify parsing becaus the parser can make some assumptions. Soft convenbtions are basicly recomendations of how to store common data. +A few conventions are hard, and some are soft. Hard convention that a user HAS to follow in order to produce a valid file. Hard conventions simplify parsing becaus the parser can make some assumptions. Soft convenbtions are basically recomendations of how to store common data. If you use HxA for something not covered by the conventions but need a convention for your use case. Please let us know so that we can add it! */ diff --git a/core/encoding/json/marshal.odin b/core/encoding/json/marshal.odin index 2a92cec28..9dbe31cb6 100644 --- a/core/encoding/json/marshal.odin +++ b/core/encoding/json/marshal.odin @@ -44,7 +44,7 @@ Marshal_Options :: struct { mjson_skipped_first_braces_end: bool, } -marshal :: proc(v: any, opt: Marshal_Options = {}, allocator := context.allocator) -> (data: []byte, err: Marshal_Error) { +marshal :: proc(v: any, opt := Marshal_Options{}, allocator := context.allocator) -> (data: []byte, err: Marshal_Error) { b := strings.builder_make(allocator) defer if err != nil { strings.builder_destroy(&b) @@ -192,6 +192,9 @@ marshal_to_writer :: proc(w: io.Writer, v: any, opt: ^Marshal_Options) -> (err: case runtime.Type_Info_Multi_Pointer: return .Unsupported_Type + case runtime.Type_Info_Soa_Pointer: + return .Unsupported_Type + case runtime.Type_Info_Procedure: return .Unsupported_Type diff --git a/core/encoding/json/unmarshal.odin b/core/encoding/json/unmarshal.odin index 2ff268a21..97d2421d4 100644 --- a/core/encoding/json/unmarshal.odin +++ b/core/encoding/json/unmarshal.odin @@ -325,7 +325,7 @@ unmarshal_object :: proc(p: ^Parser, v: any, end_token: Token_Kind) -> (err: Unm UNSUPPORTED_TYPE := Unsupported_Type_Error{v.id, p.curr_token} if end_token == .Close_Brace { - assert(expect_token(p, .Open_Brace) == nil) + unmarshal_expect_token(p, .Open_Brace) } v := v @@ -473,7 +473,7 @@ unmarshal_object :: proc(p: ^Parser, v: any, end_token: Token_Kind) -> (err: Unm } if end_token == .Close_Brace { - assert(expect_token(p, .Close_Brace) == nil) + unmarshal_expect_token(p, .Close_Brace) } return } diff --git a/core/encoding/xml/example/xml_example.odin b/core/encoding/xml/example/xml_example.odin index f7e74840e..887b40764 100644 --- a/core/encoding/xml/example/xml_example.odin +++ b/core/encoding/xml/example/xml_example.odin @@ -84,7 +84,7 @@ example :: proc() { doc_hash :: proc(doc: ^xml.Document, print := false) -> (crc32: u32) { buf: strings.Builder - defer strings.destroy_builder(&buf) + defer strings.builder_destroy(&buf) w := strings.to_writer(&buf) xml.print(w, doc) diff --git a/core/fmt/fmt.odin b/core/fmt/fmt.odin index 78d4ddcd7..e68280bfe 100644 --- a/core/fmt/fmt.odin +++ b/core/fmt/fmt.odin @@ -77,7 +77,7 @@ register_user_formatter :: proc(id: typeid, formatter: User_Formatter) -> Regist // They must be freed accordingly aprint :: proc(args: ..any, sep := " ") -> string { str: strings.Builder - strings.init_builder(&str) + strings.builder_init(&str) sbprint(buf=&str, args=args, sep=sep) return strings.to_string(str) } @@ -85,7 +85,7 @@ aprint :: proc(args: ..any, sep := " ") -> string { // They must be freed accordingly aprintln :: proc(args: ..any, sep := " ") -> string { str: strings.Builder - strings.init_builder(&str) + strings.builder_init(&str) sbprintln(buf=&str, args=args, sep=sep) return strings.to_string(str) } @@ -93,7 +93,7 @@ aprintln :: proc(args: ..any, sep := " ") -> string { // They must be freed accordingly aprintf :: proc(fmt: string, args: ..any) -> string { str: strings.Builder - strings.init_builder(&str) + strings.builder_init(&str) sbprintf(&str, fmt, ..args) return strings.to_string(str) } @@ -102,21 +102,21 @@ aprintf :: proc(fmt: string, args: ..any) -> string { // tprint procedure return a string that was allocated with the current context's temporary allocator tprint :: proc(args: ..any, sep := " ") -> string { str: strings.Builder - strings.init_builder(&str, context.temp_allocator) + strings.builder_init(&str, context.temp_allocator) sbprint(buf=&str, args=args, sep=sep) return strings.to_string(str) } // tprintln procedure return a string that was allocated with the current context's temporary allocator tprintln :: proc(args: ..any, sep := " ") -> string { str: strings.Builder - strings.init_builder(&str, context.temp_allocator) + strings.builder_init(&str, context.temp_allocator) sbprintln(buf=&str, args=args, sep=sep) return strings.to_string(str) } // tprintf procedure return a string that was allocated with the current context's temporary allocator tprintf :: proc(fmt: string, args: ..any) -> string { str: strings.Builder - strings.init_builder(&str, context.temp_allocator) + strings.builder_init(&str, context.temp_allocator) sbprintf(&str, fmt, ..args) return strings.to_string(str) } @@ -776,7 +776,7 @@ fmt_rune :: proc(fi: ^Info, r: rune, verb: rune) { case 'c', 'r', 'v': io.write_rune(fi.writer, r, &fi.n) case 'q': - fi.n += strings.write_quoted_rune(fi.writer, r) + fi.n += io.write_quoted_rune(fi.writer, r) case: fmt_int(fi, u64(r), false, 32, verb) } @@ -1014,7 +1014,7 @@ fmt_pointer :: proc(fi: ^Info, p: rawptr, verb: rune) { u := u64(uintptr(p)) switch verb { case 'p', 'v': - if !fi.hash || verb == 'v' { + if !fi.hash && verb == 'v' { io.write_string(fi.writer, "0x", &fi.n) } _fmt_int(fi, u, 16, false, 8*size_of(rawptr), __DIGITS_UPPER) @@ -1031,6 +1031,15 @@ fmt_pointer :: proc(fi: ^Info, p: rawptr, verb: rune) { } } +fmt_soa_pointer :: proc(fi: ^Info, p: runtime.Raw_Soa_Pointer, verb: rune) { + io.write_string(fi.writer, "#soa{data=0x", &fi.n) + _fmt_int(fi, u64(uintptr(p.data)), 16, false, 8*size_of(rawptr), __DIGITS_UPPER) + io.write_string(fi.writer, ", index=", &fi.n) + _fmt_int(fi, u64(p.index), 10, false, 8*size_of(rawptr), __DIGITS_UPPER) + io.write_string(fi.writer, "}", &fi.n) +} + + enum_value_to_string :: proc(val: any) -> (string, bool) { v := val v.id = runtime.typeid_base(v.id) @@ -1867,6 +1876,10 @@ fmt_value :: proc(fi: ^Info, v: any, verb: rune) { fmt_pointer(fi, ptr, verb) } + case runtime.Type_Info_Soa_Pointer: + ptr := (^runtime.Raw_Soa_Pointer)(v.data)^ + fmt_soa_pointer(fi, ptr, verb) + case runtime.Type_Info_Multi_Pointer: ptr := (^rawptr)(v.data)^ if ptr == nil { diff --git a/core/hash/crc.odin b/core/hash/crc.odin index b504e92fb..9c0048a0f 100644 --- a/core/hash/crc.odin +++ b/core/hash/crc.odin @@ -1,8 +1,8 @@ package hash @(optimization_mode="speed") -crc64_ecma_182 :: proc(data: []byte, seed := u32(0)) -> u64 #no_bounds_check { - result := u64(seed) +crc64_ecma_182 :: proc(data: []byte, seed := u64(0)) -> (result: u64) #no_bounds_check { + result = seed #no_bounds_check for b in data { result = result<<8 ~ _crc64_table_ecma_182[((result>>56) ~ u64(b)) & 0xff] } diff --git a/core/hash/hash.odin b/core/hash/hash.odin index 63708a096..870d6a638 100644 --- a/core/hash/hash.odin +++ b/core/hash/hash.odin @@ -172,108 +172,114 @@ murmur32 :: proc(data: []byte, seed := u32(0)) -> u32 { return h1 } +// See https://github.com/aappleby/smhasher/blob/master/src/MurmurHash2.cpp#L96 @(optimization_mode="speed") -murmur64 :: proc(data: []byte, seed := u64(0x9747b28c)) -> u64 { - when size_of(int) == 8 { - m :: 0xc6a4a7935bd1e995 - r :: 47 +murmur64a :: proc(data: []byte, seed := u64(0x9747b28c)) -> u64 { + m :: 0xc6a4a7935bd1e995 + r :: 47 - h: u64 = seed ~ (u64(len(data)) * m) - data64 := mem.slice_ptr(cast(^u64)raw_data(data), len(data)/size_of(u64)) + h: u64 = seed ~ (u64(len(data)) * m) + data64 := mem.slice_data_cast([]u64, data) - for _, i in data64 { - k := data64[i] + for _, i in data64 { + k := data64[i] - k *= m - k ~= k>>r - k *= m + k *= m + k ~= k>>r + k *= m - h ~= k - h *= m - } - - switch len(data)&7 { - case 7: h ~= u64(data[6]) << 48; fallthrough - case 6: h ~= u64(data[5]) << 40; fallthrough - case 5: h ~= u64(data[4]) << 32; fallthrough - case 4: h ~= u64(data[3]) << 24; fallthrough - case 3: h ~= u64(data[2]) << 16; fallthrough - case 2: h ~= u64(data[1]) << 8; fallthrough - case 1: - h ~= u64(data[0]) - h *= m - } - - h ~= h>>r + h ~= k h *= m - h ~= h>>r - - return h - } else { - m :: 0x5bd1e995 - r :: 24 - - h1 := u32(seed) ~ u32(len(data)) - h2 := u32(seed) >> 32 - data32 := mem.slice_ptr(cast(^u32)raw_data(data), len(data)/size_of(u32)) - len := len(data) - i := 0 - - for len >= 8 { - k1, k2: u32 - k1 = data32[i]; i += 1 - k1 *= m - k1 ~= k1>>r - k1 *= m - h1 *= m - h1 ~= k1 - len -= 4 - - k2 = data32[i]; i += 1 - k2 *= m - k2 ~= k2>>r - k2 *= m - h2 *= m - h2 ~= k2 - len -= 4 - } - - if len >= 4 { - k1: u32 - k1 = data32[i]; i += 1 - k1 *= m - k1 ~= k1>>r - k1 *= m - h1 *= m - h1 ~= k1 - len -= 4 - } - - // TODO(bill): Fix this - #no_bounds_check data8 := mem.slice_to_bytes(data32[i:])[:3] - switch len { - case 3: - h2 ~= u32(data8[2]) << 16 - fallthrough - case 2: - h2 ~= u32(data8[1]) << 8 - fallthrough - case 1: - h2 ~= u32(data8[0]) - h2 *= m - } - - h1 ~= h2>>18 - h1 *= m - h2 ~= h1>>22 - h2 *= m - h1 ~= h2>>17 - h1 *= m - h2 ~= h1>>19 - h2 *= m - - return u64(h1)<<32 | u64(h2) } + + offset := len(data64) * size_of(u64) + + switch len(data)&7 { + case 7: h ~= u64(data[offset + 6]) << 48; fallthrough + case 6: h ~= u64(data[offset + 5]) << 40; fallthrough + case 5: h ~= u64(data[offset + 4]) << 32; fallthrough + case 4: h ~= u64(data[offset + 3]) << 24; fallthrough + case 3: h ~= u64(data[offset + 2]) << 16; fallthrough + case 2: h ~= u64(data[offset + 1]) << 8; fallthrough + case 1: + h ~= u64(data[offset + 0]) + h *= m + } + + h ~= h>>r + h *= m + h ~= h>>r + + return h +} + +// See https://github.com/aappleby/smhasher/blob/master/src/MurmurHash2.cpp#L140 +@(optimization_mode="speed") +murmur64b :: proc(data: []byte, seed := u64(0x9747b28c)) -> u64 { + m :: 0x5bd1e995 + r :: 24 + + h1 := u32(seed) ~ u32(len(data)) + h2 := u32(seed) >> 32 + + data32 := mem.slice_ptr(cast(^u32)raw_data(data), len(data)/size_of(u32)) + len := len(data) + i := 0 + + for len >= 8 { + k1, k2: u32 + k1 = data32[i]; i += 1 + k1 *= m + k1 ~= k1>>r + k1 *= m + h1 *= m + h1 ~= k1 + len -= 4 + + k2 = data32[i]; i += 1 + k2 *= m + k2 ~= k2>>r + k2 *= m + h2 *= m + h2 ~= k2 + len -= 4 + } + + if len >= 4 { + k1: u32 + k1 = data32[i]; i += 1 + k1 *= m + k1 ~= k1>>r + k1 *= m + h1 *= m + h1 ~= k1 + len -= 4 + } + + // TODO(bill): Fix this + #no_bounds_check data8 := mem.slice_to_bytes(data32[i:])[:3] + switch len { + case 3: + h2 ~= u32(data8[2]) << 16 + fallthrough + case 2: + h2 ~= u32(data8[1]) << 8 + fallthrough + case 1: + h2 ~= u32(data8[0]) + h2 *= m + } + + h1 ~= h2>>18 + h1 *= m + h2 ~= h1>>22 + h2 *= m + h1 ~= h2>>17 + h1 *= m + h2 ~= h1>>19 + h2 *= m + + return u64(h1)<<32 | u64(h2) } @(optimization_mode="speed") diff --git a/core/image/common.odin b/core/image/common.odin index baacd64d9..31f622d2b 100644 --- a/core/image/common.odin +++ b/core/image/common.odin @@ -469,7 +469,7 @@ return_single_channel :: proc(img: ^Image, channel: Channel) -> (res: ^Image, ok } // Does the image have 1 or 2 channels, a valid bit depth (8 or 16), -// Is the pointer valid, are the dimenions valid? +// Is the pointer valid, are the dimensions valid? is_valid_grayscale_image :: proc(img: ^Image) -> (ok: bool) { // Were we actually given a valid image? if img == nil { @@ -489,7 +489,7 @@ is_valid_grayscale_image :: proc(img: ^Image) -> (ok: bool) { // This returns 0 if any of the inputs is zero. bytes_expected := compute_buffer_size(img.width, img.height, img.channels, img.depth) - // If the dimenions are invalid or the buffer size doesn't match the image characteristics, bail. + // If the dimensions are invalid or the buffer size doesn't match the image characteristics, bail. if bytes_expected == 0 || bytes_expected != len(img.pixels.buf) || img.width * img.height > MAX_DIMENSIONS { return false } @@ -498,7 +498,7 @@ is_valid_grayscale_image :: proc(img: ^Image) -> (ok: bool) { } // Does the image have 3 or 4 channels, a valid bit depth (8 or 16), -// Is the pointer valid, are the dimenions valid? +// Is the pointer valid, are the dimensions valid? is_valid_color_image :: proc(img: ^Image) -> (ok: bool) { // Were we actually given a valid image? if img == nil { @@ -518,7 +518,7 @@ is_valid_color_image :: proc(img: ^Image) -> (ok: bool) { // This returns 0 if any of the inputs is zero. bytes_expected := compute_buffer_size(img.width, img.height, img.channels, img.depth) - // If the dimenions are invalid or the buffer size doesn't match the image characteristics, bail. + // If the dimensions are invalid or the buffer size doesn't match the image characteristics, bail. if bytes_expected == 0 || bytes_expected != len(img.pixels.buf) || img.width * img.height > MAX_DIMENSIONS { return false } @@ -527,7 +527,7 @@ is_valid_color_image :: proc(img: ^Image) -> (ok: bool) { } // Does the image have 1..4 channels, a valid bit depth (8 or 16), -// Is the pointer valid, are the dimenions valid? +// Is the pointer valid, are the dimensions valid? is_valid_image :: proc(img: ^Image) -> (ok: bool) { // Were we actually given a valid image? if img == nil { diff --git a/core/image/netpbm/netpbm.odin b/core/image/netpbm/netpbm.odin index 5a504cd7c..18545092d 100644 --- a/core/image/netpbm/netpbm.odin +++ b/core/image/netpbm/netpbm.odin @@ -130,7 +130,7 @@ save_to_buffer :: proc(img: ^Image, custom_info: Info = {}, allocator := context // we will write to a string builder data: strings.Builder - strings.init_builder(&data) + strings.builder_init(&data) // all PNM headers start with the format fmt.sbprintf(&data, "%s\n", header.format) @@ -409,7 +409,7 @@ _parse_header_pam :: proc(data: []byte, allocator := context.allocator) -> (head // string buffer for the tupltype tupltype: strings.Builder - strings.init_builder(&tupltype, context.temp_allocator); defer strings.destroy_builder(&tupltype) + strings.builder_init(&tupltype, context.temp_allocator); defer strings.builder_destroy(&tupltype) fmt.sbprint(&tupltype, "") // PAM uses actual lines, so we can iterate easily diff --git a/core/image/png/example.odin b/core/image/png/example.odin index 17436c260..c1e2f0c73 100644 --- a/core/image/png/example.odin +++ b/core/image/png/example.odin @@ -219,7 +219,7 @@ write_image_as_ppm :: proc(filename: string, image: ^image.Image) -> (success: b defer close(fd) write_string(fd, - fmt.tprintf("P6\n%v %v\n%v\n", width, height, (1 << uint(depth) - 1)), + fmt.tprintf("P6\n%v %v\n%v\n", width, height, uint(1 << uint(depth) - 1)), ) if channels == 3 { diff --git a/core/image/png/png.odin b/core/image/png/png.odin index 35fdb58d8..3faa39c83 100644 --- a/core/image/png/png.odin +++ b/core/image/png/png.odin @@ -1002,7 +1002,7 @@ load_from_context :: proc(ctx: ^$C, options := Options{}, allocator := context.a o16 = o16[out_image_channels:] } case: - unreachable("We should never seen # channels other than 1-4 inclusive.") + panic("We should never seen # channels other than 1-4 inclusive.") } img.pixels = t @@ -1195,7 +1195,7 @@ load_from_context :: proc(ctx: ^$C, options := Options{}, allocator := context.a o = o[out_image_channels:] } case: - unreachable("We should never seen # channels other than 1-4 inclusive.") + panic("We should never seen # channels other than 1-4 inclusive.") } img.pixels = t @@ -1206,7 +1206,7 @@ load_from_context :: proc(ctx: ^$C, options := Options{}, allocator := context.a This may change if we ever don't expand 1, 2 and 4 bit images. But, those raw returns will likely bypass this processing pipeline. */ - unreachable("We should never see bit depths other than 8, 16 and 'Paletted' here.") + panic("We should never see bit depths other than 8, 16 and 'Paletted' here.") } return img, nil diff --git a/core/io/util.odin b/core/io/util.odin index 9d253807b..46aa97919 100644 --- a/core/io/util.odin +++ b/core/io/util.odin @@ -247,6 +247,30 @@ write_quoted_string :: proc(w: Writer, str: string, quote: byte = '"', n_written return } +// writer append a quoted rune into the byte buffer, return the written size +write_quoted_rune :: proc(w: Writer, r: rune) -> (n: int) { + _write_byte :: #force_inline proc(w: Writer, c: byte) -> int { + err := write_byte(w, c) + return 1 if err == nil else 0 + } + + quote := byte('\'') + n += _write_byte(w, quote) + buf, width := utf8.encode_rune(r) + if width == 1 && r == utf8.RUNE_ERROR { + n += _write_byte(w, '\\') + n += _write_byte(w, 'x') + n += _write_byte(w, DIGITS_LOWER[buf[0]>>4]) + n += _write_byte(w, DIGITS_LOWER[buf[0]&0xf]) + } else { + i, _ := write_escaped_rune(w, r, quote) + n += i + } + n += _write_byte(w, quote) + return +} + + Tee_Reader :: struct { diff --git a/core/log/file_console_logger.odin b/core/log/file_console_logger.odin index 7f0d3b07a..70fee0be9 100644 --- a/core/log/file_console_logger.odin +++ b/core/log/file_console_logger.odin @@ -56,7 +56,7 @@ create_console_logger :: proc(lowest := Level.Debug, opt := Default_Console_Logg return Logger{file_console_logger_proc, data, lowest, opt} } -destroy_console_logger :: proc(log: ^Logger) { +destroy_console_logger :: proc(log: Logger) { free(log.data) } diff --git a/core/log/log.odin b/core/log/log.odin index d34a135cc..a699247b8 100644 --- a/core/log/log.odin +++ b/core/log/log.odin @@ -6,7 +6,6 @@ import "core:fmt" // NOTE(bill, 2019-12-31): These are defined in `package runtime` as they are used in the `context`. This is to prevent an import definition cycle. -Level :: runtime.Logger_Level /* Logger_Level :: enum { Debug = 0, @@ -16,8 +15,8 @@ Logger_Level :: enum { Fatal = 40, } */ +Level :: runtime.Logger_Level -Option :: runtime.Logger_Option /* Option :: enum { Level, @@ -30,11 +29,12 @@ Option :: enum { Terminal_Color } */ +Option :: runtime.Logger_Option -Options :: runtime.Logger_Options /* Options :: bit_set[Option]; */ +Options :: runtime.Logger_Options Full_Timestamp_Opts :: Options{ .Date, @@ -52,12 +52,11 @@ Location_File_Opts :: Options{ } -Logger_Proc :: runtime.Logger_Proc /* Logger_Proc :: #type proc(data: rawptr, level: Level, text: string, options: Options, location := #caller_location); */ +Logger_Proc :: runtime.Logger_Proc -Logger :: runtime.Logger /* Logger :: struct { procedure: Logger_Proc, @@ -66,6 +65,7 @@ Logger :: struct { options: Logger_Options, } */ +Logger :: runtime.Logger nil_logger_proc :: proc(data: rawptr, level: Level, text: string, options: Options, location := #caller_location) { // Do nothing @@ -75,7 +75,6 @@ nil_logger :: proc() -> Logger { return Logger{nil_logger_proc, nil, Level.Debug, nil} } -// TODO(bill): Should these be redesigned so that they are do not rely upon `package fmt`? debugf :: proc(fmt_str: string, args: ..any, location := #caller_location) { logf(level=.Debug, fmt_str=fmt_str, args=args, location=location) } diff --git a/core/log/log_allocator.odin b/core/log/log_allocator.odin new file mode 100644 index 000000000..997bd1a68 --- /dev/null +++ b/core/log/log_allocator.odin @@ -0,0 +1,107 @@ +package log + +import "core:runtime" + +Log_Allocator :: struct { + allocator: runtime.Allocator, + level: Level, + prefix: string, + locked: bool, +} + +log_allocator_init :: proc(la: ^Log_Allocator, level: Level, allocator := context.allocator, prefix := "") { + la.allocator = allocator + la.level = level + la.prefix = prefix + la.locked = false +} + + +log_allocator :: proc(la: ^Log_Allocator) -> runtime.Allocator { + return runtime.Allocator{ + procedure = log_allocator_proc, + data = la, + } +} + +log_allocator_proc :: proc(allocator_data: rawptr, mode: runtime.Allocator_Mode, + size, alignment: int, + old_memory: rawptr, old_size: int, location := #caller_location) -> ([]byte, runtime.Allocator_Error) { + la := (^Log_Allocator)(allocator_data) + + padding := " " if la.prefix != "" else "" + + if !la.locked { + la.locked = true + defer la.locked = false + + switch mode { + case .Alloc: + logf( + level=la.level, + fmt_str = "%s%s>>> ALLOCATOR(mode=.Alloc, size=%d, alignment=%d)", + args = {la.prefix, padding, size, alignment}, + location = location, + ) + case .Free: + if old_size != 0 { + logf( + level=la.level, + fmt_str = "%s%s<<< ALLOCATOR(mode=.Free, ptr=%p, size=%d)", + args = {la.prefix, padding, old_memory, old_size}, + location = location, + ) + } else { + logf( + level=la.level, + fmt_str = "%s%s<<< ALLOCATOR(mode=.Free, ptr=%p)", + args = {la.prefix, padding, old_memory}, + location = location, + ) + } + case .Free_All: + logf( + level=la.level, + fmt_str = "%s%s<<< ALLOCATOR(mode=.Free_All)", + args = {la.prefix, padding}, + location = location, + ) + case .Resize: + logf( + level=la.level, + fmt_str = "%s%s>>> ALLOCATOR(mode=.Resize, ptr=%p, old_size=%d, size=%d, alignment=%d)", + args = {la.prefix, padding, old_memory, old_size, size, alignment}, + location = location, + ) + case .Query_Features: + logf( + level=la.level, + fmt_str = "%s%ALLOCATOR(mode=.Query_Features)", + args = {la.prefix, padding}, + location = location, + ) + case .Query_Info: + logf( + level=la.level, + fmt_str = "%s%ALLOCATOR(mode=.Query_Info)", + args = {la.prefix, padding}, + location = location, + ) + } + } + + data, err := la.allocator.procedure(la.allocator.data, mode, size, alignment, old_memory, old_size, location) + if !la.locked { + la.locked = true + defer la.locked = false + if err != nil { + logf( + level=la.level, + fmt_str = "%s%ALLOCATOR ERROR=%v", + args = {la.prefix, padding, error}, + location = location, + ) + } + } + return data, err +} \ No newline at end of file diff --git a/core/math/big/prime.odin b/core/math/big/prime.odin index 3cce69675..6f972937a 100644 --- a/core/math/big/prime.odin +++ b/core/math/big/prime.odin @@ -449,7 +449,7 @@ internal_int_is_prime :: proc(a: ^Int, miller_rabin_trials := int(-1), miller_ra in the loop is non-zero, although very low. -- NOTE(Jeroen): This is not yet true in Odin, but I have some ideas. - If the BPSW test and/or the addtional Frobenious test have been + If the BPSW test and/or the additional Frobenious test have been performed instead of just the Miller-Rabin test with the bases 2 and 3, a single extra test should suffice, so such a very unlikely event will not do much harm. diff --git a/core/math/ease/ease.odin b/core/math/ease/ease.odin index 5a767b5a9..0bd7c3641 100644 --- a/core/math/ease/ease.odin +++ b/core/math/ease/ease.odin @@ -325,9 +325,9 @@ ease :: proc "contextless" (type: Ease, p: $T) -> T // in case type was invalid return 0 } - Flux_Map :: struct($T: typeid) { values: map[^T]Flux_Tween(T), + keys_to_be_deleted: [dynamic]^T, } Flux_Tween :: struct($T: typeid) { @@ -353,15 +353,17 @@ Flux_Tween :: struct($T: typeid) { } // init flux map to a float type and a wanted cap -flux_init :: proc($T: typeid, cap := 8) -> Flux_Map(T) where intrinsics.type_is_float(T) { +flux_init :: proc($T: typeid, value_capacity := 8) -> Flux_Map(T) where intrinsics.type_is_float(T) { return { - make(map[^T]Flux_Tween(T), cap), + values = make(map[^T]Flux_Tween(T), value_capacity), + keys_to_be_deleted = make([dynamic]^T, 0, value_capacity) } } // delete map content flux_destroy :: proc(flux: Flux_Map($T)) where intrinsics.type_is_float(T) { delete(flux.values) + delete(flux.keys_to_be_deleted) } // clear map content, stops all animations @@ -374,8 +376,8 @@ flux_clear :: proc(flux: ^Flux_Map($T)) where intrinsics.type_is_float(T) { // return value can be used to set callbacks flux_to :: proc( flux: ^Flux_Map($T), - value: ^f32, - goal: f32, + value: ^T, + goal: T, type: Ease = .Quadratic_Out, duration: time.Duration = time.Second, delay: f64 = 0, @@ -413,6 +415,8 @@ flux_tween_init :: proc(tween: ^Flux_Tween($T), duration: time.Duration) where i // calls callbacks in all stages, when they're filled // deletes tween from the map after completion flux_update :: proc(flux: ^Flux_Map($T), dt: f64) where intrinsics.type_is_float(T) { + clear(&flux.keys_to_be_deleted) + for key, tween in &flux.values { delay_remainder := f64(0) @@ -451,7 +455,8 @@ flux_update :: proc(flux: ^Flux_Map($T), dt: f64) where intrinsics.type_is_float } if tween.progress >= 1 { - delete_key(&flux.values, key) + // append keys to array that will be deleted after the loop + append(&flux.keys_to_be_deleted, key) if tween.on_complete != nil { tween.on_complete(flux, tween.data) @@ -459,17 +464,24 @@ flux_update :: proc(flux: ^Flux_Map($T), dt: f64) where intrinsics.type_is_float } } } + + // loop through keys that should be deleted from the map + if len(flux.keys_to_be_deleted) != 0 { + for key in flux.keys_to_be_deleted { + delete_key(&flux.values, key) + } + } } // stop a specific key inside the map // returns true when it successfully removed the key flux_stop :: proc(flux: ^Flux_Map($T), key: ^T) -> bool where intrinsics.type_is_float(T) { - if key in flux.values { - delete_key(&flux.values, key) - return true - } + if key in flux.values { + delete_key(&flux.values, key) + return true + } - return false + return false } // returns the amount of time left for the tween animation, if the key exists in the map diff --git a/core/math/linalg/specific.odin b/core/math/linalg/specific.odin index a4aaeb012..c4ecb194f 100644 --- a/core/math/linalg/specific.odin +++ b/core/math/linalg/specific.odin @@ -476,21 +476,21 @@ quaternion_angle_axis :: proc{ angle_from_quaternion_f16 :: proc(q: Quaternionf16) -> f16 { if abs(q.w) > math.SQRT_THREE*0.5 { - return math.asin(q.x*q.x + q.y*q.y + q.z*q.z) * 2 + return math.asin(math.sqrt(q.x*q.x + q.y*q.y + q.z*q.z)) * 2 } return math.acos(q.w) * 2 } angle_from_quaternion_f32 :: proc(q: Quaternionf32) -> f32 { if abs(q.w) > math.SQRT_THREE*0.5 { - return math.asin(q.x*q.x + q.y*q.y + q.z*q.z) * 2 + return math.asin(math.sqrt(q.x*q.x + q.y*q.y + q.z*q.z)) * 2 } return math.acos(q.w) * 2 } angle_from_quaternion_f64 :: proc(q: Quaternionf64) -> f64 { if abs(q.w) > math.SQRT_THREE*0.5 { - return math.asin(q.x*q.x + q.y*q.y + q.z*q.z) * 2 + return math.asin(math.sqrt(q.x*q.x + q.y*q.y + q.z*q.z)) * 2 } return math.acos(q.w) * 2 diff --git a/core/math/math.odin b/core/math/math.odin index b711c160f..5a6115e55 100644 --- a/core/math/math.odin +++ b/core/math/math.odin @@ -185,16 +185,23 @@ log :: proc{ log_f64, log_f64le, log_f64be, } -log2_f16 :: logb_f16 -log2_f16le :: logb_f16le -log2_f16be :: logb_f16be -log2_f32 :: logb_f32 -log2_f32le :: logb_f32le -log2_f32be :: logb_f32be -log2_f64 :: logb_f64 -log2_f64le :: logb_f64le -log2_f64be :: logb_f64be -log2 :: logb +log2_f16 :: proc "contextless" (x: f16) -> f16 { return log(f16(x), f16(2.0)) } +log2_f16le :: proc "contextless" (x: f16le) -> f16le { return f16le(log_f16(f16(x), f16(2.0))) } +log2_f16be :: proc "contextless" (x: f16be) -> f16be { return f16be(log_f16(f16(x), f16(2.0))) } + +log2_f32 :: proc "contextless" (x: f32) -> f32 { return log(f32(x), f32(2.0)) } +log2_f32le :: proc "contextless" (x: f32le) -> f32le { return f32le(log_f32(f32(x), f32(2.0))) } +log2_f32be :: proc "contextless" (x: f32be) -> f32be { return f32be(log_f32(f32(x), f32(2.0))) } + +log2_f64 :: proc "contextless" (x: f64) -> f64 { return log(f64(x), f64(2.0)) } +log2_f64le :: proc "contextless" (x: f64le) -> f64le { return f64le(log_f64(f64(x), f64(2.0))) } +log2_f64be :: proc "contextless" (x: f64be) -> f64be { return f64be(log_f64(f64(x), f64(2.0))) } + +log2 :: proc{ + log2_f16, log2_f16le, log2_f16be, + log2_f32, log2_f32le, log2_f32be, + log2_f64, log2_f64le, log2_f64be, +} log10_f16 :: proc "contextless" (x: f16) -> f16 { return ln(x)/LN10 } log10_f16le :: proc "contextless" (x: f16le) -> f16le { return f16le(log10_f16(f16(x))) } @@ -1357,7 +1364,7 @@ atan :: proc "contextless" (x: $T) -> T where intrinsics.type_is_float(T) { } asin :: proc "contextless" (x: $T) -> T where intrinsics.type_is_float(T) { - return atan2(x, 1 + sqrt(1 - x*x)) + return atan2(x, sqrt(1 - x*x)) } acos :: proc "contextless" (x: $T) -> T where intrinsics.type_is_float(T) { diff --git a/core/mem/alloc.odin b/core/mem/alloc.odin index 7416ebdb7..54004d333 100644 --- a/core/mem/alloc.odin +++ b/core/mem/alloc.odin @@ -61,114 +61,38 @@ DEFAULT_PAGE_SIZE :: 4 * 1024 alloc :: proc(size: int, alignment: int = DEFAULT_ALIGNMENT, allocator := context.allocator, loc := #caller_location) -> rawptr { - if size == 0 { - return nil - } - if allocator.procedure == nil { - return nil - } - data, err := allocator.procedure(allocator.data, Allocator_Mode.Alloc, size, alignment, nil, 0, loc) - _ = err + data, _ := runtime.mem_alloc(size, alignment, allocator, loc) return raw_data(data) } alloc_bytes :: proc(size: int, alignment: int = DEFAULT_ALIGNMENT, allocator := context.allocator, loc := #caller_location) -> ([]byte, Allocator_Error) { - if size == 0 { - return nil, nil - } - if allocator.procedure == nil { - return nil, nil - } - return allocator.procedure(allocator.data, Allocator_Mode.Alloc, size, alignment, nil, 0, loc) + return runtime.mem_alloc(size, alignment, allocator, loc) } free :: proc(ptr: rawptr, allocator := context.allocator, loc := #caller_location) -> Allocator_Error { - if ptr == nil { - return nil - } - if allocator.procedure == nil { - return nil - } - _, err := allocator.procedure(allocator.data, Allocator_Mode.Free, 0, 0, ptr, 0, loc) - return err + return runtime.mem_free(ptr, allocator, loc) } free_bytes :: proc(bytes: []byte, allocator := context.allocator, loc := #caller_location) -> Allocator_Error { - if bytes == nil { - return nil - } - if allocator.procedure == nil { - return nil - } - _, err := allocator.procedure(allocator.data, Allocator_Mode.Free, 0, 0, raw_data(bytes), len(bytes), loc) - return err + return runtime.mem_free_bytes(bytes, allocator, loc) } free_all :: proc(allocator := context.allocator, loc := #caller_location) -> Allocator_Error { - if allocator.procedure != nil { - _, err := allocator.procedure(allocator.data, Allocator_Mode.Free_All, 0, 0, nil, 0, loc) - return err - } - return nil + return runtime.mem_free_all(allocator, loc) } resize :: proc(ptr: rawptr, old_size, new_size: int, alignment: int = DEFAULT_ALIGNMENT, allocator := context.allocator, loc := #caller_location) -> rawptr { - if allocator.procedure == nil { - return nil - } - if new_size == 0 { - if ptr != nil { - allocator.procedure(allocator.data, Allocator_Mode.Free, 0, 0, ptr, old_size, loc) - } - return nil - } else if ptr == nil { - _, err := allocator.procedure(allocator.data, Allocator_Mode.Alloc, new_size, alignment, nil, 0, loc) - _ = err - return nil - } - data, err := allocator.procedure(allocator.data, Allocator_Mode.Resize, new_size, alignment, ptr, old_size, loc) - if err == .Mode_Not_Implemented { - data, err = allocator.procedure(allocator.data, Allocator_Mode.Alloc, new_size, alignment, nil, 0, loc) - if err != nil { - return nil - } - runtime.copy(data, byte_slice(ptr, old_size)) - _, err = allocator.procedure(allocator.data, Allocator_Mode.Free, 0, 0, ptr, old_size, loc) - return raw_data(data) - } + data, _ := runtime.mem_resize(ptr, old_size, new_size, alignment, allocator, loc) return raw_data(data) } resize_bytes :: proc(old_data: []byte, new_size: int, alignment: int = DEFAULT_ALIGNMENT, allocator := context.allocator, loc := #caller_location) -> ([]byte, Allocator_Error) { - if allocator.procedure == nil { - return nil, nil - } - ptr := raw_data(old_data) - old_size := len(old_data) - if new_size == 0 { - if ptr != nil { - _, err := allocator.procedure(allocator.data, Allocator_Mode.Free, 0, 0, ptr, old_size, loc) - return nil, err - } - return nil, nil - } else if ptr == nil { - return allocator.procedure(allocator.data, Allocator_Mode.Alloc, new_size, alignment, nil, 0, loc) - } - data, err := allocator.procedure(allocator.data, Allocator_Mode.Resize, new_size, alignment, ptr, old_size, loc) - if err == .Mode_Not_Implemented { - data, err = allocator.procedure(allocator.data, Allocator_Mode.Alloc, new_size, alignment, nil, 0, loc) - if err != nil { - return data, err - } - runtime.copy(data, old_data) - _, err = allocator.procedure(allocator.data, Allocator_Mode.Free, 0, 0, ptr, old_size, loc) - } - return data, err + return runtime.mem_resize(raw_data(old_data), len(old_data), new_size, alignment, allocator, loc) } query_features :: proc(allocator: Allocator, loc := #caller_location) -> (set: Allocator_Mode_Set) { if allocator.procedure != nil { - allocator.procedure(allocator.data, Allocator_Mode.Query_Features, 0, 0, &set, 0, loc) + allocator.procedure(allocator.data, .Query_Features, 0, 0, &set, 0, loc) return set } return nil @@ -177,7 +101,7 @@ query_features :: proc(allocator: Allocator, loc := #caller_location) -> (set: A query_info :: proc(pointer: rawptr, allocator: Allocator, loc := #caller_location) -> (props: Allocator_Query_Info) { props.pointer = pointer if allocator.procedure != nil { - allocator.procedure(allocator.data, Allocator_Mode.Query_Info, 0, 0, &props, 0, loc) + allocator.procedure(allocator.data, .Query_Info, 0, 0, &props, 0, loc) } return } diff --git a/core/mem/allocators.odin b/core/mem/allocators.odin index e2a0cc0fc..0d52d2599 100644 --- a/core/mem/allocators.odin +++ b/core/mem/allocators.odin @@ -31,6 +31,14 @@ Arena_Temp_Memory :: struct { } +arena_init :: proc(a: ^Arena, data: []byte) { + a.data = data + a.offset = 0 + a.peak_used = 0 + a.temp_count = 0 +} + +@(deprecated="prefer 'mem.arena_init'") init_arena :: proc(a: ^Arena, data: []byte) { a.data = data a.offset = 0 @@ -293,6 +301,14 @@ Stack :: struct { peak_used: int, } +stack_init :: proc(s: ^Stack, data: []byte) { + s.data = data + s.prev_offset = 0 + s.curr_offset = 0 + s.peak_used = 0 +} + +@(deprecated="prefer 'mem.stack_init'") init_stack :: proc(s: ^Stack, data: []byte) { s.data = data s.prev_offset = 0 @@ -445,27 +461,34 @@ Small_Stack_Allocation_Header :: struct { // Small_Stack is a stack-like allocator which uses the smallest possible header but at the cost of non-strict memory freeing order Small_Stack :: struct { - data: []byte, - offset: int, + data: []byte, + offset: int, peak_used: int, } +small_stack_init :: proc(s: ^Small_Stack, data: []byte) { + s.data = data + s.offset = 0 + s.peak_used = 0 +} + +@(deprecated="prefer 'small_stack_init'") init_small_stack :: proc(s: ^Small_Stack, data: []byte) { - s.data = data - s.offset = 0 + s.data = data + s.offset = 0 s.peak_used = 0 } small_stack_allocator :: proc(stack: ^Small_Stack) -> Allocator { return Allocator{ procedure = small_stack_allocator_proc, - data = stack, + data = stack, } } small_stack_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode, size, alignment: int, - old_memory: rawptr, old_size: int, ocation := #caller_location) -> ([]byte, Allocator_Error) { + old_memory: rawptr, old_size: int, location := #caller_location) -> ([]byte, Allocator_Error) { s := cast(^Small_Stack)allocator_data if s.data == nil { diff --git a/core/mem/raw.odin b/core/mem/raw.odin index 2bce2d7aa..8322ace30 100644 --- a/core/mem/raw.odin +++ b/core/mem/raw.odin @@ -8,6 +8,7 @@ Raw_Cstring :: runtime.Raw_Cstring Raw_Slice :: runtime.Raw_Slice Raw_Dynamic_Array :: runtime.Raw_Dynamic_Array Raw_Map :: runtime.Raw_Map +Raw_Soa_Pointer :: runtime.Raw_Soa_Pointer Raw_Complex64 :: struct {real, imag: f32} Raw_Complex128 :: struct {real, imag: f64} diff --git a/core/mem/virtual/arena_util.odin b/core/mem/virtual/arena_util.odin index 408566299..0e152db7a 100644 --- a/core/mem/virtual/arena_util.odin +++ b/core/mem/virtual/arena_util.odin @@ -1,5 +1,10 @@ package mem_virtual +arena_init :: proc{ + static_arena_init, + growing_arena_init, +} + arena_temp_begin :: proc{ static_arena_temp_begin, growing_arena_temp_begin, diff --git a/core/mem/virtual/growing_arena.odin b/core/mem/virtual/growing_arena.odin index 9a4272a1e..f7f32b48e 100644 --- a/core/mem/virtual/growing_arena.odin +++ b/core/mem/virtual/growing_arena.odin @@ -13,6 +13,13 @@ Growing_Arena :: struct { DEFAULT_MINIMUM_BLOCK_SIZE :: 1<<20 // 1 MiB should be enough +growing_arena_init :: proc(arena: ^Static_Arena, reserved: uint = DEFAULT_MINIMUM_BLOCK_SIZE) -> (err: Allocator_Error) { + arena.block = memory_block_alloc(0, reserved, {}) or_return + arena.total_used = 0 + arena.total_reserved = arena.block.reserved + return +} + growing_arena_alloc :: proc(arena: ^Growing_Arena, min_size: int, alignment: int) -> (data: []byte, err: Allocator_Error) { align_forward_offset :: proc "contextless" (arena: ^Growing_Arena, alignment: int) -> uint #no_bounds_check { alignment_offset := uint(0) @@ -37,7 +44,7 @@ growing_arena_alloc :: proc(arena: ^Growing_Arena, min_size: int, alignment: int block_size := max(size, arena.minimum_block_size) - new_block := memory_block_alloc(block_size, block_size, {}) or_return + new_block := memory_block_alloc(size, block_size, {}) or_return new_block.prev = arena.curr_block arena.curr_block = new_block arena.total_reserved += new_block.reserved diff --git a/core/mem/virtual/virtual_darwin.odin b/core/mem/virtual/virtual_darwin.odin new file mode 100644 index 000000000..5505149b0 --- /dev/null +++ b/core/mem/virtual/virtual_darwin.odin @@ -0,0 +1,148 @@ +//+build darwin +//+private +package mem_virtual + +foreign import libc "System.framework" +import "core:c" + +PROT_NONE :: 0x0 /* [MC2] no permissions */ +PROT_READ :: 0x1 /* [MC2] pages can be read */ +PROT_WRITE :: 0x2 /* [MC2] pages can be written */ +PROT_EXEC :: 0x4 /* [MC2] pages can be executed */ + +// Sharing options +MAP_SHARED :: 0x1 /* [MF|SHM] share changes */ +MAP_PRIVATE :: 0x2 /* [MF|SHM] changes are private */ + +// Other flags +MAP_FIXED :: 0x0010 /* [MF|SHM] interpret addr exactly */ +MAP_RENAME :: 0x0020 /* Sun: rename private pages to file */ +MAP_NORESERVE :: 0x0040 /* Sun: don't reserve needed swap area */ +MAP_RESERVED0080 :: 0x0080 /* previously unimplemented MAP_INHERIT */ +MAP_NOEXTEND :: 0x0100 /* for MAP_FILE, don't change file size */ +MAP_HASSEMAPHORE :: 0x0200 /* region may contain semaphores */ +MAP_NOCACHE :: 0x0400 /* don't cache pages for this mapping */ +MAP_JIT :: 0x0800 /* Allocate a region that will be used for JIT purposes */ + +// Mapping type +MAP_FILE :: 0x0000 /* map from file (default) */ +MAP_ANONYMOUS :: 0x1000 /* allocated from memory, swap space */ + + +/* + * The MAP_RESILIENT_* flags can be used when the caller wants to map some + * possibly unreliable memory and be able to access it safely, possibly + * getting the wrong contents rather than raising any exception. + * For safety reasons, such mappings have to be read-only (PROT_READ access + * only). + * + * MAP_RESILIENT_CODESIGN: + * accessing this mapping will not generate code-signing violations, + * even if the contents are tainted. + * MAP_RESILIENT_MEDIA: + * accessing this mapping will not generate an exception if the contents + * are not available (unreachable removable or remote media, access beyond + * end-of-file, ...). Missing contents will be replaced with zeroes. + */ +MAP_RESILIENT_CODESIGN :: 0x2000 /* no code-signing failures */ +MAP_RESILIENT_MEDIA :: 0x4000 /* no backing-store failures */ + +MAP_32BIT :: 0x8000 /* Return virtual addresses <4G only */ + +// Flags used to support translated processes. +MAP_TRANSLATED_ALLOW_EXECUTE :: 0x20000 /* allow execute in translated processes */ +MAP_UNIX03 :: 0x40000 /* UNIX03 compliance */ + +// Process memory locking +MCL_CURRENT :: 0x0001 /* [ML] Lock only current memory */ +MCL_FUTURE :: 0x0002 /* [ML] Lock all future memory as well */ + +MADV_NORMAL :: 0 /* [MC1] no further special treatment */ +MADV_RANDOM :: 1 /* [MC1] expect random page refs */ +MADV_SEQUENTIAL :: 2 /* [MC1] expect sequential page refs */ +MADV_WILLNEED :: 3 /* [MC1] will need these pages */ +MADV_DONTNEED :: 4 /* [MC1] dont need these pages */ +MADV_FREE :: 5 /* pages unneeded, discard contents */ +MADV_ZERO_WIRED_PAGES :: 6 /* zero the wired pages that have not been unwired before the entry is deleted */ +MADV_FREE_REUSABLE :: 7 /* pages can be reused (by anyone) */ +MADV_FREE_REUSE :: 8 /* caller wants to reuse those pages */ +MADV_CAN_REUSE :: 9 +MADV_PAGEOUT :: 10 /* page out now (internal only) */ + +// msync() flags +MS_ASYNC :: 0x0001 /* [MF|SIO] return immediately */ +MS_INVALIDATE :: 0x0002 /* [MF|SIO] invalidate all cached data */ +MS_SYNC :: 0x0010 /* [MF|SIO] msync synchronously */ +MS_KILLPAGES :: 0x0004 /* invalidate pages, leave mapped */ +MS_DEACTIVATE :: 0x0008 /* deactivate pages, leave mapped */ + +// Return bits from mincore +MINCORE_INCORE :: 0x1 /* Page is incore */ +MINCORE_REFERENCED :: 0x2 /* Page has been referenced by us */ +MINCORE_MODIFIED :: 0x4 /* Page has been modified by us */ +MINCORE_REFERENCED_OTHER :: 0x8 /* Page has been referenced */ +MINCORE_MODIFIED_OTHER :: 0x10 /* Page has been modified */ +MINCORE_PAGED_OUT :: 0x20 /* Page has been paged out */ +MINCORE_COPIED :: 0x40 /* Page has been copied */ +MINCORE_ANONYMOUS :: 0x80 /* Page belongs to an anonymous object */ + +// Allocation failure result +MAP_FAILED : rawptr = rawptr(~uintptr(0)) + +foreign libc { + @(link_name="mlockall") _mlockall :: proc(flags: c.int) -> c.int --- + @(link_name="munlockall") _munlockall :: proc() -> c.int --- + @(link_name="mlock") _mlock :: proc(addr: rawptr, len: c.size_t) -> c.int --- + @(link_name="mmap") _mmap :: proc(addr: rawptr, len: c.size_t, prot: c.int, flags: c.int, fd: c.int, offset: int) -> rawptr --- + @(link_name="mprotect") _mprotect :: proc(addr: rawptr, len: c.size_t, prot: c.int) -> c.int --- + @(link_name="msync") _msync :: proc(addr: rawptr, len: c.size_t) -> c.int --- + @(link_name="munlock") _munlock :: proc(addr: rawptr, len: c.size_t) -> c.int --- + @(link_name="munmap") _munmap :: proc(addr: rawptr, len: c.size_t) -> c.int --- + @(link_name="shm_open") _shm_open :: proc(name: cstring, oflag: c.int, #c_vararg args: ..any) -> c.int --- + @(link_name="shm_unlink") _shm_unlink :: proc(name: cstring) -> c.int --- + @(link_name="posix_madvise") _posix_madvise :: proc(addr: rawptr, len: c.size_t, advice: c.int) -> c.int --- + @(link_name="madvise") _madvise :: proc(addr: rawptr, len: c.size_t, advice: c.int) -> c.int --- + @(link_name="mincore") _mincore :: proc(addr: rawptr, len: c.size_t, vec: cstring) -> c.int --- + @(link_name="minherit") _minherit :: proc(addr: rawptr, len: c.size_t, inherit: c.int) -> c.int --- +} + + +_reserve :: proc "contextless" (size: uint) -> (data: []byte, err: Allocator_Error) { + result := _mmap(nil, size, PROT_NONE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) + if result == MAP_FAILED { + return nil, .Out_Of_Memory + } + return ([^]byte)(uintptr(result))[:size], nil +} + +_commit :: proc "contextless" (data: rawptr, size: uint) -> Allocator_Error { + result := _mprotect(data, size, PROT_READ|PROT_WRITE) + if result != 0 { + return .Out_Of_Memory + } + return nil +} +_decommit :: proc "contextless" (data: rawptr, size: uint) { + _mprotect(data, size, PROT_NONE) + _madvise(data, size, MADV_FREE) +} +_release :: proc "contextless" (data: rawptr, size: uint) { + _munmap(data, size) +} +_protect :: proc "contextless" (data: rawptr, size: uint, flags: Protect_Flags) -> bool { + pflags: c.int + pflags = PROT_NONE + if .Read in flags { pflags |= PROT_READ } + if .Write in flags { pflags |= PROT_WRITE } + if .Execute in flags { pflags |= PROT_EXEC } + err := _mprotect(data, size, pflags) + return err != 0 +} + + +_platform_memory_init :: proc() { + DEFAULT_PAGE_SIZE = 4096 + + // is power of two + assert(DEFAULT_PAGE_SIZE != 0 && (DEFAULT_PAGE_SIZE & (DEFAULT_PAGE_SIZE-1)) == 0) +} diff --git a/core/odin/ast/ast.odin b/core/odin/ast/ast.odin index f4aa67446..c8f73a31b 100644 --- a/core/odin/ast/ast.odin +++ b/core/odin/ast/ast.odin @@ -552,13 +552,20 @@ unparen_expr :: proc(expr: ^Expr) -> (val: ^Expr) { return } +Field_Flags :: distinct bit_set[Field_Flag] + Field_Flag :: enum { + Invalid, + Unknown, + Ellipsis, Using, No_Alias, C_Vararg, Auto_Cast, Any_Int, + Subtype, + By_Ptr, Results, Tags, @@ -566,11 +573,38 @@ Field_Flag :: enum { Typeid_Token, } -Field_Flags :: distinct bit_set[Field_Flag] +field_flag_strings := [Field_Flag]string{ + .Invalid = "", + .Unknown = "", + + .Ellipsis = "..", + .Using = "using", + .No_Alias = "#no_alias", + .C_Vararg = "#c_vararg", + .Auto_Cast = "auto_cast", + .Any_Int = "#any_int", + .Subtype = "#subtype", + .By_Ptr = "#by_ptr", + + .Results = "results", + .Tags = "field tag", + .Default_Parameters = "default parameters", + .Typeid_Token = "typeid", +} + +field_hash_flag_strings := []struct{key: string, flag: Field_Flag}{ + {"no_alias", .No_Alias}, + {"c_vararg", .C_Vararg}, + {"any_int", .Any_Int}, + {"subtype", .Subtype}, + {"by_ptr", .By_Ptr}, +} + Field_Flags_Struct :: Field_Flags{ .Using, .Tags, + .Subtype, } Field_Flags_Record_Poly_Params :: Field_Flags{ .Typeid_Token, @@ -583,6 +617,7 @@ Field_Flags_Signature :: Field_Flags{ .C_Vararg, .Auto_Cast, .Any_Int, + .By_Ptr, .Default_Parameters, } @@ -665,6 +700,7 @@ Proc_Type :: struct { Pointer_Type :: struct { using node: Expr, + tag: ^Expr, pointer: tokenizer.Pos, elem: ^Expr, } diff --git a/core/odin/ast/clone.odin b/core/odin/ast/clone.odin index 400c064f5..5ec6bc335 100644 --- a/core/odin/ast/clone.odin +++ b/core/odin/ast/clone.odin @@ -286,6 +286,7 @@ clone_node :: proc(node: ^Node) -> ^Node { r.results = auto_cast clone(r.results) case ^Pointer_Type: r.elem = clone(r.elem) + r.tag = clone(r.tag) case ^Multi_Pointer_Type: r.elem = clone(r.elem) case ^Array_Type: diff --git a/core/odin/doc-format/doc_format.odin b/core/odin/doc-format/doc_format.odin index 62682004d..895fcf70d 100644 --- a/core/odin/doc-format/doc_format.odin +++ b/core/odin/doc-format/doc_format.odin @@ -186,6 +186,7 @@ Type_Kind :: enum u32le { Relative_Slice = 21, Multi_Pointer = 22, Matrix = 23, + Soa_Pointer = 24, } Type_Elems_Cap :: 4 @@ -245,6 +246,7 @@ Type :: struct { // .Relative_Slice - 2 types: 0=slice type, 1=base integer // .Multi_Pointer - 1 type: 0=element // .Matrix - 1 type: 0=element + // .Soa_Pointer - 1 type: 0=element types: Array(Type_Index), // Used by: diff --git a/core/odin/parser/parser.odin b/core/odin/parser/parser.odin index 52ecb4781..470aaccbd 100644 --- a/core/odin/parser/parser.odin +++ b/core/odin/parser/parser.odin @@ -1611,20 +1611,6 @@ new_ast_field :: proc(names: []^ast.Expr, type: ^ast.Expr, default_value: ^ast.E return field } - -Field_Prefix :: enum { - Invalid, - Unknown, - - Using, - No_Alias, - C_Vararg, - Auto_Cast, - Any_Int, -} - -Field_Prefixes :: distinct bit_set[Field_Prefix] - Expr_And_Flags :: struct { expr: ^ast.Expr, flags: ast.Field_Flags, @@ -1666,7 +1652,7 @@ convert_to_ident_list :: proc(p: ^Parser, list: []Expr_And_Flags, ignore_flags, return idents[:] } -is_token_field_prefix :: proc(p: ^Parser) -> Field_Prefix { +is_token_field_prefix :: proc(p: ^Parser) -> ast.Field_Flag { #partial switch p.curr_tok.kind { case .EOF: return .Invalid @@ -1677,17 +1663,15 @@ is_token_field_prefix :: proc(p: ^Parser) -> Field_Prefix { advance_token(p) return .Auto_Cast case .Hash: + tok: tokenizer.Token advance_token(p) - defer advance_token(p) - #partial switch p.curr_tok.kind { - case .Ident: - switch p.curr_tok.text { - case "no_alias": - return .No_Alias - case "c_vararg": - return .C_Vararg - case "any_int": - return .Any_Int + tok = p.curr_tok + advance_token(p) + if tok.kind == .Ident { + for kf in ast.field_hash_flag_strings { + if kf.key == tok.text { + return kf.flag + } } } return .Unknown @@ -1695,8 +1679,8 @@ is_token_field_prefix :: proc(p: ^Parser) -> Field_Prefix { return .Invalid } -parse_field_prefixes :: proc(p: ^Parser) -> ast.Field_Flags { - counts: [len(Field_Prefix)]int +parse_field_prefixes :: proc(p: ^Parser) -> (flags: ast.Field_Flags) { + counts: [len(ast.Field_Flag)]int for { kind := is_token_field_prefix(p) @@ -1712,31 +1696,17 @@ parse_field_prefixes :: proc(p: ^Parser) -> ast.Field_Flags { counts[kind] += 1 } - flags: ast.Field_Flags - - for kind in Field_Prefix { + for kind in ast.Field_Flag { count := counts[kind] - switch kind { - case .Invalid, .Unknown: // Ignore - case .Using: - if count > 1 { error(p, p.curr_tok.pos, "multiple 'using' in this field list") } - if count > 0 { flags += {.Using} } - case .No_Alias: - if count > 1 { error(p, p.curr_tok.pos, "multiple '#no_alias' in this field list") } - if count > 0 { flags += {.No_Alias} } - case .C_Vararg: - if count > 1 { error(p, p.curr_tok.pos, "multiple '#c_vararg' in this field list") } - if count > 0 { flags += {.C_Vararg} } - case .Auto_Cast: - if count > 1 { error(p, p.curr_tok.pos, "multiple 'auto_cast' in this field list") } - if count > 0 { flags += {.Auto_Cast} } - case .Any_Int: - if count > 1 { error(p, p.curr_tok.pos, "multiple '#any_int' in this field list") } - if count > 0 { flags += {.Any_Int} } + if kind == .Invalid || kind == .Unknown { + // Ignore + } else { + if count > 1 { error(p, p.curr_tok.pos, "multiple '%s' in this field list", ast.field_flag_strings[kind]) } + if count > 0 { flags += {kind} } } } - return flags + return } check_field_flag_prefixes :: proc(p: ^Parser, name_count: int, allowed_flags, set_flags: ast.Field_Flags) -> (flags: ast.Field_Flags) { @@ -1748,19 +1718,13 @@ check_field_flag_prefixes :: proc(p: ^Parser, name_count: int, allowed_flags, se for flag in ast.Field_Flag { if flag not_in allowed_flags && flag in flags { - switch flag { - case .Using: - error(p, p.curr_tok.pos, "'using' is not allowed within this field list") - case .No_Alias: - error(p, p.curr_tok.pos, "'#no_alias' is not allowed within this field list") - case .C_Vararg: - error(p, p.curr_tok.pos, "'#c_vararg' is not allowed within this field list") - case .Auto_Cast: - error(p, p.curr_tok.pos, "'auto_cast' is not allowed within this field list") - case .Any_Int: - error(p, p.curr_tok.pos, "'#any_int' is not allowed within this field list") + #partial switch flag { + case .Unknown, .Invalid: + // ignore case .Tags, .Ellipsis, .Results, .Default_Parameters, .Typeid_Token: panic("Impossible prefixes") + case: + error(p, p.curr_tok.pos, "'%s' is not allowed within this field list", ast.field_flag_strings[flag]) } flags -= {flag} } @@ -2271,7 +2235,7 @@ parse_operand :: proc(p: ^Parser, lhs: bool) -> ^ast.Expr { return parse_call_expr(p, bd) - case "soa", "simd": + case "soa": bd := ast.new(ast.Basic_Directive, tok.pos, end_pos(name)) bd.tok = tok bd.name = name.text @@ -2280,6 +2244,20 @@ parse_operand :: proc(p: ^Parser, lhs: bool) -> ^ast.Expr { #partial switch t in type.derived_expr { case ^ast.Array_Type: t.tag = bd case ^ast.Dynamic_Array_Type: t.tag = bd + case ^ast.Pointer_Type: t.tag = bd + case: + error(p, original_type.pos, "expected an array or pointer type after #%s", name.text) + } + return original_type + + case "simd": + bd := ast.new(ast.Basic_Directive, tok.pos, end_pos(name)) + bd.tok = tok + bd.name = name.text + original_type := parse_type(p) + type := ast.unparen_expr(original_type) + #partial switch t in type.derived_expr { + case ^ast.Array_Type: t.tag = bd case: error(p, original_type.pos, "expected an array type after #%s", name.text) } @@ -2631,7 +2609,6 @@ parse_operand :: proc(p: ^Parser, lhs: bool) -> ^ast.Expr { tok := expect_token(p, .Union) poly_params: ^ast.Field_List align: ^ast.Expr - is_maybe: bool is_no_nil: bool is_shared_nil: bool @@ -2656,10 +2633,7 @@ parse_operand :: proc(p: ^Parser, lhs: bool) -> ^ast.Expr { } align = parse_expr(p, true) case "maybe": - if is_maybe { - error(p, tag.pos, "duplicate union tag '#%s'", tag.text) - } - is_maybe = true + error(p, tag.pos, "#%s functionality has now been merged with standard 'union' functionality", tag.text) case "no_nil": if is_no_nil { error(p, tag.pos, "duplicate union tag '#%s'", tag.text) @@ -2676,19 +2650,12 @@ parse_operand :: proc(p: ^Parser, lhs: bool) -> ^ast.Expr { } p.expr_level = prev_level - if is_no_nil && is_maybe { - error(p, p.curr_tok.pos, "#maybe and #no_nil cannot be applied together") - } if is_no_nil && is_shared_nil { error(p, p.curr_tok.pos, "#shared_nil and #no_nil cannot be applied together") } - if is_shared_nil && is_maybe { - error(p, p.curr_tok.pos, "#maybe and #shared_nil cannot be applied together") - } union_kind := ast.Union_Type_Kind.Normal switch { - case is_maybe: union_kind = .maybe case is_no_nil: union_kind = .no_nil case is_shared_nil: union_kind = .shared_nil } diff --git a/core/odin/printer/printer.odin b/core/odin/printer/printer.odin index 807cc99bd..63a3b543d 100644 --- a/core/odin/printer/printer.odin +++ b/core/odin/printer/printer.odin @@ -151,7 +151,7 @@ print :: proc(p: ^Printer, file: ^ast.File) -> string { fix_lines(p) - builder := strings.make_builder(0, 5 * mem.Megabyte, p.allocator) + builder := strings.builder_make(0, 5 * mem.Megabyte, p.allocator) last_line := 0 diff --git a/core/odin/printer/visit.odin b/core/odin/printer/visit.odin index 70140f180..66166aa81 100644 --- a/core/odin/printer/visit.odin +++ b/core/odin/printer/visit.odin @@ -71,7 +71,7 @@ push_comment :: proc(p: ^Printer, comment: tokenizer.Token) -> int { return 0 } else { - builder := strings.make_builder(context.temp_allocator) + builder := strings.builder_make(context.temp_allocator) c_len := len(comment.text) trim_space := true @@ -90,12 +90,12 @@ push_comment :: proc(p: ^Printer, comment: tokenizer.Token) -> int { continue case c == '\r' && comment.text[min(c_len - 1, i + 1)] == '\n': append(&multilines, strings.to_string(builder)) - builder = strings.make_builder(context.temp_allocator) + builder = strings.builder_make(context.temp_allocator) trim_space = true i += 1 case c == '\n': append(&multilines, strings.to_string(builder)) - builder = strings.make_builder(context.temp_allocator) + builder = strings.builder_make(context.temp_allocator) trim_space = true case c == '/' && comment.text[min(c_len - 1, i + 1)] == '*': strings.write_string(&builder, "/*") diff --git a/core/os/dir_freebsd.odin b/core/os/dir_freebsd.odin index 74c410a51..c664ffb34 100644 --- a/core/os/dir_freebsd.odin +++ b/core/os/dir_freebsd.odin @@ -1,6 +1,5 @@ package os -import "core:strings" import "core:mem" read_dir :: proc(fd: Handle, n: int, allocator := context.allocator) -> (fi: []File_Info, err: Errno) { @@ -51,10 +50,13 @@ read_dir :: proc(fd: Handle, n: int, allocator := context.allocator) -> (fi: []F continue } - fullpath := strings.join( []string{ dirpath, filename }, "/", context.temp_allocator) + fullpath := make([]byte, len(dirpath)+1+len(filename)) + copy(fullpath, dirpath) + copy(fullpath[len(dirpath):], "/") + copy(fullpath[len(dirpath)+1:], filename) defer delete(fullpath, context.temp_allocator) - fi_, err = stat(fullpath, allocator) + fi_, err = stat(string(fullpath), allocator) if err != ERROR_NONE { for fi__ in dfi { file_info_delete(fi__, allocator) diff --git a/core/os/env_windows.odin b/core/os/env_windows.odin index 9a33a0611..6e14127ed 100644 --- a/core/os/env_windows.odin +++ b/core/os/env_windows.odin @@ -11,24 +11,24 @@ lookup_env :: proc(key: string, allocator := context.allocator) -> (value: strin return } wkey := win32.utf8_to_wstring(key) - b := make([dynamic]u16, 100, context.temp_allocator) - for { - n := win32.GetEnvironmentVariableW(wkey, raw_data(b), u32(len(b))) - if n == 0 { - err := win32.GetLastError() - if err == u32(ERROR_ENVVAR_NOT_FOUND) { - return "", false - } + n := win32.GetEnvironmentVariableW(wkey, nil, 0) + if n == 0 { + err := win32.GetLastError() + if err == u32(ERROR_ENVVAR_NOT_FOUND) { + return "", false } - - if n <= u32(len(b)) { - value, _ = win32.utf16_to_utf8(b[:n], allocator) - found = true - return - } - - resize(&b, len(b)*2) } + b := make([dynamic]u16, n, context.temp_allocator) + n = win32.GetEnvironmentVariableW(wkey, raw_data(b), u32(len(b))) + if n == 0 { + err := win32.GetLastError() + if err == u32(ERROR_ENVVAR_NOT_FOUND) { + return "", false + } + } + value, _ = win32.utf16_to_utf8(b[:n], allocator) + found = true + return } diff --git a/core/os/os2/user.odin b/core/os/os2/user.odin index 1fb653b85..00cccd2a7 100644 --- a/core/os/os2/user.odin +++ b/core/os/os2/user.odin @@ -6,19 +6,19 @@ import "core:runtime" user_cache_dir :: proc(allocator: runtime.Allocator) -> (dir: string, err: Error) { #partial switch ODIN_OS { case .Windows: - dir = get_env("LocalAppData") + dir = get_env("LocalAppData", allocator) if dir != "" { dir = strings.clone_safe(dir, allocator) or_return } case .Darwin: - dir = get_env("HOME") + dir = get_env("HOME", allocator) if dir != "" { dir = strings.concatenate_safe({dir, "/Library/Caches"}, allocator) or_return } case: // All other UNIX systems - dir = get_env("XDG_CACHE_HOME") + dir = get_env("XDG_CACHE_HOME", allocator) if dir == "" { - dir = get_env("HOME") + dir = get_env("HOME", allocator) if dir == "" { return } @@ -34,19 +34,19 @@ user_cache_dir :: proc(allocator: runtime.Allocator) -> (dir: string, err: Error user_config_dir :: proc(allocator: runtime.Allocator) -> (dir: string, err: Error) { #partial switch ODIN_OS { case .Windows: - dir = get_env("AppData") + dir = get_env("AppData", allocator) if dir != "" { dir = strings.clone_safe(dir, allocator) or_return } case .Darwin: - dir = get_env("HOME") + dir = get_env("HOME", allocator) if dir != "" { dir = strings.concatenate_safe({dir, "/Library/Application Support"}, allocator) or_return } case: // All other UNIX systems - dir = get_env("XDG_CACHE_HOME") + dir = get_env("XDG_CACHE_HOME", allocator) if dir == "" { - dir = get_env("HOME") + dir = get_env("HOME", allocator) if dir == "" { return } @@ -59,13 +59,13 @@ user_config_dir :: proc(allocator: runtime.Allocator) -> (dir: string, err: Erro return } -user_home_dir :: proc() -> (dir: string, err: Error) { +user_home_dir :: proc(allocator: runtime.Allocator) -> (dir: string, err: Error) { env := "HOME" #partial switch ODIN_OS { case .Windows: env = "USERPROFILE" } - if v := get_env(env); v != "" { + if v := get_env(env, allocator); v != "" { return v, nil } return "", .Invalid_Path diff --git a/core/os/os_freebsd.odin b/core/os/os_freebsd.odin index a991caafc..adf4f246f 100644 --- a/core/os/os_freebsd.odin +++ b/core/os/os_freebsd.odin @@ -241,13 +241,13 @@ S_ISGID :: 0o2000 // Set group id on execution S_ISVTX :: 0o1000 // Directory restrcted delete -S_ISLNK :: #force_inline proc(m: mode_t) -> bool do return (m & S_IFMT) == S_IFLNK -S_ISREG :: #force_inline proc(m: mode_t) -> bool do return (m & S_IFMT) == S_IFREG -S_ISDIR :: #force_inline proc(m: mode_t) -> bool do return (m & S_IFMT) == S_IFDIR -S_ISCHR :: #force_inline proc(m: mode_t) -> bool do return (m & S_IFMT) == S_IFCHR -S_ISBLK :: #force_inline proc(m: mode_t) -> bool do return (m & S_IFMT) == S_IFBLK -S_ISFIFO :: #force_inline proc(m: mode_t) -> bool do return (m & S_IFMT) == S_IFIFO -S_ISSOCK :: #force_inline proc(m: mode_t) -> bool do return (m & S_IFMT) == S_IFSOCK +S_ISLNK :: #force_inline proc(m: mode_t) -> bool { return (m & S_IFMT) == S_IFLNK } +S_ISREG :: #force_inline proc(m: mode_t) -> bool { return (m & S_IFMT) == S_IFREG } +S_ISDIR :: #force_inline proc(m: mode_t) -> bool { return (m & S_IFMT) == S_IFDIR } +S_ISCHR :: #force_inline proc(m: mode_t) -> bool { return (m & S_IFMT) == S_IFCHR } +S_ISBLK :: #force_inline proc(m: mode_t) -> bool { return (m & S_IFMT) == S_IFBLK } +S_ISFIFO :: #force_inline proc(m: mode_t) -> bool { return (m & S_IFMT) == S_IFIFO } +S_ISSOCK :: #force_inline proc(m: mode_t) -> bool { return (m & S_IFMT) == S_IFSOCK } F_OK :: 0 // Test for file existance X_OK :: 1 // Test for execute permission @@ -257,7 +257,7 @@ R_OK :: 4 // Test for read permission foreign libc { @(link_name="__error") __errno_location :: proc() -> ^int --- - @(link_name="open") _unix_open :: proc(path: cstring, flags: c.int, mode: c.int) -> Handle --- + @(link_name="open") _unix_open :: proc(path: cstring, flags: c.int, mode: c.int) -> Handle --- @(link_name="close") _unix_close :: proc(fd: Handle) -> c.int --- @(link_name="read") _unix_read :: proc(fd: Handle, buf: rawptr, size: c.size_t) -> c.ssize_t --- @(link_name="write") _unix_write :: proc(fd: Handle, buf: rawptr, size: c.size_t) -> c.ssize_t --- diff --git a/core/path/filepath/match.odin b/core/path/filepath/match.odin index 00a9c9fb0..c932f202a 100644 --- a/core/path/filepath/match.odin +++ b/core/path/filepath/match.odin @@ -271,7 +271,7 @@ _glob :: proc(dir, pattern: string, matches: ^[dynamic]string, allocator := cont } - d, derr := os.open(dir) + d, derr := os.open(dir, os.O_RDONLY) if derr != 0 { return } diff --git a/core/path/slashpath/path.odin b/core/path/slashpath/path.odin index 8ac10e655..865f619bf 100644 --- a/core/path/slashpath/path.odin +++ b/core/path/slashpath/path.odin @@ -146,7 +146,7 @@ clean :: proc(path: string, allocator := context.allocator) -> string { } // join joins numerous path elements into a single path -join :: proc(elems: ..string, allocator := context.allocator) -> string { +join :: proc(elems: []string, allocator := context.allocator) -> string { context.allocator = allocator for elem, i in elems { if elem != "" { diff --git a/core/reflect/reflect.odin b/core/reflect/reflect.odin index 27a83e680..794f9668a 100644 --- a/core/reflect/reflect.odin +++ b/core/reflect/reflect.odin @@ -34,6 +34,7 @@ Type_Info_Simd_Vector :: runtime.Type_Info_Simd_Vector Type_Info_Relative_Pointer :: runtime.Type_Info_Relative_Pointer Type_Info_Relative_Slice :: runtime.Type_Info_Relative_Slice Type_Info_Matrix :: runtime.Type_Info_Matrix +Type_Info_Soa_Pointer :: runtime.Type_Info_Soa_Pointer Type_Info_Enum_Value :: runtime.Type_Info_Enum_Value @@ -68,6 +69,7 @@ Type_Kind :: enum { Relative_Pointer, Relative_Slice, Matrix, + Soa_Pointer, } @@ -102,6 +104,7 @@ type_kind :: proc(T: typeid) -> Type_Kind { case Type_Info_Relative_Pointer: return .Relative_Pointer case Type_Info_Relative_Slice: return .Relative_Slice case Type_Info_Matrix: return .Matrix + case Type_Info_Soa_Pointer: return .Soa_Pointer } } @@ -194,6 +197,7 @@ typeid_elem :: proc(id: typeid) -> typeid { } case Type_Info_Pointer: return v.elem.id case Type_Info_Multi_Pointer: return v.elem.id + case Type_Info_Soa_Pointer: return v.elem.id case Type_Info_Array: return v.elem.id case Type_Info_Enumerated_Array: return v.elem.id case Type_Info_Slice: return v.elem.id @@ -1419,6 +1423,7 @@ equal :: proc(a, b: any, including_indirect_array_recursion := false, recursion_ Type_Info_Enum, Type_Info_Simd_Vector, Type_Info_Relative_Pointer, + Type_Info_Soa_Pointer, Type_Info_Matrix: return mem.compare_byte_ptrs((^byte)(a.data), (^byte)(b.data), t.size) == 0 diff --git a/core/reflect/types.odin b/core/reflect/types.odin index b211abb45..edd4f7a26 100644 --- a/core/reflect/types.odin +++ b/core/reflect/types.odin @@ -68,6 +68,11 @@ are_types_identical :: proc(a, b: ^Type_Info) -> bool { y := b.variant.(Type_Info_Multi_Pointer) or_return return are_types_identical(x.elem, y.elem) + case Type_Info_Soa_Pointer: + y := b.variant.(Type_Info_Soa_Pointer) or_return + return are_types_identical(x.elem, y.elem) + + case Type_Info_Procedure: y := b.variant.(Type_Info_Procedure) or_return switch { @@ -256,6 +261,11 @@ is_multi_pointer :: proc(info: ^Type_Info) -> bool { _, ok := type_info_base(info).variant.(Type_Info_Multi_Pointer) return ok } +is_soa_pointer :: proc(info: ^Type_Info) -> bool { + if info == nil { return false } + _, ok := type_info_base(info).variant.(Type_Info_Soa_Pointer) + return ok +} is_pointer_internally :: proc(info: ^Type_Info) -> bool { if info == nil { return false } #partial switch v in info.variant { @@ -437,6 +447,9 @@ write_type_writer :: proc(w: io.Writer, ti: ^Type_Info, n_written: ^int = nil) - case Type_Info_Multi_Pointer: io.write_string(w, "[^]", &n) or_return write_type(w, info.elem, &n) or_return + case Type_Info_Soa_Pointer: + io.write_string(w, "#soa ^", &n) or_return + write_type(w, info.elem, &n) or_return case Type_Info_Procedure: io.write_string(w, "proc", &n) or_return if info.params == nil { @@ -573,11 +586,11 @@ write_type_writer :: proc(w: io.Writer, ti: ^Type_Info, n_written: ^int = nil) - write_type(w, info.elem, &n) or_return case is_rune(info.elem): io.write_encoded_rune(w, rune(info.lower), true, &n) or_return - io.write_string(w, "..", &n) or_return + io.write_string(w, "..=", &n) or_return io.write_encoded_rune(w, rune(info.upper), true, &n) or_return case: io.write_i64(w, info.lower, 10, &n) or_return - io.write_string(w, "..", &n) or_return + io.write_string(w, "..=", &n) or_return io.write_i64(w, info.upper, 10, &n) or_return } if info.underlying != nil { diff --git a/core/runtime/core.odin b/core/runtime/core.odin index 8fb3d7210..0310aff6d 100644 --- a/core/runtime/core.odin +++ b/core/runtime/core.odin @@ -176,6 +176,9 @@ Type_Info_Matrix :: struct { column_count: int, // Total element count = column_count * elem_stride } +Type_Info_Soa_Pointer :: struct { + elem: ^Type_Info, +} Type_Info_Flag :: enum u8 { Comparable = 0, @@ -217,6 +220,7 @@ Type_Info :: struct { Type_Info_Relative_Pointer, Type_Info_Relative_Slice, Type_Info_Matrix, + Type_Info_Soa_Pointer, }, } @@ -403,6 +407,12 @@ Raw_Cstring :: struct { data: [^]byte, } +Raw_Soa_Pointer :: struct { + data: rawptr, + index: int, +} + + /* // Defined internally by the compiler diff --git a/core/runtime/core_builtin.odin b/core/runtime/core_builtin.odin index cebb91987..b779ffade 100644 --- a/core/runtime/core_builtin.odin +++ b/core/runtime/core_builtin.odin @@ -143,7 +143,7 @@ free_all :: proc{mem_free_all} @builtin delete_string :: proc(str: string, allocator := context.allocator, loc := #caller_location) -> Allocator_Error { - return mem_free(raw_data(str), allocator, loc) + return mem_free_with_size(raw_data(str), len(str), allocator, loc) } @builtin delete_cstring :: proc(str: cstring, allocator := context.allocator, loc := #caller_location) -> Allocator_Error { @@ -151,17 +151,24 @@ delete_cstring :: proc(str: cstring, allocator := context.allocator, loc := #cal } @builtin delete_dynamic_array :: proc(array: $T/[dynamic]$E, loc := #caller_location) -> Allocator_Error { - return mem_free(raw_data(array), array.allocator, loc) + return mem_free_with_size(raw_data(array), cap(array)*size_of(E), array.allocator, loc) } @builtin delete_slice :: proc(array: $T/[]$E, allocator := context.allocator, loc := #caller_location) -> Allocator_Error { - return mem_free(raw_data(array), allocator, loc) + return mem_free_with_size(raw_data(array), len(array)*size_of(E), allocator, loc) } @builtin delete_map :: proc(m: $T/map[$K]$V, loc := #caller_location) -> Allocator_Error { + Entry :: struct { + hash: uintptr, + next: int, + key: K, + value: V, + } + raw := transmute(Raw_Map)m err := delete_slice(raw.hashes, raw.entries.allocator, loc) - err1 := mem_free(raw.entries.data, raw.entries.allocator, loc) + err1 := mem_free_with_size(raw.entries.data, raw.entries.cap*size_of(Entry), raw.entries.allocator, loc) if err == nil { err = err1 } @@ -339,19 +346,22 @@ append_elem :: proc(array: ^$T/[dynamic]$E, arg: E, loc := #caller_location) { if array == nil { return } - - if cap(array) < len(array)+1 { - cap := 2 * cap(array) + max(8, 1) - _ = reserve(array, cap, loc) - } - if cap(array)-len(array) > 0 { - a := (^Raw_Dynamic_Array)(array) - when size_of(E) != 0 { - data := ([^]E)(a.data) - assert(condition=data != nil, loc=loc) - data[a.len] = arg - } + when size_of(E) == 0 { a.len += 1 + } else { + if cap(array) < len(array)+1 { + cap := 2 * cap(array) + max(8, 1) + _ = reserve(array, cap, loc) + } + if cap(array)-len(array) > 0 { + a := (^Raw_Dynamic_Array)(array) + when size_of(E) != 0 { + data := ([^]E)(a.data) + assert(condition=data != nil, loc=loc) + data[a.len] = arg + } + a.len += 1 + } } } @@ -366,20 +376,23 @@ append_elems :: proc(array: ^$T/[dynamic]$E, args: ..E, loc := #caller_location) return } - - if cap(array) < len(array)+arg_len { - cap := 2 * cap(array) + max(8, arg_len) - _ = reserve(array, cap, loc) - } - arg_len = min(cap(array)-len(array), arg_len) - if arg_len > 0 { - a := (^Raw_Dynamic_Array)(array) - when size_of(E) != 0 { - data := ([^]E)(a.data) - assert(condition=data != nil, loc=loc) - intrinsics.mem_copy(&data[a.len], raw_data(args), size_of(E) * arg_len) - } + when size_of(E) == 0 { a.len += arg_len + } else { + if cap(array) < len(array)+arg_len { + cap := 2 * cap(array) + max(8, arg_len) + _ = reserve(array, cap, loc) + } + arg_len = min(cap(array)-len(array), arg_len) + if arg_len > 0 { + a := (^Raw_Dynamic_Array)(array) + when size_of(E) != 0 { + data := ([^]E)(a.data) + assert(condition=data != nil, loc=loc) + intrinsics.mem_copy(&data[a.len], raw_data(args), size_of(E) * arg_len) + } + a.len += arg_len + } } } @@ -413,7 +426,7 @@ append_nothing :: proc(array: ^$T/[dynamic]$E, loc := #caller_location) { @builtin -insert_at_elem :: proc(array: ^$T/[dynamic]$E, index: int, arg: E, loc := #caller_location) -> (ok: bool) #no_bounds_check { +inject_at_elem :: proc(array: ^$T/[dynamic]$E, index: int, arg: E, loc := #caller_location) -> (ok: bool) #no_bounds_check { if array == nil { return } @@ -432,7 +445,7 @@ insert_at_elem :: proc(array: ^$T/[dynamic]$E, index: int, arg: E, loc := #calle } @builtin -insert_at_elems :: proc(array: ^$T/[dynamic]$E, index: int, args: ..E, loc := #caller_location) -> (ok: bool) #no_bounds_check { +inject_at_elems :: proc(array: ^$T/[dynamic]$E, index: int, args: ..E, loc := #caller_location) -> (ok: bool) #no_bounds_check { if array == nil { return } @@ -456,7 +469,7 @@ insert_at_elems :: proc(array: ^$T/[dynamic]$E, index: int, args: ..E, loc := #c } @builtin -insert_at_elem_string :: proc(array: ^$T/[dynamic]$E/u8, index: int, arg: string, loc := #caller_location) -> (ok: bool) #no_bounds_check { +inject_at_elem_string :: proc(array: ^$T/[dynamic]$E/u8, index: int, arg: string, loc := #caller_location) -> (ok: bool) #no_bounds_check { if array == nil { return } @@ -477,7 +490,51 @@ insert_at_elem_string :: proc(array: ^$T/[dynamic]$E/u8, index: int, arg: string return } -@builtin insert_at :: proc{insert_at_elem, insert_at_elems, insert_at_elem_string} +@builtin inject_at :: proc{inject_at_elem, inject_at_elems, inject_at_elem_string} + + + +@builtin +assign_at_elem :: proc(array: ^$T/[dynamic]$E, index: int, arg: E, loc := #caller_location) -> (ok: bool) #no_bounds_check { + if index < len(array) { + array[index] = arg + ok = true + } else if resize(array, index+1, loc) { + array[index] = arg + ok = true + } + return +} + + +@builtin +assign_at_elems :: proc(array: ^$T/[dynamic]$E, index: int, args: ..E, loc := #caller_location) -> (ok: bool) #no_bounds_check { + if index+len(args) < len(array) { + copy(array[index:], args) + ok = true + } else if resize(array, index+1+len(args), loc) { + copy(array[index:], args) + ok = true + } + return +} + + +@builtin +assign_at_elem_string :: proc(array: ^$T/[dynamic]$E/u8, index: int, arg: string, loc := #caller_location) -> (ok: bool) #no_bounds_check { + if len(args) == 0 { + ok = true + } else if index+len(args) < len(array) { + copy(array[index:], args) + ok = true + } else if resize(array, index+1+len(args), loc) { + copy(array[index:], args) + ok = true + } + return +} + +@builtin assign_at :: proc{assign_at_elem, assign_at_elems, assign_at_elem_string} @@ -735,17 +792,3 @@ unimplemented :: proc(message := "", loc := #caller_location) -> ! { } p("not yet implemented", message, loc) } - -@builtin -@(disabled=ODIN_DISABLE_ASSERT) -unreachable :: proc(message := "", loc := #caller_location) -> ! { - p := context.assertion_failure_proc - if p == nil { - p = default_assertion_failure_proc - } - if message != "" { - p("internal error", message, loc) - } else { - p("internal error", "entered unreachable code", loc) - } -} diff --git a/core/runtime/dynamic_array_internal.odin b/core/runtime/dynamic_array_internal.odin index d39c2dd0b..b6a685fcf 100644 --- a/core/runtime/dynamic_array_internal.odin +++ b/core/runtime/dynamic_array_internal.odin @@ -29,11 +29,15 @@ __dynamic_array_reserve :: proc(array_: rawptr, elem_size, elem_align: int, cap: new_size := cap * elem_size allocator := array.allocator - new_data, err := allocator.procedure(allocator.data, .Resize, new_size, elem_align, array.data, old_size, loc) + new_data, err := mem_resize(array.data, old_size, new_size, elem_align, allocator, loc) if err != nil { return false } - if new_data != nil || elem_size == 0 { + if elem_size == 0 { + array.data = raw_data(new_data) + array.cap = cap + return true + } else if new_data != nil { array.data = raw_data(new_data) array.cap = min(cap, len(new_data)/elem_size) return true @@ -59,7 +63,7 @@ __dynamic_array_shrink :: proc(array_: rawptr, elem_size, elem_align: int, new_c new_size := new_cap * elem_size allocator := array.allocator - new_data, err := allocator.procedure(allocator.data, .Resize, new_size, elem_align, array.data, old_size, loc) + new_data, err := mem_resize(array.data, old_size, new_size, elem_align, allocator, loc) if err != nil { return } diff --git a/core/runtime/dynamic_map_internal.odin b/core/runtime/dynamic_map_internal.odin index fee0f570f..35b42d488 100644 --- a/core/runtime/dynamic_map_internal.odin +++ b/core/runtime/dynamic_map_internal.odin @@ -194,12 +194,15 @@ __slice_resize :: proc(array_: ^$T/[]$E, new_count: int, allocator: Allocator, l new_size := new_count*size_of(T) new_data, err := mem_resize(array.data, old_size, new_size, align_of(T), allocator, loc) - if new_data == nil || err != nil { + if err != nil { return false } - array.data = new_data - array.len = new_count - return true + if new_data != nil || size_of(E) == 0 { + array.data = raw_data(new_data) + array.len = new_count + return true + } + return false } __dynamic_map_reset_entries :: proc(using header: Map_Header, loc := #caller_location) { @@ -207,7 +210,7 @@ __dynamic_map_reset_entries :: proc(using header: Map_Header, loc := #caller_loc m.hashes[i] = -1 } - for i in 0 ..< m.entries.len { + for i in 0.. rawptr { if data == nil { return nil } - if len < 0 { + if len <= 0 { return data } intrinsics.mem_zero(data, len) @@ -111,22 +111,18 @@ mem_zero :: proc "contextless" (data: rawptr, len: int) -> rawptr { } mem_copy :: proc "contextless" (dst, src: rawptr, len: int) -> rawptr { - if src == nil { - return dst + if src != nil && dst != src && len > 0 { + // NOTE(bill): This _must_ be implemented like C's memmove + intrinsics.mem_copy(dst, src, len) } - - // NOTE(bill): This _must_ be implemented like C's memmove - intrinsics.mem_copy(dst, src, len) return dst } mem_copy_non_overlapping :: proc "contextless" (dst, src: rawptr, len: int) -> rawptr { - if src == nil { - return dst + if src != nil && dst != src && len > 0 { + // NOTE(bill): This _must_ be implemented like C's memcpy + intrinsics.mem_copy_non_overlapping(dst, src, len) } - - // NOTE(bill): This _must_ be implemented like C's memcpy - intrinsics.mem_copy_non_overlapping(dst, src, len) return dst } @@ -142,28 +138,38 @@ mem_alloc_bytes :: #force_inline proc(size: int, alignment: int = DEFAULT_ALIGNM return allocator.procedure(allocator.data, .Alloc, size, alignment, nil, 0, loc) } -mem_alloc :: #force_inline proc(size: int, alignment: int = DEFAULT_ALIGNMENT, allocator := context.allocator, loc := #caller_location) -> (rawptr, Allocator_Error) { - if size == 0 { +mem_alloc :: #force_inline proc(size: int, alignment: int = DEFAULT_ALIGNMENT, allocator := context.allocator, loc := #caller_location) -> ([]byte, Allocator_Error) { + if size == 0 || allocator.procedure == nil { return nil, nil } - if allocator.procedure == nil { - return nil, nil - } - data, err := allocator.procedure(allocator.data, .Alloc, size, alignment, nil, 0, loc) - return raw_data(data), err + return allocator.procedure(allocator.data, .Alloc, size, alignment, nil, 0, loc) } mem_free :: #force_inline proc(ptr: rawptr, allocator := context.allocator, loc := #caller_location) -> Allocator_Error { - if ptr == nil { - return .None - } - if allocator.procedure == nil { - return .None + if ptr == nil || allocator.procedure == nil { + return nil } _, err := allocator.procedure(allocator.data, .Free, 0, 0, ptr, 0, loc) return err } +mem_free_with_size :: #force_inline proc(ptr: rawptr, byte_count: int, allocator := context.allocator, loc := #caller_location) -> Allocator_Error { + if ptr == nil || allocator.procedure == nil { + return nil + } + _, err := allocator.procedure(allocator.data, .Free, 0, 0, ptr, byte_count, loc) + return err +} + +mem_free_bytes :: #force_inline proc(bytes: []byte, allocator := context.allocator, loc := #caller_location) -> Allocator_Error { + if bytes == nil || allocator.procedure == nil { + return nil + } + _, err := allocator.procedure(allocator.data, .Free, 0, 0, raw_data(bytes), len(bytes), loc) + return err +} + + mem_free_all :: #force_inline proc(allocator := context.allocator, loc := #caller_location) -> (err: Allocator_Error) { if allocator.procedure != nil { _, err = allocator.procedure(allocator.data, .Free_All, 0, 0, nil, 0, loc) @@ -171,21 +177,34 @@ mem_free_all :: #force_inline proc(allocator := context.allocator, loc := #calle return } -mem_resize :: #force_inline proc(ptr: rawptr, old_size, new_size: int, alignment: int = DEFAULT_ALIGNMENT, allocator := context.allocator, loc := #caller_location) -> (new_ptr: rawptr, err: Allocator_Error) { - new_data: []byte - switch { - case allocator.procedure == nil: - return - case new_size == 0: - new_data, err = allocator.procedure(allocator.data, .Free, 0, 0, ptr, 0, loc) - case ptr == nil: - new_data, err = allocator.procedure(allocator.data, .Alloc, new_size, alignment, nil, 0, loc) - case: - new_data, err = allocator.procedure(allocator.data, .Resize, new_size, alignment, ptr, old_size, loc) +mem_resize :: proc(ptr: rawptr, old_size, new_size: int, alignment: int = DEFAULT_ALIGNMENT, allocator := context.allocator, loc := #caller_location) -> ([]byte, Allocator_Error) { + if allocator.procedure == nil { + return nil, nil } - new_ptr = raw_data(new_data) - return + if new_size == 0 { + if ptr != nil { + _, err := allocator.procedure(allocator.data, .Free, 0, 0, ptr, old_size, loc) + return nil, err + } + return nil, nil + } else if ptr == nil { + return allocator.procedure(allocator.data, .Alloc, new_size, alignment, nil, 0, loc) + } else if old_size == new_size && uintptr(ptr) % uintptr(alignment) == 0 { + return ([^]byte)(ptr)[:old_size], nil + } + + data, err := allocator.procedure(allocator.data, .Resize, new_size, alignment, ptr, old_size, loc) + if err == .Mode_Not_Implemented { + data, err = allocator.procedure(allocator.data, .Alloc, new_size, alignment, nil, 0, loc) + if err != nil { + return data, err + } + copy(data, ([^]byte)(ptr)[:old_size]) + _, err = allocator.procedure(allocator.data, .Free, 0, 0, ptr, old_size, loc) + } + return data, err } + memory_equal :: proc "contextless" (x, y: rawptr, n: int) -> bool { switch { case n == 0: return true @@ -341,7 +360,12 @@ string_eq :: proc "contextless" (lhs, rhs: string) -> bool { string_cmp :: proc "contextless" (a, b: string) -> int { x := transmute(Raw_String)a y := transmute(Raw_String)b - return memory_compare(x.data, y.data, min(x.len, y.len)) + + ret := memory_compare(x.data, y.data, min(x.len, y.len)) + if ret == 0 && x.len != y.len { + return -1 if x.len < y.len else +1 + } + return ret } string_ne :: #force_inline proc "contextless" (a, b: string) -> bool { return !string_eq(a, b) } diff --git a/core/runtime/print.odin b/core/runtime/print.odin index 89c196fc2..959dad3a9 100644 --- a/core/runtime/print.odin +++ b/core/runtime/print.odin @@ -228,6 +228,9 @@ print_type :: proc "contextless" (ti: ^Type_Info) { case Type_Info_Multi_Pointer: print_string("[^]") print_type(info.elem) + case Type_Info_Soa_Pointer: + print_string("#soa ^") + print_type(info.elem) case Type_Info_Procedure: print_string("proc") if info.params == nil { diff --git a/core/sort/sort.odin b/core/sort/sort.odin index 2ce74294d..a2b4d7ea0 100644 --- a/core/sort/sort.odin +++ b/core/sort/sort.odin @@ -684,5 +684,10 @@ compare_f64s :: proc(a, b: f64) -> int { compare_strings :: proc(a, b: string) -> int { x := transmute(mem.Raw_String)a y := transmute(mem.Raw_String)b - return mem.compare_byte_ptrs(x.data, y.data, min(x.len, y.len)) + + ret := mem.compare_byte_ptrs(x.data, y.data, min(x.len, y.len)) + if ret == 0 && x.len != y.len { + return -1 if x.len < y.len else +1 + } + return ret } diff --git a/core/strings/builder.odin b/core/strings/builder.odin index a910b0988..6de42804d 100644 --- a/core/strings/builder.odin +++ b/core/strings/builder.odin @@ -17,50 +17,53 @@ Builder :: struct { } // return a builder, default length 0 / cap 16 are done through make -make_builder_none :: proc(allocator := context.allocator) -> Builder { +builder_make_none :: proc(allocator := context.allocator) -> Builder { return Builder{buf=make([dynamic]byte, allocator)} } // return a builder, with a set length `len` and cap 16 byte buffer -make_builder_len :: proc(len: int, allocator := context.allocator) -> Builder { +builder_make_len :: proc(len: int, allocator := context.allocator) -> Builder { return Builder{buf=make([dynamic]byte, len, allocator)} } // return a builder, with a set length `len` byte buffer and a custom `cap` -make_builder_len_cap :: proc(len, cap: int, allocator := context.allocator) -> Builder { +builder_make_len_cap :: proc(len, cap: int, allocator := context.allocator) -> Builder { return Builder{buf=make([dynamic]byte, len, cap, allocator)} } -// overload simple `make_builder_*` with or without len / cap parameters -make_builder :: proc{ - make_builder_none, - make_builder_len, - make_builder_len_cap, +// overload simple `builder_make_*` with or without len / cap parameters +builder_make :: proc{ + builder_make_none, + builder_make_len, + builder_make_len_cap, } // initialize a builder, default length 0 / cap 16 are done through make // replaces the existing `buf` -init_builder_none :: proc(b: ^Builder, allocator := context.allocator) { +builder_init_none :: proc(b: ^Builder, allocator := context.allocator) -> ^Builder { b.buf = make([dynamic]byte, allocator) + return b } // initialize a builder, with a set length `len` and cap 16 byte buffer // replaces the existing `buf` -init_builder_len :: proc(b: ^Builder, len: int, allocator := context.allocator) { +builder_init_len :: proc(b: ^Builder, len: int, allocator := context.allocator) -> ^Builder { b.buf = make([dynamic]byte, len, allocator) + return b } // initialize a builder, with a set length `len` byte buffer and a custom `cap` // replaces the existing `buf` -init_builder_len_cap :: proc(b: ^Builder, len, cap: int, allocator := context.allocator) { +builder_init_len_cap :: proc(b: ^Builder, len, cap: int, allocator := context.allocator) -> ^Builder { b.buf = make([dynamic]byte, len, cap, allocator) + return b } -// overload simple `init_builder_*` with or without len / ap parameters -init_builder :: proc{ - init_builder_none, - init_builder_len, - init_builder_len_cap, +// overload simple `builder_init_*` with or without len / ap parameters +builder_init :: proc{ + builder_init_none, + builder_init_len, + builder_init_len_cap, } @(private) @@ -103,18 +106,18 @@ to_writer :: proc(b: ^Builder) -> io.Writer { } // delete and clear the builder byte buffer content -destroy_builder :: proc(b: ^Builder) { +builder_destroy :: proc(b: ^Builder) { delete(b.buf) clear(&b.buf) } // reserve the builfer byte buffer to a specific cap, when it's higher than before -grow_builder :: proc(b: ^Builder, cap: int) { +builder_grow :: proc(b: ^Builder, cap: int) { reserve(&b.buf, cap) } // clear the builder byte buffer content -reset_builder :: proc(b: ^Builder) { +builder_reset :: proc(b: ^Builder) { clear(&b.buf) } @@ -165,7 +168,7 @@ builder_space :: proc(b: Builder) -> int { /* appends a byte to the builder, returns the append diff - builder := strings.make_builder() + builder := strings.builder_make() strings.write_byte(&builder, 'a') // 1 strings.write_byte(&builder, 'b') // 1 strings.write_byte(&builder, 'c') // 1 @@ -181,7 +184,7 @@ write_byte :: proc(b: ^Builder, x: byte) -> (n: int) { /* appends a slice of bytes to the builder, returns the append diff - builder := strings.make_builder() + builder := strings.builder_make() bytes := [?]byte { 'a', 'b', 'c' } strings.write_bytes(&builder, bytes[:]) // 3 fmt.println(strings.to_string(builder)) // -> abc @@ -196,77 +199,46 @@ write_bytes :: proc(b: ^Builder, x: []byte) -> (n: int) { /* appends a single rune into the builder, returns written rune size and an `io.Error` - builder := strings.make_builder() - strings.write_rune_builder(&builder, 'ä') // 2 None - strings.write_rune_builder(&builder, 'b') // 1 None - strings.write_rune_builder(&builder, 'c') // 1 None + builder := strings.builder_make() + strings.write_rune(&builder, 'ä') // 2 None + strings.write_rune(&builder, 'b') // 1 None + strings.write_rune(&builder, 'c') // 1 None fmt.println(strings.to_string(builder)) // -> äbc */ -write_rune_builder :: proc(b: ^Builder, r: rune) -> (int, io.Error) { +write_rune :: proc(b: ^Builder, r: rune) -> (int, io.Error) { return io.write_rune(to_writer(b), r) } /* appends a quoted rune into the builder, returns written size - builder := strings.make_builder() + builder := strings.builder_make() strings.write_string(&builder, "abc") // 3 - strings.write_quoted_rune_builder(&builder, 'ä') // 4 + strings.write_quoted_rune(&builder, 'ä') // 4 strings.write_string(&builder, "abc") // 3 fmt.println(strings.to_string(builder)) // -> abc'ä'abc */ -write_quoted_rune_builder :: proc(b: ^Builder, r: rune) -> (n: int) { - return write_quoted_rune(to_writer(b), r) +write_quoted_rune :: proc(b: ^Builder, r: rune) -> (n: int) { + return io.write_quoted_rune(to_writer(b), r) } -@(private) -_write_byte :: proc(w: io.Writer, c: byte) -> int { - err := io.write_byte(w, c) - return 1 if err == nil else 0 -} - -// writer append a quoted rune into the byte buffer, return the written size -write_quoted_rune :: proc(w: io.Writer, r: rune) -> (n: int) { - quote := byte('\'') - n += _write_byte(w, quote) - buf, width := utf8.encode_rune(r) - if width == 1 && r == utf8.RUNE_ERROR { - n += _write_byte(w, '\\') - n += _write_byte(w, 'x') - n += _write_byte(w, DIGITS_LOWER[buf[0]>>4]) - n += _write_byte(w, DIGITS_LOWER[buf[0]&0xf]) - } else { - i, _ := io.write_escaped_rune(w, r, quote) - n += i - } - n += _write_byte(w, quote) - return -} - -// overload for `write_string_*` variants -write_string :: proc{ - write_string_builder, - write_string_writer, -} /* appends a string to the builder, return the written byte size - builder := strings.make_builder() + builder := strings.builder_make() strings.write_string(&builder, "a") // 1 strings.write_string(&builder, "bc") // 2 strings.write_string(&builder, "xyz") // 3 fmt.println(strings.to_string(builder)) // -> abcxyz */ -write_string_builder :: proc(b: ^Builder, s: string) -> (n: int) { - return write_string_writer(to_writer(b), s) +write_string :: proc(b: ^Builder, s: string) -> (n: int) { + n0 := len(b.buf) + append(&b.buf, s) + n1 := len(b.buf) + return n1-n0 } -// appends a string to the writer -write_string_writer :: proc(w: io.Writer, s: string) -> (n: int) { - n, _ = io.write(w, transmute([]byte)s) - return -} // pops and returns the last byte in the builder // returns 0 when the builder is empty @@ -297,70 +269,36 @@ pop_rune :: proc(b: ^Builder) -> (r: rune, width: int) { @(private) DIGITS_LOWER := "0123456789abcdefx" -// overload for `write_quoted_string_*` variants -write_quoted_string :: proc{ - write_quoted_string_builder, - write_quoted_string_writer, -} - /* append a quoted string into the builder, return the written byte size - builder := strings.make_builder() + builder := strings.builder_make() strings.write_quoted_string(&builder, "a") // 3 strings.write_quoted_string(&builder, "bc", '\'') // 4 strings.write_quoted_string(&builder, "xyz") // 5 fmt.println(strings.to_string(builder)) // -> "a"'bc'xyz" */ -write_quoted_string_builder :: proc(b: ^Builder, str: string, quote: byte = '"') -> (n: int) { +write_quoted_string :: proc(b: ^Builder, str: string, quote: byte = '"') -> (n: int) { n, _ = io.write_quoted_string(to_writer(b), str, quote) return } -@(deprecated="prefer io.write_quoted_string") -write_quoted_string_writer :: proc(w: io.Writer, str: string, quote: byte = '"') -> (n: int) { - n, _ = io.write_quoted_string(w, str, quote) - return -} - -// overload for `write_encoded_rune_*` -write_encoded_rune :: proc{ - write_encoded_rune_builder, - write_encoded_rune_writer, -} // appends a rune to the builder, optional `write_quote` boolean tag, returns the written rune size -write_encoded_rune_builder :: proc(b: ^Builder, r: rune, write_quote := true) -> (n: int) { +write_encoded_rune :: proc(b: ^Builder, r: rune, write_quote := true) -> (n: int) { n, _ = io.write_encoded_rune(to_writer(b), r, write_quote) return } -@(deprecated="prefer io.write_encoded_rune") -write_encoded_rune_writer :: proc(w: io.Writer, r: rune, write_quote := true) -> (n: int) { - n, _ = io.write_encoded_rune(w, r, write_quote) - return -} - -// overload for `write_escaped_rune_*` -write_escaped_rune :: proc{ - write_escaped_rune_builder, - write_escaped_rune_writer, -} // appends a rune to the builder, fully written out in case of escaped runes e.g. '\a' will be written as such // when `r` and `quote` match and `quote` is `\\` - they will be written as two slashes // `html_safe` flag in case the runes '<', '>', '&' should be encoded as digits e.g. `\u0026` -write_escaped_rune_builder :: proc(b: ^Builder, r: rune, quote: byte, html_safe := false) -> (n: int) { +write_escaped_rune :: proc(b: ^Builder, r: rune, quote: byte, html_safe := false) -> (n: int) { n, _ = io.write_escaped_rune(to_writer(b), r, quote, html_safe) return } -@(deprecated="prefer io.write_escaped_rune") -write_escaped_rune_writer :: proc(w: io.Writer, r: rune, quote: byte, html_safe := false) -> (n: int) { - n, _ = io.write_escaped_rune(w, r, quote, html_safe) - return -} - // writes a u64 value `i` in `base` = 10 into the builder, returns the written amount of characters write_u64 :: proc(b: ^Builder, i: u64, base: int = 10) -> (n: int) { buf: [32]byte diff --git a/core/strings/conversion.odin b/core/strings/conversion.odin index 5e7110281..ab827490d 100644 --- a/core/strings/conversion.odin +++ b/core/strings/conversion.odin @@ -10,7 +10,7 @@ to_valid_utf8 :: proc(s, replacement: string, allocator := context.allocator) -> } b: Builder - init_builder(&b, 0, 0, allocator) + builder_init(&b, 0, 0, allocator) s := s for c, i in s { @@ -20,7 +20,7 @@ to_valid_utf8 :: proc(s, replacement: string, allocator := context.allocator) -> _, w := utf8.decode_rune_in_string(s[i:]) if w == 1 { - grow_builder(&b, len(s) + len(replacement)) + builder_grow(&b, len(s) + len(replacement)) write_string(&b, s[:i]) s = s[i:] break @@ -67,9 +67,9 @@ to_valid_utf8 :: proc(s, replacement: string, allocator := context.allocator) -> */ to_lower :: proc(s: string, allocator := context.allocator) -> string { b: Builder - init_builder(&b, 0, len(s), allocator) + builder_init(&b, 0, len(s), allocator) for r in s { - write_rune_builder(&b, unicode.to_lower(r)) + write_rune(&b, unicode.to_lower(r)) } return to_string(b) } @@ -83,9 +83,9 @@ to_lower :: proc(s: string, allocator := context.allocator) -> string { */ to_upper :: proc(s: string, allocator := context.allocator) -> string { b: Builder - init_builder(&b, 0, len(s), allocator) + builder_init(&b, 0, len(s), allocator) for r in s { - write_rune_builder(&b, unicode.to_upper(r)) + write_rune(&b, unicode.to_upper(r)) } return to_string(b) } @@ -147,7 +147,7 @@ to_camel_case :: proc(s: string, allocator := context.allocator) -> string { s := s s = trim_space(s) b: Builder - init_builder(&b, 0, len(s), allocator) + builder_init(&b, 0, len(s), allocator) w := to_writer(&b) string_case_iterator(w, s, proc(w: io.Writer, prev, curr, next: rune) { @@ -172,7 +172,7 @@ to_pascal_case :: proc(s: string, allocator := context.allocator) -> string { s := s s = trim_space(s) b: Builder - init_builder(&b, 0, len(s), allocator) + builder_init(&b, 0, len(s), allocator) w := to_writer(&b) string_case_iterator(w, s, proc(w: io.Writer, prev, curr, next: rune) { @@ -203,7 +203,7 @@ to_delimiter_case :: proc(s: string, delimiter: rune, all_upper_case: bool, allo s := s s = trim_space(s) b: Builder - init_builder(&b, 0, len(s), allocator) + builder_init(&b, 0, len(s), allocator) w := to_writer(&b) adjust_case := unicode.to_upper if all_upper_case else unicode.to_lower @@ -272,7 +272,7 @@ to_ada_case :: proc(s: string, allocator := context.allocator) -> string { s := s s = trim_space(s) b: Builder - init_builder(&b, 0, len(s), allocator) + builder_init(&b, 0, len(s), allocator) w := to_writer(&b) prev, curr: rune diff --git a/core/strings/intern.odin b/core/strings/intern.odin index 1e9577e61..5e9193a0d 100644 --- a/core/strings/intern.odin +++ b/core/strings/intern.odin @@ -31,31 +31,31 @@ intern_destroy :: proc(m: ^Intern) { // returns the `text` string from the intern map - gets set if it didnt exist yet // the returned string lives as long as the map entry lives -intern_get :: proc(m: ^Intern, text: string) -> string { - entry := _intern_get_entry(m, text) - #no_bounds_check return string(entry.str[:entry.len]) +intern_get :: proc(m: ^Intern, text: string) -> (str: string, err: runtime.Allocator_Error) { + entry := _intern_get_entry(m, text) or_return + #no_bounds_check return string(entry.str[:entry.len]), nil } // returns the `text` cstring from the intern map - gets set if it didnt exist yet // the returned cstring lives as long as the map entry lives -intern_get_cstring :: proc(m: ^Intern, text: string) -> cstring { - entry := _intern_get_entry(m, text) - return cstring(&entry.str[0]) +intern_get_cstring :: proc(m: ^Intern, text: string) -> (str: cstring, err: runtime.Allocator_Error) { + entry := _intern_get_entry(m, text) or_return + return cstring(&entry.str[0]), nil } // looks up wether the `text` string exists in the map, returns the entry // sets & allocates the entry if it wasnt set yet -_intern_get_entry :: proc(m: ^Intern, text: string) -> ^Intern_Entry #no_bounds_check { +_intern_get_entry :: proc(m: ^Intern, text: string) -> (new_entry: ^Intern_Entry, err: runtime.Allocator_Error) #no_bounds_check { if prev, ok := m.entries[text]; ok { - return prev + return prev, nil } if m.allocator.procedure == nil { m.allocator = context.allocator } entry_size := int(offset_of(Intern_Entry, str)) + len(text) + 1 - ptr, _ := runtime.mem_alloc(entry_size, align_of(Intern_Entry), m.allocator) - new_entry := (^Intern_Entry)(ptr) + bytes := runtime.mem_alloc(entry_size, align_of(Intern_Entry), m.allocator) or_return + new_entry = (^Intern_Entry)(raw_data(bytes)) new_entry.len = len(text) copy(new_entry.str[:new_entry.len], text) @@ -63,5 +63,5 @@ _intern_get_entry :: proc(m: ^Intern, text: string) -> ^Intern_Entry #no_bounds_ key := string(new_entry.str[:new_entry.len]) m.entries[key] = new_entry - return new_entry + return new_entry, nil } diff --git a/core/strings/strings.odin b/core/strings/strings.odin index 6bdafbba4..603917698 100644 --- a/core/strings/strings.odin +++ b/core/strings/strings.odin @@ -1553,7 +1553,7 @@ split_multi_iterate :: proc(using sm: ^Split_Multi) -> (res: string, ok: bool) # scrub :: proc(s: string, replacement: string, allocator := context.allocator) -> string { str := s b: Builder - init_builder(&b, 0, len(s), allocator) + builder_init(&b, 0, len(s), allocator) has_error := false cursor := 0 @@ -1622,7 +1622,7 @@ expand_tabs :: proc(s: string, tab_size: int, allocator := context.allocator) -> } b: Builder - init_builder(&b, allocator) + builder_init(&b, allocator) writer := to_writer(&b) str := s column: int @@ -1690,8 +1690,8 @@ centre_justify :: proc(str: string, length: int, pad: string, allocator := conte pad_len := rune_count(pad) b: Builder - init_builder(&b, allocator) - grow_builder(&b, len(str) + (remains/pad_len + 1)*len(pad)) + builder_init(&b, allocator) + builder_grow(&b, len(str) + (remains/pad_len + 1)*len(pad)) w := to_writer(&b) @@ -1713,8 +1713,8 @@ left_justify :: proc(str: string, length: int, pad: string, allocator := context pad_len := rune_count(pad) b: Builder - init_builder(&b, allocator) - grow_builder(&b, len(str) + (remains/pad_len + 1)*len(pad)) + builder_init(&b, allocator) + builder_grow(&b, len(str) + (remains/pad_len + 1)*len(pad)) w := to_writer(&b) @@ -1735,8 +1735,8 @@ right_justify :: proc(str: string, length: int, pad: string, allocator := contex pad_len := rune_count(pad) b: Builder - init_builder(&b, allocator) - grow_builder(&b, len(str) + (remains/pad_len + 1)*len(pad)) + builder_init(&b, allocator) + builder_grow(&b, len(str) + (remains/pad_len + 1)*len(pad)) w := to_writer(&b) diff --git a/core/sync/futex_freebsd.odin b/core/sync/futex_freebsd.odin index 2e1d065bc..07fba82a8 100644 --- a/core/sync/futex_freebsd.odin +++ b/core/sync/futex_freebsd.odin @@ -3,24 +3,22 @@ package sync import "core:c" -import "core:os" import "core:time" UMTX_OP_WAIT :: 2 UMTX_OP_WAKE :: 3 +ETIMEDOUT :: 60 + foreign import libc "system:c" foreign libc { _umtx_op :: proc "c" (obj: rawptr, op: c.int, val: c.ulong, uaddr: rawptr, uaddr2: rawptr) -> c.int --- + __error :: proc "c" () -> ^c.int --- } _futex_wait :: proc(f: ^Futex, expected: u32) -> bool { - timeout := os.Unix_File_Time{ - seconds = 5, - nanoseconds = 0, - } - + timeout := [2]i64{14400, 0} // 4 hours for { res := _umtx_op(f, UMTX_OP_WAIT, c.ulong(expected), nil, &timeout) @@ -28,7 +26,7 @@ _futex_wait :: proc(f: ^Futex, expected: u32) -> bool { return true } - if os.Errno(os.get_last_error()) == os.ETIMEDOUT { + if __error()^ == ETIMEDOUT { continue } @@ -42,16 +40,14 @@ _futex_wait_with_timeout :: proc(f: ^Futex, expected: u32, duration: time.Durati return false } - res := _umtx_op(f, UMTX_OP_WAIT, c.ulong(expected), nil, &os.Unix_File_Time{ - seconds = (os.time_t)(duration/1e9), - nanoseconds = (c.long)(duration%1e9), - }) + timeout := [2]i64{i64(duration/1e9), i64(duration%1e9)} + res := _umtx_op(f, UMTX_OP_WAIT, c.ulong(expected), nil, &timeout) if res != -1 { return true } - if os.Errno(os.get_last_error()) == os.ETIMEDOUT { + if __error()^ == ETIMEDOUT { return false } diff --git a/core/sync/primitives_freebsd.odin b/core/sync/primitives_freebsd.odin index e6219acf1..2d7cbf18d 100644 --- a/core/sync/primitives_freebsd.odin +++ b/core/sync/primitives_freebsd.odin @@ -2,8 +2,14 @@ //+private package sync -import "core:os" +import "core:c" + +foreign import dl "system:dl" + +foreign dl { + pthread_getthreadid_np :: proc "c" () -> c.int --- +} _current_thread_id :: proc "contextless" () -> int { - return os.current_thread_id() + return int(pthread_getthreadid_np()) } diff --git a/core/sys/wasm/wasi/wasi_api.odin b/core/sys/wasm/wasi/wasi_api.odin index 2e2a99617..d4f0d19cf 100644 --- a/core/sys/wasm/wasi/wasi_api.odin +++ b/core/sys/wasm/wasi/wasi_api.odin @@ -1148,6 +1148,156 @@ foreign wasi { */ how: sdflags_t, ) -> errno_t --- + + + /** + * Return a description of the given preopened file descriptor. + */ + fd_prestat_dir_name :: proc( + fd: fd_t, + /** + * A buffer into which to write the preopened directory name. + */ + path: string, + ) -> errno_t --- + /** + * Create a directory. + * Note: This is similar to `mkdirat` in POSIX. + */ + path_create_directory :: proc( + fd: fd_t, + /** + * The path at which to create the directory. + */ + path: string, + ) -> errno_t --- + /** + * Adjust the timestamps of a file or directory. + * Note: This is similar to `utimensat` in POSIX. + */ + path_filestat_set_times :: proc( + fd: fd_t, + /** + * Flags determining the method of how the path is resolved. + */ + flags: lookupflags_t, + /** + * The path of the file or directory to operate on. + */ + path: string, + /** + * The desired values of the data access timestamp. + */ + atim: timestamp_t, + /** + * The desired values of the data modification timestamp. + */ + mtim: timestamp_t, + /** + * A bitmask indicating which timestamps to adjust. + */ + fst_flags: fstflags_t, + ) -> errno_t --- + /** + * Remove a directory. + * Return `errno::notempty` if the directory is not empty. + * Note: This is similar to `unlinkat(fd, path, AT_REMOVEDIR)` in POSIX. + */ + path_remove_directory :: proc( + fd: fd_t, + /** + * The path to a directory to remove. + */ + path: string, + ) -> errno_t --- + /** + * Create a hard link. + * Note: This is similar to `linkat` in POSIX. + */ + path_link :: proc( + old_fd: fd_t, + /** + * Flags determining the method of how the path is resolved. + */ + old_flags: lookupflags_t, + /** + * The source path from which to link. + */ + old_path: string, + /** + * The working directory at which the resolution of the new path starts. + */ + new_fd: fd_t, + /** + * The destination path at which to create the hard link. + */ + new_path: string, + ) -> errno_t --- + + /** + * Rename a file or directory. + * Note: This is similar to `renameat` in POSIX. + */ + path_rename :: proc( + fd: fd_t, + /** + * The source path of the file or directory to rename. + */ + old_path: string, + /** + * The working directory at which the resolution of the new path starts. + */ + new_fd: fd_t, + /** + * The destination path to which to rename the file or directory. + */ + new_path: string, + ) -> errno_t --- + + /** + * Create a symbolic link. + * Note: This is similar to `symlinkat` in POSIX. + */ + path_symlink :: proc( + /** + * The contents of the symbolic link. + */ + old_path: string, + fd: fd_t, + /** + * The destination path at which to create the symbolic link. + */ + new_path: string, + ) -> errno_t --- + + /** + * Unlink a file. + * Return `errno::isdir` if the path refers to a directory. + * Note: This is similar to `unlinkat(fd, path, 0)` in POSIX. + */ + path_unlink_file :: proc( + fd: fd_t, + /** + * The path to a file to unlink. + */ + path: string, + ) -> errno_t --- + + /** + * Write high-quality random data into a buffer. + * This function blocks when the implementation is unable to immediately + * provide sufficient high-quality random data. + * This function may execute slowly, so when large mounts of random data are + * required, it's advisable to use this function to seed a pseudo-random + * number generator, rather than to provide the random data directly. + */ + random_get :: proc( + /** + * The buffer to fill with random data. + */ + buf: []u8, + ) -> errno_t --- + } /** @@ -1250,7 +1400,7 @@ fd_pread :: proc "c" ( */ offset: filesize_t, ) -> (n: size_t, err: errno_t) { - err = wasi_fd_pread(fd, raw_data(iovs), len(iovs), offset, &n) + err = wasi_fd_pread(fd, iovs, offset, &n) return } /** @@ -1281,7 +1431,7 @@ fd_pwrite :: proc "c" ( */ offset: filesize_t, ) -> (n: size_t, err: errno_t) { - err = wasi_fd_pwrite(fd, raw_data(iovs), len(iovs), offset, &n) + err = wasi_fd_pwrite(fd, iovs, offset, &n) return } /** @@ -1297,7 +1447,7 @@ fd_read :: proc "c" ( */ iovs: []iovec_t, ) -> (n: size_t, err: errno_t) { - err = wasi_fd_read(fd, raw_data(iovs), len(iovs), &n) + err = wasi_fd_read(fd, iovs, &n) return } /** @@ -1324,7 +1474,7 @@ fd_readdir :: proc "c" ( */ cookie: dircookie_t, ) -> (n: size_t, err: errno_t) { - err = wasi_fd_readdir(fd, raw_data(buf), len(buf), cookie, &n) + err = wasi_fd_readdir(fd, buf, cookie, &n) return } /** @@ -1370,7 +1520,7 @@ fd_write :: proc "c" ( */ iovs: []ciovec_t, ) -> (n: size_t, err: errno_t) { - err = wasi_fd_write(fd, raw_data(iovs), len(iovs), &n) + err = wasi_fd_write(fd, iovs, &n) return } /** @@ -1390,7 +1540,7 @@ path_filestat_get :: proc "c" ( */ path: string, ) -> (offset: filestat_t, err: errno_t) { - err = wasi_path_filestat_get(fd, flags, raw_data(path), len(path), &offset) + err = wasi_path_filestat_get(fd, flags, path, &offset) return } /** @@ -1432,7 +1582,7 @@ path_open :: proc "c" ( fs_rights_inheriting: rights_t, fdflags: fdflags_t, ) -> (file: fd_t, err: errno_t) { - err = wasi_path_open(fd, dirflags, raw_data(path), len(path), oflags, fs_rights_base, fs_rights_inheriting, fdflags, &file) + err = wasi_path_open(fd, dirflags, path, oflags, fs_rights_base, fs_rights_inheriting, fdflags, &file) return } /** @@ -1452,7 +1602,7 @@ path_readlink :: proc "c" ( */ buf: []u8, ) -> (n: size_t, err: errno_t) { - err = wasi_path_readlink(fd, raw_data(path), len(path), raw_data(buf), len(buf), &n) + err = wasi_path_readlink(fd, path, buf, &n) return } /** @@ -1495,7 +1645,7 @@ sock_recv :: proc "c" ( */ ri_flags: riflags_t, ) -> (n: size_t, flags: roflags_t, err: errno_t) { - err = wasi_sock_recv(fd, raw_data(ri_data), len(ri_data), ri_flags, &n, &flags) + err = wasi_sock_recv(fd, ri_data, ri_flags, &n, &flags) return } /** @@ -1516,172 +1666,11 @@ sock_send :: proc "c" ( */ si_flags: siflags_t, ) -> (n: size_t, err: errno_t) { - err = wasi_sock_send(fd, raw_data(si_data), len(si_data), si_flags, &n) + err = wasi_sock_send(fd, si_data, si_flags, &n) return } -/** - * Return a description of the given preopened file descriptor. - */ -fd_prestat_dir_name :: proc( - fd: fd_t, - /** - * A buffer into which to write the preopened directory name. - */ - path: string, -) -> errno_t { - return wasm_fd_prestat_dir_name(fd, raw_data(path), len(path)) -} -/** - * Create a directory. - * Note: This is similar to `mkdirat` in POSIX. - */ -path_create_directory :: proc( - fd: fd_t, - /** - * The path at which to create the directory. - */ - path: string, -) -> errno_t { - return wasm_path_create_directory(fd, raw_data(path), len(path)) -} -/** - * Adjust the timestamps of a file or directory. - * Note: This is similar to `utimensat` in POSIX. - */ -path_filestat_set_times :: proc( - fd: fd_t, - /** - * Flags determining the method of how the path is resolved. - */ - flags: lookupflags_t, - /** - * The path of the file or directory to operate on. - */ - path: string, - /** - * The desired values of the data access timestamp. - */ - atim: timestamp_t, - /** - * The desired values of the data modification timestamp. - */ - mtim: timestamp_t, - /** - * A bitmask indicating which timestamps to adjust. - */ - fst_flags: fstflags_t, -) -> errno_t { - return wasm_path_filestat_set_times(fd, flags, raw_data(path), len(path), atim, mtim, fst_flags) -} -/** - * Remove a directory. - * Return `errno::notempty` if the directory is not empty. - * Note: This is similar to `unlinkat(fd, path, AT_REMOVEDIR)` in POSIX. - */ -path_remove_directory :: proc( - fd: fd_t, - /** - * The path to a directory to remove. - */ - path: string, -) -> errno_t { - return wasm_path_remove_directory(fd, raw_data(path), len(path)) -} -/** - * Create a hard link. - * Note: This is similar to `linkat` in POSIX. - */ -path_link :: proc( - old_fd: fd_t, - /** - * Flags determining the method of how the path is resolved. - */ - old_flags: lookupflags_t, - /** - * The source path from which to link. - */ - old_path: string, - /** - * The working directory at which the resolution of the new path starts. - */ - new_fd: fd_t, - /** - * The destination path at which to create the hard link. - */ - new_path: string, -) -> errno_t { - return wasm_path_link(old_fd, old_flags, raw_data(old_path), len(old_path), new_fd, raw_data(new_path), len(new_path)) -} -/** - * Rename a file or directory. - * Note: This is similar to `renameat` in POSIX. - */ -path_rename :: proc( - fd: fd_t, - /** - * The source path of the file or directory to rename. - */ - old_path: string, - /** - * The working directory at which the resolution of the new path starts. - */ - new_fd: fd_t, - /** - * The destination path to which to rename the file or directory. - */ - new_path: string, -) -> errno_t { - return wasm_path_rename(fd, raw_data(old_path), len(old_path), new_fd, raw_data(new_path), len(new_path)) -} -/** - * Create a symbolic link. - * Note: This is similar to `symlinkat` in POSIX. - */ -path_symlink :: proc( - /** - * The contents of the symbolic link. - */ - old_path: string, - fd: fd_t, - /** - * The destination path at which to create the symbolic link. - */ - new_path: string, -) -> errno_t { - return wasm_path_symlink(raw_data(old_path), len(old_path), fd, raw_data(new_path), len(new_path)) -} -/** - * Unlink a file. - * Return `errno::isdir` if the path refers to a directory. - * Note: This is similar to `unlinkat(fd, path, 0)` in POSIX. - */ -path_unlink_file :: proc( - fd: fd_t, - /** - * The path to a file to unlink. - */ - path: string, -) -> errno_t { - return wasm_path_unlink_file(fd, raw_data(path), len(path)) -} -/** - * Write high-quality random data into a buffer. - * This function blocks when the implementation is unable to immediately - * provide sufficient high-quality random data. - * This function may execute slowly, so when large mounts of random data are - * required, it's advisable to use this function to seed a pseudo-random - * number generator, rather than to provide the random data directly. - */ -random_get :: proc( - /** - * The buffer to fill with random data. - */ - buf: []u8, -) -> errno_t { - return wasm_random_get(raw_data(buf), len(buf)) -} @@ -1722,8 +1711,7 @@ foreign wasi { @(link_name="fd_pread") wasi_fd_pread :: proc( fd: fd_t, - iovs: [^]iovec_t, - iovs_len: size_t, + iovs: []iovec_t, offset: filesize_t, retptr0: ^size_t, ) -> errno_t --- @@ -1735,23 +1723,20 @@ foreign wasi { @(link_name="fd_pwrite") wasi_fd_pwrite :: proc( fd: fd_t, - iovs: [^]ciovec_t, - iovs_len: size_t, + iovs: []ciovec_t, offset: filesize_t, retptr0: ^size_t, ) -> errno_t --- @(link_name="fd_read") wasi_fd_read :: proc( fd: fd_t, - iovs: [^]iovec_t, - iovs_len: size_t, + iovs: []iovec_t, retptr0: ^size_t, ) -> errno_t --- @(link_name="fd_readdir") wasi_fd_readdir :: proc( fd: fd_t, - buf: [^]u8, - buf_len: size_t, + buf: []u8, cookie: dircookie_t, retptr0: ^size_t, ) -> errno_t --- @@ -1770,8 +1755,7 @@ foreign wasi { @(link_name="fd_write") wasi_fd_write :: proc( fd: fd_t, - iovs: [^]ciovec_t, - iovs_len: size_t, + iovs: []ciovec_t, retptr0: ^size_t, ) -> errno_t --- @(link_name="path_filestat_get") @@ -1781,16 +1765,14 @@ foreign wasi { /** * The path of the file or directory to inspect. */ - path: [^]u8, - path_len: size_t, + path: string, retptr0: ^filestat_t, ) -> errno_t --- @(link_name="path_open") wasi_path_open :: proc( fd: fd_t, dirflags: lookupflags_t, - path: [^]u8, - path_len: size_t, + path: string, oflags: oflags_t, fs_rights_base: rights_t, fs_rights_inheriting: rights_t, @@ -1800,10 +1782,8 @@ foreign wasi { @(link_name="path_readlink") wasi_path_readlink :: proc( fd: fd_t, - path: [^]u8, - path_len: size_t, - buf: [^]u8, - buf_len: size_t, + path: string, + buf: []u8, retptr0: ^size_t, ) -> errno_t --- @(link_name="poll_oneoff") @@ -1816,8 +1796,7 @@ foreign wasi { @(link_name="sock_recv") wasi_sock_recv :: proc( fd: fd_t, - ri_data: [^]iovec_t, - ri_data_len: size_t, + ri_data: []iovec_t, ri_flags: riflags_t, retptr0: ^size_t, retptr1: ^roflags_t, @@ -1825,75 +1804,8 @@ foreign wasi { @(link_name="sock_send") wasi_sock_send :: proc( fd: fd_t, - si_data: [^]ciovec_t, - si_data_len: size_t, + si_data: []ciovec_t, si_flags: siflags_t, retptr0: ^size_t, ) -> errno_t --- - @(link_name="fd_prestat_dir_name") - wasm_fd_prestat_dir_name :: proc( - fd: fd_t, - path: [^]u8, - path_len: size_t, - ) -> errno_t --- - @(link_name="path_create_directory") - wasm_path_create_directory :: proc( - fd: fd_t, - path: [^]u8, - path_len: size_t, - ) -> errno_t --- - @(link_name="path_filestat_set_times") - wasm_path_filestat_set_times :: proc( - fd: fd_t, - flags: lookupflags_t, - path: [^]u8, - path_len: size_t, - atim: timestamp_t, - mtim: timestamp_t, - fst_flags: fstflags_t, - ) -> errno_t --- - @(link_name="path_remove_directory") - wasm_path_remove_directory :: proc( - fd: fd_t, - path: [^]u8, - path_len: size_t, - ) -> errno_t --- - @(link_name="path_link") - wasm_path_link :: proc( - old_fd: fd_t, - old_flags: lookupflags_t, - old_path: [^]u8, - old_path_len: size_t, - new_fd: fd_t, - new_path: [^]u8, - new_path_len: size_t, - ) -> errno_t --- - @(link_name="path_rename") - wasm_path_rename :: proc( - fd: fd_t, - old_path: [^]u8, - old_path_len: size_t, - new_fd: fd_t, - new_path: [^]u8, - new_path_len: size_t, - ) -> errno_t --- - @(link_name="path_symlink") - wasm_path_symlink :: proc( - old_path: [^]u8, - old_path_len: size_t, - fd: fd_t, - new_path: [^]u8, - new_path_len: size_t, - ) -> errno_t --- - @(link_name="path_unlink_file") - wasm_path_unlink_file :: proc( - fd: fd_t, - path: [^]u8, - path_len: size_t, - ) -> errno_t --- - @(link_name="random_get") - wasm_random_get :: proc( - buf: [^]u8, - buf_len: size_t, - ) -> errno_t --- } diff --git a/core/sys/windows/user32.odin b/core/sys/windows/user32.odin index 47de354b6..d0a0d5b0a 100644 --- a/core/sys/windows/user32.odin +++ b/core/sys/windows/user32.odin @@ -18,6 +18,7 @@ foreign user32 { RegisterClassW :: proc(lpWndClass: ^WNDCLASSW) -> ATOM --- RegisterClassExW :: proc(^WNDCLASSEXW) -> ATOM --- + UnregisterClassW :: proc(lpClassName: LPCWSTR, hInstance: HINSTANCE) -> BOOL --- CreateWindowExW :: proc( dwExStyle: DWORD, diff --git a/core/sys/windows/util.odin b/core/sys/windows/util.odin index c350032f0..2bdd72ed2 100644 --- a/core/sys/windows/util.odin +++ b/core/sys/windows/util.odin @@ -22,6 +22,10 @@ GET_Y_LPARAM :: #force_inline proc "contextless" (lp: LPARAM) -> c_int { return cast(c_int)cast(c_short)HIWORD(cast(DWORD)lp) } +MAKE_WORD :: #force_inline proc "contextless" (x, y: WORD) -> WORD { + return x << 8 | y +} + utf8_to_utf16 :: proc(s: string, allocator := context.temp_allocator) -> []u16 { if len(s) < 1 { return nil diff --git a/core/sys/windows/winerror.odin b/core/sys/windows/winerror.odin index b7652d1f4..7bd0bfe9f 100644 --- a/core/sys/windows/winerror.odin +++ b/core/sys/windows/winerror.odin @@ -45,4 +45,4 @@ ERROR_NOT_SAME_OBJECT : DWORD : 1656 E_NOTIMPL :: HRESULT(-0x7fff_bfff) // 0x8000_4001 -SUCCEEDED :: #force_inline proc(#any_int result: int) -> bool do return result >= 0 +SUCCEEDED :: #force_inline proc(#any_int result: int) -> bool { return result >= 0 } diff --git a/core/text/i18n/gettext.odin b/core/text/i18n/gettext.odin index d99ec1c9b..d5537a19c 100644 --- a/core/text/i18n/gettext.odin +++ b/core/text/i18n/gettext.odin @@ -99,14 +99,14 @@ parse_mo_from_bytes :: proc(data: []byte, options := DEFAULT_PARSE_OPTIONS, plur } for k in keys { - interned_key := strings.intern_get(&translation.intern, string(k)) + interned_key, _ := strings.intern_get(&translation.intern, string(k)) interned_vals := make([]string, len(keys)) last_val: string i := 0 for v in vals { - interned_vals[i] = strings.intern_get(&translation.intern, string(v)) + interned_vals[i], _ = strings.intern_get(&translation.intern, string(v)) last_val = interned_vals[i] i += 1 } diff --git a/core/text/i18n/qt_linguist.odin b/core/text/i18n/qt_linguist.odin index 036a89eeb..e7c1f9974 100644 --- a/core/text/i18n/qt_linguist.odin +++ b/core/text/i18n/qt_linguist.odin @@ -59,9 +59,9 @@ parse_qt_linguist_from_bytes :: proc(data: []byte, options := DEFAULT_PARSE_OPTI return translation, .TS_File_Expected_Context_Name, } - section_name := strings.intern_get(&translation.intern, "") + section_name, _ := strings.intern_get(&translation.intern, "") if !options.merge_sections { - section_name = strings.intern_get(&translation.intern, ts.elements[section_name_id].value) + section_name, _ = strings.intern_get(&translation.intern, ts.elements[section_name_id].value) } if section_name not_in translation.k_v { @@ -92,8 +92,8 @@ parse_qt_linguist_from_bytes :: proc(data: []byte, options := DEFAULT_PARSE_OPTI return translation, .TS_File_Expected_Translation } - source := strings.intern_get(&translation.intern, ts.elements[source_id].value) - xlat := strings.intern_get(&translation.intern, ts.elements[translation_id].value) + source, _ := strings.intern_get(&translation.intern, ts.elements[source_id].value) + xlat, _ := strings.intern_get(&translation.intern, ts.elements[translation_id].value) if source in section { return translation, .Duplicate_Key @@ -124,7 +124,7 @@ parse_qt_linguist_from_bytes :: proc(data: []byte, options := DEFAULT_PARSE_OPTI if !numerus_found { break } - numerus := strings.intern_get(&translation.intern, ts.elements[numerus_id].value) + numerus, _ := strings.intern_get(&translation.intern, ts.elements[numerus_id].value) section[source][num_plurals] = numerus num_plurals += 1 diff --git a/core/unicode/tools/generate_entity_table.odin b/core/unicode/tools/generate_entity_table.odin index 075ec1cca..328ba9091 100644 --- a/core/unicode/tools/generate_entity_table.odin +++ b/core/unicode/tools/generate_entity_table.odin @@ -45,7 +45,7 @@ generate_encoding_entity_table :: proc() { printf("\"%v\" loaded and parsed.\n", filename) generated_buf: strings.Builder - defer strings.destroy_builder(&generated_buf) + defer strings.builder_destroy(&generated_buf) w := strings.to_writer(&generated_buf) charlist, charlist_ok := xml.find_child_by_ident(doc.root, "charlist") diff --git a/src/build_settings.cpp b/src/build_settings.cpp index a82cc80c9..d49f1cecf 100644 --- a/src/build_settings.cpp +++ b/src/build_settings.cpp @@ -204,7 +204,7 @@ enum BuildPath : u8 { BuildPath_Main_Package, // Input Path to the package directory (or file) we're building. BuildPath_RC, // Input Path for .rc file, can be set with `-resource:`. BuildPath_RES, // Output Path for .res file, generated from previous. - BuildPath_Win_SDK_Root, // windows_sdk_root + BuildPath_Win_SDK_Bin_Path, // windows_sdk_bin_path BuildPath_Win_SDK_UM_Lib, // windows_sdk_um_library_path BuildPath_Win_SDK_UCRT_Lib, // windows_sdk_ucrt_library_path BuildPath_VS_EXE, // vs_exe_path @@ -1322,6 +1322,7 @@ bool init_build_paths(String init_filename) { } #if defined(GB_SYSTEM_WINDOWS) + if (bc->metrics.os == TargetOs_windows) { if (bc->resource_filepath.len > 0) { bc->build_paths[BuildPath_RC] = path_from_string(ha, bc->resource_filepath); bc->build_paths[BuildPath_RES] = path_from_string(ha, bc->resource_filepath); @@ -1335,7 +1336,7 @@ bool init_build_paths(String init_filename) { if ((bc->command_kind & Command__does_build) && (!bc->ignore_microsoft_magic)) { // NOTE(ic): It would be nice to extend this so that we could specify the Visual Studio version that we want instead of defaulting to the latest. - Find_Result_Utf8 find_result = find_visual_studio_and_windows_sdk_utf8(); + Find_Result find_result = find_visual_studio_and_windows_sdk(); defer (mc_free_all()); if (find_result.windows_sdk_version == 0) { @@ -1356,8 +1357,8 @@ bool init_build_paths(String init_filename) { if (find_result.windows_sdk_um_library_path.len > 0) { GB_ASSERT(find_result.windows_sdk_ucrt_library_path.len > 0); - if (find_result.windows_sdk_root.len > 0) { - bc->build_paths[BuildPath_Win_SDK_Root] = path_from_string(ha, find_result.windows_sdk_root); + if (find_result.windows_sdk_bin_path.len > 0) { + bc->build_paths[BuildPath_Win_SDK_Bin_Path] = path_from_string(ha, find_result.windows_sdk_bin_path); } if (find_result.windows_sdk_um_library_path.len > 0) { @@ -1377,6 +1378,7 @@ bool init_build_paths(String init_filename) { } } } + } #endif // All the build targets and OSes. diff --git a/src/check_builtin.cpp b/src/check_builtin.cpp index 8108604ba..8f8f7f9e2 100644 --- a/src/check_builtin.cpp +++ b/src/check_builtin.cpp @@ -3569,6 +3569,7 @@ bool check_builtin_procedure(CheckerContext *c, Operand *operand, Ast *call, i32 operand->mode = Addressing_NoValue; break; + case BuiltinProc_unreachable: case BuiltinProc_trap: case BuiltinProc_debug_trap: operand->mode = Addressing_NoValue; diff --git a/src/check_decl.cpp b/src/check_decl.cpp index 86280b6cb..9d043e60a 100644 --- a/src/check_decl.cpp +++ b/src/check_decl.cpp @@ -320,7 +320,7 @@ void check_type_decl(CheckerContext *ctx, Entity *e, Ast *init_expr, Type *def) } else if (is_type_any(e->type)) { error(init_expr, "'distinct' cannot be applied to 'any'"); is_distinct = false; - } else if (is_type_simd_vector(e->type)) { + } else if (is_type_simd_vector(e->type) || is_type_soa_pointer(e->type)) { gbString str = type_to_string(e->type); error(init_expr, "'distinct' cannot be applied to '%s'", str); gb_string_free(str); diff --git a/src/check_expr.cpp b/src/check_expr.cpp index b42301cd6..f6c94466b 100644 --- a/src/check_expr.cpp +++ b/src/check_expr.cpp @@ -119,6 +119,7 @@ void check_or_else_split_types(CheckerContext *c, Operand *x, String const &name void check_or_else_expr_no_value_error(CheckerContext *c, String const &name, Operand const &x, Type *type_hint); void check_or_return_split_types(CheckerContext *c, Operand *x, String const &name, Type **left_type_, Type **right_type_); +bool is_diverging_expr(Ast *expr); void check_did_you_mean_print(DidYouMeanAnswers *d, char const *prefix = "") { auto results = did_you_mean_results(d); @@ -2051,7 +2052,7 @@ bool check_is_not_addressable(CheckerContext *c, Operand *o) { return false; } - return o->mode != Addressing_Variable; + return o->mode != Addressing_Variable && o->mode != Addressing_SoaVariable; } void check_unary_expr(CheckerContext *c, Operand *o, Token op, Ast *node) { @@ -2068,9 +2069,6 @@ void check_unary_expr(CheckerContext *c, Operand *o, Token op, Ast *node) { error(op, "Cannot take the pointer address of '%s' which is a procedure parameter", str); } else { switch (o->mode) { - case Addressing_SoaVariable: - error(op, "Cannot take the pointer address of '%s' as it is an indirect index of an SOA struct", str); - break; case Addressing_Constant: error(op, "Cannot take the pointer address of '%s' which is a constant", str); break; @@ -2098,7 +2096,19 @@ void check_unary_expr(CheckerContext *c, Operand *o, Token op, Ast *node) { return; } - o->type = alloc_type_pointer(o->type); + if (o->mode == Addressing_SoaVariable) { + ast_node(ue, UnaryExpr, node); + if (ast_node_expect(ue->expr, Ast_IndexExpr)) { + ast_node(ie, IndexExpr, ue->expr); + Type *soa_type = type_of_expr(ie->expr); + GB_ASSERT(is_type_soa_struct(soa_type)); + o->type = alloc_type_soa_pointer(soa_type); + } else { + o->type = alloc_type_pointer(o->type); + } + } else { + o->type = alloc_type_pointer(o->type); + } switch (o->mode) { case Addressing_OptionalOk: @@ -2495,8 +2505,17 @@ void check_shift(CheckerContext *c, Operand *x, Operand *y, Ast *node, Type *typ x->expr->tav.is_lhs = true; } x->mode = Addressing_Value; - if (type_hint && is_type_integer(type_hint)) { - x->type = type_hint; + if (type_hint) { + if (is_type_integer(type_hint)) { + x->type = type_hint; + } else { + gbString x_str = expr_to_string(x->expr); + gbString to_type = type_to_string(type_hint); + error(node, "Conversion of shifted operand '%s' to '%s' is not allowed", x_str, to_type); + gb_string_free(x_str); + gb_string_free(to_type); + x->mode = Addressing_Invalid; + } } // x->value = x_val; return; @@ -2512,7 +2531,7 @@ void check_shift(CheckerContext *c, Operand *x, Operand *y, Ast *node, Type *typ // TODO(bill): Should we support shifts for fixed arrays and #simd vectors? if (!is_type_integer(x->type)) { - gbString err_str = expr_to_string(y->expr); + gbString err_str = expr_to_string(x->expr); error(node, "Shift operand '%s' must be an integer", err_str); gb_string_free(err_str); x->mode = Addressing_Invalid; @@ -2959,7 +2978,14 @@ void check_binary_matrix(CheckerContext *c, Token const &op, Operand *x, Operand goto matrix_error; } x->mode = Addressing_Value; - x->type = alloc_type_matrix(xt->Matrix.elem, xt->Matrix.row_count, yt->Matrix.column_count); + if (are_types_identical(xt, yt)) { + if (!is_type_named(x->type) && is_type_named(y->type)) { + // prefer the named type + x->type = y->type; + } + } else { + x->type = alloc_type_matrix(xt->Matrix.elem, xt->Matrix.row_count, yt->Matrix.column_count); + } goto matrix_success; } else if (yt->kind == Type_Array) { if (!are_types_identical(xt->Matrix.elem, yt->Array.elem)) { @@ -3021,7 +3047,6 @@ void check_binary_matrix(CheckerContext *c, Token const &op, Operand *x, Operand matrix_success: x->type = check_matrix_type_hint(x->type, type_hint); - return; @@ -7393,8 +7418,25 @@ ExprKind check_or_else_expr(CheckerContext *c, Operand *o, Ast *node, Type *type return Expr_Expr; } - check_multi_expr_with_type_hint(c, &y, default_value, x.type); - error_operand_no_value(&y); + bool y_is_diverging = false; + check_expr_base(c, &y, default_value, x.type); + switch (y.mode) { + case Addressing_NoValue: + if (is_diverging_expr(y.expr)) { + // Allow + y.mode = Addressing_Value; + y_is_diverging = true; + } else { + error_operand_no_value(&y); + y.mode = Addressing_Invalid; + } + break; + case Addressing_Type: + error_operand_not_expression(&y); + y.mode = Addressing_Invalid; + break; + } + if (y.mode == Addressing_Invalid) { o->mode = Addressing_Value; o->type = t_invalid; @@ -7408,7 +7450,9 @@ ExprKind check_or_else_expr(CheckerContext *c, Operand *o, Ast *node, Type *type add_type_and_value(&c->checker->info, arg, x.mode, x.type, x.value); if (left_type != nullptr) { - check_assignment(c, &y, left_type, name); + if (!y_is_diverging) { + check_assignment(c, &y, left_type, name); + } } else { check_or_else_expr_no_value_error(c, name, x, type_hint); } @@ -8404,23 +8448,30 @@ ExprKind check_compound_literal(CheckerContext *c, Operand *o, Ast *node, Type * if (is_type_bit_set(type)) { // NOTE(bill): Encode as an integer - i64 lower = base_type(type)->BitSet.lower; + Type *bt = base_type(type); + BigInt bits = {}; + BigInt one = {}; + big_int_from_u64(&one, 1); - u64 bits = 0; - for_array(index, cl->elems) { - Ast *elem = cl->elems[index]; - GB_ASSERT(elem->kind != Ast_FieldValue); - TypeAndValue tav = elem->tav; - ExactValue i = exact_value_to_integer(tav.value); - if (i.kind != ExactValue_Integer) { + for_array(i, cl->elems) { + Ast *e = cl->elems[i]; + GB_ASSERT(e->kind != Ast_FieldValue); + + TypeAndValue tav = e->tav; + if (tav.mode != Addressing_Constant) { continue; } - i64 val = big_int_to_i64(&i.value_integer); - val -= lower; - u64 bit = u64(1ll<BitSet.lower; + u64 index = cast(u64)(v-lower); + BigInt bit = {}; + big_int_from_u64(&bit, index); + big_int_shl(&bit, &one, &bit); + big_int_or(&bits, &bits, &bit); } - o->value = exact_value_u64(bits); + o->value.kind = ExactValue_Integer; + o->value.value_integer = bits; } else if (is_type_constant_type(type) && cl->elems.count == 0) { ExactValue value = exact_value_compound(node); Type *bt = core_type(type); @@ -8643,6 +8694,8 @@ ExprKind check_selector_call_expr(CheckerContext *c, Operand *o, Ast *node, Type Ast *first_arg = x.expr->SelectorExpr.expr; GB_ASSERT(first_arg != nullptr); + first_arg->state_flags |= StateFlag_SelectorCallExpr; + Type *pt = base_type(x.type); GB_ASSERT(pt->kind == Type_Proc); Type *first_type = nullptr; @@ -8668,6 +8721,7 @@ ExprKind check_selector_call_expr(CheckerContext *c, Operand *o, Ast *node, Type y.mode = first_arg->tav.mode; y.type = first_arg->tav.type; y.value = first_arg->tav.value; + if (check_is_assignable_to(c, &y, first_type)) { // Do nothing, it's valid } else { @@ -9286,7 +9340,7 @@ ExprKind check_expr_base_internal(CheckerContext *c, Operand *o, Ast *node, Type check_unary_expr(c, o, ue->op, node); } o->expr = node; - return kind; + return Expr_Expr; case_end; @@ -9342,6 +9396,9 @@ ExprKind check_expr_base_internal(CheckerContext *c, Operand *o, Ast *node, Type if (t->kind == Type_Pointer && !is_type_empty_union(t->Pointer.elem)) { o->mode = Addressing_Variable; o->type = t->Pointer.elem; + } 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); @@ -10034,9 +10091,10 @@ gbString write_expr_to_string(gbString str, Ast *node, bool shorthand) { str = write_expr_to_string(str, ce->proc, shorthand); str = gb_string_appendc(str, "("); - for_array(i, ce->args) { + isize idx0 = cast(isize)ce->was_selector; + for (isize i = idx0; i < ce->args.count; i++) { Ast *arg = ce->args[i]; - if (i > 0) { + if (i > idx0) { str = gb_string_appendc(str, ", "); } str = write_expr_to_string(str, arg, shorthand); diff --git a/src/check_stmt.cpp b/src/check_stmt.cpp index a6f6f1a7d..451325324 100644 --- a/src/check_stmt.cpp +++ b/src/check_stmt.cpp @@ -1,8 +1,5 @@ -bool is_diverging_stmt(Ast *stmt) { - if (stmt->kind != Ast_ExprStmt) { - return false; - } - Ast *expr = unparen_expr(stmt->ExprStmt.expr); +bool is_diverging_expr(Ast *expr) { + expr = unparen_expr(expr); if (expr->kind != Ast_CallExpr) { return false; } @@ -26,6 +23,12 @@ bool is_diverging_stmt(Ast *stmt) { t = base_type(t); return t != nullptr && t->kind == Type_Proc && t->Proc.diverging; } +bool is_diverging_stmt(Ast *stmt) { + if (stmt->kind != Ast_ExprStmt) { + return false; + } + return is_diverging_expr(stmt->ExprStmt.expr); +} bool contains_deferred_call(Ast *node) { if (node->viral_state_flags & ViralStateFlag_ContainsDeferredProcedure) { diff --git a/src/check_type.cpp b/src/check_type.cpp index bc89a9be9..da0a9706b 100644 --- a/src/check_type.cpp +++ b/src/check_type.cpp @@ -695,11 +695,6 @@ void check_union_type(CheckerContext *ctx, Type *union_type, Ast *node, Arrayalign, "A union with #no_nil must have at least 2 variants"); } break; - case UnionType_maybe: - if (variants.count != 1) { - error(ut->align, "A union with #maybe must have at 1 variant, got %lld", cast(long long)variants.count); - } - break; } if (ut->align != nullptr) { @@ -1345,7 +1340,9 @@ ParameterValue handle_parameter_value(CheckerContext *ctx, Type *in_type, Type * param_value.kind = ParameterValue_Constant; param_value.value = o.value; } else { - error(expr, "Default parameter must be a constant, %d", o.mode); + gbString s = expr_to_string(o.expr); + error(expr, "Default parameter must be a constant, got %s", s); + gb_string_free(s); } } } else { @@ -1614,6 +1611,10 @@ Type *check_get_params(CheckerContext *ctx, Scope *scope, Ast *_params, bool *is error(name, "'#any_int' can only be applied to variable fields"); p->flags &= ~FieldFlag_any_int; } + if (p->flags&FieldFlag_by_ptr) { + error(name, "'#by_ptr' can only be applied to variable fields"); + p->flags &= ~FieldFlag_by_ptr; + } param = alloc_entity_type_name(scope, name->Ident.token, type, EntityState_Resolved); param->TypeName.is_type_alias = true; @@ -1690,10 +1691,17 @@ Type *check_get_params(CheckerContext *ctx, Scope *scope, Ast *_params, bool *is if (p->flags&FieldFlag_no_alias) { if (!is_type_pointer(type)) { - error(name, "'#no_alias' can only be applied to fields of pointer type"); + error(name, "'#no_alias' can only be applied pointer typed parameters"); p->flags &= ~FieldFlag_no_alias; // Remove the flag } } + if (p->flags&FieldFlag_by_ptr) { + if (is_type_internally_pointer_like(type)) { + error(name, "'#by_ptr' can only be applied to non-pointer-like parameters"); + p->flags &= ~FieldFlag_by_ptr; // Remove the flag + } + } + if (is_poly_name) { if (p->flags&FieldFlag_no_alias) { error(name, "'#no_alias' can only be applied to non constant values"); @@ -1711,6 +1719,10 @@ Type *check_get_params(CheckerContext *ctx, Scope *scope, Ast *_params, bool *is error(name, "'#const' can only be applied to variable fields"); p->flags &= ~FieldFlag_const; } + if (p->flags&FieldFlag_by_ptr) { + error(name, "'#by_ptr' can only be applied to variable fields"); + p->flags &= ~FieldFlag_by_ptr; + } if (!is_type_constant_type(type) && !is_type_polymorphic(type)) { gbString str = type_to_string(type); @@ -1743,6 +1755,9 @@ Type *check_get_params(CheckerContext *ctx, Scope *scope, Ast *_params, bool *is if (p->flags&FieldFlag_const) { param->flags |= EntityFlag_ConstInput; } + if (p->flags&FieldFlag_by_ptr) { + param->flags |= EntityFlag_ByPtr; + } param->state = EntityState_Resolved; // NOTE(bill): This should have be resolved whilst determining it add_entity(ctx, scope, name, param); @@ -2678,14 +2693,55 @@ bool check_type_internal(CheckerContext *ctx, Ast *e, Type **type, Type *named_t case_ast_node(ue, UnaryExpr, e); switch (ue->op.kind) { case Token_Pointer: - *type = alloc_type_pointer(check_type(ctx, ue->expr)); - set_base_type(named_type, *type); - return true; + { + Type *elem = check_type(ctx, ue->expr); + *type = alloc_type_pointer(elem); + set_base_type(named_type, *type); + return true; + } } case_end; case_ast_node(pt, PointerType, e); - *type = alloc_type_pointer(check_type(ctx, pt->type)); + CheckerContext c = *ctx; + c.type_path = new_checker_type_path(); + defer (destroy_checker_type_path(c.type_path)); + + Type *elem = t_invalid; + Operand o = {}; + check_expr_or_type(&c, &o, pt->type); + if (o.mode != Addressing_Invalid && o.mode != Addressing_Type) { + // NOTE(bill): call check_type_expr again to get a consistent error message + begin_error_block(); + elem = check_type_expr(&c, pt->type, nullptr); + if (o.mode == Addressing_Variable) { + gbString s = expr_to_string(pt->type); + error_line("\tSuggestion: ^ is used for pointer types, did you mean '&%s'?\n", s); + gb_string_free(s); + } + end_error_block(); + } else { + elem = o.type; + } + + if (pt->tag != nullptr) { + GB_ASSERT(pt->tag->kind == Ast_BasicDirective); + String name = pt->tag->BasicDirective.name.string; + if (name == "soa") { + // TODO(bill): generic #soa pointers + if (is_type_soa_struct(elem)) { + *type = alloc_type_soa_pointer(elem); + } else { + error(pt->tag, "#soa pointers require an #soa record type as the element"); + *type = alloc_type_pointer(elem); + } + } else { + error(pt->tag, "Invalid tag applied to pointer, got #%.*s", LIT(name)); + *type = alloc_type_pointer(elem); + } + } else { + *type = alloc_type_pointer(elem); + } set_base_type(named_type, *type); return true; case_end; diff --git a/src/checker.cpp b/src/checker.cpp index 874839ece..c75fc86af 100644 --- a/src/checker.cpp +++ b/src/checker.cpp @@ -1947,6 +1947,11 @@ void add_type_info_type_internal(CheckerContext *c, Type *t) { add_type_info_type_internal(c, bt->Matrix.elem); break; + case Type_SoaPointer: + add_type_info_type_internal(c, bt->SoaPointer.elem); + break; + + default: GB_PANIC("Unhandled type: %*.s %d", LIT(type_strings[bt->kind]), bt->kind); break; @@ -2164,6 +2169,10 @@ void add_min_dep_type_info(Checker *c, Type *t) { add_min_dep_type_info(c, bt->Matrix.elem); break; + case Type_SoaPointer: + add_min_dep_type_info(c, bt->SoaPointer.elem); + break; + default: GB_PANIC("Unhandled type: %*.s", LIT(type_strings[bt->kind])); break; @@ -2756,6 +2765,7 @@ void init_core_type_info(Checker *c) { t_type_info_relative_pointer = find_core_type(c, str_lit("Type_Info_Relative_Pointer")); t_type_info_relative_slice = find_core_type(c, str_lit("Type_Info_Relative_Slice")); 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_named_ptr = alloc_type_pointer(t_type_info_named); t_type_info_integer_ptr = alloc_type_pointer(t_type_info_integer); @@ -2784,6 +2794,7 @@ void init_core_type_info(Checker *c) { t_type_info_relative_pointer_ptr = alloc_type_pointer(t_type_info_relative_pointer); t_type_info_relative_slice_ptr = alloc_type_pointer(t_type_info_relative_slice); 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); } void init_mem_allocator(Checker *c) { diff --git a/src/checker_builtin_procs.hpp b/src/checker_builtin_procs.hpp index 05f256775..8dd021255 100644 --- a/src/checker_builtin_procs.hpp +++ b/src/checker_builtin_procs.hpp @@ -40,6 +40,8 @@ enum BuiltinProcId { BuiltinProc_hadamard_product, BuiltinProc_matrix_flatten, + BuiltinProc_unreachable, + BuiltinProc_DIRECTIVE, // NOTE(bill): This is used for specialized hash-prefixed procedures // "Intrinsics" @@ -330,6 +332,8 @@ gb_global BuiltinProc builtin_procs[BuiltinProc_COUNT] = { {STR_LIT("hadamard_product"), 2, false, Expr_Expr, BuiltinProcPkg_builtin}, {STR_LIT("matrix_flatten"), 1, false, Expr_Expr, BuiltinProcPkg_builtin}, + {STR_LIT("unreachable"), 0, false, Expr_Expr, BuiltinProcPkg_builtin, /*diverging*/true}, + {STR_LIT(""), 0, true, Expr_Expr, BuiltinProcPkg_builtin}, // DIRECTIVE @@ -341,7 +345,7 @@ gb_global BuiltinProc builtin_procs[BuiltinProc_COUNT] = { {STR_LIT("alloca"), 2, false, Expr_Expr, BuiltinProcPkg_intrinsics}, {STR_LIT("cpu_relax"), 0, false, Expr_Stmt, BuiltinProcPkg_intrinsics}, - {STR_LIT("trap"), 0, false, Expr_Stmt, BuiltinProcPkg_intrinsics, /*diverging*/true}, + {STR_LIT("trap"), 0, false, Expr_Expr, BuiltinProcPkg_intrinsics, /*diverging*/true}, {STR_LIT("debug_trap"), 0, false, Expr_Stmt, BuiltinProcPkg_intrinsics, /*diverging*/false}, {STR_LIT("read_cycle_counter"), 0, false, Expr_Expr, BuiltinProcPkg_intrinsics}, @@ -434,7 +438,7 @@ gb_global BuiltinProc builtin_procs[BuiltinProc_COUNT] = { {STR_LIT("simd_neg"), 1, false, Expr_Expr, BuiltinProcPkg_intrinsics}, - {STR_LIT("simd_abs"), 2, false, Expr_Expr, BuiltinProcPkg_intrinsics}, + {STR_LIT("simd_abs"), 1, false, Expr_Expr, BuiltinProcPkg_intrinsics}, {STR_LIT("simd_min"), 2, false, Expr_Expr, BuiltinProcPkg_intrinsics}, {STR_LIT("simd_max"), 2, false, Expr_Expr, BuiltinProcPkg_intrinsics}, diff --git a/src/docs_format.cpp b/src/docs_format.cpp index ee32d0e05..b13b8b364 100644 --- a/src/docs_format.cpp +++ b/src/docs_format.cpp @@ -83,6 +83,7 @@ enum OdinDocTypeKind : u32 { OdinDocType_RelativeSlice = 21, OdinDocType_MultiPointer = 22, OdinDocType_Matrix = 23, + OdinDocType_SoaPointer = 24, }; enum OdinDocTypeFlag_Basic : u32 { @@ -98,7 +99,6 @@ enum OdinDocTypeFlag_Struct : u32 { enum OdinDocTypeFlag_Union : u32 { OdinDocTypeFlag_Union_polymorphic = 1<<0, OdinDocTypeFlag_Union_no_nil = 1<<1, - OdinDocTypeFlag_Union_maybe = 1<<2, OdinDocTypeFlag_Union_shared_nil = 1<<3, }; diff --git a/src/docs_writer.cpp b/src/docs_writer.cpp index 2f531a45c..1b8e1fc34 100644 --- a/src/docs_writer.cpp +++ b/src/docs_writer.cpp @@ -532,6 +532,10 @@ OdinDocTypeIndex odin_doc_type(OdinDocWriter *w, Type *type) { doc_type.kind = OdinDocType_MultiPointer; doc_type.types = odin_doc_type_as_slice(w, type->MultiPointer.elem); break; + case Type_SoaPointer: + doc_type.kind = OdinDocType_SoaPointer; + doc_type.types = odin_doc_type_as_slice(w, type->SoaPointer.elem); + break; case Type_Array: doc_type.kind = OdinDocType_Array; doc_type.elem_count_len = 1; diff --git a/src/entity.cpp b/src/entity.cpp index 76e6912b9..3d3712328 100644 --- a/src/entity.cpp +++ b/src/entity.cpp @@ -83,6 +83,7 @@ enum EntityFlag : u64 { EntityFlag_CustomLinkage_LinkOnce = 1ull<<44, EntityFlag_Require = 1ull<<50, + EntityFlag_ByPtr = 1ull<<51, // enforce parameter is passed by pointer EntityFlag_Overridden = 1ull<<63, }; diff --git a/src/gb/gb.h b/src/gb/gb.h index 48d3c9aec..d09c7618b 100644 --- a/src/gb/gb.h +++ b/src/gb/gb.h @@ -90,6 +90,10 @@ extern "C" { #error This operating system is not supported #endif +#if defined(GB_SYSTEM_OPENBSD) +#include +#endif + #if defined(_MSC_VER) #define GB_COMPILER_MSVC 1 #elif defined(__GNUC__) diff --git a/src/llvm_abi.cpp b/src/llvm_abi.cpp index 60a07e531..2ee8dc673 100644 --- a/src/llvm_abi.cpp +++ b/src/llvm_abi.cpp @@ -62,7 +62,7 @@ bool lb_is_type_kind(LLVMTypeRef type, LLVMTypeKind kind) { return LLVMGetTypeKind(type) == kind; } -LLVMTypeRef lb_function_type_to_llvm_ptr(lbFunctionType *ft, bool is_var_arg) { +LLVMTypeRef lb_function_type_to_llvm_raw(lbFunctionType *ft, bool is_var_arg) { unsigned arg_count = cast(unsigned)ft->args.count; unsigned offset = 0; @@ -108,10 +108,16 @@ LLVMTypeRef lb_function_type_to_llvm_ptr(lbFunctionType *ft, bool is_var_arg) { } unsigned total_arg_count = arg_index; LLVMTypeRef func_type = LLVMFunctionType(ret, args, total_arg_count, is_var_arg); - return LLVMPointerType(func_type, 0); + return func_type; } +// LLVMTypeRef lb_function_type_to_llvm_ptr(lbFunctionType *ft, bool is_var_arg) { +// LLVMTypeRef func_type = lb_function_type_to_llvm_raw(ft, is_var_arg); +// return LLVMPointerType(func_type, 0); +// } + + void lb_add_function_type_attributes(LLVMValueRef fn, lbFunctionType *ft, ProcCallingConvention calling_convention) { if (ft == nullptr) { return; @@ -217,7 +223,7 @@ i64 lb_sizeof(LLVMTypeRef type) { break; case LLVMArrayTypeKind: { - LLVMTypeRef elem = LLVMGetElementType(type); + LLVMTypeRef elem = OdinLLVMGetArrayElementType(type); i64 elem_size = lb_sizeof(elem); i64 count = LLVMGetArrayLength(type); i64 size = count * elem_size; @@ -229,11 +235,11 @@ i64 lb_sizeof(LLVMTypeRef type) { return 8; case LLVMVectorTypeKind: { - LLVMTypeRef elem = LLVMGetElementType(type); + LLVMTypeRef elem = OdinLLVMGetVectorElementType(type); i64 elem_size = lb_sizeof(elem); i64 count = LLVMGetVectorSize(type); i64 size = count * elem_size; - return gb_clamp(next_pow2(size), 1, build_context.max_align); + return next_pow2(size); } } @@ -277,14 +283,14 @@ i64 lb_alignof(LLVMTypeRef type) { } break; case LLVMArrayTypeKind: - return lb_alignof(LLVMGetElementType(type)); + return lb_alignof(OdinLLVMGetArrayElementType(type)); case LLVMX86_MMXTypeKind: return 8; case LLVMVectorTypeKind: { // TODO(bill): This appears to be correct but LLVM isn't necessarily "great" with regards to documentation - LLVMTypeRef elem = LLVMGetElementType(type); + LLVMTypeRef elem = OdinLLVMGetVectorElementType(type); i64 elem_size = lb_sizeof(elem); i64 count = LLVMGetVectorSize(type); i64 size = count * elem_size; @@ -787,7 +793,7 @@ namespace lbAbiAmd64SysV { case LLVMArrayTypeKind: { i64 len = LLVMGetArrayLength(t); - LLVMTypeRef elem = LLVMGetElementType(t); + LLVMTypeRef elem = OdinLLVMGetArrayElementType(t); i64 elem_sz = lb_sizeof(elem); for (i64 i = 0; i < len; i++) { classify_with(elem, cls, ix, off + i*elem_sz); @@ -797,20 +803,27 @@ namespace lbAbiAmd64SysV { case LLVMVectorTypeKind: { i64 len = LLVMGetVectorSize(t); - LLVMTypeRef elem = LLVMGetElementType(t); + LLVMTypeRef elem = OdinLLVMGetVectorElementType(t); i64 elem_sz = lb_sizeof(elem); LLVMTypeKind elem_kind = LLVMGetTypeKind(elem); RegClass reg = RegClass_NoClass; + unsigned elem_width = LLVMGetIntTypeWidth(elem); switch (elem_kind) { case LLVMIntegerTypeKind: case LLVMHalfTypeKind: - switch (LLVMGetIntTypeWidth(elem)) { - case 8: reg = RegClass_SSEInt8; - case 16: reg = RegClass_SSEInt16; - case 32: reg = RegClass_SSEInt32; - case 64: reg = RegClass_SSEInt64; + switch (elem_width) { + case 8: reg = RegClass_SSEInt8; break; + case 16: reg = RegClass_SSEInt16; break; + case 32: reg = RegClass_SSEInt32; break; + case 64: reg = RegClass_SSEInt64; break; default: - GB_PANIC("Unhandled integer width for vector type"); + if (elem_width > 64) { + for (i64 i = 0; i < len; i++) { + classify_with(elem, cls, ix, off + i*elem_sz); + } + break; + } + GB_PANIC("Unhandled integer width for vector type %u", elem_width); } break; case LLVMFloatTypeKind: @@ -900,7 +913,7 @@ namespace lbAbiArm64 { if (len == 0) { return false; } - LLVMTypeRef elem = LLVMGetElementType(type); + LLVMTypeRef elem = OdinLLVMGetArrayElementType(type); LLVMTypeRef base_type = nullptr; unsigned member_count = 0; if (is_homogenous_aggregate(c, elem, &base_type, &member_count)) { @@ -1116,7 +1129,7 @@ namespace lbAbiWasm { } if (sz <= MAX_DIRECT_STRUCT_SIZE) { if (kind == LLVMArrayTypeKind) { - if (is_basic_register_type(LLVMGetElementType(type))) { + if (is_basic_register_type(OdinLLVMGetArrayElementType(type))) { return true; } } else if (kind == LLVMStructTypeKind) { diff --git a/src/llvm_backend.cpp b/src/llvm_backend.cpp index cf7389ec1..6ee1541d6 100644 --- a/src/llvm_backend.cpp +++ b/src/llvm_backend.cpp @@ -739,11 +739,11 @@ lbProcedure *lb_create_startup_runtime(lbModule *main_module, lbProcedure *start lb_begin_procedure_body(p); if (startup_type_info) { - LLVMBuildCall2(p->builder, LLVMGetElementType(lb_type(main_module, startup_type_info->type)), startup_type_info->value, nullptr, 0, ""); + LLVMBuildCall2(p->builder, lb_type_internal_for_procedures_raw(main_module, startup_type_info->type), startup_type_info->value, nullptr, 0, ""); } if (objc_names) { - LLVMBuildCall2(p->builder, LLVMGetElementType(lb_type(main_module, objc_names->type)), objc_names->value, nullptr, 0, ""); + LLVMBuildCall2(p->builder, lb_type_internal_for_procedures_raw(main_module, objc_names->type), objc_names->value, nullptr, 0, ""); } for_array(i, global_variables) { @@ -762,7 +762,7 @@ lbProcedure *lb_create_startup_runtime(lbModule *main_module, lbProcedure *start if (init_expr != nullptr) { lbValue init = lb_build_expr(p, init_expr); if (init.value == nullptr) { - LLVMTypeRef global_type = LLVMGetElementType(LLVMTypeOf(var->var.value)); + LLVMTypeRef global_type = llvm_addr_type(p->module, var->var); if (is_type_untyped_undef(init.type)) { // LLVMSetInitializer(var->var.value, LLVMGetUndef(global_type)); LLVMSetInitializer(var->var.value, LLVMConstNull(global_type)); @@ -805,8 +805,7 @@ lbProcedure *lb_create_startup_runtime(lbModule *main_module, lbProcedure *start lb_emit_store(p, data, lb_emit_conv(p, gp, t_rawptr)); lb_emit_store(p, ti, lb_type_info(main_module, var_type)); } else { - LLVMTypeRef pvt = LLVMTypeOf(var->var.value); - LLVMTypeRef vt = LLVMGetElementType(pvt); + LLVMTypeRef vt = llvm_addr_type(p->module, var->var); lbValue src0 = lb_emit_conv(p, var->init, t); LLVMValueRef src = OdinLLVMBuildTransmute(p, src0.value, vt); LLVMValueRef dst = var->var.value; @@ -933,7 +932,7 @@ lbProcedure *lb_create_main_procedure(lbModule *m, lbProcedure *startup_runtime) GB_ASSERT(LLVMIsConstant(vals[1])); GB_ASSERT(LLVMIsConstant(vals[2])); - LLVMValueRef dst = LLVMConstInBoundsGEP(all_tests_array.value, indices, gb_count_of(indices)); + LLVMValueRef dst = LLVMConstInBoundsGEP2(llvm_addr_type(m, all_tests_array), all_tests_array.value, indices, gb_count_of(indices)); LLVMValueRef src = llvm_const_named_struct(m, t_Internal_Test, vals, gb_count_of(vals)); LLVMBuildStore(p->builder, src, dst); diff --git a/src/llvm_backend.hpp b/src/llvm_backend.hpp index f65e1079e..79f0f37e7 100644 --- a/src/llvm_backend.hpp +++ b/src/llvm_backend.hpp @@ -42,6 +42,18 @@ #define ODIN_LLVM_MINIMUM_VERSION_12 0 #endif +#if LLVM_VERSION_MAJOR > 13 || (LLVM_VERSION_MAJOR == 13 && LLVM_VERSION_MINOR >= 0 && LLVM_VERSION_PATCH > 0) +#define ODIN_LLVM_MINIMUM_VERSION_13 1 +#else +#define ODIN_LLVM_MINIMUM_VERSION_13 0 +#endif + +#if LLVM_VERSION_MAJOR > 14 || (LLVM_VERSION_MAJOR == 14 && LLVM_VERSION_MINOR >= 0 && LLVM_VERSION_PATCH > 0) +#define ODIN_LLVM_MINIMUM_VERSION_14 1 +#else +#define ODIN_LLVM_MINIMUM_VERSION_14 0 +#endif + struct lbProcedure; struct lbValue { @@ -115,6 +127,7 @@ struct lbModule { AstPackage *pkg; // associated PtrMap types; + PtrMap func_raw_types; PtrMap struct_field_remapping; // Key: LLVMTypeRef or Type * i32 internal_type_level; @@ -292,11 +305,18 @@ struct lbProcedure { LLVMMetadataRef debug_info; lbCopyElisionHint copy_elision_hint; + + PtrMap selector_values; + PtrMap selector_addr; }; - +#if !ODIN_LLVM_MINIMUM_VERSION_14 +#define LLVMConstGEP2(Ty__, ConstantVal__, ConstantIndices__, NumIndices__) LLVMConstGEP(ConstantVal__, ConstantIndices__, NumIndices__) +#define LLVMConstInBoundsGEP2(Ty__, ConstantVal__, ConstantIndices__, NumIndices__) LLVMConstInBoundsGEP(ConstantVal__, ConstantIndices__, NumIndices__) +#define LLVMBuildPtrDiff2(Builder__, Ty__, LHS__, RHS__, Name__) LLVMBuildPtrDiff(Builder__, LHS__, RHS__, Name__) +#endif bool lb_init_generator(lbGenerator *gen, Checker *c); @@ -311,7 +331,8 @@ lbProcedure *lb_create_procedure(lbModule *module, Entity *entity, bool ignore_b void lb_end_procedure(lbProcedure *p); -LLVMTypeRef lb_type(lbModule *m, Type *type); +LLVMTypeRef lb_type(lbModule *m, Type *type); +LLVMTypeRef llvm_get_element_type(LLVMTypeRef type); lbBlock *lb_create_block(lbProcedure *p, char const *name, bool append=false); @@ -324,7 +345,7 @@ lbValue lb_const_int(lbModule *m, Type *type, u64 value); lbAddr lb_addr(lbValue addr); Type *lb_addr_type(lbAddr const &addr); -LLVMTypeRef lb_addr_lb_type(lbAddr const &addr); +LLVMTypeRef llvm_addr_type(lbModule *module, lbValue addr_val); void lb_addr_store(lbProcedure *p, lbAddr addr, lbValue value); lbValue lb_addr_load(lbProcedure *p, lbAddr const &addr); lbValue lb_emit_load(lbProcedure *p, lbValue v); @@ -336,8 +357,9 @@ lbValue lb_build_expr(lbProcedure *p, Ast *expr); lbAddr lb_build_addr(lbProcedure *p, Ast *expr); void lb_build_stmt_list(lbProcedure *p, Array const &stmts); -lbValue lb_build_gep(lbProcedure *p, lbValue const &value, i32 index) ; - +lbValue lb_emit_epi(lbProcedure *p, lbValue const &value, isize index); +lbValue lb_emit_epi(lbModule *m, lbValue const &value, isize index); +lbValue lb_emit_array_epi(lbModule *m, lbValue s, isize index); lbValue lb_emit_struct_ep(lbProcedure *p, lbValue s, i32 index); lbValue lb_emit_struct_ev(lbProcedure *p, lbValue s, i32 index); lbValue lb_emit_array_epi(lbProcedure *p, lbValue value, isize index); @@ -477,9 +499,17 @@ LLVMTypeRef lb_type_padding_filler(lbModule *m, i64 padding, i64 padding_align); LLVMValueRef llvm_basic_shuffle(lbProcedure *p, LLVMValueRef vector, LLVMValueRef mask); +LLVMValueRef lb_call_intrinsic(lbProcedure *p, const char *name, LLVMValueRef* args, unsigned arg_count, LLVMTypeRef* types, unsigned type_count); void lb_mem_copy_overlapping(lbProcedure *p, lbValue dst, lbValue src, lbValue len, bool is_volatile=false); void lb_mem_copy_non_overlapping(lbProcedure *p, lbValue dst, lbValue src, lbValue len, bool is_volatile=false); +LLVMValueRef lb_mem_zero_ptr_internal(lbProcedure *p, LLVMValueRef ptr, LLVMValueRef len, unsigned alignment, bool is_volatile); +i64 lb_max_zero_init_size(void) { + return cast(i64)(4*build_context.word_size); +} + +LLVMTypeRef OdinLLVMGetArrayElementType(LLVMTypeRef type); +LLVMTypeRef OdinLLVMGetVectorElementType(LLVMTypeRef type); #define LB_STARTUP_RUNTIME_PROC_NAME "__$startup_runtime" #define LB_STARTUP_TYPE_INFO_PROC_NAME "__$startup_type_info" diff --git a/src/llvm_backend_const.cpp b/src/llvm_backend_const.cpp index 00452eb50..954778bb6 100644 --- a/src/llvm_backend_const.cpp +++ b/src/llvm_backend_const.cpp @@ -10,11 +10,12 @@ bool lb_is_const(lbValue value) { return false; } - bool lb_is_const_or_global(lbValue value) { if (lb_is_const(value)) { return true; } + // TODO remove use of LLVMGetElementType + #if 0 if (LLVMGetValueKind(value.value) == LLVMGlobalVariableValueKind) { LLVMTypeRef t = LLVMGetElementType(LLVMTypeOf(value.value)); if (!lb_is_type_kind(t, LLVMPointerTypeKind)) { @@ -23,6 +24,7 @@ bool lb_is_const_or_global(lbValue value) { LLVMTypeRef elem = LLVMGetElementType(t); return lb_is_type_kind(elem, LLVMFunctionTypeKind); } + #endif return false; } @@ -418,7 +420,7 @@ lbValue lb_const_value(lbModule *m, Type *type, ExactValue value, bool allow_loc { LLVMValueRef indices[2] = {llvm_zero(m), llvm_zero(m)}; - LLVMValueRef ptr = LLVMBuildInBoundsGEP(p->builder, array_data, indices, 2, ""); + LLVMValueRef ptr = LLVMBuildInBoundsGEP2(p->builder, llvm_type, array_data, indices, 2, ""); LLVMValueRef len = LLVMConstInt(lb_type(m, t_int), count, true); lbAddr slice = lb_add_local_generated(p, type, false); lb_fill_slice(p, slice, {ptr, alloc_type_pointer(elem)}, {len, t_int}); @@ -445,7 +447,7 @@ lbValue lb_const_value(lbModule *m, Type *type, ExactValue value, bool allow_loc { LLVMValueRef indices[2] = {llvm_zero(m), llvm_zero(m)}; - LLVMValueRef ptr = LLVMConstInBoundsGEP(array_data, indices, 2); + LLVMValueRef ptr = LLVMConstInBoundsGEP2(lb_type(m, t), array_data, indices, 2); LLVMValueRef len = LLVMConstInt(lb_type(m, t_int), count, true); LLVMValueRef values[2] = {ptr, len}; @@ -1007,7 +1009,7 @@ lbValue lb_const_value(lbModule *m, Type *type, ExactValue value, bool allow_loc for (isize i = 0; i < value_count; i++) { LLVMValueRef val = old_values[i]; if (!LLVMIsConstant(val)) { - LLVMValueRef dst = LLVMBuildStructGEP(p->builder, v.addr.value, cast(unsigned)i, ""); + LLVMValueRef dst = LLVMBuildStructGEP2(p->builder, llvm_addr_type(p->module, v.addr), v.addr.value, cast(unsigned)i, ""); LLVMBuildStore(p->builder, val, dst); } } @@ -1025,7 +1027,10 @@ lbValue lb_const_value(lbModule *m, Type *type, ExactValue value, bool allow_loc return lb_const_nil(m, original_type); } - u64 bits = 0; + BigInt bits = {}; + BigInt one = {}; + big_int_from_u64(&one, 1); + for_array(i, cl->elems) { Ast *e = cl->elems[i]; GB_ASSERT(e->kind != Ast_FieldValue); @@ -1037,18 +1042,13 @@ lbValue lb_const_value(lbModule *m, Type *type, ExactValue value, bool allow_loc GB_ASSERT(tav.value.kind == ExactValue_Integer); i64 v = big_int_to_i64(&tav.value.value_integer); i64 lower = type->BitSet.lower; - bits |= 1ull<decl_block->block; + LLVMBasicBlockRef block = p->curr_block->block; LLVMMetadataRef llvm_debug_loc = lb_debug_location_from_token_pos(p, token.pos); LLVMMetadataRef llvm_expr = LLVMDIBuilderCreateExpression(m->debug_builder, nullptr, 0); lb_set_llvm_metadata(m, ptr, llvm_expr); diff --git a/src/llvm_backend_expr.cpp b/src/llvm_backend_expr.cpp index 1894e85f6..5fd2fbe6f 100644 --- a/src/llvm_backend_expr.cpp +++ b/src/llvm_backend_expr.cpp @@ -1,3 +1,4 @@ +lbValue lb_emit_arith_matrix(lbProcedure *p, TokenKind op, lbValue lhs, lbValue rhs, Type *type, bool component_wise=false); lbValue lb_emit_logical_binary_expr(lbProcedure *p, TokenKind op, Ast *left, Ast *right, Type *type) { lbModule *m = p->module; @@ -242,8 +243,9 @@ lbValue lb_emit_unary_arith(lbProcedure *p, TokenKind op, lbValue x, Type *type) LLVMValueRef v1 = LLVMBuildFNeg(p->builder, LLVMBuildExtractValue(p->builder, x.value, 1, ""), ""); lbAddr addr = lb_add_local_generated(p, x.type, false); - LLVMBuildStore(p->builder, v0, LLVMBuildStructGEP(p->builder, addr.addr.value, 0, "")); - LLVMBuildStore(p->builder, v1, LLVMBuildStructGEP(p->builder, addr.addr.value, 1, "")); + LLVMTypeRef type = llvm_addr_type(p->module, addr.addr); + LLVMBuildStore(p->builder, v0, LLVMBuildStructGEP2(p->builder, type, addr.addr.value, 0, "")); + LLVMBuildStore(p->builder, v1, LLVMBuildStructGEP2(p->builder, type, addr.addr.value, 1, "")); return lb_addr_load(p, addr); } else if (is_type_quaternion(x.type)) { @@ -253,10 +255,11 @@ lbValue lb_emit_unary_arith(lbProcedure *p, TokenKind op, lbValue x, Type *type) LLVMValueRef v3 = LLVMBuildFNeg(p->builder, LLVMBuildExtractValue(p->builder, x.value, 3, ""), ""); lbAddr addr = lb_add_local_generated(p, x.type, false); - LLVMBuildStore(p->builder, v0, LLVMBuildStructGEP(p->builder, addr.addr.value, 0, "")); - LLVMBuildStore(p->builder, v1, LLVMBuildStructGEP(p->builder, addr.addr.value, 1, "")); - LLVMBuildStore(p->builder, v2, LLVMBuildStructGEP(p->builder, addr.addr.value, 2, "")); - LLVMBuildStore(p->builder, v3, LLVMBuildStructGEP(p->builder, addr.addr.value, 3, "")); + LLVMTypeRef type = llvm_addr_type(p->module, addr.addr); + LLVMBuildStore(p->builder, v0, LLVMBuildStructGEP2(p->builder, type, addr.addr.value, 0, "")); + LLVMBuildStore(p->builder, v1, LLVMBuildStructGEP2(p->builder, type, addr.addr.value, 1, "")); + LLVMBuildStore(p->builder, v2, LLVMBuildStructGEP2(p->builder, type, addr.addr.value, 2, "")); + LLVMBuildStore(p->builder, v3, LLVMBuildStructGEP2(p->builder, type, addr.addr.value, 3, "")); return lb_addr_load(p, addr); } else if (is_type_simd_vector(x.type)) { Type *elem = base_array_type(x.type); @@ -265,6 +268,11 @@ lbValue lb_emit_unary_arith(lbProcedure *p, TokenKind op, lbValue x, Type *type) } else { res.value = LLVMBuildNeg(p->builder, x.value, ""); } + } else if (is_type_matrix(x.type)) { + lbValue zero = {}; + zero.value = LLVMConstNull(lb_type(p->module, type)); + zero.type = type; + return lb_emit_arith_matrix(p, Token_Sub, zero, x, type, true); } else { GB_PANIC("Unhandled type %s", type_to_string(x.type)); } @@ -537,7 +545,7 @@ LLVMValueRef lb_matrix_to_vector(lbProcedure *p, lbValue matrix) { #if 1 LLVMValueRef ptr = lb_address_from_load_or_generate_local(p, matrix).value; LLVMValueRef matrix_vector_ptr = LLVMBuildPointerCast(p->builder, ptr, LLVMPointerType(total_matrix_type, 0), ""); - LLVMValueRef matrix_vector = LLVMBuildLoad(p->builder, matrix_vector_ptr, ""); + LLVMValueRef matrix_vector = LLVMBuildLoad2(p->builder, total_matrix_type, matrix_vector_ptr, ""); LLVMSetAlignment(matrix_vector, cast(unsigned)type_align_of(mt)); return matrix_vector; #else @@ -549,7 +557,7 @@ LLVMValueRef lb_matrix_to_vector(lbProcedure *p, lbValue matrix) { LLVMValueRef lb_matrix_trimmed_vector_mask(lbProcedure *p, Type *mt) { mt = base_type(mt); GB_ASSERT(mt->kind == Type_Matrix); - + unsigned stride = cast(unsigned)matrix_type_stride_in_elems(mt); unsigned row_count = cast(unsigned)mt->Matrix.row_count; unsigned column_count = cast(unsigned)mt->Matrix.column_count; @@ -561,23 +569,23 @@ LLVMValueRef lb_matrix_trimmed_vector_mask(lbProcedure *p, Type *mt) { mask_elems[mask_elems_index++] = lb_const_int(p->module, t_u32, offset).value; } } - + LLVMValueRef mask = LLVMConstVector(mask_elems.data, cast(unsigned)mask_elems.count); return mask; } LLVMValueRef lb_matrix_to_trimmed_vector(lbProcedure *p, lbValue m) { LLVMValueRef vector = lb_matrix_to_vector(p, m); - + Type *mt = base_type(m.type); GB_ASSERT(mt->kind == Type_Matrix); - + unsigned stride = cast(unsigned)matrix_type_stride_in_elems(mt); unsigned row_count = cast(unsigned)mt->Matrix.row_count; if (stride == row_count) { return vector; } - + LLVMValueRef mask = lb_matrix_trimmed_vector_mask(p, mt); LLVMValueRef trimmed_vector = llvm_basic_shuffle(p, vector, mask); return trimmed_vector; @@ -613,28 +621,28 @@ lbValue lb_emit_matrix_tranpose(lbProcedure *p, lbValue m, Type *type) { } Type *mt = base_type(m.type); GB_ASSERT(mt->kind == Type_Matrix); - + if (lb_is_matrix_simdable(mt)) { unsigned stride = cast(unsigned)matrix_type_stride_in_elems(mt); unsigned row_count = cast(unsigned)mt->Matrix.row_count; unsigned column_count = cast(unsigned)mt->Matrix.column_count; - + auto rows = slice_make(permanent_allocator(), row_count); auto mask_elems = slice_make(permanent_allocator(), column_count); - + LLVMValueRef vector = lb_matrix_to_vector(p, m); for (unsigned i = 0; i < row_count; i++) { for (unsigned j = 0; j < column_count; j++) { unsigned offset = stride*j + i; mask_elems[j] = lb_const_int(p->module, t_u32, offset).value; } - + // transpose mask LLVMValueRef mask = LLVMConstVector(mask_elems.data, column_count); LLVMValueRef row = llvm_basic_shuffle(p, vector, mask); rows[i] = row; } - + lbAddr res = lb_add_local_generated(p, type, true); for_array(i, rows) { LLVMValueRef row = rows[i]; @@ -643,12 +651,12 @@ lbValue lb_emit_matrix_tranpose(lbProcedure *p, lbValue m, Type *type) { ptr = LLVMBuildPointerCast(p->builder, ptr, LLVMPointerType(LLVMTypeOf(row), 0), ""); LLVMBuildStore(p->builder, row, ptr); } - + return lb_addr_load(p, res); } - + lbAddr res = lb_add_local_generated(p, type, true); - + i64 row_count = mt->Matrix.row_count; i64 column_count = mt->Matrix.column_count; for (i64 j = 0; j < column_count; j++) { @@ -666,10 +674,10 @@ lbValue lb_matrix_cast_vector_to_type(lbProcedure *p, LLVMValueRef vector, Type LLVMValueRef res_ptr = res.addr.value; unsigned alignment = cast(unsigned)gb_max(type_align_of(type), lb_alignof(LLVMTypeOf(vector))); LLVMSetAlignment(res_ptr, alignment); - + res_ptr = LLVMBuildPointerCast(p->builder, res_ptr, LLVMPointerType(LLVMTypeOf(vector), 0), ""); LLVMBuildStore(p->builder, vector, res_ptr); - + return lb_addr_load(p, res); } @@ -681,14 +689,14 @@ lbValue lb_emit_matrix_flatten(lbProcedure *p, lbValue m, Type *type) { } Type *mt = base_type(m.type); GB_ASSERT(mt->kind == Type_Matrix); - + if (lb_is_matrix_simdable(mt)) { LLVMValueRef vector = lb_matrix_to_trimmed_vector(p, m); return lb_matrix_cast_vector_to_type(p, vector, type); } - + lbAddr res = lb_add_local_generated(p, type, true); - + i64 row_count = mt->Matrix.row_count; i64 column_count = mt->Matrix.column_count; for (i64 j = 0; j < column_count; j++) { @@ -709,17 +717,17 @@ lbValue lb_emit_outer_product(lbProcedure *p, lbValue a, lbValue b, Type *type) GB_ASSERT(mt->kind == Type_Matrix); GB_ASSERT(at->kind == Type_Array); GB_ASSERT(bt->kind == Type_Array); - - + + i64 row_count = mt->Matrix.row_count; i64 column_count = mt->Matrix.column_count; - + GB_ASSERT(row_count == at->Array.count); GB_ASSERT(column_count == bt->Array.count); - - + + lbAddr res = lb_add_local_generated(p, type, true); - + for (i64 j = 0; j < column_count; j++) { for (i64 i = 0; i < row_count; i++) { lbValue x = lb_emit_struct_ev(p, a, cast(i32)i); @@ -735,51 +743,51 @@ lbValue lb_emit_outer_product(lbProcedure *p, lbValue a, lbValue b, Type *type) lbValue lb_emit_matrix_mul(lbProcedure *p, lbValue lhs, lbValue rhs, Type *type) { // TODO(bill): Handle edge case for f16 types on x86(-64) platforms - + Type *xt = base_type(lhs.type); Type *yt = base_type(rhs.type); - + GB_ASSERT(is_type_matrix(type)); GB_ASSERT(is_type_matrix(xt)); GB_ASSERT(is_type_matrix(yt)); GB_ASSERT(xt->Matrix.column_count == yt->Matrix.row_count); GB_ASSERT(are_types_identical(xt->Matrix.elem, yt->Matrix.elem)); - + Type *elem = xt->Matrix.elem; - + unsigned outer_rows = cast(unsigned)xt->Matrix.row_count; unsigned inner = cast(unsigned)xt->Matrix.column_count; unsigned outer_columns = cast(unsigned)yt->Matrix.column_count; - + if (lb_is_matrix_simdable(xt)) { unsigned x_stride = cast(unsigned)matrix_type_stride_in_elems(xt); unsigned y_stride = cast(unsigned)matrix_type_stride_in_elems(yt); - + auto x_rows = slice_make(permanent_allocator(), outer_rows); auto y_columns = slice_make(permanent_allocator(), outer_columns); - + LLVMValueRef x_vector = lb_matrix_to_vector(p, lhs); LLVMValueRef y_vector = lb_matrix_to_vector(p, rhs); - + auto mask_elems = slice_make(permanent_allocator(), inner); for (unsigned i = 0; i < outer_rows; i++) { for (unsigned j = 0; j < inner; j++) { unsigned offset = x_stride*j + i; mask_elems[j] = lb_const_int(p->module, t_u32, offset).value; } - + // transpose mask LLVMValueRef mask = LLVMConstVector(mask_elems.data, inner); LLVMValueRef row = llvm_basic_shuffle(p, x_vector, mask); x_rows[i] = row; } - + for (unsigned i = 0; i < outer_columns; i++) { LLVMValueRef mask = llvm_mask_iota(p->module, y_stride*i, inner); LLVMValueRef column = llvm_basic_shuffle(p, y_vector, mask); y_columns[i] = column; } - + lbAddr res = lb_add_local_generated(p, type, true); for_array(i, x_rows) { LLVMValueRef x_row = x_rows[i]; @@ -789,15 +797,15 @@ lbValue lb_emit_matrix_mul(lbProcedure *p, lbValue lhs, lbValue rhs, Type *type) lbValue dst = lb_emit_matrix_epi(p, res.addr, i, j); LLVMBuildStore(p->builder, elem, dst.value); } - } + } return lb_addr_load(p, res); } - + { lbAddr res = lb_add_local_generated(p, type, true); - + auto inners = slice_make(permanent_allocator(), inner); - + for (unsigned j = 0; j < outer_columns; j++) { for (unsigned i = 0; i < outer_rows; i++) { lbValue dst = lb_emit_matrix_epi(p, res.addr, i, j); @@ -805,7 +813,7 @@ lbValue lb_emit_matrix_mul(lbProcedure *p, lbValue lhs, lbValue rhs, Type *type) inners[k][0] = lb_emit_matrix_ev(p, lhs, i, k); inners[k][1] = lb_emit_matrix_ev(p, rhs, k, j); } - + lbValue sum = lb_const_nil(p->module, elem); for (unsigned k = 0; k < inner; k++) { lbValue a = inners[k][0]; @@ -815,51 +823,51 @@ lbValue lb_emit_matrix_mul(lbProcedure *p, lbValue lhs, lbValue rhs, Type *type) lb_emit_store(p, dst, sum); } } - + return lb_addr_load(p, res); } } lbValue lb_emit_matrix_mul_vector(lbProcedure *p, lbValue lhs, lbValue rhs, Type *type) { // TODO(bill): Handle edge case for f16 types on x86(-64) platforms - + Type *mt = base_type(lhs.type); Type *vt = base_type(rhs.type); - + GB_ASSERT(is_type_matrix(mt)); GB_ASSERT(is_type_array_like(vt)); - + i64 vector_count = get_array_type_count(vt); - + GB_ASSERT(mt->Matrix.column_count == vector_count); GB_ASSERT(are_types_identical(mt->Matrix.elem, base_array_type(vt))); - + Type *elem = mt->Matrix.elem; - + if (lb_is_matrix_simdable(mt)) { unsigned stride = cast(unsigned)matrix_type_stride_in_elems(mt); - + unsigned row_count = cast(unsigned)mt->Matrix.row_count; unsigned column_count = cast(unsigned)mt->Matrix.column_count; auto m_columns = slice_make(permanent_allocator(), column_count); auto v_rows = slice_make(permanent_allocator(), column_count); - - LLVMValueRef matrix_vector = lb_matrix_to_vector(p, lhs); - + + LLVMValueRef matrix_vector = lb_matrix_to_vector(p, lhs); + for (unsigned column_index = 0; column_index < column_count; column_index++) { LLVMValueRef mask = llvm_mask_iota(p->module, stride*column_index, row_count); LLVMValueRef column = llvm_basic_shuffle(p, matrix_vector, mask); m_columns[column_index] = column; } - + for (unsigned row_index = 0; row_index < column_count; row_index++) { LLVMValueRef value = lb_emit_struct_ev(p, rhs, row_index).value; LLVMValueRef row = llvm_vector_broadcast(p, value, row_count); v_rows[row_index] = row; } - + GB_ASSERT(column_count > 0); - + LLVMValueRef vector = nullptr; for (i64 i = 0; i < column_count; i++) { if (i == 0) { @@ -868,51 +876,51 @@ lbValue lb_emit_matrix_mul_vector(lbProcedure *p, lbValue lhs, lbValue rhs, Type vector = llvm_vector_mul_add(p, m_columns[i], v_rows[i], vector); } } - + return lb_matrix_cast_vector_to_type(p, vector, type); } - + lbAddr res = lb_add_local_generated(p, type, true); - + for (i64 i = 0; i < mt->Matrix.row_count; i++) { for (i64 j = 0; j < mt->Matrix.column_count; j++) { lbValue dst = lb_emit_matrix_epi(p, res.addr, i, 0); lbValue d0 = lb_emit_load(p, dst); - + lbValue a = lb_emit_matrix_ev(p, lhs, i, j); lbValue b = lb_emit_struct_ev(p, rhs, cast(i32)j); lbValue c = lb_emit_mul_add(p, a, b, d0, elem); lb_emit_store(p, dst, c); } } - + return lb_addr_load(p, res); } lbValue lb_emit_vector_mul_matrix(lbProcedure *p, lbValue lhs, lbValue rhs, Type *type) { // TODO(bill): Handle edge case for f16 types on x86(-64) platforms - + Type *mt = base_type(rhs.type); Type *vt = base_type(lhs.type); - + GB_ASSERT(is_type_matrix(mt)); GB_ASSERT(is_type_array_like(vt)); - + i64 vector_count = get_array_type_count(vt); - + GB_ASSERT(vector_count == mt->Matrix.row_count); GB_ASSERT(are_types_identical(mt->Matrix.elem, base_array_type(vt))); - + Type *elem = mt->Matrix.elem; - + if (lb_is_matrix_simdable(mt)) { unsigned stride = cast(unsigned)matrix_type_stride_in_elems(mt); - + unsigned row_count = cast(unsigned)mt->Matrix.row_count; unsigned column_count = cast(unsigned)mt->Matrix.column_count; gb_unused(column_count); auto m_columns = slice_make(permanent_allocator(), row_count); auto v_rows = slice_make(permanent_allocator(), row_count); - + LLVMValueRef matrix_vector = lb_matrix_to_vector(p, rhs); auto mask_elems = slice_make(permanent_allocator(), column_count); @@ -921,21 +929,21 @@ lbValue lb_emit_vector_mul_matrix(lbProcedure *p, lbValue lhs, lbValue rhs, Type unsigned offset = row_index + column_index*stride; mask_elems[column_index] = lb_const_int(p->module, t_u32, offset).value; } - + // transpose mask LLVMValueRef mask = LLVMConstVector(mask_elems.data, column_count); LLVMValueRef column = llvm_basic_shuffle(p, matrix_vector, mask); m_columns[row_index] = column; } - + for (unsigned column_index = 0; column_index < row_count; column_index++) { LLVMValueRef value = lb_emit_struct_ev(p, lhs, column_index).value; LLVMValueRef row = llvm_vector_broadcast(p, value, column_count); v_rows[column_index] = row; } - + GB_ASSERT(row_count > 0); - + LLVMValueRef vector = nullptr; for (i64 i = 0; i < row_count; i++) { if (i == 0) { @@ -949,41 +957,41 @@ lbValue lb_emit_vector_mul_matrix(lbProcedure *p, lbValue lhs, lbValue rhs, Type LLVMValueRef res_ptr = res.addr.value; unsigned alignment = cast(unsigned)gb_max(type_align_of(type), lb_alignof(LLVMTypeOf(vector))); LLVMSetAlignment(res_ptr, alignment); - + res_ptr = LLVMBuildPointerCast(p->builder, res_ptr, LLVMPointerType(LLVMTypeOf(vector), 0), ""); LLVMBuildStore(p->builder, vector, res_ptr); - + return lb_addr_load(p, res); } - + lbAddr res = lb_add_local_generated(p, type, true); - + for (i64 j = 0; j < mt->Matrix.column_count; j++) { for (i64 k = 0; k < mt->Matrix.row_count; k++) { lbValue dst = lb_emit_matrix_epi(p, res.addr, 0, j); lbValue d0 = lb_emit_load(p, dst); - + lbValue a = lb_emit_struct_ev(p, lhs, cast(i32)k); lbValue b = lb_emit_matrix_ev(p, rhs, k, j); lbValue c = lb_emit_mul_add(p, a, b, d0, elem); lb_emit_store(p, dst, c); } } - + return lb_addr_load(p, res); } -lbValue lb_emit_arith_matrix(lbProcedure *p, TokenKind op, lbValue lhs, lbValue rhs, Type *type, bool component_wise=false) { +lbValue lb_emit_arith_matrix(lbProcedure *p, TokenKind op, lbValue lhs, lbValue rhs, Type *type, bool component_wise) { GB_ASSERT(is_type_matrix(lhs.type) || is_type_matrix(rhs.type)); - - + + if (op == Token_Mul && !component_wise) { Type *xt = base_type(lhs.type); Type *yt = base_type(rhs.type); - + if (xt->kind == Type_Matrix) { if (yt->kind == Type_Matrix) { return lb_emit_matrix_mul(p, lhs, rhs, type); @@ -994,17 +1002,17 @@ lbValue lb_emit_arith_matrix(lbProcedure *p, TokenKind op, lbValue lhs, lbValue GB_ASSERT(yt->kind == Type_Matrix); return lb_emit_vector_mul_matrix(p, lhs, rhs, type); } - + } else { if (is_type_matrix(lhs.type)) { rhs = lb_emit_conv(p, rhs, lhs.type); } else { lhs = lb_emit_conv(p, lhs, rhs.type); } - + Type *xt = base_type(lhs.type); Type *yt = base_type(rhs.type); - + GB_ASSERT_MSG(are_types_identical(xt, yt), "%s %.*s %s", type_to_string(lhs.type), LIT(token_strings[op]), type_to_string(rhs.type)); GB_ASSERT(xt->kind == Type_Matrix); // element-wise arithmetic @@ -1013,8 +1021,8 @@ lbValue lb_emit_arith_matrix(lbProcedure *p, TokenKind op, lbValue lhs, lbValue lbValue array_rhs = rhs; Type *array_type = alloc_type_array(xt->Matrix.elem, matrix_type_total_internal_elems(xt)); GB_ASSERT(type_size_of(array_type) == type_size_of(xt)); - - array_lhs.type = array_type; + + array_lhs.type = array_type; array_rhs.type = array_type; if (token_is_comparison(op)) { @@ -1027,7 +1035,7 @@ lbValue lb_emit_arith_matrix(lbProcedure *p, TokenKind op, lbValue lhs, lbValue } } - + GB_PANIC("TODO: lb_emit_arith_matrix"); return {}; @@ -1308,13 +1316,13 @@ lbValue lb_build_binary_expr(lbProcedure *p, Ast *expr) { ast_node(be, BinaryExpr, expr); TypeAndValue tv = type_and_value_of_expr(expr); - + if (is_type_matrix(be->left->tav.type) || is_type_matrix(be->right->tav.type)) { lbValue left = lb_build_expr(p, be->left); lbValue right = lb_build_expr(p, be->right); return lb_emit_arith_matrix(p, be->op.kind, left, right, default_type(tv.type)); } - + switch (be->op.kind) { case Token_Add: @@ -1425,10 +1433,13 @@ lbValue lb_build_binary_expr(lbProcedure *p, Ast *expr) { Type *it = bit_set_to_int(rt); left = lb_emit_conv(p, left, it); + if (is_type_different_to_arch_endianness(it)) { + left = lb_emit_byte_swap(p, left, integer_endian_type_to_platform_type(it)); + } - lbValue lower = lb_const_value(p->module, it, exact_value_i64(rt->BitSet.lower)); - lbValue key = lb_emit_arith(p, Token_Sub, left, lower, it); - lbValue bit = lb_emit_arith(p, Token_Shl, lb_const_int(p->module, it, 1), key, it); + lbValue lower = lb_const_value(p->module, left.type, exact_value_i64(rt->BitSet.lower)); + lbValue key = lb_emit_arith(p, Token_Sub, left, lower, left.type); + lbValue bit = lb_emit_arith(p, Token_Shl, lb_const_int(p->module, left.type, 1), key, left.type); bit = lb_emit_conv(p, bit, it); lbValue old_value = lb_emit_transmute(p, right, it); @@ -1681,7 +1692,7 @@ lbValue lb_emit_conv(lbProcedure *p, lbValue value, Type *t) { } return res; } - + if (is_type_complex(src) && is_type_complex(dst)) { Type *ft = base_complex_elem_type(dst); @@ -1771,7 +1782,7 @@ lbValue lb_emit_conv(lbProcedure *p, lbValue value, Type *t) { } return lb_emit_conv(p, res, t); } - + if (is_type_integer_128bit(dst)) { auto args = array_make(temporary_allocator(), 1); args[0] = value; @@ -2044,10 +2055,10 @@ lbValue lb_emit_conv(lbProcedure *p, lbValue value, Type *t) { } return lb_addr_load(p, v); } - + if (is_type_matrix(dst) && !is_type_matrix(src)) { GB_ASSERT_MSG(dst->Matrix.row_count == dst->Matrix.column_count, "%s <- %s", type_to_string(dst), type_to_string(src)); - + Type *elem = base_array_type(dst); lbValue e = lb_emit_conv(p, value, elem); lbAddr v = lb_add_local_generated(p, t, false); @@ -2056,16 +2067,16 @@ lbValue lb_emit_conv(lbProcedure *p, lbValue value, Type *t) { lbValue ptr = lb_emit_matrix_epi(p, v.addr, j, j); lb_emit_store(p, ptr, e); } - - + + return lb_addr_load(p, v); } - + if (is_type_matrix(dst) && is_type_matrix(src)) { GB_ASSERT(dst->kind == Type_Matrix); GB_ASSERT(src->kind == Type_Matrix); lbAddr v = lb_add_local_generated(p, t, true); - + if (is_matrix_square(dst) && is_matrix_square(dst)) { for (i64 j = 0; j < dst->Matrix.column_count; j++) { for (i64 i = 0; i < dst->Matrix.row_count; i++) { @@ -2084,15 +2095,15 @@ lbValue lb_emit_conv(lbProcedure *p, lbValue value, Type *t) { i64 dst_count = dst->Matrix.row_count*dst->Matrix.column_count; i64 src_count = src->Matrix.row_count*src->Matrix.column_count; GB_ASSERT(dst_count == src_count); - + lbValue pdst = v.addr; lbValue psrc = lb_address_from_load_or_generate_local(p, value); - + bool same_elem_base_types = are_types_identical( base_type(dst->Matrix.elem), base_type(src->Matrix.elem) ); - + if (same_elem_base_types && type_size_of(dst) == type_size_of(src)) { lb_mem_copy_overlapping(p, v.addr, psrc, lb_const_int(p->module, t_int, type_size_of(dst))); } else { @@ -2106,9 +2117,9 @@ lbValue lb_emit_conv(lbProcedure *p, lbValue value, Type *t) { } } return lb_addr_load(p, v); - } - - + } + + if (is_type_any(dst)) { if (is_type_untyped_nil(src)) { @@ -2261,6 +2272,9 @@ lbValue lb_emit_comp(lbProcedure *p, TokenKind op_kind, lbValue left, lbValue ri } } + a = core_type(left.type); + b = core_type(right.type); + if (is_type_matrix(a) && (op_kind == Token_CmpEq || op_kind == Token_NotEq)) { Type *tl = base_type(a); lbValue lhs = lb_address_from_load_or_generate_local(p, left); @@ -2712,7 +2726,7 @@ lbValue lb_emit_comp_against_nil(lbProcedure *p, TokenKind op_kind, lbValue x) { unsigned indices[2] = {0, 0}; lbValue hashes_data = lb_emit_struct_ep(p, map_ptr, 0); lbValue hashes_data_ptr_ptr = lb_emit_struct_ep(p, hashes_data, 0); - LLVMValueRef hashes_data_ptr = LLVMBuildLoad(p->builder, hashes_data_ptr_ptr.value, ""); + LLVMValueRef hashes_data_ptr = LLVMBuildLoad2(p->builder, llvm_addr_type(p->module, hashes_data_ptr_ptr), hashes_data_ptr_ptr.value, ""); if (op_kind == Token_CmpEq) { res.value = LLVMBuildIsNull(p->builder, hashes_data_ptr, ""); @@ -2791,7 +2805,15 @@ lbValue lb_emit_comp_against_nil(lbProcedure *p, TokenKind op_kind, lbValue x) { return {}; } +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); + lbValue idx = lb_emit_struct_ep(p, v.addr, 1); + lb_emit_store(p, ptr, addr); + lb_emit_store(p, idx, index); + return lb_addr_load(p, v); +} lbValue lb_build_unary_and(lbProcedure *p, Ast *expr) { ast_node(ue, UnaryExpr, expr); @@ -2830,7 +2852,17 @@ lbValue lb_build_unary_and(lbProcedure *p, Ast *expr) { lb_emit_store(p, gep1, ok); return lb_addr_load(p, res); - } if (ue_expr->kind == Ast_CompoundLit) { + } else if (is_type_soa_pointer(tv.type)) { + ast_node(ie, IndexExpr, ue_expr); + lbValue addr = lb_build_addr_ptr(p, ie->expr); + lbValue index = lb_build_expr(p, ie->index); + + if (!build_context.no_bounds_check) { + // TODO(bill): soa bounds checking + } + + return lb_make_soa_pointer(p, tv.type, addr, index); + } else if (ue_expr->kind == Ast_CompoundLit) { lbValue v = lb_build_expr(p, ue->expr); Type *type = v.type; @@ -2993,9 +3025,8 @@ lbValue lb_build_unary_and(lbProcedure *p, Ast *expr) { return lb_build_addr_ptr(p, ue->expr); } +lbValue lb_build_expr_internal(lbProcedure *p, Ast *expr); lbValue lb_build_expr(lbProcedure *p, Ast *expr) { - lbModule *m = p->module; - u16 prev_state_flags = p->state_flags; defer (p->state_flags = prev_state_flags); @@ -3022,6 +3053,38 @@ lbValue lb_build_expr(lbProcedure *p, Ast *expr) { p->state_flags = out; } + + // IMPORTANT NOTE(bill): + // Selector Call Expressions (foo->bar(...)) + // must only evaluate `foo` once as it gets transformed into + // `foo.bar(foo, ...)` + // And if `foo` is a procedure call or something more complex, storing the value + // once is a very good idea + // If a stored value is found, it must be removed from the cache + if (expr->state_flags & StateFlag_SelectorCallExpr) { + lbValue *pp = map_get(&p->selector_values, expr); + if (pp != nullptr) { + lbValue res = *pp; + map_remove(&p->selector_values, expr); + return res; + } + lbAddr *pa = map_get(&p->selector_addr, expr); + if (pa != nullptr) { + lbAddr res = *pa; + map_remove(&p->selector_addr, expr); + return lb_addr_load(p, res); + } + } + lbValue res = lb_build_expr_internal(p, expr); + if (expr->state_flags & StateFlag_SelectorCallExpr) { + map_set(&p->selector_values, expr, res); + } + return res; +} + +lbValue lb_build_expr_internal(lbProcedure *p, Ast *expr) { + lbModule *m = p->module; + expr = unparen_expr(expr); TokenPos expr_pos = ast_token(expr).pos; @@ -3040,17 +3103,6 @@ lbValue lb_build_expr(lbProcedure *p, Ast *expr) { return lb_const_value(p->module, type, tv.value); } - #if 0 - LLVMMetadataRef prev_debug_location = nullptr; - if (p->debug_info != nullptr) { - prev_debug_location = LLVMGetCurrentDebugLocation2(p->builder); - LLVMSetCurrentDebugLocation2(p->builder, lb_debug_location_from_ast(p, expr)); - } - defer (if (prev_debug_location != nullptr) { - LLVMSetCurrentDebugLocation2(p->builder, prev_debug_location); - }); - #endif - switch (expr->kind) { case_ast_node(bl, BasicLit, expr); TokenPos pos = bl->token.pos; @@ -3119,14 +3171,7 @@ lbValue lb_build_expr(lbProcedure *p, Ast *expr) { case_ast_node(se, SelectorCallExpr, expr); GB_ASSERT(se->modified_call); - TypeAndValue tav = type_and_value_of_expr(expr); - GB_ASSERT(tav.mode != Addressing_Invalid); - lbValue res = lb_build_call_expr(p, se->call); - - ast_node(ce, CallExpr, se->call); - ce->sce_temp_data = gb_alloc_copy(permanent_allocator(), &res, gb_size_of(res)); - - return res; + return lb_build_call_expr(p, se->call); case_end; case_ast_node(te, TernaryIfExpr, expr); @@ -3142,19 +3187,27 @@ lbValue lb_build_expr(lbProcedure *p, Ast *expr) { lb_start_block(p, then); Type *type = default_type(type_of_expr(expr)); + LLVMTypeRef llvm_type = lb_type(p->module, type); incoming_values[0] = lb_emit_conv(p, lb_build_expr(p, te->x), type).value; + if (is_type_internally_pointer_like(type)) { + incoming_values[0] = LLVMBuildBitCast(p->builder, incoming_values[0], llvm_type, ""); + } lb_emit_jump(p, done); lb_start_block(p, else_); incoming_values[1] = lb_emit_conv(p, lb_build_expr(p, te->y), type).value; + if (is_type_internally_pointer_like(type)) { + incoming_values[1] = LLVMBuildBitCast(p->builder, incoming_values[1], llvm_type, ""); + } + lb_emit_jump(p, done); lb_start_block(p, done); lbValue res = {}; - res.value = LLVMBuildPhi(p->builder, lb_type(p->module, type), ""); + res.value = LLVMBuildPhi(p->builder, llvm_type, ""); res.type = type; GB_ASSERT(p->curr_block->preds.count >= 2); @@ -3292,7 +3345,7 @@ lbValue lb_build_expr(lbProcedure *p, Ast *expr) { default: GB_PANIC("Unhandled inline asm dialect"); break; } - LLVMTypeRef func_type = LLVMGetElementType(lb_type(p->module, t)); + LLVMTypeRef func_type = lb_type_internal_for_procedures_raw(p->module, t); LLVMValueRef the_asm = llvm_get_inline_asm(func_type, asm_string, constraints_string, ia->has_side_effects, ia->has_side_effects, dialect); GB_ASSERT(the_asm != nullptr); return {the_asm, t}; @@ -3412,9 +3465,929 @@ lbAddr lb_build_array_swizzle_addr(lbProcedure *p, AstCallExpr *ce, TypeAndValue } +lbAddr lb_build_addr_internal(lbProcedure *p, Ast *expr); lbAddr lb_build_addr(lbProcedure *p, Ast *expr) { expr = unparen_expr(expr); + // IMPORTANT NOTE(bill): + // Selector Call Expressions (foo->bar(...)) + // must only evaluate `foo` once as it gets transformed into + // `foo.bar(foo, ...)` + // And if `foo` is a procedure call or something more complex, storing the value + // once is a very good idea + // If a stored value is found, it must be removed from the cache + if (expr->state_flags & StateFlag_SelectorCallExpr) { + lbAddr *pp = map_get(&p->selector_addr, expr); + if (pp != nullptr) { + lbAddr res = *pp; + map_remove(&p->selector_addr, expr); + return res; + } + } + lbAddr addr = lb_build_addr_internal(p, expr); + if (expr->state_flags & StateFlag_SelectorCallExpr) { + map_set(&p->selector_addr, expr, addr); + } + return addr; +} + +void lb_build_addr_compound_lit_populate(lbProcedure *p, Slice const &elems, Array *temp_data, Type *compound_type) { + Type *bt = base_type(compound_type); + Type *et = nullptr; + switch (bt->kind) { + case Type_Array: et = bt->Array.elem; break; + case Type_EnumeratedArray: et = bt->EnumeratedArray.elem; break; + case Type_Slice: et = bt->Slice.elem; break; + case Type_BitSet: et = bt->BitSet.elem; break; + case Type_DynamicArray: et = bt->DynamicArray.elem; break; + case Type_SimdVector: et = bt->SimdVector.elem; break; + case Type_Matrix: et = bt->Matrix.elem; break; + } + GB_ASSERT(et != nullptr); + + + // NOTE(bill): Separate value, gep, store into their own chunks + for_array(i, elems) { + Ast *elem = elems[i]; + if (elem->kind == Ast_FieldValue) { + ast_node(fv, FieldValue, elem); + if (lb_is_elem_const(fv->value, et)) { + continue; + } + if (is_ast_range(fv->field)) { + ast_node(ie, BinaryExpr, fv->field); + TypeAndValue lo_tav = ie->left->tav; + TypeAndValue hi_tav = ie->right->tav; + GB_ASSERT(lo_tav.mode == Addressing_Constant); + GB_ASSERT(hi_tav.mode == Addressing_Constant); + + TokenKind op = ie->op.kind; + i64 lo = exact_value_to_i64(lo_tav.value); + i64 hi = exact_value_to_i64(hi_tav.value); + if (op != Token_RangeHalf) { + hi += 1; + } + + lbValue value = lb_emit_conv(p, lb_build_expr(p, fv->value), et); + + GB_ASSERT((hi-lo) > 0); + + if (bt->kind == Type_Matrix) { + for (i64 k = lo; k < hi; k++) { + lbCompoundLitElemTempData data = {}; + data.value = value; + + data.elem_index = matrix_row_major_index_to_offset(bt, k); + array_add(temp_data, data); + } + } else { + enum {MAX_ELEMENT_AMOUNT = 32}; + if ((hi-lo) <= MAX_ELEMENT_AMOUNT) { + for (i64 k = lo; k < hi; k++) { + lbCompoundLitElemTempData data = {}; + data.value = value; + data.elem_index = k; + array_add(temp_data, data); + } + } else { + lbCompoundLitElemTempData data = {}; + data.value = value; + data.elem_index = lo; + data.elem_length = hi-lo; + array_add(temp_data, data); + } + } + } else { + auto tav = fv->field->tav; + GB_ASSERT(tav.mode == Addressing_Constant); + i64 index = exact_value_to_i64(tav.value); + + lbValue value = lb_emit_conv(p, lb_build_expr(p, fv->value), et); + GB_ASSERT(!is_type_tuple(value.type)); + + lbCompoundLitElemTempData data = {}; + data.value = value; + data.expr = fv->value; + if (bt->kind == Type_Matrix) { + data.elem_index = matrix_row_major_index_to_offset(bt, index); + } else { + data.elem_index = index; + } + array_add(temp_data, data); + } + + } else { + if (lb_is_elem_const(elem, et)) { + continue; + } + + lbValue field_expr = lb_build_expr(p, elem); + GB_ASSERT(!is_type_tuple(field_expr.type)); + + lbValue ev = lb_emit_conv(p, field_expr, et); + + lbCompoundLitElemTempData data = {}; + data.value = ev; + if (bt->kind == Type_Matrix) { + data.elem_index = matrix_row_major_index_to_offset(bt, i); + } else { + data.elem_index = i; + } + array_add(temp_data, data); + } + } +} +void lb_build_addr_compound_lit_assign_array(lbProcedure *p, Array const &temp_data) { + for_array(i, temp_data) { + auto td = temp_data[i]; + if (td.value.value != nullptr) { + if (td.elem_length > 0) { + auto loop_data = lb_loop_start(p, cast(isize)td.elem_length, t_i32); + { + lbValue dst = td.gep; + dst = lb_emit_ptr_offset(p, dst, loop_data.idx); + lb_emit_store(p, dst, td.value); + } + lb_loop_end(p, loop_data); + } else { + lb_emit_store(p, td.gep, td.value); + } + } + } +} + +lbAddr lb_build_addr_index_expr(lbProcedure *p, Ast *expr) { + ast_node(ie, IndexExpr, expr); + + Type *t = base_type(type_of_expr(ie->expr)); + + bool deref = is_type_pointer(t); + t = base_type(type_deref(t)); + if (is_type_soa_struct(t)) { + // SOA STRUCTURES!!!! + lbValue val = lb_build_addr_ptr(p, ie->expr); + if (deref) { + val = lb_emit_load(p, val); + } + + lbValue index = lb_build_expr(p, ie->index); + return lb_addr_soa_variable(val, index, ie->index); + } + + if (ie->expr->tav.mode == Addressing_SoaVariable) { + // SOA Structures for slices/dynamic arrays + GB_ASSERT(is_type_pointer(type_of_expr(ie->expr))); + + lbValue field = lb_build_expr(p, ie->expr); + lbValue index = lb_build_expr(p, ie->index); + + + if (!build_context.no_bounds_check) { + // TODO HACK(bill): Clean up this hack to get the length for bounds checking + // GB_ASSERT(LLVMIsALoadInst(field.value)); + + // lbValue a = {}; + // a.value = LLVMGetOperand(field.value, 0); + // a.type = alloc_type_pointer(field.type); + + // irInstr *b = &a->Instr; + // GB_ASSERT(b->kind == irInstr_StructElementPtr); + // lbValue base_struct = b->StructElementPtr.address; + + // GB_ASSERT(is_type_soa_struct(type_deref(ir_type(base_struct)))); + // lbValue len = ir_soa_struct_len(p, base_struct); + // lb_emit_bounds_check(p, ast_token(ie->index), index, len); + } + lbValue val = lb_emit_ptr_offset(p, field, index); + return lb_addr(val); + } + + GB_ASSERT_MSG(is_type_indexable(t), "%s %s", type_to_string(t), expr_to_string(expr)); + + if (is_type_map(t)) { + lbAddr map_addr = lb_build_addr(p, ie->expr); + lbValue map_val = lb_addr_load(p, map_addr); + if (deref) { + map_val = lb_emit_load(p, map_val); + } + + lbValue key = lb_build_expr(p, ie->index); + key = lb_emit_conv(p, key, t->Map.key); + + Type *result_type = type_of_expr(expr); + lbValue map_ptr = lb_address_from_load_or_generate_local(p, map_val); + return lb_addr_map(map_ptr, key, t, result_type); + } + + switch (t->kind) { + case Type_Array: { + lbValue array = {}; + array = lb_build_addr_ptr(p, ie->expr); + if (deref) { + array = lb_emit_load(p, array); + } + lbValue index = lb_build_expr(p, ie->index); + index = lb_emit_conv(p, index, t_int); + lbValue elem = lb_emit_array_ep(p, array, index); + + auto index_tv = type_and_value_of_expr(ie->index); + if (index_tv.mode != Addressing_Constant) { + lbValue len = lb_const_int(p->module, t_int, t->Array.count); + lb_emit_bounds_check(p, ast_token(ie->index), index, len); + } + return lb_addr(elem); + } + + case Type_EnumeratedArray: { + lbValue array = {}; + array = lb_build_addr_ptr(p, ie->expr); + if (deref) { + array = lb_emit_load(p, array); + } + + Type *index_type = t->EnumeratedArray.index; + + auto index_tv = type_and_value_of_expr(ie->index); + + lbValue index = {}; + if (compare_exact_values(Token_NotEq, *t->EnumeratedArray.min_value, exact_value_i64(0))) { + if (index_tv.mode == Addressing_Constant) { + ExactValue idx = exact_value_sub(index_tv.value, *t->EnumeratedArray.min_value); + index = lb_const_value(p->module, index_type, idx); + } else { + index = lb_emit_conv(p, lb_build_expr(p, ie->index), t_int); + index = lb_emit_arith(p, Token_Sub, index, lb_const_value(p->module, index_type, *t->EnumeratedArray.min_value), index_type); + } + } else { + index = lb_emit_conv(p, lb_build_expr(p, ie->index), t_int); + } + + lbValue elem = lb_emit_array_ep(p, array, index); + + if (index_tv.mode != Addressing_Constant) { + lbValue len = lb_const_int(p->module, t_int, t->EnumeratedArray.count); + lb_emit_bounds_check(p, ast_token(ie->index), index, len); + } + return lb_addr(elem); + } + + case Type_Slice: { + lbValue slice = {}; + slice = lb_build_expr(p, ie->expr); + if (deref) { + slice = lb_emit_load(p, slice); + } + lbValue elem = lb_slice_elem(p, slice); + lbValue index = lb_emit_conv(p, lb_build_expr(p, ie->index), t_int); + lbValue len = lb_slice_len(p, slice); + lb_emit_bounds_check(p, ast_token(ie->index), index, len); + lbValue v = lb_emit_ptr_offset(p, elem, index); + return lb_addr(v); + } + + case Type_MultiPointer: { + lbValue multi_ptr = {}; + multi_ptr = lb_build_expr(p, ie->expr); + if (deref) { + multi_ptr = lb_emit_load(p, multi_ptr); + } + lbValue index = lb_build_expr(p, ie->index); + lbValue v = {}; + + LLVMValueRef indices[1] = {index.value}; + v.value = LLVMBuildGEP2(p->builder, lb_type(p->module, t->MultiPointer.elem), multi_ptr.value, indices, 1, "foo"); + v.type = alloc_type_pointer(t->MultiPointer.elem); + return lb_addr(v); + } + + case Type_RelativeSlice: { + lbAddr slice_addr = {}; + if (deref) { + slice_addr = lb_addr(lb_build_expr(p, ie->expr)); + } else { + slice_addr = lb_build_addr(p, ie->expr); + } + lbValue slice = lb_addr_load(p, slice_addr); + + lbValue elem = lb_slice_elem(p, slice); + lbValue index = lb_emit_conv(p, lb_build_expr(p, ie->index), t_int); + lbValue len = lb_slice_len(p, slice); + lb_emit_bounds_check(p, ast_token(ie->index), index, len); + lbValue v = lb_emit_ptr_offset(p, elem, index); + return lb_addr(v); + } + + case Type_DynamicArray: { + lbValue dynamic_array = {}; + dynamic_array = lb_build_expr(p, ie->expr); + if (deref) { + dynamic_array = lb_emit_load(p, dynamic_array); + } + lbValue elem = lb_dynamic_array_elem(p, dynamic_array); + lbValue len = lb_dynamic_array_len(p, dynamic_array); + lbValue index = lb_emit_conv(p, lb_build_expr(p, ie->index), t_int); + lb_emit_bounds_check(p, ast_token(ie->index), index, len); + lbValue v = lb_emit_ptr_offset(p, elem, index); + return lb_addr(v); + } + + case Type_Matrix: { + lbValue matrix = {}; + matrix = lb_build_addr_ptr(p, ie->expr); + if (deref) { + matrix = lb_emit_load(p, matrix); + } + lbValue index = lb_build_expr(p, ie->index); + index = lb_emit_conv(p, index, t_int); + lbValue elem = lb_emit_matrix_ep(p, matrix, lb_const_int(p->module, t_int, 0), index); + elem = lb_emit_conv(p, elem, alloc_type_pointer(type_of_expr(expr))); + + auto index_tv = type_and_value_of_expr(ie->index); + if (index_tv.mode != Addressing_Constant) { + lbValue len = lb_const_int(p->module, t_int, t->Matrix.column_count); + lb_emit_bounds_check(p, ast_token(ie->index), index, len); + } + return lb_addr(elem); + } + + + case Type_Basic: { // Basic_string + lbValue str; + lbValue elem; + lbValue len; + lbValue index; + + str = lb_build_expr(p, ie->expr); + if (deref) { + str = lb_emit_load(p, str); + } + elem = lb_string_elem(p, str); + len = lb_string_len(p, str); + + index = lb_emit_conv(p, lb_build_expr(p, ie->index), t_int); + lb_emit_bounds_check(p, ast_token(ie->index), index, len); + + return lb_addr(lb_emit_ptr_offset(p, elem, index)); + } + } + return {}; +} + + +lbAddr lb_build_addr_slice_expr(lbProcedure *p, Ast *expr) { + ast_node(se, SliceExpr, expr); + + lbValue low = lb_const_int(p->module, t_int, 0); + lbValue high = {}; + + if (se->low != nullptr) { + low = lb_correct_endianness(p, lb_build_expr(p, se->low)); + } + if (se->high != nullptr) { + high = lb_correct_endianness(p, lb_build_expr(p, se->high)); + } + + bool no_indices = se->low == nullptr && se->high == nullptr; + + lbAddr addr = lb_build_addr(p, se->expr); + lbValue base = lb_addr_load(p, addr); + Type *type = base_type(base.type); + + if (is_type_pointer(type)) { + type = base_type(type_deref(type)); + addr = lb_addr(base); + base = lb_addr_load(p, addr); + } + + switch (type->kind) { + case Type_Slice: { + Type *slice_type = type; + lbValue len = lb_slice_len(p, base); + if (high.value == nullptr) high = len; + + if (!no_indices) { + lb_emit_slice_bounds_check(p, se->open, low, high, len, se->low != nullptr); + } + + lbValue elem = lb_emit_ptr_offset(p, lb_slice_elem(p, base), low); + lbValue new_len = lb_emit_arith(p, Token_Sub, high, low, t_int); + + lbAddr slice = lb_add_local_generated(p, slice_type, false); + lb_fill_slice(p, slice, elem, new_len); + return slice; + } + + case Type_RelativeSlice: + GB_PANIC("TODO(bill): Type_RelativeSlice 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); + + lbValue len = lb_dynamic_array_len(p, base); + if (high.value == nullptr) high = len; + + if (!no_indices) { + lb_emit_slice_bounds_check(p, se->open, low, high, len, se->low != nullptr); + } + + lbValue elem = lb_emit_ptr_offset(p, lb_dynamic_array_elem(p, base), low); + lbValue new_len = lb_emit_arith(p, Token_Sub, high, low, t_int); + + lbAddr slice = lb_add_local_generated(p, slice_type, false); + lb_fill_slice(p, slice, elem, new_len); + return slice; + } + + case Type_MultiPointer: { + lbAddr res = lb_add_local_generated(p, type_of_expr(expr), false); + if (se->high == nullptr) { + lbValue offset = base; + LLVMValueRef indices[1] = {low.value}; + offset.value = LLVMBuildGEP2(p->builder, lb_type(p->module, offset.type->MultiPointer.elem), offset.value, indices, 1, ""); + lb_addr_store(p, res, offset); + } else { + low = lb_emit_conv(p, low, t_int); + high = lb_emit_conv(p, high, t_int); + + lb_emit_multi_pointer_slice_bounds_check(p, se->open, low, high); + + LLVMValueRef indices[1] = {low.value}; + LLVMValueRef ptr = LLVMBuildGEP2(p->builder, lb_type(p->module, base.type->MultiPointer.elem), base.value, indices, 1, ""); + LLVMValueRef len = LLVMBuildSub(p->builder, high.value, low.value, ""); + + LLVMValueRef gep0 = lb_emit_struct_ep(p, res.addr, 0).value; + LLVMValueRef gep1 = lb_emit_struct_ep(p, res.addr, 1).value; + LLVMBuildStore(p->builder, ptr, gep0); + LLVMBuildStore(p->builder, len, gep1); + } + return res; + } + + case Type_Array: { + Type *slice_type = alloc_type_slice(type->Array.elem); + lbValue len = lb_const_int(p->module, t_int, type->Array.count); + + if (high.value == nullptr) high = len; + + bool low_const = type_and_value_of_expr(se->low).mode == Addressing_Constant; + bool high_const = type_and_value_of_expr(se->high).mode == Addressing_Constant; + + if (!low_const || !high_const) { + if (!no_indices) { + lb_emit_slice_bounds_check(p, se->open, low, high, len, se->low != nullptr); + } + } + lbValue elem = lb_emit_ptr_offset(p, lb_array_elem(p, lb_addr_get_ptr(p, addr)), low); + lbValue new_len = lb_emit_arith(p, Token_Sub, high, low, t_int); + + lbAddr slice = lb_add_local_generated(p, slice_type, false); + lb_fill_slice(p, slice, elem, new_len); + return slice; + } + + case Type_Basic: { + GB_ASSERT(type == t_string); + lbValue len = lb_string_len(p, base); + if (high.value == nullptr) high = len; + + if (!no_indices) { + lb_emit_slice_bounds_check(p, se->open, low, high, len, se->low != nullptr); + } + + lbValue elem = lb_emit_ptr_offset(p, lb_string_elem(p, base), low); + lbValue new_len = lb_emit_arith(p, Token_Sub, high, low, t_int); + + lbAddr str = lb_add_local_generated(p, t_string, false); + lb_fill_string(p, str, elem, new_len); + return str; + } + + + case Type_Struct: + if (is_type_soa_struct(type)) { + lbValue len = lb_soa_struct_len(p, lb_addr_get_ptr(p, addr)); + if (high.value == nullptr) high = len; + + if (!no_indices) { + lb_emit_slice_bounds_check(p, se->open, low, high, len, se->low != nullptr); + } + #if 1 + + lbAddr dst = lb_add_local_generated(p, type_of_expr(expr), true); + if (type->Struct.soa_kind == StructSoa_Fixed) { + i32 field_count = cast(i32)type->Struct.fields.count; + for (i32 i = 0; i < field_count; i++) { + lbValue field_dst = lb_emit_struct_ep(p, dst.addr, i); + lbValue field_src = lb_emit_struct_ep(p, lb_addr_get_ptr(p, addr), i); + field_src = lb_emit_array_ep(p, field_src, low); + lb_emit_store(p, field_dst, field_src); + } + + lbValue len_dst = lb_emit_struct_ep(p, dst.addr, field_count); + lbValue new_len = lb_emit_arith(p, Token_Sub, high, low, t_int); + lb_emit_store(p, len_dst, new_len); + } else if (type->Struct.soa_kind == StructSoa_Slice) { + if (no_indices) { + lb_addr_store(p, dst, base); + } else { + i32 field_count = cast(i32)type->Struct.fields.count - 1; + for (i32 i = 0; i < field_count; i++) { + lbValue field_dst = lb_emit_struct_ep(p, dst.addr, i); + lbValue field_src = lb_emit_struct_ev(p, base, i); + field_src = lb_emit_ptr_offset(p, field_src, low); + lb_emit_store(p, field_dst, field_src); + } + + + lbValue len_dst = lb_emit_struct_ep(p, dst.addr, field_count); + lbValue new_len = lb_emit_arith(p, Token_Sub, high, low, t_int); + lb_emit_store(p, len_dst, new_len); + } + } else if (type->Struct.soa_kind == StructSoa_Dynamic) { + i32 field_count = cast(i32)type->Struct.fields.count - 3; + for (i32 i = 0; i < field_count; i++) { + lbValue field_dst = lb_emit_struct_ep(p, dst.addr, i); + lbValue field_src = lb_emit_struct_ev(p, base, i); + field_src = lb_emit_ptr_offset(p, field_src, low); + lb_emit_store(p, field_dst, field_src); + } + + + lbValue len_dst = lb_emit_struct_ep(p, dst.addr, field_count); + lbValue new_len = lb_emit_arith(p, Token_Sub, high, low, t_int); + lb_emit_store(p, len_dst, new_len); + } + + return dst; + #endif + } + break; + + } + + GB_PANIC("Unknown slicable type"); + return {}; +} + + +lbAddr lb_build_addr_compound_lit(lbProcedure *p, Ast *expr) { + ast_node(cl, CompoundLit, expr); + + Type *type = type_of_expr(expr); + Type *bt = base_type(type); + + lbAddr v = lb_add_local_generated(p, type, true); + + Type *et = nullptr; + switch (bt->kind) { + case Type_Array: et = bt->Array.elem; break; + case Type_EnumeratedArray: et = bt->EnumeratedArray.elem; break; + case Type_Slice: et = bt->Slice.elem; break; + case Type_BitSet: et = bt->BitSet.elem; break; + case Type_SimdVector: et = bt->SimdVector.elem; break; + case Type_Matrix: et = bt->Matrix.elem; break; + } + + String proc_name = {}; + if (p->entity) { + proc_name = p->entity->token.string; + } + TokenPos pos = ast_token(expr).pos; + + switch (bt->kind) { + default: GB_PANIC("Unknown CompoundLit type: %s", type_to_string(type)); break; + + case Type_Struct: { + // TODO(bill): "constant" '#raw_union's are not initialized constantly at the moment. + // NOTE(bill): This is due to the layout of the unions when printed to LLVM-IR + bool is_raw_union = is_type_raw_union(bt); + GB_ASSERT(is_type_struct(bt) || is_raw_union); + TypeStruct *st = &bt->Struct; + if (cl->elems.count > 0) { + lb_addr_store(p, v, lb_const_value(p->module, type, exact_value_compound(expr))); + lbValue comp_lit_ptr = lb_addr_get_ptr(p, v); + + for_array(field_index, cl->elems) { + Ast *elem = cl->elems[field_index]; + + lbValue field_expr = {}; + Entity *field = nullptr; + isize index = field_index; + + if (elem->kind == Ast_FieldValue) { + ast_node(fv, FieldValue, elem); + String name = fv->field->Ident.token.string; + Selection sel = lookup_field(bt, name, false); + index = sel.index[0]; + elem = fv->value; + TypeAndValue tav = type_and_value_of_expr(elem); + } else { + TypeAndValue tav = type_and_value_of_expr(elem); + Selection sel = lookup_field_from_index(bt, st->fields[field_index]->Variable.field_index); + index = sel.index[0]; + } + + field = st->fields[index]; + Type *ft = field->type; + if (!is_raw_union && !is_type_typeid(ft) && lb_is_elem_const(elem, ft)) { + continue; + } + + field_expr = lb_build_expr(p, elem); + + lbValue gep = {}; + if (is_raw_union) { + gep = lb_emit_conv(p, comp_lit_ptr, alloc_type_pointer(ft)); + } else { + gep = lb_emit_struct_ep(p, comp_lit_ptr, cast(i32)index); + } + + Type *fet = field_expr.type; + GB_ASSERT(fet->kind != Type_Tuple); + + // HACK TODO(bill): THIS IS A MASSIVE HACK!!!! + if (is_type_union(ft) && !are_types_identical(fet, ft) && !is_type_untyped(fet)) { + GB_ASSERT_MSG(union_variant_index(ft, fet) > 0, "%s", type_to_string(fet)); + + lb_emit_store_union_variant(p, gep, field_expr, fet); + } else { + lbValue fv = lb_emit_conv(p, field_expr, ft); + lb_emit_store(p, gep, fv); + } + } + } + break; + } + + case Type_Map: { + if (cl->elems.count == 0) { + break; + } + GB_ASSERT(!build_context.no_dynamic_literals); + { + auto args = array_make(permanent_allocator(), 3); + args[0] = lb_gen_map_header(p, v.addr, type); + args[1] = lb_const_int(p->module, t_int, 2*cl->elems.count); + args[2] = lb_emit_source_code_location(p, proc_name, pos); + lb_emit_runtime_call(p, "__dynamic_map_reserve", args); + } + for_array(field_index, cl->elems) { + Ast *elem = cl->elems[field_index]; + ast_node(fv, FieldValue, elem); + + lbValue key = lb_build_expr(p, fv->field); + lbValue value = lb_build_expr(p, fv->value); + lb_insert_dynamic_map_key_and_value(p, v, type, key, value, elem); + } + break; + } + + case Type_Array: { + if (cl->elems.count > 0) { + lb_addr_store(p, v, lb_const_value(p->module, type, exact_value_compound(expr))); + + auto temp_data = array_make(temporary_allocator(), 0, cl->elems.count); + + lb_build_addr_compound_lit_populate(p, cl->elems, &temp_data, type); + + lbValue dst_ptr = lb_addr_get_ptr(p, v); + for_array(i, temp_data) { + i32 index = cast(i32)(temp_data[i].elem_index); + temp_data[i].gep = lb_emit_array_epi(p, dst_ptr, index); + } + + lb_build_addr_compound_lit_assign_array(p, temp_data); + } + break; + } + case Type_EnumeratedArray: { + if (cl->elems.count > 0) { + lb_addr_store(p, v, lb_const_value(p->module, type, exact_value_compound(expr))); + + auto temp_data = array_make(temporary_allocator(), 0, cl->elems.count); + + lb_build_addr_compound_lit_populate(p, cl->elems, &temp_data, type); + + lbValue dst_ptr = lb_addr_get_ptr(p, v); + i64 index_offset = exact_value_to_i64(*bt->EnumeratedArray.min_value); + for_array(i, temp_data) { + i32 index = cast(i32)(temp_data[i].elem_index - index_offset); + temp_data[i].gep = lb_emit_array_epi(p, dst_ptr, index); + } + + lb_build_addr_compound_lit_assign_array(p, temp_data); + } + break; + } + case Type_Slice: { + if (cl->elems.count > 0) { + lbValue slice = lb_const_value(p->module, type, exact_value_compound(expr)); + + lbValue data = lb_slice_elem(p, slice); + + auto temp_data = array_make(temporary_allocator(), 0, cl->elems.count); + + lb_build_addr_compound_lit_populate(p, cl->elems, &temp_data, type); + + for_array(i, temp_data) { + temp_data[i].gep = lb_emit_ptr_offset(p, data, lb_const_int(p->module, t_int, temp_data[i].elem_index)); + } + + lb_build_addr_compound_lit_assign_array(p, temp_data); + + { + lbValue count = {}; + count.type = t_int; + + if (lb_is_const(slice)) { + unsigned indices[1] = {1}; + count.value = LLVMConstExtractValue(slice.value, indices, gb_count_of(indices)); + } else { + count.value = LLVMBuildExtractValue(p->builder, slice.value, 1, ""); + } + lb_fill_slice(p, v, data, count); + } + } + break; + } + + case Type_DynamicArray: { + if (cl->elems.count == 0) { + break; + } + GB_ASSERT(!build_context.no_dynamic_literals); + + Type *et = bt->DynamicArray.elem; + lbValue size = lb_const_int(p->module, t_int, type_size_of(et)); + lbValue align = lb_const_int(p->module, t_int, type_align_of(et)); + + i64 item_count = gb_max(cl->max_count, cl->elems.count); + { + + auto args = array_make(permanent_allocator(), 5); + args[0] = lb_emit_conv(p, lb_addr_get_ptr(p, v), t_rawptr); + args[1] = size; + args[2] = align; + args[3] = lb_const_int(p->module, t_int, item_count); + args[4] = lb_emit_source_code_location(p, proc_name, pos); + lb_emit_runtime_call(p, "__dynamic_array_reserve", args); + } + + lbValue items = lb_generate_local_array(p, et, item_count); + + auto temp_data = array_make(temporary_allocator(), 0, cl->elems.count); + lb_build_addr_compound_lit_populate(p, cl->elems, &temp_data, type); + + for_array(i, temp_data) { + temp_data[i].gep = lb_emit_array_epi(p, items, temp_data[i].elem_index); + } + lb_build_addr_compound_lit_assign_array(p, temp_data); + + { + auto args = array_make(permanent_allocator(), 6); + args[0] = lb_emit_conv(p, v.addr, t_rawptr); + args[1] = size; + args[2] = align; + args[3] = lb_emit_conv(p, items, t_rawptr); + args[4] = lb_const_int(p->module, t_int, item_count); + args[5] = lb_emit_source_code_location(p, proc_name, pos); + lb_emit_runtime_call(p, "__dynamic_array_append", args); + } + break; + } + + case Type_Basic: { + GB_ASSERT(is_type_any(bt)); + if (cl->elems.count > 0) { + lb_addr_store(p, v, lb_const_value(p->module, type, exact_value_compound(expr))); + String field_names[2] = { + str_lit("data"), + str_lit("id"), + }; + Type *field_types[2] = { + t_rawptr, + t_typeid, + }; + + for_array(field_index, cl->elems) { + Ast *elem = cl->elems[field_index]; + + lbValue field_expr = {}; + isize index = field_index; + + if (elem->kind == Ast_FieldValue) { + ast_node(fv, FieldValue, elem); + Selection sel = lookup_field(bt, fv->field->Ident.token.string, false); + index = sel.index[0]; + elem = fv->value; + } else { + TypeAndValue tav = type_and_value_of_expr(elem); + Selection sel = lookup_field(bt, field_names[field_index], false); + index = sel.index[0]; + } + + field_expr = lb_build_expr(p, elem); + + GB_ASSERT(field_expr.type->kind != Type_Tuple); + + Type *ft = field_types[index]; + lbValue fv = lb_emit_conv(p, field_expr, ft); + lbValue gep = lb_emit_struct_ep(p, lb_addr_get_ptr(p, v), cast(i32)index); + lb_emit_store(p, gep, fv); + } + } + + break; + } + + case Type_BitSet: { + i64 sz = type_size_of(type); + if (cl->elems.count > 0 && sz > 0) { + lb_addr_store(p, v, lb_const_value(p->module, type, exact_value_compound(expr))); + + lbValue lower = lb_const_value(p->module, t_int, exact_value_i64(bt->BitSet.lower)); + for_array(i, cl->elems) { + Ast *elem = cl->elems[i]; + GB_ASSERT(elem->kind != Ast_FieldValue); + + if (lb_is_elem_const(elem, et)) { + continue; + } + + lbValue expr = lb_build_expr(p, elem); + GB_ASSERT(expr.type->kind != Type_Tuple); + + Type *it = bit_set_to_int(bt); + lbValue one = lb_const_value(p->module, it, exact_value_i64(1)); + lbValue e = lb_emit_conv(p, expr, it); + e = lb_emit_arith(p, Token_Sub, e, lower, it); + e = lb_emit_arith(p, Token_Shl, one, e, it); + + lbValue old_value = lb_emit_transmute(p, lb_addr_load(p, v), it); + lbValue new_value = lb_emit_arith(p, Token_Or, old_value, e, it); + new_value = lb_emit_transmute(p, new_value, type); + lb_addr_store(p, v, new_value); + } + } + break; + } + + case Type_Matrix: { + if (cl->elems.count > 0) { + lb_addr_store(p, v, lb_const_value(p->module, type, exact_value_compound(expr))); + + auto temp_data = array_make(temporary_allocator(), 0, cl->elems.count); + + lb_build_addr_compound_lit_populate(p, cl->elems, &temp_data, type); + + lbValue dst_ptr = lb_addr_get_ptr(p, v); + for_array(i, temp_data) { + temp_data[i].gep = lb_emit_array_epi(p, dst_ptr, temp_data[i].elem_index); + } + + lb_build_addr_compound_lit_assign_array(p, temp_data); + } + break; + } + + case Type_SimdVector: { + if (cl->elems.count > 0) { + lbValue vector_value = lb_const_value(p->module, type, exact_value_compound(expr)); + defer (lb_addr_store(p, v, vector_value)); + + auto temp_data = array_make(temporary_allocator(), 0, cl->elems.count); + + lb_build_addr_compound_lit_populate(p, cl->elems, &temp_data, type); + + // TODO(bill): reduce the need for individual `insertelement` if a `shufflevector` + // might be a better option + for_array(i, temp_data) { + auto td = temp_data[i]; + if (td.value.value != nullptr) { + if (td.elem_length > 0) { + for (i64 k = 0; k < td.elem_length; k++) { + LLVMValueRef index = lb_const_int(p->module, t_u32, td.elem_index + k).value; + vector_value.value = LLVMBuildInsertElement(p->builder, vector_value.value, td.value.value, index, ""); + } + } else { + LLVMValueRef index = lb_const_int(p->module, t_u32, td.elem_index).value; + vector_value.value = LLVMBuildInsertElement(p->builder, vector_value.value, td.value.value, index, ""); + + } + } + } + } + break; + } + } + + return v; +} + + +lbAddr lb_build_addr_internal(lbProcedure *p, Ast *expr) { switch (expr->kind) { case_ast_node(i, Implicit, expr); lbAddr v = {}; @@ -3546,6 +4519,7 @@ lbAddr lb_build_addr(lbProcedure *p, Ast *expr) { // NOTE(bill): just patch the index in place sel.index[0] = addr.swizzle.indices[sel.index[0]]; } + lbValue a = lb_addr_get_ptr(p, addr); a = lb_emit_deep_field_gep(p, a, sel); return lb_addr(a); @@ -3556,9 +4530,6 @@ lbAddr lb_build_addr(lbProcedure *p, Ast *expr) { case_end; case_ast_node(se, SelectorCallExpr, expr); - GB_ASSERT(se->modified_call); - TypeAndValue tav = type_and_value_of_expr(expr); - GB_ASSERT(tav.mode != Addressing_Invalid); lbValue e = lb_build_expr(p, expr); return lb_addr(lb_address_from_load_or_generate_local(p, e)); case_end; @@ -3600,225 +4571,15 @@ lbAddr lb_build_addr(lbProcedure *p, Ast *expr) { case_end; case_ast_node(ie, IndexExpr, expr); - Type *t = base_type(type_of_expr(ie->expr)); - - bool deref = is_type_pointer(t); - t = base_type(type_deref(t)); - if (is_type_soa_struct(t)) { - // SOA STRUCTURES!!!! - lbValue val = lb_build_addr_ptr(p, ie->expr); - if (deref) { - val = lb_emit_load(p, val); - } - - lbValue index = lb_build_expr(p, ie->index); - return lb_addr_soa_variable(val, index, ie->index); - } - - if (ie->expr->tav.mode == Addressing_SoaVariable) { - // SOA Structures for slices/dynamic arrays - GB_ASSERT(is_type_pointer(type_of_expr(ie->expr))); - - lbValue field = lb_build_expr(p, ie->expr); - lbValue index = lb_build_expr(p, ie->index); - - - if (!build_context.no_bounds_check) { - // TODO HACK(bill): Clean up this hack to get the length for bounds checking - // GB_ASSERT(LLVMIsALoadInst(field.value)); - - // lbValue a = {}; - // a.value = LLVMGetOperand(field.value, 0); - // a.type = alloc_type_pointer(field.type); - - // irInstr *b = &a->Instr; - // GB_ASSERT(b->kind == irInstr_StructElementPtr); - // lbValue base_struct = b->StructElementPtr.address; - - // GB_ASSERT(is_type_soa_struct(type_deref(ir_type(base_struct)))); - // lbValue len = ir_soa_struct_len(p, base_struct); - // lb_emit_bounds_check(p, ast_token(ie->index), index, len); - } - lbValue val = lb_emit_ptr_offset(p, field, index); - return lb_addr(val); - } - - GB_ASSERT_MSG(is_type_indexable(t), "%s %s", type_to_string(t), expr_to_string(expr)); - - if (is_type_map(t)) { - lbAddr map_addr = lb_build_addr(p, ie->expr); - lbValue map_val = lb_addr_load(p, map_addr); - if (deref) { - map_val = lb_emit_load(p, map_val); - } - - lbValue key = lb_build_expr(p, ie->index); - key = lb_emit_conv(p, key, t->Map.key); - - Type *result_type = type_of_expr(expr); - lbValue map_ptr = lb_address_from_load_or_generate_local(p, map_val); - return lb_addr_map(map_ptr, key, t, result_type); - } - - switch (t->kind) { - case Type_Array: { - lbValue array = {}; - array = lb_build_addr_ptr(p, ie->expr); - if (deref) { - array = lb_emit_load(p, array); - } - lbValue index = lb_build_expr(p, ie->index); - index = lb_emit_conv(p, index, t_int); - lbValue elem = lb_emit_array_ep(p, array, index); - - auto index_tv = type_and_value_of_expr(ie->index); - if (index_tv.mode != Addressing_Constant) { - lbValue len = lb_const_int(p->module, t_int, t->Array.count); - lb_emit_bounds_check(p, ast_token(ie->index), index, len); - } - return lb_addr(elem); - } - - case Type_EnumeratedArray: { - lbValue array = {}; - array = lb_build_addr_ptr(p, ie->expr); - if (deref) { - array = lb_emit_load(p, array); - } - - Type *index_type = t->EnumeratedArray.index; - - auto index_tv = type_and_value_of_expr(ie->index); - - lbValue index = {}; - if (compare_exact_values(Token_NotEq, *t->EnumeratedArray.min_value, exact_value_i64(0))) { - if (index_tv.mode == Addressing_Constant) { - ExactValue idx = exact_value_sub(index_tv.value, *t->EnumeratedArray.min_value); - index = lb_const_value(p->module, index_type, idx); - } else { - index = lb_emit_conv(p, lb_build_expr(p, ie->index), t_int); - index = lb_emit_arith(p, Token_Sub, index, lb_const_value(p->module, index_type, *t->EnumeratedArray.min_value), index_type); - } - } else { - index = lb_emit_conv(p, lb_build_expr(p, ie->index), t_int); - } - - lbValue elem = lb_emit_array_ep(p, array, index); - - if (index_tv.mode != Addressing_Constant) { - lbValue len = lb_const_int(p->module, t_int, t->EnumeratedArray.count); - lb_emit_bounds_check(p, ast_token(ie->index), index, len); - } - return lb_addr(elem); - } - - case Type_Slice: { - lbValue slice = {}; - slice = lb_build_expr(p, ie->expr); - if (deref) { - slice = lb_emit_load(p, slice); - } - lbValue elem = lb_slice_elem(p, slice); - lbValue index = lb_emit_conv(p, lb_build_expr(p, ie->index), t_int); - lbValue len = lb_slice_len(p, slice); - lb_emit_bounds_check(p, ast_token(ie->index), index, len); - lbValue v = lb_emit_ptr_offset(p, elem, index); - return lb_addr(v); - } - - case Type_MultiPointer: { - lbValue multi_ptr = {}; - multi_ptr = lb_build_expr(p, ie->expr); - if (deref) { - multi_ptr = lb_emit_load(p, multi_ptr); - } - lbValue index = lb_build_expr(p, ie->index); - lbValue v = {}; - - LLVMValueRef indices[1] = {index.value}; - v.value = LLVMBuildGEP(p->builder, multi_ptr.value, indices, 1, ""); - v.type = alloc_type_pointer(t->MultiPointer.elem); - return lb_addr(v); - } - - case Type_RelativeSlice: { - lbAddr slice_addr = {}; - if (deref) { - slice_addr = lb_addr(lb_build_expr(p, ie->expr)); - } else { - slice_addr = lb_build_addr(p, ie->expr); - } - lbValue slice = lb_addr_load(p, slice_addr); - - lbValue elem = lb_slice_elem(p, slice); - lbValue index = lb_emit_conv(p, lb_build_expr(p, ie->index), t_int); - lbValue len = lb_slice_len(p, slice); - lb_emit_bounds_check(p, ast_token(ie->index), index, len); - lbValue v = lb_emit_ptr_offset(p, elem, index); - return lb_addr(v); - } - - case Type_DynamicArray: { - lbValue dynamic_array = {}; - dynamic_array = lb_build_expr(p, ie->expr); - if (deref) { - dynamic_array = lb_emit_load(p, dynamic_array); - } - lbValue elem = lb_dynamic_array_elem(p, dynamic_array); - lbValue len = lb_dynamic_array_len(p, dynamic_array); - lbValue index = lb_emit_conv(p, lb_build_expr(p, ie->index), t_int); - lb_emit_bounds_check(p, ast_token(ie->index), index, len); - lbValue v = lb_emit_ptr_offset(p, elem, index); - return lb_addr(v); - } - - case Type_Matrix: { - lbValue matrix = {}; - matrix = lb_build_addr_ptr(p, ie->expr); - if (deref) { - matrix = lb_emit_load(p, matrix); - } - lbValue index = lb_build_expr(p, ie->index); - index = lb_emit_conv(p, index, t_int); - lbValue elem = lb_emit_matrix_ep(p, matrix, lb_const_int(p->module, t_int, 0), index); - elem = lb_emit_conv(p, elem, alloc_type_pointer(type_of_expr(expr))); - - auto index_tv = type_and_value_of_expr(ie->index); - if (index_tv.mode != Addressing_Constant) { - lbValue len = lb_const_int(p->module, t_int, t->Matrix.column_count); - lb_emit_bounds_check(p, ast_token(ie->index), index, len); - } - return lb_addr(elem); - } - - - case Type_Basic: { // Basic_string - lbValue str; - lbValue elem; - lbValue len; - lbValue index; - - str = lb_build_expr(p, ie->expr); - if (deref) { - str = lb_emit_load(p, str); - } - elem = lb_string_elem(p, str); - len = lb_string_len(p, str); - - index = lb_emit_conv(p, lb_build_expr(p, ie->index), t_int); - lb_emit_bounds_check(p, ast_token(ie->index), index, len); - - return lb_addr(lb_emit_ptr_offset(p, elem, index)); - } - } + return lb_build_addr_index_expr(p, expr); case_end; - + case_ast_node(ie, MatrixIndexExpr, expr); Type *t = base_type(type_of_expr(ie->expr)); bool deref = is_type_pointer(t); t = base_type(type_deref(t)); - + lbValue m = {}; m = lb_build_addr_ptr(p, ie->expr); if (deref) { @@ -3838,210 +4599,25 @@ lbAddr lb_build_addr(lbProcedure *p, Ast *expr) { lb_emit_matrix_bounds_check(p, ast_token(ie->row_index), row_index, column_index, row_count, column_count); } return lb_addr(elem); - - + + case_end; case_ast_node(se, SliceExpr, expr); - - lbValue low = lb_const_int(p->module, t_int, 0); - lbValue high = {}; - - if (se->low != nullptr) { - low = lb_correct_endianness(p, lb_build_expr(p, se->low)); - } - if (se->high != nullptr) { - high = lb_correct_endianness(p, lb_build_expr(p, se->high)); - } - - bool no_indices = se->low == nullptr && se->high == nullptr; - - lbAddr addr = lb_build_addr(p, se->expr); - lbValue base = lb_addr_load(p, addr); - Type *type = base_type(base.type); - - if (is_type_pointer(type)) { - type = base_type(type_deref(type)); - addr = lb_addr(base); - base = lb_addr_load(p, addr); - } - - switch (type->kind) { - case Type_Slice: { - Type *slice_type = type; - lbValue len = lb_slice_len(p, base); - if (high.value == nullptr) high = len; - - if (!no_indices) { - lb_emit_slice_bounds_check(p, se->open, low, high, len, se->low != nullptr); - } - - lbValue elem = lb_emit_ptr_offset(p, lb_slice_elem(p, base), low); - lbValue new_len = lb_emit_arith(p, Token_Sub, high, low, t_int); - - lbAddr slice = lb_add_local_generated(p, slice_type, false); - lb_fill_slice(p, slice, elem, new_len); - return slice; - } - - case Type_RelativeSlice: - GB_PANIC("TODO(bill): Type_RelativeSlice 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); - - lbValue len = lb_dynamic_array_len(p, base); - if (high.value == nullptr) high = len; - - if (!no_indices) { - lb_emit_slice_bounds_check(p, se->open, low, high, len, se->low != nullptr); - } - - lbValue elem = lb_emit_ptr_offset(p, lb_dynamic_array_elem(p, base), low); - lbValue new_len = lb_emit_arith(p, Token_Sub, high, low, t_int); - - lbAddr slice = lb_add_local_generated(p, slice_type, false); - lb_fill_slice(p, slice, elem, new_len); - return slice; - } - - case Type_MultiPointer: { - lbAddr res = lb_add_local_generated(p, type_of_expr(expr), false); - if (se->high == nullptr) { - lbValue offset = base; - LLVMValueRef indices[1] = {low.value}; - offset.value = LLVMBuildGEP(p->builder, offset.value, indices, 1, ""); - lb_addr_store(p, res, offset); - } else { - low = lb_emit_conv(p, low, t_int); - high = lb_emit_conv(p, high, t_int); - - lb_emit_multi_pointer_slice_bounds_check(p, se->open, low, high); - - LLVMValueRef indices[1] = {low.value}; - LLVMValueRef ptr = LLVMBuildGEP(p->builder, base.value, indices, 1, ""); - LLVMValueRef len = LLVMBuildSub(p->builder, high.value, low.value, ""); - - LLVMValueRef gep0 = lb_emit_struct_ep(p, res.addr, 0).value; - LLVMValueRef gep1 = lb_emit_struct_ep(p, res.addr, 1).value; - LLVMBuildStore(p->builder, ptr, gep0); - LLVMBuildStore(p->builder, len, gep1); - } - return res; - } - - case Type_Array: { - Type *slice_type = alloc_type_slice(type->Array.elem); - lbValue len = lb_const_int(p->module, t_int, type->Array.count); - - if (high.value == nullptr) high = len; - - bool low_const = type_and_value_of_expr(se->low).mode == Addressing_Constant; - bool high_const = type_and_value_of_expr(se->high).mode == Addressing_Constant; - - if (!low_const || !high_const) { - if (!no_indices) { - lb_emit_slice_bounds_check(p, se->open, low, high, len, se->low != nullptr); - } - } - lbValue elem = lb_emit_ptr_offset(p, lb_array_elem(p, lb_addr_get_ptr(p, addr)), low); - lbValue new_len = lb_emit_arith(p, Token_Sub, high, low, t_int); - - lbAddr slice = lb_add_local_generated(p, slice_type, false); - lb_fill_slice(p, slice, elem, new_len); - return slice; - } - - case Type_Basic: { - GB_ASSERT(type == t_string); - lbValue len = lb_string_len(p, base); - if (high.value == nullptr) high = len; - - if (!no_indices) { - lb_emit_slice_bounds_check(p, se->open, low, high, len, se->low != nullptr); - } - - lbValue elem = lb_emit_ptr_offset(p, lb_string_elem(p, base), low); - lbValue new_len = lb_emit_arith(p, Token_Sub, high, low, t_int); - - lbAddr str = lb_add_local_generated(p, t_string, false); - lb_fill_string(p, str, elem, new_len); - return str; - } - - - case Type_Struct: - if (is_type_soa_struct(type)) { - lbValue len = lb_soa_struct_len(p, lb_addr_get_ptr(p, addr)); - if (high.value == nullptr) high = len; - - if (!no_indices) { - lb_emit_slice_bounds_check(p, se->open, low, high, len, se->low != nullptr); - } - #if 1 - - lbAddr dst = lb_add_local_generated(p, type_of_expr(expr), true); - if (type->Struct.soa_kind == StructSoa_Fixed) { - i32 field_count = cast(i32)type->Struct.fields.count; - for (i32 i = 0; i < field_count; i++) { - lbValue field_dst = lb_emit_struct_ep(p, dst.addr, i); - lbValue field_src = lb_emit_struct_ep(p, lb_addr_get_ptr(p, addr), i); - field_src = lb_emit_array_ep(p, field_src, low); - lb_emit_store(p, field_dst, field_src); - } - - lbValue len_dst = lb_emit_struct_ep(p, dst.addr, field_count); - lbValue new_len = lb_emit_arith(p, Token_Sub, high, low, t_int); - lb_emit_store(p, len_dst, new_len); - } else if (type->Struct.soa_kind == StructSoa_Slice) { - if (no_indices) { - lb_addr_store(p, dst, base); - } else { - i32 field_count = cast(i32)type->Struct.fields.count - 1; - for (i32 i = 0; i < field_count; i++) { - lbValue field_dst = lb_emit_struct_ep(p, dst.addr, i); - lbValue field_src = lb_emit_struct_ev(p, base, i); - field_src = lb_emit_ptr_offset(p, field_src, low); - lb_emit_store(p, field_dst, field_src); - } - - - lbValue len_dst = lb_emit_struct_ep(p, dst.addr, field_count); - lbValue new_len = lb_emit_arith(p, Token_Sub, high, low, t_int); - lb_emit_store(p, len_dst, new_len); - } - } else if (type->Struct.soa_kind == StructSoa_Dynamic) { - i32 field_count = cast(i32)type->Struct.fields.count - 3; - for (i32 i = 0; i < field_count; i++) { - lbValue field_dst = lb_emit_struct_ep(p, dst.addr, i); - lbValue field_src = lb_emit_struct_ev(p, base, i); - field_src = lb_emit_ptr_offset(p, field_src, low); - lb_emit_store(p, field_dst, field_src); - } - - - lbValue len_dst = lb_emit_struct_ep(p, dst.addr, field_count); - lbValue new_len = lb_emit_arith(p, Token_Sub, high, low, t_int); - lb_emit_store(p, len_dst, new_len); - } - - return dst; - #endif - } - break; - - } - - GB_PANIC("Unknown slicable type"); + return lb_build_addr_slice_expr(p, expr); case_end; case_ast_node(de, DerefExpr, expr); - if (is_type_relative_pointer(type_of_expr(de->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;\ + return addr; + } else 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); + return lb_addr_soa_variable(ptr, idx, nullptr); } lbValue addr = lb_build_expr(p, de->expr); return lb_addr(addr); @@ -4077,747 +4653,7 @@ lbAddr lb_build_addr(lbProcedure *p, Ast *expr) { case_end; case_ast_node(cl, CompoundLit, expr); - Type *type = type_of_expr(expr); - Type *bt = base_type(type); - - lbAddr v = lb_add_local_generated(p, type, true); - - Type *et = nullptr; - switch (bt->kind) { - case Type_Array: et = bt->Array.elem; break; - case Type_EnumeratedArray: et = bt->EnumeratedArray.elem; break; - case Type_Slice: et = bt->Slice.elem; break; - case Type_BitSet: et = bt->BitSet.elem; break; - case Type_SimdVector: et = bt->SimdVector.elem; break; - case Type_Matrix: et = bt->Matrix.elem; break; - } - - String proc_name = {}; - if (p->entity) { - proc_name = p->entity->token.string; - } - TokenPos pos = ast_token(expr).pos; - - switch (bt->kind) { - default: GB_PANIC("Unknown CompoundLit type: %s", type_to_string(type)); break; - - case Type_Struct: { - // TODO(bill): "constant" '#raw_union's are not initialized constantly at the moment. - // NOTE(bill): This is due to the layout of the unions when printed to LLVM-IR - bool is_raw_union = is_type_raw_union(bt); - GB_ASSERT(is_type_struct(bt) || is_raw_union); - TypeStruct *st = &bt->Struct; - if (cl->elems.count > 0) { - lb_addr_store(p, v, lb_const_value(p->module, type, exact_value_compound(expr))); - lbValue comp_lit_ptr = lb_addr_get_ptr(p, v); - - for_array(field_index, cl->elems) { - Ast *elem = cl->elems[field_index]; - - lbValue field_expr = {}; - Entity *field = nullptr; - isize index = field_index; - - if (elem->kind == Ast_FieldValue) { - ast_node(fv, FieldValue, elem); - String name = fv->field->Ident.token.string; - Selection sel = lookup_field(bt, name, false); - index = sel.index[0]; - elem = fv->value; - TypeAndValue tav = type_and_value_of_expr(elem); - } else { - TypeAndValue tav = type_and_value_of_expr(elem); - Selection sel = lookup_field_from_index(bt, st->fields[field_index]->Variable.field_index); - index = sel.index[0]; - } - - field = st->fields[index]; - Type *ft = field->type; - if (!is_raw_union && !is_type_typeid(ft) && lb_is_elem_const(elem, ft)) { - continue; - } - - field_expr = lb_build_expr(p, elem); - - lbValue gep = {}; - if (is_raw_union) { - gep = lb_emit_conv(p, comp_lit_ptr, alloc_type_pointer(ft)); - } else { - gep = lb_emit_struct_ep(p, comp_lit_ptr, cast(i32)index); - } - - Type *fet = field_expr.type; - GB_ASSERT(fet->kind != Type_Tuple); - - // HACK TODO(bill): THIS IS A MASSIVE HACK!!!! - if (is_type_union(ft) && !are_types_identical(fet, ft) && !is_type_untyped(fet)) { - GB_ASSERT_MSG(union_variant_index(ft, fet) > 0, "%s", type_to_string(fet)); - - lb_emit_store_union_variant(p, gep, field_expr, fet); - } else { - lbValue fv = lb_emit_conv(p, field_expr, ft); - lb_emit_store(p, gep, fv); - } - } - } - break; - } - - case Type_Map: { - if (cl->elems.count == 0) { - break; - } - { - auto args = array_make(permanent_allocator(), 3); - args[0] = lb_gen_map_header(p, v.addr, type); - args[1] = lb_const_int(p->module, t_int, 2*cl->elems.count); - args[2] = lb_emit_source_code_location(p, proc_name, pos); - lb_emit_runtime_call(p, "__dynamic_map_reserve", args); - } - for_array(field_index, cl->elems) { - Ast *elem = cl->elems[field_index]; - ast_node(fv, FieldValue, elem); - - lbValue key = lb_build_expr(p, fv->field); - lbValue value = lb_build_expr(p, fv->value); - lb_insert_dynamic_map_key_and_value(p, v, type, key, value, elem); - } - break; - } - - case Type_Array: { - if (cl->elems.count > 0) { - lb_addr_store(p, v, lb_const_value(p->module, type, exact_value_compound(expr))); - - auto temp_data = array_make(temporary_allocator(), 0, cl->elems.count); - - // NOTE(bill): Separate value, gep, store into their own chunks - for_array(i, cl->elems) { - Ast *elem = cl->elems[i]; - if (elem->kind == Ast_FieldValue) { - ast_node(fv, FieldValue, elem); - if (lb_is_elem_const(fv->value, et)) { - continue; - } - if (is_ast_range(fv->field)) { - ast_node(ie, BinaryExpr, fv->field); - TypeAndValue lo_tav = ie->left->tav; - TypeAndValue hi_tav = ie->right->tav; - GB_ASSERT(lo_tav.mode == Addressing_Constant); - GB_ASSERT(hi_tav.mode == Addressing_Constant); - - TokenKind op = ie->op.kind; - i64 lo = exact_value_to_i64(lo_tav.value); - i64 hi = exact_value_to_i64(hi_tav.value); - if (op != Token_RangeHalf) { - hi += 1; - } - - lbValue value = lb_build_expr(p, fv->value); - - for (i64 k = lo; k < hi; k++) { - lbCompoundLitElemTempData data = {}; - data.value = value; - data.elem_index = cast(i32)k; - array_add(&temp_data, data); - } - - } else { - auto tav = fv->field->tav; - GB_ASSERT(tav.mode == Addressing_Constant); - i64 index = exact_value_to_i64(tav.value); - - lbValue value = lb_build_expr(p, fv->value); - lbCompoundLitElemTempData data = {}; - data.value = lb_emit_conv(p, value, et); - data.expr = fv->value; - data.elem_index = cast(i32)index; - array_add(&temp_data, data); - } - - } else { - if (lb_is_elem_const(elem, et)) { - continue; - } - lbCompoundLitElemTempData data = {}; - data.expr = elem; - data.elem_index = cast(i32)i; - array_add(&temp_data, data); - } - } - - for_array(i, temp_data) { - temp_data[i].gep = lb_emit_array_epi(p, lb_addr_get_ptr(p, v), temp_data[i].elem_index); - } - - for_array(i, temp_data) { - lbValue field_expr = temp_data[i].value; - Ast *expr = temp_data[i].expr; - - auto prev_hint = lb_set_copy_elision_hint(p, lb_addr(temp_data[i].gep), expr); - - if (field_expr.value == nullptr) { - field_expr = lb_build_expr(p, expr); - } - Type *t = field_expr.type; - GB_ASSERT(t->kind != Type_Tuple); - lbValue ev = lb_emit_conv(p, field_expr, et); - - if (!p->copy_elision_hint.used) { - temp_data[i].value = ev; - } - - lb_reset_copy_elision_hint(p, prev_hint); - } - - for_array(i, temp_data) { - if (temp_data[i].value.value != nullptr) { - lb_emit_store(p, temp_data[i].gep, temp_data[i].value); - } - } - } - break; - } - case Type_EnumeratedArray: { - if (cl->elems.count > 0) { - lb_addr_store(p, v, lb_const_value(p->module, type, exact_value_compound(expr))); - - auto temp_data = array_make(temporary_allocator(), 0, cl->elems.count); - - // NOTE(bill): Separate value, gep, store into their own chunks - for_array(i, cl->elems) { - Ast *elem = cl->elems[i]; - if (elem->kind == Ast_FieldValue) { - ast_node(fv, FieldValue, elem); - if (lb_is_elem_const(fv->value, et)) { - continue; - } - if (is_ast_range(fv->field)) { - ast_node(ie, BinaryExpr, fv->field); - TypeAndValue lo_tav = ie->left->tav; - TypeAndValue hi_tav = ie->right->tav; - GB_ASSERT(lo_tav.mode == Addressing_Constant); - GB_ASSERT(hi_tav.mode == Addressing_Constant); - - TokenKind op = ie->op.kind; - i64 lo = exact_value_to_i64(lo_tav.value); - i64 hi = exact_value_to_i64(hi_tav.value); - if (op != Token_RangeHalf) { - hi += 1; - } - - lbValue value = lb_build_expr(p, fv->value); - - for (i64 k = lo; k < hi; k++) { - lbCompoundLitElemTempData data = {}; - data.value = value; - data.elem_index = cast(i32)k; - array_add(&temp_data, data); - } - - } else { - auto tav = fv->field->tav; - GB_ASSERT(tav.mode == Addressing_Constant); - i64 index = exact_value_to_i64(tav.value); - - lbValue value = lb_build_expr(p, fv->value); - lbCompoundLitElemTempData data = {}; - data.value = lb_emit_conv(p, value, et); - data.expr = fv->value; - data.elem_index = cast(i32)index; - array_add(&temp_data, data); - } - - } else { - if (lb_is_elem_const(elem, et)) { - continue; - } - lbCompoundLitElemTempData data = {}; - data.expr = elem; - data.elem_index = cast(i32)i; - array_add(&temp_data, data); - } - } - - - i32 index_offset = cast(i32)exact_value_to_i64(*bt->EnumeratedArray.min_value); - - for_array(i, temp_data) { - i32 index = temp_data[i].elem_index - index_offset; - temp_data[i].gep = lb_emit_array_epi(p, lb_addr_get_ptr(p, v), index); - } - - for_array(i, temp_data) { - lbValue field_expr = temp_data[i].value; - Ast *expr = temp_data[i].expr; - - auto prev_hint = lb_set_copy_elision_hint(p, lb_addr(temp_data[i].gep), expr); - - if (field_expr.value == nullptr) { - field_expr = lb_build_expr(p, expr); - } - Type *t = field_expr.type; - GB_ASSERT(t->kind != Type_Tuple); - lbValue ev = lb_emit_conv(p, field_expr, et); - - if (!p->copy_elision_hint.used) { - temp_data[i].value = ev; - } - - lb_reset_copy_elision_hint(p, prev_hint); - } - - for_array(i, temp_data) { - if (temp_data[i].value.value != nullptr) { - lb_emit_store(p, temp_data[i].gep, temp_data[i].value); - } - } - } - break; - } - case Type_Slice: { - if (cl->elems.count > 0) { - lbValue slice = lb_const_value(p->module, type, exact_value_compound(expr)); - - lbValue data = lb_slice_elem(p, slice); - - auto temp_data = array_make(temporary_allocator(), 0, cl->elems.count); - - for_array(i, cl->elems) { - Ast *elem = cl->elems[i]; - if (elem->kind == Ast_FieldValue) { - ast_node(fv, FieldValue, elem); - - if (lb_is_elem_const(fv->value, et)) { - continue; - } - - if (is_ast_range(fv->field)) { - ast_node(ie, BinaryExpr, fv->field); - TypeAndValue lo_tav = ie->left->tav; - TypeAndValue hi_tav = ie->right->tav; - GB_ASSERT(lo_tav.mode == Addressing_Constant); - GB_ASSERT(hi_tav.mode == Addressing_Constant); - - TokenKind op = ie->op.kind; - i64 lo = exact_value_to_i64(lo_tav.value); - i64 hi = exact_value_to_i64(hi_tav.value); - if (op != Token_RangeHalf) { - hi += 1; - } - - lbValue value = lb_emit_conv(p, lb_build_expr(p, fv->value), et); - - for (i64 k = lo; k < hi; k++) { - lbCompoundLitElemTempData data = {}; - data.value = value; - data.elem_index = cast(i32)k; - array_add(&temp_data, data); - } - - } else { - GB_ASSERT(fv->field->tav.mode == Addressing_Constant); - i64 index = exact_value_to_i64(fv->field->tav.value); - - lbValue field_expr = lb_build_expr(p, fv->value); - GB_ASSERT(!is_type_tuple(field_expr.type)); - - lbValue ev = lb_emit_conv(p, field_expr, et); - - lbCompoundLitElemTempData data = {}; - data.value = ev; - data.elem_index = cast(i32)index; - array_add(&temp_data, data); - } - } else { - if (lb_is_elem_const(elem, et)) { - continue; - } - lbValue field_expr = lb_build_expr(p, elem); - GB_ASSERT(!is_type_tuple(field_expr.type)); - - lbValue ev = lb_emit_conv(p, field_expr, et); - - lbCompoundLitElemTempData data = {}; - data.value = ev; - data.elem_index = cast(i32)i; - array_add(&temp_data, data); - } - } - - for_array(i, temp_data) { - temp_data[i].gep = lb_emit_ptr_offset(p, data, lb_const_int(p->module, t_int, temp_data[i].elem_index)); - } - - for_array(i, temp_data) { - lb_emit_store(p, temp_data[i].gep, temp_data[i].value); - } - - { - lbValue count = {}; - count.type = t_int; - - if (lb_is_const(slice)) { - unsigned indices[1] = {1}; - count.value = LLVMConstExtractValue(slice.value, indices, gb_count_of(indices)); - } else { - count.value = LLVMBuildExtractValue(p->builder, slice.value, 1, ""); - } - lb_fill_slice(p, v, data, count); - } - } - break; - } - - case Type_DynamicArray: { - if (cl->elems.count == 0) { - break; - } - Type *et = bt->DynamicArray.elem; - lbValue size = lb_const_int(p->module, t_int, type_size_of(et)); - lbValue align = lb_const_int(p->module, t_int, type_align_of(et)); - - i64 item_count = gb_max(cl->max_count, cl->elems.count); - { - - auto args = array_make(permanent_allocator(), 5); - args[0] = lb_emit_conv(p, lb_addr_get_ptr(p, v), t_rawptr); - args[1] = size; - args[2] = align; - args[3] = lb_const_int(p->module, t_int, 2*item_count); // TODO(bill): Is this too much waste? - args[4] = lb_emit_source_code_location(p, proc_name, pos); - lb_emit_runtime_call(p, "__dynamic_array_reserve", args); - } - - lbValue items = lb_generate_local_array(p, et, item_count); - // lbValue items = lb_generate_global_array(p->module, et, item_count, str_lit("dacl$"), cast(i64)cast(intptr)expr); - - for_array(i, cl->elems) { - Ast *elem = cl->elems[i]; - if (elem->kind == Ast_FieldValue) { - ast_node(fv, FieldValue, elem); - if (is_ast_range(fv->field)) { - ast_node(ie, BinaryExpr, fv->field); - TypeAndValue lo_tav = ie->left->tav; - TypeAndValue hi_tav = ie->right->tav; - GB_ASSERT(lo_tav.mode == Addressing_Constant); - GB_ASSERT(hi_tav.mode == Addressing_Constant); - - TokenKind op = ie->op.kind; - i64 lo = exact_value_to_i64(lo_tav.value); - i64 hi = exact_value_to_i64(hi_tav.value); - if (op != Token_RangeHalf) { - hi += 1; - } - - lbValue value = lb_emit_conv(p, lb_build_expr(p, fv->value), et); - - for (i64 k = lo; k < hi; k++) { - lbValue ep = lb_emit_array_epi(p, items, cast(i32)k); - lb_emit_store(p, ep, value); - } - } else { - GB_ASSERT(fv->field->tav.mode == Addressing_Constant); - - i64 field_index = exact_value_to_i64(fv->field->tav.value); - - lbValue ev = lb_build_expr(p, fv->value); - lbValue value = lb_emit_conv(p, ev, et); - lbValue ep = lb_emit_array_epi(p, items, cast(i32)field_index); - lb_emit_store(p, ep, value); - } - } else { - lbValue value = lb_emit_conv(p, lb_build_expr(p, elem), et); - lbValue ep = lb_emit_array_epi(p, items, cast(i32)i); - lb_emit_store(p, ep, value); - } - } - - { - auto args = array_make(permanent_allocator(), 6); - args[0] = lb_emit_conv(p, v.addr, t_rawptr); - args[1] = size; - args[2] = align; - args[3] = lb_emit_conv(p, items, t_rawptr); - args[4] = lb_const_int(p->module, t_int, item_count); - args[5] = lb_emit_source_code_location(p, proc_name, pos); - lb_emit_runtime_call(p, "__dynamic_array_append", args); - } - break; - } - - case Type_Basic: { - GB_ASSERT(is_type_any(bt)); - if (cl->elems.count > 0) { - lb_addr_store(p, v, lb_const_value(p->module, type, exact_value_compound(expr))); - String field_names[2] = { - str_lit("data"), - str_lit("id"), - }; - Type *field_types[2] = { - t_rawptr, - t_typeid, - }; - - for_array(field_index, cl->elems) { - Ast *elem = cl->elems[field_index]; - - lbValue field_expr = {}; - isize index = field_index; - - if (elem->kind == Ast_FieldValue) { - ast_node(fv, FieldValue, elem); - Selection sel = lookup_field(bt, fv->field->Ident.token.string, false); - index = sel.index[0]; - elem = fv->value; - } else { - TypeAndValue tav = type_and_value_of_expr(elem); - Selection sel = lookup_field(bt, field_names[field_index], false); - index = sel.index[0]; - } - - field_expr = lb_build_expr(p, elem); - - GB_ASSERT(field_expr.type->kind != Type_Tuple); - - Type *ft = field_types[index]; - lbValue fv = lb_emit_conv(p, field_expr, ft); - lbValue gep = lb_emit_struct_ep(p, lb_addr_get_ptr(p, v), cast(i32)index); - lb_emit_store(p, gep, fv); - } - } - - break; - } - - case Type_BitSet: { - i64 sz = type_size_of(type); - if (cl->elems.count > 0 && sz > 0) { - lb_addr_store(p, v, lb_const_value(p->module, type, exact_value_compound(expr))); - - lbValue lower = lb_const_value(p->module, t_int, exact_value_i64(bt->BitSet.lower)); - for_array(i, cl->elems) { - Ast *elem = cl->elems[i]; - GB_ASSERT(elem->kind != Ast_FieldValue); - - if (lb_is_elem_const(elem, et)) { - continue; - } - - lbValue expr = lb_build_expr(p, elem); - GB_ASSERT(expr.type->kind != Type_Tuple); - - Type *it = bit_set_to_int(bt); - lbValue one = lb_const_value(p->module, it, exact_value_i64(1)); - lbValue e = lb_emit_conv(p, expr, it); - e = lb_emit_arith(p, Token_Sub, e, lower, it); - e = lb_emit_arith(p, Token_Shl, one, e, it); - - lbValue old_value = lb_emit_transmute(p, lb_addr_load(p, v), it); - lbValue new_value = lb_emit_arith(p, Token_Or, old_value, e, it); - new_value = lb_emit_transmute(p, new_value, type); - lb_addr_store(p, v, new_value); - } - } - break; - } - - case Type_Matrix: { - if (cl->elems.count > 0) { - lb_addr_store(p, v, lb_const_value(p->module, type, exact_value_compound(expr))); - - auto temp_data = array_make(temporary_allocator(), 0, cl->elems.count); - - // NOTE(bill): Separate value, gep, store into their own chunks - for_array(i, cl->elems) { - Ast *elem = cl->elems[i]; - - if (elem->kind == Ast_FieldValue) { - ast_node(fv, FieldValue, elem); - if (lb_is_elem_const(fv->value, et)) { - continue; - } - if (is_ast_range(fv->field)) { - ast_node(ie, BinaryExpr, fv->field); - TypeAndValue lo_tav = ie->left->tav; - TypeAndValue hi_tav = ie->right->tav; - GB_ASSERT(lo_tav.mode == Addressing_Constant); - GB_ASSERT(hi_tav.mode == Addressing_Constant); - - TokenKind op = ie->op.kind; - i64 lo = exact_value_to_i64(lo_tav.value); - i64 hi = exact_value_to_i64(hi_tav.value); - if (op != Token_RangeHalf) { - hi += 1; - } - - lbValue value = lb_build_expr(p, fv->value); - - for (i64 k = lo; k < hi; k++) { - lbCompoundLitElemTempData data = {}; - data.value = value; - - data.elem_index = cast(i32)matrix_row_major_index_to_offset(bt, k); - array_add(&temp_data, data); - } - - } else { - auto tav = fv->field->tav; - GB_ASSERT(tav.mode == Addressing_Constant); - i64 index = exact_value_to_i64(tav.value); - - lbValue value = lb_build_expr(p, fv->value); - lbCompoundLitElemTempData data = {}; - data.value = lb_emit_conv(p, value, et); - data.expr = fv->value; - - data.elem_index = cast(i32)matrix_row_major_index_to_offset(bt, index); - array_add(&temp_data, data); - } - - } else { - if (lb_is_elem_const(elem, et)) { - continue; - } - lbCompoundLitElemTempData data = {}; - data.expr = elem; - data.elem_index = cast(i32)matrix_row_major_index_to_offset(bt, i); - array_add(&temp_data, data); - } - } - - for_array(i, temp_data) { - temp_data[i].gep = lb_emit_array_epi(p, lb_addr_get_ptr(p, v), temp_data[i].elem_index); - } - - for_array(i, temp_data) { - lbValue field_expr = temp_data[i].value; - Ast *expr = temp_data[i].expr; - - auto prev_hint = lb_set_copy_elision_hint(p, lb_addr(temp_data[i].gep), expr); - - if (field_expr.value == nullptr) { - field_expr = lb_build_expr(p, expr); - } - Type *t = field_expr.type; - GB_ASSERT(t->kind != Type_Tuple); - lbValue ev = lb_emit_conv(p, field_expr, et); - - if (!p->copy_elision_hint.used) { - temp_data[i].value = ev; - } - - lb_reset_copy_elision_hint(p, prev_hint); - } - - for_array(i, temp_data) { - if (temp_data[i].value.value != nullptr) { - lb_emit_store(p, temp_data[i].gep, temp_data[i].value); - } - } - } - break; - } - - case Type_SimdVector: { - if (cl->elems.count > 0) { - lbValue vector_value = lb_const_value(p->module, type, exact_value_compound(expr)); - defer (lb_addr_store(p, v, vector_value)); - - auto temp_data = array_make(temporary_allocator(), 0, cl->elems.count); - - // NOTE(bill): Separate value, store into their own chunks - for_array(i, cl->elems) { - Ast *elem = cl->elems[i]; - if (elem->kind == Ast_FieldValue) { - ast_node(fv, FieldValue, elem); - if (lb_is_elem_const(fv->value, et)) { - continue; - } - if (is_ast_range(fv->field)) { - ast_node(ie, BinaryExpr, fv->field); - TypeAndValue lo_tav = ie->left->tav; - TypeAndValue hi_tav = ie->right->tav; - GB_ASSERT(lo_tav.mode == Addressing_Constant); - GB_ASSERT(hi_tav.mode == Addressing_Constant); - - TokenKind op = ie->op.kind; - i64 lo = exact_value_to_i64(lo_tav.value); - i64 hi = exact_value_to_i64(hi_tav.value); - if (op != Token_RangeHalf) { - hi += 1; - } - - lbValue value = lb_build_expr(p, fv->value); - - for (i64 k = lo; k < hi; k++) { - lbCompoundLitElemTempData data = {}; - data.value = value; - data.elem_index = cast(i32)k; - array_add(&temp_data, data); - } - - } else { - auto tav = fv->field->tav; - GB_ASSERT(tav.mode == Addressing_Constant); - i64 index = exact_value_to_i64(tav.value); - - lbValue value = lb_build_expr(p, fv->value); - lbCompoundLitElemTempData data = {}; - data.value = lb_emit_conv(p, value, et); - data.expr = fv->value; - data.elem_index = cast(i32)index; - array_add(&temp_data, data); - } - - } else { - if (lb_is_elem_const(elem, et)) { - continue; - } - lbCompoundLitElemTempData data = {}; - data.expr = elem; - data.elem_index = cast(i32)i; - array_add(&temp_data, data); - } - } - - - for_array(i, temp_data) { - lbValue field_expr = temp_data[i].value; - Ast *expr = temp_data[i].expr; - - auto prev_hint = lb_set_copy_elision_hint(p, lb_addr(temp_data[i].gep), expr); - - if (field_expr.value == nullptr) { - field_expr = lb_build_expr(p, expr); - } - Type *t = field_expr.type; - GB_ASSERT(t->kind != Type_Tuple); - lbValue ev = lb_emit_conv(p, field_expr, et); - - if (!p->copy_elision_hint.used) { - temp_data[i].value = ev; - } - - lb_reset_copy_elision_hint(p, prev_hint); - } - - - // TODO(bill): reduce the need for individual `insertelement` if a `shufflevector` - // might be a better option - - for_array(i, temp_data) { - if (temp_data[i].value.value != nullptr) { - LLVMValueRef index = lb_const_int(p->module, t_u32, temp_data[i].elem_index).value; - vector_value.value = LLVMBuildInsertElement(p->builder, vector_value.value, temp_data[i].value.value, index, ""); - } - } - } - break; - } - } - - return v; + return lb_build_addr_compound_lit(p, expr); case_end; case_ast_node(tc, TypeCast, expr); diff --git a/src/llvm_backend_general.cpp b/src/llvm_backend_general.cpp index e3d30ccff..ee6980de4 100644 --- a/src/llvm_backend_general.cpp +++ b/src/llvm_backend_general.cpp @@ -56,6 +56,7 @@ void lb_init_module(lbModule *m, Checker *c) { gbAllocator a = heap_allocator(); map_init(&m->types, a); + map_init(&m->func_raw_types, a); map_init(&m->struct_field_remapping, a); map_init(&m->values, a); map_init(&m->soa_values, a); @@ -174,7 +175,8 @@ struct lbLoopData { struct lbCompoundLitElemTempData { Ast * expr; lbValue value; - i32 elem_index; + i64 elem_index; + i64 elem_length; lbValue gep; }; @@ -211,6 +213,45 @@ void lb_loop_end(lbProcedure *p, lbLoopData const &data) { } +// This emits a GEP at 0, index +lbValue lb_emit_epi(lbProcedure *p, lbValue const &value, isize index) { + GB_ASSERT(is_type_pointer(value.type)); + Type *type = type_deref(value.type); + + LLVMValueRef indices[2] = { + LLVMConstInt(lb_type(p->module, t_int), 0, false), + LLVMConstInt(lb_type(p->module, t_int), cast(unsigned long long)index, false), + }; + LLVMTypeRef llvm_type = lb_type(p->module, type); + lbValue res = {}; + Type *ptr = base_array_type(type); + res.type = alloc_type_pointer(ptr); + if (LLVMIsConstant(value.value)) { + res.value = LLVMConstGEP2(llvm_type, value.value, indices, gb_count_of(indices)); + } else { + res.value = LLVMBuildGEP2(p->builder, llvm_type, value.value, indices, gb_count_of(indices), ""); + } + return res; +} +// This emits a GEP at 0, index +lbValue lb_emit_epi(lbModule *m, lbValue const &value, isize index) { + GB_ASSERT(is_type_pointer(value.type)); + GB_ASSERT(LLVMIsConstant(value.value)); + Type *type = type_deref(value.type); + + LLVMValueRef indices[2] = { + LLVMConstInt(lb_type(m, t_int), 0, false), + LLVMConstInt(lb_type(m, t_int), cast(unsigned long long)index, false), + }; + lbValue res = {}; + Type *ptr = base_array_type(type); + res.type = alloc_type_pointer(ptr); + res.value = LLVMConstGEP2(lb_type(m, type), value.value, indices, gb_count_of(indices)); + return res; +} + + + LLVMValueRef llvm_zero(lbModule *m) { return LLVMConstInt(lb_type(m, t_int), 0, false); } @@ -341,9 +382,6 @@ Type *lb_addr_type(lbAddr const &addr) { } return type_deref(addr.addr.type); } -LLVMTypeRef lb_addr_lb_type(lbAddr const &addr) { - return LLVMGetElementType(LLVMTypeOf(addr.addr.value)); -} lbValue lb_addr_get_ptr(lbProcedure *p, lbAddr const &addr) { if (addr.addr.value == nullptr) { @@ -530,6 +568,13 @@ void lb_emit_slice_bounds_check(lbProcedure *p, Token token, lbValue low, lbValu } } +unsigned lb_try_get_alignment(LLVMValueRef addr_ptr, unsigned default_alignment) { + if (LLVMIsAGlobalValue(addr_ptr) || LLVMIsAAllocaInst(addr_ptr) || LLVMIsALoadInst(addr_ptr)) { + return LLVMGetAlignment(addr_ptr); + } + return default_alignment; +} + bool lb_try_update_alignment(LLVMValueRef addr_ptr, unsigned alignment) { if (LLVMIsAGlobalValue(addr_ptr) || LLVMIsAAllocaInst(addr_ptr) || LLVMIsALoadInst(addr_ptr)) { if (LLVMGetAlignment(addr_ptr) < alignment) { @@ -852,6 +897,20 @@ bool lb_is_type_proc_recursive(Type *t) { void lb_emit_store(lbProcedure *p, lbValue ptr, lbValue value) { GB_ASSERT(value.value != nullptr); Type *a = type_deref(ptr.type); + + if (LLVMIsNull(value.value)) { + LLVMTypeRef src_t = llvm_addr_type(p->module, ptr); + if (is_type_proc(a)) { + LLVMTypeRef rawptr_type = lb_type(p->module, t_rawptr); + LLVMTypeRef rawptr_ptr_type = LLVMPointerType(rawptr_type, 0); + LLVMBuildStore(p->builder, LLVMConstNull(rawptr_type), LLVMBuildBitCast(p->builder, ptr.value, rawptr_ptr_type, "")); + } else if (lb_sizeof(src_t) <= lb_max_zero_init_size()) { + LLVMBuildStore(p->builder, LLVMConstNull(src_t), ptr.value); + } else { + lb_mem_zero_ptr(p, ptr.value, a, 1); + } + return; + } if (is_type_boolean(a)) { // NOTE(bill): There are multiple sized booleans, thus force a conversion (if necessarily) value = lb_emit_conv(p, value, a); @@ -861,13 +920,48 @@ void lb_emit_store(lbProcedure *p, lbValue ptr, lbValue value) { GB_ASSERT_MSG(are_types_identical(ca, core_type(value.type)), "%s != %s", type_to_string(a), type_to_string(value.type)); } + enum {MAX_STORE_SIZE = 64}; + + if (lb_sizeof(LLVMTypeOf(value.value)) > MAX_STORE_SIZE) { + if (LLVMIsALoadInst(value.value)) { + LLVMValueRef dst_ptr = ptr.value; + LLVMValueRef src_ptr_original = LLVMGetOperand(value.value, 0); + LLVMValueRef src_ptr = LLVMBuildPointerCast(p->builder, src_ptr_original, LLVMTypeOf(dst_ptr), ""); + + LLVMBuildMemMove(p->builder, + dst_ptr, lb_try_get_alignment(dst_ptr, 1), + src_ptr, lb_try_get_alignment(src_ptr_original, 1), + LLVMConstInt(LLVMInt64TypeInContext(p->module->ctx), lb_sizeof(LLVMTypeOf(value.value)), false)); + return; + } else if (LLVMIsConstant(value.value)) { + lbAddr addr = lb_add_global_generated(p->module, value.type, value, nullptr); + LLVMValueRef global_data = addr.addr.value; + // make it truly private data + LLVMSetLinkage(global_data, LLVMPrivateLinkage); + LLVMSetUnnamedAddress(global_data, LLVMGlobalUnnamedAddr); + LLVMSetGlobalConstant(global_data, true); + + LLVMValueRef dst_ptr = ptr.value; + LLVMValueRef src_ptr = global_data; + src_ptr = LLVMBuildPointerCast(p->builder, src_ptr, LLVMTypeOf(dst_ptr), ""); + + LLVMBuildMemMove(p->builder, + dst_ptr, lb_try_get_alignment(dst_ptr, 1), + src_ptr, lb_try_get_alignment(global_data, 1), + LLVMConstInt(LLVMInt64TypeInContext(p->module->ctx), lb_sizeof(LLVMTypeOf(value.value)), false)); + return; + } + } + if (lb_is_type_proc_recursive(a)) { // NOTE(bill, 2020-11-11): Because of certain LLVM rules, a procedure value may be // stored as regular pointer with no procedure information - LLVMTypeRef src_t = LLVMGetElementType(LLVMTypeOf(ptr.value)); - LLVMValueRef v = LLVMBuildPointerCast(p->builder, value.value, src_t, ""); - LLVMBuildStore(p->builder, v, ptr.value); + LLVMTypeRef rawptr_type = lb_type(p->module, t_rawptr); + LLVMTypeRef rawptr_ptr_type = LLVMPointerType(rawptr_type, 0); + LLVMBuildStore(p->builder, + LLVMBuildPointerCast(p->builder, value.value, rawptr_type, ""), + LLVMBuildPointerCast(p->builder, ptr.value, rawptr_ptr_type, "")); } else { Type *ca = core_type(a); if (ca->kind == Type_Basic || ca->kind == Type_Proc) { @@ -880,8 +974,8 @@ void lb_emit_store(lbProcedure *p, lbValue ptr, lbValue value) { } } -LLVMTypeRef llvm_addr_type(lbValue addr_val) { - return LLVMGetElementType(LLVMTypeOf(addr_val.value)); +LLVMTypeRef llvm_addr_type(lbModule *module, lbValue addr_val) { + return lb_type(module, type_deref(addr_val.type)); } lbValue lb_emit_load(lbProcedure *p, lbValue value) { @@ -890,12 +984,18 @@ lbValue lb_emit_load(lbProcedure *p, lbValue value) { Type *vt = base_type(value.type); GB_ASSERT(vt->kind == Type_MultiPointer); Type *t = vt->MultiPointer.elem; - LLVMValueRef v = LLVMBuildLoad2(p->builder, llvm_addr_type(value), value.value, ""); + LLVMValueRef v = LLVMBuildLoad2(p->builder, lb_type(p->module, t), value.value, ""); return lbValue{v, t}; + } else if (is_type_soa_pointer(value.type)) { + lbValue ptr = lb_emit_struct_ev(p, value, 0); + lbValue idx = lb_emit_struct_ev(p, value, 1); + lbAddr addr = lb_addr_soa_variable(ptr, idx, nullptr); + return lb_addr_load(p, addr); } + GB_ASSERT(is_type_pointer(value.type)); Type *t = type_deref(value.type); - LLVMValueRef v = LLVMBuildLoad2(p->builder, llvm_addr_type(value), value.value, ""); + LLVMValueRef v = LLVMBuildLoad2(p->builder, lb_type(p->module, t), value.value, ""); return lbValue{v, t}; } @@ -1160,12 +1260,12 @@ lbValue lb_emit_union_tag_ptr(lbProcedure *p, lbValue u) { Type *tag_type = union_tag_type(ut); - LLVMTypeRef uvt = LLVMGetElementType(LLVMTypeOf(u.value)); + LLVMTypeRef uvt = llvm_addr_type(p->module, u); unsigned element_count = LLVMCountStructElementTypes(uvt); GB_ASSERT_MSG(element_count >= 2, "element_count=%u (%s) != (%s)", element_count, type_to_string(ut), LLVMPrintTypeToString(uvt)); lbValue tag_ptr = {}; - tag_ptr.value = LLVMBuildStructGEP(p->builder, u.value, 1, ""); + tag_ptr.value = LLVMBuildStructGEP2(p->builder, uvt, u.value, 1, ""); tag_ptr.type = alloc_type_pointer(tag_type); return tag_ptr; } @@ -1389,6 +1489,116 @@ String lb_get_entity_name(lbModule *m, Entity *e, String default_name) { } +LLVMTypeRef lb_type_internal_for_procedures_raw(lbModule *m, Type *type) { + Type *original_type = type; + type = base_type(original_type); + GB_ASSERT(type->kind == Type_Proc); + + LLVMTypeRef *found = map_get(&m->func_raw_types, type); + if (found) { + return *found; + } + + unsigned param_count = 0; + if (type->Proc.calling_convention == ProcCC_Odin) { + param_count += 1; + } + + if (type->Proc.param_count != 0) { + GB_ASSERT(type->Proc.params->kind == Type_Tuple); + for_array(i, type->Proc.params->Tuple.variables) { + Entity *e = type->Proc.params->Tuple.variables[i]; + if (e->kind != Entity_Variable) { + continue; + } + if (e->flags & EntityFlag_CVarArg) { + continue; + } + param_count += 1; + } + } + m->internal_type_level += 1; + defer (m->internal_type_level -= 1); + + LLVMTypeRef ret = nullptr; + LLVMTypeRef *params = gb_alloc_array(permanent_allocator(), LLVMTypeRef, param_count); + if (type->Proc.result_count != 0) { + Type *single_ret = reduce_tuple_to_single_type(type->Proc.results); + ret = lb_type(m, single_ret); + if (ret != nullptr) { + if (is_type_boolean(single_ret) && + is_calling_convention_none(type->Proc.calling_convention) && + type_size_of(single_ret) <= 1) { + ret = LLVMInt1TypeInContext(m->ctx); + } + } + } + + unsigned param_index = 0; + if (type->Proc.param_count != 0) { + GB_ASSERT(type->Proc.params->kind == Type_Tuple); + for_array(i, type->Proc.params->Tuple.variables) { + Entity *e = type->Proc.params->Tuple.variables[i]; + if (e->kind != Entity_Variable) { + continue; + } + if (e->flags & EntityFlag_CVarArg) { + continue; + } + Type *e_type = reduce_tuple_to_single_type(e->type); + + LLVMTypeRef param_type = nullptr; + if (e->flags & EntityFlag_ByPtr) { + param_type = lb_type(m, alloc_type_pointer(e_type)); + } else if (is_type_boolean(e_type) && + type_size_of(e_type) <= 1) { + param_type = LLVMInt1TypeInContext(m->ctx); + } else { + if (is_type_proc(e_type)) { + param_type = lb_type(m, t_rawptr); + } else { + param_type = lb_type(m, e_type); + } + } + + params[param_index++] = param_type; + } + } + if (param_index < param_count) { + params[param_index++] = lb_type(m, t_rawptr); + } + GB_ASSERT(param_index == param_count); + + lbFunctionType *ft = lb_get_abi_info(m->ctx, params, param_count, ret, ret != nullptr, type->Proc.calling_convention); + { + for_array(j, ft->args) { + auto arg = ft->args[j]; + GB_ASSERT_MSG(LLVMGetTypeContext(arg.type) == ft->ctx, + "\n\t%s %td/%td" + "\n\tArgTypeCtx: %p\n\tCurrentCtx: %p\n\tGlobalCtx: %p", + LLVMPrintTypeToString(arg.type), + j, ft->args.count, + LLVMGetTypeContext(arg.type), ft->ctx, LLVMGetGlobalContext()); + } + GB_ASSERT_MSG(LLVMGetTypeContext(ft->ret.type) == ft->ctx, + "\n\t%s" + "\n\tRetTypeCtx: %p\n\tCurrentCtx: %p\n\tGlobalCtx: %p", + LLVMPrintTypeToString(ft->ret.type), + LLVMGetTypeContext(ft->ret.type), ft->ctx, LLVMGetGlobalContext()); + } + + map_set(&m->function_type_map, type, ft); + LLVMTypeRef new_abi_fn_type = lb_function_type_to_llvm_raw(ft, type->Proc.c_vararg); + + GB_ASSERT_MSG(LLVMGetTypeContext(new_abi_fn_type) == m->ctx, + "\n\tFuncTypeCtx: %p\n\tCurrentCtx: %p\n\tGlobalCtx: %p", + LLVMGetTypeContext(new_abi_fn_type), m->ctx, LLVMGetGlobalContext()); + + map_set(&m->func_raw_types, type, new_abi_fn_type); + + return new_abi_fn_type; + +} LLVMTypeRef lb_type_internal(lbModule *m, Type *type) { LLVMContextRef ctx = m->ctx; i64 size = type_size_of(type); // Check size @@ -1892,102 +2102,8 @@ LLVMTypeRef lb_type_internal(lbModule *m, Type *type) { if (m->internal_type_level > 1) { // TODO HACK(bill): is this really enough? return LLVMPointerType(LLVMIntTypeInContext(m->ctx, 8), 0); } else { - unsigned param_count = 0; - if (type->Proc.calling_convention == ProcCC_Odin) { - param_count += 1; - } - - if (type->Proc.param_count != 0) { - GB_ASSERT(type->Proc.params->kind == Type_Tuple); - for_array(i, type->Proc.params->Tuple.variables) { - Entity *e = type->Proc.params->Tuple.variables[i]; - if (e->kind != Entity_Variable) { - continue; - } - if (e->flags & EntityFlag_CVarArg) { - continue; - } - param_count += 1; - } - } - m->internal_type_level += 1; - defer (m->internal_type_level -= 1); - - LLVMTypeRef ret = nullptr; - LLVMTypeRef *params = gb_alloc_array(permanent_allocator(), LLVMTypeRef, param_count); - if (type->Proc.result_count != 0) { - Type *single_ret = reduce_tuple_to_single_type(type->Proc.results); - ret = lb_type(m, single_ret); - if (ret != nullptr) { - if (is_type_boolean(single_ret) && - is_calling_convention_none(type->Proc.calling_convention) && - type_size_of(single_ret) <= 1) { - ret = LLVMInt1TypeInContext(m->ctx); - } - } - } - - unsigned param_index = 0; - if (type->Proc.param_count != 0) { - GB_ASSERT(type->Proc.params->kind == Type_Tuple); - for_array(i, type->Proc.params->Tuple.variables) { - Entity *e = type->Proc.params->Tuple.variables[i]; - if (e->kind != Entity_Variable) { - continue; - } - if (e->flags & EntityFlag_CVarArg) { - continue; - } - - Type *e_type = reduce_tuple_to_single_type(e->type); - - LLVMTypeRef param_type = nullptr; - if (is_type_boolean(e_type) && - type_size_of(e_type) <= 1) { - param_type = LLVMInt1TypeInContext(m->ctx); - } else { - if (is_type_proc(e_type)) { - param_type = lb_type(m, t_rawptr); - } else { - param_type = lb_type(m, e_type); - } - } - - params[param_index++] = param_type; - } - } - if (param_index < param_count) { - params[param_index++] = lb_type(m, t_rawptr); - } - GB_ASSERT(param_index == param_count); - - lbFunctionType *ft = lb_get_abi_info(m->ctx, params, param_count, ret, ret != nullptr, type->Proc.calling_convention); - { - for_array(j, ft->args) { - auto arg = ft->args[j]; - GB_ASSERT_MSG(LLVMGetTypeContext(arg.type) == ft->ctx, - "\n\t%s %td/%td" - "\n\tArgTypeCtx: %p\n\tCurrentCtx: %p\n\tGlobalCtx: %p", - LLVMPrintTypeToString(arg.type), - j, ft->args.count, - LLVMGetTypeContext(arg.type), ft->ctx, LLVMGetGlobalContext()); - } - GB_ASSERT_MSG(LLVMGetTypeContext(ft->ret.type) == ft->ctx, - "\n\t%s" - "\n\tRetTypeCtx: %p\n\tCurrentCtx: %p\n\tGlobalCtx: %p", - LLVMPrintTypeToString(ft->ret.type), - LLVMGetTypeContext(ft->ret.type), ft->ctx, LLVMGetGlobalContext()); - } - - map_set(&m->function_type_map, type, ft); - LLVMTypeRef new_abi_fn_ptr_type = lb_function_type_to_llvm_ptr(ft, type->Proc.c_vararg); - LLVMTypeRef new_abi_fn_type = LLVMGetElementType(new_abi_fn_ptr_type); - - GB_ASSERT_MSG(LLVMGetTypeContext(new_abi_fn_type) == m->ctx, - "\n\tFuncTypeCtx: %p\n\tCurrentCtx: %p\n\tGlobalCtx: %p", - LLVMGetTypeContext(new_abi_fn_type), m->ctx, LLVMGetGlobalContext()); - - return new_abi_fn_ptr_type; + LLVMTypeRef proc_raw_type = lb_type_internal_for_procedures_raw(m, type); + return LLVMPointerType(proc_raw_type, 0); } break; @@ -2030,6 +2146,15 @@ LLVMTypeRef lb_type_internal(lbModule *m, Type *type) { m->internal_type_level += 1; return t; } + + case Type_SoaPointer: + { + unsigned field_count = 2; + LLVMTypeRef *fields = gb_alloc_array(permanent_allocator(), LLVMTypeRef, field_count); + fields[0] = LLVMPointerType(lb_type(m, type->Pointer.elem), 0); + fields[1] = LLVMIntTypeInContext(ctx, 8*cast(unsigned)build_context.word_size); + return LLVMStructTypeInContext(ctx, fields, field_count, false); + } } @@ -2234,6 +2359,17 @@ void lb_emit_if(lbProcedure *p, lbValue cond, lbBlock *true_block, lbBlock *fals } +gb_inline LLVMTypeRef OdinLLVMGetInternalElementType(LLVMTypeRef type) { + return LLVMGetElementType(type); +} +LLVMTypeRef OdinLLVMGetArrayElementType(LLVMTypeRef type) { + GB_ASSERT(lb_is_type_kind(type, LLVMArrayTypeKind)); + return OdinLLVMGetInternalElementType(type); +} +LLVMTypeRef OdinLLVMGetVectorElementType(LLVMTypeRef type) { + GB_ASSERT(lb_is_type_kind(type, LLVMVectorTypeKind)); + return OdinLLVMGetInternalElementType(type); +} LLVMValueRef OdinLLVMBuildTransmute(lbProcedure *p, LLVMValueRef val, LLVMTypeRef dst_type) { @@ -2304,7 +2440,7 @@ general_end:; if (LLVMIsALoadInst(val) && (src_size >= dst_size && src_align >= dst_align)) { LLVMValueRef val_ptr = LLVMGetOperand(val, 0); val_ptr = LLVMBuildPointerCast(p->builder, val_ptr, LLVMPointerType(dst_type, 0), ""); - LLVMValueRef loaded_val = LLVMBuildLoad(p->builder, val_ptr, ""); + LLVMValueRef loaded_val = LLVMBuildLoad2(p->builder, dst_type, val_ptr, ""); // LLVMSetAlignment(loaded_val, gb_min(src_align, dst_align)); @@ -2320,7 +2456,7 @@ general_end:; LLVMValueRef nptr = LLVMBuildPointerCast(p->builder, ptr, LLVMPointerType(src_type, 0), ""); LLVMBuildStore(p->builder, val, nptr); - return LLVMBuildLoad(p->builder, ptr, ""); + return LLVMBuildLoad2(p->builder, dst_type, ptr, ""); } } @@ -2346,11 +2482,15 @@ LLVMValueRef lb_find_or_add_entity_string_ptr(lbModule *m, String const &str) { isize len = gb_snprintf(name, max_len, "csbs$%x", id); len -= 1; - LLVMValueRef global_data = LLVMAddGlobal(m->mod, LLVMTypeOf(data), name); + LLVMTypeRef type = LLVMTypeOf(data); + LLVMValueRef global_data = LLVMAddGlobal(m->mod, type, name); LLVMSetInitializer(global_data, data); LLVMSetLinkage(global_data, LLVMPrivateLinkage); + LLVMSetUnnamedAddress(global_data, LLVMGlobalUnnamedAddr); + LLVMSetAlignment(global_data, 1); + LLVMSetGlobalConstant(global_data, true); - LLVMValueRef ptr = LLVMConstInBoundsGEP(global_data, indices, 2); + LLVMValueRef ptr = LLVMConstInBoundsGEP2(type, global_data, indices, 2); string_map_set(&m->const_strings, key, ptr); return ptr; } @@ -2388,13 +2528,17 @@ lbValue lb_find_or_add_entity_string_byte_slice(lbModule *m, String const &str) isize len = gb_snprintf(name, max_len, "csbs$%x", id); len -= 1; } - LLVMValueRef global_data = LLVMAddGlobal(m->mod, LLVMTypeOf(data), name); + LLVMTypeRef type = LLVMTypeOf(data); + LLVMValueRef global_data = LLVMAddGlobal(m->mod, type, name); LLVMSetInitializer(global_data, data); LLVMSetLinkage(global_data, LLVMPrivateLinkage); + LLVMSetUnnamedAddress(global_data, LLVMGlobalUnnamedAddr); + LLVMSetAlignment(global_data, 1); + LLVMSetGlobalConstant(global_data, true); LLVMValueRef ptr = nullptr; if (str.len != 0) { - ptr = LLVMConstInBoundsGEP(global_data, indices, 2); + ptr = LLVMConstInBoundsGEP2(type, global_data, indices, 2); } else { ptr = LLVMConstNull(lb_type(m, t_u8_ptr)); } @@ -2627,6 +2771,7 @@ lbValue lb_generate_global_array(lbModule *m, Type *elem_type, i64 count, String g.type = alloc_type_pointer(t); LLVMSetInitializer(g.value, LLVMConstNull(lb_type(m, t))); LLVMSetLinkage(g.value, LLVMPrivateLinkage); + LLVMSetUnnamedAddress(g.value, LLVMGlobalUnnamedAddr); string_map_set(&m->members, s, g); return g; } @@ -2709,20 +2854,18 @@ lbAddr lb_add_local(lbProcedure *p, Type *type, Entity *e, bool zero_init, i32 p if (!zero_init && !force_no_init) { // If there is any padding of any kind, just zero init regardless of zero_init parameter LLVMTypeKind kind = LLVMGetTypeKind(llvm_type); + if (kind == LLVMArrayTypeKind) { + kind = LLVMGetTypeKind(lb_type(p->module, core_array_type(type))); + } + if (kind == LLVMStructTypeKind) { i64 sz = type_size_of(type); if (type_size_of_struct_pretend_is_packed(type) != sz) { zero_init = true; } - } else if (kind == LLVMArrayTypeKind) { - zero_init = true; } } - if (zero_init) { - lb_mem_zero_ptr(p, ptr, type, alignment); - } - lbValue val = {}; val.value = ptr; val.type = alloc_type_pointer(type); @@ -2732,6 +2875,10 @@ lbAddr lb_add_local(lbProcedure *p, Type *type, Entity *e, bool zero_init, i32 p lb_add_debug_local_variable(p, ptr, type, e->token); } + if (zero_init) { + lb_mem_zero_ptr(p, ptr, type, alignment); + } + return lb_addr(val); } diff --git a/src/llvm_backend_opt.cpp b/src/llvm_backend_opt.cpp index 6b80b21d6..e2f51b868 100644 --- a/src/llvm_backend_opt.cpp +++ b/src/llvm_backend_opt.cpp @@ -62,7 +62,9 @@ void lb_basic_populate_function_pass_manager(LLVMPassManagerRef fpm, i32 optimiz LLVMAddPromoteMemoryToRegisterPass(fpm); LLVMAddMergedLoadStoreMotionPass(fpm); LLVM_ADD_CONSTANT_VALUE_PASS(fpm); - LLVMAddEarlyCSEPass(fpm); + if (!build_context.ODIN_DEBUG) { + LLVMAddEarlyCSEPass(fpm); + } } } diff --git a/src/llvm_backend_proc.cpp b/src/llvm_backend_proc.cpp index 75ca77641..392ff14c2 100644 --- a/src/llvm_backend_proc.cpp +++ b/src/llvm_backend_proc.cpp @@ -1,3 +1,13 @@ + +LLVMValueRef lb_call_intrinsic(lbProcedure *p, const char *name, LLVMValueRef* args, unsigned arg_count, LLVMTypeRef* types, unsigned type_count) +{ + unsigned id = LLVMLookupIntrinsicID(name, gb_strlen(name)); + GB_ASSERT_MSG(id != 0, "Unable to find %s", name); + LLVMValueRef ip = LLVMGetIntrinsicDeclaration(p->module->mod, id, types, type_count); + LLVMTypeRef call_type = LLVMIntrinsicGetType(p->module->ctx, id, types, type_count); + return LLVMBuildCall2(p->builder, call_type, ip, args, arg_count, ""); +} + void lb_mem_copy_overlapping(lbProcedure *p, lbValue dst, lbValue src, lbValue len, bool is_volatile) { dst = lb_emit_conv(p, dst, t_rawptr); src = lb_emit_conv(p, src, t_rawptr); @@ -10,23 +20,23 @@ void lb_mem_copy_overlapping(lbProcedure *p, lbValue dst, lbValue src, lbValue l name = "llvm.memmove.inline"; } } - LLVMTypeRef types[3] = { lb_type(p->module, t_rawptr), lb_type(p->module, t_rawptr), lb_type(p->module, t_int) }; - unsigned id = LLVMLookupIntrinsicID(name, gb_strlen(name)); - GB_ASSERT_MSG(id != 0, "Unable to find %s.%s.%s.%s", name, LLVMPrintTypeToString(types[0]), LLVMPrintTypeToString(types[1]), LLVMPrintTypeToString(types[2])); - LLVMValueRef ip = LLVMGetIntrinsicDeclaration(p->module->mod, id, types, gb_count_of(types)); + LLVMValueRef args[4] = { + dst.value, + src.value, + len.value, + LLVMConstInt(LLVMInt1TypeInContext(p->module->ctx), 0, is_volatile) + }; - LLVMValueRef args[4] = {}; - args[0] = dst.value; - args[1] = src.value; - args[2] = len.value; - args[3] = LLVMConstInt(LLVMInt1TypeInContext(p->module->ctx), 0, is_volatile); - LLVMBuildCall(p->builder, ip, args, gb_count_of(args), ""); + lb_call_intrinsic(p, name, args, gb_count_of(args), types, gb_count_of(types)); } + + + void lb_mem_copy_non_overlapping(lbProcedure *p, lbValue dst, lbValue src, lbValue len, bool is_volatile) { dst = lb_emit_conv(p, dst, t_rawptr); src = lb_emit_conv(p, src, t_rawptr); @@ -45,16 +55,14 @@ void lb_mem_copy_non_overlapping(lbProcedure *p, lbValue dst, lbValue src, lbVal lb_type(p->module, t_rawptr), lb_type(p->module, t_int) }; - unsigned id = LLVMLookupIntrinsicID(name, gb_strlen(name)); - GB_ASSERT_MSG(id != 0, "Unable to find %s.%s.%s.%s", name, LLVMPrintTypeToString(types[0]), LLVMPrintTypeToString(types[1]), LLVMPrintTypeToString(types[2])); - LLVMValueRef ip = LLVMGetIntrinsicDeclaration(p->module->mod, id, types, gb_count_of(types)); - LLVMValueRef args[4] = {}; - args[0] = dst.value; - args[1] = src.value; - args[2] = len.value; - args[3] = LLVMConstInt(LLVMInt1TypeInContext(p->module->ctx), 0, is_volatile); - LLVMBuildCall(p->builder, ip, args, gb_count_of(args), ""); + LLVMValueRef args[4] = { + dst.value, + src.value, + len.value, + LLVMConstInt(LLVMInt1TypeInContext(p->module->ctx), 0, is_volatile) }; + + lb_call_intrinsic(p, name, args, gb_count_of(args), types, gb_count_of(types)); } @@ -113,14 +121,15 @@ lbProcedure *lb_create_procedure(lbModule *m, Entity *entity, bool ignore_body) p->branch_blocks.allocator = a; p->context_stack.allocator = a; p->scope_stack.allocator = a; + map_init(&p->selector_values, a, 0); + map_init(&p->selector_addr, a, 0); if (p->is_foreign) { lb_add_foreign_library_path(p->module, entity->Procedure.foreign_library); } char *c_link_name = alloc_cstring(permanent_allocator(), p->name); - LLVMTypeRef func_ptr_type = lb_type(m, p->type); - LLVMTypeRef func_type = LLVMGetElementType(func_ptr_type); + LLVMTypeRef func_type = lb_get_procedure_raw_type(m, p->type); p->value = LLVMAddFunction(m->mod, c_link_name, func_type); @@ -344,8 +353,7 @@ lbProcedure *lb_create_dummy_procedure(lbModule *m, String link_name, Type *type char *c_link_name = alloc_cstring(permanent_allocator(), p->name); - LLVMTypeRef func_ptr_type = lb_type(m, p->type); - LLVMTypeRef func_type = LLVMGetElementType(func_ptr_type); + LLVMTypeRef func_type = lb_get_procedure_raw_type(m, p->type); p->value = LLVMAddFunction(m->mod, c_link_name, func_type); @@ -433,6 +441,40 @@ void lb_start_block(lbProcedure *p, lbBlock *b) { p->curr_block = b; } +void lb_set_debug_position_to_procedure_begin(lbProcedure *p) { + if (p->debug_info == nullptr) { + return; + } + TokenPos pos = {}; + if (p->body != nullptr) { + pos = ast_token(p->body).pos; + } else if (p->type_expr != nullptr) { + pos = ast_token(p->type_expr).pos; + } else if (p->entity != nullptr) { + pos = p->entity->token.pos; + } + if (pos.file_id != 0) { + LLVMSetCurrentDebugLocation2(p->builder, lb_debug_location_from_token_pos(p, pos)); + } +} + +void lb_set_debug_position_to_procedure_end(lbProcedure *p) { + if (p->debug_info == nullptr) { + return; + } + TokenPos pos = {}; + if (p->body != nullptr) { + pos = ast_end_token(p->body).pos; + } else if (p->type_expr != nullptr) { + pos = ast_end_token(p->type_expr).pos; + } else if (p->entity != nullptr) { + pos = p->entity->token.pos; + } + if (pos.file_id != 0) { + LLVMSetCurrentDebugLocation2(p->builder, lb_debug_location_from_token_pos(p, pos)); + } +} + void lb_begin_procedure_body(lbProcedure *p) { DeclInfo *decl = decl_info_of_entity(p->entity); if (decl != nullptr) { @@ -564,29 +606,21 @@ void lb_begin_procedure_body(lbProcedure *p) { lb_push_context_onto_stack_from_implicit_parameter(p); } - lb_start_block(p, p->entry_block); - + lb_set_debug_position_to_procedure_begin(p); if (p->debug_info != nullptr) { - TokenPos pos = {}; - if (p->body != nullptr) { - pos = ast_token(p->body).pos; - } else if (p->type_expr != nullptr) { - pos = ast_token(p->type_expr).pos; - } else if (p->entity != nullptr) { - pos = p->entity->token.pos; - } - if (pos.file_id != 0) { - LLVMSetCurrentDebugLocation2(p->builder, lb_debug_location_from_token_pos(p, pos)); - } - if (p->context_stack.count != 0) { + p->curr_block = p->decl_block; lb_add_debug_context_variable(p, lb_find_or_generate_context_ptr(p)); } } + + lb_start_block(p, p->entry_block); } void lb_end_procedure_body(lbProcedure *p) { + lb_set_debug_position_to_procedure_begin(p); + LLVMPositionBuilderAtEnd(p->builder, p->decl_block->block); LLVMBuildBr(p->builder, p->entry_block->block); LLVMPositionBuilderAtEnd(p->builder, p->curr_block->block); @@ -598,6 +632,7 @@ void lb_end_procedure_body(lbProcedure *p) { instr = LLVMGetLastInstruction(p->curr_block->block); if (!lb_is_instr_terminating(instr)) { lb_emit_defer_stmts(p, lbDeferExit_Return, nullptr); + lb_set_debug_position_to_procedure_end(p); LLVMBuildRetVoid(p->builder); } } @@ -716,12 +751,12 @@ lbValue lb_emit_call_internal(lbProcedure *p, lbValue value, lbValue return_ptr, GB_ASSERT(curr_block != p->decl_block->block); { - LLVMTypeRef ftp = lb_type(p->module, value.type); + LLVMTypeRef fnp = lb_type_internal_for_procedures_raw(p->module, value.type); + LLVMTypeRef ftp = LLVMPointerType(fnp, 0); LLVMValueRef fn = value.value; if (!lb_is_type_kind(LLVMTypeOf(value.value), LLVMFunctionTypeKind)) { fn = LLVMBuildPointerCast(p->builder, fn, ftp, ""); } - LLVMTypeRef fnp = LLVMGetElementType(LLVMTypeOf(fn)); GB_ASSERT_MSG(lb_is_type_kind(fnp, LLVMFunctionTypeKind), "%s", LLVMPrintTypeToString(fnp)); { @@ -1235,13 +1270,8 @@ lbValue lb_build_builtin_simd_proc(lbProcedure *p, Ast *expr, TypeAndValue const } args[args_count++] = arg0.value; - LLVMTypeRef types[1] = {lb_type(p->module, arg0.type)}; - unsigned id = LLVMLookupIntrinsicID(name, gb_strlen(name)); - GB_ASSERT_MSG(id != 0, "Unable to find %s.%s", name, LLVMPrintTypeToString(types[0])); - LLVMValueRef ip = LLVMGetIntrinsicDeclaration(p->module->mod, id, types, gb_count_of(types)); - - res.value = LLVMBuildCall(p->builder, ip, args, cast(unsigned)args_count, ""); + res.value = lb_call_intrinsic(p, name, args, cast(unsigned)args_count, types, gb_count_of(types)); return res; } case BuiltinProc_simd_reduce_min: @@ -1274,15 +1304,11 @@ lbValue lb_build_builtin_simd_proc(lbProcedure *p, Ast *expr, TypeAndValue const case BuiltinProc_simd_reduce_or: name = "llvm.vector.reduce.or"; break; case BuiltinProc_simd_reduce_xor: name = "llvm.vector.reduce.xor"; break; } - LLVMTypeRef types[1] = {lb_type(p->module, arg0.type)}; - unsigned id = LLVMLookupIntrinsicID(name, gb_strlen(name)); - GB_ASSERT_MSG(id != 0, "Unable to find %s.%s", name, LLVMPrintTypeToString(types[0])); - LLVMValueRef ip = LLVMGetIntrinsicDeclaration(p->module->mod, id, types, gb_count_of(types)); - LLVMValueRef args[1] = {}; - args[0] = arg0.value; + LLVMTypeRef types[1] = { lb_type(p->module, arg0.type) }; + LLVMValueRef args[1] = { arg0.value }; - res.value = LLVMBuildCall(p->builder, ip, args, gb_count_of(args), ""); + res.value = lb_call_intrinsic(p, name, args, gb_count_of(args), types, gb_count_of(types)); return res; } @@ -1331,15 +1357,10 @@ lbValue lb_build_builtin_simd_proc(lbProcedure *p, Ast *expr, TypeAndValue const case BuiltinProc_simd_nearest: name = "llvm.nearbyint"; break; } - LLVMTypeRef types[1] = {lb_type(p->module, arg0.type)}; - unsigned id = LLVMLookupIntrinsicID(name, gb_strlen(name)); - GB_ASSERT_MSG(id != 0, "Unable to find %s.%s", name, LLVMPrintTypeToString(types[0])); - LLVMValueRef ip = LLVMGetIntrinsicDeclaration(p->module->mod, id, types, gb_count_of(types)); + LLVMTypeRef types[1] = { lb_type(p->module, arg0.type) }; + LLVMValueRef args[1] = { arg0.value }; - LLVMValueRef args[1] = {}; - args[0] = arg0.value; - - res.value = LLVMBuildCall(p->builder, ip, args, gb_count_of(args), ""); + res.value = lb_call_intrinsic(p, name, args, gb_count_of(args), types, gb_count_of(types)); return res; } @@ -1403,15 +1424,10 @@ lbValue lb_build_builtin_simd_proc(lbProcedure *p, Ast *expr, TypeAndValue const } LLVMTypeRef types[1] = {lb_type(p->module, arg0.type)}; - unsigned id = LLVMLookupIntrinsicID(name, gb_strlen(name)); - GB_ASSERT_MSG(id != 0, "Unable to find %s.%s", name, LLVMPrintTypeToString(types[0])); - LLVMValueRef ip = LLVMGetIntrinsicDeclaration(p->module->mod, id, types, gb_count_of(types)); - LLVMValueRef args[2] = {}; - args[0] = arg0.value; - args[1] = arg1.value; + LLVMValueRef args[2] = { arg0.value, arg1.value }; - res.value = LLVMBuildCall(p->builder, ip, args, gb_count_of(args), ""); + res.value = lb_call_intrinsic(p, name, args, gb_count_of(args), types, gb_count_of(types)); return res; } @@ -1822,6 +1838,11 @@ lbValue lb_build_builtin_proc(lbProcedure *p, Ast *expr, TypeAndValue const &tv, return lb_emit_matrix_flatten(p, m, tv.type); } + case BuiltinProc_unreachable: + lb_emit_unreachable(p); + return {}; + + // "Intrinsics" case BuiltinProc_alloca: @@ -1869,11 +1890,7 @@ lbValue lb_build_builtin_proc(lbProcedure *p, Ast *expr, TypeAndValue const &tv, case BuiltinProc_trap: name = "llvm.trap"; break; } - unsigned id = LLVMLookupIntrinsicID(name, gb_strlen(name)); - GB_ASSERT_MSG(id != 0, "Unable to find %s", name); - LLVMValueRef ip = LLVMGetIntrinsicDeclaration(p->module->mod, id, nullptr, 0); - - LLVMBuildCall(p->builder, ip, nullptr, 0, ""); + lb_call_intrinsic(p, name, nullptr, 0, nullptr, 0); if (id == BuiltinProc_trap) { LLVMBuildUnreachable(p->builder); } @@ -1893,11 +1910,7 @@ lbValue lb_build_builtin_proc(lbProcedure *p, Ast *expr, TypeAndValue const &tv, res.value = LLVMBuildCall2(p->builder, func_type, the_asm, nullptr, 0, ""); } else { char const *name = "llvm.readcyclecounter"; - unsigned id = LLVMLookupIntrinsicID(name, gb_strlen(name)); - GB_ASSERT_MSG(id != 0, "Unable to find %s", name); - LLVMValueRef ip = LLVMGetIntrinsicDeclaration(p->module->mod, id, nullptr, 0); - - res.value = LLVMBuildCall(p->builder, ip, nullptr, 0, ""); + res.value = lb_call_intrinsic(p, name, nullptr, 0, nullptr, 0); } return res; } @@ -1952,16 +1965,11 @@ lbValue lb_build_builtin_proc(lbProcedure *p, Ast *expr, TypeAndValue const &tv, } } LLVMTypeRef types[1] = {lb_type(p->module, type)}; - unsigned id = LLVMLookupIntrinsicID(name, gb_strlen(name)); - GB_ASSERT_MSG(id != 0, "Unable to find %s.%s", name, LLVMPrintTypeToString(types[0])); - LLVMValueRef ip = LLVMGetIntrinsicDeclaration(p->module->mod, id, types, gb_count_of(types)); - LLVMValueRef args[2] = {}; - args[0] = x.value; - args[1] = y.value; + LLVMValueRef args[2] = { x.value, y.value }; lbValue res = {}; - res.value = LLVMBuildCall(p->builder, ip, args, gb_count_of(args), ""); + res.value = lb_call_intrinsic(p, name, args, gb_count_of(args), types, gb_count_of(types)); if (is_type_tuple(main_type)) { Type *res_type = nullptr; @@ -1988,15 +1996,11 @@ lbValue lb_build_builtin_proc(lbProcedure *p, Ast *expr, TypeAndValue const &tv, char const *name = "llvm.sqrt"; LLVMTypeRef types[1] = {lb_type(p->module, type)}; - unsigned id = LLVMLookupIntrinsicID(name, gb_strlen(name)); - GB_ASSERT_MSG(id != 0, "Unable to find %s.%s", name, LLVMPrintTypeToString(types[0])); - LLVMValueRef ip = LLVMGetIntrinsicDeclaration(p->module->mod, id, types, gb_count_of(types)); - LLVMValueRef args[1] = {}; - args[0] = x.value; + LLVMValueRef args[1] = { x.value }; lbValue res = {}; - res.value = LLVMBuildCall(p->builder, ip, args, gb_count_of(args), ""); + res.value = lb_call_intrinsic(p, name, args, gb_count_of(args), types, gb_count_of(types)); res.type = type; return res; } @@ -2011,17 +2015,11 @@ lbValue lb_build_builtin_proc(lbProcedure *p, Ast *expr, TypeAndValue const &tv, char const *name = "llvm.fma"; LLVMTypeRef types[1] = {lb_type(p->module, type)}; - unsigned id = LLVMLookupIntrinsicID(name, gb_strlen(name)); - GB_ASSERT_MSG(id != 0, "Unable to find %s.%s", name, LLVMPrintTypeToString(types[0])); - LLVMValueRef ip = LLVMGetIntrinsicDeclaration(p->module->mod, id, types, gb_count_of(types)); - LLVMValueRef args[3] = {}; - args[0] = x.value; - args[1] = y.value; - args[2] = z.value; + LLVMValueRef args[3] = { x.value, y.value, z.value }; lbValue res = {}; - res.value = LLVMBuildCall(p->builder, ip, args, gb_count_of(args), ""); + res.value = lb_call_intrinsic(p, name, args, gb_count_of(args), types, gb_count_of(types)); res.type = type; return res; } @@ -2080,7 +2078,7 @@ lbValue lb_build_builtin_proc(lbProcedure *p, Ast *expr, TypeAndValue const &tv, lbValue res = {}; res.type = tv.type; - res.value = LLVMBuildGEP(p->builder, ptr.value, indices, gb_count_of(indices), ""); + res.value = LLVMBuildGEP2(p->builder, lb_type(p->module, type_deref(tv.type)), ptr.value, indices, gb_count_of(indices), ""); return res; } case BuiltinProc_ptr_sub: @@ -2089,7 +2087,7 @@ lbValue lb_build_builtin_proc(lbProcedure *p, Ast *expr, TypeAndValue const &tv, lbValue ptr1 = lb_build_expr(p, ce->args[1]); LLVMTypeRef type_int = lb_type(p->module, t_int); - LLVMValueRef diff = LLVMBuildPtrDiff(p->builder, ptr0.value, ptr1.value, ""); + LLVMValueRef diff = LLVMBuildPtrDiff2(p->builder, lb_type(p->module, ptr0.type), ptr0.value, ptr1.value, ""); diff = LLVMBuildIntCast2(p->builder, diff, type_int, /*signed*/true, ""); lbValue res = {}; @@ -2140,7 +2138,7 @@ lbValue lb_build_builtin_proc(lbProcedure *p, Ast *expr, TypeAndValue const &tv, case BuiltinProc_atomic_load_explicit: { lbValue dst = lb_build_expr(p, ce->args[0]); - LLVMValueRef instr = LLVMBuildLoad(p->builder, dst.value, ""); + LLVMValueRef instr = LLVMBuildLoad2(p->builder, lb_type(p->module, type_deref(dst.type)), dst.value, ""); switch (id) { case BuiltinProc_non_temporal_load: { @@ -2314,18 +2312,14 @@ lbValue lb_build_builtin_proc(lbProcedure *p, Ast *expr, TypeAndValue const &tv, GB_ASSERT(name != nullptr); LLVMTypeRef types[1] = {lb_type(p->module, platform_type)}; - unsigned id = LLVMLookupIntrinsicID(name, gb_strlen(name)); - GB_ASSERT_MSG(id != 0, "Unable to find %s.%s", name, LLVMPrintTypeToString(types[0])); - LLVMValueRef ip = LLVMGetIntrinsicDeclaration(p->module->mod, id, types, gb_count_of(types)); - lbValue res = {}; - LLVMValueRef args[3] = {}; - args[0] = x.value; - args[1] = y.value; - args[2] = scale.value; + LLVMValueRef args[3] = { + x.value, + y.value, + scale.value }; - res.value = LLVMBuildCall(p->builder, ip, args, gb_count_of(args), ""); + res.value = lb_call_intrinsic(p, name, args, gb_count_of(args), types, gb_count_of(types)); res.type = platform_type; return lb_emit_conv(p, res, tv.type); } @@ -2339,17 +2333,10 @@ lbValue lb_build_builtin_proc(lbProcedure *p, Ast *expr, TypeAndValue const &tv, char const *name = "llvm.expect"; LLVMTypeRef types[1] = {lb_type(p->module, t)}; - unsigned id = LLVMLookupIntrinsicID(name, gb_strlen(name)); - GB_ASSERT_MSG(id != 0, "Unable to find %s.%s", name, LLVMPrintTypeToString(types[0])); - LLVMValueRef ip = LLVMGetIntrinsicDeclaration(p->module->mod, id, types, gb_count_of(types)); - lbValue res = {}; + LLVMValueRef args[2] = { x.value, y.value }; - LLVMValueRef args[2] = {}; - args[0] = x.value; - args[1] = y.value; - - res.value = LLVMBuildCall(p->builder, ip, args, gb_count_of(args), ""); + res.value = lb_call_intrinsic(p, name, args, gb_count_of(args), types, gb_count_of(types)); res.type = t; return lb_emit_conv(p, res, t); } @@ -2385,9 +2372,6 @@ lbValue lb_build_builtin_proc(lbProcedure *p, Ast *expr, TypeAndValue const &tv, char const *name = "llvm.prefetch"; LLVMTypeRef types[1] = {lb_type(p->module, t_rawptr)}; - unsigned id = LLVMLookupIntrinsicID(name, gb_strlen(name)); - GB_ASSERT_MSG(id != 0, "Unable to find %s.%s", name, LLVMPrintTypeToString(types[0])); - LLVMValueRef ip = LLVMGetIntrinsicDeclaration(p->module->mod, id, types, gb_count_of(types)); LLVMTypeRef llvm_i32 = lb_type(p->module, t_i32); LLVMValueRef args[4] = {}; @@ -2397,7 +2381,7 @@ lbValue lb_build_builtin_proc(lbProcedure *p, Ast *expr, TypeAndValue const &tv, args[3] = LLVMConstInt(llvm_i32, cache, false); lbValue res = {}; - res.value = LLVMBuildCall(p->builder, ip, args, gb_count_of(args), ""); + res.value = lb_call_intrinsic(p, name, args, gb_count_of(args), types, gb_count_of(types)); res.type = nullptr; return res; } @@ -2643,7 +2627,8 @@ lbValue lb_build_builtin_proc(lbProcedure *p, Ast *expr, TypeAndValue const &tv, isize len = gb_snprintf(name, max_len, "csbs$%x", id); len -= 1; } - LLVMValueRef global_data = LLVMAddGlobal(m->mod, LLVMTypeOf(array), name); + LLVMTypeRef type = LLVMTypeOf(array); + LLVMValueRef global_data = LLVMAddGlobal(m->mod, type, name); LLVMSetInitializer(global_data, array); LLVMSetLinkage(global_data, LLVMInternalLinkage); @@ -2655,7 +2640,7 @@ lbValue lb_build_builtin_proc(lbProcedure *p, Ast *expr, TypeAndValue const &tv, }; lbValue res = {}; res.type = tv.type; - res.value = LLVMBuildInBoundsGEP(p->builder, global_data, indices, gb_count_of(indices), ""); + res.value = LLVMBuildInBoundsGEP2(p->builder, type, global_data, indices, gb_count_of(indices), ""); return res; } @@ -2666,9 +2651,6 @@ lbValue lb_build_builtin_proc(lbProcedure *p, Ast *expr, TypeAndValue const &tv, LLVMTypeRef types[1] = { lb_type(p->module, t_uintptr), }; - unsigned id = LLVMLookupIntrinsicID(name, gb_strlen(name)); - GB_ASSERT_MSG(id != 0, "Unable to find %s", name, LLVMPrintTypeToString(types[0])); - LLVMValueRef ip = LLVMGetIntrinsicDeclaration(p->module->mod, id, types, gb_count_of(types)); LLVMValueRef args[2] = {}; args[0] = lb_emit_conv(p, lb_build_expr(p, ce->args[0]), t_uintptr).value; @@ -2676,7 +2658,7 @@ lbValue lb_build_builtin_proc(lbProcedure *p, Ast *expr, TypeAndValue const &tv, lbValue res = {}; res.type = tv.type; - res.value = LLVMBuildCall(p->builder, ip, args, gb_count_of(args), ""); + res.value = lb_call_intrinsic(p, name, args, gb_count_of(args), types, gb_count_of(types)); return res; } case BuiltinProc_wasm_memory_size: @@ -2685,16 +2667,13 @@ lbValue lb_build_builtin_proc(lbProcedure *p, Ast *expr, TypeAndValue const &tv, LLVMTypeRef types[1] = { lb_type(p->module, t_uintptr), }; - unsigned id = LLVMLookupIntrinsicID(name, gb_strlen(name)); - GB_ASSERT_MSG(id != 0, "Unable to find %s", name, LLVMPrintTypeToString(types[0])); - LLVMValueRef ip = LLVMGetIntrinsicDeclaration(p->module->mod, id, types, gb_count_of(types)); LLVMValueRef args[1] = {}; args[0] = lb_emit_conv(p, lb_build_expr(p, ce->args[0]), t_uintptr).value; lbValue res = {}; res.type = tv.type; - res.value = LLVMBuildCall(p->builder, ip, args, gb_count_of(args), ""); + res.value = lb_call_intrinsic(p, name, args, gb_count_of(args), types, gb_count_of(types)); return res; } @@ -2704,9 +2683,6 @@ lbValue lb_build_builtin_proc(lbProcedure *p, Ast *expr, TypeAndValue const &tv, LLVMTypeRef types[1] = { lb_type(p->module, t_u32), }; - unsigned id = LLVMLookupIntrinsicID(name, gb_strlen(name)); - GB_ASSERT_MSG(id != 0, "Unable to find %s", name, LLVMPrintTypeToString(types[0])); - LLVMValueRef ip = LLVMGetIntrinsicDeclaration(p->module->mod, id, nullptr, 0); // types, gb_count_of(types)); Type *t_u32_ptr = alloc_type_pointer(t_u32); @@ -2717,7 +2693,7 @@ lbValue lb_build_builtin_proc(lbProcedure *p, Ast *expr, TypeAndValue const &tv, lbValue res = {}; res.type = tv.type; - res.value = LLVMBuildCall(p->builder, ip, args, gb_count_of(args), ""); + res.value = lb_call_intrinsic(p, name, args, gb_count_of(args), types, gb_count_of(types)); return res; } @@ -2727,19 +2703,16 @@ lbValue lb_build_builtin_proc(lbProcedure *p, Ast *expr, TypeAndValue const &tv, LLVMTypeRef types[1] = { lb_type(p->module, t_u32), }; - unsigned id = LLVMLookupIntrinsicID(name, gb_strlen(name)); - GB_ASSERT_MSG(id != 0, "Unable to find %s", name, LLVMPrintTypeToString(types[0])); - LLVMValueRef ip = LLVMGetIntrinsicDeclaration(p->module->mod, id, nullptr, 0); // types, gb_count_of(types)); Type *t_u32_ptr = alloc_type_pointer(t_u32); - LLVMValueRef args[2] = {}; - args[0] = lb_emit_conv(p, lb_build_expr(p, ce->args[0]), t_u32_ptr).value; - args[1] = lb_emit_conv(p, lb_build_expr(p, ce->args[1]), t_u32).value; + LLVMValueRef args[2] = { + lb_emit_conv(p, lb_build_expr(p, ce->args[0]), t_u32_ptr).value, + lb_emit_conv(p, lb_build_expr(p, ce->args[1]), t_u32).value }; lbValue res = {}; res.type = tv.type; - res.value = LLVMBuildCall(p->builder, ip, args, gb_count_of(args), ""); + res.value = lb_call_intrinsic(p, name, args, gb_count_of(args), types, gb_count_of(types)); return res; } @@ -2748,7 +2721,7 @@ lbValue lb_build_builtin_proc(lbProcedure *p, Ast *expr, TypeAndValue const &tv, { Type *param_types[2] = {t_u32, t_u32}; Type *type = alloc_type_proc_from_types(param_types, gb_count_of(param_types), tv.type, false, ProcCC_None); - LLVMTypeRef func_type = LLVMGetElementType(lb_type(p->module, type)); + LLVMTypeRef func_type = lb_get_procedure_raw_type(p->module, type); LLVMValueRef the_asm = llvm_get_inline_asm( func_type, str_lit("cpuid"), @@ -2768,7 +2741,7 @@ lbValue lb_build_builtin_proc(lbProcedure *p, Ast *expr, TypeAndValue const &tv, case BuiltinProc_x86_xgetbv: { Type *type = alloc_type_proc_from_types(&t_u32, 1, tv.type, false, ProcCC_None); - LLVMTypeRef func_type = LLVMGetElementType(lb_type(p->module, type)); + LLVMTypeRef func_type = lb_get_procedure_raw_type(p->module, type); LLVMValueRef the_asm = llvm_get_inline_asm( func_type, str_lit("xgetbv"), @@ -2832,10 +2805,6 @@ lbValue lb_build_call_expr(lbProcedure *p, Ast *expr) { expr = unparen_expr(expr); ast_node(ce, CallExpr, expr); - if (ce->sce_temp_data) { - return *(lbValue *)ce->sce_temp_data; - } - lbValue res = lb_build_call_expr_internal(p, expr); if (ce->optional_ok_one) { // TODO(bill): Minor hack for #optional_ok procedures diff --git a/src/llvm_backend_stmt.cpp b/src/llvm_backend_stmt.cpp index 2afb5300b..f131bb3db 100644 --- a/src/llvm_backend_stmt.cpp +++ b/src/llvm_backend_stmt.cpp @@ -1210,8 +1210,8 @@ void lb_build_switch_stmt(lbProcedure *p, AstSwitchStmt *ss, Scope *scope) { } lb_emit_jump(p, done); - lb_close_scope(p, lbDeferExit_Default, done); lb_start_block(p, done); + lb_close_scope(p, lbDeferExit_Default, done); } void lb_store_type_case_implicit(lbProcedure *p, Ast *clause, lbValue value) { @@ -1253,7 +1253,6 @@ void lb_type_case_body(lbProcedure *p, Ast *label, Ast *clause, lbBlock *body, l ast_node(cc, CaseClause, clause); lb_push_target_list(p, label, done, nullptr, nullptr); - lb_open_scope(p, body->scope); lb_build_stmt_list(p, cc->stmts); lb_close_scope(p, lbDeferExit_Default, body); lb_pop_target_list(p); @@ -1263,6 +1262,7 @@ void lb_type_case_body(lbProcedure *p, Ast *label, Ast *clause, lbBlock *body, l void lb_build_type_switch_stmt(lbProcedure *p, AstTypeSwitchStmt *ss) { lbModule *m = p->module; + lb_open_scope(p, ss->scope); ast_node(as, AssignStmt, ss->tag); GB_ASSERT(as->lhs.count == 1); @@ -1321,6 +1321,7 @@ void lb_build_type_switch_stmt(lbProcedure *p, AstTypeSwitchStmt *ss) { for_array(i, body->stmts) { Ast *clause = body->stmts[i]; ast_node(cc, CaseClause, clause); + lb_open_scope(p, cc->scope); if (cc->list.count == 0) { lb_start_block(p, default_block); lb_store_type_case_implicit(p, clause, parent_value); @@ -1329,6 +1330,9 @@ void lb_build_type_switch_stmt(lbProcedure *p, AstTypeSwitchStmt *ss) { } lbBlock *body = lb_create_block(p, "typeswitch.body"); + if (p->debug_info != nullptr) { + LLVMSetCurrentDebugLocation2(p->builder, lb_debug_location_from_ast(p, clause)); + } Type *case_type = nullptr; for_array(type_index, cc->list) { case_type = type_of_expr(cc->list[type_index]); @@ -1375,6 +1379,7 @@ void lb_build_type_switch_stmt(lbProcedure *p, AstTypeSwitchStmt *ss) { lb_emit_jump(p, done); lb_start_block(p, done); + lb_close_scope(p, lbDeferExit_Default, done); } @@ -1721,6 +1726,9 @@ void lb_build_for_stmt(lbProcedure *p, Ast *node) { ast_node(fs, ForStmt, node); lb_open_scope(p, fs->scope); // Open Scope here + if (p->debug_info != nullptr) { + LLVMSetCurrentDebugLocation2(p->builder, lb_debug_location_from_ast(p, node)); + } if (fs->init != nullptr) { #if 1 @@ -1741,11 +1749,14 @@ void lb_build_for_stmt(lbProcedure *p, Ast *node) { post = lb_create_block(p, "for.post"); } - lb_emit_jump(p, loop); lb_start_block(p, loop); if (loop != body) { + // right now the condition (all expressions) will not set it's debug location, so we will do it here + if (p->debug_info != nullptr) { + LLVMSetCurrentDebugLocation2(p->builder, lb_debug_location_from_ast(p, fs->cond)); + } lb_build_cond(p, fs->cond, body, done); lb_start_block(p, body); } @@ -1753,10 +1764,12 @@ void lb_build_for_stmt(lbProcedure *p, Ast *node) { lb_push_target_list(p, fs->label, done, post, nullptr); lb_build_stmt(p, fs->body); - lb_close_scope(p, lbDeferExit_Default, nullptr); lb_pop_target_list(p); + if (p->debug_info != nullptr) { + LLVMSetCurrentDebugLocation2(p->builder, lb_debug_end_location_from_ast(p, fs->body)); + } lb_emit_jump(p, post); if (fs->post != nullptr) { @@ -1766,6 +1779,7 @@ void lb_build_for_stmt(lbProcedure *p, Ast *node) { } lb_start_block(p, done); + lb_close_scope(p, lbDeferExit_Default, nullptr); } void lb_build_assign_stmt_array(lbProcedure *p, TokenKind op, lbAddr const &lhs, lbValue const &value) { @@ -1971,14 +1985,9 @@ void lb_build_stmt(lbProcedure *p, Ast *node) { } } - LLVMMetadataRef prev_debug_location = nullptr; if (p->debug_info != nullptr) { - prev_debug_location = LLVMGetCurrentDebugLocation2(p->builder); LLVMSetCurrentDebugLocation2(p->builder, lb_debug_location_from_ast(p, node)); } - defer (if (prev_debug_location != nullptr) { - LLVMSetCurrentDebugLocation2(p->builder, prev_debug_location); - }); u16 prev_state_flags = p->state_flags; defer (p->state_flags = prev_state_flags); @@ -2073,7 +2082,8 @@ void lb_build_stmt(lbProcedure *p, Ast *node) { lbAddr lval = {}; if (!is_blank_ident(name)) { Entity *e = entity_of_node(name); - bool zero_init = true; // Always do it + // bool zero_init = true; // Always do it + bool zero_init = vd->values.count == 0; lval = lb_add_local(p, e->type, e, zero_init); } array_add(&lvals, lval); diff --git a/src/llvm_backend_type.cpp b/src/llvm_backend_type.cpp index 2e7b2788a..d424fa5b2 100644 --- a/src/llvm_backend_type.cpp +++ b/src/llvm_backend_type.cpp @@ -57,6 +57,7 @@ lbValue lb_typeid(lbModule *m, Type *type) { case Type_SimdVector: kind = Typeid_Simd_Vector; break; case Type_RelativePointer: kind = Typeid_Relative_Pointer; break; case Type_RelativeSlice: kind = Typeid_Relative_Slice; break; + case Type_SoaPointer: kind = Typeid_SoaPointer; break; } if (is_type_cstring(type)) { @@ -97,34 +98,12 @@ lbValue lb_type_info(lbModule *m, Type *type) { isize index = lb_type_info_index(m->info, type); GB_ASSERT(index >= 0); - LLVMTypeRef it = lb_type(m, t_int); - LLVMValueRef indices[2] = { - LLVMConstInt(it, 0, false), - LLVMConstInt(it, index, true), - }; - - lbValue value = {}; - value.value = LLVMConstGEP(lb_global_type_info_data_ptr(m).value, indices, gb_count_of(indices)); - value.type = t_type_info_ptr; - return value; + lbValue data = lb_global_type_info_data_ptr(m); + return lb_emit_array_epi(m, data, index); } -lbValue lb_get_type_info_ptr(lbModule *m, Type *type) { - GB_ASSERT(!build_context.disallow_rtti); - - i32 index = cast(i32)lb_type_info_index(m->info, type); - GB_ASSERT(index >= 0); - // gb_printf_err("%d %s\n", index, type_to_string(type)); - - LLVMValueRef indices[2] = { - LLVMConstInt(lb_type(m, t_int), 0, false), - LLVMConstInt(lb_type(m, t_int), index, false), - }; - - lbValue res = {}; - res.type = t_type_info_ptr; - res.value = LLVMConstGEP(lb_global_type_info_data_ptr(m).value, indices, cast(unsigned)gb_count_of(indices)); - return res; +LLVMTypeRef lb_get_procedure_raw_type(lbModule *m, Type *type) { + return lb_type_internal_for_procedures_raw(m, type); } @@ -178,10 +157,10 @@ void lb_setup_type_info_data(lbProcedure *p) { // NOTE(bill): Setup type_info da LLVMValueRef indices[2] = {llvm_zero(m), llvm_zero(m)}; LLVMValueRef values[2] = { - LLVMConstInBoundsGEP(lb_global_type_info_data_ptr(m).value, indices, gb_count_of(indices)), + LLVMConstInBoundsGEP2(lb_type(m, lb_global_type_info_data_entity->type), lb_global_type_info_data_ptr(m).value, indices, gb_count_of(indices)), LLVMConstInt(lb_type(m, t_int), type->Array.count, true), }; - LLVMValueRef slice = llvm_const_named_struct_internal(llvm_addr_type(global_type_table), values, gb_count_of(values)); + LLVMValueRef slice = llvm_const_named_struct_internal(lb_type(m, type_deref(global_type_table.type)), values, gb_count_of(values)); LLVMSetInitializer(global_type_table.value, slice); } @@ -260,7 +239,7 @@ void lb_setup_type_info_data(lbProcedure *p) { // NOTE(bill): Setup type_info da LLVMValueRef vals[4] = { lb_const_string(p->module, t->Named.type_name->token.string).value, - lb_get_type_info_ptr(m, t->Named.base).value, + lb_type_info(m, t->Named.base).value, pkg_name, loc.value }; @@ -419,7 +398,7 @@ void lb_setup_type_info_data(lbProcedure *p) { // NOTE(bill): Setup type_info da case Type_Pointer: { tag = lb_const_ptr_cast(m, variant_ptr, t_type_info_pointer_ptr); - lbValue gep = lb_get_type_info_ptr(m, t->Pointer.elem); + lbValue gep = lb_type_info(m, t->Pointer.elem); LLVMValueRef vals[1] = { gep.value, @@ -433,7 +412,21 @@ void lb_setup_type_info_data(lbProcedure *p) { // NOTE(bill): Setup type_info da } case Type_MultiPointer: { tag = lb_const_ptr_cast(m, variant_ptr, t_type_info_multi_pointer_ptr); - lbValue gep = lb_get_type_info_ptr(m, t->MultiPointer.elem); + lbValue gep = lb_type_info(m, t->MultiPointer.elem); + + LLVMValueRef vals[1] = { + gep.value, + }; + + lbValue res = {}; + res.type = type_deref(tag.type); + res.value = llvm_const_named_struct(m, res.type, vals, gb_count_of(vals)); + lb_emit_store(p, tag, res); + break; + } + case Type_SoaPointer: { + tag = lb_const_ptr_cast(m, variant_ptr, t_type_info_soa_pointer_ptr); + lbValue gep = lb_type_info(m, t->SoaPointer.elem); LLVMValueRef vals[1] = { gep.value, @@ -450,7 +443,7 @@ void lb_setup_type_info_data(lbProcedure *p) { // NOTE(bill): Setup type_info da i64 ez = type_size_of(t->Array.elem); LLVMValueRef vals[3] = { - lb_get_type_info_ptr(m, t->Array.elem).value, + lb_type_info(m, t->Array.elem).value, lb_const_int(m, t_int, ez).value, lb_const_int(m, t_int, t->Array.count).value, }; @@ -465,8 +458,8 @@ void lb_setup_type_info_data(lbProcedure *p) { // NOTE(bill): Setup type_info da tag = lb_const_ptr_cast(m, variant_ptr, t_type_info_enumerated_array_ptr); LLVMValueRef vals[7] = { - lb_get_type_info_ptr(m, t->EnumeratedArray.elem).value, - lb_get_type_info_ptr(m, t->EnumeratedArray.index).value, + lb_type_info(m, t->EnumeratedArray.elem).value, + lb_type_info(m, t->EnumeratedArray.index).value, lb_const_int(m, t_int, type_size_of(t->EnumeratedArray.elem)).value, lb_const_int(m, t_int, t->EnumeratedArray.count).value, @@ -497,7 +490,7 @@ void lb_setup_type_info_data(lbProcedure *p) { // NOTE(bill): Setup type_info da tag = lb_const_ptr_cast(m, variant_ptr, t_type_info_dynamic_array_ptr); LLVMValueRef vals[2] = { - lb_get_type_info_ptr(m, t->DynamicArray.elem).value, + lb_type_info(m, t->DynamicArray.elem).value, lb_const_int(m, t_int, type_size_of(t->DynamicArray.elem)).value, }; @@ -511,7 +504,7 @@ void lb_setup_type_info_data(lbProcedure *p) { // NOTE(bill): Setup type_info da tag = lb_const_ptr_cast(m, variant_ptr, t_type_info_slice_ptr); LLVMValueRef vals[2] = { - lb_get_type_info_ptr(m, t->Slice.elem).value, + lb_type_info(m, t->Slice.elem).value, lb_const_int(m, t_int, type_size_of(t->Slice.elem)).value, }; @@ -527,10 +520,10 @@ void lb_setup_type_info_data(lbProcedure *p) { // NOTE(bill): Setup type_info da LLVMValueRef params = LLVMConstNull(lb_type(m, t_type_info_ptr)); LLVMValueRef results = LLVMConstNull(lb_type(m, t_type_info_ptr)); if (t->Proc.params != nullptr) { - params = lb_get_type_info_ptr(m, t->Proc.params).value; + params = lb_type_info(m, t->Proc.params).value; } if (t->Proc.results != nullptr) { - results = lb_get_type_info_ptr(m, t->Proc.results).value; + results = lb_type_info(m, t->Proc.results).value; } LLVMValueRef vals[4] = { @@ -649,7 +642,7 @@ void lb_setup_type_info_data(lbProcedure *p) { // NOTE(bill): Setup type_info da // NOTE(bill): Zeroth is nil so ignore it for (isize variant_index = 0; variant_index < variant_count; variant_index++) { Type *vt = t->Union.variants[variant_index]; - lbValue tip = lb_get_type_info_ptr(m, vt); + lbValue tip = lb_type_info(m, vt); lbValue index = lb_const_int(m, t_int, variant_index); lbValue type_info = lb_emit_ptr_offset(p, memory_types, index); @@ -737,7 +730,7 @@ void lb_setup_type_info_data(lbProcedure *p) { // NOTE(bill): Setup type_info da for (isize source_index = 0; source_index < count; source_index++) { // TODO(bill): Order fields in source order not layout order Entity *f = t->Struct.fields[source_index]; - lbValue tip = lb_get_type_info_ptr(m, f->type); + lbValue tip = lb_type_info(m, f->type); i64 foffset = 0; if (!t->Struct.is_raw_union) { GB_ASSERT(t->Struct.offsets != nullptr); @@ -794,11 +787,11 @@ void lb_setup_type_info_data(lbProcedure *p) { // NOTE(bill): Setup type_info da tag = lb_const_ptr_cast(m, variant_ptr, t_type_info_map_ptr); init_map_internal_types(t); - lbValue gst = lb_get_type_info_ptr(m, t->Map.generated_struct_type); + lbValue gst = lb_type_info(m, t->Map.generated_struct_type); LLVMValueRef vals[5] = { - lb_get_type_info_ptr(m, t->Map.key).value, - lb_get_type_info_ptr(m, t->Map.value).value, + lb_type_info(m, t->Map.key).value, + lb_type_info(m, t->Map.value).value, gst.value, lb_get_equal_proc_for_type(m, t->Map.key).value, lb_get_hasher_proc_for_type(m, t->Map.key).value @@ -819,13 +812,13 @@ void lb_setup_type_info_data(lbProcedure *p) { // NOTE(bill): Setup type_info da LLVMValueRef vals[4] = { - lb_get_type_info_ptr(m, t->BitSet.elem).value, + lb_type_info(m, t->BitSet.elem).value, LLVMConstNull(lb_type(m, t_type_info_ptr)), lb_const_int(m, t_i64, t->BitSet.lower).value, lb_const_int(m, t_i64, t->BitSet.upper).value, }; if (t->BitSet.underlying != nullptr) { - vals[1] =lb_get_type_info_ptr(m, t->BitSet.underlying).value; + vals[1] =lb_type_info(m, t->BitSet.underlying).value; } lbValue res = {}; @@ -841,7 +834,7 @@ void lb_setup_type_info_data(lbProcedure *p) { // NOTE(bill): Setup type_info da LLVMValueRef vals[3] = {}; - vals[0] = lb_get_type_info_ptr(m, t->SimdVector.elem).value; + vals[0] = lb_type_info(m, t->SimdVector.elem).value; vals[1] = lb_const_int(m, t_int, type_size_of(t->SimdVector.elem)).value; vals[2] = lb_const_int(m, t_int, t->SimdVector.count).value; @@ -856,8 +849,8 @@ void lb_setup_type_info_data(lbProcedure *p) { // NOTE(bill): Setup type_info da { tag = lb_const_ptr_cast(m, variant_ptr, t_type_info_relative_pointer_ptr); LLVMValueRef vals[2] = { - lb_get_type_info_ptr(m, t->RelativePointer.pointer_type).value, - lb_get_type_info_ptr(m, t->RelativePointer.base_integer).value, + lb_type_info(m, t->RelativePointer.pointer_type).value, + lb_type_info(m, t->RelativePointer.base_integer).value, }; lbValue res = {}; @@ -870,8 +863,8 @@ void lb_setup_type_info_data(lbProcedure *p) { // NOTE(bill): Setup type_info da { tag = lb_const_ptr_cast(m, variant_ptr, t_type_info_relative_slice_ptr); LLVMValueRef vals[2] = { - lb_get_type_info_ptr(m, t->RelativeSlice.slice_type).value, - lb_get_type_info_ptr(m, t->RelativeSlice.base_integer).value, + lb_type_info(m, t->RelativeSlice.slice_type).value, + lb_type_info(m, t->RelativeSlice.base_integer).value, }; lbValue res = {}; @@ -886,7 +879,7 @@ void lb_setup_type_info_data(lbProcedure *p) { // NOTE(bill): Setup type_info da i64 ez = type_size_of(t->Matrix.elem); LLVMValueRef vals[5] = { - lb_get_type_info_ptr(m, t->Matrix.elem).value, + lb_type_info(m, t->Matrix.elem).value, lb_const_int(m, t_int, ez).value, lb_const_int(m, t_int, matrix_type_stride_in_elems(t)).value, lb_const_int(m, t_int, t->Matrix.row_count).value, diff --git a/src/llvm_backend_utility.cpp b/src/llvm_backend_utility.cpp index 52d3a17cf..ce7b43321 100644 --- a/src/llvm_backend_utility.cpp +++ b/src/llvm_backend_utility.cpp @@ -1,3 +1,5 @@ +lbValue lb_lookup_runtime_procedure(lbModule *m, String const &name); + bool lb_is_type_aggregate(Type *t) { t = base_type(t); switch (t->kind) { @@ -37,6 +39,12 @@ bool lb_is_type_aggregate(Type *t) { return false; } +void lb_emit_unreachable(lbProcedure *p) { + LLVMValueRef instr = LLVMGetLastInstruction(p->curr_block->block); + if (instr == nullptr || !lb_is_instr_terminating(instr)) { + LLVMBuildUnreachable(p->builder); + } +} lbValue lb_correct_endianness(lbProcedure *p, lbValue value) { Type *src = core_type(value.type); @@ -48,18 +56,19 @@ lbValue lb_correct_endianness(lbProcedure *p, lbValue value) { return value; } -void lb_mem_zero_ptr_internal(lbProcedure *p, LLVMValueRef ptr, LLVMValueRef len, unsigned alignment, bool is_volatile) { +LLVMValueRef lb_mem_zero_ptr_internal(lbProcedure *p, LLVMValueRef ptr, LLVMValueRef len, unsigned alignment, bool is_volatile) { bool is_inlinable = false; i64 const_len = 0; if (LLVMIsConstant(len)) { const_len = cast(i64)LLVMConstIntGetSExtValue(len); // TODO(bill): Determine when it is better to do the `*.inline` versions - if (const_len <= 4*build_context.word_size) { + if (const_len <= lb_max_zero_init_size()) { is_inlinable = true; } } + char const *name = "llvm.memset"; if (is_inlinable) { name = "llvm.memset.inline"; @@ -69,17 +78,28 @@ void lb_mem_zero_ptr_internal(lbProcedure *p, LLVMValueRef ptr, LLVMValueRef len lb_type(p->module, t_rawptr), lb_type(p->module, t_int) }; - unsigned id = LLVMLookupIntrinsicID(name, gb_strlen(name)); - GB_ASSERT_MSG(id != 0, "Unable to find %s.%s.%s", name, LLVMPrintTypeToString(types[0]), LLVMPrintTypeToString(types[1])); - LLVMValueRef ip = LLVMGetIntrinsicDeclaration(p->module->mod, id, types, gb_count_of(types)); + if (true || is_inlinable) { - LLVMValueRef args[4] = {}; - args[0] = LLVMBuildPointerCast(p->builder, ptr, types[0], ""); - args[1] = LLVMConstInt(LLVMInt8TypeInContext(p->module->ctx), 0, false); - args[2] = LLVMBuildIntCast2(p->builder, len, types[1], /*signed*/false, ""); - args[3] = LLVMConstInt(LLVMInt1TypeInContext(p->module->ctx), is_volatile, false); + LLVMValueRef args[4] = {}; + args[0] = LLVMBuildPointerCast(p->builder, ptr, types[0], ""); + args[1] = LLVMConstInt(LLVMInt8TypeInContext(p->module->ctx), 0, false); + args[2] = LLVMBuildIntCast2(p->builder, len, types[1], /*signed*/false, ""); + args[3] = LLVMConstInt(LLVMInt1TypeInContext(p->module->ctx), is_volatile, false); + + return lb_call_intrinsic(p, name, args, gb_count_of(args), types, gb_count_of(types)); + } else { + lbValue pr = lb_lookup_runtime_procedure(p->module, str_lit("memset")); + + LLVMValueRef args[3] = {}; + args[0] = LLVMBuildPointerCast(p->builder, ptr, types[0], ""); + args[1] = LLVMConstInt(LLVMInt32TypeInContext(p->module->ctx), 0, false); + args[2] = LLVMBuildIntCast2(p->builder, len, types[1], /*signed*/false, ""); + + // We always get the function pointer type rather than the function and there is apparently no way around that? + LLVMTypeRef type = lb_type_internal_for_procedures_raw(p->module, pr.type); + return LLVMBuildCall2(p->builder, type, pr.value, args, gb_count_of(args), ""); + } - LLVMBuildCall(p->builder, ip, args, gb_count_of(args), ""); } void lb_mem_zero_ptr(lbProcedure *p, LLVMValueRef ptr, Type *type, unsigned alignment) { @@ -335,40 +355,57 @@ lbValue lb_emit_or_else(lbProcedure *p, Ast *arg, Ast *else_expr, TypeAndValue c lbValue rhs = {}; lb_emit_try_lhs_rhs(p, arg, tv, &lhs, &rhs); - LLVMValueRef incoming_values[2] = {}; - LLVMBasicBlockRef incoming_blocks[2] = {}; - GB_ASSERT(else_expr != nullptr); - lbBlock *then = lb_create_block(p, "or_else.then"); - lbBlock *done = lb_create_block(p, "or_else.done"); // NOTE(bill): Append later - lbBlock *else_ = lb_create_block(p, "or_else.else"); - - lb_emit_if(p, lb_emit_try_has_value(p, rhs), then, else_); - lb_start_block(p, then); Type *type = default_type(tv.type); - incoming_values[0] = lb_emit_conv(p, lhs, type).value; + if (is_diverging_expr(else_expr)) { + lbBlock *then = lb_create_block(p, "or_else.then"); + lbBlock *else_ = lb_create_block(p, "or_else.else"); - lb_emit_jump(p, done); - lb_start_block(p, else_); + lb_emit_if(p, lb_emit_try_has_value(p, rhs), then, else_); + // NOTE(bill): else block needs to be straight afterwards to make sure that the actual value is used + // from the then block + lb_start_block(p, else_); - incoming_values[1] = lb_emit_conv(p, lb_build_expr(p, else_expr), type).value; + lb_build_expr(p, else_expr); + lb_emit_unreachable(p); // add just in case - lb_emit_jump(p, done); - lb_start_block(p, done); + lb_start_block(p, then); + return lb_emit_conv(p, lhs, type); + } else { + LLVMValueRef incoming_values[2] = {}; + LLVMBasicBlockRef incoming_blocks[2] = {}; - lbValue res = {}; - res.value = LLVMBuildPhi(p->builder, lb_type(p->module, type), ""); - res.type = type; + lbBlock *then = lb_create_block(p, "or_else.then"); + lbBlock *done = lb_create_block(p, "or_else.done"); // NOTE(bill): Append later + lbBlock *else_ = lb_create_block(p, "or_else.else"); - GB_ASSERT(p->curr_block->preds.count >= 2); - incoming_blocks[0] = p->curr_block->preds[0]->block; - incoming_blocks[1] = p->curr_block->preds[1]->block; + lb_emit_if(p, lb_emit_try_has_value(p, rhs), then, else_); + lb_start_block(p, then); - LLVMAddIncoming(res.value, incoming_values, incoming_blocks, 2); + incoming_values[0] = lb_emit_conv(p, lhs, type).value; - return res; + lb_emit_jump(p, done); + lb_start_block(p, else_); + + incoming_values[1] = lb_emit_conv(p, lb_build_expr(p, else_expr), type).value; + + lb_emit_jump(p, done); + lb_start_block(p, done); + + lbValue res = {}; + res.value = LLVMBuildPhi(p->builder, lb_type(p->module, type), ""); + res.type = type; + + GB_ASSERT(p->curr_block->preds.count >= 2); + incoming_blocks[0] = p->curr_block->preds[0]->block; + incoming_blocks[1] = p->curr_block->preds[1]->block; + + LLVMAddIncoming(res.value, incoming_values, incoming_blocks, 2); + + return res; + } } void lb_build_return_stmt(lbProcedure *p, Slice const &return_results); @@ -445,15 +482,11 @@ lbValue lb_emit_byte_swap(lbProcedure *p, lbValue value, Type *end_type) { char const *name = "llvm.bswap"; LLVMTypeRef types[1] = {lb_type(p->module, value.type)}; - unsigned id = LLVMLookupIntrinsicID(name, gb_strlen(name)); - GB_ASSERT_MSG(id != 0, "Unable to find %s.%s", name, LLVMPrintTypeToString(types[0])); - LLVMValueRef ip = LLVMGetIntrinsicDeclaration(p->module->mod, id, types, gb_count_of(types)); - LLVMValueRef args[1] = {}; - args[0] = value.value; + LLVMValueRef args[1] = { value.value }; lbValue res = {}; - res.value = LLVMBuildCall(p->builder, ip, args, gb_count_of(args), ""); + res.value = lb_call_intrinsic(p, name, args, gb_count_of(args), types, gb_count_of(types)); res.type = value.type; if (is_type_float(original_type)) { @@ -471,15 +504,10 @@ lbValue lb_emit_count_ones(lbProcedure *p, lbValue x, Type *type) { char const *name = "llvm.ctpop"; LLVMTypeRef types[1] = {lb_type(p->module, type)}; - unsigned id = LLVMLookupIntrinsicID(name, gb_strlen(name)); - GB_ASSERT_MSG(id != 0, "Unable to find %s.%s", name, LLVMPrintTypeToString(types[0])); - LLVMValueRef ip = LLVMGetIntrinsicDeclaration(p->module->mod, id, types, gb_count_of(types)); - - LLVMValueRef args[1] = {}; - args[0] = x.value; + LLVMValueRef args[1] = { x.value }; lbValue res = {}; - res.value = LLVMBuildCall(p->builder, ip, args, gb_count_of(args), ""); + res.value = lb_call_intrinsic(p, name, args, gb_count_of(args), types, gb_count_of(types)); res.type = type; return res; } @@ -500,16 +528,13 @@ lbValue lb_emit_count_trailing_zeros(lbProcedure *p, lbValue x, Type *type) { char const *name = "llvm.cttz"; LLVMTypeRef types[1] = {lb_type(p->module, type)}; - unsigned id = LLVMLookupIntrinsicID(name, gb_strlen(name)); - GB_ASSERT_MSG(id != 0, "Unable to find %s.%s", name, LLVMPrintTypeToString(types[0])); - LLVMValueRef ip = LLVMGetIntrinsicDeclaration(p->module->mod, id, types, gb_count_of(types)); - LLVMValueRef args[2] = {}; - args[0] = x.value; - args[1] = LLVMConstNull(LLVMInt1TypeInContext(p->module->ctx)); + LLVMValueRef args[2] = { + x.value, + LLVMConstNull(LLVMInt1TypeInContext(p->module->ctx)) }; lbValue res = {}; - res.value = LLVMBuildCall(p->builder, ip, args, gb_count_of(args), ""); + res.value = lb_call_intrinsic(p, name, args, gb_count_of(args), types, gb_count_of(types)); res.type = type; return res; } @@ -519,16 +544,13 @@ lbValue lb_emit_count_leading_zeros(lbProcedure *p, lbValue x, Type *type) { char const *name = "llvm.ctlz"; LLVMTypeRef types[1] = {lb_type(p->module, type)}; - unsigned id = LLVMLookupIntrinsicID(name, gb_strlen(name)); - GB_ASSERT_MSG(id != 0, "Unable to find %s.%s", name, LLVMPrintTypeToString(types[0])); - LLVMValueRef ip = LLVMGetIntrinsicDeclaration(p->module->mod, id, types, gb_count_of(types)); - LLVMValueRef args[2] = {}; - args[0] = x.value; - args[1] = LLVMConstNull(LLVMInt1TypeInContext(p->module->ctx)); + LLVMValueRef args[2] = { + x.value, + LLVMConstNull(LLVMInt1TypeInContext(p->module->ctx)) }; lbValue res = {}; - res.value = LLVMBuildCall(p->builder, ip, args, gb_count_of(args), ""); + res.value = lb_call_intrinsic(p, name, args, gb_count_of(args), types, gb_count_of(types)); res.type = type; return res; } @@ -540,15 +562,11 @@ lbValue lb_emit_reverse_bits(lbProcedure *p, lbValue x, Type *type) { char const *name = "llvm.bitreverse"; LLVMTypeRef types[1] = {lb_type(p->module, type)}; - unsigned id = LLVMLookupIntrinsicID(name, gb_strlen(name)); - GB_ASSERT_MSG(id != 0, "Unable to find %s.%s", name, LLVMPrintTypeToString(types[0])); - LLVMValueRef ip = LLVMGetIntrinsicDeclaration(p->module->mod, id, types, gb_count_of(types)); - LLVMValueRef args[1] = {}; - args[0] = x.value; + LLVMValueRef args[1] = { x.value }; lbValue res = {}; - res.value = LLVMBuildCall(p->builder, ip, args, gb_count_of(args), ""); + res.value = lb_call_intrinsic(p, name, args, gb_count_of(args), types, gb_count_of(types)); res.type = type; return res; } @@ -969,6 +987,11 @@ lbValue lb_emit_struct_ep(lbProcedure *p, lbValue s, i32 index) { case 0: result_type = t->RelativeSlice.base_integer; break; case 1: result_type = t->RelativeSlice.base_integer; break; } + } else if (is_type_soa_pointer(t)) { + switch (index) { + case 0: result_type = alloc_type_pointer(t->SoaPointer.elem); break; + case 1: result_type = t_int; break; + } } else { GB_PANIC("TODO(bill): struct_gep type: %s, %d", type_to_string(s.type), index); } @@ -979,15 +1002,16 @@ lbValue lb_emit_struct_ep(lbProcedure *p, lbValue s, i32 index) { index = lb_convert_struct_index(p->module, t, index); if (lb_is_const(s)) { + // NOTE(bill): this cannot be replaced with lb_emit_epi lbModule *m = p->module; lbValue res = {}; LLVMValueRef indices[2] = {llvm_zero(m), LLVMConstInt(lb_type(m, t_i32), index, false)}; - res.value = LLVMConstGEP(s.value, indices, gb_count_of(indices)); + res.value = LLVMConstGEP2(lb_type(m, type_deref(s.type)), s.value, indices, gb_count_of(indices)); res.type = alloc_type_pointer(result_type); return res; } else { lbValue res = {}; - LLVMTypeRef st = LLVMGetElementType(LLVMTypeOf(s.value)); + LLVMTypeRef st = lb_type(p->module, type_deref(s.type)); // gb_printf_err("%s\n", type_to_string(s.type)); // gb_printf_err("%s\n", LLVMPrintTypeToString(LLVMTypeOf(s.value))); // gb_printf_err("%d\n", index); @@ -995,7 +1019,7 @@ lbValue lb_emit_struct_ep(lbProcedure *p, lbValue s, i32 index) { unsigned count = LLVMCountStructElementTypes(st); GB_ASSERT_MSG(count >= cast(unsigned)index, "%u %d %d", count, index, original_index); - res.value = LLVMBuildStructGEP(p->builder, s.value, cast(unsigned)index, ""); + res.value = LLVMBuildStructGEP2(p->builder, st, s.value, cast(unsigned)index, ""); res.type = alloc_type_pointer(result_type); return res; } @@ -1099,6 +1123,13 @@ lbValue lb_emit_struct_ev(lbProcedure *p, lbValue s, i32 index) { result_type = t->Array.elem; break; + case Type_SoaPointer: + switch (index) { + case 0: result_type = alloc_type_pointer(t->SoaPointer.elem); break; + case 1: result_type = t_int; break; + } + break; + default: GB_PANIC("TODO(bill): struct_ev type: %s, %d", type_to_string(s.type), index); break; @@ -1126,7 +1157,28 @@ lbValue lb_emit_deep_field_gep(lbProcedure *p, lbValue e, Selection sel) { } type = core_type(type); - if (is_type_quaternion(type)) { + if (type->kind == Type_SoaPointer) { + lbValue addr = lb_emit_struct_ep(p, e, 0); + lbValue index = lb_emit_struct_ep(p, e, 1); + addr = lb_emit_load(p, addr); + index = lb_emit_load(p, index); + + i32 first_index = sel.index[0]; + Selection sub_sel = sel; + sub_sel.index.data += 1; + sub_sel.index.count -= 1; + + lbValue arr = lb_emit_struct_ep(p, addr, first_index); + + Type *t = base_type(type_deref(addr.type)); + GB_ASSERT(is_type_soa_struct(t)); + + if (t->Struct.soa_kind == StructSoa_Fixed) { + e = lb_emit_array_ep(p, arr, index); + } else { + e = lb_emit_ptr_offset(p, lb_emit_load(p, arr), index); + } + } else if (is_type_quaternion(type)) { e = lb_emit_struct_ep(p, e, index); } else if (is_type_raw_union(type)) { type = get_struct_field_type(type, index); @@ -1201,7 +1253,12 @@ lbValue lb_emit_array_ep(lbProcedure *p, lbValue s, lbValue index) { Type *ptr = base_array_type(st); lbValue res = {}; - res.value = LLVMBuildGEP(p->builder, s.value, indices, 2, ""); + + if (LLVMIsConstant(s.value) && LLVMIsConstant(index.value)) { + res.value = LLVMConstGEP2(lb_type(p->module, st), s.value, indices, gb_count_of(indices)); + } else { + res.value = LLVMBuildGEP2(p->builder, lb_type(p->module, st), s.value, indices, gb_count_of(indices), ""); + } res.type = alloc_type_pointer(ptr); return res; } @@ -1211,24 +1268,16 @@ lbValue lb_emit_array_epi(lbProcedure *p, lbValue s, isize index) { GB_ASSERT(is_type_pointer(t)); Type *st = base_type(type_deref(t)); GB_ASSERT_MSG(is_type_array(st) || is_type_enumerated_array(st) || is_type_matrix(st), "%s", type_to_string(st)); - GB_ASSERT(0 <= index); - Type *ptr = base_array_type(st); - - - LLVMValueRef indices[2] = { - LLVMConstInt(lb_type(p->module, t_int), 0, false), - LLVMConstInt(lb_type(p->module, t_int), cast(unsigned)index, false), - }; - - lbValue res = {}; - if (lb_is_const(s)) { - res.value = LLVMConstGEP(s.value, indices, gb_count_of(indices)); - } else { - res.value = LLVMBuildGEP(p->builder, s.value, indices, gb_count_of(indices), ""); - } - res.type = alloc_type_pointer(ptr); - return res; + return lb_emit_epi(p, s, index); +} +lbValue lb_emit_array_epi(lbModule *m, lbValue s, isize index) { + Type *t = s.type; + GB_ASSERT(is_type_pointer(t)); + Type *st = base_type(type_deref(t)); + GB_ASSERT_MSG(is_type_array(st) || is_type_enumerated_array(st) || is_type_matrix(st), "%s", type_to_string(st)); + GB_ASSERT(0 <= index); + return lb_emit_epi(m, s, index); } lbValue lb_emit_ptr_offset(lbProcedure *p, lbValue ptr, lbValue index) { @@ -1236,11 +1285,12 @@ lbValue lb_emit_ptr_offset(lbProcedure *p, lbValue ptr, lbValue index) { LLVMValueRef indices[1] = {index.value}; lbValue res = {}; res.type = ptr.type; + LLVMTypeRef type = lb_type(p->module, type_deref(ptr.type)); if (lb_is_const(ptr) && lb_is_const(index)) { - res.value = LLVMConstGEP(ptr.value, indices, 1); + res.value = LLVMConstGEP2(type, ptr.value, indices, 1); } else { - res.value = LLVMBuildGEP(p->builder, ptr.value, indices, 1, ""); + res.value = LLVMBuildGEP2(p->builder, type, ptr.value, indices, 1, ""); } return res; } @@ -1249,63 +1299,18 @@ lbValue lb_emit_matrix_epi(lbProcedure *p, lbValue s, isize row, isize column) { Type *t = s.type; GB_ASSERT(is_type_pointer(t)); Type *mt = base_type(type_deref(t)); - - Type *ptr = base_array_type(mt); - if (column == 0) { GB_ASSERT_MSG(is_type_matrix(mt) || is_type_array_like(mt), "%s", type_to_string(mt)); - - LLVMValueRef indices[2] = { - LLVMConstInt(lb_type(p->module, t_int), 0, false), - LLVMConstInt(lb_type(p->module, t_int), cast(unsigned)row, false), - }; - - lbValue res = {}; - if (lb_is_const(s)) { - res.value = LLVMConstGEP(s.value, indices, gb_count_of(indices)); - } else { - res.value = LLVMBuildGEP(p->builder, s.value, indices, gb_count_of(indices), ""); - } - - Type *ptr = base_array_type(mt); - res.type = alloc_type_pointer(ptr); - return res; + return lb_emit_epi(p, s, row); } else if (row == 0 && is_type_array_like(mt)) { - LLVMValueRef indices[2] = { - LLVMConstInt(lb_type(p->module, t_int), 0, false), - LLVMConstInt(lb_type(p->module, t_int), cast(unsigned)column, false), - }; - - lbValue res = {}; - if (lb_is_const(s)) { - res.value = LLVMConstGEP(s.value, indices, gb_count_of(indices)); - } else { - res.value = LLVMBuildGEP(p->builder, s.value, indices, gb_count_of(indices), ""); - } - - Type *ptr = base_array_type(mt); - res.type = alloc_type_pointer(ptr); - return res; + return lb_emit_epi(p, s, column); } GB_ASSERT_MSG(is_type_matrix(mt), "%s", type_to_string(mt)); isize offset = matrix_indices_to_offset(mt, row, column); - - LLVMValueRef indices[2] = { - LLVMConstInt(lb_type(p->module, t_int), 0, false), - LLVMConstInt(lb_type(p->module, t_int), cast(unsigned)offset, false), - }; - - lbValue res = {}; - if (lb_is_const(s)) { - res.value = LLVMConstGEP(s.value, indices, gb_count_of(indices)); - } else { - res.value = LLVMBuildGEP(p->builder, s.value, indices, gb_count_of(indices), ""); - } - res.type = alloc_type_pointer(ptr); - return res; + return lb_emit_epi(p, s, offset); } lbValue lb_emit_matrix_ep(lbProcedure *p, lbValue s, lbValue row, lbValue column) { @@ -1328,11 +1333,12 @@ lbValue lb_emit_matrix_ep(lbProcedure *p, lbValue s, lbValue row, lbValue column index, }; + LLVMTypeRef type = lb_type(p->module, mt); lbValue res = {}; if (lb_is_const(s)) { - res.value = LLVMConstGEP(s.value, indices, gb_count_of(indices)); + res.value = LLVMConstGEP2(type, s.value, indices, gb_count_of(indices)); } else { - res.value = LLVMBuildGEP(p->builder, s.value, indices, gb_count_of(indices), ""); + res.value = LLVMBuildGEP2(p->builder, type, s.value, indices, gb_count_of(indices), ""); } res.type = alloc_type_pointer(ptr); return res; @@ -1536,18 +1542,12 @@ lbValue lb_emit_mul_add(lbProcedure *p, lbValue a, lbValue b, lbValue c, Type *t if (is_possible) { char const *name = "llvm.fma"; - unsigned id = LLVMLookupIntrinsicID(name, gb_strlen(name)); - GB_ASSERT_MSG(id != 0, "Unable to find %s", name); - - LLVMTypeRef types[1] = {}; - types[0] = lb_type(m, t); - - LLVMValueRef ip = LLVMGetIntrinsicDeclaration(m->mod, id, types, gb_count_of(types)); - LLVMValueRef values[3] = {}; - values[0] = a.value; - values[1] = b.value; - values[2] = c.value; - LLVMValueRef call = LLVMBuildCall(p->builder, ip, values, gb_count_of(values), ""); + LLVMTypeRef types[1] = { lb_type(m, t) }; + LLVMValueRef values[3] = { + a.value, + b.value, + c.value }; + LLVMValueRef call = lb_call_intrinsic(p, name, values, gb_count_of(values), types, gb_count_of(types)); return {call, t}; } else { lbValue x = lb_emit_arith(p, Token_Mul, a, b, t); @@ -1646,7 +1646,7 @@ LLVMValueRef llvm_vector_expand_to_power_of_two(lbProcedure *p, LLVMValueRef val LLVMValueRef llvm_vector_reduce_add(lbProcedure *p, LLVMValueRef value) { LLVMTypeRef type = LLVMTypeOf(value); GB_ASSERT(LLVMGetTypeKind(type) == LLVMVectorTypeKind); - LLVMTypeRef elem = LLVMGetElementType(type); + LLVMTypeRef elem = OdinLLVMGetVectorElementType(type); unsigned len = LLVMGetVectorSize(type); if (len == 0) { return LLVMConstNull(type); @@ -1676,15 +1676,9 @@ LLVMValueRef llvm_vector_reduce_add(lbProcedure *p, LLVMValueRef value) { unsigned id = LLVMLookupIntrinsicID(name, gb_strlen(name)); if (id != 0 && false) { - LLVMTypeRef types[1] = {}; - types[0] = type; - - LLVMValueRef ip = LLVMGetIntrinsicDeclaration(p->module->mod, id, types, gb_count_of(types)); - LLVMValueRef values[2] = {}; - values[0] = LLVMConstNull(elem); - values[1] = value; - LLVMValueRef call = LLVMBuildCall(p->builder, ip, values+value_offset, value_count, ""); - return call; + LLVMTypeRef types[1] = { type }; + LLVMValueRef values[2] = { LLVMConstNull(elem), value }; + return lb_call_intrinsic(p, name, values + value_offset, value_count, types, gb_count_of(types)); } // Manual reduce @@ -1728,7 +1722,7 @@ LLVMValueRef llvm_vector_reduce_add(lbProcedure *p, LLVMValueRef value) { LLVMValueRef llvm_vector_add(lbProcedure *p, LLVMValueRef a, LLVMValueRef b) { GB_ASSERT(LLVMTypeOf(a) == LLVMTypeOf(b)); - LLVMTypeRef elem = LLVMGetElementType(LLVMTypeOf(a)); + LLVMTypeRef elem = OdinLLVMGetVectorElementType(LLVMTypeOf(a)); if (LLVMGetTypeKind(elem) == LLVMIntegerTypeKind) { return LLVMBuildAdd(p->builder, a, b, ""); @@ -1739,7 +1733,7 @@ LLVMValueRef llvm_vector_add(lbProcedure *p, LLVMValueRef a, LLVMValueRef b) { LLVMValueRef llvm_vector_mul(lbProcedure *p, LLVMValueRef a, LLVMValueRef b) { GB_ASSERT(LLVMTypeOf(a) == LLVMTypeOf(b)); - LLVMTypeRef elem = LLVMGetElementType(LLVMTypeOf(a)); + LLVMTypeRef elem = OdinLLVMGetVectorElementType(LLVMTypeOf(a)); if (LLVMGetTypeKind(elem) == LLVMIntegerTypeKind) { return LLVMBuildMul(p->builder, a, b, ""); @@ -1753,14 +1747,13 @@ LLVMValueRef llvm_vector_dot(lbProcedure *p, LLVMValueRef a, LLVMValueRef b) { } LLVMValueRef llvm_vector_mul_add(lbProcedure *p, LLVMValueRef a, LLVMValueRef b, LLVMValueRef c) { - lbModule *m = p->module; - + LLVMTypeRef t = LLVMTypeOf(a); GB_ASSERT(t == LLVMTypeOf(b)); GB_ASSERT(t == LLVMTypeOf(c)); GB_ASSERT(LLVMGetTypeKind(t) == LLVMVectorTypeKind); - LLVMTypeRef elem = LLVMGetElementType(t); + LLVMTypeRef elem = OdinLLVMGetVectorElementType(t); bool is_possible = false; @@ -1776,18 +1769,9 @@ LLVMValueRef llvm_vector_mul_add(lbProcedure *p, LLVMValueRef a, LLVMValueRef b, if (is_possible) { char const *name = "llvm.fmuladd"; - unsigned id = LLVMLookupIntrinsicID(name, gb_strlen(name)); - GB_ASSERT_MSG(id != 0, "Unable to find %s", name); - - LLVMTypeRef types[1] = {}; - types[0] = t; - - LLVMValueRef ip = LLVMGetIntrinsicDeclaration(m->mod, id, types, gb_count_of(types)); - LLVMValueRef values[3] = {}; - values[0] = a; - values[1] = b; - values[2] = c; - LLVMValueRef call = LLVMBuildCall(p->builder, ip, values, gb_count_of(values), ""); + LLVMTypeRef types[1] = { t }; + LLVMValueRef values[3] = { a, b, c}; + LLVMValueRef call = lb_call_intrinsic(p, name, values, gb_count_of(values), types, gb_count_of(types)); return call; } else { LLVMValueRef x = llvm_vector_mul(p, a, b); @@ -1802,7 +1786,7 @@ LLVMValueRef llvm_get_inline_asm(LLVMTypeRef func_type, String const &str, Strin cast(char *)clobbers.text, cast(size_t)clobbers.len, has_side_effects, is_align_stack, dialect - #if LLVM_VERSION_MAJOR >= 13 + #if LLVM_VERSION_MAJOR >= 13 , /*CanThrow*/false #endif ); @@ -1842,8 +1826,6 @@ void lb_set_wasm_export_attributes(LLVMValueRef value, String export_name) { } -lbValue lb_lookup_runtime_procedure(lbModule *m, String const &name); - lbAddr lb_handle_objc_find_or_register_selector(lbProcedure *p, String const &name) { lbAddr *found = string_map_get(&p->module->objc_selectors, name); diff --git a/src/main.cpp b/src/main.cpp index 7531fb37c..ef1b8dda1 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -283,6 +283,9 @@ i32 linker_stage(lbGenerator *gen) { String vs_exe_path = path_to_string(heap_allocator(), build_context.build_paths[BuildPath_VS_EXE]); defer (gb_free(heap_allocator(), vs_exe_path.text)); + 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)); + char const *subsystem_str = build_context.use_subsystem_windows ? "WINDOWS" : "CONSOLE"; if (!build_context.use_lld) { // msvc if (build_context.has_resource) { @@ -292,7 +295,8 @@ i32 linker_stage(lbGenerator *gen) { defer (gb_free(heap_allocator(), res_path.text)); result = system_exec_command_line_app("msvc-link", - "\"rc.exe\" /nologo /fo \"%.*s\" \"%.*s\"", + "\"%.*src.exe\" /nologo /fo \"%.*s\" \"%.*s\"", + LIT(windows_sdk_bin_path), LIT(res_path), LIT(rc_path) ); @@ -463,8 +467,15 @@ i32 linker_stage(lbGenerator *gen) { // correctly this way since all the other dependencies provided implicitly // by the compiler frontend are still needed and most of the command // line arguments prepared previously are incompatible with ld. - link_settings = gb_string_appendc(link_settings, "-Wl,-init,'_odin_entry_point' "); - link_settings = gb_string_appendc(link_settings, "-Wl,-fini,'_odin_exit_point' "); + if (build_context.metrics.os == TargetOs_darwin) { + link_settings = gb_string_appendc(link_settings, "-Wl,-init,'__odin_entry_point' "); + // NOTE(weshardee): __odin_exit_point should also be added, but -fini + // does not exist on MacOS + } else { + link_settings = gb_string_appendc(link_settings, "-Wl,-init,'_odin_entry_point' "); + link_settings = gb_string_appendc(link_settings, "-Wl,-fini,'_odin_exit_point' "); + } + } else if (build_context.metrics.os != TargetOs_openbsd) { // OpenBSD defaults to PIE executable. do not pass -no-pie for it. link_settings = gb_string_appendc(link_settings, "-no-pie "); @@ -1551,7 +1562,7 @@ bool parse_build_flags(Array args) { bad_flags = true; break; } - build_context.resource_filepath = substring(path, 0, string_extension_position(path)); + build_context.resource_filepath = path; build_context.has_resource = true; } else { gb_printf_err("Invalid -resource path, got %.*s\n", LIT(path)); diff --git a/src/microsoft_craziness.h b/src/microsoft_craziness.h index 812513875..7d23f2557 100644 --- a/src/microsoft_craziness.h +++ b/src/microsoft_craziness.h @@ -50,18 +50,7 @@ gb_global gbAllocator mc_allocator = heap_allocator(); struct Find_Result { int windows_sdk_version; // Zero if no Windows SDK found. - wchar_t const *windows_sdk_root; - wchar_t const *windows_sdk_um_library_path; - wchar_t const *windows_sdk_ucrt_library_path; - - wchar_t const *vs_exe_path; - wchar_t const *vs_library_path; -}; - -struct Find_Result_Utf8 { - int windows_sdk_version; // Zero if no Windows SDK found. - - String windows_sdk_root; + String windows_sdk_bin_path; String windows_sdk_um_library_path; String windows_sdk_ucrt_library_path; @@ -69,8 +58,6 @@ struct Find_Result_Utf8 { String vs_library_path; }; -Find_Result_Utf8 find_visual_studio_and_windows_sdk_utf8(); - String mc_wstring_to_string(wchar_t const *str) { return string16_to_string(mc_allocator, make_string16_c(str)); } @@ -87,6 +74,10 @@ String mc_concat(String a, String b, String c) { return concatenate3_strings(mc_allocator, a, b, c); } +String mc_concat(String a, String b, String c, String d) { + return concatenate4_strings(mc_allocator, a, b, c, d); +} + String mc_get_env(String key) { char const * value = gb_get_env((char const *)key.text, mc_allocator); return make_string_c(value); @@ -219,19 +210,19 @@ struct DECLSPEC_UUID("42843719-DB4C-46C2-8E7C-64F1816EFD5B") DECLSPEC_NOVTABLE I // The beginning of the actual code that does things. -struct Version_Data_Utf8 { - i32 best_version[4]; // For Windows 8 versions, only two of these numbers are used. +struct Version_Data { + i32 best_version[4]; String best_name; }; -typedef void (*MC_Visit_Proc)(String short_name, String full_name, Version_Data_Utf8 *data); -bool mc_visit_files(String dir_name, Version_Data_Utf8 *data, MC_Visit_Proc proc) { +typedef void (*MC_Visit_Proc)(String short_name, String full_name, Version_Data *data); +bool mc_visit_files(String dir_name, Version_Data *data, MC_Visit_Proc proc) { // Visit everything in one folder (non-recursively). If it's a directory // that doesn't start with ".", call the visit proc on it. The visit proc // will see if the filename conforms to the expected versioning pattern. - String wildcard_name = mc_concat(dir_name, str_lit("\\*")); + String wildcard_name = mc_concat(dir_name, str_lit("*")); defer (mc_free(wildcard_name)); MC_Find_Data find_data; @@ -242,7 +233,7 @@ bool mc_visit_files(String dir_name, Version_Data_Utf8 *data, MC_Visit_Proc proc bool success = true; while (success) { if ((find_data.file_attributes & FILE_ATTRIBUTE_DIRECTORY) && (find_data.filename[0] != '.')) { - String full_name = mc_concat(dir_name, str_lit("\\"), find_data.filename); + String full_name = mc_concat(dir_name, find_data.filename); defer (mc_free(full_name)); proc(find_data.filename, full_name, data); @@ -284,7 +275,7 @@ String find_windows_kit_root(HKEY key, String const version) { return value; } -void win10_best(String short_name, String full_name, Version_Data_Utf8 *data) { +void win10_best(String short_name, String full_name, Version_Data *data) { // Find the Windows 10 subdirectory with the highest version number. int i0, i1, i2, i3; @@ -304,11 +295,11 @@ void win10_best(String short_name, String full_name, Version_Data_Utf8 *data) { // we have to copy_string and free here because visit_files free's the full_name string // after we execute this function, so Win*_Data would contain an invalid pointer. - if (data->best_name.len > 0) mc_free(data->best_name); + if (data->best_name.len) mc_free(data->best_name); data->best_name = copy_string(mc_allocator, full_name); - if (data->best_name.len > 0) { + if (data->best_name.len) { data->best_version[0] = i0; data->best_version[1] = i1; data->best_version[2] = i2; @@ -316,34 +307,8 @@ void win10_best(String short_name, String full_name, Version_Data_Utf8 *data) { } } -void win8_best(String short_name, String full_name, Version_Data_Utf8 *data) { - // Find the Windows 8 subdirectory with the highest version number. - - int i0, i1; - auto success = sscanf_s((const char *const)short_name.text, "winv%d.%d", &i0, &i1); - if (success < 2) return; - - if (i0 < data->best_version[0]) return; - else if (i0 == data->best_version[0]) { - if (i1 < data->best_version[1]) return; - } - - // we have to copy_string and free here because visit_files free's the full_name string - // after we execute this function, so Win*_Data would contain an invalid pointer. - if (data->best_name.len > 0) mc_free(data->best_name); - data->best_name = copy_string(mc_allocator, full_name); - - if (data->best_name.len > 0) { - data->best_version[0] = i0; - data->best_version[1] = i1; - } -} - -void find_windows_kit_root(Find_Result_Utf8 *result) { - // Information about the Windows 10 and Windows 8 development kits - // is stored in the same place in the registry. We open a key - // to that place, first checking preferntially for a Windows 10 kit, - // then, if that's not found, a Windows 8 kit. +void find_windows_kit_paths(Find_Result *result) { + bool sdk_found = false; HKEY main_key; @@ -355,44 +320,42 @@ void find_windows_kit_root(Find_Result_Utf8 *result) { // Look for a Windows 10 entry. String windows10_root = find_windows_kit_root(main_key, str_lit("KitsRoot10")); - if (windows10_root.len > 0) { + if (windows10_root.len) { defer (mc_free(windows10_root)); - String windows10_lib = mc_concat(windows10_root, str_lit("Lib")); + String windows10_lib = mc_concat(windows10_root, str_lit("Lib\\")); + Version_Data data_lib = {0}; + mc_visit_files(windows10_lib, &data_lib, win10_best); defer (mc_free(windows10_lib)); + defer (mc_free(data_lib.best_name)); - Version_Data_Utf8 data = {0}; - mc_visit_files(windows10_lib, &data, win10_best); - if (data.best_name.len > 0) { - result->windows_sdk_version = 10; - result->windows_sdk_root = mc_concat(data.best_name, str_lit("\\")); - return; + String windows10_bin = mc_concat(windows10_root, str_lit("bin\\")); + Version_Data data_bin = {0}; + mc_visit_files(windows10_bin, &data_bin, win10_best); + defer (mc_free(windows10_bin)); + defer (mc_free(data_bin.best_name)); + + if (data_lib.best_name.len && data_bin.best_name.len) { + if (build_context.metrics.arch == TargetArch_amd64) { + result->windows_sdk_um_library_path = mc_concat(data_lib.best_name, str_lit("\\um\\x64\\")); + result->windows_sdk_ucrt_library_path = mc_concat(data_lib.best_name, str_lit("\\ucrt\\x64\\")); + result->windows_sdk_bin_path = mc_concat(data_bin.best_name, str_lit("\\x64\\")); + sdk_found = true; + } else if (build_context.metrics.arch == TargetArch_i386) { + result->windows_sdk_um_library_path = mc_concat(data_lib.best_name, str_lit("\\um\\x86\\")); + result->windows_sdk_ucrt_library_path = mc_concat(data_lib.best_name, str_lit("\\ucrt\\x86\\")); + result->windows_sdk_bin_path = mc_concat(data_bin.best_name, str_lit("\\x86\\")); + sdk_found = true; + } } - mc_free(data.best_name); } - // Look for a Windows 8 entry. - String windows8_root = find_windows_kit_root(main_key, str_lit("KitsRoot81")); - - if (windows8_root.len > 0) { - defer (mc_free(windows8_root)); - - String windows8_lib = mc_concat(windows8_root, str_lit("Lib")); - defer (mc_free(windows8_lib)); - - Version_Data_Utf8 data = {0}; - mc_visit_files(windows8_lib, &data, win8_best); - if (data.best_name.len > 0) { - result->windows_sdk_version = 8; - result->windows_sdk_root = mc_concat(data.best_name, str_lit("\\")); - return; - } - mc_free(data.best_name); + if (sdk_found) { + result->windows_sdk_version = 10; } - // If we get here, we failed to find anything. } -bool find_visual_studio_by_fighting_through_microsoft_craziness(Find_Result_Utf8 *result) { +bool find_visual_studio_by_fighting_through_microsoft_craziness(Find_Result *result) { // The name of this procedure is kind of cryptic. Its purpose is // to fight through Microsoft craziness. The things that the fine // Visual Studio team want you to do, JUST TO FIND A SINGLE FOLDER @@ -555,54 +518,97 @@ bool find_visual_studio_by_fighting_through_microsoft_craziness(Find_Result_Utf8 } // NOTE(WalterPlinge): Environment variables can help to find Visual C++ and WinSDK paths for both -// official and portable installations (like mmozeiko's portable msvc script). This will only use -// the first paths it finds, and won't overwrite any values that `result` already has. -bool find_msvc_install_from_env_vars(Find_Result_Utf8 *result) { +// official and portable installations (like mmozeiko's portable msvc script). +void find_windows_kit_paths_from_env_vars(Find_Result *result) { if (build_context.metrics.arch != TargetArch_amd64 && build_context.metrics.arch != TargetArch_i386) { - return false; + return; } - // We can find windows sdk using the following combination of env vars: - // (UniversalCRTSdkDir or WindowsSdkDir) and (WindowsSDKLibVersion or WindowsSDKVersion) - bool sdk_found = false; + // We can find windows sdk lib dir using the following combination of env vars: + // (WindowsSdkDir or UniversalCRTSdkDir) and (WindowsSDKVersion or WindowsSDKLibVersion) + bool sdk_lib_found = false; + + // We can find windows sdk bin dir using the following combination of env vars: + // (WindowsSdkVerBinPath) or ((WindowsSdkBinPath or WindowsSdkDir or UniversalCRTSdkDir) and (WindowsSDKVersion || WindowsSDKLibVersion)) + bool sdk_bin_found = false; // These appear to be suitable env vars used by Visual Studio String win_sdk_ver_env = mc_get_env(str_lit("WindowsSDKVersion")); - String win_sdk_lib_env = mc_get_env(str_lit("WindowsSDKLibVersion")); + String win_sdk_lib_ver_env = mc_get_env(str_lit("WindowsSDKLibVersion")); String win_sdk_dir_env = mc_get_env(str_lit("WindowsSdkDir")); String crt_sdk_dir_env = mc_get_env(str_lit("UniversalCRTSdkDir")); + String win_sdk_bin_path_env = mc_get_env(str_lit("WindowsSdkBinPath")); + String win_sdk_ver_bin_path_env = mc_get_env(str_lit("WindowsSdkVerBinPath")); defer ({ mc_free(win_sdk_ver_env); - mc_free(win_sdk_lib_env); + mc_free(win_sdk_lib_ver_env); mc_free(win_sdk_dir_env); mc_free(crt_sdk_dir_env); + mc_free(win_sdk_bin_path_env); + mc_free(win_sdk_ver_bin_path_env); }); + if (win_sdk_ver_bin_path_env.len || ((win_sdk_bin_path_env.len || win_sdk_dir_env.len || crt_sdk_dir_env.len) && (win_sdk_ver_env.len || win_sdk_lib_ver_env.len))) { + String bin; + defer (mc_free(bin)); + + if (win_sdk_ver_bin_path_env.len) { + String dir = win_sdk_ver_bin_path_env; + + // Add trailing '\' in case it was missing + bin = mc_concat(dir, dir[dir.len - 1] != '\\' ? str_lit("\\") : str_lit("")); + } else { + String dir = win_sdk_bin_path_env.len ? win_sdk_bin_path_env : win_sdk_dir_env.len ? win_sdk_dir_env : crt_sdk_dir_env; + String ver = win_sdk_ver_env.len ? win_sdk_ver_env : win_sdk_lib_ver_env; + + // Add trailing '\' in case it was missing + dir = mc_concat(dir, dir[dir.len - 1] != '\\' ? str_lit("\\") : str_lit("")); + ver = mc_concat(ver, ver[ver.len - 1] != '\\' ? str_lit("\\") : str_lit("")); + defer (mc_free(dir)); + defer (mc_free(ver)); + + // Append "bin" for win_sdk_dir_env and crt_sdk_dir_env + String dir_bin = mc_concat(dir, win_sdk_bin_path_env.len ? str_lit("") : str_lit("bin\\")); + defer (mc_free(dir_bin)); + + bin = mc_concat(dir_bin, ver); + } + + if (build_context.metrics.arch == TargetArch_amd64) { + result->windows_sdk_bin_path = mc_concat(bin, str_lit("x64\\")); + sdk_bin_found = true; + } else if (build_context.metrics.arch == TargetArch_i386) { + result->windows_sdk_bin_path = mc_concat(bin, str_lit("x86\\")); + sdk_bin_found = true; + } + } + // NOTE(WalterPlinge): If any combination is found, let's just assume they are correct - if ((win_sdk_ver_env.len || win_sdk_lib_env.len) && (win_sdk_dir_env.len || crt_sdk_dir_env.len)) { - //? Maybe we need to handle missing '\' at end of strings, so far it doesn't seem an issue + if ((win_sdk_ver_env.len || win_sdk_lib_ver_env.len) && (win_sdk_dir_env.len || crt_sdk_dir_env.len)) { String dir = win_sdk_dir_env.len ? win_sdk_dir_env : crt_sdk_dir_env; - String ver = win_sdk_ver_env.len ? win_sdk_ver_env : win_sdk_lib_env; + String ver = win_sdk_ver_env.len ? win_sdk_ver_env : win_sdk_lib_ver_env; - // These have trailing '\' as we are just composing the path - String um_dir = build_context.metrics.arch == TargetArch_amd64 - ? str_lit("um\\x64\\") - : str_lit("um\\x86\\"); - String ucrt_dir = build_context.metrics.arch == TargetArch_amd64 - ? str_lit("ucrt\\x64\\") - : str_lit("ucrt\\x86\\"); + // Add trailing '\' in case it was missing + dir = mc_concat(dir, dir[dir.len - 1] != '\\' ? str_lit("\\") : str_lit("")); + ver = mc_concat(ver, ver[ver.len - 1] != '\\' ? str_lit("\\") : str_lit("")); + defer (mc_free(dir)); + defer (mc_free(ver)); - result->windows_sdk_root = mc_concat(dir, str_lit("Lib\\"), ver); - result->windows_sdk_um_library_path = mc_concat(result->windows_sdk_root, um_dir); - result->windows_sdk_ucrt_library_path = mc_concat(result->windows_sdk_root, ucrt_dir); - - sdk_found = true; + if (build_context.metrics.arch == TargetArch_amd64) { + result->windows_sdk_um_library_path = mc_concat(dir, str_lit("Lib\\"), ver, str_lit("um\\x64\\")); + result->windows_sdk_ucrt_library_path = mc_concat(dir, str_lit("Lib\\"), ver, str_lit("ucrt\\x64\\")); + sdk_lib_found = true; + } else if (build_context.metrics.arch == TargetArch_i386) { + result->windows_sdk_um_library_path = mc_concat(dir, str_lit("Lib\\"), ver, str_lit("um\\x86\\")); + result->windows_sdk_ucrt_library_path = mc_concat(dir, str_lit("Lib\\"), ver, str_lit("ucrt\\x86\\")); + sdk_lib_found = true; + } } // If we haven't found it yet, we can loop through LIB for specific folders //? This may not be robust enough using `um\x64` and `ucrt\x64` - if (!sdk_found) { + if (!sdk_lib_found) { String lib = mc_get_env(str_lit("LIB")); defer (mc_free(lib)); @@ -624,72 +630,67 @@ bool find_msvc_install_from_env_vars(Find_Result_Utf8 *result) { continue; } hi = c; - String dir = substring(lib, lo, hi); defer (lo = hi + 1); + // Skip when there are two ;; in a row + if (lo == hi) { + continue; + } + + String dir = substring(lib, lo, hi); + // Remove the last slash so we can match with the strings above String end = dir[dir.len - 1] == '\\' ? substring(dir, 0, dir.len - 1) : substring(dir, 0, dir.len); - // Find one and we can make the other if (string_ends_with(end, um_dir)) { - result->windows_sdk_um_library_path = mc_concat(end, str_lit("\\")); - break; + result->windows_sdk_um_library_path = mc_concat(end, str_lit("\\")); } else if (string_ends_with(end, ucrt_dir)) { result->windows_sdk_ucrt_library_path = mc_concat(end, str_lit("\\")); + } + + if (result->windows_sdk_um_library_path.len && result->windows_sdk_ucrt_library_path.len) { + sdk_lib_found = true; break; } } - - // Get the root from the one we found, and make the other - // NOTE(WalterPlinge): we need to copy the string so that we don't risk a double free - if (result->windows_sdk_um_library_path.len > 0) { - String root = substring(result->windows_sdk_um_library_path, 0, result->windows_sdk_um_library_path.len - 1 - um_dir.len); - result->windows_sdk_root = copy_string(mc_allocator, root); - result->windows_sdk_ucrt_library_path = mc_concat(result->windows_sdk_root, ucrt_dir, str_lit("\\")); - } else if (result->windows_sdk_ucrt_library_path.len > 0) { - String root = substring(result->windows_sdk_ucrt_library_path, 0, result->windows_sdk_ucrt_library_path.len - 1 - ucrt_dir.len); - result->windows_sdk_root = copy_string(mc_allocator, root); - result->windows_sdk_um_library_path = mc_concat(result->windows_sdk_root, um_dir, str_lit("\\")); - } - - if (result->windows_sdk_root.len > 0) { - sdk_found = true; - } } } // NOTE(WalterPlinge): So far this function assumes it will only be called if MSVC was // installed using mmozeiko's portable msvc script, which uses the windows 10 sdk. // This may need to be changed later if it ends up causing problems. - if (sdk_found && result->windows_sdk_version == 0) { + if (sdk_bin_found && sdk_lib_found) { result->windows_sdk_version = 10; } +} + +// NOTE(WalterPlinge): Environment variables can help to find Visual C++ and WinSDK paths for both +// official and portable installations (like mmozeiko's portable msvc script). This will only use +// the first paths it finds, and won't overwrite any values that `result` already has. +void find_visual_studio_paths_from_env_vars(Find_Result *result) { + if (build_context.metrics.arch != TargetArch_amd64 && build_context.metrics.arch != TargetArch_i386) { + return; + } bool vs_found = false; - if (result->vs_exe_path.len > 0 && result->vs_library_path.len > 0) { - vs_found = true; - } - // We can find visual studio using VCToolsInstallDir - if (!vs_found) { - String vctid = mc_get_env(str_lit("VCToolsInstallDir")); - defer (mc_free(vctid)); + String vctid = mc_get_env(str_lit("VCToolsInstallDir")); + defer (mc_free(vctid)); - if (vctid.len) { - String exe = build_context.metrics.arch == TargetArch_amd64 - ? str_lit("bin\\Hostx64\\x64\\") - : str_lit("bin\\Hostx86\\x86\\"); - String lib = build_context.metrics.arch == TargetArch_amd64 - ? str_lit("lib\\x64\\") - : str_lit("lib\\x86\\"); + if (vctid.len) { + String exe = build_context.metrics.arch == TargetArch_amd64 + ? str_lit("bin\\Hostx64\\x64\\") + : str_lit("bin\\Hostx86\\x86\\"); + String lib = build_context.metrics.arch == TargetArch_amd64 + ? str_lit("lib\\x64\\") + : str_lit("lib\\x86\\"); - result->vs_exe_path = mc_concat(vctid, exe); - result->vs_library_path = mc_concat(vctid, lib); - vs_found = true; - } + result->vs_exe_path = mc_concat(vctid, exe); + result->vs_library_path = mc_concat(vctid, lib); + vs_found = true; } // If we haven't found it yet, we can loop through Path for specific folders @@ -701,21 +702,32 @@ bool find_msvc_install_from_env_vars(Find_Result_Utf8 *result) { String exe = build_context.metrics.arch == TargetArch_amd64 ? str_lit("bin\\Hostx64\\x64") : str_lit("bin\\Hostx86\\x86"); + // The environment variable may have an uppercase X even though the folder is lowercase + String exe2 = build_context.metrics.arch == TargetArch_amd64 + ? str_lit("bin\\HostX64\\x64") + : str_lit("bin\\HostX86\\x86"); String lib = build_context.metrics.arch == TargetArch_amd64 ? str_lit("lib\\x64") : str_lit("lib\\x86"); isize lo = {0}; isize hi = {0}; - for (isize c = 0; c < path.len; c += 1) { - if (path[c] != ';') { + for (isize c = 0; c <= path.len; c += 1) { + if (c != path.len && path[c] != ';') { continue; } hi = c; - String dir = substring(path, lo, hi); defer (lo = hi + 1); + // Skip when there are two ;; in a row + if (lo == hi) { + continue; + } + + String dir = substring(path, lo, hi); + + // Remove the last slash so we can match with the strings above String end = dir[dir.len - 1] == '\\' ? substring(dir, 0, dir.len - 1) : substring(dir, 0, dir.len); @@ -726,7 +738,10 @@ bool find_msvc_install_from_env_vars(Find_Result_Utf8 *result) { defer (mc_free(cl)); defer (mc_free(link)); - if (!string_ends_with(end, exe) || !gb_file_exists((char *)cl.text) || !gb_file_exists((char *)link.text)) { + if (!string_ends_with(end, exe) && !string_ends_with(end, exe2)) { + continue; + } + if (!gb_file_exists((char *)cl.text) || !gb_file_exists((char *)link.text)) { continue; } @@ -735,42 +750,36 @@ bool find_msvc_install_from_env_vars(Find_Result_Utf8 *result) { result->vs_library_path = mc_concat(root, lib, str_lit("\\")); vs_found = true; + break; } } } - - return sdk_found && vs_found; } -Find_Result_Utf8 find_visual_studio_and_windows_sdk_utf8() { - Find_Result_Utf8 r = {}; - find_windows_kit_root(&r); - - if (r.windows_sdk_root.len > 0) { - if (build_context.metrics.arch == TargetArch_amd64) { - r.windows_sdk_um_library_path = mc_concat(r.windows_sdk_root, str_lit("um\\x64\\")); - r.windows_sdk_ucrt_library_path = mc_concat(r.windows_sdk_root, str_lit("ucrt\\x64\\")); - } else if (build_context.metrics.arch == TargetArch_i386) { - r.windows_sdk_um_library_path = mc_concat(r.windows_sdk_root, str_lit("um\\x86\\")); - r.windows_sdk_ucrt_library_path = mc_concat(r.windows_sdk_root, str_lit("ucrt\\x86\\")); - } - } - +Find_Result find_visual_studio_and_windows_sdk() { + Find_Result r = {}; + find_windows_kit_paths(&r); find_visual_studio_by_fighting_through_microsoft_craziness(&r); - bool all_found = - r.windows_sdk_root.len > 0 && - r.windows_sdk_um_library_path.len > 0 && - r.windows_sdk_ucrt_library_path.len > 0 && - r.vs_exe_path.len > 0 && - r.vs_library_path.len > 0; + bool sdk_found = + r.windows_sdk_bin_path.len && + r.windows_sdk_um_library_path.len && + r.windows_sdk_ucrt_library_path.len ; - if (!all_found) { - find_msvc_install_from_env_vars(&r); + bool vs_found = + r.vs_exe_path.len && + r.vs_library_path.len ; + + if (!sdk_found) { + find_windows_kit_paths_from_env_vars(&r); + } + + if (!vs_found) { + find_visual_studio_paths_from_env_vars(&r); } #if 0 - printf("windows_sdk_root: %.*s\n", LIT(r.windows_sdk_root)); + printf("windows_sdk_bin_path: %.*s\n", LIT(r.windows_sdk_bin_path)); printf("windows_sdk_um_library_path: %.*s\n", LIT(r.windows_sdk_um_library_path)); printf("windows_sdk_ucrt_library_path: %.*s\n", LIT(r.windows_sdk_ucrt_library_path)); printf("vs_exe_path: %.*s\n", LIT(r.vs_exe_path)); diff --git a/src/odin_compiler.natvis b/src/odin_compiler.natvis new file mode 100644 index 000000000..845eaf1c0 --- /dev/null +++ b/src/odin_compiler.natvis @@ -0,0 +1,28 @@ + + + + {text,[len]s8} + text,[len]s8 + + + {{ size={count} capacity={capacity} }} + + + count + data + + + + + {{ size={count} }} + + + count + data + + + + + Procedure {name} + + diff --git a/src/parser.cpp b/src/parser.cpp index a6f30cdfd..9a5531289 100644 --- a/src/parser.cpp +++ b/src/parser.cpp @@ -356,6 +356,7 @@ Ast *clone_ast(Ast *node) { break; case Ast_PointerType: n->PointerType.type = clone_ast(n->PointerType.type); + n->PointerType.tag = clone_ast(n->PointerType.tag); break; case Ast_MultiPointerType: n->MultiPointerType.type = clone_ast(n->MultiPointerType.type); @@ -2167,10 +2168,11 @@ Ast *parse_operand(AstFile *f, bool lhs) { Ast *original_type = parse_type(f); Ast *type = unparen_expr(original_type); switch (type->kind) { - case Ast_ArrayType: type->ArrayType.tag = tag; break; + case Ast_ArrayType: type->ArrayType.tag = tag; break; case Ast_DynamicArrayType: type->DynamicArrayType.tag = tag; break; + case Ast_PointerType: type->PointerType.tag = tag; break; default: - syntax_error(type, "Expected an array type after #%.*s, got %.*s", LIT(name.string), LIT(ast_strings[type->kind])); + syntax_error(type, "Expected an array or pointer type after #%.*s, got %.*s", LIT(name.string), LIT(ast_strings[type->kind])); break; } return original_type; @@ -2324,11 +2326,7 @@ Ast *parse_operand(AstFile *f, bool lhs) { body = convert_stmt_to_body(f, parse_stmt(f)); f->curr_proc = curr_proc; - if (build_context.disallow_do) { - syntax_error(body, "'do' has been disallowed"); - } else if (!ast_on_same_line(type, body)) { - syntax_error(body, "The body of a 'do' must be on the same line as the signature"); - } + syntax_error(body, "'do' for procedure bodies is not allowed, prefer {}"); return ast_proc_lit(f, type, body, tags, where_token, where_clauses); } @@ -2552,21 +2550,15 @@ Ast *parse_operand(AstFile *f, bool lhs) { syntax_error(tag, "Invalid union tag '#%.*s'", LIT(tag.string)); } } - if (no_nil && maybe) { - syntax_error(f->curr_token, "#maybe and #no_nil cannot be applied together"); - } + if (no_nil && shared_nil) { syntax_error(f->curr_token, "#shared_nil and #no_nil cannot be applied together"); } - if (shared_nil && maybe) { - syntax_error(f->curr_token, "#maybe and #shared_nil cannot be applied together"); - } - if (maybe) { - union_kind = UnionType_maybe; syntax_error(f->curr_token, "#maybe functionality has now been merged with standard 'union' functionality"); - } else if (no_nil) { + } + if (no_nil) { union_kind = UnionType_no_nil; } else if (shared_nil) { union_kind = UnionType_shared_nil; @@ -3554,47 +3546,34 @@ Ast *parse_var_type(AstFile *f, bool allow_ellipsis, bool allow_typeid_token) { } -enum FieldPrefixKind : i32 { - FieldPrefix_Unknown = -1, - FieldPrefix_Invalid = 0, - - FieldPrefix_using, // implies #subtype - FieldPrefix_const, - FieldPrefix_no_alias, - FieldPrefix_c_vararg, - FieldPrefix_auto_cast, - FieldPrefix_any_int, - FieldPrefix_subtype, // does not imply `using` semantics -}; - struct ParseFieldPrefixMapping { String name; TokenKind token_kind; - FieldPrefixKind prefix; FieldFlag flag; }; gb_global ParseFieldPrefixMapping parse_field_prefix_mappings[] = { - {str_lit("using"), Token_using, FieldPrefix_using, FieldFlag_using}, - {str_lit("auto_cast"), Token_auto_cast, FieldPrefix_auto_cast, FieldFlag_auto_cast}, - {str_lit("no_alias"), Token_Hash, FieldPrefix_no_alias, FieldFlag_no_alias}, - {str_lit("c_vararg"), Token_Hash, FieldPrefix_c_vararg, FieldFlag_c_vararg}, - {str_lit("const"), Token_Hash, FieldPrefix_const, FieldFlag_const}, - {str_lit("any_int"), Token_Hash, FieldPrefix_any_int, FieldFlag_any_int}, - {str_lit("subtype"), Token_Hash, FieldPrefix_subtype, FieldFlag_subtype}, + {str_lit("using"), Token_using, FieldFlag_using}, + {str_lit("auto_cast"), Token_auto_cast, FieldFlag_auto_cast}, + {str_lit("no_alias"), Token_Hash, FieldFlag_no_alias}, + {str_lit("c_vararg"), Token_Hash, FieldFlag_c_vararg}, + {str_lit("const"), Token_Hash, FieldFlag_const}, + {str_lit("any_int"), Token_Hash, FieldFlag_any_int}, + {str_lit("subtype"), Token_Hash, FieldFlag_subtype}, + {str_lit("by_ptr"), Token_Hash, FieldFlag_by_ptr}, }; -FieldPrefixKind is_token_field_prefix(AstFile *f) { +FieldFlag is_token_field_prefix(AstFile *f) { switch (f->curr_token.kind) { case Token_EOF: - return FieldPrefix_Invalid; + return FieldFlag_Invalid; case Token_using: - return FieldPrefix_using; + return FieldFlag_using; case Token_auto_cast: - return FieldPrefix_auto_cast; + return FieldFlag_auto_cast; case Token_Hash: advance_token(f); @@ -3604,33 +3583,33 @@ FieldPrefixKind is_token_field_prefix(AstFile *f) { auto const &mapping = parse_field_prefix_mappings[i]; if (mapping.token_kind == Token_Hash) { if (f->curr_token.string == mapping.name) { - return mapping.prefix; + return mapping.flag; } } } break; } - return FieldPrefix_Unknown; + return FieldFlag_Unknown; } - return FieldPrefix_Invalid; + return FieldFlag_Invalid; } u32 parse_field_prefixes(AstFile *f) { i32 counts[gb_count_of(parse_field_prefix_mappings)] = {}; for (;;) { - FieldPrefixKind kind = is_token_field_prefix(f); - if (kind == FieldPrefix_Invalid) { + FieldFlag flag = is_token_field_prefix(f); + if (flag & FieldFlag_Invalid) { break; } - if (kind == FieldPrefix_Unknown) { + if (flag & FieldFlag_Unknown) { syntax_error(f->curr_token, "Unknown prefix kind '#%.*s'", LIT(f->curr_token.string)); advance_token(f); continue; } for (i32 i = 0; i < gb_count_of(parse_field_prefix_mappings); i++) { - if (parse_field_prefix_mappings[i].prefix == kind) { + if (parse_field_prefix_mappings[i].flag == flag) { counts[i] += 1; advance_token(f); break; @@ -3896,7 +3875,8 @@ Ast *parse_field_list(AstFile *f, isize *name_count_, u32 allowed_flags, TokenKi while (f->curr_token.kind != follow && - f->curr_token.kind != Token_EOF) { + f->curr_token.kind != Token_EOF && + f->curr_token.kind != Token_Semicolon) { CommentGroup *docs = f->lead_comment; u32 set_flags = parse_field_prefixes(f); Token tag = {}; @@ -3924,7 +3904,7 @@ Ast *parse_field_list(AstFile *f, isize *name_count_, u32 allowed_flags, TokenKi default_value = parse_expr(f, false); if (!allow_default_parameters) { syntax_error(f->curr_token, "Default parameters are only allowed for procedures"); - default_value = nullptr; + default_value = nullptr; } } diff --git a/src/parser.hpp b/src/parser.hpp index c167ef6d5..bfdae58a5 100644 --- a/src/parser.hpp +++ b/src/parser.hpp @@ -282,6 +282,8 @@ enum StateFlag : u8 { StateFlag_type_assert = 1<<2, StateFlag_no_type_assert = 1<<3, + StateFlag_SelectorCallExpr = 1<<6, + StateFlag_BeenHandled = 1<<7, }; @@ -300,13 +302,18 @@ enum FieldFlag : u32 { FieldFlag_const = 1<<5, FieldFlag_any_int = 1<<6, FieldFlag_subtype = 1<<7, + FieldFlag_by_ptr = 1<<8, // Internal use by the parser only FieldFlag_Tags = 1<<10, FieldFlag_Results = 1<<16, + + FieldFlag_Unknown = 1u<<30, + FieldFlag_Invalid = 1u<<31, + // Parameter List Restrictions - FieldFlag_Signature = FieldFlag_ellipsis|FieldFlag_using|FieldFlag_no_alias|FieldFlag_c_vararg|FieldFlag_auto_cast|FieldFlag_const|FieldFlag_any_int, + FieldFlag_Signature = FieldFlag_ellipsis|FieldFlag_using|FieldFlag_no_alias|FieldFlag_c_vararg|FieldFlag_auto_cast|FieldFlag_const|FieldFlag_any_int|FieldFlag_by_ptr, FieldFlag_Struct = FieldFlag_using|FieldFlag_subtype|FieldFlag_Tags, }; @@ -332,7 +339,6 @@ char const *inline_asm_dialect_strings[InlineAsmDialect_COUNT] = { enum UnionTypeKind : u8 { UnionType_Normal = 0, - UnionType_maybe = 1, // removed UnionType_no_nil = 2, UnionType_shared_nil = 3, }; @@ -411,7 +417,7 @@ AST_KIND(_ExprBegin, "", bool) \ Token ellipsis; \ ProcInlining inlining; \ bool optional_ok_one; \ - void *sce_temp_data; \ + bool was_selector; \ }) \ AST_KIND(FieldValue, "field value", struct { Token eq; Ast *field, *value; }) \ AST_KIND(EnumFieldValue, "enum field value", struct { \ @@ -644,7 +650,8 @@ AST_KIND(_TypeBegin, "", bool) \ }) \ AST_KIND(PointerType, "pointer type", struct { \ Token token; \ - Ast *type; \ + Ast *type; \ + Ast *tag; \ }) \ AST_KIND(RelativeType, "relative type", struct { \ Ast *tag; \ diff --git a/src/string.cpp b/src/string.cpp index 44eccd2d2..bc55e370c 100644 --- a/src/string.cpp +++ b/src/string.cpp @@ -324,6 +324,16 @@ String concatenate3_strings(gbAllocator a, String const &x, String const &y, Str data[len] = 0; return make_string(data, len); } +String concatenate4_strings(gbAllocator a, String const &x, String const &y, String const &z, String const &w) { + isize len = x.len+y.len+z.len+w.len; + u8 *data = gb_alloc_array(a, u8, len+1); + gb_memmove(data, x.text, x.len); + gb_memmove(data+x.len, y.text, y.len); + gb_memmove(data+x.len+y.len, z.text, z.len); + gb_memmove(data+x.len+y.len+z.len, w.text, w.len); + data[len] = 0; + return make_string(data, len); +} String string_join_and_quote(gbAllocator a, Array strings) { if (!strings.count) { diff --git a/src/types.cpp b/src/types.cpp index 5f112ce09..cba27fd6f 100644 --- a/src/types.cpp +++ b/src/types.cpp @@ -278,7 +278,8 @@ struct TypeProc { Type *generic_row_count; \ Type *generic_column_count; \ i64 stride_in_bytes; \ - }) + }) \ + TYPE_KIND(SoaPointer, struct { Type *elem; }) enum TypeKind { @@ -350,6 +351,7 @@ enum Typeid_Kind : u8 { Typeid_Relative_Pointer, Typeid_Relative_Slice, Typeid_Matrix, + Typeid_SoaPointer, }; // IMPORTANT NOTE(bill): This must match the same as the in core.odin @@ -644,6 +646,7 @@ gb_global Type *t_type_info_simd_vector = nullptr; gb_global Type *t_type_info_relative_pointer = nullptr; gb_global Type *t_type_info_relative_slice = nullptr; gb_global Type *t_type_info_matrix = nullptr; +gb_global Type *t_type_info_soa_pointer = nullptr; gb_global Type *t_type_info_named_ptr = nullptr; gb_global Type *t_type_info_integer_ptr = nullptr; @@ -672,6 +675,7 @@ gb_global Type *t_type_info_simd_vector_ptr = nullptr; gb_global Type *t_type_info_relative_pointer_ptr = nullptr; gb_global Type *t_type_info_relative_slice_ptr = nullptr; gb_global Type *t_type_info_matrix_ptr = nullptr; +gb_global Type *t_type_info_soa_pointer_ptr = nullptr; gb_global Type *t_allocator = nullptr; gb_global Type *t_allocator_ptr = nullptr; @@ -735,6 +739,7 @@ Type * bit_set_to_int(Type *t); bool are_types_identical(Type *x, Type *y); bool is_type_pointer(Type *t); +bool is_type_soa_pointer(Type *t); bool is_type_proc(Type *t); bool is_type_slice(Type *t); bool is_type_integer(Type *t); @@ -917,6 +922,13 @@ Type *alloc_type_multi_pointer(Type *elem) { return t; } +Type *alloc_type_soa_pointer(Type *elem) { + Type *t = alloc_type(Type_SoaPointer); + t->SoaPointer.elem = elem; + return t; +} + + Type *alloc_type_array(Type *elem, i64 count, Type *generic_count = nullptr) { if (generic_count != nullptr) { Type *t = alloc_type(Type_Array); @@ -1109,11 +1121,17 @@ Type *type_deref(Type *t) { if (bt == nullptr) { return nullptr; } - if (bt->kind == Type_Pointer) { + switch (bt->kind) { + case Type_Pointer: return bt->Pointer.elem; - } - if (bt->kind == Type_RelativePointer) { + case Type_RelativePointer: return type_deref(bt->RelativePointer.pointer_type); + case Type_SoaPointer: + { + Type *elem = base_type(bt->SoaPointer.elem); + GB_ASSERT(elem->kind == Type_Struct && elem->Struct.soa_kind != StructSoa_None); + return elem->Struct.soa_elem; + } } } return t; @@ -1327,6 +1345,10 @@ bool is_type_pointer(Type *t) { } return t->kind == Type_Pointer; } +bool is_type_soa_pointer(Type *t) { + t = base_type(t); + return t->kind == Type_SoaPointer; +} bool is_type_multi_pointer(Type *t) { t = base_type(t); return t->kind == Type_MultiPointer; @@ -1804,7 +1826,7 @@ bool is_type_dereferenceable(Type *t) { if (is_type_rawptr(t)) { return false; } - return is_type_pointer(t); + return is_type_pointer(t) || is_type_soa_pointer(t); } @@ -2079,6 +2101,9 @@ bool is_type_polymorphic(Type *t, bool or_specialized=false) { case Type_Pointer: return is_type_polymorphic(t->Pointer.elem, or_specialized); + case Type_SoaPointer: + return is_type_polymorphic(t->SoaPointer.elem, or_specialized); + case Type_EnumeratedArray: if (is_type_polymorphic(t->EnumeratedArray.index, or_specialized)) { return true; @@ -2196,6 +2221,7 @@ bool type_has_nil(Type *t) { case Type_Slice: case Type_Proc: case Type_Pointer: + case Type_SoaPointer: case Type_MultiPointer: case Type_DynamicArray: case Type_Map: @@ -2262,6 +2288,8 @@ bool is_type_comparable(Type *t) { return true; case Type_Pointer: return true; + case Type_SoaPointer: + return true; case Type_MultiPointer: return true; case Type_Enum: @@ -2335,6 +2363,7 @@ bool is_type_simple_compare(Type *t) { case Type_Pointer: case Type_MultiPointer: + case Type_SoaPointer: case Type_Proc: case Type_BitSet: return true; @@ -2558,6 +2587,12 @@ bool are_types_identical_internal(Type *x, Type *y, bool check_tuple_names) { } break; + case Type_SoaPointer: + if (y->kind == Type_SoaPointer) { + return are_types_identical(x->SoaPointer.elem, y->SoaPointer.elem); + } + break; + case Type_Named: if (y->kind == Type_Named) { return x->Named.type_name == y->Named.type_name; @@ -3475,6 +3510,9 @@ i64 type_align_of_internal(Type *t, TypePath *path) { return type_align_of_internal(t->RelativePointer.base_integer, path); case Type_RelativeSlice: return type_align_of_internal(t->RelativeSlice.base_integer, path); + + case Type_SoaPointer: + return build_context.word_size; } // return gb_clamp(next_pow2(type_size_of(t)), 1, build_context.max_align); @@ -3580,6 +3618,9 @@ i64 type_size_of_internal(Type *t, TypePath *path) { case Type_MultiPointer: return build_context.word_size; + case Type_SoaPointer: + return build_context.word_size*2; + case Type_Array: { i64 count, align, size, alignment; count = t->Array.count; @@ -4017,6 +4058,11 @@ gbString write_type_to_string(gbString str, Type *type, bool shorthand=false) { str = write_type_to_string(str, type->Pointer.elem); break; + case Type_SoaPointer: + str = gb_string_appendc(str, "#soa ^"); + str = write_type_to_string(str, type->SoaPointer.elem); + break; + case Type_MultiPointer: str = gb_string_appendc(str, "[^]"); str = write_type_to_string(str, type->Pointer.elem); diff --git a/tests/core/encoding/varint/test_core_varint.odin b/tests/core/encoding/varint/test_core_varint.odin index 2c3669afa..ee1798aa7 100644 --- a/tests/core/encoding/varint/test_core_varint.odin +++ b/tests/core/encoding/varint/test_core_varint.odin @@ -79,8 +79,8 @@ test_leb128 :: proc(t: ^testing.T) { } } - for num_bytes in 1..uint(16) { - for _ in 0..RANDOM_TESTS { + for num_bytes in 1..=uint(16) { + for _ in 0..=RANDOM_TESTS { unsigned, signed := get_random(num_bytes) { @@ -109,7 +109,7 @@ test_leb128 :: proc(t: ^testing.T) { get_random :: proc(byte_count: uint) -> (u: u128, i: i128) { assert(byte_count >= 0 && byte_count <= size_of(u128)) - for _ in 1..byte_count { + for _ in 1..=byte_count { u <<= 8 u |= u128(rand.uint32() & 0xff) } diff --git a/tests/core/encoding/xml/test_core_xml.odin b/tests/core/encoding/xml/test_core_xml.odin index a17594b7e..3cfc75a65 100644 --- a/tests/core/encoding/xml/test_core_xml.odin +++ b/tests/core/encoding/xml/test_core_xml.odin @@ -281,7 +281,7 @@ doc_to_string :: proc(doc: ^xml.Document) -> (result: string) { } buf: strings.Builder - defer strings.destroy_builder(&buf) + defer strings.builder_destroy(&buf) print(strings.to_writer(&buf), doc) return strings.clone(strings.to_string(buf)) diff --git a/vendor/OpenGL/helpers.odin b/vendor/OpenGL/helpers.odin index 661de318c..927129130 100644 --- a/vendor/OpenGL/helpers.odin +++ b/vendor/OpenGL/helpers.odin @@ -5,6 +5,7 @@ package odin_gl import "core:os" import "core:fmt" import "core:strings" +_ :: fmt Shader_Type :: enum i32 { NONE = 0x0000, diff --git a/vendor/OpenGL/impl.odin b/vendor/OpenGL/impl.odin index 530865cb0..e9adda4bd 100644 --- a/vendor/OpenGL/impl.odin +++ b/vendor/OpenGL/impl.odin @@ -947,6 +947,13 @@ impl_DrawTransformFeedbackStream: proc "c" (mode: u32, id: u32, stream: u32) impl_BeginQueryIndexed: proc "c" (target: u32, index: u32, id: u32) impl_EndQueryIndexed: proc "c" (target: u32, index: u32) impl_GetQueryIndexediv: proc "c" (target: u32, index: u32, pname: u32, params: [^]i32) +impl_GetTextureHandleARB: proc "c" (texture: u32) -> u64 +impl_GetTextureSamplerHandleARB: proc "c" (texture, sampler: u32) -> u64 +impl_GetImageHandleARB: proc "c" (texture: u32, level: i32, layered: bool, layer: i32, format: u32) -> u64 +impl_MakeTextureHandleResidentARB: proc "c" (handle: u64) +impl_MakeImageHandleResidentARB: proc "c" (handle: u64, access: u32) +impl_MakeTextureHandleNonResidentARB:proc "c" (handle: u64) +impl_MakeImageHandleNonResidentARB: proc "c" (handle: u64) load_4_0 :: proc(set_proc_address: Set_Proc_Address_Type) { set_proc_address(&impl_MinSampleShading, "glMinSampleShading") @@ -995,6 +1002,42 @@ load_4_0 :: proc(set_proc_address: Set_Proc_Address_Type) { set_proc_address(&impl_BeginQueryIndexed, "glBeginQueryIndexed") set_proc_address(&impl_EndQueryIndexed, "glEndQueryIndexed") set_proc_address(&impl_GetQueryIndexediv, "glGetQueryIndexediv") + + // Load ARB (architecture review board, vendor specific) extensions that might be available + set_proc_address(&impl_GetTextureHandleARB, "glGetTextureHandleARB") + if impl_GetTextureHandleARB == nil { + set_proc_address(&impl_GetTextureHandleARB, "glGetTextureHandleNV") + } + + set_proc_address(&impl_GetTextureSamplerHandleARB, "glGetTextureSamplerHandleARB") + if impl_GetTextureSamplerHandleARB == nil { + set_proc_address(&impl_GetTextureSamplerHandleARB, "glGetTextureSamplerHandleNV") + } + + set_proc_address(&impl_GetImageHandleARB, "glGetImageHandleARB") + if impl_GetImageHandleARB == nil { + set_proc_address(&impl_GetImageHandleARB, "glGetImageHandleNV") + } + + set_proc_address(&impl_MakeTextureHandleResidentARB, "glMakeTextureHandleResidentARB") + if impl_MakeTextureHandleResidentARB == nil { + set_proc_address(&impl_MakeTextureHandleResidentARB, "glMakeTextureHandleResidentNV") + } + + set_proc_address(&impl_MakeImageHandleResidentARB, "glMakeImageHandleResidentARB") + if impl_MakeImageHandleResidentARB == nil { + set_proc_address(&impl_MakeImageHandleResidentARB, "glMakeImageHandleResidentNV") + } + + set_proc_address(&impl_MakeTextureHandleNonResidentARB, "glMakeTextureHandleNonResidentARB") + if impl_MakeTextureHandleNonResidentARB == nil { + set_proc_address(&impl_MakeTextureHandleNonResidentARB, "glMakeTextureHandleNonResidentNV") + } + + set_proc_address(&impl_MakeImageHandleNonResidentARB, "glMakeImageHandleNonResidentARB") + if impl_MakeImageHandleNonResidentARB == nil { + set_proc_address(&impl_MakeImageHandleNonResidentARB, "glMakeImageHandleNonResidentNV") + } } @@ -1594,3 +1637,4 @@ load_4_6 :: proc(set_proc_address: Set_Proc_Address_Type) { set_proc_address(&impl_MultiDrawElementsIndirectCount, "glMultiDrawElementsIndirectCount") set_proc_address(&impl_PolygonOffsetClamp, "glPolygonOffsetClamp") } + diff --git a/vendor/OpenGL/wrappers.odin b/vendor/OpenGL/wrappers.odin index c0b5304b0..b62ed216b 100644 --- a/vendor/OpenGL/wrappers.odin +++ b/vendor/OpenGL/wrappers.odin @@ -449,6 +449,20 @@ when !ODIN_DEBUG { BeginQueryIndexed :: proc "c" (target: u32, index: u32, id: u32) { impl_BeginQueryIndexed(target, index, id) } EndQueryIndexed :: proc "c" (target: u32, index: u32) { impl_EndQueryIndexed(target, index) } GetQueryIndexediv :: proc "c" (target: u32, index: u32, pname: u32, params: [^]i32) { impl_GetQueryIndexediv(target, index, pname, params) } + GetTextureHandleARB :: proc "c" (texture: u32) -> u64 + { return impl_GetTextureHandleARB(texture) } + GetTextureSamplerHandleARB :: proc "c" (texture, sampler: u32) -> u64 + { return impl_GetTextureSamplerHandleARB(texture, sampler) } + GetImageHandleARB :: proc "c" (texture: u32, level: i32, layered: bool, layer: i32, format: u32) -> u64 + { return impl_GetImageHandleARB(texture, level, layered, layer, format) } + MakeTextureHandleResidentARB :: proc "c" (handle: u64) + { impl_MakeTextureHandleResidentARB(handle) } + MakeImageHandleResidentARB :: proc "c" (handle: u64, access: u32) + { impl_MakeImageHandleResidentARB(handle, access) } + MakeTextureHandleNonResidentARB:: proc "c" (handle: u64) + { impl_MakeTextureHandleNonResidentARB(handle) } + MakeImageHandleNonResidentARB :: proc "c" (handle: u64) + { impl_MakeImageHandleNonResidentARB(handle) } // VERSION_4_1 ReleaseShaderCompiler :: proc "c" () { impl_ReleaseShaderCompiler() } @@ -1249,6 +1263,22 @@ when !ODIN_DEBUG { BeginQueryIndexed :: proc "c" (target: u32, index: u32, id: u32, loc := #caller_location) { impl_BeginQueryIndexed(target, index, id); debug_helper(loc, 0, target, index, id) } EndQueryIndexed :: proc "c" (target: u32, index: u32, loc := #caller_location) { impl_EndQueryIndexed(target, index); debug_helper(loc, 0, target, index) } GetQueryIndexediv :: proc "c" (target: u32, index: u32, pname: u32, params: [^]i32, loc := #caller_location) { impl_GetQueryIndexediv(target, index, pname, params); debug_helper(loc, 0, target, index, pname, params) } + GetTextureHandleARB :: proc "c" (target: u32, loc := #caller_location) -> u64 + { ret := impl_GetTextureHandleARB(target); debug_helper(loc, 0, target); return ret } + GetTextureSamplerHandleARB :: proc "c" (texture, sampler: u32, loc := #caller_location) -> u64 + { ret := impl_GetTextureSamplerHandleARB(texture, sampler); debug_helper(loc, 0, texture, sampler); return ret } + GetImageHandleARB :: proc "c" (texture: u32, level: i32, layered: bool, layer: i32, format: u32, loc := #caller_location) -> u64 + { ret := impl_GetImageHandleARB(texture, level, layered, layer, format); debug_helper(loc, 0, texture, level, layered, layer, format); return ret } + MakeTextureHandleResidentARB :: proc "c" (handle: u64, loc := #caller_location) + { impl_MakeTextureHandleResidentARB(handle); debug_helper(loc, 0, handle) } + MakeImageHandleResidentARB :: proc "c" (handle: u64, access: u32, loc := #caller_location) + { impl_MakeImageHandleResidentARB(handle, access); debug_helper(loc, 0, handle, access) } + MakeTextureHandleNonResidentARB:: proc "c" (handle: u64, loc := #caller_location) + { impl_MakeTextureHandleNonResidentARB(handle); debug_helper(loc, 0, handle) } + MakeImageHandleNonResidentARB :: proc "c" (handle: u64, loc := #caller_location) + { impl_MakeImageHandleNonResidentARB(handle); debug_helper(loc, 0, handle) } + + // VERSION_4_1 ReleaseShaderCompiler :: proc "c" (loc := #caller_location) { impl_ReleaseShaderCompiler(); debug_helper(loc, 0) } diff --git a/vendor/directx/d3d11/d3d11.odin b/vendor/directx/d3d11/d3d11.odin index 2adb7925a..db1611bc4 100644 --- a/vendor/directx/d3d11/d3d11.odin +++ b/vendor/directx/d3d11/d3d11.odin @@ -3625,3 +3625,36 @@ IFunctionLinkingGraph_VTable :: struct { GetLastError: proc "stdcall" (this: ^IFunctionLinkingGraph, ppErrorBuffer: ^^IBlob) -> HRESULT, GenerateHlsl: proc "stdcall" (this: ^IFunctionLinkingGraph, uFlags: u32, ppBuffer: ^^IBlob) -> HRESULT, } + +IDebug_UUID_STRING :: "79CF2233-7536-4948-9D36-1E4692DC5760" +IDebug_UUID := &IID{0x79CF2233, 0x7536, 0x4948, {0x9D, 0x36, 0x1E, 0x46, 0x92, 0xDC, 0x57, 0x60}} + +IDebug :: struct #raw_union { + #subtype iunknown: IUnknown, + using id3d11debug_vtable: ^IDebug_VTable, +} + +RLDO_FLAGS :: enum u32 { // TODO: make bit_set + SUMMARY = 0x1, + DETAIL = 0x2, + IGNORE_INTERNAL = 0x4, +} + +DEBUG_FEATURE :: enum u32 { // TODO: make bit_set + FLUSH_PER_RENDER_OP = 0x1, + FINISH_PER_RENDER_OP = 0x2, + FEATURE_PRESENT_PER_RENDER_OP = 0x4, +} + +IDebug_VTable :: struct { + using iunkown_vtable: IUnknown_VTable, + SetFeatureMask: proc "stdcall" (this: ^IDebug, mask: DEBUG_FEATURE) -> HRESULT, + GetFeatureMask: proc "stdcall" (this: ^IDebug) -> DEBUG_FEATURE, + SetPresentPerRenderOpDelay: proc "stdcall" (this: ^IDebug, Milliseconds: u32) -> HRESULT, + GetPresentPerRenderOpDelay: proc "stdcall" (this: ^IDebug) -> u32, + SetSwapChain: proc "stdcall" (this: ^IDebug, pSwapChain: ^dxgi.ISwapChain) -> HRESULT, + GetSwapChain: proc "stdcall" (this: ^IDebug, ppSwapChain: ^^dxgi.ISwapChain) -> HRESULT, + ValidateContext: proc "stdcall" (this: ^IDebug, pContext: ^IDeviceContext) -> HRESULT, + ReportLiveDeviceObjects: proc "stdcall" (this: ^IDebug, Flags: RLDO_FLAGS) -> HRESULT, + ValidateContextForDispatch: proc "stdcall" (this: ^IDebug, pContext: ^IDeviceContext) -> HRESULT, +} diff --git a/vendor/directx/dxgi/dxgi.odin b/vendor/directx/dxgi/dxgi.odin index ae0cfd17a..24b85e9f3 100644 --- a/vendor/directx/dxgi/dxgi.odin +++ b/vendor/directx/dxgi/dxgi.odin @@ -38,10 +38,10 @@ IUnknown_VTable :: struct { @(default_calling_convention="stdcall") foreign dxgi { - CreateDXGIFactory :: proc(riid: ^IID, ppFactory: rawptr) -> HRESULT --- - CreateDXGIFactory1 :: proc(riid: ^IID, ppFactory: rawptr) -> HRESULT --- - CreateDXGIFactory2 :: proc(Flags: u32, riid: ^IID, ppFactory: rawptr) -> HRESULT --- - DXGIGetDebugInterface1 :: proc(Flags: u32, riid: ^IID, pDebug: rawptr) -> HRESULT --- + CreateDXGIFactory :: proc(riid: ^IID, ppFactory: ^rawptr) -> HRESULT --- + CreateDXGIFactory1 :: proc(riid: ^IID, ppFactory: ^rawptr) -> HRESULT --- + CreateDXGIFactory2 :: proc(Flags: u32, riid: ^IID, ppFactory: ^rawptr) -> HRESULT --- + DXGIGetDebugInterface1 :: proc(Flags: u32, riid: ^IID, pDebug: ^rawptr) -> HRESULT --- } STANDARD_MULTISAMPLE_QUALITY_PATTERN :: 0xffffffff diff --git a/vendor/glfw/bindings/bindings.odin b/vendor/glfw/bindings/bindings.odin index aa1578153..aea09e31d 100644 --- a/vendor/glfw/bindings/bindings.odin +++ b/vendor/glfw/bindings/bindings.odin @@ -13,6 +13,13 @@ when ODIN_OS == .Windows { } else when ODIN_OS == .Linux { // TODO: Add the billion-or-so static libs to link to in linux foreign import glfw "system:glfw" +} else when ODIN_OS == .Darwin { + foreign import glfw { + "../lib/darwin/libglfw3.a", + "system:Cocoa.framework", + "system:IOKit.framework", + "system:OpenGL.framework", + } } else { foreign import glfw "system:glfw" } diff --git a/vendor/glfw/constants.odin b/vendor/glfw/constants.odin index 77cbd7309..245cfef52 100644 --- a/vendor/glfw/constants.odin +++ b/vendor/glfw/constants.odin @@ -4,7 +4,7 @@ package glfw /* Versions */ VERSION_MAJOR :: 3 VERSION_MINOR :: 3 -VERSION_REVISION :: 4 +VERSION_REVISION :: 8 /* Booleans */ TRUE :: true diff --git a/vendor/glfw/lib/darwin/libglfw3.a b/vendor/glfw/lib/darwin/libglfw3.a new file mode 100644 index 000000000..77506567f Binary files /dev/null and b/vendor/glfw/lib/darwin/libglfw3.a differ diff --git a/vendor/glfw/lib/glfw3.dll b/vendor/glfw/lib/glfw3.dll index 0359bb4f3..733404cf4 100644 Binary files a/vendor/glfw/lib/glfw3.dll and b/vendor/glfw/lib/glfw3.dll differ diff --git a/vendor/glfw/lib/glfw3.lib b/vendor/glfw/lib/glfw3.lib index e59770e65..2a52eb4eb 100644 Binary files a/vendor/glfw/lib/glfw3.lib and b/vendor/glfw/lib/glfw3.lib differ diff --git a/vendor/glfw/lib/glfw3_mt.lib b/vendor/glfw/lib/glfw3_mt.lib index 231030e92..30ba4adab 100644 Binary files a/vendor/glfw/lib/glfw3_mt.lib and b/vendor/glfw/lib/glfw3_mt.lib differ diff --git a/vendor/glfw/lib/glfw3dll.lib b/vendor/glfw/lib/glfw3dll.lib index cfcf1197b..e1b3f3efc 100644 Binary files a/vendor/glfw/lib/glfw3dll.lib and b/vendor/glfw/lib/glfw3dll.lib differ diff --git a/vendor/microui/microui.odin b/vendor/microui/microui.odin index 09a6b8430..7967f7f4b 100644 --- a/vendor/microui/microui.odin +++ b/vendor/microui/microui.odin @@ -353,7 +353,7 @@ end :: proc(ctx: ^Context) { /* reset input state */ ctx.key_pressed_bits = {} // clear - strings.reset_builder(&ctx.text_input) + strings.builder_reset(&ctx.text_input) ctx.mouse_pressed_bits = {} // clear ctx.mouse_released_bits = {} // clear ctx.scroll_delta = Vec2{0, 0} diff --git a/vendor/raylib/macos-arm64/libraylib.4.0.0.dylib b/vendor/raylib/macos-arm64/libraylib.4.0.0.dylib new file mode 100644 index 000000000..a40219baa Binary files /dev/null and b/vendor/raylib/macos-arm64/libraylib.4.0.0.dylib differ diff --git a/vendor/raylib/macos-arm64/libraylib.400.dylib b/vendor/raylib/macos-arm64/libraylib.400.dylib new file mode 100644 index 000000000..a40219baa Binary files /dev/null and b/vendor/raylib/macos-arm64/libraylib.400.dylib differ diff --git a/vendor/raylib/macos-arm64/libraylib.a b/vendor/raylib/macos-arm64/libraylib.a new file mode 100644 index 000000000..5eddcb8fa Binary files /dev/null and b/vendor/raylib/macos-arm64/libraylib.a differ diff --git a/vendor/raylib/macos-arm64/libraylib.dylib b/vendor/raylib/macos-arm64/libraylib.dylib new file mode 100644 index 000000000..a40219baa Binary files /dev/null and b/vendor/raylib/macos-arm64/libraylib.dylib differ diff --git a/vendor/raylib/raylib.odin b/vendor/raylib/raylib.odin index 3fc3e051f..a694c83a4 100644 --- a/vendor/raylib/raylib.odin +++ b/vendor/raylib/raylib.odin @@ -106,11 +106,20 @@ when ODIN_OS == .Windows { "system:pthread", } } else when ODIN_OS == .Darwin { - foreign import lib { - "macos/libraylib.a", - "system:Cocoa.framework", - "system:OpenGL.framework", - "system:IOKit.framework", + when ODIN_ARCH == .arm64 { + foreign import lib { + "macos-arm64/libraylib.a", + "system:Cocoa.framework", + "system:OpenGL.framework", + "system:IOKit.framework", + } + } else { + foreign import lib { + "macos/libraylib.a", + "system:Cocoa.framework", + "system:OpenGL.framework", + "system:IOKit.framework", + } } } else { foreign import lib "system:raylib" diff --git a/vendor/stb/image/stb_image_resize.odin b/vendor/stb/image/stb_image_resize.odin index 362ec9315..5763e142a 100644 --- a/vendor/stb/image/stb_image_resize.odin +++ b/vendor/stb/image/stb_image_resize.odin @@ -185,4 +185,4 @@ foreign lib { space: colorspace, alloc_context: rawptr, s0, t0, s1, t1: f32) -> c.int --- -} \ No newline at end of file +} diff --git a/vendor/stb/lib/darwin/libstb_image.a b/vendor/stb/lib/darwin/libstb_image.a new file mode 100644 index 000000000..06ce44321 Binary files /dev/null and b/vendor/stb/lib/darwin/libstb_image.a differ diff --git a/vendor/stb/lib/darwin/stb_image.a b/vendor/stb/lib/darwin/stb_image.a new file mode 100644 index 000000000..1379d6f9e Binary files /dev/null and b/vendor/stb/lib/darwin/stb_image.a differ diff --git a/vendor/stb/lib/darwin/stb_image_resize.a b/vendor/stb/lib/darwin/stb_image_resize.a new file mode 100644 index 000000000..f39c507a6 Binary files /dev/null and b/vendor/stb/lib/darwin/stb_image_resize.a differ diff --git a/vendor/stb/lib/darwin/stb_image_write.a b/vendor/stb/lib/darwin/stb_image_write.a new file mode 100644 index 000000000..bce02b33d Binary files /dev/null and b/vendor/stb/lib/darwin/stb_image_write.a differ diff --git a/vendor/stb/lib/darwin/stb_rect_pack.a b/vendor/stb/lib/darwin/stb_rect_pack.a new file mode 100644 index 000000000..3b55ab802 Binary files /dev/null and b/vendor/stb/lib/darwin/stb_rect_pack.a differ diff --git a/vendor/stb/lib/darwin/stb_truetype.a b/vendor/stb/lib/darwin/stb_truetype.a new file mode 100644 index 000000000..c4a895b54 Binary files /dev/null and b/vendor/stb/lib/darwin/stb_truetype.a differ diff --git a/vendor/wasm/README.md b/vendor/wasm/README.md index 55cbd1b7a..d0b0a2f6f 100644 --- a/vendor/wasm/README.md +++ b/vendor/wasm/README.md @@ -10,6 +10,6 @@ The `js_wasm32` target assumes that the WASM output will be ran within a web bro -``` \ No newline at end of file +``` diff --git a/vendor/wasm/js/events.odin b/vendor/wasm/js/events.odin index dd9524adb..136c0610d 100644 --- a/vendor/wasm/js/events.odin +++ b/vendor/wasm/js/events.odin @@ -79,6 +79,8 @@ Event_Kind :: enum u32 { Context_Menu, + Custom, + } event_kind_string := [Event_Kind]string{ .Invalid = "", @@ -155,6 +157,8 @@ event_kind_string := [Event_Kind]string{ .Touch_Start = "touchstart", .Context_Menu = "contextmenu", + + .Custom = "?custom?", } Delta_Mode :: enum u32 { @@ -186,6 +190,13 @@ Event_Phase :: enum u8 { Bubbling_Phase = 3, } +Event_Option :: enum u8 { + Bubbles = 0, + Cancelable = 1, + Composed = 2, +} +Event_Options :: distinct bit_set[Event_Option; u8] + Event :: struct { kind: Event_Kind, target_kind: Event_Target_Kind, @@ -194,9 +205,7 @@ Event :: struct { timestamp: f64, phase: Event_Phase, - bubbles: bool, - cancelable: bool, - composed: bool, + options: Event_Options, is_composing: bool, is_trusted: bool, @@ -255,6 +264,7 @@ foreign dom_lib { event_stop_propagation :: proc() --- event_stop_immediate_propagation :: proc() --- event_prevent_default :: proc() --- + dispatch_custom_event :: proc(id: string, name: string, options := Event_Options{}) -> bool --- } add_event_listener :: proc(id: string, kind: Event_Kind, user_data: rawptr, callback: proc(e: Event), use_capture := false) -> bool { @@ -301,6 +311,23 @@ remove_event_listener_from_event :: proc(e: Event) -> bool { return remove_event_listener(e.id, e.kind, e.user_data, e.callback) } +add_custom_event_listener :: proc(id: string, name: string, user_data: rawptr, callback: proc(e: Event), use_capture := false) -> bool { + @(default_calling_convention="contextless") + foreign dom_lib { + @(link_name="add_event_listener") + _add_event_listener :: proc(id: string, name: string, name_code: Event_Kind, user_data: rawptr, callback: proc "odin" (Event), use_capture: bool) -> bool --- + } + return _add_event_listener(id, name, .Custom, user_data, callback, use_capture) +} +remove_custom_event_listener :: proc(id: string, name: string, user_data: rawptr, callback: proc(e: Event)) -> bool { + @(default_calling_convention="contextless") + foreign dom_lib { + @(link_name="remove_event_listener") + _remove_event_listener :: proc(id: string, name: string, user_data: rawptr, callback: proc "odin" (Event)) -> bool --- + } + return _remove_event_listener(id, name, user_data, callback) +} + diff --git a/vendor/wasm/js/runtime.js b/vendor/wasm/js/runtime.js index 424a9a4db..60d114506 100644 --- a/vendor/wasm/js/runtime.js +++ b/vendor/wasm/js/runtime.js @@ -1,6 +1,14 @@ "use strict"; (function() { + +function getElement(name) { + if (name) { + return document.getElementById(name); + } + return undefined; +} + class WasmMemoryInterface { constructor() { this.memory = null; @@ -204,12 +212,12 @@ class WebGLInterface { return { SetCurrentContextById: (name_ptr, name_len) => { let name = this.mem.loadString(name_ptr, name_len); - let element = document.getElementById(name); + let element = getElement(name); return this.setCurrentContext(element, {alpha: true, antialias: true, depth: true, premultipliedAlpha: true}); }, CreateCurrentContextById: (name_ptr, name_len, attributes) => { let name = this.mem.loadString(name_ptr, name_len); - let element = document.getElementById(name); + let element = getElement(name); let contextSettings = { alpha: !(attributes & (1<<0)), @@ -1380,9 +1388,11 @@ function odinSetupDefaultImports(wasmMemoryInterface, consoleElement) { wmi.storeF64(off(8), e.timeStamp*1e-3); wmi.storeU8(off(1), e.eventPhase); - wmi.storeU8(off(1), !!e.bubbles); - wmi.storeU8(off(1), !!e.cancelable); - wmi.storeU8(off(1), !!e.composed); + let options = 0; + if (!!e.bubbles) { options |= 1<<0; } + if (!!e.cancelable) { options |= 1<<1; } + if (!!e.composed) { options |= 1<<2; } + wmi.storeU8(off(1), options); wmi.storeU8(off(1), !!e.isComposing); wmi.storeU8(off(1), !!e.isTrusted); @@ -1433,7 +1443,7 @@ function odinSetupDefaultImports(wasmMemoryInterface, consoleElement) { add_event_listener: (id_ptr, id_len, name_ptr, name_len, name_code, data, callback, use_capture) => { let id = wasmMemoryInterface.loadString(id_ptr, id_len); let name = wasmMemoryInterface.loadString(name_ptr, name_len); - let element = document.getElementById(id); + let element = getElement(id); if (element == undefined) { return false; } @@ -1454,7 +1464,7 @@ function odinSetupDefaultImports(wasmMemoryInterface, consoleElement) { remove_event_listener: (id_ptr, id_len, name_ptr, name_len, data, callback) => { let id = wasmMemoryInterface.loadString(id_ptr, id_len); let name = wasmMemoryInterface.loadString(name_ptr, name_len); - let element = document.getElementById(id); + let element = getElement(id); if (element == undefined) { return false; } @@ -1514,14 +1524,31 @@ function odinSetupDefaultImports(wasmMemoryInterface, consoleElement) { } }, + dispatch_custom_event: (id_ptr, id_len, name_ptr, name_len, options_bits) => { + let id = wasmMemoryInterface.loadString(id_ptr, id_len); + let name = wasmMemoryInterface.loadString(name_ptr, name_len); + let options = { + bubbles: (options_bits & (1<<0)) !== 0, + cancelabe: (options_bits & (1<<1)) !== 0, + composed: (options_bits & (1<<2)) !== 0, + }; + + let element = getElement(id); + if (element) { + element.dispatchEvent(new Event(name, options)); + return true; + } + return false; + }, + get_element_value_f64: (id_ptr, id_len) => { let id = wasmMemoryInterface.loadString(id_ptr, id_len); - let element = document.getElementById(id); + let element = getElement(id); return element ? element.value : 0; }, get_element_value_string: (id_ptr, id_len, buf_ptr, buf_len) => { let id = wasmMemoryInterface.loadString(id_ptr, id_len); - let element = document.getElementById(id); + let element = getElement(id); if (element) { let str = element.value; if (buf_len > 0 && buf_ptr) { @@ -1535,7 +1562,7 @@ function odinSetupDefaultImports(wasmMemoryInterface, consoleElement) { }, get_element_value_string_length: (id_ptr, id_len) => { let id = wasmMemoryInterface.loadString(id_ptr, id_len); - let element = document.getElementById(id); + let element = getElement(id); if (element) { return element.value.length; } @@ -1543,7 +1570,7 @@ function odinSetupDefaultImports(wasmMemoryInterface, consoleElement) { }, get_element_min_max: (ptr_array2_f64, id_ptr, id_len) => { let id = wasmMemoryInterface.loadString(id_ptr, id_len); - let element = document.getElementById(id); + let element = getElement(id); if (element) { let values = wasmMemoryInterface.loadF64Array(ptr_array2_f64, 2); values[0] = element.min; @@ -1552,7 +1579,7 @@ function odinSetupDefaultImports(wasmMemoryInterface, consoleElement) { }, set_element_value_f64: (id_ptr, id_len, value) => { let id = wasmMemoryInterface.loadString(id_ptr, id_len); - let element = document.getElementById(id); + let element = getElement(id); if (element) { element.value = value; } @@ -1560,7 +1587,7 @@ function odinSetupDefaultImports(wasmMemoryInterface, consoleElement) { set_element_value_string: (id_ptr, id_len, value_ptr, value_id) => { let id = wasmMemoryInterface.loadString(id_ptr, id_len); let value = wasmMemoryInterface.loadString(value_ptr, value_len); - let element = document.getElementById(id); + let element = getElement(id); if (element) { element.value = value; } @@ -1569,7 +1596,7 @@ function odinSetupDefaultImports(wasmMemoryInterface, consoleElement) { get_bounding_client_rect: (rect_ptr, id_ptr, id_len) => { let id = wasmMemoryInterface.loadString(id_ptr, id_len); - let element = document.getElementById(id); + let element = getElement(id); if (element) { let values = wasmMemoryInterface.loadF64Array(rect_ptr, 4); let rect = element.getBoundingClientRect();