xref: /freebsd-src/contrib/llvm-project/llvm/lib/Bitcode/Writer/BitWriter.cpp (revision 5b27928474e6a4103d65b347544705c40c9618fd)
10b57cec5SDimitry Andric //===-- BitWriter.cpp -----------------------------------------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric 
90b57cec5SDimitry Andric #include "llvm-c/BitWriter.h"
100b57cec5SDimitry Andric #include "llvm/Bitcode/BitcodeWriter.h"
110b57cec5SDimitry Andric #include "llvm/IR/Module.h"
120b57cec5SDimitry Andric #include "llvm/Support/FileSystem.h"
130b57cec5SDimitry Andric #include "llvm/Support/MemoryBuffer.h"
140b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h"
150b57cec5SDimitry Andric using namespace llvm;
160b57cec5SDimitry Andric 
170b57cec5SDimitry Andric 
180b57cec5SDimitry Andric /*===-- Operations on modules ---------------------------------------------===*/
190b57cec5SDimitry Andric 
LLVMWriteBitcodeToFile(LLVMModuleRef M,const char * Path)200b57cec5SDimitry Andric int LLVMWriteBitcodeToFile(LLVMModuleRef M, const char *Path) {
210b57cec5SDimitry Andric   std::error_code EC;
22*8bcb0991SDimitry Andric   raw_fd_ostream OS(Path, EC, sys::fs::OF_None);
230b57cec5SDimitry Andric 
240b57cec5SDimitry Andric   if (EC)
250b57cec5SDimitry Andric     return -1;
260b57cec5SDimitry Andric 
270b57cec5SDimitry Andric   WriteBitcodeToFile(*unwrap(M), OS);
280b57cec5SDimitry Andric   return 0;
290b57cec5SDimitry Andric }
300b57cec5SDimitry Andric 
LLVMWriteBitcodeToFD(LLVMModuleRef M,int FD,int ShouldClose,int Unbuffered)310b57cec5SDimitry Andric int LLVMWriteBitcodeToFD(LLVMModuleRef M, int FD, int ShouldClose,
320b57cec5SDimitry Andric                          int Unbuffered) {
330b57cec5SDimitry Andric   raw_fd_ostream OS(FD, ShouldClose, Unbuffered);
340b57cec5SDimitry Andric 
350b57cec5SDimitry Andric   WriteBitcodeToFile(*unwrap(M), OS);
360b57cec5SDimitry Andric   return 0;
370b57cec5SDimitry Andric }
380b57cec5SDimitry Andric 
LLVMWriteBitcodeToFileHandle(LLVMModuleRef M,int FileHandle)390b57cec5SDimitry Andric int LLVMWriteBitcodeToFileHandle(LLVMModuleRef M, int FileHandle) {
400b57cec5SDimitry Andric   return LLVMWriteBitcodeToFD(M, FileHandle, true, false);
410b57cec5SDimitry Andric }
420b57cec5SDimitry Andric 
LLVMWriteBitcodeToMemoryBuffer(LLVMModuleRef M)430b57cec5SDimitry Andric LLVMMemoryBufferRef LLVMWriteBitcodeToMemoryBuffer(LLVMModuleRef M) {
440b57cec5SDimitry Andric   std::string Data;
450b57cec5SDimitry Andric   raw_string_ostream OS(Data);
460b57cec5SDimitry Andric 
470b57cec5SDimitry Andric   WriteBitcodeToFile(*unwrap(M), OS);
480b57cec5SDimitry Andric   return wrap(MemoryBuffer::getMemBufferCopy(OS.str()).release());
490b57cec5SDimitry Andric }
50