Esempio n. 1
0
        public static void CompileAssembly(string inputFile, string outputFile, bool generateIR = false, System.Type[] additionalTypes = null)
        {
            var assemblyDefinition = LoadAssembly(inputFile);

            var compiler = new Compiler();

            compiler.RegisterMainAssembly(assemblyDefinition);

            if (additionalTypes != null)
            {
                foreach (var type in additionalTypes)
                {
                    var assembly     = assemblyDefinition.MainModule.AssemblyResolver.Resolve(type.Assembly.FullName);
                    var resolvedType = assembly.MainModule.GetType(type.FullName);
                    compiler.RegisterType(resolvedType);
                }
            }

            var module = compiler.GenerateModule();

            if (generateIR)
            {
                var irFile = Path.ChangeExtension(outputFile, "ll");
                var ir     = LLVM.PrintModuleToString(module);
                File.WriteAllText(irFile, ir);
            }

            LLVM.WriteBitcodeToFile(module, outputFile);
        }
Esempio n. 2
0
        public static void CompileAssembly(string inputFile, string outputFile, bool generateIR = false, System.Type[] additionalTypes = null)
        {
            var assemblyDefinition = LoadAssembly(inputFile);

            var compiler = new Compiler();
            compiler.RegisterMainAssembly(assemblyDefinition);

            if (additionalTypes != null)
            {
                foreach (var type in additionalTypes)
                {
                    var assembly = assemblyDefinition.MainModule.AssemblyResolver.Resolve(type.Assembly.FullName);
                    var resolvedType = assembly.MainModule.GetType(type.FullName);
                    compiler.RegisterType(resolvedType);
                }
            }

            var module = compiler.GenerateModule();

            if (generateIR)
            {
                var irFile = Path.ChangeExtension(outputFile, "ll");
                var ir = LLVM.PrintModuleToString(module);
                File.WriteAllText(irFile, ir);
            }

            LLVM.WriteBitcodeToFile(module, outputFile);
        }
Esempio n. 3
0
        public static void CompileAssembly(string inputFile, string outputFile, string triple, bool generateIR = false, bool verifyModule = true, System.Type[] additionalTypes = null)
        {
            var assemblyDefinition = LoadAssembly(inputFile);

            // Generate marshalling code for PInvoke
            var mcg = new MarshalCodeGenerator(assemblyDefinition);
            mcg.Generate();
            //mcg.AssemblyDefinition.Write(Path.Combine(Path.GetDirectoryName(inputFile), Path.GetFileNameWithoutExtension(inputFile) + ".Marshalled.dll"), new WriterParameters {  });

            var compiler = new Compiler(triple);
            compiler.TestMode = additionalTypes != null;
            compiler.PrepareAssembly(assemblyDefinition);

            if (additionalTypes != null)
            {
                foreach (var type in additionalTypes)
                {
                    var assembly = assemblyDefinition.MainModule.AssemblyResolver.Resolve(type.Assembly.FullName);
                    var resolvedType = assembly.MainModule.GetType(type.FullName);
                    compiler.RegisterType(resolvedType);
                }
            }

            foreach (var type in assemblyDefinition.MainModule.Types)
            {
                foreach (var attribute in type.CustomAttributes)
                {
                    if (attribute.AttributeType.Name == "EmbedTestAttribute")
                    {
                        compiler.RegisterType((TypeReference)attribute.ConstructorArguments[0].Value);
                    }
                }
            }

            compiler.ProcessAssembly(assemblyDefinition);

            var module = compiler.GenerateModule();

            if (generateIR)
            {
                var irFile = Path.ChangeExtension(outputFile, "ll");
                string message;
                if (LLVM.PrintModuleToFile(module, irFile, out message))
                {
                    throw new InvalidOperationException(message);
                }
            }

            if (verifyModule)
            {
                // Verify module
                string message;
                if (LLVM.VerifyModule(module, VerifierFailureAction.PrintMessageAction, out message))
                {
                    throw new InvalidOperationException(message);
                }
            }

            LLVM.WriteBitcodeToFile(module, outputFile);
        }
Esempio n. 4
0
        public static void CompileAssembly(string inputFile, string outputFile, bool generateIR = false, bool verifyModule = true, System.Type[] additionalTypes = null)
        {
            var assemblyDefinition = LoadAssembly(inputFile);

            var compiler = new Compiler();

            compiler.TestMode = additionalTypes != null;
            compiler.PrepareAssembly(assemblyDefinition);

            if (additionalTypes != null)
            {
                foreach (var type in additionalTypes)
                {
                    var assembly     = assemblyDefinition.MainModule.AssemblyResolver.Resolve(type.Assembly.FullName);
                    var resolvedType = assembly.MainModule.GetType(type.FullName);
                    compiler.RegisterType(resolvedType);
                }
            }

            foreach (var type in assemblyDefinition.MainModule.Types)
            {
                foreach (var attribute in type.CustomAttributes)
                {
                    if (attribute.AttributeType.Name == "EmbedTestAttribute")
                    {
                        compiler.RegisterType((TypeReference)attribute.ConstructorArguments[0].Value);
                    }
                }
            }

            compiler.ProcessAssembly(assemblyDefinition);

            var module = compiler.GenerateModule();

            if (generateIR)
            {
                var    irFile = Path.ChangeExtension(outputFile, "ll");
                string message;
                if (LLVM.PrintModuleToFile(module, irFile, out message))
                {
                    throw new InvalidOperationException(message);
                }
            }

            if (verifyModule)
            {
                // Verify module
                string message;
                if (LLVM.VerifyModule(module, VerifierFailureAction.PrintMessageAction, out message))
                {
                    throw new InvalidOperationException(message);
                }
            }

            LLVM.WriteBitcodeToFile(module, outputFile);
        }