Ejemplo n.º 1
0
/*
 * Do the real delegate invokation.
 */
static void _DelegateInvoke(ILDelegateInvokeParams *params)
{
	ILMethod *method;
	ILType *type;
	ILUInt32 size;
	PackDelegateUserData userData;

	/* If this is a multicast delegate, then execute "prev" first */
	if(params->delegate->prev)
	{
		ILDelegateInvokeParams prevParams;

		prevParams.thread = params->thread;
		prevParams.cif = params->cif;
		prevParams.result = params->result;
		prevParams.args = params->args;
		prevParams.delegate = (System_Delegate *)(params->delegate->prev);
;
		_DelegateInvoke(&prevParams);
		if(_ILExecThreadHasException(params->thread))
		{
			return;
		}
	}

	/* Extract the method from the delegate */
	method = params->delegate->methodInfo;
	if(!method)
	{
		ILExecThreadThrowSystem(params->thread, "System.MissingMethodException",
								(const char *)0);
		return;
	}

	/* Call the method */
	userData.args = params->args;
	userData.pinvokeInfo = (ILMethod *)ILTypeGetDelegateMethod
		(ILType_FromClass(GetObjectClass(params->delegate)));
	userData.needThis = 0;
	if(_ILCallMethod(params->thread, method,
				     UnpackDelegateResult, params->result,
				     0, params->delegate->target,
				     PackDelegateParams, &userData))
	{
		/* An exception occurred, which is already stored in the thread */
		type = ILMethod_Signature(method);
		type = ILTypeGetEnumType(ILTypeGetReturn(type));
		if(type != ILType_Void)
		{
			/* Clear the native return value, because we cannot assume
			   that the native caller knows how to handle exceptions */
			size = ILSizeOfType(params->thread, type);
			ILMemZero(params->result, size);
		}
	}
}
Ejemplo n.º 2
0
/*
 * Populate a list of "ffi" type descriptors with information
 * about the non-static fields of a class.  Returns zero if
 * out of memory.
 */
static int PopulateStructFFI(ILExecProcess *process, ILClass *classInfo,
							  ffi_type **fieldTypes, unsigned *posn)
{
	ILClass *parent;
	ILField *field;
	ILType *type;
	ffi_type *ffi;

	/* Process the parent class first */
	parent = ILClass_ParentClass(classInfo);
	if(parent)
	{
		if(!PopulateStructFFI(process, parent, fieldTypes, posn))
		{
			return 0;
		}
	}

	/* Process the non-static fields in this class */
	field = 0;
	while((field = (ILField *)ILClassNextMemberByKind
			(classInfo, (ILMember *)field, IL_META_MEMBERKIND_FIELD)) != 0)
	{
		if(!ILField_IsStatic(field))
		{
			type = ILTypeGetEnumType(ILField_Type(field));
			if(ILType_IsValueType(type))
			{
				/* Process an embedded structure type */
				ffi = StructToFFI(process, ILType_ToValueType(type));
				if(!ffi)
				{
					return 0;
				}
			}
			else
			{
				/* Process a non-structure type */
				ffi = TypeToFFI(process, type, 0);
			}
			fieldTypes[(*posn)++] = ffi;
		}
	}

	/* Done */
	return 1;
}
Ejemplo n.º 3
0
/*
 * Determine if a stack item is assignment-compatible with
 * a particular memory slot (argument, local, field, etc).
 */
static int AssignCompatible(ILMethod *method, ILEngineStackItem *item,
							ILType *type, int unsafeAllowed)
{
	ILImage *image;
	ILClass *classInfo;
	ILClass *classInfo2;
	ILMethod *methodRef;
	ILType *objType;

	/* Check for safe and unsafe pointer assignments */
	if(item->engineType == ILEngineType_I)
	{
		methodRef = MethodRefToMethod(item->typeInfo);
		if(methodRef)
		{
			/* Assigning a method reference, obtained via "ldftn"
			   or "ldvirtftn", to a method pointer destination */
			if(ILTypeIdentical(ILMethod_Signature(methodRef), type))
			{
				return 1;
			}
		}
		else if(item->typeInfo != 0 && ILType_IsComplex(item->typeInfo))
		{
			/* May be trying to assign a method pointer to a method type */
			if(ILType_IsMethod(item->typeInfo))
			{
				if(ILTypeIdentical(item->typeInfo, type))
				{
					return 1;
				}
			}
		}
		if(unsafeAllowed)
		{
			if(type != 0 && ILType_IsComplex(type))
			{
				if((ILType_Kind(type) & IL_TYPE_COMPLEX_METHOD) != 0 ||
				   ILType_Kind(type) == IL_TYPE_COMPLEX_PTR)
				{
					return 1;
				}
			}
		}
	}

	/* Check for regular assignments */
	if(item->engineType == ILEngineType_I4 ||
	   item->engineType == ILEngineType_I)
	{
		type = ILTypeGetEnumType(type);
		switch((unsigned long)type)
		{
			case (unsigned long)ILType_Boolean:
			case (unsigned long)ILType_Int8:
			case (unsigned long)ILType_UInt8:
			case (unsigned long)ILType_Int16:
			case (unsigned long)ILType_UInt16:
			case (unsigned long)ILType_Char:
			case (unsigned long)ILType_Int32:
			case (unsigned long)ILType_UInt32:
			case (unsigned long)ILType_Int:
			case (unsigned long)ILType_UInt:	return 1;
			default: break;
		}

		if(!unsafeAllowed)
		{
			return 0;
		}

		/* Allow a native int to be assigned to a complex type */
		if(type != 0 && ILType_IsComplex(type) && 
						item->engineType == ILEngineType_I)
		{
			if(ILType_Kind(type) == IL_TYPE_COMPLEX_PTR ||
			  ILType_Kind(type) == IL_TYPE_COMPLEX_BYREF) 
			{
				return 1;
			}
		}
		return 0;
	}
	else if(item->engineType == ILEngineType_I8)
	{
		type = ILTypeGetEnumType(type);
		return (type == ILType_Int64 || type == ILType_UInt64);
	}
	else if(item->engineType == ILEngineType_F)
	{
		return (type == ILType_Float32 ||
		        type == ILType_Float64 ||
		        type == ILType_Float);
	}
	else if(item->engineType == ILEngineType_O)
	{
		if(!(item->typeInfo))
		{
			/* A "null" constant was pushed, which is
			   compatible with any object reference type */
			return IsObjectRef(type);
		}
		if(!IsObjectRef(type) || !IsObjectRef(item->typeInfo))
		{
			/* Both types must be object references */
			return 0;
		}
		/* make a copy to avoid unecessary complications */
		objType=item->typeInfo;
		if(ILType_IsArray(type) && ILType_IsArray(objType) &&
			(ILTypeGetRank(type) == ILTypeGetRank(objType)))
		{
			objType=ILTypeGetElemType(objType);
			type=ILTypeGetElemType(type);
		}
		image = ILProgramItem_Image(method);
		classInfo = ILClassResolve(ILClassFromType(image, 0, type, 0));
		classInfo2 = ILClassResolve
			(ILClassFromType(image, 0, objType, 0));
		if(classInfo && classInfo2)
		{
			/* Is the type a regular class or an interface? */
			if(!ILClass_IsInterface(classInfo))
			{
				/* Regular class: the value must inherit from the type */
				if(ILClassInheritsFrom(classInfo2, classInfo))
				{
					return 1;
				}

				/* If "classInfo2" is an interface, then the conversion
				   is OK if "type" is "System.Object", because all
				   interfaces inherit from "System.Object", even though
				   the metadata doesn't explicitly say so */
				if(ILClass_IsInterface(classInfo2))
				{
					return ILTypeIsObjectClass(type);
				}

				/* The conversion is not OK */
				return 0;
			}
			else
			{
				/* Interface which the value must implement or inherit from */
				return ILClassImplements(classInfo2, classInfo) ||
				       ILClassInheritsFrom(classInfo2, classInfo);
			}
		}
		else
		{
			return 0;
		}
	}
	else if(item->engineType == ILEngineType_MV)
	{
		/* Can only assign managed values to an exact type destination */
		return ILTypeIdentical(item->typeInfo, type);
	}
	else if(item->engineType == ILEngineType_TypedRef)
	{
		/* The type must be "typedref" */
		return (type == ILType_TypedRef);
	}
	else if(item->engineType == ILEngineType_M ||
	        item->engineType == ILEngineType_T)
	{
		/* Cannot assign managed pointers to variables or fields,
		   unless we are in "unsafe" mode */
		if(!unsafeAllowed)
		{
			return 0;
		}

		/* Allow an assignment to any pointer, reference, or native
		   destination, regardless of type.  This allows C/C++ code
		   to arbitrarily cast pointers via assignment */
		if(type != 0 && ILType_IsComplex(type))
		{
			if(ILType_Kind(type) == IL_TYPE_COMPLEX_PTR ||
			   ILType_Kind(type) == IL_TYPE_COMPLEX_BYREF ||
			   (ILType_Kind(type) & IL_TYPE_COMPLEX_METHOD) != 0)
			{
				return 1;
			}
		}
		else if(type == ILType_Int || type == ILType_UInt)
		{
			return 1;
		}
		return 0;
	}
	else
	{
		/* Invalid type: never assignment-compatible with anything */
		return 0;
	}
}
Ejemplo n.º 4
0
/*
 * Convert a type into an engine type.
 */
static ILEngineType TypeToEngineType(ILType *type)
{
	type = ILTypeGetEnumType(type);
	if(ILType_IsPrimitive(type))
	{
		switch(ILType_ToElement(type))
		{
			case IL_META_ELEMTYPE_BOOLEAN:
			case IL_META_ELEMTYPE_I1:
			case IL_META_ELEMTYPE_U1:
			case IL_META_ELEMTYPE_I2:
			case IL_META_ELEMTYPE_U2:
			case IL_META_ELEMTYPE_CHAR:
			case IL_META_ELEMTYPE_I4:
			case IL_META_ELEMTYPE_U4:		return ILEngineType_I4;

			case IL_META_ELEMTYPE_I8:
			case IL_META_ELEMTYPE_U8:		return ILEngineType_I8;

			case IL_META_ELEMTYPE_I:
			case IL_META_ELEMTYPE_U:		return ILEngineType_I;

			case IL_META_ELEMTYPE_R4:
			case IL_META_ELEMTYPE_R8:
			case IL_META_ELEMTYPE_R:		return ILEngineType_F;

			case IL_META_ELEMTYPE_TYPEDBYREF: return ILEngineType_TypedRef;
		}
		return ILEngineType_I4;
	}
	else if(ILType_IsValueType(type))
	{
		return ILEngineType_MV;
	}
	else if(ILType_IsComplex(type) && type != 0)
	{
		switch(ILType_Kind(type))
		{
			case IL_TYPE_COMPLEX_PTR:
			{
				/* Unsafe pointers are represented as native integers */
				return ILEngineType_I;
			}
			/* Not reached */

			case IL_TYPE_COMPLEX_BYREF:
			{
				/* Reference values are managed pointers */
				return ILEngineType_M;
			}
			/* Not reached */

			case IL_TYPE_COMPLEX_PINNED:
			{
				/* Pinned types are the same as their underlying type */
				return TypeToEngineType(ILType_Ref(type));
			}
			/* Not reached */

			case IL_TYPE_COMPLEX_CMOD_REQD:
			case IL_TYPE_COMPLEX_CMOD_OPT:
			{
				/* Strip the modifier and inspect the underlying type */
				return TypeToEngineType(type->un.modifier__.type__);
			}
			/* Not reached */

			case IL_TYPE_COMPLEX_METHOD:
			case IL_TYPE_COMPLEX_METHOD | IL_TYPE_COMPLEX_METHOD_SENTINEL:
			{
				/* Pass method pointers around the system as "I".  Higher
				   level code will also set the "typeInfo" field to reflect
				   the signature so that method pointers become verifiable */
				return ILEngineType_I;
			}
			/* Not reached */
		}
	}
	return ILEngineType_O;
}
Ejemplo n.º 5
0
/*
 * Convert a type into its primitive form, ignoring slight differences
 * in type that don't matter because we can blindly cast between the
 * equivalents without losing type-safety.
 */
static int ArgTypeToPrimitive(ILType *type)
{
	if(ILType_IsPrimitive(type))
	{
		switch(ILType_ToElement(type))
		{
			case IL_META_ELEMTYPE_BOOLEAN:
			case IL_META_ELEMTYPE_I1:
			case IL_META_ELEMTYPE_U1:
				return IL_META_ELEMTYPE_I1;

			case IL_META_ELEMTYPE_I2:
			case IL_META_ELEMTYPE_U2:
			case IL_META_ELEMTYPE_CHAR:
				return IL_META_ELEMTYPE_I2;

			case IL_META_ELEMTYPE_I4:
			case IL_META_ELEMTYPE_U4:
		#ifdef IL_NATIVE_INT32
			case IL_META_ELEMTYPE_I:
			case IL_META_ELEMTYPE_U:
		#endif
				return IL_META_ELEMTYPE_I4;

			case IL_META_ELEMTYPE_I8:
			case IL_META_ELEMTYPE_U8:
		#ifdef IL_NATIVE_INT64
			case IL_META_ELEMTYPE_I:
			case IL_META_ELEMTYPE_U:
		#endif
				return IL_META_ELEMTYPE_I8;

			case IL_META_ELEMTYPE_R4:
				return IL_META_ELEMTYPE_R4;

			case IL_META_ELEMTYPE_R8:
			case IL_META_ELEMTYPE_R:
				return IL_META_ELEMTYPE_R8;

			default: break;
		}
		return IL_META_ELEMTYPE_END;
	}
	else if(ILType_IsValueType(type))
	{
		if(ILTypeIsEnum(type))
		{
			return ArgTypeToPrimitive(ILTypeGetEnumType(type));
		}
		else
		{
			return IL_META_ELEMTYPE_VALUETYPE;
		}
	}
	else if(ILType_IsPointer(type))
	{
	#ifdef IL_NATIVE_INT32
		return IL_META_ELEMTYPE_I4;
	#else
		return IL_META_ELEMTYPE_I8;
	#endif
	}
	else
	{
		return IL_META_ELEMTYPE_END;
	}
}
Ejemplo n.º 6
0
/*
 * Unpack the result of a delegate closure call.
 */
static void UnpackDelegateResult(ILExecThread *thread, ILMethod *method,
					             int isCtor, void *result, void *userData)
{
	ILMethod *pinvokeInfo = ((PackDelegateUserData *)userData)->pinvokeInfo;
	ILType *signature = ILMethod_Signature(method);
	ILType *paramType;
	ILUInt32 size, sizeInWords;
	ILNativeFloat tempFloat;
	ILUInt32 marshalType;
	char *customName;
	int customNameLen;
	char *customCookie;
	int customCookieLen;

	/* Marshal return types that need special handling */
	marshalType = ILPInvokeGetMarshalType
			(0, pinvokeInfo, 0, &customName, &customNameLen,
			 &customCookie, &customCookieLen, ILTypeGetReturn(signature));
	if(marshalType != IL_META_MARSHAL_DIRECT)
	{
		switch(marshalType)
		{
			case IL_META_MARSHAL_ANSI_STRING:
			{
				/* Marshal an ANSI string back to the native world */
				*((char **)result) = ILStringToAnsi
					(thread, (ILString *)(thread->stackTop[-1].ptrValue));
				--(thread->stackTop);
			}
			return;

			case IL_META_MARSHAL_UTF8_STRING:
			{
				/* Marshal a UTF-8 string back to the native world */
				*((char **)result) = ILStringToUTF8
					(thread, (ILString *)(thread->stackTop[-1].ptrValue));
				--(thread->stackTop);
			}
			return;

			case IL_META_MARSHAL_UTF16_STRING:
			{
				/* Marshal a UTF-16 string back to the native world */
				*((ILUInt16 **)result) = ILStringToUTF16
					(thread, (ILString *)(thread->stackTop[-1].ptrValue));
				--(thread->stackTop);
			}
			return;

			case IL_META_MARSHAL_FNPTR:
			{
				/* Convert a delegate into a function closure pointer */
				*((void **)result) = _ILDelegateGetClosure
					(thread, (ILObject *)(thread->stackTop[-1].ptrValue));
				--(thread->stackTop);
			}
			return;

			case IL_META_MARSHAL_ARRAY:
			{
				/* Convert an array into a pointer to its first member */
				void *array = thread->stackTop[-1].ptrValue;
				--(thread->stackTop);
				if(array)
				{
					*((void **)result) = ArrayToBuffer(array);
				}
				else
				{
					*((void **)result) = 0;
				}
			}
			return;

			case IL_META_MARSHAL_CUSTOM:
			{
				/* Marshal a custom value to the native world */
				*((void **)result) = _ILObjectToCustom
					(thread, (ILObject *)(thread->stackTop[-1].ptrValue),
					 customName, customNameLen, customCookie, customCookieLen);
				--(thread->stackTop);
			}
			return;
		}
	}

	/* Copy the return value into place */
	paramType = ILTypeGetEnumType(ILTypeGetReturn(signature));
	if(ILType_IsPrimitive(paramType))
	{
		/* Process a primitive value */
		switch(ILType_ToElement(paramType))
		{
			case IL_META_ELEMTYPE_VOID:		break;

			case IL_META_ELEMTYPE_BOOLEAN:
			case IL_META_ELEMTYPE_I1:
			case IL_META_ELEMTYPE_U1:
			case IL_META_ELEMTYPE_I2:
			case IL_META_ELEMTYPE_U2:
			case IL_META_ELEMTYPE_CHAR:
			case IL_META_ELEMTYPE_I4:
			case IL_META_ELEMTYPE_U4:
		#ifdef IL_NATIVE_INT32
			case IL_META_ELEMTYPE_I:
			case IL_META_ELEMTYPE_U:
		#endif
			{
				*((ILInt32 *)result) = thread->stackTop[-1].intValue;
				--(thread->stackTop);
			}
			break;

			case IL_META_ELEMTYPE_I8:
			case IL_META_ELEMTYPE_U8:
		#ifdef IL_NATIVE_INT64
			case IL_META_ELEMTYPE_I:
			case IL_META_ELEMTYPE_U:
		#endif
			{
				ILMemCpy(result,
						 thread->stackTop - CVM_WORDS_PER_LONG,
						 sizeof(ILInt64));
				thread->stackTop -= CVM_WORDS_PER_LONG;
			}
			break;

			case IL_META_ELEMTYPE_R4:
			{
				ILMemCpy(&tempFloat,
						 thread->stackTop - CVM_WORDS_PER_NATIVE_FLOAT,
						 sizeof(ILNativeFloat));
				*((ILFloat *)result) = (ILFloat)tempFloat;
				thread->stackTop -= CVM_WORDS_PER_NATIVE_FLOAT;
			}
			break;

			case IL_META_ELEMTYPE_R8:
			{
				ILMemCpy(&tempFloat,
						 thread->stackTop - CVM_WORDS_PER_NATIVE_FLOAT,
						 sizeof(ILNativeFloat));
				*((ILDouble *)result) = (ILDouble)tempFloat;
				thread->stackTop -= CVM_WORDS_PER_NATIVE_FLOAT;
			}
			break;

			case IL_META_ELEMTYPE_R:
			{
				ILMemCpy(result,
						 thread->stackTop - CVM_WORDS_PER_NATIVE_FLOAT,
						 sizeof(ILNativeFloat));
				thread->stackTop -= CVM_WORDS_PER_NATIVE_FLOAT;
			}
			break;

			case IL_META_ELEMTYPE_TYPEDBYREF:
			{
				ILMemCpy(result,
						 thread->stackTop - CVM_WORDS_PER_TYPED_REF,
						 sizeof(ILTypedRef));
				thread->stackTop -= CVM_WORDS_PER_TYPED_REF;
			}
			break;
		}
	}
	else if(ILType_IsClass(paramType))
	{
		/* Process an object reference */
		*((void **)result) = thread->stackTop[-1].ptrValue;
		--(thread->stackTop);
	}
	else if(ILType_IsValueType(paramType))
	{
		/* Process a value type */
		size = ILSizeOfType(thread, paramType);
		sizeInWords = ((size + sizeof(CVMWord) - 1) / sizeof(CVMWord));
		ILMemCpy(result, thread->stackTop - sizeInWords, size);
		thread->stackTop -= sizeInWords;
	}
	else
	{
		/* Assume that everything else is an object reference */
		*((void **)result) = thread->stackTop[-1].ptrValue;
		--(thread->stackTop);
	}
}
Ejemplo n.º 7
0
/*
 * Pack the parameters for a delegate closure call onto the CVM stack.
 */
static int PackDelegateParams(ILExecThread *thread, ILMethod *method,
					          int isCtor, void *_this, void *userData)
{
	void **args = ((PackDelegateUserData *)userData)->args;
	ILMethod *pinvokeInfo = ((PackDelegateUserData *)userData)->pinvokeInfo;
	ILType *signature = ILMethod_Signature(method);
	CVMWord *stacktop, *stacklimit;
	ILUInt32 param, numParams;
	ILType *paramType;
	void *ptr;
	ILUInt32 size, sizeInWords;
	ILNativeFloat tempFloat;
	ILUInt32 marshalType;
	char *customName;
	int customNameLen;
	char *customCookie;
	int customCookieLen;
	char *strValue;

	/* Get the top and extent of the stack */
	stacktop = thread->stackTop;
	stacklimit = thread->stackLimit;

	/* Push the arguments onto the evaluation stack */
	if(ILType_HasThis(signature))
	{
		/* Push the "this" argument */
		CHECK_SPACE(1);
		if(((PackDelegateUserData *)userData)->needThis)
		{
			/* We get the "this" value from the incoming arguments */
			stacktop->ptrValue = *((void **)(*args));
			++args;
		}
		else
		{
			/* We get the "this" value from the delegate object */
			stacktop->ptrValue = _this;
		}
		++stacktop;
	}
	numParams = ILTypeNumParams(signature);
	for(param = 1; param <= numParams; ++param)
	{
		/* Marshal parameters that need special handling */
		marshalType = ILPInvokeGetMarshalType(0, pinvokeInfo, param,
											  &customName, &customNameLen,
											  &customCookie, &customCookieLen,
											  ILTypeGetParam(signature, param));
		if(marshalType != IL_META_MARSHAL_DIRECT)
		{
			switch(marshalType)
			{
				case IL_META_MARSHAL_ANSI_STRING:
				{
					/* Marshal an ANSI string from the native world */
					CHECK_SPACE(1);
					strValue = *((char **)(*args));
					if(strValue)
					{
						stacktop->ptrValue = ILStringCreate(thread, strValue);

						/* Free the native string */
						ILFreeNativeString(strValue);

						if(!(stacktop->ptrValue))
						{
							return 1;
						}
					}
					else
					{
						stacktop->ptrValue = 0;
					}
					++args;
					++stacktop;
				}
				continue;

				case IL_META_MARSHAL_UTF8_STRING:
				{
					/* Marshal a UTF-8 string from the native world */
					CHECK_SPACE(1);
					strValue = *((char **)(*args));
					if(strValue)
					{
						stacktop->ptrValue =
							ILStringCreateUTF8(thread, strValue);

						/* Free the native string */
						ILFreeNativeString(strValue);

						if(!(stacktop->ptrValue))
						{
							return 1;
						}
					}
					else
					{
						stacktop->ptrValue = 0;
					}
					++args;
					++stacktop;
				}
				continue;

				case IL_META_MARSHAL_UTF16_STRING:
				{
					/* Marshal a UTF-16 string from the native world */
					CHECK_SPACE(1);
					strValue = *((char **)(*args));
					if(strValue)
					{
						stacktop->ptrValue =
							ILStringWCreate(thread, (ILUInt16 *)strValue);

						/* Free the native string */
						ILFreeNativeString(strValue);
					
						if(!(stacktop->ptrValue))
						{
							return 1;
						}
					}
					else
					{
						stacktop->ptrValue = 0;
					}
					++args;
					++stacktop;
				}
				continue;

				case IL_META_MARSHAL_CUSTOM:
				{
					/* Marshal a custom value from the native world */
					CHECK_SPACE(1);
					stacktop->ptrValue = _ILCustomToObject
						(thread, *((void **)(*args)),
						 customName, customNameLen,
						 customCookie, customCookieLen);
					if(_ILExecThreadHasException(thread))
					{
						return 1;
					}
					++args;
					++stacktop;
				}
				continue;
			}
		}

		/* Marshal the parameter directly */
		paramType = ILTypeGetEnumType(ILTypeGetParam(signature, param));
		if(ILType_IsPrimitive(paramType))
		{
			/* Process a primitive value */
			switch(ILType_ToElement(paramType))
			{
				case IL_META_ELEMTYPE_VOID:		break;

				case IL_META_ELEMTYPE_BOOLEAN:
				case IL_META_ELEMTYPE_I1:
				case IL_META_ELEMTYPE_U1:
				case IL_META_ELEMTYPE_I2:
				case IL_META_ELEMTYPE_U2:
				case IL_META_ELEMTYPE_CHAR:
				case IL_META_ELEMTYPE_I4:
				case IL_META_ELEMTYPE_U4:
			#ifdef IL_NATIVE_INT32
				case IL_META_ELEMTYPE_I:
				case IL_META_ELEMTYPE_U:
			#endif
				{
					CHECK_SPACE(1);
					stacktop->intValue = *((ILInt32 *)(*args));
					++args;
					++stacktop;
				}
				break;

				case IL_META_ELEMTYPE_I8:
				case IL_META_ELEMTYPE_U8:
			#ifdef IL_NATIVE_INT64
				case IL_META_ELEMTYPE_I:
				case IL_META_ELEMTYPE_U:
			#endif
				{
					CHECK_SPACE(CVM_WORDS_PER_LONG);
					ILMemCpy(stacktop, *args, sizeof(ILInt64));
					++args;
					stacktop += CVM_WORDS_PER_LONG;
				}
				break;

				case IL_META_ELEMTYPE_R4:
				{
					CHECK_SPACE(CVM_WORDS_PER_NATIVE_FLOAT);
					tempFloat = (ILNativeFloat)(*((ILFloat *)(*args)));
					ILMemCpy(stacktop, &tempFloat, sizeof(ILNativeFloat));
					++args;
					stacktop += CVM_WORDS_PER_NATIVE_FLOAT;
				}
				break;

				case IL_META_ELEMTYPE_R8:
				{
					CHECK_SPACE(CVM_WORDS_PER_NATIVE_FLOAT);
					tempFloat = (ILNativeFloat)(*((ILDouble *)(*args)));
					ILMemCpy(stacktop, &tempFloat, sizeof(ILNativeFloat));
					++args;
					stacktop += CVM_WORDS_PER_NATIVE_FLOAT;
				}
				break;

				case IL_META_ELEMTYPE_R:
				{
					CHECK_SPACE(CVM_WORDS_PER_NATIVE_FLOAT);
					ILMemCpy(stacktop, *args, sizeof(ILNativeFloat));
					++args;
					stacktop += CVM_WORDS_PER_NATIVE_FLOAT;
				}
				break;

				case IL_META_ELEMTYPE_TYPEDBYREF:
				{
					CHECK_SPACE(CVM_WORDS_PER_TYPED_REF);
					ILMemCpy(stacktop, *args, sizeof(ILTypedRef));
					++args;
					stacktop += CVM_WORDS_PER_TYPED_REF;
				}
				break;
			}
		}
		else if(ILType_IsClass(paramType))
		{
			/* Process an object reference */
			CHECK_SPACE(1);
			stacktop->ptrValue = *((void **)(*args));
			++args;
			++stacktop;
		}
		else if(ILType_IsValueType(paramType))
		{
			/* Process a value type which was passed by value */
			ptr = *args;
			++args;
			size = ILSizeOfType(thread, paramType);
			sizeInWords = ((size + sizeof(CVMWord) - 1) / sizeof(CVMWord));
			CHECK_SPACE(sizeInWords);
			ILMemCpy(stacktop, ptr, size);
			stacktop += sizeInWords;
		}
		else if(paramType != 0 && ILType_IsComplex(paramType) &&
				(ILType_Kind(paramType) == IL_TYPE_COMPLEX_BYREF || 
				 ILType_Kind(paramType) == IL_TYPE_COMPLEX_PTR))
		{
			/* Process a value that is being passed by reference */
			CHECK_SPACE(1);
			stacktop->ptrValue = *((void **)(*args));
			++args;
			++stacktop;
		}
		else
		{
			/* Assume that everything else is an object reference */
			CHECK_SPACE(1);
			stacktop->ptrValue = *args;
			++args;
			++stacktop;
		}
	}

	/* Update the stack top */
	thread->stackTop = stacktop;
	return 0;
}
Ejemplo n.º 8
0
void *_ILMakeCifForConstructor(ILExecProcess *process, ILMethod *method, int isInternal)
{
	ILType *signature = ILMethod_Signature(method);
	ILUInt32 numArgs;
	ILUInt32 numParams;
	ffi_cif *cif;
	ffi_type **args;
	ffi_type *rtype;
	ILUInt32 arg;
	ILUInt32 param;

	/* Determine the number of argument blocks that we need */
	numArgs = numParams = ILTypeNumParams(signature);
	if(isInternal)
	{
		/* This is an "internalcall" or "runtime" method
		   which needs an extra argument for the thread */
		++numArgs;
	}

	/* Allocate space for the cif */
	cif = (ffi_cif *)ILMalloc(sizeof(ffi_cif) +
							  sizeof(ffi_type *) * numArgs);
	if(!cif)
	{
		return 0;
	}
	args = ((ffi_type **)(cif + 1));

	/* The return value is always a pointer, indicating the object
	   that was just allocated by the constructor */
	rtype = &ffi_type_pointer;

	/* Convert the argument types */
	arg = 0;
	if(isInternal)
	{
		/* Pointer argument for the thread */
		args[arg++] = &ffi_type_pointer;
	}
	for(param = 1; param <= numParams; ++param)
	{
		args[arg++] = TypeToFFI(process, ILTypeGetEnumType
									(ILTypeGetParam(signature, param)),
							    isInternal);
	}

	/* Limit the number of arguments if we cannot use raw mode */
	if(!_ILCVMCanUseRawCalls(method, isInternal) &&
	   numArgs > (CVM_MAX_NATIVE_ARGS + 1))
	{
		numArgs = CVM_MAX_NATIVE_ARGS + 1;
	}

	/* Prepare the "ffi_cif" structure for the call */
	if(ffi_prep_cif(cif, FFI_DEFAULT_ABI, numArgs, rtype, args) != FFI_OK)
	{
		fprintf(stderr, "Cannot marshal a type in the definition of %s::%s\n",
				ILClass_Name(ILMethod_Owner(method)), ILMethod_Name(method));
		return 0;
	}

	/* Ready to go */
	return (void *)cif;
}