CALL

	pushes the current IP then jumps

ENTER does:

      push ebp
      mov ebp, esp
      sub esp, number of variable words

LEAVE is equivalent to:

      mov esp, ebp
      pop ebp








Hmm, I need to figure out what happens when the VM calls the optimised method:

It does:

	       i32OptimisedRet = (*pMethod)(optop -  pstMethodTemp->u16ArgSize + 1, nativeStackFrame);

which means it does roughly:

	    push native_stack_frame

which results absolute stack origin being

	    stack origin - 0x0  ->  ptr to native stack frame

then it does:
      
	    push new_optop

	    stack origin - 0x0  ->  ptr to native stack frame
	    stack origin - 0x4  ->  ptr to optop

then we (Fast generator's prologue) do:

     	qc("push ebp");

	    stack origin - 0x0  ->  ptr to native stack frame
	    stack origin - 0x4  ->  ptr to optop
	    stack origin - 0x8  ->  ptr to saved ebp


	qc("mov ebp,esp");
	qc("mov eax, 0x" + Integer.toHexString(class_pointer));
	qc("push eax"); //This creates our first local variable (actually part of the method frame) which points to the current class
	// this will be at ebp - 4

	    stack origin - 0x0  ->  ptr to native stack frame
	    stack origin - 0x4  ->  ptr to optop
	    stack origin - 0x8  ->  ptr to saved ebp   <<<<<<<< ------- this is where ebp points to
	    stack origin - 0xc  ->  ptr to class structure   

									 it means that stack frame is at [ebp + 0xc]
									 
	qc("mov eax, 0x" + Integer.toHexString(JNI_pointer));
	qc("push eax"); //This creates our second local var, which is pointer to the JNIEnv structure
	// this will be at ebp - 8

	    stack origin - 0x0  ->  ptr to native stack frame
	    stack origin - 0x4  ->  ptr to optop
	    stack origin - 0x8  ->  ptr to saved ebp   <<<<<<<< ------- this is where ebp points to
	    stack origin - 0xc  ->  ptr to class structure
	    stack origin - 0x10  ->  ptr to JNI env


	qc("sub esp, 0x4"); //We make space for one more local variable (used to store ebx in times of call :-))


	    stack origin - 0x0  ->  ptr to native stack frame
	    stack origin - 0x4  ->  ptr to optop
	    stack origin - 0x8  ->  ptr to saved ebp  <<<<<<<< ------- this is where ebp points to
	    stack origin - 0xc  ->  ptr to class structure 
	    stack origin - 0x10  ->  ptr to JNI env
	    stack origin - 0x14  ->  space for ebx

this results in optop being at [ebp + 0x8]
	        stack frame at [ebp + 0xc]

		class structure at [ebp - 0x4]
		JNI env		at [ebp - 0x8]
		ebx		at [ebp - 0xc]


 public int getField()
     {
        return afield;
      } 


Here is the getField assembly:

0 @  0 push ebp
1 @  1 mov ebp,esp
2 @  3 mov eax, 0x80c6f00
3 @  8 push eax
4 @  9 mov eax, 0x8090df8
5 @  14 push eax
6 @  15 sub esp, 0x4
7 @  21 mov eax,[ebp + 0x8]
8 @  24 push [eax]
9 @  26 mov [ebp - 12],ebx
10 @  29 pop edx
11 @  30 mov ebx, 0x0
12 @  35 mov eax, 0x0
13 @  40 push eax
14 @  41 push edx
15 @  42 mov eax, esp
16 @  44 sub eax,4
17 @  49 push eax
18 @  50 call ebx
19 @  52 cmp eax,0x0
20 @  57 je 0x0
21 @  59 leave
22 @  60 ret
23 @  61 mov eax, esp
24 @  63 mov eax, [eax]
25 @  65 add esp, 12
26 @  71 mov ebx, [ebp - 12]
27 @  74 push eax
28 @  75 pop edx
29 @  76 mov eax,[ebp + 0x8]
30 @  79 mov [eax], edx
31 @  81 mov eax, 0x0
32 @  86 leave
33 @  87 ret     

and the meat:

0 55
1 89
2 e5
3 b8
4 0
5 6f
6 c
7 8
8 50
9 b8
10 f8
11 d
12 9
13 8
14 50
15 81
16 ec
17 4
18 0
19 0
20 0
21 8b
22 45
23 8
24 ff
25 30
26 89
27 5d
28 f4
29 5a
30 bb
31 c8
32 83
33 7        
34 8
35 b8
36 0
37 0
38 0
39 0
40 50
41 52
42 89
43 e0
44 2d
45 4
46 0
47 0
48 0
49 50
50 ff
51 d3
52 3d
53 0
54 0
55 0
56 0
57 74  -- here we jump over two bytes (should it be 3?)
58 2
59 c9
60 c3
61 89
62 e0
63 8b
64 0
65 81
66 c4
67 c
68 0
69 0
70 0
71 8b
72 5d
73 f4
74 50
75 5a
76 8b
77 45
78 8
79 89
80 10
81 b8
82 0
83 0
84 0
85 0
86 c9
87 c3     








//Debugging the int2byte method (uses shl sar and and and or)
//Fixed part of the and ins

//Debugging the reverse jump method( uses a reverse jump)

1. Check the instruction 10  if_icmplt 5
2. That looks like source line 27 & 28
3. They point to machine code  80 & 82
4. It seems to be a forward jump of 27, definitely wrong
5. Ok go to Java source code and print out the distance we told it to jump
6. It says
   ---------- Generating reverse jump ----------
   ---------- Generating reverse jump patching location 83 and value is 39  ----------   
7. Now 0x27 = 32 + 7 = 39.
8. It should be a *reverse* jump!
9. 83 is the right location to patch though
10. Of course, we just calculated the distance, but didn't make it a negative jump
11. Right now, it's -39 (or d9)
12. So 82 - 39 = 43
13. Let's print out what we're aiming at
14. It says ---------- Generating reverse jump to 43 ---------- 
15. So the jump is correct. Well let's check it with an ASM example

16. It is:

00000000 <_runme>:
   0:   50                      pushl  %eax
      1:   52                      pushl  %edx

      00000002 <jlabel>:
         2:   52                      pushl  %edx
	    3:   01 c9                   addl   %ecx,%ecx
	       5:   e9 f8 ff ff ff          jmp    2 <jlabel>

	       0000000a <alabel>:
	          a:   c9                      leave
		     b:   c3                      ret     

17. It jumps over 3 + 5 = 8 instructions. We forgot to include the current inst!
18. It now says: ---------- Generating reverse jump patching location 83 and value is -41  ----------   
19. Lets check that 84 - 41 = 43
20. That looks right. It jumps to the iload_1 instruction .
21. So, is our jump correct? 

Method int reverseJump()
   0 iconst_0
   1 istore_1
   2 goto 8
   5 iinc 1 1
   8 iload_1
   9 iconst_5
  10 if_icmplt 5
  13 iload_1
  14 ireturn     


0 @  0 push ebp
1 @  1 mov ebp,esp
2 @  3 mov eax, 0x80f8ab8
3 @  8 push eax
4 @  9 mov eax, 0x8091b70
5 @  14 push eax
6 @  15 sub esp, 0x4
7 @  21 mov eax,0x0
8 @  26 push eax
9 @  27 mov eax,[ebp + 0x8]
10 @  30 add eax,0x4
11 @  35 pop edx
12 @  36 mov [eax],edx
13 @  38 jmp 0x0
14 @  43 mov eax,[ebp + 0x8]
15 @  46 add eax, 4
16 @  51 mov ecx, 1
17 @  56 mov edx, [eax]
18 @  58 add edx, ecx
19 @  60 mov [eax], edx
20 @  62 mov eax,[ebp + 0x8]
21 @  65 add eax,0x4
22 @  70 push [eax]
23 @  72 mov eax,0x5
24 @  77 push eax
25 @  78 pop ecx
26 @  79 pop eax
27 @  80 cmp eax,ecx
28 @  82 jl 0x0
29 @  84 mov eax,[ebp + 0x8]
30 @  87 add eax,0x4
31 @  92 push [eax]
32 @  94 pop edx
33 @  95 mov eax,[ebp + 0x8]
34 @  98 mov [eax], edx
35 @  100 mov eax, 0x0
36 @  105 leave
37 @  106 ret

Returning array [B@40b41834
Popping orphan, stack 0x40b41834, cavalry/translator/FastGenerator.compileMethod
]Result of compileMethod is 0x40b41834
0 55
1 89
2 e5
3 b8
4 b8
5 8a
6 f
7 8
8 50
9 b8
10 70
11 1b
12 9
13 8
14 50
15 81
16 ec
17 4
18 0
19 0
20 0
21 b8
22 0
23 0
24 0
25 0
26 50
27 8b
28 45
29 8
30 5
31 4
32 0
33 0
34 0
35 5a
36 89
37 10
38 e9
39 13
40 0
41 0
42 0
43 8b
44 45
45 8
46 5
47 4
48 0
49 0
50 0
51 b9
52 1
53 0
54 0
55 0
56 8b
57 2
58 1
59 ca
60 89
61 10
62 8b
63 45
64 8
65 5
66 4
67 0
68 0
69 0
70 ff
71 30
72 b8
73 5
74 0
75 0
76 0
77 50
78 59
79 58
80 39
81 c8
82 7c
83 27
84 8b
85 45
86 8
87 5
88 4
89 0
90 0
91 0
92 ff
93 30
94 5a
95 8b
96 45
97 8
98 89
99 10
100 b8
101 0
102 0
103 0
104 0
105 c9
106 c3   

















