2007-12-11 08:20:48 +08:00
|
|
|
//===-- BitReader.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-12-11 08:20:48 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm-c/BitReader.h"
|
2015-12-18 09:46:52 +08:00
|
|
|
#include "llvm-c/Core.h"
|
2016-11-11 13:34:58 +08:00
|
|
|
#include "llvm/Bitcode/BitcodeReader.h"
|
2013-01-02 19:36:10 +08:00
|
|
|
#include "llvm/IR/LLVMContext.h"
|
2013-05-02 04:59:00 +08:00
|
|
|
#include "llvm/IR/Module.h"
|
2007-12-11 08:20:48 +08:00
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
2015-02-03 08:49:57 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2008-02-20 19:08:44 +08:00
|
|
|
#include <cstring>
|
2012-12-04 00:50:05 +08:00
|
|
|
#include <string>
|
2007-12-11 08:20:48 +08:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
2007-12-20 06:30:40 +08:00
|
|
|
/* Builds a module from the bitcode in the specified memory buffer, returning a
|
|
|
|
reference to the module via the OutModule parameter. Returns 0 on success.
|
2010-01-10 06:27:07 +08:00
|
|
|
Optionally returns a human-readable error message via OutMessage. */
|
2015-12-18 22:06:34 +08:00
|
|
|
LLVMBool LLVMParseBitcode(LLVMMemoryBufferRef MemBuf, LLVMModuleRef *OutModule,
|
|
|
|
char **OutMessage) {
|
2016-04-15 05:59:01 +08:00
|
|
|
return LLVMParseBitcodeInContext(LLVMGetGlobalContext(), MemBuf, OutModule,
|
2010-02-16 05:08:22 +08:00
|
|
|
OutMessage);
|
2009-07-02 15:17:57 +08:00
|
|
|
}
|
|
|
|
|
2015-12-19 07:46:42 +08:00
|
|
|
LLVMBool LLVMParseBitcode2(LLVMMemoryBufferRef MemBuf,
|
|
|
|
LLVMModuleRef *OutModule) {
|
2016-04-15 05:59:01 +08:00
|
|
|
return LLVMParseBitcodeInContext2(LLVMGetGlobalContext(), MemBuf, OutModule);
|
2015-12-19 07:46:42 +08:00
|
|
|
}
|
|
|
|
|
2010-01-10 06:27:07 +08:00
|
|
|
LLVMBool LLVMParseBitcodeInContext(LLVMContextRef ContextRef,
|
|
|
|
LLVMMemoryBufferRef MemBuf,
|
|
|
|
LLVMModuleRef *OutModule,
|
|
|
|
char **OutMessage) {
|
2015-02-03 08:49:57 +08:00
|
|
|
MemoryBufferRef Buf = unwrap(MemBuf)->getMemBufferRef();
|
|
|
|
LLVMContext &Ctx = *unwrap(ContextRef);
|
|
|
|
|
2016-11-13 15:00:17 +08:00
|
|
|
Expected<std::unique_ptr<Module>> ModuleOrErr = parseBitcodeFile(Buf, Ctx);
|
|
|
|
if (Error Err = ModuleOrErr.takeError()) {
|
|
|
|
std::string Message;
|
|
|
|
handleAllErrors(std::move(Err), [&](ErrorInfoBase &EIB) {
|
|
|
|
Message = EIB.message();
|
|
|
|
});
|
2015-12-15 07:17:03 +08:00
|
|
|
if (OutMessage)
|
2015-02-03 09:53:03 +08:00
|
|
|
*OutMessage = strdup(Message.c_str());
|
2015-12-18 22:06:34 +08:00
|
|
|
*OutModule = wrap((Module *)nullptr);
|
2007-12-11 08:20:48 +08:00
|
|
|
return 1;
|
|
|
|
}
|
2012-11-25 23:23:39 +08:00
|
|
|
|
2015-06-17 06:27:55 +08:00
|
|
|
*OutModule = wrap(ModuleOrErr.get().release());
|
2007-12-20 06:30:40 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-12-19 07:46:42 +08:00
|
|
|
LLVMBool LLVMParseBitcodeInContext2(LLVMContextRef ContextRef,
|
|
|
|
LLVMMemoryBufferRef MemBuf,
|
|
|
|
LLVMModuleRef *OutModule) {
|
|
|
|
MemoryBufferRef Buf = unwrap(MemBuf)->getMemBufferRef();
|
|
|
|
LLVMContext &Ctx = *unwrap(ContextRef);
|
|
|
|
|
2016-11-13 15:00:17 +08:00
|
|
|
ErrorOr<std::unique_ptr<Module>> ModuleOrErr =
|
|
|
|
expectedToErrorOrAndEmitErrors(Ctx, parseBitcodeFile(Buf, Ctx));
|
2015-12-19 07:46:42 +08:00
|
|
|
if (ModuleOrErr.getError()) {
|
|
|
|
*OutModule = wrap((Module *)nullptr);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
*OutModule = wrap(ModuleOrErr.get().release());
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-12-20 06:30:40 +08:00
|
|
|
/* Reads a module from the specified path, returning via the OutModule parameter
|
|
|
|
a module provider which performs lazy deserialization. Returns 0 on success.
|
2012-11-25 23:23:39 +08:00
|
|
|
Optionally returns a human-readable error message via OutMessage. */
|
2010-03-03 07:58:54 +08:00
|
|
|
LLVMBool LLVMGetBitcodeModuleInContext(LLVMContextRef ContextRef,
|
|
|
|
LLVMMemoryBufferRef MemBuf,
|
2015-12-18 22:06:34 +08:00
|
|
|
LLVMModuleRef *OutM, char **OutMessage) {
|
2015-12-18 21:58:05 +08:00
|
|
|
LLVMContext &Ctx = *unwrap(ContextRef);
|
2014-08-27 06:00:09 +08:00
|
|
|
std::unique_ptr<MemoryBuffer> Owner(unwrap(MemBuf));
|
2016-11-13 15:00:17 +08:00
|
|
|
Expected<std::unique_ptr<Module>> ModuleOrErr =
|
2016-11-08 14:03:43 +08:00
|
|
|
getOwningLazyBitcodeModule(std::move(Owner), Ctx);
|
2016-11-14 15:26:17 +08:00
|
|
|
// Release the buffer if we didn't take ownership of it since we never owned
|
|
|
|
// it anyway.
|
|
|
|
(void)Owner.release();
|
2012-11-25 23:23:39 +08:00
|
|
|
|
2016-11-13 15:00:17 +08:00
|
|
|
if (Error Err = ModuleOrErr.takeError()) {
|
|
|
|
std::string Message;
|
|
|
|
handleAllErrors(std::move(Err), [&](ErrorInfoBase &EIB) {
|
|
|
|
Message = EIB.message();
|
|
|
|
});
|
2007-12-11 08:20:48 +08:00
|
|
|
if (OutMessage)
|
2015-12-18 21:58:05 +08:00
|
|
|
*OutMessage = strdup(Message.c_str());
|
2016-11-13 15:00:17 +08:00
|
|
|
*OutM = wrap((Module *)nullptr);
|
2007-12-11 08:20:48 +08:00
|
|
|
return 1;
|
|
|
|
}
|
2012-11-25 23:23:39 +08:00
|
|
|
|
2015-06-17 06:27:55 +08:00
|
|
|
*OutM = wrap(ModuleOrErr.get().release());
|
2014-01-14 02:31:04 +08:00
|
|
|
|
2007-12-11 08:20:48 +08:00
|
|
|
return 0;
|
2010-03-03 07:58:54 +08:00
|
|
|
}
|
|
|
|
|
2015-12-19 07:46:42 +08:00
|
|
|
LLVMBool LLVMGetBitcodeModuleInContext2(LLVMContextRef ContextRef,
|
|
|
|
LLVMMemoryBufferRef MemBuf,
|
|
|
|
LLVMModuleRef *OutM) {
|
|
|
|
LLVMContext &Ctx = *unwrap(ContextRef);
|
|
|
|
std::unique_ptr<MemoryBuffer> Owner(unwrap(MemBuf));
|
|
|
|
|
2016-11-13 15:00:17 +08:00
|
|
|
ErrorOr<std::unique_ptr<Module>> ModuleOrErr = expectedToErrorOrAndEmitErrors(
|
|
|
|
Ctx, getOwningLazyBitcodeModule(std::move(Owner), Ctx));
|
2015-12-19 07:46:42 +08:00
|
|
|
Owner.release();
|
|
|
|
|
|
|
|
if (ModuleOrErr.getError()) {
|
|
|
|
*OutM = wrap((Module *)nullptr);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
*OutM = wrap(ModuleOrErr.get().release());
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-03-03 07:58:54 +08:00
|
|
|
LLVMBool LLVMGetBitcodeModule(LLVMMemoryBufferRef MemBuf, LLVMModuleRef *OutM,
|
|
|
|
char **OutMessage) {
|
|
|
|
return LLVMGetBitcodeModuleInContext(LLVMGetGlobalContext(), MemBuf, OutM,
|
|
|
|
OutMessage);
|
|
|
|
}
|
2015-12-19 07:46:42 +08:00
|
|
|
|
|
|
|
LLVMBool LLVMGetBitcodeModule2(LLVMMemoryBufferRef MemBuf,
|
|
|
|
LLVMModuleRef *OutM) {
|
|
|
|
return LLVMGetBitcodeModuleInContext2(LLVMGetGlobalContext(), MemBuf, OutM);
|
|
|
|
}
|