#include "mono/metadata/threadpool.h"
#include "mono/metadata/monitor.h"
#include <string.h>
+#include <errno.h>
//#define DEBUG_RUNTIME_CODE
};
#endif
+/* This mutex protects the various marshalling related caches in MonoImage */
+static CRITICAL_SECTION marshal_mutex;
+
+/* Maps wrapper methods to the methods they wrap */
+static MonoGHashTable *wrapper_hash;
+
+static guint32 last_error_tls_id;
+
+void
+mono_marshal_init (void)
+{
+ static gboolean module_initialized = FALSE;
+
+ if (!module_initialized) {
+ module_initialized = TRUE;
+ InitializeCriticalSection (&marshal_mutex);
+ wrapper_hash = mono_g_hash_table_new (NULL, NULL);
+ last_error_tls_id = TlsAlloc ();
+ }
+}
+
gpointer
mono_delegate_to_ftnptr (MonoDelegate *delegate)
{
void
mono_mb_emit_ldstr (MonoMethodBuilder *mb, char *str)
{
- mono_mb_emit_byte (mb, CEE_LDSTR);
+ mono_mb_emit_byte (mb, CEE_LDSTR);
mono_mb_emit_i4 (mb, mono_mb_add_data (mb, str));
}
}
}
+guint32
+mono_mb_emit_branch (MonoMethodBuilder *mb, guint8 op)
+{
+ guint32 res;
+ mono_mb_emit_byte (mb, op);
+ res = mb->pos;
+ mono_mb_emit_i4 (mb, 0);
+ return res;
+}
+
void
mono_mb_emit_managed_call (MonoMethodBuilder *mb, MonoMethod *method, MonoMethodSignature *opt_sig)
{
}
void
-mono_mb_emit_exception (MonoMethodBuilder *mb)
+mono_mb_emit_exception (MonoMethodBuilder *mb, const char *exc_name)
{
/* fixme: we need a better way to throw exception,
* supporting several exception types and messages */
- static MonoMethod *missing_method_ctor = NULL;
+ MonoMethod *ctor = NULL;
- if (!missing_method_ctor) {
- MonoClass *mme = mono_class_from_name (mono_defaults.corlib, "System", "MissingMethodException");
- int i;
- mono_class_init (mme);
- for (i = 0; i < mme->method.count; ++i) {
- if (strcmp (mme->methods [i]->name, ".ctor") == 0 && mme->methods [i]->signature->param_count == 0) {
- missing_method_ctor = mme->methods [i];
- break;
- }
+ MonoClass *mme = mono_class_from_name (mono_defaults.corlib, "System", exc_name);
+ int i;
+ mono_class_init (mme);
+ for (i = 0; i < mme->method.count; ++i) {
+ if (strcmp (mme->methods [i]->name, ".ctor") == 0 && mme->methods [i]->signature->param_count == 0) {
+ ctor = mme->methods [i];
+ break;
}
}
+ g_assert (ctor);
mono_mb_emit_byte (mb, CEE_NEWOBJ);
- mono_mb_emit_i4 (mb, mono_mb_add_data (mb, missing_method_ctor));
+ mono_mb_emit_i4 (mb, mono_mb_add_data (mb, ctor));
mono_mb_emit_byte (mb, CEE_THROW);
}
{
switch (conv) {
case MONO_MARSHAL_CONV_BOOL_I4:
+ mono_mb_emit_byte (mb, CEE_LDLOC_1);
mono_mb_emit_byte (mb, CEE_LDLOC_0);
- mono_mb_emit_byte (mb, CEE_LDIND_I);
+ mono_mb_emit_byte (mb, CEE_LDIND_I4);
mono_mb_emit_byte (mb, CEE_BRFALSE_S);
- mono_mb_emit_byte (mb, 5);
- mono_mb_emit_byte (mb, CEE_LDLOC_1);
+ mono_mb_emit_byte (mb, 3);
mono_mb_emit_byte (mb, CEE_LDC_I4_1);
- mono_mb_emit_byte (mb, CEE_STIND_I1);
mono_mb_emit_byte (mb, CEE_BR_S);
- mono_mb_emit_byte (mb, 3);
+ mono_mb_emit_byte (mb, 1);
+ mono_mb_emit_byte (mb, CEE_LDC_I4_0);
+ mono_mb_emit_byte (mb, CEE_STIND_I1);
+ break;
+ case MONO_MARSHAL_CONV_BOOL_VARIANTBOOL:
mono_mb_emit_byte (mb, CEE_LDLOC_1);
+ mono_mb_emit_byte (mb, CEE_LDLOC_0);
+ mono_mb_emit_byte (mb, CEE_LDIND_I2);
+ mono_mb_emit_byte (mb, CEE_BRFALSE_S);
+ mono_mb_emit_byte (mb, 3);
+ mono_mb_emit_byte (mb, CEE_LDC_I4_1);
+ mono_mb_emit_byte (mb, CEE_BR_S);
+ mono_mb_emit_byte (mb, 1);
mono_mb_emit_byte (mb, CEE_LDC_I4_0);
mono_mb_emit_byte (mb, CEE_STIND_I1);
break;
int esize;
if (type->type == MONO_TYPE_SZARRAY) {
- eclass = mono_class_from_mono_type (type->data.type);
+ eclass = type->data.klass;
} else {
g_assert_not_reached ();
}
// know if thats the correct behaviour
break;
}
+ case MONO_MARSHAL_CONV_ARRAY_LPARRAY:
+ g_error ("Structure field of type %s can't be marshalled as LPArray", mono_class_from_mono_type (type)->name);
+ break;
case MONO_MARSHAL_CONV_STR_LPWSTR:
case MONO_MARSHAL_CONV_STR_BSTR:
case MONO_MARSHAL_CONV_STR_ANSIBSTR:
case MONO_MARSHAL_CONV_STR_TBSTR:
case MONO_MARSHAL_CONV_ARRAY_SAVEARRAY:
- case MONO_MARSHAL_CONV_ARRAY_LPARRAY:
case MONO_MARSHAL_CONV_STR_BYVALWSTR:
- case MONO_MARSHAL_CONV_BOOL_VARIANTBOOL:
default:
g_warning ("marshaling conversion %d not implemented", conv);
g_assert_not_reached ();
mono_mb_emit_byte (mb, CEE_LDIND_U1);
mono_mb_emit_byte (mb, CEE_STIND_I4);
break;
+ case MONO_MARSHAL_CONV_BOOL_VARIANTBOOL:
+ mono_mb_emit_byte (mb, CEE_LDLOC_1);
+ mono_mb_emit_byte (mb, CEE_LDLOC_0);
+ mono_mb_emit_byte (mb, CEE_LDIND_U1);
+ mono_mb_emit_byte (mb, CEE_NEG);
+ mono_mb_emit_byte (mb, CEE_STIND_I2);
+ break;
case MONO_MARSHAL_CONV_STR_LPWSTR:
case MONO_MARSHAL_CONV_STR_LPSTR:
case MONO_MARSHAL_CONV_STR_LPTSTR:
int esize;
if (type->type == MONO_TYPE_SZARRAY) {
- eclass = mono_class_from_mono_type (type->data.type);
+ eclass = type->data.klass;
} else {
g_assert_not_reached ();
}
- if (eclass->valuetype)
- esize = mono_class_native_size (eclass, NULL);
- else
- esize = sizeof (gpointer);
+ if (eclass->valuetype)
+ esize = mono_class_native_size (eclass, NULL);
+ else
+ esize = sizeof (gpointer);
if (!usize)
break;
mono_mb_patch_addr_s (mb, pos, mb->pos - pos - 1);
break;
}
- case MONO_MARSHAL_CONV_BOOL_VARIANTBOOL:
default:
g_warning ("marshalling conversion %d not implemented", conv);
g_assert_not_reached ();
info = mono_marshal_load_type_info (klass);
+ if (info->native_size == 0)
+ return;
+
if (klass->blittable) {
mono_mb_emit_byte (mb, CEE_LDLOC_1);
mono_mb_emit_byte (mb, CEE_LDLOC_0);
msize = klass->instance_size - info->fields [i].field->offset;
usize = info->native_size - info->fields [i].offset;
} else {
- msize = klass->fields [i + 1].offset - info->fields [i].field->offset;
+ msize = info->fields [i + 1].field->offset - info->fields [i].field->offset;
usize = info->fields [i + 1].offset - info->fields [i].offset;
}
g_assert ((msize >= 0) && (usize >= 0));
break;
case MONO_TYPE_I2:
case MONO_TYPE_U2:
+ case MONO_TYPE_CHAR:
mono_mb_emit_byte (mb, CEE_LDLOC_1);
mono_mb_emit_byte (mb, CEE_LDLOC_0);
mono_mb_emit_byte (mb, CEE_LDIND_I2);
g_assert (delegate);
+ if (delegate->target && mono_object_class (delegate->target) == mono_defaults.transparent_proxy_class) {
+
+ MonoTransparentProxy* tp = (MonoTransparentProxy *)delegate->target;
+ if (!tp->klass->contextbound || tp->rp->context != (MonoObject *) mono_context_get ()) {
+
+ // If the target is a proxy, make a direct call. Is proxy's work
+ // to make the call asynchronous.
+
+ MonoAsyncResult *ares;
+ MonoObject *exc;
+ MonoArray *out_args;
+ HANDLE handle;
+ method = delegate->method_info->method;
+
+ msg = mono_method_call_message_new (method, params, NULL, &async_callback, &state);
+ handle = CreateEvent (NULL, TRUE, FALSE, NULL);
+ ares = mono_async_result_new (mono_domain_get (), handle, state, handle);
+ ares->async_delegate = (MonoObject *)async_callback;
+ msg->async_result = ares;
+ msg->call_type = CallType_BeginInvoke;
+
+ mono_remoting_invoke ((MonoObject *)tp->rp, msg, &exc, &out_args);
+ return ares;
+ }
+ }
+
klass = delegate->object.vtable->klass;
method = mono_get_delegate_invoke (klass);
g_assert (method != NULL);
im = mono_get_delegate_invoke (method->klass);
-
msg = mono_method_call_message_new (method, params, im, &async_callback, &state);
return mono_thread_pool_add ((MonoObject *)delegate, msg, async_callback, state);
return result;
}
+static inline MonoMethod*
+mono_marshal_find_in_cache (GHashTable *cache, gpointer key)
+{
+ MonoMethod *res;
+
+ EnterCriticalSection (&marshal_mutex);
+ res = g_hash_table_lookup (cache, key);
+ LeaveCriticalSection (&marshal_mutex);
+ return res;
+}
+
+/* Create the method from the builder and place it in the cache */
+static inline MonoMethod*
+mono_mb_create_and_cache (GHashTable *cache, gpointer key,
+ MonoMethodBuilder *mb, MonoMethodSignature *sig,
+ int max_stack)
+{
+ MonoMethod *res;
+
+ EnterCriticalSection (&marshal_mutex);
+ res = g_hash_table_lookup (cache, key);
+ if (!res) {
+ /* This does not acquire any locks */
+ res = mono_mb_create_method (mb, sig, max_stack);
+ g_hash_table_insert (cache, key, res);
+ mono_g_hash_table_insert (wrapper_hash, res, key);
+ }
+ else
+ /* Somebody created it before us */
+ ;
+ LeaveCriticalSection (&marshal_mutex);
+
+ return res;
+}
+
+MonoMethod *
+mono_marshal_method_from_wrapper (MonoMethod *wrapper)
+{
+ MonoMethod *res;
+
+ g_assert (
+ (wrapper->wrapper_type == MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK) ||
+ (wrapper->wrapper_type == MONO_WRAPPER_SYNCHRONIZED));
+
+ EnterCriticalSection (&marshal_mutex);
+ res = mono_g_hash_table_lookup (wrapper_hash, wrapper);
+ LeaveCriticalSection (&marshal_mutex);
+
+ if (wrapper->wrapper_type == MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK)
+ /* See mono_marshal_get_remoting_invoke_with_check */
+ return (MonoMethod*)((char*)res - 1);
+ else
+ return res;
+}
+
MonoMethod *
mono_marshal_get_delegate_begin_invoke (MonoMethod *method)
{
sig = method->signature;
cache = method->klass->image->delegate_begin_invoke_cache;
- if ((res = (MonoMethod *)g_hash_table_lookup (cache, sig)))
+ if ((res = mono_marshal_find_in_cache (cache, sig)))
return res;
g_assert (sig->hasthis);
mono_mb_emit_native_call (mb, csig, mono_delegate_begin_invoke);
mono_mb_emit_byte (mb, CEE_RET);
- res = mono_mb_create_method (mb, sig, sig->param_count + 16);
+ res = mono_mb_create_and_cache (cache, sig, mb, sig, sig->param_count + 16);
mono_mb_free (mb);
- g_hash_table_insert (cache, sig, res);
return res;
}
ares = mono_array_get (msg->args, gpointer, sig->param_count - 1);
g_assert (ares);
- res = mono_thread_pool_finish (ares, &out_args, &exc);
+ if (delegate->target && mono_object_class (delegate->target) == mono_defaults.transparent_proxy_class) {
+ MonoTransparentProxy* tp = (MonoTransparentProxy *)delegate->target;
+ msg = (MonoMethodMessage *)mono_object_new (domain, mono_defaults.mono_method_message_class);
+ mono_message_init (domain, msg, delegate->method_info, NULL);
+ msg->call_type = CallType_EndInvoke;
+ msg->async_result = ares;
+ res = mono_remoting_invoke ((MonoObject *)tp->rp, msg, &exc, &out_args);
+ }
+ else
+ res = mono_thread_pool_finish (ares, &out_args, &exc);
if (exc) {
char *strace = mono_string_to_utf8 (((MonoException*)exc)->stack_trace);
sig = method->signature;
cache = method->klass->image->delegate_end_invoke_cache;
- if ((res = (MonoMethod *)g_hash_table_lookup (cache, sig)))
+ if ((res = mono_marshal_find_in_cache (cache, sig)))
return res;
g_assert (sig->hasthis);
} else
mono_mb_emit_restore_result (mb, sig->ret);
- res = mono_mb_create_method (mb, sig, sig->param_count + 16);
+ res = mono_mb_create_and_cache (cache, sig,
+ mb, sig, sig->param_count + 16);
mono_mb_free (mb);
- g_hash_table_insert (cache, sig, res);
return res;
}
int i;
MonoMethodSignature *sig = method->signature;
int count = sig->param_count;
-#ifdef _MSC_VER
- gpointer* mparams = (gpointer*) _alloca(count*sizeof(gpointer));
-#else
- gpointer mparams[count];
-#endif
+ gpointer* mparams = (gpointer*) alloca(count*sizeof(gpointer));
for (i=0; i<count; i++) {
MonoClass *class = mono_class_from_mono_type (sig->params [i]);
return method;
cache = method->klass->image->remoting_invoke_cache;
- if ((res = (MonoMethod *)g_hash_table_lookup (cache, method)))
+ if ((res = mono_marshal_find_in_cache (cache, method)))
return res;
if (!csig) {
mono_mb_emit_restore_result (mb, sig->ret);
}
- res = mono_mb_create_method (mb, sig, sig->param_count + 16);
+ res = mono_mb_create_and_cache (cache, method, mb, sig, sig->param_count + 16);
mono_mb_free (mb);
- g_hash_table_insert (cache, method, res);
+
return res;
}
g_assert (sig->hasthis);
cache = method->klass->image->remoting_invoke_cache;
- if ((res = (MonoMethod *)g_hash_table_lookup (cache, (char *)method + 1)))
+ if ((res = mono_marshal_find_in_cache (cache, (char *)method + 1)))
return res;
mb = mono_mb_new (method->klass, method->name, MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK);
mono_mb_emit_managed_call (mb, method, method->signature);
mono_mb_emit_byte (mb, CEE_RET);
- res = mono_mb_create_method (mb, sig, sig->param_count + 16);
+ res = mono_mb_create_and_cache (cache, (char*)method + 1,
+ mb, sig, sig->param_count + 16);
mono_mb_free (mb);
- g_hash_table_insert (cache, (char *)method + 1, res);
+
return res;
}
sig = method->signature;
cache = method->klass->image->delegate_invoke_cache;
- if ((res = (MonoMethod *)g_hash_table_lookup (cache, sig)))
+ if ((res = mono_marshal_find_in_cache (cache, sig)))
return res;
sigsize = sizeof (MonoMethodSignature) + sig->param_count * sizeof (MonoType *);
mono_mb_patch_addr (mb, pos1, mb->pos - (pos1 + 4));
mono_mb_emit_byte (mb, CEE_RET);
- res = mono_mb_create_method (mb, sig, sig->param_count + 16);
+ res = mono_mb_create_and_cache (cache, sig,
+ mb, sig, sig->param_count + 16);
mono_mb_free (mb);
- g_hash_table_insert (cache, sig, res);
-
return res;
}
g_assert (method);
cache = method->klass->image->runtime_invoke_cache;
- if ((res = (MonoMethod *)g_hash_table_lookup (cache, method)))
+ if ((res = mono_marshal_find_in_cache (cache, method)))
return res;
/* to make it work with our special string constructors */
mono_mb_emit_ldloc (mb, 0);
mono_mb_emit_byte (mb, CEE_RET);
- res = mono_mb_create_method (mb, csig, sig->param_count + 16);
+ res = mono_mb_create_and_cache (cache, method,
+ mb, csig, sig->param_count + 16);
mono_mb_free (mb);
header = ((MonoMethodNormal *)res)->header;
header->num_clauses = 1;
header->clauses = clause;
- g_hash_table_insert (cache, method, res);
-
return res;
}
MonoMethod *res;
GHashTable *cache;
int i, pos, sigsize, *tmp_locals;
+ static MonoMethodSignature *alloc_sig = NULL;
+ int retobj_var = 0;
g_assert (method != NULL);
g_assert (!method->signature->pinvoke);
cache = method->klass->image->managed_wrapper_cache;
- if (!this && (res = (MonoMethod *)g_hash_table_lookup (cache, method)))
+ if (!this && (res = mono_marshal_find_in_cache (cache, method)))
return res;
+ /* Under MS, the allocation should be done using CoTaskMemAlloc */
+ if (!alloc_sig) {
+ alloc_sig = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
+ alloc_sig->params [0] = &mono_defaults.int_class->byval_arg;
+ alloc_sig->ret = &mono_defaults.int_class->byval_arg;
+ alloc_sig->pinvoke = 1;
+ }
+
if (this) {
/* fime: howto free that memory ? */
}
-
+
sig = method->signature;
mb = mono_mb_new (method->klass, method->name, MONO_WRAPPER_NATIVE_TO_MANAGED);
/* allocate local 2 (boolean) delete_old */
mono_mb_add_local (mb, &mono_defaults.boolean_class->byval_arg);
+ if (!MONO_TYPE_IS_VOID(sig->ret)) {
+ /* allocate local 3 to store the return value */
+ mono_mb_add_local (mb, sig->ret);
+ }
+
mono_mb_emit_byte (mb, CEE_LDNULL);
mono_mb_emit_byte (mb, CEE_STLOC_2);
csig->hasthis = 0;
csig->pinvoke = 1;
+#ifdef PLATFORM_WIN32
+ /*
+ * Under windows, delegates passed to native code must use the STDCALL
+ * calling convention.
+ */
+ csig->call_convention = MONO_CALL_STDCALL;
+#endif
+
/* fixme: howto handle this ? */
if (sig->hasthis) {
}
switch (t->type) {
+ case MONO_TYPE_CLASS: {
+ klass = t->data.klass;
+
+ /* FIXME: Raise a MarshalDirectiveException here */
+ g_assert ((klass->flags & TYPE_ATTRIBUTE_LAYOUT_MASK) != TYPE_ATTRIBUTE_AUTO_LAYOUT);
+
+ tmp_locals [i] = mono_mb_add_local (mb, &mono_defaults.int_class->byval_arg);
+
+ if (t->attrs & PARAM_ATTRIBUTE_OUT) {
+ mono_mb_emit_byte (mb, CEE_LDNULL);
+ mono_mb_emit_stloc (mb, tmp_locals [i]);
+ break;
+ }
+
+ /* Set src */
+ mono_mb_emit_ldarg (mb, i);
+ if (t->byref) {
+ int pos2;
+
+ /* Check for NULL and raise an exception */
+ mono_mb_emit_byte (mb, CEE_BRTRUE);
+ pos2 = mb->pos;
+ mono_mb_emit_i4 (mb, 0);
+
+ mono_mb_emit_exception (mb, "ArgumentNullException");
+
+ mono_mb_patch_addr (mb, pos2, mb->pos - (pos2 + 4));
+ mono_mb_emit_ldarg (mb, i);
+ mono_mb_emit_byte (mb, CEE_LDIND_I);
+ }
+
+ mono_mb_emit_byte (mb, CEE_STLOC_0);
+
+ mono_mb_emit_byte (mb, CEE_LDC_I4_0);
+ mono_mb_emit_stloc (mb, tmp_locals [i]);
+
+ mono_mb_emit_byte (mb, CEE_LDLOC_0);
+ mono_mb_emit_byte (mb, CEE_BRFALSE);
+ pos = mb->pos;
+ mono_mb_emit_i4 (mb, 0);
+
+ /* Create and set dst */
+ mono_mb_emit_byte (mb, MONO_CUSTOM_PREFIX);
+ mono_mb_emit_byte (mb, CEE_MONO_NEWOBJ);
+ mono_mb_emit_i4 (mb, mono_mb_add_data (mb, klass));
+ mono_mb_emit_stloc (mb, tmp_locals [i]);
+ mono_mb_emit_ldloc (mb, tmp_locals [i]);
+ mono_mb_emit_icon (mb, sizeof (MonoObject));
+ mono_mb_emit_byte (mb, CEE_ADD);
+ mono_mb_emit_byte (mb, CEE_STLOC_1);
+
+ /* emit valuetype conversion code */
+ emit_struct_conv (mb, klass, TRUE);
+
+ mono_mb_patch_addr (mb, pos, mb->pos - (pos + 4));
+ break;
+ }
case MONO_TYPE_VALUETYPE:
klass = sig->params [i]->data.klass;
}
break;
case MONO_TYPE_CLASS:
+ if (t->byref)
+ mono_mb_emit_ldloc_addr (mb, tmp_locals [i]);
+ else
+ mono_mb_emit_ldloc (mb, tmp_locals [i]);
+ break;
case MONO_TYPE_ARRAY:
case MONO_TYPE_SZARRAY:
case MONO_TYPE_OBJECT:
if (!sig->ret->byref) {
switch (sig->ret->type) {
case MONO_TYPE_VOID:
+ break;
case MONO_TYPE_BOOLEAN:
case MONO_TYPE_I1:
case MONO_TYPE_U1:
case MONO_TYPE_I8:
case MONO_TYPE_U8:
case MONO_TYPE_OBJECT:
- mono_mb_emit_byte (mb, CEE_RET);
+ mono_mb_emit_byte (mb, CEE_STLOC_3);
break;
case MONO_TYPE_STRING:
csig->ret = &mono_defaults.int_class->byval_arg;
mono_mb_emit_byte (mb, MONO_CUSTOM_PREFIX);
mono_mb_emit_byte (mb, CEE_MONO_FUNC1);
mono_mb_emit_byte (mb, MONO_MARSHAL_CONV_STR_LPSTR);
- mono_mb_emit_byte (mb, CEE_RET);
+ mono_mb_emit_byte (mb, CEE_STLOC_3);
break;
- case MONO_TYPE_VALUETYPE: {
- int tmp;
+ case MONO_TYPE_VALUETYPE:
klass = sig->ret->data.klass;
if (((klass->flags & TYPE_ATTRIBUTE_LAYOUT_MASK) == TYPE_ATTRIBUTE_EXPLICIT_LAYOUT) ||
klass->blittable || klass->enumtype)
mono_mb_emit_byte (mb, CEE_STLOC_0);
/* allocate space for the native struct and
* store the address into dst_ptr */
- tmp = mono_mb_add_local (mb, &mono_defaults.int_class->byval_arg);
- g_assert (tmp);
+ retobj_var = mono_mb_add_local (mb, &mono_defaults.int_class->byval_arg);
+ g_assert (retobj_var);
mono_mb_emit_icon (mb, mono_class_native_size (klass, NULL));
- mono_mb_emit_byte (mb, CEE_PREFIX1);
- mono_mb_emit_byte (mb, CEE_LOCALLOC);
+ mono_mb_emit_native_call (mb, alloc_sig, mono_marshal_alloc);
mono_mb_emit_byte (mb, CEE_STLOC_1);
mono_mb_emit_byte (mb, CEE_LDLOC_1);
- mono_mb_emit_stloc (mb, tmp);
+ mono_mb_emit_stloc (mb, retobj_var);
/* emit valuetype conversion code */
emit_struct_conv (mb, klass, FALSE);
- mono_mb_emit_ldloc (mb, tmp);
- mono_mb_emit_byte (mb, MONO_CUSTOM_PREFIX);
- mono_mb_emit_byte (mb, CEE_MONO_RETOBJ);
- mono_mb_emit_i4 (mb, mono_mb_add_data (mb, klass));
+ break;
+ case MONO_TYPE_CLASS: {
+ int pos2;
+
+ klass = sig->ret->data.klass;
+
+ /* FIXME: Raise a MarshalDirectiveException here */
+ g_assert ((klass->flags & TYPE_ATTRIBUTE_LAYOUT_MASK) != TYPE_ATTRIBUTE_AUTO_LAYOUT);
+
+ mono_mb_emit_byte (mb, CEE_STLOC_0);
+ /* Check for null */
+ mono_mb_emit_byte (mb, CEE_LDLOC_0);
+ pos = mono_mb_emit_branch (mb, CEE_BRTRUE);
+ mono_mb_emit_byte (mb, CEE_LDNULL);
+ mono_mb_emit_byte (mb, CEE_STLOC_3);
+ pos2 = mono_mb_emit_branch (mb, CEE_BR);
+
+ mono_mb_patch_addr (mb, pos, mb->pos - (pos + 4));
+
+ /* Set src */
+ mono_mb_emit_byte (mb, CEE_LDLOC_0);
+ mono_mb_emit_icon (mb, sizeof (MonoObject));
+ mono_mb_emit_byte (mb, CEE_ADD);
+ mono_mb_emit_byte (mb, CEE_STLOC_0);
+
+ /* Allocate and set dest */
+ mono_mb_emit_icon (mb, mono_class_native_size (klass, NULL));
+ mono_mb_emit_native_call (mb, alloc_sig, mono_marshal_alloc);
+ mono_mb_emit_byte (mb, CEE_DUP);
+ mono_mb_emit_byte (mb, CEE_STLOC_1);
+ mono_mb_emit_byte (mb, CEE_STLOC_3);
+
+ emit_struct_conv (mb, klass, FALSE);
+
+ mono_mb_patch_addr (mb, pos2, mb->pos - (pos2 + 4));
break;
}
default:
g_assert_not_reached ();
}
} else {
+ mono_mb_emit_byte (mb, CEE_STLOC_3);
+ }
+
+ /* Convert byref arguments back */
+ for (i = 0; i < sig->param_count; i ++) {
+ MonoType *t = sig->params [i];
+
+ if (!t->byref)
+ break;
+
+ switch (t->type) {
+ case MONO_TYPE_CLASS: {
+ int pos2;
+
+ klass = t->data.klass;
+
+ /* Check for null */
+ mono_mb_emit_ldloc (mb, tmp_locals [i]);
+ pos = mono_mb_emit_branch (mb, CEE_BRTRUE);
+ mono_mb_emit_ldarg (mb, i);
+ mono_mb_emit_byte (mb, CEE_LDC_I4_0);
+ mono_mb_emit_byte (mb, CEE_STIND_I);
+ pos2 = mono_mb_emit_branch (mb, CEE_BR);
+
+ mono_mb_patch_addr (mb, pos, mb->pos - (pos + 4));
+
+ /* Set src */
+ mono_mb_emit_ldloc (mb, tmp_locals [i]);
+ mono_mb_emit_icon (mb, sizeof (MonoObject));
+ mono_mb_emit_byte (mb, CEE_ADD);
+ mono_mb_emit_byte (mb, CEE_STLOC_0);
+
+ /* Allocate and set dest */
+ mono_mb_emit_icon (mb, mono_class_native_size (klass, NULL));
+ mono_mb_emit_native_call (mb, alloc_sig, mono_marshal_alloc);
+ mono_mb_emit_byte (mb, CEE_STLOC_1);
+
+ /* Update argument pointer */
+ mono_mb_emit_ldarg (mb, i);
+ mono_mb_emit_byte (mb, CEE_LDLOC_1);
+ mono_mb_emit_byte (mb, CEE_STIND_I);
+
+ /* emit valuetype conversion code */
+ emit_struct_conv (mb, klass, FALSE);
+
+ mono_mb_patch_addr (mb, pos2, mb->pos - (pos2 + 4));
+ break;
+ }
+ }
+ }
+
+ if (retobj_var) {
+ mono_mb_emit_ldloc (mb, retobj_var);
+ mono_mb_emit_byte (mb, MONO_CUSTOM_PREFIX);
+ mono_mb_emit_byte (mb, CEE_MONO_RETOBJ);
+ mono_mb_emit_i4 (mb, mono_mb_add_data (mb, klass));
+ }
+ else {
+ if (!MONO_TYPE_IS_VOID(sig->ret))
+ mono_mb_emit_byte (mb, CEE_LDLOC_3);
mono_mb_emit_byte (mb, CEE_RET);
}
- res = mono_mb_create_method (mb, csig, sig->param_count + 16);
+ if (!this)
+ res = mono_mb_create_and_cache (cache, method,
+ mb, csig, sig->param_count + 16);
+ else
+ res = mono_mb_create_method (mb, csig, sig->param_count + 16);
mono_mb_free (mb);
- if (!this)
- g_hash_table_insert (cache, method, res);
+ //printf ("CODE FOR %s: \n%s.\n", mono_method_full_name (res, TRUE), mono_disasm_code (0, res, ((MonoMethodNormal*)res)->header->code, ((MonoMethodNormal*)res)->header->code + ((MonoMethodNormal*)res)->header->code_size));
return res;
}
char *name;
int t, pos0, pos1;
- if (!ldfld_hash)
- ldfld_hash = g_hash_table_new (NULL, NULL);
-
-
t = type->type;
if (!type->byref) {
klass = mono_defaults.int_class;
}
- if ((res = g_hash_table_lookup (ldfld_hash, klass)))
+ EnterCriticalSection (&marshal_mutex);
+ if (!ldfld_hash)
+ ldfld_hash = g_hash_table_new (NULL, NULL);
+ res = g_hash_table_lookup (ldfld_hash, klass);
+ LeaveCriticalSection (&marshal_mutex);
+ if (res)
return res;
name = g_strdup_printf ("__ldfld_wrapper_%s.%s", klass->name_space, klass->name);
mono_mb_emit_byte (mb, CEE_RET);
- res = mono_mb_create_method (mb, sig, sig->param_count + 16);
+ res = mono_mb_create_and_cache (ldfld_hash, klass,
+ mb, sig, sig->param_count + 16);
mono_mb_free (mb);
- g_hash_table_insert (ldfld_hash, klass, res);
-
return res;
}
char *name;
int t, pos;
- if (!stfld_hash)
- stfld_hash = g_hash_table_new (NULL, NULL);
-
t = type->type;
if (!type->byref) {
klass = mono_defaults.int_class;
}
- if ((res = g_hash_table_lookup (stfld_hash, klass)))
+ EnterCriticalSection (&marshal_mutex);
+ if (!stfld_hash)
+ stfld_hash = g_hash_table_new (NULL, NULL);
+ res = g_hash_table_lookup (stfld_hash, klass);
+ LeaveCriticalSection (&marshal_mutex);
+ if (res)
return res;
name = g_strdup_printf ("__stfld_wrapper_%s.%s", klass->name_space, klass->name);
mono_mb_emit_byte (mb, CEE_RET);
- res = mono_mb_create_method (mb, sig, sig->param_count + 16);
+ res = mono_mb_create_and_cache (stfld_hash, klass,
+ mb, sig, sig->param_count + 16);
mono_mb_free (mb);
- g_hash_table_insert (stfld_hash, klass, res);
-
return res;
}
/*
* generates IL code for the icall wrapper (the generated method
- * calls the unamnaged code in func)
+ * calls the unmanaged code in func)
*/
MonoMethod *
mono_marshal_get_icall_wrapper (MonoMethodSignature *sig, const char *name, gconstpointer func)
/*
* generates IL code for the pinvoke wrapper (the generated method
- * calls the unamnage code in method->addr)
+ * calls the unmanaged code in method->addr)
*/
MonoMethod *
mono_marshal_get_native_wrapper (MonoMethod *method)
g_assert (method->signature->pinvoke);
cache = method->klass->image->native_wrapper_cache;
- if ((res = (MonoMethod *)g_hash_table_lookup (cache, method)))
+ if ((res = mono_marshal_find_in_cache (cache, method)))
return res;
sig = method->signature;
(method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL))
pinvoke = TRUE;
+ if (!method->addr) {
+ if (pinvoke)
+ mono_lookup_pinvoke_call (method);
+ else
+ method->addr = mono_lookup_internal_call (method);
+ }
+
mb = mono_mb_new (method->klass, method->name, MONO_WRAPPER_MANAGED_TO_NATIVE);
mb->method->save_lmf = 1;
- if (pinvoke && !method->addr)
- mono_lookup_pinvoke_call (method);
-
piinfo = (MonoMethodPInvoke *)method;
if (!method->addr) {
- mono_mb_emit_exception (mb);
+ mono_mb_emit_exception (mb, "MissingMethodException");
csig = g_memdup (sig, sigsize);
csig->pinvoke = 0;
- res = mono_mb_create_method (mb, csig, csig->param_count + 16);
+ res = mono_mb_create_and_cache (cache, method,
+ mb, csig, csig->param_count + 16);
mono_mb_free (mb);
- g_hash_table_insert (cache, method, res);
return res;
}
csig = g_memdup (csig, sigsize);
csig->pinvoke = 0;
- res = mono_mb_create_method (mb, csig, csig->param_count + 16);
+ res = mono_mb_create_and_cache (cache, method,
+ mb, csig, csig->param_count + 16);
mono_mb_free (mb);
- g_hash_table_insert (cache, method, res);
return res;
}
/* we first do all conversions */
tmp_locals = alloca (sizeof (int) * sig->param_count);
+
for (i = 0; i < sig->param_count; i ++) {
MonoType *t = sig->params [i];
MonoMarshalSpec *spec = mspecs [i + 1];
csig->params [argnum] = &mono_defaults.int_class->byval_arg;
tmp_locals [i] = mono_mb_add_local (mb, &mono_defaults.int_class->byval_arg);
- mono_mb_emit_ldarg (mb, argnum);
- mono_mb_emit_byte (mb, MONO_CUSTOM_PREFIX);
- mono_mb_emit_byte (mb, CEE_MONO_FUNC1);
- if (klass->element_class == mono_defaults.string_class)
+ if (klass->element_class == mono_defaults.string_class) {
+ mono_mb_emit_ldarg (mb, argnum);
+ mono_mb_emit_byte (mb, MONO_CUSTOM_PREFIX);
+ mono_mb_emit_byte (mb, CEE_MONO_FUNC1);
+
mono_mb_emit_byte (mb, MONO_MARSHAL_CONV_STRARRAY_STRLPARRAY);
- else
- mono_mb_emit_byte (mb, MONO_MARSHAL_CONV_ARRAY_LPARRAY);
- mono_mb_emit_stloc (mb, tmp_locals [i]);
- break;
- }
+ mono_mb_emit_stloc (mb, tmp_locals [i]);
+ }
+ else if (klass->element_class->blittable) {
+ mono_mb_emit_ldarg (mb, argnum);
+ mono_mb_emit_byte (mb, MONO_CUSTOM_PREFIX);
+ mono_mb_emit_byte (mb, CEE_MONO_FUNC1);
+
+ mono_mb_emit_byte (mb, MONO_MARSHAL_CONV_ARRAY_LPARRAY);
+ mono_mb_emit_stloc (mb, tmp_locals [i]);
+ }
+ else {
+ MonoClass *eklass;
+ guint32 label1, label2, label3;
+ int index_var, dest_ptr;
+
+ dest_ptr = mono_mb_add_local (mb, &mono_defaults.int_class->byval_arg);
+
+ /* Check null */
+ mono_mb_emit_ldarg (mb, argnum);
+ mono_mb_emit_stloc (mb, tmp_locals [i]);
+ mono_mb_emit_ldarg (mb, argnum);
+ mono_mb_emit_byte (mb, CEE_BRFALSE);
+ label1 = mb->pos;
+ mono_mb_emit_i4 (mb, 0);
+
+ /* allocate space for the native struct and store the address */
+ eklass = klass->element_class;
+ mono_mb_emit_icon (mb, mono_class_native_size (eklass, NULL));
+ mono_mb_emit_ldarg (mb, argnum);
+ mono_mb_emit_byte (mb, CEE_LDLEN);
+ mono_mb_emit_byte (mb, CEE_MUL);
+ mono_mb_emit_byte (mb, CEE_PREFIX1);
+ mono_mb_emit_byte (mb, CEE_LOCALLOC);
+ mono_mb_emit_stloc (mb, tmp_locals [i]);
+
+ mono_mb_emit_ldloc (mb, tmp_locals [i]);
+ mono_mb_emit_stloc (mb, dest_ptr);
+
+ /* Emit marshalling loop */
+ index_var = mono_mb_add_local (mb, &mono_defaults.int_class->byval_arg);
+ mono_mb_emit_byte (mb, CEE_LDC_I4_0);
+ mono_mb_emit_stloc (mb, index_var);
+ label2 = mb->pos;
+ mono_mb_emit_ldloc (mb, index_var);
+ mono_mb_emit_ldarg (mb, argnum);
+ mono_mb_emit_byte (mb, CEE_LDLEN);
+ mono_mb_emit_byte (mb, CEE_BGE);
+ label3 = mb->pos;
+ mono_mb_emit_i4 (mb, 0);
+
+ /* Emit marshalling code */
+
+ /* set the src_ptr */
+ mono_mb_emit_ldarg (mb, argnum);
+ mono_mb_emit_ldloc (mb, index_var);
+ mono_mb_emit_byte (mb, CEE_LDELEMA);
+ mono_mb_emit_i4 (mb, mono_mb_add_data (mb, eklass));
+ mono_mb_emit_byte (mb, CEE_STLOC_0);
+
+ /* set dst_ptr */
+ mono_mb_emit_ldloc (mb, dest_ptr);
+ mono_mb_emit_byte (mb, CEE_STLOC_1);
+
+ /* emit valuetype conversion code */
+ emit_struct_conv (mb, eklass, FALSE);
+
+ mono_mb_emit_add_to_local (mb, index_var, 1);
+ mono_mb_emit_add_to_local (mb, dest_ptr, mono_class_native_size (eklass, NULL));
+
+ mono_mb_emit_byte (mb, CEE_BR);
+ mono_mb_emit_i4 (mb, label2 - (mb->pos + 4));
+
+ mono_mb_patch_addr (mb, label1, mb->pos - (label1 + 4));
+ mono_mb_patch_addr (mb, label3, mb->pos - (label3 + 4));
+ }
+
+ break;
+ case MONO_TYPE_BOOLEAN: {
+ MonoType *local_type;
+ int variant_bool = 0;
+ if (!t->byref)
+ continue;
+ if (spec == NULL) {
+ local_type = &mono_defaults.int32_class->byval_arg;
+ } else {
+ switch (spec->native) {
+ case MONO_NATIVE_I1:
+ local_type = &mono_defaults.byte_class->byval_arg;
+ break;
+ case MONO_NATIVE_VARIANTBOOL:
+ local_type = &mono_defaults.int16_class->byval_arg;
+ variant_bool = 1;
+ break;
+ default:
+ g_warning ("marshalling bool as native type %x is currently not supported", spec->native);
+ break;
+ }
+ }
+ csig->params [argnum] = &mono_defaults.int_class->byval_arg;
+ tmp_locals [i] = mono_mb_add_local (mb, local_type);
+ mono_mb_emit_ldarg (mb, argnum);
+ mono_mb_emit_byte (mb, CEE_LDIND_I1);
+ if (variant_bool)
+ mono_mb_emit_byte (mb, CEE_NEG);
+ mono_mb_emit_stloc (mb, tmp_locals [i]);
+ break;
+ }
+ }
}
/* push all arguments */
switch (t->type) {
case MONO_TYPE_BOOLEAN:
- if (t->byref)
- g_warning ("byref boolean marshalling not inplemented");
- mono_mb_emit_ldarg (mb, argnum);
+ if (t->byref) {
+ g_assert (tmp_locals [i]);
+ mono_mb_emit_ldloc_addr (mb, tmp_locals [i]);
+ } else
+ mono_mb_emit_ldarg (mb, argnum);
break;
case MONO_TYPE_I1:
case MONO_TYPE_U1:
/* call the native method */
mono_mb_emit_native_call (mb, csig, method->addr);
+ /* Set LastError if needed */
+ if (piinfo->piflags & PINVOKE_ATTRIBUTE_SUPPORTS_LAST_ERROR) {
+ MonoMethodSignature *lasterr_sig;
+
+ lasterr_sig = mono_metadata_signature_alloc (mono_defaults.corlib, 0);
+ lasterr_sig->ret = &mono_defaults.void_class->byval_arg;
+ lasterr_sig->pinvoke = 1;
+
+ mono_mb_emit_native_call (mb, lasterr_sig, mono_marshal_set_last_error);
+ }
+
/* convert the result */
if (!sig->ret->byref) {
MonoMarshalSpec *spec = mspecs [0];
type = sig->ret->type;
- handle_enum:
- switch (type) {
- case MONO_TYPE_VOID:
- break;
- case MONO_TYPE_I1:
- case MONO_TYPE_U1:
- case MONO_TYPE_I2:
- case MONO_TYPE_U2:
- case MONO_TYPE_I4:
- case MONO_TYPE_U4:
- case MONO_TYPE_I:
- case MONO_TYPE_U:
- case MONO_TYPE_PTR:
- case MONO_TYPE_R4:
- case MONO_TYPE_R8:
- case MONO_TYPE_I8:
- case MONO_TYPE_U8:
- /* no conversions necessary */
- mono_mb_emit_byte (mb, CEE_STLOC_3);
- break;
- case MONO_TYPE_BOOLEAN:
- /* maybe we need to make sure that it fits within 8 bits */
- mono_mb_emit_byte (mb, CEE_STLOC_3);
- break;
- case MONO_TYPE_VALUETYPE:
- klass = sig->ret->data.klass;
- if (klass->enumtype) {
- type = sig->ret->data.klass->enum_basetype->type;
- goto handle_enum;
- }
+ if (spec && spec->native == MONO_NATIVE_CUSTOM) {
+ MonoType *mtype;
+ MonoClass *mklass;
+ MonoMethod *marshal_native_to_managed;
+ MonoMethod *get_instance;
- if (((klass->flags & TYPE_ATTRIBUTE_LAYOUT_MASK) == TYPE_ATTRIBUTE_EXPLICIT_LAYOUT) ||
- klass->blittable) {
+ mtype = mono_reflection_type_from_name (spec->data.custom_data.custom_name, method->klass->image);
+ g_assert (mtype != NULL);
+ mklass = mono_class_from_mono_type (mtype);
+ g_assert (mklass != NULL);
+
+ marshal_native_to_managed = mono_find_method_by_name (mklass, "MarshalNativeToManaged", 1);
+ g_assert (marshal_native_to_managed);
+ get_instance = mono_find_method_by_name (mklass, "GetInstance", 1);
+ g_assert (get_instance);
+
+ switch (type) {
+ case MONO_TYPE_CLASS:
+ case MONO_TYPE_OBJECT:
+ case MONO_TYPE_STRING:
+ case MONO_TYPE_ARRAY:
+ case MONO_TYPE_SZARRAY:
+ mono_mb_emit_byte (mb, CEE_STLOC_3);
+
+ mono_mb_emit_ldstr (mb, spec->data.custom_data.cookie);
+
+ mono_mb_emit_byte (mb, CEE_CALL);
+ mono_mb_emit_i4 (mb, mono_mb_add_data (mb, get_instance));
+
+ mono_mb_emit_byte (mb, CEE_LDLOC_3);
+
+ mono_mb_emit_byte (mb, CEE_CALLVIRT);
+ mono_mb_emit_i4 (mb, mono_mb_add_data (mb, marshal_native_to_managed));
+
mono_mb_emit_byte (mb, CEE_STLOC_3);
break;
+ default:
+ g_warning ("custom marshalling of type %x is currently not supported", type);
+ g_assert_not_reached ();
+ break;
}
- /* load pointer to returned value type */
- mono_mb_emit_byte (mb, MONO_CUSTOM_PREFIX);
- mono_mb_emit_byte (mb, CEE_MONO_VTADDR);
- /* store the address of the source into local variable 0 */
- mono_mb_emit_byte (mb, CEE_STLOC_0);
- /* set dst_ptr */
- mono_mb_emit_ldloc_addr (mb, 3);
- mono_mb_emit_byte (mb, CEE_STLOC_1);
+ } else {
- /* emit valuetype conversion code */
- emit_struct_conv (mb, sig->ret->data.klass, TRUE);
- break;
- case MONO_TYPE_STRING:
+ handle_enum:
+ switch (type) {
+ case MONO_TYPE_VOID:
+ break;
+ case MONO_TYPE_I1:
+ case MONO_TYPE_U1:
+ case MONO_TYPE_I2:
+ case MONO_TYPE_U2:
+ case MONO_TYPE_I4:
+ case MONO_TYPE_U4:
+ case MONO_TYPE_I:
+ case MONO_TYPE_U:
+ case MONO_TYPE_PTR:
+ case MONO_TYPE_R4:
+ case MONO_TYPE_R8:
+ case MONO_TYPE_I8:
+ case MONO_TYPE_U8:
+ /* no conversions necessary */
+ mono_mb_emit_byte (mb, CEE_STLOC_3);
+ break;
+ case MONO_TYPE_BOOLEAN:
+ /* maybe we need to make sure that it fits within 8 bits */
+ mono_mb_emit_byte (mb, CEE_STLOC_3);
+ break;
+ case MONO_TYPE_VALUETYPE:
+ klass = sig->ret->data.klass;
+ if (klass->enumtype) {
+ type = sig->ret->data.klass->enum_basetype->type;
+ goto handle_enum;
+ }
+
+ if (((klass->flags & TYPE_ATTRIBUTE_LAYOUT_MASK) == TYPE_ATTRIBUTE_EXPLICIT_LAYOUT) ||
+ klass->blittable) {
+ mono_mb_emit_byte (mb, CEE_STLOC_3);
+ break;
+ }
+ /* load pointer to returned value type */
+ mono_mb_emit_byte (mb, MONO_CUSTOM_PREFIX);
+ mono_mb_emit_byte (mb, CEE_MONO_VTADDR);
+ /* store the address of the source into local variable 0 */
+ mono_mb_emit_byte (mb, CEE_STLOC_0);
+ /* set dst_ptr */
+ mono_mb_emit_ldloc_addr (mb, 3);
+ mono_mb_emit_byte (mb, CEE_STLOC_1);
+
+ /* emit valuetype conversion code */
+ emit_struct_conv (mb, sig->ret->data.klass, TRUE);
+ break;
+ case MONO_TYPE_STRING:
#ifdef GTK_SHARP_FIXED
- mono_mb_emit_byte (mb, CEE_STLOC_0);
- mono_mb_emit_byte (mb, CEE_LDLOC_0);
+ mono_mb_emit_byte (mb, CEE_STLOC_0);
+ mono_mb_emit_byte (mb, CEE_LDLOC_0);
#endif
-
- mono_mb_emit_byte (mb, MONO_CUSTOM_PREFIX);
- mono_mb_emit_byte (mb, CEE_MONO_FUNC1);
- if (spec) {
- switch (spec->native) {
- case MONO_NATIVE_LPWSTR:
- mono_mb_emit_byte (mb, MONO_MARSHAL_CONV_LPWSTR_STR);
- break;
- default:
- g_warning ("marshalling conversion not implemented");
- g_assert_not_reached ();
+
+ mono_mb_emit_byte (mb, MONO_CUSTOM_PREFIX);
+ mono_mb_emit_byte (mb, CEE_MONO_FUNC1);
+ if (spec) {
+ switch (spec->native) {
+ case MONO_NATIVE_LPWSTR:
+ mono_mb_emit_byte (mb, MONO_MARSHAL_CONV_LPWSTR_STR);
+ break;
+ default:
+ g_warning ("marshalling conversion not implemented");
+ g_assert_not_reached ();
+ }
+ } else {
+ mono_mb_emit_byte (mb, MONO_MARSHAL_CONV_LPSTR_STR);
}
- } else {
- mono_mb_emit_byte (mb, MONO_MARSHAL_CONV_LPSTR_STR);
- }
- mono_mb_emit_byte (mb, CEE_STLOC_3);
+ mono_mb_emit_byte (mb, CEE_STLOC_3);
#ifdef GTK_SHARP_FIXED
- /* free the string */
- mono_mb_emit_byte (mb, CEE_LDLOC_0);
- mono_mb_emit_byte (mb, MONO_CUSTOM_PREFIX);
- mono_mb_emit_byte (mb, CEE_MONO_FREE);
+ /* free the string */
+ mono_mb_emit_byte (mb, CEE_LDLOC_0);
+ mono_mb_emit_byte (mb, MONO_CUSTOM_PREFIX);
+ mono_mb_emit_byte (mb, CEE_MONO_FREE);
#endif
- break;
- case MONO_TYPE_ARRAY:
- case MONO_TYPE_SZARRAY:
- case MONO_TYPE_CLASS:
- case MONO_TYPE_OBJECT:
- /* fixme: we need conversions here */
- mono_mb_emit_byte (mb, CEE_STLOC_3);
- break;
- case MONO_TYPE_CHAR:
- /* fixme: we need conversions here */
- mono_mb_emit_byte (mb, CEE_STLOC_3);
- break;
- case MONO_TYPE_TYPEDBYREF:
- case MONO_TYPE_FNPTR:
- default:
- g_warning ("return type 0x%02x unknown", sig->ret->type);
- g_assert_not_reached ();
+ break;
+ case MONO_TYPE_CLASS:
+ case MONO_TYPE_OBJECT:
+ klass = sig->ret->data.klass;
+
+ /* set src */
+ mono_mb_emit_byte (mb, CEE_STLOC_0);
+
+ mono_mb_emit_byte (mb, CEE_LDNULL);
+ mono_mb_emit_byte (mb, CEE_STLOC_3);
+
+
+ mono_mb_emit_byte (mb, CEE_LDLOC_0);
+ mono_mb_emit_byte (mb, CEE_BRFALSE);
+ pos = mb->pos;
+ mono_mb_emit_i4 (mb, 0);
+
+ /* allocate result object */
+
+ mono_mb_emit_byte (mb, MONO_CUSTOM_PREFIX);
+ mono_mb_emit_byte (mb, CEE_MONO_NEWOBJ);
+ mono_mb_emit_i4 (mb, mono_mb_add_data (mb, klass));
+ mono_mb_emit_byte (mb, CEE_STLOC_3);
+
+ /* set dst */
+
+ mono_mb_emit_byte (mb, CEE_LDLOC_3);
+ mono_mb_emit_byte (mb, MONO_CUSTOM_PREFIX);
+ mono_mb_emit_byte (mb, CEE_MONO_OBJADDR);
+ mono_mb_emit_icon (mb, sizeof (MonoObject));
+ mono_mb_emit_byte (mb, CEE_ADD);
+ mono_mb_emit_byte (mb, CEE_STLOC_1);
+
+ /* emit conversion code */
+ emit_struct_conv (mb, klass, TRUE);
+
+ mono_mb_patch_addr (mb, pos, mb->pos - (pos + 4));
+ break;
+ case MONO_TYPE_ARRAY:
+ case MONO_TYPE_SZARRAY:
+ /* fixme: we need conversions here */
+ mono_mb_emit_byte (mb, CEE_STLOC_3);
+ break;
+ case MONO_TYPE_CHAR:
+ /* fixme: we need conversions here */
+ mono_mb_emit_byte (mb, CEE_STLOC_3);
+ break;
+ case MONO_TYPE_TYPEDBYREF:
+ case MONO_TYPE_FNPTR:
+ default:
+ g_warning ("return type 0x%02x unknown", sig->ret->type);
+ g_assert_not_reached ();
+ }
}
} else {
mono_mb_emit_byte (mb, CEE_STLOC_3);
break;
}
- if (!t->byref)
+ if (!(t->byref || (t->attrs & PARAM_ATTRIBUTE_OUT)))
continue;
- if (t->attrs & PARAM_ATTRIBUTE_OUT) {
+ if (t->byref && (t->attrs & PARAM_ATTRIBUTE_OUT)) {
/* allocate a new object new object */
mono_mb_emit_ldarg (mb, argnum);
mono_mb_emit_byte (mb, MONO_CUSTOM_PREFIX);
/* dst = *argument */
mono_mb_emit_ldarg (mb, argnum);
- mono_mb_emit_byte (mb, CEE_LDIND_I);
+
+ if (t->byref)
+ mono_mb_emit_byte (mb, CEE_LDIND_I);
+
mono_mb_emit_byte (mb, CEE_STLOC_1);
mono_mb_emit_byte (mb, CEE_LDLOC_1);
continue;
klass = mono_class_from_mono_type (t);
-
+
if (klass->element_class == mono_defaults.string_class) {
g_assert (tmp_locals [i]);
mono_mb_patch_addr (mb, pos, mb->pos - (pos + 4));
}
+ if (t->attrs & PARAM_ATTRIBUTE_OUT) {
+ /* FIXME: Optimize blittable case */
+ MonoClass *eklass;
+ guint32 label1, label2, label3;
+ int index_var, src_ptr;
+
+ eklass = klass->element_class;
+ src_ptr = mono_mb_add_local (mb, &mono_defaults.int_class->byval_arg);
+
+ /* Check null */
+ mono_mb_emit_ldarg (mb, argnum);
+ mono_mb_emit_byte (mb, CEE_BRFALSE);
+ label1 = mb->pos;
+ mono_mb_emit_i4 (mb, 0);
+
+ mono_mb_emit_ldloc (mb, tmp_locals [i]);
+ mono_mb_emit_stloc (mb, src_ptr);
+
+ /* Emit marshalling loop */
+ index_var = mono_mb_add_local (mb, &mono_defaults.int_class->byval_arg);
+ mono_mb_emit_byte (mb, CEE_LDC_I4_0);
+ mono_mb_emit_stloc (mb, index_var);
+ label2 = mb->pos;
+ mono_mb_emit_ldloc (mb, index_var);
+ mono_mb_emit_ldarg (mb, argnum);
+ mono_mb_emit_byte (mb, CEE_LDLEN);
+ mono_mb_emit_byte (mb, CEE_BGE);
+ label3 = mb->pos;
+ mono_mb_emit_i4 (mb, 0);
+
+ /* Emit marshalling code */
+
+ /* set the src_ptr */
+ mono_mb_emit_ldloc (mb, src_ptr);
+ mono_mb_emit_byte (mb, CEE_STLOC_0);
+
+ /* set dst_ptr */
+ mono_mb_emit_ldarg (mb, argnum);
+ mono_mb_emit_ldloc (mb, index_var);
+ mono_mb_emit_byte (mb, CEE_LDELEMA);
+ mono_mb_emit_i4 (mb, mono_mb_add_data (mb, eklass));
+ mono_mb_emit_byte (mb, CEE_STLOC_1);
+
+ /* emit valuetype conversion code */
+ emit_struct_conv (mb, eklass, TRUE);
+
+ mono_mb_emit_add_to_local (mb, index_var, 1);
+ mono_mb_emit_add_to_local (mb, src_ptr, mono_class_native_size (eklass, NULL));
+
+ mono_mb_emit_byte (mb, CEE_BR);
+ mono_mb_emit_i4 (mb, label2 - (mb->pos + 4));
+
+ mono_mb_patch_addr (mb, label1, mb->pos - (label1 + 4));
+ mono_mb_patch_addr (mb, label3, mb->pos - (label3 + 4));
+ }
break;
+ case MONO_TYPE_BOOLEAN:
+ if (!t->byref)
+ continue;
+ mono_mb_emit_ldarg (mb, argnum);
+ mono_mb_emit_ldloc (mb, tmp_locals [i]);
+ if (mspecs [i + 1] != NULL && mspecs [i + 1]->native == MONO_NATIVE_VARIANTBOOL)
+ mono_mb_emit_byte (mb, CEE_NEG);
+ mono_mb_emit_byte (mb, CEE_STIND_I1);
}
}
-
if (!MONO_TYPE_IS_VOID(sig->ret))
mono_mb_emit_byte (mb, CEE_LDLOC_3);
csig = g_memdup (sig, sigsize);
csig->pinvoke = 0;
- res = mono_mb_create_method (mb, csig, csig->param_count + 16);
+ res = mono_mb_create_and_cache (cache, method,
+ mb, csig, csig->param_count + 16);
mono_mb_free (mb);
- g_hash_table_insert (cache, method, res);
-
for (i = sig->param_count; i >= 0; i--)
g_free (mspecs [i]);
g_free (mspecs);
+ //printf ("CODE FOR %s: \n%s.\n", mono_method_full_name (res, TRUE), mono_disasm_code (0, res, ((MonoMethodNormal*)res)->header->code, ((MonoMethodNormal*)res)->header->code + ((MonoMethodNormal*)res)->header->code_size));
+
return res;
}
return method;
cache = method->klass->image->synchronized_cache;
- if ((res = (MonoMethod *)g_hash_table_lookup (cache, method)))
+ if ((res = mono_marshal_find_in_cache (cache, method)))
return res;
sig = method->signature;
mono_mb_emit_ldloc (mb, ret_local);
mono_mb_emit_byte (mb, CEE_RET);
- res = mono_mb_create_method (mb, sig, sig->param_count + 16);
+ res = mono_mb_create_and_cache (cache, method,
+ mb, sig, sig->param_count + 16);
mono_mb_free (mb);
header = ((MonoMethodNormal *)res)->header;
header->num_clauses = 1;
header->clauses = clause;
- g_hash_table_insert (cache, method, res);
-
return res;
}
return result;
}
+void
+mono_marshal_set_last_error (void)
+{
+#ifdef WIN32
+ TlsSetValue (last_error_tls_id, (gpointer)GetLastError ());
+#else
+ TlsSetValue (last_error_tls_id, (gpointer)errno);
+#endif
+}
+
void
ves_icall_System_Runtime_InteropServices_Marshal_copy_to_unmanaged (MonoArray *src, gint32 start_index,
gpointer dest, gint32 length)
{
MONO_ARCH_SAVE_REGS;
- return (GetLastError ());
+ return ((guint32)TlsGetValue (last_error_tls_id));
}
guint32
info->fields [j].field = &klass->fields [i];
+ if ((klass->field.count == 1) && (klass->instance_size == sizeof (MonoObject)) &&
+ (strcmp (klass->fields [i].name, "$PRIVATE$") == 0)) {
+ /* This field is a hack inserted by MCS to empty structures */
+ continue;
+ }
+
switch (layout) {
case TYPE_ATTRIBUTE_AUTO_LAYOUT:
case TYPE_ATTRIBUTE_SEQUENTIAL_LAYOUT:
return 0;
}
+/* __alignof__ returns the preferred alignment of values not the actual alignment used by
+ the compiler so is wrong e.g. for Linux where doubles are aligned on a 4 byte boundary
+ but __alignof__ returns 8 - using G_STRUCT_OFFSET works better */
+#define ALIGNMENT(type) G_STRUCT_OFFSET(struct { char c; type x; }, x)
+
gint32
mono_marshal_type_size (MonoType *type, MonoMarshalSpec *mspec, gint32 *align,
gboolean as_field, gboolean unicode)
return 1;
case MONO_NATIVE_I2:
case MONO_NATIVE_U2:
+ case MONO_NATIVE_VARIANTBOOL:
*align = 2;
return 2;
case MONO_NATIVE_I4:
return 4;
case MONO_NATIVE_I8:
case MONO_NATIVE_U8:
- *align = 4;
+ *align = ALIGNMENT(guint64);
return 8;
case MONO_NATIVE_R4:
*align = 4;
return 4;
case MONO_NATIVE_R8:
- *align = 4;
+ *align = ALIGNMENT(double);
return 8;
case MONO_NATIVE_INT:
case MONO_NATIVE_UINT:
case MONO_NATIVE_IDISPATCH:
case MONO_NATIVE_INTERFACE:
case MONO_NATIVE_ASANY:
- case MONO_NATIVE_VARIANTBOOL:
case MONO_NATIVE_FUNC:
case MONO_NATIVE_LPSTRUCT:
- *align = 4;
+ *align = ALIGNMENT(gpointer);
return sizeof (gpointer);
case MONO_NATIVE_STRUCT:
klass = mono_class_from_mono_type (type);