[m-rev.] diff: add more builtin ops on uints (part 1)

Julien Fischer jfischer at opturion.com
Tue Apr 11 13:07:43 AEST 2017


The following will need to bootstrap before the new oprations can be
made available in the standard library -- I'll commit the second part
some time next week.  Also, there's been some discussion on of the
operand types and semantics of the shift operations -- the outcome of
those discussions won't affect the code below.

---------------------

Add more builtin ops on uints (part 1).

compiler/builtin_ops.m:
      Add unchecked left and right shifts for uints as well
      as the reverse modes of addition and subtraction.

library/uint.m:
      Add commented out mode declarations for addition and
      subtraction; they can be uncommented once the above
      has bootstrapped.

compiler/bytecode.m:
compiler/c_util.m:
compiler/erl_call_gen.m:
compiler/llds.m:
compiler/llds_out_data.m:
compiler/ml_global_data.m:
compiler/mlds_to_c.m:
compiler/mlds_to_cs.m:
compiler/mlds_to_java.m:
compiler/opt_debug.m:
      Conform to the above changes.

Julien.

diff --git a/compiler/builtin_ops.m b/compiler/builtin_ops.m
index 356f0b8..e19eee9 100644
--- a/compiler/builtin_ops.m
+++ b/compiler/builtin_ops.m
@@ -111,6 +111,9 @@
      ;       uint_bitwise_or
      ;       uint_bitwise_xor

+    ;       uint_unchecked_left_shift
+    ;       uint_unchecked_right_shift
+
      ;       float_plus      %  XXX the integer versions use different names.
      ;       float_minus     %  E.g add instead of plus etc.
      ;       float_times
@@ -397,15 +400,43 @@ builtin_translation(ModuleName, PredName, ProcNum, Args, Code) :-
              ProcNum = 0,
              Args = [X, Y],
              Code = assign(Y, unary(uint_bitwise_complement, leaf(X)))
-    ;
-            ( PredName = "+", ArithOp = uint_add
-            ; PredName = "-", ArithOp = uint_sub
-            ; PredName = "*", ArithOp = uint_mul
+        ;
+            PredName = "+",
+            Args = [X, Y, Z],
+            (
+                ProcNum = 0,
+                Code = assign(Z, binary(uint_add, leaf(X), leaf(Y)))
+            ;
+                ProcNum = 1,
+                Code = assign(X, binary(uint_sub, leaf(Z), leaf(Y)))
+            ;
+                ProcNum = 2,
+                Code = assign(Y, binary(uint_sub, leaf(Z), leaf(X)))
+            )
+        ;
+            PredName = "-",
+            Args = [X, Y, Z],
+            (
+                ProcNum = 0,
+                Code = assign(Z, binary(int_sub, leaf(X), leaf(Y)))
+            ;
+                ProcNum = 1,
+                Code = assign(X, binary(uint_add, leaf(Y), leaf(Z)))
+            ;
+                ProcNum = 2,
+                Code = assign(Y, binary(uint_sub, leaf(X), leaf(Z)))
+            )
+        ;
+            ( PredName = "*", ArithOp = uint_mul
              ; PredName = "unchecked_quotient", ArithOp = uint_div
              ; PredName = "unchecked_rem", ArithOp = uint_mod
              ; PredName = "/\\", ArithOp = uint_bitwise_and
              ; PredName = "\\/", ArithOp = uint_bitwise_or
              ; PredName = "xor", ArithOp = uint_bitwise_xor
+            ; PredName = "unchecked_left_shift",
+                ArithOp = uint_unchecked_left_shift
+            ; PredName = "unchecked_right_shift",
+                ArithOp = uint_unchecked_right_shift
              ),
              ProcNum = 0,
              Args = [X, Y, Z],
diff --git a/compiler/bytecode.m b/compiler/bytecode.m
index 21af01a..5dc8d79 100644
--- a/compiler/bytecode.m
+++ b/compiler/bytecode.m
@@ -1090,6 +1090,8 @@ binop_code(uint_mod,                55).
  binop_code(uint_bitwise_and,        56).
  binop_code(uint_bitwise_or,         57).
  binop_code(uint_bitwise_xor,        58).
+binop_code(uint_unchecked_left_shift, 59).
+binop_code(uint_unchecked_right_shift, 60).

  :- pred binop_debug(binary_op::in, string::out) is det.

@@ -1152,6 +1154,8 @@ binop_debug(uint_mod,               "uint_mod").
  binop_debug(uint_bitwise_and,       "uint_bitwise_and").
  binop_debug(uint_bitwise_or,        "uint_bitwise_or").
  binop_debug(uint_bitwise_xor,       "uint_bitwise_xor").
+binop_debug(uint_unchecked_left_shift, "uint_unchecked_left_shift").
+binop_debug(uint_unchecked_right_shift, "uint_unchecked_right_shift").

  :- pred unop_code(unary_op::in, int::out) is det.

diff --git a/compiler/c_util.m b/compiler/c_util.m
index 90fb6a1..4f25295 100644
--- a/compiler/c_util.m
+++ b/compiler/c_util.m
@@ -855,6 +855,8 @@ binop_category_string(uint_mod, uint_binary_infix_binop, "%").
  binop_category_string(uint_bitwise_and, uint_binary_infix_binop, "&").
  binop_category_string(uint_bitwise_or, uint_binary_infix_binop, "|").
  binop_category_string(uint_bitwise_xor, uint_binary_infix_binop, "^").
+binop_category_string(uint_unchecked_left_shift, uint_binary_infix_binop, "<<").
+binop_category_string(uint_unchecked_right_shift, uint_binary_infix_binop, ">>").

  binop_category_string(float_plus, float_arith_binop, "+").
  binop_category_string(float_minus, float_arith_binop, "-").
diff --git a/compiler/erl_call_gen.m b/compiler/erl_call_gen.m
index 194d515..2efea95 100644
--- a/compiler/erl_call_gen.m
+++ b/compiler/erl_call_gen.m
@@ -566,6 +566,8 @@ std_binop_to_elds(StdBinOp, EldsBinOp) :-
          ; StdBinOp = uint_bitwise_and,      EldsBinOp = elds.band
          ; StdBinOp = uint_bitwise_or,       EldsBinOp = elds.bor
          ; StdBinOp = uint_bitwise_xor,      EldsBinOp = elds.bxor
+        ; StdBinOp = uint_unchecked_left_shift, EldsBinOp = elds.bsl
+        ; StdBinOp = uint_unchecked_right_shift, EldsBinOp = elds.bsr
          )
      ).

diff --git a/compiler/llds.m b/compiler/llds.m
index a6366eb..02df86c 100644
--- a/compiler/llds.m
+++ b/compiler/llds.m
@@ -1769,6 +1769,8 @@ binop_return_type(uint_mod, lt_unsigned).
  binop_return_type(uint_bitwise_and, lt_unsigned).
  binop_return_type(uint_bitwise_or, lt_unsigned).
  binop_return_type(uint_bitwise_xor, lt_unsigned).
+binop_return_type(uint_unchecked_left_shift, lt_unsigned).
+binop_return_type(uint_unchecked_right_shift, lt_unsigned).

  register_type(reg_r, lt_word).
  register_type(reg_f, lt_float).
diff --git a/compiler/llds_out_data.m b/compiler/llds_out_data.m
index f214136..bbcab36 100644
--- a/compiler/llds_out_data.m
+++ b/compiler/llds_out_data.m
@@ -1024,6 +1024,8 @@ output_rval(Info, Rval, !IO) :-
              ; Op = uint_bitwise_and, OpStr = "&"
              ; Op = uint_bitwise_or, OpStr = "|"
              ; Op = uint_bitwise_xor, OpStr = "^"
+            ; Op = uint_unchecked_left_shift, OpStr = "<<"
+            ; Op = uint_unchecked_right_shift, OpStr = ">>"
              ),
              io.write_string("(", !IO),
              output_rval_as_type(Info, SubRvalA, lt_unsigned, !IO),
diff --git a/compiler/ml_global_data.m b/compiler/ml_global_data.m
index 0bd2825..3a2776b 100644
--- a/compiler/ml_global_data.m
+++ b/compiler/ml_global_data.m
@@ -606,6 +606,8 @@ ml_specialize_generic_array_binop(Op, IsFloat) :-
          ; Op = uint_bitwise_and
          ; Op = uint_bitwise_or
          ; Op = uint_bitwise_xor
+        ; Op = uint_unchecked_left_shift
+        ; Op = uint_unchecked_right_shift
          ; Op = float_eq
          ; Op = float_ne
          ; Op = float_lt
diff --git a/compiler/mlds_to_c.m b/compiler/mlds_to_c.m
index 2bebf57..070027e 100644
--- a/compiler/mlds_to_c.m
+++ b/compiler/mlds_to_c.m
@@ -4615,6 +4615,8 @@ mlds_output_binop(Opts, Op, X, Y, !IO) :-
          ; Op = uint_bitwise_and, OpStr = "&"
          ; Op = uint_bitwise_or, OpStr = "|"
          ; Op = uint_bitwise_xor, OpStr = "^"
+        ; Op = uint_unchecked_left_shift, OpStr = "<<"
+        ; Op = uint_unchecked_right_shift, OpStr = ">>"
          ),
          io.write_string("(", !IO),
          mlds_output_rval_as_op_arg(Opts, X, !IO),
diff --git a/compiler/mlds_to_cs.m b/compiler/mlds_to_cs.m
index 6ce2f7e..70b0aa0 100644
--- a/compiler/mlds_to_cs.m
+++ b/compiler/mlds_to_cs.m
@@ -3797,6 +3797,8 @@ output_binop(Info, Op, X, Y, !IO) :-
          ; Op = uint_bitwise_and
          ; Op = uint_bitwise_or
          ; Op = uint_bitwise_xor
+        ; Op = uint_unchecked_left_shift
+        ; Op = uint_unchecked_right_shift
          ; Op = float_plus
          ; Op = float_minus
          ; Op = float_times
@@ -3859,6 +3861,8 @@ output_binary_op(Op, !IO) :-
          ; Op = uint_bitwise_and, OpStr = "&"
          ; Op = uint_bitwise_or, OpStr = "|"
          ; Op = uint_bitwise_xor, OpStr = "^"
+        ; Op = uint_unchecked_left_shift, OpStr = "<<"
+        ; Op = uint_unchecked_right_shift, OpStr = ">>"

          ; Op = float_eq, OpStr = "=="
          ; Op = float_ne, OpStr = "!="
diff --git a/compiler/mlds_to_java.m b/compiler/mlds_to_java.m
index d915d0f..6c674b3 100644
--- a/compiler/mlds_to_java.m
+++ b/compiler/mlds_to_java.m
@@ -5205,6 +5205,8 @@ output_binop(Info, Op, X, Y, !IO) :-
          ; Op = uint_bitwise_and
          ; Op = uint_bitwise_or
          ; Op = uint_bitwise_xor
+        ; Op = uint_unchecked_left_shift
+        ; Op = uint_unchecked_right_shift
          ),
          io.write_string("(", !IO),
          output_rval(Info, X, !IO),
@@ -5305,6 +5307,8 @@ output_binary_op(Op, !IO) :-
          ; Op = uint_bitwise_and, OpStr = "&"
          ; Op = uint_bitwise_or, OpStr = "|"
          ; Op = uint_bitwise_xor, OpStr = "^"
+        ; Op = uint_unchecked_left_shift, OpStr = "<<"
+        ; Op = uint_unchecked_right_shift, OpStr = ">>>"

          ; Op = float_eq, OpStr = "=="
          ; Op = float_ne, OpStr = "!="
diff --git a/compiler/opt_debug.m b/compiler/opt_debug.m
index 1458e8a..40ee7ca 100644
--- a/compiler/opt_debug.m
+++ b/compiler/opt_debug.m
@@ -870,6 +870,8 @@ dump_binop(uint_mod) = "uint%".
  dump_binop(uint_bitwise_and) = "uint&".
  dump_binop(uint_bitwise_or) = "uint|".
  dump_binop(uint_bitwise_xor) = "uint^".
+dump_binop(uint_unchecked_left_shift) = "uint_unchecked<<".
+dump_binop(uint_unchecked_right_shift) = "uint_unchecked>>".
  dump_binop(float_plus) = "fl+".
  dump_binop(float_minus) = "fl-".
  dump_binop(float_times) = "fl*".
diff --git a/library/uint.m b/library/uint.m
index 9d79531..5bce93f 100644
--- a/library/uint.m
+++ b/library/uint.m
@@ -53,11 +53,17 @@

      % Addition.
      %
-:- func (uint::in) + (uint::in) = (uint::uo) is det.
+:- func uint + int = uint.
+:- mode in   + in  = uo is det.
+%:- mode uo   + in  = in is det.
+%:- mode in   + uo  = in is det.

      % Subtraction.
      %
-:- func (uint::in) - (uint::in) = (uint::uo) is det.
+:- func uint - uint = uint.
+:- mode in   - in   = uo is det.
+%:- mode uo   - in   = in is det.
+%:- mode in   - uo   = in is det.

      % Multiplication.
      %


More information about the reviews mailing list