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-01-07 19:48:04 +08:00
|
|
|
#include "llvm/Support/system_error.h"
|
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
|
|
|
|
|
|
|
|
2013-03-26 10:25:37 +08:00
|
|
|
Module *llvm::getLazyIRModule(MemoryBuffer *Buffer, SMDiagnostic &Err,
|
2013-04-02 03:47:56 +08:00
|
|
|
LLVMContext &Context) {
|
2013-03-26 10:25:37 +08:00
|
|
|
if (isBitcode((const unsigned char *)Buffer->getBufferStart(),
|
|
|
|
(const unsigned char *)Buffer->getBufferEnd())) {
|
|
|
|
std::string ErrMsg;
|
2014-01-14 02:31:04 +08:00
|
|
|
ErrorOr<Module *> ModuleOrErr = getLazyBitcodeModule(Buffer, Context);
|
|
|
|
if (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());
|
2013-03-26 10:25:37 +08:00
|
|
|
// ParseBitcodeFile does not take ownership of the Buffer in the
|
|
|
|
// case of an error.
|
|
|
|
delete Buffer;
|
2014-01-14 02:31:04 +08:00
|
|
|
return NULL;
|
2013-03-26 10:25:37 +08:00
|
|
|
}
|
2014-01-14 02:31:04 +08:00
|
|
|
return ModuleOrErr.get();
|
2013-03-26 10:25:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return ParseAssembly(Buffer, 0, Err, Context);
|
|
|
|
}
|
|
|
|
|
|
|
|
Module *llvm::getLazyIRFileModule(const std::string &Filename, SMDiagnostic &Err,
|
2013-04-02 03:47:56 +08:00
|
|
|
LLVMContext &Context) {
|
2014-03-06 13:51:42 +08:00
|
|
|
std::unique_ptr<MemoryBuffer> File;
|
2013-06-25 13:28:34 +08:00
|
|
|
if (error_code ec = MemoryBuffer::getFileOrSTDIN(Filename, File)) {
|
2013-03-26 10:25:37 +08:00
|
|
|
Err = SMDiagnostic(Filename, SourceMgr::DK_Error,
|
|
|
|
"Could not open input file: " + ec.message());
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-03-05 18:19:29 +08:00
|
|
|
return getLazyIRModule(File.release(), Err, Context);
|
2013-03-26 10:25:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Module *llvm::ParseIR(MemoryBuffer *Buffer, SMDiagnostic &Err,
|
|
|
|
LLVMContext &Context) {
|
2013-04-03 23:33:45 +08:00
|
|
|
NamedRegionTimer T(TimeIRParsingName, TimeIRParsingGroupName,
|
|
|
|
TimePassesIsEnabled);
|
2013-03-26 10:25:37 +08:00
|
|
|
if (isBitcode((const unsigned char *)Buffer->getBufferStart(),
|
|
|
|
(const unsigned char *)Buffer->getBufferEnd())) {
|
2014-01-15 09:08:23 +08:00
|
|
|
ErrorOr<Module *> ModuleOrErr = parseBitcodeFile(Buffer, Context);
|
|
|
|
Module *M = 0;
|
|
|
|
if (error_code EC = ModuleOrErr.getError())
|
2013-03-26 10:25:37 +08:00
|
|
|
Err = SMDiagnostic(Buffer->getBufferIdentifier(), SourceMgr::DK_Error,
|
2014-01-15 09:08:23 +08:00
|
|
|
EC.message());
|
|
|
|
else
|
|
|
|
M = ModuleOrErr.get();
|
|
|
|
// parseBitcodeFile does not take ownership of the Buffer.
|
2013-03-26 10:25:37 +08:00
|
|
|
delete Buffer;
|
|
|
|
return M;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ParseAssembly(Buffer, 0, Err, Context);
|
|
|
|
}
|
|
|
|
|
|
|
|
Module *llvm::ParseIRFile(const std::string &Filename, SMDiagnostic &Err,
|
|
|
|
LLVMContext &Context) {
|
2014-03-06 13:51:42 +08:00
|
|
|
std::unique_ptr<MemoryBuffer> File;
|
2013-06-25 13:28:34 +08:00
|
|
|
if (error_code ec = MemoryBuffer::getFileOrSTDIN(Filename, File)) {
|
2013-03-26 10:25:37 +08:00
|
|
|
Err = SMDiagnostic(Filename, SourceMgr::DK_Error,
|
|
|
|
"Could not open input file: " + ec.message());
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-03-05 18:19:29 +08:00
|
|
|
return ParseIR(File.release(), 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;
|
|
|
|
|
|
|
|
*OutM = wrap(ParseIR(unwrap(MemBuf), Diag, *unwrap(ContextRef)));
|
|
|
|
|
|
|
|
if(!*OutM) {
|
|
|
|
if (OutMessage) {
|
|
|
|
std::string buf;
|
|
|
|
raw_string_ostream os(buf);
|
|
|
|
|
|
|
|
Diag.print(NULL, os, false);
|
|
|
|
os.flush();
|
|
|
|
|
|
|
|
*OutMessage = strdup(buf.c_str());
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|