[m-rev.] diff: runtime trace conditions on java

Peter Wang novalazy at gmail.com
Fri Jun 19 17:14:47 AEST 2009


Branches: main

Support environment variables in runtime trace conditions on Java.  For each
unique environment variable name referenced in the module, we create a private
class member to record whether that environment variable was defined when the
class is initialised.

compiler/mlds_to_java.m:
        As above.

compiler/simplify.m:
        Don't abort on seeing a runtime trace condition for target_java.

diff --git a/compiler/mlds_to_java.m b/compiler/mlds_to_java.m
index ffd7255..13f899f 100644
--- a/compiler/mlds_to_java.m
+++ b/compiler/mlds_to_java.m
@@ -364,6 +364,8 @@ output_java_src_file(ModuleInfo, Indent, MLDS, !IO) :-
     output_exported_enums(Indent + 1, ModuleInfo, ExportedEnums, !IO),
     io.write_string("\n// InitPreds\n", !IO),
     output_inits(Indent + 1, ModuleInfo, InitPreds, !IO),
+    io.write_string("\n// EnvVarNames\n", !IO),
+    output_env_vars(Indent + 1, NonRttiDefns, !IO),
     output_src_end(Indent, ModuleName, !IO).
     % XXX Need to handle non-Java foreign code at this point.

@@ -1032,6 +1034,53 @@ output_init_2(Indent, InitPred, !IO) :-

 %-----------------------------------------------------------------------------%
 %
+% Code to output globals for environment variables.
+%
+
+:- pred output_env_vars(indent::in, list(mlds_defn)::in, io::di, io::uo)
+    is det.
+
+output_env_vars(Indent, NonRttiDefns, !IO) :-
+    list.foldl(collect_env_var_names, NonRttiDefns, set.init, EnvVarNamesSet),
+    EnvVarNames = set.to_sorted_list(EnvVarNamesSet),
+    (
+        EnvVarNames = []
+    ;
+        EnvVarNames = [_ | _],
+        list.foldl(output_env_var_definition(Indent), EnvVarNames, !IO)
+    ).
+
+:- pred collect_env_var_names(mlds_defn::in,
+    set(string)::in, set(string)::out) is det.
+
+collect_env_var_names(Defn, !EnvVarNames) :-
+    Defn = mlds_defn(_, _, _, EntityDefn),
+    (
+        EntityDefn = mlds_data(_, _, _)
+    ;
+        EntityDefn = mlds_function(_, _, _, _, EnvVarNames),
+        set.union(EnvVarNames, !EnvVarNames)
+    ;
+        EntityDefn = mlds_class(_)
+    ).
+
+:- pred output_env_var_definition(indent::in, string::in, io::di, io::uo)
+    is det.
+
+output_env_var_definition(Indent, EnvVarName, !IO) :-
+    % We use int because the generated code compares against zero, and changing
+    % that is more trouble than it's worth as it affects the C backends.
+    indent_line(Indent, !IO),
+    io.write_string("private static int mercury_envvar_", !IO),
+    io.write_string(EnvVarName, !IO),
+    io.write_string(" =\n", !IO),
+    indent_line(Indent + 1, !IO),
+    io.write_string("java.lang.System.getenv(\"", !IO),
+    io.write_string(EnvVarName, !IO),
+    io.write_string("\") == null ? 0 : 1;\n", !IO).
+
+%-----------------------------------------------------------------------------%
+%
 % Code to output the start and end of a source file.
 %

@@ -1191,9 +1240,7 @@ output_defn_body(_, ModuleInfo, Name, OutputAux, _,
     output_data_defn(ModuleInfo, Name, OutputAux, Type, Initializer, !IO).
 output_defn_body(Indent, ModuleInfo, Name, OutputAux, Context,
         mlds_function(MaybePredProcId, Signature, MaybeBody,
-        _Attributes, EnvVarNames), !IO) :-
-    expect(set.empty(EnvVarNames), this_file,
-        "output_defn_body: EnvVarNames"),
+            _Attributes, _EnvVarNames), !IO) :-
     output_maybe(MaybePredProcId, output_pred_proc_id, !IO),
     output_func(Indent, ModuleInfo, Name, OutputAux, Context,
         Signature, MaybeBody, !IO).
@@ -3224,8 +3271,10 @@ output_lval(ModuleInfo, Lval, ModuleName, !IO) :-
         Lval = ml_mem_ref(Rval, _Type),
         output_bracketed_rval(ModuleInfo, Rval, ModuleName, !IO)
     ;
-        Lval = ml_global_var_ref(_),
-        sorry(this_file, "output_lval: global_var_ref NYI")
+        Lval = ml_global_var_ref(GlobalVarRef),
+        GlobalVarRef = env_var_ref(EnvVarName),
+        io.write_string("mercury_envvar_", !IO),
+        io.write_string(EnvVarName, !IO)
     ;
         Lval = ml_var(qual(ModName, QualKind, Name), _),
         QualName = qual(ModName, QualKind, entity_data(mlds_data_var(Name))),
diff --git a/compiler/simplify.m b/compiler/simplify.m
index 4376ce6..7c101fa 100644
--- a/compiler/simplify.m
+++ b/compiler/simplify.m
@@ -1946,13 +1946,15 @@ simplify_goal_trace_goal(MaybeCompiletimeExpr,
MaybeRuntimeExpr, SubGoal,
                 Target = target_erlang,
                 !:EvalAttributes = default_attributes(lang_erlang)
             ;
+                Target = target_java,
+                !:EvalAttributes = default_attributes(lang_java)
+            ;
                 ( Target = target_il
-                ; Target = target_java
                 ; Target = target_asm
                 ; Target = target_x86_64
                 ),
-                sorry(this_file, "NYI: runtime trace conditions "
-                    ++ "in languages other than C")
+                sorry(this_file,
+                    "runtime trace conditions for this target language")
             ),
             set_may_call_mercury(proc_will_not_call_mercury, !EvalAttributes),
             set_thread_safe(proc_thread_safe, !EvalAttributes),
--------------------------------------------------------------------------
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