Java Program:


class Foo {
  static void g (int b[]) {
    for (int i = 0; i < b.length; i++)
      b[i] = i;
  }

  static int f (int n) {
    int[] a = new int[n];
    g(a);
    int s = 0;
    for (int i = 0; i < a.length; i++)
      s += a[i] * a[i];
    return s;
  }

  public static void main (String args[]) {
    System.out.println (f(42));
  }
}

Byte Code:

Compiled from foo.java
class Foo extends java.lang.Object {
    Foo();
	/*   ()V   */
    static void g(int[]);
	/*   ([I)V   */
    static int f(int);
	/*   (I)I   */
    public static void main(java.lang.String[]);
	/*   ([Ljava/lang/String;)V   */
}

Method Foo()
   0 aload_0
   1 invokespecial #1 <Method java.lang.Object()>
   4 return

Method void g(int[])
   0 iconst_0
   1 istore_1
   2 goto 12
   5 aload_0
   6 iload_1
   7 iload_1
   8 iastore
   9 iinc 1 1
  12 iload_1
  13 aload_0
  14 arraylength
  15 if_icmplt 5
  18 return

Method int f(int)
   0 iload_0		; push n (local 0)
   1 newarray int	; allocate new array of length n
   3 astore_1		; pop as a (local 1)
   4 aload_1		; push a
   5 invokestatic #2 <Method void g(int[])> ; invoke g(a)
   8 iconst_0		; push 0
   9 istore_2		; pop as s (local 2)
  10 iconst_0		; push 0
  11 istore_3		; pop as i (local 3)
  12 goto 28		; jump to loop test
  15 iload_2		; push s
  16 aload_1		; push a (address)
  17 iload_3		; push i
  18 iaload		; tos = a[i]
  19 aload_1		; push a 
  20 iload_3		; push i
  21 iaload		; tos = a[i]
  22 imul		; tos = a[i] * a[i]
  23 iadd		; tos = s + a[i] * a[i]
  24 istore_2		; pop as s
  25 iinc 3 1		; i++
  28 iload_3		; push i
  29 aload_1		; push a
  30 arraylength	; tos = a.arraylength
  31 if_icmplt 15	; if i < a.arraylength goto 15
  34 iload_2		; push s
  35 ireturn		; return 

Method void main(java.lang.String[])
   0 getstatic #3 <Field java.io.PrintStream out>
   3 bipush 42
   5 invokestatic #4 <Method int f(int)>
   8 invokevirtual #5 <Method void println(int)>
  11 return

JIT Compiler Output (Kaffe JIT3)

callinfo = 0xbffff080
Method: Foo.main([Ljava/lang/String;)V
 L0:  1
c:	pushl ebp
c:	movl esp,ebp
c:	subl #?,esp
c:	pushl edi
c:	pushl esi
c:	pushl ebx
12:	movl 8(ebp),ebx
18:	movl 12(ebp),eax
1a:	cmpl esp,eax
20:	jugt +5
25:	call soft_stackoverflow
2b:	movl 0x8136dc0,ecx
2d:	pushl eax
32:	pushl #42
38:	movl ecx,-8(ebp)
3e:	call *81df0c8
44:	addl 8,esp
4a:	movl -8(ebp),ecx
4c:	movl (ecx),edx
52:	movl 124(edx),edx
58:	pushl 12(ebp)
5a:	pushl eax
5c:	pushl ecx
5e:	call edx
64:	addl 12,esp
6a:	popl ebx
6a:	popl esi
6a:	popl edi
6a:	movl ebp,esp
6a:	popl ebp
6b:	ret
Translating Foo.main([Ljava/lang/String;)V (static) 0x81b3848
callinfo = 0xbffff058
Method: Foo.f(I)I
 L0:  1 L1:  3 L2:  3 L3:  3
c:	pushl ebp		;  save old frame pointer
c:	movl esp,ebp		;  set frame pointer = stack pointer
c:	subl #?,esp		;  make space for locals on stack
c:	pushl edi		;  save callee-save registers
c:	pushl esi
c:	pushl ebx
12:	movl 12(ebp),eax	;  get stack limit (arg1)
14:	cmpl esp,eax		;  compare new stack against limit
1a:	jugt 25			;  jump if no overflow
1f:	call soft_stackoverflow ;  if overflow go handle it
25:	movl 8(ebp),ecx		;  load n (arg0)
27:	pushl ecx		;  push 
2c:	pushl #10		;  push array object identifier
31:	call allocate		;  allocate array object
37:	addl 8,esp		;  clean up stack
39:	movl eax,ebx		;  save a 
3f:	pushl 12(ebp)		;  push stack limit
41:	pushl ebx		;  push a 
47:	call *81df078		;  call g
4d:	addl 8,esp		;  clean up stack
4f:	xorl esi,esi		;  zero s
51:	xorl edi,edi		;  zero i
56:	jmpl 91			;  jump to loop test
5c:	cmpl 8(ebx),edi		;  compare i to a.length
62:	juge ?			;  if i < 0 or i >= a.length go raise exception 
69:	mov 12(ebx,edi,4),eax	;  load a[i]
6f:	cmpl 8(ebx),edi		;  compare i to a.length
75:	juge ?			;  if i < 0 or i >= a.length go raise exception 
7c:	mov 12(ebx,edi,4),ecx	;  load a[i]
7f:	imull ecx,eax		;  calculate a[i] * a[i]
81:	movl esi,ecx		;  move s to arithmetic register (!)
83:	addl eax,ecx		;  s = s + a[i] * a[i]
85:	movl ecx,esi		;  move new s back
8b:	addl #1,edi		;  i++
91:	movl 8(ebx),eax		;  get a.length 
93:	cmpl eax,edi		;  compare i with a.length
99:	jlt 5c			;  if i < a.length, jump back to top of loop
9b:	movl esi,eax		;  set return value to be s
a1:	popl ebx		;  restore callee-save registers
a1:	popl esi
a1:	popl edi
a1:	movl ebp,esp		;  reset old stack pointer
a1:	popl ebp		;  reset old frame pointer
a2:	ret			;  return
a7:	pushl <return_addr>
ac:	jmpl ?
b1:	pushl <return_addr>
b6:	jmpl ?
Translating Foo.f(I)I (static) 0x81c99f8
callinfo = 0xbffff020
Method: Foo.g([I)V
 L0:  3 L1:  3
c:	pushl ebp
c:	movl esp,ebp
c:	subl #?,esp
c:	pushl edi
c:	pushl esi
c:	pushl ebx
12:	movl 8(ebp),ebx
18:	movl 12(ebp),eax
1a:	cmpl esp,eax
20:	jugt +5
25:	call soft_stackoverflow
27:	xorl esi,esi
2c:	jmpl ?
32:	cmpl 8(ebx),esi
38:	juge ?
3f:	mov esi,12(ebx,esi,4)
45:	addl #1,esi
4b:	movl 8(ebx),eax
4d:	cmpl eax,esi
53:	jlt ?
59:	popl ebx
59:	popl esi
59:	popl edi
59:	movl ebp,esp
59:	popl ebp
5a:	ret
5f:	pushl <return_addr>
64:	jmpl ?
Translating Foo.g([I)V (static) 0x81a00c0

Possible Optimized Machine Code:

f:
	pushl %ebp		;  save frame pointer
	movl %esp,%ebp		;  set new frame pointer
	pushl %esi		;  save callee-save registers
	pushl %ebx		;  
	movl 8(%ebp),%esi	;  fetch n
	movl %esi,%eax		;  copy n
	sall $2,%eax		;  multiply by 4
	subl %eax,%esp		;  make space on stack for array object (no header)
	movl %esp,%ebx		;  save a
	movl %esp,%edx		;  copy a to b
	xorl %eax,%eax		;  zero i
	cmpl %esi,%eax		;  compare n to i
	jge .L14		;  skip loop if n < 0
.L11:
	movl %eax,12(%edx,%eax,4) ;  b[i] = i
	incl %eax		;  i++
	cmpl %esi,%eax		;  compare n, i
	jl .L11			;  jump to top of loop if i < n
.L14:
	xorl %ecx,%ecx		;  s = 0
	xorl %edx,%edx		;  i = 0
	cmpl %esi,%edx		;  compare n to i
	jge .L16		;  skip loop if n < 0
.L18:
	movl 12(%ebx,%edx,4),%eax ;  load a[i]
	imull %eax,%eax		;  a[i] * a[i]
	addl %eax,%ecx		;  add to s
	incl %edx		;  i++
	cmpl %esi,%edx		;  compare n to i
	jl .L18			;  jump to top of loop if i < n
.L16:
	movl %ecx,%eax		;  move s to return spot
	leal -8(%ebp),%esp	;  restore old stack pointer
	popl %ebx		;  restore saved local registers
	popl %esi		;  ditto
	leave			;  restore old frame pointer
	ret			;  return
	




.globl g
	.type	 g,@function
g:
	pushl %ebp
	movl %esp,%ebp
	movl 8(%ebp),%ecx
	movl 12(%ebp),%edx
	xorl %eax,%eax
	cmpl %edx,%eax
	jge .L4
	.p2align 4,,7
.L6:
	movl %eax,(%ecx,%eax,4)
	incl %eax
	cmpl %edx,%eax
	jl .L6
.L4:
	leave
	ret
.Lfe2:
	.size	 g,.Lfe2-g
	.align 4
.globl main
	.type	 main,@function
main:
	pushl %ebp
	movl %esp,%ebp
	subl $8,%esp
	addl $-8,%esp
	addl $-12,%esp
	pushl $42
	call f
	pushl %eax
	pushl $.LC0
	call printf
	leave
	ret
.Lfe3:
	.size	 main,.Lfe3-main
	.ident	"GCC: (GNU) 2.95.4  (Debian prerelease)"
