Ejemplo n.º 1
0
        public void ShouldExecuteAllTasksInOrder()
        {
            var commands = new Mock <IProgramCommand>[]
            {
                new Mock <IProgramCommand>(),
                new Mock <IProgramCommand>(),
                new Mock <IProgramCommand>()
            };

            int j = 0;

            for (int i = 0; i < commands.Length; ++i)
            {
                // I cannot reference 'i' inside Callback because it will have always value 3 inside callback.
                // It's always referencing the same thing inside callback. meh
                // I need to create local variable - oh gosh.
                int a = i;
                commands[i].Setup(x => x.Execute(robotMock.Object)).Callback(() => Assert.Equal(j++, a));
            }

            programMock.SetupGet(x => x.Commands).Returns(commands.Select(x => x.Object));

            var exec = new ProgramExecutor(programMock.Object, robotMock.Object);

            exec.Start();

            Thread.Sleep(1000);

            Assert.True(exec.IsCompleted);
        }
        public void ExecuteProgram_ReadsInCorrectly(int[] program, int positionValue, int inputValue)
        {
            mockWriter.Setup(mw => mw.ReadLine()).Returns("2");
            programExecutor = new ProgramExecutor(mockWriter.Object);
            var output = programExecutor.ExecuteProgram(program);

            Assert.Equal(inputValue, output[positionValue]);
        }
Ejemplo n.º 3
0
        private static void RemoveFixedPrograms(FuzzlynOptions options, string dir)
        {
            string[]     files      = Directory.GetFiles(dir, "*.cs");
            List <ulong> toRereduce = new List <ulong>();

            for (int i = 0; i < files.Length; i++)
            {
                Console.Title = $"Processing {i + 1}/{files.Length}";

                string          contents = File.ReadAllText(files[i]);
                MatchCollection matches  = Regex.Matches(contents, "// Seed: ([0-9]+)");
                if (matches.Count != 1)
                {
                    continue;
                }

                ulong seed = ulong.Parse(matches[0].Groups[1].Value);
                options.Seed = seed;
                var cg = new CodeGenerator(options);
                CompilationUnitSyntax original = cg.GenerateProgram(false);

                CompileResult debug   = Compiler.Compile(original, Compiler.DebugOptions);
                CompileResult release = Compiler.Compile(original, Compiler.ReleaseOptions);

                if (debug.CompileErrors.Length > 0 || release.CompileErrors.Length > 0)
                {
                    continue;
                }

                if (debug.RoslynException != null || release.RoslynException != null)
                {
                    continue;
                }

                ProgramPairResults execResults = ProgramExecutor.RunPair(new ProgramPair(debug.Assembly, release.Assembly));
                if (execResults.DebugResult.Checksum != execResults.ReleaseResult.Checksum ||
                    execResults.DebugResult.ExceptionType != execResults.ReleaseResult.ExceptionType)
                {
                    // Execute the reduced form to see if we get interesting behavior.
                    // Otherwise we may need to rereduce it.
                    if (!IsReducedVersionInteresting(execResults, contents))
                    {
                        toRereduce.Add(seed);
                        Console.WriteLine("Marking {0} for rereduction", Path.GetFileName(files[i]));
                    }

                    continue;
                }

                Console.WriteLine("Removing {0}", Path.GetFileName(files[i]));
                File.Delete(files[i]);
            }

            const string rereduceFile = "Rereduce_required.txt";

            File.WriteAllText(rereduceFile, string.Join(Environment.NewLine, toRereduce));
            Console.WriteLine("Wrote {0} seeds to be rereduced to '{1}'", toRereduce.Count, Path.GetFullPath(rereduceFile));
        }
        public void ExecuteProgram_OutputsCorrectDiagnosticsCode(int[] program, string readLineOutput,
                                                                 string expectedOutput)
        {
            mockWriter.Setup(mw => mw.ReadLine()).Returns(readLineOutput);
            programExecutor = new ProgramExecutor(mockWriter.Object);
            var output = programExecutor.ExecuteProgram(program);

            mockWriter.Verify(mw => mw.Write(It.Is <string>(s => s.Equals(expectedOutput))));
        }
Ejemplo n.º 5
0
        public void RunGalaxy()
        {
            var declarations = File.ReadAllLines("galaxy.txt");
            var main         = "ap galaxy ap ap cons 0 146";
            var executor     = new ProgramExecutor();
            var result       = executor.Execute(main, declarations);

            TestContext.Progress.WriteLine(result.PrettyPrint());
        }
Ejemplo n.º 6
0
        private List <Command> GetProgramCommands(JSProgram program)
        {
            (CDFG cdfg, _) = TestTools.ParseProgram(program);
            TestCommandExecutor      executor        = new TestCommandExecutor();
            ProgramExecutor <string> programExecutor = new ProgramExecutor <string>(executor);

            programExecutor.TimeBetweenCommands = 0;
            programExecutor.ShowEmptyRectangles = false;
            programExecutor.Run(10, 10, cdfg, false);

            return(executor.Commands);
        }
Ejemplo n.º 7
0
        private async void Window_Loaded(object sender, RoutedEventArgs e)
        {
            CompilerOptions.PROGRAM_FOLDER_PATH = PROGRAMS_FOLDER_PATH;
            var programData = InlineProgram.LoadProgram("Basic protocol for E. coli Quick");
            //var programData = InlineProgram.LoadProgram("showcasing using inline program block");
            //var programData = InlineProgram.LoadProgram("UsingDiluter3");

            var sassdad = XmlParser.Parse("<xml xmlns='http://www.w3.org/1999/xhtml'><variables><variable type='' id='gZ,c_HQgu:,yfA:J+Lxt'>input_fluid_name</variable></variables><block type='start' id='7skRs`d%@XtBz=rV_7{/' x='528' y='219'><statement name='program'><block type='inputDeclaration' id='2lbQ[al;v|T3G{$fy9Tn'><field name='inputName' id='gZ,c_HQgu:,yfA:J+Lxt' variabletype=''>asd</field><field name='inputAmount'>1</field></block></statement></block></xml>");

            for (int i = 0; i < 1; i++)
            {
                BenchmarkExecutor        executor = new BenchmarkExecutor();
                ProgramExecutor <string> CurrentlyExecutionProgram = new ProgramExecutor <string>(executor);
                CurrentlyExecutionProgram.TimeBetweenCommands     = 0;
                CurrentlyExecutionProgram.ShowEmptyRectangles     = false;
                CurrentlyExecutionProgram.EnableOptimizations     = true;
                CurrentlyExecutionProgram.EnableGarbageCollection = true;
                CurrentlyExecutionProgram.EnableSparseElectrodes  = false;

                CurrentlyExecutionProgram.Run(45, 45, programData.cdfg, false);
            }

            this.Close();

            //Run in another thread to not block the UI
            await Task.Run(() =>
            {
                SettingsInfo settings = new SettingsInfo();
                settings.LoadSettings(SETTINGS_FILE_PATH);

                this.Updater = new WebUpdater(Browser, settings);

                Browser.Load("costum://index.html");
                Browser.JavascriptObjectRepository.Register("saver", new Saver(Browser), true);
                Browser.JavascriptObjectRepository.Register("webUpdater", Updater, true);
                //Wait for the MainFrame to finish loading
                Browser.FrameLoadEnd += async(s, args) =>
                {
                    //Wait for the MainFrame to finish loading
                    if (args.Frame.IsMain)
                    {
                        //Run in another thread to not block the UI
                        await Task.Run(() =>
                        {
                            GiveSettingsToJS(settings);
                            GiveProgramsToJS();
                        });
                    }
                };
            });
        }
Ejemplo n.º 8
0
        public void Test()
        {
            var add4     = "add4 = ap add ap add ap add ap add 4";
            var main     = "ap add4 8 5 5 5 5 5 3 3 3";
            var fff      = "fff = ap t fff";
            var main1    = "ap fff 42";
            var executor = new ProgramExecutor();
            var result   = executor.Execute(main, add4);

            //var result = executor.Execute(main1, fff);N
            Console.WriteLine(result.PrettyPrint());
            Console.WriteLine(result.Print());
            Console.WriteLine(AstReducer.ReducesCount);
        }
Ejemplo n.º 9
0
        private ProgramPairResults CompileAndRun(CompilationUnitSyntax prog)
        {
            CompileResult progDebug   = Compiler.Compile(prog, Compiler.DebugOptions);
            CompileResult progRelease = Compiler.Compile(prog, Compiler.ReleaseOptions);

            if (progDebug.Assembly == null || progRelease.Assembly == null)
            {
                return(null);
            }

            ProgramPair        pair    = new ProgramPair(progDebug.Assembly, progRelease.Assembly);
            ProgramPairResults results = ProgramExecutor.RunPair(pair);

            return(results);
        }
Ejemplo n.º 10
0
        private void RunSimulator(CDFG cdfg, bool alreadyOptimized)
        {
            lock (simulatorLocker)
            {
                if (CurrentlyExecutionProgram != null)
                {
                    CurrentlyExecutionProgram.KeepRunning.Cancel();
                }
                simulatorThread?.Join();
                simulatorThread = new Thread(() =>
                {
                    try
                    {
                        int boardWidth = Settings.BoardWidth;
                        int boardHeight = Settings.BoardHeight;
                        int timeBetweenCommands = (int)((1f / Settings.CommandFrequency) * 1000);
                        using (SimulatorConnector executor = new SimulatorConnector(Browser, boardWidth, boardHeight))
                        {
                            CurrentlyExecutionProgram = new ProgramExecutor<string>(executor);
                            CurrentlyExecutionProgram.TimeBetweenCommands = timeBetweenCommands;
                            CurrentlyExecutionProgram.ShowEmptyRectangles = Settings.ShowEmptyRectangles;
                            CurrentlyExecutionProgram.EnableOptimizations = Settings.EnableOptimizations;
                            CurrentlyExecutionProgram.EnableGarbageCollection = Settings.EnableGC;
                            CurrentlyExecutionProgram.EnableSparseElectrodes = Settings.EnableSparseBoard;

                            CurrentlyExecutionProgram.Run(boardWidth, boardHeight, cdfg, alreadyOptimized);
                        }
                    }
                    catch (InternalRuntimeException e)
                    {
                        Browser.ExecuteScriptAsync($"ShowUnexpectedError(\"{e.Message.Replace('\"', '\'')}\");");
                    }
                    catch (RuntimeException e)
                    {
                        Browser.ExecuteScriptAsync($"ShowUnexpectedError(\"{e.Message.Replace('\"', '\'')}\");");
                    }
                    catch (ThreadInterruptedException)
                    {

                    }
                    catch (Exception e)
                    {
                        Debug.WriteLine(e.Message + Environment.NewLine + e.StackTrace);
                    }
                });
                simulatorThread.Start();
            }
        }
Ejemplo n.º 11
0
        private static string BuildAndRun(string filePath)
        {
            ILLanguage backend = new CecilBackendLanguage(filePath);

            using (var interpreter = new Interpreter <ILLanguage>(backend))
                using (var source = new StreamReader(filePath))
                {
                    if (!interpreter.Parse(source, filePath))
                    {
                        throw new AssertionException("Parsing of file " + filePath + " failed!");
                    }
                }

            backend.Save();

            return(ProgramExecutor.Execute(Path.GetFileNameWithoutExtension(filePath) + ".exe"));
        }
Ejemplo n.º 12
0
        public void CannotStartSameExecutorTwice()
        {
            var commands = new Mock <IProgramCommand>[]
            {
                new Mock <IProgramCommand>(),
                new Mock <IProgramCommand>(),
                new Mock <IProgramCommand>()
            };

            programMock.SetupGet(x => x.Commands).Returns(commands.Select(x => x.Object));

            var exec = new ProgramExecutor(programMock.Object, robotMock.Object);

            exec.Start();

            Assert.Throws <RobotsException>(() => exec.Start());
        }
Ejemplo n.º 13
0
        public void ShouldTriggerProperEvents()
        {
            var commands = new Mock <IProgramCommand>[]
            {
                new Mock <IProgramCommand>(),
                new Mock <IProgramCommand>(),
                new Mock <IProgramCommand>()
            };

            int j = 0;

            for (int i = 0; i < commands.Length; ++i)
            {
                commands[i].Setup(x => x.Execute(robotMock.Object)).Callback(() => ++ j);
            }

            programMock.SetupGet(x => x.Commands).Returns(commands.Select(x => x.Object));

            var exec = new ProgramExecutor(programMock.Object, robotMock.Object);

            exec.CommandExecutionStart += (_, e) => Assert.Equal(commands[j].Object, e.Command);
            exec.CommandExecutionEnd   += (_, e) => Assert.Equal(commands[j - 1].Object, e.Command);
            exec.ProgramExecutionEnd   += (_, e) => Assert.Equal(commands.Length, j);
        }
Ejemplo n.º 14
0
        private static void Main(string[] args)
        {
            ulong?         seed                   = null;
            int?           numPrograms            = null;
            int?           parallelism            = null;
            FuzzlynOptions options                = null;
            bool           help                   = false;
            bool           dumpOptions            = false;
            bool?          output                 = null;
            bool?          executePrograms        = null;
            bool?          enableChecksumming     = null;
            bool?          reduce                 = null;
            bool?          reduceInChildProcesses = null;
            string         reduceDebugGitDir      = null;
            string         removeFixed            = null;
            bool?          stats                  = null;
            OptionSet      optionSet              = new OptionSet
            {
                { "seed=|s=", "Seed to use when generating a single program", (ulong v) => seed = v },
                { "parallelism=", "Number of cores to use", (int?p) => parallelism = p },
                { "num-programs=|n=", "Number of programs to generate", (int v) => numPrograms = v },
                {
                    "options=",
                    "Path to options.json. Command-line options will override options from this file.",
                    s => options = JsonConvert.DeserializeObject <FuzzlynOptions>(File.ReadAllText(s))
                },
                { "dump-options", "Dump options to stdout and do nothing else", v => dumpOptions = v != null },
                { "output-source", "Output program source instead of feeding them directly to Roslyn and execution", v => output = v != null },
                { "execute-programs", "Accept programs to execute on stdin and report back differences", v => executePrograms = v != null },
                { "checksum", "Enable or disable checksumming in the generated code", v => enableChecksumming = v != null },
                { "reduce", "Reduce program to a minimal example", v => reduce = v != null },
                { "reduce-use-child-processes", "Check reduced example candidates in child processes", v => reduceInChildProcesses = v != null },
                { "reduce-debug-git-dir=", "Create reduce path in specified dir (must not exists beforehand)", v => reduceDebugGitDir = v },
                { "remove-fixed=", "Remove fixed programs in directory", v => removeFixed = v },
                { "stats", "Generate a bunch of programs and record their sizes", v => stats = v != null },
                { "help|h", v => help = v != null }
            };

            string error = null;

            try
            {
                List <string> leftover = optionSet.Parse(args);
                if (leftover.Any())
                {
                    error = "Unknown arguments: " + string.Join(" ", leftover);
                }
            }
            catch (OptionException ex)
            {
                error = ex.Message;
            }

            if (error != null)
            {
                Console.WriteLine("Fuzzlyn: {0}", error);
                Console.WriteLine("Use --help for help.");
                return;
            }

            if (help)
            {
                Console.WriteLine("Usage: fuzzlyn.exe");
                optionSet.WriteOptionDescriptions(Console.Out);
                return;
            }

            if (executePrograms.HasValue && executePrograms.Value)
            {
                ProgramExecutor.Run();
                return;
            }

            if (options == null)
            {
                options = new FuzzlynOptions();
            }

            if (seed.HasValue)
            {
                options.Seed = seed.Value;
            }
            if (numPrograms.HasValue)
            {
                options.NumPrograms = numPrograms.Value;
            }
            if (parallelism.HasValue)
            {
                options.Parallelism = parallelism.Value;
            }
            if (output.HasValue)
            {
                options.Output = output.Value;
            }
            if (enableChecksumming.HasValue)
            {
                options.EnableChecksumming = enableChecksumming.Value;
            }
            if (reduce.HasValue)
            {
                options.Reduce = reduce.Value;
            }
            if (reduceInChildProcesses.HasValue)
            {
                options.ReduceWithChildProcesses = reduceInChildProcesses.Value;
            }
            if (stats.HasValue)
            {
                options.Stats = stats.Value;
            }

            if (options.NumPrograms != 1 && options.Seed.HasValue)
            {
                Console.WriteLine("Error: Must specify exactly 1 program if a seed is specified.");
                return;
            }

            if (options.Reduce && !options.Seed.HasValue)
            {
                Console.WriteLine("Error: Cannot reduce without a seed.");
                return;
            }

            if (dumpOptions)
            {
                Console.Write(JsonConvert.SerializeObject(options, Formatting.Indented));
                return;
            }

            string val = Environment.GetEnvironmentVariable("COMPlus_TieredCompilation");

            if (val != "0")
            {
                Console.WriteLine(
                    "Please set the COMPlus_TieredCompilation environment variable " +
                    "to \"0\" before starting Fuzzlyn.");
                Console.WriteLine("For cmd use \"set COMPlus_TieredCompilation=0\".");
                Console.WriteLine("For powershell use \"$env:COMPlus_TieredCompilation='0'\"");
                Console.WriteLine("For bash use \"export COMPlus_TieredCompilation=0\"");
                Console.WriteLine("For Visual Studio, check the debug tab");
            }
            else if (removeFixed != null)
            {
                RemoveFixedPrograms(options, removeFixed);
            }
            else if (options.Reduce)
            {
                ReduceProgram(options, reduceDebugGitDir);
            }
            else if (options.Stats)
            {
                GenerateProgramsAndGetStats(options);
            }
            else if (options.Output)
            {
                GenerateProgramsAndOutput(options);
            }
            else
            {
                GenerateProgramsAndCheck(options);
            }

            if (Debugger.IsAttached)
            {
                Console.ReadLine();
            }
        }
Ejemplo n.º 15
0
        private static void RemoveFixedPrograms(FuzzlynOptions options, string dir)
        {
            const string rereduceFile = "Rereduce_required.txt";

            string[] files = Directory.GetFiles(dir, "*.cs").OrderBy(p => p.ToLowerInvariant()).ToArray();
            for (int i = 0; i < files.Length; i++)
            {
                Console.Title = $"Processing {i + 1}/{files.Length}";

                string          contents = File.ReadAllText(files[i]);
                MatchCollection matches  = Regex.Matches(contents, "// Seed: ([0-9]+)");
                if (matches.Count != 1)
                {
                    continue;
                }

                ulong seed = ulong.Parse(matches[0].Groups[1].Value);
                Console.Write("Processing {0}: ", seed);

                options.Seed = seed;
                var cg = new CodeGenerator(options);
                CompilationUnitSyntax original = cg.GenerateProgram();

                CompileResult debug   = Compiler.Compile(original, Compiler.DebugOptions);
                CompileResult release = Compiler.Compile(original, Compiler.ReleaseOptions);

                if (debug.CompileErrors.Length > 0 || release.CompileErrors.Length > 0)
                {
                    Console.WriteLine("Compiler error");
                    continue;
                }

                if (debug.RoslynException != null || release.RoslynException != null)
                {
                    Console.WriteLine("Compiler exception");
                    continue;
                }

                ProgramPairResults execResults = ProgramExecutor.RunSeparately(
                    new List <ProgramPair> {
                    new ProgramPair(false, debug.Assembly, release.Assembly)
                })
                                                 ?.Single();

                if (execResults == null)
                {
                    Console.WriteLine("Crashed sub-process, still interesting");
                    continue;
                }

                if (execResults.DebugResult.Checksum != execResults.ReleaseResult.Checksum ||
                    execResults.DebugResult.ExceptionType != execResults.ReleaseResult.ExceptionType)
                {
                    // Execute the reduced form to see if we get interesting behavior.
                    // Otherwise we may need to rereduce it.
                    // HOAX: Currently IsReducedVersionInteresting runs the programs
                    // in our own process, so we are conservative and do not run programs
                    // that may crash us (it is possible that the unreduced example does not
                    // crash, but that the reduced does.
                    if (contents.Contains("Crashes the runtime") || IsReducedVersionInteresting(execResults, contents))
                    {
                        Console.WriteLine("Still interesting");
                    }
                    else
                    {
                        File.AppendAllText(rereduceFile, seed + Environment.NewLine);
                        Console.WriteLine("Marked for rereduction");
                    }

                    continue;
                }

                Console.WriteLine("Deleted, no longer interesting");
                File.Delete(files[i]);
            }
        }
Ejemplo n.º 16
0
        public async void Update(string xml)
        {
            try
            {
                //xml = xml.Replace("&lt", "<");
                //await JSExecutor.InvokeAsync<string>("alert", xml);
                //throw new Exception(xml);
                (CDFG cdfg, List <ParseException> exceptions) = XmlParser.Parse(xml);
                if (exceptions.Count == 0)
                {
                    bool optimizedCDFG = false;
                    if (Settings.CreateGraph)
                    {
                        if (ProgramExecutor <string> .CanOptimizeCDFG(cdfg) && Settings.EnableOptimizations)
                        {
                            int boardWidth  = Settings.BoardWidth;
                            int boardHeight = Settings.BoardHeight;


                            cancelSource?.Cancel();

                            cancelSource = new CancellationTokenSource();
                            CDFG newCdfg = new CDFG();
                            newCdfg.StartDFG = ProgramExecutor <string> .OptimizeCDFG <string>(boardWidth, boardHeight, cdfg, cancelSource.Token, Settings.EnableGC);

                            newCdfg.AddNode(null, newCdfg.StartDFG);

                            if (cancelSource.IsCancellationRequested)
                            {
                                return;
                            }

                            cdfg          = newCdfg;
                            optimizedCDFG = true;
                        }
                        (string nodes, string edges) = SimpleGraph.CDFGToSimpleGraph(cdfg);
                        await JSExecutor.InvokeAsync <string>("setGraph", nodes, edges);
                    }
                    await JSExecutor.InvokeAsync <string>("ClearErrors");

                    RunSimulator(cdfg, optimizedCDFG);
                }
                else
                {
                    var errorInfos = exceptions.GroupBy(e => e.ID)
                                     .Select(e => $"{{id: \"{e.Key}\", message: \"{String.Join(@"\n", e.Select(ee => ee.Message))}\"}}");
                    await JSExecutor.InvokeAsync <string>("ShowBlocklyErrors", errorInfos.ToArray());
                }
            }
            catch (ParseException e)
            {
                await JSExecutor.InvokeAsync <string>("ShowUnexpectedError", e.Message);

                Debug.WriteLine(e.Message + Environment.NewLine + e.StackTrace);
            }
            catch (Exception e)
            {
                await JSExecutor.InvokeAsync <string>("ShowUnexpectedError", e.Message);

                Debug.WriteLine(e.Message + Environment.NewLine + e.StackTrace);
            }
        }
Ejemplo n.º 17
0
        private static void Main(string[] args)
        {
            /*
             * if (args.Length == 0)
             * {
             *  Console.WriteLine("No input file specified");
             *  return;
             * }
             *
             * var sourcePath = args[0];
             */
            var codeEmitter = new HappyPenguinCodeEmitter();

            /*
             * codeEmitter.Emit(OpCode.PushA);
             * codeEmitter.Emit(OpCode.Load, 0xFF, 0xFF);
             * codeEmitter.Emit(OpCode.Load, 0xFFFF);
             * codeEmitter.Emit(OpCode.Load, 0xAF, 0xAF);
             * codeEmitter.Emit(OpCode.Load, 0xAFAF);
             * codeEmitter.Emit(OpCode.Return);
             */

            //The # represents the programCounter number of the below program
            codeEmitter.Emit(OpCode.Nop);                                                //0.
            codeEmitter.Emit(OpCode.MovReg, (byte)RegisterId.A, 0x44);                   //1. Set A to 0x44
            codeEmitter.Emit(OpCode.DupA, 0xFFAA);                                       //2. Copy the data in A to 0xFFAA in memory
            codeEmitter.Emit(OpCode.LoadB, 0xFFAA);                                      //3. Copy the data at 0xFFAA from A to B
            codeEmitter.Emit(OpCode.PushA);                                              //4. Push A's value onto the stack
            codeEmitter.Emit(OpCode.MovReg, (byte)RegisterId.A, 0x33);                   //5. Set A to 0x33
            codeEmitter.Emit(OpCode.XchangeAB);                                          //6. Exchange the values of A and B
            codeEmitter.Emit(OpCode.PopA);                                               //7. Pop the stored value of A from the stack back into A
            codeEmitter.Emit(OpCode.MovReg, (byte)RegisterId.A, 0x22);                   //8. Set A to 0x22
            codeEmitter.Emit(OpCode.CompareReg, (byte)RegisterId.A, (byte)RegisterId.B); //9. Compare A and B
            codeEmitter.Emit(OpCode.JumpZ, 0xE);                                         //10. Jump to PC=0x if the registers in the previous comparison were equal
            codeEmitter.Emit(OpCode.DupA, 0xFFAA);                                       //11. Copy the data in A to 0xFFAA in memory
            codeEmitter.Emit(OpCode.LoadB, 0xFFAA);                                      //12. Copy the data at 0xFFAA from A to B
            codeEmitter.Emit(OpCode.Jump, 0x9);                                          //13. Jump back to line 0x9 to do comparison again
            codeEmitter.Emit(OpCode.Nop);                                                //14. Jump target from 10.
            codeEmitter.Emit(OpCode.Jump, 0x14);                                         //15. Jump to after function
            codeEmitter.Emit(OpCode.PopA);                                               //16. Retrieve A parameter
            codeEmitter.Emit(OpCode.Nop);                                                //17. Temporary placeholder
            codeEmitter.Emit(OpCode.Nop);                                                //18. Temporary placeholder
            codeEmitter.Emit(OpCode.Return);                                             //19. Return from function
            codeEmitter.Emit(OpCode.Nop);                                                //20. Jump target from 14.
            codeEmitter.Emit(OpCode.MovReg, (byte)RegisterId.A, 0x4);                    //21. Set A to 0x4
            codeEmitter.Emit(OpCode.ShiftLeft, (byte)RegisterId.A, 0x2);                 //22. Do a simple multiply by 4 with shifting
            codeEmitter.Emit(OpCode.PushA);                                              //23. Store A
            codeEmitter.Emit(OpCode.PopB);                                               //24. Restore B from stack
            codeEmitter.Emit(OpCode.ShiftRight, (byte)RegisterId.A, 0x2);                //25. Undo the multiply
            codeEmitter.Emit(OpCode.CompareReg, (byte)RegisterId.A, (byte)RegisterId.B); //26. Compare A and B
            codeEmitter.Emit(OpCode.JumpZ, 0x1D);                                        //27. Jump to PC=1d if the registers in the previous comparison were equal
            codeEmitter.Emit(OpCode.Fault, 0x1);                                         //28. Fault with code 0x1
            codeEmitter.Emit(OpCode.Nop);                                                //29. Jump target
            codeEmitter.Emit(OpCode.MovReg, (byte)RegisterId.A, 0x11);                   //30. Load 0x11 into A as the call target
            codeEmitter.Emit(OpCode.PushA);                                              //31. Push the A register onto the stack as the call target
            codeEmitter.Emit(OpCode.Call);                                               //32. Call the function from the address on the stack
            codeEmitter.Emit(OpCode.Halt);                                               //End of program. Halt.

            var vmProgram   = codeEmitter.GetEmittedCode();
            var codeEncoder = new CodeEncoder();

            using (var outputFileStream = File.Open("testprogram.🐧", FileMode.Create))
            {
                var outStream = new MemoryStream();
                codeEncoder.EncodeCodeToStream(vmProgram, outStream);
                outStream.Position = 0;
                var loadedProgram = codeEncoder.ReadCodeFromStream(outStream);
                outStream.Position = 0;
                outStream.CopyTo(outputFileStream);
            }

            var executor = new ProgramExecutor(vmProgram);

            executor.InitializeMachine();
            executor.ExecuteCode();
            //Dump memory

            /*
             * for (int i = 0; i < executor.Memory.Length; i++)
             * {
             *  Console.Write($"{executor.Memory[i]} ");
             * }
             */
        }
 public void ExecuteProgram_CorrectlyParsesProgram(int[] program, int[] expectedOutput)
 {
     programExecutor = new ProgramExecutor(mockWriter.Object);
     Assert.Equal(expectedOutput, programExecutor.ExecuteProgram(program));
 }
Ejemplo n.º 19
0
        public CompilationUnitSyntax Reduce()
        {
            CompileResult debug   = Compiler.Compile(Original, Compiler.DebugOptions);
            CompileResult release = Compiler.Compile(Original, Compiler.ReleaseOptions);

            Func <CompilationUnitSyntax, bool> isInteresting;

            if (debug.RoslynException != null || release.RoslynException != null)
            {
                CSharpCompilationOptions opts = debug.RoslynException != null ? Compiler.DebugOptions : Compiler.ReleaseOptions;
                isInteresting = program => Compiler.Compile(program, opts).RoslynException != null;
            }
            else if (debug.CompileErrors.Length > 0 || release.CompileErrors.Length > 0)
            {
                CSharpCompilationOptions opts = debug.CompileErrors.Length > 0 ? Compiler.DebugOptions : Compiler.ReleaseOptions;
                isInteresting = program =>
                {
                    CompileResult recompiled = Compiler.Compile(program, opts);
                    if (recompiled.CompileErrors.Length <= 0)
                    {
                        return(false);
                    }

                    return(recompiled.CompileErrors[0].Id == (debug.CompileErrors.Length > 0 ? debug.CompileErrors[0] : release.CompileErrors[0]).Id);
                };
            }
            else
            {
                var origPair = new ProgramPair(debug.Assembly, release.Assembly);
                ProgramPairResults origResults = ProgramExecutor.RunPair(origPair);
                if (origResults.DebugResult.Checksum == origResults.ReleaseResult.Checksum &&
                    origResults.DebugResult.ExceptionType == origResults.ReleaseResult.ExceptionType)
                {
                    throw new InvalidOperationException("Program has no errors");
                }

                isInteresting = prog =>
                {
                    ProgramPairResults results = CompileAndRun(prog);
                    if (results == null)
                    {
                        return(false);
                    }

                    // Do exceptions first because they will almost always change checksum
                    if (origResults.DebugResult.ExceptionType != origResults.ReleaseResult.ExceptionType)
                    {
                        // Must throw same exceptions in debug and release to be bad.
                        return(results.DebugResult.ExceptionType == origResults.DebugResult.ExceptionType &&
                               results.ReleaseResult.ExceptionType == origResults.ReleaseResult.ExceptionType);
                    }
                    else
                    {
                        if (results.DebugResult.ExceptionType != origResults.DebugResult.ExceptionType ||
                            results.ReleaseResult.ExceptionType != origResults.ReleaseResult.ExceptionType)
                        {
                            return(false);
                        }
                    }

                    return(results.DebugResult.Checksum != results.ReleaseResult.Checksum);
                };
            }

            // Save original comments as simplification may remove it by removing an unnecessary type.
            SyntaxTriviaList originalTrivia = Original.GetLeadingTrivia();

            Reduced = Original.WithLeadingTrivia();

            Reduced = CoarseSimplify(Reduced, isInteresting);
            List <SyntaxNode> simplifiedNodes = new List <SyntaxNode>();
            bool first = true;
            bool any   = true;

            while (any)
            {
                any = false;
                while (true)
                {
                    if (!SimplifyOne("Statements", Reduced.DescendantNodes().Where(n => n is StatementSyntax).ToList()))
                    {
                        break;
                    }
                    any = true;
                }

                while (true)
                {
                    if (!SimplifyOne("Expressions", Reduced.DescendantNodes().Where(n => n is ExpressionSyntax).ToList()))
                    {
                        break;
                    }
                    any = true;
                }

                while (true)
                {
                    List <SyntaxNode> members =
                        Reduced.DescendantNodesAndSelf().Where(n => n is MemberDeclarationSyntax || n is CompilationUnitSyntax).ToList();

                    if (!SimplifyOne("Members", members))
                    {
                        break;
                    }
                    any = true;
                }

                first = false;

                bool SimplifyOne(string name, List <SyntaxNode> list)
                {
                    for (int i = 0; i < 2000; i++)
                    {
                        Console.Title = $"Simplifying {name}. Iter: {i}";

                        SyntaxNode node = list[_rng.Next(list.Count)];
                        // Do not optimize checksum args and call itself.
                        // We still want to remove these statements, however, so we focus on the expression only.
                        InvocationExpressionSyntax invocParent = node.FirstAncestorOrSelf <InvocationExpressionSyntax>();
                        if (invocParent != null && IsChecksumCall(invocParent))
                        {
                            continue;
                        }

                        // If we fail at creating a new bad example, then we want to be able to restore the state
                        // so the reducer will not blow these up unnecessarily.
                        int origVarCounter = _varCounter;

                        simplifiedNodes.Clear();
                        SimplifyNode(node, !first, simplifiedNodes);

                        foreach (SyntaxNode candidateNode in simplifiedNodes)
                        {
                            CompilationUnitSyntax candidate = Reduced.ReplaceNode(node, candidateNode);
                            if (isInteresting(candidate))
                            {
                                Reduced = candidate;
                                return(true);
                            }
                        }

                        _varCounter = origVarCounter;
                    }

                    return(false);
                }
            }

            List <SyntaxTrivia> outputComments = GetOutputComments(debug, release).Select(Comment).ToList();

            SimplifyRuntime();
            double           oldSizeKiB = Original.NormalizeWhitespace().ToString().Length / 1024.0;
            double           newSizeKiB = Reduced.NormalizeWhitespace().ToString().Length / 1024.0;
            SyntaxTriviaList newTrivia  =
                originalTrivia.Add(Comment(FormattableString.Invariant($"// Reduced from {oldSizeKiB:F1} KiB to {newSizeKiB:F1} KiB")))
                .AddRange(outputComments);

            Reduced = Reduced.WithLeadingTrivia(newTrivia);

            return(Reduced);
        }
 public void ExecuteProgram_OutputsCorrectDiagnostics(int[] program, string expectedOutput)
 {
     programExecutor = new ProgramExecutor(mockWriter.Object);
     programExecutor.ExecuteProgram(program);
     mockWriter.Verify(mw => mw.Write(It.Is <string>((t => t.Equals(expectedOutput)))));
 }
 public void ExecuteProgram_EqualsOpCodeWorksAsExpected(int[] program, int[] expectedOutput)
 {
     programExecutor = new ProgramExecutor(mockWriter.Object);
     Assert.Equal(expectedOutput, programExecutor.ExecuteProgram(program));
 }
Ejemplo n.º 22
0
        public void Update(string xml)
        {
            try
            {
                (CDFG cdfg, List<ParseException> exceptions) = XmlParser.Parse(xml);
                if (exceptions.Count == 0)
                {
                    string js = String.Empty;
                    bool optimizedCDFG = false;
                    if (Settings.CreateGraph)
                    {
                        if (ProgramExecutor<string>.CanOptimizeCDFG(cdfg) && Settings.EnableOptimizations)
                        {
                            int boardWidth = Settings.BoardWidth;
                            int boardHeight = Settings.BoardHeight;

                            
                            cancelSource?.Cancel();

                            cancelSource = new CancellationTokenSource();
                            CDFG newCdfg = new CDFG();
                            newCdfg.StartDFG = ProgramExecutor<string>.OptimizeCDFG<string>(boardWidth, boardHeight, cdfg, cancelSource.Token, Settings.EnableGC);
                            newCdfg.AddNode(null, newCdfg.StartDFG);

                            if (cancelSource.IsCancellationRequested)
                            {
                                return;
                            }

                            cdfg = newCdfg;
                            optimizedCDFG = true;
                        }
                        (string nodes, string edges) = SimpleGraph.CDFGToSimpleGraph(cdfg);
                        js = $"setGraph({nodes}, {edges});";
                    }
                    js += $"ClearErrors();";
                    Browser.ExecuteScriptAsync(js);

                    RunSimulator(cdfg, optimizedCDFG);
                }
                else
                {
                    var errorInfos = exceptions.GroupBy(e => e.ID)
                                               .Select(e => $"{{id: \"{e.Key}\", message: \"{String.Join(@"\n", e.Select(ee => ee.Message))}\"}}");
                    string ids = string.Join(", ", errorInfos);
                    string js = $"ShowBlocklyErrors([{ids}]);";
                    Browser.ExecuteScriptAsync(js);
                }
            }
            catch (ParseException e)
            {
                string message = $"ShowUnexpectedError(\"{e.Message.Replace('\"', ' ').Replace('\'', ' ')}\");";
                Browser.ExecuteScriptAsync(message);
                Debug.WriteLine(e.Message + Environment.NewLine + e.StackTrace);
            }
            catch (Exception e)
            {
                string message = $"ShowUnexpectedError(\"{e.Message.Replace('\"', ' ').Replace('\'', ' ')}\");";
                Browser.ExecuteScriptAsync(message);
                Debug.WriteLine(e.Message + Environment.NewLine + e.StackTrace);
            }
        }
Ejemplo n.º 23
0
        private static void Main(string[] args)
        {
            ulong?         seed               = null;
            int?           numPrograms        = null;
            int?           parallelism        = null;
            FuzzlynOptions options            = null;
            bool           help               = false;
            bool           dumpOptions        = false;
            bool?          output             = null;
            bool?          executePrograms    = null;
            bool?          enableChecksumming = null;
            bool?          reduce             = null;
            string         removeFixed        = null;
            OptionSet      optionSet          = new OptionSet
            {
                { "seed=|s=", "Seed to use when generating a single program", (ulong v) => seed = v },
                { "parallelism=", "Number of cores to use", (int?p) => parallelism = p },
                { "num-programs=|n=", "Number of programs to generate", (int v) => numPrograms = v },
                {
                    "options=",
                    "Path to options.json. Command-line options will override options from this file.",
                    s => options = JsonConvert.DeserializeObject <FuzzlynOptions>(File.ReadAllText(s))
                },
                { "dump-options", "Dump options to stdout and do nothing else", v => dumpOptions = v != null },
                { "output-source", "Output program source instead of feeding them directly to Roslyn and execution", v => output = v != null },
                { "execute-programs", "Accept programs to execute on stdin and report back differences", v => executePrograms = v != null },
                { "checksum", "Enable or disable checksumming in the generated code", v => enableChecksumming = v != null },
                { "reduce", "Reduce program to a minimal example", v => reduce = v != null },
                { "remove-fixed=", "Remove fixed programs in directory", v => removeFixed = v },
                { "help|h", v => help = v != null }
            };


            string error = null;

            try
            {
                List <string> leftover = optionSet.Parse(args);
                if (leftover.Any())
                {
                    error = "Unknown arguments: " + string.Join(" ", leftover);
                }
            }
            catch (OptionException ex)
            {
                error = ex.Message;
            }

            if (error != null)
            {
                Console.WriteLine("Fuzzlyn: {0}", error);
                Console.WriteLine("Use --help for help.");
                return;
            }

            if (help)
            {
                Console.WriteLine("Usage: fuzzlyn.exe");
                optionSet.WriteOptionDescriptions(Console.Out);
                return;
            }

            if (executePrograms.HasValue && executePrograms.Value)
            {
                ProgramExecutor.Run();
                return;
            }

            if (options == null)
            {
                options = new FuzzlynOptions();
            }

            if (seed.HasValue)
            {
                options.Seed = seed.Value;
            }
            if (numPrograms.HasValue)
            {
                options.NumPrograms = numPrograms.Value;
            }
            if (parallelism.HasValue)
            {
                options.Parallelism = parallelism.Value;
            }
            if (output.HasValue)
            {
                options.Output = output.Value;
            }
            if (enableChecksumming.HasValue)
            {
                options.EnableChecksumming = enableChecksumming.Value;
            }
            if (reduce.HasValue)
            {
                options.Reduce = reduce.Value;
            }

            if (options.NumPrograms != 1 && options.Seed.HasValue)
            {
                Console.WriteLine("Error: Must specify exactly 1 program if a seed is specified.");
                return;
            }

            if (options.NumPrograms != 1 && options.Output)
            {
                Console.WriteLine("Error: Must specify exactly 1 program if output is desired.");
                return;
            }

            if (options.Reduce && !options.Seed.HasValue)
            {
                Console.WriteLine("Error: Cannot reduce without a seed.");
                return;
            }

            if (dumpOptions)
            {
                Console.Write(JsonConvert.SerializeObject(options, Formatting.Indented));
                return;
            }

            if (removeFixed != null)
            {
                RemoveFixedPrograms(options, removeFixed);
                return;
            }

            if (options.Reduce)
            {
                ReduceProgram(options);
            }
            else
            {
                GenerateProgram(options);
            }
        }
Ejemplo n.º 24
0
        static void Main(string[] args)
        {
            if (!Directory.Exists("unoptimizedPrograms"))
            {
                Directory.CreateDirectory("unoptimizedPrograms");
            }

            List <perf_data> unoptimizedDatas   = new List <perf_data>();
            List <perf_data> optimizedDatas     = new List <perf_data>();
            List <perf_data> optimizedNoGCDatas = new List <perf_data>();
            int       nameID = 0;
            Random    random = new Random(15231);
            TestTools tools  = new TestTools();

            for (int i = 0; i < 2000; i++)
            {
                try
                {
                    perf_data unoptimizedData   = new perf_data();
                    perf_data optimizedData     = new perf_data();
                    perf_data optimizedNoGCData = new perf_data();
                    JSProgram program           = new JSProgram();
                    program.Render = false;

                    tools.ClearWorkspace();
                    program.CreateCDFG(3, 15, random);
                    tools.ExecuteJS(program);

                    string xml    = tools.GetWorkspaceString();
                    var    result = XmlParser.Parse(xml);
                    if (result.Item2.Count > 0)
                    {
                        i--;
                        continue;
                    }

                    int     testCount = 5;
                    float[] untimes   = new float[testCount];
                    float[] optimes   = new float[testCount];

                    Stopwatch watch = new Stopwatch();
                    for (int z = 0; z < testCount; z++)
                    {
                        watch.Reset();
                        watch.Start();
                        TestCommandExecutor      commandExecutor = new TestCommandExecutor();
                        ProgramExecutor <string> programExecutor = new ProgramExecutor <string>(commandExecutor);
                        programExecutor.TimeBetweenCommands     = 0;
                        programExecutor.EnableOptimizations     = false;
                        programExecutor.EnableGarbageCollection = false;
                        programExecutor.Run(100, 100, result.Item1, false);
                        unoptimizedData.makespan = commandExecutor.ticks;
                        watch.Stop();
                        untimes[z] = watch.ElapsedMilliseconds / (float)testCount;
                    }
                    {
                        int minSize = 10;
                        while (true)
                        {
                            try
                            {
                                result.Item1.Nodes.ForEach(x => x.dfg.Nodes.ForEach(qq => qq.value.Reset()));
                                TestCommandExecutor      commandExecutor = new TestCommandExecutor();
                                ProgramExecutor <string> programExecutor = new ProgramExecutor <string>(commandExecutor);
                                programExecutor.TimeBetweenCommands     = 0;
                                programExecutor.EnableOptimizations     = false;
                                programExecutor.EnableGarbageCollection = false;
                                programExecutor.Run(minSize, minSize, result.Item1, false);
                                break;
                            }
                            catch (Exception e)
                            {
                                if (minSize > 100)
                                {
                                    Console.Write(e.Message + Environment.NewLine + e.StackTrace);
                                }
                                minSize++;
                            }
                        }
                        unoptimizedData.size = minSize;
                    }
                    for (int z = 0; z < testCount; z++)
                    {
                        watch.Reset();
                        watch.Start();
                        TestCommandExecutor      commandExecutor = new TestCommandExecutor();
                        ProgramExecutor <string> programExecutor = new ProgramExecutor <string>(commandExecutor);
                        programExecutor.TimeBetweenCommands     = 0;
                        programExecutor.EnableOptimizations     = true;
                        programExecutor.EnableGarbageCollection = true;
                        programExecutor.Run(100, 100, result.Item1, false);
                        optimizedData.makespan = commandExecutor.ticks;
                        watch.Stop();
                        optimes[z] = watch.ElapsedMilliseconds / (float)testCount;
                    }
                    {
                        int minSize = 10;
                        while (true)
                        {
                            try
                            {
                                result.Item1.Nodes.ForEach(x => x.dfg.Nodes.ForEach(qq => qq.value.Reset()));
                                TestCommandExecutor      commandExecutor = new TestCommandExecutor();
                                ProgramExecutor <string> programExecutor = new ProgramExecutor <string>(commandExecutor);
                                programExecutor.TimeBetweenCommands     = 0;
                                programExecutor.EnableOptimizations     = true;
                                programExecutor.EnableGarbageCollection = true;
                                programExecutor.Run(minSize, minSize, result.Item1, false);
                                break;
                            }
                            catch (Exception)
                            {
                                minSize++;
                            }
                        }
                        optimizedData.size = minSize;
                    }

                    {
                        TestCommandExecutor      commandExecutor = new TestCommandExecutor();
                        ProgramExecutor <string> programExecutor = new ProgramExecutor <string>(commandExecutor);
                        programExecutor.TimeBetweenCommands     = 0;
                        programExecutor.EnableOptimizations     = true;
                        programExecutor.EnableGarbageCollection = false;
                        programExecutor.Run(100, 100, result.Item1, false);
                        optimizedNoGCData.makespan = commandExecutor.ticks;
                    }
                    {
                        int minSize = 10;
                        while (true)
                        {
                            try
                            {
                                result.Item1.Nodes.ForEach(x => x.dfg.Nodes.ForEach(qq => qq.value.Reset()));
                                TestCommandExecutor      commandExecutor = new TestCommandExecutor();
                                ProgramExecutor <string> programExecutor = new ProgramExecutor <string>(commandExecutor);
                                programExecutor.TimeBetweenCommands     = 0;
                                programExecutor.EnableOptimizations     = true;
                                programExecutor.EnableGarbageCollection = false;
                                programExecutor.Run(minSize, minSize, result.Item1, false);
                                break;
                            }
                            catch (Exception)
                            {
                                minSize++;
                            }
                        }
                        optimizedNoGCData.size = minSize;
                    }

                    unoptimizedData.time = untimes.Min();
                    optimizedData.time   = optimes.Min();

                    unoptimizedDatas.Add(unoptimizedData);
                    optimizedDatas.Add(optimizedData);
                    optimizedNoGCDatas.Add(optimizedNoGCData);

                    string path = Path.Combine("unoptimizedPrograms", $"program_{nameID++}.bc");
                    File.WriteAllText(path, xml);
                }
                catch (Exception e)
                {
                    //Console.Write(e.Message + Environment.NewLine + e.StackTrace);
                    i--;
                }

                Console.WriteLine(i);
            }
            tools.AssemblyCleanup();

            File.WriteAllText("unoptimized_data.txt", String.Join(Environment.NewLine, unoptimizedDatas.Select(x => x.makespan + " " + x.time.ToString(CultureInfo.InvariantCulture) + " " + x.size)));
            File.WriteAllText("optimized_data.txt", String.Join(Environment.NewLine, optimizedDatas.Select(x => x.makespan + " " + x.time.ToString(CultureInfo.InvariantCulture) + " " + x.size)));
            File.WriteAllText("optimized_no_gc_data.txt", String.Join(Environment.NewLine, optimizedNoGCDatas.Select(x => x.makespan + " " + x.size)));
        }