Beispiel #1
0
        /// <summary>Link another module into this one</summary>
        /// <param name="otherModule">module to link into this one</param>
        /// <remarks>
        /// <note type="warning">
        /// <paramref name="otherModule"/> is destroyed by this process and no longer usable
        /// when this method returns.
        /// </note>
        /// </remarks>
        public void Link(BitcodeModule otherModule)
        {
            ValidateHandle( );
            otherModule.ValidateNotNull(nameof(otherModule));

            if (otherModule.Context != Context)
            {
                throw new ArgumentException("Linking modules from different contexts is not allowed", nameof(otherModule));
            }

            if (!LLVMLinkModules2(ModuleHandle, otherModule.ModuleHandle).Succeeded)
            {
                throw new InternalCodeGeneratorException("Module link error");
            }

            Context.RemoveModule(otherModule);
            otherModule.ModuleHandle = new LLVMModuleRef(IntPtr.Zero);
        }
        /// <summary>Generate code for the target machine from a module</summary>
        /// <param name="module"><see cref="BitcodeModule"/> to generate the code from</param>
        /// <param name="path">Path to the output file</param>
        /// <param name="fileType">Type of file to emit</param>
        public void EmitToFile(BitcodeModule module, string path, CodeGenFileType fileType)
        {
            module.ValidateNotNull(nameof(module));
            path.ValidateNotNullOrWhiteSpace(nameof(path));
            fileType.ValidateDefined(nameof(path));

            if (module.TargetTriple != null && Triple != module.TargetTriple)
            {
                throw new ArgumentException(Resources.Triple_specified_for_the_module_doesn_t_match_target_machine, nameof(module));
            }

            var status = LLVMTargetMachineEmitToFile(TargetMachineHandle
                                                     , module.ModuleHandle
                                                     , path
                                                     , ( LLVMCodeGenFileType )fileType
                                                     , out string errTxt
                                                     );

            if (status.Failed)
            {
                throw new InternalCodeGeneratorException(errTxt);
            }
        }
        /// <summary>Emits the module for the target machine to a <see cref="MemoryBuffer"/></summary>
        /// <param name="module">Module to emit to the buffer</param>
        /// <param name="fileType">Type of file to generate into the buffer</param>
        /// <returns><see cref="MemoryBuffer"/> containing the generated code</returns>
        /// <remarks>
        /// The <see cref="BitcodeModule.TargetTriple"/> must match the <see cref="Triple"/> for this
        /// target.
        /// </remarks>
        public MemoryBuffer EmitToBuffer(BitcodeModule module, CodeGenFileType fileType)
        {
            module.ValidateNotNull(nameof(module));
            fileType.ValidateDefined(nameof(fileType));

            if (module.TargetTriple != null && Triple != module.TargetTriple)
            {
                throw new ArgumentException(Resources.Triple_specified_for_the_module_doesn_t_match_target_machine, nameof(module));
            }

            var status = LLVMTargetMachineEmitToMemoryBuffer(TargetMachineHandle
                                                             , module.ModuleHandle
                                                             , ( LLVMCodeGenFileType )fileType
                                                             , out string errTxt
                                                             , out LLVMMemoryBufferRef bufferHandle
                                                             );

            if (status.Failed)
            {
                throw new InternalCodeGeneratorException(errTxt);
            }

            return(new MemoryBuffer(bufferHandle));
        }