From patchwork Wed Jul 28 13:46:19 2010 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Lance Taylor X-Patchwork-Id: 60142 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) by ozlabs.org (Postfix) with SMTP id 08617B6EED for ; Wed, 28 Jul 2010 23:46:40 +1000 (EST) Received: (qmail 11236 invoked by alias); 28 Jul 2010 13:46:39 -0000 Received: (qmail 11226 invoked by uid 22791); 28 Jul 2010 13:46:38 -0000 X-SWARE-Spam-Status: No, hits=-2.3 required=5.0 tests=AWL, BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, SPF_HELO_PASS, TW_CC, T_RP_MATCHES_RCVD, T_TVD_MIME_NO_HEADERS X-Spam-Check-By: sourceware.org Received: from smtp-out.google.com (HELO smtp-out.google.com) (216.239.44.51) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Wed, 28 Jul 2010 13:46:25 +0000 Received: from wpaz9.hot.corp.google.com (wpaz9.hot.corp.google.com [172.24.198.73]) by smtp-out.google.com with ESMTP id o6SDkOaa008810 for ; Wed, 28 Jul 2010 06:46:24 -0700 Received: from wyj26 (wyj26.prod.google.com [10.241.227.90]) by wpaz9.hot.corp.google.com with ESMTP id o6SDjsjU030982 for ; Wed, 28 Jul 2010 06:46:23 -0700 Received: by wyj26 with SMTP id 26so5557563wyj.27 for ; Wed, 28 Jul 2010 06:46:22 -0700 (PDT) Received: by 10.227.136.136 with SMTP id r8mr10626084wbt.156.1280324782566; Wed, 28 Jul 2010 06:46:22 -0700 (PDT) Received: from coign.google.com (dhcp-172-28-249-136.lul.corp.google.com [172.28.249.136]) by mx.google.com with ESMTPS id a20sm3633862wba.12.2010.07.28.06.46.21 (version=TLSv1/SSLv3 cipher=RC4-MD5); Wed, 28 Jul 2010 06:46:21 -0700 (PDT) From: Ian Lance Taylor To: gcc-patches@gcc.gnu.org, gofrontend-dev@googlegroups.com Subject: [gccgo] Do array index computations in sizetype Date: Wed, 28 Jul 2010 06:46:19 -0700 Message-ID: User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) MIME-Version: 1.0 X-System-Of-Record: true X-IsSubscribed: yes Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org 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),