authorgravatar for johnnymarler@gmail.comJonathan Marler <johnnymarler@gmail.com> 2019-08-22 13:56:04-06:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-08-23 11:14:08-04:00
log9322eee80aadd6d9f80ebc11d29d09898237a14a
treec8848c067244ee16f9cd5525a366d4bb451ae474
parent0e75fef1decdaba918b2abc84977ecadb010ad32

Encapsulate bigint representation, assert on cast data loss


5 files changed, 55 insertions(+), 31 deletions(-)

src/analyze.cpp+4-4
......@@ -1036,7 +1036,7 @@ static bool analyze_const_align(CodeGen *g, Scope *scope, AstNode *node, uint32_
10361036 if (type_is_invalid(align_result->type))
10371037 return false;
10381038
1039 uint32_t align_bytes = bigint_as_unsigned(&align_result->data.x_bigint);
1039 uint32_t align_bytes = bigint_as_u32(&align_result->data.x_bigint);
10401040 if (align_bytes == 0) {
10411041 add_node_error(g, node, buf_sprintf("alignment must be >= 1"));
10421042 return false;
......@@ -1068,7 +1068,7 @@ static bool analyze_const_string(CodeGen *g, Scope *scope, AstNode *node, Buf **
10681068 return true;
10691069 }
10701070 expand_undef_array(g, array_val);
1071 size_t len = bigint_as_unsigned(&len_field->data.x_bigint);
1071 size_t len = bigint_as_usize(&len_field->data.x_bigint);
10721072 Buf *result = buf_alloc();
10731073 buf_resize(result, len);
10741074 for (size_t i = 0; i < len; i += 1) {
......@@ -1078,7 +1078,7 @@ static bool analyze_const_string(CodeGen *g, Scope *scope, AstNode *node, Buf **
10781078 add_node_error(g, node, buf_sprintf("use of undefined value"));
10791079 return false;
10801080 }
1081 uint64_t big_c = bigint_as_unsigned(&char_val->data.x_bigint);
1081 uint64_t big_c = bigint_as_u64(&char_val->data.x_bigint);
10821082 assert(big_c <= UINT8_MAX);
10831083 uint8_t c = (uint8_t)big_c;
10841084 buf_ptr(result)[i] = c;
......@@ -5976,7 +5976,7 @@ void render_const_value(CodeGen *g, Buf *buf, ConstExprValue *const_val) {
59765976 {
59775977 if (is_slice(type_entry)) {
59785978 ConstExprValue *len_val = &const_val->data.x_struct.fields[slice_len_index];
5979 size_t len = bigint_as_unsigned(&len_val->data.x_bigint);
5979 size_t len = bigint_as_usize(&len_val->data.x_bigint);
59805980
59815981 ConstExprValue *ptr_val = &const_val->data.x_struct.fields[slice_ptr_index];
59825982 if (ptr_val->special == ConstValSpecialUndef) {
src/bigint.cpp+22-1
......@@ -15,6 +15,8 @@
1515#include <limits>
1616#include <algorithm>
1717
18static uint64_t bigint_as_unsigned(const BigInt *bigint);
19
1820static void bigint_normalize(BigInt *dest) {
1921 const uint64_t *digits = bigint_ptr(dest);
2022
......@@ -1660,7 +1662,7 @@ size_t bigint_clz(const BigInt *bi, size_t bit_count) {
16601662 return count;
16611663}
16621664
1663uint64_t bigint_as_unsigned(const BigInt *bigint) {
1665static uint64_t bigint_as_unsigned(const BigInt *bigint) {
16641666 assert(!bigint->is_negative);
16651667 if (bigint->digit_count == 0) {
16661668 return 0;
......@@ -1671,6 +1673,25 @@ uint64_t bigint_as_unsigned(const BigInt *bigint) {
16711673 }
16721674}
16731675
1676uint64_t bigint_as_u64(const BigInt *bigint)
1677{
1678 return bigint_as_unsigned(bigint);
1679}
1680
1681uint32_t bigint_as_u32(const BigInt *bigint) {
1682 uint64_t value64 = bigint_as_unsigned(bigint);
1683 uint32_t value32 = (uint32_t)value64;
1684 assert (value64 == value32);
1685 return value32;
1686}
1687
1688size_t bigint_as_usize(const BigInt *bigint) {
1689 uint64_t value64 = bigint_as_unsigned(bigint);
1690 size_t valueUsize = (size_t)value64;
1691 assert (value64 == valueUsize);
1692 return valueUsize;
1693}
1694
16741695int64_t bigint_as_signed(const BigInt *bigint) {
16751696 if (bigint->digit_count == 0) {
16761697 return 0;
src/bigint.hpp+4-1
......@@ -36,7 +36,10 @@ void bigint_init_bigfloat(BigInt *dest, const BigFloat *op);
3636void bigint_init_data(BigInt *dest, const uint64_t *digits, size_t digit_count, bool is_negative);
3737
3838// panics if number won't fit
39uint64_t bigint_as_unsigned(const BigInt *bigint);
39uint64_t bigint_as_u64(const BigInt *bigint);
40uint32_t bigint_as_u32(const BigInt *bigint);
41size_t bigint_as_usize(const BigInt *bigint);
42
4043int64_t bigint_as_signed(const BigInt *bigint);
4144
4245static inline const uint64_t *bigint_ptr(const BigInt *bigint) {
src/codegen.cpp+1-1
......@@ -2872,7 +2872,7 @@ static void add_error_range_check(CodeGen *g, ZigType *err_set_type, ZigType *in
28722872 eval_min_max_value_int(g, int_type, &biggest_possible_err_val, true);
28732873
28742874 if (bigint_fits_in_bits(&biggest_possible_err_val, 64, false) &&
2875 bigint_as_unsigned(&biggest_possible_err_val) < g->errors_by_index.length)
2875 bigint_as_usize(&biggest_possible_err_val) < g->errors_by_index.length)
28762876 {
28772877 ok_bit = neq_zero_bit;
28782878 } else {
src/ir.cpp+24-24
......@@ -5766,7 +5766,7 @@ static IrInstruction *ir_gen_pointer_type(IrBuilder *irb, Scope *scope, AstNode
57665766 buf_sprintf("value %s too large for u32 bit offset", buf_ptr(val_buf)));
57675767 return irb->codegen->invalid_instruction;
57685768 }
5769 bit_offset_start = bigint_as_unsigned(node->data.pointer_type.bit_offset_start);
5769 bit_offset_start = bigint_as_u32(node->data.pointer_type.bit_offset_start);
57705770 }
57715771
57725772 uint32_t host_int_bytes = 0;
......@@ -5778,7 +5778,7 @@ static IrInstruction *ir_gen_pointer_type(IrBuilder *irb, Scope *scope, AstNode
57785778 buf_sprintf("value %s too large for u32 byte count", buf_ptr(val_buf)));
57795779 return irb->codegen->invalid_instruction;
57805780 }
5781 host_int_bytes = bigint_as_unsigned(node->data.pointer_type.host_int_bytes);
5781 host_int_bytes = bigint_as_u32(node->data.pointer_type.host_int_bytes);
57825782 }
57835783
57845784 if (host_int_bytes != 0 && bit_offset_start >= host_int_bytes * 8) {
......@@ -11550,7 +11550,7 @@ static IrInstruction *ir_analyze_int_to_err(IrAnalyze *ira, IrInstruction *sourc
1155011550 return ira->codegen->invalid_instruction;
1155111551 }
1155211552
11553 size_t index = bigint_as_unsigned(&val->data.x_bigint);
11553 size_t index = bigint_as_usize(&val->data.x_bigint);
1155411554 result->value.data.x_err_set = ira->codegen->errors_by_index.at(index);
1155511555 return result;
1155611556 } else {
......@@ -12520,7 +12520,7 @@ static bool ir_resolve_align(IrAnalyze *ira, IrInstruction *value, uint32_t *out
1252012520 if (!const_val)
1252112521 return false;
1252212522
12523 uint32_t align_bytes = bigint_as_unsigned(&const_val->data.x_bigint);
12523 uint32_t align_bytes = bigint_as_u32(&const_val->data.x_bigint);
1252412524 if (align_bytes == 0) {
1252512525 ir_add_error(ira, value, buf_sprintf("alignment must be >= 1"));
1252612526 return false;
......@@ -12547,7 +12547,7 @@ static bool ir_resolve_unsigned(IrAnalyze *ira, IrInstruction *value, ZigType *i
1254712547 if (!const_val)
1254812548 return false;
1254912549
12550 *out = bigint_as_unsigned(&const_val->data.x_bigint);
12550 *out = bigint_as_u64(&const_val->data.x_bigint);
1255112551 return true;
1255212552}
1255312553
......@@ -12595,7 +12595,7 @@ static bool ir_resolve_atomic_order(IrAnalyze *ira, IrInstruction *value, Atomic
1259512595 if (!const_val)
1259612596 return false;
1259712597
12598 *out = (AtomicOrder)bigint_as_unsigned(&const_val->data.x_enum_tag);
12598 *out = (AtomicOrder)bigint_as_u32(&const_val->data.x_enum_tag);
1259912599 return true;
1260012600}
1260112601
......@@ -12615,7 +12615,7 @@ static bool ir_resolve_atomic_rmw_op(IrAnalyze *ira, IrInstruction *value, Atomi
1261512615 if (!const_val)
1261612616 return false;
1261712617
12618 *out = (AtomicRmwOp)bigint_as_unsigned(&const_val->data.x_enum_tag);
12618 *out = (AtomicRmwOp)bigint_as_u32(&const_val->data.x_enum_tag);
1261912619 return true;
1262012620}
1262112621
......@@ -12635,7 +12635,7 @@ static bool ir_resolve_global_linkage(IrAnalyze *ira, IrInstruction *value, Glob
1263512635 if (!const_val)
1263612636 return false;
1263712637
12638 *out = (GlobalLinkageId)bigint_as_unsigned(&const_val->data.x_enum_tag);
12638 *out = (GlobalLinkageId)bigint_as_u32(&const_val->data.x_enum_tag);
1263912639 return true;
1264012640}
1264112641
......@@ -12655,7 +12655,7 @@ static bool ir_resolve_float_mode(IrAnalyze *ira, IrInstruction *value, FloatMod
1265512655 if (!const_val)
1265612656 return false;
1265712657
12658 *out = (FloatMode)bigint_as_unsigned(&const_val->data.x_enum_tag);
12658 *out = (FloatMode)bigint_as_u32(&const_val->data.x_enum_tag);
1265912659 return true;
1266012660}
1266112661
......@@ -12684,7 +12684,7 @@ static Buf *ir_resolve_str(IrAnalyze *ira, IrInstruction *value) {
1268412684 return array_val->data.x_array.data.s_buf;
1268512685 }
1268612686 expand_undef_array(ira->codegen, array_val);
12687 size_t len = bigint_as_unsigned(&len_field->data.x_bigint);
12687 size_t len = bigint_as_usize(&len_field->data.x_bigint);
1268812688 Buf *result = buf_alloc();
1268912689 buf_resize(result, len);
1269012690 for (size_t i = 0; i < len; i += 1) {
......@@ -12694,7 +12694,7 @@ static Buf *ir_resolve_str(IrAnalyze *ira, IrInstruction *value) {
1269412694 ir_add_error(ira, casted_value, buf_sprintf("use of undefined value"));
1269512695 return nullptr;
1269612696 }
12697 uint64_t big_c = bigint_as_unsigned(&char_val->data.x_bigint);
12697 uint64_t big_c = bigint_as_u64(&char_val->data.x_bigint);
1269812698 assert(big_c <= UINT8_MAX);
1269912699 uint8_t c = (uint8_t)big_c;
1270012700 buf_ptr(result)[i] = c;
......@@ -13829,7 +13829,7 @@ static IrInstruction *ir_analyze_array_cat(IrAnalyze *ira, IrInstructionBinOp *i
1382913829 op1_array_val = ptr_val->data.x_ptr.data.base_array.array_val;
1383013830 op1_array_index = ptr_val->data.x_ptr.data.base_array.elem_index;
1383113831 ConstExprValue *len_val = &op1_val->data.x_struct.fields[slice_len_index];
13832 op1_array_end = op1_array_index + bigint_as_unsigned(&len_val->data.x_bigint);
13832 op1_array_end = op1_array_index + bigint_as_usize(&len_val->data.x_bigint);
1383313833 } else {
1383413834 ir_add_error(ira, op1,
1383513835 buf_sprintf("expected array or C string literal, found '%s'", buf_ptr(&op1->value.type->name)));
......@@ -13862,7 +13862,7 @@ static IrInstruction *ir_analyze_array_cat(IrAnalyze *ira, IrInstructionBinOp *i
1386213862 op2_array_val = ptr_val->data.x_ptr.data.base_array.array_val;
1386313863 op2_array_index = ptr_val->data.x_ptr.data.base_array.elem_index;
1386413864 ConstExprValue *len_val = &op2_val->data.x_struct.fields[slice_len_index];
13865 op2_array_end = op2_array_index + bigint_as_unsigned(&len_val->data.x_bigint);
13865 op2_array_end = op2_array_index + bigint_as_usize(&len_val->data.x_bigint);
1386613866 } else {
1386713867 ir_add_error(ira, op2,
1386813868 buf_sprintf("expected array or C string literal, found '%s'", buf_ptr(&op2->value.type->name)));
......@@ -16734,7 +16734,7 @@ static IrInstruction *ir_analyze_instruction_elem_ptr(IrAnalyze *ira, IrInstruct
1673416734 uint64_t abi_align = get_abi_alignment(ira->codegen, return_type->data.pointer.child_type);
1673516735 uint64_t ptr_align = get_ptr_align(ira->codegen, return_type);
1673616736 if (instr_is_comptime(casted_elem_index)) {
16737 uint64_t index = bigint_as_unsigned(&casted_elem_index->value.data.x_bigint);
16737 uint64_t index = bigint_as_u64(&casted_elem_index->value.data.x_bigint);
1673816738 if (array_type->id == ZigTypeIdArray) {
1673916739 uint64_t array_len = array_type->data.array.len;
1674016740 if (index >= array_len) {
......@@ -16896,7 +16896,7 @@ static IrInstruction *ir_analyze_instruction_elem_ptr(IrAnalyze *ira, IrInstruct
1689616896 ConstExprValue *len_field = &array_ptr_val->data.x_struct.fields[slice_len_index];
1689716897 IrInstruction *result = ir_const(ira, &elem_ptr_instruction->base, return_type);
1689816898 ConstExprValue *out_val = &result->value;
16899 uint64_t slice_len = bigint_as_unsigned(&len_field->data.x_bigint);
16899 uint64_t slice_len = bigint_as_u64(&len_field->data.x_bigint);
1690016900 if (index >= slice_len) {
1690116901 ir_add_error_node(ira, elem_ptr_instruction->base.source_node,
1690216902 buf_sprintf("index %" ZIG_PRI_u64 " outside slice of size %" ZIG_PRI_u64,
......@@ -21181,7 +21181,7 @@ static IrInstruction *ir_analyze_instruction_from_bytes(IrAnalyze *ira, IrInstru
2118121181
2118221182 ConstExprValue *len_val = &val->data.x_struct.fields[slice_len_index];
2118321183 if (value_is_comptime(len_val)) {
21184 known_len = bigint_as_unsigned(&len_val->data.x_bigint);
21184 known_len = bigint_as_u64(&len_val->data.x_bigint);
2118521185 have_known_len = true;
2118621186 }
2118721187 }
......@@ -21527,7 +21527,7 @@ static IrInstruction *ir_analyze_instruction_memset(IrAnalyze *ira, IrInstructio
2152721527 zig_panic("TODO memset on null ptr");
2152821528 }
2152921529
21530 size_t count = bigint_as_unsigned(&casted_count->value.data.x_bigint);
21530 size_t count = bigint_as_usize(&casted_count->value.data.x_bigint);
2153121531 size_t end = start + count;
2153221532 if (end > bound_end) {
2153321533 ir_add_error(ira, count_value, buf_sprintf("out of bounds pointer access"));
......@@ -21612,7 +21612,7 @@ static IrInstruction *ir_analyze_instruction_memcpy(IrAnalyze *ira, IrInstructio
2161221612 casted_count->value.special == ConstValSpecialStatic &&
2161321613 casted_dest_ptr->value.data.x_ptr.special != ConstPtrSpecialHardCodedAddr)
2161421614 {
21615 size_t count = bigint_as_unsigned(&casted_count->value.data.x_bigint);
21615 size_t count = bigint_as_usize(&casted_count->value.data.x_bigint);
2161621616
2161721617 ConstExprValue *dest_ptr_val = &casted_dest_ptr->value;
2161821618 ConstExprValue *dest_elements;
......@@ -21897,7 +21897,7 @@ static IrInstruction *ir_analyze_instruction_slice(IrAnalyze *ira, IrInstruction
2189721897 case ConstPtrSpecialBaseArray:
2189821898 array_val = parent_ptr->data.x_ptr.data.base_array.array_val;
2189921899 abs_offset = parent_ptr->data.x_ptr.data.base_array.elem_index;
21900 rel_end = bigint_as_unsigned(&len_val->data.x_bigint);
21900 rel_end = bigint_as_usize(&len_val->data.x_bigint);
2190121901 break;
2190221902 case ConstPtrSpecialBaseStruct:
2190321903 zig_panic("TODO slice const inner struct");
......@@ -21910,7 +21910,7 @@ static IrInstruction *ir_analyze_instruction_slice(IrAnalyze *ira, IrInstruction
2191021910 case ConstPtrSpecialHardCodedAddr:
2191121911 array_val = nullptr;
2191221912 abs_offset = 0;
21913 rel_end = bigint_as_unsigned(&len_val->data.x_bigint);
21913 rel_end = bigint_as_usize(&len_val->data.x_bigint);
2191421914 break;
2191521915 case ConstPtrSpecialFunction:
2191621916 zig_panic("TODO slice of slice cast from function");
......@@ -21921,7 +21921,7 @@ static IrInstruction *ir_analyze_instruction_slice(IrAnalyze *ira, IrInstruction
2192121921 zig_unreachable();
2192221922 }
2192321923
21924 uint64_t start_scalar = bigint_as_unsigned(&casted_start->value.data.x_bigint);
21924 uint64_t start_scalar = bigint_as_u64(&casted_start->value.data.x_bigint);
2192521925 if (!ptr_is_undef && start_scalar > rel_end) {
2192621926 ir_add_error(ira, &instruction->base, buf_sprintf("out of bounds slice"));
2192721927 return ira->codegen->invalid_instruction;
......@@ -21929,7 +21929,7 @@ static IrInstruction *ir_analyze_instruction_slice(IrAnalyze *ira, IrInstruction
2192921929
2193021930 uint64_t end_scalar;
2193121931 if (end) {
21932 end_scalar = bigint_as_unsigned(&end->value.data.x_bigint);
21932 end_scalar = bigint_as_u64(&end->value.data.x_bigint);
2193321933 } else {
2193421934 end_scalar = rel_end;
2193521935 }
......@@ -23500,7 +23500,7 @@ static Error buf_read_value_bytes(IrAnalyze *ira, CodeGen *codegen, AstNode *sou
2350023500 BigInt bn;
2350123501 bigint_read_twos_complement(&bn, buf, codegen->builtin_types.entry_usize->data.integral.bit_count,
2350223502 codegen->is_big_endian, false);
23503 val->data.x_ptr.data.hard_coded_addr.addr = bigint_as_unsigned(&bn);
23503 val->data.x_ptr.data.hard_coded_addr.addr = bigint_as_usize(&bn);
2350423504 return ErrorNone;
2350523505 }
2350623506 case ZigTypeIdArray:
......@@ -23693,7 +23693,7 @@ static IrInstruction *ir_analyze_int_to_ptr(IrAnalyze *ira, IrInstruction *sourc
2369323693 if (!val)
2369423694 return ira->codegen->invalid_instruction;
2369523695
23696 uint64_t addr = bigint_as_unsigned(&val->data.x_bigint);
23696 uint64_t addr = bigint_as_u64(&val->data.x_bigint);
2369723697 if (!ptr_allows_addr_zero(ptr_type) && addr == 0) {
2369823698 ir_add_error(ira, source_instr,
2369923699 buf_sprintf("pointer type '%s' does not allow address zero", buf_ptr(&ptr_type->name)));