mirror of
https://github.com/c64scene-ar/llvm-6502.git
synced 2025-01-10 02:36:06 +00:00
f0356fe140
Modules and ModuleProviders. Because the "ModuleProvider" simply materializes GlobalValues now, and doesn't provide modules, it's renamed to "GVMaterializer". Code that used to need a ModuleProvider to materialize Functions can now materialize the Functions directly. Functions no longer use a magic linkage to record that they're materializable; they simply ask the GVMaterializer. Because the C ABI must never change, we can't remove LLVMModuleProviderRef or the functions that refer to it. Instead, because Module now exposes the same functionality ModuleProvider used to, we store a Module* in any LLVMModuleProviderRef and translate in the wrapper methods. The bindings to other languages still use the ModuleProvider concept. It would probably be worth some time to update them to follow the C++ more closely, but I don't intend to do it. Fixes http://llvm.org/PR5737 and http://llvm.org/PR5735. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@94686 91177308-0d34-0410-b5e6-96231b3b80d8
111 lines
4.1 KiB
C++
111 lines
4.1 KiB
C++
//===---- 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"
|
|
|
|
namespace llvm {
|
|
|
|
/// 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.
|
|
inline Module *getIRModule(MemoryBuffer *Buffer,
|
|
SMDiagnostic &Err,
|
|
LLVMContext &Context) {
|
|
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(), -1, -1, 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);
|
|
}
|
|
|
|
/// If the given file 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.
|
|
inline Module *getIRFileModule(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 getIRModule(F, 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,
|
|
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(Buffer->getBufferIdentifier(), -1, -1, ErrMsg, "");
|
|
return M;
|
|
}
|
|
|
|
return ParseAssembly(Buffer, 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, Err, Context);
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|