[m-rev.] for review: grade independent .mh files (bug #52)

Julien Fischer juliensf at csse.unimelb.edu.au
Thu Mar 27 15:25:44 AEDT 2008


For review by anyone.

Estimated hours taken: 2
Branches: main

Make the .mh files grade independent again.  They were not grade independent
because the types of the globals used to implement mutables can differ between
the high- and low-level C backends.  The fix is to output declarations for
both backends in .mh files and select between them using #ifdef MR_HIGHLEVEL_CODE.

compiler/make_hlds_passes.m:
 	Write out #ifdef protected declarations for the globals used to
 	implement mutables that are appropriate for both C backends.

 	Separate out the code for generating the mutable declarations and
 	definitions.  While there is some common structure the above change
 	makes it quite hard to follow if they are combined.  In this case
 	separating them is the lesser of two evils.

compiler/handle_options.m:
 	Add a comment mentioning that if the value of the
 	mutable_always_boxed option is changed then the code in
 	make_hlds_passes that processes mutable declarations will need to be
 	updated.

Julien.

Index: compiler/handle_options.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/handle_options.m,v
retrieving revision 1.317
diff -u -r1.317 handle_options.m
--- compiler/handle_options.m	23 Jan 2008 13:12:15 -0000	1.317
+++ compiler/handle_options.m	27 Mar 2008 04:14:03 -0000
@@ -1088,6 +1088,9 @@
          option_implies(single_prec_float, unboxed_float, bool(yes),
              !Globals),

+        % Changing this means that the code in make_hlds_passes.m that
+        % handles the declarations for the global variables used by
+        % mutables should also be updated.
          option_implies(highlevel_code, mutable_always_boxed, bool(no),
              !Globals),

Index: compiler/make_hlds_passes.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/make_hlds_passes.m,v
retrieving revision 1.77
diff -u -r1.77 make_hlds_passes.m
--- compiler/make_hlds_passes.m	15 Feb 2008 08:31:58 -0000	1.77
+++ compiler/make_hlds_passes.m	27 Mar 2008 04:14:03 -0000
@@ -1657,9 +1657,12 @@
      % We add the foreign code declaration and definition here rather than
      % in pass 2 because the target-language-specific type name depends on
      % whether there are any foreign_type declarations for Type.
-    get_c_mutable_global_foreign_decl_defn(!.ModuleInfo, Type,
+    get_c_mutable_global_foreign_decl(!.ModuleInfo, Type,
          TargetMutableName, IsConstant, IsThreadLocal, Context,
-        ForeignDecl, ForeignDefn),
+        ForeignDecl),
+    get_c_mutable_global_foreign_defn(!.ModuleInfo, Type,
+        TargetMutableName, IsConstant, IsThreadLocal, Context,
+        ForeignDefn),
      ItemStatus0 = item_status(status_local, may_be_unqualified),
      add_item_decl_pass_2(ForeignDecl, ItemStatus0, _, !ModuleInfo, !Specs),
      add_item_decl_pass_2(ForeignDefn, ItemStatus0, _, !ModuleInfo, !Specs).
@@ -1668,23 +1671,36 @@
      % The bool argument says whether the mutable is a constant mutable
      % or not.
      %
-:- pred get_c_mutable_global_foreign_decl_defn(module_info::in, mer_type::in,
+:- pred get_c_mutable_global_foreign_decl(module_info::in, mer_type::in,
      string::in, bool::in, mutable_thread_local::in, prog_context::in,
      item::out, item::out) is det.

-get_c_mutable_global_foreign_decl_defn(ModuleInfo, Type, TargetMutableName,
-        IsConstant, IsThreadLocal, Context, DeclItem, DefnItem) :-
+get_c_mutable_global_foreign_decl(ModuleInfo, Type, TargetMutableName,
+        IsConstant, IsThreadLocal, Context, DeclItem) :-
+    % This declaration will be included in the .mh files.  Since these are
+    % grade independent we need to output both the high- and low-level C
+    % declarations for the global used to implement the mutable and make
+    % the choice conditional on whether MR_HIGHLEVEL_CODE is defined.
+    %
      module_info_get_globals(ModuleInfo, Globals),
      globals.lookup_bool_option(Globals, mutable_always_boxed, AlwaysBoxed),
      (
          IsThreadLocal = mutable_not_thread_local,
-        TypeName = global_foreign_type_name(AlwaysBoxed, lang_c,
-            ModuleInfo, Type)
+        % The first argument in the following calls to
+        % global_foreign_type_name says whether the mutable should always be
+        % boxed or not.  The only difference between the high- and low-level
+        % C backends is that in the latter mutables are *always* boxed,
+        % whereas in the former they may not be.
+        HighLevelTypeName = global_foreign_type_name(no, lang_c, ModuleInfo,
+            Type),
+        LowLevelTypeName = global_foreign_type_name(yes, lang_c, ModuleInfo,
+            Type),
      ;
          IsThreadLocal = mutable_thread_local,
          % For thread-local mutables, the variable holds an index into an
          % array.
-        TypeName = "MR_Unsigned"
+        HighLevelTypeName = "MR_Unsigned",
+        LowLevelTypeName  = "MR_Unsigned",
      ),

      % Constant mutables do not require mutexes as their values are never
@@ -1695,29 +1711,67 @@
          )
      ->
          LockDecl = [],
-        LockDefn = []
      ;
          LockDecl = [
              "#ifdef MR_THREAD_SAFE\n",
              "    extern MercuryLock ",
              mutable_mutex_var_name(TargetMutableName), ";\n",
              "#endif\n"
-        ],
-        LockDefn = [
-            "#ifdef MR_THREAD_SAFE\n",
-            "    MercuryLock ",
-            mutable_mutex_var_name(TargetMutableName), ";\n",
-            "#endif\n"
          ]
      ),

      DeclBody = string.append_list([
-        "extern ", TypeName, " ", TargetMutableName, ";\n" | LockDecl]),
+        "#ifdef MR_HIGHLEVEL_CODE\n",
+        "    extern ", HighLevelTypeName, " ", TargetMutableName, ";\n",
+        "#else\n",
+        "    extern ", LowLevelTypeName, " ", TargetMutableName, ";\n",
+        "#endif\n" | LockDecl]),
+
      DeclPragma =
          pragma_foreign_decl(lang_c, foreign_decl_is_exported, DeclBody),
      DeclItemPragma = item_pragma_info(compiler(mutable_decl), DeclPragma,
          Context),
-    DeclItem = item_pragma(DeclItemPragma),
+    DeclItem = item_pragma(DeclItemPragma).
+ 
+    % Create the C foreign_defn for the mutable.
+    % The bool argument says whether the mutable is a constant mutable
+    % or not.
+    %
+:- pred get_c_mutable_global_foreign_defn(module_info::in, mer_type::in,
+    string::in, bool::in, mutable_thread_local::in, prog_context::in,
+    item::out, item::out) is det.
+
+get_c_mutable_global_foreign_defn(ModuleInfo, Type, TargetMutableName,
+        IsConstant, IsThreadLocal, Context, DefnItem) :-
+    module_info_get_globals(ModuleInfo, Globals),
+    globals.lookup_bool_option(Globals, mutable_always_boxed, AlwaysBoxed),
+    (
+        IsThreadLocal = mutable_not_thread_local,
+        TypeName = global_foreign_type_name(AlwaysBoxed, lang_c, ModuleInfo,
+            Type)
+    ;
+        IsThreadLocal = mutable_thread_local,
+        % For thread-local mutables, the variable holds an index into an
+        % array.
+        TypeName = "MR_Unsigned"
+    ),
+
+    % Constant mutables do not require mutexes as their values are never
+    % updated. Thread-local mutables do not require mutexes either.
+    (
+        ( IsConstant = yes
+        ; IsThreadLocal = mutable_thread_local
+        )
+    ->
+        LockDefn = []
+    ;
+        LockDefn = [
+            "#ifdef MR_THREAD_SAFE\n",
+            "    MercuryLock ",
+            mutable_mutex_var_name(TargetMutableName), ";\n",
+            "#endif\n"
+        ]
+    ),

      DefnBody = string.append_list([
          TypeName, " ", TargetMutableName, ";\n" | LockDefn]),

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