[m-rev.] diff: mlds static constants for java

Peter Wang novalazy at gmail.com
Wed Sep 23 18:00:43 AEST 2009


Branches: main

Restore the Java backend, after the --from-ground-term-threshold was dropped
back down, enabling the MLDS static data implementation.

compiler/ml_unify_gen.m:
        Add casts for integer rvals inside ground term initialisers.

        For high-level data, don't add a remote tag as an extra initialiser
        argument for ground terms.

compiler/mlds_to_java.m:
        When an integer is cast to an enumeration type, output a reference to a
        static class instance.

        Output initialisers when the class constructor has no arguments.
        I'm not sure why this was ever otherwise.

compiler/ml_code_util.m:
        Add an XXX to investigate later.

diff --git a/compiler/ml_code_util.m b/compiler/ml_code_util.m
index a14b531..cacef0a 100644
--- a/compiler/ml_code_util.m
+++ b/compiler/ml_code_util.m
@@ -2853,6 +2853,7 @@ ml_gen_box_const_rval(ModuleInfo, Context, Type, Rval, BoxedRval,
         % [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
diff --git a/compiler/ml_unify_gen.m b/compiler/ml_unify_gen.m
index c5cb694..425e79c 100644
--- a/compiler/ml_unify_gen.m
+++ b/compiler/ml_unify_gen.m
@@ -2047,10 +2047,12 @@ ml_gen_ground_term_conjunct_tag(ModuleInfo, Target, HighLevelData, VarTypes,
         % Constants.
         (
             ConsTag = int_tag(Int),
-            % See the comment in ml_gen_constant.
+            % We need explicit casts for enumerations so that the Java backend
+            % knows to output an enumeration constant instead of a plain int.
+            % See also the comment in ml_gen_constant.
             IntRval = ml_const(mlconst_int(Int)),
-            ( VarType = char_type ->
-                ConstRval = ml_unop(cast(mlds_native_char_type), IntRval)
+            ( VarType \= int_type ->
+                ConstRval = ml_unop(cast(MLDS_Type), IntRval)
             ;
                 ConstRval = IntRval
             )
@@ -2129,15 +2131,16 @@ ml_gen_ground_term_conjunct_tag(ModuleInfo, Target, HighLevelData, VarTypes,
             ExtraInitializers = []
         ;
             ConsTag = shared_remote_tag(Ptag, Stag),
-            StagRval0 = ml_const(mlconst_int(Stag)),
             (
                 HighLevelData = no,
-                StagRval = ml_unop(box(mlds_native_char_type), StagRval0)
+                StagRval0 = ml_const(mlconst_int(Stag)),
+                % XXX why is this cast here?
+                StagRval = ml_unop(box(mlds_native_char_type), StagRval0),
+                ExtraInitializers = [init_obj(StagRval)]
             ;
                 HighLevelData = yes,
-                StagRval = StagRval0
-            ),
-            ExtraInitializers = [init_obj(StagRval)]
+                ExtraInitializers = []
+            )
         ),
         ml_gen_ground_term_conjunct_compound(ModuleInfo, Target, HighLevelData,
             VarTypes, Var, VarType, MLDS_Type,
diff --git a/compiler/mlds_to_java.m b/compiler/mlds_to_java.m
index 6c2ecc9..787326a 100644
--- a/compiler/mlds_to_java.m
+++ b/compiler/mlds_to_java.m
@@ -2225,8 +2225,7 @@ output_initializer(ModuleInfo, ModuleName, OutputAux, Type, Initializer, !IO) :-
 
 needs_initialization(no_initializer) = no.
 needs_initialization(init_obj(_)) = yes.
-needs_initialization(init_struct(_Type, [])) = no.
-needs_initialization(init_struct(_Type, [_ | _])) = yes.
+needs_initialization(init_struct(_, _)) = yes.
 needs_initialization(init_array(_)) = yes.
 
 :- pred output_initializer_alloc_only(module_info::in, mlds_initializer::in,
@@ -4031,6 +4030,22 @@ output_rval(ModuleInfo, Rval, ModuleName, !IO) :-
 output_unop(ModuleInfo, Unop, Expr, ModuleName, !IO) :-
     (
         Unop = cast(Type),
+        output_cast_rval(ModuleInfo, Type, Expr, ModuleName, !IO)
+    ;
+        Unop = box(Type),
+        output_boxed_rval(ModuleInfo, Type, Expr, ModuleName, !IO)
+    ;
+        Unop = unbox(Type),
+        output_unboxed_rval(ModuleInfo, Type, Expr, ModuleName, !IO)
+    ;
+        Unop = std_unop(StdUnop),
+        output_std_unop(ModuleInfo, StdUnop, Expr, ModuleName, !IO)
+    ).
+
+:- pred output_cast_rval(module_info::in, mlds_type::in, mlds_rval::in,
+    mlds_module_name::in, io::di, io::uo) is det.
+
+output_cast_rval(ModuleInfo, Type, Expr, ModuleName, !IO) :-
         % rtti_to_mlds.m generates casts from int to
         % jmercury.runtime.PseudoTypeInfo, but for Java
         % we need to treat these as constructions, not casts.
@@ -4063,17 +4078,22 @@ output_unop(ModuleInfo, Unop, Expr, ModuleName, !IO) :-
             output_rval(ModuleInfo, Expr, ModuleName, !IO),
             io.write_string(")", !IO)
         ;
-            output_cast_rval(ModuleInfo, Type, Expr, ModuleName, !IO)
-        )
-    ;
-        Unop = box(Type),
-        output_boxed_rval(ModuleInfo, Type, Expr, ModuleName, !IO)
+        type_is_object(Type),
+        Expr = ml_const(mlconst_int(N))
+    ->
+        % If it is a enumeration object make a reference to a static instance.
+        output_type(normal_style, Type, !IO),
+        io.write_string(".K", !IO),
+        io.write_int(N, !IO)
     ;
-        Unop = unbox(Type),
-        output_unboxed_rval(ModuleInfo, Type, Expr, ModuleName, !IO)
+        io.write_string("(", !IO),
+        output_type(normal_style, Type, !IO),
+        io.write_string(") ", !IO),
+        ( java_builtin_type(Type, "int", _, _) ->
+            output_rval_maybe_with_enum(ModuleInfo, Expr, ModuleName, !IO)
     ;
-        Unop = std_unop(StdUnop),
-        output_std_unop(ModuleInfo, StdUnop, Expr, ModuleName, !IO)
+            output_rval(ModuleInfo, Expr, ModuleName, !IO)
+        )
     ).
 
 :- pred have_preallocated_pseudo_type_var(int::in) is semidet.
@@ -4083,19 +4103,6 @@ have_preallocated_pseudo_type_var(N) :-
     N >= 1,
     N =< 5.
 
-:- pred output_cast_rval(module_info::in, mlds_type::in, mlds_rval::in,
-    mlds_module_name::in, io::di, io::uo) is det.
-
-output_cast_rval(ModuleInfo, Type, Expr, ModuleName, !IO) :-
-    io.write_string("(", !IO),
-    output_type(normal_style, Type, !IO),
-    io.write_string(") ", !IO),
-    ( java_builtin_type(Type, "int", _, _) ->
-        output_rval_maybe_with_enum(ModuleInfo, Expr, ModuleName, !IO)
-    ;
-        output_rval(ModuleInfo, Expr, ModuleName, !IO)
-    ).
-
 :- pred output_boxed_rval(module_info::in, mlds_type::in, mlds_rval::in,
      mlds_module_name::in, io::di, io::uo) is det.
 

--------------------------------------------------------------------------
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