Merge branch 'master' into windows-llvm-11.1.0

This commit is contained in:
gingerBill
2022-02-07 11:27:30 +00:00
14 changed files with 2181 additions and 1765 deletions

3
.gitignore vendored
View File

@@ -7,6 +7,9 @@
# User-specific files (MonoDevelop/Xamarin Studio)
*.userprefs
# For macOS
.DS_Store
# Build results
[Dd]ebug/
[Dd]ebugPublic/

View File

@@ -11,11 +11,119 @@ INDEX_SHIFT :: 6
@(private="file")
INDEX_MASK :: 63
@(private="file")
NUM_BITS :: 64
Bit_Array :: struct {
bits: [dynamic]u64,
bias: int,
max_index: int,
}
Bit_Array_Iterator :: struct {
array: ^Bit_Array,
word_idx: int,
bit_idx: uint,
}
/*
In:
- ba: ^Bit_Array - the array to iterate over
Out:
- it: ^Bit_Array_Iterator - the iterator that holds iteration state
*/
make_iterator :: proc (ba: ^Bit_Array) -> (it: Bit_Array_Iterator) {
return Bit_Array_Iterator { array = ba }
}
/*
In:
- it: ^Bit_Array_Iterator - the iterator struct that holds the state.
Out:
- set: bool - the state of the bit at `index`
- index: int - the next bit of the Bit_Array referenced by `it`.
- ok: bool - `true` if the iterator returned a valid index,
`false` if there were no more bits
*/
iterate_by_all :: proc (it: ^Bit_Array_Iterator) -> (set: bool, index: int, ok: bool) {
index = it.word_idx * NUM_BITS + int(it.bit_idx) + it.array.bias
if index > it.array.max_index { return false, 0, false }
word := it.array.bits[it.word_idx] if len(it.array.bits) > it.word_idx else 0
set = (word >> it.bit_idx & 1) == 1
it.bit_idx += 1
if it.bit_idx >= NUM_BITS {
it.bit_idx = 0
it.word_idx += 1
}
return set, index, true
}
/*
In:
- it: ^Bit_Array_Iterator - the iterator struct that holds the state.
Out:
- index: int - the next set bit of the Bit_Array referenced by `it`.
- ok: bool - `true` if the iterator returned a valid index,
`false` if there were no more bits set
*/
iterate_by_set :: proc (it: ^Bit_Array_Iterator) -> (index: int, ok: bool) {
return iterate_internal_(it, true)
}
/*
In:
- it: ^Bit_Array_Iterator - the iterator struct that holds the state.
Out:
- index: int - the next unset bit of the Bit_Array referenced by `it`.
- ok: bool - `true` if the iterator returned a valid index,
`false` if there were no more unset bits
*/
iterate_by_unset:: proc (it: ^Bit_Array_Iterator) -> (index: int, ok: bool) {
return iterate_internal_(it, false)
}
@(private="file")
iterate_internal_ :: proc (it: ^Bit_Array_Iterator, $ITERATE_SET_BITS: bool) -> (index: int, ok: bool) {
word := it.array.bits[it.word_idx] if len(it.array.bits) > it.word_idx else 0
when ! ITERATE_SET_BITS { word = ~word }
// if the word is empty or we have already gone over all the bits in it,
// b.bit_idx is greater than the index of any set bit in the word,
// meaning that word >> b.bit_idx == 0.
for it.word_idx < len(it.array.bits) && word >> it.bit_idx == 0 {
it.word_idx += 1
it.bit_idx = 0
word = it.array.bits[it.word_idx] if len(it.array.bits) > it.word_idx else 0
when ! ITERATE_SET_BITS { word = ~word }
}
// if we are iterating the set bits, reaching the end of the array means we have no more bits to check
when ITERATE_SET_BITS {
if it.word_idx >= len(it.array.bits) {
return 0, false
}
}
// reaching here means that the word has some set bits
it.bit_idx += uint(intrinsics.count_trailing_zeros(word >> it.bit_idx))
index = it.word_idx * NUM_BITS + int(it.bit_idx) + it.array.bias
it.bit_idx += 1
if it.bit_idx >= NUM_BITS {
it.bit_idx = 0
it.word_idx += 1
}
return index, index <= it.array.max_index
}
/*
In:
- ba: ^Bit_Array - a pointer to the Bit Array
@@ -70,6 +178,7 @@ set :: proc(ba: ^Bit_Array, #any_int index: uint, allocator := context.allocator
resize_if_needed(ba, leg_index) or_return
ba.max_index = max(idx, ba.max_index)
ba.bits[leg_index] |= 1 << uint(bit_index)
return true
}
@@ -87,6 +196,7 @@ create :: proc(max_index: int, min_index := 0, allocator := context.allocator) -
res = Bit_Array{
bias = min_index,
max_index = max_index,
}
return res, resize_if_needed(&res, legs)
}
@@ -121,4 +231,4 @@ resize_if_needed :: proc(ba: ^Bit_Array, legs: int, allocator := context.allocat
resize(&ba.bits, legs + 1)
}
return len(ba.bits) > legs
}
}

View File

@@ -151,6 +151,7 @@ Comp_Lit :: struct {
open: tokenizer.Pos,
elems: []^Expr,
close: tokenizer.Pos,
tag: ^Expr,
}

View File

@@ -2273,6 +2273,24 @@ parse_operand :: proc(p: ^Parser, lhs: bool) -> ^ast.Expr {
return original_type
case "partial":
tag := ast.new(ast.Basic_Directive, tok.pos, end_pos(name))
tag.tok = tok
tag.name = name.text
original_expr := parse_expr(p, lhs)
expr := ast.unparen_expr(original_expr)
switch t in &expr.derived {
case ast.Comp_Lit:
t.tag = tag
case ast.Array_Type:
t.tag = tag
error(p, tok.pos, "#%s has been replaced with #sparse for non-contiguous enumerated array types", name.text)
case:
error(p, tok.pos, "expected a compound literal after #%s", name.text)
}
return original_expr
case "sparse":
tag := ast.new(ast.Basic_Directive, tok.pos, end_pos(name))
tag.tok = tok
tag.name = name.text

View File

@@ -290,12 +290,20 @@ foreign libc {
@(link_name="fstat64") _unix_fstat :: proc(fd: Handle, stat: ^OS_Stat) -> c.int ---
@(link_name="readlink") _unix_readlink :: proc(path: cstring, buf: ^byte, bufsiz: c.size_t) -> c.ssize_t ---
@(link_name="access") _unix_access :: proc(path: cstring, mask: int) -> int ---
@(link_name="fdopendir$INODE64") _unix_fdopendir :: proc(fd: Handle) -> Dir ---
@(link_name="fdopendir$INODE64") _unix_fdopendir_amd64 :: proc(fd: Handle) -> Dir ---
@(link_name="readdir_r$INODE64") _unix_readdir_r_amd64 :: proc(dirp: Dir, entry: ^Dirent, result: ^^Dirent) -> c.int ---
@(link_name="fdopendir") _unix_fdopendir_arm64 :: proc(fd: Handle) -> Dir ---
@(link_name="readdir_r") _unix_readdir_r_arm64 :: proc(dirp: Dir, entry: ^Dirent, result: ^^Dirent) -> c.int ---
@(link_name="closedir") _unix_closedir :: proc(dirp: Dir) -> c.int ---
@(link_name="rewinddir") _unix_rewinddir :: proc(dirp: Dir) ---
@(link_name="readdir_r$INODE64") _unix_readdir_r :: proc(dirp: Dir, entry: ^Dirent, result: ^^Dirent) -> c.int ---
@(link_name="fcntl") _unix_fcntl :: proc(fd: Handle, cmd: c.int, buf: ^byte) -> c.int ---
@(link_name="rename") _unix_rename :: proc(old: cstring, new: cstring) -> c.int ---
@(link_name="remove") _unix_remove :: proc(path: cstring) -> c.int ---
@(link_name="fchmod") _unix_fchmod :: proc(fildes: Handle, mode: u16) -> c.int ---
@(link_name="malloc") _unix_malloc :: proc(size: int) -> rawptr ---
@@ -312,6 +320,14 @@ foreign libc {
@(link_name="exit") _unix_exit :: proc(status: c.int) -> ! ---
}
when ODIN_ARCH != "arm64" {
_unix_fdopendir :: proc {_unix_fdopendir_amd64}
_unix_readdir_r :: proc {_unix_readdir_r_amd64}
} else {
_unix_fdopendir :: proc {_unix_fdopendir_arm64}
_unix_readdir_r :: proc {_unix_readdir_r_arm64}
}
foreign dl {
@(link_name="dlopen") _unix_dlopen :: proc(filename: cstring, flags: int) -> rawptr ---
@(link_name="dlsym") _unix_dlsym :: proc(handle: rawptr, symbol: cstring) -> rawptr ---
@@ -412,6 +428,65 @@ is_path_separator :: proc(r: rune) -> bool {
return r == '/'
}
is_file_handle :: proc(fd: Handle) -> bool {
s, err := _fstat(fd)
if err != ERROR_NONE {
return false
}
return S_ISREG(cast(u32)s.mode)
}
is_file_path :: proc(path: string, follow_links: bool = true) -> bool {
s: OS_Stat
err: Errno
if follow_links {
s, err = _stat(path)
} else {
s, err = _lstat(path)
}
if err != ERROR_NONE {
return false
}
return S_ISREG(cast(u32)s.mode)
}
is_dir_handle :: proc(fd: Handle) -> bool {
s, err := _fstat(fd)
if err != ERROR_NONE {
return false
}
return S_ISDIR(cast(u32)s.mode)
}
is_dir_path :: proc(path: string, follow_links: bool = true) -> bool {
s: OS_Stat
err: Errno
if follow_links {
s, err = _stat(path)
} else {
s, err = _lstat(path)
}
if err != ERROR_NONE {
return false
}
return S_ISDIR(cast(u32)s.mode)
}
is_file :: proc {is_file_path, is_file_handle}
is_dir :: proc {is_dir_path, is_dir_handle}
rename :: proc(old: string, new: string) -> bool {
old_cstr := strings.clone_to_cstring(old, context.temp_allocator)
new_cstr := strings.clone_to_cstring(new, context.temp_allocator)
return _unix_rename(old_cstr, new_cstr) != -1
}
remove :: proc(path: string) -> bool {
path_cstr := strings.clone_to_cstring(path, context.temp_allocator)
return _unix_remove(path_cstr) != -1
}
@private
_stat :: proc(path: string) -> (OS_Stat, Errno) {

View File

@@ -71,7 +71,7 @@ _walk :: proc(info: os.File_Info, walk_proc: Walk_Proc) -> (err: os.Errno, skip_
@(private)
read_dir :: proc(dir_name: string, allocator := context.temp_allocator) -> ([]os.File_Info, os.Errno) {
f, err := os.open(dir_name)
f, err := os.open(dir_name, os.O_RDONLY)
if err != 0 {
return nil, err
}

View File

@@ -2183,9 +2183,43 @@ bool check_builtin_procedure(CheckerContext *c, Operand *operand, Ast *call, i32
}
operand->mode = Addressing_Value;
if (is_type_array(t)) {
if (t->kind == Type_Array) {
i32 rank = type_math_rank(t);
// Do nothing
operand->type = x.type;
operand->type = x.type;
if (rank > 2) {
gbString s = type_to_string(x.type);
error(call, "'%.*s' expects a matrix or array with a rank of 2, got %s of rank %d", LIT(builtin_name), s, rank);
gb_string_free(s);
return false;
} else if (rank == 2) {
Type *inner = base_type(t->Array.elem);
GB_ASSERT(inner->kind == Type_Array);
Type *elem = inner->Array.elem;
Type *array_inner = alloc_type_array(elem, t->Array.count);
Type *array_outer = alloc_type_array(array_inner, inner->Array.count);
operand->type = array_outer;
i64 elements = t->Array.count*inner->Array.count;
i64 size = type_size_of(operand->type);
if (!is_type_valid_for_matrix_elems(elem)) {
gbString s = type_to_string(x.type);
error(call, "'%.*s' expects a matrix or array with a base element type of an integer, float, or complex number, got %s", LIT(builtin_name), s);
gb_string_free(s);
} else if (elements > MATRIX_ELEMENT_COUNT_MAX) {
gbString s = type_to_string(x.type);
error(call, "'%.*s' expects a matrix or array with a maximum of %d elements, got %s with %lld elements", LIT(builtin_name), MATRIX_ELEMENT_COUNT_MAX, s, elements);
gb_string_free(s);
} else if (elements > MATRIX_ELEMENT_COUNT_MAX) {
gbString s = type_to_string(x.type);
error(call, "'%.*s' expects a matrix or array with non-zero elements, got %s", LIT(builtin_name), MATRIX_ELEMENT_COUNT_MAX, s);
gb_string_free(s);
} else if (size > MATRIX_ELEMENT_MAX_SIZE) {
gbString s = type_to_string(x.type);
error(call, "Too large of a type for '%.*s', got %s of size %lld, maximum size %d", LIT(builtin_name), s, cast(long long)size, MATRIX_ELEMENT_MAX_SIZE);
gb_string_free(s);
}
}
} else {
GB_ASSERT(t->kind == Type_Matrix);
operand->type = alloc_type_matrix(t->Matrix.elem, t->Matrix.column_count, t->Matrix.row_count);

File diff suppressed because it is too large Load Diff

View File

@@ -921,7 +921,7 @@ void check_switch_stmt(CheckerContext *ctx, Ast *node, u32 mod_flags) {
}
}
PtrMap<uintptr, TypeAndToken> seen = {}; // NOTE(bill): Multimap, Key: ExactValue
SeenMap seen = {}; // NOTE(bill): Multimap, Key: ExactValue
map_init(&seen, heap_allocator());
defer (map_destroy(&seen));
@@ -1065,7 +1065,7 @@ void check_switch_stmt(CheckerContext *ctx, Ast *node, u32 mod_flags) {
if (unhandled.count == 1) {
error_no_newline(node, "Unhandled switch case: %.*s", LIT(unhandled[0]->token.string));
} else {
error_no_newline(node, "Unhandled switch cases: ");
error(node, "Unhandled switch cases:");
for_array(i, unhandled) {
Entity *f = unhandled[i];
error_line("\t%.*s\n", LIT(f->token.string));

View File

@@ -504,6 +504,7 @@ enum VettedEntityKind {
VettedEntity_Unused,
VettedEntity_Shadowed,
VettedEntity_Shadowed_And_Unused,
};
struct VettedEntity {
VettedEntityKind kind;
@@ -625,12 +626,18 @@ void check_scope_usage(Checker *c, Scope *scope) {
MUTEX_GUARD_BLOCK(scope->mutex) for_array(i, scope->elements.entries) {
Entity *e = scope->elements.entries[i].value;
if (e == nullptr) continue;
VettedEntity ve = {};
if (vet_unused && check_vet_unused(c, e, &ve)) {
array_add(&vetted_entities, ve);
}
if (vet_shadowing && check_vet_shadowing(c, e, &ve)) {
array_add(&vetted_entities, ve);
VettedEntity ve_unused = {};
VettedEntity ve_shadowed = {};
bool is_unused = vet_unused && check_vet_unused(c, e, &ve_unused);
bool is_shadowed = vet_shadowing && check_vet_shadowing(c, e, &ve_shadowed);
if (is_unused && is_shadowed) {
VettedEntity ve_both = ve_shadowed;
ve_both.kind = VettedEntity_Shadowed_And_Unused;
array_add(&vetted_entities, ve_both);
} else if (is_unused) {
array_add(&vetted_entities, ve_unused);
} else if (is_shadowed) {
array_add(&vetted_entities, ve_shadowed);
}
}
@@ -642,16 +649,18 @@ void check_scope_usage(Checker *c, Scope *scope) {
Entity *other = ve.other;
String name = e->token.string;
if (build_context.vet) {
if (ve.kind == VettedEntity_Shadowed_And_Unused) {
error(e->token, "'%.*s' declared but not used, possibly shadows declaration at line %d", LIT(name), other->token.pos.line);
} else if (build_context.vet) {
switch (ve.kind) {
case VettedEntity_Unused:
error(e->token, "'%.*s' declared but not used", LIT(name));
break;
case VettedEntity_Shadowed:
if (e->flags&EntityFlag_Using) {
error(e->token, "Declaration of '%.*s' from 'using' shadows declaration at line %lld", LIT(name), cast(long long)other->token.pos.line);
error(e->token, "Declaration of '%.*s' from 'using' shadows declaration at line %d", LIT(name), other->token.pos.line);
} else {
error(e->token, "Declaration of '%.*s' shadows declaration at line %lld", LIT(name), cast(long long)other->token.pos.line);
error(e->token, "Declaration of '%.*s' shadows declaration at line %d", LIT(name), other->token.pos.line);
}
break;
default:

View File

@@ -115,8 +115,8 @@ LLVMValueRef llvm_const_cast(LLVMValueRef val, LLVMTypeRef dst) {
lbValue lb_const_ptr_cast(lbModule *m, lbValue value, Type *t) {
GB_ASSERT(is_type_pointer(value.type));
GB_ASSERT(is_type_pointer(t));
GB_ASSERT(is_type_internally_pointer_like(value.type));
GB_ASSERT(is_type_internally_pointer_like(t));
GB_ASSERT(lb_is_const(value));
lbValue res = {};
@@ -175,7 +175,7 @@ LLVMValueRef llvm_const_array(LLVMTypeRef elem_type, LLVMValueRef *values, isize
}
LLVMValueRef llvm_const_slice(lbModule *m, lbValue data, lbValue len) {
GB_ASSERT(is_type_pointer(data.type));
GB_ASSERT(is_type_pointer(data.type) || is_type_multi_pointer(data.type));
GB_ASSERT(are_types_identical(len.type, t_int));
LLVMValueRef vals[2] = {
data.value,
@@ -568,7 +568,7 @@ lbValue lb_const_value(lbModule *m, Type *type, ExactValue value, bool allow_loc
}
case ExactValue_Integer:
if (is_type_pointer(type)) {
if (is_type_pointer(type) || is_type_multi_pointer(type)) {
LLVMTypeRef t = lb_type(m, original_type);
LLVMValueRef i = lb_big_int_to_llvm(m, t_uintptr, &value.value_integer);
res.value = LLVMConstIntToPtr(i, t);

View File

@@ -580,6 +580,27 @@ LLVMValueRef lb_matrix_to_trimmed_vector(lbProcedure *p, lbValue m) {
lbValue lb_emit_matrix_tranpose(lbProcedure *p, lbValue m, Type *type) {
if (is_type_array(m.type)) {
i32 rank = type_math_rank(m.type);
if (rank == 2) {
lbAddr addr = lb_add_local_generated(p, type, false);
lbValue dst = addr.addr;
lbValue src = m;
i32 n = cast(i32)get_array_type_count(m.type);
i32 m = cast(i32)get_array_type_count(type);
// m.type == [n][m]T
// type == [m][n]T
for (i32 j = 0; j < m; j++) {
lbValue dst_col = lb_emit_struct_ep(p, dst, j);
for (i32 i = 0; i < n; i++) {
lbValue dst_row = lb_emit_struct_ep(p, dst_col, i);
lbValue src_col = lb_emit_struct_ev(p, src, i);
lbValue src_row = lb_emit_struct_ev(p, src_col, j);
lb_emit_store(p, dst_row, src_row);
}
}
return lb_addr_load(p, addr);
}
// no-op
m.type = type;
return m;
@@ -1834,6 +1855,15 @@ lbValue lb_emit_conv(lbProcedure *p, lbValue value, Type *t) {
return lb_addr_load(p, parent);
}
}
if (dst->Union.variants.count == 1) {
Type *vt = dst->Union.variants[0];
if (internal_check_is_assignable_to(src, vt)) {
value = lb_emit_conv(p, value, vt);
lbAddr parent = lb_add_local_generated(p, t, true);
lb_emit_store_union_variant(p, parent.addr, value, vt);
return lb_addr_load(p, parent);
}
}
}
// NOTE(bill): This has to be done before 'Pointer <-> Pointer' as it's

View File

@@ -363,6 +363,7 @@ enum TypeInfoFlag : u32 {
enum : int {
MATRIX_ELEMENT_COUNT_MIN = 1,
MATRIX_ELEMENT_COUNT_MAX = 16,
MATRIX_ELEMENT_MAX_SIZE = MATRIX_ELEMENT_COUNT_MAX * (2 * 8), // complex128
};
@@ -1583,6 +1584,24 @@ Type *core_array_type(Type *t) {
}
}
i32 type_math_rank(Type *t) {
i32 rank = 0;
for (;;) {
t = base_type(t);
switch (t->kind) {
case Type_Array:
rank += 1;
t = t->Array.elem;
break;
case Type_Matrix:
rank += 2;
t = t->Matrix.elem;
break;
default:
return rank;
}
}
}
Type *base_complex_elem_type(Type *t) {

View File

@@ -114,7 +114,6 @@ main :: proc() {
filepath.walk(path, walk_files);
for file in files {
fmt.println(file);
backup_path := strings.concatenate({file, "_bk"});
defer delete(backup_path);