Ejemplo n.º 1
0
        private static void RegisterCustomNodeTypes(PluginInfo pluginInfo)
        {
            Contract.Requires(pluginInfo != null);

            var customNodeTypes = pluginInfo.Interface.GetCustomNodeTypes();

            if (customNodeTypes == null)
            {
                return;
            }

            if (customNodeTypes.NodeTypes == null || customNodeTypes.Serializer == null || customNodeTypes.CodeGenerator == null)
            {
                throw new ArgumentException();                 // TODO
            }

            foreach (var nodeType in customNodeTypes.NodeTypes)
            {
                if (!nodeType.IsSubclassOf(typeof(BaseNode)))
                {
                    throw new ArgumentException($"Type '{nodeType}' is not a valid node.");
                }
            }

            pluginInfo.CustomNodeTypes = customNodeTypes;

            NodeTypesBuilder.AddPluginNodeGroup(pluginInfo.Interface, customNodeTypes.NodeTypes);

            CustomNodeSerializer.Add(customNodeTypes.Serializer);
            CppCodeGenerator.Add(customNodeTypes.CodeGenerator);
        }
Ejemplo n.º 2
0
        public StringBuilder BuildFullSourceCode(CrRuntimeLibrary crRuntime)
        {
            ComputeEscapeAnalysis(MethodClosure.Values.ToList());

            return(CppCodeGenerator.GenerateSourceStringBuilder(EntryInterpreter, UsedTypes,
                                                                MethodClosure.Values.ToList(), _virtualMethodTable, crRuntime));
        }
Ejemplo n.º 3
0
        private static void GenerateCppCode(string metaFile, string targetDir)
        {
            Logger.Instance.Info("Generating C++ code...");
            var metaInfo = MetaInfo.FromXmlFile(metaFile);
            var cppGen   = new CppCodeGenerator(metaInfo);

            cppGen.Generate(targetDir);
            Logger.Instance.Info("Done into {0}", targetDir);
        }
Ejemplo n.º 4
0
        public StringBuilder BuildFullSourceCode()
        {
            var         entryInterpreter = ResolveMethod(EntryPoint);
            List <Type> usedTypes        = MappedTypes.Values.ToList();
            var         typeTable        = new TypeDescriptionTable(usedTypes, this);

            return(CppCodeGenerator.GenerateSourceStringBuilder(entryInterpreter, typeTable,
                                                                MethodImplementations.Values.ToList(), this));
        }
Ejemplo n.º 5
0
        public void ShouldGenerateCppCode()
        {
            var metaInfo  = MetaInfo.FromXmlFile(@"TestData\meta-info.xml");
            var actualDir = Path.Combine(TestContext.TestResultsDirectory, "ActualCppCode");

            var generator = new CppCodeGenerator(metaInfo);

            generator.Generate(actualDir);

            CompareDirectories(@"TestData\Out\CppCode", actualDir);
        }
Ejemplo n.º 6
0
        public ClosureEntities(CppCodeGenerator getCppCodeGenerator)
        {
            _getCppCodeGenerator = getCppCodeGenerator;

            MappedTypes           = new Dictionary <Type, Type>();
            MethodImplementations = new Dictionary <MethodBaseKey, MethodInterpreter>(new MethodBaseKeyComparer());
            AbstractMethods       = new HashSet <MethodInfo>();
            LookupMethods         = new Dictionary <MethodInterpreterKey, MethodInterpreter>();

            EntitiesBuilder.SetupSteps();

            SetupProgramOptimizationTable();
        }
Ejemplo n.º 7
0
        private static void DeregisterCustomNodeTypes(PluginInfo pluginInfo)
        {
            Contract.Requires(pluginInfo != null);

            if (pluginInfo.CustomNodeTypes == null)
            {
                return;
            }

            NodeTypesBuilder.RemovePluginNodeGroup(pluginInfo.Interface);

            CustomNodeSerializer.Remove(pluginInfo.CustomNodeTypes.Serializer);
            CppCodeGenerator.Remove(pluginInfo.CustomNodeTypes.CodeGenerator);
        }
Ejemplo n.º 8
0
        //private static void TestIgnoreCase()
        //{
        //    Microsoft.Automata.Utilities.IgnoreCaseRelationGenerator.Generate(
        //        "Microsoft.Automata.Generated",
        //        "IgnoreCaseRelation",
        //    @"C:\GitHub\AutomataDotNet\Automata\src\Automata\Internal\Generated");
        //}

        static void TestCppCodeGen(Regex[] regexes)
        {
            Automaton <BDD>[] automata  = new Automaton <BDD> [regexes.Length];
            Automaton <BDD>[] Cautomata = new Automaton <BDD> [regexes.Length];
            var solver = new CharSetSolver();

            #region convert the regexes to automata
            Console.Write("Converting {0} regexes to automata and minimizing the automata ...", regexes.Length);
            int t = System.Environment.TickCount;
            Func <Automaton <BDD>, bool> IsFull = (a => a.StateCount == 1 && a.IsFinalState(a.InitialState) && a.IsLoopState(a.InitialState) && a.GetMovesCountFrom(a.InitialState) == 1 && a.GetMoveFrom(a.InitialState).Label.Equals(solver.True));
            for (int i = 0; i < regexes.Length; i++)
            {
                try
                {
                    var aut = CppCodeGenerator.Regex2Automaton(solver, regexes[i]);
                    automata[i] = aut;
                    if (IsFull(automata[i]) || automata[i].IsEmpty)
                    {
                        Console.WriteLine("\nReplacing trivial regex \"{0}\" with \"^dummy$\"", i, regexes[i]);
                        regexes[i]  = new Regex("^dummy$");
                        automata[i] = CppCodeGenerator.Regex2Automaton(solver, regexes[i]);
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine("\nCoverting regex {0}: '{1}' failed, reason: {2}, replacing with \"^dummy$\"", i, regexes[i], e.Message);
                    regexes[i]  = new Regex("^dummy$");
                    automata[i] = CppCodeGenerator.Regex2Automaton(solver, regexes[i]);
                }
            }
            t = System.Environment.TickCount - t;
            Console.WriteLine(string.Format(" done ({0}ms)", t));
            #endregion

            #region complement the automata
            t = System.Environment.TickCount;
            Console.Write("Creating complements of autmata ...");
            for (int i = 0; i < regexes.Length; i++)
            {
                Cautomata[i] = automata[i].Complement().Minimize();
            }
            t = System.Environment.TickCount - t;
            Console.WriteLine(string.Format(" done ({0}ms)", t));
            #endregion

            #region generate positive test strings
            Console.Write(string.Format("Generating a positive test set for all automata ", NrOfStrings));
            t = System.Environment.TickCount;
            List <string[]> members  = new List <string[]>();
            List <string[]> Cmembers = new List <string[]>();
            for (int id = 0; id < automata.Length; id++)
            {
                Console.Write(".");
                var M    = automata[id].Intersect(solver.Convert("^[\0-\x7F]{0," + CodeGenTests.MaxStringLength + "}$", RegexOptions.Singleline)).Determinize();
                var tmp  = new string[NrOfStrings];
                int time = System.Environment.TickCount;
                for (int i = 0; i < NrOfStrings; i++)
                {
                    tmp[i] = solver.GenerateMemberUniformly(M);
                    //if (i % 10 == 0)
                    //    Console.Write(".");
                }
                time = System.Environment.TickCount - time;
                members.Add(tmp);
            }
            t = System.Environment.TickCount - t;
            Console.WriteLine(string.Format(" done ({0}ms)", t));
            #endregion

            #region generate negative test strings
            t = System.Environment.TickCount;
            Console.Write(string.Format("Generating a negative test set for all automata ", NrOfStrings));
            for (int id = 0; id < Cautomata.Length; id++)
            {
                Console.Write(".");
                //var M = Cautomata[id].Intersect(solver.Convert("^[^\uD800-\uDFFF]{0,100}$", RegexOptions.Singleline), solver).Determinize(solver);
                var M   = Cautomata[id].Intersect(solver.Convert("^[\0-\uFFFF]{0,100}$", RegexOptions.Singleline)).Determinize();
                var tmp = new string[NrOfStrings];
                for (int i = 0; i < NrOfStrings; i++)
                {
                    tmp[i] = solver.GenerateMemberUniformly(M);
                    //if (i % 10 == 0)
                    //    Console.Write(".");
                }
                Cmembers.Add(tmp);
            }
            t = System.Environment.TickCount - t;
            Console.WriteLine(string.Format(" done ({0}ms)", t));
            #endregion

            #region generate c++

            int t2 = System.Environment.TickCount;
            CppTest.Compile(automata, solver, true);
            t2 = System.Environment.TickCount - t2;
            Console.WriteLine(string.Format(" done ({0}ms)", t2));
            #endregion

            #region convert the test strings to UTF8
            List <byte[][]> membersUTF8  = new List <byte[][]>();
            List <byte[][]> CmembersUTF8 = new List <byte[][]>();
            for (int id = 0; id < automata.Length; id++)
            {
                var tmp = new byte[NrOfStrings][];
                for (int i = 0; i < NrOfStrings; i++)
                {
                    tmp[i] = Encoding.UTF8.GetBytes(members[id][i]);
                }
                membersUTF8.Add(tmp);
            }
            for (int id = 0; id < Cautomata.Length; id++)
            {
                var tmp = new byte[NrOfStrings][];
                for (int i = 0; i < NrOfStrings; i++)
                {
                    tmp[i] = Encoding.UTF8.GetBytes(Cmembers[id][i]);
                }
                CmembersUTF8.Add(tmp);
            }
            #endregion

            #region compute tot nr of bits
            double bits = 0;
            for (int id = 0; id < automata.Length; id++)
            {
                int nrBytes = 0;
                for (int i = 0; i < NrOfStrings; i++)
                {
                    nrBytes += membersUTF8[id][i].Length + CmembersUTF8[id][i].Length;
                }
                bits += (nrBytes * 8.0);
            }
            bits = bits * CodeGenTests.Repetitions; //repeated Reps times
            #endregion

            #region run c++ tests
            Console.Write("Running c++  tests ... ");
            double totsec_cpp = 0;
            for (int id = 0; id < automata.Length; id++)
            {
                double sec_cpp  = 0;
                int    accepted = CppTest.Test(true, id, membersUTF8[id], members[id], out sec_cpp);
                totsec_cpp += sec_cpp;
                int Caccepted = CppTest.Test(false, id, CmembersUTF8[id], Cmembers[id], out sec_cpp);
                totsec_cpp += sec_cpp;
            }
            double bps_cpp  = bits / totsec_cpp;
            double mbps_cpp = (bps_cpp / 1000000.0);
            int    Mbps_cpp = (int)Math.Round(mbps_cpp);
            Console.WriteLine(string.Format("{0}sec, throughput = {1}Mbps", totsec_cpp, Mbps_cpp));
            #endregion

            #region run .NET tests
            Console.Write("Running .NET tests ... ");
            double totsec_net = 0;
            for (int id = 0; id < automata.Length; id++)
            {
                DotNetTest.Compile(regexes[id]); //make sure each regex is precompiled
                double sec_net;
                int    accepted2 = DotNetTest.Test(true, members[id], out sec_net);
                totsec_net += sec_net;
                int Caccepted2 = DotNetTest.Test(false, Cmembers[id], out sec_net);
                totsec_net += sec_net;
            }
            double bps_net  = bits / totsec_net;
            double mbps_net = (bps_net / 1000000.0);
            int    Mbps_net = (int)Math.Round(mbps_net);
            Console.WriteLine(string.Format("{0}sec, throughput = {1}Mbps", totsec_net, Mbps_net));
            #endregion

            Console.WriteLine(string.Format("speedup (.NET-time/c++-time) = {0}X", ((int)Math.Round(totsec_net / totsec_cpp))));
        }
Ejemplo n.º 9
0
    public static int Main(string[] args)
    {
        string        optProtoFilePath = "";
        string        optLanguage      = "";
        string        optOutputDir     = "";
        List <string> optSearchPath    = new List <string>();
        string        optNewLineType   = "";

        // parse command line options
        {
            OptionSet options = new OptionSet();
            options.Add("f=", v => optProtoFilePath = v);
            options.Add("l=", v => optLanguage      = v);
            options.Add("o=", v => optOutputDir     = v);
            options.Add("I=", v => optSearchPath.Add(v));
            options.Add("n=", v => optNewLineType = v);

            try {
                options.Parse(args);
            } catch (OptionException e) {
                Console.Error.WriteLine(e.Message);
                return(1);
            }
        }


        // check command line options
        if (optProtoFilePath == "" ||
            optLanguage == "")
        {
            PrintUsage();
            return(1);
        }
        if (optOutputDir == "")
        {
            optOutputDir = ".";
        }

        if (File.Exists(optProtoFilePath) == false)
        {
            Console.Error.WriteLine(string.Format(
                                        "error: can not find protocol file `{0}`",
                                        optProtoFilePath));
            return(1);
        }
        if (Directory.Exists(optOutputDir) == false)
        {
            Console.Error.WriteLine(string.Format(
                                        "error: can not find output directory `{0}`",
                                        optOutputDir));
            return(1);
        }

        using (ProtocolParser parser = new ProtocolParser()) {
            if (parser.Parse(optProtoFilePath, optSearchPath) == false)
            {
                return(1);
            }

            BaseCodeGenerator generator = null;
            if (optLanguage == "cpp")
            {
                generator = new CppCodeGenerator();
            }
            else if (optLanguage == "php")
            {
                generator = new PhpCodeGenerator();
            }
            else if (optLanguage == "csharp")
            {
                generator = new CSharpCodeGenerator();
            }
            else
            {
                Console.Error.WriteLine(string.Format(
                                            "error: language `{0}` is not supported",
                                            optLanguage));
                return(1);
            }

            using (generator) {
                BaseCodeGenerator.NewLineType newLineType =
                    BaseCodeGenerator.NewLineType.Unix;
                if (optNewLineType == "dos")
                {
                    newLineType = BaseCodeGenerator.NewLineType.Dos;
                }

                if (generator.Generate(parser.Descriptor,
                                       optOutputDir, newLineType) == false)
                {
                    return(1);
                }
            }
        }

        return(0);
    }