public Result CreateDelegate(AstNodeStm astNodeStm, int totalInstructions) { var time0 = DateTime.UtcNow; astNodeStm = AstOptimizerPsp.GlobalOptimize(_processor, Ast.Statements(astNodeStm, Ast.Return())); var time1 = DateTime.UtcNow; #if DEBUG_GENERATE_IL Console.WriteLine("{0}", GeneratorIL.GenerateToString <GeneratorILPsp>(DynamicMethod, AstNodeStm)); #endif #if DEBUG_GENERATE_IL_CSHARP Console.WriteLine("{0}", AstNodeExtensions.ToCSharpString(AstNodeStm).Replace("CpuThreadState.", "")); #endif Action <CpuThreadState> Delegate; var time2 = time1; var disableOptimizations = DynarecConfig.DisableDotNetJitOptimizations; if (totalInstructions >= DynarecConfig.InstructionCountToDisableOptimizations) { disableOptimizations = true; } if (Platform.IsMono) { disableOptimizations = false; } if (DynarecConfig.ForceJitOptimizationsOnEvenLargeFunctions) { disableOptimizations = false; } try { Delegate = MethodCreator.CreateDynamicMethod <Action <CpuThreadState> >( //Delegate = MethodCreator.CreateMethodInClass<Action<CpuThreadState>>( Assembly.GetExecutingAssembly().ManifestModule, $"DynamicMethod_0x{this._pc:X}", disableOptimizations, dynamicMethod => { astNodeStm.GenerateIl(dynamicMethod); time2 = DateTime.UtcNow; } ); } catch (InvalidProgramException) { Console.Error.WriteLine("Invalid Delegate:"); #if LOG_TRACE Console.WriteLine("Invalid Delegate:"); foreach (var Line in SafeILGenerator.GetEmittedInstructions()) { if (Line.Substr(0, 1) == ":") { Console.WriteLine("{0}", Line); } else { Console.WriteLine(" {0}", Line); } } #endif throw; } var time3 = DateTime.UtcNow; return(new Result { Delegate = Delegate, DisableOptimizations = disableOptimizations, TimeOptimize = time1 - time0, TimeGenerateIl = time2 - time1, TimeCreateDelegate = time3 - time2, }); }