Example #1
0
        public static int Main(string[] args)
        {
            (string sourceFilePath, int exitCode) = ProcessArgs(args);
            if (exitCode != 0)
            {
                return(exitCode);
            }

            string objFilePath = Path.ChangeExtension(sourceFilePath, ".o");
            string irFilePath  = Path.ChangeExtension(sourceFilePath, ".ll");
            string asmPath     = Path.ChangeExtension(sourceFilePath, ".s");

            using var rdr     = File.OpenText(sourceFilePath);
            using var libLLVM = InitializeLLVM( );
            libLLVM.RegisterTarget(CodeGenTarget.Native);

            var machine = new TargetMachine(Triple.HostTriple);
            var parser  = new Parser(LanguageLevel.MutableVariables);

            using var generator = new CodeGenerator(parser.GlobalState, machine);
            Console.WriteLine("Ubiquity.NET.Llvm Kaleidoscope Compiler - {0}", parser.LanguageLevel);
            Console.WriteLine("Compiling {0}", sourceFilePath);

            IParseErrorLogger errorLogger = new ColoredConsoleParseErrorLogger( );

            // time the parse and code generation
            var timer = System.Diagnostics.Stopwatch.StartNew( );
            var ast   = parser.Parse(rdr);

            if (!errorLogger.CheckAndShowParseErrors(ast))
            {
                (bool hasValue, BitcodeModule? module) = generator.Generate(ast);
                if (!hasValue)
                {
                    Console.Error.WriteLine("No module generated");
                }
                else if (!module !.Verify(out string errMsg))
                {
                    Console.Error.WriteLine(errMsg);
                }
                else
                {
                    machine.EmitToFile(module, objFilePath, CodeGenFileType.ObjectFile);
                    timer.Stop( );

                    Console.WriteLine("Wrote {0}", objFilePath);
                    if (!module.WriteToTextFile(irFilePath, out string msg))
                    {
                        Console.Error.WriteLine(msg);
                        return(-1);
                    }

                    machine.EmitToFile(module, asmPath, CodeGenFileType.AssemblySource);
                    Console.WriteLine("Compilation Time: {0}", timer.Elapsed);
                }
            }
Example #2
0
        /// <summary>C# version of the LLVM Kaleidoscope language tutorial</summary>
        /// <param name="args">Command line arguments to the application</param>
        /// <returns>0 on success; non-zero on error</returns>
        /// <remarks>
        /// The command line options at present are 'WaitForDebugger' and the source file name
        ///
        /// Specifying 'WaitForDebugger' will trigger a wait loop in Main() to wait
        /// for an attached debugger if one is not yet attached. This is useful
        /// for mixed mode native+managed debugging as the SDK project system does
        /// not support that on launch.
        /// </remarks>
        public static int Main(string[] args)
        {
            (string sourceFilePath, int exitCode) = ProcessArgs(args);
            if (exitCode != 0)
            {
                return(exitCode);
            }

            string objFilePath = Path.ChangeExtension(sourceFilePath, ".o");
            string irFilePath  = Path.ChangeExtension(sourceFilePath, ".ll");
            string asmPath     = Path.ChangeExtension(sourceFilePath, ".s");

            using (TextReader rdr = File.OpenText(sourceFilePath))
                using (InitializeLLVM( ))
                {
                    RegisterNative( );

                    var machine = new TargetMachine(Triple.HostTriple);
                    var parser  = new Parser(LanguageLevel.MutableVariables);
                    using (var generator = new CodeGenerator(parser.GlobalState, machine, sourceFilePath, true))
                    {
                        Console.WriteLine("Llvm.NET Kaleidoscope Compiler - {0}", parser.LanguageLevel);
                        Console.WriteLine("Compiling {0}", sourceFilePath);

                        // time the parse and code generation
                        var      timer = System.Diagnostics.Stopwatch.StartNew( );
                        IAstNode ast   = parser.Parse(rdr);
                        generator.Generate(ast, null);
                        if (!generator.Module.Verify(out string errMsg))
                        {
                            Console.Error.WriteLine(errMsg);
                        }
                        else
                        {
                            machine.EmitToFile(generator.Module, objFilePath, CodeGenFileType.ObjectFile);
                            timer.Stop( );

                            Console.WriteLine("Wrote {0}", objFilePath);
                            if (!generator.Module.WriteToTextFile(irFilePath, out string msg))
                            {
                                Console.Error.WriteLine(msg);
                                return(-1);
                            }

                            machine.EmitToFile(generator.Module, asmPath, CodeGenFileType.AssemblySource);
                            Console.WriteLine("Compilation Time: {0}", timer.Elapsed);
                        }
                    }
                }

            return(0);
        }