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));
        }
Example #2
0
        private void CheckAnswer(
            IReadOnlyList <CodeBlock> instructions,
            Dictionary <VirtualRegister, List <VirtualRegister> > expectedInterferenceGraph,
            Dictionary <VirtualRegister, List <VirtualRegister> > expectedCopyGraph)
        {
            var result = new LivenessAnalyzer().GetInterferenceCopyGraphs(instructions);

            MappingEquivalence.AssertAreEquivalentCollection(
                expectedInterferenceGraph.ToDictionary(t => t.Key, t => (IReadOnlyCollection <VirtualRegister>)t.Value),
                result.InterferenceGraph);

            MappingEquivalence.AssertAreEquivalentCollection(
                expectedCopyGraph.ToDictionary(t => t.Key, t => (IReadOnlyCollection <VirtualRegister>)t.Value),
                result.CopyGraph);
        }
Example #3
0
        public IFunctionToAsmGenerator Generate()
        {
            var livenessAnalyzer             = new LivenessAnalyzer();
            var registerAllocator            = new RegisterAllocator();
            var instructionsTemplatesFactory = new Templates.InstructionsTemplatesFactory();
            var instructionTemplates         = instructionsTemplatesFactory.CreateInstructionTemplates();
            var instructionSelector          = new InstructionSelector.InstructionSelector(instructionTemplates);
            var cfgLinearizer      = new CfgLinearizer.CfgLinearizer();
            var labelFactory       = new LabelFactory(new LabelIdGuidGenerator());
            var readWriteGenerator = new ReadWriteGenerator();

            return(new FunctionToAsmGenerator(
                       livenessAnalyzer,
                       registerAllocator,
                       instructionSelector,
                       cfgLinearizer,
                       labelFactory,
                       readWriteGenerator));
        }