Foundation in compiling

tradition loop

1
2
3
4
5
6
public void spin() {
int i;
for(i=0; i<10; i++) {
;
}
}

In vm there many frame at any point,
but only the operand stack in currrent frame is active.

javap -c Spin.class

1
2
3
4
5
6
7
8
9
10
public void spin();
Code:
0: iconst_0
1: istore_1
2: iload_1
3: bipush 10
5: if_icmpge 14
8: iinc 1, 1
11: goto 2
14: return
a b
[0] push int constant 0
[1] store into local variable 1 (i=0)
[2] push local variable 1 (-> i)
[3] push int constant 10
[5] break if great or equals (i>=10)
[8] increment local variable 1 by 1 (i++)
[11] loop
[14] return void when done

Instructions

  • iconst_<i> push an int constant to operand stack
  • bipush immediate push int constant to operand stack
  • istore_<i> pops an int from the operand stack and stores it in local variable i
  • iload_<i> pushes the value in local variable i on to the operand stack