public static Action <uint, TType> GenerateSwitchDelegate <TType>(string name, IEnumerable <InstructionInfo> instructionInfoList) { return(GenerateSwitch <Action <uint, TType> >(name, instructionInfoList, instructionInfo => { var instructionInfoName = (instructionInfo != null) ? instructionInfo.Name : "Default"; var methodInfo = typeof(TType).GetMethod(instructionInfoName); if (methodInfo == null) { throw new Exception( $"Cannot find method \'{instructionInfoName}\' on type \'{typeof(TType).Name}\' {name}, {instructionInfo?.Name} "); } //Console.WriteLine("MethodInfo: {0}", MethodInfo); //Console.WriteLine("Argument(1): {0}", typeof(TType)); if (methodInfo.IsStatic) { return Ast.Statement(Ast.CallStatic(methodInfo, Ast.Argument <TType>(1))); } else { return Ast.Statement(Ast.CallInstance(Ast.Argument <TType>(1), methodInfo)); } })); }
public void TestAdd0() { var Node = (AstNode)((ast.Argument <int>(0, "Arg") + ast.Immediate(0)) * ast.Immediate(3)); Assert.AreEqual("((Arg + 0) * 3)", new GeneratorCSharp().GenerateRoot(Node).ToString()); Node = new AstOptimizer().Optimize(Node); Assert.AreEqual("(Arg * 3)", new GeneratorCSharp().GenerateRoot(Node).ToString()); }
public void TestReinterpret() { var testArgument = Ast.Argument <float>(0, "Input"); var ast = Ast.Statements( Ast.Return( Ast.Reinterpret <int>(testArgument) ) ); var func = _generatorIl.GenerateDelegate <Func <float, int> >("Test", ast); int b = 1234567; float a = 0; *(int *)&a = b; Assert.Equal(b, func(a)); }
public void TestReinterpret() { var TestArgument = ast.Argument <float>(0, "Input"); var Ast = ast.Statements( ast.Return( ast.Reinterpret <int>(TestArgument) ) ); var Func = GeneratorIL.GenerateDelegate <Func <float, int> >("Test", Ast); int b = 1234567; float a = 0; *(int *)&a = b; Assert.AreEqual(b, Func(a)); }
public void TestMethod1() { var ast2 = ast.Statements( ast.Assign(ast.Argument <int>(0, "Argument0"), 777), ast.Return() ); Console.WriteLine(AstSerializer.SerializeAsXml(ast2)); }
public static MethodInfo GetFastMemoryReader(IntPtr fixedGlobalAddress) { var cacheKey = new CacheKey { FixedGlobalAddress = fixedGlobalAddress }; if (Cache.ContainsKey(cacheKey)) { return(Cache[cacheKey]); } const string dllName = "FastPspMemoryUtils_Gen.dll"; const string typeName = "Memory"; const string methodName = "Get"; //var assemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(new AssemblyName("FastPspMemoryUtils_Gen"), AssemblyBuilderAccess.RunAndCollect, dllName); var assemblyBuilder = AssemblyBuilder.DefineDynamicAssembly(new AssemblyName("FastPspMemoryUtils_Gen"), AssemblyBuilderAccess.RunAndCollect); //var moduleBuilder = assemblyBuilder.DefineDynamicModule(assemblyBuilder.GetName().Name, dllName, true); var moduleBuilder = assemblyBuilder.DefineDynamicModule(assemblyBuilder.GetName().Name); var typeBuilder = moduleBuilder.DefineType(typeName, TypeAttributes.Sealed | TypeAttributes.Public | TypeAttributes.Class); var method = typeBuilder.DefineMethod(methodName, MethodAttributes.Final | MethodAttributes.Public | MethodAttributes.Static, CallingConventions.Standard, typeof(void *), new[] { typeof(uint) }); var constructorInfo = typeof(MethodImplAttribute).GetConstructor(new[] { typeof(MethodImplOptions) }); method.SetCustomAttribute(new CustomAttributeBuilder( constructorInfo, new object[] { MethodImplOptions.AggressiveInlining })); var constructor = typeof(TargetedPatchingOptOutAttribute).GetConstructor(new[] { typeof(string) }); method.SetCustomAttribute(new CustomAttributeBuilder( constructor, new object[] { "Performance critical to inline across NGen image boundaries" })); //Method.GetILGenerator(); var astTree = ast.Return( ast.Cast( typeof(void *), ast.Immediate(fixedGlobalAddress) + ast.Binary(ast.Argument <uint>(0), "&", ast.Immediate(FastPspMemory.FastMemoryMask)) ) ); new GeneratorIl().Reset().Init(method, method.GetILGenerator()).GenerateRoot(astTree); var type = typeBuilder.CreateType(); Cache[cacheKey] = type.GetMethod(methodName); return(Cache[cacheKey]); }
public void TestMethod2() { var pool = new IlInstanceHolderPool(typeof(int), 16); var item = pool.Alloc(); var astNode = ast.Statements( ast.Assign(ast.StaticFieldAccess(item.FieldInfo), ast.Argument <int>(0, "Value")), ast.Return() ); _testOutputHelper.WriteLine(GeneratorCSharp.GenerateString <GeneratorCSharp>(astNode)); var generatorIl = new GeneratorIl(); var itemSet = generatorIl.GenerateDelegate <Action <int> >("ItemSet", astNode); itemSet(10); Assert.Equal(10, item.Value); }
public void TestMethod2() { var Pool = new ILInstanceHolderPool(typeof(int), 16); var Item = Pool.Alloc(); var AstNode = ast.Statements( ast.Assign(ast.StaticFieldAccess(Item.FieldInfo), ast.Argument <int>(0, "Value")), ast.Return() ); Console.WriteLine(GeneratorCSharp.GenerateString <GeneratorCSharp>(AstNode)); var GeneratorIL = new GeneratorIL(); var ItemSet = GeneratorIL.GenerateDelegate <Action <int> >("ItemSet", AstNode); ItemSet(10); Assert.AreEqual(10, Item.Value); }
static Action <uint, CpuInterpreter> GenerateSwitch() { var CIType = typeof(CpuInterpreter); var nameToInfo = new Dictionary <string, MethodInfo>(); foreach (var methodInfo in CIType.GetMethods()) { var instructionNames = methodInfo.GetSingleAttribute <InstructionName>(); if (instructionNames != null) { nameToInfo[instructionNames.Name] = methodInfo; } } return(EmitLookupGenerator.GenerateSwitch <Action <uint, CpuInterpreter> >(nameof(CpuInterpreterSwitchGenerator), InstructionTable.All, instructionInfo => { var methodInfo = nameToInfo[InstructionNames.Unknown]; if (instructionInfo != null && nameToInfo.ContainsKey(instructionInfo.Name)) { methodInfo = nameToInfo[instructionInfo.Name]; } Console.WriteLine($"{methodInfo} {instructionInfo}"); if (methodInfo == null) { } return Ast.Statements( Ast.Statement(Ast.CallInstance(Ast.Argument <CpuInterpreter>(1), methodInfo)), Ast.Return() ); } )); }
public Result Disassemble(uint pc, Instruction instruction) { if (ProcessCallback == null) { var dictionary = new Dictionary <InstructionInfo, int>(); InstructionLookup = InstructionTable.All.ToArray(); for (var n = 0; n < InstructionLookup.Length; n++) { dictionary[InstructionLookup[n]] = n; } ProcessCallback = EmitLookupGenerator.GenerateSwitch <Func <uint, MipsDisassembler, Result> >("", InstructionTable.All, instructionInfo => ast.Return(ast.CallStatic( (Func <uint, int, Result>)_InternalHandle, ast.Argument <uint>(0), instructionInfo != null ? dictionary[instructionInfo] : -1 ))); } var result = ProcessCallback(instruction, this); if (result.InstructionInfo == null) { Console.Error.WriteLine( $"Instruction at 0x{pc:X8} with data 0x{(uint) instruction:X8} didn't generate a value"); result.InstructionInfo = InstructionTable.Unknown; } result.InstructionPc = pc; return(result); }