Initial revision

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@2 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Chris Lattner
2001-06-06 20:29:01 +00:00
parent 8d0afd3d32
commit 009505452b
145 changed files with 19198 additions and 0 deletions
+154
View File
@@ -0,0 +1,154 @@
//===-- WriteConst.cpp - Functions for writing constants ---------*- C++ -*--=//
//
// This file implements the routines for encoding constants to a bytecode
// stream.
//
// 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/ConstPoolVals.h"
#include "llvm/SymbolTable.h"
#include "llvm/DerivedTypes.h"
void BytecodeWriter::outputType(const Type *T) {
output_vbr((unsigned)T->getPrimitiveID(), Out);
// That's all there is to handling primitive types...
if (T->isPrimitiveType())
return; // We might do this if we alias a prim type: %x = type int
switch (T->getPrimitiveID()) { // Handle derived types now.
case Type::MethodTyID: {
const MethodType *MT = (const MethodType*)T;
int Slot = Table.getValSlot(MT->getReturnType());
assert(Slot != -1 && "Type used but not available!!");
output_vbr((unsigned)Slot, Out);
// Output all of the arguments...
MethodType::ParamTypes::const_iterator I = MT->getParamTypes().begin();
for (; I != MT->getParamTypes().end(); I++) {
Slot = Table.getValSlot(*I);
assert(Slot != -1 && "Type used but not available!!");
output_vbr((unsigned)Slot, Out);
}
// Terminate list with VoidTy
output_vbr((unsigned)Type::VoidTy->getPrimitiveID(), Out);
break;
}
case Type::ArrayTyID: {
const ArrayType *AT = (const ArrayType*)T;
int Slot = Table.getValSlot(AT->getElementType());
assert(Slot != -1 && "Type used but not available!!");
output_vbr((unsigned)Slot, Out);
//cerr << "Type slot = " << Slot << " Type = " << T->getName() << endl;
output_vbr(AT->getNumElements(), Out);
break;
}
case Type::StructTyID: {
const StructType *ST = (const StructType*)T;
// Output all of the element types...
StructType::ElementTypes::const_iterator I = ST->getElementTypes().begin();
for (; I != ST->getElementTypes().end(); I++) {
int Slot = Table.getValSlot(*I);
assert(Slot != -1 && "Type used but not available!!");
output_vbr((unsigned)Slot, Out);
}
// Terminate list with VoidTy
output_vbr((unsigned)Type::VoidTy->getPrimitiveID(), Out);
break;
}
case Type::PointerTyID: {
const PointerType *PT = (const PointerType*)T;
int Slot = Table.getValSlot(PT->getValueType());
assert(Slot != -1 && "Type used but not available!!");
output_vbr((unsigned)Slot, Out);
break;
}
case Type::ModuleTyID:
case Type::PackedTyID:
default:
cerr << __FILE__ << ":" << __LINE__ << ": Don't know how to serialize"
<< " Type '" << T->getName() << "'\n";
break;
}
}
bool BytecodeWriter::outputConstant(const ConstPoolVal *CPV) {
switch (CPV->getType()->getPrimitiveID()) {
case Type::BoolTyID: // Boolean Types
if (((const ConstPoolBool*)CPV)->getValue())
output_vbr((unsigned)1, Out);
else
output_vbr((unsigned)0, Out);
break;
case Type::UByteTyID: // Unsigned integer types...
case Type::UShortTyID:
case Type::UIntTyID:
case Type::ULongTyID:
output_vbr(((const ConstPoolUInt*)CPV)->getValue(), Out);
break;
case Type::SByteTyID: // Signed integer types...
case Type::ShortTyID:
case Type::IntTyID:
case Type::LongTyID:
output_vbr(((const ConstPoolSInt*)CPV)->getValue(), Out);
break;
case Type::TypeTyID: // Serialize type type
outputType(((const ConstPoolType*)CPV)->getValue());
break;
case Type::ArrayTyID: {
const ConstPoolArray *CPA = (const ConstPoolArray *)CPV;
unsigned size = CPA->getValues().size();
if (!((const ArrayType *)CPA->getType())->isSized())
output_vbr(size, Out); // Not for sized arrays!!!
for (unsigned i = 0; i < size; i++) {
int Slot = Table.getValSlot(CPA->getValues()[i]);
assert(Slot != -1 && "Constant used but not available!!");
output_vbr((unsigned)Slot, Out);
}
break;
}
case Type::StructTyID: {
const ConstPoolStruct *CPS = (const ConstPoolStruct*)CPV;
const vector<ConstPoolUse> &Vals = CPS->getValues();
for (unsigned i = 0; i < Vals.size(); ++i) {
int Slot = Table.getValSlot(Vals[i]);
assert(Slot != -1 && "Constant used but not available!!");
output_vbr((unsigned)Slot, Out);
}
break;
}
case Type::FloatTyID: // Floating point types...
case Type::DoubleTyID:
// TODO: Floating point type serialization
case Type::VoidTyID:
case Type::LabelTyID:
default:
cerr << __FILE__ << ":" << __LINE__ << ": Don't know how to serialize"
<< " type '" << CPV->getType()->getName() << "'\n";
break;
}
return false;
}
+184
View File
@@ -0,0 +1,184 @@
//===-- WriteInst.cpp - Functions for writing instructions -------*- C++ -*--=//
//
// This file implements the routines for encoding instruction opcodes to a
// bytecode stream.
//
// 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"
#include "llvm/Method.h"
#include "llvm/BasicBlock.h"
#include "llvm/Instruction.h"
#include "llvm/DerivedTypes.h"
#include "llvm/Tools/DataTypes.h"
#include <algorithm>
typedef unsigned char uchar;
// outputInstructionFormat0 - Output those wierd instructions that have a large
// number of operands or have large operands themselves...
//
// Format: [opcode] [type] [numargs] [arg0] [arg1] ... [arg<numargs-1>]
//
static void outputInstructionFormat0(const Instruction *I,
const SlotCalculator &Table,
unsigned Type, vector<uchar> &Out) {
// Opcode must have top two bits clear...
output_vbr(I->getInstType(), Out); // Instruction Opcode ID
output_vbr(Type, Out); // Result type
unsigned NumArgs; // Count the number of arguments to the instruction
for (NumArgs = 0; I->getOperand(NumArgs); NumArgs++) /*empty*/;
output_vbr(NumArgs, Out);
for (unsigned i = 0; const Value *N = I->getOperand(i); i++) {
assert(i < NumArgs && "Count of arguments failed!");
int Slot = Table.getValSlot(N);
output_vbr((unsigned)Slot, Out);
}
align32(Out); // We must maintain correct alignment!
}
// outputInstructionFormat1 - Output one operand instructions, knowing that no
// operand index is >= 2^12.
//
static void outputInstructionFormat1(const Instruction *I,
const SlotCalculator &Table, int *Slots,
unsigned Type, vector<uchar> &Out) {
unsigned IType = I->getInstType(); // Instruction Opcode ID
// bits Instruction format:
// --------------------------
// 31-30: Opcode type, fixed to 1.
// 29-24: Opcode
// 23-12: Resulting type plane
// 11- 0: Operand #1 (if set to (2^12-1), then zero operands)
//
unsigned Opcode = (1 << 30) | (IType << 24) | (Type << 12) | Slots[0];
// cerr << "1 " << IType << " " << Type << " " << Slots[0] << endl;
output(Opcode, Out);
}
// outputInstructionFormat2 - Output two operand instructions, knowing that no
// operand index is >= 2^8.
//
static void outputInstructionFormat2(const Instruction *I,
const SlotCalculator &Table, int *Slots,
unsigned Type, vector<uchar> &Out) {
unsigned IType = I->getInstType(); // Instruction Opcode ID
// bits Instruction format:
// --------------------------
// 31-30: Opcode type, fixed to 2.
// 29-24: Opcode
// 23-16: Resulting type plane
// 15- 8: Operand #1
// 7- 0: Operand #2
//
unsigned Opcode = (2 << 30) | (IType << 24) | (Type << 16) |
(Slots[0] << 8) | (Slots[1] << 0);
// cerr << "2 " << IType << " " << Type << " " << Slots[0] << " "
// << Slots[1] << endl;
output(Opcode, Out);
}
// outputInstructionFormat3 - Output three operand instructions, knowing that no
// operand index is >= 2^6.
//
static void outputInstructionFormat3(const Instruction *I,
const SlotCalculator &Table, int *Slots,
unsigned Type, vector<uchar> &Out) {
unsigned IType = I->getInstType(); // Instruction Opcode ID
// bits Instruction format:
// --------------------------
// 31-30: Opcode type, fixed to 3
// 29-24: Opcode
// 23-18: Resulting type plane
// 17-12: Operand #1
// 11- 6: Operand #2
// 5- 0: Operand #3
//
unsigned Opcode = (3 << 30) | (IType << 24) | (Type << 18) |
(Slots[0] << 12) | (Slots[1] << 6) | (Slots[2] << 0);
// cerr << "3 " << IType << " " << Type << " " << Slots[0] << " "
// << Slots[1] << " " << Slots[2] << endl;
output(Opcode, Out);
}
bool BytecodeWriter::processInstruction(const Instruction *I) {
assert(I->getInstType() < 64 && "Opcode too big???");
unsigned NumOperands = 0;
int MaxOpSlot = 0;
int Slots[3]; Slots[0] = (1 << 12)-1;
const Value *Def;
while ((Def = I->getOperand(NumOperands))) {
int slot = Table.getValSlot(Def);
assert(slot != -1 && "Broken bytecode!");
if (slot > MaxOpSlot) MaxOpSlot = slot;
if (NumOperands < 3) Slots[NumOperands] = slot;
NumOperands++;
}
// Figure out which type to encode with the instruction. Typically we want
// the type of the first parameter, as opposed to the type of the instruction
// (for example, with setcc, we always know it returns bool, but the type of
// the first param is actually interesting). But if we have no arguments
// we take the type of the instruction itself.
//
const Type *Ty;
if (NumOperands)
Ty = I->getOperand(0)->getType();
else
Ty = I->getType();
unsigned Type;
int Slot = Table.getValSlot(Ty);
assert(Slot != -1 && "Type not available!!?!");
Type = (unsigned)Slot;
// Decide which instruction encoding to use. This is determined primarily by
// the number of operands, and secondarily by whether or not the max operand
// will fit into the instruction encoding. More operands == fewer bits per
// operand.
//
switch (NumOperands) {
case 0:
case 1:
if (MaxOpSlot < (1 << 12)-1) { // -1 because we use 4095 to indicate 0 ops
outputInstructionFormat1(I, Table, Slots, Type, Out);
return false;
}
break;
case 2:
if (MaxOpSlot < (1 << 8)) {
outputInstructionFormat2(I, Table, Slots, Type, Out);
return false;
}
break;
case 3:
if (MaxOpSlot < (1 << 6)) {
outputInstructionFormat3(I, Table, Slots, Type, Out);
return false;
}
break;
}
outputInstructionFormat0(I, Table, Type, Out);
return false;
}
+7
View File
@@ -0,0 +1,7 @@
LEVEL = ../../..
LIBRARYNAME = bcwriter
include $(LEVEL)/Makefile.common
+195
View File
@@ -0,0 +1,195 @@
//===-- SlotCalculator.cpp - Calculate what slots values land in ------------=//
//
// This file implements a useful analysis step to figure out what numbered
// slots values in a program will land in (keeping track of per plane
// information as required.
//
// This is used primarily for when writing a file to disk, either in bytecode
// or source format.
//
//===----------------------------------------------------------------------===//
#include "llvm/Analysis/SlotCalculator.h"
#include "llvm/ConstantPool.h"
#include "llvm/Method.h"
#include "llvm/Module.h"
#include "llvm/BasicBlock.h"
#include "llvm/ConstPoolVals.h"
#include "llvm/iOther.h"
#include "llvm/DerivedTypes.h"
SlotCalculator::SlotCalculator(const Module *M, bool IgnoreNamed) {
IgnoreNamedNodes = IgnoreNamed;
TheModule = M;
// Preload table... Make sure that all of the primitive types are in the table
// and that their Primitive ID is equal to their slot #
//
for (unsigned i = 0; i < Type::FirstDerivedTyID; ++i) {
assert(Type::getPrimitiveType((Type::PrimitiveID)i));
insertVal(Type::getPrimitiveType((Type::PrimitiveID)i));
}
if (M == 0) return; // Empty table...
bool Result = processModule(M);
assert(Result == false && "Error in processModule!");
}
SlotCalculator::SlotCalculator(const Method *M, bool IgnoreNamed) {
IgnoreNamedNodes = IgnoreNamed;
TheModule = M ? M->getParent() : 0;
// Preload table... Make sure that all of the primitive types are in the table
// and that their Primitive ID is equal to their slot #
//
for (unsigned i = 0; i < Type::FirstDerivedTyID; ++i) {
assert(Type::getPrimitiveType((Type::PrimitiveID)i));
insertVal(Type::getPrimitiveType((Type::PrimitiveID)i));
}
if (TheModule == 0) return; // Empty table...
bool Result = processModule(TheModule);
assert(Result == false && "Error in processModule!");
incorporateMethod(M);
}
void SlotCalculator::incorporateMethod(const Method *M) {
assert(ModuleLevel.size() == 0 && "Module already incorporated!");
// Save the Table state before we process the method...
for (unsigned i = 0; i < Table.size(); ++i) {
ModuleLevel.push_back(Table[i].size());
}
// Process the method to incorporate its values into our table
processMethod(M);
}
void SlotCalculator::purgeMethod() {
assert(ModuleLevel.size() != 0 && "Module not incorporated!");
unsigned NumModuleTypes = ModuleLevel.size();
// First, remove values from existing type planes
for (unsigned i = 0; i < NumModuleTypes; ++i) {
unsigned ModuleSize = ModuleLevel[i]; // Size of plane before method came
while (Table[i].size() != ModuleSize) {
NodeMap.erase(NodeMap.find(Table[i].back())); // Erase from nodemap
Table[i].pop_back(); // Shrink plane
}
}
// We don't need this state anymore, free it up.
ModuleLevel.clear();
// Next, remove any type planes defined by the method...
while (NumModuleTypes != Table.size()) {
TypePlane &Plane = Table.back();
while (Plane.size()) {
NodeMap.erase(NodeMap.find(Plane.back())); // Erase from nodemap
Plane.pop_back(); // Shrink plane
}
Table.pop_back(); // Nuke the plane, we don't like it.
}
}
bool SlotCalculator::processConstant(const ConstPoolVal *CPV) {
//cerr << "Inserting constant: '" << CPV->getStrValue() << endl;
insertVal(CPV);
return false;
}
// processType - This callback occurs when an derived type is discovered
// at the class level. This activity occurs when processing a constant pool.
//
bool SlotCalculator::processType(const Type *Ty) {
//cerr << "processType: " << Ty->getName() << endl;
// TODO: Don't leak memory!!! Free this in the dtor!
insertVal(new ConstPoolType(Ty));
return false;
}
bool SlotCalculator::visitMethod(const Method *M) {
//cerr << "visitMethod: '" << M->getType()->getName() << "'\n";
insertVal(M);
return false;
}
bool SlotCalculator::processMethodArgument(const MethodArgument *MA) {
insertVal(MA);
return false;
}
bool SlotCalculator::processBasicBlock(const BasicBlock *BB) {
insertVal(BB);
ModuleAnalyzer::processBasicBlock(BB); // Lets visit the instructions too!
return false;
}
bool SlotCalculator::processInstruction(const Instruction *I) {
insertVal(I);
return false;
}
int SlotCalculator::getValSlot(const Value *D) const {
map<const Value*, unsigned>::const_iterator I = NodeMap.find(D);
if (I == NodeMap.end()) return -1;
return (int)I->second;
}
void SlotCalculator::insertVal(const Value *D) {
if (D == 0) return;
// If this node does not contribute to a plane, or if the node has a
// name and we don't want names, then ignore the silly node...
//
if (D->getType() == Type::VoidTy || (IgnoreNamedNodes && D->hasName()))
return;
const Type *Typ = D->getType();
unsigned Ty = Typ->getPrimitiveID();
if (Typ->isDerivedType()) {
int DefSlot = getValSlot(Typ);
if (DefSlot == -1) { // Have we already entered this type?
// This can happen if a type is first seen in an instruction. For
// example, if you say 'malloc uint', this defines a type 'uint*' that
// may be undefined at this point.
//
cerr << "SHOULDNT HAPPEN Adding Type ba: " << Typ->getName() << endl;
assert(0 && "SHouldn't this be taken care of by processType!?!?!");
// Nope... add this to the Type plane now!
insertVal(Typ);
DefSlot = getValSlot(Typ);
assert(DefSlot >= 0 && "Type didn't get inserted correctly!");
}
Ty = (unsigned)DefSlot;
}
if (Table.size() <= Ty) // Make sure we have the type plane allocated...
Table.resize(Ty+1, TypePlane());
// Insert node into table and NodeMap...
NodeMap[D] = Table[Ty].size();
if (Typ == Type::TypeTy && // If it's a type constant, add the Type also
D->getValueType() != Value::TypeVal) {
assert(D->getValueType() == Value::ConstantVal &&
"All Type instances should be constant types!");
const ConstPoolType *CPT = (const ConstPoolType*)D;
int Slot = getValSlot(CPT->getValue());
if (Slot == -1) {
// Only add if it's not already here!
NodeMap[CPT->getValue()] = Table[Ty].size();
} else if (!CPT->hasName()) { // If the type has no name...
NodeMap[D] = (unsigned)Slot; // Don't readd type, merge.
return;
}
}
Table[Ty].push_back(D);
}
+96
View File
@@ -0,0 +1,96 @@
//===-- llvm/Analysis/SlotCalculator.h - Calculate value slots ---*- C++ -*-==//
//
// This ModuleAnalyzer subclass calculates the slots that values will land in.
// This is useful for when writing bytecode or assembly out, because you have
// to know these things.
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_ANALYSIS_SLOTCALCULATOR_H
#define LLVM_ANALYSIS_SLOTCALCULATOR_H
#include "llvm/Analysis/ModuleAnalyzer.h"
#include "llvm/SymTabValue.h"
#include <vector>
#include <map>
class SlotCalculator : public ModuleAnalyzer {
const Module *TheModule;
bool IgnoreNamedNodes; // Shall we not count named nodes?
typedef vector<const Value*> TypePlane;
vector <TypePlane> Table;
map<const Value *, unsigned> NodeMap;
// ModuleLevel - Used to keep track of which values belong to the module,
// and which values belong to the currently incorporated method.
//
vector <unsigned> ModuleLevel;
public:
SlotCalculator(const Module *M, bool IgnoreNamed);
SlotCalculator(const Method *M, bool IgnoreNamed);// Start out in incorp state
inline ~SlotCalculator() {}
// getValSlot returns < 0 on error!
int getValSlot(const Value *D) const;
inline unsigned getNumPlanes() const { return Table.size(); }
inline unsigned getModuleLevel(unsigned Plane) const {
return Plane < ModuleLevel.size() ? ModuleLevel[Plane] : 0;
}
inline const TypePlane &getPlane(unsigned Plane) const {
return Table[Plane];
}
// If you'd like to deal with a method, use these two methods to get its data
// into the SlotCalculator!
//
void incorporateMethod(const Method *M);
void purgeMethod();
protected:
// insertVal - Insert a value into the value table...
//
void insertVal(const Value *D);
// visitMethod - This member is called after the constant pool has been
// processed. The default implementation of this is a noop.
//
virtual bool visitMethod(const Method *M);
// processConstant is called once per each constant in the constant pool. It
// traverses the constant pool such that it visits each constant in the
// order of its type. Thus, all 'int' typed constants shall be visited
// sequentially, etc...
//
virtual bool processConstant(const ConstPoolVal *CPV);
// processType - This callback occurs when an derived type is discovered
// at the class level. This activity occurs when processing a constant pool.
//
virtual bool processType(const Type *Ty);
// processMethods - The default implementation of this method loops through
// all of the methods in the module and processModule's them. We don't want
// this (we want to explicitly visit them with incorporateMethod), so we
// disable it.
//
virtual bool processMethods(const Module *M) { return false; }
// processMethodArgument - This member is called for every argument that
// is passed into the method.
//
virtual bool processMethodArgument(const MethodArgument *MA);
// processBasicBlock - This member is called for each basic block in a methd.
//
virtual bool processBasicBlock(const BasicBlock *BB);
// processInstruction - This member is called for each Instruction in a methd.
//
virtual bool processInstruction(const Instruction *I);
};
#endif
+182
View File
@@ -0,0 +1,182 @@
//===-- 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
// to a vector of unsigned char's, then copies the vector to an ostream. The
// 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. :( :( :(
//
// The choice of the vector data structure is influenced by the extremely fast
// "append" speed, plus the free "seek"/replace in the middle of the stream.
//
// 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"
#include "llvm/Method.h"
#include "llvm/BasicBlock.h"
#include "llvm/ConstPoolVals.h"
#include "llvm/SymbolTable.h"
#include "llvm/DerivedTypes.h"
#include <string.h>
#include <algorithm>
BytecodeWriter::BytecodeWriter(vector<unsigned char> &o, const Module *M)
: Out(o), Table(M, false) {
outputSignature();
// Emit the top level CLASS block.
BytecodeBlock ModuleBlock(BytecodeFormat::Module, Out);
// Output largest ID of first "primitive" type:
output_vbr((unsigned)Type::FirstDerivedTyID, Out);
align32(Out);
// Do the whole module now!
processModule(M);
// If needed, output the symbol table for the class...
if (M->hasSymbolTable())
outputSymbolTable(*M->getSymbolTable());
}
// TODO: REMOVE
#include "llvm/Assembly/Writer.h"
bool BytecodeWriter::processConstPool(const ConstantPool &CP, bool isMethod) {
BytecodeBlock *CPool = new BytecodeBlock(BytecodeFormat::ConstantPool, Out);
unsigned NumPlanes = Table.getNumPlanes();
for (unsigned pno = 0; pno < NumPlanes; pno++) {
const vector<const Value*> &Plane = Table.getPlane(pno);
if (Plane.empty()) continue; // Skip empty type planes...
unsigned ValNo = 0; // Don't reemit module constants
if (isMethod) ValNo = Table.getModuleLevel(pno);
unsigned NumConstants = 0;
for (unsigned vn = ValNo; vn < Plane.size(); vn++)
if (Plane[vn]->getValueType() == Value::ConstantVal)
NumConstants++;
if (NumConstants == 0) continue; // Skip empty type planes...
// Output type header: [num entries][type id number]
//
output_vbr(NumConstants, Out);
// 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);
//cerr << "NC: " << NumConstants << " Slot = " << hex << Slot << endl;
for (; ValNo < Plane.size(); ValNo++) {
const Value *V = Plane[ValNo];
if (V->getValueType() == Value::ConstantVal) {
//cerr << "Serializing value: <" << V->getType() << ">: "
// << ((const ConstPoolVal*)V)->getStrValue() << ":"
// << Out.size() << "\n";
outputConstant((const ConstPoolVal*)V);
}
}
}
delete CPool; // End bytecode block section!
if (!isMethod) { // The ModuleInfoBlock follows directly after the c-pool
assert(CP.getParent()->getValueType() == Value::ModuleVal);
outputModuleInfoBlock((const Module*)CP.getParent());
}
return false;
}
void BytecodeWriter::outputModuleInfoBlock(const Module *M) {
BytecodeBlock ModuleInfoBlock(BytecodeFormat::ModuleGlobalInfo, Out);
// Output the types of the methods in this class
Module::MethodListType::const_iterator I = M->getMethodList().begin();
while (I != M->getMethodList().end()) {
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);
I++;
}
output_vbr((unsigned)Table.getValSlot(Type::VoidTy), Out);
align32(Out);
}
bool BytecodeWriter::processMethod(const Method *M) {
BytecodeBlock MethodBlock(BytecodeFormat::Method, Out);
Table.incorporateMethod(M);
if (ModuleAnalyzer::processMethod(M)) return true;
// If needed, output the symbol table for the method...
if (M->hasSymbolTable())
outputSymbolTable(*M->getSymbolTable());
Table.purgeMethod();
return false;
}
bool BytecodeWriter::processBasicBlock(const BasicBlock *BB) {
BytecodeBlock MethodBlock(BytecodeFormat::BasicBlock, Out);
return ModuleAnalyzer::processBasicBlock(BB);
}
void BytecodeWriter::outputSymbolTable(const SymbolTable &MST) {
BytecodeBlock MethodBlock(BytecodeFormat::SymbolTable, Out);
for (SymbolTable::const_iterator TI = MST.begin(); TI != MST.end(); TI++) {
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);
for (; I != End; I++) {
// Symtab entry: [def slot #][name]
Slot = Table.getValSlot(I->second);
assert (Slot != -1 && "Value in symtab but not in method!!");
output_vbr((unsigned)Slot, Out);
output(I->first, Out, false); // Don't force alignment...
}
}
}
void WriteBytecodeToFile(const Module *C, ostream &Out) {
assert(C && "You can't write a null class!!");
vector<unsigned char> Buffer;
// This object populates buffer for us...
BytecodeWriter BCW(Buffer, C);
// Okay, write the vector out to the ostream now...
Out.write(&Buffer[0], Buffer.size());
Out.flush();
}
+74
View File
@@ -0,0 +1,74 @@
//===-- WriterInternals.h - Data structures shared by the Writer -*- C++ -*--=//
//
// This header defines the interface used between components of the bytecode
// writer.
//
// 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. :)
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_LIB_BYTECODE_WRITER_WRITERINTERNALS_H
#define LLVM_LIB_BYTECODE_WRITER_WRITERINTERNALS_H
#include "llvm/Bytecode/Writer.h"
#include "llvm/Bytecode/Format.h"
#include "llvm/Bytecode/Primitives.h"
#include "llvm/Analysis/SlotCalculator.h"
#include "llvm/Tools/DataTypes.h"
#include "llvm/Instruction.h"
class BytecodeWriter : public ModuleAnalyzer {
vector<unsigned char> &Out;
SlotCalculator Table;
public:
BytecodeWriter(vector<unsigned char> &o, const Module *M);
protected:
virtual bool processConstPool(const ConstantPool &CP, bool isMethod);
virtual bool processMethod(const Method *M);
virtual bool processBasicBlock(const BasicBlock *BB);
virtual bool processInstruction(const Instruction *I);
private :
inline void outputSignature() {
static const unsigned char *Sig = (const unsigned char*)"llvm";
Out.insert(Out.end(), Sig, Sig+4); // output the bytecode signature...
}
void outputModuleInfoBlock(const Module *C);
void outputSymbolTable(const SymbolTable &ST);
bool outputConstant(const ConstPoolVal *CPV);
void outputType(const Type *T);
};
// BytecodeBlock - Little helper class that helps us do backpatching of bytecode
// block sizes really easily. It backpatches when it goes out of scope.
//
class BytecodeBlock {
unsigned Loc;
vector<unsigned char> &Out;
BytecodeBlock(const BytecodeBlock &); // do not implement
void operator=(const BytecodeBlock &); // do not implement
public:
inline BytecodeBlock(unsigned ID, vector<unsigned char> &o) : Out(o) {
output(ID, Out);
output((unsigned)0, Out); // Reserve the space for the block size...
Loc = Out.size();
}
inline ~BytecodeBlock() { // Do backpatch when block goes out
// of scope...
// cerr << "OldLoc = " << Loc << " NewLoc = " << NewLoc << " diff = " << (NewLoc-Loc) << endl;
output((unsigned)(Out.size()-Loc), Out, (int)Loc-4);
align32(Out); // Blocks must ALWAYS be aligned
}
};
#endif