private static void AddRuntimeReference(ChelaCompiler compiler) { compiler.LoadReference(RuntimeLibrary); }
public static void Main(string[] args) { List<string> inputFiles = new List<string>(); List<string> references = new List<string>(); List<string> nativeLibs = new List<string>(); List<string> libFindPaths = new List<string>(); List<ResourceData> resources = new List<ResourceData> (); string outputFile = null; bool verbose = false; bool bitcode = false; bool debug = false; bool llvmBitcode = false; bool assembler = false; bool llvmAssembler = false; bool objectCode = false; bool noruntime = false; bool noreflection = false; bool bench = false; int optimization = 0; ModuleType moduleType = ModuleType.Executable; // Parse the command line. CommandLineParser parser = new CommandLineParser(); parser.Help = PrintHelp; parser.Miscellaneous = delegate (string input) { inputFiles.Add(input); }; // Output file. parser.AddArgument(delegate(string output) { outputFile = output; }, "-o", "--output"); // Help. parser.AddArgument(PrintHelp, "-h", "--help"); // Resource. parser.AddArgument(delegate(string nameId) { string fileName = nameId; string name; if(nameId.IndexOf(',') != -1) { string[] pair = nameId.Split(','); fileName = pair[0]; name = pair[1]; } else { name = Path.GetFileName(fileName); } resources.Add(new ResourceData(fileName, name)); }, "-resource"); // Benchmarking. parser.AddArgument(delegate() { bench = true; }, "-benchmark"); // Version. parser.AddArgument(PrintVersion, "--version"); // Verbose parser.AddArgument(delegate() { verbose = true; }, "-v", "--verbose"); // Debug parser.AddArgument(delegate() { debug = true; }, "-g"); // Optimization. parser.AddPrefix(delegate(string level) { optimization = int.Parse(level); }, "-O"); // Reference. parser.AddArgument(delegate(string refName) { references.Add(refName); }, "-r", "-ref"); // Native libraries. parser.AddArgument(delegate(string libName) { nativeLibs.Add(libName); }, "-l"); parser.AddPrefix(delegate(string libName) { nativeLibs.Add(libName); }, "-l"); // Library find paths. parser.AddPrefix(delegate(string libPath) { libFindPaths.Add(libPath); }, "-L"); // Target. parser.AddArgument(delegate(string targetType) { if(targetType == "lib" || targetType == "library") moduleType = ModuleType.Library; else if(targetType == "exe" || targetType == "executable") moduleType = ModuleType.Executable; else { Console.Error.WriteLine("Unknown module type '" + targetType + "'"); PrintHelp(); System.Environment.Exit(-1); } }, "-t", "--target"); // Implicit reference to the runtime. parser.AddArgument(delegate() { noruntime = true; }, "-noruntime"); // No reflection info. parser.AddArgument(delegate() { noreflection = true; }, "-noreflection"); // Bitcode. parser.AddArgument(delegate() { bitcode = true; }, "-b"); // Llvm Bitcode. parser.AddArgument(delegate() { llvmBitcode = true; }, "-B"); // Llvm assembler. parser.AddArgument(delegate() { llvmAssembler = true; }, "-S"); // Assembler. parser.AddArgument(delegate() { assembler = true; }, "-s"); // Object code. parser.AddArgument(delegate() { objectCode = true; }, "-c"); // Parse the command line. try { parser.Parse(args); } catch(Exception e) { Console.Error.WriteLine(e.ToString()); Environment.Exit(-1); } if(outputFile == null || outputFile == "") outputFile = "chelamod.out"; try { // Create the compiler. ChelaCompiler compiler = new ChelaCompiler(moduleType); compiler.SetDebugBuild(debug); // Set benchmarking. Benchmark.Enabled = bench; // Add runtime search paths. if(!noruntime) AddRuntimePaths(compiler); // Libraries paths. foreach(string libPath in libFindPaths) compiler.AddLibraryPath(libPath); // Add runtime reference. if(!noruntime) AddRuntimeReference(compiler); // Load the references. foreach(string modRef in references) compiler.LoadReference(modRef); // Add native libraries. foreach(string natLib in nativeLibs) compiler.AddNativeLibrary(natLib); // Add each resource. foreach(ResourceData resource in resources) compiler.AddResource(resource); // Compile each input file. foreach(string input in inputFiles) { // Compile the input file. compiler.CompileFile(input); } // Link the program. compiler.LinkProgram(); // Set the module name. string moduleName = CreateModuleName(outputFile); compiler.SetModuleName(moduleName); // Dump the program. if(verbose) compiler.Dump(); // Write the chela bitcode. string bitcodeFile = bitcode ? outputFile : CreateTempName(); compiler.WriteOutput(bitcodeFile); // Write the benchmark resume. if(bench) Benchmark.Resume(); // Return if the bitcode was the only thing asked. if(bitcode) return; // Build the ChelaVm command line. StringBuilder commandLine = new StringBuilder(); // Add the debug options. if(debug) AppendArg(commandLine, "-g"); // Add the no runtime flag. if(noruntime) AppendArg(commandLine, "-noruntime"); // Add the no reflection flag if specified. if(noreflection) AppendArg(commandLine, "-noreflection"); // Add the optimization level. AppendArg(commandLine, "-O" + optimization); // Set the output type. if(llvmBitcode) AppendArg(commandLine, "-B"); else if(llvmAssembler) AppendArg(commandLine, "-S"); else if(assembler) AppendArg(commandLine, "-s"); else if(objectCode) AppendArg(commandLine, "-c"); // Add the library find paths. foreach(string libPath in libFindPaths) AppendArg(commandLine, "-L" + libPath); // Add the output file. AppendArg(commandLine, "-o"); AppendArg(commandLine, outputFile); // Add the bitcode file. AppendArg(commandLine, bitcodeFile); // Invoke the ChelaVm compiler. System.Diagnostics.Process proc = new System.Diagnostics.Process(); proc.StartInfo.WorkingDirectory = Environment.CurrentDirectory; proc.StartInfo.FileName = GetChelaVm(); proc.StartInfo.UseShellExecute = false; proc.StartInfo.Arguments = commandLine.ToString(); proc.StartInfo.RedirectStandardError = false; proc.StartInfo.RedirectStandardOutput = false; proc.StartInfo.RedirectStandardInput = false; proc.Start(); proc.WaitForExit(); //System.Console.WriteLine("{0} {1}", GetChelaVm(), commandLine.ToString()); Environment.ExitCode = proc.ExitCode; proc.Close(); // Delete the temporal file. System.IO.File.Delete(bitcodeFile); } catch(CompilerException e) { Console.Error.WriteLine(e.Message); if(verbose) Console.Error.WriteLine(e.StackTrace); Environment.ExitCode = -1; } catch(ApplicationException e) { Console.Error.WriteLine(e.Message); Console.Error.WriteLine(e.StackTrace); Environment.ExitCode = -1; } }