Patchwork [gccgo] Do array index computations in sizetype

login
register
mail settings
Submitter Ian Taylor
Date July 28, 2010, 1:46 p.m.
Message ID <mcrzkxbn2hg.fsf@google.com>
Download mbox | patch
Permalink /patch/60142/
State New
Headers show

Comments

Ian Taylor - July 28, 2010, 1:46 p.m.
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

Patch

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),