[m-rev.] for review: Avoid 0xffffffff in code to write csharp/java ints.
Peter Wang
novalazy at gmail.com
Tue Apr 14 16:45:35 AEST 2020
compiler/mlds_to_cs_data.m:
compiler/mlds_to_java_data.m:
Implement output_int_const_for_{csharp,java} without using
0xffffffff. When that constant appears in a C source file,
the C compiler may warn about it as it will not fit in a 32-bit
signed int.
diff --git a/compiler/mlds_to_cs_data.m b/compiler/mlds_to_cs_data.m
index d6b66f97f..5c1d88ad7 100644
--- a/compiler/mlds_to_cs_data.m
+++ b/compiler/mlds_to_cs_data.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.
%---------------------------------------------------------------------------%
@@ -77,10 +77,12 @@
:- import_module parse_tree.prog_type.
:- import_module int.
+:- import_module int32.
:- import_module map.
:- import_module require.
:- import_module string.
:- import_module term.
+:- import_module uint32.
%---------------------------------------------------------------------------%
%---------------------------------------------------------------------------%
@@ -799,12 +801,14 @@ output_int_const_for_csharp(N, !IO) :-
% in mlds_to_java_data.m.
( if
N > 0,
- N >> 31 = 1
+ not int32.from_int(N, _I32),
+ uint32.from_int(N, U32)
then
% The bit pattern fits in 32 bits, but is too big for a positive
- % integer. The C# compiler will give an error about this, unless we
+ % integer. The C# compiler will report an error about this unless we
% tell it otherwise.
- io.format("unchecked((int) 0x%x)", [i(N /\ 0xffffffff)], !IO)
+ N32 = uint32.cast_to_int(U32),
+ io.format("unchecked((int) 0x%x)", [i(N32)], !IO)
else
io.write_int(N, !IO)
).
diff --git a/compiler/mlds_to_java_data.m b/compiler/mlds_to_java_data.m
index bc5789074..8378a93c0 100644
--- a/compiler/mlds_to_java_data.m
+++ b/compiler/mlds_to_java_data.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.
%---------------------------------------------------------------------------%
@@ -112,6 +112,7 @@
:- import_module string.
:- import_module term.
:- import_module uint.
+:- import_module uint32.
%---------------------------------------------------------------------------%
@@ -973,11 +974,13 @@ output_int_const_for_java(N, !IO) :-
% expressed in hexadecimal (nor as the negative decimal -1).
( if
N > 0,
- N >> 31 = 1
+ not int32.from_int(N, _I32),
+ uint32.from_int(N, U32)
then
% The bit pattern fits in 32 bits, but is too large to write as a
% positive decimal. This branch is unreachable on a 32-bit compiler.
- io.format("0x%x", [i(N /\ 0xffffffff)], !IO)
+ N32 = uint32.cast_to_int(U32),
+ io.format("0x%x", [i(N32)], !IO)
else
io.write_int(N, !IO)
).
--
2.25.0
More information about the reviews
mailing list