Author: Dietmar Maurer (dietmar@ximian.com)
(C) 2001 Ximian, Inc.
 
Howto trigger JIT compilation
=============================

The JIT translates CIL code to native code on a per method basis. For example
if you have this simple program:

public class Test {
	public static void Main () {		
		System.Console.WriteLine ("Hello");
	}
}

the JIT first compiles the Main function. Unfortunately Main() contains another
reference to System.Console.WriteLine(), so the JIT also needs the address for
WriteLine() to generate a call instruction.

The simplest solution would be to JIT compile System.Console.WriteLine()
to generate that address. But that would mean that we JIT compile half of our
class library at once, since WriteLine() uses many other classes and function,
and we have to call the JIT for each of them. Even worse there is the
possibility of cyclic references, and we would end up in an endless loop.

Thus we need some kind of trampoline function for JIT compilation. Such a
trampoline first calls the JIT compiler to create native code, and then jumps
directly into that code. Whenever the JIT needs the address of a function (to
emit a call instruction) it uses the address of those trampoline functions.

One drawback of this approach is that it requires an additional indirection. We
always call the trampoline. Inside the trampoline we need to check if the
method is already compiled or not, and when not compiled we start JIT
compilation. After that we call the code. This process is quite time consuming
and shows very bad performance.

The solution is to add some logic to the trampoline function to detect from
where it is called. It is then possible for the JIT to patch the call
instruction in the caller, so that it directly calls the JIT compiled code
next time.

Implementation for x86
======================

emit-x86.c (arch_create_jit_trampoline): return the JIT trampoline function

emit-x86.c (x86_magic_trampoline): contains the code to detect the caller and
patch the call instruction.

emit-x86.c (arch_compile_method): JIT compile a method


