Exemplo n.º 1
0
        public Compiler(Log log, Backend backend, SourcePackage package, CompilerOptions options)
            : base(log)
        {
            // This is a block of dependency injection to initialize the Compiler
            var il         = new Namespace();
            var extensions = new ExtensionRoot();

            Backend = backend;
            var disk = Disk = new Disk(log, true);

            Shell = new Shell(log);
            var essentials   = Essentials = new Essentials();
            var resolver     = NameResolver = new NameResolver(this);
            var ilf          = ILFactory = new ILFactory(backend, il, essentials, resolver, this);
            var data         = Data = new BuildData(il, extensions, ilf);
            var environment  = Environment = new BuildEnvironment(backend, package, options, extensions, ilf, this);
            var input        = Input = new SourceReader(log, package, environment);
            var blockBuilder = BlockBuilder = new BlockBuilder(backend, il, ilf, resolver, this);
            var typeBuilder  = TypeBuilder = new TypeBuilder(environment, ilf, resolver, this);
            var bundle       = BundleBuilder = new BundleBuilder(backend, environment, ilf, this);

            AstProcessor = new AstProcessor(il, blockBuilder, typeBuilder, resolver, environment);
            UxlProcessor = new UxlProcessor(disk, backend.Name, il, extensions, environment, ilf);
            Plugins      = new PluginCache(log, bundle, ilf, environment);
            var pass = Pass = new CompilerPass(disk, data, environment, ilf, backend, input.Package, typeBuilder, resolver);

            Utilities      = new Utilities(il, pass);
            ILVerifier     = new ILVerifier(pass);
            ConstantFolder = new ConstantFolder(pass);
            ILStripper     = new ILStripper(pass);
        }
Exemplo n.º 2
0
        public bool Compile(string[] filenames)
        {
            bool success = true;

            Console.WriteLine("MultiPascal Delphi compiler");

            // Load, preprocess and order them
            planner.LoadFiles(filenames);

            if (planner.GetNumFiles() == 0)
            {
                return(false);
            }

            Console.WriteLine(planner.ListFiles());

            AstPrinter   astPrinter = new AstPrinter();
            NameResolver resolver   = new NameResolver();

            int skip = 0;

            foreach (SourceFile sf in planner.GetSourceFiles())
            {
                if (++skip < 0)
                {
                    continue;
                }

                Console.Write("####### Compile file " + Path.GetFileName(sf.name) + ": ");

                if (sf.preprocText == null)                             // preprocessing failed
                {
                    success = false;
                    Console.Error.WriteLine("preprocessing failed");
                    break;
                }

                StringReader sr = new StringReader(sf.preprocText);

                TranslationUnit ast = null;

                try {
                    ast = parser.Parse(sr, sf);
                }
                catch (ParserException e)
                {
                    Console.Error.WriteLine(e);
                    Console.Error.WriteLine("Parsing failed");
                    success = false;
                    break;
                }

                if (ast == null)
                {
                    Console.ReadLine();
                    continue;
                }

                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("Parsed OK: " + ast.name + " " + ast.ToString());
                Console.ResetColor();

                resolver.Reset(sf);
                resolver.Process(ast);
                Console.WriteLine("Name Resolving OK: " + ast.name + " " + ast.ToString());

                DeclarationsEnvironment env = resolver.declEnv;
                env.InitEnvironment();
                //	Console.WriteLine(env.symEnv.ListTreeFromRoot(Int32.Max, false));

                PostProcessing.SetParents(ast);
                //	new ParentProcessor().StartProcessing(ast);
                Console.WriteLine("SET parents OK: " + ast.name + " " + ast.ToString());

                //	astPrinter.Process(ast);
                //	Console.WriteLine(astPrinter.Output());

                ConstantFolder constfolder = new ConstantFolder();
                constfolder.Process(ast);

                CppCodegen cppGen = new CppCodegen();
                Console.WriteLine("Now compiling...");
                cppGen.Process(ast);
                Console.WriteLine(cppGen.Output());

                LlvmILGen llvmGen = new LlvmILGen();
                //	llvmGen.Process(ast);
            }

            return(success);
        }
Exemplo n.º 3
0
 public static SyntaxNode FoldConstants(this SyntaxNode node) =>
 ConstantFolder.Fold(node);