2 * marshal.c: Routines for marshaling complex types in P/Invoke methods.
5 * Paolo Molaro (lupus@ximian.com)
7 * Copyright 2002-2003 Ximian, Inc (http://www.ximian.com)
8 * Copyright 2004-2009 Novell, Inc (http://www.novell.com)
20 #include "metadata/marshal.h"
21 #include "metadata/method-builder.h"
22 #include "metadata/tabledefs.h"
23 #include "metadata/exception.h"
24 #include "metadata/appdomain.h"
25 #include "mono/metadata/debug-helpers.h"
26 #include "mono/metadata/threadpool.h"
27 #include "mono/metadata/threads.h"
28 #include "mono/metadata/monitor.h"
29 #include "mono/metadata/metadata-internals.h"
30 #include "mono/metadata/domain-internals.h"
31 #include "mono/metadata/gc-internal.h"
32 #include "mono/metadata/threads-types.h"
33 #include "mono/metadata/string-icalls.h"
34 #include "mono/metadata/attrdefs.h"
35 #include "mono/metadata/gc-internal.h"
36 #include "mono/metadata/cominterop.h"
37 #include "mono/utils/mono-counters.h"
41 /* #define DEBUG_RUNTIME_CODE */
43 #define OPDEF(a,b,c,d,e,f,g,h,i,j) \
47 MONO_MARSHAL_NONE
, /* No marshalling needed */
48 MONO_MARSHAL_COPY
, /* Can be copied by value to the new domain */
49 MONO_MARSHAL_COPY_OUT
, /* out parameter that needs to be copied back to the original instance */
50 MONO_MARSHAL_SERIALIZE
/* Value needs to be serialized into the new domain */
51 } MonoXDomainMarshalType
;
54 #include "mono/cil/opcode.def"
59 struct _MonoRemotingMethods
{
61 MonoMethod
*invoke_with_check
;
62 MonoMethod
*xdomain_invoke
;
63 MonoMethod
*xdomain_dispatch
;
66 typedef struct _MonoRemotingMethods MonoRemotingMethods
;
69 * This mutex protects the various marshalling related caches in MonoImage
70 * and a few other data structures static to this file.
71 * Note that when this lock is held it is not possible to take other runtime
72 * locks like the loader lock.
74 #define mono_marshal_lock() EnterCriticalSection (&marshal_mutex)
75 #define mono_marshal_unlock() LeaveCriticalSection (&marshal_mutex)
76 static CRITICAL_SECTION marshal_mutex
;
77 static gboolean marshal_mutex_initialized
;
79 static guint32 last_error_tls_id
;
81 static guint32 load_type_info_tls_id
;
84 delegate_hash_table_add (MonoDelegate
*d
, MonoObject
**target_loc
);
87 emit_struct_conv (MonoMethodBuilder
*mb
, MonoClass
*klass
, gboolean to_object
);
90 mono_struct_delete_old (MonoClass
*klass
, char *ptr
);
93 mono_marshal_string_to_utf16 (MonoString
*s
);
96 mono_marshal_string_to_utf16_copy (MonoString
*s
);
99 mono_string_to_lpstr (MonoString
*string_obj
);
101 static MonoStringBuilder
*
102 mono_string_utf8_to_builder2 (char *text
);
104 static MonoStringBuilder
*
105 mono_string_utf16_to_builder2 (gunichar2
*text
);
108 mono_byvalarray_to_array (MonoArray
*arr
, gpointer native_arr
, MonoClass
*eltype
, guint32 elnum
);
111 mono_array_to_byvalarray (gpointer native_arr
, MonoArray
*arr
, MonoClass
*eltype
, guint32 elnum
);
114 mono_remoting_wrapper (MonoMethod
*method
, gpointer
*params
);
116 static MonoAsyncResult
*
117 mono_delegate_begin_invoke (MonoDelegate
*delegate
, gpointer
*params
);
120 mono_delegate_end_invoke (MonoDelegate
*delegate
, gpointer
*params
);
123 mono_marshal_xdomain_copy_out_value (MonoObject
*src
, MonoObject
*dst
);
126 mono_marshal_set_domain_by_id (gint32 id
, MonoBoolean push
);
129 mono_marshal_check_domain_image (gint32 domain_id
, MonoImage
*image
);
132 mono_upgrade_remote_class_wrapper (MonoReflectionType
*rtype
, MonoTransparentProxy
*tproxy
);
134 static MonoReflectionType
*
135 type_from_handle (MonoType
*handle
);
138 mono_marshal_set_last_error_windows (int error
);
140 static void init_safe_handle (void);
142 /* MonoMethod pointers to SafeHandle::DangerousAddRef and ::DangerousRelease */
143 static MonoMethod
*sh_dangerous_add_ref
;
144 static MonoMethod
*sh_dangerous_release
;
150 sh_dangerous_add_ref
= mono_class_get_method_from_name (
151 mono_defaults
.safehandle_class
, "DangerousAddRef", 1);
152 sh_dangerous_release
= mono_class_get_method_from_name (
153 mono_defaults
.safehandle_class
, "DangerousRelease", 0);
157 register_icall (gpointer func
, const char *name
, const char *sigstr
, gboolean save
)
159 MonoMethodSignature
*sig
= mono_create_icall_signature (sigstr
);
161 mono_register_jit_icall (func
, name
, sig
, save
);
164 static MonoMethodSignature
*
165 signature_dup (MonoImage
*image
, MonoMethodSignature
*sig
)
167 MonoMethodSignature
*res
;
170 res
= mono_metadata_signature_alloc (image
, sig
->param_count
);
171 sigsize
= MONO_SIZEOF_METHOD_SIGNATURE
+ sig
->param_count
* sizeof (MonoType
*);
172 memcpy (res
, sig
, sigsize
);
178 mono_signature_no_pinvoke (MonoMethod
*method
)
180 MonoMethodSignature
*sig
= mono_method_signature (method
);
182 sig
= signature_dup (method
->klass
->image
, sig
);
183 sig
->pinvoke
= FALSE
;
190 mono_marshal_init (void)
192 static gboolean module_initialized
= FALSE
;
194 if (!module_initialized
) {
195 module_initialized
= TRUE
;
196 InitializeCriticalSection (&marshal_mutex
);
197 marshal_mutex_initialized
= TRUE
;
198 last_error_tls_id
= TlsAlloc ();
199 load_type_info_tls_id
= TlsAlloc ();
201 register_icall (ves_icall_System_Threading_Thread_ResetAbort
, "ves_icall_System_Threading_Thread_ResetAbort", "void", TRUE
);
202 register_icall (mono_marshal_string_to_utf16
, "mono_marshal_string_to_utf16", "ptr obj", FALSE
);
203 register_icall (mono_marshal_string_to_utf16_copy
, "mono_marshal_string_to_utf16_copy", "ptr obj", FALSE
);
204 register_icall (mono_string_to_utf16
, "mono_string_to_utf16", "ptr obj", FALSE
);
205 register_icall (mono_string_from_utf16
, "mono_string_from_utf16", "obj ptr", FALSE
);
206 register_icall (mono_string_new_wrapper
, "mono_string_new_wrapper", "obj ptr", FALSE
);
207 register_icall (mono_string_to_utf8
, "mono_string_to_utf8", "ptr obj", FALSE
);
208 register_icall (mono_string_to_lpstr
, "mono_string_to_lpstr", "ptr obj", FALSE
);
209 register_icall (mono_string_to_ansibstr
, "mono_string_to_ansibstr", "ptr object", FALSE
);
210 register_icall (mono_string_builder_to_utf8
, "mono_string_builder_to_utf8", "ptr object", FALSE
);
211 register_icall (mono_string_builder_to_utf16
, "mono_string_builder_to_utf16", "ptr object", FALSE
);
212 register_icall (mono_array_to_savearray
, "mono_array_to_savearray", "ptr object", FALSE
);
213 register_icall (mono_array_to_lparray
, "mono_array_to_lparray", "ptr object", FALSE
);
214 register_icall (mono_free_lparray
, "mono_free_lparray", "void object ptr", FALSE
);
215 register_icall (mono_byvalarray_to_array
, "mono_byvalarray_to_array", "void object ptr ptr int32", FALSE
);
216 register_icall (mono_array_to_byvalarray
, "mono_array_to_byvalarray", "void ptr object ptr int32", FALSE
);
217 register_icall (mono_delegate_to_ftnptr
, "mono_delegate_to_ftnptr", "ptr object", FALSE
);
218 register_icall (mono_ftnptr_to_delegate
, "mono_ftnptr_to_delegate", "object ptr ptr", FALSE
);
219 register_icall (mono_marshal_asany
, "mono_marshal_asany", "ptr object int32 int32", FALSE
);
220 register_icall (mono_marshal_free_asany
, "mono_marshal_free_asany", "void object ptr int32 int32", FALSE
);
221 register_icall (mono_marshal_alloc
, "mono_marshal_alloc", "ptr int32", FALSE
);
222 register_icall (mono_marshal_free
, "mono_marshal_free", "void ptr", FALSE
);
223 register_icall (mono_marshal_set_last_error
, "mono_marshal_set_last_error", "void", FALSE
);
224 register_icall (mono_marshal_set_last_error_windows
, "mono_marshal_set_last_error_windows", "void int32", FALSE
);
225 register_icall (mono_string_utf8_to_builder
, "mono_string_utf8_to_builder", "void ptr ptr", FALSE
);
226 register_icall (mono_string_utf8_to_builder2
, "mono_string_utf8_to_builder2", "object ptr", FALSE
);
227 register_icall (mono_string_utf16_to_builder
, "mono_string_utf16_to_builder", "void ptr ptr", FALSE
);
228 register_icall (mono_string_utf16_to_builder2
, "mono_string_utf16_to_builder2", "object ptr", FALSE
);
229 register_icall (mono_marshal_free_array
, "mono_marshal_free_array", "void ptr int32", FALSE
);
230 register_icall (mono_string_to_byvalstr
, "mono_string_to_byvalstr", "void ptr ptr int32", FALSE
);
231 register_icall (mono_string_to_byvalwstr
, "mono_string_to_byvalwstr", "void ptr ptr int32", FALSE
);
232 register_icall (g_free
, "g_free", "void ptr", FALSE
);
233 register_icall (mono_object_isinst
, "mono_object_isinst", "object object ptr", FALSE
);
234 register_icall (mono_struct_delete_old
, "mono_struct_delete_old", "void ptr ptr", FALSE
);
235 register_icall (mono_remoting_wrapper
, "mono_remoting_wrapper", "object ptr ptr", FALSE
);
236 register_icall (mono_delegate_begin_invoke
, "mono_delegate_begin_invoke", "object object ptr", FALSE
);
237 register_icall (mono_delegate_end_invoke
, "mono_delegate_end_invoke", "object object ptr", FALSE
);
238 register_icall (mono_marshal_xdomain_copy_value
, "mono_marshal_xdomain_copy_value", "object object", FALSE
);
239 register_icall (mono_marshal_xdomain_copy_out_value
, "mono_marshal_xdomain_copy_out_value", "void object object", FALSE
);
240 register_icall (mono_marshal_set_domain_by_id
, "mono_marshal_set_domain_by_id", "int32 int32 int32", FALSE
);
241 register_icall (mono_marshal_check_domain_image
, "mono_marshal_check_domain_image", "int32 int32 ptr", FALSE
);
242 register_icall (mono_compile_method
, "mono_compile_method", "ptr ptr", FALSE
);
243 register_icall (mono_context_get
, "mono_context_get", "object", FALSE
);
244 register_icall (mono_context_set
, "mono_context_set", "void object", FALSE
);
245 register_icall (mono_upgrade_remote_class_wrapper
, "mono_upgrade_remote_class_wrapper", "void object object", FALSE
);
246 register_icall (type_from_handle
, "type_from_handle", "object ptr", FALSE
);
247 register_icall (mono_gc_wbarrier_generic_nostore
, "wb_generic", "void ptr", FALSE
);
249 mono_cominterop_init ();
254 mono_marshal_cleanup (void)
256 mono_cominterop_cleanup ();
258 TlsFree (load_type_info_tls_id
);
259 TlsFree (last_error_tls_id
);
260 DeleteCriticalSection (&marshal_mutex
);
261 marshal_mutex_initialized
= FALSE
;
264 MonoClass
*byte_array_class
;
265 static MonoMethod
*method_rs_serialize
, *method_rs_deserialize
, *method_exc_fixexc
, *method_rs_appdomain_target
;
266 static MonoMethod
*method_set_context
, *method_get_context
;
267 static MonoMethod
*method_set_call_context
, *method_needs_context_sink
, *method_rs_serialize_exc
;
270 mono_remoting_marshal_init (void)
274 static gboolean module_initialized
= FALSE
;
276 if (!module_initialized
) {
277 klass
= mono_class_from_name (mono_defaults
.corlib
, "System.Runtime.Remoting", "RemotingServices");
278 method_rs_serialize
= mono_class_get_method_from_name (klass
, "SerializeCallData", -1);
279 method_rs_deserialize
= mono_class_get_method_from_name (klass
, "DeserializeCallData", -1);
280 method_rs_serialize_exc
= mono_class_get_method_from_name (klass
, "SerializeExceptionData", -1);
282 klass
= mono_defaults
.real_proxy_class
;
283 method_rs_appdomain_target
= mono_class_get_method_from_name (klass
, "GetAppDomainTarget", -1);
285 klass
= mono_defaults
.exception_class
;
286 method_exc_fixexc
= mono_class_get_method_from_name (klass
, "FixRemotingException", -1);
288 klass
= mono_defaults
.thread_class
;
289 method_get_context
= mono_class_get_method_from_name (klass
, "get_CurrentContext", -1);
291 klass
= mono_defaults
.appdomain_class
;
292 method_set_context
= mono_class_get_method_from_name (klass
, "InternalSetContext", -1);
293 byte_array_class
= mono_array_class_get (mono_defaults
.byte_class
, 1);
295 klass
= mono_class_from_name (mono_defaults
.corlib
, "System.Runtime.Remoting.Messaging", "CallContext");
296 method_set_call_context
= mono_class_get_method_from_name (klass
, "SetCurrentCallContext", -1);
298 klass
= mono_class_from_name (mono_defaults
.corlib
, "System.Runtime.Remoting.Contexts", "Context");
299 method_needs_context_sink
= mono_class_get_method_from_name (klass
, "get_NeedsContextSink", -1);
301 module_initialized
= TRUE
;
306 mono_delegate_to_ftnptr (MonoDelegate
*delegate
)
308 MonoMethod
*method
, *wrapper
;
310 MonoObject
**target_loc
;
315 if (delegate
->delegate_trampoline
)
316 return delegate
->delegate_trampoline
;
318 klass
= ((MonoObject
*)delegate
)->vtable
->klass
;
319 g_assert (klass
->delegate
);
321 method
= delegate
->method
;
323 if (mono_method_signature (method
)->pinvoke
) {
324 const char *exc_class
, *exc_arg
;
327 ftnptr
= mono_lookup_pinvoke_call (method
, &exc_class
, &exc_arg
);
329 g_assert (exc_class
);
330 mono_raise_exception (mono_exception_from_name_msg (mono_defaults
.corlib
, "System", exc_class
, exc_arg
));
335 if (delegate
->target
) {
336 /* Produce a location which can be embedded in JITted code */
337 target_loc
= mono_gc_alloc_fixed (sizeof (MonoObject
*), NULL
);
338 *target_loc
= delegate
->target
;
343 wrapper
= mono_marshal_get_managed_wrapper (method
, klass
, target_loc
);
345 delegate
->delegate_trampoline
= mono_compile_method (wrapper
);
347 // Add the delegate to the delegate hash table
348 delegate_hash_table_add (delegate
, target_loc
);
350 /* when the object is collected, collect the dynamic method, too */
351 mono_object_register_finalizer ((MonoObject
*)delegate
);
353 return delegate
->delegate_trampoline
;
357 * this hash table maps from a delegate trampoline object to a weak reference
358 * of the delegate. As an optimizations with a non-moving GC we store the
359 * object pointer itself, otherwise we use a GC handle.
361 static GHashTable
*delegate_hash_table
;
362 /* Contains root locations pointing to the this arguments of delegates */
363 static MonoGHashTable
*delegate_target_locations
;
366 delegate_hash_table_new (void) {
367 return g_hash_table_new (NULL
, NULL
);
371 delegate_hash_table_remove (MonoDelegate
*d
)
373 MonoObject
**target_loc
;
374 #ifdef HAVE_MOVING_COLLECTOR
377 mono_marshal_lock ();
378 if (delegate_hash_table
== NULL
)
379 delegate_hash_table
= delegate_hash_table_new ();
380 #ifdef HAVE_MOVING_COLLECTOR
381 gchandle
= GPOINTER_TO_UINT (g_hash_table_lookup (delegate_hash_table
, d
->delegate_trampoline
));
383 g_hash_table_remove (delegate_hash_table
, d
->delegate_trampoline
);
384 if (delegate_target_locations
)
385 target_loc
= mono_g_hash_table_lookup (delegate_target_locations
, d
->delegate_trampoline
);
389 mono_g_hash_table_remove (delegate_target_locations
, d
->delegate_trampoline
);
390 mono_marshal_unlock ();
392 mono_gc_free_fixed (target_loc
);
394 #ifdef HAVE_MOVING_COLLECTOR
395 mono_gchandle_free (gchandle
);
400 delegate_hash_table_add (MonoDelegate
*d
, MonoObject
**target_loc
)
402 #ifdef HAVE_MOVING_COLLECTOR
403 guint32 gchandle
= mono_gchandle_new_weakref ((MonoObject
*)d
, FALSE
);
404 guint32 old_gchandle
;
406 mono_marshal_lock ();
407 if (delegate_hash_table
== NULL
)
408 delegate_hash_table
= delegate_hash_table_new ();
409 if (delegate_target_locations
== NULL
) {
410 /* Has to be conservative as the values are not object references */
411 delegate_target_locations
= mono_g_hash_table_new_type (NULL
, NULL
, MONO_HASH_CONSERVATIVE_GC
);
412 MONO_GC_REGISTER_ROOT (delegate_target_locations
);
414 #ifdef HAVE_MOVING_COLLECTOR
415 old_gchandle
= GPOINTER_TO_UINT (g_hash_table_lookup (delegate_hash_table
, d
->delegate_trampoline
));
416 g_hash_table_insert (delegate_hash_table
, d
->delegate_trampoline
, GUINT_TO_POINTER (gchandle
));
418 mono_gchandle_free (old_gchandle
);
420 g_hash_table_insert (delegate_hash_table
, d
->delegate_trampoline
, d
);
423 /* This keeps target_loc alive for Boehm */
424 mono_g_hash_table_insert (delegate_target_locations
, d
->delegate_trampoline
, target_loc
);
425 mono_marshal_unlock ();
429 mono_ftnptr_to_delegate (MonoClass
*klass
, gpointer ftn
)
431 #ifdef HAVE_MOVING_COLLECTOR
439 mono_marshal_lock ();
440 if (delegate_hash_table
== NULL
)
441 delegate_hash_table
= delegate_hash_table_new ();
443 #ifdef HAVE_MOVING_COLLECTOR
444 gchandle
= GPOINTER_TO_UINT (g_hash_table_lookup (delegate_hash_table
, ftn
));
445 mono_marshal_unlock ();
447 d
= (MonoDelegate
*)mono_gchandle_get_target (gchandle
);
451 d
= g_hash_table_lookup (delegate_hash_table
, ftn
);
452 mono_marshal_unlock ();
455 /* This is a native function, so construct a delegate for it */
456 static MonoClass
*UnmanagedFunctionPointerAttribute
;
457 MonoMethodSignature
*sig
;
459 MonoMarshalSpec
**mspecs
;
460 MonoCustomAttrInfo
*cinfo
;
461 MonoReflectionUnmanagedFunctionPointerAttribute
*attr
;
462 MonoMethod
*invoke
= mono_get_delegate_invoke (klass
);
463 MonoMethodPInvoke piinfo
;
466 memset (&piinfo
, 0, sizeof (piinfo
));
467 if (!UnmanagedFunctionPointerAttribute
)
468 UnmanagedFunctionPointerAttribute
= mono_class_from_name (mono_defaults
.corlib
, "System.Runtime.InteropServices", "UnmanagedFunctionPointerAttribute");
470 /* The attribute is only available in Net 2.0 */
471 if (UnmanagedFunctionPointerAttribute
) {
473 * The pinvoke attributes are stored in a real custom attribute so we have to
476 cinfo
= mono_custom_attrs_from_class (klass
);
478 attr
= (MonoReflectionUnmanagedFunctionPointerAttribute
*)mono_custom_attrs_get_attr (cinfo
, UnmanagedFunctionPointerAttribute
);
480 piinfo
.piflags
= (attr
->call_conv
<< 8) | (attr
->charset
? (attr
->charset
- 1) * 2 : 1) | attr
->set_last_error
;
483 mono_custom_attrs_free (cinfo
);
487 mspecs
= g_new0 (MonoMarshalSpec
*, mono_method_signature (invoke
)->param_count
+ 1);
488 mono_method_get_marshal_info (invoke
, mspecs
);
489 /* Freed below so don't alloc from mempool */
490 sig
= mono_metadata_signature_dup (mono_method_signature (invoke
));
493 wrapper
= mono_marshal_get_native_func_wrapper (klass
->image
, sig
, &piinfo
, mspecs
, ftn
);
495 for (i
= mono_method_signature (invoke
)->param_count
; i
>= 0; i
--)
497 mono_metadata_free_marshal_spec (mspecs
[i
]);
501 d
= (MonoDelegate
*)mono_object_new (mono_domain_get (), klass
);
502 mono_delegate_ctor_with_method ((MonoObject
*)d
, NULL
, mono_compile_method (wrapper
), wrapper
);
505 if (d
->object
.vtable
->domain
!= mono_domain_get ())
506 mono_raise_exception (mono_get_exception_not_supported ("Delegates cannot be marshalled from native code into a domain other than their home domain"));
512 mono_delegate_free_ftnptr (MonoDelegate
*delegate
)
517 delegate_hash_table_remove (delegate
);
519 ptr
= (gpointer
)InterlockedExchangePointer (&delegate
->delegate_trampoline
, NULL
);
521 if (!delegate
->target
) {
522 /* The wrapper method is shared between delegates -> no need to free it */
527 ji
= mono_jit_info_table_find (mono_domain_get (), mono_get_addr_from_ftnptr (ptr
));
530 mono_runtime_free_method (mono_object_domain (delegate
), ji
->method
);
535 mono_array_to_savearray (MonoArray
*array
)
540 g_assert_not_reached ();
545 mono_array_to_lparray (MonoArray
*array
)
547 gpointer
*nativeArray
= NULL
;
548 int nativeArraySize
= 0;
557 klass
= array
->obj
.vtable
->klass
;
559 switch (klass
->element_class
->byval_arg
.type
) {
561 g_assert_not_reached ();
564 case MONO_TYPE_CLASS
:
565 nativeArraySize
= array
->max_length
;
566 nativeArray
= malloc(sizeof(gpointer
) * nativeArraySize
);
567 for(i
= 0; i
< nativeArraySize
; ++i
)
568 nativeArray
[i
] = ves_icall_System_Runtime_InteropServices_Marshal_GetIUnknownForObjectInternal(((gpointer
*)array
->vector
)[i
]);
572 case MONO_TYPE_BOOLEAN
:
585 case MONO_TYPE_VALUETYPE
:
588 case MONO_TYPE_GENERICINST
:
590 case MONO_TYPE_OBJECT
:
591 case MONO_TYPE_ARRAY
:
592 case MONO_TYPE_SZARRAY
:
593 case MONO_TYPE_STRING
:
595 g_warning ("type 0x%x not handled", klass
->element_class
->byval_arg
.type
);
596 g_assert_not_reached ();
599 return array
->vector
;
603 mono_free_lparray (MonoArray
*array
, gpointer
* nativeArray
)
614 klass
= array
->obj
.vtable
->klass
;
616 switch (klass
->element_class
->byval_arg
.type
) {
617 case MONO_TYPE_CLASS
:
618 for(i
= 0; i
< array
->max_length
; ++i
)
619 mono_marshal_free_ccw(nativeArray
[i
]);
627 mono_byvalarray_to_array (MonoArray
*arr
, gpointer native_arr
, MonoClass
*elclass
, guint32 elnum
)
629 g_assert (arr
->obj
.vtable
->klass
->element_class
== mono_defaults
.char_class
);
631 if (elclass
== mono_defaults
.byte_class
) {
632 GError
*error
= NULL
;
636 ut
= g_utf8_to_utf16 (native_arr
, elnum
, NULL
, &items_written
, &error
);
639 memcpy (mono_array_addr (arr
, guint16
, 0), ut
, items_written
* sizeof (guint16
));
643 g_error_free (error
);
646 g_assert_not_reached ();
650 mono_array_to_byvalarray (gpointer native_arr
, MonoArray
*arr
, MonoClass
*elclass
, guint32 elnum
)
652 g_assert (arr
->obj
.vtable
->klass
->element_class
== mono_defaults
.char_class
);
654 if (elclass
== mono_defaults
.byte_class
) {
656 GError
*error
= NULL
;
658 as
= g_utf16_to_utf8 (mono_array_addr (arr
, gunichar2
, 0), mono_array_length (arr
), NULL
, NULL
, &error
);
660 MonoException
*exc
= mono_get_exception_argument ("string", error
->message
);
661 g_error_free (error
);
662 mono_raise_exception (exc
);
665 memcpy (native_arr
, as
, MIN (strlen (as
), elnum
));
668 g_assert_not_reached ();
673 mono_string_utf8_to_builder (MonoStringBuilder
*sb
, char *text
)
675 GError
*error
= NULL
;
685 ut
= g_utf8_to_utf16 (text
, l
, NULL
, &items_written
, &error
);
687 if (items_written
> mono_stringbuilder_capacity (sb
))
688 items_written
= mono_stringbuilder_capacity (sb
);
691 if (! sb
->str
|| sb
->str
== sb
->cached_str
) {
692 MONO_OBJECT_SETREF (sb
, str
, mono_string_new_size (mono_domain_get (), items_written
));
693 sb
->cached_str
= NULL
;
696 memcpy (mono_string_chars (sb
->str
), ut
, items_written
* 2);
697 sb
->length
= items_written
;
699 g_error_free (error
);
705 mono_string_utf8_to_builder2 (char *text
)
708 MonoStringBuilder
*sb
;
709 static MonoClass
*string_builder_class
;
710 static MonoMethod
*sb_ctor
;
717 if (!string_builder_class
) {
718 MonoMethodDesc
*desc
;
720 string_builder_class
= mono_class_from_name (mono_defaults
.corlib
, "System.Text", "StringBuilder");
721 g_assert (string_builder_class
);
722 desc
= mono_method_desc_new (":.ctor(int)", FALSE
);
723 sb_ctor
= mono_method_desc_search_in_class (desc
, string_builder_class
);
725 mono_method_desc_free (desc
);
730 sb
= (MonoStringBuilder
*)mono_object_new (mono_domain_get (), string_builder_class
);
733 mono_runtime_invoke (sb_ctor
, sb
, args
, &exc
);
736 mono_string_utf8_to_builder (sb
, text
);
742 * FIXME: This routine does not seem to do what it seems to do
743 * the @text is never copied into the string builder
746 mono_string_utf16_to_builder (MonoStringBuilder
*sb
, gunichar2
*text
)
753 g_assert (mono_string_chars (sb
->str
) == text
);
755 for (len
= 0; text
[len
] != 0; ++len
)
762 mono_string_utf16_to_builder2 (gunichar2
*text
)
765 MonoStringBuilder
*sb
;
766 static MonoClass
*string_builder_class
;
767 static MonoMethod
*sb_ctor
;
774 if (!string_builder_class
) {
775 MonoMethodDesc
*desc
;
777 string_builder_class
= mono_class_from_name (mono_defaults
.corlib
, "System.Text", "StringBuilder");
778 g_assert (string_builder_class
);
779 desc
= mono_method_desc_new (":.ctor(int)", FALSE
);
780 sb_ctor
= mono_method_desc_search_in_class (desc
, string_builder_class
);
782 mono_method_desc_free (desc
);
785 for (len
= 0; text
[len
] != 0; ++len
)
788 sb
= (MonoStringBuilder
*)mono_object_new (mono_domain_get (), string_builder_class
);
791 mono_runtime_invoke (sb_ctor
, sb
, args
, &exc
);
795 memcpy (mono_string_chars (sb
->str
), text
, len
* 2);
801 * mono_string_builder_to_utf8:
802 * @sb: the string builder
804 * Converts to utf8 the contents of the MonoStringBuilder.
806 * Returns: a utf8 string with the contents of the StringBuilder.
808 * The return value must be released with g_free.
811 mono_string_builder_to_utf8 (MonoStringBuilder
*sb
)
813 GError
*error
= NULL
;
814 gchar
*tmp
, *res
= NULL
;
819 if ((sb
->str
== sb
->cached_str
) && (sb
->str
->length
== 0)) {
821 * The sb could have been allocated with the default capacity and be empty.
822 * we need to alloc a buffer of the default capacity in this case.
824 MONO_OBJECT_SETREF (sb
, str
, mono_string_new_size (mono_domain_get (), 16));
825 sb
->cached_str
= NULL
;
828 tmp
= g_utf16_to_utf8 (mono_string_chars (sb
->str
), sb
->length
, NULL
, NULL
, &error
);
830 g_error_free (error
);
831 mono_raise_exception (mono_get_exception_execution_engine ("Failed to convert StringBuilder from utf16 to utf8"));
833 res
= mono_marshal_alloc (mono_stringbuilder_capacity (sb
) + 1);
834 memcpy (res
, tmp
, sb
->length
+ 1);
842 * mono_string_builder_to_utf16:
843 * @sb: the string builder
845 * Converts to utf16 the contents of the MonoStringBuilder.
847 * Returns: a utf16 string with the contents of the StringBuilder.
849 * The return value must not be freed.
852 mono_string_builder_to_utf16 (MonoStringBuilder
*sb
)
860 * The stringbuilder might not have ownership of this string. If this is
861 * the case, we must duplicate the string, so that we don't munge immutable
864 if (sb
->str
== sb
->cached_str
) {
866 * The sb could have been allocated with the default capacity and be empty.
867 * we need to alloc a buffer of the default capacity in this case.
869 if (sb
->str
->length
== 0)
870 MONO_OBJECT_SETREF (sb
, str
, mono_string_new_size (mono_domain_get (), 16));
872 MONO_OBJECT_SETREF (sb
, str
, mono_string_new_utf16 (mono_domain_get (), mono_string_chars (sb
->str
), mono_stringbuilder_capacity (sb
)));
873 sb
->cached_str
= NULL
;
877 *(mono_string_chars (sb
->str
)) = '\0';
879 return mono_string_chars (sb
->str
);
883 mono_string_to_lpstr (MonoString
*s
)
888 GError
*error
= NULL
;
894 as
= CoTaskMemAlloc (1);
899 tmp
= g_utf16_to_utf8 (mono_string_chars (s
), s
->length
, NULL
, &len
, &error
);
901 MonoException
*exc
= mono_get_exception_argument ("string", error
->message
);
902 g_error_free (error
);
903 mono_raise_exception(exc
);
906 as
= CoTaskMemAlloc (len
+ 1);
907 memcpy (as
, tmp
, len
+ 1);
912 return mono_string_to_utf8 (s
);
917 mono_string_to_ansibstr (MonoString
*string_obj
)
919 g_error ("UnmanagedMarshal.BStr is not implemented.");
924 * mono_string_to_byvalstr:
925 * @dst: Where to store the null-terminated utf8 decoded string.
926 * @src: the MonoString to copy.
927 * @size: the maximum number of bytes to copy.
929 * Copies the MonoString pointed to by @src as a utf8 string
930 * into @dst, it copies at most @size bytes into the destination.
933 mono_string_to_byvalstr (gpointer dst
, MonoString
*src
, int size
)
938 g_assert (dst
!= NULL
);
941 memset (dst
, 0, size
);
945 s
= mono_string_to_utf8 (src
);
946 len
= MIN (size
, strlen (s
));
949 memcpy (dst
, s
, len
);
954 * mono_string_to_byvalwstr:
955 * @dst: Where to store the null-terminated utf16 decoded string.
956 * @src: the MonoString to copy.
957 * @size: the maximum number of bytes to copy.
959 * Copies the MonoString pointed to by @src as a utf16 string into
960 * @dst, it copies at most @size bytes into the destination (including
961 * a terminating 16-bit zero terminator).
964 mono_string_to_byvalwstr (gpointer dst
, MonoString
*src
, int size
)
968 g_assert (dst
!= NULL
);
972 memset (dst
, 0, size
* 2);
976 len
= MIN (size
, (mono_string_length (src
)));
977 memcpy (dst
, mono_string_chars (src
), size
* 2);
978 if (size
<= mono_string_length (src
))
980 *((gunichar2
*) dst
+ len
) = 0;
984 mono_mb_emit_proxy_check (MonoMethodBuilder
*mb
, int branch_code
)
987 mono_mb_emit_ldflda (mb
, G_STRUCT_OFFSET (MonoObject
, vtable
));
988 mono_mb_emit_byte (mb
, CEE_LDIND_I
);
989 mono_mb_emit_icon (mb
, G_STRUCT_OFFSET (MonoVTable
, klass
));
990 mono_mb_emit_byte (mb
, CEE_ADD
);
991 mono_mb_emit_byte (mb
, CEE_LDIND_I
);
992 mono_mb_emit_byte (mb
, MONO_CUSTOM_PREFIX
);
993 mono_mb_emit_byte (mb
, CEE_MONO_CLASSCONST
);
994 mono_mb_emit_i4 (mb
, mono_mb_add_data (mb
, mono_defaults
.transparent_proxy_class
));
995 pos
= mono_mb_emit_branch (mb
, branch_code
);
1000 mono_mb_emit_xdomain_check (MonoMethodBuilder
*mb
, int branch_code
)
1003 mono_mb_emit_ldflda (mb
, G_STRUCT_OFFSET (MonoTransparentProxy
, rp
));
1004 mono_mb_emit_byte (mb
, CEE_LDIND_REF
);
1005 mono_mb_emit_ldflda (mb
, G_STRUCT_OFFSET (MonoRealProxy
, target_domain_id
));
1006 mono_mb_emit_byte (mb
, CEE_LDIND_I4
);
1007 mono_mb_emit_icon (mb
, -1);
1008 pos
= mono_mb_emit_branch (mb
, branch_code
);
1013 mono_mb_emit_contextbound_check (MonoMethodBuilder
*mb
, int branch_code
)
1015 static int offset
= -1;
1019 mono_marshal_find_bitfield_offset (MonoClass
, contextbound
, &offset
, &mask
);
1021 mono_mb_emit_ldflda (mb
, G_STRUCT_OFFSET (MonoTransparentProxy
, remote_class
));
1022 mono_mb_emit_byte (mb
, CEE_LDIND_REF
);
1023 mono_mb_emit_ldflda (mb
, G_STRUCT_OFFSET (MonoRemoteClass
, proxy_class
));
1024 mono_mb_emit_byte (mb
, CEE_LDIND_REF
);
1025 mono_mb_emit_ldflda (mb
, offset
);
1026 mono_mb_emit_byte (mb
, CEE_LDIND_U1
);
1027 mono_mb_emit_icon (mb
, mask
);
1028 mono_mb_emit_byte (mb
, CEE_AND
);
1029 mono_mb_emit_icon (mb
, 0);
1030 return mono_mb_emit_branch (mb
, branch_code
);
1034 mono_mb_emit_exception_marshal_directive (MonoMethodBuilder
*mb
, const char *msg
)
1036 mono_mb_emit_exception_full (mb
, "System.Runtime.InteropServices", "MarshalDirectiveException", msg
);
1040 mono_type_to_ldind (MonoType
*type
)
1046 switch (type
->type
) {
1048 return CEE_LDIND_I1
;
1050 case MONO_TYPE_BOOLEAN
:
1051 return CEE_LDIND_U1
;
1053 return CEE_LDIND_I2
;
1055 case MONO_TYPE_CHAR
:
1056 return CEE_LDIND_U2
;
1058 return CEE_LDIND_I4
;
1060 return CEE_LDIND_U4
;
1064 case MONO_TYPE_FNPTR
:
1066 case MONO_TYPE_CLASS
:
1067 case MONO_TYPE_STRING
:
1068 case MONO_TYPE_OBJECT
:
1069 case MONO_TYPE_SZARRAY
:
1070 case MONO_TYPE_ARRAY
:
1071 return CEE_LDIND_REF
;
1074 return CEE_LDIND_I8
;
1076 return CEE_LDIND_R4
;
1078 return CEE_LDIND_R8
;
1079 case MONO_TYPE_VALUETYPE
:
1080 if (type
->data
.klass
->enumtype
) {
1081 type
= mono_class_enum_basetype (type
->data
.klass
);
1085 case MONO_TYPE_TYPEDBYREF
:
1087 case MONO_TYPE_GENERICINST
:
1088 type
= &type
->data
.generic_class
->container_class
->byval_arg
;
1091 g_error ("unknown type 0x%02x in type_to_ldind", type
->type
);
1097 mono_type_to_stind (MonoType
*type
)
1103 switch (type
->type
) {
1106 case MONO_TYPE_BOOLEAN
:
1107 return CEE_STIND_I1
;
1110 case MONO_TYPE_CHAR
:
1111 return CEE_STIND_I2
;
1114 return CEE_STIND_I4
;
1118 case MONO_TYPE_FNPTR
:
1120 case MONO_TYPE_CLASS
:
1121 case MONO_TYPE_STRING
:
1122 case MONO_TYPE_OBJECT
:
1123 case MONO_TYPE_SZARRAY
:
1124 case MONO_TYPE_ARRAY
:
1125 return CEE_STIND_REF
;
1128 return CEE_STIND_I8
;
1130 return CEE_STIND_R4
;
1132 return CEE_STIND_R8
;
1133 case MONO_TYPE_VALUETYPE
:
1134 if (type
->data
.klass
->enumtype
) {
1135 type
= mono_class_enum_basetype (type
->data
.klass
);
1139 case MONO_TYPE_TYPEDBYREF
:
1141 case MONO_TYPE_GENERICINST
:
1142 type
= &type
->data
.generic_class
->container_class
->byval_arg
;
1145 g_error ("unknown type 0x%02x in type_to_stind", type
->type
);
1151 emit_ptr_to_object_conv (MonoMethodBuilder
*mb
, MonoType
*type
, MonoMarshalConv conv
, MonoMarshalSpec
*mspec
)
1154 case MONO_MARSHAL_CONV_BOOL_I4
:
1155 mono_mb_emit_ldloc (mb
, 1);
1156 mono_mb_emit_ldloc (mb
, 0);
1157 mono_mb_emit_byte (mb
, CEE_LDIND_I4
);
1158 mono_mb_emit_byte (mb
, CEE_BRFALSE_S
);
1159 mono_mb_emit_byte (mb
, 3);
1160 mono_mb_emit_byte (mb
, CEE_LDC_I4_1
);
1161 mono_mb_emit_byte (mb
, CEE_BR_S
);
1162 mono_mb_emit_byte (mb
, 1);
1163 mono_mb_emit_byte (mb
, CEE_LDC_I4_0
);
1164 mono_mb_emit_byte (mb
, CEE_STIND_I1
);
1166 case MONO_MARSHAL_CONV_BOOL_VARIANTBOOL
:
1167 mono_mb_emit_ldloc (mb
, 1);
1168 mono_mb_emit_ldloc (mb
, 0);
1169 mono_mb_emit_byte (mb
, CEE_LDIND_I2
);
1170 mono_mb_emit_byte (mb
, CEE_BRFALSE_S
);
1171 mono_mb_emit_byte (mb
, 3);
1172 mono_mb_emit_byte (mb
, CEE_LDC_I4_1
);
1173 mono_mb_emit_byte (mb
, CEE_BR_S
);
1174 mono_mb_emit_byte (mb
, 1);
1175 mono_mb_emit_byte (mb
, CEE_LDC_I4_0
);
1176 mono_mb_emit_byte (mb
, CEE_STIND_I1
);
1178 case MONO_MARSHAL_CONV_ARRAY_BYVALARRAY
: {
1179 MonoClass
*eklass
= NULL
;
1182 if (type
->type
== MONO_TYPE_SZARRAY
) {
1183 eklass
= type
->data
.klass
;
1185 g_assert_not_reached ();
1188 esize
= mono_class_native_size (eklass
, NULL
);
1190 /* create a new array */
1191 mono_mb_emit_ldloc (mb
, 1);
1192 mono_mb_emit_icon (mb
, mspec
->data
.array_data
.num_elem
);
1193 mono_mb_emit_op (mb
, CEE_NEWARR
, eklass
);
1194 mono_mb_emit_byte (mb
, CEE_STIND_I
);
1196 if (eklass
->blittable
) {
1197 /* copy the elements */
1198 mono_mb_emit_ldloc (mb
, 1);
1199 mono_mb_emit_byte (mb
, CEE_LDIND_I
);
1200 mono_mb_emit_icon (mb
, G_STRUCT_OFFSET (MonoArray
, vector
));
1201 mono_mb_emit_byte (mb
, CEE_ADD
);
1202 mono_mb_emit_ldloc (mb
, 0);
1203 mono_mb_emit_icon (mb
, mspec
->data
.array_data
.num_elem
* esize
);
1204 mono_mb_emit_byte (mb
, CEE_PREFIX1
);
1205 mono_mb_emit_byte (mb
, CEE_CPBLK
);
1208 int array_var
, src_var
, dst_var
, index_var
;
1209 guint32 label2
, label3
;
1211 array_var
= mono_mb_add_local (mb
, &mono_defaults
.object_class
->byval_arg
);
1212 src_var
= mono_mb_add_local (mb
, &mono_defaults
.int_class
->byval_arg
);
1213 dst_var
= mono_mb_add_local (mb
, &mono_defaults
.int_class
->byval_arg
);
1216 mono_mb_emit_ldloc (mb
, 1);
1217 mono_mb_emit_byte (mb
, CEE_LDIND_REF
);
1218 mono_mb_emit_stloc (mb
, array_var
);
1220 /* save the old src pointer */
1221 mono_mb_emit_ldloc (mb
, 0);
1222 mono_mb_emit_stloc (mb
, src_var
);
1223 /* save the old dst pointer */
1224 mono_mb_emit_ldloc (mb
, 1);
1225 mono_mb_emit_stloc (mb
, dst_var
);
1227 /* Emit marshalling loop */
1228 index_var
= mono_mb_add_local (mb
, &mono_defaults
.int_class
->byval_arg
);
1229 mono_mb_emit_byte (mb
, CEE_LDC_I4_0
);
1230 mono_mb_emit_stloc (mb
, index_var
);
1233 label2
= mono_mb_get_label (mb
);
1234 mono_mb_emit_ldloc (mb
, index_var
);
1235 mono_mb_emit_ldloc (mb
, array_var
);
1236 mono_mb_emit_byte (mb
, CEE_LDLEN
);
1237 label3
= mono_mb_emit_branch (mb
, CEE_BGE
);
1239 /* src is already set */
1242 mono_mb_emit_ldloc (mb
, array_var
);
1243 mono_mb_emit_ldloc (mb
, index_var
);
1244 mono_mb_emit_op (mb
, CEE_LDELEMA
, eklass
);
1245 mono_mb_emit_stloc (mb
, 1);
1247 /* Do the conversion */
1248 emit_struct_conv (mb
, eklass
, TRUE
);
1251 mono_mb_emit_add_to_local (mb
, index_var
, 1);
1253 mono_mb_emit_branch_label (mb
, CEE_BR
, label2
);
1255 mono_mb_patch_branch (mb
, label3
);
1257 /* restore the old src pointer */
1258 mono_mb_emit_ldloc (mb
, src_var
);
1259 mono_mb_emit_stloc (mb
, 0);
1260 /* restore the old dst pointer */
1261 mono_mb_emit_ldloc (mb
, dst_var
);
1262 mono_mb_emit_stloc (mb
, 1);
1266 case MONO_MARSHAL_CONV_ARRAY_BYVALCHARARRAY
: {
1267 MonoClass
*eclass
= mono_defaults
.char_class
;
1269 /* create a new array */
1270 mono_mb_emit_ldloc (mb
, 1);
1271 mono_mb_emit_icon (mb
, mspec
->data
.array_data
.num_elem
);
1272 mono_mb_emit_op (mb
, CEE_NEWARR
, eclass
);
1273 mono_mb_emit_byte (mb
, CEE_STIND_REF
);
1275 mono_mb_emit_ldloc (mb
, 1);
1276 mono_mb_emit_byte (mb
, CEE_LDIND_REF
);
1277 mono_mb_emit_ldloc (mb
, 0);
1278 mono_mb_emit_ptr (mb
, mono_defaults
.byte_class
);
1279 mono_mb_emit_icon (mb
, mspec
->data
.array_data
.num_elem
);
1280 mono_mb_emit_icall (mb
, mono_byvalarray_to_array
);
1283 case MONO_MARSHAL_CONV_STR_BYVALSTR
:
1284 mono_mb_emit_ldloc (mb
, 1);
1285 mono_mb_emit_ldloc (mb
, 0);
1286 mono_mb_emit_icall (mb
, mono_string_new_wrapper
);
1287 mono_mb_emit_byte (mb
, CEE_STIND_REF
);
1289 case MONO_MARSHAL_CONV_STR_BYVALWSTR
:
1290 mono_mb_emit_ldloc (mb
, 1);
1291 mono_mb_emit_ldloc (mb
, 0);
1292 mono_mb_emit_icall (mb
, mono_string_from_utf16
);
1293 mono_mb_emit_byte (mb
, CEE_STIND_REF
);
1295 case MONO_MARSHAL_CONV_STR_LPTSTR
:
1296 mono_mb_emit_ldloc (mb
, 1);
1297 mono_mb_emit_ldloc (mb
, 0);
1298 mono_mb_emit_byte (mb
, CEE_LDIND_I
);
1300 mono_mb_emit_icall (mb
, mono_string_from_utf16
);
1302 mono_mb_emit_icall (mb
, mono_string_new_wrapper
);
1304 mono_mb_emit_byte (mb
, CEE_STIND_REF
);
1306 case MONO_MARSHAL_CONV_STR_LPSTR
:
1307 mono_mb_emit_ldloc (mb
, 1);
1308 mono_mb_emit_ldloc (mb
, 0);
1309 mono_mb_emit_byte (mb
, CEE_LDIND_I
);
1310 mono_mb_emit_icall (mb
, mono_string_new_wrapper
);
1311 mono_mb_emit_byte (mb
, CEE_STIND_REF
);
1313 case MONO_MARSHAL_CONV_STR_LPWSTR
:
1314 mono_mb_emit_ldloc (mb
, 1);
1315 mono_mb_emit_ldloc (mb
, 0);
1316 mono_mb_emit_byte (mb
, CEE_LDIND_I
);
1317 mono_mb_emit_icall (mb
, mono_string_from_utf16
);
1318 mono_mb_emit_byte (mb
, CEE_STIND_REF
);
1320 case MONO_MARSHAL_CONV_OBJECT_STRUCT
: {
1321 MonoClass
*klass
= mono_class_from_mono_type (type
);
1322 int src_var
, dst_var
;
1324 src_var
= mono_mb_add_local (mb
, &mono_defaults
.int_class
->byval_arg
);
1325 dst_var
= mono_mb_add_local (mb
, &mono_defaults
.int_class
->byval_arg
);
1327 /* *dst = new object */
1328 mono_mb_emit_ldloc (mb
, 1);
1329 mono_mb_emit_byte (mb
, MONO_CUSTOM_PREFIX
);
1330 mono_mb_emit_op (mb
, CEE_MONO_NEWOBJ
, klass
);
1331 mono_mb_emit_byte (mb
, CEE_STIND_REF
);
1333 /* save the old src pointer */
1334 mono_mb_emit_ldloc (mb
, 0);
1335 mono_mb_emit_stloc (mb
, src_var
);
1336 /* save the old dst pointer */
1337 mono_mb_emit_ldloc (mb
, 1);
1338 mono_mb_emit_stloc (mb
, dst_var
);
1340 /* dst = pointer to newly created object data */
1341 mono_mb_emit_ldloc (mb
, 1);
1342 mono_mb_emit_byte (mb
, CEE_LDIND_I
);
1343 mono_mb_emit_icon (mb
, sizeof (MonoObject
));
1344 mono_mb_emit_byte (mb
, CEE_ADD
);
1345 mono_mb_emit_stloc (mb
, 1);
1347 emit_struct_conv (mb
, klass
, TRUE
);
1349 /* restore the old src pointer */
1350 mono_mb_emit_ldloc (mb
, src_var
);
1351 mono_mb_emit_stloc (mb
, 0);
1352 /* restore the old dst pointer */
1353 mono_mb_emit_ldloc (mb
, dst_var
);
1354 mono_mb_emit_stloc (mb
, 1);
1357 case MONO_MARSHAL_CONV_DEL_FTN
: {
1358 MonoClass
*klass
= mono_class_from_mono_type (type
);
1360 mono_mb_emit_ldloc (mb
, 1);
1361 mono_mb_emit_byte (mb
, MONO_CUSTOM_PREFIX
);
1362 mono_mb_emit_op (mb
, CEE_MONO_CLASSCONST
, klass
);
1363 mono_mb_emit_ldloc (mb
, 0);
1364 mono_mb_emit_byte (mb
, CEE_LDIND_I
);
1365 mono_mb_emit_icall (mb
, mono_ftnptr_to_delegate
);
1366 mono_mb_emit_byte (mb
, CEE_STIND_REF
);
1369 case MONO_MARSHAL_CONV_ARRAY_LPARRAY
:
1370 g_error ("Structure field of type %s can't be marshalled as LPArray", mono_class_from_mono_type (type
)->name
);
1374 case MONO_MARSHAL_CONV_OBJECT_INTERFACE
:
1375 case MONO_MARSHAL_CONV_OBJECT_IUNKNOWN
:
1376 case MONO_MARSHAL_CONV_OBJECT_IDISPATCH
:
1377 mono_cominterop_emit_ptr_to_object_conv (mb
, type
, conv
, mspec
);
1379 #endif /* DISABLE_COM */
1381 case MONO_MARSHAL_CONV_SAFEHANDLE
: {
1383 * Passing SafeHandles as ref does not allow the unmanaged code
1384 * to change the SafeHandle value. If the value is changed,
1385 * we should issue a diagnostic exception (NotSupportedException)
1386 * that informs the user that changes to handles in unmanaged code
1389 * Since we currently have no access to the original
1390 * SafeHandle that was used during the marshalling,
1391 * for now we just ignore this, and ignore/discard any
1392 * changes that might have happened to the handle.
1397 case MONO_MARSHAL_CONV_HANDLEREF
: {
1399 * Passing HandleRefs in a struct that is ref()ed does not
1400 * copy the values back to the HandleRef
1405 case MONO_MARSHAL_CONV_STR_BSTR
:
1406 case MONO_MARSHAL_CONV_STR_ANSIBSTR
:
1407 case MONO_MARSHAL_CONV_STR_TBSTR
:
1408 case MONO_MARSHAL_CONV_ARRAY_SAVEARRAY
:
1410 char *msg
= g_strdup_printf ("marshaling conversion %d not implemented", conv
);
1412 mono_mb_emit_exception_marshal_directive (mb
, msg
);
1420 conv_to_icall (MonoMarshalConv conv
)
1423 case MONO_MARSHAL_CONV_STR_LPWSTR
:
1424 return mono_marshal_string_to_utf16
;
1425 case MONO_MARSHAL_CONV_LPWSTR_STR
:
1426 return mono_string_from_utf16
;
1427 case MONO_MARSHAL_CONV_LPSTR_STR
:
1428 return mono_string_new_wrapper
;
1429 case MONO_MARSHAL_CONV_STR_LPTSTR
:
1431 return mono_marshal_string_to_utf16
;
1433 return mono_string_to_lpstr
;
1435 case MONO_MARSHAL_CONV_STR_LPSTR
:
1436 return mono_string_to_lpstr
;
1437 case MONO_MARSHAL_CONV_STR_BSTR
:
1438 return mono_string_to_bstr
;
1439 case MONO_MARSHAL_CONV_BSTR_STR
:
1440 return mono_string_from_bstr
;
1441 case MONO_MARSHAL_CONV_STR_TBSTR
:
1442 case MONO_MARSHAL_CONV_STR_ANSIBSTR
:
1443 return mono_string_to_ansibstr
;
1444 case MONO_MARSHAL_CONV_SB_LPSTR
:
1445 return mono_string_builder_to_utf8
;
1446 case MONO_MARSHAL_CONV_SB_LPTSTR
:
1448 return mono_string_builder_to_utf16
;
1450 return mono_string_builder_to_utf8
;
1452 case MONO_MARSHAL_CONV_SB_LPWSTR
:
1453 return mono_string_builder_to_utf16
;
1454 case MONO_MARSHAL_CONV_ARRAY_SAVEARRAY
:
1455 return mono_array_to_savearray
;
1456 case MONO_MARSHAL_CONV_ARRAY_LPARRAY
:
1457 return mono_array_to_lparray
;
1458 case MONO_MARSHAL_FREE_LPARRAY
:
1459 return mono_free_lparray
;
1460 case MONO_MARSHAL_CONV_DEL_FTN
:
1461 return mono_delegate_to_ftnptr
;
1462 case MONO_MARSHAL_CONV_FTN_DEL
:
1463 return mono_ftnptr_to_delegate
;
1464 case MONO_MARSHAL_CONV_LPSTR_SB
:
1465 return mono_string_utf8_to_builder
;
1466 case MONO_MARSHAL_CONV_LPTSTR_SB
:
1468 return mono_string_utf16_to_builder
;
1470 return mono_string_utf8_to_builder
;
1472 case MONO_MARSHAL_CONV_LPWSTR_SB
:
1473 return mono_string_utf16_to_builder
;
1474 case MONO_MARSHAL_FREE_ARRAY
:
1475 return mono_marshal_free_array
;
1476 case MONO_MARSHAL_CONV_STR_BYVALSTR
:
1477 return mono_string_to_byvalstr
;
1478 case MONO_MARSHAL_CONV_STR_BYVALWSTR
:
1479 return mono_string_to_byvalwstr
;
1481 g_assert_not_reached ();
1488 emit_object_to_ptr_conv (MonoMethodBuilder
*mb
, MonoType
*type
, MonoMarshalConv conv
, MonoMarshalSpec
*mspec
)
1493 case MONO_MARSHAL_CONV_BOOL_I4
:
1494 mono_mb_emit_ldloc (mb
, 1);
1495 mono_mb_emit_ldloc (mb
, 0);
1496 mono_mb_emit_byte (mb
, CEE_LDIND_U1
);
1497 mono_mb_emit_byte (mb
, CEE_STIND_I4
);
1499 case MONO_MARSHAL_CONV_BOOL_VARIANTBOOL
:
1500 mono_mb_emit_ldloc (mb
, 1);
1501 mono_mb_emit_ldloc (mb
, 0);
1502 mono_mb_emit_byte (mb
, CEE_LDIND_U1
);
1503 mono_mb_emit_byte (mb
, CEE_NEG
);
1504 mono_mb_emit_byte (mb
, CEE_STIND_I2
);
1506 case MONO_MARSHAL_CONV_STR_LPWSTR
:
1507 case MONO_MARSHAL_CONV_STR_LPSTR
:
1508 case MONO_MARSHAL_CONV_STR_LPTSTR
:
1509 case MONO_MARSHAL_CONV_STR_BSTR
:
1510 case MONO_MARSHAL_CONV_STR_ANSIBSTR
:
1511 case MONO_MARSHAL_CONV_STR_TBSTR
: {
1514 /* free space if free == true */
1515 mono_mb_emit_ldloc (mb
, 2);
1516 pos
= mono_mb_emit_short_branch (mb
, CEE_BRFALSE_S
);
1517 mono_mb_emit_ldloc (mb
, 1);
1518 mono_mb_emit_byte (mb
, CEE_LDIND_I
);
1519 mono_mb_emit_icall (mb
, g_free
);
1520 mono_mb_patch_short_branch (mb
, pos
);
1522 mono_mb_emit_ldloc (mb
, 1);
1523 mono_mb_emit_ldloc (mb
, 0);
1524 mono_mb_emit_byte (mb
, CEE_LDIND_REF
);
1525 mono_mb_emit_icall (mb
, conv_to_icall (conv
));
1526 mono_mb_emit_byte (mb
, CEE_STIND_I
);
1529 case MONO_MARSHAL_CONV_ARRAY_SAVEARRAY
:
1530 case MONO_MARSHAL_CONV_ARRAY_LPARRAY
:
1531 case MONO_MARSHAL_CONV_DEL_FTN
:
1532 mono_mb_emit_ldloc (mb
, 1);
1533 mono_mb_emit_ldloc (mb
, 0);
1534 mono_mb_emit_byte (mb
, CEE_LDIND_REF
);
1535 mono_mb_emit_icall (mb
, conv_to_icall (conv
));
1536 mono_mb_emit_byte (mb
, CEE_STIND_I
);
1538 case MONO_MARSHAL_CONV_STR_BYVALSTR
:
1539 case MONO_MARSHAL_CONV_STR_BYVALWSTR
: {
1542 mono_mb_emit_ldloc (mb
, 1); /* dst */
1543 mono_mb_emit_ldloc (mb
, 0);
1544 mono_mb_emit_byte (mb
, CEE_LDIND_REF
); /* src String */
1545 mono_mb_emit_icon (mb
, mspec
->data
.array_data
.num_elem
);
1546 mono_mb_emit_icall (mb
, conv_to_icall (conv
));
1549 case MONO_MARSHAL_CONV_ARRAY_BYVALARRAY
: {
1550 MonoClass
*eklass
= NULL
;
1553 if (type
->type
== MONO_TYPE_SZARRAY
) {
1554 eklass
= type
->data
.klass
;
1556 g_assert_not_reached ();
1559 if (eklass
->valuetype
)
1560 esize
= mono_class_native_size (eklass
, NULL
);
1562 esize
= sizeof (gpointer
);
1564 mono_mb_emit_ldloc (mb
, 0);
1565 mono_mb_emit_byte (mb
, CEE_LDIND_REF
);
1566 pos
= mono_mb_emit_branch (mb
, CEE_BRFALSE
);
1568 if (eklass
->blittable
) {
1569 mono_mb_emit_ldloc (mb
, 1);
1570 mono_mb_emit_ldloc (mb
, 0);
1571 mono_mb_emit_byte (mb
, CEE_LDIND_REF
);
1572 mono_mb_emit_ldflda (mb
, G_STRUCT_OFFSET (MonoArray
, vector
));
1573 mono_mb_emit_icon (mb
, mspec
->data
.array_data
.num_elem
* esize
);
1574 mono_mb_emit_byte (mb
, CEE_PREFIX1
);
1575 mono_mb_emit_byte (mb
, CEE_CPBLK
);
1577 int array_var
, src_var
, dst_var
, index_var
;
1578 guint32 label2
, label3
;
1580 array_var
= mono_mb_add_local (mb
, &mono_defaults
.object_class
->byval_arg
);
1581 src_var
= mono_mb_add_local (mb
, &mono_defaults
.int_class
->byval_arg
);
1582 dst_var
= mono_mb_add_local (mb
, &mono_defaults
.int_class
->byval_arg
);
1585 mono_mb_emit_ldloc (mb
, 0);
1586 mono_mb_emit_byte (mb
, CEE_LDIND_REF
);
1587 mono_mb_emit_stloc (mb
, array_var
);
1589 /* save the old src pointer */
1590 mono_mb_emit_ldloc (mb
, 0);
1591 mono_mb_emit_stloc (mb
, src_var
);
1592 /* save the old dst pointer */
1593 mono_mb_emit_ldloc (mb
, 1);
1594 mono_mb_emit_stloc (mb
, dst_var
);
1596 /* Emit marshalling loop */
1597 index_var
= mono_mb_add_local (mb
, &mono_defaults
.int_class
->byval_arg
);
1598 mono_mb_emit_byte (mb
, CEE_LDC_I4_0
);
1599 mono_mb_emit_stloc (mb
, index_var
);
1602 label2
= mono_mb_get_label (mb
);
1603 mono_mb_emit_ldloc (mb
, index_var
);
1604 mono_mb_emit_ldloc (mb
, array_var
);
1605 mono_mb_emit_byte (mb
, CEE_LDLEN
);
1606 label3
= mono_mb_emit_branch (mb
, CEE_BGE
);
1609 mono_mb_emit_ldloc (mb
, array_var
);
1610 mono_mb_emit_ldloc (mb
, index_var
);
1611 mono_mb_emit_op (mb
, CEE_LDELEMA
, eklass
);
1612 mono_mb_emit_stloc (mb
, 0);
1614 /* dst is already set */
1616 /* Do the conversion */
1617 emit_struct_conv (mb
, eklass
, FALSE
);
1620 mono_mb_emit_add_to_local (mb
, index_var
, 1);
1622 mono_mb_emit_branch_label (mb
, CEE_BR
, label2
);
1624 mono_mb_patch_branch (mb
, label3
);
1626 /* restore the old src pointer */
1627 mono_mb_emit_ldloc (mb
, src_var
);
1628 mono_mb_emit_stloc (mb
, 0);
1629 /* restore the old dst pointer */
1630 mono_mb_emit_ldloc (mb
, dst_var
);
1631 mono_mb_emit_stloc (mb
, 1);
1634 mono_mb_patch_branch (mb
, pos
);
1637 case MONO_MARSHAL_CONV_ARRAY_BYVALCHARARRAY
: {
1638 mono_mb_emit_ldloc (mb
, 0);
1639 mono_mb_emit_byte (mb
, CEE_LDIND_REF
);
1640 pos
= mono_mb_emit_short_branch (mb
, CEE_BRFALSE_S
);
1642 mono_mb_emit_ldloc (mb
, 1);
1643 mono_mb_emit_ldloc (mb
, 0);
1644 mono_mb_emit_byte (mb
, CEE_LDIND_REF
);
1645 mono_mb_emit_ptr (mb
, mono_defaults
.byte_class
);
1646 mono_mb_emit_icon (mb
, mspec
->data
.array_data
.num_elem
);
1647 mono_mb_emit_icall (mb
, mono_array_to_byvalarray
);
1648 mono_mb_patch_short_branch (mb
, pos
);
1651 case MONO_MARSHAL_CONV_OBJECT_STRUCT
: {
1652 int src_var
, dst_var
;
1654 src_var
= mono_mb_add_local (mb
, &mono_defaults
.int_class
->byval_arg
);
1655 dst_var
= mono_mb_add_local (mb
, &mono_defaults
.int_class
->byval_arg
);
1657 mono_mb_emit_ldloc (mb
, 0);
1658 mono_mb_emit_byte (mb
, CEE_LDIND_I
);
1659 pos
= mono_mb_emit_branch (mb
, CEE_BRFALSE
);
1661 /* save the old src pointer */
1662 mono_mb_emit_ldloc (mb
, 0);
1663 mono_mb_emit_stloc (mb
, src_var
);
1664 /* save the old dst pointer */
1665 mono_mb_emit_ldloc (mb
, 1);
1666 mono_mb_emit_stloc (mb
, dst_var
);
1668 /* src = pointer to object data */
1669 mono_mb_emit_ldloc (mb
, 0);
1670 mono_mb_emit_byte (mb
, CEE_LDIND_I
);
1671 mono_mb_emit_icon (mb
, sizeof (MonoObject
));
1672 mono_mb_emit_byte (mb
, CEE_ADD
);
1673 mono_mb_emit_stloc (mb
, 0);
1675 emit_struct_conv (mb
, mono_class_from_mono_type (type
), FALSE
);
1677 /* restore the old src pointer */
1678 mono_mb_emit_ldloc (mb
, src_var
);
1679 mono_mb_emit_stloc (mb
, 0);
1680 /* restore the old dst pointer */
1681 mono_mb_emit_ldloc (mb
, dst_var
);
1682 mono_mb_emit_stloc (mb
, 1);
1684 mono_mb_patch_branch (mb
, pos
);
1689 case MONO_MARSHAL_CONV_OBJECT_INTERFACE
:
1690 case MONO_MARSHAL_CONV_OBJECT_IDISPATCH
:
1691 case MONO_MARSHAL_CONV_OBJECT_IUNKNOWN
:
1692 mono_cominterop_emit_object_to_ptr_conv (mb
, type
, conv
, mspec
);
1694 #endif /* DISABLE_COM */
1696 case MONO_MARSHAL_CONV_SAFEHANDLE
: {
1697 int dar_release_slot
, pos
;
1699 dar_release_slot
= mono_mb_add_local (mb
, &mono_defaults
.boolean_class
->byval_arg
);
1702 * The following is ifdefed-out, because I have no way of doing the
1703 * DangerousRelease when destroying the structure
1706 /* set release = false */
1707 mono_mb_emit_icon (mb
, 0);
1708 mono_mb_emit_stloc (mb
, dar_release_slot
);
1709 if (!sh_dangerous_add_ref
)
1710 init_safe_handle ();
1712 /* safehandle.DangerousAddRef (ref release) */
1713 mono_mb_emit_ldloc (mb
, 0); /* the source */
1714 mono_mb_emit_byte (mb
, CEE_LDIND_I
);
1715 mono_mb_emit_ldloc_addr (mb
, dar_release_slot
);
1716 mono_mb_emit_managed_call (mb
, sh_dangerous_add_ref
, NULL
);
1718 mono_mb_emit_ldloc (mb
, 0);
1719 mono_mb_emit_byte (mb
, CEE_LDIND_I
);
1720 pos
= mono_mb_emit_branch (mb
, CEE_BRTRUE
);
1721 mono_mb_emit_exception (mb
, "ArgumentNullException", NULL
);
1722 mono_mb_patch_branch (mb
, pos
);
1724 /* Pull the handle field from SafeHandle */
1725 mono_mb_emit_ldloc (mb
, 1);
1726 mono_mb_emit_ldloc (mb
, 0);
1727 mono_mb_emit_byte (mb
, CEE_LDIND_I
);
1728 mono_mb_emit_ldflda (mb
, G_STRUCT_OFFSET (MonoSafeHandle
, handle
));
1729 mono_mb_emit_byte (mb
, CEE_LDIND_I
);
1730 mono_mb_emit_byte (mb
, CEE_STIND_I
);
1734 case MONO_MARSHAL_CONV_HANDLEREF
: {
1735 mono_mb_emit_ldloc (mb
, 1);
1736 mono_mb_emit_ldloc (mb
, 0);
1737 mono_mb_emit_icon (mb
, G_STRUCT_OFFSET (MonoHandleRef
, handle
));
1738 mono_mb_emit_byte (mb
, CEE_ADD
);
1739 mono_mb_emit_byte (mb
, CEE_LDIND_I
);
1740 mono_mb_emit_byte (mb
, CEE_STIND_I
);
1745 char *msg
= g_strdup_printf ("marshalling conversion %d not implemented", conv
);
1746 MonoException
*exc
= mono_get_exception_not_implemented (msg
);
1747 g_warning ("%s", msg
);
1749 mono_raise_exception (exc
);
1755 emit_struct_conv (MonoMethodBuilder
*mb
, MonoClass
*klass
, gboolean to_object
)
1757 MonoMarshalType
*info
;
1761 emit_struct_conv(mb
, klass
->parent
, to_object
);
1763 info
= mono_marshal_load_type_info (klass
);
1765 if (info
->native_size
== 0)
1768 if (klass
->blittable
) {
1769 int msize
= mono_class_value_size (klass
, NULL
);
1770 g_assert (msize
== info
->native_size
);
1771 mono_mb_emit_ldloc (mb
, 1);
1772 mono_mb_emit_ldloc (mb
, 0);
1773 mono_mb_emit_icon (mb
, msize
);
1774 mono_mb_emit_byte (mb
, CEE_PREFIX1
);
1775 mono_mb_emit_byte (mb
, CEE_CPBLK
);
1777 mono_mb_emit_add_to_local (mb
, 0, msize
);
1778 mono_mb_emit_add_to_local (mb
, 1, msize
);
1782 if (klass
!= mono_defaults
.safehandle_class
) {
1783 if ((klass
->flags
& TYPE_ATTRIBUTE_LAYOUT_MASK
) == TYPE_ATTRIBUTE_AUTO_LAYOUT
) {
1784 char *msg
= g_strdup_printf ("Type %s which is passed to unmanaged code must have a StructLayout attribute.",
1785 mono_type_full_name (&klass
->byval_arg
));
1786 mono_mb_emit_exception_marshal_directive (mb
, msg
);
1791 for (i
= 0; i
< info
->num_fields
; i
++) {
1792 MonoMarshalNative ntype
;
1793 MonoMarshalConv conv
;
1794 MonoType
*ftype
= info
->fields
[i
].field
->type
;
1797 gboolean last_field
= i
< (info
->num_fields
-1) ? 0 : 1;
1799 if (ftype
->attrs
& FIELD_ATTRIBUTE_STATIC
)
1802 ntype
= mono_type_to_unmanaged (ftype
, info
->fields
[i
].mspec
, TRUE
, klass
->unicode
, &conv
);
1805 msize
= klass
->instance_size
- info
->fields
[i
].field
->offset
;
1806 usize
= info
->native_size
- info
->fields
[i
].offset
;
1808 msize
= info
->fields
[i
+ 1].field
->offset
- info
->fields
[i
].field
->offset
;
1809 usize
= info
->fields
[i
+ 1].offset
- info
->fields
[i
].offset
;
1812 if (klass
!= mono_defaults
.safehandle_class
){
1814 * FIXME: Should really check for usize==0 and msize>0, but we apply
1815 * the layout to the managed structure as well.
1818 if (((klass
->flags
& TYPE_ATTRIBUTE_LAYOUT_MASK
) == TYPE_ATTRIBUTE_EXPLICIT_LAYOUT
) && (usize
== 0)) {
1819 if (MONO_TYPE_IS_REFERENCE (info
->fields
[i
].field
->type
) ||
1820 ((!last_field
&& MONO_TYPE_IS_REFERENCE (info
->fields
[i
+ 1].field
->type
))))
1821 g_error ("Type %s which has an [ExplicitLayout] attribute cannot have a "
1822 "reference field at the same offset as another field.",
1823 mono_type_full_name (&klass
->byval_arg
));
1828 case MONO_MARSHAL_CONV_NONE
: {
1831 if (ftype
->byref
|| ftype
->type
== MONO_TYPE_I
||
1832 ftype
->type
== MONO_TYPE_U
) {
1833 mono_mb_emit_ldloc (mb
, 1);
1834 mono_mb_emit_ldloc (mb
, 0);
1835 mono_mb_emit_byte (mb
, CEE_LDIND_I
);
1836 mono_mb_emit_byte (mb
, CEE_STIND_I
);
1847 case MONO_TYPE_BOOLEAN
:
1850 case MONO_TYPE_CHAR
:
1856 mono_mb_emit_ldloc (mb
, 1);
1857 mono_mb_emit_ldloc (mb
, 0);
1858 mono_mb_emit_byte (mb
, mono_type_to_ldind (ftype
));
1859 mono_mb_emit_byte (mb
, mono_type_to_stind (ftype
));
1861 case MONO_TYPE_VALUETYPE
: {
1862 int src_var
, dst_var
;
1864 if (ftype
->data
.klass
->enumtype
) {
1865 ftype
= mono_class_enum_basetype (ftype
->data
.klass
);
1869 src_var
= mono_mb_add_local (mb
, &mono_defaults
.int_class
->byval_arg
);
1870 dst_var
= mono_mb_add_local (mb
, &mono_defaults
.int_class
->byval_arg
);
1872 /* save the old src pointer */
1873 mono_mb_emit_ldloc (mb
, 0);
1874 mono_mb_emit_stloc (mb
, src_var
);
1875 /* save the old dst pointer */
1876 mono_mb_emit_ldloc (mb
, 1);
1877 mono_mb_emit_stloc (mb
, dst_var
);
1879 emit_struct_conv (mb
, ftype
->data
.klass
, to_object
);
1881 /* restore the old src pointer */
1882 mono_mb_emit_ldloc (mb
, src_var
);
1883 mono_mb_emit_stloc (mb
, 0);
1884 /* restore the old dst pointer */
1885 mono_mb_emit_ldloc (mb
, dst_var
);
1886 mono_mb_emit_stloc (mb
, 1);
1889 case MONO_TYPE_OBJECT
: {
1890 mono_init_com_types ();
1892 static MonoMethod
*variant_clear
= NULL
;
1893 static MonoMethod
*get_object_for_native_variant
= NULL
;
1896 variant_clear
= mono_class_get_method_from_name (mono_defaults
.variant_class
, "Clear", 0);
1897 if (!get_object_for_native_variant
)
1898 get_object_for_native_variant
= mono_class_get_method_from_name (mono_defaults
.marshal_class
, "GetObjectForNativeVariant", 1);
1899 mono_mb_emit_ldloc (mb
, 1);
1900 mono_mb_emit_ldloc (mb
, 0);
1901 mono_mb_emit_managed_call (mb
, get_object_for_native_variant
, NULL
);
1902 mono_mb_emit_byte (mb
, CEE_STIND_REF
);
1904 mono_mb_emit_ldloc (mb
, 0);
1905 mono_mb_emit_managed_call (mb
, variant_clear
, NULL
);
1908 static MonoMethod
*get_native_variant_for_object
= NULL
;
1910 if (!get_native_variant_for_object
)
1911 get_native_variant_for_object
= mono_class_get_method_from_name (mono_defaults
.marshal_class
, "GetNativeVariantForObject", 2);
1913 mono_mb_emit_ldloc (mb
, 0);
1914 mono_mb_emit_byte(mb
, CEE_LDIND_REF
);
1915 mono_mb_emit_ldloc (mb
, 1);
1916 mono_mb_emit_managed_call (mb
, get_native_variant_for_object
, NULL
);
1922 g_warning ("marshaling type %02x not implemented", ftype
->type
);
1923 g_assert_not_reached ();
1928 int src_var
, dst_var
;
1930 src_var
= mono_mb_add_local (mb
, &mono_defaults
.int_class
->byval_arg
);
1931 dst_var
= mono_mb_add_local (mb
, &mono_defaults
.int_class
->byval_arg
);
1933 /* save the old src pointer */
1934 mono_mb_emit_ldloc (mb
, 0);
1935 mono_mb_emit_stloc (mb
, src_var
);
1936 /* save the old dst pointer */
1937 mono_mb_emit_ldloc (mb
, 1);
1938 mono_mb_emit_stloc (mb
, dst_var
);
1941 emit_ptr_to_object_conv (mb
, ftype
, conv
, info
->fields
[i
].mspec
);
1943 emit_object_to_ptr_conv (mb
, ftype
, conv
, info
->fields
[i
].mspec
);
1945 /* restore the old src pointer */
1946 mono_mb_emit_ldloc (mb
, src_var
);
1947 mono_mb_emit_stloc (mb
, 0);
1948 /* restore the old dst pointer */
1949 mono_mb_emit_ldloc (mb
, dst_var
);
1950 mono_mb_emit_stloc (mb
, 1);
1955 mono_mb_emit_add_to_local (mb
, 0, usize
);
1956 mono_mb_emit_add_to_local (mb
, 1, msize
);
1958 mono_mb_emit_add_to_local (mb
, 0, msize
);
1959 mono_mb_emit_add_to_local (mb
, 1, usize
);
1965 emit_struct_free (MonoMethodBuilder
*mb
, MonoClass
*klass
, int struct_var
)
1967 /* Call DestroyStructure */
1968 /* FIXME: Only do this if needed */
1969 mono_mb_emit_byte (mb
, MONO_CUSTOM_PREFIX
);
1970 mono_mb_emit_op (mb
, CEE_MONO_CLASSCONST
, klass
);
1971 mono_mb_emit_ldloc (mb
, struct_var
);
1972 mono_mb_emit_icall (mb
, mono_struct_delete_old
);
1976 emit_thread_interrupt_checkpoint_call (MonoMethodBuilder
*mb
, gpointer checkpoint_func
)
1980 mono_mb_emit_ptr (mb
, (gpointer
) mono_thread_interruption_request_flag ());
1981 mono_mb_emit_byte (mb
, CEE_LDIND_U4
);
1982 pos_noabort
= mono_mb_emit_branch (mb
, CEE_BRFALSE
);
1984 mono_mb_emit_byte (mb
, MONO_CUSTOM_PREFIX
);
1985 mono_mb_emit_byte (mb
, CEE_MONO_NOT_TAKEN
);
1987 mono_mb_emit_icall (mb
, checkpoint_func
);
1989 mono_mb_patch_branch (mb
, pos_noabort
);
1993 emit_thread_interrupt_checkpoint (MonoMethodBuilder
*mb
)
1995 if (strstr (mb
->name
, "mono_thread_interruption_checkpoint"))
1998 emit_thread_interrupt_checkpoint_call (mb
, mono_thread_interruption_checkpoint
);
2002 emit_thread_force_interrupt_checkpoint (MonoMethodBuilder
*mb
)
2004 emit_thread_interrupt_checkpoint_call (mb
, mono_thread_force_interruption_checkpoint
);
2008 mono_marshal_emit_thread_interrupt_checkpoint (MonoMethodBuilder
*mb
)
2010 emit_thread_interrupt_checkpoint (mb
);
2013 static MonoAsyncResult
*
2014 mono_delegate_begin_invoke (MonoDelegate
*delegate
, gpointer
*params
)
2016 MonoMethodMessage
*msg
;
2017 MonoDelegate
*async_callback
;
2018 MonoMulticastDelegate
*mcast_delegate
;
2022 MonoMethod
*method
= NULL
, *method2
= NULL
;
2024 g_assert (delegate
);
2025 mcast_delegate
= (MonoMulticastDelegate
*) delegate
;
2026 if (mcast_delegate
->prev
!= NULL
)
2027 mono_raise_exception (mono_get_exception_argument (NULL
, "The delegate must have only one target"));
2029 if (delegate
->target
&& mono_object_class (delegate
->target
) == mono_defaults
.transparent_proxy_class
) {
2031 MonoTransparentProxy
* tp
= (MonoTransparentProxy
*)delegate
->target
;
2032 if (!tp
->remote_class
->proxy_class
->contextbound
|| tp
->rp
->context
!= (MonoObject
*) mono_context_get ()) {
2034 /* If the target is a proxy, make a direct call. Is proxy's work
2035 // to make the call asynchronous.
2037 MonoAsyncResult
*ares
;
2039 MonoArray
*out_args
;
2040 method
= delegate
->method
;
2042 msg
= mono_method_call_message_new (mono_marshal_method_from_wrapper (method
), params
, NULL
, &async_callback
, &state
);
2043 ares
= mono_async_result_new (mono_domain_get (), NULL
, state
, NULL
, NULL
);
2044 MONO_OBJECT_SETREF (ares
, async_delegate
, (MonoObject
*)delegate
);
2045 MONO_OBJECT_SETREF (ares
, async_callback
, (MonoObject
*)async_callback
);
2046 MONO_OBJECT_SETREF (msg
, async_result
, ares
);
2047 msg
->call_type
= CallType_BeginInvoke
;
2050 mono_remoting_invoke ((MonoObject
*)tp
->rp
, msg
, &exc
, &out_args
);
2052 mono_raise_exception ((MonoException
*) exc
);
2057 klass
= delegate
->object
.vtable
->klass
;
2059 method
= mono_get_delegate_invoke (klass
);
2060 method2
= mono_class_get_method_from_name (klass
, "BeginInvoke", -1);
2063 g_assert (method
!= NULL
);
2065 im
= mono_get_delegate_invoke (method
->klass
);
2066 msg
= mono_method_call_message_new (method
, params
, im
, &async_callback
, &state
);
2068 return mono_thread_pool_add ((MonoObject
*)delegate
, msg
, async_callback
, state
);
2072 mono_mb_emit_save_args (MonoMethodBuilder
*mb
, MonoMethodSignature
*sig
, gboolean save_this
)
2074 int i
, params_var
, tmp_var
;
2076 /* allocate local (pointer) *params[] */
2077 params_var
= mono_mb_add_local (mb
, &mono_defaults
.int_class
->byval_arg
);
2078 /* allocate local (pointer) tmp */
2079 tmp_var
= mono_mb_add_local (mb
, &mono_defaults
.int_class
->byval_arg
);
2081 /* alloate space on stack to store an array of pointers to the arguments */
2082 mono_mb_emit_icon (mb
, sizeof (gpointer
) * (sig
->param_count
+ 1));
2083 mono_mb_emit_byte (mb
, CEE_PREFIX1
);
2084 mono_mb_emit_byte (mb
, CEE_LOCALLOC
);
2085 mono_mb_emit_stloc (mb
, params_var
);
2088 mono_mb_emit_ldloc (mb
, params_var
);
2089 mono_mb_emit_stloc (mb
, tmp_var
);
2091 if (save_this
&& sig
->hasthis
) {
2092 mono_mb_emit_ldloc (mb
, tmp_var
);
2093 mono_mb_emit_ldarg_addr (mb
, 0);
2094 mono_mb_emit_byte (mb
, CEE_STIND_I
);
2095 /* tmp = tmp + sizeof (gpointer) */
2096 if (sig
->param_count
)
2097 mono_mb_emit_add_to_local (mb
, tmp_var
, sizeof (gpointer
));
2101 for (i
= 0; i
< sig
->param_count
; i
++) {
2102 mono_mb_emit_ldloc (mb
, tmp_var
);
2103 mono_mb_emit_ldarg_addr (mb
, i
+ sig
->hasthis
);
2104 mono_mb_emit_byte (mb
, CEE_STIND_I
);
2105 /* tmp = tmp + sizeof (gpointer) */
2106 if (i
< (sig
->param_count
- 1))
2107 mono_mb_emit_add_to_local (mb
, tmp_var
, sizeof (gpointer
));
2114 mono_signature_to_name (MonoMethodSignature
*sig
, const char *prefix
)
2118 GString
*res
= g_string_new ("");
2121 g_string_append (res
, prefix
);
2122 g_string_append_c (res
, '_');
2125 mono_type_get_desc (res
, sig
->ret
, FALSE
);
2128 g_string_append (res
, "__this__");
2130 for (i
= 0; i
< sig
->param_count
; ++i
) {
2131 g_string_append_c (res
, '_');
2132 mono_type_get_desc (res
, sig
->params
[i
], FALSE
);
2135 g_string_free (res
, FALSE
);
2140 * mono_marshal_get_string_encoding:
2142 * Return the string encoding which should be used for a given parameter.
2144 static MonoMarshalNative
2145 mono_marshal_get_string_encoding (MonoMethodPInvoke
*piinfo
, MonoMarshalSpec
*spec
)
2147 /* First try the parameter marshal info */
2149 if (spec
->native
== MONO_NATIVE_LPARRAY
) {
2150 if ((spec
->data
.array_data
.elem_type
!= 0) && (spec
->data
.array_data
.elem_type
!= MONO_NATIVE_MAX
))
2151 return spec
->data
.array_data
.elem_type
;
2154 return spec
->native
;
2158 return MONO_NATIVE_LPSTR
;
2160 /* Then try the method level marshal info */
2161 switch (piinfo
->piflags
& PINVOKE_ATTRIBUTE_CHAR_SET_MASK
) {
2162 case PINVOKE_ATTRIBUTE_CHAR_SET_ANSI
:
2163 return MONO_NATIVE_LPSTR
;
2164 case PINVOKE_ATTRIBUTE_CHAR_SET_UNICODE
:
2165 return MONO_NATIVE_LPWSTR
;
2166 case PINVOKE_ATTRIBUTE_CHAR_SET_AUTO
:
2168 return MONO_NATIVE_LPWSTR
;
2170 return MONO_NATIVE_LPSTR
;
2173 return MONO_NATIVE_LPSTR
;
2177 static MonoMarshalConv
2178 mono_marshal_get_string_to_ptr_conv (MonoMethodPInvoke
*piinfo
, MonoMarshalSpec
*spec
)
2180 MonoMarshalNative encoding
= mono_marshal_get_string_encoding (piinfo
, spec
);
2183 case MONO_NATIVE_LPWSTR
:
2184 return MONO_MARSHAL_CONV_STR_LPWSTR
;
2185 case MONO_NATIVE_LPSTR
:
2186 return MONO_MARSHAL_CONV_STR_LPSTR
;
2187 case MONO_NATIVE_LPTSTR
:
2188 return MONO_MARSHAL_CONV_STR_LPTSTR
;
2189 case MONO_NATIVE_BSTR
:
2190 return MONO_MARSHAL_CONV_STR_BSTR
;
2196 static MonoMarshalConv
2197 mono_marshal_get_stringbuilder_to_ptr_conv (MonoMethodPInvoke
*piinfo
, MonoMarshalSpec
*spec
)
2199 MonoMarshalNative encoding
= mono_marshal_get_string_encoding (piinfo
, spec
);
2202 case MONO_NATIVE_LPWSTR
:
2203 return MONO_MARSHAL_CONV_SB_LPWSTR
;
2205 case MONO_NATIVE_LPSTR
:
2206 return MONO_MARSHAL_CONV_SB_LPSTR
;
2208 case MONO_NATIVE_LPTSTR
:
2209 return MONO_MARSHAL_CONV_SB_LPTSTR
;
2216 static MonoMarshalConv
2217 mono_marshal_get_ptr_to_string_conv (MonoMethodPInvoke
*piinfo
, MonoMarshalSpec
*spec
, gboolean
*need_free
)
2219 MonoMarshalNative encoding
= mono_marshal_get_string_encoding (piinfo
, spec
);
2224 case MONO_NATIVE_LPWSTR
:
2226 return MONO_MARSHAL_CONV_LPWSTR_STR
;
2227 case MONO_NATIVE_LPSTR
:
2228 return MONO_MARSHAL_CONV_LPSTR_STR
;
2229 case MONO_NATIVE_LPTSTR
:
2230 return MONO_MARSHAL_CONV_LPTSTR_STR
;
2231 case MONO_NATIVE_BSTR
:
2232 return MONO_MARSHAL_CONV_BSTR_STR
;
2238 static MonoMarshalConv
2239 mono_marshal_get_ptr_to_stringbuilder_conv (MonoMethodPInvoke
*piinfo
, MonoMarshalSpec
*spec
, gboolean
*need_free
)
2241 MonoMarshalNative encoding
= mono_marshal_get_string_encoding (piinfo
, spec
);
2246 case MONO_NATIVE_LPWSTR
:
2248 * mono_string_builder_to_utf16 does not allocate a
2249 * new buffer, so no need to free it.
2252 return MONO_MARSHAL_CONV_LPWSTR_SB
;
2253 case MONO_NATIVE_LPSTR
:
2254 return MONO_MARSHAL_CONV_LPSTR_SB
;
2256 case MONO_NATIVE_LPTSTR
:
2257 return MONO_MARSHAL_CONV_LPTSTR_SB
;
2265 * Return whenever a field of a native structure or an array member needs to
2269 mono_marshal_need_free (MonoType
*t
, MonoMethodPInvoke
*piinfo
, MonoMarshalSpec
*spec
)
2271 MonoMarshalNative encoding
;
2272 MonoMarshalConv conv
;
2275 case MONO_TYPE_VALUETYPE
:
2276 /* FIXME: Optimize this */
2278 case MONO_TYPE_OBJECT
:
2279 case MONO_TYPE_CLASS
:
2280 if (t
->data
.klass
== mono_defaults
.stringbuilder_class
) {
2282 conv
= mono_marshal_get_ptr_to_stringbuilder_conv (piinfo
, spec
, &need_free
);
2286 case MONO_TYPE_STRING
:
2287 encoding
= mono_marshal_get_string_encoding (piinfo
, spec
);
2288 return (encoding
== MONO_NATIVE_LPWSTR
) ? FALSE
: TRUE
;
2295 * Return the hash table pointed to by VAR, lazily creating it if neccesary.
2298 get_cache (GHashTable
**var
, GHashFunc hash_func
, GCompareFunc equal_func
)
2301 mono_marshal_lock ();
2304 g_hash_table_new (hash_func
, equal_func
);
2305 mono_memory_barrier ();
2308 mono_marshal_unlock ();
2314 mono_marshal_get_cache (GHashTable
**var
, GHashFunc hash_func
, GCompareFunc equal_func
)
2316 return get_cache (var
, hash_func
, equal_func
);
2320 mono_marshal_find_in_cache (GHashTable
*cache
, gpointer key
)
2324 mono_marshal_lock ();
2325 res
= g_hash_table_lookup (cache
, key
);
2326 mono_marshal_unlock ();
2330 /* Create the method from the builder and place it in the cache */
2332 mono_mb_create_and_cache (GHashTable
*cache
, gpointer key
,
2333 MonoMethodBuilder
*mb
, MonoMethodSignature
*sig
,
2338 mono_marshal_lock ();
2339 res
= g_hash_table_lookup (cache
, key
);
2340 mono_marshal_unlock ();
2343 newm
= mono_mb_create_method (mb
, sig
, max_stack
);
2344 mono_marshal_lock ();
2345 res
= g_hash_table_lookup (cache
, key
);
2348 g_hash_table_insert (cache
, key
, res
);
2349 mono_marshal_set_wrapper_info (res
, key
);
2350 mono_marshal_unlock ();
2352 mono_marshal_unlock ();
2353 mono_free_method (newm
);
2361 static inline MonoMethod
*
2362 mono_marshal_remoting_find_in_cache (MonoMethod
*method
, int wrapper_type
)
2364 MonoMethod
*res
= NULL
;
2365 MonoRemotingMethods
*wrps
;
2367 mono_marshal_lock ();
2368 if (method
->klass
->image
->remoting_invoke_cache
)
2369 wrps
= g_hash_table_lookup (method
->klass
->image
->remoting_invoke_cache
, method
);
2374 switch (wrapper_type
) {
2375 case MONO_WRAPPER_REMOTING_INVOKE
: res
= wrps
->invoke
; break;
2376 case MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK
: res
= wrps
->invoke_with_check
; break;
2377 case MONO_WRAPPER_XDOMAIN_INVOKE
: res
= wrps
->xdomain_invoke
; break;
2378 case MONO_WRAPPER_XDOMAIN_DISPATCH
: res
= wrps
->xdomain_dispatch
; break;
2382 /* it is important to do the unlock after the load from wrps, since in
2383 * mono_remoting_mb_create_and_cache () we drop the marshal lock to be able
2384 * to take the loader lock and some other thread may set the fields.
2386 mono_marshal_unlock ();
2390 /* Create the method from the builder and place it in the cache */
2391 static inline MonoMethod
*
2392 mono_remoting_mb_create_and_cache (MonoMethod
*key
, MonoMethodBuilder
*mb
,
2393 MonoMethodSignature
*sig
, int max_stack
)
2395 MonoMethod
**res
= NULL
;
2396 MonoRemotingMethods
*wrps
;
2397 GHashTable
*cache
= get_cache (&key
->klass
->image
->remoting_invoke_cache
, mono_aligned_addr_hash
, NULL
);
2399 mono_marshal_lock ();
2400 wrps
= g_hash_table_lookup (cache
, key
);
2402 wrps
= g_new0 (MonoRemotingMethods
, 1);
2403 g_hash_table_insert (cache
, key
, wrps
);
2406 switch (mb
->method
->wrapper_type
) {
2407 case MONO_WRAPPER_REMOTING_INVOKE
: res
= &wrps
->invoke
; break;
2408 case MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK
: res
= &wrps
->invoke_with_check
; break;
2409 case MONO_WRAPPER_XDOMAIN_INVOKE
: res
= &wrps
->xdomain_invoke
; break;
2410 case MONO_WRAPPER_XDOMAIN_DISPATCH
: res
= &wrps
->xdomain_dispatch
; break;
2411 default: g_assert_not_reached (); break;
2413 mono_marshal_unlock ();
2417 newm
= mono_mb_create_method (mb
, sig
, max_stack
);
2419 mono_marshal_lock ();
2422 mono_marshal_set_wrapper_info (*res
, key
);
2423 mono_marshal_unlock ();
2425 mono_marshal_unlock ();
2426 mono_free_method (newm
);
2434 mono_marshal_method_from_wrapper (MonoMethod
*wrapper
)
2438 if (wrapper
->wrapper_type
== MONO_WRAPPER_NONE
|| wrapper
->wrapper_type
== MONO_WRAPPER_DYNAMIC_METHOD
)
2441 res
= mono_method_get_wrapper_data (wrapper
, 1);
2448 * mono_marshal_get_wrapper_info:
2450 * Retrieve the pointer stored by mono_marshal_set_wrapper_info.
2453 mono_marshal_get_wrapper_info (MonoMethod
*wrapper
)
2455 g_assert (wrapper
->wrapper_type
);
2457 return mono_method_get_wrapper_data (wrapper
, 1);
2461 * mono_marshal_set_wrapper_info:
2463 * Store an arbitrary pointer inside the wrapper which is retrievable by
2464 * mono_marshal_get_wrapper_info. The format of the data depends on the type of the
2465 * wrapper (method->wrapper_type).
2468 mono_marshal_set_wrapper_info (MonoMethod
*method
, gpointer data
)
2472 if (method
->wrapper_type
== MONO_WRAPPER_NONE
|| method
->wrapper_type
== MONO_WRAPPER_DYNAMIC_METHOD
)
2475 datav
= ((MonoMethodWrapper
*)method
)->method_data
;
2480 * get_wrapper_target_class:
2482 * Return the class where a wrapper method should be placed.
2485 get_wrapper_target_class (MonoImage
*image
)
2491 * - can't put all wrappers into an mscorlib class, because they reference
2492 * metadata (signature) so they should be put into the same image as the
2493 * method they wrap, so they are unloaded together.
2494 * - putting them into a class with a type initalizer could cause the
2495 * initializer to be executed which can be a problem if the wrappers are
2497 * - putting them into an inflated class can cause problems if the the
2498 * class is deleted because it references an image which is unloaded.
2499 * To avoid these problems, we put the wrappers into the <Module> class of
2503 klass
= ((MonoDynamicImage
*)image
)->wrappers_type
;
2505 klass
= mono_class_get (image
, mono_metadata_make_token (MONO_TABLE_TYPEDEF
, 1));
2512 mono_marshal_get_delegate_begin_invoke (MonoMethod
*method
)
2514 MonoMethodSignature
*sig
;
2515 MonoMethodBuilder
*mb
;
2521 g_assert (method
&& method
->klass
->parent
== mono_defaults
.multicastdelegate_class
&&
2522 !strcmp (method
->name
, "BeginInvoke"));
2524 sig
= mono_signature_no_pinvoke (method
);
2526 cache
= get_cache (&method
->klass
->image
->delegate_begin_invoke_cache
,
2527 (GHashFunc
)mono_signature_hash
,
2528 (GCompareFunc
)mono_metadata_signature_equal
);
2529 if ((res
= mono_marshal_find_in_cache (cache
, sig
)))
2532 g_assert (sig
->hasthis
);
2534 name
= mono_signature_to_name (sig
, "begin_invoke");
2535 mb
= mono_mb_new (get_wrapper_target_class (method
->klass
->image
), name
, MONO_WRAPPER_DELEGATE_BEGIN_INVOKE
);
2538 params_var
= mono_mb_emit_save_args (mb
, sig
, FALSE
);
2540 mono_mb_emit_ldarg (mb
, 0);
2541 mono_mb_emit_ldloc (mb
, params_var
);
2542 mono_mb_emit_icall (mb
, mono_delegate_begin_invoke
);
2543 mono_mb_emit_byte (mb
, CEE_RET
);
2545 res
= mono_mb_create_and_cache (cache
, sig
, mb
, sig
, sig
->param_count
+ 16);
2551 mono_delegate_end_invoke (MonoDelegate
*delegate
, gpointer
*params
)
2553 MonoDomain
*domain
= mono_domain_get ();
2554 MonoAsyncResult
*ares
;
2555 MonoMethod
*method
= NULL
;
2556 MonoMethodSignature
*sig
;
2557 MonoMethodMessage
*msg
;
2558 MonoObject
*res
, *exc
;
2559 MonoArray
*out_args
;
2562 g_assert (delegate
);
2564 if (!delegate
->method_info
) {
2565 g_assert (delegate
->method
);
2566 MONO_OBJECT_SETREF (delegate
, method_info
, mono_method_get_object (domain
, delegate
->method
, NULL
));
2569 if (!delegate
->method_info
|| !delegate
->method_info
->method
)
2570 g_assert_not_reached ();
2572 klass
= delegate
->object
.vtable
->klass
;
2574 method
= mono_class_get_method_from_name (klass
, "EndInvoke", -1);
2575 g_assert (method
!= NULL
);
2577 sig
= mono_signature_no_pinvoke (method
);
2579 msg
= mono_method_call_message_new (method
, params
, NULL
, NULL
, NULL
);
2581 ares
= mono_array_get (msg
->args
, gpointer
, sig
->param_count
- 1);
2583 mono_raise_exception (mono_exception_from_name_msg (mono_defaults
.corlib
, "System.Runtime.Remoting", "RemotingException", "The async result object is null or of an unexpected type."));
2587 if (ares
->async_delegate
!= (MonoObject
*)delegate
) {
2588 mono_raise_exception (mono_get_exception_invalid_operation (
2589 "The IAsyncResult object provided does not match this delegate."));
2593 if (delegate
->target
&& mono_object_class (delegate
->target
) == mono_defaults
.transparent_proxy_class
) {
2594 MonoTransparentProxy
* tp
= (MonoTransparentProxy
*)delegate
->target
;
2595 msg
= (MonoMethodMessage
*)mono_object_new (domain
, mono_defaults
.mono_method_message_class
);
2596 mono_message_init (domain
, msg
, delegate
->method_info
, NULL
);
2597 msg
->call_type
= CallType_EndInvoke
;
2598 MONO_OBJECT_SETREF (msg
, async_result
, ares
);
2599 res
= mono_remoting_invoke ((MonoObject
*)tp
->rp
, msg
, &exc
, &out_args
);
2601 res
= mono_thread_pool_finish (ares
, &out_args
, &exc
);
2605 if (((MonoException
*)exc
)->stack_trace
) {
2606 char *strace
= mono_string_to_utf8 (((MonoException
*)exc
)->stack_trace
);
2608 tmp
= g_strdup_printf ("%s\nException Rethrown at:\n", strace
);
2610 MONO_OBJECT_SETREF (((MonoException
*)exc
), stack_trace
, mono_string_new (domain
, tmp
));
2613 mono_raise_exception ((MonoException
*)exc
);
2616 mono_method_return_message_restore (method
, params
, out_args
);
2621 mono_mb_emit_restore_result (MonoMethodBuilder
*mb
, MonoType
*return_type
)
2623 MonoType
*t
= mono_type_get_underlying_type (return_type
);
2625 if (return_type
->byref
)
2626 return_type
= &mono_defaults
.int_class
->byval_arg
;
2629 case MONO_TYPE_VOID
:
2630 g_assert_not_reached ();
2633 case MONO_TYPE_STRING
:
2634 case MONO_TYPE_CLASS
:
2635 case MONO_TYPE_OBJECT
:
2636 case MONO_TYPE_ARRAY
:
2637 case MONO_TYPE_SZARRAY
:
2641 case MONO_TYPE_BOOLEAN
:
2644 case MONO_TYPE_CHAR
:
2654 mono_mb_emit_op (mb
, CEE_UNBOX
, mono_class_from_mono_type (return_type
));
2655 mono_mb_emit_byte (mb
, mono_type_to_ldind (return_type
));
2657 case MONO_TYPE_GENERICINST
:
2658 if (!mono_type_generic_inst_is_valuetype (t
))
2661 case MONO_TYPE_VALUETYPE
: {
2662 MonoClass
*klass
= mono_class_from_mono_type (return_type
);
2663 mono_mb_emit_op (mb
, CEE_UNBOX
, klass
);
2664 mono_mb_emit_op (mb
, CEE_LDOBJ
, klass
);
2668 g_warning ("type 0x%x not handled", return_type
->type
);
2669 g_assert_not_reached ();
2672 mono_mb_emit_byte (mb
, CEE_RET
);
2676 mono_marshal_get_delegate_end_invoke (MonoMethod
*method
)
2678 MonoMethodSignature
*sig
;
2679 MonoMethodBuilder
*mb
;
2685 g_assert (method
&& method
->klass
->parent
== mono_defaults
.multicastdelegate_class
&&
2686 !strcmp (method
->name
, "EndInvoke"));
2688 sig
= mono_signature_no_pinvoke (method
);
2690 cache
= get_cache (&method
->klass
->image
->delegate_end_invoke_cache
,
2691 (GHashFunc
)mono_signature_hash
,
2692 (GCompareFunc
)mono_metadata_signature_equal
);
2693 if ((res
= mono_marshal_find_in_cache (cache
, sig
)))
2696 g_assert (sig
->hasthis
);
2698 name
= mono_signature_to_name (sig
, "end_invoke");
2699 mb
= mono_mb_new (get_wrapper_target_class (method
->klass
->image
), name
, MONO_WRAPPER_DELEGATE_END_INVOKE
);
2702 params_var
= mono_mb_emit_save_args (mb
, sig
, FALSE
);
2704 mono_mb_emit_ldarg (mb
, 0);
2705 mono_mb_emit_ldloc (mb
, params_var
);
2706 mono_mb_emit_icall (mb
, mono_delegate_end_invoke
);
2708 if (sig
->ret
->type
== MONO_TYPE_VOID
) {
2709 mono_mb_emit_byte (mb
, CEE_POP
);
2710 mono_mb_emit_byte (mb
, CEE_RET
);
2712 mono_mb_emit_restore_result (mb
, sig
->ret
);
2714 res
= mono_mb_create_and_cache (cache
, sig
,
2715 mb
, sig
, sig
->param_count
+ 16);
2722 mono_remoting_wrapper (MonoMethod
*method
, gpointer
*params
)
2724 MonoMethodMessage
*msg
;
2725 MonoTransparentProxy
*this;
2726 MonoObject
*res
, *exc
;
2727 MonoArray
*out_args
;
2729 this = *((MonoTransparentProxy
**)params
[0]);
2732 g_assert (((MonoObject
*)this)->vtable
->klass
== mono_defaults
.transparent_proxy_class
);
2734 /* skip the this pointer */
2737 if (this->remote_class
->proxy_class
->contextbound
&& this->rp
->context
== (MonoObject
*) mono_context_get ())
2740 MonoMethodSignature
*sig
= mono_method_signature (method
);
2741 int count
= sig
->param_count
;
2742 gpointer
* mparams
= (gpointer
*) alloca(count
*sizeof(gpointer
));
2744 for (i
=0; i
<count
; i
++) {
2745 MonoClass
*class = mono_class_from_mono_type (sig
->params
[i
]);
2746 if (class->valuetype
) {
2747 if (sig
->params
[i
]->byref
) {
2748 mparams
[i
] = *((gpointer
*)params
[i
]);
2750 /* runtime_invoke expects a boxed instance */
2751 if (mono_class_is_nullable (mono_class_from_mono_type (sig
->params
[i
])))
2752 mparams
[i
] = mono_nullable_box (params
[i
], class);
2754 mparams
[i
] = params
[i
];
2757 mparams
[i
] = *((gpointer
**)params
[i
]);
2761 return mono_runtime_invoke (method
, method
->klass
->valuetype
? mono_object_unbox ((MonoObject
*)this): this, mparams
, NULL
);
2764 msg
= mono_method_call_message_new (method
, params
, NULL
, NULL
, NULL
);
2766 res
= mono_remoting_invoke ((MonoObject
*)this->rp
, msg
, &exc
, &out_args
);
2769 mono_raise_exception ((MonoException
*)exc
);
2771 mono_method_return_message_restore (method
, params
, out_args
);
2777 mono_marshal_get_remoting_invoke (MonoMethod
*method
)
2779 MonoMethodSignature
*sig
;
2780 MonoMethodBuilder
*mb
;
2786 if (method
->wrapper_type
== MONO_WRAPPER_REMOTING_INVOKE
|| method
->wrapper_type
== MONO_WRAPPER_XDOMAIN_INVOKE
)
2789 /* this seems to be the best plase to put this, as all remoting invokes seem to get filtered through here */
2790 if (method
->klass
->is_com_object
|| method
->klass
== mono_defaults
.com_object_class
) {
2791 MonoVTable
*vtable
= mono_class_vtable (mono_domain_get (), method
->klass
);
2792 g_assert (vtable
); /*FIXME do proper error handling*/
2794 if (!vtable
->remote
) {
2796 return mono_cominterop_get_invoke (method
);
2798 g_assert_not_reached ();
2803 sig
= mono_signature_no_pinvoke (method
);
2805 /* we cant remote methods without this pointer */
2809 if ((res
= mono_marshal_remoting_find_in_cache (method
, MONO_WRAPPER_REMOTING_INVOKE
)))
2812 mb
= mono_mb_new (method
->klass
, method
->name
, MONO_WRAPPER_REMOTING_INVOKE
);
2813 mb
->method
->save_lmf
= 1;
2815 params_var
= mono_mb_emit_save_args (mb
, sig
, TRUE
);
2817 mono_mb_emit_ptr (mb
, method
);
2818 mono_mb_emit_ldloc (mb
, params_var
);
2819 mono_mb_emit_icall (mb
, mono_remoting_wrapper
);
2820 emit_thread_interrupt_checkpoint (mb
);
2822 if (sig
->ret
->type
== MONO_TYPE_VOID
) {
2823 mono_mb_emit_byte (mb
, CEE_POP
);
2824 mono_mb_emit_byte (mb
, CEE_RET
);
2826 mono_mb_emit_restore_result (mb
, sig
->ret
);
2829 res
= mono_remoting_mb_create_and_cache (method
, mb
, sig
, sig
->param_count
+ 16);
2835 /* mono_get_xdomain_marshal_type()
2836 * Returns the kind of marshalling that a type needs for cross domain calls.
2838 static MonoXDomainMarshalType
2839 mono_get_xdomain_marshal_type (MonoType
*t
)
2842 case MONO_TYPE_VOID
:
2843 g_assert_not_reached ();
2847 case MONO_TYPE_BOOLEAN
:
2850 case MONO_TYPE_CHAR
:
2857 return MONO_MARSHAL_NONE
;
2858 case MONO_TYPE_STRING
:
2859 return MONO_MARSHAL_COPY
;
2860 case MONO_TYPE_ARRAY
:
2861 case MONO_TYPE_SZARRAY
: {
2862 MonoClass
*elem_class
= mono_class_from_mono_type (t
)->element_class
;
2863 if (mono_get_xdomain_marshal_type (&(elem_class
->byval_arg
)) != MONO_MARSHAL_SERIALIZE
)
2864 return MONO_MARSHAL_COPY
;
2869 return MONO_MARSHAL_SERIALIZE
;
2873 /* mono_marshal_xdomain_copy_value
2874 * Makes a copy of "val" suitable for the current domain.
2877 mono_marshal_xdomain_copy_value (MonoObject
*val
)
2880 if (val
== NULL
) return NULL
;
2882 domain
= mono_domain_get ();
2884 switch (mono_object_class (val
)->byval_arg
.type
) {
2885 case MONO_TYPE_VOID
:
2886 g_assert_not_reached ();
2890 case MONO_TYPE_BOOLEAN
:
2893 case MONO_TYPE_CHAR
:
2899 case MONO_TYPE_R8
: {
2900 return mono_value_box (domain
, mono_object_class (val
), ((char*)val
) + sizeof(MonoObject
));
2902 case MONO_TYPE_STRING
: {
2903 MonoString
*str
= (MonoString
*) val
;
2904 return (MonoObject
*) mono_string_new_utf16 (domain
, mono_string_chars (str
), mono_string_length (str
));
2906 case MONO_TYPE_ARRAY
:
2907 case MONO_TYPE_SZARRAY
: {
2909 MonoXDomainMarshalType mt
= mono_get_xdomain_marshal_type (&(mono_object_class (val
)->element_class
->byval_arg
));
2910 if (mt
== MONO_MARSHAL_SERIALIZE
) return NULL
;
2911 acopy
= mono_array_clone_in_domain (domain
, (MonoArray
*) val
);
2912 if (mt
== MONO_MARSHAL_COPY
) {
2913 int i
, len
= mono_array_length (acopy
);
2914 for (i
= 0; i
< len
; i
++) {
2915 MonoObject
*item
= mono_array_get (acopy
, gpointer
, i
);
2916 mono_array_setref (acopy
, i
, mono_marshal_xdomain_copy_value (item
));
2919 return (MonoObject
*) acopy
;
2923 if (mono_object_class (val
) == mono_defaults
.stringbuilder_class
) {
2924 MonoStringBuilder
*oldsb
= (MonoStringBuilder
*) val
;
2925 MonoStringBuilder
*newsb
= (MonoStringBuilder
*) mono_object_new (domain
, mono_defaults
.stringbuilder_class
);
2926 MONO_OBJECT_SETREF (newsb
, str
, mono_string_new_utf16 (domain
, mono_string_chars (oldsb
->str
), mono_string_length (oldsb
->str
)));
2927 newsb
->length
= oldsb
->length
;
2928 newsb
->max_capacity
= (gint32
)0x7fffffff;
2929 return (MonoObject
*) newsb
;
2934 /* mono_marshal_xdomain_copy_out_value()
2935 * Copies the contents of the src instance into the dst instance. src and dst
2936 * must have the same type, and if they are arrays, the same size.
2939 mono_marshal_xdomain_copy_out_value (MonoObject
*src
, MonoObject
*dst
)
2941 if (src
== NULL
|| dst
== NULL
) return;
2943 g_assert (mono_object_class (src
) == mono_object_class (dst
));
2945 switch (mono_object_class (src
)->byval_arg
.type
) {
2946 case MONO_TYPE_ARRAY
:
2947 case MONO_TYPE_SZARRAY
: {
2948 int mt
= mono_get_xdomain_marshal_type (&(mono_object_class (src
)->element_class
->byval_arg
));
2949 if (mt
== MONO_MARSHAL_SERIALIZE
) return;
2950 if (mt
== MONO_MARSHAL_COPY
) {
2951 int i
, len
= mono_array_length ((MonoArray
*)dst
);
2952 for (i
= 0; i
< len
; i
++) {
2953 MonoObject
*item
= mono_array_get ((MonoArray
*)src
, gpointer
, i
);
2954 mono_array_setref ((MonoArray
*)dst
, i
, mono_marshal_xdomain_copy_value (item
));
2957 mono_array_full_copy ((MonoArray
*)src
, (MonoArray
*)dst
);
2963 if (mono_object_class (src
) == mono_defaults
.stringbuilder_class
) {
2964 MonoStringBuilder
*src_sb
= (MonoStringBuilder
*) src
;
2965 MonoStringBuilder
*dst_sb
= (MonoStringBuilder
*) dst
;
2967 MONO_OBJECT_SETREF (dst_sb
, str
, mono_string_new_utf16 (mono_object_domain (dst
), mono_string_chars (src_sb
->str
), mono_string_length (src_sb
->str
)));
2968 dst_sb
->cached_str
= NULL
;
2969 dst_sb
->length
= src_sb
->length
;
2974 mono_marshal_emit_xdomain_copy_value (MonoMethodBuilder
*mb
, MonoClass
*pclass
)
2976 mono_mb_emit_icall (mb
, mono_marshal_xdomain_copy_value
);
2977 mono_mb_emit_op (mb
, CEE_CASTCLASS
, pclass
);
2981 mono_marshal_emit_xdomain_copy_out_value (MonoMethodBuilder
*mb
, MonoClass
*pclass
)
2983 mono_mb_emit_icall (mb
, mono_marshal_xdomain_copy_out_value
);
2986 /* mono_marshal_supports_fast_xdomain()
2987 * Returns TRUE if the method can use the fast xdomain wrapper.
2990 mono_marshal_supports_fast_xdomain (MonoMethod
*method
)
2992 return !method
->klass
->contextbound
&&
2993 !((method
->flags
& METHOD_ATTRIBUTE_SPECIAL_NAME
) && (strcmp (".ctor", method
->name
) == 0));
2997 mono_marshal_set_domain_by_id (gint32 id
, MonoBoolean push
)
2999 MonoDomain
*current_domain
= mono_domain_get ();
3000 MonoDomain
*domain
= mono_domain_get_by_id (id
);
3002 if (!domain
|| !mono_domain_set (domain
, FALSE
))
3003 mono_raise_exception (mono_get_exception_appdomain_unloaded ());
3006 mono_thread_push_appdomain_ref (domain
);
3008 mono_thread_pop_appdomain_ref ();
3010 return current_domain
->domain_id
;
3014 mono_marshal_emit_switch_domain (MonoMethodBuilder
*mb
)
3016 mono_mb_emit_icall (mb
, mono_marshal_set_domain_by_id
);
3019 /* mono_marshal_emit_load_domain_method ()
3020 * Loads into the stack a pointer to the code of the provided method for
3021 * the current domain.
3024 mono_marshal_emit_load_domain_method (MonoMethodBuilder
*mb
, MonoMethod
*method
)
3026 /* We need a pointer to the method for the running domain (not the domain
3027 * that compiles the method).
3029 mono_mb_emit_ptr (mb
, method
);
3030 mono_mb_emit_icall (mb
, mono_compile_method
);
3033 /* mono_marshal_check_domain_image ()
3034 * Returns TRUE if the image is loaded in the specified
3035 * application domain.
3038 mono_marshal_check_domain_image (gint32 domain_id
, MonoImage
*image
)
3043 MonoDomain
*domain
= mono_domain_get_by_id (domain_id
);
3047 mono_domain_assemblies_lock (domain
);
3048 for (tmp
= domain
->domain_assemblies
; tmp
; tmp
= tmp
->next
) {
3050 if (ass
->image
== image
)
3053 mono_domain_assemblies_unlock (domain
);
3058 /* mono_marshal_get_xappdomain_dispatch ()
3059 * Generates a method that dispatches a method call from another domain into
3060 * the current domain.
3063 mono_marshal_get_xappdomain_dispatch (MonoMethod
*method
, int *marshal_types
, int complex_count
, int complex_out_count
, int ret_marshal_type
)
3065 MonoMethodSignature
*sig
, *csig
;
3066 MonoMethodBuilder
*mb
;
3068 int i
, j
, param_index
, copy_locals_base
;
3069 MonoClass
*ret_class
= NULL
;
3070 int loc_array
=0, loc_return
=0, loc_serialized_exc
=0;
3071 MonoExceptionClause
*main_clause
;
3073 gboolean copy_return
;
3075 if ((res
= mono_marshal_remoting_find_in_cache (method
, MONO_WRAPPER_XDOMAIN_DISPATCH
)))
3078 sig
= mono_method_signature (method
);
3079 copy_return
= (sig
->ret
->type
!= MONO_TYPE_VOID
&& ret_marshal_type
!= MONO_MARSHAL_SERIALIZE
);
3082 csig
= mono_metadata_signature_alloc (mono_defaults
.corlib
, 3 + sig
->param_count
- complex_count
);
3083 csig
->params
[j
++] = &mono_defaults
.object_class
->byval_arg
;
3084 csig
->params
[j
++] = &byte_array_class
->this_arg
;
3085 csig
->params
[j
++] = &byte_array_class
->this_arg
;
3086 for (i
= 0; i
< sig
->param_count
; i
++) {
3087 if (marshal_types
[i
] != MONO_MARSHAL_SERIALIZE
)
3088 csig
->params
[j
++] = sig
->params
[i
];
3091 csig
->ret
= sig
->ret
;
3093 csig
->ret
= &mono_defaults
.void_class
->byval_arg
;
3095 csig
->hasthis
= FALSE
;
3097 mb
= mono_mb_new (method
->klass
, method
->name
, MONO_WRAPPER_XDOMAIN_DISPATCH
);
3098 mb
->method
->save_lmf
= 1;
3102 loc_serialized_exc
= mono_mb_add_local (mb
, &byte_array_class
->byval_arg
);
3103 if (complex_count
> 0)
3104 loc_array
= mono_mb_add_local (mb
, &mono_defaults
.object_class
->byval_arg
);
3105 if (sig
->ret
->type
!= MONO_TYPE_VOID
) {
3106 loc_return
= mono_mb_add_local (mb
, sig
->ret
);
3107 ret_class
= mono_class_from_mono_type (sig
->ret
);
3112 main_clause
= mono_image_alloc0 (method
->klass
->image
, sizeof (MonoExceptionClause
));
3113 main_clause
->try_offset
= mono_mb_get_label (mb
);
3115 /* Clean the call context */
3117 mono_mb_emit_byte (mb
, CEE_LDNULL
);
3118 mono_mb_emit_managed_call (mb
, method_set_call_context
, NULL
);
3119 mono_mb_emit_byte (mb
, CEE_POP
);
3121 /* Deserialize call data */
3123 mono_mb_emit_ldarg (mb
, 1);
3124 mono_mb_emit_byte (mb
, CEE_LDIND_REF
);
3125 mono_mb_emit_byte (mb
, CEE_DUP
);
3126 pos
= mono_mb_emit_short_branch (mb
, CEE_BRFALSE_S
);
3128 mono_marshal_emit_xdomain_copy_value (mb
, byte_array_class
);
3129 mono_mb_emit_managed_call (mb
, method_rs_deserialize
, NULL
);
3131 if (complex_count
> 0)
3132 mono_mb_emit_stloc (mb
, loc_array
);
3134 mono_mb_emit_byte (mb
, CEE_POP
);
3136 mono_mb_patch_short_branch (mb
, pos
);
3138 /* Get the target object */
3140 mono_mb_emit_ldarg (mb
, 0);
3141 mono_mb_emit_managed_call (mb
, method_rs_appdomain_target
, NULL
);
3143 /* Load the arguments */
3145 copy_locals_base
= mb
->locals
;
3146 param_index
= 3; // Index of the first non-serialized parameter of this wrapper
3148 for (i
= 0; i
< sig
->param_count
; i
++) {
3149 MonoType
*pt
= sig
->params
[i
];
3150 MonoClass
*pclass
= mono_class_from_mono_type (pt
);
3151 switch (marshal_types
[i
]) {
3152 case MONO_MARSHAL_SERIALIZE
: {
3153 /* take the value from the serialized array */
3154 mono_mb_emit_ldloc (mb
, loc_array
);
3155 mono_mb_emit_icon (mb
, j
++);
3157 if (pclass
->valuetype
) {
3158 mono_mb_emit_byte (mb
, CEE_LDELEM_REF
);
3159 mono_mb_emit_op (mb
, CEE_UNBOX
, pclass
);
3161 mono_mb_emit_op (mb
, CEE_LDELEMA
, pclass
);
3164 if (pclass
->valuetype
) {
3165 mono_mb_emit_byte (mb
, CEE_LDELEM_REF
);
3166 mono_mb_emit_op (mb
, CEE_UNBOX
, pclass
);
3167 mono_mb_emit_op (mb
, CEE_LDOBJ
, pclass
);
3169 mono_mb_emit_byte (mb
, CEE_LDELEM_REF
);
3170 if (pclass
!= mono_defaults
.object_class
) {
3171 mono_mb_emit_op (mb
, CEE_CASTCLASS
, pclass
);
3177 case MONO_MARSHAL_COPY_OUT
: {
3178 /* Keep a local copy of the value since we need to copy it back after the call */
3179 int copy_local
= mono_mb_add_local (mb
, &(pclass
->byval_arg
));
3180 mono_mb_emit_ldarg (mb
, param_index
++);
3181 mono_marshal_emit_xdomain_copy_value (mb
, pclass
);
3182 mono_mb_emit_byte (mb
, CEE_DUP
);
3183 mono_mb_emit_stloc (mb
, copy_local
);
3186 case MONO_MARSHAL_COPY
: {
3187 mono_mb_emit_ldarg (mb
, param_index
);
3189 mono_mb_emit_byte (mb
, CEE_DUP
);
3190 mono_mb_emit_byte (mb
, CEE_DUP
);
3191 mono_mb_emit_byte (mb
, CEE_LDIND_REF
);
3192 mono_marshal_emit_xdomain_copy_value (mb
, pclass
);
3193 mono_mb_emit_byte (mb
, CEE_STIND_REF
);
3195 mono_marshal_emit_xdomain_copy_value (mb
, pclass
);
3200 case MONO_MARSHAL_NONE
:
3201 mono_mb_emit_ldarg (mb
, param_index
++);
3206 /* Make the call to the real object */
3208 emit_thread_force_interrupt_checkpoint (mb
);
3210 mono_mb_emit_op (mb
, CEE_CALLVIRT
, method
);
3212 if (sig
->ret
->type
!= MONO_TYPE_VOID
)
3213 mono_mb_emit_stloc (mb
, loc_return
);
3215 /* copy back MONO_MARSHAL_COPY_OUT parameters */
3219 for (i
= 0; i
< sig
->param_count
; i
++) {
3220 if (marshal_types
[i
] == MONO_MARSHAL_SERIALIZE
) continue;
3221 if (marshal_types
[i
] == MONO_MARSHAL_COPY_OUT
) {
3222 mono_mb_emit_ldloc (mb
, copy_locals_base
+ (j
++));
3223 mono_mb_emit_ldarg (mb
, param_index
);
3224 mono_marshal_emit_xdomain_copy_out_value (mb
, mono_class_from_mono_type (sig
->params
[i
]));
3229 /* Serialize the return values */
3231 if (complex_out_count
> 0) {
3232 /* Reset parameters in the array that don't need to be serialized back */
3234 for (i
= 0; i
< sig
->param_count
; i
++) {
3235 if (marshal_types
[i
] != MONO_MARSHAL_SERIALIZE
) continue;
3236 if (!sig
->params
[i
]->byref
) {
3237 mono_mb_emit_ldloc (mb
, loc_array
);
3238 mono_mb_emit_icon (mb
, j
);
3239 mono_mb_emit_byte (mb
, CEE_LDNULL
);
3240 mono_mb_emit_byte (mb
, CEE_STELEM_REF
);
3245 /* Add the return value to the array */
3247 if (ret_marshal_type
== MONO_MARSHAL_SERIALIZE
) {
3248 mono_mb_emit_ldloc (mb
, loc_array
);
3249 mono_mb_emit_icon (mb
, complex_count
); /* The array has an additional slot to hold the ret value */
3250 mono_mb_emit_ldloc (mb
, loc_return
);
3252 g_assert (ret_class
); /*FIXME properly fail here*/
3253 if (ret_class
->valuetype
) {
3254 mono_mb_emit_op (mb
, CEE_BOX
, ret_class
);
3256 mono_mb_emit_byte (mb
, CEE_STELEM_REF
);
3261 mono_mb_emit_ldarg (mb
, 1);
3262 mono_mb_emit_ldloc (mb
, loc_array
);
3263 mono_mb_emit_managed_call (mb
, method_rs_serialize
, NULL
);
3264 mono_mb_emit_byte (mb
, CEE_STIND_REF
);
3265 } else if (ret_marshal_type
== MONO_MARSHAL_SERIALIZE
) {
3266 mono_mb_emit_ldarg (mb
, 1);
3267 mono_mb_emit_ldloc (mb
, loc_return
);
3268 if (ret_class
->valuetype
) {
3269 mono_mb_emit_op (mb
, CEE_BOX
, ret_class
);
3271 mono_mb_emit_managed_call (mb
, method_rs_serialize
, NULL
);
3272 mono_mb_emit_byte (mb
, CEE_STIND_REF
);
3274 mono_mb_emit_ldarg (mb
, 1);
3275 mono_mb_emit_byte (mb
, CEE_LDNULL
);
3276 mono_mb_emit_managed_call (mb
, method_rs_serialize
, NULL
);
3277 mono_mb_emit_byte (mb
, CEE_STIND_REF
);
3280 mono_mb_emit_ldarg (mb
, 2);
3281 mono_mb_emit_byte (mb
, CEE_LDNULL
);
3282 mono_mb_emit_byte (mb
, CEE_STIND_REF
);
3283 pos_leave
= mono_mb_emit_branch (mb
, CEE_LEAVE
);
3285 /* Main exception catch */
3286 main_clause
->flags
= MONO_EXCEPTION_CLAUSE_NONE
;
3287 main_clause
->try_len
= mono_mb_get_pos (mb
) - main_clause
->try_offset
;
3288 main_clause
->data
.catch_class
= mono_defaults
.object_class
;
3291 main_clause
->handler_offset
= mono_mb_get_label (mb
);
3292 mono_mb_emit_managed_call (mb
, method_rs_serialize_exc
, NULL
);
3293 mono_mb_emit_stloc (mb
, loc_serialized_exc
);
3294 mono_mb_emit_ldarg (mb
, 2);
3295 mono_mb_emit_ldloc (mb
, loc_serialized_exc
);
3296 mono_mb_emit_byte (mb
, CEE_STIND_REF
);
3297 mono_mb_emit_branch (mb
, CEE_LEAVE
);
3298 main_clause
->handler_len
= mono_mb_get_pos (mb
) - main_clause
->handler_offset
;
3301 mono_mb_patch_branch (mb
, pos_leave
);
3304 mono_mb_emit_ldloc (mb
, loc_return
);
3306 mono_mb_emit_byte (mb
, CEE_RET
);
3308 mono_mb_set_clauses (mb
, 1, main_clause
);
3310 res
= mono_remoting_mb_create_and_cache (method
, mb
, csig
, csig
->param_count
+ 16);
3316 /* mono_marshal_get_xappdomain_invoke ()
3317 * Generates a fast remoting wrapper for cross app domain calls.
3320 mono_marshal_get_xappdomain_invoke (MonoMethod
*method
)
3322 MonoMethodSignature
*sig
;
3323 MonoMethodBuilder
*mb
;
3325 int i
, j
, complex_count
, complex_out_count
, copy_locals_base
;
3327 MonoClass
*ret_class
= NULL
;
3328 MonoMethod
*xdomain_method
;
3329 int ret_marshal_type
= MONO_MARSHAL_NONE
;
3330 int loc_array
=0, loc_serialized_data
=-1, loc_real_proxy
;
3331 int loc_old_domainid
, loc_domainid
, loc_return
=0, loc_serialized_exc
=0, loc_context
;
3332 int pos
, pos_dispatch
, pos_noex
;
3333 gboolean copy_return
= FALSE
;
3337 if (method
->wrapper_type
== MONO_WRAPPER_REMOTING_INVOKE
|| method
->wrapper_type
== MONO_WRAPPER_XDOMAIN_INVOKE
)
3340 /* we cant remote methods without this pointer */
3341 if (!mono_method_signature (method
)->hasthis
)
3344 if (!mono_marshal_supports_fast_xdomain (method
))
3345 return mono_marshal_get_remoting_invoke (method
);
3347 mono_remoting_marshal_init ();
3349 if ((res
= mono_marshal_remoting_find_in_cache (method
, MONO_WRAPPER_XDOMAIN_INVOKE
)))
3352 sig
= mono_signature_no_pinvoke (method
);
3354 mb
= mono_mb_new (method
->klass
, method
->name
, MONO_WRAPPER_XDOMAIN_INVOKE
);
3355 mb
->method
->save_lmf
= 1;
3357 /* Count the number of parameters that need to be serialized */
3359 marshal_types
= alloca (sizeof (int) * sig
->param_count
);
3360 complex_count
= complex_out_count
= 0;
3361 for (i
= 0; i
< sig
->param_count
; i
++) {
3362 MonoType
*ptype
= sig
->params
[i
];
3363 int mt
= mono_get_xdomain_marshal_type (ptype
);
3365 /* If the [Out] attribute is applied to a parameter that can be internally copied,
3366 * the copy will be made by reusing the original object instance
3368 if ((ptype
->attrs
& PARAM_ATTRIBUTE_OUT
) != 0 && mt
== MONO_MARSHAL_COPY
&& !ptype
->byref
)
3369 mt
= MONO_MARSHAL_COPY_OUT
;
3370 else if (mt
== MONO_MARSHAL_SERIALIZE
) {
3372 if (ptype
->byref
) complex_out_count
++;
3374 marshal_types
[i
] = mt
;
3377 if (sig
->ret
->type
!= MONO_TYPE_VOID
) {
3378 ret_marshal_type
= mono_get_xdomain_marshal_type (sig
->ret
);
3379 ret_class
= mono_class_from_mono_type (sig
->ret
);
3380 copy_return
= ret_marshal_type
!= MONO_MARSHAL_SERIALIZE
;
3385 if (complex_count
> 0)
3386 loc_array
= mono_mb_add_local (mb
, &mono_defaults
.object_class
->byval_arg
);
3387 loc_serialized_data
= mono_mb_add_local (mb
, &byte_array_class
->byval_arg
);
3388 loc_real_proxy
= mono_mb_add_local (mb
, &mono_defaults
.object_class
->byval_arg
);
3390 loc_return
= mono_mb_add_local (mb
, sig
->ret
);
3391 loc_old_domainid
= mono_mb_add_local (mb
, &mono_defaults
.int32_class
->byval_arg
);
3392 loc_domainid
= mono_mb_add_local (mb
, &mono_defaults
.int32_class
->byval_arg
);
3393 loc_serialized_exc
= mono_mb_add_local (mb
, &byte_array_class
->byval_arg
);
3394 loc_context
= mono_mb_add_local (mb
, &mono_defaults
.object_class
->byval_arg
);
3396 /* Save thread domain data */
3398 mono_mb_emit_icall (mb
, mono_context_get
);
3399 mono_mb_emit_byte (mb
, CEE_DUP
);
3400 mono_mb_emit_stloc (mb
, loc_context
);
3402 /* If the thread is not running in the default context, it needs to go
3403 * through the whole remoting sink, since the context is going to change
3405 mono_mb_emit_managed_call (mb
, method_needs_context_sink
, NULL
);
3406 pos
= mono_mb_emit_short_branch (mb
, CEE_BRTRUE_S
);
3408 /* Another case in which the fast path can't be used: when the target domain
3409 * has a different image for the same assembly.
3412 /* Get the target domain id */
3414 mono_mb_emit_ldarg (mb
, 0);
3415 mono_mb_emit_ldflda (mb
, G_STRUCT_OFFSET (MonoTransparentProxy
, rp
));
3416 mono_mb_emit_byte (mb
, CEE_LDIND_REF
);
3417 mono_mb_emit_byte (mb
, CEE_DUP
);
3418 mono_mb_emit_stloc (mb
, loc_real_proxy
);
3420 mono_mb_emit_ldflda (mb
, G_STRUCT_OFFSET (MonoRealProxy
, target_domain_id
));
3421 mono_mb_emit_byte (mb
, CEE_LDIND_I4
);
3422 mono_mb_emit_stloc (mb
, loc_domainid
);
3424 /* Check if the target domain has the same image for the required assembly */
3426 mono_mb_emit_ldloc (mb
, loc_domainid
);
3427 mono_mb_emit_ptr (mb
, method
->klass
->image
);
3428 mono_mb_emit_icall (mb
, mono_marshal_check_domain_image
);
3429 pos_dispatch
= mono_mb_emit_short_branch (mb
, CEE_BRTRUE_S
);
3431 /* Use the whole remoting sink to dispatch this message */
3433 mono_mb_patch_short_branch (mb
, pos
);
3435 mono_mb_emit_ldarg (mb
, 0);
3436 for (i
= 0; i
< sig
->param_count
; i
++)
3437 mono_mb_emit_ldarg (mb
, i
+ 1);
3439 mono_mb_emit_managed_call (mb
, mono_marshal_get_remoting_invoke (method
), NULL
);
3440 mono_mb_emit_byte (mb
, CEE_RET
);
3441 mono_mb_patch_short_branch (mb
, pos_dispatch
);
3443 /* Create the array that will hold the parameters to be serialized */
3445 if (complex_count
> 0) {
3446 mono_mb_emit_icon (mb
, (ret_marshal_type
== MONO_MARSHAL_SERIALIZE
&& complex_out_count
> 0) ? complex_count
+ 1 : complex_count
); /* +1 for the return type */
3447 mono_mb_emit_op (mb
, CEE_NEWARR
, mono_defaults
.object_class
);
3450 for (i
= 0; i
< sig
->param_count
; i
++) {
3452 if (marshal_types
[i
] != MONO_MARSHAL_SERIALIZE
) continue;
3453 pclass
= mono_class_from_mono_type (sig
->params
[i
]);
3454 mono_mb_emit_byte (mb
, CEE_DUP
);
3455 mono_mb_emit_icon (mb
, j
);
3456 mono_mb_emit_ldarg (mb
, i
+ 1); /* 0=this */
3457 if (sig
->params
[i
]->byref
) {
3458 if (pclass
->valuetype
)
3459 mono_mb_emit_op (mb
, CEE_LDOBJ
, pclass
);
3461 mono_mb_emit_byte (mb
, CEE_LDIND_REF
);
3463 if (pclass
->valuetype
)
3464 mono_mb_emit_op (mb
, CEE_BOX
, pclass
);
3465 mono_mb_emit_byte (mb
, CEE_STELEM_REF
);
3468 mono_mb_emit_stloc (mb
, loc_array
);
3470 /* Serialize parameters */
3472 mono_mb_emit_ldloc (mb
, loc_array
);
3473 mono_mb_emit_managed_call (mb
, method_rs_serialize
, NULL
);
3474 mono_mb_emit_stloc (mb
, loc_serialized_data
);
3476 mono_mb_emit_byte (mb
, CEE_LDNULL
);
3477 mono_mb_emit_managed_call (mb
, method_rs_serialize
, NULL
);
3478 mono_mb_emit_stloc (mb
, loc_serialized_data
);
3483 mono_mb_emit_ldloc (mb
, loc_domainid
);
3484 mono_mb_emit_byte (mb
, CEE_LDC_I4_1
);
3485 mono_marshal_emit_switch_domain (mb
);
3486 mono_mb_emit_stloc (mb
, loc_old_domainid
);
3488 /* Load the arguments */
3490 mono_mb_emit_ldloc (mb
, loc_real_proxy
);
3491 mono_mb_emit_ldloc_addr (mb
, loc_serialized_data
);
3492 mono_mb_emit_ldloc_addr (mb
, loc_serialized_exc
);
3494 copy_locals_base
= mb
->locals
;
3495 for (i
= 0; i
< sig
->param_count
; i
++) {
3496 switch (marshal_types
[i
]) {
3497 case MONO_MARSHAL_SERIALIZE
:
3499 case MONO_MARSHAL_COPY
: {
3500 mono_mb_emit_ldarg (mb
, i
+1);
3501 if (sig
->params
[i
]->byref
) {
3502 /* make a local copy of the byref parameter. The real parameter
3503 * will be updated after the xdomain call
3505 MonoClass
*pclass
= mono_class_from_mono_type (sig
->params
[i
]);
3506 int copy_local
= mono_mb_add_local (mb
, &(pclass
->byval_arg
));
3507 mono_mb_emit_byte (mb
, CEE_LDIND_REF
);
3508 mono_mb_emit_stloc (mb
, copy_local
);
3509 mono_mb_emit_ldloc_addr (mb
, copy_local
);
3513 case MONO_MARSHAL_COPY_OUT
:
3514 case MONO_MARSHAL_NONE
:
3515 mono_mb_emit_ldarg (mb
, i
+1);
3520 /* Make the call to the invoke wrapper in the target domain */
3522 xdomain_method
= mono_marshal_get_xappdomain_dispatch (method
, marshal_types
, complex_count
, complex_out_count
, ret_marshal_type
);
3523 mono_marshal_emit_load_domain_method (mb
, xdomain_method
);
3524 mono_mb_emit_calli (mb
, mono_method_signature (xdomain_method
));
3527 mono_mb_emit_stloc (mb
, loc_return
);
3531 mono_mb_emit_ldloc (mb
, loc_old_domainid
);
3532 mono_mb_emit_byte (mb
, CEE_LDC_I4_0
);
3533 mono_marshal_emit_switch_domain (mb
);
3534 mono_mb_emit_byte (mb
, CEE_POP
);
3536 /* Restore thread domain data */
3538 mono_mb_emit_ldloc (mb
, loc_context
);
3539 mono_mb_emit_icall (mb
, mono_context_set
);
3541 /* if (loc_serialized_exc != null) ... */
3543 mono_mb_emit_ldloc (mb
, loc_serialized_exc
);
3544 pos_noex
= mono_mb_emit_short_branch (mb
, CEE_BRFALSE_S
);
3546 mono_mb_emit_ldloc (mb
, loc_serialized_exc
);
3547 mono_marshal_emit_xdomain_copy_value (mb
, byte_array_class
);
3548 mono_mb_emit_managed_call (mb
, method_rs_deserialize
, NULL
);
3549 mono_mb_emit_op (mb
, CEE_CASTCLASS
, mono_defaults
.exception_class
);
3550 mono_mb_emit_managed_call (mb
, method_exc_fixexc
, NULL
);
3551 mono_mb_emit_byte (mb
, CEE_THROW
);
3552 mono_mb_patch_short_branch (mb
, pos_noex
);
3554 /* copy back non-serialized output parameters */
3557 for (i
= 0; i
< sig
->param_count
; i
++) {
3558 if (!sig
->params
[i
]->byref
|| marshal_types
[i
] != MONO_MARSHAL_COPY
) continue;
3559 mono_mb_emit_ldarg (mb
, i
+ 1);
3560 mono_mb_emit_ldloc (mb
, copy_locals_base
+ (j
++));
3561 mono_marshal_emit_xdomain_copy_value (mb
, mono_class_from_mono_type (sig
->params
[i
]));
3562 mono_mb_emit_byte (mb
, CEE_STIND_REF
);
3565 /* Deserialize out parameters */
3567 if (complex_out_count
> 0) {
3568 mono_mb_emit_ldloc (mb
, loc_serialized_data
);
3569 mono_marshal_emit_xdomain_copy_value (mb
, byte_array_class
);
3570 mono_mb_emit_managed_call (mb
, method_rs_deserialize
, NULL
);
3571 mono_mb_emit_stloc (mb
, loc_array
);
3573 /* Copy back output parameters and return type */
3576 for (i
= 0; i
< sig
->param_count
; i
++) {
3577 if (marshal_types
[i
] != MONO_MARSHAL_SERIALIZE
) continue;
3578 if (sig
->params
[i
]->byref
) {
3579 MonoClass
*pclass
= mono_class_from_mono_type (sig
->params
[i
]);
3580 mono_mb_emit_ldarg (mb
, i
+ 1);
3581 mono_mb_emit_ldloc (mb
, loc_array
);
3582 mono_mb_emit_icon (mb
, j
);
3583 mono_mb_emit_byte (mb
, CEE_LDELEM_REF
);
3584 if (pclass
->valuetype
) {
3585 mono_mb_emit_op (mb
, CEE_UNBOX
, pclass
);
3586 mono_mb_emit_op (mb
, CEE_LDOBJ
, pclass
);
3587 mono_mb_emit_op (mb
, CEE_STOBJ
, pclass
);
3589 if (pclass
!= mono_defaults
.object_class
)
3590 mono_mb_emit_op (mb
, CEE_CASTCLASS
, pclass
);
3591 mono_mb_emit_byte (mb
, CEE_STIND_REF
);
3597 if (ret_marshal_type
== MONO_MARSHAL_SERIALIZE
) {
3598 mono_mb_emit_ldloc (mb
, loc_array
);
3599 mono_mb_emit_icon (mb
, complex_count
);
3600 mono_mb_emit_byte (mb
, CEE_LDELEM_REF
);
3601 if (ret_class
->valuetype
) {
3602 mono_mb_emit_op (mb
, CEE_UNBOX
, ret_class
);
3603 mono_mb_emit_op (mb
, CEE_LDOBJ
, ret_class
);
3606 } else if (ret_marshal_type
== MONO_MARSHAL_SERIALIZE
) {
3607 mono_mb_emit_ldloc (mb
, loc_serialized_data
);
3608 mono_marshal_emit_xdomain_copy_value (mb
, byte_array_class
);
3609 mono_mb_emit_managed_call (mb
, method_rs_deserialize
, NULL
);
3610 if (ret_class
->valuetype
) {
3611 mono_mb_emit_op (mb
, CEE_UNBOX
, ret_class
);
3612 mono_mb_emit_op (mb
, CEE_LDOBJ
, ret_class
);
3613 } else if (ret_class
!= mono_defaults
.object_class
) {
3614 mono_mb_emit_op (mb
, CEE_CASTCLASS
, ret_class
);
3617 mono_mb_emit_ldloc (mb
, loc_serialized_data
);
3618 mono_mb_emit_byte (mb
, CEE_DUP
);
3619 pos
= mono_mb_emit_short_branch (mb
, CEE_BRFALSE_S
);
3620 mono_marshal_emit_xdomain_copy_value (mb
, byte_array_class
);
3622 mono_mb_patch_short_branch (mb
, pos
);
3623 mono_mb_emit_managed_call (mb
, method_rs_deserialize
, NULL
);
3624 mono_mb_emit_byte (mb
, CEE_POP
);
3628 mono_mb_emit_ldloc (mb
, loc_return
);
3629 if (ret_marshal_type
== MONO_MARSHAL_COPY
)
3630 mono_marshal_emit_xdomain_copy_value (mb
, ret_class
);
3633 mono_mb_emit_byte (mb
, CEE_RET
);
3635 res
= mono_remoting_mb_create_and_cache (method
, mb
, sig
, sig
->param_count
+ 16);
3642 mono_marshal_get_remoting_invoke_for_target (MonoMethod
*method
, MonoRemotingTarget target_type
)
3644 if (target_type
== MONO_REMOTING_TARGET_APPDOMAIN
) {
3645 return mono_marshal_get_xappdomain_invoke (method
);
3646 } else if (target_type
== MONO_REMOTING_TARGET_COMINTEROP
) {
3648 return mono_cominterop_get_invoke (method
);
3650 g_assert_not_reached ();
3653 return mono_marshal_get_remoting_invoke (method
);
3659 G_GNUC_UNUSED
static gpointer
3660 mono_marshal_load_remoting_wrapper (MonoRealProxy
*rp
, MonoMethod
*method
)
3662 if (rp
->target_domain_id
!= -1)
3663 return mono_compile_method (mono_marshal_get_xappdomain_invoke (method
));
3665 return mono_compile_method (mono_marshal_get_remoting_invoke (method
));
3669 mono_marshal_get_remoting_invoke_with_check (MonoMethod
*method
)
3671 MonoMethodSignature
*sig
;
3672 MonoMethodBuilder
*mb
;
3673 MonoMethod
*res
, *native
;
3674 int i
, pos
, pos_rem
;
3678 if (method
->wrapper_type
== MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK
)
3681 /* we cant remote methods without this pointer */
3682 g_assert (mono_method_signature (method
)->hasthis
);
3684 if ((res
= mono_marshal_remoting_find_in_cache (method
, MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK
)))
3687 sig
= mono_signature_no_pinvoke (method
);
3689 mb
= mono_mb_new (method
->klass
, method
->name
, MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK
);
3691 for (i
= 0; i
<= sig
->param_count
; i
++)
3692 mono_mb_emit_ldarg (mb
, i
);
3694 mono_mb_emit_ldarg (mb
, 0);
3695 pos
= mono_mb_emit_proxy_check (mb
, CEE_BNE_UN
);
3697 if (mono_marshal_supports_fast_xdomain (method
)) {
3698 mono_mb_emit_ldarg (mb
, 0);
3699 pos_rem
= mono_mb_emit_xdomain_check (mb
, CEE_BEQ
);
3701 /* wrapper for cross app domain calls */
3702 native
= mono_marshal_get_xappdomain_invoke (method
);
3703 mono_mb_emit_managed_call (mb
, native
, mono_method_signature (native
));
3704 mono_mb_emit_byte (mb
, CEE_RET
);
3706 mono_mb_patch_branch (mb
, pos_rem
);
3708 /* wrapper for normal remote calls */
3709 native
= mono_marshal_get_remoting_invoke (method
);
3710 mono_mb_emit_managed_call (mb
, native
, mono_method_signature (native
));
3711 mono_mb_emit_byte (mb
, CEE_RET
);
3714 mono_mb_patch_branch (mb
, pos
);
3715 mono_mb_emit_managed_call (mb
, method
, mono_method_signature (method
));
3716 mono_mb_emit_byte (mb
, CEE_RET
);
3718 res
= mono_remoting_mb_create_and_cache (method
, mb
, sig
, sig
->param_count
+ 16);
3726 MonoMethodSignature
*sig
;
3728 } SignatureMethodPair
;
3731 signature_method_pair_hash (gconstpointer data
)
3733 SignatureMethodPair
*pair
= (SignatureMethodPair
*)data
;
3735 return mono_signature_hash (pair
->sig
) ^ mono_aligned_addr_hash (pair
->method
);
3739 signature_method_pair_equal (SignatureMethodPair
*pair1
, SignatureMethodPair
*pair2
)
3741 return mono_metadata_signature_equal (pair1
->sig
, pair2
->sig
) && (pair1
->method
== pair2
->method
);
3745 free_signature_method_pair (SignatureMethodPair
*pair
)
3751 * the returned method invokes all methods in a multicast delegate.
3754 mono_marshal_get_delegate_invoke (MonoMethod
*method
, MonoDelegate
*del
)
3756 MonoMethodSignature
*sig
, *static_sig
;
3758 MonoMethodBuilder
*mb
;
3759 MonoMethod
*res
, *newm
;
3761 SignatureMethodPair key
;
3762 SignatureMethodPair
*new_key
;
3763 int local_prev
, local_target
;
3766 MonoMethod
*target_method
= NULL
;
3767 MonoClass
*target_class
= NULL
;
3768 gboolean callvirt
= FALSE
;
3771 * If the delegate target is null, and the target method is not static, a virtual
3772 * call is made to that method with the first delegate argument as this. This is
3773 * a non-documented .NET feature.
3775 if (del
&& !del
->target
&& del
->method
&& mono_method_signature (del
->method
)->hasthis
) {
3777 target_method
= del
->method
;
3778 if (target_method
->is_inflated
) {
3779 MonoType
*target_type
;
3781 g_assert (method
->signature
->hasthis
);
3782 target_type
= mono_class_inflate_generic_type (method
->signature
->params
[0],
3783 mono_method_get_context (method
));
3784 target_class
= mono_class_from_mono_type (target_type
);
3786 target_class
= del
->method
->klass
;
3790 g_assert (method
&& method
->klass
->parent
== mono_defaults
.multicastdelegate_class
&&
3791 !strcmp (method
->name
, "Invoke"));
3793 sig
= mono_signature_no_pinvoke (method
);
3796 /* We need to cache the signature+method pair */
3797 mono_marshal_lock ();
3798 if (!method
->klass
->image
->delegate_abstract_invoke_cache
)
3799 method
->klass
->image
->delegate_abstract_invoke_cache
= g_hash_table_new_full (signature_method_pair_hash
, (GEqualFunc
)signature_method_pair_equal
, (GDestroyNotify
)free_signature_method_pair
, NULL
);
3800 cache
= method
->klass
->image
->delegate_abstract_invoke_cache
;
3802 key
.method
= target_method
;
3803 res
= g_hash_table_lookup (cache
, &key
);
3804 mono_marshal_unlock ();
3808 cache
= get_cache (&method
->klass
->image
->delegate_invoke_cache
,
3809 (GHashFunc
)mono_signature_hash
,
3810 (GCompareFunc
)mono_metadata_signature_equal
);
3811 if ((res
= mono_marshal_find_in_cache (cache
, sig
)))
3815 static_sig
= signature_dup (method
->klass
->image
, sig
);
3816 static_sig
->hasthis
= 0;
3818 name
= mono_signature_to_name (sig
, "invoke");
3819 mb
= mono_mb_new (get_wrapper_target_class (method
->klass
->image
), name
, MONO_WRAPPER_DELEGATE_INVOKE
);
3822 /* allocate local 0 (object) */
3823 local_target
= mono_mb_add_local (mb
, &mono_defaults
.object_class
->byval_arg
);
3824 local_prev
= mono_mb_add_local (mb
, &mono_defaults
.object_class
->byval_arg
);
3826 g_assert (sig
->hasthis
);
3830 * prev.Invoke( args .. );
3831 * return this.<target>( args .. );
3834 /* this wrapper can be used in unmanaged-managed transitions */
3835 emit_thread_interrupt_checkpoint (mb
);
3837 /* get this->prev */
3838 mono_mb_emit_ldarg (mb
, 0);
3839 mono_mb_emit_ldflda (mb
, G_STRUCT_OFFSET (MonoMulticastDelegate
, prev
));
3840 mono_mb_emit_byte (mb
, CEE_LDIND_REF
);
3841 mono_mb_emit_stloc (mb
, local_prev
);
3842 mono_mb_emit_ldloc (mb
, local_prev
);
3844 /* if prev != null */
3845 pos0
= mono_mb_emit_branch (mb
, CEE_BRFALSE
);
3849 mono_mb_emit_byte (mb
, MONO_CUSTOM_PREFIX
);
3850 mono_mb_emit_byte (mb
, CEE_MONO_NOT_TAKEN
);
3852 mono_mb_emit_ldloc (mb
, local_prev
);
3853 for (i
= 0; i
< sig
->param_count
; i
++)
3854 mono_mb_emit_ldarg (mb
, i
+ 1);
3855 mono_mb_emit_op (mb
, CEE_CALLVIRT
, method
);
3856 if (sig
->ret
->type
!= MONO_TYPE_VOID
)
3857 mono_mb_emit_byte (mb
, CEE_POP
);
3859 /* continued or prev == null */
3860 mono_mb_patch_branch (mb
, pos0
);
3862 /* get this->target */
3863 mono_mb_emit_ldarg (mb
, 0);
3864 mono_mb_emit_ldflda (mb
, G_STRUCT_OFFSET (MonoDelegate
, target
));
3865 mono_mb_emit_byte (mb
, CEE_LDIND_REF
);
3866 mono_mb_emit_stloc (mb
, local_target
);
3868 /* if target != null */
3869 mono_mb_emit_ldloc (mb
, local_target
);
3870 pos0
= mono_mb_emit_branch (mb
, CEE_BRFALSE
);
3872 /* then call this->method_ptr nonstatic */
3875 mono_mb_emit_exception_full (mb
, "System", "NotImplementedException", "");
3877 mono_mb_emit_ldloc (mb
, local_target
);
3878 for (i
= 0; i
< sig
->param_count
; ++i
)
3879 mono_mb_emit_ldarg (mb
, i
+ 1);
3880 mono_mb_emit_ldarg (mb
, 0);
3881 mono_mb_emit_ldflda (mb
, G_STRUCT_OFFSET (MonoDelegate
, method_ptr
));
3882 mono_mb_emit_byte (mb
, CEE_LDIND_I
);
3883 mono_mb_emit_op (mb
, CEE_CALLI
, sig
);
3885 mono_mb_emit_byte (mb
, CEE_RET
);
3888 /* else [target == null] call this->method_ptr static */
3889 mono_mb_patch_branch (mb
, pos0
);
3892 mono_mb_emit_ldarg (mb
, 1);
3893 mono_mb_emit_op (mb
, CEE_CASTCLASS
, target_class
);
3894 for (i
= 1; i
< sig
->param_count
; ++i
)
3895 mono_mb_emit_ldarg (mb
, i
+ 1);
3896 mono_mb_emit_op (mb
, CEE_CALLVIRT
, target_method
);
3898 for (i
= 0; i
< sig
->param_count
; ++i
)
3899 mono_mb_emit_ldarg (mb
, i
+ 1);
3900 mono_mb_emit_ldarg (mb
, 0);
3901 mono_mb_emit_ldflda (mb
, G_STRUCT_OFFSET (MonoDelegate
, method_ptr
));
3902 mono_mb_emit_byte (mb
, CEE_LDIND_I
);
3903 mono_mb_emit_op (mb
, CEE_CALLI
, static_sig
);
3906 mono_mb_emit_byte (mb
, CEE_RET
);
3909 // From mono_mb_create_and_cache
3910 mb
->skip_visibility
= 1;
3911 newm
= mono_mb_create_method (mb
, sig
, sig
->param_count
+ 16);
3912 /*We perform double checked locking, so must fence before publishing*/
3913 mono_memory_barrier ();
3914 mono_marshal_lock ();
3915 res
= g_hash_table_lookup (cache
, &key
);
3918 new_key
= g_new0 (SignatureMethodPair
, 1);
3920 new_key
->method
= target_method
;
3921 g_hash_table_insert (cache
, new_key
, res
);
3922 mono_marshal_set_wrapper_info (res
, new_key
);
3923 mono_marshal_unlock ();
3925 mono_marshal_unlock ();
3926 mono_free_method (newm
);
3929 mb
->skip_visibility
= 1;
3930 res
= mono_mb_create_and_cache (cache
, sig
, mb
, sig
, sig
->param_count
+ 16);
3938 * signature_dup_add_this:
3940 * Make a copy of @sig, adding an explicit this argument.
3942 static MonoMethodSignature
*
3943 signature_dup_add_this (MonoMethodSignature
*sig
, MonoClass
*klass
)
3945 MonoMethodSignature
*res
;
3948 res
= mono_metadata_signature_alloc (klass
->image
, sig
->param_count
+ 1);
3949 memcpy (res
, sig
, MONO_SIZEOF_METHOD_SIGNATURE
);
3950 res
->param_count
= sig
->param_count
+ 1;
3951 res
->hasthis
= FALSE
;
3952 for (i
= sig
->param_count
- 1; i
>= 0; i
--)
3953 res
->params
[i
+ 1] = sig
->params
[i
];
3954 res
->params
[0] = &mono_ptr_class_get (&klass
->byval_arg
)->byval_arg
;
3960 MonoMethodSignature
*ctor_sig
;
3961 MonoMethodSignature
*sig
;
3964 /* protected by the marshal lock, contains CtorSigPair pointers */
3965 static GSList
*strsig_list
= NULL
;
3967 static MonoMethodSignature
*
3968 lookup_string_ctor_signature (MonoMethodSignature
*sig
)
3970 MonoMethodSignature
*callsig
;
3974 mono_marshal_lock ();
3976 for (item
= strsig_list
; item
; item
= item
->next
) {
3978 /* mono_metadata_signature_equal () is safe to call with the marshal lock
3979 * because it is lock-free.
3981 if (mono_metadata_signature_equal (sig
, cs
->ctor_sig
)) {
3986 mono_marshal_unlock ();
3990 static MonoMethodSignature
*
3991 add_string_ctor_signature (MonoMethod
*method
)
3993 MonoMethodSignature
*callsig
;
3996 callsig
= signature_dup (method
->klass
->image
, mono_method_signature (method
));
3997 callsig
->ret
= &mono_defaults
.string_class
->byval_arg
;
3998 cs
= g_new (CtorSigPair
, 1);
4000 cs
->ctor_sig
= mono_method_signature (method
);
4002 mono_marshal_lock ();
4003 strsig_list
= g_slist_prepend (strsig_list
, cs
);
4004 mono_marshal_unlock ();
4009 * mono_marshal_get_string_ctor_signature:
4011 * Return the modified signature used by string ctors (they return the newly created
4014 MonoMethodSignature
*
4015 mono_marshal_get_string_ctor_signature (MonoMethod
*method
)
4017 MonoMethodSignature
*sig
= lookup_string_ctor_signature (mono_method_signature (method
));
4019 sig
= add_string_ctor_signature (method
);
4025 get_runtime_invoke_type (MonoType
*t
, gboolean ret
)
4028 /* Can't share this with 'I' as that needs another indirection */
4031 if (MONO_TYPE_IS_REFERENCE (t
))
4032 return &mono_defaults
.object_class
->byval_arg
;
4035 /* The result needs to be boxed */
4041 return &mono_defaults
.sbyte_class
->byval_arg
;
4043 return &mono_defaults
.int16_class
->byval_arg
;
4045 return &mono_defaults
.int32_class
->byval_arg
;
4047 return &mono_defaults
.int64_class
->byval_arg
;
4048 case MONO_TYPE_BOOLEAN
:
4049 return &mono_defaults
.sbyte_class
->byval_arg
;
4050 case MONO_TYPE_CHAR
:
4051 return &mono_defaults
.int16_class
->byval_arg
;
4054 return &mono_defaults
.int_class
->byval_arg
;
4055 case MONO_TYPE_VALUETYPE
:
4056 if (t
->data
.klass
->enumtype
) {
4057 t
= mono_class_enum_basetype (t
->data
.klass
);
4067 * mono_marshal_get_runtime_invoke_sig:
4069 * Return a common signature used for sharing runtime invoke wrappers.
4071 static MonoMethodSignature
*
4072 mono_marshal_get_runtime_invoke_sig (MonoMethodSignature
*sig
)
4074 MonoMethodSignature
*res
= mono_metadata_signature_dup (sig
);
4077 res
->ret
= get_runtime_invoke_type (sig
->ret
, TRUE
);
4078 for (i
= 0; i
< res
->param_count
; ++i
)
4079 res
->params
[i
] = get_runtime_invoke_type (sig
->params
[i
], FALSE
);
4085 runtime_invoke_signature_equal (MonoMethodSignature
*sig1
, MonoMethodSignature
*sig2
)
4087 /* Can't share wrappers which return a vtype since it needs to be boxed */
4088 if (sig1
->ret
!= sig2
->ret
&& !(MONO_TYPE_IS_REFERENCE (sig1
->ret
) && MONO_TYPE_IS_REFERENCE (sig2
->ret
)) && !mono_metadata_type_equal (sig1
->ret
, sig2
->ret
))
4091 return mono_metadata_signature_equal (sig1
, sig2
);
4095 * generates IL code for the runtime invoke function
4096 * MonoObject *runtime_invoke (MonoObject *this, void **params, MonoObject **exc, void* method)
4098 * we also catch exceptions if exc != null
4099 * If VIRTUAL is TRUE, then METHOD is invoked virtually on THIS. This is useful since
4100 * it means that the compiled code for METHOD does not have to be looked up
4101 * before calling the runtime invoke wrapper. In this case, the wrapper ignores
4102 * its METHOD argument.
4105 mono_marshal_get_runtime_invoke (MonoMethod
*method
, gboolean
virtual)
4107 MonoMethodSignature
*sig
, *csig
, *callsig
;
4108 MonoExceptionClause
*clause
;
4109 MonoMethodBuilder
*mb
;
4110 GHashTable
*cache
= NULL
;
4111 MonoClass
*target_klass
;
4112 MonoMethod
*res
= NULL
;
4113 static MonoString
*string_dummy
= NULL
;
4114 static MonoMethodSignature
*cctor_signature
= NULL
;
4115 static MonoMethodSignature
*finalize_signature
= NULL
;
4118 gboolean need_direct_wrapper
= FALSE
;
4119 int *tmp_nullable_locals
;
4123 if (!cctor_signature
) {
4124 cctor_signature
= mono_metadata_signature_alloc (mono_defaults
.corlib
, 0);
4125 cctor_signature
->ret
= &mono_defaults
.void_class
->byval_arg
;
4127 if (!finalize_signature
) {
4128 finalize_signature
= mono_metadata_signature_alloc (mono_defaults
.corlib
, 0);
4129 finalize_signature
->ret
= &mono_defaults
.void_class
->byval_arg
;
4130 finalize_signature
->hasthis
= 1;
4134 need_direct_wrapper
= TRUE
;
4137 * Use a separate cache indexed by methods to speed things up and to avoid the
4138 * boundless mempool growth caused by the signature_dup stuff below.
4141 cache
= get_cache (&method
->klass
->image
->runtime_invoke_vcall_cache
, mono_aligned_addr_hash
, NULL
);
4143 cache
= get_cache (&method
->klass
->image
->runtime_invoke_direct_cache
, mono_aligned_addr_hash
, NULL
);
4144 res
= mono_marshal_find_in_cache (cache
, method
);
4148 if (method
->klass
->rank
&& (method
->iflags
& METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL
) &&
4149 (method
->iflags
& METHOD_IMPL_ATTRIBUTE_NATIVE
)) {
4151 * Array Get/Set/Address methods. The JIT implements them using inline code
4152 * so we need to create an invoke wrapper which calls the method directly.
4154 need_direct_wrapper
= TRUE
;
4157 if (method
->string_ctor
) {
4158 callsig
= lookup_string_ctor_signature (mono_method_signature (method
));
4160 callsig
= add_string_ctor_signature (method
);
4161 /* Can't share this as we push a string as this */
4162 need_direct_wrapper
= TRUE
;
4164 if (method
->klass
->valuetype
&& mono_method_signature (method
)->hasthis
) {
4166 * Valuetype methods receive a managed pointer as the this argument.
4167 * Create a new signature to reflect this.
4169 callsig
= signature_dup_add_this (mono_method_signature (method
), method
->klass
);
4170 /* Can't share this as it would be shared with static methods taking an IntPtr argument */
4171 need_direct_wrapper
= TRUE
;
4173 if (method
->dynamic
)
4174 callsig
= signature_dup (method
->klass
->image
, mono_method_signature (method
));
4176 callsig
= mono_method_signature (method
);
4181 /* Vtypes/nullables/Byrefs cause too many problems */
4182 for (i
= 0; i
< callsig
->param_count
; ++i
) {
4183 if (MONO_TYPE_ISSTRUCT (callsig
->params
[i
]) || callsig
->params
[i
]->byref
)
4184 need_direct_wrapper
= TRUE
;
4189 * We try to share runtime invoke wrappers between different methods but have to
4190 * be careful about methods whose klass has a type cctor, since putting the wrapper
4191 * into that klass would mean that calling a method of klass A might invoke the
4192 * type initializer of class B, or throw an exception if the type initializer
4193 * was called before and failed. See #349621 for an example.
4194 * We avoid that for mscorlib methods by putting every wrapper into the object class.
4196 if (method
->klass
->image
== mono_defaults
.corlib
)
4197 target_klass
= mono_defaults
.object_class
;
4199 /* Try to share wrappers for non-corlib methods with simple signatures */
4200 if (mono_metadata_signature_equal (callsig
, cctor_signature
)) {
4201 callsig
= cctor_signature
;
4202 target_klass
= mono_defaults
.object_class
;
4203 } else if (mono_metadata_signature_equal (callsig
, finalize_signature
)) {
4204 callsig
= finalize_signature
;
4205 target_klass
= mono_defaults
.object_class
;
4207 // FIXME: This breaks too many things
4209 if (mono_class_get_cctor (method->klass))
4210 need_direct_wrapper = TRUE;
4213 target_klass
= get_wrapper_target_class (method
->klass
->image
);
4217 if (need_direct_wrapper
) {
4218 /* Already searched at the start */
4220 MonoMethodSignature
*tmp_sig
;
4222 callsig
= mono_marshal_get_runtime_invoke_sig (callsig
);
4224 cache
= get_cache (&target_klass
->image
->runtime_invoke_cache
,
4225 (GHashFunc
)mono_signature_hash
,
4226 (GCompareFunc
)runtime_invoke_signature_equal
);
4228 /* from mono_marshal_find_in_cache */
4229 mono_marshal_lock ();
4230 res
= g_hash_table_lookup (cache
, callsig
);
4231 mono_marshal_unlock ();
4238 /* Make a copy of the signature from the image mempool */
4240 callsig
= mono_metadata_signature_dup_full (target_klass
->image
, callsig
);
4244 /* to make it work with our special string constructors */
4245 if (!string_dummy
) {
4246 MONO_GC_REGISTER_ROOT (string_dummy
);
4247 string_dummy
= mono_string_new_wrapper ("dummy");
4250 sig
= mono_method_signature (method
);
4252 csig
= mono_metadata_signature_alloc (target_klass
->image
, 4);
4254 csig
->ret
= &mono_defaults
.object_class
->byval_arg
;
4255 if (method
->klass
->valuetype
&& mono_method_signature (method
)->hasthis
)
4256 csig
->params
[0] = callsig
->params
[0];
4258 csig
->params
[0] = &mono_defaults
.object_class
->byval_arg
;
4259 csig
->params
[1] = &mono_defaults
.int_class
->byval_arg
;
4260 csig
->params
[2] = &mono_defaults
.int_class
->byval_arg
;
4261 csig
->params
[3] = &mono_defaults
.int_class
->byval_arg
;
4263 name
= mono_signature_to_name (callsig
, virtual ? "runtime_invoke_virtual" : "runtime_invoke");
4264 mb
= mono_mb_new (target_klass
, name
, MONO_WRAPPER_RUNTIME_INVOKE
);
4267 /* allocate local 0 (object) tmp */
4268 mono_mb_add_local (mb
, &mono_defaults
.object_class
->byval_arg
);
4269 /* allocate local 1 (object) exc */
4270 mono_mb_add_local (mb
, &mono_defaults
.object_class
->byval_arg
);
4272 /* cond set *exc to null */
4273 mono_mb_emit_byte (mb
, CEE_LDARG_2
);
4274 mono_mb_emit_byte (mb
, CEE_BRFALSE_S
);
4275 mono_mb_emit_byte (mb
, 3);
4276 mono_mb_emit_byte (mb
, CEE_LDARG_2
);
4277 mono_mb_emit_byte (mb
, CEE_LDNULL
);
4278 mono_mb_emit_byte (mb
, CEE_STIND_REF
);
4280 emit_thread_force_interrupt_checkpoint (mb
);
4283 g_assert (sig
->hasthis
);
4284 g_assert (method
->flags
& METHOD_ATTRIBUTE_VIRTUAL
);
4288 if (method
->string_ctor
) {
4289 mono_mb_emit_ptr (mb
, string_dummy
);
4291 mono_mb_emit_ldarg (mb
, 0);
4295 tmp_nullable_locals
= g_new0 (int, sig
->param_count
);
4297 for (i
= 0; i
< sig
->param_count
; i
++) {
4298 MonoType
*t
= sig
->params
[i
];
4301 mono_mb_emit_ldarg (mb
, 1);
4303 mono_mb_emit_icon (mb
, sizeof (gpointer
) * i
);
4304 mono_mb_emit_byte (mb
, CEE_ADD
);
4308 mono_mb_emit_byte (mb
, CEE_LDIND_I
);
4309 /* A Nullable<T> type don't have a boxed form, it's either null or a boxed T.
4310 * So to make this work we unbox it to a local variablee and push a reference to that.
4312 if (t
->type
== MONO_TYPE_GENERICINST
&& mono_class_is_nullable (mono_class_from_mono_type (t
))) {
4313 tmp_nullable_locals
[i
] = mono_mb_add_local (mb
, &mono_class_from_mono_type (t
)->byval_arg
);
4315 mono_mb_emit_op (mb
, CEE_UNBOX_ANY
, mono_class_from_mono_type (t
));
4316 mono_mb_emit_stloc (mb
, tmp_nullable_locals
[i
]);
4317 mono_mb_emit_ldloc_addr (mb
, tmp_nullable_locals
[i
]);
4322 /*FIXME 'this doesn't handle generic enums. Shouldn't we?*/
4323 type
= sig
->params
[i
]->type
;
4327 case MONO_TYPE_BOOLEAN
:
4331 case MONO_TYPE_CHAR
:
4340 mono_mb_emit_byte (mb
, CEE_LDIND_I
);
4341 mono_mb_emit_byte (mb
, mono_type_to_ldind (sig
->params
[i
]));
4343 case MONO_TYPE_STRING
:
4344 case MONO_TYPE_CLASS
:
4345 case MONO_TYPE_ARRAY
:
4347 case MONO_TYPE_SZARRAY
:
4348 case MONO_TYPE_OBJECT
:
4349 mono_mb_emit_byte (mb
, mono_type_to_ldind (sig
->params
[i
]));
4351 case MONO_TYPE_GENERICINST
:
4352 if (!mono_type_generic_inst_is_valuetype (sig
->params
[i
])) {
4353 mono_mb_emit_byte (mb
, mono_type_to_ldind (sig
->params
[i
]));
4358 case MONO_TYPE_VALUETYPE
:
4359 if (type
== MONO_TYPE_VALUETYPE
&& t
->data
.klass
->enumtype
) {
4360 type
= mono_class_enum_basetype (t
->data
.klass
)->type
;
4363 mono_mb_emit_byte (mb
, CEE_LDIND_I
);
4364 if (mono_class_is_nullable (mono_class_from_mono_type (sig
->params
[i
]))) {
4365 /* Need to convert a boxed vtype to an mp to a Nullable struct */
4366 mono_mb_emit_op (mb
, CEE_UNBOX
, mono_class_from_mono_type (sig
->params
[i
]));
4367 mono_mb_emit_op (mb
, CEE_LDOBJ
, mono_class_from_mono_type (sig
->params
[i
]));
4369 mono_mb_emit_op (mb
, CEE_LDOBJ
, mono_class_from_mono_type (sig
->params
[i
]));
4373 g_assert_not_reached ();
4378 mono_mb_emit_op (mb
, CEE_CALLVIRT
, method
);
4379 } else if (need_direct_wrapper
) {
4380 mono_mb_emit_op (mb
, CEE_CALL
, method
);
4382 mono_mb_emit_ldarg (mb
, 3);
4383 mono_mb_emit_calli (mb
, callsig
);
4386 if (sig
->ret
->byref
) {
4388 g_assert_not_reached ();
4391 switch (sig
->ret
->type
) {
4392 case MONO_TYPE_VOID
:
4393 if (!method
->string_ctor
)
4394 mono_mb_emit_byte (mb
, CEE_LDNULL
);
4396 case MONO_TYPE_BOOLEAN
:
4397 case MONO_TYPE_CHAR
:
4410 case MONO_TYPE_VALUETYPE
:
4411 case MONO_TYPE_TYPEDBYREF
:
4412 case MONO_TYPE_GENERICINST
:
4413 /* box value types */
4414 mono_mb_emit_op (mb
, CEE_BOX
, mono_class_from_mono_type (sig
->ret
));
4416 case MONO_TYPE_STRING
:
4417 case MONO_TYPE_CLASS
:
4418 case MONO_TYPE_ARRAY
:
4419 case MONO_TYPE_SZARRAY
:
4420 case MONO_TYPE_OBJECT
:
4424 /* The result is an IntPtr */
4425 mono_mb_emit_op (mb
, CEE_BOX
, mono_defaults
.int_class
);
4428 g_assert_not_reached ();
4431 mono_mb_emit_stloc (mb
, 0);
4433 /* Convert back nullable-byref arguments */
4434 for (i
= 0; i
< sig
->param_count
; i
++) {
4435 MonoType
*t
= sig
->params
[i
];
4438 * Box the result and put it back into the array, the caller will have
4439 * to obtain it from there.
4441 if (t
->byref
&& t
->type
== MONO_TYPE_GENERICINST
&& mono_class_is_nullable (mono_class_from_mono_type (t
))) {
4442 mono_mb_emit_ldarg (mb
, 1);
4443 mono_mb_emit_icon (mb
, sizeof (gpointer
) * i
);
4444 mono_mb_emit_byte (mb
, CEE_ADD
);
4446 mono_mb_emit_ldloc (mb
, tmp_nullable_locals
[i
]);
4447 mono_mb_emit_op (mb
, CEE_BOX
, mono_class_from_mono_type (t
));
4449 mono_mb_emit_byte (mb
, CEE_STIND_REF
);
4453 pos
= mono_mb_emit_branch (mb
, CEE_LEAVE
);
4455 clause
= mono_image_alloc0 (target_klass
->image
, sizeof (MonoExceptionClause
));
4456 clause
->flags
= MONO_EXCEPTION_CLAUSE_FILTER
;
4457 clause
->try_len
= mono_mb_get_label (mb
);
4460 clause
->data
.filter_offset
= mono_mb_get_label (mb
);
4462 mono_mb_emit_byte (mb
, CEE_POP
);
4463 mono_mb_emit_byte (mb
, CEE_LDARG_2
);
4464 mono_mb_emit_byte (mb
, CEE_LDC_I4_0
);
4465 mono_mb_emit_byte (mb
, CEE_PREFIX1
);
4466 mono_mb_emit_byte (mb
, CEE_CGT_UN
);
4467 mono_mb_emit_byte (mb
, CEE_PREFIX1
);
4468 mono_mb_emit_byte (mb
, CEE_ENDFILTER
);
4470 clause
->handler_offset
= mono_mb_get_label (mb
);
4473 /* store exception */
4474 mono_mb_emit_stloc (mb
, 1);
4476 mono_mb_emit_byte (mb
, CEE_LDARG_2
);
4477 mono_mb_emit_ldloc (mb
, 1);
4478 mono_mb_emit_byte (mb
, CEE_STIND_REF
);
4480 mono_mb_emit_byte (mb
, CEE_LDNULL
);
4481 mono_mb_emit_stloc (mb
, 0);
4483 mono_mb_emit_branch (mb
, CEE_LEAVE
);
4485 clause
->handler_len
= mono_mb_get_pos (mb
) - clause
->handler_offset
;
4487 mono_mb_set_clauses (mb
, 1, clause
);
4490 mono_mb_patch_branch (mb
, pos
);
4491 mono_mb_emit_ldloc (mb
, 0);
4492 mono_mb_emit_byte (mb
, CEE_RET
);
4494 g_free (tmp_nullable_locals
);
4496 if (need_direct_wrapper
) {
4497 mb
->skip_visibility
= 1;
4498 res
= mono_mb_create_and_cache (cache
, method
, mb
, csig
, sig
->param_count
+ 16);
4500 /* taken from mono_mb_create_and_cache */
4501 mono_marshal_lock ();
4502 res
= g_hash_table_lookup (cache
, callsig
);
4503 mono_marshal_unlock ();
4505 /* Somebody may have created it before us */
4508 newm
= mono_mb_create_method (mb
, csig
, sig
->param_count
+ 16);
4510 mono_marshal_lock ();
4511 res
= g_hash_table_lookup (cache
, callsig
);
4514 g_hash_table_insert (cache
, callsig
, res
);
4515 /* Can't insert it into wrapper_hash since the key is a signature */
4516 g_hash_table_insert (method
->klass
->image
->runtime_invoke_direct_cache
, method
, res
);
4518 mono_free_method (newm
);
4520 mono_marshal_unlock ();
4523 /* end mono_mb_create_and_cache */
4532 * mono_marshal_get_runtime_invoke_dynamic:
4534 * Return a method which can be used to invoke managed methods from native code
4536 * The signature of the returned method is given by RuntimeInvokeDynamicFunction:
4537 * void runtime_invoke (void *args, MonoObject **exc, void *compiled_method)
4538 * ARGS should point to an architecture specific structure containing
4539 * the arguments and space for the return value.
4540 * The other arguments are the same as for runtime_invoke (), except that
4541 * ARGS should contain the this argument too.
4542 * This wrapper serves the same purpose as the runtime-invoke wrappers, but there
4543 * is only one copy of it, which is useful in full-aot.
4546 mono_marshal_get_runtime_invoke_dynamic (void)
4548 static MonoMethod
*method
;
4549 MonoMethodSignature
*csig
;
4550 MonoExceptionClause
*clause
;
4551 MonoMethodBuilder
*mb
;
4558 csig
= mono_metadata_signature_alloc (mono_defaults
.corlib
, 4);
4560 csig
->ret
= &mono_defaults
.void_class
->byval_arg
;
4561 csig
->params
[0] = &mono_defaults
.int_class
->byval_arg
;
4562 csig
->params
[1] = &mono_defaults
.int_class
->byval_arg
;
4563 csig
->params
[2] = &mono_defaults
.int_class
->byval_arg
;
4564 csig
->params
[3] = &mono_defaults
.int_class
->byval_arg
;
4566 name
= g_strdup ("runtime_invoke_dynamic");
4567 mb
= mono_mb_new (mono_defaults
.object_class
, name
, MONO_WRAPPER_RUNTIME_INVOKE
);
4570 /* allocate local 0 (object) tmp */
4571 mono_mb_add_local (mb
, &mono_defaults
.object_class
->byval_arg
);
4572 /* allocate local 1 (object) exc */
4573 mono_mb_add_local (mb
, &mono_defaults
.object_class
->byval_arg
);
4575 /* cond set *exc to null */
4576 mono_mb_emit_byte (mb
, CEE_LDARG_1
);
4577 mono_mb_emit_byte (mb
, CEE_BRFALSE_S
);
4578 mono_mb_emit_byte (mb
, 3);
4579 mono_mb_emit_byte (mb
, CEE_LDARG_1
);
4580 mono_mb_emit_byte (mb
, CEE_LDNULL
);
4581 mono_mb_emit_byte (mb
, CEE_STIND_REF
);
4583 emit_thread_force_interrupt_checkpoint (mb
);
4585 mono_mb_emit_byte (mb
, CEE_LDARG_0
);
4586 mono_mb_emit_byte (mb
, CEE_LDARG_2
);
4587 mono_mb_emit_byte (mb
, MONO_CUSTOM_PREFIX
);
4588 mono_mb_emit_byte (mb
, CEE_MONO_DYN_CALL
);
4590 pos
= mono_mb_emit_branch (mb
, CEE_LEAVE
);
4592 clause
= mono_image_alloc0 (mono_defaults
.corlib
, sizeof (MonoExceptionClause
));
4593 clause
->flags
= MONO_EXCEPTION_CLAUSE_FILTER
;
4594 clause
->try_len
= mono_mb_get_label (mb
);
4597 clause
->data
.filter_offset
= mono_mb_get_label (mb
);
4599 mono_mb_emit_byte (mb
, CEE_POP
);
4600 mono_mb_emit_byte (mb
, CEE_LDARG_1
);
4601 mono_mb_emit_byte (mb
, CEE_LDC_I4_0
);
4602 mono_mb_emit_byte (mb
, CEE_PREFIX1
);
4603 mono_mb_emit_byte (mb
, CEE_CGT_UN
);
4604 mono_mb_emit_byte (mb
, CEE_PREFIX1
);
4605 mono_mb_emit_byte (mb
, CEE_ENDFILTER
);
4607 clause
->handler_offset
= mono_mb_get_label (mb
);
4610 /* store exception */
4611 mono_mb_emit_stloc (mb
, 1);
4613 mono_mb_emit_byte (mb
, CEE_LDARG_1
);
4614 mono_mb_emit_ldloc (mb
, 1);
4615 mono_mb_emit_byte (mb
, CEE_STIND_REF
);
4617 mono_mb_emit_byte (mb
, CEE_LDNULL
);
4618 mono_mb_emit_stloc (mb
, 0);
4620 /* Check for the abort exception */
4621 mono_mb_emit_ldloc (mb
, 1);
4622 mono_mb_emit_op (mb
, CEE_ISINST
, mono_defaults
.threadabortexception_class
);
4623 posna
= mono_mb_emit_short_branch (mb
, CEE_BRFALSE_S
);
4625 /* Delay the abort exception */
4626 mono_mb_emit_icall (mb
, ves_icall_System_Threading_Thread_ResetAbort
);
4628 mono_mb_patch_short_branch (mb
, posna
);
4629 mono_mb_emit_branch (mb
, CEE_LEAVE
);
4631 clause
->handler_len
= mono_mb_get_pos (mb
) - clause
->handler_offset
;
4633 mono_mb_set_clauses (mb
, 1, clause
);
4636 mono_mb_patch_branch (mb
, pos
);
4637 //mono_mb_emit_ldloc (mb, 0);
4638 mono_mb_emit_byte (mb
, CEE_RET
);
4640 mono_loader_lock ();
4641 /* double-checked locking */
4643 method
= mono_mb_create_method (mb
, csig
, 16);
4645 mono_loader_unlock ();
4653 mono_mb_emit_auto_layout_exception (MonoMethodBuilder
*mb
, MonoClass
*klass
)
4655 char *msg
= g_strdup_printf ("The type `%s.%s' layout needs to be Sequential or Explicit",
4656 klass
->name_space
, klass
->name
);
4658 mono_mb_emit_exception_marshal_directive (mb
, msg
);
4662 * mono_marshal_get_ldfld_remote_wrapper:
4663 * @klass: The return type
4665 * This method generates a wrapper for calling mono_load_remote_field_new.
4666 * The return type is ignored for now, as mono_load_remote_field_new () always
4667 * returns an object. In the future, to optimize some codepaths, we might
4668 * call a different function that takes a pointer to a valuetype, instead.
4671 mono_marshal_get_ldfld_remote_wrapper (MonoClass
*klass
)
4673 MonoMethodSignature
*sig
, *csig
;
4674 MonoMethodBuilder
*mb
;
4676 static MonoMethod
* cached
= NULL
;
4678 mono_marshal_lock ();
4680 mono_marshal_unlock ();
4683 mono_marshal_unlock ();
4685 mb
= mono_mb_new_no_dup_name (mono_defaults
.object_class
, "__mono_load_remote_field_new_wrapper", MONO_WRAPPER_LDFLD_REMOTE
);
4687 mb
->method
->save_lmf
= 1;
4689 sig
= mono_metadata_signature_alloc (mono_defaults
.corlib
, 3);
4690 sig
->params
[0] = &mono_defaults
.object_class
->byval_arg
;
4691 sig
->params
[1] = &mono_defaults
.int_class
->byval_arg
;
4692 sig
->params
[2] = &mono_defaults
.int_class
->byval_arg
;
4693 sig
->ret
= &mono_defaults
.object_class
->byval_arg
;
4695 mono_mb_emit_ldarg (mb
, 0);
4696 mono_mb_emit_ldarg (mb
, 1);
4697 mono_mb_emit_ldarg (mb
, 2);
4699 csig
= mono_metadata_signature_alloc (mono_defaults
.corlib
, 3);
4700 csig
->params
[0] = &mono_defaults
.object_class
->byval_arg
;
4701 csig
->params
[1] = &mono_defaults
.int_class
->byval_arg
;
4702 csig
->params
[2] = &mono_defaults
.int_class
->byval_arg
;
4703 csig
->ret
= &mono_defaults
.object_class
->byval_arg
;
4706 mono_mb_emit_native_call (mb
, csig
, mono_load_remote_field_new
);
4707 emit_thread_interrupt_checkpoint (mb
);
4709 mono_mb_emit_byte (mb
, CEE_RET
);
4711 mono_marshal_lock ();
4713 mono_marshal_unlock ();
4716 newm
= mono_mb_create_method (mb
, sig
, 4);
4717 mono_marshal_lock ();
4722 mono_marshal_unlock ();
4724 mono_marshal_unlock ();
4725 mono_free_method (newm
);
4734 * mono_marshal_get_ldfld_wrapper:
4735 * @type: the type of the field
4737 * This method generates a function which can be use to load a field with type
4738 * @type from an object. The generated function has the following signature:
4739 * <@type> ldfld_wrapper (MonoObject *this, MonoClass *class, MonoClassField *field, int offset)
4742 mono_marshal_get_ldfld_wrapper (MonoType
*type
)
4744 MonoMethodSignature
*sig
;
4745 MonoMethodBuilder
*mb
;
4750 int t
, pos0
, pos1
= 0;
4752 type
= mono_type_get_underlying_type (type
);
4757 if (type
->type
== MONO_TYPE_SZARRAY
) {
4758 klass
= mono_defaults
.array_class
;
4759 } else if (type
->type
== MONO_TYPE_VALUETYPE
) {
4760 klass
= type
->data
.klass
;
4761 } else if (t
== MONO_TYPE_OBJECT
|| t
== MONO_TYPE_CLASS
|| t
== MONO_TYPE_STRING
) {
4762 klass
= mono_defaults
.object_class
;
4763 } else if (t
== MONO_TYPE_PTR
|| t
== MONO_TYPE_FNPTR
) {
4764 klass
= mono_defaults
.int_class
;
4765 } else if (t
== MONO_TYPE_GENERICINST
) {
4766 if (mono_type_generic_inst_is_valuetype (type
))
4767 klass
= mono_class_from_mono_type (type
);
4769 klass
= mono_defaults
.object_class
;
4771 klass
= mono_class_from_mono_type (type
);
4774 klass
= mono_defaults
.int_class
;
4777 cache
= get_cache (&klass
->image
->ldfld_wrapper_cache
, mono_aligned_addr_hash
, NULL
);
4778 if ((res
= mono_marshal_find_in_cache (cache
, klass
)))
4781 /* we add the %p pointer value of klass because class names are not unique */
4782 name
= g_strdup_printf ("__ldfld_wrapper_%p_%s.%s", klass
, klass
->name_space
, klass
->name
);
4783 mb
= mono_mb_new (mono_defaults
.object_class
, name
, MONO_WRAPPER_LDFLD
);
4786 sig
= mono_metadata_signature_alloc (mono_defaults
.corlib
, 4);
4787 sig
->params
[0] = &mono_defaults
.object_class
->byval_arg
;
4788 sig
->params
[1] = &mono_defaults
.int_class
->byval_arg
;
4789 sig
->params
[2] = &mono_defaults
.int_class
->byval_arg
;
4790 sig
->params
[3] = &mono_defaults
.int_class
->byval_arg
;
4791 sig
->ret
= &klass
->byval_arg
;
4793 mono_mb_emit_ldarg (mb
, 0);
4794 pos0
= mono_mb_emit_proxy_check (mb
, CEE_BNE_UN
);
4796 mono_mb_emit_ldarg (mb
, 0);
4797 mono_mb_emit_ldarg (mb
, 1);
4798 mono_mb_emit_ldarg (mb
, 2);
4800 mono_mb_emit_managed_call (mb
, mono_marshal_get_ldfld_remote_wrapper (klass
), NULL
);
4803 csig = mono_metadata_signature_alloc (mono_defaults.corlib, 3);
4804 csig->params [0] = &mono_defaults.object_class->byval_arg;
4805 csig->params [1] = &mono_defaults.int_class->byval_arg;
4806 csig->params [2] = &mono_defaults.int_class->byval_arg;
4807 csig->ret = &klass->this_arg;
4810 mono_mb_emit_native_call (mb, csig, mono_load_remote_field_new);
4811 emit_thread_interrupt_checkpoint (mb);
4814 if (klass
->valuetype
) {
4815 mono_mb_emit_op (mb
, CEE_UNBOX
, klass
);
4816 pos1
= mono_mb_emit_branch (mb
, CEE_BR
);
4818 mono_mb_emit_byte (mb
, CEE_RET
);
4822 mono_mb_patch_branch (mb
, pos0
);
4824 mono_mb_emit_ldarg (mb
, 0);
4825 mono_mb_emit_byte (mb
, MONO_CUSTOM_PREFIX
);
4826 mono_mb_emit_byte (mb
, CEE_MONO_OBJADDR
);
4827 mono_mb_emit_ldarg (mb
, 3);
4828 mono_mb_emit_byte (mb
, CEE_ADD
);
4830 if (klass
->valuetype
)
4831 mono_mb_patch_branch (mb
, pos1
);
4836 case MONO_TYPE_BOOLEAN
:
4837 case MONO_TYPE_CHAR
:
4846 case MONO_TYPE_ARRAY
:
4847 case MONO_TYPE_SZARRAY
:
4848 case MONO_TYPE_OBJECT
:
4849 case MONO_TYPE_CLASS
:
4850 case MONO_TYPE_STRING
:
4854 case MONO_TYPE_FNPTR
:
4855 mono_mb_emit_byte (mb
, mono_type_to_ldind (type
));
4857 case MONO_TYPE_VALUETYPE
:
4858 g_assert (!klass
->enumtype
);
4859 mono_mb_emit_op (mb
, CEE_LDOBJ
, klass
);
4861 case MONO_TYPE_GENERICINST
:
4862 if (mono_type_generic_inst_is_valuetype (type
)) {
4863 mono_mb_emit_op (mb
, CEE_LDOBJ
, klass
);
4865 mono_mb_emit_byte (mb
, CEE_LDIND_REF
);
4869 g_warning ("type %x not implemented", type
->type
);
4870 g_assert_not_reached ();
4873 mono_mb_emit_byte (mb
, CEE_RET
);
4875 res
= mono_mb_create_and_cache (cache
, klass
,
4876 mb
, sig
, sig
->param_count
+ 16);
4883 * mono_marshal_get_ldflda_wrapper:
4884 * @type: the type of the field
4886 * This method generates a function which can be used to load a field address
4887 * from an object. The generated function has the following signature:
4888 * gpointer ldflda_wrapper (MonoObject *this, MonoClass *class, MonoClassField *field, int offset);
4891 mono_marshal_get_ldflda_wrapper (MonoType
*type
)
4893 MonoMethodSignature
*sig
;
4894 MonoMethodBuilder
*mb
;
4899 int t
, pos0
, pos1
, pos2
, pos3
;
4901 type
= mono_type_get_underlying_type (type
);
4905 if (type
->type
== MONO_TYPE_SZARRAY
) {
4906 klass
= mono_defaults
.array_class
;
4907 } else if (type
->type
== MONO_TYPE_VALUETYPE
) {
4908 klass
= type
->data
.klass
;
4909 } else if (t
== MONO_TYPE_OBJECT
|| t
== MONO_TYPE_CLASS
|| t
== MONO_TYPE_STRING
||
4910 t
== MONO_TYPE_CLASS
) {
4911 klass
= mono_defaults
.object_class
;
4912 } else if (t
== MONO_TYPE_PTR
|| t
== MONO_TYPE_FNPTR
) {
4913 klass
= mono_defaults
.int_class
;
4914 } else if (t
== MONO_TYPE_GENERICINST
) {
4915 if (mono_type_generic_inst_is_valuetype (type
))
4916 klass
= mono_class_from_mono_type (type
);
4918 klass
= mono_defaults
.object_class
;
4920 klass
= mono_class_from_mono_type (type
);
4923 klass
= mono_defaults
.int_class
;
4926 cache
= get_cache (&klass
->image
->ldflda_wrapper_cache
, mono_aligned_addr_hash
, NULL
);
4927 if ((res
= mono_marshal_find_in_cache (cache
, klass
)))
4930 /* we add the %p pointer value of klass because class names are not unique */
4931 name
= g_strdup_printf ("__ldflda_wrapper_%p_%s.%s", klass
, klass
->name_space
, klass
->name
);
4932 mb
= mono_mb_new (mono_defaults
.object_class
, name
, MONO_WRAPPER_LDFLDA
);
4935 sig
= mono_metadata_signature_alloc (mono_defaults
.corlib
, 4);
4936 sig
->params
[0] = &mono_defaults
.object_class
->byval_arg
;
4937 sig
->params
[1] = &mono_defaults
.int_class
->byval_arg
;
4938 sig
->params
[2] = &mono_defaults
.int_class
->byval_arg
;
4939 sig
->params
[3] = &mono_defaults
.int_class
->byval_arg
;
4940 sig
->ret
= &mono_defaults
.int_class
->byval_arg
;
4942 /* if typeof (this) != transparent_proxy goto pos0 */
4943 mono_mb_emit_ldarg (mb
, 0);
4944 pos0
= mono_mb_emit_proxy_check (mb
, CEE_BNE_UN
);
4946 /* if same_appdomain goto pos1 */
4947 mono_mb_emit_ldarg (mb
, 0);
4948 pos1
= mono_mb_emit_xdomain_check (mb
, CEE_BEQ
);
4950 mono_mb_emit_exception_full (mb
, "System", "InvalidOperationException", "Attempt to load field address from object in another appdomain.");
4952 /* same app domain */
4953 mono_mb_patch_branch (mb
, pos1
);
4955 /* if typeof (this) != contextbound goto pos2 */
4956 mono_mb_emit_ldarg (mb
, 0);
4957 pos2
= mono_mb_emit_contextbound_check (mb
, CEE_BEQ
);
4959 /* if this->rp->context == mono_context_get goto pos3 */
4960 mono_mb_emit_ldarg (mb
, 0);
4961 mono_mb_emit_ldflda (mb
, G_STRUCT_OFFSET (MonoTransparentProxy
, rp
));
4962 mono_mb_emit_byte (mb
, CEE_LDIND_REF
);
4963 mono_mb_emit_ldflda (mb
, G_STRUCT_OFFSET (MonoRealProxy
, context
));
4964 mono_mb_emit_byte (mb
, CEE_LDIND_REF
);
4965 mono_mb_emit_icall (mb
, mono_context_get
);
4966 pos3
= mono_mb_emit_branch (mb
, CEE_BEQ
);
4968 mono_mb_emit_exception_full (mb
, "System", "InvalidOperationException", "Attempt to load field address from object in another context.");
4970 mono_mb_patch_branch (mb
, pos2
);
4971 mono_mb_patch_branch (mb
, pos3
);
4973 /* return the address of the field from this->rp->unwrapped_server */
4974 mono_mb_emit_ldarg (mb
, 0);
4975 mono_mb_emit_ldflda (mb
, G_STRUCT_OFFSET (MonoTransparentProxy
, rp
));
4976 mono_mb_emit_byte (mb
, CEE_LDIND_REF
);
4977 mono_mb_emit_ldflda (mb
, G_STRUCT_OFFSET (MonoRealProxy
, unwrapped_server
));
4978 mono_mb_emit_byte (mb
, CEE_LDIND_REF
);
4979 mono_mb_emit_byte (mb
, MONO_CUSTOM_PREFIX
);
4980 mono_mb_emit_byte (mb
, CEE_MONO_OBJADDR
);
4981 mono_mb_emit_ldarg (mb
, 3);
4982 mono_mb_emit_byte (mb
, CEE_ADD
);
4983 mono_mb_emit_byte (mb
, CEE_RET
);
4985 /* not a proxy: return the address of the field directly */
4986 mono_mb_patch_branch (mb
, pos0
);
4988 mono_mb_emit_ldarg (mb
, 0);
4989 mono_mb_emit_byte (mb
, MONO_CUSTOM_PREFIX
);
4990 mono_mb_emit_byte (mb
, CEE_MONO_OBJADDR
);
4991 mono_mb_emit_ldarg (mb
, 3);
4992 mono_mb_emit_byte (mb
, CEE_ADD
);
4994 mono_mb_emit_byte (mb
, CEE_RET
);
4996 res
= mono_mb_create_and_cache (cache
, klass
,
4997 mb
, sig
, sig
->param_count
+ 16);
5004 * mono_marshal_get_stfld_remote_wrapper:
5005 * klass: The type of the field
5007 * This function generates a wrapper for calling mono_store_remote_field_new
5008 * with the appropriate signature.
5009 * Similarly to mono_marshal_get_ldfld_remote_wrapper () this doesn't depend on the
5010 * klass argument anymore.
5013 mono_marshal_get_stfld_remote_wrapper (MonoClass
*klass
)
5015 MonoMethodSignature
*sig
, *csig
;
5016 MonoMethodBuilder
*mb
;
5018 static MonoMethod
*cached
= NULL
;
5020 mono_marshal_lock ();
5022 mono_marshal_unlock ();
5025 mono_marshal_unlock ();
5027 mb
= mono_mb_new_no_dup_name (mono_defaults
.object_class
, "__mono_store_remote_field_new_wrapper", MONO_WRAPPER_STFLD_REMOTE
);
5029 mb
->method
->save_lmf
= 1;
5031 sig
= mono_metadata_signature_alloc (mono_defaults
.corlib
, 4);
5032 sig
->params
[0] = &mono_defaults
.object_class
->byval_arg
;
5033 sig
->params
[1] = &mono_defaults
.int_class
->byval_arg
;
5034 sig
->params
[2] = &mono_defaults
.int_class
->byval_arg
;
5035 sig
->params
[3] = &mono_defaults
.object_class
->byval_arg
;
5036 sig
->ret
= &mono_defaults
.void_class
->byval_arg
;
5038 mono_mb_emit_ldarg (mb
, 0);
5039 mono_mb_emit_ldarg (mb
, 1);
5040 mono_mb_emit_ldarg (mb
, 2);
5041 mono_mb_emit_ldarg (mb
, 3);
5043 csig
= mono_metadata_signature_alloc (mono_defaults
.corlib
, 4);
5044 csig
->params
[0] = &mono_defaults
.object_class
->byval_arg
;
5045 csig
->params
[1] = &mono_defaults
.int_class
->byval_arg
;
5046 csig
->params
[2] = &mono_defaults
.int_class
->byval_arg
;
5047 csig
->params
[3] = &mono_defaults
.object_class
->byval_arg
;
5048 csig
->ret
= &mono_defaults
.void_class
->byval_arg
;
5051 mono_mb_emit_native_call (mb
, csig
, mono_store_remote_field_new
);
5052 emit_thread_interrupt_checkpoint (mb
);
5054 mono_mb_emit_byte (mb
, CEE_RET
);
5056 mono_marshal_lock ();
5058 mono_marshal_unlock ();
5061 newm
= mono_mb_create_method (mb
, sig
, 6);
5062 mono_marshal_lock ();
5067 mono_marshal_unlock ();
5069 mono_marshal_unlock ();
5070 mono_free_method (newm
);
5079 * mono_marshal_get_stfld_wrapper:
5080 * @type: the type of the field
5082 * This method generates a function which can be use to store a field with type
5083 * @type. The generated function has the following signature:
5084 * void stfld_wrapper (MonoObject *this, MonoClass *class, MonoClassField *field, int offset, <@type> val)
5087 mono_marshal_get_stfld_wrapper (MonoType
*type
)
5089 MonoMethodSignature
*sig
;
5090 MonoMethodBuilder
*mb
;
5097 type
= mono_type_get_underlying_type (type
);
5101 if (type
->type
== MONO_TYPE_SZARRAY
) {
5102 klass
= mono_defaults
.array_class
;
5103 } else if (type
->type
== MONO_TYPE_VALUETYPE
) {
5104 klass
= type
->data
.klass
;
5105 } else if (t
== MONO_TYPE_OBJECT
|| t
== MONO_TYPE_CLASS
|| t
== MONO_TYPE_STRING
) {
5106 klass
= mono_defaults
.object_class
;
5107 } else if (t
== MONO_TYPE_PTR
|| t
== MONO_TYPE_FNPTR
) {
5108 klass
= mono_defaults
.int_class
;
5109 } else if (t
== MONO_TYPE_GENERICINST
) {
5110 if (mono_type_generic_inst_is_valuetype (type
))
5111 klass
= mono_class_from_mono_type (type
);
5113 klass
= mono_defaults
.object_class
;
5115 klass
= mono_class_from_mono_type (type
);
5118 klass
= mono_defaults
.int_class
;
5121 cache
= get_cache (&klass
->image
->stfld_wrapper_cache
, mono_aligned_addr_hash
, NULL
);
5122 if ((res
= mono_marshal_find_in_cache (cache
, klass
)))
5125 /* we add the %p pointer value of klass because class names are not unique */
5126 name
= g_strdup_printf ("__stfld_wrapper_%p_%s.%s", klass
, klass
->name_space
, klass
->name
);
5127 mb
= mono_mb_new (mono_defaults
.object_class
, name
, MONO_WRAPPER_STFLD
);
5130 sig
= mono_metadata_signature_alloc (mono_defaults
.corlib
, 5);
5131 sig
->params
[0] = &mono_defaults
.object_class
->byval_arg
;
5132 sig
->params
[1] = &mono_defaults
.int_class
->byval_arg
;
5133 sig
->params
[2] = &mono_defaults
.int_class
->byval_arg
;
5134 sig
->params
[3] = &mono_defaults
.int_class
->byval_arg
;
5135 sig
->params
[4] = &klass
->byval_arg
;
5136 sig
->ret
= &mono_defaults
.void_class
->byval_arg
;
5138 mono_mb_emit_ldarg (mb
, 0);
5139 pos
= mono_mb_emit_proxy_check (mb
, CEE_BNE_UN
);
5141 mono_mb_emit_ldarg (mb
, 0);
5142 mono_mb_emit_ldarg (mb
, 1);
5143 mono_mb_emit_ldarg (mb
, 2);
5144 mono_mb_emit_ldarg (mb
, 4);
5145 if (klass
->valuetype
)
5146 mono_mb_emit_op (mb
, CEE_BOX
, klass
);
5148 mono_mb_emit_managed_call (mb
, mono_marshal_get_stfld_remote_wrapper (klass
), NULL
);
5150 mono_mb_emit_byte (mb
, CEE_RET
);
5152 mono_mb_patch_branch (mb
, pos
);
5154 mono_mb_emit_ldarg (mb
, 0);
5155 mono_mb_emit_byte (mb
, MONO_CUSTOM_PREFIX
);
5156 mono_mb_emit_byte (mb
, CEE_MONO_OBJADDR
);
5157 mono_mb_emit_ldarg (mb
, 3);
5158 mono_mb_emit_byte (mb
, CEE_ADD
);
5159 mono_mb_emit_ldarg (mb
, 4);
5164 case MONO_TYPE_BOOLEAN
:
5165 case MONO_TYPE_CHAR
:
5174 case MONO_TYPE_ARRAY
:
5175 case MONO_TYPE_SZARRAY
:
5176 case MONO_TYPE_OBJECT
:
5177 case MONO_TYPE_CLASS
:
5178 case MONO_TYPE_STRING
:
5182 case MONO_TYPE_FNPTR
:
5183 mono_mb_emit_byte (mb
, mono_type_to_stind (type
));
5185 case MONO_TYPE_VALUETYPE
:
5186 g_assert (!klass
->enumtype
);
5187 mono_mb_emit_op (mb
, CEE_STOBJ
, klass
);
5189 case MONO_TYPE_GENERICINST
:
5190 mono_mb_emit_op (mb
, CEE_STOBJ
, klass
);
5193 g_warning ("type %x not implemented", type
->type
);
5194 g_assert_not_reached ();
5197 mono_mb_emit_byte (mb
, CEE_RET
);
5199 res
= mono_mb_create_and_cache (cache
, klass
,
5200 mb
, sig
, sig
->param_count
+ 16);
5207 * generates IL code for the icall wrapper (the generated method
5208 * calls the unmanaged code in func)
5211 mono_marshal_get_icall_wrapper (MonoMethodSignature
*sig
, const char *name
, gconstpointer func
, gboolean check_exceptions
)
5213 MonoMethodSignature
*csig
, *csig2
;
5214 MonoMethodBuilder
*mb
;
5218 g_assert (sig
->pinvoke
);
5220 mb
= mono_mb_new (mono_defaults
.object_class
, name
, MONO_WRAPPER_MANAGED_TO_NATIVE
);
5222 mb
->method
->save_lmf
= 1;
5224 /* Add an explicit this argument */
5226 csig2
= signature_dup_add_this (sig
, mono_defaults
.object_class
);
5228 csig2
= signature_dup (mono_defaults
.corlib
, sig
);
5231 mono_mb_emit_byte (mb
, CEE_LDARG_0
);
5233 for (i
= 0; i
< sig
->param_count
; i
++)
5234 mono_mb_emit_ldarg (mb
, i
+ sig
->hasthis
);
5236 mono_mb_emit_native_call (mb
, csig2
, (gpointer
) func
);
5237 if (check_exceptions
)
5238 emit_thread_interrupt_checkpoint (mb
);
5239 mono_mb_emit_byte (mb
, CEE_RET
);
5241 csig
= signature_dup (mono_defaults
.corlib
, sig
);
5243 if (csig
->call_convention
== MONO_CALL_VARARG
)
5244 csig
->call_convention
= 0;
5246 res
= mono_mb_create_method (mb
, csig
, csig
->param_count
+ 16);
5253 emit_marshal_custom (EmitMarshalContext
*m
, int argnum
, MonoType
*t
,
5254 MonoMarshalSpec
*spec
,
5255 int conv_arg
, MonoType
**conv_arg_type
,
5256 MarshalAction action
)
5260 static MonoClass
*ICustomMarshaler
= NULL
;
5261 static MonoMethod
*cleanup_native
, *cleanup_managed
;
5262 static MonoMethod
*marshal_managed_to_native
, *marshal_native_to_managed
;
5263 MonoMethod
*get_instance
;
5264 MonoMethodBuilder
*mb
= m
->mb
;
5265 char *exception_msg
= NULL
;
5269 if (!ICustomMarshaler
) {
5270 ICustomMarshaler
= mono_class_from_name (mono_defaults
.corlib
, "System.Runtime.InteropServices", "ICustomMarshaler");
5271 g_assert (ICustomMarshaler
);
5273 cleanup_native
= mono_class_get_method_from_name (ICustomMarshaler
, "CleanUpNativeData", 1);
5274 g_assert (cleanup_native
);
5275 cleanup_managed
= mono_class_get_method_from_name (ICustomMarshaler
, "CleanUpManagedData", 1);
5276 g_assert (cleanup_managed
);
5277 marshal_managed_to_native
= mono_class_get_method_from_name (ICustomMarshaler
, "MarshalManagedToNative", 1);
5278 g_assert (marshal_managed_to_native
);
5279 marshal_native_to_managed
= mono_class_get_method_from_name (ICustomMarshaler
, "MarshalNativeToManaged", 1);
5280 g_assert (marshal_native_to_managed
);
5283 mtype
= mono_reflection_type_from_name (spec
->data
.custom_data
.custom_name
, m
->image
);
5284 g_assert (mtype
!= NULL
);
5285 mklass
= mono_class_from_mono_type (mtype
);
5286 g_assert (mklass
!= NULL
);
5288 if (!mono_class_is_assignable_from (ICustomMarshaler
, mklass
))
5289 exception_msg
= g_strdup_printf ("Custom marshaler '%s' does not implement the ICustomMarshaler interface.", mklass
->name
);
5291 get_instance
= mono_class_get_method_from_name_flags (mklass
, "GetInstance", 1, METHOD_ATTRIBUTE_STATIC
);
5293 MonoMethodSignature
*get_sig
= mono_method_signature (get_instance
);
5294 if ((get_sig
->ret
->type
!= MONO_TYPE_CLASS
) ||
5295 (mono_class_from_mono_type (get_sig
->ret
) != ICustomMarshaler
) ||
5296 (get_sig
->params
[0]->type
!= MONO_TYPE_STRING
))
5297 get_instance
= NULL
;
5301 exception_msg
= g_strdup_printf ("Custom marshaler '%s' does not implement a static GetInstance method that takes a single string parameter and returns an ICustomMarshaler.", mklass
->name
);
5303 /* Throw exception and emit compensation code if neccesary */
5304 if (exception_msg
) {
5306 case MARSHAL_ACTION_CONV_IN
:
5307 case MARSHAL_ACTION_CONV_RESULT
:
5308 case MARSHAL_ACTION_MANAGED_CONV_RESULT
:
5309 if ((action
== MARSHAL_ACTION_CONV_RESULT
) || (action
== MARSHAL_ACTION_MANAGED_CONV_RESULT
))
5310 mono_mb_emit_byte (mb
, CEE_POP
);
5312 mono_mb_emit_exception_full (mb
, "System", "ApplicationException", exception_msg
);
5313 g_free (exception_msg
);
5316 case MARSHAL_ACTION_PUSH
:
5317 mono_mb_emit_byte (mb
, CEE_LDNULL
);
5325 /* FIXME: MS.NET seems to create one instance for each klass + cookie pair */
5326 /* FIXME: MS.NET throws an exception if GetInstance returns null */
5329 case MARSHAL_ACTION_CONV_IN
:
5331 case MONO_TYPE_CLASS
:
5332 case MONO_TYPE_OBJECT
:
5333 case MONO_TYPE_STRING
:
5334 case MONO_TYPE_ARRAY
:
5335 case MONO_TYPE_SZARRAY
:
5336 case MONO_TYPE_VALUETYPE
:
5340 g_warning ("custom marshalling of type %x is currently not supported", t
->type
);
5341 g_assert_not_reached ();
5345 conv_arg
= mono_mb_add_local (mb
, &mono_defaults
.int_class
->byval_arg
);
5347 mono_mb_emit_byte (mb
, CEE_LDNULL
);
5348 mono_mb_emit_stloc (mb
, conv_arg
);
5350 if (t
->byref
&& (t
->attrs
& PARAM_ATTRIBUTE_OUT
))
5353 /* Minic MS.NET behavior */
5354 if (!t
->byref
&& (t
->attrs
& PARAM_ATTRIBUTE_OUT
) && !(t
->attrs
& PARAM_ATTRIBUTE_IN
))
5357 /* Check for null */
5358 mono_mb_emit_ldarg (mb
, argnum
);
5360 mono_mb_emit_byte (mb
, CEE_LDIND_I
);
5361 pos2
= mono_mb_emit_branch (mb
, CEE_BRFALSE
);
5363 mono_mb_emit_ldstr (mb
, g_strdup (spec
->data
.custom_data
.cookie
));
5365 mono_mb_emit_op (mb
, CEE_CALL
, get_instance
);
5367 mono_mb_emit_ldarg (mb
, argnum
);
5369 mono_mb_emit_byte (mb
, CEE_LDIND_REF
);
5371 if (t
->type
== MONO_TYPE_VALUETYPE
) {
5373 * Since we can't determine the type of the argument, we
5374 * will assume the unmanaged function takes a pointer.
5376 *conv_arg_type
= &mono_defaults
.int_class
->byval_arg
;
5378 mono_mb_emit_op (mb
, CEE_BOX
, mono_class_from_mono_type (t
));
5381 mono_mb_emit_op (mb
, CEE_CALLVIRT
, marshal_managed_to_native
);
5382 mono_mb_emit_stloc (mb
, conv_arg
);
5384 mono_mb_patch_branch (mb
, pos2
);
5387 case MARSHAL_ACTION_CONV_OUT
:
5388 /* Check for null */
5389 mono_mb_emit_ldloc (mb
, conv_arg
);
5390 pos2
= mono_mb_emit_branch (mb
, CEE_BRFALSE
);
5393 mono_mb_emit_ldarg (mb
, argnum
);
5395 mono_mb_emit_ldstr (mb
, g_strdup (spec
->data
.custom_data
.cookie
));
5397 mono_mb_emit_op (mb
, CEE_CALL
, get_instance
);
5399 mono_mb_emit_ldloc (mb
, conv_arg
);
5400 mono_mb_emit_op (mb
, CEE_CALLVIRT
, marshal_native_to_managed
);
5401 mono_mb_emit_byte (mb
, CEE_STIND_REF
);
5402 } else if (t
->attrs
& PARAM_ATTRIBUTE_OUT
) {
5403 mono_mb_emit_ldstr (mb
, g_strdup (spec
->data
.custom_data
.cookie
));
5405 mono_mb_emit_op (mb
, CEE_CALL
, get_instance
);
5407 mono_mb_emit_ldloc (mb
, conv_arg
);
5408 mono_mb_emit_op (mb
, CEE_CALLVIRT
, marshal_native_to_managed
);
5410 /* We have nowhere to store the result */
5411 mono_mb_emit_byte (mb
, CEE_POP
);
5414 mono_mb_emit_ldstr (mb
, g_strdup (spec
->data
.custom_data
.cookie
));
5416 mono_mb_emit_op (mb
, CEE_CALL
, get_instance
);
5418 mono_mb_emit_ldloc (mb
, conv_arg
);
5420 mono_mb_emit_op (mb
, CEE_CALLVIRT
, cleanup_native
);
5422 mono_mb_patch_branch (mb
, pos2
);
5425 case MARSHAL_ACTION_PUSH
:
5427 mono_mb_emit_ldloc_addr (mb
, conv_arg
);
5429 mono_mb_emit_ldloc (mb
, conv_arg
);
5432 case MARSHAL_ACTION_CONV_RESULT
:
5433 loc1
= mono_mb_add_local (mb
, &mono_defaults
.int_class
->byval_arg
);
5435 mono_mb_emit_stloc (mb
, 3);
5437 mono_mb_emit_ldloc (mb
, 3);
5438 mono_mb_emit_stloc (mb
, loc1
);
5440 /* Check for null */
5441 mono_mb_emit_ldloc (mb
, 3);
5442 pos2
= mono_mb_emit_branch (mb
, CEE_BRFALSE
);
5444 mono_mb_emit_ldstr (mb
, g_strdup (spec
->data
.custom_data
.cookie
));
5446 mono_mb_emit_op (mb
, CEE_CALL
, get_instance
);
5447 mono_mb_emit_byte (mb
, CEE_DUP
);
5449 mono_mb_emit_ldloc (mb
, 3);
5450 mono_mb_emit_op (mb
, CEE_CALLVIRT
, marshal_native_to_managed
);
5451 mono_mb_emit_stloc (mb
, 3);
5453 mono_mb_emit_ldloc (mb
, loc1
);
5454 mono_mb_emit_op (mb
, CEE_CALLVIRT
, cleanup_native
);
5456 mono_mb_patch_branch (mb
, pos2
);
5459 case MARSHAL_ACTION_MANAGED_CONV_IN
:
5460 conv_arg
= mono_mb_add_local (mb
, &mono_defaults
.object_class
->byval_arg
);
5462 mono_mb_emit_byte (mb
, CEE_LDNULL
);
5463 mono_mb_emit_stloc (mb
, conv_arg
);
5465 if (t
->byref
&& t
->attrs
& PARAM_ATTRIBUTE_OUT
)
5468 /* Check for null */
5469 mono_mb_emit_ldarg (mb
, argnum
);
5471 mono_mb_emit_byte (mb
, CEE_LDIND_I
);
5472 pos2
= mono_mb_emit_branch (mb
, CEE_BRFALSE
);
5474 mono_mb_emit_ldstr (mb
, g_strdup (spec
->data
.custom_data
.cookie
));
5475 mono_mb_emit_op (mb
, CEE_CALL
, get_instance
);
5477 mono_mb_emit_ldarg (mb
, argnum
);
5479 mono_mb_emit_byte (mb
, CEE_LDIND_I
);
5481 mono_mb_emit_op (mb
, CEE_CALLVIRT
, marshal_native_to_managed
);
5482 mono_mb_emit_stloc (mb
, conv_arg
);
5484 mono_mb_patch_branch (mb
, pos2
);
5487 case MARSHAL_ACTION_MANAGED_CONV_RESULT
:
5488 g_assert (!t
->byref
);
5490 loc1
= mono_mb_add_local (mb
, &mono_defaults
.object_class
->byval_arg
);
5492 mono_mb_emit_stloc (mb
, 3);
5494 mono_mb_emit_ldloc (mb
, 3);
5495 mono_mb_emit_stloc (mb
, loc1
);
5497 /* Check for null */
5498 mono_mb_emit_ldloc (mb
, 3);
5499 pos2
= mono_mb_emit_branch (mb
, CEE_BRFALSE
);
5501 mono_mb_emit_ldstr (mb
, g_strdup (spec
->data
.custom_data
.cookie
));
5502 mono_mb_emit_op (mb
, CEE_CALL
, get_instance
);
5503 mono_mb_emit_byte (mb
, CEE_DUP
);
5505 mono_mb_emit_ldloc (mb
, 3);
5506 mono_mb_emit_op (mb
, CEE_CALLVIRT
, marshal_managed_to_native
);
5507 mono_mb_emit_stloc (mb
, 3);
5509 mono_mb_emit_ldloc (mb
, loc1
);
5510 mono_mb_emit_op (mb
, CEE_CALLVIRT
, cleanup_managed
);
5512 mono_mb_patch_branch (mb
, pos2
);
5515 case MARSHAL_ACTION_MANAGED_CONV_OUT
:
5517 /* Check for null */
5518 mono_mb_emit_ldloc (mb
, conv_arg
);
5519 pos2
= mono_mb_emit_branch (mb
, CEE_BRFALSE
);
5522 mono_mb_emit_ldarg (mb
, argnum
);
5524 mono_mb_emit_ldstr (mb
, g_strdup (spec
->data
.custom_data
.cookie
));
5526 mono_mb_emit_op (mb
, CEE_CALL
, get_instance
);
5528 mono_mb_emit_ldloc (mb
, conv_arg
);
5529 mono_mb_emit_op (mb
, CEE_CALLVIRT
, marshal_managed_to_native
);
5530 mono_mb_emit_byte (mb
, CEE_STIND_I
);
5533 /* Call CleanUpManagedData */
5534 mono_mb_emit_ldstr (mb
, g_strdup (spec
->data
.custom_data
.cookie
));
5536 mono_mb_emit_op (mb
, CEE_CALL
, get_instance
);
5538 mono_mb_emit_ldloc (mb
, conv_arg
);
5539 mono_mb_emit_op (mb
, CEE_CALLVIRT
, cleanup_managed
);
5541 mono_mb_patch_branch (mb
, pos2
);
5545 g_assert_not_reached ();
5552 emit_marshal_asany (EmitMarshalContext
*m
, int argnum
, MonoType
*t
,
5553 MonoMarshalSpec
*spec
,
5554 int conv_arg
, MonoType
**conv_arg_type
,
5555 MarshalAction action
)
5557 MonoMethodBuilder
*mb
= m
->mb
;
5560 case MARSHAL_ACTION_CONV_IN
: {
5561 MonoMarshalNative encoding
= mono_marshal_get_string_encoding (m
->piinfo
, NULL
);
5563 g_assert (t
->type
== MONO_TYPE_OBJECT
);
5564 g_assert (!t
->byref
);
5566 conv_arg
= mono_mb_add_local (mb
, &mono_defaults
.int_class
->byval_arg
);
5567 mono_mb_emit_ldarg (mb
, argnum
);
5568 mono_mb_emit_icon (mb
, encoding
);
5569 mono_mb_emit_icon (mb
, t
->attrs
);
5570 mono_mb_emit_icall (mb
, mono_marshal_asany
);
5571 mono_mb_emit_stloc (mb
, conv_arg
);
5575 case MARSHAL_ACTION_PUSH
:
5576 mono_mb_emit_ldloc (mb
, conv_arg
);
5579 case MARSHAL_ACTION_CONV_OUT
: {
5580 MonoMarshalNative encoding
= mono_marshal_get_string_encoding (m
->piinfo
, NULL
);
5582 mono_mb_emit_ldarg (mb
, argnum
);
5583 mono_mb_emit_ldloc (mb
, conv_arg
);
5584 mono_mb_emit_icon (mb
, encoding
);
5585 mono_mb_emit_icon (mb
, t
->attrs
);
5586 mono_mb_emit_icall (mb
, mono_marshal_free_asany
);
5591 g_assert_not_reached ();
5598 emit_marshal_vtype (EmitMarshalContext
*m
, int argnum
, MonoType
*t
,
5599 MonoMarshalSpec
*spec
,
5600 int conv_arg
, MonoType
**conv_arg_type
,
5601 MarshalAction action
)
5603 MonoMethodBuilder
*mb
= m
->mb
;
5604 MonoClass
*klass
, *date_time_class
;
5607 klass
= mono_class_from_mono_type (t
);
5609 date_time_class
= mono_class_from_name_cached (mono_defaults
.corlib
, "System", "DateTime");
5612 case MARSHAL_ACTION_CONV_IN
:
5613 if (klass
== date_time_class
) {
5614 /* Convert it to an OLE DATE type */
5615 static MonoMethod
*to_oadate
;
5618 to_oadate
= mono_class_get_method_from_name (date_time_class
, "ToOADate", 0);
5619 g_assert (to_oadate
);
5621 conv_arg
= mono_mb_add_local (mb
, &mono_defaults
.double_class
->byval_arg
);
5624 mono_mb_emit_ldarg (mb
, argnum
);
5625 pos
= mono_mb_emit_branch (mb
, CEE_BRFALSE
);
5628 if (!(t
->byref
&& !(t
->attrs
& PARAM_ATTRIBUTE_IN
) && (t
->attrs
& PARAM_ATTRIBUTE_OUT
))) {
5630 m
->csig
->params
[argnum
- m
->csig
->hasthis
] = &mono_defaults
.double_class
->byval_arg
;
5632 mono_mb_emit_ldarg_addr (mb
, argnum
);
5633 mono_mb_emit_managed_call (mb
, to_oadate
, NULL
);
5634 mono_mb_emit_stloc (mb
, conv_arg
);
5638 mono_mb_patch_branch (mb
, pos
);
5642 if (((klass
->flags
& TYPE_ATTRIBUTE_LAYOUT_MASK
) == TYPE_ATTRIBUTE_EXPLICIT_LAYOUT
) ||
5643 klass
->blittable
|| klass
->enumtype
)
5646 conv_arg
= mono_mb_add_local (mb
, &mono_defaults
.int_class
->byval_arg
);
5648 /* store the address of the source into local variable 0 */
5650 mono_mb_emit_ldarg (mb
, argnum
);
5652 mono_mb_emit_ldarg_addr (mb
, argnum
);
5654 mono_mb_emit_stloc (mb
, 0);
5656 /* allocate space for the native struct and
5657 * store the address into local variable 1 (dest) */
5658 mono_mb_emit_icon (mb
, mono_class_native_size (klass
, NULL
));
5659 mono_mb_emit_byte (mb
, CEE_PREFIX1
);
5660 mono_mb_emit_byte (mb
, CEE_LOCALLOC
);
5661 mono_mb_emit_stloc (mb
, conv_arg
);
5664 mono_mb_emit_ldloc (mb
, 0);
5665 pos
= mono_mb_emit_branch (mb
, CEE_BRFALSE
);
5668 if (!(t
->byref
&& !(t
->attrs
& PARAM_ATTRIBUTE_IN
) && (t
->attrs
& PARAM_ATTRIBUTE_OUT
))) {
5670 mono_mb_emit_ldloc (mb
, conv_arg
);
5671 mono_mb_emit_stloc (mb
, 1);
5673 /* emit valuetype conversion code */
5674 emit_struct_conv (mb
, klass
, FALSE
);
5678 mono_mb_patch_branch (mb
, pos
);
5681 case MARSHAL_ACTION_PUSH
:
5682 if (spec
&& spec
->native
== MONO_NATIVE_LPSTRUCT
) {
5684 g_assert (!t
->byref
);
5686 /* Have to change the signature since the vtype is passed byref */
5687 m
->csig
->params
[argnum
- m
->csig
->hasthis
] = &mono_defaults
.int_class
->byval_arg
;
5689 if (((klass
->flags
& TYPE_ATTRIBUTE_LAYOUT_MASK
) == TYPE_ATTRIBUTE_EXPLICIT_LAYOUT
) ||
5690 klass
->blittable
|| klass
->enumtype
)
5691 mono_mb_emit_ldarg_addr (mb
, argnum
);
5693 mono_mb_emit_ldloc (mb
, conv_arg
);
5697 if (klass
== date_time_class
) {
5699 mono_mb_emit_ldloc_addr (mb
, conv_arg
);
5701 mono_mb_emit_ldloc (mb
, conv_arg
);
5705 if (((klass
->flags
& TYPE_ATTRIBUTE_LAYOUT_MASK
) == TYPE_ATTRIBUTE_EXPLICIT_LAYOUT
) ||
5706 klass
->blittable
|| klass
->enumtype
) {
5707 mono_mb_emit_ldarg (mb
, argnum
);
5710 mono_mb_emit_ldloc (mb
, conv_arg
);
5712 mono_mb_emit_byte (mb
, MONO_CUSTOM_PREFIX
);
5713 mono_mb_emit_op (mb
, CEE_MONO_LDNATIVEOBJ
, klass
);
5717 case MARSHAL_ACTION_CONV_OUT
:
5718 if (klass
== date_time_class
) {
5719 /* Convert from an OLE DATE type */
5720 static MonoMethod
*from_oadate
;
5725 if (!((t
->attrs
& PARAM_ATTRIBUTE_IN
) && !(t
->attrs
& PARAM_ATTRIBUTE_OUT
))) {
5727 from_oadate
= mono_class_get_method_from_name (date_time_class
, "FromOADate", 1);
5728 g_assert (from_oadate
);
5730 mono_mb_emit_ldarg (mb
, argnum
);
5731 mono_mb_emit_ldloc (mb
, conv_arg
);
5732 mono_mb_emit_managed_call (mb
, from_oadate
, NULL
);
5733 mono_mb_emit_op (mb
, CEE_STOBJ
, date_time_class
);
5738 if (((klass
->flags
& TYPE_ATTRIBUTE_LAYOUT_MASK
) == TYPE_ATTRIBUTE_EXPLICIT_LAYOUT
) ||
5739 klass
->blittable
|| klass
->enumtype
)
5743 /* dst = argument */
5744 mono_mb_emit_ldarg (mb
, argnum
);
5745 mono_mb_emit_stloc (mb
, 1);
5747 mono_mb_emit_ldloc (mb
, 1);
5748 pos
= mono_mb_emit_branch (mb
, CEE_BRFALSE
);
5750 if (!((t
->attrs
& PARAM_ATTRIBUTE_IN
) && !(t
->attrs
& PARAM_ATTRIBUTE_OUT
))) {
5751 /* src = tmp_locals [i] */
5752 mono_mb_emit_ldloc (mb
, conv_arg
);
5753 mono_mb_emit_stloc (mb
, 0);
5755 /* emit valuetype conversion code */
5756 emit_struct_conv (mb
, klass
, TRUE
);
5760 emit_struct_free (mb
, klass
, conv_arg
);
5763 mono_mb_patch_branch (mb
, pos
);
5766 case MARSHAL_ACTION_CONV_RESULT
:
5767 if (((klass
->flags
& TYPE_ATTRIBUTE_LAYOUT_MASK
) == TYPE_ATTRIBUTE_EXPLICIT_LAYOUT
) ||
5769 mono_mb_emit_stloc (mb
, 3);
5773 /* load pointer to returned value type */
5774 mono_mb_emit_byte (mb
, MONO_CUSTOM_PREFIX
);
5775 mono_mb_emit_byte (mb
, CEE_MONO_VTADDR
);
5776 /* store the address of the source into local variable 0 */
5777 mono_mb_emit_stloc (mb
, 0);
5779 mono_mb_emit_ldloc_addr (mb
, 3);
5780 mono_mb_emit_stloc (mb
, 1);
5782 /* emit valuetype conversion code */
5783 emit_struct_conv (mb
, klass
, TRUE
);
5786 case MARSHAL_ACTION_MANAGED_CONV_IN
:
5787 if (((klass
->flags
& TYPE_ATTRIBUTE_LAYOUT_MASK
) == TYPE_ATTRIBUTE_EXPLICIT_LAYOUT
) ||
5788 klass
->blittable
|| klass
->enumtype
) {
5793 conv_arg
= mono_mb_add_local (mb
, &klass
->byval_arg
);
5795 if (t
->attrs
& PARAM_ATTRIBUTE_OUT
)
5799 mono_mb_emit_ldarg (mb
, argnum
);
5801 mono_mb_emit_ldarg_addr (mb
, argnum
);
5802 mono_mb_emit_stloc (mb
, 0);
5805 mono_mb_emit_ldloc (mb
, 0);
5806 pos
= mono_mb_emit_branch (mb
, CEE_BRFALSE
);
5809 mono_mb_emit_ldloc_addr (mb
, conv_arg
);
5810 mono_mb_emit_stloc (mb
, 1);
5812 /* emit valuetype conversion code */
5813 emit_struct_conv (mb
, klass
, TRUE
);
5816 mono_mb_patch_branch (mb
, pos
);
5819 case MARSHAL_ACTION_MANAGED_CONV_OUT
:
5820 if (((klass
->flags
& TYPE_ATTRIBUTE_LAYOUT_MASK
) == TYPE_ATTRIBUTE_EXPLICIT_LAYOUT
) ||
5821 klass
->blittable
|| klass
->enumtype
) {
5825 /* Check for null */
5826 mono_mb_emit_ldarg (mb
, argnum
);
5827 pos2
= mono_mb_emit_branch (mb
, CEE_BRFALSE
);
5830 mono_mb_emit_ldloc_addr (mb
, conv_arg
);
5831 mono_mb_emit_stloc (mb
, 0);
5834 mono_mb_emit_ldarg (mb
, argnum
);
5835 mono_mb_emit_stloc (mb
, 1);
5837 /* emit valuetype conversion code */
5838 emit_struct_conv (mb
, klass
, FALSE
);
5840 mono_mb_patch_branch (mb
, pos2
);
5843 case MARSHAL_ACTION_MANAGED_CONV_RESULT
:
5844 if (((klass
->flags
& TYPE_ATTRIBUTE_LAYOUT_MASK
) == TYPE_ATTRIBUTE_EXPLICIT_LAYOUT
) ||
5845 klass
->blittable
|| klass
->enumtype
) {
5846 mono_mb_emit_stloc (mb
, 3);
5851 /* load pointer to returned value type */
5852 mono_mb_emit_byte (mb
, MONO_CUSTOM_PREFIX
);
5853 mono_mb_emit_byte (mb
, CEE_MONO_VTADDR
);
5855 /* store the address of the source into local variable 0 */
5856 mono_mb_emit_stloc (mb
, 0);
5857 /* allocate space for the native struct and
5858 * store the address into dst_ptr */
5859 m
->retobj_var
= mono_mb_add_local (mb
, &mono_defaults
.int_class
->byval_arg
);
5860 m
->retobj_class
= klass
;
5861 g_assert (m
->retobj_var
);
5862 mono_mb_emit_icon (mb
, mono_class_native_size (klass
, NULL
));
5863 mono_mb_emit_byte (mb
, CEE_CONV_I
);
5864 mono_mb_emit_icall (mb
, mono_marshal_alloc
);
5865 mono_mb_emit_stloc (mb
, 1);
5866 mono_mb_emit_ldloc (mb
, 1);
5867 mono_mb_emit_stloc (mb
, m
->retobj_var
);
5869 /* emit valuetype conversion code */
5870 emit_struct_conv (mb
, klass
, FALSE
);
5874 g_assert_not_reached ();
5881 emit_marshal_string (EmitMarshalContext
*m
, int argnum
, MonoType
*t
,
5882 MonoMarshalSpec
*spec
,
5883 int conv_arg
, MonoType
**conv_arg_type
,
5884 MarshalAction action
)
5886 MonoMethodBuilder
*mb
= m
->mb
;
5887 MonoMarshalNative encoding
= mono_marshal_get_string_encoding (m
->piinfo
, spec
);
5888 MonoMarshalConv conv
= mono_marshal_get_string_to_ptr_conv (m
->piinfo
, spec
);
5892 case MARSHAL_ACTION_CONV_IN
:
5893 *conv_arg_type
= &mono_defaults
.int_class
->byval_arg
;
5894 conv_arg
= mono_mb_add_local (mb
, &mono_defaults
.int_class
->byval_arg
);
5897 if (t
->attrs
& PARAM_ATTRIBUTE_OUT
)
5900 mono_mb_emit_ldarg (mb
, argnum
);
5901 mono_mb_emit_byte (mb
, CEE_LDIND_I
);
5903 mono_mb_emit_ldarg (mb
, argnum
);
5907 char *msg
= g_strdup_printf ("string marshalling conversion %d not implemented", encoding
);
5908 MonoException
*exc
= mono_get_exception_not_implemented (msg
);
5909 g_warning ("%s", msg
);
5911 mono_raise_exception (exc
);
5914 mono_mb_emit_icall (mb
, conv_to_icall (conv
));
5916 mono_mb_emit_stloc (mb
, conv_arg
);
5919 case MARSHAL_ACTION_CONV_OUT
:
5920 conv
= mono_marshal_get_ptr_to_string_conv (m
->piinfo
, spec
, &need_free
);
5922 char *msg
= g_strdup_printf ("string marshalling conversion %d not implemented", encoding
);
5923 mono_mb_emit_exception_marshal_directive (mb
, msg
);
5927 if (t
->byref
&& (t
->attrs
& PARAM_ATTRIBUTE_OUT
)) {
5928 mono_mb_emit_ldarg (mb
, argnum
);
5929 mono_mb_emit_ldloc (mb
, conv_arg
);
5930 mono_mb_emit_icall (mb
, conv_to_icall (conv
));
5931 mono_mb_emit_byte (mb
, CEE_STIND_REF
);
5934 if (need_free
|| (t
->byref
&& (t
->attrs
& PARAM_ATTRIBUTE_OUT
))) {
5935 mono_mb_emit_ldloc (mb
, conv_arg
);
5936 if (conv
== MONO_MARSHAL_CONV_BSTR_STR
)
5937 mono_mb_emit_icall (mb
, mono_free_bstr
);
5939 mono_mb_emit_icall (mb
, mono_marshal_free
);
5943 case MARSHAL_ACTION_PUSH
:
5945 mono_mb_emit_ldloc_addr (mb
, conv_arg
);
5947 mono_mb_emit_ldloc (mb
, conv_arg
);
5950 case MARSHAL_ACTION_CONV_RESULT
:
5951 mono_mb_emit_stloc (mb
, 0);
5953 conv
= mono_marshal_get_ptr_to_string_conv (m
->piinfo
, spec
, &need_free
);
5955 char *msg
= g_strdup_printf ("string marshalling conversion %d not implemented", encoding
);
5956 mono_mb_emit_exception_marshal_directive (mb
, msg
);
5960 mono_mb_emit_ldloc (mb
, 0);
5961 mono_mb_emit_icall (mb
, conv_to_icall (conv
));
5962 mono_mb_emit_stloc (mb
, 3);
5964 /* free the string */
5965 mono_mb_emit_ldloc (mb
, 0);
5966 if (conv
== MONO_MARSHAL_CONV_BSTR_STR
)
5967 mono_mb_emit_icall (mb
, mono_free_bstr
);
5969 mono_mb_emit_icall (mb
, mono_marshal_free
);
5972 case MARSHAL_ACTION_MANAGED_CONV_IN
:
5973 conv_arg
= mono_mb_add_local (mb
, &mono_defaults
.object_class
->byval_arg
);
5975 *conv_arg_type
= &mono_defaults
.int_class
->byval_arg
;
5977 conv
= mono_marshal_get_ptr_to_string_conv (m
->piinfo
, spec
, &need_free
);
5979 char *msg
= g_strdup_printf ("string marshalling conversion %d not implemented", encoding
);
5980 mono_mb_emit_exception_marshal_directive (mb
, msg
);
5984 mono_mb_emit_ldarg (mb
, argnum
);
5986 mono_mb_emit_byte (mb
, CEE_LDIND_I
);
5987 mono_mb_emit_icall (mb
, conv_to_icall (conv
));
5988 mono_mb_emit_stloc (mb
, conv_arg
);
5991 case MARSHAL_ACTION_MANAGED_CONV_OUT
:
5994 mono_mb_emit_ldarg (mb
, argnum
);
5995 mono_mb_emit_ldloc (mb
, conv_arg
);
5996 mono_mb_emit_icall (mb
, conv_to_icall (conv
));
5997 mono_mb_emit_byte (mb
, CEE_STIND_I
);
6002 case MARSHAL_ACTION_MANAGED_CONV_RESULT
:
6003 if (conv_to_icall (conv
) == mono_marshal_string_to_utf16
)
6004 /* We need to make a copy so the caller is able to free it */
6005 mono_mb_emit_icall (mb
, mono_marshal_string_to_utf16_copy
);
6007 mono_mb_emit_icall (mb
, conv_to_icall (conv
));
6008 mono_mb_emit_stloc (mb
, 3);
6012 g_assert_not_reached ();
6019 emit_marshal_safehandle (EmitMarshalContext
*m
, int argnum
, MonoType
*t
,
6020 MonoMarshalSpec
*spec
, int conv_arg
,
6021 MonoType
**conv_arg_type
, MarshalAction action
)
6023 MonoMethodBuilder
*mb
= m
->mb
;
6026 case MARSHAL_ACTION_CONV_IN
: {
6027 MonoType
*intptr_type
;
6028 int dar_release_slot
, pos
;
6030 intptr_type
= &mono_defaults
.int_class
->byval_arg
;
6031 conv_arg
= mono_mb_add_local (mb
, intptr_type
);
6032 *conv_arg_type
= intptr_type
;
6034 if (!sh_dangerous_add_ref
)
6035 init_safe_handle ();
6037 mono_mb_emit_ldarg (mb
, argnum
);
6038 pos
= mono_mb_emit_branch (mb
, CEE_BRTRUE
);
6039 mono_mb_emit_exception (mb
, "ArgumentNullException", NULL
);
6041 mono_mb_patch_branch (mb
, pos
);
6044 * My tests in show that ref SafeHandles are not really
6045 * passed as ref objects. Instead a NULL is passed as the
6048 mono_mb_emit_icon (mb
, 0);
6049 mono_mb_emit_stloc (mb
, conv_arg
);
6053 /* Create local to hold the ref parameter to DangerousAddRef */
6054 dar_release_slot
= mono_mb_add_local (mb
, &mono_defaults
.boolean_class
->byval_arg
);
6056 /* set release = false; */
6057 mono_mb_emit_icon (mb
, 0);
6058 mono_mb_emit_stloc (mb
, dar_release_slot
);
6060 /* safehandle.DangerousAddRef (ref release) */
6061 mono_mb_emit_ldarg (mb
, argnum
);
6062 mono_mb_emit_ldloc_addr (mb
, dar_release_slot
);
6063 mono_mb_emit_managed_call (mb
, sh_dangerous_add_ref
, NULL
);
6065 /* Pull the handle field from SafeHandle */
6066 mono_mb_emit_ldarg (mb
, argnum
);
6067 mono_mb_emit_ldflda (mb
, G_STRUCT_OFFSET (MonoSafeHandle
, handle
));
6068 mono_mb_emit_byte (mb
, CEE_LDIND_I
);
6069 mono_mb_emit_stloc (mb
, conv_arg
);
6074 case MARSHAL_ACTION_PUSH
:
6076 mono_mb_emit_ldloc_addr (mb
, conv_arg
);
6078 mono_mb_emit_ldloc (mb
, conv_arg
);
6081 case MARSHAL_ACTION_CONV_OUT
: {
6082 /* The slot for the boolean is the next temporary created after conv_arg, see the CONV_IN code */
6083 int dar_release_slot
= conv_arg
+ 1;
6086 if (!sh_dangerous_release
)
6087 init_safe_handle ();
6093 * My tests indicate that ref SafeHandles parameters are not actually
6094 * passed by ref, but instead a new Handle is created regardless of
6095 * whether a change happens in the unmanaged side.
6097 * Also, the Handle is created before calling into unmanaged code,
6098 * but we do not support that mechanism (getting to the original
6099 * handle) and it makes no difference where we create this
6101 ctor
= mono_class_get_method_from_name (t
->data
.klass
, ".ctor", 0);
6103 mono_mb_emit_exception (mb
, "MissingMethodException", "paramterless constructor required");
6106 /* refval = new SafeHandleDerived ()*/
6107 mono_mb_emit_ldarg (mb
, argnum
);
6108 mono_mb_emit_op (mb
, CEE_NEWOBJ
, ctor
);
6109 mono_mb_emit_byte (mb
, CEE_STIND_REF
);
6111 /* refval.handle = returned_handle */
6112 mono_mb_emit_ldarg (mb
, argnum
);
6113 mono_mb_emit_byte (mb
, CEE_LDIND_REF
);
6114 mono_mb_emit_ldflda (mb
, G_STRUCT_OFFSET (MonoSafeHandle
, handle
));
6115 mono_mb_emit_ldloc (mb
, conv_arg
);
6116 mono_mb_emit_byte (mb
, CEE_STIND_I
);
6118 mono_mb_emit_ldloc (mb
, dar_release_slot
);
6119 label_next
= mono_mb_emit_branch (mb
, CEE_BRFALSE
);
6120 mono_mb_emit_ldarg (mb
, argnum
);
6121 mono_mb_emit_managed_call (mb
, sh_dangerous_release
, NULL
);
6122 mono_mb_patch_branch (mb
, label_next
);
6127 case MARSHAL_ACTION_CONV_RESULT
: {
6128 MonoMethod
*ctor
= NULL
;
6129 int intptr_handle_slot
;
6131 if (t
->data
.klass
->flags
& TYPE_ATTRIBUTE_ABSTRACT
){
6132 mono_mb_emit_byte (mb
, CEE_POP
);
6133 mono_mb_emit_exception_marshal_directive (mb
, "Returned SafeHandles should not be abstract");
6137 ctor
= mono_class_get_method_from_name (t
->data
.klass
, ".ctor", 0);
6139 mono_mb_emit_byte (mb
, CEE_POP
);
6140 mono_mb_emit_exception (mb
, "MissingMethodException", "paramterless constructor required");
6143 /* Store the IntPtr results into a local */
6144 intptr_handle_slot
= mono_mb_add_local (mb
, &mono_defaults
.int_class
->byval_arg
);
6145 mono_mb_emit_stloc (mb
, intptr_handle_slot
);
6147 /* Create return value */
6148 mono_mb_emit_op (mb
, CEE_NEWOBJ
, ctor
);
6149 mono_mb_emit_stloc (mb
, 3);
6151 /* Set the return.handle to the value, am using ldflda, not sure if thats a good idea */
6152 mono_mb_emit_ldloc (mb
, 3);
6153 mono_mb_emit_ldflda (mb
, G_STRUCT_OFFSET (MonoSafeHandle
, handle
));
6154 mono_mb_emit_ldloc (mb
, intptr_handle_slot
);
6155 mono_mb_emit_byte (mb
, CEE_STIND_I
);
6159 case MARSHAL_ACTION_MANAGED_CONV_IN
:
6160 fprintf (stderr
, "mono/marshal: SafeHandles missing MANAGED_CONV_IN\n");
6163 case MARSHAL_ACTION_MANAGED_CONV_OUT
:
6164 fprintf (stderr
, "mono/marshal: SafeHandles missing MANAGED_CONV_OUT\n");
6167 case MARSHAL_ACTION_MANAGED_CONV_RESULT
:
6168 fprintf (stderr
, "mono/marshal: SafeHandles missing MANAGED_CONV_RESULT\n");
6171 printf ("Unhandled case for MarshalAction: %d\n", action
);
6178 emit_marshal_handleref (EmitMarshalContext
*m
, int argnum
, MonoType
*t
,
6179 MonoMarshalSpec
*spec
, int conv_arg
,
6180 MonoType
**conv_arg_type
, MarshalAction action
)
6182 MonoMethodBuilder
*mb
= m
->mb
;
6185 case MARSHAL_ACTION_CONV_IN
: {
6186 MonoType
*intptr_type
;
6188 intptr_type
= &mono_defaults
.int_class
->byval_arg
;
6189 conv_arg
= mono_mb_add_local (mb
, intptr_type
);
6190 *conv_arg_type
= intptr_type
;
6193 mono_mb_emit_exception_marshal_directive (mb
,
6194 "HandleRefs can not be returned from unmanaged code (or passed by ref)");
6197 mono_mb_emit_ldarg_addr (mb
, argnum
);
6198 mono_mb_emit_icon (mb
, G_STRUCT_OFFSET (MonoHandleRef
, handle
));
6199 mono_mb_emit_byte (mb
, CEE_ADD
);
6200 mono_mb_emit_byte (mb
, CEE_LDIND_I
);
6201 mono_mb_emit_stloc (mb
, conv_arg
);
6205 case MARSHAL_ACTION_PUSH
:
6206 mono_mb_emit_ldloc (mb
, conv_arg
);
6209 case MARSHAL_ACTION_CONV_OUT
: {
6210 /* no resource release required */
6214 case MARSHAL_ACTION_CONV_RESULT
: {
6215 mono_mb_emit_exception_marshal_directive (mb
,
6216 "HandleRefs can not be returned from unmanaged code (or passed by ref)");
6220 case MARSHAL_ACTION_MANAGED_CONV_IN
:
6221 fprintf (stderr
, "mono/marshal: SafeHandles missing MANAGED_CONV_IN\n");
6224 case MARSHAL_ACTION_MANAGED_CONV_OUT
:
6225 fprintf (stderr
, "mono/marshal: SafeHandles missing MANAGED_CONV_OUT\n");
6228 case MARSHAL_ACTION_MANAGED_CONV_RESULT
:
6229 fprintf (stderr
, "mono/marshal: SafeHandles missing MANAGED_CONV_RESULT\n");
6232 fprintf (stderr
, "Unhandled case for MarshalAction: %d\n", action
);
6239 emit_marshal_object (EmitMarshalContext
*m
, int argnum
, MonoType
*t
,
6240 MonoMarshalSpec
*spec
,
6241 int conv_arg
, MonoType
**conv_arg_type
,
6242 MarshalAction action
)
6244 MonoMethodBuilder
*mb
= m
->mb
;
6245 MonoClass
*klass
= mono_class_from_mono_type (t
);
6249 case MARSHAL_ACTION_CONV_IN
:
6250 *conv_arg_type
= &mono_defaults
.int_class
->byval_arg
;
6251 conv_arg
= mono_mb_add_local (mb
, &mono_defaults
.int_class
->byval_arg
);
6253 m
->orig_conv_args
[argnum
] = 0;
6255 if (mono_class_from_mono_type (t
) == mono_defaults
.object_class
) {
6256 char *msg
= g_strdup_printf ("Marshalling of type object is not implemented");
6257 mono_mb_emit_exception_marshal_directive (mb
, msg
);
6261 if (klass
->delegate
) {
6263 if (!(t
->attrs
& PARAM_ATTRIBUTE_OUT
)) {
6264 char *msg
= g_strdup_printf ("Byref marshalling of delegates is not implemented.");
6265 mono_mb_emit_exception_marshal_directive (mb
, msg
);
6267 mono_mb_emit_byte (mb
, CEE_LDNULL
);
6268 mono_mb_emit_stloc (mb
, conv_arg
);
6270 mono_mb_emit_ldarg (mb
, argnum
);
6271 mono_mb_emit_icall (mb
, conv_to_icall (MONO_MARSHAL_CONV_DEL_FTN
));
6272 mono_mb_emit_stloc (mb
, conv_arg
);
6274 } else if (klass
== mono_defaults
.stringbuilder_class
) {
6275 MonoMarshalNative encoding
= mono_marshal_get_string_encoding (m
->piinfo
, spec
);
6276 MonoMarshalConv conv
= mono_marshal_get_stringbuilder_to_ptr_conv (m
->piinfo
, spec
);
6279 if (!(t
->attrs
& PARAM_ATTRIBUTE_OUT
)) {
6280 char *msg
= g_strdup_printf ("Byref marshalling of stringbuilders is not implemented.");
6281 mono_mb_emit_exception_marshal_directive (mb
, msg
);
6286 mono_mb_emit_ldarg (mb
, argnum
);
6289 mono_mb_emit_icall (mb
, conv_to_icall (conv
));
6291 char *msg
= g_strdup_printf ("stringbuilder marshalling conversion %d not implemented", encoding
);
6292 MonoException
*exc
= mono_get_exception_not_implemented (msg
);
6293 g_warning ("%s", msg
);
6295 mono_raise_exception (exc
);
6298 mono_mb_emit_stloc (mb
, conv_arg
);
6299 } else if (klass
->blittable
) {
6300 mono_mb_emit_byte (mb
, CEE_LDNULL
);
6301 mono_mb_emit_stloc (mb
, conv_arg
);
6303 mono_mb_emit_ldarg (mb
, argnum
);
6304 pos
= mono_mb_emit_branch (mb
, CEE_BRFALSE
);
6306 mono_mb_emit_ldarg (mb
, argnum
);
6307 mono_mb_emit_ldflda (mb
, sizeof (MonoObject
));
6308 mono_mb_emit_stloc (mb
, conv_arg
);
6310 mono_mb_patch_branch (mb
, pos
);
6313 mono_mb_emit_byte (mb
, CEE_LDNULL
);
6314 mono_mb_emit_stloc (mb
, conv_arg
);
6317 /* we dont need any conversions for out parameters */
6318 if (t
->attrs
& PARAM_ATTRIBUTE_OUT
)
6321 mono_mb_emit_ldarg (mb
, argnum
);
6322 mono_mb_emit_byte (mb
, CEE_LDIND_I
);
6325 mono_mb_emit_ldarg (mb
, argnum
);
6326 mono_mb_emit_byte (mb
, MONO_CUSTOM_PREFIX
);
6327 mono_mb_emit_byte (mb
, CEE_MONO_OBJADDR
);
6330 /* store the address of the source into local variable 0 */
6331 mono_mb_emit_stloc (mb
, 0);
6332 mono_mb_emit_ldloc (mb
, 0);
6333 pos
= mono_mb_emit_branch (mb
, CEE_BRFALSE
);
6335 /* allocate space for the native struct and store the address */
6336 mono_mb_emit_icon (mb
, mono_class_native_size (klass
, NULL
));
6337 mono_mb_emit_byte (mb
, CEE_PREFIX1
);
6338 mono_mb_emit_byte (mb
, CEE_LOCALLOC
);
6339 mono_mb_emit_stloc (mb
, conv_arg
);
6342 /* Need to store the original buffer so we can free it later */
6343 m
->orig_conv_args
[argnum
] = mono_mb_add_local (mb
, &mono_defaults
.int_class
->byval_arg
);
6344 mono_mb_emit_ldloc (mb
, conv_arg
);
6345 mono_mb_emit_stloc (mb
, m
->orig_conv_args
[argnum
]);
6348 /* set the src_ptr */
6349 mono_mb_emit_ldloc (mb
, 0);
6350 mono_mb_emit_ldflda (mb
, sizeof (MonoObject
));
6351 mono_mb_emit_stloc (mb
, 0);
6354 mono_mb_emit_ldloc (mb
, conv_arg
);
6355 mono_mb_emit_stloc (mb
, 1);
6357 /* emit valuetype conversion code */
6358 emit_struct_conv (mb
, klass
, FALSE
);
6360 mono_mb_patch_branch (mb
, pos
);
6364 case MARSHAL_ACTION_CONV_OUT
:
6365 if (klass
== mono_defaults
.stringbuilder_class
) {
6367 MonoMarshalNative encoding
;
6368 MonoMarshalConv conv
;
6370 encoding
= mono_marshal_get_string_encoding (m
->piinfo
, spec
);
6371 conv
= mono_marshal_get_ptr_to_stringbuilder_conv (m
->piinfo
, spec
, &need_free
);
6373 g_assert (encoding
!= -1);
6376 g_assert ((t
->attrs
& PARAM_ATTRIBUTE_OUT
));
6380 mono_mb_emit_ldarg (mb
, argnum
);
6381 mono_mb_emit_ldloc (mb
, conv_arg
);
6384 case MONO_NATIVE_LPWSTR
:
6385 mono_mb_emit_icall (mb
, mono_string_utf16_to_builder2
);
6387 case MONO_NATIVE_LPSTR
:
6388 mono_mb_emit_icall (mb
, mono_string_utf8_to_builder2
);
6391 g_assert_not_reached ();
6394 mono_mb_emit_byte (mb
, CEE_STIND_REF
);
6396 mono_mb_emit_ldarg (mb
, argnum
);
6397 mono_mb_emit_ldloc (mb
, conv_arg
);
6399 mono_mb_emit_icall (mb
, conv_to_icall (conv
));
6403 mono_mb_emit_ldloc (mb
, conv_arg
);
6404 mono_mb_emit_icall (mb
, mono_marshal_free
);
6409 if (klass
->delegate
) {
6411 mono_mb_emit_ldarg (mb
, argnum
);
6412 mono_mb_emit_byte (mb
, MONO_CUSTOM_PREFIX
);
6413 mono_mb_emit_op (mb
, CEE_MONO_CLASSCONST
, klass
);
6414 mono_mb_emit_ldloc (mb
, conv_arg
);
6415 mono_mb_emit_icall (mb
, conv_to_icall (MONO_MARSHAL_CONV_FTN_DEL
));
6416 mono_mb_emit_byte (mb
, CEE_STIND_REF
);
6421 if (t
->byref
&& (t
->attrs
& PARAM_ATTRIBUTE_OUT
)) {
6422 /* allocate a new object */
6423 mono_mb_emit_ldarg (mb
, argnum
);
6424 mono_mb_emit_byte (mb
, MONO_CUSTOM_PREFIX
);
6425 mono_mb_emit_op (mb
, CEE_MONO_NEWOBJ
, klass
);
6426 mono_mb_emit_byte (mb
, CEE_STIND_REF
);
6429 /* dst = *argument */
6430 mono_mb_emit_ldarg (mb
, argnum
);
6433 mono_mb_emit_byte (mb
, CEE_LDIND_I
);
6435 mono_mb_emit_stloc (mb
, 1);
6437 mono_mb_emit_ldloc (mb
, 1);
6438 pos
= mono_mb_emit_branch (mb
, CEE_BRFALSE
);
6440 if (t
->byref
|| (t
->attrs
& PARAM_ATTRIBUTE_OUT
)) {
6441 mono_mb_emit_ldloc (mb
, 1);
6442 mono_mb_emit_icon (mb
, sizeof (MonoObject
));
6443 mono_mb_emit_byte (mb
, CEE_ADD
);
6444 mono_mb_emit_stloc (mb
, 1);
6446 /* src = tmp_locals [i] */
6447 mono_mb_emit_ldloc (mb
, conv_arg
);
6448 mono_mb_emit_stloc (mb
, 0);
6450 /* emit valuetype conversion code */
6451 emit_struct_conv (mb
, klass
, TRUE
);
6453 /* Free the structure returned by the native code */
6454 emit_struct_free (mb
, klass
, conv_arg
);
6456 if (m
->orig_conv_args
[argnum
]) {
6458 * If the native function changed the pointer, then free
6459 * the original structure plus the new pointer.
6461 mono_mb_emit_ldloc (mb
, m
->orig_conv_args
[argnum
]);
6462 mono_mb_emit_ldloc (mb
, conv_arg
);
6463 pos2
= mono_mb_emit_branch (mb
, CEE_BEQ
);
6465 if (!(t
->attrs
& PARAM_ATTRIBUTE_OUT
)) {
6466 g_assert (m
->orig_conv_args
[argnum
]);
6468 emit_struct_free (mb
, klass
, m
->orig_conv_args
[argnum
]);
6471 mono_mb_emit_ldloc (mb
, conv_arg
);
6472 mono_mb_emit_icall (mb
, g_free
);
6474 mono_mb_patch_branch (mb
, pos2
);
6478 /* Free the original structure passed to native code */
6479 emit_struct_free (mb
, klass
, conv_arg
);
6481 mono_mb_patch_branch (mb
, pos
);
6484 case MARSHAL_ACTION_PUSH
:
6486 mono_mb_emit_ldloc_addr (mb
, conv_arg
);
6488 mono_mb_emit_ldloc (mb
, conv_arg
);
6491 case MARSHAL_ACTION_CONV_RESULT
:
6492 if (klass
->delegate
) {
6493 g_assert (!t
->byref
);
6494 mono_mb_emit_stloc (mb
, 0);
6495 mono_mb_emit_byte (mb
, MONO_CUSTOM_PREFIX
);
6496 mono_mb_emit_op (mb
, CEE_MONO_CLASSCONST
, klass
);
6497 mono_mb_emit_ldloc (mb
, 0);
6498 mono_mb_emit_icall (mb
, conv_to_icall (MONO_MARSHAL_CONV_FTN_DEL
));
6499 mono_mb_emit_stloc (mb
, 3);
6502 mono_mb_emit_stloc (mb
, 0);
6504 /* Make a copy since emit_conv modifies local 0 */
6505 loc
= mono_mb_add_local (mb
, &mono_defaults
.int_class
->byval_arg
);
6506 mono_mb_emit_ldloc (mb
, 0);
6507 mono_mb_emit_stloc (mb
, loc
);
6509 mono_mb_emit_byte (mb
, CEE_LDNULL
);
6510 mono_mb_emit_stloc (mb
, 3);
6512 mono_mb_emit_ldloc (mb
, 0);
6513 pos
= mono_mb_emit_branch (mb
, CEE_BRFALSE
);
6515 /* allocate result object */
6517 mono_mb_emit_byte (mb
, MONO_CUSTOM_PREFIX
);
6518 mono_mb_emit_op (mb
, CEE_MONO_NEWOBJ
, klass
);
6519 mono_mb_emit_stloc (mb
, 3);
6523 mono_mb_emit_ldloc (mb
, 3);
6524 mono_mb_emit_ldflda (mb
, sizeof (MonoObject
));
6525 mono_mb_emit_stloc (mb
, 1);
6527 /* emit conversion code */
6528 emit_struct_conv (mb
, klass
, TRUE
);
6530 emit_struct_free (mb
, klass
, loc
);
6532 /* Free the pointer allocated by unmanaged code */
6533 mono_mb_emit_ldloc (mb
, loc
);
6534 mono_mb_emit_icall (mb
, g_free
);
6535 mono_mb_patch_branch (mb
, pos
);
6539 case MARSHAL_ACTION_MANAGED_CONV_IN
:
6540 conv_arg
= mono_mb_add_local (mb
, &klass
->byval_arg
);
6542 if (klass
->delegate
) {
6543 g_assert (!t
->byref
);
6544 mono_mb_emit_byte (mb
, MONO_CUSTOM_PREFIX
);
6545 mono_mb_emit_op (mb
, CEE_MONO_CLASSCONST
, klass
);
6546 mono_mb_emit_ldarg (mb
, argnum
);
6547 mono_mb_emit_icall (mb
, conv_to_icall (MONO_MARSHAL_CONV_FTN_DEL
));
6548 mono_mb_emit_stloc (mb
, conv_arg
);
6552 if (klass
== mono_defaults
.stringbuilder_class
) {
6553 MonoMarshalNative encoding
;
6555 encoding
= mono_marshal_get_string_encoding (m
->piinfo
, spec
);
6558 g_assert (encoding
== MONO_NATIVE_LPSTR
);
6560 g_assert (!t
->byref
);
6561 g_assert (encoding
!= -1);
6563 mono_mb_emit_ldarg (mb
, argnum
);
6564 mono_mb_emit_icall (mb
, mono_string_utf8_to_builder2
);
6565 mono_mb_emit_stloc (mb
, conv_arg
);
6569 /* The class can not have an automatic layout */
6570 if ((klass
->flags
& TYPE_ATTRIBUTE_LAYOUT_MASK
) == TYPE_ATTRIBUTE_AUTO_LAYOUT
) {
6571 mono_mb_emit_auto_layout_exception (mb
, klass
);
6575 if (t
->attrs
& PARAM_ATTRIBUTE_OUT
) {
6576 mono_mb_emit_byte (mb
, CEE_LDNULL
);
6577 mono_mb_emit_stloc (mb
, conv_arg
);
6582 mono_mb_emit_ldarg (mb
, argnum
);
6586 /* Check for NULL and raise an exception */
6587 pos2
= mono_mb_emit_branch (mb
, CEE_BRTRUE
);
6589 mono_mb_emit_exception (mb
, "ArgumentNullException", NULL
);
6591 mono_mb_patch_branch (mb
, pos2
);
6592 mono_mb_emit_ldarg (mb
, argnum
);
6593 mono_mb_emit_byte (mb
, CEE_LDIND_I
);
6596 mono_mb_emit_stloc (mb
, 0);
6598 mono_mb_emit_byte (mb
, CEE_LDC_I4_0
);
6599 mono_mb_emit_stloc (mb
, conv_arg
);
6601 mono_mb_emit_ldloc (mb
, 0);
6602 pos
= mono_mb_emit_branch (mb
, CEE_BRFALSE
);
6604 /* Create and set dst */
6605 mono_mb_emit_byte (mb
, MONO_CUSTOM_PREFIX
);
6606 mono_mb_emit_op (mb
, CEE_MONO_NEWOBJ
, klass
);
6607 mono_mb_emit_stloc (mb
, conv_arg
);
6608 mono_mb_emit_ldloc (mb
, conv_arg
);
6609 mono_mb_emit_ldflda (mb
, sizeof (MonoObject
));
6610 mono_mb_emit_stloc (mb
, 1);
6612 /* emit valuetype conversion code */
6613 emit_struct_conv (mb
, klass
, TRUE
);
6615 mono_mb_patch_branch (mb
, pos
);
6618 case MARSHAL_ACTION_MANAGED_CONV_OUT
:
6620 /* Check for null */
6621 mono_mb_emit_ldloc (mb
, conv_arg
);
6622 pos
= mono_mb_emit_branch (mb
, CEE_BRTRUE
);
6623 mono_mb_emit_ldarg (mb
, argnum
);
6624 mono_mb_emit_byte (mb
, CEE_LDC_I4_0
);
6625 mono_mb_emit_byte (mb
, CEE_STIND_REF
);
6626 pos2
= mono_mb_emit_branch (mb
, CEE_BR
);
6628 mono_mb_patch_branch (mb
, pos
);
6631 mono_mb_emit_ldloc (mb
, conv_arg
);
6632 mono_mb_emit_ldflda (mb
, sizeof (MonoObject
));
6633 mono_mb_emit_stloc (mb
, 0);
6635 /* Allocate and set dest */
6636 mono_mb_emit_icon (mb
, mono_class_native_size (klass
, NULL
));
6637 mono_mb_emit_byte (mb
, CEE_CONV_I
);
6638 mono_mb_emit_icall (mb
, mono_marshal_alloc
);
6639 mono_mb_emit_stloc (mb
, 1);
6641 /* Update argument pointer */
6642 mono_mb_emit_ldarg (mb
, argnum
);
6643 mono_mb_emit_ldloc (mb
, 1);
6644 mono_mb_emit_byte (mb
, CEE_STIND_I
);
6646 /* emit valuetype conversion code */
6647 emit_struct_conv (mb
, klass
, FALSE
);
6649 mono_mb_patch_branch (mb
, pos2
);
6651 /* byval [Out] marshalling */
6653 /* FIXME: Handle null */
6656 mono_mb_emit_ldloc (mb
, conv_arg
);
6657 mono_mb_emit_ldflda (mb
, sizeof (MonoObject
));
6658 mono_mb_emit_stloc (mb
, 0);
6661 mono_mb_emit_ldarg (mb
, argnum
);
6662 mono_mb_emit_stloc (mb
, 1);
6664 /* emit valuetype conversion code */
6665 emit_struct_conv (mb
, klass
, FALSE
);
6669 case MARSHAL_ACTION_MANAGED_CONV_RESULT
:
6670 if (klass
->delegate
) {
6671 mono_mb_emit_icall (mb
, conv_to_icall (MONO_MARSHAL_CONV_DEL_FTN
));
6672 mono_mb_emit_stloc (mb
, 3);
6676 /* The class can not have an automatic layout */
6677 if ((klass
->flags
& TYPE_ATTRIBUTE_LAYOUT_MASK
) == TYPE_ATTRIBUTE_AUTO_LAYOUT
) {
6678 mono_mb_emit_auto_layout_exception (mb
, klass
);
6682 mono_mb_emit_stloc (mb
, 0);
6683 /* Check for null */
6684 mono_mb_emit_ldloc (mb
, 0);
6685 pos
= mono_mb_emit_branch (mb
, CEE_BRTRUE
);
6686 mono_mb_emit_byte (mb
, CEE_LDNULL
);
6687 mono_mb_emit_stloc (mb
, 3);
6688 pos2
= mono_mb_emit_branch (mb
, CEE_BR
);
6690 mono_mb_patch_branch (mb
, pos
);
6693 mono_mb_emit_ldloc (mb
, 0);
6694 mono_mb_emit_ldflda (mb
, sizeof (MonoObject
));
6695 mono_mb_emit_stloc (mb
, 0);
6697 /* Allocate and set dest */
6698 mono_mb_emit_icon (mb
, mono_class_native_size (klass
, NULL
));
6699 mono_mb_emit_byte (mb
, CEE_CONV_I
);
6700 mono_mb_emit_icall (mb
, mono_marshal_alloc
);
6701 mono_mb_emit_byte (mb
, CEE_DUP
);
6702 mono_mb_emit_stloc (mb
, 1);
6703 mono_mb_emit_stloc (mb
, 3);
6705 emit_struct_conv (mb
, klass
, FALSE
);
6707 mono_mb_patch_branch (mb
, pos2
);
6711 g_assert_not_reached ();
6718 emit_marshal_variant (EmitMarshalContext
*m
, int argnum
, MonoType
*t
,
6719 MonoMarshalSpec
*spec
,
6720 int conv_arg
, MonoType
**conv_arg_type
,
6721 MarshalAction action
)
6723 MonoMethodBuilder
*mb
= m
->mb
;
6724 static MonoMethod
*get_object_for_native_variant
= NULL
;
6725 static MonoMethod
*get_native_variant_for_object
= NULL
;
6727 mono_init_com_types ();
6729 if (!get_object_for_native_variant
)
6730 get_object_for_native_variant
= mono_class_get_method_from_name (mono_defaults
.marshal_class
, "GetObjectForNativeVariant", 1);
6731 g_assert (get_object_for_native_variant
);
6733 if (!get_native_variant_for_object
)
6734 get_native_variant_for_object
= mono_class_get_method_from_name (mono_defaults
.marshal_class
, "GetNativeVariantForObject", 2);
6735 g_assert (get_native_variant_for_object
);
6738 case MARSHAL_ACTION_CONV_IN
: {
6739 conv_arg
= mono_mb_add_local (mb
, &mono_defaults
.variant_class
->byval_arg
);
6742 *conv_arg_type
= &mono_defaults
.variant_class
->this_arg
;
6744 *conv_arg_type
= &mono_defaults
.variant_class
->byval_arg
;
6746 if (t
->byref
&& !(t
->attrs
& PARAM_ATTRIBUTE_IN
) && t
->attrs
& PARAM_ATTRIBUTE_OUT
)
6749 mono_mb_emit_ldarg (mb
, argnum
);
6751 mono_mb_emit_byte(mb
, CEE_LDIND_REF
);
6752 mono_mb_emit_ldloc_addr (mb
, conv_arg
);
6753 mono_mb_emit_managed_call (mb
, get_native_variant_for_object
, NULL
);
6757 case MARSHAL_ACTION_CONV_OUT
: {
6758 static MonoMethod
*variant_clear
= NULL
;
6761 variant_clear
= mono_class_get_method_from_name (mono_defaults
.variant_class
, "Clear", 0);
6762 g_assert (variant_clear
);
6765 if (t
->byref
&& (t
->attrs
& PARAM_ATTRIBUTE_OUT
|| !(t
->attrs
& PARAM_ATTRIBUTE_IN
))) {
6766 mono_mb_emit_ldarg (mb
, argnum
);
6767 mono_mb_emit_ldloc_addr (mb
, conv_arg
);
6768 mono_mb_emit_managed_call (mb
, get_object_for_native_variant
, NULL
);
6769 mono_mb_emit_byte (mb
, CEE_STIND_REF
);
6772 mono_mb_emit_ldloc_addr (mb
, conv_arg
);
6773 mono_mb_emit_managed_call (mb
, variant_clear
, NULL
);
6777 case MARSHAL_ACTION_PUSH
:
6779 mono_mb_emit_ldloc_addr (mb
, conv_arg
);
6781 mono_mb_emit_ldloc (mb
, conv_arg
);
6784 case MARSHAL_ACTION_CONV_RESULT
: {
6785 char *msg
= g_strdup ("Marshalling of VARIANT not supported as a return type.");
6786 mono_mb_emit_exception_marshal_directive (mb
, msg
);
6790 case MARSHAL_ACTION_MANAGED_CONV_IN
: {
6791 conv_arg
= mono_mb_add_local (mb
, &mono_defaults
.object_class
->byval_arg
);
6794 *conv_arg_type
= &mono_defaults
.variant_class
->this_arg
;
6796 *conv_arg_type
= &mono_defaults
.variant_class
->byval_arg
;
6798 if (t
->byref
&& !(t
->attrs
& PARAM_ATTRIBUTE_IN
) && t
->attrs
& PARAM_ATTRIBUTE_OUT
)
6802 mono_mb_emit_ldarg (mb
, argnum
);
6804 mono_mb_emit_ldarg_addr (mb
, argnum
);
6805 mono_mb_emit_managed_call (mb
, get_object_for_native_variant
, NULL
);
6806 mono_mb_emit_stloc (mb
, conv_arg
);
6810 case MARSHAL_ACTION_MANAGED_CONV_OUT
: {
6811 if (t
->byref
&& (t
->attrs
& PARAM_ATTRIBUTE_OUT
|| !(t
->attrs
& PARAM_ATTRIBUTE_IN
))) {
6812 mono_mb_emit_ldloc (mb
, conv_arg
);
6813 mono_mb_emit_ldarg (mb
, argnum
);
6814 mono_mb_emit_managed_call (mb
, get_native_variant_for_object
, NULL
);
6819 case MARSHAL_ACTION_MANAGED_CONV_RESULT
: {
6820 char *msg
= g_strdup ("Marshalling of VARIANT not supported as a return type.");
6821 mono_mb_emit_exception_marshal_directive (mb
, msg
);
6826 g_assert_not_reached ();
6833 emit_marshal_array (EmitMarshalContext
*m
, int argnum
, MonoType
*t
,
6834 MonoMarshalSpec
*spec
,
6835 int conv_arg
, MonoType
**conv_arg_type
,
6836 MarshalAction action
)
6838 MonoMethodBuilder
*mb
= m
->mb
;
6839 MonoClass
*klass
= mono_class_from_mono_type (t
);
6840 gboolean need_convert
, need_free
;
6841 MonoMarshalNative encoding
;
6843 encoding
= mono_marshal_get_string_encoding (m
->piinfo
, spec
);
6846 case MARSHAL_ACTION_CONV_IN
:
6847 *conv_arg_type
= &mono_defaults
.object_class
->byval_arg
;
6848 conv_arg
= mono_mb_add_local (mb
, &mono_defaults
.object_class
->byval_arg
);
6850 if (klass
->element_class
->blittable
) {
6851 mono_mb_emit_ldarg (mb
, argnum
);
6853 mono_mb_emit_byte (mb
, CEE_LDIND_I
);
6854 mono_mb_emit_icall (mb
, conv_to_icall (MONO_MARSHAL_CONV_ARRAY_LPARRAY
));
6855 mono_mb_emit_stloc (mb
, conv_arg
);
6858 guint32 label1
, label2
, label3
;
6859 int index_var
, src_var
, dest_ptr
, esize
;
6860 MonoMarshalConv conv
;
6861 gboolean is_string
= FALSE
;
6863 dest_ptr
= mono_mb_add_local (mb
, &mono_defaults
.int_class
->byval_arg
);
6865 eklass
= klass
->element_class
;
6867 if (eklass
== mono_defaults
.string_class
) {
6869 conv
= mono_marshal_get_string_to_ptr_conv (m
->piinfo
, spec
);
6871 else if (eklass
== mono_defaults
.stringbuilder_class
) {
6873 conv
= mono_marshal_get_stringbuilder_to_ptr_conv (m
->piinfo
, spec
);
6878 src_var
= mono_mb_add_local (mb
, &mono_defaults
.object_class
->byval_arg
);
6879 mono_mb_emit_ldarg (mb
, argnum
);
6881 mono_mb_emit_byte (mb
, CEE_LDIND_I
);
6882 mono_mb_emit_stloc (mb
, src_var
);
6885 mono_mb_emit_ldloc (mb
, src_var
);
6886 mono_mb_emit_stloc (mb
, conv_arg
);
6887 mono_mb_emit_ldloc (mb
, src_var
);
6888 label1
= mono_mb_emit_branch (mb
, CEE_BRFALSE
);
6892 char *msg
= g_strdup_printf ("string/stringbuilder marshalling conversion %d not implemented", encoding
);
6893 MonoException
*exc
= mono_get_exception_not_implemented (msg
);
6894 g_warning ("%s", msg
);
6896 mono_raise_exception (exc
);
6901 esize
= sizeof (gpointer
);
6903 esize
= mono_class_native_size (eklass
, NULL
);
6905 /* allocate space for the native struct and store the address */
6906 mono_mb_emit_icon (mb
, esize
);
6907 mono_mb_emit_ldloc (mb
, src_var
);
6908 mono_mb_emit_byte (mb
, CEE_LDLEN
);
6910 if (eklass
== mono_defaults
.string_class
) {
6911 /* Make the array bigger for the terminating null */
6912 mono_mb_emit_byte (mb
, CEE_LDC_I4_1
);
6913 mono_mb_emit_byte (mb
, CEE_ADD
);
6915 mono_mb_emit_byte (mb
, CEE_MUL
);
6916 mono_mb_emit_byte (mb
, CEE_PREFIX1
);
6917 mono_mb_emit_byte (mb
, CEE_LOCALLOC
);
6918 mono_mb_emit_stloc (mb
, conv_arg
);
6920 mono_mb_emit_ldloc (mb
, conv_arg
);
6921 mono_mb_emit_stloc (mb
, dest_ptr
);
6923 /* Emit marshalling loop */
6924 index_var
= mono_mb_add_local (mb
, &mono_defaults
.int_class
->byval_arg
);
6925 mono_mb_emit_byte (mb
, CEE_LDC_I4_0
);
6926 mono_mb_emit_stloc (mb
, index_var
);
6927 label2
= mono_mb_get_label (mb
);
6928 mono_mb_emit_ldloc (mb
, index_var
);
6929 mono_mb_emit_ldloc (mb
, src_var
);
6930 mono_mb_emit_byte (mb
, CEE_LDLEN
);
6931 label3
= mono_mb_emit_branch (mb
, CEE_BGE
);
6933 /* Emit marshalling code */
6936 mono_mb_emit_ldloc (mb
, dest_ptr
);
6937 mono_mb_emit_ldloc (mb
, src_var
);
6938 mono_mb_emit_ldloc (mb
, index_var
);
6939 mono_mb_emit_byte (mb
, CEE_LDELEM_REF
);
6940 mono_mb_emit_icall (mb
, conv_to_icall (conv
));
6941 mono_mb_emit_byte (mb
, CEE_STIND_I
);
6943 /* set the src_ptr */
6944 mono_mb_emit_ldloc (mb
, src_var
);
6945 mono_mb_emit_ldloc (mb
, index_var
);
6946 mono_mb_emit_op (mb
, CEE_LDELEMA
, eklass
);
6947 mono_mb_emit_stloc (mb
, 0);
6950 mono_mb_emit_ldloc (mb
, dest_ptr
);
6951 mono_mb_emit_stloc (mb
, 1);
6953 /* emit valuetype conversion code */
6954 emit_struct_conv (mb
, eklass
, FALSE
);
6957 mono_mb_emit_add_to_local (mb
, index_var
, 1);
6958 mono_mb_emit_add_to_local (mb
, dest_ptr
, esize
);
6960 mono_mb_emit_branch_label (mb
, CEE_BR
, label2
);
6962 mono_mb_patch_branch (mb
, label3
);
6964 if (eklass
== mono_defaults
.string_class
) {
6965 /* Null terminate */
6966 mono_mb_emit_ldloc (mb
, dest_ptr
);
6967 mono_mb_emit_byte (mb
, CEE_LDC_I4_0
);
6968 mono_mb_emit_byte (mb
, CEE_STIND_REF
);
6971 mono_mb_patch_branch (mb
, label1
);
6976 case MARSHAL_ACTION_CONV_OUT
:
6977 /* Unicode character arrays are implicitly marshalled as [Out] under MS.NET */
6978 need_convert
= ((klass
->element_class
== mono_defaults
.char_class
) && (encoding
== MONO_NATIVE_LPWSTR
)) || (klass
->element_class
== mono_defaults
.stringbuilder_class
) || (t
->attrs
& PARAM_ATTRIBUTE_OUT
);
6979 need_free
= mono_marshal_need_free (&klass
->element_class
->byval_arg
,
6982 if (need_convert
|| need_free
) {
6983 /* FIXME: Optimize blittable case */
6985 guint32 label1
, label2
, label3
;
6986 int index_var
, src_ptr
, loc
, esize
;
6988 eklass
= klass
->element_class
;
6989 if ((eklass
== mono_defaults
.stringbuilder_class
) || (eklass
== mono_defaults
.string_class
))
6990 esize
= sizeof (gpointer
);
6992 esize
= mono_class_native_size (eklass
, NULL
);
6993 src_ptr
= mono_mb_add_local (mb
, &mono_defaults
.int_class
->byval_arg
);
6994 loc
= mono_mb_add_local (mb
, &mono_defaults
.int_class
->byval_arg
);
6997 mono_mb_emit_ldarg (mb
, argnum
);
6999 mono_mb_emit_byte (mb
, CEE_LDIND_I
);
7000 label1
= mono_mb_emit_branch (mb
, CEE_BRFALSE
);
7002 mono_mb_emit_ldloc (mb
, conv_arg
);
7003 mono_mb_emit_stloc (mb
, src_ptr
);
7005 /* Emit marshalling loop */
7006 index_var
= mono_mb_add_local (mb
, &mono_defaults
.int_class
->byval_arg
);
7007 mono_mb_emit_byte (mb
, CEE_LDC_I4_0
);
7008 mono_mb_emit_stloc (mb
, index_var
);
7009 label2
= mono_mb_get_label (mb
);
7010 mono_mb_emit_ldloc (mb
, index_var
);
7011 mono_mb_emit_ldarg (mb
, argnum
);
7013 mono_mb_emit_byte (mb
, CEE_LDIND_REF
);
7014 mono_mb_emit_byte (mb
, CEE_LDLEN
);
7015 label3
= mono_mb_emit_branch (mb
, CEE_BGE
);
7017 /* Emit marshalling code */
7019 if (eklass
== mono_defaults
.stringbuilder_class
) {
7020 gboolean need_free2
;
7021 MonoMarshalConv conv
= mono_marshal_get_ptr_to_stringbuilder_conv (m
->piinfo
, spec
, &need_free2
);
7023 g_assert (conv
!= -1);
7026 mono_mb_emit_ldarg (mb
, argnum
);
7028 mono_mb_emit_byte (mb
, CEE_LDIND_I
);
7029 mono_mb_emit_ldloc (mb
, index_var
);
7030 mono_mb_emit_byte (mb
, CEE_LDELEM_REF
);
7033 mono_mb_emit_ldloc (mb
, src_ptr
);
7034 mono_mb_emit_byte (mb
, CEE_LDIND_I
);
7036 mono_mb_emit_icall (mb
, conv_to_icall (conv
));
7040 mono_mb_emit_ldloc (mb
, src_ptr
);
7041 mono_mb_emit_byte (mb
, CEE_LDIND_I
);
7043 mono_mb_emit_icall (mb
, mono_marshal_free
);
7046 else if (eklass
== mono_defaults
.string_class
) {
7049 mono_mb_emit_ldloc (mb
, src_ptr
);
7050 mono_mb_emit_byte (mb
, CEE_LDIND_I
);
7052 mono_mb_emit_icall (mb
, mono_marshal_free
);
7057 /* set the src_ptr */
7058 mono_mb_emit_ldloc (mb
, src_ptr
);
7059 mono_mb_emit_stloc (mb
, 0);
7062 mono_mb_emit_ldarg (mb
, argnum
);
7064 mono_mb_emit_byte (mb
, CEE_LDIND_REF
);
7065 mono_mb_emit_ldloc (mb
, index_var
);
7066 mono_mb_emit_op (mb
, CEE_LDELEMA
, eklass
);
7067 mono_mb_emit_stloc (mb
, 1);
7069 /* emit valuetype conversion code */
7070 emit_struct_conv (mb
, eklass
, TRUE
);
7074 mono_mb_emit_ldloc (mb
, src_ptr
);
7075 mono_mb_emit_stloc (mb
, loc
);
7076 mono_mb_emit_ldloc (mb
, loc
);
7078 emit_struct_free (mb
, eklass
, loc
);
7082 mono_mb_emit_add_to_local (mb
, index_var
, 1);
7083 mono_mb_emit_add_to_local (mb
, src_ptr
, esize
);
7085 mono_mb_emit_branch_label (mb
, CEE_BR
, label2
);
7087 mono_mb_patch_branch (mb
, label1
);
7088 mono_mb_patch_branch (mb
, label3
);
7091 if (klass
->element_class
->blittable
) {
7092 /* free memory allocated (if any) by MONO_MARSHAL_CONV_ARRAY_LPARRAY */
7094 mono_mb_emit_ldarg (mb
, argnum
);
7095 mono_mb_emit_ldloc (mb
, conv_arg
);
7096 mono_mb_emit_icall (mb
, conv_to_icall (MONO_MARSHAL_FREE_LPARRAY
));
7101 case MARSHAL_ACTION_PUSH
:
7103 mono_mb_emit_ldloc_addr (mb
, conv_arg
);
7105 mono_mb_emit_ldloc (mb
, conv_arg
);
7108 case MARSHAL_ACTION_CONV_RESULT
:
7109 /* fixme: we need conversions here */
7110 mono_mb_emit_stloc (mb
, 3);
7113 case MARSHAL_ACTION_MANAGED_CONV_IN
: {
7115 guint32 label1
, label2
, label3
;
7116 int index_var
, src_ptr
, loc
, esize
, param_num
, num_elem
;
7117 MonoMarshalConv conv
;
7118 gboolean is_string
= FALSE
;
7120 conv_arg
= mono_mb_add_local (mb
, &mono_defaults
.object_class
->byval_arg
);
7121 *conv_arg_type
= &mono_defaults
.int_class
->byval_arg
;
7124 char *msg
= g_strdup ("Byref array marshalling to managed code is not implemented.");
7125 mono_mb_emit_exception_marshal_directive (mb
, msg
);
7129 char *msg
= g_strdup ("[MarshalAs] attribute required to marshal arrays to managed code.");
7130 mono_mb_emit_exception_marshal_directive (mb
, msg
);
7133 if (spec
->native
!= MONO_NATIVE_LPARRAY
) {
7134 char *msg
= g_strdup ("Non LPArray marshalling of arrays to managed code is not implemented.");
7135 mono_mb_emit_exception_marshal_directive (mb
, msg
);
7139 /* FIXME: t is from the method which is wrapped, not the delegate type */
7140 /* g_assert (t->attrs & PARAM_ATTRIBUTE_IN); */
7142 param_num
= spec
->data
.array_data
.param_num
;
7143 num_elem
= spec
->data
.array_data
.num_elem
;
7144 if (spec
->data
.array_data
.elem_mult
== 0)
7145 /* param_num is not specified */
7148 if (param_num
== -1) {
7149 if (num_elem
<= 0) {
7150 char *msg
= g_strdup ("Either SizeConst or SizeParamIndex should be specified when marshalling arrays to managed code.");
7151 mono_mb_emit_exception_marshal_directive (mb
, msg
);
7156 /* FIXME: Optimize blittable case */
7158 eklass
= klass
->element_class
;
7159 if (eklass
== mono_defaults
.string_class
) {
7161 conv
= mono_marshal_get_ptr_to_string_conv (m
->piinfo
, spec
, &need_free
);
7163 else if (eklass
== mono_defaults
.stringbuilder_class
) {
7165 conv
= mono_marshal_get_ptr_to_stringbuilder_conv (m
->piinfo
, spec
, &need_free
);
7170 mono_marshal_load_type_info (eklass
);
7173 esize
= sizeof (gpointer
);
7175 esize
= mono_class_native_size (eklass
, NULL
);
7176 src_ptr
= mono_mb_add_local (mb
, &mono_defaults
.int_class
->byval_arg
);
7177 loc
= mono_mb_add_local (mb
, &mono_defaults
.int_class
->byval_arg
);
7179 mono_mb_emit_byte (mb
, CEE_LDNULL
);
7180 mono_mb_emit_stloc (mb
, conv_arg
);
7182 /* Check param index */
7183 if (param_num
!= -1) {
7184 if (param_num
>= m
->sig
->param_count
) {
7185 char *msg
= g_strdup ("Array size control parameter index is out of range.");
7186 mono_mb_emit_exception_marshal_directive (mb
, msg
);
7189 switch (m
->sig
->params
[param_num
]->type
) {
7202 char *msg
= g_strdup ("Array size control parameter must be an integral type.");
7203 mono_mb_emit_exception_marshal_directive (mb
, msg
);
7210 mono_mb_emit_ldarg (mb
, argnum
);
7211 label1
= mono_mb_emit_branch (mb
, CEE_BRFALSE
);
7213 mono_mb_emit_ldarg (mb
, argnum
);
7214 mono_mb_emit_stloc (mb
, src_ptr
);
7216 /* Create managed array */
7218 * The LPArray marshalling spec says that sometimes param_num starts
7219 * from 1, sometimes it starts from 0. But MS seems to allways start
7223 if (param_num
== -1) {
7224 mono_mb_emit_icon (mb
, num_elem
);
7226 mono_mb_emit_ldarg (mb
, param_num
);
7228 mono_mb_emit_icon (mb
, num_elem
);
7229 mono_mb_emit_byte (mb
, CEE_ADD
);
7231 mono_mb_emit_byte (mb
, CEE_CONV_OVF_I
);
7234 mono_mb_emit_op (mb
, CEE_NEWARR
, eklass
);
7235 mono_mb_emit_stloc (mb
, conv_arg
);
7237 if (eklass
->blittable
) {
7238 mono_mb_emit_ldloc (mb
, conv_arg
);
7239 mono_mb_emit_byte (mb
, CEE_CONV_I
);
7240 mono_mb_emit_icon (mb
, G_STRUCT_OFFSET (MonoArray
, vector
));
7241 mono_mb_emit_byte (mb
, CEE_ADD
);
7242 mono_mb_emit_ldarg (mb
, argnum
);
7243 mono_mb_emit_ldloc (mb
, conv_arg
);
7244 mono_mb_emit_byte (mb
, CEE_LDLEN
);
7245 mono_mb_emit_icon (mb
, esize
);
7246 mono_mb_emit_byte (mb
, CEE_MUL
);
7247 mono_mb_emit_byte (mb
, CEE_PREFIX1
);
7248 mono_mb_emit_byte (mb
, CEE_CPBLK
);
7252 /* Emit marshalling loop */
7253 index_var
= mono_mb_add_local (mb
, &mono_defaults
.int_class
->byval_arg
);
7254 mono_mb_emit_byte (mb
, CEE_LDC_I4_0
);
7255 mono_mb_emit_stloc (mb
, index_var
);
7256 label2
= mono_mb_get_label (mb
);
7257 mono_mb_emit_ldloc (mb
, index_var
);
7258 mono_mb_emit_ldloc (mb
, conv_arg
);
7259 mono_mb_emit_byte (mb
, CEE_LDLEN
);
7260 label3
= mono_mb_emit_branch (mb
, CEE_BGE
);
7262 /* Emit marshalling code */
7264 g_assert (conv
!= -1);
7266 mono_mb_emit_ldloc (mb
, conv_arg
);
7267 mono_mb_emit_ldloc (mb
, index_var
);
7269 mono_mb_emit_ldloc (mb
, src_ptr
);
7270 mono_mb_emit_byte (mb
, CEE_LDIND_I
);
7272 mono_mb_emit_icall (mb
, conv_to_icall (conv
));
7273 mono_mb_emit_byte (mb
, CEE_STELEM_REF
);
7276 char *msg
= g_strdup ("Marshalling of non-string and non-blittable arrays to managed code is not implemented.");
7277 mono_mb_emit_exception_marshal_directive (mb
, msg
);
7281 mono_mb_emit_add_to_local (mb
, index_var
, 1);
7282 mono_mb_emit_add_to_local (mb
, src_ptr
, esize
);
7284 mono_mb_emit_branch_label (mb
, CEE_BR
, label2
);
7286 mono_mb_patch_branch (mb
, label1
);
7287 mono_mb_patch_branch (mb
, label3
);
7291 case MARSHAL_ACTION_MANAGED_CONV_OUT
: {
7293 guint32 label1
, label2
, label3
;
7294 int index_var
, dest_ptr
, loc
, esize
, param_num
, num_elem
;
7295 MonoMarshalConv conv
;
7296 gboolean is_string
= FALSE
;
7299 /* Already handled in CONV_IN */
7302 /* These are already checked in CONV_IN */
7303 g_assert (!t
->byref
);
7304 g_assert (spec
->native
== MONO_NATIVE_LPARRAY
);
7305 g_assert (t
->attrs
& PARAM_ATTRIBUTE_OUT
);
7307 param_num
= spec
->data
.array_data
.param_num
;
7308 num_elem
= spec
->data
.array_data
.num_elem
;
7310 if (spec
->data
.array_data
.elem_mult
== 0)
7311 /* param_num is not specified */
7314 if (param_num
== -1) {
7315 if (num_elem
<= 0) {
7316 g_assert_not_reached ();
7320 /* FIXME: Optimize blittable case */
7322 eklass
= klass
->element_class
;
7323 if (eklass
== mono_defaults
.string_class
) {
7325 conv
= mono_marshal_get_string_to_ptr_conv (m
->piinfo
, spec
);
7327 else if (eklass
== mono_defaults
.stringbuilder_class
) {
7329 conv
= mono_marshal_get_stringbuilder_to_ptr_conv (m
->piinfo
, spec
);
7334 mono_marshal_load_type_info (eklass
);
7337 esize
= sizeof (gpointer
);
7339 esize
= mono_class_native_size (eklass
, NULL
);
7341 dest_ptr
= mono_mb_add_local (mb
, &mono_defaults
.int_class
->byval_arg
);
7342 loc
= mono_mb_add_local (mb
, &mono_defaults
.int_class
->byval_arg
);
7345 mono_mb_emit_ldloc (mb
, conv_arg
);
7346 label1
= mono_mb_emit_branch (mb
, CEE_BRFALSE
);
7348 mono_mb_emit_ldarg (mb
, argnum
);
7349 mono_mb_emit_stloc (mb
, dest_ptr
);
7351 if (eklass
->blittable
) {
7353 mono_mb_emit_ldarg (mb
, argnum
);
7355 mono_mb_emit_ldloc (mb
, conv_arg
);
7356 mono_mb_emit_byte (mb
, CEE_CONV_I
);
7357 mono_mb_emit_icon (mb
, G_STRUCT_OFFSET (MonoArray
, vector
));
7358 mono_mb_emit_byte (mb
, CEE_ADD
);
7360 mono_mb_emit_ldloc (mb
, conv_arg
);
7361 mono_mb_emit_byte (mb
, CEE_LDLEN
);
7362 mono_mb_emit_icon (mb
, esize
);
7363 mono_mb_emit_byte (mb
, CEE_MUL
);
7364 mono_mb_emit_byte (mb
, CEE_PREFIX1
);
7365 mono_mb_emit_byte (mb
, CEE_CPBLK
);
7369 /* Emit marshalling loop */
7370 index_var
= mono_mb_add_local (mb
, &mono_defaults
.int_class
->byval_arg
);
7371 mono_mb_emit_byte (mb
, CEE_LDC_I4_0
);
7372 mono_mb_emit_stloc (mb
, index_var
);
7373 label2
= mono_mb_get_label (mb
);
7374 mono_mb_emit_ldloc (mb
, index_var
);
7375 mono_mb_emit_ldloc (mb
, conv_arg
);
7376 mono_mb_emit_byte (mb
, CEE_LDLEN
);
7377 label3
= mono_mb_emit_branch (mb
, CEE_BGE
);
7379 /* Emit marshalling code */
7381 g_assert (conv
!= -1);
7384 mono_mb_emit_ldloc (mb
, dest_ptr
);
7387 mono_mb_emit_ldloc (mb
, conv_arg
);
7388 mono_mb_emit_ldloc (mb
, index_var
);
7390 mono_mb_emit_byte (mb
, CEE_LDELEM_REF
);
7392 mono_mb_emit_icall (mb
, conv_to_icall (conv
));
7393 mono_mb_emit_byte (mb
, CEE_STIND_I
);
7396 char *msg
= g_strdup ("Marshalling of non-string and non-blittable arrays to managed code is not implemented.");
7397 mono_mb_emit_exception_marshal_directive (mb
, msg
);
7401 mono_mb_emit_add_to_local (mb
, index_var
, 1);
7402 mono_mb_emit_add_to_local (mb
, dest_ptr
, esize
);
7404 mono_mb_emit_branch_label (mb
, CEE_BR
, label2
);
7406 mono_mb_patch_branch (mb
, label1
);
7407 mono_mb_patch_branch (mb
, label3
);
7411 case MARSHAL_ACTION_MANAGED_CONV_RESULT
: {
7413 guint32 label1
, label2
, label3
;
7414 int index_var
, src
, dest
, esize
;
7415 MonoMarshalConv conv
= -1;
7416 gboolean is_string
= FALSE
;
7418 g_assert (!t
->byref
);
7420 eklass
= klass
->element_class
;
7422 mono_marshal_load_type_info (eklass
);
7424 if (eklass
== mono_defaults
.string_class
) {
7426 conv
= mono_marshal_get_string_to_ptr_conv (m
->piinfo
, spec
);
7429 g_assert_not_reached ();
7433 esize
= sizeof (gpointer
);
7435 esize
= mono_class_native_size (eklass
, NULL
);
7437 src
= mono_mb_add_local (mb
, &mono_defaults
.object_class
->byval_arg
);
7438 dest
= mono_mb_add_local (mb
, &mono_defaults
.int_class
->byval_arg
);
7440 mono_mb_emit_stloc (mb
, src
);
7441 mono_mb_emit_ldloc (mb
, src
);
7442 mono_mb_emit_stloc (mb
, 3);
7444 /* Check for null */
7445 mono_mb_emit_ldloc (mb
, src
);
7446 label1
= mono_mb_emit_branch (mb
, CEE_BRFALSE
);
7448 /* Allocate native array */
7449 mono_mb_emit_icon (mb
, esize
);
7450 mono_mb_emit_ldloc (mb
, src
);
7451 mono_mb_emit_byte (mb
, CEE_LDLEN
);
7453 if (eklass
== mono_defaults
.string_class
) {
7454 /* Make the array bigger for the terminating null */
7455 mono_mb_emit_byte (mb
, CEE_LDC_I4_1
);
7456 mono_mb_emit_byte (mb
, CEE_ADD
);
7458 mono_mb_emit_byte (mb
, CEE_MUL
);
7459 mono_mb_emit_icall (mb
, mono_marshal_alloc
);
7460 mono_mb_emit_stloc (mb
, dest
);
7461 mono_mb_emit_ldloc (mb
, dest
);
7462 mono_mb_emit_stloc (mb
, 3);
7464 /* Emit marshalling loop */
7465 index_var
= mono_mb_add_local (mb
, &mono_defaults
.int_class
->byval_arg
);
7466 mono_mb_emit_byte (mb
, CEE_LDC_I4_0
);
7467 mono_mb_emit_stloc (mb
, index_var
);
7468 label2
= mono_mb_get_label (mb
);
7469 mono_mb_emit_ldloc (mb
, index_var
);
7470 mono_mb_emit_ldloc (mb
, src
);
7471 mono_mb_emit_byte (mb
, CEE_LDLEN
);
7472 label3
= mono_mb_emit_branch (mb
, CEE_BGE
);
7474 /* Emit marshalling code */
7476 g_assert (conv
!= -1);
7479 mono_mb_emit_ldloc (mb
, dest
);
7482 mono_mb_emit_ldloc (mb
, src
);
7483 mono_mb_emit_ldloc (mb
, index_var
);
7485 mono_mb_emit_byte (mb
, CEE_LDELEM_REF
);
7487 mono_mb_emit_icall (mb
, conv_to_icall (conv
));
7488 mono_mb_emit_byte (mb
, CEE_STIND_I
);
7491 char *msg
= g_strdup ("Marshalling of non-string arrays to managed code is not implemented.");
7492 mono_mb_emit_exception_marshal_directive (mb
, msg
);
7496 mono_mb_emit_add_to_local (mb
, index_var
, 1);
7497 mono_mb_emit_add_to_local (mb
, dest
, esize
);
7499 mono_mb_emit_branch_label (mb
, CEE_BR
, label2
);
7501 mono_mb_patch_branch (mb
, label3
);
7502 mono_mb_patch_branch (mb
, label1
);
7506 g_assert_not_reached ();
7513 emit_marshal_boolean (EmitMarshalContext
*m
, int argnum
, MonoType
*t
,
7514 MonoMarshalSpec
*spec
,
7515 int conv_arg
, MonoType
**conv_arg_type
,
7516 MarshalAction action
)
7518 MonoMethodBuilder
*mb
= m
->mb
;
7521 case MARSHAL_ACTION_CONV_IN
: {
7522 MonoType
*local_type
;
7524 guint8 ldc_op
= CEE_LDC_I4_1
;
7527 local_type
= &mono_defaults
.int32_class
->byval_arg
;
7529 switch (spec
->native
) {
7530 case MONO_NATIVE_I1
:
7531 case MONO_NATIVE_U1
:
7532 local_type
= &mono_defaults
.byte_class
->byval_arg
;
7534 case MONO_NATIVE_VARIANTBOOL
:
7535 local_type
= &mono_defaults
.int16_class
->byval_arg
;
7536 ldc_op
= CEE_LDC_I4_M1
;
7538 case MONO_NATIVE_BOOLEAN
:
7539 local_type
= &mono_defaults
.int32_class
->byval_arg
;
7542 g_warning ("marshalling bool as native type %x is currently not supported", spec
->native
);
7543 local_type
= &mono_defaults
.int32_class
->byval_arg
;
7548 *conv_arg_type
= &mono_defaults
.int_class
->byval_arg
;
7550 *conv_arg_type
= local_type
;
7551 conv_arg
= mono_mb_add_local (mb
, local_type
);
7553 mono_mb_emit_ldarg (mb
, argnum
);
7555 mono_mb_emit_byte (mb
, CEE_LDIND_I1
);
7556 label_false
= mono_mb_emit_branch (mb
, CEE_BRFALSE
);
7557 mono_mb_emit_byte (mb
, ldc_op
);
7558 mono_mb_emit_stloc (mb
, conv_arg
);
7559 mono_mb_patch_branch (mb
, label_false
);
7564 case MARSHAL_ACTION_CONV_OUT
:
7566 int label_false
, label_end
;
7570 mono_mb_emit_ldarg (mb
, argnum
);
7571 mono_mb_emit_ldloc (mb
, conv_arg
);
7573 label_false
= mono_mb_emit_branch (mb
, CEE_BRFALSE
);
7574 mono_mb_emit_byte (mb
, CEE_LDC_I4_1
);
7576 label_end
= mono_mb_emit_branch (mb
, CEE_BR
);
7577 mono_mb_patch_branch (mb
, label_false
);
7578 mono_mb_emit_byte (mb
, CEE_LDC_I4_0
);
7579 mono_mb_patch_branch (mb
, label_end
);
7581 mono_mb_emit_byte (mb
, CEE_STIND_I1
);
7585 case MARSHAL_ACTION_PUSH
:
7587 mono_mb_emit_ldloc_addr (mb
, conv_arg
);
7589 mono_mb_emit_ldloc (mb
, conv_arg
);
7591 mono_mb_emit_ldarg (mb
, argnum
);
7594 case MARSHAL_ACTION_CONV_RESULT
:
7595 /* maybe we need to make sure that it fits within 8 bits */
7596 mono_mb_emit_stloc (mb
, 3);
7599 case MARSHAL_ACTION_MANAGED_CONV_IN
: {
7600 MonoClass
* conv_arg_class
= mono_defaults
.int32_class
;
7601 guint8 ldop
= CEE_LDIND_I4
;
7602 int label_null
, label_false
;
7604 conv_arg
= mono_mb_add_local (mb
, &mono_defaults
.boolean_class
->byval_arg
);
7607 switch (spec
->native
) {
7608 case MONO_NATIVE_I1
:
7609 case MONO_NATIVE_U1
:
7610 conv_arg_class
= mono_defaults
.byte_class
;
7611 ldop
= CEE_LDIND_I1
;
7613 case MONO_NATIVE_VARIANTBOOL
:
7614 conv_arg_class
= mono_defaults
.int16_class
;
7615 ldop
= CEE_LDIND_I2
;
7617 case MONO_NATIVE_BOOLEAN
:
7620 g_warning ("marshalling bool as native type %x is currently not supported", spec
->native
);
7625 *conv_arg_type
= &conv_arg_class
->this_arg
;
7627 *conv_arg_type
= &conv_arg_class
->byval_arg
;
7630 mono_mb_emit_ldarg (mb
, argnum
);
7634 label_null
= mono_mb_emit_branch (mb
, CEE_BRFALSE
);
7635 mono_mb_emit_ldarg (mb
, argnum
);
7636 mono_mb_emit_byte (mb
, ldop
);
7640 label_false
= mono_mb_emit_branch (mb
, CEE_BRFALSE
);
7641 mono_mb_emit_byte (mb
, CEE_LDC_I4_1
);
7642 mono_mb_emit_stloc (mb
, conv_arg
);
7643 mono_mb_patch_branch (mb
, label_false
);
7646 mono_mb_patch_branch (mb
, label_null
);
7650 case MARSHAL_ACTION_MANAGED_CONV_OUT
: {
7651 guint8 stop
= CEE_STIND_I4
;
7652 guint8 ldc_op
= CEE_LDC_I4_1
;
7653 int label_null
,label_false
, label_end
;;
7658 switch (spec
->native
) {
7659 case MONO_NATIVE_I1
:
7660 case MONO_NATIVE_U1
:
7661 stop
= CEE_STIND_I1
;
7663 case MONO_NATIVE_VARIANTBOOL
:
7664 stop
= CEE_STIND_I2
;
7665 ldc_op
= CEE_LDC_I4_M1
;
7673 mono_mb_emit_ldarg (mb
, argnum
);
7674 label_null
= mono_mb_emit_branch (mb
, CEE_BRFALSE
);
7676 mono_mb_emit_ldarg (mb
, argnum
);
7677 mono_mb_emit_ldloc (mb
, conv_arg
);
7679 label_false
= mono_mb_emit_branch (mb
, CEE_BRFALSE
);
7680 mono_mb_emit_byte (mb
, ldc_op
);
7681 label_end
= mono_mb_emit_branch (mb
, CEE_BR
);
7683 mono_mb_patch_branch (mb
, label_false
);
7684 mono_mb_emit_byte (mb
, CEE_LDC_I4_0
);
7685 mono_mb_patch_branch (mb
, label_end
);
7687 mono_mb_emit_byte (mb
, stop
);
7688 mono_mb_patch_branch (mb
, label_null
);
7693 g_assert_not_reached ();
7700 emit_marshal_ptr (EmitMarshalContext
*m
, int argnum
, MonoType
*t
,
7701 MonoMarshalSpec
*spec
, int conv_arg
,
7702 MonoType
**conv_arg_type
, MarshalAction action
)
7704 MonoMethodBuilder
*mb
= m
->mb
;
7707 case MARSHAL_ACTION_CONV_IN
:
7708 if (MONO_TYPE_ISSTRUCT (t
->data
.type
) && !mono_class_from_mono_type (t
->data
.type
)->blittable
) {
7709 char *msg
= g_strdup_printf ("Can not marshal 'parameter #%d': Pointers can not reference marshaled structures. Use byref instead.", argnum
+ 1);
7710 mono_mb_emit_exception_marshal_directive (m
->mb
, msg
);
7714 case MARSHAL_ACTION_PUSH
:
7715 mono_mb_emit_ldarg (mb
, argnum
);
7718 case MARSHAL_ACTION_CONV_RESULT
:
7719 /* no conversions necessary */
7720 mono_mb_emit_stloc (mb
, 3);
7731 emit_marshal_char (EmitMarshalContext
*m
, int argnum
, MonoType
*t
,
7732 MonoMarshalSpec
*spec
, int conv_arg
,
7733 MonoType
**conv_arg_type
, MarshalAction action
)
7735 MonoMethodBuilder
*mb
= m
->mb
;
7738 case MARSHAL_ACTION_PUSH
:
7739 /* fixme: dont know how to marshal that. We cant simply
7740 * convert it to a one byte UTF8 character, because an
7741 * unicode character may need more that one byte in UTF8 */
7742 mono_mb_emit_ldarg (mb
, argnum
);
7745 case MARSHAL_ACTION_CONV_RESULT
:
7746 /* fixme: we need conversions here */
7747 mono_mb_emit_stloc (mb
, 3);
7758 emit_marshal_scalar (EmitMarshalContext
*m
, int argnum
, MonoType
*t
,
7759 MonoMarshalSpec
*spec
, int conv_arg
,
7760 MonoType
**conv_arg_type
, MarshalAction action
)
7762 MonoMethodBuilder
*mb
= m
->mb
;
7765 case MARSHAL_ACTION_PUSH
:
7766 mono_mb_emit_ldarg (mb
, argnum
);
7769 case MARSHAL_ACTION_CONV_RESULT
:
7770 /* no conversions necessary */
7771 mono_mb_emit_stloc (mb
, 3);
7782 emit_marshal (EmitMarshalContext
*m
, int argnum
, MonoType
*t
,
7783 MonoMarshalSpec
*spec
, int conv_arg
,
7784 MonoType
**conv_arg_type
, MarshalAction action
)
7786 /* Ensure that we have marshalling info for this param */
7787 mono_marshal_load_type_info (mono_class_from_mono_type (t
));
7789 if (spec
&& spec
->native
== MONO_NATIVE_CUSTOM
)
7790 return emit_marshal_custom (m
, argnum
, t
, spec
, conv_arg
, conv_arg_type
, action
);
7792 if (spec
&& spec
->native
== MONO_NATIVE_ASANY
)
7793 return emit_marshal_asany (m
, argnum
, t
, spec
, conv_arg
, conv_arg_type
, action
);
7796 case MONO_TYPE_VALUETYPE
:
7797 if (t
->data
.klass
== mono_defaults
.handleref_class
)
7798 return emit_marshal_handleref (m
, argnum
, t
, spec
, conv_arg
, conv_arg_type
, action
);
7800 return emit_marshal_vtype (m
, argnum
, t
, spec
, conv_arg
, conv_arg_type
, action
);
7801 case MONO_TYPE_STRING
:
7802 return emit_marshal_string (m
, argnum
, t
, spec
, conv_arg
, conv_arg_type
, action
);
7803 case MONO_TYPE_CLASS
:
7804 case MONO_TYPE_OBJECT
:
7805 if (spec
&& spec
->native
== MONO_NATIVE_STRUCT
)
7806 return emit_marshal_variant (m
, argnum
, t
, spec
, conv_arg
, conv_arg_type
, action
);
7809 if (spec
&& (spec
->native
== MONO_NATIVE_IUNKNOWN
||
7810 spec
->native
== MONO_NATIVE_IDISPATCH
||
7811 spec
->native
== MONO_NATIVE_INTERFACE
))
7812 return mono_cominterop_emit_marshal_com_interface (m
, argnum
, t
, spec
, conv_arg
, conv_arg_type
, action
);
7813 if (spec
&& (spec
->native
== MONO_NATIVE_SAFEARRAY
) &&
7814 (spec
->data
.safearray_data
.elem_type
== MONO_VARIANT_VARIANT
) &&
7815 ((action
== MARSHAL_ACTION_CONV_OUT
) || (action
== MARSHAL_ACTION_CONV_IN
) || (action
== MARSHAL_ACTION_PUSH
)))
7816 return mono_cominterop_emit_marshal_safearray (m
, argnum
, t
, spec
, conv_arg
, conv_arg_type
, action
);
7819 if (mono_defaults
.safehandle_class
!= NULL
&& t
->data
.klass
&&
7820 mono_class_is_subclass_of (t
->data
.klass
, mono_defaults
.safehandle_class
, FALSE
))
7821 return emit_marshal_safehandle (m
, argnum
, t
, spec
, conv_arg
, conv_arg_type
, action
);
7823 return emit_marshal_object (m
, argnum
, t
, spec
, conv_arg
, conv_arg_type
, action
);
7824 case MONO_TYPE_ARRAY
:
7825 case MONO_TYPE_SZARRAY
:
7826 return emit_marshal_array (m
, argnum
, t
, spec
, conv_arg
, conv_arg_type
, action
);
7827 case MONO_TYPE_BOOLEAN
:
7828 return emit_marshal_boolean (m
, argnum
, t
, spec
, conv_arg
, conv_arg_type
, action
);
7830 return emit_marshal_ptr (m
, argnum
, t
, spec
, conv_arg
, conv_arg_type
, action
);
7831 case MONO_TYPE_CHAR
:
7832 return emit_marshal_char (m
, argnum
, t
, spec
, conv_arg
, conv_arg_type
, action
);
7845 case MONO_TYPE_FNPTR
:
7846 return emit_marshal_scalar (m
, argnum
, t
, spec
, conv_arg
, conv_arg_type
, action
);
7847 case MONO_TYPE_GENERICINST
:
7848 if (mono_type_generic_inst_is_valuetype (t
))
7849 return emit_marshal_vtype (m
, argnum
, t
, spec
, conv_arg
, conv_arg_type
, action
);
7851 return emit_marshal_object (m
, argnum
, t
, spec
, conv_arg
, conv_arg_type
, action
);
7858 * mono_marshal_emit_native_wrapper:
7859 * @image: the image to use for looking up custom marshallers
7860 * @sig: The signature of the native function
7861 * @piinfo: Marshalling information
7862 * @mspecs: Marshalling information
7863 * @aot: whenever the created method will be compiled by the AOT compiler
7864 * @method: if non-NULL, the pinvoke method to call
7865 * @check_exceptions: Whenever to check for pending exceptions after the native call
7867 * generates IL code for the pinvoke wrapper, the generated code calls @func.
7870 mono_marshal_emit_native_wrapper (MonoImage
*image
, MonoMethodBuilder
*mb
, MonoMethodSignature
*sig
, MonoMethodPInvoke
*piinfo
, MonoMarshalSpec
**mspecs
, gpointer func
, gboolean aot
, gboolean check_exceptions
)
7872 EmitMarshalContext m
;
7873 MonoMethodSignature
*csig
;
7875 int i
, argnum
, *tmp_locals
;
7877 static MonoMethodSignature
*get_last_error_sig
= NULL
;
7882 /* we copy the signature, so that we can set pinvoke to 0 */
7883 csig
= signature_dup (mb
->method
->klass
->image
, sig
);
7888 /* we allocate local for use with emit_struct_conv() */
7889 /* allocate local 0 (pointer) src_ptr */
7890 mono_mb_add_local (mb
, &mono_defaults
.int_class
->byval_arg
);
7891 /* allocate local 1 (pointer) dst_ptr */
7892 mono_mb_add_local (mb
, &mono_defaults
.int_class
->byval_arg
);
7893 /* allocate local 2 (boolean) delete_old */
7894 mono_mb_add_local (mb
, &mono_defaults
.boolean_class
->byval_arg
);
7896 /* delete_old = FALSE */
7897 mono_mb_emit_icon (mb
, 0);
7898 mono_mb_emit_stloc (mb
, 2);
7900 if (!MONO_TYPE_IS_VOID(sig
->ret
)) {
7901 /* allocate local 3 to store the return value */
7902 mono_mb_add_local (mb
, sig
->ret
);
7905 if (mspecs
[0] && mspecs
[0]->native
== MONO_NATIVE_CUSTOM
) {
7906 /* Return type custom marshaling */
7908 * Since we can't determine the return type of the unmanaged function,
7909 * we assume it returns a pointer, and pass that pointer to
7910 * MarshalNativeToManaged.
7912 csig
->ret
= &mono_defaults
.int_class
->byval_arg
;
7915 /* we first do all conversions */
7916 tmp_locals
= alloca (sizeof (int) * sig
->param_count
);
7917 m
.orig_conv_args
= alloca (sizeof (int) * (sig
->param_count
+ 1));
7919 for (i
= 0; i
< sig
->param_count
; i
++) {
7920 tmp_locals
[i
] = emit_marshal (&m
, i
+ sig
->hasthis
, sig
->params
[i
], mspecs
[i
+ 1], 0, &csig
->params
[i
], MARSHAL_ACTION_CONV_IN
);
7923 /* push all arguments */
7926 mono_mb_emit_byte (mb
, CEE_LDARG_0
);
7928 for (i
= 0; i
< sig
->param_count
; i
++) {
7929 emit_marshal (&m
, i
+ sig
->hasthis
, sig
->params
[i
], mspecs
[i
+ 1], tmp_locals
[i
], NULL
, MARSHAL_ACTION_PUSH
);
7932 /* call the native method */
7933 if (MONO_CLASS_IS_IMPORT (mb
->method
->klass
)) {
7935 mono_mb_emit_cominterop_call (mb
, csig
, &piinfo
->method
);
7937 g_assert_not_reached ();
7942 /* Reuse the ICALL_ADDR opcode for pinvokes too */
7943 mono_mb_emit_byte (mb
, MONO_CUSTOM_PREFIX
);
7944 mono_mb_emit_op (mb
, CEE_MONO_ICALL_ADDR
, &piinfo
->method
);
7945 mono_mb_emit_calli (mb
, csig
);
7947 mono_mb_emit_native_call (mb
, csig
, func
);
7951 /* Set LastError if needed */
7952 if (piinfo
->piflags
& PINVOKE_ATTRIBUTE_SUPPORTS_LAST_ERROR
) {
7953 if (!get_last_error_sig
) {
7954 get_last_error_sig
= mono_metadata_signature_alloc (mono_defaults
.corlib
, 0);
7955 get_last_error_sig
->ret
= &mono_defaults
.int_class
->byval_arg
;
7956 get_last_error_sig
->pinvoke
= 1;
7961 * Have to call GetLastError () early and without a wrapper, since various runtime components could
7962 * clobber its value.
7964 mono_mb_emit_native_call (mb
, get_last_error_sig
, GetLastError
);
7965 mono_mb_emit_icall (mb
, mono_marshal_set_last_error_windows
);
7967 mono_mb_emit_icall (mb
, mono_marshal_set_last_error
);
7971 /* convert the result */
7972 if (!sig
->ret
->byref
) {
7973 MonoMarshalSpec
*spec
= mspecs
[0];
7974 type
= sig
->ret
->type
;
7976 if (spec
&& spec
->native
== MONO_NATIVE_CUSTOM
) {
7977 emit_marshal (&m
, 0, sig
->ret
, spec
, 0, NULL
, MARSHAL_ACTION_CONV_RESULT
);
7982 case MONO_TYPE_VOID
:
7984 case MONO_TYPE_VALUETYPE
:
7985 klass
= sig
->ret
->data
.klass
;
7986 if (klass
->enumtype
) {
7987 type
= mono_class_enum_basetype (sig
->ret
->data
.klass
)->type
;
7990 emit_marshal (&m
, 0, sig
->ret
, spec
, 0, NULL
, MARSHAL_ACTION_CONV_RESULT
);
8004 case MONO_TYPE_FNPTR
:
8005 case MONO_TYPE_STRING
:
8006 case MONO_TYPE_CLASS
:
8007 case MONO_TYPE_OBJECT
:
8008 case MONO_TYPE_BOOLEAN
:
8009 case MONO_TYPE_ARRAY
:
8010 case MONO_TYPE_SZARRAY
:
8011 case MONO_TYPE_CHAR
:
8013 case MONO_TYPE_GENERICINST
:
8014 emit_marshal (&m
, 0, sig
->ret
, spec
, 0, NULL
, MARSHAL_ACTION_CONV_RESULT
);
8016 case MONO_TYPE_TYPEDBYREF
:
8018 g_warning ("return type 0x%02x unknown", sig
->ret
->type
);
8019 g_assert_not_reached ();
8023 mono_mb_emit_stloc (mb
, 3);
8027 * Need to call this after converting the result since MONO_VTADDR needs
8028 * to be adjacent to the call instruction.
8030 if (check_exceptions
)
8031 emit_thread_interrupt_checkpoint (mb
);
8033 /* we need to convert byref arguments back and free string arrays */
8034 for (i
= 0; i
< sig
->param_count
; i
++) {
8035 MonoType
*t
= sig
->params
[i
];
8036 MonoMarshalSpec
*spec
= mspecs
[i
+ 1];
8038 argnum
= i
+ sig
->hasthis
;
8040 if (spec
&& ((spec
->native
== MONO_NATIVE_CUSTOM
) || (spec
->native
== MONO_NATIVE_ASANY
))) {
8041 emit_marshal (&m
, argnum
, t
, spec
, tmp_locals
[i
], NULL
, MARSHAL_ACTION_CONV_OUT
);
8046 case MONO_TYPE_STRING
:
8047 case MONO_TYPE_VALUETYPE
:
8048 case MONO_TYPE_CLASS
:
8049 case MONO_TYPE_OBJECT
:
8050 case MONO_TYPE_SZARRAY
:
8051 case MONO_TYPE_BOOLEAN
:
8052 emit_marshal (&m
, argnum
, t
, spec
, tmp_locals
[i
], NULL
, MARSHAL_ACTION_CONV_OUT
);
8057 if (!MONO_TYPE_IS_VOID(sig
->ret
))
8058 mono_mb_emit_ldloc (mb
, 3);
8060 mono_mb_emit_byte (mb
, CEE_RET
);
8063 G_GNUC_UNUSED
static void
8064 code_for (MonoMethod
*method
) {
8065 MonoMethodHeader
*header
= mono_method_get_header (method
);
8066 printf ("CODE FOR %s: \n%s.\n", mono_method_full_name (method
, TRUE
), mono_disasm_code (0, method
, header
->code
, header
->code
+ header
->code_size
));
8070 * mono_marshal_get_native_wrapper:
8071 * @method: The MonoMethod to wrap.
8072 * @check_exceptions: Whenever to check for pending exceptions
8074 * generates IL code for the pinvoke wrapper (the generated method
8075 * calls the unmanaged code in piinfo->addr)
8078 mono_marshal_get_native_wrapper (MonoMethod
*method
, gboolean check_exceptions
, gboolean aot
)
8080 MonoMethodSignature
*sig
, *csig
;
8081 MonoMethodPInvoke
*piinfo
= (MonoMethodPInvoke
*) method
;
8082 MonoMethodBuilder
*mb
;
8083 MonoMarshalSpec
**mspecs
;
8086 gboolean pinvoke
= FALSE
;
8089 const char *exc_class
= "MissingMethodException";
8090 const char *exc_arg
= NULL
;
8092 g_assert (method
!= NULL
);
8093 g_assert (mono_method_signature (method
)->pinvoke
);
8096 cache
= get_cache (&method
->klass
->image
->native_wrapper_aot_cache
, mono_aligned_addr_hash
, NULL
);
8098 cache
= get_cache (&method
->klass
->image
->native_wrapper_cache
, mono_aligned_addr_hash
, NULL
);
8099 if ((res
= mono_marshal_find_in_cache (cache
, method
)))
8102 if (MONO_CLASS_IS_IMPORT (method
->klass
)) {
8104 return mono_cominterop_get_native_wrapper (method
);
8106 g_assert_not_reached ();
8110 sig
= mono_method_signature (method
);
8112 if (!(method
->iflags
& METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL
) &&
8113 (method
->flags
& METHOD_ATTRIBUTE_PINVOKE_IMPL
))
8116 if (!piinfo
->addr
) {
8118 if (method
->iflags
& METHOD_IMPL_ATTRIBUTE_NATIVE
)
8119 exc_arg
= "Method contains unsupported native code";
8121 mono_lookup_pinvoke_call (method
, &exc_class
, &exc_arg
);
8123 piinfo
->addr
= mono_lookup_internal_call (method
);
8126 /* hack - redirect certain string constructors to CreateString */
8127 if (piinfo
->addr
== ves_icall_System_String_ctor_RedirectToCreateString
) {
8128 g_assert (!pinvoke
);
8129 g_assert (method
->string_ctor
);
8130 g_assert (sig
->hasthis
);
8132 /* CreateString returns a value */
8133 csig
= signature_dup (method
->klass
->image
, sig
);
8134 csig
->ret
= &mono_defaults
.string_class
->byval_arg
;
8138 while ((res
= mono_class_get_methods (mono_defaults
.string_class
, &iter
))) {
8139 if (!strcmp ("CreateString", res
->name
) &&
8140 mono_metadata_signature_equal (csig
, mono_method_signature (res
))) {
8142 g_assert (!(res
->iflags
& METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL
));
8143 g_assert (!(res
->flags
& METHOD_ATTRIBUTE_PINVOKE_IMPL
));
8145 /* create a wrapper to preserve .ctor in stack trace */
8146 mb
= mono_mb_new (method
->klass
, method
->name
, MONO_WRAPPER_MANAGED_TO_MANAGED
);
8148 mono_mb_emit_byte (mb
, CEE_LDARG_0
);
8149 for (i
= 1; i
<= csig
->param_count
; i
++)
8150 mono_mb_emit_ldarg (mb
, i
);
8151 mono_mb_emit_managed_call (mb
, res
, NULL
);
8152 mono_mb_emit_byte (mb
, CEE_RET
);
8154 /* use native_wrapper_cache because internal calls are looked up there */
8155 res
= mono_mb_create_and_cache (cache
, method
,
8156 mb
, csig
, csig
->param_count
+ 1);
8164 /* exception will be thrown */
8165 piinfo
->addr
= NULL
;
8166 g_warning ("cannot find CreateString for .ctor");
8169 mb
= mono_mb_new (method
->klass
, method
->name
, MONO_WRAPPER_MANAGED_TO_NATIVE
);
8171 mb
->method
->save_lmf
= 1;
8174 * In AOT mode and embedding scenarios, it is possible that the icall is not
8175 * registered in the runtime doing the AOT compilation.
8177 if (!piinfo
->addr
&& !aot
) {
8178 mono_mb_emit_exception (mb
, exc_class
, exc_arg
);
8179 csig
= signature_dup (method
->klass
->image
, sig
);
8181 res
= mono_mb_create_and_cache (cache
, method
,
8182 mb
, csig
, csig
->param_count
+ 16);
8187 /* internal calls: we simply push all arguments and call the method (no conversions) */
8188 if (method
->iflags
& (METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL
| METHOD_IMPL_ATTRIBUTE_RUNTIME
)) {
8190 csig
= signature_dup_add_this (sig
, method
->klass
);
8192 csig
= signature_dup (method
->klass
->image
, sig
);
8194 /* hack - string constructors returns a value */
8195 if (method
->string_ctor
)
8196 csig
->ret
= &mono_defaults
.string_class
->byval_arg
;
8202 * Add a null check since public icalls can be called with 'call' which
8203 * does no such check.
8205 mono_mb_emit_byte (mb
, CEE_LDARG_0
);
8206 pos
= mono_mb_emit_branch (mb
, CEE_BRTRUE
);
8207 mono_mb_emit_exception (mb
, "NullReferenceException", NULL
);
8208 mono_mb_patch_branch (mb
, pos
);
8210 mono_mb_emit_byte (mb
, CEE_LDARG_0
);
8213 for (i
= 0; i
< sig
->param_count
; i
++)
8214 mono_mb_emit_ldarg (mb
, i
+ sig
->hasthis
);
8217 mono_mb_emit_byte (mb
, MONO_CUSTOM_PREFIX
);
8218 mono_mb_emit_op (mb
, CEE_MONO_ICALL_ADDR
, &piinfo
->method
);
8219 mono_mb_emit_calli (mb
, csig
);
8221 g_assert (piinfo
->addr
);
8222 mono_mb_emit_native_call (mb
, csig
, piinfo
->addr
);
8224 if (check_exceptions
)
8225 emit_thread_interrupt_checkpoint (mb
);
8226 mono_mb_emit_byte (mb
, CEE_RET
);
8228 csig
= signature_dup (method
->klass
->image
, csig
);
8230 res
= mono_mb_create_and_cache (cache
, method
,
8231 mb
, csig
, csig
->param_count
+ 16);
8238 g_assert (piinfo
->addr
);
8240 mspecs
= g_new (MonoMarshalSpec
*, sig
->param_count
+ 1);
8241 mono_method_get_marshal_info (method
, mspecs
);
8243 mono_marshal_emit_native_wrapper (mb
->method
->klass
->image
, mb
, sig
, piinfo
, mspecs
, piinfo
->addr
, aot
, check_exceptions
);
8245 csig
= signature_dup (method
->klass
->image
, sig
);
8247 res
= mono_mb_create_and_cache (cache
, method
,
8248 mb
, csig
, csig
->param_count
+ 16);
8251 for (i
= sig
->param_count
; i
>= 0; i
--)
8253 mono_metadata_free_marshal_spec (mspecs
[i
]);
8256 /* code_for (res); */
8262 * mono_marshal_get_native_func_wrapper:
8263 * @image: The image to use for memory allocation and for looking up custom marshallers.
8264 * @sig: The signature of the function
8265 * @func: The native function to wrap
8267 * Returns a wrapper method around native functions, similar to the pinvoke
8271 mono_marshal_get_native_func_wrapper (MonoImage
*image
, MonoMethodSignature
*sig
,
8272 MonoMethodPInvoke
*piinfo
, MonoMarshalSpec
**mspecs
, gpointer func
)
8274 MonoMethodSignature
*csig
;
8276 MonoMethodBuilder
*mb
;
8281 cache
= get_cache (&image
->native_wrapper_cache
, mono_aligned_addr_hash
, NULL
);
8282 if ((res
= mono_marshal_find_in_cache (cache
, func
)))
8285 name
= g_strdup_printf ("wrapper_native_%p", func
);
8286 mb
= mono_mb_new (mono_defaults
.object_class
, name
, MONO_WRAPPER_MANAGED_TO_NATIVE
);
8287 mb
->method
->save_lmf
= 1;
8289 mono_marshal_emit_native_wrapper (image
, mb
, sig
, piinfo
, mspecs
, func
, FALSE
, TRUE
);
8291 csig
= signature_dup (image
, sig
);
8293 res
= mono_mb_create_and_cache (cache
, func
,
8294 mb
, csig
, csig
->param_count
+ 16);
8297 mono_marshal_set_wrapper_info (res
, NULL
);
8299 /* code_for (res); */
8305 * mono_marshal_emit_managed_wrapper:
8307 * Emit the body of a native-to-managed wrapper. INVOKE_SIG is the signature of
8308 * the delegate which wraps the managed method to be called. For closed delegates,
8309 * it could have fewer parameters than the method it wraps.
8310 * THIS_LOC is the memory location where the target of the delegate is stored.
8313 mono_marshal_emit_managed_wrapper (MonoMethodBuilder
*mb
, MonoMethodSignature
*invoke_sig
, MonoMarshalSpec
**mspecs
, EmitMarshalContext
* m
, MonoMethod
*method
, MonoObject
** this_loc
)
8315 MonoMethodSignature
*sig
, *csig
;
8317 gboolean closed
= FALSE
;
8322 /* allocate local 0 (pointer) src_ptr */
8323 mono_mb_add_local (mb
, &mono_defaults
.int_class
->byval_arg
);
8324 /* allocate local 1 (pointer) dst_ptr */
8325 mono_mb_add_local (mb
, &mono_defaults
.int_class
->byval_arg
);
8326 /* allocate local 2 (boolean) delete_old */
8327 mono_mb_add_local (mb
, &mono_defaults
.boolean_class
->byval_arg
);
8329 if (!sig
->hasthis
&& sig
->param_count
!= invoke_sig
->param_count
) {
8330 /* Closed delegate */
8331 g_assert (sig
->param_count
== invoke_sig
->param_count
+ 1);
8333 /* Use a new signature without the first argument */
8334 sig
= mono_metadata_signature_dup (sig
);
8335 memmove (&sig
->params
[0], &sig
->params
[1], (sig
->param_count
- 1) * sizeof (MonoType
*));
8336 sig
->param_count
--;
8339 if (!MONO_TYPE_IS_VOID(sig
->ret
)) {
8340 /* allocate local 3 to store the return value */
8341 mono_mb_add_local (mb
, sig
->ret
);
8344 mono_mb_emit_icon (mb
, 0);
8345 mono_mb_emit_stloc (mb
, 2);
8347 /* we first do all conversions */
8348 tmp_locals
= alloca (sizeof (int) * sig
->param_count
);
8349 for (i
= 0; i
< sig
->param_count
; i
++) {
8350 MonoType
*t
= sig
->params
[i
];
8353 case MONO_TYPE_OBJECT
:
8354 case MONO_TYPE_CLASS
:
8355 case MONO_TYPE_VALUETYPE
:
8356 case MONO_TYPE_ARRAY
:
8357 case MONO_TYPE_SZARRAY
:
8358 case MONO_TYPE_STRING
:
8359 case MONO_TYPE_BOOLEAN
:
8360 tmp_locals
[i
] = emit_marshal (m
, i
, sig
->params
[i
], mspecs
[i
+ 1], 0, &csig
->params
[i
], MARSHAL_ACTION_MANAGED_CONV_IN
);
8369 emit_thread_interrupt_checkpoint (mb
);
8373 mono_mb_emit_ptr (mb
, this_loc
);
8374 mono_mb_emit_byte (mb
, CEE_LDIND_REF
);
8377 g_assert_not_reached ();
8379 } else if (closed
) {
8380 mono_mb_emit_ptr (mb
, this_loc
);
8381 mono_mb_emit_byte (mb
, CEE_LDIND_REF
);
8384 for (i
= 0; i
< sig
->param_count
; i
++) {
8385 MonoType
*t
= sig
->params
[i
];
8387 if (tmp_locals
[i
]) {
8389 mono_mb_emit_ldloc_addr (mb
, tmp_locals
[i
]);
8391 mono_mb_emit_ldloc (mb
, tmp_locals
[i
]);
8394 mono_mb_emit_ldarg (mb
, i
);
8397 mono_mb_emit_managed_call (mb
, method
, NULL
);
8399 if (mspecs
[0] && mspecs
[0]->native
== MONO_NATIVE_CUSTOM
) {
8400 emit_marshal (m
, 0, sig
->ret
, mspecs
[0], 0, NULL
, MARSHAL_ACTION_MANAGED_CONV_RESULT
);
8401 } else if (!sig
->ret
->byref
) {
8402 switch (sig
->ret
->type
) {
8403 case MONO_TYPE_VOID
:
8405 case MONO_TYPE_BOOLEAN
:
8408 case MONO_TYPE_CHAR
:
8420 case MONO_TYPE_OBJECT
:
8421 mono_mb_emit_stloc (mb
, 3);
8423 case MONO_TYPE_STRING
:
8424 csig
->ret
= &mono_defaults
.int_class
->byval_arg
;
8425 emit_marshal (m
, 0, sig
->ret
, mspecs
[0], 0, NULL
, MARSHAL_ACTION_MANAGED_CONV_RESULT
);
8427 case MONO_TYPE_VALUETYPE
:
8428 case MONO_TYPE_CLASS
:
8429 case MONO_TYPE_SZARRAY
:
8430 emit_marshal (m
, 0, sig
->ret
, mspecs
[0], 0, NULL
, MARSHAL_ACTION_MANAGED_CONV_RESULT
);
8433 g_warning ("return type 0x%02x unknown", sig
->ret
->type
);
8434 g_assert_not_reached ();
8437 mono_mb_emit_stloc (mb
, 3);
8440 /* Convert byref arguments back */
8441 for (i
= 0; i
< sig
->param_count
; i
++) {
8442 MonoType
*t
= sig
->params
[i
];
8443 MonoMarshalSpec
*spec
= mspecs
[i
+ 1];
8445 if (spec
&& spec
->native
== MONO_NATIVE_CUSTOM
) {
8446 emit_marshal (m
, i
, t
, mspecs
[i
+ 1], tmp_locals
[i
], NULL
, MARSHAL_ACTION_MANAGED_CONV_OUT
);
8448 else if (t
->byref
) {
8450 case MONO_TYPE_CLASS
:
8451 case MONO_TYPE_VALUETYPE
:
8452 case MONO_TYPE_OBJECT
:
8453 case MONO_TYPE_STRING
:
8454 case MONO_TYPE_BOOLEAN
:
8455 emit_marshal (m
, i
, t
, mspecs
[i
+ 1], tmp_locals
[i
], NULL
, MARSHAL_ACTION_MANAGED_CONV_OUT
);
8459 else if (invoke_sig
->params
[i
]->attrs
& PARAM_ATTRIBUTE_OUT
) {
8460 /* The [Out] information is encoded in the delegate signature */
8462 case MONO_TYPE_SZARRAY
:
8463 case MONO_TYPE_CLASS
:
8464 case MONO_TYPE_VALUETYPE
:
8465 emit_marshal (m
, i
, invoke_sig
->params
[i
], mspecs
[i
+ 1], tmp_locals
[i
], NULL
, MARSHAL_ACTION_MANAGED_CONV_OUT
);
8468 g_assert_not_reached ();
8473 if (m
->retobj_var
) {
8474 mono_mb_emit_ldloc (mb
, m
->retobj_var
);
8475 mono_mb_emit_byte (mb
, MONO_CUSTOM_PREFIX
);
8476 mono_mb_emit_op (mb
, CEE_MONO_RETOBJ
, m
->retobj_class
);
8479 if (!MONO_TYPE_IS_VOID(sig
->ret
))
8480 mono_mb_emit_ldloc (mb
, 3);
8481 mono_mb_emit_byte (mb
, CEE_RET
);
8490 mono_marshal_set_callconv_from_modopt (MonoMethod
*method
, MonoMethodSignature
*csig
)
8492 MonoMethodSignature
*sig
;
8497 * Under windows, delegates passed to native code must use the STDCALL
8498 * calling convention.
8500 csig
->call_convention
= MONO_CALL_STDCALL
;
8503 sig
= mono_method_signature (method
);
8505 /* Change default calling convention if needed */
8506 /* Why is this a modopt ? */
8507 if (sig
->ret
&& sig
->ret
->num_mods
) {
8508 for (i
= 0; i
< sig
->ret
->num_mods
; ++i
) {
8509 MonoClass
*cmod_class
= mono_class_get (method
->klass
->image
, sig
->ret
->modifiers
[i
].token
);
8510 g_assert (cmod_class
);
8511 if ((cmod_class
->image
== mono_defaults
.corlib
) && !strcmp (cmod_class
->name_space
, "System.Runtime.CompilerServices")) {
8512 if (!strcmp (cmod_class
->name
, "CallConvCdecl"))
8513 csig
->call_convention
= MONO_CALL_C
;
8514 else if (!strcmp (cmod_class
->name
, "CallConvStdcall"))
8515 csig
->call_convention
= MONO_CALL_STDCALL
;
8516 else if (!strcmp (cmod_class
->name
, "CallConvFastcall"))
8517 csig
->call_convention
= MONO_CALL_FASTCALL
;
8518 else if (!strcmp (cmod_class
->name
, "CallConvThiscall"))
8519 csig
->call_convention
= MONO_CALL_THISCALL
;
8526 * generates IL code to call managed methods from unmanaged code
8529 mono_marshal_get_managed_wrapper (MonoMethod
*method
, MonoClass
*delegate_klass
, MonoObject
**this_loc
)
8531 static MonoClass
*UnmanagedFunctionPointerAttribute
;
8532 MonoMethodSignature
*sig
, *csig
, *invoke_sig
;
8533 MonoMethodBuilder
*mb
;
8534 MonoMethod
*res
, *invoke
;
8535 MonoMarshalSpec
**mspecs
;
8536 MonoMethodPInvoke piinfo
;
8539 EmitMarshalContext m
;
8541 g_assert (method
!= NULL
);
8542 g_assert (!mono_method_signature (method
)->pinvoke
);
8545 * FIXME: Should cache the method+delegate type pair, since the same method
8546 * could be called with different delegates, thus different marshalling
8549 cache
= get_cache (&method
->klass
->image
->managed_wrapper_cache
, mono_aligned_addr_hash
, NULL
);
8550 if (!this_loc
&& (res
= mono_marshal_find_in_cache (cache
, method
)))
8553 invoke
= mono_get_delegate_invoke (delegate_klass
);
8554 invoke_sig
= mono_method_signature (invoke
);
8556 mspecs
= g_new0 (MonoMarshalSpec
*, mono_method_signature (invoke
)->param_count
+ 1);
8557 mono_method_get_marshal_info (invoke
, mspecs
);
8559 sig
= mono_method_signature (method
);
8561 mb
= mono_mb_new (method
->klass
, method
->name
, MONO_WRAPPER_NATIVE_TO_MANAGED
);
8563 /* we copy the signature, so that we can modify it */
8565 /* Need to free this later */
8566 csig
= mono_metadata_signature_dup (invoke_sig
);
8568 csig
= signature_dup (method
->klass
->image
, invoke_sig
);
8577 m
.image
= method
->klass
->image
;
8579 mono_marshal_set_callconv_from_modopt (invoke
, csig
);
8581 /* Handle the UnmanagedFunctionPointerAttribute */
8582 if (!UnmanagedFunctionPointerAttribute
)
8583 UnmanagedFunctionPointerAttribute
= mono_class_from_name (mono_defaults
.corlib
, "System.Runtime.InteropServices", "UnmanagedFunctionPointerAttribute");
8585 /* The attribute is only available in Net 2.0 */
8586 if (UnmanagedFunctionPointerAttribute
) {
8587 MonoCustomAttrInfo
*cinfo
;
8588 MonoCustomAttrEntry
*attr
;
8591 * The pinvoke attributes are stored in a real custom attribute. Obtain the
8592 * contents of the attribute without constructing it, as that might not be
8593 * possible when running in cross-compiling mode.
8595 cinfo
= mono_custom_attrs_from_class (delegate_klass
);
8598 for (i
= 0; i
< cinfo
->num_attrs
; ++i
) {
8599 if (mono_class_has_parent (cinfo
->attrs
[i
].ctor
->klass
, UnmanagedFunctionPointerAttribute
)) {
8600 attr
= &cinfo
->attrs
[i
];
8606 MonoArray
*typed_args
, *named_args
;
8607 CattrNamedArg
*arginfo
;
8611 MonoBoolean set_last_error
= 0;
8612 MonoBoolean best_fit_mapping
= 0;
8613 MonoBoolean throw_on_unmappable
= 0;
8615 mono_reflection_create_custom_attr_data_args (mono_defaults
.corlib
, attr
->ctor
, attr
->data
, attr
->data_size
, &typed_args
, &named_args
, &arginfo
);
8617 g_assert (mono_array_length (typed_args
) == 1);
8620 o
= mono_array_get (typed_args
, MonoObject
*, 0);
8621 call_conv
= *(gint32
*)mono_object_unbox (o
);
8624 for (i
= 0; i
< mono_array_length (named_args
); ++i
) {
8625 CattrNamedArg
*narg
= &arginfo
[i
];
8627 o
= mono_array_get (named_args
, MonoObject
*, i
);
8629 g_assert (narg
->field
);
8630 if (!strcmp (narg
->field
->name
, "CharSet")) {
8631 charset
= *(gint32
*)mono_object_unbox (o
);
8632 } else if (!strcmp (narg
->field
->name
, "SetLastError")) {
8633 set_last_error
= *(MonoBoolean
*)mono_object_unbox (o
);
8634 } else if (!strcmp (narg
->field
->name
, "BestFitMapping")) {
8635 best_fit_mapping
= *(MonoBoolean
*)mono_object_unbox (o
);
8636 } else if (!strcmp (narg
->field
->name
, "ThrowOnUnmappableChar")) {
8637 throw_on_unmappable
= *(MonoBoolean
*)mono_object_unbox (o
);
8639 g_assert_not_reached ();
8645 memset (&piinfo
, 0, sizeof (piinfo
));
8647 piinfo
.piflags
= (call_conv
<< 8) | (charset
? (charset
- 1) * 2 : 1) | set_last_error
;
8649 csig
->call_convention
= call_conv
- 1;
8652 if (cinfo
&& !cinfo
->cached
)
8653 mono_custom_attrs_free (cinfo
);
8656 mono_marshal_emit_managed_wrapper (mb
, invoke_sig
, mspecs
, &m
, method
, this_loc
);
8659 res
= mono_mb_create_and_cache (cache
, method
,
8660 mb
, csig
, sig
->param_count
+ 16);
8663 res
= mono_mb_create_method (mb
, csig
, sig
->param_count
+ 16);
8667 for (i
= mono_method_signature (invoke
)->param_count
; i
>= 0; i
--)
8669 mono_metadata_free_marshal_spec (mspecs
[i
]);
8672 /* code_for (res); */
8678 mono_marshal_get_vtfixup_ftnptr (MonoImage
*image
, guint32 token
, guint16 type
)
8681 MonoMethodSignature
*sig
;
8682 MonoMethodBuilder
*mb
;
8687 method
= mono_get_method (image
, token
, NULL
);
8690 if (type
& (VTFIXUP_TYPE_FROM_UNMANAGED
| VTFIXUP_TYPE_FROM_UNMANAGED_RETAIN_APPDOMAIN
)) {
8691 MonoMethodSignature
*csig
;
8692 MonoMarshalSpec
**mspecs
;
8693 EmitMarshalContext m
;
8695 sig
= mono_method_signature (method
);
8696 g_assert (!sig
->hasthis
);
8698 mspecs
= g_new0 (MonoMarshalSpec
*, sig
->param_count
+ 1);
8699 mono_method_get_marshal_info (method
, mspecs
);
8701 mb
= mono_mb_new (method
->klass
, method
->name
, MONO_WRAPPER_NATIVE_TO_MANAGED
);
8702 csig
= signature_dup (image
, sig
);
8713 mono_marshal_set_callconv_from_modopt (method
, csig
);
8715 /* FIXME: Implement VTFIXUP_TYPE_FROM_UNMANAGED_RETAIN_APPDOMAIN. */
8717 mono_marshal_emit_managed_wrapper (mb
, sig
, mspecs
, &m
, method
, NULL
);
8720 method
= mono_mb_create_method (mb
, csig
, sig
->param_count
+ 16);
8723 for (i
= sig
->param_count
; i
>= 0; i
--)
8725 mono_metadata_free_marshal_spec (mspecs
[i
]);
8728 return mono_compile_method (method
);
8731 sig
= mono_method_signature (method
);
8732 mb
= mono_mb_new (method
->klass
, method
->name
, MONO_WRAPPER_MANAGED_TO_MANAGED
);
8734 param_count
= sig
->param_count
+ sig
->hasthis
;
8735 for (i
= 0; i
< param_count
; i
++)
8736 mono_mb_emit_ldarg (mb
, i
);
8738 if (type
& VTFIXUP_TYPE_CALL_MOST_DERIVED
)
8739 mono_mb_emit_op (mb
, CEE_CALLVIRT
, method
);
8741 mono_mb_emit_op (mb
, CEE_CALL
, method
);
8742 mono_mb_emit_byte (mb
, CEE_RET
);
8745 method
= mono_mb_create_method (mb
, sig
, param_count
);
8748 return mono_compile_method (method
);
8751 static MonoReflectionType
*
8752 type_from_handle (MonoType
*handle
)
8754 MonoDomain
*domain
= mono_domain_get ();
8755 MonoClass
*klass
= mono_class_from_mono_type (handle
);
8757 MONO_ARCH_SAVE_REGS
;
8759 mono_class_init (klass
);
8760 return mono_type_get_object (domain
, handle
);
8764 * mono_marshal_get_isinst:
8765 * @klass: the type of the field
8767 * This method generates a function which can be used to check if an object is
8768 * an instance of the given type, icluding the case where the object is a proxy.
8769 * The generated function has the following signature:
8770 * MonoObject* __isinst_wrapper_ (MonoObject *obj)
8773 mono_marshal_get_isinst (MonoClass
*klass
)
8775 static MonoMethodSignature
*isint_sig
= NULL
;
8778 int pos_was_ok
, pos_failed
, pos_end
, pos_end2
;
8780 MonoMethodBuilder
*mb
;
8782 cache
= get_cache (&klass
->image
->isinst_cache
, mono_aligned_addr_hash
, NULL
);
8783 if ((res
= mono_marshal_find_in_cache (cache
, klass
)))
8787 isint_sig
= mono_metadata_signature_alloc (mono_defaults
.corlib
, 1);
8788 isint_sig
->params
[0] = &mono_defaults
.object_class
->byval_arg
;
8789 isint_sig
->ret
= &mono_defaults
.object_class
->byval_arg
;
8790 isint_sig
->pinvoke
= 0;
8793 name
= g_strdup_printf ("__isinst_wrapper_%s", klass
->name
);
8794 mb
= mono_mb_new (mono_defaults
.object_class
, name
, MONO_WRAPPER_ISINST
);
8797 mb
->method
->save_lmf
= 1;
8799 /* check if the object is a proxy that needs special cast */
8800 mono_mb_emit_ldarg (mb
, 0);
8801 mono_mb_emit_byte (mb
, MONO_CUSTOM_PREFIX
);
8802 mono_mb_emit_op (mb
, CEE_MONO_CISINST
, klass
);
8804 /* The result of MONO_ISINST can be:
8805 0) the type check succeeded
8806 1) the type check did not succeed
8807 2) a CanCastTo call is needed */
8809 mono_mb_emit_byte (mb
, CEE_DUP
);
8810 pos_was_ok
= mono_mb_emit_branch (mb
, CEE_BRFALSE
);
8812 mono_mb_emit_byte (mb
, CEE_LDC_I4_2
);
8813 pos_failed
= mono_mb_emit_branch (mb
, CEE_BNE_UN
);
8815 /* get the real proxy from the transparent proxy*/
8817 mono_mb_emit_ldarg (mb
, 0);
8818 mono_mb_emit_managed_call (mb
, mono_marshal_get_proxy_cancast (klass
), NULL
);
8819 pos_end
= mono_mb_emit_branch (mb
, CEE_BR
);
8823 mono_mb_patch_branch (mb
, pos_failed
);
8824 mono_mb_emit_byte (mb
, CEE_LDNULL
);
8825 pos_end2
= mono_mb_emit_branch (mb
, CEE_BR
);
8829 mono_mb_patch_branch (mb
, pos_was_ok
);
8830 mono_mb_emit_byte (mb
, CEE_POP
);
8831 mono_mb_emit_ldarg (mb
, 0);
8835 mono_mb_patch_branch (mb
, pos_end
);
8836 mono_mb_patch_branch (mb
, pos_end2
);
8837 mono_mb_emit_byte (mb
, CEE_RET
);
8839 res
= mono_mb_create_and_cache (cache
, klass
, mb
, isint_sig
, isint_sig
->param_count
+ 16);
8846 * mono_marshal_get_castclass:
8847 * @klass: the type of the field
8849 * This method generates a function which can be used to cast an object to
8850 * an instance of the given type, icluding the case where the object is a proxy.
8851 * The generated function has the following signature:
8852 * MonoObject* __castclass_wrapper_ (MonoObject *obj)
8855 mono_marshal_get_castclass (MonoClass
*klass
)
8857 static MonoMethodSignature
*castclass_sig
= NULL
;
8860 int pos_was_ok
, pos_was_ok2
;
8862 MonoMethodBuilder
*mb
;
8864 cache
= get_cache (&klass
->image
->castclass_cache
, mono_aligned_addr_hash
, NULL
);
8865 if ((res
= mono_marshal_find_in_cache (cache
, klass
)))
8868 if (!castclass_sig
) {
8869 castclass_sig
= mono_metadata_signature_alloc (mono_defaults
.corlib
, 1);
8870 castclass_sig
->params
[0] = &mono_defaults
.object_class
->byval_arg
;
8871 castclass_sig
->ret
= &mono_defaults
.object_class
->byval_arg
;
8872 castclass_sig
->pinvoke
= 0;
8875 name
= g_strdup_printf ("__castclass_wrapper_%s", klass
->name
);
8876 mb
= mono_mb_new (mono_defaults
.object_class
, name
, MONO_WRAPPER_CASTCLASS
);
8879 mb
->method
->save_lmf
= 1;
8881 /* check if the object is a proxy that needs special cast */
8882 mono_mb_emit_ldarg (mb
, 0);
8883 mono_mb_emit_byte (mb
, MONO_CUSTOM_PREFIX
);
8884 mono_mb_emit_op (mb
, CEE_MONO_CCASTCLASS
, klass
);
8886 /* The result of MONO_ISINST can be:
8887 0) the cast is valid
8888 1) cast of unknown proxy type
8889 or an exception if the cast is is invalid
8892 pos_was_ok
= mono_mb_emit_branch (mb
, CEE_BRFALSE
);
8894 /* get the real proxy from the transparent proxy*/
8896 mono_mb_emit_ldarg (mb
, 0);
8897 mono_mb_emit_managed_call (mb
, mono_marshal_get_proxy_cancast (klass
), NULL
);
8898 pos_was_ok2
= mono_mb_emit_branch (mb
, CEE_BRTRUE
);
8901 mono_mb_emit_exception (mb
, "InvalidCastException", NULL
);
8904 mono_mb_patch_branch (mb
, pos_was_ok
);
8905 mono_mb_patch_branch (mb
, pos_was_ok2
);
8906 mono_mb_emit_ldarg (mb
, 0);
8909 mono_mb_emit_byte (mb
, CEE_RET
);
8911 res
= mono_mb_create_and_cache (cache
, klass
, mb
, castclass_sig
, castclass_sig
->param_count
+ 16);
8918 mono_marshal_get_proxy_cancast (MonoClass
*klass
)
8920 static MonoMethodSignature
*isint_sig
= NULL
;
8923 int pos_failed
, pos_end
;
8925 MonoMethod
*can_cast_to
;
8926 MonoMethodDesc
*desc
;
8927 MonoMethodBuilder
*mb
;
8929 cache
= get_cache (&klass
->image
->proxy_isinst_cache
, mono_aligned_addr_hash
, NULL
);
8930 if ((res
= mono_marshal_find_in_cache (cache
, klass
)))
8934 isint_sig
= mono_metadata_signature_alloc (mono_defaults
.corlib
, 1);
8935 isint_sig
->params
[0] = &mono_defaults
.object_class
->byval_arg
;
8936 isint_sig
->ret
= &mono_defaults
.object_class
->byval_arg
;
8937 isint_sig
->pinvoke
= 0;
8940 name
= g_strdup_printf ("__proxy_isinst_wrapper_%s", klass
->name
);
8941 mb
= mono_mb_new (mono_defaults
.object_class
, name
, MONO_WRAPPER_PROXY_ISINST
);
8944 mb
->method
->save_lmf
= 1;
8946 /* get the real proxy from the transparent proxy*/
8947 mono_mb_emit_ldarg (mb
, 0);
8948 mono_mb_emit_ldflda (mb
, G_STRUCT_OFFSET (MonoTransparentProxy
, rp
));
8949 mono_mb_emit_byte (mb
, CEE_LDIND_REF
);
8951 /* get the reflection type from the type handle */
8952 mono_mb_emit_ptr (mb
, &klass
->byval_arg
);
8953 mono_mb_emit_icall (mb
, type_from_handle
);
8955 mono_mb_emit_ldarg (mb
, 0);
8957 /* make the call to CanCastTo (type, ob) */
8958 desc
= mono_method_desc_new ("IRemotingTypeInfo:CanCastTo", FALSE
);
8959 can_cast_to
= mono_method_desc_search_in_class (desc
, mono_defaults
.iremotingtypeinfo_class
);
8960 g_assert (can_cast_to
);
8961 mono_method_desc_free (desc
);
8962 mono_mb_emit_op (mb
, CEE_CALLVIRT
, can_cast_to
);
8964 pos_failed
= mono_mb_emit_branch (mb
, CEE_BRFALSE
);
8966 /* Upgrade the proxy vtable by calling: mono_upgrade_remote_class_wrapper (type, ob)*/
8967 mono_mb_emit_ptr (mb
, &klass
->byval_arg
);
8968 mono_mb_emit_icall (mb
, type_from_handle
);
8969 mono_mb_emit_ldarg (mb
, 0);
8971 mono_mb_emit_icall (mb
, mono_upgrade_remote_class_wrapper
);
8972 emit_thread_interrupt_checkpoint (mb
);
8974 mono_mb_emit_ldarg (mb
, 0);
8975 pos_end
= mono_mb_emit_branch (mb
, CEE_BR
);
8979 mono_mb_patch_branch (mb
, pos_failed
);
8980 mono_mb_emit_byte (mb
, CEE_LDNULL
);
8984 mono_mb_patch_branch (mb
, pos_end
);
8985 mono_mb_emit_byte (mb
, CEE_RET
);
8987 res
= mono_mb_create_and_cache (cache
, klass
, mb
, isint_sig
, isint_sig
->param_count
+ 16);
8994 mono_upgrade_remote_class_wrapper (MonoReflectionType
*rtype
, MonoTransparentProxy
*tproxy
)
8997 MonoDomain
*domain
= ((MonoObject
*)tproxy
)->vtable
->domain
;
8998 klass
= mono_class_from_mono_type (rtype
->type
);
8999 mono_upgrade_remote_class (domain
, (MonoObject
*)tproxy
, klass
);
9003 * mono_marshal_get_struct_to_ptr:
9006 * generates IL code for StructureToPtr (object structure, IntPtr ptr, bool fDeleteOld)
9009 mono_marshal_get_struct_to_ptr (MonoClass
*klass
)
9011 MonoMethodBuilder
*mb
;
9012 static MonoMethod
*stoptr
= NULL
;
9015 g_assert (klass
!= NULL
);
9017 mono_marshal_load_type_info (klass
);
9019 if (klass
->marshal_info
->str_to_ptr
)
9020 return klass
->marshal_info
->str_to_ptr
;
9023 stoptr
= mono_class_get_method_from_name (mono_defaults
.marshal_class
, "StructureToPtr", 3);
9026 mb
= mono_mb_new (klass
, stoptr
->name
, MONO_WRAPPER_UNKNOWN
);
9028 if (klass
->blittable
) {
9029 mono_mb_emit_byte (mb
, CEE_LDARG_1
);
9030 mono_mb_emit_byte (mb
, CEE_LDARG_0
);
9031 mono_mb_emit_ldflda (mb
, sizeof (MonoObject
));
9032 mono_mb_emit_icon (mb
, mono_class_value_size (klass
, NULL
));
9033 mono_mb_emit_byte (mb
, CEE_PREFIX1
);
9034 mono_mb_emit_byte (mb
, CEE_CPBLK
);
9037 /* allocate local 0 (pointer) src_ptr */
9038 mono_mb_add_local (mb
, &mono_defaults
.int_class
->byval_arg
);
9039 /* allocate local 1 (pointer) dst_ptr */
9040 mono_mb_add_local (mb
, &mono_defaults
.int_class
->byval_arg
);
9041 /* allocate local 2 (boolean) delete_old */
9042 mono_mb_add_local (mb
, &mono_defaults
.boolean_class
->byval_arg
);
9043 mono_mb_emit_byte (mb
, CEE_LDARG_2
);
9044 mono_mb_emit_stloc (mb
, 2);
9046 /* initialize src_ptr to point to the start of object data */
9047 mono_mb_emit_byte (mb
, CEE_LDARG_0
);
9048 mono_mb_emit_ldflda (mb
, sizeof (MonoObject
));
9049 mono_mb_emit_stloc (mb
, 0);
9051 /* initialize dst_ptr */
9052 mono_mb_emit_byte (mb
, CEE_LDARG_1
);
9053 mono_mb_emit_stloc (mb
, 1);
9055 emit_struct_conv (mb
, klass
, FALSE
);
9058 mono_mb_emit_byte (mb
, CEE_RET
);
9060 res
= mono_mb_create_method (mb
, mono_signature_no_pinvoke (stoptr
), 0);
9063 klass
->marshal_info
->str_to_ptr
= res
;
9068 * mono_marshal_get_ptr_to_struct:
9071 * generates IL code for PtrToStructure (IntPtr src, object structure)
9074 mono_marshal_get_ptr_to_struct (MonoClass
*klass
)
9076 MonoMethodBuilder
*mb
;
9077 static MonoMethodSignature
*ptostr
= NULL
;
9080 g_assert (klass
!= NULL
);
9082 mono_marshal_load_type_info (klass
);
9084 if (klass
->marshal_info
->ptr_to_str
)
9085 return klass
->marshal_info
->ptr_to_str
;
9088 MonoMethodSignature
*sig
;
9090 /* Create the signature corresponding to
9091 static void PtrToStructure (IntPtr ptr, object structure);
9092 defined in class/corlib/System.Runtime.InteropServices/Marshal.cs */
9093 sig
= mono_create_icall_signature ("void ptr object");
9094 sig
= signature_dup (mono_defaults
.corlib
, sig
);
9096 mono_memory_barrier ();
9100 mb
= mono_mb_new (klass
, "PtrToStructure", MONO_WRAPPER_UNKNOWN
);
9102 if (klass
->blittable
) {
9103 mono_mb_emit_byte (mb
, CEE_LDARG_1
);
9104 mono_mb_emit_ldflda (mb
, sizeof (MonoObject
));
9105 mono_mb_emit_byte (mb
, CEE_LDARG_0
);
9106 mono_mb_emit_icon (mb
, mono_class_value_size (klass
, NULL
));
9107 mono_mb_emit_byte (mb
, CEE_PREFIX1
);
9108 mono_mb_emit_byte (mb
, CEE_CPBLK
);
9111 /* allocate local 0 (pointer) src_ptr */
9112 mono_mb_add_local (mb
, &mono_defaults
.int_class
->byval_arg
);
9113 /* allocate local 1 (pointer) dst_ptr */
9114 mono_mb_add_local (mb
, &klass
->this_arg
);
9116 /* initialize src_ptr to point to the start of object data */
9117 mono_mb_emit_byte (mb
, CEE_LDARG_0
);
9118 mono_mb_emit_stloc (mb
, 0);
9120 /* initialize dst_ptr */
9121 mono_mb_emit_byte (mb
, CEE_LDARG_1
);
9122 mono_mb_emit_op (mb
, CEE_UNBOX
, klass
);
9123 mono_mb_emit_stloc (mb
, 1);
9125 emit_struct_conv (mb
, klass
, TRUE
);
9128 mono_mb_emit_byte (mb
, CEE_RET
);
9130 res
= mono_mb_create_method (mb
, ptostr
, 0);
9133 klass
->marshal_info
->ptr_to_str
= res
;
9138 * generates IL code for the synchronized wrapper: the generated method
9139 * calls METHOD while locking 'this' or the parent type.
9142 mono_marshal_get_synchronized_wrapper (MonoMethod
*method
)
9144 static MonoMethod
*enter_method
, *exit_method
, *gettypefromhandle_method
;
9145 MonoMethodSignature
*sig
;
9146 MonoExceptionClause
*clause
;
9147 MonoMethodBuilder
*mb
;
9150 int i
, pos
, this_local
, ret_local
= 0;
9154 if (method
->wrapper_type
== MONO_WRAPPER_SYNCHRONIZED
)
9157 cache
= get_cache (&method
->klass
->image
->synchronized_cache
, mono_aligned_addr_hash
, NULL
);
9158 if ((res
= mono_marshal_find_in_cache (cache
, method
)))
9161 sig
= signature_dup (method
->klass
->image
, mono_method_signature (method
));
9164 mb
= mono_mb_new (method
->klass
, method
->name
, MONO_WRAPPER_SYNCHRONIZED
);
9167 if (!MONO_TYPE_IS_VOID (sig
->ret
))
9168 ret_local
= mono_mb_add_local (mb
, sig
->ret
);
9170 if (method
->klass
->valuetype
&& !(method
->flags
& MONO_METHOD_ATTR_STATIC
)) {
9171 mono_class_set_failure (method
->klass
, MONO_EXCEPTION_TYPE_LOAD
, NULL
);
9172 /* This will throw the type load exception when the wrapper is compiled */
9173 mono_mb_emit_byte (mb
, CEE_LDNULL
);
9174 mono_mb_emit_op (mb
, CEE_ISINST
, method
->klass
);
9175 mono_mb_emit_byte (mb
, CEE_POP
);
9177 if (!MONO_TYPE_IS_VOID (sig
->ret
))
9178 mono_mb_emit_ldloc (mb
, ret_local
);
9179 mono_mb_emit_byte (mb
, CEE_RET
);
9181 res
= mono_mb_create_and_cache (cache
, method
,
9182 mb
, sig
, sig
->param_count
+ 16);
9189 this_local
= mono_mb_add_local (mb
, &mono_defaults
.object_class
->byval_arg
);
9191 clause
= mono_image_alloc0 (method
->klass
->image
, sizeof (MonoExceptionClause
));
9192 clause
->flags
= MONO_EXCEPTION_CLAUSE_FINALLY
;
9194 mono_loader_lock ();
9196 if (!enter_method
) {
9197 MonoMethodDesc
*desc
;
9199 desc
= mono_method_desc_new ("Monitor:Enter", FALSE
);
9200 enter_method
= mono_method_desc_search_in_class (desc
, mono_defaults
.monitor_class
);
9201 g_assert (enter_method
);
9202 mono_method_desc_free (desc
);
9204 desc
= mono_method_desc_new ("Monitor:Exit", FALSE
);
9205 exit_method
= mono_method_desc_search_in_class (desc
, mono_defaults
.monitor_class
);
9206 g_assert (exit_method
);
9207 mono_method_desc_free (desc
);
9209 desc
= mono_method_desc_new ("Type:GetTypeFromHandle", FALSE
);
9210 gettypefromhandle_method
= mono_method_desc_search_in_class (desc
, mono_defaults
.monotype_class
->parent
);
9211 g_assert (gettypefromhandle_method
);
9212 mono_method_desc_free (desc
);
9215 mono_loader_unlock ();
9217 /* Push this or the type object */
9218 if (method
->flags
& METHOD_ATTRIBUTE_STATIC
) {
9219 /* We have special handling for this in the JIT */
9220 int index
= mono_mb_add_data (mb
, method
->klass
);
9221 mono_mb_add_data (mb
, mono_defaults
.typehandle_class
);
9222 mono_mb_emit_byte (mb
, CEE_LDTOKEN
);
9223 mono_mb_emit_i4 (mb
, index
);
9225 mono_mb_emit_managed_call (mb
, gettypefromhandle_method
, NULL
);
9228 mono_mb_emit_ldarg (mb
, 0);
9229 mono_mb_emit_stloc (mb
, this_local
);
9231 /* Call Monitor::Enter() */
9232 mono_mb_emit_ldloc (mb
, this_local
);
9233 mono_mb_emit_managed_call (mb
, enter_method
, NULL
);
9235 clause
->try_offset
= mono_mb_get_label (mb
);
9237 /* Call the method */
9239 mono_mb_emit_ldarg (mb
, 0);
9240 for (i
= 0; i
< sig
->param_count
; i
++)
9241 mono_mb_emit_ldarg (mb
, i
+ (sig
->hasthis
== TRUE
));
9243 mono_mb_emit_managed_call (mb
, method
, NULL
);
9245 if (!MONO_TYPE_IS_VOID (sig
->ret
))
9246 mono_mb_emit_stloc (mb
, ret_local
);
9248 pos
= mono_mb_emit_branch (mb
, CEE_LEAVE
);
9250 clause
->try_len
= mono_mb_get_pos (mb
) - clause
->try_offset
;
9251 clause
->handler_offset
= mono_mb_get_label (mb
);
9253 /* Call Monitor::Exit() */
9254 mono_mb_emit_ldloc (mb
, this_local
);
9255 mono_mb_emit_managed_call (mb
, exit_method
, NULL
);
9256 mono_mb_emit_byte (mb
, CEE_ENDFINALLY
);
9258 clause
->handler_len
= mono_mb_get_pos (mb
) - clause
->handler_offset
;
9260 mono_mb_patch_branch (mb
, pos
);
9261 if (!MONO_TYPE_IS_VOID (sig
->ret
))
9262 mono_mb_emit_ldloc (mb
, ret_local
);
9263 mono_mb_emit_byte (mb
, CEE_RET
);
9265 mono_mb_set_clauses (mb
, 1, clause
);
9267 res
= mono_mb_create_and_cache (cache
, method
,
9268 mb
, sig
, sig
->param_count
+ 16);
9276 * the returned method calls 'method' unboxing the this argument
9279 mono_marshal_get_unbox_wrapper (MonoMethod
*method
)
9281 MonoMethodSignature
*sig
= mono_method_signature (method
);
9283 MonoMethodBuilder
*mb
;
9287 cache
= get_cache (&method
->klass
->image
->unbox_wrapper_cache
, mono_aligned_addr_hash
, NULL
);
9288 if ((res
= mono_marshal_find_in_cache (cache
, method
)))
9291 mb
= mono_mb_new (method
->klass
, method
->name
, MONO_WRAPPER_UNBOX
);
9293 g_assert (sig
->hasthis
);
9295 mono_mb_emit_ldarg (mb
, 0);
9296 mono_mb_emit_icon (mb
, sizeof (MonoObject
));
9297 mono_mb_emit_byte (mb
, CEE_ADD
);
9298 for (i
= 0; i
< sig
->param_count
; ++i
)
9299 mono_mb_emit_ldarg (mb
, i
+ 1);
9300 mono_mb_emit_managed_call (mb
, method
, NULL
);
9301 mono_mb_emit_byte (mb
, CEE_RET
);
9303 res
= mono_mb_create_and_cache (cache
, method
,
9304 mb
, sig
, sig
->param_count
+ 16);
9307 /* code_for (res); */
9313 mono_marshal_get_stelemref ()
9315 static MonoMethod
* ret
= NULL
;
9316 MonoMethodSignature
*sig
;
9317 MonoMethodBuilder
*mb
;
9319 guint32 b1
, b2
, b3
, b4
;
9322 int array_slot_addr
;
9327 mb
= mono_mb_new (mono_defaults
.object_class
, "stelemref", MONO_WRAPPER_STELEMREF
);
9330 sig
= mono_metadata_signature_alloc (mono_defaults
.corlib
, 3);
9332 /* void stelemref (void* array, int idx, void* value) */
9333 sig
->ret
= &mono_defaults
.void_class
->byval_arg
;
9334 sig
->params
[0] = &mono_defaults
.object_class
->byval_arg
;
9335 sig
->params
[1] = &mono_defaults
.int_class
->byval_arg
; /* this is a natural sized int */
9336 sig
->params
[2] = &mono_defaults
.object_class
->byval_arg
;
9338 aklass
= mono_mb_add_local (mb
, &mono_defaults
.int_class
->byval_arg
);
9339 vklass
= mono_mb_add_local (mb
, &mono_defaults
.int_class
->byval_arg
);
9340 array_slot_addr
= mono_mb_add_local (mb
, &mono_defaults
.object_class
->this_arg
);
9344 <ldelema (bound check)>
9348 aklass = array->vtable->klass->element_class;
9349 vklass = value->vtable->klass;
9351 if (vklass->idepth < aklass->idepth)
9354 if (vklass->supertypes [aklass->idepth - 1] != aklass)
9358 *array_slot_addr = value;
9362 if (mono_object_isinst (value, aklass))
9365 throw new ArrayTypeMismatchException ();
9368 /* ldelema (implicit bound check) */
9369 mono_mb_emit_ldarg (mb
, 0);
9370 mono_mb_emit_ldarg (mb
, 1);
9371 mono_mb_emit_op (mb
, CEE_LDELEMA
, mono_defaults
.object_class
);
9372 mono_mb_emit_stloc (mb
, array_slot_addr
);
9374 /* if (!value) goto do_store */
9375 mono_mb_emit_ldarg (mb
, 2);
9376 b1
= mono_mb_emit_branch (mb
, CEE_BRFALSE
);
9378 /* aklass = array->vtable->klass->element_class */
9379 mono_mb_emit_ldarg (mb
, 0);
9380 mono_mb_emit_ldflda (mb
, G_STRUCT_OFFSET (MonoObject
, vtable
));
9381 mono_mb_emit_byte (mb
, CEE_LDIND_I
);
9382 mono_mb_emit_ldflda (mb
, G_STRUCT_OFFSET (MonoVTable
, klass
));
9383 mono_mb_emit_byte (mb
, CEE_LDIND_I
);
9384 mono_mb_emit_ldflda (mb
, G_STRUCT_OFFSET (MonoClass
, element_class
));
9385 mono_mb_emit_byte (mb
, CEE_LDIND_I
);
9386 mono_mb_emit_stloc (mb
, aklass
);
9388 /* vklass = value->vtable->klass */
9389 mono_mb_emit_ldarg (mb
, 2);
9390 mono_mb_emit_ldflda (mb
, G_STRUCT_OFFSET (MonoObject
, vtable
));
9391 mono_mb_emit_byte (mb
, CEE_LDIND_I
);
9392 mono_mb_emit_ldflda (mb
, G_STRUCT_OFFSET (MonoVTable
, klass
));
9393 mono_mb_emit_byte (mb
, CEE_LDIND_I
);
9394 mono_mb_emit_stloc (mb
, vklass
);
9396 /* if (vklass->idepth < aklass->idepth) goto failue */
9397 mono_mb_emit_ldloc (mb
, vklass
);
9398 mono_mb_emit_ldflda (mb
, G_STRUCT_OFFSET (MonoClass
, idepth
));
9399 mono_mb_emit_byte (mb
, CEE_LDIND_U2
);
9401 mono_mb_emit_ldloc (mb
, aklass
);
9402 mono_mb_emit_ldflda (mb
, G_STRUCT_OFFSET (MonoClass
, idepth
));
9403 mono_mb_emit_byte (mb
, CEE_LDIND_U2
);
9405 b2
= mono_mb_emit_branch (mb
, CEE_BLT_UN
);
9407 /* if (vklass->supertypes [aklass->idepth - 1] != aklass) goto failure */
9408 mono_mb_emit_ldloc (mb
, vklass
);
9409 mono_mb_emit_ldflda (mb
, G_STRUCT_OFFSET (MonoClass
, supertypes
));
9410 mono_mb_emit_byte (mb
, CEE_LDIND_I
);
9412 mono_mb_emit_ldloc (mb
, aklass
);
9413 mono_mb_emit_ldflda (mb
, G_STRUCT_OFFSET (MonoClass
, idepth
));
9414 mono_mb_emit_byte (mb
, CEE_LDIND_U2
);
9415 mono_mb_emit_icon (mb
, 1);
9416 mono_mb_emit_byte (mb
, CEE_SUB
);
9417 mono_mb_emit_icon (mb
, sizeof (void*));
9418 mono_mb_emit_byte (mb
, CEE_MUL
);
9419 mono_mb_emit_byte (mb
, CEE_ADD
);
9420 mono_mb_emit_byte (mb
, CEE_LDIND_I
);
9422 mono_mb_emit_ldloc (mb
, aklass
);
9424 b3
= mono_mb_emit_branch (mb
, CEE_BNE_UN
);
9426 copy_pos
= mono_mb_get_label (mb
);
9428 mono_mb_patch_branch (mb
, b1
);
9429 mono_mb_emit_ldloc (mb
, array_slot_addr
);
9430 mono_mb_emit_ldarg (mb
, 2);
9431 mono_mb_emit_byte (mb
, CEE_STIND_REF
);
9433 mono_mb_emit_byte (mb
, CEE_RET
);
9436 mono_mb_patch_branch (mb
, b2
);
9437 mono_mb_patch_branch (mb
, b3
);
9439 mono_mb_emit_ldarg (mb
, 2);
9440 mono_mb_emit_ldloc (mb
, aklass
);
9441 mono_mb_emit_icall (mb
, mono_object_isinst
);
9443 b4
= mono_mb_emit_branch (mb
, CEE_BRTRUE
);
9444 mono_mb_patch_addr (mb
, b4
, copy_pos
- (b4
+ 4));
9445 mono_mb_emit_exception (mb
, "ArrayTypeMismatchException", NULL
);
9447 mono_mb_emit_byte (mb
, CEE_RET
);
9448 ret
= mono_mb_create_method (mb
, sig
, 4);
9459 /* LOCKING: vars accessed under the marshal lock */
9460 static ArrayElemAddr
*elem_addr_cache
= NULL
;
9461 static int elem_addr_cache_size
= 0;
9462 static int elem_addr_cache_next
= 0;
9465 * mono_marshal_get_array_address:
9466 * @rank: rank of the array type
9467 * @elem_size: size in bytes of an element of an array.
9469 * Returns a MonoMethd that implements the code to get the address
9470 * of an element in a multi-dimenasional array of @rank dimensions.
9471 * The returned method takes an array as the first argument and then
9472 * @rank indexes for the @rank dimensions.
9475 mono_marshal_get_array_address (int rank
, int elem_size
)
9478 MonoMethodBuilder
*mb
;
9479 MonoMethodSignature
*sig
;
9480 int i
, bounds
, ind
, realidx
;
9481 int branch_pos
, *branch_positions
;
9485 mono_marshal_lock ();
9486 for (i
= 0; i
< elem_addr_cache_next
; ++i
) {
9487 if (elem_addr_cache
[i
].rank
== rank
&& elem_addr_cache
[i
].elem_size
== elem_size
) {
9488 ret
= elem_addr_cache
[i
].method
;
9492 mono_marshal_unlock ();
9496 branch_positions
= g_new0 (int, rank
);
9498 sig
= mono_metadata_signature_alloc (mono_defaults
.corlib
, 1 + rank
);
9500 /* void* address (void* array, int idx0, int idx1, int idx2, ...) */
9501 sig
->ret
= &mono_defaults
.int_class
->byval_arg
;
9502 sig
->params
[0] = &mono_defaults
.object_class
->byval_arg
;
9503 for (i
= 0; i
< rank
; ++i
) {
9504 sig
->params
[i
+ 1] = &mono_defaults
.int32_class
->byval_arg
;
9507 mb
= mono_mb_new (mono_defaults
.object_class
, "ElementAddr", MONO_WRAPPER_MANAGED_TO_MANAGED
);
9509 bounds
= mono_mb_add_local (mb
, &mono_defaults
.int_class
->byval_arg
);
9510 ind
= mono_mb_add_local (mb
, &mono_defaults
.int32_class
->byval_arg
);
9511 realidx
= mono_mb_add_local (mb
, &mono_defaults
.int32_class
->byval_arg
);
9513 /* bounds = array->bounds; */
9514 mono_mb_emit_ldarg (mb
, 0);
9515 mono_mb_emit_ldflda (mb
, G_STRUCT_OFFSET (MonoArray
, bounds
));
9516 mono_mb_emit_byte (mb
, CEE_LDIND_I
);
9517 mono_mb_emit_stloc (mb
, bounds
);
9519 /* ind is the overall element index, realidx is the partial index in a single dimension */
9520 /* ind = idx0 - bounds [0].lower_bound */
9521 mono_mb_emit_ldarg (mb
, 1);
9522 mono_mb_emit_ldloc (mb
, bounds
);
9523 mono_mb_emit_icon (mb
, G_STRUCT_OFFSET (MonoArrayBounds
, lower_bound
));
9524 mono_mb_emit_byte (mb
, CEE_ADD
);
9525 mono_mb_emit_byte (mb
, CEE_LDIND_I4
);
9526 mono_mb_emit_byte (mb
, CEE_SUB
);
9527 mono_mb_emit_stloc (mb
, ind
);
9528 /* if (ind >= bounds [0].length) goto exeception; */
9529 mono_mb_emit_ldloc (mb
, ind
);
9530 mono_mb_emit_ldloc (mb
, bounds
);
9531 mono_mb_emit_icon (mb
, G_STRUCT_OFFSET (MonoArrayBounds
, length
));
9532 mono_mb_emit_byte (mb
, CEE_ADD
);
9533 mono_mb_emit_byte (mb
, CEE_LDIND_I4
);
9534 /* note that we use unsigned comparison */
9535 branch_pos
= mono_mb_emit_branch (mb
, CEE_BGE_UN
);
9537 /* For large ranks (> 4?) use a loop n IL later to reduce code size.
9538 * We could also decide to ignore the passed elem_size and get it
9539 * from the array object, to reduce the number of methods we generate:
9540 * the additional cost is 3 memory loads and a non-immediate mul.
9542 for (i
= 1; i
< rank
; ++i
) {
9543 /* realidx = idxi - bounds [i].lower_bound */
9544 mono_mb_emit_ldarg (mb
, 1 + i
);
9545 mono_mb_emit_ldloc (mb
, bounds
);
9546 mono_mb_emit_icon (mb
, (i
* sizeof (MonoArrayBounds
)) + G_STRUCT_OFFSET (MonoArrayBounds
, lower_bound
));
9547 mono_mb_emit_byte (mb
, CEE_ADD
);
9548 mono_mb_emit_byte (mb
, CEE_LDIND_I4
);
9549 mono_mb_emit_byte (mb
, CEE_SUB
);
9550 mono_mb_emit_stloc (mb
, realidx
);
9551 /* if (realidx >= bounds [i].length) goto exeception; */
9552 mono_mb_emit_ldloc (mb
, realidx
);
9553 mono_mb_emit_ldloc (mb
, bounds
);
9554 mono_mb_emit_icon (mb
, (i
* sizeof (MonoArrayBounds
)) + G_STRUCT_OFFSET (MonoArrayBounds
, length
));
9555 mono_mb_emit_byte (mb
, CEE_ADD
);
9556 mono_mb_emit_byte (mb
, CEE_LDIND_I4
);
9557 branch_positions
[i
] = mono_mb_emit_branch (mb
, CEE_BGE_UN
);
9558 /* ind = ind * bounds [i].length + realidx */
9559 mono_mb_emit_ldloc (mb
, ind
);
9560 mono_mb_emit_ldloc (mb
, bounds
);
9561 mono_mb_emit_icon (mb
, (i
* sizeof (MonoArrayBounds
)) + G_STRUCT_OFFSET (MonoArrayBounds
, length
));
9562 mono_mb_emit_byte (mb
, CEE_ADD
);
9563 mono_mb_emit_byte (mb
, CEE_LDIND_I4
);
9564 mono_mb_emit_byte (mb
, CEE_MUL
);
9565 mono_mb_emit_ldloc (mb
, realidx
);
9566 mono_mb_emit_byte (mb
, CEE_ADD
);
9567 mono_mb_emit_stloc (mb
, ind
);
9570 /* return array->vector + ind * element_size */
9571 mono_mb_emit_ldarg (mb
, 0);
9572 mono_mb_emit_ldflda (mb
, G_STRUCT_OFFSET (MonoArray
, vector
));
9573 mono_mb_emit_ldloc (mb
, ind
);
9574 mono_mb_emit_icon (mb
, elem_size
);
9575 mono_mb_emit_byte (mb
, CEE_MUL
);
9576 mono_mb_emit_byte (mb
, CEE_ADD
);
9577 mono_mb_emit_byte (mb
, CEE_RET
);
9579 /* patch the branches to get here and throw */
9580 for (i
= 1; i
< rank
; ++i
) {
9581 mono_mb_patch_branch (mb
, branch_positions
[i
]);
9583 mono_mb_patch_branch (mb
, branch_pos
);
9584 /* throw exception */
9585 mono_mb_emit_exception (mb
, "IndexOutOfRangeException", NULL
);
9587 g_free (branch_positions
);
9588 ret
= mono_mb_create_method (mb
, sig
, 4);
9591 /* cache the result */
9593 mono_marshal_lock ();
9594 for (i
= 0; i
< elem_addr_cache_next
; ++i
) {
9595 if (elem_addr_cache
[i
].rank
== rank
&& elem_addr_cache
[i
].elem_size
== elem_size
) {
9596 /* FIXME: free ret */
9597 ret
= elem_addr_cache
[i
].method
;
9603 ElementAddrWrapperInfo
*info
;
9605 if (elem_addr_cache_next
>= elem_addr_cache_size
) {
9606 int new_size
= elem_addr_cache_size
+ 4;
9607 ArrayElemAddr
*new_array
= g_new0 (ArrayElemAddr
, new_size
);
9608 memcpy (new_array
, elem_addr_cache
, elem_addr_cache_size
* sizeof (ArrayElemAddr
));
9609 g_free (elem_addr_cache
);
9610 elem_addr_cache
= new_array
;
9611 elem_addr_cache_size
= new_size
;
9613 elem_addr_cache
[elem_addr_cache_next
].rank
= rank
;
9614 elem_addr_cache
[elem_addr_cache_next
].elem_size
= elem_size
;
9615 elem_addr_cache
[elem_addr_cache_next
].method
= ret
;
9617 info
= mono_image_alloc0 (mono_defaults
.corlib
, sizeof (ElementAddrWrapperInfo
));
9619 info
->elem_size
= elem_size
;
9621 mono_marshal_set_wrapper_info (ret
, info
);
9623 mono_marshal_unlock ();
9628 mono_marshal_alloc (gulong size
)
9633 res
= CoTaskMemAlloc (size
);
9635 res
= g_try_malloc ((gulong
)size
);
9637 mono_gc_out_of_memory ((gulong
)size
);
9643 mono_marshal_free (gpointer ptr
)
9646 CoTaskMemFree (ptr
);
9653 mono_marshal_free_array (gpointer
*ptr
, int size
)
9660 for (i
= 0; i
< size
; i
++)
9666 mono_marshal_string_to_utf16 (MonoString
*s
)
9668 return s
? mono_string_chars (s
) : NULL
;
9672 mono_marshal_string_to_utf16_copy (MonoString
*s
)
9677 gunichar2
*res
= mono_marshal_alloc ((mono_string_length (s
) * 2) + 2);
9678 memcpy (res
, mono_string_chars (s
), mono_string_length (s
) * 2);
9679 res
[mono_string_length (s
)] = 0;
9685 * mono_marshal_set_last_error:
9687 * This function is invoked to set the last error value from a P/Invoke call
9688 * which has SetLastError set.
9691 mono_marshal_set_last_error (void)
9694 TlsSetValue (last_error_tls_id
, GINT_TO_POINTER (GetLastError ()));
9696 TlsSetValue (last_error_tls_id
, GINT_TO_POINTER (errno
));
9701 mono_marshal_set_last_error_windows (int error
)
9704 TlsSetValue (last_error_tls_id
, GINT_TO_POINTER (error
));
9709 ves_icall_System_Runtime_InteropServices_Marshal_copy_to_unmanaged (MonoArray
*src
, gint32 start_index
,
9710 gpointer dest
, gint32 length
)
9715 MONO_ARCH_SAVE_REGS
;
9717 MONO_CHECK_ARG_NULL (src
);
9718 MONO_CHECK_ARG_NULL (dest
);
9720 if (src
->obj
.vtable
->klass
->rank
!= 1)
9721 mono_raise_exception (mono_get_exception_argument ("array", "array is multi-dimensional"));
9722 if (start_index
< 0)
9723 mono_raise_exception (mono_get_exception_argument ("startIndex", "Must be >= 0"));
9725 mono_raise_exception (mono_get_exception_argument ("length", "Must be >= 0"));
9726 if (start_index
+ length
> mono_array_length (src
))
9727 mono_raise_exception (mono_get_exception_argument ("length", "start_index + length > array length"));
9729 element_size
= mono_array_element_size (src
->obj
.vtable
->klass
);
9731 /* no references should be involved */
9732 source_addr
= mono_array_addr_with_size (src
, element_size
, start_index
);
9734 memcpy (dest
, source_addr
, length
* element_size
);
9738 ves_icall_System_Runtime_InteropServices_Marshal_copy_from_unmanaged (gpointer src
, gint32 start_index
,
9739 MonoArray
*dest
, gint32 length
)
9744 MONO_ARCH_SAVE_REGS
;
9746 MONO_CHECK_ARG_NULL (src
);
9747 MONO_CHECK_ARG_NULL (dest
);
9749 if (dest
->obj
.vtable
->klass
->rank
!= 1)
9750 mono_raise_exception (mono_get_exception_argument ("array", "array is multi-dimensional"));
9751 if (start_index
< 0)
9752 mono_raise_exception (mono_get_exception_argument ("startIndex", "Must be >= 0"));
9754 mono_raise_exception (mono_get_exception_argument ("length", "Must be >= 0"));
9755 if (start_index
+ length
> mono_array_length (dest
))
9756 mono_raise_exception (mono_get_exception_argument ("length", "start_index + length > array length"));
9758 element_size
= mono_array_element_size (dest
->obj
.vtable
->klass
);
9760 /* no references should be involved */
9761 dest_addr
= mono_array_addr_with_size (dest
, element_size
, start_index
);
9763 memcpy (dest_addr
, src
, length
* element_size
);
9766 #if NO_UNALIGNED_ACCESS
9767 #define RETURN_UNALIGNED(type, addr) \
9770 memcpy(&val, p + offset, sizeof(val)); \
9773 #define WRITE_UNALIGNED(type, addr, val) \
9774 memcpy(addr, &val, sizeof(type))
9776 #define RETURN_UNALIGNED(type, addr) \
9777 return *(type*)(p + offset);
9778 #define WRITE_UNALIGNED(type, addr, val) \
9779 (*(type *)(addr) = (val))
9783 ves_icall_System_Runtime_InteropServices_Marshal_ReadIntPtr (gpointer ptr
, gint32 offset
)
9787 MONO_ARCH_SAVE_REGS
;
9789 RETURN_UNALIGNED(gpointer
, p
+ offset
);
9793 ves_icall_System_Runtime_InteropServices_Marshal_ReadByte (gpointer ptr
, gint32 offset
)
9797 MONO_ARCH_SAVE_REGS
;
9799 return *(unsigned char*)(p
+ offset
);
9803 ves_icall_System_Runtime_InteropServices_Marshal_ReadInt16 (gpointer ptr
, gint32 offset
)
9807 MONO_ARCH_SAVE_REGS
;
9809 RETURN_UNALIGNED(gint16
, p
+ offset
);
9813 ves_icall_System_Runtime_InteropServices_Marshal_ReadInt32 (gpointer ptr
, gint32 offset
)
9817 MONO_ARCH_SAVE_REGS
;
9819 RETURN_UNALIGNED(gint32
, p
+ offset
);
9823 ves_icall_System_Runtime_InteropServices_Marshal_ReadInt64 (gpointer ptr
, gint32 offset
)
9827 MONO_ARCH_SAVE_REGS
;
9829 RETURN_UNALIGNED(gint64
, p
+ offset
);
9833 ves_icall_System_Runtime_InteropServices_Marshal_WriteByte (gpointer ptr
, gint32 offset
, unsigned char val
)
9837 MONO_ARCH_SAVE_REGS
;
9839 *(unsigned char*)(p
+ offset
) = val
;
9843 ves_icall_System_Runtime_InteropServices_Marshal_WriteIntPtr (gpointer ptr
, gint32 offset
, gpointer val
)
9847 MONO_ARCH_SAVE_REGS
;
9849 WRITE_UNALIGNED(gpointer
, p
+ offset
, val
);
9853 ves_icall_System_Runtime_InteropServices_Marshal_WriteInt16 (gpointer ptr
, gint32 offset
, gint16 val
)
9857 MONO_ARCH_SAVE_REGS
;
9859 WRITE_UNALIGNED(gint16
, p
+ offset
, val
);
9863 ves_icall_System_Runtime_InteropServices_Marshal_WriteInt32 (gpointer ptr
, gint32 offset
, gint32 val
)
9867 MONO_ARCH_SAVE_REGS
;
9869 WRITE_UNALIGNED(gint32
, p
+ offset
, val
);
9873 ves_icall_System_Runtime_InteropServices_Marshal_WriteInt64 (gpointer ptr
, gint32 offset
, gint64 val
)
9877 MONO_ARCH_SAVE_REGS
;
9879 WRITE_UNALIGNED(gint64
, p
+ offset
, val
);
9883 ves_icall_System_Runtime_InteropServices_Marshal_PtrToStringAnsi (char *ptr
)
9885 MONO_ARCH_SAVE_REGS
;
9890 return mono_string_new (mono_domain_get (), ptr
);
9894 ves_icall_System_Runtime_InteropServices_Marshal_PtrToStringAnsi_len (char *ptr
, gint32 len
)
9896 MONO_ARCH_SAVE_REGS
;
9899 mono_raise_exception (mono_get_exception_argument_null ("ptr"));
9900 g_assert_not_reached ();
9903 return mono_string_new_len (mono_domain_get (), ptr
, len
);
9908 ves_icall_System_Runtime_InteropServices_Marshal_PtrToStringUni (guint16
*ptr
)
9910 MonoDomain
*domain
= mono_domain_get ();
9914 MONO_ARCH_SAVE_REGS
;
9922 return mono_string_new_utf16 (domain
, ptr
, len
);
9926 ves_icall_System_Runtime_InteropServices_Marshal_PtrToStringUni_len (guint16
*ptr
, gint32 len
)
9928 MonoDomain
*domain
= mono_domain_get ();
9930 MONO_ARCH_SAVE_REGS
;
9933 mono_raise_exception (mono_get_exception_argument_null ("ptr"));
9934 g_assert_not_reached ();
9937 return mono_string_new_utf16 (domain
, ptr
, len
);
9942 ves_icall_System_Runtime_InteropServices_Marshal_GetLastWin32Error (void)
9944 MONO_ARCH_SAVE_REGS
;
9946 return (GPOINTER_TO_INT (TlsGetValue (last_error_tls_id
)));
9950 ves_icall_System_Runtime_InteropServices_Marshal_SizeOf (MonoReflectionType
*rtype
)
9956 MONO_ARCH_SAVE_REGS
;
9958 MONO_CHECK_ARG_NULL (rtype
);
9961 klass
= mono_class_from_mono_type (type
);
9962 layout
= (klass
->flags
& TYPE_ATTRIBUTE_LAYOUT_MASK
);
9964 if (layout
== TYPE_ATTRIBUTE_AUTO_LAYOUT
) {
9968 msg
= g_strdup_printf ("Type %s cannot be marshaled as an unmanaged structure.", klass
->name
);
9969 exc
= mono_get_exception_argument ("t", msg
);
9971 mono_raise_exception (exc
);
9975 return mono_class_native_size (klass
, NULL
);
9979 ves_icall_System_Runtime_InteropServices_Marshal_StructureToPtr (MonoObject
*obj
, gpointer dst
, MonoBoolean delete_old
)
9984 MONO_ARCH_SAVE_REGS
;
9986 MONO_CHECK_ARG_NULL (obj
);
9987 MONO_CHECK_ARG_NULL (dst
);
9989 method
= mono_marshal_get_struct_to_ptr (obj
->vtable
->klass
);
9993 pa
[2] = &delete_old
;
9995 mono_runtime_invoke (method
, NULL
, pa
, NULL
);
9999 ptr_to_structure (gpointer src
, MonoObject
*dst
)
10001 MonoMethod
*method
;
10004 method
= mono_marshal_get_ptr_to_struct (dst
->vtable
->klass
);
10009 mono_runtime_invoke (method
, NULL
, pa
, NULL
);
10013 ves_icall_System_Runtime_InteropServices_Marshal_PtrToStructure (gpointer src
, MonoObject
*dst
)
10017 MONO_ARCH_SAVE_REGS
;
10019 MONO_CHECK_ARG_NULL (src
);
10020 MONO_CHECK_ARG_NULL (dst
);
10022 t
= mono_type_get_underlying_type (mono_class_get_type (dst
->vtable
->klass
));
10024 if (t
->type
== MONO_TYPE_VALUETYPE
) {
10025 MonoException
*exc
;
10028 tmp
= g_strdup_printf ("Destination is a boxed value type.");
10029 exc
= mono_get_exception_argument ("dst", tmp
);
10032 mono_raise_exception (exc
);
10036 ptr_to_structure (src
, dst
);
10040 ves_icall_System_Runtime_InteropServices_Marshal_PtrToStructure_type (gpointer src
, MonoReflectionType
*type
)
10042 MonoDomain
*domain
= mono_domain_get ();
10045 MONO_ARCH_SAVE_REGS
;
10047 MONO_CHECK_ARG_NULL (src
);
10048 MONO_CHECK_ARG_NULL (type
);
10050 res
= mono_object_new (domain
, mono_class_from_mono_type (type
->type
));
10052 ptr_to_structure (src
, res
);
10058 ves_icall_System_Runtime_InteropServices_Marshal_OffsetOf (MonoReflectionType
*type
, MonoString
*field_name
)
10060 MonoMarshalType
*info
;
10063 int match_index
= -1;
10065 MONO_ARCH_SAVE_REGS
;
10067 MONO_CHECK_ARG_NULL (type
);
10068 MONO_CHECK_ARG_NULL (field_name
);
10070 fname
= mono_string_to_utf8 (field_name
);
10071 klass
= mono_class_from_mono_type (type
->type
);
10073 while (klass
&& match_index
== -1) {
10074 MonoClassField
* field
;
10076 gpointer iter
= NULL
;
10077 while ((field
= mono_class_get_fields (klass
, &iter
))) {
10078 if (field
->type
->attrs
& FIELD_ATTRIBUTE_STATIC
)
10080 if (!strcmp (fname
, mono_field_get_name (field
))) {
10087 if (match_index
== -1)
10088 klass
= klass
->parent
;
10093 if(match_index
== -1) {
10094 MonoException
* exc
;
10097 /* Get back original class instance */
10098 klass
= mono_class_from_mono_type (type
->type
);
10100 tmp
= g_strdup_printf ("Field passed in is not a marshaled member of the type %s", klass
->name
);
10101 exc
= mono_get_exception_argument ("fieldName", tmp
);
10104 mono_raise_exception ((MonoException
*)exc
);
10107 info
= mono_marshal_load_type_info (klass
);
10108 return info
->fields
[match_index
].offset
;
10112 ves_icall_System_Runtime_InteropServices_Marshal_StringToHGlobalAnsi (MonoString
*string
)
10117 tres
= mono_string_to_utf8 (string
);
10121 len
= strlen (tres
) + 1;
10122 ret
= ves_icall_System_Runtime_InteropServices_Marshal_AllocHGlobal (len
);
10123 memcpy (ret
, tres
, len
);
10128 return mono_string_to_utf8 (string
);
10133 ves_icall_System_Runtime_InteropServices_Marshal_StringToHGlobalUni (MonoString
*string
)
10135 MONO_ARCH_SAVE_REGS
;
10137 if (string
== NULL
)
10140 #ifdef TARGET_WIN32
10141 gunichar2
*res
= ves_icall_System_Runtime_InteropServices_Marshal_AllocHGlobal
10142 ((mono_string_length (string
) + 1) * 2);
10144 gunichar2
*res
= g_malloc ((mono_string_length (string
) + 1) * 2);
10146 memcpy (res
, mono_string_chars (string
), mono_string_length (string
) * 2);
10147 res
[mono_string_length (string
)] = 0;
10153 mono_struct_delete_old (MonoClass
*klass
, char *ptr
)
10155 MonoMarshalType
*info
;
10158 info
= mono_marshal_load_type_info (klass
);
10160 for (i
= 0; i
< info
->num_fields
; i
++) {
10161 MonoMarshalNative ntype
;
10162 MonoMarshalConv conv
;
10163 MonoType
*ftype
= info
->fields
[i
].field
->type
;
10166 if (ftype
->attrs
& FIELD_ATTRIBUTE_STATIC
)
10169 ntype
= mono_type_to_unmanaged (ftype
, info
->fields
[i
].mspec
, TRUE
,
10170 klass
->unicode
, &conv
);
10172 cpos
= ptr
+ info
->fields
[i
].offset
;
10175 case MONO_MARSHAL_CONV_NONE
:
10176 if (MONO_TYPE_ISSTRUCT (ftype
)) {
10177 mono_struct_delete_old (ftype
->data
.klass
, cpos
);
10181 case MONO_MARSHAL_CONV_STR_LPWSTR
:
10182 /* We assume this field points inside a MonoString */
10184 case MONO_MARSHAL_CONV_STR_LPTSTR
:
10185 #ifdef TARGET_WIN32
10186 /* We assume this field points inside a MonoString
10190 case MONO_MARSHAL_CONV_STR_LPSTR
:
10191 case MONO_MARSHAL_CONV_STR_BSTR
:
10192 case MONO_MARSHAL_CONV_STR_ANSIBSTR
:
10193 case MONO_MARSHAL_CONV_STR_TBSTR
:
10194 mono_marshal_free (*(gpointer
*)cpos
);
10204 ves_icall_System_Runtime_InteropServices_Marshal_DestroyStructure (gpointer src
, MonoReflectionType
*type
)
10208 MONO_ARCH_SAVE_REGS
;
10210 MONO_CHECK_ARG_NULL (src
);
10211 MONO_CHECK_ARG_NULL (type
);
10213 klass
= mono_class_from_mono_type (type
->type
);
10215 mono_struct_delete_old (klass
, (char *)src
);
10219 ves_icall_System_Runtime_InteropServices_Marshal_AllocHGlobal (int size
)
10223 MONO_ARCH_SAVE_REGS
;
10225 if ((gulong
)size
== 0)
10226 /* This returns a valid pointer for size 0 on MS.NET */
10230 res
= GlobalAlloc (GMEM_FIXED
, (gulong
)size
);
10232 res
= g_try_malloc ((gulong
)size
);
10235 mono_gc_out_of_memory ((gulong
)size
);
10241 ves_icall_System_Runtime_InteropServices_Marshal_ReAllocHGlobal (gpointer ptr
, int size
)
10246 mono_gc_out_of_memory ((gulong
)size
);
10251 res
= GlobalReAlloc (ptr
, (gulong
)size
, GMEM_MOVEABLE
);
10253 res
= g_try_realloc (ptr
, (gulong
)size
);
10256 mono_gc_out_of_memory ((gulong
)size
);
10262 ves_icall_System_Runtime_InteropServices_Marshal_FreeHGlobal (void *ptr
)
10264 MONO_ARCH_SAVE_REGS
;
10274 ves_icall_System_Runtime_InteropServices_Marshal_AllocCoTaskMem (int size
)
10276 MONO_ARCH_SAVE_REGS
;
10279 return CoTaskMemAlloc (size
);
10281 return g_try_malloc ((gulong
)size
);
10286 ves_icall_System_Runtime_InteropServices_Marshal_FreeCoTaskMem (void *ptr
)
10288 MONO_ARCH_SAVE_REGS
;
10291 CoTaskMemFree (ptr
);
10298 ves_icall_System_Runtime_InteropServices_Marshal_ReAllocCoTaskMem (gpointer ptr
, int size
)
10300 MONO_ARCH_SAVE_REGS
;
10303 return CoTaskMemRealloc (ptr
, size
);
10305 return g_try_realloc (ptr
, (gulong
)size
);
10310 ves_icall_System_Runtime_InteropServices_Marshal_UnsafeAddrOfPinnedArrayElement (MonoArray
*arrayobj
, int index
)
10312 return mono_array_addr_with_size (arrayobj
, mono_array_element_size (arrayobj
->obj
.vtable
->klass
), index
);
10316 ves_icall_System_Runtime_InteropServices_Marshal_GetDelegateForFunctionPointerInternal (void *ftn
, MonoReflectionType
*type
)
10318 return mono_ftnptr_to_delegate (mono_type_get_class (type
->type
), ftn
);
10322 * mono_marshal_is_loading_type_info:
10324 * Return whenever mono_marshal_load_type_info () is being executed for KLASS by this
10328 mono_marshal_is_loading_type_info (MonoClass
*klass
)
10330 GSList
*loads_list
= TlsGetValue (load_type_info_tls_id
);
10332 return g_slist_find (loads_list
, klass
) != NULL
;
10336 * mono_marshal_load_type_info:
10338 * Initialize klass->marshal_info using information from metadata. This function can
10339 * recursively call itself, and the caller is responsible to avoid that by calling
10340 * mono_marshal_is_loading_type_info () beforehand.
10342 * LOCKING: Acquires the loader lock.
10345 mono_marshal_load_type_info (MonoClass
* klass
)
10348 guint32 native_size
= 0, min_align
= 1;
10349 MonoMarshalType
*info
;
10350 MonoClassField
* field
;
10353 GSList
*loads_list
;
10355 g_assert (klass
!= NULL
);
10357 if (klass
->marshal_info
)
10358 return klass
->marshal_info
;
10360 if (!klass
->inited
)
10361 mono_class_init (klass
);
10363 mono_loader_lock ();
10365 if (klass
->marshal_info
) {
10366 mono_loader_unlock ();
10367 return klass
->marshal_info
;
10371 * This function can recursively call itself, so we keep the list of classes which are
10372 * under initialization in a TLS list.
10374 g_assert (!mono_marshal_is_loading_type_info (klass
));
10375 loads_list
= TlsGetValue (load_type_info_tls_id
);
10376 loads_list
= g_slist_prepend (loads_list
, klass
);
10377 TlsSetValue (load_type_info_tls_id
, loads_list
);
10380 while ((field
= mono_class_get_fields (klass
, &iter
))) {
10381 if (field
->type
->attrs
& FIELD_ATTRIBUTE_STATIC
)
10383 if (mono_field_is_deleted (field
))
10388 layout
= klass
->flags
& TYPE_ATTRIBUTE_LAYOUT_MASK
;
10390 /* The mempool is protected by the loader lock */
10391 info
= mono_image_alloc0 (klass
->image
, MONO_SIZEOF_MARSHAL_TYPE
+ sizeof (MonoMarshalField
) * count
);
10392 info
->num_fields
= count
;
10394 /* Try to find a size for this type in metadata */
10395 mono_metadata_packing_from_typedef (klass
->image
, klass
->type_token
, NULL
, &native_size
);
10397 if (klass
->parent
) {
10398 int parent_size
= mono_class_native_size (klass
->parent
, NULL
);
10400 /* Add parent size to real size */
10401 native_size
+= parent_size
;
10402 info
->native_size
= parent_size
;
10407 while ((field
= mono_class_get_fields (klass
, &iter
))) {
10411 if (field
->type
->attrs
& FIELD_ATTRIBUTE_STATIC
)
10414 if (mono_field_is_deleted (field
))
10416 if (field
->type
->attrs
& FIELD_ATTRIBUTE_HAS_FIELD_MARSHAL
)
10417 mono_metadata_field_info_with_mempool (klass
->image
, mono_metadata_token_index (mono_class_get_field_token (field
)) - 1,
10418 NULL
, NULL
, &info
->fields
[j
].mspec
);
10420 info
->fields
[j
].field
= field
;
10422 if ((mono_class_num_fields (klass
) == 1) && (klass
->instance_size
== sizeof (MonoObject
)) &&
10423 (strcmp (mono_field_get_name (field
), "$PRIVATE$") == 0)) {
10424 /* This field is a hack inserted by MCS to empty structures */
10429 case TYPE_ATTRIBUTE_AUTO_LAYOUT
:
10430 case TYPE_ATTRIBUTE_SEQUENTIAL_LAYOUT
:
10431 size
= mono_marshal_type_size (field
->type
, info
->fields
[j
].mspec
,
10432 &align
, TRUE
, klass
->unicode
);
10433 align
= klass
->packing_size
? MIN (klass
->packing_size
, align
): align
;
10434 min_align
= MAX (align
, min_align
);
10435 info
->fields
[j
].offset
= info
->native_size
;
10436 info
->fields
[j
].offset
+= align
- 1;
10437 info
->fields
[j
].offset
&= ~(align
- 1);
10438 info
->native_size
= info
->fields
[j
].offset
+ size
;
10440 case TYPE_ATTRIBUTE_EXPLICIT_LAYOUT
:
10441 size
= mono_marshal_type_size (field
->type
, info
->fields
[j
].mspec
,
10442 &align
, TRUE
, klass
->unicode
);
10443 align
= klass
->packing_size
? MIN (klass
->packing_size
, align
): align
;
10444 min_align
= MAX (align
, min_align
);
10445 info
->fields
[j
].offset
= field
->offset
- sizeof (MonoObject
);
10446 info
->native_size
= MAX (info
->native_size
, info
->fields
[j
].offset
+ size
);
10452 if(layout
!= TYPE_ATTRIBUTE_AUTO_LAYOUT
) {
10453 info
->native_size
= MAX (native_size
, info
->native_size
);
10456 if (info
->native_size
& (min_align
- 1)) {
10457 info
->native_size
+= min_align
- 1;
10458 info
->native_size
&= ~(min_align
- 1);
10461 info
->min_align
= min_align
;
10463 /* Update the class's blittable info, if the layouts don't match */
10464 if (info
->native_size
!= mono_class_value_size (klass
, NULL
))
10465 klass
->blittable
= FALSE
;
10467 /* If this is an array type, ensure that we have element info */
10468 if (klass
->element_class
&& !mono_marshal_is_loading_type_info (klass
->element_class
)) {
10469 mono_marshal_load_type_info (klass
->element_class
);
10472 loads_list
= TlsGetValue (load_type_info_tls_id
);
10473 loads_list
= g_slist_remove (loads_list
, klass
);
10474 TlsSetValue (load_type_info_tls_id
, loads_list
);
10476 /*We do double-checking locking on marshal_info */
10477 mono_memory_barrier ();
10479 klass
->marshal_info
= info
;
10481 mono_loader_unlock ();
10483 return klass
->marshal_info
;
10487 * mono_class_native_size:
10490 * Returns: the native size of an object instance (when marshaled
10491 * to unmanaged code)
10494 mono_class_native_size (MonoClass
*klass
, guint32
*align
)
10496 if (!klass
->marshal_info
) {
10497 if (mono_marshal_is_loading_type_info (klass
)) {
10502 mono_marshal_load_type_info (klass
);
10507 *align
= klass
->marshal_info
->min_align
;
10509 return klass
->marshal_info
->native_size
;
10512 /* __alignof__ returns the preferred alignment of values not the actual alignment used by
10513 the compiler so is wrong e.g. for Linux where doubles are aligned on a 4 byte boundary
10514 but __alignof__ returns 8 - using G_STRUCT_OFFSET works better */
10515 #define ALIGNMENT(type) G_STRUCT_OFFSET(struct { char c; type x; }, x)
10518 * mono_type_native_stack_size:
10519 * @t: the type to return the size it uses on the stack
10521 * Returns: the number of bytes required to hold an instance of this
10522 * type on the native stack
10525 mono_type_native_stack_size (MonoType
*t
, guint32
*align
)
10529 g_assert (t
!= NULL
);
10535 *align
= sizeof (gpointer
);
10536 return sizeof (gpointer
);
10540 case MONO_TYPE_BOOLEAN
:
10541 case MONO_TYPE_CHAR
:
10552 case MONO_TYPE_STRING
:
10553 case MONO_TYPE_OBJECT
:
10554 case MONO_TYPE_CLASS
:
10555 case MONO_TYPE_SZARRAY
:
10556 case MONO_TYPE_PTR
:
10557 case MONO_TYPE_FNPTR
:
10558 case MONO_TYPE_ARRAY
:
10559 *align
= sizeof (gpointer
);
10560 return sizeof (gpointer
);
10565 *align
= ALIGNMENT (gdouble
);
10569 *align
= ALIGNMENT (glong
);
10571 case MONO_TYPE_GENERICINST
:
10572 if (!mono_type_generic_inst_is_valuetype (t
)) {
10573 *align
= sizeof (gpointer
);
10574 return sizeof (gpointer
);
10577 case MONO_TYPE_TYPEDBYREF
:
10578 case MONO_TYPE_VALUETYPE
: {
10580 MonoClass
*klass
= mono_class_from_mono_type (t
);
10582 if (klass
->enumtype
)
10583 return mono_type_native_stack_size (mono_class_enum_basetype (klass
), align
);
10585 size
= mono_class_native_size (klass
, align
);
10586 *align
= *align
+ 3;
10596 g_error ("type 0x%02x unknown", t
->type
);
10602 mono_marshal_type_size (MonoType
*type
, MonoMarshalSpec
*mspec
, guint32
*align
,
10603 gboolean as_field
, gboolean unicode
)
10605 MonoMarshalNative native_type
= mono_type_to_unmanaged (type
, mspec
, as_field
, unicode
, NULL
);
10608 switch (native_type
) {
10609 case MONO_NATIVE_BOOLEAN
:
10612 case MONO_NATIVE_I1
:
10613 case MONO_NATIVE_U1
:
10616 case MONO_NATIVE_I2
:
10617 case MONO_NATIVE_U2
:
10618 case MONO_NATIVE_VARIANTBOOL
:
10621 case MONO_NATIVE_I4
:
10622 case MONO_NATIVE_U4
:
10623 case MONO_NATIVE_ERROR
:
10626 case MONO_NATIVE_I8
:
10627 case MONO_NATIVE_U8
:
10628 *align
= ALIGNMENT(guint64
);
10630 case MONO_NATIVE_R4
:
10633 case MONO_NATIVE_R8
:
10634 *align
= ALIGNMENT(double);
10636 case MONO_NATIVE_INT
:
10637 case MONO_NATIVE_UINT
:
10638 case MONO_NATIVE_LPSTR
:
10639 case MONO_NATIVE_LPWSTR
:
10640 case MONO_NATIVE_LPTSTR
:
10641 case MONO_NATIVE_BSTR
:
10642 case MONO_NATIVE_ANSIBSTR
:
10643 case MONO_NATIVE_TBSTR
:
10644 case MONO_NATIVE_LPARRAY
:
10645 case MONO_NATIVE_SAFEARRAY
:
10646 case MONO_NATIVE_IUNKNOWN
:
10647 case MONO_NATIVE_IDISPATCH
:
10648 case MONO_NATIVE_INTERFACE
:
10649 case MONO_NATIVE_ASANY
:
10650 case MONO_NATIVE_FUNC
:
10651 case MONO_NATIVE_LPSTRUCT
:
10652 *align
= ALIGNMENT(gpointer
);
10653 return sizeof (gpointer
);
10654 case MONO_NATIVE_STRUCT
:
10655 klass
= mono_class_from_mono_type (type
);
10656 if (klass
== mono_defaults
.object_class
&&
10657 (mspec
&& mspec
->native
== MONO_NATIVE_STRUCT
)) {
10661 return mono_class_native_size (klass
, align
);
10662 case MONO_NATIVE_BYVALTSTR
: {
10663 int esize
= unicode
? 2: 1;
10666 return mspec
->data
.array_data
.num_elem
* esize
;
10668 case MONO_NATIVE_BYVALARRAY
: {
10669 // FIXME: Have to consider ArraySubType
10671 klass
= mono_class_from_mono_type (type
);
10672 if (klass
->element_class
== mono_defaults
.char_class
) {
10673 esize
= unicode
? 2 : 1;
10676 esize
= mono_class_native_size (klass
->element_class
, align
);
10679 return mspec
->data
.array_data
.num_elem
* esize
;
10681 case MONO_NATIVE_CUSTOM
:
10682 *align
= sizeof (gpointer
);
10683 return sizeof (gpointer
);
10685 case MONO_NATIVE_CURRENCY
:
10686 case MONO_NATIVE_VBBYREFSTR
:
10688 g_error ("native type %02x not implemented", native_type
);
10691 g_assert_not_reached ();
10696 mono_marshal_asany (MonoObject
*o
, MonoMarshalNative string_encoding
, int param_attrs
)
10704 t
= &o
->vtable
->klass
->byval_arg
;
10708 case MONO_TYPE_PTR
:
10711 case MONO_TYPE_BOOLEAN
:
10714 case MONO_TYPE_CHAR
:
10719 return mono_object_unbox (o
);
10721 case MONO_TYPE_STRING
:
10722 switch (string_encoding
) {
10723 case MONO_NATIVE_LPWSTR
:
10724 return mono_string_to_utf16 ((MonoString
*)o
);
10726 case MONO_NATIVE_LPSTR
:
10727 return mono_string_to_lpstr ((MonoString
*)o
);
10730 g_warning ("marshaling conversion %d not implemented", string_encoding
);
10731 g_assert_not_reached ();
10734 case MONO_TYPE_CLASS
:
10735 case MONO_TYPE_VALUETYPE
: {
10736 MonoMethod
*method
;
10739 MonoBoolean delete_old
= FALSE
;
10741 klass
= t
->data
.klass
;
10743 if ((klass
->flags
& TYPE_ATTRIBUTE_LAYOUT_MASK
) == TYPE_ATTRIBUTE_AUTO_LAYOUT
)
10746 if (klass
->valuetype
&& (((klass
->flags
& TYPE_ATTRIBUTE_LAYOUT_MASK
) == TYPE_ATTRIBUTE_EXPLICIT_LAYOUT
) ||
10747 klass
->blittable
|| klass
->enumtype
))
10748 return mono_object_unbox (o
);
10750 res
= mono_marshal_alloc (mono_class_native_size (klass
, NULL
));
10752 if (!((param_attrs
& PARAM_ATTRIBUTE_OUT
) && !(param_attrs
& PARAM_ATTRIBUTE_IN
))) {
10753 method
= mono_marshal_get_struct_to_ptr (o
->vtable
->klass
);
10757 pa
[2] = &delete_old
;
10759 mono_runtime_invoke (method
, NULL
, pa
, NULL
);
10766 mono_raise_exception (mono_get_exception_argument ("", "No PInvoke conversion exists for value passed to Object-typed parameter."));
10772 mono_marshal_free_asany (MonoObject
*o
, gpointer ptr
, MonoMarshalNative string_encoding
, int param_attrs
)
10780 t
= &o
->vtable
->klass
->byval_arg
;
10782 case MONO_TYPE_STRING
:
10783 switch (string_encoding
) {
10784 case MONO_NATIVE_LPWSTR
:
10785 case MONO_NATIVE_LPSTR
:
10786 mono_marshal_free (ptr
);
10789 g_warning ("marshaling conversion %d not implemented", string_encoding
);
10790 g_assert_not_reached ();
10793 case MONO_TYPE_CLASS
:
10794 case MONO_TYPE_VALUETYPE
: {
10795 klass
= t
->data
.klass
;
10797 if (klass
->valuetype
&& (((klass
->flags
& TYPE_ATTRIBUTE_LAYOUT_MASK
) == TYPE_ATTRIBUTE_EXPLICIT_LAYOUT
) ||
10798 klass
->blittable
|| klass
->enumtype
))
10801 if (param_attrs
& PARAM_ATTRIBUTE_OUT
) {
10802 MonoMethod
*method
= mono_marshal_get_ptr_to_struct (o
->vtable
->klass
);
10808 mono_runtime_invoke (method
, NULL
, pa
, NULL
);
10811 if (!((param_attrs
& PARAM_ATTRIBUTE_OUT
) && !(param_attrs
& PARAM_ATTRIBUTE_IN
))) {
10812 mono_struct_delete_old (klass
, ptr
);
10815 mono_marshal_free (ptr
);
10824 mono_marshal_get_generic_array_helper (MonoClass
*class, MonoClass
*iface
, gchar
*name
, MonoMethod
*method
)
10826 MonoMethodSignature
*sig
, *csig
;
10827 MonoMethodBuilder
*mb
;
10831 mb
= mono_mb_new_no_dup_name (class, name
, MONO_WRAPPER_MANAGED_TO_MANAGED
);
10832 mb
->method
->slot
= -1;
10834 mb
->method
->flags
= METHOD_ATTRIBUTE_PRIVATE
| METHOD_ATTRIBUTE_VIRTUAL
|
10835 METHOD_ATTRIBUTE_NEW_SLOT
| METHOD_ATTRIBUTE_HIDE_BY_SIG
| METHOD_ATTRIBUTE_FINAL
;
10837 sig
= mono_method_signature (method
);
10838 csig
= signature_dup (method
->klass
->image
, sig
);
10839 csig
->generic_param_count
= 0;
10841 mono_mb_emit_ldarg (mb
, 0);
10842 for (i
= 0; i
< csig
->param_count
; i
++)
10843 mono_mb_emit_ldarg (mb
, i
+ 1);
10844 mono_mb_emit_managed_call (mb
, method
, NULL
);
10845 mono_mb_emit_byte (mb
, CEE_RET
);
10847 /* We can corlib internal methods */
10848 mb
->skip_visibility
= TRUE
;
10850 res
= mono_mb_create_method (mb
, csig
, csig
->param_count
+ 16);
10858 * The mono_win32_compat_* functions are implementations of inline
10859 * Windows kernel32 APIs, which are DllImport-able under MS.NET,
10860 * although not exported by kernel32.
10862 * We map the appropiate kernel32 entries to these functions using
10863 * dllmaps declared in the global etc/mono/config.
10867 mono_win32_compat_CopyMemory (gpointer dest
, gconstpointer source
, gsize length
)
10869 if (!dest
|| !source
)
10872 memcpy (dest
, source
, length
);
10876 mono_win32_compat_FillMemory (gpointer dest
, gsize length
, guchar fill
)
10878 memset (dest
, fill
, length
);
10882 mono_win32_compat_MoveMemory (gpointer dest
, gconstpointer source
, gsize length
)
10884 if (!dest
|| !source
)
10887 memmove (dest
, source
, length
);
10891 mono_win32_compat_ZeroMemory (gpointer dest
, gsize length
)
10893 memset (dest
, 0, length
);
10897 mono_marshal_find_nonzero_bit_offset (guint8
*buf
, int len
, int *byte_offset
, guint8
*bitmask
)
10902 for (i
= 0; i
< len
; ++i
)
10906 g_assert (i
< len
);
10909 while (byte
&& !(byte
& 1))
10911 g_assert (byte
== 1);
10914 *bitmask
= buf
[i
];
10918 mono_marshal_get_thunk_invoke_wrapper (MonoMethod
*method
)
10920 MonoMethodBuilder
*mb
;
10921 MonoMethodSignature
*sig
, *csig
;
10922 MonoExceptionClause
*clause
;
10927 int i
, param_count
, sig_size
, pos_leave
;
10931 klass
= method
->klass
;
10932 image
= method
->klass
->image
;
10933 cache
= get_cache (&image
->thunk_invoke_cache
, mono_aligned_addr_hash
, NULL
);
10935 if ((res
= mono_marshal_find_in_cache (cache
, method
)))
10938 sig
= mono_method_signature (method
);
10939 mb
= mono_mb_new (klass
, method
->name
, MONO_WRAPPER_NATIVE_TO_MANAGED
);
10941 /* add "this" and exception param */
10942 param_count
= sig
->param_count
+ sig
->hasthis
+ 1;
10944 /* dup & extend signature */
10945 csig
= mono_metadata_signature_alloc (image
, param_count
);
10946 sig_size
= MONO_SIZEOF_METHOD_SIGNATURE
+ sig
->param_count
* sizeof (MonoType
*);
10947 memcpy (csig
, sig
, sig_size
);
10948 csig
->param_count
= param_count
;
10951 csig
->call_convention
= MONO_CALL_DEFAULT
;
10953 if (sig
->hasthis
) {
10955 csig
->params
[0] = &klass
->byval_arg
;
10956 /* move params up by one */
10957 for (i
= 0; i
< sig
->param_count
; i
++)
10958 csig
->params
[i
+ 1] = sig
->params
[i
];
10961 /* setup exception param as byref+[out] */
10962 csig
->params
[param_count
- 1] = mono_metadata_type_dup (image
,
10963 &mono_defaults
.exception_class
->byval_arg
);
10964 csig
->params
[param_count
- 1]->byref
= 1;
10965 csig
->params
[param_count
- 1]->attrs
= PARAM_ATTRIBUTE_OUT
;
10967 /* convert struct return to object */
10968 if (MONO_TYPE_ISSTRUCT (sig
->ret
))
10969 csig
->ret
= &mono_defaults
.object_class
->byval_arg
;
10971 /* local 0 (temp for exception object) */
10972 mono_mb_add_local (mb
, &mono_defaults
.object_class
->byval_arg
);
10974 /* local 1 (temp for result) */
10975 if (!MONO_TYPE_IS_VOID (sig
->ret
))
10976 mono_mb_add_local (mb
, sig
->ret
);
10978 /* clear exception arg */
10979 mono_mb_emit_ldarg (mb
, param_count
- 1);
10980 mono_mb_emit_byte (mb
, CEE_LDNULL
);
10981 mono_mb_emit_byte (mb
, CEE_STIND_REF
);
10984 clause
= mono_image_alloc0 (image
, sizeof (MonoExceptionClause
));
10985 clause
->try_offset
= mono_mb_get_label (mb
);
10987 /* push method's args */
10988 for (i
= 0; i
< param_count
- 1; i
++) {
10992 mono_mb_emit_ldarg (mb
, i
);
10994 /* get the byval type of the param */
10995 klass
= mono_class_from_mono_type (csig
->params
[i
]);
10996 type
= &klass
->byval_arg
;
10998 /* unbox struct args */
10999 if (MONO_TYPE_ISSTRUCT (type
)) {
11000 mono_mb_emit_op (mb
, CEE_UNBOX
, klass
);
11002 /* byref args & and the "this" arg must remain a ptr.
11003 Otherwise make a copy of the value type */
11004 if (!(csig
->params
[i
]->byref
|| (i
== 0 && sig
->hasthis
)))
11005 mono_mb_emit_op (mb
, CEE_LDOBJ
, klass
);
11007 csig
->params
[i
] = &mono_defaults
.object_class
->byval_arg
;
11012 if (method
->flags
& METHOD_ATTRIBUTE_VIRTUAL
)
11013 mono_mb_emit_op (mb
, CEE_CALLVIRT
, method
);
11015 mono_mb_emit_op (mb
, CEE_CALL
, method
);
11017 /* save result at local 1 */
11018 if (!MONO_TYPE_IS_VOID (sig
->ret
))
11019 mono_mb_emit_stloc (mb
, 1);
11021 pos_leave
= mono_mb_emit_branch (mb
, CEE_LEAVE
);
11024 clause
->flags
= MONO_EXCEPTION_CLAUSE_NONE
;
11025 clause
->try_len
= mono_mb_get_pos (mb
) - clause
->try_offset
;
11026 clause
->data
.catch_class
= mono_defaults
.object_class
;
11028 clause
->handler_offset
= mono_mb_get_label (mb
);
11030 /* store exception at local 0 */
11031 mono_mb_emit_stloc (mb
, 0);
11032 mono_mb_emit_ldarg (mb
, param_count
- 1);
11033 mono_mb_emit_ldloc (mb
, 0);
11034 mono_mb_emit_byte (mb
, CEE_STIND_REF
);
11035 mono_mb_emit_branch (mb
, CEE_LEAVE
);
11037 clause
->handler_len
= mono_mb_get_pos (mb
) - clause
->handler_offset
;
11039 mono_mb_set_clauses (mb
, 1, clause
);
11041 mono_mb_patch_branch (mb
, pos_leave
);
11044 if (!MONO_TYPE_IS_VOID (sig
->ret
)) {
11045 mono_mb_emit_ldloc (mb
, 1);
11047 /* box the return value */
11048 if (MONO_TYPE_ISSTRUCT (sig
->ret
))
11049 mono_mb_emit_op (mb
, CEE_BOX
, mono_class_from_mono_type (sig
->ret
));
11052 mono_mb_emit_byte (mb
, CEE_RET
);
11054 res
= mono_mb_create_and_cache (cache
, method
, mb
, csig
, param_count
+ 16);
11061 * mono_marshal_free_dynamic_wrappers:
11063 * Free wrappers of the dynamic method METHOD.
11066 mono_marshal_free_dynamic_wrappers (MonoMethod
*method
)
11068 g_assert (method
->dynamic
);
11070 mono_marshal_lock ();
11072 * FIXME: We currently leak the wrappers. Freeing them would be tricky as
11073 * they could be shared with other methods ?
11075 if (method
->klass
->image
->runtime_invoke_direct_cache
)
11076 g_hash_table_remove (method
->klass
->image
->runtime_invoke_direct_cache
, method
);
11077 mono_marshal_unlock ();
11081 * mono_marshal_free_inflated_wrappers:
11083 * Free wrappers of the inflated method METHOD.
11087 signature_method_pair_matches_signature (gpointer key
, gpointer value
, gpointer user_data
)
11089 SignatureMethodPair
*pair
= (SignatureMethodPair
*)key
;
11090 MonoMethodSignature
*sig
= (MonoMethodSignature
*)user_data
;
11092 return mono_metadata_signature_equal (pair
->sig
, sig
);
11096 mono_marshal_free_inflated_wrappers (MonoMethod
*method
)
11098 MonoMethodSignature
*sig
= method
->signature
;
11100 g_assert (method
->is_inflated
);
11102 /* Ignore calls occuring late during cleanup. */
11103 if (!marshal_mutex_initialized
)
11106 mono_marshal_lock ();
11108 * FIXME: We currently leak the wrappers. Freeing them would be tricky as
11109 * they could be shared with other methods ?
11113 * indexed by MonoMethodSignature
11115 /* FIXME: This could remove unrelated wrappers as well */
11116 if (sig
&& method
->klass
->image
->delegate_begin_invoke_cache
)
11117 g_hash_table_remove (method
->klass
->image
->delegate_begin_invoke_cache
, sig
);
11118 if (sig
&& method
->klass
->image
->delegate_end_invoke_cache
)
11119 g_hash_table_remove (method
->klass
->image
->delegate_end_invoke_cache
, sig
);
11120 if (sig
&& method
->klass
->image
->delegate_invoke_cache
)
11121 g_hash_table_remove (method
->klass
->image
->delegate_invoke_cache
, sig
);
11122 if (sig
&& method
->klass
->image
->runtime_invoke_cache
)
11123 g_hash_table_remove (method
->klass
->image
->runtime_invoke_cache
, sig
);
11126 * indexed by SignatureMethodPair
11128 if (sig
&& method
->klass
->image
->delegate_abstract_invoke_cache
)
11129 g_hash_table_foreach_remove (method
->klass
->image
->delegate_abstract_invoke_cache
,
11130 signature_method_pair_matches_signature
, (gpointer
)sig
);
11133 * indexed by MonoMethod pointers
11135 if (method
->klass
->image
->runtime_invoke_direct_cache
)
11136 g_hash_table_remove (method
->klass
->image
->runtime_invoke_direct_cache
, method
);
11137 if (method
->klass
->image
->managed_wrapper_cache
)
11138 g_hash_table_remove (method
->klass
->image
->managed_wrapper_cache
, method
);
11139 if (method
->klass
->image
->native_wrapper_cache
)
11140 g_hash_table_remove (method
->klass
->image
->native_wrapper_cache
, method
);
11141 if (method
->klass
->image
->remoting_invoke_cache
)
11142 g_hash_table_remove (method
->klass
->image
->remoting_invoke_cache
, method
);
11143 if (method
->klass
->image
->synchronized_cache
)
11144 g_hash_table_remove (method
->klass
->image
->synchronized_cache
, method
);
11145 if (method
->klass
->image
->unbox_wrapper_cache
)
11146 g_hash_table_remove (method
->klass
->image
->unbox_wrapper_cache
, method
);
11147 if (method
->klass
->image
->cominterop_invoke_cache
)
11148 g_hash_table_remove (method
->klass
->image
->cominterop_invoke_cache
, method
);
11149 if (method
->klass
->image
->cominterop_wrapper_cache
)
11150 g_hash_table_remove (method
->klass
->image
->cominterop_wrapper_cache
, method
);
11151 if (method
->klass
->image
->thunk_invoke_cache
)
11152 g_hash_table_remove (method
->klass
->image
->thunk_invoke_cache
, method
);
11154 mono_marshal_unlock ();