* Emit bytecode using a deque instead of a vector to be faster

* Internal rep no longer has a constant pool
* Support emission of recursive types
* Don't output a constant pool for an external method
* The bytecode writer is no longer a module analyzer


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@449 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Chris Lattner 2001-09-07 16:39:41 +00:00
parent 1d670cc402
commit e8fdde179d
4 changed files with 114 additions and 73 deletions

View File

@ -81,10 +81,10 @@ void BytecodeWriter::outputType(const Type *T) {
} }
case Type::ModuleTyID: case Type::ModuleTyID:
case Type::PackedTyID: //case Type::PackedTyID:
default: default:
cerr << __FILE__ << ":" << __LINE__ << ": Don't know how to serialize" cerr << __FILE__ << ":" << __LINE__ << ": Don't know how to serialize"
<< " Type '" << T->getName() << "'\n"; << " Type '" << T->getDescription() << "'\n";
break; break;
} }
} }
@ -113,7 +113,7 @@ bool BytecodeWriter::outputConstant(const ConstPoolVal *CPV) {
break; break;
case Type::TypeTyID: // Serialize type type case Type::TypeTyID: // Serialize type type
outputType(((const ConstPoolType*)CPV)->getValue()); assert(0 && "Types should not be in the ConstPool!");
break; break;
case Type::ArrayTyID: { case Type::ArrayTyID: {
@ -123,7 +123,7 @@ bool BytecodeWriter::outputConstant(const ConstPoolVal *CPV) {
output_vbr(size, Out); // Not for sized arrays!!! output_vbr(size, Out); // Not for sized arrays!!!
for (unsigned i = 0; i < size; i++) { for (unsigned i = 0; i < size; i++) {
int Slot = Table.getValSlot(CPA->getValues()[i]); int Slot = Table.getValSlot(CPA->getOperand(i));
assert(Slot != -1 && "Constant used but not available!!"); assert(Slot != -1 && "Constant used but not available!!");
output_vbr((unsigned)Slot, Out); output_vbr((unsigned)Slot, Out);
} }

View File

@ -26,7 +26,7 @@ typedef unsigned char uchar;
// //
static void outputInstructionFormat0(const Instruction *I, static void outputInstructionFormat0(const Instruction *I,
const SlotCalculator &Table, const SlotCalculator &Table,
unsigned Type, vector<uchar> &Out) { unsigned Type, deque<uchar> &Out) {
// Opcode must have top two bits clear... // Opcode must have top two bits clear...
output_vbr(I->getOpcode(), Out); // Instruction Opcode ID output_vbr(I->getOpcode(), Out); // Instruction Opcode ID
output_vbr(Type, Out); // Result type output_vbr(Type, Out); // Result type
@ -54,7 +54,7 @@ static void outputInstructionFormat0(const Instruction *I,
// //
static void outputInstrVarArgsCall(const Instruction *I, static void outputInstrVarArgsCall(const Instruction *I,
const SlotCalculator &Table, unsigned Type, const SlotCalculator &Table, unsigned Type,
vector<uchar> &Out) { deque<uchar> &Out) {
assert(I->getOpcode() == Instruction::Call /*|| assert(I->getOpcode() == Instruction::Call /*||
I->getOpcode() == Instruction::ICall */); I->getOpcode() == Instruction::ICall */);
// Opcode must have top two bits clear... // Opcode must have top two bits clear...
@ -94,7 +94,7 @@ static void outputInstrVarArgsCall(const Instruction *I,
// //
static void outputInstructionFormat1(const Instruction *I, static void outputInstructionFormat1(const Instruction *I,
const SlotCalculator &Table, int *Slots, const SlotCalculator &Table, int *Slots,
unsigned Type, vector<uchar> &Out) { unsigned Type, deque<uchar> &Out) {
unsigned IType = I->getOpcode(); // Instruction Opcode ID unsigned IType = I->getOpcode(); // Instruction Opcode ID
// bits Instruction format: // bits Instruction format:
@ -115,7 +115,7 @@ static void outputInstructionFormat1(const Instruction *I,
// //
static void outputInstructionFormat2(const Instruction *I, static void outputInstructionFormat2(const Instruction *I,
const SlotCalculator &Table, int *Slots, const SlotCalculator &Table, int *Slots,
unsigned Type, vector<uchar> &Out) { unsigned Type, deque<uchar> &Out) {
unsigned IType = I->getOpcode(); // Instruction Opcode ID unsigned IType = I->getOpcode(); // Instruction Opcode ID
// bits Instruction format: // bits Instruction format:
@ -139,7 +139,7 @@ static void outputInstructionFormat2(const Instruction *I,
// //
static void outputInstructionFormat3(const Instruction *I, static void outputInstructionFormat3(const Instruction *I,
const SlotCalculator &Table, int *Slots, const SlotCalculator &Table, int *Slots,
unsigned Type, vector<uchar> &Out) { unsigned Type, deque<uchar> &Out) {
unsigned IType = I->getOpcode(); // Instruction Opcode ID unsigned IType = I->getOpcode(); // Instruction Opcode ID
// bits Instruction format: // bits Instruction format:
@ -158,7 +158,9 @@ static void outputInstructionFormat3(const Instruction *I,
output(Opcode, Out); output(Opcode, Out);
} }
bool BytecodeWriter::processInstruction(const Instruction *I) { #include "llvm/Assembly/Writer.h"
void BytecodeWriter::processInstruction(const Instruction *I) {
assert(I->getOpcode() < 64 && "Opcode too big???"); assert(I->getOpcode() < 64 && "Opcode too big???");
unsigned NumOperands = I->getNumOperands(); unsigned NumOperands = I->getNumOperands();
@ -215,7 +217,7 @@ bool BytecodeWriter::processInstruction(const Instruction *I) {
} else if (I->getOpcode() == Instruction::Call && // Handle VarArg calls } else if (I->getOpcode() == Instruction::Call && // Handle VarArg calls
I->getOperand(0)->getType()->isMethodType()->isVarArg()) { I->getOperand(0)->getType()->isMethodType()->isVarArg()) {
outputInstrVarArgsCall(I, Table, Type, Out); outputInstrVarArgsCall(I, Table, Type, Out);
return false; return;
} }
// Decide which instruction encoding to use. This is determined primarily by // Decide which instruction encoding to use. This is determined primarily by
@ -228,21 +230,21 @@ bool BytecodeWriter::processInstruction(const Instruction *I) {
case 1: case 1:
if (MaxOpSlot < (1 << 12)-1) { // -1 because we use 4095 to indicate 0 ops if (MaxOpSlot < (1 << 12)-1) { // -1 because we use 4095 to indicate 0 ops
outputInstructionFormat1(I, Table, Slots, Type, Out); outputInstructionFormat1(I, Table, Slots, Type, Out);
return false; return;
} }
break; break;
case 2: case 2:
if (MaxOpSlot < (1 << 8)) { if (MaxOpSlot < (1 << 8)) {
outputInstructionFormat2(I, Table, Slots, Type, Out); outputInstructionFormat2(I, Table, Slots, Type, Out);
return false; return;
} }
break; break;
case 3: case 3:
if (MaxOpSlot < (1 << 6)) { if (MaxOpSlot < (1 << 6)) {
outputInstructionFormat3(I, Table, Slots, Type, Out); outputInstructionFormat3(I, Table, Slots, Type, Out);
return false; return;
} }
break; break;
} }
@ -250,5 +252,4 @@ bool BytecodeWriter::processInstruction(const Instruction *I) {
// If we weren't handled before here, we either have a large number of // If we weren't handled before here, we either have a large number of
// operands or a large operand index that we are refering to. // operands or a large operand index that we are refering to.
outputInstructionFormat0(I, Table, Type, Out); outputInstructionFormat0(I, Table, Type, Out);
return false;
} }

View File

@ -6,13 +6,15 @@
// variables in the method tables... // variables in the method tables...
// //
// Note that this file uses an unusual technique of outputting all the bytecode // Note that this file uses an unusual technique of outputting all the bytecode
// to a vector of unsigned char's, then copies the vector to an ostream. The // to a deque of unsigned char's, then copies the deque to an ostream. The
// reason for this is that we must do "seeking" in the stream to do back- // reason for this is that we must do "seeking" in the stream to do back-
// patching, and some very important ostreams that we want to support (like // patching, and some very important ostreams that we want to support (like
// pipes) do not support seeking. :( :( :( // pipes) do not support seeking. :( :( :(
// //
// The choice of the vector data structure is influenced by the extremely fast // The choice of the deque data structure is influenced by the extremely fast
// "append" speed, plus the free "seek"/replace in the middle of the stream. // "append" speed, plus the free "seek"/replace in the middle of the stream. I
// didn't use a vector because the stream could end up very large and copying
// the whole thing to reallocate would be kinda silly.
// //
// Note that the performance of this library is not terribly important, because // 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 // it shouldn't be used by JIT type applications... so it is not a huge focus
@ -27,10 +29,11 @@
#include "llvm/ConstPoolVals.h" #include "llvm/ConstPoolVals.h"
#include "llvm/SymbolTable.h" #include "llvm/SymbolTable.h"
#include "llvm/DerivedTypes.h" #include "llvm/DerivedTypes.h"
#include "llvm/Support/STLExtras.h"
#include <string.h> #include <string.h>
#include <algorithm> #include <algorithm>
BytecodeWriter::BytecodeWriter(vector<unsigned char> &o, const Module *M) BytecodeWriter::BytecodeWriter(deque<unsigned char> &o, const Module *M)
: Out(o), Table(M, false) { : Out(o), Table(M, false) {
outputSignature(); outputSignature();
@ -38,14 +41,21 @@ BytecodeWriter::BytecodeWriter(vector<unsigned char> &o, const Module *M)
// Emit the top level CLASS block. // Emit the top level CLASS block.
BytecodeBlock ModuleBlock(BytecodeFormat::Module, Out); BytecodeBlock ModuleBlock(BytecodeFormat::Module, Out);
// Output largest ID of first "primitive" type: // Output the ID of first "derived" type:
output_vbr((unsigned)Type::FirstDerivedTyID, Out); output_vbr((unsigned)Type::FirstDerivedTyID, Out);
align32(Out); align32(Out);
// Do the whole module now! // Output module level constants, including types used by the method protos
processModule(M); outputConstants(false);
// If needed, output the symbol table for the class... // The ModuleInfoBlock follows directly after the Module constant pool
outputModuleInfoBlock(M);
// Do the whole module now! Process each method at a time...
for_each(M->begin(), M->end(),
bind_obj(this, &BytecodeWriter::processMethod));
// If needed, output the symbol table for the module...
if (M->hasSymbolTable()) if (M->hasSymbolTable())
outputSymbolTable(*M->getSymbolTable()); outputSymbolTable(*M->getSymbolTable());
} }
@ -53,53 +63,55 @@ BytecodeWriter::BytecodeWriter(vector<unsigned char> &o, const Module *M)
// TODO: REMOVE // TODO: REMOVE
#include "llvm/Assembly/Writer.h" #include "llvm/Assembly/Writer.h"
bool BytecodeWriter::processConstPool(const ConstantPool &CP, bool isMethod) { void BytecodeWriter::outputConstants(bool isMethod) {
BytecodeBlock *CPool = new BytecodeBlock(BytecodeFormat::ConstantPool, Out); BytecodeBlock CPool(BytecodeFormat::ConstantPool, Out);
unsigned NumPlanes = Table.getNumPlanes(); unsigned NumPlanes = Table.getNumPlanes();
for (unsigned pno = 0; pno < NumPlanes; pno++) { for (unsigned pno = 0; pno < NumPlanes; pno++) {
const vector<const Value*> &Plane = Table.getPlane(pno); const vector<const Value*> &Plane = Table.getPlane(pno);
if (Plane.empty()) continue; // Skip empty type planes... if (Plane.empty()) continue; // Skip empty type planes...
unsigned ValNo = 0; // Don't reemit module constants unsigned ValNo = 0;
if (isMethod) ValNo = Table.getModuleLevel(pno); if (isMethod) // Don't reemit module constants
ValNo = Table.getModuleLevel(pno);
else if (pno == Type::TypeTyID)
ValNo = Type::FirstDerivedTyID; // Start emitting at the derived types...
unsigned NumConstants = 0; // Scan through and ignore method arguments...
for (unsigned vn = ValNo; vn < Plane.size(); vn++) for (; ValNo < Plane.size() && Plane[ValNo]->isMethodArgument(); ValNo++)
if (Plane[vn]->isConstant()) /*empty*/;
NumConstants++;
if (NumConstants == 0) continue; // Skip empty type planes... unsigned NC = ValNo; // Number of constants
for (; NC < Plane.size() &&
(Plane[NC]->isConstant() || Plane[NC]->isType()); 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 type header: [num entries][type id number]
// //
output_vbr(NumConstants, Out); output_vbr(NC, Out);
// Output the Type ID Number... // Output the Type ID Number...
int Slot = Table.getValSlot(Plane.front()->getType()); int Slot = Table.getValSlot(Plane.front()->getType());
assert (Slot != -1 && "Type in constant pool but not in method!!"); assert (Slot != -1 && "Type in constant pool but not in method!!");
output_vbr((unsigned)Slot, Out); output_vbr((unsigned)Slot, Out);
//cerr << "NC: " << NumConstants << " Slot = " << hex << Slot << endl; //cout << "Emitting " << NC << " constants of type '"
// << Plane.front()->getType()->getName() << "' = Slot #" << Slot << endl;
for (; ValNo < Plane.size(); ValNo++) { for (unsigned i = ValNo; i < ValNo+NC; ++i) {
const Value *V = Plane[ValNo]; const Value *V = Plane[i];
if (const ConstPoolVal *CPV = V->castConstant()) { if (const ConstPoolVal *CPV = V->castConstant()) {
//cerr << "Serializing value: <" << V->getType() << ">: " //cerr << "Serializing value: <" << V->getType() << ">: "
// << ((const ConstPoolVal*)V)->getStrValue() << ":" // << ((const ConstPoolVal*)V)->getStrValue() << ":"
// << Out.size() << "\n"; // << Out.size() << "\n";
outputConstant(CPV); outputConstant(CPV);
} else {
const Type *Ty = V->castTypeAsserting();
outputType(Ty);
} }
} }
} }
delete CPool; // End bytecode block section!
if (!isMethod) // The ModuleInfoBlock follows directly after the c-pool
outputModuleInfoBlock(CP.getParentV()->castModuleAsserting());
return false;
} }
void BytecodeWriter::outputModuleInfoBlock(const Module *M) { void BytecodeWriter::outputModuleInfoBlock(const Module *M) {
@ -116,25 +128,35 @@ void BytecodeWriter::outputModuleInfoBlock(const Module *M) {
align32(Out); align32(Out);
} }
bool BytecodeWriter::processMethod(const Method *M) { void BytecodeWriter::processMethod(const Method *M) {
BytecodeBlock MethodBlock(BytecodeFormat::Method, Out); BytecodeBlock MethodBlock(BytecodeFormat::Method, Out);
Table.incorporateMethod(M); // Only output the constant pool and other goodies if needed...
if (!M->isExternal()) {
// Get slot information about the method...
Table.incorporateMethod(M);
if (ModuleAnalyzer::processMethod(M)) return true; // Output information about the constants in the method...
outputConstants(true);
// If needed, output the symbol table for the method...
if (M->hasSymbolTable())
outputSymbolTable(*M->getSymbolTable());
Table.purgeMethod(); // Output basic block nodes...
return false; for_each(M->begin(), M->end(),
bind_obj(this, &BytecodeWriter::processBasicBlock));
// If needed, output the symbol table for the method...
if (M->hasSymbolTable())
outputSymbolTable(*M->getSymbolTable());
Table.purgeMethod();
}
} }
bool BytecodeWriter::processBasicBlock(const BasicBlock *BB) { void BytecodeWriter::processBasicBlock(const BasicBlock *BB) {
BytecodeBlock MethodBlock(BytecodeFormat::BasicBlock, Out); BytecodeBlock MethodBlock(BytecodeFormat::BasicBlock, Out);
return ModuleAnalyzer::processBasicBlock(BB); // Process all the instructions in the bb...
for_each(BB->begin(), BB->end(),
bind_obj(this, &BytecodeWriter::processInstruction));
} }
void BytecodeWriter::outputSymbolTable(const SymbolTable &MST) { void BytecodeWriter::outputSymbolTable(const SymbolTable &MST) {
@ -157,7 +179,7 @@ void BytecodeWriter::outputSymbolTable(const SymbolTable &MST) {
for (; I != End; ++I) { for (; I != End; ++I) {
// Symtab entry: [def slot #][name] // Symtab entry: [def slot #][name]
Slot = Table.getValSlot(I->second); Slot = Table.getValSlot(I->second);
assert (Slot != -1 && "Value in symtab but not in method!!"); assert(Slot != -1 && "Value in symtab but has no slot number!!");
output_vbr((unsigned)Slot, Out); output_vbr((unsigned)Slot, Out);
output(I->first, Out, false); // Don't force alignment... output(I->first, Out, false); // Don't force alignment...
} }
@ -165,14 +187,31 @@ void BytecodeWriter::outputSymbolTable(const SymbolTable &MST) {
} }
void WriteBytecodeToFile(const Module *C, ostream &Out) { void WriteBytecodeToFile(const Module *C, ostream &Out) {
assert(C && "You can't write a null class!!"); assert(C && "You can't write a null module!!");
vector<unsigned char> Buffer; deque<unsigned char> Buffer;
// This object populates buffer for us... // This object populates buffer for us...
BytecodeWriter BCW(Buffer, C); BytecodeWriter BCW(Buffer, C);
// Okay, write the vector out to the ostream now... // Okay, write the deque out to the ostream now... the deque is not
Out.write(&Buffer[0], Buffer.size()); // sequential in memory, however, so write out as much as possible in big
// chunks, until we're done.
//
deque<unsigned char>::const_iterator I = Buffer.begin(), E = Buffer.end();
while (I != E) { // Loop until it's all written
// Scan to see how big this chunk is...
const unsigned char *ChunkPtr = &*I;
const unsigned char *LastPtr = ChunkPtr;
while (I != E) {
const unsigned char *ThisPtr = &*++I;
if (LastPtr+1 != ThisPtr) break;// Advanced by more than a byte of memory?
LastPtr = ThisPtr;
}
// Write out the chunk...
Out.write(ChunkPtr, LastPtr-ChunkPtr+(I != E));
}
Out.flush(); Out.flush();
} }

View File

@ -14,22 +14,22 @@
#include "llvm/Bytecode/Writer.h" #include "llvm/Bytecode/Writer.h"
#include "llvm/Bytecode/Format.h" #include "llvm/Bytecode/Format.h"
#include "llvm/Bytecode/Primitives.h"
#include "llvm/Analysis/SlotCalculator.h" #include "llvm/Analysis/SlotCalculator.h"
#include "llvm/Support/DataTypes.h" #include "llvm/Bytecode/Primitives.h"
#include "llvm/Instruction.h" #include "llvm/Instruction.h"
#include <deque>
class BytecodeWriter : public ModuleAnalyzer { class BytecodeWriter {
vector<unsigned char> &Out; deque<unsigned char> &Out;
SlotCalculator Table; SlotCalculator Table;
public: public:
BytecodeWriter(vector<unsigned char> &o, const Module *M); BytecodeWriter(deque<unsigned char> &o, const Module *M);
protected: protected:
virtual bool processConstPool(const ConstantPool &CP, bool isMethod); void outputConstants(bool isMethod);
virtual bool processMethod(const Method *M); void processMethod(const Method *M);
virtual bool processBasicBlock(const BasicBlock *BB); void processBasicBlock(const BasicBlock *BB);
virtual bool processInstruction(const Instruction *I); void processInstruction(const Instruction *I);
private : private :
inline void outputSignature() { inline void outputSignature() {
@ -51,12 +51,12 @@ private :
// //
class BytecodeBlock { class BytecodeBlock {
unsigned Loc; unsigned Loc;
vector<unsigned char> &Out; deque<unsigned char> &Out;
BytecodeBlock(const BytecodeBlock &); // do not implement BytecodeBlock(const BytecodeBlock &); // do not implement
void operator=(const BytecodeBlock &); // do not implement void operator=(const BytecodeBlock &); // do not implement
public: public:
inline BytecodeBlock(unsigned ID, vector<unsigned char> &o) : Out(o) { inline BytecodeBlock(unsigned ID, deque<unsigned char> &o) : Out(o) {
output(ID, Out); output(ID, Out);
output((unsigned)0, Out); // Reserve the space for the block size... output((unsigned)0, Out); // Reserve the space for the block size...
Loc = Out.size(); Loc = Out.size();
@ -64,7 +64,8 @@ public:
inline ~BytecodeBlock() { // Do backpatch when block goes out inline ~BytecodeBlock() { // Do backpatch when block goes out
// of scope... // of scope...
// cerr << "OldLoc = " << Loc << " NewLoc = " << NewLoc << " diff = " << (NewLoc-Loc) << endl; //cerr << "OldLoc = " << Loc << " NewLoc = " << NewLoc << " diff = "
// << (NewLoc-Loc) << endl;
output((unsigned)(Out.size()-Loc), Out, (int)Loc-4); output((unsigned)(Out.size()-Loc), Out, (int)Loc-4);
align32(Out); // Blocks must ALWAYS be aligned align32(Out); // Blocks must ALWAYS be aligned
} }