Read and write section info from/to .bc files

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@24321 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Chris Lattner 2005-11-12 01:33:40 +00:00
parent ba0ffcc1bc
commit 404cddfcf9
3 changed files with 80 additions and 20 deletions

View File

@ -1891,6 +1891,10 @@ void BytecodeReader::ParseModuleGlobalInfo() {
if (Handler) Handler->handleModuleGlobalsBegin(); if (Handler) Handler->handleModuleGlobalsBegin();
// SectionID - If a global has an explicit section specified, this map
// remembers the ID until we can translate it into a string.
std::map<GlobalValue*, unsigned> SectionID;
// Read global variables... // Read global variables...
unsigned VarType = read_vbr_uint(); unsigned VarType = read_vbr_uint();
while (VarType != Type::VoidTyID) { // List is terminated by Void while (VarType != Type::VoidTyID) { // List is terminated by Void
@ -1903,6 +1907,7 @@ void BytecodeReader::ParseModuleGlobalInfo() {
bool isConstant = VarType & 1; bool isConstant = VarType & 1;
bool hasInitializer = (VarType & 2) != 0; bool hasInitializer = (VarType & 2) != 0;
unsigned Alignment = 0; unsigned Alignment = 0;
unsigned GlobalSectionID = 0;
// An extension word is present when linkage = 3 (internal) and hasinit = 0. // An extension word is present when linkage = 3 (internal) and hasinit = 0.
if (LinkageID == 3 && !hasInitializer) { if (LinkageID == 3 && !hasInitializer) {
@ -1912,6 +1917,9 @@ void BytecodeReader::ParseModuleGlobalInfo() {
hasInitializer = ExtWord & 1; hasInitializer = ExtWord & 1;
LinkageID = (ExtWord >> 1) & 7; LinkageID = (ExtWord >> 1) & 7;
Alignment = (1 << ((ExtWord >> 4) & 31)) >> 1; Alignment = (1 << ((ExtWord >> 4) & 31)) >> 1;
if (ExtWord & (1 << 9)) // Has a section ID.
GlobalSectionID = read_vbr_uint();
} }
GlobalValue::LinkageTypes Linkage; GlobalValue::LinkageTypes Linkage;
@ -1942,6 +1950,9 @@ void BytecodeReader::ParseModuleGlobalInfo() {
GV->setAlignment(Alignment); GV->setAlignment(Alignment);
insertValue(GV, SlotNo, ModuleValues); insertValue(GV, SlotNo, ModuleValues);
if (GlobalSectionID != 0)
SectionID[GV] = GlobalSectionID;
unsigned initSlot = 0; unsigned initSlot = 0;
if (hasInitializer) { if (hasInitializer) {
initSlot = read_vbr_uint(); initSlot = read_vbr_uint();
@ -1975,9 +1986,8 @@ void BytecodeReader::ParseModuleGlobalInfo() {
const FunctionType* FTy = const FunctionType* FTy =
cast<FunctionType>(cast<PointerType>(Ty)->getElementType()); cast<FunctionType>(cast<PointerType>(Ty)->getElementType());
// Insert the place holder. // Insert the place holder.
Function* Func = new Function(FTy, GlobalValue::ExternalLinkage, Function *Func = new Function(FTy, GlobalValue::ExternalLinkage,
"", TheModule); "", TheModule);
insertValue(Func, (FnSignature & (~0U >> 1)) >> 5, ModuleValues); insertValue(Func, (FnSignature & (~0U >> 1)) >> 5, ModuleValues);
@ -1997,6 +2007,9 @@ void BytecodeReader::ParseModuleGlobalInfo() {
unsigned ExtWord = read_vbr_uint(); unsigned ExtWord = read_vbr_uint();
Alignment = (1 << (ExtWord & 31)) >> 1; Alignment = (1 << (ExtWord & 31)) >> 1;
CC |= ((ExtWord >> 5) & 15) << 4; CC |= ((ExtWord >> 5) & 15) << 4;
if (ExtWord & (1 << 10)) // Has a section ID.
SectionID[Func] = read_vbr_uint();
} }
Func->setCallingConv(CC-1); Func->setCallingConv(CC-1);
@ -2014,28 +2027,47 @@ void BytecodeReader::ParseModuleGlobalInfo() {
// remove elements efficiently from the back of the vector. // remove elements efficiently from the back of the vector.
std::reverse(FunctionSignatureList.begin(), FunctionSignatureList.end()); std::reverse(FunctionSignatureList.begin(), FunctionSignatureList.end());
// If this bytecode format has dependent library information in it .. /// SectionNames - This contains the list of section names encoded in the
if (!hasNoDependentLibraries) { /// moduleinfoblock. Functions and globals with an explicit section index
// Read in the number of dependent library items that follow /// into this to get their section name.
std::vector<std::string> SectionNames;
if (hasInconsistentModuleGlobalInfo) {
align32();
} else if (!hasNoDependentLibraries) {
// If this bytecode format has dependent library information in it, read in
// the number of dependent library items that follow.
unsigned num_dep_libs = read_vbr_uint(); unsigned num_dep_libs = read_vbr_uint();
std::string dep_lib; std::string dep_lib;
while( num_dep_libs-- ) { while (num_dep_libs--) {
dep_lib = read_str(); dep_lib = read_str();
TheModule->addLibrary(dep_lib); TheModule->addLibrary(dep_lib);
if (Handler) if (Handler)
Handler->handleDependentLibrary(dep_lib); Handler->handleDependentLibrary(dep_lib);
} }
// Read target triple and place into the module.
// Read target triple and place into the module
std::string triple = read_str(); std::string triple = read_str();
TheModule->setTargetTriple(triple); TheModule->setTargetTriple(triple);
if (Handler) if (Handler)
Handler->handleTargetTriple(triple); Handler->handleTargetTriple(triple);
if (At != BlockEnd) {
// If the file has section info in it, read the section names now.
unsigned NumSections = read_vbr_uint();
while (NumSections--)
SectionNames.push_back(read_str());
}
} }
if (hasInconsistentModuleGlobalInfo) // If any globals are in specified sections, assign them now.
align32(); for (std::map<GlobalValue*, unsigned>::iterator I = SectionID.begin(), E =
SectionID.end(); I != E; ++I)
if (I->second) {
if (I->second > SectionID.size())
error("SectionID out of range for global!");
I->first->setSection(SectionNames[I->second-1]);
}
// This is for future proofing... in the future extra fields may be added that // This is for future proofing... in the future extra fields may be added that
// we don't understand, so we transparently ignore them. // we don't understand, so we transparently ignore them.

View File

@ -922,6 +922,11 @@ static unsigned getEncodedLinkage(const GlobalValue *GV) {
void BytecodeWriter::outputModuleInfoBlock(const Module *M) { void BytecodeWriter::outputModuleInfoBlock(const Module *M) {
BytecodeBlock ModuleInfoBlock(BytecodeFormat::ModuleGlobalInfoBlockID, *this); BytecodeBlock ModuleInfoBlock(BytecodeFormat::ModuleGlobalInfoBlockID, *this);
// Give numbers to sections as we encounter them.
unsigned SectionIDCounter = 0;
std::vector<std::string> SectionNames;
std::map<std::string, unsigned> SectionID;
// Output the types for the global variables in the module... // Output the types for the global variables in the module...
for (Module::const_global_iterator I = M->global_begin(), for (Module::const_global_iterator I = M->global_begin(),
End = M->global_end(); I != End; ++I) { End = M->global_end(); I != End; ++I) {
@ -933,7 +938,7 @@ void BytecodeWriter::outputModuleInfoBlock(const Module *M) {
// Fields: bit0 = isConstant, bit1 = hasInitializer, bit2-4=Linkage, // Fields: bit0 = isConstant, bit1 = hasInitializer, bit2-4=Linkage,
// bit5+ = Slot # for type. // bit5+ = Slot # for type.
bool HasExtensionWord = I->getAlignment() != 0; bool HasExtensionWord = (I->getAlignment() != 0) || I->hasSection();
// If we need to use the extension byte, set linkage=3(internal) and // If we need to use the extension byte, set linkage=3(internal) and
// initializer = 0 (impossible!). // initializer = 0 (impossible!).
@ -947,11 +952,22 @@ void BytecodeWriter::outputModuleInfoBlock(const Module *M) {
output_vbr(oSlot); output_vbr(oSlot);
// The extension word has this format: bit 0 = has initializer, bit 1-3 = // The extension word has this format: bit 0 = has initializer, bit 1-3 =
// linkage, bit 4-8 = alignment (log2), bits 10+ = future use. // linkage, bit 4-8 = alignment (log2), bit 9 = has SectionID,
// bits 10+ = future use.
unsigned ExtWord = (unsigned)I->hasInitializer() | unsigned ExtWord = (unsigned)I->hasInitializer() |
(getEncodedLinkage(I) << 1) | (getEncodedLinkage(I) << 1) |
((Log2_32(I->getAlignment())+1) << 4); ((Log2_32(I->getAlignment())+1) << 4) |
((unsigned)I->hasSection() << 9);
output_vbr(ExtWord); output_vbr(ExtWord);
if (I->hasSection()) {
// Give section names unique ID's.
unsigned &Entry = SectionID[I->getSection()];
if (Entry == 0) {
Entry = ++SectionIDCounter;
SectionNames.push_back(I->getSection());
}
output_vbr(Entry);
}
} }
// If we have an initializer, output it now. // If we have an initializer, output it now.
@ -975,16 +991,27 @@ void BytecodeWriter::outputModuleInfoBlock(const Module *M) {
if (I->isExternal()) // If external, we don't have an FunctionInfo block. if (I->isExternal()) // If external, we don't have an FunctionInfo block.
ID |= 1 << 4; ID |= 1 << 4;
if (I->getAlignment() || (CC & ~15) != 0) if (I->getAlignment() || I->hasSection() || (CC & ~15) != 0)
ID |= 1 << 31; // Do we need an extension word? ID |= 1 << 31; // Do we need an extension word?
output_vbr(ID); output_vbr(ID);
if (ID & (1 << 31)) { if (ID & (1 << 31)) {
// Extension byte: bits 0-4 = alignment, bits 5-9 = top nibble of calling // Extension byte: bits 0-4 = alignment, bits 5-9 = top nibble of calling
// convention. // convention, bit 10 = hasSectionID.
ID = (Log2_32(I->getAlignment())+1) | ((CC >> 4) << 5); ID = (Log2_32(I->getAlignment())+1) | ((CC >> 4) << 5) |
(I->hasSection() << 10);
output_vbr(ID); output_vbr(ID);
// Give section names unique ID's.
if (I->hasSection()) {
unsigned &Entry = SectionID[I->getSection()];
if (Entry == 0) {
Entry = ++SectionIDCounter;
SectionNames.push_back(I->getSection());
}
output_vbr(Entry);
}
} }
} }
output_vbr((unsigned)Table.getSlot(Type::VoidTy) << 5); output_vbr((unsigned)Table.getSlot(Type::VoidTy) << 5);
@ -998,6 +1025,11 @@ void BytecodeWriter::outputModuleInfoBlock(const Module *M) {
// Output the target triple from the module // Output the target triple from the module
output(M->getTargetTriple()); output(M->getTargetTriple());
// Emit the table of section names.
output_vbr((unsigned)SectionNames.size());
for (unsigned i = 0, e = SectionNames.size(); i != e; ++i)
output(SectionNames[i]);
} }
void BytecodeWriter::outputInstructions(const Function *F) { void BytecodeWriter::outputInstructions(const Function *F) {

View File

@ -10,10 +10,6 @@
// This header defines the interface used between components of the bytecode // This header defines the interface used between components of the bytecode
// writer. // writer.
// //
// Note that the performance of this library is not terribly important, because
// it shouldn't be used by JIT type applications... so it is not a huge focus
// at least. :)
//
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
#ifndef LLVM_LIB_BYTECODE_WRITER_WRITERINTERNALS_H #ifndef LLVM_LIB_BYTECODE_WRITER_WRITERINTERNALS_H