2007-09-18 03:18:57 +00:00
|
|
|
//===-- BitWriter.cpp -----------------------------------------------------===//
|
|
|
|
//
|
2019-01-19 08:50:56 +00:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2007-09-18 03:18:57 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm-c/BitWriter.h"
|
2016-11-11 05:34:58 +00:00
|
|
|
#include "llvm/Bitcode/BitcodeWriter.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"
|
2016-11-11 05:34:58 +00:00
|
|
|
#include "llvm/Support/MemoryBuffer.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;
|
2019-08-05 05:43:48 +00:00
|
|
|
raw_fd_ostream OS(Path, EC, sys::fs::OF_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
|
|
|
|
2018-02-14 19:11:32 +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
|
|
|
|
2018-02-14 19:11:32 +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);
|
|
|
|
|
2018-02-14 19:11:32 +00:00
|
|
|
WriteBitcodeToFile(*unwrap(M), OS);
|
2014-10-14 00:30:59 +00:00
|
|
|
return wrap(MemoryBuffer::getMemBufferCopy(OS.str()).release());
|
|
|
|
}
|