2013-03-26 10:25:37 +08:00
|
|
|
//===---- IRReader.cpp - Reader for LLVM IR files -------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/IRReader/IRReader.h"
|
2014-01-07 19:48:04 +08:00
|
|
|
#include "llvm-c/Core.h"
|
|
|
|
#include "llvm-c/IRReader.h"
|
2014-01-07 20:34:26 +08:00
|
|
|
#include "llvm/AsmParser/Parser.h"
|
2013-03-26 10:25:37 +08:00
|
|
|
#include "llvm/Bitcode/ReaderWriter.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;
|
|
|
|
}
|
|
|
|
|
2013-07-16 09:17:10 +08:00
|
|
|
static const char *const TimeIRParsingGroupName = "LLVM IR Parsing";
|
|
|
|
static const char *const TimeIRParsingName = "Parse IR";
|
2013-04-03 23:33:45 +08:00
|
|
|
|
2014-08-20 00:58:54 +08:00
|
|
|
static std::unique_ptr<Module>
|
|
|
|
getLazyIRModule(std::unique_ptr<MemoryBuffer> Buffer, SMDiagnostic &Err,
|
|
|
|
LLVMContext &Context) {
|
2013-03-26 10:25:37 +08:00
|
|
|
if (isBitcode((const unsigned char *)Buffer->getBufferStart(),
|
|
|
|
(const unsigned char *)Buffer->getBufferEnd())) {
|
2015-06-17 06:27:55 +08:00
|
|
|
ErrorOr<std::unique_ptr<Module>> ModuleOrErr =
|
2014-09-04 01:31:46 +08:00
|
|
|
getLazyBitcodeModule(std::move(Buffer), Context);
|
2014-06-13 10:24:39 +08:00
|
|
|
if (std::error_code EC = ModuleOrErr.getError()) {
|
2013-03-26 10:25:37 +08:00
|
|
|
Err = SMDiagnostic(Buffer->getBufferIdentifier(), SourceMgr::DK_Error,
|
2014-01-14 02:31:04 +08:00
|
|
|
EC.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,
|
|
|
|
LLVMContext &Context) {
|
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
|
|
|
}
|
|
|
|
|
2014-08-27 01:29:46 +08:00
|
|
|
return getLazyIRModule(std::move(FileOrErr.get()), Err, Context);
|
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,
|
2014-08-27 01:29:46 +08:00
|
|
|
LLVMContext &Context) {
|
2013-04-03 23:33:45 +08:00
|
|
|
NamedRegionTimer T(TimeIRParsingName, TimeIRParsingGroupName,
|
|
|
|
TimePassesIsEnabled);
|
2014-08-27 05:49:01 +08:00
|
|
|
if (isBitcode((const unsigned char *)Buffer.getBufferStart(),
|
|
|
|
(const unsigned char *)Buffer.getBufferEnd())) {
|
2015-06-17 06:27:55 +08:00
|
|
|
ErrorOr<std::unique_ptr<Module>> ModuleOrErr =
|
|
|
|
parseBitcodeFile(Buffer, Context);
|
2014-08-27 01:29:46 +08:00
|
|
|
if (std::error_code EC = ModuleOrErr.getError()) {
|
2014-08-27 05:49:01 +08:00
|
|
|
Err = SMDiagnostic(Buffer.getBufferIdentifier(), SourceMgr::DK_Error,
|
2014-01-15 09:08:23 +08:00
|
|
|
EC.message());
|
2014-08-27 01:29:46 +08:00
|
|
|
return nullptr;
|
|
|
|
}
|
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, Err, Context);
|
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,
|
|
|
|
LLVMContext &Context) {
|
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
|
|
|
}
|
|
|
|
|
2014-08-27 05:49:01 +08:00
|
|
|
return parseIR(FileOrErr.get()->getMemBufferRef(), Err, Context);
|
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;
|
|
|
|
}
|