[m-rev.] diff: two fixes for uint support

Julien Fischer jfischer at opturion.com
Wed May 24 09:58:09 AEST 2017


Two fixes for uint support.

1. Fix an abort in the MLDS backend involving switches on uints.

2. Make string.string/1 handle uints.

compiler/ml_switch_gen.m:
     Handle uints properly in a spot.

library/string.to_string.m:
      Make string.string/1 handle uint values.

tests/hard_coded/Mmakefile:
tests/hard_coded/uint_switch_test.{m,exp}:
      Add a test case.

Julien.

diff --git a/compiler/ml_switch_gen.m b/compiler/ml_switch_gen.m
index 381aad679..2df7c2223 100644
--- a/compiler/ml_switch_gen.m
+++ b/compiler/ml_switch_gen.m
@@ -601,6 +601,9 @@ ml_tagged_cons_id_to_match_cond(MLDS_Type, TaggedConsId, MatchCond) :-
              Rval = ml_const(mlconst_enum(Int, MLDS_Type))
          )
      ;
+        Tag = uint_tag(UInt),
+        Rval = ml_const(mlconst_uint(UInt))
+    ;
          Tag = string_tag(String),
          Rval = ml_const(mlconst_string(String))
      ;
@@ -608,7 +611,6 @@ ml_tagged_cons_id_to_match_cond(MLDS_Type, TaggedConsId, MatchCond) :-
          Rval = ml_const(mlconst_foreign(ForeignLang, ForeignTag, MLDS_Type))
      ;
          ( Tag = float_tag(_)
-        ; Tag = uint_tag(_)
          ; Tag = closure_tag(_, _, _)
          ; Tag = type_ctor_info_tag(_, _, _)
          ; Tag = base_typeclass_info_tag(_, _, _)
diff --git a/library/string.to_string.m b/library/string.to_string.m
index b1179e40b..0a9d0f731 100644
--- a/library/string.to_string.m
+++ b/library/string.to_string.m
@@ -104,7 +104,7 @@ value_to_revstrings(NonCanon, OpsTable, X, !Rs) :-

  value_to_revstrings_prio(NonCanon, OpsTable, Priority, X, !Rs) :-
      % We need to special-case the builtin types:
-    %   int, char, float, string
+    %   int, uint, char, float, string
      %   type_info, univ, c_pointer, array
      %   and private_builtin.type_info

@@ -114,6 +114,8 @@ value_to_revstrings_prio(NonCanon, OpsTable, Priority, X, !Rs) :-
          add_revstring(term_io.quoted_char(Char), !Rs)
      else if dynamic_cast(X, Int) then
          add_revstring(string.int_to_string(Int), !Rs)
+    else if dynamic_cast(X, UInt) then
+        add_revstring(string.uint_to_string(UInt) ++ "u", !Rs)
      else if dynamic_cast(X, Float) then
          add_revstring(string.float_to_string(Float), !Rs)
      else if dynamic_cast(X, Bitmap) then
diff --git a/tests/hard_coded/Mmakefile b/tests/hard_coded/Mmakefile
index a63d8e81d..7bffd236d 100644
--- a/tests/hard_coded/Mmakefile
+++ b/tests/hard_coded/Mmakefile
@@ -356,6 +356,7 @@ ORDINARY_PROGS =	\
  	type_to_term \
  	type_to_term_bug \
  	uc_export_enum \
+ 	uint_switch_test \
  	unicode_test \
  	unify_existq_cons \
  	unify_expression \
diff --git a/tests/hard_coded/uint_switch_test.exp b/tests/hard_coded/uint_switch_test.exp
index e69de29bb..a8d5d39a6 100644
--- a/tests/hard_coded/uint_switch_test.exp
+++ b/tests/hard_coded/uint_switch_test.exp
@@ -0,0 +1,9 @@
+foo(0u, _) ==> <<FALSE>>
+foo(1u, "one")
+foo(2u, _) ==> <<FALSE>>
+foo(3u, "three")
+foo(4u, _) ==> <<FALSE>>
+foo(5u, "five")
+foo(6u, _) ==> <<FALSE>>
+foo(7u, "seven")
+foo(8u, _) ==> <<FALSE>>
diff --git a/tests/hard_coded/uint_switch_test.m b/tests/hard_coded/uint_switch_test.m
index e69de29bb..662f034cd 100644
--- a/tests/hard_coded/uint_switch_test.m
+++ b/tests/hard_coded/uint_switch_test.m
@@ -0,0 +1,41 @@
+%---------------------------------------------------------------------------%
+% vim: ft=mercury ts=4 sw=4 et
+%---------------------------------------------------------------------------%
+
+:- module uint_switch_test.
+:- interface.
+
+:- import_module io.
+
+:- pred main(io::di, io::uo) is det.
+
+:- implementation.
+
+:- import_module list.
+:- import_module string.
+
+main(!IO) :-
+    test_foo_mode_0([0u, 1u, 2u, 3u, 4u, 5u, 6u, 7u, 8u], !IO).
+
+:- pred foo(uint, string).
+:- mode foo(in, out) is semidet.
+
+foo(1u, "one").
+foo(3u, "three").
+foo(5u, "five").
+foo(7u, "seven").
+
+:- pred test_foo_mode_0(list(uint)::in, io::di, io::uo) is det.
+
+test_foo_mode_0(Values, !IO) :-
+    (
+        Values = []
+    ;
+        Values = [Value | ValuesPrime],
+        ( if foo(Value, Result) then
+            io.format("foo(%s, %s)\n", [s(string(Value)), s(string(Result))], !IO)
+        else
+            io.format("foo(%s, _) ==> <<FALSE>>\n", [s(string(Value))], !IO)
+        ),
+        test_foo_mode_0(ValuesPrime, !IO)
+    ).


More information about the reviews mailing list