[m-rev.] for post-commit review: fix bug #245 - crash in Java stdlib initialisation

Julien Fischer juliensf at csse.unimelb.edu.au
Tue Dec 13 02:55:39 AEDT 2011


For post-commit review by Peter Wang.

The alternative fix mentioned in ml_unify_gen.m is preferable to the one
below.  The reason I haven't used it is that I'm not sure how it would
affect the intervening HLDS->HLDS passes.  Since this bug is holding up
the 11.07 release, I intend to go with this one for now and look into
the alternative later.

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

Branches: main, 11.07

Fix bug #245: the Java backend was aborting during standard library
initialisation because the fix to bug #234 (compatibility with Java 1.7) was
incomplete -- in some cases, the arity of variable arity type_ctors was being
passed to the constructor of TypeInfo_Struct class.

compiler/ml_unify_gen.m:
 	When compiling to Java fixup the arguments type_info_cell_constructors
 	so that the arity of a variable arity type ctor is not included in the
 	MLDS arguments generated for the type_info_cell_constructors.  This
 	conforms to what the Java version of the runtime now expects.

 	Add a note about an alternative fix.

compiler/rtti_to_mlds.m:
 	Add pointer to the above since the code here needs to be kept
 	consistent with it.

 	For the Java backend, (some) pseudo type-infos also use the
 	TypeInfo_Struct class, so we need to omit the arity argument
 	in the corresponding calls to the constructor here too.

Julien.

Index: compiler/ml_unify_gen.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/ml_unify_gen.m,v
retrieving revision 1.163
diff -u -r1.163 ml_unify_gen.m
--- compiler/ml_unify_gen.m	6 Sep 2011 05:20:42 -0000	1.163
+++ compiler/ml_unify_gen.m	12 Dec 2011 15:41:49 -0000
@@ -664,7 +664,17 @@

  ml_gen_new_object_dynamically(MaybeConsId, MaybeCtorName, MaybeTag, ExplicitSecTag,
          _Var, VarLval, VarType, MLDS_Type, ExtraRvals, ExtraTypes,
-        ArgVars, ArgTypes, ArgModes, TakeAddr, Context, Statements, !Info) :-
+        ArgVars0, ArgTypes0, ArgModes0, TakeAddr, Context, Statements, !Info) :-
+
+    % Fixup type_info_cell_constructor argument lists for the Java backend.
+    % (See below for an explanation.)
+    maybe_fixup_type_info_cell_constructor_args(!.Info, MaybeConsId,
+        ArgVars0, ArgVars),
+    maybe_fixup_type_info_cell_constructor_args(!.Info, MaybeConsId,
+        ArgTypes0, ArgTypes),
+    maybe_fixup_type_info_cell_constructor_args(!.Info, MaybeConsId,
+        ArgModes0, ArgModes),
+
      % Find out the types of the constructor arguments and generate rvals
      % for them (boxing/unboxing if needed).
      ml_gen_var_list(!.Info, ArgVars, ArgLvals),
@@ -744,10 +754,18 @@

  ml_gen_new_object_statically(MaybeConsId, MaybeCtorName, MaybeTag,
          Var, VarLval, VarType, MLDS_Type, ExtraRvals, ExtraTypes,
-        ArgVars, ArgTypes, Context, Statements, !Info) :-
+        ArgVars0, ArgTypes0, Context, Statements, !Info) :-
      % Find out the types of the constructor arguments.
      ml_gen_info_get_module_info(!.Info, ModuleInfo),
      ml_gen_info_get_high_level_data(!.Info, HighLevelData),
+ 
+    % Fixup type_info_cell_constructor argument lists for the Java backend.
+    % (See below for an explanation.)
+    maybe_fixup_type_info_cell_constructor_args(!.Info, MaybeConsId,
+        ArgVars0, ArgVars),
+    maybe_fixup_type_info_cell_constructor_args(!.Info, MaybeConsId,
+        ArgTypes0, ArgTypes),
+
      get_maybe_cons_id_arg_types(ModuleInfo, MaybeConsId, ArgTypes, VarType,
          ConsArgTypes, ConsArgWidths),

@@ -836,6 +854,61 @@
      AssignStatement = ml_gen_assign(VarLval, Rval, Context),
      Statements = [AssignStatement].

+    % Fixup the arguments of type_info_cell_constructors to conform
+    % to what the Java version of the runtime expects.
+    %
+    % For the Java backend we need to treat type_info_cell_constructors with a
+    % variable arity type_ctor specially.  polymorphism.m generates these so
+    % that the second argument is an integer giving the arity of the type_ctor,
+    % but there is no corresponding constructor with an integer argument in
+    % the TypeInfo_Struct class in the Java version of the runtime.
+    % Having such a constructor would cause problems with restrictions on
+    % overloading in Java.  (In any case, it is unnecessary since the arity
+    % can be obtained from the length of the array holding the remaining
+    % arguments.)
+    %
+    % The job of this predicate is to remove the arity argument from the
+    % argument list of a type_info_cell_constructor if we are compiling to
+    % Java and we have a varaible arity type_ctor.
+    %
+    % NOTE: this code needs to kept consistent with:
+    %
+    %       compiler/rtti_to_mods.gen_type_info_defn/6
+    %       java/runtime/TypeInfo_Struct.java
+    %
+    % XXX it might be better to modify polymoprhism.m so that it never
+    % inserts the arity argument in the first place.
+    %
+:- pred maybe_fixup_type_info_cell_constructor_args(ml_gen_info::in,
+    maybe(cons_id)::in, list(T)::in, list(T)::out) is det.
+
+maybe_fixup_type_info_cell_constructor_args(Info, MaybeConsId, !Args) :-
+    ml_gen_info_get_target(Info, Target),
+    ( Target = target_java ->
+        ( 
+            MaybeConsId = no
+        ;
+            MaybeConsId = yes(ConsId),
+            (
+                ConsId = type_info_cell_constructor(TypeCtor),
+                ( type_ctor_is_higher_order(TypeCtor, _, _, _)
+                ; type_ctor_is_tuple(TypeCtor)
+                )
+            ->
+                ( !.Args = [TypeInfoCtorArg, _ArityArg | OtherArgs] ->
+                    !:Args = [TypeInfoCtorArg | OtherArgs]
+                ;
+                    unexpected($module, $pred,
+                        "misformed type_info_cell_constructor args")
+                )
+            ;
+                true
+            )
+        )
+    ;
+        true
+    ).
+
  :- pred ml_gen_new_object_reuse_cell(maybe(cons_id)::in, maybe(ctor_name)::in,
      mlds_tag::in, maybe(mlds_tag)::in, bool::in,
      prog_var::in, mlds_lval::in, mer_type::in, mlds_type::in,
Index: compiler/rtti_to_mlds.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/rtti_to_mlds.m,v
retrieving revision 1.105
diff -u -r1.105 rtti_to_mlds.m
--- compiler/rtti_to_mlds.m	30 Nov 2011 15:38:57 -0000	1.105
+++ compiler/rtti_to_mlds.m	12 Dec 2011 15:45:54 -0000
@@ -467,6 +467,14 @@
                  % TypeInfo_Struct class doesn't have a constructor that
                  % supports it -- see java/runtime/TypeInfo_Struct.java for
                  % details.
+                %
+                % NOTE: this needs to be kept consistent with
+                %
+                %     ml_unify_gen.maybe_fixup_type_info_cell_constructor_args/4
+                %     java/runtime/TypeInfo_Struct.java
+                %
+                % as well as the code for handling pseudo type-infos below.
+                %
                  InitializerArgs = [InitRttiName, InitCastRttiDatasArray]
              ;
                  InitializerArgs = [
@@ -542,13 +550,29 @@
                  !GlobalData),
              RttiTypeCtor = var_arity_id_to_rtti_type_ctor(VarArityId),
              module_info_get_name(ModuleInfo, ModuleName),
-            Initializer = init_struct(mlds_rtti_type(item_type(RttiId)), [
-                gen_init_rtti_name(ModuleName, RttiTypeCtor,
-                    type_ctor_type_ctor_info),
-                gen_init_int(list.length(ArgTypes)),
-                gen_init_cast_rtti_datas_array(mlds_pseudo_type_info_type,
-                    ModuleName, ArgRttiDatas)
-            ]),
+            module_info_get_globals(ModuleInfo, Globals),
+            globals.get_target(Globals, TargetLang),
+ 
+            InitRttiName = gen_init_rtti_name(ModuleName, RttiTypeCtor,
+                type_ctor_type_ctor_info),
+            InitCastRttiDatasArray = gen_init_cast_rtti_datas_array(
+                mlds_pseudo_type_info_type, ModuleName, ArgRttiDatas),
+            ( TargetLang = target_java ->
+                % For Java we need to omit the arity argument as the
+                % TypeInfo_Struct class doesn't have a constructor that
+                % supports it.  The TypeInfo_Struct class is used to
+                % represent pseudo type-infos with the Java backend.
+                % (See java/runtime/PseudoTypeInfo.java for details.)
+                InitializerArgs = [InitRttiName, InitCastRttiDatasArray]
+            ;
+                InitializerArgs = [
+                    InitRttiName,
+                    gen_init_int(list.length(ArgTypes)),
+                    InitCastRttiDatasArray
+                ]
+            ),
+            Initializer = init_struct(mlds_rtti_type(item_type(RttiId)), 
+                InitializerArgs),
              rtti_entity_name_and_init_to_defn(Name, RttiId, Initializer,
                  !GlobalData),


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