Add sanity casts for 32/64 bit correctness

This commit is contained in:
gingerBill
2021-07-12 11:03:12 +01:00
parent ff2e5c3efe
commit 76707e1d2f
10 changed files with 66 additions and 48 deletions

View File

@@ -281,7 +281,25 @@ void big_int_mul(BigInt *dst, BigInt const *x, BigInt const *y) {
u64 leading_zeros_u64(u64 x) {
#if defined(GB_COMPILER_MSVC)
return __lzcnt64(x);
#if defined(GB_ARCH_64_BIT)
return __lzcnt64(x);
#else
u64 y, n;
n = 0;
y = x;
L:
if (x < 0) {
return n;
}
if (y == 0) {
return 64-n;
}
n++;
x <<= 1;
y >>= 1;
goto L;
#endif
#else
return cast(u64)__builtin_clzll(cast(unsigned long long)x);
#endif

View File

@@ -1120,7 +1120,7 @@ bool check_builtin_procedure(CheckerContext *c, Operand *operand, Ast *call, i32
// TODO(bill): Should I copy each of the entities or is this good enough?
gb_memmove_array(tuple->Tuple.variables.data, type->Struct.fields.data, variable_count);
} else if (is_type_array(type)) {
isize variable_count = type->Array.count;
isize variable_count = cast(isize)type->Array.count;
array_init(&tuple->Tuple.variables, a, variable_count);
for (isize i = 0; i < variable_count; i++) {
tuple->Tuple.variables[i] = alloc_entity_array_elem(nullptr, blank_token, type->Array.elem, cast(i32)i);
@@ -1916,8 +1916,8 @@ bool check_builtin_procedure(CheckerContext *c, Operand *operand, Ast *call, i32
if (is_type_array(elem)) {
Type *old_array = base_type(elem);
soa_struct = alloc_type_struct();
soa_struct->Struct.fields = array_make<Entity *>(heap_allocator(), old_array->Array.count);
soa_struct->Struct.tags = array_make<String>(heap_allocator(), old_array->Array.count);
soa_struct->Struct.fields = array_make<Entity *>(heap_allocator(), cast(isize)old_array->Array.count);
soa_struct->Struct.tags = array_make<String>(heap_allocator(), cast(isize)old_array->Array.count);
soa_struct->Struct.node = operand->expr;
soa_struct->Struct.soa_kind = StructSoa_Fixed;
soa_struct->Struct.soa_elem = elem;
@@ -1933,7 +1933,7 @@ bool check_builtin_procedure(CheckerContext *c, Operand *operand, Ast *call, i32
str_lit("w")
};
for (i64 i = 0; i < old_array->Array.count; i++) {
for (isize i = 0; i < cast(isize)old_array->Array.count; i++) {
Type *array_type = alloc_type_array(old_array->Array.elem, count);
Token token = {};
token.string = params_xyzw[i];
@@ -2891,7 +2891,7 @@ bool check_builtin_procedure(CheckerContext *c, Operand *operand, Ast *call, i32
if (bt->kind == Type_Proc) {
count = bt->Proc.param_count;
if (index < count) {
param = bt->Proc.params->Tuple.variables[index];
param = bt->Proc.params->Tuple.variables[cast(isize)index];
}
}
@@ -2950,7 +2950,7 @@ bool check_builtin_procedure(CheckerContext *c, Operand *operand, Ast *call, i32
if (bt->kind == Type_Proc) {
count = bt->Proc.result_count;
if (index < count) {
param = bt->Proc.results->Tuple.variables[index];
param = bt->Proc.results->Tuple.variables[cast(isize)index];
}
}

View File

@@ -8118,7 +8118,7 @@ ExprKind check_expr_base_internal(CheckerContext *c, Operand *o, Ast *node, Type
o->mode = Addressing_Constant;
o->type = t;
o->value = exact_value_string(substring(s, indices[0], indices[1]));
o->value = exact_value_string(substring(s, cast(isize)indices[0], cast(isize)indices[1]));
}
case_end;

View File

@@ -2218,7 +2218,7 @@ Type *make_soa_struct_internal(CheckerContext *ctx, Ast *array_typ_expr, Ast *el
soa_struct->Struct.scope = scope;
} else if (is_type_array(elem)) {
Type *old_array = base_type(elem);
field_count = old_array->Array.count;
field_count = cast(isize)old_array->Array.count;
soa_struct = alloc_type_struct();
soa_struct->Struct.fields = array_make<Entity *>(heap_allocator(), field_count+extra_field_count);
@@ -2238,7 +2238,7 @@ Type *make_soa_struct_internal(CheckerContext *ctx, Ast *array_typ_expr, Ast *el
str_lit("w")
};
for (i64 i = 0; i < old_array->Array.count; i++) {
for (isize i = 0; i < cast(isize)old_array->Array.count; i++) {
Type *field_type = nullptr;
if (soa_kind == StructSoa_Fixed) {
GB_ASSERT(count >= 0);

View File

@@ -341,7 +341,7 @@ bool sub_overflow_u64(u64 x, u64 y, u64 *result) {
}
void mul_overflow_u64(u64 x, u64 y, u64 *lo, u64 *hi) {
#if defined(GB_COMPILER_MSVC)
#if defined(GB_COMPILER_MSVC) && defined(GB_ARCH_64_BIT)
*lo = _umul128(x, y, hi);
#else
// URL(bill): https://stackoverflow.com/questions/25095741/how-can-i-multiply-64-bit-operands-and-get-128-bit-result-portably#25096197
@@ -697,9 +697,9 @@ isize next_pow2_isize(isize n) {
n |= n >> 4;
n |= n >> 8;
n |= n >> 16;
if (gb_size_of(isize) == 8) {
#if defined(GB_ARCH_64_BIT)
n |= n >> 32;
}
#endif
n++;
return n;
}

View File

@@ -187,7 +187,7 @@ T *odin_doc_get_item(OdinDocWriter *w, OdinDocWriterItemTracker<T> *t, u32 index
if (w->state != OdinDocWriterState_Writing) {
return nullptr;
}
GB_ASSERT(index < t->len);
GB_ASSERT(index < cast(u32)t->len);
uintptr data = cast(uintptr)w->data + cast(uintptr)(t->offset + gb_size_of(T)*index);
return cast(T *)data;
}

View File

@@ -565,7 +565,7 @@ namespace lbAbiAmd64SysV {
}
void unify(Array<RegClass> *cls, i64 i, RegClass const newv) {
RegClass const oldv = (*cls)[i];
RegClass const oldv = (*cls)[cast(isize)i];
if (oldv == newv) {
return;
}
@@ -597,7 +597,7 @@ namespace lbAbiAmd64SysV {
}
}
(*cls)[i] = to_write;
(*cls)[cast(isize)i] = to_write;
}
void fixup(LLVMTypeRef t, Array<RegClass> *cls) {
@@ -606,7 +606,7 @@ namespace lbAbiAmd64SysV {
if (e > 2 && (lb_is_type_kind(t, LLVMStructTypeKind) ||
lb_is_type_kind(t, LLVMArrayTypeKind) ||
lb_is_type_kind(t, LLVMVectorTypeKind))) {
RegClass &oldv = (*cls)[i];
RegClass &oldv = (*cls)[cast(isize)i];
if (is_sse(oldv)) {
for (i++; i < e; i++) {
if (oldv != RegClass_SSEUp) {
@@ -620,7 +620,7 @@ namespace lbAbiAmd64SysV {
}
} else {
while (i < e) {
RegClass &oldv = (*cls)[i];
RegClass &oldv = (*cls)[cast(isize)i];
if (oldv == RegClass_Memory) {
all_mem(cls);
return;

View File

@@ -491,7 +491,7 @@ void lb_addr_store(lbProcedure *p, lbAddr addr, lbValue value) {
field_count = elem_type->Struct.fields.count;
break;
case Type_Array:
field_count = elem_type->Array.count;
field_count = cast(isize)elem_type->Array.count;
break;
}
for (isize i = 0; i < field_count; i++) {
@@ -1559,7 +1559,7 @@ LLVMTypeRef lb_type_internal(lbModule *m, Type *type) {
}
}
isize param_index = 0;
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) {
@@ -5714,7 +5714,7 @@ void lb_build_assign_stmt_array(lbProcedure *p, TokenKind op, lbAddr const &lhs,
} else {
lbValue y = lb_address_from_load_or_generate_local(p, rhs);
auto loop_data = lb_loop_start(p, count, t_i32);
auto loop_data = lb_loop_start(p, cast(isize)count, t_i32);
lbValue a_ptr = lb_emit_array_ep(p, x, loop_data.idx);
lbValue b_ptr = lb_emit_array_ep(p, y, loop_data.idx);
@@ -6440,7 +6440,7 @@ lbValue lb_const_value(lbModule *m, Type *type, ExactValue value, bool allow_loc
if (count == 0) {
return lb_const_nil(m, type);
}
count = gb_max(cl->max_count, count);
count = gb_max(cast(isize)cl->max_count, count);
Type *elem = base_type(type)->Slice.elem;
Type *t = alloc_type_array(elem, count);
lbValue backing_array = lb_const_value(m, t, value, allow_local);
@@ -6510,7 +6510,7 @@ lbValue lb_const_value(lbModule *m, Type *type, ExactValue value, bool allow_loc
isize width = 1;
String s = value.value_string;
LLVMValueRef *elems = gb_alloc_array(permanent_allocator(), LLVMValueRef, count);
LLVMValueRef *elems = gb_alloc_array(permanent_allocator(), LLVMValueRef, cast(isize)count);
for (i64 i = 0; i < count && offset < s.len; i++) {
width = gb_utf8_decode(s.text+offset, s.len-offset, &rune);
@@ -6551,7 +6551,7 @@ lbValue lb_const_value(lbModule *m, Type *type, ExactValue value, bool allow_loc
lbValue single_elem = lb_const_value(m, elem, value, allow_local);
LLVMValueRef *elems = gb_alloc_array(permanent_allocator(), LLVMValueRef, count);
LLVMValueRef *elems = gb_alloc_array(permanent_allocator(), LLVMValueRef, cast(isize)count);
for (i64 i = 0; i < count; i++) {
elems[i] = single_elem.value;
}
@@ -6675,7 +6675,7 @@ lbValue lb_const_value(lbModule *m, Type *type, ExactValue value, bool allow_loc
}
if (cl->elems[0]->kind == Ast_FieldValue) {
// TODO(bill): This is O(N*M) and will be quite slow; it should probably be sorted before hand
LLVMValueRef *values = gb_alloc_array(temporary_allocator(), LLVMValueRef, type->Array.count);
LLVMValueRef *values = gb_alloc_array(temporary_allocator(), LLVMValueRef, cast(isize)type->Array.count);
isize value_index = 0;
for (i64 i = 0; i < type->Array.count; i++) {
@@ -6727,12 +6727,12 @@ lbValue lb_const_value(lbModule *m, Type *type, ExactValue value, bool allow_loc
}
}
res.value = lb_build_constant_array_values(m, type, elem_type, type->Array.count, values, allow_local);
res.value = lb_build_constant_array_values(m, type, elem_type, cast(isize)type->Array.count, values, allow_local);
return res;
} else {
GB_ASSERT_MSG(elem_count == type->Array.count, "%td != %td", elem_count, type->Array.count);
LLVMValueRef *values = gb_alloc_array(temporary_allocator(), LLVMValueRef, type->Array.count);
LLVMValueRef *values = gb_alloc_array(temporary_allocator(), LLVMValueRef, cast(isize)type->Array.count);
for (isize i = 0; i < elem_count; i++) {
TypeAndValue tav = cl->elems[i]->tav;
@@ -6743,7 +6743,7 @@ lbValue lb_const_value(lbModule *m, Type *type, ExactValue value, bool allow_loc
values[i] = LLVMConstNull(lb_type(m, elem_type));
}
res.value = lb_build_constant_array_values(m, type, elem_type, type->Array.count, values, allow_local);
res.value = lb_build_constant_array_values(m, type, elem_type, cast(isize)type->Array.count, values, allow_local);
return res;
}
} else if (is_type_enumerated_array(type)) {
@@ -6755,7 +6755,7 @@ lbValue lb_const_value(lbModule *m, Type *type, ExactValue value, bool allow_loc
}
if (cl->elems[0]->kind == Ast_FieldValue) {
// TODO(bill): This is O(N*M) and will be quite slow; it should probably be sorted before hand
LLVMValueRef *values = gb_alloc_array(temporary_allocator(), LLVMValueRef, type->EnumeratedArray.count);
LLVMValueRef *values = gb_alloc_array(temporary_allocator(), LLVMValueRef, cast(isize)type->EnumeratedArray.count);
isize value_index = 0;
@@ -6811,12 +6811,12 @@ lbValue lb_const_value(lbModule *m, Type *type, ExactValue value, bool allow_loc
}
}
res.value = lb_build_constant_array_values(m, type, elem_type, type->EnumeratedArray.count, values, allow_local);
res.value = lb_build_constant_array_values(m, type, elem_type, cast(isize)type->EnumeratedArray.count, values, allow_local);
return res;
} else {
GB_ASSERT_MSG(elem_count == type->EnumeratedArray.count, "%td != %td", elem_count, type->EnumeratedArray.count);
LLVMValueRef *values = gb_alloc_array(temporary_allocator(), LLVMValueRef, type->EnumeratedArray.count);
LLVMValueRef *values = gb_alloc_array(temporary_allocator(), LLVMValueRef, cast(isize)type->EnumeratedArray.count);
for (isize i = 0; i < elem_count; i++) {
TypeAndValue tav = cl->elems[i]->tav;
@@ -6827,7 +6827,7 @@ lbValue lb_const_value(lbModule *m, Type *type, ExactValue value, bool allow_loc
values[i] = LLVMConstNull(lb_type(m, elem_type));
}
res.value = lb_build_constant_array_values(m, type, elem_type, type->EnumeratedArray.count, values, allow_local);
res.value = lb_build_constant_array_values(m, type, elem_type, cast(isize)type->EnumeratedArray.count, values, allow_local);
return res;
}
} else if (is_type_simd_vector(type)) {
@@ -6840,7 +6840,7 @@ lbValue lb_const_value(lbModule *m, Type *type, ExactValue value, bool allow_loc
}
GB_ASSERT(elem_type_can_be_constant(elem_type));
isize total_elem_count = type->SimdVector.count;
isize total_elem_count = cast(isize)type->SimdVector.count;
LLVMValueRef *values = gb_alloc_array(temporary_allocator(), LLVMValueRef, total_elem_count);
for (isize i = 0; i < elem_count; i++) {
@@ -6853,7 +6853,7 @@ lbValue lb_const_value(lbModule *m, Type *type, ExactValue value, bool allow_loc
for (isize i = elem_count; i < type->SimdVector.count; i++) {
values[i] = LLVMConstNull(et);
}
for (isize i = 0; i< total_elem_count; i++) {
for (isize i = 0; i < total_elem_count; i++) {
values[i] = llvm_const_cast(values[i], et);
}
@@ -7397,7 +7397,7 @@ lbValue lb_emit_arith_array(lbProcedure *p, TokenKind op, lbValue lhs, lbValue r
lbAddr res = lb_add_local_generated(p, type, false);
auto loop_data = lb_loop_start(p, count, t_i32);
auto loop_data = lb_loop_start(p, cast(isize)count, t_i32);
lbValue a_ptr = lb_emit_array_ep(p, x, loop_data.idx);
lbValue b_ptr = lb_emit_array_ep(p, y, loop_data.idx);
@@ -9477,9 +9477,9 @@ lbValue lb_soa_struct_len(lbProcedure *p, lbValue value) {
isize n = 0;
Type *elem = base_type(t->Struct.soa_elem);
if (elem->kind == Type_Struct) {
n = elem->Struct.fields.count;
n = cast(isize)elem->Struct.fields.count;
} else if (elem->kind == Type_Array) {
n = elem->Array.count;
n = cast(isize)elem->Array.count;
} else {
GB_PANIC("Unreachable");
}
@@ -9509,9 +9509,9 @@ lbValue lb_soa_struct_cap(lbProcedure *p, lbValue value) {
isize n = 0;
Type *elem = base_type(t->Struct.soa_elem);
if (elem->kind == Type_Struct) {
n = elem->Struct.fields.count+1;
n = cast(isize)elem->Struct.fields.count+1;
} else if (elem->kind == Type_Array) {
n = elem->Array.count+1;
n = cast(isize)elem->Array.count+1;
} else {
GB_PANIC("Unreachable");
}
@@ -11680,7 +11680,7 @@ lbValue lb_get_hasher_proc_for_type(lbModule *m, Type *type) {
auto args = array_make<lbValue>(permanent_allocator(), 2);
lbValue elem_hasher = lb_get_hasher_proc_for_type(m, type->Array.elem);
auto loop_data = lb_loop_start(p, type->Array.count, t_i32);
auto loop_data = lb_loop_start(p, cast(isize)type->Array.count, t_i32);
data = lb_emit_conv(p, data, pt);
@@ -11701,7 +11701,7 @@ lbValue lb_get_hasher_proc_for_type(lbModule *m, Type *type) {
auto args = array_make<lbValue>(permanent_allocator(), 2);
lbValue elem_hasher = lb_get_hasher_proc_for_type(m, type->EnumeratedArray.elem);
auto loop_data = lb_loop_start(p, type->EnumeratedArray.count, t_i32);
auto loop_data = lb_loop_start(p, cast(isize)type->EnumeratedArray.count, t_i32);
data = lb_emit_conv(p, data, pt);
@@ -13010,7 +13010,7 @@ lbValue lb_const_hash(lbModule *m, lbValue key, Type *key_type) {
unsigned len_indices[] = {1};
LLVMValueRef data = LLVMConstExtractValue(key.value, data_indices, gb_count_of(data_indices));
LLVMValueRef len = LLVMConstExtractValue(key.value, len_indices, gb_count_of(len_indices));
isize length = LLVMConstIntGetSExtValue(len);
i64 length = LLVMConstIntGetSExtValue(len);
char const *text = nullptr;
if (false && length != 0) {
if (LLVMGetConstOpcode(data) != LLVMGetElementPtr) {
@@ -13021,7 +13021,7 @@ lbValue lb_const_hash(lbModule *m, lbValue key, Type *key_type) {
size_t ulength = 0;
text = LLVMGetAsString(data, &ulength);
gb_printf_err("%td %td %s\n", length, ulength, text);
length = gb_min(length, cast(isize)ulength);
length = gb_min(length, cast(i64)ulength);
}
hash = fnv64a(text, cast(isize)length);
} else {

View File

@@ -448,7 +448,7 @@ bool find_visual_studio_by_fighting_through_microsoft_craziness(Find_Result *res
auto version_bytes = (tools_file_size.QuadPart + 1) * 2; // Warning: This multiplication by 2 presumes there is no variable-length encoding in the wchars (wacky characters in the file could betray this expectation).
if (version_bytes > 0x7FFFFFFF) continue; // Avoid overflow.
wchar_t *version = (wchar_t *)calloc(1, version_bytes);
wchar_t *version = (wchar_t *)calloc(1, (usize)version_bytes);
defer (free(version));
auto read_result = fgetws(version, (int)version_bytes, f);

View File

@@ -91,7 +91,7 @@ gb_inline void ptr_set_grow(PtrSet<T> *s) {
template <typename T>
void ptr_set_rehash(PtrSet<T> *s, isize new_count) {
PtrSetIndex i, j;
isize i, j;
PtrSet<T> ns = {};
ptr_set_init(&ns, s->hashes.allocator);
array_resize(&ns.hashes, new_count);
@@ -108,9 +108,9 @@ void ptr_set_rehash(PtrSet<T> *s, isize new_count) {
fr = ptr_set__find(&ns, e->ptr);
j = ptr_set__add_entry(&ns, e->ptr);
if (fr.entry_prev == PTR_SET_SENTINEL) {
ns.hashes.data[fr.hash_index] = j;
ns.hashes.data[fr.hash_index] = cast(PtrSetIndex)j;
} else {
ns.entries.data[fr.entry_prev].next = j;
ns.entries.data[fr.entry_prev].next = cast(PtrSetIndex)j;
}
ns.entries.data[j].next = fr.entry_index;
if (ptr_set__full(&ns)) {
@@ -185,7 +185,7 @@ void ptr_set__erase(PtrSet<T> *s, PtrSetFindResult fr) {
} else {
s->entries.data[fr.entry_prev].next = s->entries.data[fr.entry_index].next;
}
if (fr.entry_index == s->entries.count-1) {
if (cast(isize)fr.entry_index == s->entries.count-1) {
array_pop(&s->entries);
return;
}