aboutsummaryrefslogtreecommitdiffstats
path: root/include/llvm
diff options
context:
space:
mode:
authorDan Gohman <gohman@apple.com>2009-09-02 17:54:06 +0000
committerDan Gohman <gohman@apple.com>2009-09-02 17:54:06 +0000
commit87750fcfc8d6ace324d0a237ae7da14d120fff34 (patch)
tree360ed12eb605dbadc279f812512173649b15c008 /include/llvm
parent3bd52827bd65bdb5d6b6440a5fce0747eb414a60 (diff)
downloadexternal_llvm-87750fcfc8d6ace324d0a237ae7da14d120fff34.zip
external_llvm-87750fcfc8d6ace324d0a237ae7da14d120fff34.tar.gz
external_llvm-87750fcfc8d6ace324d0a237ae7da14d120fff34.tar.bz2
Add convenience functions for reading in LLVM IR that autodetect
and LLVM Assembly and LLVM Bitcode and automatically call the corresponding reader. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@80809 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'include/llvm')
-rw-r--r--include/llvm/Support/IRReader.h117
1 files changed, 117 insertions, 0 deletions
diff --git a/include/llvm/Support/IRReader.h b/include/llvm/Support/IRReader.h
new file mode 100644
index 0000000..5670011
--- /dev/null
+++ b/include/llvm/Support/IRReader.h
@@ -0,0 +1,117 @@
+//===---- 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
+
+#include "llvm/Assembly/Parser.h"
+#include "llvm/Bitcode/ReaderWriter.h"
+#include "llvm/Support/MemoryBuffer.h"
+#include "llvm/Support/SourceMgr.h"
+#include "llvm/ModuleProvider.h"
+
+namespace llvm {
+
+ /// If the given MemoryBuffer holds a bitcode image, return a ModuleProvider
+ /// for it which does lazy deserialization of function bodies. Otherwise,
+ /// attempt to parse it as LLVM Assembly and return a fully populated
+ /// ModuleProvider. This function *always* takes ownership of the given
+ /// MemoryBuffer.
+ inline ModuleProvider *getIRModuleProvider(MemoryBuffer *Buffer,
+ const std::string &Filename,
+ SMDiagnostic &Err,
+ LLVMContext &Context) {
+ if (isBitcode((const unsigned char *)Buffer->getBufferStart(),
+ (const unsigned char *)Buffer->getBufferEnd())) {
+ std::string ErrMsg;
+ ModuleProvider *MP = getBitcodeModuleProvider(Buffer, Context, &ErrMsg);
+ if (MP == 0) {
+ Err = SMDiagnostic(Filename, -1, -1, ErrMsg, "");
+ // ParseBitcodeFile does not take ownership of the Buffer in the
+ // case of an error.
+ delete Buffer;
+ }
+ return MP;
+ }
+
+ Module *M = ParseAssembly(Buffer, Filename, 0, Err, Context);
+ if (M == 0)
+ return 0;
+ return new ExistingModuleProvider(M);
+ }
+
+ /// If the given file holds a bitcode image, return a ModuleProvider
+ /// for it which does lazy deserialization of function bodies. Otherwise,
+ /// attempt to parse it as LLVM Assembly and return a fully populated
+ /// ModuleProvider.
+ inline ModuleProvider *getIRFileModuleProvider(const std::string &Filename,
+ SMDiagnostic &Err,
+ LLVMContext &Context) {
+ std::string ErrMsg;
+ MemoryBuffer *F = MemoryBuffer::getFileOrSTDIN(Filename.c_str(), &ErrMsg);
+ if (F == 0) {
+ Err = SMDiagnostic(Filename, -1, -1,
+ "Could not open input file '" + Filename + "'", "");
+ return 0;
+ }
+
+ return getIRModuleProvider(F, Filename, Err, Context);
+ }
+
+ /// 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,
+ const std::string &Filename,
+ 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);
+ // ParseBitcodeFile does not take ownership of the Buffer.
+ delete Buffer;
+ if (M == 0)
+ Err = SMDiagnostic(Filename, -1, -1, ErrMsg, "");
+ return M;
+ }
+
+ return ParseAssembly(Buffer, Filename, 0, Err, Context);
+ }
+
+ /// 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) {
+ std::string ErrMsg;
+ MemoryBuffer *F = MemoryBuffer::getFileOrSTDIN(Filename.c_str(), &ErrMsg);
+ if (F == 0) {
+ Err = SMDiagnostic(Filename, -1, -1,
+ "Could not open input file '" + Filename + "'", "");
+ return 0;
+ }
+
+ return ParseIR(F, Filename, Err, Context);
+ }
+
+}
+
+#endif