2009-09-02 17:54:06 +00:00
|
|
|
//===---- llvm/Support/IRReader.h - Reader for LLVM IR files ----*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file defines functions for reading LLVM IR. They support both
|
|
|
|
// Bitcode and Assembly, automatically detecting the input format.
|
|
|
|
//
|
|
|
|
// These functions must be defined in a header file in order to avoid
|
|
|
|
// library dependencies, since they reference both Bitcode and Assembly
|
|
|
|
// functions.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef LLVM_SUPPORT_IRREADER_H
|
|
|
|
#define LLVM_SUPPORT_IRREADER_H
|
|
|
|
|
2010-12-16 03:29:14 +00:00
|
|
|
#include "llvm/ADT/OwningPtr.h"
|
2009-09-02 17:54:06 +00:00
|
|
|
#include "llvm/Assembly/Parser.h"
|
|
|
|
#include "llvm/Bitcode/ReaderWriter.h"
|
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
|
|
|
#include "llvm/Support/SourceMgr.h"
|
2010-12-09 17:36:48 +00:00
|
|
|
#include "llvm/Support/system_error.h"
|
2009-09-02 17:54:06 +00:00
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
|
2010-01-27 20:34:15 +00:00
|
|
|
/// If the given MemoryBuffer holds a bitcode image, return a Module for it
|
|
|
|
/// which does lazy deserialization of function bodies. Otherwise, attempt to
|
|
|
|
/// parse it as LLVM Assembly and return a fully populated Module. This
|
|
|
|
/// function *always* takes ownership of the given MemoryBuffer.
|
2010-01-29 21:11:04 +00:00
|
|
|
inline Module *getLazyIRModule(MemoryBuffer *Buffer,
|
|
|
|
SMDiagnostic &Err,
|
|
|
|
LLVMContext &Context) {
|
2009-09-02 17:54:06 +00:00
|
|
|
if (isBitcode((const unsigned char *)Buffer->getBufferStart(),
|
|
|
|
(const unsigned char *)Buffer->getBufferEnd())) {
|
|
|
|
std::string ErrMsg;
|
2010-01-27 20:34:15 +00:00
|
|
|
Module *M = getLazyBitcodeModule(Buffer, Context, &ErrMsg);
|
|
|
|
if (M == 0) {
|
2010-04-06 18:06:18 +00:00
|
|
|
Err = SMDiagnostic(Buffer->getBufferIdentifier(), ErrMsg);
|
2009-09-02 17:54:06 +00:00
|
|
|
// ParseBitcodeFile does not take ownership of the Buffer in the
|
|
|
|
// case of an error.
|
|
|
|
delete Buffer;
|
|
|
|
}
|
2010-01-27 20:34:15 +00:00
|
|
|
return M;
|
2009-09-02 17:54:06 +00:00
|
|
|
}
|
|
|
|
|
2010-01-27 20:34:15 +00:00
|
|
|
return ParseAssembly(Buffer, 0, Err, Context);
|
2009-09-02 17:54:06 +00:00
|
|
|
}
|
|
|
|
|
2010-01-27 20:34:15 +00:00
|
|
|
/// If the given file holds a bitcode image, return a Module
|
2009-09-02 17:54:06 +00:00
|
|
|
/// for it which does lazy deserialization of function bodies. Otherwise,
|
|
|
|
/// attempt to parse it as LLVM Assembly and return a fully populated
|
2010-01-27 20:34:15 +00:00
|
|
|
/// Module.
|
2010-01-29 21:11:04 +00:00
|
|
|
inline Module *getLazyIRFileModule(const std::string &Filename,
|
|
|
|
SMDiagnostic &Err,
|
|
|
|
LLVMContext &Context) {
|
2010-12-16 03:29:14 +00:00
|
|
|
OwningPtr<MemoryBuffer> File;
|
|
|
|
if (error_code ec = MemoryBuffer::getFileOrSTDIN(Filename.c_str(), File)) {
|
2010-12-09 17:36:48 +00:00
|
|
|
Err = SMDiagnostic(Filename,
|
|
|
|
"Could not open input file: " + ec.message());
|
2009-09-02 17:54:06 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-12-16 03:29:14 +00:00
|
|
|
return getLazyIRModule(File.take(), Err, Context);
|
2009-09-02 17:54:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// If the given MemoryBuffer holds a bitcode image, return a Module
|
|
|
|
/// for it. Otherwise, attempt to parse it as LLVM Assembly and return
|
|
|
|
/// a Module for it. This function *always* takes ownership of the given
|
|
|
|
/// MemoryBuffer.
|
|
|
|
inline Module *ParseIR(MemoryBuffer *Buffer,
|
|
|
|
SMDiagnostic &Err,
|
|
|
|
LLVMContext &Context) {
|
|
|
|
if (isBitcode((const unsigned char *)Buffer->getBufferStart(),
|
|
|
|
(const unsigned char *)Buffer->getBufferEnd())) {
|
|
|
|
std::string ErrMsg;
|
|
|
|
Module *M = ParseBitcodeFile(Buffer, Context, &ErrMsg);
|
|
|
|
if (M == 0)
|
2010-04-06 18:06:18 +00:00
|
|
|
Err = SMDiagnostic(Buffer->getBufferIdentifier(), ErrMsg);
|
2010-07-23 23:27:43 +00:00
|
|
|
// ParseBitcodeFile does not take ownership of the Buffer.
|
|
|
|
delete Buffer;
|
2009-09-02 17:54:06 +00:00
|
|
|
return M;
|
|
|
|
}
|
|
|
|
|
2009-09-08 22:20:35 +00:00
|
|
|
return ParseAssembly(Buffer, 0, Err, Context);
|
2009-09-02 17:54:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// If the given file holds a bitcode image, return a Module for it.
|
|
|
|
/// Otherwise, attempt to parse it as LLVM Assembly and return a Module
|
|
|
|
/// for it.
|
|
|
|
inline Module *ParseIRFile(const std::string &Filename,
|
|
|
|
SMDiagnostic &Err,
|
|
|
|
LLVMContext &Context) {
|
2010-12-16 03:29:14 +00:00
|
|
|
OwningPtr<MemoryBuffer> File;
|
|
|
|
if (error_code ec = MemoryBuffer::getFileOrSTDIN(Filename.c_str(), File)) {
|
2010-12-09 17:36:48 +00:00
|
|
|
Err = SMDiagnostic(Filename,
|
|
|
|
"Could not open input file: " + ec.message());
|
2009-09-02 17:54:06 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-12-16 03:29:14 +00:00
|
|
|
return ParseIR(File.take(), Err, Context);
|
2009-09-02 17:54:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|