2001-06-07 04:29:01 +08:00
|
|
|
//===-- Writer.cpp - Library for writing VM bytecode files -------*- C++ -*--=//
|
|
|
|
//
|
|
|
|
// This library implements the functionality defined in llvm/Bytecode/Writer.h
|
|
|
|
//
|
|
|
|
// This library uses the Analysis library to figure out offsets for
|
|
|
|
// variables in the method tables...
|
|
|
|
//
|
|
|
|
// Note that this file uses an unusual technique of outputting all the bytecode
|
2001-09-08 00:39:41 +08:00
|
|
|
// to a deque of unsigned char's, then copies the deque to an ostream. The
|
2001-06-07 04:29:01 +08:00
|
|
|
// 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
|
|
|
|
// pipes) do not support seeking. :( :( :(
|
|
|
|
//
|
2001-09-08 00:39:41 +08:00
|
|
|
// 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. 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.
|
2001-06-07 04:29:01 +08:00
|
|
|
//
|
|
|
|
// 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. :)
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "WriterInternals.h"
|
|
|
|
#include "llvm/Module.h"
|
2001-09-10 15:58:01 +08:00
|
|
|
#include "llvm/GlobalVariable.h"
|
2001-06-07 04:29:01 +08:00
|
|
|
#include "llvm/Method.h"
|
|
|
|
#include "llvm/BasicBlock.h"
|
|
|
|
#include "llvm/ConstPoolVals.h"
|
|
|
|
#include "llvm/SymbolTable.h"
|
|
|
|
#include "llvm/DerivedTypes.h"
|
2001-09-08 00:39:41 +08:00
|
|
|
#include "llvm/Support/STLExtras.h"
|
2001-06-07 04:29:01 +08:00
|
|
|
#include <string.h>
|
|
|
|
#include <algorithm>
|
|
|
|
|
2001-09-08 00:39:41 +08:00
|
|
|
BytecodeWriter::BytecodeWriter(deque<unsigned char> &o, const Module *M)
|
2001-06-07 04:29:01 +08:00
|
|
|
: Out(o), Table(M, false) {
|
|
|
|
|
|
|
|
outputSignature();
|
|
|
|
|
|
|
|
// Emit the top level CLASS block.
|
|
|
|
BytecodeBlock ModuleBlock(BytecodeFormat::Module, Out);
|
|
|
|
|
2001-09-08 00:39:41 +08:00
|
|
|
// Output the ID of first "derived" type:
|
2001-06-07 04:29:01 +08:00
|
|
|
output_vbr((unsigned)Type::FirstDerivedTyID, Out);
|
|
|
|
align32(Out);
|
|
|
|
|
2001-09-08 00:39:41 +08:00
|
|
|
// Output module level constants, including types used by the method protos
|
|
|
|
outputConstants(false);
|
2001-06-07 04:29:01 +08:00
|
|
|
|
2001-09-08 00:39:41 +08:00
|
|
|
// 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...
|
2001-06-07 04:29:01 +08:00
|
|
|
if (M->hasSymbolTable())
|
|
|
|
outputSymbolTable(*M->getSymbolTable());
|
|
|
|
}
|
|
|
|
|
2001-09-08 00:39:41 +08:00
|
|
|
void BytecodeWriter::outputConstants(bool isMethod) {
|
|
|
|
BytecodeBlock CPool(BytecodeFormat::ConstantPool, Out);
|
2001-06-07 04:29:01 +08:00
|
|
|
|
|
|
|
unsigned NumPlanes = Table.getNumPlanes();
|
|
|
|
for (unsigned pno = 0; pno < NumPlanes; pno++) {
|
|
|
|
const vector<const Value*> &Plane = Table.getPlane(pno);
|
2001-09-08 00:39:41 +08:00
|
|
|
if (Plane.empty()) continue; // Skip empty type planes...
|
2001-06-07 04:29:01 +08:00
|
|
|
|
2001-09-08 00:39:41 +08:00
|
|
|
unsigned ValNo = 0;
|
|
|
|
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...
|
2001-06-07 04:29:01 +08:00
|
|
|
|
2001-09-08 00:39:41 +08:00
|
|
|
// Scan through and ignore method arguments...
|
2001-10-02 04:11:19 +08:00
|
|
|
for (; ValNo < Plane.size() && isa<MethodArgument>(Plane[ValNo]); ValNo++)
|
2001-09-08 00:39:41 +08:00
|
|
|
/*empty*/;
|
2001-06-07 04:29:01 +08:00
|
|
|
|
2001-09-08 00:39:41 +08:00
|
|
|
unsigned NC = ValNo; // Number of constants
|
|
|
|
for (; NC < Plane.size() &&
|
2001-10-02 02:26:53 +08:00
|
|
|
(isa<ConstPoolVal>(Plane[NC]) ||
|
|
|
|
isa<Type>(Plane[NC])); NC++) /*empty*/;
|
2001-09-08 00:39:41 +08:00
|
|
|
NC -= ValNo; // Convert from index into count
|
|
|
|
if (NC == 0) continue; // Skip empty type planes...
|
2001-06-07 04:29:01 +08:00
|
|
|
|
|
|
|
// Output type header: [num entries][type id number]
|
|
|
|
//
|
2001-09-08 00:39:41 +08:00
|
|
|
output_vbr(NC, Out);
|
2001-06-07 04:29:01 +08:00
|
|
|
|
|
|
|
// Output the Type ID Number...
|
|
|
|
int Slot = Table.getValSlot(Plane.front()->getType());
|
|
|
|
assert (Slot != -1 && "Type in constant pool but not in method!!");
|
|
|
|
output_vbr((unsigned)Slot, Out);
|
|
|
|
|
2001-09-08 00:39:41 +08:00
|
|
|
//cout << "Emitting " << NC << " constants of type '"
|
|
|
|
// << Plane.front()->getType()->getName() << "' = Slot #" << Slot << endl;
|
2001-06-07 04:29:01 +08:00
|
|
|
|
2001-09-08 00:39:41 +08:00
|
|
|
for (unsigned i = ValNo; i < ValNo+NC; ++i) {
|
|
|
|
const Value *V = Plane[i];
|
2001-10-02 04:11:19 +08:00
|
|
|
if (const ConstPoolVal *CPV = dyn_cast<ConstPoolVal>(V)) {
|
2001-06-07 04:29:01 +08:00
|
|
|
//cerr << "Serializing value: <" << V->getType() << ">: "
|
|
|
|
// << ((const ConstPoolVal*)V)->getStrValue() << ":"
|
|
|
|
// << Out.size() << "\n";
|
2001-06-28 07:41:11 +08:00
|
|
|
outputConstant(CPV);
|
2001-09-08 00:39:41 +08:00
|
|
|
} else {
|
2001-10-13 14:48:38 +08:00
|
|
|
outputType(cast<const Type>(V));
|
2001-06-07 04:29:01 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void BytecodeWriter::outputModuleInfoBlock(const Module *M) {
|
|
|
|
BytecodeBlock ModuleInfoBlock(BytecodeFormat::ModuleGlobalInfo, Out);
|
|
|
|
|
2001-09-10 15:58:01 +08:00
|
|
|
// Output the types for the global variables in the module...
|
|
|
|
for (Module::const_giterator I = M->gbegin(), End = M->gend(); I != End;++I) {
|
2001-09-18 12:01:05 +08:00
|
|
|
const GlobalVariable *GV = *I;
|
|
|
|
int Slot = Table.getValSlot(GV->getType());
|
2001-09-10 15:58:01 +08:00
|
|
|
assert(Slot != -1 && "Module global vars is broken!");
|
2001-09-18 12:01:05 +08:00
|
|
|
|
|
|
|
// Fields: bit0 = isConstant, bit1 = hasInitializer, bit2+ = slot#
|
|
|
|
unsigned oSlot = ((unsigned)Slot << 2) | (GV->hasInitializer() << 1) |
|
2001-10-18 14:11:23 +08:00
|
|
|
GV->isConstant();
|
2001-09-18 12:01:05 +08:00
|
|
|
output_vbr(oSlot, Out);
|
|
|
|
|
2001-10-13 14:48:38 +08:00
|
|
|
// If we have an initializer, output it now.
|
2001-09-18 12:01:05 +08:00
|
|
|
if (GV->hasInitializer()) {
|
|
|
|
Slot = Table.getValSlot(GV->getInitializer());
|
|
|
|
assert(Slot != -1 && "No slot for global var initializer!");
|
|
|
|
output_vbr((unsigned)Slot, Out);
|
|
|
|
}
|
2001-09-10 15:58:01 +08:00
|
|
|
}
|
|
|
|
output_vbr((unsigned)Table.getValSlot(Type::VoidTy), Out);
|
|
|
|
|
|
|
|
// Output the types of the methods in this module...
|
2001-06-28 07:41:11 +08:00
|
|
|
for (Module::const_iterator I = M->begin(), End = M->end(); I != End; ++I) {
|
2001-06-07 04:29:01 +08:00
|
|
|
int Slot = Table.getValSlot((*I)->getType());
|
|
|
|
assert(Slot != -1 && "Module const pool is broken!");
|
|
|
|
assert(Slot >= Type::FirstDerivedTyID && "Derived type not in range!");
|
|
|
|
output_vbr((unsigned)Slot, Out);
|
|
|
|
}
|
|
|
|
output_vbr((unsigned)Table.getValSlot(Type::VoidTy), Out);
|
2001-09-10 15:58:01 +08:00
|
|
|
|
|
|
|
|
2001-06-07 04:29:01 +08:00
|
|
|
align32(Out);
|
|
|
|
}
|
|
|
|
|
2001-09-08 00:39:41 +08:00
|
|
|
void BytecodeWriter::processMethod(const Method *M) {
|
2001-06-07 04:29:01 +08:00
|
|
|
BytecodeBlock MethodBlock(BytecodeFormat::Method, Out);
|
|
|
|
|
2001-09-08 00:39:41 +08:00
|
|
|
// Only output the constant pool and other goodies if needed...
|
|
|
|
if (!M->isExternal()) {
|
|
|
|
// Get slot information about the method...
|
|
|
|
Table.incorporateMethod(M);
|
2001-06-07 04:29:01 +08:00
|
|
|
|
2001-09-08 00:39:41 +08:00
|
|
|
// Output information about the constants in the method...
|
|
|
|
outputConstants(true);
|
2001-06-07 04:29:01 +08:00
|
|
|
|
2001-09-08 00:39:41 +08:00
|
|
|
// Output basic block nodes...
|
|
|
|
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();
|
|
|
|
}
|
2001-06-07 04:29:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-09-08 00:39:41 +08:00
|
|
|
void BytecodeWriter::processBasicBlock(const BasicBlock *BB) {
|
2001-06-07 04:29:01 +08:00
|
|
|
BytecodeBlock MethodBlock(BytecodeFormat::BasicBlock, Out);
|
2001-09-08 00:39:41 +08:00
|
|
|
// Process all the instructions in the bb...
|
|
|
|
for_each(BB->begin(), BB->end(),
|
|
|
|
bind_obj(this, &BytecodeWriter::processInstruction));
|
2001-06-07 04:29:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void BytecodeWriter::outputSymbolTable(const SymbolTable &MST) {
|
|
|
|
BytecodeBlock MethodBlock(BytecodeFormat::SymbolTable, Out);
|
|
|
|
|
2001-06-28 07:41:11 +08:00
|
|
|
for (SymbolTable::const_iterator TI = MST.begin(); TI != MST.end(); ++TI) {
|
2001-06-07 04:29:01 +08:00
|
|
|
SymbolTable::type_const_iterator I = MST.type_begin(TI->first);
|
|
|
|
SymbolTable::type_const_iterator End = MST.type_end(TI->first);
|
|
|
|
int Slot;
|
|
|
|
|
|
|
|
if (I == End) continue; // Don't mess with an absent type...
|
|
|
|
|
|
|
|
// Symtab block header: [num entries][type id number]
|
|
|
|
output_vbr(MST.type_size(TI->first), Out);
|
|
|
|
|
|
|
|
Slot = Table.getValSlot(TI->first);
|
|
|
|
assert(Slot != -1 && "Type in symtab, but not in table!");
|
|
|
|
output_vbr((unsigned)Slot, Out);
|
|
|
|
|
2001-06-28 07:41:11 +08:00
|
|
|
for (; I != End; ++I) {
|
2001-06-07 04:29:01 +08:00
|
|
|
// Symtab entry: [def slot #][name]
|
|
|
|
Slot = Table.getValSlot(I->second);
|
2001-09-08 00:39:41 +08:00
|
|
|
assert(Slot != -1 && "Value in symtab but has no slot number!!");
|
2001-06-07 04:29:01 +08:00
|
|
|
output_vbr((unsigned)Slot, Out);
|
|
|
|
output(I->first, Out, false); // Don't force alignment...
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void WriteBytecodeToFile(const Module *C, ostream &Out) {
|
2001-09-08 00:39:41 +08:00
|
|
|
assert(C && "You can't write a null module!!");
|
2001-06-07 04:29:01 +08:00
|
|
|
|
2001-09-08 00:39:41 +08:00
|
|
|
deque<unsigned char> Buffer;
|
2001-06-07 04:29:01 +08:00
|
|
|
|
|
|
|
// This object populates buffer for us...
|
|
|
|
BytecodeWriter BCW(Buffer, C);
|
|
|
|
|
2001-09-08 00:39:41 +08:00
|
|
|
// Okay, write the deque out to the ostream now... the deque is not
|
|
|
|
// 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;
|
2001-11-05 05:32:41 +08:00
|
|
|
if (LastPtr+1 != ThisPtr) { // Advanced by more than a byte of memory?
|
|
|
|
++LastPtr;
|
|
|
|
break;
|
|
|
|
}
|
2001-09-08 00:39:41 +08:00
|
|
|
LastPtr = ThisPtr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write out the chunk...
|
2001-11-05 05:32:41 +08:00
|
|
|
Out.write(ChunkPtr, LastPtr-ChunkPtr);
|
2001-09-08 00:39:41 +08:00
|
|
|
}
|
|
|
|
|
2001-06-07 04:29:01 +08:00
|
|
|
Out.flush();
|
|
|
|
}
|