//
// The LLVM Compiler Infrastructure
//
-// This file was developed by Reid Spencer and is distributed under the
-// University of Illinois Open Source License. See LICENSE.TXT for details.
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
#include "llvm/Bitcode/ReaderWriter.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Module.h"
+#include <cstdlib>
#include <memory>
using namespace llvm;
/// Read a variable-bit-rate encoded unsigned integer
-inline unsigned readInteger(const char*&At, const char*End){
+static inline unsigned readInteger(const char*&At, const char*End) {
unsigned Shift = 0;
unsigned Result = 0;
break;
}
- // Determine if this is a bytecode file
+ // Determine if this is a bitcode file
switch (sys::IdentifyFileType(At, 4)) {
case sys::Bitcode_FileType:
- case sys::Bytecode_FileType:
- flags |= ArchiveMember::BytecodeFlag;
- break;
- case sys::CompressedBytecode_FileType:
- flags |= ArchiveMember::CompressedBytecodeFlag;
- flags &= ~ArchiveMember::CompressedFlag;
+ flags |= ArchiveMember::BitcodeFlag;
break;
default:
- flags &= ~(ArchiveMember::BytecodeFlag|
- ArchiveMember::CompressedBytecodeFlag);
+ flags &= ~ArchiveMember::BitcodeFlag;
break;
}
ArchiveMember* member = new ArchiveMember(this);
// Fill in fields of the ArchiveMember
- member->next = 0;
- member->prev = 0;
member->parent = this;
member->path.set(pathname);
member->info.fileSize = MemberSize;
bool
Archive::checkSignature(std::string* error) {
// Check the magic string at file's header
- if (mapfile->size() < 8 || memcmp(base, ARFILE_MAGIC, 8)) {
+ if (mapfile->getBufferSize() < 8 || memcmp(base, ARFILE_MAGIC, 8)) {
if (error)
*error = "invalid signature for an archive file";
return false;
members.clear();
symTab.clear();
const char *At = base;
- const char *End = base + mapfile->size();
+ const char *End = mapfile->getBufferEnd();
if (!checkSignature(error))
return false;
// Open and completely load the archive file.
Archive*
-Archive::OpenAndLoad(const sys::Path& file, std::string* ErrorMessage)
-{
- std::auto_ptr<Archive> result ( new Archive(file));
+Archive::OpenAndLoad(const sys::Path& file, LLVMContext& C,
+ std::string* ErrorMessage) {
+ std::auto_ptr<Archive> result ( new Archive(file, C));
if (result->mapToMemory(ErrorMessage))
return 0;
if (!result->loadArchive(ErrorMessage))
// Get all the bitcode modules from the archive
bool
-Archive::getAllModules(std::vector<Module*>& Modules, std::string* ErrMessage) {
+Archive::getAllModules(std::vector<Module*>& Modules,
+ std::string* ErrMessage) {
for (iterator I=begin(), E=end(); I != E; ++I) {
- if (I->isBytecode() || I->isCompressedBytecode()) {
- std::string FullMemberName = archPath.toString() +
- "(" + I->getPath().toString() + ")";
+ if (I->isBitcode()) {
+ std::string FullMemberName = archPath.str() +
+ "(" + I->getPath().str() + ")";
MemoryBuffer *Buffer =
MemoryBuffer::getNewMemBuffer(I->getSize(), FullMemberName.c_str());
memcpy((char*)Buffer->getBufferStart(), I->getData(), I->getSize());
- Module *M = ParseBitcodeFile(Buffer, ErrMessage);
+ Module *M = ParseBitcodeFile(Buffer, Context, ErrMessage);
delete Buffer;
if (!M)
return true;
members.clear();
symTab.clear();
const char *At = base;
- const char *End = base + mapfile->size();
+ const char *End = mapfile->getBufferEnd();
// Make sure we're dealing with an archive
if (!checkSignature(ErrorMsg))
}
// Open the archive and load just the symbol tables
-Archive*
-Archive::OpenAndLoadSymbols(const sys::Path& file, std::string* ErrorMessage) {
- std::auto_ptr<Archive> result ( new Archive(file) );
+Archive* Archive::OpenAndLoadSymbols(const sys::Path& file,
+ LLVMContext& C,
+ std::string* ErrorMessage) {
+ std::auto_ptr<Archive> result ( new Archive(file, C) );
if (result->mapToMemory(ErrorMessage))
return 0;
if (!result->loadSymbolTable(ErrorMessage))
return result.release();
}
-// Look up one symbol in the symbol table and return a ModuleProvider for the
-// module that defines that symbol.
-ModuleProvider*
+// Look up one symbol in the symbol table and return the module that defines
+// that symbol.
+Module*
Archive::findModuleDefiningSymbol(const std::string& symbol,
std::string* ErrMsg) {
SymTabType::iterator SI = symTab.find(symbol);
// Module hasn't been loaded yet, we need to load it
const char* modptr = base + fileOffset;
- ArchiveMember* mbr = parseMemberHeader(modptr, base + mapfile->size(),ErrMsg);
+ ArchiveMember* mbr = parseMemberHeader(modptr, mapfile->getBufferEnd(),
+ ErrMsg);
if (!mbr)
return 0;
- // Now, load the bitcode module to get the ModuleProvider
- std::string FullMemberName = archPath.toString() + "(" +
- mbr->getPath().toString() + ")";
+ // Now, load the bitcode module to get the Module.
+ std::string FullMemberName = archPath.str() + "(" +
+ mbr->getPath().str() + ")";
MemoryBuffer *Buffer =MemoryBuffer::getNewMemBuffer(mbr->getSize(),
FullMemberName.c_str());
memcpy((char*)Buffer->getBufferStart(), mbr->getData(), mbr->getSize());
- ModuleProvider *mp = getBitcodeModuleProvider(Buffer, ErrMsg);
- if (!mp)
+ Module *m = getLazyBitcodeModule(Buffer, Context, ErrMsg);
+ if (!m)
return 0;
- modules.insert(std::make_pair(fileOffset, std::make_pair(mp, mbr)));
+ modules.insert(std::make_pair(fileOffset, std::make_pair(m, mbr)));
- return mp;
+ return m;
}
// Look up multiple symbols in the symbol table and return a set of
-// ModuleProviders that define those symbols.
+// Modules that define those symbols.
bool
Archive::findModulesDefiningSymbols(std::set<std::string>& symbols,
- std::set<ModuleProvider*>& result,
+ std::set<Module*>& result,
std::string* error) {
if (!mapfile || !base) {
if (error)
// below.
// Get a pointer to the first file
- const char* At = ((const char*)base) + firstFileOffset;
- const char* End = ((const char*)base) + mapfile->size();
+ const char* At = base + firstFileOffset;
+ const char* End = mapfile->getBufferEnd();
while ( At < End) {
// Compute the offset to be put in the symbol table
return false;
// If it contains symbols
- if (mbr->isBytecode() || mbr->isCompressedBytecode()) {
+ if (mbr->isBitcode()) {
// Get the symbols
std::vector<std::string> symbols;
- std::string FullMemberName = archPath.toString() + "(" +
- mbr->getPath().toString() + ")";
- ModuleProvider* MP =
+ std::string FullMemberName = archPath.str() + "(" +
+ mbr->getPath().str() + ")";
+ Module* M =
GetBitcodeSymbols((const unsigned char*)At, mbr->getSize(),
- FullMemberName, symbols, error);
+ FullMemberName, Context, symbols, error);
- if (MP) {
+ if (M) {
// Insert the module's symbols into the symbol table
for (std::vector<std::string>::iterator I = symbols.begin(),
E=symbols.end(); I != E; ++I ) {
symTab.insert(std::make_pair(*I, offset));
}
- // Insert the ModuleProvider and the ArchiveMember into the table of
+ // Insert the Module and the ArchiveMember into the table of
// modules.
- modules.insert(std::make_pair(offset, std::make_pair(MP, mbr)));
+ modules.insert(std::make_pair(offset, std::make_pair(M, mbr)));
} else {
if (error)
*error = "Can't parse bitcode member: " +
- mbr->getPath().toString() + ": " + *error;
+ mbr->getPath().str() + ": " + *error;
delete mbr;
return false;
}
for (std::set<std::string>::iterator I=symbols.begin(),
E=symbols.end(); I != E;) {
// See if this symbol exists
- ModuleProvider* mp = findModuleDefiningSymbol(*I,error);
- if (mp) {
- // The symbol exists, insert the ModuleProvider into our result,
- // duplicates wil be ignored
- result.insert(mp);
+ Module* m = findModuleDefiningSymbol(*I,error);
+ if (m) {
+ // The symbol exists, insert the Module into our result, duplicates will
+ // be ignored.
+ result.insert(m);
// Remove the symbol now that its been resolved, being careful to
// post-increment the iterator.
bool Archive::isBitcodeArchive() {
// Make sure the symTab has been loaded. In most cases this should have been
// done when the archive was constructed, but still, this is just in case.
- if (!symTab.size())
+ if (symTab.empty())
if (!loadSymbolTable(0))
return false;
// Scan the archive, trying to load a bitcode member. We only load one to
// see if this works.
for (iterator I = begin(), E = end(); I != E; ++I) {
- if (!I->isBytecode() && !I->isCompressedBytecode())
+ if (!I->isBitcode())
continue;
std::string FullMemberName =
- archPath.toString() + "(" + I->getPath().toString() + ")";
+ archPath.str() + "(" + I->getPath().str() + ")";
MemoryBuffer *Buffer =
MemoryBuffer::getNewMemBuffer(I->getSize(), FullMemberName.c_str());
memcpy((char*)Buffer->getBufferStart(), I->getData(), I->getSize());
- Module *M = ParseBitcodeFile(Buffer);
+ Module *M = ParseBitcodeFile(Buffer, Context);
delete Buffer;
if (!M)
return false; // Couldn't parse bitcode, not a bitcode archive.