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)
9 * Copyright 2011 Xamarin, Inc (http://www.xamarin.com)
21 #include "metadata/marshal.h"
22 #include "metadata/method-builder.h"
23 #include "metadata/tabledefs.h"
24 #include "metadata/exception.h"
25 #include "metadata/appdomain.h"
26 #include "mono/metadata/debug-helpers.h"
27 #include "mono/metadata/threadpool.h"
28 #include "mono/metadata/threads.h"
29 #include "mono/metadata/monitor.h"
30 #include "mono/metadata/metadata-internals.h"
31 #include "mono/metadata/domain-internals.h"
32 #include "mono/metadata/gc-internal.h"
33 #include "mono/metadata/threads-types.h"
34 #include "mono/metadata/string-icalls.h"
35 #include "mono/metadata/attrdefs.h"
36 #include "mono/metadata/gc-internal.h"
37 #include "mono/metadata/cominterop.h"
38 #include "mono/utils/mono-counters.h"
39 #include "mono/utils/mono-tls.h"
40 #include "mono/utils/mono-memory-model.h"
44 /* #define DEBUG_RUNTIME_CODE */
46 #define OPDEF(a,b,c,d,e,f,g,h,i,j) \
50 MONO_MARSHAL_NONE
, /* No marshalling needed */
51 MONO_MARSHAL_COPY
, /* Can be copied by value to the new domain */
52 MONO_MARSHAL_COPY_OUT
, /* out parameter that needs to be copied back to the original instance */
53 MONO_MARSHAL_SERIALIZE
/* Value needs to be serialized into the new domain */
54 } MonoXDomainMarshalType
;
57 #include "mono/cil/opcode.def"
62 struct _MonoRemotingMethods
{
64 MonoMethod
*invoke_with_check
;
65 MonoMethod
*xdomain_invoke
;
66 MonoMethod
*xdomain_dispatch
;
69 typedef struct _MonoRemotingMethods MonoRemotingMethods
;
72 * This mutex protects the various marshalling related caches in MonoImage
73 * and a few other data structures static to this file.
74 * Note that when this lock is held it is not possible to take other runtime
75 * locks like the loader lock.
77 #define mono_marshal_lock() EnterCriticalSection (&marshal_mutex)
78 #define mono_marshal_unlock() LeaveCriticalSection (&marshal_mutex)
79 static CRITICAL_SECTION marshal_mutex
;
80 static gboolean marshal_mutex_initialized
;
82 static MonoNativeTlsKey last_error_tls_id
;
84 static MonoNativeTlsKey load_type_info_tls_id
;
86 static gboolean use_aot_wrappers
;
89 delegate_hash_table_add (MonoDelegate
*d
);
92 emit_struct_conv (MonoMethodBuilder
*mb
, MonoClass
*klass
, gboolean to_object
);
95 emit_struct_conv_full (MonoMethodBuilder
*mb
, MonoClass
*klass
, gboolean to_object
, MonoMarshalNative string_encoding
);
98 mono_struct_delete_old (MonoClass
*klass
, char *ptr
);
101 mono_marshal_string_to_utf16 (MonoString
*s
);
104 mono_marshal_string_to_utf16_copy (MonoString
*s
);
107 mono_string_to_lpstr (MonoString
*string_obj
);
109 static MonoStringBuilder
*
110 mono_string_utf8_to_builder2 (char *text
);
112 static MonoStringBuilder
*
113 mono_string_utf16_to_builder2 (gunichar2
*text
);
116 mono_string_new_len_wrapper (const char *text
, guint length
);
119 mono_string_from_byvalwstr (gunichar2
*data
, int len
);
122 mono_byvalarray_to_array (MonoArray
*arr
, gpointer native_arr
, MonoClass
*eltype
, guint32 elnum
);
125 mono_array_to_byvalarray (gpointer native_arr
, MonoArray
*arr
, MonoClass
*eltype
, guint32 elnum
);
128 static MonoAsyncResult
*
129 mono_delegate_begin_invoke (MonoDelegate
*delegate
, gpointer
*params
);
132 mono_delegate_end_invoke (MonoDelegate
*delegate
, gpointer
*params
);
135 mono_marshal_xdomain_copy_out_value (MonoObject
*src
, MonoObject
*dst
);
138 mono_marshal_set_domain_by_id (gint32 id
, MonoBoolean push
);
141 mono_marshal_check_domain_image (gint32 domain_id
, MonoImage
*image
);
143 #ifndef DISABLE_REMOTING
145 mono_remoting_wrapper (MonoMethod
*method
, gpointer
*params
);
148 mono_upgrade_remote_class_wrapper (MonoReflectionType
*rtype
, MonoTransparentProxy
*tproxy
);
152 static MonoReflectionType
*
153 type_from_handle (MonoType
*handle
);
156 mono_marshal_set_last_error_windows (int error
);
158 static void init_safe_handle (void);
160 /* MonoMethod pointers to SafeHandle::DangerousAddRef and ::DangerousRelease */
161 static MonoMethod
*sh_dangerous_add_ref
;
162 static MonoMethod
*sh_dangerous_release
;
168 sh_dangerous_add_ref
= mono_class_get_method_from_name (
169 mono_defaults
.safehandle_class
, "DangerousAddRef", 1);
170 sh_dangerous_release
= mono_class_get_method_from_name (
171 mono_defaults
.safehandle_class
, "DangerousRelease", 0);
175 register_icall (gpointer func
, const char *name
, const char *sigstr
, gboolean save
)
177 MonoMethodSignature
*sig
= mono_create_icall_signature (sigstr
);
179 mono_register_jit_icall (func
, name
, sig
, save
);
182 static MonoMethodSignature
*
183 signature_dup (MonoImage
*image
, MonoMethodSignature
*sig
)
185 MonoMethodSignature
*res
;
188 res
= mono_metadata_signature_alloc (image
, sig
->param_count
);
189 sigsize
= MONO_SIZEOF_METHOD_SIGNATURE
+ sig
->param_count
* sizeof (MonoType
*);
190 memcpy (res
, sig
, sigsize
);
196 mono_signature_no_pinvoke (MonoMethod
*method
)
198 MonoMethodSignature
*sig
= mono_method_signature (method
);
200 sig
= signature_dup (method
->klass
->image
, sig
);
201 sig
->pinvoke
= FALSE
;
208 mono_marshal_init (void)
210 static gboolean module_initialized
= FALSE
;
212 if (!module_initialized
) {
213 module_initialized
= TRUE
;
214 InitializeCriticalSection (&marshal_mutex
);
215 marshal_mutex_initialized
= TRUE
;
216 mono_native_tls_alloc (&last_error_tls_id
, NULL
);
217 mono_native_tls_alloc (&load_type_info_tls_id
, NULL
);
219 register_icall (ves_icall_System_Threading_Thread_ResetAbort
, "ves_icall_System_Threading_Thread_ResetAbort", "void", TRUE
);
220 register_icall (mono_marshal_string_to_utf16
, "mono_marshal_string_to_utf16", "ptr obj", FALSE
);
221 register_icall (mono_marshal_string_to_utf16_copy
, "mono_marshal_string_to_utf16_copy", "ptr obj", FALSE
);
222 register_icall (mono_string_to_utf16
, "mono_string_to_utf16", "ptr obj", FALSE
);
223 register_icall (mono_string_from_utf16
, "mono_string_from_utf16", "obj ptr", FALSE
);
224 register_icall (mono_string_from_byvalwstr
, "mono_string_from_byvalwstr", "obj ptr int", FALSE
);
225 register_icall (mono_string_new_wrapper
, "mono_string_new_wrapper", "obj ptr", FALSE
);
226 register_icall (mono_string_new_len_wrapper
, "mono_string_new_len_wrapper", "obj ptr int", FALSE
);
227 register_icall (mono_string_to_utf8
, "mono_string_to_utf8", "ptr obj", FALSE
);
228 register_icall (mono_string_to_lpstr
, "mono_string_to_lpstr", "ptr obj", FALSE
);
229 register_icall (mono_string_to_ansibstr
, "mono_string_to_ansibstr", "ptr object", FALSE
);
230 register_icall (mono_string_builder_to_utf8
, "mono_string_builder_to_utf8", "ptr object", FALSE
);
231 register_icall (mono_string_builder_to_utf16
, "mono_string_builder_to_utf16", "ptr object", FALSE
);
232 register_icall (mono_array_to_savearray
, "mono_array_to_savearray", "ptr object", FALSE
);
233 register_icall (mono_array_to_lparray
, "mono_array_to_lparray", "ptr object", FALSE
);
234 register_icall (mono_free_lparray
, "mono_free_lparray", "void object ptr", FALSE
);
235 register_icall (mono_byvalarray_to_array
, "mono_byvalarray_to_array", "void object ptr ptr int32", FALSE
);
236 register_icall (mono_array_to_byvalarray
, "mono_array_to_byvalarray", "void ptr object ptr int32", FALSE
);
237 register_icall (mono_delegate_to_ftnptr
, "mono_delegate_to_ftnptr", "ptr object", FALSE
);
238 register_icall (mono_ftnptr_to_delegate
, "mono_ftnptr_to_delegate", "object ptr ptr", FALSE
);
239 register_icall (mono_marshal_asany
, "mono_marshal_asany", "ptr object int32 int32", FALSE
);
240 register_icall (mono_marshal_free_asany
, "mono_marshal_free_asany", "void object ptr int32 int32", FALSE
);
241 register_icall (mono_marshal_alloc
, "mono_marshal_alloc", "ptr int32", FALSE
);
242 register_icall (mono_marshal_free
, "mono_marshal_free", "void ptr", FALSE
);
243 register_icall (mono_marshal_set_last_error
, "mono_marshal_set_last_error", "void", FALSE
);
244 register_icall (mono_marshal_set_last_error_windows
, "mono_marshal_set_last_error_windows", "void int32", FALSE
);
245 register_icall (mono_string_utf8_to_builder
, "mono_string_utf8_to_builder", "void ptr ptr", FALSE
);
246 register_icall (mono_string_utf8_to_builder2
, "mono_string_utf8_to_builder2", "object ptr", FALSE
);
247 register_icall (mono_string_utf16_to_builder
, "mono_string_utf16_to_builder", "void ptr ptr", FALSE
);
248 register_icall (mono_string_utf16_to_builder2
, "mono_string_utf16_to_builder2", "object ptr", FALSE
);
249 register_icall (mono_marshal_free_array
, "mono_marshal_free_array", "void ptr int32", FALSE
);
250 register_icall (mono_string_to_byvalstr
, "mono_string_to_byvalstr", "void ptr ptr int32", FALSE
);
251 register_icall (mono_string_to_byvalwstr
, "mono_string_to_byvalwstr", "void ptr ptr int32", FALSE
);
252 register_icall (g_free
, "g_free", "void ptr", FALSE
);
253 register_icall (mono_object_isinst
, "mono_object_isinst", "object object ptr", FALSE
);
254 register_icall (mono_struct_delete_old
, "mono_struct_delete_old", "void ptr ptr", FALSE
);
255 register_icall (mono_delegate_begin_invoke
, "mono_delegate_begin_invoke", "object object ptr", FALSE
);
256 register_icall (mono_delegate_end_invoke
, "mono_delegate_end_invoke", "object object ptr", FALSE
);
257 register_icall (mono_marshal_xdomain_copy_value
, "mono_marshal_xdomain_copy_value", "object object", FALSE
);
258 register_icall (mono_marshal_xdomain_copy_out_value
, "mono_marshal_xdomain_copy_out_value", "void object object", FALSE
);
259 register_icall (mono_marshal_set_domain_by_id
, "mono_marshal_set_domain_by_id", "int32 int32 int32", FALSE
);
260 register_icall (mono_marshal_check_domain_image
, "mono_marshal_check_domain_image", "int32 int32 ptr", FALSE
);
261 register_icall (mono_compile_method
, "mono_compile_method", "ptr ptr", FALSE
);
262 register_icall (mono_context_get
, "mono_context_get", "object", FALSE
);
263 register_icall (mono_context_set
, "mono_context_set", "void object", FALSE
);
264 register_icall (type_from_handle
, "type_from_handle", "object ptr", FALSE
);
265 register_icall (mono_gc_wbarrier_generic_nostore
, "wb_generic", "void ptr", FALSE
);
266 register_icall (mono_gchandle_get_target
, "mono_gchandle_get_target", "object int32", TRUE
);
268 #ifndef DISABLE_REMOTING
269 register_icall (mono_remoting_wrapper
, "mono_remoting_wrapper", "object ptr ptr", FALSE
);
270 register_icall (mono_upgrade_remote_class_wrapper
, "mono_upgrade_remote_class_wrapper", "void object object", FALSE
);
272 mono_cominterop_init ();
277 mono_marshal_cleanup (void)
279 mono_cominterop_cleanup ();
281 mono_native_tls_free (load_type_info_tls_id
);
282 mono_native_tls_free (last_error_tls_id
);
283 DeleteCriticalSection (&marshal_mutex
);
284 marshal_mutex_initialized
= FALSE
;
287 #ifndef DISABLE_REMOTING
288 static MonoClass
*byte_array_class
;
289 static MonoMethod
*method_rs_serialize
, *method_rs_deserialize
, *method_exc_fixexc
, *method_rs_appdomain_target
;
290 static MonoMethod
*method_set_call_context
, *method_needs_context_sink
, *method_rs_serialize_exc
;
293 mono_remoting_marshal_init (void)
297 static gboolean module_initialized
= FALSE
;
299 if (!module_initialized
) {
300 klass
= mono_class_from_name (mono_defaults
.corlib
, "System.Runtime.Remoting", "RemotingServices");
301 method_rs_serialize
= mono_class_get_method_from_name (klass
, "SerializeCallData", -1);
302 method_rs_deserialize
= mono_class_get_method_from_name (klass
, "DeserializeCallData", -1);
303 method_rs_serialize_exc
= mono_class_get_method_from_name (klass
, "SerializeExceptionData", -1);
305 klass
= mono_defaults
.real_proxy_class
;
306 method_rs_appdomain_target
= mono_class_get_method_from_name (klass
, "GetAppDomainTarget", -1);
308 klass
= mono_defaults
.exception_class
;
309 method_exc_fixexc
= mono_class_get_method_from_name (klass
, "FixRemotingException", -1);
311 byte_array_class
= mono_array_class_get (mono_defaults
.byte_class
, 1);
313 klass
= mono_class_from_name (mono_defaults
.corlib
, "System.Runtime.Remoting.Messaging", "CallContext");
314 method_set_call_context
= mono_class_get_method_from_name (klass
, "SetCurrentCallContext", -1);
316 klass
= mono_class_from_name (mono_defaults
.corlib
, "System.Runtime.Remoting.Contexts", "Context");
317 method_needs_context_sink
= mono_class_get_method_from_name (klass
, "get_NeedsContextSink", -1);
319 module_initialized
= TRUE
;
325 mono_delegate_to_ftnptr (MonoDelegate
*delegate
)
327 MonoMethod
*method
, *wrapper
;
329 uint32_t target_handle
= 0;
334 if (delegate
->delegate_trampoline
)
335 return delegate
->delegate_trampoline
;
337 klass
= ((MonoObject
*)delegate
)->vtable
->klass
;
338 g_assert (klass
->delegate
);
340 method
= delegate
->method
;
342 if (mono_method_signature (method
)->pinvoke
) {
343 const char *exc_class
, *exc_arg
;
346 ftnptr
= mono_lookup_pinvoke_call (method
, &exc_class
, &exc_arg
);
348 g_assert (exc_class
);
349 mono_raise_exception (mono_exception_from_name_msg (mono_defaults
.corlib
, "System", exc_class
, exc_arg
));
354 if (delegate
->target
) {
355 /* Produce a location which can be embedded in JITted code */
356 target_handle
= mono_gchandle_new_weakref (delegate
->target
, FALSE
);
359 wrapper
= mono_marshal_get_managed_wrapper (method
, klass
, target_handle
);
361 delegate
->delegate_trampoline
= mono_compile_method (wrapper
);
363 // Add the delegate to the delegate hash table
364 delegate_hash_table_add (delegate
);
366 /* when the object is collected, collect the dynamic method, too */
367 mono_object_register_finalizer ((MonoObject
*)delegate
);
369 return delegate
->delegate_trampoline
;
373 * this hash table maps from a delegate trampoline object to a weak reference
374 * of the delegate. As an optimizations with a non-moving GC we store the
375 * object pointer itself, otherwise we use a GC handle.
377 static GHashTable
*delegate_hash_table
;
380 delegate_hash_table_new (void) {
381 return g_hash_table_new (NULL
, NULL
);
385 delegate_hash_table_remove (MonoDelegate
*d
)
387 guint32 gchandle
= 0;
389 mono_marshal_lock ();
390 if (delegate_hash_table
== NULL
)
391 delegate_hash_table
= delegate_hash_table_new ();
392 if (mono_gc_is_moving ())
393 gchandle
= GPOINTER_TO_UINT (g_hash_table_lookup (delegate_hash_table
, d
->delegate_trampoline
));
394 g_hash_table_remove (delegate_hash_table
, d
->delegate_trampoline
);
395 mono_marshal_unlock ();
396 if (mono_gc_is_moving ())
397 mono_gchandle_free (gchandle
);
401 delegate_hash_table_add (MonoDelegate
*d
)
404 guint32 old_gchandle
;
406 mono_marshal_lock ();
407 if (delegate_hash_table
== NULL
)
408 delegate_hash_table
= delegate_hash_table_new ();
409 if (mono_gc_is_moving ()) {
410 gchandle
= mono_gchandle_new_weakref ((MonoObject
*)d
, FALSE
);
411 old_gchandle
= GPOINTER_TO_UINT (g_hash_table_lookup (delegate_hash_table
, d
->delegate_trampoline
));
412 g_hash_table_insert (delegate_hash_table
, d
->delegate_trampoline
, GUINT_TO_POINTER (gchandle
));
414 mono_gchandle_free (old_gchandle
);
416 g_hash_table_insert (delegate_hash_table
, d
->delegate_trampoline
, d
);
418 mono_marshal_unlock ();
422 * mono_marshal_use_aot_wrappers:
424 * Instructs this module to use AOT compatible wrappers.
427 mono_marshal_use_aot_wrappers (gboolean use
)
429 use_aot_wrappers
= use
;
433 parse_unmanaged_function_pointer_attr (MonoClass
*klass
, MonoMethodPInvoke
*piinfo
)
435 static MonoClass
*UnmanagedFunctionPointerAttribute
;
436 MonoCustomAttrInfo
*cinfo
;
437 MonoReflectionUnmanagedFunctionPointerAttribute
*attr
;
439 if (!UnmanagedFunctionPointerAttribute
)
440 UnmanagedFunctionPointerAttribute
= mono_class_from_name (mono_defaults
.corlib
, "System.Runtime.InteropServices", "UnmanagedFunctionPointerAttribute");
442 /* The attribute is only available in Net 2.0 */
443 if (UnmanagedFunctionPointerAttribute
) {
445 * The pinvoke attributes are stored in a real custom attribute so we have to
448 cinfo
= mono_custom_attrs_from_class (klass
);
449 if (cinfo
&& !mono_runtime_get_no_exec ()) {
450 attr
= (MonoReflectionUnmanagedFunctionPointerAttribute
*)mono_custom_attrs_get_attr (cinfo
, UnmanagedFunctionPointerAttribute
);
452 piinfo
->piflags
= (attr
->call_conv
<< 8) | (attr
->charset
? (attr
->charset
- 1) * 2 : 1) | attr
->set_last_error
;
455 mono_custom_attrs_free (cinfo
);
461 mono_ftnptr_to_delegate (MonoClass
*klass
, gpointer ftn
)
469 mono_marshal_lock ();
470 if (delegate_hash_table
== NULL
)
471 delegate_hash_table
= delegate_hash_table_new ();
473 if (mono_gc_is_moving ()) {
474 gchandle
= GPOINTER_TO_UINT (g_hash_table_lookup (delegate_hash_table
, ftn
));
475 mono_marshal_unlock ();
477 d
= (MonoDelegate
*)mono_gchandle_get_target (gchandle
);
481 d
= g_hash_table_lookup (delegate_hash_table
, ftn
);
482 mono_marshal_unlock ();
485 /* This is a native function, so construct a delegate for it */
486 MonoMethodSignature
*sig
;
488 MonoMarshalSpec
**mspecs
;
489 MonoMethod
*invoke
= mono_get_delegate_invoke (klass
);
490 MonoMethodPInvoke piinfo
;
494 if (use_aot_wrappers
) {
495 wrapper
= mono_marshal_get_native_func_wrapper_aot (klass
);
496 this = mono_value_box (mono_domain_get (), mono_defaults
.int_class
, &ftn
);
498 memset (&piinfo
, 0, sizeof (piinfo
));
499 parse_unmanaged_function_pointer_attr (klass
, &piinfo
);
501 mspecs
= g_new0 (MonoMarshalSpec
*, mono_method_signature (invoke
)->param_count
+ 1);
502 mono_method_get_marshal_info (invoke
, mspecs
);
503 /* Freed below so don't alloc from mempool */
504 sig
= mono_metadata_signature_dup (mono_method_signature (invoke
));
507 wrapper
= mono_marshal_get_native_func_wrapper (klass
->image
, sig
, &piinfo
, mspecs
, ftn
);
510 for (i
= mono_method_signature (invoke
)->param_count
; i
>= 0; i
--)
512 mono_metadata_free_marshal_spec (mspecs
[i
]);
517 d
= (MonoDelegate
*)mono_object_new (mono_domain_get (), klass
);
518 mono_delegate_ctor_with_method ((MonoObject
*)d
, this, mono_compile_method (wrapper
), wrapper
);
521 if (d
->object
.vtable
->domain
!= mono_domain_get ())
522 mono_raise_exception (mono_get_exception_not_supported ("Delegates cannot be marshalled from native code into a domain other than their home domain"));
528 mono_delegate_free_ftnptr (MonoDelegate
*delegate
)
533 delegate_hash_table_remove (delegate
);
535 ptr
= (gpointer
)InterlockedExchangePointer (&delegate
->delegate_trampoline
, NULL
);
537 if (!delegate
->target
) {
538 /* The wrapper method is shared between delegates -> no need to free it */
545 ji
= mono_jit_info_table_find (mono_domain_get (), mono_get_addr_from_ftnptr (ptr
));
548 method_data
= ((MonoMethodWrapper
*)ji
->method
)->method_data
;
550 /*the target gchandle is the first entry after size and the wrapper itself.*/
551 gchandle
= GPOINTER_TO_UINT (method_data
[2]);
554 mono_gchandle_free (gchandle
);
556 mono_runtime_free_method (mono_object_domain (delegate
), ji
->method
);
561 mono_string_from_byvalwstr (gunichar2
*data
, int max_len
)
563 MonoDomain
*domain
= mono_domain_get ();
569 while (data
[len
]) len
++;
571 return mono_string_new_utf16 (domain
, data
, MIN (len
, max_len
));
575 mono_array_to_savearray (MonoArray
*array
)
580 g_assert_not_reached ();
585 mono_array_to_lparray (MonoArray
*array
)
587 gpointer
*nativeArray
= NULL
;
588 int nativeArraySize
= 0;
597 klass
= array
->obj
.vtable
->klass
;
599 switch (klass
->element_class
->byval_arg
.type
) {
601 g_assert_not_reached ();
604 case MONO_TYPE_CLASS
:
605 nativeArraySize
= array
->max_length
;
606 nativeArray
= malloc(sizeof(gpointer
) * nativeArraySize
);
607 for(i
= 0; i
< nativeArraySize
; ++i
)
608 nativeArray
[i
] = ves_icall_System_Runtime_InteropServices_Marshal_GetIUnknownForObjectInternal(((gpointer
*)array
->vector
)[i
]);
612 case MONO_TYPE_BOOLEAN
:
625 case MONO_TYPE_VALUETYPE
:
629 case MONO_TYPE_GENERICINST
:
630 case MONO_TYPE_OBJECT
:
631 case MONO_TYPE_ARRAY
:
632 case MONO_TYPE_SZARRAY
:
633 case MONO_TYPE_STRING
:
635 g_warning ("type 0x%x not handled", klass
->element_class
->byval_arg
.type
);
636 g_assert_not_reached ();
639 return array
->vector
;
643 mono_free_lparray (MonoArray
*array
, gpointer
* nativeArray
)
654 klass
= array
->obj
.vtable
->klass
;
656 switch (klass
->element_class
->byval_arg
.type
) {
657 case MONO_TYPE_CLASS
:
658 for(i
= 0; i
< array
->max_length
; ++i
)
659 mono_marshal_free_ccw(nativeArray
[i
]);
667 mono_byvalarray_to_array (MonoArray
*arr
, gpointer native_arr
, MonoClass
*elclass
, guint32 elnum
)
669 g_assert (arr
->obj
.vtable
->klass
->element_class
== mono_defaults
.char_class
);
671 if (elclass
== mono_defaults
.byte_class
) {
672 GError
*error
= NULL
;
676 ut
= g_utf8_to_utf16 (native_arr
, elnum
, NULL
, &items_written
, &error
);
679 memcpy (mono_array_addr (arr
, guint16
, 0), ut
, items_written
* sizeof (guint16
));
683 g_error_free (error
);
686 g_assert_not_reached ();
690 mono_array_to_byvalarray (gpointer native_arr
, MonoArray
*arr
, MonoClass
*elclass
, guint32 elnum
)
692 g_assert (arr
->obj
.vtable
->klass
->element_class
== mono_defaults
.char_class
);
694 if (elclass
== mono_defaults
.byte_class
) {
696 GError
*error
= NULL
;
698 as
= g_utf16_to_utf8 (mono_array_addr (arr
, gunichar2
, 0), mono_array_length (arr
), NULL
, NULL
, &error
);
700 MonoException
*exc
= mono_get_exception_argument ("string", error
->message
);
701 g_error_free (error
);
702 mono_raise_exception (exc
);
705 memcpy (native_arr
, as
, MIN (strlen (as
), elnum
));
708 g_assert_not_reached ();
713 mono_string_utf8_to_builder (MonoStringBuilder
*sb
, char *text
)
715 GError
*error
= NULL
;
725 ut
= g_utf8_to_utf16 (text
, l
, NULL
, &items_written
, &error
);
727 if (items_written
> mono_stringbuilder_capacity (sb
))
728 items_written
= mono_stringbuilder_capacity (sb
);
731 if (! sb
->str
|| sb
->str
== sb
->cached_str
)
732 MONO_OBJECT_SETREF (sb
, str
, mono_string_new_size (mono_domain_get (), items_written
));
734 memcpy (mono_string_chars (sb
->str
), ut
, items_written
* 2);
735 sb
->length
= items_written
;
736 sb
->cached_str
= NULL
;
738 g_error_free (error
);
744 mono_string_utf8_to_builder2 (char *text
)
747 MonoStringBuilder
*sb
;
748 static MonoClass
*string_builder_class
;
749 static MonoMethod
*sb_ctor
;
756 if (!string_builder_class
) {
757 MonoMethodDesc
*desc
;
759 string_builder_class
= mono_class_from_name (mono_defaults
.corlib
, "System.Text", "StringBuilder");
760 g_assert (string_builder_class
);
761 desc
= mono_method_desc_new (":.ctor(int)", FALSE
);
762 sb_ctor
= mono_method_desc_search_in_class (desc
, string_builder_class
);
764 mono_method_desc_free (desc
);
769 sb
= (MonoStringBuilder
*)mono_object_new (mono_domain_get (), string_builder_class
);
772 mono_runtime_invoke (sb_ctor
, sb
, args
, &exc
);
775 mono_string_utf8_to_builder (sb
, text
);
781 * FIXME: This routine does not seem to do what it seems to do
782 * the @text is never copied into the string builder
785 mono_string_utf16_to_builder (MonoStringBuilder
*sb
, gunichar2
*text
)
792 g_assert (mono_string_chars (sb
->str
) == text
);
794 for (len
= 0; text
[len
] != 0; ++len
)
801 mono_string_utf16_to_builder2 (gunichar2
*text
)
804 MonoStringBuilder
*sb
;
805 static MonoClass
*string_builder_class
;
806 static MonoMethod
*sb_ctor
;
813 if (!string_builder_class
) {
814 MonoMethodDesc
*desc
;
816 string_builder_class
= mono_class_from_name (mono_defaults
.corlib
, "System.Text", "StringBuilder");
817 g_assert (string_builder_class
);
818 desc
= mono_method_desc_new (":.ctor(int)", FALSE
);
819 sb_ctor
= mono_method_desc_search_in_class (desc
, string_builder_class
);
821 mono_method_desc_free (desc
);
824 for (len
= 0; text
[len
] != 0; ++len
)
827 sb
= (MonoStringBuilder
*)mono_object_new (mono_domain_get (), string_builder_class
);
830 mono_runtime_invoke (sb_ctor
, sb
, args
, &exc
);
834 memcpy (mono_string_chars (sb
->str
), text
, len
* 2);
840 * mono_string_builder_to_utf8:
841 * @sb: the string builder
843 * Converts to utf8 the contents of the MonoStringBuilder.
845 * Returns: a utf8 string with the contents of the StringBuilder.
847 * The return value must be released with g_free.
850 mono_string_builder_to_utf8 (MonoStringBuilder
*sb
)
852 GError
*error
= NULL
;
853 gchar
*tmp
, *res
= NULL
;
858 if ((sb
->str
== sb
->cached_str
) && (sb
->str
->length
== 0)) {
860 * The sb could have been allocated with the default capacity and be empty.
861 * we need to alloc a buffer of the default capacity in this case.
863 MONO_OBJECT_SETREF (sb
, str
, mono_string_new_size (mono_domain_get (), 16));
864 sb
->cached_str
= NULL
;
867 tmp
= g_utf16_to_utf8 (mono_string_chars (sb
->str
), sb
->length
, NULL
, NULL
, &error
);
869 g_error_free (error
);
870 mono_raise_exception (mono_get_exception_execution_engine ("Failed to convert StringBuilder from utf16 to utf8"));
872 res
= mono_marshal_alloc (mono_stringbuilder_capacity (sb
) + 1);
873 memcpy (res
, tmp
, sb
->length
+ 1);
881 * mono_string_builder_to_utf16:
882 * @sb: the string builder
884 * Converts to utf16 the contents of the MonoStringBuilder.
886 * Returns: a utf16 string with the contents of the StringBuilder.
888 * The return value must not be freed.
891 mono_string_builder_to_utf16 (MonoStringBuilder
*sb
)
899 * The stringbuilder might not have ownership of this string. If this is
900 * the case, we must duplicate the string, so that we don't munge immutable
903 if (sb
->str
== sb
->cached_str
) {
905 * The sb could have been allocated with the default capacity and be empty.
906 * we need to alloc a buffer of the default capacity in this case.
908 if (sb
->str
->length
== 0)
909 MONO_OBJECT_SETREF (sb
, str
, mono_string_new_size (mono_domain_get (), 16));
911 MONO_OBJECT_SETREF (sb
, str
, mono_string_new_utf16 (mono_domain_get (), mono_string_chars (sb
->str
), mono_stringbuilder_capacity (sb
)));
912 sb
->cached_str
= NULL
;
916 *(mono_string_chars (sb
->str
)) = '\0';
918 return mono_string_chars (sb
->str
);
922 mono_string_to_lpstr (MonoString
*s
)
927 GError
*error
= NULL
;
933 as
= CoTaskMemAlloc (1);
938 tmp
= g_utf16_to_utf8 (mono_string_chars (s
), s
->length
, NULL
, &len
, &error
);
940 MonoException
*exc
= mono_get_exception_argument ("string", error
->message
);
941 g_error_free (error
);
942 mono_raise_exception(exc
);
945 as
= CoTaskMemAlloc (len
+ 1);
946 memcpy (as
, tmp
, len
+ 1);
951 return mono_string_to_utf8 (s
);
956 mono_string_to_ansibstr (MonoString
*string_obj
)
958 g_error ("UnmanagedMarshal.BStr is not implemented.");
963 * mono_string_to_byvalstr:
964 * @dst: Where to store the null-terminated utf8 decoded string.
965 * @src: the MonoString to copy.
966 * @size: the maximum number of bytes to copy.
968 * Copies the MonoString pointed to by @src as a utf8 string
969 * into @dst, it copies at most @size bytes into the destination.
972 mono_string_to_byvalstr (gpointer dst
, MonoString
*src
, int size
)
977 g_assert (dst
!= NULL
);
980 memset (dst
, 0, size
);
984 s
= mono_string_to_utf8 (src
);
985 len
= MIN (size
, strlen (s
));
988 memcpy (dst
, s
, len
);
993 * mono_string_to_byvalwstr:
994 * @dst: Where to store the null-terminated utf16 decoded string.
995 * @src: the MonoString to copy.
996 * @size: the maximum number of bytes to copy.
998 * Copies the MonoString pointed to by @src as a utf16 string into
999 * @dst, it copies at most @size bytes into the destination (including
1000 * a terminating 16-bit zero terminator).
1003 mono_string_to_byvalwstr (gpointer dst
, MonoString
*src
, int size
)
1007 g_assert (dst
!= NULL
);
1008 g_assert (size
> 1);
1011 memset (dst
, 0, size
* 2);
1015 len
= MIN (size
, (mono_string_length (src
)));
1016 memcpy (dst
, mono_string_chars (src
), size
* 2);
1017 if (size
<= mono_string_length (src
))
1019 *((gunichar2
*) dst
+ len
) = 0;
1023 mono_string_new_len_wrapper (const char *text
, guint length
)
1025 return mono_string_new_len (mono_domain_get (), text
, length
);
1029 #ifndef DISABLE_REMOTING
1031 mono_mb_emit_proxy_check (MonoMethodBuilder
*mb
, int branch_code
)
1034 mono_mb_emit_ldflda (mb
, G_STRUCT_OFFSET (MonoObject
, vtable
));
1035 mono_mb_emit_byte (mb
, CEE_LDIND_I
);
1036 mono_mb_emit_icon (mb
, G_STRUCT_OFFSET (MonoVTable
, klass
));
1037 mono_mb_emit_byte (mb
, CEE_ADD
);
1038 mono_mb_emit_byte (mb
, CEE_LDIND_I
);
1039 mono_mb_emit_byte (mb
, MONO_CUSTOM_PREFIX
);
1040 mono_mb_emit_byte (mb
, CEE_MONO_CLASSCONST
);
1041 mono_mb_emit_i4 (mb
, mono_mb_add_data (mb
, mono_defaults
.transparent_proxy_class
));
1042 pos
= mono_mb_emit_branch (mb
, branch_code
);
1047 mono_mb_emit_xdomain_check (MonoMethodBuilder
*mb
, int branch_code
)
1050 mono_mb_emit_ldflda (mb
, G_STRUCT_OFFSET (MonoTransparentProxy
, rp
));
1051 mono_mb_emit_byte (mb
, CEE_LDIND_REF
);
1052 mono_mb_emit_ldflda (mb
, G_STRUCT_OFFSET (MonoRealProxy
, target_domain_id
));
1053 mono_mb_emit_byte (mb
, CEE_LDIND_I4
);
1054 mono_mb_emit_icon (mb
, -1);
1055 pos
= mono_mb_emit_branch (mb
, branch_code
);
1060 mono_mb_emit_contextbound_check (MonoMethodBuilder
*mb
, int branch_code
)
1062 static int offset
= -1;
1066 mono_marshal_find_bitfield_offset (MonoClass
, contextbound
, &offset
, &mask
);
1068 mono_mb_emit_ldflda (mb
, G_STRUCT_OFFSET (MonoTransparentProxy
, remote_class
));
1069 mono_mb_emit_byte (mb
, CEE_LDIND_REF
);
1070 mono_mb_emit_ldflda (mb
, G_STRUCT_OFFSET (MonoRemoteClass
, proxy_class
));
1071 mono_mb_emit_byte (mb
, CEE_LDIND_REF
);
1072 mono_mb_emit_ldflda (mb
, offset
);
1073 mono_mb_emit_byte (mb
, CEE_LDIND_U1
);
1074 mono_mb_emit_icon (mb
, mask
);
1075 mono_mb_emit_byte (mb
, CEE_AND
);
1076 mono_mb_emit_icon (mb
, 0);
1077 return mono_mb_emit_branch (mb
, branch_code
);
1079 #endif /* DISABLE_REMOTING */
1082 * mono_mb_emit_exception_marshal_directive:
1084 * This function assumes ownership of MSG, which should be malloc-ed.
1087 mono_mb_emit_exception_marshal_directive (MonoMethodBuilder
*mb
, char *msg
)
1092 s
= mono_image_strdup (mb
->method
->klass
->image
, msg
);
1097 mono_mb_emit_exception_full (mb
, "System.Runtime.InteropServices", "MarshalDirectiveException", s
);
1101 mono_type_to_ldind (MonoType
*type
)
1107 switch (type
->type
) {
1109 return CEE_LDIND_I1
;
1111 case MONO_TYPE_BOOLEAN
:
1112 return CEE_LDIND_U1
;
1114 return CEE_LDIND_I2
;
1116 case MONO_TYPE_CHAR
:
1117 return CEE_LDIND_U2
;
1119 return CEE_LDIND_I4
;
1121 return CEE_LDIND_U4
;
1125 case MONO_TYPE_FNPTR
:
1127 case MONO_TYPE_CLASS
:
1128 case MONO_TYPE_STRING
:
1129 case MONO_TYPE_OBJECT
:
1130 case MONO_TYPE_SZARRAY
:
1131 case MONO_TYPE_ARRAY
:
1132 return CEE_LDIND_REF
;
1135 return CEE_LDIND_I8
;
1137 return CEE_LDIND_R4
;
1139 return CEE_LDIND_R8
;
1140 case MONO_TYPE_VALUETYPE
:
1141 if (type
->data
.klass
->enumtype
) {
1142 type
= mono_class_enum_basetype (type
->data
.klass
);
1146 case MONO_TYPE_TYPEDBYREF
:
1148 case MONO_TYPE_GENERICINST
:
1149 type
= &type
->data
.generic_class
->container_class
->byval_arg
;
1152 g_error ("unknown type 0x%02x in type_to_ldind", type
->type
);
1158 mono_type_to_stind (MonoType
*type
)
1164 switch (type
->type
) {
1167 case MONO_TYPE_BOOLEAN
:
1168 return CEE_STIND_I1
;
1171 case MONO_TYPE_CHAR
:
1172 return CEE_STIND_I2
;
1175 return CEE_STIND_I4
;
1179 case MONO_TYPE_FNPTR
:
1181 case MONO_TYPE_CLASS
:
1182 case MONO_TYPE_STRING
:
1183 case MONO_TYPE_OBJECT
:
1184 case MONO_TYPE_SZARRAY
:
1185 case MONO_TYPE_ARRAY
:
1186 return CEE_STIND_REF
;
1189 return CEE_STIND_I8
;
1191 return CEE_STIND_R4
;
1193 return CEE_STIND_R8
;
1194 case MONO_TYPE_VALUETYPE
:
1195 if (type
->data
.klass
->enumtype
) {
1196 type
= mono_class_enum_basetype (type
->data
.klass
);
1200 case MONO_TYPE_TYPEDBYREF
:
1202 case MONO_TYPE_GENERICINST
:
1203 type
= &type
->data
.generic_class
->container_class
->byval_arg
;
1206 g_error ("unknown type 0x%02x in type_to_stind", type
->type
);
1212 emit_ptr_to_object_conv (MonoMethodBuilder
*mb
, MonoType
*type
, MonoMarshalConv conv
, MonoMarshalSpec
*mspec
)
1215 case MONO_MARSHAL_CONV_BOOL_I4
:
1216 mono_mb_emit_ldloc (mb
, 1);
1217 mono_mb_emit_ldloc (mb
, 0);
1218 mono_mb_emit_byte (mb
, CEE_LDIND_I4
);
1219 mono_mb_emit_byte (mb
, CEE_BRFALSE_S
);
1220 mono_mb_emit_byte (mb
, 3);
1221 mono_mb_emit_byte (mb
, CEE_LDC_I4_1
);
1222 mono_mb_emit_byte (mb
, CEE_BR_S
);
1223 mono_mb_emit_byte (mb
, 1);
1224 mono_mb_emit_byte (mb
, CEE_LDC_I4_0
);
1225 mono_mb_emit_byte (mb
, CEE_STIND_I1
);
1227 case MONO_MARSHAL_CONV_BOOL_VARIANTBOOL
:
1228 mono_mb_emit_ldloc (mb
, 1);
1229 mono_mb_emit_ldloc (mb
, 0);
1230 mono_mb_emit_byte (mb
, CEE_LDIND_I2
);
1231 mono_mb_emit_byte (mb
, CEE_BRFALSE_S
);
1232 mono_mb_emit_byte (mb
, 3);
1233 mono_mb_emit_byte (mb
, CEE_LDC_I4_1
);
1234 mono_mb_emit_byte (mb
, CEE_BR_S
);
1235 mono_mb_emit_byte (mb
, 1);
1236 mono_mb_emit_byte (mb
, CEE_LDC_I4_0
);
1237 mono_mb_emit_byte (mb
, CEE_STIND_I1
);
1239 case MONO_MARSHAL_CONV_ARRAY_BYVALARRAY
: {
1240 MonoClass
*eklass
= NULL
;
1243 if (type
->type
== MONO_TYPE_SZARRAY
) {
1244 eklass
= type
->data
.klass
;
1246 g_assert_not_reached ();
1249 esize
= mono_class_native_size (eklass
, NULL
);
1251 /* create a new array */
1252 mono_mb_emit_ldloc (mb
, 1);
1253 mono_mb_emit_icon (mb
, mspec
->data
.array_data
.num_elem
);
1254 mono_mb_emit_op (mb
, CEE_NEWARR
, eklass
);
1255 mono_mb_emit_byte (mb
, CEE_STIND_I
);
1257 if (eklass
->blittable
) {
1258 /* copy the elements */
1259 mono_mb_emit_ldloc (mb
, 1);
1260 mono_mb_emit_byte (mb
, CEE_LDIND_I
);
1261 mono_mb_emit_icon (mb
, G_STRUCT_OFFSET (MonoArray
, vector
));
1262 mono_mb_emit_byte (mb
, CEE_ADD
);
1263 mono_mb_emit_ldloc (mb
, 0);
1264 mono_mb_emit_icon (mb
, mspec
->data
.array_data
.num_elem
* esize
);
1265 mono_mb_emit_byte (mb
, CEE_PREFIX1
);
1266 mono_mb_emit_byte (mb
, CEE_CPBLK
);
1269 int array_var
, src_var
, dst_var
, index_var
;
1270 guint32 label2
, label3
;
1272 array_var
= mono_mb_add_local (mb
, &mono_defaults
.object_class
->byval_arg
);
1273 src_var
= mono_mb_add_local (mb
, &mono_defaults
.int_class
->byval_arg
);
1274 dst_var
= mono_mb_add_local (mb
, &mono_defaults
.int_class
->byval_arg
);
1277 mono_mb_emit_ldloc (mb
, 1);
1278 mono_mb_emit_byte (mb
, CEE_LDIND_REF
);
1279 mono_mb_emit_stloc (mb
, array_var
);
1281 /* save the old src pointer */
1282 mono_mb_emit_ldloc (mb
, 0);
1283 mono_mb_emit_stloc (mb
, src_var
);
1284 /* save the old dst pointer */
1285 mono_mb_emit_ldloc (mb
, 1);
1286 mono_mb_emit_stloc (mb
, dst_var
);
1288 /* Emit marshalling loop */
1289 index_var
= mono_mb_add_local (mb
, &mono_defaults
.int_class
->byval_arg
);
1290 mono_mb_emit_byte (mb
, CEE_LDC_I4_0
);
1291 mono_mb_emit_stloc (mb
, index_var
);
1294 label2
= mono_mb_get_label (mb
);
1295 mono_mb_emit_ldloc (mb
, index_var
);
1296 mono_mb_emit_ldloc (mb
, array_var
);
1297 mono_mb_emit_byte (mb
, CEE_LDLEN
);
1298 label3
= mono_mb_emit_branch (mb
, CEE_BGE
);
1300 /* src is already set */
1303 mono_mb_emit_ldloc (mb
, array_var
);
1304 mono_mb_emit_ldloc (mb
, index_var
);
1305 mono_mb_emit_op (mb
, CEE_LDELEMA
, eklass
);
1306 mono_mb_emit_stloc (mb
, 1);
1308 /* Do the conversion */
1309 emit_struct_conv (mb
, eklass
, TRUE
);
1312 mono_mb_emit_add_to_local (mb
, index_var
, 1);
1314 mono_mb_emit_branch_label (mb
, CEE_BR
, label2
);
1316 mono_mb_patch_branch (mb
, label3
);
1318 /* restore the old src pointer */
1319 mono_mb_emit_ldloc (mb
, src_var
);
1320 mono_mb_emit_stloc (mb
, 0);
1321 /* restore the old dst pointer */
1322 mono_mb_emit_ldloc (mb
, dst_var
);
1323 mono_mb_emit_stloc (mb
, 1);
1327 case MONO_MARSHAL_CONV_ARRAY_BYVALCHARARRAY
: {
1328 MonoClass
*eclass
= mono_defaults
.char_class
;
1330 /* create a new array */
1331 mono_mb_emit_ldloc (mb
, 1);
1332 mono_mb_emit_icon (mb
, mspec
->data
.array_data
.num_elem
);
1333 mono_mb_emit_op (mb
, CEE_NEWARR
, eclass
);
1334 mono_mb_emit_byte (mb
, CEE_STIND_REF
);
1336 mono_mb_emit_ldloc (mb
, 1);
1337 mono_mb_emit_byte (mb
, CEE_LDIND_REF
);
1338 mono_mb_emit_ldloc (mb
, 0);
1339 mono_mb_emit_ptr (mb
, mono_defaults
.byte_class
);
1340 mono_mb_emit_icon (mb
, mspec
->data
.array_data
.num_elem
);
1341 mono_mb_emit_icall (mb
, mono_byvalarray_to_array
);
1344 case MONO_MARSHAL_CONV_STR_BYVALSTR
:
1345 mono_mb_emit_ldloc (mb
, 1);
1346 mono_mb_emit_ldloc (mb
, 0);
1347 mono_mb_emit_icall (mb
, mono_string_new_wrapper
);
1348 mono_mb_emit_byte (mb
, CEE_STIND_REF
);
1350 case MONO_MARSHAL_CONV_STR_BYVALWSTR
:
1351 if (mspec
&& mspec
->native
== MONO_NATIVE_BYVALTSTR
&& mspec
->data
.array_data
.num_elem
) {
1352 mono_mb_emit_ldloc (mb
, 1);
1353 mono_mb_emit_ldloc (mb
, 0);
1354 mono_mb_emit_icon (mb
, mspec
->data
.array_data
.num_elem
);
1355 mono_mb_emit_icall (mb
, mono_string_from_byvalwstr
);
1357 mono_mb_emit_ldloc (mb
, 1);
1358 mono_mb_emit_ldloc (mb
, 0);
1359 mono_mb_emit_icall (mb
, mono_string_from_utf16
);
1361 mono_mb_emit_byte (mb
, CEE_STIND_REF
);
1363 case MONO_MARSHAL_CONV_STR_LPTSTR
:
1364 mono_mb_emit_ldloc (mb
, 1);
1365 mono_mb_emit_ldloc (mb
, 0);
1366 mono_mb_emit_byte (mb
, CEE_LDIND_I
);
1368 mono_mb_emit_icall (mb
, mono_string_from_utf16
);
1370 mono_mb_emit_icall (mb
, mono_string_new_wrapper
);
1372 mono_mb_emit_byte (mb
, CEE_STIND_REF
);
1374 case MONO_MARSHAL_CONV_STR_LPSTR
:
1375 mono_mb_emit_ldloc (mb
, 1);
1376 mono_mb_emit_ldloc (mb
, 0);
1377 mono_mb_emit_byte (mb
, CEE_LDIND_I
);
1378 mono_mb_emit_icall (mb
, mono_string_new_wrapper
);
1379 mono_mb_emit_byte (mb
, CEE_STIND_REF
);
1381 case MONO_MARSHAL_CONV_STR_LPWSTR
:
1382 mono_mb_emit_ldloc (mb
, 1);
1383 mono_mb_emit_ldloc (mb
, 0);
1384 mono_mb_emit_byte (mb
, CEE_LDIND_I
);
1385 mono_mb_emit_icall (mb
, mono_string_from_utf16
);
1386 mono_mb_emit_byte (mb
, CEE_STIND_REF
);
1388 case MONO_MARSHAL_CONV_OBJECT_STRUCT
: {
1389 MonoClass
*klass
= mono_class_from_mono_type (type
);
1390 int src_var
, dst_var
;
1392 src_var
= mono_mb_add_local (mb
, &mono_defaults
.int_class
->byval_arg
);
1393 dst_var
= mono_mb_add_local (mb
, &mono_defaults
.int_class
->byval_arg
);
1395 /* *dst = new object */
1396 mono_mb_emit_ldloc (mb
, 1);
1397 mono_mb_emit_byte (mb
, MONO_CUSTOM_PREFIX
);
1398 mono_mb_emit_op (mb
, CEE_MONO_NEWOBJ
, klass
);
1399 mono_mb_emit_byte (mb
, CEE_STIND_REF
);
1401 /* save the old src pointer */
1402 mono_mb_emit_ldloc (mb
, 0);
1403 mono_mb_emit_stloc (mb
, src_var
);
1404 /* save the old dst pointer */
1405 mono_mb_emit_ldloc (mb
, 1);
1406 mono_mb_emit_stloc (mb
, dst_var
);
1408 /* dst = pointer to newly created object data */
1409 mono_mb_emit_ldloc (mb
, 1);
1410 mono_mb_emit_byte (mb
, CEE_LDIND_I
);
1411 mono_mb_emit_icon (mb
, sizeof (MonoObject
));
1412 mono_mb_emit_byte (mb
, CEE_ADD
);
1413 mono_mb_emit_stloc (mb
, 1);
1415 emit_struct_conv (mb
, klass
, TRUE
);
1417 /* restore the old src pointer */
1418 mono_mb_emit_ldloc (mb
, src_var
);
1419 mono_mb_emit_stloc (mb
, 0);
1420 /* restore the old dst pointer */
1421 mono_mb_emit_ldloc (mb
, dst_var
);
1422 mono_mb_emit_stloc (mb
, 1);
1425 case MONO_MARSHAL_CONV_DEL_FTN
: {
1426 MonoClass
*klass
= mono_class_from_mono_type (type
);
1428 mono_mb_emit_ldloc (mb
, 1);
1429 mono_mb_emit_byte (mb
, MONO_CUSTOM_PREFIX
);
1430 mono_mb_emit_op (mb
, CEE_MONO_CLASSCONST
, klass
);
1431 mono_mb_emit_ldloc (mb
, 0);
1432 mono_mb_emit_byte (mb
, CEE_LDIND_I
);
1433 mono_mb_emit_icall (mb
, mono_ftnptr_to_delegate
);
1434 mono_mb_emit_byte (mb
, CEE_STIND_REF
);
1437 case MONO_MARSHAL_CONV_ARRAY_LPARRAY
:
1438 g_error ("Structure field of type %s can't be marshalled as LPArray", mono_class_from_mono_type (type
)->name
);
1442 case MONO_MARSHAL_CONV_OBJECT_INTERFACE
:
1443 case MONO_MARSHAL_CONV_OBJECT_IUNKNOWN
:
1444 case MONO_MARSHAL_CONV_OBJECT_IDISPATCH
:
1445 mono_cominterop_emit_ptr_to_object_conv (mb
, type
, conv
, mspec
);
1447 #endif /* DISABLE_COM */
1449 case MONO_MARSHAL_CONV_SAFEHANDLE
: {
1451 * Passing SafeHandles as ref does not allow the unmanaged code
1452 * to change the SafeHandle value. If the value is changed,
1453 * we should issue a diagnostic exception (NotSupportedException)
1454 * that informs the user that changes to handles in unmanaged code
1457 * Since we currently have no access to the original
1458 * SafeHandle that was used during the marshalling,
1459 * for now we just ignore this, and ignore/discard any
1460 * changes that might have happened to the handle.
1465 case MONO_MARSHAL_CONV_HANDLEREF
: {
1467 * Passing HandleRefs in a struct that is ref()ed does not
1468 * copy the values back to the HandleRef
1473 case MONO_MARSHAL_CONV_STR_BSTR
:
1474 case MONO_MARSHAL_CONV_STR_ANSIBSTR
:
1475 case MONO_MARSHAL_CONV_STR_TBSTR
:
1476 case MONO_MARSHAL_CONV_ARRAY_SAVEARRAY
:
1478 char *msg
= g_strdup_printf ("marshaling conversion %d not implemented", conv
);
1480 mono_mb_emit_exception_marshal_directive (mb
, msg
);
1487 conv_to_icall (MonoMarshalConv conv
)
1490 case MONO_MARSHAL_CONV_STR_LPWSTR
:
1491 return mono_marshal_string_to_utf16
;
1492 case MONO_MARSHAL_CONV_LPWSTR_STR
:
1493 return mono_string_from_utf16
;
1494 case MONO_MARSHAL_CONV_LPTSTR_STR
:
1495 return mono_string_new_wrapper
;
1496 case MONO_MARSHAL_CONV_LPSTR_STR
:
1497 return mono_string_new_wrapper
;
1498 case MONO_MARSHAL_CONV_STR_LPTSTR
:
1500 return mono_marshal_string_to_utf16
;
1502 return mono_string_to_lpstr
;
1504 case MONO_MARSHAL_CONV_STR_LPSTR
:
1505 return mono_string_to_lpstr
;
1506 case MONO_MARSHAL_CONV_STR_BSTR
:
1507 return mono_string_to_bstr
;
1508 case MONO_MARSHAL_CONV_BSTR_STR
:
1509 return mono_string_from_bstr
;
1510 case MONO_MARSHAL_CONV_STR_TBSTR
:
1511 case MONO_MARSHAL_CONV_STR_ANSIBSTR
:
1512 return mono_string_to_ansibstr
;
1513 case MONO_MARSHAL_CONV_SB_LPSTR
:
1514 return mono_string_builder_to_utf8
;
1515 case MONO_MARSHAL_CONV_SB_LPTSTR
:
1517 return mono_string_builder_to_utf16
;
1519 return mono_string_builder_to_utf8
;
1521 case MONO_MARSHAL_CONV_SB_LPWSTR
:
1522 return mono_string_builder_to_utf16
;
1523 case MONO_MARSHAL_CONV_ARRAY_SAVEARRAY
:
1524 return mono_array_to_savearray
;
1525 case MONO_MARSHAL_CONV_ARRAY_LPARRAY
:
1526 return mono_array_to_lparray
;
1527 case MONO_MARSHAL_FREE_LPARRAY
:
1528 return mono_free_lparray
;
1529 case MONO_MARSHAL_CONV_DEL_FTN
:
1530 return mono_delegate_to_ftnptr
;
1531 case MONO_MARSHAL_CONV_FTN_DEL
:
1532 return mono_ftnptr_to_delegate
;
1533 case MONO_MARSHAL_CONV_LPSTR_SB
:
1534 return mono_string_utf8_to_builder
;
1535 case MONO_MARSHAL_CONV_LPTSTR_SB
:
1537 return mono_string_utf16_to_builder
;
1539 return mono_string_utf8_to_builder
;
1541 case MONO_MARSHAL_CONV_LPWSTR_SB
:
1542 return mono_string_utf16_to_builder
;
1543 case MONO_MARSHAL_FREE_ARRAY
:
1544 return mono_marshal_free_array
;
1545 case MONO_MARSHAL_CONV_STR_BYVALSTR
:
1546 return mono_string_to_byvalstr
;
1547 case MONO_MARSHAL_CONV_STR_BYVALWSTR
:
1548 return mono_string_to_byvalwstr
;
1550 g_assert_not_reached ();
1557 emit_object_to_ptr_conv (MonoMethodBuilder
*mb
, MonoType
*type
, MonoMarshalConv conv
, MonoMarshalSpec
*mspec
)
1562 case MONO_MARSHAL_CONV_BOOL_I4
:
1563 mono_mb_emit_ldloc (mb
, 1);
1564 mono_mb_emit_ldloc (mb
, 0);
1565 mono_mb_emit_byte (mb
, CEE_LDIND_U1
);
1566 mono_mb_emit_byte (mb
, CEE_STIND_I4
);
1568 case MONO_MARSHAL_CONV_BOOL_VARIANTBOOL
:
1569 mono_mb_emit_ldloc (mb
, 1);
1570 mono_mb_emit_ldloc (mb
, 0);
1571 mono_mb_emit_byte (mb
, CEE_LDIND_U1
);
1572 mono_mb_emit_byte (mb
, CEE_NEG
);
1573 mono_mb_emit_byte (mb
, CEE_STIND_I2
);
1575 case MONO_MARSHAL_CONV_STR_LPWSTR
:
1576 case MONO_MARSHAL_CONV_STR_LPSTR
:
1577 case MONO_MARSHAL_CONV_STR_LPTSTR
:
1578 case MONO_MARSHAL_CONV_STR_BSTR
:
1579 case MONO_MARSHAL_CONV_STR_ANSIBSTR
:
1580 case MONO_MARSHAL_CONV_STR_TBSTR
: {
1583 /* free space if free == true */
1584 mono_mb_emit_ldloc (mb
, 2);
1585 pos
= mono_mb_emit_short_branch (mb
, CEE_BRFALSE_S
);
1586 mono_mb_emit_ldloc (mb
, 1);
1587 mono_mb_emit_byte (mb
, CEE_LDIND_I
);
1588 mono_mb_emit_icall (mb
, g_free
);
1589 mono_mb_patch_short_branch (mb
, pos
);
1591 mono_mb_emit_ldloc (mb
, 1);
1592 mono_mb_emit_ldloc (mb
, 0);
1593 mono_mb_emit_byte (mb
, CEE_LDIND_REF
);
1594 mono_mb_emit_icall (mb
, conv_to_icall (conv
));
1595 mono_mb_emit_byte (mb
, CEE_STIND_I
);
1598 case MONO_MARSHAL_CONV_ARRAY_SAVEARRAY
:
1599 case MONO_MARSHAL_CONV_ARRAY_LPARRAY
:
1600 case MONO_MARSHAL_CONV_DEL_FTN
:
1601 mono_mb_emit_ldloc (mb
, 1);
1602 mono_mb_emit_ldloc (mb
, 0);
1603 mono_mb_emit_byte (mb
, CEE_LDIND_REF
);
1604 mono_mb_emit_icall (mb
, conv_to_icall (conv
));
1605 mono_mb_emit_byte (mb
, CEE_STIND_I
);
1607 case MONO_MARSHAL_CONV_STR_BYVALSTR
:
1608 case MONO_MARSHAL_CONV_STR_BYVALWSTR
: {
1611 mono_mb_emit_ldloc (mb
, 1); /* dst */
1612 mono_mb_emit_ldloc (mb
, 0);
1613 mono_mb_emit_byte (mb
, CEE_LDIND_REF
); /* src String */
1614 mono_mb_emit_icon (mb
, mspec
->data
.array_data
.num_elem
);
1615 mono_mb_emit_icall (mb
, conv_to_icall (conv
));
1618 case MONO_MARSHAL_CONV_ARRAY_BYVALARRAY
: {
1619 MonoClass
*eklass
= NULL
;
1622 if (type
->type
== MONO_TYPE_SZARRAY
) {
1623 eklass
= type
->data
.klass
;
1625 g_assert_not_reached ();
1628 if (eklass
->valuetype
)
1629 esize
= mono_class_native_size (eklass
, NULL
);
1631 esize
= sizeof (gpointer
);
1633 mono_mb_emit_ldloc (mb
, 0);
1634 mono_mb_emit_byte (mb
, CEE_LDIND_REF
);
1635 pos
= mono_mb_emit_branch (mb
, CEE_BRFALSE
);
1637 if (eklass
->blittable
) {
1638 mono_mb_emit_ldloc (mb
, 1);
1639 mono_mb_emit_ldloc (mb
, 0);
1640 mono_mb_emit_byte (mb
, CEE_LDIND_REF
);
1641 mono_mb_emit_ldflda (mb
, G_STRUCT_OFFSET (MonoArray
, vector
));
1642 mono_mb_emit_icon (mb
, mspec
->data
.array_data
.num_elem
* esize
);
1643 mono_mb_emit_byte (mb
, CEE_PREFIX1
);
1644 mono_mb_emit_byte (mb
, CEE_CPBLK
);
1646 int array_var
, src_var
, dst_var
, index_var
;
1647 guint32 label2
, label3
;
1649 array_var
= mono_mb_add_local (mb
, &mono_defaults
.object_class
->byval_arg
);
1650 src_var
= mono_mb_add_local (mb
, &mono_defaults
.int_class
->byval_arg
);
1651 dst_var
= mono_mb_add_local (mb
, &mono_defaults
.int_class
->byval_arg
);
1654 mono_mb_emit_ldloc (mb
, 0);
1655 mono_mb_emit_byte (mb
, CEE_LDIND_REF
);
1656 mono_mb_emit_stloc (mb
, array_var
);
1658 /* save the old src pointer */
1659 mono_mb_emit_ldloc (mb
, 0);
1660 mono_mb_emit_stloc (mb
, src_var
);
1661 /* save the old dst pointer */
1662 mono_mb_emit_ldloc (mb
, 1);
1663 mono_mb_emit_stloc (mb
, dst_var
);
1665 /* Emit marshalling loop */
1666 index_var
= mono_mb_add_local (mb
, &mono_defaults
.int_class
->byval_arg
);
1667 mono_mb_emit_byte (mb
, CEE_LDC_I4_0
);
1668 mono_mb_emit_stloc (mb
, index_var
);
1671 label2
= mono_mb_get_label (mb
);
1672 mono_mb_emit_ldloc (mb
, index_var
);
1673 mono_mb_emit_ldloc (mb
, array_var
);
1674 mono_mb_emit_byte (mb
, CEE_LDLEN
);
1675 label3
= mono_mb_emit_branch (mb
, CEE_BGE
);
1678 mono_mb_emit_ldloc (mb
, array_var
);
1679 mono_mb_emit_ldloc (mb
, index_var
);
1680 mono_mb_emit_op (mb
, CEE_LDELEMA
, eklass
);
1681 mono_mb_emit_stloc (mb
, 0);
1683 /* dst is already set */
1685 /* Do the conversion */
1686 emit_struct_conv (mb
, eklass
, FALSE
);
1689 mono_mb_emit_add_to_local (mb
, index_var
, 1);
1691 mono_mb_emit_branch_label (mb
, CEE_BR
, label2
);
1693 mono_mb_patch_branch (mb
, label3
);
1695 /* restore the old src pointer */
1696 mono_mb_emit_ldloc (mb
, src_var
);
1697 mono_mb_emit_stloc (mb
, 0);
1698 /* restore the old dst pointer */
1699 mono_mb_emit_ldloc (mb
, dst_var
);
1700 mono_mb_emit_stloc (mb
, 1);
1703 mono_mb_patch_branch (mb
, pos
);
1706 case MONO_MARSHAL_CONV_ARRAY_BYVALCHARARRAY
: {
1707 mono_mb_emit_ldloc (mb
, 0);
1708 mono_mb_emit_byte (mb
, CEE_LDIND_REF
);
1709 pos
= mono_mb_emit_short_branch (mb
, CEE_BRFALSE_S
);
1711 mono_mb_emit_ldloc (mb
, 1);
1712 mono_mb_emit_ldloc (mb
, 0);
1713 mono_mb_emit_byte (mb
, CEE_LDIND_REF
);
1714 mono_mb_emit_ptr (mb
, mono_defaults
.byte_class
);
1715 mono_mb_emit_icon (mb
, mspec
->data
.array_data
.num_elem
);
1716 mono_mb_emit_icall (mb
, mono_array_to_byvalarray
);
1717 mono_mb_patch_short_branch (mb
, pos
);
1720 case MONO_MARSHAL_CONV_OBJECT_STRUCT
: {
1721 int src_var
, dst_var
;
1723 src_var
= mono_mb_add_local (mb
, &mono_defaults
.int_class
->byval_arg
);
1724 dst_var
= mono_mb_add_local (mb
, &mono_defaults
.int_class
->byval_arg
);
1726 mono_mb_emit_ldloc (mb
, 0);
1727 mono_mb_emit_byte (mb
, CEE_LDIND_I
);
1728 pos
= mono_mb_emit_branch (mb
, CEE_BRFALSE
);
1730 /* save the old src pointer */
1731 mono_mb_emit_ldloc (mb
, 0);
1732 mono_mb_emit_stloc (mb
, src_var
);
1733 /* save the old dst pointer */
1734 mono_mb_emit_ldloc (mb
, 1);
1735 mono_mb_emit_stloc (mb
, dst_var
);
1737 /* src = pointer to object data */
1738 mono_mb_emit_ldloc (mb
, 0);
1739 mono_mb_emit_byte (mb
, CEE_LDIND_I
);
1740 mono_mb_emit_icon (mb
, sizeof (MonoObject
));
1741 mono_mb_emit_byte (mb
, CEE_ADD
);
1742 mono_mb_emit_stloc (mb
, 0);
1744 emit_struct_conv (mb
, mono_class_from_mono_type (type
), FALSE
);
1746 /* restore the old src pointer */
1747 mono_mb_emit_ldloc (mb
, src_var
);
1748 mono_mb_emit_stloc (mb
, 0);
1749 /* restore the old dst pointer */
1750 mono_mb_emit_ldloc (mb
, dst_var
);
1751 mono_mb_emit_stloc (mb
, 1);
1753 mono_mb_patch_branch (mb
, pos
);
1758 case MONO_MARSHAL_CONV_OBJECT_INTERFACE
:
1759 case MONO_MARSHAL_CONV_OBJECT_IDISPATCH
:
1760 case MONO_MARSHAL_CONV_OBJECT_IUNKNOWN
:
1761 mono_cominterop_emit_object_to_ptr_conv (mb
, type
, conv
, mspec
);
1763 #endif /* DISABLE_COM */
1765 case MONO_MARSHAL_CONV_SAFEHANDLE
: {
1766 int dar_release_slot
, pos
;
1768 dar_release_slot
= mono_mb_add_local (mb
, &mono_defaults
.boolean_class
->byval_arg
);
1771 * The following is ifdefed-out, because I have no way of doing the
1772 * DangerousRelease when destroying the structure
1775 /* set release = false */
1776 mono_mb_emit_icon (mb
, 0);
1777 mono_mb_emit_stloc (mb
, dar_release_slot
);
1778 if (!sh_dangerous_add_ref
)
1779 init_safe_handle ();
1781 /* safehandle.DangerousAddRef (ref release) */
1782 mono_mb_emit_ldloc (mb
, 0); /* the source */
1783 mono_mb_emit_byte (mb
, CEE_LDIND_I
);
1784 mono_mb_emit_ldloc_addr (mb
, dar_release_slot
);
1785 mono_mb_emit_managed_call (mb
, sh_dangerous_add_ref
, NULL
);
1787 mono_mb_emit_ldloc (mb
, 0);
1788 mono_mb_emit_byte (mb
, CEE_LDIND_I
);
1789 pos
= mono_mb_emit_branch (mb
, CEE_BRTRUE
);
1790 mono_mb_emit_exception (mb
, "ArgumentNullException", NULL
);
1791 mono_mb_patch_branch (mb
, pos
);
1793 /* Pull the handle field from SafeHandle */
1794 mono_mb_emit_ldloc (mb
, 1);
1795 mono_mb_emit_ldloc (mb
, 0);
1796 mono_mb_emit_byte (mb
, CEE_LDIND_I
);
1797 mono_mb_emit_ldflda (mb
, G_STRUCT_OFFSET (MonoSafeHandle
, handle
));
1798 mono_mb_emit_byte (mb
, CEE_LDIND_I
);
1799 mono_mb_emit_byte (mb
, CEE_STIND_I
);
1803 case MONO_MARSHAL_CONV_HANDLEREF
: {
1804 mono_mb_emit_ldloc (mb
, 1);
1805 mono_mb_emit_ldloc (mb
, 0);
1806 mono_mb_emit_icon (mb
, G_STRUCT_OFFSET (MonoHandleRef
, handle
));
1807 mono_mb_emit_byte (mb
, CEE_ADD
);
1808 mono_mb_emit_byte (mb
, CEE_LDIND_I
);
1809 mono_mb_emit_byte (mb
, CEE_STIND_I
);
1814 char *msg
= g_strdup_printf ("marshalling conversion %d not implemented", conv
);
1815 MonoException
*exc
= mono_get_exception_not_implemented (msg
);
1816 g_warning ("%s", msg
);
1818 mono_raise_exception (exc
);
1824 emit_struct_conv_full (MonoMethodBuilder
*mb
, MonoClass
*klass
, gboolean to_object
,
1825 MonoMarshalNative string_encoding
)
1827 MonoMarshalType
*info
;
1831 emit_struct_conv(mb
, klass
->parent
, to_object
);
1833 info
= mono_marshal_load_type_info (klass
);
1835 if (info
->native_size
== 0)
1838 if (klass
->blittable
) {
1839 int msize
= mono_class_value_size (klass
, NULL
);
1840 g_assert (msize
== info
->native_size
);
1841 mono_mb_emit_ldloc (mb
, 1);
1842 mono_mb_emit_ldloc (mb
, 0);
1843 mono_mb_emit_icon (mb
, msize
);
1844 mono_mb_emit_byte (mb
, CEE_PREFIX1
);
1845 mono_mb_emit_byte (mb
, CEE_CPBLK
);
1847 mono_mb_emit_add_to_local (mb
, 0, msize
);
1848 mono_mb_emit_add_to_local (mb
, 1, msize
);
1852 if (klass
!= mono_defaults
.safehandle_class
) {
1853 if ((klass
->flags
& TYPE_ATTRIBUTE_LAYOUT_MASK
) == TYPE_ATTRIBUTE_AUTO_LAYOUT
) {
1854 char *msg
= g_strdup_printf ("Type %s which is passed to unmanaged code must have a StructLayout attribute.",
1855 mono_type_full_name (&klass
->byval_arg
));
1856 mono_mb_emit_exception_marshal_directive (mb
, msg
);
1861 for (i
= 0; i
< info
->num_fields
; i
++) {
1862 MonoMarshalNative ntype
;
1863 MonoMarshalConv conv
;
1864 MonoType
*ftype
= info
->fields
[i
].field
->type
;
1867 gboolean last_field
= i
< (info
->num_fields
-1) ? 0 : 1;
1869 if (ftype
->attrs
& FIELD_ATTRIBUTE_STATIC
)
1872 ntype
= mono_type_to_unmanaged (ftype
, info
->fields
[i
].mspec
, TRUE
, klass
->unicode
, &conv
);
1875 msize
= klass
->instance_size
- info
->fields
[i
].field
->offset
;
1876 usize
= info
->native_size
- info
->fields
[i
].offset
;
1878 msize
= info
->fields
[i
+ 1].field
->offset
- info
->fields
[i
].field
->offset
;
1879 usize
= info
->fields
[i
+ 1].offset
- info
->fields
[i
].offset
;
1882 if (klass
!= mono_defaults
.safehandle_class
){
1884 * FIXME: Should really check for usize==0 and msize>0, but we apply
1885 * the layout to the managed structure as well.
1888 if (((klass
->flags
& TYPE_ATTRIBUTE_LAYOUT_MASK
) == TYPE_ATTRIBUTE_EXPLICIT_LAYOUT
) && (usize
== 0)) {
1889 if (MONO_TYPE_IS_REFERENCE (info
->fields
[i
].field
->type
) ||
1890 ((!last_field
&& MONO_TYPE_IS_REFERENCE (info
->fields
[i
+ 1].field
->type
))))
1891 g_error ("Type %s which has an [ExplicitLayout] attribute cannot have a "
1892 "reference field at the same offset as another field.",
1893 mono_type_full_name (&klass
->byval_arg
));
1898 case MONO_MARSHAL_CONV_NONE
: {
1901 if (ftype
->byref
|| ftype
->type
== MONO_TYPE_I
||
1902 ftype
->type
== MONO_TYPE_U
) {
1903 mono_mb_emit_ldloc (mb
, 1);
1904 mono_mb_emit_ldloc (mb
, 0);
1905 mono_mb_emit_byte (mb
, CEE_LDIND_I
);
1906 mono_mb_emit_byte (mb
, CEE_STIND_I
);
1917 case MONO_TYPE_BOOLEAN
:
1920 case MONO_TYPE_CHAR
:
1926 mono_mb_emit_ldloc (mb
, 1);
1927 mono_mb_emit_ldloc (mb
, 0);
1928 if (t
== MONO_TYPE_CHAR
&& ntype
== MONO_NATIVE_U1
&& string_encoding
!= MONO_NATIVE_LPWSTR
) {
1930 mono_mb_emit_byte (mb
, CEE_LDIND_U1
);
1931 mono_mb_emit_byte (mb
, CEE_STIND_I2
);
1933 mono_mb_emit_byte (mb
, CEE_LDIND_U2
);
1934 mono_mb_emit_byte (mb
, CEE_STIND_I1
);
1937 mono_mb_emit_byte (mb
, mono_type_to_ldind (ftype
));
1938 mono_mb_emit_byte (mb
, mono_type_to_stind (ftype
));
1941 case MONO_TYPE_VALUETYPE
: {
1942 int src_var
, dst_var
;
1944 if (ftype
->data
.klass
->enumtype
) {
1945 ftype
= mono_class_enum_basetype (ftype
->data
.klass
);
1949 src_var
= mono_mb_add_local (mb
, &mono_defaults
.int_class
->byval_arg
);
1950 dst_var
= mono_mb_add_local (mb
, &mono_defaults
.int_class
->byval_arg
);
1952 /* save the old src pointer */
1953 mono_mb_emit_ldloc (mb
, 0);
1954 mono_mb_emit_stloc (mb
, src_var
);
1955 /* save the old dst pointer */
1956 mono_mb_emit_ldloc (mb
, 1);
1957 mono_mb_emit_stloc (mb
, dst_var
);
1959 emit_struct_conv (mb
, ftype
->data
.klass
, to_object
);
1961 /* restore the old src pointer */
1962 mono_mb_emit_ldloc (mb
, src_var
);
1963 mono_mb_emit_stloc (mb
, 0);
1964 /* restore the old dst pointer */
1965 mono_mb_emit_ldloc (mb
, dst_var
);
1966 mono_mb_emit_stloc (mb
, 1);
1969 case MONO_TYPE_OBJECT
: {
1971 mono_init_com_types ();
1973 static MonoMethod
*variant_clear
= NULL
;
1974 static MonoMethod
*get_object_for_native_variant
= NULL
;
1977 variant_clear
= mono_class_get_method_from_name (mono_defaults
.variant_class
, "Clear", 0);
1978 if (!get_object_for_native_variant
)
1979 get_object_for_native_variant
= mono_class_get_method_from_name (mono_defaults
.marshal_class
, "GetObjectForNativeVariant", 1);
1980 mono_mb_emit_ldloc (mb
, 1);
1981 mono_mb_emit_ldloc (mb
, 0);
1982 mono_mb_emit_managed_call (mb
, get_object_for_native_variant
, NULL
);
1983 mono_mb_emit_byte (mb
, CEE_STIND_REF
);
1985 mono_mb_emit_ldloc (mb
, 0);
1986 mono_mb_emit_managed_call (mb
, variant_clear
, NULL
);
1989 static MonoMethod
*get_native_variant_for_object
= NULL
;
1991 if (!get_native_variant_for_object
)
1992 get_native_variant_for_object
= mono_class_get_method_from_name (mono_defaults
.marshal_class
, "GetNativeVariantForObject", 2);
1994 mono_mb_emit_ldloc (mb
, 0);
1995 mono_mb_emit_byte(mb
, CEE_LDIND_REF
);
1996 mono_mb_emit_ldloc (mb
, 1);
1997 mono_mb_emit_managed_call (mb
, get_native_variant_for_object
, NULL
);
2000 char *msg
= g_strdup_printf ("COM support was disabled at compilation time.");
2001 mono_mb_emit_exception_marshal_directive (mb
, msg
);
2007 g_warning ("marshaling type %02x not implemented", ftype
->type
);
2008 g_assert_not_reached ();
2013 int src_var
, dst_var
;
2015 src_var
= mono_mb_add_local (mb
, &mono_defaults
.int_class
->byval_arg
);
2016 dst_var
= mono_mb_add_local (mb
, &mono_defaults
.int_class
->byval_arg
);
2018 /* save the old src pointer */
2019 mono_mb_emit_ldloc (mb
, 0);
2020 mono_mb_emit_stloc (mb
, src_var
);
2021 /* save the old dst pointer */
2022 mono_mb_emit_ldloc (mb
, 1);
2023 mono_mb_emit_stloc (mb
, dst_var
);
2026 emit_ptr_to_object_conv (mb
, ftype
, conv
, info
->fields
[i
].mspec
);
2028 emit_object_to_ptr_conv (mb
, ftype
, conv
, info
->fields
[i
].mspec
);
2030 /* restore the old src pointer */
2031 mono_mb_emit_ldloc (mb
, src_var
);
2032 mono_mb_emit_stloc (mb
, 0);
2033 /* restore the old dst pointer */
2034 mono_mb_emit_ldloc (mb
, dst_var
);
2035 mono_mb_emit_stloc (mb
, 1);
2040 mono_mb_emit_add_to_local (mb
, 0, usize
);
2041 mono_mb_emit_add_to_local (mb
, 1, msize
);
2043 mono_mb_emit_add_to_local (mb
, 0, msize
);
2044 mono_mb_emit_add_to_local (mb
, 1, usize
);
2050 emit_struct_conv (MonoMethodBuilder
*mb
, MonoClass
*klass
, gboolean to_object
)
2052 emit_struct_conv_full (mb
, klass
, to_object
, -1);
2056 emit_struct_free (MonoMethodBuilder
*mb
, MonoClass
*klass
, int struct_var
)
2058 /* Call DestroyStructure */
2059 /* FIXME: Only do this if needed */
2060 mono_mb_emit_byte (mb
, MONO_CUSTOM_PREFIX
);
2061 mono_mb_emit_op (mb
, CEE_MONO_CLASSCONST
, klass
);
2062 mono_mb_emit_ldloc (mb
, struct_var
);
2063 mono_mb_emit_icall (mb
, mono_struct_delete_old
);
2067 emit_thread_interrupt_checkpoint_call (MonoMethodBuilder
*mb
, gpointer checkpoint_func
)
2071 mono_mb_emit_ptr (mb
, (gpointer
) mono_thread_interruption_request_flag ());
2072 mono_mb_emit_byte (mb
, CEE_LDIND_U4
);
2073 pos_noabort
= mono_mb_emit_branch (mb
, CEE_BRFALSE
);
2075 mono_mb_emit_byte (mb
, MONO_CUSTOM_PREFIX
);
2076 mono_mb_emit_byte (mb
, CEE_MONO_NOT_TAKEN
);
2078 mono_mb_emit_icall (mb
, checkpoint_func
);
2080 mono_mb_patch_branch (mb
, pos_noabort
);
2084 emit_thread_interrupt_checkpoint (MonoMethodBuilder
*mb
)
2086 if (strstr (mb
->name
, "mono_thread_interruption_checkpoint"))
2089 emit_thread_interrupt_checkpoint_call (mb
, mono_thread_interruption_checkpoint
);
2093 emit_thread_force_interrupt_checkpoint (MonoMethodBuilder
*mb
)
2095 emit_thread_interrupt_checkpoint_call (mb
, mono_thread_force_interruption_checkpoint
);
2099 mono_marshal_emit_thread_interrupt_checkpoint (MonoMethodBuilder
*mb
)
2101 emit_thread_interrupt_checkpoint (mb
);
2104 #endif /* DISABLE_JIT */
2106 static MonoAsyncResult
*
2107 mono_delegate_begin_invoke (MonoDelegate
*delegate
, gpointer
*params
)
2109 MonoMethodMessage
*msg
;
2110 MonoDelegate
*async_callback
;
2111 MonoMulticastDelegate
*mcast_delegate
;
2115 MonoMethod
*method
= NULL
, *method2
= NULL
;
2117 g_assert (delegate
);
2118 mcast_delegate
= (MonoMulticastDelegate
*) delegate
;
2119 if (mcast_delegate
->prev
!= NULL
)
2120 mono_raise_exception (mono_get_exception_argument (NULL
, "The delegate must have only one target"));
2122 #ifndef DISABLE_REMOTING
2123 if (delegate
->target
&& mono_object_class (delegate
->target
) == mono_defaults
.transparent_proxy_class
) {
2125 MonoTransparentProxy
* tp
= (MonoTransparentProxy
*)delegate
->target
;
2126 if (!mono_class_is_contextbound (tp
->remote_class
->proxy_class
) || tp
->rp
->context
!= (MonoObject
*) mono_context_get ()) {
2128 /* If the target is a proxy, make a direct call. Is proxy's work
2129 // to make the call asynchronous.
2131 MonoAsyncResult
*ares
;
2133 MonoArray
*out_args
;
2134 method
= delegate
->method
;
2136 msg
= mono_method_call_message_new (mono_marshal_method_from_wrapper (method
), params
, NULL
, &async_callback
, &state
);
2137 ares
= mono_async_result_new (mono_domain_get (), NULL
, state
, NULL
, NULL
);
2138 MONO_OBJECT_SETREF (ares
, async_delegate
, (MonoObject
*)delegate
);
2139 MONO_OBJECT_SETREF (ares
, async_callback
, (MonoObject
*)async_callback
);
2140 MONO_OBJECT_SETREF (msg
, async_result
, ares
);
2141 msg
->call_type
= CallType_BeginInvoke
;
2144 mono_remoting_invoke ((MonoObject
*)tp
->rp
, msg
, &exc
, &out_args
);
2146 mono_raise_exception ((MonoException
*) exc
);
2152 klass
= delegate
->object
.vtable
->klass
;
2154 method
= mono_get_delegate_invoke (klass
);
2155 method2
= mono_class_get_method_from_name (klass
, "BeginInvoke", -1);
2158 g_assert (method
!= NULL
);
2160 im
= mono_get_delegate_invoke (method
->klass
);
2161 msg
= mono_method_call_message_new (method
, params
, im
, &async_callback
, &state
);
2163 return mono_thread_pool_add ((MonoObject
*)delegate
, msg
, async_callback
, state
);
2169 mono_mb_emit_save_args (MonoMethodBuilder
*mb
, MonoMethodSignature
*sig
, gboolean save_this
)
2171 int i
, params_var
, tmp_var
;
2173 /* allocate local (pointer) *params[] */
2174 params_var
= mono_mb_add_local (mb
, &mono_defaults
.int_class
->byval_arg
);
2175 /* allocate local (pointer) tmp */
2176 tmp_var
= mono_mb_add_local (mb
, &mono_defaults
.int_class
->byval_arg
);
2178 /* alloate space on stack to store an array of pointers to the arguments */
2179 mono_mb_emit_icon (mb
, sizeof (gpointer
) * (sig
->param_count
+ 1));
2180 mono_mb_emit_byte (mb
, CEE_PREFIX1
);
2181 mono_mb_emit_byte (mb
, CEE_LOCALLOC
);
2182 mono_mb_emit_stloc (mb
, params_var
);
2185 mono_mb_emit_ldloc (mb
, params_var
);
2186 mono_mb_emit_stloc (mb
, tmp_var
);
2188 if (save_this
&& sig
->hasthis
) {
2189 mono_mb_emit_ldloc (mb
, tmp_var
);
2190 mono_mb_emit_ldarg_addr (mb
, 0);
2191 mono_mb_emit_byte (mb
, CEE_STIND_I
);
2192 /* tmp = tmp + sizeof (gpointer) */
2193 if (sig
->param_count
)
2194 mono_mb_emit_add_to_local (mb
, tmp_var
, sizeof (gpointer
));
2198 for (i
= 0; i
< sig
->param_count
; i
++) {
2199 mono_mb_emit_ldloc (mb
, tmp_var
);
2200 mono_mb_emit_ldarg_addr (mb
, i
+ sig
->hasthis
);
2201 mono_mb_emit_byte (mb
, CEE_STIND_I
);
2202 /* tmp = tmp + sizeof (gpointer) */
2203 if (i
< (sig
->param_count
- 1))
2204 mono_mb_emit_add_to_local (mb
, tmp_var
, sizeof (gpointer
));
2210 #endif /* DISABLE_JIT */
2213 mono_signature_to_name (MonoMethodSignature
*sig
, const char *prefix
)
2217 GString
*res
= g_string_new ("");
2220 g_string_append (res
, prefix
);
2221 g_string_append_c (res
, '_');
2224 mono_type_get_desc (res
, sig
->ret
, FALSE
);
2227 g_string_append (res
, "__this__");
2229 for (i
= 0; i
< sig
->param_count
; ++i
) {
2230 g_string_append_c (res
, '_');
2231 mono_type_get_desc (res
, sig
->params
[i
], FALSE
);
2234 g_string_free (res
, FALSE
);
2239 * mono_marshal_get_string_encoding:
2241 * Return the string encoding which should be used for a given parameter.
2243 static MonoMarshalNative
2244 mono_marshal_get_string_encoding (MonoMethodPInvoke
*piinfo
, MonoMarshalSpec
*spec
)
2246 /* First try the parameter marshal info */
2248 if (spec
->native
== MONO_NATIVE_LPARRAY
) {
2249 if ((spec
->data
.array_data
.elem_type
!= 0) && (spec
->data
.array_data
.elem_type
!= MONO_NATIVE_MAX
))
2250 return spec
->data
.array_data
.elem_type
;
2253 return spec
->native
;
2257 return MONO_NATIVE_LPSTR
;
2259 /* Then try the method level marshal info */
2260 switch (piinfo
->piflags
& PINVOKE_ATTRIBUTE_CHAR_SET_MASK
) {
2261 case PINVOKE_ATTRIBUTE_CHAR_SET_ANSI
:
2262 return MONO_NATIVE_LPSTR
;
2263 case PINVOKE_ATTRIBUTE_CHAR_SET_UNICODE
:
2264 return MONO_NATIVE_LPWSTR
;
2265 case PINVOKE_ATTRIBUTE_CHAR_SET_AUTO
:
2267 return MONO_NATIVE_LPWSTR
;
2269 return MONO_NATIVE_LPSTR
;
2272 return MONO_NATIVE_LPSTR
;
2276 static MonoMarshalConv
2277 mono_marshal_get_string_to_ptr_conv (MonoMethodPInvoke
*piinfo
, MonoMarshalSpec
*spec
)
2279 MonoMarshalNative encoding
= mono_marshal_get_string_encoding (piinfo
, spec
);
2282 case MONO_NATIVE_LPWSTR
:
2283 return MONO_MARSHAL_CONV_STR_LPWSTR
;
2284 case MONO_NATIVE_LPSTR
:
2285 case MONO_NATIVE_VBBYREFSTR
:
2286 return MONO_MARSHAL_CONV_STR_LPSTR
;
2287 case MONO_NATIVE_LPTSTR
:
2288 return MONO_MARSHAL_CONV_STR_LPTSTR
;
2289 case MONO_NATIVE_BSTR
:
2290 return MONO_MARSHAL_CONV_STR_BSTR
;
2296 static MonoMarshalConv
2297 mono_marshal_get_stringbuilder_to_ptr_conv (MonoMethodPInvoke
*piinfo
, MonoMarshalSpec
*spec
)
2299 MonoMarshalNative encoding
= mono_marshal_get_string_encoding (piinfo
, spec
);
2302 case MONO_NATIVE_LPWSTR
:
2303 return MONO_MARSHAL_CONV_SB_LPWSTR
;
2305 case MONO_NATIVE_LPSTR
:
2306 return MONO_MARSHAL_CONV_SB_LPSTR
;
2308 case MONO_NATIVE_LPTSTR
:
2309 return MONO_MARSHAL_CONV_SB_LPTSTR
;
2316 static MonoMarshalConv
2317 mono_marshal_get_ptr_to_string_conv (MonoMethodPInvoke
*piinfo
, MonoMarshalSpec
*spec
, gboolean
*need_free
)
2319 MonoMarshalNative encoding
= mono_marshal_get_string_encoding (piinfo
, spec
);
2324 case MONO_NATIVE_LPWSTR
:
2326 return MONO_MARSHAL_CONV_LPWSTR_STR
;
2327 case MONO_NATIVE_LPSTR
:
2328 case MONO_NATIVE_VBBYREFSTR
:
2329 return MONO_MARSHAL_CONV_LPSTR_STR
;
2330 case MONO_NATIVE_LPTSTR
:
2331 return MONO_MARSHAL_CONV_LPTSTR_STR
;
2332 case MONO_NATIVE_BSTR
:
2333 return MONO_MARSHAL_CONV_BSTR_STR
;
2339 static MonoMarshalConv
2340 mono_marshal_get_ptr_to_stringbuilder_conv (MonoMethodPInvoke
*piinfo
, MonoMarshalSpec
*spec
, gboolean
*need_free
)
2342 MonoMarshalNative encoding
= mono_marshal_get_string_encoding (piinfo
, spec
);
2347 case MONO_NATIVE_LPWSTR
:
2349 * mono_string_builder_to_utf16 does not allocate a
2350 * new buffer, so no need to free it.
2353 return MONO_MARSHAL_CONV_LPWSTR_SB
;
2354 case MONO_NATIVE_LPSTR
:
2355 return MONO_MARSHAL_CONV_LPSTR_SB
;
2357 case MONO_NATIVE_LPTSTR
:
2358 return MONO_MARSHAL_CONV_LPTSTR_SB
;
2366 * Return whenever a field of a native structure or an array member needs to
2370 mono_marshal_need_free (MonoType
*t
, MonoMethodPInvoke
*piinfo
, MonoMarshalSpec
*spec
)
2372 MonoMarshalNative encoding
;
2373 MonoMarshalConv conv
;
2376 case MONO_TYPE_VALUETYPE
:
2377 /* FIXME: Optimize this */
2379 case MONO_TYPE_OBJECT
:
2380 case MONO_TYPE_CLASS
:
2381 if (t
->data
.klass
== mono_defaults
.stringbuilder_class
) {
2383 conv
= mono_marshal_get_ptr_to_stringbuilder_conv (piinfo
, spec
, &need_free
);
2387 case MONO_TYPE_STRING
:
2388 encoding
= mono_marshal_get_string_encoding (piinfo
, spec
);
2389 return (encoding
== MONO_NATIVE_LPWSTR
) ? FALSE
: TRUE
;
2396 * Return the hash table pointed to by VAR, lazily creating it if neccesary.
2399 get_cache (GHashTable
**var
, GHashFunc hash_func
, GCompareFunc equal_func
)
2402 mono_marshal_lock ();
2405 g_hash_table_new (hash_func
, equal_func
);
2406 mono_memory_barrier ();
2409 mono_marshal_unlock ();
2415 get_cache_full (GHashTable
**var
, GHashFunc hash_func
, GCompareFunc equal_func
, GDestroyNotify key_destroy_func
, GDestroyNotify value_destroy_func
)
2418 mono_marshal_lock ();
2421 g_hash_table_new_full (hash_func
, equal_func
, key_destroy_func
, value_destroy_func
);
2422 mono_memory_barrier ();
2425 mono_marshal_unlock ();
2431 mono_marshal_get_cache (GHashTable
**var
, GHashFunc hash_func
, GCompareFunc equal_func
)
2433 return get_cache (var
, hash_func
, equal_func
);
2437 mono_marshal_find_in_cache (GHashTable
*cache
, gpointer key
)
2441 mono_marshal_lock ();
2442 res
= g_hash_table_lookup (cache
, key
);
2443 mono_marshal_unlock ();
2447 /* Create the method from the builder and place it in the cache */
2449 mono_mb_create_and_cache (GHashTable
*cache
, gpointer key
,
2450 MonoMethodBuilder
*mb
, MonoMethodSignature
*sig
,
2455 mono_marshal_lock ();
2456 res
= g_hash_table_lookup (cache
, key
);
2457 mono_marshal_unlock ();
2460 newm
= mono_mb_create_method (mb
, sig
, max_stack
);
2461 mono_marshal_lock ();
2462 res
= g_hash_table_lookup (cache
, key
);
2465 g_hash_table_insert (cache
, key
, res
);
2466 mono_marshal_set_wrapper_info (res
, key
);
2467 mono_marshal_unlock ();
2469 mono_marshal_unlock ();
2470 mono_free_method (newm
);
2478 static inline MonoMethod
*
2479 mono_marshal_remoting_find_in_cache (MonoMethod
*method
, int wrapper_type
)
2481 MonoMethod
*res
= NULL
;
2482 MonoRemotingMethods
*wrps
;
2484 mono_marshal_lock ();
2485 if (method
->klass
->image
->remoting_invoke_cache
)
2486 wrps
= g_hash_table_lookup (method
->klass
->image
->remoting_invoke_cache
, method
);
2491 switch (wrapper_type
) {
2492 case MONO_WRAPPER_REMOTING_INVOKE
: res
= wrps
->invoke
; break;
2493 case MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK
: res
= wrps
->invoke_with_check
; break;
2494 case MONO_WRAPPER_XDOMAIN_INVOKE
: res
= wrps
->xdomain_invoke
; break;
2495 case MONO_WRAPPER_XDOMAIN_DISPATCH
: res
= wrps
->xdomain_dispatch
; break;
2499 /* it is important to do the unlock after the load from wrps, since in
2500 * mono_remoting_mb_create_and_cache () we drop the marshal lock to be able
2501 * to take the loader lock and some other thread may set the fields.
2503 mono_marshal_unlock ();
2507 /* Create the method from the builder and place it in the cache */
2508 static inline MonoMethod
*
2509 mono_remoting_mb_create_and_cache (MonoMethod
*key
, MonoMethodBuilder
*mb
,
2510 MonoMethodSignature
*sig
, int max_stack
)
2512 MonoMethod
**res
= NULL
;
2513 MonoRemotingMethods
*wrps
;
2514 GHashTable
*cache
= get_cache_full (&key
->klass
->image
->remoting_invoke_cache
, mono_aligned_addr_hash
, NULL
, NULL
, g_free
);
2516 mono_marshal_lock ();
2517 wrps
= g_hash_table_lookup (cache
, key
);
2519 wrps
= g_new0 (MonoRemotingMethods
, 1);
2520 g_hash_table_insert (cache
, key
, wrps
);
2523 switch (mb
->method
->wrapper_type
) {
2524 case MONO_WRAPPER_REMOTING_INVOKE
: res
= &wrps
->invoke
; break;
2525 case MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK
: res
= &wrps
->invoke_with_check
; break;
2526 case MONO_WRAPPER_XDOMAIN_INVOKE
: res
= &wrps
->xdomain_invoke
; break;
2527 case MONO_WRAPPER_XDOMAIN_DISPATCH
: res
= &wrps
->xdomain_dispatch
; break;
2528 default: g_assert_not_reached (); break;
2530 mono_marshal_unlock ();
2534 newm
= mono_mb_create_method (mb
, sig
, max_stack
);
2536 mono_marshal_lock ();
2539 mono_marshal_set_wrapper_info (*res
, key
);
2540 mono_marshal_unlock ();
2542 mono_marshal_unlock ();
2543 mono_free_method (newm
);
2551 mono_marshal_method_from_wrapper (MonoMethod
*wrapper
)
2554 int wrapper_type
= wrapper
->wrapper_type
;
2557 if (wrapper_type
== MONO_WRAPPER_NONE
|| wrapper_type
== MONO_WRAPPER_DYNAMIC_METHOD
)
2560 switch (wrapper_type
) {
2561 case MONO_WRAPPER_REMOTING_INVOKE
:
2562 case MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK
:
2563 case MONO_WRAPPER_XDOMAIN_INVOKE
:
2564 case MONO_WRAPPER_SYNCHRONIZED
:
2565 case MONO_WRAPPER_UNBOX
:
2566 res
= mono_marshal_get_wrapper_info (wrapper
);
2570 case MONO_WRAPPER_MANAGED_TO_NATIVE
:
2571 info
= mono_marshal_get_wrapper_info (wrapper
);
2572 if (info
&& (info
->subtype
== WRAPPER_SUBTYPE_NONE
|| info
->subtype
== WRAPPER_SUBTYPE_NATIVE_FUNC_AOT
))
2573 return info
->d
.managed_to_native
.method
;
2576 case MONO_WRAPPER_RUNTIME_INVOKE
:
2577 info
= mono_marshal_get_wrapper_info (wrapper
);
2578 if (info
&& (info
->subtype
== WRAPPER_SUBTYPE_RUNTIME_INVOKE_DIRECT
|| info
->subtype
== WRAPPER_SUBTYPE_RUNTIME_INVOKE_VIRTUAL
))
2579 return info
->d
.runtime_invoke
.method
;
2588 * mono_marshal_get_wrapper_info:
2590 * Retrieve the pointer stored by mono_marshal_set_wrapper_info. The type of data
2591 * returned depends on the wrapper type. It is usually a method, a class, or a
2592 * WrapperInfo structure.
2595 mono_marshal_get_wrapper_info (MonoMethod
*wrapper
)
2597 g_assert (wrapper
->wrapper_type
);
2599 return mono_method_get_wrapper_data (wrapper
, 1);
2603 * mono_marshal_set_wrapper_info:
2605 * Store an arbitrary pointer inside the wrapper which is retrievable by
2606 * mono_marshal_get_wrapper_info. The format of the data depends on the type of the
2607 * wrapper (method->wrapper_type).
2610 mono_marshal_set_wrapper_info (MonoMethod
*method
, gpointer data
)
2614 if (method
->wrapper_type
== MONO_WRAPPER_NONE
|| method
->wrapper_type
== MONO_WRAPPER_DYNAMIC_METHOD
)
2617 datav
= ((MonoMethodWrapper
*)method
)->method_data
;
2622 mono_wrapper_info_create (MonoMethod
*wrapper
, WrapperSubtype subtype
)
2626 info
= mono_image_alloc0 (wrapper
->klass
->image
, sizeof (WrapperInfo
));
2627 info
->subtype
= subtype
;
2632 * get_wrapper_target_class:
2634 * Return the class where a wrapper method should be placed.
2637 get_wrapper_target_class (MonoImage
*image
)
2643 * - can't put all wrappers into an mscorlib class, because they reference
2644 * metadata (signature) so they should be put into the same image as the
2645 * method they wrap, so they are unloaded together.
2646 * - putting them into a class with a type initalizer could cause the
2647 * initializer to be executed which can be a problem if the wrappers are
2649 * - putting them into an inflated class can cause problems if the the
2650 * class is deleted because it references an image which is unloaded.
2651 * To avoid these problems, we put the wrappers into the <Module> class of
2655 klass
= ((MonoDynamicImage
*)image
)->wrappers_type
;
2657 klass
= mono_class_get (image
, mono_metadata_make_token (MONO_TABLE_TYPEDEF
, 1));
2664 check_generic_delegate_wrapper_cache (GHashTable
*cache
, MonoMethod
*orig_method
, MonoMethod
*def_method
, MonoGenericContext
*ctx
)
2667 MonoMethod
*inst
, *def
;
2670 * Look for the instance
2672 res
= mono_marshal_find_in_cache (cache
, orig_method
->klass
);
2677 * Look for the definition
2679 def
= mono_marshal_find_in_cache (cache
, def_method
->klass
);
2681 inst
= mono_class_inflate_generic_method (def
, ctx
);
2683 mono_memory_barrier ();
2684 mono_marshal_lock ();
2685 res
= g_hash_table_lookup (cache
, orig_method
->klass
);
2687 g_hash_table_insert (cache
, orig_method
->klass
, inst
);
2690 mono_marshal_unlock ();
2697 cache_generic_delegate_wrapper (GHashTable
*cache
, MonoMethod
*orig_method
, MonoMethod
*def
, MonoGenericContext
*ctx
)
2699 MonoMethod
*inst
, *res
;
2702 * We use the same cache for the generic definition and the instances.
2704 inst
= mono_class_inflate_generic_method (def
, ctx
);
2705 mono_memory_barrier ();
2706 mono_marshal_lock ();
2707 res
= g_hash_table_lookup (cache
, orig_method
->klass
);
2709 g_hash_table_insert (cache
, orig_method
->klass
, inst
);
2712 mono_marshal_unlock ();
2717 mono_marshal_get_delegate_begin_invoke (MonoMethod
*method
)
2719 MonoMethodSignature
*sig
;
2720 MonoMethodBuilder
*mb
;
2725 MonoGenericContext
*ctx
= NULL
;
2726 MonoMethod
*orig_method
= NULL
;
2728 g_assert (method
&& method
->klass
->parent
== mono_defaults
.multicastdelegate_class
&&
2729 !strcmp (method
->name
, "BeginInvoke"));
2732 * For generic delegates, create a generic wrapper, and returns an instance to help AOT.
2734 if (method
->is_inflated
) {
2735 orig_method
= method
;
2736 ctx
= &((MonoMethodInflated
*)method
)->context
;
2737 method
= ((MonoMethodInflated
*)method
)->declaring
;
2740 sig
= mono_signature_no_pinvoke (method
);
2746 cache
= get_cache (&method
->klass
->image
->delegate_begin_invoke_generic_cache
, mono_aligned_addr_hash
, NULL
);
2747 res
= check_generic_delegate_wrapper_cache (cache
, orig_method
, method
, ctx
);
2751 cache
= get_cache (&method
->klass
->image
->delegate_begin_invoke_cache
,
2752 (GHashFunc
)mono_signature_hash
,
2753 (GCompareFunc
)mono_metadata_signature_equal
);
2754 if ((res
= mono_marshal_find_in_cache (cache
, sig
)))
2758 g_assert (sig
->hasthis
);
2760 name
= mono_signature_to_name (sig
, "begin_invoke");
2762 mb
= mono_mb_new (method
->klass
, name
, MONO_WRAPPER_DELEGATE_BEGIN_INVOKE
);
2764 mb
= mono_mb_new (get_wrapper_target_class (method
->klass
->image
), name
, MONO_WRAPPER_DELEGATE_BEGIN_INVOKE
);
2768 params_var
= mono_mb_emit_save_args (mb
, sig
, FALSE
);
2770 mono_mb_emit_ldarg (mb
, 0);
2771 mono_mb_emit_ldloc (mb
, params_var
);
2772 mono_mb_emit_icall (mb
, mono_delegate_begin_invoke
);
2773 mono_mb_emit_byte (mb
, CEE_RET
);
2778 def
= mono_mb_create_and_cache (cache
, method
->klass
, mb
, sig
, sig
->param_count
+ 16);
2779 res
= cache_generic_delegate_wrapper (cache
, orig_method
, def
, ctx
);
2781 res
= mono_mb_create_and_cache (cache
, sig
, mb
, sig
, sig
->param_count
+ 16);
2789 mono_delegate_end_invoke (MonoDelegate
*delegate
, gpointer
*params
)
2791 MonoDomain
*domain
= mono_domain_get ();
2792 MonoAsyncResult
*ares
;
2793 MonoMethod
*method
= NULL
;
2794 MonoMethodSignature
*sig
;
2795 MonoMethodMessage
*msg
;
2796 MonoObject
*res
, *exc
;
2797 MonoArray
*out_args
;
2800 g_assert (delegate
);
2802 if (!delegate
->method_info
) {
2803 g_assert (delegate
->method
);
2804 MONO_OBJECT_SETREF (delegate
, method_info
, mono_method_get_object (domain
, delegate
->method
, NULL
));
2807 if (!delegate
->method_info
|| !delegate
->method_info
->method
)
2808 g_assert_not_reached ();
2810 klass
= delegate
->object
.vtable
->klass
;
2812 method
= mono_class_get_method_from_name (klass
, "EndInvoke", -1);
2813 g_assert (method
!= NULL
);
2815 sig
= mono_signature_no_pinvoke (method
);
2817 msg
= mono_method_call_message_new (method
, params
, NULL
, NULL
, NULL
);
2819 ares
= mono_array_get (msg
->args
, gpointer
, sig
->param_count
- 1);
2821 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."));
2825 if (ares
->async_delegate
!= (MonoObject
*)delegate
) {
2826 mono_raise_exception (mono_get_exception_invalid_operation (
2827 "The IAsyncResult object provided does not match this delegate."));
2831 #ifndef DISABLE_REMOTING
2832 if (delegate
->target
&& mono_object_is_transparent_proxy (delegate
->target
)) {
2833 MonoTransparentProxy
* tp
= (MonoTransparentProxy
*)delegate
->target
;
2834 msg
= (MonoMethodMessage
*)mono_object_new (domain
, mono_defaults
.mono_method_message_class
);
2835 mono_message_init (domain
, msg
, delegate
->method_info
, NULL
);
2836 msg
->call_type
= CallType_EndInvoke
;
2837 MONO_OBJECT_SETREF (msg
, async_result
, ares
);
2838 res
= mono_remoting_invoke ((MonoObject
*)tp
->rp
, msg
, &exc
, &out_args
);
2842 res
= mono_thread_pool_finish (ares
, &out_args
, &exc
);
2846 if (((MonoException
*)exc
)->stack_trace
) {
2847 char *strace
= mono_string_to_utf8 (((MonoException
*)exc
)->stack_trace
);
2849 tmp
= g_strdup_printf ("%s\nException Rethrown at:\n", strace
);
2851 MONO_OBJECT_SETREF (((MonoException
*)exc
), stack_trace
, mono_string_new (domain
, tmp
));
2854 mono_raise_exception ((MonoException
*)exc
);
2857 mono_method_return_message_restore (method
, params
, out_args
);
2864 mono_mb_emit_restore_result (MonoMethodBuilder
*mb
, MonoType
*return_type
)
2866 MonoType
*t
= mono_type_get_underlying_type (return_type
);
2868 if (return_type
->byref
)
2869 return_type
= &mono_defaults
.int_class
->byval_arg
;
2872 case MONO_TYPE_VOID
:
2873 g_assert_not_reached ();
2876 case MONO_TYPE_STRING
:
2877 case MONO_TYPE_CLASS
:
2878 case MONO_TYPE_OBJECT
:
2879 case MONO_TYPE_ARRAY
:
2880 case MONO_TYPE_SZARRAY
:
2884 case MONO_TYPE_BOOLEAN
:
2887 case MONO_TYPE_CHAR
:
2897 mono_mb_emit_op (mb
, CEE_UNBOX
, mono_class_from_mono_type (return_type
));
2898 mono_mb_emit_byte (mb
, mono_type_to_ldind (return_type
));
2900 case MONO_TYPE_GENERICINST
:
2901 if (!mono_type_generic_inst_is_valuetype (t
))
2904 case MONO_TYPE_VALUETYPE
: {
2905 MonoClass
*klass
= mono_class_from_mono_type (return_type
);
2906 mono_mb_emit_op (mb
, CEE_UNBOX
, klass
);
2907 mono_mb_emit_op (mb
, CEE_LDOBJ
, klass
);
2911 case MONO_TYPE_MVAR
: {
2912 MonoClass
*klass
= mono_class_from_mono_type (return_type
);
2913 mono_mb_emit_op (mb
, CEE_UNBOX_ANY
, klass
);
2917 g_warning ("type 0x%x not handled", return_type
->type
);
2918 g_assert_not_reached ();
2921 mono_mb_emit_byte (mb
, CEE_RET
);
2924 #endif /* DISABLE_JIT */
2927 mono_marshal_get_delegate_end_invoke (MonoMethod
*method
)
2929 MonoMethodSignature
*sig
;
2930 MonoMethodBuilder
*mb
;
2935 MonoGenericContext
*ctx
= NULL
;
2936 MonoMethod
*orig_method
= NULL
;
2938 g_assert (method
&& method
->klass
->parent
== mono_defaults
.multicastdelegate_class
&&
2939 !strcmp (method
->name
, "EndInvoke"));
2942 * For generic delegates, create a generic wrapper, and returns an instance to help AOT.
2944 if (method
->is_inflated
) {
2945 orig_method
= method
;
2946 ctx
= &((MonoMethodInflated
*)method
)->context
;
2947 method
= ((MonoMethodInflated
*)method
)->declaring
;
2950 sig
= mono_signature_no_pinvoke (method
);
2956 cache
= get_cache (&method
->klass
->image
->delegate_end_invoke_generic_cache
, mono_aligned_addr_hash
, NULL
);
2957 res
= check_generic_delegate_wrapper_cache (cache
, orig_method
, method
, ctx
);
2961 cache
= get_cache (&method
->klass
->image
->delegate_end_invoke_cache
,
2962 (GHashFunc
)mono_signature_hash
,
2963 (GCompareFunc
)mono_metadata_signature_equal
);
2964 if ((res
= mono_marshal_find_in_cache (cache
, sig
)))
2968 g_assert (sig
->hasthis
);
2970 name
= mono_signature_to_name (sig
, "end_invoke");
2972 mb
= mono_mb_new (method
->klass
, name
, MONO_WRAPPER_DELEGATE_END_INVOKE
);
2974 mb
= mono_mb_new (get_wrapper_target_class (method
->klass
->image
), name
, MONO_WRAPPER_DELEGATE_END_INVOKE
);
2978 params_var
= mono_mb_emit_save_args (mb
, sig
, FALSE
);
2980 mono_mb_emit_ldarg (mb
, 0);
2981 mono_mb_emit_ldloc (mb
, params_var
);
2982 mono_mb_emit_icall (mb
, mono_delegate_end_invoke
);
2984 if (sig
->ret
->type
== MONO_TYPE_VOID
) {
2985 mono_mb_emit_byte (mb
, CEE_POP
);
2986 mono_mb_emit_byte (mb
, CEE_RET
);
2988 mono_mb_emit_restore_result (mb
, sig
->ret
);
2993 def
= mono_mb_create_and_cache (cache
, method
->klass
, mb
, sig
, sig
->param_count
+ 16);
2994 res
= cache_generic_delegate_wrapper (cache
, orig_method
, def
, ctx
);
2996 res
= mono_mb_create_and_cache (cache
, sig
,
2997 mb
, sig
, sig
->param_count
+ 16);
3004 #ifndef DISABLE_REMOTING
3007 mono_remoting_wrapper (MonoMethod
*method
, gpointer
*params
)
3009 MonoMethodMessage
*msg
;
3010 MonoTransparentProxy
*this;
3011 MonoObject
*res
, *exc
;
3012 MonoArray
*out_args
;
3014 this = *((MonoTransparentProxy
**)params
[0]);
3017 g_assert (((MonoObject
*)this)->vtable
->klass
== mono_defaults
.transparent_proxy_class
);
3019 /* skip the this pointer */
3022 if (mono_class_is_contextbound (this->remote_class
->proxy_class
) && this->rp
->context
== (MonoObject
*) mono_context_get ())
3025 MonoMethodSignature
*sig
= mono_method_signature (method
);
3026 int count
= sig
->param_count
;
3027 gpointer
* mparams
= (gpointer
*) alloca(count
*sizeof(gpointer
));
3029 for (i
=0; i
<count
; i
++) {
3030 MonoClass
*class = mono_class_from_mono_type (sig
->params
[i
]);
3031 if (class->valuetype
) {
3032 if (sig
->params
[i
]->byref
) {
3033 mparams
[i
] = *((gpointer
*)params
[i
]);
3035 /* runtime_invoke expects a boxed instance */
3036 if (mono_class_is_nullable (mono_class_from_mono_type (sig
->params
[i
])))
3037 mparams
[i
] = mono_nullable_box (params
[i
], class);
3039 mparams
[i
] = params
[i
];
3042 mparams
[i
] = *((gpointer
**)params
[i
]);
3046 return mono_runtime_invoke (method
, method
->klass
->valuetype
? mono_object_unbox ((MonoObject
*)this): this, mparams
, NULL
);
3049 msg
= mono_method_call_message_new (method
, params
, NULL
, NULL
, NULL
);
3051 res
= mono_remoting_invoke ((MonoObject
*)this->rp
, msg
, &exc
, &out_args
);
3054 mono_raise_exception ((MonoException
*)exc
);
3056 mono_method_return_message_restore (method
, params
, out_args
);
3062 mono_marshal_get_remoting_invoke (MonoMethod
*method
)
3064 MonoMethodSignature
*sig
;
3065 MonoMethodBuilder
*mb
;
3071 if (method
->wrapper_type
== MONO_WRAPPER_REMOTING_INVOKE
|| method
->wrapper_type
== MONO_WRAPPER_XDOMAIN_INVOKE
)
3074 /* this seems to be the best plase to put this, as all remoting invokes seem to get filtered through here */
3076 if (mono_class_is_com_object (method
->klass
) || method
->klass
== mono_defaults
.com_object_class
) {
3077 MonoVTable
*vtable
= mono_class_vtable (mono_domain_get (), method
->klass
);
3078 g_assert (vtable
); /*FIXME do proper error handling*/
3080 if (!mono_vtable_is_remote (vtable
)) {
3081 return mono_cominterop_get_invoke (method
);
3086 sig
= mono_signature_no_pinvoke (method
);
3088 /* we cant remote methods without this pointer */
3092 if ((res
= mono_marshal_remoting_find_in_cache (method
, MONO_WRAPPER_REMOTING_INVOKE
)))
3095 mb
= mono_mb_new (method
->klass
, method
->name
, MONO_WRAPPER_REMOTING_INVOKE
);
3098 mb
->method
->save_lmf
= 1;
3100 params_var
= mono_mb_emit_save_args (mb
, sig
, TRUE
);
3102 mono_mb_emit_ptr (mb
, method
);
3103 mono_mb_emit_ldloc (mb
, params_var
);
3104 mono_mb_emit_icall (mb
, mono_remoting_wrapper
);
3105 emit_thread_interrupt_checkpoint (mb
);
3107 if (sig
->ret
->type
== MONO_TYPE_VOID
) {
3108 mono_mb_emit_byte (mb
, CEE_POP
);
3109 mono_mb_emit_byte (mb
, CEE_RET
);
3111 mono_mb_emit_restore_result (mb
, sig
->ret
);
3115 res
= mono_remoting_mb_create_and_cache (method
, mb
, sig
, sig
->param_count
+ 16);
3121 #endif /* DISABLE_REMOTING */
3124 /* mono_get_xdomain_marshal_type()
3125 * Returns the kind of marshalling that a type needs for cross domain calls.
3127 static MonoXDomainMarshalType
3128 mono_get_xdomain_marshal_type (MonoType
*t
)
3131 case MONO_TYPE_VOID
:
3132 g_assert_not_reached ();
3136 case MONO_TYPE_BOOLEAN
:
3139 case MONO_TYPE_CHAR
:
3146 return MONO_MARSHAL_NONE
;
3147 case MONO_TYPE_STRING
:
3148 return MONO_MARSHAL_COPY
;
3149 case MONO_TYPE_ARRAY
:
3150 case MONO_TYPE_SZARRAY
: {
3151 MonoClass
*elem_class
= mono_class_from_mono_type (t
)->element_class
;
3152 if (mono_get_xdomain_marshal_type (&(elem_class
->byval_arg
)) != MONO_MARSHAL_SERIALIZE
)
3153 return MONO_MARSHAL_COPY
;
3158 return MONO_MARSHAL_SERIALIZE
;
3162 /* mono_marshal_xdomain_copy_value
3163 * Makes a copy of "val" suitable for the current domain.
3166 mono_marshal_xdomain_copy_value (MonoObject
*val
)
3169 if (val
== NULL
) return NULL
;
3171 domain
= mono_domain_get ();
3173 switch (mono_object_class (val
)->byval_arg
.type
) {
3174 case MONO_TYPE_VOID
:
3175 g_assert_not_reached ();
3179 case MONO_TYPE_BOOLEAN
:
3182 case MONO_TYPE_CHAR
:
3188 case MONO_TYPE_R8
: {
3189 return mono_value_box (domain
, mono_object_class (val
), ((char*)val
) + sizeof(MonoObject
));
3191 case MONO_TYPE_STRING
: {
3192 MonoString
*str
= (MonoString
*) val
;
3193 return (MonoObject
*) mono_string_new_utf16 (domain
, mono_string_chars (str
), mono_string_length (str
));
3195 case MONO_TYPE_ARRAY
:
3196 case MONO_TYPE_SZARRAY
: {
3198 MonoXDomainMarshalType mt
= mono_get_xdomain_marshal_type (&(mono_object_class (val
)->element_class
->byval_arg
));
3199 if (mt
== MONO_MARSHAL_SERIALIZE
) return NULL
;
3200 acopy
= mono_array_clone_in_domain (domain
, (MonoArray
*) val
);
3201 if (mt
== MONO_MARSHAL_COPY
) {
3202 int i
, len
= mono_array_length (acopy
);
3203 for (i
= 0; i
< len
; i
++) {
3204 MonoObject
*item
= mono_array_get (acopy
, gpointer
, i
);
3205 mono_array_setref (acopy
, i
, mono_marshal_xdomain_copy_value (item
));
3208 return (MonoObject
*) acopy
;
3212 if (mono_object_class (val
) == mono_defaults
.stringbuilder_class
) {
3213 MonoStringBuilder
*oldsb
= (MonoStringBuilder
*) val
;
3214 MonoStringBuilder
*newsb
= (MonoStringBuilder
*) mono_object_new (domain
, mono_defaults
.stringbuilder_class
);
3215 MONO_OBJECT_SETREF (newsb
, str
, mono_string_new_utf16 (domain
, mono_string_chars (oldsb
->str
), mono_string_length (oldsb
->str
)));
3216 newsb
->length
= oldsb
->length
;
3217 newsb
->max_capacity
= (gint32
)0x7fffffff;
3218 return (MonoObject
*) newsb
;
3223 /* mono_marshal_xdomain_copy_out_value()
3224 * Copies the contents of the src instance into the dst instance. src and dst
3225 * must have the same type, and if they are arrays, the same size.
3228 mono_marshal_xdomain_copy_out_value (MonoObject
*src
, MonoObject
*dst
)
3230 if (src
== NULL
|| dst
== NULL
) return;
3232 g_assert (mono_object_class (src
) == mono_object_class (dst
));
3234 switch (mono_object_class (src
)->byval_arg
.type
) {
3235 case MONO_TYPE_ARRAY
:
3236 case MONO_TYPE_SZARRAY
: {
3237 int mt
= mono_get_xdomain_marshal_type (&(mono_object_class (src
)->element_class
->byval_arg
));
3238 if (mt
== MONO_MARSHAL_SERIALIZE
) return;
3239 if (mt
== MONO_MARSHAL_COPY
) {
3240 int i
, len
= mono_array_length ((MonoArray
*)dst
);
3241 for (i
= 0; i
< len
; i
++) {
3242 MonoObject
*item
= mono_array_get ((MonoArray
*)src
, gpointer
, i
);
3243 mono_array_setref ((MonoArray
*)dst
, i
, mono_marshal_xdomain_copy_value (item
));
3246 mono_array_full_copy ((MonoArray
*)src
, (MonoArray
*)dst
);
3252 if (mono_object_class (src
) == mono_defaults
.stringbuilder_class
) {
3253 MonoStringBuilder
*src_sb
= (MonoStringBuilder
*) src
;
3254 MonoStringBuilder
*dst_sb
= (MonoStringBuilder
*) dst
;
3256 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
)));
3257 dst_sb
->cached_str
= NULL
;
3258 dst_sb
->length
= src_sb
->length
;
3263 #if !(defined (DISABLE_JIT) || defined (DISABLE_REMOTING))
3266 mono_marshal_emit_xdomain_copy_value (MonoMethodBuilder
*mb
, MonoClass
*pclass
)
3268 mono_mb_emit_icall (mb
, mono_marshal_xdomain_copy_value
);
3269 mono_mb_emit_op (mb
, CEE_CASTCLASS
, pclass
);
3273 mono_marshal_emit_xdomain_copy_out_value (MonoMethodBuilder
*mb
, MonoClass
*pclass
)
3275 mono_mb_emit_icall (mb
, mono_marshal_xdomain_copy_out_value
);
3279 #ifndef DISABLE_REMOTING
3280 /* mono_marshal_supports_fast_xdomain()
3281 * Returns TRUE if the method can use the fast xdomain wrapper.
3284 mono_marshal_supports_fast_xdomain (MonoMethod
*method
)
3286 return !mono_class_is_contextbound (method
->klass
) &&
3287 !((method
->flags
& METHOD_ATTRIBUTE_SPECIAL_NAME
) && (strcmp (".ctor", method
->name
) == 0));
3292 mono_marshal_set_domain_by_id (gint32 id
, MonoBoolean push
)
3294 MonoDomain
*current_domain
= mono_domain_get ();
3295 MonoDomain
*domain
= mono_domain_get_by_id (id
);
3297 if (!domain
|| !mono_domain_set (domain
, FALSE
))
3298 mono_raise_exception (mono_get_exception_appdomain_unloaded ());
3301 mono_thread_push_appdomain_ref (domain
);
3303 mono_thread_pop_appdomain_ref ();
3305 return current_domain
->domain_id
;
3308 #if !(defined (DISABLE_JIT) || defined (DISABLE_REMOTING))
3310 mono_marshal_emit_switch_domain (MonoMethodBuilder
*mb
)
3312 mono_mb_emit_icall (mb
, mono_marshal_set_domain_by_id
);
3315 /* mono_marshal_emit_load_domain_method ()
3316 * Loads into the stack a pointer to the code of the provided method for
3317 * the current domain.
3320 mono_marshal_emit_load_domain_method (MonoMethodBuilder
*mb
, MonoMethod
*method
)
3322 /* We need a pointer to the method for the running domain (not the domain
3323 * that compiles the method).
3325 mono_mb_emit_ptr (mb
, method
);
3326 mono_mb_emit_icall (mb
, mono_compile_method
);
3330 /* mono_marshal_check_domain_image ()
3331 * Returns TRUE if the image is loaded in the specified
3332 * application domain.
3335 mono_marshal_check_domain_image (gint32 domain_id
, MonoImage
*image
)
3340 MonoDomain
*domain
= mono_domain_get_by_id (domain_id
);
3344 mono_domain_assemblies_lock (domain
);
3345 for (tmp
= domain
->domain_assemblies
; tmp
; tmp
= tmp
->next
) {
3347 if (ass
->image
== image
)
3350 mono_domain_assemblies_unlock (domain
);
3355 #ifndef DISABLE_REMOTING
3357 /* mono_marshal_get_xappdomain_dispatch ()
3358 * Generates a method that dispatches a method call from another domain into
3359 * the current domain.
3362 mono_marshal_get_xappdomain_dispatch (MonoMethod
*method
, int *marshal_types
, int complex_count
, int complex_out_count
, int ret_marshal_type
)
3364 MonoMethodSignature
*sig
, *csig
;
3365 MonoMethodBuilder
*mb
;
3367 int i
, j
, param_index
, copy_locals_base
;
3368 MonoClass
*ret_class
= NULL
;
3369 int loc_array
=0, loc_return
=0, loc_serialized_exc
=0;
3370 MonoExceptionClause
*main_clause
;
3372 gboolean copy_return
;
3374 if ((res
= mono_marshal_remoting_find_in_cache (method
, MONO_WRAPPER_XDOMAIN_DISPATCH
)))
3377 sig
= mono_method_signature (method
);
3378 copy_return
= (sig
->ret
->type
!= MONO_TYPE_VOID
&& ret_marshal_type
!= MONO_MARSHAL_SERIALIZE
);
3381 csig
= mono_metadata_signature_alloc (mono_defaults
.corlib
, 3 + sig
->param_count
- complex_count
);
3382 csig
->params
[j
++] = &mono_defaults
.object_class
->byval_arg
;
3383 csig
->params
[j
++] = &byte_array_class
->this_arg
;
3384 csig
->params
[j
++] = &byte_array_class
->this_arg
;
3385 for (i
= 0; i
< sig
->param_count
; i
++) {
3386 if (marshal_types
[i
] != MONO_MARSHAL_SERIALIZE
)
3387 csig
->params
[j
++] = sig
->params
[i
];
3390 csig
->ret
= sig
->ret
;
3392 csig
->ret
= &mono_defaults
.void_class
->byval_arg
;
3394 csig
->hasthis
= FALSE
;
3396 mb
= mono_mb_new (method
->klass
, method
->name
, MONO_WRAPPER_XDOMAIN_DISPATCH
);
3397 mb
->method
->save_lmf
= 1;
3402 loc_serialized_exc
= mono_mb_add_local (mb
, &byte_array_class
->byval_arg
);
3403 if (complex_count
> 0)
3404 loc_array
= mono_mb_add_local (mb
, &mono_defaults
.object_class
->byval_arg
);
3405 if (sig
->ret
->type
!= MONO_TYPE_VOID
) {
3406 loc_return
= mono_mb_add_local (mb
, sig
->ret
);
3407 ret_class
= mono_class_from_mono_type (sig
->ret
);
3412 main_clause
= mono_image_alloc0 (method
->klass
->image
, sizeof (MonoExceptionClause
));
3413 main_clause
->try_offset
= mono_mb_get_label (mb
);
3415 /* Clean the call context */
3417 mono_mb_emit_byte (mb
, CEE_LDNULL
);
3418 mono_mb_emit_managed_call (mb
, method_set_call_context
, NULL
);
3419 mono_mb_emit_byte (mb
, CEE_POP
);
3421 /* Deserialize call data */
3423 mono_mb_emit_ldarg (mb
, 1);
3424 mono_mb_emit_byte (mb
, CEE_LDIND_REF
);
3425 mono_mb_emit_byte (mb
, CEE_DUP
);
3426 pos
= mono_mb_emit_short_branch (mb
, CEE_BRFALSE_S
);
3428 mono_marshal_emit_xdomain_copy_value (mb
, byte_array_class
);
3429 mono_mb_emit_managed_call (mb
, method_rs_deserialize
, NULL
);
3431 if (complex_count
> 0)
3432 mono_mb_emit_stloc (mb
, loc_array
);
3434 mono_mb_emit_byte (mb
, CEE_POP
);
3436 mono_mb_patch_short_branch (mb
, pos
);
3438 /* Get the target object */
3440 mono_mb_emit_ldarg (mb
, 0);
3441 mono_mb_emit_managed_call (mb
, method_rs_appdomain_target
, NULL
);
3443 /* Load the arguments */
3445 copy_locals_base
= mb
->locals
;
3446 param_index
= 3; // Index of the first non-serialized parameter of this wrapper
3448 for (i
= 0; i
< sig
->param_count
; i
++) {
3449 MonoType
*pt
= sig
->params
[i
];
3450 MonoClass
*pclass
= mono_class_from_mono_type (pt
);
3451 switch (marshal_types
[i
]) {
3452 case MONO_MARSHAL_SERIALIZE
: {
3453 /* take the value from the serialized array */
3454 mono_mb_emit_ldloc (mb
, loc_array
);
3455 mono_mb_emit_icon (mb
, j
++);
3457 if (pclass
->valuetype
) {
3458 mono_mb_emit_byte (mb
, CEE_LDELEM_REF
);
3459 mono_mb_emit_op (mb
, CEE_UNBOX
, pclass
);
3461 mono_mb_emit_op (mb
, CEE_LDELEMA
, pclass
);
3464 if (pclass
->valuetype
) {
3465 mono_mb_emit_byte (mb
, CEE_LDELEM_REF
);
3466 mono_mb_emit_op (mb
, CEE_UNBOX
, pclass
);
3467 mono_mb_emit_op (mb
, CEE_LDOBJ
, pclass
);
3469 mono_mb_emit_byte (mb
, CEE_LDELEM_REF
);
3470 if (pclass
!= mono_defaults
.object_class
) {
3471 mono_mb_emit_op (mb
, CEE_CASTCLASS
, pclass
);
3477 case MONO_MARSHAL_COPY_OUT
: {
3478 /* Keep a local copy of the value since we need to copy it back after the call */
3479 int copy_local
= mono_mb_add_local (mb
, &(pclass
->byval_arg
));
3480 mono_mb_emit_ldarg (mb
, param_index
++);
3481 mono_marshal_emit_xdomain_copy_value (mb
, pclass
);
3482 mono_mb_emit_byte (mb
, CEE_DUP
);
3483 mono_mb_emit_stloc (mb
, copy_local
);
3486 case MONO_MARSHAL_COPY
: {
3487 mono_mb_emit_ldarg (mb
, param_index
);
3489 mono_mb_emit_byte (mb
, CEE_DUP
);
3490 mono_mb_emit_byte (mb
, CEE_DUP
);
3491 mono_mb_emit_byte (mb
, CEE_LDIND_REF
);
3492 mono_marshal_emit_xdomain_copy_value (mb
, pclass
);
3493 mono_mb_emit_byte (mb
, CEE_STIND_REF
);
3495 mono_marshal_emit_xdomain_copy_value (mb
, pclass
);
3500 case MONO_MARSHAL_NONE
:
3501 mono_mb_emit_ldarg (mb
, param_index
++);
3506 /* Make the call to the real object */
3508 emit_thread_force_interrupt_checkpoint (mb
);
3510 mono_mb_emit_op (mb
, CEE_CALLVIRT
, method
);
3512 if (sig
->ret
->type
!= MONO_TYPE_VOID
)
3513 mono_mb_emit_stloc (mb
, loc_return
);
3515 /* copy back MONO_MARSHAL_COPY_OUT parameters */
3519 for (i
= 0; i
< sig
->param_count
; i
++) {
3520 if (marshal_types
[i
] == MONO_MARSHAL_SERIALIZE
) continue;
3521 if (marshal_types
[i
] == MONO_MARSHAL_COPY_OUT
) {
3522 mono_mb_emit_ldloc (mb
, copy_locals_base
+ (j
++));
3523 mono_mb_emit_ldarg (mb
, param_index
);
3524 mono_marshal_emit_xdomain_copy_out_value (mb
, mono_class_from_mono_type (sig
->params
[i
]));
3529 /* Serialize the return values */
3531 if (complex_out_count
> 0) {
3532 /* Reset parameters in the array that don't need to be serialized back */
3534 for (i
= 0; i
< sig
->param_count
; i
++) {
3535 if (marshal_types
[i
] != MONO_MARSHAL_SERIALIZE
) continue;
3536 if (!sig
->params
[i
]->byref
) {
3537 mono_mb_emit_ldloc (mb
, loc_array
);
3538 mono_mb_emit_icon (mb
, j
);
3539 mono_mb_emit_byte (mb
, CEE_LDNULL
);
3540 mono_mb_emit_byte (mb
, CEE_STELEM_REF
);
3545 /* Add the return value to the array */
3547 if (ret_marshal_type
== MONO_MARSHAL_SERIALIZE
) {
3548 mono_mb_emit_ldloc (mb
, loc_array
);
3549 mono_mb_emit_icon (mb
, complex_count
); /* The array has an additional slot to hold the ret value */
3550 mono_mb_emit_ldloc (mb
, loc_return
);
3552 g_assert (ret_class
); /*FIXME properly fail here*/
3553 if (ret_class
->valuetype
) {
3554 mono_mb_emit_op (mb
, CEE_BOX
, ret_class
);
3556 mono_mb_emit_byte (mb
, CEE_STELEM_REF
);
3561 mono_mb_emit_ldarg (mb
, 1);
3562 mono_mb_emit_ldloc (mb
, loc_array
);
3563 mono_mb_emit_managed_call (mb
, method_rs_serialize
, NULL
);
3564 mono_mb_emit_byte (mb
, CEE_STIND_REF
);
3565 } else if (ret_marshal_type
== MONO_MARSHAL_SERIALIZE
) {
3566 mono_mb_emit_ldarg (mb
, 1);
3567 mono_mb_emit_ldloc (mb
, loc_return
);
3568 if (ret_class
->valuetype
) {
3569 mono_mb_emit_op (mb
, CEE_BOX
, ret_class
);
3571 mono_mb_emit_managed_call (mb
, method_rs_serialize
, NULL
);
3572 mono_mb_emit_byte (mb
, CEE_STIND_REF
);
3574 mono_mb_emit_ldarg (mb
, 1);
3575 mono_mb_emit_byte (mb
, CEE_LDNULL
);
3576 mono_mb_emit_managed_call (mb
, method_rs_serialize
, NULL
);
3577 mono_mb_emit_byte (mb
, CEE_STIND_REF
);
3580 mono_mb_emit_ldarg (mb
, 2);
3581 mono_mb_emit_byte (mb
, CEE_LDNULL
);
3582 mono_mb_emit_byte (mb
, CEE_STIND_REF
);
3583 pos_leave
= mono_mb_emit_branch (mb
, CEE_LEAVE
);
3585 /* Main exception catch */
3586 main_clause
->flags
= MONO_EXCEPTION_CLAUSE_NONE
;
3587 main_clause
->try_len
= mono_mb_get_pos (mb
) - main_clause
->try_offset
;
3588 main_clause
->data
.catch_class
= mono_defaults
.object_class
;
3591 main_clause
->handler_offset
= mono_mb_get_label (mb
);
3592 mono_mb_emit_managed_call (mb
, method_rs_serialize_exc
, NULL
);
3593 mono_mb_emit_stloc (mb
, loc_serialized_exc
);
3594 mono_mb_emit_ldarg (mb
, 2);
3595 mono_mb_emit_ldloc (mb
, loc_serialized_exc
);
3596 mono_mb_emit_byte (mb
, CEE_STIND_REF
);
3597 mono_mb_emit_branch (mb
, CEE_LEAVE
);
3598 main_clause
->handler_len
= mono_mb_get_pos (mb
) - main_clause
->handler_offset
;
3601 mono_mb_patch_branch (mb
, pos_leave
);
3604 mono_mb_emit_ldloc (mb
, loc_return
);
3606 mono_mb_emit_byte (mb
, CEE_RET
);
3608 mono_mb_set_clauses (mb
, 1, main_clause
);
3611 res
= mono_remoting_mb_create_and_cache (method
, mb
, csig
, csig
->param_count
+ 16);
3617 /* mono_marshal_get_xappdomain_invoke ()
3618 * Generates a fast remoting wrapper for cross app domain calls.
3621 mono_marshal_get_xappdomain_invoke (MonoMethod
*method
)
3623 MonoMethodSignature
*sig
;
3624 MonoMethodBuilder
*mb
;
3626 int i
, j
, complex_count
, complex_out_count
, copy_locals_base
;
3628 MonoClass
*ret_class
= NULL
;
3629 MonoMethod
*xdomain_method
;
3630 int ret_marshal_type
= MONO_MARSHAL_NONE
;
3631 int loc_array
=0, loc_serialized_data
=-1, loc_real_proxy
;
3632 int loc_old_domainid
, loc_domainid
, loc_return
=0, loc_serialized_exc
=0, loc_context
;
3633 int pos
, pos_dispatch
, pos_noex
;
3634 gboolean copy_return
= FALSE
;
3638 if (method
->wrapper_type
== MONO_WRAPPER_REMOTING_INVOKE
|| method
->wrapper_type
== MONO_WRAPPER_XDOMAIN_INVOKE
)
3641 /* we cant remote methods without this pointer */
3642 if (!mono_method_signature (method
)->hasthis
)
3645 if (!mono_marshal_supports_fast_xdomain (method
))
3646 return mono_marshal_get_remoting_invoke (method
);
3648 mono_remoting_marshal_init ();
3650 if ((res
= mono_marshal_remoting_find_in_cache (method
, MONO_WRAPPER_XDOMAIN_INVOKE
)))
3653 sig
= mono_signature_no_pinvoke (method
);
3655 mb
= mono_mb_new (method
->klass
, method
->name
, MONO_WRAPPER_XDOMAIN_INVOKE
);
3656 mb
->method
->save_lmf
= 1;
3658 /* Count the number of parameters that need to be serialized */
3660 marshal_types
= alloca (sizeof (int) * sig
->param_count
);
3661 complex_count
= complex_out_count
= 0;
3662 for (i
= 0; i
< sig
->param_count
; i
++) {
3663 MonoType
*ptype
= sig
->params
[i
];
3664 int mt
= mono_get_xdomain_marshal_type (ptype
);
3666 /* If the [Out] attribute is applied to a parameter that can be internally copied,
3667 * the copy will be made by reusing the original object instance
3669 if ((ptype
->attrs
& PARAM_ATTRIBUTE_OUT
) != 0 && mt
== MONO_MARSHAL_COPY
&& !ptype
->byref
)
3670 mt
= MONO_MARSHAL_COPY_OUT
;
3671 else if (mt
== MONO_MARSHAL_SERIALIZE
) {
3673 if (ptype
->byref
) complex_out_count
++;
3675 marshal_types
[i
] = mt
;
3678 if (sig
->ret
->type
!= MONO_TYPE_VOID
) {
3679 ret_marshal_type
= mono_get_xdomain_marshal_type (sig
->ret
);
3680 ret_class
= mono_class_from_mono_type (sig
->ret
);
3681 copy_return
= ret_marshal_type
!= MONO_MARSHAL_SERIALIZE
;
3687 if (complex_count
> 0)
3688 loc_array
= mono_mb_add_local (mb
, &mono_defaults
.object_class
->byval_arg
);
3689 loc_serialized_data
= mono_mb_add_local (mb
, &byte_array_class
->byval_arg
);
3690 loc_real_proxy
= mono_mb_add_local (mb
, &mono_defaults
.object_class
->byval_arg
);
3692 loc_return
= mono_mb_add_local (mb
, sig
->ret
);
3693 loc_old_domainid
= mono_mb_add_local (mb
, &mono_defaults
.int32_class
->byval_arg
);
3694 loc_domainid
= mono_mb_add_local (mb
, &mono_defaults
.int32_class
->byval_arg
);
3695 loc_serialized_exc
= mono_mb_add_local (mb
, &byte_array_class
->byval_arg
);
3696 loc_context
= mono_mb_add_local (mb
, &mono_defaults
.object_class
->byval_arg
);
3698 /* Save thread domain data */
3700 mono_mb_emit_icall (mb
, mono_context_get
);
3701 mono_mb_emit_byte (mb
, CEE_DUP
);
3702 mono_mb_emit_stloc (mb
, loc_context
);
3704 /* If the thread is not running in the default context, it needs to go
3705 * through the whole remoting sink, since the context is going to change
3707 mono_mb_emit_managed_call (mb
, method_needs_context_sink
, NULL
);
3708 pos
= mono_mb_emit_short_branch (mb
, CEE_BRTRUE_S
);
3710 /* Another case in which the fast path can't be used: when the target domain
3711 * has a different image for the same assembly.
3714 /* Get the target domain id */
3716 mono_mb_emit_ldarg (mb
, 0);
3717 mono_mb_emit_ldflda (mb
, G_STRUCT_OFFSET (MonoTransparentProxy
, rp
));
3718 mono_mb_emit_byte (mb
, CEE_LDIND_REF
);
3719 mono_mb_emit_byte (mb
, CEE_DUP
);
3720 mono_mb_emit_stloc (mb
, loc_real_proxy
);
3722 mono_mb_emit_ldflda (mb
, G_STRUCT_OFFSET (MonoRealProxy
, target_domain_id
));
3723 mono_mb_emit_byte (mb
, CEE_LDIND_I4
);
3724 mono_mb_emit_stloc (mb
, loc_domainid
);
3726 /* Check if the target domain has the same image for the required assembly */
3728 mono_mb_emit_ldloc (mb
, loc_domainid
);
3729 mono_mb_emit_ptr (mb
, method
->klass
->image
);
3730 mono_mb_emit_icall (mb
, mono_marshal_check_domain_image
);
3731 pos_dispatch
= mono_mb_emit_short_branch (mb
, CEE_BRTRUE_S
);
3733 /* Use the whole remoting sink to dispatch this message */
3735 mono_mb_patch_short_branch (mb
, pos
);
3737 mono_mb_emit_ldarg (mb
, 0);
3738 for (i
= 0; i
< sig
->param_count
; i
++)
3739 mono_mb_emit_ldarg (mb
, i
+ 1);
3741 mono_mb_emit_managed_call (mb
, mono_marshal_get_remoting_invoke (method
), NULL
);
3742 mono_mb_emit_byte (mb
, CEE_RET
);
3743 mono_mb_patch_short_branch (mb
, pos_dispatch
);
3745 /* Create the array that will hold the parameters to be serialized */
3747 if (complex_count
> 0) {
3748 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 */
3749 mono_mb_emit_op (mb
, CEE_NEWARR
, mono_defaults
.object_class
);
3752 for (i
= 0; i
< sig
->param_count
; i
++) {
3754 if (marshal_types
[i
] != MONO_MARSHAL_SERIALIZE
) continue;
3755 pclass
= mono_class_from_mono_type (sig
->params
[i
]);
3756 mono_mb_emit_byte (mb
, CEE_DUP
);
3757 mono_mb_emit_icon (mb
, j
);
3758 mono_mb_emit_ldarg (mb
, i
+ 1); /* 0=this */
3759 if (sig
->params
[i
]->byref
) {
3760 if (pclass
->valuetype
)
3761 mono_mb_emit_op (mb
, CEE_LDOBJ
, pclass
);
3763 mono_mb_emit_byte (mb
, CEE_LDIND_REF
);
3765 if (pclass
->valuetype
)
3766 mono_mb_emit_op (mb
, CEE_BOX
, pclass
);
3767 mono_mb_emit_byte (mb
, CEE_STELEM_REF
);
3770 mono_mb_emit_stloc (mb
, loc_array
);
3772 /* Serialize parameters */
3774 mono_mb_emit_ldloc (mb
, loc_array
);
3775 mono_mb_emit_managed_call (mb
, method_rs_serialize
, NULL
);
3776 mono_mb_emit_stloc (mb
, loc_serialized_data
);
3778 mono_mb_emit_byte (mb
, CEE_LDNULL
);
3779 mono_mb_emit_managed_call (mb
, method_rs_serialize
, NULL
);
3780 mono_mb_emit_stloc (mb
, loc_serialized_data
);
3785 mono_mb_emit_ldloc (mb
, loc_domainid
);
3786 mono_mb_emit_byte (mb
, CEE_LDC_I4_1
);
3787 mono_marshal_emit_switch_domain (mb
);
3788 mono_mb_emit_stloc (mb
, loc_old_domainid
);
3790 /* Load the arguments */
3792 mono_mb_emit_ldloc (mb
, loc_real_proxy
);
3793 mono_mb_emit_ldloc_addr (mb
, loc_serialized_data
);
3794 mono_mb_emit_ldloc_addr (mb
, loc_serialized_exc
);
3796 copy_locals_base
= mb
->locals
;
3797 for (i
= 0; i
< sig
->param_count
; i
++) {
3798 switch (marshal_types
[i
]) {
3799 case MONO_MARSHAL_SERIALIZE
:
3801 case MONO_MARSHAL_COPY
: {
3802 mono_mb_emit_ldarg (mb
, i
+1);
3803 if (sig
->params
[i
]->byref
) {
3804 /* make a local copy of the byref parameter. The real parameter
3805 * will be updated after the xdomain call
3807 MonoClass
*pclass
= mono_class_from_mono_type (sig
->params
[i
]);
3808 int copy_local
= mono_mb_add_local (mb
, &(pclass
->byval_arg
));
3809 mono_mb_emit_byte (mb
, CEE_LDIND_REF
);
3810 mono_mb_emit_stloc (mb
, copy_local
);
3811 mono_mb_emit_ldloc_addr (mb
, copy_local
);
3815 case MONO_MARSHAL_COPY_OUT
:
3816 case MONO_MARSHAL_NONE
:
3817 mono_mb_emit_ldarg (mb
, i
+1);
3822 /* Make the call to the invoke wrapper in the target domain */
3824 xdomain_method
= mono_marshal_get_xappdomain_dispatch (method
, marshal_types
, complex_count
, complex_out_count
, ret_marshal_type
);
3825 mono_marshal_emit_load_domain_method (mb
, xdomain_method
);
3826 mono_mb_emit_calli (mb
, mono_method_signature (xdomain_method
));
3829 mono_mb_emit_stloc (mb
, loc_return
);
3833 mono_mb_emit_ldloc (mb
, loc_old_domainid
);
3834 mono_mb_emit_byte (mb
, CEE_LDC_I4_0
);
3835 mono_marshal_emit_switch_domain (mb
);
3836 mono_mb_emit_byte (mb
, CEE_POP
);
3838 /* Restore thread domain data */
3840 mono_mb_emit_ldloc (mb
, loc_context
);
3841 mono_mb_emit_icall (mb
, mono_context_set
);
3843 /* if (loc_serialized_exc != null) ... */
3845 mono_mb_emit_ldloc (mb
, loc_serialized_exc
);
3846 pos_noex
= mono_mb_emit_short_branch (mb
, CEE_BRFALSE_S
);
3848 mono_mb_emit_ldloc (mb
, loc_serialized_exc
);
3849 mono_marshal_emit_xdomain_copy_value (mb
, byte_array_class
);
3850 mono_mb_emit_managed_call (mb
, method_rs_deserialize
, NULL
);
3851 mono_mb_emit_op (mb
, CEE_CASTCLASS
, mono_defaults
.exception_class
);
3852 mono_mb_emit_managed_call (mb
, method_exc_fixexc
, NULL
);
3853 mono_mb_emit_byte (mb
, CEE_THROW
);
3854 mono_mb_patch_short_branch (mb
, pos_noex
);
3856 /* copy back non-serialized output parameters */
3859 for (i
= 0; i
< sig
->param_count
; i
++) {
3860 if (!sig
->params
[i
]->byref
|| marshal_types
[i
] != MONO_MARSHAL_COPY
) continue;
3861 mono_mb_emit_ldarg (mb
, i
+ 1);
3862 mono_mb_emit_ldloc (mb
, copy_locals_base
+ (j
++));
3863 mono_marshal_emit_xdomain_copy_value (mb
, mono_class_from_mono_type (sig
->params
[i
]));
3864 mono_mb_emit_byte (mb
, CEE_STIND_REF
);
3867 /* Deserialize out parameters */
3869 if (complex_out_count
> 0) {
3870 mono_mb_emit_ldloc (mb
, loc_serialized_data
);
3871 mono_marshal_emit_xdomain_copy_value (mb
, byte_array_class
);
3872 mono_mb_emit_managed_call (mb
, method_rs_deserialize
, NULL
);
3873 mono_mb_emit_stloc (mb
, loc_array
);
3875 /* Copy back output parameters and return type */
3878 for (i
= 0; i
< sig
->param_count
; i
++) {
3879 if (marshal_types
[i
] != MONO_MARSHAL_SERIALIZE
) continue;
3880 if (sig
->params
[i
]->byref
) {
3881 MonoClass
*pclass
= mono_class_from_mono_type (sig
->params
[i
]);
3882 mono_mb_emit_ldarg (mb
, i
+ 1);
3883 mono_mb_emit_ldloc (mb
, loc_array
);
3884 mono_mb_emit_icon (mb
, j
);
3885 mono_mb_emit_byte (mb
, CEE_LDELEM_REF
);
3886 if (pclass
->valuetype
) {
3887 mono_mb_emit_op (mb
, CEE_UNBOX
, pclass
);
3888 mono_mb_emit_op (mb
, CEE_LDOBJ
, pclass
);
3889 mono_mb_emit_op (mb
, CEE_STOBJ
, pclass
);
3891 if (pclass
!= mono_defaults
.object_class
)
3892 mono_mb_emit_op (mb
, CEE_CASTCLASS
, pclass
);
3893 mono_mb_emit_byte (mb
, CEE_STIND_REF
);
3899 if (ret_marshal_type
== MONO_MARSHAL_SERIALIZE
) {
3900 mono_mb_emit_ldloc (mb
, loc_array
);
3901 mono_mb_emit_icon (mb
, complex_count
);
3902 mono_mb_emit_byte (mb
, CEE_LDELEM_REF
);
3903 if (ret_class
->valuetype
) {
3904 mono_mb_emit_op (mb
, CEE_UNBOX
, ret_class
);
3905 mono_mb_emit_op (mb
, CEE_LDOBJ
, ret_class
);
3908 } else if (ret_marshal_type
== MONO_MARSHAL_SERIALIZE
) {
3909 mono_mb_emit_ldloc (mb
, loc_serialized_data
);
3910 mono_marshal_emit_xdomain_copy_value (mb
, byte_array_class
);
3911 mono_mb_emit_managed_call (mb
, method_rs_deserialize
, NULL
);
3912 if (ret_class
->valuetype
) {
3913 mono_mb_emit_op (mb
, CEE_UNBOX
, ret_class
);
3914 mono_mb_emit_op (mb
, CEE_LDOBJ
, ret_class
);
3915 } else if (ret_class
!= mono_defaults
.object_class
) {
3916 mono_mb_emit_op (mb
, CEE_CASTCLASS
, ret_class
);
3919 mono_mb_emit_ldloc (mb
, loc_serialized_data
);
3920 mono_mb_emit_byte (mb
, CEE_DUP
);
3921 pos
= mono_mb_emit_short_branch (mb
, CEE_BRFALSE_S
);
3922 mono_marshal_emit_xdomain_copy_value (mb
, byte_array_class
);
3924 mono_mb_patch_short_branch (mb
, pos
);
3925 mono_mb_emit_managed_call (mb
, method_rs_deserialize
, NULL
);
3926 mono_mb_emit_byte (mb
, CEE_POP
);
3930 mono_mb_emit_ldloc (mb
, loc_return
);
3931 if (ret_marshal_type
== MONO_MARSHAL_COPY
)
3932 mono_marshal_emit_xdomain_copy_value (mb
, ret_class
);
3935 mono_mb_emit_byte (mb
, CEE_RET
);
3936 #endif /* DISABLE_JIT */
3938 res
= mono_remoting_mb_create_and_cache (method
, mb
, sig
, sig
->param_count
+ 16);
3945 mono_marshal_get_remoting_invoke_for_target (MonoMethod
*method
, MonoRemotingTarget target_type
)
3947 if (target_type
== MONO_REMOTING_TARGET_APPDOMAIN
) {
3948 return mono_marshal_get_xappdomain_invoke (method
);
3949 } else if (target_type
== MONO_REMOTING_TARGET_COMINTEROP
) {
3951 return mono_cominterop_get_invoke (method
);
3953 g_assert_not_reached ();
3956 return mono_marshal_get_remoting_invoke (method
);
3962 G_GNUC_UNUSED
static gpointer
3963 mono_marshal_load_remoting_wrapper (MonoRealProxy
*rp
, MonoMethod
*method
)
3965 if (rp
->target_domain_id
!= -1)
3966 return mono_compile_method (mono_marshal_get_xappdomain_invoke (method
));
3968 return mono_compile_method (mono_marshal_get_remoting_invoke (method
));
3972 mono_marshal_get_remoting_invoke_with_check (MonoMethod
*method
)
3974 MonoMethodSignature
*sig
;
3975 MonoMethodBuilder
*mb
;
3976 MonoMethod
*res
, *native
;
3977 int i
, pos
, pos_rem
;
3981 if (method
->wrapper_type
== MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK
)
3984 /* we cant remote methods without this pointer */
3985 g_assert (mono_method_signature (method
)->hasthis
);
3987 if ((res
= mono_marshal_remoting_find_in_cache (method
, MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK
)))
3990 sig
= mono_signature_no_pinvoke (method
);
3992 mb
= mono_mb_new (method
->klass
, method
->name
, MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK
);
3995 for (i
= 0; i
<= sig
->param_count
; i
++)
3996 mono_mb_emit_ldarg (mb
, i
);
3998 mono_mb_emit_ldarg (mb
, 0);
3999 pos
= mono_mb_emit_proxy_check (mb
, CEE_BNE_UN
);
4001 if (mono_marshal_supports_fast_xdomain (method
)) {
4002 mono_mb_emit_ldarg (mb
, 0);
4003 pos_rem
= mono_mb_emit_xdomain_check (mb
, CEE_BEQ
);
4005 /* wrapper for cross app domain calls */
4006 native
= mono_marshal_get_xappdomain_invoke (method
);
4007 mono_mb_emit_managed_call (mb
, native
, mono_method_signature (native
));
4008 mono_mb_emit_byte (mb
, CEE_RET
);
4010 mono_mb_patch_branch (mb
, pos_rem
);
4012 /* wrapper for normal remote calls */
4013 native
= mono_marshal_get_remoting_invoke (method
);
4014 mono_mb_emit_managed_call (mb
, native
, mono_method_signature (native
));
4015 mono_mb_emit_byte (mb
, CEE_RET
);
4018 mono_mb_patch_branch (mb
, pos
);
4019 mono_mb_emit_managed_call (mb
, method
, mono_method_signature (method
));
4020 mono_mb_emit_byte (mb
, CEE_RET
);
4023 res
= mono_remoting_mb_create_and_cache (method
, mb
, sig
, sig
->param_count
+ 16);
4029 #endif /* DISABLE_REMOTING */
4033 MonoMethodSignature
*sig
;
4035 } SignatureMethodPair
;
4038 signature_method_pair_hash (gconstpointer data
)
4040 SignatureMethodPair
*pair
= (SignatureMethodPair
*)data
;
4042 return mono_signature_hash (pair
->sig
) ^ mono_aligned_addr_hash (pair
->method
);
4046 signature_method_pair_equal (SignatureMethodPair
*pair1
, SignatureMethodPair
*pair2
)
4048 return mono_metadata_signature_equal (pair1
->sig
, pair2
->sig
) && (pair1
->method
== pair2
->method
);
4052 signature_method_pair_matches_method (gpointer key
, gpointer value
, gpointer user_data
)
4054 SignatureMethodPair
*pair
= (SignatureMethodPair
*)key
;
4055 MonoMethod
*method
= (MonoMethod
*)user_data
;
4057 return pair
->method
== method
;
4061 free_signature_method_pair (SignatureMethodPair
*pair
)
4067 * the returned method invokes all methods in a multicast delegate.
4070 mono_marshal_get_delegate_invoke (MonoMethod
*method
, MonoDelegate
*del
)
4072 MonoMethodSignature
*sig
, *static_sig
, *invoke_sig
;
4074 MonoMethodBuilder
*mb
;
4075 MonoMethod
*res
, *newm
;
4077 SignatureMethodPair key
;
4078 SignatureMethodPair
*new_key
;
4079 int local_prev
, local_target
;
4082 MonoMethod
*target_method
= NULL
;
4083 MonoClass
*target_class
= NULL
;
4084 gboolean callvirt
= FALSE
;
4085 gboolean closed_over_null
= FALSE
;
4086 gboolean static_method_with_first_arg_bound
= FALSE
;
4087 MonoGenericContext
*ctx
= NULL
;
4088 MonoGenericContainer
*container
= NULL
;
4089 MonoMethod
*orig_method
= NULL
;
4092 * If the delegate target is null, and the target method is not static, a virtual
4093 * call is made to that method with the first delegate argument as this. This is
4094 * a non-documented .NET feature.
4096 if (del
&& !del
->target
&& del
->method
&& mono_method_signature (del
->method
)->hasthis
) {
4098 target_method
= del
->method
;
4099 if (target_method
->is_inflated
) {
4100 MonoType
*target_type
;
4102 g_assert (method
->signature
->hasthis
);
4103 target_type
= mono_class_inflate_generic_type (method
->signature
->params
[0],
4104 mono_method_get_context (method
));
4105 target_class
= mono_class_from_mono_type (target_type
);
4107 target_class
= del
->method
->klass
;
4111 g_assert (method
&& method
->klass
->parent
== mono_defaults
.multicastdelegate_class
&&
4112 !strcmp (method
->name
, "Invoke"));
4114 invoke_sig
= sig
= mono_signature_no_pinvoke (method
);
4117 closed_over_null
= sig
->param_count
== mono_method_signature (del
->method
)->param_count
;
4119 if (del
&& del
->method
&& mono_method_signature (del
->method
)->param_count
== sig
->param_count
+ 1 && (del
->method
->flags
& METHOD_ATTRIBUTE_STATIC
)) {
4120 invoke_sig
= mono_method_signature (del
->method
);
4121 target_method
= del
->method
;
4122 static_method_with_first_arg_bound
= TRUE
;
4126 * For generic delegates, create a generic wrapper, and returns an instance to help AOT.
4128 if (method
->is_inflated
&& !callvirt
&& !static_method_with_first_arg_bound
) {
4129 orig_method
= method
;
4130 ctx
= &((MonoMethodInflated
*)method
)->context
;
4131 method
= ((MonoMethodInflated
*)method
)->declaring
;
4133 container
= mono_method_get_generic_container (method
);
4135 container
= method
->klass
->generic_container
;
4136 g_assert (container
);
4138 invoke_sig
= sig
= mono_signature_no_pinvoke (method
);
4145 cache
= get_cache (&method
->klass
->image
->delegate_invoke_generic_cache
, mono_aligned_addr_hash
, NULL
);
4146 res
= check_generic_delegate_wrapper_cache (cache
, orig_method
, method
, ctx
);
4149 } else if (callvirt
|| static_method_with_first_arg_bound
) {
4150 GHashTable
**cache_ptr
;
4151 if (static_method_with_first_arg_bound
)
4152 cache_ptr
= &method
->klass
->image
->delegate_bound_static_invoke_cache
;
4154 cache_ptr
= &method
->klass
->image
->delegate_abstract_invoke_cache
;
4156 /* We need to cache the signature+method pair */
4157 mono_marshal_lock ();
4159 *cache_ptr
= g_hash_table_new_full (signature_method_pair_hash
, (GEqualFunc
)signature_method_pair_equal
, (GDestroyNotify
)free_signature_method_pair
, NULL
);
4161 key
.sig
= invoke_sig
;
4162 key
.method
= target_method
;
4163 res
= g_hash_table_lookup (cache
, &key
);
4164 mono_marshal_unlock ();
4168 cache
= get_cache (&method
->klass
->image
->delegate_invoke_cache
,
4169 (GHashFunc
)mono_signature_hash
,
4170 (GCompareFunc
)mono_metadata_signature_equal
);
4171 res
= mono_marshal_find_in_cache (cache
, sig
);
4176 static_sig
= signature_dup (method
->klass
->image
, sig
);
4177 static_sig
->hasthis
= 0;
4178 if (!static_method_with_first_arg_bound
)
4179 invoke_sig
= static_sig
;
4181 name
= mono_signature_to_name (sig
, "invoke");
4183 mb
= mono_mb_new (method
->klass
, name
, MONO_WRAPPER_DELEGATE_INVOKE
);
4185 mb
= mono_mb_new (get_wrapper_target_class (method
->klass
->image
), name
, MONO_WRAPPER_DELEGATE_INVOKE
);
4189 /* allocate local 0 (object) */
4190 local_target
= mono_mb_add_local (mb
, &mono_defaults
.object_class
->byval_arg
);
4191 local_prev
= mono_mb_add_local (mb
, &mono_defaults
.object_class
->byval_arg
);
4193 g_assert (sig
->hasthis
);
4197 * prev.Invoke( args .. );
4198 * return this.<target>( args .. );
4201 /* this wrapper can be used in unmanaged-managed transitions */
4202 emit_thread_interrupt_checkpoint (mb
);
4204 /* get this->prev */
4205 mono_mb_emit_ldarg (mb
, 0);
4206 mono_mb_emit_ldflda (mb
, G_STRUCT_OFFSET (MonoMulticastDelegate
, prev
));
4207 mono_mb_emit_byte (mb
, CEE_LDIND_REF
);
4208 mono_mb_emit_stloc (mb
, local_prev
);
4209 mono_mb_emit_ldloc (mb
, local_prev
);
4211 /* if prev != null */
4212 pos0
= mono_mb_emit_branch (mb
, CEE_BRFALSE
);
4216 mono_mb_emit_byte (mb
, MONO_CUSTOM_PREFIX
);
4217 mono_mb_emit_byte (mb
, CEE_MONO_NOT_TAKEN
);
4219 mono_mb_emit_ldloc (mb
, local_prev
);
4220 for (i
= 0; i
< sig
->param_count
; i
++)
4221 mono_mb_emit_ldarg (mb
, i
+ 1);
4223 mono_mb_emit_op (mb
, CEE_CALLVIRT
, mono_class_inflate_generic_method (method
, &container
->context
));
4225 mono_mb_emit_op (mb
, CEE_CALLVIRT
, method
);
4226 if (sig
->ret
->type
!= MONO_TYPE_VOID
)
4227 mono_mb_emit_byte (mb
, CEE_POP
);
4229 /* continued or prev == null */
4230 mono_mb_patch_branch (mb
, pos0
);
4232 /* get this->target */
4233 mono_mb_emit_ldarg (mb
, 0);
4234 mono_mb_emit_ldflda (mb
, G_STRUCT_OFFSET (MonoDelegate
, target
));
4235 mono_mb_emit_byte (mb
, CEE_LDIND_REF
);
4236 mono_mb_emit_stloc (mb
, local_target
);
4238 /*static methods with bound first arg can have null target and still be bound*/
4239 if (!static_method_with_first_arg_bound
) {
4240 /* if target != null */
4241 mono_mb_emit_ldloc (mb
, local_target
);
4242 pos0
= mono_mb_emit_branch (mb
, CEE_BRFALSE
);
4244 /* then call this->method_ptr nonstatic */
4247 mono_mb_emit_exception_full (mb
, "System", "NotImplementedException", "");
4249 mono_mb_emit_ldloc (mb
, local_target
);
4250 for (i
= 0; i
< sig
->param_count
; ++i
)
4251 mono_mb_emit_ldarg (mb
, i
+ 1);
4252 mono_mb_emit_ldarg (mb
, 0);
4253 mono_mb_emit_ldflda (mb
, G_STRUCT_OFFSET (MonoDelegate
, method_ptr
));
4254 mono_mb_emit_byte (mb
, CEE_LDIND_I
);
4255 mono_mb_emit_op (mb
, CEE_CALLI
, sig
);
4257 mono_mb_emit_byte (mb
, CEE_RET
);
4260 /* else [target == null] call this->method_ptr static */
4261 mono_mb_patch_branch (mb
, pos0
);
4265 if (!closed_over_null
) {
4266 mono_mb_emit_ldarg (mb
, 1);
4267 mono_mb_emit_op (mb
, CEE_CASTCLASS
, target_class
);
4268 for (i
= 1; i
< sig
->param_count
; ++i
)
4269 mono_mb_emit_ldarg (mb
, i
+ 1);
4270 mono_mb_emit_op (mb
, CEE_CALLVIRT
, target_method
);
4272 mono_mb_emit_byte (mb
, CEE_LDNULL
);
4273 for (i
= 0; i
< sig
->param_count
; ++i
)
4274 mono_mb_emit_ldarg (mb
, i
+ 1);
4275 mono_mb_emit_op (mb
, CEE_CALL
, target_method
);
4278 if (static_method_with_first_arg_bound
) {
4279 mono_mb_emit_ldloc (mb
, local_target
);
4280 if (!MONO_TYPE_IS_REFERENCE (invoke_sig
->params
[0]))
4281 mono_mb_emit_op (mb
, CEE_UNBOX_ANY
, mono_class_from_mono_type (invoke_sig
->params
[0]));
4283 for (i
= 0; i
< sig
->param_count
; ++i
)
4284 mono_mb_emit_ldarg (mb
, i
+ 1);
4285 mono_mb_emit_ldarg (mb
, 0);
4286 mono_mb_emit_ldflda (mb
, G_STRUCT_OFFSET (MonoDelegate
, method_ptr
));
4287 mono_mb_emit_byte (mb
, CEE_LDIND_I
);
4288 mono_mb_emit_op (mb
, CEE_CALLI
, invoke_sig
);
4291 mono_mb_emit_byte (mb
, CEE_RET
);
4293 mb
->skip_visibility
= 1;
4294 #endif /* DISABLE_JIT */
4299 def
= mono_mb_create_and_cache (cache
, method
->klass
, mb
, sig
, sig
->param_count
+ 16);
4300 res
= cache_generic_delegate_wrapper (cache
, orig_method
, def
, ctx
);
4301 } else if (static_method_with_first_arg_bound
|| callvirt
) {
4302 // From mono_mb_create_and_cache
4303 newm
= mono_mb_create_method (mb
, sig
, sig
->param_count
+ 16);
4304 /*We perform double checked locking, so must fence before publishing*/
4305 mono_memory_barrier ();
4306 mono_marshal_lock ();
4307 res
= g_hash_table_lookup (cache
, &key
);
4310 new_key
= g_new0 (SignatureMethodPair
, 1);
4312 if (static_method_with_first_arg_bound
)
4313 new_key
->sig
= signature_dup (del
->method
->klass
->image
, key
.sig
);
4314 g_hash_table_insert (cache
, new_key
, res
);
4315 mono_marshal_set_wrapper_info (res
, new_key
);
4316 mono_marshal_unlock ();
4318 mono_marshal_unlock ();
4319 mono_free_method (newm
);
4322 res
= mono_mb_create_and_cache (cache
, sig
, mb
, sig
, sig
->param_count
+ 16);
4330 * signature_dup_add_this:
4332 * Make a copy of @sig, adding an explicit this argument.
4334 static MonoMethodSignature
*
4335 signature_dup_add_this (MonoImage
*image
, MonoMethodSignature
*sig
, MonoClass
*klass
)
4337 MonoMethodSignature
*res
;
4340 res
= mono_metadata_signature_alloc (image
, sig
->param_count
+ 1);
4341 memcpy (res
, sig
, MONO_SIZEOF_METHOD_SIGNATURE
);
4342 res
->param_count
= sig
->param_count
+ 1;
4343 res
->hasthis
= FALSE
;
4344 for (i
= sig
->param_count
- 1; i
>= 0; i
--)
4345 res
->params
[i
+ 1] = sig
->params
[i
];
4346 res
->params
[0] = klass
->valuetype
? &klass
->this_arg
: &klass
->byval_arg
;
4352 MonoMethodSignature
*ctor_sig
;
4353 MonoMethodSignature
*sig
;
4356 /* protected by the marshal lock, contains CtorSigPair pointers */
4357 static GSList
*strsig_list
= NULL
;
4359 static MonoMethodSignature
*
4360 lookup_string_ctor_signature (MonoMethodSignature
*sig
)
4362 MonoMethodSignature
*callsig
;
4366 mono_marshal_lock ();
4368 for (item
= strsig_list
; item
; item
= item
->next
) {
4370 /* mono_metadata_signature_equal () is safe to call with the marshal lock
4371 * because it is lock-free.
4373 if (mono_metadata_signature_equal (sig
, cs
->ctor_sig
)) {
4378 mono_marshal_unlock ();
4382 static MonoMethodSignature
*
4383 add_string_ctor_signature (MonoMethod
*method
)
4385 MonoMethodSignature
*callsig
;
4388 callsig
= signature_dup (method
->klass
->image
, mono_method_signature (method
));
4389 callsig
->ret
= &mono_defaults
.string_class
->byval_arg
;
4390 cs
= g_new (CtorSigPair
, 1);
4392 cs
->ctor_sig
= mono_method_signature (method
);
4394 mono_marshal_lock ();
4395 strsig_list
= g_slist_prepend (strsig_list
, cs
);
4396 mono_marshal_unlock ();
4401 * mono_marshal_get_string_ctor_signature:
4403 * Return the modified signature used by string ctors (they return the newly created
4406 MonoMethodSignature
*
4407 mono_marshal_get_string_ctor_signature (MonoMethod
*method
)
4409 MonoMethodSignature
*sig
= lookup_string_ctor_signature (mono_method_signature (method
));
4411 sig
= add_string_ctor_signature (method
);
4417 get_runtime_invoke_type (MonoType
*t
, gboolean ret
)
4420 /* Can't share this with 'I' as that needs another indirection */
4423 if (MONO_TYPE_IS_REFERENCE (t
))
4424 return &mono_defaults
.object_class
->byval_arg
;
4427 /* The result needs to be boxed */
4433 return &mono_defaults
.sbyte_class
->byval_arg
;
4435 return &mono_defaults
.int16_class
->byval_arg
;
4437 return &mono_defaults
.int32_class
->byval_arg
;
4439 return &mono_defaults
.int64_class
->byval_arg
;
4440 case MONO_TYPE_BOOLEAN
:
4441 return &mono_defaults
.sbyte_class
->byval_arg
;
4442 case MONO_TYPE_CHAR
:
4443 return &mono_defaults
.int16_class
->byval_arg
;
4446 return &mono_defaults
.int_class
->byval_arg
;
4447 case MONO_TYPE_VALUETYPE
:
4448 if (t
->data
.klass
->enumtype
) {
4449 t
= mono_class_enum_basetype (t
->data
.klass
);
4459 * mono_marshal_get_runtime_invoke_sig:
4461 * Return a common signature used for sharing runtime invoke wrappers.
4463 static MonoMethodSignature
*
4464 mono_marshal_get_runtime_invoke_sig (MonoMethodSignature
*sig
)
4466 MonoMethodSignature
*res
= mono_metadata_signature_dup (sig
);
4469 res
->ret
= get_runtime_invoke_type (sig
->ret
, TRUE
);
4470 for (i
= 0; i
< res
->param_count
; ++i
)
4471 res
->params
[i
] = get_runtime_invoke_type (sig
->params
[i
], FALSE
);
4477 runtime_invoke_signature_equal (MonoMethodSignature
*sig1
, MonoMethodSignature
*sig2
)
4479 /* Can't share wrappers which return a vtype since it needs to be boxed */
4480 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
))
4483 return mono_metadata_signature_equal (sig1
, sig2
);
4491 * Emit the call to the wrapper method from a runtime invoke wrapper.
4494 emit_invoke_call (MonoMethodBuilder
*mb
, MonoMethod
*method
,
4495 MonoMethodSignature
*sig
, MonoMethodSignature
*callsig
,
4497 gboolean
virtual, gboolean need_direct_wrapper
)
4499 static MonoString
*string_dummy
= NULL
;
4501 int *tmp_nullable_locals
;
4502 gboolean void_ret
= FALSE
;
4504 /* to make it work with our special string constructors */
4505 if (!string_dummy
) {
4506 MONO_GC_REGISTER_ROOT_SINGLE (string_dummy
);
4507 string_dummy
= mono_string_new_wrapper ("dummy");
4511 g_assert (sig
->hasthis
);
4512 g_assert (method
->flags
& METHOD_ATTRIBUTE_VIRTUAL
);
4516 if (method
->string_ctor
) {
4517 if (mono_gc_is_moving ()) {
4518 mono_mb_emit_ptr (mb
, &string_dummy
);
4519 mono_mb_emit_byte (mb
, CEE_LDIND_REF
);
4521 mono_mb_emit_ptr (mb
, string_dummy
);
4524 mono_mb_emit_ldarg (mb
, 0);
4528 tmp_nullable_locals
= g_new0 (int, sig
->param_count
);
4530 for (i
= 0; i
< sig
->param_count
; i
++) {
4531 MonoType
*t
= sig
->params
[i
];
4534 mono_mb_emit_ldarg (mb
, 1);
4536 mono_mb_emit_icon (mb
, sizeof (gpointer
) * i
);
4537 mono_mb_emit_byte (mb
, CEE_ADD
);
4541 mono_mb_emit_byte (mb
, CEE_LDIND_I
);
4542 /* A Nullable<T> type don't have a boxed form, it's either null or a boxed T.
4543 * So to make this work we unbox it to a local variablee and push a reference to that.
4545 if (t
->type
== MONO_TYPE_GENERICINST
&& mono_class_is_nullable (mono_class_from_mono_type (t
))) {
4546 tmp_nullable_locals
[i
] = mono_mb_add_local (mb
, &mono_class_from_mono_type (t
)->byval_arg
);
4548 mono_mb_emit_op (mb
, CEE_UNBOX_ANY
, mono_class_from_mono_type (t
));
4549 mono_mb_emit_stloc (mb
, tmp_nullable_locals
[i
]);
4550 mono_mb_emit_ldloc_addr (mb
, tmp_nullable_locals
[i
]);
4555 /*FIXME 'this doesn't handle generic enums. Shouldn't we?*/
4556 type
= sig
->params
[i
]->type
;
4560 case MONO_TYPE_BOOLEAN
:
4564 case MONO_TYPE_CHAR
:
4573 mono_mb_emit_byte (mb
, CEE_LDIND_I
);
4574 mono_mb_emit_byte (mb
, mono_type_to_ldind (sig
->params
[i
]));
4576 case MONO_TYPE_STRING
:
4577 case MONO_TYPE_CLASS
:
4578 case MONO_TYPE_ARRAY
:
4580 case MONO_TYPE_SZARRAY
:
4581 case MONO_TYPE_OBJECT
:
4582 mono_mb_emit_byte (mb
, mono_type_to_ldind (sig
->params
[i
]));
4584 case MONO_TYPE_GENERICINST
:
4585 if (!mono_type_generic_inst_is_valuetype (sig
->params
[i
])) {
4586 mono_mb_emit_byte (mb
, mono_type_to_ldind (sig
->params
[i
]));
4591 case MONO_TYPE_VALUETYPE
:
4592 if (type
== MONO_TYPE_VALUETYPE
&& t
->data
.klass
->enumtype
) {
4593 type
= mono_class_enum_basetype (t
->data
.klass
)->type
;
4596 mono_mb_emit_byte (mb
, CEE_LDIND_I
);
4597 if (mono_class_is_nullable (mono_class_from_mono_type (sig
->params
[i
]))) {
4598 /* Need to convert a boxed vtype to an mp to a Nullable struct */
4599 mono_mb_emit_op (mb
, CEE_UNBOX
, mono_class_from_mono_type (sig
->params
[i
]));
4600 mono_mb_emit_op (mb
, CEE_LDOBJ
, mono_class_from_mono_type (sig
->params
[i
]));
4602 mono_mb_emit_op (mb
, CEE_LDOBJ
, mono_class_from_mono_type (sig
->params
[i
]));
4606 g_assert_not_reached ();
4611 mono_mb_emit_op (mb
, CEE_CALLVIRT
, method
);
4612 } else if (need_direct_wrapper
) {
4613 mono_mb_emit_op (mb
, CEE_CALL
, method
);
4615 mono_mb_emit_ldarg (mb
, 3);
4616 mono_mb_emit_calli (mb
, callsig
);
4619 if (sig
->ret
->byref
) {
4621 g_assert_not_reached ();
4624 switch (sig
->ret
->type
) {
4625 case MONO_TYPE_VOID
:
4626 if (!method
->string_ctor
)
4629 case MONO_TYPE_BOOLEAN
:
4630 case MONO_TYPE_CHAR
:
4643 case MONO_TYPE_VALUETYPE
:
4644 case MONO_TYPE_TYPEDBYREF
:
4645 case MONO_TYPE_GENERICINST
:
4646 /* box value types */
4647 mono_mb_emit_op (mb
, CEE_BOX
, mono_class_from_mono_type (sig
->ret
));
4649 case MONO_TYPE_STRING
:
4650 case MONO_TYPE_CLASS
:
4651 case MONO_TYPE_ARRAY
:
4652 case MONO_TYPE_SZARRAY
:
4653 case MONO_TYPE_OBJECT
:
4657 /* The result is an IntPtr */
4658 mono_mb_emit_op (mb
, CEE_BOX
, mono_defaults
.int_class
);
4661 g_assert_not_reached ();
4665 mono_mb_emit_stloc (mb
, loc_res
);
4667 /* Convert back nullable-byref arguments */
4668 for (i
= 0; i
< sig
->param_count
; i
++) {
4669 MonoType
*t
= sig
->params
[i
];
4672 * Box the result and put it back into the array, the caller will have
4673 * to obtain it from there.
4675 if (t
->byref
&& t
->type
== MONO_TYPE_GENERICINST
&& mono_class_is_nullable (mono_class_from_mono_type (t
))) {
4676 mono_mb_emit_ldarg (mb
, 1);
4677 mono_mb_emit_icon (mb
, sizeof (gpointer
) * i
);
4678 mono_mb_emit_byte (mb
, CEE_ADD
);
4680 mono_mb_emit_ldloc (mb
, tmp_nullable_locals
[i
]);
4681 mono_mb_emit_op (mb
, CEE_BOX
, mono_class_from_mono_type (t
));
4683 mono_mb_emit_byte (mb
, CEE_STIND_REF
);
4687 g_free (tmp_nullable_locals
);
4691 emit_runtime_invoke_body (MonoMethodBuilder
*mb
, MonoClass
*target_klass
, MonoMethod
*method
,
4692 MonoMethodSignature
*sig
, MonoMethodSignature
*callsig
,
4693 gboolean
virtual, gboolean need_direct_wrapper
)
4696 MonoExceptionClause
*clause
;
4697 int loc_res
, loc_exc
;
4699 /* The wrapper looks like this:
4705 * } catch (Exception e) {
4713 /* allocate local 0 (object) tmp */
4714 loc_res
= mono_mb_add_local (mb
, &mono_defaults
.object_class
->byval_arg
);
4715 /* allocate local 1 (object) exc */
4716 loc_exc
= mono_mb_add_local (mb
, &mono_defaults
.object_class
->byval_arg
);
4718 /* *exc is assumed to be initialized to NULL by the caller */
4720 mono_mb_emit_byte (mb
, CEE_LDARG_2
);
4721 labels
[0] = mono_mb_emit_branch (mb
, CEE_BRFALSE
);
4726 labels
[1] = mono_mb_get_label (mb
);
4727 emit_thread_force_interrupt_checkpoint (mb
);
4728 emit_invoke_call (mb
, method
, sig
, callsig
, loc_res
, virtual, need_direct_wrapper
);
4730 labels
[2] = mono_mb_emit_branch (mb
, CEE_LEAVE
);
4732 /* Add a try clause around the call */
4733 clause
= mono_image_alloc0 (target_klass
->image
, sizeof (MonoExceptionClause
));
4734 clause
->flags
= MONO_EXCEPTION_CLAUSE_NONE
;
4735 clause
->data
.catch_class
= mono_defaults
.exception_class
;
4736 clause
->try_offset
= labels
[1];
4737 clause
->try_len
= mono_mb_get_label (mb
) - labels
[1];
4739 clause
->handler_offset
= mono_mb_get_label (mb
);
4742 mono_mb_emit_stloc (mb
, loc_exc
);
4743 mono_mb_emit_byte (mb
, CEE_LDARG_2
);
4744 mono_mb_emit_ldloc (mb
, loc_exc
);
4745 mono_mb_emit_byte (mb
, CEE_STIND_REF
);
4747 mono_mb_emit_branch (mb
, CEE_LEAVE
);
4749 clause
->handler_len
= mono_mb_get_pos (mb
) - clause
->handler_offset
;
4751 mono_mb_set_clauses (mb
, 1, clause
);
4753 mono_mb_patch_branch (mb
, labels
[2]);
4754 mono_mb_emit_ldloc (mb
, loc_res
);
4755 mono_mb_emit_byte (mb
, CEE_RET
);
4760 mono_mb_patch_branch (mb
, labels
[0]);
4761 emit_thread_force_interrupt_checkpoint (mb
);
4762 emit_invoke_call (mb
, method
, sig
, callsig
, loc_res
, virtual, need_direct_wrapper
);
4764 mono_mb_emit_ldloc (mb
, 0);
4765 mono_mb_emit_byte (mb
, CEE_RET
);
4770 * generates IL code for the runtime invoke function
4771 * MonoObject *runtime_invoke (MonoObject *this, void **params, MonoObject **exc, void* method)
4773 * we also catch exceptions if exc != null
4774 * If VIRTUAL is TRUE, then METHOD is invoked virtually on THIS. This is useful since
4775 * it means that the compiled code for METHOD does not have to be looked up
4776 * before calling the runtime invoke wrapper. In this case, the wrapper ignores
4777 * its METHOD argument.
4780 mono_marshal_get_runtime_invoke (MonoMethod
*method
, gboolean
virtual)
4782 MonoMethodSignature
*sig
, *csig
, *callsig
;
4783 MonoMethodBuilder
*mb
;
4784 GHashTable
*cache
= NULL
;
4785 MonoClass
*target_klass
;
4786 MonoMethod
*res
= NULL
;
4787 static MonoMethodSignature
*cctor_signature
= NULL
;
4788 static MonoMethodSignature
*finalize_signature
= NULL
;
4790 const char *param_names
[16];
4791 gboolean need_direct_wrapper
= FALSE
;
4796 if (!cctor_signature
) {
4797 cctor_signature
= mono_metadata_signature_alloc (mono_defaults
.corlib
, 0);
4798 cctor_signature
->ret
= &mono_defaults
.void_class
->byval_arg
;
4800 if (!finalize_signature
) {
4801 finalize_signature
= mono_metadata_signature_alloc (mono_defaults
.corlib
, 0);
4802 finalize_signature
->ret
= &mono_defaults
.void_class
->byval_arg
;
4803 finalize_signature
->hasthis
= 1;
4807 need_direct_wrapper
= TRUE
;
4810 * Use a separate cache indexed by methods to speed things up and to avoid the
4811 * boundless mempool growth caused by the signature_dup stuff below.
4814 cache
= get_cache (&method
->klass
->image
->runtime_invoke_vcall_cache
, mono_aligned_addr_hash
, NULL
);
4816 cache
= get_cache (&method
->klass
->image
->runtime_invoke_direct_cache
, mono_aligned_addr_hash
, NULL
);
4817 res
= mono_marshal_find_in_cache (cache
, method
);
4821 if (method
->klass
->rank
&& (method
->iflags
& METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL
) &&
4822 (method
->iflags
& METHOD_IMPL_ATTRIBUTE_NATIVE
)) {
4824 * Array Get/Set/Address methods. The JIT implements them using inline code
4825 * so we need to create an invoke wrapper which calls the method directly.
4827 need_direct_wrapper
= TRUE
;
4830 if (method
->string_ctor
) {
4831 callsig
= lookup_string_ctor_signature (mono_method_signature (method
));
4833 callsig
= add_string_ctor_signature (method
);
4834 /* Can't share this as we push a string as this */
4835 need_direct_wrapper
= TRUE
;
4837 if (method
->klass
->valuetype
&& mono_method_signature (method
)->hasthis
) {
4839 * Valuetype methods receive a managed pointer as the this argument.
4840 * Create a new signature to reflect this.
4842 callsig
= signature_dup_add_this (method
->klass
->image
, mono_method_signature (method
), method
->klass
);
4843 /* Can't share this as it would be shared with static methods taking an IntPtr argument */
4844 need_direct_wrapper
= TRUE
;
4846 if (method
->dynamic
)
4847 callsig
= signature_dup (method
->klass
->image
, mono_method_signature (method
));
4849 callsig
= mono_method_signature (method
);
4853 target_klass
= get_wrapper_target_class (method
->klass
->image
);
4855 /* Try to share wrappers for non-corlib methods with simple signatures */
4856 if (mono_metadata_signature_equal (callsig
, cctor_signature
)) {
4857 callsig
= cctor_signature
;
4858 target_klass
= mono_defaults
.object_class
;
4859 } else if (mono_metadata_signature_equal (callsig
, finalize_signature
)) {
4860 callsig
= finalize_signature
;
4861 target_klass
= mono_defaults
.object_class
;
4864 if (need_direct_wrapper
) {
4865 /* Already searched at the start */
4867 MonoMethodSignature
*tmp_sig
;
4869 callsig
= mono_marshal_get_runtime_invoke_sig (callsig
);
4871 cache
= get_cache (&target_klass
->image
->runtime_invoke_cache
,
4872 (GHashFunc
)mono_signature_hash
,
4873 (GCompareFunc
)runtime_invoke_signature_equal
);
4875 /* from mono_marshal_find_in_cache */
4876 mono_marshal_lock ();
4877 res
= g_hash_table_lookup (cache
, callsig
);
4878 mono_marshal_unlock ();
4885 /* Make a copy of the signature from the image mempool */
4887 callsig
= mono_metadata_signature_dup_full (target_klass
->image
, callsig
);
4891 sig
= mono_method_signature (method
);
4893 csig
= mono_metadata_signature_alloc (target_klass
->image
, 4);
4895 csig
->ret
= &mono_defaults
.object_class
->byval_arg
;
4896 if (method
->klass
->valuetype
&& mono_method_signature (method
)->hasthis
)
4897 csig
->params
[0] = callsig
->params
[0];
4899 csig
->params
[0] = &mono_defaults
.object_class
->byval_arg
;
4900 csig
->params
[1] = &mono_defaults
.int_class
->byval_arg
;
4901 csig
->params
[2] = &mono_defaults
.int_class
->byval_arg
;
4902 csig
->params
[3] = &mono_defaults
.int_class
->byval_arg
;
4905 /* This is called from runtime code so it has to be cdecl */
4906 csig
->call_convention
= MONO_CALL_C
;
4909 name
= mono_signature_to_name (callsig
, virtual ? "runtime_invoke_virtual" : "runtime_invoke");
4910 mb
= mono_mb_new (target_klass
, name
, MONO_WRAPPER_RUNTIME_INVOKE
);
4914 param_names
[0] = "this";
4915 param_names
[1] = "params";
4916 param_names
[2] = "exc";
4917 param_names
[3] = "method";
4918 mono_mb_set_param_names (mb
, param_names
);
4920 emit_runtime_invoke_body (mb
, target_klass
, method
, sig
, callsig
, virtual, need_direct_wrapper
);
4923 if (need_direct_wrapper
) {
4925 mb
->skip_visibility
= 1;
4927 res
= mono_mb_create_and_cache (cache
, method
, mb
, csig
, sig
->param_count
+ 16);
4928 info
= mono_wrapper_info_create (res
, virtual ? WRAPPER_SUBTYPE_RUNTIME_INVOKE_VIRTUAL
: WRAPPER_SUBTYPE_RUNTIME_INVOKE_DIRECT
);
4929 info
->d
.runtime_invoke
.method
= method
;
4930 mono_marshal_set_wrapper_info (res
, info
);
4932 /* taken from mono_mb_create_and_cache */
4933 mono_marshal_lock ();
4934 res
= g_hash_table_lookup (cache
, callsig
);
4935 mono_marshal_unlock ();
4937 /* Somebody may have created it before us */
4940 newm
= mono_mb_create_method (mb
, csig
, sig
->param_count
+ 16);
4942 mono_marshal_lock ();
4943 res
= g_hash_table_lookup (cache
, callsig
);
4946 g_hash_table_insert (cache
, callsig
, res
);
4947 /* Can't insert it into wrapper_hash since the key is a signature */
4948 g_hash_table_insert (method
->klass
->image
->runtime_invoke_direct_cache
, method
, res
);
4949 info
= mono_wrapper_info_create (res
, WRAPPER_SUBTYPE_RUNTIME_INVOKE_NORMAL
);
4950 info
->d
.runtime_invoke
.sig
= callsig
;
4951 mono_marshal_set_wrapper_info (res
, info
);
4953 mono_free_method (newm
);
4955 mono_marshal_unlock ();
4958 /* end mono_mb_create_and_cache */
4967 * mono_marshal_get_runtime_invoke_dynamic:
4969 * Return a method which can be used to invoke managed methods from native code
4971 * The signature of the returned method is given by RuntimeInvokeDynamicFunction:
4972 * void runtime_invoke (void *args, MonoObject **exc, void *compiled_method)
4973 * ARGS should point to an architecture specific structure containing
4974 * the arguments and space for the return value.
4975 * The other arguments are the same as for runtime_invoke (), except that
4976 * ARGS should contain the this argument too.
4977 * This wrapper serves the same purpose as the runtime-invoke wrappers, but there
4978 * is only one copy of it, which is useful in full-aot.
4979 * The wrapper info for the wrapper is a WrapperInfo structure.
4982 mono_marshal_get_runtime_invoke_dynamic (void)
4984 static MonoMethod
*method
;
4985 MonoMethodSignature
*csig
;
4986 MonoExceptionClause
*clause
;
4987 MonoMethodBuilder
*mb
;
4995 csig
= mono_metadata_signature_alloc (mono_defaults
.corlib
, 4);
4997 csig
->ret
= &mono_defaults
.void_class
->byval_arg
;
4998 csig
->params
[0] = &mono_defaults
.int_class
->byval_arg
;
4999 csig
->params
[1] = &mono_defaults
.int_class
->byval_arg
;
5000 csig
->params
[2] = &mono_defaults
.int_class
->byval_arg
;
5001 csig
->params
[3] = &mono_defaults
.int_class
->byval_arg
;
5003 name
= g_strdup ("runtime_invoke_dynamic");
5004 mb
= mono_mb_new (mono_defaults
.object_class
, name
, MONO_WRAPPER_RUNTIME_INVOKE
);
5008 /* allocate local 0 (object) tmp */
5009 mono_mb_add_local (mb
, &mono_defaults
.object_class
->byval_arg
);
5010 /* allocate local 1 (object) exc */
5011 mono_mb_add_local (mb
, &mono_defaults
.object_class
->byval_arg
);
5013 /* cond set *exc to null */
5014 mono_mb_emit_byte (mb
, CEE_LDARG_1
);
5015 mono_mb_emit_byte (mb
, CEE_BRFALSE_S
);
5016 mono_mb_emit_byte (mb
, 3);
5017 mono_mb_emit_byte (mb
, CEE_LDARG_1
);
5018 mono_mb_emit_byte (mb
, CEE_LDNULL
);
5019 mono_mb_emit_byte (mb
, CEE_STIND_REF
);
5021 emit_thread_force_interrupt_checkpoint (mb
);
5023 mono_mb_emit_byte (mb
, CEE_LDARG_0
);
5024 mono_mb_emit_byte (mb
, CEE_LDARG_2
);
5025 mono_mb_emit_byte (mb
, MONO_CUSTOM_PREFIX
);
5026 mono_mb_emit_byte (mb
, CEE_MONO_DYN_CALL
);
5028 pos
= mono_mb_emit_branch (mb
, CEE_LEAVE
);
5030 clause
= mono_image_alloc0 (mono_defaults
.corlib
, sizeof (MonoExceptionClause
));
5031 clause
->flags
= MONO_EXCEPTION_CLAUSE_FILTER
;
5032 clause
->try_len
= mono_mb_get_label (mb
);
5035 clause
->data
.filter_offset
= mono_mb_get_label (mb
);
5037 mono_mb_emit_byte (mb
, CEE_POP
);
5038 mono_mb_emit_byte (mb
, CEE_LDARG_1
);
5039 mono_mb_emit_byte (mb
, CEE_LDC_I4_0
);
5040 mono_mb_emit_byte (mb
, CEE_PREFIX1
);
5041 mono_mb_emit_byte (mb
, CEE_CGT_UN
);
5042 mono_mb_emit_byte (mb
, CEE_PREFIX1
);
5043 mono_mb_emit_byte (mb
, CEE_ENDFILTER
);
5045 clause
->handler_offset
= mono_mb_get_label (mb
);
5048 /* store exception */
5049 mono_mb_emit_stloc (mb
, 1);
5051 mono_mb_emit_byte (mb
, CEE_LDARG_1
);
5052 mono_mb_emit_ldloc (mb
, 1);
5053 mono_mb_emit_byte (mb
, CEE_STIND_REF
);
5055 mono_mb_emit_byte (mb
, CEE_LDNULL
);
5056 mono_mb_emit_stloc (mb
, 0);
5058 /* Check for the abort exception */
5059 mono_mb_emit_ldloc (mb
, 1);
5060 mono_mb_emit_op (mb
, CEE_ISINST
, mono_defaults
.threadabortexception_class
);
5061 posna
= mono_mb_emit_short_branch (mb
, CEE_BRFALSE_S
);
5063 /* Delay the abort exception */
5064 mono_mb_emit_icall (mb
, ves_icall_System_Threading_Thread_ResetAbort
);
5066 mono_mb_patch_short_branch (mb
, posna
);
5067 mono_mb_emit_branch (mb
, CEE_LEAVE
);
5069 clause
->handler_len
= mono_mb_get_pos (mb
) - clause
->handler_offset
;
5071 mono_mb_set_clauses (mb
, 1, clause
);
5074 mono_mb_patch_branch (mb
, pos
);
5075 //mono_mb_emit_ldloc (mb, 0);
5076 mono_mb_emit_byte (mb
, CEE_RET
);
5077 #endif /* DISABLE_JIT */
5079 mono_loader_lock ();
5080 /* double-checked locking */
5082 method
= mono_mb_create_method (mb
, csig
, 16);
5083 info
= mono_wrapper_info_create (method
, WRAPPER_SUBTYPE_RUNTIME_INVOKE_DYNAMIC
);
5084 mono_marshal_set_wrapper_info (method
, info
);
5086 mono_loader_unlock ();
5095 mono_mb_emit_auto_layout_exception (MonoMethodBuilder
*mb
, MonoClass
*klass
)
5097 char *msg
= g_strdup_printf ("The type `%s.%s' layout needs to be Sequential or Explicit",
5098 klass
->name_space
, klass
->name
);
5100 mono_mb_emit_exception_marshal_directive (mb
, msg
);
5104 #ifndef DISABLE_REMOTING
5106 * mono_marshal_get_ldfld_remote_wrapper:
5107 * @klass: The return type
5109 * This method generates a wrapper for calling mono_load_remote_field_new.
5110 * The return type is ignored for now, as mono_load_remote_field_new () always
5111 * returns an object. In the future, to optimize some codepaths, we might
5112 * call a different function that takes a pointer to a valuetype, instead.
5115 mono_marshal_get_ldfld_remote_wrapper (MonoClass
*klass
)
5117 MonoMethodSignature
*sig
, *csig
;
5118 MonoMethodBuilder
*mb
;
5120 static MonoMethod
* cached
= NULL
;
5122 mono_marshal_lock ();
5124 mono_marshal_unlock ();
5127 mono_marshal_unlock ();
5129 mb
= mono_mb_new_no_dup_name (mono_defaults
.object_class
, "__mono_load_remote_field_new_wrapper", MONO_WRAPPER_LDFLD_REMOTE
);
5131 mb
->method
->save_lmf
= 1;
5133 sig
= mono_metadata_signature_alloc (mono_defaults
.corlib
, 3);
5134 sig
->params
[0] = &mono_defaults
.object_class
->byval_arg
;
5135 sig
->params
[1] = &mono_defaults
.int_class
->byval_arg
;
5136 sig
->params
[2] = &mono_defaults
.int_class
->byval_arg
;
5137 sig
->ret
= &mono_defaults
.object_class
->byval_arg
;
5140 mono_mb_emit_ldarg (mb
, 0);
5141 mono_mb_emit_ldarg (mb
, 1);
5142 mono_mb_emit_ldarg (mb
, 2);
5144 csig
= mono_metadata_signature_alloc (mono_defaults
.corlib
, 3);
5145 csig
->params
[0] = &mono_defaults
.object_class
->byval_arg
;
5146 csig
->params
[1] = &mono_defaults
.int_class
->byval_arg
;
5147 csig
->params
[2] = &mono_defaults
.int_class
->byval_arg
;
5148 csig
->ret
= &mono_defaults
.object_class
->byval_arg
;
5151 mono_mb_emit_native_call (mb
, csig
, mono_load_remote_field_new
);
5152 emit_thread_interrupt_checkpoint (mb
);
5154 mono_mb_emit_byte (mb
, CEE_RET
);
5157 mono_marshal_lock ();
5159 mono_marshal_unlock ();
5162 newm
= mono_mb_create_method (mb
, sig
, 4);
5163 mono_marshal_lock ();
5168 mono_marshal_unlock ();
5170 mono_marshal_unlock ();
5171 mono_free_method (newm
);
5180 * mono_marshal_get_ldfld_wrapper:
5181 * @type: the type of the field
5183 * This method generates a function which can be use to load a field with type
5184 * @type from an object. The generated function has the following signature:
5185 * <@type> ldfld_wrapper (MonoObject *this, MonoClass *class, MonoClassField *field, int offset)
5188 mono_marshal_get_ldfld_wrapper (MonoType
*type
)
5190 MonoMethodSignature
*sig
;
5191 MonoMethodBuilder
*mb
;
5196 int t
, pos0
, pos1
= 0;
5198 type
= mono_type_get_underlying_type (type
);
5203 if (type
->type
== MONO_TYPE_SZARRAY
) {
5204 klass
= mono_defaults
.array_class
;
5205 } else if (type
->type
== MONO_TYPE_VALUETYPE
) {
5206 klass
= type
->data
.klass
;
5207 } else if (t
== MONO_TYPE_OBJECT
|| t
== MONO_TYPE_CLASS
|| t
== MONO_TYPE_STRING
) {
5208 klass
= mono_defaults
.object_class
;
5209 } else if (t
== MONO_TYPE_PTR
|| t
== MONO_TYPE_FNPTR
) {
5210 klass
= mono_defaults
.int_class
;
5211 } else if (t
== MONO_TYPE_GENERICINST
) {
5212 if (mono_type_generic_inst_is_valuetype (type
))
5213 klass
= mono_class_from_mono_type (type
);
5215 klass
= mono_defaults
.object_class
;
5217 klass
= mono_class_from_mono_type (type
);
5220 klass
= mono_defaults
.int_class
;
5223 cache
= get_cache (&klass
->image
->ldfld_wrapper_cache
, mono_aligned_addr_hash
, NULL
);
5224 if ((res
= mono_marshal_find_in_cache (cache
, klass
)))
5227 /* we add the %p pointer value of klass because class names are not unique */
5228 name
= g_strdup_printf ("__ldfld_wrapper_%p_%s.%s", klass
, klass
->name_space
, klass
->name
);
5229 mb
= mono_mb_new (mono_defaults
.object_class
, name
, MONO_WRAPPER_LDFLD
);
5232 sig
= mono_metadata_signature_alloc (mono_defaults
.corlib
, 4);
5233 sig
->params
[0] = &mono_defaults
.object_class
->byval_arg
;
5234 sig
->params
[1] = &mono_defaults
.int_class
->byval_arg
;
5235 sig
->params
[2] = &mono_defaults
.int_class
->byval_arg
;
5236 sig
->params
[3] = &mono_defaults
.int_class
->byval_arg
;
5237 sig
->ret
= &klass
->byval_arg
;
5240 mono_mb_emit_ldarg (mb
, 0);
5241 pos0
= mono_mb_emit_proxy_check (mb
, CEE_BNE_UN
);
5243 mono_mb_emit_ldarg (mb
, 0);
5244 mono_mb_emit_ldarg (mb
, 1);
5245 mono_mb_emit_ldarg (mb
, 2);
5247 mono_mb_emit_managed_call (mb
, mono_marshal_get_ldfld_remote_wrapper (klass
), NULL
);
5250 csig = mono_metadata_signature_alloc (mono_defaults.corlib, 3);
5251 csig->params [0] = &mono_defaults.object_class->byval_arg;
5252 csig->params [1] = &mono_defaults.int_class->byval_arg;
5253 csig->params [2] = &mono_defaults.int_class->byval_arg;
5254 csig->ret = &klass->this_arg;
5257 mono_mb_emit_native_call (mb, csig, mono_load_remote_field_new);
5258 emit_thread_interrupt_checkpoint (mb);
5261 if (klass
->valuetype
) {
5262 mono_mb_emit_op (mb
, CEE_UNBOX
, klass
);
5263 pos1
= mono_mb_emit_branch (mb
, CEE_BR
);
5265 mono_mb_emit_byte (mb
, CEE_RET
);
5269 mono_mb_patch_branch (mb
, pos0
);
5271 mono_mb_emit_ldarg (mb
, 0);
5272 mono_mb_emit_byte (mb
, MONO_CUSTOM_PREFIX
);
5273 mono_mb_emit_byte (mb
, CEE_MONO_OBJADDR
);
5274 mono_mb_emit_ldarg (mb
, 3);
5275 mono_mb_emit_byte (mb
, CEE_ADD
);
5277 if (klass
->valuetype
)
5278 mono_mb_patch_branch (mb
, pos1
);
5283 case MONO_TYPE_BOOLEAN
:
5284 case MONO_TYPE_CHAR
:
5293 case MONO_TYPE_ARRAY
:
5294 case MONO_TYPE_SZARRAY
:
5295 case MONO_TYPE_OBJECT
:
5296 case MONO_TYPE_CLASS
:
5297 case MONO_TYPE_STRING
:
5301 case MONO_TYPE_FNPTR
:
5302 mono_mb_emit_byte (mb
, mono_type_to_ldind (type
));
5304 case MONO_TYPE_VALUETYPE
:
5305 g_assert (!klass
->enumtype
);
5306 mono_mb_emit_op (mb
, CEE_LDOBJ
, klass
);
5308 case MONO_TYPE_GENERICINST
:
5309 if (mono_type_generic_inst_is_valuetype (type
)) {
5310 mono_mb_emit_op (mb
, CEE_LDOBJ
, klass
);
5312 mono_mb_emit_byte (mb
, CEE_LDIND_REF
);
5316 g_warning ("type %x not implemented", type
->type
);
5317 g_assert_not_reached ();
5320 mono_mb_emit_byte (mb
, CEE_RET
);
5321 #endif /* DISABLE_JIT */
5323 res
= mono_mb_create_and_cache (cache
, klass
,
5324 mb
, sig
, sig
->param_count
+ 16);
5331 * mono_marshal_get_ldflda_wrapper:
5332 * @type: the type of the field
5334 * This method generates a function which can be used to load a field address
5335 * from an object. The generated function has the following signature:
5336 * gpointer ldflda_wrapper (MonoObject *this, MonoClass *class, MonoClassField *field, int offset);
5339 mono_marshal_get_ldflda_wrapper (MonoType
*type
)
5341 MonoMethodSignature
*sig
;
5342 MonoMethodBuilder
*mb
;
5347 int t
, pos0
, pos1
, pos2
, pos3
;
5349 type
= mono_type_get_underlying_type (type
);
5353 if (type
->type
== MONO_TYPE_SZARRAY
) {
5354 klass
= mono_defaults
.array_class
;
5355 } else if (type
->type
== MONO_TYPE_VALUETYPE
) {
5356 klass
= type
->data
.klass
;
5357 } else if (t
== MONO_TYPE_OBJECT
|| t
== MONO_TYPE_CLASS
|| t
== MONO_TYPE_STRING
||
5358 t
== MONO_TYPE_CLASS
) {
5359 klass
= mono_defaults
.object_class
;
5360 } else if (t
== MONO_TYPE_PTR
|| t
== MONO_TYPE_FNPTR
) {
5361 klass
= mono_defaults
.int_class
;
5362 } else if (t
== MONO_TYPE_GENERICINST
) {
5363 if (mono_type_generic_inst_is_valuetype (type
))
5364 klass
= mono_class_from_mono_type (type
);
5366 klass
= mono_defaults
.object_class
;
5368 klass
= mono_class_from_mono_type (type
);
5371 klass
= mono_defaults
.int_class
;
5374 cache
= get_cache (&klass
->image
->ldflda_wrapper_cache
, mono_aligned_addr_hash
, NULL
);
5375 if ((res
= mono_marshal_find_in_cache (cache
, klass
)))
5378 /* we add the %p pointer value of klass because class names are not unique */
5379 name
= g_strdup_printf ("__ldflda_wrapper_%p_%s.%s", klass
, klass
->name_space
, klass
->name
);
5380 mb
= mono_mb_new (mono_defaults
.object_class
, name
, MONO_WRAPPER_LDFLDA
);
5383 sig
= mono_metadata_signature_alloc (mono_defaults
.corlib
, 4);
5384 sig
->params
[0] = &mono_defaults
.object_class
->byval_arg
;
5385 sig
->params
[1] = &mono_defaults
.int_class
->byval_arg
;
5386 sig
->params
[2] = &mono_defaults
.int_class
->byval_arg
;
5387 sig
->params
[3] = &mono_defaults
.int_class
->byval_arg
;
5388 sig
->ret
= &mono_defaults
.int_class
->byval_arg
;
5391 /* if typeof (this) != transparent_proxy goto pos0 */
5392 mono_mb_emit_ldarg (mb
, 0);
5393 pos0
= mono_mb_emit_proxy_check (mb
, CEE_BNE_UN
);
5395 /* if same_appdomain goto pos1 */
5396 mono_mb_emit_ldarg (mb
, 0);
5397 pos1
= mono_mb_emit_xdomain_check (mb
, CEE_BEQ
);
5399 mono_mb_emit_exception_full (mb
, "System", "InvalidOperationException", "Attempt to load field address from object in another appdomain.");
5401 /* same app domain */
5402 mono_mb_patch_branch (mb
, pos1
);
5404 /* if typeof (this) != contextbound goto pos2 */
5405 mono_mb_emit_ldarg (mb
, 0);
5406 pos2
= mono_mb_emit_contextbound_check (mb
, CEE_BEQ
);
5408 /* if this->rp->context == mono_context_get goto pos3 */
5409 mono_mb_emit_ldarg (mb
, 0);
5410 mono_mb_emit_ldflda (mb
, G_STRUCT_OFFSET (MonoTransparentProxy
, rp
));
5411 mono_mb_emit_byte (mb
, CEE_LDIND_REF
);
5412 mono_mb_emit_ldflda (mb
, G_STRUCT_OFFSET (MonoRealProxy
, context
));
5413 mono_mb_emit_byte (mb
, CEE_LDIND_REF
);
5414 mono_mb_emit_icall (mb
, mono_context_get
);
5415 pos3
= mono_mb_emit_branch (mb
, CEE_BEQ
);
5417 mono_mb_emit_exception_full (mb
, "System", "InvalidOperationException", "Attempt to load field address from object in another context.");
5419 mono_mb_patch_branch (mb
, pos2
);
5420 mono_mb_patch_branch (mb
, pos3
);
5422 /* return the address of the field from this->rp->unwrapped_server */
5423 mono_mb_emit_ldarg (mb
, 0);
5424 mono_mb_emit_ldflda (mb
, G_STRUCT_OFFSET (MonoTransparentProxy
, rp
));
5425 mono_mb_emit_byte (mb
, CEE_LDIND_REF
);
5426 mono_mb_emit_ldflda (mb
, G_STRUCT_OFFSET (MonoRealProxy
, unwrapped_server
));
5427 mono_mb_emit_byte (mb
, CEE_LDIND_REF
);
5428 mono_mb_emit_byte (mb
, MONO_CUSTOM_PREFIX
);
5429 mono_mb_emit_byte (mb
, CEE_MONO_OBJADDR
);
5430 mono_mb_emit_ldarg (mb
, 3);
5431 mono_mb_emit_byte (mb
, CEE_ADD
);
5432 mono_mb_emit_byte (mb
, CEE_RET
);
5434 /* not a proxy: return the address of the field directly */
5435 mono_mb_patch_branch (mb
, pos0
);
5437 mono_mb_emit_ldarg (mb
, 0);
5438 mono_mb_emit_byte (mb
, MONO_CUSTOM_PREFIX
);
5439 mono_mb_emit_byte (mb
, CEE_MONO_OBJADDR
);
5440 mono_mb_emit_ldarg (mb
, 3);
5441 mono_mb_emit_byte (mb
, CEE_ADD
);
5443 mono_mb_emit_byte (mb
, CEE_RET
);
5446 res
= mono_mb_create_and_cache (cache
, klass
,
5447 mb
, sig
, sig
->param_count
+ 16);
5454 * mono_marshal_get_stfld_remote_wrapper:
5455 * klass: The type of the field
5457 * This function generates a wrapper for calling mono_store_remote_field_new
5458 * with the appropriate signature.
5459 * Similarly to mono_marshal_get_ldfld_remote_wrapper () this doesn't depend on the
5460 * klass argument anymore.
5463 mono_marshal_get_stfld_remote_wrapper (MonoClass
*klass
)
5465 MonoMethodSignature
*sig
, *csig
;
5466 MonoMethodBuilder
*mb
;
5468 static MonoMethod
*cached
= NULL
;
5470 mono_marshal_lock ();
5472 mono_marshal_unlock ();
5475 mono_marshal_unlock ();
5477 mb
= mono_mb_new_no_dup_name (mono_defaults
.object_class
, "__mono_store_remote_field_new_wrapper", MONO_WRAPPER_STFLD_REMOTE
);
5479 mb
->method
->save_lmf
= 1;
5481 sig
= mono_metadata_signature_alloc (mono_defaults
.corlib
, 4);
5482 sig
->params
[0] = &mono_defaults
.object_class
->byval_arg
;
5483 sig
->params
[1] = &mono_defaults
.int_class
->byval_arg
;
5484 sig
->params
[2] = &mono_defaults
.int_class
->byval_arg
;
5485 sig
->params
[3] = &mono_defaults
.object_class
->byval_arg
;
5486 sig
->ret
= &mono_defaults
.void_class
->byval_arg
;
5489 mono_mb_emit_ldarg (mb
, 0);
5490 mono_mb_emit_ldarg (mb
, 1);
5491 mono_mb_emit_ldarg (mb
, 2);
5492 mono_mb_emit_ldarg (mb
, 3);
5494 csig
= mono_metadata_signature_alloc (mono_defaults
.corlib
, 4);
5495 csig
->params
[0] = &mono_defaults
.object_class
->byval_arg
;
5496 csig
->params
[1] = &mono_defaults
.int_class
->byval_arg
;
5497 csig
->params
[2] = &mono_defaults
.int_class
->byval_arg
;
5498 csig
->params
[3] = &mono_defaults
.object_class
->byval_arg
;
5499 csig
->ret
= &mono_defaults
.void_class
->byval_arg
;
5502 mono_mb_emit_native_call (mb
, csig
, mono_store_remote_field_new
);
5503 emit_thread_interrupt_checkpoint (mb
);
5505 mono_mb_emit_byte (mb
, CEE_RET
);
5508 mono_marshal_lock ();
5510 mono_marshal_unlock ();
5513 newm
= mono_mb_create_method (mb
, sig
, 6);
5514 mono_marshal_lock ();
5519 mono_marshal_unlock ();
5521 mono_marshal_unlock ();
5522 mono_free_method (newm
);
5531 * mono_marshal_get_stfld_wrapper:
5532 * @type: the type of the field
5534 * This method generates a function which can be use to store a field with type
5535 * @type. The generated function has the following signature:
5536 * void stfld_wrapper (MonoObject *this, MonoClass *class, MonoClassField *field, int offset, <@type> val)
5539 mono_marshal_get_stfld_wrapper (MonoType
*type
)
5541 MonoMethodSignature
*sig
;
5542 MonoMethodBuilder
*mb
;
5549 type
= mono_type_get_underlying_type (type
);
5553 if (type
->type
== MONO_TYPE_SZARRAY
) {
5554 klass
= mono_defaults
.array_class
;
5555 } else if (type
->type
== MONO_TYPE_VALUETYPE
) {
5556 klass
= type
->data
.klass
;
5557 } else if (t
== MONO_TYPE_OBJECT
|| t
== MONO_TYPE_CLASS
|| t
== MONO_TYPE_STRING
) {
5558 klass
= mono_defaults
.object_class
;
5559 } else if (t
== MONO_TYPE_PTR
|| t
== MONO_TYPE_FNPTR
) {
5560 klass
= mono_defaults
.int_class
;
5561 } else if (t
== MONO_TYPE_GENERICINST
) {
5562 if (mono_type_generic_inst_is_valuetype (type
))
5563 klass
= mono_class_from_mono_type (type
);
5565 klass
= mono_defaults
.object_class
;
5567 klass
= mono_class_from_mono_type (type
);
5570 klass
= mono_defaults
.int_class
;
5573 cache
= get_cache (&klass
->image
->stfld_wrapper_cache
, mono_aligned_addr_hash
, NULL
);
5574 if ((res
= mono_marshal_find_in_cache (cache
, klass
)))
5577 /* we add the %p pointer value of klass because class names are not unique */
5578 name
= g_strdup_printf ("__stfld_wrapper_%p_%s.%s", klass
, klass
->name_space
, klass
->name
);
5579 mb
= mono_mb_new (mono_defaults
.object_class
, name
, MONO_WRAPPER_STFLD
);
5582 sig
= mono_metadata_signature_alloc (mono_defaults
.corlib
, 5);
5583 sig
->params
[0] = &mono_defaults
.object_class
->byval_arg
;
5584 sig
->params
[1] = &mono_defaults
.int_class
->byval_arg
;
5585 sig
->params
[2] = &mono_defaults
.int_class
->byval_arg
;
5586 sig
->params
[3] = &mono_defaults
.int_class
->byval_arg
;
5587 sig
->params
[4] = &klass
->byval_arg
;
5588 sig
->ret
= &mono_defaults
.void_class
->byval_arg
;
5591 mono_mb_emit_ldarg (mb
, 0);
5592 pos
= mono_mb_emit_proxy_check (mb
, CEE_BNE_UN
);
5594 mono_mb_emit_ldarg (mb
, 0);
5595 mono_mb_emit_ldarg (mb
, 1);
5596 mono_mb_emit_ldarg (mb
, 2);
5597 mono_mb_emit_ldarg (mb
, 4);
5598 if (klass
->valuetype
)
5599 mono_mb_emit_op (mb
, CEE_BOX
, klass
);
5601 mono_mb_emit_managed_call (mb
, mono_marshal_get_stfld_remote_wrapper (klass
), NULL
);
5603 mono_mb_emit_byte (mb
, CEE_RET
);
5605 mono_mb_patch_branch (mb
, pos
);
5607 mono_mb_emit_ldarg (mb
, 0);
5608 mono_mb_emit_byte (mb
, MONO_CUSTOM_PREFIX
);
5609 mono_mb_emit_byte (mb
, CEE_MONO_OBJADDR
);
5610 mono_mb_emit_ldarg (mb
, 3);
5611 mono_mb_emit_byte (mb
, CEE_ADD
);
5612 mono_mb_emit_ldarg (mb
, 4);
5617 case MONO_TYPE_BOOLEAN
:
5618 case MONO_TYPE_CHAR
:
5627 case MONO_TYPE_ARRAY
:
5628 case MONO_TYPE_SZARRAY
:
5629 case MONO_TYPE_OBJECT
:
5630 case MONO_TYPE_CLASS
:
5631 case MONO_TYPE_STRING
:
5635 case MONO_TYPE_FNPTR
:
5636 mono_mb_emit_byte (mb
, mono_type_to_stind (type
));
5638 case MONO_TYPE_VALUETYPE
:
5639 g_assert (!klass
->enumtype
);
5640 mono_mb_emit_op (mb
, CEE_STOBJ
, klass
);
5642 case MONO_TYPE_GENERICINST
:
5643 mono_mb_emit_op (mb
, CEE_STOBJ
, klass
);
5646 g_warning ("type %x not implemented", type
->type
);
5647 g_assert_not_reached ();
5650 mono_mb_emit_byte (mb
, CEE_RET
);
5653 res
= mono_mb_create_and_cache (cache
, klass
,
5654 mb
, sig
, sig
->param_count
+ 16);
5659 #endif /* DISABLE_REMOTING */
5662 * generates IL code for the icall wrapper (the generated method
5663 * calls the unmanaged code in func)
5664 * The wrapper info for the wrapper is a WrapperInfo structure.
5667 mono_marshal_get_icall_wrapper (MonoMethodSignature
*sig
, const char *name
, gconstpointer func
, gboolean check_exceptions
)
5669 MonoMethodSignature
*csig
, *csig2
;
5670 MonoMethodBuilder
*mb
;
5675 g_assert (sig
->pinvoke
);
5677 mb
= mono_mb_new (mono_defaults
.object_class
, name
, MONO_WRAPPER_MANAGED_TO_NATIVE
);
5679 mb
->method
->save_lmf
= 1;
5681 /* Add an explicit this argument */
5683 csig2
= signature_dup_add_this (mono_defaults
.corlib
, sig
, mono_defaults
.object_class
);
5685 csig2
= signature_dup (mono_defaults
.corlib
, sig
);
5689 mono_mb_emit_byte (mb
, CEE_LDARG_0
);
5691 for (i
= 0; i
< sig
->param_count
; i
++)
5692 mono_mb_emit_ldarg (mb
, i
+ sig
->hasthis
);
5694 mono_mb_emit_byte (mb
, MONO_CUSTOM_PREFIX
);
5695 mono_mb_emit_op (mb
, CEE_MONO_JIT_ICALL_ADDR
, (gpointer
)func
);
5696 mono_mb_emit_calli (mb
, csig2
);
5697 if (check_exceptions
)
5698 emit_thread_interrupt_checkpoint (mb
);
5699 mono_mb_emit_byte (mb
, CEE_RET
);
5702 csig
= signature_dup (mono_defaults
.corlib
, sig
);
5704 if (csig
->call_convention
== MONO_CALL_VARARG
)
5705 csig
->call_convention
= 0;
5707 res
= mono_mb_create_method (mb
, csig
, csig
->param_count
+ 16);
5710 info
= mono_wrapper_info_create (res
, WRAPPER_SUBTYPE_ICALL_WRAPPER
);
5711 info
->d
.icall
.func
= (gpointer
)func
;
5712 mono_marshal_set_wrapper_info (res
, info
);
5718 emit_marshal_custom (EmitMarshalContext
*m
, int argnum
, MonoType
*t
,
5719 MonoMarshalSpec
*spec
,
5720 int conv_arg
, MonoType
**conv_arg_type
,
5721 MarshalAction action
)
5724 if (action
== MARSHAL_ACTION_CONV_IN
&& t
->type
== MONO_TYPE_VALUETYPE
)
5725 *conv_arg_type
= &mono_defaults
.int_class
->byval_arg
;
5730 static MonoClass
*ICustomMarshaler
= NULL
;
5731 static MonoMethod
*cleanup_native
, *cleanup_managed
;
5732 static MonoMethod
*marshal_managed_to_native
, *marshal_native_to_managed
;
5733 MonoMethod
*get_instance
= NULL
;
5734 MonoMethodBuilder
*mb
= m
->mb
;
5735 char *exception_msg
= NULL
;
5739 if (!ICustomMarshaler
) {
5740 ICustomMarshaler
= mono_class_from_name (mono_defaults
.corlib
, "System.Runtime.InteropServices", "ICustomMarshaler");
5741 if (!ICustomMarshaler
) {
5742 exception_msg
= g_strdup ("Current profile doesn't support ICustomMarshaler");
5743 goto handle_exception
;
5746 cleanup_native
= mono_class_get_method_from_name (ICustomMarshaler
, "CleanUpNativeData", 1);
5747 g_assert (cleanup_native
);
5748 cleanup_managed
= mono_class_get_method_from_name (ICustomMarshaler
, "CleanUpManagedData", 1);
5749 g_assert (cleanup_managed
);
5750 marshal_managed_to_native
= mono_class_get_method_from_name (ICustomMarshaler
, "MarshalManagedToNative", 1);
5751 g_assert (marshal_managed_to_native
);
5752 marshal_native_to_managed
= mono_class_get_method_from_name (ICustomMarshaler
, "MarshalNativeToManaged", 1);
5753 g_assert (marshal_native_to_managed
);
5756 mtype
= mono_reflection_type_from_name (spec
->data
.custom_data
.custom_name
, m
->image
);
5757 g_assert (mtype
!= NULL
);
5758 mklass
= mono_class_from_mono_type (mtype
);
5759 g_assert (mklass
!= NULL
);
5761 if (!mono_class_is_assignable_from (ICustomMarshaler
, mklass
))
5762 exception_msg
= g_strdup_printf ("Custom marshaler '%s' does not implement the ICustomMarshaler interface.", mklass
->name
);
5764 get_instance
= mono_class_get_method_from_name_flags (mklass
, "GetInstance", 1, METHOD_ATTRIBUTE_STATIC
);
5766 MonoMethodSignature
*get_sig
= mono_method_signature (get_instance
);
5767 if ((get_sig
->ret
->type
!= MONO_TYPE_CLASS
) ||
5768 (mono_class_from_mono_type (get_sig
->ret
) != ICustomMarshaler
) ||
5769 (get_sig
->params
[0]->type
!= MONO_TYPE_STRING
))
5770 get_instance
= NULL
;
5774 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
);
5777 /* Throw exception and emit compensation code if neccesary */
5778 if (exception_msg
) {
5780 case MARSHAL_ACTION_CONV_IN
:
5781 case MARSHAL_ACTION_CONV_RESULT
:
5782 case MARSHAL_ACTION_MANAGED_CONV_RESULT
:
5783 if ((action
== MARSHAL_ACTION_CONV_RESULT
) || (action
== MARSHAL_ACTION_MANAGED_CONV_RESULT
))
5784 mono_mb_emit_byte (mb
, CEE_POP
);
5786 mono_mb_emit_exception_full (mb
, "System", "ApplicationException", exception_msg
);
5787 g_free (exception_msg
);
5790 case MARSHAL_ACTION_PUSH
:
5791 mono_mb_emit_byte (mb
, CEE_LDNULL
);
5799 /* FIXME: MS.NET seems to create one instance for each klass + cookie pair */
5800 /* FIXME: MS.NET throws an exception if GetInstance returns null */
5803 case MARSHAL_ACTION_CONV_IN
:
5805 case MONO_TYPE_CLASS
:
5806 case MONO_TYPE_OBJECT
:
5807 case MONO_TYPE_STRING
:
5808 case MONO_TYPE_ARRAY
:
5809 case MONO_TYPE_SZARRAY
:
5810 case MONO_TYPE_VALUETYPE
:
5814 g_warning ("custom marshalling of type %x is currently not supported", t
->type
);
5815 g_assert_not_reached ();
5819 conv_arg
= mono_mb_add_local (mb
, &mono_defaults
.int_class
->byval_arg
);
5821 mono_mb_emit_byte (mb
, CEE_LDNULL
);
5822 mono_mb_emit_stloc (mb
, conv_arg
);
5824 if (t
->byref
&& (t
->attrs
& PARAM_ATTRIBUTE_OUT
))
5827 /* Minic MS.NET behavior */
5828 if (!t
->byref
&& (t
->attrs
& PARAM_ATTRIBUTE_OUT
) && !(t
->attrs
& PARAM_ATTRIBUTE_IN
))
5831 /* Check for null */
5832 mono_mb_emit_ldarg (mb
, argnum
);
5834 mono_mb_emit_byte (mb
, CEE_LDIND_I
);
5835 pos2
= mono_mb_emit_branch (mb
, CEE_BRFALSE
);
5837 mono_mb_emit_ldstr (mb
, g_strdup (spec
->data
.custom_data
.cookie
));
5839 mono_mb_emit_op (mb
, CEE_CALL
, get_instance
);
5841 mono_mb_emit_ldarg (mb
, argnum
);
5843 mono_mb_emit_byte (mb
, CEE_LDIND_REF
);
5845 if (t
->type
== MONO_TYPE_VALUETYPE
) {
5847 * Since we can't determine the type of the argument, we
5848 * will assume the unmanaged function takes a pointer.
5850 *conv_arg_type
= &mono_defaults
.int_class
->byval_arg
;
5852 mono_mb_emit_op (mb
, CEE_BOX
, mono_class_from_mono_type (t
));
5855 mono_mb_emit_op (mb
, CEE_CALLVIRT
, marshal_managed_to_native
);
5856 mono_mb_emit_stloc (mb
, conv_arg
);
5858 mono_mb_patch_branch (mb
, pos2
);
5861 case MARSHAL_ACTION_CONV_OUT
:
5862 /* Check for null */
5863 mono_mb_emit_ldloc (mb
, conv_arg
);
5864 pos2
= mono_mb_emit_branch (mb
, CEE_BRFALSE
);
5867 mono_mb_emit_ldarg (mb
, argnum
);
5869 mono_mb_emit_ldstr (mb
, g_strdup (spec
->data
.custom_data
.cookie
));
5871 mono_mb_emit_op (mb
, CEE_CALL
, get_instance
);
5873 mono_mb_emit_ldloc (mb
, conv_arg
);
5874 mono_mb_emit_op (mb
, CEE_CALLVIRT
, marshal_native_to_managed
);
5875 mono_mb_emit_byte (mb
, CEE_STIND_REF
);
5876 } else if (t
->attrs
& PARAM_ATTRIBUTE_OUT
) {
5877 mono_mb_emit_ldstr (mb
, g_strdup (spec
->data
.custom_data
.cookie
));
5879 mono_mb_emit_op (mb
, CEE_CALL
, get_instance
);
5881 mono_mb_emit_ldloc (mb
, conv_arg
);
5882 mono_mb_emit_op (mb
, CEE_CALLVIRT
, marshal_native_to_managed
);
5884 /* We have nowhere to store the result */
5885 mono_mb_emit_byte (mb
, CEE_POP
);
5888 mono_mb_emit_ldstr (mb
, g_strdup (spec
->data
.custom_data
.cookie
));
5890 mono_mb_emit_op (mb
, CEE_CALL
, get_instance
);
5892 mono_mb_emit_ldloc (mb
, conv_arg
);
5894 mono_mb_emit_op (mb
, CEE_CALLVIRT
, cleanup_native
);
5896 mono_mb_patch_branch (mb
, pos2
);
5899 case MARSHAL_ACTION_PUSH
:
5901 mono_mb_emit_ldloc_addr (mb
, conv_arg
);
5903 mono_mb_emit_ldloc (mb
, conv_arg
);
5906 case MARSHAL_ACTION_CONV_RESULT
:
5907 loc1
= mono_mb_add_local (mb
, &mono_defaults
.int_class
->byval_arg
);
5909 mono_mb_emit_stloc (mb
, 3);
5911 mono_mb_emit_ldloc (mb
, 3);
5912 mono_mb_emit_stloc (mb
, loc1
);
5914 /* Check for null */
5915 mono_mb_emit_ldloc (mb
, 3);
5916 pos2
= mono_mb_emit_branch (mb
, CEE_BRFALSE
);
5918 mono_mb_emit_ldstr (mb
, g_strdup (spec
->data
.custom_data
.cookie
));
5920 mono_mb_emit_op (mb
, CEE_CALL
, get_instance
);
5921 mono_mb_emit_byte (mb
, CEE_DUP
);
5923 mono_mb_emit_ldloc (mb
, 3);
5924 mono_mb_emit_op (mb
, CEE_CALLVIRT
, marshal_native_to_managed
);
5925 mono_mb_emit_stloc (mb
, 3);
5927 mono_mb_emit_ldloc (mb
, loc1
);
5928 mono_mb_emit_op (mb
, CEE_CALLVIRT
, cleanup_native
);
5930 mono_mb_patch_branch (mb
, pos2
);
5933 case MARSHAL_ACTION_MANAGED_CONV_IN
:
5934 conv_arg
= mono_mb_add_local (mb
, &mono_defaults
.object_class
->byval_arg
);
5936 mono_mb_emit_byte (mb
, CEE_LDNULL
);
5937 mono_mb_emit_stloc (mb
, conv_arg
);
5939 if (t
->byref
&& t
->attrs
& PARAM_ATTRIBUTE_OUT
)
5942 /* Check for null */
5943 mono_mb_emit_ldarg (mb
, argnum
);
5945 mono_mb_emit_byte (mb
, CEE_LDIND_I
);
5946 pos2
= mono_mb_emit_branch (mb
, CEE_BRFALSE
);
5948 mono_mb_emit_ldstr (mb
, g_strdup (spec
->data
.custom_data
.cookie
));
5949 mono_mb_emit_op (mb
, CEE_CALL
, get_instance
);
5951 mono_mb_emit_ldarg (mb
, argnum
);
5953 mono_mb_emit_byte (mb
, CEE_LDIND_I
);
5955 mono_mb_emit_op (mb
, CEE_CALLVIRT
, marshal_native_to_managed
);
5956 mono_mb_emit_stloc (mb
, conv_arg
);
5958 mono_mb_patch_branch (mb
, pos2
);
5961 case MARSHAL_ACTION_MANAGED_CONV_RESULT
:
5962 g_assert (!t
->byref
);
5964 loc1
= mono_mb_add_local (mb
, &mono_defaults
.object_class
->byval_arg
);
5966 mono_mb_emit_stloc (mb
, 3);
5968 mono_mb_emit_ldloc (mb
, 3);
5969 mono_mb_emit_stloc (mb
, loc1
);
5971 /* Check for null */
5972 mono_mb_emit_ldloc (mb
, 3);
5973 pos2
= mono_mb_emit_branch (mb
, CEE_BRFALSE
);
5975 mono_mb_emit_ldstr (mb
, g_strdup (spec
->data
.custom_data
.cookie
));
5976 mono_mb_emit_op (mb
, CEE_CALL
, get_instance
);
5977 mono_mb_emit_byte (mb
, CEE_DUP
);
5979 mono_mb_emit_ldloc (mb
, 3);
5980 mono_mb_emit_op (mb
, CEE_CALLVIRT
, marshal_managed_to_native
);
5981 mono_mb_emit_stloc (mb
, 3);
5983 mono_mb_emit_ldloc (mb
, loc1
);
5984 mono_mb_emit_op (mb
, CEE_CALLVIRT
, cleanup_managed
);
5986 mono_mb_patch_branch (mb
, pos2
);
5989 case MARSHAL_ACTION_MANAGED_CONV_OUT
:
5991 /* Check for null */
5992 mono_mb_emit_ldloc (mb
, conv_arg
);
5993 pos2
= mono_mb_emit_branch (mb
, CEE_BRFALSE
);
5996 mono_mb_emit_ldarg (mb
, argnum
);
5998 mono_mb_emit_ldstr (mb
, g_strdup (spec
->data
.custom_data
.cookie
));
6000 mono_mb_emit_op (mb
, CEE_CALL
, get_instance
);
6002 mono_mb_emit_ldloc (mb
, conv_arg
);
6003 mono_mb_emit_op (mb
, CEE_CALLVIRT
, marshal_managed_to_native
);
6004 mono_mb_emit_byte (mb
, CEE_STIND_I
);
6007 /* Call CleanUpManagedData */
6008 mono_mb_emit_ldstr (mb
, g_strdup (spec
->data
.custom_data
.cookie
));
6010 mono_mb_emit_op (mb
, CEE_CALL
, get_instance
);
6012 mono_mb_emit_ldloc (mb
, conv_arg
);
6013 mono_mb_emit_op (mb
, CEE_CALLVIRT
, cleanup_managed
);
6015 mono_mb_patch_branch (mb
, pos2
);
6019 g_assert_not_reached ();
6027 emit_marshal_asany (EmitMarshalContext
*m
, int argnum
, MonoType
*t
,
6028 MonoMarshalSpec
*spec
,
6029 int conv_arg
, MonoType
**conv_arg_type
,
6030 MarshalAction action
)
6033 MonoMethodBuilder
*mb
= m
->mb
;
6036 case MARSHAL_ACTION_CONV_IN
: {
6037 MonoMarshalNative encoding
= mono_marshal_get_string_encoding (m
->piinfo
, NULL
);
6039 g_assert (t
->type
== MONO_TYPE_OBJECT
);
6040 g_assert (!t
->byref
);
6042 conv_arg
= mono_mb_add_local (mb
, &mono_defaults
.int_class
->byval_arg
);
6043 mono_mb_emit_ldarg (mb
, argnum
);
6044 mono_mb_emit_icon (mb
, encoding
);
6045 mono_mb_emit_icon (mb
, t
->attrs
);
6046 mono_mb_emit_icall (mb
, mono_marshal_asany
);
6047 mono_mb_emit_stloc (mb
, conv_arg
);
6051 case MARSHAL_ACTION_PUSH
:
6052 mono_mb_emit_ldloc (mb
, conv_arg
);
6055 case MARSHAL_ACTION_CONV_OUT
: {
6056 MonoMarshalNative encoding
= mono_marshal_get_string_encoding (m
->piinfo
, NULL
);
6058 mono_mb_emit_ldarg (mb
, argnum
);
6059 mono_mb_emit_ldloc (mb
, conv_arg
);
6060 mono_mb_emit_icon (mb
, encoding
);
6061 mono_mb_emit_icon (mb
, t
->attrs
);
6062 mono_mb_emit_icall (mb
, mono_marshal_free_asany
);
6067 g_assert_not_reached ();
6074 emit_marshal_vtype (EmitMarshalContext
*m
, int argnum
, MonoType
*t
,
6075 MonoMarshalSpec
*spec
,
6076 int conv_arg
, MonoType
**conv_arg_type
,
6077 MarshalAction action
)
6080 MonoMethodBuilder
*mb
= m
->mb
;
6081 MonoClass
*klass
, *date_time_class
;
6084 klass
= mono_class_from_mono_type (t
);
6086 date_time_class
= mono_class_from_name_cached (mono_defaults
.corlib
, "System", "DateTime");
6089 case MARSHAL_ACTION_CONV_IN
:
6090 if (klass
== date_time_class
) {
6091 /* Convert it to an OLE DATE type */
6092 static MonoMethod
*to_oadate
;
6095 to_oadate
= mono_class_get_method_from_name (date_time_class
, "ToOADate", 0);
6096 g_assert (to_oadate
);
6098 conv_arg
= mono_mb_add_local (mb
, &mono_defaults
.double_class
->byval_arg
);
6101 mono_mb_emit_ldarg (mb
, argnum
);
6102 pos
= mono_mb_emit_branch (mb
, CEE_BRFALSE
);
6105 if (!(t
->byref
&& !(t
->attrs
& PARAM_ATTRIBUTE_IN
) && (t
->attrs
& PARAM_ATTRIBUTE_OUT
))) {
6107 m
->csig
->params
[argnum
- m
->csig
->hasthis
] = &mono_defaults
.double_class
->byval_arg
;
6109 mono_mb_emit_ldarg_addr (mb
, argnum
);
6110 mono_mb_emit_managed_call (mb
, to_oadate
, NULL
);
6111 mono_mb_emit_stloc (mb
, conv_arg
);
6115 mono_mb_patch_branch (mb
, pos
);
6119 if (((klass
->flags
& TYPE_ATTRIBUTE_LAYOUT_MASK
) == TYPE_ATTRIBUTE_EXPLICIT_LAYOUT
) ||
6120 klass
->blittable
|| klass
->enumtype
)
6123 conv_arg
= mono_mb_add_local (mb
, &mono_defaults
.int_class
->byval_arg
);
6125 /* store the address of the source into local variable 0 */
6127 mono_mb_emit_ldarg (mb
, argnum
);
6129 mono_mb_emit_ldarg_addr (mb
, argnum
);
6131 mono_mb_emit_stloc (mb
, 0);
6133 /* allocate space for the native struct and
6134 * store the address into local variable 1 (dest) */
6135 mono_mb_emit_icon (mb
, mono_class_native_size (klass
, NULL
));
6136 mono_mb_emit_byte (mb
, CEE_PREFIX1
);
6137 mono_mb_emit_byte (mb
, CEE_LOCALLOC
);
6138 mono_mb_emit_stloc (mb
, conv_arg
);
6141 mono_mb_emit_ldloc (mb
, 0);
6142 pos
= mono_mb_emit_branch (mb
, CEE_BRFALSE
);
6145 if (!(t
->byref
&& !(t
->attrs
& PARAM_ATTRIBUTE_IN
) && (t
->attrs
& PARAM_ATTRIBUTE_OUT
))) {
6147 mono_mb_emit_ldloc (mb
, conv_arg
);
6148 mono_mb_emit_stloc (mb
, 1);
6150 /* emit valuetype conversion code */
6151 emit_struct_conv (mb
, klass
, FALSE
);
6155 mono_mb_patch_branch (mb
, pos
);
6158 case MARSHAL_ACTION_PUSH
:
6159 if (spec
&& spec
->native
== MONO_NATIVE_LPSTRUCT
) {
6161 g_assert (!t
->byref
);
6163 /* Have to change the signature since the vtype is passed byref */
6164 m
->csig
->params
[argnum
- m
->csig
->hasthis
] = &mono_defaults
.int_class
->byval_arg
;
6166 if (((klass
->flags
& TYPE_ATTRIBUTE_LAYOUT_MASK
) == TYPE_ATTRIBUTE_EXPLICIT_LAYOUT
) ||
6167 klass
->blittable
|| klass
->enumtype
)
6168 mono_mb_emit_ldarg_addr (mb
, argnum
);
6170 mono_mb_emit_ldloc (mb
, conv_arg
);
6174 if (klass
== date_time_class
) {
6176 mono_mb_emit_ldloc_addr (mb
, conv_arg
);
6178 mono_mb_emit_ldloc (mb
, conv_arg
);
6182 if (((klass
->flags
& TYPE_ATTRIBUTE_LAYOUT_MASK
) == TYPE_ATTRIBUTE_EXPLICIT_LAYOUT
) ||
6183 klass
->blittable
|| klass
->enumtype
) {
6184 mono_mb_emit_ldarg (mb
, argnum
);
6187 mono_mb_emit_ldloc (mb
, conv_arg
);
6189 mono_mb_emit_byte (mb
, MONO_CUSTOM_PREFIX
);
6190 mono_mb_emit_op (mb
, CEE_MONO_LDNATIVEOBJ
, klass
);
6194 case MARSHAL_ACTION_CONV_OUT
:
6195 if (klass
== date_time_class
) {
6196 /* Convert from an OLE DATE type */
6197 static MonoMethod
*from_oadate
;
6202 if (!((t
->attrs
& PARAM_ATTRIBUTE_IN
) && !(t
->attrs
& PARAM_ATTRIBUTE_OUT
))) {
6204 from_oadate
= mono_class_get_method_from_name (date_time_class
, "FromOADate", 1);
6205 g_assert (from_oadate
);
6207 mono_mb_emit_ldarg (mb
, argnum
);
6208 mono_mb_emit_ldloc (mb
, conv_arg
);
6209 mono_mb_emit_managed_call (mb
, from_oadate
, NULL
);
6210 mono_mb_emit_op (mb
, CEE_STOBJ
, date_time_class
);
6215 if (((klass
->flags
& TYPE_ATTRIBUTE_LAYOUT_MASK
) == TYPE_ATTRIBUTE_EXPLICIT_LAYOUT
) ||
6216 klass
->blittable
|| klass
->enumtype
)
6220 /* dst = argument */
6221 mono_mb_emit_ldarg (mb
, argnum
);
6222 mono_mb_emit_stloc (mb
, 1);
6224 mono_mb_emit_ldloc (mb
, 1);
6225 pos
= mono_mb_emit_branch (mb
, CEE_BRFALSE
);
6227 if (!((t
->attrs
& PARAM_ATTRIBUTE_IN
) && !(t
->attrs
& PARAM_ATTRIBUTE_OUT
))) {
6228 /* src = tmp_locals [i] */
6229 mono_mb_emit_ldloc (mb
, conv_arg
);
6230 mono_mb_emit_stloc (mb
, 0);
6232 /* emit valuetype conversion code */
6233 emit_struct_conv (mb
, klass
, TRUE
);
6237 emit_struct_free (mb
, klass
, conv_arg
);
6240 mono_mb_patch_branch (mb
, pos
);
6243 case MARSHAL_ACTION_CONV_RESULT
:
6244 if (((klass
->flags
& TYPE_ATTRIBUTE_LAYOUT_MASK
) == TYPE_ATTRIBUTE_EXPLICIT_LAYOUT
) ||
6246 mono_mb_emit_stloc (mb
, 3);
6250 /* load pointer to returned value type */
6251 mono_mb_emit_byte (mb
, MONO_CUSTOM_PREFIX
);
6252 mono_mb_emit_byte (mb
, CEE_MONO_VTADDR
);
6253 /* store the address of the source into local variable 0 */
6254 mono_mb_emit_stloc (mb
, 0);
6256 mono_mb_emit_ldloc_addr (mb
, 3);
6257 mono_mb_emit_stloc (mb
, 1);
6259 /* emit valuetype conversion code */
6260 emit_struct_conv (mb
, klass
, TRUE
);
6263 case MARSHAL_ACTION_MANAGED_CONV_IN
:
6264 if (((klass
->flags
& TYPE_ATTRIBUTE_LAYOUT_MASK
) == TYPE_ATTRIBUTE_EXPLICIT_LAYOUT
) ||
6265 klass
->blittable
|| klass
->enumtype
) {
6270 conv_arg
= mono_mb_add_local (mb
, &klass
->byval_arg
);
6272 if (t
->attrs
& PARAM_ATTRIBUTE_OUT
)
6276 mono_mb_emit_ldarg (mb
, argnum
);
6278 mono_mb_emit_ldarg_addr (mb
, argnum
);
6279 mono_mb_emit_stloc (mb
, 0);
6282 mono_mb_emit_ldloc (mb
, 0);
6283 pos
= mono_mb_emit_branch (mb
, CEE_BRFALSE
);
6286 mono_mb_emit_ldloc_addr (mb
, conv_arg
);
6287 mono_mb_emit_stloc (mb
, 1);
6289 /* emit valuetype conversion code */
6290 emit_struct_conv (mb
, klass
, TRUE
);
6293 mono_mb_patch_branch (mb
, pos
);
6296 case MARSHAL_ACTION_MANAGED_CONV_OUT
:
6297 if (((klass
->flags
& TYPE_ATTRIBUTE_LAYOUT_MASK
) == TYPE_ATTRIBUTE_EXPLICIT_LAYOUT
) ||
6298 klass
->blittable
|| klass
->enumtype
) {
6302 /* Check for null */
6303 mono_mb_emit_ldarg (mb
, argnum
);
6304 pos2
= mono_mb_emit_branch (mb
, CEE_BRFALSE
);
6307 mono_mb_emit_ldloc_addr (mb
, conv_arg
);
6308 mono_mb_emit_stloc (mb
, 0);
6311 mono_mb_emit_ldarg (mb
, argnum
);
6312 mono_mb_emit_stloc (mb
, 1);
6314 /* emit valuetype conversion code */
6315 emit_struct_conv (mb
, klass
, FALSE
);
6317 mono_mb_patch_branch (mb
, pos2
);
6320 case MARSHAL_ACTION_MANAGED_CONV_RESULT
:
6321 if (((klass
->flags
& TYPE_ATTRIBUTE_LAYOUT_MASK
) == TYPE_ATTRIBUTE_EXPLICIT_LAYOUT
) ||
6322 klass
->blittable
|| klass
->enumtype
) {
6323 mono_mb_emit_stloc (mb
, 3);
6328 /* load pointer to returned value type */
6329 mono_mb_emit_byte (mb
, MONO_CUSTOM_PREFIX
);
6330 mono_mb_emit_byte (mb
, CEE_MONO_VTADDR
);
6332 /* store the address of the source into local variable 0 */
6333 mono_mb_emit_stloc (mb
, 0);
6334 /* allocate space for the native struct and
6335 * store the address into dst_ptr */
6336 m
->retobj_var
= mono_mb_add_local (mb
, &mono_defaults
.int_class
->byval_arg
);
6337 m
->retobj_class
= klass
;
6338 g_assert (m
->retobj_var
);
6339 mono_mb_emit_icon (mb
, mono_class_native_size (klass
, NULL
));
6340 mono_mb_emit_byte (mb
, CEE_CONV_I
);
6341 mono_mb_emit_icall (mb
, mono_marshal_alloc
);
6342 mono_mb_emit_stloc (mb
, 1);
6343 mono_mb_emit_ldloc (mb
, 1);
6344 mono_mb_emit_stloc (mb
, m
->retobj_var
);
6346 /* emit valuetype conversion code */
6347 emit_struct_conv (mb
, klass
, FALSE
);
6351 g_assert_not_reached ();
6358 emit_marshal_string (EmitMarshalContext
*m
, int argnum
, MonoType
*t
,
6359 MonoMarshalSpec
*spec
,
6360 int conv_arg
, MonoType
**conv_arg_type
,
6361 MarshalAction action
)
6365 case MARSHAL_ACTION_CONV_IN
:
6366 *conv_arg_type
= &mono_defaults
.int_class
->byval_arg
;
6368 case MARSHAL_ACTION_MANAGED_CONV_IN
:
6369 *conv_arg_type
= &mono_defaults
.int_class
->byval_arg
;
6373 MonoMethodBuilder
*mb
= m
->mb
;
6374 MonoMarshalNative encoding
= mono_marshal_get_string_encoding (m
->piinfo
, spec
);
6375 MonoMarshalConv conv
= mono_marshal_get_string_to_ptr_conv (m
->piinfo
, spec
);
6379 case MARSHAL_ACTION_CONV_IN
:
6380 *conv_arg_type
= &mono_defaults
.int_class
->byval_arg
;
6381 conv_arg
= mono_mb_add_local (mb
, &mono_defaults
.int_class
->byval_arg
);
6384 if (t
->attrs
& PARAM_ATTRIBUTE_OUT
)
6387 mono_mb_emit_ldarg (mb
, argnum
);
6388 mono_mb_emit_byte (mb
, CEE_LDIND_I
);
6390 mono_mb_emit_ldarg (mb
, argnum
);
6394 char *msg
= g_strdup_printf ("string marshalling conversion %d not implemented", encoding
);
6395 mono_mb_emit_exception_marshal_directive (mb
, msg
);
6397 mono_mb_emit_icall (mb
, conv_to_icall (conv
));
6399 mono_mb_emit_stloc (mb
, conv_arg
);
6403 case MARSHAL_ACTION_CONV_OUT
:
6404 conv
= mono_marshal_get_ptr_to_string_conv (m
->piinfo
, spec
, &need_free
);
6406 char *msg
= g_strdup_printf ("string marshalling conversion %d not implemented", encoding
);
6407 mono_mb_emit_exception_marshal_directive (mb
, msg
);
6411 if (encoding
== MONO_NATIVE_VBBYREFSTR
) {
6412 static MonoMethod
*m
;
6415 m
= mono_class_get_method_from_name_flags (mono_defaults
.string_class
, "get_Length", -1, 0);
6420 char *msg
= g_strdup_printf ("VBByRefStr marshalling requires a ref parameter.", encoding
);
6421 mono_mb_emit_exception_marshal_directive (mb
, msg
);
6426 * Have to allocate a new string with the same length as the original, and
6427 * copy the contents of the buffer pointed to by CONV_ARG into it.
6429 g_assert (t
->byref
);
6430 mono_mb_emit_ldarg (mb
, argnum
);
6431 mono_mb_emit_ldloc (mb
, conv_arg
);
6432 mono_mb_emit_ldarg (mb
, argnum
);
6433 mono_mb_emit_byte (mb
, CEE_LDIND_I
);
6434 mono_mb_emit_managed_call (mb
, m
, NULL
);
6435 mono_mb_emit_icall (mb
, mono_string_new_len_wrapper
);
6436 mono_mb_emit_byte (mb
, CEE_STIND_REF
);
6437 } else if (t
->byref
&& (t
->attrs
& PARAM_ATTRIBUTE_OUT
|| !(t
->attrs
& PARAM_ATTRIBUTE_IN
))) {
6438 mono_mb_emit_ldarg (mb
, argnum
);
6439 mono_mb_emit_ldloc (mb
, conv_arg
);
6440 mono_mb_emit_icall (mb
, conv_to_icall (conv
));
6441 mono_mb_emit_byte (mb
, CEE_STIND_REF
);
6446 mono_mb_emit_ldloc (mb
, conv_arg
);
6447 if (conv
== MONO_MARSHAL_CONV_BSTR_STR
)
6448 mono_mb_emit_icall (mb
, mono_free_bstr
);
6450 mono_mb_emit_icall (mb
, mono_marshal_free
);
6454 case MARSHAL_ACTION_PUSH
:
6455 if (t
->byref
&& encoding
!= MONO_NATIVE_VBBYREFSTR
)
6456 mono_mb_emit_ldloc_addr (mb
, conv_arg
);
6458 mono_mb_emit_ldloc (mb
, conv_arg
);
6461 case MARSHAL_ACTION_CONV_RESULT
:
6462 mono_mb_emit_stloc (mb
, 0);
6464 conv
= mono_marshal_get_ptr_to_string_conv (m
->piinfo
, spec
, &need_free
);
6466 char *msg
= g_strdup_printf ("string marshalling conversion %d not implemented", encoding
);
6467 mono_mb_emit_exception_marshal_directive (mb
, msg
);
6471 mono_mb_emit_ldloc (mb
, 0);
6472 mono_mb_emit_icall (mb
, conv_to_icall (conv
));
6473 mono_mb_emit_stloc (mb
, 3);
6475 /* free the string */
6476 mono_mb_emit_ldloc (mb
, 0);
6477 if (conv
== MONO_MARSHAL_CONV_BSTR_STR
)
6478 mono_mb_emit_icall (mb
, mono_free_bstr
);
6480 mono_mb_emit_icall (mb
, mono_marshal_free
);
6483 case MARSHAL_ACTION_MANAGED_CONV_IN
:
6484 conv_arg
= mono_mb_add_local (mb
, &mono_defaults
.object_class
->byval_arg
);
6486 *conv_arg_type
= &mono_defaults
.int_class
->byval_arg
;
6489 if (t
->attrs
& PARAM_ATTRIBUTE_OUT
)
6493 conv
= mono_marshal_get_ptr_to_string_conv (m
->piinfo
, spec
, &need_free
);
6495 char *msg
= g_strdup_printf ("string marshalling conversion %d not implemented", encoding
);
6496 mono_mb_emit_exception_marshal_directive (mb
, msg
);
6500 mono_mb_emit_ldarg (mb
, argnum
);
6502 mono_mb_emit_byte (mb
, CEE_LDIND_I
);
6503 mono_mb_emit_icall (mb
, conv_to_icall (conv
));
6504 mono_mb_emit_stloc (mb
, conv_arg
);
6507 case MARSHAL_ACTION_MANAGED_CONV_OUT
:
6510 mono_mb_emit_ldarg (mb
, argnum
);
6511 mono_mb_emit_ldloc (mb
, conv_arg
);
6512 mono_mb_emit_icall (mb
, conv_to_icall (conv
));
6513 mono_mb_emit_byte (mb
, CEE_STIND_I
);
6518 case MARSHAL_ACTION_MANAGED_CONV_RESULT
:
6519 if (conv_to_icall (conv
) == mono_marshal_string_to_utf16
)
6520 /* We need to make a copy so the caller is able to free it */
6521 mono_mb_emit_icall (mb
, mono_marshal_string_to_utf16_copy
);
6523 mono_mb_emit_icall (mb
, conv_to_icall (conv
));
6524 mono_mb_emit_stloc (mb
, 3);
6528 g_assert_not_reached ();
6536 emit_marshal_safehandle (EmitMarshalContext
*m
, int argnum
, MonoType
*t
,
6537 MonoMarshalSpec
*spec
, int conv_arg
,
6538 MonoType
**conv_arg_type
, MarshalAction action
)
6541 if (action
== MARSHAL_ACTION_CONV_IN
)
6542 *conv_arg_type
= &mono_defaults
.int_class
->byval_arg
;
6544 MonoMethodBuilder
*mb
= m
->mb
;
6547 case MARSHAL_ACTION_CONV_IN
: {
6548 MonoType
*intptr_type
;
6549 int dar_release_slot
, pos
;
6551 intptr_type
= &mono_defaults
.int_class
->byval_arg
;
6552 conv_arg
= mono_mb_add_local (mb
, intptr_type
);
6553 *conv_arg_type
= intptr_type
;
6555 if (!sh_dangerous_add_ref
)
6556 init_safe_handle ();
6558 mono_mb_emit_ldarg (mb
, argnum
);
6559 pos
= mono_mb_emit_branch (mb
, CEE_BRTRUE
);
6560 mono_mb_emit_exception (mb
, "ArgumentNullException", NULL
);
6562 mono_mb_patch_branch (mb
, pos
);
6565 * My tests in show that ref SafeHandles are not really
6566 * passed as ref objects. Instead a NULL is passed as the
6569 mono_mb_emit_icon (mb
, 0);
6570 mono_mb_emit_stloc (mb
, conv_arg
);
6574 /* Create local to hold the ref parameter to DangerousAddRef */
6575 dar_release_slot
= mono_mb_add_local (mb
, &mono_defaults
.boolean_class
->byval_arg
);
6577 /* set release = false; */
6578 mono_mb_emit_icon (mb
, 0);
6579 mono_mb_emit_stloc (mb
, dar_release_slot
);
6581 /* safehandle.DangerousAddRef (ref release) */
6582 mono_mb_emit_ldarg (mb
, argnum
);
6583 mono_mb_emit_ldloc_addr (mb
, dar_release_slot
);
6584 mono_mb_emit_managed_call (mb
, sh_dangerous_add_ref
, NULL
);
6586 /* Pull the handle field from SafeHandle */
6587 mono_mb_emit_ldarg (mb
, argnum
);
6588 mono_mb_emit_ldflda (mb
, G_STRUCT_OFFSET (MonoSafeHandle
, handle
));
6589 mono_mb_emit_byte (mb
, CEE_LDIND_I
);
6590 mono_mb_emit_stloc (mb
, conv_arg
);
6595 case MARSHAL_ACTION_PUSH
:
6597 mono_mb_emit_ldloc_addr (mb
, conv_arg
);
6599 mono_mb_emit_ldloc (mb
, conv_arg
);
6602 case MARSHAL_ACTION_CONV_OUT
: {
6603 /* The slot for the boolean is the next temporary created after conv_arg, see the CONV_IN code */
6604 int dar_release_slot
= conv_arg
+ 1;
6607 if (!sh_dangerous_release
)
6608 init_safe_handle ();
6614 * My tests indicate that ref SafeHandles parameters are not actually
6615 * passed by ref, but instead a new Handle is created regardless of
6616 * whether a change happens in the unmanaged side.
6618 * Also, the Handle is created before calling into unmanaged code,
6619 * but we do not support that mechanism (getting to the original
6620 * handle) and it makes no difference where we create this
6622 ctor
= mono_class_get_method_from_name (t
->data
.klass
, ".ctor", 0);
6624 mono_mb_emit_exception (mb
, "MissingMethodException", "paramterless constructor required");
6627 /* refval = new SafeHandleDerived ()*/
6628 mono_mb_emit_ldarg (mb
, argnum
);
6629 mono_mb_emit_op (mb
, CEE_NEWOBJ
, ctor
);
6630 mono_mb_emit_byte (mb
, CEE_STIND_REF
);
6632 /* refval.handle = returned_handle */
6633 mono_mb_emit_ldarg (mb
, argnum
);
6634 mono_mb_emit_byte (mb
, CEE_LDIND_REF
);
6635 mono_mb_emit_ldflda (mb
, G_STRUCT_OFFSET (MonoSafeHandle
, handle
));
6636 mono_mb_emit_ldloc (mb
, conv_arg
);
6637 mono_mb_emit_byte (mb
, CEE_STIND_I
);
6639 mono_mb_emit_ldloc (mb
, dar_release_slot
);
6640 label_next
= mono_mb_emit_branch (mb
, CEE_BRFALSE
);
6641 mono_mb_emit_ldarg (mb
, argnum
);
6642 mono_mb_emit_managed_call (mb
, sh_dangerous_release
, NULL
);
6643 mono_mb_patch_branch (mb
, label_next
);
6648 case MARSHAL_ACTION_CONV_RESULT
: {
6649 MonoMethod
*ctor
= NULL
;
6650 int intptr_handle_slot
;
6652 if (t
->data
.klass
->flags
& TYPE_ATTRIBUTE_ABSTRACT
){
6653 mono_mb_emit_byte (mb
, CEE_POP
);
6654 mono_mb_emit_exception_marshal_directive (mb
, g_strdup ("Returned SafeHandles should not be abstract"));
6658 ctor
= mono_class_get_method_from_name (t
->data
.klass
, ".ctor", 0);
6660 mono_mb_emit_byte (mb
, CEE_POP
);
6661 mono_mb_emit_exception (mb
, "MissingMethodException", "paramterless constructor required");
6664 /* Store the IntPtr results into a local */
6665 intptr_handle_slot
= mono_mb_add_local (mb
, &mono_defaults
.int_class
->byval_arg
);
6666 mono_mb_emit_stloc (mb
, intptr_handle_slot
);
6668 /* Create return value */
6669 mono_mb_emit_op (mb
, CEE_NEWOBJ
, ctor
);
6670 mono_mb_emit_stloc (mb
, 3);
6672 /* Set the return.handle to the value, am using ldflda, not sure if thats a good idea */
6673 mono_mb_emit_ldloc (mb
, 3);
6674 mono_mb_emit_ldflda (mb
, G_STRUCT_OFFSET (MonoSafeHandle
, handle
));
6675 mono_mb_emit_ldloc (mb
, intptr_handle_slot
);
6676 mono_mb_emit_byte (mb
, CEE_STIND_I
);
6680 case MARSHAL_ACTION_MANAGED_CONV_IN
:
6681 fprintf (stderr
, "mono/marshal: SafeHandles missing MANAGED_CONV_IN\n");
6684 case MARSHAL_ACTION_MANAGED_CONV_OUT
:
6685 fprintf (stderr
, "mono/marshal: SafeHandles missing MANAGED_CONV_OUT\n");
6688 case MARSHAL_ACTION_MANAGED_CONV_RESULT
:
6689 fprintf (stderr
, "mono/marshal: SafeHandles missing MANAGED_CONV_RESULT\n");
6692 printf ("Unhandled case for MarshalAction: %d\n", action
);
6700 emit_marshal_handleref (EmitMarshalContext
*m
, int argnum
, MonoType
*t
,
6701 MonoMarshalSpec
*spec
, int conv_arg
,
6702 MonoType
**conv_arg_type
, MarshalAction action
)
6705 if (action
== MARSHAL_ACTION_CONV_IN
)
6706 *conv_arg_type
= &mono_defaults
.int_class
->byval_arg
;
6708 MonoMethodBuilder
*mb
= m
->mb
;
6711 case MARSHAL_ACTION_CONV_IN
: {
6712 MonoType
*intptr_type
;
6714 intptr_type
= &mono_defaults
.int_class
->byval_arg
;
6715 conv_arg
= mono_mb_add_local (mb
, intptr_type
);
6716 *conv_arg_type
= intptr_type
;
6719 char *msg
= g_strdup ("HandleRefs can not be returned from unmanaged code (or passed by ref)");
6720 mono_mb_emit_exception_marshal_directive (mb
, msg
);
6723 mono_mb_emit_ldarg_addr (mb
, argnum
);
6724 mono_mb_emit_icon (mb
, G_STRUCT_OFFSET (MonoHandleRef
, handle
));
6725 mono_mb_emit_byte (mb
, CEE_ADD
);
6726 mono_mb_emit_byte (mb
, CEE_LDIND_I
);
6727 mono_mb_emit_stloc (mb
, conv_arg
);
6731 case MARSHAL_ACTION_PUSH
:
6732 mono_mb_emit_ldloc (mb
, conv_arg
);
6735 case MARSHAL_ACTION_CONV_OUT
: {
6736 /* no resource release required */
6740 case MARSHAL_ACTION_CONV_RESULT
: {
6741 char *msg
= g_strdup ("HandleRefs can not be returned from unmanaged code (or passed by ref)");
6742 mono_mb_emit_exception_marshal_directive (mb
, msg
);
6746 case MARSHAL_ACTION_MANAGED_CONV_IN
:
6747 fprintf (stderr
, "mono/marshal: SafeHandles missing MANAGED_CONV_IN\n");
6750 case MARSHAL_ACTION_MANAGED_CONV_OUT
:
6751 fprintf (stderr
, "mono/marshal: SafeHandles missing MANAGED_CONV_OUT\n");
6754 case MARSHAL_ACTION_MANAGED_CONV_RESULT
:
6755 fprintf (stderr
, "mono/marshal: SafeHandles missing MANAGED_CONV_RESULT\n");
6758 fprintf (stderr
, "Unhandled case for MarshalAction: %d\n", action
);
6766 emit_marshal_object (EmitMarshalContext
*m
, int argnum
, MonoType
*t
,
6767 MonoMarshalSpec
*spec
,
6768 int conv_arg
, MonoType
**conv_arg_type
,
6769 MarshalAction action
)
6772 if (action
== MARSHAL_ACTION_CONV_IN
)
6773 *conv_arg_type
= &mono_defaults
.int_class
->byval_arg
;
6775 MonoMethodBuilder
*mb
= m
->mb
;
6776 MonoClass
*klass
= mono_class_from_mono_type (t
);
6780 case MARSHAL_ACTION_CONV_IN
:
6781 *conv_arg_type
= &mono_defaults
.int_class
->byval_arg
;
6782 conv_arg
= mono_mb_add_local (mb
, &mono_defaults
.int_class
->byval_arg
);
6784 m
->orig_conv_args
[argnum
] = 0;
6786 if (mono_class_from_mono_type (t
) == mono_defaults
.object_class
) {
6787 char *msg
= g_strdup_printf ("Marshalling of type object is not implemented");
6788 mono_mb_emit_exception_marshal_directive (mb
, msg
);
6792 if (klass
->delegate
) {
6794 if (!(t
->attrs
& PARAM_ATTRIBUTE_OUT
)) {
6795 char *msg
= g_strdup_printf ("Byref marshalling of delegates is not implemented.");
6796 mono_mb_emit_exception_marshal_directive (mb
, msg
);
6798 mono_mb_emit_byte (mb
, CEE_LDNULL
);
6799 mono_mb_emit_stloc (mb
, conv_arg
);
6801 mono_mb_emit_ldarg (mb
, argnum
);
6802 mono_mb_emit_icall (mb
, conv_to_icall (MONO_MARSHAL_CONV_DEL_FTN
));
6803 mono_mb_emit_stloc (mb
, conv_arg
);
6805 } else if (klass
== mono_defaults
.stringbuilder_class
) {
6806 MonoMarshalNative encoding
= mono_marshal_get_string_encoding (m
->piinfo
, spec
);
6807 MonoMarshalConv conv
= mono_marshal_get_stringbuilder_to_ptr_conv (m
->piinfo
, spec
);
6811 if (!(t
->attrs
& PARAM_ATTRIBUTE_OUT
)) {
6812 char *msg
= g_strdup_printf ("Byref marshalling of stringbuilders is not implemented.");
6813 mono_mb_emit_exception_marshal_directive (mb
, msg
);
6819 if (t
->byref
&& !t
->attrs
& PARAM_ATTRIBUTE_IN
&& t
->attrs
& PARAM_ATTRIBUTE_OUT
)
6823 char *msg
= g_strdup_printf ("stringbuilder marshalling conversion %d not implemented", encoding
);
6824 mono_mb_emit_exception_marshal_directive (mb
, msg
);
6828 mono_mb_emit_ldarg (mb
, argnum
);
6830 mono_mb_emit_byte (mb
, CEE_LDIND_I
);
6832 mono_mb_emit_icall (mb
, conv_to_icall (conv
));
6833 mono_mb_emit_stloc (mb
, conv_arg
);
6834 } else if (klass
->blittable
) {
6835 mono_mb_emit_byte (mb
, CEE_LDNULL
);
6836 mono_mb_emit_stloc (mb
, conv_arg
);
6838 mono_mb_emit_ldarg (mb
, argnum
);
6839 pos
= mono_mb_emit_branch (mb
, CEE_BRFALSE
);
6841 mono_mb_emit_ldarg (mb
, argnum
);
6842 mono_mb_emit_ldflda (mb
, sizeof (MonoObject
));
6843 mono_mb_emit_stloc (mb
, conv_arg
);
6845 mono_mb_patch_branch (mb
, pos
);
6848 mono_mb_emit_byte (mb
, CEE_LDNULL
);
6849 mono_mb_emit_stloc (mb
, conv_arg
);
6852 /* we dont need any conversions for out parameters */
6853 if (t
->attrs
& PARAM_ATTRIBUTE_OUT
)
6856 mono_mb_emit_ldarg (mb
, argnum
);
6857 mono_mb_emit_byte (mb
, CEE_LDIND_I
);
6860 mono_mb_emit_ldarg (mb
, argnum
);
6861 mono_mb_emit_byte (mb
, MONO_CUSTOM_PREFIX
);
6862 mono_mb_emit_byte (mb
, CEE_MONO_OBJADDR
);
6865 /* store the address of the source into local variable 0 */
6866 mono_mb_emit_stloc (mb
, 0);
6867 mono_mb_emit_ldloc (mb
, 0);
6868 pos
= mono_mb_emit_branch (mb
, CEE_BRFALSE
);
6870 /* allocate space for the native struct and store the address */
6871 mono_mb_emit_icon (mb
, mono_class_native_size (klass
, NULL
));
6872 mono_mb_emit_byte (mb
, CEE_PREFIX1
);
6873 mono_mb_emit_byte (mb
, CEE_LOCALLOC
);
6874 mono_mb_emit_stloc (mb
, conv_arg
);
6877 /* Need to store the original buffer so we can free it later */
6878 m
->orig_conv_args
[argnum
] = mono_mb_add_local (mb
, &mono_defaults
.int_class
->byval_arg
);
6879 mono_mb_emit_ldloc (mb
, conv_arg
);
6880 mono_mb_emit_stloc (mb
, m
->orig_conv_args
[argnum
]);
6883 /* set the src_ptr */
6884 mono_mb_emit_ldloc (mb
, 0);
6885 mono_mb_emit_ldflda (mb
, sizeof (MonoObject
));
6886 mono_mb_emit_stloc (mb
, 0);
6889 mono_mb_emit_ldloc (mb
, conv_arg
);
6890 mono_mb_emit_stloc (mb
, 1);
6892 /* emit valuetype conversion code */
6893 emit_struct_conv (mb
, klass
, FALSE
);
6895 mono_mb_patch_branch (mb
, pos
);
6899 case MARSHAL_ACTION_CONV_OUT
:
6900 if (klass
== mono_defaults
.stringbuilder_class
) {
6902 MonoMarshalNative encoding
;
6903 MonoMarshalConv conv
;
6905 encoding
= mono_marshal_get_string_encoding (m
->piinfo
, spec
);
6906 conv
= mono_marshal_get_ptr_to_stringbuilder_conv (m
->piinfo
, spec
, &need_free
);
6908 g_assert (encoding
!= -1);
6911 //g_assert (!(t->attrs & PARAM_ATTRIBUTE_OUT));
6915 mono_mb_emit_ldarg (mb
, argnum
);
6916 mono_mb_emit_ldloc (mb
, conv_arg
);
6919 case MONO_NATIVE_LPWSTR
:
6920 mono_mb_emit_icall (mb
, mono_string_utf16_to_builder2
);
6922 case MONO_NATIVE_LPSTR
:
6923 mono_mb_emit_icall (mb
, mono_string_utf8_to_builder2
);
6926 g_assert_not_reached ();
6929 mono_mb_emit_byte (mb
, CEE_STIND_REF
);
6931 mono_mb_emit_ldarg (mb
, argnum
);
6932 mono_mb_emit_ldloc (mb
, conv_arg
);
6934 mono_mb_emit_icall (mb
, conv_to_icall (conv
));
6938 mono_mb_emit_ldloc (mb
, conv_arg
);
6939 mono_mb_emit_icall (mb
, mono_marshal_free
);
6944 if (klass
->delegate
) {
6946 mono_mb_emit_ldarg (mb
, argnum
);
6947 mono_mb_emit_byte (mb
, MONO_CUSTOM_PREFIX
);
6948 mono_mb_emit_op (mb
, CEE_MONO_CLASSCONST
, klass
);
6949 mono_mb_emit_ldloc (mb
, conv_arg
);
6950 mono_mb_emit_icall (mb
, conv_to_icall (MONO_MARSHAL_CONV_FTN_DEL
));
6951 mono_mb_emit_byte (mb
, CEE_STIND_REF
);
6956 if (t
->byref
&& (t
->attrs
& PARAM_ATTRIBUTE_OUT
)) {
6957 /* allocate a new object */
6958 mono_mb_emit_ldarg (mb
, argnum
);
6959 mono_mb_emit_byte (mb
, MONO_CUSTOM_PREFIX
);
6960 mono_mb_emit_op (mb
, CEE_MONO_NEWOBJ
, klass
);
6961 mono_mb_emit_byte (mb
, CEE_STIND_REF
);
6964 /* dst = *argument */
6965 mono_mb_emit_ldarg (mb
, argnum
);
6968 mono_mb_emit_byte (mb
, CEE_LDIND_I
);
6970 mono_mb_emit_stloc (mb
, 1);
6972 mono_mb_emit_ldloc (mb
, 1);
6973 pos
= mono_mb_emit_branch (mb
, CEE_BRFALSE
);
6975 if (t
->byref
|| (t
->attrs
& PARAM_ATTRIBUTE_OUT
)) {
6976 mono_mb_emit_ldloc (mb
, 1);
6977 mono_mb_emit_icon (mb
, sizeof (MonoObject
));
6978 mono_mb_emit_byte (mb
, CEE_ADD
);
6979 mono_mb_emit_stloc (mb
, 1);
6981 /* src = tmp_locals [i] */
6982 mono_mb_emit_ldloc (mb
, conv_arg
);
6983 mono_mb_emit_stloc (mb
, 0);
6985 /* emit valuetype conversion code */
6986 emit_struct_conv (mb
, klass
, TRUE
);
6988 /* Free the structure returned by the native code */
6989 emit_struct_free (mb
, klass
, conv_arg
);
6991 if (m
->orig_conv_args
[argnum
]) {
6993 * If the native function changed the pointer, then free
6994 * the original structure plus the new pointer.
6996 mono_mb_emit_ldloc (mb
, m
->orig_conv_args
[argnum
]);
6997 mono_mb_emit_ldloc (mb
, conv_arg
);
6998 pos2
= mono_mb_emit_branch (mb
, CEE_BEQ
);
7000 if (!(t
->attrs
& PARAM_ATTRIBUTE_OUT
)) {
7001 g_assert (m
->orig_conv_args
[argnum
]);
7003 emit_struct_free (mb
, klass
, m
->orig_conv_args
[argnum
]);
7006 mono_mb_emit_ldloc (mb
, conv_arg
);
7007 mono_mb_emit_icall (mb
, mono_marshal_free
);
7009 mono_mb_patch_branch (mb
, pos2
);
7013 /* Free the original structure passed to native code */
7014 emit_struct_free (mb
, klass
, conv_arg
);
7016 mono_mb_patch_branch (mb
, pos
);
7019 case MARSHAL_ACTION_PUSH
:
7021 mono_mb_emit_ldloc_addr (mb
, conv_arg
);
7023 mono_mb_emit_ldloc (mb
, conv_arg
);
7026 case MARSHAL_ACTION_CONV_RESULT
:
7027 if (klass
->delegate
) {
7028 g_assert (!t
->byref
);
7029 mono_mb_emit_stloc (mb
, 0);
7030 mono_mb_emit_byte (mb
, MONO_CUSTOM_PREFIX
);
7031 mono_mb_emit_op (mb
, CEE_MONO_CLASSCONST
, klass
);
7032 mono_mb_emit_ldloc (mb
, 0);
7033 mono_mb_emit_icall (mb
, conv_to_icall (MONO_MARSHAL_CONV_FTN_DEL
));
7034 mono_mb_emit_stloc (mb
, 3);
7037 mono_mb_emit_stloc (mb
, 0);
7039 /* Make a copy since emit_conv modifies local 0 */
7040 loc
= mono_mb_add_local (mb
, &mono_defaults
.int_class
->byval_arg
);
7041 mono_mb_emit_ldloc (mb
, 0);
7042 mono_mb_emit_stloc (mb
, loc
);
7044 mono_mb_emit_byte (mb
, CEE_LDNULL
);
7045 mono_mb_emit_stloc (mb
, 3);
7047 mono_mb_emit_ldloc (mb
, 0);
7048 pos
= mono_mb_emit_branch (mb
, CEE_BRFALSE
);
7050 /* allocate result object */
7052 mono_mb_emit_byte (mb
, MONO_CUSTOM_PREFIX
);
7053 mono_mb_emit_op (mb
, CEE_MONO_NEWOBJ
, klass
);
7054 mono_mb_emit_stloc (mb
, 3);
7058 mono_mb_emit_ldloc (mb
, 3);
7059 mono_mb_emit_ldflda (mb
, sizeof (MonoObject
));
7060 mono_mb_emit_stloc (mb
, 1);
7062 /* emit conversion code */
7063 emit_struct_conv (mb
, klass
, TRUE
);
7065 emit_struct_free (mb
, klass
, loc
);
7067 /* Free the pointer allocated by unmanaged code */
7068 mono_mb_emit_ldloc (mb
, loc
);
7069 mono_mb_emit_icall (mb
, mono_marshal_free
);
7070 mono_mb_patch_branch (mb
, pos
);
7074 case MARSHAL_ACTION_MANAGED_CONV_IN
:
7075 conv_arg
= mono_mb_add_local (mb
, &klass
->byval_arg
);
7077 if (klass
->delegate
) {
7078 g_assert (!t
->byref
);
7079 mono_mb_emit_byte (mb
, MONO_CUSTOM_PREFIX
);
7080 mono_mb_emit_op (mb
, CEE_MONO_CLASSCONST
, klass
);
7081 mono_mb_emit_ldarg (mb
, argnum
);
7082 mono_mb_emit_icall (mb
, conv_to_icall (MONO_MARSHAL_CONV_FTN_DEL
));
7083 mono_mb_emit_stloc (mb
, conv_arg
);
7087 if (klass
== mono_defaults
.stringbuilder_class
) {
7088 MonoMarshalNative encoding
;
7090 encoding
= mono_marshal_get_string_encoding (m
->piinfo
, spec
);
7093 g_assert (encoding
== MONO_NATIVE_LPSTR
);
7095 g_assert (!t
->byref
);
7096 g_assert (encoding
!= -1);
7098 mono_mb_emit_ldarg (mb
, argnum
);
7099 mono_mb_emit_icall (mb
, mono_string_utf8_to_builder2
);
7100 mono_mb_emit_stloc (mb
, conv_arg
);
7104 /* The class can not have an automatic layout */
7105 if ((klass
->flags
& TYPE_ATTRIBUTE_LAYOUT_MASK
) == TYPE_ATTRIBUTE_AUTO_LAYOUT
) {
7106 mono_mb_emit_auto_layout_exception (mb
, klass
);
7110 if (t
->attrs
& PARAM_ATTRIBUTE_OUT
) {
7111 mono_mb_emit_byte (mb
, CEE_LDNULL
);
7112 mono_mb_emit_stloc (mb
, conv_arg
);
7117 mono_mb_emit_ldarg (mb
, argnum
);
7121 /* Check for NULL and raise an exception */
7122 pos2
= mono_mb_emit_branch (mb
, CEE_BRTRUE
);
7124 mono_mb_emit_exception (mb
, "ArgumentNullException", NULL
);
7126 mono_mb_patch_branch (mb
, pos2
);
7127 mono_mb_emit_ldarg (mb
, argnum
);
7128 mono_mb_emit_byte (mb
, CEE_LDIND_I
);
7131 mono_mb_emit_stloc (mb
, 0);
7133 mono_mb_emit_byte (mb
, CEE_LDC_I4_0
);
7134 mono_mb_emit_stloc (mb
, conv_arg
);
7136 mono_mb_emit_ldloc (mb
, 0);
7137 pos
= mono_mb_emit_branch (mb
, CEE_BRFALSE
);
7139 /* Create and set dst */
7140 mono_mb_emit_byte (mb
, MONO_CUSTOM_PREFIX
);
7141 mono_mb_emit_op (mb
, CEE_MONO_NEWOBJ
, klass
);
7142 mono_mb_emit_stloc (mb
, conv_arg
);
7143 mono_mb_emit_ldloc (mb
, conv_arg
);
7144 mono_mb_emit_ldflda (mb
, sizeof (MonoObject
));
7145 mono_mb_emit_stloc (mb
, 1);
7147 /* emit valuetype conversion code */
7148 emit_struct_conv (mb
, klass
, TRUE
);
7150 mono_mb_patch_branch (mb
, pos
);
7153 case MARSHAL_ACTION_MANAGED_CONV_OUT
:
7155 /* Check for null */
7156 mono_mb_emit_ldloc (mb
, conv_arg
);
7157 pos
= mono_mb_emit_branch (mb
, CEE_BRTRUE
);
7158 mono_mb_emit_ldarg (mb
, argnum
);
7159 mono_mb_emit_byte (mb
, CEE_LDC_I4_0
);
7160 mono_mb_emit_byte (mb
, CEE_STIND_REF
);
7161 pos2
= mono_mb_emit_branch (mb
, CEE_BR
);
7163 mono_mb_patch_branch (mb
, pos
);
7166 mono_mb_emit_ldloc (mb
, conv_arg
);
7167 mono_mb_emit_ldflda (mb
, sizeof (MonoObject
));
7168 mono_mb_emit_stloc (mb
, 0);
7170 /* Allocate and set dest */
7171 mono_mb_emit_icon (mb
, mono_class_native_size (klass
, NULL
));
7172 mono_mb_emit_byte (mb
, CEE_CONV_I
);
7173 mono_mb_emit_icall (mb
, mono_marshal_alloc
);
7174 mono_mb_emit_stloc (mb
, 1);
7176 /* Update argument pointer */
7177 mono_mb_emit_ldarg (mb
, argnum
);
7178 mono_mb_emit_ldloc (mb
, 1);
7179 mono_mb_emit_byte (mb
, CEE_STIND_I
);
7181 /* emit valuetype conversion code */
7182 emit_struct_conv (mb
, klass
, FALSE
);
7184 mono_mb_patch_branch (mb
, pos2
);
7186 /* byval [Out] marshalling */
7188 /* FIXME: Handle null */
7191 mono_mb_emit_ldloc (mb
, conv_arg
);
7192 mono_mb_emit_ldflda (mb
, sizeof (MonoObject
));
7193 mono_mb_emit_stloc (mb
, 0);
7196 mono_mb_emit_ldarg (mb
, argnum
);
7197 mono_mb_emit_stloc (mb
, 1);
7199 /* emit valuetype conversion code */
7200 emit_struct_conv (mb
, klass
, FALSE
);
7204 case MARSHAL_ACTION_MANAGED_CONV_RESULT
:
7205 if (klass
->delegate
) {
7206 mono_mb_emit_icall (mb
, conv_to_icall (MONO_MARSHAL_CONV_DEL_FTN
));
7207 mono_mb_emit_stloc (mb
, 3);
7211 /* The class can not have an automatic layout */
7212 if ((klass
->flags
& TYPE_ATTRIBUTE_LAYOUT_MASK
) == TYPE_ATTRIBUTE_AUTO_LAYOUT
) {
7213 mono_mb_emit_auto_layout_exception (mb
, klass
);
7217 mono_mb_emit_stloc (mb
, 0);
7218 /* Check for null */
7219 mono_mb_emit_ldloc (mb
, 0);
7220 pos
= mono_mb_emit_branch (mb
, CEE_BRTRUE
);
7221 mono_mb_emit_byte (mb
, CEE_LDNULL
);
7222 mono_mb_emit_stloc (mb
, 3);
7223 pos2
= mono_mb_emit_branch (mb
, CEE_BR
);
7225 mono_mb_patch_branch (mb
, pos
);
7228 mono_mb_emit_ldloc (mb
, 0);
7229 mono_mb_emit_ldflda (mb
, sizeof (MonoObject
));
7230 mono_mb_emit_stloc (mb
, 0);
7232 /* Allocate and set dest */
7233 mono_mb_emit_icon (mb
, mono_class_native_size (klass
, NULL
));
7234 mono_mb_emit_byte (mb
, CEE_CONV_I
);
7235 mono_mb_emit_icall (mb
, mono_marshal_alloc
);
7236 mono_mb_emit_byte (mb
, CEE_DUP
);
7237 mono_mb_emit_stloc (mb
, 1);
7238 mono_mb_emit_stloc (mb
, 3);
7240 emit_struct_conv (mb
, klass
, FALSE
);
7242 mono_mb_patch_branch (mb
, pos2
);
7246 g_assert_not_reached ();
7257 emit_marshal_variant (EmitMarshalContext
*m
, int argnum
, MonoType
*t
,
7258 MonoMarshalSpec
*spec
,
7259 int conv_arg
, MonoType
**conv_arg_type
,
7260 MarshalAction action
)
7262 MonoMethodBuilder
*mb
= m
->mb
;
7263 static MonoMethod
*get_object_for_native_variant
= NULL
;
7264 static MonoMethod
*get_native_variant_for_object
= NULL
;
7266 mono_init_com_types ();
7268 if (!get_object_for_native_variant
)
7269 get_object_for_native_variant
= mono_class_get_method_from_name (mono_defaults
.marshal_class
, "GetObjectForNativeVariant", 1);
7270 g_assert (get_object_for_native_variant
);
7272 if (!get_native_variant_for_object
)
7273 get_native_variant_for_object
= mono_class_get_method_from_name (mono_defaults
.marshal_class
, "GetNativeVariantForObject", 2);
7274 g_assert (get_native_variant_for_object
);
7277 case MARSHAL_ACTION_CONV_IN
: {
7278 conv_arg
= mono_mb_add_local (mb
, &mono_defaults
.variant_class
->byval_arg
);
7281 *conv_arg_type
= &mono_defaults
.variant_class
->this_arg
;
7283 *conv_arg_type
= &mono_defaults
.variant_class
->byval_arg
;
7285 if (t
->byref
&& !(t
->attrs
& PARAM_ATTRIBUTE_IN
) && t
->attrs
& PARAM_ATTRIBUTE_OUT
)
7288 mono_mb_emit_ldarg (mb
, argnum
);
7290 mono_mb_emit_byte(mb
, CEE_LDIND_REF
);
7291 mono_mb_emit_ldloc_addr (mb
, conv_arg
);
7292 mono_mb_emit_managed_call (mb
, get_native_variant_for_object
, NULL
);
7296 case MARSHAL_ACTION_CONV_OUT
: {
7297 static MonoMethod
*variant_clear
= NULL
;
7300 variant_clear
= mono_class_get_method_from_name (mono_defaults
.variant_class
, "Clear", 0);
7301 g_assert (variant_clear
);
7304 if (t
->byref
&& (t
->attrs
& PARAM_ATTRIBUTE_OUT
|| !(t
->attrs
& PARAM_ATTRIBUTE_IN
))) {
7305 mono_mb_emit_ldarg (mb
, argnum
);
7306 mono_mb_emit_ldloc_addr (mb
, conv_arg
);
7307 mono_mb_emit_managed_call (mb
, get_object_for_native_variant
, NULL
);
7308 mono_mb_emit_byte (mb
, CEE_STIND_REF
);
7311 mono_mb_emit_ldloc_addr (mb
, conv_arg
);
7312 mono_mb_emit_managed_call (mb
, variant_clear
, NULL
);
7316 case MARSHAL_ACTION_PUSH
:
7318 mono_mb_emit_ldloc_addr (mb
, conv_arg
);
7320 mono_mb_emit_ldloc (mb
, conv_arg
);
7323 case MARSHAL_ACTION_CONV_RESULT
: {
7324 char *msg
= g_strdup ("Marshalling of VARIANT not supported as a return type.");
7325 mono_mb_emit_exception_marshal_directive (mb
, msg
);
7329 case MARSHAL_ACTION_MANAGED_CONV_IN
: {
7330 conv_arg
= mono_mb_add_local (mb
, &mono_defaults
.object_class
->byval_arg
);
7333 *conv_arg_type
= &mono_defaults
.variant_class
->this_arg
;
7335 *conv_arg_type
= &mono_defaults
.variant_class
->byval_arg
;
7337 if (t
->byref
&& !(t
->attrs
& PARAM_ATTRIBUTE_IN
) && t
->attrs
& PARAM_ATTRIBUTE_OUT
)
7341 mono_mb_emit_ldarg (mb
, argnum
);
7343 mono_mb_emit_ldarg_addr (mb
, argnum
);
7344 mono_mb_emit_managed_call (mb
, get_object_for_native_variant
, NULL
);
7345 mono_mb_emit_stloc (mb
, conv_arg
);
7349 case MARSHAL_ACTION_MANAGED_CONV_OUT
: {
7350 if (t
->byref
&& (t
->attrs
& PARAM_ATTRIBUTE_OUT
|| !(t
->attrs
& PARAM_ATTRIBUTE_IN
))) {
7351 mono_mb_emit_ldloc (mb
, conv_arg
);
7352 mono_mb_emit_ldarg (mb
, argnum
);
7353 mono_mb_emit_managed_call (mb
, get_native_variant_for_object
, NULL
);
7358 case MARSHAL_ACTION_MANAGED_CONV_RESULT
: {
7359 char *msg
= g_strdup ("Marshalling of VARIANT not supported as a return type.");
7360 mono_mb_emit_exception_marshal_directive (mb
, msg
);
7365 g_assert_not_reached ();
7371 #endif /* DISABLE_COM */
7372 #endif /* DISABLE_JIT */
7375 mono_pinvoke_is_unicode (MonoMethodPInvoke
*piinfo
)
7377 switch (piinfo
->piflags
& PINVOKE_ATTRIBUTE_CHAR_SET_MASK
) {
7378 case PINVOKE_ATTRIBUTE_CHAR_SET_ANSI
:
7380 case PINVOKE_ATTRIBUTE_CHAR_SET_UNICODE
:
7382 case PINVOKE_ATTRIBUTE_CHAR_SET_AUTO
:
7394 emit_marshal_array (EmitMarshalContext
*m
, int argnum
, MonoType
*t
,
7395 MonoMarshalSpec
*spec
,
7396 int conv_arg
, MonoType
**conv_arg_type
,
7397 MarshalAction action
)
7401 case MARSHAL_ACTION_CONV_IN
:
7402 *conv_arg_type
= &mono_defaults
.object_class
->byval_arg
;
7404 case MARSHAL_ACTION_MANAGED_CONV_IN
:
7405 *conv_arg_type
= &mono_defaults
.int_class
->byval_arg
;
7409 MonoMethodBuilder
*mb
= m
->mb
;
7410 MonoClass
*klass
= mono_class_from_mono_type (t
);
7411 gboolean need_convert
, need_free
;
7412 MonoMarshalNative encoding
;
7414 encoding
= mono_marshal_get_string_encoding (m
->piinfo
, spec
);
7417 case MARSHAL_ACTION_CONV_IN
:
7418 *conv_arg_type
= &mono_defaults
.object_class
->byval_arg
;
7419 conv_arg
= mono_mb_add_local (mb
, &mono_defaults
.object_class
->byval_arg
);
7421 if (klass
->element_class
->blittable
) {
7422 mono_mb_emit_ldarg (mb
, argnum
);
7424 mono_mb_emit_byte (mb
, CEE_LDIND_I
);
7425 mono_mb_emit_icall (mb
, conv_to_icall (MONO_MARSHAL_CONV_ARRAY_LPARRAY
));
7426 mono_mb_emit_stloc (mb
, conv_arg
);
7429 guint32 label1
, label2
, label3
;
7430 int index_var
, src_var
, dest_ptr
, esize
;
7431 MonoMarshalConv conv
;
7432 gboolean is_string
= FALSE
;
7434 dest_ptr
= mono_mb_add_local (mb
, &mono_defaults
.int_class
->byval_arg
);
7436 eklass
= klass
->element_class
;
7438 if (eklass
== mono_defaults
.string_class
) {
7440 conv
= mono_marshal_get_string_to_ptr_conv (m
->piinfo
, spec
);
7442 else if (eklass
== mono_defaults
.stringbuilder_class
) {
7444 conv
= mono_marshal_get_stringbuilder_to_ptr_conv (m
->piinfo
, spec
);
7449 if (is_string
&& conv
== -1) {
7450 char *msg
= g_strdup_printf ("string/stringbuilder marshalling conversion %d not implemented", encoding
);
7451 mono_mb_emit_exception_marshal_directive (mb
, msg
);
7455 src_var
= mono_mb_add_local (mb
, &mono_defaults
.object_class
->byval_arg
);
7456 mono_mb_emit_ldarg (mb
, argnum
);
7458 mono_mb_emit_byte (mb
, CEE_LDIND_I
);
7459 mono_mb_emit_stloc (mb
, src_var
);
7462 mono_mb_emit_ldloc (mb
, src_var
);
7463 mono_mb_emit_stloc (mb
, conv_arg
);
7464 mono_mb_emit_ldloc (mb
, src_var
);
7465 label1
= mono_mb_emit_branch (mb
, CEE_BRFALSE
);
7468 esize
= sizeof (gpointer
);
7469 else if (eklass
== mono_defaults
.char_class
) /*can't call mono_marshal_type_size since it causes all sorts of asserts*/
7470 esize
= mono_pinvoke_is_unicode (m
->piinfo
) ? 2 : 1;
7472 esize
= mono_class_native_size (eklass
, NULL
);
7474 /* allocate space for the native struct and store the address */
7475 mono_mb_emit_icon (mb
, esize
);
7476 mono_mb_emit_ldloc (mb
, src_var
);
7477 mono_mb_emit_byte (mb
, CEE_LDLEN
);
7479 if (eklass
== mono_defaults
.string_class
) {
7480 /* Make the array bigger for the terminating null */
7481 mono_mb_emit_byte (mb
, CEE_LDC_I4_1
);
7482 mono_mb_emit_byte (mb
, CEE_ADD
);
7484 mono_mb_emit_byte (mb
, CEE_MUL
);
7485 mono_mb_emit_byte (mb
, CEE_PREFIX1
);
7486 mono_mb_emit_byte (mb
, CEE_LOCALLOC
);
7487 mono_mb_emit_stloc (mb
, conv_arg
);
7489 mono_mb_emit_ldloc (mb
, conv_arg
);
7490 mono_mb_emit_stloc (mb
, dest_ptr
);
7492 /* Emit marshalling loop */
7493 index_var
= mono_mb_add_local (mb
, &mono_defaults
.int_class
->byval_arg
);
7494 mono_mb_emit_byte (mb
, CEE_LDC_I4_0
);
7495 mono_mb_emit_stloc (mb
, index_var
);
7496 label2
= mono_mb_get_label (mb
);
7497 mono_mb_emit_ldloc (mb
, index_var
);
7498 mono_mb_emit_ldloc (mb
, src_var
);
7499 mono_mb_emit_byte (mb
, CEE_LDLEN
);
7500 label3
= mono_mb_emit_branch (mb
, CEE_BGE
);
7502 /* Emit marshalling code */
7505 mono_mb_emit_ldloc (mb
, dest_ptr
);
7506 mono_mb_emit_ldloc (mb
, src_var
);
7507 mono_mb_emit_ldloc (mb
, index_var
);
7508 mono_mb_emit_byte (mb
, CEE_LDELEM_REF
);
7509 mono_mb_emit_icall (mb
, conv_to_icall (conv
));
7510 mono_mb_emit_byte (mb
, CEE_STIND_I
);
7512 /* set the src_ptr */
7513 mono_mb_emit_ldloc (mb
, src_var
);
7514 mono_mb_emit_ldloc (mb
, index_var
);
7515 mono_mb_emit_op (mb
, CEE_LDELEMA
, eklass
);
7516 mono_mb_emit_stloc (mb
, 0);
7519 mono_mb_emit_ldloc (mb
, dest_ptr
);
7520 mono_mb_emit_stloc (mb
, 1);
7522 /* emit valuetype conversion code */
7523 emit_struct_conv_full (mb
, eklass
, FALSE
, eklass
== mono_defaults
.char_class
? encoding
: -1);
7526 mono_mb_emit_add_to_local (mb
, index_var
, 1);
7527 mono_mb_emit_add_to_local (mb
, dest_ptr
, esize
);
7529 mono_mb_emit_branch_label (mb
, CEE_BR
, label2
);
7531 mono_mb_patch_branch (mb
, label3
);
7533 if (eklass
== mono_defaults
.string_class
) {
7534 /* Null terminate */
7535 mono_mb_emit_ldloc (mb
, dest_ptr
);
7536 mono_mb_emit_byte (mb
, CEE_LDC_I4_0
);
7537 mono_mb_emit_byte (mb
, CEE_STIND_REF
);
7540 mono_mb_patch_branch (mb
, label1
);
7545 case MARSHAL_ACTION_CONV_OUT
:
7546 /* Unicode character arrays are implicitly marshalled as [Out] under MS.NET */
7547 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
);
7548 need_free
= mono_marshal_need_free (&klass
->element_class
->byval_arg
,
7551 if (need_convert
|| need_free
) {
7552 /* FIXME: Optimize blittable case */
7554 guint32 label1
, label2
, label3
;
7555 int index_var
, src_ptr
, loc
, esize
;
7557 eklass
= klass
->element_class
;
7558 if ((eklass
== mono_defaults
.stringbuilder_class
) || (eklass
== mono_defaults
.string_class
))
7559 esize
= sizeof (gpointer
);
7560 else if (eklass
== mono_defaults
.char_class
)
7561 esize
= mono_pinvoke_is_unicode (m
->piinfo
) ? 2 : 1;
7563 esize
= mono_class_native_size (eklass
, NULL
);
7564 src_ptr
= mono_mb_add_local (mb
, &mono_defaults
.int_class
->byval_arg
);
7565 loc
= mono_mb_add_local (mb
, &mono_defaults
.int_class
->byval_arg
);
7568 mono_mb_emit_ldarg (mb
, argnum
);
7570 mono_mb_emit_byte (mb
, CEE_LDIND_I
);
7571 label1
= mono_mb_emit_branch (mb
, CEE_BRFALSE
);
7573 mono_mb_emit_ldloc (mb
, conv_arg
);
7574 mono_mb_emit_stloc (mb
, src_ptr
);
7576 /* Emit marshalling loop */
7577 index_var
= mono_mb_add_local (mb
, &mono_defaults
.int_class
->byval_arg
);
7578 mono_mb_emit_byte (mb
, CEE_LDC_I4_0
);
7579 mono_mb_emit_stloc (mb
, index_var
);
7580 label2
= mono_mb_get_label (mb
);
7581 mono_mb_emit_ldloc (mb
, index_var
);
7582 mono_mb_emit_ldarg (mb
, argnum
);
7584 mono_mb_emit_byte (mb
, CEE_LDIND_REF
);
7585 mono_mb_emit_byte (mb
, CEE_LDLEN
);
7586 label3
= mono_mb_emit_branch (mb
, CEE_BGE
);
7588 /* Emit marshalling code */
7590 if (eklass
== mono_defaults
.stringbuilder_class
) {
7591 gboolean need_free2
;
7592 MonoMarshalConv conv
= mono_marshal_get_ptr_to_stringbuilder_conv (m
->piinfo
, spec
, &need_free2
);
7594 g_assert (conv
!= -1);
7597 mono_mb_emit_ldarg (mb
, argnum
);
7599 mono_mb_emit_byte (mb
, CEE_LDIND_I
);
7600 mono_mb_emit_ldloc (mb
, index_var
);
7601 mono_mb_emit_byte (mb
, CEE_LDELEM_REF
);
7604 mono_mb_emit_ldloc (mb
, src_ptr
);
7605 mono_mb_emit_byte (mb
, CEE_LDIND_I
);
7607 mono_mb_emit_icall (mb
, conv_to_icall (conv
));
7611 mono_mb_emit_ldloc (mb
, src_ptr
);
7612 mono_mb_emit_byte (mb
, CEE_LDIND_I
);
7614 mono_mb_emit_icall (mb
, mono_marshal_free
);
7617 else if (eklass
== mono_defaults
.string_class
) {
7620 mono_mb_emit_ldloc (mb
, src_ptr
);
7621 mono_mb_emit_byte (mb
, CEE_LDIND_I
);
7623 mono_mb_emit_icall (mb
, mono_marshal_free
);
7628 /* set the src_ptr */
7629 mono_mb_emit_ldloc (mb
, src_ptr
);
7630 mono_mb_emit_stloc (mb
, 0);
7633 mono_mb_emit_ldarg (mb
, argnum
);
7635 mono_mb_emit_byte (mb
, CEE_LDIND_REF
);
7636 mono_mb_emit_ldloc (mb
, index_var
);
7637 mono_mb_emit_op (mb
, CEE_LDELEMA
, eklass
);
7638 mono_mb_emit_stloc (mb
, 1);
7640 /* emit valuetype conversion code */
7641 emit_struct_conv_full (mb
, eklass
, TRUE
, eklass
== mono_defaults
.char_class
? encoding
: -1);
7645 mono_mb_emit_ldloc (mb
, src_ptr
);
7646 mono_mb_emit_stloc (mb
, loc
);
7647 mono_mb_emit_ldloc (mb
, loc
);
7649 emit_struct_free (mb
, eklass
, loc
);
7653 mono_mb_emit_add_to_local (mb
, index_var
, 1);
7654 mono_mb_emit_add_to_local (mb
, src_ptr
, esize
);
7656 mono_mb_emit_branch_label (mb
, CEE_BR
, label2
);
7658 mono_mb_patch_branch (mb
, label1
);
7659 mono_mb_patch_branch (mb
, label3
);
7662 if (klass
->element_class
->blittable
) {
7663 /* free memory allocated (if any) by MONO_MARSHAL_CONV_ARRAY_LPARRAY */
7665 mono_mb_emit_ldarg (mb
, argnum
);
7666 mono_mb_emit_ldloc (mb
, conv_arg
);
7667 mono_mb_emit_icall (mb
, conv_to_icall (MONO_MARSHAL_FREE_LPARRAY
));
7672 case MARSHAL_ACTION_PUSH
:
7674 mono_mb_emit_ldloc_addr (mb
, conv_arg
);
7676 mono_mb_emit_ldloc (mb
, conv_arg
);
7679 case MARSHAL_ACTION_CONV_RESULT
:
7680 /* fixme: we need conversions here */
7681 mono_mb_emit_stloc (mb
, 3);
7684 case MARSHAL_ACTION_MANAGED_CONV_IN
: {
7686 guint32 label1
, label2
, label3
;
7687 int index_var
, src_ptr
, loc
, esize
, param_num
, num_elem
;
7688 MonoMarshalConv conv
;
7689 gboolean is_string
= FALSE
;
7691 conv_arg
= mono_mb_add_local (mb
, &mono_defaults
.object_class
->byval_arg
);
7692 *conv_arg_type
= &mono_defaults
.int_class
->byval_arg
;
7695 char *msg
= g_strdup ("Byref array marshalling to managed code is not implemented.");
7696 mono_mb_emit_exception_marshal_directive (mb
, msg
);
7700 char *msg
= g_strdup ("[MarshalAs] attribute required to marshal arrays to managed code.");
7701 mono_mb_emit_exception_marshal_directive (mb
, msg
);
7704 if (spec
->native
!= MONO_NATIVE_LPARRAY
) {
7705 char *msg
= g_strdup ("Non LPArray marshalling of arrays to managed code is not implemented.");
7706 mono_mb_emit_exception_marshal_directive (mb
, msg
);
7710 /* FIXME: t is from the method which is wrapped, not the delegate type */
7711 /* g_assert (t->attrs & PARAM_ATTRIBUTE_IN); */
7713 param_num
= spec
->data
.array_data
.param_num
;
7714 num_elem
= spec
->data
.array_data
.num_elem
;
7715 if (spec
->data
.array_data
.elem_mult
== 0)
7716 /* param_num is not specified */
7719 if (param_num
== -1) {
7720 if (num_elem
<= 0) {
7721 char *msg
= g_strdup ("Either SizeConst or SizeParamIndex should be specified when marshalling arrays to managed code.");
7722 mono_mb_emit_exception_marshal_directive (mb
, msg
);
7727 /* FIXME: Optimize blittable case */
7729 eklass
= klass
->element_class
;
7730 if (eklass
== mono_defaults
.string_class
) {
7732 conv
= mono_marshal_get_ptr_to_string_conv (m
->piinfo
, spec
, &need_free
);
7734 else if (eklass
== mono_defaults
.stringbuilder_class
) {
7736 conv
= mono_marshal_get_ptr_to_stringbuilder_conv (m
->piinfo
, spec
, &need_free
);
7741 mono_marshal_load_type_info (eklass
);
7744 esize
= sizeof (gpointer
);
7746 esize
= mono_class_native_size (eklass
, NULL
);
7747 src_ptr
= mono_mb_add_local (mb
, &mono_defaults
.int_class
->byval_arg
);
7748 loc
= mono_mb_add_local (mb
, &mono_defaults
.int_class
->byval_arg
);
7750 mono_mb_emit_byte (mb
, CEE_LDNULL
);
7751 mono_mb_emit_stloc (mb
, conv_arg
);
7753 /* Check param index */
7754 if (param_num
!= -1) {
7755 if (param_num
>= m
->sig
->param_count
) {
7756 char *msg
= g_strdup ("Array size control parameter index is out of range.");
7757 mono_mb_emit_exception_marshal_directive (mb
, msg
);
7760 switch (m
->sig
->params
[param_num
]->type
) {
7773 char *msg
= g_strdup ("Array size control parameter must be an integral type.");
7774 mono_mb_emit_exception_marshal_directive (mb
, msg
);
7781 mono_mb_emit_ldarg (mb
, argnum
);
7782 label1
= mono_mb_emit_branch (mb
, CEE_BRFALSE
);
7784 mono_mb_emit_ldarg (mb
, argnum
);
7785 mono_mb_emit_stloc (mb
, src_ptr
);
7787 /* Create managed array */
7789 * The LPArray marshalling spec says that sometimes param_num starts
7790 * from 1, sometimes it starts from 0. But MS seems to allways start
7794 if (param_num
== -1) {
7795 mono_mb_emit_icon (mb
, num_elem
);
7797 mono_mb_emit_ldarg (mb
, param_num
);
7799 mono_mb_emit_icon (mb
, num_elem
);
7800 mono_mb_emit_byte (mb
, CEE_ADD
);
7802 mono_mb_emit_byte (mb
, CEE_CONV_OVF_I
);
7805 mono_mb_emit_op (mb
, CEE_NEWARR
, eklass
);
7806 mono_mb_emit_stloc (mb
, conv_arg
);
7808 if (eklass
->blittable
) {
7809 mono_mb_emit_ldloc (mb
, conv_arg
);
7810 mono_mb_emit_byte (mb
, CEE_CONV_I
);
7811 mono_mb_emit_icon (mb
, G_STRUCT_OFFSET (MonoArray
, vector
));
7812 mono_mb_emit_byte (mb
, CEE_ADD
);
7813 mono_mb_emit_ldarg (mb
, argnum
);
7814 mono_mb_emit_ldloc (mb
, conv_arg
);
7815 mono_mb_emit_byte (mb
, CEE_LDLEN
);
7816 mono_mb_emit_icon (mb
, esize
);
7817 mono_mb_emit_byte (mb
, CEE_MUL
);
7818 mono_mb_emit_byte (mb
, CEE_PREFIX1
);
7819 mono_mb_emit_byte (mb
, CEE_CPBLK
);
7823 /* Emit marshalling loop */
7824 index_var
= mono_mb_add_local (mb
, &mono_defaults
.int_class
->byval_arg
);
7825 mono_mb_emit_byte (mb
, CEE_LDC_I4_0
);
7826 mono_mb_emit_stloc (mb
, index_var
);
7827 label2
= mono_mb_get_label (mb
);
7828 mono_mb_emit_ldloc (mb
, index_var
);
7829 mono_mb_emit_ldloc (mb
, conv_arg
);
7830 mono_mb_emit_byte (mb
, CEE_LDLEN
);
7831 label3
= mono_mb_emit_branch (mb
, CEE_BGE
);
7833 /* Emit marshalling code */
7835 g_assert (conv
!= -1);
7837 mono_mb_emit_ldloc (mb
, conv_arg
);
7838 mono_mb_emit_ldloc (mb
, index_var
);
7840 mono_mb_emit_ldloc (mb
, src_ptr
);
7841 mono_mb_emit_byte (mb
, CEE_LDIND_I
);
7843 mono_mb_emit_icall (mb
, conv_to_icall (conv
));
7844 mono_mb_emit_byte (mb
, CEE_STELEM_REF
);
7847 char *msg
= g_strdup ("Marshalling of non-string and non-blittable arrays to managed code is not implemented.");
7848 mono_mb_emit_exception_marshal_directive (mb
, msg
);
7852 mono_mb_emit_add_to_local (mb
, index_var
, 1);
7853 mono_mb_emit_add_to_local (mb
, src_ptr
, esize
);
7855 mono_mb_emit_branch_label (mb
, CEE_BR
, label2
);
7857 mono_mb_patch_branch (mb
, label1
);
7858 mono_mb_patch_branch (mb
, label3
);
7862 case MARSHAL_ACTION_MANAGED_CONV_OUT
: {
7864 guint32 label1
, label2
, label3
;
7865 int index_var
, dest_ptr
, loc
, esize
, param_num
, num_elem
;
7866 MonoMarshalConv conv
;
7867 gboolean is_string
= FALSE
;
7870 /* Already handled in CONV_IN */
7873 /* These are already checked in CONV_IN */
7874 g_assert (!t
->byref
);
7875 g_assert (spec
->native
== MONO_NATIVE_LPARRAY
);
7876 g_assert (t
->attrs
& PARAM_ATTRIBUTE_OUT
);
7878 param_num
= spec
->data
.array_data
.param_num
;
7879 num_elem
= spec
->data
.array_data
.num_elem
;
7881 if (spec
->data
.array_data
.elem_mult
== 0)
7882 /* param_num is not specified */
7885 if (param_num
== -1) {
7886 if (num_elem
<= 0) {
7887 g_assert_not_reached ();
7891 /* FIXME: Optimize blittable case */
7893 eklass
= klass
->element_class
;
7894 if (eklass
== mono_defaults
.string_class
) {
7896 conv
= mono_marshal_get_string_to_ptr_conv (m
->piinfo
, spec
);
7898 else if (eklass
== mono_defaults
.stringbuilder_class
) {
7900 conv
= mono_marshal_get_stringbuilder_to_ptr_conv (m
->piinfo
, spec
);
7905 mono_marshal_load_type_info (eklass
);
7908 esize
= sizeof (gpointer
);
7910 esize
= mono_class_native_size (eklass
, NULL
);
7912 dest_ptr
= mono_mb_add_local (mb
, &mono_defaults
.int_class
->byval_arg
);
7913 loc
= mono_mb_add_local (mb
, &mono_defaults
.int_class
->byval_arg
);
7916 mono_mb_emit_ldloc (mb
, conv_arg
);
7917 label1
= mono_mb_emit_branch (mb
, CEE_BRFALSE
);
7919 mono_mb_emit_ldarg (mb
, argnum
);
7920 mono_mb_emit_stloc (mb
, dest_ptr
);
7922 if (eklass
->blittable
) {
7924 mono_mb_emit_ldarg (mb
, argnum
);
7926 mono_mb_emit_ldloc (mb
, conv_arg
);
7927 mono_mb_emit_byte (mb
, CEE_CONV_I
);
7928 mono_mb_emit_icon (mb
, G_STRUCT_OFFSET (MonoArray
, vector
));
7929 mono_mb_emit_byte (mb
, CEE_ADD
);
7931 mono_mb_emit_ldloc (mb
, conv_arg
);
7932 mono_mb_emit_byte (mb
, CEE_LDLEN
);
7933 mono_mb_emit_icon (mb
, esize
);
7934 mono_mb_emit_byte (mb
, CEE_MUL
);
7935 mono_mb_emit_byte (mb
, CEE_PREFIX1
);
7936 mono_mb_emit_byte (mb
, CEE_CPBLK
);
7940 /* Emit marshalling loop */
7941 index_var
= mono_mb_add_local (mb
, &mono_defaults
.int_class
->byval_arg
);
7942 mono_mb_emit_byte (mb
, CEE_LDC_I4_0
);
7943 mono_mb_emit_stloc (mb
, index_var
);
7944 label2
= mono_mb_get_label (mb
);
7945 mono_mb_emit_ldloc (mb
, index_var
);
7946 mono_mb_emit_ldloc (mb
, conv_arg
);
7947 mono_mb_emit_byte (mb
, CEE_LDLEN
);
7948 label3
= mono_mb_emit_branch (mb
, CEE_BGE
);
7950 /* Emit marshalling code */
7952 g_assert (conv
!= -1);
7955 mono_mb_emit_ldloc (mb
, dest_ptr
);
7958 mono_mb_emit_ldloc (mb
, conv_arg
);
7959 mono_mb_emit_ldloc (mb
, index_var
);
7961 mono_mb_emit_byte (mb
, CEE_LDELEM_REF
);
7963 mono_mb_emit_icall (mb
, conv_to_icall (conv
));
7964 mono_mb_emit_byte (mb
, CEE_STIND_I
);
7967 char *msg
= g_strdup ("Marshalling of non-string and non-blittable arrays to managed code is not implemented.");
7968 mono_mb_emit_exception_marshal_directive (mb
, msg
);
7972 mono_mb_emit_add_to_local (mb
, index_var
, 1);
7973 mono_mb_emit_add_to_local (mb
, dest_ptr
, esize
);
7975 mono_mb_emit_branch_label (mb
, CEE_BR
, label2
);
7977 mono_mb_patch_branch (mb
, label1
);
7978 mono_mb_patch_branch (mb
, label3
);
7982 case MARSHAL_ACTION_MANAGED_CONV_RESULT
: {
7984 guint32 label1
, label2
, label3
;
7985 int index_var
, src
, dest
, esize
;
7986 MonoMarshalConv conv
= -1;
7987 gboolean is_string
= FALSE
;
7989 g_assert (!t
->byref
);
7991 eklass
= klass
->element_class
;
7993 mono_marshal_load_type_info (eklass
);
7995 if (eklass
== mono_defaults
.string_class
) {
7997 conv
= mono_marshal_get_string_to_ptr_conv (m
->piinfo
, spec
);
8000 g_assert_not_reached ();
8004 esize
= sizeof (gpointer
);
8005 else if (eklass
== mono_defaults
.char_class
)
8006 esize
= mono_pinvoke_is_unicode (m
->piinfo
) ? 2 : 1;
8008 esize
= mono_class_native_size (eklass
, NULL
);
8010 src
= mono_mb_add_local (mb
, &mono_defaults
.object_class
->byval_arg
);
8011 dest
= mono_mb_add_local (mb
, &mono_defaults
.int_class
->byval_arg
);
8013 mono_mb_emit_stloc (mb
, src
);
8014 mono_mb_emit_ldloc (mb
, src
);
8015 mono_mb_emit_stloc (mb
, 3);
8017 /* Check for null */
8018 mono_mb_emit_ldloc (mb
, src
);
8019 label1
= mono_mb_emit_branch (mb
, CEE_BRFALSE
);
8021 /* Allocate native array */
8022 mono_mb_emit_icon (mb
, esize
);
8023 mono_mb_emit_ldloc (mb
, src
);
8024 mono_mb_emit_byte (mb
, CEE_LDLEN
);
8026 if (eklass
== mono_defaults
.string_class
) {
8027 /* Make the array bigger for the terminating null */
8028 mono_mb_emit_byte (mb
, CEE_LDC_I4_1
);
8029 mono_mb_emit_byte (mb
, CEE_ADD
);
8031 mono_mb_emit_byte (mb
, CEE_MUL
);
8032 mono_mb_emit_icall (mb
, mono_marshal_alloc
);
8033 mono_mb_emit_stloc (mb
, dest
);
8034 mono_mb_emit_ldloc (mb
, dest
);
8035 mono_mb_emit_stloc (mb
, 3);
8037 /* Emit marshalling loop */
8038 index_var
= mono_mb_add_local (mb
, &mono_defaults
.int_class
->byval_arg
);
8039 mono_mb_emit_byte (mb
, CEE_LDC_I4_0
);
8040 mono_mb_emit_stloc (mb
, index_var
);
8041 label2
= mono_mb_get_label (mb
);
8042 mono_mb_emit_ldloc (mb
, index_var
);
8043 mono_mb_emit_ldloc (mb
, src
);
8044 mono_mb_emit_byte (mb
, CEE_LDLEN
);
8045 label3
= mono_mb_emit_branch (mb
, CEE_BGE
);
8047 /* Emit marshalling code */
8049 g_assert (conv
!= -1);
8052 mono_mb_emit_ldloc (mb
, dest
);
8055 mono_mb_emit_ldloc (mb
, src
);
8056 mono_mb_emit_ldloc (mb
, index_var
);
8058 mono_mb_emit_byte (mb
, CEE_LDELEM_REF
);
8060 mono_mb_emit_icall (mb
, conv_to_icall (conv
));
8061 mono_mb_emit_byte (mb
, CEE_STIND_I
);
8064 char *msg
= g_strdup ("Marshalling of non-string arrays to managed code is not implemented.");
8065 mono_mb_emit_exception_marshal_directive (mb
, msg
);
8069 mono_mb_emit_add_to_local (mb
, index_var
, 1);
8070 mono_mb_emit_add_to_local (mb
, dest
, esize
);
8072 mono_mb_emit_branch_label (mb
, CEE_BR
, label2
);
8074 mono_mb_patch_branch (mb
, label3
);
8075 mono_mb_patch_branch (mb
, label1
);
8079 g_assert_not_reached ();
8086 marshal_boolean_conv_in_get_local_type (MonoMarshalSpec
*spec
, guint8
*ldc_op
/*out*/)
8089 return &mono_defaults
.int32_class
->byval_arg
;
8091 switch (spec
->native
) {
8092 case MONO_NATIVE_I1
:
8093 case MONO_NATIVE_U1
:
8094 return &mono_defaults
.byte_class
->byval_arg
;
8095 case MONO_NATIVE_VARIANTBOOL
:
8096 if (ldc_op
) *ldc_op
= CEE_LDC_I4_M1
;
8097 return &mono_defaults
.int16_class
->byval_arg
;
8098 case MONO_NATIVE_BOOLEAN
:
8099 return &mono_defaults
.int32_class
->byval_arg
;
8101 g_warning ("marshalling bool as native type %x is currently not supported", spec
->native
);
8102 return &mono_defaults
.int32_class
->byval_arg
;
8108 marshal_boolean_managed_conv_in_get_conv_arg_class (MonoMarshalSpec
*spec
, guint8
*ldop
/*out*/)
8110 MonoClass
* conv_arg_class
= mono_defaults
.int32_class
;
8112 switch (spec
->native
) {
8113 case MONO_NATIVE_I1
:
8114 case MONO_NATIVE_U1
:
8115 conv_arg_class
= mono_defaults
.byte_class
;
8116 if (ldop
) *ldop
= CEE_LDIND_I1
;
8118 case MONO_NATIVE_VARIANTBOOL
:
8119 conv_arg_class
= mono_defaults
.int16_class
;
8120 if (ldop
) *ldop
= CEE_LDIND_I2
;
8122 case MONO_NATIVE_BOOLEAN
:
8125 g_warning ("marshalling bool as native type %x is currently not supported", spec
->native
);
8128 return conv_arg_class
;
8132 emit_marshal_boolean (EmitMarshalContext
*m
, int argnum
, MonoType
*t
,
8133 MonoMarshalSpec
*spec
,
8134 int conv_arg
, MonoType
**conv_arg_type
,
8135 MarshalAction action
)
8139 case MARSHAL_ACTION_CONV_IN
:
8141 *conv_arg_type
= &mono_defaults
.int_class
->byval_arg
;
8143 *conv_arg_type
= marshal_boolean_conv_in_get_local_type (spec
, NULL
);
8146 case MARSHAL_ACTION_MANAGED_CONV_IN
: {
8147 MonoClass
* conv_arg_class
= marshal_boolean_managed_conv_in_get_conv_arg_class (spec
, NULL
);
8149 *conv_arg_type
= &conv_arg_class
->this_arg
;
8151 *conv_arg_type
= &conv_arg_class
->byval_arg
;
8157 MonoMethodBuilder
*mb
= m
->mb
;
8160 case MARSHAL_ACTION_CONV_IN
: {
8161 MonoType
*local_type
;
8163 guint8 ldc_op
= CEE_LDC_I4_1
;
8165 local_type
= marshal_boolean_conv_in_get_local_type (spec
, &ldc_op
);
8167 *conv_arg_type
= &mono_defaults
.int_class
->byval_arg
;
8169 *conv_arg_type
= local_type
;
8170 conv_arg
= mono_mb_add_local (mb
, local_type
);
8172 mono_mb_emit_ldarg (mb
, argnum
);
8174 mono_mb_emit_byte (mb
, CEE_LDIND_I1
);
8175 label_false
= mono_mb_emit_branch (mb
, CEE_BRFALSE
);
8176 mono_mb_emit_byte (mb
, ldc_op
);
8177 mono_mb_emit_stloc (mb
, conv_arg
);
8178 mono_mb_patch_branch (mb
, label_false
);
8183 case MARSHAL_ACTION_CONV_OUT
:
8185 int label_false
, label_end
;
8189 mono_mb_emit_ldarg (mb
, argnum
);
8190 mono_mb_emit_ldloc (mb
, conv_arg
);
8192 label_false
= mono_mb_emit_branch (mb
, CEE_BRFALSE
);
8193 mono_mb_emit_byte (mb
, CEE_LDC_I4_1
);
8195 label_end
= mono_mb_emit_branch (mb
, CEE_BR
);
8196 mono_mb_patch_branch (mb
, label_false
);
8197 mono_mb_emit_byte (mb
, CEE_LDC_I4_0
);
8198 mono_mb_patch_branch (mb
, label_end
);
8200 mono_mb_emit_byte (mb
, CEE_STIND_I1
);
8204 case MARSHAL_ACTION_PUSH
:
8206 mono_mb_emit_ldloc_addr (mb
, conv_arg
);
8208 mono_mb_emit_ldloc (mb
, conv_arg
);
8210 mono_mb_emit_ldarg (mb
, argnum
);
8213 case MARSHAL_ACTION_CONV_RESULT
:
8214 /* maybe we need to make sure that it fits within 8 bits */
8215 mono_mb_emit_stloc (mb
, 3);
8218 case MARSHAL_ACTION_MANAGED_CONV_IN
: {
8219 MonoClass
* conv_arg_class
= mono_defaults
.int32_class
;
8220 guint8 ldop
= CEE_LDIND_I4
;
8221 int label_null
, label_false
;
8223 conv_arg_class
= marshal_boolean_managed_conv_in_get_conv_arg_class (spec
, &ldop
);
8224 conv_arg
= mono_mb_add_local (mb
, &mono_defaults
.boolean_class
->byval_arg
);
8227 *conv_arg_type
= &conv_arg_class
->this_arg
;
8229 *conv_arg_type
= &conv_arg_class
->byval_arg
;
8232 mono_mb_emit_ldarg (mb
, argnum
);
8236 label_null
= mono_mb_emit_branch (mb
, CEE_BRFALSE
);
8237 mono_mb_emit_ldarg (mb
, argnum
);
8238 mono_mb_emit_byte (mb
, ldop
);
8242 label_false
= mono_mb_emit_branch (mb
, CEE_BRFALSE
);
8243 mono_mb_emit_byte (mb
, CEE_LDC_I4_1
);
8244 mono_mb_emit_stloc (mb
, conv_arg
);
8245 mono_mb_patch_branch (mb
, label_false
);
8248 mono_mb_patch_branch (mb
, label_null
);
8252 case MARSHAL_ACTION_MANAGED_CONV_OUT
: {
8253 guint8 stop
= CEE_STIND_I4
;
8254 guint8 ldc_op
= CEE_LDC_I4_1
;
8255 int label_null
,label_false
, label_end
;;
8260 switch (spec
->native
) {
8261 case MONO_NATIVE_I1
:
8262 case MONO_NATIVE_U1
:
8263 stop
= CEE_STIND_I1
;
8265 case MONO_NATIVE_VARIANTBOOL
:
8266 stop
= CEE_STIND_I2
;
8267 ldc_op
= CEE_LDC_I4_M1
;
8275 mono_mb_emit_ldarg (mb
, argnum
);
8276 label_null
= mono_mb_emit_branch (mb
, CEE_BRFALSE
);
8278 mono_mb_emit_ldarg (mb
, argnum
);
8279 mono_mb_emit_ldloc (mb
, conv_arg
);
8281 label_false
= mono_mb_emit_branch (mb
, CEE_BRFALSE
);
8282 mono_mb_emit_byte (mb
, ldc_op
);
8283 label_end
= mono_mb_emit_branch (mb
, CEE_BR
);
8285 mono_mb_patch_branch (mb
, label_false
);
8286 mono_mb_emit_byte (mb
, CEE_LDC_I4_0
);
8287 mono_mb_patch_branch (mb
, label_end
);
8289 mono_mb_emit_byte (mb
, stop
);
8290 mono_mb_patch_branch (mb
, label_null
);
8295 g_assert_not_reached ();
8302 emit_marshal_ptr (EmitMarshalContext
*m
, int argnum
, MonoType
*t
,
8303 MonoMarshalSpec
*spec
, int conv_arg
,
8304 MonoType
**conv_arg_type
, MarshalAction action
)
8307 MonoMethodBuilder
*mb
= m
->mb
;
8310 case MARSHAL_ACTION_CONV_IN
:
8311 /* MS seems to allow this in some cases, ie. bxc #158 */
8313 if (MONO_TYPE_ISSTRUCT (t->data.type) && !mono_class_from_mono_type (t->data.type)->blittable) {
8314 char *msg = g_strdup_printf ("Can not marshal 'parameter #%d': Pointers can not reference marshaled structures. Use byref instead.", argnum + 1);
8315 mono_mb_emit_exception_marshal_directive (m->mb, msg);
8320 case MARSHAL_ACTION_PUSH
:
8321 mono_mb_emit_ldarg (mb
, argnum
);
8324 case MARSHAL_ACTION_CONV_RESULT
:
8325 /* no conversions necessary */
8326 mono_mb_emit_stloc (mb
, 3);
8337 emit_marshal_char (EmitMarshalContext
*m
, int argnum
, MonoType
*t
,
8338 MonoMarshalSpec
*spec
, int conv_arg
,
8339 MonoType
**conv_arg_type
, MarshalAction action
)
8342 MonoMethodBuilder
*mb
= m
->mb
;
8345 case MARSHAL_ACTION_PUSH
:
8346 /* fixme: dont know how to marshal that. We cant simply
8347 * convert it to a one byte UTF8 character, because an
8348 * unicode character may need more that one byte in UTF8 */
8349 mono_mb_emit_ldarg (mb
, argnum
);
8352 case MARSHAL_ACTION_CONV_RESULT
:
8353 /* fixme: we need conversions here */
8354 mono_mb_emit_stloc (mb
, 3);
8365 emit_marshal_scalar (EmitMarshalContext
*m
, int argnum
, MonoType
*t
,
8366 MonoMarshalSpec
*spec
, int conv_arg
,
8367 MonoType
**conv_arg_type
, MarshalAction action
)
8370 MonoMethodBuilder
*mb
= m
->mb
;
8373 case MARSHAL_ACTION_PUSH
:
8374 mono_mb_emit_ldarg (mb
, argnum
);
8377 case MARSHAL_ACTION_CONV_RESULT
:
8378 /* no conversions necessary */
8379 mono_mb_emit_stloc (mb
, 3);
8390 emit_marshal (EmitMarshalContext
*m
, int argnum
, MonoType
*t
,
8391 MonoMarshalSpec
*spec
, int conv_arg
,
8392 MonoType
**conv_arg_type
, MarshalAction action
)
8394 /* Ensure that we have marshalling info for this param */
8395 mono_marshal_load_type_info (mono_class_from_mono_type (t
));
8397 if (spec
&& spec
->native
== MONO_NATIVE_CUSTOM
)
8398 return emit_marshal_custom (m
, argnum
, t
, spec
, conv_arg
, conv_arg_type
, action
);
8400 if (spec
&& spec
->native
== MONO_NATIVE_ASANY
)
8401 return emit_marshal_asany (m
, argnum
, t
, spec
, conv_arg
, conv_arg_type
, action
);
8404 case MONO_TYPE_VALUETYPE
:
8405 if (t
->data
.klass
== mono_defaults
.handleref_class
)
8406 return emit_marshal_handleref (m
, argnum
, t
, spec
, conv_arg
, conv_arg_type
, action
);
8408 return emit_marshal_vtype (m
, argnum
, t
, spec
, conv_arg
, conv_arg_type
, action
);
8409 case MONO_TYPE_STRING
:
8410 return emit_marshal_string (m
, argnum
, t
, spec
, conv_arg
, conv_arg_type
, action
);
8411 case MONO_TYPE_CLASS
:
8412 case MONO_TYPE_OBJECT
:
8414 if (spec
&& spec
->native
== MONO_NATIVE_STRUCT
)
8415 return emit_marshal_variant (m
, argnum
, t
, spec
, conv_arg
, conv_arg_type
, action
);
8417 if (spec
&& (spec
->native
== MONO_NATIVE_IUNKNOWN
||
8418 spec
->native
== MONO_NATIVE_IDISPATCH
||
8419 spec
->native
== MONO_NATIVE_INTERFACE
))
8420 return mono_cominterop_emit_marshal_com_interface (m
, argnum
, t
, spec
, conv_arg
, conv_arg_type
, action
);
8421 if (spec
&& (spec
->native
== MONO_NATIVE_SAFEARRAY
) &&
8422 (spec
->data
.safearray_data
.elem_type
== MONO_VARIANT_VARIANT
) &&
8423 ((action
== MARSHAL_ACTION_CONV_OUT
) || (action
== MARSHAL_ACTION_CONV_IN
) || (action
== MARSHAL_ACTION_PUSH
)))
8424 return mono_cominterop_emit_marshal_safearray (m
, argnum
, t
, spec
, conv_arg
, conv_arg_type
, action
);
8427 if (mono_defaults
.safehandle_class
!= NULL
&& t
->data
.klass
&&
8428 mono_class_is_subclass_of (t
->data
.klass
, mono_defaults
.safehandle_class
, FALSE
))
8429 return emit_marshal_safehandle (m
, argnum
, t
, spec
, conv_arg
, conv_arg_type
, action
);
8431 return emit_marshal_object (m
, argnum
, t
, spec
, conv_arg
, conv_arg_type
, action
);
8432 case MONO_TYPE_ARRAY
:
8433 case MONO_TYPE_SZARRAY
:
8434 return emit_marshal_array (m
, argnum
, t
, spec
, conv_arg
, conv_arg_type
, action
);
8435 case MONO_TYPE_BOOLEAN
:
8436 return emit_marshal_boolean (m
, argnum
, t
, spec
, conv_arg
, conv_arg_type
, action
);
8438 return emit_marshal_ptr (m
, argnum
, t
, spec
, conv_arg
, conv_arg_type
, action
);
8439 case MONO_TYPE_CHAR
:
8440 return emit_marshal_char (m
, argnum
, t
, spec
, conv_arg
, conv_arg_type
, action
);
8453 case MONO_TYPE_FNPTR
:
8454 return emit_marshal_scalar (m
, argnum
, t
, spec
, conv_arg
, conv_arg_type
, action
);
8455 case MONO_TYPE_GENERICINST
:
8456 if (mono_type_generic_inst_is_valuetype (t
))
8457 return emit_marshal_vtype (m
, argnum
, t
, spec
, conv_arg
, conv_arg_type
, action
);
8459 return emit_marshal_object (m
, argnum
, t
, spec
, conv_arg
, conv_arg_type
, action
);
8466 * mono_marshal_emit_native_wrapper:
8467 * @image: the image to use for looking up custom marshallers
8468 * @sig: The signature of the native function
8469 * @piinfo: Marshalling information
8470 * @mspecs: Marshalling information
8471 * @aot: whenever the created method will be compiled by the AOT compiler
8472 * @method: if non-NULL, the pinvoke method to call
8473 * @check_exceptions: Whenever to check for pending exceptions after the native call
8474 * @func_param: the function to call is passed as a boxed IntPtr as the first parameter
8476 * generates IL code for the pinvoke wrapper, the generated code calls @func.
8479 mono_marshal_emit_native_wrapper (MonoImage
*image
, MonoMethodBuilder
*mb
, MonoMethodSignature
*sig
, MonoMethodPInvoke
*piinfo
, MonoMarshalSpec
**mspecs
, gpointer func
, gboolean aot
, gboolean check_exceptions
, gboolean func_param
)
8481 EmitMarshalContext m
;
8482 MonoMethodSignature
*csig
;
8484 int i
, argnum
, *tmp_locals
;
8485 int type
, param_shift
= 0;
8486 static MonoMethodSignature
*get_last_error_sig
= NULL
;
8491 /* we copy the signature, so that we can set pinvoke to 0 */
8493 /* The function address is passed as the first argument */
8494 g_assert (!sig
->hasthis
);
8497 csig
= signature_dup (mb
->method
->klass
->image
, sig
);
8505 /* we allocate local for use with emit_struct_conv() */
8506 /* allocate local 0 (pointer) src_ptr */
8507 mono_mb_add_local (mb
, &mono_defaults
.int_class
->byval_arg
);
8508 /* allocate local 1 (pointer) dst_ptr */
8509 mono_mb_add_local (mb
, &mono_defaults
.int_class
->byval_arg
);
8510 /* allocate local 2 (boolean) delete_old */
8511 mono_mb_add_local (mb
, &mono_defaults
.boolean_class
->byval_arg
);
8513 /* delete_old = FALSE */
8514 mono_mb_emit_icon (mb
, 0);
8515 mono_mb_emit_stloc (mb
, 2);
8517 if (!MONO_TYPE_IS_VOID(sig
->ret
)) {
8518 /* allocate local 3 to store the return value */
8519 mono_mb_add_local (mb
, sig
->ret
);
8522 if (mspecs
[0] && mspecs
[0]->native
== MONO_NATIVE_CUSTOM
) {
8523 /* Return type custom marshaling */
8525 * Since we can't determine the return type of the unmanaged function,
8526 * we assume it returns a pointer, and pass that pointer to
8527 * MarshalNativeToManaged.
8529 csig
->ret
= &mono_defaults
.int_class
->byval_arg
;
8532 /* we first do all conversions */
8533 tmp_locals
= alloca (sizeof (int) * sig
->param_count
);
8534 m
.orig_conv_args
= alloca (sizeof (int) * (sig
->param_count
+ 1));
8536 for (i
= 0; i
< sig
->param_count
; i
++) {
8537 tmp_locals
[i
] = emit_marshal (&m
, i
+ param_shift
, sig
->params
[i
], mspecs
[i
+ 1], 0, &csig
->params
[i
], MARSHAL_ACTION_CONV_IN
);
8540 /* push all arguments */
8543 mono_mb_emit_byte (mb
, CEE_LDARG_0
);
8545 for (i
= 0; i
< sig
->param_count
; i
++) {
8546 emit_marshal (&m
, i
+ param_shift
, sig
->params
[i
], mspecs
[i
+ 1], tmp_locals
[i
], NULL
, MARSHAL_ACTION_PUSH
);
8549 /* call the native method */
8551 mono_mb_emit_byte (mb
, CEE_LDARG_0
);
8552 mono_mb_emit_op (mb
, CEE_UNBOX
, mono_defaults
.int_class
);
8553 mono_mb_emit_byte (mb
, CEE_LDIND_I
);
8554 mono_mb_emit_calli (mb
, csig
);
8555 } else if (MONO_CLASS_IS_IMPORT (mb
->method
->klass
)) {
8557 mono_mb_emit_cominterop_call (mb
, csig
, &piinfo
->method
);
8559 g_assert_not_reached ();
8564 /* Reuse the ICALL_ADDR opcode for pinvokes too */
8565 mono_mb_emit_byte (mb
, MONO_CUSTOM_PREFIX
);
8566 mono_mb_emit_op (mb
, CEE_MONO_ICALL_ADDR
, &piinfo
->method
);
8567 mono_mb_emit_calli (mb
, csig
);
8569 mono_mb_emit_native_call (mb
, csig
, func
);
8573 /* Set LastError if needed */
8574 if (piinfo
->piflags
& PINVOKE_ATTRIBUTE_SUPPORTS_LAST_ERROR
) {
8575 if (!get_last_error_sig
) {
8576 get_last_error_sig
= mono_metadata_signature_alloc (mono_defaults
.corlib
, 0);
8577 get_last_error_sig
->ret
= &mono_defaults
.int_class
->byval_arg
;
8578 get_last_error_sig
->pinvoke
= 1;
8583 * Have to call GetLastError () early and without a wrapper, since various runtime components could
8584 * clobber its value.
8586 mono_mb_emit_native_call (mb
, get_last_error_sig
, GetLastError
);
8587 mono_mb_emit_icall (mb
, mono_marshal_set_last_error_windows
);
8589 mono_mb_emit_icall (mb
, mono_marshal_set_last_error
);
8593 /* convert the result */
8594 if (!sig
->ret
->byref
) {
8595 MonoMarshalSpec
*spec
= mspecs
[0];
8596 type
= sig
->ret
->type
;
8598 if (spec
&& spec
->native
== MONO_NATIVE_CUSTOM
) {
8599 emit_marshal (&m
, 0, sig
->ret
, spec
, 0, NULL
, MARSHAL_ACTION_CONV_RESULT
);
8604 case MONO_TYPE_VOID
:
8606 case MONO_TYPE_VALUETYPE
:
8607 klass
= sig
->ret
->data
.klass
;
8608 if (klass
->enumtype
) {
8609 type
= mono_class_enum_basetype (sig
->ret
->data
.klass
)->type
;
8612 emit_marshal (&m
, 0, sig
->ret
, spec
, 0, NULL
, MARSHAL_ACTION_CONV_RESULT
);
8626 case MONO_TYPE_FNPTR
:
8627 case MONO_TYPE_STRING
:
8628 case MONO_TYPE_CLASS
:
8629 case MONO_TYPE_OBJECT
:
8630 case MONO_TYPE_BOOLEAN
:
8631 case MONO_TYPE_ARRAY
:
8632 case MONO_TYPE_SZARRAY
:
8633 case MONO_TYPE_CHAR
:
8635 case MONO_TYPE_GENERICINST
:
8636 emit_marshal (&m
, 0, sig
->ret
, spec
, 0, NULL
, MARSHAL_ACTION_CONV_RESULT
);
8638 case MONO_TYPE_TYPEDBYREF
:
8640 g_warning ("return type 0x%02x unknown", sig
->ret
->type
);
8641 g_assert_not_reached ();
8645 mono_mb_emit_stloc (mb
, 3);
8649 * Need to call this after converting the result since MONO_VTADDR needs
8650 * to be adjacent to the call instruction.
8652 if (check_exceptions
)
8653 emit_thread_interrupt_checkpoint (mb
);
8655 /* we need to convert byref arguments back and free string arrays */
8656 for (i
= 0; i
< sig
->param_count
; i
++) {
8657 MonoType
*t
= sig
->params
[i
];
8658 MonoMarshalSpec
*spec
= mspecs
[i
+ 1];
8660 argnum
= i
+ param_shift
;
8662 if (spec
&& ((spec
->native
== MONO_NATIVE_CUSTOM
) || (spec
->native
== MONO_NATIVE_ASANY
))) {
8663 emit_marshal (&m
, argnum
, t
, spec
, tmp_locals
[i
], NULL
, MARSHAL_ACTION_CONV_OUT
);
8668 case MONO_TYPE_STRING
:
8669 case MONO_TYPE_VALUETYPE
:
8670 case MONO_TYPE_CLASS
:
8671 case MONO_TYPE_OBJECT
:
8672 case MONO_TYPE_SZARRAY
:
8673 case MONO_TYPE_BOOLEAN
:
8674 emit_marshal (&m
, argnum
, t
, spec
, tmp_locals
[i
], NULL
, MARSHAL_ACTION_CONV_OUT
);
8679 if (!MONO_TYPE_IS_VOID(sig
->ret
))
8680 mono_mb_emit_ldloc (mb
, 3);
8682 mono_mb_emit_byte (mb
, CEE_RET
);
8684 #endif /* DISABLE_JIT */
8687 G_GNUC_UNUSED
static void
8688 code_for (MonoMethod
*method
) {
8689 MonoMethodHeader
*header
= mono_method_get_header (method
);
8690 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
));
8694 * mono_marshal_get_native_wrapper:
8695 * @method: The MonoMethod to wrap.
8696 * @check_exceptions: Whenever to check for pending exceptions
8698 * generates IL code for the pinvoke wrapper (the generated method
8699 * calls the unmanaged code in piinfo->addr)
8700 * The wrapper info for the wrapper is a WrapperInfo structure.
8703 mono_marshal_get_native_wrapper (MonoMethod
*method
, gboolean check_exceptions
, gboolean aot
)
8705 MonoMethodSignature
*sig
, *csig
;
8706 MonoMethodPInvoke
*piinfo
= (MonoMethodPInvoke
*) method
;
8707 MonoMethodBuilder
*mb
;
8708 MonoMarshalSpec
**mspecs
;
8711 gboolean pinvoke
= FALSE
;
8714 const char *exc_class
= "MissingMethodException";
8715 const char *exc_arg
= NULL
;
8718 g_assert (method
!= NULL
);
8719 g_assert (mono_method_signature (method
)->pinvoke
);
8722 cache
= get_cache (&method
->klass
->image
->native_wrapper_aot_cache
, mono_aligned_addr_hash
, NULL
);
8724 cache
= get_cache (&method
->klass
->image
->native_wrapper_cache
, mono_aligned_addr_hash
, NULL
);
8725 if ((res
= mono_marshal_find_in_cache (cache
, method
)))
8728 if (MONO_CLASS_IS_IMPORT (method
->klass
)) {
8730 return mono_cominterop_get_native_wrapper (method
);
8732 g_assert_not_reached ();
8736 sig
= mono_method_signature (method
);
8738 if (!(method
->iflags
& METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL
) &&
8739 (method
->flags
& METHOD_ATTRIBUTE_PINVOKE_IMPL
))
8742 if (!piinfo
->addr
) {
8744 if (method
->iflags
& METHOD_IMPL_ATTRIBUTE_NATIVE
)
8745 exc_arg
= "Method contains unsupported native code";
8747 mono_lookup_pinvoke_call (method
, &exc_class
, &exc_arg
);
8749 piinfo
->addr
= mono_lookup_internal_call (method
);
8752 /* hack - redirect certain string constructors to CreateString */
8753 if (piinfo
->addr
== ves_icall_System_String_ctor_RedirectToCreateString
) {
8754 g_assert (!pinvoke
);
8755 g_assert (method
->string_ctor
);
8756 g_assert (sig
->hasthis
);
8758 /* CreateString returns a value */
8759 csig
= signature_dup (method
->klass
->image
, sig
);
8760 csig
->ret
= &mono_defaults
.string_class
->byval_arg
;
8764 while ((res
= mono_class_get_methods (mono_defaults
.string_class
, &iter
))) {
8765 if (!strcmp ("CreateString", res
->name
) &&
8766 mono_metadata_signature_equal (csig
, mono_method_signature (res
))) {
8769 g_assert (!(res
->iflags
& METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL
));
8770 g_assert (!(res
->flags
& METHOD_ATTRIBUTE_PINVOKE_IMPL
));
8772 /* create a wrapper to preserve .ctor in stack trace */
8773 mb
= mono_mb_new (method
->klass
, method
->name
, MONO_WRAPPER_MANAGED_TO_MANAGED
);
8776 mono_mb_emit_byte (mb
, CEE_LDARG_0
);
8777 for (i
= 1; i
<= csig
->param_count
; i
++)
8778 mono_mb_emit_ldarg (mb
, i
);
8779 mono_mb_emit_managed_call (mb
, res
, NULL
);
8780 mono_mb_emit_byte (mb
, CEE_RET
);
8783 /* use native_wrapper_cache because internal calls are looked up there */
8784 res
= mono_mb_create_and_cache (cache
, method
,
8785 mb
, csig
, csig
->param_count
+ 1);
8789 info
= mono_wrapper_info_create (res
, WRAPPER_SUBTYPE_STRING_CTOR
);
8790 info
->d
.string_ctor
.method
= method
;
8792 mono_marshal_set_wrapper_info (res
, info
);
8798 /* exception will be thrown */
8799 piinfo
->addr
= NULL
;
8800 g_warning ("cannot find CreateString for .ctor");
8803 mb
= mono_mb_new (method
->klass
, method
->name
, MONO_WRAPPER_MANAGED_TO_NATIVE
);
8805 mb
->method
->save_lmf
= 1;
8808 * In AOT mode and embedding scenarios, it is possible that the icall is not
8809 * registered in the runtime doing the AOT compilation.
8811 if (!piinfo
->addr
&& !aot
) {
8813 mono_mb_emit_exception (mb
, exc_class
, exc_arg
);
8815 csig
= signature_dup (method
->klass
->image
, sig
);
8817 res
= mono_mb_create_and_cache (cache
, method
,
8818 mb
, csig
, csig
->param_count
+ 16);
8821 info
= mono_wrapper_info_create (res
, WRAPPER_SUBTYPE_NONE
);
8822 info
->d
.managed_to_native
.method
= method
;
8823 mono_marshal_set_wrapper_info (res
, info
);
8828 /* internal calls: we simply push all arguments and call the method (no conversions) */
8829 if (method
->iflags
& (METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL
| METHOD_IMPL_ATTRIBUTE_RUNTIME
)) {
8831 csig
= signature_dup_add_this (method
->klass
->image
, sig
, method
->klass
);
8833 csig
= signature_dup (method
->klass
->image
, sig
);
8835 /* hack - string constructors returns a value */
8836 if (method
->string_ctor
)
8837 csig
->ret
= &mono_defaults
.string_class
->byval_arg
;
8844 * Add a null check since public icalls can be called with 'call' which
8845 * does no such check.
8847 mono_mb_emit_byte (mb
, CEE_LDARG_0
);
8848 pos
= mono_mb_emit_branch (mb
, CEE_BRTRUE
);
8849 mono_mb_emit_exception (mb
, "NullReferenceException", NULL
);
8850 mono_mb_patch_branch (mb
, pos
);
8852 mono_mb_emit_byte (mb
, CEE_LDARG_0
);
8855 for (i
= 0; i
< sig
->param_count
; i
++)
8856 mono_mb_emit_ldarg (mb
, i
+ sig
->hasthis
);
8859 mono_mb_emit_byte (mb
, MONO_CUSTOM_PREFIX
);
8860 mono_mb_emit_op (mb
, CEE_MONO_ICALL_ADDR
, &piinfo
->method
);
8861 mono_mb_emit_calli (mb
, csig
);
8863 g_assert (piinfo
->addr
);
8864 mono_mb_emit_native_call (mb
, csig
, piinfo
->addr
);
8866 if (check_exceptions
)
8867 emit_thread_interrupt_checkpoint (mb
);
8868 mono_mb_emit_byte (mb
, CEE_RET
);
8871 csig
= signature_dup (method
->klass
->image
, csig
);
8873 res
= mono_mb_create_and_cache (cache
, method
,
8874 mb
, csig
, csig
->param_count
+ 16);
8876 info
= mono_wrapper_info_create (res
, WRAPPER_SUBTYPE_NONE
);
8877 info
->d
.managed_to_native
.method
= method
;
8878 mono_marshal_set_wrapper_info (res
, info
);
8886 g_assert (piinfo
->addr
);
8889 mspecs
= g_new (MonoMarshalSpec
*, sig
->param_count
+ 1);
8890 mono_method_get_marshal_info (method
, mspecs
);
8892 mono_marshal_emit_native_wrapper (mb
->method
->klass
->image
, mb
, sig
, piinfo
, mspecs
, piinfo
->addr
, aot
, check_exceptions
, FALSE
);
8895 csig
= signature_dup (method
->klass
->image
, sig
);
8897 res
= mono_mb_create_and_cache (cache
, method
,
8898 mb
, csig
, csig
->param_count
+ 16);
8901 info
= mono_wrapper_info_create (res
, WRAPPER_SUBTYPE_NONE
);
8902 info
->d
.managed_to_native
.method
= method
;
8903 mono_marshal_set_wrapper_info (res
, info
);
8906 for (i
= sig
->param_count
; i
>= 0; i
--)
8908 mono_metadata_free_marshal_spec (mspecs
[i
]);
8912 /* code_for (res); */
8918 * mono_marshal_get_native_func_wrapper:
8919 * @image: The image to use for memory allocation and for looking up custom marshallers.
8920 * @sig: The signature of the function
8921 * @func: The native function to wrap
8923 * Returns a wrapper method around native functions, similar to the pinvoke
8927 mono_marshal_get_native_func_wrapper (MonoImage
*image
, MonoMethodSignature
*sig
,
8928 MonoMethodPInvoke
*piinfo
, MonoMarshalSpec
**mspecs
, gpointer func
)
8930 MonoMethodSignature
*csig
;
8932 MonoMethodBuilder
*mb
;
8937 cache
= get_cache (&image
->native_wrapper_cache
, mono_aligned_addr_hash
, NULL
);
8938 if ((res
= mono_marshal_find_in_cache (cache
, func
)))
8941 name
= g_strdup_printf ("wrapper_native_%p", func
);
8942 mb
= mono_mb_new (mono_defaults
.object_class
, name
, MONO_WRAPPER_MANAGED_TO_NATIVE
);
8943 mb
->method
->save_lmf
= 1;
8946 mono_marshal_emit_native_wrapper (image
, mb
, sig
, piinfo
, mspecs
, func
, FALSE
, TRUE
, FALSE
);
8949 csig
= signature_dup (image
, sig
);
8951 res
= mono_mb_create_and_cache (cache
, func
,
8952 mb
, csig
, csig
->param_count
+ 16);
8955 mono_marshal_set_wrapper_info (res
, NULL
);
8961 * The wrapper receives the native function as a boxed IntPtr as its 'this' argument. This is easier to support in
8965 mono_marshal_get_native_func_wrapper_aot (MonoClass
*klass
)
8967 MonoMethodSignature
*sig
, *csig
;
8968 MonoMethodBuilder
*mb
;
8973 MonoMethodPInvoke mpiinfo
;
8974 MonoMethodPInvoke
*piinfo
= &mpiinfo
;
8975 MonoMarshalSpec
**mspecs
;
8976 MonoMethod
*invoke
= mono_get_delegate_invoke (klass
);
8977 MonoImage
*image
= invoke
->klass
->image
;
8980 // FIXME: include UnmanagedFunctionPointerAttribute info
8983 * The wrapper is associated with the delegate type, to pick up the marshalling info etc.
8985 cache
= get_cache (&image
->native_func_wrapper_aot_cache
, mono_aligned_addr_hash
, NULL
);
8986 if ((res
= mono_marshal_find_in_cache (cache
, invoke
)))
8989 memset (&mpiinfo
, 0, sizeof (mpiinfo
));
8990 parse_unmanaged_function_pointer_attr (klass
, &mpiinfo
);
8992 mspecs
= g_new0 (MonoMarshalSpec
*, mono_method_signature (invoke
)->param_count
+ 1);
8993 mono_method_get_marshal_info (invoke
, mspecs
);
8994 /* Freed below so don't alloc from mempool */
8995 sig
= mono_metadata_signature_dup (mono_method_signature (invoke
));
8998 name
= g_strdup_printf ("wrapper_aot_native");
8999 mb
= mono_mb_new (invoke
->klass
, name
, MONO_WRAPPER_MANAGED_TO_NATIVE
);
9000 mb
->method
->save_lmf
= 1;
9003 mono_marshal_emit_native_wrapper (image
, mb
, sig
, piinfo
, mspecs
, NULL
, FALSE
, TRUE
, TRUE
);
9006 g_assert (!sig
->hasthis
);
9007 csig
= signature_dup_add_this (image
, sig
, mono_defaults
.int_class
);
9009 res
= mono_mb_create_and_cache (cache
, invoke
,
9010 mb
, csig
, csig
->param_count
+ 16);
9013 info
= mono_wrapper_info_create (res
, WRAPPER_SUBTYPE_NATIVE_FUNC_AOT
);
9014 info
->d
.managed_to_native
.method
= invoke
;
9016 mono_marshal_set_wrapper_info (res
, info
);
9018 for (i
= mono_method_signature (invoke
)->param_count
; i
>= 0; i
--)
9020 mono_metadata_free_marshal_spec (mspecs
[i
]);
9028 * mono_marshal_emit_managed_wrapper:
9030 * Emit the body of a native-to-managed wrapper. INVOKE_SIG is the signature of
9031 * the delegate which wraps the managed method to be called. For closed delegates,
9032 * it could have fewer parameters than the method it wraps.
9033 * THIS_LOC is the memory location where the target of the delegate is stored.
9036 mono_marshal_emit_managed_wrapper (MonoMethodBuilder
*mb
, MonoMethodSignature
*invoke_sig
, MonoMarshalSpec
**mspecs
, EmitMarshalContext
* m
, MonoMethod
*method
, uint32_t target_handle
)
9039 MonoMethodSignature
*sig
, *csig
;
9045 /* we first do all conversions */
9046 for (i
= 0; i
< sig
->param_count
; i
++) {
9047 MonoType
*t
= sig
->params
[i
];
9050 case MONO_TYPE_OBJECT
:
9051 case MONO_TYPE_CLASS
:
9052 case MONO_TYPE_VALUETYPE
:
9053 case MONO_TYPE_ARRAY
:
9054 case MONO_TYPE_SZARRAY
:
9055 case MONO_TYPE_STRING
:
9056 case MONO_TYPE_BOOLEAN
:
9057 emit_marshal (m
, i
, sig
->params
[i
], mspecs
[i
+ 1], 0, &csig
->params
[i
], MARSHAL_ACTION_MANAGED_CONV_IN
);
9061 MonoMethodSignature
*sig
, *csig
;
9063 gboolean closed
= FALSE
;
9068 /* allocate local 0 (pointer) src_ptr */
9069 mono_mb_add_local (mb
, &mono_defaults
.int_class
->byval_arg
);
9070 /* allocate local 1 (pointer) dst_ptr */
9071 mono_mb_add_local (mb
, &mono_defaults
.int_class
->byval_arg
);
9072 /* allocate local 2 (boolean) delete_old */
9073 mono_mb_add_local (mb
, &mono_defaults
.boolean_class
->byval_arg
);
9075 if (!sig
->hasthis
&& sig
->param_count
!= invoke_sig
->param_count
) {
9076 /* Closed delegate */
9077 g_assert (sig
->param_count
== invoke_sig
->param_count
+ 1);
9079 /* Use a new signature without the first argument */
9080 sig
= mono_metadata_signature_dup (sig
);
9081 memmove (&sig
->params
[0], &sig
->params
[1], (sig
->param_count
- 1) * sizeof (MonoType
*));
9082 sig
->param_count
--;
9085 if (!MONO_TYPE_IS_VOID(sig
->ret
)) {
9086 /* allocate local 3 to store the return value */
9087 mono_mb_add_local (mb
, sig
->ret
);
9090 mono_mb_emit_icon (mb
, 0);
9091 mono_mb_emit_stloc (mb
, 2);
9094 * Might need to attach the thread to the JIT or change the
9095 * domain for the callback.
9097 mono_mb_emit_byte (mb
, MONO_CUSTOM_PREFIX
);
9098 mono_mb_emit_byte (mb
, CEE_MONO_JIT_ATTACH
);
9100 /* we first do all conversions */
9101 tmp_locals
= alloca (sizeof (int) * sig
->param_count
);
9102 for (i
= 0; i
< sig
->param_count
; i
++) {
9103 MonoType
*t
= sig
->params
[i
];
9106 case MONO_TYPE_OBJECT
:
9107 case MONO_TYPE_CLASS
:
9108 case MONO_TYPE_VALUETYPE
:
9109 case MONO_TYPE_ARRAY
:
9110 case MONO_TYPE_SZARRAY
:
9111 case MONO_TYPE_STRING
:
9112 case MONO_TYPE_BOOLEAN
:
9113 tmp_locals
[i
] = emit_marshal (m
, i
, sig
->params
[i
], mspecs
[i
+ 1], 0, &csig
->params
[i
], MARSHAL_ACTION_MANAGED_CONV_IN
);
9122 emit_thread_interrupt_checkpoint (mb
);
9125 if (target_handle
) {
9126 mono_mb_emit_icon (mb
, (gint32
)target_handle
);
9127 mono_mb_emit_icall (mb
, mono_gchandle_get_target
);
9130 g_assert_not_reached ();
9132 } else if (closed
) {
9133 mono_mb_emit_icon (mb
, (gint32
)target_handle
);
9134 mono_mb_emit_icall (mb
, mono_gchandle_get_target
);
9137 for (i
= 0; i
< sig
->param_count
; i
++) {
9138 MonoType
*t
= sig
->params
[i
];
9140 if (tmp_locals
[i
]) {
9142 mono_mb_emit_ldloc_addr (mb
, tmp_locals
[i
]);
9144 mono_mb_emit_ldloc (mb
, tmp_locals
[i
]);
9147 mono_mb_emit_ldarg (mb
, i
);
9150 mono_mb_emit_managed_call (mb
, method
, NULL
);
9152 if (mspecs
[0] && mspecs
[0]->native
== MONO_NATIVE_CUSTOM
) {
9153 emit_marshal (m
, 0, sig
->ret
, mspecs
[0], 0, NULL
, MARSHAL_ACTION_MANAGED_CONV_RESULT
);
9154 } else if (!sig
->ret
->byref
) {
9155 switch (sig
->ret
->type
) {
9156 case MONO_TYPE_VOID
:
9158 case MONO_TYPE_BOOLEAN
:
9161 case MONO_TYPE_CHAR
:
9173 case MONO_TYPE_OBJECT
:
9174 mono_mb_emit_stloc (mb
, 3);
9176 case MONO_TYPE_STRING
:
9177 csig
->ret
= &mono_defaults
.int_class
->byval_arg
;
9178 emit_marshal (m
, 0, sig
->ret
, mspecs
[0], 0, NULL
, MARSHAL_ACTION_MANAGED_CONV_RESULT
);
9180 case MONO_TYPE_VALUETYPE
:
9181 case MONO_TYPE_CLASS
:
9182 case MONO_TYPE_SZARRAY
:
9183 emit_marshal (m
, 0, sig
->ret
, mspecs
[0], 0, NULL
, MARSHAL_ACTION_MANAGED_CONV_RESULT
);
9186 g_warning ("return type 0x%02x unknown", sig
->ret
->type
);
9187 g_assert_not_reached ();
9190 mono_mb_emit_stloc (mb
, 3);
9193 /* Convert byref arguments back */
9194 for (i
= 0; i
< sig
->param_count
; i
++) {
9195 MonoType
*t
= sig
->params
[i
];
9196 MonoMarshalSpec
*spec
= mspecs
[i
+ 1];
9198 if (spec
&& spec
->native
== MONO_NATIVE_CUSTOM
) {
9199 emit_marshal (m
, i
, t
, mspecs
[i
+ 1], tmp_locals
[i
], NULL
, MARSHAL_ACTION_MANAGED_CONV_OUT
);
9201 else if (t
->byref
) {
9203 case MONO_TYPE_CLASS
:
9204 case MONO_TYPE_VALUETYPE
:
9205 case MONO_TYPE_OBJECT
:
9206 case MONO_TYPE_STRING
:
9207 case MONO_TYPE_BOOLEAN
:
9208 emit_marshal (m
, i
, t
, mspecs
[i
+ 1], tmp_locals
[i
], NULL
, MARSHAL_ACTION_MANAGED_CONV_OUT
);
9212 else if (invoke_sig
->params
[i
]->attrs
& PARAM_ATTRIBUTE_OUT
) {
9213 /* The [Out] information is encoded in the delegate signature */
9215 case MONO_TYPE_SZARRAY
:
9216 case MONO_TYPE_CLASS
:
9217 case MONO_TYPE_VALUETYPE
:
9218 emit_marshal (m
, i
, invoke_sig
->params
[i
], mspecs
[i
+ 1], tmp_locals
[i
], NULL
, MARSHAL_ACTION_MANAGED_CONV_OUT
);
9221 g_assert_not_reached ();
9226 mono_mb_emit_byte (mb
, MONO_CUSTOM_PREFIX
);
9227 mono_mb_emit_byte (mb
, CEE_MONO_JIT_DETACH
);
9229 if (m
->retobj_var
) {
9230 mono_mb_emit_ldloc (mb
, m
->retobj_var
);
9231 mono_mb_emit_byte (mb
, MONO_CUSTOM_PREFIX
);
9232 mono_mb_emit_op (mb
, CEE_MONO_RETOBJ
, m
->retobj_class
);
9235 if (!MONO_TYPE_IS_VOID(sig
->ret
))
9236 mono_mb_emit_ldloc (mb
, 3);
9237 mono_mb_emit_byte (mb
, CEE_RET
);
9246 mono_marshal_set_callconv_from_modopt (MonoMethod
*method
, MonoMethodSignature
*csig
)
9248 MonoMethodSignature
*sig
;
9253 * Under windows, delegates passed to native code must use the STDCALL
9254 * calling convention.
9256 csig
->call_convention
= MONO_CALL_STDCALL
;
9259 sig
= mono_method_signature (method
);
9261 /* Change default calling convention if needed */
9262 /* Why is this a modopt ? */
9263 if (sig
->ret
&& sig
->ret
->num_mods
) {
9264 for (i
= 0; i
< sig
->ret
->num_mods
; ++i
) {
9265 MonoClass
*cmod_class
= mono_class_get (method
->klass
->image
, sig
->ret
->modifiers
[i
].token
);
9266 g_assert (cmod_class
);
9267 if ((cmod_class
->image
== mono_defaults
.corlib
) && !strcmp (cmod_class
->name_space
, "System.Runtime.CompilerServices")) {
9268 if (!strcmp (cmod_class
->name
, "CallConvCdecl"))
9269 csig
->call_convention
= MONO_CALL_C
;
9270 else if (!strcmp (cmod_class
->name
, "CallConvStdcall"))
9271 csig
->call_convention
= MONO_CALL_STDCALL
;
9272 else if (!strcmp (cmod_class
->name
, "CallConvFastcall"))
9273 csig
->call_convention
= MONO_CALL_FASTCALL
;
9274 else if (!strcmp (cmod_class
->name
, "CallConvThiscall"))
9275 csig
->call_convention
= MONO_CALL_THISCALL
;
9282 * generates IL code to call managed methods from unmanaged code
9283 * If target_handle==0, the wrapper info will be a WrapperInfo structure.
9286 mono_marshal_get_managed_wrapper (MonoMethod
*method
, MonoClass
*delegate_klass
, uint32_t target_handle
)
9288 static MonoClass
*UnmanagedFunctionPointerAttribute
;
9289 MonoMethodSignature
*sig
, *csig
, *invoke_sig
;
9290 MonoMethodBuilder
*mb
;
9291 MonoMethod
*res
, *invoke
;
9292 MonoMarshalSpec
**mspecs
;
9293 MonoMethodPInvoke piinfo
;
9296 EmitMarshalContext m
;
9298 g_assert (method
!= NULL
);
9299 g_assert (!mono_method_signature (method
)->pinvoke
);
9302 * FIXME: Should cache the method+delegate type pair, since the same method
9303 * could be called with different delegates, thus different marshalling
9306 cache
= get_cache (&method
->klass
->image
->managed_wrapper_cache
, mono_aligned_addr_hash
, NULL
);
9307 if (!target_handle
&& (res
= mono_marshal_find_in_cache (cache
, method
)))
9310 invoke
= mono_get_delegate_invoke (delegate_klass
);
9311 invoke_sig
= mono_method_signature (invoke
);
9313 mspecs
= g_new0 (MonoMarshalSpec
*, mono_method_signature (invoke
)->param_count
+ 1);
9314 mono_method_get_marshal_info (invoke
, mspecs
);
9316 sig
= mono_method_signature (method
);
9318 mb
= mono_mb_new (method
->klass
, method
->name
, MONO_WRAPPER_NATIVE_TO_MANAGED
);
9320 /*the target gchandle must be the first entry after size and the wrapper itself.*/
9321 mono_mb_add_data (mb
, GUINT_TO_POINTER (target_handle
));
9323 /* we copy the signature, so that we can modify it */
9325 /* Need to free this later */
9326 csig
= mono_metadata_signature_dup (invoke_sig
);
9328 csig
= signature_dup (method
->klass
->image
, invoke_sig
);
9337 m
.image
= method
->klass
->image
;
9339 mono_marshal_set_callconv_from_modopt (invoke
, csig
);
9341 /* Handle the UnmanagedFunctionPointerAttribute */
9342 if (!UnmanagedFunctionPointerAttribute
)
9343 UnmanagedFunctionPointerAttribute
= mono_class_from_name (mono_defaults
.corlib
, "System.Runtime.InteropServices", "UnmanagedFunctionPointerAttribute");
9345 /* The attribute is only available in Net 2.0 */
9346 if (UnmanagedFunctionPointerAttribute
) {
9347 MonoCustomAttrInfo
*cinfo
;
9348 MonoCustomAttrEntry
*attr
;
9351 * The pinvoke attributes are stored in a real custom attribute. Obtain the
9352 * contents of the attribute without constructing it, as that might not be
9353 * possible when running in cross-compiling mode.
9355 cinfo
= mono_custom_attrs_from_class (delegate_klass
);
9358 for (i
= 0; i
< cinfo
->num_attrs
; ++i
) {
9359 MonoClass
*ctor_class
= cinfo
->attrs
[i
].ctor
->klass
;
9360 if (mono_class_has_parent (ctor_class
, UnmanagedFunctionPointerAttribute
)) {
9361 attr
= &cinfo
->attrs
[i
];
9367 MonoArray
*typed_args
, *named_args
;
9368 CattrNamedArg
*arginfo
;
9372 MonoBoolean set_last_error
= 0;
9373 MonoBoolean best_fit_mapping
= 0;
9374 MonoBoolean throw_on_unmappable
= 0;
9376 mono_reflection_create_custom_attr_data_args (mono_defaults
.corlib
, attr
->ctor
, attr
->data
, attr
->data_size
, &typed_args
, &named_args
, &arginfo
);
9378 g_assert (mono_array_length (typed_args
) == 1);
9381 o
= mono_array_get (typed_args
, MonoObject
*, 0);
9382 call_conv
= *(gint32
*)mono_object_unbox (o
);
9385 for (i
= 0; i
< mono_array_length (named_args
); ++i
) {
9386 CattrNamedArg
*narg
= &arginfo
[i
];
9388 o
= mono_array_get (named_args
, MonoObject
*, i
);
9390 g_assert (narg
->field
);
9391 if (!strcmp (narg
->field
->name
, "CharSet")) {
9392 charset
= *(gint32
*)mono_object_unbox (o
);
9393 } else if (!strcmp (narg
->field
->name
, "SetLastError")) {
9394 set_last_error
= *(MonoBoolean
*)mono_object_unbox (o
);
9395 } else if (!strcmp (narg
->field
->name
, "BestFitMapping")) {
9396 best_fit_mapping
= *(MonoBoolean
*)mono_object_unbox (o
);
9397 } else if (!strcmp (narg
->field
->name
, "ThrowOnUnmappableChar")) {
9398 throw_on_unmappable
= *(MonoBoolean
*)mono_object_unbox (o
);
9400 g_assert_not_reached ();
9406 memset (&piinfo
, 0, sizeof (piinfo
));
9408 piinfo
.piflags
= (call_conv
<< 8) | (charset
? (charset
- 1) * 2 : 1) | set_last_error
;
9410 csig
->call_convention
= call_conv
- 1;
9413 if (cinfo
&& !cinfo
->cached
)
9414 mono_custom_attrs_free (cinfo
);
9417 mono_marshal_emit_managed_wrapper (mb
, invoke_sig
, mspecs
, &m
, method
, target_handle
);
9419 if (!target_handle
) {
9422 res
= mono_mb_create_and_cache (cache
, method
,
9423 mb
, csig
, sig
->param_count
+ 16);
9424 // FIXME: Associate it with the method+delegate_klass pair
9425 info
= mono_wrapper_info_create (res
, WRAPPER_SUBTYPE_NONE
);
9426 info
->d
.native_to_managed
.method
= method
;
9427 info
->d
.native_to_managed
.klass
= delegate_klass
;
9428 mono_marshal_set_wrapper_info (res
, info
);
9433 res
= mono_mb_create_method (mb
, csig
, sig
->param_count
+ 16);
9437 for (i
= mono_method_signature (invoke
)->param_count
; i
>= 0; i
--)
9439 mono_metadata_free_marshal_spec (mspecs
[i
]);
9442 /* code_for (res); */
9448 mono_marshal_get_vtfixup_ftnptr (MonoImage
*image
, guint32 token
, guint16 type
)
9451 MonoMethodSignature
*sig
;
9452 MonoMethodBuilder
*mb
;
9457 method
= mono_get_method (image
, token
, NULL
);
9460 if (type
& (VTFIXUP_TYPE_FROM_UNMANAGED
| VTFIXUP_TYPE_FROM_UNMANAGED_RETAIN_APPDOMAIN
)) {
9461 MonoMethodSignature
*csig
;
9462 MonoMarshalSpec
**mspecs
;
9463 EmitMarshalContext m
;
9465 sig
= mono_method_signature (method
);
9466 g_assert (!sig
->hasthis
);
9468 mspecs
= g_new0 (MonoMarshalSpec
*, sig
->param_count
+ 1);
9469 mono_method_get_marshal_info (method
, mspecs
);
9471 mb
= mono_mb_new (method
->klass
, method
->name
, MONO_WRAPPER_NATIVE_TO_MANAGED
);
9472 csig
= signature_dup (image
, sig
);
9483 mono_marshal_set_callconv_from_modopt (method
, csig
);
9485 /* FIXME: Implement VTFIXUP_TYPE_FROM_UNMANAGED_RETAIN_APPDOMAIN. */
9487 mono_marshal_emit_managed_wrapper (mb
, sig
, mspecs
, &m
, method
, 0);
9492 method
= mono_mb_create_method (mb
, csig
, sig
->param_count
+ 16);
9495 for (i
= sig
->param_count
; i
>= 0; i
--)
9497 mono_metadata_free_marshal_spec (mspecs
[i
]);
9500 return mono_compile_method (method
);
9503 sig
= mono_method_signature (method
);
9504 mb
= mono_mb_new (method
->klass
, method
->name
, MONO_WRAPPER_MANAGED_TO_MANAGED
);
9507 param_count
= sig
->param_count
+ sig
->hasthis
;
9508 for (i
= 0; i
< param_count
; i
++)
9509 mono_mb_emit_ldarg (mb
, i
);
9511 if (type
& VTFIXUP_TYPE_CALL_MOST_DERIVED
)
9512 mono_mb_emit_op (mb
, CEE_CALLVIRT
, method
);
9514 mono_mb_emit_op (mb
, CEE_CALL
, method
);
9515 mono_mb_emit_byte (mb
, CEE_RET
);
9520 method
= mono_mb_create_method (mb
, sig
, param_count
);
9523 return mono_compile_method (method
);
9526 static MonoReflectionType
*
9527 type_from_handle (MonoType
*handle
)
9529 MonoDomain
*domain
= mono_domain_get ();
9530 MonoClass
*klass
= mono_class_from_mono_type (handle
);
9532 MONO_ARCH_SAVE_REGS
;
9534 mono_class_init (klass
);
9535 return mono_type_get_object (domain
, handle
);
9539 * This does the equivalent of mono_object_castclass_with_cache.
9540 * The wrapper info for the wrapper is a WrapperInfo structure.
9543 mono_marshal_get_castclass_with_cache (void)
9545 static MonoMethod
*cached
;
9547 MonoMethodBuilder
*mb
;
9548 MonoMethodSignature
*sig
;
9549 int return_null_pos
, cache_miss_pos
, invalid_cast_pos
;
9555 mb
= mono_mb_new (mono_defaults
.object_class
, "__castclass_with_cache", MONO_WRAPPER_CASTCLASS
);
9556 sig
= mono_metadata_signature_alloc (mono_defaults
.corlib
, 3);
9557 sig
->params
[0] = &mono_defaults
.object_class
->byval_arg
;
9558 sig
->params
[1] = &mono_defaults
.int_class
->byval_arg
;
9559 sig
->params
[2] = &mono_defaults
.int_class
->byval_arg
;
9560 sig
->ret
= &mono_defaults
.object_class
->byval_arg
;
9564 /* allocate local 0 (pointer) obj_vtable */
9565 mono_mb_add_local (mb
, &mono_defaults
.int_class
->byval_arg
);
9568 mono_mb_emit_ldarg (mb
, 0);
9569 return_null_pos
= mono_mb_emit_branch (mb
, CEE_BRFALSE
);
9571 /*obj_vtable = obj->vtable;*/
9572 mono_mb_emit_ldarg (mb
, 0);
9573 mono_mb_emit_ldflda (mb
, G_STRUCT_OFFSET (MonoObject
, vtable
));
9574 mono_mb_emit_byte (mb
, CEE_LDIND_I
);
9575 mono_mb_emit_stloc (mb
, 0);
9578 mono_mb_emit_ldarg (mb
, 2);
9579 mono_mb_emit_byte (mb
, CEE_LDIND_I
);
9580 mono_mb_emit_ldloc (mb
, 0);
9582 /*if (*cache == obj_vtable)*/
9583 cache_miss_pos
= mono_mb_emit_branch (mb
, CEE_BNE_UN
);
9586 mono_mb_emit_ldarg (mb
, 0);
9587 mono_mb_emit_byte (mb
, CEE_RET
);
9589 mono_mb_patch_branch (mb
, cache_miss_pos
);
9590 /*if (mono_object_isinst (obj, klass)) */
9591 mono_mb_emit_ldarg (mb
, 0);
9592 mono_mb_emit_ldarg (mb
, 1);
9593 mono_mb_emit_icall (mb
, mono_object_isinst
);
9594 invalid_cast_pos
= mono_mb_emit_branch (mb
, CEE_BRFALSE
);
9596 /**cache = obj_vtable;*/
9597 mono_mb_emit_ldarg (mb
, 2);
9598 mono_mb_emit_ldloc (mb
, 0);
9599 mono_mb_emit_byte (mb
, CEE_STIND_I
);
9602 mono_mb_emit_ldarg (mb
, 0);
9603 mono_mb_emit_byte (mb
, CEE_RET
);
9606 mono_mb_patch_branch (mb
, invalid_cast_pos
);
9607 mono_mb_emit_exception (mb
, "InvalidCastException", NULL
);
9610 mono_mb_patch_branch (mb
, return_null_pos
);
9611 mono_mb_emit_byte (mb
, CEE_LDNULL
);
9612 mono_mb_emit_byte (mb
, CEE_RET
);
9613 #endif /* DISABLE_JIT */
9615 res
= mono_mb_create_method (mb
, sig
, 8);
9616 info
= mono_wrapper_info_create (res
, WRAPPER_SUBTYPE_CASTCLASS_WITH_CACHE
);
9617 mono_marshal_set_wrapper_info (res
, info
);
9620 if (InterlockedCompareExchangePointer ((volatile gpointer
*)&cached
, res
, NULL
)) {
9621 mono_free_method (res
);
9622 mono_metadata_free_method_signature (sig
);
9630 * This does the equivalent of mono_object_isinst_with_cache.
9631 * The wrapper info for the wrapper is a WrapperInfo structure.
9634 mono_marshal_get_isinst_with_cache (void)
9636 static MonoMethod
*cached
;
9638 MonoMethodBuilder
*mb
;
9639 MonoMethodSignature
*sig
;
9640 int return_null_pos
, cache_miss_pos
, cache_hit_pos
, not_an_instance_pos
, negative_cache_hit_pos
;
9646 mb
= mono_mb_new (mono_defaults
.object_class
, "__isinst_with_cache", MONO_WRAPPER_CASTCLASS
);
9647 sig
= mono_metadata_signature_alloc (mono_defaults
.corlib
, 3);
9648 sig
->params
[0] = &mono_defaults
.object_class
->byval_arg
;
9649 sig
->params
[1] = &mono_defaults
.int_class
->byval_arg
;
9650 sig
->params
[2] = &mono_defaults
.int_class
->byval_arg
;
9651 sig
->ret
= &mono_defaults
.object_class
->byval_arg
;
9655 /* allocate local 0 (pointer) obj_vtable */
9656 mono_mb_add_local (mb
, &mono_defaults
.int_class
->byval_arg
);
9657 /* allocate local 1 (pointer) cached_vtable */
9658 mono_mb_add_local (mb
, &mono_defaults
.int_class
->byval_arg
);
9661 mono_mb_emit_ldarg (mb
, 0);
9662 return_null_pos
= mono_mb_emit_branch (mb
, CEE_BRFALSE
);
9664 /*obj_vtable = obj->vtable;*/
9665 mono_mb_emit_ldarg (mb
, 0);
9666 mono_mb_emit_ldflda (mb
, G_STRUCT_OFFSET (MonoObject
, vtable
));
9667 mono_mb_emit_byte (mb
, CEE_LDIND_I
);
9668 mono_mb_emit_stloc (mb
, 0);
9670 /* cached_vtable = *cache*/
9671 mono_mb_emit_ldarg (mb
, 2);
9672 mono_mb_emit_byte (mb
, CEE_LDIND_I
);
9673 mono_mb_emit_stloc (mb
, 1);
9675 mono_mb_emit_ldloc (mb
, 1);
9676 mono_mb_emit_byte (mb
, CEE_LDC_I4
);
9677 mono_mb_emit_i4 (mb
, ~0x1);
9678 mono_mb_emit_byte (mb
, CEE_CONV_U
);
9679 mono_mb_emit_byte (mb
, CEE_AND
);
9680 mono_mb_emit_ldloc (mb
, 0);
9681 /*if ((cached_vtable & ~0x1)== obj_vtable)*/
9682 cache_miss_pos
= mono_mb_emit_branch (mb
, CEE_BNE_UN
);
9684 /*return (cached_vtable & 0x1) ? NULL : obj;*/
9685 mono_mb_emit_ldloc (mb
, 1);
9686 mono_mb_emit_byte(mb
, CEE_LDC_I4_1
);
9687 mono_mb_emit_byte (mb
, CEE_CONV_U
);
9688 mono_mb_emit_byte (mb
, CEE_AND
);
9689 negative_cache_hit_pos
= mono_mb_emit_branch (mb
, CEE_BRTRUE
);
9692 mono_mb_emit_ldarg (mb
, 0);
9693 cache_hit_pos
= mono_mb_emit_branch (mb
, CEE_BR
);
9696 mono_mb_patch_branch (mb
, negative_cache_hit_pos
);
9697 mono_mb_emit_byte (mb
, CEE_LDNULL
);
9699 mono_mb_patch_branch (mb
, cache_hit_pos
);
9700 mono_mb_emit_byte (mb
, CEE_RET
);
9702 mono_mb_patch_branch (mb
, cache_miss_pos
);
9703 /*if (mono_object_isinst (obj, klass)) */
9704 mono_mb_emit_ldarg (mb
, 0);
9705 mono_mb_emit_ldarg (mb
, 1);
9706 mono_mb_emit_icall (mb
, mono_object_isinst
);
9707 not_an_instance_pos
= mono_mb_emit_branch (mb
, CEE_BRFALSE
);
9709 /**cache = obj_vtable;*/
9710 mono_mb_emit_ldarg (mb
, 2);
9711 mono_mb_emit_ldloc (mb
, 0);
9712 mono_mb_emit_byte (mb
, CEE_STIND_I
);
9715 mono_mb_emit_ldarg (mb
, 0);
9716 mono_mb_emit_byte (mb
, CEE_RET
);
9719 mono_mb_patch_branch (mb
, not_an_instance_pos
);
9720 /* *cache = (gpointer)(obj_vtable | 0x1);*/
9721 mono_mb_emit_ldarg (mb
, 2);
9722 /*obj_vtable | 0x1*/
9723 mono_mb_emit_ldloc (mb
, 0);
9724 mono_mb_emit_byte(mb
, CEE_LDC_I4_1
);
9725 mono_mb_emit_byte (mb
, CEE_CONV_U
);
9726 mono_mb_emit_byte (mb
, CEE_OR
);
9729 mono_mb_emit_byte (mb
, CEE_STIND_I
);
9732 mono_mb_patch_branch (mb
, return_null_pos
);
9733 mono_mb_emit_byte (mb
, CEE_LDNULL
);
9734 mono_mb_emit_byte (mb
, CEE_RET
);
9737 res
= mono_mb_create_method (mb
, sig
, 8);
9738 info
= mono_wrapper_info_create (res
, WRAPPER_SUBTYPE_ISINST_WITH_CACHE
);
9739 mono_marshal_set_wrapper_info (res
, info
);
9742 if (InterlockedCompareExchangePointer ((volatile gpointer
*)&cached
, res
, NULL
)) {
9743 mono_free_method (res
);
9744 mono_metadata_free_method_signature (sig
);
9752 * mono_marshal_get_isinst:
9753 * @klass: the type of the field
9755 * This method generates a function which can be used to check if an object is
9756 * an instance of the given type, icluding the case where the object is a proxy.
9757 * The generated function has the following signature:
9758 * MonoObject* __isinst_wrapper_ (MonoObject *obj)
9761 mono_marshal_get_isinst (MonoClass
*klass
)
9763 static MonoMethodSignature
*isint_sig
= NULL
;
9766 int pos_was_ok
, pos_failed
, pos_end
, pos_end2
;
9768 MonoMethodBuilder
*mb
;
9770 cache
= get_cache (&klass
->image
->isinst_cache
, mono_aligned_addr_hash
, NULL
);
9771 if ((res
= mono_marshal_find_in_cache (cache
, klass
)))
9775 isint_sig
= mono_metadata_signature_alloc (mono_defaults
.corlib
, 1);
9776 isint_sig
->params
[0] = &mono_defaults
.object_class
->byval_arg
;
9777 isint_sig
->ret
= &mono_defaults
.object_class
->byval_arg
;
9778 isint_sig
->pinvoke
= 0;
9781 name
= g_strdup_printf ("__isinst_wrapper_%s", klass
->name
);
9782 mb
= mono_mb_new (mono_defaults
.object_class
, name
, MONO_WRAPPER_ISINST
);
9785 mb
->method
->save_lmf
= 1;
9788 /* check if the object is a proxy that needs special cast */
9789 mono_mb_emit_ldarg (mb
, 0);
9790 mono_mb_emit_byte (mb
, MONO_CUSTOM_PREFIX
);
9791 mono_mb_emit_op (mb
, CEE_MONO_CISINST
, klass
);
9793 /* The result of MONO_CISINST can be:
9794 0) the type check succeeded
9795 1) the type check did not succeed
9796 2) a CanCastTo call is needed */
9797 #ifndef DISABLE_REMOTING
9798 mono_mb_emit_byte (mb
, CEE_DUP
);
9799 pos_was_ok
= mono_mb_emit_branch (mb
, CEE_BRFALSE
);
9801 mono_mb_emit_byte (mb
, CEE_LDC_I4_2
);
9802 pos_failed
= mono_mb_emit_branch (mb
, CEE_BNE_UN
);
9804 /* get the real proxy from the transparent proxy*/
9806 mono_mb_emit_ldarg (mb
, 0);
9807 mono_mb_emit_managed_call (mb
, mono_marshal_get_proxy_cancast (klass
), NULL
);
9808 pos_end
= mono_mb_emit_branch (mb
, CEE_BR
);
9812 mono_mb_patch_branch (mb
, pos_failed
);
9813 mono_mb_emit_byte (mb
, CEE_LDNULL
);
9814 pos_end2
= mono_mb_emit_branch (mb
, CEE_BR
);
9818 mono_mb_patch_branch (mb
, pos_was_ok
);
9819 mono_mb_emit_byte (mb
, CEE_POP
);
9820 mono_mb_emit_ldarg (mb
, 0);
9824 mono_mb_patch_branch (mb
, pos_end
);
9825 mono_mb_patch_branch (mb
, pos_end2
);
9826 mono_mb_emit_byte (mb
, CEE_RET
);
9828 pos_was_ok
= mono_mb_emit_branch (mb
, CEE_BRFALSE
);
9832 mono_mb_emit_byte (mb
, CEE_LDNULL
);
9833 pos_end
= mono_mb_emit_branch (mb
, CEE_BR
);
9837 mono_mb_patch_branch (mb
, pos_was_ok
);
9838 mono_mb_emit_ldarg (mb
, 0);
9842 mono_mb_patch_branch (mb
, pos_end
);
9843 mono_mb_emit_byte (mb
, CEE_RET
);
9844 #endif /* DISABLE_REMOTING */
9845 #endif /* DISABLE_JIT */
9847 res
= mono_mb_create_and_cache (cache
, klass
, mb
, isint_sig
, isint_sig
->param_count
+ 16);
9854 * mono_marshal_get_castclass:
9855 * @klass: the type of the field
9857 * This method generates a function which can be used to cast an object to
9858 * an instance of the given type, icluding the case where the object is a proxy.
9859 * The generated function has the following signature:
9860 * MonoObject* __castclass_wrapper_ (MonoObject *obj)
9861 * The wrapper info for the wrapper is a WrapperInfo structure.
9864 mono_marshal_get_castclass (MonoClass
*klass
)
9866 static MonoMethodSignature
*castclass_sig
= NULL
;
9869 int pos_was_ok
, pos_was_ok2
;
9871 MonoMethodBuilder
*mb
;
9874 cache
= get_cache (&klass
->image
->castclass_cache
, mono_aligned_addr_hash
, NULL
);
9875 if ((res
= mono_marshal_find_in_cache (cache
, klass
)))
9878 if (!castclass_sig
) {
9879 castclass_sig
= mono_metadata_signature_alloc (mono_defaults
.corlib
, 1);
9880 castclass_sig
->params
[0] = &mono_defaults
.object_class
->byval_arg
;
9881 castclass_sig
->ret
= &mono_defaults
.object_class
->byval_arg
;
9882 castclass_sig
->pinvoke
= 0;
9885 name
= g_strdup_printf ("__castclass_wrapper_%s", klass
->name
);
9886 mb
= mono_mb_new (mono_defaults
.object_class
, name
, MONO_WRAPPER_CASTCLASS
);
9889 mb
->method
->save_lmf
= 1;
9892 /* check if the object is a proxy that needs special cast */
9893 mono_mb_emit_ldarg (mb
, 0);
9894 mono_mb_emit_byte (mb
, MONO_CUSTOM_PREFIX
);
9895 mono_mb_emit_op (mb
, CEE_MONO_CCASTCLASS
, klass
);
9897 /* The result of MONO_CCASTCLASS can be:
9898 0) the cast is valid
9899 1) cast of unknown proxy type
9900 or an exception if the cast is is invalid
9902 #ifndef DISABLE_REMOTING
9903 pos_was_ok
= mono_mb_emit_branch (mb
, CEE_BRFALSE
);
9905 /* get the real proxy from the transparent proxy*/
9907 mono_mb_emit_ldarg (mb
, 0);
9908 mono_mb_emit_managed_call (mb
, mono_marshal_get_proxy_cancast (klass
), NULL
);
9909 pos_was_ok2
= mono_mb_emit_branch (mb
, CEE_BRTRUE
);
9912 mono_mb_emit_exception (mb
, "InvalidCastException", NULL
);
9915 mono_mb_patch_branch (mb
, pos_was_ok
);
9916 mono_mb_patch_branch (mb
, pos_was_ok2
);
9918 /* MONO_CCASTCLASS leaves an int in the stack with the result, pop it. */
9919 mono_mb_emit_byte (mb
, CEE_POP
);
9920 #endif /* DISABLE_REMOTING */
9922 mono_mb_emit_ldarg (mb
, 0);
9925 mono_mb_emit_byte (mb
, CEE_RET
);
9926 #endif /* DISABLE_JIT */
9928 res
= mono_mb_create_and_cache (cache
, klass
, mb
, castclass_sig
, castclass_sig
->param_count
+ 16);
9931 info
= mono_wrapper_info_create (res
, WRAPPER_SUBTYPE_NONE
);
9932 mono_marshal_set_wrapper_info (res
, info
);
9937 #ifndef DISABLE_REMOTING
9939 mono_marshal_get_proxy_cancast (MonoClass
*klass
)
9941 static MonoMethodSignature
*isint_sig
= NULL
;
9944 int pos_failed
, pos_end
;
9945 char *name
, *klass_name
;
9946 MonoMethod
*can_cast_to
;
9947 MonoMethodDesc
*desc
;
9948 MonoMethodBuilder
*mb
;
9950 cache
= get_cache (&klass
->image
->proxy_isinst_cache
, mono_aligned_addr_hash
, NULL
);
9951 if ((res
= mono_marshal_find_in_cache (cache
, klass
)))
9955 isint_sig
= mono_metadata_signature_alloc (mono_defaults
.corlib
, 1);
9956 isint_sig
->params
[0] = &mono_defaults
.object_class
->byval_arg
;
9957 isint_sig
->ret
= &mono_defaults
.object_class
->byval_arg
;
9958 isint_sig
->pinvoke
= 0;
9961 klass_name
= mono_type_full_name (&klass
->byval_arg
);
9962 name
= g_strdup_printf ("__proxy_isinst_wrapper_%s", klass_name
);
9963 mb
= mono_mb_new (mono_defaults
.object_class
, name
, MONO_WRAPPER_PROXY_ISINST
);
9964 g_free (klass_name
);
9967 mb
->method
->save_lmf
= 1;
9970 /* get the real proxy from the transparent proxy*/
9971 mono_mb_emit_ldarg (mb
, 0);
9972 mono_mb_emit_ldflda (mb
, G_STRUCT_OFFSET (MonoTransparentProxy
, rp
));
9973 mono_mb_emit_byte (mb
, CEE_LDIND_REF
);
9975 /* get the reflection type from the type handle */
9976 mono_mb_emit_ptr (mb
, &klass
->byval_arg
);
9977 mono_mb_emit_icall (mb
, type_from_handle
);
9979 mono_mb_emit_ldarg (mb
, 0);
9981 /* make the call to CanCastTo (type, ob) */
9982 desc
= mono_method_desc_new ("IRemotingTypeInfo:CanCastTo", FALSE
);
9983 can_cast_to
= mono_method_desc_search_in_class (desc
, mono_defaults
.iremotingtypeinfo_class
);
9984 g_assert (can_cast_to
);
9985 mono_method_desc_free (desc
);
9986 mono_mb_emit_op (mb
, CEE_CALLVIRT
, can_cast_to
);
9988 pos_failed
= mono_mb_emit_branch (mb
, CEE_BRFALSE
);
9990 /* Upgrade the proxy vtable by calling: mono_upgrade_remote_class_wrapper (type, ob)*/
9991 mono_mb_emit_ptr (mb
, &klass
->byval_arg
);
9992 mono_mb_emit_icall (mb
, type_from_handle
);
9993 mono_mb_emit_ldarg (mb
, 0);
9995 mono_mb_emit_icall (mb
, mono_upgrade_remote_class_wrapper
);
9996 emit_thread_interrupt_checkpoint (mb
);
9998 mono_mb_emit_ldarg (mb
, 0);
9999 pos_end
= mono_mb_emit_branch (mb
, CEE_BR
);
10003 mono_mb_patch_branch (mb
, pos_failed
);
10004 mono_mb_emit_byte (mb
, CEE_LDNULL
);
10008 mono_mb_patch_branch (mb
, pos_end
);
10009 mono_mb_emit_byte (mb
, CEE_RET
);
10012 res
= mono_mb_create_and_cache (cache
, klass
, mb
, isint_sig
, isint_sig
->param_count
+ 16);
10019 mono_upgrade_remote_class_wrapper (MonoReflectionType
*rtype
, MonoTransparentProxy
*tproxy
)
10022 MonoDomain
*domain
= ((MonoObject
*)tproxy
)->vtable
->domain
;
10023 klass
= mono_class_from_mono_type (rtype
->type
);
10024 mono_upgrade_remote_class (domain
, (MonoObject
*)tproxy
, klass
);
10026 #endif /*DISABLE_REMOTING */
10029 * mono_marshal_get_struct_to_ptr:
10032 * generates IL code for StructureToPtr (object structure, IntPtr ptr, bool fDeleteOld)
10033 * The wrapper info for the wrapper is a WrapperInfo structure.
10036 mono_marshal_get_struct_to_ptr (MonoClass
*klass
)
10038 MonoMethodBuilder
*mb
;
10039 static MonoMethod
*stoptr
= NULL
;
10043 g_assert (klass
!= NULL
);
10045 mono_marshal_load_type_info (klass
);
10047 if (klass
->marshal_info
->str_to_ptr
)
10048 return klass
->marshal_info
->str_to_ptr
;
10051 stoptr
= mono_class_get_method_from_name (mono_defaults
.marshal_class
, "StructureToPtr", 3);
10054 mb
= mono_mb_new (klass
, stoptr
->name
, MONO_WRAPPER_UNKNOWN
);
10056 #ifndef DISABLE_JIT
10057 if (klass
->blittable
) {
10058 mono_mb_emit_byte (mb
, CEE_LDARG_1
);
10059 mono_mb_emit_byte (mb
, CEE_LDARG_0
);
10060 mono_mb_emit_ldflda (mb
, sizeof (MonoObject
));
10061 mono_mb_emit_icon (mb
, mono_class_value_size (klass
, NULL
));
10062 mono_mb_emit_byte (mb
, CEE_PREFIX1
);
10063 mono_mb_emit_byte (mb
, CEE_CPBLK
);
10066 /* allocate local 0 (pointer) src_ptr */
10067 mono_mb_add_local (mb
, &mono_defaults
.int_class
->byval_arg
);
10068 /* allocate local 1 (pointer) dst_ptr */
10069 mono_mb_add_local (mb
, &mono_defaults
.int_class
->byval_arg
);
10070 /* allocate local 2 (boolean) delete_old */
10071 mono_mb_add_local (mb
, &mono_defaults
.boolean_class
->byval_arg
);
10072 mono_mb_emit_byte (mb
, CEE_LDARG_2
);
10073 mono_mb_emit_stloc (mb
, 2);
10075 /* initialize src_ptr to point to the start of object data */
10076 mono_mb_emit_byte (mb
, CEE_LDARG_0
);
10077 mono_mb_emit_ldflda (mb
, sizeof (MonoObject
));
10078 mono_mb_emit_stloc (mb
, 0);
10080 /* initialize dst_ptr */
10081 mono_mb_emit_byte (mb
, CEE_LDARG_1
);
10082 mono_mb_emit_stloc (mb
, 1);
10084 emit_struct_conv (mb
, klass
, FALSE
);
10087 mono_mb_emit_byte (mb
, CEE_RET
);
10090 res
= mono_mb_create_method (mb
, mono_signature_no_pinvoke (stoptr
), 0);
10093 info
= mono_wrapper_info_create (res
, WRAPPER_SUBTYPE_STRUCTURE_TO_PTR
);
10094 mono_marshal_set_wrapper_info (res
, info
);
10096 klass
->marshal_info
->str_to_ptr
= res
;
10101 * mono_marshal_get_ptr_to_struct:
10104 * generates IL code for PtrToStructure (IntPtr src, object structure)
10105 * The wrapper info for the wrapper is a WrapperInfo structure.
10108 mono_marshal_get_ptr_to_struct (MonoClass
*klass
)
10110 MonoMethodBuilder
*mb
;
10111 static MonoMethodSignature
*ptostr
= NULL
;
10115 g_assert (klass
!= NULL
);
10117 mono_marshal_load_type_info (klass
);
10119 if (klass
->marshal_info
->ptr_to_str
)
10120 return klass
->marshal_info
->ptr_to_str
;
10123 MonoMethodSignature
*sig
;
10125 /* Create the signature corresponding to
10126 static void PtrToStructure (IntPtr ptr, object structure);
10127 defined in class/corlib/System.Runtime.InteropServices/Marshal.cs */
10128 sig
= mono_create_icall_signature ("void ptr object");
10129 sig
= signature_dup (mono_defaults
.corlib
, sig
);
10131 mono_memory_barrier ();
10135 mb
= mono_mb_new (klass
, "PtrToStructure", MONO_WRAPPER_UNKNOWN
);
10137 #ifndef DISABLE_JIT
10138 if (klass
->blittable
) {
10139 mono_mb_emit_byte (mb
, CEE_LDARG_1
);
10140 mono_mb_emit_ldflda (mb
, sizeof (MonoObject
));
10141 mono_mb_emit_byte (mb
, CEE_LDARG_0
);
10142 mono_mb_emit_icon (mb
, mono_class_value_size (klass
, NULL
));
10143 mono_mb_emit_byte (mb
, CEE_PREFIX1
);
10144 mono_mb_emit_byte (mb
, CEE_CPBLK
);
10147 /* allocate local 0 (pointer) src_ptr */
10148 mono_mb_add_local (mb
, &mono_defaults
.int_class
->byval_arg
);
10149 /* allocate local 1 (pointer) dst_ptr */
10150 mono_mb_add_local (mb
, &klass
->this_arg
);
10152 /* initialize src_ptr to point to the start of object data */
10153 mono_mb_emit_byte (mb
, CEE_LDARG_0
);
10154 mono_mb_emit_stloc (mb
, 0);
10156 /* initialize dst_ptr */
10157 mono_mb_emit_byte (mb
, CEE_LDARG_1
);
10158 mono_mb_emit_op (mb
, CEE_UNBOX
, klass
);
10159 mono_mb_emit_stloc (mb
, 1);
10161 emit_struct_conv (mb
, klass
, TRUE
);
10164 mono_mb_emit_byte (mb
, CEE_RET
);
10167 res
= mono_mb_create_method (mb
, ptostr
, 0);
10170 info
= mono_wrapper_info_create (res
, WRAPPER_SUBTYPE_PTR_TO_STRUCTURE
);
10171 mono_marshal_set_wrapper_info (res
, info
);
10173 klass
->marshal_info
->ptr_to_str
= res
;
10178 * Return a dummy wrapper for METHOD which is called by synchronized wrappers.
10179 * This is used to avoid infinite recursion since it is hard to determine where to
10180 * replace a method with its synchronized wrapper, and where not.
10181 * The runtime should execute METHOD instead of the wrapper.
10182 * The wrapper info for the wrapper is a WrapperInfo structure.
10185 mono_marshal_get_synchronized_inner_wrapper (MonoMethod
*method
)
10187 MonoMethodBuilder
*mb
;
10189 MonoMethodSignature
*sig
;
10192 mb
= mono_mb_new (method
->klass
, method
->name
, MONO_WRAPPER_UNKNOWN
);
10193 #ifndef DISABLE_JIT
10194 mono_mb_emit_exception_full (mb
, "System", "ExecutionEngineException", "Shouldn't be called.");
10195 mono_mb_emit_byte (mb
, CEE_RET
);
10197 sig
= signature_dup (method
->klass
->image
, mono_method_signature (method
));
10198 res
= mono_mb_create_method (mb
, sig
, 0);
10200 info
= mono_wrapper_info_create (res
, WRAPPER_SUBTYPE_SYNCHRONIZED_INNER
);
10201 info
->d
.synchronized_inner
.method
= method
;
10202 mono_marshal_set_wrapper_info (res
, info
);
10207 * generates IL code for the synchronized wrapper: the generated method
10208 * calls METHOD while locking 'this' or the parent type.
10211 mono_marshal_get_synchronized_wrapper (MonoMethod
*method
)
10213 static MonoMethod
*enter_method
, *exit_method
, *gettypefromhandle_method
;
10214 MonoMethodSignature
*sig
;
10215 MonoExceptionClause
*clause
;
10216 MonoMethodBuilder
*mb
;
10219 int i
, pos
, this_local
, ret_local
= 0;
10223 if (method
->wrapper_type
== MONO_WRAPPER_SYNCHRONIZED
)
10226 cache
= get_cache (&method
->klass
->image
->synchronized_cache
, mono_aligned_addr_hash
, NULL
);
10227 if ((res
= mono_marshal_find_in_cache (cache
, method
)))
10230 sig
= signature_dup (method
->klass
->image
, mono_method_signature (method
));
10233 mb
= mono_mb_new (method
->klass
, method
->name
, MONO_WRAPPER_SYNCHRONIZED
);
10235 #ifndef DISABLE_JIT
10237 if (!MONO_TYPE_IS_VOID (sig
->ret
))
10238 ret_local
= mono_mb_add_local (mb
, sig
->ret
);
10241 if (method
->klass
->valuetype
&& !(method
->flags
& MONO_METHOD_ATTR_STATIC
)) {
10242 mono_class_set_failure (method
->klass
, MONO_EXCEPTION_TYPE_LOAD
, NULL
);
10243 #ifndef DISABLE_JIT
10244 /* This will throw the type load exception when the wrapper is compiled */
10245 mono_mb_emit_byte (mb
, CEE_LDNULL
);
10246 mono_mb_emit_op (mb
, CEE_ISINST
, method
->klass
);
10247 mono_mb_emit_byte (mb
, CEE_POP
);
10249 if (!MONO_TYPE_IS_VOID (sig
->ret
))
10250 mono_mb_emit_ldloc (mb
, ret_local
);
10251 mono_mb_emit_byte (mb
, CEE_RET
);
10254 res
= mono_mb_create_and_cache (cache
, method
,
10255 mb
, sig
, sig
->param_count
+ 16);
10261 #ifndef DISABLE_JIT
10263 this_local
= mono_mb_add_local (mb
, &mono_defaults
.object_class
->byval_arg
);
10265 clause
= mono_image_alloc0 (method
->klass
->image
, sizeof (MonoExceptionClause
));
10266 clause
->flags
= MONO_EXCEPTION_CLAUSE_FINALLY
;
10269 mono_loader_lock ();
10271 if (!enter_method
) {
10272 MonoMethodDesc
*desc
;
10274 desc
= mono_method_desc_new ("Monitor:Enter", FALSE
);
10275 enter_method
= mono_method_desc_search_in_class (desc
, mono_defaults
.monitor_class
);
10276 g_assert (enter_method
);
10277 mono_method_desc_free (desc
);
10279 desc
= mono_method_desc_new ("Monitor:Exit", FALSE
);
10280 exit_method
= mono_method_desc_search_in_class (desc
, mono_defaults
.monitor_class
);
10281 g_assert (exit_method
);
10282 mono_method_desc_free (desc
);
10284 desc
= mono_method_desc_new ("Type:GetTypeFromHandle", FALSE
);
10285 gettypefromhandle_method
= mono_method_desc_search_in_class (desc
, mono_defaults
.monotype_class
->parent
);
10286 g_assert (gettypefromhandle_method
);
10287 mono_method_desc_free (desc
);
10290 mono_loader_unlock ();
10292 #ifndef DISABLE_JIT
10293 /* Push this or the type object */
10294 if (method
->flags
& METHOD_ATTRIBUTE_STATIC
) {
10295 /* We have special handling for this in the JIT */
10296 int index
= mono_mb_add_data (mb
, method
->klass
);
10297 mono_mb_add_data (mb
, mono_defaults
.typehandle_class
);
10298 mono_mb_emit_byte (mb
, CEE_LDTOKEN
);
10299 mono_mb_emit_i4 (mb
, index
);
10301 mono_mb_emit_managed_call (mb
, gettypefromhandle_method
, NULL
);
10304 mono_mb_emit_ldarg (mb
, 0);
10305 mono_mb_emit_stloc (mb
, this_local
);
10307 /* Call Monitor::Enter() */
10308 mono_mb_emit_ldloc (mb
, this_local
);
10309 mono_mb_emit_managed_call (mb
, enter_method
, NULL
);
10311 clause
->try_offset
= mono_mb_get_label (mb
);
10313 /* Call the method */
10315 mono_mb_emit_ldarg (mb
, 0);
10316 for (i
= 0; i
< sig
->param_count
; i
++)
10317 mono_mb_emit_ldarg (mb
, i
+ (sig
->hasthis
== TRUE
));
10319 mono_mb_emit_managed_call (mb
, method
, NULL
);
10321 if (!MONO_TYPE_IS_VOID (sig
->ret
))
10322 mono_mb_emit_stloc (mb
, ret_local
);
10324 pos
= mono_mb_emit_branch (mb
, CEE_LEAVE
);
10326 clause
->try_len
= mono_mb_get_pos (mb
) - clause
->try_offset
;
10327 clause
->handler_offset
= mono_mb_get_label (mb
);
10329 /* Call Monitor::Exit() */
10330 mono_mb_emit_ldloc (mb
, this_local
);
10331 mono_mb_emit_managed_call (mb
, exit_method
, NULL
);
10332 mono_mb_emit_byte (mb
, CEE_ENDFINALLY
);
10334 clause
->handler_len
= mono_mb_get_pos (mb
) - clause
->handler_offset
;
10336 mono_mb_patch_branch (mb
, pos
);
10337 if (!MONO_TYPE_IS_VOID (sig
->ret
))
10338 mono_mb_emit_ldloc (mb
, ret_local
);
10339 mono_mb_emit_byte (mb
, CEE_RET
);
10341 mono_mb_set_clauses (mb
, 1, clause
);
10344 res
= mono_mb_create_and_cache (cache
, method
,
10345 mb
, sig
, sig
->param_count
+ 16);
10353 * the returned method calls 'method' unboxing the this argument
10356 mono_marshal_get_unbox_wrapper (MonoMethod
*method
)
10358 MonoMethodSignature
*sig
= mono_method_signature (method
);
10360 MonoMethodBuilder
*mb
;
10364 cache
= get_cache (&method
->klass
->image
->unbox_wrapper_cache
, mono_aligned_addr_hash
, NULL
);
10365 if ((res
= mono_marshal_find_in_cache (cache
, method
)))
10368 mb
= mono_mb_new (method
->klass
, method
->name
, MONO_WRAPPER_UNBOX
);
10370 g_assert (sig
->hasthis
);
10372 #ifndef DISABLE_JIT
10373 mono_mb_emit_ldarg (mb
, 0);
10374 mono_mb_emit_icon (mb
, sizeof (MonoObject
));
10375 mono_mb_emit_byte (mb
, CEE_ADD
);
10376 for (i
= 0; i
< sig
->param_count
; ++i
)
10377 mono_mb_emit_ldarg (mb
, i
+ 1);
10378 mono_mb_emit_managed_call (mb
, method
, NULL
);
10379 mono_mb_emit_byte (mb
, CEE_RET
);
10382 res
= mono_mb_create_and_cache (cache
, method
,
10383 mb
, sig
, sig
->param_count
+ 16);
10386 /* code_for (res); */
10392 STELEMREF_OBJECT
, /*no check at all*/
10393 STELEMREF_SEALED_CLASS
, /*check vtable->klass->element_type */
10394 STELEMREF_CLASS
, /*only the klass->parents check*/
10395 STELEMREF_INTERFACE
, /*interfaces without variant generic arguments. */
10396 STELEMREF_COMPLEX
, /*arrays, MBR or types with variant generic args - go straight to icalls*/
10397 STELEMREF_KIND_COUNT
10400 static const char *strelemref_wrapper_name
[] = {
10401 "object", "sealed_class", "class", "interface", "complex"
10405 is_monomorphic_array (MonoClass
*klass
)
10407 MonoClass
*element_class
;
10408 if (klass
->rank
!= 1)
10411 element_class
= klass
->element_class
;
10412 return (element_class
->flags
& TYPE_ATTRIBUTE_SEALED
) || element_class
->valuetype
;
10416 get_virtual_stelemref_kind (MonoClass
*element_class
)
10418 if (element_class
== mono_defaults
.object_class
)
10419 return STELEMREF_OBJECT
;
10420 if (is_monomorphic_array (element_class
))
10421 return STELEMREF_SEALED_CLASS
;
10422 /* Compressed interface bitmaps require code that is quite complex, so don't optimize for it. */
10423 if (MONO_CLASS_IS_INTERFACE (element_class
) && !mono_class_has_variant_generic_params (element_class
))
10424 #ifdef COMPRESSED_INTERFACE_BITMAP
10425 return STELEMREF_COMPLEX
;
10427 return STELEMREF_INTERFACE
;
10429 /*Arrays are sealed but are covariant on their element type, We can't use any of the fast paths.*/
10430 if (mono_class_is_marshalbyref (element_class
) || element_class
->rank
|| mono_class_has_variant_generic_params (element_class
))
10431 return STELEMREF_COMPLEX
;
10432 if (element_class
->flags
& TYPE_ATTRIBUTE_SEALED
)
10433 return STELEMREF_SEALED_CLASS
;
10434 return STELEMREF_CLASS
;
10437 #ifndef DISABLE_JIT
10440 load_array_element_address (MonoMethodBuilder
*mb
)
10442 mono_mb_emit_ldarg (mb
, 0);
10443 mono_mb_emit_ldarg (mb
, 1);
10444 mono_mb_emit_op (mb
, CEE_LDELEMA
, mono_defaults
.object_class
);
10448 load_array_class (MonoMethodBuilder
*mb
, int aklass
)
10450 mono_mb_emit_ldarg (mb
, 0);
10451 mono_mb_emit_ldflda (mb
, G_STRUCT_OFFSET (MonoObject
, vtable
));
10452 mono_mb_emit_byte (mb
, CEE_LDIND_I
);
10453 mono_mb_emit_ldflda (mb
, G_STRUCT_OFFSET (MonoVTable
, klass
));
10454 mono_mb_emit_byte (mb
, CEE_LDIND_I
);
10455 mono_mb_emit_ldflda (mb
, G_STRUCT_OFFSET (MonoClass
, element_class
));
10456 mono_mb_emit_byte (mb
, CEE_LDIND_I
);
10457 mono_mb_emit_stloc (mb
, aklass
);
10461 load_value_class (MonoMethodBuilder
*mb
, int vklass
)
10463 mono_mb_emit_ldarg (mb
, 2);
10464 mono_mb_emit_ldflda (mb
, G_STRUCT_OFFSET (MonoObject
, vtable
));
10465 mono_mb_emit_byte (mb
, CEE_LDIND_I
);
10466 mono_mb_emit_ldflda (mb
, G_STRUCT_OFFSET (MonoVTable
, klass
));
10467 mono_mb_emit_byte (mb
, CEE_LDIND_I
);
10468 mono_mb_emit_stloc (mb
, vklass
);
10474 record_slot_vstore (MonoObject
*array
, size_t index
, MonoObject
*value
)
10476 char *name
= mono_type_get_full_name (array
->vtable
->klass
->element_class
);
10477 printf ("slow vstore of %s\n", name
);
10483 * The wrapper info for the wrapper is a WrapperInfo structure.
10486 * - Separate simple interfaces from variant interfaces or mbr types. This way we can avoid the icall for them.
10487 * - Emit a (new) mono bytecode that produces OP_COND_EXC_NE_UN to raise ArrayTypeMismatch
10488 * - Maybe mve some MonoClass field into the vtable to reduce the number of loads
10489 * - Add a case for arrays of arrays.
10492 get_virtual_stelemref_wrapper (int kind
)
10494 static MonoMethod
*cached_methods
[STELEMREF_KIND_COUNT
] = { NULL
}; /*object iface sealed regular*/
10495 static MonoMethodSignature
*signature
;
10496 MonoMethodBuilder
*mb
;
10499 const char *param_names
[16];
10500 guint32 b1
, b2
, b3
;
10501 int aklass
, vklass
, vtable
, uiid
;
10502 int array_slot_addr
;
10505 if (cached_methods
[kind
])
10506 return cached_methods
[kind
];
10508 name
= g_strdup_printf ("virt_stelemref_%s", strelemref_wrapper_name
[kind
]);
10509 mb
= mono_mb_new (mono_defaults
.object_class
, name
, MONO_WRAPPER_STELEMREF
);
10513 MonoMethodSignature
*sig
= mono_metadata_signature_alloc (mono_defaults
.corlib
, 2);
10515 /* void this::stelemref (size_t idx, void* value) */
10516 sig
->ret
= &mono_defaults
.void_class
->byval_arg
;
10517 sig
->hasthis
= TRUE
;
10518 sig
->params
[0] = &mono_defaults
.int_class
->byval_arg
; /* this is a natural sized int */
10519 sig
->params
[1] = &mono_defaults
.object_class
->byval_arg
;
10523 #ifndef DISABLE_JIT
10524 param_names
[0] = "index";
10525 param_names
[1] = "value";
10526 mono_mb_set_param_names (mb
, param_names
);
10528 /*For now simply call plain old stelemref*/
10530 case STELEMREF_OBJECT
:
10531 /* ldelema (implicit bound check) */
10532 load_array_element_address (mb
);
10534 mono_mb_emit_ldarg (mb
, 2);
10535 mono_mb_emit_byte (mb
, CEE_STIND_REF
);
10536 mono_mb_emit_byte (mb
, CEE_RET
);
10539 case STELEMREF_COMPLEX
:
10541 <ldelema (bound check)>
10544 if (!mono_object_isinst (value, aklass))
10548 *array_slot_addr = value;
10551 throw new ArrayTypeMismatchException ();
10554 aklass
= mono_mb_add_local (mb
, &mono_defaults
.int_class
->byval_arg
);
10555 array_slot_addr
= mono_mb_add_local (mb
, &mono_defaults
.object_class
->this_arg
);
10559 /*Use this to debug/record stores that are going thru the slow path*/
10560 MonoMethodSignature
*csig
;
10561 csig
= mono_metadata_signature_alloc (mono_defaults
.corlib
, 3);
10562 csig
->ret
= &mono_defaults
.void_class
->byval_arg
;
10563 csig
->params
[0] = &mono_defaults
.object_class
->byval_arg
;
10564 csig
->params
[1] = &mono_defaults
.int_class
->byval_arg
; /* this is a natural sized int */
10565 csig
->params
[2] = &mono_defaults
.object_class
->byval_arg
;
10566 mono_mb_emit_ldarg (mb
, 0);
10567 mono_mb_emit_ldarg (mb
, 1);
10568 mono_mb_emit_ldarg (mb
, 2);
10569 mono_mb_emit_native_call (mb
, csig
, record_slot_vstore
);
10573 /* ldelema (implicit bound check) */
10574 load_array_element_address (mb
);
10575 mono_mb_emit_stloc (mb
, array_slot_addr
);
10577 /* if (!value) goto do_store */
10578 mono_mb_emit_ldarg (mb
, 2);
10579 b1
= mono_mb_emit_branch (mb
, CEE_BRFALSE
);
10581 /* aklass = array->vtable->klass->element_class */
10582 load_array_class (mb
, aklass
);
10584 /*if (mono_object_isinst (value, aklass)) */
10585 mono_mb_emit_ldarg (mb
, 2);
10586 mono_mb_emit_ldloc (mb
, aklass
);
10587 mono_mb_emit_icall (mb
, mono_object_isinst
);
10588 b2
= mono_mb_emit_branch (mb
, CEE_BRFALSE
);
10591 mono_mb_patch_branch (mb
, b1
);
10592 mono_mb_emit_ldloc (mb
, array_slot_addr
);
10593 mono_mb_emit_ldarg (mb
, 2);
10594 mono_mb_emit_byte (mb
, CEE_STIND_REF
);
10595 mono_mb_emit_byte (mb
, CEE_RET
);
10597 /* do_exception: */
10598 mono_mb_patch_branch (mb
, b2
);
10600 mono_mb_emit_exception (mb
, "ArrayTypeMismatchException", NULL
);
10603 case STELEMREF_SEALED_CLASS
:
10605 <ldelema (bound check)>
10609 aklass = array->vtable->klass->element_class;
10610 vklass = value->vtable->klass;
10612 if (vklass != aklass)
10616 *array_slot_addr = value;
10619 throw new ArrayTypeMismatchException ();
10621 aklass
= mono_mb_add_local (mb
, &mono_defaults
.int_class
->byval_arg
);
10622 vklass
= mono_mb_add_local (mb
, &mono_defaults
.int_class
->byval_arg
);
10623 array_slot_addr
= mono_mb_add_local (mb
, &mono_defaults
.object_class
->this_arg
);
10626 /* ldelema (implicit bound check) */
10627 load_array_element_address (mb
);
10628 mono_mb_emit_stloc (mb
, array_slot_addr
);
10630 /* if (!value) goto do_store */
10631 mono_mb_emit_ldarg (mb
, 2);
10632 b1
= mono_mb_emit_branch (mb
, CEE_BRFALSE
);
10634 /* aklass = array->vtable->klass->element_class */
10635 load_array_class (mb
, aklass
);
10637 /* vklass = value->vtable->klass */
10638 load_value_class (mb
, vklass
);
10640 /*if (vklass != aklass) goto do_exception; */
10641 mono_mb_emit_ldloc (mb
, aklass
);
10642 mono_mb_emit_ldloc (mb
, vklass
);
10643 b2
= mono_mb_emit_branch (mb
, CEE_BNE_UN
);
10646 mono_mb_patch_branch (mb
, b1
);
10647 mono_mb_emit_ldloc (mb
, array_slot_addr
);
10648 mono_mb_emit_ldarg (mb
, 2);
10649 mono_mb_emit_byte (mb
, CEE_STIND_REF
);
10650 mono_mb_emit_byte (mb
, CEE_RET
);
10652 /* do_exception: */
10653 mono_mb_patch_branch (mb
, b2
);
10654 mono_mb_emit_exception (mb
, "ArrayTypeMismatchException", NULL
);
10657 case STELEMREF_CLASS
:
10660 <ldelema (bound check)>
10664 aklass = array->vtable->klass->element_class;
10665 vklass = value->vtable->klass;
10667 if (vklass->idepth < aklass->idepth)
10670 if (vklass->supertypes [aklass->idepth - 1] != aklass)
10674 *array_slot_addr = value;
10678 throw new ArrayTypeMismatchException ();
10680 aklass
= mono_mb_add_local (mb
, &mono_defaults
.int_class
->byval_arg
);
10681 vklass
= mono_mb_add_local (mb
, &mono_defaults
.int_class
->byval_arg
);
10682 array_slot_addr
= mono_mb_add_local (mb
, &mono_defaults
.object_class
->this_arg
);
10684 /* ldelema (implicit bound check) */
10685 load_array_element_address (mb
);
10686 mono_mb_emit_stloc (mb
, array_slot_addr
);
10688 /* if (!value) goto do_store */
10689 mono_mb_emit_ldarg (mb
, 2);
10690 b1
= mono_mb_emit_branch (mb
, CEE_BRFALSE
);
10692 /* aklass = array->vtable->klass->element_class */
10693 load_array_class (mb
, aklass
);
10695 /* vklass = value->vtable->klass */
10696 load_value_class (mb
, vklass
);
10698 /*if (mono_object_isinst (value, aklass)) */
10699 mono_mb_emit_ldarg (mb
, 2);
10700 mono_mb_emit_ldloc (mb
, aklass
);
10701 mono_mb_emit_icall (mb
, mono_object_isinst
);
10702 b2
= mono_mb_emit_branch (mb
, CEE_BRFALSE
);
10704 /* if (vklass->idepth < aklass->idepth) goto failue */
10705 mono_mb_emit_ldloc (mb
, vklass
);
10706 mono_mb_emit_ldflda (mb
, G_STRUCT_OFFSET (MonoClass
, idepth
));
10707 mono_mb_emit_byte (mb
, CEE_LDIND_U2
);
10709 mono_mb_emit_ldloc (mb
, aklass
);
10710 mono_mb_emit_ldflda (mb
, G_STRUCT_OFFSET (MonoClass
, idepth
));
10711 mono_mb_emit_byte (mb
, CEE_LDIND_U2
);
10713 b2
= mono_mb_emit_branch (mb
, CEE_BLT_UN
);
10715 /* if (vklass->supertypes [aklass->idepth - 1] != aklass) goto failure */
10716 mono_mb_emit_ldloc (mb
, vklass
);
10717 mono_mb_emit_ldflda (mb
, G_STRUCT_OFFSET (MonoClass
, supertypes
));
10718 mono_mb_emit_byte (mb
, CEE_LDIND_I
);
10720 mono_mb_emit_ldloc (mb
, aklass
);
10721 mono_mb_emit_ldflda (mb
, G_STRUCT_OFFSET (MonoClass
, idepth
));
10722 mono_mb_emit_byte (mb
, CEE_LDIND_U2
);
10723 mono_mb_emit_icon (mb
, 1);
10724 mono_mb_emit_byte (mb
, CEE_SUB
);
10725 mono_mb_emit_icon (mb
, sizeof (void*));
10726 mono_mb_emit_byte (mb
, CEE_MUL
);
10727 mono_mb_emit_byte (mb
, CEE_ADD
);
10728 mono_mb_emit_byte (mb
, CEE_LDIND_I
);
10730 mono_mb_emit_ldloc (mb
, aklass
);
10731 b3
= mono_mb_emit_branch (mb
, CEE_BNE_UN
);
10734 mono_mb_patch_branch (mb
, b1
);
10735 mono_mb_emit_ldloc (mb
, array_slot_addr
);
10736 mono_mb_emit_ldarg (mb
, 2);
10737 mono_mb_emit_byte (mb
, CEE_STIND_REF
);
10738 mono_mb_emit_byte (mb
, CEE_RET
);
10740 /* do_exception: */
10741 mono_mb_patch_branch (mb
, b2
);
10742 mono_mb_patch_branch (mb
, b3
);
10744 mono_mb_emit_exception (mb
, "ArrayTypeMismatchException", NULL
);
10747 case STELEMREF_INTERFACE
:
10754 klass = array->obj.vtable->klass->element_class;
10755 vt = value->vtable;
10756 uiid = klass->interface_id;
10757 if (uiid > vt->max_interface_id)
10759 if (!(vt->interface_bitmap [(uiid) >> 3] & (1 << ((uiid)&7))))
10762 mono_array_setref (array, index, value);
10765 mono_raise_exception (mono_get_exception_array_type_mismatch ());*/
10767 array_slot_addr
= mono_mb_add_local (mb
, &mono_defaults
.object_class
->this_arg
);
10768 aklass
= mono_mb_add_local (mb
, &mono_defaults
.int_class
->byval_arg
);
10769 vtable
= mono_mb_add_local (mb
, &mono_defaults
.int_class
->byval_arg
);
10770 uiid
= mono_mb_add_local (mb
, &mono_defaults
.int32_class
->byval_arg
);
10772 /* ldelema (implicit bound check) */
10773 load_array_element_address (mb
);
10774 mono_mb_emit_stloc (mb
, array_slot_addr
);
10776 /* if (!value) goto do_store */
10777 mono_mb_emit_ldarg (mb
, 2);
10778 b1
= mono_mb_emit_branch (mb
, CEE_BRFALSE
);
10780 /* klass = array->vtable->klass->element_class */
10781 load_array_class (mb
, aklass
);
10783 /* vt = value->vtable */
10784 mono_mb_emit_ldarg (mb
, 2);
10785 mono_mb_emit_ldflda (mb
, G_STRUCT_OFFSET (MonoObject
, vtable
));
10786 mono_mb_emit_byte (mb
, CEE_LDIND_I
);
10787 mono_mb_emit_stloc (mb
, vtable
);
10789 /* uiid = klass->interface_id; */
10790 mono_mb_emit_ldloc (mb
, aklass
);
10791 mono_mb_emit_ldflda (mb
, G_STRUCT_OFFSET (MonoClass
, interface_id
));
10792 mono_mb_emit_byte (mb
, CEE_LDIND_U2
);
10793 mono_mb_emit_stloc (mb
, uiid
);
10795 /*if (uiid > vt->max_interface_id)*/
10796 mono_mb_emit_ldloc (mb
, uiid
);
10797 mono_mb_emit_ldloc (mb
, vtable
);
10798 mono_mb_emit_ldflda (mb
, G_STRUCT_OFFSET (MonoVTable
, max_interface_id
));
10799 mono_mb_emit_byte (mb
, CEE_LDIND_U2
);
10800 b2
= mono_mb_emit_branch (mb
, CEE_BGT_UN
);
10802 /* if (!(vt->interface_bitmap [(uiid) >> 3] & (1 << ((uiid)&7)))) */
10804 /*vt->interface_bitmap*/
10805 mono_mb_emit_ldloc (mb
, vtable
);
10806 mono_mb_emit_ldflda (mb
, G_STRUCT_OFFSET (MonoVTable
, interface_bitmap
));
10807 mono_mb_emit_byte (mb
, CEE_LDIND_I
);
10810 mono_mb_emit_ldloc (mb
, uiid
);
10811 mono_mb_emit_icon (mb
, 3);
10812 mono_mb_emit_byte (mb
, CEE_SHR_UN
);
10814 /*vt->interface_bitmap [(uiid) >> 3]*/
10815 mono_mb_emit_byte (mb
, CEE_ADD
); /*interface_bitmap is a guint8 array*/
10816 mono_mb_emit_byte (mb
, CEE_LDIND_U1
);
10818 /*(1 << ((uiid)&7)))*/
10819 mono_mb_emit_icon (mb
, 1);
10820 mono_mb_emit_ldloc (mb
, uiid
);
10821 mono_mb_emit_icon (mb
, 7);
10822 mono_mb_emit_byte (mb
, CEE_AND
);
10823 mono_mb_emit_byte (mb
, CEE_SHL
);
10825 /*bitwise and the whole thing*/
10826 mono_mb_emit_byte (mb
, CEE_AND
);
10827 b3
= mono_mb_emit_branch (mb
, CEE_BRFALSE
);
10830 mono_mb_patch_branch (mb
, b1
);
10831 mono_mb_emit_ldloc (mb
, array_slot_addr
);
10832 mono_mb_emit_ldarg (mb
, 2);
10833 mono_mb_emit_byte (mb
, CEE_STIND_REF
);
10834 mono_mb_emit_byte (mb
, CEE_RET
);
10836 /* do_exception: */
10837 mono_mb_patch_branch (mb
, b2
);
10838 mono_mb_patch_branch (mb
, b3
);
10839 mono_mb_emit_exception (mb
, "ArrayTypeMismatchException", NULL
);
10843 mono_mb_emit_ldarg (mb
, 0);
10844 mono_mb_emit_ldarg (mb
, 1);
10845 mono_mb_emit_ldarg (mb
, 2);
10846 mono_mb_emit_managed_call (mb
, mono_marshal_get_stelemref (), NULL
);
10847 mono_mb_emit_byte (mb
, CEE_RET
);
10850 #endif /* DISABLE_JIT */
10852 res
= mono_mb_create_method (mb
, signature
, 4);
10853 res
->flags
|= METHOD_ATTRIBUTE_VIRTUAL
;
10855 info
= mono_wrapper_info_create (res
, WRAPPER_SUBTYPE_VIRTUAL_STELEMREF
);
10856 info
->d
.virtual_stelemref
.kind
= kind
;
10857 mono_marshal_set_wrapper_info (res
, info
);
10859 mono_marshal_lock ();
10860 if (!cached_methods
[kind
]) {
10861 cached_methods
[kind
] = res
;
10862 mono_marshal_unlock ();
10864 mono_marshal_unlock ();
10865 mono_free_method (res
);
10869 return cached_methods
[kind
];
10873 mono_marshal_get_virtual_stelemref (MonoClass
*array_class
)
10877 g_assert (array_class
->rank
== 1);
10878 kind
= get_virtual_stelemref_kind (array_class
->element_class
);
10880 return get_virtual_stelemref_wrapper (kind
);
10884 mono_marshal_get_virtual_stelemref_wrappers (int *nwrappers
)
10889 *nwrappers
= STELEMREF_KIND_COUNT
;
10890 res
= g_malloc0 (STELEMREF_KIND_COUNT
* sizeof (MonoMethod
*));
10891 for (i
= 0; i
< STELEMREF_KIND_COUNT
; ++i
)
10892 res
[i
] = get_virtual_stelemref_wrapper (i
);
10897 * The wrapper info for the wrapper is a WrapperInfo structure.
10900 mono_marshal_get_stelemref ()
10902 static MonoMethod
* ret
= NULL
;
10903 MonoMethodSignature
*sig
;
10904 MonoMethodBuilder
*mb
;
10907 guint32 b1
, b2
, b3
, b4
;
10909 int aklass
, vklass
;
10910 int array_slot_addr
;
10915 mb
= mono_mb_new (mono_defaults
.object_class
, "stelemref", MONO_WRAPPER_STELEMREF
);
10918 sig
= mono_metadata_signature_alloc (mono_defaults
.corlib
, 3);
10920 /* void stelemref (void* array, int idx, void* value) */
10921 sig
->ret
= &mono_defaults
.void_class
->byval_arg
;
10922 sig
->params
[0] = &mono_defaults
.object_class
->byval_arg
;
10923 sig
->params
[1] = &mono_defaults
.int_class
->byval_arg
; /* this is a natural sized int */
10924 sig
->params
[2] = &mono_defaults
.object_class
->byval_arg
;
10926 #ifndef DISABLE_JIT
10927 aklass
= mono_mb_add_local (mb
, &mono_defaults
.int_class
->byval_arg
);
10928 vklass
= mono_mb_add_local (mb
, &mono_defaults
.int_class
->byval_arg
);
10929 array_slot_addr
= mono_mb_add_local (mb
, &mono_defaults
.object_class
->this_arg
);
10933 <ldelema (bound check)>
10937 aklass = array->vtable->klass->element_class;
10938 vklass = value->vtable->klass;
10940 if (vklass->idepth < aklass->idepth)
10943 if (vklass->supertypes [aklass->idepth - 1] != aklass)
10947 *array_slot_addr = value;
10951 if (mono_object_isinst (value, aklass))
10954 throw new ArrayTypeMismatchException ();
10957 /* ldelema (implicit bound check) */
10958 mono_mb_emit_ldarg (mb
, 0);
10959 mono_mb_emit_ldarg (mb
, 1);
10960 mono_mb_emit_op (mb
, CEE_LDELEMA
, mono_defaults
.object_class
);
10961 mono_mb_emit_stloc (mb
, array_slot_addr
);
10963 /* if (!value) goto do_store */
10964 mono_mb_emit_ldarg (mb
, 2);
10965 b1
= mono_mb_emit_branch (mb
, CEE_BRFALSE
);
10967 /* aklass = array->vtable->klass->element_class */
10968 mono_mb_emit_ldarg (mb
, 0);
10969 mono_mb_emit_ldflda (mb
, G_STRUCT_OFFSET (MonoObject
, vtable
));
10970 mono_mb_emit_byte (mb
, CEE_LDIND_I
);
10971 mono_mb_emit_ldflda (mb
, G_STRUCT_OFFSET (MonoVTable
, klass
));
10972 mono_mb_emit_byte (mb
, CEE_LDIND_I
);
10973 mono_mb_emit_ldflda (mb
, G_STRUCT_OFFSET (MonoClass
, element_class
));
10974 mono_mb_emit_byte (mb
, CEE_LDIND_I
);
10975 mono_mb_emit_stloc (mb
, aklass
);
10977 /* vklass = value->vtable->klass */
10978 mono_mb_emit_ldarg (mb
, 2);
10979 mono_mb_emit_ldflda (mb
, G_STRUCT_OFFSET (MonoObject
, vtable
));
10980 mono_mb_emit_byte (mb
, CEE_LDIND_I
);
10981 mono_mb_emit_ldflda (mb
, G_STRUCT_OFFSET (MonoVTable
, klass
));
10982 mono_mb_emit_byte (mb
, CEE_LDIND_I
);
10983 mono_mb_emit_stloc (mb
, vklass
);
10985 /* if (vklass->idepth < aklass->idepth) goto failue */
10986 mono_mb_emit_ldloc (mb
, vklass
);
10987 mono_mb_emit_ldflda (mb
, G_STRUCT_OFFSET (MonoClass
, idepth
));
10988 mono_mb_emit_byte (mb
, CEE_LDIND_U2
);
10990 mono_mb_emit_ldloc (mb
, aklass
);
10991 mono_mb_emit_ldflda (mb
, G_STRUCT_OFFSET (MonoClass
, idepth
));
10992 mono_mb_emit_byte (mb
, CEE_LDIND_U2
);
10994 b2
= mono_mb_emit_branch (mb
, CEE_BLT_UN
);
10996 /* if (vklass->supertypes [aklass->idepth - 1] != aklass) goto failure */
10997 mono_mb_emit_ldloc (mb
, vklass
);
10998 mono_mb_emit_ldflda (mb
, G_STRUCT_OFFSET (MonoClass
, supertypes
));
10999 mono_mb_emit_byte (mb
, CEE_LDIND_I
);
11001 mono_mb_emit_ldloc (mb
, aklass
);
11002 mono_mb_emit_ldflda (mb
, G_STRUCT_OFFSET (MonoClass
, idepth
));
11003 mono_mb_emit_byte (mb
, CEE_LDIND_U2
);
11004 mono_mb_emit_icon (mb
, 1);
11005 mono_mb_emit_byte (mb
, CEE_SUB
);
11006 mono_mb_emit_icon (mb
, sizeof (void*));
11007 mono_mb_emit_byte (mb
, CEE_MUL
);
11008 mono_mb_emit_byte (mb
, CEE_ADD
);
11009 mono_mb_emit_byte (mb
, CEE_LDIND_I
);
11011 mono_mb_emit_ldloc (mb
, aklass
);
11013 b3
= mono_mb_emit_branch (mb
, CEE_BNE_UN
);
11015 copy_pos
= mono_mb_get_label (mb
);
11017 mono_mb_patch_branch (mb
, b1
);
11018 mono_mb_emit_ldloc (mb
, array_slot_addr
);
11019 mono_mb_emit_ldarg (mb
, 2);
11020 mono_mb_emit_byte (mb
, CEE_STIND_REF
);
11022 mono_mb_emit_byte (mb
, CEE_RET
);
11025 mono_mb_patch_branch (mb
, b2
);
11026 mono_mb_patch_branch (mb
, b3
);
11028 mono_mb_emit_ldarg (mb
, 2);
11029 mono_mb_emit_ldloc (mb
, aklass
);
11030 mono_mb_emit_icall (mb
, mono_object_isinst
);
11032 b4
= mono_mb_emit_branch (mb
, CEE_BRTRUE
);
11033 mono_mb_patch_addr (mb
, b4
, copy_pos
- (b4
+ 4));
11034 mono_mb_emit_exception (mb
, "ArrayTypeMismatchException", NULL
);
11036 mono_mb_emit_byte (mb
, CEE_RET
);
11039 ret
= mono_mb_create_method (mb
, sig
, 4);
11042 info
= mono_wrapper_info_create (ret
, WRAPPER_SUBTYPE_NONE
);
11043 mono_marshal_set_wrapper_info (ret
, info
);
11049 * mono_marshal_get_gsharedvt_in_wrapper:
11051 * This wrapper handles calls from normal code to gsharedvt code.
11053 * The wrapper info for the wrapper is a WrapperInfo structure.
11056 mono_marshal_get_gsharedvt_in_wrapper (void)
11058 static MonoMethod
* ret
= NULL
;
11059 MonoMethodSignature
*sig
;
11060 MonoMethodBuilder
*mb
;
11066 mb
= mono_mb_new (mono_defaults
.object_class
, "gsharedvt_in", MONO_WRAPPER_UNKNOWN
);
11068 sig
= mono_metadata_signature_alloc (mono_defaults
.corlib
, 0);
11069 sig
->ret
= &mono_defaults
.void_class
->byval_arg
;
11071 #ifndef DISABLE_JIT
11073 * The body is generated by the JIT, we use a wrapper instead of a trampoline so EH works.
11075 mono_mb_emit_byte (mb
, CEE_RET
);
11078 ret
= mono_mb_create_method (mb
, sig
, 4);
11081 info
= mono_wrapper_info_create (ret
, WRAPPER_SUBTYPE_GSHAREDVT_IN
);
11082 mono_marshal_set_wrapper_info (ret
, info
);
11088 * mono_marshal_get_gsharedvt_out_wrapper:
11090 * This wrapper handles calls from gsharedvt code to normal code.
11092 * The wrapper info for the wrapper is a WrapperInfo structure.
11095 mono_marshal_get_gsharedvt_out_wrapper (void)
11097 static MonoMethod
* ret
= NULL
;
11098 MonoMethodSignature
*sig
;
11099 MonoMethodBuilder
*mb
;
11105 mb
= mono_mb_new (mono_defaults
.object_class
, "gsharedvt_out", MONO_WRAPPER_UNKNOWN
);
11107 sig
= mono_metadata_signature_alloc (mono_defaults
.corlib
, 0);
11108 sig
->ret
= &mono_defaults
.void_class
->byval_arg
;
11110 #ifndef DISABLE_JIT
11112 * The body is generated by the JIT, we use a wrapper instead of a trampoline so EH works.
11114 mono_mb_emit_byte (mb
, CEE_RET
);
11117 ret
= mono_mb_create_method (mb
, sig
, 4);
11120 info
= mono_wrapper_info_create (ret
, WRAPPER_SUBTYPE_GSHAREDVT_OUT
);
11121 mono_marshal_set_wrapper_info (ret
, info
);
11129 MonoMethod
*method
;
11132 /* LOCKING: vars accessed under the marshal lock */
11133 static ArrayElemAddr
*elem_addr_cache
= NULL
;
11134 static int elem_addr_cache_size
= 0;
11135 static int elem_addr_cache_next
= 0;
11138 * mono_marshal_get_array_address:
11139 * @rank: rank of the array type
11140 * @elem_size: size in bytes of an element of an array.
11142 * Returns a MonoMethd that implements the code to get the address
11143 * of an element in a multi-dimenasional array of @rank dimensions.
11144 * The returned method takes an array as the first argument and then
11145 * @rank indexes for the @rank dimensions.
11148 mono_marshal_get_array_address (int rank
, int elem_size
)
11151 MonoMethodBuilder
*mb
;
11152 MonoMethodSignature
*sig
;
11153 int i
, bounds
, ind
, realidx
;
11154 int branch_pos
, *branch_positions
;
11158 mono_marshal_lock ();
11159 for (i
= 0; i
< elem_addr_cache_next
; ++i
) {
11160 if (elem_addr_cache
[i
].rank
== rank
&& elem_addr_cache
[i
].elem_size
== elem_size
) {
11161 ret
= elem_addr_cache
[i
].method
;
11165 mono_marshal_unlock ();
11169 branch_positions
= g_new0 (int, rank
);
11171 sig
= mono_metadata_signature_alloc (mono_defaults
.corlib
, 1 + rank
);
11173 /* void* address (void* array, int idx0, int idx1, int idx2, ...) */
11174 sig
->ret
= &mono_defaults
.int_class
->byval_arg
;
11175 sig
->params
[0] = &mono_defaults
.object_class
->byval_arg
;
11176 for (i
= 0; i
< rank
; ++i
) {
11177 sig
->params
[i
+ 1] = &mono_defaults
.int32_class
->byval_arg
;
11180 mb
= mono_mb_new (mono_defaults
.object_class
, "ElementAddr", MONO_WRAPPER_MANAGED_TO_MANAGED
);
11182 #ifndef DISABLE_JIT
11183 bounds
= mono_mb_add_local (mb
, &mono_defaults
.int_class
->byval_arg
);
11184 ind
= mono_mb_add_local (mb
, &mono_defaults
.int32_class
->byval_arg
);
11185 realidx
= mono_mb_add_local (mb
, &mono_defaults
.int32_class
->byval_arg
);
11187 /* bounds = array->bounds; */
11188 mono_mb_emit_ldarg (mb
, 0);
11189 mono_mb_emit_ldflda (mb
, G_STRUCT_OFFSET (MonoArray
, bounds
));
11190 mono_mb_emit_byte (mb
, CEE_LDIND_I
);
11191 mono_mb_emit_stloc (mb
, bounds
);
11193 /* ind is the overall element index, realidx is the partial index in a single dimension */
11194 /* ind = idx0 - bounds [0].lower_bound */
11195 mono_mb_emit_ldarg (mb
, 1);
11196 mono_mb_emit_ldloc (mb
, bounds
);
11197 mono_mb_emit_icon (mb
, G_STRUCT_OFFSET (MonoArrayBounds
, lower_bound
));
11198 mono_mb_emit_byte (mb
, CEE_ADD
);
11199 mono_mb_emit_byte (mb
, CEE_LDIND_I4
);
11200 mono_mb_emit_byte (mb
, CEE_SUB
);
11201 mono_mb_emit_stloc (mb
, ind
);
11202 /* if (ind >= bounds [0].length) goto exeception; */
11203 mono_mb_emit_ldloc (mb
, ind
);
11204 mono_mb_emit_ldloc (mb
, bounds
);
11205 mono_mb_emit_icon (mb
, G_STRUCT_OFFSET (MonoArrayBounds
, length
));
11206 mono_mb_emit_byte (mb
, CEE_ADD
);
11207 mono_mb_emit_byte (mb
, CEE_LDIND_I4
);
11208 /* note that we use unsigned comparison */
11209 branch_pos
= mono_mb_emit_branch (mb
, CEE_BGE_UN
);
11211 /* For large ranks (> 4?) use a loop n IL later to reduce code size.
11212 * We could also decide to ignore the passed elem_size and get it
11213 * from the array object, to reduce the number of methods we generate:
11214 * the additional cost is 3 memory loads and a non-immediate mul.
11216 for (i
= 1; i
< rank
; ++i
) {
11217 /* realidx = idxi - bounds [i].lower_bound */
11218 mono_mb_emit_ldarg (mb
, 1 + i
);
11219 mono_mb_emit_ldloc (mb
, bounds
);
11220 mono_mb_emit_icon (mb
, (i
* sizeof (MonoArrayBounds
)) + G_STRUCT_OFFSET (MonoArrayBounds
, lower_bound
));
11221 mono_mb_emit_byte (mb
, CEE_ADD
);
11222 mono_mb_emit_byte (mb
, CEE_LDIND_I4
);
11223 mono_mb_emit_byte (mb
, CEE_SUB
);
11224 mono_mb_emit_stloc (mb
, realidx
);
11225 /* if (realidx >= bounds [i].length) goto exeception; */
11226 mono_mb_emit_ldloc (mb
, realidx
);
11227 mono_mb_emit_ldloc (mb
, bounds
);
11228 mono_mb_emit_icon (mb
, (i
* sizeof (MonoArrayBounds
)) + G_STRUCT_OFFSET (MonoArrayBounds
, length
));
11229 mono_mb_emit_byte (mb
, CEE_ADD
);
11230 mono_mb_emit_byte (mb
, CEE_LDIND_I4
);
11231 branch_positions
[i
] = mono_mb_emit_branch (mb
, CEE_BGE_UN
);
11232 /* ind = ind * bounds [i].length + realidx */
11233 mono_mb_emit_ldloc (mb
, ind
);
11234 mono_mb_emit_ldloc (mb
, bounds
);
11235 mono_mb_emit_icon (mb
, (i
* sizeof (MonoArrayBounds
)) + G_STRUCT_OFFSET (MonoArrayBounds
, length
));
11236 mono_mb_emit_byte (mb
, CEE_ADD
);
11237 mono_mb_emit_byte (mb
, CEE_LDIND_I4
);
11238 mono_mb_emit_byte (mb
, CEE_MUL
);
11239 mono_mb_emit_ldloc (mb
, realidx
);
11240 mono_mb_emit_byte (mb
, CEE_ADD
);
11241 mono_mb_emit_stloc (mb
, ind
);
11244 /* return array->vector + ind * element_size */
11245 mono_mb_emit_ldarg (mb
, 0);
11246 mono_mb_emit_ldflda (mb
, G_STRUCT_OFFSET (MonoArray
, vector
));
11247 mono_mb_emit_ldloc (mb
, ind
);
11248 mono_mb_emit_icon (mb
, elem_size
);
11249 mono_mb_emit_byte (mb
, CEE_MUL
);
11250 mono_mb_emit_byte (mb
, CEE_ADD
);
11251 mono_mb_emit_byte (mb
, CEE_RET
);
11253 /* patch the branches to get here and throw */
11254 for (i
= 1; i
< rank
; ++i
) {
11255 mono_mb_patch_branch (mb
, branch_positions
[i
]);
11257 mono_mb_patch_branch (mb
, branch_pos
);
11258 /* throw exception */
11259 mono_mb_emit_exception (mb
, "IndexOutOfRangeException", NULL
);
11261 g_free (branch_positions
);
11262 #endif /* DISABLE_JIT */
11264 ret
= mono_mb_create_method (mb
, sig
, 4);
11267 /* cache the result */
11269 mono_marshal_lock ();
11270 for (i
= 0; i
< elem_addr_cache_next
; ++i
) {
11271 if (elem_addr_cache
[i
].rank
== rank
&& elem_addr_cache
[i
].elem_size
== elem_size
) {
11272 /* FIXME: free ret */
11273 ret
= elem_addr_cache
[i
].method
;
11281 if (elem_addr_cache_next
>= elem_addr_cache_size
) {
11282 int new_size
= elem_addr_cache_size
+ 4;
11283 ArrayElemAddr
*new_array
= g_new0 (ArrayElemAddr
, new_size
);
11284 memcpy (new_array
, elem_addr_cache
, elem_addr_cache_size
* sizeof (ArrayElemAddr
));
11285 g_free (elem_addr_cache
);
11286 elem_addr_cache
= new_array
;
11287 elem_addr_cache_size
= new_size
;
11289 elem_addr_cache
[elem_addr_cache_next
].rank
= rank
;
11290 elem_addr_cache
[elem_addr_cache_next
].elem_size
= elem_size
;
11291 elem_addr_cache
[elem_addr_cache_next
].method
= ret
;
11292 elem_addr_cache_next
++;
11294 info
= mono_wrapper_info_create (ret
, WRAPPER_SUBTYPE_ELEMENT_ADDR
);
11295 info
->d
.element_addr
.rank
= rank
;
11296 info
->d
.element_addr
.elem_size
= elem_size
;
11297 mono_marshal_set_wrapper_info (ret
, info
);
11299 mono_marshal_unlock ();
11304 mono_marshal_alloc (gulong size
)
11309 res
= CoTaskMemAlloc (size
);
11311 res
= g_try_malloc ((gulong
)size
);
11313 mono_gc_out_of_memory ((gulong
)size
);
11319 mono_marshal_free (gpointer ptr
)
11322 CoTaskMemFree (ptr
);
11329 mono_marshal_free_array (gpointer
*ptr
, int size
)
11336 for (i
= 0; i
< size
; i
++)
11342 mono_marshal_string_to_utf16 (MonoString
*s
)
11344 return s
? mono_string_chars (s
) : NULL
;
11348 mono_marshal_string_to_utf16_copy (MonoString
*s
)
11353 gunichar2
*res
= mono_marshal_alloc ((mono_string_length (s
) * 2) + 2);
11354 memcpy (res
, mono_string_chars (s
), mono_string_length (s
) * 2);
11355 res
[mono_string_length (s
)] = 0;
11361 * mono_marshal_set_last_error:
11363 * This function is invoked to set the last error value from a P/Invoke call
11364 * which has SetLastError set.
11367 mono_marshal_set_last_error (void)
11370 mono_native_tls_set_value (last_error_tls_id
, GINT_TO_POINTER (GetLastError ()));
11372 mono_native_tls_set_value (last_error_tls_id
, GINT_TO_POINTER (errno
));
11377 mono_marshal_set_last_error_windows (int error
)
11380 mono_native_tls_set_value (last_error_tls_id
, GINT_TO_POINTER (error
));
11385 ves_icall_System_Runtime_InteropServices_Marshal_copy_to_unmanaged (MonoArray
*src
, gint32 start_index
,
11386 gpointer dest
, gint32 length
)
11391 MONO_ARCH_SAVE_REGS
;
11393 MONO_CHECK_ARG_NULL (src
);
11394 MONO_CHECK_ARG_NULL (dest
);
11396 if (src
->obj
.vtable
->klass
->rank
!= 1)
11397 mono_raise_exception (mono_get_exception_argument ("array", "array is multi-dimensional"));
11398 if (start_index
< 0)
11399 mono_raise_exception (mono_get_exception_argument ("startIndex", "Must be >= 0"));
11401 mono_raise_exception (mono_get_exception_argument ("length", "Must be >= 0"));
11402 if (start_index
+ length
> mono_array_length (src
))
11403 mono_raise_exception (mono_get_exception_argument ("length", "start_index + length > array length"));
11405 element_size
= mono_array_element_size (src
->obj
.vtable
->klass
);
11407 /* no references should be involved */
11408 source_addr
= mono_array_addr_with_size (src
, element_size
, start_index
);
11410 memcpy (dest
, source_addr
, length
* element_size
);
11414 ves_icall_System_Runtime_InteropServices_Marshal_copy_from_unmanaged (gpointer src
, gint32 start_index
,
11415 MonoArray
*dest
, gint32 length
)
11420 MONO_ARCH_SAVE_REGS
;
11422 MONO_CHECK_ARG_NULL (src
);
11423 MONO_CHECK_ARG_NULL (dest
);
11425 if (dest
->obj
.vtable
->klass
->rank
!= 1)
11426 mono_raise_exception (mono_get_exception_argument ("array", "array is multi-dimensional"));
11427 if (start_index
< 0)
11428 mono_raise_exception (mono_get_exception_argument ("startIndex", "Must be >= 0"));
11430 mono_raise_exception (mono_get_exception_argument ("length", "Must be >= 0"));
11431 if (start_index
+ length
> mono_array_length (dest
))
11432 mono_raise_exception (mono_get_exception_argument ("length", "start_index + length > array length"));
11434 element_size
= mono_array_element_size (dest
->obj
.vtable
->klass
);
11436 /* no references should be involved */
11437 dest_addr
= mono_array_addr_with_size (dest
, element_size
, start_index
);
11439 memcpy (dest_addr
, src
, length
* element_size
);
11442 #if NO_UNALIGNED_ACCESS
11443 #define RETURN_UNALIGNED(type, addr) \
11446 memcpy(&val, p + offset, sizeof(val)); \
11449 #define WRITE_UNALIGNED(type, addr, val) \
11450 memcpy(addr, &val, sizeof(type))
11452 #define RETURN_UNALIGNED(type, addr) \
11453 return *(type*)(p + offset);
11454 #define WRITE_UNALIGNED(type, addr, val) \
11455 (*(type *)(addr) = (val))
11459 ves_icall_System_Runtime_InteropServices_Marshal_ReadIntPtr (gpointer ptr
, gint32 offset
)
11463 MONO_ARCH_SAVE_REGS
;
11465 RETURN_UNALIGNED(gpointer
, p
+ offset
);
11469 ves_icall_System_Runtime_InteropServices_Marshal_ReadByte (gpointer ptr
, gint32 offset
)
11473 MONO_ARCH_SAVE_REGS
;
11475 return *(unsigned char*)(p
+ offset
);
11479 ves_icall_System_Runtime_InteropServices_Marshal_ReadInt16 (gpointer ptr
, gint32 offset
)
11483 MONO_ARCH_SAVE_REGS
;
11485 RETURN_UNALIGNED(gint16
, p
+ offset
);
11489 ves_icall_System_Runtime_InteropServices_Marshal_ReadInt32 (gpointer ptr
, gint32 offset
)
11493 MONO_ARCH_SAVE_REGS
;
11495 RETURN_UNALIGNED(gint32
, p
+ offset
);
11499 ves_icall_System_Runtime_InteropServices_Marshal_ReadInt64 (gpointer ptr
, gint32 offset
)
11503 MONO_ARCH_SAVE_REGS
;
11505 RETURN_UNALIGNED(gint64
, p
+ offset
);
11509 ves_icall_System_Runtime_InteropServices_Marshal_WriteByte (gpointer ptr
, gint32 offset
, unsigned char val
)
11513 MONO_ARCH_SAVE_REGS
;
11515 *(unsigned char*)(p
+ offset
) = val
;
11519 ves_icall_System_Runtime_InteropServices_Marshal_WriteIntPtr (gpointer ptr
, gint32 offset
, gpointer val
)
11523 MONO_ARCH_SAVE_REGS
;
11525 WRITE_UNALIGNED(gpointer
, p
+ offset
, val
);
11529 ves_icall_System_Runtime_InteropServices_Marshal_WriteInt16 (gpointer ptr
, gint32 offset
, gint16 val
)
11533 MONO_ARCH_SAVE_REGS
;
11535 WRITE_UNALIGNED(gint16
, p
+ offset
, val
);
11539 ves_icall_System_Runtime_InteropServices_Marshal_WriteInt32 (gpointer ptr
, gint32 offset
, gint32 val
)
11543 MONO_ARCH_SAVE_REGS
;
11545 WRITE_UNALIGNED(gint32
, p
+ offset
, val
);
11549 ves_icall_System_Runtime_InteropServices_Marshal_WriteInt64 (gpointer ptr
, gint32 offset
, gint64 val
)
11553 MONO_ARCH_SAVE_REGS
;
11555 WRITE_UNALIGNED(gint64
, p
+ offset
, val
);
11559 ves_icall_System_Runtime_InteropServices_Marshal_PtrToStringAnsi (char *ptr
)
11561 MONO_ARCH_SAVE_REGS
;
11566 return mono_string_new (mono_domain_get (), ptr
);
11570 ves_icall_System_Runtime_InteropServices_Marshal_PtrToStringAnsi_len (char *ptr
, gint32 len
)
11572 MONO_ARCH_SAVE_REGS
;
11575 mono_raise_exception (mono_get_exception_argument_null ("ptr"));
11576 g_assert_not_reached ();
11579 return mono_string_new_len (mono_domain_get (), ptr
, len
);
11584 ves_icall_System_Runtime_InteropServices_Marshal_PtrToStringUni (guint16
*ptr
)
11586 MonoDomain
*domain
= mono_domain_get ();
11590 MONO_ARCH_SAVE_REGS
;
11598 return mono_string_new_utf16 (domain
, ptr
, len
);
11602 ves_icall_System_Runtime_InteropServices_Marshal_PtrToStringUni_len (guint16
*ptr
, gint32 len
)
11604 MonoDomain
*domain
= mono_domain_get ();
11606 MONO_ARCH_SAVE_REGS
;
11609 mono_raise_exception (mono_get_exception_argument_null ("ptr"));
11610 g_assert_not_reached ();
11613 return mono_string_new_utf16 (domain
, ptr
, len
);
11618 ves_icall_System_Runtime_InteropServices_Marshal_GetLastWin32Error (void)
11620 MONO_ARCH_SAVE_REGS
;
11622 return (GPOINTER_TO_INT (mono_native_tls_get_value (last_error_tls_id
)));
11626 ves_icall_System_Runtime_InteropServices_Marshal_SizeOf (MonoReflectionType
*rtype
)
11632 MONO_ARCH_SAVE_REGS
;
11634 MONO_CHECK_ARG_NULL (rtype
);
11636 type
= rtype
->type
;
11637 klass
= mono_class_from_mono_type (type
);
11638 if (!mono_class_init (klass
))
11639 mono_raise_exception (mono_class_get_exception_for_failure (klass
));
11641 layout
= (klass
->flags
& TYPE_ATTRIBUTE_LAYOUT_MASK
);
11643 if (layout
== TYPE_ATTRIBUTE_AUTO_LAYOUT
) {
11645 MonoException
*exc
;
11647 msg
= g_strdup_printf ("Type %s cannot be marshaled as an unmanaged structure.", klass
->name
);
11648 exc
= mono_get_exception_argument ("t", msg
);
11650 mono_raise_exception (exc
);
11654 return mono_class_native_size (klass
, NULL
);
11658 ves_icall_System_Runtime_InteropServices_Marshal_StructureToPtr (MonoObject
*obj
, gpointer dst
, MonoBoolean delete_old
)
11660 MonoMethod
*method
;
11663 MONO_ARCH_SAVE_REGS
;
11665 MONO_CHECK_ARG_NULL (obj
);
11666 MONO_CHECK_ARG_NULL (dst
);
11668 method
= mono_marshal_get_struct_to_ptr (obj
->vtable
->klass
);
11672 pa
[2] = &delete_old
;
11674 mono_runtime_invoke (method
, NULL
, pa
, NULL
);
11678 ptr_to_structure (gpointer src
, MonoObject
*dst
)
11680 MonoMethod
*method
;
11683 method
= mono_marshal_get_ptr_to_struct (dst
->vtable
->klass
);
11688 mono_runtime_invoke (method
, NULL
, pa
, NULL
);
11692 ves_icall_System_Runtime_InteropServices_Marshal_PtrToStructure (gpointer src
, MonoObject
*dst
)
11696 MONO_ARCH_SAVE_REGS
;
11698 MONO_CHECK_ARG_NULL (src
);
11699 MONO_CHECK_ARG_NULL (dst
);
11701 t
= mono_type_get_underlying_type (mono_class_get_type (dst
->vtable
->klass
));
11703 if (t
->type
== MONO_TYPE_VALUETYPE
) {
11704 MonoException
*exc
;
11707 tmp
= g_strdup_printf ("Destination is a boxed value type.");
11708 exc
= mono_get_exception_argument ("dst", tmp
);
11711 mono_raise_exception (exc
);
11715 ptr_to_structure (src
, dst
);
11719 ves_icall_System_Runtime_InteropServices_Marshal_PtrToStructure_type (gpointer src
, MonoReflectionType
*type
)
11722 MonoDomain
*domain
= mono_domain_get ();
11725 MONO_ARCH_SAVE_REGS
;
11727 MONO_CHECK_ARG_NULL (src
);
11728 MONO_CHECK_ARG_NULL (type
);
11730 klass
= mono_class_from_mono_type (type
->type
);
11731 if (!mono_class_init (klass
))
11732 mono_raise_exception (mono_class_get_exception_for_failure (klass
));
11734 res
= mono_object_new (domain
, klass
);
11736 ptr_to_structure (src
, res
);
11742 ves_icall_System_Runtime_InteropServices_Marshal_OffsetOf (MonoReflectionType
*type
, MonoString
*field_name
)
11744 MonoMarshalType
*info
;
11747 int match_index
= -1;
11749 MONO_ARCH_SAVE_REGS
;
11751 MONO_CHECK_ARG_NULL (type
);
11752 MONO_CHECK_ARG_NULL (field_name
);
11754 fname
= mono_string_to_utf8 (field_name
);
11755 klass
= mono_class_from_mono_type (type
->type
);
11756 if (!mono_class_init (klass
))
11757 mono_raise_exception (mono_class_get_exception_for_failure (klass
));
11759 while (klass
&& match_index
== -1) {
11760 MonoClassField
* field
;
11762 gpointer iter
= NULL
;
11763 while ((field
= mono_class_get_fields (klass
, &iter
))) {
11764 if (field
->type
->attrs
& FIELD_ATTRIBUTE_STATIC
)
11766 if (!strcmp (fname
, mono_field_get_name (field
))) {
11773 if (match_index
== -1)
11774 klass
= klass
->parent
;
11779 if(match_index
== -1) {
11780 MonoException
* exc
;
11783 /* Get back original class instance */
11784 klass
= mono_class_from_mono_type (type
->type
);
11786 tmp
= g_strdup_printf ("Field passed in is not a marshaled member of the type %s", klass
->name
);
11787 exc
= mono_get_exception_argument ("fieldName", tmp
);
11790 mono_raise_exception ((MonoException
*)exc
);
11793 info
= mono_marshal_load_type_info (klass
);
11794 return info
->fields
[match_index
].offset
;
11798 ves_icall_System_Runtime_InteropServices_Marshal_StringToHGlobalAnsi (MonoString
*string
)
11803 tres
= mono_string_to_utf8 (string
);
11807 len
= strlen (tres
) + 1;
11808 ret
= ves_icall_System_Runtime_InteropServices_Marshal_AllocHGlobal (len
);
11809 memcpy (ret
, tres
, len
);
11814 return mono_string_to_utf8 (string
);
11819 ves_icall_System_Runtime_InteropServices_Marshal_StringToHGlobalUni (MonoString
*string
)
11821 MONO_ARCH_SAVE_REGS
;
11823 if (string
== NULL
)
11826 #ifdef TARGET_WIN32
11827 gunichar2
*res
= ves_icall_System_Runtime_InteropServices_Marshal_AllocHGlobal
11828 ((mono_string_length (string
) + 1) * 2);
11830 gunichar2
*res
= g_malloc ((mono_string_length (string
) + 1) * 2);
11832 memcpy (res
, mono_string_chars (string
), mono_string_length (string
) * 2);
11833 res
[mono_string_length (string
)] = 0;
11839 mono_struct_delete_old (MonoClass
*klass
, char *ptr
)
11841 MonoMarshalType
*info
;
11844 info
= mono_marshal_load_type_info (klass
);
11846 for (i
= 0; i
< info
->num_fields
; i
++) {
11847 MonoMarshalNative ntype
;
11848 MonoMarshalConv conv
;
11849 MonoType
*ftype
= info
->fields
[i
].field
->type
;
11852 if (ftype
->attrs
& FIELD_ATTRIBUTE_STATIC
)
11855 ntype
= mono_type_to_unmanaged (ftype
, info
->fields
[i
].mspec
, TRUE
,
11856 klass
->unicode
, &conv
);
11858 cpos
= ptr
+ info
->fields
[i
].offset
;
11861 case MONO_MARSHAL_CONV_NONE
:
11862 if (MONO_TYPE_ISSTRUCT (ftype
)) {
11863 mono_struct_delete_old (ftype
->data
.klass
, cpos
);
11867 case MONO_MARSHAL_CONV_STR_LPWSTR
:
11868 /* We assume this field points inside a MonoString */
11870 case MONO_MARSHAL_CONV_STR_LPTSTR
:
11871 #ifdef TARGET_WIN32
11872 /* We assume this field points inside a MonoString
11876 case MONO_MARSHAL_CONV_STR_LPSTR
:
11877 case MONO_MARSHAL_CONV_STR_BSTR
:
11878 case MONO_MARSHAL_CONV_STR_ANSIBSTR
:
11879 case MONO_MARSHAL_CONV_STR_TBSTR
:
11880 mono_marshal_free (*(gpointer
*)cpos
);
11890 ves_icall_System_Runtime_InteropServices_Marshal_DestroyStructure (gpointer src
, MonoReflectionType
*type
)
11894 MONO_ARCH_SAVE_REGS
;
11896 MONO_CHECK_ARG_NULL (src
);
11897 MONO_CHECK_ARG_NULL (type
);
11899 klass
= mono_class_from_mono_type (type
->type
);
11900 if (!mono_class_init (klass
))
11901 mono_raise_exception (mono_class_get_exception_for_failure (klass
));
11903 mono_struct_delete_old (klass
, (char *)src
);
11907 ves_icall_System_Runtime_InteropServices_Marshal_AllocHGlobal (int size
)
11911 MONO_ARCH_SAVE_REGS
;
11913 if ((gulong
)size
== 0)
11914 /* This returns a valid pointer for size 0 on MS.NET */
11918 res
= GlobalAlloc (GMEM_FIXED
, (gulong
)size
);
11920 res
= g_try_malloc ((gulong
)size
);
11923 mono_gc_out_of_memory ((gulong
)size
);
11929 ves_icall_System_Runtime_InteropServices_Marshal_ReAllocHGlobal (gpointer ptr
, int size
)
11934 mono_gc_out_of_memory ((gulong
)size
);
11939 res
= GlobalReAlloc (ptr
, (gulong
)size
, GMEM_MOVEABLE
);
11941 res
= g_try_realloc (ptr
, (gulong
)size
);
11944 mono_gc_out_of_memory ((gulong
)size
);
11950 ves_icall_System_Runtime_InteropServices_Marshal_FreeHGlobal (void *ptr
)
11952 MONO_ARCH_SAVE_REGS
;
11962 ves_icall_System_Runtime_InteropServices_Marshal_AllocCoTaskMem (int size
)
11964 MONO_ARCH_SAVE_REGS
;
11967 return CoTaskMemAlloc (size
);
11969 return g_try_malloc ((gulong
)size
);
11974 ves_icall_System_Runtime_InteropServices_Marshal_FreeCoTaskMem (void *ptr
)
11976 MONO_ARCH_SAVE_REGS
;
11979 CoTaskMemFree (ptr
);
11986 ves_icall_System_Runtime_InteropServices_Marshal_ReAllocCoTaskMem (gpointer ptr
, int size
)
11988 MONO_ARCH_SAVE_REGS
;
11991 return CoTaskMemRealloc (ptr
, size
);
11993 return g_try_realloc (ptr
, (gulong
)size
);
11998 ves_icall_System_Runtime_InteropServices_Marshal_UnsafeAddrOfPinnedArrayElement (MonoArray
*arrayobj
, int index
)
12000 return mono_array_addr_with_size (arrayobj
, mono_array_element_size (arrayobj
->obj
.vtable
->klass
), index
);
12004 ves_icall_System_Runtime_InteropServices_Marshal_GetDelegateForFunctionPointerInternal (void *ftn
, MonoReflectionType
*type
)
12006 MonoClass
*klass
= mono_type_get_class (type
->type
);
12007 if (!mono_class_init (klass
))
12008 mono_raise_exception (mono_class_get_exception_for_failure (klass
));
12010 return mono_ftnptr_to_delegate (klass
, ftn
);
12014 * mono_marshal_is_loading_type_info:
12016 * Return whenever mono_marshal_load_type_info () is being executed for KLASS by this
12020 mono_marshal_is_loading_type_info (MonoClass
*klass
)
12022 GSList
*loads_list
= mono_native_tls_get_value (load_type_info_tls_id
);
12024 return g_slist_find (loads_list
, klass
) != NULL
;
12028 * mono_marshal_load_type_info:
12030 * Initialize klass->marshal_info using information from metadata. This function can
12031 * recursively call itself, and the caller is responsible to avoid that by calling
12032 * mono_marshal_is_loading_type_info () beforehand.
12034 * LOCKING: Acquires the loader lock.
12037 mono_marshal_load_type_info (MonoClass
* klass
)
12040 guint32 native_size
= 0, min_align
= 1, packing
;
12041 MonoMarshalType
*info
;
12042 MonoClassField
* field
;
12045 GSList
*loads_list
;
12047 g_assert (klass
!= NULL
);
12049 if (klass
->marshal_info
)
12050 return klass
->marshal_info
;
12052 if (!klass
->inited
)
12053 mono_class_init (klass
);
12055 mono_loader_lock ();
12057 if (klass
->marshal_info
) {
12058 mono_loader_unlock ();
12059 return klass
->marshal_info
;
12063 * This function can recursively call itself, so we keep the list of classes which are
12064 * under initialization in a TLS list.
12066 g_assert (!mono_marshal_is_loading_type_info (klass
));
12067 loads_list
= mono_native_tls_get_value (load_type_info_tls_id
);
12068 loads_list
= g_slist_prepend (loads_list
, klass
);
12069 mono_native_tls_set_value (load_type_info_tls_id
, loads_list
);
12072 while ((field
= mono_class_get_fields (klass
, &iter
))) {
12073 if (field
->type
->attrs
& FIELD_ATTRIBUTE_STATIC
)
12075 if (mono_field_is_deleted (field
))
12080 layout
= klass
->flags
& TYPE_ATTRIBUTE_LAYOUT_MASK
;
12082 /* The mempool is protected by the loader lock */
12083 info
= mono_image_alloc0 (klass
->image
, MONO_SIZEOF_MARSHAL_TYPE
+ sizeof (MonoMarshalField
) * count
);
12084 info
->num_fields
= count
;
12086 /* Try to find a size for this type in metadata */
12087 mono_metadata_packing_from_typedef (klass
->image
, klass
->type_token
, NULL
, &native_size
);
12089 if (klass
->parent
) {
12090 int parent_size
= mono_class_native_size (klass
->parent
, NULL
);
12092 /* Add parent size to real size */
12093 native_size
+= parent_size
;
12094 info
->native_size
= parent_size
;
12097 packing
= klass
->packing_size
? klass
->packing_size
: 8;
12100 while ((field
= mono_class_get_fields (klass
, &iter
))) {
12104 if (field
->type
->attrs
& FIELD_ATTRIBUTE_STATIC
)
12107 if (mono_field_is_deleted (field
))
12109 if (field
->type
->attrs
& FIELD_ATTRIBUTE_HAS_FIELD_MARSHAL
)
12110 mono_metadata_field_info_with_mempool (klass
->image
, mono_metadata_token_index (mono_class_get_field_token (field
)) - 1,
12111 NULL
, NULL
, &info
->fields
[j
].mspec
);
12113 info
->fields
[j
].field
= field
;
12115 if ((mono_class_num_fields (klass
) == 1) && (klass
->instance_size
== sizeof (MonoObject
)) &&
12116 (strcmp (mono_field_get_name (field
), "$PRIVATE$") == 0)) {
12117 /* This field is a hack inserted by MCS to empty structures */
12122 case TYPE_ATTRIBUTE_AUTO_LAYOUT
:
12123 case TYPE_ATTRIBUTE_SEQUENTIAL_LAYOUT
:
12124 size
= mono_marshal_type_size (field
->type
, info
->fields
[j
].mspec
,
12125 &align
, TRUE
, klass
->unicode
);
12126 align
= klass
->packing_size
? MIN (klass
->packing_size
, align
): align
;
12127 min_align
= MAX (align
, min_align
);
12128 info
->fields
[j
].offset
= info
->native_size
;
12129 info
->fields
[j
].offset
+= align
- 1;
12130 info
->fields
[j
].offset
&= ~(align
- 1);
12131 info
->native_size
= info
->fields
[j
].offset
+ size
;
12133 case TYPE_ATTRIBUTE_EXPLICIT_LAYOUT
:
12134 size
= mono_marshal_type_size (field
->type
, info
->fields
[j
].mspec
,
12135 &align
, TRUE
, klass
->unicode
);
12136 min_align
= packing
;
12137 info
->fields
[j
].offset
= field
->offset
- sizeof (MonoObject
);
12138 info
->native_size
= MAX (info
->native_size
, info
->fields
[j
].offset
+ size
);
12144 if (layout
!= TYPE_ATTRIBUTE_AUTO_LAYOUT
) {
12145 info
->native_size
= MAX (native_size
, info
->native_size
);
12147 * If the provided Size is equal or larger than the calculated size, and there
12148 * was no Pack attribute, we set min_align to 1 to avoid native_size being increased
12150 if (layout
== TYPE_ATTRIBUTE_EXPLICIT_LAYOUT
)
12151 if (native_size
&& native_size
== info
->native_size
&& klass
->packing_size
== 0)
12155 if (info
->native_size
& (min_align
- 1)) {
12156 info
->native_size
+= min_align
- 1;
12157 info
->native_size
&= ~(min_align
- 1);
12160 info
->min_align
= min_align
;
12162 /* Update the class's blittable info, if the layouts don't match */
12163 if (info
->native_size
!= mono_class_value_size (klass
, NULL
))
12164 klass
->blittable
= FALSE
;
12166 /* If this is an array type, ensure that we have element info */
12167 if (klass
->rank
&& !mono_marshal_is_loading_type_info (klass
->element_class
)) {
12168 mono_marshal_load_type_info (klass
->element_class
);
12171 loads_list
= mono_native_tls_get_value (load_type_info_tls_id
);
12172 loads_list
= g_slist_remove (loads_list
, klass
);
12173 mono_native_tls_set_value (load_type_info_tls_id
, loads_list
);
12175 /*We do double-checking locking on marshal_info */
12176 mono_memory_barrier ();
12178 klass
->marshal_info
= info
;
12180 mono_loader_unlock ();
12182 return klass
->marshal_info
;
12186 * mono_class_native_size:
12189 * Returns: the native size of an object instance (when marshaled
12190 * to unmanaged code)
12193 mono_class_native_size (MonoClass
*klass
, guint32
*align
)
12195 if (!klass
->marshal_info
) {
12196 if (mono_marshal_is_loading_type_info (klass
)) {
12201 mono_marshal_load_type_info (klass
);
12206 *align
= klass
->marshal_info
->min_align
;
12208 return klass
->marshal_info
->native_size
;
12211 /* __alignof__ returns the preferred alignment of values not the actual alignment used by
12212 the compiler so is wrong e.g. for Linux where doubles are aligned on a 4 byte boundary
12213 but __alignof__ returns 8 - using G_STRUCT_OFFSET works better */
12214 #define ALIGNMENT(type) G_STRUCT_OFFSET(struct { char c; type x; }, x)
12217 * mono_type_native_stack_size:
12218 * @t: the type to return the size it uses on the stack
12220 * Returns: the number of bytes required to hold an instance of this
12221 * type on the native stack
12224 mono_type_native_stack_size (MonoType
*t
, guint32
*align
)
12228 g_assert (t
!= NULL
);
12234 *align
= sizeof (gpointer
);
12235 return sizeof (gpointer
);
12239 case MONO_TYPE_BOOLEAN
:
12240 case MONO_TYPE_CHAR
:
12251 case MONO_TYPE_STRING
:
12252 case MONO_TYPE_OBJECT
:
12253 case MONO_TYPE_CLASS
:
12254 case MONO_TYPE_SZARRAY
:
12255 case MONO_TYPE_PTR
:
12256 case MONO_TYPE_FNPTR
:
12257 case MONO_TYPE_ARRAY
:
12258 *align
= sizeof (gpointer
);
12259 return sizeof (gpointer
);
12264 *align
= ALIGNMENT (gdouble
);
12268 *align
= ALIGNMENT (glong
);
12270 case MONO_TYPE_GENERICINST
:
12271 if (!mono_type_generic_inst_is_valuetype (t
)) {
12272 *align
= sizeof (gpointer
);
12273 return sizeof (gpointer
);
12276 case MONO_TYPE_TYPEDBYREF
:
12277 case MONO_TYPE_VALUETYPE
: {
12279 MonoClass
*klass
= mono_class_from_mono_type (t
);
12281 if (klass
->enumtype
)
12282 return mono_type_native_stack_size (mono_class_enum_basetype (klass
), align
);
12284 size
= mono_class_native_size (klass
, align
);
12285 *align
= *align
+ 3;
12295 g_error ("type 0x%02x unknown", t
->type
);
12301 mono_marshal_type_size (MonoType
*type
, MonoMarshalSpec
*mspec
, guint32
*align
,
12302 gboolean as_field
, gboolean unicode
)
12304 MonoMarshalNative native_type
= mono_type_to_unmanaged (type
, mspec
, as_field
, unicode
, NULL
);
12307 switch (native_type
) {
12308 case MONO_NATIVE_BOOLEAN
:
12311 case MONO_NATIVE_I1
:
12312 case MONO_NATIVE_U1
:
12315 case MONO_NATIVE_I2
:
12316 case MONO_NATIVE_U2
:
12317 case MONO_NATIVE_VARIANTBOOL
:
12320 case MONO_NATIVE_I4
:
12321 case MONO_NATIVE_U4
:
12322 case MONO_NATIVE_ERROR
:
12325 case MONO_NATIVE_I8
:
12326 case MONO_NATIVE_U8
:
12327 *align
= ALIGNMENT(guint64
);
12329 case MONO_NATIVE_R4
:
12332 case MONO_NATIVE_R8
:
12333 *align
= ALIGNMENT(double);
12335 case MONO_NATIVE_INT
:
12336 case MONO_NATIVE_UINT
:
12337 case MONO_NATIVE_LPSTR
:
12338 case MONO_NATIVE_LPWSTR
:
12339 case MONO_NATIVE_LPTSTR
:
12340 case MONO_NATIVE_BSTR
:
12341 case MONO_NATIVE_ANSIBSTR
:
12342 case MONO_NATIVE_TBSTR
:
12343 case MONO_NATIVE_LPARRAY
:
12344 case MONO_NATIVE_SAFEARRAY
:
12345 case MONO_NATIVE_IUNKNOWN
:
12346 case MONO_NATIVE_IDISPATCH
:
12347 case MONO_NATIVE_INTERFACE
:
12348 case MONO_NATIVE_ASANY
:
12349 case MONO_NATIVE_FUNC
:
12350 case MONO_NATIVE_LPSTRUCT
:
12351 *align
= ALIGNMENT(gpointer
);
12352 return sizeof (gpointer
);
12353 case MONO_NATIVE_STRUCT
:
12354 klass
= mono_class_from_mono_type (type
);
12355 if (klass
== mono_defaults
.object_class
&&
12356 (mspec
&& mspec
->native
== MONO_NATIVE_STRUCT
)) {
12360 return mono_class_native_size (klass
, align
);
12361 case MONO_NATIVE_BYVALTSTR
: {
12362 int esize
= unicode
? 2: 1;
12365 return mspec
->data
.array_data
.num_elem
* esize
;
12367 case MONO_NATIVE_BYVALARRAY
: {
12368 // FIXME: Have to consider ArraySubType
12370 klass
= mono_class_from_mono_type (type
);
12371 if (klass
->element_class
== mono_defaults
.char_class
) {
12372 esize
= unicode
? 2 : 1;
12375 esize
= mono_class_native_size (klass
->element_class
, align
);
12378 return mspec
->data
.array_data
.num_elem
* esize
;
12380 case MONO_NATIVE_CUSTOM
:
12381 *align
= sizeof (gpointer
);
12382 return sizeof (gpointer
);
12384 case MONO_NATIVE_CURRENCY
:
12385 case MONO_NATIVE_VBBYREFSTR
:
12387 g_error ("native type %02x not implemented", native_type
);
12390 g_assert_not_reached ();
12395 mono_marshal_asany (MonoObject
*o
, MonoMarshalNative string_encoding
, int param_attrs
)
12403 t
= &o
->vtable
->klass
->byval_arg
;
12407 case MONO_TYPE_PTR
:
12410 case MONO_TYPE_BOOLEAN
:
12413 case MONO_TYPE_CHAR
:
12418 return mono_object_unbox (o
);
12420 case MONO_TYPE_STRING
:
12421 switch (string_encoding
) {
12422 case MONO_NATIVE_LPWSTR
:
12423 return mono_marshal_string_to_utf16_copy ((MonoString
*)o
);
12425 case MONO_NATIVE_LPSTR
:
12426 return mono_string_to_lpstr ((MonoString
*)o
);
12429 g_warning ("marshaling conversion %d not implemented", string_encoding
);
12430 g_assert_not_reached ();
12433 case MONO_TYPE_CLASS
:
12434 case MONO_TYPE_VALUETYPE
: {
12435 MonoMethod
*method
;
12438 MonoBoolean delete_old
= FALSE
;
12440 klass
= t
->data
.klass
;
12442 if ((klass
->flags
& TYPE_ATTRIBUTE_LAYOUT_MASK
) == TYPE_ATTRIBUTE_AUTO_LAYOUT
)
12445 if (klass
->valuetype
&& (((klass
->flags
& TYPE_ATTRIBUTE_LAYOUT_MASK
) == TYPE_ATTRIBUTE_EXPLICIT_LAYOUT
) ||
12446 klass
->blittable
|| klass
->enumtype
))
12447 return mono_object_unbox (o
);
12449 res
= mono_marshal_alloc (mono_class_native_size (klass
, NULL
));
12451 if (!((param_attrs
& PARAM_ATTRIBUTE_OUT
) && !(param_attrs
& PARAM_ATTRIBUTE_IN
))) {
12452 method
= mono_marshal_get_struct_to_ptr (o
->vtable
->klass
);
12456 pa
[2] = &delete_old
;
12458 mono_runtime_invoke (method
, NULL
, pa
, NULL
);
12465 mono_raise_exception (mono_get_exception_argument ("", "No PInvoke conversion exists for value passed to Object-typed parameter."));
12471 mono_marshal_free_asany (MonoObject
*o
, gpointer ptr
, MonoMarshalNative string_encoding
, int param_attrs
)
12479 t
= &o
->vtable
->klass
->byval_arg
;
12481 case MONO_TYPE_STRING
:
12482 switch (string_encoding
) {
12483 case MONO_NATIVE_LPWSTR
:
12484 case MONO_NATIVE_LPSTR
:
12485 mono_marshal_free (ptr
);
12488 g_warning ("marshaling conversion %d not implemented", string_encoding
);
12489 g_assert_not_reached ();
12492 case MONO_TYPE_CLASS
:
12493 case MONO_TYPE_VALUETYPE
: {
12494 klass
= t
->data
.klass
;
12496 if (klass
->valuetype
&& (((klass
->flags
& TYPE_ATTRIBUTE_LAYOUT_MASK
) == TYPE_ATTRIBUTE_EXPLICIT_LAYOUT
) ||
12497 klass
->blittable
|| klass
->enumtype
))
12500 if (param_attrs
& PARAM_ATTRIBUTE_OUT
) {
12501 MonoMethod
*method
= mono_marshal_get_ptr_to_struct (o
->vtable
->klass
);
12507 mono_runtime_invoke (method
, NULL
, pa
, NULL
);
12510 if (!((param_attrs
& PARAM_ATTRIBUTE_OUT
) && !(param_attrs
& PARAM_ATTRIBUTE_IN
))) {
12511 mono_struct_delete_old (klass
, ptr
);
12514 mono_marshal_free (ptr
);
12523 mono_marshal_get_generic_array_helper (MonoClass
*class, MonoClass
*iface
, gchar
*name
, MonoMethod
*method
)
12525 MonoMethodSignature
*sig
, *csig
;
12526 MonoMethodBuilder
*mb
;
12531 mb
= mono_mb_new_no_dup_name (class, name
, MONO_WRAPPER_MANAGED_TO_MANAGED
);
12532 mb
->method
->slot
= -1;
12534 mb
->method
->flags
= METHOD_ATTRIBUTE_PRIVATE
| METHOD_ATTRIBUTE_VIRTUAL
|
12535 METHOD_ATTRIBUTE_NEW_SLOT
| METHOD_ATTRIBUTE_HIDE_BY_SIG
| METHOD_ATTRIBUTE_FINAL
;
12537 sig
= mono_method_signature (method
);
12538 csig
= signature_dup (method
->klass
->image
, sig
);
12539 csig
->generic_param_count
= 0;
12541 #ifndef DISABLE_JIT
12542 mono_mb_emit_ldarg (mb
, 0);
12543 for (i
= 0; i
< csig
->param_count
; i
++)
12544 mono_mb_emit_ldarg (mb
, i
+ 1);
12545 mono_mb_emit_managed_call (mb
, method
, NULL
);
12546 mono_mb_emit_byte (mb
, CEE_RET
);
12548 /* We can corlib internal methods */
12549 mb
->skip_visibility
= TRUE
;
12552 res
= mono_mb_create_method (mb
, csig
, csig
->param_count
+ 16);
12554 info
= mono_wrapper_info_create (res
, WRAPPER_SUBTYPE_GENERIC_ARRAY_HELPER
);
12555 info
->d
.generic_array_helper
.method
= method
;
12556 mono_marshal_set_wrapper_info (res
, info
);
12564 * The mono_win32_compat_* functions are implementations of inline
12565 * Windows kernel32 APIs, which are DllImport-able under MS.NET,
12566 * although not exported by kernel32.
12568 * We map the appropiate kernel32 entries to these functions using
12569 * dllmaps declared in the global etc/mono/config.
12573 mono_win32_compat_CopyMemory (gpointer dest
, gconstpointer source
, gsize length
)
12575 if (!dest
|| !source
)
12578 memcpy (dest
, source
, length
);
12582 mono_win32_compat_FillMemory (gpointer dest
, gsize length
, guchar fill
)
12584 memset (dest
, fill
, length
);
12588 mono_win32_compat_MoveMemory (gpointer dest
, gconstpointer source
, gsize length
)
12590 if (!dest
|| !source
)
12593 memmove (dest
, source
, length
);
12597 mono_win32_compat_ZeroMemory (gpointer dest
, gsize length
)
12599 memset (dest
, 0, length
);
12603 mono_marshal_find_nonzero_bit_offset (guint8
*buf
, int len
, int *byte_offset
, guint8
*bitmask
)
12608 for (i
= 0; i
< len
; ++i
)
12612 g_assert (i
< len
);
12615 while (byte
&& !(byte
& 1))
12617 g_assert (byte
== 1);
12620 *bitmask
= buf
[i
];
12624 mono_marshal_get_thunk_invoke_wrapper (MonoMethod
*method
)
12626 MonoMethodBuilder
*mb
;
12627 MonoMethodSignature
*sig
, *csig
;
12628 MonoExceptionClause
*clause
;
12633 int i
, param_count
, sig_size
, pos_leave
;
12637 klass
= method
->klass
;
12638 image
= method
->klass
->image
;
12639 cache
= get_cache (&image
->thunk_invoke_cache
, mono_aligned_addr_hash
, NULL
);
12641 if ((res
= mono_marshal_find_in_cache (cache
, method
)))
12644 sig
= mono_method_signature (method
);
12645 mb
= mono_mb_new (klass
, method
->name
, MONO_WRAPPER_NATIVE_TO_MANAGED
);
12647 /* add "this" and exception param */
12648 param_count
= sig
->param_count
+ sig
->hasthis
+ 1;
12650 /* dup & extend signature */
12651 csig
= mono_metadata_signature_alloc (image
, param_count
);
12652 sig_size
= MONO_SIZEOF_METHOD_SIGNATURE
+ sig
->param_count
* sizeof (MonoType
*);
12653 memcpy (csig
, sig
, sig_size
);
12654 csig
->param_count
= param_count
;
12657 csig
->call_convention
= MONO_CALL_DEFAULT
;
12659 if (sig
->hasthis
) {
12661 csig
->params
[0] = &klass
->byval_arg
;
12662 /* move params up by one */
12663 for (i
= 0; i
< sig
->param_count
; i
++)
12664 csig
->params
[i
+ 1] = sig
->params
[i
];
12667 /* setup exception param as byref+[out] */
12668 csig
->params
[param_count
- 1] = mono_metadata_type_dup (image
,
12669 &mono_defaults
.exception_class
->byval_arg
);
12670 csig
->params
[param_count
- 1]->byref
= 1;
12671 csig
->params
[param_count
- 1]->attrs
= PARAM_ATTRIBUTE_OUT
;
12673 /* convert struct return to object */
12674 if (MONO_TYPE_ISSTRUCT (sig
->ret
))
12675 csig
->ret
= &mono_defaults
.object_class
->byval_arg
;
12677 #ifndef DISABLE_JIT
12678 /* local 0 (temp for exception object) */
12679 mono_mb_add_local (mb
, &mono_defaults
.object_class
->byval_arg
);
12681 /* local 1 (temp for result) */
12682 if (!MONO_TYPE_IS_VOID (sig
->ret
))
12683 mono_mb_add_local (mb
, sig
->ret
);
12685 /* clear exception arg */
12686 mono_mb_emit_ldarg (mb
, param_count
- 1);
12687 mono_mb_emit_byte (mb
, CEE_LDNULL
);
12688 mono_mb_emit_byte (mb
, CEE_STIND_REF
);
12691 clause
= mono_image_alloc0 (image
, sizeof (MonoExceptionClause
));
12692 clause
->try_offset
= mono_mb_get_label (mb
);
12694 /* push method's args */
12695 for (i
= 0; i
< param_count
- 1; i
++) {
12699 mono_mb_emit_ldarg (mb
, i
);
12701 /* get the byval type of the param */
12702 klass
= mono_class_from_mono_type (csig
->params
[i
]);
12703 type
= &klass
->byval_arg
;
12705 /* unbox struct args */
12706 if (MONO_TYPE_ISSTRUCT (type
)) {
12707 mono_mb_emit_op (mb
, CEE_UNBOX
, klass
);
12709 /* byref args & and the "this" arg must remain a ptr.
12710 Otherwise make a copy of the value type */
12711 if (!(csig
->params
[i
]->byref
|| (i
== 0 && sig
->hasthis
)))
12712 mono_mb_emit_op (mb
, CEE_LDOBJ
, klass
);
12714 csig
->params
[i
] = &mono_defaults
.object_class
->byval_arg
;
12719 if (method
->flags
& METHOD_ATTRIBUTE_VIRTUAL
)
12720 mono_mb_emit_op (mb
, CEE_CALLVIRT
, method
);
12722 mono_mb_emit_op (mb
, CEE_CALL
, method
);
12724 /* save result at local 1 */
12725 if (!MONO_TYPE_IS_VOID (sig
->ret
))
12726 mono_mb_emit_stloc (mb
, 1);
12728 pos_leave
= mono_mb_emit_branch (mb
, CEE_LEAVE
);
12731 clause
->flags
= MONO_EXCEPTION_CLAUSE_NONE
;
12732 clause
->try_len
= mono_mb_get_pos (mb
) - clause
->try_offset
;
12733 clause
->data
.catch_class
= mono_defaults
.object_class
;
12735 clause
->handler_offset
= mono_mb_get_label (mb
);
12737 /* store exception at local 0 */
12738 mono_mb_emit_stloc (mb
, 0);
12739 mono_mb_emit_ldarg (mb
, param_count
- 1);
12740 mono_mb_emit_ldloc (mb
, 0);
12741 mono_mb_emit_byte (mb
, CEE_STIND_REF
);
12742 mono_mb_emit_branch (mb
, CEE_LEAVE
);
12744 clause
->handler_len
= mono_mb_get_pos (mb
) - clause
->handler_offset
;
12746 mono_mb_set_clauses (mb
, 1, clause
);
12748 mono_mb_patch_branch (mb
, pos_leave
);
12751 if (!MONO_TYPE_IS_VOID (sig
->ret
)) {
12752 mono_mb_emit_ldloc (mb
, 1);
12754 /* box the return value */
12755 if (MONO_TYPE_ISSTRUCT (sig
->ret
))
12756 mono_mb_emit_op (mb
, CEE_BOX
, mono_class_from_mono_type (sig
->ret
));
12759 mono_mb_emit_byte (mb
, CEE_RET
);
12762 res
= mono_mb_create_and_cache (cache
, method
, mb
, csig
, param_count
+ 16);
12769 * mono_marshal_free_dynamic_wrappers:
12771 * Free wrappers of the dynamic method METHOD.
12774 mono_marshal_free_dynamic_wrappers (MonoMethod
*method
)
12776 MonoImage
*image
= method
->klass
->image
;
12778 g_assert (method
->dynamic
);
12780 /* This could be called during shutdown */
12781 if (marshal_mutex_initialized
)
12782 mono_marshal_lock ();
12784 * FIXME: We currently leak the wrappers. Freeing them would be tricky as
12785 * they could be shared with other methods ?
12787 if (image
->runtime_invoke_direct_cache
)
12788 g_hash_table_remove (image
->runtime_invoke_direct_cache
, method
);
12789 if (image
->delegate_bound_static_invoke_cache
)
12790 g_hash_table_foreach_remove (image
->delegate_bound_static_invoke_cache
, signature_method_pair_matches_method
, method
);
12791 if (image
->delegate_abstract_invoke_cache
)
12792 g_hash_table_foreach_remove (image
->delegate_abstract_invoke_cache
, signature_method_pair_matches_method
, method
);
12794 if (marshal_mutex_initialized
)
12795 mono_marshal_unlock ();
12799 signature_method_pair_matches_signature (gpointer key
, gpointer value
, gpointer user_data
)
12801 SignatureMethodPair
*pair
= (SignatureMethodPair
*)key
;
12802 MonoMethodSignature
*sig
= (MonoMethodSignature
*)user_data
;
12804 return mono_metadata_signature_equal (pair
->sig
, sig
);
12808 * mono_marshal_free_inflated_wrappers:
12810 * Free wrappers of the inflated method METHOD.
12813 mono_marshal_free_inflated_wrappers (MonoMethod
*method
)
12815 MonoMethodSignature
*sig
= method
->signature
;
12817 g_assert (method
->is_inflated
);
12819 /* Ignore calls occuring late during cleanup. */
12820 if (!marshal_mutex_initialized
)
12823 mono_marshal_lock ();
12825 * FIXME: We currently leak the wrappers. Freeing them would be tricky as
12826 * they could be shared with other methods ?
12830 * indexed by MonoMethodSignature
12832 /* FIXME: This could remove unrelated wrappers as well */
12833 if (sig
&& method
->klass
->image
->delegate_begin_invoke_cache
)
12834 g_hash_table_remove (method
->klass
->image
->delegate_begin_invoke_cache
, sig
);
12835 if (sig
&& method
->klass
->image
->delegate_end_invoke_cache
)
12836 g_hash_table_remove (method
->klass
->image
->delegate_end_invoke_cache
, sig
);
12837 if (sig
&& method
->klass
->image
->delegate_invoke_cache
)
12838 g_hash_table_remove (method
->klass
->image
->delegate_invoke_cache
, sig
);
12839 if (sig
&& method
->klass
->image
->runtime_invoke_cache
)
12840 g_hash_table_remove (method
->klass
->image
->runtime_invoke_cache
, sig
);
12843 * indexed by SignatureMethodPair
12845 if (sig
&& method
->klass
->image
->delegate_abstract_invoke_cache
)
12846 g_hash_table_foreach_remove (method
->klass
->image
->delegate_abstract_invoke_cache
,
12847 signature_method_pair_matches_signature
, (gpointer
)sig
);
12849 if (sig
&& method
->klass
->image
->delegate_bound_static_invoke_cache
)
12850 g_hash_table_foreach_remove (method
->klass
->image
->delegate_bound_static_invoke_cache
,
12851 signature_method_pair_matches_signature
, (gpointer
)sig
);
12854 * indexed by MonoMethod pointers
12856 if (method
->klass
->image
->runtime_invoke_direct_cache
)
12857 g_hash_table_remove (method
->klass
->image
->runtime_invoke_direct_cache
, method
);
12858 if (method
->klass
->image
->managed_wrapper_cache
)
12859 g_hash_table_remove (method
->klass
->image
->managed_wrapper_cache
, method
);
12860 if (method
->klass
->image
->native_wrapper_cache
)
12861 g_hash_table_remove (method
->klass
->image
->native_wrapper_cache
, method
);
12862 if (method
->klass
->image
->remoting_invoke_cache
)
12863 g_hash_table_remove (method
->klass
->image
->remoting_invoke_cache
, method
);
12864 if (method
->klass
->image
->synchronized_cache
)
12865 g_hash_table_remove (method
->klass
->image
->synchronized_cache
, method
);
12866 if (method
->klass
->image
->unbox_wrapper_cache
)
12867 g_hash_table_remove (method
->klass
->image
->unbox_wrapper_cache
, method
);
12868 if (method
->klass
->image
->cominterop_invoke_cache
)
12869 g_hash_table_remove (method
->klass
->image
->cominterop_invoke_cache
, method
);
12870 if (method
->klass
->image
->cominterop_wrapper_cache
)
12871 g_hash_table_remove (method
->klass
->image
->cominterop_wrapper_cache
, method
);
12872 if (method
->klass
->image
->thunk_invoke_cache
)
12873 g_hash_table_remove (method
->klass
->image
->thunk_invoke_cache
, method
);
12874 if (method
->klass
->image
->native_func_wrapper_aot_cache
)
12875 g_hash_table_remove (method
->klass
->image
->native_func_wrapper_aot_cache
, method
);
12877 mono_marshal_unlock ();