Exemple #1
0
        public void Process(File file, TextWriter writer)
        {
            var decoder  = new Decoder(file);
            var program  = new ScriptProgram(decoder);
            var analyzer = new ControlFlowAnalyzer(program);

            analyzer.Analyze();

            // Dump the code paths
            foreach (Function function in program.Functions)
            {
                writer.WriteLine("function " + function.Name);
                foreach (CodePath path in function.CodePaths)
                {
                    writer.WriteLine("    " + path.Name + ":");
                    writer.WriteLine(string.Format("        0x{0:x} --> 0x{1:x}", path.StartOffset, path.EndOffset));
                    if (path.ParentCodePath != null)
                    {
                        writer.WriteLine("        parent: {0}, exit: 0x{1:x}, reentry: 0x{2:x}",
                                         path.ParentCodePath.Name, path.ParentExitInstruction.Instruction.Offset,
                                         path.ParentEntryTargetInstruction.Instruction.Offset);
                    }
                }
                writer.WriteLine();
            }
        }
        public Package Analyze(
            PackageSyntax packageSyntax,
            FixedDictionary <string, Package> references)
        {
            // First pull over all the lexer and parser errors from the compilation units
            var diagnostics = AllDiagnostics(packageSyntax);

            var scopesBuilder = new LexicalScopesBuilder(diagnostics, packageSyntax, references);

            scopesBuilder.BuildScopesInPackage(packageSyntax);

            // Make a list of all the member declarations (i.e. not namespaces)
            var memberDeclarations = packageSyntax.CompilationUnits
                                     .SelectMany(cu => cu.AllMemberDeclarations).ToFixedList();

            // TODO we can't do full type checking without some IL gen and code execution, how to handle that?

            // Do type checking
            TypeResolver.Check(memberDeclarations, diagnostics);

#if DEBUG
            TypeResolutionValidator.Validate(memberDeclarations);
            // TODO validate that all ReferencedSymbols lists have a single value non-errored code
#endif
            MoveChecker.Check(memberDeclarations, diagnostics);

            ShadowChecker.Check(memberDeclarations, diagnostics);

            // TODO we need to check definite assignment as part of this
            BindingMutabilityChecker.Check(memberDeclarations, diagnostics);

            // --------------------------------------------------
            // This is where the representation transitions to IR
            ControlFlowAnalyzer.BuildGraphs(memberDeclarations);
            // --------------------------------------------------

            var liveness = LivenessAnalyzer.Analyze(memberDeclarations);

            DeleteInserter.Transform(memberDeclarations, liveness);

            BorrowChecker.Check(memberDeclarations, diagnostics);

            // Build final declaration objects and find the entry point
            var declarationBuilder = new DeclarationBuilder();
            var declarations       = declarationBuilder.Build(memberDeclarations);
            var entryPoint         = DetermineEntryPoint(declarations, diagnostics);

            return(new Package(packageSyntax.Name, diagnostics.Build(), references, declarations, entryPoint));
        }
Exemple #3
0
        public void Process(File file, TextWriter writer)
        {
            var decoder  = new Decoder(file);
            var program  = new ScriptProgram(decoder);
            var analyzer = new ControlFlowAnalyzer(program);

            analyzer.Analyze();

            foreach (Function function in program.Functions)
            {
                writer.WriteLine(string.Format(".function {0} (params={1}, vars={2}, return={3})",
                                               function.Name, function.ParameterCount, function.VariableCount, function.ReturnCount));

                ProcessCodePath(writer, function.MainCodePath, "   ");

                writer.WriteLine(".endfunction");
                writer.WriteLine();
            }
        }
Exemple #4
0
        public void Process(File file, TextWriter writer)
        {
            var decoder  = new Decoder(file);
            var program  = new ScriptProgram(decoder);
            var analyzer = new ControlFlowAnalyzer(program);

            analyzer.Analyze();

            var stackAnalyzer = new StackUseAnalyzer(program);

            stackAnalyzer.Analyze();

            foreach (Function function in program.Functions)
            {
                var sb = new StringBuilder();
                for (int i = 0; i < function.ParameterCount; i++)
                {
                    if (i != 0)
                    {
                        sb.Append(", ");
                    }
                    sb.Append("var");
                    sb.Append(i);
                }

                writer.WriteLine(string.Format("{0} {1}({2})", function.ReturnCount > 0 ? "function" : "void",
                                               function.Name, sb));
                writer.WriteLine("{");

                if (function.VariableCount > 2)
                {
                    writer.Write("   auto ");
                    for (int i = 2; i < function.VariableCount; i++)
                    {
                        if (i != 2)
                        {
                            writer.Write(", ");
                        }
                        writer.Write("var" + (i + function.ParameterCount));
                    }
                    writer.WriteLine(";");
                }

                if (function.TemporaryCount > 0)
                {
                    writer.Write("   auto ");
                    for (int i = 0; i < function.TemporaryCount; i++)
                    {
                        if (i != 0)
                        {
                            writer.Write(", ");
                        }
                        writer.Write("temp" + i);
                    }
                    writer.WriteLine(";");
                }

                if (function.TemporaryCount > 0 || function.VariableCount > 2)
                {
                    writer.WriteLine();
                }

                ProcessCodePath(writer, function.MainCodePath, "   ");

                writer.WriteLine("}");
                writer.WriteLine();
            }
        }