[m-rev.] for review: Document where the target char value range is used.

Peter Wang novalazy at gmail.com
Wed Mar 25 11:30:22 AEDT 2015


Document where the target char value range is used.

Explicitly use the target's char value range in the appropriate places
instead of the host's char value range.  The range of char values is the
same for all existing targets, though, so this change mainly serves as
documentation.

compiler/string_encoding.m:
	Add `target_char_range' predicate.

compiler/switch_util.m:
compiler/type_util.m:
	Use the target char value range where appropriate.

	Delete XXXs.

diff --git a/compiler/string_encoding.m b/compiler/string_encoding.m
index ac78a46..d20c2c6 100644
--- a/compiler/string_encoding.m
+++ b/compiler/string_encoding.m
@@ -18,6 +18,9 @@
     --->    utf8
     ;       utf16.
 
+:- pred target_char_range(compilation_target, int, int).
+:- mode target_char_range(in, out, out) is det.
+
 :- func target_string_encoding(compilation_target) = string_encoding.
 
 :- pred to_code_unit_list(string_encoding::in, string::in, list(int)::out)
@@ -33,6 +36,11 @@
 
 :- import_module string.
 
+target_char_range(_Target, Min, Max) :-
+    % The range of `char' is the same for all existing targets.
+    Min = 0,
+    Max = 0x10ffff.
+
 target_string_encoding(Target) = Encoding :-
     (
         ( Target = target_c
diff --git a/compiler/switch_util.m b/compiler/switch_util.m
index 8f6d749..d080931 100644
--- a/compiler/switch_util.m
+++ b/compiler/switch_util.m
@@ -367,6 +367,7 @@
 
 :- implementation.
 
+:- import_module backend_libs.string_encoding.
 :- import_module check_hlds.
 :- import_module check_hlds.type_util.
 :- import_module hlds.hlds_data.
@@ -664,13 +665,11 @@ is_smart_indexing_allowed_for_category(Globals, SwitchCategory) = Allowed :-
 type_range(ModuleInfo, TypeCtorCat, Type, Min, Max, NumValues) :-
     (
         TypeCtorCat = ctor_cat_builtin(cat_builtin_char),
-        % XXX The following code uses the host's character size, not the
-        % target's, so it won't work if cross-compiling to a machine with
-        % a different character size. Note also that some code in both
-        % dense_switch.m and in lookup_switch.m assumes that
-        % char.min_char_value is 0.
-        char.min_char_value(Min),
-        char.max_char_value(Max)
+        % Note also that some code in both dense_switch.m and in
+        % lookup_switch.m assumes that min_char_value is 0.
+        module_info_get_globals(ModuleInfo, Globals),
+        globals.get_target(Globals, Target),
+        target_char_range(Target, Min, Max)
     ;
         TypeCtorCat = ctor_cat_enum(cat_enum_mercury),
         Min = 0,
diff --git a/compiler/type_util.m b/compiler/type_util.m
index c497d47..61cf47e 100644
--- a/compiler/type_util.m
+++ b/compiler/type_util.m
@@ -364,6 +364,7 @@
 
 :- import_module backend_libs.
 :- import_module backend_libs.foreign.
+:- import_module backend_libs.string_encoding.
 :- import_module libs.
 :- import_module libs.globals.
 :- import_module libs.options.
@@ -980,11 +981,9 @@ substitute_type_args_3(Subst, [Arg0 | Args0], [Arg | Args]) :-
 switch_type_num_functors(ModuleInfo, Type, NumFunctors) :-
     type_to_ctor(Type, TypeCtor),
     ( if TypeCtor = type_ctor(unqualified("character"), 0) then
-        % XXX The following code uses the source machine's character size,
-        % not the target's, so it won't work if cross-compiling to a machine
-        % with a different size character.
-        char.max_char_value(MaxChar),
-        char.min_char_value(MinChar),
+        module_info_get_globals(ModuleInfo, Globals),
+        globals.get_target(Globals, Target),
+        target_char_range(Target, MinChar, MaxChar),
         NumFunctors = MaxChar - MinChar + 1
     else if type_ctor_is_tuple(TypeCtor) then
         NumFunctors = 1
-- 
2.1.2




More information about the reviews mailing list