2007-09-18 11:18:57 +08:00
|
|
|
//===-- BitWriter.cpp -----------------------------------------------------===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08: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 11:18:57 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm-c/BitWriter.h"
|
2016-11-11 13:34:58 +08:00
|
|
|
#include "llvm/Bitcode/BitcodeWriter.h"
|
2013-05-02 04:59:00 +08:00
|
|
|
#include "llvm/IR/Module.h"
|
2014-04-30 07:26:49 +08:00
|
|
|
#include "llvm/Support/FileSystem.h"
|
2016-11-11 13:34:58 +08:00
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
2009-08-23 15:49:08 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2007-09-18 11:18:57 +08:00
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
|
|
|
|
/*===-- Operations on modules ---------------------------------------------===*/
|
|
|
|
|
|
|
|
int LLVMWriteBitcodeToFile(LLVMModuleRef M, const char *Path) {
|
2014-08-26 02:16:47 +08:00
|
|
|
std::error_code EC;
|
2019-08-05 13:43:48 +08:00
|
|
|
raw_fd_ostream OS(Path, EC, sys::fs::OF_None);
|
2012-11-25 23:23:39 +08:00
|
|
|
|
2014-08-26 02:16:47 +08:00
|
|
|
if (EC)
|
2007-09-18 11:18:57 +08:00
|
|
|
return -1;
|
2012-11-25 23:23:39 +08:00
|
|
|
|
2018-02-15 03:11:32 +08:00
|
|
|
WriteBitcodeToFile(*unwrap(M), OS);
|
2007-09-18 11:18:57 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-03-06 08:30:06 +08:00
|
|
|
int LLVMWriteBitcodeToFD(LLVMModuleRef M, int FD, int ShouldClose,
|
|
|
|
int Unbuffered) {
|
|
|
|
raw_fd_ostream OS(FD, ShouldClose, Unbuffered);
|
2012-11-25 23:23:39 +08:00
|
|
|
|
2018-02-15 03:11:32 +08:00
|
|
|
WriteBitcodeToFile(*unwrap(M), OS);
|
2007-09-18 11:18:57 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2010-03-06 08:30:06 +08:00
|
|
|
|
|
|
|
int LLVMWriteBitcodeToFileHandle(LLVMModuleRef M, int FileHandle) {
|
|
|
|
return LLVMWriteBitcodeToFD(M, FileHandle, true, false);
|
|
|
|
}
|
2014-10-14 08:30:59 +08:00
|
|
|
|
|
|
|
LLVMMemoryBufferRef LLVMWriteBitcodeToMemoryBuffer(LLVMModuleRef M) {
|
|
|
|
std::string Data;
|
|
|
|
raw_string_ostream OS(Data);
|
|
|
|
|
2018-02-15 03:11:32 +08:00
|
|
|
WriteBitcodeToFile(*unwrap(M), OS);
|
2014-10-14 08:30:59 +08:00
|
|
|
return wrap(MemoryBuffer::getMemBufferCopy(OS.str()).release());
|
|
|
|
}
|