2013-03-26 10:25:37 +08:00
|
|
|
//===---- IRReader.cpp - Reader for LLVM IR files -------------------------===//
|
|
|
|
//
|
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
|
2013-03-26 10:25:37 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/IRReader/IRReader.h"
|
2014-01-07 19:48:04 +08:00
|
|
|
#include "llvm-c/IRReader.h"
|
2014-01-07 20:34:26 +08:00
|
|
|
#include "llvm/AsmParser/Parser.h"
|
2016-11-11 13:34:58 +08:00
|
|
|
#include "llvm/Bitcode/BitcodeReader.h"
|
2013-11-06 17:21:15 +08:00
|
|
|
#include "llvm/IR/LLVMContext.h"
|
|
|
|
#include "llvm/IR/Module.h"
|
2013-03-26 10:25:37 +08:00
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
|
|
|
#include "llvm/Support/SourceMgr.h"
|
2013-04-03 23:33:45 +08:00
|
|
|
#include "llvm/Support/Timer.h"
|
2013-11-06 17:21:15 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2014-06-13 01:38:55 +08:00
|
|
|
#include <system_error>
|
2013-03-26 10:25:37 +08:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
2013-04-03 23:33:45 +08:00
|
|
|
namespace llvm {
|
|
|
|
extern bool TimePassesIsEnabled;
|
|
|
|
}
|
|
|
|
|
2016-11-19 03:43:18 +08:00
|
|
|
static const char *const TimeIRParsingGroupName = "irparse";
|
|
|
|
static const char *const TimeIRParsingGroupDescription = "LLVM IR Parsing";
|
|
|
|
static const char *const TimeIRParsingName = "parse";
|
|
|
|
static const char *const TimeIRParsingDescription = "Parse IR";
|
2013-04-03 23:33:45 +08:00
|
|
|
|
2019-02-12 06:01:13 +08:00
|
|
|
std::unique_ptr<Module>
|
|
|
|
llvm::getLazyIRModule(std::unique_ptr<MemoryBuffer> Buffer, SMDiagnostic &Err,
|
|
|
|
LLVMContext &Context, bool ShouldLazyLoadMetadata) {
|
2013-03-26 10:25:37 +08:00
|
|
|
if (isBitcode((const unsigned char *)Buffer->getBufferStart(),
|
|
|
|
(const unsigned char *)Buffer->getBufferEnd())) {
|
2016-11-13 15:00:17 +08:00
|
|
|
Expected<std::unique_ptr<Module>> ModuleOrErr = getOwningLazyBitcodeModule(
|
2015-12-18 01:14:09 +08:00
|
|
|
std::move(Buffer), Context, ShouldLazyLoadMetadata);
|
2016-11-13 15:00:17 +08:00
|
|
|
if (Error E = ModuleOrErr.takeError()) {
|
|
|
|
handleAllErrors(std::move(E), [&](ErrorInfoBase &EIB) {
|
|
|
|
Err = SMDiagnostic(Buffer->getBufferIdentifier(), SourceMgr::DK_Error,
|
|
|
|
EIB.message());
|
|
|
|
});
|
2014-04-15 14:32:26 +08:00
|
|
|
return nullptr;
|
2013-03-26 10:25:37 +08:00
|
|
|
}
|
2015-06-17 06:27:55 +08:00
|
|
|
return std::move(ModuleOrErr.get());
|
2013-03-26 10:25:37 +08:00
|
|
|
}
|
|
|
|
|
2014-08-27 05:49:01 +08:00
|
|
|
return parseAssembly(Buffer->getMemBufferRef(), Err, Context);
|
2013-03-26 10:25:37 +08:00
|
|
|
}
|
|
|
|
|
2014-08-27 01:29:46 +08:00
|
|
|
std::unique_ptr<Module> llvm::getLazyIRFileModule(StringRef Filename,
|
|
|
|
SMDiagnostic &Err,
|
2015-12-18 01:14:09 +08:00
|
|
|
LLVMContext &Context,
|
|
|
|
bool ShouldLazyLoadMetadata) {
|
2014-07-07 01:43:13 +08:00
|
|
|
ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr =
|
|
|
|
MemoryBuffer::getFileOrSTDIN(Filename);
|
|
|
|
if (std::error_code EC = FileOrErr.getError()) {
|
2013-03-26 10:25:37 +08:00
|
|
|
Err = SMDiagnostic(Filename, SourceMgr::DK_Error,
|
2014-07-07 01:43:13 +08:00
|
|
|
"Could not open input file: " + EC.message());
|
2014-04-15 14:32:26 +08:00
|
|
|
return nullptr;
|
2013-03-26 10:25:37 +08:00
|
|
|
}
|
|
|
|
|
2015-12-18 01:14:09 +08:00
|
|
|
return getLazyIRModule(std::move(FileOrErr.get()), Err, Context,
|
|
|
|
ShouldLazyLoadMetadata);
|
2013-03-26 10:25:37 +08:00
|
|
|
}
|
|
|
|
|
2014-08-27 05:49:01 +08:00
|
|
|
std::unique_ptr<Module> llvm::parseIR(MemoryBufferRef Buffer, SMDiagnostic &Err,
|
2017-10-03 02:31:29 +08:00
|
|
|
LLVMContext &Context,
|
2018-01-31 06:32:39 +08:00
|
|
|
bool UpgradeDebugInfo,
|
|
|
|
StringRef DataLayoutString) {
|
2016-11-19 03:43:18 +08:00
|
|
|
NamedRegionTimer T(TimeIRParsingName, TimeIRParsingDescription,
|
|
|
|
TimeIRParsingGroupName, TimeIRParsingGroupDescription,
|
2013-04-03 23:33:45 +08:00
|
|
|
TimePassesIsEnabled);
|
2014-08-27 05:49:01 +08:00
|
|
|
if (isBitcode((const unsigned char *)Buffer.getBufferStart(),
|
|
|
|
(const unsigned char *)Buffer.getBufferEnd())) {
|
2016-11-13 15:00:17 +08:00
|
|
|
Expected<std::unique_ptr<Module>> ModuleOrErr =
|
2015-06-17 06:27:55 +08:00
|
|
|
parseBitcodeFile(Buffer, Context);
|
2016-11-13 15:00:17 +08:00
|
|
|
if (Error E = ModuleOrErr.takeError()) {
|
|
|
|
handleAllErrors(std::move(E), [&](ErrorInfoBase &EIB) {
|
|
|
|
Err = SMDiagnostic(Buffer.getBufferIdentifier(), SourceMgr::DK_Error,
|
|
|
|
EIB.message());
|
|
|
|
});
|
2014-08-27 01:29:46 +08:00
|
|
|
return nullptr;
|
|
|
|
}
|
2018-01-31 06:32:39 +08:00
|
|
|
if (!DataLayoutString.empty())
|
|
|
|
ModuleOrErr.get()->setDataLayout(DataLayoutString);
|
2015-06-17 06:27:55 +08:00
|
|
|
return std::move(ModuleOrErr.get());
|
2013-03-26 10:25:37 +08:00
|
|
|
}
|
|
|
|
|
2018-01-31 06:32:39 +08:00
|
|
|
return parseAssembly(Buffer, Err, Context, nullptr, UpgradeDebugInfo,
|
|
|
|
DataLayoutString);
|
2013-03-26 10:25:37 +08:00
|
|
|
}
|
|
|
|
|
2014-08-27 01:29:46 +08:00
|
|
|
std::unique_ptr<Module> llvm::parseIRFile(StringRef Filename, SMDiagnostic &Err,
|
2017-10-03 02:31:29 +08:00
|
|
|
LLVMContext &Context,
|
2018-01-31 06:32:39 +08:00
|
|
|
bool UpgradeDebugInfo,
|
|
|
|
StringRef DataLayoutString) {
|
2014-07-07 01:43:13 +08:00
|
|
|
ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr =
|
|
|
|
MemoryBuffer::getFileOrSTDIN(Filename);
|
|
|
|
if (std::error_code EC = FileOrErr.getError()) {
|
2013-03-26 10:25:37 +08:00
|
|
|
Err = SMDiagnostic(Filename, SourceMgr::DK_Error,
|
2014-07-07 01:43:13 +08:00
|
|
|
"Could not open input file: " + EC.message());
|
2014-04-15 14:32:26 +08:00
|
|
|
return nullptr;
|
2013-03-26 10:25:37 +08:00
|
|
|
}
|
|
|
|
|
2017-10-03 02:31:29 +08:00
|
|
|
return parseIR(FileOrErr.get()->getMemBufferRef(), Err, Context,
|
2018-01-31 06:32:39 +08:00
|
|
|
UpgradeDebugInfo, DataLayoutString);
|
2013-03-26 10:25:37 +08:00
|
|
|
}
|
2013-11-06 17:21:15 +08:00
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// C API.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
LLVMBool LLVMParseIRInContext(LLVMContextRef ContextRef,
|
|
|
|
LLVMMemoryBufferRef MemBuf, LLVMModuleRef *OutM,
|
|
|
|
char **OutMessage) {
|
|
|
|
SMDiagnostic Diag;
|
|
|
|
|
2014-06-27 12:33:58 +08:00
|
|
|
std::unique_ptr<MemoryBuffer> MB(unwrap(MemBuf));
|
2014-08-27 05:49:01 +08:00
|
|
|
*OutM =
|
|
|
|
wrap(parseIR(MB->getMemBufferRef(), Diag, *unwrap(ContextRef)).release());
|
2013-11-06 17:21:15 +08:00
|
|
|
|
|
|
|
if(!*OutM) {
|
|
|
|
if (OutMessage) {
|
2014-06-27 06:52:05 +08:00
|
|
|
std::string buf;
|
|
|
|
raw_string_ostream os(buf);
|
|
|
|
|
2014-04-15 14:32:26 +08:00
|
|
|
Diag.print(nullptr, os, false);
|
2014-06-27 06:52:05 +08:00
|
|
|
os.flush();
|
|
|
|
|
|
|
|
*OutMessage = strdup(buf.c_str());
|
2013-11-06 17:21:15 +08:00
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|