[m-rev.] diff: don't box floats/char fields in java

Peter Wang novalazy at gmail.com
Thu Sep 24 13:30:19 AEST 2009


Branches: main

Don't box float and char fields in Java classes.

compiler/ml_code_util.m:
        Make ml_must_box_field_type take into account the compilation target,
        and not succeed for Java.

        Make ml_gen_box_const_rval only treat floats specially for C and asm
        grades, as per the comment.

compiler/mlds_to_java.m:
        Use '\u0000' as the initialiser for character values.

tests/hard_coded/ee_dummy.m:
tests/hard_coded/foreign_name_mutable.m:
        Unrelated: make these tests compile again in the java grade.

diff --git a/compiler/ml_code_util.m b/compiler/ml_code_util.m
index cacef0a..bf00bd6 100644
--- a/compiler/ml_code_util.m
+++ b/compiler/ml_code_util.m
@@ -1588,12 +1588,25 @@ ml_gen_field_name(MaybeFieldName, ArgNum) = FieldName :-
     % not word-sized, because the code for `arg' etc. in std_util.m rely
     % on all arguments being word-sized.
     % XXX Currently we box such types even for the other MLDS based back-ends
-    % that don't need it, e.g. the .NET and Java back-ends. This routine should
-    % be modified to check the target.
+    % that don't need it, e.g. the .NET back-end.
     %
 ml_must_box_field_type(ModuleInfo, Type) :-
-    classify_type(ModuleInfo, Type) = Category,
-    ml_must_box_field_type_category(Category) = yes.
+    module_info_get_globals(ModuleInfo, Globals),
+    globals.get_target(Globals, Target),
+    (
+        ( Target = target_c
+        ; Target = target_il
+        ; Target = target_asm
+        ; Target = target_x86_64
+        ; Target = target_erlang
+        ),
+        classify_type(ModuleInfo, Type) = Category,
+        MustBox = ml_must_box_field_type_category(Category)
+    ;
+        Target = target_java,
+        MustBox = no
+    ),
+    MustBox = yes.
 
 :- func ml_must_box_field_type_category(type_ctor_category) = bool.
 
@@ -2849,14 +2862,14 @@ ml_gen_box_const_rval(ModuleInfo, Context, Type, Rval, BoxedRval,
         % specially, since boxed floats normally get heap allocated, whereas
         % for other types boxing is just a cast (casts are OK in static
         % initializers, but calls to malloc() are not).
-        %
-        % [For the .NET and Java back-ends, this code currently never gets
-        % called, since currently we don't support static ground term
-        % optimization for those back-ends.]
-        % XXX we now support static ground terms for Java
-
         ( Type = mercury_type(builtin_type(builtin_type_float), _, _)
         ; Type = mlds_native_float_type
+        ),
+        module_info_get_globals(ModuleInfo, Globals),
+        globals.get_target(Globals, Target),
+        ( Target = target_c
+        ; Target = target_asm
+        ; Target = target_x86_64
         )
     ->
         % Generate a local static constant for this float.
diff --git a/compiler/mlds_to_java.m b/compiler/mlds_to_java.m
index 787326a..bce3e23 100644
--- a/compiler/mlds_to_java.m
+++ b/compiler/mlds_to_java.m
@@ -2128,11 +2128,13 @@ get_java_type_initializer(Type) = Initializer :-
         Type = mercury_type(_, CtorCat, _),
         (
             ( CtorCat = ctor_cat_builtin(cat_builtin_int)
-            ; CtorCat = ctor_cat_builtin(cat_builtin_char)
             ; CtorCat = ctor_cat_builtin(cat_builtin_float)
             ),
             Initializer = "0"
         ;
+            CtorCat = ctor_cat_builtin(cat_builtin_char),
+            Initializer = "'\\u0000'"
+        ;
             ( CtorCat = ctor_cat_builtin(cat_builtin_string)
             ; CtorCat = ctor_cat_system(_)
             ; CtorCat = ctor_cat_higher_order
@@ -2148,10 +2150,12 @@ get_java_type_initializer(Type) = Initializer :-
     ;
         ( Type = mlds_native_int_type
         ; Type = mlds_native_float_type
-        ; Type = mlds_native_char_type
         ),
         Initializer = "0"
     ;
+        Type = mlds_native_char_type,
+        Initializer = "'\\u0000'"
+    ;
         Type = mlds_native_bool_type,
         Initializer = "false"
     ;
diff --git a/tests/hard_coded/ee_dummy.m b/tests/hard_coded/ee_dummy.m
index 818f3b3..0428ed2 100644
--- a/tests/hard_coded/ee_dummy.m
+++ b/tests/hard_coded/ee_dummy.m
@@ -52,7 +52,7 @@ main(!IO) :-
 	check_dummy_type(X::in, Result::out, IO0::di, IO::uo),
 	[will_not_call_mercury, promise_pure],
 "
-	Result = (X == FOO_dummy_type) ? bool.ML_YES : bool.ML_NO;
+	Result = (X == FOO_dummy_type) ? bool.YES : bool.NO;
 	IO = IO0;
 ").
 
@@ -69,6 +69,6 @@ main(!IO) :-
 	check_poly_dummy_type(X::in, Result::out, IO0::di, IO::uo),
 	[will_not_call_mercury, promise_pure],
 "
-	Result = (X == BAR_poly_dummy_type) ? bool.ML_YES : bool.ML_NO;
+	Result = (X == BAR_poly_dummy_type) ? bool.YES : bool.NO;
 	IO = IO0;
 ").
diff --git a/tests/hard_coded/foreign_name_mutable.m b/tests/hard_coded/foreign_name_mutable.m
index fa5c879..c066fff 100644
--- a/tests/hard_coded/foreign_name_mutable.m
+++ b/tests/hard_coded/foreign_name_mutable.m
@@ -37,6 +37,7 @@ main(!IO) :-
 	increment_global(IO0::di, IO::uo),
 	[will_not_call_mercury, promise_pure],
 "
-	FOO++;
+	int x = (Integer) FOO;
+	FOO = x + 1;
 	IO = IO0;
 ").

--------------------------------------------------------------------------
mercury-reviews mailing list
Post messages to:       mercury-reviews at csse.unimelb.edu.au
Administrative Queries: owner-mercury-reviews at csse.unimelb.edu.au
Subscriptions:          mercury-reviews-request at csse.unimelb.edu.au
--------------------------------------------------------------------------



More information about the reviews mailing list