From patchwork Wed Jul 28 13:46:19 2010 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: [gccgo] Do array index computations in sizetype Date: Wed, 28 Jul 2010 03:46:19 -0000 From: Ian Taylor X-Patchwork-Id: 60142 Message-Id: To: gcc-patches@gcc.gnu.org, gofrontend-dev@googlegroups.com Array indexes are stored as int, but when we compute an array offset we should use sizetype (aka size_t). That way, although the number of elements in the array is restricted to 32 bits, the total size of the array is not. Committed to gccgo branch. Ian diff -r 9bddc2bbd1b7 go/expressions.cc --- a/go/expressions.cc Wed Jul 28 06:37:17 2010 -0700 +++ b/go/expressions.cc Wed Jul 28 06:42:37 2010 -0700 @@ -8670,6 +8670,7 @@ build3(COND_EXPR, void_type_node, bad_index, crash, NULL_TREE), start_tree); + start_tree = fold_convert_loc(loc, sizetype, start_tree); if (array_type->length() != NULL) { @@ -8683,14 +8684,10 @@ tree values = array_type->value_pointer_tree(gogo, array_tree); tree element_type_tree = array_type->element_type()->get_tree(gogo); tree element_size = TYPE_SIZE_UNIT(element_type_tree); - tree offset = fold_build2_loc(loc, MULT_EXPR, TREE_TYPE(length_tree), - start_tree, - fold_convert_loc(loc, - TREE_TYPE(length_tree), - element_size)); + tree offset = fold_build2_loc(loc, MULT_EXPR, sizetype, + start_tree, element_size); tree ptr = fold_build2_loc(loc, POINTER_PLUS_EXPR, - TREE_TYPE(values), values, - fold_convert_loc(loc, sizetype, offset)); + TREE_TYPE(values), values, offset); return build_fold_indirect_ref(ptr); } } @@ -8727,17 +8724,16 @@ tree element_type_tree = array_type->element_type()->get_tree(gogo); tree element_size = TYPE_SIZE_UNIT(element_type_tree); - element_size = fold_convert_loc(loc, TREE_TYPE(length_tree), element_size); - - tree offset = fold_build2_loc(loc, MULT_EXPR, TREE_TYPE(length_tree), - start_tree, element_size); + + tree offset = fold_build2_loc(loc, MULT_EXPR, sizetype, + fold_convert_loc(loc, sizetype, start_tree), + element_size); tree value_pointer = array_type->value_pointer_tree(gogo, array_tree); value_pointer = fold_build2_loc(loc, POINTER_PLUS_EXPR, TREE_TYPE(value_pointer), - value_pointer, - fold_convert_loc(loc, sizetype, offset)); + value_pointer, offset); tree result_length_tree = fold_build2_loc(loc, MINUS_EXPR, TREE_TYPE(length_tree),