mirror of
https://github.com/Ed94/Odin.git
synced 2026-08-02 20:58:15 +00:00
Change behaviour for zero-sized value types of array-related types; Fix make behaviour to always zero memory
This commit is contained in:
+6
-2
@@ -111,7 +111,10 @@ make_slice :: inline proc($T: typeid/[]$E, auto_cast len: int, allocator := cont
|
|||||||
make_aligned :: proc($T: typeid/[]$E, auto_cast len: int, alignment: int, allocator := context.allocator, loc := #caller_location) -> T {
|
make_aligned :: proc($T: typeid/[]$E, auto_cast len: int, alignment: int, allocator := context.allocator, loc := #caller_location) -> T {
|
||||||
runtime.make_slice_error_loc(loc, len);
|
runtime.make_slice_error_loc(loc, len);
|
||||||
data := alloc(size_of(E)*len, alignment, allocator, loc);
|
data := alloc(size_of(E)*len, alignment, allocator, loc);
|
||||||
if data == nil do return nil;
|
if data == nil && size_of(E) != 0 {
|
||||||
|
return nil;
|
||||||
|
}
|
||||||
|
zero(data, size_of(E)*len);
|
||||||
s := Raw_Slice{data, len};
|
s := Raw_Slice{data, len};
|
||||||
return transmute(T)s;
|
return transmute(T)s;
|
||||||
}
|
}
|
||||||
@@ -125,9 +128,10 @@ make_dynamic_array_len_cap :: proc($T: typeid/[dynamic]$E, auto_cast len: int, a
|
|||||||
runtime.make_dynamic_array_error_loc(loc, len, cap);
|
runtime.make_dynamic_array_error_loc(loc, len, cap);
|
||||||
data := alloc(size_of(E)*cap, align_of(E), allocator, loc);
|
data := alloc(size_of(E)*cap, align_of(E), allocator, loc);
|
||||||
s := Raw_Dynamic_Array{data, len, cap, allocator};
|
s := Raw_Dynamic_Array{data, len, cap, allocator};
|
||||||
if data == nil {
|
if data == nil && size_of(E) != 0 {
|
||||||
s.len, s.cap = 0, 0;
|
s.len, s.cap = 0, 0;
|
||||||
}
|
}
|
||||||
|
zero(data, size_of(E)*len);
|
||||||
return transmute(T)s;
|
return transmute(T)s;
|
||||||
}
|
}
|
||||||
make_map :: proc($T: typeid/map[$K]$E, auto_cast cap: int = 16, allocator := context.allocator, loc := #caller_location) -> T {
|
make_map :: proc($T: typeid/map[$K]$E, auto_cast cap: int = 16, allocator := context.allocator, loc := #caller_location) -> T {
|
||||||
|
|||||||
+17
-9
@@ -609,7 +609,10 @@ new_clone :: inline proc(data: $T, allocator := context.allocator, loc := #calle
|
|||||||
make_aligned :: proc($T: typeid/[]$E, auto_cast len: int, alignment: int, allocator := context.allocator, loc := #caller_location) -> T {
|
make_aligned :: proc($T: typeid/[]$E, auto_cast len: int, alignment: int, allocator := context.allocator, loc := #caller_location) -> T {
|
||||||
make_slice_error_loc(loc, len);
|
make_slice_error_loc(loc, len);
|
||||||
data := mem_alloc(size_of(E)*len, alignment, allocator, loc);
|
data := mem_alloc(size_of(E)*len, alignment, allocator, loc);
|
||||||
if data == nil do return nil;
|
if data == nil && size_of(E) != 0 {
|
||||||
|
return nil;
|
||||||
|
}
|
||||||
|
mem_zero(data, size_of(E)*len);
|
||||||
s := Raw_Slice{data, len};
|
s := Raw_Slice{data, len};
|
||||||
return transmute(T)s;
|
return transmute(T)s;
|
||||||
}
|
}
|
||||||
@@ -634,9 +637,10 @@ make_dynamic_array_len_cap :: proc($T: typeid/[dynamic]$E, auto_cast len: int, a
|
|||||||
make_dynamic_array_error_loc(loc, len, cap);
|
make_dynamic_array_error_loc(loc, len, cap);
|
||||||
data := mem_alloc(size_of(E)*cap, align_of(E), allocator, loc);
|
data := mem_alloc(size_of(E)*cap, align_of(E), allocator, loc);
|
||||||
s := Raw_Dynamic_Array{data, len, cap, allocator};
|
s := Raw_Dynamic_Array{data, len, cap, allocator};
|
||||||
if data == nil {
|
if data == nil && size_of(E) != 0 {
|
||||||
s.len, s.cap = 0, 0;
|
s.len, s.cap = 0, 0;
|
||||||
}
|
}
|
||||||
|
mem_zero(data, size_of(E)*cap);
|
||||||
return transmute(T)s;
|
return transmute(T)s;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -697,10 +701,12 @@ append_elem :: proc(array: ^$T/[dynamic]$E, arg: E, loc := #caller_location) {
|
|||||||
arg_len = min(cap(array)-len(array), arg_len);
|
arg_len = min(cap(array)-len(array), arg_len);
|
||||||
if arg_len > 0 {
|
if arg_len > 0 {
|
||||||
a := (^Raw_Dynamic_Array)(array);
|
a := (^Raw_Dynamic_Array)(array);
|
||||||
data := (^E)(a.data);
|
if size_of(E) != 0 {
|
||||||
assert(data != nil);
|
data := (^E)(a.data);
|
||||||
val := arg;
|
assert(data != nil);
|
||||||
mem_copy(ptr_offset(data, a.len), &val, size_of(E));
|
val := arg;
|
||||||
|
mem_copy(ptr_offset(data, a.len), &val, size_of(E));
|
||||||
|
}
|
||||||
a.len += arg_len;
|
a.len += arg_len;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -719,9 +725,11 @@ append_elems :: proc(array: ^$T/[dynamic]$E, args: ..E, loc := #caller_location)
|
|||||||
arg_len = min(cap(array)-len(array), arg_len);
|
arg_len = min(cap(array)-len(array), arg_len);
|
||||||
if arg_len > 0 {
|
if arg_len > 0 {
|
||||||
a := (^Raw_Dynamic_Array)(array);
|
a := (^Raw_Dynamic_Array)(array);
|
||||||
data := (^E)(a.data);
|
if size_of(E) != 0 {
|
||||||
assert(data != nil);
|
data := (^E)(a.data);
|
||||||
mem_copy(ptr_offset(data, a.len), &args[0], size_of(E) * arg_len);
|
assert(data != nil);
|
||||||
|
mem_copy(ptr_offset(data, a.len), &args[0], size_of(E) * arg_len);
|
||||||
|
}
|
||||||
a.len += arg_len;
|
a.len += arg_len;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+11
-54
@@ -4414,6 +4414,7 @@ irValue *ir_emit_comp_against_nil(irProcedure *proc, TokenKind op_kind, irValue
|
|||||||
irValue *ptr = ir_emit_conv(proc, x, t_u8_ptr);
|
irValue *ptr = ir_emit_conv(proc, x, t_u8_ptr);
|
||||||
return ir_emit_comp(proc, op_kind, ptr, v_raw_nil);
|
return ir_emit_comp(proc, op_kind, ptr, v_raw_nil);
|
||||||
} else if (is_type_any(t)) {
|
} else if (is_type_any(t)) {
|
||||||
|
// TODO(bill): is this correct behaviour for nil comparison for any?
|
||||||
irValue *data = ir_emit_struct_ev(proc, x, 0);
|
irValue *data = ir_emit_struct_ev(proc, x, 0);
|
||||||
irValue *ti = ir_emit_struct_ev(proc, x, 1);
|
irValue *ti = ir_emit_struct_ev(proc, x, 1);
|
||||||
if (op_kind == Token_CmpEq) {
|
if (op_kind == Token_CmpEq) {
|
||||||
@@ -4426,32 +4427,14 @@ irValue *ir_emit_comp_against_nil(irProcedure *proc, TokenKind op_kind, irValue
|
|||||||
return ir_emit_arith(proc, Token_And, a, b, t_bool);
|
return ir_emit_arith(proc, Token_And, a, b, t_bool);
|
||||||
}
|
}
|
||||||
} else if (is_type_slice(t)) {
|
} else if (is_type_slice(t)) {
|
||||||
irValue *data = ir_emit_struct_ev(proc, x, 0);
|
irValue *len = ir_emit_struct_ev(proc, x, 1);
|
||||||
irValue *cap = ir_emit_struct_ev(proc, x, 1);
|
|
||||||
if (op_kind == Token_CmpEq) {
|
|
||||||
irValue *a = ir_emit_comp(proc, Token_CmpEq, data, v_raw_nil);
|
|
||||||
irValue *b = ir_emit_comp(proc, Token_CmpEq, cap, v_zero);
|
|
||||||
return ir_emit_arith(proc, Token_Or, a, b, t_bool);
|
|
||||||
} else if (op_kind == Token_NotEq) {
|
|
||||||
irValue *a = ir_emit_comp(proc, Token_NotEq, data, v_raw_nil);
|
|
||||||
irValue *b = ir_emit_comp(proc, Token_NotEq, cap, v_zero);
|
|
||||||
return ir_emit_arith(proc, Token_And, a, b, t_bool);
|
|
||||||
}
|
|
||||||
} else if (is_type_dynamic_array(t)) {
|
|
||||||
irValue *data = ir_emit_struct_ev(proc, x, 0);
|
|
||||||
irValue *cap = ir_emit_struct_ev(proc, x, 2);
|
|
||||||
if (op_kind == Token_CmpEq) {
|
|
||||||
irValue *a = ir_emit_comp(proc, Token_CmpEq, data, v_raw_nil);
|
|
||||||
irValue *b = ir_emit_comp(proc, Token_CmpEq, cap, v_zero);
|
|
||||||
return ir_emit_arith(proc, Token_Or, a, b, t_bool);
|
|
||||||
} else if (op_kind == Token_NotEq) {
|
|
||||||
irValue *a = ir_emit_comp(proc, Token_NotEq, data, v_raw_nil);
|
|
||||||
irValue *b = ir_emit_comp(proc, Token_NotEq, cap, v_zero);
|
|
||||||
return ir_emit_arith(proc, Token_And, a, b, t_bool);
|
|
||||||
}
|
|
||||||
} else if (is_type_map(t)) {
|
|
||||||
irValue *len = ir_map_len(proc, x);
|
|
||||||
return ir_emit_comp(proc, op_kind, len, v_zero);
|
return ir_emit_comp(proc, op_kind, len, v_zero);
|
||||||
|
} else if (is_type_dynamic_array(t)) {
|
||||||
|
irValue *cap = ir_emit_struct_ev(proc, x, 2);
|
||||||
|
return ir_emit_comp(proc, op_kind, cap, v_zero);
|
||||||
|
} else if (is_type_map(t)) {
|
||||||
|
irValue *cap = ir_map_cap(proc, x);
|
||||||
|
return ir_emit_comp(proc, op_kind, cap, v_zero);
|
||||||
} else if (is_type_union(t)) {
|
} else if (is_type_union(t)) {
|
||||||
if (type_size_of(t) == 0) {
|
if (type_size_of(t) == 0) {
|
||||||
return ir_emit_comp(proc, op_kind, v_zero, v_zero);
|
return ir_emit_comp(proc, op_kind, v_zero, v_zero);
|
||||||
@@ -4481,38 +4464,12 @@ irValue *ir_emit_comp_against_nil(irProcedure *proc, TokenKind op_kind, irValue
|
|||||||
if (bt->Struct.soa_kind == StructSoa_Slice) {
|
if (bt->Struct.soa_kind == StructSoa_Slice) {
|
||||||
ir_emit_comment(proc, str_lit("soa-slice-nil-comp"));
|
ir_emit_comment(proc, str_lit("soa-slice-nil-comp"));
|
||||||
irValue *len = ir_soa_struct_len(proc, x);
|
irValue *len = ir_soa_struct_len(proc, x);
|
||||||
if (bt->Struct.fields.count > 1) {
|
return ir_emit_comp(proc, op_kind, len, v_zero);
|
||||||
irValue *data = ir_emit_struct_ev(proc, x, 0);
|
|
||||||
if (op_kind == Token_CmpEq) {
|
|
||||||
irValue *a = ir_emit_comp(proc, Token_CmpEq, data, v_raw_nil);
|
|
||||||
irValue *b = ir_emit_comp(proc, Token_CmpEq, len, v_zero);
|
|
||||||
return ir_emit_arith(proc, Token_Or, a, b, t_bool);
|
|
||||||
} else if (op_kind == Token_NotEq) {
|
|
||||||
irValue *a = ir_emit_comp(proc, Token_NotEq, data, v_raw_nil);
|
|
||||||
irValue *b = ir_emit_comp(proc, Token_NotEq, len, v_zero);
|
|
||||||
return ir_emit_arith(proc, Token_And, a, b, t_bool);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
return ir_emit_comp(proc, op_kind, len, v_zero);
|
|
||||||
}
|
|
||||||
} else if (bt->Struct.soa_kind == StructSoa_Dynamic) {
|
} else if (bt->Struct.soa_kind == StructSoa_Dynamic) {
|
||||||
ir_emit_comment(proc, str_lit("soa-dynamic-array-nil-comp"));
|
ir_emit_comment(proc, str_lit("soa-dynamic-array-nil-comp"));
|
||||||
|
|
||||||
irValue *cap = ir_soa_struct_len(proc, x);
|
irValue *cap = ir_soa_struct_cap(proc, x);
|
||||||
if (bt->Struct.fields.count > 1) {
|
return ir_emit_comp(proc, op_kind, cap, v_zero);
|
||||||
irValue *data = ir_emit_struct_ev(proc, x, 0);
|
|
||||||
if (op_kind == Token_CmpEq) {
|
|
||||||
irValue *a = ir_emit_comp(proc, Token_CmpEq, data, v_raw_nil);
|
|
||||||
irValue *b = ir_emit_comp(proc, Token_CmpEq, cap, v_zero);
|
|
||||||
return ir_emit_arith(proc, Token_Or, a, b, t_bool);
|
|
||||||
} else if (op_kind == Token_NotEq) {
|
|
||||||
irValue *a = ir_emit_comp(proc, Token_NotEq, data, v_raw_nil);
|
|
||||||
irValue *b = ir_emit_comp(proc, Token_NotEq, cap, v_zero);
|
|
||||||
return ir_emit_arith(proc, Token_And, a, b, t_bool);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
return ir_emit_comp(proc, op_kind, cap, v_zero);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nullptr;
|
return nullptr;
|
||||||
|
|||||||
+26
-50
@@ -7952,6 +7952,7 @@ lbValue lb_emit_comp_against_nil(lbProcedure *p, TokenKind op_kind, lbValue x) {
|
|||||||
}
|
}
|
||||||
return res;
|
return res;
|
||||||
} else if (is_type_any(t)) {
|
} else if (is_type_any(t)) {
|
||||||
|
// TODO(bill): is this correct behaviour for nil comparison for any?
|
||||||
lbValue data = lb_emit_struct_ev(p, x, 0);
|
lbValue data = lb_emit_struct_ev(p, x, 0);
|
||||||
lbValue ti = lb_emit_struct_ev(p, x, 1);
|
lbValue ti = lb_emit_struct_ev(p, x, 1);
|
||||||
if (op_kind == Token_CmpEq) {
|
if (op_kind == Token_CmpEq) {
|
||||||
@@ -7966,31 +7967,21 @@ lbValue lb_emit_comp_against_nil(lbProcedure *p, TokenKind op_kind, lbValue x) {
|
|||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
} else if (is_type_slice(t)) {
|
} else if (is_type_slice(t)) {
|
||||||
lbValue data = lb_emit_struct_ev(p, x, 0);
|
lbValue len = lb_emit_struct_ev(p, x, 1);
|
||||||
lbValue cap = lb_emit_struct_ev(p, x, 1);
|
|
||||||
if (op_kind == Token_CmpEq) {
|
if (op_kind == Token_CmpEq) {
|
||||||
LLVMValueRef a = LLVMBuildIsNull(p->builder, data.value, "");
|
res.value = LLVMBuildIsNull(p->builder, len.value, "");
|
||||||
LLVMValueRef b = LLVMBuildIsNull(p->builder, cap.value, "");
|
|
||||||
res.value = LLVMBuildOr(p->builder, a, b, "");
|
|
||||||
return res;
|
return res;
|
||||||
} else if (op_kind == Token_NotEq) {
|
} else if (op_kind == Token_NotEq) {
|
||||||
LLVMValueRef a = LLVMBuildIsNotNull(p->builder, data.value, "");
|
res.value = LLVMBuildIsNotNull(p->builder, len.value, "");
|
||||||
LLVMValueRef b = LLVMBuildIsNotNull(p->builder, cap.value, "");
|
|
||||||
res.value = LLVMBuildAnd(p->builder, a, b, "");
|
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
} else if (is_type_dynamic_array(t)) {
|
} else if (is_type_dynamic_array(t)) {
|
||||||
lbValue data = lb_emit_struct_ev(p, x, 0);
|
|
||||||
lbValue cap = lb_emit_struct_ev(p, x, 2);
|
lbValue cap = lb_emit_struct_ev(p, x, 2);
|
||||||
if (op_kind == Token_CmpEq) {
|
if (op_kind == Token_CmpEq) {
|
||||||
LLVMValueRef a = LLVMBuildIsNull(p->builder, data.value, "");
|
res.value = LLVMBuildIsNull(p->builder, cap.value, "");
|
||||||
LLVMValueRef b = LLVMBuildIsNull(p->builder, cap.value, "");
|
|
||||||
res.value = LLVMBuildOr(p->builder, a, b, "");
|
|
||||||
return res;
|
return res;
|
||||||
} else if (op_kind == Token_NotEq) {
|
} else if (op_kind == Token_NotEq) {
|
||||||
LLVMValueRef a = LLVMBuildIsNotNull(p->builder, data.value, "");
|
res.value = LLVMBuildIsNotNull(p->builder, cap.value, "");
|
||||||
LLVMValueRef b = LLVMBuildIsNotNull(p->builder, cap.value, "");
|
|
||||||
res.value = LLVMBuildAnd(p->builder, a, b, "");
|
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
} else if (is_type_map(t)) {
|
} else if (is_type_map(t)) {
|
||||||
@@ -8019,41 +8010,26 @@ lbValue lb_emit_comp_against_nil(lbProcedure *p, TokenKind op_kind, lbValue x) {
|
|||||||
lbValue res = lb_emit_comp(p, op_kind, val, lb_const_int(p->module, t_int, 0));
|
lbValue res = lb_emit_comp(p, op_kind, val, lb_const_int(p->module, t_int, 0));
|
||||||
return res;
|
return res;
|
||||||
} else if (is_type_soa_struct(t)) {
|
} else if (is_type_soa_struct(t)) {
|
||||||
GB_PANIC("#soa struct nil comparison");
|
Type *bt = base_type(t);
|
||||||
// Type *bt = base_type(t);
|
if (bt->Struct.soa_kind == StructSoa_Slice) {
|
||||||
// if (bt->Struct.soa_kind == StructSoa_Slice) {
|
lbValue len = lb_soa_struct_len(p, x);
|
||||||
// lbValue len = lb_soa_struct_len(p, x);
|
if (op_kind == Token_CmpEq) {
|
||||||
// if (bt->Struct.fields.count > 1) {
|
res.value = LLVMBuildIsNull(p->builder, len.value, "");
|
||||||
// lbValue data = lb_emit_struct_ev(p, x, 0);
|
return res;
|
||||||
// if (op_kind == Token_CmpEq) {
|
} else if (op_kind == Token_NotEq) {
|
||||||
// lbValue a = lb_emit_comp(p, Token_CmpEq, data, v_raw_nil);
|
res.value = LLVMBuildIsNotNull(p->builder, len.value, "");
|
||||||
// lbValue b = lb_emit_comp(p, Token_CmpEq, len, v_zero);
|
return res;
|
||||||
// return lb_emit_arith(p, Token_Or, a, b, t_bool);
|
}
|
||||||
// } else if (op_kind == Token_NotEq) {
|
} else if (bt->Struct.soa_kind == StructSoa_Dynamic) {
|
||||||
// lbValue a = lb_emit_comp(p, Token_NotEq, data, v_raw_nil);
|
lbValue cap = lb_soa_struct_cap(p, x);
|
||||||
// lbValue b = lb_emit_comp(p, Token_NotEq, len, v_zero);
|
if (op_kind == Token_CmpEq) {
|
||||||
// return lb_emit_arith(p, Token_And, a, b, t_bool);
|
res.value = LLVMBuildIsNull(p->builder, cap.value, "");
|
||||||
// }
|
return res;
|
||||||
// } else {
|
} else if (op_kind == Token_NotEq) {
|
||||||
// return lb_emit_comp(p, op_kind, len, v_zero);
|
res.value = LLVMBuildIsNotNull(p->builder, cap.value, "");
|
||||||
// }
|
return res;
|
||||||
// } else if (bt->Struct.soa_kind == StructSoa_Dynamic) {
|
}
|
||||||
// lbValue cap = lb_soa_struct_len(p, x);
|
}
|
||||||
// if (bt->Struct.fields.count > 1) {
|
|
||||||
// lbValue data = lb_emit_struct_ev(p, x, 0);
|
|
||||||
// if (op_kind == Token_CmpEq) {
|
|
||||||
// lbValue a = lb_emit_comp(p, Token_CmpEq, data, v_raw_nil);
|
|
||||||
// lbValue b = lb_emit_comp(p, Token_CmpEq, cap, v_zero);
|
|
||||||
// return lb_emit_arith(p, Token_Or, a, b, t_bool);
|
|
||||||
// } else if (op_kind == Token_NotEq) {
|
|
||||||
// lbValue a = lb_emit_comp(p, Token_NotEq, data, v_raw_nil);
|
|
||||||
// lbValue b = lb_emit_comp(p, Token_NotEq, cap, v_zero);
|
|
||||||
// return lb_emit_arith(p, Token_And, a, b, t_bool);
|
|
||||||
// }
|
|
||||||
// } else {
|
|
||||||
// return lb_emit_comp(p, op_kind, cap, v_zero);
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
}
|
}
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user