Ejemplo n.º 1
0
static void CVMCoder_ValueCtorArgs(ILCoder *coder, ILClass *classInfo,
								   ILEngineStackItem *args, ILUInt32 numArgs)
{
	ILUInt32 posn = ComputeStackSize(coder, args, numArgs);
	ILUInt32 size = GetTypeSize(_ILCoderToILCVMCoder(coder)->process,
								ILType_FromValueType(classInfo));
	CVM_OUT_DWIDE(COP_NEW_VALUE, posn, size);
	CVM_ADJUST(size + 1);
}
Ejemplo n.º 2
0
/*
 * Convert a "struct" class into a "ffi" type descriptor.
 * Returns zero if out of memory.
 */
static ffi_type *StructToFFI(ILExecProcess *process, ILClass *classInfo)
{
#if !FFI_NO_STRUCTS
	ILClass *current;
	ILField *field;
	unsigned numFields;
	ffi_type *descr;
	ffi_type **fieldTypes;
	ILUInt32 explicitSize;
	ILUInt32 explicitAlignment;

	/* Count the number of non-static fields in the class */
	numFields = 0;
	if(!ILClass_IsExplicitLayout(classInfo))
	{
		current = classInfo;
		while(current != 0)
		{
			field = 0;
			while((field = (ILField *)ILClassNextMemberByKind
				(current, (ILMember *)field, IL_META_MEMBERKIND_FIELD)) != 0)
			{
				if(!ILField_IsStatic(field))
				{
					++numFields;
				}
			}
			current = ILClass_ParentClass(current);
		}
		explicitSize = 0;
		explicitAlignment = 0;
	}
	else
	{
		/* Use the explicit layout information from "layout.c" */
		explicitSize = _ILLayoutClassReturn(process, classInfo, &explicitAlignment);
	}

	/* Allocate space for the struct's type descriptor */
	descr = (ffi_type *)ILMalloc(sizeof(ffi_type) +
								 sizeof(ffi_type *) * (numFields + 1));
	if(!descr)
	{
		return 0;
	}
	fieldTypes = (ffi_type **)(descr + 1);

	/* Initialize the main descriptor's fields */
	descr->size = (size_t)explicitSize;
	descr->alignment = (unsigned short)explicitAlignment;
	descr->type = FFI_TYPE_STRUCT;
	descr->elements = fieldTypes;

	/* Populate the "fieldTypes" table with the "ffi" type descriptors */
	fieldTypes[numFields] = 0;
	if(!ILClass_IsExplicitLayout(classInfo))
	{
		numFields = 0;
		if(!PopulateStructFFI(process, classInfo, fieldTypes, &numFields))
		{
			ILFree(descr);
			return 0;
		}
	}

	/* Return the descriptor to the caller */
	return descr;
#else
	char *name = ILTypeToName(ILType_FromValueType(classInfo));
	if(name)
	{
		fprintf(stderr, "Cannot pass structures of type `%s' by value to "
						"PInvoke methods\n", name);
		ILFree(name);
	}
	return 0;
#endif /* !FFI_NO_STRUCTS */
}