[m-rev.] for review: Avoid right shift count overflow warnings.
Peter Wang
novalazy at gmail.com
Mon Jul 30 16:55:29 AEST 2018
compiler/mlds_to_cs_data.m:
compiler/mlds_to_java_data.m:
In output_int_const_for_csharp and output_int_const_for_java,
right shift by 31 instead of 32 to avoid shift count overflow
warnings from 32-bit compilers.
Simplify the code.
---
compiler/mlds_to_cs_data.m | 14 ++------------
compiler/mlds_to_java_data.m | 14 ++------------
2 files changed, 4 insertions(+), 24 deletions(-)
diff --git a/compiler/mlds_to_cs_data.m b/compiler/mlds_to_cs_data.m
index 1579d0b07..75d49ac5c 100644
--- a/compiler/mlds_to_cs_data.m
+++ b/compiler/mlds_to_cs_data.m
@@ -791,32 +791,22 @@ output_rval_const_for_csharp(Info, Const, !IO) :-
Initializer = get_default_initializer_for_csharp(Info, Type),
io.write_string(Initializer, !IO)
).
:- pred output_int_const_for_csharp(int::in, io::di, io::uo) is det.
output_int_const_for_csharp(N, !IO) :-
% You may wish to see the comment on output_int_const_for_java
% in mlds_to_java_data.m.
( if
- N >= 0,
- N /\ 0x80000000 = 0x80000000,
-
- % The next six lines are the result of inlining
- % "N `legacy_right_shift` 32 = 0" here and simplifying.
- % We don't want to call legacy_right_shift because it is obsolete.
- bits_per_int(IntBits),
- ( if IntBits > 32 then
- unchecked_right_shift(N, 32) = 0
- else
- true
- )
+ N > 0,
+ N >> 31 = 1
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
% tell it otherwise.
io.format("unchecked((int) 0x%x)", [i(N /\ 0xffffffff)], !IO)
else
io.write_int(N, !IO)
).
:- pred output_uint_const_for_csharp(uint::in, io::di, io::uo) is det.
diff --git a/compiler/mlds_to_java_data.m b/compiler/mlds_to_java_data.m
index 9485fc586..df13da685 100644
--- a/compiler/mlds_to_java_data.m
+++ b/compiler/mlds_to_java_data.m
@@ -965,32 +965,22 @@ output_rval_const_for_java(Info, Const, !IO) :-
:- pred output_int_const_for_java(int::in, io::di, io::uo) is det.
output_int_const_for_java(N, !IO) :-
% The Mercury compiler could be using 64-bit integers but Java has 32-bit
% ints. A literal 0xffffffff in a source file would be interpreted by a
% 64-bit Mercury compiler as 4294967295. If it is written out in decimal,
% a Java compiler would rightly complain because the integer is too large
% to fit in a 32-bit int. However, it won't complain if the literal is
% expressed in hexadecimal (nor as the negative decimal -1).
( if
- N >= 0,
- N /\ 0x80000000 = 0x80000000,
-
- % The next six lines are the result of inlining
- % "N `legacy_right_shift` 32 = 0" here and simplifying.
- % We don't want to call legacy_right_shift because it is obsolete.
- bits_per_int(IntBits),
- ( if IntBits > 32 then
- unchecked_right_shift(N, 32) = 0
- else
- true
- )
+ N > 0,
+ N >> 31 = 1
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)
else
io.write_int(N, !IO)
).
:- pred mlds_output_code_addr_for_java(java_out_info::in, mlds_code_addr::in,
bool::in, io::di, io::uo) is det.
--
2.18.0
More information about the reviews
mailing list