Bekas

Hi,

Is the 'calli' IL opcode supported by the Xbox360 .NET framework (for calling managed function pointers)
Will I be able to get a pointer for a managed function (with 'ldftn') and call it using 'calli'


Re: XNA Framework Function pointers and calli opcode

Michael Hansen

you cant not use opcode , but here is a sample of getting a datastructure

in the datastructure you can have a void to call other things

here is a sample verry fast access to a class in memory

 

 

[StructLayout(LayoutKind.Sequential, Size=64, Pack=1,CharSet=CharSet.Ansi)]
public class MyDataStructure
{
	[ MarshalAs( UnmanagedType.ByValArray, SizeConst=40 )]
	public byte[] name;

	public byte eyeColor;
	public byte hairColor;
	public Int16 age; // Two byte integer
	public Int16 height;
	public Int16 weight;
	public Int16 noOfPets;
	public byte sign;

	[ MarshalAs( UnmanagedType.ByValArray, SizeConst=3 )]
	public byte[] birthDate; //yymmdd

	[ MarshalAs( UnmanagedType.ByValArray, SizeConst=10 )]
	public byte[] unused;

}
public static object RawDataToObject(ref byte[] rawData,Type overlayType)
{
	object result = null;

	GCHandle pinnedRawData = GCHandle.Alloc(rawData,GCHandleType.Pinned);
	try
	{

		// Get the address of the data array
		IntPtr pinnedRawDataPtr = pinnedRawData.AddrOfPinnedObject(); 

		// overlay the data type on top of the raw data
		result = Marshal.PtrToStructure(pinnedRawDataPtr ,	overlayType);
	}
	finally
	{
		// must explicitly release
		pinnedRawData.Free();
	} 

	return result;
}

// We assume GetNextRecord retuns the 64 byte array
byte[] arrayOfBytes = GetNextRecord();
MyDataStructure mds = (MyDataStructure)RawDataToObject(ref arrayOfBytes, typeof( MyDataStructure ) );


// Now we can reference the data structure
// however we would like
string name = ASCIIEncoding.ASCII.GetString( mds.name );
string age = mds.age.ToString();







Re: XNA Framework Function pointers and calli opcode

Bekas

Michael Hansen wrote:
you cant not use opcode

I was under the impression that you can create a managed assembly using ILASM and load it on Xbox360 through XNA, isn't this correct

Thanks for the sample, but my question was about function pointers. Using IL you can get the function pointer of a managed function using 'ldftn', store it, and, when necessary, call it using 'calli'.
Will these opcodes be allowed for the Xbox360 CLR




Re: XNA Framework Function pointers and calli opcode

Leaf.

I'm only guessing but I don't think there should be any difference between the IL opcodes that are supported on the two runtimes - the differences are in the libraries and run time implementations. Creating C# delegates compiles down to IL using 'ldftn' so that should be fine.

Cheers,
Leaf.





Re: XNA Framework Function pointers and calli opcode

Bekas

 Leaf. wrote:
I'm only guessing but I don't think there should be any difference between the IL opcodes that are supported on the two runtimes - the differences are in the libraries and run time implementations. Creating C# delegates compiles down to IL using 'ldftn' so that should be fine.

Actually delegates do not use 'ldftn'. For example, here's some IL code for calling a delegate:

//load delegate
IL_0001: ldsfld class Test.Program/VirMethodDelegate Test.Program::SubVirMethodDel

//pass parameters
IL_0006: ldc.i4.1

//invoke it
IL_0007: callvirt instance void Test.Program/VirMethodDelegate::Invoke(int32)


'ldftn'+'calli' is an unsafe way to call functions, and given the strict security of Xbox's CLR, it's not certain that it is supported..




Re: XNA Framework Function pointers and calli opcode

Leaf.

This bit of IL was from C# code that created a delegate, not code that loads a static delegate field onto the stack (like ldsfld would).

ldftn DataArray.DataNode DataArray.Tests.Tests::TestCommandHandler(DataArray.DataCommand)
newobj instance void DataArray.DelegateCommandHandler/CmdDelegate::.ctor(object, native int)

Still doesn't answer your question about 'calli'.

Cheers,
Leaf.





Re: XNA Framework Function pointers and calli opcode

Shawn Hargreaves - MSFT

The .NET CF does not implement the full set of IL opcodes from the desktop CLR. It has the subset needed to run clean (ie. verifiable) IL, and also the ops for unsafe code, but it doesn't include some of the more unusual opcodes that are only used to implement managed C++ and the IJW interop stuff. I suspect in fact they just implemented all the opcodes that the C# compiler generates :-)

I don't have a comprehensive list of which opcodes these are, but calli sounds rather C++ to me. The easiest way to find out for sure is probably just to try it and see!