[m-rev.] for review: use fixed size integer literals in the library and compiler

Julien Fischer jfischer at opturion.com
Tue Aug 29 00:12:22 AEST 2017


This doesn't really require a review, however if anyone
has not yet updated to rotd-2017-08-23 or later let me know
and I will delay committing it.

-------

Use fixed size integer literals in the library and compiler.

configure.ac:
     Check that the bootstrap compiler supports fixed size
     integer types.

library/int16.m:
library/int32.m:
library/int8.m:
library/integer.m:
library/uint16.m:
library/uint32.m:
library/uint8.m:
compiler/lookup_switch.m:
      Use fixed size integer literals instead of casting from ints.

Julien.

diff --git a/compiler/lookup_switch.m b/compiler/lookup_switch.m
index 62e60c9ed..aad76a645 100644
--- a/compiler/lookup_switch.m
+++ b/compiler/lookup_switch.m
@@ -944,16 +944,6 @@ generate_bit_vec_args([Word - Bits | Rest], Count, [Rval | Rvals]) :-

  %-----------------------------------------------------------------------------%

-    % XXX FIXED SIZE INTS
-    % These imports can be deleted once support for fixed size integer literals
-    % has bootstrapped.
-:- import_module int8.
-:- import_module uint8.
-:- import_module int16.
-:- import_module uint16.
-:- import_module int32.
-:- import_module uint32.
-
  default_value_for_type(lt_bool) = const(llconst_int(0)).
  default_value_for_type(lt_int_least8) = const(llconst_int(0)).
  default_value_for_type(lt_uint_least8) = const(llconst_int(0)).
@@ -963,12 +953,12 @@ default_value_for_type(lt_int_least32) = const(llconst_int(0)).
  default_value_for_type(lt_uint_least32) = const(llconst_int(0)).
  default_value_for_type(lt_int(int_type_int)) = const(llconst_int(0)).
  default_value_for_type(lt_int(int_type_uint)) = const(llconst_uint(0u)).
-default_value_for_type(lt_int(int_type_int8)) = const(llconst_int8(cast_from_int(0))).
-default_value_for_type(lt_int(int_type_uint8)) = const(llconst_uint8(cast_from_int(0))).
-default_value_for_type(lt_int(int_type_int16)) = const(llconst_int16(cast_from_int(0))).
-default_value_for_type(lt_int(int_type_uint16)) = const(llconst_uint16(cast_from_int(0))).
-default_value_for_type(lt_int(int_type_int32)) = const(llconst_int32(cast_from_int(0))).
-default_value_for_type(lt_int(int_type_uint32)) = const(llconst_uint32(cast_from_int(0))).
+default_value_for_type(lt_int(int_type_int8)) = const(llconst_int8(0i8)).
+default_value_for_type(lt_int(int_type_uint8)) = const(llconst_uint8(0u8)).
+default_value_for_type(lt_int(int_type_int16)) = const(llconst_int16(0i16)).
+default_value_for_type(lt_int(int_type_uint16)) = const(llconst_uint16(0u16)).
+default_value_for_type(lt_int(int_type_int32)) = const(llconst_int32(0i32)).
+default_value_for_type(lt_int(int_type_uint32)) = const(llconst_uint32(0u32)).
  default_value_for_type(lt_float) = const(llconst_float(0.0)).
  default_value_for_type(lt_string) = const(llconst_string("")).
  default_value_for_type(lt_data_ptr) = const(llconst_int(0)).
diff --git a/configure.ac b/configure.ac
index a609a392d..37e03ac3f 100644
--- a/configure.ac
+++ b/configure.ac
@@ -362,7 +362,7 @@ then

          :- implementation.

-        :- import_module bool, char, string, int, list, uint, univ.
+        :- import_module bool, char, string, int, list, uint32, univ.

          :- mutable(global_var,
              int,
@@ -406,10 +406,10 @@ then
          :- type bear ---> brown ; black ; teddy.
          :- pragma foreign_export_enum("Java", bear/0).

-        :- pred my_uint_xor(uint::in, uint::out) is det.
+        :- pred my_uint_xor(uint32::in, uint32::out) is det.

          my_uint_xor(A, B) :-
-            561u = uint.xor(A, B).
+            561u32 = uint32.xor(A, B).

          :- pred p(t::out) is det.

diff --git a/library/int16.m b/library/int16.m
index 1acf3ca8b..359bbe942 100644
--- a/library/int16.m
+++ b/library/int16.m
@@ -321,19 +321,19 @@ min(X, Y) =
  X div Y = Div :-
      Trunc = X // Y,
      ( if
-        ( X >= cast_from_int(0), Y >= cast_from_int(0)
-        ; X < cast_from_int(0), Y < cast_from_int(0)
-        ; X rem Y = cast_from_int(0)
+        ( X >= 0i16, Y >= 0i16
+        ; X < 0i16, Y < 0i16
+        ; X rem Y = 0i16
          )
      then
          Div = Trunc
      else
-        Div = Trunc - cast_from_int(1)
+        Div = Trunc - 1i16
      ).

  :- pragma inline('//'/2).
  X // Y = Div :-
-    ( if Y = cast_from_int(0) then
+    ( if Y = 0i16 then
          throw(math.domain_error("int16.'//': division by zero"))
      else
          Div = unchecked_quotient(X, Y)
@@ -346,7 +346,7 @@ X mod Y = X - (X div Y) * Y.

  :- pragma inline(rem/2).
  X rem Y = Rem :-
-    ( if Y = cast_from_int(0) then
+    ( if Y = 0i16 then
          throw(math.domain_error("int16.rem: division by zero"))
      else
          Rem = unchecked_rem(X, Y)
@@ -374,17 +374,17 @@ X >> Y = Result :-

  :- pragma inline(even/1).
  even(X) :-
-    (X /\ cast_from_int(1)) = cast_from_int(0).
+    (X /\ 1i16) = 0i16.

  :- pragma inline(odd/1).
  odd(X) :-
-    (X /\ cast_from_int(1)) \= cast_from_int(0).
+    (X /\ 1i16) \= 0i16.

  %---------------------------------------------------------------------------%

-min_int16 = cast_from_int(-32_768).
+min_int16 = -32_768_i16.

-max_int16 = cast_from_int(32_767).
+max_int16 = 32_767_i16.

  %---------------------------------------------------------------------------%

diff --git a/library/int32.m b/library/int32.m
index 3c5272fc1..3311e1581 100644
--- a/library/int32.m
+++ b/library/int32.m
@@ -335,19 +335,19 @@ to_int(_) = _ :-
  X div Y = Div :-
      Trunc = X // Y,
      ( if
-        ( X >= cast_from_int(0), Y >= cast_from_int(0)
-        ; X < cast_from_int(0), Y < cast_from_int(0)
-        ; X rem Y = cast_from_int(0)
+        ( X >= 0i32, Y >= 0i32
+        ; X < 0i32, Y < 0i32
+        ; X rem Y = 0i32
          )
      then
          Div = Trunc
      else
-        Div = Trunc - cast_from_int(1)
+        Div = Trunc - 1i32
      ).

  :- pragma inline('//'/2).
  X // Y = Div :-
-    ( if Y = cast_from_int(0) then
+    ( if Y = 0i32 then
          throw(math.domain_error("int32.'//': division by zero"))
      else
          Div = unchecked_quotient(X, Y)
@@ -360,7 +360,7 @@ X mod Y = X  - (X div Y) * Y.

  :- pragma inline(rem/2).
  X rem Y = Rem :-
-    ( if Y = cast_from_int(0) then
+    ( if Y = 0i32 then
          throw(math.domain_error("int32.rem: division by zero"))
      else
          Rem = unchecked_rem(X, Y)
@@ -396,11 +396,11 @@ min(X, Y) =

  :- pragma inline(even/1).
  even(X) :-
-    (X /\ cast_from_int(1)) = cast_from_int(0).
+    (X /\ 1i32) = 0i32.

  :- pragma inline(odd/1).
  odd(X) :-
-    (X /\ cast_from_int(1)) \= cast_from_int(0).
+    (X /\ 1i32) \= 0i32.

  %---------------------------------------------------------------------------%

diff --git a/library/int8.m b/library/int8.m
index f702c94f4..e2d990e49 100644
--- a/library/int8.m
+++ b/library/int8.m
@@ -318,19 +318,19 @@ cast_from_uint8(_) = _ :-
  X div Y = Div :-
      Trunc = X // Y,
      ( if
-        ( X >= cast_from_int(0), Y >= cast_from_int(0)
-        ; X < cast_from_int(0), Y < cast_from_int(0)
-        ; X rem Y = cast_from_int(0)
+        ( X >= 0i8, Y >= 0i8
+        ; X < 0i8, Y < 0i8
+        ; X rem Y = 0i8
          )
      then
          Div = Trunc
      else
-        Div = Trunc - cast_from_int(1)
+        Div = Trunc - 1i8
      ).

  :- pragma inline('//'/2).
  X // Y = Div :-
-    ( if Y = cast_from_int(0) then
+    ( if Y = 0i8 then
          throw(math.domain_error("int8.'//': division by zero"))
      else
          Div = unchecked_quotient(X, Y)
@@ -343,7 +343,7 @@ X mod Y = X - (X div Y) * Y.

  :- pragma inline(rem/2).
  X rem Y = Rem :-
-    ( if Y = cast_from_int(0) then
+    ( if Y = 0i8 then
          throw(math.domain_error("int8.rem: division by zero"))
      else
          Rem = unchecked_rem(X, Y)
@@ -379,17 +379,17 @@ min(X, Y) =

  :- pragma inline(even/1).
  even(X) :-
-    (X /\ cast_from_int(1)) = cast_from_int(0).
+    (X /\ 1i8) = 0i8.

  :- pragma inline(odd/1).
  odd(X) :-
-    (X /\ cast_from_int(1)) \= cast_from_int(0).
+    (X /\ 1i8) \= 0i8.

  %---------------------------------------------------------------------------%

-min_int8 = cast_from_int(-128).
+min_int8 = -128_i8.

-max_int8 = cast_from_int(127).
+max_int8 = 127_i8.

  %---------------------------------------------------------------------------%

diff --git a/library/integer.m b/library/integer.m
index ed984891e..d6bc6fa55 100644
--- a/library/integer.m
+++ b/library/integer.m
@@ -1413,7 +1413,7 @@ det_to_uint(Integer) = UInt :-

  to_int8(Integer, Int8) :-
      ( if is_zero(Integer) then
-        Int8 = int8.cast_from_int(0)
+        Int8 = 0i8
      else
          Integer = i(_, [Digit]),
          int8.from_int(Digit, Int8)
@@ -1431,7 +1431,7 @@ det_to_int8(Integer) = Int8 :-

  to_uint8(Integer, UInt8) :-
      ( if is_zero(Integer) then
-        UInt8 = uint8.cast_from_int(0)
+        UInt8 = 0u8
      else
          Integer = i(_, [Digit]),
          uint8.from_int(Digit, UInt8)
@@ -1493,7 +1493,7 @@ to_uint32(Integer, UInt32) :-
      Integer >= integer.zero,
      Integer =< integer.from_uint32(uint32.max_uint32),
      Integer = i(_Sign, Digits),
-    UInt32 = uint32_list(Digits, uint32.cast_from_int(0)).
+    UInt32 = uint32_list(Digits, 0u32).

  :- func uint32_list(list(int), uint32) = uint32.

@@ -1802,7 +1802,7 @@ from_int32(I32) = Integer :-
      Integer = integer(I).

  from_uint32(U32) = Integer :-
-    ( if U32 = cast_from_int(0) then
+    ( if U32 = 0u32 then
          Integer = integer.zero
      else if U32 < cast_from_int(base) then
          Integer = i(1, [cast_to_int(U32)])
@@ -1817,7 +1817,7 @@ uint32_to_digits(U) = uint32_to_digits_2(U, integer.zero).
  :- func uint32_to_digits_2(uint32, integer) = integer.

  uint32_to_digits_2(U, Tail) = Result :-
-    ( if U = cast_from_int(0) then
+    ( if U = 0u32 then
          Result = Tail
      else
          Tail = i(Length, Digits),
diff --git a/library/uint16.m b/library/uint16.m
index d325df05d..63cf097bf 100644
--- a/library/uint16.m
+++ b/library/uint16.m
@@ -35,6 +35,8 @@

  :- func to_int(uint16) = int.

+%--------------------------------------------------------------------------%
+
      % Less than.
      %
  :- pred (uint16::in) < (uint16::in) is semidet.
@@ -267,7 +269,7 @@ X div Y = X // Y.

  :- pragma inline('//'/2).
  X // Y = Div :-
-    ( if Y = cast_from_int(0) then
+    ( if Y = 0u16 then
          throw(math.domain_error("uint16.'//': division by zero"))
      else
          Div = unchecked_quotient(X, Y)
@@ -280,7 +282,7 @@ X mod Y = X rem Y.

  :- pragma inline(rem/2).
  X rem Y = Rem :-
-    ( if Y = cast_from_int(0) then
+    ( if Y = 0u16 then
          throw(math.domain_error("uint16.rem: division by zero"))
      else
          Rem = unchecked_rem(X, Y)
@@ -316,11 +318,11 @@ min(X, Y) =

  :- pragma inline(even/1).
  even(X) :-
-    (X /\ cast_from_int(1)) = cast_from_int(0).
+    (X /\ 1u16) = 0u16.

  :- pragma inline(odd/1).
  odd(X) :-
-    (X /\ cast_from_int(1)) \= cast_from_int(0).
+    (X /\ 1u16) \= 0u16.

  %---------------------------------------------------------------------------%

diff --git a/library/uint32.m b/library/uint32.m
index 3ced692c7..ec6507534 100644
--- a/library/uint32.m
+++ b/library/uint32.m
@@ -33,6 +33,8 @@

  :- func cast_to_int(uint32) = int.

+%---------------------------------------------------------------------------%
+
      % Less than.
      %
  :- pred (uint32::in) < (uint32::in) is semidet.
@@ -294,7 +296,7 @@ X div Y = X // Y.

  :- pragma inline('//'/2).
  X // Y = Div :-
-    ( if Y = cast_from_int(0) then
+    ( if Y = 0u32 then
          throw(math.domain_error("uint32.'//': division by zero"))
      else
          Div = unchecked_quotient(X, Y)
@@ -307,7 +309,7 @@ X mod Y = X rem Y.

  :- pragma inline(rem/2).
  X rem Y = Rem :-
-    ( if Y = cast_from_int(0) then
+    ( if Y = 0u32 then
          throw(math.domain_error("uint32.rem: division by zero"))
      else
          Rem = unchecked_rem(X, Y)
@@ -343,11 +345,11 @@ min(X, Y) =

  :- pragma inline(even/1).
  even(X) :-
-    (X /\ cast_from_int(1)) = cast_from_int(0).
+    (X /\ 1u32) = 0u32.

  :- pragma inline(odd/1).
  odd(X) :-
-    (X /\ cast_from_int(1)) \= cast_from_int(0).
+    (X /\ 1u32) \= 0u32.

  %---------------------------------------------------------------------------%

diff --git a/library/uint8.m b/library/uint8.m
index 8f88d7656..7d853d542 100644
--- a/library/uint8.m
+++ b/library/uint8.m
@@ -35,6 +35,8 @@

  :- func to_int(uint8) = int.

+%---------------------------------------------------------------------------%
+
      % Less than.
      %
  :- pred (uint8::in) < (uint8::in) is semidet.
@@ -267,7 +269,7 @@ X div Y = X // Y.

  :- pragma inline('//'/2).
  X // Y = Div :-
-    ( if Y = cast_from_int(0) then
+    ( if Y = 0u8 then
          throw(math.domain_error("uint.'//': division by zero"))
      else
          Div = unchecked_quotient(X, Y)
@@ -280,7 +282,7 @@ X mod Y = X rem Y.

  :- pragma inline(rem/2).
  X rem Y = Rem :-
-    ( if Y = cast_from_int(0) then
+    ( if Y = 0u8 then
          throw(math.domain_error("uint8.rem: division by zero"))
      else
          Rem = unchecked_rem(X, Y)
@@ -316,11 +318,11 @@ min(X, Y) =

  :- pragma inline(even/1).
  even(X) :-
-    (X /\ cast_from_int(1)) = cast_from_int(0).
+    (X /\ 1u8) = 0u8.

  :- pragma inline(odd/1).
  odd(X) :-
-    (X /\ cast_from_int(1)) \= cast_from_int(0).
+    (X /\ 1u8) \= 0u8.

  %---------------------------------------------------------------------------%



More information about the reviews mailing list