public void Flush()
        {
            if (_textEmitter != null)
            {
                _textEmitter.Flush();
                _textEmitter.Dispose();
                _textEmitter = null;

                string platform = _flags.HasFlag(CompilationFlags.Platform32) ? " /32BITPREFERRED" : " /X64";
                string debug = _flags.HasFlag(CompilationFlags.NoDebug) ? string.Empty : " /DEBUG";
                string dll = _flags.HasFlag(CompilationFlags.WriteDll) ? " /DLL" : string.Empty;

                // Find the path to ilasm.exe
                string mscorlibPath = typeof(object).Assembly.Location;
                string frameworkDir = Path.GetDirectoryName(mscorlibPath);
                string ilasmPath = Path.Combine(frameworkDir, "ilasm.exe");

                // Invoke ilasm to convert the textual CIL into a PE file.
                Process process = new Process();
                process.StartInfo.FileName = ilasmPath;
                process.StartInfo.WorkingDirectory = Path.GetDirectoryName(_outputFile);
                process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
                process.StartInfo.Arguments = string.Format(
                    @"{0}{1}{2}{3} /OUTPUT={4}",
                    _ilFile,
                    platform,
                    debug,
                    dll,
                    _outputFile);

                process.Start();
                process.WaitForExit();
            }
        }
Exemple #2
0
        public void Flush()
        {
            if (_textEmitter != null)
            {
                _textEmitter.Flush();
                _textEmitter.Dispose();
                _textEmitter = null;

                string platform = _flags.HasFlag(CompilationFlags.Platform32) ? " /32BITPREFERRED" : " /X64";
                string debug    = _flags.HasFlag(CompilationFlags.NoDebug) ? string.Empty : " /DEBUG";
                string dll      = _flags.HasFlag(CompilationFlags.WriteDll) ? " /DLL" : string.Empty;

                // Find the path to ilasm.exe
                string mscorlibPath = typeof(object).Assembly.Location;
                string frameworkDir = Path.GetDirectoryName(mscorlibPath);
                string ilasmPath    = Path.Combine(frameworkDir, "ilasm.exe");

                // Invoke ilasm to convert the textual CIL into a PE file.
                Process process = new Process();
                process.StartInfo.FileName         = ilasmPath;
                process.StartInfo.WorkingDirectory = Path.GetDirectoryName(_outputFile);
                process.StartInfo.WindowStyle      = ProcessWindowStyle.Hidden;
                process.StartInfo.Arguments        = string.Format(
                    @"{0}{1}{2}{3} /OUTPUT={4}",
                    _ilFile,
                    platform,
                    debug,
                    dll,
                    _outputFile);

                process.Start();
                process.WaitForExit();
            }
        }
        /// <summary>
        /// Initializes a new instance of the PeEmitter class.
        /// Use this constructor to when saving the PE file to disk
        /// </summary>
        /// <param name="outputFile">Path to output file</param>
        /// <param name="flags">Compiler flags</param>
        public PeEmitter(string outputFile, CompilationFlags flags)
        {
            _ilFile = GetPathToTempFile("il");
            _textEmitter = new TextEmitter(_ilFile);

            _outputFile = outputFile;
            _flags = flags;
        }
        /// <summary>
        /// Initializes a new instance of the PeEmitter class.
        /// Use this constructor to when saving the PE file to disk
        /// </summary>
        /// <param name="outputFile">Path to output file</param>
        /// <param name="flags">Compiler flags</param>
        public PeEmitter(string outputFile, CompilationFlags flags)
        {
            _ilFile = GetPathToTempFile("il");
            _textEmitter = new TextEmitter(_ilFile);

            _outputFile = outputFile;
            _flags = flags;
        }
        public static TestCompilerContext Create(string compiland, GlobalSymbolList globals, CompilationFlags flags)
        {
            byte[] buffer = Encoding.Default.GetBytes(compiland);
            MemoryStream input = new MemoryStream(buffer);
            StreamReader reader = new StreamReader(input);
            MemoryStream output = new MemoryStream();
            TextEmitter emitter = new TextEmitter(output);

            TestCompilerContext testContext = new TestCompilerContext(input, reader, output, emitter, flags);
            if (globals != null)
            {
                foreach (var symbol in globals.Items)
                    testContext.SymbolTable.Add(symbol.Item1, symbol.Item2, StorageClass.Global, null);
            }

            return testContext;
        }
        public static CmdLineCompilerContext Create(string sourcePath, CompilationFlags flags)
        {
            string outputFile;
            IEmitter emitter;

            if (flags.HasFlag(CompilationFlags.Assembly))
            {
                outputFile = Path.ChangeExtension(sourcePath, "il");
                emitter = new TextEmitter(outputFile);
            }
            else
            {
                outputFile = Path.ChangeExtension(sourcePath, "exe");
                emitter = new PeEmitter(outputFile, flags);
            }

            Stream inputFile = File.Open(sourcePath, FileMode.Open, FileAccess.Read);
            StreamReader reader = new StreamReader(inputFile);

            return new CmdLineCompilerContext(sourcePath, inputFile, reader, emitter, flags);
        }
        public void Flush()
        {
            if (_textEmitter != null)
            {
                _textEmitter.Flush();
                _textEmitter.Dispose();
                _textEmitter = null;

                string platform = _flags.HasFlag(CompilationFlags.Platform32) ? " -32BITPREFERRED" : " -X64";
                string debug = _flags.HasFlag(CompilationFlags.NoDebug) ? string.Empty : " -DEBUG";
                string dll = _flags.HasFlag(CompilationFlags.WriteDll) ? " -DLL" : string.Empty;

                string mscorlibPath;
                string frameworkDir;
                string ilasmPath = null;

                bool isWindows = RuntimeInformation.IsOSPlatform(OSPlatform.Windows);

                // If we are on Windows, and we aren't targetting .NET Core, look next to our CoreLib's ilasm.exe. If we are running on desktop framework this will work.
                if (!_flags.HasFlag(CompilationFlags.NetCore) && isWindows)
                {
                    mscorlibPath = typeof(object).Assembly.Location;
                    frameworkDir = Path.GetDirectoryName(mscorlibPath);
                    ilasmPath = Path.Combine(frameworkDir, "ilasm.exe");
                }

                if (ilasmPath == null || !File.Exists(ilasmPath))
                {
                    string thisDir = Path.GetDirectoryName(GetType().Assembly.Location);
                    string ilasmFileName = isWindows ? "ilasm.exe" : "ilasm";
                    ilasmPath = Path.Combine(thisDir, ilasmFileName);

                    if (!File.Exists(ilasmPath))
                        throw new FileNotFoundException("ilasm cannot be found, make sure netcore ilasm is present in the same directory as IrisCompiler.dll");

                    if (!string.IsNullOrEmpty(debug)) // netcore ilasm requires explicitly specifying pdb format
                        debug += " -PDBFMT=PORTABLE";
                }

                // Invoke ilasm to convert the textual CIL into a PE file.
                using (Process process = new Process())
                {
                    process.StartInfo.FileName = ilasmPath;
                    process.StartInfo.WorkingDirectory = Path.GetDirectoryName(_outputFile);
                    process.StartInfo.RedirectStandardError = true;
                    process.StartInfo.RedirectStandardOutput = true;
                    process.StartInfo.CreateNoWindow = true;
                    process.StartInfo.Arguments = string.Format(
                        @"{0} -QUIET{1}{2}{3} -OUTPUT={4}",
                        _ilFile,
                        platform,
                        debug,
                        dll,
                        _outputFile);
                    process.StartInfo.UseShellExecute = false;

                    process.OutputDataReceived += (object sender, DataReceivedEventArgs e) =>
                    {
                        // Ignore output. This ensures that the output stream doesn't fill up.
                    };
                    process.Start();
                    process.WaitForExit();

                    if (process.ExitCode != 0)
                    {
                        throw new InvalidOperationException("ilasm failed.\nError text: " + process.StandardError.ReadToEnd());
                    }
                }
            }
        }