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