Merge pull request #5714 from alexischr/update_bockbuild
[mono.git] / docs / sources / mono-api-dynamic-codegen.html
1 <h2>Internal: Dynamic Code Generation</h2>
2
3         <p>The dynamic code generation interface inside the Mono
4         runtime is similar to the API exposed by
5         System.Reflection.Emit.
6
7         <p>This interface is used by Mono internally to generate code
8         on the flight in a cross-platform fashion.  For example,
9         P/Invoke marshalling in Mono is implemented in terms of this
10         interface, but it is also used in various other parts of the
11         runtime.
12
13         <p>Unlike Reflection.Emit, the dynamic code generation
14         interface does not start with an assembly builder.   The code
15         generation interface starts directly at the method level,
16         which is represented by a pointer to the MonoMethodBuilder
17         structure.
18
19         <p>To JIT this method, the process is this:
20
21         <ul>
22                 <li>Create a <tt>MonoMethodBuilder</tt> object using
23                 the <tt>mono_mb_new</tt> method.  The method's class
24                 is specified as the first argument.
25
26                 <li>Create the method signature, using
27                 <tt>mono_metadata_signature_alloc</tt>.  The call
28                 takes the number of arguments that the method takes.
29                 Then you must initialize the types for each one of the
30                 parameters.
31
32                 <li>Emit the CIL code, using one of the
33                 <tt>mono_mb_emit_*</tt> functions.   There are some
34                 helper routines that you can use.
35
36                 <li>Create the <tt>MonoMethod</tt> from the
37                 <tt>MethodBuilder</tt> using
38                 <tt>mono_mb_create_method</tt>.
39
40                 <li>Release the <tt>MonoMethodBuilder</tt> resources
41                 using mono_mb_free. 
42         </ul>
43
44         <p>The result of this process is a <tt>MonoMethod</tt> which
45         can be called using <tt><a
46         href="api:mono_create_jit_trampoline">mono_create_jit_trampoline</a></tt>
47         routine or can be passed to any other functions that require
48         the MonoMethod.
49
50         <p>Example:
51
52         <pre>
53 MonoMethod *adder ()
54 {
55     MonoMethodBuilder *mb;
56     MonoMethodSignature *sig;
57     MonoMethod *method;
58     
59     mb = mono_mb_new (mono_defaults.object_class, "adder", MONO_WRAPPER_NONE);
60
61     /* Setup method signature */
62     sig = mono_metadata_signature_alloc (2);
63     sig->ret = &amp;mono_get_int32_class ()->byval_arg;
64     sig->params [0] = &amp;mono_get_int32_class ()->byval_arg;
65     sig->params [1] = &amp;mono_defaults.int32_class->byval_arg;
66
67     /* Emit CIL code */
68     mono_mb_emit_ldarg (mb, 0);
69     mono_mb_emit_ldarg (mb, 1);
70     mono_mb_emit_byte (mb, CEE_ADD);
71     mono_mb_emit_byte (mb, CEE_RET);
72
73     /* Get the method */
74     method = mono_mb_create_method (mb, sig, max_stack);
75     
76     /* Cleanup */
77     mono_mb-free (mb);
78     return method;
79 }
80         </pre>
81         
82 <h4><a name="api:mono_mb_new">mono_mb_new</a></h4>
83
84         <p>The possible values for the <i>type</i> argument are:
85
86 <pre>
87         MONO_WRAPPER_NONE
88         MONO_WRAPPER_DELEGATE_INVOKE
89         MONO_WRAPPER_DELEGATE_BEGIN_INVOKE
90         MONO_WRAPPER_DELEGATE_END_INVOKE
91         MONO_WRAPPER_RUNTIME_INVOKE
92         MONO_WRAPPER_NATIVE_TO_MANAGED
93         MONO_WRAPPER_MANAGED_TO_NATIVE
94         MONO_WRAPPER_REMOTING_INVOKE
95         MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK
96         MONO_WRAPPER_XDOMAIN_INVOKE
97         MONO_WRAPPER_XDOMAIN_DISPATCH
98         MONO_WRAPPER_LDFLD
99         MONO_WRAPPER_STFLD
100         MONO_WRAPPER_LDFLD_REMOTE
101         MONO_WRAPPER_STFLD_REMOTE
102         MONO_WRAPPER_SYNCHRONIZED
103         MONO_WRAPPER_DYNAMIC_METHOD
104         MONO_WRAPPER_ISINST
105         MONO_WRAPPER_CASTCLASS
106         MONO_WRAPPER_PROXY_ISINST
107         MONO_WRAPPER_STELEMREF
108         MONO_WRAPPER_UNBOX
109         MONO_WRAPPER_LDFLDA
110         MONO_WRAPPER_UNKNOWN
111 </pre>
112
113 <h3>Emitting IL</h3>
114
115         <p>Functions that can be used to generate IL on the flight,
116         similar in spirit to System.Reflection.Emit.ILGenerator.
117         
118 <h4><a name="api:mono_mb_emit_add_to_local">mono_mb_emit_add_to_local</a></h4>
119 <h4><a name="api:mono_mb_emit_branch">mono_mb_emit_branch</a></h4>
120 <h4><a name="api:mono_mb_emit_byte">mono_mb_emit_byte</a></h4>
121 <h4><a name="api:mono_mb_emit_exception">mono_mb_emit_exception</a></h4>
122 <h4><a name="api:mono_mb_emit_i2">mono_mb_emit_i2</a></h4>
123 <h4><a name="api:mono_mb_emit_i4">mono_mb_emit_i4</a></h4>
124 <h4><a name="api:mono_mb_emit_icon">mono_mb_emit_icon</a></h4>
125 <h4><a name="api:mono_mb_emit_ldarg_addr">mono_mb_emit_ldarg_addr</a></h4>
126 <h4><a name="api:mono_mb_emit_ldarg">mono_mb_emit_ldarg</a></h4>
127 <h4><a name="api:mono_mb_emit_ldflda">mono_mb_emit_ldflda</a></h4>
128 <h4><a name="api:mono_mb_emit_ldloc_addr">mono_mb_emit_ldloc_addr</a></h4>
129 <h4><a name="api:mono_mb_emit_ldloc">mono_mb_emit_ldloc</a></h4>
130 <h4><a name="api:mono_mb_emit_ldstr">mono_mb_emit_ldstr</a></h4>
131 <h4><a name="api:mono_mb_emit_managed_call">mono_mb_emit_managed_call</a></h4>
132 <h4><a name="api:mono_mb_emit_native_call">mono_mb_emit_native_call</a></h4>
133 <h4><a name="api:mono_mb_emit_stloc">mono_mb_emit_stloc</a></h4>
134
135 <h3>Local variables and Methods</h3>
136 <h4><a name="api:mono_mb_create_method">mono_mb_create_method</a></h4>
137 <h4><a name="api:mono_mb_add_data">mono_mb_add_data</a></h4>
138 <h4><a name="api:mono_mb_add_local">mono_mb_add_local</a></h4>
139 <h4><a name="api:mono_mb_free">mono_mb_free</a></h4>
140
141 <h3>Patching Addresses</h3>
142 <h4><a name="api:mono_mb_patch_addr">mono_mb_patch_addr</a></h4>
143 <h4><a name="api:mono_mb_patch_addr_s">mono_mb_patch_addr_s</a></h4>
144
145 <h3>Method Signatures</h3>
146 <h4><a name="api:mono_metadata_signature_alloc">mono_metadata_signature_alloc</a></h4>
147 <h4><a name="api:mono_metadata_signature_dup">mono_metadata_signature_dup</a></h4>
148 <h4><a name="api:mono_metadata_signature_equal">mono_metadata_signature_equal</a></h4>
149
150