2003-09-23 01:44:46 +02:00
|
|
|
//===- ReaderWrappers.cpp - Parse bytecode from file or buffer -----------===//
|
2003-10-20 21:43:21 +02: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 01:44:46 +02:00
|
|
|
//
|
|
|
|
// This file implements loading and parsing a bytecode file and parsing a
|
|
|
|
// bytecode module from a given buffer.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2003-09-23 01:38:23 +02:00
|
|
|
#include "ReaderInternals.h"
|
2003-10-18 07:54:18 +02:00
|
|
|
#include "llvm/Module.h"
|
|
|
|
#include "llvm/Instructions.h"
|
2003-09-23 01:38:23 +02:00
|
|
|
#include "Support/StringExtras.h"
|
|
|
|
#include "Config/fcntl.h"
|
2003-10-06 05:30:28 +02:00
|
|
|
#include <sys/stat.h>
|
2003-09-23 01:38:23 +02:00
|
|
|
#include "Config/unistd.h"
|
|
|
|
#include "Config/sys/mman.h"
|
|
|
|
|
2003-11-11 23:41:34 +01:00
|
|
|
namespace llvm {
|
|
|
|
|
2003-10-18 07:54:18 +02:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// BytecodeFileReader - Read from an mmap'able file descriptor.
|
|
|
|
//
|
|
|
|
|
2003-09-23 01:38:23 +02:00
|
|
|
namespace {
|
2003-09-23 18:13:28 +02:00
|
|
|
/// FDHandle - Simple handle class to make sure a file descriptor gets closed
|
|
|
|
/// when the object is destroyed.
|
|
|
|
///
|
|
|
|
class FDHandle {
|
|
|
|
int FD;
|
|
|
|
public:
|
|
|
|
FDHandle(int fd) : FD(fd) {}
|
|
|
|
operator int() const { return FD; }
|
|
|
|
~FDHandle() {
|
|
|
|
if (FD != -1) close(FD);
|
|
|
|
}
|
|
|
|
};
|
2003-09-23 01:38:23 +02:00
|
|
|
|
|
|
|
/// BytecodeFileReader - parses a bytecode file from a file
|
|
|
|
///
|
|
|
|
class BytecodeFileReader : public BytecodeParser {
|
|
|
|
private:
|
|
|
|
unsigned char *Buffer;
|
|
|
|
int Length;
|
|
|
|
|
|
|
|
BytecodeFileReader(const BytecodeFileReader&); // Do not implement
|
2003-09-23 17:09:26 +02:00
|
|
|
void operator=(const BytecodeFileReader &BFR); // Do not implement
|
2003-09-23 01:38:23 +02:00
|
|
|
|
|
|
|
public:
|
|
|
|
BytecodeFileReader(const std::string &Filename);
|
|
|
|
~BytecodeFileReader();
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
BytecodeFileReader::BytecodeFileReader(const std::string &Filename) {
|
|
|
|
FDHandle FD = open(Filename.c_str(), O_RDONLY);
|
|
|
|
if (FD == -1)
|
|
|
|
throw std::string("Error opening file!");
|
|
|
|
|
|
|
|
// Stat the file to get its length...
|
|
|
|
struct stat StatBuf;
|
|
|
|
if (fstat(FD, &StatBuf) == -1 || StatBuf.st_size == 0)
|
|
|
|
throw std::string("Error stat'ing file!");
|
|
|
|
|
|
|
|
// mmap in the file all at once...
|
|
|
|
Length = StatBuf.st_size;
|
2003-09-25 06:13:53 +02:00
|
|
|
Buffer = (unsigned char*)mmap(0, Length, PROT_READ, MAP_PRIVATE, FD, 0);
|
|
|
|
|
2003-09-23 01:38:23 +02:00
|
|
|
if (Buffer == (unsigned char*)MAP_FAILED)
|
|
|
|
throw std::string("Error mmapping file!");
|
|
|
|
|
2003-10-08 21:55:47 +02:00
|
|
|
try {
|
|
|
|
// Parse the bytecode we mmapped in
|
|
|
|
ParseBytecode(Buffer, Length, Filename);
|
|
|
|
} catch (...) {
|
|
|
|
munmap((char*)Buffer, Length);
|
|
|
|
throw;
|
|
|
|
}
|
2003-09-23 01:38:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
BytecodeFileReader::~BytecodeFileReader() {
|
|
|
|
// Unmmap the bytecode...
|
|
|
|
munmap((char*)Buffer, Length);
|
|
|
|
}
|
|
|
|
|
2003-10-18 07:54:18 +02:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// BytecodeBufferReader - Read from a memory buffer
|
|
|
|
//
|
2003-09-23 18:13:28 +02:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
/// BytecodeBufferReader - parses a bytecode file from a buffer
|
|
|
|
///
|
|
|
|
class BytecodeBufferReader : public BytecodeParser {
|
|
|
|
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,
|
|
|
|
const std::string &ModuleID);
|
|
|
|
~BytecodeBufferReader();
|
|
|
|
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
BytecodeBufferReader::BytecodeBufferReader(const unsigned char *Buf,
|
2003-09-25 00:04:02 +02:00
|
|
|
unsigned Length,
|
2003-09-23 18:13:28 +02:00
|
|
|
const std::string &ModuleID)
|
|
|
|
{
|
|
|
|
// If not aligned, allocate a new buffer to hold the bytecode...
|
|
|
|
const unsigned char *ParseBegin = 0;
|
|
|
|
if ((intptr_t)Buf & 3) {
|
2003-09-25 00:04:02 +02:00
|
|
|
Buffer = new unsigned char[Length+4];
|
2003-09-25 00:34:17 +02:00
|
|
|
unsigned Offset = 4 - ((intptr_t)Buffer & 3); // Make sure it's aligned
|
2003-09-23 18:13:28 +02:00
|
|
|
ParseBegin = Buffer + Offset;
|
2003-09-25 00:04:02 +02:00
|
|
|
memcpy((unsigned char*)ParseBegin, Buf, Length); // Copy it over
|
2003-09-23 18:13:28 +02:00
|
|
|
MustDelete = true;
|
|
|
|
} else {
|
|
|
|
// If we don't need to copy it over, just use the caller's copy
|
2003-09-23 23:19:11 +02:00
|
|
|
ParseBegin = Buffer = Buf;
|
2003-09-23 18:13:28 +02:00
|
|
|
MustDelete = false;
|
|
|
|
}
|
2003-10-08 21:55:47 +02:00
|
|
|
try {
|
|
|
|
ParseBytecode(ParseBegin, Length, ModuleID);
|
|
|
|
} catch (...) {
|
|
|
|
if (MustDelete) delete [] Buffer;
|
|
|
|
throw;
|
|
|
|
}
|
2003-09-23 18:13:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
BytecodeBufferReader::~BytecodeBufferReader() {
|
|
|
|
if (MustDelete) delete [] Buffer;
|
|
|
|
}
|
|
|
|
|
2003-10-18 07:54:18 +02:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// BytecodeStdinReader - Read bytecode from Standard Input
|
|
|
|
//
|
2003-09-23 18:13:28 +02:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
/// BytecodeStdinReader - parses a bytecode file from stdin
|
|
|
|
///
|
|
|
|
class BytecodeStdinReader : public BytecodeParser {
|
|
|
|
private:
|
|
|
|
std::vector<unsigned char> FileData;
|
|
|
|
unsigned char *FileBuf;
|
|
|
|
|
|
|
|
BytecodeStdinReader(const BytecodeStdinReader&); // Do not implement
|
|
|
|
void operator=(const BytecodeStdinReader &BFR); // Do not implement
|
|
|
|
|
|
|
|
public:
|
|
|
|
BytecodeStdinReader();
|
|
|
|
};
|
|
|
|
}
|
2003-09-23 01:38:23 +02:00
|
|
|
|
|
|
|
BytecodeStdinReader::BytecodeStdinReader() {
|
|
|
|
int BlockSize;
|
|
|
|
unsigned char Buffer[4096*4];
|
|
|
|
|
|
|
|
// Read in all of the data from stdin, we cannot mmap stdin...
|
2003-11-11 23:41:34 +01:00
|
|
|
while ((BlockSize = ::read(0 /*stdin*/, Buffer, 4096*4))) {
|
2003-09-23 01:38:23 +02:00
|
|
|
if (BlockSize == -1)
|
|
|
|
throw std::string("Error reading from stdin!");
|
2003-09-23 18:13:28 +02:00
|
|
|
|
2003-09-23 01:38:23 +02:00
|
|
|
FileData.insert(FileData.end(), Buffer, Buffer+BlockSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (FileData.empty())
|
|
|
|
throw std::string("Standard Input empty!");
|
|
|
|
|
|
|
|
FileBuf = &FileData[0];
|
|
|
|
ParseBytecode(FileBuf, FileData.size(), "<stdin>");
|
|
|
|
}
|
|
|
|
|
2003-10-18 07:54:18 +02:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Varargs transmogrification code...
|
2003-09-23 18:13:28 +02:00
|
|
|
//
|
2003-10-18 07:54:18 +02: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-23 18:13:28 +02:00
|
|
|
// Wrapper functions
|
2003-10-18 07:54:18 +02:00
|
|
|
//===----------------------------------------------------------------------===//
|
2003-09-23 18:13:28 +02:00
|
|
|
|
|
|
|
/// getBytecodeBufferModuleProvider - lazy function-at-a-time loading from a
|
|
|
|
/// buffer
|
2003-10-04 22:14:59 +02:00
|
|
|
ModuleProvider*
|
2003-09-23 01:38:23 +02:00
|
|
|
getBytecodeBufferModuleProvider(const unsigned char *Buffer, unsigned Length,
|
|
|
|
const std::string &ModuleID) {
|
2003-10-18 07:54:18 +02:00
|
|
|
return CheckVarargs(new BytecodeBufferReader(Buffer, Length, ModuleID));
|
2003-09-23 01:38:23 +02:00
|
|
|
}
|
|
|
|
|
2003-09-23 18:13:28 +02:00
|
|
|
/// ParseBytecodeBuffer - Parse a given bytecode buffer
|
|
|
|
///
|
2003-09-23 01:38:23 +02:00
|
|
|
Module *ParseBytecodeBuffer(const unsigned char *Buffer, unsigned Length,
|
|
|
|
const std::string &ModuleID, std::string *ErrorStr){
|
2003-09-23 18:13:28 +02:00
|
|
|
try {
|
2003-10-04 22:14:59 +02:00
|
|
|
std::auto_ptr<ModuleProvider>
|
2003-10-04 21:19:37 +02:00
|
|
|
AMP(getBytecodeBufferModuleProvider(Buffer, Length, ModuleID));
|
|
|
|
return AMP->releaseModule();
|
2003-09-23 18:13:28 +02:00
|
|
|
} catch (std::string &err) {
|
2003-09-25 00:10:47 +02:00
|
|
|
if (ErrorStr) *ErrorStr = err;
|
2003-09-23 18:13:28 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2003-09-23 01:38:23 +02:00
|
|
|
}
|
|
|
|
|
2003-09-23 18:13:28 +02:00
|
|
|
/// getBytecodeModuleProvider - lazy function-at-a-time loading from a file
|
2003-09-23 01:38:23 +02:00
|
|
|
///
|
2003-10-04 22:14:59 +02:00
|
|
|
ModuleProvider *getBytecodeModuleProvider(const std::string &Filename) {
|
2003-09-23 01:38:23 +02:00
|
|
|
if (Filename != std::string("-")) // Read from a file...
|
2003-10-18 07:54:18 +02:00
|
|
|
return CheckVarargs(new BytecodeFileReader(Filename));
|
2003-09-23 01:38:23 +02:00
|
|
|
else // Read from stdin
|
2003-10-18 07:54:18 +02:00
|
|
|
return CheckVarargs(new BytecodeStdinReader());
|
2003-09-23 01:38:23 +02:00
|
|
|
}
|
|
|
|
|
2003-09-23 18:13:28 +02:00
|
|
|
/// ParseBytecodeFile - Parse the given bytecode file
|
|
|
|
///
|
2003-09-23 01:38:23 +02:00
|
|
|
Module *ParseBytecodeFile(const std::string &Filename, std::string *ErrorStr) {
|
2003-09-23 18:13:28 +02:00
|
|
|
try {
|
2003-10-04 22:14:59 +02:00
|
|
|
std::auto_ptr<ModuleProvider> AMP(getBytecodeModuleProvider(Filename));
|
2003-10-04 21:19:37 +02:00
|
|
|
return AMP->releaseModule();
|
2003-09-23 18:13:28 +02:00
|
|
|
} catch (std::string &err) {
|
2003-09-25 00:10:47 +02:00
|
|
|
if (ErrorStr) *ErrorStr = err;
|
2003-09-23 18:13:28 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2003-09-23 01:38:23 +02:00
|
|
|
}
|
2003-11-11 23:41:34 +01:00
|
|
|
|
|
|
|
} // End llvm namespace
|