Unreachable statements - JVM

It is not allowed to drop o_ the end of a method body. A compile-time error occurs if the body of a method can complete normally. It is also a compiletime error if a statement cannot be executed because it is unreachable. Every Java compiler must carry out a conservative ow analysis to make sure that all statements are reachable. For this purpose two static predicates reachable and normal are computed at compile-time. The predicates have the following intended meanings:

The predicates are de_ned such that normal(_) implies reachable(_). Moreover, if a substatement is reachable, then the statement is reachable.

The reachability analysis is also used in in the proof that bytecode generated by the Java compiler of Part II is accepted by the bytecode veri_er. There, one has to show that execution cannot jump out of the code of a method.

At the root position of a method body the predicate reachable is determined by the following initial conditions: Let _block be the body of an instance method, constructor or class method or a static initializer. Then reachable(_) is True.

For the other positions in the block the predicates reachable and normal can then be computed in a top-down manner. Instead of explaining exactly how the functions are computed, in Table below we just state the equivalences the predicates have to satisfy. Constant boolean expressions are not treated the same way in all control structures. For example, in the while statement

Reachability constraints

Reachability constraints

the statement stm is not considered as reachable. In the if-then-else statement if (false) stm the statement stm, however, is considered as reachable. The reason for this di_erence is to allow programmers to use the if-then-else statement for conditional compilation".

For positions in expressions the predicate reachable is determined by the following de_nition.

De_nition (Reachable expression). An expression is reachable i_ the innermost statement containing it is reachable.

If an abruption occurs in a try block, the corresponding finally block is executed before the abruption moves upwards. If the finally block does not complete normally, the old abruption is discarded and cannot exit the try-finally statement.

De_nition(Abruption can exit statement). An abruption at position _ can exit stm, if for every substatement _(s finally _b) of stm such that _ is in s the predicate normal(_) is true.

We say that a break statement at position _ can exit stm, if an abruption at position _ can exit stm.

De_nition (Statement can throw exception). A statement stm can throw an exception E, if one of the following conditions is true:

  1. E = RuntimeException or E = Error, or
  2. stm contains a reachable statement _throws _exp such that T (_) = E, the exception E is not caught in stm and an abruption at position _ can exit stm, or
  3. stm contains a reachable method invocation _c=m(exps) such that E occurs in the throws clause of m in c, the exception E is not caught in stm and an abruption at position _ can exit stm.

At run-time the current position is always reachable. If a statement completes normally, then the statement is normal. The converse is not true. There are normal statements which can complete abruptly.

For example, in an if-then-else-statement one branch may be normal whereas the other branch completes abruptly.

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

JVM Topics