2007-09-18 03:18:57 +00:00
|
|
|
//===-- BitWriter.cpp -----------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 20:36:04 +00:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2007-09-18 03:18:57 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm-c/BitWriter.h"
|
|
|
|
#include "llvm/Bitcode/ReaderWriter.h"
|
2013-05-01 20:59:00 +00:00
|
|
|
#include "llvm/IR/Module.h"
|
2014-04-29 23:26:49 +00:00
|
|
|
#include "llvm/Support/FileSystem.h"
|
2009-08-23 07:49:08 +00:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2007-09-18 03:18:57 +00:00
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
|
|
|
|
/*===-- Operations on modules ---------------------------------------------===*/
|
|
|
|
|
|
|
|
int LLVMWriteBitcodeToFile(LLVMModuleRef M, const char *Path) {
|
2014-08-25 18:16:47 +00:00
|
|
|
std::error_code EC;
|
|
|
|
raw_fd_ostream OS(Path, EC, sys::fs::F_None);
|
2012-11-25 15:23:39 +00:00
|
|
|
|
2014-08-25 18:16:47 +00:00
|
|
|
if (EC)
|
2007-09-18 03:18:57 +00:00
|
|
|
return -1;
|
2012-11-25 15:23:39 +00:00
|
|
|
|
2009-08-23 07:49:08 +00:00
|
|
|
WriteBitcodeToFile(unwrap(M), OS);
|
2007-09-18 03:18:57 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-03-06 00:30:06 +00:00
|
|
|
int LLVMWriteBitcodeToFD(LLVMModuleRef M, int FD, int ShouldClose,
|
|
|
|
int Unbuffered) {
|
|
|
|
raw_fd_ostream OS(FD, ShouldClose, Unbuffered);
|
2012-11-25 15:23:39 +00:00
|
|
|
|
2009-08-23 07:49:08 +00:00
|
|
|
WriteBitcodeToFile(unwrap(M), OS);
|
2007-09-18 03:18:57 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2010-03-06 00:30:06 +00:00
|
|
|
|
|
|
|
int LLVMWriteBitcodeToFileHandle(LLVMModuleRef M, int FileHandle) {
|
|
|
|
return LLVMWriteBitcodeToFD(M, FileHandle, true, false);
|
|
|
|
}
|
2014-10-14 00:30:59 +00:00
|
|
|
|
|
|
|
LLVMMemoryBufferRef LLVMWriteBitcodeToMemoryBuffer(LLVMModuleRef M) {
|
|
|
|
std::string Data;
|
|
|
|
raw_string_ostream OS(Data);
|
|
|
|
|
|
|
|
WriteBitcodeToFile(unwrap(M), OS);
|
|
|
|
return wrap(MemoryBuffer::getMemBufferCopy(OS.str()).release());
|
|
|
|
}
|