mono-api-dynamic-codegen.html 5.0 KB

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