[m-rev.] for review: Output INT32_MIN instead of INT32_C(-2147483648).

Peter Wang novalazy at gmail.com
Thu Apr 9 15:56:06 AEST 2020

When -2147483648i32 is used in a Mercury source file,
the generated C source file would contain:


When compiled with gcc in C89/C90 mode (the default on older versions)
on a 32-bit platform, gcc would generate this warning:

    warning: this decimal constant is unsigned only in ISO C90

because the unsuffixed decimal constant 2147483648 is given the type
`unsigned long int' according to C89/C90 rules.
We previously saw the same warning with Mercury `int' literals.

    In C, write out the value of the min_int32 as the symbolic
    constant INT32_MIN. This mirrors behaviour for min_int64.

diff --git a/compiler/c_util.m b/compiler/c_util.m
index ec23ad24f..d131a8526 100644
--- a/compiler/c_util.m
+++ b/compiler/c_util.m
@@ -331,6 +331,7 @@
 :- import_module bool.
 :- import_module int.
+:- import_module int32.
 :- import_module int64.
 :- import_module integer.
 :- import_module require.
@@ -885,9 +886,13 @@ output_uint16_expr_cur_stream(N, !IO) :-
     output_uint16_expr(Stream, N, !IO).
 output_int32_expr(Stream, N, !IO) :-
-    io.write_string(Stream, "INT32_C(", !IO),
-    io.write_int32(Stream, N, !IO),
-    io.write_string(Stream, ")", !IO).
+    ( if N = min_int32 then
+        io.write_string("INT32_MIN", !IO)
+    else
+        io.write_string(Stream, "INT32_C(", !IO),
+        io.write_int32(Stream, N, !IO),
+        io.write_string(Stream, ")", !IO)
+    ).
 output_int32_expr_cur_stream(N, !IO) :-
     io.output_stream(Stream, !IO),

More information about the reviews mailing list