[m-rev.] for review: Avoid 0xffffffff in code to shorten overlong identifiers.

Peter Wang novalazy at gmail.com
Tue Apr 14 16:45:36 AEST 2020


compiler/c_util.m:
    Add hex_hash32 function to replace similar code in the following
    three modules. Avoid using 0xffffffff as that constant may cause the
    C compiler to emit warnings.

elds_to_erlang.m:
mlds_to_cs_name.m:
mlds_to_java_class.m:
    Use hex_hash32.

diff --git a/compiler/c_util.m b/compiler/c_util.m
index d131a8526..6fafc4362 100644
--- a/compiler/c_util.m
+++ b/compiler/c_util.m
@@ -2,7 +2,7 @@
 % vim: ft=mercury ts=4 sw=4 et
 %---------------------------------------------------------------------------%
 % Copyright (C) 1999-2007, 2009-2012 The University of Melbourne.
-% Copyright (C) 2013-2018 The Mercury team.
+% Copyright (C) 2013-2020 The Mercury team.
 % This file may only be copied under the terms of the GNU General
 % Public License - see the file COPYING in the Mercury distribution.
 %---------------------------------------------------------------------------%
@@ -324,6 +324,17 @@
     %
 :- pred is_valid_c_identifier(string::in) is semidet.
 
+%---------------------------------------------------------------------------%
+%
+% Utility predicate to shorten overlong identifiers.
+%
+
+    % Return hexadecimal encoded hash of a string.
+    % The resulting string has a length of 8 characters, and it must be
+    % consistent across different compiler backends and bit widths.
+    %
+:- func hex_hash32(string) = string.
+
 %---------------------------------------------------------------------------%
 %---------------------------------------------------------------------------%
 
@@ -1119,6 +1130,16 @@ is_valid_c_identifier(S) :-
     char.is_alpha_or_underscore(Start),
     string.is_all_alnum_or_underscore(S).
 
+%---------------------------------------------------------------------------%
+
+hex_hash32(S0) = S :-
+    Hash = string.hash(S0),
+    % Mask off the lower 32 bits without using 0xffffffff in the generated
+    % target code.
+    Hi = (Hash >> 16) /\ 0xffff,
+    Lo = Hash /\ 0xffff,
+    S = string.format("%04x%04x", [i(Hi), i(Lo)]).
+
 %---------------------------------------------------------------------------%
 :- end_module backend_libs.c_util.
 %---------------------------------------------------------------------------%
diff --git a/compiler/elds_to_erlang.m b/compiler/elds_to_erlang.m
index 990195424..6a95bb32a 100644
--- a/compiler/elds_to_erlang.m
+++ b/compiler/elds_to_erlang.m
@@ -2,7 +2,7 @@
 % vim: ft=mercury ts=4 sw=4 et
 %-----------------------------------------------------------------------------%
 % Copyright (C) 2007-2012 The University of Melbourne.
-% Copyright (C) 2013-2018 The Mercury team.
+% Copyright (C) 2013-2020 The Mercury team.
 % This file may only be copied under the terms of the GNU General
 % Public License - see the file COPYING in the Mercury distribution.
 %-----------------------------------------------------------------------------%
@@ -47,6 +47,7 @@
 :- implementation.
 
 :- import_module backend_libs.
+:- import_module backend_libs.c_util.
 :- import_module backend_libs.rtti.
 :- import_module hlds.hlds_pred.
 :- import_module hlds.hlds_rtti.
@@ -1052,17 +1053,9 @@ shorten_long_atom_name(Name0) = Name :-
     ( if string.length(Name0) =< 200 then
         Name = Name0
     else
-        % Use only lower 32 bits of the hash value so that the result is the
-        % same on 64-bit machines as 32-bit machines.
-        %
-        % XXX we assume that `int' is at least 32 bits wide
-        %
-        % XXX it would be better to use a cryptographic hash function
-        %
-        Hash = string.hash(Name0) /\ 0xffffffff,
         Left = string.left(Name0, 64),
+        Middle = c_util.hex_hash32(Name0),
         Right = string.right(Name0, 64),
-        Middle = string.int_to_base_string(Hash, 16),
         Name = string.append_list([Left, "...", Middle, "...", Right])
     ).
 
diff --git a/compiler/mlds_to_cs_name.m b/compiler/mlds_to_cs_name.m
index acd8f56d7..b7d74801f 100644
--- a/compiler/mlds_to_cs_name.m
+++ b/compiler/mlds_to_cs_name.m
@@ -2,7 +2,7 @@
 % vim: ft=mercury ts=4 sw=4 et
 %---------------------------------------------------------------------------%
 % Copyright (C) 2010-2012 The University of Melbourne.
-% Copyright (C) 2013-2018 The Mercury team.
+% Copyright (C) 2013-2018, 2020 The Mercury team.
 % This file may only be copied under the terms of the GNU General
 % Public License - see the file COPYING in the Mercury distribution.
 %---------------------------------------------------------------------------%
@@ -88,6 +88,7 @@
 :- implementation.
 
 :- import_module backend_libs.
+:- import_module backend_libs.c_util.
 :- import_module backend_libs.rtti.
 :- import_module hlds.
 :- import_module hlds.hlds_module.
@@ -348,9 +349,9 @@ write_identifier_string_for_csharp(String, !IO) :-
     Length = string.length(String),
     ( if Length > 511 then
         Left = string.left(String, 251),
+        Middle = c_util.hex_hash32(String),
         Right = string.right(String, 250),
-        Hash = string.hash(String) /\ 0xffffffff,
-        io.format("%s_%08x_%s", [s(Left), i(Hash), s(Right)], !IO)
+        io.format("%s_%s_%s", [s(Left), s(Middle), s(Right)], !IO)
     else
         io.write_string(String, !IO)
     ).
diff --git a/compiler/mlds_to_java_class.m b/compiler/mlds_to_java_class.m
index e79d96505..c480df1d9 100644
--- a/compiler/mlds_to_java_class.m
+++ b/compiler/mlds_to_java_class.m
@@ -2,7 +2,7 @@
 % vim: ft=mercury ts=4 sw=4 et
 %---------------------------------------------------------------------------%
 % Copyright (C) 2000-2012 The University of Melbourne.
-% Copyright (C) 2013-2018 The Mercury team.
+% Copyright (C) 2013-2018, 2020 The Mercury team.
 % This file may only be copied under the terms of the GNU General
 % Public License - see the file COPYING in the Mercury distribution.
 %---------------------------------------------------------------------------%
@@ -49,6 +49,8 @@
 
 :- implementation.
 
+:- import_module backend_libs.
+:- import_module backend_libs.c_util.
 :- import_module hlds.
 :- import_module hlds.hlds_module.
 :- import_module mdbcomp.
@@ -391,9 +393,9 @@ shorten_class_name(ClassName0) = ClassName :-
         % characters by underscores. The s_ prefix avoids having f_ as the
         % prefix which is used to indicate a mangled name.
         Left = string.left(ClassName0, 44),
+        Middle = c_util.hex_hash32(ClassName0),
         Right = string.right(ClassName0, 44),
-        Hash = string.hash(ClassName0) /\ 0xffffffff,
-        GenName = string.format("s_%s_%08x_%s", [s(Left), i(Hash), s(Right)]),
+        GenName = "s_" ++ Left ++ Middle ++ Right,
         GenList = string.to_char_list(GenName),
         FilterList = list.map(replace_non_alphanum_underscore, GenList),
         ClassName = string.from_char_list(FilterList)
-- 
2.25.0



More information about the reviews mailing list