Compiling While
Assume the compilation of c will generate a sequence that will leave 0 or 1 on the stack, expressing the truth value of c:
[[ while(c) s ]] =
nStart: [[c]] if_eq nAfter [[s]] goto nStart nAfter: ...
Example
Java code:
class Test { static boolean condition(int n) { ... } static void work(int n) { ... } static void test() { int n = 100; while (condition(n)) { n = n - 11; work(n); } } }
Bytecodes for test():
static void test(); Code: 0: bipush 100 2: istore_0 3: iload_0 4: invokestatic #4; //Method condition:(I)Z 7: ifeq 22 10: iload_0 11: bipush 11 13: isub 14: istore_0 15: iload_0 16: invokestatic #5; //Method work:(I)V 19: goto 3 22: return
See also Compiled Counting Examples