From a7dac3db79a813e6f28bedeeafcecbba0c28115c Mon Sep 17 00:00:00 2001 From: "Vikram S. Adve" Date: Sun, 14 Jul 2002 23:07:51 +0000 Subject: [PATCH] Write out the plane for types first, since values of primitive types may be constructed by expressions of other types (and so the contents of the primitive type planes must come after all types). Use a helper function outputConstantsInPlane in outputConstants to do this. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@2898 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Bytecode/Writer/Writer.cpp | 92 +++++++++++++++++++++------------- 1 file changed, 57 insertions(+), 35 deletions(-) diff --git a/lib/Bytecode/Writer/Writer.cpp b/lib/Bytecode/Writer/Writer.cpp index 75a25f2015c..334622dfa23 100644 --- a/lib/Bytecode/Writer/Writer.cpp +++ b/lib/Bytecode/Writer/Writer.cpp @@ -54,53 +54,75 @@ BytecodeWriter::BytecodeWriter(std::deque &o, const Module *M) outputSymbolTable(*M->getSymbolTable()); } +// Helper function for outputConstants(). +// Writes out all the constants in the plane Plane starting at entry StartNo. +// +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++) + /*empty*/; + + unsigned NC = ValNo; // Number of constants + for (; NC < Plane.size() && + (isa(Plane[NC]) || isa(Plane[NC])); NC++) + /*empty*/; + NC -= ValNo; // Convert from index into count + if (NC == 0) return; // Skip empty type planes... + + // Output type header: [num entries][type id number] + // + output_vbr(NC, Out); + + // Output the Type ID Number... + int Slot = Table.getValSlot(Plane.front()->getType()); + assert (Slot != -1 && "Type in constant pool but not in function!!"); + output_vbr((unsigned)Slot, Out); + + //cerr << "Emitting " << NC << " constants of type '" + // << Plane.front()->getType()->getName() << "' = Slot #" << Slot << "\n"; + + for (unsigned i = ValNo; i < ValNo+NC; ++i) { + const Value *V = Plane[i]; + if (const Constant *CPV = dyn_cast(V)) { + //cerr << "Serializing value: <" << V->getType() << ">: " << V << ":" + // << Out.size() << "\n"; + outputConstant(CPV); + } else { + outputType(cast(V)); + } + } +} + void BytecodeWriter::outputConstants(bool isFunction) { BytecodeBlock CPool(BytecodeFormat::ConstantPool, Out); 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()) continue; // Skip empty type planes... - + unsigned ValNo = 0; if (isFunction) // Don't reemit module constants ValNo = Table.getModuleLevel(pno); else if (pno == Type::TypeTyID) - ValNo = Type::FirstDerivedTyID; // Start emitting at the derived types... + continue; // Type plane was written out above - // Scan through and ignore function arguments... - for (; ValNo < Plane.size() && isa(Plane[ValNo]); ValNo++) - /*empty*/; - - unsigned NC = ValNo; // Number of constants - for (; NC < Plane.size() && - (isa(Plane[NC]) || isa(Plane[NC])); NC++) - /*empty*/; - NC -= ValNo; // Convert from index into count - if (NC == 0) continue; // Skip empty type planes... - - // Output type header: [num entries][type id number] - // - output_vbr(NC, Out); - - // Output the Type ID Number... - int Slot = Table.getValSlot(Plane.front()->getType()); - assert (Slot != -1 && "Type in constant pool but not in function!!"); - output_vbr((unsigned)Slot, Out); - - //cerr << "Emitting " << NC << " constants of type '" - // << Plane.front()->getType()->getName() << "' = Slot #" << Slot << "\n"; - - for (unsigned i = ValNo; i < ValNo+NC; ++i) { - const Value *V = Plane[i]; - if (const Constant *CPV = dyn_cast(V)) { - //cerr << "Serializing value: <" << V->getType() << ">: " << V << ":" - // << Out.size() << "\n"; - outputConstant(CPV); - } else { - outputType(cast(V)); - } - } + outputConstantsInPlane(Plane, ValNo); // Write out constants in the plane } }