2003-09-22 23:44:46 +00:00
|
|
|
//===- ReaderWrappers.cpp - Parse bytecode from file or buffer -----------===//
|
2005-04-21 21:44:41 +00:00
|
|
|
//
|
2003-10-20 19:43:21 +00:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file was developed by the LLVM research group and is distributed under
|
|
|
|
// the University of Illinois Open Source License. See LICENSE.TXT for details.
|
2005-04-21 21:44:41 +00:00
|
|
|
//
|
2003-10-20 19:43:21 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
2003-09-22 23:44:46 +00:00
|
|
|
//
|
|
|
|
// This file implements loading and parsing a bytecode file and parsing a
|
|
|
|
// bytecode module from a given buffer.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2004-06-29 23:24:14 +00:00
|
|
|
#include "llvm/Bytecode/Analyzer.h"
|
2003-11-19 16:06:55 +00:00
|
|
|
#include "llvm/Bytecode/Reader.h"
|
2004-06-29 23:24:14 +00:00
|
|
|
#include "Reader.h"
|
2003-10-18 05:54:18 +00:00
|
|
|
#include "llvm/Module.h"
|
|
|
|
#include "llvm/Instructions.h"
|
2004-09-01 22:55:40 +00:00
|
|
|
#include "llvm/ADT/StringExtras.h"
|
2004-12-13 18:25:27 +00:00
|
|
|
#include "llvm/System/MappedFile.h"
|
2003-12-29 21:35:05 +00:00
|
|
|
#include <cerrno>
|
2004-12-21 07:51:33 +00:00
|
|
|
#include <iostream>
|
2003-11-19 16:06:55 +00:00
|
|
|
using namespace llvm;
|
2003-11-11 22:41:34 +00:00
|
|
|
|
2003-10-18 05:54:18 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// BytecodeFileReader - Read from an mmap'able file descriptor.
|
|
|
|
//
|
|
|
|
|
2003-09-22 23:38:23 +00:00
|
|
|
namespace {
|
|
|
|
/// BytecodeFileReader - parses a bytecode file from a file
|
|
|
|
///
|
2004-06-29 23:24:14 +00:00
|
|
|
class BytecodeFileReader : public BytecodeReader {
|
2003-09-22 23:38:23 +00:00
|
|
|
private:
|
2004-12-13 18:25:27 +00:00
|
|
|
sys::MappedFile mapFile;
|
2003-09-22 23:38:23 +00:00
|
|
|
|
|
|
|
BytecodeFileReader(const BytecodeFileReader&); // Do not implement
|
2003-09-23 15:09:26 +00:00
|
|
|
void operator=(const BytecodeFileReader &BFR); // Do not implement
|
2003-09-22 23:38:23 +00:00
|
|
|
|
|
|
|
public:
|
2004-06-29 23:24:14 +00:00
|
|
|
BytecodeFileReader(const std::string &Filename, llvm::BytecodeHandler* H=0);
|
2003-09-22 23:38:23 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2004-06-29 23:24:14 +00:00
|
|
|
BytecodeFileReader::BytecodeFileReader(const std::string &Filename,
|
2005-04-21 21:44:41 +00:00
|
|
|
llvm::BytecodeHandler* H )
|
2004-06-29 23:24:14 +00:00
|
|
|
: BytecodeReader(H)
|
2004-12-13 18:25:27 +00:00
|
|
|
, mapFile( sys::Path(Filename))
|
2004-06-29 23:24:14 +00:00
|
|
|
{
|
2004-12-13 18:25:27 +00:00
|
|
|
mapFile.map();
|
|
|
|
unsigned char* buffer = reinterpret_cast<unsigned char*>(mapFile.base());
|
|
|
|
ParseBytecode(buffer, mapFile.size(), Filename);
|
2003-09-22 23:38:23 +00:00
|
|
|
}
|
|
|
|
|
2003-10-18 05:54:18 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// BytecodeBufferReader - Read from a memory buffer
|
|
|
|
//
|
2003-09-23 16:13:28 +00:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
/// BytecodeBufferReader - parses a bytecode file from a buffer
|
|
|
|
///
|
2004-06-29 23:24:14 +00:00
|
|
|
class BytecodeBufferReader : public BytecodeReader {
|
2003-09-23 16:13:28 +00:00
|
|
|
private:
|
|
|
|
const unsigned char *Buffer;
|
|
|
|
bool MustDelete;
|
|
|
|
|
|
|
|
BytecodeBufferReader(const BytecodeBufferReader&); // Do not implement
|
|
|
|
void operator=(const BytecodeBufferReader &BFR); // Do not implement
|
|
|
|
|
|
|
|
public:
|
|
|
|
BytecodeBufferReader(const unsigned char *Buf, unsigned Length,
|
2004-06-29 23:24:14 +00:00
|
|
|
const std::string &ModuleID,
|
2004-07-04 11:03:03 +00:00
|
|
|
llvm::BytecodeHandler* Handler = 0);
|
2003-09-23 16:13:28 +00:00
|
|
|
~BytecodeBufferReader();
|
|
|
|
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
BytecodeBufferReader::BytecodeBufferReader(const unsigned char *Buf,
|
2003-09-24 22:04:02 +00:00
|
|
|
unsigned Length,
|
2004-06-29 23:24:14 +00:00
|
|
|
const std::string &ModuleID,
|
2004-07-04 11:03:03 +00:00
|
|
|
llvm::BytecodeHandler* H )
|
2004-06-29 23:24:14 +00:00
|
|
|
: BytecodeReader(H)
|
2003-09-23 16:13:28 +00:00
|
|
|
{
|
|
|
|
// If not aligned, allocate a new buffer to hold the bytecode...
|
|
|
|
const unsigned char *ParseBegin = 0;
|
2004-08-04 22:56:46 +00:00
|
|
|
if (reinterpret_cast<uint64_t>(Buf) & 3) {
|
2003-09-24 22:04:02 +00:00
|
|
|
Buffer = new unsigned char[Length+4];
|
2003-09-24 22:34:17 +00:00
|
|
|
unsigned Offset = 4 - ((intptr_t)Buffer & 3); // Make sure it's aligned
|
2003-09-23 16:13:28 +00:00
|
|
|
ParseBegin = Buffer + Offset;
|
2003-09-24 22:04:02 +00:00
|
|
|
memcpy((unsigned char*)ParseBegin, Buf, Length); // Copy it over
|
2003-09-23 16:13:28 +00:00
|
|
|
MustDelete = true;
|
|
|
|
} else {
|
|
|
|
// If we don't need to copy it over, just use the caller's copy
|
2003-09-23 21:19:11 +00:00
|
|
|
ParseBegin = Buffer = Buf;
|
2003-09-23 16:13:28 +00:00
|
|
|
MustDelete = false;
|
|
|
|
}
|
2003-10-08 19:55:47 +00:00
|
|
|
try {
|
2004-08-21 20:52:03 +00:00
|
|
|
ParseBytecode(ParseBegin, Length, ModuleID);
|
2003-10-08 19:55:47 +00:00
|
|
|
} catch (...) {
|
|
|
|
if (MustDelete) delete [] Buffer;
|
|
|
|
throw;
|
|
|
|
}
|
2003-09-23 16:13:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
BytecodeBufferReader::~BytecodeBufferReader() {
|
|
|
|
if (MustDelete) delete [] Buffer;
|
|
|
|
}
|
|
|
|
|
2003-10-18 05:54:18 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// BytecodeStdinReader - Read bytecode from Standard Input
|
|
|
|
//
|
2003-09-23 16:13:28 +00:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
/// BytecodeStdinReader - parses a bytecode file from stdin
|
2005-04-21 21:44:41 +00:00
|
|
|
///
|
2004-06-29 23:24:14 +00:00
|
|
|
class BytecodeStdinReader : public BytecodeReader {
|
2003-09-23 16:13:28 +00:00
|
|
|
private:
|
|
|
|
std::vector<unsigned char> FileData;
|
|
|
|
unsigned char *FileBuf;
|
|
|
|
|
|
|
|
BytecodeStdinReader(const BytecodeStdinReader&); // Do not implement
|
|
|
|
void operator=(const BytecodeStdinReader &BFR); // Do not implement
|
|
|
|
|
|
|
|
public:
|
2004-06-29 23:24:14 +00:00
|
|
|
BytecodeStdinReader( llvm::BytecodeHandler* H = 0 );
|
2003-09-23 16:13:28 +00:00
|
|
|
};
|
|
|
|
}
|
2003-09-22 23:38:23 +00:00
|
|
|
|
2005-04-21 21:44:41 +00:00
|
|
|
BytecodeStdinReader::BytecodeStdinReader( BytecodeHandler* H )
|
2004-06-29 23:24:14 +00:00
|
|
|
: BytecodeReader(H)
|
|
|
|
{
|
2004-12-21 07:51:33 +00:00
|
|
|
char Buffer[4096*4];
|
2003-09-22 23:38:23 +00:00
|
|
|
|
|
|
|
// Read in all of the data from stdin, we cannot mmap stdin...
|
2004-12-21 07:51:33 +00:00
|
|
|
while (std::cin.good()) {
|
|
|
|
std::cin.read(Buffer, 4096*4);
|
|
|
|
int BlockSize = std::cin.gcount();
|
|
|
|
if (0 >= BlockSize)
|
|
|
|
break;
|
2003-09-22 23:38:23 +00:00
|
|
|
FileData.insert(FileData.end(), Buffer, Buffer+BlockSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (FileData.empty())
|
|
|
|
throw std::string("Standard Input empty!");
|
|
|
|
|
|
|
|
FileBuf = &FileData[0];
|
2004-08-21 20:52:03 +00:00
|
|
|
ParseBytecode(FileBuf, FileData.size(), "<stdin>");
|
2003-09-22 23:38:23 +00:00
|
|
|
}
|
|
|
|
|
2005-06-18 18:34:52 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Varargs transmogrification code...
|
|
|
|
//
|
|
|
|
|
|
|
|
// CheckVarargs - This is used to automatically translate old-style varargs to
|
|
|
|
// new style varargs for backwards compatibility.
|
|
|
|
static ModuleProvider* CheckVarargs(ModuleProvider* MP) {
|
|
|
|
Module* M = MP->getModule();
|
|
|
|
|
|
|
|
// check to see if va_start takes arguements...
|
|
|
|
Function* F = M->getNamedFunction("llvm.va_start");
|
|
|
|
if(F == 0) return MP; //No varargs use, just return.
|
|
|
|
|
|
|
|
if (F->getFunctionType()->getNumParams() == 1)
|
|
|
|
return MP; // Modern varargs processing, just return.
|
|
|
|
|
|
|
|
// If we get to this point, we know that we have an old-style module.
|
|
|
|
// Materialize the whole thing to perform the rewriting.
|
|
|
|
MP->materializeModule();
|
|
|
|
|
|
|
|
if(Function* F = M->getNamedFunction("llvm.va_start")) {
|
|
|
|
assert(F->arg_size() == 0 && "Obsolete va_start takes 0 argument!");
|
2005-07-27 06:12:32 +00:00
|
|
|
|
2005-06-18 18:34:52 +00:00
|
|
|
//foo = va_start()
|
|
|
|
// ->
|
|
|
|
//bar = alloca typeof(foo)
|
|
|
|
//va_start(bar)
|
|
|
|
//foo = load bar
|
2005-07-27 06:12:32 +00:00
|
|
|
|
2005-06-18 18:34:52 +00:00
|
|
|
const Type* RetTy = Type::getPrimitiveType(Type::VoidTyID);
|
|
|
|
const Type* ArgTy = F->getFunctionType()->getReturnType();
|
|
|
|
const Type* ArgTyPtr = PointerType::get(ArgTy);
|
2005-07-27 06:12:32 +00:00
|
|
|
Function* NF = M->getOrInsertFunction("llvm.va_start",
|
2005-06-18 18:34:52 +00:00
|
|
|
RetTy, ArgTyPtr, 0);
|
|
|
|
|
|
|
|
for(Value::use_iterator I = F->use_begin(), E = F->use_end(); I != E;)
|
|
|
|
if (CallInst* CI = dyn_cast<CallInst>(*I++)) {
|
|
|
|
AllocaInst* bar = new AllocaInst(ArgTy, 0, "vastart.fix.1", CI);
|
|
|
|
new CallInst(NF, bar, "", CI);
|
|
|
|
Value* foo = new LoadInst(bar, "vastart.fix.2", CI);
|
|
|
|
CI->replaceAllUsesWith(foo);
|
|
|
|
CI->getParent()->getInstList().erase(CI);
|
|
|
|
}
|
|
|
|
F->setName("");
|
|
|
|
}
|
2005-07-27 06:12:32 +00:00
|
|
|
|
2005-06-18 18:34:52 +00:00
|
|
|
if(Function* F = M->getNamedFunction("llvm.va_end")) {
|
|
|
|
assert(F->arg_size() == 1 && "Obsolete va_end takes 1 argument!");
|
|
|
|
//vaend foo
|
|
|
|
// ->
|
|
|
|
//bar = alloca 1 of typeof(foo)
|
|
|
|
//vaend bar
|
|
|
|
const Type* RetTy = Type::getPrimitiveType(Type::VoidTyID);
|
|
|
|
const Type* ArgTy = F->getFunctionType()->getParamType(0);
|
|
|
|
const Type* ArgTyPtr = PointerType::get(ArgTy);
|
2005-07-27 06:12:32 +00:00
|
|
|
Function* NF = M->getOrInsertFunction("llvm.va_end",
|
2005-06-18 18:34:52 +00:00
|
|
|
RetTy, ArgTyPtr, 0);
|
2005-07-27 06:12:32 +00:00
|
|
|
|
2005-06-18 18:34:52 +00:00
|
|
|
for(Value::use_iterator I = F->use_begin(), E = F->use_end(); I != E;)
|
|
|
|
if (CallInst* CI = dyn_cast<CallInst>(*I++)) {
|
|
|
|
AllocaInst* bar = new AllocaInst(ArgTy, 0, "vaend.fix.1", CI);
|
2005-06-19 14:04:55 +00:00
|
|
|
new StoreInst(CI->getOperand(1), bar, CI);
|
2005-06-18 18:34:52 +00:00
|
|
|
new CallInst(NF, bar, "", CI);
|
|
|
|
CI->getParent()->getInstList().erase(CI);
|
|
|
|
}
|
|
|
|
F->setName("");
|
|
|
|
}
|
2005-07-27 06:12:32 +00:00
|
|
|
|
2005-06-18 18:34:52 +00:00
|
|
|
if(Function* F = M->getNamedFunction("llvm.va_copy")) {
|
|
|
|
assert(F->arg_size() == 1 && "Obsolete va_copy takes 1 argument!");
|
|
|
|
//foo = vacopy(bar)
|
|
|
|
// ->
|
|
|
|
//a = alloca 1 of typeof(foo)
|
2005-06-22 21:04:42 +00:00
|
|
|
//b = alloca 1 of typeof(foo)
|
|
|
|
//store bar -> b
|
|
|
|
//vacopy(a, b)
|
2005-06-18 18:34:52 +00:00
|
|
|
//foo = load a
|
2005-07-27 06:12:32 +00:00
|
|
|
|
2005-06-18 18:34:52 +00:00
|
|
|
const Type* RetTy = Type::getPrimitiveType(Type::VoidTyID);
|
|
|
|
const Type* ArgTy = F->getFunctionType()->getReturnType();
|
|
|
|
const Type* ArgTyPtr = PointerType::get(ArgTy);
|
2005-07-27 06:12:32 +00:00
|
|
|
Function* NF = M->getOrInsertFunction("llvm.va_copy",
|
2005-06-22 21:04:42 +00:00
|
|
|
RetTy, ArgTyPtr, ArgTyPtr, 0);
|
2005-07-27 06:12:32 +00:00
|
|
|
|
2005-06-18 18:34:52 +00:00
|
|
|
for(Value::use_iterator I = F->use_begin(), E = F->use_end(); I != E;)
|
|
|
|
if (CallInst* CI = dyn_cast<CallInst>(*I++)) {
|
|
|
|
AllocaInst* a = new AllocaInst(ArgTy, 0, "vacopy.fix.1", CI);
|
2005-06-22 21:04:42 +00:00
|
|
|
AllocaInst* b = new AllocaInst(ArgTy, 0, "vacopy.fix.2", CI);
|
|
|
|
new StoreInst(CI->getOperand(1), b, CI);
|
|
|
|
new CallInst(NF, a, b, "", CI);
|
|
|
|
Value* foo = new LoadInst(a, "vacopy.fix.3", CI);
|
2005-06-18 18:34:52 +00:00
|
|
|
CI->replaceAllUsesWith(foo);
|
|
|
|
CI->getParent()->getInstList().erase(CI);
|
|
|
|
}
|
|
|
|
F->setName("");
|
|
|
|
}
|
|
|
|
return MP;
|
|
|
|
}
|
|
|
|
|
2003-10-18 05:54:18 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
2003-09-23 16:13:28 +00:00
|
|
|
// Wrapper functions
|
2003-10-18 05:54:18 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
2003-09-23 16:13:28 +00:00
|
|
|
|
|
|
|
/// getBytecodeBufferModuleProvider - lazy function-at-a-time loading from a
|
|
|
|
/// buffer
|
2005-04-21 21:44:41 +00:00
|
|
|
ModuleProvider*
|
2003-11-19 16:06:55 +00:00
|
|
|
llvm::getBytecodeBufferModuleProvider(const unsigned char *Buffer,
|
|
|
|
unsigned Length,
|
2004-06-29 23:24:14 +00:00
|
|
|
const std::string &ModuleID,
|
2004-07-04 11:03:03 +00:00
|
|
|
BytecodeHandler* H ) {
|
2005-06-18 18:34:52 +00:00
|
|
|
return CheckVarargs(
|
|
|
|
new BytecodeBufferReader(Buffer, Length, ModuleID, H));
|
2003-09-22 23:38:23 +00:00
|
|
|
}
|
|
|
|
|
2003-09-23 16:13:28 +00:00
|
|
|
/// ParseBytecodeBuffer - Parse a given bytecode buffer
|
|
|
|
///
|
2003-11-19 16:06:55 +00:00
|
|
|
Module *llvm::ParseBytecodeBuffer(const unsigned char *Buffer, unsigned Length,
|
|
|
|
const std::string &ModuleID,
|
|
|
|
std::string *ErrorStr){
|
2003-09-23 16:13:28 +00:00
|
|
|
try {
|
2003-10-04 20:14:59 +00:00
|
|
|
std::auto_ptr<ModuleProvider>
|
2003-10-04 19:19:37 +00:00
|
|
|
AMP(getBytecodeBufferModuleProvider(Buffer, Length, ModuleID));
|
|
|
|
return AMP->releaseModule();
|
2003-09-23 16:13:28 +00:00
|
|
|
} catch (std::string &err) {
|
2003-09-24 22:10:47 +00:00
|
|
|
if (ErrorStr) *ErrorStr = err;
|
2003-09-23 16:13:28 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2003-09-22 23:38:23 +00:00
|
|
|
}
|
|
|
|
|
2003-09-23 16:13:28 +00:00
|
|
|
/// getBytecodeModuleProvider - lazy function-at-a-time loading from a file
|
2003-09-22 23:38:23 +00:00
|
|
|
///
|
2004-06-29 23:24:14 +00:00
|
|
|
ModuleProvider *llvm::getBytecodeModuleProvider(const std::string &Filename,
|
2004-07-04 11:03:03 +00:00
|
|
|
BytecodeHandler* H) {
|
2003-09-22 23:38:23 +00:00
|
|
|
if (Filename != std::string("-")) // Read from a file...
|
2005-06-18 18:34:52 +00:00
|
|
|
return CheckVarargs(new BytecodeFileReader(Filename,H));
|
2003-09-22 23:38:23 +00:00
|
|
|
else // Read from stdin
|
2005-06-18 18:34:52 +00:00
|
|
|
return CheckVarargs(new BytecodeStdinReader(H));
|
2003-09-22 23:38:23 +00:00
|
|
|
}
|
|
|
|
|
2003-09-23 16:13:28 +00:00
|
|
|
/// ParseBytecodeFile - Parse the given bytecode file
|
|
|
|
///
|
2003-11-19 16:06:55 +00:00
|
|
|
Module *llvm::ParseBytecodeFile(const std::string &Filename,
|
|
|
|
std::string *ErrorStr) {
|
2003-09-23 16:13:28 +00:00
|
|
|
try {
|
2003-10-04 20:14:59 +00:00
|
|
|
std::auto_ptr<ModuleProvider> AMP(getBytecodeModuleProvider(Filename));
|
2003-10-04 19:19:37 +00:00
|
|
|
return AMP->releaseModule();
|
2003-09-23 16:13:28 +00:00
|
|
|
} catch (std::string &err) {
|
2003-09-24 22:10:47 +00:00
|
|
|
if (ErrorStr) *ErrorStr = err;
|
2003-09-23 16:13:28 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2003-09-22 23:38:23 +00:00
|
|
|
}
|
2003-11-11 22:41:34 +00:00
|
|
|
|
2004-06-29 23:24:14 +00:00
|
|
|
// AnalyzeBytecodeFile - analyze one file
|
2004-08-21 20:52:03 +00:00
|
|
|
Module* llvm::AnalyzeBytecodeFile(
|
|
|
|
const std::string &Filename, ///< File to analyze
|
|
|
|
BytecodeAnalysis& bca, ///< Statistical output
|
|
|
|
std::string *ErrorStr, ///< Error output
|
2004-09-12 20:56:38 +00:00
|
|
|
std::ostream* output ///< Dump output
|
|
|
|
)
|
2004-06-29 23:24:14 +00:00
|
|
|
{
|
|
|
|
try {
|
2004-09-12 20:47:33 +00:00
|
|
|
BytecodeHandler* analyzerHandler =createBytecodeAnalyzerHandler(bca,output);
|
2004-06-29 23:24:14 +00:00
|
|
|
std::auto_ptr<ModuleProvider> AMP(
|
|
|
|
getBytecodeModuleProvider(Filename,analyzerHandler));
|
|
|
|
return AMP->releaseModule();
|
|
|
|
} catch (std::string &err) {
|
|
|
|
if (ErrorStr) *ErrorStr = err;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// AnalyzeBytecodeBuffer - analyze a buffer
|
|
|
|
Module* llvm::AnalyzeBytecodeBuffer(
|
2004-08-21 20:52:03 +00:00
|
|
|
const unsigned char* Buffer, ///< Pointer to start of bytecode buffer
|
|
|
|
unsigned Length, ///< Size of the bytecode buffer
|
|
|
|
const std::string& ModuleID, ///< Identifier for the module
|
|
|
|
BytecodeAnalysis& bca, ///< The results of the analysis
|
|
|
|
std::string* ErrorStr, ///< Errors, if any.
|
2004-09-12 20:56:38 +00:00
|
|
|
std::ostream* output ///< Dump output, if any
|
|
|
|
)
|
2004-06-29 23:24:14 +00:00
|
|
|
{
|
|
|
|
try {
|
2004-08-21 20:52:03 +00:00
|
|
|
BytecodeHandler* hdlr = createBytecodeAnalyzerHandler(bca, output);
|
2004-06-29 23:24:14 +00:00
|
|
|
std::auto_ptr<ModuleProvider>
|
|
|
|
AMP(getBytecodeBufferModuleProvider(Buffer, Length, ModuleID, hdlr));
|
|
|
|
return AMP->releaseModule();
|
|
|
|
} catch (std::string &err) {
|
|
|
|
if (ErrorStr) *ErrorStr = err;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-04-21 21:44:41 +00:00
|
|
|
bool llvm::GetBytecodeDependentLibraries(const std::string &fname,
|
2004-09-12 20:47:33 +00:00
|
|
|
Module::LibraryListType& deplibs) {
|
2004-08-24 22:46:20 +00:00
|
|
|
try {
|
|
|
|
std::auto_ptr<ModuleProvider> AMP( getBytecodeModuleProvider(fname));
|
|
|
|
Module* M = AMP->releaseModule();
|
2004-11-06 08:56:40 +00:00
|
|
|
|
2004-08-24 22:46:20 +00:00
|
|
|
deplibs = M->getLibraries();
|
|
|
|
delete M;
|
|
|
|
return true;
|
|
|
|
} catch (...) {
|
|
|
|
deplibs.clear();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-02-13 17:42:11 +00:00
|
|
|
static void getSymbols(Module*M, std::vector<std::string>& symbols) {
|
2004-11-14 22:00:48 +00:00
|
|
|
// Loop over global variables
|
2005-03-15 04:54:21 +00:00
|
|
|
for (Module::global_iterator GI = M->global_begin(), GE=M->global_end(); GI != GE; ++GI)
|
2005-02-13 18:12:20 +00:00
|
|
|
if (!GI->isExternal() && !GI->hasInternalLinkage())
|
2005-02-13 17:42:11 +00:00
|
|
|
if (!GI->getName().empty())
|
|
|
|
symbols.push_back(GI->getName());
|
|
|
|
|
|
|
|
// Loop over functions.
|
|
|
|
for (Module::iterator FI = M->begin(), FE = M->end(); FI != FE; ++FI)
|
|
|
|
if (!FI->isExternal() && !FI->hasInternalLinkage())
|
|
|
|
if (!FI->getName().empty())
|
|
|
|
symbols.push_back(FI->getName());
|
2004-11-14 22:00:48 +00:00
|
|
|
}
|
|
|
|
|
2004-11-06 08:56:40 +00:00
|
|
|
// Get just the externally visible defined symbols from the bytecode
|
|
|
|
bool llvm::GetBytecodeSymbols(const sys::Path& fName,
|
|
|
|
std::vector<std::string>& symbols) {
|
|
|
|
try {
|
2005-04-21 21:44:41 +00:00
|
|
|
std::auto_ptr<ModuleProvider> AMP(
|
2004-12-11 00:14:15 +00:00
|
|
|
getBytecodeModuleProvider(fName.toString()));
|
2004-11-06 08:56:40 +00:00
|
|
|
|
|
|
|
// Get the module from the provider
|
2004-11-16 06:41:05 +00:00
|
|
|
Module* M = AMP->materializeModule();
|
2004-11-06 08:56:40 +00:00
|
|
|
|
2004-11-14 22:00:48 +00:00
|
|
|
// Get the symbols
|
|
|
|
getSymbols(M, symbols);
|
2004-11-06 08:56:40 +00:00
|
|
|
|
|
|
|
// Done with the module
|
|
|
|
return true;
|
|
|
|
|
|
|
|
} catch (...) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-04-21 21:44:41 +00:00
|
|
|
ModuleProvider*
|
2004-11-15 01:20:11 +00:00
|
|
|
llvm::GetBytecodeSymbols(const unsigned char*Buffer, unsigned Length,
|
2004-11-16 06:41:05 +00:00
|
|
|
const std::string& ModuleID,
|
|
|
|
std::vector<std::string>& symbols) {
|
2004-11-14 22:00:48 +00:00
|
|
|
|
2004-11-16 06:41:05 +00:00
|
|
|
ModuleProvider* MP = 0;
|
2004-11-14 22:00:48 +00:00
|
|
|
try {
|
2004-11-16 06:41:05 +00:00
|
|
|
// Get the module provider
|
|
|
|
MP = getBytecodeBufferModuleProvider(Buffer, Length, ModuleID);
|
2004-11-14 22:00:48 +00:00
|
|
|
|
|
|
|
// Get the module from the provider
|
2004-11-15 01:20:11 +00:00
|
|
|
Module* M = MP->materializeModule();
|
2004-11-14 22:00:48 +00:00
|
|
|
|
|
|
|
// Get the symbols
|
|
|
|
getSymbols(M, symbols);
|
|
|
|
|
2004-11-16 06:41:05 +00:00
|
|
|
// Done with the module. Note that ModuleProvider will delete the
|
|
|
|
// Module when it is deleted. Also note that its the caller's responsibility
|
|
|
|
// to delete the ModuleProvider.
|
2004-11-15 01:20:11 +00:00
|
|
|
return MP;
|
2004-11-14 22:00:48 +00:00
|
|
|
|
|
|
|
} catch (...) {
|
2004-11-22 02:58:47 +00:00
|
|
|
// We delete only the ModuleProvider here because its destructor will
|
2004-11-16 06:41:05 +00:00
|
|
|
// also delete the Module (we used materializeModule not releaseModule).
|
|
|
|
delete MP;
|
2004-11-14 22:00:48 +00:00
|
|
|
}
|
2004-11-15 01:20:11 +00:00
|
|
|
return 0;
|
2004-11-14 22:00:48 +00:00
|
|
|
}
|