2003-09-23 07:44:46 +08:00
|
|
|
//===- ReaderWrappers.cpp - Parse bytecode from file or buffer -----------===//
|
2005-04-22 05:44:41 +08:00
|
|
|
//
|
2003-10-21 03:43:21 +08: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-22 05:44:41 +08:00
|
|
|
//
|
2003-10-21 03:43:21 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2003-09-23 07:44:46 +08:00
|
|
|
//
|
|
|
|
// This file implements loading and parsing a bytecode file and parsing a
|
|
|
|
// bytecode module from a given buffer.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2004-06-30 07:24:14 +08:00
|
|
|
#include "llvm/Bytecode/Analyzer.h"
|
2003-11-20 00:06:55 +08:00
|
|
|
#include "llvm/Bytecode/Reader.h"
|
2004-06-30 07:24:14 +08:00
|
|
|
#include "Reader.h"
|
2003-10-18 13:54:18 +08:00
|
|
|
#include "llvm/Module.h"
|
|
|
|
#include "llvm/Instructions.h"
|
2004-09-02 06:55:40 +08:00
|
|
|
#include "llvm/ADT/StringExtras.h"
|
2004-12-14 02:25:27 +08:00
|
|
|
#include "llvm/System/MappedFile.h"
|
2006-06-08 07:18:34 +08:00
|
|
|
#include "llvm/System/Program.h"
|
2003-12-30 05:35:05 +08:00
|
|
|
#include <cerrno>
|
2004-12-21 15:51:33 +08:00
|
|
|
#include <iostream>
|
2005-12-26 22:23:22 +08:00
|
|
|
#include <memory>
|
|
|
|
|
2003-11-20 00:06:55 +08:00
|
|
|
using namespace llvm;
|
2003-11-12 06:41:34 +08:00
|
|
|
|
2003-10-18 13:54:18 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// BytecodeFileReader - Read from an mmap'able file descriptor.
|
|
|
|
//
|
|
|
|
|
2003-09-23 07:38:23 +08:00
|
|
|
namespace {
|
|
|
|
/// BytecodeFileReader - parses a bytecode file from a file
|
|
|
|
///
|
2004-06-30 07:24:14 +08:00
|
|
|
class BytecodeFileReader : public BytecodeReader {
|
2003-09-23 07:38:23 +08:00
|
|
|
private:
|
2006-08-26 01:43:11 +08:00
|
|
|
std::string fileName;
|
2004-12-14 02:25:27 +08:00
|
|
|
sys::MappedFile mapFile;
|
2003-09-23 07:38:23 +08:00
|
|
|
|
|
|
|
BytecodeFileReader(const BytecodeFileReader&); // Do not implement
|
2003-09-23 23:09:26 +08:00
|
|
|
void operator=(const BytecodeFileReader &BFR); // Do not implement
|
2003-09-23 07:38:23 +08:00
|
|
|
|
|
|
|
public:
|
2004-06-30 07:24:14 +08:00
|
|
|
BytecodeFileReader(const std::string &Filename, llvm::BytecodeHandler* H=0);
|
2006-08-26 01:43:11 +08:00
|
|
|
bool read(std::string* ErrMsg);
|
2003-09-23 07:38:23 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2004-06-30 07:24:14 +08:00
|
|
|
BytecodeFileReader::BytecodeFileReader(const std::string &Filename,
|
2005-04-22 05:44:41 +08:00
|
|
|
llvm::BytecodeHandler* H )
|
2004-06-30 07:24:14 +08:00
|
|
|
: BytecodeReader(H)
|
2006-08-26 01:43:11 +08:00
|
|
|
, fileName(Filename)
|
2006-08-23 00:10:12 +08:00
|
|
|
, mapFile()
|
2004-06-30 07:24:14 +08:00
|
|
|
{
|
2006-08-26 01:43:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool BytecodeFileReader::read(std::string* ErrMsg) {
|
|
|
|
if (mapFile.open(sys::Path(fileName), sys::MappedFile::READ_ACCESS, ErrMsg))
|
|
|
|
return true;
|
|
|
|
if (!mapFile.map(ErrMsg)) {
|
|
|
|
mapFile.close();
|
|
|
|
return true;
|
|
|
|
}
|
2004-12-14 02:25:27 +08:00
|
|
|
unsigned char* buffer = reinterpret_cast<unsigned char*>(mapFile.base());
|
2006-08-26 01:43:11 +08:00
|
|
|
if (ParseBytecode(buffer, mapFile.size(), fileName, ErrMsg)) {
|
|
|
|
return true;
|
2006-08-23 00:10:12 +08:00
|
|
|
}
|
2006-08-26 01:43:11 +08:00
|
|
|
return false;
|
2003-09-23 07:38:23 +08:00
|
|
|
}
|
|
|
|
|
2003-10-18 13:54:18 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// BytecodeBufferReader - Read from a memory buffer
|
|
|
|
//
|
2003-09-24 00:13:28 +08:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
/// BytecodeBufferReader - parses a bytecode file from a buffer
|
|
|
|
///
|
2004-06-30 07:24:14 +08:00
|
|
|
class BytecodeBufferReader : public BytecodeReader {
|
2003-09-24 00:13:28 +08:00
|
|
|
private:
|
|
|
|
const unsigned char *Buffer;
|
2006-08-26 01:43:11 +08:00
|
|
|
const unsigned char *Buf;
|
|
|
|
unsigned Length;
|
|
|
|
std::string ModuleID;
|
2003-09-24 00:13:28 +08:00
|
|
|
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-30 07:24:14 +08:00
|
|
|
const std::string &ModuleID,
|
2004-07-04 19:03:03 +08:00
|
|
|
llvm::BytecodeHandler* Handler = 0);
|
2003-09-24 00:13:28 +08:00
|
|
|
~BytecodeBufferReader();
|
|
|
|
|
2006-08-26 01:43:11 +08:00
|
|
|
bool read(std::string* ErrMsg);
|
|
|
|
|
2003-09-24 00:13:28 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2006-08-26 01:43:11 +08:00
|
|
|
BytecodeBufferReader::BytecodeBufferReader(const unsigned char *buf,
|
|
|
|
unsigned len,
|
|
|
|
const std::string &modID,
|
|
|
|
llvm::BytecodeHandler* H)
|
2004-06-30 07:24:14 +08:00
|
|
|
: BytecodeReader(H)
|
2006-08-26 01:43:11 +08:00
|
|
|
, Buffer(0)
|
|
|
|
, Buf(buf)
|
|
|
|
, Length(len)
|
|
|
|
, ModuleID(modID)
|
|
|
|
, MustDelete(false)
|
2003-09-24 00:13:28 +08:00
|
|
|
{
|
2006-08-26 01:43:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
BytecodeBufferReader::~BytecodeBufferReader() {
|
|
|
|
if (MustDelete) delete [] Buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
BytecodeBufferReader::read(std::string* ErrMsg) {
|
2003-09-24 00:13:28 +08:00
|
|
|
// If not aligned, allocate a new buffer to hold the bytecode...
|
|
|
|
const unsigned char *ParseBegin = 0;
|
2004-08-05 06:56:46 +08:00
|
|
|
if (reinterpret_cast<uint64_t>(Buf) & 3) {
|
2003-09-25 06:04:02 +08:00
|
|
|
Buffer = new unsigned char[Length+4];
|
2003-09-25 06:34:17 +08:00
|
|
|
unsigned Offset = 4 - ((intptr_t)Buffer & 3); // Make sure it's aligned
|
2003-09-24 00:13:28 +08:00
|
|
|
ParseBegin = Buffer + Offset;
|
2003-09-25 06:04:02 +08:00
|
|
|
memcpy((unsigned char*)ParseBegin, Buf, Length); // Copy it over
|
2003-09-24 00:13:28 +08:00
|
|
|
MustDelete = true;
|
|
|
|
} else {
|
|
|
|
// If we don't need to copy it over, just use the caller's copy
|
2003-09-24 05:19:11 +08:00
|
|
|
ParseBegin = Buffer = Buf;
|
2003-09-24 00:13:28 +08:00
|
|
|
MustDelete = false;
|
|
|
|
}
|
2006-08-26 01:43:11 +08:00
|
|
|
if (ParseBytecode(ParseBegin, Length, ModuleID, ErrMsg)) {
|
2003-10-09 03:55:47 +08:00
|
|
|
if (MustDelete) delete [] Buffer;
|
2006-08-26 01:43:11 +08:00
|
|
|
return true;
|
2003-10-09 03:55:47 +08:00
|
|
|
}
|
2006-08-26 01:43:11 +08:00
|
|
|
return false;
|
2003-09-24 00:13:28 +08:00
|
|
|
}
|
|
|
|
|
2003-10-18 13:54:18 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// BytecodeStdinReader - Read bytecode from Standard Input
|
|
|
|
//
|
2003-09-24 00:13:28 +08:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
/// BytecodeStdinReader - parses a bytecode file from stdin
|
2005-04-22 05:44:41 +08:00
|
|
|
///
|
2004-06-30 07:24:14 +08:00
|
|
|
class BytecodeStdinReader : public BytecodeReader {
|
2003-09-24 00:13:28 +08: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-30 07:24:14 +08:00
|
|
|
BytecodeStdinReader( llvm::BytecodeHandler* H = 0 );
|
2006-08-26 01:43:11 +08:00
|
|
|
bool read(std::string* ErrMsg);
|
2003-09-24 00:13:28 +08:00
|
|
|
};
|
|
|
|
}
|
2003-09-23 07:38:23 +08:00
|
|
|
|
2005-04-22 05:44:41 +08:00
|
|
|
BytecodeStdinReader::BytecodeStdinReader( BytecodeHandler* H )
|
2004-06-30 07:24:14 +08:00
|
|
|
: BytecodeReader(H)
|
2006-08-26 01:43:11 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
BytecodeStdinReader::read(std::string* ErrMsg)
|
2004-06-30 07:24:14 +08:00
|
|
|
{
|
2006-06-08 07:18:34 +08:00
|
|
|
sys::Program::ChangeStdinToBinary();
|
2004-12-21 15:51:33 +08:00
|
|
|
char Buffer[4096*4];
|
2003-09-23 07:38:23 +08:00
|
|
|
|
|
|
|
// Read in all of the data from stdin, we cannot mmap stdin...
|
2004-12-21 15:51:33 +08:00
|
|
|
while (std::cin.good()) {
|
|
|
|
std::cin.read(Buffer, 4096*4);
|
|
|
|
int BlockSize = std::cin.gcount();
|
|
|
|
if (0 >= BlockSize)
|
|
|
|
break;
|
2003-09-23 07:38:23 +08:00
|
|
|
FileData.insert(FileData.end(), Buffer, Buffer+BlockSize);
|
|
|
|
}
|
|
|
|
|
2006-08-26 01:43:11 +08:00
|
|
|
if (FileData.empty()) {
|
|
|
|
if (ErrMsg)
|
|
|
|
*ErrMsg = "Standard Input is empty!";
|
|
|
|
return true;
|
|
|
|
}
|
2003-09-23 07:38:23 +08:00
|
|
|
|
|
|
|
FileBuf = &FileData[0];
|
2006-08-26 01:43:11 +08:00
|
|
|
if (ParseBytecode(FileBuf, FileData.size(), "<stdin>", ErrMsg))
|
|
|
|
return true;
|
|
|
|
return false;
|
2003-09-23 07:38:23 +08:00
|
|
|
}
|
|
|
|
|
2005-06-19 02:34:52 +08: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.
|
2006-07-07 05:35:01 +08:00
|
|
|
if (MP->materializeModule() == 0)
|
|
|
|
return 0;
|
2005-06-19 02:34:52 +08:00
|
|
|
|
|
|
|
if(Function* F = M->getNamedFunction("llvm.va_start")) {
|
|
|
|
assert(F->arg_size() == 0 && "Obsolete va_start takes 0 argument!");
|
2005-07-27 14:12:32 +08:00
|
|
|
|
2005-06-19 02:34:52 +08:00
|
|
|
//foo = va_start()
|
|
|
|
// ->
|
|
|
|
//bar = alloca typeof(foo)
|
|
|
|
//va_start(bar)
|
|
|
|
//foo = load bar
|
2005-07-27 14:12:32 +08:00
|
|
|
|
2005-06-19 02:34:52 +08:00
|
|
|
const Type* RetTy = Type::getPrimitiveType(Type::VoidTyID);
|
|
|
|
const Type* ArgTy = F->getFunctionType()->getReturnType();
|
|
|
|
const Type* ArgTyPtr = PointerType::get(ArgTy);
|
2005-07-27 14:12:32 +08:00
|
|
|
Function* NF = M->getOrInsertFunction("llvm.va_start",
|
2005-10-23 12:37:20 +08:00
|
|
|
RetTy, ArgTyPtr, (Type *)0);
|
2005-06-19 02:34:52 +08: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, "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 14:12:32 +08:00
|
|
|
|
2005-06-19 02:34:52 +08: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 14:12:32 +08:00
|
|
|
Function* NF = M->getOrInsertFunction("llvm.va_end",
|
2005-10-23 12:37:20 +08:00
|
|
|
RetTy, ArgTyPtr, (Type *)0);
|
2005-07-27 14:12:32 +08:00
|
|
|
|
2005-06-19 02:34:52 +08: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 22:04:55 +08:00
|
|
|
new StoreInst(CI->getOperand(1), bar, CI);
|
2005-06-19 02:34:52 +08:00
|
|
|
new CallInst(NF, bar, "", CI);
|
|
|
|
CI->getParent()->getInstList().erase(CI);
|
|
|
|
}
|
|
|
|
F->setName("");
|
|
|
|
}
|
2005-07-27 14:12:32 +08:00
|
|
|
|
2005-06-19 02:34:52 +08: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-23 05:04:42 +08:00
|
|
|
//b = alloca 1 of typeof(foo)
|
|
|
|
//store bar -> b
|
|
|
|
//vacopy(a, b)
|
2005-06-19 02:34:52 +08:00
|
|
|
//foo = load a
|
2005-07-27 14:12:32 +08:00
|
|
|
|
2005-06-19 02:34:52 +08:00
|
|
|
const Type* RetTy = Type::getPrimitiveType(Type::VoidTyID);
|
|
|
|
const Type* ArgTy = F->getFunctionType()->getReturnType();
|
|
|
|
const Type* ArgTyPtr = PointerType::get(ArgTy);
|
2005-07-27 14:12:32 +08:00
|
|
|
Function* NF = M->getOrInsertFunction("llvm.va_copy",
|
2005-10-23 12:37:20 +08:00
|
|
|
RetTy, ArgTyPtr, ArgTyPtr, (Type *)0);
|
2005-07-27 14:12:32 +08:00
|
|
|
|
2005-06-19 02:34:52 +08: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-23 05:04:42 +08: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-19 02:34:52 +08:00
|
|
|
CI->replaceAllUsesWith(foo);
|
|
|
|
CI->getParent()->getInstList().erase(CI);
|
|
|
|
}
|
|
|
|
F->setName("");
|
|
|
|
}
|
|
|
|
return MP;
|
|
|
|
}
|
|
|
|
|
2003-10-18 13:54:18 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2003-09-24 00:13:28 +08:00
|
|
|
// Wrapper functions
|
2003-10-18 13:54:18 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2003-09-24 00:13:28 +08:00
|
|
|
|
|
|
|
/// getBytecodeBufferModuleProvider - lazy function-at-a-time loading from a
|
|
|
|
/// buffer
|
2005-04-22 05:44:41 +08:00
|
|
|
ModuleProvider*
|
2003-11-20 00:06:55 +08:00
|
|
|
llvm::getBytecodeBufferModuleProvider(const unsigned char *Buffer,
|
|
|
|
unsigned Length,
|
2004-06-30 07:24:14 +08:00
|
|
|
const std::string &ModuleID,
|
2006-08-26 01:43:11 +08:00
|
|
|
std::string* ErrMsg,
|
|
|
|
BytecodeHandler* H) {
|
|
|
|
BytecodeBufferReader* rdr =
|
|
|
|
new BytecodeBufferReader(Buffer, Length, ModuleID, H);
|
|
|
|
if (rdr->read(ErrMsg))
|
|
|
|
return 0;
|
|
|
|
return CheckVarargs(rdr);
|
2003-09-23 07:38:23 +08:00
|
|
|
}
|
|
|
|
|
2003-09-24 00:13:28 +08:00
|
|
|
/// ParseBytecodeBuffer - Parse a given bytecode buffer
|
|
|
|
///
|
2003-11-20 00:06:55 +08:00
|
|
|
Module *llvm::ParseBytecodeBuffer(const unsigned char *Buffer, unsigned Length,
|
|
|
|
const std::string &ModuleID,
|
2006-08-26 01:43:11 +08:00
|
|
|
std::string *ErrMsg){
|
|
|
|
ModuleProvider* MP =
|
|
|
|
getBytecodeBufferModuleProvider(Buffer, Length, ModuleID, ErrMsg, 0);
|
|
|
|
if (!MP)
|
2003-09-24 00:13:28 +08:00
|
|
|
return 0;
|
2006-08-26 01:43:11 +08:00
|
|
|
return MP->releaseModule();
|
2003-09-23 07:38:23 +08:00
|
|
|
}
|
|
|
|
|
2003-09-24 00:13:28 +08:00
|
|
|
/// getBytecodeModuleProvider - lazy function-at-a-time loading from a file
|
2003-09-23 07:38:23 +08:00
|
|
|
///
|
2006-08-26 01:43:11 +08:00
|
|
|
ModuleProvider *
|
|
|
|
llvm::getBytecodeModuleProvider(const std::string &Filename,
|
|
|
|
std::string* ErrMsg,
|
|
|
|
BytecodeHandler* H) {
|
|
|
|
// Read from a file
|
|
|
|
if (Filename != std::string("-")) {
|
|
|
|
BytecodeFileReader* rdr = new BytecodeFileReader(Filename, H);
|
|
|
|
if (rdr->read(ErrMsg))
|
|
|
|
return 0;
|
|
|
|
return CheckVarargs(rdr);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read from stdin
|
|
|
|
BytecodeStdinReader* rdr = new BytecodeStdinReader(H);
|
|
|
|
if (rdr->read(ErrMsg))
|
|
|
|
return 0;
|
|
|
|
return CheckVarargs(rdr);
|
2003-09-23 07:38:23 +08:00
|
|
|
}
|
|
|
|
|
2003-09-24 00:13:28 +08:00
|
|
|
/// ParseBytecodeFile - Parse the given bytecode file
|
|
|
|
///
|
2003-11-20 00:06:55 +08:00
|
|
|
Module *llvm::ParseBytecodeFile(const std::string &Filename,
|
2006-08-26 01:43:11 +08:00
|
|
|
std::string *ErrMsg) {
|
|
|
|
ModuleProvider* MP = getBytecodeModuleProvider(Filename, ErrMsg);
|
|
|
|
if (!MP)
|
2003-09-24 00:13:28 +08:00
|
|
|
return 0;
|
2006-08-26 01:43:11 +08:00
|
|
|
return MP->releaseModule();
|
2003-09-23 07:38:23 +08:00
|
|
|
}
|
2003-11-12 06:41:34 +08:00
|
|
|
|
2004-06-30 07:24:14 +08:00
|
|
|
// AnalyzeBytecodeFile - analyze one file
|
2004-08-22 04:52:03 +08:00
|
|
|
Module* llvm::AnalyzeBytecodeFile(
|
|
|
|
const std::string &Filename, ///< File to analyze
|
|
|
|
BytecodeAnalysis& bca, ///< Statistical output
|
2006-08-26 01:43:11 +08:00
|
|
|
std::string *ErrMsg, ///< Error output
|
2004-09-13 04:56:38 +08:00
|
|
|
std::ostream* output ///< Dump output
|
|
|
|
)
|
2004-06-30 07:24:14 +08:00
|
|
|
{
|
2006-08-26 01:43:11 +08:00
|
|
|
BytecodeHandler* AH = createBytecodeAnalyzerHandler(bca,output);
|
|
|
|
ModuleProvider* MP = getBytecodeModuleProvider(Filename, ErrMsg, AH);
|
|
|
|
if (!MP)
|
2004-06-30 07:24:14 +08:00
|
|
|
return 0;
|
2006-08-26 01:43:11 +08:00
|
|
|
return MP->releaseModule();
|
2004-06-30 07:24:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// AnalyzeBytecodeBuffer - analyze a buffer
|
|
|
|
Module* llvm::AnalyzeBytecodeBuffer(
|
2004-08-22 04:52:03 +08: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
|
2006-08-26 01:43:11 +08:00
|
|
|
std::string* ErrMsg, ///< Errors, if any.
|
2004-09-13 04:56:38 +08:00
|
|
|
std::ostream* output ///< Dump output, if any
|
|
|
|
)
|
2004-06-30 07:24:14 +08:00
|
|
|
{
|
2006-08-26 01:43:11 +08:00
|
|
|
BytecodeHandler* hdlr = createBytecodeAnalyzerHandler(bca, output);
|
|
|
|
ModuleProvider* MP =
|
|
|
|
getBytecodeBufferModuleProvider(Buffer, Length, ModuleID, ErrMsg, hdlr);
|
|
|
|
if (!MP)
|
2004-06-30 07:24:14 +08:00
|
|
|
return 0;
|
2006-08-26 01:43:11 +08:00
|
|
|
return MP->releaseModule();
|
2004-06-30 07:24:14 +08:00
|
|
|
}
|
|
|
|
|
2005-04-22 05:44:41 +08:00
|
|
|
bool llvm::GetBytecodeDependentLibraries(const std::string &fname,
|
2006-08-26 01:43:11 +08:00
|
|
|
Module::LibraryListType& deplibs,
|
|
|
|
std::string* ErrMsg) {
|
|
|
|
ModuleProvider* MP = getBytecodeModuleProvider(fname, ErrMsg);
|
|
|
|
if (!MP) {
|
2004-08-25 06:46:20 +08:00
|
|
|
deplibs.clear();
|
2006-08-26 01:43:11 +08:00
|
|
|
return true;
|
2004-08-25 06:46:20 +08:00
|
|
|
}
|
2006-08-26 01:43:11 +08:00
|
|
|
Module* M = MP->releaseModule();
|
|
|
|
deplibs = M->getLibraries();
|
|
|
|
delete M;
|
|
|
|
return false;
|
2004-08-25 06:46:20 +08:00
|
|
|
}
|
|
|
|
|
2005-02-14 01:42:11 +08:00
|
|
|
static void getSymbols(Module*M, std::vector<std::string>& symbols) {
|
2004-11-15 06:00:48 +08:00
|
|
|
// Loop over global variables
|
2005-03-15 12:54:21 +08:00
|
|
|
for (Module::global_iterator GI = M->global_begin(), GE=M->global_end(); GI != GE; ++GI)
|
2005-02-14 02:12:20 +08:00
|
|
|
if (!GI->isExternal() && !GI->hasInternalLinkage())
|
2005-02-14 01:42:11 +08: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-15 06:00:48 +08:00
|
|
|
}
|
|
|
|
|
2004-11-06 16:56:40 +08:00
|
|
|
// Get just the externally visible defined symbols from the bytecode
|
|
|
|
bool llvm::GetBytecodeSymbols(const sys::Path& fName,
|
2006-08-26 01:43:11 +08:00
|
|
|
std::vector<std::string>& symbols,
|
|
|
|
std::string* ErrMsg) {
|
|
|
|
ModuleProvider* MP = getBytecodeModuleProvider(fName.toString(), ErrMsg);
|
|
|
|
if (!MP)
|
|
|
|
return true;
|
2004-11-06 16:56:40 +08:00
|
|
|
|
2006-07-07 05:35:01 +08:00
|
|
|
// Get the module from the provider
|
2006-08-26 01:43:11 +08:00
|
|
|
Module* M = MP->materializeModule();
|
|
|
|
if (M == 0)
|
|
|
|
return true;
|
2004-11-06 16:56:40 +08:00
|
|
|
|
2006-07-07 05:35:01 +08:00
|
|
|
// Get the symbols
|
|
|
|
getSymbols(M, symbols);
|
2004-11-06 16:56:40 +08:00
|
|
|
|
2006-07-07 05:35:01 +08:00
|
|
|
// Done with the module
|
|
|
|
return true;
|
2004-11-06 16:56:40 +08:00
|
|
|
}
|
|
|
|
|
2005-04-22 05:44:41 +08:00
|
|
|
ModuleProvider*
|
2004-11-15 09:20:11 +08:00
|
|
|
llvm::GetBytecodeSymbols(const unsigned char*Buffer, unsigned Length,
|
2004-11-16 14:41:05 +08:00
|
|
|
const std::string& ModuleID,
|
2006-08-26 01:43:11 +08:00
|
|
|
std::vector<std::string>& symbols,
|
|
|
|
std::string* ErrMsg) {
|
|
|
|
// Get the module provider
|
|
|
|
ModuleProvider* MP =
|
|
|
|
getBytecodeBufferModuleProvider(Buffer, Length, ModuleID, ErrMsg, 0);
|
|
|
|
if (!MP)
|
|
|
|
return 0;
|
2004-11-15 06:00:48 +08:00
|
|
|
|
2006-08-26 01:43:11 +08:00
|
|
|
// Get the module from the provider
|
|
|
|
Module* M = MP->materializeModule();
|
|
|
|
if (M == 0) {
|
2004-11-16 14:41:05 +08:00
|
|
|
delete MP;
|
2006-08-26 01:43:11 +08:00
|
|
|
return 0;
|
2004-11-15 06:00:48 +08:00
|
|
|
}
|
2006-08-26 01:43:11 +08:00
|
|
|
|
|
|
|
// Get the symbols
|
|
|
|
getSymbols(M, symbols);
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
return MP;
|
2004-11-15 06:00:48 +08:00
|
|
|
}
|