Пример #1
0
        /// <summary>
        /// Parses a file and outputs a .asm file
        /// </summary>
        /// <returns></returns>
        public static bool DoCompileFile(string inputFile, OptimizeLevel opLevel = OptimizeLevel.OptimizeNone, PassCount passCount = PassCount.PassAlls)
        {
            optimizeLevel = opLevel;
            string fileContents = TryOpenFile(inputFile);

            if (string.IsNullOrEmpty(fileContents))
            {
                return(false);
            }
            var module = DoCompile(fileContents);

            var asmWriter = new StreamWriter(Path.GetFileNameWithoutExtension(inputFile) + "_compiled.z80");

            asmWriter.Write(AssemblyGenerator.GenerateCode(ref module));
            asmWriter.Close();

            string outputFile = inputFile.Replace("expected", "actual");

            if (outputFile.Equals(inputFile))
            {
                outputFile = Path.GetFileNameWithoutExtension(inputFile) + "_compiled.c";
            }

            var writer = new StreamWriter(outputFile);

            writer.Write(module.ToString().Replace("\r\n", "\n").Replace("\n", "\r\n"));
            writer.Close();

            return(true);
        }
Пример #2
0
        static Module DoCompile(string fileContents, OptimizeLevel opLevel = OptimizeLevel.OptimizeNone, PassCount passCount = PassCount.PassAlls)
        {
            var tokenizerTokens = Tokenizer.Tokenize(fileContents);

            var preprocessorParser = new PreprocessorParser(tokenizerTokens);
            var preProcessorTokens = preprocessorParser.Parse();

            var tokenPassTokens = RunTokenPasses(preProcessorTokens);

            var tokens = tokenPassTokens.GetEnumerator();

            tokens.MoveNext();

            var currentModule = Module.ParseModule(ref tokens);

            if (optimizeLevel != OptimizeLevel.OptimizeNone)
            {
                Optimizer.Loop.Optimizer.RunOptimizer(ref currentModule, optimizeLevel);
            }

            WriteModule(currentModule, 1);

            // Statement passes
            if (passCount >= PassCount.Pass2)
            {
                StatementPasses.BlockCollapse.Run(currentModule);
                StatementPasses.IfGotoConversion.Run(currentModule);
                StatementPasses.LoopGotoConversion.Run(currentModule);
                StatementPasses.ReorderDeclarations.Run(currentModule);
                StatementPasses.MarkRecursiveFunctions.Run(currentModule);
            }
            WriteModule(currentModule, 2);

            //if (optimizeLevel != OptimizeLevel.OptimizeNone)
            //	Optimizer.Optimizer.RunOptimizer(ref currentModule, optimizeLevel);

            WriteModule(currentModule, 3);

            if (passCount >= PassCount.Pass3)
            {
                //StatementPasses.ReplaceLocalsWithGlobals.Run(currentModule);
                StatementPasses.ApplyCallingConvention.Run(currentModule);
                StatementPasses.LabelMerger.Run(currentModule);
                StatementPasses.RemovePointlessGotos.Run(currentModule);

                // Adjust the arithmetic
                StatementPasses.ConvertAddSubToIncDec.Run(currentModule);
                StatementPasses.RemoveMathImmediates.Run(currentModule);

                StatementPasses.RegisterAllocator.DumbRegisterAllocator.Run(currentModule);
            }

            currentModule.IntermediateStrings.Add("#include <string.h>");
            string code    = currentModule.ToString();
            var    asmCode = AssemblyGenerator.GenerateCode(ref currentModule);

            return(currentModule);
        }
Пример #3
0
 public VirtualTemplate(
     string name,
     OptimizeLevel l,
     TemplateCache cache = null)
 {
     Name = name;
     Cache = cache ?? new TemplateCache();
     TrimMode = TrimMode.FollowingNewLine;
     OptimizeLevel = l;
 }