From 428bf5abc9aba53c3a8452f6eea0b127a9b3891f Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Wed, 19 Mar 2003 20:56:46 +0000 Subject: Changes to the V2 bytecode format: - Null values are implicitly encoded instead of explicitly, this makes things more compact! - More compactly represent ConstantPointerRefs - Bytecode files are represented as: Header|GlobalTypes|GlobalVars/Function Protos|Constants|Functions|SymTab instead of Header|GlobalTypes|Constants|GlobalVars/Function Protos|Functions|SymTab which makes a lot of things simpler. Writer changes: - We now explictly encode versioning information in the bytecode files. - This allows new code to read bytecode files produced by old code, but new bytecode files can have enhancements such as the above. Although this makes the reader a bit more complex (having to deal with old formats), the writer only needs to be able to produce the most recent version. llvm-svn: 5749 --- llvm/lib/Bytecode/Writer/Writer.cpp | 64 ++++++++++++++++++++++--------------- 1 file changed, 38 insertions(+), 26 deletions(-) (limited to 'llvm/lib/Bytecode/Writer/Writer.cpp') diff --git a/llvm/lib/Bytecode/Writer/Writer.cpp b/llvm/lib/Bytecode/Writer/Writer.cpp index 92b6a62cc60..8f4f785c4d2 100644 --- a/llvm/lib/Bytecode/Writer/Writer.cpp +++ b/llvm/lib/Bytecode/Writer/Writer.cpp @@ -43,19 +43,36 @@ BytecodeWriter::BytecodeWriter(std::deque &o, const Module *M) // Emit the top level CLASS block. BytecodeBlock ModuleBlock(BytecodeFormat::Module, Out); - // Output the ID of first "derived" type: - output_vbr((unsigned)Type::FirstDerivedTyID, Out); + bool isBigEndian = true; + bool hasLongPointers = true; + + // Output the version identifier... we are currently on bytecode version #1 + unsigned Version = (1 << 4) | isBigEndian | (hasLongPointers << 1); + output_vbr(Version, Out); align32(Out); - // Output module level constants, including types used by the function protos - outputConstants(false); + { + BytecodeBlock CPool(BytecodeFormat::GlobalTypePlane, Out); + + // Write the type plane for types first because earlier planes (e.g. for a + // primitive type like float) may have constants constructed using types + // coming later (e.g., via getelementptr from a pointer type). The type + // plane is needed before types can be fwd or bkwd referenced. + const std::vector &Plane = Table.getPlane(Type::TypeTyID); + assert(!Plane.empty() && "No types at all?"); + unsigned ValNo = Type::FirstDerivedTyID; // Start at the derived types... + outputConstantsInPlane(Plane, ValNo); // Write out the types + } - // The ModuleInfoBlock follows directly after the Module constant pool + // The ModuleInfoBlock follows directly after the type information outputModuleInfoBlock(M); + // Output module level constants, used for global variable initializers + outputConstants(false); + // Do the whole module now! Process each function at a time... for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I) - processMethod(I); + outputFunction(I); // If needed, output the symbol table for the module... outputSymbolTable(M->getSymbolTable()); @@ -68,8 +85,9 @@ void BytecodeWriter::outputConstantsInPlane(const std::vector &Plane, unsigned StartNo) { unsigned ValNo = StartNo; - // Scan through and ignore function arguments... - for (; ValNo < Plane.size() && isa(Plane[ValNo]); ValNo++) + // Scan through and ignore function arguments/global values... + for (; ValNo < Plane.size() && (isa(Plane[ValNo]) || + isa(Plane[ValNo])); ValNo++) /*empty*/; unsigned NC = ValNo; // Number of constants @@ -98,7 +116,7 @@ void BytecodeWriter::outputConstantsInPlane(const std::vector // << Out.size() << "\n"; outputConstant(CPV); } else { - outputType(cast(V)); + outputType(cast(V)); } } } @@ -108,26 +126,21 @@ void BytecodeWriter::outputConstants(bool isFunction) { unsigned NumPlanes = Table.getNumPlanes(); - // Write the type plane for types first because earlier planes - // (e.g. for a primitive type like float) may have constants constructed - // using types coming later (e.g., via getelementptr from a pointer type). - // The type plane is needed before types can be fwd or bkwd referenced. - if (!isFunction) { - const std::vector &Plane = Table.getPlane(Type::TypeTyID); - assert(!Plane.empty() && "No types at all?"); - unsigned ValNo = Type::FirstDerivedTyID; // Start at the derived types... - outputConstantsInPlane(Plane, ValNo); // Write out the types - } - for (unsigned pno = 0; pno != NumPlanes; pno++) { const std::vector &Plane = Table.getPlane(pno); - if (!Plane.empty()) { // Skip empty type planes... + if (!Plane.empty()) { // Skip empty type planes... unsigned ValNo = 0; - if (isFunction) // Don't reemit module constants - ValNo = Table.getModuleLevel(pno); + if (isFunction) // Don't reemit module constants + ValNo += Table.getModuleLevel(pno); else if (pno == Type::TypeTyID) // If type plane wasn't written out above continue; + if (pno >= Type::FirstDerivedTyID) { + // Skip zero initializer + if (ValNo == 0) + ValNo = 1; + } + outputConstantsInPlane(Plane, ValNo); // Write out constants in the plane } } @@ -142,7 +155,7 @@ void BytecodeWriter::outputModuleInfoBlock(const Module *M) { assert(Slot != -1 && "Module global vars is broken!"); // Fields: bit0 = isConstant, bit1 = hasInitializer, bit2=InternalLinkage, - // bit3+ = slot# + // bit3+ = Slot # for type unsigned oSlot = ((unsigned)Slot << 3) | (I->hasInternalLinkage() << 2) | (I->hasInitializer() << 1) | I->isConstant(); output_vbr(oSlot, Out); @@ -165,11 +178,10 @@ void BytecodeWriter::outputModuleInfoBlock(const Module *M) { } output_vbr((unsigned)Table.getValSlot(Type::VoidTy), Out); - align32(Out); } -void BytecodeWriter::processMethod(const Function *F) { +void BytecodeWriter::outputFunction(const Function *F) { BytecodeBlock FunctionBlock(BytecodeFormat::Function, Out); output_vbr((unsigned)F->hasInternalLinkage(), Out); // Only output the constant pool and other goodies if needed... -- cgit v1.2.3