keith471 / COMP520_peephole

Other
0 stars 0 forks source link

Boolean expressions #10

Closed keith471 closed 7 years ago

keith471 commented 7 years ago

Try an if statement with multiple logical ors, such as:

        if (x == 1 || x == 2 || x == 3 || x == 4) {
            // do something
        }

The resulting jasmin code is very repetitive:

  iload_2
  iconst_1
  if_icmpeq true_4

  iconst_0
  goto stop_5

  true_4:
  iconst_1

  stop_5:
  dup
  ifne true_3

  pop
  iload_2
  iconst_2
  if_icmpeq true_6

  iconst_0
  goto true_3

  true_6:
  iconst_1

  true_3:
  dup
  ifne true_2

  pop
  iload_2
  iconst_3
  if_icmpeq true_8

  iconst_0
  goto true_2

  true_8:
  iconst_1

  true_2:
  dup
  ifne true_1

  pop
  iload_2
  iconst_4
  if_icmpeq true_10

  goto stop_0

  true_10:
  iconst_1

  true_1:
  ifeq stop_0

  aload_1
  ldc "made it in"
  invokevirtual joos/lib/JoosIO/println(Ljava/lang/String;)V

  stop_0:

If the first boolean expression is true, then we should go straight to lable true_1. Instead, we take a roundabout way: true_4-> true_3 -> true_2 -> true_1. We should be able to eliminate these extraneous labels but I am not sure how...