Implemented convenience wrapper types

This commit is contained in:
Stefanos A 2013-11-27 00:03:03 +01:00
parent 82b0b477da
commit 4f58348502

View file

@ -115,13 +115,31 @@ namespace OpenTK.Rewrite
var entry_points = type.Fields.FirstOrDefault(f => f.Name == "EntryPoints"); var entry_points = type.Fields.FirstOrDefault(f => f.Name == "EntryPoints");
if (entry_points != null) if (entry_points != null)
{ {
foreach (var method in type.Methods) // Build list of entry point signatures and slots
var native_signatures = new List<MethodDefinition>();
native_signatures.AddRange(type.Methods
.Where(t => t.CustomAttributes.Any(a => a.AttributeType.Name == "SlotAttribute")));
// Rewrite all wrapper methods
var wrapper_signatures = new List<MethodDefinition>();
wrapper_signatures.AddRange(type.Methods
.Where(m => m.IsPublic && m.CustomAttributes.Any(a => a.AttributeType.Name == "AutoGeneratedAttribute")));
foreach (var wrapper in wrapper_signatures)
{ {
if (method.CustomAttributes.Any(a => a.AttributeType.Name == "SlotAttribute")) var signature_name = (string)wrapper.CustomAttributes
{ .First(a => a.AttributeType.Name == "AutoGeneratedAttribute")
ProcessMethod(method, entry_points); .Fields.First(f => f.Name == "EntryPoint").Argument.Value;
} var signature = native_signatures.First(s => s.Name == signature_name);
var slot = (int)signature.CustomAttributes
.First(a => a.AttributeType.Name == "SlotAttribute")
.ConstructorArguments[0].Value;
ProcessMethod(wrapper, signature, slot, entry_points);
} }
//RemoveNativeSignatures();
//RemoveSupportingAttributes();
} }
if (type.Name == "RewrittenAttribute") if (type.Name == "RewrittenAttribute")
@ -133,27 +151,96 @@ namespace OpenTK.Rewrite
} }
// Create body for method // Create body for method
static void ProcessMethod(MethodDefinition method, FieldDefinition entry_points) static void ProcessMethod(MethodDefinition wrapper, MethodDefinition native, int slot, FieldDefinition entry_points)
{ {
var nint = method.DeclaringType.Module.Import(typeof(IntPtr)); var nint = wrapper.DeclaringType.Module.Import(typeof(IntPtr));
var body = method.Body; //var nint = new TypeReference("System", "IntPtr", wrapper.DeclaringType.Module, null);
var instructions = body.Instructions; var body = wrapper.Body;
var il = body.GetILProcessor(); var il = body.GetILProcessor();
var slot_attribute = method.CustomAttributes var instructions = body.Instructions;
.First(a => a.AttributeType.Name == "SlotAttribute");
var slot = (int)slot_attribute.ConstructorArguments.First().Value;
instructions.Clear(); instructions.Clear();
if (wrapper.ReturnType != native.ReturnType)
{
Console.Error.WriteLine("Return type wrappers not implemented yet ({0})", native.Name);
}
// Declare pinned variables for every reference and array parameter // Declare pinned variables for every reference and array parameter
// and push each parameter on the stack // and push each parameter on the stack
EmitParameters(method, nint, body, il); EmitParameters(wrapper, nint, body, il);
// Patch convenience wrappers
if (wrapper.Parameters.Count < native.Parameters.Count)
{
int parameter_count = EmitParameters(wrapper, nint, body, il);
int difference = native.Parameters.Count - parameter_count;
if (difference == 2)
{
// Convert sized out-array/reference to return value, for example:
// void GenTextures(int n, int[] textures) -> int GenTexture()
// {
// const int n = 1;
// int buffers;
// calli GenTextures(n, &textures);
// return result;
// }
body.Variables.Add(new VariableDefinition(native.Parameters.Last().ParameterType));
il.Emit(OpCodes.Ldc_I4, 1); // const int n = 1
il.Emit(OpCodes.Ldloca, body.Variables.Count - 1); // &buffers
}
else if (difference == 1 && wrapper.ReturnType.Name != "Void")
{
// Convert unsized out-array/reference to return value, for example:
// void GetBoolean(GetPName pname, out bool data) -> bool GetBoolean(GetPName pname)
// {
// bool result;
// GetBooleanv(pname, &result);
// return result;
// }
body.Variables.Add(new VariableDefinition(native.ReturnType));
il.Emit(OpCodes.Ldloca, body.Variables.Count - 1);
}
else if (difference == 1 && wrapper.ReturnType.Name == "Void")
{
// Convert in-array/reference to single element, for example:
// void DeleteTextures(int n, ref int textures) -> void DeleteTexture(int texture)
// {
// const int n = 1;
// calli DeleteTextures(n, &textures);
// }
il.Emit(OpCodes.Ldc_I4, 1); // const int n = 1
il.Emit(OpCodes.Ldarga, wrapper.Parameters.Last()); // &textures
}
else
{
Console.Error.WriteLine("Unknown wrapper type for ({0})", native.Name);
}
}
// push the entry point address on the stack // push the entry point address on the stack
EmitEntryPoint(entry_points, il, slot); EmitEntryPoint(entry_points, il, slot);
// issue calli // issue calli
EmitCall(il, method); EmitCall(il, native);
if (wrapper.ReturnType.Name != "Void")
{
if (wrapper.Parameters.Count < native.Parameters.Count)
{
// Convenience wrapper. The result is stored in the last local variable
il.Emit(OpCodes.Ldloc, body.Variables.Count - 1);
}
else if (wrapper.ReturnType != native.ReturnType)
{
Console.Error.WriteLine("Return wrappers not implemented yet ({0})", native.Name);
}
else
{
// nothing to do, the native call leaves the return value
// on the stack and we return that unmodified to the caller.
}
}
// return // return
il.Emit(OpCodes.Ret); il.Emit(OpCodes.Ret);
@ -161,9 +248,10 @@ namespace OpenTK.Rewrite
body.OptimizeMacros(); body.OptimizeMacros();
} }
static void EmitParameters(MethodDefinition method, TypeReference nint, MethodBody body, ILProcessor il) static int EmitParameters(MethodDefinition method, TypeReference nint, MethodBody body, ILProcessor il)
{ {
for (int i = 0; i < method.Parameters.Count; i++) int i;
for (i = 0; i < method.Parameters.Count; i++)
{ {
var p = method.Parameters[i]; var p = method.Parameters[i];
il.Emit(OpCodes.Ldarg, i); il.Emit(OpCodes.Ldarg, i);
@ -177,6 +265,7 @@ namespace OpenTK.Rewrite
//il.Emit(OpCodes.Conv_I); //il.Emit(OpCodes.Conv_I);
} }
} }
return i;
} }
static void EmitEntryPoint(FieldDefinition entry_points, ILProcessor il, int slot) static void EmitEntryPoint(FieldDefinition entry_points, ILProcessor il, int slot)
@ -202,16 +291,5 @@ namespace OpenTK.Rewrite
// we do not need any special preparation before emiting calli. // we do not need any special preparation before emiting calli.
il.Emit(OpCodes.Calli, signature); il.Emit(OpCodes.Calli, signature);
} }
// IntPtr Pin<T>({ref} T{[];[,];[,,]} arg)
// Pin the parameter and return an unmanaged pointer
static void RewritePin(ILProcessor il, Instruction inst, MethodReference reference)
{
var greference = reference as GenericInstanceMethod;
if (greference == null)
throw new InvalidOperationException("reference must match generic method Pin<T>");
var ptype = greference.GenericArguments.First();
}
} }
} }