mirror of
https://github.com/c64scene-ar/llvm-6502.git
synced 2025-01-27 14:34:58 +00:00
Changes to the V2 bytecode format:
- Null values are implicitly encoded instead of explicitly, this makes things more compact! - More compactly represent ConstantPointerRefs - Bytecode files are represented as: Header|GlobalTypes|GlobalVars/Function Protos|Constants|Functions|SymTab instead of Header|GlobalTypes|Constants|GlobalVars/Function Protos|Functions|SymTab which makes a lot of things simpler. Writer changes: - We now explictly encode versioning information in the bytecode files. - This allows new code to read bytecode files produced by old code, but new bytecode files can have enhancements such as the above. Although this makes the reader a bit more complex (having to deal with old formats), the writer only needs to be able to produce the most recent version. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@5749 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
parent
52e20b0977
commit
186a1f71e6
@ -13,8 +13,6 @@
|
|||||||
#include "llvm/Constants.h"
|
#include "llvm/Constants.h"
|
||||||
#include "llvm/SymbolTable.h"
|
#include "llvm/SymbolTable.h"
|
||||||
#include "llvm/DerivedTypes.h"
|
#include "llvm/DerivedTypes.h"
|
||||||
#include <iostream>
|
|
||||||
using std::cerr;
|
|
||||||
|
|
||||||
void BytecodeWriter::outputType(const Type *T) {
|
void BytecodeWriter::outputType(const Type *T) {
|
||||||
output_vbr((unsigned)T->getPrimitiveID(), Out);
|
output_vbr((unsigned)T->getPrimitiveID(), Out);
|
||||||
@ -52,7 +50,7 @@ void BytecodeWriter::outputType(const Type *T) {
|
|||||||
int Slot = Table.getValSlot(AT->getElementType());
|
int Slot = Table.getValSlot(AT->getElementType());
|
||||||
assert(Slot != -1 && "Type used but not available!!");
|
assert(Slot != -1 && "Type used but not available!!");
|
||||||
output_vbr((unsigned)Slot, Out);
|
output_vbr((unsigned)Slot, Out);
|
||||||
//cerr << "Type slot = " << Slot << " Type = " << T->getName() << endl;
|
//std::cerr << "Type slot = " << Slot << " Type = " << T->getName() << endl;
|
||||||
|
|
||||||
output_vbr(AT->getNumElements(), Out);
|
output_vbr(AT->getNumElements(), Out);
|
||||||
break;
|
break;
|
||||||
@ -89,13 +87,15 @@ void BytecodeWriter::outputType(const Type *T) {
|
|||||||
|
|
||||||
//case Type::PackedTyID:
|
//case Type::PackedTyID:
|
||||||
default:
|
default:
|
||||||
cerr << __FILE__ << ":" << __LINE__ << ": Don't know how to serialize"
|
std::cerr << __FILE__ << ":" << __LINE__ << ": Don't know how to serialize"
|
||||||
<< " Type '" << T->getDescription() << "'\n";
|
<< " Type '" << T->getDescription() << "'\n";
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool BytecodeWriter::outputConstant(const Constant *CPV) {
|
bool BytecodeWriter::outputConstant(const Constant *CPV) {
|
||||||
|
assert((CPV->getType()->isPrimitiveType() || !CPV->isNullValue()) &&
|
||||||
|
"Shouldn't output null constants!");
|
||||||
|
|
||||||
// We must check for a ConstantExpr before switching by type because
|
// We must check for a ConstantExpr before switching by type because
|
||||||
// a ConstantExpr can be of any type, and has no explicit value.
|
// a ConstantExpr can be of any type, and has no explicit value.
|
||||||
@ -121,9 +121,9 @@ bool BytecodeWriter::outputConstant(const Constant *CPV) {
|
|||||||
switch (CPV->getType()->getPrimitiveID()) {
|
switch (CPV->getType()->getPrimitiveID()) {
|
||||||
case Type::BoolTyID: // Boolean Types
|
case Type::BoolTyID: // Boolean Types
|
||||||
if (cast<const ConstantBool>(CPV)->getValue())
|
if (cast<const ConstantBool>(CPV)->getValue())
|
||||||
output_vbr((unsigned)1, Out);
|
output_vbr(1U, Out);
|
||||||
else
|
else
|
||||||
output_vbr((unsigned)0, Out);
|
output_vbr(0U, Out);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case Type::UByteTyID: // Unsigned integer types...
|
case Type::UByteTyID: // Unsigned integer types...
|
||||||
@ -171,17 +171,11 @@ bool BytecodeWriter::outputConstant(const Constant *CPV) {
|
|||||||
|
|
||||||
case Type::PointerTyID: {
|
case Type::PointerTyID: {
|
||||||
const ConstantPointer *CPP = cast<const ConstantPointer>(CPV);
|
const ConstantPointer *CPP = cast<const ConstantPointer>(CPV);
|
||||||
if (isa<ConstantPointerNull>(CPP)) {
|
assert(!isa<ConstantPointerNull>(CPP) && "Null should be already emitted!");
|
||||||
output_vbr((unsigned)0, Out);
|
const ConstantPointerRef *CPR = cast<ConstantPointerRef>(CPP);
|
||||||
} else if (const ConstantPointerRef *CPR =
|
int Slot = Table.getValSlot((Value*)CPR->getValue());
|
||||||
dyn_cast<ConstantPointerRef>(CPP)) {
|
assert(Slot != -1 && "Global used but not available!!");
|
||||||
output_vbr((unsigned)1, Out);
|
output_vbr((unsigned)Slot, Out);
|
||||||
int Slot = Table.getValSlot((Value*)CPR->getValue());
|
|
||||||
assert(Slot != -1 && "Global used but not available!!");
|
|
||||||
output_vbr((unsigned)Slot, Out);
|
|
||||||
} else {
|
|
||||||
assert(0 && "Unknown ConstantPointer Subclass!");
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -199,8 +193,8 @@ bool BytecodeWriter::outputConstant(const Constant *CPV) {
|
|||||||
case Type::VoidTyID:
|
case Type::VoidTyID:
|
||||||
case Type::LabelTyID:
|
case Type::LabelTyID:
|
||||||
default:
|
default:
|
||||||
cerr << __FILE__ << ":" << __LINE__ << ": Don't know how to serialize"
|
std::cerr << __FILE__ << ":" << __LINE__ << ": Don't know how to serialize"
|
||||||
<< " type '" << CPV->getType()->getName() << "'\n";
|
<< " type '" << CPV->getType()->getName() << "'\n";
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
|
@ -43,19 +43,36 @@ BytecodeWriter::BytecodeWriter(std::deque<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 the ID of first "derived" type:
|
bool isBigEndian = true;
|
||||||
output_vbr((unsigned)Type::FirstDerivedTyID, Out);
|
bool hasLongPointers = true;
|
||||||
|
|
||||||
|
// Output the version identifier... we are currently on bytecode version #1
|
||||||
|
unsigned Version = (1 << 4) | isBigEndian | (hasLongPointers << 1);
|
||||||
|
output_vbr(Version, Out);
|
||||||
align32(Out);
|
align32(Out);
|
||||||
|
|
||||||
// Output module level constants, including types used by the function protos
|
{
|
||||||
outputConstants(false);
|
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
|
||||||
|
}
|
||||||
|
|
||||||
// The ModuleInfoBlock follows directly after the Module constant pool
|
// The ModuleInfoBlock follows directly after the type information
|
||||||
outputModuleInfoBlock(M);
|
outputModuleInfoBlock(M);
|
||||||
|
|
||||||
|
// Output module level constants, used for global variable initializers
|
||||||
|
outputConstants(false);
|
||||||
|
|
||||||
// Do the whole module now! Process each function at a time...
|
// Do the whole module now! Process each function at a time...
|
||||||
for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
|
for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
|
||||||
processMethod(I);
|
outputFunction(I);
|
||||||
|
|
||||||
// If needed, output the symbol table for the module...
|
// If needed, output the symbol table for the module...
|
||||||
outputSymbolTable(M->getSymbolTable());
|
outputSymbolTable(M->getSymbolTable());
|
||||||
@ -68,8 +85,9 @@ void BytecodeWriter::outputConstantsInPlane(const std::vector<const Value*>
|
|||||||
&Plane, unsigned StartNo) {
|
&Plane, unsigned StartNo) {
|
||||||
unsigned ValNo = StartNo;
|
unsigned ValNo = StartNo;
|
||||||
|
|
||||||
// Scan through and ignore function arguments...
|
// Scan through and ignore function arguments/global values...
|
||||||
for (; ValNo < Plane.size() && isa<Argument>(Plane[ValNo]); ValNo++)
|
for (; ValNo < Plane.size() && (isa<Argument>(Plane[ValNo]) ||
|
||||||
|
isa<GlobalValue>(Plane[ValNo])); ValNo++)
|
||||||
/*empty*/;
|
/*empty*/;
|
||||||
|
|
||||||
unsigned NC = ValNo; // Number of constants
|
unsigned NC = ValNo; // Number of constants
|
||||||
@ -98,7 +116,7 @@ void BytecodeWriter::outputConstantsInPlane(const std::vector<const Value*>
|
|||||||
// << Out.size() << "\n";
|
// << Out.size() << "\n";
|
||||||
outputConstant(CPV);
|
outputConstant(CPV);
|
||||||
} else {
|
} else {
|
||||||
outputType(cast<const Type>(V));
|
outputType(cast<Type>(V));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -108,26 +126,21 @@ void BytecodeWriter::outputConstants(bool isFunction) {
|
|||||||
|
|
||||||
unsigned NumPlanes = Table.getNumPlanes();
|
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<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
|
|
||||||
}
|
|
||||||
|
|
||||||
for (unsigned pno = 0; pno != NumPlanes; pno++) {
|
for (unsigned pno = 0; pno != NumPlanes; pno++) {
|
||||||
const std::vector<const Value*> &Plane = Table.getPlane(pno);
|
const std::vector<const Value*> &Plane = Table.getPlane(pno);
|
||||||
if (!Plane.empty()) { // Skip empty type planes...
|
if (!Plane.empty()) { // Skip empty type planes...
|
||||||
unsigned ValNo = 0;
|
unsigned ValNo = 0;
|
||||||
if (isFunction) // Don't reemit module constants
|
if (isFunction) // Don't reemit module constants
|
||||||
ValNo = Table.getModuleLevel(pno);
|
ValNo += Table.getModuleLevel(pno);
|
||||||
else if (pno == Type::TypeTyID) // If type plane wasn't written out above
|
else if (pno == Type::TypeTyID) // If type plane wasn't written out above
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
|
if (pno >= Type::FirstDerivedTyID) {
|
||||||
|
// Skip zero initializer
|
||||||
|
if (ValNo == 0)
|
||||||
|
ValNo = 1;
|
||||||
|
}
|
||||||
|
|
||||||
outputConstantsInPlane(Plane, ValNo); // Write out constants in the plane
|
outputConstantsInPlane(Plane, ValNo); // Write out constants in the plane
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -142,7 +155,7 @@ void BytecodeWriter::outputModuleInfoBlock(const Module *M) {
|
|||||||
assert(Slot != -1 && "Module global vars is broken!");
|
assert(Slot != -1 && "Module global vars is broken!");
|
||||||
|
|
||||||
// Fields: bit0 = isConstant, bit1 = hasInitializer, bit2=InternalLinkage,
|
// Fields: bit0 = isConstant, bit1 = hasInitializer, bit2=InternalLinkage,
|
||||||
// bit3+ = slot#
|
// bit3+ = Slot # for type
|
||||||
unsigned oSlot = ((unsigned)Slot << 3) | (I->hasInternalLinkage() << 2) |
|
unsigned oSlot = ((unsigned)Slot << 3) | (I->hasInternalLinkage() << 2) |
|
||||||
(I->hasInitializer() << 1) | I->isConstant();
|
(I->hasInitializer() << 1) | I->isConstant();
|
||||||
output_vbr(oSlot, Out);
|
output_vbr(oSlot, Out);
|
||||||
@ -165,11 +178,10 @@ void BytecodeWriter::outputModuleInfoBlock(const Module *M) {
|
|||||||
}
|
}
|
||||||
output_vbr((unsigned)Table.getValSlot(Type::VoidTy), Out);
|
output_vbr((unsigned)Table.getValSlot(Type::VoidTy), Out);
|
||||||
|
|
||||||
|
|
||||||
align32(Out);
|
align32(Out);
|
||||||
}
|
}
|
||||||
|
|
||||||
void BytecodeWriter::processMethod(const Function *F) {
|
void BytecodeWriter::outputFunction(const Function *F) {
|
||||||
BytecodeBlock FunctionBlock(BytecodeFormat::Function, Out);
|
BytecodeBlock FunctionBlock(BytecodeFormat::Function, Out);
|
||||||
output_vbr((unsigned)F->hasInternalLinkage(), Out);
|
output_vbr((unsigned)F->hasInternalLinkage(), Out);
|
||||||
// Only output the constant pool and other goodies if needed...
|
// Only output the constant pool and other goodies if needed...
|
||||||
|
@ -25,8 +25,8 @@ public:
|
|||||||
BytecodeWriter(std::deque<unsigned char> &o, const Module *M);
|
BytecodeWriter(std::deque<unsigned char> &o, const Module *M);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
void outputConstants(bool isMethod);
|
void outputConstants(bool isFunction);
|
||||||
void processMethod(const Function *F);
|
void outputFunction(const Function *F);
|
||||||
void processBasicBlock(const BasicBlock &BB);
|
void processBasicBlock(const BasicBlock &BB);
|
||||||
void processInstruction(const Instruction &I);
|
void processInstruction(const Instruction &I);
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user