2013-03-26 03:25:37 +01: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"
|
|
|
|
#include "llvm/ADT/OwningPtr.h"
|
|
|
|
#include "llvm/Assembly/Parser.h"
|
|
|
|
#include "llvm/Bitcode/ReaderWriter.h"
|
2013-11-06 10:21:15 +01:00
|
|
|
#include "llvm/IR/LLVMContext.h"
|
|
|
|
#include "llvm/IR/Module.h"
|
2013-03-26 03:25:37 +01:00
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
|
|
|
#include "llvm/Support/SourceMgr.h"
|
|
|
|
#include "llvm/Support/system_error.h"
|
2013-04-03 17:33:45 +02:00
|
|
|
#include "llvm/Support/Timer.h"
|
2013-11-06 10:21:15 +01:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
#include "llvm-c/Core.h"
|
|
|
|
#include "llvm-c/IRReader.h"
|
2013-03-26 03:25:37 +01:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
2013-04-03 17:33:45 +02:00
|
|
|
namespace llvm {
|
|
|
|
extern bool TimePassesIsEnabled;
|
|
|
|
}
|
|
|
|
|
2013-07-16 03:17:10 +02:00
|
|
|
static const char *const TimeIRParsingGroupName = "LLVM IR Parsing";
|
|
|
|
static const char *const TimeIRParsingName = "Parse IR";
|
2013-04-03 17:33:45 +02:00
|
|
|
|
|
|
|
|
2013-03-26 03:25:37 +01:00
|
|
|
Module *llvm::getLazyIRModule(MemoryBuffer *Buffer, SMDiagnostic &Err,
|
2013-04-01 21:47:56 +02:00
|
|
|
LLVMContext &Context) {
|
2013-03-26 03:25:37 +01:00
|
|
|
if (isBitcode((const unsigned char *)Buffer->getBufferStart(),
|
|
|
|
(const unsigned char *)Buffer->getBufferEnd())) {
|
|
|
|
std::string ErrMsg;
|
|
|
|
Module *M = getLazyBitcodeModule(Buffer, Context, &ErrMsg);
|
|
|
|
if (M == 0) {
|
|
|
|
Err = SMDiagnostic(Buffer->getBufferIdentifier(), SourceMgr::DK_Error,
|
|
|
|
ErrMsg);
|
|
|
|
// ParseBitcodeFile does not take ownership of the Buffer in the
|
|
|
|
// case of an error.
|
|
|
|
delete Buffer;
|
|
|
|
}
|
|
|
|
return M;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ParseAssembly(Buffer, 0, Err, Context);
|
|
|
|
}
|
|
|
|
|
|
|
|
Module *llvm::getLazyIRFileModule(const std::string &Filename, SMDiagnostic &Err,
|
2013-04-01 21:47:56 +02:00
|
|
|
LLVMContext &Context) {
|
2013-03-26 03:25:37 +01:00
|
|
|
OwningPtr<MemoryBuffer> File;
|
2013-06-25 07:28:34 +02:00
|
|
|
if (error_code ec = MemoryBuffer::getFileOrSTDIN(Filename, File)) {
|
2013-03-26 03:25:37 +01:00
|
|
|
Err = SMDiagnostic(Filename, SourceMgr::DK_Error,
|
|
|
|
"Could not open input file: " + ec.message());
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return getLazyIRModule(File.take(), Err, Context);
|
|
|
|
}
|
|
|
|
|
|
|
|
Module *llvm::ParseIR(MemoryBuffer *Buffer, SMDiagnostic &Err,
|
|
|
|
LLVMContext &Context) {
|
2013-04-03 17:33:45 +02:00
|
|
|
NamedRegionTimer T(TimeIRParsingName, TimeIRParsingGroupName,
|
|
|
|
TimePassesIsEnabled);
|
2013-03-26 03:25:37 +01:00
|
|
|
if (isBitcode((const unsigned char *)Buffer->getBufferStart(),
|
|
|
|
(const unsigned char *)Buffer->getBufferEnd())) {
|
|
|
|
std::string ErrMsg;
|
|
|
|
Module *M = ParseBitcodeFile(Buffer, Context, &ErrMsg);
|
|
|
|
if (M == 0)
|
|
|
|
Err = SMDiagnostic(Buffer->getBufferIdentifier(), SourceMgr::DK_Error,
|
|
|
|
ErrMsg);
|
|
|
|
// ParseBitcodeFile does not take ownership of the Buffer.
|
|
|
|
delete Buffer;
|
|
|
|
return M;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ParseAssembly(Buffer, 0, Err, Context);
|
|
|
|
}
|
|
|
|
|
|
|
|
Module *llvm::ParseIRFile(const std::string &Filename, SMDiagnostic &Err,
|
|
|
|
LLVMContext &Context) {
|
|
|
|
OwningPtr<MemoryBuffer> File;
|
2013-06-25 07:28:34 +02:00
|
|
|
if (error_code ec = MemoryBuffer::getFileOrSTDIN(Filename, File)) {
|
2013-03-26 03:25:37 +01:00
|
|
|
Err = SMDiagnostic(Filename, SourceMgr::DK_Error,
|
|
|
|
"Could not open input file: " + ec.message());
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ParseIR(File.take(), Err, Context);
|
|
|
|
}
|
2013-11-06 10:21:15 +01: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;
|
|
|
|
}
|