예제 #1
0
파일: Program.cs 프로젝트: kinoh/LLVM.NET
        static void Main(string[] args)
        {
            var context = new LLVMContext();
            var module = new Module("NoName", context);
            var builder = new IRBuilder(context);
            var t = builder.getInt32Ty();
            var funcType = FunctionType.get(t, false);
            var main = Function.Create(funcType, GlobalValue.LinkageTypes.ExternalLinkage, "main", module);
            var entry = BasicBlock.Create(context, "entrypoint", main);
            builder.SetInsertPoint(entry);

            var hello = builder.CreateGlobalStringPtr("Hello, World!");

            var putsType = FunctionType.get(builder.getInt32Ty(),
                new[] { builder.getInt8Ty().getPointerTo(0) }, false);
            var putsFunc = module.getOrInsertFunction("puts", putsType);

            builder.CreateCall(putsFunc, hello);

            var variable = builder.CreateAlloca(t);
            var val = ConstantInt.get(t, 127);
            builder.CreateStore(val, variable);

            builder.CreateRetVoid();

            var pm = new PassManager();
            var os = new raw_fd_ostream("code.ll");
            pm.add(Passes.createPrintModulePass(os));
            pm.run(module);
            os.close();

            Console.ReadKey();
        }
예제 #2
0
        static void Main(string[] args)
        {
            LLVMContext context = new LLVMContext();
            BrainF compiler = new BrainF(context);

            compiler.Parse("test.bf", 512);
            compiler.GeneratedModule.Dump();

            Console.ReadLine();
        }
예제 #3
0
 /// <summary>
 /// Initialises a new instance of this class.
 /// </summary>
 public BrainF(LLVMContext context)
 {
     this.context = context;
 }
예제 #4
0
        static void Main(string[] args)
        {
            if (args.Length < 1)
            {
                Console.WriteLine("Usage: BASICLLVM <inputfile> <flags>");
                Console.WriteLine("-debug enables compiler debugging output");
                Console.WriteLine("-block blocks on finished compile");
                Console.WriteLine("-o <outputfile> outputs to specified file");
                Console.WriteLine("-output LL/S/EXE specifies desired output format");
                Console.WriteLine("-output LL/S/EXE specifies desired output format");
                return;
            }
            // Interpret arguments
            string inputFile         = args[0];
            string defaultOutputFile = inputFile.Substring(0, inputFile.LastIndexOf("."));

            outputFile = defaultOutputFile;

            bool outNext    = false;
            bool formatNext = false;

            foreach (string arg in args)
            {
                if (outNext)
                {
                    outputFile = arg;
                    outNext    = false;
                }
                if (formatNext)
                {
                    outputFormat = (arg == "EXE") ? outputFormats.EXE : ((arg == "S") ? outputFormats.S : outputFormats.LL);
                    formatNext   = false;
                }
                if (arg == "-debug")
                {
                    debug = true;
                }
                if (arg == "-block")
                {
                    block = true;
                }
                if (arg == "-o")
                {
                    outNext = true;
                }
                if (arg == "-output")
                {
                    formatNext = true;
                }
            }
            if (outputFile.Equals(defaultOutputFile))
            {
                if (outputFormat == outputFormats.LL)
                {
                    outputFile += ".ll";
                }
                if (outputFormat == outputFormats.S)
                {
                    outputFile += ".s";
                }
                if (outputFormat == outputFormats.EXE)
                {
                    outputFile += ".exe";
                }
            }

            // Setup LLVM
            LLVMContext context = new LLVMContext();

            Parser.context = context;

            /*
             * PARSE STAGE
             */

            if (debug)
            {
                Console.WriteLine("---");
            }
            if (debug)
            {
                Console.WriteLine();
            }

            if (debug)
            {
                Console.WriteLine("1 - Parsing");
            }
            List <Statement> lines = Parser.parseFile(inputFile);

            if (lines == null)
            {
                // parsing failed
                if (block)
                {
                    Console.ReadLine();
                }
                return;
            }
            if (debug)
            {
                Console.WriteLine("Done");
            }

            if (debug)
            {
                Console.WriteLine();
            }
            if (debug)
            {
                Console.WriteLine("---");
            }
            if (debug)
            {
                Console.WriteLine();
            }

            /*
             * COMPILE STAGE
             */
            if (debug)
            {
                Console.WriteLine("2 - Compiling");
            }
            // Setup LLVM module

            Module module = new Module(context, "SourceFile");

            Parser.module = module;
            // Setup LLVM function
            LLVM.Type[]  mainArgs     = new LLVM.Type[] { Parser.i32, Parser.i8pp };
            FunctionType mainType     = new FunctionType(Parser.i32, mainArgs);
            Function     mainFunction = new Function(module, "main", mainType);

            Parser.function = mainFunction;


            try
            {
                for (int i = 0; i < lines.Count; i++)
                {
                    Parser.currentLine = i;
                    // Compile Line
                    lines[i].code();

                    if (lines[i].hasLineNumber)
                    {
                        // add code line number to dictionary
                        if (!Parser.variables.lines.ContainsKey(lines[i].lineNumber))
                        {
                            Parser.variables.lines.Add(lines[i].lineNumber, lines[i]);
                        }
                        else
                        {
                            CompileException ex = new CompileException("Duplicate line number");
                            ex.message = "Line numbers must be unique";
                            throw ex;
                        }
                    }
                }

                // add jumps between adjacent lines
                for (int i = 0; i < lines.Count - 1; i++)
                {
                    lines[i].jumpToNext(lines[i + 1]);
                }

                // process control flow statements
                for (int i = 0; i < lines.Count; i++)
                {
                    lines[i].processGoto();
                }
            }
            catch (CompileException ex)
            {
                ex.print("COMPILE ERROR");
                if (block)
                {
                    Console.ReadLine();
                }
                return;
            }

            if (debug)
            {
                Console.WriteLine("Done");
            }
            if (debug)
            {
                Console.WriteLine();
                Console.WriteLine("---");
                Console.WriteLine();
                // Compile is complete
                ConsoleColor prevColor = Console.ForegroundColor;
                Console.ForegroundColor = ConsoleColor.Magenta;
                module.Dump();                 // Dump LLVM IR for debug purposes
                Console.ForegroundColor = prevColor;
                Console.WriteLine();
                Console.WriteLine("---");
                Console.WriteLine();
                Console.WriteLine("3 - Output");
            }

            // Write out LLVM module to file
            try
            {
                module.WriteToFile("ll.tmp");
            }
            catch (Exception e)
            {
                CompileException.printColour("Failed to write to output file", ConsoleColor.Red);
                Console.WriteLine(e.Message);
            }


            // Call llc and gcc as well if we were asked to do that
            switch (outputFormat)
            {
            case outputFormats.LL:
                File.Delete(outputFile);
                File.Move("ll.tmp", outputFile);
                break;

            case outputFormats.S:
                do_llc();
                File.Delete(outputFile);
                File.Move("ll.tmp.s", outputFile);
                break;

            case outputFormats.EXE:
                do_llc();
                do_gcc();
                break;
            }

            if (debug)
            {
                Console.WriteLine("Compile complete");
            }
            if (block)
            {
                Console.ReadLine();
            }
        }