2003-10-13 03:32:08 +00:00
|
|
|
//===-- Writer.cpp - Library for writing VM bytecode files ----------------===//
|
2001-06-06 20:29:01 +00:00
|
|
|
//
|
|
|
|
// This library implements the functionality defined in llvm/Bytecode/Writer.h
|
|
|
|
//
|
|
|
|
// Note that this file uses an unusual technique of outputting all the bytecode
|
2003-09-15 00:33:20 +00:00
|
|
|
// to a deque of unsigned char, then copies the deque to an ostream. The
|
2001-06-06 20:29:01 +00: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-07 16:39:41 +00: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-06 20:29:01 +00: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"
|
2002-07-23 19:56:44 +00:00
|
|
|
#include "llvm/Bytecode/WriteBytecodePass.h"
|
2001-06-06 20:29:01 +00:00
|
|
|
#include "llvm/Module.h"
|
|
|
|
#include "llvm/SymbolTable.h"
|
|
|
|
#include "llvm/DerivedTypes.h"
|
2001-11-29 16:32:16 +00:00
|
|
|
#include "Support/STLExtras.h"
|
2002-10-01 22:38:41 +00:00
|
|
|
#include "Support/Statistic.h"
|
2003-06-30 21:59:07 +00:00
|
|
|
#include "Config/string.h"
|
2001-06-06 20:29:01 +00:00
|
|
|
#include <algorithm>
|
|
|
|
|
2002-07-23 19:56:44 +00:00
|
|
|
static RegisterPass<WriteBytecodePass> X("emitbytecode", "Bytecode Writer");
|
|
|
|
|
2002-07-26 18:40:14 +00:00
|
|
|
static Statistic<>
|
2002-10-01 22:38:41 +00:00
|
|
|
BytesWritten("bytecodewriter", "Number of bytecode bytes written");
|
2002-07-23 19:56:44 +00:00
|
|
|
|
|
|
|
|
2002-01-20 22:54:45 +00:00
|
|
|
BytecodeWriter::BytecodeWriter(std::deque<unsigned char> &o, const Module *M)
|
2001-06-06 20:29:01 +00:00
|
|
|
: Out(o), Table(M, false) {
|
|
|
|
|
|
|
|
outputSignature();
|
|
|
|
|
|
|
|
// Emit the top level CLASS block.
|
|
|
|
BytecodeBlock ModuleBlock(BytecodeFormat::Module, Out);
|
|
|
|
|
2003-08-24 13:47:36 +00:00
|
|
|
bool isBigEndian = M->getEndianness() == Module::BigEndian;
|
|
|
|
bool hasLongPointers = M->getPointerSize() == Module::Pointer64;
|
|
|
|
bool hasNoEndianness = M->getEndianness() == Module::AnyEndianness;
|
|
|
|
bool hasNoPointerSize = M->getPointerSize() == Module::AnyPointerSize;
|
2003-03-19 20:56:46 +00:00
|
|
|
|
2003-04-16 21:16:05 +00:00
|
|
|
// Output the version identifier... we are currently on bytecode version #2
|
2003-08-24 13:47:36 +00:00
|
|
|
unsigned Version = (2 << 4) | isBigEndian | (hasLongPointers << 1) |
|
|
|
|
(hasNoEndianness << 2) | (hasNoPointerSize << 3);
|
2003-03-19 20:56:46 +00:00
|
|
|
output_vbr(Version, Out);
|
2001-06-06 20:29:01 +00:00
|
|
|
align32(Out);
|
|
|
|
|
2003-03-19 20:56:46 +00:00
|
|
|
{
|
|
|
|
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<const Value*> &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
|
|
|
|
}
|
2001-06-06 20:29:01 +00:00
|
|
|
|
2003-03-19 20:56:46 +00:00
|
|
|
// The ModuleInfoBlock follows directly after the type information
|
2001-09-07 16:39:41 +00:00
|
|
|
outputModuleInfoBlock(M);
|
|
|
|
|
2003-03-19 20:56:46 +00:00
|
|
|
// Output module level constants, used for global variable initializers
|
|
|
|
outputConstants(false);
|
|
|
|
|
2002-04-07 22:49:37 +00:00
|
|
|
// Do the whole module now! Process each function at a time...
|
2002-06-25 16:13:21 +00:00
|
|
|
for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
|
2003-03-19 20:56:46 +00:00
|
|
|
outputFunction(I);
|
2001-09-07 16:39:41 +00:00
|
|
|
|
|
|
|
// If needed, output the symbol table for the module...
|
2002-11-20 18:36:02 +00:00
|
|
|
outputSymbolTable(M->getSymbolTable());
|
2001-06-06 20:29:01 +00:00
|
|
|
}
|
|
|
|
|
2002-07-14 23:07:51 +00:00
|
|
|
// Helper function for outputConstants().
|
|
|
|
// Writes out all the constants in the plane Plane starting at entry StartNo.
|
|
|
|
//
|
|
|
|
void BytecodeWriter::outputConstantsInPlane(const std::vector<const Value*>
|
|
|
|
&Plane, unsigned StartNo) {
|
|
|
|
unsigned ValNo = StartNo;
|
|
|
|
|
2003-03-19 20:56:46 +00:00
|
|
|
// Scan through and ignore function arguments/global values...
|
|
|
|
for (; ValNo < Plane.size() && (isa<Argument>(Plane[ValNo]) ||
|
|
|
|
isa<GlobalValue>(Plane[ValNo])); ValNo++)
|
2002-07-14 23:07:51 +00:00
|
|
|
/*empty*/;
|
|
|
|
|
|
|
|
unsigned NC = ValNo; // Number of constants
|
|
|
|
for (; NC < Plane.size() &&
|
|
|
|
(isa<Constant>(Plane[NC]) || isa<Type>(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...
|
2003-10-17 02:02:40 +00:00
|
|
|
int Slot = Table.getSlot(Plane.front()->getType());
|
2002-07-14 23:07:51 +00:00
|
|
|
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<Constant>(V)) {
|
|
|
|
//cerr << "Serializing value: <" << V->getType() << ">: " << V << ":"
|
|
|
|
// << Out.size() << "\n";
|
|
|
|
outputConstant(CPV);
|
|
|
|
} else {
|
2003-03-19 20:56:46 +00:00
|
|
|
outputType(cast<Type>(V));
|
2002-07-14 23:07:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-03-26 18:01:55 +00:00
|
|
|
void BytecodeWriter::outputConstants(bool isFunction) {
|
2001-09-07 16:39:41 +00:00
|
|
|
BytecodeBlock CPool(BytecodeFormat::ConstantPool, Out);
|
2001-06-06 20:29:01 +00:00
|
|
|
|
|
|
|
unsigned NumPlanes = Table.getNumPlanes();
|
2003-03-19 20:56:46 +00:00
|
|
|
|
2003-05-22 18:35:38 +00:00
|
|
|
// Output the type plane before any constants!
|
|
|
|
if (isFunction && NumPlanes > Type::TypeTyID) {
|
|
|
|
const std::vector<const Value*> &Plane = Table.getPlane(Type::TypeTyID);
|
|
|
|
if (!Plane.empty()) { // Skip empty type planes...
|
|
|
|
unsigned ValNo = Table.getModuleLevel(Type::TypeTyID);
|
|
|
|
outputConstantsInPlane(Plane, ValNo);
|
2002-10-14 03:34:17 +00:00
|
|
|
}
|
2001-06-06 20:29:01 +00:00
|
|
|
}
|
2003-05-22 18:35:38 +00:00
|
|
|
|
|
|
|
for (unsigned pno = 0; pno != NumPlanes; pno++)
|
|
|
|
if (pno != Type::TypeTyID) { // Type plane handled above.
|
|
|
|
const std::vector<const Value*> &Plane = Table.getPlane(pno);
|
|
|
|
if (!Plane.empty()) { // Skip empty type planes...
|
|
|
|
unsigned ValNo = 0;
|
2003-09-11 22:34:13 +00:00
|
|
|
if (isFunction) // Don't re-emit module constants
|
2003-05-22 18:35:38 +00:00
|
|
|
ValNo += Table.getModuleLevel(pno);
|
|
|
|
|
|
|
|
if (pno >= Type::FirstDerivedTyID) {
|
|
|
|
// Skip zero initializer
|
|
|
|
if (ValNo == 0)
|
|
|
|
ValNo = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write out constants in the plane
|
|
|
|
outputConstantsInPlane(Plane, ValNo);
|
|
|
|
}
|
|
|
|
}
|
2001-06-06 20:29:01 +00:00
|
|
|
}
|
|
|
|
|
2003-10-16 18:28:50 +00:00
|
|
|
static unsigned getEncodedLinkage(const GlobalValue *GV) {
|
|
|
|
switch (GV->getLinkage()) {
|
|
|
|
default: assert(0 && "Invalid linkage!");
|
|
|
|
case GlobalValue::ExternalLinkage: return 0;
|
|
|
|
case GlobalValue::LinkOnceLinkage: return 1;
|
|
|
|
case GlobalValue::WeakLinkage: return 1;
|
|
|
|
case GlobalValue::AppendingLinkage: return 2;
|
|
|
|
case GlobalValue::InternalLinkage: return 3;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-06-06 20:29:01 +00:00
|
|
|
void BytecodeWriter::outputModuleInfoBlock(const Module *M) {
|
|
|
|
BytecodeBlock ModuleInfoBlock(BytecodeFormat::ModuleGlobalInfo, Out);
|
|
|
|
|
2001-09-10 07:58:01 +00:00
|
|
|
// Output the types for the global variables in the module...
|
|
|
|
for (Module::const_giterator I = M->gbegin(), End = M->gend(); I != End;++I) {
|
2003-10-17 02:02:40 +00:00
|
|
|
int Slot = Table.getSlot(I->getType());
|
2001-09-10 07:58:01 +00:00
|
|
|
assert(Slot != -1 && "Module global vars is broken!");
|
2001-09-18 04:01:05 +00:00
|
|
|
|
2003-04-16 21:16:05 +00:00
|
|
|
// Fields: bit0 = isConstant, bit1 = hasInitializer, bit2,3=Linkage,
|
|
|
|
// bit4+ = Slot # for type
|
2003-10-16 18:28:50 +00:00
|
|
|
unsigned oSlot = ((unsigned)Slot << 4) | (getEncodedLinkage(I) << 2) |
|
2002-06-25 16:13:21 +00:00
|
|
|
(I->hasInitializer() << 1) | I->isConstant();
|
2001-09-18 04:01:05 +00:00
|
|
|
output_vbr(oSlot, Out);
|
|
|
|
|
2001-10-13 06:48:38 +00:00
|
|
|
// If we have an initializer, output it now.
|
2002-06-25 16:13:21 +00:00
|
|
|
if (I->hasInitializer()) {
|
2003-10-17 02:02:40 +00:00
|
|
|
Slot = Table.getSlot((Value*)I->getInitializer());
|
2001-09-18 04:01:05 +00:00
|
|
|
assert(Slot != -1 && "No slot for global var initializer!");
|
|
|
|
output_vbr((unsigned)Slot, Out);
|
|
|
|
}
|
2001-09-10 07:58:01 +00:00
|
|
|
}
|
2003-10-17 02:02:40 +00:00
|
|
|
output_vbr((unsigned)Table.getSlot(Type::VoidTy), Out);
|
2001-09-10 07:58:01 +00:00
|
|
|
|
2002-04-07 22:49:37 +00:00
|
|
|
// Output the types of the functions in this module...
|
2001-06-27 23:41:11 +00:00
|
|
|
for (Module::const_iterator I = M->begin(), End = M->end(); I != End; ++I) {
|
2003-10-17 02:02:40 +00:00
|
|
|
int Slot = Table.getSlot(I->getType());
|
2001-06-06 20:29:01 +00:00
|
|
|
assert(Slot != -1 && "Module const pool is broken!");
|
|
|
|
assert(Slot >= Type::FirstDerivedTyID && "Derived type not in range!");
|
|
|
|
output_vbr((unsigned)Slot, Out);
|
|
|
|
}
|
2003-10-17 02:02:40 +00:00
|
|
|
output_vbr((unsigned)Table.getSlot(Type::VoidTy), Out);
|
2001-09-10 07:58:01 +00:00
|
|
|
|
2001-06-06 20:29:01 +00:00
|
|
|
align32(Out);
|
|
|
|
}
|
|
|
|
|
2003-03-19 20:56:46 +00:00
|
|
|
void BytecodeWriter::outputFunction(const Function *F) {
|
2002-03-29 03:51:11 +00:00
|
|
|
BytecodeBlock FunctionBlock(BytecodeFormat::Function, Out);
|
2003-10-16 18:28:50 +00:00
|
|
|
output_vbr(getEncodedLinkage(F), Out);
|
2001-09-07 16:39:41 +00:00
|
|
|
// Only output the constant pool and other goodies if needed...
|
2002-06-25 16:13:21 +00:00
|
|
|
if (!F->isExternal()) {
|
2001-11-26 18:56:10 +00:00
|
|
|
|
2002-04-07 22:49:37 +00:00
|
|
|
// Get slot information about the function...
|
2002-06-25 16:13:21 +00:00
|
|
|
Table.incorporateFunction(F);
|
2001-06-06 20:29:01 +00:00
|
|
|
|
2002-04-07 22:49:37 +00:00
|
|
|
// Output information about the constants in the function...
|
2001-09-07 16:39:41 +00:00
|
|
|
outputConstants(true);
|
2001-06-06 20:29:01 +00:00
|
|
|
|
2001-09-07 16:39:41 +00:00
|
|
|
// Output basic block nodes...
|
2002-06-25 16:13:21 +00:00
|
|
|
for (Function::const_iterator I = F->begin(), E = F->end(); I != E; ++I)
|
|
|
|
processBasicBlock(*I);
|
2001-09-07 16:39:41 +00:00
|
|
|
|
2002-04-07 22:49:37 +00:00
|
|
|
// If needed, output the symbol table for the function...
|
2002-11-20 18:36:02 +00:00
|
|
|
outputSymbolTable(F->getSymbolTable());
|
2001-09-07 16:39:41 +00:00
|
|
|
|
2002-04-07 22:49:37 +00:00
|
|
|
Table.purgeFunction();
|
2001-09-07 16:39:41 +00:00
|
|
|
}
|
2001-06-06 20:29:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-06-25 16:13:21 +00:00
|
|
|
void BytecodeWriter::processBasicBlock(const BasicBlock &BB) {
|
2002-03-26 18:01:55 +00:00
|
|
|
BytecodeBlock FunctionBlock(BytecodeFormat::BasicBlock, Out);
|
2001-09-07 16:39:41 +00:00
|
|
|
// Process all the instructions in the bb...
|
2002-06-25 16:13:21 +00:00
|
|
|
for(BasicBlock::const_iterator I = BB.begin(), E = BB.end(); I != E; ++I)
|
|
|
|
processInstruction(*I);
|
2001-06-06 20:29:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void BytecodeWriter::outputSymbolTable(const SymbolTable &MST) {
|
2002-03-26 18:01:55 +00:00
|
|
|
BytecodeBlock FunctionBlock(BytecodeFormat::SymbolTable, Out);
|
2001-06-06 20:29:01 +00:00
|
|
|
|
2001-06-27 23:41:11 +00:00
|
|
|
for (SymbolTable::const_iterator TI = MST.begin(); TI != MST.end(); ++TI) {
|
2001-06-06 20:29:01 +00: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);
|
|
|
|
|
2003-10-17 02:02:40 +00:00
|
|
|
Slot = Table.getSlot(TI->first);
|
2001-06-06 20:29:01 +00:00
|
|
|
assert(Slot != -1 && "Type in symtab, but not in table!");
|
|
|
|
output_vbr((unsigned)Slot, Out);
|
|
|
|
|
2001-06-27 23:41:11 +00:00
|
|
|
for (; I != End; ++I) {
|
2001-06-06 20:29:01 +00:00
|
|
|
// Symtab entry: [def slot #][name]
|
2003-10-17 02:02:40 +00:00
|
|
|
Slot = Table.getSlot(I->second);
|
2001-09-07 16:39:41 +00:00
|
|
|
assert(Slot != -1 && "Value in symtab but has no slot number!!");
|
2001-06-06 20:29:01 +00:00
|
|
|
output_vbr((unsigned)Slot, Out);
|
|
|
|
output(I->first, Out, false); // Don't force alignment...
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-06-25 20:44:04 +00:00
|
|
|
void WriteBytecodeToFile(const Module *C, std::ostream &Out) {
|
2001-09-07 16:39:41 +00:00
|
|
|
assert(C && "You can't write a null module!!");
|
2001-06-06 20:29:01 +00:00
|
|
|
|
2002-01-20 22:54:45 +00:00
|
|
|
std::deque<unsigned char> Buffer;
|
2001-06-06 20:29:01 +00:00
|
|
|
|
|
|
|
// This object populates buffer for us...
|
|
|
|
BytecodeWriter BCW(Buffer, C);
|
|
|
|
|
2002-07-26 18:40:14 +00:00
|
|
|
// Keep track of how much we've written...
|
|
|
|
BytesWritten += Buffer.size();
|
|
|
|
|
2001-09-07 16:39:41 +00: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.
|
|
|
|
//
|
2002-01-20 22:54:45 +00:00
|
|
|
std::deque<unsigned char>::const_iterator I = Buffer.begin(),E = Buffer.end();
|
2001-09-07 16:39:41 +00:00
|
|
|
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-04 21:32:41 +00:00
|
|
|
if (LastPtr+1 != ThisPtr) { // Advanced by more than a byte of memory?
|
|
|
|
++LastPtr;
|
|
|
|
break;
|
|
|
|
}
|
2001-09-07 16:39:41 +00:00
|
|
|
LastPtr = ThisPtr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write out the chunk...
|
2002-01-20 22:54:45 +00:00
|
|
|
Out.write((char*)ChunkPtr, LastPtr-ChunkPtr);
|
2001-09-07 16:39:41 +00:00
|
|
|
}
|
|
|
|
|
2001-06-06 20:29:01 +00:00
|
|
|
Out.flush();
|
|
|
|
}
|