[m-rev.] diff: Add a builtin unsigned word sized integer type -- Part 1b
Julien Fischer
jfischer at opturion.com
Tue Oct 25 23:54:10 AEDT 2016
Add a builtin unsigned word sized integer type -- Part 1b.
compiler/builtin_ops.m:
Implement unchecked_quotient, unchecked_rem, /\, \/, xor and \
as builtin operations.
compiler/bytecode.m:
compiler/c_util.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 change.
compiler/ml_unify_gen.m:
Fix a bug in my previous change: we should use uint_eq to test for
equality of uints, not eq.
compiler/hlds_data.m:
Document uint_tag/1.
runtime/mercury_tabling_macros.h:
Address review comment from Peter.
runtime/mercury_tabling_preds.h:
Add tabling macros for uints that I missed the first time around.
diff --git a/compiler/builtin_ops.m b/compiler/builtin_ops.m
index 4183ad3..ee57c0f 100644
--- a/compiler/builtin_ops.m
+++ b/compiler/builtin_ops.m
@@ -37,6 +37,7 @@
; unmkbody
; bitwise_complement
; logical_not
+ ; uint_bitwise_complement
; hash_string
; hash_string2
; hash_string3
@@ -103,6 +104,12 @@
; uint_add
; uint_sub
; uint_mul
+ ; uint_div
+ ; uint_mod
+
+ ; uint_bitwise_and
+ ; uint_bitwise_or
+ ; uint_bitwise_xor
; float_plus % XXX the integer versions use different names.
; float_minus % E.g add instead of plus etc.
@@ -386,20 +393,23 @@ builtin_translation(ModuleName, PredName, ProcNum, Args, Code) :-
;
ModuleName = "uint",
(
- PredName = "+",
- Args = [X, Y, Z],
+ PredName = "\\",
ProcNum = 0,
- Code = assign(Z, binary(uint_add, leaf(X), leaf(Y)))
- ;
- PredName = "-",
- Args = [X, Y, Z],
+ 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 = "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
+ ),
ProcNum = 0,
- Code = assign(Z, binary(uint_sub, leaf(X), leaf(Y)))
- ;
- PredName = "*",
Args = [X, Y, Z],
- ProcNum = 0,
- Code = assign(Z, binary(uint_mul, leaf(X), leaf(Y)))
+ Code = assign(Z, binary(ArithOp, leaf(X), leaf(Y)))
;
( PredName = ">", CompareOp = uint_gt
; PredName = "<", CompareOp = uint_lt
diff --git a/compiler/bytecode.m b/compiler/bytecode.m
index 66c9166..21af01a 100644
--- a/compiler/bytecode.m
+++ b/compiler/bytecode.m
@@ -1085,6 +1085,11 @@ binop_code(uint_ge, 50).
binop_code(uint_add, 51).
binop_code(uint_sub, 52).
binop_code(uint_mul, 53).
+binop_code(uint_div, 54).
+binop_code(uint_mod, 55).
+binop_code(uint_bitwise_and, 56).
+binop_code(uint_bitwise_or, 57).
+binop_code(uint_bitwise_xor, 58).
:- pred binop_debug(binary_op::in, string::out) is det.
@@ -1142,6 +1147,11 @@ binop_debug(uint_ge, "uint_ge").
binop_debug(uint_add, "uint_add").
binop_debug(uint_sub, "uint_sub").
binop_debug(uint_mul, "uint_mul").
+binop_debug(uint_div, "uint_div").
+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").
:- pred unop_code(unary_op::in, int::out) is det.
@@ -1159,6 +1169,7 @@ unop_code(hash_string3, 10).
unop_code(hash_string4, 11).
unop_code(hash_string5, 12).
unop_code(hash_string6, 13).
+unop_code(uint_bitwise_complement, 14).
:- pred unop_debug(unary_op::in, string::out) is det.
@@ -1176,6 +1187,7 @@ unop_debug(hash_string3, "hash_string3").
unop_debug(hash_string4, "hash_string4").
unop_debug(hash_string5, "hash_string5").
unop_debug(hash_string6, "hash_string6").
+unop_debug(uint_bitwise_complement, "uint_bitwise_complement").
%---------------------------------------------------------------------------%
diff --git a/compiler/c_util.m b/compiler/c_util.m
index 8c6a66b..3d13d0e 100644
--- a/compiler/c_util.m
+++ b/compiler/c_util.m
@@ -218,7 +218,7 @@
; string_compare_binop
; unsigned_compare_binop
; uint_compare_binop
- ; uint_arith_binop
+ ; uint_binary_infix_binop
; float_compare_binop
; float_arith_binop
; int_or_bool_binary_infix_binop
@@ -792,6 +792,7 @@ unary_prefix_op(mkbody, "MR_mkbody").
unary_prefix_op(unmkbody, "MR_unmkbody").
unary_prefix_op(bitwise_complement, "~").
unary_prefix_op(logical_not, "!").
+unary_prefix_op(uint_bitwise_complement, "~").
unary_prefix_op(hash_string, "MR_hash_string").
unary_prefix_op(hash_string2, "MR_hash_string2").
unary_prefix_op(hash_string3, "MR_hash_string3").
@@ -827,9 +828,14 @@ binop_category_string(uint_ge, uint_compare_binop, ">=").
binop_category_string(uint_lt, uint_compare_binop, "<").
binop_category_string(uint_gt, uint_compare_binop, ">").
-binop_category_string(uint_add, uint_arith_binop, "+").
-binop_category_string(uint_sub, uint_arith_binop, "-").
-binop_category_string(uint_mul, uint_arith_binop, "*").
+binop_category_string(uint_add, uint_binary_infix_binop, "+").
+binop_category_string(uint_sub, uint_binary_infix_binop, "-").
+binop_category_string(uint_mul, uint_binary_infix_binop, "*").
+binop_category_string(uint_div, uint_binary_infix_binop, "/").
+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(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 b496945..4976df1 100644
--- a/compiler/erl_call_gen.m
+++ b/compiler/erl_call_gen.m
@@ -494,8 +494,9 @@ std_unop_to_elds(StdUnOp, EldsUnOp) :-
),
fail
;
- ( StdUnOp = bitwise_complement, EldsUnOp = elds.bnot
- ; StdUnOp = logical_not, EldsUnOp = elds.logical_not
+ ( StdUnOp = bitwise_complement, EldsUnOp = elds.bnot
+ ; StdUnOp = uint_bitwise_complement, EldsUnOp = elds.bnot
+ ; StdUnOp = logical_not, EldsUnOp = elds.logical_not
)
).
@@ -561,6 +562,11 @@ std_binop_to_elds(StdBinOp, EldsBinOp) :-
; StdBinOp = uint_add, EldsBinOp = elds.add
; StdBinOp = uint_sub, EldsBinOp = elds.sub
; StdBinOp = uint_mul, EldsBinOp = elds.mul
+ ; StdBinOp = uint_div, EldsBinOp = elds.int_div
+ ; StdBinOp = uint_mod, EldsBinOp = elds.(rem)
+ ; StdBinOp = uint_bitwise_and, EldsBinOp = elds.band
+ ; StdBinOp = uint_bitwise_or, EldsBinOp = elds.bor
+ ; StdBinOp = uint_bitwise_xor, EldsBinOp = elds.bxor
)
).
diff --git a/compiler/hlds_data.m b/compiler/hlds_data.m
index 7f2f0d7..ca760c1 100644
--- a/compiler/hlds_data.m
+++ b/compiler/hlds_data.m
@@ -597,7 +597,10 @@ cons_table_optimize(!ConsTable) :-
% the specified integer value. This is used for enumerations and
% character constants as well as for int constants.
- ; uint_tag(int)
+ ; uint_tag(int) % XXX UINT
+ % This means the constant is represented just as a word containing
+ % the specified unsigned integer value. This is used for uint
+ % constants.
; foreign_tag(foreign_language, string)
% This means the constant is represented by the string which is
diff --git a/compiler/llds.m b/compiler/llds.m
index 0a2c093..503c59a 100644
--- a/compiler/llds.m
+++ b/compiler/llds.m
@@ -1692,6 +1692,7 @@ unop_return_type(hash_string3, lt_integer).
unop_return_type(hash_string4, lt_integer).
unop_return_type(hash_string5, lt_integer).
unop_return_type(hash_string6, lt_integer).
+unop_return_type(uint_bitwise_complement, lt_unsigned).
unop_arg_type(mktag, lt_word).
unop_arg_type(tag, lt_word).
@@ -1707,6 +1708,7 @@ unop_arg_type(hash_string3, lt_string).
unop_arg_type(hash_string4, lt_string).
unop_arg_type(hash_string5, lt_string).
unop_arg_type(hash_string6, lt_string).
+unop_arg_type(uint_bitwise_complement, lt_unsigned).
binop_return_type(int_add, lt_integer).
binop_return_type(int_sub, lt_integer).
@@ -1762,6 +1764,11 @@ binop_return_type(uint_ge, lt_bool).
binop_return_type(uint_add, lt_unsigned).
binop_return_type(uint_sub, lt_unsigned).
binop_return_type(uint_mul, lt_unsigned).
+binop_return_type(uint_div, lt_unsigned).
+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).
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 2617aad..cc1d60b 100644
--- a/compiler/llds_out_data.m
+++ b/compiler/llds_out_data.m
@@ -1019,6 +1019,11 @@ output_rval(Info, Rval, !IO) :-
; Op = uint_add, OpStr = "+"
; Op = uint_sub, OpStr = "-"
; Op = uint_mul, OpStr = "*"
+ ; Op = uint_div, OpStr = "/"
+ ; Op = uint_mod, OpStr = "%"
+ ; Op = uint_bitwise_and, OpStr = "&"
+ ; Op = uint_bitwise_or, OpStr = "|"
+ ; Op = uint_bitwise_xor, 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 b511d95..af5ce4e 100644
--- a/compiler/ml_global_data.m
+++ b/compiler/ml_global_data.m
@@ -598,6 +598,11 @@ ml_specialize_generic_array_binop(Op, IsFloat) :-
; Op = uint_add
; Op = uint_sub
; Op = uint_mul
+ ; Op = uint_div
+ ; Op = uint_mod
+ ; Op = uint_bitwise_and
+ ; Op = uint_bitwise_or
+ ; Op = uint_bitwise_xor
; Op = float_eq
; Op = float_ne
; Op = float_lt
diff --git a/compiler/ml_unify_gen.m b/compiler/ml_unify_gen.m
index 359ed20..a5fe940 100644
--- a/compiler/ml_unify_gen.m
+++ b/compiler/ml_unify_gen.m
@@ -2220,7 +2220,7 @@ ml_gen_tag_test_rval(Tag, Type, ModuleInfo, Rval) = TagTestRval :-
TagTestRval = ml_binop(eq, Rval, ConstRval)
;
Tag = uint_tag(UInt),
- TagTestRval = ml_binop(eq, Rval, ml_const(mlconst_uint(UInt))) % XXX is that right?
+ TagTestRval = ml_binop(uint_eq, Rval, ml_const(mlconst_uint(UInt)))
;
Tag = foreign_tag(ForeignLang, ForeignVal),
MLDS_Type = mercury_type_to_mlds_type(ModuleInfo, Type),
diff --git a/compiler/mlds_to_c.m b/compiler/mlds_to_c.m
index 8e998db..903821e 100644
--- a/compiler/mlds_to_c.m
+++ b/compiler/mlds_to_c.m
@@ -4610,6 +4610,11 @@ mlds_output_binop(Opts, Op, X, Y, !IO) :-
; Op = uint_add, OpStr = "+"
; Op = uint_sub, OpStr = "-"
; Op = uint_mul, OpStr = "*"
+ ; Op = uint_div, OpStr = "/"
+ ; Op = uint_mod, OpStr = "%"
+ ; Op = uint_bitwise_and, OpStr = "&"
+ ; Op = uint_bitwise_or, OpStr = "|"
+ ; Op = uint_bitwise_xor, 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 f8844c4..bb09c3c 100644
--- a/compiler/mlds_to_cs.m
+++ b/compiler/mlds_to_cs.m
@@ -3609,6 +3609,7 @@ output_std_unop(Info, UnaryOp, Expr, !IO) :-
; UnaryOp = hash_string4, UnaryOpStr = "mercury.String.hash4_1_f_0"
; UnaryOp = hash_string5, UnaryOpStr = "mercury.String.hash5_1_f_0"
; UnaryOp = hash_string6, UnaryOpStr = "mercury.String.hash6_1_f_0"
+ ; UnaryOp = uint_bitwise_complement, UnaryOpStr = "~"
),
io.write_string(UnaryOpStr, !IO),
io.write_string("(", !IO),
@@ -3693,6 +3694,11 @@ output_binop(Info, Op, X, Y, !IO) :-
; Op = uint_add
; Op = uint_sub
; Op = uint_mul
+ ; Op = uint_div
+ ; Op = uint_mod
+ ; Op = uint_bitwise_and
+ ; Op = uint_bitwise_or
+ ; Op = uint_bitwise_xor
; Op = float_plus
; Op = float_minus
; Op = float_times
@@ -3750,6 +3756,11 @@ output_binary_op(Op, !IO) :-
; Op = uint_add, OpStr = "+"
; Op = uint_sub, OpStr = "-"
; Op = uint_mul, OpStr = "*"
+ ; Op = uint_div, OpStr = "/"
+ ; Op = uint_mod, OpStr = "%"
+ ; Op = uint_bitwise_and, OpStr = "&"
+ ; Op = uint_bitwise_or, OpStr = "|"
+ ; Op = uint_bitwise_xor, OpStr = "^"
; Op = float_eq, OpStr = "=="
; Op = float_ne, OpStr = "!="
diff --git a/compiler/mlds_to_java.m b/compiler/mlds_to_java.m
index e23805b..9b86e78 100644
--- a/compiler/mlds_to_java.m
+++ b/compiler/mlds_to_java.m
@@ -5017,6 +5017,7 @@ output_std_unop(Info, UnaryOp, Expr, !IO) :-
; UnaryOp = hash_string4, UnaryOpStr = "mercury.String.hash4_1_f_0"
; UnaryOp = hash_string5, UnaryOpStr = "mercury.String.hash5_1_f_0"
; UnaryOp = hash_string6, UnaryOpStr = "mercury.String.hash6_1_f_0"
+ ; UnaryOp = uint_bitwise_complement, UnaryOpStr = "~"
),
io.write_string(UnaryOpStr, !IO),
io.write_string("(", !IO),
@@ -5130,6 +5131,9 @@ output_binop(Info, Op, X, Y, !IO) :-
; Op = uint_add
; Op = uint_sub
; Op = uint_mul
+ ; Op = uint_bitwise_and
+ ; Op = uint_bitwise_or
+ ; Op = uint_bitwise_xor
),
io.write_string("(", !IO),
output_rval(Info, X, !IO),
@@ -5151,6 +5155,17 @@ output_binop(Info, Op, X, Y, !IO) :-
io.write_string(" ((", !IO),
output_rval(Info, Y, !IO),
io.write_string(") & 0xffffffffL))", !IO)
+ ;
+ ( Op = uint_div
+ ; Op = uint_mod
+ ),
+ io.write_string("((int)((", !IO),
+ output_rval(Info, X, !IO),
+ io.write_string(") & 0xffffffffL) ", !IO),
+ output_binary_op(Op, !IO),
+ io.write_string(" ((", !IO),
+ output_rval(Info, Y, !IO),
+ io.write_string(") & 0xffffffffL))", !IO)
).
% Output an Rval and if the Rval is an enumeration object append the string
@@ -5200,8 +5215,8 @@ output_binary_op(Op, !IO) :-
; Op = uint_eq, OpStr = "=="
; Op = uint_ne, OpStr = "!="
- % NOTE: unsigned comparisons require special handling
- % in Java. See output_binop/6 above.
+ % NOTE: unsigned comparisons require special handling in Java.
+ % See output_binop/6 above.
; Op = uint_lt, OpStr = "<"
; Op = uint_gt, OpStr = ">"
; Op = uint_le, OpStr = "<="
@@ -5211,6 +5226,15 @@ output_binary_op(Op, !IO) :-
; Op = uint_sub, OpStr = "-"
; Op = uint_mul, OpStr = "*"
+ % NOTE: unsigned div and mod require special handling in Java.
+ % See output_binop/6 above.
+ ; Op = uint_div, OpStr = "/"
+ ; Op = uint_mod, OpStr = "%"
+
+ ; Op = uint_bitwise_and, OpStr = "&"
+ ; Op = uint_bitwise_or, OpStr = "|"
+ ; Op = uint_bitwise_xor, OpStr = "^"
+
; Op = float_eq, OpStr = "=="
; Op = float_ne, OpStr = "!="
; Op = float_le, OpStr = "<="
diff --git a/compiler/opt_debug.m b/compiler/opt_debug.m
index af16259..efad811 100644
--- a/compiler/opt_debug.m
+++ b/compiler/opt_debug.m
@@ -842,6 +842,7 @@ dump_unop(hash_string3) = "hash_string3".
dump_unop(hash_string4) = "hash_string4".
dump_unop(hash_string5) = "hash_string5".
dump_unop(hash_string6) = "hash_string6".
+dump_unop(uint_bitwise_complement) = "uint_bitwise_complement".
dump_binop(array_index(_)) = "array_index".
dump_binop(string_unsafe_index_code_unit) = "string_unsafe_index_code_unit".
@@ -864,6 +865,11 @@ dump_binop(uint_ge) = "uint>=".
dump_binop(uint_add) = "uint+".
dump_binop(uint_sub) = "uint-".
dump_binop(uint_mul) = "uint*".
+dump_binop(uint_div) = "uint/".
+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(float_plus) = "fl+".
dump_binop(float_minus) = "fl-".
dump_binop(float_times) = "fl*".
diff --git a/runtime/mercury_tabling_macros.h b/runtime/mercury_tabling_macros.h
index f904391..41f9f21 100644
--- a/runtime/mercury_tabling_macros.h
+++ b/runtime/mercury_tabling_macros.h
@@ -231,8 +231,8 @@
(t) = MR_RAW_TABLE_UINT((t0), (value)); \
} \
if (MR_tabledebug) { \
- printf("TABLE %p: uint %ud => %p\n", \
- (t0), (long) (value), (t)); \
+ printf("TABLE %p: uint %u => %p\n", \
+ (t0), (unsigned long) (value), (t)); \
} \
} while (0)
diff --git a/runtime/mercury_tabling_preds.h b/runtime/mercury_tabling_preds.h
index 36044f0..5e8faea 100644
--- a/runtime/mercury_tabling_preds.h
+++ b/runtime/mercury_tabling_preds.h
@@ -18,6 +18,8 @@
#ifndef MR_ALLOW_RESET
#define MR_table_lookup_insert_int(a, b, c) \
MR_tbl_lookup_insert_int(NULL, MR_FALSE, MR_FALSE, a, b, c)
+#define MR_table_lookup_insert_uint(a, b, c) \
+ MR_tbl_lookup_insert_uint(NULL, MR_FALSE, MR_FALSE, a, b, c)
#define MR_table_lookup_insert_start_int(a, b, c, d) \
MR_tbl_lookup_insert_start_int(NULL, MR_FALSE, MR_FALSE, a, b, c, d)
#define MR_table_lookup_insert_char(a, b, c) \
@@ -45,6 +47,8 @@
#define MR_table_save_int_answer(a, b, c) \
MR_tbl_save_any_answer(MR_FALSE, a, b, c)
+#define MR_table_save_uint_answer(a, b, c) \
+ MR_tbl_save_any_answer(MR_FALSE, a, b, c)
#define MR_table_save_char_answer(a, b, c) \
MR_tbl_save_char_answer(MR_FALSE, a, b, c)
#define MR_table_save_string_answer(a, b, c) \
@@ -58,6 +62,8 @@
#define MR_table_restore_int_answer(a, b, c) \
MR_tbl_restore_any_answer(MR_FALSE, a, b, c)
+#define MR_table_restore_uint_answer(a, b, c) \
+ MR_tbl_restore_any_answer(MR_FALSE, a, b, c)
#define MR_table_restore_char_answer(a, b, c) \
MR_tbl_restore_char_answer(MR_FALSE, a, b, c)
#define MR_table_restore_string_answer(a, b, c) \
@@ -148,6 +154,11 @@
MR_TABLE_INT(stats, debug, back, T, T0, (MR_Integer) V); \
} while (0)
+#define MR_tbl_lookup_insert_uint(stats, debug, back, T0, V, T) \
+ do { \
+ MR_TABLE_UINT(stats, debug, back, T, T0, (MR_Unsigned) V); \
+ } while (0)
+
#define MR_tbl_lookup_insert_start_int(stats, debug, back, T0, S, V, T) \
do { \
MR_TABLE_START_INT(stats, debug, back, T, T0, \
@@ -221,6 +232,12 @@
&MR_TYPE_CTOR_INFO_NAME(builtin, int, 0)); \
} while (0)
+#define MR_tbl_save_uint_answer(debug, AB, Offset, V) \
+ do { \
+ MR_TABLE_SAVE_ANSWER(debug, AB, Offset, V, \
+ &MR_TYPE_CTOR_INFO_NAME(builtin, uint, 0)); \
+ } while (0)
+
#define MR_tbl_save_char_answer(debug, AB, Offset, V) \
do { \
MR_TABLE_SAVE_ANSWER(debug, AB, Offset, V, \
@@ -257,6 +274,11 @@
V = (MR_Integer) MR_TABLE_GET_ANSWER(debug, AB, Offset); \
} while (0)
+#define MR_tbl_restore_uint_answer(debug, AB, Offset, V) \
+ do { \
+ V = (MR_Unsigned) MR_TABLE_GET_ANSWER(debug, AB, Offset); \
+ } while (0)
+
#define MR_tbl_restore_char_answer(debug, AB, Offset, V) \
do { \
V = (MR_Char) MR_TABLE_GET_ANSWER(debug, AB, Offset); \
More information about the reviews
mailing list