[m-rev.] for review: java packaging

Peter Wang novalazy at gmail.com
Fri May 22 12:52:21 AEST 2009


2009/5/21 Julien Fischer <juliensf at csse.unimelb.edu.au>:
>
> On Wed, 20 May 2009, Peter Wang wrote:
>
>> Branches: main
>>
>> Previously, unqualified Mercury modules would result in Java classes which
>> were placed in the default (unnamed) package.  This is a problem as Java
>> doesn't allow packaged classes to import types from the unpackaged
>> classes.
>> This patch gives all Java classes generated from Mercury files the
>> "mercury."
>> package prefix.
...
>
> That looks fine -- please ensure that mmc --make still works in hlc
> grades before committing though ;-)

The patch was incomplete.  I needed to add the "mercury." prefix in a few
more places.  Then I threw in a couple of changes to the shell script
generation.  Committed with the following relative diff.

For interest's sake: with a few more hacks sitting in my workspace, the
Mercury compiler can be compiled in the java grade and is capable of
compiling a few modules.  The performance is dominated by class loading and
JIT-ing.  By reusing a JVM instance so that effort is not wasted, ... the
short version is that the Mercury compiler seems to be currently ~30-40%
slower in the java grade than asm_fast.gc when compiling string.m.  The long
version is that it's not a scientific test using the same versions of the
compiler, with the same optimisation levels, etc.  But it shows it's worth
me pursuing this further ;-)


@@ -20,7 +20,13 @@


 compiler/java_names.m:
-        Add an outermost "mercury" qualifier on all module names.
+        Add a function to add an outermost "mercury" qualifier on module names.
+
+        Make `java_module_name' add the outermost "mercury" qualifier.
+
+compiler/mlds_to_java.m:
+        Add the outermost "mercury" qualifier wherever we are writing out
+        a package name.

 compiler/compile_target_code.m:
         Fix creation of `.jar' files, as described.
@@ -45,6 +51,10 @@
         `--use-grade-subdirs' is enabled, and when the main module is
         packaged (as it will be).

+        Make Java shell scripts get the path to the class directory from $0,
+        and make the path to the Java interpreter overridable by an environment
+        variable.
+
         Make `list_class_files_for_jar' scan the directory containing class
         files, to include nested classes.  Return a list of class files.

diff -u b/compiler/java_names.m b/compiler/java_names.m
--- b/compiler/java_names.m
+++ b/compiler/java_names.m
@@ -62,6 +62,11 @@
     %
 :- func java_module_name(module_name) = module_name.

+    % Return the given module name with an outermost "mercury" qualifier,
+    % if it is not already present.
+    %
+:- func enforce_outermost_mercury_qualifier(module_name) = module_name.
+
     % If the given name conficts with a reserved Java word we must add a
     % prefix to it to avoid compilation errors.
     %
@@ -180,13 +185,16 @@
     % Java doesn't allow packaged classes to import types from the default
     % unnamed package.  We don't do this earlier so as not to disturb other
     % MLDS backends.
+    QualModuleName = enforce_outermost_mercury_qualifier(ModuleName),
+    mangle_sym_name_for_java_2(QualModuleName, module_qual,
+        package_name_mangling, JavaModuleName).
+
+enforce_outermost_mercury_qualifier(ModuleName) = QualModuleName :-
     ( outermost_qualifier(ModuleName) = "mercury" ->
         QualModuleName = ModuleName
     ;
         QualModuleName = add_outermost_qualifier("mercury", ModuleName)
-    ),
-    mangle_sym_name_for_java_2(QualModuleName, module_qual,
-        package_name_mangling, JavaModuleName).
+    ).

 %-----------------------------------------------------------------------------%

diff -u b/compiler/module_cmds.m b/compiler/module_cmds.m
--- b/compiler/module_cmds.m
+++ b/compiler/module_cmds.m
@@ -679,7 +679,7 @@
     globals.io_lookup_accumulating_option(java_classpath, Java_Incl_Dirs0,
         !IO),
     % We prepend the .class files' directory and the current CLASSPATH.
-    Java_Incl_Dirs = [ClassDirName, "$CLASSPATH" | Java_Incl_Dirs0],
+    Java_Incl_Dirs = ["$DIR/" ++ ClassDirName, "$CLASSPATH" | Java_Incl_Dirs0],
     ClassPath = string.join_list(PathSeparator, Java_Incl_Dirs),

     globals.io_lookup_string_option(java_interpreter, Java, !IO),
@@ -693,9 +693,11 @@
         % XXX On Windows we should output a .bat file instead
         list.foldl(io.write_string(ShellScript), [
             "#!/bin/sh\n",
+            "DIR=${0%/*}\n",
             "CLASSPATH=", ClassPath, "\n",
             "export CLASSPATH\n",
-            "exec ", Java, " ", ClassName, " \"$@\"\n"
+            "JAVA=${JAVA:-", Java, "}\n",
+            "exec $JAVA ", ClassName, " \"$@\"\n"
         ], !IO),
         io.close_output(ShellScript, !IO),
         io.call_system("chmod a+x " ++ FileName, ChmodResult, !IO),
only in patch2:
unchanged:
--- a/compiler/mlds_to_java.m
+++ b/compiler/mlds_to_java.m
@@ -299,8 +299,9 @@ output_import(Import, !IO) :-
         unexpected(this_file, "foreign import in Java backend")
     ),
     SymName = mlds_module_name_to_sym_name(ImportName),
-    mangle_sym_name_for_java(SymName, module_qual, ".", package_name_mangling,
-        ClassFile),
+    PackageSymName = enforce_outermost_mercury_qualifier(SymName),
+    mangle_sym_name_for_java(PackageSymName, module_qual, ".",
+        package_name_mangling, ClassFile),
     % There are issues related to using import statements and Java's naming
     % conventions. To avoid these problems, we output dependencies as comments
     % only. This is ok, since we always use fully qualified names anyway.
@@ -318,6 +319,10 @@ output_java_src_file(ModuleInfo, Indent, MLDS, !IO) :-
     % Run further transformations on the MLDS.
     MLDS = mlds(ModuleName, AllForeignCode, Imports, Defns0,
         InitPreds, _FinalPreds, _ExportedEnums),
+
+    % Do NOT enforce the outermost "mercury" qualifier here.  This module
+    % name is compared with other module names in the MLDS, to avoid
+    % unnecessary module qualification.
     MLDS_ModuleName = mercury_module_name_to_mlds(ModuleName),

     % Find and build list of all methods which would have their addresses
@@ -1242,8 +1247,9 @@ output_interface(Interface, !IO) :-
             Arity, _)
     ->
         SymName = mlds_module_name_to_sym_name(ModuleQualifier),
-        mangle_sym_name_for_java(SymName, convert_qual_kind(QualKind), ".",
-            package_name_mangling, ModuleName),
+        PackageSymName = enforce_outermost_mercury_qualifier(SymName),
+        mangle_sym_name_for_java(PackageSymName, convert_qual_kind(QualKind),
+            ".", package_name_mangling, ModuleName),
         io.format("%s.%s", [s(ModuleName), s(Name)], !IO),
         %
         % Check if the interface is one of the ones in the runtime
@@ -1804,7 +1810,8 @@ output_fully_qualified_thing(qual(ModuleName,
QualKind, Name), OutputFunc,
     mlds_module_name_to_sym_name(ModuleName) = WholeModuleName,

     % Write the package name components.
-    mangle_sym_name_for_java(PackageName, module_qual, Qualifier,
+    QualPackageName = enforce_outermost_mercury_qualifier(PackageName),
+    mangle_sym_name_for_java(QualPackageName, module_qual, Qualifier,
         package_name_mangling, MangledPackageName),
     io.write_string(MangledPackageName, !IO),

@@ -3562,7 +3569,8 @@
mlds_output_proc_label(mlds_proc_label(PredLabel, ProcId), !IO) :-

 mlds_output_data_addr(data_addr(ModuleQualifier, DataName), !IO) :-
     SymName = mlds_module_name_to_sym_name(ModuleQualifier),
-    mangle_sym_name_for_java(SymName, module_qual, ".",
+    PackageSymName = enforce_outermost_mercury_qualifier(SymName),
+    mangle_sym_name_for_java(PackageSymName, module_qual, ".",
         package_name_mangling, ModuleName),
     io.write_string(ModuleName, !IO),
     io.write_string(".", !IO),
--------------------------------------------------------------------------
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