Hi all,

I'm submitting the following patch as an enhancement rather than a bug
fix.  Jikes already correctly follows the JLS by skipping code
generation for conditional compilation sections (that is, if/else blocks
where the expression is a constant that can be evaluated at compile
time, such as a reference to a static final boolean).

This patch lets the semantic pass skip those sections which will be
optimized away.  My agenda here, as noted in the comments, is to provide
a way to write code that uses JDK 1.2 APIs but does not rely on them, in
a similar way to the role of the preprocessor in traditional C
compilers.  Consider the following test code:

public class Test {
  public static final boolean USE_JDK_1_1 = true;
  public static void main(String[] argv) {
    if (USE_JDK_1_1) {
      System.out.println("JDK 1.1 has no 'strict' modifier");
    } else {
      System.out.println("The value of the 'strict' modifier is: " +
java.lang.reflect.Modifier.STRICT);
    }
  }
}

Under javac and unpatched jikes, trying to compile this on a system
without JDK 1.2 installed exits with an error because
java.lang.reflect.Modifier contains no STRICT attribute, even though no
code referring to java.lang.reflect.Modifier would have been generated.

Looking over the JLS I don't see that this in any way breaks with the
spec, although it does differ from javac's behavior.  This code is run
after the parsing phase, so even code that will not be checked
semantically needs to be well-formed structurally.

I think this would be a very useful addition and can immediately see its
use in many open source projects I'm working with, especially GNU
Classpath, which is faced with the task of trying to support multiple
API versions from the same source tree for things like the JSDK.

The patch is against the 0.44 files from the posted cvsjikes_tar.gz.  I
noted that the jikes.tar.gz and jikes.zip files available from the web
site still contain 0.43.

Thanks,

Wes

--- body.cpp    Tue Feb 23 14:23:08 1999
+++ body.cpp.new        Tue Feb 23 14:23:02 1999
@@ -331,10 +331,29 @@
     // Recall that the parser ensures that the statements that appear
in an if-statement
     // (both the true and false statement) are enclosed in a block.
     //
+
+    // WWB (23 February 1999) --
+    // Notes on conditional compilation: We do not need to do a
semantic
+    // check on blocks of code bound by constant expressions (public
+    // static final variables), because these will not be written to
the
+    // .class file anyway (see ByteCode::EmitStatement() in
bytecode.cpp).
+    // This allows for avoiding compilation of code that uses APIs that

+    // may not be available, for instance by defining
+    //    public static final boolean compileWithJDK_1_2 = false;
+    // Note that jikes will still reject the code in the block that
+    // gets optimized away if it does not fit the grammar.
+
+    int conditional_compile = -1;  // disabled by default.
+    if ( if_statement -> expression -> IsConstant() ) {
+       conditional_compile = ( (IntLiteralValue *) if_statement ->
expression -> value ) -> value;
+    }
+
     if_statement -> true_statement -> is_reachable = if_statement ->
is_reachable;
-    ProcessBlock(if_statement -> true_statement);
+    if (conditional_compile != 0) {
+       ProcessBlock(if_statement -> true_statement);
+    }

-    if (if_statement -> false_statement_opt)
+    if ( (conditional_compile != 1) && (if_statement ->
false_statement_opt) )
     {
         if_statement -> false_statement_opt -> is_reachable =
if_statement -> is_reachable;
         ProcessBlock(if_statement -> false_statement_opt);



--
W E S   B I G G S
Senior Software Engineer
__________________________________
USWeb/CKS http://uswebcks.com/
2850 Ocean Park Blvd., Suite 100
Santa Monica, CA 90405
ph: +1 310 664 5335
mailto:[EMAIL PROTECTED]


Reply via email to