Why is the following java code converted to jasmin like this?

118 views Asked by At

I have the following sample java class that I want to convert to jasmin:

public class JavaModel {
public int main() {
    int a = 0;
    int b = 5;
    if(a < b || a < b || a < b || a < b) {
        System.out.println("Hi");
    }
    return 0;
}
}

When I convert it to jasmin with:

javac src/test/java/JavaModel.java
javap -c src/test/java/JavaModel.class > src/test/java/JavaModelOutput

I get the following output:

Compiled from "JavaModel.java"
public class JavaModel {
public JavaModel();
Code:
   0: aload_0
   1: invokespecial #1                  // Method java/lang/Object."<init>":()V
   4: return

public int main();
Code:
   0: iconst_0
   1: istore_1
   2: iconst_5
   3: istore_2
   4: iload_1
   5: iload_2
   6: if_icmplt     24
   9: iload_1
  10: iload_2
  11: if_icmplt     24
  14: iload_1
  15: iload_2
  16: if_icmplt     24
  19: iload_1
  20: iload_2
  21: if_icmpge     32
  24: getstatic                       
  27: ldc                             
  29: invokevirtual                  
  32: iconst_0
  33: ireturn
  }

Why is line 21 converted like this? Shouldn't it be the same as 6,11,16?

2

There are 2 answers

3
Peter Cordes On

The last compare/branch in a chain of conditions has to skip over the if body if it shouldn't run. All the others in the chain of || short-circuit evaluation are jumping to the if body if it should run.

As Andy's answer points out, if() goto next_line is a NOP, going the same place whether the condition is true or false.

You could have the last if_cmpxx jump also be a conditional jump to the if body if you put the if body out-of-line, so the fall-through is into the return 0.

0
Andy Turner On
  21: if_icmpge     32

If this line were instead:

  21: if_icmplt     24

then it would jump to line 24 if the icmplt is true, but it would do the same if it were false. Effectively, it would be a no-op.