/// <summary> /// Starts a locking-block scope using the given <paramref cref="lockObject"/> parameter. /// </summary> /// <param name="lockObject">The object to do the locking on.</param> /// <exception cref="NotSupportedException">Nested locks currently not supported.</exception> public void BeginLock(FieldDefinition lockObject) { var body = _method.Body; _method.Body.InitLocals = true; _lockLocal = body.Variables.Count; if (_tryBegin != null) { throw new NotSupportedException("nested locks currently not supported"); } _tryBegin = _processor.CreateLdloc(_lockLocal); body.Variables.Add(new VariableDefinition(_module.TypeSystem.Object)); _lockCheckLocal = body.Variables.Count; var checkLocalVariable = new VariableDefinition(_module.TypeSystem.Boolean); body.Variables.Add(checkLocalVariable); _processor.Emit(OpCodes.Ldsfld, lockObject); _processor.EmitStloc(_lockLocal); _processor.Emit(OpCodes.Ldc_I4_0); _processor.EmitStloc(_lockCheckLocal); _processor.Append(_tryBegin); _processor.Emit(OpCodes.Ldloca_S, checkLocalVariable); MethodReference monitorEnter = _module.ImportReference(GetMonitorEnter(_module)); _processor.Emit(OpCodes.Call, monitorEnter); }
private void CreateLibraryLoadSymbol(MethodDefinition m, string mangledName, ILProcessor processor) { int fnPtrLocalIndex = m.Body.Variables.Count; m.Body.Variables.Add(new VariableDefinition(Module.TypeSystem.IntPtr)); var importLoaderAttribute = GetImportLoaderAttribute(); if (importLoaderAttribute.LoaderCookie == null) { processor.Emit(OpCodes.Ldnull); } else { processor.Emit(OpCodes.Ldstr, importLoaderAttribute.LoaderCookie); } processor.Emit(OpCodes.Call, Module.ImportReference(importLoaderAttribute.GetInstanceMethod)); processor.Emit(OpCodes.Call, CreateOrGetLibraryLoadLib(importLoaderAttribute)); var libLoaderGetProc = new MethodReference(nameof(ICustomLibraryLoader.GetProcAddress), Module.TypeSystem.IntPtr, Module.ImportReference(importLoaderAttribute.GetInstanceMethod.ReturnType)); libLoaderGetProc.HasThis = true; libLoaderGetProc.Parameters.Add(new ParameterDefinition(Module.TypeSystem.IntPtr)); libLoaderGetProc.Parameters.Add(new ParameterDefinition(Module.TypeSystem.String)); processor.Emit(OpCodes.Ldstr, _mangledName); processor.Emit(OpCodes.Callvirt, libLoaderGetProc); processor.Emit(OpCodes.Dup); processor.EmitStloc(fnPtrLocalIndex); var reloadFnPtr = processor.CreateLdloc(fnPtrLocalIndex); processor.Emit(OpCodes.Brtrue_S, reloadFnPtr); //TODO: perhaps special error handling processor.Append(reloadFnPtr); processor.Emit(OpCodes.Stsfld, FunctionPointer); }
private void AddLoadMethod(TypeDefinition type, FieldDefinition dataField) { MethodDefinition item = new MethodDefinition("Load", MethodAttributes.CompilerControlled | MethodAttributes.FamANDAssem | MethodAttributes.Family | MethodAttributes.HideBySig | MethodAttributes.Static, base.ModuleContext.IntPtrType); type.Methods.Add(item); MethodBody body = item.Body; ILProcessor iLProcessor = body.GetILProcessor(); TypeReference corLibType = base.ModuleContext.GetCorLibType("System.Byte"); TypeReference variableType = base.ModuleContext.GetCorLibType("System.Int32"); TypeReference reference3 = base.ModuleContext.GetCorLibType("System.IntPtr"); TypeReference reference4 = base.ModuleContext.GetCorLibType("System.Runtime.InteropServices.Marshal"); TypeReference reference5 = base.ModuleContext.GetCorLibType("System.IO.Stream"); TypeReference reference6 = base.ModuleContext.GetCorLibType("System.Type"); VariableDefinition definition2 = new VariableDefinition("stream", reference5); VariableDefinition definition3 = new VariableDefinition("length", variableType); VariableDefinition definition4 = new VariableDefinition("buffer", new ArrayType(corLibType)); VariableDefinition definition5 = new VariableDefinition("result", reference3); body.Variables.Add(definition2); body.Variables.Add(definition3); body.Variables.Add(definition4); body.Variables.Add(definition5); body.InitLocals = true; iLProcessor.Emit(OpCodes.Ldtoken, type); iLProcessor.Emit(OpCodes.Call, base.ModuleContext.GetCorLibMethod(reference6, "GetTypeFromHandle")); if (base.OperationContext.IsWSA) { iLProcessor.Emit(OpCodes.Call, base.ModuleContext.GetCorLibMethod("System.Reflection.IntrospectionExtensions", "GetTypeInfo")); iLProcessor.Emit(OpCodes.Callvirt, base.ModuleContext.GetCorLibMethod("System.Reflection.TypeInfo", "get_Assembly")); } else { iLProcessor.Emit(OpCodes.Callvirt, base.ModuleContext.GetCorLibMethod(reference6, "get_Assembly")); } iLProcessor.Emit(OpCodes.Ldstr, "UnityMetadata"); iLProcessor.Emit(OpCodes.Callvirt, base.ModuleContext.GetCorLibMethod("System.Reflection.Assembly", <> c.< > 9__2_0 ?? (< > c.< > 9__2_0 = new Func <MethodDefinition, bool>(< > c.< > 9. < AddLoadMethod > b__2_0)))); iLProcessor.EmitStloc(definition2); iLProcessor.EmitLdloc(definition2); iLProcessor.Emit(OpCodes.Callvirt, base.ModuleContext.GetCorLibMethod(reference5, "get_Length")); iLProcessor.Emit(OpCodes.Conv_I4); iLProcessor.EmitStloc(definition3); iLProcessor.EmitLdloc(definition3); iLProcessor.Emit(OpCodes.Newarr, corLibType); iLProcessor.EmitStloc(definition4); iLProcessor.EmitLdloc(definition2); iLProcessor.EmitLdloc(definition4); iLProcessor.EmitLdc_I4(0); iLProcessor.EmitLdloc(definition3); iLProcessor.Emit(OpCodes.Callvirt, base.ModuleContext.GetCorLibMethod(reference5, "Read")); iLProcessor.Emit(OpCodes.Pop); iLProcessor.EmitLdloc(definition2); iLProcessor.Emit(OpCodes.Callvirt, base.ModuleContext.GetCorLibMethod(reference5, <> c.< > 9__2_1 ?? (< > c.< > 9__2_1 = new Func <MethodDefinition, bool>(< > c.< > 9. < AddLoadMethod > b__2_1)))); iLProcessor.EmitLdloc(definition3); iLProcessor.Emit(OpCodes.Call, base.ModuleContext.GetCorLibMethod(reference4, "AllocCoTaskMem")); iLProcessor.EmitStloc(definition5); iLProcessor.EmitLdloc(definition4); iLProcessor.EmitLdc_I4(0); iLProcessor.EmitLdloc(definition5); iLProcessor.EmitLdloc(definition3); iLProcessor.Emit(OpCodes.Call, base.ModuleContext.GetCorLibMethod(reference4, <> c.< > 9__2_2 ?? (< > c.< > 9__2_2 = new Func <MethodDefinition, bool>(< > c.< > 9. < AddLoadMethod > b__2_2)))); foreach (AssemblyWrapper wrapper in base.MetadataContainer.Assemblies) { if (wrapper.GetUnityTypeMethod != null) { iLProcessor.EmitLdloc(definition5); iLProcessor.EmitLdc_I4(wrapper.Offset); iLProcessor.Emit(OpCodes.Add); iLProcessor.Emit(OpCodes.Ldftn, base.ModuleContext.Import(wrapper.GetUnityTypeMethod)); iLProcessor.Emit(OpCodes.Stind_I); } } iLProcessor.EmitLdloc(definition5); iLProcessor.Emit(OpCodes.Stsfld, dataField); iLProcessor.EmitLdloc(definition5); iLProcessor.Emit(OpCodes.Ret); }
public static void EmitBool(this ILProcessor il, bool value, VariableDefinition variable) { il.EmitBool(value); il.EmitStloc(variable); }