add bitcode support

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@36856 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Chris Lattner 2007-05-06 06:18:07 +00:00
parent 1a019e5ffd
commit e07c15c734
4 changed files with 88 additions and 100 deletions

View File

@ -13,10 +13,14 @@
#include "ArchiveInternals.h" #include "ArchiveInternals.h"
#include "llvm/Bytecode/Reader.h" #include "llvm/Bytecode/Reader.h"
#include "llvm/Bitcode/ReaderWriter.h"
#include "llvm/Support/Compressor.h" #include "llvm/Support/Compressor.h"
#include "llvm/Support/MemoryBuffer.h"
#include <memory> #include <memory>
using namespace llvm; using namespace llvm;
static bool Bitcode = false;
/// Read a variable-bit-rate encoded unsigned integer /// Read a variable-bit-rate encoded unsigned integer
inline unsigned readInteger(const char*&At, const char*End){ inline unsigned readInteger(const char*&At, const char*End){
unsigned Shift = 0; unsigned Shift = 0;
@ -205,6 +209,7 @@ Archive::parseMemberHeader(const char*& At, const char* End, std::string* error)
// Determine if this is a bytecode file // Determine if this is a bytecode file
switch (sys::IdentifyFileType(At, 4)) { switch (sys::IdentifyFileType(At, 4)) {
case sys::Bitcode_FileType:
case sys::Bytecode_FileType: case sys::Bytecode_FileType:
flags |= ArchiveMember::BytecodeFlag; flags |= ArchiveMember::BytecodeFlag;
break; break;
@ -350,10 +355,21 @@ Archive::getAllModules(std::vector<Module*>& Modules, std::string* ErrMessage) {
if (I->isBytecode() || I->isCompressedBytecode()) { if (I->isBytecode() || I->isCompressedBytecode()) {
std::string FullMemberName = archPath.toString() + std::string FullMemberName = archPath.toString() +
"(" + I->getPath().toString() + ")"; "(" + I->getPath().toString() + ")";
Module* M = ParseBytecodeBuffer((const unsigned char*)I->getData(), Module *M;
if (Bitcode) {
MemoryBuffer *Buffer =
MemoryBuffer::getNewMemBuffer(I->getSize(), FullMemberName.c_str());
memcpy((char*)Buffer->getBufferStart(), I->getData(), I->getSize());
M = ParseBitcodeFile(Buffer, ErrMessage);
delete Buffer;
} else {
M = ParseBytecodeBuffer((const unsigned char*)I->getData(),
I->getSize(), FullMemberName, I->getSize(), FullMemberName,
Compressor::decompressToNewBuffer, Compressor::decompressToNewBuffer,
ErrMessage); ErrMessage);
}
if (!M) if (!M)
return true; return true;
@ -486,7 +502,15 @@ Archive::findModuleDefiningSymbol(const std::string& symbol,
// Now, load the bytecode module to get the ModuleProvider // Now, load the bytecode module to get the ModuleProvider
std::string FullMemberName = archPath.toString() + "(" + std::string FullMemberName = archPath.toString() + "(" +
mbr->getPath().toString() + ")"; mbr->getPath().toString() + ")";
ModuleProvider* mp = getBytecodeBufferModuleProvider( ModuleProvider* mp;
if (Bitcode) {
MemoryBuffer *Buffer =MemoryBuffer::getNewMemBuffer(mbr->getSize(),
FullMemberName.c_str());
memcpy((char*)Buffer->getBufferStart(), mbr->getData(), mbr->getSize());
mp = getBitcodeModuleProvider(Buffer, ErrMsg);
} else
mp = getBytecodeBufferModuleProvider(
(const unsigned char*) mbr->getData(), mbr->getSize(), (const unsigned char*) mbr->getData(), mbr->getSize(),
FullMemberName, Decompressor, ErrMsg, 0); FullMemberName, Decompressor, ErrMsg, 0);
if (!mp) if (!mp)
@ -612,8 +636,18 @@ bool Archive::isBytecodeArchive() {
std::string FullMemberName = std::string FullMemberName =
archPath.toString() + "(" + I->getPath().toString() + ")"; archPath.toString() + "(" + I->getPath().toString() + ")";
Module* M = ParseBytecodeBuffer((const unsigned char*)I->getData(), Module *M;
if (Bitcode) {
MemoryBuffer *Buffer =
MemoryBuffer::getNewMemBuffer(I->getSize(), FullMemberName.c_str());
memcpy((char*)Buffer->getBufferStart(), I->getData(), I->getSize());
M = ParseBitcodeFile(Buffer);
delete Buffer;
} else {
M = ParseBytecodeBuffer((const unsigned char*)I->getData(),
I->getSize(), FullMemberName); I->getSize(), FullMemberName);
}
if (!M) if (!M)
return false; // Couldn't parse bytecode, not a bytecode archive. return false; // Couldn't parse bytecode, not a bytecode archive.
delete M; delete M;

View File

@ -13,6 +13,7 @@
#include "ArchiveInternals.h" #include "ArchiveInternals.h"
#include "llvm/Bytecode/Reader.h" #include "llvm/Bytecode/Reader.h"
#include "llvm/Bitcode/ReaderWriter.h"
#include "llvm/Support/Compressor.h" #include "llvm/Support/Compressor.h"
#include "llvm/System/Signals.h" #include "llvm/System/Signals.h"
#include "llvm/System/Process.h" #include "llvm/System/Process.h"
@ -178,6 +179,7 @@ Archive::addFileBefore(const sys::Path& filePath, iterator where,
std::string magic; std::string magic;
mbr->path.getMagicNumber(magic,4); mbr->path.getMagicNumber(magic,4);
switch (sys::IdentifyFileType(magic.c_str(),4)) { switch (sys::IdentifyFileType(magic.c_str(),4)) {
case sys::Bitcode_FileType:
case sys::Bytecode_FileType: case sys::Bytecode_FileType:
flags |= ArchiveMember::BytecodeFlag; flags |= ArchiveMember::BytecodeFlag;
break; break;
@ -261,40 +263,7 @@ Archive::writeMember(
} }
} }
// Determine if we actually should compress this member int hdrSize = fSize;
bool willCompress =
(ShouldCompress &&
!member.isCompressed() &&
!member.isCompressedBytecode() &&
!member.isLLVMSymbolTable() &&
!member.isSVR4SymbolTable() &&
!member.isBSD4SymbolTable());
// Perform the compression. Note that if the file is uncompressed bytecode
// then we turn the file into compressed bytecode rather than treating it as
// compressed data. This is necessary since it allows us to determine that the
// file contains bytecode instead of looking like a regular compressed data
// member. A compressed bytecode file has its content compressed but has a
// magic number of "llvc". This acounts for the +/-4 arithmetic in the code
// below.
int hdrSize;
if (willCompress) {
char* output = 0;
if (member.isBytecode()) {
data +=4;
fSize -= 4;
}
fSize = Compressor::compressToNewBuffer(data,fSize,output,ErrMsg);
if (fSize == 0)
return true;
data = output;
if (member.isBytecode())
hdrSize = -fSize-4;
else
hdrSize = -fSize;
} else {
hdrSize = fSize;
}
// Compute the fields of the header // Compute the fields of the header
ArchiveMemberHeader Hdr; ArchiveMemberHeader Hdr;
@ -309,10 +278,6 @@ Archive::writeMember(
member.getPath().toString().length()); member.getPath().toString().length());
} }
// Make sure we write the compressed bytecode magic number if we should.
if (willCompress && member.isBytecode())
ARFile.write("llvc",4);
// Write the (possibly compressed) member's content to the file. // Write the (possibly compressed) member's content to the file.
ARFile.write(data,fSize); ARFile.write(data,fSize);
@ -320,11 +285,6 @@ Archive::writeMember(
if ((ARFile.tellp() & 1) == 1) if ((ARFile.tellp() & 1) == 1)
ARFile << ARFILE_PAD; ARFile << ARFILE_PAD;
// Free the compressed data, if necessary
if (willCompress) {
free((void*)data);
}
// Close the mapped file if it was opened // Close the mapped file if it was opened
if (mFile != 0) { if (mFile != 0) {
mFile->close(); mFile->close();

View File

@ -13,10 +13,14 @@
#include "ArchiveInternals.h" #include "ArchiveInternals.h"
#include "llvm/Bytecode/Reader.h" #include "llvm/Bytecode/Reader.h"
#include "llvm/Bitcode/ReaderWriter.h"
#include "llvm/Support/Compressor.h" #include "llvm/Support/Compressor.h"
#include "llvm/Support/MemoryBuffer.h"
#include <memory> #include <memory>
using namespace llvm; using namespace llvm;
static bool Bitcode = false;
/// Read a variable-bit-rate encoded unsigned integer /// Read a variable-bit-rate encoded unsigned integer
inline unsigned readInteger(const char*&At, const char*End){ inline unsigned readInteger(const char*&At, const char*End){
unsigned Shift = 0; unsigned Shift = 0;
@ -205,6 +209,7 @@ Archive::parseMemberHeader(const char*& At, const char* End, std::string* error)
// Determine if this is a bytecode file // Determine if this is a bytecode file
switch (sys::IdentifyFileType(At, 4)) { switch (sys::IdentifyFileType(At, 4)) {
case sys::Bitcode_FileType:
case sys::Bytecode_FileType: case sys::Bytecode_FileType:
flags |= ArchiveMember::BytecodeFlag; flags |= ArchiveMember::BytecodeFlag;
break; break;
@ -350,10 +355,21 @@ Archive::getAllModules(std::vector<Module*>& Modules, std::string* ErrMessage) {
if (I->isBytecode() || I->isCompressedBytecode()) { if (I->isBytecode() || I->isCompressedBytecode()) {
std::string FullMemberName = archPath.toString() + std::string FullMemberName = archPath.toString() +
"(" + I->getPath().toString() + ")"; "(" + I->getPath().toString() + ")";
Module* M = ParseBytecodeBuffer((const unsigned char*)I->getData(), Module *M;
if (Bitcode) {
MemoryBuffer *Buffer =
MemoryBuffer::getNewMemBuffer(I->getSize(), FullMemberName.c_str());
memcpy((char*)Buffer->getBufferStart(), I->getData(), I->getSize());
M = ParseBitcodeFile(Buffer, ErrMessage);
delete Buffer;
} else {
M = ParseBytecodeBuffer((const unsigned char*)I->getData(),
I->getSize(), FullMemberName, I->getSize(), FullMemberName,
Compressor::decompressToNewBuffer, Compressor::decompressToNewBuffer,
ErrMessage); ErrMessage);
}
if (!M) if (!M)
return true; return true;
@ -486,7 +502,15 @@ Archive::findModuleDefiningSymbol(const std::string& symbol,
// Now, load the bytecode module to get the ModuleProvider // Now, load the bytecode module to get the ModuleProvider
std::string FullMemberName = archPath.toString() + "(" + std::string FullMemberName = archPath.toString() + "(" +
mbr->getPath().toString() + ")"; mbr->getPath().toString() + ")";
ModuleProvider* mp = getBytecodeBufferModuleProvider( ModuleProvider* mp;
if (Bitcode) {
MemoryBuffer *Buffer =MemoryBuffer::getNewMemBuffer(mbr->getSize(),
FullMemberName.c_str());
memcpy((char*)Buffer->getBufferStart(), mbr->getData(), mbr->getSize());
mp = getBitcodeModuleProvider(Buffer, ErrMsg);
} else
mp = getBytecodeBufferModuleProvider(
(const unsigned char*) mbr->getData(), mbr->getSize(), (const unsigned char*) mbr->getData(), mbr->getSize(),
FullMemberName, Decompressor, ErrMsg, 0); FullMemberName, Decompressor, ErrMsg, 0);
if (!mp) if (!mp)
@ -612,8 +636,18 @@ bool Archive::isBytecodeArchive() {
std::string FullMemberName = std::string FullMemberName =
archPath.toString() + "(" + I->getPath().toString() + ")"; archPath.toString() + "(" + I->getPath().toString() + ")";
Module* M = ParseBytecodeBuffer((const unsigned char*)I->getData(), Module *M;
if (Bitcode) {
MemoryBuffer *Buffer =
MemoryBuffer::getNewMemBuffer(I->getSize(), FullMemberName.c_str());
memcpy((char*)Buffer->getBufferStart(), I->getData(), I->getSize());
M = ParseBitcodeFile(Buffer);
delete Buffer;
} else {
M = ParseBytecodeBuffer((const unsigned char*)I->getData(),
I->getSize(), FullMemberName); I->getSize(), FullMemberName);
}
if (!M) if (!M)
return false; // Couldn't parse bytecode, not a bytecode archive. return false; // Couldn't parse bytecode, not a bytecode archive.
delete M; delete M;

View File

@ -13,6 +13,7 @@
#include "ArchiveInternals.h" #include "ArchiveInternals.h"
#include "llvm/Bytecode/Reader.h" #include "llvm/Bytecode/Reader.h"
#include "llvm/Bitcode/ReaderWriter.h"
#include "llvm/Support/Compressor.h" #include "llvm/Support/Compressor.h"
#include "llvm/System/Signals.h" #include "llvm/System/Signals.h"
#include "llvm/System/Process.h" #include "llvm/System/Process.h"
@ -178,6 +179,7 @@ Archive::addFileBefore(const sys::Path& filePath, iterator where,
std::string magic; std::string magic;
mbr->path.getMagicNumber(magic,4); mbr->path.getMagicNumber(magic,4);
switch (sys::IdentifyFileType(magic.c_str(),4)) { switch (sys::IdentifyFileType(magic.c_str(),4)) {
case sys::Bitcode_FileType:
case sys::Bytecode_FileType: case sys::Bytecode_FileType:
flags |= ArchiveMember::BytecodeFlag; flags |= ArchiveMember::BytecodeFlag;
break; break;
@ -261,40 +263,7 @@ Archive::writeMember(
} }
} }
// Determine if we actually should compress this member int hdrSize = fSize;
bool willCompress =
(ShouldCompress &&
!member.isCompressed() &&
!member.isCompressedBytecode() &&
!member.isLLVMSymbolTable() &&
!member.isSVR4SymbolTable() &&
!member.isBSD4SymbolTable());
// Perform the compression. Note that if the file is uncompressed bytecode
// then we turn the file into compressed bytecode rather than treating it as
// compressed data. This is necessary since it allows us to determine that the
// file contains bytecode instead of looking like a regular compressed data
// member. A compressed bytecode file has its content compressed but has a
// magic number of "llvc". This acounts for the +/-4 arithmetic in the code
// below.
int hdrSize;
if (willCompress) {
char* output = 0;
if (member.isBytecode()) {
data +=4;
fSize -= 4;
}
fSize = Compressor::compressToNewBuffer(data,fSize,output,ErrMsg);
if (fSize == 0)
return true;
data = output;
if (member.isBytecode())
hdrSize = -fSize-4;
else
hdrSize = -fSize;
} else {
hdrSize = fSize;
}
// Compute the fields of the header // Compute the fields of the header
ArchiveMemberHeader Hdr; ArchiveMemberHeader Hdr;
@ -309,10 +278,6 @@ Archive::writeMember(
member.getPath().toString().length()); member.getPath().toString().length());
} }
// Make sure we write the compressed bytecode magic number if we should.
if (willCompress && member.isBytecode())
ARFile.write("llvc",4);
// Write the (possibly compressed) member's content to the file. // Write the (possibly compressed) member's content to the file.
ARFile.write(data,fSize); ARFile.write(data,fSize);
@ -320,11 +285,6 @@ Archive::writeMember(
if ((ARFile.tellp() & 1) == 1) if ((ARFile.tellp() & 1) == 1)
ARFile << ARFILE_PAD; ARFile << ARFILE_PAD;
// Free the compressed data, if necessary
if (willCompress) {
free((void*)data);
}
// Close the mapped file if it was opened // Close the mapped file if it was opened
if (mFile != 0) { if (mFile != 0) {
mFile->close(); mFile->close();