コード例 #1
0
ファイル: TestBase.cs プロジェクト: claassen/CmC
        public TestVM ExecuteTestVM(string testCode)
        {
            CompilationContext context = CmCompiler.CompileText(testCode);

            byte[] objectCode;

            using (var s = new MemoryStream())
            {
                CmCompiler.CreateObjectCode(s, new RIVMArchitecture(), context.GetIR(), context.GetStringConstants(), context.GetGlobalVariables(), context.GetFunctions());
                objectCode = s.GetBuffer();
            }

            byte[] executableCode;

            using (var s = new MemoryStream())
            {
                CmLinker.Link(s, new List <byte[]> {
                    objectCode
                }, false, SystemMemoryMap.BIOS_ROM_START);
                executableCode = s.GetBuffer();
            }

            var bios = new BIOS(executableCode);

            var mmu = new MMU(SystemMemoryMap.BIOS_STACK_END, bios, null, null);

            var cpu = new CPU(mmu);

            cpu.Start();

            return(new TestVM(cpu));
        }
コード例 #2
0
        static int Main(string[] args)
        {
            try
            {
                string sourceFilePath           = "";
                string objectFilePath           = "";
                bool   generateAssemblyFile     = false;
                bool   generateAssemblyComments = false;

                for (int i = 0; i < args.Length; i++)
                {
                    if (args[i] == "-a")
                    {
                        generateAssemblyFile = true;
                    }
                    else if (args[i] == "-ac")
                    {
                        generateAssemblyFile     = true;
                        generateAssemblyComments = true;
                    }
                    else if (args[i] == "-o")
                    {
                        i++;
                        objectFilePath = args[i];
                    }
                    else
                    {
                        sourceFilePath = args[i];
                    }
                }

                if (String.IsNullOrEmpty(sourceFilePath))
                {
                    ShowUsage();
                    return(-1);
                }

                CompilationContext context = CmCompiler.CompileFile(sourceFilePath);

                objectFilePath = !String.IsNullOrEmpty(objectFilePath)
                    ? objectFilePath
                    : Path.Combine(
                    Path.GetDirectoryName(sourceFilePath),
                    Path.GetFileNameWithoutExtension(sourceFilePath) + ".o"
                    );

                using (var fs = new FileStream(objectFilePath, FileMode.Create, FileAccess.Write, FileShare.None))
                {
                    CmCompiler.CreateObjectCode(fs, new RIVMArchitecture(), context.GetIR(), context.GetStringConstants(), context.GetGlobalVariables(), context.GetFunctions());
                }

                if (generateAssemblyFile)
                {
                    string assemblyFilePath = Path.Combine(
                        Path.GetDirectoryName(sourceFilePath),
                        Path.GetFileNameWithoutExtension(sourceFilePath) + ".a"
                        );

                    using (var fs = new FileStream(assemblyFilePath, FileMode.Create, FileAccess.Write, FileShare.None))
                    {
                        CmCompiler.GenerateAssemblyOutput(fs, context.GetIR(), generateAssemblyComments);
                    }
                }

                return(0);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error: " + ex.ToString());
                return(-1);
            }
        }