2003-09-23 07:44:46 +08:00
|
|
|
//===- ReaderWrappers.cpp - Parse bytecode from file or buffer -----------===//
|
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.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
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"
|
2003-12-30 05:35:05 +08:00
|
|
|
#include "Support/FileUtilities.h"
|
2003-09-23 07:38:23 +08:00
|
|
|
#include "Support/StringExtras.h"
|
|
|
|
#include "Config/unistd.h"
|
2003-12-30 05:35:05 +08:00
|
|
|
#include <cerrno>
|
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:
|
|
|
|
unsigned char *Buffer;
|
2004-05-28 08:24:41 +08:00
|
|
|
unsigned Length;
|
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);
|
2003-09-23 07:38:23 +08:00
|
|
|
~BytecodeFileReader();
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2003-12-12 08:47:44 +08:00
|
|
|
static std::string ErrnoMessage (int savedErrNum, std::string descr) {
|
|
|
|
return ::strerror(savedErrNum) + std::string(", while trying to ") + descr;
|
|
|
|
}
|
|
|
|
|
2004-06-30 07:24:14 +08:00
|
|
|
BytecodeFileReader::BytecodeFileReader(const std::string &Filename,
|
|
|
|
llvm::BytecodeHandler* H )
|
|
|
|
: BytecodeReader(H)
|
|
|
|
{
|
2004-05-28 08:24:41 +08:00
|
|
|
Buffer = (unsigned char*)ReadFileIntoAddressSpace(Filename, Length);
|
|
|
|
if (Buffer == 0)
|
|
|
|
throw "Error reading file '" + Filename + "'.";
|
2003-09-23 07:38:23 +08:00
|
|
|
|
2003-10-09 03:55:47 +08:00
|
|
|
try {
|
|
|
|
// Parse the bytecode we mmapped in
|
2004-08-22 04:52:03 +08:00
|
|
|
ParseBytecode(Buffer, Length, Filename);
|
2003-10-09 03:55:47 +08:00
|
|
|
} catch (...) {
|
2004-05-28 08:24:41 +08:00
|
|
|
UnmapFileFromAddressSpace(Buffer, Length);
|
2003-10-09 03:55:47 +08:00
|
|
|
throw;
|
|
|
|
}
|
2003-09-23 07:38:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
BytecodeFileReader::~BytecodeFileReader() {
|
|
|
|
// Unmmap the bytecode...
|
2004-05-28 08:24:41 +08:00
|
|
|
UnmapFileFromAddressSpace(Buffer, Length);
|
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;
|
|
|
|
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();
|
|
|
|
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
BytecodeBufferReader::BytecodeBufferReader(const unsigned char *Buf,
|
2003-09-25 06:04:02 +08:00
|
|
|
unsigned Length,
|
2004-06-30 07:24:14 +08:00
|
|
|
const std::string &ModuleID,
|
2004-07-04 19:03:03 +08:00
|
|
|
llvm::BytecodeHandler* H )
|
2004-06-30 07:24:14 +08:00
|
|
|
: BytecodeReader(H)
|
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;
|
|
|
|
}
|
2003-10-09 03:55:47 +08:00
|
|
|
try {
|
2004-08-22 04:52:03 +08:00
|
|
|
ParseBytecode(ParseBegin, Length, ModuleID);
|
2003-10-09 03:55:47 +08:00
|
|
|
} catch (...) {
|
|
|
|
if (MustDelete) delete [] Buffer;
|
|
|
|
throw;
|
|
|
|
}
|
2003-09-24 00:13:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
BytecodeBufferReader::~BytecodeBufferReader() {
|
|
|
|
if (MustDelete) delete [] Buffer;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
///
|
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 );
|
2003-09-24 00:13:28 +08:00
|
|
|
};
|
|
|
|
}
|
2003-09-23 07:38:23 +08:00
|
|
|
|
2004-06-30 07:24:14 +08:00
|
|
|
BytecodeStdinReader::BytecodeStdinReader( BytecodeHandler* H )
|
|
|
|
: BytecodeReader(H)
|
|
|
|
{
|
2003-09-23 07:38:23 +08:00
|
|
|
int BlockSize;
|
|
|
|
unsigned char Buffer[4096*4];
|
|
|
|
|
|
|
|
// Read in all of the data from stdin, we cannot mmap stdin...
|
2003-11-12 06:41:34 +08:00
|
|
|
while ((BlockSize = ::read(0 /*stdin*/, Buffer, 4096*4))) {
|
2003-09-23 07:38:23 +08:00
|
|
|
if (BlockSize == -1)
|
2003-12-12 08:47:44 +08:00
|
|
|
throw ErrnoMessage(errno, "read from standard input");
|
2003-09-24 00:13:28 +08:00
|
|
|
|
2003-09-23 07:38:23 +08:00
|
|
|
FileData.insert(FileData.end(), Buffer, Buffer+BlockSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (FileData.empty())
|
|
|
|
throw std::string("Standard Input empty!");
|
|
|
|
|
|
|
|
FileBuf = &FileData[0];
|
2004-08-22 04:52:03 +08:00
|
|
|
ParseBytecode(FileBuf, FileData.size(), "<stdin>");
|
2003-09-23 07:38:23 +08:00
|
|
|
}
|
|
|
|
|
2003-10-18 13:54:18 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Varargs transmogrification code...
|
2003-09-24 00:13:28 +08:00
|
|
|
//
|
2003-10-18 13:54:18 +08:00
|
|
|
|
|
|
|
// 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 arguments...
|
|
|
|
Function *F = M->getNamedFunction("llvm.va_start");
|
|
|
|
if (F == 0) return MP; // No varargs use, just return.
|
|
|
|
|
|
|
|
if (F->getFunctionType()->getNumParams() == 0)
|
|
|
|
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 the user is making use of obsolete varargs intrinsics, adjust them for
|
|
|
|
// the user.
|
|
|
|
if (Function *F = M->getNamedFunction("llvm.va_start")) {
|
|
|
|
assert(F->asize() == 1 && "Obsolete va_start takes 1 argument!");
|
|
|
|
|
|
|
|
const Type *RetTy = F->getFunctionType()->getParamType(0);
|
|
|
|
RetTy = cast<PointerType>(RetTy)->getElementType();
|
|
|
|
Function *NF = M->getOrInsertFunction("llvm.va_start", RetTy, 0);
|
|
|
|
|
|
|
|
for (Value::use_iterator I = F->use_begin(), E = F->use_end(); I != E; )
|
|
|
|
if (CallInst *CI = dyn_cast<CallInst>(*I++)) {
|
|
|
|
Value *V = new CallInst(NF, "", CI);
|
|
|
|
new StoreInst(V, CI->getOperand(1), CI);
|
|
|
|
CI->getParent()->getInstList().erase(CI);
|
|
|
|
}
|
|
|
|
F->setName("");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Function *F = M->getNamedFunction("llvm.va_end")) {
|
|
|
|
assert(F->asize() == 1 && "Obsolete va_end takes 1 argument!");
|
|
|
|
const Type *ArgTy = F->getFunctionType()->getParamType(0);
|
|
|
|
ArgTy = cast<PointerType>(ArgTy)->getElementType();
|
|
|
|
Function *NF = M->getOrInsertFunction("llvm.va_end", Type::VoidTy,
|
|
|
|
ArgTy, 0);
|
|
|
|
|
|
|
|
for (Value::use_iterator I = F->use_begin(), E = F->use_end(); I != E; )
|
|
|
|
if (CallInst *CI = dyn_cast<CallInst>(*I++)) {
|
|
|
|
Value *V = new LoadInst(CI->getOperand(1), "", CI);
|
|
|
|
new CallInst(NF, V, "", CI);
|
|
|
|
CI->getParent()->getInstList().erase(CI);
|
|
|
|
}
|
|
|
|
F->setName("");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Function *F = M->getNamedFunction("llvm.va_copy")) {
|
|
|
|
assert(F->asize() == 2 && "Obsolete va_copy takes 2 argument!");
|
|
|
|
const Type *ArgTy = F->getFunctionType()->getParamType(0);
|
|
|
|
ArgTy = cast<PointerType>(ArgTy)->getElementType();
|
|
|
|
Function *NF = M->getOrInsertFunction("llvm.va_copy", ArgTy,
|
|
|
|
ArgTy, 0);
|
|
|
|
|
|
|
|
for (Value::use_iterator I = F->use_begin(), E = F->use_end(); I != E; )
|
|
|
|
if (CallInst *CI = dyn_cast<CallInst>(*I++)) {
|
|
|
|
Value *V = new CallInst(NF, CI->getOperand(2), "", CI);
|
|
|
|
new StoreInst(V, CI->getOperand(1), CI);
|
|
|
|
CI->getParent()->getInstList().erase(CI);
|
|
|
|
}
|
|
|
|
F->setName("");
|
|
|
|
}
|
|
|
|
return MP;
|
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
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
|
2003-10-05 04:14:59 +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,
|
2004-07-04 19:03:03 +08:00
|
|
|
BytecodeHandler* H ) {
|
2004-06-30 07:24:14 +08:00
|
|
|
return CheckVarargs(
|
|
|
|
new BytecodeBufferReader(Buffer, Length, ModuleID, H));
|
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,
|
|
|
|
std::string *ErrorStr){
|
2003-09-24 00:13:28 +08:00
|
|
|
try {
|
2003-10-05 04:14:59 +08:00
|
|
|
std::auto_ptr<ModuleProvider>
|
2003-10-05 03:19:37 +08:00
|
|
|
AMP(getBytecodeBufferModuleProvider(Buffer, Length, ModuleID));
|
|
|
|
return AMP->releaseModule();
|
2003-09-24 00:13:28 +08:00
|
|
|
} catch (std::string &err) {
|
2003-09-25 06:10:47 +08:00
|
|
|
if (ErrorStr) *ErrorStr = err;
|
2003-09-24 00:13:28 +08:00
|
|
|
return 0;
|
|
|
|
}
|
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
|
|
|
///
|
2004-06-30 07:24:14 +08:00
|
|
|
ModuleProvider *llvm::getBytecodeModuleProvider(const std::string &Filename,
|
2004-07-04 19:03:03 +08:00
|
|
|
BytecodeHandler* H) {
|
2003-09-23 07:38:23 +08:00
|
|
|
if (Filename != std::string("-")) // Read from a file...
|
2004-06-30 07:24:14 +08:00
|
|
|
return CheckVarargs(new BytecodeFileReader(Filename,H));
|
2003-09-23 07:38:23 +08:00
|
|
|
else // Read from stdin
|
2004-06-30 07:24:14 +08:00
|
|
|
return CheckVarargs(new BytecodeStdinReader(H));
|
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,
|
|
|
|
std::string *ErrorStr) {
|
2003-09-24 00:13:28 +08:00
|
|
|
try {
|
2003-10-05 04:14:59 +08:00
|
|
|
std::auto_ptr<ModuleProvider> AMP(getBytecodeModuleProvider(Filename));
|
2003-10-05 03:19:37 +08:00
|
|
|
return AMP->releaseModule();
|
2003-09-24 00:13:28 +08:00
|
|
|
} catch (std::string &err) {
|
2003-09-25 06:10:47 +08:00
|
|
|
if (ErrorStr) *ErrorStr = err;
|
2003-09-24 00:13:28 +08:00
|
|
|
return 0;
|
|
|
|
}
|
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
|
|
|
|
std::string *ErrorStr, ///< Error output
|
|
|
|
std::ostream* output ///< Dump output
|
|
|
|
)
|
2004-06-30 07:24:14 +08:00
|
|
|
{
|
|
|
|
try {
|
2004-08-22 04:52:03 +08:00
|
|
|
BytecodeHandler* analyzerHandler = createBytecodeAnalyzerHandler(bca,output);
|
2004-06-30 07:24:14 +08: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-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
|
|
|
|
std::string* ErrorStr, ///< Errors, if any.
|
|
|
|
std::ostream* output ///< Dump output, if any
|
|
|
|
)
|
2004-06-30 07:24:14 +08:00
|
|
|
{
|
|
|
|
try {
|
2004-08-22 04:52:03 +08:00
|
|
|
BytecodeHandler* hdlr = createBytecodeAnalyzerHandler(bca, output);
|
2004-06-30 07:24:14 +08:00
|
|
|
std::auto_ptr<ModuleProvider>
|
|
|
|
AMP(getBytecodeBufferModuleProvider(Buffer, Length, ModuleID, hdlr));
|
|
|
|
return AMP->releaseModule();
|
|
|
|
} catch (std::string &err) {
|
|
|
|
if (ErrorStr) *ErrorStr = err;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-08-25 06:46:20 +08:00
|
|
|
bool llvm::GetBytecodeDependentLibraries(const std::string &fname,
|
|
|
|
std::vector<std::string>& deplibs) {
|
|
|
|
try {
|
|
|
|
std::auto_ptr<ModuleProvider> AMP( getBytecodeModuleProvider(fname));
|
|
|
|
Module* M = AMP->releaseModule();
|
|
|
|
deplibs = M->getLibraries();
|
|
|
|
delete M;
|
|
|
|
return true;
|
|
|
|
} catch (...) {
|
|
|
|
deplibs.clear();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-06-30 07:24:14 +08:00
|
|
|
// vim: sw=2 ai
|