Exemplo n.º 1
0
        static ILoaderResult Load(string input, AssemblerPackageOutputType inputType)
        {
            if (!System.IO.File.Exists(input))
            {
                throw new FileNotFoundException($"Source input file {input} not found.", input);
            }

            // Loader Stage 1 - read file
            ILoaderResult loaded;

            using (var fs = new FileStream(input, FileMode.Open, FileAccess.Read, FileShare.Read))
            {
                switch (inputType)
                {
                case AssemblerPackageOutputType.Elf32:
                    loaded = new LoaderElf32(fs).LoadImage();
                    break;

                case AssemblerPackageOutputType.Elf64:
                    loaded = new LoaderElf64(fs).LoadImage();
                    break;

                default:
                    throw new InvalidOperationException($"Unknown packaging format: {inputType}");
                }
            }

            return(loaded);
        }
Exemplo n.º 2
0
        static ICompilationResult Assemble(string output, AssemblerPackageOutputType outputType, string format, string input)
        {
            if (!System.IO.File.Exists(input))
            {
                return(CompilationResultBase.Error($"Source input file {input} not found."));
            }

            if (System.IO.File.Exists(output))
            {
                Console.Error.WriteLine($"Executable output file {output} already exists.");
                // DEBUG
                System.IO.File.Delete(output);
                //return -4;
            }

            // Compile.
            ICompilationResult compilation;
            {
                Console.Out.WriteLine($"Compiling source file: {input}");
                IBytecodeCompiler?compiler = null;
                switch (outputType)
                {
                case AssemblerPackageOutputType.Elf32:
                    Console.Out.WriteLine($"Packaging bytecode as: {Enum.GetName(typeof(AssemblerPackageOutputType), outputType)}");
                    compiler = new BytecodeCompiler <UInt32>();
                    break;

                case AssemblerPackageOutputType.Elf64:
                    Console.Out.WriteLine($"Packaging bytecode as: {Enum.GetName(typeof(AssemblerPackageOutputType), outputType)}");
                    compiler = new BytecodeCompiler <UInt64>();
                    break;

                default:
                    Console.Error.WriteLine($"Unsupported assembler output type {outputType}.");
                    System.Environment.Exit(-5);
                    return(null);
                }

                compilation = compiler.Compile(input);

                if (compilation.Errors.Count > 0)
                {
                    Console.Error.WriteLine($"Compilation failed with {compilation.Errors.Count} errors.");
                    return(compilation);
                }
            }

            // Package.
            IPackager packager;

            using (var fs = new FileStream(output, FileMode.Create, FileAccess.Write, FileShare.None, 4096, FileOptions.SequentialScan))
            {
                switch (outputType)
                {
                case AssemblerPackageOutputType.AOut32:
                    packager = new PackagerAOut32((CompilationResult32)compilation);
                    packager.Write(fs);
                    break;

                case AssemblerPackageOutputType.Elf32:
                    packager = new PackagerElf32((CompilationResult32)compilation);
                    packager.Write(fs);
                    break;

                case AssemblerPackageOutputType.Elf64:
                    packager = new PackagerElf64((CompilationResult64)compilation);
                    packager.Write(fs);
                    break;

                default:
                    throw new InvalidOperationException($"Unknown packaging format: {outputType}");
                }

                fs.Flush();
                fs.Close();
            }

            return(compilation);
        }